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

struct file 的最重要成员在这展示. 1.mode_t f_mode;       文件模式确定文件是可读的或者是可写的(或者都是), 通过位 FMODE_READ 和FMODE_WRITE. 你可能想在你的 open 或者 ioctl 函数中检查这个成员的读写许可, 但是不需要检查读写许可, 因为内核在调用你的方法之前检查. 当文件还没有为那种存取而打开时读或写的企图被拒绝, 驱动甚至不知道这个情况. 2.loff_t f_pos;       当前读写位置. loff_t 在所有平台都是 64 位( 在 gcc 术语里是 long long ). 驱动可以读这个值,如果它需要知道文件中的当前位置, 但是正常地不应该改变它; 读和写应当使用它们作为最后参数而收到的指针来更新一个位置, 代替直接作用于 filp->f_pos. 这个规则的一个例外是在 llseek 方法中, 它的目的就是改变文件位置. 3.unsigned int f_flags;       这些是文件标志, 例如 O_RDONLY, O_NONBLOCK, 和 O_SYNC. 驱动应当检查O_NONBLOCK 标志来看是否是请求非阻塞操作; 其他标志很少使用. 特别地, 应当检查读/写许可, 使用 f_mode 而不是f_flags. 所有的标志在头文件<linux/fcntl.h> 中定义. 4.struct file_operations *f_op;       和文件关联的操作. 内核安排指针作为它的open 实现的一部分, 接着读取它当它需要分派任何的操作时. filp->f_op 中的值从不由内核保存为后面的引用; 这意味着你可改变你的文件关联的文件操作, 在你返回调用者之后新方法会起作用. 例如, 关联到主编号 1 (/dev/null, /dev/zero, 等等)的 open 代码根据打开的次编号来替代 filp->f_op 中的操作. 这个做法允许实现几种行为, 在同一个主编号下而不必在每个系统调用中引入开销. 替换文件操作的能力是面向 对象编程的"方法重载"的内核对等体. 5.void *private_data;       open 系统调用设置这个指针为 NULL, 在为驱动调用 open 方法之前. 你可自由使用这个成员或者忽略它; 你可以使用这个成员来指向分配的数据, 但是接着你必须记住在内核销毁文件结构之前, 在 release 方法中释放那个内存. private_data 是一个有用的资源, 在系统调用间保留状态信息, 我们大部分例子模块都使用它. 6.struct dentry *f_dentry; 关联到文件的目录入口( dentry )结构. 设备驱动编写者正常地不需要关心 dentry 结构, 除了作为 filp->f_dentry->d_inode 存取 inode 结构.

