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
515
|
/*
* Copyright (c) 2006-2025 RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2022-10-10 Bernard The first version of rewrite dfs
*/
#include <rtthread.h>
#include "dfs.h"
#include "dfs_file.h"
#include "dfs_private.h"
#include "dfs_dentry.h"
#include "dfs_mnt.h"
#define DBG_TAG "DFS.dentry"
#define DBG_LVL DBG_WARNING
#include <rtdbg.h>
#define DFS_DENTRY_HASH_NR 32
struct dentry_hash_head
{
rt_list_t head[DFS_DENTRY_HASH_NR];
};
static struct dentry_hash_head hash_head;
/**
* @brief Calculate hash value for a dentry based on mount point and path
*
* @param[in] mnt Pointer to the mount point structure
* @param[in] path Path string to be hashed (can be NULL)
*
* @return uint32_t Calculated hash value within range [0, DFS_DENTRY_HASH_NR-1]
*/
static uint32_t _dentry_hash(struct dfs_mnt *mnt, const char *path)
{
uint32_t val = 0;
if (path)
{
while (*path)
{
val = ((val << 5) + val) + *path++;
}
}
return (val ^ (unsigned long) mnt) & (DFS_DENTRY_HASH_NR - 1);
}
/**
* @brief Create a new directory entry (dentry) structure
*
* @param[in] mnt Pointer to the mount point structure
* @param[in] path Path string for the dentry (absolute or relative)
* @param[in] is_rela_path Flag indicating if path is relative (RT_TRUE) or absolute (RT_FALSE)
*
* @return struct dfs_dentry* Pointer to newly created dentry, or NULL if creation failed
*
* @note The created dentry will have its ref_count initialized to 1 and DENTRY_IS_ALLOCED flag set
*/
static struct dfs_dentry *_dentry_create(struct dfs_mnt *mnt, char *path, rt_bool_t is_rela_path)
{
struct dfs_dentry *dentry = RT_NULL;
if (mnt == RT_NULL || path == RT_NULL)
{
return dentry;
}
dentry = (struct dfs_dentry *)rt_calloc(1, sizeof(struct dfs_dentry));
if (dentry)
{
char *dentry_path = path;
if (!is_rela_path)
{
int mntpoint_len = strlen(mnt->fullpath);
if (rt_strncmp(mnt->fullpath, dentry_path, mntpoint_len) == 0)
{
dentry_path += mntpoint_len;
}
}
dentry->pathname = strlen(dentry_path) ? rt_strdup(dentry_path) : rt_strdup(path);
dentry->mnt = dfs_mnt_ref(mnt);
rt_atomic_store(&(dentry->ref_count), 1);
dentry->flags |= DENTRY_IS_ALLOCED;
LOG_I("create a dentry:%p for %s", dentry, mnt->fullpath);
}
return dentry;
}
/**
* @brief Create a new directory entry (dentry) with absolute path
*
* @param[in] mnt Pointer to the mount point structure
* @param[in] fullpath Absolute path string for the dentry
*
* @return struct dfs_dentry* Pointer to newly created dentry, or NULL if creation failed
*
* @note This is a wrapper for _dentry_create() with is_rela_path set to RT_FALSE
* @see _dentry_create()
*/
struct dfs_dentry *dfs_dentry_create(struct dfs_mnt *mnt, char *fullpath)
{
return _dentry_create(mnt, fullpath, RT_FALSE);
}
/**
* @brief Create a new directory entry (dentry) with relative path
*
* @param[in] mnt Pointer to the mount point structure
* @param[in] rela_path Relative path string for the dentry
*
* @return struct dfs_dentry* Pointer to newly created dentry, or NULL if creation failed
*
* @note This is a wrapper for _dentry_create() with is_rela_path set to RT_TRUE
* @see _dentry_create()
*/
struct dfs_dentry *dfs_dentry_create_rela(struct dfs_mnt *mnt, char *rela_path)
{
return _dentry_create(mnt, rela_path, RT_TRUE);;
}
/**
* @brief Increase reference count for a directory entry (dentry)
*
* @param[in,out] dentry Pointer to the directory entry structure to be referenced
*
* @return struct dfs_dentry* The same dentry pointer that was passed in
*
* @note This function will also increase reference count for associated vnode if exists
*/
struct dfs_dentry * dfs_dentry_ref(struct dfs_dentry *dentry)
{
if (dentry)
{
int ret = dfs_file_lock();
if (ret == RT_EOK)
{
rt_atomic_add(&(dentry->ref_count), 1);
if (dentry->vnode)
{
rt_atomic_add(&(dentry->vnode->ref_count), 1);
}
dfs_file_unlock();
}
}
return dentry;
}
/**
* @brief Decrease reference count for a directory entry (dentry) and free if count reaches zero
*
* @param[in,out] dentry Pointer to the directory entry structure to be unreferenced
*
* @return struct dfs_dentry* The same dentry pointer if ref_count > 0, NULL if freed
*/
struct dfs_dentry *dfs_dentry_unref(struct dfs_dentry *dentry)
{
rt_err_t ret = RT_EOK;
if (dentry)
{
ret = dfs_file_lock();
if (ret == RT_EOK)
{
if (dentry->flags & DENTRY_IS_ALLOCED)
{
rt_atomic_sub(&(dentry->ref_count), 1);
}
if (rt_atomic_load(&(dentry->ref_count)) == 0)
{
DLOG(msg, "dentry", "dentry", DLOG_MSG, "free dentry, ref_count=0");
if (dentry->flags & DENTRY_IS_ADDHASH)
{
rt_list_remove(&dentry->hashlist);
}
/* release vnode */
if (dentry->vnode)
{
dfs_vnode_unref(dentry->vnode);
}
/* release mnt */
DLOG(msg, "dentry", "mnt", DLOG_MSG, "dfs_mnt_unref(dentry->mnt)");
if (dentry->mnt)
{
dfs_mnt_unref(dentry->mnt);
}
dfs_file_unlock();
LOG_I("free a dentry: %p", dentry);
rt_free(dentry->pathname);
rt_free(dentry);
dentry = RT_NULL;
}
else
{
if (dentry->vnode)
{
rt_atomic_sub(&(dentry->vnode->ref_count), 1);
}
dfs_file_unlock();
DLOG(note, "dentry", "dentry ref_count=%d", rt_atomic_load(&(dentry->ref_count)));
}
}
}
return dentry;
}
/**
* @brief Look up a directory entry (dentry) in hash table by mount point and path
*
* @param[in] mnt Pointer to the mount point structure to search for
* @param[in] path Path string to search for
*
* @return struct dfs_dentry* Pointer to found dentry (with increased ref_count), or NULL if not found
*/
static struct dfs_dentry *_dentry_hash_lookup(struct dfs_mnt *mnt, const char *path)
{
rt_err_t ret = RT_EOK;
struct dfs_dentry *entry = RT_NULL;
ret = dfs_file_lock();
if (ret == RT_EOK)
{
rt_list_for_each_entry(entry, &hash_head.head[_dentry_hash(mnt, path)], hashlist)
{
if (entry->mnt == mnt && !strcmp(entry->pathname, path))
{
dfs_dentry_ref(entry);
dfs_file_unlock();
return entry;
}
}
dfs_file_unlock();
}
return RT_NULL;
}
/**
* @brief Insert a directory entry (dentry) into the hash table
*
* @param[in,out] dentry Pointer to the directory entry to be inserted
*/
void dfs_dentry_insert(struct dfs_dentry *dentry)
{
dfs_file_lock();
rt_list_insert_after(&hash_head.head[_dentry_hash(dentry->mnt, dentry->pathname)], &dentry->hashlist);
dentry->flags |= DENTRY_IS_ADDHASH;
dfs_file_unlock();
}
/**
* @brief Look up a directory entry (dentry) in the filesystem
*
* @param[in] mnt Pointer to the mount point structure
* @param[in] path Path string to look up
* @param[in] flags Additional lookup flags (currently unused)
*
* @return struct dfs_dentry* Pointer to found/created dentry (with increased ref_count), or NULL if not found
*
* @note This function first searches for dentry in hash table,
* If not found and filesystem supports lookup operation:
* - Creates new dentry
* - Calls filesystem's lookup operation to get vnode
* - If vnode is successfully obtained, adds dentry to hash table
*/
struct dfs_dentry *dfs_dentry_lookup(struct dfs_mnt *mnt, const char *path, uint32_t flags)
{
struct dfs_dentry *dentry;
struct dfs_vnode *vnode = RT_NULL;
int mntpoint_len = strlen(mnt->fullpath);
if (rt_strncmp(mnt->fullpath, path, mntpoint_len) == 0)
{
path += mntpoint_len;
if ((*path) == '\0')
{
/* root */
path = "/";
}
}
dfs_file_lock();
dentry = _dentry_hash_lookup(mnt, path);
if (!dentry)
{
if (mnt->fs_ops->lookup)
{
DLOG(activate, "dentry");
/* not in hash table, create it */
DLOG(msg, "dentry", "dentry", DLOG_MSG, "dfs_dentry_create_rela(mnt=%s, path=%s)", mnt->fullpath, path);
dentry = dfs_dentry_create_rela(mnt, (char*)path);
if (dentry)
{
DLOG(msg, "dentry", mnt->fs_ops->name, DLOG_MSG, "vnode=fs_ops->lookup(dentry)");
if (dfs_is_mounted(mnt) == 0)
{
vnode = mnt->fs_ops->lookup(dentry);
}
if (vnode)
{
DLOG(msg, mnt->fs_ops->name, "dentry", DLOG_MSG_RET, "return vnode");
dentry->vnode = vnode; /* the refcount of created vnode is 1. no need to reference */
dfs_file_lock();
rt_list_insert_after(&hash_head.head[_dentry_hash(mnt, path)], &dentry->hashlist);
dentry->flags |= DENTRY_IS_ADDHASH;
dfs_file_unlock();
if (dentry->flags & (DENTRY_IS_ALLOCED | DENTRY_IS_ADDHASH)
&& !(dentry->flags & DENTRY_IS_OPENED))
{
rt_err_t ret = dfs_file_lock();
if (ret == RT_EOK)
{
dentry->flags |= DENTRY_IS_OPENED;
dfs_file_unlock();
}
}
}
else
{
DLOG(msg, mnt->fs_ops->name, "dentry", DLOG_MSG_RET, "no dentry");
DLOG(msg, "dentry", "dentry", DLOG_MSG, "dfs_dentry_unref(dentry)");
dfs_dentry_unref(dentry);
dentry = RT_NULL;
}
}
DLOG(deactivate, "dentry");
}
}
else
{
DLOG(note, "dentry", "found dentry");
}
dfs_file_unlock();
return dentry;
}
/**
* @brief Get the full path of a directory entry by combining mount point and relative path
*
* @param[in] dentry Pointer to the directory entry structure
*
* @return char* Newly allocated string containing full path, or NULL if allocation failed
*
* @note The caller is responsible for freeing the returned string using rt_free()
* @note Handles path concatenation with or without additional '/' separator
*/
char* dfs_dentry_full_path(struct dfs_dentry* dentry)
{
char *path = NULL;
if (dentry && dentry->mnt)
{
int mnt_len = strlen(dentry->mnt->fullpath);
int path_len = strlen(dentry->pathname);
path = (char *) rt_malloc(mnt_len + path_len + 3);
if (path)
{
if (dentry->pathname[0] == '/' || dentry->mnt->fullpath[mnt_len - 1] == '/')
{
rt_snprintf(path, mnt_len + path_len + 2, "%s%s", dentry->mnt->fullpath,
dentry->pathname);
}
else
{
rt_snprintf(path, mnt_len + path_len + 2, "%s/%s", dentry->mnt->fullpath,
dentry->pathname);
}
}
}
return path;
}
/**
* @brief Get the parent directory path of a dentry by combining mount point and path
*
* @param[in] dentry Pointer to the directory entry structure
*
* @return char* Newly allocated string containing parent path, or NULL if allocation failed
*
* @note The caller is responsible for freeing the returned string using rt_free()
* @note Handles both absolute and relative paths correctly
* @note Returns mount point path if dentry is at root directory
*/
char* dfs_dentry_pathname(struct dfs_dentry* dentry)
{
char *pathname = RT_NULL;
char *index = RT_NULL;
index = strrchr(dentry->pathname, '/');
if (index)
{
int length = index - dentry->pathname;
int path_length = strlen(dentry->mnt->fullpath) + length + 3;
pathname = (char*) rt_malloc(path_length);
if (pathname)
{
if (dentry->pathname[0] == '/')
{
rt_snprintf(pathname, path_length - 1, "%s%.*s", dentry->mnt->fullpath,
length, dentry->pathname);
}
else
{
rt_snprintf(pathname, path_length - 1, "%s/%.*s", dentry->mnt->fullpath,
length, dentry->pathname);
}
}
}
else
{
pathname = rt_strdup(dentry->mnt->fullpath);
}
return pathname;
}
/**
* @brief Calculate CRC32 checksum for the full path of a directory entry
*
* @param[in] dentry Pointer to the directory entry structure
*
* @return uint32_t CRC32 checksum value of the full path
*
* @note Uses standard CRC32 polynomial 0xEDB88320
*/
uint32_t dfs_dentry_full_path_crc32(struct dfs_dentry* dentry)
{
uint32_t crc32 = 0xFFFFFFFF;
char *fullpath = dfs_dentry_full_path(dentry);
if (fullpath)
{
int i = 0;
while(fullpath[i] != '\0')
{
for (uint8_t b = 1; b; b <<= 1)
{
crc32 ^= (fullpath[i] & b) ? 1 : 0;
crc32 = (crc32 & 1) ? crc32 >> 1 ^ 0xEDB88320 : crc32 >> 1;
}
i ++;
}
rt_free(fullpath);
}
return crc32;
}
/**
* @brief Initialize the dentry hash table
*
* @return int Always returns 0 indicating success
*
* @note Initializes all hash buckets in the dentry hash table
*/
int dfs_dentry_init(void)
{
int i = 0;
for(i = 0; i < DFS_DENTRY_HASH_NR; i++)
{
rt_list_init(&hash_head.head[i]);
}
return 0;
}
/**
* @brief Dump all directory entries in the hash table for debugging
*
* @param[in] argc Number of command line arguments (unused)
* @param[in] argv Array of command line arguments (unused)
*
* @return int Always returns 0 indicating success
*
* @note Prints each dentry's full path, memory address and reference count
*/
int dfs_dentry_dump(int argc, char** argv)
{
int index = 0;
struct dfs_dentry *entry = RT_NULL;
dfs_lock();
for (index = 0; index < DFS_DENTRY_HASH_NR; index ++)
{
rt_list_for_each_entry(entry, &hash_head.head[index], hashlist)
{
printf("dentry: %s%s @ %p, ref_count = %zd\n", entry->mnt->fullpath, entry->pathname, entry, (size_t)rt_atomic_load(&entry->ref_count));
}
}
dfs_unlock();
return 0;
}
MSH_CMD_EXPORT_ALIAS(dfs_dentry_dump, dentry_dump, dump dentry in the system);
|