linux 内核文件结构体,文件结构体struct file(Linux 2.6.23内核)

一、定义:

struct file结构体定义在/linux/include/linux/fs.h(Linux 2.6.11内核)中,其原型是:721struct file {

723 * fu_list becomes invalid after file_free is called and queued via

724 * fu_rcuhead for RCU freeing

726 union {

729 } f_u;

735 unsigned int f_flags;

739 unsigned int f_uid, f_gid;

742 unsigned long f_version;

745#endif

746 /* needed for tty driver, and maybe others */

750 /* Used by fs/eventpoll.c to link all the hooks to this file */

753#endif /* #ifdef CONFIG_EPOLL */

二、作用:

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

三、个字段详解:

1、

union {

struct list_head fu_list;

struct rcu_head rcuhead;

}f_u;

其中的struct list_head定义在 linux/include/linux/list.h中,原型为:

list_head是内核中最常用的建立双向循环链表的结构,在此用于通用文件对象链表的指针。

struct rcu_head定义在linux/include/linux/rcupdate.h中,其原型为:

RCU(Read-Copy Update)是Linux 2.6内核中新的锁机制,具体参考:

在此用于更新文件。fu_list在file_free()函数被调用以后就无效了,队列通过rcu_head来释放RCU。

2、struct path f_path;

被定义在linux/include/linux/namei.h中,其原型为:

688struct fown_struct {689 rwlock_t lock; /* protects pid, uid, euid fields */690 struct pid *pid; /* pid or -pgrp where SIGIO should be sent */691 enum pid_type pid_type;/*Kind of process group SIGIO should be sent to*/692 uid_t uid, euid; /* uid/euid of process setting the owner */693 int signum; /* posix.1b rt signal to be delivered on IO */694};

该结构的作用是通过信号进行I/O时间通知的数据。

9、unsigned int f_uid, f_gid;

标识文件的所有者id,所有者所在组的id.

10、struct file_ra_state f_ra;

struct file_ra_state结构被定义在/linux/include/linux/fs.h中,原型为:700pgoff_t start; /* where readahead started */

701 unsigned long size; /* # of readahead pages */

702 unsigned long async_size; /* do asynchronous readahead when

703 there are only # of pages ahead */

705 unsigned long ra_pages; /* Maximum readahead window */

706 unsigned long mmap_hit; /* Cache hit stat for mmap accesses */

707 unsigned long mmap_miss; /* Cache miss stat for mmap accesses */

708 unsigned long prev_index; /* Cache last read() position */

709 unsigned int prev_offset; /* Offset where last read() ended in a page */

文件预读状态,文件预读算法使用的主要数据结构,当打开一个文件时,f_ra中出了perv_page(默认为-1)和ra_apges(对该文件允许的最大预读量)这两个字段外,其他的所有字端都置为0。

11、unsigned long f_version;

记录文件的版本号,每次使用后都自动递增。

12、

#ifdef CONFIG_SECURITY

void *f_security;

#endif

此处我的理解是如果在编译内核时配置了安全措施,那么struct file结构中就会有void *f_security数据项,用来描述安全措施或者是记录与安全有关的信息。

13、void *private_data;

系统在调用驱动程序的open方法前将这个指针置为NULL。驱动程序可以将这个字段用于任意目的,也可以忽略这个字段。驱动程序可以用这个字段指向已分配的数据,但是一定要在内核释放file结构前的release方法中清除它。

14、

#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 */

被用在fs/eventpoll.c来链接所有钩到这个文件上。其中f_ep_links是文件的事件轮询等待者链表的头,f_ep_lock是保护f_ep_links链表的自旋锁。

15、struct address_space *f_mapping;

struct address_space被定义在/linux/include/linux/fs.h中,此处是指向文件地址空间的指针。

四、应用:

在驱动开发中,文件读/写模式mode、标志f_flags都是设备驱动关心的内容,而私有数据指针private_data在驱动中被广泛使用,大多被指向设备驱动自定义的用于描述设备的结构体。驱动程序中常用如下类似的代码来检测用户打开文件的读写方式:

if (file->f_mode & FMODE_WRITE) //用户要求可写

{

}

if (file->f_mode & FMODE_READ) //用户要求可读

{

}

下面的代码可用于判断以阻塞还是非阻塞方式打开设备文件:

if (file->f_flags & O_NONBLOCK) //非阻塞

pr_debug("open:non-blocking\n");

else //阻塞

pr_debug("open:blocking\n");

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值