Linux VFS相关结构体

1. 概述

Linux 虚拟文件系统是建立在具体文件系统之上,其包括几种主要的对象,分别是超级块对象,目录项对象,索引节点对象,与进程相关的文件对象,安装点对象,文件系统类型对象。在VFS中,多个文件系统可以被安装在同一个目录,例如/dev/sda和/dev/sdb先后被安装在/project目录下,则/dev/sdb会隐藏/dev/sda文件系统。同一个文件系统也可以安装在不同的目录下,此时只有一个超级块对象。因此,超级块对象代表了文件系统。目录项对象主要用于路径查找过程,例如,将文件系统/dev/sda安装在/project目录下,首先需要找到/project/目录的目录项对象,另外,目录项对象和目录不同,每个目录和文件都具有目录项对象,存在于内存。索引节点对象描述了文件或目录的元数据,包括文件的访问时间等,在创建索引节点的时候,将文件的相关操作指针赋给索引节点对象i_fpos。每个打开的文件对象对应一个文件描述符,常用于文件的open操作。每个文件系统都有对应的类型对象,如ext3,ext2,ext4和fuse文件系统,而这个结构体存储了相关的文件类型。安装点对象表示文件系统的挂载位置以及不同文件系统之间的关系,新挂载的文件系统都需要加入mount tree.

2. 对象

(1) superblock对象<linux/fs.h>

struct super_block{

Type                  Field            Description

 struct list_head      s_list                            /*Pointers for superblock list所有的superblock链表*/

 dev_t                 s_dev                                     /*Device identifier超级块对应的设备类型*/
 unsigned long         s_blocksize                  /*Block size in bytes每个块的字节数*/
unsigned long         s_old_blocksize          /*Block size in bytes as reported by the underlying block device drive*/
unsigned char  s_blocksize_bits              /*Block size in number of bits每个块占多少比特*/
unsigned char         s_dirt                          /*Modified (dirty) flag超级块为脏,需要和磁盘上的超级块同步*/
unsigned long long    s_maxbytes             /*Maximum size of the files文件的最大长度*/
struct file_system_type * s_type         /*Filesystem type所属的文件系统类型如ext3*/
struct super_operations *  s_op           /*Superblock methods超级块的相关操作,对象包括属性和操作*/
struct dquot_operations * dq_op          /*Disk quota handling methods*/
struct quotactl_ops * s_qcop                /*Disk quota administration methods*/
struct export_operations * s_export_op    /*Export operations used by network filesystems网络文件系统相关操作*/
unsigned long         s_flags                        /*Mount flags挂载标志*/
unsigned long         s_magic                        /*Filesystem magic number文件系统魔数*/
struct dentry *       s_root                      /*Dentry object of the filesystem's root directory文件系统的根目录项对象*/
struct rw_semaphore   s_umount          /*Semaphore used for unmounting umount时使用*/
struct semaphore      s_lock                  /*Superblock semaphore*/
int                   s_count                             /*Reference counter计数器,一个文件系统可能被安装到多个目录*/
int                  s_syncing                          /*Flag indicating that inodes of the superblock are being synchronized */
int                  s_need_sync_fs               /*Flag used when synchronizing the superblock's mounted filesystem*/                                
atomic_t             s_active                      /*Secondary reference counter*/                                     
void *               s_security                      /* Pointer to superblock security structure*/
struct xattr_handler ** s_xattr          /*Pointer to superblock extended attribute structure*/
struct list_head     s_inodes                 /*List of all inodes所有的inode节点*/
struct list_head     s_dirty                   /*List of modified inodes修改的inode节点链表*/
struct list_head     s_io                        /*List of inodes waiting to be written to disk等待写到磁盘上的inode节点列表*/
struct hlist_head    s_anon                   /*List of anonymous dentries for handling remote network filesystems*/
struct list_head     s_files                   /*List of file objects文件对象链表*/                   
struct block_device *s_bdev                /*Pointer to the block device driver descriptor块设备描述符*/
struct list_head     s_instances          /*Pointers for a list of superblock objects of a given filesystem type属于指定文件系统类型的链表指针*/
struct quota_info    s_dquot                 /*Descriptor for disk quota*/
int                  s_frozen                             /*Flag used when freezing the filesystem (forcing it to a consistent state)*/
wait_queue_head_t    s_wait_unfrozen  /*Wait queue where processes sleep until the filesystem is unforzen*/
char[]               s_id                                      /*Name of the block device containing the superblock*/
void *               s_fs_info                             /*Pointer to superblock information of a specific filesystem具体文件系统的超级块信息*/
struct semaphore     s_vfs_rename_sem   /*Semaphore used by VFS when renaming files across directories*/
u32                  s_time_gran                           /*Timestamp's granularity (in nanoseconds)*/
}

