VFS虚拟文件系统

VFS虚拟文件系统

虚拟文件系统的意义:

为用户空间程序提供了文件和文件系统相关的接口。通过虚拟文件系统,程序可以利用标准的Unix系统调用对不同的文件系统,甚至不同的介质上的文件系统进行读写操作。

VFS概述

VFS是一种软件机制,与它相关的数据结构都只存在于物理内存当中。所以在每次系统初始化期间,linux都首先要在内存中构造一颗VFS的目录树(在linux的源码中称之为namespace) 对于每个已经挂载的文件系统,VFS在内核中都生成一个超级块结构(struct super_block实例),超级块代表一个已经安装的文件系统,用于存储文件系统的控制信息,例如文件系统类型、大小、所有inode对象、脏的inode链表等。inode和super block在存储介质中都是有实际映射的,即存储介质中也存在超级块和inode。但是由于不同类型的文件系统差异,超级块和inode的结构不尽相同。而VFS的作用就是通过具体的设备驱动获得某个文件系统中的超级块和inode节点,然后将其中的信息填充到内核中的struct super_block和struct inode中,以此来试图对不同文件系统进行统一管理。由于块设备速度较慢(于内存而言),可能需要很长时间才能找到与一个文件名关联的inode。Linux使用目录项(dentry)缓存来快速访问此前的查找操作结果。在VFS读取了一个目录或文件的数据之后,则创建一个dentry实例(struct dentry),以缓存找到的数据。dentry结构的主要用途就是建立文件名和相关的inode之间的联系。一个文件系统中的dentry对象都被放在一个散列表中,同时不再使用的dentry对象被放到超级块指向的一个LRU链表中,在某个时间点会删除比较老的对象以释放内存。

另外简单提一下两个数据结构:

每种注册到内核的文件系统类型以struct file_system_type结构表示,每种文件系统类型中都有一个链表,指向所有属于该类型的文件系统的超级块。

当一个文件系统挂载到内核文件系统的目录树上,会生成一个挂载点,用来管理所挂载的文件系统的信息。该挂载点用一个struct vfsmount结构表示。

以上的结构关系大致如下:

文件系统抽象层

能够使用通用接口对所有类型的文件系统进行操作,是因为内核在它的底层文件系统接口上建立了一个抽象层。 VFS抽象层之所以能够衔接各种各样的文件系统,是因为它定义了所有文件系统都支持的、基本的、概念上的接口和数据结构。

Unix文件系统

Unix使用四种和文件系统相关的传统抽象概念:文件、目录项、索引节点和安装节点 。 linux系统中所有都看作是文件,文件通过目录组织起来。在Unix系统当中,目录属于普通文件,它列出包含在其中的所有文件。 Unix系统当中将文件的相关信息和文件本身这两个概念加以区分,例如访问控制权限等。文件的相关信息,有时也被称为文件的元数据,被存储在单独的数据结构当中,该结构被称之为索引节点(inode)。 对于其他的一些文件系统,比如FAT NTFS,他们没有索引节点的概念,但是如果需要在Unix系统中工作的话,还是需要进行封装,封装成适合Unix系统的格式。

VFS对象及其数据结构

VFS其实采用的是面向对象的设计思路,使用一组数据结构来代表通用文件对象。 VFS中有四个主要的对象类型,分别是: 超级块对象,它代表一个具体的已经安装的文件系统 索引节点对象,代表一个具体的文件 目录项对象,代表一个目录项,是路径的一个组成部分。目录是一个文件,不要和目录项混淆 文件对象,它代表由进程打开的文件 每个对象都包含一个操作对象(即对数据的操作方法,函数),这些操作对象描述了内核针对主要对象可以使用的方法: super_operations对象,其中包括内核针对特定文件系统所能够调用的方法 inode_operations对象,内核针对特定文件所能够调用的方法 dentry_operations对象,其中包括内核针对特定目录所能调用的方法 file_operations对象,其中包括进程针对已打开文件所能够调用的方法

超级块对象

各种文件系统都必须实现超级块对象,该对象用于存储特定文件系统的信息。通常对应于存放在磁盘特定扇区中的文件系统超级块或文件系统控制块。

