当前位置:编程学习 > C/C++ >>

file_operations结构体介绍

1.在内核中file_operations源码 linux-2.6.38/include/linux/fs.h


[cpp]
/*
 * NOTE:
 * all file operations except setlease can be called without
 * the big kernel lock held in all filesystems.
 */ 
struct file_operations { 
    struct module *owner; 
    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); 
    unsigned int (*poll) (struct file *, struct poll_table_struct *); 
    long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long); 
    long (*compat_ioctl) (struct file *, unsigned int, unsigned long); 
    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 *, int datasync); 
    int (*aio_fsync) (struct kiocb *, int datasync); 
    int (*fasync) (int, struct file *, int); 
    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 **); 
    long (*fallocate)(struct file *file, int mode, loff_t offset, 
              loff_t len); 
}; 

/*
 * NOTE:
 * all file operations except setlease can be called without
 * the big kernel lock held in all filesystems.
 */
struct file_operations {
 struct module *owner;
 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);
 unsigned int (*poll) (struct file *, struct poll_table_struct *);
 long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
 long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
 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 *, int datasync);
 int (*aio_fsync) (struct kiocb *, int datasync);
 int (*fasync) (int, struct file *, int);
 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 **);
 long (*fallocate)(struct file *file, int mode, loff_t offset,
     loff_t len);
};

 


1.struct module *owner

第一个file_operations 成员根本不是一个操作;它是一个指向拥有这个结构的模块的指针.这

个成员用来在它的操作还在被使用时阻止模块被卸载.几乎所有时间中,它被简单初始化为

THIS_MODULE, 一个在<linux/module.h> 中定义的宏.

2.loff_t (*llseek) (struct file *, loff_t, int);

llseek 方法用作改变文件中的当前读/写位置,并且新位置作为(正的)返回值.loff_t 参数是一

个"longoffset", 并且就算在32位平台上也至少64 位宽.错误由一个负返回值指示.如果这个

函数指针是NULL, seek 调用会以潜在地无法预知的方式修改file 结构中的位置计数器.

3.ssize_t (*read) (struct file *, char __user *, size_t, loff_t*);

用来从设备中获取数据.在这个位置的一个空指针导致read 系统调用以-EINVAL("Invalid

argument") 失败.一个非负返回值代表了成功读取的字节数(返回值是一个"signed size" 类型,

常常是目标平台本地的整数类型).

4.ssize_t (*aio_read)(struct kiocb *, char __user *, size_t,loff_t);

初始化一个异步读-- 可能在函数返回前不结束的读操作.如果这个方法是NULL, 所有的操

作会由read 代替进行(同步地).

5.ssize_t (*write) (struct file *, const char __user *, size_t,loff_t *);

发送数据给设备.如果NULL, -EINVAL 返回给调用write 系统调用的程序.如果非负,返回值

代表成功写的字节数.

6.ssize_t (*aio_write)(struct kiocb *, const char __user *,size_t, loff_t *);

初始化设备上的一个异步写.

7.int (*readdir) (struct file *, void *, filldir_t);

对于设备文件这个成员应当为NULL; 它用来读取目录,并且仅对文件系统有用.

8.unsigned int (*poll) (struct file *, struct poll_table_struct*);

poll 方法是3 个系统调用的后端:poll, epoll, 和select, 都用作查询对一个或多个文件描述符的

读或写是否会阻塞.poll 方法应当返回一个位掩码指示是否非阻塞的读或写是可能的,并且,

可能地,提供给内核信息用来使调用进程睡眠直到I/O 变为可能.如果一个驱动的poll 方法

为NULL, 设备假定为不阻塞地可读可写.

9.int (*ioctl) (struct inode *, struct file *, unsigned int,unsigned long);

ioctl 系统调用提供了发出设备特定命令的方法(例如格式化软盘的一个磁道,这不是读也不

是写).另外,几个ioctl 命令被内核识别而不必引用fops 表.如果设备不提供ioctl 方法,对于

任何未事先定义的请求(-ENOTTY,"设备无这样的ioctl"), 系统调用返回一个错误.

10.int (*mmap) (struct file *, struct vm_area_struct *);

mmap 用来请求将设备内存映射到进程的地址空间.如果这个方法是NULL, mmap 系统调用

返回-ENODEV.

11.int (*open) (struct inode *, struct file *);

尽管这常常是对设备文件进行的第一个操作,不要求驱动声明一个对应的方法.如果这个项

是NULL, 设备打开一直成功,但是你的驱动不会得到通知.

12.int (*flush) (struct file *);

flush 操作在进程关闭它的设备文件描述符的拷贝时调用;它应当执行(并且等待)设备的任何

未完成的操作.这个必须不要和用户查询请求的fsync 操作混淆了.当前,flush 在很少驱动中

使用;SCSI 磁带驱动使用它,例如,为确保所有写的数据在设备关闭前写到磁带上.如果flush

为NULL, 内核

补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,