Skip to content

Commit 035552a

Browse files
committed
Add version info for software library and on-disk structures
An annoying part of filesystems is that the software library can change independently of the on-disk structures. For this reason versioning is very important, and must be handled separately for the software and on-disk parts. In this patch, littlefs provides two version numbers at compile time, with major and minor parts, in the form of 6 macros. LFS_VERSION // Library version, uint32_t encoded LFS_VERSION_MAJOR // Major - Backwards incompatible changes LFS_VERSION_MINOR // Minor - Feature additions LFS_DISK_VERSION // On-disk version, uint32_t encoded LFS_DISK_VERSION_MAJOR // Major - Backwards incompatible changes LFS_DISK_VERSION_MINOR // Minor - Feature additions Note that littlefs will error if it finds a major version number that is different, or a minor version number that has regressed.
1 parent 997c2e5 commit 035552a

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

lfs.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2067,7 +2067,7 @@ int lfs_format(lfs_t *lfs, const struct lfs_config *cfg) {
20672067
.d.type = LFS_TYPE_SUPERBLOCK,
20682068
.d.elen = sizeof(superblock.d) - sizeof(superblock.d.magic) - 4,
20692069
.d.nlen = sizeof(superblock.d.magic),
2070-
.d.version = 0x00010001,
2070+
.d.version = LFS_DISK_VERSION,
20712071
.d.magic = {"littlefs"},
20722072
.d.block_size = lfs->cfg->block_size,
20732073
.d.block_count = lfs->cfg->block_count,
@@ -2140,10 +2140,11 @@ int lfs_mount(lfs_t *lfs, const struct lfs_config *cfg) {
21402140
return LFS_ERR_CORRUPT;
21412141
}
21422142

2143-
if (superblock.d.version > (0x00010001 | 0x0000ffff)) {
2144-
LFS_ERROR("Invalid version %d.%d",
2145-
0xffff & (superblock.d.version >> 16),
2146-
0xffff & (superblock.d.version >> 0));
2143+
uint16_t major_version = (0xffff & (superblock.d.version >> 16));
2144+
uint16_t minor_version = (0xffff & (superblock.d.version >> 0));
2145+
if ((major_version != LFS_DISK_VERSION_MAJOR ||
2146+
minor_version > LFS_DISK_VERSION_MINOR)) {
2147+
LFS_ERROR("Invalid version %d.%d", major_version, minor_version);
21472148
return LFS_ERR_INVAL;
21482149
}
21492150

lfs.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,23 @@
2222
#include <stdbool.h>
2323

2424

25+
/// Version info ///
26+
27+
// Software library version
28+
// Major (top-nibble), incremented on backwards incompatible changes
29+
// Minor (bottom-nibble), incremented on feature additions
30+
#define LFS_VERSION 0x00010002
31+
#define LFS_VERSION_MAJOR (0xffff & (LFS_VERSION >> 16))
32+
#define LFS_VERSION_MINOR (0xffff & (LFS_VERSION >> 0))
33+
34+
// Version of On-disk data structures
35+
// Major (top-nibble), incremented on backwards incompatible changes
36+
// Minor (bottom-nibble), incremented on feature additions
37+
#define LFS_DISK_VERSION 0x00010001
38+
#define LFS_DISK_VERSION_MAJOR (0xffff & (LFS_DISK_VERSION >> 16))
39+
#define LFS_DISK_VERSION_MINOR (0xffff & (LFS_DISK_VERSION >> 0))
40+
41+
2542
/// Definitions ///
2643

2744
// Type definitions

0 commit comments

Comments
 (0)