struct super_block {
/* 全局链表元素 */
struct list_head s_list;
/* 底层文件系统所在的设备 */
dev_t s_dev;
/* 文件系统中每一块的长度 */
unsigned long s_blocksize;
/* 文件系统中每一块的长度(以2为底的对数) */
unsigned char s_blocksize_bits;
/* 是否需要向磁盘回写 */
unsigned char s_dirt;
unsigned long long s_maxbytes; /* Max file size */
/* 文件系统类型 */
struct file_system_type *s_type;
/* 超级块操作方法 */
const struct super_operations *s_op;
struct dquot_operations *dq_op;
struct quotactl_ops *s_qcop;
const struct export_operations *s_export_op;
unsigned long s_flags;
unsigned long s_magic;
/* 全局根目录的dentry */
struct dentry *s_root;
struct rw_semaphore s_umount;
struct mutex s_lock;
int s_count;
int s_need_sync;
atomic_t s_active;
#ifdef CONFIG_SECURITY
void *s_security;
#endif
struct xattr_handler **s_xattr;
/* 超级块管理的所有inode的链表 */
struct list_head s_inodes; /* all inodes */
/* 脏的inode的链表 */
struct list_head s_dirty; /* dirty inodes */
struct list_head s_io; /* parked for writeback */
struct list_head s_more_io; /* parked for more writeback */
struct hlist_head s_anon; /* anonymous dentries for (nfs) exporting */
/* file结构的链表,该超级块上所有打开的文件 */
struct list_head s_files;
/* s_dentry_lru and s_nr_dentry_unused are protected by dcache_lock */
/* 不再使用的dentry的LRU链表 */
struct list_head s_dentry_lru; /* unused dentry lru */
int s_nr_dentry_unused; /* # of dentry on lru */struct block_device *s_bdev;
struct mtd_info *s_mtd;
/* 相同文件系统类型的超级块链表的节点 */
struct list_head s_instances;
struct quota_info s_dquot; /* Diskquota specific options */int s_frozen;
wait_queue_head_t s_wait_unfrozen;char s_id[32]; /* Informational name */void *s_fs_info; /* Filesystem private info */
fmode_t s_mode;/*
* The next field is for VFS *only*. No filesystems have any business
* even looking at it. You had been warned.
*/
struct mutex s_vfs_rename_mutex; /* Kludge *//* Granularity of c/m/atime in ns.
Cannot be worse than a second */
u32 s_time_gran;/*
* Filesystem subtype. If non-empty the filesystem type field
* in /proc/mounts will be "type.subtype"
*/
char *s_subtype;/*
* Saved mount options for lazy filesystems using
* generic_show_options()
*/
char *s_options;
};


索引节点对象

索引节点对象包含了内核在操作文件或目录时需要的全部信息。

struct inode {
/* 全局的散列表 */
struct hlist_node i_hash;
/* 根据inode的状态可能处理不同的链表中(inode_unused/inode_in_use/super_block->dirty) */
struct list_head i_list;
/* super_block->s_inodes链表的节点 */
struct list_head i_sb_list;
/* inode对应的dentry链表,可能多个dentry指向同一个文件 */
struct list_head i_dentry;
/* inode编号 */
unsigned long i_ino;
/* 访问该inode的进程数目 */
atomic_t i_count;
/* inode的硬链接数 */
unsigned int i_nlink;
uid_t i_uid;
gid_t i_gid;
/* inode表示设备文件时的设备号 */
dev_t i_rdev;
u64 i_version;
/* 文件的大小,以字节为单位 */
loff_t i_size;
#ifdef __NEED_I_SIZE_ORDERED
seqcount_t i_size_seqcount;
#endif
/* 最后访问时间 */
struct timespec i_atime;
/* 最后修改inode数据的时间 */
struct timespec i_mtime;
/* 最后修改inode自身的时间 */
struct timespec i_ctime;
/* 以block为单位的inode的大小 */
blkcnt_t i_blocks;
unsigned int i_blkbits;
unsigned short i_bytes;
/* 文件属性,低12位为文件访问权限,同chmod参数含义,其余位为文件类型,如普通文件、目录、socket、设备文件等 */
umode_t i_mode;
spinlock_t i_lock; /* i_blocks, i_bytes, maybe i_size */
struct mutex i_mutex;
struct rw_semaphore i_alloc_sem;
/* inode操作 */
const struct inode_operations *i_op;
/* file操作 */
const struct file_operations *i_fop;
/* inode所属的super_block */
struct super_block *i_sb;
struct file_lock *i_flock;
/* inode的地址空间映射 */
struct address_space *i_mapping;
struct address_space i_data;
#ifdef CONFIG_QUOTA
struct dquot *i_dquot[MAXQUOTAS];
#endif
struct list_head i_devices; /* 若为设备文件的inode,则为设备的打开文件列表节点 */
union {
struct pipe_inode_info *i_pipe;
struct block_device *i_bdev; /* 若为块设备的inode,则指向该设备实例 */
struct cdev *i_cdev; /* 若为字符设备的inode,则指向该设备实例 */
};__u32 i_generation;#ifdef CONFIG_FSNOTIFY
__u32 i_fsnotify_mask; /* all events this inode cares about */
struct hlist_head i_fsnotify_mark_entries; /* fsnotify mark entries */
#endif#ifdef CONFIG_INOTIFY
struct list_head inotify_watches; /* watches on this inode */
struct mutex inotify_mutex; /* protects the watches list */
#endifunsigned long i_state;
unsigned long dirtied_when; /* jiffies of first dirtying */unsigned int i_flags; /* 文件打开标记,如noatime */atomic_t i_writecount;
#ifdef CONFIG_SECURITY
void *i_security;
#endif
#ifdef CONFIG_FS_POSIX_ACL
struct posix_acl *i_acl;
struct posix_acl *i_default_acl;
#endif
void *i_private; /* fs or device private pointer */
};

