Linux常用文件結構體

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

  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

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

  };

 2、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;     /* used by d_revalidate */

  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];      存放短文件名

  };

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;

  };
 4、索引節點對象由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;
};

 5、struct file_operations {
 struct module *owner;
 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 *);
 ssize_t (*aio_read) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
 ssize_t (*aio_write) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
 int (*readdir) (struct file *, void *, filldir_t);
 unsigned int (*poll) (struct file *, struct poll_table_struct *);
 int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);
 long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
 long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
 int (*mmap) (struct file *, struct vm_area_struct *);
 int (*open) (struct inode *, struct file *);
 int (*flush) (struct file *, fl_owner_t id);
 int (*release) (struct inode *, struct file *);
 int (*fsync) (struct file *, struct dentry *, int datasync);
 int (*aio_fsync) (struct kiocb *, int datasync);
 int (*fasync) (int, struct file *, int);
 int (*lock) (struct file *, int, struct file_lock *);
 ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
 unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
 int (*check_flags)(int);
 int (*flock) (struct file *, int, struct file_lock *);
 ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
 ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
 int (*setlease)(struct file *, long, struct file_lock **);
};

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章