Standard Functions

Several auxiliary functions that ease handling dentry objects are provided by the kernel. Their implementation is mostly an exercise in list management and data structure handling, so I won't bother to discuss their code. It is, however, important to show their prototypes and describe their effect since we will come across them frequently in discussing implementation of VFS operations. The following auxiliary functions require a pointer to struct dentry as parameter. Each performs one simple operation.

□ dget needs to be called whenever a dentry instance is put to use by some part of the kernel. Calling dget increments the reference count of the object; that is, it acquires a reference to it.

□ dput is the counterpart to dget: It must be called when a dentry instance is not required any more by a user in the kernel.

The function decrements the usage count of a dentry object. If the count drops to zero, the dentry_operations->d_delete method is called if it is available. Additionally, the instance is unhashed from the global dentry hash using d_drop, and also taken away from the LRU list and put on the unused list.

If the object is not contained in the hash when dput is called, it is deleted from memory via kfree.

□ d_drop unhashes a dentry instance from the global dentry hash tables. It is automatically called from dput if the usage count drops to zero, but can also be called manually if a dentry cache object needs to be invalidated. __d_drop is a variant of d_drop that does not automatically handle locking.

□ d_delete unhashes a dentry object using d drop if it is still contained on the global dentry hash tables. If only one user for the object remains, dentry_iput is also called to decrement the usage count of the inode associated with the dentry object.

d_delete is usually called immediately before dput. This ensures that the dentry object will be erased by dput since it is not on the global dentry hash anymore.

Some helper functions are more complicated, so it's best to inspect their prototypes.12 <dcache.h>

extern void d_instantiate(struct dentry *, struct inode *);

struct

dentry

*

d_alloc(struct dentry *, const struct

qstr *);

struct

dentry

*

d_alloc_anon(struct inode *);

struct

dentry

*

d_splice_alias(struct inode *, struct

dentry *);

static

inline

void d_add(struct dentry *entry, struct

inode *inode)

struct

dentry

*

d_lookup(struct dentry *, struct qstr

□ d_instantiate associates a dentry instance with an inode. This means setting the d_inode field and adding the dentry to the list headed by inode->i_dentry.

□ d_add instantiates a dentry object by using d_instantiate. Additionally, the object is added to the global inode hash table dentry_hashtable.

□ d_alloc allocates memory for a new instance of struct dentry as the name does suggest. The fields are initialized, and if a parent dentry is given, the superblock pointer for the new dentry is taken from the parent. Additionally, the new dentry is added to the subdirectory list of the parent headed by parent->d_subdirs.

□ d_alloc_anon allocates memory for an instance of struct dentry but does not set up any connections with a parent dentry — this is why no such parameter is required in contrast to d_alloc. The new dentry is added to two lists: the superblock-specific list of anonymous dentry objects headed by super_block->s_anon and the list of all dentry instances associated with the inode, which is headed by inode->i_dentry.

Note that if the inode already contains a disconnected dentry as allocated by a previous call to d_alloc_anon, this copy is used instead of creating a new instance.

□ d_splice_alias splices a disconnected dentry into the dentry tree. The inode parameter required by the function denotes the inode to which the dentry is supposed to be associated.

For inodes that represent any filesystem object other than directories, it suffices to call d_add. For directories, the function ensures that only a single dentry alias is present, which requires some more administrative work that I won't bother to discuss in detail.

□ d_lookup takes the dentry instance of a directory and searches for a dentry object that represents a file with name.

Continue reading here: Working with VFS Objects

Was this article helpful?

0 0