tinyfs 设计

数据结构

定义文件名长度、文件数量、块大小等。

1
2
3
4
#define MAX_NAMELEN (32)
#define MAX_FILES (32)
#define MAX_FILEBYTES (512)
#define TINYFS_MAGIC (0x20231118)

定义目录项格式:目录是由许多“文件”的文件名 -inode 值构成的列表。

1
2
3
4
5
// 定义每一个目录项的格式
struct tinyfs_dir_entry {
char filename[MAX_NAMELEN];
uint8_t idx;
};

定义一个目录项下最多存放多少个文件:由数据块大小除以每个目录数据占用的空间计算得出。

1
#define MAX_DENTRY_NR (MAX_FILEBYTES / sizeof(struct tinyfs_dir_entry))

定义每一个“文件”的格式:由元数据和数据部分构成。其中数据部分采用柔性数组,在创建“文件”时一并申请元数据和数据部分的内存空间。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// 定义每一个文件的格式
struct tinyfs_file_blk {
uint8_t busy; // 块是否被占用
mode_t mode; // 文件模式
uint8_t idx; // 块索引

union {
uint8_t file_size; // 文件大小
uint8_t dir_children; // 目录下的文件数量
};

// 数据部分,单个文件支持最大 MAX_FILEBYTES 字节
// 若是目录则可存储 MAX_DENTRY_NR 个目录项
char data[0];
};
虚拟文件系统 tinyfs 磁盘(内存)组织结构

接口实现

全局变量与公共接口

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
33
// 模拟磁盘的内存空间,在创建文件时搜索一个可用的块位置并申请块资源
static struct tinyfs_file_blk* disk[MAX_FILES + 1];

// 当前已创建文件的数量
static int files_count = 0;

// 初始化磁盘空间,所有块位置都可用
static void init_disk(void) {
for (int i = 0; i < MAX_FILES; i++) {
disk[i] = NULL;
}
}

// 分配新的块,并初始化一些参数
static struct tinyfs_file_blk* alloc_block(uint8_t idx, mode_t mode) {
struct tinyfs_file_blk* blk = kmalloc(sizeof(struct tinyfs_file_blk) + MAX_FILEBYTES, GFP_KERNEL);
if (!blk)
return NULL;

blk->busy = 1;
blk->mode = mode;
blk->idx = idx;

if (S_ISDIR(mode))
blk->dir_children = 0;
else
blk->file_size = 0;

// 数据部分占用的空间
memset(blk->data, 0, MAX_FILEBYTES);

return blk;
}

文件系统注册与注销

注册时指定文件系统类型,其中除 fs 的名字外、还有挂载和卸载的钩子函数。注销时,要释放所有“文件”的磁盘资源(这里是内存资源)。

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
33
34
35
36
37
38
// 文件系统类型定义
struct file_system_type tinyfs_fs_type = {
.owner = THIS_MODULE,
.name = "tinyfs",
.mount = tinyfs_mount,
.kill_sb = tinyfs_kill_superblock,
};

// 文件系统初始化
static int tinyfs_init(void) {
int ret;

init_disk();

ret = register_filesystem(&tinyfs_fs_type);
if (ret)
pr_err("Register tinyfs failed\n");

pr_info("tinyfs loaded\n");
return ret;
}

// 文件系统退出
static void tinyfs_exit(void) {
unregister_filesystem(&tinyfs_fs_type);

for (uint8_t i = 0; i < MAX_FILES + 1; i++) {
if (disk[i]) {
kfree(disk[i]);
disk[i] = NULL;
}
}

pr_info("tinyfs unloaded\n");
}

module_init(tinyfs_init);
module_exit(tinyfs_exit);

挂载文件系统

挂载文件系统:mount -t tinyfs none /mnt/tinyfs。通过 mount_nodev 函数申请 super block 资源,并使用指定的钩子函数 tinyfs_fill_super 初始化它,并与 tinyfs 的私有数据部分相关联。

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// 文件系统的挂载和初始化:从已申请的 sb 资源中申请 root inode 并初始化,以及持久化根目录内容到硬盘
int tinyfs_fill_super(struct super_block* sb, void* data, int flags) {
struct inode* root_inode;
int mode = S_IFDIR | 0755;

sb->s_magic = TINYFS_MAGIC;
sb->s_op = &tinyfs_super_operations;

root_inode = new_inode(sb);
if (!root_inode) {
pr_err("Failed to create root inode\n");
return -ENOMEM;
}

// Init uid, gid, mode for new inode according to posix standards
inode_init_owner(&nop_mnt_idmap, root_inode, NULL, mode);
root_inode->i_ino = 1; // 根目录的 inode 编号为 1
root_inode->i_sb = sb;
root_inode->i_op = &tinyfs_inode_ops;
root_inode->i_fop = &tinyfs_dir_operations;
root_inode->__i_atime = root_inode->__i_mtime = root_inode->__i_ctime = current_time(root_inode);

sb->s_root = d_make_root(root_inode); // 创建文件系统的 dentry 根目录
if (!sb->s_root) {
pr_err("Failed to create root dentry\n");
return -ENOMEM;
}

files_count++; // 更新文件计数

// 挂载的根目录的目录内容初始化
disk[1] = alloc_block(1, mode);
if (!disk[1]) {
pr_err("Failed to alloc a disk block space\n");
return -ENOMEM;
}
root_inode->i_private = disk[1];

pr_info("tinyfs mounted successfully\n");

return 0;
}

// 挂载文件系统:mount -t tinyfs none /mnt/tinyfs
static struct dentry* tinyfs_mount(struct file_system_type* fs_type, int flags, const char* dev_name, void* data) {
/*
* mount_nodev() 用于挂载一个没有设备文件(即设备为空)或者说是虚拟文件系统的文件系统
* 函数内,主要是通过 sget() 分配一个超级块 s,然后使用钩子函数 tinyfs_fill_super() 来初始化超级块的一些数据
* error = tinyfs_fill_super(s, data, flags & SB_SILENT ? 1 : 0);
* 在初始化成功后,通过 dget(s->s_root) 增加文件系统的根目录项的引用计数,并返回根目录项
*/
return mount_nodev(fs_type, flags, data, tinyfs_fill_super);
}

卸载文件系统

销毁文件系统:umount /mnt/tinyfs

1
2
3
4
5
// 销毁文件系统:umount /mnt/tinyfs
static void tinyfs_kill_superblock(struct super_block* sb) {
kill_anon_super(sb);
pr_info("tinyfs unmounted and resources cleaned up\n");
}

file 操作方法实现

通过 tinyfs_file_operations 变量初始化 file 的操作方法(这里是文件读、写接口)。

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// 文件操作结构体,包含了 read 和 write 方法
const struct file_operations tinyfs_file_operations = {
.read = tinyfs_file_read,
.write = tinyfs_file_write,
};