file 结构如下所示:   struct file {   union {   struct list_head fu_list; 文件对象链表指针linux/include/linux/list.h   struct rcu_head fu_rcuhead; RCU(Read-Copy Update)是Linux 2.6内核中新的锁机制   } f_u;   struct path f_path; 包含dentry和mnt两个成员,用于确定文件路径   #define f_dentry f_path.dentry f_path的成员之一,当前文件的dentry结构   #define f_vfsmnt f_path.mnt 表示当前文件所在文件系统的挂载根目录   const struct file_operations *f_op; 与该文件相关联的操作函数   atomic_t f_count; 文件的引用计数(有多少进程打开该文件)   unsigned int f_flags; 对应于open时指定的flag   mode_t f_mode; 读写模式:open的mod_t mode参数   off_t f_pos; 该文件在当前进程中的文件偏移量   struct fown_struct f_owner; 该结构的作用是通过信号进行I/O时间通知的数据。   unsigned int f_uid, f_gid; 文件所有者id,所有者组id   struct file_ra_state f_ra; 在linux/include/linux/fs.h中定义,文件预读相关   unsigned long f_version;   #ifdef CONFIG_SECURITY   void *f_security;   #endif      void *private_data;   #ifdef CONFIG_EPOLL      struct list_head f_ep_links;   spinlock_t f_ep_lock;   #endif   struct address_space *f_mapping;   };   1.2 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;   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]; 存放短文件名   };   1.3 struct files_struct   对于每个进程,包含一个files_struct结构,用来记录文件描述符的使用情况,定义在include/linux/file.h中   struct files_struct   {   atomic_t count; 使用该表的进程数   struct fdtable *fdt;   struct fdtable fdtab;   spinlock_t file_lock ____cacheline_aligned_in_smp;   int next_fd; 数值最小的最近关闭文件的文件描述符,下一个可用的文件描述符   struct embedded_fd_set close_on_exec_init; 执行exec时需要关闭的文件描述符初值集合   struct embedded_fd_set open_fds_init; 文件描述符的屏蔽字初值集合   struct file * fd_array[NR_OPEN_DEFAULT]; 默认打开的fd队列   };   struct fdtable {   unsigned int max_fds;   struct file ** fd; 指向打开的文件描述符列表的指针,开始的时候指向fd_array,   当超过max_fds时,重新分配地址   fd_set *close_on_exec; 执行exec需要关闭的文件描述符位图(fork,exec即不被子进程继承的文件   描述符)   fd_set *open_fds; 打开的文件描述符位图   struct rcu_head rcu;   struct fdtable *next;   };   1.4 struct fs_struct   struct fs_struct {   atomic_t count; 计数器   rwlock_t lock; 读写锁   int umask;   struct dentry * root, * pwd, * altroot;根目录("/"),当前目录以及替换根目录   struct vfsmount * rootmnt, * pwdmnt, * altrootmnt;   };   1.5 struct inode   索引节点对象由inode结构体表示,定义文件在linux/fs.h中。   struct inode {   struct hlist_node i_hash; 哈希表   struct list_head i_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; 使用者id   gid_t i_gid; 使用者id组   kdev_t i_rdev; 实设备标识符   loff_t i_size; 以字节为单位的文件大小   struct timespec i_atime; 最后访问时间   struct timespec i_mtime; 最后修改(modify)时间   struct timespec i_ctime; 最后改变(change)时间   unsigned int i_blkbits; 以位为单位的块大小   unsigned long i_blksize; 以字节为单位的块大小   unsigned long i_version; 版本号   unsigned long i_blocks; 文件的块数   unsigned short i_bytes; 使用的字节数   spinlock_t i_lock; 自旋锁   struct rw_semaphore i_alloc_sem; 索引节点信号量   struct inode_operations *i_op; 索引节点操作表   struct file_operations *i_fop; 默认的索引节点操作   struct super_block *i_sb; 相关的超级块   struct file_lock *i_flock; 文件锁链表   struct address_space *i_mapping; 相关的地址映射   struct address_space i_data; 设备地址映射   struct dquot *i_dquot[MAXQUOTAS];节点的磁盘限额   struct list_head i_devices; 块设备链表   struct pipe_inode_info *i_pipe; 管道信息   struct block_device *i_bdev; 块设备驱动   unsigned long i_dnotify_mask;目录通知掩码   struct dnotify_struct *i_dnotify; 目录通知   unsigned long i_state; 状态标志   unsigned long dirtied_when;首次修改时间   unsigned int i_flags; 文件系统标志   unsigned char i_sock; 套接字   atomic_t i_writecount; 写者记数   void *i_security; 安全模块   __u32 i_generation; 索引节点版本号   union {   void *generic_ip;文件特殊信息   } u;   };

1、struct inode──字符设备驱动相关的重要结构介绍

内核中用inode结构表示具体的文件,而用file结构表示打开的文件描述符。Linux2.6.27内核中,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;     struct mutex        i_mutex; struct rw_semaphore    i_alloc_sem; const struct inode_operations    *i_op; const struct file_operations    *i_fop;     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; struct dnotify_struct    *i_dnotify; #endif
#ifdef CONFIG_INOTIFY struct list_head    inotify_watches; struct mutex        inotify_mutex;     #endif
unsigned long        i_state; unsigned long        dirtied_when;    
unsigned int        i_flags;
atomic_t        i_writecount; #ifdef CONFIG_SECURITY void            *i_security; #endif void            *i_private; };

  我们在进程中打开一个文件F,实际上就是要在内存中建立F的dentry,和inode结构,并让它们与进程结构联系来,把VFS中定义的接口给接起来。我们来看一看这个经典的图:

linux中什么是文件结构体?的更多相关文章

  1. Linux中表示“时间”的结构体和相关函数

    转载于:http://blog.chinaunix.net/uid-25909722-id-2827364.html      Linux中表示“时间”的结构体和相关函数 2011-09-13 17: ...

  2. linux中结构体对齐【转】

    转自:https://blog.csdn.net/suifengpiao_2011/article/details/47260085 linux中定义对齐字节 typedef struct  sdk_ ...

  3. Linux中的两个经典宏定义:获取结构体成员地址,根据成员地址获得结构体地址;Linux中双向链表的经典实现。

    倘若你查看过Linux Kernel的源码,那么你对 offsetof 和 container_of 这两个宏应该不陌生.这两个宏最初是极客写出的,后来在Linux内核中被推广使用. 1. offse ...

  4. Linux中进程控制块PCB-------task_struct结构体结构

    Linux中task_struct用来控制管理进程,结构如下: struct task_struct { //说明了该进程是否可以执行,还是可中断等信息 volatile long state; // ...

  5. Linux就这个范儿 第15章 七种武器 linux 同步IO: sync、fsync与fdatasync Linux中的内存大页面huge page/large page David Cutler Linux读写内存数据的三种方式

    Linux就这个范儿 第15章 七种武器  linux 同步IO: sync.fsync与fdatasync   Linux中的内存大页面huge page/large page  David Cut ...

  6. 浅谈Linux中的信号处理机制(二)

    首先谢谢 @小尧弟 这位朋友对我昨天夜里写的一篇<浅谈Linux中的信号处理机制(一)>的指正,之前的题目我用的“浅析”一词,给人一种要剖析内核的感觉.本人自知功力不够,尚且不能对着Lin ...

  7. Linux中信号量处理

    参考文章: http://blog.csdn.net/qinxiongxu/article/details/7830537/ 信号量一. 什么是信号量信号量的使用主要是用来保护共享资源,使得资源在一个 ...

  8. [tty与uart]1.Linux中tty框架与uart框架之间的调用关系剖析

    转自:http://developer.51cto.com/art/201209/357501_all.htm 目录 1.tty框架 2.uart框架 3.自底向上 4.自顶向下 5.关系图 在这期间 ...

  9. linux中的namespace

         本文将就namespace这个知识点,进行简单的归纳总结,力求通俗易通.在资料汇总的过程中,参考了许多网上的博客资料,在文章尾部给出相关链接.      namespace,命名空间,从名字 ...

随机推荐

  1. beyond compare 软件学习

    beyond compare 软件可以实现基本的文件对比,这点和 NotePad++ 的功能一样.但是在实现文件夹与文件夹之间的对比的话,就要使用 beyond compare 进行对比,效率是成倍提 ...

  2. H265 Rtp封包

    H265 Rtp封包可以参考Ffmpeg,具体实现在文件rtpenc_h264_hevc.c(4.0.1版本),核心的方法是nal_send 这个方法有些绕,下面帖子具体的代码及注释. static ...

  3. 1 android 人脸识别

    1 https://www.google.com.hk/search?newwindow=1&safe=strict&q=android+%E5%9B%BE%E7%89%87%E4%B ...

  4. Ansible@一个高效的配置管理工具--Ansible configure management--翻译(五)

    无书面许可请勿转载 高级Playbook Extra variables You may have seen in our template example in the previous chapt ...

  5. Atitit.远程接口 监控与木马   常用的api 标准化v2 q216

    Atitit.远程接口 监控与木马   常用的api 标准化v2 q216 1. 木马与远程接口 监控的常用的api2 1.1. 文件复制2 1.2. 屏幕定时截图2 1.3. 邮件发送2 1.4.  ...

  6. 篇章二:[AngularJS] 使用AngularAMD動態載入Service

    前言 「使用AngularAMD動態載入Controller」:這篇文章裡介紹如何使用AngularAMD來動態載入Controller.本篇文章以此為基礎,介紹如何使用AngularAMD來動態載入 ...

  7. oracle中的not in和not exists注意事项

    NOT IN:不包括空值 NOT EXISTS:包括空值

  8. js中的DOM节点

    文档对象模型DOM(Document Object Model)定义访问和处理HTML文档的标准方法. DOM 将HTML文档呈现为带有元素.属性和文本的树结构(节点树). 把上面的代码拆分为Dom节 ...

  9. ntp服务及其配置

    集群中使用NTP服务 简介 之前搭建zookeeper时报了一个错,我以为是ntp的问题,结果不是.这里详细学习一下如何在集群中使用ntp服务. 什么是ntp服务 来自ntp的百度百科: NTP服务器 ...

  10. PHP-Socket-阻塞与非阻塞,同步与异步概念的理解

    1. 概念理解 在进行网络编程时,我们常常见到同步(Sync)/异步(Async),阻塞(Block)/非阻塞(Unblock)四种调用方式:同步:      所谓同步,就是在发出一个功能调用时,在没 ...