Chinaunix首页 | 论坛 | 博客
  • 博客访问: 733960
  • 博文数量: 79
  • 博客积分: 2671
  • 博客等级: 少校
  • 技术积分: 1247
  • 用 户 组: 普通用户
  • 注册时间: 2010-04-02 15:26
个人简介

宅男

文章分类

全部博文(79)

文章存档

2017年(11)

2016年(12)

2015年(6)

2012年(10)

2011年(33)

2010年(7)

分类: LINUX

2010-04-11 00:28:56

最近在看《linux内核源代码情景分析》,再集合2.6.16的内核在理解这些内容
文件系统,这个词有几种不同的含义,一般是指操作系统用来管理文件系统以及对文件进行操作的机制及其实现。
虚拟文件系统对用户程序隐去了各种不同文件系统的实现细节,为用户程序提供了一个统一的、抽象的、虚拟的文件系统的界面。
不同的文件系统通过不同的程序来实现各种功能,但是在VFS之间的界面则有明确的定义,这个界面就是file_operations数据结构

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 (*aio_read) (struct kiocb *, char __user *, size_t, loff_t);
    ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
    ........
};


每个文件系统都有自己的file_operations数据结构
每个进程通过“打开文件”open()来与具体的文件建立连接,这种连接以file数据结构为代表,结构中有个的file_operations结构 指针f_op,指向具体的file_operations数据结构
进程与文件的连接,进程已打开的文件,是进程的一部分财产,归进程所有


struct task_struct {
.....
/* filesystem information */
    struct fs_struct *fs;
/* open file information */
    struct files_struct *files;
......
};

在里面有两个指针fs,files。其中一个指向files_struct数据结构,下面我们在看一下:

struct files_struct {
.....
struct file * fd_array[NR_OPEN_DEFAULT];
.....
};

这个指针数组代表着一个进程打开的文件

struct file {
......
    struct dentry        *f_dentry; //目录项
    struct vfsmount *f_vfsmnt;
    struct file_operations    *f_op;//操作结构体
......
};

下面具体的来看一下代表目录项的dentry结构体:


struct dentry {
    atomic_t d_count;//共享计数
    unsigned int d_flags;        
    spinlock_t d_lock;        
    struct inode *d_inode;//索引节点,下面再具体解释
    struct hlist_node d_hash;//hash链表
    struct dentry *d_parent;//上一级目录项,类似于上一级文件夹
    struct qstr d_name;
    struct list_head d_lru;    //指向一个dentry_unused队列,里面都是共享计数为0,暂时没有释放的dentry数据结构
    union {
        struct list_head d_child;    /* child of parent list *///该目录项的子目录
         struct rcu_head d_rcu;
    } d_u;
    struct list_head d_subdirs;//该目录项的子目录项
    struct list_head d_alias;    //存在一个多个目录项指向同一个索引节点的情况,这些目录项被链表串起,这个指向链表头
    unsigned long d_time;        
    struct dentry_operations *d_op;//dentry操作结构体
    struct super_block *d_sb;    //指向该目录项所属文件系统的super_block结构体
    void *d_fsdata;            
#ifdef CONFIG_PROFILING
    struct dcookie_struct *d_cookie; /* cookie, if any */
#endif
    int d_mounted;
    unsigned char d_iname[DNAME_INLINE_LEN_MIN];//名字

};


上面我们提到了一种情况:多和目录项指向同一个inode的情况,这儿再解释一下索引节点;

struct inode {
    struct hlist_node    i_hash;//索引节点的hash表
    struct list_head    i_list;//指向一个链表dirty_inode,该链表中的节点都是在内存中修改了,没来得及写入设备的节点

    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 mutex        i_mutex;
    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;
....
};

一个file文件中有一个指向dentry目录项的指针,dentry数据结构中有一个指向索引节点inode的指针
,但是干嘛不将dentry和inode合二为一呢?
就像我们上面所提到的一样,可能存在多个目录项指向同一个索引节点inode的情况
目录项dentry和inode索引节点从不同的角度描述了文件各个方面的属性,dentry描述的是逻辑的文件,而inode描述的是文件的物理属性,联想到我们在WINDOWS中存在的“快捷方式”,这样的理解就容易的多了
所以可以存在多个逻辑文件(目录项)指向同一个物理文件(索引节点)的情况。
阅读(4955) | 评论(0) | 转发(2) |
0

上一篇:没有了

下一篇:编译模块

给主人留下些什么吧!~~