VFS,super_block,inode,dentry—结构体图解

总结:
VFS只存在于内存中,它在系统启动时被创建,系统关闭时注销。
VFS的作用就是屏蔽各类文件系统的差异,给用户、应用程序、甚至Linux其他管理模块提供统一的接口集合。
管理VFS数据结构的组成部分主要包括超级块和inode。
 
VFS是物理文件系统与服务之间的一个接口层,它对Linux的每个文件系统的所有细节进行抽象,使得不同的文件系统在Linux核心以及系统中运行的进程看来都是相同的。
严格的说,VFS并不是一种实际的文件系统。它只存在于内存中,不存在于任何外存空间。VFS在系统启动时建立,在系统关闭时消亡。
 
VFS使Linux同时安装、支持许多不同类型的文件系统成为可能。VFS拥有关于各种特殊文件系统的公共界面,当某个进程发布了一个面向文件的系统调用时,内核将调用VFS中对应的函数,这个函数处理一些与物理结构无关的操作,并且把它重定向为真实文件系统中相应的函数调用,后者用来处理那些与物理结构相关的操作。
 
下图就是逻辑上对VFS及其下层实际文件系统的组织图,可以看到用户层只能于VFS打交道,而不能直接访问实际的文件系统,比如EXT2、EXT3、PROC,换句话说,
就是用户层不用也不能区别对待这些真正的文件系统,不过,SOCKET虽然也属于VFS的管辖范围,但是有其特殊性,
就是不能像打开大部分文件系统下的“文件”一样打开socket,它只能被创建,而且内核中对其有特殊性处理。
 
 
    VFS描述文件系统使用超级块和inode 的方式,所谓超级块就是对所有文件系统的管理机构,每种文件系统都要把自己的信息挂到super_blocks这么一个全局链表上。
内核中是分成2个步骤完成:首先每个文件系统必须通过register_filesystem函数将自己的file_system_type挂接到file_systems这个全局变量上,
然后调用kern_mount函数把自己的文件相关操作函数集合表挂到super_blocks上。每种文件系统类型的读超级块的例程(get_sb)必须由自己实现。
 
    文件系统由子目录和文件构成。每个子目录和文件只能由唯一的inode 描述。inode 是Linux管理文件系统的最基本单位,也是文件系统连接任何子目录、文件的桥梁。
VFS inode的内容取自物理设备上的文件系统,由文件系统指定的操作函数(i_op 属性指定)填写。VFS inode只存在于内存中,可通过inode缓存访问。
 

1、super_block

  • 相关的数据结构为:
  1. struct super_block
  2. {
  3. struct list_head s_list;/* Keep this first */// 连接super_block的链表
  4. dev_t s_dev;/* search index; _not_ kdev_t */
  5. unsignedlong s_blocksize;
  6. unsignedlong s_old_blocksize;
  7. unsignedchar s_blocksize_bits;
  8. unsignedchar s_dirt;
  9. unsignedlonglong s_maxbytes;/* Max file size */
  10. struct file_system_type *s_type;// 所表示的文件系统的类型
  11. struct super_operations *s_op;// 文件相关操作函数集合表
  12. struct dquot_operations *dq_op;//
  13. struct quotactl_ops *s_qcop;//
  14. struct export_operations *s_export_op;//
  15. unsignedlong s_flags;//
  16. unsignedlong s_magic;//
  17. struct dentry *s_root;// Linux文件系统中某个索引节点(inode)的链接
  18. struct rw_semaphore s_umount;//
  19. struct semaphore s_lock;//
  20. int s_count;//
  21. int s_syncing;//
  22. int s_need_sync_fs;//
  23. atomic_t s_active;//
  24. void*s_security;//
  25. struct xattr_handler **s_xattr;//
  26. struct list_head s_inodes;/* all inodes */// 链接文件系统的inode
  27. struct list_head s_dirty;/* dirty inodes */
  28. struct list_head s_io;/* parked for writeback */
  29. struct hlist_head s_anon;/* anonymous dentries for (nfs) exporting */
  30. struct list_head s_files;// 对于每一个打开的文件,由file对象来表示。链接文件系统中file
  31. struct block_device *s_bdev;//
  32. struct list_head s_instances;//
  33. struct quota_info s_dquot;/* Diskquota specific options */
  34. int s_frozen;//
  35. wait_queue_head_t s_wait_unfrozen;//
  36. char s_id[32];/* Informational name */
  37. void*s_fs_info;/* Filesystem private info */
  38. /**
  39. * The next field is for VFS *only*. No filesystems have any business
  40. * even looking at it. You had been warned.
  41. */
  42. struct semaphore s_vfs_rename_sem;/* Kludge */
  43. /* Granuality of c/m/atime in ns.
  44. Cannot be worse than a second */
  45. u32 s_time_gran;
  46. };
 
  • super_block存在于两个链表中,一个是系统所有super_block的链表, 一个是对于特定的文件系统的super_block链表.
      所有的super_block都存在于 super_blocks(VFS管理层) 链表中:

  • 对于特定的文件系统(文件系统层的具体文件系统), 该文件系统的所有的super_block 都存在于file_sytem_type中的fs_supers链表中.
     而所有的文件系统,都存在于file_systems链表中.这是通过调用register_filesystem接口来注册文件系统的.
     int register_filesystem(struct file_system_type * fs) 

 
 
2、inode
inode(发音:eye-node)译成中文就是索引节点,它用来存放档案及目录的基本信息,包含时间、档名、使用者及群组等。
 
