file結構體和inode結構體

來源:互聯網
上載者:User

(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)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; /* 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)索引節點對象由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;
};

      inode 譯成中文就是索引節點。每個存放裝置或存放裝置的分區(存放裝置是硬碟、磁碟片、隨身碟 ... ...
)被格式化為檔案系統後,應該有兩部份,一部份是inode,另一部份是Block,Block是用來儲存資料用的。而inode呢,就是用來儲存這些數
據的資訊,這些資訊包括檔案大小、屬主、歸屬的使用者組、讀寫權限等。inode為每個檔案進行資訊索引,所以就有了inode的數值。作業系統根據指令,
能通過inode值最快的找到相對應的檔案。
      做個比喻,比如一本書,存放裝置或分區就相當於這本書,Block相當於書中的每一頁,inode 就相當於這本書前面的目錄,一本書有很多的內容,如果想尋找某部份的內容,我們可以先查目錄,通過目錄能最快的找到我們想要看的內容。
      當我們用ls 查看某個目錄或檔案時,如果加上-i 參數,就可以看到inode節點了;比如ls -li lsfile.sh ,最前面的數值就是inode資訊。

參考原文:http://blog.csdn.net/sealyao/archive/2009/10/11/4626875.aspx

參考原文:http://blog.csdn.net/wangdeng1314/archive/2009/06/10/4254132.aspx

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.