1. 程式人生 > >檔案結構體struct file(Linux 2.6.23核心) (轉)

檔案結構體struct file(Linux 2.6.23核心) (轉)

struct file結構體定義在/linux/include/linux/fs.h(Linux 2.6.11核心)中,其原型是:
struct file {
        /*
         * fu_list becomes invalid after file_free is called and queued via
         * fu_rcuhead for RCU freeing
         */
        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_t                f_count;
        unsigned int            f_flags;
        mode_t                  f_mode;
        loff_t                  f_pos;
        struct fown_struct      f_owner;
        unsigned int            f_uid, f_gid;
        struct file_ra_state    f_ra;
        unsigned long           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;
};
檔案結構體代表一個開啟的檔案,系統中的每個開啟的檔案在核心空間都有一個關聯的struct file。它由核心在開啟檔案時建立,並傳遞給在檔案上進行操作的任何函式。在檔案的所有例項都關閉後,核心釋放這個資料結構。在核心建立和驅動原始碼中,struct file的指標通常被命名為file或filp。一下是對結構中的每個資料成員的解釋:
一、
union {
    struct list_head fu_list;
    struct rcu_head rcuhead;
}f_u;
其中的struct list_head定義在 linux/include/linux/list.h中,原型為:
struct list_head {
        struct list_head *next, *prev;
};
用於通用檔案物件連結串列的指標。
struct rcu_head定義在linux/include/linux/rcupdate.h中,其原型為:
/**
* struct rcu_head - callback structure for use with RCU
* @next: next update requests in a list
* @func: actual update function to call after the grace period.
*/
struct rcu_head {
        struct rcu_head *next;
        void (*func)(struct rcu_head *head);
};
RCU(Read-Copy Update)是Linux 2.6核心中新的鎖機制,具體在這裡有介紹:
http://www.ibm.com/developerworks/cn/linux/l-rcu/
二、
struct path             f_path;
被定義在linux/include/linux/namei.h中,其原型為:
struct path {
        struct vfsmount *mnt;
        struct dentry *dentry;
};
在早些版本的核心中並沒有此結構,而是直接將path的兩個資料成員作為struct file的資料成員,
struct vfsmount *mnt的作用是指出該檔案的已安裝的檔案系統,
struct dentry *dentry是與檔案相關的目錄項物件。
三、
const struct file_operations    *f_op;
被定義在linux/include/linux/fs.h中,其中包含著與檔案關聯的操作,如:
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 *);
等。當開啟一個檔案時,核心就建立一個與該檔案相關聯的struct file結構,其中的*f_op就指向的是
具體對該檔案進行操作的函式。例如使用者呼叫系統呼叫read來讀取該檔案的內容時,那麼系統呼叫read最終會陷入核心呼叫sys_read函式,而sys_read最終會調用於該檔案關聯的struct file結構中的f_op->read函式對檔案內容進行讀取。
四、
atomic_t                f_count;
atomic_t被定義為:
typedef struct { volatile int counter; } atomic_t;
volatile修飾字段告訴gcc不要對該型別的資料做優化處理,對它的訪問都是對記憶體的訪問,而不是對暫存器的訪問。
本質是int型別,之所以這樣寫是讓編譯器對基於該型別變數的操作進行嚴格的型別檢查。此處f_count的作用是記錄對檔案物件的引用計數,也即當前有多少個程序在使用該檔案。
五、
unsigned int            f_flags;
當開啟檔案時指定的標誌,對應系統呼叫open的int flags引數。驅動程式為了支援非阻塞型操作需要檢查這個標誌。
六、
mode_t                  f_mode;
對檔案的讀寫模式,對應系統呼叫open的mod_t mode引數。如果驅動程式需要這個值,可以直接讀取這個欄位。
mod_t被定義為:
typedef unsigned int __kernel_mode_t;
typedef __kernel_mode_t         mode_t;
七、
loff_t                  f_pos;
當前的檔案指標位置,即檔案的讀寫位置。
loff_t被定義為:
typedef long long       __kernel_loff_t;
typedef __kernel_loff_t         loff_t;
八、
struct fown_struct      f_owner;
struct fown_struct在linux/include/linux/fs.h被定義,原型為:
struct fown_struct {
        rwlock_t lock;          /* protects pid, uid, euid fields */
        struct pid *pid;        /* pid or -pgrp where SIGIO should be sent */
        enum pid_type pid_type; /* Kind of process group SIGIO should be sent to */
        uid_t uid, euid;        /* uid/euid of process setting the owner */
        int signum;             /* posix.1b rt signal to be delivered on IO */
};
該結構的作用是通過訊號進行I/O時間通知的資料。
九、
unsigned int            f_uid, f_gid;
標識檔案的所有者id,所有者所在組的id.
十、
struct file_ra_state    f_ra;
struct file_ra_state結構被定義在/linux/include/linux/fs.h中,原型為:
struct file_ra_state {
        pgoff_t start;                  /* where readahead started */
        unsigned long size;             /* # of readahead pages */
        unsigned long async_size;       /* do asynchronous readahead when
                                           there are only # of pages ahead */
                                          
        unsigned long ra_pages;         /* Maximum readahead window */
        unsigned long mmap_hit;         /* Cache hit stat for mmap accesses */
        unsigned long mmap_miss;        /* Cache miss stat for mmap accesses */
        unsigned long prev_index;       /* Cache last read() position */
        unsigned int prev_offset;       /* Offset where last read() ended in a page */
};
檔案預讀狀態,檔案預讀演算法使用的主要資料結構,當開啟一個檔案時,f_ra中出了perv_page(預設為-1)和ra_apges(對該檔案允許的最大預讀量)這兩個欄位外,其他的所有西端都置為0。
十一、
unsigned long           f_version;
記錄檔案的版本號,每次使用後都自動遞增。
十二、
#ifdef CONFIG_SECURITY
        void                    *f_security;
#endif
此處我的理解是如果在編譯核心時配置了安全措施,那麼struct file結構中就會有void *f_security資料項,用來描述安全措施或者是記錄與安全有關的資訊。
十三、
void *private_data;
系統在呼叫驅動程式的open方法前將這個指標置為NULL。驅動程式可以將這個欄位用於任意目的,也可以忽略這個欄位。驅動程式可以用這個欄位指向已分配的資料,但是一定要在核心釋放file結構前的release方法中清除它。
十四、
#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 */
被用在fs/eventpoll.c來連結所有鉤到這個檔案上。其中f_ep_links是檔案的事件輪詢等待者連結串列的頭,f_ep_lock是保護f_ep_links連結串列的自旋鎖。
十五、struct address_space    *f_mapping;
struct address_space被定義在/linux/include/linux/fs.h中,此處是指向檔案地址空間的指標。
  在驅動開發中,檔案讀/寫模式mode、標誌f_flags都是裝置驅動關心的內容,而私有資料指標private_data在折本驅動中被廣泛使用,大多被指向裝置驅動自定義用於描述裝置的結構體。
驅動程式中常用如下類似的程式碼來檢測使用者開啟檔案的讀寫方式:
if (file->f_mode & FMODE_WRITE) //使用者要求可寫
  {
  }
  if (file->f_mode & FMODE_READ) //使用者要求可讀
  {
  }
下面的程式碼可用於判斷以阻塞還是非阻塞方式開啟裝置檔案:
  if (file->f_flags & O_NONBLOCK) //非阻塞
      pr_debug("open:non-blocking\n");
  else //阻塞
      pr_debug("open:blocking\n");
參考:
Linux裝置驅動開發詳解
深入理解linux核心