linux驱动---file_operations之llseek

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

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
设备驱动程序与内核的接口通过数据结构 file_operations 完成。 在 Linux 中,每个设备驱动程序都对应一个 file_operations 结构体,它用于定义设备驱动程序的操作方法和回调函数。file_operations 结构体通常包含以下字段: - 文件打开方法(open):用于打开设备文件,通常会进行设备初始化等操作。 - 文件读取方法(read):用于从设备中读取数据。 - 文件写入方法(write):用于向设备中写入数据。 - 文件定位方法(llseek):用于定位文件读写指针的位置。 - 文件控制方法(ioctl):用于执行设备的控制操作。 - 文件释放方法(release):用于释放设备资源,通常会进行设备关闭等操作。 设备驱动程序通过实现 file_operations 结构体中定义的方法和回调函数来提供对设备的访问。当应用程序打开设备文件并执行读写等操作时,内核会根据文件描述符找到对应的 file_operations 结构体,并调用相应的方法来完成具体的操作。因此,file_operations 结构体是设备驱动程序与内核的接口之一,它定义了设备驱动程序的操作方法和内核的调用方式,是设备驱动程序实现的关键。 需要注意的是,file_operations 结构体只是设备驱动程序与内核的接口之一,与文件系统的 inode_operations、super_block_operations、dentry_operations 等不同。file_operations 结构体用于定义设备驱动程序的操作方法和回调函数,而 inode_operations、super_block_operations、dentry_operations 等则用于定义文件系统的操作方法和回调函数。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值