Linux中的inode与dentry

理解文件系统中的inode与dentry。

inode

inode,全称为index node(索引节点)。它定义在include/linux/fs.h中:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
struct inode {
umode_t i_mode;
kuid_t i_uid;
kgid_t i_gid;
struct super_block *i_sb;
unsigned long i_ino; /* inode number */
union {
const unsigned int i_nlink; /* 硬链接数 */
unsigned int __i_nlink;
};
dev_t i_rdev; /* 设备号 */
loff_t i_size; /* 文件大小(以位为单位)*/
struct timespec64 i_atime; /* access time */
struct timespec64 i_mtime; /* modify time */
struct timespec64 i_ctime; /* change time */
unsigned short i_bytes;

atomic_t i_count; /* 内存引用计数 */
union {
struct hlist_head i_dentry; /* “被使用的”目录项链表 */
struct rcu_head i_rcu;
};
// ...
};

1.umode_t i_mode;中的类型umode_t定义在include/linux/types.h

1
typedef unsigned short		umode_t;

在C语言中,short类型占用2个字节,共计16位。变量i_mode的内存布局如下图所示:

2.struct hlist_head定义在include/linux/types.h中:

1
2
3
4
5
6
7
struct hlist_head {
struct hlist_node *first;
};

struct hlist_node {
struct hlist_node *next, **pprev;
};

dentry

dentry,全称为directory entry(目录项)。它定义在include/linux/dcache.h中:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/*
* "quick string" -- eases parameter passing, but more importantly
* saves "metadata" about the string (ie length and the hash).
*
* hash comes first so it snuggles against d_parent in the
* dentry.
*/
struct qstr {
union {
struct {
HASH_LEN_DECLARE;
};
u64 hash_len;
};
const unsigned char *name;
};

struct dentry {
struct dentry *d_parent; /* 父目录 */
struct qstr d_name; /* 目录项名称 */
struct inode *d_inode; /* 索引节点 */
unsigned char d_iname[DNAME_INLINE_LEN]; /* 短名称 */

struct list_head d_child; /* 父目录链表 */
struct list_head d_subdirs; /* 子目录链表 */

union {
struct list_head d_lru; /* LRU list(“最近被使用的”双向链表) */
wait_queue_head_t *d_wait; /* in-lookup ones only */
};
// ....
};

其中,struct list_head定义在include/linux/types.h中:

1
2
3
4
// 双向链表
struct list_head {
struct list_head *next, *prev;
};

super block

super block,超级块,定义在include/linux/fs.h中:

1
2
3
struct super_block {
struct dentry *s_root; /* 文件系统的根目录 */
};

file

file,文件,定义在include/linux/fs.h中:

1
2
3
struct file {
struct inode *f_inode; /* 文件的inode */
};

https://github.com/torvalds/linux/blob/master/include/linux/sched.h

1
2
3
4
struct task_struct {
/* Open file information: */
struct files_struct *files;
};

https://github.com/torvalds/linux/blob/master/include/linux/fdtable.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
* Open file table structure
*/
struct files_struct {
/*
* read mostly part
*/
atomic_t count;

struct fdtable __rcu *fdt;
struct fdtable fdtab;

struct file __rcu * fd_array[NR_OPEN_DEFAULT];
};

struct fdtable {
unsigned int max_fds;
struct file __rcu **fd; /* current fd array */
unsigned long *close_on_exec;
unsigned long *open_fds;
unsigned long *full_fds_bits;
struct rcu_head rcu;
};

使用Shell命令查看文件信息

ls

查看文件的相关属性ls -l

1
2
3
hgs:storage-systems hegongshan$ ls -l
total 16
-rw-r--r--@ 1 hegongshan staff 4199 10 9 18:26 README.md

解析:

1
2
3
[file mode] [number of links] [owner name] [group name] [number of bytes in the file] [modify time] [path name]

modify time: [month] [day] [hour]:[minute]

stat

查看文件的状态信息。

参考资料

  1. 《Linux内核设计与实现》,机械工业出版社
  2. Linux,https://github.com/torvalds/linux/

----------本文结束感谢您的阅读----------
坚持原创技术分享,您的支持将鼓励我继续创作!