Skip to content

Commit

Permalink
Changed fsinfo.minor_version -> fsinfo.disk_version
Browse files Browse the repository at this point in the history
Version are now returned with major/minor packed into 32-bits,
so 0x00020001 is the current disk version, for example.

1. This needed to change to use a disk_* prefix for consistency with the
   defines that already exist for LFS_VERSION/LFS_DISK_VERSION.

2. Encoding the version this way has the nice side effect of making 0 an
   invalid value. This is useful for adding a similar config option
   that needs to have reasonable default behavior for backwards
   compatibility.

In theory this uses more space, but in practice most other config/status
is 32-bits in littlefs. We would be wasting this space for alignment
anyways.
  • Loading branch information
geky committed Jun 7, 2023
1 parent 8610f7c commit c5fb3f1
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 20 deletions.
6 changes: 3 additions & 3 deletions lfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -4424,7 +4424,7 @@ static int lfs_fs_rawstat(lfs_t *lfs, struct lfs_fsinfo *fsinfo) {
// if the superblock is up-to-date, we must be on the most recent
// minor version of littlefs
if (!lfs_gstate_needssuperblock(&lfs->gstate)) {
fsinfo->minor_version = LFS_DISK_VERSION_MINOR;
fsinfo->disk_version = LFS_DISK_VERSION;

// otherwise we need to read the minor version on disk
} else {
Expand All @@ -4444,8 +4444,8 @@ static int lfs_fs_rawstat(lfs_t *lfs, struct lfs_fsinfo *fsinfo) {
}
lfs_superblock_fromle32(&superblock);

// read the minor version
fsinfo->minor_version = (0xffff & (superblock.version >> 0));
// read the on-disk version
fsinfo->disk_version = superblock.version;
}

// find the current block usage
Expand Down
4 changes: 2 additions & 2 deletions lfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,8 @@ struct lfs_info {

// Filesystem info structure
struct lfs_fsinfo {
// On-disk minor version.
uint16_t minor_version;
// On-disk version.
uint32_t disk_version;

// Number of blocks in use, this is the same as lfs_fs_size.
//
Expand Down
26 changes: 13 additions & 13 deletions tests/test_compat.toml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ code = '''
// we should be able to read the version using lfs_fs_stat
struct lfs_fsinfo fsinfo;
lfs_fs_stat(&lfs, &fsinfo) => 0;
assert(fsinfo.minor_version == LFSP_DISK_VERSION_MINOR);
assert(fsinfo.disk_version == LFSP_DISK_VERSION);

lfs_unmount(&lfs) => 0;
'''
Expand Down Expand Up @@ -113,7 +113,7 @@ code = '''
// we should be able to read the version using lfs_fs_stat
struct lfs_fsinfo fsinfo;
lfs_fs_stat(&lfs, &fsinfo) => 0;
assert(fsinfo.minor_version == LFSP_DISK_VERSION_MINOR);
assert(fsinfo.disk_version == LFSP_DISK_VERSION);

// can we list the directories?
lfs_dir_t dir;
Expand Down Expand Up @@ -182,7 +182,7 @@ code = '''
// we should be able to read the version using lfs_fs_stat
struct lfs_fsinfo fsinfo;
lfs_fs_stat(&lfs, &fsinfo) => 0;
assert(fsinfo.minor_version == LFSP_DISK_VERSION_MINOR);
assert(fsinfo.disk_version == LFSP_DISK_VERSION);

// can we list the files?
lfs_dir_t dir;
Expand Down Expand Up @@ -272,7 +272,7 @@ code = '''
// we should be able to read the version using lfs_fs_stat
struct lfs_fsinfo fsinfo;
lfs_fs_stat(&lfs, &fsinfo) => 0;
assert(fsinfo.minor_version == LFSP_DISK_VERSION_MINOR);
assert(fsinfo.disk_version == LFSP_DISK_VERSION);

// can we list the directories?
lfs_dir_t dir;
Expand Down Expand Up @@ -369,7 +369,7 @@ code = '''
// we should be able to read the version using lfs_fs_stat
struct lfs_fsinfo fsinfo;
lfs_fs_stat(&lfs, &fsinfo) => 0;
assert(fsinfo.minor_version == LFSP_DISK_VERSION_MINOR);
assert(fsinfo.disk_version == LFSP_DISK_VERSION);

// write another COUNT/2 dirs
for (lfs_size_t i = COUNT/2; i < COUNT; i++) {
Expand Down Expand Up @@ -451,7 +451,7 @@ code = '''
// we should be able to read the version using lfs_fs_stat
struct lfs_fsinfo fsinfo;
lfs_fs_stat(&lfs, &fsinfo) => 0;
assert(fsinfo.minor_version == LFSP_DISK_VERSION_MINOR);
assert(fsinfo.disk_version == LFSP_DISK_VERSION);

// write half COUNT files
prng = 42;
Expand Down Expand Up @@ -573,7 +573,7 @@ code = '''
// we should be able to read the version using lfs_fs_stat
struct lfs_fsinfo fsinfo;
lfs_fs_stat(&lfs, &fsinfo) => 0;
assert(fsinfo.minor_version == LFSP_DISK_VERSION_MINOR);
assert(fsinfo.disk_version == LFSP_DISK_VERSION);

// write half COUNT files
prng = 42;
Expand Down Expand Up @@ -1358,7 +1358,7 @@ code = '''

struct lfs_fsinfo fsinfo;
lfs_fs_stat(&lfs, &fsinfo) => 0;
assert(fsinfo.minor_version == LFS_DISK_VERSION_MINOR-1);
assert(fsinfo.disk_version == LFS_DISK_VERSION-1);

lfs_file_open(&lfs, &file, "test", LFS_O_RDONLY) => 0;
uint8_t buffer[8];
Expand All @@ -1368,23 +1368,23 @@ code = '''

// minor version should be unchanged
lfs_fs_stat(&lfs, &fsinfo) => 0;
assert(fsinfo.minor_version == LFS_DISK_VERSION_MINOR-1);
assert(fsinfo.disk_version == LFS_DISK_VERSION-1);

lfs_unmount(&lfs) => 0;

// if we write, we need to bump the minor version
lfs_mount(&lfs, cfg) => 0;

lfs_fs_stat(&lfs, &fsinfo) => 0;
assert(fsinfo.minor_version == LFS_DISK_VERSION_MINOR-1);
assert(fsinfo.disk_version == LFS_DISK_VERSION-1);

lfs_file_open(&lfs, &file, "test", LFS_O_WRONLY | LFS_O_TRUNC) => 0;
lfs_file_write(&lfs, &file, "teeeeest", 8) => 8;
lfs_file_close(&lfs, &file) => 0;

// minor version should be changed
lfs_fs_stat(&lfs, &fsinfo) => 0;
assert(fsinfo.minor_version == LFS_DISK_VERSION_MINOR);
assert(fsinfo.disk_version == LFS_DISK_VERSION);

lfs_unmount(&lfs) => 0;

Expand All @@ -1393,7 +1393,7 @@ code = '''

// minor version should have changed
lfs_fs_stat(&lfs, &fsinfo) => 0;
assert(fsinfo.minor_version == LFS_DISK_VERSION_MINOR);
assert(fsinfo.disk_version == LFS_DISK_VERSION);

lfs_file_open(&lfs, &file, "test", LFS_O_RDONLY) => 0;
lfs_file_read(&lfs, &file, buffer, 8) => 8;
Expand All @@ -1402,7 +1402,7 @@ code = '''

// yep, still changed
lfs_fs_stat(&lfs, &fsinfo) => 0;
assert(fsinfo.minor_version == LFS_DISK_VERSION_MINOR);
assert(fsinfo.disk_version == LFS_DISK_VERSION);

lfs_unmount(&lfs) => 0;
'''
4 changes: 2 additions & 2 deletions tests/test_superblocks.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ code = '''

struct lfs_fsinfo fsinfo;
lfs_fs_stat(&lfs, &fsinfo) => 0;
assert(fsinfo.minor_version == LFS_DISK_VERSION_MINOR);
assert(fsinfo.disk_version == LFS_DISK_VERSION);
assert(fsinfo.block_usage > 0 && fsinfo.block_usage < BLOCK_COUNT);
assert(fsinfo.name_max == LFS_NAME_MAX);
assert(fsinfo.file_max == LFS_FILE_MAX);
Expand Down Expand Up @@ -73,7 +73,7 @@ code = '''

struct lfs_fsinfo fsinfo;
lfs_fs_stat(&lfs, &fsinfo) => 0;
assert(fsinfo.minor_version == LFS_DISK_VERSION_MINOR);
assert(fsinfo.disk_version == LFS_DISK_VERSION);
assert(fsinfo.block_usage > 0 && fsinfo.block_usage < BLOCK_COUNT);
assert(fsinfo.name_max == TWEAKED_NAME_MAX);
assert(fsinfo.file_max == TWEAKED_FILE_MAX);
Expand Down

0 comments on commit c5fb3f1

Please sign in to comment.