// 写入文件的实现:ssize_t write(int fd, const void buf[.count], size_t count);
ssize_t tinyfs_file_write(struct file* filp, const char __user* buf, size_t len, loff_t* ppos) {
struct tinyfs_file_blk* blk;
char* buffer;

blk = filp->f_path.dentry->d_inode->i_private; // 获取文件块
if (!blk) {
pr_err("Null pointer\n");
return -EFAULT;
}

buffer = (char*)&blk->data[0];
buffer += *ppos; // 将指针指向当前偏移位置

// 写入的长度不能超过文件的剩余数据空间
len = min(MAX_FILEBYTES - (size_t)blk->file_size, len);

if (copy_from_user(buffer, buf, len)) {
pr_err("Failed to copy data from user to kernel\n");
return -EFAULT;
}
*ppos += len; // 更新文件偏移量
blk->file_size = *ppos; // 更新文件大小

pr_info("Written %zu bytes to file, total %lld bytes\n", len, *ppos);

return len;
}

// 读取文件的实现:ssize_t read(int fd, void buf[.count], size_t count);
ssize_t tinyfs_file_read(struct file* filp, char __user* buf, size_t len, loff_t* ppos) {
struct tinyfs_file_blk* blk;
char* buffer;

// 获取文件块
blk = (struct tinyfs_file_blk*)filp->f_path.dentry->d_inode->i_private;
if (!blk) {
pr_err("Null pointer\n");
return -EFAULT;
}

// 如果当前偏移量超过文件大小,返回 0
if (*ppos > blk->file_size) {
pr_err("Failed to read, end of file reached: ppos=%lld, file_size=%d\n", *ppos, blk->file_size);
return 0;
}

// 读取的长度不能超过文件的大小
len = min((size_t)blk->file_size, len);

buffer = (char*)&blk->data[0];
if (copy_to_user(buf, buffer, len)) {
pr_err("Failed to copy data from kernel to user\n");
return -EFAULT;
}
*ppos += len; // 更新文件偏移量

pr_info("Read successful: ppos=%lld, len=%zu\n", *ppos, len);

return len;
}

目录文件操作方法实现

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
33
34
35
36
37
38
39
40
41
42
// 目录操作结构体,包含了 readdir(iterate_shared) 方法
const struct file_operations tinyfs_dir_operations = {
.owner = THIS_MODULE,
.iterate_shared = tinyfs_iterate,
};

// 读取目录的实现:遍历目录中的所有文件
static int tinyfs_iterate(struct file* filp, struct dir_context* ctx) {
loff_t pos;
struct tinyfs_file_blk* blk;
struct tinyfs_dir_entry* entry;
int i;

// 获取当前文件的位置,如果文件指针已经不在起始位置,说明已经读取完
pos = filp->f_pos;
if (pos)
return 0;

// 获取当前目录块的私有数据
blk = (struct tinyfs_file_blk*)filp->f_path.dentry->d_inode->i_private;
if (!blk) {
pr_err("Null pointer\n");
return -EFAULT;
}

// 如果当前块不是目录,返回错误
if (!S_ISDIR(blk->mode)) {
pr_err("Not a directory block\n");
return -ENOTDIR;
}

// 遍历目录中的所有文件
entry = (struct tinyfs_dir_entry*)&blk->data[0];
for (i = 0; i < blk->dir_children; i++) {
pr_info("Reading entry %d-th: %s\n", i + 1, entry[i].filename);
// 在目录列表中将目录条目(文件名和元数据)填充到目录缓冲区中
dir_emit(ctx, entry[i].filename, strnlen(entry[i].filename, MAX_NAMELEN), entry[i].idx, DT_UNKNOWN);
ctx->pos++;
}

return 0;
}

inode 操作方法实现

通过 tinyfs_inode_ops 变量初始化 inode 的操作方法(这里是创建文件、删除文件、创建目录、删除目录、查找“文件”)。

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// inode 操作集,包含文件和目录的操作
static struct inode_operations tinyfs_inode_ops = {
.create = tinyfs_create,
.unlink = tinyfs_unlink,
.mkdir = tinyfs_mkdir,
.rmdir = tinyfs_rmdir,
.lookup = tinyfs_lookup,
};

// 创建文件或目录的实现
static int tinyfs_do_create(struct inode* dir, struct dentry* dentry, umode_t mode) {
struct inode* inode;
struct super_block* sb;
struct tinyfs_dir_entry* entry;
struct tinyfs_file_blk *blk, *pblk;
int idx = -1;

sb = dir->i_sb;
if (!sb) {
pr_err("Null pointer\n");
return -EFAULT;
}

// 找到一个空闲块位置
for (int i = 2; i < MAX_FILES + 1; i++) {
if (!disk[i]) {
idx = i;
break;
}
}

if (files_count >= MAX_FILES || -1 == idx) {
pr_err("No space left to create file or directory\n");
return -ENOSPC;
}

// 如果模式既不是目录也不是文件,返回无效参数错误
if (!S_ISDIR(mode) && !S_ISREG(mode)) {
pr_err("Invalid mode (%u): neither directory nor file\n", mode);
return -EINVAL;
}

// 在找到的空闲块处分配块资源
disk[idx] = alloc_block(idx, mode);
if (!disk[idx]) {
pr_err("Failed to alloc a disk block space\n");
return -ENOMEM;
}

inode = new_inode(sb);
if (!inode) {
pr_err("Failed to create new inode\n");
return -ENOMEM;
}
inode->i_ino = idx;
inode->i_sb = sb;
inode->i_op = &tinyfs_inode_ops;
inode->__i_atime = inode->__i_mtime = inode->__i_ctime = current_time(inode);

blk = disk[idx];
blk->mode = mode;

// 按目录或文件,初始化相应成员
if (S_ISDIR(mode)) {
blk->dir_children = 0;
inode->i_fop = &tinyfs_dir_operations;
pr_info("Created directory: %s\n", dentry->d_name.name);
} else if (S_ISREG(mode)) {
blk->file_size = 0;
inode->i_fop = &tinyfs_file_operations;
pr_info("Created file: %s\n", dentry->d_name.name);
}

// 挂上 tinyfs 的私有数据块(第 idx 个 block)
inode->i_private = blk;

// 获取目录对应的 tinyfs 的私有数据块
pblk = (struct tinyfs_file_blk*)dir->i_private;
if (pblk->dir_children + 1 > MAX_DENTRY_NR) {
pr_err("No space left to save dir entry\n");
return -ENOMEM;
}

// 将新创建的文件或目录加入到父目录的子目录列表中
entry = (struct tinyfs_dir_entry*)&pblk->data[0];
entry += pblk->dir_children; // 跳过已存在的前 N 个孩子的内存,指到新孩子的存储位置

entry->idx = idx;
strncpy(entry->filename, dentry->d_name.name, MAX_NAMELEN - 1);

pblk->dir_children++; // 孩子数量加一

// 关键步骤:初始化并将新 inode 的添加到 dentry,即将文件或目录记录在当前目录下
inode_init_owner(&nop_mnt_idmap, inode, dir, mode);
d_add(dentry, inode);

files_count++;

return 0;
}

