VFS之mount函数分析

来源:互联网 发布:网络推广人员工资高吗 编辑:程序博客网 时间:2024/05/01 02:22

linux混混之牢骚:

在linux混的久了,看见凤姐都觉得眉清目秀的。以前总以看尽天下A片,心中自然无码作为人生的追求。却在党国的熏陶下成功的变成了linux程序员,诶......少壮不努力,死在程序里!至理名言啊~~~~ 日子还要过下去,生活总归要继续,我的武藤兰,我的饭岛爱、我的南宫优子,我的小泽玛利亚、我的松岛枫、我的吉泽明步、我的柚木提娜,我要分析VFS了。。。。。

 

linux version: 2.6.39

mount命令调用关系:

mount > /fs/namespace/SYSCALL_DEFINE5(mount,....); > do_mount > do_new_mount

程序分析:

 do_kern_mount

struct vfsmount *do_kern_mount(const char *fstype, int flags, const char *name, void *data){ struct file_system_type *type = get_fs_type(fstype); struct vfsmount *mnt; if (!type)  return ERR_PTR(-ENODEV); mnt = vfs_kern_mount(type, flags, name, data);    /*初始化vfsmount结构,并对其进行赋值*/ if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) &&     !mnt->mnt_sb->s_subtype)  mnt = fs_set_subtype(mnt, fstype); put_filesystem(type); return mnt;}

do_kern_mount 主要调用了vfs_kern_mount()和fs_set_subtype()

 

vfs_kern_mount

vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data){struct vfsmount *mnt;struct dentry *root;if (!type)return ERR_PTR(-ENODEV);mnt = alloc_vfsmnt(name);                //主要是allocate一个vfsmount 的空间,并对vfsmount 进行初始化。(???调用kmem_cache_zalloc申请,具体是个什么样的空间?)if (!mnt)return ERR_PTR(-ENOMEM);if (flags & MS_KERNMOUNT)mnt->mnt_flags = MNT_INTERNAL;root = mount_fs(type, flags, name, data); //调用具体的fs来mount,并得到vfs需要的dentry。if (IS_ERR(root)) {free_vfsmnt(mnt);return ERR_CAST(root);}mnt->mnt_root = root;mnt->mnt_sb = root->d_sb;mnt->mnt_mountpoint = mnt->mnt_root;mnt->mnt_parent = mnt;                   //对dentry进行赋值return mnt;}

有此可见,vfs_kern_mount函数功能: 申请vfsmount结构,并调用到具体的相应文件系统的mount函数,相应的文件系统mount返回一个super_block的结构,vfs对其进行处理初步得到dentry结构, 最后用dentry结构对vfsmount结构的成员变量进行初步赋值(后面还会对vfsmount进行修改)。

 

以下大致分析vfs_kern_mount函数的调用关系:

mount_fs

struct dentry *mount_fs(struct file_system_type *type, int flags, const char *name, void *data){struct dentry *root;struct super_block *sb;char *secdata = NULL;int error = -ENOMEM;if (data && !(type->fs_flags & FS_BINARY_MOUNTDATA)) {secdata = alloc_secdata();if (!secdata)goto out;error = security_sb_copy_data(data, secdata);    /*该函数在security/security.c中,可以看一下该目录下的Makefile和KCONFIG文件,了解该模块的用。大概只是安全的作用*/if (error)goto out_free_secdata;}root = type->mount(type, flags, name, data);          //调用file_system_type中,mount。file_system在register_filesystem函数中对特定文件系统进行赋值。if (IS_ERR(root)) {error = PTR_ERR(root);goto out_free_secdata;}sb = root->d_sb;BUG_ON(!sb);WARN_ON(!sb->s_bdi);WARN_ON(sb->s_bdi == &default_backing_dev_info);sb->s_flags |= MS_BORN;error = security_sb_kern_mount(sb, flags, secdata);if (error)goto out_sb;/* * filesystems should never set s_maxbytes larger than MAX_LFS_FILESIZE * but s_maxbytes was an unsigned long long for many releases. Throw * this warning for a little while to try and catch filesystems that * violate this rule. This warning should be either removed or * converted to a BUG() in 2.6.34. */WARN((sb->s_maxbytes < 0), "%s set sb->s_maxbytes to ""negative value (%lld)\n", type->name, sb->s_maxbytes);up_write(&sb->s_umount);free_secdata(secdata);return root;out_sb:dput(root);deactivate_locked_super(sb);out_free_secdata:free_secdata(secdata);out:return ERR_PTR(error);}

 

 fs_set_subtype

static struct vfsmount *fs_set_subtype(struct vfsmount *mnt, const char *fstype){int err;const char *subtype = strchr(fstype, '.');if (subtype) {subtype++;err = -EINVAL;if (!subtype[0])goto err;} elsesubtype = "";mnt->mnt_sb->s_subtype = kstrdup(subtype, GFP_KERNEL);    //函数主要就是对 赋值文件系统类型。err = -ENOMEM;if (!mnt->mnt_sb->s_subtype)goto err;return mnt; err:mntput(mnt);return ERR_PTR(err);}

do_kern_mount函数分析完成。

do_add_mount

do_add_mount主要是将前面生成的vfsmount的结构放在一些链表中。

do_add_mount算法等比较多,就不分析了。 贴下代码,以后有空再做:

/* * add a mount into a namespace's mount tree */static int do_add_mount(struct vfsmount *newmnt, struct path *path, int mnt_flags){int err;mnt_flags &= ~(MNT_SHARED | MNT_WRITE_HOLD | MNT_INTERNAL);err = lock_mount(path);if (err)return err;err = -EINVAL;if (!(mnt_flags & MNT_SHRINKABLE) && !check_mnt(path->mnt))goto unlock;/* Refuse the same filesystem on the same mount point */err = -EBUSY;if (path->mnt->mnt_sb == newmnt->mnt_sb &&    path->mnt->mnt_root == path->dentry)goto unlock;err = -EINVAL;if (S_ISLNK(newmnt->mnt_root->d_inode->i_mode))goto unlock;newmnt->mnt_flags = mnt_flags;err = graft_tree(newmnt, path);               //这个函数为主要函数,将vfsmount添加到tree中。unlock:unlock_mount(path);return err;}

 

总结:

在mount过程中,主要的操作就是建立vfsmount结构,并将其添加到各种list中。

 

原创粉丝点击