inode 是 UNIX 操作系统中的一种数据结构,其本质是结构体,它包含了与文件系统中各个文件相关的一些重要信息。在 UNIX 中创建文件系统时,同时将会创建大量的 inode 。
通常,文件系统磁盘空间中大约百分之一空间分配给了 inode 表。
dentry的中文名称是目录项,是Linux文件系统中某个索引节点(inode)的链接。这个索引节点可以是文件的,也可以是目录的。
 
inode对应于物理磁盘上的具体对象,dentry是一个内存实体,其中的d_inode成员指向对应的inode。
相关的数据结构为:
  1. /*
  2. * Keep mostly read-only and often accessed (especially for
  3. * the RCU path lookup and 'stat' data) fields at the beginning
  4. * of the 'struct inode'
  5. */
  6. struct inode
  7. {
  8. umode_t i_mode;
  9. unsignedshort i_opflags;
  10. kuid_t i_uid;
  11. kgid_t i_gid;
  12. unsignedint i_flags;
  13. #ifdef CONFIG_FS_POSIX_ACL
  14. struct posix_acl *i_acl;
  15. struct posix_acl *i_default_acl;
  16. #endif
  17. conststruct inode_operations *i_op;
  18. struct super_block *i_sb;
  19. struct address_space *i_mapping;
  20. #ifdef CONFIG_SECURITY
  21. void*i_security;
  22. #endif
  23. /* Stat data, not accessed from path walking */
  24. unsignedlong i_ino;
  25. /*
  26. * Filesystems may only read i_nlink directly. They shall use the
  27. * following functions for modification:
  28. *
  29. * (set|clear|inc|drop)_nlink
  30. * inode_(inc|dec)_link_count
  31. */
  32. union
  33. {
  34. constunsignedint i_nlink;
  35. unsignedint __i_nlink;
  36. };
  37. dev_t i_rdev;
  38. loff_t i_size;
  39. struct timespec i_atime;
  40. struct timespec i_mtime;
  41. struct timespec i_ctime;
  42. spinlock_t i_lock;/* i_blocks, i_bytes, maybe i_size */
  43. unsignedshort i_bytes;
  44. unsignedint i_blkbits;
  45. blkcnt_t i_blocks;
  46. #ifdef __NEED_I_SIZE_ORDERED
  47. seqcount_t i_size_seqcount;
  48. #endif
  49. /* Misc */
  50. unsignedlong i_state;
  51. struct mutex i_mutex;
  52. unsignedlong dirtied_when;/* jiffies of first dirtying */
  53. unsignedlong dirtied_time_when;
  54. struct hlist_node i_hash;
  55. struct list_head i_wb_list;/* backing dev IO list */
  56. struct list_head i_lru;/* inode LRU list */
  57. struct list_head i_sb_list;
  58. union
  59. {
  60. struct hlist_head i_dentry;
  61. struct rcu_head i_rcu;
  62. };
  63. u64 i_version;
  64. atomic_t i_count;
  65. atomic_t i_dio_count;
  66. atomic_t i_writecount;
  67. #ifdef CONFIG_IMA
  68. atomic_t i_readcount;/* struct files open RO */
  69. #endif
  70. conststruct file_operations *i_fop;/* former ->i_op->default_file_ops */
  71. struct file_lock_context *i_flctx;
  72. struct address_space i_data;
  73. struct list_head i_devices;
  74. union
  75. {
  76. struct pipe_inode_info *i_pipe;
  77. struct block_device *i_bdev;
  78. struct cdev *i_cdev;
  79. };
  80. __u32 i_generation;
  81. #ifdef CONFIG_FSNOTIFY
  82. __u32 i_fsnotify_mask;/* all events this inode cares about */
  83. struct hlist_head i_fsnotify_marks;
  84. #endif
  85. void*i_private;/* fs or device private pointer */
  86. };
inode存在于两个双向链表中:
一个是inode所在文件系统的super_block的 s_inodes 链表中
一个是根据inode的使用状态存在于以下三个链表中的某个链表中:
  • 未用的: inode_unused 链表
  • 正在使用的: inode_in_use 链表
  • 脏的: super block中的s_dirty 链表
另外,还有一个重要的链表: inode_hashtable(这个暂不介绍).

3、dentry
  1. struct dentry
  2. {
  3. /* RCU lookup touched fields */
  4. unsignedint d_flags;/* protected by d_lock */
  5. seqcount_t d_seq;/* per dentry seqlock */
  6. struct hlist_bl_node d_hash;/* lookup hash list */
  7. struct dentry *d_parent;/* parent directory */
  8. struct qstr d_name;
  9. struct inode *d_inode;/* Where the name belongs to - NULL is
  10. * negative */
  11. unsignedchar d_iname[DNAME_INLINE_LEN];/* small names */
  12. /* Ref lookup also touches following */
  13. struct lockref d_lockref;/* per-dentry lock and refcount */
  14. conststruct dentry_operations *d_op;
  15. struct super_block *d_sb;/* The root of the dentry tree */
  16. unsignedlong d_time;/* used by d_revalidate */
  17. void*d_fsdata;/* fs-specific data */
  18. struct list_head d_lru;/* LRU list */
  19. struct list_head d_child;/* child of parent list */
  20. struct list_head d_subdirs;/* our children */
  21. /*
  22. * d_alias and d_rcu can share memory
  23. */
  24. union
  25. {
  26. struct hlist_node d_alias;/* inode alias list */
  27. struct rcu_head d_rcu;
  28. } d_u;
  29. };
dentry对象存在于三个双向链表中:
  • 所有未用的目录项: dentry_unused 链表
  • 正在使用的目录项: 对应inode的 i_dentry 链表
  • 表示父子目录结构的链表
另外,还有一个重要的链表: inode_hashtable(这个暂不介绍).





posted @ 2015-07-31 09:31  指上弹兵赵小括  阅读(10109)  评论(0编辑  收藏  举报