zoukankan      html  css  js  c++  java
  • [文件系统]文件系统学习笔记(九)---rootfs

    一:根文件系统(rootfs)

    1,rootfs文件系统的注册
    在linux kernel初始化阶段会调用int __init init_rootfs(void)向内核注册rootfs文件系统,
    init_rootfs()的核心的函数为register_filesystem();这个函数将结构file_system_type?注册在内核一 个单链表中,

    [cpp] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. 307 int __init init_rootfs(void)  
    2. 308 {  
    3. 309         int err;  
    4. 311         err = bdi_init(&ramfs_backing_dev_info);  
    5. 312         if (err)  
    6. 313                 return err;  
    7. 315         err = register_filesystem(&rootfs_fs_type);  
    8. 316         if (err)  
    9. 317                 bdi_destroy(&ramfs_backing_dev_info);  
    10. 319         return err;  
    11. 320 }  
    12.   
    13. 288 static struct file_system_type rootfs_fs_type = {  
    14. 289         .name           = "rootfs",  
    15. 290         .mount         = rootfs_mount,  
    16. 291         .kill_sb        = kill_litter_super,  
    17. 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在CODE上查看代码片派生到我的代码片
     
    1. 1060 struct dentry *mount_nodev(struct file_system_type *fs_type,  
    2. 1061         int flags, void *data,  
    3. 1062         int (*fill_super)(struct super_block *, void *, int))  
    4. 1063 {  
    5. 1064         int error;  
    6. 1065         struct super_block *s = sget(fs_type, NULL, set_anon_super, NULL);  
    7. 1067         if (IS_ERR(s))  
    8. 1068                 return ERR_CAST(s);  
    9. 1069   
    10. 1070         s->s_flags = flags;  
    11. 1072         error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);  
    12. 1073         if (error) {  
    13. 1074                 deactivate_locked_super(s);  
    14. 1075                 return ERR_PTR(error);  
    15. 1076         }  
    16. 1077         s->s_flags |= MS_ACTIVE;  
    17. 1078         return dget(s->s_root);  
    18. 1079 }  



    上述函数sget()函数的作用主要是分配一个超级快(super_block)的实例,并.通过s_list加入链表super_blocks,然后通过s_instances加入链表typer->fs_supers。

    fill_super()函数调用到ramfs_fill_super()函数,ramfs_fill_super()函数的定义如下:

    [cpp] view plaincopy在CODE上查看代码片派生到我的代码片
     
      1. 209 int ramfs_fill_super(struct super_block *sb, void *data, int silent)  
      2. 210 {  
      3. 211         struct ramfs_fs_info *fsi;  
      4. 212         struct inode *inode;  
      5. 213         int err;  
      6. 215         save_mount_options(sb, data);  
      7. 217         fsi = kzalloc(sizeof(struct ramfs_fs_info), GFP_KERNEL);  
      8. 218         sb->s_fs_info = fsi;  
      9. 219         if (!fsi)  
      10. 220                 return -ENOMEM;  
      11. 222         err = ramfs_parse_options(data, &fsi->mount_opts);  
      12. 223         if (err)  
      13. 224                 return err;  
      14. 226         sb->s_maxbytes          = MAX_LFS_FILESIZE;  //文件的最大值  
      15. 227         sb->s_blocksize         = PAGE_CACHE_SIZE;  //以字节为单位的块的大小  
      16. 228         sb->s_blocksize_bits    = PAGE_CACHE_SHIFT;  //以位为单位的块的大小  
      17. 229         sb->s_magic             = RAMFS_MAGIC;    
      18. 230         sb->s_op                = &ramfs_ops;  //超级块的方法 ,在处理inode的时候会有用  
      19. 231         sb->s_time_gran         = 1;  
      20. 233         inode = ramfs_get_inode(sb, NULL, S_IFDIR | fsi->mount_opts.mode, 0);  //建立根目录索引节点  
      21. 234         sb->s_root = d_make_root(inode);  //建立根目录目录对象; 超级块的s_root指向刚建立的根目录对象  
      22. 235         if (!sb->s_root)  
      23. 236                 return -ENOMEM;  
      24. 238         return 0;  
      25. 239 }  
      26.   
      27.  54 struct inode *ramfs_get_inode(struct super_block *sb,  
      28.  55                                 const struct inode *dir, umode_t mode, dev_t dev)  
      29.  56 {  
      30.  57         struct inode * inode = new_inode(sb); //在索引节点高速缓存里创建一个inode,  
      31.  59         if (inode) {  
      32.  60                 inode->i_ino = get_next_ino();  //获取一个inode number  
      33.  61                 inode_init_owner(inode, dir, mode); //设置inode,初始化uid gid mode等  
      34.  62                 inode->i_mapping->a_ops = &ramfs_aops;  
      35.  63                 inode->i_mapping->backing_dev_info = &ramfs_backing_dev_info;  
      36.  64                 mapping_set_gfp_mask(inode->i_mapping, GFP_HIGHUSER);  
      37.  65                 mapping_set_unevictable(inode->i_mapping);  
      38.  66                 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;  
      39.  67                 switch (mode & S_IFMT) {  
      40.  68                 default:               //特殊文件;如:字符~块设备文件,FIFO,SOCKET文件  
      41.  69                         init_special_inode(inode, mode, dev);  
      42.  70                         break;  
      43.  71                 case S_IFREG:         //普通文件  
      44.  72                         inode->i_op = &ramfs_file_inode_operations;  
      45.  73                         inode->i_fop = &ramfs_file_operations;  
      46.  74                         break;  
      47.  75                 case S_IFDIR:         //目录文件  
      48.  76                         inode->i_op = &ramfs_dir_inode_operations;  
      49.  77                         inode->i_fop = &simple_dir_operations;  
      50.  79                         /* directory inodes start off with i_nlink == 2 (for "." entry) */  
      51.  80                         inc_nlink(inode);  
      52.  81                         break;  
      53.  82                 case S_IFLNK:         //链接文件  
      54.  83                         inode->i_op = &page_symlink_inode_operations;  
      55.  84                         break;  
      56.  85                 }  
      57.  86         }  
      58.  87         return inode;              //返回创建的inode与对应的目录项对象关联  
      59.  88 }  
  • 相关阅读:
    MinIO:入门
    JS中面向对象的多种继承方式
    点击按钮实现图片下载
    给大家推荐一个免费的云平台-阿贝云
    REPLACE
    SUBSTRING_REGEXPR 截取字符串
    基本的git/linux/g++/ 等指令
    C++引用和指针&, *
    go语言异常处理 error panic recover defer
    django.db.utils.IntegrityError: (1048, "Column 'id' cannot be null")
  • 原文地址:https://www.cnblogs.com/zhiliao112/p/4067848.html
Copyright © 2011-2022 走看看