虚拟文件系统(VFS)是linux内核和具体I/O设备之间的封装的一层共通访问接口,通过这层接口,linux内核可以以同一的方式访问各种I/O设备。
虚拟文件系统本身是linux内核的一部分,是纯软件的东西,并不需要任何硬件的支持。
主要内容:
- 虚拟文件系统的作用
- 虚拟文件系统的4个主要对象
- 文件系统相关的数据结构
- 进程相关的数据结构
- 小结
1. 虚拟文件系统的作用
虚拟文件系统(VFS)是linux内核和存储设备之间的抽象层,主要有以下好处。
- 简化了应用程序的开发:应用通过统一的系统调用访问各种存储介质
- 简化了新文件系统加入内核的过程:新文件系统只要实现VFS的各个接口即可,不需要修改内核部分
2. 虚拟文件系统的4个主要对象
虚拟文件中的4个主要对象,具体每个对象的含义参见如下的详细介绍。
2.1 超级块
超级块(super_block)主要存储文件系统相关的信息,这是个针对文件系统级别的概念。
它一般存储在磁盘的特定扇区中,但是对于那些基于内存的文件系统(比如proc,sysfs),超级块是在使用时创建在内存中的。
超级块的定义在:<linux/fs.h>
/* * 超级块结构中定义的字段非常多, * 这里只介绍一些重要的属性 */ struct super_block { struct list_head s_list; /* 指向所有超级块的链表 */ const struct super_operations *s_op; /* 超级块方法 */ struct dentry *s_root; /* 目录挂载点 */ struct mutex s_lock; /* 超级块信号量 */ int s_count; /* 超级块引用计数 */ struct list_head s_inodes; /* inode链表 */ struct mtd_info *s_mtd; /* 存储磁盘信息 */ fmode_t s_mode; /* 安装权限 */ }; /* * 其中的 s_op 中定义了超级块的操作方法 * 这里只介绍一些相对重要的函数 */ struct super_operations { struct inode *(*alloc_inode)(struct super_block *sb); /* 创建和初始化一个索引节点对象 */ void (*destroy_inode)(struct inode *); /* 释放给定的索引节点 */ void (*dirty_inode) (struct inode *); /* VFS在索引节点被修改时会调用这个函数 */ int (*write_inode) (struct inode *, int); /* 将索引节点写入磁盘,wait表示写操作是否需要同步 */ void (*drop_inode) (struct inode *); /* 最后一个指向索引节点的引用被删除后,VFS会调用这个函数 */ void (*delete_inode) (struct inode *); /* 从磁盘上删除指定的索引节点 */ void (*put_super) (struct super_block *); /* 卸载文件系统时由VFS调用,用来释放超级块 */ void (*write_super) (struct super_block *); /* 用给定的超级块更新磁盘上的超级块 */ int (*sync_fs)(struct super_block *sb, int wait); /* 使文件系统中的数据与磁盘上的数据同步 */ int (*statfs) (struct dentry *, struct kstatfs *); /* VFS调用该函数获取文件系统状态 */ int (*remount_fs) (struct super_block *, int *, char *); /* 指定新的安装选项重新安装文件系统时,VFS会调用该函数 */ void (*clear_inode) (struct inode *); /* VFS调用该函数释放索引节点,并清空包含相关数据的所有页面 */ void (*umount_begin) (struct super_block *); /* VFS调用该函数中断安装操作 */ };