目录项对象

VFS将目录看做文件,对于一个路径来说,路径中的每一个组成部分都由一个索引节点对象表示。 VFS经常需要执行目录相关的操作,比如路径名查找等。为了方便查找操作,VFS引入了目录项的概念。因为文件的访问一般具有空间和时间上的局限性。 即多数时候我们需要对同一个文件进行读写访问,这是时间上的局限性。有时是对同个目录下的多个文件进行访问,这是空间上的局限性。而将该路径上的所有目录项都进行缓存到目录项缓存中,可以极大的缩小我们的搜索时间,提高命中率。所以引入了目录项对象的操作。 而且目录项对象和前面两个对象不同,目录项对象没有对应的磁盘数据结构,VFS根据字符串形式的路径名现场创建它。 目录项的状态 目录项对象有三种状态:被使用、未被使用、负状态 被使用的目录项:是指目录项对应一个有效的索引节点,表明该对象存在一个或者多个使用者。 未被使用的目录项:是指目录项对应一个有效的索引节点,但是VFS当前并没有使用它,即当前指向的文件并没有被占用,但是该目录项仍然被保存在缓存中,以便于需要的时候再次使用它。 负状态的目录项:是指目录项没有对应有效的索引节点。即该索引节点已经被删除,但是目录项仍然保存在缓存中。

struct dentry {
atomic_t d_count;
unsigned int d_flags; /* protected by d_lock */
spinlock_t d_lock; /* per dentry lock */
/* 该dentry是否是一个装载点 */
int d_mounted;
/* 文件所属的inode */
struct inode *d_inode;
/*
* The next three fields are touched by __d_lookup. Place them here so they all fit in a cache line.
*/
/* 全局的dentry散列表 */
struct hlist_node d_hash; /* lookup hash list */
/* 父目录的dentry */
struct dentry *d_parent; /* parent directory */
/* 文件的名称,例如对/tmp/a.sh,文件名即为a.sh */
struct qstr d_name;
/* 脏的dentry链表的节点 */
struct list_head d_lru; /* LRU list */
/*
* d_child and d_rcu can share memory
*/
union {
struct list_head d_child; /* child of parent list */
struct rcu_head d_rcu;
} d_u;
/* 该dentry子目录中的dentry的节点链表 */
struct list_head d_subdirs; /* our children */
/* 硬链接使用几个不同名称表示同一个文件时,用于连接各个dentry */
struct list_head d_alias; /* inode alias list */
unsigned long d_time; /* used by d_revalidate */
const struct dentry_operations *d_op;
/* 所属的super_block */
struct super_block *d_sb; /* The root of the dentry tree */
void *d_fsdata; /* fs-specific data */
/* 如果文件名由少量字符组成,在保存在这里,加速访问 */
unsigned char d_iname[DNAME_INLINE_LEN_MIN]; /* small names */
};

文件对象

文件对象表示进程已经打开的文件。从用户的角度来看VFS,文件对象是我们先接触到的东西。

文件对象是已打开的文件在内存中的表示。因为多个进程可以操作同一个文件,所以同一个文件可能有多个对应的文件对象。

文件对象仅仅在进程观点上代表已经打开的文件,它反过来指向目录项对象(反过来指向索引节点)

与进程相关的数据结构

进程会记录自身使用的全部资源。比如文件、内存等。对于文件的记录,就是使用到了VFS提供的三个数据结构。

file_struct{}结构体,里面会保存进程全部打开的文件对象。由进程描述符中的files目录项所指向。

fs_struct{}结构体。由进程描述符的fs域指向,包含文件系统和进程的信息。该结构包含了当前进程的工作目录和根目录

