struct_inode_和_struct_file The inode structure is used by the kernel internally to represent files. Therefore, it is different from the file structure that represents an open file descriptor. There can be numerous file structures representing multiple open descriptors on a single file, but they all point to a single inode structure. I節(jié)點(diǎn),與文件描述符相然不一樣了, I節(jié)點(diǎn)實(shí)際上指向的內(nèi)容就是物理上的一個(gè)文件在內(nèi)存中的拷貝, 而同一個(gè)物理文件有可能存在多個(gè)文件描述符, 但最終它們都指向同一個(gè)I節(jié)點(diǎn) struct inode 和 struct file 收藏 1、struct inode──字符設(shè)備驅(qū)動(dòng)相關(guān)的重要結(jié)構(gòu)介紹 內(nèi)核中用inode結(jié)構(gòu)表示具體的文件,而用file結(jié)構(gòu)表示打開(kāi)的文件描述符。Linux2.6.27內(nèi)核中,inode結(jié)構(gòu)體具體定義如下: 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; //該成員表示設(shè)備文件的inode結(jié)構(gòu),它包含了真正的設(shè)備編號(hào)。 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; /* i_blocks, i_bytes, maybe i_size */ struct mutex i_mutex; struct rw_semaphore i_alloc_sem; const struct inode_operations *i_op; const struct file_operations *i_fop; /* former ->i_op->default_file_ops */ 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; //該成員表示字符設(shè)備的內(nèi)核的 內(nèi)部結(jié)構(gòu)。當(dāng)inode指向一個(gè)字符設(shè)備文件時(shí),該成員包含了指向struct cdev結(jié)構(gòu)的指針,其中cdev結(jié)構(gòu)是字符設(shè)備結(jié)構(gòu)體。 }; int i_cindex; __u32 i_generation; #ifdef CONFIG_DNOTIFY unsigned long i_dnotify_mask; /* Directory notify events */ struct dnotify_struct *i_dnotify; /* for directory notifications */ #endif #ifdef CONFIG_INOTIFY struct list_head inotify_watches; /* watches on this inode */ struct mutex inotify_mutex; /* protects the watches list */ #endif unsigned long i_state; unsigned long dirtied_when; /* jiffies of first dirtying */ unsigned int i_flags; atomic_t i_writecount; #ifdef CONFIG_SECURITY void *i_security; #endif void *i_private; /* fs or device private pointer */ }; 2、struct file ──字符設(shè)備驅(qū)動(dòng)相關(guān)重要結(jié)構(gòu) 文件結(jié)構(gòu) 代表一個(gè)打開(kāi)的文件描述符,它不是專(zhuān)門(mén)給驅(qū)動(dòng)程序使用的,系統(tǒng)中每一個(gè)打開(kāi)的文件在內(nèi)核中都有一個(gè)關(guān)聯(lián)的struct file。它由內(nèi)核在open時(shí)創(chuàng)建,并傳遞給在文件上操作的任何函數(shù),知道最后關(guān)閉。當(dāng)文件的所有實(shí)例都關(guān)閉之后,內(nèi)核釋放這個(gè)數(shù)據(jù)結(jié)構(gòu)。 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 //該成員是對(duì)應(yīng)的 目錄結(jié)構(gòu) 。 #define f_vfsmnt f_path.mnt const struct file_operations *f_op; //該操作 是定義文件關(guān)聯(lián)的操作的。內(nèi)核在執(zhí)行open時(shí)對(duì)這個(gè) 指針賦值。 atomic_long_t f_count; unsigned int f_flags; //該成員是文件標(biāo)志。 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; u64 f_version; #ifdef CONFIG_SECURITY void *f_security; #endif /* needed for tty driver, and maybe others */ void *private_data;//該成員是系統(tǒng)調(diào)用時(shí)保存狀態(tài)信息非常有用的資源。 #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; #ifdef CONFIG_DEBUG_WRITECOUNT unsigned long f_mnt_write_state; #endif }; ---------------------------------------------------------------------------- file結(jié)構(gòu)體和inode結(jié)構(gòu)體 (1)struct file結(jié)構(gòu)體定義在include/linux/fs.h中定義。文件結(jié)構(gòu)體代表一個(gè)打開(kāi)的文件,系統(tǒng)中的每個(gè)打開(kāi)的文件在內(nèi)核空間都有一個(gè)關(guān)聯(lián)的 struct file。它由內(nèi)核在打開(kāi)文件時(shí)創(chuàng)建,并傳遞給在文件上進(jìn)行操作的任何函數(shù)。在文件的所有實(shí)例都關(guān)閉后,內(nèi)核釋放這個(gè)數(shù)據(jù)結(jié)構(gòu)。在內(nèi)核創(chuàng)建和驅(qū)動(dòng)源碼中,struct file的指針通常被命名為file或filp。如下所示: struct file { union { struct list_head fu_list; 文件對(duì)象鏈表指針linux/include/linux/list.h struct rcu_head fu_rcuhead; RCU(Read-Copy Update)是Linux 2.6內(nèi)核中新的鎖機(jī)制 } f_u; struct path f_path; 包含dentry和mnt兩個(gè)成員,用于確定文件路徑 #define f_dentry f_path.dentry f_path的成員之一,當(dāng)前文件的dentry結(jié)構(gòu) #define f_vfsmnt f_path.mnt 表示當(dāng)前文件所在文件系統(tǒng)的掛載根目錄 const struct file_operations *f_op; 與該文件相關(guān)聯(lián)的操作函數(shù) atomic_t f_count; 文件的引用計(jì)數(shù)(有多少進(jìn)程打開(kāi)該文件) unsigned int f_flags; 對(duì)應(yīng)于open時(shí)指定的flag mode_t f_mode; 讀寫(xiě)模式:open的mod_t mode參數(shù) off_t f_pos; 該文件在當(dāng)前進(jìn)程中的文件偏移量 struct fown_struct f_owner; 該結(jié)構(gòu)的作用是通過(guò)信號(hào)進(jìn)行I/O時(shí)間通知的數(shù)據(jù)。 unsigned int f_uid, f_gid; 文件所有者id,所有者組id struct file_ra_state f_ra; 在linux/include/linux/fs.h中定義,文件預(yù)讀相關(guān) 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 的中文名稱(chēng)是目錄項(xiàng),是Linux文件系統(tǒng)中某個(gè)索引節(jié)點(diǎn)(inode)的鏈接。這個(gè)索引節(jié)點(diǎn)可以是文件,也可以是目錄。inode(可理解為ext2 inode)對(duì)應(yīng)于物理磁盤(pán)上的具體對(duì)象,dentry是一個(gè)內(nèi)存實(shí)體,其中的d_inode成員指向?qū)?yīng)的inode。也就是說(shuō),一個(gè)inode可以在運(yùn)行的時(shí)候鏈接多個(gè)dentry,而d_count記錄了這個(gè)鏈接的數(shù)量。 struct dentry { atomic_t d_count; 目錄項(xiàng)對(duì)象使用計(jì)數(shù)器,可以有未使用態(tài),使用態(tài)和負(fù)狀態(tài) unsigned int d_flags; 目錄項(xiàng)標(biāo)志 struct inode * d_inode; 與文件名關(guān)聯(lián)的索引節(jié)點(diǎn) struct dentry * d_parent; 父目錄的目錄項(xiàng)對(duì)象 struct list_head d_hash; 散列表表項(xiàng)的指針 struct list_head d_lru; 未使用鏈表的指針 struct list_head d_child; 父目錄中目錄項(xiàng)對(duì)象的鏈表的指針 struct list_head d_subdirs;對(duì)目錄而言,表示子目錄目錄項(xiàng)對(duì)象的鏈表 struct list_head d_alias; 相關(guān)索引節(jié)點(diǎn)(別名)的鏈表 int d_mounted; 對(duì)于安裝點(diǎn)而言,表示被安裝文件系統(tǒng)根項(xiàng) struct qstr d_name; 文件名 unsigned long d_time; /* used by d_revalidate */ struct dentry_operations *d_op; 目錄項(xiàng)方法 struct super_block * d_sb; 文件的超級(jí)塊對(duì)象 vunsigned long d_vfs_flags; void * d_fsdata;與文件系統(tǒng)相關(guān)的數(shù)據(jù) unsigned char d_iname [DNAME_INLINE_LEN]; 存放短文件名 }; (3)索引節(jié)點(diǎn)對(duì)象由inode結(jié)構(gòu)體表示,定義文件在linux/fs.h中。 struct inode { struct hlist_node i_hash; 哈希表 struct list_head i_list; 索引節(jié)點(diǎn)鏈表 struct list_head i_dentry; 目錄項(xiàng)鏈表 unsigned long i_ino; 節(jié)點(diǎn)號(hào) atomic_t i_count; 引用記數(shù) umode_t i_mode; 訪問(wèn)權(quán)限控制 unsigned int i_nlink; 硬鏈接數(shù) uid_t i_uid; 使用者id gid_t i_gid; 使用者id組 kdev_t i_rdev; 實(shí)設(shè)備標(biāo)識(shí)符 loff_t i_size; 以字節(jié)為單位的文件大小 struct timespec i_atime; 最后訪問(wèn)時(shí)間 struct timespec i_mtime; 最后修改(modify)時(shí)間 struct timespec i_ctime; 最后改變(change)時(shí)間 unsigned int i_blkbits; 以位為單位的塊大小 unsigned long i_blksize; 以字節(jié)為單位的塊大小 unsigned long i_version; 版本號(hào) unsigned long i_blocks; 文件的塊數(shù) unsigned short i_bytes; 使用的字節(jié)數(shù) spinlock_t i_lock; 自旋鎖 struct rw_semaphore i_alloc_sem; 索引節(jié)點(diǎn)信號(hào)量 struct inode_operations *i_op; 索引節(jié)點(diǎn)操作表 struct file_operations *i_fop; 默認(rèn)的索引節(jié)點(diǎn)操作 struct super_block *i_sb; 相關(guān)的超級(jí)塊 struct file_lock *i_flock; 文件鎖鏈表 struct address_space *i_mapping; 相關(guān)的地址映射 struct address_space i_data; 設(shè)備地址映射 struct dquot *i_dquot[MAXQUOTAS];節(jié)點(diǎn)的磁盤(pán)限額 struct list_head i_devices; 塊設(shè)備鏈表 struct pipe_inode_info *i_pipe; 管道信息 struct block_device *i_bdev; 塊設(shè)備驅(qū)動(dòng) unsigned long i_dnotify_mask;目錄通知掩碼 struct dnotify_struct *i_dnotify; 目錄通知 unsigned long i_state; 狀態(tài)標(biāo)志 unsigned long dirtied_when;首次修改時(shí)間 unsigned int i_flags; 文件系統(tǒng)標(biāo)志 unsigned char i_sock; 套接字 atomic_t i_writecount; 寫(xiě)者記數(shù) void *i_security; 安全模塊 __u32 i_generation; 索引節(jié)點(diǎn)版本號(hào) union { void *generic_ip;文件特殊信息 } u; }; 深圳專(zhuān)業(yè)嵌入式技術(shù)實(shí)訓(xùn),咨詢郭老師Q754634522 inode 譯成中文就是索引節(jié)點(diǎn)。每個(gè)存儲(chǔ)設(shè)備或存儲(chǔ)設(shè)備的分區(qū)(存儲(chǔ)設(shè)備是硬盤(pán)、軟盤(pán)、U盤(pán) ... ... )被格式化為文件系統(tǒng)后,應(yīng)該有兩部份,一部份是inode,另一部份是Block,Block是用來(lái)存儲(chǔ)數(shù)據(jù)用的。而inode呢,就是用來(lái)存儲(chǔ)這些數(shù)據(jù)的信息,這些信息包括文件大小、屬主、歸屬的用戶組、讀寫(xiě)權(quán)限等。inode為每個(gè)文件進(jìn)行信息索引,所以就有了inode的數(shù)值。操作系統(tǒng)根據(jù)指令,能通過(guò)inode值最快的找到相對(duì)應(yīng)的文件。 做個(gè)比喻,比如一本書(shū),存儲(chǔ)設(shè)備或分區(qū)就相當(dāng)于這本書(shū),Block相當(dāng)于書(shū)中的每一頁(yè),inode 就相當(dāng)于這本書(shū)前面的目錄,一本書(shū)有很多的內(nèi)容,如果想查找某部份的內(nèi)容,我們可以先查目錄,通過(guò)目錄能最快的找到我們想要看的內(nèi)容。 當(dāng)我們用ls 查看某個(gè)目錄或文件時(shí),如果加上-i 參數(shù),就可以看到inode節(jié)點(diǎn)了;比如ls -li lsfile.sh ,最前面的數(shù)值就是inode信息。 調(diào)用過(guò)程: write函數(shù)------>write系統(tǒng)調(diào)用------->先取出設(shè)備節(jié)點(diǎn)上的主、次設(shè)備號(hào)(在INODE節(jié)點(diǎn)上有描述,具體可以在網(wǎng)上去查查什么是inode節(jié)點(diǎn))-------->根據(jù)主設(shè)備號(hào)找到內(nèi)核設(shè)備管理中的fileoperations結(jié)構(gòu)體中的light_write函數(shù)地址(你在驅(qū)動(dòng)中曾經(jīng)注冊(cè)過(guò))--------->回調(diào)你的light_write函數(shù)--------->返回 深圳專(zhuān)業(yè)嵌入式技術(shù)實(shí)訓(xùn),咨詢郭老師Q754634522 |