linux的虚拟文件系统VFS
虚拟文件系统(virtual file system),别名虚拟文件系统开关,是linux中的一个软件层,向用户空间提供文件系统操作接口。
VFS包含的系统调用包括open(2)、stat(2)、read(2)、write(2)、chmod(2)等等,这些系统调用在进程环境中执行。下面几个重要的数据结构是VFS(虚拟文件系统中涉及到的数据结构):
1、Directory Entry Cache(dcache)
VFS实现了系统调用open(2)、stat(2)、chmod(2),和其他类似的文件系统调用。用于这些系统调用中的参数被VFS用来进行directory entry cache的对象搜索,其中directory entry cache有多个名字,比如dentry cache或者dcache。 通过dentry cache提供了一种快速查找机制,可以快速的根据路径名字找到具体的dentry。需要特别说明的是:dentry是存在于内存中的,不会保存到磁盘上进行永久性存储的。
2、inode 对象
每一个dentry对象都有一个指针指向具体的inode对象。inode是文件系统的对象,比如正规文件(regular file)、目录(directory)、FIFO(管道)。这些inode对象存在于磁盘(块设备文件文件系统)或者内存(linux下的伪文件系统procfs)。如果是存在于磁盘上的inode对象则需要加载到内存中去,当发生的改变需要保存到磁盘上去。一个inode对象可能有多个dentry对象指向它(因为linux下实现了硬连接特性)。
为了查找一个具体的inode对象需要在父目录的inode上执行lookup操作。而具体的lookup方法是由inode存放的文件系统具体的实现,这个操作是由具体的文件系统进行初始化。一旦VFS拥有具体的dentry对象的时候,很多系统调用就可以迅速完成,比如stat(2)系统调用,它就是获取inode上的数据,根据dentry上的指针可以迅速的找到inode对象。
3、File对象
打开一个文件还需要另外一个操作:分配一个File结构的对象(这是linux内核端的文件描述符的实现)。新申请的File对象初始化了一个指针指向dentry对象和多个文件操作函数(比如read、write、mmap等操作)。File数据结构放在这个进程的文件描述表中(file descriptor table)。
用户空间的read、write等操作都是通过用户空间态(userspace file descriptor)的描述符获得正确的File结构,然后调用File结构中的具体的方法。只要文件处于打开状态,linux内核中就有对应的dentry对象,自然也有对象的inode对象。
4、文件系统对象(filesystem)
登记和卸载一个文件系统使用下面的函数调用。
#include <linux/fs.h>
extern int register_filesystem(struct file_system_type *);
extern int unregister_filesystem(struct file_system_type *);
传入的参数struct file_system_type描述了具体的文件系统。当发出一个命令,需要在你的名字空间的一个目录上挂载一个文件系统的时候,VFS会调用具体文件中的mount方法。当mount操作完成之后,会返回一个struct dentry对象,这个时候会新建一个vfsmount对象,用于指向这个dentry对象,同时这个vfsmount对象会添加到这个挂载点上。所以当路径解析到达这个目录的时候,会跳转到这个vfsmount指向的文件系统中去。
在/proc/filesystems下可以观察到所有挂载的文件系统。
以下是file_system_type结构:
struct file_system_type {
const char *name;
int fs_flags;
struct dentry *(*mount) (struct file_system_type *, int,
const char *, void *);
void (*kill_sb) (struct super_block *);
struct module *owner;
struct file_system_type * next;
struct list_head fs_supers;
struct lock_class_key s_lock_key;
struct lock_class_key s_umount_key;
};
name:文件系统的名字,当前支持的文件名字有“ext2”,“ext3”,“ext4”,“msdos”等
fs_flags:一些标志,比如:FS_REQUIRES_DEV, FS_NO_DCACHE
mount:重要的field,当一个filesystem实例化的时候需要具体的filesystem的mount方法
5、superblock对象
一个superblock(超级块)对象代表了一个挂载的文件系统。
下面是关于超级块的操作的数据结构:
struct super_operations {
struct inode *(*alloc_inode)(struct super_block *sb);
void (*destroy_inode)(struct inode *);
void (*dirty_inode) (struct inode *, int flags);
int (*write_inode) (struct inode *, int);
void (*drop_inode) (struct inode *);
void (*delete_inode) (struct inode *);
void (*put_super) (struct super_block *);
int (*sync_fs)(struct super_block *sb, int wait);
int (*freeze_fs) (struct super_block *);
int (*unfreeze_fs) (struct super_block *);
int (*statfs) (struct dentry *, struct kstatfs *);
int (*remount_fs) (struct super_block *, int *, char *);
void (*clear_inode) (struct inode *);
void (*umount_begin) (struct super_block *);
int (*show_options)(struct seq_file *, struct dentry *);
ssize_t (*quota_read)(struct super_block *, int, char *, size_t, loff_t);
ssize_t (*quota_write)(struct super_block *, int, const char *, size_t, loff_t);
int (*nr_cached_objects)(struct super_block *);
void (*free_cached_objects)(struct super_block *, int);
};
所有的方法都没有lock来维持,这说明了这些方法只可以在一个进程上下文中执行,不能在中断上下文中执行。
6、address space对象
address_space对象用于对page cache中的page进行分组和管理。它可以用来跟踪一个文件中的page cache和一个文件中映射到进程地址空间中的文件区域。
address space提供了大量有显著特征的服务,比如根据通过地址查找page,追踪标志为Dirty或者WriteBack的page。
Further Reading:
1.Creating Linux virtual filesystems. 2002. http://lwn.net/Articles/13325/
2.A tour of the Linux VFS by Michael K. Johnson. 1996. http://www.tldp.org/LDP/khg/HyperNews/get/fs/vfstour.html
linux的虚拟文件系统VFS的更多相关文章
- 使用 /proc 文件系统来访问 linux操作系统 内核的内容 && 虚拟文件系统vfs及proc详解
http://blog.163.com/he_junwei/blog/static/19793764620152743325659/ http://www.01yun.com/other/201304 ...
- Linux虚拟文件系统VFS解决
参考<Linux内核设计与实现> 虚拟文件系统(VFS)它是linux核心和详细I/O一个普通的访问接口之间的包装设备,通过这层界面,linux内核能够以同一的方式訪问各种I/O设备. 虚 ...
- 虚拟文件系统(VFS)
原文链接:http://www.orlion.ga/1008/ linux在不同的文件系统之上做了一个抽象层,使得文件.目录.读写访问等概念都成为抽象层概念,这个抽象层被称为虚拟文件系统(VFS). ...
- (转)linux内核虚拟文件系统浅析
转自http://hi.baidu.com/_kouu/item/4e9db87580328244ef1e53d0 ###### 虚拟文件系统(VFS)在我看来, "虚拟"二字主要 ...
- 虚拟文件系统VFS
Linux的文件系统是由虚拟文件系统作为媒介搭建起来的,虚拟文件系统VFS(Virtual File System)是Linux内核层实现的一种架构,为用户空间提供统一的文件操作接口.它在内核内部为不 ...
- (转)linux内核虚拟文件系统浅析【转】
转自:https://www.cnblogs.com/woainilsr/p/3590716.html 转自http://hi.baidu.com/_kouu/item/4e9db8758032824 ...
- linux文件系统体系结构 和 虚拟文件系统(VFS)
图 1. Linux 文件系统组件的体系结构 用户空间包含一些应用程序(例如,文件系统的使用者)和 GNU C 库(glibc),它们为文件系统调用(打开.读取.写和关闭)提供用户接口.系统调用接口的 ...
- linux虚拟文件系统vfs
linux可以挂载不同的文件系统(EXT2,FAT,NTFS),用同一的样式呈现给用户,读写操作用起来都一样,这是怎样做到的呢? linux内核在各种不同的文件系统格式上做了一个抽象层,使得文件.目录 ...
- Linux虚拟文件系统–VFS简介
http://www.embeddedlinux.org.cn/emb-linux/file-system/201712/20-7907.html 导读 Linux中可以支持多种文件系统,而且支持各种 ...
随机推荐
- 使用input做简单的上传图片
css 代码: .container{ width: 200px; height: 200px; border: 1px solid #666; } HTML 代码: <input type=& ...
- jetty插件实现 热部署
<plugin> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-maven-plugin ...
- python判断一个数字是整数还是浮点数&判断整除
判断整数还是浮点数 >>> a=123 >>> b=123.123 >>> isinstance(a,int) True >>&g ...
- 使用 npm 安装 Vue
使用 npm 安装 Vue 需要 node.js 就不多说了(从 nodejs.org 中下载 nodejs ) (1)安装 Vue,在 cmd 里直接输入: npm install -g cnpm ...
- 算法之求质数(Java语言)
质数(Prime number) 又称素数,指在的自然数中,除了1和该数自身外,无法被其他自然数整除的数(也可定义为只有1与该数本身两个因数的数). 算法原理 验证一个数字 n 是否为素数的一种简单但 ...
- (转)informatica 面试题大全
1 What is the difference between a data warehouse and a data mart? Ø Dataware house: It is a collect ...
- c# datarow[] 转换成 datatable, List<T> 转datatable
c# datarow[] 转换成 datatable, List<T> 转datatable DdataRow[]转成Datatable private DataTable ToDat ...
- windows程序设置开机自动启动
//调用方法:设置开机启动 SetAutoRun(Process.GetCurrentProcess().ProcessName, true, Application.StartupPath + @& ...
- 关于Unity3d的Quaternion.Slerp的学习
首先在场景中创建三个cube的GameObject,from表示要转换之前的样子,to表示转换之后的样子,change表示转的效果.如下图所示: 其中from和change cube开始运行之前的tr ...
- ORACLE 11GR2 安装时配置了域,后期删除
因为用了一个安全平台.此平台居然不支持oracle中的服务吗有"."而这个点就是因为当时安装oracle录入了域.原来以为是修改服务名.百多了很多,最后发现就是删除域即可 感谢此文 ...