// 创建目录的实现
static int tinyfs_mkdir(struct mnt_idmap* idmap, struct inode* dir, struct dentry* dentry, umode_t mode) {
return tinyfs_do_create(dir, dentry, S_IFDIR | mode);
}

// 创建文件的实现
static int tinyfs_create(struct mnt_idmap* idmap, struct inode* dir, struct dentry* dentry, umode_t mode, bool excl) {
return tinyfs_do_create(dir, dentry, mode);
}

// 通过 sb 创建一个 inode 并初始化它
static struct inode* tinyfs_iget(struct super_block* sb, int idx) {
struct inode* inode;
struct tinyfs_file_blk* blk;

blk = disk[idx];
if (!blk) {
pr_err("Null pointer\n");
return NULL;
}

inode = new_inode(sb);
if (!inode) {
pr_err("Failed to create a new inode\n");
return NULL;
}
inode->i_ino = idx;
inode->i_sb = sb;
inode->i_op = &tinyfs_inode_ops;

if (S_ISDIR(blk->mode))
inode->i_fop = &tinyfs_dir_operations;
else if (S_ISREG(blk->mode))
inode->i_fop = &tinyfs_file_operations;

inode->__i_atime = inode->__i_mtime = inode->__i_ctime = current_time(inode);
inode->i_private = blk;

return inode;
}

struct dentry* tinyfs_lookup(struct inode* parent_inode, struct dentry* child_dentry, unsigned int flags) {
struct super_block* sb = parent_inode->i_sb;
struct tinyfs_file_blk* blk;
struct tinyfs_dir_entry* entry;

blk = (struct tinyfs_file_blk*)parent_inode->i_private;

entry = (struct tinyfs_dir_entry*)&blk->data[0];
for (int i = 0; i < blk->dir_children; i++) {
if (!strcmp(entry[i].filename, child_dentry->d_name.name)) {
struct inode* inode = tinyfs_iget(sb, entry[i].idx);
if (!inode) {
pr_err("Null pointer\n");
return NULL;
}
struct tinyfs_file_blk* inner = (struct tinyfs_file_blk*)inode->i_private;
inode_init_owner(&nop_mnt_idmap, inode, parent_inode, inner->mode);
d_add(child_dentry, inode);
pr_info("found dentry: %s\n", child_dentry->d_name.name);
return NULL;
}
}

return NULL;
}

// 删除目录的实现
int tinyfs_rmdir(struct inode* dir, struct dentry* dentry) {
struct inode* inode = dentry->d_inode;
struct tinyfs_file_blk* blk = (struct tinyfs_file_blk*)inode->i_private; // 获取目录的磁盘块
struct tinyfs_file_blk* pblk = (struct tinyfs_file_blk*)dir->i_private; // 获取父目录的磁盘块
struct tinyfs_dir_entry* entry;

// 检查是否为空目录
if (blk->dir_children > 0) {
pr_err("Cannot remove non-empty directory: %s\n", dentry->d_name.name);
return -ENOTEMPTY;
}

// 从父目录中移除该目录的目录项
entry = (struct tinyfs_dir_entry*)&pblk->data[0];
for (int i = 0; i < pblk->dir_children; i++) {
if (!strcmp(entry[i].filename, dentry->d_name.name)) { // 找到该文件
// 移动父目录中后续的目录项,覆盖当前的目录项
for (int j = i; j < pblk->dir_children - 1; j++) {
memcpy(&entry[j], &entry[j + 1], sizeof(struct tinyfs_dir_entry)); // 移动目录项
}
pblk->dir_children--;
break;
}
}

// 释放该文件的所有内存
disk[blk->idx] = NULL;
kfree(blk);
blk = NULL;

pr_info("Removed directory: %s\n", dentry->d_name.name);

return simple_rmdir(dir, dentry); // 调用 VFS 的 rmdir 实现
}

// 删除文件的实现
int tinyfs_unlink(struct inode* dir, struct dentry* dentry) {
struct inode* inode = dentry->d_inode;
struct tinyfs_file_blk* blk = (struct tinyfs_file_blk*)inode->i_private;
struct tinyfs_file_blk* pblk = (struct tinyfs_file_blk*)dir->i_private;
struct tinyfs_dir_entry* entry;

// 更新父目录的子目录列表,移除该文件的目录项
entry = (struct tinyfs_dir_entry*)&pblk->data[0];
for (int i = 0; i < pblk->dir_children; i++) {
if (!strcmp(entry[i].filename, dentry->d_name.name)) { // 找到该文件
for (int j = i; j < pblk->dir_children - 1; j++) {
memcpy(&entry[j], &entry[j + 1], sizeof(struct tinyfs_dir_entry)); // 移动目录项
}
pblk->dir_children--;
break;
}
}

// 释放该文件的所有内存
disk[blk->idx] = NULL;
kfree(blk);
blk = NULL;

pr_info("Removed file: %s\n", dentry->d_name.name);

return simple_unlink(dir, dentry); // 调用 VFS 的 unlink 实现
}

关键梳理

1
2
3
// tinyfs_fill_super()
root_inode->i_op = &tinyfs_inode_ops;
root_inode->i_fop = &tinyfs_dir_operations;

在挂载 tinyfs 文件系统时,在初始化的 super block 中指定了关于根目录的 inode 操作和根目录的目录 file 操作。这样就可以在挂载的根目录下,进行 touch/mkdir/ls 操作了。

1
2
3
4
5
6
7
8
// tinyfs_do_create()
inode->i_op = &tinyfs_inode_ops;

if (S_ISDIR(mode)) {
inode->i_fop = &tinyfs_dir_operations;
} else if (S_ISREG(mode)) {
inode->i_fop = &tinyfs_file_operations;
}

在 inode 的 .create 方法 tinyfs_do_create 中,指定了对于“文件”是目录还是文件的操作方法,这样在任意目录创建的文件就有了读、写操作;创建的目录就有了 ls 遍历操作。

完整代码

注意,本套代码在 Linux 6.8 内核版本下运行与测试。代码中不乏诸多 BUG,如:

  1. 删除非空目录
  2. 硬链接引用计数
  3. ls 无法显示当前目录 . 和父目录 ..
  4. 读取文件时会显示两份数据

