Chinaunix首页 | 论坛 | 博客
  • 博客访问: 9731
  • 博文数量: 4
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 40
  • 用 户 组: 普通用户
  • 注册时间: 2014-09-03 21:26
文章分类
文章存档

2015年(4)

我的朋友

分类: LINUX

2015-07-01 21:22:26

1. kern_mount:

点击(此处)折叠或打开

  1. #define kern_mount(type) kern_mount_data(type, NULL)
    kern_mount相当于直接调用了kern_mount_data函数。

2.kern_mount_data

点击(此处)折叠或打开

  1. struct vfsmount *kern_mount_data(struct file_system_type *type, void *data)
  2. {
  3.     struct vfsmount *mnt;
  4.     mnt = vfs_kern_mount(type, MS_KERNMOUNT, type->name, data); /**/
  5.     if (!IS_ERR(mnt)) {
  6.         /*
  7.          * it is a longterm mount, don't release mnt until
  8.          * we unmount before file sys is unregistered
  9.         */
  10.         real_mount(mnt)->mnt_ns = MNT_NS_INTERNAL; /* 将包含vfsmount的mount对象的mnt_ns(struct mnt_namespace)赋值ERR_PTR(-EINVAL) */
  11.     }
  12.     return mnt;
  13. }


3. vfs_kern_mount

点击(此处)折叠或打开

  1. struct vfsmount *
  2. vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data)
  3. {
  4.     struct mount *mnt;
  5.     struct dentry *root;

  6.     if (!type)
  7.         return ERR_PTR(-ENODEV);

  8.     mnt = alloc_vfsmnt(name); /*分配一个mnt对象,并对其进行部分初始化*/
  9.     if (!mnt)
  10.         return ERR_PTR(-ENOMEM);

  11.     if (flags & MS_KERNMOUNT)
  12.         mnt->mnt.mnt_flags = MNT_INTERNAL;

  13.     root = mount_fs(type, flags, name, data);  /* 获取该文件系统的根目录的dentry,同时也获取super_block */
  14.     if (IS_ERR(root)) {
  15.         free_vfsmnt(mnt);
  16.         return ERR_CAST(root);
  17.     }

  18.     /* differentiate nand and emmc*/
  19.     if(root->d_sb->s_mtd){
  20.         g_rsrvd_size = NAND_DATA_RESERVED_SIZE;
  21.     }
  22.     /* 对mnt对象与root进行绑定 */
  23.     mnt->mnt.mnt_root = root;
  24.     mnt->mnt.mnt_sb = root->d_sb;
  25.     mnt->mnt_mountpoint = mnt->mnt.mnt_root;
  26.     mnt->mnt_parent = mnt;
  27.     br_write_lock(&vfsmount_lock);
  28.     list_add_tail(&mnt->mnt_instance, &root->d_sb->s_mounts);  /* 将mnt添加到root->d_sb->s_mounts链表中 */
  29.     br_write_unlock(&vfsmount_lock);
  30.     return &mnt->mnt;
  31. }

4. mount_fs

点击(此处)折叠或打开

  1. struct dentry *
  2. mount_fs(struct file_system_type *type, int flags, const char *name, void *data)
  3. {
  4.     struct dentry *root;
  5.     struct super_block *sb;
  6.     char *secdata = NULL;
  7.     int error = -ENOMEM;

  8.     if (data && !(type->fs_flags & FS_BINARY_MOUNTDATA)) {  /* 在kern_mount调用中data为NULL,所以该if判断为假 */
  9.         secdata = alloc_secdata();
  10.         if (!secdata)
  11.             goto out;

  12.         error = security_sb_copy_data(data, secdata);
  13.         if (error)
  14.             goto out_free_secdata;
  15.     }

  16.     root = type->mount(type, flags, name, data);  /* 调用file_system_type中的 mount方法,通过该方法获取该文件系统的根目录dentry,同时也获取super_block. file_system_type的mount方法kernel提供以实现的函数:mount_single,mount_pseudo等 */
  17.     if (IS_ERR(root)) {
  18.         error = PTR_ERR(root);
  19.         goto out_free_secdata;
  20.     }
  21.     sb = root->d_sb;
  22.     BUG_ON(!sb);
  23.     WARN_ON(!sb->s_bdi);
  24.     WARN_ON(sb->s_bdi == &default_backing_dev_info);
  25.     sb->s_flags |= MS_BORN;

  26.     error = security_sb_kern_mount(sb, flags, secdata);
  27.     if (error)
  28.         goto out_sb;

  29.     /*
  30.      * filesystems should never set s_maxbytes larger than MAX_LFS_FILESIZE
  31.      * but s_maxbytes was an unsigned long long for many releases. Throw
  32.      * this warning for a little while to try and catch filesystems that
  33.      * violate this rule.
  34.      */
  35.     WARN((sb->s_maxbytes < 0), "%s set sb->s_maxbytes to "
  36.         "negative value (%lld)\n", type->name, sb->s_maxbytes);

  37.     up_write(&sb->s_umount);
  38.     free_secdata(secdata);
  39.     return root;
  40. out_sb:
  41.     dput(root);
  42.     deactivate_locked_super(sb);
  43. out_free_secdata:
  44.     free_secdata(secdata);
  45. out:
  46.     return ERR_PTR(error);
  47. }

总结:kern_count函数主要用于那些没有实体介质的文件系统,该函数主要是获取文件系统的super_block对象与根目录的inode与dentry对象,并将这些对象加入到系统链表。

阅读(2721) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~