Chinaunix首页 | 论坛 | 博客
  • 博客访问: 9330205
  • 博文数量: 1669
  • 博客积分: 16831
  • 博客等级: 上将
  • 技术积分: 12594
  • 用 户 组: 普通用户
  • 注册时间: 2011-02-25 07:23
个人简介

柔中带刚,刚中带柔,淫荡中富含柔和,刚猛中荡漾风骚,无坚不摧,无孔不入!

文章分类

全部博文(1669)

文章存档

2023年(4)

2022年(1)

2021年(10)

2020年(24)

2019年(4)

2018年(19)

2017年(66)

2016年(60)

2015年(49)

2014年(201)

2013年(221)

2012年(638)

2011年(372)

分类: LINUX

2012-07-18 09:10:58

vfs, superblock, inode, dentry, file结构体图解  

2011-11-02 16:58:56|  分类: rh131笔记 |  标签: |字号 

super block


相关的数据结构为:

struct super_block {
        struct list_head        s_list;                /* Keep this first */
        dev_t                        s_dev;                /* search index; _not_ kdev_t */
        unsigned long                s_blocksize;
        unsigned long                s_old_blocksize;
        unsigned char                s_blocksize_bits;
        unsigned char                s_dirt;
        unsigned long long        s_maxbytes;        /* Max file size */
        struct file_system_type        *s_type;
        struct super_operations        *s_op;
        struct dquot_operations        *dq_op;
        struct quotactl_ops        *s_qcop;
        struct export_operations *s_export_op;
        unsigned long                s_flags;
        unsigned long                s_magic;
        struct dentry                *s_root;
        struct rw_semaphore        s_umount;
        struct semaphore        s_lock;
        int                        s_count;
        int                        s_syncing;
        int                        s_need_sync_fs;
        atomic_t                s_active;
        void                    *s_security;
        struct xattr_handler        **s_xattr;

        struct list_head        s_inodes;        /* all inodes */
        struct list_head        s_dirty;        /* dirty inodes */
        struct list_head        s_io;                /* parked for writeback */
        struct hlist_head        s_anon;                /* anonymous dentries for (nfs) exporting */
        struct list_head        s_files;

        struct block_device        *s_bdev;
        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 */

        /*
         * The next field is for VFS *only*. No filesystems have any business
         * even looking at it. You had been warned.
         */
        struct semaphore s_vfs_rename_sem;        /* Kludge */

        /* Granuality of c/m/atime in ns.
           Cannot be worse than a second */
        u32                   s_time_gran;
};




super_block存在于两个链表中,一个是系统所有super_block的链表, 一个是对于特定的文件系统的super_block链表.

                                                                             
所有的super_block都存在于 super-blocks 链表中:


vfs, superblock, inode, dentry, file结构体图解 - zhuzhu - 五事九思  (大连Linux主机维护) 



对于特定的文件系统, 该文件系统的所有的super block 都存在于file_sytem_type中的fs_supers链表中.
而所有的文件系统,都存在于file_systems链表中.这是通过调用register_filesystem接口来注册文件系统的.
int register_filesystem(struct file_system_type * fs)  


vfs, superblock, inode, dentry, file结构体图解 - zhuzhu - 五事九思  (大连Linux主机维护) 



2. inode

相关的数据结构为:


struct inode {
        struct hlist_node        i_hash;
        struct list_head        i_list;
        struct list_head        i_sb_list;
        struct list_head        i_dentry;
        unsigned long                i_ino;
        atomic_t                i_count;
        umode_t                        i_mode;
        unsigned int                i_nlink;
        uid_t                        i_uid;
        gid_t                        i_gid;
        dev_t                        i_rdev;
        loff_t                        i_size;
        struct timespec                i_atime;
        struct timespec                i_mtime;
        struct timespec                i_ctime;
        unsigned int                i_blkbits;
        unsigned long                i_blksize;
        unsigned long                i_version;
        unsigned long                i_blocks;
        unsigned short          i_bytes;
        spinlock_t                i_lock;        /* i_blocks, i_bytes, maybe i_size */
        struct semaphore        i_sem;
        struct rw_semaphore        i_alloc_sem;
        struct inode_operations        *i_op;
        struct file_operations        *i_fop;        /* former ->i_op->default_file_ops */
        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
        /* These three should probably be a union */
        struct list_head        i_devices;
        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