tinyfs.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
#define MAX_NAMELEN (32)
#define MAX_FILES (32)
#define MAX_FILEBYTES (512)
#define TINYFS_MAGIC (0x20231118)

// 定义每一个目录项的格式
struct tinyfs_dir_entry {
char filename[MAX_NAMELEN];
uint8_t idx;
};

#define MAX_DENTRY_NR (MAX_FILEBYTES / sizeof(struct tinyfs_dir_entry))

// 定义每一个文件的格式
struct tinyfs_file_blk {
uint8_t busy; // 块是否被占用
mode_t mode; // 文件模式
uint8_t idx; // 块索引

union {
uint8_t file_size; // 文件大小
uint8_t dir_children; // 目录下的文件数量
};

// 数据部分,单个文件支持最大 MAX_FILEBYTES 字节
// 若是目录则可存储 MAX_DENTRY_NR 个目录项
char data[0];
};

tinyfs.c

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/time.h>
#include <linux/uaccess.h>

#include "tinyfs.h"

// 函数声明
static int tinyfs_iterate(struct file* filp, struct dir_context* ctx);
ssize_t tinyfs_file_read(struct file* filp, char __user* buf, size_t len, loff_t* ppos);
ssize_t tinyfs_file_write(struct file* filp, const char __user* buf, size_t len, loff_t* ppos);
static int tinyfs_do_create(struct inode* dir, struct dentry* dentry, umode_t mode);
static int tinyfs_mkdir(struct mnt_idmap* idmap, struct inode* dir, struct dentry* dentry, umode_t mode);
static int tinyfs_create(struct mnt_idmap* idmap, struct inode* dir, struct dentry* dentry, umode_t mode, bool excl);
int tinyfs_rmdir(struct inode* dir, struct dentry* dentry);
int tinyfs_unlink(struct inode* dir, struct dentry* dentry);
struct dentry* tinyfs_lookup(struct inode* parent_inode, struct dentry* child_dentry, unsigned int flags);
static struct dentry* tinyfs_mount(struct file_system_type* fs_type, int flags, const char* dev_name, void* data);
int tinyfs_fill_super(struct super_block* sb, void* data, int flags);

// 模拟磁盘的内存空间,在创建文件时搜索一个可用的块位置并申请块资源
static struct tinyfs_file_blk* disk[MAX_FILES + 1];

// 当前已创建文件的数量
static int files_count = 0;

// 初始化磁盘空间,所有块位置都可用
static void init_disk(void) {
for (int i = 0; i < MAX_FILES; i++) {
disk[i] = NULL;
}
}

// 分配新的块,并初始化一些参数
static struct tinyfs_file_blk* alloc_block(uint8_t idx, mode_t mode) {
struct tinyfs_file_blk* blk = kmalloc(sizeof(struct tinyfs_file_blk) + MAX_FILEBYTES, GFP_KERNEL);
if (!blk)
return NULL;

blk->busy = 1;
blk->mode = mode;
blk->idx = idx;

if (S_ISDIR(mode))
blk->dir_children = 0;
else
blk->file_size = 0;

// 数据部分占用的空间
memset(blk->data, 0, MAX_FILEBYTES);

return blk;
}

// 目录操作结构体,包含了 readdir(iterate_shared) 方法
const struct file_operations tinyfs_dir_operations = {
.owner = THIS_MODULE,
.iterate_shared = tinyfs_iterate,
};

// 读取目录的实现:遍历目录中的所有文件
static int tinyfs_iterate(struct file* filp, struct dir_context* ctx) {
loff_t pos;
struct tinyfs_file_blk* blk;
struct tinyfs_dir_entry* entry;
int i;

// 获取当前文件的位置,如果文件指针已经不在起始位置,说明已经读取完
pos = filp->f_pos;
if (pos)
return 0;

// 获取当前目录块的私有数据
blk = (struct tinyfs_file_blk*)filp->f_path.dentry->d_inode->i_private;
if (!blk) {
pr_err("Null pointer\n");
return -EFAULT;
}

// 如果当前块不是目录,返回错误
if (!S_ISDIR(blk->mode)) {
pr_err("Not a directory block\n");
return -ENOTDIR;
}

// 遍历目录中的所有文件
entry = (struct tinyfs_dir_entry*)&blk->data[0];
for (i = 0; i < blk->dir_children; i++) {
pr_info("Reading entry %d-th: %s\n", i + 1, entry[i].filename);
// 在目录列表中将目录条目(文件名和元数据)填充到目录缓冲区中
dir_emit(ctx, entry[i].filename, strnlen(entry[i].filename, MAX_NAMELEN), entry[i].idx, DT_UNKNOWN);
ctx->pos++;
}

return 0;
}

// 文件操作结构体,包含了 read 和 write 方法
const struct file_operations tinyfs_file_operations = {
.read = tinyfs_file_read,
.write = tinyfs_file_write,
};

// 写入文件的实现:ssize_t write(int fd, const void buf[.count], size_t count);
ssize_t tinyfs_file_write(struct file* filp, const char __user* buf, size_t len, loff_t* ppos) {
struct tinyfs_file_blk* blk;
char* buffer;

blk = filp->f_path.dentry->d_inode->i_private; // 获取文件块
if (!blk) {
pr_err("Null pointer\n");
return -EFAULT;
}

buffer = (char*)&blk->data[0];
buffer += *ppos; // 将指针指向当前偏移位置

// 写入的长度不能超过文件的剩余数据空间
len = min(MAX_FILEBYTES - (size_t)blk->file_size, len);

if (copy_from_user(buffer, buf, len)) {
pr_err("Failed to copy data from user to kernel\n");
return -EFAULT;
}
*ppos += len; // 更新文件偏移量
blk->file_size = *ppos; // 更新文件大小

pr_info("Written %zu bytes to file, total %lld bytes\n", len, *ppos);

return len;
}

// 读取文件的实现:ssize_t read(int fd, void buf[.count], size_t count);
ssize_t tinyfs_file_read(struct file* filp, char __user* buf, size_t len, loff_t* ppos) {
struct tinyfs_file_blk* blk;
char* buffer;

// 获取文件块
blk = (struct tinyfs_file_blk*)filp->f_path.dentry->d_inode->i_private;
if (!blk) {
pr_err("Null pointer\n");
return -EFAULT;
}

// 如果当前偏移量超过文件大小,返回 0
if (*ppos > blk->file_size) {
pr_err("Failed to read, end of file reached: ppos=%lld, file_size=%d\n", *ppos, blk->file_size);
return 0;
}

// 读取的长度不能超过文件的大小
len = min((size_t)blk->file_size, len);

buffer = (char*)&blk->data[0];
if (copy_to_user(buf, buffer, len)) {
pr_err("Failed to copy data from kernel to user\n");
return -EFAULT;
}
*ppos += len; // 更新文件偏移量

pr_info("Read successful: ppos=%lld, len=%zu\n", *ppos, len);

return len;
}