/*
 * NOTE: write_inode, delete_inode, clear_inode, put_inode can be called
 * without the big kernel lock held in all filesystems.
 */
struct super_operations {
 struct inode *(*alloc_inode)(struct super_block *sb);              /*为索引节点分配空间*/
  void (*destroy_inode)(struct inode *);                                         /*destory 索引节点对象*/
  void (*read_inode) (struct inode *);                                              /*利用i_ino索引节点号从磁盘上读取相关的数据来填充索引节点*/
   void (*dirty_inode) (struct inode *);                                           /*当索引节点被标识为dirty调用,即修改过*/
   int (*write_inode) (struct inode *, int flag);                             /*更新文件系统的索引节点,flag表示是否需要同步*/
   void (*put_inode) (struct inode *);                                               /*当索引节点被释放时*/     
   void (*drop_inode) (struct inode *);                                            /*当索引节点即将被destory*/
   void (*delete_inode) (struct inode *);                                        /*当索引节点被destory时,删除VFS的索引节点和磁盘上的文件数据和元数据*/
   void (*put_super) (struct super_block *);                                /*释放超级块对象,因为相应的文件系统已经umounted*/
   void (*write_super) (struct super_block *);                            /*更新文件系统的超级块*/
   int (*sync_fs)(struct super_block *sb, int wait);                  /*同步文件系统*/
   void (*write_super_lockfs) (struct super_block *);
   void (*unlockfs) (struct super_block *);
   int (*statfs) (struct dentry *, struct kstatfs *buf);                  /*返回文件的信息放在buf*/
   int (*remount_fs) (struct super_block *, int *, char *);              /'*重新安装文件系统*/
   void (*clear_inode) (struct inode *);                                              /*当索引节点正在destory*/
   void (*umount_begin) (struct vfsmount *, int);                            /*终止mount操作,因为相应的umount正在开始,仅用于网络文件系统*/
   int (*show_options)(struct seq_file *, struct vfsmount *);
   int (*show_stats)(struct seq_file *, struct vfsmount *);
#ifdef CONFIG_QUOTA
    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);
#endif

};

(2)inode节点对象,每个文件拥有一个inode节点对象,包含了文件的元数据信息,在<linux/fs.h>头文件

struct inode {
    struct hlist_node    i_hash;                     /*Pointers for the hash list指向hash链表指针*/
    struct list_head    i_list;                       /*Pointers for the list that describes the inode's current state索引节点状态链表,分为没有使用,正在使用和脏链表*/
    struct list_head    i_sb_list;                /*指向超级块链表的指针*/
    struct list_head    i_dentry;                 /*与这个inode节点相关联的目录项对象*/
    unsigned long        i_ino;                         /*索引节点号*/
    atomic_t        i_count;                            /*索引节点的使用计数*/
    unsigned int        i_nlink;                      /*硬连接数,多个硬连接共享同一个索引节点,但目录项对象不同(在不同的目录下)*/
    uid_t            i_uid;                                  /*owner identifier*/
    gid_t            i_gid;                                 /*group identifier*/
    dev_t            i_rdev;                              /*real device identifier实际设备标识*/
    unsigned long        i_version;             /*version number,automatically increased after each use*/
    loff_t            i_size;                           /*文件长度*/
#ifdef __NEED_I_SIZE_ORDERED
    seqcount_t        i_size_seqcount;
#endif
    struct timespec        i_atime;           /*文件的最后访问时间*/
    struct timespec        i_mtime;          /*文件最后修改时间*/
    struct timespec        i_ctime;          /*索引节点最后修改时间*/
    unsigned int        i_blkbits;            /*块大小*/
    blkcnt_t        i_blocks;                   /*文件的块数量*/
    unsigned short          i_bytes;        /*文件最后一个块的字节数*/
    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;
    struct inode_operations    *i_op;        /*索引节点相关操作*/
    const struct file_operations    *i_fop;    /* former ->i_op->default_file_ops 在索引节点创建时赋值,在打开文件时赋值给文件对象file*/
    struct super_block    *i_sb;                       /*超级块对象指针*/
    struct file_lock    *i_flock;
    struct address_space    *i_mapping;        /*地址空间对象指针*/
    struct address_space    i_data;               /*地址空间对象*/
#ifdef CONFIG_QUOTA
    struct dquot        *i_dquot[MAXQUOTAS];
#endif
    struct list_head    i_devices;
    union {
        struct pipe_inode_info    *i_pipe;
        struct block_device    *i_bdev;
        struct cdev        *i_cdev;
    };
    int            i_cindex;

