linux 2.6 下file_operations下函数详解

本文详细介绍了Linux环境下文件操作结构file_operations的内容,包括各种文件操作函数指针的作用和用途,如读写、寻址、文件锁等,适用于理解Linux内核中文件系统的实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

基于kernel 2.6.15
struct
file_operations{
        struct module *owner; 
        //
指向该结构的模的指,避免正在操作被卸,一般初始化THIS_MODULES
        loff_t (*llseek) (struct file *, loff_t, int);
        // llseek
用来修改文件当前的写位置,返回新位置
        // loff_t
一个"偏移量"。此函数针为seek用将会以不可期的方式修改file构中的位置数器。
       
ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
        //
设备中同步读取数据。取成功返回取的字数。NULL返回-EINVAL
        ssize_t (*aio_read) (struct kiocb *, char __user *, size_t, loff_t);
        //
初始化一个异取操作,NULL全部通read
        ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
        //
设备发送数据。
        ssize_t (*aio_write) (struct kiocb *, const char __user *, size_t, loff_t);
        //
初始化一个异的写入操作。
        int (*readdir) (struct file *, void *, filldir_t);
        //
用于取目设备文件,字段 NULL
        unsigned int (*poll) (struct file *, struct poll_table_struct *);
        //
返回一个位掩,用来指出非阻塞的取或写入是否可能。
       
// pool义为 NULL设备会被认为即可也可写。
       
int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);
        //
提供一种执设备特殊命令的方法。不置入口点,返回-ENOTTY
        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 *);
        //
用于求将设备内存映射到程地址空。如果无此方法,将访问-ENODEV
        int (*open) (struct inode *, struct file *);
        //
如果空,设备的打操作永成功,但系不会通知驱动程序
        //
由VFS用,当VFS打一个文件,即建立了一个新的"struct file",之后用open方法分配文件构。open属于struct inode_operations。
        int (*flush) (struct file *);
        //
生在关闭设备文件描述符副本,行并等待,若NULL,内核将忽略用户应用程序的求。
        int (*release) (struct inode *, struct file *);
        // file
,将用此指函数,release与open相同可NULL
        int (*fsync) (struct file *, struct dentry *, int datasync);
        //
刷新待理的数据,如果驱动程序没有实现,fsync用将返回-EINVAL
        int (*aio_fsync) (struct kiocb *, int datasync);
        //
fsync
        int (*fasync) (int, struct file *, int);
        //
通知设备FASYNC化,如果设备不支持异通知,字段可以NULL
        int (*lock) (struct file *, int, struct file_lock *);
        //
实现文件设备驱动常不去实现此lock
        ssize_t (*readv) (struct file *, const struct iovec *, unsigned long, loff_t *);
        ssize_t (*writev) (struct file *, const struct iovec *, unsigned long, loff_t *);
        // readv
和writev 分散/聚集型的写操作,实现进及多个内存区域的或写操作。
        ssize_t (*sendfile) (struct file *, loff_t *, size_t, read_actor_t, void *);
        //
实现sendfile用的取部分,将数据从一个文件描述符移到另一个,设备驱动通常将其 NULL
        ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
        //
实现sendfile用的另一部分,内核用将其数据送到对应文件,次一个数据设备驱动通常将其NULL
        unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
        //
程地址空找到一个合适的位置,以便将底层设备中的内存段映射到位置。大部分驱动可将其NULL
        int (*check_flags)(int);
        //
块检查传递给fcntl(F_SETEL...)用的
        int (*dir_notify)(struct file *filp, unsigned long arg);
        //
用程序使用fcntl来求目通知方法。仅对文件系有效,驱动程序不必实现
        int (*flock) (struct file *, int, struct file_lock *);
        //
实现文件
};
 
  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值