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

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

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

二、后期初始化

这阶段对inode、dentry、mount、字符设备驱动模型以及块设备驱动模型做了相应的初始化。

[cpp]
  1. /*vfs初始化,在start_kernel中调用*/  
  2. void __init vfs_caches_init(unsigned long mempages)  
  3. {  
  4.     unsigned long reserve;  
  5.   
  6.     /* Base hash sizes on available memory, with a reserve equal to 
  7.            150% of current kernel size */  
  8.   
  9.     reserve = min((mempages - nr_free_pages()) * 3/2, mempages - 1);  
  10.     mempages -= reserve;  
  11.     /*为路径名申请的cache*/  
  12.     names_cachep = kmem_cache_create("names_cache", PATH_MAX, 0,  
  13.             SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);  
  14.     /*dentry及其相关内容初始化*/  
  15.     dcache_init();  
  16.     inode_init();/*inode初始化*/  
  17.     files_init(mempages);/*文件相关信息初始化,包括文件描述符表初始化*/  
  18.     mnt_init();/*mount 的初始化*/  
  19.     bdev_cache_init();  
  20.     /*字符设备驱动模型的初始化*/  
  21.     chrdev_init();  
  22. }  

2.1 dentry初始化

[cpp]
  1. static void __init dcache_init(void)  
  2. {  
  3.     int loop;  
  4.   
  5.     /*  
  6.      * A constructor could be added for stable state like the lists, 
  7.      * but it is probably not worth it because of the cache nature 
  8.      * of the dcache.  
  9.      *//*从cache中申请目录cache*/  
  10.     dentry_cache = KMEM_CACHE(dentry,  
  11.         SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|SLAB_MEM_SPREAD);  
  12.     /*注册一个shrinker*/  
  13.     register_shrinker(&dcache_shrinker);  
  14.   
  15.     /* Hash may have been set up in dcache_init_early */  
  16.     if (!hashdist)  
  17.         return;  
  18.     /*下面的操作在前面的初始化中已经做了*/  
  19.     dentry_hashtable =  
  20.         alloc_large_system_hash("Dentry cache",  
  21.                     sizeof(struct hlist_head),  
  22.                     dhash_entries,  
  23.                     13,  
  24.                     0,  
  25.                     &d_hash_shift,  
  26.                     &d_hash_mask,  
  27.                     0);  
  28.   
  29.     for (loop = 0; loop < (1 << d_hash_shift); loop++)  
  30.         INIT_HLIST_HEAD(&dentry_hashtable[loop]);  
  31. }  

2.2 inode初始化

[cpp]
  1. void __init inode_init(void)  
  2. {  
  3.     int loop;  
  4.   
  5.     /* inode slab cache */  
  6.     /*slab中分配inode缓存*/  
  7.     inode_cachep = kmem_cache_create("inode_cache",  
  8.                      sizeof(struct inode),  
  9.                      0,  
  10.                      (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|  
  11.                      SLAB_MEM_SPREAD),  
  12.                      init_once);  
  13.     /*注册icache shrinker,将参数对应的shrinker加入指定队列*/  
  14.     register_shrinker(&icache_shrinker);  
  15.   
  16.     /* Hash may have been set up in inode_init_early */  
  17.     if (!hashdist)  
  18.         return;  
  19.     /*分配数组对应空间*/  
  20.     inode_hashtable =  
  21.         alloc_large_system_hash("Inode-cache",  
  22.                     sizeof(struct hlist_head),  
  23.                     ihash_entries,  
  24.                     14,  
  25.                     0,  
  26.                     &i_hash_shift,  
  27.                     &i_hash_mask,  
  28.                     0);  
  29.     /*初始化链表组*/  
  30.     for (loop = 0; loop < (1 << i_hash_shift); loop++)  
  31.         INIT_HLIST_HEAD(&inode_hashtable[loop]);  
  32. }  

2.3 files初始化

[cpp]
  1. void __init files_init(unsigned long mempages)  
  2. {   
  3.     int n;   
  4.     /*申请文件cache*/  
  5.     filp_cachep = kmem_cache_create("filp", sizeof(struct file), 0,  
  6.             SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);  
  7.   
  8.     /* 
  9.      * One file with associated inode and dcache is very roughly 1K. 
  10.      * Per default don't use more than 10% of our memory for files.  
  11.      */   
  12.   
  13.     n = (mempages * (PAGE_SIZE / 1024)) / 10;  
  14.     files_stat.max_files = n; /*更新文件统计信息*/  
  15.     if (files_stat.max_files < NR_FILE)  
  16.         files_stat.max_files = NR_FILE;  
  17.     files_defer_init();/*释放文件描述符表*/  
  18.     percpu_counter_init(&nr_files, 0);  
  19. }   

2.4 mount初始化

[cpp]
  1. void __init mnt_init(void)  
  2. {  
  3.     unsigned u;  
  4.     int err;  
  5.   
  6.     init_rwsem(&namespace_sem);  
  7.     /*mnt cache初始化*/  
  8.     mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct vfsmount),  
  9.             0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);  
  10.     /*mount hashtable内存申请*/  
  11.     mount_hashtable = (struct list_head *)__get_free_page(GFP_ATOMIC);  
  12.   
  13.     if (!mount_hashtable)  
  14.         panic("Failed to allocate mount hash table\n");  
  15.   
  16.     printk("Mount-cache hash table entries: %lu\n", HASH_SIZE);  
  17.   
  18.     for (u = 0; u < HASH_SIZE; u++)  
  19.         INIT_LIST_HEAD(&mount_hashtable[u]);/*初始化hashtable链表*/  
  20.   
  21.     err = sysfs_init();/*sysfs文件系统初始化*/  
  22.     if (err)  
  23.         printk(KERN_WARNING "%s: sysfs_init error: %d\n",  
  24.             __func__, err);  
  25.     fs_kobj = kobject_create_and_add("fs", NULL);  
  26.     if (!fs_kobj)  
  27.         printk(KERN_WARNING "%s: kobj create error\n", __func__);  
  28.     init_rootfs();/*初始化ramfs和rootfs*/  
  29.     init_mount_tree();/*初始化mount tree*/  
  30. }  
[cpp]
  1. static void __init init_mount_tree(void)  
  2. {  
  3.     struct vfsmount *mnt;  
  4.     struct mnt_namespace *ns;  
  5.     struct path root;  
  6.   
  7.     mnt = do_kern_mount("rootfs", 0, "rootfs", NULL);  
  8.     if (IS_ERR(mnt))  
  9.         panic("Can't create rootfs");  
  10.     ns = create_mnt_ns(mnt);/*为mnt创建命名空间*/  
  11.     if (IS_ERR(ns))  
  12.         panic("Can't allocate initial namespace");  
  13.     /*初始化进程的相关命名空间*/  
  14.     init_task.nsproxy->mnt_ns = ns;  
  15.     get_mnt_ns(ns);/*命名空间的进程数加一*/  
  16.     /*更新root的相关字段*/  
  17.     root.mnt = ns->root;  
  18.     root.dentry = ns->root->mnt_root;  
  19.     /*设置fs的当前路径和当前root*/  
  20.     set_fs_pwd(current->fs, &root);  
  21.     set_fs_root(current->fs, &root);  
  22. }  
linux
相关资讯       Linux基础教程 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

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