    __u32            i_generation;

#ifdef CONFIG_DNOTIFY
    unsigned long        i_dnotify_mask; /* Directory notify events */
    struct dnotify_struct    *i_dnotify; /* for directory notifications */
#endif

#ifdef CONFIG_INOTIFY
    struct list_head    inotify_watches; /* watches on this inode */
    struct mutex        inotify_mutex;    /* protects the watches list */
#endif

    unsigned long        i_state;                 /*索引节点状态,是否为脏*/
    unsigned long        dirtied_when;    /* jiffies of first dirtying */

    unsigned int        i_flags;            /*文件系统安装标志*/

    atomic_t        i_writecount;
#ifdef CONFIG_SECURITY
    void            *i_security;
#endif
    void            *i_private; /* fs or device private pointer */
};


struct inode_operations {
    int (*create) (struct inode *,struct dentry *,int, struct nameidata *);                       /*创建一个和目录项对象相关联的磁盘索引节点*/
    struct dentry * (*lookup) (struct inode *,struct dentry *, struct nameidata *);       /*在目录项高速缓存查找和索引节点相关的目录项对象*/
    int (*link) (struct dentry *old_dentry,struct inode *,struct dentry *new_dentry);  /*创建一个硬连接,关联新的目录项对象*/
    int (*unlink) (struct inode *,struct dentry *);                                                                    /*移除硬连接*/
    int (*symlink) (struct inode *,struct dentry *,const char *);                                           /*创建符号链接*/  
    int (*mkdir) (struct inode *,struct dentry *,int);                                                              /*创建一个新的索引节点*/
    int (*rmdir) (struct inode *,struct dentry *);
    int (*mknod) (struct inode *,struct dentry *,int,dev_t);
    int (*rename) (struct inode *, struct dentry *,
     struct inode *, struct dentry *);
    int (*readlink) (struct dentry *, char __user *,int);
    void * (*follow_link) (struct dentry *, struct nameidata *);                                       /*转化符号链接*/
    void (*put_link) (struct dentry *, struct nameidata *, void *);
    void (*truncate) (struct inode *);
    int (*permission) (struct inode *, int, struct nameidata *);
    int (*setattr) (struct dentry *, struct iattr *);
    int (*getattr) (struct vfsmount *mnt, struct dentry *, struct kstat *);
    int (*setxattr) (struct dentry *, const char *,const void *,size_t,int);
    ssize_t (*getxattr) (struct dentry *, const char *, void *, size_t);
    ssize_t (*listxattr) (struct dentry *, char *, size_t);
    int (*removexattr) (struct dentry *, const char *);
    void (*truncate_range)(struct inode *, loff_t, loff_t);
};


(3)文件对象struct file,当进程打开文件时,在内存创建一个文件对象,在磁盘上没有相对应的映像,所以没有dirty标志,<linux/fs.h>

struct file {
    /*
     * fu_list becomes invalid after file_free is called and queued via
     * fu_rcuhead for RCU freeing
     */
    union {
        struct list_head    fu_list;                /*指向文件对象的链表指针*/
        struct rcu_head     fu_rcuhead;    
    } f_u;
    struct path        f_path;
#define f_dentry    f_path.dentry              /*和文件对象相关联的目录项对象*/
#define f_vfsmnt    f_path.mnt                   /*包含文件对象的安装点对象*/
    const struct file_operations    *f_op;       /*文件操作表指针*/
    atomic_t        f_count;                                 /*文件对象索引次数,一个文件可以被多个进程同时打开,每个进程的struct file不相同*/
    unsigned int         f_flags;                          /*打开文件时的指定标志*/
    mode_t            f_mode;                                 /*打开方式*/
    loff_t            f_pos;                                   /*当前文件指针的位置,非常重要*/
    struct fown_struct    f_owner;
    unsigned int        f_uid, f_gid;                /*用户ID,组ID*/
    struct file_ra_state    f_ra;                /*文件预读状态*/

