手机版
你好,游客 登录 注册
背景:
阅读新闻

Linux虚拟文件系统(内核初始化<一>)

[日期:2012-02-09] 来源:Linux社区  作者:bullbat [字体:大 中 小]

2.4.1 创建命名空间

[cpp]
  1. /** 
  2.  * create_mnt_ns - creates a private namespace and adds a root filesystem 
  3.  * @mnt: pointer to the new root filesystem mountpoint 
  4.  */  
  5. struct mnt_namespace *create_mnt_ns(struct vfsmount *mnt)  
  6. {  
  7.     struct mnt_namespace *new_ns;  
  8.   
  9.     new_ns = alloc_mnt_ns();/*分配命名空间*/  
  10.     if (!IS_ERR(new_ns)) {  
  11.         /*下面为和mnt建立关系*/  
  12.         mnt->mnt_ns = new_ns;  
  13.         new_ns->root = mnt;  
  14.         list_add(&new_ns->list, &new_ns->root->mnt_list);  
  15.     }  
  16.     return new_ns;  
  17. }  
[cpp]
  1. static struct mnt_namespace *alloc_mnt_ns(void)  
  2. {  
  3.     struct mnt_namespace *new_ns;  
  4.     /*从cache中分配命名空间*/     
  5.     new_ns = kmalloc(sizeof(struct mnt_namespace), GFP_KERNEL);  
  6.     if (!new_ns)  
  7.         return ERR_PTR(-ENOMEM);  
  8.     /*下面为相关字段的初始化*/  
  9.     atomic_set(&new_ns->count, 1);  
  10.     new_ns->root = NULL;  
  11.     INIT_LIST_HEAD(&new_ns->list);  
  12.     init_waitqueue_head(&new_ns->poll);  
  13.     new_ns->event = 0;  
  14.     return new_ns;  
  15. }     

2.4.2 创建mount

[cpp]
  1. struct vfsmount *  
  2. do_kern_mount(const char *fstype, int flags, const char *name, void *data)  
  3. {  
  4.     struct file_system_type *type = get_fs_type(fstype);  
  5.     struct vfsmount *mnt;  
  6.     if (!type)  
  7.         return ERR_PTR(-ENODEV);  
  8.     mnt = vfs_kern_mount(type, flags, name, data);  
  9.     if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) &&  
  10.         !mnt->mnt_sb->s_subtype)  
  11.         mnt = fs_set_subtype(mnt, fstype);  
  12.     put_filesystem(type);  
  13.     return mnt;  
  14. }  
[cpp]
  1. struct vfsmount *  
  2. vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data)  
  3. {  
  4.     struct vfsmount *mnt;  
  5.     char *secdata = NULL;  
  6.     int error;  
  7.   
  8.     if (!type)  
  9.         return ERR_PTR(-ENODEV);  
  10.   
  11.     error = -ENOMEM;  
  12.     /*从slab中分配一个mnt*/  
  13.     mnt = alloc_vfsmnt(name);  
  14.     if (!mnt)  
  15.         goto out;  
  16.   
  17.     if (data && !(type->fs_flags & FS_BINARY_MOUNTDATA)) {  
  18.         secdata = alloc_secdata();  
  19.         if (!secdata)  
  20.             goto out_mnt;  
  21.   
  22.         error = security_sb_copy_data(data, secdata);  
  23.         if (error)  
  24.             goto out_free_secdata;  
  25.     }  
  26.     /*调用文件系统控制结构体的get_sb()*/  
  27.     error = type->get_sb(type, flags, name, data, mnt);  
  28.     if (error < 0)  
  29.         goto out_free_secdata;  
  30.     BUG_ON(!mnt->mnt_sb);  
  31.   
  32.     error = security_sb_kern_mount(mnt->mnt_sb, flags, secdata);  
  33.     if (error)  
  34.         goto out_sb;  
  35.   
  36.     /* 
  37.      * filesystems should never set s_maxbytes larger than MAX_LFS_FILESIZE 
  38.      * but s_maxbytes was an unsigned long long for many releases. Throw 
  39.      * this warning for a little while to try and catch filesystems that 
  40.      * violate this rule. This warning should be either removed or 
  41.      * converted to a BUG() in 2.6.34. 
  42.      */  
  43.     WARN((mnt->mnt_sb->s_maxbytes < 0), "%s set sb->s_maxbytes to "  
  44.         "negative value (%lld)\n", type->name, mnt->mnt_sb->s_maxbytes);  
  45.     /*初始化mnt相关字段*/  
  46.     mnt->mnt_mountpoint = mnt->mnt_root;  
  47.     mnt->mnt_parent = mnt;  
  48.     up_write(&mnt->mnt_sb->s_umount);  
  49.     free_secdata(secdata);  
  50.     return mnt;  
  51. out_sb:  
  52.     dput(mnt->mnt_root);  
  53.     deactivate_locked_super(mnt->mnt_sb);  
  54. out_free_secdata:  
  55.     free_secdata(secdata);  
  56. out_mnt:  
  57.     free_vfsmnt(mnt);  
  58. out:  
  59.     return ERR_PTR(error);  
  60. }  