// inode 操作集,包含文件和目录的操作
static struct inode_operations tinyfs_inode_ops = {
.create = tinyfs_create,
.unlink = tinyfs_unlink,
.mkdir = tinyfs_mkdir,
.rmdir = tinyfs_rmdir,
.lookup = tinyfs_lookup,
};

// 创建文件或目录的实现
static int tinyfs_do_create(struct inode* dir, struct dentry* dentry, umode_t mode) {
struct inode* inode;
struct super_block* sb;
struct tinyfs_dir_entry* entry;
struct tinyfs_file_blk *blk, *pblk;
int idx = -1;

sb = dir->i_sb;
if (!sb) {
pr_err("Null pointer\n");
return -EFAULT;
}

// 找到一个空闲块位置
for (int i = 2; i < MAX_FILES + 1; i++) {
if (!disk[i]) {
idx = i;
break;
}
}

if (files_count >= MAX_FILES || -1 == idx) {
pr_err("No space left to create file or directory\n");
return -ENOSPC;
}

// 如果模式既不是目录也不是文件,返回无效参数错误
if (!S_ISDIR(mode) && !S_ISREG(mode)) {
pr_err("Invalid mode (%u): neither directory nor file\n", mode);
return -EINVAL;
}

// 在找到的空闲块处分配块资源
disk[idx] = alloc_block(idx, mode);
if (!disk[idx]) {
pr_err("Failed to alloc a disk block space\n");
return -ENOMEM;
}

inode = new_inode(sb);
if (!inode) {
pr_err("Failed to create new inode\n");
return -ENOMEM;
}
inode->i_ino = idx;
inode->i_sb = sb;
inode->i_op = &tinyfs_inode_ops;
inode->__i_atime = inode->__i_mtime = inode->__i_ctime = current_time(inode);

blk = disk[idx];
blk->mode = mode;

// 按目录或文件,初始化相应成员
if (S_ISDIR(mode)) {
blk->dir_children = 0;
inode->i_fop = &tinyfs_dir_operations;
pr_info("Created directory: %s\n", dentry->d_name.name);
} else if (S_ISREG(mode)) {
blk->file_size = 0;
inode->i_fop = &tinyfs_file_operations;
pr_info("Created file: %s\n", dentry->d_name.name);
}

// 挂上 tinyfs 的私有数据块(第 idx 个 block)
inode->i_private = blk;

// 获取目录对应的 tinyfs 的私有数据块
pblk = (struct tinyfs_file_blk*)dir->i_private;
if (pblk->dir_children + 1 > MAX_DENTRY_NR) {
pr_err("No space left to save dir entry\n");
return -ENOMEM;
}

// 将新创建的文件或目录加入到父目录的子目录列表中
entry = (struct tinyfs_dir_entry*)&pblk->data[0];
entry += pblk->dir_children; // 跳过已存在的前 N 个孩子的内存,指到新孩子的存储位置

entry->idx = idx;
strncpy(entry->filename, dentry->d_name.name, MAX_NAMELEN - 1);

pblk->dir_children++; // 孩子数量加一

// 关键步骤:初始化并将新 inode 的添加到 dentry,即将文件或目录记录在当前目录下
inode_init_owner(&nop_mnt_idmap, inode, dir, mode);
d_add(dentry, inode);

files_count++;

return 0;
}

// 创建目录的实现
static int tinyfs_mkdir(struct mnt_idmap* idmap, struct inode* dir, struct dentry* dentry, umode_t mode) {
return tinyfs_do_create(dir, dentry, S_IFDIR | mode);
}

// 创建文件的实现
static int tinyfs_create(struct mnt_idmap* idmap, struct inode* dir, struct dentry* dentry, umode_t mode, bool excl) {
return tinyfs_do_create(dir, dentry, mode);
}

// 通过 sb 创建一个 inode 并初始化它
static struct inode* tinyfs_iget(struct super_block* sb, int idx) {
struct inode* inode;
struct tinyfs_file_blk* blk;

blk = disk[idx];
if (!blk) {
pr_err("Null pointer\n");
return NULL;
}

inode = new_inode(sb);
if (!inode) {
pr_err("Failed to create a new inode\n");
return NULL;
}
inode->i_ino = idx;
inode->i_sb = sb;
inode->i_op = &tinyfs_inode_ops;

if (S_ISDIR(blk->mode))
inode->i_fop = &tinyfs_dir_operations;
else if (S_ISREG(blk->mode))
inode->i_fop = &tinyfs_file_operations;

inode->__i_atime = inode->__i_mtime = inode->__i_ctime = current_time(inode);
inode->i_private = blk;

return inode;
}

struct dentry* tinyfs_lookup(struct inode* parent_inode, struct dentry* child_dentry, unsigned int flags) {
struct super_block* sb = parent_inode->i_sb;
struct tinyfs_file_blk* blk;
struct tinyfs_dir_entry* entry;

blk = (struct tinyfs_file_blk*)parent_inode->i_private;

entry = (struct tinyfs_dir_entry*)&blk->data[0];
for (int i = 0; i < blk->dir_children; i++) {
if (!strcmp(entry[i].filename, child_dentry->d_name.name)) {
struct inode* inode = tinyfs_iget(sb, entry[i].idx);
if (!inode) {
pr_err("Null pointer\n");
return NULL;
}
struct tinyfs_file_blk* inner = (struct tinyfs_file_blk*)inode->i_private;
inode_init_owner(&nop_mnt_idmap, inode, parent_inode, inner->mode);
d_add(child_dentry, inode);
pr_info("found dentry: %s\n", child_dentry->d_name.name);
return NULL;
}
}

return NULL;
}

// 删除目录的实现
int tinyfs_rmdir(struct inode* dir, struct dentry* dentry) {
struct inode* inode = dentry->d_inode;
struct tinyfs_file_blk* blk = (struct tinyfs_file_blk*)inode->i_private; // 获取目录的磁盘块
struct tinyfs_file_blk* pblk = (struct tinyfs_file_blk*)dir->i_private; // 获取父目录的磁盘块
struct tinyfs_dir_entry* entry;

// 检查是否为空目录
if (blk->dir_children > 0) {
pr_err("Cannot remove non-empty directory: %s\n", dentry->d_name.name);
return -ENOTEMPTY;
}

// 从父目录中移除该目录的目录项
entry = (struct tinyfs_dir_entry*)&pblk->data[0];
for (int i = 0; i < pblk->dir_children; i++) {
if (!strcmp(entry[i].filename, dentry->d_name.name)) { // 找到该文件
// 移动父目录中后续的目录项,覆盖当前的目录项
for (int j = i; j < pblk->dir_children - 1; j++) {
memcpy(&entry[j], &entry[j + 1], sizeof(struct tinyfs_dir_entry)); // 移动目录项
}
pblk->dir_children--;
break;
}
}

// 释放该文件的所有内存
disk[blk->idx] = NULL;
kfree(blk);
blk = NULL;

pr_info("Removed directory: %s\n", dentry->d_name.name);

return simple_rmdir(dir, dentry); // 调用 VFS 的 rmdir 实现
}