    unsigned long        f_version;
#ifdef CONFIG_SECURITY
    void            *f_security;
#endif
    /* needed for tty driver, and maybe others */
    void            *private_data;

#ifdef CONFIG_EPOLL
    /* Used by fs/eventpoll.c to link all the hooks to this file */
    struct list_head    f_ep_links;
    spinlock_t        f_ep_lock;
#endif /* #ifdef CONFIG_EPOLL */
    struct address_space    *f_mapping;              /*地址空间指针*/
};


/*
 * NOTE:
 * read, write, poll, fsync, readv, writev, unlocked_ioctl and compat_ioctl
 * can be called without the big kernel lock held in all filesystems.
 */
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);
    int (*aio_fsync) (struct kiocb *, int datasync);
    int (*fasync) (int, struct file *, int);
    int (*lock) (struct file *, int, struct file_lock *);
    ssize_t (*sendfile) (struct file *, loff_t *, size_t, read_actor_t, void *);
    ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
    unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
    int (*check_flags)(int);
    int (*dir_notify)(struct file *filp, unsigned long arg);
    int (*flock) (struct file *, int, struct file_lock *);
    ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
    ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
};


(4)目录项对象,内核为路径的每一部分创建目录项对象,如/tmp/test,会为/,tmp和test分别创建目录项对象,用于定位文件或目录。为了加快查找,目录项对象通常存放在目录项高速缓存。目录项对象在磁盘上没有相对应的映像。当目录项对象不在dentry_cache中,就需要从磁盘读取相关信息,在内存创建一个目录项对象和相关联的索引节点对象。在<linux/dcache.h>

struct dentry {
    atomic_t d_count;                 /*使用计数*/
    unsigned int d_flags;        /* protected by d_lock 目录项缓存标志 */
    spinlock_t d_lock;        /* per dentry lock */
    struct inode *d_inode;        /* Where the name belongs to - NULL is
                     * negative 和文件名相关联的inode节点*/
    /*
     * The next three fields are touched by __d_lookup.  Place them here
     * so they all fit in a cache line.
     */
    struct hlist_node d_hash;    /* lookup hash list */
    struct dentry *d_parent;    /* parent directory 父目录项对象*/
    struct qstr d_name;           /*文件名结构体,包括文件名长度,文件名,hash值*/

    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;
    struct list_head d_subdirs;    /* our children */
    struct list_head d_alias;    /* inode alias list */
    unsigned long d_time;        /* used by d_revalidate */
    struct dentry_operations *d_op;  /*目录项操作方法*/
    struct super_block *d_sb;    /* The root of the dentry tree */
    void *d_fsdata;            /* fs-specific data */
#ifdef CONFIG_PROFILING
    struct dcookie_struct *d_cookie; /* cookie, if any */
#endif
    int d_mounted;                                /*文件系统的安装次数,同一目录可以安装不同的文件系统*/
    unsigned char d_iname[DNAME_INLINE_LEN_MIN];    /* small names */
};

struct dentry_operations {
    int (*d_revalidate)(struct dentry *, struct nameidata *);                     /*判定目录项是否还有效*/
    int (*d_hash) (struct dentry *, struct qstr *);                                         /*hash值*/
    int (*d_compare) (struct dentry *, struct qstr *, struct qstr *);         /*比较文件名*/
    int (*d_delete)(struct dentry *);                                                                /*d_count为0时调用*/
    void (*d_release)(struct dentry *);
    void (*d_iput)(struct dentry *, struct inode *);
};


(5)进程相关结构体:每个进程拥有自己的当前工作目录和根目录,通过fs_struct结构体维护<linux/fs_struct.h>