        unsigned long                i_state;
        unsigned long                dirtied_when;        /* jiffies of first dirtying */

        unsigned int                i_flags;

        atomic_t                i_writecount;
        void                        *i_security;
        union {
                void                *generic_ip;
        } u;
#ifdef __NEED_I_SIZE_ORDERED
        seqcount_t                i_size_seqcount;
#endif
};



inode存在于两个双向链表中:
一个是inode所在文件系统的super block的 s_inodes 链表中

一个是根据inode的使用状态存在于以下三个链表中的某个链表中:
1. 未用的: inode_unused 链表
2. 正在使用的: inode_in_use 链表
3. 脏的: super block中的s_dirty 链表


另外,还有一个重要的链表: inode_hashtable(这个暂不介绍).


vfs, superblock, inode, dentry, file结构体图解 - zhuzhu - 五事九思  (大连Linux主机维护)


__________________________________



3. dentry


相关的数据结构为:


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 */
        /*
         * The next three fields are touched by __d_lookup.  Place them here
         * so they all fit in a 16-byte range, with 16-byte alignment.
         */
        struct dentry *d_parent;        /* parent directory */
        struct qstr d_name;

        struct list_head d_lru;                /* LRU list */
        struct list_head d_child;        /* child of parent list */
        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 */
        struct rcu_head d_rcu;
        struct dcookie_struct *d_cookie; /* cookie, if any */
        struct hlist_node d_hash;        /* lookup hash list */        
        int d_mounted;
        unsigned char d_iname[DNAME_INLINE_LEN_MIN];        /* small names */
};


dentry对象存在于三个双向链表中:
所有未用的目录项: dentry_unused 链表
正在使用的目录项: 对应inode的 i_dentry 链表
表示父子目录结构的链表

另外,还有一个重要的链表: inode_hashtable(这个暂不介绍).

vfs, superblock, inode, dentry, file结构体图解 - zhuzhu - 五事九思  (大连Linux主机维护)


__________________________________
http://blog.sina.com.cn/bytex4. 进程相关的信息
和进程相关的信息, 涉及到四个重要的数据结构:
file, fs_struct, files_struct 和 namespace


相关的数据结构为:


struct file {
        struct list_head        f_list;
        struct dentry                *f_dentry;
        struct vfsmount         *f_vfsmnt;
        struct file_operations        *f_op;
        atomic_t                f_count;
        unsigned int                 f_flags;
        mode_t                        f_mode;
        int                        f_error;
        loff_t                        f_pos;
        struct fown_struct        f_owner;
        unsigned int                f_uid, f_gid;
        struct file_ra_state        f_ra;

        size_t                        f_maxcount;
        unsigned long                f_version;
        void                        *f_security;

        /* 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;
};

-------------------------------------------------------------------------------

struct fs_struct {
        atomic_t count;
        rwlock_t lock;
        int umask;
        struct dentry * root, * pwd, * altroot;
        struct vfsmount * rootmnt, * pwdmnt, * altrootmnt;
};

-------------------------------------------------------------------------------

struct files_struct {
        atomic_t count;
        spinlock_t file_lock;     /* Protects all the below members.  Nests inside tsk->alloc_lock */
        int max_fds;
        int max_fdset;
        int next_fd;
        struct file ** fd;      /* current fd array */
        fd_set *close_on_exec;
        fd_set *open_fds;
        fd_set close_on_exec_init;
        fd_set open_fds_init;
        struct file * fd_array[NR_OPEN_DEFAULT];
};

-------------------------------------------------------------------------------

struct namespace {
        atomic_t                count;
        struct vfsmount *        root;
        struct list_head        list;
        struct rw_semaphore        sem;
};


每个进程都有自己的namespace.

fs_struct用于表示进程与文件系统之间的结构关系,比如当前的工作目录,进程的根目录等等.

files_struct 用于表示当前进程打开的文件.

而对于每一个打开的文件,由file对象来表示.

vfs, superblock, inode, dentry, file结构体图解 - zhuzhu - 五事九思  (大连Linux主机维护) 


Linux中,常常用文件描述符(file descriptor)来表示一个打开的文件,这个描述符的值往往是一个大于或等于0的整数.
而这个整数,其实就是在files_struct中file数组fd的下标.
对于所有打开的文件, 这些文件描述符会存储在open_fds的位图中.







inux常见文件结构体  

2010-03-17 10:28:10|  分类: 默认分类|字号 

http://blog.csdn.net/sealyao/archive/2009/10/02/4626875.aspx

一、 常见文件相关结构体

以2.6.22为例,其他的2.6.X版本可能调整,但是变化不大。

1.1 struct file

struct file结构体定义在include/linux/fs.h中定义。文件结构体代表一个打开的文件,系统中的每个打开的文件在内核空间都有一个关联的 struct file。它由内核在打开文件时创建,并传递给在文件上进行操作的任何函数。在文件的所有实例都关闭后,内核释放这个数据结构。在内核创建和驱动源码 中,struct file的指针通常被命名为file或filp。如下所示:

struct file {

        union {

             struct list_head fu_list; 文件对象链表指针linux/include/linux/list.h

             struct rcu_head fu_rcuhead; RCU(Read-Copy Update)是Linux 2.6内核中新的锁机制

        } f_u;

        struct path f_path;  包含dentry和mnt两个成员,用于确定文件路径

        #define f_dentry  f_path.dentry  f_path的成员之一,当前文件的dentry结构

        #define f_vfsmnt  f_path.mnt  表示当前文件所在文件系统的挂载根目录

        const struct file_operations *f_op; 与该文件相关联的操作函数

        atomic_t  f_count; 文件的引用计数(有多少进程打开该文件)

        unsigned int  f_flags;  对应于open时指定的flag

        mode_t  f_mode; 读写模式:open的mod_t mode参数

        off_t  f_pos; 该文件在当前进程中的文件偏移量

        struct fown_struct f_owner; 该结构的作用是通过信号进行I/O时间通知的数据。

        unsigned int  f_uid, f_gid; 文件所有者id,所有者组id

        struct file_ra_state f_ra;  在linux/include/linux/fs.h中定义,文件预读相关

        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;

};

1.2 struct dentry

dentry的中文名称是目录项,是Linux文件系统中某个索引节点(inode)的链接。这个索引节点可以是文件,也可以是目录。 inode(可理解为ext2 inode)对应于物理磁盘上的具体对象,dentry是一个内存实体,其中的d_inode成员指向对应的inode。也就是说,一个inode可以在 运行的时候链接多个dentry,而d_count记录了这个链接的数量。

struct dentry { 
        atomic_t d_count;  目录项对象使用计数器,可以有未使用态,使用态和负状态                                            
        unsigned int d_flags;    目录项标志 
        struct inode * d_inode;   与文件名关联的索引节点 
        struct dentry * d_parent;    父目录的目录项对象 
        struct list_head d_hash;    散列表表项的指针 
        struct list_head d_lru;   未使用链表的指针 
        struct list_head d_child;    父目录中目录项对象的链表的指针 
        struct list_head d_subdirs;   对目录而言,表示子目录目录项对象的链表 
        struct list_head d_alias;    相关索引节点(别名)的链表 
        int d_mounted;     对于安装点而言,表示被安装文件系统根项 
        struct qstr d_name;    文件名 
        unsigned long d_time;     /* used by d_revalidate */ 
        struct dentry_operations *d_op;      目录项方法 
        struct super_block * d_sb;      文件的超级块对象 
        vunsigned long d_vfs_flags; 
        void * d_fsdata;      与文件系统相关的数据 
        unsigned char d_iname [DNAME_INLINE_LEN];      存放短文件名

};

1.3 struct files_struct

对于每个进程,包含一个files_struct结构,用来记录文件描述符的使用情况,定义在include/linux/file.h中

struct files_struct

{

        atomic_t count;     使用该表的进程数

        struct fdtable *fdt;

        struct fdtable fdtab;

        spinlock_t file_lock ____cacheline_aligned_in_smp;

        int next_fd;         数值最小的最近关闭文件的文件描述符,下一个可用的文件描述符

        struct embedded_fd_set close_on_exec_init;    执行exec时需要关闭的文件描述符初值集合

        struct embedded_fd_set open_fds_init;     文件描述符的屏蔽字初值集合

        struct file * fd_array[NR_OPEN_DEFAULT];      默认打开的fd队列

};

struct fdtable {

        unsigned int max_fds;

        struct file ** fd;    指向打开的文件描述符列表的指针,开始的时候指向fd_array,

                                   当超过max_fds时,重新分配地址

        fd_set *close_on_exec; 执行exec需要关闭的文件描述符位图(fork,exec即不被子进程继承的文件

                                            描述符)

        fd_set *open_fds; 打开的文件描述符位图

        struct rcu_head rcu;

        struct fdtable *next;

};

1.4 struct  fs_struct

struct fs_struct {

atomic_t count; 计数器

rwlock_t lock; 读写锁

int umask;

struct dentry * root, * pwd, * altroot;根目录("/"),当前目录以及替换根目录

struct vfsmount * rootmnt, * pwdmnt, * altrootmnt;

};

1.5 struct inode

索引节点对象由inode结构体表示,定义文件在linux/fs.h中。

struct inode { 
        struct hlist_node       i_hash; 哈希表 
        struct list_head        i_list;   索引节点链表 
        struct list_head        i_dentry; 目录项链表 
        unsigned long           i_ino;  节点号 
        atomic_t                i_count; 引用记数 
        umode_t                 i_mode; 访问权限控制 
        unsigned int            i_nlink; 硬链接数 
        uid_t                   i_uid;  使用者id 
        gid_t                   i_gid;  使用者id组 
        kdev_t                  i_rdev; 实设备标识符 
        loff_t                  i_size;  以字节为单位的文件大小 
        struct timespec         i_atime; 最后访问时间 
        struct timespec         i_mtime; 最后修改(modify)时间 
        struct timespec         i_ctime; 最后改变(change)时间 
        unsigned int            i_blkbits; 以位为单位的块大小 
        unsigned long           i_blksize; 以字节为单位的块大小 
        unsigned long           i_version; 版本号 
        unsigned long           i_blocks; 文件的块数 
        unsigned short          i_bytes; 使用的字节数 
        spinlock_t              i_lock; 自旋锁 
        struct rw_semaphore     i_alloc_sem; 索引节点信号量 
        struct inode_operations *i_op; 索引节点操作表 
        struct file_operations  *i_fop; 默认的索引节点操作 
        struct super_block      *i_sb; 相关的超级块 
        struct file_lock        *i_flock; 文件锁链表 
        struct address_space    *i_mapping; 相关的地址映射 
        struct address_space    i_data; 设备地址映射 
        struct dquot            *i_dquot[MAXQUOTAS];节点的磁盘限额 
        struct list_head        i_devices; 块设备链表 
        struct pipe_inode_info  *i_pipe; 管道信息 
        struct block_device     *i_bdev; 块设备驱动 
        unsigned long           i_dnotify_mask;目录通知掩码 
        struct dnotify_struct   *i_dnotify; 目录通知 
        unsigned long           i_state; 状态标志 
        unsigned long           dirtied_when;首次修改时间 
        unsigned int            i_flags; 文件系统标志 
        unsigned char           i_sock; 套接字 
        atomic_t                i_writecount; 写者记数 
        void                    *i_security; 安全模块 
        __u32                   i_generation; 索引节点版本号 
        union { 
                void            *generic_ip;文件特殊信息 
        } u; 
};

       我们在进程中打开一个文件F,实际上就是要在内存中建立F的dentry,和inode结构,并让它们与进程结构联系来,把VFS中定义的接口给接起来。我们来看一看这个经典的图:

下图为多个进程打开同一文件的情况:

wps_clip_image-5416

阅读(1756) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~