一些重要的数据结构(VFS核心结构体)
1.super_block
和mount一一对应,这也意味着如果mount操作,在内核中分配新的结构体,存储分区信息
umount时,VFS就把该结构体释放掉
mount几次内核中就有几个super_block结构体
2.inode
和打开文件一一对应,记录文件信息,关闭文件时,如果内存不紧张,不会释放,
如果多个人同时打开一个文件,VFS只需创建一个inode
3.file <linux/fs.h>
和用户态对文件操作open一一对应,每次open生成一个file结构体,
内核中有多个file结构体和打开的文件对应
该结构与用户空间的FILE没有任何关联,他代表一个打开的文件,它由内核在open时创建
直到最后的close函数,在文件的所有实例都被关闭之后,内核会释放这个数据结构
指向struct file的指针称为文件指针
4.file_operations <linux/fs.h>
其中包括了一组函数指针,每个打开的文件和一组函数关联起来;这些操作用于实现系统调用
file_operations和指向这类结构的指针叫做fops
file_operations的初始化方式
字符设备的注册
在运行的时候获得一个独立的cdev结构:
此时就可以将cdev结构嵌入到自己的设备的特定结构中
以下代码用来初始化已分配到的结构:
通过以下代码告知内核结构的信息:
dev 是cdev结构
num 是该设备对应的第一设备编号
count 是应该和该设备关联的设备号的数量
scull中的设备注册
在scull内部, 他通过struct scull_dev的结构来表示每个设备
量子:我们把每一个内存区域称之为量子,一个指向这些量子的指针数组称之为量子集
早期办法(内核2.6之前的版本的设备注册的方法)
注册:
释放:
1.super_block
和mount一一对应,这也意味着如果mount操作,在内核中分配新的结构体,存储分区信息
umount时,VFS就把该结构体释放掉
mount几次内核中就有几个super_block结构体
2.inode
和打开文件一一对应,记录文件信息,关闭文件时,如果内存不紧张,不会释放,
如果多个人同时打开一个文件,VFS只需创建一个inode
3.file <linux/fs.h>
和用户态对文件操作open一一对应,每次open生成一个file结构体,
内核中有多个file结构体和打开的文件对应
该结构与用户空间的FILE没有任何关联,他代表一个打开的文件,它由内核在open时创建
直到最后的close函数,在文件的所有实例都被关闭之后,内核会释放这个数据结构
指向struct file的指针称为文件指针
- struct file {
- union {
- struct list_head fu_list;
- struct rcu_head fu_rcuhead;
- } f_u;
- struct path f_path;
- #define f_dentry f_path.dentry
- //文件对应的目录项结构
- #define f_vfsmnt f_path.mnt
- const struct file_operations *f_op;
- atomic_long_t f_count;
- unsigned int f_flags;
- //文件标志, 如O_RDONLY, O_NONBLOCK, O_SYNC
- fmode_t f_mode;
- //文件模式
- loff_t f_pos;
- //文件当前读/写位置
- struct fown_struct f_owner;
- unsigned int f_uid, f_gid;
- struct file_ra_state f_ra;
- u64 f_version;
- #ifdef CONFIG_SECURITY
- void *f_security;
- #endif
- /* needed for tty driver, and maybe others */
- void *private_data;
-
- #ifdef CONFIG_EPOLL
- /* Used by fs/eventpoll.c to link all the hooks to this file */
- struct list_head f_ep_links;
- spinlock_t f_ep_lock;
- #endif /* #ifdef CONFIG_EPOLL */
- struct address_space *f_mapping;
- #ifdef CONFIG_DEBUG_WRITECOUNT
- unsigned long f_mnt_write_state;
- #endif
- };
其中包括了一组函数指针,每个打开的文件和一组函数关联起来;这些操作用于实现系统调用
file_operations和指向这类结构的指针叫做fops
- struct file_operations {
- struct module *owner;
- 指向拥有该结构的模块的指针,防止模块的操作在被使用的时候卸载该模块
- .onwer = THIS_MODULE
- loff_t (*llseek) (struct file *, loff_t, int);
- 修改文件的当前读写位置,返回新位置
- 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);
- 对设备文件来说,应该置null,用于读取目录,只对文件系统有用
- unsigned int (*poll) (struct file *, struct poll_table_struct *);
- 是poll,epoll,select的后端实现
- int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);
- 提供了一种执行设备特定命令的方法
- 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 *);
- 对设备文件执行的第一个操作,但不要求驱动程序一定声明该方法,
- 如果置为NULL,设备的打开永远有效,但系统不会通知驱动程序
- int (*flush) (struct file *, fl_owner_t id);
- 发生在进程关闭设备文件描述符副本的时候,用来执行设备上尚未完成的操作
- int (*release) (struct inode *, struct file *);
- 当file被释放时, 将调用这个操作,与open相仿,也可设置为NULL
- int (*fsync) (struct file *, struct dentry *, int datasync);
- 刷新待处理的数据
- int (*aio_fsync) (struct kiocb *, int datasync);
- fsync的异步方法
- int (*fasync) (int, struct file *, int);
- 通知设备其FASYNC状态发生了变化
- 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);
- 检查传递给fcntl(F_SETFL...)调用的标志
- int (*dir_notify)(struct file *filp, unsigned long arg);
- 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 **);
- };
- struct file_operations scull_fops = {
- .owner = THIS_MODULE,
- .llseek = scull_llseek,
- .read = scull_read,
- .write = scull_write,
- .ioctl = scull_ioctl,
- .open = scull_open,
- .release = scull_release,
- };
在运行的时候获得一个独立的cdev结构:
- struct cdev *my_cdev = cdev_alloc();
- my_cdev->ops = &my_fops;
以下代码用来初始化已分配到的结构:
- void cdev_init(struct cdev *cdev, struct file_operations *fops);
- int cdev_add(struct cdev *dev, dev_t num, unsigned int count);
dev 是cdev结构
num 是该设备对应的第一设备编号
count 是应该和该设备关联的设备号的数量
scull中的设备注册
在scull内部, 他通过struct scull_dev的结构来表示每个设备
- struct scull_dev {
- struct scull_qset *data; /* 指向第一个量子集的指针 */
- int quantum; /* 当前量子的大小 */
- int qset; /* 当前数组的大小 */
- unsigned long size; /* 保存在其中的数据总量 */
- unsigned int access_key; /* 由sculluid和scullpriv使用 */
- struct semaphore sem; /* 互斥信号量 */
- struct cdev cdev /* 字符设备结构 */
- };
- static void scull_setup_cdev(struct scull_dev *dev, int index)
- {
- int err, devno;
- devno = MKDEV(scull_major, scull_minor index);
-
- cdev_init(&dev->sdev, &scull_fops);
- dev->cdev.owner = THIS_MODULE;
- dev->cdev.ops = &scull_fops;
- err = cdev_add(&dev->cdev, devno, 1);
- if (err) {
- printk(KERN_NOTICE "Error %d adding scull%d", err, index);
- }
- }
早期办法(内核2.6之前的版本的设备注册的方法)
注册:
- int register_chrdev(unsigned int major,
- const char * name,
- struct file_operations *fops);
释放:
- int unregister_chrdev(unsigned int major, const char * name);