struct fs_struct {
    atomic_t count;  
    rwlock_t lock;
    int umask;                                                       /*设置文件权限*/
    struct dentry * root, * pwd, * altroot;      /*根目录项对象,当前目录项对象,altroot通常为空*/
    struct vfsmount * rootmnt, * pwdmnt, * altrootmnt;   /*根目录的安装的文件系统安装点对象,当前工作目录安装的文件系统安装点对象*/
};


(6)与进程关联的files_struct对象,包括指向struct file对象的数组指针fd,一个进程打开的最多文件数max_fds等,在<linux/file.h>

/*
 * Open file table structure
 */
struct files_struct {
  /*
   * read mostly part
   */
    atomic_t count;
    struct fdtable *fdt;   /*文件表指针*/
    struct fdtable fdtab;
  /*
   * written part on a separate cache line in SMP
   */
    spinlock_t file_lock ____cacheline_aligned_in_smp;
    int next_fd;                                                              /*下一个文件描述符*/
    struct embedded_fd_set close_on_exec_init;
    struct embedded_fd_set open_fds_init;
    struct file * fd_array[NR_OPEN_DEFAULT];      /*文件对象数组指针*/
};

struct fdtable {
    unsigned int max_fds;      /*当前最大的文件描述符数*/
    struct file ** fd;      /* current fd array */
    fd_set *close_on_exec;
    fd_set *open_fds;             /*已经打开的文件描述符集*/
    struct rcu_head rcu;
    struct fdtable *next;
};


(7)文件系统类型file_system_type ,每个文件系统类型对应多个具体的文件系统,在<linux/fs.h>

/*文件系统类型说明 */
struct file_system_type {
    const char *name;/*文件系统名称*/
    int fs_flags;/*文件系统类型标志*/
    int (*get_sb) (struct file_system_type *, int,
    const char *, void *, struct vfsmount *);/*读超级块*/
    void (*kill_sb) (struct super_block *);/*移除超级块*/
    struct module *owner;/*指向实现文件系统的module*/
    struct file_system_type * next;/*指向文件系统类型表的下一个*/
    struct list_head fs_supers;/*给定类型文件系统的超级块表头*/
    struct lock_class_key s_lock_key;
    struct lock_class_key s_umount_key;
};
其中,

(1) file_systems是所有的文件系统类型链表头,如ext2,ext3,ext4. 而next指针指向下一个文件系统类型。
(2)fs_supers是同类文件系统的链表头,下一个由超级块对象的s_instances指针指向.

(3)get_sb和kill_sb分配一个超级块对象和销毁一个超级块对象。


(8)mount point 对象,为了在内存中保存挂载点,挂载标志,以及和其它文件系统之间的关系,如父子关系。必须维护一个vfsmount对象,在<linux/mount.h>

struct vfsmount {
    struct list_head mnt_hash; /*指向hash表指针*/
    struct vfsmount *mnt_parent;    /* fs we are mounted on 父安装点*/
    struct dentry *mnt_mountpoint;    /* dentry of mountpoint 安装点目录项对象*/
    struct dentry *mnt_root;    /* root of the mounted tree 安装点根的目录项对象*/
    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 子安装点的下一个对象*/
    atomic_t mnt_count;/*使用次数*/
    int mnt_flags;/*安装标志*/
    int mnt_expiry_mark;        /* true if marked for expiry */
    char *mnt_devname;        /* Name of device e.g. /dev/dsk/hda1 设备名*/
    struct list_head mnt_list;    /*指向命名空间的下一个安装点对象,其中链表头存在命名空间的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_pinned;
};

处理文件系统挂载的相关函数:

alloc_vfsmnt(name)

Allocates and initializes a mounted filesystem descriptor


free_vfsmnt(mnt)

Frees a mounted filesystem descriptor pointed by mnt


lookup_mnt(mnt,dentry)

Looks up a descriptor in the hash table and returns its address


3. 总结

super_block-关联每个具体的文件系统 

inode-对应文件元数据信息

dentry-目录项对象,内存表示形式,文件和目录都拥有目录项对象

file-打开文件在内存的表示形式

fs_struct-进程相关的根目录,工作目录,根安装点和当前目录安装点

file_system_type-在启动时注册文件系统类型,如EXT3,或者是加载模块时注册

vfsmount-维护已挂载的文件系统之间的关系,如父子关系




猜你喜欢

转载自blog.csdn.net/chenjin_zhong/article/details/8444789