file结构体

参考:Linux字符设备中的两个重要结构体(file、inode)

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

重要成员:

1. const struct file_operations *f_op;

和文件关联的操作

2. void *private_data;

私有指针,大多用于指向设备驱动自定义用于描述设备的结构体。

 1 struct file {
 2     /*
 3      * fu_list becomes invalid after file_free is called and queued via
 4      * fu_rcuhead for RCU freeing
 5      */
 6     union {
 7         struct list_head    fu_list;
 8         struct rcu_head     fu_rcuhead;
 9     } f_u;
10     struct path        f_path;
11 #define f_dentry    f_path.dentry
12 #define f_vfsmnt    f_path.mnt
13     const struct file_operations    *f_op;
14     spinlock_t        f_lock;  /* f_ep_links, f_flags, no IRQ */
15 #ifdef CONFIG_SMP
16     int            f_sb_list_cpu;
17 #endif
18     atomic_long_t        f_count;
19     unsigned int         f_flags;
20     fmode_t            f_mode;
21     loff_t            f_pos;
22     struct fown_struct    f_owner;
23     const struct cred    *f_cred;
24     struct file_ra_state    f_ra;
25 
26     u64            f_version;
27 #ifdef CONFIG_SECURITY
28     void            *f_security;
29 #endif
30     /* needed for tty driver, and maybe others */
31     void            *private_data;
32 
33 #ifdef CONFIG_EPOLL
34     /* Used by fs/eventpoll.c to link all the hooks to this file */
35     struct list_head    f_ep_links;
36 #endif /* #ifdef CONFIG_EPOLL */
37     struct address_space    *f_mapping;
38 #ifdef CONFIG_DEBUG_WRITECOUNT
39     unsigned long f_mnt_write_state;
40 #endif
41 };
原文地址:https://www.cnblogs.com/yangjiguang/p/6030423.html