Linux内核文件系统-虚拟文件系统-文件操作

建议点击这里查看个人主页上的最新原文

点击这里在哔哩哔哩bilibili在线观看配套的教学视频

点击这里在哔哩哔哩bilibili在线观看配套的加餐视频(就是一些补充)

点击跳转到内核课程所有目录

一般的Linux书籍都是先讲解进程和内存相关的知识,但我想先讲解文件系统。第一,因为我就是做文件系统的,更擅长这一块,其他模块的内容我还要再去好好看看书,毕竟不能误人子弟嘛;第二,是因为文件系统模块更接近于用户态,是相对比较好理解的内容(当然想深入还是要下大功夫的),由文件系统入手比较适合初学者。

虚拟文件系统英文全称Virtual file system,缩写为VFS,又称为虚拟文件切换系统(virtual filesystem switch)。所有的文件系统都要先经过虚拟文件系统层,虚拟文件系统相当于制定了一套规则,如果你想写一个新的文件系统,只需要遵守这套规则就可以了。

VFS虽然是用C语言写的,但使用了面向对象的设计思路。

文件对象中最重要的一个成员是f_op,你会发现,文件操作方法名和很多系统调用很像。

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 (*read_iter) (struct kiocb *, struct iov_iter *);
        ssize_t (*write_iter) (struct kiocb *, struct iov_iter *);
        int (*iopoll)(struct kiocb *kiocb, struct io_comp_batch *,
                        unsigned int flags);
        int (*iterate_shared) (struct file *, struct dir_context *); // v6.6在iterate_dir中加读锁,但在较早的版本(如v4.19)有些文件系统未实现此方法时加写锁
        __poll_t (*poll) (struct file *, struct poll_table_struct *); // 睡眠等待给定文件活动
        long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long); // 不需要持有BKL,相比compat_ioctl,优先实现此方法
        long (*compat_ioctl) (struct file *, unsigned int, unsigned long); // 可移植变种,也不需要持有BKL
        int (*mmap) (struct file *, struct vm_area_struct *); // 将文件映射到地址空间上
        unsigned long mmap_supported_flags;
        int (*open) (struct inode *, struct file *); // 创建新的文件对象,与inode关联
        int (*flush) (struct file *, fl_owner_t id); // 已打开文件的引用计数减少时调用,作用取决于具体的文件系统
        int (*release) (struct inode *, struct file *); // 当引用计数为0时调用,作用取决于具体的文件系统
        int (*fsync) (struct file *, loff_t, loff_t, int datasync); // 所有文件的缓存数据写回磁盘
        int (*fasync) (int, struct file *, int); // 打开或关闭异步IO的通告信号
        int (*lock) (struct file *, int, struct file_lock *); // 给文件上锁
        unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long); // 获取未使用的地址空间来映射给定的文件
        int (*check_flags)(int); // 检查fcntl()系统调用的flags的有效性,只有nfs实现了
        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);
        void (*splice_eof)(struct file *file);
        int (*setlease)(struct file *, int, struct file_lock **, void **);
        long (*fallocate)(struct file *file, int mode, loff_t offset,
                          loff_t len);
        void (*show_fdinfo)(struct seq_file *m, struct file *f);
#ifndef CONFIG_MMU
        unsigned (*mmap_capabilities)(struct file *);
#endif
        ssize_t (*copy_file_range)(struct file *, loff_t, struct file *,
                        loff_t, size_t, unsigned int);
        loff_t (*remap_file_range)(struct file *file_in, loff_t pos_in,
                                   struct file *file_out, loff_t pos_out,
                                   loff_t len, unsigned int remap_flags);
        int (*fadvise)(struct file *, loff_t, loff_t, int);
        int (*uring_cmd)(struct io_uring_cmd *ioucmd, unsigned int issue_flags);
        int (*uring_cmd_iopoll)(struct io_uring_cmd *, struct io_comp_batch *,
                                unsigned int poll_flags);
} __randomize_layout;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值