// 删除文件的实现
int tinyfs_unlink(struct inode* dir, struct dentry* dentry) {
struct inode* inode = dentry->d_inode;
struct tinyfs_file_blk* blk = (struct tinyfs_file_blk*)inode->i_private;
struct tinyfs_file_blk* pblk = (struct tinyfs_file_blk*)dir->i_private;
struct tinyfs_dir_entry* entry;

// 更新父目录的子目录列表,移除该文件的目录项
entry = (struct tinyfs_dir_entry*)&pblk->data[0];
for (int i = 0; i < pblk->dir_children; i++) {
if (!strcmp(entry[i].filename, dentry->d_name.name)) { // 找到该文件
for (int j = i; j < pblk->dir_children - 1; j++) {
memcpy(&entry[j], &entry[j + 1], sizeof(struct tinyfs_dir_entry)); // 移动目录项
}
pblk->dir_children--;
break;
}
}

// 释放该文件的所有内存
disk[blk->idx] = NULL;
kfree(blk);
blk = NULL;

pr_info("Removed file: %s\n", dentry->d_name.name);

return simple_unlink(dir, dentry); // 调用 VFS 的 unlink 实现
}

static const struct super_operations tinyfs_super_operations = {
// .alloc_inode = new_inode,
.statfs = simple_statfs, // 使用通用的文件系统统计操作
.drop_inode = generic_delete_inode, // 删除 inode 的默认操作
};

// 文件系统的挂载和初始化:从已申请的 sb 资源中申请 root inode 并初始化,以及持久化根目录内容到硬盘
int tinyfs_fill_super(struct super_block* sb, void* data, int flags) {
struct inode* root_inode;
int mode = S_IFDIR | 0755;

sb->s_magic = TINYFS_MAGIC;
sb->s_op = &tinyfs_super_operations;

root_inode = new_inode(sb);
if (!root_inode) {
pr_err("Failed to create root inode\n");
return -ENOMEM;
}

// Init uid, gid, mode for new inode according to posix standards
inode_init_owner(&nop_mnt_idmap, root_inode, NULL, mode);
root_inode->i_ino = 1; // 根目录的 inode 编号为 1
root_inode->i_sb = sb;
root_inode->i_op = &tinyfs_inode_ops;
root_inode->i_fop = &tinyfs_dir_operations;
root_inode->__i_atime = root_inode->__i_mtime = root_inode->__i_ctime = current_time(root_inode);

sb->s_root = d_make_root(root_inode); // 创建文件系统的 dentry 根目录
if (!sb->s_root) {
pr_err("Failed to create root dentry\n");
return -ENOMEM;
}

files_count++; // 更新文件计数

// 挂载的根目录的目录内容初始化
disk[1] = alloc_block(1, mode);
if (!disk[1]) {
pr_err("Failed to alloc a disk block space\n");
return -ENOMEM;
}
root_inode->i_private = disk[1];

pr_info("tinyfs mounted successfully\n");

return 0;
}

// 挂载文件系统:mount -t tinyfs none /mnt/tinyfs
static struct dentry* tinyfs_mount(struct file_system_type* fs_type, int flags, const char* dev_name, void* data) {
/*
* mount_nodev() 用于挂载一个没有设备文件(即设备为空)或者说是虚拟文件系统的文件系统
* 函数内,主要是通过 sget() 分配一个超级块 s,然后使用钩子函数 tinyfs_fill_super() 来初始化超级块的一些数据
* error = tinyfs_fill_super(s, data, flags & SB_SILENT ? 1 : 0);
* 在初始化成功后,通过 dget(s->s_root) 增加文件系统的根目录项的引用计数,并返回根目录项
*/
return mount_nodev(fs_type, flags, data, tinyfs_fill_super);
}

// 销毁文件系统:umount /mnt/tinyfs
static void tinyfs_kill_superblock(struct super_block* sb) {
kill_anon_super(sb);
pr_info("tinyfs unmounted and resources cleaned up\n");
}

// 文件系统类型定义
struct file_system_type tinyfs_fs_type = {
.owner = THIS_MODULE,
.name = "tinyfs",
.mount = tinyfs_mount,
.kill_sb = tinyfs_kill_superblock,
};

// 文件系统初始化
static int tinyfs_init(void) {
int ret;

init_disk();

ret = register_filesystem(&tinyfs_fs_type);
if (ret)
pr_err("Register tinyfs failed\n");

pr_info("tinyfs loaded\n");
return ret;
}

// 文件系统退出
static void tinyfs_exit(void) {
unregister_filesystem(&tinyfs_fs_type);

for (uint8_t i = 0; i < MAX_FILES + 1; i++) {
if (disk[i]) {
kfree(disk[i]);
disk[i] = NULL;
}
}

pr_info("tinyfs unloaded\n");
}

module_init(tinyfs_init);
module_exit(tinyfs_exit);

MODULE_LICENSE("GPL");

/*
umount -f /mnt/tinyfs
rmmod tinyfs
make clean
make
insmod tinyfs.ko
mount -t tinyfs none /mnt/tinyfs
*/

测试

挂载命令

1
2
3
4
make clean
make
insmod tinyfs.ko
mount -t tinyfs none /mnt/tinyfs

卸载命令

需要在不进入 /mnt/tinyfs 目录下执行卸载命令。

1
2
umount -f /mnt/tinyfs
rmmod tinyfs

