linux中什么是文件結構體?


struct file結構體定義在include/linux/fs.h中定義。文件結構體代表一個打開的文件,系統中的每個打開的文件在內核空間都有一個關聯的 struct file。它由內核在打開文件時創建,並傳遞給在文件上進行操作的任何函數。在文件的所有實例都關閉后,內核釋放這個數據結構。在內核創建和驅動源碼中,struct file的指針通常被命名為file或filp。

 

struct file 的最重要成員在這展示. 1.mode_t f_mode;       文件模式確定文件是可讀的或者是可寫的(或者都是), 通過位 FMODE_READ 和FMODE_WRITE. 你可能想在你的 open 或者 ioctl 函數中檢查這個成員的讀寫許可, 但是不需要檢查讀寫許可, 因為內核在調用你的方法之前檢查. 當文件還沒有為那種存取而打開時讀或寫的企圖被拒絕, 驅動甚至不知道這個情況. 2.loff_t f_pos;       當前讀寫位置. loff_t 在所有平台都是 64 位( 在 gcc 術語里是 long long ). 驅動可以讀這個值,如果它需要知道文件中的當前位置, 但是正常地不應該改變它; 讀和寫應當使用它們作為最后參數而收到的指針來更新一個位置, 代替直接作用於 filp->f_pos. 這個規則的一個例外是在 llseek 方法中, 它的目的就是改變文件位置. 3.unsigned int f_flags;       這些是文件標志, 例如 O_RDONLY, O_NONBLOCK, 和 O_SYNC. 驅動應當檢查O_NONBLOCK 標志來看是否是請求非阻塞操作; 其他標志很少使用. 特別地, 應當檢查讀/寫許可, 使用 f_mode 而不是f_flags. 所有的標志在頭文件<linux/fcntl.h> 中定義. 4.struct file_operations *f_op;       和文件關聯的操作. 內核安排指針作為它的open 實現的一部分, 接着讀取它當它需要分派任何的操作時. filp->f_op 中的值從不由內核保存為后面的引用; 這意味着你可改變你的文件關聯的文件操作, 在你返回調用者之后新方法會起作用. 例如, 關聯到主編號 1 (/dev/null, /dev/zero, 等等)的 open 代碼根據打開的次編號來替代 filp->f_op 中的操作. 這個做法允許實現幾種行為, 在同一個主編號下而不必在每個系統調用中引入開銷. 替換文件操作的能力是面向 對象編程的"方法重載"的內核對等體. 5.void *private_data;       open 系統調用設置這個指針為 NULL, 在為驅動調用 open 方法之前. 你可自由使用這個成員或者忽略它; 你可以使用這個成員來指向分配的數據, 但是接着你必須記住在內核銷毀文件結構之前, 在 release 方法中釋放那個內存. private_data 是一個有用的資源, 在系統調用間保留狀態信息, 我們大部分例子模塊都使用它. 6.struct dentry *f_dentry; 關聯到文件的目錄入口( dentry )結構. 設備驅動編寫者正常地不需要關心 dentry 結構, 除了作為 filp->f_dentry->d_inode 存取 inode 結構.

 

