Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 10: Filesystems without Persistent Storage


inode->i_op = &simple_dir_inode_operations;
inode->i_fop = &simple_dir_operations;

/* directory inodes start off with i_nlink == 2
* (for "." entry) */
inc_nlink(inode);
break;
}
}
return inode;
}

Besides allocating a new instance ofstruct inode, the kernel needs to decide which file and inode oper-
ations are to be associated with the file depending on the information in the access mode. For device
special files, the standard routineinit_special_file(not connected with libfs) is used. The more inter-
esting cases, however, are regular files and directories. Directories require the standard file and inode
operations as discussed above; this ensures with nofurther effort that the new directory is correctly
handled.

Regular files cannot be provided with boilerplate file operations. It is at least necessary to manually
specify theread,write,andopenmethods.readis supposed to prepare data from kernel memory and
copy them into userspace, whilewritecan be used to read input from the user and apply it somehow.
This is all that is required to implement custom files!

A filesystem also requires a superblock. Thankfully for lazy programmers, libfs provides the method
simple_fill_super, that can be used to fill in a given superblock:

<fs.h>
int simple_fill_super(struct super_block *s, int magic, struct tree_descr *files);

sis the superblock in question, andmagicspecifies a unique magic number which can be used to identify
the filesystem. Thefilesparameter provides a very convenient method to populate the virtual filesystem
with what it is supposed to contain: files! Unfortunately, only files in a single directory can be specified
with this method, but this is not a real limitation for virtual filesystems. More content can still be added
later dynamically.

An array withstruct tree_descrelements is used to describe the initial set of files. The structure is
defined as follows:

<fs.h>
struct tree_descr {
char *name;
const struct file_operations *ops;
int mode;
};

namedenotes the filename,opspoints to the associated file operations, andmodespecifies the access bits.

The last entry in the list must be of the form{ "", NULL, 0 }.
Free download pdf