Zbr's days.

About :: TODO :: Blog :: RSS :: Old blog :: Projects :: GIT :: Gallery :: Notes

Fri, 18 Jan 2008

Anatomy of the filesystem. Object creation and removal.

Let's first discuss object creation. It is pretty simple, each directory inode has inode_operations structure, which contains ->create()/->mkdir() callbacks. Prototype of both looks like this:

static int pohmelfs_create(struct inode *dir, struct dentry *dentry, int mode,
		struct nameidata *nd);
static int pohmelfs_mkdir(struct inode *dir, struct dentry *dentry, int mode);
Where dir is parent directory inode, dentry is directory entry structure, which contains inode for given object (dentry->d_inode, it is NULL for the object being created, since there is no inode yet for the given dentry), its name (dentry->d_name) and lots of other interesting fields, which are not that interesting for filesystem creation. FS code should allocate space for the new entry and add it there.
At the end one has to fill dentry with new inode info, it can be done either by d_add(dentry, &npi->vfs_inode);, or more correct by d_instantiate(dentry, &npi->vfs_inode);, which is called from d_add(), which then adds dentry into hash chains. Ext2 also multiple times marks inode as dirty, the same does minix. This operation has no effect on network filesystem, afaics, but for block based filesystems it adds inode into dirty list. However, practice shows that d_instantiate(dentry, &npi->vfs_inode); is not enough, and d_add(dentry, &npi->vfs_inode); should be called for network filesystem.

Object removal is essentially the same. There are following callbacks invoked by VFS layer, when object is being deleted: ->unlink() and ->rmdir().
The former is called for usual files, nodes and so on, the latter - when you call rmdir(). Both have following prototype:
static int pohmelfs_unlink(struct inode *dir, struct dentry *dentry);
static int pohmelfs_rmdir(struct inode *dir, struct dentry *dentry);
Where dir is parent directory inode and dentry contains directory entry, which in turn has inode pointer and name of the object.
Filesystem should remove appropriate object from the disk, update its fields and mainly offsets, used in the ->readdir() callbacks.

All described callbacks should return negative error value or zero in case of correct completion.

/devel/fs :: Link / Comments ()