file 結構如下所示:   struct file {   union {   struct list_head fu_list; 文件對象鏈表指針linux/include/linux/list.h   struct rcu_head fu_rcuhead; RCU(Read-Copy Update)是Linux 2.6內核中新的鎖機制   } f_u;   struct path f_path; 包含dentry和mnt兩個成員,用於確定文件路徑   #define f_dentry f_path.dentry f_path的成員之一,當前文件的dentry結構   #define f_vfsmnt f_path.mnt 表示當前文件所在文件系統的掛載根目錄   const struct file_operations *f_op; 與該文件相關聯的操作函數   atomic_t f_count; 文件的引用計數(有多少進程打開該文件)   unsigned int f_flags; 對應於open時指定的flag   mode_t f_mode; 讀寫模式:open的mod_t mode參數   off_t f_pos; 該文件在當前進程中的文件偏移量   struct fown_struct f_owner; 該結構的作用是通過信號進行I/O時間通知的數據。   unsigned int f_uid, f_gid; 文件所有者id,所有者組id   struct file_ra_state f_ra; 在linux/include/linux/fs.h中定義,文件預讀相關   unsigned long f_version;   #ifdef CONFIG_SECURITY   void *f_security;   #endif      void *private_data;   #ifdef CONFIG_EPOLL      struct list_head f_ep_links;   spinlock_t f_ep_lock;   #endif   struct address_space *f_mapping;   };   1.2 struct dentry   dentry的中文名稱是目錄項,是Linux文件系統中某個索引節點(inode)的鏈接。這個索引節點可以是文件,也可以是目錄。 inode(可理解為ext2 inode)對應於物理磁盤上的具體對象,dentry是一個內存實體,其中的d_inode成員指向對應的inode。也就是說,一個inode可以在運行的時候鏈接多個dentry,而d_count記錄了這個鏈接的數量。   struct dentry {   atomic_t d_count; 目錄項對象使用計數器,可以有未使用態,使用態和負狀態   unsigned int d_flags; 目錄項標志   struct inode * d_inode; 與文件名關聯的索引節點   struct dentry * d_parent; 父目錄的目錄項對象   struct list_head d_hash; 散列表表項的指針   struct list_head d_lru; 未使用鏈表的指針   struct list_head d_child; 父目錄中目錄項對象的鏈表的指針   struct list_head d_subdirs; 對目錄而言,表示子目錄目錄項對象的鏈表   struct list_head d_alias; 相關索引節點(別名)的鏈表   int d_mounted; 對於安裝點而言,表示被安裝文件系統根項   struct qstr d_name; 文件名   unsigned long d_time;   struct dentry_operations *d_op; 目錄項方法   struct super_block * d_sb; 文件的超級塊對象   vunsigned long d_vfs_flags;   void * d_fsdata; 與文件系統相關的數據   unsigned char d_iname [DNAME_INLINE_LEN]; 存放短文件名   };   1.3 struct files_struct   對於每個進程,包含一個files_struct結構,用來記錄文件描述符的使用情況,定義在include/linux/file.h中   struct files_struct   {   atomic_t count; 使用該表的進程數   struct fdtable *fdt;   struct fdtable fdtab;   spinlock_t file_lock ____cacheline_aligned_in_smp;   int next_fd; 數值最小的最近關閉文件的文件描述符,下一個可用的文件描述符   struct embedded_fd_set close_on_exec_init; 執行exec時需要關閉的文件描述符初值集合   struct embedded_fd_set open_fds_init; 文件描述符的屏蔽字初值集合   struct file * fd_array[NR_OPEN_DEFAULT]; 默認打開的fd隊列   };   struct fdtable {   unsigned int max_fds;   struct file ** fd; 指向打開的文件描述符列表的指針,開始的時候指向fd_array,   當超過max_fds時,重新分配地址   fd_set *close_on_exec; 執行exec需要關閉的文件描述符位圖(fork,exec即不被子進程繼承的文件   描述符)   fd_set *open_fds; 打開的文件描述符位圖   struct rcu_head rcu;   struct fdtable *next;   };   1.4 struct fs_struct   struct fs_struct {   atomic_t count; 計數器   rwlock_t lock; 讀寫鎖   int umask;   struct dentry * root, * pwd, * altroot;根目錄("/"),當前目錄以及替換根目錄   struct vfsmount * rootmnt, * pwdmnt, * altrootmnt;   };   1.5 struct inode   索引節點對象由inode結構體表示,定義文件在linux/fs.h中。   struct inode {   struct hlist_node i_hash; 哈希表   struct list_head i_list; 索引節點鏈表   struct list_head i_dentry; 目錄項鏈表   unsigned long i_ino; 節點號   atomic_t i_count; 引用記數   umode_t i_mode; 訪問權限控制   unsigned int i_nlink; 硬鏈接數   uid_t i_uid; 使用者id   gid_t i_gid; 使用者id組   kdev_t i_rdev; 實設備標識符   loff_t i_size; 以字節為單位的文件大小   struct timespec i_atime; 最后訪問時間   struct timespec i_mtime; 最后修改(modify)時間   struct timespec i_ctime; 最后改變(change)時間   unsigned int i_blkbits; 以位為單位的塊大小   unsigned long i_blksize; 以字節為單位的塊大小   unsigned long i_version; 版本號   unsigned long i_blocks; 文件的塊數   unsigned short i_bytes; 使用的字節數   spinlock_t i_lock; 自旋鎖   struct rw_semaphore i_alloc_sem; 索引節點信號量   struct inode_operations *i_op; 索引節點操作表   struct file_operations *i_fop; 默認的索引節點操作   struct super_block *i_sb; 相關的超級塊   struct file_lock *i_flock; 文件鎖鏈表   struct address_space *i_mapping; 相關的地址映射   struct address_space i_data; 設備地址映射   struct dquot *i_dquot[MAXQUOTAS];節點的磁盤限額   struct list_head i_devices; 塊設備鏈表   struct pipe_inode_info *i_pipe; 管道信息   struct block_device *i_bdev; 塊設備驅動   unsigned long i_dnotify_mask;目錄通知掩碼   struct dnotify_struct *i_dnotify; 目錄通知   unsigned long i_state; 狀態標志   unsigned long dirtied_when;首次修改時間   unsigned int i_flags; 文件系統標志   unsigned char i_sock; 套接字   atomic_t i_writecount; 寫者記數   void *i_security; 安全模塊   __u32 i_generation; 索引節點版本號   union {   void *generic_ip;文件特殊信息   } u;   };

 

1、struct inode──字符設備驅動相關的重要結構介紹

內核中用inode結構表示具體的文件,而用file結構表示打開的文件描述符。Linux2.6.27內核中,inode結構體具體定義如下: struct inode { struct hlist_node    i_hash; struct list_head    i_list; struct list_head    i_sb_list; struct list_head    i_dentry; unsigned long        i_ino; atomic_t        i_count; unsigned int        i_nlink; uid_t            i_uid; gid_t            i_gid;  dev_t            i_rdev;   //該成員表示設備文件的inode結構,它包含了真正的設備編號。 u64            i_version; loff_t            i_size; #ifdef __NEED_I_SIZE_ORDERED seqcount_t        i_size_seqcount; #endif struct timespec        i_atime; struct timespec        i_mtime; struct timespec        i_ctime; unsigned int        i_blkbits; blkcnt_t        i_blocks; unsigned short          i_bytes; umode_t            i_mode; spinlock_t        i_lock;     struct mutex        i_mutex; struct rw_semaphore    i_alloc_sem; const struct inode_operations    *i_op; const struct file_operations    *i_fop;     struct super_block    *i_sb; struct file_lock    *i_flock; struct address_space    *i_mapping; struct address_space    i_data; #ifdef CONFIG_QUOTA struct dquot        *i_dquot[MAXQUOTAS]; #endif struct list_head    i_devices; union { struct pipe_inode_info    *i_pipe; struct block_device    *i_bdev;  struct cdev        *i_cdev; //該成員表示字符設備的內核的 內部結構。當inode指向一個字符設備文件時,該成員包含了指向struct cdev結構的指針,其中cdev結構是字符設備結構體。 }; int            i_cindex;
__u32            i_generation;
#ifdef CONFIG_DNOTIFY unsigned long        i_dnotify_mask; struct dnotify_struct    *i_dnotify; #endif
#ifdef CONFIG_INOTIFY struct list_head    inotify_watches; struct mutex        inotify_mutex;     #endif
unsigned long        i_state; unsigned long        dirtied_when;    
unsigned int        i_flags;
atomic_t        i_writecount; #ifdef CONFIG_SECURITY void            *i_security; #endif void            *i_private; };

  我們在進程中打開一個文件F,實際上就是要在內存中建立F的dentry,和inode結構,並讓它們與進程結構聯系來,把VFS中定義的接口給接起來。我們來看一看這個經典的圖:

file結構體詳解


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM