소스 검색

Added LFS_MULTIVERSION, made lfs2.0 support a compile-time option

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.
Christopher Haster 2 년 전
부모
커밋
eb9af7abe5
3개의 변경된 파일29개의 추가작업 그리고 3개의 파일을 삭제
  1. 17 3
      lfs.c
  2. 2 0
      lfs.h
  3. 10 0
      runners/test_runner.c

+ 17 - 3
lfs.c

@@ -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;
     }
 }
@@ -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
             //
@@ -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
@@ -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 = {
@@ -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
     //

+ 2 - 0
lfs.h

@@ -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

+ 10 - 0
runners/test_runner.c

@@ -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 = {
@@ -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 = {
@@ -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 = {
@@ -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 = {
@@ -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 = {