[cpp]
  1. struct vfsmount *alloc_vfsmnt(const char *name)  
  2. {  
  3.     /*从slab中获得mnt*/  
  4.     struct vfsmount *mnt = kmem_cache_zalloc(mnt_cache, GFP_KERNEL);  
  5.     /*下面进行对mnt的初始化*/  
  6.     if (mnt) {  
  7.         int err;  
  8.   
  9.         err = mnt_alloc_id(mnt);  
  10.         if (err)  
  11.             goto out_free_cache;  
  12.   
  13.         if (name) {  
  14.             mnt->mnt_devname = kstrdup(name, GFP_KERNEL);  
  15.             if (!mnt->mnt_devname)  
  16.                 goto out_free_id;  
  17.         }  
  18.   
  19.         atomic_set(&mnt->mnt_count, 1);  
  20.         INIT_LIST_HEAD(&mnt->mnt_hash);  
  21.         INIT_LIST_HEAD(&mnt->mnt_child);  
  22.         INIT_LIST_HEAD(&mnt->mnt_mounts);  
  23.         INIT_LIST_HEAD(&mnt->mnt_list);  
  24.         INIT_LIST_HEAD(&mnt->mnt_expire);  
  25.         INIT_LIST_HEAD(&mnt->mnt_share);  
  26.         INIT_LIST_HEAD(&mnt->mnt_slave_list);  
  27.         INIT_LIST_HEAD(&mnt->mnt_slave);  
  28. #ifdef CONFIG_SMP   
  29.         mnt->mnt_writers = alloc_percpu(int);  
  30.         if (!mnt->mnt_writers)  
  31.             goto out_free_devname;  
  32. #else   
  33.         mnt->mnt_writers = 0;  
  34. #endif   
  35.     }  
  36.     return mnt;  
  37.   
  38. #ifdef CONFIG_SMP   
  39. out_free_devname:  
  40.     kfree(mnt->mnt_devname);  
  41. #endif   
  42. out_free_id:  
  43.     mnt_free_id(mnt);  
  44. out_free_cache:  
  45.     kmem_cache_free(mnt_cache, mnt);  
  46.     return NULL;  
  47. }  

2.5 块设备驱动模型初始化

[cpp]
  1. void __init bdev_cache_init(void)  
  2. {  
  3.     int err;  
  4.     struct vfsmount *bd_mnt;  
  5.     /*block cache初始化*/  
  6.     bdev_cachep = kmem_cache_create("bdev_cache", sizeof(struct bdev_inode),  
  7.             0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|  
  8.                 SLAB_MEM_SPREAD|SLAB_PANIC),  
  9.             init_once);  
  10.     /*注册block dev*/  
  11.     err = register_filesystem(&bd_type);  
  12.     if (err)  
  13.         panic("Cannot register bdev pseudo-fs");  
  14.     bd_mnt = kern_mount(&bd_type);  
  15.     if (IS_ERR(bd_mnt))  
  16.         panic("Cannot create bdev pseudo-fs");  
  17.     /* 
  18.      * This vfsmount structure is only used to obtain the 
  19.      * blockdev_superblock, so tell kmemleak not to report it. 
  20.      */  
  21.     kmemleak_not_leak(bd_mnt);  
  22.     blockdev_superblock = bd_mnt->mnt_sb;    /* For writeback */  
  23. }  

2.6 字符设备驱动模型初始化

[cpp]
  1. void __init chrdev_init(void)  
  2. {  
  3.     cdev_map = kobj_map_init(base_probe, &chrdevs_lock);  
  4.     /*字符设备驱动初始化*/  
  5.     bdi_init(&directly_mappable_cdev_bdi);  
  6. }  

这里对linux虚拟文件系统的初始化工作做了整体的梳理,后面将对涉及到的细节做补充,包括inode和dentry cache shrinker的注册、sysfs的初始化等。

linux
相关资讯       Linux基础教程 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款