mirror of
				https://github.com/eledio-devices/thirdparty-littlefs.git
				synced 2025-10-31 08:42:40 +01:00 
			
		
		
		
	Initially, littlefs relied entirely on bad-block detection for wear-leveling. Conceptually, at the end of a devices lifespan, all blocks would be worn evenly, even if they weren't worn out at the same time. However, this doesn't work for all devices, rather than causing corruption during writes, wear reduces a devices "sticking power", causing bits to flip over time. This means for many devices, true wear-leveling (dynamic or static) is required. Fortunately, way back at the beginning, littlefs was designed to do full dynamic wear-leveling, only dropping it when making the retrospectively short-sighted realization that bad-block detection is theoretically sufficient. We can enable dynamic wear-leveling with only a few tweaks to littlefs. These can be implemented without breaking backwards compatibility. 1. Evict metadata-pairs after a certain number of writes. Eviction in this case is identical to a relocation to recover from a bad block. We move our data and stick the old block back into our pool of blocks. For knowing when to evict, we already have a revision count for each metadata-pair which gives us enough information. We add the configuration option block_cycles and evict when our revision count is a multiple of this value. 2. Now all blocks participate in COW behaviour. However we don't store the state of our allocator, so every boot cycle we reuse the first blocks on storage. This is very bad on a microcontroller, where we may reboot often. We need a way to spread our usage across the disk. To pull this off, we can simply randomize which block we start our allocator at. But we need a random number generator that is different on each boot. Fortunately we have a great source of entropy, our filesystem. So we seed our block allocator with a simple hash of the CRCs on our metadata-pairs. This can be done for free since we already need to scan the metadata-pairs during mount. What we end up with is a uniform distribution of wear on storage. The wear is not perfect, if a block is used for metadata it gets more wear, and the randomization may not be exact. But we can never actually get perfect wear-leveling, since we're already resigned to dynamic wear-leveling at the file level. With the addition of metadata logging, we end up with a really interesting two-stage wear-leveling algorithm. At the low-level, metadata is statically wear-leveled. At the high-level, blocks are dynamically wear-leveled. --- This specific commit implements the first step, eviction of metadata pairs. Entertwining this into the already complicated compact logic was a bit annoying, however we can combine the logic for superblock expansion with the logic for metadata-pair eviction.
		
			
				
	
	
		
			117 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
	
	
			
		
		
	
	
			117 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
	
	
| /// AUTOGENERATED TEST ///
 | |
| #include "lfs.h"
 | |
| #include "emubd/lfs_emubd.h"
 | |
| #include <stdio.h>
 | |
| #include <string.h>
 | |
| #include <stdlib.h>
 | |
| 
 | |
| 
 | |
| // test stuff
 | |
| static void test_log(const char *s, uintmax_t v) {{
 | |
|     printf("%s: %jd\n", s, v);
 | |
| }}
 | |
| 
 | |
| static void test_assert(const char *file, unsigned line,
 | |
|         const char *s, uintmax_t v, uintmax_t e) {{
 | |
|     static const char *last[6] = {{0, 0}};
 | |
|     if (v != e || !(last[0] == s || last[1] == s ||
 | |
|             last[2] == s || last[3] == s ||
 | |
|             last[4] == s || last[5] == s)) {{
 | |
|         test_log(s, v);
 | |
|         last[0] = last[1];
 | |
|         last[1] = last[2];
 | |
|         last[2] = last[3];
 | |
|         last[3] = last[4];
 | |
|         last[4] = last[5];
 | |
|         last[5] = s;
 | |
|     }}
 | |
| 
 | |
|     if (v != e) {{
 | |
|         fprintf(stderr, "\033[31m%s:%u: assert %s failed with %jd, "
 | |
|                 "expected %jd\033[0m\n", file, line, s, v, e);
 | |
|         exit(-2);
 | |
|     }}
 | |
| }}
 | |
| 
 | |
| #define test_assert(s, v, e) test_assert(__FILE__, __LINE__, s, v, e)
 | |
| 
 | |
| 
 | |
| // utility functions for traversals
 | |
| static int __attribute__((used)) test_count(void *p, lfs_block_t b) {{
 | |
|     (void)b;
 | |
|     unsigned *u = (unsigned*)p;
 | |
|     *u += 1;
 | |
|     return 0;
 | |
| }}
 | |
| 
 | |
| 
 | |
| // lfs declarations
 | |
| lfs_t lfs;
 | |
| lfs_emubd_t bd;
 | |
| lfs_file_t file[4];
 | |
| lfs_dir_t dir[4];
 | |
| struct lfs_info info;
 | |
| 
 | |
| uint8_t buffer[1024];
 | |
| uint8_t wbuffer[1024];
 | |
| uint8_t rbuffer[1024];
 | |
| lfs_size_t size;
 | |
| lfs_size_t wsize;
 | |
| lfs_size_t rsize;
 | |
| 
 | |
| uintmax_t test;
 | |
| 
 | |
| #ifndef LFS_READ_SIZE
 | |
| #define LFS_READ_SIZE 16
 | |
| #endif
 | |
| 
 | |
| #ifndef LFS_PROG_SIZE
 | |
| #define LFS_PROG_SIZE LFS_READ_SIZE
 | |
| #endif
 | |
| 
 | |
| #ifndef LFS_BLOCK_SIZE
 | |
| #define LFS_BLOCK_SIZE 512
 | |
| #endif
 | |
| 
 | |
| #ifndef LFS_BLOCK_COUNT
 | |
| #define LFS_BLOCK_COUNT 1024
 | |
| #endif
 | |
| 
 | |
| #ifndef LFS_BLOCK_CYCLES
 | |
| #define LFS_BLOCK_CYCLES 1024
 | |
| #endif
 | |
| 
 | |
| #ifndef LFS_CACHE_SIZE
 | |
| #define LFS_CACHE_SIZE 64
 | |
| #endif
 | |
| 
 | |
| #ifndef LFS_LOOKAHEAD
 | |
| #define LFS_LOOKAHEAD 128
 | |
| #endif
 | |
| 
 | |
| const struct lfs_config cfg = {{
 | |
|     .context = &bd,
 | |
|     .read  = &lfs_emubd_read,
 | |
|     .prog  = &lfs_emubd_prog,
 | |
|     .erase = &lfs_emubd_erase,
 | |
|     .sync  = &lfs_emubd_sync,
 | |
| 
 | |
|     .read_size    = LFS_READ_SIZE,
 | |
|     .prog_size    = LFS_PROG_SIZE,
 | |
|     .block_size   = LFS_BLOCK_SIZE,
 | |
|     .block_count  = LFS_BLOCK_COUNT,
 | |
|     .block_cycles = LFS_BLOCK_CYCLES,
 | |
|     .cache_size   = LFS_CACHE_SIZE,
 | |
|     .lookahead    = LFS_LOOKAHEAD,
 | |
| }};
 | |
| 
 | |
| 
 | |
| // Entry point
 | |
| int main(void) {{
 | |
|     lfs_emubd_create(&cfg, "blocks");
 | |
| 
 | |
| {tests}
 | |
| 
 | |
|     lfs_emubd_destroy(&cfg);
 | |
| }}
 |