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
+31 -31
View File
@@ -29,7 +29,7 @@ code = '''
cfg.block_count = SMALLER_BLOCK_COUNT;
lfs_t lfs;
lfsr_format(&lfs, &cfg) => 0;
lfsr_mount(&lfs, &cfg) => 0;
lfsr_mount(&lfs, LFS_M_RDWR, &cfg) => 0;
// fsstat up to date?
struct lfs_fsinfo fsinfo;
@@ -66,7 +66,7 @@ code = '''
// try to mount with a bigger block count
cfg = *CFG;
cfg.block_count = BIGGER_BLOCK_COUNT;
lfsr_mount(&lfs, &cfg) => 0;
lfsr_mount(&lfs, LFS_M_RDWR, &cfg) => 0;
// fsstat up to date?
lfsr_fs_stat(&lfs, &fsinfo) => 0;
@@ -105,7 +105,7 @@ code = '''
lfsr_unmount(&lfs) => 0;
// stays after a mount?
lfsr_mount(&lfs, &cfg) => 0;
lfsr_mount(&lfs, LFS_M_RDWR, &cfg) => 0;
// fsstat up to date?
lfsr_fs_stat(&lfs, &fsinfo) => 0;
@@ -148,7 +148,7 @@ code = '''
cfg.block_count = BIGGER_BLOCK_COUNT;
lfs_t lfs;
lfsr_format(&lfs, &cfg) => 0;
lfsr_mount(&lfs, &cfg) => 0;
lfsr_mount(&lfs, LFS_M_RDWR, &cfg) => 0;
// fsstat up to date?
struct lfs_fsinfo fsinfo;
@@ -185,7 +185,7 @@ code = '''
// try to mount with a smaller block count
cfg = *CFG;
cfg.block_count = SMALLER_BLOCK_COUNT;
lfsr_mount(&lfs, &cfg) => LFS_ERR_NOTSUP;
lfsr_mount(&lfs, LFS_M_RDWR, &cfg) => LFS_ERR_NOTSUP;
'''
# test we can grow a filesystem
@@ -209,7 +209,7 @@ code = '''
cfg.block_count = SMALLER_BLOCK_COUNT;
lfs_t lfs;
lfsr_format(&lfs, &cfg) => 0;
lfsr_mount(&lfs, &cfg) => 0;
lfsr_mount(&lfs, LFS_M_RDWR, &cfg) => 0;
// fsstat up to date?
struct lfs_fsinfo fsinfo;
@@ -246,7 +246,7 @@ code = '''
// try to grow our filesystem
cfg = *CFG;
cfg.block_count = BIGGER_BLOCK_COUNT;
lfsr_mount(&lfs, &cfg) => 0;
lfsr_mount(&lfs, LFS_M_RDWR, &cfg) => 0;
lfsr_fs_grow(&lfs, BIGGER_BLOCK_COUNT) => 0;
@@ -287,7 +287,7 @@ code = '''
lfsr_unmount(&lfs) => 0;
// stays after a mount?
lfsr_mount(&lfs, &cfg) => 0;
lfsr_mount(&lfs, LFS_M_RDWR, &cfg) => 0;
// fsstat up to date?
lfsr_fs_stat(&lfs, &fsinfo) => 0;
@@ -324,7 +324,7 @@ code = '''
cfg.block_count = SMALLER_BLOCK_COUNT;
lfs_t lfs;
lfsr_format(&lfs, &cfg) => 0;
lfsr_mount(&lfs, &cfg) => 0;
lfsr_mount(&lfs, LFS_M_RDWR, &cfg) => 0;
// fsstat up to date?
struct lfs_fsinfo fsinfo;
@@ -361,7 +361,7 @@ code = '''
// try to grow to same size
cfg = *CFG;
cfg.block_count = SMALLER_BLOCK_COUNT;
lfsr_mount(&lfs, &cfg) => 0;
lfsr_mount(&lfs, LFS_M_RDWR, &cfg) => 0;
lfsr_fs_grow(&lfs, SMALLER_BLOCK_COUNT) => 0;
@@ -402,7 +402,7 @@ code = '''
lfsr_unmount(&lfs) => 0;
// stays after a mount?
lfsr_mount(&lfs, &cfg) => 0;
lfsr_mount(&lfs, LFS_M_RDWR, &cfg) => 0;
// fsstat up to date?
lfsr_fs_stat(&lfs, &fsinfo) => 0;
@@ -442,7 +442,7 @@ code = '''
lfs_t lfs;
lfsr_format(&lfs, &cfg) => 0;
// mount with maximum block count
lfsr_mount(&lfs, CFG) => 0;
lfsr_mount(&lfs, LFS_M_RDWR, CFG) => 0;
// fsstat up to date?
struct lfs_fsinfo fsinfo;
@@ -495,7 +495,7 @@ code = '''
// remount?
if (REMOUNT) {
lfsr_unmount(&lfs) => 0;
lfsr_mount(&lfs, CFG) => 0;
lfsr_mount(&lfs, LFS_M_RDWR, CFG) => 0;
}
// fsstat up to date?
@@ -512,7 +512,7 @@ code = '''
// remount?
if (remount) {
lfsr_unmount(&lfs) => 0;
lfsr_mount(&lfs, CFG) => 0;
lfsr_mount(&lfs, LFS_M_RDWR, CFG) => 0;
}
// grm should be zero here
@@ -585,7 +585,7 @@ code = '''
lfs_t lfs;
lfsr_format(&lfs, &cfg) => 0;
// mount with maximum block count
lfsr_mount(&lfs, CFG) => 0;
lfsr_mount(&lfs, LFS_M_RDWR, CFG) => 0;
// fsstat up to date?
struct lfs_fsinfo fsinfo;
@@ -731,7 +731,7 @@ code = '''
// remount?
if (REMOUNT) {
lfsr_unmount(&lfs) => 0;
lfsr_mount(&lfs, CFG) => 0;
lfsr_mount(&lfs, LFS_M_RDWR, CFG) => 0;
}
// fsstat up to date?
@@ -748,7 +748,7 @@ code = '''
// remount?
if (remount) {
lfsr_unmount(&lfs) => 0;
lfsr_mount(&lfs, CFG) => 0;
lfsr_mount(&lfs, LFS_M_RDWR, CFG) => 0;
}
// grm should be zero here
@@ -816,7 +816,7 @@ code = '''
lfs_t lfs;
lfsr_format(&lfs, &cfg) => 0;
// mount with maximum block count
lfsr_mount(&lfs, CFG) => 0;
lfsr_mount(&lfs, LFS_M_RDWR, CFG) => 0;
// fsstat up to date?
struct lfs_fsinfo fsinfo;
@@ -891,7 +891,7 @@ code = '''
// remount?
if (REMOUNT) {
lfsr_unmount(&lfs) => 0;
lfsr_mount(&lfs, CFG) => 0;
lfsr_mount(&lfs, LFS_M_RDWR, CFG) => 0;
}
// fsstat up to date?
@@ -908,7 +908,7 @@ code = '''
// remount?
if (remount) {
lfsr_unmount(&lfs) => 0;
lfsr_mount(&lfs, CFG) => 0;
lfsr_mount(&lfs, LFS_M_RDWR, CFG) => 0;
}
// check that our writes worked
@@ -965,7 +965,7 @@ code = '''
lfs_t lfs;
lfsr_format(&lfs, &cfg) => 0;
// mount with maximum block count
lfsr_mount(&lfs, CFG) => 0;
lfsr_mount(&lfs, LFS_M_RDWR, CFG) => 0;
// fsstat up to date?
struct lfs_fsinfo fsinfo;
@@ -1157,7 +1157,7 @@ code = '''
// remount?
if (REMOUNT) {
lfsr_unmount(&lfs) => 0;
lfsr_mount(&lfs, CFG) => 0;
lfsr_mount(&lfs, LFS_M_RDWR, CFG) => 0;
}
// fsstat up to date?
@@ -1174,7 +1174,7 @@ code = '''
// remount?
if (remount) {
lfsr_unmount(&lfs) => 0;
lfsr_mount(&lfs, CFG) => 0;
lfsr_mount(&lfs, LFS_M_RDWR, CFG) => 0;
}
// check that our files match our simulation
@@ -1259,7 +1259,7 @@ code = '''
lfs_t lfs;
lfsr_format(&lfs, &cfg) => 0;
// mount with maximum block count
lfsr_mount(&lfs, CFG) => 0;
lfsr_mount(&lfs, LFS_M_RDWR, CFG) => 0;
// fsstat up to date?
struct lfs_fsinfo fsinfo;
@@ -1683,7 +1683,7 @@ code = '''
lfs_t lfs;
lfsr_format(&lfs, &cfg) => 0;
// mount with maximum block count
lfsr_mount(&lfs, CFG) => 0;
lfsr_mount(&lfs, LFS_M_RDWR, CFG) => 0;
// fsstat up to date?
struct lfs_fsinfo fsinfo;
@@ -2208,14 +2208,14 @@ reentrant = true
code = '''
// format once per test
lfs_t lfs;
int err = lfsr_mount(&lfs, CFG);
int err = lfsr_mount(&lfs, LFS_M_RDWR, CFG);
if (err) {
// start with a small number of blocks
struct lfs_config cfg = *CFG;
cfg.block_count = INIT_BLOCK_COUNT;
lfsr_format(&lfs, &cfg) => 0;
// mount with maximum block count
lfsr_mount(&lfs, CFG) => 0;
lfsr_mount(&lfs, LFS_M_RDWR, CFG) => 0;
// fsstat up to date?
struct lfs_fsinfo fsinfo;
@@ -2439,7 +2439,7 @@ code = '''
// remount?
if (remount) {
lfsr_unmount(&lfs) => 0;
lfsr_mount(&lfs, CFG) => 0;
lfsr_mount(&lfs, LFS_M_RDWR, CFG) => 0;
}
// check that things look more-or-less ok
@@ -2521,14 +2521,14 @@ reentrant = true
code = '''
// format once per test
lfs_t lfs;
int err = lfsr_mount(&lfs, CFG);
int err = lfsr_mount(&lfs, LFS_M_RDWR, CFG);
if (err) {
// start with a small number of blocks
struct lfs_config cfg = *CFG;
cfg.block_count = INIT_BLOCK_COUNT;
lfsr_format(&lfs, &cfg) => 0;
// mount with maximum block count
lfsr_mount(&lfs, CFG) => 0;
lfsr_mount(&lfs, LFS_M_RDWR, CFG) => 0;
// fsstat up to date?
struct lfs_fsinfo fsinfo;
@@ -2879,7 +2879,7 @@ code = '''
// remount?
if (remount) {
lfsr_unmount(&lfs) => 0;
lfsr_mount(&lfs, CFG) => 0;
lfsr_mount(&lfs, LFS_M_RDWR, CFG) => 0;
}
// check that things look more-or-less ok