linux内核inode结构

inode是linux内核的文件系统里边最重要的数据结构,可以说,一个inode就代表了一个文件,inode结构体保存了文件的大小,创建时间,文件的块大小等各种参数,一个文件可以有多个dentry,因为在linux里由于软连接,硬连接的存在,指向一个文件的路径可能有多个,但是一个文件的inode只能有一个,一般在文件系统里氛围inode区和数据区,而inode区的大小能占到10%左右。
inode数据结构定义在include/linux/fs.h,我们看一下他的定义。

 
  1. struct inode {

  2. struct hlist_node i_hash;

  3. struct list_head i_list;

  4. struct list_head i_sb_list;

  5. struct list_head i_dentry;

  6. unsigned long i_ino;

  7. atomic_t i_count;

  8. unsigned int i_nlink;

  9. uid_t i_uid;

  10. gid_t i_gid;

  11. dev_t i_rdev;

  12. unsigned long i_version;

  13. loff_t i_size;

  14. #ifdef __NEED_I_SIZE_ORDERED

  15. seqcount_t i_size_seqcount;

  16. #endif

  17. struct timespec i_atime;

  18. struct timespec i_mtime;

  19. struct timespec i_ctime;

  20. unsigned int i_blkbits;

  21. blkcnt_t i_blocks;

  22. unsigned short i_bytes;

  23. umode_t i_mode;

  24. spinlock_t i_lock; /* i_blocks, i_bytes, maybe i_size */

  25. struct mutex i_mutex;

  26. struct rw_semaphore i_alloc_sem;

  27. const struct inode_operations *i_op;

  28. const struct file_operations *i_fop; /* former ->i_op->default_file_ops */

  29. struct super_block *i_sb;

  30. struct file_lock *i_flock;

  31. struct address_space *i_mapping;

  32. struct address_space i_data;

  33. #ifdef CONFIG_QUOTA

  34. struct dquot *i_dquot[MAXQUOTAS];

  35. #endif

  36. struct list_head i_devices;

  37. union {

  38. struct pipe_inode_info *i_pipe;

  39. struct block_device *i_bdev;

  40. struct cdev *i_cdev;

  41. };

  42. int i_cindex;

  43.  
  44.  
  45. __u32 i_generation;

  46.  
  47.  
  48. #ifdef CONFIG_DNOTIFY

  49. unsigned long i_dnotify_mask; /* Directory notify events */

  50. struct dnotify_struct *i_dnotify; /* for directory notifications */

  51. #endif

  52.  
  53.  
  54. #ifdef CONFIG_INOTIFY

  55. struct list_head inotify_watches; /* watches on this inode */

  56. struct mutex inotify_mutex; /* protects the watches list */

  57. #endif

  58.  
  59.  
  60. unsigned long i_state;

  61. unsigned long dirtied_when; /* jiffies of first dirtying */

  62.  
  63.  
  64. unsigned int i_flags;

  65.  
  66.  
  67. atomic_t i_writecount;

  68. #ifdef CONFIG_SECURITY

  69. void *i_security;

  70. #endif

  71. void *i_private; /* fs or device private pointer */

  72. };



这个结构体很庞大,毕竟记录了一个文件的很多信息,我们来逐个分析一下成员变量的作用。
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; /* 私有数据 */
};

猜你喜欢

转载自blog.csdn.net/sijinxiaotongxue/article/details/81123727