测试过程

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
root@Standard-PC:/mnt# ls -li
总计 0
1 drwxr-xr-x 1 root root 0 11 月 19 18:48 tinyfs
root@Standard-PC:/mnt# cd tinyfs/
root@Standard-PC:/mnt/tinyfs# ls -li
总计 0
root@Standard-PC:/mnt/tinyfs# mkdir test1
root@Standard-PC:/mnt/tinyfs# mkdir test2
root@Standard-PC:/mnt/tinyfs# mkdir test3
root@Standard-PC:/mnt/tinyfs# touch text1.txt
root@Standard-PC:/mnt/tinyfs# touch text2.txt
root@Standard-PC:/mnt/tinyfs# mkdir test4
root@Standard-PC:/mnt/tinyfs# echo 12345 > text3.txt
root@Standard-PC:/mnt/tinyfs#
root@Standard-PC:/mnt/tinyfs# ls -l -i
总计 0
2 drwxr-xr-x 1 root root 0 11 月 19 18:49 test1
3 drwxr-xr-x 1 root root 0 11 月 19 18:49 test2
4 drwxr-xr-x 1 root root 0 11 月 19 18:49 test3
7 drwxr-xr-x 1 root root 0 11 月 19 18:50 test4
5 -rw-r--r-- 1 root root 0 11 月 19 18:49 text1.txt
6 -rw-r--r-- 1 root root 0 11 月 19 18:50 text2.txt
8 -rw-r--r-- 1 root root 0 11 月 19 18:50 text3.txt
root@Standard-PC:/mnt/tinyfs# rm text1.txt
root@Standard-PC:/mnt/tinyfs# ls -l -i
总计 0
2 drwxr-xr-x 1 root root 0 11 月 19 18:49 test1
3 drwxr-xr-x 1 root root 0 11 月 19 18:49 test2
4 drwxr-xr-x 1 root root 0 11 月 19 18:49 test3
7 drwxr-xr-x 1 root root 0 11 月 19 18:50 test4
6 -rw-r--r-- 1 root root 0 11 月 19 18:50 text2.txt
8 -rw-r--r-- 1 root root 0 11 月 19 18:50 text3.txt
root@Standard-PC:/mnt/tinyfs# echo abcd > text1.txt
root@Standard-PC:/mnt/tinyfs# ls -l -i
总计 0
2 drwxr-xr-x 1 root root 0 11 月 19 18:49 test1
3 drwxr-xr-x 1 root root 0 11 月 19 18:49 test2
4 drwxr-xr-x 1 root root 0 11 月 19 18:49 test3
7 drwxr-xr-x 1 root root 0 11 月 19 18:50 test4
5 -rw-r--r-- 1 root root 0 11 月 19 18:51 text1.txt
6 -rw-r--r-- 1 root root 0 11 月 19 18:50 text2.txt
8 -rw-r--r-- 1 root root 0 11 月 19 18:50 text3.txt
root@Standard-PC:/mnt/tinyfs# cd test2
root@Standard-PC:/mnt/tinyfs/test2# mkdir test21
root@Standard-PC:/mnt/tinyfs/test2# touch text21.txt
root@Standard-PC:/mnt/tinyfs/test2# mkdir test22
root@Standard-PC:/mnt/tinyfs/test2# touch text22.txt
root@Standard-PC:/mnt/tinyfs/test2# ls -l -i
总计 0
9 drwxr-xr-x 1 root root 0 11 月 19 18:52 test21
11 drwxr-xr-x 1 root root 0 11 月 19 18:52 test22
10 -rw-r--r-- 1 root root 0 11 月 19 18:52 text21.txt
12 -rw-r--r-- 1 root root 0 11 月 19 18:52 text22.txt
root@Standard-PC:/mnt/tinyfs/test2# rm text21.txt
root@Standard-PC:/mnt/tinyfs/test2# rm -r test22
root@Standard-PC:/mnt/tinyfs/test2# ls -l -i
总计 0
9 drwxr-xr-x 1 root root 0 11 月 19 18:52 test21
12 -rw-r--r-- 1 root root 0 11 月 19 18:52 text22.txt
root@Standard-PC:/mnt/tinyfs/test2# ls
test21 text22.txt
root@Standard-PC:/mnt/tinyfs/test2# cd ..
root@Standard-PC:/mnt/tinyfs# ls
test1 test2 test3 test4 text1.txt text2.txt text3.txt
root@Standard-PC:/mnt/tinyfs# tree
.
├── test1
├── test2
│   ├── test21
│   └── text22.txt
├── test3
├── test4
├── text1.txt
├── text2.txt
└── text3.txt

5 directories, 4 files
root@Standard-PC:/mnt/tinyfs# cat text3.txt
12345
12345 // 数据被读取了两次!
root@Standard-PC:/mnt/tinyfs# rm -r test2
root@Standard-PC:/mnt/tinyfs# ls
test1 test3 test4 text1.txt text2.txt text3.txt
root@Standard-PC:/mnt/tinyfs# cd test2
-bash: cd: test2: 没有那个文件或目录
root@Standard-PC:/mnt/tinyfs# touch 1.txt
root@Standard-PC:/mnt/tinyfs# touch 2.txt
root@Standard-PC:/mnt/tinyfs# touch 3.txt
root@Standard-PC:/mnt/tinyfs#
root@Standard-PC:/mnt/tinyfs# ls -l -i
总计 0
3 -rw-r--r-- 1 root root 0 11 月 19 18:55 1.txt
9 -rw-r--r-- 1 root root 0 11 月 19 18:55 2.txt
10 -rw-r--r-- 1 root root 0 11 月 19 18:55 3.txt
2 drwxr-xr-x 1 root root 0 11 月 19 18:49 test1
4 drwxr-xr-x 1 root root 0 11 月 19 18:49 test3
7 drwxr-xr-x 1 root root 0 11 月 19 18:50 test4
5 -rw-r--r-- 1 root root 0 11 月 19 18:51 text1.txt
6 -rw-r--r-- 1 root root 0 11 月 19 18:50 text2.txt
8 -rw-r--r-- 1 root root 0 11 月 19 18:50 text3.txt
root@Standard-PC:/mnt/tinyfs#

内核日志

