Chinaunix首页 | 论坛 | 博客
  • 博客访问: 420122
  • 博文数量: 122
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 688
  • 用 户 组: 普通用户
  • 注册时间: 2013-09-04 12:30
文章分类

全部博文(122)

文章存档

2017年(5)

2016年(4)

2015年(56)

2014年(41)

2013年(16)

我的朋友

分类: 服务器与存储

2014-12-13 13:55:28

转自:(有改动)

 Linux下的文件系统中宏观上主要分为三层:一是上层的文件系统的系统调用;二是虚拟文件系统VFS(Virtual File System)层,三是挂载到VFS中的各种实际文件系统。

         VFS是一种软件机制,只存在于内存中,每次系统初始化期间Linux都会先在内存中构造一棵VFS的目录树(也就是源码中的namespace)。VFS主要的作用是对上层应用屏蔽底层不同的调用方法,提供一套统一的调用接口,二是便于对不同的文件系统进行组织管理。因此,VFS其实就是文件系统组织管理中的一个抽象层。

super block

相关的数据结构为:
struct super_block {
        struct list_head        s_list;               /* 系统将所有文件系统的超级块组成链表*/
        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;        
        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;        /* 所有的inode节点链表*/
        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 链表中:

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

 
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(这个暂不介绍).
__________________________________
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;               //目录项名称,与inode对应起来
        struct list_head d_lru;                /* LRU list */
        struct list_head d_child;        /* 链入父目录的子目录中  */
        struct list_head d_subdirs;        /* 子目录链表 */
        struct list_head d_alias;        /*  链入inode节点的i_dentry链表,因为一个inode可能对应多个dentry(硬链接) */
        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(这个暂不介绍).
     综上所述,在内存中, 每个文件都有一个dentry(目录项)和inode(索引节点)结构,dentry记录着文件名,上级目录等信息,正是它形成了我们所看到的树状结构;而有关该文件的组织和管理的信息主要存放inode里面,它记录着文件在存储介质上的位置与分布。同时dentry->d_inode指向相应的inode结构。dentry与inode是多对一的关系,因为有可能一个文件有好几个文件名(inode(可理解为ext2 inode)对应于物理磁盘上的具体对象,dentry是一个内存实体,其中的d_inode成员指向对应的inode。也就是说,一个inode可以在运行的时候链接多个dentry,而d_count记录了这个链接的数量。)

硬链接:其实就是同一个文件具有多个别名,具有相同inode,而dentry不同。

              1. 文件具有相同的inode和data block;

              2. 只能对已存在的文件进行创建;

              3. 不能交叉文件系统进行硬链接的创建

              4. 不能对目录进行创建,只能对文件创建硬链接

              5. 删除一个硬链接并不影响其他具有相同inode号的文件;

软链接:软链接具有自己的inode,即具有自己的文件,只是这个文件中存放的内容是另一个文件的路径名。因此软链接具有自己的inode号以及用户数据块。

              1. 软链接有自己的文件属性及权限等;

              2. 软链接可以对不存在的文件或目录创建;

              3. 软链接可以交叉文件系统;

              4. 软链接可以对文件或目录创建;

              5. 创建软链接时,链接计数i_nlink不会增加;


__________________________________
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对象来表示.

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

注意:VFS文件系统中的inode和dentry与实际文件系统的inode和dentry有一定的关系,但不能等同。真实磁盘文件的inode和dentry是存在于物理外存上的,但VFS中的inode和dentry是存在于内存中的,系统读取外存中的inode和dentry信息进行一定加工后,生成内存中的inode和dentry。虚拟的文件系统也具有inode和dentry结构,只是这是系统根据相应的规则生成的,不存在于实际外存中。


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