mount过程分析之七(do_add_mount)

上面六篇文章简要的略过了mount过程如何进入内核,通过VFS层,进入到特定文件系统专有的mount处理函数,读取super block构建struct mount结构体后返回的过程。现在我们得到了一个struct mount挂载实例,接下来要做的就是把这个新的装载实例加入到全局文件系统树中。


sys_mount - > do_mount -> do_new_mount(续)

上面在do_new_mount后沿着vfs_kern_mount -> mount_fs -> xfs_fs_mount -> mount_bdev -> xfs_fs_fill_super的顺序过了一遍,看到了mount的时候是如何利用文件系统提供的mount方法构造出一个Struct mount结构的。现在回到do_new_mount中vfs_kern_mount函数返回的地方,如下:

static int do_new_mount(struct path *path, const char *fstype, int flags,
                        int mnt_flags, const char *name, void *data)
{
        struct file_system_type *type;
        struct user_namespace *user_ns = current->nsproxy->mnt_ns->user_ns;
        struct vfsmount *mnt;
        int err;

//....
//....

        // 得到了struct mount结构体,返回其成员vfsmount
        mnt = vfs_kern_mount(type, flags, name, data);
        // 如果此文件系统还有子类型(多见于FUSE),设置子文件系统类型名
        if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) &&
            !mnt->mnt_sb->s_subtype)
                mnt = fs_set_subtype(mnt, fstype);

        // 就此file_system_type用完了(其mount回调函数完成了构建struct mount的任务)
        put_filesystem(type);
        if (IS_ERR(mnt))
                return PTR_ERR(mnt);

        // 准备将得到的mnt结构加入全局文件系统树
        // 注意path变量,也就是mountpoint在这里
        err = do_add_mount(real_mount(mnt), path, mnt_flags);
        if (err)
                mntput(mnt);
        return err;
}


可以看到vfs_kern_mount返回了一个struct vfsmount结构体的指针,我们上面说了vfsmount是mount结构体中的一个成员(具体参见前面struct mount和struct vfsmount的定义)。
在说do_add_mount前我们先来说一下这里为什么要返回vfsmount这个mount结构体的成员,而不是直接返回mount结构体。对于了解gcc特性的人其实不难理解,linux借助了gcc的一个特性,这个特性可以根据一个结构体的成员计算出这个结构体的首指针。具体可以参见内核中的container_of()函数的实现,或者直接查阅typeof这个gcc提供的关键字。来看do_add_mount的第一个参数real_mount(mnt),它的实现就是借助了container_of(),如下:
static inline struct mount *real_mount(struct vfsmount *mnt)
{
        return container_of(mnt, struct mount, mnt);
}

它的意思就是根据struct mount中的mnt成员,计算出mnt这个变量所属的struct mount结构体的地址并返回struct mount的地址。换句话说它就是得到我们上面一直构建出的struct mount结构的首地址。至于为什么要先返回mount的成员,再反过来计算得到mount的地址,这就是另一个辩论不清的问题了。也许是历史原因(以前没有struct mount结构体),或者作者偏好;)

sys_mount - > do_mount -> do_new_mount ->

  • 11
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
VhdManager.exe VhdManager.ini ; Generated (13.09.2015 18:44:54) by Simple Vhd Manager v1.0 ; www.sordum.org [Main] Language=Auto WinSetOnTop=0 GridLines=1 NewVhdSize=2,5 NewVhdOpt=0 [VirtualDiskFiles] 1=C:\20180619_034757.vhd [Language_English] 01="BlueLife" 02="Attach marked and detach unmarked files" 03="Add file to list" 04="Remove file from list" 05="Refresh list" 06="Disk Management" 07="Action\n Location\n Attached\n Boot Menu\n Virtual Size" 08="&File;\n &Edit;\n &Options;\n Tools \n &Help;" 09="&Add; File" 10="&Create; and Attach VHD" 11="&Attach; Marked Files" 12="&Detach; All Files" 13="&Remove; Entries In Boot Menu" 14="&Exit;" 15="&Attach;" 16="&Detach;" 17="&Read;-Only" 18="&Add; to Boot Menu" 19="&Remove; From Boot Menu" 20="&Paste; File" 21="&Remove;" 22="&Remove; All" 23="&Check; - Uncheck" 24="&Check; All" 25="&Uncheck; All" 26="&Open; Location" 27="&File; Properties" 28="&Disk;" 29="&Cd;-Rom" 30="&Refresh;" 31="&Open; With Explorer" 32="&Change; Drive Letter" 33="&Remove; Letter" 34="&Drive; Properties" 35="&Attach; Marked To System Startup" 36="&Disable; Autorun" 37="&Always; On Top" 38="&Show; Grid Lines" 39="&Add; to Context Menu" 40="&Show; Hidden Files" 41="&Show; File Extensions" 42="Disk &Management;" 43="Diskpart &Command; Prompt" 44="&Msconfig;" 45="&Restart; Windows Explorer" 46="&Donate;" 47="&HomePage;" 48="&Contact;" 49="&About;" 50="Please Wait ..." 51="Select Virtual Disk File\n Virtual Disk Files\n All Files" 52="Enter boot menu name\nOk\nCancel" 53="Added" 54="Unable to write file!" 55="File not found!" 56="The file path must not contain any spaces. Please correct it and try again." 57="Virtual hard disk size \nVirtual hard disk format \n Virtual hard disk type \nDynamically expanding \n Fixed" 58="%drive% not enough free space!\n Do you wish to continue?" 59="An error occurred. Check your settings and try again! \nError Code:%error%" 60="Mount &Options;\n &Mount; \n &Unmount; \n &Open; With Simple VHD Manager\n VHD Mount \n V

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值