一:根文件系统(rootfs)
1,rootfs文件系统的注册
在linux kernel初始化阶段会调用int __init init_rootfs(void)向内核注册rootfs文件系统,
init_rootfs()的核心的函数为register_filesystem();这个函数将结构file_system_type?注册在内核一 个单链表中,
[cpp] view plaincopy
- 307 int __init init_rootfs(void)
- 308 {
- 309 int err;
- 311 err = bdi_init(&ramfs_backing_dev_info);
- 312 if (err)
- 313 return err;
- 315 err = register_filesystem(&rootfs_fs_type);
- 316 if (err)
- 317 bdi_destroy(&ramfs_backing_dev_info);
- 319 return err;
- 320 }
- 288 static struct file_system_type rootfs_fs_type = {
- 289 .name = "rootfs",
- 290 .mount = rootfs_mount,
- 291 .kill_sb = kill_litter_super,
- 292 };
2,rootfs_mount()函数解析
static struct dentry *rootfs_mount(struct file_system_type *fs_type, int flags, const char *dev_name, void *data)
rootfs_mount()函数首先会调用mount_nodev()函数,mount_nodev()函数原型如下:mount_nodev(fs_type, flags, data, ramfs_fill_super);
[cpp] view plaincopy
- 1060 struct dentry *mount_nodev(struct file_system_type *fs_type,
- 1061 int flags, void *data,
- 1062 int (*fill_super)(struct super_block *, void *, int))
- 1063 {
- 1064 int error;
- 1065 struct super_block *s = sget(fs_type, NULL, set_anon_super, NULL);
- 1067 if (IS_ERR(s))
- 1068 return ERR_CAST(s);
- 1069
- 1070 s->s_flags = flags;
- 1072 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
- 1073 if (error) {
- 1074 deactivate_locked_super(s);
- 1075 return ERR_PTR(error);
- 1076 }
- 1077 s->s_flags |= MS_ACTIVE;
- 1078 return dget(s->s_root);
- 1079 }
上述函数sget()函数的作用主要是分配一个超级快(super_block)的实例,并.通过s_list加入链表super_blocks,然后通过s_instances加入链表typer->fs_supers。
fill_super()函数调用到ramfs_fill_super()函数,ramfs_fill_super()函数的定义如下:
[cpp] view plaincopy
- 209 int ramfs_fill_super(struct super_block *sb, void *data, int silent)
- 210 {
- 211 struct ramfs_fs_info *fsi;
- 212 struct inode *inode;
- 213 int err;
- 215 save_mount_options(sb, data);
- 217 fsi = kzalloc(sizeof(struct ramfs_fs_info), GFP_KERNEL);
- 218 sb->s_fs_info = fsi;
- 219 if (!fsi)
- 220 return -ENOMEM;
- 222 err = ramfs_parse_options(data, &fsi->mount_opts);
- 223 if (err)
- 224 return err;
- 226 sb->s_maxbytes = MAX_LFS_FILESIZE; //文件的最大值
- 227 sb->s_blocksize = PAGE_CACHE_SIZE; //以字节为单位的块的大小
- 228 sb->s_blocksize_bits = PAGE_CACHE_SHIFT; //以位为单位的块的大小
- 229 sb->s_magic = RAMFS_MAGIC;
- 230 sb->s_op = &ramfs_ops; //超级块的方法 ,在处理inode的时候会有用
- 231 sb->s_time_gran = 1;
- 233 inode = ramfs_get_inode(sb, NULL, S_IFDIR | fsi->mount_opts.mode, 0); //建立根目录索引节点
- 234 sb->s_root = d_make_root(inode); //建立根目录目录对象; 超级块的s_root指向刚建立的根目录对象
- 235 if (!sb->s_root)
- 236 return -ENOMEM;
- 238 return 0;
- 239 }
- 54 struct inode *ramfs_get_inode(struct super_block *sb,
- 55 const struct inode *dir, umode_t mode, dev_t dev)
- 56 {
- 57 struct inode * inode = new_inode(sb); //在索引节点高速缓存里创建一个inode,
- 59 if (inode) {
- 60 inode->i_ino = get_next_ino(); //获取一个inode number
- 61 inode_init_owner(inode, dir, mode); //设置inode,初始化uid gid mode等
- 62 inode->i_mapping->a_ops = &ramfs_aops;
- 63 inode->i_mapping->backing_dev_info = &ramfs_backing_dev_info;
- 64 mapping_set_gfp_mask(inode->i_mapping, GFP_HIGHUSER);
- 65 mapping_set_unevictable(inode->i_mapping);
- 66 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
- 67 switch (mode & S_IFMT) {
- 68 default: //特殊文件;如:字符~块设备文件,FIFO,SOCKET文件
- 69 init_special_inode(inode, mode, dev);
- 70 break;
- 71 case S_IFREG: //普通文件
- 72 inode->i_op = &ramfs_file_inode_operations;
- 73 inode->i_fop = &ramfs_file_operations;
- 74 break;
- 75 case S_IFDIR: //目录文件
- 76 inode->i_op = &ramfs_dir_inode_operations;
- 77 inode->i_fop = &simple_dir_operations;
- 79 /* directory inodes start off with i_nlink == 2 (for "." entry) */
- 80 inc_nlink(inode);
- 81 break;
- 82 case S_IFLNK: //链接文件
- 83 inode->i_op = &page_symlink_inode_operations;
- 84 break;
- 85 }
- 86 }
- 87 return inode; //返回创建的inode与对应的目录项对象关联
- 88 }
时间: 2024-10-13 16:06:33