从代码层次深入分析文件系统

(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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Linux技术芯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值