PatchworkOS
Loading...
Searching...
No Matches

Directory entry. More...

Data Structures

struct  dentry_ops_t
 Dentry operations structure. More...
 
struct  dentry_t
 Directory entry structure. More...
 

Macros

#define DENTRY_IS_ROOT(dentry)   (dentry->parent == dentry)
 Macro to check if a dentry is the root entry in its filesystem.
 

Typedefs

typedef uint64_t dentry_id_t
 Dentry ID type.
 

Enumerations

enum  dentry_flags_t {
  DENTRY_NONE = 0 ,
  DENTRY_NEGATIVE = 1 << 1
}
 Flags for a dentry. More...
 

Functions

dentry_tdentry_new (superblock_t *superblock, dentry_t *parent, const char *name)
 Create a new dentry.
 
uint64_t dentry_make_positive (dentry_t *dentry, inode_t *inode)
 Make a dentry positive by associating it with an inode.
 
void dentry_inc_mount_count (dentry_t *dentry)
 Increments the mount count of a dentry.
 
void dentry_dec_mount_count (dentry_t *dentry)
 Decrements the mount count of a dentry.
 
uint64_t dentry_generic_getdents (dentry_t *dentry, dirent_t *buffer, uint64_t count, uint64_t *offset, path_flags_t flags)
 Helper function for a basic getdents.
 

Detailed Description

Directory entry.

A dentry represents the actual name in the filesystem hierarchy. It can be either positive, meaning it has an associated inode, or negative, meaning it does not have an associated inode. When traversing a filesystem this is the thing you actually walk through.

Macro Definition Documentation

◆ DENTRY_IS_ROOT

#define DENTRY_IS_ROOT (   dentry)    (dentry->parent == dentry)

Macro to check if a dentry is the root entry in its filesystem.

A dentry is considered the root if its parent is itself.

Parameters
dentryThe dentry to check.
Returns
true if the dentry is the root, false otherwise.

Definition at line 43 of file dentry.h.

Typedef Documentation

◆ dentry_id_t

Dentry ID type.

Definition at line 33 of file dentry.h.

Enumeration Type Documentation

◆ dentry_flags_t

Flags for a dentry.

Enumerator
DENTRY_NONE 

No flags.

DENTRY_NEGATIVE 

This dentry is negative, meaning it does not have an associated inode.

A negative dentry is created when a lookup for a name in a directory fails, it is used to cache the fact that the name does not exist in that directory and also allows us to avoid race conditions where two processes try to create the same file at the same time.

Definition at line 49 of file dentry.h.

Function Documentation

◆ dentry_dec_mount_count()

void dentry_dec_mount_count ( dentry_t dentry)

Decrements the mount count of a dentry.

Parameters
dentryThe dentry to decrement the mount count of.

Definition at line 133 of file dentry.c.

References atomic_fetch_sub, and dentry_t::mountCount.

Referenced by mount_free().

◆ dentry_generic_getdents()

uint64_t dentry_generic_getdents ( dentry_t dentry,
dirent_t buffer,
uint64_t  count,
uint64_t offset,
path_flags_t  flags 
)

Helper function for a basic getdents.

This function can be used by filesystems that do not have any special requirements for getdents.

In practice this is only useful for in-memory filesystems.

Used by setting the dentry ops getdents to this function.

Definition at line 195 of file dentry.c.

References getdents_ctx_t::basePath, buffer, dentry_t::children, dentry_t::childrenMutex, count, getdents_recursive_traversal(), getdents_write(), getdents_ctx_t::index, dentry_t::inode, INODE_DIR, LIST_FOR_EACH, MAX_PATH, MIN, MUTEX_SCOPE, dentry_t::name, inode_t::number, dentry_t::parent, PATH_RECURSIVE, snprintf(), start(), and inode_t::type.

◆ dentry_inc_mount_count()

void dentry_inc_mount_count ( dentry_t dentry)

Increments the mount count of a dentry.

Parameters
dentryThe dentry to increment the mount count of.

Definition at line 128 of file dentry.c.

References atomic_fetch_add, and dentry_t::mountCount.

Referenced by mount_new().

◆ dentry_make_positive()

uint64_t dentry_make_positive ( dentry_t dentry,
inode_t inode 
)

Make a dentry positive by associating it with an inode.

This will also add the dentry to its parent's list of children, set the dentrys inode and its flags to positive.

Parameters
dentryThe dentry to make positive.
inodeThe inode to associate with the dentry.

Definition at line 101 of file dentry.c.

References assert, atomic_fetch_and, atomic_load, dentry_t::children, dentry_t::childrenMutex, DENTRY_IS_ROOT, DENTRY_NEGATIVE, EINVAL, ERR, errno, dentry_t::inode, list_push(), MUTEX_SCOPE, NULL, dentry_t::parent, REF, and dentry_t::siblingEntry.

Referenced by ramfs_create(), ramfs_link(), ramfs_load_dir(), ramfs_load_file(), sysfs_dir_new(), sysfs_file_new(), sysfs_mount(), and sysfs_mount_new().

◆ dentry_new()

dentry_t * dentry_new ( superblock_t superblock,
dentry_t parent,
const char *  name 
)

Create a new dentry.

Will not add the dentry to its parent's list of children but it will appear in the dentry cache as a negative dentry until dentry_make_positive() is called making it positive. This is needed to solve some race conditions when creating new files. While the dentry is negative it is not possible to create another dentry of the same name in the same parent, and any lookup to the dentry will fail until it is made positive.

Note that this function will set errno == EEXIST if a dentry with the same name and parent already exists in the dentry cache, this is very useful for solving race conditions when looking up dentries.

There is no dentry_free() instead use DEREF().

Parameters
superblockThe superblock the dentry belongs to.
parentThe parent dentry, can be NULL if this is a root dentry.
nameThe name of the dentry.
Returns
On success, the new dentry. On failure, returns NULL and errno is set.

Definition at line 51 of file dentry.c.

References assert, atomic_init, dentry_t::children, dentry_t::childrenMutex, dentry_free(), DENTRY_NEGATIVE, superblock_t::dentryOps, DEREF, EINVAL, ERR, errno, dentry_t::id, dentry_t::inode, list_entry_init(), list_init(), malloc(), map_entry_init(), dentry_t::mapEntry, MAX_NAME, dentry_t::mountCount, mutex_init(), dentry_t::name, NULL, dentry_t::ops, dentry_t::parent, dentry_t::private, dentry_t::ref, REF, ref_init(), dentry_t::siblingEntry, strncpy(), strnlen_s(), dentry_t::superblock, vfs_add_dentry(), and vfs_get_new_id().

Referenced by ramfs_load_dir(), ramfs_load_file(), sysfs_dir_new(), sysfs_file_new(), sysfs_mount(), sysfs_mount_new(), and vfs_get_or_lookup_dentry().