file_operations中各項解析

來源:互聯網
上載者:User
linux裝置驅動中file_operations結構體分析 

struct module *owner

第一個 file_operations 成員根本不是一個操作; 它是一個指向擁有這個結構的模組的指標. 這個成員用來在它的操作還在被使用時阻止模組被卸載. 幾乎所有時間中, 它被簡單初始化為 THIS_MODULE, 一個在 <linux/module.h> 中定義的宏.

loff_t (*llseek) (struct file *, loff_t, int);

llseek 方法用作改變檔案中的當前讀/寫位置, 並且新位置作為(正的)傳回值. loff_t 參數是一個"long offset", 並且就算在 32位平台上也至少 64 位元寬. 錯誤由一個負傳回值指示. 如果這個函數指標是 NULL, seek 調用會以潛在地無法預知的方式修改 file 結構中的位置計數器( 在"file 結構" 一節中描述).

ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);

用來從裝置中擷取資料. 在這個位置的一個null 指標導致 read 系統調用以 -EINVAL("Invalid argument") 失敗. 一個非負傳回值代表了成功讀取的位元組數( 傳回值是一個 "signed size" 類型, 常常是目標平台本地的整數類型).

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

發送資料給裝置. 如果 NULL, -EINVAL 返回給調用 write 系統調用的程式. 如果非負, 傳回值代表成功寫的位元組數.

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

poll 方法是 3 個系統調用的後端: poll, epoll, 和 select, 都用作查詢對一個或多個檔案描述符的讀或寫是否會阻塞. poll 方法應當返回一個位元遮罩指示是否非阻塞的讀或寫是可能的, 並且, 可能地, 提供給核心資訊用來使調用進程睡眠直到 I/O 變為可能. 如果一個驅動的 poll 方法為 NULL, 裝置假定為不阻塞地可讀可寫.

int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);

ioctl 系統調用提供了發出裝置特定命令的方法(例如格式化磁碟片的一個磁軌, 這不是讀也不是寫). 另外, 幾個 ioctl 命令被核心識別而不必引用 fops 表. 如果裝置不提供 ioctl 方法, 對於任何未事先定義的請求(-ENOTTY, "裝置無這樣的 ioctl"), 系統調用返回一個錯誤.

int (*mmap) (struct file *, struct vm_area_struct *);

mmap 用來請求將裝置記憶體映射到進程的地址空間. 如果這個方法是 NULL, mmap 系統調用返回 -ENODEV.

int (*open) (struct inode *, struct file *);

儘管這常常是對裝置檔案進行的第一個操作, 不要求驅動聲明一個對應的方法. 如果這個項是 NULL, 裝置開啟一直成功, 但是你的驅動不會得到通知.

int (*flush) (struct file *);

flush 操作在進程關閉它的裝置檔案描述符的拷貝時調用; 它應當執行(並且等待)裝置的任何未完成的操作. 這個必須不要和使用者查詢請求的 fsync 操作混淆了. 當前, flush 在很少驅動中使用; SCSI 磁帶驅動使用它, 例如, 為確保所有寫的資料在裝置關閉前寫到磁帶上. 如果 flush 為 NULL, 核心簡單地忽略使用者應用程式的請求.

int (*release) (struct inode *, struct file *);

在檔案結構被釋放時引用這個操作. 如同 open, release 可以為 NULL.

int (*fsync) (struct file *, struct dentry *, int);

這個方法是 fsync 系統調用的後端, 使用者調用來重新整理任何掛著的資料. 如果這個指標是 NULL, 系統調用返回 -EINVAL.

int (*aio_fsync)(struct kiocb *, int);

這是 fsync 方法的非同步版本.

int (*fasync) (int, struct file *, int);

這個操作用來通知裝置它的 FASYNC 標誌的改變. 非同步通知是一個進階的主題, 在第 6 章中描述. 這個成員可以是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 *);

這些方法實現發散/匯聚讀和寫操作. 應用程式偶爾需要做一個包含多個記憶體區的單個讀或寫操作; 這些系統調用允許它們這樣做而不必對資料進行額外拷貝. 如果這些函數指標為 NULL, read 和 write 方法被調用( 可能多於一次 ).

ssize_t (*sendfile)(struct file *, loff_t *, size_t, read_actor_t, void *);

這個方法實現 sendfile 系統調用的讀, 使用最少的拷貝從一個檔案描述符搬移資料到另一個. 例如, 它被一個需要傳送檔案內容到一個網路連接的 網頁伺服器使用. 裝置驅動常常使 sendfile 為 NULL.

ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);

sendpage 是 sendfile 的另一半; 它由核心調用來發送資料, 一次一頁, 到對應的檔案. 裝置驅動實際上不實現 sendpage.

unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);

這個方法的目的是在進程的地址空間找一個合適的位置來映射在底層裝置上的記憶體段中. 這個任務通常由記憶體管理代碼進行; 這個方法存在為了使驅動能強制特殊裝置可能有的任何的對齊請求. 大部分驅動可以置這個方法為 NULL.[10]

int (*check_flags)(int)

這個方法允許模組檢查傳遞給 fnctl(F_SETFL...) 調用的標誌.

int (*dir_notify)(struct file *, unsigned long);

這個方法在應用程式使用 fcntl 來請求目錄改變通知時調用. 只對檔案系統有用; 驅動不需要實現 dir_notify.

scull 裝置驅動只實現最重要的裝置方法. 它的 file_operations 結構是如下初始化的:

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.