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 *
@@ -38,6 +38,17 @@ RT_OBJECT_HOOKLIST_DEFINE(dfs_mnt_umnt);
3838 *
3939 */
4040
41+ /**
42+ * @brief Create a new dfs_mnt structure instance.
43+ *
44+ * This function allocates memory to create a new dfs_mnt structure instance and initializes it.
45+ * If the memory allocation is successful, it copies the input path string into the instance and initializes related lists and flags.
46+ *
47+ * @param[in] path The path string to be mounted. This path information will be copied to the newly created dfs_mnt instance.
48+ *
49+ * @return If the memory allocation is successful, returns a pointer to the newly created dfs_mnt structure;
50+ * if the memory allocation fails, returns RT_NULL.
51+ */
4152struct dfs_mnt * dfs_mnt_create (const char * path )
4253{
4354 struct dfs_mnt * mnt = rt_calloc (1 , sizeof (struct dfs_mnt ));
@@ -59,6 +70,18 @@ struct dfs_mnt *dfs_mnt_create(const char *path)
5970 return mnt ;
6071}
6172
73+ /**
74+ * @brief Insert a child mount point into the mount tree.
75+ *
76+ * This function inserts a child mount point into the specified parent mount point's child list.
77+ * If the parent mount point is not provided, it will try to find the appropriate mount point based on the child's path.
78+ * If the child mount point is the root, it will update the global root mount point accordingly.
79+ *
80+ * @param[in,out] mnt Pointer to the parent dfs_mnt structure. If NULL, it will be updated to the appropriate mount point.
81+ * @param[in] child Pointer to the child dfs_mnt structure to be inserted.
82+ *
83+ * @return Always returns 0 to indicate success.
84+ */
6285int dfs_mnt_insert (struct dfs_mnt * mnt , struct dfs_mnt * child )
6386{
6487 if (child )
@@ -112,7 +135,18 @@ int dfs_mnt_insert(struct dfs_mnt* mnt, struct dfs_mnt* child)
112135 return 0 ;
113136}
114137
115- /* remove mnt from mnt_tree */
138+ /**
139+ * @brief Remove a mount point from the mount tree.
140+ *
141+ * This function attempts to remove a specified mount point from the mount tree.
142+ * It can only remove a mount point if it has no child mount points. If the mount point
143+ * has children, it logs a warning message instead of performing the removal.
144+ *
145+ * @param[in] mnt Pointer to the dfs_mnt structure representing the mount point to be removed.
146+ *
147+ * @return Returns RT_EOK if the mount point is successfully removed.
148+ * Returns -RT_ERROR if the mount point has child mount points and cannot be removed.
149+ */
116150int dfs_mnt_remove (struct dfs_mnt * mnt )
117151{
118152 int ret = - RT_ERROR ;
@@ -136,6 +170,19 @@ int dfs_mnt_remove(struct dfs_mnt* mnt)
136170 return ret ;
137171}
138172
173+ /**
174+ * @brief Recursively search for a mount point associated with a specific device ID in the mount tree.
175+ *
176+ * This function traverses the mount tree starting from the given mount point `mnt` to find
177+ * a mount point that is associated with the specified device ID `dev_id`. It uses a depth-first
178+ * search algorithm to iterate through the child mount points.
179+ *
180+ * @param[in] mnt Pointer to the root dfs_mnt structure from which the search will start.
181+ * @param[in] dev_id Pointer to the device ID to search for.
182+ *
183+ * @return If a mount point associated with the given device ID is found, returns a pointer to the corresponding dfs_mnt structure.
184+ * Otherwise, returns RT_NULL.
185+ */
139186static struct dfs_mnt * _dfs_mnt_dev_lookup (struct dfs_mnt * mnt , rt_device_t dev_id )
140187{
141188 struct dfs_mnt * ret = RT_NULL , * iter = RT_NULL ;
@@ -160,6 +207,19 @@ static struct dfs_mnt *_dfs_mnt_dev_lookup(struct dfs_mnt *mnt, rt_device_t dev_
160207 return ret ;
161208}
162209
210+ /**
211+ * @brief Search for a mount point associated with a specific device ID in the mount tree.
212+ *
213+ * This function initiates a search for a mount point that is associated with the specified
214+ * device ID `dev_id` starting from the root mount point. It first checks the root mount point
215+ * directly, and if not found, it recursively searches the entire mount tree using the
216+ * internal helper function `_dfs_mnt_dev_lookup`.
217+ *
218+ * @param[in] dev_id Pointer to the device ID to search for.
219+ *
220+ * @return If a mount point associated with the given device ID is found, returns a pointer to the corresponding dfs_mnt structure.
221+ * Otherwise, returns RT_NULL.
222+ */
163223struct dfs_mnt * dfs_mnt_dev_lookup (rt_device_t dev_id )
164224{
165225 struct dfs_mnt * mnt = _root_mnt ;
@@ -184,12 +244,16 @@ struct dfs_mnt *dfs_mnt_dev_lookup(rt_device_t dev_id)
184244}
185245
186246/**
187- * this function will return the file system mounted on specified path.
247+ * @brief Look up the mount point associated with a given full path.
248+ *
249+ * This function searches the mount tree starting from the root mount point to find
250+ * the most specific mount point that matches the given full path. It traverses down
251+ * the mount tree to identify the deepest mount point that is a prefix of the given path.
188252 *
189- * @param path the specified path string.
253+ * @param[in] fullpath The full path string for which to find the associated mount point .
190254 *
191- * @return the found file system or NULL if no file system mounted on
192- * specified path
255+ * @return If a matching mount point is found, returns a pointer to the corresponding dfs_mnt structure.
256+ * Otherwise, returns RT_NULL.
193257 */
194258struct dfs_mnt * dfs_mnt_lookup (const char * fullpath )
195259{
@@ -236,6 +300,18 @@ struct dfs_mnt *dfs_mnt_lookup(const char *fullpath)
236300 return mnt ;
237301}
238302
303+ /**
304+ * @brief Increase the reference count of a dfs_mnt structure instance.
305+ *
306+ * This function increments the reference count of the specified dfs_mnt structure.
307+ * The reference count is used to track how many parts of the system are currently
308+ * using this mount point.
309+ *
310+ * @param[in,out] mnt Pointer to the dfs_mnt structure whose reference count is to be increased.
311+ * If the pointer is valid, the reference count within the structure will be modified.
312+ * @return Returns the same pointer to the dfs_mnt structure that was passed in.
313+ * If the input pointer is NULL, it simply returns NULL.
314+ */
239315struct dfs_mnt * dfs_mnt_ref (struct dfs_mnt * mnt )
240316{
241317 if (mnt )
@@ -247,6 +323,18 @@ struct dfs_mnt* dfs_mnt_ref(struct dfs_mnt* mnt)
247323 return mnt ;
248324}
249325
326+ /**
327+ * @brief Decrease the reference count of a dfs_mnt structure instance and free it if necessary.
328+ *
329+ * This function decrements the reference count of the specified dfs_mnt structure.
330+ * If the reference count reaches zero after the decrement, it will perform the unmount operation,
331+ * trigger the unmount hook, free the allocated path memory, and finally free the dfs_mnt structure itself.
332+ *
333+ * @param[in,out] mnt Pointer to the dfs_mnt structure whose reference count is to be decreased.
334+ * If the reference count reaches zero, the structure will be freed.
335+ *
336+ * @return returns RT_EOK to indicate success.
337+ */
250338int dfs_mnt_unref (struct dfs_mnt * mnt )
251339{
252340 rt_err_t ret = RT_EOK ;
@@ -286,6 +374,19 @@ int dfs_mnt_unref(struct dfs_mnt *mnt)
286374 return ret ;
287375}
288376
377+ /**
378+ * @brief Set specific flags for a dfs_mnt structure instance.
379+ *
380+ * This function sets specific flags for the given dfs_mnt structure.
381+ * If the MS_RDONLY flag is included in the input flags, it sets the MNT_RDONLY flag
382+ * for the mount point and cleans the page cache if the page cache feature is enabled.
383+ *
384+ * @param[in,out] mnt Pointer to the dfs_mnt structure for which flags are to be set.
385+ * The structure's `flags` member will be modified if necessary.
386+ * @param[in] flags The flags to be set for the mount point. This includes the MS_RDONLY flag.
387+ *
388+ * @return returns 0 to indicate success.
389+ */
289390int dfs_mnt_setflags (struct dfs_mnt * mnt , int flags )
290391{
291392 int error = 0 ;
@@ -301,6 +402,19 @@ int dfs_mnt_setflags(struct dfs_mnt *mnt, int flags)
301402 return error ;
302403}
303404
405+ /**
406+ * @brief Destroy a dfs_mnt structure instance and unmount it if necessary.
407+ *
408+ * This function attempts to destroy the specified dfs_mnt structure instance.
409+ * If the mount point is currently mounted, it marks the mount point as unmounted,
410+ * sets the unmount flag, and removes it from the mount list if it was added.
411+ * Finally, it decreases the reference count of the mount point and frees the
412+ * structure if the reference count reaches zero.
413+ *
414+ * @param[in,out] mnt Pointer to the dfs_mnt structure to be destroyed.
415+ *
416+ * @return Returns RT_EOK to indicate success.
417+ */
304418int dfs_mnt_destroy (struct dfs_mnt * mnt )
305419{
306420 rt_err_t ret = RT_EOK ;
@@ -324,6 +438,23 @@ int dfs_mnt_destroy(struct dfs_mnt* mnt)
324438 return ret ;
325439}
326440
441+ /**
442+ * @brief Recursively traverse the mount point tree and apply a callback function.
443+ *
444+ * This function performs a depth-first traversal of the mount point tree starting from the given mount point.
445+ * It applies the specified callback function to each mount point in the tree. If the callback function returns
446+ * a non-NULL pointer, the traversal stops and the result is returned immediately.
447+ *
448+ * @param[in] mnt Pointer to the root dfs_mnt structure from which the traversal will start.
449+ * If NULL, the function will return RT_NULL without performing any traversal.
450+ * @param[in] func Pointer to the callback function to be applied to each mount point.
451+ * The callback function takes a pointer to a dfs_mnt structure and a generic parameter,
452+ * and returns a pointer to a dfs_mnt structure or RT_NULL.
453+ * @param[in] parameter Generic pointer to a parameter that will be passed to the callback function.
454+ *
455+ * @return If the callback function returns a non-NULL pointer during the traversal, returns that pointer.
456+ * Otherwise, returns RT_NULL.
457+ */
327458static struct dfs_mnt * _dfs_mnt_foreach (struct dfs_mnt * mnt , struct dfs_mnt * (* func )(struct dfs_mnt * mnt , void * parameter ), void * parameter )
328459{
329460 struct dfs_mnt * iter , * ret = NULL ;
@@ -355,6 +486,19 @@ static struct dfs_mnt* _dfs_mnt_foreach(struct dfs_mnt *mnt, struct dfs_mnt* (*f
355486 return ret ;
356487}
357488
489+ /**
490+ * @brief Compare a mount point's device ID with a given device object.
491+ *
492+ * This function checks if the device ID associated with a specified mount point
493+ * matches the given device object. If a match is found, it returns a pointer to
494+ * the corresponding dfs_mnt structure; otherwise, it returns RT_NULL.
495+ *
496+ * @param[in] mnt Pointer to the dfs_mnt structure representing the mount point to be checked.
497+ * @param[in] device Pointer to the device object to compare against the mount point's device ID.
498+ *
499+ * @return If the device ID of the mount point matches the given device object, returns a pointer to the dfs_mnt structure.
500+ * Otherwise, returns RT_NULL.
501+ */
358502static struct dfs_mnt * _mnt_cmp_devid (struct dfs_mnt * mnt , void * device )
359503{
360504 struct dfs_mnt * ret = RT_NULL ;
@@ -374,7 +518,7 @@ static struct dfs_mnt* _mnt_cmp_devid(struct dfs_mnt *mnt, void *device)
374518/**
375519 * this function will return the mounted path for specified device.
376520 *
377- * @param device the device object which is mounted.
521+ * @param[in] device the device object which is mounted.
378522 *
379523 * @return the mounted path or NULL if none device mounted.
380524 */
@@ -396,6 +540,19 @@ const char *dfs_mnt_get_mounted_path(struct rt_device *device)
396540 return path ;
397541}
398542
543+ /**
544+ * @brief Print information about a mount point to the console.
545+ *
546+ * This function is designed to be used as a callback in the mount point tree traversal.
547+ * It prints the file system name, device name (or `(NULL)` if no device is associated),
548+ * mount path, and reference count of the specified mount point to the console using `rt_kprintf`.
549+ *
550+ * @param[in] mnt Pointer to the dfs_mnt structure representing the mount point to be printed.
551+ * If NULL, the function does nothing.
552+ * @param[in] parameter A generic pointer to a parameter. This parameter is not used in this function.
553+ *
554+ * @return Always returns RT_NULL as it is a callback function mainly used for side - effects (printing).
555+ */
399556static struct dfs_mnt * _mnt_dump (struct dfs_mnt * mnt , void * parameter )
400557{
401558 if (mnt )
@@ -415,6 +572,22 @@ static struct dfs_mnt* _mnt_dump(struct dfs_mnt *mnt, void *parameter)
415572 return RT_NULL ;
416573}
417574
575+ /**
576+ * @brief Compare a mount point's full path with a given path.
577+ *
578+ * This function is designed to be used as a callback in the mount point tree traversal.
579+ * It compares the full path of the specified mount point with the given path.
580+ * If the mount point's full path starts with the given path, it returns a pointer to the dfs_mnt structure;
581+ * otherwise, it returns RT_NULL.
582+ *
583+ * @param[in] mnt Pointer to the dfs_mnt structure representing the mount point to be checked.
584+ * If NULL, the function will not perform the comparison and return RT_NULL.
585+ * @param[in] parameter A generic pointer to a parameter, which should be cast to a `const char*`
586+ * representing the path to compare against the mount point's full path.
587+ *
588+ * @return If the mount point's full path starts with the given path, returns a pointer to the dfs_mnt structure.
589+ * Otherwise, returns RT_NULL.
590+ */
418591static struct dfs_mnt * _mnt_cmp_path (struct dfs_mnt * mnt , void * parameter )
419592{
420593 const char * fullpath = (const char * )parameter ;
@@ -428,6 +601,22 @@ static struct dfs_mnt* _mnt_cmp_path(struct dfs_mnt* mnt, void *parameter)
428601 return ret ;
429602}
430603
604+ /**
605+ * @brief Check if a mount point has a child mount point matching the given path.
606+ *
607+ * This function checks whether the specified mount point has a child mount point
608+ * whose full path starts with the given path. It uses a depth-first traversal of
609+ * the mount point tree starting from the provided mount point and applies the
610+ * `_mnt_cmp_path` callback function to each mount point.
611+ *
612+ * @param[in] mnt Pointer to the root dfs_mnt structure from which the search will start.
613+ * If NULL, the function will return RT_FALSE without performing any search.
614+ * @param[in] fullpath The full path string to compare against the child mount points' paths.
615+ * If NULL, the function will return RT_FALSE without performing any search.
616+ *
617+ * @return Returns RT_TRUE if a child mount point with a matching path is found.
618+ * Returns RT_FALSE if no matching child mount point is found, or if either input parameter is NULL.
619+ */
431620rt_bool_t dfs_mnt_has_child_mnt (struct dfs_mnt * mnt , const char * fullpath )
432621{
433622 int ret = RT_FALSE ;
@@ -449,6 +638,19 @@ rt_bool_t dfs_mnt_has_child_mnt(struct dfs_mnt *mnt, const char* fullpath)
449638 return ret ;
450639}
451640
641+ /**
642+ * @brief List all mount points starting from a specified mount point.
643+ *
644+ * This function lists information about all mount points in the mount point tree,
645+ * starting from the specified mount point. If the input mount point is NULL,
646+ * it starts from the root mount point. It uses the `_dfs_mnt_foreach` function
647+ * with the `_mnt_dump` callback to print mount point information.
648+ *
649+ * @param[in] mnt Pointer to the dfs_mnt structure from which to start listing mount points.
650+ * If NULL, the function will start from the root mount point.
651+ *
652+ * @return Always returns 0 to indicate success.
653+ */
452654int dfs_mnt_list (struct dfs_mnt * mnt )
453655{
454656 if (!mnt ) mnt = _root_mnt ;
@@ -462,6 +664,16 @@ int dfs_mnt_list(struct dfs_mnt *mnt)
462664 return 0 ;
463665}
464666
667+ /**
668+ * @brief Traverse all mount points in the mount tree and apply a callback function.
669+ *
670+ * @param[in] func Pointer to the callback function to be applied to each mount point.
671+ * The callback function takes a pointer to a `dfs_mnt` structure and a generic parameter,
672+ * and returns a pointer to a `dfs_mnt` structure or `RT_NULL`.
673+ * @param[in] parameter Generic pointer to a parameter that will be passed to the callback function.
674+ *
675+ * @return Always returns 0.
676+ */
465677int dfs_mnt_foreach (struct dfs_mnt * (* func )(struct dfs_mnt * mnt , void * parameter ), void * parameter )
466678{
467679 /* lock file system */
@@ -471,4 +683,4 @@ int dfs_mnt_foreach(struct dfs_mnt* (*func)(struct dfs_mnt *mnt, void *parameter
471683 dfs_unlock ();
472684
473685 return 0 ;
474- }
686+ }
0 commit comments