1
1
/*
2
- * Copyright (c) 2006-2023, RT-Thread Development Team
2
+ * Copyright (c) 2006-2025 RT-Thread Development Team
3
3
*
4
4
* SPDX-License-Identifier: Apache-2.0
5
5
*
@@ -26,6 +26,14 @@ struct dentry_hash_head
26
26
};
27
27
static struct dentry_hash_head hash_head ;
28
28
29
+ /**
30
+ * @brief Calculate hash value for a dentry based on mount point and path
31
+ *
32
+ * @param[in] mnt Pointer to the mount point structure
33
+ * @param[in] path Path string to be hashed (can be NULL)
34
+ *
35
+ * @return uint32_t Calculated hash value within range [0, DFS_DENTRY_HASH_NR-1]
36
+ */
29
37
static uint32_t _dentry_hash (struct dfs_mnt * mnt , const char * path )
30
38
{
31
39
uint32_t val = 0 ;
@@ -40,6 +48,17 @@ static uint32_t _dentry_hash(struct dfs_mnt *mnt, const char *path)
40
48
return (val ^ (unsigned long ) mnt ) & (DFS_DENTRY_HASH_NR - 1 );
41
49
}
42
50
51
+ /**
52
+ * @brief Create a new directory entry (dentry) structure
53
+ *
54
+ * @param[in] mnt Pointer to the mount point structure
55
+ * @param[in] path Path string for the dentry (absolute or relative)
56
+ * @param[in] is_rela_path Flag indicating if path is relative (RT_TRUE) or absolute (RT_FALSE)
57
+ *
58
+ * @return struct dfs_dentry* Pointer to newly created dentry, or NULL if creation failed
59
+ *
60
+ * @note The created dentry will have its ref_count initialized to 1 and DENTRY_IS_ALLOCED flag set
61
+ */
43
62
static struct dfs_dentry * _dentry_create (struct dfs_mnt * mnt , char * path , rt_bool_t is_rela_path )
44
63
{
45
64
struct dfs_dentry * dentry = RT_NULL ;
@@ -75,16 +94,47 @@ static struct dfs_dentry *_dentry_create(struct dfs_mnt *mnt, char *path, rt_boo
75
94
return dentry ;
76
95
}
77
96
97
+ /**
98
+ * @brief Create a new directory entry (dentry) with absolute path
99
+ *
100
+ * @param[in] mnt Pointer to the mount point structure
101
+ * @param[in] fullpath Absolute path string for the dentry
102
+ *
103
+ * @return struct dfs_dentry* Pointer to newly created dentry, or NULL if creation failed
104
+ *
105
+ * @note This is a wrapper for _dentry_create() with is_rela_path set to RT_FALSE
106
+ * @see _dentry_create()
107
+ */
78
108
struct dfs_dentry * dfs_dentry_create (struct dfs_mnt * mnt , char * fullpath )
79
109
{
80
110
return _dentry_create (mnt , fullpath , RT_FALSE );
81
111
}
82
112
113
+ /**
114
+ * @brief Create a new directory entry (dentry) with relative path
115
+ *
116
+ * @param[in] mnt Pointer to the mount point structure
117
+ * @param[in] rela_path Relative path string for the dentry
118
+ *
119
+ * @return struct dfs_dentry* Pointer to newly created dentry, or NULL if creation failed
120
+ *
121
+ * @note This is a wrapper for _dentry_create() with is_rela_path set to RT_TRUE
122
+ * @see _dentry_create()
123
+ */
83
124
struct dfs_dentry * dfs_dentry_create_rela (struct dfs_mnt * mnt , char * rela_path )
84
125
{
85
126
return _dentry_create (mnt , rela_path , RT_TRUE );;
86
127
}
87
128
129
+ /**
130
+ * @brief Increase reference count for a directory entry (dentry)
131
+ *
132
+ * @param[in,out] dentry Pointer to the directory entry structure to be referenced
133
+ *
134
+ * @return struct dfs_dentry* The same dentry pointer that was passed in
135
+ *
136
+ * @note This function will also increase reference count for associated vnode if exists
137
+ */
88
138
struct dfs_dentry * dfs_dentry_ref (struct dfs_dentry * dentry )
89
139
{
90
140
if (dentry )
@@ -104,6 +154,13 @@ struct dfs_dentry * dfs_dentry_ref(struct dfs_dentry *dentry)
104
154
return dentry ;
105
155
}
106
156
157
+ /**
158
+ * @brief Decrease reference count for a directory entry (dentry) and free if count reaches zero
159
+ *
160
+ * @param[in,out] dentry Pointer to the directory entry structure to be unreferenced
161
+ *
162
+ * @return struct dfs_dentry* The same dentry pointer if ref_count > 0, NULL if freed
163
+ */
107
164
struct dfs_dentry * dfs_dentry_unref (struct dfs_dentry * dentry )
108
165
{
109
166
rt_err_t ret = RT_EOK ;
@@ -161,6 +218,14 @@ struct dfs_dentry *dfs_dentry_unref(struct dfs_dentry *dentry)
161
218
return dentry ;
162
219
}
163
220
221
+ /**
222
+ * @brief Look up a directory entry (dentry) in hash table by mount point and path
223
+ *
224
+ * @param[in] mnt Pointer to the mount point structure to search for
225
+ * @param[in] path Path string to search for
226
+ *
227
+ * @return struct dfs_dentry* Pointer to found dentry (with increased ref_count), or NULL if not found
228
+ */
164
229
static struct dfs_dentry * _dentry_hash_lookup (struct dfs_mnt * mnt , const char * path )
165
230
{
166
231
rt_err_t ret = RT_EOK ;
@@ -185,6 +250,11 @@ static struct dfs_dentry *_dentry_hash_lookup(struct dfs_mnt *mnt, const char *p
185
250
return RT_NULL ;
186
251
}
187
252
253
+ /**
254
+ * @brief Insert a directory entry (dentry) into the hash table
255
+ *
256
+ * @param[in,out] dentry Pointer to the directory entry to be inserted
257
+ */
188
258
void dfs_dentry_insert (struct dfs_dentry * dentry )
189
259
{
190
260
dfs_file_lock ();
@@ -193,8 +263,20 @@ void dfs_dentry_insert(struct dfs_dentry *dentry)
193
263
dfs_file_unlock ();
194
264
}
195
265
196
- /*
197
- * lookup a dentry, return this dentry and increase refcount if exist, otherwise return NULL
266
+ /**
267
+ * @brief Look up a directory entry (dentry) in the filesystem
268
+ *
269
+ * @param[in] mnt Pointer to the mount point structure
270
+ * @param[in] path Path string to look up
271
+ * @param[in] flags Additional lookup flags (currently unused)
272
+ *
273
+ * @return struct dfs_dentry* Pointer to found/created dentry (with increased ref_count), or NULL if not found
274
+ *
275
+ * @note This function first searches for dentry in hash table,
276
+ * If not found and filesystem supports lookup operation:
277
+ * - Creates new dentry
278
+ * - Calls filesystem's lookup operation to get vnode
279
+ * - If vnode is successfully obtained, adds dentry to hash table
198
280
*/
199
281
struct dfs_dentry * dfs_dentry_lookup (struct dfs_mnt * mnt , const char * path , uint32_t flags )
200
282
{
@@ -270,6 +352,16 @@ struct dfs_dentry *dfs_dentry_lookup(struct dfs_mnt *mnt, const char *path, uint
270
352
return dentry ;
271
353
}
272
354
355
+ /**
356
+ * @brief Get the full path of a directory entry by combining mount point and relative path
357
+ *
358
+ * @param[in] dentry Pointer to the directory entry structure
359
+ *
360
+ * @return char* Newly allocated string containing full path, or NULL if allocation failed
361
+ *
362
+ * @note The caller is responsible for freeing the returned string using rt_free()
363
+ * @note Handles path concatenation with or without additional '/' separator
364
+ */
273
365
char * dfs_dentry_full_path (struct dfs_dentry * dentry )
274
366
{
275
367
char * path = NULL ;
@@ -298,6 +390,17 @@ char* dfs_dentry_full_path(struct dfs_dentry* dentry)
298
390
return path ;
299
391
}
300
392
393
+ /**
394
+ * @brief Get the parent directory path of a dentry by combining mount point and path
395
+ *
396
+ * @param[in] dentry Pointer to the directory entry structure
397
+ *
398
+ * @return char* Newly allocated string containing parent path, or NULL if allocation failed
399
+ *
400
+ * @note The caller is responsible for freeing the returned string using rt_free()
401
+ * @note Handles both absolute and relative paths correctly
402
+ * @note Returns mount point path if dentry is at root directory
403
+ */
301
404
char * dfs_dentry_pathname (struct dfs_dentry * dentry )
302
405
{
303
406
char * pathname = RT_NULL ;
@@ -332,6 +435,15 @@ char* dfs_dentry_pathname(struct dfs_dentry* dentry)
332
435
return pathname ;
333
436
}
334
437
438
+ /**
439
+ * @brief Calculate CRC32 checksum for the full path of a directory entry
440
+ *
441
+ * @param[in] dentry Pointer to the directory entry structure
442
+ *
443
+ * @return uint32_t CRC32 checksum value of the full path
444
+ *
445
+ * @note Uses standard CRC32 polynomial 0xEDB88320
446
+ */
335
447
uint32_t dfs_dentry_full_path_crc32 (struct dfs_dentry * dentry )
336
448
{
337
449
uint32_t crc32 = 0xFFFFFFFF ;
@@ -354,6 +466,13 @@ uint32_t dfs_dentry_full_path_crc32(struct dfs_dentry* dentry)
354
466
return crc32 ;
355
467
}
356
468
469
+ /**
470
+ * @brief Initialize the dentry hash table
471
+ *
472
+ * @return int Always returns 0 indicating success
473
+ *
474
+ * @note Initializes all hash buckets in the dentry hash table
475
+ */
357
476
int dfs_dentry_init (void )
358
477
{
359
478
int i = 0 ;
@@ -366,6 +485,16 @@ int dfs_dentry_init(void)
366
485
return 0 ;
367
486
}
368
487
488
+ /**
489
+ * @brief Dump all directory entries in the hash table for debugging
490
+ *
491
+ * @param[in] argc Number of command line arguments (unused)
492
+ * @param[in] argv Array of command line arguments (unused)
493
+ *
494
+ * @return int Always returns 0 indicating success
495
+ *
496
+ * @note Prints each dentry's full path, memory address and reference count
497
+ */
369
498
int dfs_dentry_dump (int argc , char * * argv )
370
499
{
371
500
int index = 0 ;
0 commit comments