1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

sys/vfs: add file-system auto-mount

This commit is contained in:
Benjamin Valentin 2021-12-04 15:23:30 +01:00
parent 70d9f2fb1a
commit 2f6aa71946
6 changed files with 81 additions and 0 deletions

View File

@ -164,6 +164,8 @@ PSEUDOMODULES += suit_transport_%
PSEUDOMODULES += suit_storage_%
PSEUDOMODULES += sys_bus_%
PSEUDOMODULES += vdd_lc_filter_%
PSEUDOMODULES += vfs_auto_format
PSEUDOMODULES += vfs_auto_mount
PSEUDOMODULES += wakaama_objects_%
PSEUDOMODULES += wifi_enterprise
PSEUDOMODULES += xtimer_on_ztimer

View File

@ -499,6 +499,7 @@ ifneq (,$(filter vfs,$(USEMODULE)))
ifeq (native, $(BOARD))
USEMODULE += native_vfs
endif
DEFAULT_MODULE += vfs_auto_mount
endif
ifneq (,$(filter sock_async_event,$(USEMODULE)))

View File

@ -154,6 +154,11 @@ void auto_init(void)
extern void auto_init_devfs(void);
auto_init_devfs();
}
if (IS_USED(MODULE_VFS_AUTO_MOUNT)) {
LOG_DEBUG("Mounting filesystems.\n");
extern void auto_init_vfs(void);
auto_init_vfs();
}
if (IS_USED(MODULE_AUTO_INIT_GNRC_IPV6_NIB)) {
LOG_DEBUG("Auto init gnrc_ipv6_nib.\n");
extern void gnrc_ipv6_nib_init(void);

View File

@ -69,6 +69,7 @@
#include "sched.h"
#include "clist.h"
#include "mtd.h"
#include "xfa.h"
#ifdef __cplusplus
extern "C" {
@ -229,6 +230,36 @@ extern "C" {
*/
#define VFS_ANY_FD (-1)
/**
* @brief Helper macro for VFS_AUTO_MOUNT
*
* @param[in] mtd MTD device to use for filesystem
*/
#define VFS_MTD(mtd) { .dev = &mtd.base }
/**
* @brief Define an automatic mountpoint
*
* @param[in] type file system type
* Can be littlefs, littlefs2, spiffs or fatfs
*
* Internally, file systems supporting this must name their
* @ref vfs_file_system_t `${TYPE}_file_system`, and must use
* a type named `${TYPE}_desc_t` for their private data
* @param[in] mtd file system backed device configuration
* @param[in] path Mount path
* @param[in] idx Unique index of the mount point
*/
#define VFS_AUTO_MOUNT(type, mtd, path, idx) \
static type ## _desc_t fs_desc_ ## idx = mtd; \
\
XFA(vfs_mountpoints_xfa, 0) \
vfs_mount_t _mount_mtd_ ## idx = { \
.fs = &type ## _file_system, \
.mount_point = path, \
.private_data = &fs_desc_ ## idx, \
}
/* Forward declarations */
/**
* @brief struct @c vfs_file_ops typedef

View File

@ -9,3 +9,12 @@ config MODULE_VFS
bool "Virtual File System (VFS)"
depends on TEST_KCONFIG
select MODULE_POSIX_HEADERS
config MODULE_VFS_AUTO_MOUNT
bool "Automatically mount configured file systems"
depends on MODULE_VFS
default y
config MODULE_VFS_AUTO_FORMAT
bool "Automatically format configured file systems if mount fails"
depends on MODULE_VFS

View File

@ -41,6 +41,16 @@
#define DEBUG_NOT_STDOUT(...)
#endif
/**
* @brief Automatic mountpoints
*/
XFA_INIT(vfs_mount_t, vfs_mountpoints_xfa);
/**
* @brief Number of automatic mountpoints
*/
#define MOUNTPOINTS_NUMOF XFA_LEN(vfs_mount_t, vfs_mountpoints_xfa)
/**
* @internal
* @brief Array of all currently open files
@ -1018,4 +1028,27 @@ int vfs_sysop_stat_from_fstat(vfs_mount_t *mountp, const char *restrict path, st
return err;
}
void auto_init_vfs(void)
{
for (unsigned i = 0; i < MOUNTPOINTS_NUMOF; ++i) {
DEBUG("vfs%u: mounting as '%s'\n", i, vfs_mountpoints_xfa[i].mount_point);
int res = vfs_mount(&vfs_mountpoints_xfa[i]);
if (res) {
if (IS_ACTIVE(MODULE_VFS_AUTO_FORMAT)) {
DEBUG("vfs%u: formatting…\n", i);
res = vfs_format(&vfs_mountpoints_xfa[i]);
if (res) {
DEBUG("vfs%u: format: error %d\n", i, res);
continue;
}
res = vfs_mount(&vfs_mountpoints_xfa[i]);
}
if (res) {
DEBUG("vfs%u mount: error %d\n", i, res);
}
}
}
}
/** @} */