namespace结构体。由进程描述符中的mmt_namespace域指向。因为子进程一般是拷贝父进程而来,默认情况下,所有的进程使用同样的命名空间,这样进程认识的根目录才是一样的。(而不是类似于windows下的各个盘符)。但是后来的内核提供了单进程命名空间(即单个进程可以看到唯一的安装文件系统)

struct task_struct {
……
/ filesystem information /
struct fs_struct *fs;
/ open file information /
struct files_struct *files;
/ namespaces /
struct nsproxy *nsproxy;
……
}

从上图可以看到,fs中包含了文件系统的挂载点和挂载点的dentry信息。而files指向了一系列的struct file结构,其中struct path结构用于将struct file和vfsmount以及dentry联系起来。struct file保存了内核所看到的文件的特征信息,进程打开的文件列表就存放在task_struct->files->fd_array[]数组以及fdtable中。

task_struct结构还存放了其打开文件的文件描述符fd的信息,这是用户进程需要用到的,用户进程在通过文件名打开一个文件后,文件名就没有用处了,之后的操作都是对文件描述符fd的,在内核中,fget_light()函数用于通过整数fd来查找对应的struct file对象。由于每个进程都维护了自己的fd列表,所以不同进程维护的fd的值可以重复,例如标准输入、标准输出和标准错误对应的fd分别为0、1、2。

struct file的mapping成员指向属于文件相关的inode实例的地址空间映射,通常它设置为inode->i_mapping。在读写一个文件时,每次都从物理设备上获取文件的话,速度会很慢,在内核中对每个文件分配一个地址空间,实际上是这个文件的数据缓存区域,在读写文件时只是操作这块缓存,通过内核有相应的同步机制将脏的页写回物理设备。super_block中维护了一个脏的inode的链表。

struct file的f_op成员指向一个struct file_operations实例(图中画错了,不是f_pos),该结构保存了指向所有可能文件操作的指针,如read/write/open等。

struct file_operations {
struct module *owner;
loff_t (*llseek) (struct file *, loff_t, int);
ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
ssize_t (*aio_read) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
ssize_t (*aio_write) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
int (*readdir) (struct file *, void *, filldir_t);
unsigned int (*poll) (struct file *, struct poll_table_struct *);
int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);
long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
int (*mmap) (struct file *, struct vm_area_struct *);
int (*open) (struct inode *, struct file *);
int (*flush) (struct file *, fl_owner_t id);
int (*release) (struct inode *, struct file *);
int (*fsync) (struct file *, struct dentry *, int datasync);
……
};
struct vfsmount {
/* 全局散列表 */
struct list_head mnt_hash;
/* 父文件系统的挂载点 */
struct vfsmount *mnt_parent; /* fs we are mounted on */
/* 父文件系统中该挂载点的dentry */
struct dentry *mnt_mountpoint; /* dentry of mountpoint */
/* 当前文件系统中该挂载点的dentry */
struct dentry *mnt_root; /* root of the mounted tree */
/* 指向super_block */
struct super_block *mnt_sb; /* pointer to superblock */
/* 该挂载点下面的子挂载点列表 */
struct list_head mnt_mounts; /* list of children, anchored here */
/* 父文件系统的子挂载点的列表节点 */
struct list_head mnt_child; /* and going through their mnt_child */
int mnt_flags;
/* 4 bytes hole on 64bits arches */
/* 挂载的设备,如/dev/dsk/hda1 */
const char *mnt_devname;
/* 虚拟文件系统命名空间中的链表节点 */
struct list_head mnt_list;
struct list_head mnt_expire; /* link in fs-specific expiry list */
struct list_head mnt_share; /* circular list of shared mounts */
struct list_head mnt_slave_list;/* list of slave mounts */
struct list_head mnt_slave; /* slave list entry */
struct vfsmount *mnt_master; /* slave is on master->mnt_slave_list */
/* 所在的虚拟文件系统命名空间*/
struct mnt_namespace *mnt_ns; /* containing namespace */
int mnt_id; /* mount identifier */
int mnt_group_id; /* peer group identifier */
/*
* We put mnt_count & mnt_expiry_mark at the end of struct vfsmount
* to let these frequently modified fields in a separate cache line
* (so that reads of mnt_flags wont ping-pong on SMP machines)
*/
atomic_t mnt_count;
int mnt_expiry_mark; /* true if marked for expiry */
int mnt_pinned;
int mnt_ghosts;
#ifdef CONFIG_SMP
int *mnt_writers;
#else
int mnt_writers;
#endif
};
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值