简述: 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介绍