从日志中可以看到,在删除目录、文件时有一些 WARNING。

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
root@Standard-PC:/home/zkwang/ccode/tinyfs# dmesg
[3554.248389] tinyfs loaded
[3573.580879] tinyfs mounted successfully
[3627.377401] Created directory: test1
[3629.360099] Created directory: test2
[3631.790973] Created directory: test3
[3645.009682] Created file: text1.txt
[3649.358875] Created file: text2.txt
[3661.254142] Created directory: test4
[3672.623560] Created file: text3.txt
[3672.623580] Written 6 bytes to file, total 6 bytes
[3696.802383] Reading entry 1-th: test1
[3696.802388] Reading entry 2-th: test2
[3696.802389] Reading entry 3-th: test3
[3696.802389] Reading entry 4-th: text1.txt
[3696.802390] Reading entry 5-th: text2.txt
[3696.802391] Reading entry 6-th: test4
[3696.802392] Reading entry 7-th: text3.txt
[3723.591431] Removed file: text1.txt
[3723.591447] ------------[cut here]------------
[3723.591448] WARNING: CPU: 1 PID: 5898 at fs/dcache.c:766 dput+0x199/0x1b0
[3723.591456] Modules linked in: tinyfs(OE) 省略...
[3723.591612] </TASK>
[3723.591613] ---[end trace 0000000000000000]---
[3728.585730] Reading entry 1-th: test1
[3728.585736] Reading entry 2-th: test2
[3728.585737] Reading entry 3-th: test3
[3728.585738] Reading entry 4-th: text2.txt
[3728.585739] Reading entry 5-th: test4
[3728.585739] Reading entry 6-th: text3.txt
[3749.471244] Created file: text1.txt
[3749.471260] Written 5 bytes to file, total 5 bytes
[3751.604462] Reading entry 1-th: test1
[3751.604478] Reading entry 2-th: test2
[3751.604479] Reading entry 3-th: test3
[3751.604480] Reading entry 4-th: text2.txt
[3751.604481] Reading entry 5-th: test4
[3751.604482] Reading entry 6-th: text3.txt
[3751.604483] Reading entry 7-th: text1.txt
[3757.179360] Reading entry 1-th: test1
[3757.179366] Reading entry 2-th: test2
[3757.179367] Reading entry 3-th: test3
[3757.179367] Reading entry 4-th: text2.txt
[3757.179368] Reading entry 5-th: test4
[3757.179369] Reading entry 6-th: text3.txt
[3757.179370] Reading entry 7-th: text1.txt
[3772.983041] Created directory: test21
[3790.182636] Created file: text21.txt
[3797.113555] Created directory: test22
[3799.803665] Created file: text22.txt
[3805.302208] Reading entry 1-th: test21
[3805.302215] Reading entry 2-th: text21.txt
[3805.302216] Reading entry 3-th: test22
[3805.302217] Reading entry 4-th: text22.txt
[3819.650587] Removed file: text21.txt
[3848.111870] Removed directory: test22
[3848.111894] ------------[cut here]------------
[3848.111895] WARNING: CPU: 8 PID: 5913 at fs/inode.c:331 drop_nlink+0x2e/0x50
[3848.111902] Modules linked in: tinyfs(OE) 省略...
[3848.112103] </TASK>
[3848.112104] ---[end trace 0000000000000000]---
[3850.845726] Reading entry 1-th: test21
[3850.845731] Reading entry 2-th: text22.txt
[3856.437171] Reading entry 1-th: test21
[3856.437175] Reading entry 2-th: text22.txt
[3859.858365] Reading entry 1-th: test1
[3859.858369] Reading entry 2-th: test2
[3859.858370] Reading entry 3-th: test3
[3859.858371] Reading entry 4-th: text2.txt
[3859.858372] Reading entry 5-th: test4
[3859.858373] Reading entry 6-th: text3.txt
[3859.858373] Reading entry 7-th: text1.txt
[3864.182500] Reading entry 1-th: test1
[3864.182506] Reading entry 2-th: test2
[3864.182507] Reading entry 3-th: test3
[3864.182507] Reading entry 4-th: text2.txt
[3864.182509] Reading entry 5-th: test4
[3864.182509] Reading entry 6-th: text3.txt
[3864.182510] Reading entry 7-th: text1.txt
[3864.182591] Reading entry 1-th: test21
[3864.182593] Reading entry 2-th: text22.txt
[3879.324995] Reading entry 1-th: test1
[3879.325000] Reading entry 2-th: test2
[3879.325001] Reading entry 3-th: test3
[3879.325002] Reading entry 4-th: text2.txt
[3879.325002] Reading entry 5-th: test4
[3879.325003] Reading entry 6-th: text3.txt
[3879.325004] Reading entry 7-th: text1.txt
[3880.713272] Read successful: ppos=6, len=6
[3880.713285] Read successful: ppos=12, len=6
[3880.713288] Failed to read, end of file reached: ppos=12, file_size=6
[3917.372096] Reading entry 1-th: test21
[3917.372102] Reading entry 2-th: text22.txt
[3917.372112] Reading entry 1-th: test21
[3917.372113] Reading entry 2-th: text22.txt
[3917.372128] Removed directory: test21
[3917.372146] ------------[cut here]------------
[3917.372148] WARNING: CPU: 8 PID: 5923 at fs/inode.c:331 drop_nlink+0x2e/0x50
[3917.372155] Modules linked in: tinyfs(OE) 省略...
[3917.372388] </TASK>
[3917.372389] ---[end trace 0000000000000000]---
[3917.372395] ------------[cut here]------------
[3917.372396] WARNING: CPU: 8 PID: 5923 at fs/inode.c:331 drop_nlink+0x2e/0x50
[3917.372399] Modules linked in: tinyfs(OE) 省略...
[3917.372514] </TASK>
[3917.372515] ---[end trace 0000000000000000]---
[3917.372529] Removed file: text22.txt
[3917.372539] Removed directory: test2
[3919.763064] Reading entry 1-th: test1
[3919.763070] Reading entry 2-th: test3
[3919.763071] Reading entry 3-th: text2.txt
[3919.763072] Reading entry 4-th: test4
[3919.763073] Reading entry 5-th: text3.txt
[3919.763074] Reading entry 6-th: text1.txt
[3925.485462] Reading entry 1-th: test1
[3925.485468] Reading entry 2-th: test3
[3925.485469] Reading entry 3-th: text2.txt
[3925.485470] Reading entry 4-th: test4
[3925.485471] Reading entry 5-th: text3.txt
[3925.485472] Reading entry 6-th: text1.txt
[3926.325130] Reading entry 1-th: test1
[3926.325134] Reading entry 2-th: test3
[3926.325135] Reading entry 3-th: text2.txt
[3926.325136] Reading entry 4-th: test4
[3926.325137] Reading entry 5-th: text3.txt
[3926.325137] Reading entry 6-th: text1.txt
[3926.587543] Reading entry 1-th: test1
[3926.587547] Reading entry 2-th: test3
[3926.587548] Reading entry 3-th: text2.txt
[3926.587548] Reading entry 4-th: test4
[3926.587549] Reading entry 5-th: text3.txt
[3926.587550] Reading entry 6-th: text1.txt
[3978.529407] Created file: 1.txt
[3981.841504] Created file: 2.txt
[3984.412749] Created file: 3.txt
[3997.119562] Reading entry 1-th: test1
[3997.119567] Reading entry 2-th: test3
[3997.119568] Reading entry 3-th: text2.txt
[3997.119569] Reading entry 4-th: test4
[3997.119570] Reading entry 5-th: text3.txt
[3997.119571] Reading entry 6-th: text1.txt
[3997.119571] Reading entry 7-th: 1.txt
[3997.119572] Reading entry 8-th: 2.txt
[3997.119573] Reading entry 9-th: 3.txt
root@Standard-PC:/home/zkwang/ccode/tinyfs#

参考资料:

  1. https://cloud.tencent.com/developer/article/1497058
  2. https://blog.csdn.net/yanghao23/article/details/135892565