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
|
/* SPDX-License-Identifier: GPL-2.0-or-later */
/* Copyright (C) 2026 Richard Qin */
/*
* test_dcache.cc — Self-tests for the dentry cache layer
*/
#include <lume/selftest.h>
#include <lume/dcache.h>
#include <lume/fs.h>
#include <lume/mount.h>
#include <lume/errno.h>
#include <lume/slab.h>
/* Root dentry/mount from VFS init */
extern dentry* g_root_dentry;
extern vfsmount* g_root_mnt;
void selftest_dcache()
{
/* ---- d_alloc: basic allocation under root ---- */
st_begin("dcache: d_alloc basic");
{
qstr name{};
name.name = "test_child";
name.len = 10;
name.hash = full_name_hash(name.name, name.len);
dentry* child = d_alloc(g_root_dentry, &name);
ST_ASSERT(child != nullptr);
ST_ASSERT_EQ(child->d_name.len, 10u);
ST_ASSERT(child->d_parent == g_root_dentry);
ST_ASSERT(child->d_inode == nullptr); /* not instantiated yet */
ST_ASSERT(child->d_sb == g_root_dentry->d_sb);
/* Name was copied into inline buffer */
bool match = true;
for (uint32 i = 0; i < name.len; i++) {
if (child->d_name.name[i] != name.name[i]) { match = false; break; }
}
ST_ASSERT(match);
d_drop(child);
dput(child);
}
st_pass();
/* ---- d_lookup: find existing child ---- */
st_begin("dcache: d_lookup finds child");
{
qstr name{};
name.name = "lookup_me";
name.len = 9;
name.hash = full_name_hash(name.name, name.len);
dentry* child = d_alloc(g_root_dentry, &name);
ST_ASSERT(child != nullptr);
/* d_lookup should find it */
dentry* found = d_lookup(g_root_dentry, &name);
ST_ASSERT(found != nullptr);
ST_ASSERT(found == child);
dput(found); /* drop d_lookup ref */
d_drop(child);
dput(child);
}
st_pass();
/* ---- d_lookup: miss returns nullptr ---- */
st_begin("dcache: d_lookup miss returns nullptr");
{
qstr name{};
name.name = "nonexistent_xyz";
name.len = 15;
name.hash = full_name_hash(name.name, name.len);
dentry* found = d_lookup(g_root_dentry, &name);
ST_ASSERT(found == nullptr);
}
st_pass();
/* ---- d_instantiate: bind inode to dentry ---- */
st_begin("dcache: d_instantiate binds inode");
{
qstr name{};
name.name = "has_inode";
name.len = 9;
name.hash = full_name_hash(name.name, name.len);
dentry* child = d_alloc(g_root_dentry, &name);
ST_ASSERT(child != nullptr);
ST_ASSERT(child->d_inode == nullptr);
/* Create a dummy inode */
inode* ip = new_inode(g_root_dentry->d_sb);
ST_ASSERT(ip != nullptr);
ip->i_mode = S_IFREG | 0644;
d_instantiate(child, ip);
ST_ASSERT(child->d_inode == ip);
ST_ASSERT(child->d_flags & DCACHE_REGULAR_TYPE);
d_drop(child);
dput(child);
}
st_pass();
/* ---- dget/dput: refcount ---- */
st_begin("dcache: dget/dput refcount");
{
qstr name{};
name.name = "reftest";
name.len = 7;
name.hash = full_name_hash(name.name, name.len);
dentry* child = d_alloc(g_root_dentry, &name);
ST_ASSERT(child != nullptr);
/* d_alloc sets refcount = 1 */
int rc1 = child->d_lockref.load();
ST_ASSERT(rc1 >= 1);
dget(child);
int rc2 = child->d_lockref.load();
ST_ASSERT_EQ(rc2, rc1 + 1);
dput(child);
int rc3 = child->d_lockref.load();
ST_ASSERT_EQ(rc3, rc1);
d_drop(child);
dput(child);
}
st_pass();
/* ---- d_drop: removes from parent children list ---- */
st_begin("dcache: d_drop removes from parent");
{
qstr name{};
name.name = "dropme";
name.len = 6;
name.hash = full_name_hash(name.name, name.len);
dentry* child = d_alloc(g_root_dentry, &name);
ST_ASSERT(child != nullptr);
/* Should be findable */
dentry* f1 = d_lookup(g_root_dentry, &name);
ST_ASSERT(f1 != nullptr);
dput(f1);
/* Drop it */
d_drop(child);
/* Should no longer be findable */
dentry* f2 = d_lookup(g_root_dentry, &name);
ST_ASSERT(f2 == nullptr);
dput(child);
}
st_pass();
/* ---- d_path: root returns "/" ---- */
st_begin("dcache: d_path root returns /");
{
path p{};
p.de = g_root_dentry;
p.mnt = g_root_mnt;
char buf[64];
char* result = d_path(&p, buf, sizeof(buf));
ST_ASSERT(result != nullptr);
ST_ASSERT(result[0] == '/');
ST_ASSERT(result[1] == '\0');
}
st_pass();
/* ---- d_path: child path ---- */
st_begin("dcache: d_path child returns /name");
{
qstr name{};
name.name = "pathtest";
name.len = 8;
name.hash = full_name_hash(name.name, name.len);
dentry* child = d_alloc(g_root_dentry, &name);
ST_ASSERT(child != nullptr);
path p{};
p.de = child;
p.mnt = g_root_mnt;
char buf[64];
char* result = d_path(&p, buf, sizeof(buf));
ST_ASSERT(result != nullptr);
/* Should be "/pathtest" */
const char* expected = "/pathtest";
bool eq = true;
for (int i = 0; expected[i]; i++) {
if (result[i] != expected[i]) { eq = false; break; }
}
ST_ASSERT(eq);
d_drop(child);
dput(child);
}
st_pass();
/* ---- d_alloc_anon: root-like dentry ---- */
st_begin("dcache: d_alloc_anon");
{
dentry* anon = d_alloc_anon(g_root_dentry->d_sb);
ST_ASSERT(anon != nullptr);
ST_ASSERT(anon->d_parent == anon); /* root: parent is self */
ST_ASSERT(anon->d_sb == g_root_dentry->d_sb);
dput(anon);
}
st_pass();
/* ---- full_name_hash: deterministic ---- */
st_begin("dcache: full_name_hash deterministic");
{
uint32 h1 = full_name_hash("hello", 5);
uint32 h2 = full_name_hash("hello", 5);
ST_ASSERT_EQ(h1, h2);
uint32 h3 = full_name_hash("world", 5);
ST_ASSERT_NE(h1, h3);
}
st_pass();
}
|