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 *
@@ -35,6 +35,17 @@ extern rt_list_t _mnt_list;
3535 */
3636/*@{*/
3737
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+ */
3849static struct dfs_filesystem_type * * _find_filesystem (const char * name )
3950{
4051 struct dfs_filesystem_type * * type ;
@@ -47,11 +58,26 @@ static struct dfs_filesystem_type **_find_filesystem(const char *name)
4758 return type ;
4859}
4960
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+ */
5068struct dfs_filesystem_type * dfs_filesystems (void )
5169{
5270 return file_systems ;
5371}
5472
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+ */
5581int dfs_register (struct dfs_filesystem_type * fs )
5682{
5783 int ret = 0 ;
@@ -71,6 +97,14 @@ int dfs_register(struct dfs_filesystem_type *fs)
7197 return ret ;
7298}
7399
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+ */
74108int dfs_unregister (struct dfs_filesystem_type * fs )
75109{
76110 int ret = 0 ;
@@ -95,7 +129,18 @@ int dfs_unregister(struct dfs_filesystem_type *fs)
95129 return ret ;
96130}
97131
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+ */
99144int dfs_remount (const char * path , rt_ubase_t flags , void * data )
100145{
101146 int rc = 0 ;
@@ -149,6 +194,30 @@ int dfs_remount(const char *path, rt_ubase_t flags, void *data)
149194 * | |
150195 * |- parent - - + (1 refcount)
151196 */
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+ */
152221int dfs_mount (const char * device_name ,
153222 const char * path ,
154223 const char * filesystemtype ,
@@ -162,6 +231,7 @@ int dfs_mount(const char *device_name,
162231 struct dfs_dentry * mntpoint_dentry = RT_NULL ;
163232 struct dfs_filesystem_type * type = * _find_filesystem (filesystemtype );
164233
234+ /* normalize the mount path */
165235 if (type )
166236 {
167237 fullpath = dfs_normalize_path (RT_NULL , path );
@@ -177,18 +247,22 @@ int dfs_mount(const char *device_name,
177247 ret = -1 ;
178248 }
179249
250+ /* Main mounting procedure */
180251 if (fullpath )
181252 {
182253 DLOG (note , "mnt" , "mount %s(%s) on path: %s" , device_name , filesystemtype , fullpath );
183254
184255 /* open specific device */
185256 if (device_name ) dev_id = rt_device_find (device_name );
186257
258+ /* Check device requirements */
187259 if (!(type -> fs_ops -> flags & FS_NEED_DEVICE ) ||
188260 ((type -> fs_ops -> flags & FS_NEED_DEVICE ) && dev_id ))
189261 {
190262 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 */
192266 if ((!mnt_parent && (strcmp (fullpath , "/" ) == 0 || strcmp (fullpath , "/dev" ) == 0 ))
193267 || (mnt_parent && strcmp (fullpath , "/" ) == 0 && strcmp (mnt_parent -> fullpath , fullpath ) != 0 ))
194268 {
@@ -197,7 +271,7 @@ int dfs_mount(const char *device_name,
197271
198272 /* it's the root file system */
199273 /* the mount point dentry is the same as root dentry. */
200-
274+ /* Create root filesystem mount point */
201275 DLOG (msg , "dfs" , "mnt" , DLOG_MSG , "mnt_parent = dfs_mnt_create(path)" );
202276 mnt_parent = dfs_mnt_create (fullpath ); /* mnt->ref_count should be 1. */
203277 if (mnt_parent )
@@ -214,6 +288,7 @@ int dfs_mount(const char *device_name,
214288 {
215289 DLOG (msg , type -> fs_ops -> name , "dfs" , DLOG_MSG_RET , "mount OK, ret root_dentry" );
216290
291+ /* Mark as mounted and insert into mount table */
217292 mnt_child = mnt_parent ;
218293 mnt_child -> flags |= MNT_IS_MOUNTED ;
219294
@@ -259,15 +334,15 @@ int dfs_mount(const char *device_name,
259334 ret = -1 ;
260335 }
261336 }
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 */
263338 {
264339 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 */
266341 if (mntpoint_dentry )
267342 {
268343 DLOG (msg , "dentry" , "dfs" , DLOG_MSG_RET , "dentry exist" );
269344 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 */
271346 if (mnt_child )
272347 {
273348 LOG_D ("create mnt point %p" , mnt_child );
@@ -344,6 +419,30 @@ int dfs_mount(const char *device_name,
344419 return ret ;
345420}
346421
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+ */
347446int dfs_umount (const char * specialfile , int flags )
348447{
349448 int ret = -1 ;
@@ -403,6 +502,16 @@ int dfs_unmount(const char *specialfile)
403502 return dfs_umount (specialfile , 0 );
404503}
405504
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+ */
406515int dfs_is_mounted (struct dfs_mnt * mnt )
407516{
408517 int ret = 0 ;
@@ -415,6 +524,32 @@ int dfs_is_mounted(struct dfs_mnt *mnt)
415524 return ret ;
416525}
417526
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+ */
418553int dfs_mkfs (const char * fs_name , const char * device_name )
419554{
420555 rt_device_t dev_id = NULL ;
@@ -468,6 +603,28 @@ int dfs_mkfs(const char *fs_name, const char *device_name)
468603 return ret ;
469604}
470605
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+ */
471628int dfs_statfs (const char * path , struct statfs * buffer )
472629{
473630 struct dfs_mnt * mnt ;
@@ -499,7 +656,7 @@ int dfs_statfs(const char *path, struct statfs *buffer)
499656/**
500657 * this function will return the mounted path for specified device.
501658 *
502- * @param device the device object which is mounted.
659+ * @param[in] device the device object which is mounted.
503660 *
504661 * @return the mounted path or NULL if none device mounted.
505662 */
@@ -513,9 +670,9 @@ const char *dfs_filesystem_get_mounted_path(struct rt_device *device)
513670/**
514671 * this function will fetch the partition table on specified buffer.
515672 *
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.
519676 *
520677 * @return RT_EOK on successful or -RT_ERROR on failed.
521678 */
@@ -566,4 +723,4 @@ int dfs_filesystem_get_partition(struct dfs_partition *part,
566723 return RT_EOK ;
567724}
568725
569- /* @} */
726+ /* @} */
0 commit comments