inode是linux内核的文件系统里边最重要的数据结构,可以说,一个inode就代表了一个文件,inode结构体保存了文件的大小,创建时间,文件的块大小等各种参数,一个文件可以有多个dentry,因为在linux里由于软连接,硬连接的存在,指向一个文件的路径可能有多个,但是一个文件的inode只能有一个,一般在文件系统里氛围inode区和数据区,而inode区的大小能占到10%左右。
inode数据结构定义在include/linux/fs.h,我们看一下他的定义。
-
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;
-
unsigned long 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;
-
};
-
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 */
-
};
这个结构体很庞大,毕竟记录了一个文件的很多信息,我们来逐个分析一下成员变量的作用。
struct hlist_node i_hash;
在文件系统里,所有的inode都在哈希链表上,加快寻找速度。
struct list_head i_list;
struct list_head i_sb_list;
struct list_head i_dentry;
这分别是标识inode在使用中状态的链表,在superblock上记录的链表,在目录上链接的链表,当新建一个inode的时候,会将i_list加在所有的已使用的inode链表上,然后再加到超级块上,最后连接到对应的目录链表上。
unsigned long i_ino;/* inode的唯一标号 */
atomic_t i_count; /* inode引用计数 */
unsigned int i_nlink;/* inode的硬连接数 */
uid_t i_uid;/* inode的对应文件的用户id */
gid_t i_gid;/* inode的对应文件的用户的组id */
dev_t i_rdev;/* 设备标识 */
unsigned long i_version;/* 版本号 */
loff_t i_size; /* 文件大小 */
struct timespec i_atime;/* 最后修改时间 */
struct timespec i_mtime;/* 文件内容更改时间 */
struct timespec i_ctime;/* 文件change time */
unsigned int i_blkbits; /* inode块大小位数 */
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; /* inode的操作函数集合 */
const struct file_operations *i_fop; /* 文件操作函数 */
struct super_block *i_sb; /* 超级块指针 */
struct file_lock *i_flock; /* 文件锁 */
struct list_head i_devices; /* 连接到设备链表上 */
__u32 i_generation;
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; /* 私有数据 */
};