Linux struct file結構

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函數對文

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

本文來自ChinaUnix博客,如果查看原文請點:http://blog.chinaunix.net/u2/70379/showart_1134191.html


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;

本文來自ChinaUnix博客,如果查看原文請點:http://blog.chinaunix.net/u2/70379/showart_1134191.html

件內容進行讀取。
四、
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;

本文來自ChinaUnix博客,如果查看原文請點:http://blog.chinaunix.net/u2/70379/showart_1134191.html

發佈了6 篇原創文章 · 獲贊 9 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章