二、后期初始化
这阶段对inode、dentry、mount、字符设备驱动模型以及块设备驱动模型做了相应的初始化。
- /*vfs初始化,在start_kernel中调用*/
- void __init vfs_caches_init(unsigned long mempages)
- {
- unsigned long reserve;
- /* Base hash sizes on available memory, with a reserve equal to
- 150% of current kernel size */
- reserve = min((mempages - nr_free_pages()) * 3/2, mempages - 1);
- mempages -= reserve;
- /*为路径名申请的cache*/
- names_cachep = kmem_cache_create("names_cache", PATH_MAX, 0,
- SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
- /*dentry及其相关内容初始化*/
- dcache_init();
- inode_init();/*inode初始化*/
- files_init(mempages);/*文件相关信息初始化,包括文件描述符表初始化*/
- mnt_init();/*mount 的初始化*/
- bdev_cache_init();
- /*字符设备驱动模型的初始化*/
- chrdev_init();
- }
2.1 dentry初始化
- static void __init dcache_init(void)
- {
- int loop;
- /*
- * A constructor could be added for stable state like the lists,
- * but it is probably not worth it because of the cache nature
- * of the dcache.
- *//*从cache中申请目录cache*/
- dentry_cache = KMEM_CACHE(dentry,
- SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|SLAB_MEM_SPREAD);
- /*注册一个shrinker*/
- register_shrinker(&dcache_shrinker);
- /* Hash may have been set up in dcache_init_early */
- if (!hashdist)
- return;
- /*下面的操作在前面的初始化中已经做了*/
- dentry_hashtable =
- alloc_large_system_hash("Dentry cache",
- sizeof(struct hlist_head),
- dhash_entries,
- 13,
- 0,
- &d_hash_shift,
- &d_hash_mask,
- 0);
- for (loop = 0; loop < (1 << d_hash_shift); loop++)
- INIT_HLIST_HEAD(&dentry_hashtable[loop]);
- }
2.2 inode初始化
- void __init inode_init(void)
- {
- int loop;
- /* inode slab cache */
- /*slab中分配inode缓存*/
- inode_cachep = kmem_cache_create("inode_cache",
- sizeof(struct inode),
- 0,
- (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|
- SLAB_MEM_SPREAD),
- init_once);
- /*注册icache shrinker,将参数对应的shrinker加入指定队列*/
- register_shrinker(&icache_shrinker);
- /* Hash may have been set up in inode_init_early */
- if (!hashdist)
- return;
- /*分配数组对应空间*/
- inode_hashtable =
- alloc_large_system_hash("Inode-cache",
- sizeof(struct hlist_head),
- ihash_entries,
- 14,
- 0,
- &i_hash_shift,
- &i_hash_mask,
- 0);
- /*初始化链表组*/
- for (loop = 0; loop < (1 << i_hash_shift); loop++)
- INIT_HLIST_HEAD(&inode_hashtable[loop]);
- }
2.3 files初始化
- void __init files_init(unsigned long mempages)
- {
- int n;
- /*申请文件cache*/
- filp_cachep = kmem_cache_create("filp", sizeof(struct file), 0,
- SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
- /*
- * One file with associated inode and dcache is very roughly 1K.
- * Per default don't use more than 10% of our memory for files.
- */
- n = (mempages * (PAGE_SIZE / 1024)) / 10;
- files_stat.max_files = n; /*更新文件统计信息*/
- if (files_stat.max_files < NR_FILE)
- files_stat.max_files = NR_FILE;
- files_defer_init();/*释放文件描述符表*/
- percpu_counter_init(&nr_files, 0);
- }
2.4 mount初始化
- void __init mnt_init(void)
- {
- unsigned u;
- int err;
- init_rwsem(&namespace_sem);
- /*mnt cache初始化*/
- mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct vfsmount),
- 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
- /*mount hashtable内存申请*/
- mount_hashtable = (struct list_head *)__get_free_page(GFP_ATOMIC);
- if (!mount_hashtable)
- panic("Failed to allocate mount hash table\n");
- printk("Mount-cache hash table entries: %lu\n", HASH_SIZE);
- for (u = 0; u < HASH_SIZE; u++)
- INIT_LIST_HEAD(&mount_hashtable[u]);/*初始化hashtable链表*/
- err = sysfs_init();/*sysfs文件系统初始化*/
- if (err)
- printk(KERN_WARNING "%s: sysfs_init error: %d\n",
- __func__, err);
- fs_kobj = kobject_create_and_add("fs", NULL);
- if (!fs_kobj)
- printk(KERN_WARNING "%s: kobj create error\n", __func__);
- init_rootfs();/*初始化ramfs和rootfs*/
- init_mount_tree();/*初始化mount tree*/
- }
- static void __init init_mount_tree(void)
- {
- struct vfsmount *mnt;
- struct mnt_namespace *ns;
- struct path root;
- mnt = do_kern_mount("rootfs", 0, "rootfs", NULL);
- if (IS_ERR(mnt))
- panic("Can't create rootfs");
- ns = create_mnt_ns(mnt);/*为mnt创建命名空间*/
- if (IS_ERR(ns))
- panic("Can't allocate initial namespace");
- /*初始化进程的相关命名空间*/
- init_task.nsproxy->mnt_ns = ns;
- get_mnt_ns(ns);/*命名空间的进程数加一*/
- /*更新root的相关字段*/
- root.mnt = ns->root;
- root.dentry = ns->root->mnt_root;
- /*设置fs的当前路径和当前root*/
- set_fs_pwd(current->fs, &root);
- set_fs_root(current->fs, &root);
- }
