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
*
@@ -38,6 +38,17 @@ RT_OBJECT_HOOKLIST_DEFINE(dfs_mnt_umnt);
38
38
*
39
39
*/
40
40
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
+ */
41
52
struct dfs_mnt * dfs_mnt_create (const char * path )
42
53
{
43
54
struct dfs_mnt * mnt = rt_calloc (1 , sizeof (struct dfs_mnt ));
@@ -59,6 +70,18 @@ struct dfs_mnt *dfs_mnt_create(const char *path)
59
70
return mnt ;
60
71
}
61
72
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
+ */
62
85
int dfs_mnt_insert (struct dfs_mnt * mnt , struct dfs_mnt * child )
63
86
{
64
87
if (child )
@@ -112,7 +135,18 @@ int dfs_mnt_insert(struct dfs_mnt* mnt, struct dfs_mnt* child)
112
135
return 0 ;
113
136
}
114
137
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
+ */
116
150
int dfs_mnt_remove (struct dfs_mnt * mnt )
117
151
{
118
152
int ret = - RT_ERROR ;
@@ -136,6 +170,19 @@ int dfs_mnt_remove(struct dfs_mnt* mnt)
136
170
return ret ;
137
171
}
138
172
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
+ */
139
186
static struct dfs_mnt * _dfs_mnt_dev_lookup (struct dfs_mnt * mnt , rt_device_t dev_id )
140
187
{
141
188
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_
160
207
return ret ;
161
208
}
162
209
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
+ */
163
223
struct dfs_mnt * dfs_mnt_dev_lookup (rt_device_t dev_id )
164
224
{
165
225
struct dfs_mnt * mnt = _root_mnt ;
@@ -184,12 +244,16 @@ struct dfs_mnt *dfs_mnt_dev_lookup(rt_device_t dev_id)
184
244
}
185
245
186
246
/**
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.
188
252
*
189
- * @param path the specified path string.
253
+ * @param[in] fullpath The full path string for which to find the associated mount point .
190
254
*
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.
193
257
*/
194
258
struct dfs_mnt * dfs_mnt_lookup (const char * fullpath )
195
259
{
@@ -236,6 +300,18 @@ struct dfs_mnt *dfs_mnt_lookup(const char *fullpath)
236
300
return mnt ;
237
301
}
238
302
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
+ */
239
315
struct dfs_mnt * dfs_mnt_ref (struct dfs_mnt * mnt )
240
316
{
241
317
if (mnt )
@@ -247,6 +323,18 @@ struct dfs_mnt* dfs_mnt_ref(struct dfs_mnt* mnt)
247
323
return mnt ;
248
324
}
249
325
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
+ */
250
338
int dfs_mnt_unref (struct dfs_mnt * mnt )
251
339
{
252
340
rt_err_t ret = RT_EOK ;
@@ -286,6 +374,19 @@ int dfs_mnt_unref(struct dfs_mnt *mnt)
286
374
return ret ;
287
375
}
288
376
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
+ */
289
390
int dfs_mnt_setflags (struct dfs_mnt * mnt , int flags )
290
391
{
291
392
int error = 0 ;
@@ -301,6 +402,19 @@ int dfs_mnt_setflags(struct dfs_mnt *mnt, int flags)
301
402
return error ;
302
403
}
303
404
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
+ */
304
418
int dfs_mnt_destroy (struct dfs_mnt * mnt )
305
419
{
306
420
rt_err_t ret = RT_EOK ;
@@ -324,6 +438,23 @@ int dfs_mnt_destroy(struct dfs_mnt* mnt)
324
438
return ret ;
325
439
}
326
440
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
+ */
327
458
static struct dfs_mnt * _dfs_mnt_foreach (struct dfs_mnt * mnt , struct dfs_mnt * (* func )(struct dfs_mnt * mnt , void * parameter ), void * parameter )
328
459
{
329
460
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
355
486
return ret ;
356
487
}
357
488
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
+ */
358
502
static struct dfs_mnt * _mnt_cmp_devid (struct dfs_mnt * mnt , void * device )
359
503
{
360
504
struct dfs_mnt * ret = RT_NULL ;
@@ -374,7 +518,7 @@ static struct dfs_mnt* _mnt_cmp_devid(struct dfs_mnt *mnt, void *device)
374
518
/**
375
519
* this function will return the mounted path for specified device.
376
520
*
377
- * @param device the device object which is mounted.
521
+ * @param[in] device the device object which is mounted.
378
522
*
379
523
* @return the mounted path or NULL if none device mounted.
380
524
*/
@@ -396,6 +540,19 @@ const char *dfs_mnt_get_mounted_path(struct rt_device *device)
396
540
return path ;
397
541
}
398
542
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
+ */
399
556
static struct dfs_mnt * _mnt_dump (struct dfs_mnt * mnt , void * parameter )
400
557
{
401
558
if (mnt )
@@ -415,6 +572,22 @@ static struct dfs_mnt* _mnt_dump(struct dfs_mnt *mnt, void *parameter)
415
572
return RT_NULL ;
416
573
}
417
574
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
+ */
418
591
static struct dfs_mnt * _mnt_cmp_path (struct dfs_mnt * mnt , void * parameter )
419
592
{
420
593
const char * fullpath = (const char * )parameter ;
@@ -428,6 +601,22 @@ static struct dfs_mnt* _mnt_cmp_path(struct dfs_mnt* mnt, void *parameter)
428
601
return ret ;
429
602
}
430
603
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
+ */
431
620
rt_bool_t dfs_mnt_has_child_mnt (struct dfs_mnt * mnt , const char * fullpath )
432
621
{
433
622
int ret = RT_FALSE ;
@@ -449,6 +638,19 @@ rt_bool_t dfs_mnt_has_child_mnt(struct dfs_mnt *mnt, const char* fullpath)
449
638
return ret ;
450
639
}
451
640
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
+ */
452
654
int dfs_mnt_list (struct dfs_mnt * mnt )
453
655
{
454
656
if (!mnt ) mnt = _root_mnt ;
@@ -462,6 +664,16 @@ int dfs_mnt_list(struct dfs_mnt *mnt)
462
664
return 0 ;
463
665
}
464
666
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
+ */
465
677
int dfs_mnt_foreach (struct dfs_mnt * (* func )(struct dfs_mnt * mnt , void * parameter ), void * parameter )
466
678
{
467
679
/* lock file system */
@@ -471,4 +683,4 @@ int dfs_mnt_foreach(struct dfs_mnt* (*func)(struct dfs_mnt *mnt, void *parameter
471
683
dfs_unlock ();
472
684
473
685
return 0 ;
474
- }
686
+ }
0 commit comments