11/*
2- * Copyright (c) 2006-2023, RT-Thread Development Team
2+ * Copyright (c) 2006-2025 RT-Thread Development Team
33 *
44 * SPDX-License-Identifier: Apache-2.0
55 *
@@ -26,6 +26,14 @@ struct dentry_hash_head
2626};
2727static struct dentry_hash_head hash_head ;
2828
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+ */
2937static uint32_t _dentry_hash (struct dfs_mnt * mnt , const char * path )
3038{
3139 uint32_t val = 0 ;
@@ -40,6 +48,17 @@ static uint32_t _dentry_hash(struct dfs_mnt *mnt, const char *path)
4048 return (val ^ (unsigned long ) mnt ) & (DFS_DENTRY_HASH_NR - 1 );
4149}
4250
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+ */
4362static struct dfs_dentry * _dentry_create (struct dfs_mnt * mnt , char * path , rt_bool_t is_rela_path )
4463{
4564 struct dfs_dentry * dentry = RT_NULL ;
@@ -75,16 +94,47 @@ static struct dfs_dentry *_dentry_create(struct dfs_mnt *mnt, char *path, rt_boo
7594 return dentry ;
7695}
7796
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+ */
78108struct dfs_dentry * dfs_dentry_create (struct dfs_mnt * mnt , char * fullpath )
79109{
80110 return _dentry_create (mnt , fullpath , RT_FALSE );
81111}
82112
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+ */
83124struct dfs_dentry * dfs_dentry_create_rela (struct dfs_mnt * mnt , char * rela_path )
84125{
85126 return _dentry_create (mnt , rela_path , RT_TRUE );;
86127}
87128
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+ */
88138struct dfs_dentry * dfs_dentry_ref (struct dfs_dentry * dentry )
89139{
90140 if (dentry )
@@ -104,6 +154,13 @@ struct dfs_dentry * dfs_dentry_ref(struct dfs_dentry *dentry)
104154 return dentry ;
105155}
106156
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+ */
107164struct dfs_dentry * dfs_dentry_unref (struct dfs_dentry * dentry )
108165{
109166 rt_err_t ret = RT_EOK ;
@@ -161,6 +218,14 @@ struct dfs_dentry *dfs_dentry_unref(struct dfs_dentry *dentry)
161218 return dentry ;
162219}
163220
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+ */
164229static struct dfs_dentry * _dentry_hash_lookup (struct dfs_mnt * mnt , const char * path )
165230{
166231 rt_err_t ret = RT_EOK ;
@@ -185,6 +250,11 @@ static struct dfs_dentry *_dentry_hash_lookup(struct dfs_mnt *mnt, const char *p
185250 return RT_NULL ;
186251}
187252
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+ */
188258void dfs_dentry_insert (struct dfs_dentry * dentry )
189259{
190260 dfs_file_lock ();
@@ -193,8 +263,20 @@ void dfs_dentry_insert(struct dfs_dentry *dentry)
193263 dfs_file_unlock ();
194264}
195265
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
198280 */
199281struct dfs_dentry * dfs_dentry_lookup (struct dfs_mnt * mnt , const char * path , uint32_t flags )
200282{
@@ -270,6 +352,16 @@ struct dfs_dentry *dfs_dentry_lookup(struct dfs_mnt *mnt, const char *path, uint
270352 return dentry ;
271353}
272354
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+ */
273365char * dfs_dentry_full_path (struct dfs_dentry * dentry )
274366{
275367 char * path = NULL ;
@@ -298,6 +390,17 @@ char* dfs_dentry_full_path(struct dfs_dentry* dentry)
298390 return path ;
299391}
300392
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+ */
301404char * dfs_dentry_pathname (struct dfs_dentry * dentry )
302405{
303406 char * pathname = RT_NULL ;
@@ -332,6 +435,15 @@ char* dfs_dentry_pathname(struct dfs_dentry* dentry)
332435 return pathname ;
333436}
334437
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+ */
335447uint32_t dfs_dentry_full_path_crc32 (struct dfs_dentry * dentry )
336448{
337449 uint32_t crc32 = 0xFFFFFFFF ;
@@ -354,6 +466,13 @@ uint32_t dfs_dentry_full_path_crc32(struct dfs_dentry* dentry)
354466 return crc32 ;
355467}
356468
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+ */
357476int dfs_dentry_init (void )
358477{
359478 int i = 0 ;
@@ -366,6 +485,16 @@ int dfs_dentry_init(void)
366485 return 0 ;
367486}
368487
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+ */
369498int dfs_dentry_dump (int argc , char * * argv )
370499{
371500 int index = 0 ;
0 commit comments