Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 10: Filesystems without Persistent Storage


To understand the idea of libfs better, let’s discuss the way directory handling is implemented. Boilerplate
instances of inode and file operations for directories are provided that can immediately be reused for any
virtual filesystem implemented along the lines of libfs:


fs/libfs.c

const struct file_operations simple_dir_operations = {
.open = dcache_dir_open,
.release = dcache_dir_close,
.llseek = dcache_dir_lseek,
.read = generic_read_dir,
.readdir = dcache_readdir,
.fsync = simple_sync_file,
};

const struct inode_operations simple_dir_inode_operations = {
.lookup = simple_lookup,
};

In contrast to the convention introduced above, the names of the routines that make up
simple_diroperationsdonotstartwithsimple. Nevertheless, they are defined infs/libfs.c.The
nomenclature reflects that the operations solely operate on objects from the dentry cache.


If a virtual filesystem sets up a proper dentry tree, it suffices to installsimple_dir_operationsand
simple_dir_inode_operationsas file or inode operations, respectively, for directories. The libfs func-
tions then ensure that the information contained on the tree is exported to userland via the standard
system calls likegetdents. Since constructing one representation from another is basically a mechanical
task, the source code is not discussed in detail.


Instead, it is more interesting to observe how new files are added to a virtual filesystem. Debugfs (dis-
cussed below) is one filesystem that employs libfs. New files (and thus new inodes) are created with the
following routine:


fs/debugfs/inode.c
static struct inode *debugfs_get_inode(struct super_block *sb, int mode, dev_t dev)
{
struct inode *inode = new_inode(sb);

if (inode) {
inode->i_mode = mode;
inode->i_uid = 0;
inode->i_gid = 0;
inode->i_blocks = 0;
inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
switch (mode & S_IFMT) {
default:
init_special_inode(inode, mode, dev);
break;
case S_IFREG:
inode->i_fop = &debugfs_file_operations;
break;
...
case S_IFDIR:
Free download pdf