从文件 I/O 看 Linux 的虚拟文件系统

http://tech.ddvip.com   2008年04月15日    社区交流

内容摘要:本文在简要介绍 VFS 的相关数据结构后,以文件 I/O 为切入点深入 Linux 内核源代码,追踪了 sys_open 和 sys_read 两个系统调用的代码结构,并在追踪的过程中理清了跨文件系统的文件操作的基本原理和“一切皆是文件”的口号得以实现的根本。

  2.2.5.2 和进程相关

  清单6. 打开的文件集

struct files_struct {//打开的文件集
     atomic_t count;       /*结构的使用计数*/
    ……
     int max_fds;         /*文件对象数的上限*/
    int max_fdset;        /*文件描述符的上限*/
    int next_fd;         /*下一个文件描述符*/
    struct file ** fd;      /*全部文件对象数组*/
    ……
};
struct fs_struct {//建立进程与文件系统的关系
     atomic_t count;       /*结构的使用计数*/
    rwlock_t lock;        /*保护该结构体的锁*/
    int umask;         /*默认的文件访问权限*/
    struct dentry * root;    /*根目录的目录项对象*/
    struct dentry * pwd;     /*当前工作目录的目录项对象*/
    struct dentry * altroot;  /*可供选择的根目录的目录项对象*/
    struct vfsmount * rootmnt;  /*根目录的安装点对象*/
    struct vfsmount * pwdmnt;  /*pwd的安装点对象*/
    struct vfsmount * altrootmnt;/*可供选择的根目录的安装点对象*/
};

  2.2.5.3 和路径查找相关

  清单7. 辅助查找

struct nameidata {
    struct dentry *dentry;   /*目录项对象的地址*/
    struct vfsmount *mnt;   /*安装点的数据*/
    struct qstr last;     /*路径中的最后一个component*/
    unsigned int flags;    /*查找标识*/
    int last_type;       /*路径中的最后一个component的类型*/
    unsigned depth;      /*当前symbolic link的嵌套深度,不能大于6*/
    char  *saved_names[MAX_NESTED_LINKS + 1];/
                  /*和嵌套symbolic link 相关的pathname*/
    union {
      struct open_intent open; /*说明文件该如何访问*/
    } intent;  /*专用数据*/
};

来源:ibm    作者:吴美清    责编:豆豆技术应用

正在加载评论...