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
*
@@ -35,6 +35,17 @@ extern rt_list_t _mnt_list;
35
35
*/
36
36
/*@{*/
37
37
38
+ /**
39
+ * @brief Find a filesystem type by name
40
+ *
41
+ * This function searches the global filesystem type list for a filesystem
42
+ * matching the given name. It returns a pointer to the pointer that holds
43
+ * the matching filesystem type (or the end-of-list pointer if not found).
44
+ *
45
+ * @param[in] name The name of the filesystem type to find
46
+ * @return struct dfs_filesystem_type** Pointer to the pointer containing
47
+ * the matching filesystem type, or the end-of-list pointer if not found
48
+ */
38
49
static struct dfs_filesystem_type * * _find_filesystem (const char * name )
39
50
{
40
51
struct dfs_filesystem_type * * type ;
@@ -47,11 +58,26 @@ static struct dfs_filesystem_type **_find_filesystem(const char *name)
47
58
return type ;
48
59
}
49
60
61
+ /**
62
+ * @brief Get the list of registered filesystem types
63
+ *
64
+ * This function returns a pointer to the head of the global filesystem type list.
65
+ *
66
+ * @return struct dfs_filesystem_type* Pointer to the head of the filesystem type list
67
+ */
50
68
struct dfs_filesystem_type * dfs_filesystems (void )
51
69
{
52
70
return file_systems ;
53
71
}
54
72
73
+ /**
74
+ * @brief Register a filesystem type
75
+ *
76
+ * This function registers a new filesystem type with the global filesystem type list.
77
+ *
78
+ * @param[in] fs Pointer to the filesystem type to register
79
+ * @return int 0 on success, or a negative error code on failure
80
+ */
55
81
int dfs_register (struct dfs_filesystem_type * fs )
56
82
{
57
83
int ret = 0 ;
@@ -71,6 +97,14 @@ int dfs_register(struct dfs_filesystem_type *fs)
71
97
return ret ;
72
98
}
73
99
100
+ /**
101
+ * @brief Unregister a filesystem type
102
+ *
103
+ * This function unregisters a filesystem type from the global filesystem type list.
104
+ *
105
+ * @param[in] fs Pointer to the filesystem type to unregister
106
+ * @return int 0 on success, or a negative error code on failure
107
+ */
74
108
int dfs_unregister (struct dfs_filesystem_type * fs )
75
109
{
76
110
int ret = 0 ;
@@ -95,7 +129,18 @@ int dfs_unregister(struct dfs_filesystem_type *fs)
95
129
return ret ;
96
130
}
97
131
98
- #define REMNT_UNSUPP_FLAGS (~(MS_REMOUNT | MS_RMT_MASK))
132
+ #define REMNT_UNSUPP_FLAGS (~(MS_REMOUNT | MS_RMT_MASK)) /* remount unsupported flags */
133
+
134
+ /**
135
+ * @brief Remount a filesystem
136
+ *
137
+ * This function remounts a filesystem at the specified path with the given flags.
138
+ *
139
+ * @param[in] path The path of the filesystem to remount
140
+ * @param[in] flags The remount flags (see MS_REMOUNT and MS_RMT_MASK)
141
+ * @param[in] data Pointer to additional data required for remounting
142
+ * @return int 0 on success, or a negative error code on failure
143
+ */
99
144
int dfs_remount (const char * path , rt_ubase_t flags , void * data )
100
145
{
101
146
int rc = 0 ;
@@ -149,6 +194,30 @@ int dfs_remount(const char *path, rt_ubase_t flags, void *data)
149
194
* | |
150
195
* |- parent - - + (1 refcount)
151
196
*/
197
+
198
+ /**
199
+ * @brief Mount a filesystem at the specified path
200
+ *
201
+ * This function mounts a filesystem of the specified type at the given path with optional device.
202
+ * It handles both root filesystem mounting and regular filesystem mounting scenarios.
203
+ *
204
+ * @param[in] device_name The name of the device to mount (optional)
205
+ * @param[in] path The path of the mount point
206
+ * @param[in] filesystemtype The type of the filesystem to mount
207
+ * @param[in] rwflag The read/write flags (see MS_RDONLY, MS_RDWR, etc.)
208
+ * @param[in] data Pointer to additional data required for mounting
209
+ *
210
+ * @return int RT_EOK on success, negative error code on failure:
211
+ * - EPERM: Path normalization failed or mount operation failed
212
+ * - ENODEV: Filesystem type not found or device not available
213
+ * - ENOMEM: Memory allocation failure
214
+ * - EIO: Filesystem lacks mount method
215
+ * - ENOTDIR: Mount point doesn't exist
216
+ * - EEXIST: Mount point already mounted
217
+ *
218
+ * @note Special handling for root filesystem ("/")
219
+ * @note Automatic reference counting management for mount points
220
+ */
152
221
int dfs_mount (const char * device_name ,
153
222
const char * path ,
154
223
const char * filesystemtype ,
@@ -162,6 +231,7 @@ int dfs_mount(const char *device_name,
162
231
struct dfs_dentry * mntpoint_dentry = RT_NULL ;
163
232
struct dfs_filesystem_type * type = * _find_filesystem (filesystemtype );
164
233
234
+ /* normalize the mount path */
165
235
if (type )
166
236
{
167
237
fullpath = dfs_normalize_path (RT_NULL , path );
@@ -177,18 +247,22 @@ int dfs_mount(const char *device_name,
177
247
ret = -1 ;
178
248
}
179
249
250
+ /* Main mounting procedure */
180
251
if (fullpath )
181
252
{
182
253
DLOG (note , "mnt" , "mount %s(%s) on path: %s" , device_name , filesystemtype , fullpath );
183
254
184
255
/* open specific device */
185
256
if (device_name ) dev_id = rt_device_find (device_name );
186
257
258
+ /* Check device requirements */
187
259
if (!(type -> fs_ops -> flags & FS_NEED_DEVICE ) ||
188
260
((type -> fs_ops -> flags & FS_NEED_DEVICE ) && dev_id ))
189
261
{
190
262
DLOG (msg , "dfs" , "mnt" , DLOG_MSG , "mnt_parent = dfs_mnt_lookup(%s)" , fullpath );
191
- mnt_parent = dfs_mnt_lookup (fullpath );
263
+ mnt_parent = dfs_mnt_lookup (fullpath ); /* Find parent mount point */
264
+
265
+ /* Handle root filesystem mounting */
192
266
if ((!mnt_parent && (strcmp (fullpath , "/" ) == 0 || strcmp (fullpath , "/dev" ) == 0 ))
193
267
|| (mnt_parent && strcmp (fullpath , "/" ) == 0 && strcmp (mnt_parent -> fullpath , fullpath ) != 0 ))
194
268
{
@@ -197,7 +271,7 @@ int dfs_mount(const char *device_name,
197
271
198
272
/* it's the root file system */
199
273
/* the mount point dentry is the same as root dentry. */
200
-
274
+ /* Create root filesystem mount point */
201
275
DLOG (msg , "dfs" , "mnt" , DLOG_MSG , "mnt_parent = dfs_mnt_create(path)" );
202
276
mnt_parent = dfs_mnt_create (fullpath ); /* mnt->ref_count should be 1. */
203
277
if (mnt_parent )
@@ -214,6 +288,7 @@ int dfs_mount(const char *device_name,
214
288
{
215
289
DLOG (msg , type -> fs_ops -> name , "dfs" , DLOG_MSG_RET , "mount OK, ret root_dentry" );
216
290
291
+ /* Mark as mounted and insert into mount table */
217
292
mnt_child = mnt_parent ;
218
293
mnt_child -> flags |= MNT_IS_MOUNTED ;
219
294
@@ -259,15 +334,15 @@ int dfs_mount(const char *device_name,
259
334
ret = -1 ;
260
335
}
261
336
}
262
- else if (mnt_parent && (strcmp (mnt_parent -> fullpath , fullpath ) != 0 ))
337
+ else if (mnt_parent && (strcmp (mnt_parent -> fullpath , fullpath ) != 0 )) /* Handle regular filesystem mounting */
263
338
{
264
339
DLOG (msg , "dfs" , "dentry" , DLOG_MSG , "mntpoint_dentry = dfs_dentry_lookup(mnt_parent, %s, 0)" , fullpath );
265
- mntpoint_dentry = dfs_dentry_lookup (mnt_parent , fullpath , 0 );
340
+ mntpoint_dentry = dfs_dentry_lookup (mnt_parent , fullpath , 0 ); /* Find mount point directory entry */
266
341
if (mntpoint_dentry )
267
342
{
268
343
DLOG (msg , "dentry" , "dfs" , DLOG_MSG_RET , "dentry exist" );
269
344
DLOG (msg , "dfs" , "mnt" , DLOG_MSG , "mnt_child = dfs_mnt_create(path)" );
270
- mnt_child = dfs_mnt_create (fullpath );
345
+ mnt_child = dfs_mnt_create (fullpath ); /* Create child mount point */
271
346
if (mnt_child )
272
347
{
273
348
LOG_D ("create mnt point %p" , mnt_child );
@@ -344,6 +419,30 @@ int dfs_mount(const char *device_name,
344
419
return ret ;
345
420
}
346
421
422
+ /**
423
+ * @brief Unmount a filesystem from the specified path
424
+ *
425
+ * This function unmounts a filesystem from the given path. It performs the following operations:
426
+ * 1. Normalizes the target path
427
+ * 2. Looks up the mount point
428
+ * 3. Checks if the filesystem can be safely unmounted
429
+ * 4. Performs cleanup operations if unmounting is successful
430
+ *
431
+ * @param[in] specialfile The path of the filesystem to unmount
432
+ * @param[in] flags Unmount flags (MNT_FORCE for forced unmount)
433
+ *
434
+ * @return int RT_EOK on success, negative error code on failure:
435
+ * - EBUSY: Filesystem is busy (in use or has child mounts)
436
+ * - EINVAL: Path is not a mount point
437
+ * - ENOTDIR: Invalid path format
438
+ *
439
+ * @note Forced unmount (MNT_FORCE) can unmount even if reference count > 1
440
+ * @note Automatically handles page cache cleanup if RT_USING_PAGECACHE is enabled
441
+ * @note The function will fail if:
442
+ * - The mount point is locked (MNT_IS_LOCKED)
443
+ * - There are child mounts present
444
+ * - Reference count > 1 and MNT_FORCE not specified
445
+ */
347
446
int dfs_umount (const char * specialfile , int flags )
348
447
{
349
448
int ret = -1 ;
@@ -403,6 +502,16 @@ int dfs_unmount(const char *specialfile)
403
502
return dfs_umount (specialfile , 0 );
404
503
}
405
504
505
+ /**
506
+ * @brief Check if a mount point is mounted
507
+ *
508
+ * This function checks if the given mount point is mounted. It returns 0 if the mount point is mounted,
509
+ * and -1 otherwise.
510
+ *
511
+ * @param[in] mnt The mount point to check
512
+ *
513
+ * @return int 0 if mounted, -1 otherwise
514
+ */
406
515
int dfs_is_mounted (struct dfs_mnt * mnt )
407
516
{
408
517
int ret = 0 ;
@@ -415,6 +524,32 @@ int dfs_is_mounted(struct dfs_mnt *mnt)
415
524
return ret ;
416
525
}
417
526
527
+ /**
528
+ * @brief Create a filesystem on the specified device
529
+ *
530
+ * This function creates a filesystem of the specified type on the given device.
531
+ * It performs the following operations:
532
+ * 1. Looks up the filesystem type
533
+ * 2. Validates device requirements
534
+ * 3. Calls the filesystem-specific mkfs operation
535
+ * 4. Handles page cache cleanup if successful (when RT_USING_PAGECACHE is enabled)
536
+ *
537
+ * @param[in] fs_name Name of the filesystem type to create (e.g., "elm", "romfs")
538
+ * @param[in] device_name Name of the device to create filesystem on (optional)
539
+ *
540
+ * @return int RT_EOK on success, negative error code on failure:
541
+ * - RT_ERROR: General error
542
+ * - ENODEV: Filesystem type not found or device not available
543
+ *
544
+ * @note For filesystems that don't require a device (FS_NEED_DEVICE not set),
545
+ * the device_name parameter can be NULL
546
+ * @note Automatically unmounts any existing filesystem on the device
547
+ * when RT_USING_PAGECACHE is enabled
548
+ * @note The function will fail if:
549
+ * - The filesystem type is not found
550
+ * - Device is required but not found
551
+ * - The filesystem doesn't implement mkfs operation
552
+ */
418
553
int dfs_mkfs (const char * fs_name , const char * device_name )
419
554
{
420
555
rt_device_t dev_id = NULL ;
@@ -468,6 +603,28 @@ int dfs_mkfs(const char *fs_name, const char *device_name)
468
603
return ret ;
469
604
}
470
605
606
+ /**
607
+ * @brief Get filesystem statistics for the specified path
608
+ *
609
+ * This function retrieves filesystem statistics (like total/available space)
610
+ * for the filesystem containing the given path. It performs the following operations:
611
+ * 1. Normalizes the input path
612
+ * 2. Looks up the mount point for the path
613
+ * 3. Calls the filesystem-specific statfs operation if available
614
+ *
615
+ * @param[in] path The path to query filesystem statistics for
616
+ * @param[out] buffer Pointer to statfs structure to store the results
617
+ *
618
+ * @return int RT_EOK on success, negative error code on failure:
619
+ * - RT_ERROR: General error (invalid path or filesystem not found)
620
+ *
621
+ * @note The function will fail if:
622
+ * - The path cannot be normalized
623
+ * - No mount point is found for the path
624
+ * - The filesystem doesn't implement statfs operation
625
+ * - The filesystem is not currently mounted
626
+ * @note The buffer parameter must point to valid memory allocated by the caller
627
+ */
471
628
int dfs_statfs (const char * path , struct statfs * buffer )
472
629
{
473
630
struct dfs_mnt * mnt ;
@@ -499,7 +656,7 @@ int dfs_statfs(const char *path, struct statfs *buffer)
499
656
/**
500
657
* this function will return the mounted path for specified device.
501
658
*
502
- * @param device the device object which is mounted.
659
+ * @param[in] device the device object which is mounted.
503
660
*
504
661
* @return the mounted path or NULL if none device mounted.
505
662
*/
@@ -513,9 +670,9 @@ const char *dfs_filesystem_get_mounted_path(struct rt_device *device)
513
670
/**
514
671
* this function will fetch the partition table on specified buffer.
515
672
*
516
- * @param part the returned partition structure.
517
- * @param buf the buffer contains partition table.
518
- * @param pindex the index of partition table to fetch.
673
+ * @param[out] part the returned partition structure.
674
+ * @param[in] buf the buffer contains partition table.
675
+ * @param[in] pindex the index of partition table to fetch.
519
676
*
520
677
* @return RT_EOK on successful or -RT_ERROR on failed.
521
678
*/
@@ -566,4 +723,4 @@ int dfs_filesystem_get_partition(struct dfs_partition *part,
566
723
return RT_EOK ;
567
724
}
568
725
569
- /* @} */
726
+ /* @} */
0 commit comments