Linux2.6X核心中檔案相關結構體總結一、 常見檔案相關結構體
以2.6.22為例,其他的2.6.X版本可能調整,但是變化不大。
1.1 struct file
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;
};
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; /* 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]; 存放短檔案名稱
};
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;
};
為一個進程中各主要的檔案相關結構的關係圖:
為多個進程開啟同一檔案的情況:
二、執行個體2.1 由相對路徑得到絕對路徑
核心的結構體不提供檔案的絕對路徑成員,由相對路徑得到絕對路徑的關鍵在得到當前的工作路徑。
當前的工作路徑可以由current->fs的pwd和pwdmnt成員得到,基本過程:
1、由pwd->d_name.Name得到當前路徑名,由pwd->d_parent得到父目錄dentry結構,
一直向上檢索,直到父目錄為"/",此時得到的是檔案在本檔案系統下的路徑。
2、如果current->fs->pwdmnt->mnt_mountpoint的路徑不是"/",說明檔案所在的檔案系統不是掛載在根目錄下,需要得到掛載點。mnt_mountpoint也是一個dentry變數,方法同1。
3、最後把檔案系統掛載點和檔案在檔案系統中的路徑拼接起來即可。
2.2 由檔案描述符得到檔案的絕對路徑
由當前進程的檔案描述符得到檔案的絕對路徑:
#if LINUX_VERSION_CODE >= 0x020616
由current->files->fdt->fd[fd]->f_path.dentry->d_name.Name
和current->files->fdt->fd[fd]->f_path.dentry->d_parent
得到在所在檔案系統下的路徑
由current->files->fdt->fd[fd]->f_path.mnt->mnt_mountpoint->d_name.Name
和current->files->fdt->fd[fd]->f_path.mnt->mnt_mountpoint->d_parent
得到檔案系統掛載點在系統中掛載點的路徑
#elif LINUX_VERSION_CODE >= 0x020610
由current->files->fdt->fd[fd]->f_dentry->d_name.Name
和current->files->fdt->fd[fd]->f_dentry->d_parent
得到在所在檔案系統下的路徑
由current->files->fdt->fd[fd]->f_vfsmnt->mnt_mountpoint->d_name.Name
和current->files->fdt->fd[fd]->f_vfsmnt->mnt_mountpoint->d_parent
得到檔案系統掛載點在系統中掛載點的路徑
#else
由current->files->fd[fd]->f_dentry->d_name.Name
和current->files->fd[fd]->f_dentry->d_parent
得到在所在檔案系統下的路徑
由current->files->fd[fd]->f_vfsmnt->mnt_mountpoint->d_name.Name
和current->files->fd[fd]->f_vfsmnt->mnt_mountpoint->d_parent
得到檔案系統掛載點在系統中掛載點的路徑
#endif
2.3 得到當前進程的絕對路徑
#if LINUX_VERSION_CODE >= 0x020616
由current->mm->mmap->vm_file->f_path.dentry->d_name.Name
和current->mm->mmap->vm_file->f_path.dentry->d_parent
得到在所在檔案系統下的路徑
由current->mm->mmap->vm_file->f_path.mnt->mnt_mountpoint->d_name.Name
和current->mm->mmap->vm_file->f_path.mnt->mnt_mountpoint->d_parent
得到檔案系統掛載點在系統中掛載點的路徑
#else
由current->mm->mmap->vm_file->f_dentry->d_name.Name
和current->mm->mmap->vm_file->f_dentry->d_parent
得到在所在檔案系統下的路徑
由current->mm->mmap->vm_file->f_vfsmnt->mnt_mountpoint->d_name.Name
和current->mm->mmap->vm_file->f_vfsmnt->mnt_mountpoint->d_parent
得到檔案系統掛載點在系統中掛載點的路徑
#endif