struct file struct inode

(1)每个进程在进程表中都有一个记录项,记录项包含一张打开的文件描述符表,可将视为矢量,每个描述符占用一项,与每个文件描述符相关联的是:

a)文件描述符标志(close_on_exec).

b))指向一个文件表项的指针。

(2)内核为所用打开文件维持一张文件表,每个文件表项包含:

a)文件状态标志(读写等等)

b)当前文件偏移量。

c)指向该文件V节点表项的指针。

(3)每个打开文件(或设备)都有一个V-node结构。v节点包含了文件类型和对此文件进行各种操作的函数指针。对于大多数文件V节点还包含文件的i节点(索引节点)。这些信息时在打开文件时从磁盘上读入内存的。对于Linux没有V节点。


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;  //该操作 是定义文件关联的操作的。内核在执行open时对这个指针赋值。 
atomic_long_t        f_count;
 unsigned int         f_flags;  //该成员是文件标志。 
mode_t            f_mode;
loff_t            f_pos;
struct fown_struct    f_owner;
unsigned int        f_uid, f_gid;
struct file_ra_state    f_ra;

u64            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;
#ifdef CONFIG_DEBUG_WRITECOUNT
unsigned long f_mnt_write_state;
#endif
};

1、file结构体

文件结构体代表一个打开的文件(设备对应于设备文件),系统在每次打开一个文件的时候都会创建一个file结构体表示此时打开的文件,又内核在打开时创建,当文件的所有实例被关闭的时候(关闭该文件同时引用该文件)该file结构体将被内核释放。


2、inode结构体

内核中用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;
unsigned int        i_nlink;
uid_t            i_uid;
gid_t            i_gid;
 dev_t            i_rdev;   //该成员表示设备文件的inode结构,它包含了真正的设备编号。
u64            i_version;
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;
const struct inode_operations    *i_op;
const 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
struct list_head    i_devices;
union {
struct pipe_inode_info    *i_pipe;
struct block_device    *i_bdev;
 struct cdev        *i_cdev; //该成员表示字符设备的内核的 内部结构。当inode指向一个字符设备文件时,该成员包含了指向struct cdev结构的指针,其中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 */
};

inode 译成中文就是索引节点。每个存储设备或存储设备的分区(存储设备是硬盘、软盘、U盘 ... ... )被格式化为文件系统后,应该有两部份,一部份是inode,另一部份是Block,Block是用来存储数据用的。而inode呢,就是用来存储这些数据的信息,这些信息包括文件大小、属主、归属的用户组、读写权限等。inode为每个文件进行信息索引,所以就有了inode的数值。操作系统根据指令,能通过inode值最快的找到相对应的文件。 
      做个比喻,比如一本书,存储设备或分区就相当于这本书,Block相当于书中的每一页,inode 就相当于这本书前面的目录,一本书有很多的内容,如果想查找某部份的内容,我们可以先查目录,通过目录能最快的找到我们想要看的内容。
      当我们用ls 查看某个目录或文件时,如果加上-i 参数,就可以看到inode节点了;比如ls -li lsfile.sh ,最前面的数值就是inode信息


3、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]; 存放短文件名

};

当访问文件时,首先open("",??), open函数通过系统调用sys_open进入内核空间, sysopen又调用do_sys_open,

do_sys_open 通过get_unused_fd(),在当前进程空间内的struct file结构数组中,
找一个空的struct file{}结构,并返回一个数组的下标号,

之后do_sys_open又调用do_filp_open,do_filp_open调用nameidata_to_filp,
nameidata_to_filp调用__dentry_open,

在__dentry_open,通过关键语句,f->f_op = fops_get(inode->i_fop);
得到了具有一个指向struct file_operations结构的指针的struct file结构指针,之后通过语句

    if (!open && f->f_op)
        open = f->f_op->open;
    if (open) {
        error = open(inode, f);
        if (error)
            goto cleanup_all;
    }
调用我们编写的struct file_operations中的xxxopen()

其他操作类似,
read调用顺序如下: 
read()->sys_read()->vfs_read()->{file->f_op->read}

用户空间的read、write---linux系统调用---间接调用设备驱动程序中file_operations结构中的函数整个调用过程是这样一个顺序的

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值