2012-07-20 135 views
0

我寫了一個簡單的字符設備驅動程序(mydev),其中包含「打開」文件操作。sys_open是如何工作的?

在用戶空間應用程序中,我打開此驅動程序節點。使用open(「/ dev/mydev」,O_RDONLY); open()系統調用在內部調用sys_open()。

我只想知道sys_open()函數如何調用我的驅動程序的打開文件操作。 VFS如何處理這個問題,它在內部調用函數。

回答

1

我發現在瞭解Linux內核的書的答案,在部分12.5.1

操作步驟,

  1. 調用的getName()來讀取進程的地址空間中的文件路徑名。

  2. 調用get_unused_fd()在current-> files-> fd中查找一個空插槽。 對應的索引(新文件描述符)存儲在fd局部變量中。

  3. 調用filp_open()函數,以參數的形式傳遞路徑名,訪問模式標誌和權限位掩碼 。該功能依次執行以下步驟: 步驟:

    a。調用get_empty_filp()來獲得一個新的文件對象。

    b。根據標誌和模式參數值 設置文件對象的f_flags和f_mode字段。

    c。調用open_namei(),執行以下操作:

    i. Invokes lookup_dentry() to interpret the file pathname and gets the 
         dentry object associated with the requested file. 
    
        ii. Performs a series of checks to verify whether the process is permitted 
         to open the file as specified by the values of the flags parameter. If so, 
         returns the address of the dentry object; otherwise, returns an error code. 
    

    d。如果訪問權限用於寫入,則檢查 inode對象的i_writecount字段的值。負值表示該文件已被內存映射, 指定必須拒絕寫入訪問(請參閱章節15.2中的第15.2節)。在這種情況下,返回一個錯誤代碼。任何其他值都指定實際寫入文件的進程數量爲 。在後一種情況下, 遞增計數器。

    e。初始化文件對象的字段;特別是將f_op字段設置爲inode對象的i_op-> default_file_ops字段的內容。這將爲將來的文件操作設置所有正確的功能。 f。如果定義了(默認)文件操作的打開方法,則調用它。 g。清除f_flags中的O_CREAT,O_EXCL,O_NOCTTY和O_TRUNC標誌。 h。返回文件對象的地址。

  4. 將current-> files-> fd [fd]設置爲文件對象的地址。
  5. 返回fd。