Skip to content

Commit

Permalink
Added LFS_MULTIVERSION, made lfs2.0 support a compile-time option
Browse files Browse the repository at this point in the history
The code-cost wasn't that bad: 16556 B -> 16754 B (+1.2%)

But moving write support of older versions behind a compile-time flag
allows us to be a bit more liberal with what gets added to support older
versions, since the cost won't hit most users.
  • Loading branch information
geky committed Jun 29, 2023
1 parent b72c96d commit eb9af7a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
20 changes: 17 additions & 3 deletions lfs.c
Expand Up @@ -520,9 +520,13 @@ static void lfs_mlist_append(lfs_t *lfs, struct lfs_mlist *mlist) {

// some other filesystem operations
static uint32_t lfs_fs_disk_version(lfs_t *lfs) {
(void)lfs;
#ifdef LFS_MULTIVERSION
if (lfs->cfg->disk_version) {
return lfs->cfg->disk_version;
} else {
} else
#endif
{
return LFS_DISK_VERSION;
}
}
Expand Down Expand Up @@ -1274,6 +1278,7 @@ static lfs_stag_t lfs_dir_fetchmatch(lfs_t *lfs,
// did we end on a valid commit? we may have an erased block
dir->erased = false;
if (maybeerased && dir->off % lfs->cfg->prog_size == 0) {
#ifdef LFS_MULTIVERSION
// note versions < lfs2.1 did not have fcrc tags, if
// we're < lfs2.1 treat missing fcrc as erased data
//
Expand All @@ -1282,7 +1287,9 @@ static lfs_stag_t lfs_dir_fetchmatch(lfs_t *lfs,
if (lfs_fs_disk_version(lfs) < 0x00020001) {
dir->erased = true;

} else if (hasfcrc) {
} else
#endif
if (hasfcrc) {
// check for an fcrc matching the next prog's erased state, if
// this failed most likely a previous prog was interrupted, we
// need a new erase
Expand Down Expand Up @@ -1632,9 +1639,14 @@ static int lfs_dir_commitcrc(lfs_t *lfs, struct lfs_commit *commit) {
return err;
}

#ifdef LFS_MULTIVERSION
// unfortunately fcrcs break mdir fetching < lfs2.1, so only write
// these if we're a >= lfs2.1 filesystem
if (lfs_fs_disk_version(lfs) >= 0x00020001) {
if (lfs_fs_disk_version(lfs) <= 0x00020000) {
// don't write fcrc
} else
#endif
{
// find the expected fcrc, don't bother avoiding a reread
// of the eperturb, it should still be in our cache
struct lfs_fcrc fcrc = {
Expand Down Expand Up @@ -4085,12 +4097,14 @@ static int lfs_init(lfs_t *lfs, const struct lfs_config *cfg) {
lfs->cfg = cfg;
int err = 0;

#ifdef LFS_MULTIVERSION
// this driver only supports minor version < current minor version
LFS_ASSERT(!lfs->cfg->disk_version || (
(0xffff & (lfs->cfg->disk_version >> 16))
== LFS_DISK_VERSION_MAJOR
&& (0xffff & (lfs->cfg->disk_version >> 0))
<= LFS_DISK_VERSION_MINOR));
#endif

// check that bool is a truthy-preserving type
//
Expand Down
2 changes: 2 additions & 0 deletions lfs.h
Expand Up @@ -264,11 +264,13 @@ struct lfs_config {
// Defaults to block_size when zero.
lfs_size_t metadata_max;

#ifdef LFS_MULTIVERSION
// On-disk version to use when writing in the form of 16-bit major version
// + 16-bit minor version. This limiting metadata to what is supported by
// older minor versions. Note that some features will be lost. Defaults to
// to the most recent minor version when zero.
uint32_t disk_version;
#endif
};

// File info structure
Expand Down
10 changes: 10 additions & 0 deletions runners/test_runner.c
Expand Up @@ -1346,7 +1346,9 @@ static void run_powerloss_none(
.block_cycles = BLOCK_CYCLES,
.cache_size = CACHE_SIZE,
.lookahead_size = LOOKAHEAD_SIZE,
#ifdef LFS_MULTIVERSION
.disk_version = DISK_VERSION,
#endif
};

struct lfs_emubd_config bdcfg = {
Expand Down Expand Up @@ -1416,7 +1418,9 @@ static void run_powerloss_linear(
.block_cycles = BLOCK_CYCLES,
.cache_size = CACHE_SIZE,
.lookahead_size = LOOKAHEAD_SIZE,
#ifdef LFS_MULTIVERSION
.disk_version = DISK_VERSION,
#endif
};

struct lfs_emubd_config bdcfg = {
Expand Down Expand Up @@ -1503,7 +1507,9 @@ static void run_powerloss_log(
.block_cycles = BLOCK_CYCLES,
.cache_size = CACHE_SIZE,
.lookahead_size = LOOKAHEAD_SIZE,
#ifdef LFS_MULTIVERSION
.disk_version = DISK_VERSION,
#endif
};

struct lfs_emubd_config bdcfg = {
Expand Down Expand Up @@ -1588,7 +1594,9 @@ static void run_powerloss_cycles(
.block_cycles = BLOCK_CYCLES,
.cache_size = CACHE_SIZE,
.lookahead_size = LOOKAHEAD_SIZE,
#ifdef LFS_MULTIVERSION
.disk_version = DISK_VERSION,
#endif
};

struct lfs_emubd_config bdcfg = {
Expand Down Expand Up @@ -1771,7 +1779,9 @@ static void run_powerloss_exhaustive(
.block_cycles = BLOCK_CYCLES,
.cache_size = CACHE_SIZE,
.lookahead_size = LOOKAHEAD_SIZE,
#ifdef LFS_MULTIVERSION
.disk_version = DISK_VERSION,
#endif
};

struct lfs_emubd_config bdcfg = {
Expand Down

0 comments on commit eb9af7a

Please sign in to comment.