Added grm rcompat flag, dropped ocompat, tweaked compat flags a bit

I'm just not seeing a use case for optional compat flags (ocompat), so
dropping for now. It seems their *nix equivalent, feature_compat, is
used to inform fsck of things, but this doesn't really make since in
littlefs since there is no fsck. Or from a different perspective,
littlefs is always running fsck.

Ocompat flags can always be added later (since they do nothing).

Unfortunately this really ruins the alignment of the tag encoding. For
whatever reason config limits tend to come in pairs. For now the best
solution is just leave tag 0x0006 unused. I guess you can consider it
reserved for hypothetical ocompat flags in the future.

---

This adds an rcompat flag for the grm, since in theory a filesystem
doesn't need to support grms if it never renames files (or creates
directories?). But if a filesystem doesn't support grms and a grms gets
written into the filesystem, this can lead to corruption.

I think every piece of gstate will end up with its own compat flag for
this reason.

---

Also renamed r/w/oflags -> r/w/ocompatflags to make their purpose
clearer.

---

The code impact of adding the grm rcompat flag is minimal, and will
probably be less for additional rcompat flags:

            code          stack
  before:  31528           2752
  after:   31584 (+0.2%)   2752 (+0.0%)
This commit is contained in:
Christopher Haster
2023-12-07 15:04:29 -06:00
parent c76ff08f67
commit 04c6b5a067
6 changed files with 281 additions and 272 deletions
+61 -32
View File
@@ -578,7 +578,7 @@ static int lfsr_bd_erase(lfs_t *lfs, lfs_block_t block) {
// 16-bit metadata tags
enum lfsr_tag_type {
enum lfsr_tag {
// the null tag is reserved
LFSR_TAG_NULL = 0x0000,
@@ -586,9 +586,8 @@ enum lfsr_tag_type {
LFSR_TAG_CONFIG = 0x0000,
LFSR_TAG_MAGIC = 0x0003,
LFSR_TAG_VERSION = 0x0004,
LFSR_TAG_RFLAGS = 0x0005,
LFSR_TAG_WFLAGS = 0x0006,
LFSR_TAG_OFLAGS = 0x0007,
LFSR_TAG_RCOMPATFLAGS = 0x0006,
LFSR_TAG_WCOMPATFLAGS = 0x0007,
LFSR_TAG_BLOCKSIZE = 0x0008,
LFSR_TAG_BLOCKCOUNT = 0x0009,
LFSR_TAG_NAMELIMIT = 0x000a,
@@ -7656,6 +7655,28 @@ static int lfsr_traversal_read(lfs_t *lfs, lfsr_traversal_t *traversal,
// return LFSR_DATA_BUF(buffer, d);
//}
// compatibility flags
//
// - WCOMPAT => Must understand to write to the filesystem
// - RCOMPAT => Must understand to read the filesystem
//
// note, "understanding" does not necessarily mean support
//
enum lfsr_rcompat {
LFSR_RCOMPAT_GRM = 0x01,
};
typedef uint8_t lfsr_rcompat_t;
typedef uint8_t lfsr_wcompat_t;
static inline bool lfsr_rcompat_hasgrm(lfsr_rcompat_t rcompat) {
return rcompat & LFSR_RCOMPAT_GRM;
}
static inline bool lfsr_rcompat_hasunknown(lfsr_rcompat_t rcompat) {
return rcompat & ~LFSR_RCOMPAT_GRM;
}
/// Filesystem init functions ///
@@ -7726,53 +7747,59 @@ static int lfsr_mountmroot(lfs_t *lfs, const lfsr_mdir_t *mroot) {
return LFS_ERR_INVAL;
}
// check for any rflags, we must understand these to read
// check for any rcompatflags, we must understand these to read
// the filesystem
err = lfsr_mdir_lookup(lfs, mroot, -1, LFSR_TAG_RFLAGS,
err = lfsr_mdir_lookup(lfs, mroot, -1, LFSR_TAG_RCOMPATFLAGS,
NULL, &data);
if (err && err != LFS_ERR_NOENT) {
return err;
}
if (err == LFS_ERR_NOENT) {
data = LFSR_DATA_NULL();
}
if (err != LFS_ERR_NOENT && lfsr_data_size(&data) > 0) {
LFS_ERROR("Incompatible rflag 0x%s%"PRIx32,
(lfsr_data_size(&data) > 0) ? "?" : "",
0);
lfsr_rcompat_t rcompat;
lfs_ssize_t size = lfsr_data_read(lfs, &data, &rcompat, 1);
if (size < 0) {
return size;
}
if (size < 1) {
rcompat = 0;
}
// unknown rcompat flags? flags must be tightly sized
if (lfsr_rcompat_hasunknown(rcompat) || lfsr_data_size(&data) > 0) {
LFS_ERROR("Incompatible rcompat flags 0x%s%"PRIx8,
(lfsr_data_size(&data) > 0) ? "??" : "",
rcompat);
return LFS_ERR_INVAL;
}
// check for any wflags, we must understand these to write
// grm supported?
if (!lfsr_rcompat_hasgrm(rcompat)) {
LFS_ERROR("Incompatible rcompat flags, no grm");
// TODO switch to read-only? upgrade?
return LFS_ERR_INVAL;
}
// check for any wcompatflags, we must understand these to write
// the filesystem
err = lfsr_mdir_lookup(lfs, mroot, -1, LFSR_TAG_WFLAGS,
err = lfsr_mdir_lookup(lfs, mroot, -1, LFSR_TAG_WCOMPATFLAGS,
NULL, &data);
if (err && err != LFS_ERR_NOENT) {
return err;
}
if (err == LFS_ERR_NOENT) {
data = LFSR_DATA_NULL();
}
if (err != LFS_ERR_NOENT && lfsr_data_size(&data) > 0) {
LFS_ERROR("Incompatible wflag 0x%s%"PRIx32,
(lfsr_data_size(&data) > 0) ? "?" : "",
0);
// unknown wcompat flags? flags must be tightly sized
if (lfsr_data_size(&data) > 0) {
LFS_ERROR("Incompatible wcompat flags 0x??");
// TODO switch to read-only?
return LFS_ERR_INVAL;
}
// check for any oflags, these are optional, if we don't
// understand an oflag we can simply clear it
err = lfsr_mdir_lookup(lfs, mroot, -1, LFSR_TAG_OFLAGS,
NULL, &data);
if (err && err != LFS_ERR_NOENT) {
return err;
}
if (err != LFS_ERR_NOENT && lfsr_data_size(&data) > 0) {
LFS_DEBUG("Found unknown oflag 0x%s%"PRIx32,
(lfsr_data_size(&data) > 0) ? "?" : "",
0);
// TODO track and clear oflags in mkconsistent?
LFS_ASSERT(false);
}
// check block size
err = lfsr_mdir_lookup(lfs, mroot, -1, LFSR_TAG_BLOCKSIZE,
NULL, &data);
@@ -8198,6 +8225,8 @@ static int lfsr_formatinited(lfs_t *lfs) {
LFSR_ATTR(-1, VERSION, 0, IMM(((const uint8_t[2]){
LFS_DISK_VERSION_MAJOR,
LFS_DISK_VERSION_MINOR}), 2)),
LFSR_ATTR(-1, RCOMPATFLAGS, 0, IMM((&(uint8_t){
LFSR_RCOMPAT_GRM}), 1)),
LFSR_ATTR(-1, BLOCKSIZE, 0, LEB128(lfs->cfg->block_size-1)),
LFSR_ATTR(-1, BLOCKCOUNT, 0, LEB128(lfs->cfg->block_count-1)),
LFSR_ATTR(-1, NAMELIMIT, 0, LEB128(lfs->name_limit)),
+2 -3
View File
@@ -14,9 +14,8 @@ TAG_NULL = 0x0000
TAG_CONFIG = 0x0000
TAG_MAGIC = 0x0003
TAG_VERSION = 0x0004
TAG_RFLAGS = 0x0005
TAG_WFLAGS = 0x0006
TAG_OFLAGS = 0x0007
TAG_RCOMPATFLAGS = 0x0006
TAG_WCOMPATFLAGS = 0x0007
TAG_BLOCKSIZE = 0x0008
TAG_BLOCKCOUNT = 0x0009
TAG_NAMELIMIT = 0x000a
+4 -6
View File
@@ -12,9 +12,8 @@ TAG_NULL = 0x0000
TAG_CONFIG = 0x0000
TAG_MAGIC = 0x0003
TAG_VERSION = 0x0004
TAG_RFLAGS = 0x0005
TAG_WFLAGS = 0x0006
TAG_OFLAGS = 0x0007
TAG_RCOMPATFLAGS = 0x0006
TAG_WCOMPATFLAGS = 0x0007
TAG_BLOCKSIZE = 0x0008
TAG_BLOCKCOUNT = 0x0009
TAG_NAMELIMIT = 0x000a
@@ -169,9 +168,8 @@ def tagrepr(tag, w, size, off=None):
'shrub' if tag & TAG_SHRUB else '',
'magic' if (tag & 0xfff) == TAG_MAGIC
else 'version' if (tag & 0xfff) == TAG_VERSION
else 'rflags' if (tag & 0xfff) == TAG_RFLAGS
else 'wflags' if (tag & 0xfff) == TAG_WFLAGS
else 'oflags' if (tag & 0xfff) == TAG_OFLAGS
else 'rcompatflags' if (tag & 0xfff) == TAG_RCOMPATFLAGS
else 'wcompatflags' if (tag & 0xfff) == TAG_WCOMPATFLAGS
else 'blocksize' if (tag & 0xfff) == TAG_BLOCKSIZE
else 'blockcount' if (tag & 0xfff) == TAG_BLOCKCOUNT
else 'sizelimit' if (tag & 0xfff) == TAG_SIZELIMIT
+16 -29
View File
@@ -13,9 +13,8 @@ TAG_NULL = 0x0000
TAG_CONFIG = 0x0000
TAG_MAGIC = 0x0003
TAG_VERSION = 0x0004
TAG_RFLAGS = 0x0005
TAG_WFLAGS = 0x0006
TAG_OFLAGS = 0x0007
TAG_RCOMPATFLAGS = 0x0006
TAG_WCOMPATFLAGS = 0x0007
TAG_BLOCKSIZE = 0x0008
TAG_BLOCKCOUNT = 0x0009
TAG_NAMELIMIT = 0x000a
@@ -198,9 +197,8 @@ def tagrepr(tag, w, size, off=None):
'shrub' if tag & TAG_SHRUB else '',
'magic' if (tag & 0xfff) == TAG_MAGIC
else 'version' if (tag & 0xfff) == TAG_VERSION
else 'rflags' if (tag & 0xfff) == TAG_RFLAGS
else 'wflags' if (tag & 0xfff) == TAG_WFLAGS
else 'oflags' if (tag & 0xfff) == TAG_OFLAGS
else 'rcompatflags' if (tag & 0xfff) == TAG_RCOMPATFLAGS
else 'wcompatflags' if (tag & 0xfff) == TAG_WCOMPATFLAGS
else 'blocksize' if (tag & 0xfff) == TAG_BLOCKSIZE
else 'blockcount' if (tag & 0xfff) == TAG_BLOCKCOUNT
else 'sizelimit' if (tag & 0xfff) == TAG_SIZELIMIT
@@ -1036,25 +1034,17 @@ class Config:
return (None, None)
@ft.cached_property
def rflags(self):
if TAG_RFLAGS in self.config:
_, data = self.config[TAG_RFLAGS]
def rcompatflags(self):
if TAG_RCOMPATFLAGS in self.config:
_, data = self.config[TAG_RCOMPATFLAGS]
return data
else:
return None
@ft.cached_property
def wflags(self):
if TAG_WFLAGS in self.config:
_, data = self.config[TAG_WFLAGS]
return data
else:
return None
@ft.cached_property
def oflags(self):
if TAG_OFLAGS in self.config:
_, data = self.config[TAG_OFLAGS]
def wcompatflags(self):
if TAG_WCOMPATFLAGS in self.config:
_, data = self.config[TAG_WCOMPATFLAGS]
return data
else:
return None
@@ -1157,15 +1147,12 @@ class Config:
for b in map(chr, self.magic))
elif tag == TAG_VERSION:
return 'version v%d.%d' % self.version
elif tag == TAG_RFLAGS:
return 'rflags 0x%s' % ''.join(
'%02x' % f for f in reversed(self.rflags))
elif tag == TAG_WFLAGS:
return 'wflags 0x%s' % ''.join(
'%02x' % f for f in reversed(self.wflags))
elif tag == TAG_OFLAGS:
return 'oflags 0x%s' % ''.join(
'%02x' % f for f in reversed(self.oflags))
elif tag == TAG_RCOMPATFLAGS:
return 'rcompatflags 0x%s' % ''.join(
'%02x' % f for f in reversed(self.rcompatflags))
elif tag == TAG_WCOMPATFLAGS:
return 'wcompatflags 0x%s' % ''.join(
'%02x' % f for f in reversed(self.wcompatflags))
elif tag == TAG_BLOCKSIZE:
return 'blocksize %d' % self.block_size
elif tag == TAG_BLOCKCOUNT:
+4 -6
View File
@@ -12,9 +12,8 @@ TAG_NULL = 0x0000
TAG_CONFIG = 0x0000
TAG_MAGIC = 0x0003
TAG_VERSION = 0x0004
TAG_RFLAGS = 0x0005
TAG_WFLAGS = 0x0006
TAG_OFLAGS = 0x0007
TAG_RCOMPATFLAGS = 0x0006
TAG_WCOMPATFLAGS = 0x0007
TAG_BLOCKSIZE = 0x0008
TAG_BLOCKCOUNT = 0x0009
TAG_NAMELIMIT = 0x000a
@@ -184,9 +183,8 @@ def tagrepr(tag, w, size, off=None):
'shrub' if tag & TAG_SHRUB else '',
'magic' if (tag & 0xfff) == TAG_MAGIC
else 'version' if (tag & 0xfff) == TAG_VERSION
else 'rflags' if (tag & 0xfff) == TAG_RFLAGS
else 'wflags' if (tag & 0xfff) == TAG_WFLAGS
else 'oflags' if (tag & 0xfff) == TAG_OFLAGS
else 'rcompatflags' if (tag & 0xfff) == TAG_RCOMPATFLAGS
else 'wcompatflags' if (tag & 0xfff) == TAG_WCOMPATFLAGS
else 'blocksize' if (tag & 0xfff) == TAG_BLOCKSIZE
else 'blockcount' if (tag & 0xfff) == TAG_BLOCKCOUNT
else 'sizelimit' if (tag & 0xfff) == TAG_SIZELIMIT
+4 -6
View File
@@ -21,9 +21,8 @@ TAG_NULL = 0x0000
TAG_CONFIG = 0x0000
TAG_MAGIC = 0x0003
TAG_VERSION = 0x0004
TAG_RFLAGS = 0x0005
TAG_WFLAGS = 0x0006
TAG_OFLAGS = 0x0007
TAG_RCOMPATFLAGS = 0x0006
TAG_WCOMPATFLAGS = 0x0007
TAG_BLOCKSIZE = 0x0008
TAG_BLOCKCOUNT = 0x0009
TAG_NAMELIMIT = 0x000a
@@ -171,9 +170,8 @@ def tagrepr(tag, w, size, off=None):
'shrub' if tag & TAG_SHRUB else '',
'magic' if (tag & 0xfff) == TAG_MAGIC
else 'version' if (tag & 0xfff) == TAG_VERSION
else 'rflags' if (tag & 0xfff) == TAG_RFLAGS
else 'wflags' if (tag & 0xfff) == TAG_WFLAGS
else 'oflags' if (tag & 0xfff) == TAG_OFLAGS
else 'rcompatflags' if (tag & 0xfff) == TAG_RCOMPATFLAGS
else 'wcompatflags' if (tag & 0xfff) == TAG_WCOMPATFLAGS
else 'blocksize' if (tag & 0xfff) == TAG_BLOCKSIZE
else 'blockcount' if (tag & 0xfff) == TAG_BLOCKCOUNT
else 'sizelimit' if (tag & 0xfff) == TAG_SIZELIMIT