linux驱动---file_operations之llseek
来源:互联网 发布:sql sum group by 编辑:程序博客网 时间:2024/05/19 19:31
简述:
file_operations中llseek是用于定位设备文件位置。
lseek,sys_lseek(在include/unistd.h,如果是在宿主机上,ubuntu所在路径/usr/include/unistd.h)这两个系统调用,会调用到file_operations中的llseek.
file_operations结构体定义:
这是Linux内核3.10版本
定义在include/linux/fs.h中
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 *, loff_t, loff_t, 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); int (*show_fdinfo)(struct seq_file *m, struct file *f);};
其中:
loff_t (llseek) (struct file , loff_t, int);
功能:定位文件位置
第一个参数:打开的文件指针(文件句柄)
第二个参数:文件偏移量(偏移量单位是字节)
第三个参数:文件起止位置标志
文件起止位置标志:
一般有下面几个标志:
#define SEEK_SET 0 /* seek relative to beginning of file */#define SEEK_CUR 1 /* seek relative to current file position */#define SEEK_END 2 /* seek relative to end of file */#define SEEK_DATA 3 /* seek to the next data */#define SEEK_HOLE 4 /* seek to the next hole */#define SEEK_MAX SEEK_HOLE
前面三个是常用的:
SEEK_SET:表从文件开始位置加上偏移量处,(偏移量单位是字节)
SEEK_CUR:表示当前文件位置加上偏移量处,(偏移量单位是字节)
SEEK_END:表示从文件结束位置加上偏移量,(偏移量单位是字节)
在系统调用lseek中:
extern __off_t lseek(int __fd , __off_t __offset, int __whence);
第三个参数__whence就是传递这个标志到驱动的。
文件位置是由struct file结构体成员f_ops表示的:
定义在include/linux/fs.h中
loff_t f_pos;
驱动程序中llseek例子:
loff_t d_llseek(struct file *f, loff_t off, int whence){ .... ... switch(whence) { case SEEK_SET: f->f_pos = off;//文件开始位置是0 break; case SEEK_CUR: f->f_pos += off; break; case SEEK_END: f->f_pos = dev->size + off; default: break; }}
dev->size是指设备文件最大空间,文件开始位置是0,自然结束位置就是最大空间的数字。
这样就达到了对文件位置f->f_pos修改,从而llseek实现了定位功能。
(其实可以随意修改f_pos来定位文件位置,但是不能这么做,会乱套。)
ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
其中read,write的第四个参数也是偏移量。
如驱动有下面的read函数:
char buf[1000];ssize_t d_read(struct file *f, char __user *up, size_t s, loff_t *off){ char *p = buf; p += (f->f_ops+(*off)); copy_to_user(up,p,s);}
p += (f->f_ops+(*p))表示定位文件位置。上面这段代码,就是从指定的位置处,读取s个字节到用户空间up。
对于不支持llseek的驱动,应该在open中调用nonseekable_open():
int nonseekable_open(struct inode *inode; struct file *filp);
并且将file_operations中的llseek设置成no_llseek,这样当有对这个文件调用lseek,系统会拒绝。
如下驱动例子:
int d_open(struct inode *in, struct file *f){ ... .. nonseekable_open(in,f); ... ..}struct file_operations op = { .llseek = no_llseek; .open = d_open; ... ..};
可以参考如下博客:
LDD3源码分析之llseek分析
lseek及llseek介绍
- linux驱动---file_operations之llseek
- linux驱动之file_operations
- linux驱动---file_operations之poll
- linux驱动-file_operations之ioctl
- linux驱动-file_operations之mmap
- linux字符设备驱动之file_operations结构体知识详解
- Linux 设备驱动中的file_operations
- linux设备驱动第三版笔记---->linux驱动重要数据结构之file_operations
- linux设备驱动中file_operations结…
- 【驱动开发】file_operations ---linux 2.6.30
- linux驱动---file_operations异步读写aio_read、aio_write
- Linux 驱动学习笔记(file_operations结构体详细分析)
- linux设备驱动中file_operations结构体分析
- 驱动该如何入门 关于file_operations和Linux设备模型
- Linux字符设备驱动——file_operations结构体
- linux驱动开发--字符设备:简单的file_operations示例
- linux设备驱动中file_operations结构体分析
- linux设备驱动中file_operations结构体分析
- 这些小工具让你的Android 开发更高效
- GStreamer播放教程03——pipeline的快捷访问
- Android开发界面显示慢-过度绘制优化
- MySQL相关操作
- python下安装模块报错:psutil/_psutil_linux.c:12:20: fatal error: Python.h: No such file or directory
- linux驱动---file_operations之llseek
- ViewPager嵌套使用SwipeRefreshLayout下拉功能不能使用
- tomcat启动时报java.io.EOFException
- 前端优化:九个技巧,提高Web性能
- 手绘板在数字绘画与设计行业的应用特色
- Extension 与主app共享数据
- web前端优化方法
- linux下vi命令大全
- Unity Animator混合树