(1)一个最简单的文件系统aufs
整个程序代码可以分为三部分。
1)首先是register_filesystem函数,这个函数把aufs文件系统登记到系统;
2)然后调用kern_mount函数为文件系统申请必备的数据结构;
3)最后在aufs文件系统内创建两目录,每个目录下面创建三个文件。
static struct vfsmount *aufs_mount;
static struct file_system_type au_fs_type = {
.owner = THIS_MODULE,
.name = "aufs",
.get_sb = aufs_get_sb,
.kill_sb = kill_litter_super,
};
static int __init aufs_init(void)
{
int retval;
struct dentry *pslot;
/* 将文件系统登记到系统 */
retval = register_filesystem(&au_fs_type);
if (!retval) {
/* 创建super_block 根dentry 根inode */
aufs_mount = kern_mount(&au_fs_type);
/* kern_mount错误就卸载文件系统 */
if (IS_ERR(aufs_mount)) {
printk(KERN_ERR "aufs: could not mount!\n");
unregister_filesystem(&au_fs_type);
return retval;
}
}
/* 创建目录和目录下的几个文件 */
pslot = aufs_create_dir("woman star",NULL);
aufs_create_file("lbb", S_IFREG | S_IRUGO, pslot, NULL, NULL);
aufs_create_file("fbb", S_IFREG | S_IRUGO, pslot, NULL, NULL);
aufs_create_file("ljl", S_IFREG | S_IRUGO, pslot, NULL, NULL);
pslot = aufs_create_dir("man star",NULL);
aufs_create_file("ldh", S_IFREG | S_IRUGO, pslot, NULL, NULL);
aufs_create_file("lcw", S_IFREG | S_IRUGO, pslot, NULL, NULL);
aufs_create_file("jw", S_IFREG | S_IRUGO, pslot, NULL, NULL);
return retval;
}
内核定义一个全局变量file_systems,用来保存所有登记的文件系统,而find_filesystem利用全局变量file_systems执行了具体的查找过程。
kern_mount函数真正为文件系统分配了超级块对象和vfsmount对象。
struct vfsmount *kern_mount_data(struct file_system_type *type, void *data)
{
struct vfsmount *mnt;
mnt = vfs_kern_mount(type, MS_KERNMOUNT, type->name, data);
}
//为文件系统创建了一个vfsmount结构
struct vfsmount *
vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data)
{
struct mount *mnt;
struct dentry *root;
mnt = alloc_vfsmnt(name);
root = mount_fs(type, flags, name, data);
mnt->mnt.mnt_root = root;
mnt->mnt.mnt_sb = root->d_sb;
mnt->mnt_mountpoint = mnt->mnt.mnt_root;
mnt->mnt_parent = mnt;
br_write_lock(&vfsmount_lock);
list_add_tail(&mnt->mnt_instance, &root->d_sb->s_mounts);
br_write_unlock(&vfsmount_lock);
return &mnt->mnt;
}