Skip to content

Commit

Permalink
fs: zms: add a version control
Browse files Browse the repository at this point in the history
some future contributions could make the deployed ZMS storage
incompatible with the current implementation.
Add a version holder to handle this if needed.

Signed-off-by: Riadh Ghaddab <rghaddab@baylibre.com>
  • Loading branch information
rghaddab committed Sep 10, 2024
1 parent 013a303 commit acdc1b8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
16 changes: 15 additions & 1 deletion subsys/fs/zms/zms.c
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,7 @@ static int zms_add_empty_ate(struct zms_fs *fs, uint64_t addr)
empty_ate.id = ZMS_HEAD_ID;
empty_ate.len = 0xffff;
empty_ate.offset = 0U;
empty_ate.data = 0xffffffff;
empty_ate.data = ZMS_DEFAULT_VERSION;
rc = zms_get_sector_cycle(fs, addr, &cycle_cnt);
if (rc) {
if (rc == -ENOENT) {
Expand Down Expand Up @@ -989,6 +989,12 @@ static int zms_init(struct zms_fs *fs)
(close_ate.cycle_cnt == empty_ate.cycle_cnt)) {
/* closed sector */
closed_sectors++;
/* Let's check first that we support this ZMS version */
if (ZMS_GET_VERSION(empty_ate.data) != ZMS_DEFAULT_VERSION) {
LOG_ERR("ZMS Version is not supported");
rc = -ENOEXEC;
goto end;
}
zms_sector_advance(fs, &addr);
/* addr is pointing to the close ATE */
rc = zms_get_sector_header(fs, addr, &empty_ate, &close_ate);
Expand Down Expand Up @@ -1033,6 +1039,13 @@ static int zms_init(struct zms_fs *fs)
zms_flash_erase_sector(fs, addr);
}
zms_add_empty_ate(fs, addr);
} else {
/* empty_ate is valid let's check the version */
if (ZMS_GET_VERSION(empty_ate.data) != ZMS_DEFAULT_VERSION) {
LOG_ERR("ZMS Version is not supported");
rc = -ENOEXEC;
goto end;
}
}
rc = zms_get_sector_cycle(fs, addr, &fs->sector_cycle);
if (rc) {
Expand Down Expand Up @@ -1186,6 +1199,7 @@ static int zms_init(struct zms_fs *fs)
rc = zms_add_gc_done_ate(fs);
}
k_mutex_unlock(&fs->zms_lock);

return rc;
}

Expand Down
3 changes: 3 additions & 0 deletions subsys/fs/zms/zms_priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ extern "C" {
#define ADDR_OFFS_MASK GENMASK64(31, 0)
#define SECTOR_NUM(x) FIELD_GET(ADDR_SECT_MASK, x)

#define ZMS_VERSION_MASK GENMASK(7, 0)
#define ZMS_GET_VERSION(x) FIELD_GET(ZMS_VERSION_MASK, x)
#define ZMS_DEFAULT_VERSION 1
#define ZMS_BLOCK_SIZE 32

Check notice on line 29 in subsys/fs/zms/zms_priv.h

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

You may want to run clang-format on this change

subsys/fs/zms/zms_priv.h:29 -#define ZMS_VERSION_MASK GENMASK(7, 0) -#define ZMS_GET_VERSION(x) FIELD_GET(ZMS_VERSION_MASK, x) +#define ZMS_VERSION_MASK GENMASK(7, 0) +#define ZMS_GET_VERSION(x) FIELD_GET(ZMS_VERSION_MASK, x) #define ZMS_DEFAULT_VERSION 1 -#define ZMS_BLOCK_SIZE 32 +#define ZMS_BLOCK_SIZE 32
#define ZMS_LOOKUP_CACHE_NO_ADDR GENMASK64(63, 0)
Expand Down
2 changes: 1 addition & 1 deletion tests/subsys/fs/zms/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ ZTEST_F(zms, test_zms_gc_corrupt_close_ate)
empty_ate.id = 0xFFFFFFFF;
empty_ate.offset = 0;
empty_ate.len = 0xFFFF;
empty_ate.data = 0xffffffff;
empty_ate.data = 0x1;
empty_ate.cycle_cnt = 1;
empty_ate.crc8 = crc8_ccitt(0xff, &empty_ate, offsetof(struct zms_ate, crc8));
ate.id = 0x1;
Expand Down

0 comments on commit acdc1b8

Please sign in to comment.