2.4.1 创建命名空间
- /**
- * create_mnt_ns - creates a private namespace and adds a root filesystem
- * @mnt: pointer to the new root filesystem mountpoint
- */
- struct mnt_namespace *create_mnt_ns(struct vfsmount *mnt)
- {
- struct mnt_namespace *new_ns;
- new_ns = alloc_mnt_ns();/*分配命名空间*/
- if (!IS_ERR(new_ns)) {
- /*下面为和mnt建立关系*/
- mnt->mnt_ns = new_ns;
- new_ns->root = mnt;
- list_add(&new_ns->list, &new_ns->root->mnt_list);
- }
- return new_ns;
- }
- static struct mnt_namespace *alloc_mnt_ns(void)
- {
- struct mnt_namespace *new_ns;
- /*从cache中分配命名空间*/
- new_ns = kmalloc(sizeof(struct mnt_namespace), GFP_KERNEL);
- if (!new_ns)
- return ERR_PTR(-ENOMEM);
- /*下面为相关字段的初始化*/
- atomic_set(&new_ns->count, 1);
- new_ns->root = NULL;
- INIT_LIST_HEAD(&new_ns->list);
- init_waitqueue_head(&new_ns->poll);
- new_ns->event = 0;
- return new_ns;
- }
2.4.2 创建mount
- struct vfsmount *
- do_kern_mount(const char *fstype, int flags, const char *name, void *data)
- {
- struct file_system_type *type = get_fs_type(fstype);
- struct vfsmount *mnt;
- if (!type)
- return ERR_PTR(-ENODEV);
- mnt = vfs_kern_mount(type, flags, name, data);
- if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) &&
- !mnt->mnt_sb->s_subtype)
- mnt = fs_set_subtype(mnt, fstype);
- put_filesystem(type);
- return mnt;
- }
- struct vfsmount *
- vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data)
- {
- struct vfsmount *mnt;
- char *secdata = NULL;
- int error;
- if (!type)
- return ERR_PTR(-ENODEV);
- error = -ENOMEM;
- /*从slab中分配一个mnt*/
- mnt = alloc_vfsmnt(name);
- if (!mnt)
- goto out;
- if (data && !(type->fs_flags & FS_BINARY_MOUNTDATA)) {
- secdata = alloc_secdata();
- if (!secdata)
- goto out_mnt;
- error = security_sb_copy_data(data, secdata);
- if (error)
- goto out_free_secdata;
- }
- /*调用文件系统控制结构体的get_sb()*/
- error = type->get_sb(type, flags, name, data, mnt);
- if (error < 0)
- goto out_free_secdata;
- BUG_ON(!mnt->mnt_sb);
- error = security_sb_kern_mount(mnt->mnt_sb, flags, secdata);
- if (error)
- goto out_sb;
- /*
- * filesystems should never set s_maxbytes larger than MAX_LFS_FILESIZE
- * but s_maxbytes was an unsigned long long for many releases. Throw
- * this warning for a little while to try and catch filesystems that
- * violate this rule. This warning should be either removed or
- * converted to a BUG() in 2.6.34.
- */
- WARN((mnt->mnt_sb->s_maxbytes < 0), "%s set sb->s_maxbytes to "
- "negative value (%lld)\n", type->name, mnt->mnt_sb->s_maxbytes);
- /*初始化mnt相关字段*/
- mnt->mnt_mountpoint = mnt->mnt_root;
- mnt->mnt_parent = mnt;
- up_write(&mnt->mnt_sb->s_umount);
- free_secdata(secdata);
- return mnt;
- out_sb:
- dput(mnt->mnt_root);
- deactivate_locked_super(mnt->mnt_sb);
- out_free_secdata:
- free_secdata(secdata);
- out_mnt:
- free_vfsmnt(mnt);
- out:
- return ERR_PTR(error);
- }
- struct vfsmount *alloc_vfsmnt(const char *name)
- {
- /*从slab中获得mnt*/
- struct vfsmount *mnt = kmem_cache_zalloc(mnt_cache, GFP_KERNEL);
- /*下面进行对mnt的初始化*/
- if (mnt) {
- int err;
- err = mnt_alloc_id(mnt);
- if (err)
- goto out_free_cache;
- if (name) {
- mnt->mnt_devname = kstrdup(name, GFP_KERNEL);
- if (!mnt->mnt_devname)
- goto out_free_id;
- }
- atomic_set(&mnt->mnt_count, 1);
- INIT_LIST_HEAD(&mnt->mnt_hash);
- INIT_LIST_HEAD(&mnt->mnt_child);
- INIT_LIST_HEAD(&mnt->mnt_mounts);
- INIT_LIST_HEAD(&mnt->mnt_list);
- INIT_LIST_HEAD(&mnt->mnt_expire);
- INIT_LIST_HEAD(&mnt->mnt_share);
- INIT_LIST_HEAD(&mnt->mnt_slave_list);
- INIT_LIST_HEAD(&mnt->mnt_slave);
- #ifdef CONFIG_SMP
- mnt->mnt_writers = alloc_percpu(int);
- if (!mnt->mnt_writers)
- goto out_free_devname;
- #else
- mnt->mnt_writers = 0;
- #endif
- }
- return mnt;
- #ifdef CONFIG_SMP
- out_free_devname:
- kfree(mnt->mnt_devname);
- #endif
- out_free_id:
- mnt_free_id(mnt);
- out_free_cache:
- kmem_cache_free(mnt_cache, mnt);
- return NULL;
- }
2.5 块设备驱动模型初始化
- void __init bdev_cache_init(void)
- {
- int err;
- struct vfsmount *bd_mnt;
- /*block cache初始化*/
- bdev_cachep = kmem_cache_create("bdev_cache", sizeof(struct bdev_inode),
- 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
- SLAB_MEM_SPREAD|SLAB_PANIC),
- init_once);
- /*注册block dev*/
- err = register_filesystem(&bd_type);
- if (err)
- panic("Cannot register bdev pseudo-fs");
- bd_mnt = kern_mount(&bd_type);
- if (IS_ERR(bd_mnt))
- panic("Cannot create bdev pseudo-fs");
- /*
- * This vfsmount structure is only used to obtain the
- * blockdev_superblock, so tell kmemleak not to report it.
- */
- kmemleak_not_leak(bd_mnt);
- blockdev_superblock = bd_mnt->mnt_sb; /* For writeback */
- }
2.6 字符设备驱动模型初始化
- void __init chrdev_init(void)
- {
- cdev_map = kobj_map_init(base_probe, &chrdevs_lock);
- /*字符设备驱动初始化*/
- bdi_init(&directly_mappable_cdev_bdi);
- }
这里对linux虚拟文件系统的初始化工作做了整体的梳理,后面将对涉及到的细节做补充,包括inode和dentry cache shrinker的注册、sysfs的初始化等。
