Skip to content

Commit 5b544e1

Browse files
pabigotcarlescufi
authored andcommitted
fs: littlefs: define mount point structures for file systems
Use the devicetree filesystem bindings to populate an fs_mount_t object that is preconfigured for a particular set of file system properties on a specified partition. At this time the mount point data is accessed by reference using the partition's devicetree node identifier. Note: While a file system can register itself before its devices are available, it cannot do the automount. In this commit the initialization priority is increased to compensate, but that's not a long-term solution. Signed-off-by: Peter Bigot <[email protected]>
1 parent 4080b77 commit 5b544e1

File tree

1 file changed

+75
-2
lines changed

1 file changed

+75
-2
lines changed

subsys/fs/littlefs_fs.c

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -751,10 +751,83 @@ static const struct fs_file_system_t littlefs_fs = {
751751
.statvfs = littlefs_statvfs,
752752
};
753753

754+
#define DT_DRV_COMPAT zephyr_fstab_littlefs
755+
#define FS_PARTITION(inst) DT_PHANDLE_BY_IDX(DT_DRV_INST(inst), partition, 0)
756+
757+
#define DEFINE_FS(inst) \
758+
static uint8_t __aligned(4) \
759+
read_buffer_##inst[DT_INST_PROP(inst, cache_size)]; \
760+
static uint8_t __aligned(4) \
761+
prog_buffer_##inst[DT_INST_PROP(inst, cache_size)]; \
762+
static uint32_t lookahead_buffer_##inst[DT_INST_PROP(inst, lookahead_size) \
763+
/ sizeof(uint32_t)]; \
764+
BUILD_ASSERT(DT_INST_PROP(inst, read_size) > 0); \
765+
BUILD_ASSERT(DT_INST_PROP(inst, prog_size) > 0); \
766+
BUILD_ASSERT(DT_INST_PROP(inst, cache_size) > 0); \
767+
BUILD_ASSERT(DT_INST_PROP(inst, lookahead_size) > 0); \
768+
BUILD_ASSERT((DT_INST_PROP(inst, lookahead_size) % 8) == 0); \
769+
BUILD_ASSERT((DT_INST_PROP(inst, cache_size) \
770+
% DT_INST_PROP(inst, read_size)) == 0); \
771+
BUILD_ASSERT((DT_INST_PROP(inst, cache_size) \
772+
% DT_INST_PROP(inst, prog_size)) == 0); \
773+
static struct fs_littlefs fs_data_##inst = { \
774+
.cfg = { \
775+
.read_size = DT_INST_PROP(inst, read_size), \
776+
.prog_size = DT_INST_PROP(inst, prog_size), \
777+
.cache_size = DT_INST_PROP(inst, cache_size), \
778+
.lookahead_size = DT_INST_PROP(inst, lookahead_size), \
779+
.read_buffer = read_buffer_##inst, \
780+
.prog_buffer = prog_buffer_##inst, \
781+
.lookahead_buffer = lookahead_buffer_##inst, \
782+
}, \
783+
}; \
784+
struct fs_mount_t FS_FSTAB_ENTRY(DT_DRV_INST(inst)) = { \
785+
.type = FS_LITTLEFS, \
786+
.mnt_point = DT_INST_PROP(inst, mount_point), \
787+
.fs_data = &fs_data_##inst, \
788+
.storage_dev = (void *)DT_FIXED_PARTITION_ID(FS_PARTITION(inst)), \
789+
.flags = FSTAB_ENTRY_DT_MOUNT_FLAGS(DT_DRV_INST(inst)), \
790+
};
791+
792+
DT_INST_FOREACH_STATUS_OKAY(DEFINE_FS)
793+
794+
#define REFERENCE_MOUNT(inst) (&FS_FSTAB_ENTRY(DT_DRV_INST(inst))),
795+
796+
static void mount_init(struct fs_mount_t *mp)
797+
{
798+
799+
LOG_INF("littlefs partition at %s", mp->mnt_point);
800+
if ((mp->flags & FS_MOUNT_FLAG_AUTOMOUNT) != 0) {
801+
int rc = fs_mount(mp);
802+
803+
if (rc < 0) {
804+
LOG_ERR("Automount %s failed: %d\n",
805+
mp->mnt_point, rc);
806+
} else {
807+
LOG_INF("Automount %s succeeded\n",
808+
mp->mnt_point);
809+
}
810+
}
811+
}
812+
754813
static int littlefs_init(const struct device *dev)
755814
{
756815
ARG_UNUSED(dev);
757-
return fs_register(FS_LITTLEFS, &littlefs_fs);
816+
static struct fs_mount_t *partitions[] = {
817+
DT_INST_FOREACH_STATUS_OKAY(REFERENCE_MOUNT)
818+
};
819+
820+
int rc = fs_register(FS_LITTLEFS, &littlefs_fs);
821+
822+
if (rc == 0) {
823+
struct fs_mount_t **mpi = partitions;
824+
825+
while (mpi < (partitions + ARRAY_SIZE(partitions))) {
826+
mount_init(*mpi++);
827+
}
828+
}
829+
830+
return rc;
758831
}
759832

760-
SYS_INIT(littlefs_init, POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT);
833+
SYS_INIT(littlefs_init, POST_KERNEL, 99);

0 commit comments

Comments
 (0)