Extended lfsr_mount to accept mount flags

This has been a long-time coming, mount flags are just too useful for
configuring a filesystem at runtime.

Currently this is limited to LFS_M_RDONLY and LFS_M_CKPROGS, but there
are a few more planned in the future:

  LFS_M_RDWR     = 0x0000, // Mount the filesystem as read and write
  LFS_M_RDONLY   = 0x0001, // Mount the filesystem as readonly
  LFS_M_STRICT*  = 0x0002, // Error if on-disk config does not match
  LFS_M_FORCE*   = 0x0004, // Ignore compat flags, mount readonly
  LFS_M_FORCEWITHRECKLESSABANDON*
                 = 0x0008, // Ignore compat flags, mount read write

  LFS_M_CKPROGS  = 0x0010, // Check progs by reading back progged data
  LFS_M_CKREADS* = 0x0020, // Check reads via checksums

  * Hypothetical

As a convenience, we also return mount flags in the struct lfs_fsinfo's
flags field as their relevant LFS_I_* variants. Though only to match
statvfs, and only because it's cheap, littlefs's API is low-level and we
should expect users to know what flags they passed to lfsr_mount.

As for the new mount flags:

- LFS_M_RDONLY - For consistency with existing APIs, this just asserts
  on write operations, which makes it a bit useless... But the info flag
  LFS_I_RDONLY may be useful for falling back to a readonly mode if
  we encounter on-disk compat issues.

  At least if implement the theoretical LFS_UNTRUSTED_USER mode
  LFS_M_RDONLY could become a runtime error.

- LFS_M_RDWR - This really just exists to compliment LFS_M_RDONLY and to
  match LFS_O_RDONLY/LFS_O_RDWR. It's just an alias for 0, and I don't
  think there will ever be a reason to make it non-0 (but I can always
  be wrong!).

- LFS_M_CKPROGS - This replaces the check_progs config option and avoids
  using a full byte to store a bool.

  We should probably also have a compile-time option to compile this out
  (LFS_NO_CKPROGS?), but that's a future thing to do.

This ended up adding a surprising bit of code, considering we're just
moving flags around, and noise in lfs_alloc added a bit of stack again:

           code          stack
  before: 35880           2672
  after:  35932 (+0.1%)   2680 (+0.3%)
