From 2eca6d25fded19ce93777ecbfdc35116b490180e Mon Sep 17 00:00:00 2001 From: Vincent Dupont Date: Tue, 2 Jan 2018 15:15:53 +0100 Subject: [PATCH] vfs: add format function in vfs api --- sys/include/vfs.h | 25 +++++++++++++++++++++++++ sys/vfs/vfs.c | 45 +++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 68 insertions(+), 2 deletions(-) diff --git a/sys/include/vfs.h b/sys/include/vfs.h index 163c5c027f..12423aaf8b 100644 --- a/sys/include/vfs.h +++ b/sys/include/vfs.h @@ -426,6 +426,16 @@ struct vfs_dir_ops { * Similar, but not equal, to struct super_operations in Linux */ struct vfs_file_system_ops { + /** + * @brief Format the file system on the given mount point + * + * @param[in] mountp file system to format + * + * @return 0 on success + * @return <0 on error + */ + int (*format) (vfs_mount_t *mountp); + /** * @brief Perform any extra processing needed after mounting a file system * @@ -695,6 +705,21 @@ int vfs_readdir(vfs_DIR *dirp, vfs_dirent_t *entry); */ int vfs_closedir(vfs_DIR *dirp); +/** + * @brief Format a file system + * + * @p mountp should have been populated in advance with a file system driver, + * a mount point, and private_data (if the file system driver uses one). + * + * @pre @p mountp must not be mounted + * + * @param[in] mountp pointer to the mount structure of the filesystem to format + * + * @return 0 on success + * @return <0 on error + */ +int vfs_format(vfs_mount_t *mountp); + /** * @brief Mount a file system * diff --git a/sys/vfs/vfs.c b/sys/vfs/vfs.c index 1414472278..3d41af5570 100644 --- a/sys/vfs/vfs.c +++ b/sys/vfs/vfs.c @@ -407,9 +407,18 @@ int vfs_closedir(vfs_DIR *dirp) return res; } -int vfs_mount(vfs_mount_t *mountp) +/** + * @brief Check if the given mount point is mounted + * + * If the mount point is not mounted, _mount_mutex will be locked by this function + * + * @param mountp mount point to check + * @return 0 on success (mount point is valid and not mounted) + * @return -EINVAL if mountp is invalid + * @return -EBUSY if mountp is already mounted + */ +static int check_mount(vfs_mount_t *mountp) { - DEBUG("vfs_mount: %p\n", (void *)mountp); if ((mountp == NULL) || (mountp->fs == NULL) || (mountp->mount_point == NULL)) { return -EINVAL; } @@ -429,11 +438,43 @@ int vfs_mount(vfs_mount_t *mountp) DEBUG("vfs_mount: Already mounted\n"); return -EBUSY; } + + return 0; +} + +int vfs_format(vfs_mount_t *mountp) +{ + DEBUG("vfs_format: %p\n", (void *)mountp); + int ret = check_mount(mountp); + if (ret < 0) { + return ret; + } + mutex_unlock(&_mount_mutex); + + if (mountp->fs->fs_op != NULL) { + if (mountp->fs->fs_op->format != NULL) { + return mountp->fs->fs_op->format(mountp); + } + } + + /* Format operation not supported */ + return -ENOTSUP; +} + +int vfs_mount(vfs_mount_t *mountp) +{ + DEBUG("vfs_mount: %p\n", (void *)mountp); + int ret = check_mount(mountp); + if (ret < 0) { + return ret; + } + if (mountp->fs->fs_op != NULL) { if (mountp->fs->fs_op->mount != NULL) { /* yes, a file system driver does not need to implement mount/umount */ int res = mountp->fs->fs_op->mount(mountp); if (res < 0) { + DEBUG("vfs_mount: error %d\n", res); mutex_unlock(&_mount_mutex); return res; }