总结:

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(这个暂不介绍).

VFS,super_block,inode,dentry—结构体图解的更多相关文章

  1. 2018.5.2 file结构体

    f_flags,File Status Flag f_pos,表示当前读写位置 f_count,表示引用计数(Reference Count): dup.fork等系统调用会导致多个文件描述符指向同一 ...

  2. inode file 结构

    inode位图(inode Bitmap) 和块位图类似,本身占一个块,其中每个bit表示一个inode是否空闲可用. inode表(inode Table) 我们知道,一个文件除了数据需要存储之外, ...

  3. C语言文件操作 FILE结构体

    内存中的数据都是暂时的,当程序结束时,它们都将丢失.为了永久性的保存大量的数据,C语言提供了对文件的操作. 1.文件和流 C将每个文件简单地作为顺序字节流(如下图).每个文件用文件结束符结束,或者在特 ...

  4. 文件系统系列学习笔记 - inode/dentry/file/super(2)

    此篇文章主要介绍下linux 文件系统下的主要对象及他们之间的关系. 1 inode inode结构中主要包含对文件或者目录原信息的描述,原信息包括但不限于文件大小.文件在磁盘块中的位置信息.权限位. ...

  5. file结构体中private_data指针的疑惑

    转:http://www.360doc.com/content/12/0506/19/1299815_209093142.shtml hi all and barry, 最近在学习字符设备驱动,不太明 ...

  6. file结构体中private_data指针的疑惑【转】

    本文转载自:http://www.cnblogs.com/pengdonglin137/p/3328984.html hi all and barry, 最近在学习字符设备驱动,不太明白private ...

  7. vfs:结构体对象

    VFS结构体 super_block 存储一个已安装的文件系统的控制信息,代表一个已安装的文件系统:每次一个实际的文件系统被安装时, 内核会从磁盘的特定位置读取一些控制信息来填充内存中的超级块对象.一 ...

  8. 文件系统VFS数据结构(超级块 inode dentry file)(收集整理)

    Linux虚拟文件系统四大对象: 1)超级块(super block) 2)索引节点(inode) 3)目录项(dentry) 4)文件对象(file) 一个进程在对一个文件进行操作时各种对象的引用过 ...

  9. Linux字符设备中的两个重要结构体(file、inode)

    对于Linux系统中,一般字符设备和驱动之间的函数调用关系如下图所示 上图描述了用户空间应用程序通过系统调用来调用程序的过程.一般而言在驱动程序的设计中,会关系 struct file 和 struc ...

随机推荐

  1. ssh 通过跳板机连接到远程服务器

    ssh 通过跳板机连接到远程服务器 import paramiko from sshtunnel import SSHTunnelForwarder import threading def read ...

  2. Java入门:绘制简单图形

    在上一节,我们学习了如何使用swing和awt工具创建一个空的窗口,本节学习如何绘制简单图形. 基本绘图介绍 Java中绘制基本图形,可以使用Java类库中的Graphics类,此类位于java.aw ...

  3. Jenkins和Gitblit集成实现提交后自动构建

    Gitblit是一个纯Java基于git的解决方案.它属于Apache Allura等伪造软件之一,它将票务系统与web ui结合在一起.我经历了一个设置过程,我想我可以帮助新用户避免痛点. 使用场景 ...

  4. GetVersionEx 正确获取windows10版本

    vs2008直接将下面xml保存成文件添加到资源文件 vc的话insert-->Resource-->Custom-->输入24,ok-->id改为1-->把下面内容保存 ...

  5. string 中的一些优化事项

    1.1 fmt  vs  "+" (无转义) import ( "testing" "fmt" ) var ( str = "he ...

  6. Cloudstack安装(二)

    Cloudstack安装 官方文档参考: http://docs.cloudstack.apache.org/projects/cloudstack-installation/en/4.9/qig.h ...

  7. caffe 配置文件详解

    主要是遇坑了,要记录一下. solver算是caffe的核心的核心,它协调着整个模型的运作.caffe程序运行必带的一个参数就是solver配置文件.运行代码一般为 # caffe train --s ...

  8. 关于ARGB_8888、ALPHA_8、ARGB_4444、RGB_565的理解

    关于ARGB_8888.ALPHA_8.ARGB_4444.RGB_565的理解 A:透明度 R:红色 G:绿 B:蓝 Bitmap.Config ARGB_4444:每个像素占四位,即A=4,R=4 ...

  9. ElastAlert监控日志告警Web攻击行为

    由于公司需要监控web攻击行为,而因某些原因搭不了waf,才不得不用ElastAlert进行告警,此为前提. 一.ELK安装 Elasticsearch 是一个分布式.可扩展.实时的搜索与数据分析引擎 ...

  10. css3-自定义字体

    参考链接http://www.w3cplus.com/content/css3-font-face 出处W3CPLUS css3-自定义字体   @font-face @font-face是CSS3中 ...