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);