This commit is contained in:
Christopher Haster
2024-07-15 01:48:37 -05:00
parent 0a3cb2dd3a
commit acfae9e072
25 changed files with 1322 additions and 1126 deletions
+29 -28
View File
@@ -40,6 +40,7 @@ code = '''
#define lfsp_config lfs_config
#define LFSP_ERR_NOENT LFS_ERR_NOENT
#define lfsp_format lfsr_format
#define LFSP_M_RDWR LFS_M_RDWR
#define lfsp_mount lfsr_mount
#define lfsp_unmount lfsr_unmount
#define lfsp_fsinfo lfs_fsinfo
@@ -84,14 +85,14 @@ code = '''
lfsp_format(&lfsp, &cfgp) => 0;
// confirm the previous mount works
lfsp_mount(&lfsp, &cfgp) => 0;
lfsp_mount(&lfsp, LFSP_M_RDWR, &cfgp) => 0;
lfsp_unmount(&lfsp) => 0;
//////
// now mount with new version
lfs_t lfs;
lfsr_mount(&lfs, CFG) => 0;
lfsr_mount(&lfs, LFS_M_RDWR, CFG) => 0;
lfsr_unmount(&lfs) => 0;
'''
@@ -111,7 +112,7 @@ code = '''
lfsp_format(&lfsp, &cfgp) => 0;
// write COUNT dirs
lfsp_mount(&lfsp, &cfgp) => 0;
lfsp_mount(&lfsp, LFSP_M_RDWR, &cfgp) => 0;
for (lfs_size_t i = 0; i < COUNT; i++) {
char name[8];
sprintf(name, "dir%03d", i);
@@ -123,7 +124,7 @@ code = '''
// now mount with new version
lfs_t lfs;
lfsr_mount(&lfs, CFG) => 0;
lfsr_mount(&lfs, LFS_M_RDWR, CFG) => 0;
// can we list the directories?
lfsr_dir_t dir;
@@ -171,7 +172,7 @@ code = '''
lfsp_format(&lfsp, &cfgp) => 0;
// write COUNT files
lfsp_mount(&lfsp, &cfgp) => 0;
lfsp_mount(&lfsp, LFSP_M_RDWR, &cfgp) => 0;
uint32_t prng = 42;
for (lfs_size_t i = 0; i < COUNT; i++) {
lfsp_file_t file;
@@ -195,7 +196,7 @@ code = '''
// now mount with new version
lfs_t lfs;
lfsr_mount(&lfs, CFG) => 0;
lfsr_mount(&lfs, LFS_M_RDWR, CFG) => 0;
// can we list the files?
lfsr_dir_t dir;
@@ -261,7 +262,7 @@ code = '''
lfsp_format(&lfsp, &cfgp) => 0;
// write COUNT files+dirs
lfsp_mount(&lfsp, &cfgp) => 0;
lfsp_mount(&lfsp, LFSP_M_RDWR, &cfgp) => 0;
uint32_t prng = 42;
for (lfs_size_t i = 0; i < COUNT; i++) {
char name[16];
@@ -288,7 +289,7 @@ code = '''
// now mount with new version
lfs_t lfs;
lfsr_mount(&lfs, CFG) => 0;
lfsr_mount(&lfs, LFS_M_RDWR, CFG) => 0;
// can we list the directories?
lfsr_dir_t dir;
@@ -378,7 +379,7 @@ code = '''
lfsp_format(&lfsp, &cfgp) => 0;
// write COUNT/2 dirs
lfsp_mount(&lfsp, &cfgp) => 0;
lfsp_mount(&lfsp, LFSP_M_RDWR, &cfgp) => 0;
for (lfs_size_t i = 0; i < COUNT/2; i++) {
char name[8];
sprintf(name, "dir%03d", i);
@@ -390,7 +391,7 @@ code = '''
// now mount with new version
lfs_t lfs;
lfsr_mount(&lfs, CFG) => 0;
lfsr_mount(&lfs, LFS_M_RDWR, CFG) => 0;
// write another COUNT/2 dirs
for (lfs_size_t i = COUNT/2; i < COUNT; i++) {
@@ -445,7 +446,7 @@ code = '''
lfsp_format(&lfsp, &cfgp) => 0;
// write half COUNT files
lfsp_mount(&lfsp, &cfgp) => 0;
lfsp_mount(&lfsp, LFSP_M_RDWR, &cfgp) => 0;
uint32_t prng = 42;
for (lfs_size_t i = 0; i < COUNT; i++) {
// write half
@@ -475,7 +476,7 @@ code = '''
// now mount with new version
lfs_t lfs;
lfsr_mount(&lfs, CFG) => 0;
lfsr_mount(&lfs, LFS_M_RDWR, CFG) => 0;
// write half COUNT files
prng = 42;
@@ -569,7 +570,7 @@ code = '''
//////
// now mount with new version
lfsp_mount(&lfsp, &cfgp) => 0;
lfsp_mount(&lfsp, LFSP_M_RDWR, &cfgp) => 0;
uint32_t prng = 42;
for (lfs_size_t i = 0; i < COUNT; i++) {
char name[16];
@@ -601,7 +602,7 @@ code = '''
// mount the new version
lfs_t lfs;
lfsr_mount(&lfs, CFG) => 0;
lfsr_mount(&lfs, LFS_M_RDWR, CFG) => 0;
// write half COUNT files
prng = 42;
@@ -717,7 +718,7 @@ code = '''
lfsr_format(&lfs, CFG) => 0;
// confirm the new mount works
lfsr_mount(&lfs, CFG) => 0;
lfsr_mount(&lfs, LFS_M_RDWR, CFG) => 0;
lfsr_unmount(&lfs) => 0;
//////
@@ -727,7 +728,7 @@ code = '''
assert(sizeof(struct lfsp_config) == sizeof(struct lfs_config));
memcpy(&cfgp, CFG, sizeof(cfgp));
lfsp_t lfsp;
lfsp_mount(&lfsp, &cfgp) => 0;
lfsp_mount(&lfsp, LFSP_M_RDWR, &cfgp) => 0;
lfsp_unmount(&lfsp) => 0;
'''
@@ -745,7 +746,7 @@ code = '''
lfsr_format(&lfs, CFG) => 0;
// write COUNT dirs
lfsr_mount(&lfs, CFG) => 0;
lfsr_mount(&lfs, LFS_M_RDWR, CFG) => 0;
for (lfs_size_t i = 0; i < COUNT; i++) {
char name[8];
sprintf(name, "dir%03d", i);
@@ -760,7 +761,7 @@ code = '''
assert(sizeof(struct lfsp_config) == sizeof(struct lfs_config));
memcpy(&cfgp, CFG, sizeof(cfgp));
lfsp_t lfsp;
lfsp_mount(&lfsp, &cfgp) => 0;
lfsp_mount(&lfsp, LFSP_M_RDWR, &cfgp) => 0;
// can we list the directories?
lfsp_dir_t dir;
@@ -805,7 +806,7 @@ code = '''
lfsr_format(&lfs, CFG) => 0;
// write COUNT files
lfsr_mount(&lfs, CFG) => 0;
lfsr_mount(&lfs, LFS_M_RDWR, CFG) => 0;
uint32_t prng = 42;
for (lfs_size_t i = 0; i < COUNT; i++) {
lfsr_file_t file;
@@ -832,7 +833,7 @@ code = '''
assert(sizeof(struct lfsp_config) == sizeof(struct lfs_config));
memcpy(&cfgp, CFG, sizeof(cfgp));
lfsp_t lfsp;
lfsp_mount(&lfsp, &cfgp) => 0;
lfsp_mount(&lfsp, LFSP_M_RDWR, &cfgp) => 0;
// can we list the files?
lfsp_dir_t dir;
@@ -895,7 +896,7 @@ code = '''
lfsr_format(&lfs, CFG) => 0;
// write COUNT files+dirs
lfsr_mount(&lfs, CFG) => 0;
lfsr_mount(&lfs, LFS_M_RDWR, CFG) => 0;
uint32_t prng = 42;
for (lfs_size_t i = 0; i < COUNT; i++) {
char name[16];
@@ -925,7 +926,7 @@ code = '''
assert(sizeof(struct lfsp_config) == sizeof(struct lfs_config));
memcpy(&cfgp, CFG, sizeof(cfgp));
lfsp_t lfsp;
lfsp_mount(&lfsp, &cfgp) => 0;
lfsp_mount(&lfsp, LFSP_M_RDWR, &cfgp) => 0;
// can we list the directories?
lfsp_dir_t dir;
@@ -1012,7 +1013,7 @@ code = '''
lfsr_format(&lfs, CFG) => 0;
// write COUNT/2 dirs
lfsr_mount(&lfs, CFG) => 0;
lfsr_mount(&lfs, LFS_M_RDWR, CFG) => 0;
for (lfs_size_t i = 0; i < COUNT/2; i++) {
char name[8];
sprintf(name, "dir%03d", i);
@@ -1027,7 +1028,7 @@ code = '''
assert(sizeof(struct lfsp_config) == sizeof(struct lfs_config));
memcpy(&cfgp, CFG, sizeof(cfgp));
lfsp_t lfsp;
lfsp_mount(&lfsp, &cfgp) => 0;
lfsp_mount(&lfsp, LFSP_M_RDWR, &cfgp) => 0;
// write another COUNT/2 dirs
for (lfs_size_t i = COUNT/2; i < COUNT; i++) {
@@ -1079,7 +1080,7 @@ code = '''
lfsr_format(&lfs, CFG) => 0;
// write half COUNT files
lfsr_mount(&lfs, CFG) => 0;
lfsr_mount(&lfs, LFS_M_RDWR, CFG) => 0;
uint32_t prng = 42;
for (lfs_size_t i = 0; i < COUNT; i++) {
// write half
@@ -1112,7 +1113,7 @@ code = '''
assert(sizeof(struct lfsp_config) == sizeof(struct lfs_config));
memcpy(&cfgp, CFG, sizeof(cfgp));
lfsp_t lfsp;
lfsp_mount(&lfsp, &cfgp) => 0;
lfsp_mount(&lfsp, LFSP_M_RDWR, &cfgp) => 0;
// write half COUNT files
prng = 42;
@@ -1201,7 +1202,7 @@ code = '''
lfsr_format(&lfs, CFG) => 0;
// write half COUNT files
lfsr_mount(&lfs, CFG) => 0;
lfsr_mount(&lfs, LFS_M_RDWR, CFG) => 0;
uint32_t prng = 42;
for (lfs_size_t i = 0; i < COUNT; i++) {
char name[16];
@@ -1237,7 +1238,7 @@ code = '''
assert(sizeof(struct lfsp_config) == sizeof(struct lfs_config));
memcpy(&cfgp, CFG, sizeof(cfgp));
lfsp_t lfsp;
lfsp_mount(&lfsp, &cfgp) => 0;
lfsp_mount(&lfsp, LFSP_M_RDWR, &cfgp) => 0;
// write half COUNT files
prng = 42;