mirror of
				https://github.com/eledio-devices/thirdparty-littlefs.git
				synced 2025-10-31 00:32:38 +01:00 
			
		
		
		
	Added allocation randomization for dynamic wear-leveling
This implements the second step of full dynamic wear-leveling, block allocation randomization. This is the key part the uniformly distributes wear across the filesystem, even through reboots. The entropy actually comes from the filesystem itself, by xoring together all of the CRCs in the metadata-pairs on the filesystem. While this sounds like a ridiculous operation, it's easy to do when we already scan the metadata-pairs at mount time. This gives us a random number we can use for block allocation. Unfortunately it's not a great general purpose random generator as the output only changes every filesystem write. Fortunately that's exactly when we need our allocator. --- Additionally, the randomization created a mess for the testing framework. Fortunately, this method of randomization is deterministic. A very useful property for reproducing bugs.
This commit is contained in:
		
							
								
								
									
										15
									
								
								lfs.c
									
									
									
									
									
								
							
							
						
						
									
										15
									
								
								lfs.c
									
									
									
									
									
								
							| @@ -879,6 +879,8 @@ static int32_t lfs_dir_fetchmatch(lfs_t *lfs, | ||||
|                 dir->tail[1] = temptail[1]; | ||||
|                 dir->split = tempsplit; | ||||
|                 dir->locals = templocals; | ||||
|  | ||||
|                 lfs->seed ^= crc; | ||||
|                 crc = 0xffffffff; | ||||
|             } else { | ||||
|                 err = lfs_bd_crc32(lfs, dir->pair[0], | ||||
| @@ -2874,6 +2876,7 @@ static int lfs_init(lfs_t *lfs, const struct lfs_config *cfg) { | ||||
|     lfs->root[0] = 0xffffffff; | ||||
|     lfs->root[1] = 0xffffffff; | ||||
|     lfs->mlist = NULL; | ||||
|     lfs->seed = 0; | ||||
|     lfs->globals.s.movepair[0] = 0xffffffff; | ||||
|     lfs->globals.s.movepair[1] = 0xffffffff; | ||||
|     lfs->globals.s.moveid = 0x3ff; | ||||
| @@ -2962,12 +2965,6 @@ int lfs_mount(lfs_t *lfs, const struct lfs_config *cfg) { | ||||
|         return err; | ||||
|     } | ||||
|  | ||||
|     // setup free lookahead | ||||
|     lfs->free.off = 0; | ||||
|     lfs->free.size = 0; | ||||
|     lfs->free.i = 0; | ||||
|     lfs_alloc_ack(lfs); | ||||
|  | ||||
|     // load superblock | ||||
|     lfs_mdir_t root; | ||||
|     err = lfs_dir_fetch(lfs, &root, (const lfs_block_t[2]){0, 1}); | ||||
| @@ -3065,6 +3062,12 @@ int lfs_mount(lfs_t *lfs, const struct lfs_config *cfg) { | ||||
|                 lfs->globals.s.moveid); | ||||
|     } | ||||
|  | ||||
|     // setup free lookahead | ||||
|     lfs->free.off = lfs->seed % lfs->cfg->block_size; | ||||
|     lfs->free.size = 0; | ||||
|     lfs->free.i = 0; | ||||
|     lfs_alloc_ack(lfs); | ||||
|  | ||||
|     return 0; | ||||
|  | ||||
| cleanup: | ||||
|   | ||||
		Reference in New Issue
	
	Block a user