日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

file_operations结构

發(fā)布時(shí)間:2024/9/21 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 file_operations结构 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Linux中字符設(shè)備驅(qū)動(dòng)程序的主要功能是實(shí)現(xiàn)設(shè)備的讀寫和控制接口。對(duì)于字符設(shè)備驅(qū)動(dòng)程序,最核心的就是file_operations結(jié)構(gòu),這個(gè)結(jié)構(gòu)實(shí)際上是VFS(虛擬文件系統(tǒng))的文件接口,它的每一個(gè)成員函數(shù)一般都對(duì)應(yīng)一個(gè)系統(tǒng)調(diào)用。用戶進(jìn)程利用系統(tǒng)調(diào)用對(duì)設(shè)備文件進(jìn)行諸如讀和寫等操作時(shí),系統(tǒng)調(diào)用通過設(shè)備文件的主設(shè)備號(hào)找到相應(yīng)的設(shè)備驅(qū)動(dòng)程序,并調(diào)用相應(yīng)的驅(qū)動(dòng)程序函數(shù)。file_operations結(jié)構(gòu)定義如下:

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?*); 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?*); ? int?(*flush)?(struct?file?*,?fl_owner_t?id); int?(*release)?(struct?inode?*,?struct?file?*); ? int?(*fsync)?(struct?file?*,?struct?dentry?*,?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?**); ? };?struct module *owner第一個(gè) file_operations 成員根本不是一個(gè)操作; 它是一個(gè)指向擁有這個(gè)結(jié)構(gòu)的模塊的指針. 這個(gè)成員用來在它的操作還在被使用時(shí)阻止模塊被卸載. 幾乎所有時(shí)間中, 它被簡(jiǎn)單初始化為 THIS_MODULE, 一個(gè)在 <linux/module.h> 中定義的宏.loff_t (*llseek) (struct file *, loff_t, int);llseek 方法用作改變文件中的當(dāng)前讀/寫位置, 并且新位置作為(正的)返回值. loff_t 參數(shù)是一個(gè)"long offset", 并且就算在 32位平臺(tái)上也至少 64 位寬. 錯(cuò)誤由一個(gè)負(fù)返回值指示. 如果這個(gè)函數(shù)指針是 NULL, seek 調(diào)用會(huì)以潛在地?zé)o法預(yù)知的方式修改 file 結(jié)構(gòu)中的位置計(jì)數(shù)器( 在"file 結(jié)構(gòu)" 一節(jié)中描述).ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);用來從設(shè)備中獲取數(shù)據(jù). 在這個(gè)位置的一個(gè)空指針導(dǎo)致 read 系統(tǒng)調(diào)用以 -EINVAL("Invalid argument") 失敗. 一個(gè)非負(fù)返回值代表了成功讀取的字節(jié)數(shù)( 返回值是一個(gè) "signed size" 類型, 常常是目標(biāo)平臺(tái)本地的整數(shù)類型).ssize_t (*aio_read)(struct kiocb *, char __user *, size_t, loff_t);初始化一個(gè)異步讀 -- 可能在函數(shù)返回前不結(jié)束的讀操作. 如果這個(gè)方法是 NULL, 所有的操作會(huì)由 read 代替進(jìn)行(同步地).ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);發(fā)送數(shù)據(jù)給設(shè)備. 如果 NULL, -EINVAL 返回給調(diào)用 write 系統(tǒng)調(diào)用的程序. 如果非負(fù), 返回值代表成功寫的字節(jié)數(shù).ssize_t (*aio_write)(struct kiocb *, const char __user *, size_t, loff_t *);初始化設(shè)備上的一個(gè)異步寫.int (*readdir) (struct file *, void *, filldir_t);對(duì)于設(shè)備文件這個(gè)成員應(yīng)當(dāng)為 NULL; 它用來讀取目錄, 并且僅對(duì)文件系統(tǒng)有用.unsigned int (*poll) (struct file *, struct poll_table_struct *);poll 方法是 3 個(gè)系統(tǒng)調(diào)用的后端: poll, epoll, 和 select, 都用作查詢對(duì)一個(gè)或多個(gè)文件描述符的讀或?qū)懯欠駮?huì)阻塞. poll 方法應(yīng)當(dāng)返回一個(gè)位掩碼指示是否非阻塞的讀或?qū)懯强赡艿? 并且, 可能地, 提供給內(nèi)核信息用來使調(diào)用進(jìn)程睡眠直到 I/O 變?yōu)榭赡? 如果一個(gè)驅(qū)動(dòng)的 poll 方法為 NULL, 設(shè)備假定為不阻塞地可讀可寫.int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);ioctl 系統(tǒng)調(diào)用提供了發(fā)出設(shè)備特定命令的方法(例如格式化軟盤的一個(gè)磁道, 這不是讀也不是寫). 另外, 幾個(gè) ioctl 命令被內(nèi)核識(shí)別而不必引用 fops 表. 如果設(shè)備不提供 ioctl 方法, 對(duì)于任何未事先定義的請(qǐng)求(-ENOTTY, "設(shè)備無這樣的 ioctl"), 系統(tǒng)調(diào)用返回一個(gè)錯(cuò)誤.int (*mmap) (struct file *, struct vm_area_struct *);mmap 用來請(qǐng)求將設(shè)備內(nèi)存映射到進(jìn)程的地址空間. 如果這個(gè)方法是 NULL, mmap 系統(tǒng)調(diào)用返回 -ENODEV.int (*open) (struct inode *, struct file *);盡管這常常是對(duì)設(shè)備文件進(jìn)行的第一個(gè)操作, 不要求驅(qū)動(dòng)聲明一個(gè)對(duì)應(yīng)的方法. 如果這個(gè)項(xiàng)是 NULL, 設(shè)備打開一直成功, 但是你的驅(qū)動(dòng)不會(huì)得到通知.int (*flush) (struct file *);flush 操作在進(jìn)程關(guān)閉它的設(shè)備文件描述符的拷貝時(shí)調(diào)用; 它應(yīng)當(dāng)執(zhí)行(并且等待)設(shè)備的任何未完成的操作. 這個(gè)必須不要和用戶查詢請(qǐng)求的 fsync 操作混淆了. 當(dāng)前, flush 在很少驅(qū)動(dòng)中使用; SCSI 磁帶驅(qū)動(dòng)使用它, 例如, 為確保所有寫的數(shù)據(jù)在設(shè)備關(guān)閉前寫到磁帶上. 如果 flush 為 NULL, 內(nèi)核簡(jiǎn)單地忽略用戶應(yīng)用程序的請(qǐng)求.int (*release) (struct inode *, struct file *);在文件結(jié)構(gòu)被釋放時(shí)引用這個(gè)操作. 如同 open, release 可以為 NULL.int (*fsync) (struct file *, struct dentry *, int);這個(gè)方法是 fsync 系統(tǒng)調(diào)用的后端, 用戶調(diào)用來刷新任何掛著的數(shù)據(jù). 如果這個(gè)指針是 NULL, 系統(tǒng)調(diào)用返回 -EINVAL.int (*aio_fsync)(struct kiocb *, int);這是 fsync 方法的異步版本.int (*fasync) (int, struct file *, int);這個(gè)操作用來通知設(shè)備它的 FASYNC 標(biāo)志的改變. 異步通知是一個(gè)高級(jí)的主題, 在第 6 章中描述. 這個(gè)成員可以是NULL 如果驅(qū)動(dòng)不支持異步通知.int (*lock) (struct file *, int, struct file_lock *);lock 方法用來實(shí)現(xiàn)文件加鎖; 加鎖對(duì)常規(guī)文件是必不可少的特性, 但是設(shè)備驅(qū)動(dòng)幾乎從不實(shí)現(xiàn)它.ssize_t (*readv) (struct file *, const struct iovec *, unsigned long, loff_t *);ssize_t (*writev) (struct file *, const struct iovec *, unsigned long, loff_t *);這些方法實(shí)現(xiàn)發(fā)散/匯聚讀和寫操作. 應(yīng)用程序偶爾需要做一個(gè)包含多個(gè)內(nèi)存區(qū)的單個(gè)讀或?qū)懖僮? 這些系統(tǒng)調(diào)用允許它們這樣做而不必對(duì)數(shù)據(jù)進(jìn)行額外拷貝. 如果這些函數(shù)指針為 NULL, read 和 write 方法被調(diào)用( 可能多于一次 ).ssize_t (*sendfile)(struct file *, loff_t *, size_t, read_actor_t, void *);這個(gè)方法實(shí)現(xiàn) sendfile 系統(tǒng)調(diào)用的讀, 使用最少的拷貝從一個(gè)文件描述符搬移數(shù)據(jù)到另一個(gè). 例如, 它被一個(gè)需要發(fā)送文件內(nèi)容到一個(gè)網(wǎng)絡(luò)連接的 web 服務(wù)器使用. 設(shè)備驅(qū)動(dòng)常常使 sendfile 為 NULL.ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);sendpage 是 sendfile 的另一半; 它由內(nèi)核調(diào)用來發(fā)送數(shù)據(jù), 一次一頁, 到對(duì)應(yīng)的文件. 設(shè)備驅(qū)動(dòng)實(shí)際上不實(shí)現(xiàn) sendpage.unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);這個(gè)方法的目的是在進(jìn)程的地址空間找一個(gè)合適的位置來映射在底層設(shè)備上的內(nèi)存段中. 這個(gè)任務(wù)通常由內(nèi)存管理代碼進(jìn)行; 這個(gè)方法存在為了使驅(qū)動(dòng)能強(qiáng)制特殊設(shè)備可能有的任何的對(duì)齊請(qǐng)求. 大部分驅(qū)動(dòng)可以置這個(gè)方法為 NULL.[10]int (*check_flags)(int)這個(gè)方法允許模塊檢查傳遞給 fnctl(F_SETFL...) 調(diào)用的標(biāo)志.int (*dir_notify)(struct file *, unsigned long);這個(gè)方法在應(yīng)用程序使用 fcntl 來請(qǐng)求目錄改變通知時(shí)調(diào)用. 只對(duì)文件系統(tǒng)有用; 驅(qū)動(dòng)不需要實(shí)現(xiàn) dir_notify.
  • scull 設(shè)備驅(qū)動(dòng)只實(shí)現(xiàn)最重要的設(shè)備方法. 它的 file_operations 結(jié)構(gòu)是如下初始化的:

    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, };

    其中struct file代表一個(gè)打開的文件,在執(zhí)行file_operation中的open操作時(shí)被創(chuàng)建。假設(shè)驅(qū)動(dòng)程序中定義的file_operation是fops,圖1-4是應(yīng)用層系統(tǒng)調(diào)用與驅(qū)動(dòng)層fops的調(diào)用關(guān)系圖。
    ?
    (點(diǎn)擊查看大圖)圖1-4? 應(yīng)用層與驅(qū)動(dòng)層的調(diào)用關(guān)系
    struct inode被內(nèi)核用來表示一個(gè)文件結(jié)點(diǎn)。文件結(jié)點(diǎn)的操作結(jié)構(gòu)定義如下:
  • struct?inode_operations?{ ?
  • ????int?(*create)?(struct?inode?*,struct?dentry?*,int,?struct?nameidata?*); ?
  • ????struct?dentry?*?(*lookup)?(struct?inode?*,struct?dentry?*,?struct?nameidata?*); ?
  • ????int?(*link)?(struct?dentry?*,struct?inode?*,struct?dentry?*); ?
  • ????int?(*unlink)?(struct?inode?*,struct?dentry?*); ?
  • ????int?(*symlink)?(struct?inode?*,struct?dentry?*,const?char?*);//創(chuàng)建軟鏈接 ?
  • ????int?(*mkdir)?(struct?inode?*,struct?dentry?*,int);//創(chuàng)建目錄 ?
  • ????int?(*rmdir)?(struct?inode?*,struct?dentry?*);//刪除結(jié)點(diǎn) ?
  • ????int?(*mknod)?(struct?inode?*,struct?dentry?*,int,dev_t);//創(chuàng)建結(jié)點(diǎn) ?
  • ????int?(*rename)?(struct?inode?*,?struct?dentry?*,struct?inode?*,?struct?dentry?*); ?
  • ????int?(*readlink)?(struct?dentry?*,?char?__user?*,int); ?
  • ????void?*?(*follow_link)?(struct?dentry?*,?struct?nameidata?*); ?
  • ????void?(*put_link)?(struct?dentry?*,?struct?nameidata?*,?void?*); ?
  • ????void?(*truncate)?(struct?inode?*); ?
  • ????int?(*permission)?(struct?inode?*,?int);//權(quán)限管理 ?
  • ????int?(*check_acl)(struct?inode?*,?int); ?
  • ????int?(*setattr)?(struct?dentry?*,?struct?iattr?*); ?
  • ????int?(*getattr)?(struct?vfsmount?*mnt,?struct?dentry?*,?struct?kstat?*); ?
  • ????int?(*setxattr)?(struct?dentry?*,?const?char?*,const?void?*,size_t,int); ?
  • ????ssize_t?(*getxattr)?(struct?dentry?*,?const?char?*,?void?*,?size_t); ?
  • ????ssize_t?(*listxattr)?(struct?dentry?*,?char?*,?size_t); ?
  • ????int?(*removexattr)?(struct?dentry?*,?const?char?*); ?
  • ????void?(*truncate_range)(struct?inode?*,?loff_t,?loff_t); ?
  • ????long?(*fallocate)(struct?inode?*inode,?int?mode,?loff_t?offset,loff_t?len); ?
  • ????int?(*fiemap)(struct?inode?*,?struct?fiemap_extent_info?*,?u64?start,u64?len); ?
  • }; ?
  • 總結(jié)

    以上是生活随笔為你收集整理的file_operations结构的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。