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 -9
View File
@@ -289,6 +289,7 @@ static int lfsr_bd_read(lfs_t *lfs,
}
// needed in lfsr_bd_prog_ for prog validation
static inline bool lfsr_m_isckprogs(uint32_t flags);
static lfs_scmp_t lfsr_bd_cmp(lfs_t *lfs,
lfs_block_t block, lfs_size_t off, lfs_size_t hint,
const void *buffer, lfs_size_t size);
@@ -304,7 +305,7 @@ static int lfsr_bd_prog_(lfs_t *lfs, lfs_block_t block, lfs_size_t off,
}
// check progs?
if (lfs->cfg->check_progs) {
if (lfsr_m_isckprogs(lfs->flags)) {
// pcache should have been dropped at this point
LFS_ASSERT(lfs->pcache.size == 0);
@@ -5907,6 +5908,14 @@ static int lfsr_data_readmptr(lfs_t *lfs, lfsr_data_t *data,
/// various flag things ///
static inline bool lfsr_m_isrdonly(uint32_t flags) {
return flags & LFS_M_RDONLY;
}
static inline bool lfsr_m_isckprogs(uint32_t flags) {
return flags & LFS_M_CKPROGS;
}
static inline bool lfsr_i_isuncompacted(uint32_t flags) {
return flags & LFS_I_UNCOMPACTED;
}
@@ -11772,11 +11781,18 @@ failed:;
static int lfs_deinit(lfs_t *lfs);
// initialize littlefs state, assert on bad configuration
static int lfs_init(lfs_t *lfs, const struct lfs_config *cfg) {
static int lfs_init(lfs_t *lfs, uint32_t flags,
const struct lfs_config *cfg) {
// TODO this all needs to be cleaned up
lfs->cfg = cfg;
int err = 0;
// unknown flags?
LFS_ASSERT((flags
& ~LFS_M_RDWR
& ~LFS_M_RDONLY
& ~LFS_M_CKPROGS) == 0);
// validate that the lfs-cfg sizes were initiated properly before
// performing any arithmetic logics with them
LFS_ASSERT(lfs->cfg->read_size != 0);
@@ -11823,10 +11839,10 @@ static int lfs_init(lfs_t *lfs, const struct lfs_config *cfg) {
// fragment_size must be <= block_size/4
LFS_ASSERT(lfs->cfg->fragment_size <= lfs->cfg->block_size/4);
// zero flags
lfs->flags = 0;
// setup flags
lfs->flags = flags;
// setup block_count so we can mutate it
// copy block_count so we can mutate it
lfs->block_count = lfs->cfg->block_count;
// setup read cache
@@ -12554,8 +12570,9 @@ static int lfsr_mountinited(lfs_t *lfs) {
return 0;
}
int lfsr_mount(lfs_t *lfs, const struct lfs_config *cfg) {
int err = lfs_init(lfs, cfg);
int lfsr_mount(lfs_t *lfs, uint32_t flags,
const struct lfs_config *cfg) {
int err = lfs_init(lfs, flags, cfg);
if (err) {
return err;
}
@@ -12674,7 +12691,7 @@ static int lfsr_formatinited(lfs_t *lfs) {
}
int lfsr_format(lfs_t *lfs, const struct lfs_config *cfg) {
int err = lfs_init(lfs, cfg);
int err = lfs_init(lfs, LFS_M_RDWR, cfg);
if (err) {
return err;
}
@@ -12702,7 +12719,10 @@ int lfsr_format(lfs_t *lfs, const struct lfs_config *cfg) {
int lfsr_fs_stat(lfs_t *lfs, struct lfs_fsinfo *fsinfo) {
// return various filesystem flags
fsinfo->flags = lfs->flags & LFS_I_UNCOMPACTED;
fsinfo->flags = lfs->flags & (
LFS_I_RDONLY
| LFS_I_CKPROGS
| LFS_I_UNCOMPACTED);
// some flags we calculate on demand
if (lfsr_grm_count(lfs) > 0 || lfsr_f_hasorphans(lfs->flags)) {