您的位置:首页 > 理论基础 > 数据结构算法

字符设备驱动程序中重要的三个数据结构file_operations、inode、file

2016-03-17 15:41 531 查看
struct _file_operations在Fs.h这个文件里面被定义的,如下所示:

struct file_operations { 

    struct module *owner;//拥有该结构的模块的指针,一般为THIS_MODULES 

    loff_t (*llseek) (struct file *, loff_t, int);//用来修改文件当前的读写位置 

    ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);//从设备中同步读取数据 

    ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);//向设备发送数据

    ssize_t (*aio_read) (struct kiocb *, const struct iovec *, unsigned long, loff_t);//初始化一个异步的读取操作 

    ssize_t (*aio_write) (struct kiocb *, const struct iovec *, unsigned long, loff_t);//初始化一个异步的写入操作 

    int (*readdir) (struct file *, void *, filldir_t);//仅用于读取目录,对于设备文件,该字段为NULL 

    unsigned int (*poll) (struct file *, struct poll_table_struct *); //轮询函数,判断目前是否可以进行非阻塞的读写或写入 

    int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long); //执行设备I/O控制命令 

    long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long); //不使用BLK文件系统,将使用此种函数指针代替ioctl 

    long (*compat_ioctl) (struct file *, unsigned int, unsigned long); //在64位系统上,32位的ioctl调用将使用此函数指针代替 

    int (*mmap) (struct file *, struct vm_area_struct *); //用于请求将设备内存映射到进程地址空间 

    int (*open) (struct inode *, struct file *); //打开 

    int (*flush) (struct file *, fl_owner_t id); 

    int (*release) (struct inode *, struct file *); //关闭 

    int (*fsync) (struct file *, struct dentry *, int datasync); //刷新待处理的数据 

    int (*aio_fsync) (struct kiocb *, int datasync); //异步刷新待处理的数据 

    int (*fasync) (int, struct file *, int); //通知设备FASYNC标志发生变化 

    int (*lock) (struct file *, int, struct file_lock *); 

    ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int); 

    unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long); 

    int (*check_flags)(int); 

    int (*flock) (struct file *, int, struct file_lock *); 

    ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int); 

    ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int); 

    int (*setlease)(struct file *, long, struct file_lock **); 

};

Linux使用file_operations结构访问驱动程序的函数,这个结构的每一个成员的名字都对应着一个调用。用户进程利用在对设备文件进行诸如read/write操作的时候,系统调用通过设备文件的主设备号找到相应的设备驱动程序,然后读取这个数据结构相应的函数指针,接着把控制权交
4000
给该函数,这是Linux的设备驱动程序工作的基本原理。

下面是struct 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;                /*使用者id */ 

gid_t           i_gid;                 /*使用者id组*/ 

dev_t           i_rdev;             //该成员表示设备文件的inode结构,它包含了真正的设备编号。 

u64             i_version; 

loff_t           i_size;              /*inode多代表的文件的大小*/ 

#ifdef __NEED_I_SIZE_ORDERED 

seqcount_t       i_size_seqcount; 

#endif 

struct timespec       i_atime;        /*inode最后一次存取的时间*/ 

struct timespec       i_mtime;       /*inode最后一次修改的时间*/ 

struct timespec       i_ctime;       /*inode的创建时间*/ 

unsigned int       i_blkbits;          /*inode在做I/O时的区块大小*/ 

blkcnt_t       i_blocks;                 /*inode所石油的block块数*/ 

unsigned short          i_bytes; 

umode_t           i_mode;    /*inode的权限*/ 

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;    /*若是字符设备,为其对应的cdev结构体指针*/ 

union { 

struct pipe_inode_info   *i_pipe; 

struct block_device   *i_bdev;     /*若是块设备,为其对应的cdev结构体指针*/ 

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结构表示具体的文件,也就是对应与硬盘上面具体的文件。提供了设备文件的信息。inode译成中文就是索引节点。每个存储设备或存储设备的分区(存储设备是硬盘、软盘、U盘 ... ... )被格式化为文件系统后,应该有两部份,一部份是inode,另一部份是Block,Block是用来存储数据用的。而inode呢,就是用来存储这些数据的信息,这些信息包括文件大小、属主、归属的用户组、读写权限等。inode为每个文件进行信息索引,所以就有了inode的数值。操作系统根据指令,能通过inode值最快的找到相对应的文件。

最后是struct file。

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;//读写模式:open的mod_t mode参数 

loff_t               f_pos;//该文件在当前进程中的文件偏移量 

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

unsigned int              f_uid, f_gid;//文件所有者id,所有者组id 

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 

};

文件结构代表一个打开的文件描述符,它不是专门给驱动程序使用的,系统中每一个打开的文件在内核中都有一个关联的struct file。它由内核在open时创建,并传递给在文件上操作的任何函数,知道最后关闭。当文件的所有实例都关闭之后,内核释放这个数据结构。

最后:总结一下,struct operations存在于设备驱动程序内部,起着联系应用程序和设备驱动的作用。struct inode存在于磁盘上,作为描述设备驱动文件的信息的作用。struct file是在执行open函数时产生的,每打开一个文件就产生一个struct file,供设备驱动关联的函数使用。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: