mirror of
				https://github.com/eledio-devices/thirdparty-littlefs.git
				synced 2025-10-31 00:32:38 +01:00 
			
		
		
		
	Cleaned up a few additional commit corner cases
- General cleanup from integration, including cleaning up some older commit code - Partial-prog tests do not make sense when prog_size == block_size (there can't be partial-progs!) - Fixed signed-comparison issue in modified filebd
This commit is contained in:
		| @@ -97,7 +97,7 @@ int lfs_filebd_read(const struct lfs_config *cfg, lfs_block_t block, | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     // file truncated? zero for reproducability |     // file truncated? zero for reproducability | ||||||
|     if (res2 < size) { |     if ((lfs_size_t)res2 < size) { | ||||||
|         memset((uint8_t*)buffer + res2, 0, size-res2); |         memset((uint8_t*)buffer + res2, 0, size-res2); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|   | |||||||
							
								
								
									
										212
									
								
								lfs.c
									
									
									
									
									
								
							
							
						
						
									
										212
									
								
								lfs.c
									
									
									
									
									
								
							| @@ -1005,12 +1005,13 @@ static lfs_stag_t lfs_dir_fetchmatch(lfs_t *lfs, | |||||||
|                 // reset crc |                 // reset crc | ||||||
|                 crc = 0xffffffff; |                 crc = 0xffffffff; | ||||||
|  |  | ||||||
|                 // check if the next page is erased |  | ||||||
|                 // |  | ||||||
|                 // this may look inefficient, but it's surprisingly efficient |  | ||||||
|                 // since cache_size is probably > prog_size, so the data will |  | ||||||
|                 // always remain in cache for the next iteration |  | ||||||
|                 if (lfs_tag_chunk(tag) == 3) { |                 if (lfs_tag_chunk(tag) == 3) { | ||||||
|  |                     // check if the next page is erased | ||||||
|  |                     // | ||||||
|  |                     // this may look inefficient, but since cache_size is | ||||||
|  |                     // probably > prog_size, the data will always remain in | ||||||
|  |                     // cache for the next iteration | ||||||
|  |  | ||||||
|                     // first we get a tag-worth of bits, this is so we can |                     // first we get a tag-worth of bits, this is so we can | ||||||
|                     // tweak our current tag to force future writes to be |                     // tweak our current tag to force future writes to be | ||||||
|                     // different than the erased state |                     // different than the erased state | ||||||
| @@ -1018,12 +1019,7 @@ static lfs_stag_t lfs_dir_fetchmatch(lfs_t *lfs, | |||||||
|                     err = lfs_bd_read(lfs, |                     err = lfs_bd_read(lfs, | ||||||
|                             NULL, &lfs->rcache, lfs->cfg->block_size, |                             NULL, &lfs->rcache, lfs->cfg->block_size, | ||||||
|                             dir->pair[0], dir->off, &etag, sizeof(etag)); |                             dir->pair[0], dir->off, &etag, sizeof(etag)); | ||||||
|                     if (err) { |                     if (err && err != LFS_ERR_CORRUPT) { | ||||||
|                         // TODO can we stop duplicating this error condition? |  | ||||||
|                         if (err == LFS_ERR_CORRUPT) { |  | ||||||
|                             dir->erased = false; |  | ||||||
|                             break; |  | ||||||
|                         } |  | ||||||
|                         return err; |                         return err; | ||||||
|                     } |                     } | ||||||
|  |  | ||||||
| @@ -1037,11 +1033,7 @@ static lfs_stag_t lfs_dir_fetchmatch(lfs_t *lfs, | |||||||
|                     err = lfs_bd_crc(lfs, |                     err = lfs_bd_crc(lfs, | ||||||
|                             NULL, &lfs->rcache, lfs->cfg->block_size, |                             NULL, &lfs->rcache, lfs->cfg->block_size, | ||||||
|                             dir->pair[0], dir->off, estate.size, &tcrc); |                             dir->pair[0], dir->off, estate.size, &tcrc); | ||||||
|                     if (err) { |                     if (err && err != LFS_ERR_CORRUPT) { | ||||||
|                         if (err == LFS_ERR_CORRUPT) { |  | ||||||
|                             dir->erased = false; |  | ||||||
|                             break; |  | ||||||
|                         } |  | ||||||
|                         return err; |                         return err; | ||||||
|                     } |                     } | ||||||
|  |  | ||||||
| @@ -1049,84 +1041,86 @@ static lfs_stag_t lfs_dir_fetchmatch(lfs_t *lfs, | |||||||
|                         dir->erased = true; |                         dir->erased = true; | ||||||
|                         break; |                         break; | ||||||
|                     } |                     } | ||||||
|                 } else { |                 } else if (lfs_tag_chunk(tag) == 2) { | ||||||
|                     // end of block commit |                     // end of block commit | ||||||
|                     // TODO handle backwards compat? |  | ||||||
|                     dir->erased = false; |                     dir->erased = false; | ||||||
|                     break; |                     break; | ||||||
|  |                 } else { | ||||||
|  |                     // for backwards compatibility we fall through here on | ||||||
|  |                     // unrecognized tags, leaving it up to the CRC to reject | ||||||
|  |                     // bad commits | ||||||
|                 } |                 } | ||||||
|  |             } else { | ||||||
|                 continue; |                 // crc the entry first, hopefully leaving it in the cache | ||||||
|             } |                 err = lfs_bd_crc(lfs, | ||||||
|  |  | ||||||
|             // crc the entry first, hopefully leaving it in the cache |  | ||||||
|             err = lfs_bd_crc(lfs, |  | ||||||
|                     NULL, &lfs->rcache, lfs->cfg->block_size, |  | ||||||
|                     dir->pair[0], off+sizeof(tag), |  | ||||||
|                     lfs_tag_dsize(tag)-sizeof(tag), &crc); |  | ||||||
|             if (err) { |  | ||||||
|                 if (err == LFS_ERR_CORRUPT) { |  | ||||||
|                     dir->erased = false; |  | ||||||
|                     break; |  | ||||||
|                 } |  | ||||||
|                 return err; |  | ||||||
|             } |  | ||||||
|  |  | ||||||
|             // directory modification tags? |  | ||||||
|             if (lfs_tag_type1(tag) == LFS_TYPE_NAME) { |  | ||||||
|                 // increase count of files if necessary |  | ||||||
|                 if (lfs_tag_id(tag) >= tempcount) { |  | ||||||
|                     tempcount = lfs_tag_id(tag) + 1; |  | ||||||
|                 } |  | ||||||
|             } else if (lfs_tag_type1(tag) == LFS_TYPE_SPLICE) { |  | ||||||
|                 tempcount += lfs_tag_splice(tag); |  | ||||||
|  |  | ||||||
|                 if (tag == (LFS_MKTAG(LFS_TYPE_DELETE, 0, 0) | |  | ||||||
|                         (LFS_MKTAG(0, 0x3ff, 0) & tempbesttag))) { |  | ||||||
|                     tempbesttag |= 0x80000000; |  | ||||||
|                 } else if (tempbesttag != -1 && |  | ||||||
|                         lfs_tag_id(tag) <= lfs_tag_id(tempbesttag)) { |  | ||||||
|                     tempbesttag += LFS_MKTAG(0, lfs_tag_splice(tag), 0); |  | ||||||
|                 } |  | ||||||
|             } else if (lfs_tag_type1(tag) == LFS_TYPE_TAIL) { |  | ||||||
|                 tempsplit = (lfs_tag_chunk(tag) & 1); |  | ||||||
|  |  | ||||||
|                 err = lfs_bd_read(lfs, |  | ||||||
|                         NULL, &lfs->rcache, lfs->cfg->block_size, |                         NULL, &lfs->rcache, lfs->cfg->block_size, | ||||||
|                         dir->pair[0], off+sizeof(tag), &temptail, 8); |                         dir->pair[0], off+sizeof(tag), | ||||||
|  |                         lfs_tag_dsize(tag)-sizeof(tag), &crc); | ||||||
|                 if (err) { |                 if (err) { | ||||||
|                     if (err == LFS_ERR_CORRUPT) { |                     if (err == LFS_ERR_CORRUPT) { | ||||||
|                         dir->erased = false; |                         dir->erased = false; | ||||||
|                         break; |                         break; | ||||||
|                     } |                     } | ||||||
|  |                     return err; | ||||||
|                 } |                 } | ||||||
|                 lfs_pair_fromle32(temptail); |  | ||||||
|             } |  | ||||||
|  |  | ||||||
|             // found a match for our fetcher? |                 // directory modification tags? | ||||||
|             if ((fmask & tag) == (fmask & ftag)) { |                 if (lfs_tag_type1(tag) == LFS_TYPE_NAME) { | ||||||
|                 int res = cb(data, tag, &(struct lfs_diskoff){ |                     // increase count of files if necessary | ||||||
|                         dir->pair[0], off+sizeof(tag)}); |                     if (lfs_tag_id(tag) >= tempcount) { | ||||||
|                 if (res < 0) { |                         tempcount = lfs_tag_id(tag) + 1; | ||||||
|                     if (res == LFS_ERR_CORRUPT) { |  | ||||||
|                         dir->erased = false; |  | ||||||
|                         break; |  | ||||||
|                     } |                     } | ||||||
|                     return res; |                 } else if (lfs_tag_type1(tag) == LFS_TYPE_SPLICE) { | ||||||
|  |                     tempcount += lfs_tag_splice(tag); | ||||||
|  |  | ||||||
|  |                     if (tag == (LFS_MKTAG(LFS_TYPE_DELETE, 0, 0) | | ||||||
|  |                             (LFS_MKTAG(0, 0x3ff, 0) & tempbesttag))) { | ||||||
|  |                         tempbesttag |= 0x80000000; | ||||||
|  |                     } else if (tempbesttag != -1 && | ||||||
|  |                             lfs_tag_id(tag) <= lfs_tag_id(tempbesttag)) { | ||||||
|  |                         tempbesttag += LFS_MKTAG(0, lfs_tag_splice(tag), 0); | ||||||
|  |                     } | ||||||
|  |                 } else if (lfs_tag_type1(tag) == LFS_TYPE_TAIL) { | ||||||
|  |                     tempsplit = (lfs_tag_chunk(tag) & 1); | ||||||
|  |  | ||||||
|  |                     err = lfs_bd_read(lfs, | ||||||
|  |                             NULL, &lfs->rcache, lfs->cfg->block_size, | ||||||
|  |                             dir->pair[0], off+sizeof(tag), &temptail, 8); | ||||||
|  |                     if (err) { | ||||||
|  |                         if (err == LFS_ERR_CORRUPT) { | ||||||
|  |                             dir->erased = false; | ||||||
|  |                             break; | ||||||
|  |                         } | ||||||
|  |                     } | ||||||
|  |                     lfs_pair_fromle32(temptail); | ||||||
|                 } |                 } | ||||||
|  |  | ||||||
|                 if (res == LFS_CMP_EQ) { |                 // found a match for our fetcher? | ||||||
|                     // found a match |                 if ((fmask & tag) == (fmask & ftag)) { | ||||||
|                     tempbesttag = tag; |                     int res = cb(data, tag, &(struct lfs_diskoff){ | ||||||
|                 } else if ((LFS_MKTAG(0x7ff, 0x3ff, 0) & tag) == |                             dir->pair[0], off+sizeof(tag)}); | ||||||
|                         (LFS_MKTAG(0x7ff, 0x3ff, 0) & tempbesttag)) { |                     if (res < 0) { | ||||||
|                     // found an identical tag, but contents didn't match |                         if (res == LFS_ERR_CORRUPT) { | ||||||
|                     // this must mean that our besttag has been overwritten |                             dir->erased = false; | ||||||
|                     tempbesttag = -1; |                             break; | ||||||
|                 } else if (res == LFS_CMP_GT && |                         } | ||||||
|                         lfs_tag_id(tag) <= lfs_tag_id(tempbesttag)) { |                         return res; | ||||||
|                     // found a greater match, keep track to keep things sorted |                     } | ||||||
|                     tempbesttag = tag | 0x80000000; |  | ||||||
|  |                     if (res == LFS_CMP_EQ) { | ||||||
|  |                         // found a match | ||||||
|  |                         tempbesttag = tag; | ||||||
|  |                     } else if ((LFS_MKTAG(0x7ff, 0x3ff, 0) & tag) == | ||||||
|  |                             (LFS_MKTAG(0x7ff, 0x3ff, 0) & tempbesttag)) { | ||||||
|  |                         // found an identical tag, but contents didn't match | ||||||
|  |                         // this must mean that our besttag has been overwritten | ||||||
|  |                         tempbesttag = -1; | ||||||
|  |                     } else if (res == LFS_CMP_GT && | ||||||
|  |                             lfs_tag_id(tag) <= lfs_tag_id(tempbesttag)) { | ||||||
|  |                         // found a greater match, keep track to keep | ||||||
|  |                         // things sorted | ||||||
|  |                         tempbesttag = tag | 0x80000000; | ||||||
|  |                     } | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
| @@ -1484,7 +1478,6 @@ static int lfs_dir_commitcrc(lfs_t *lfs, struct lfs_commit *commit) { | |||||||
|             int err = lfs_bd_read(lfs, |             int err = lfs_bd_read(lfs, | ||||||
|                     NULL, &lfs->rcache, esize, |                     NULL, &lfs->rcache, esize, | ||||||
|                     commit->block, noff, &etag, sizeof(etag)); |                     commit->block, noff, &etag, sizeof(etag)); | ||||||
|             // TODO handle erased-as-corrupt correctly? |  | ||||||
|             if (err && err != LFS_ERR_CORRUPT) { |             if (err && err != LFS_ERR_CORRUPT) { | ||||||
|                 return err; |                 return err; | ||||||
|             } |             } | ||||||
| @@ -1494,7 +1487,6 @@ static int lfs_dir_commitcrc(lfs_t *lfs, struct lfs_commit *commit) { | |||||||
|             err = lfs_bd_crc(lfs, |             err = lfs_bd_crc(lfs, | ||||||
|                     NULL, &lfs->rcache, esize, |                     NULL, &lfs->rcache, esize, | ||||||
|                     commit->block, noff, esize, &ecrc); |                     commit->block, noff, esize, &ecrc); | ||||||
|             // TODO handle erased-as-corrupt correctly? |  | ||||||
|             if (err && err != LFS_ERR_CORRUPT) { |             if (err && err != LFS_ERR_CORRUPT) { | ||||||
|                 return err; |                 return err; | ||||||
|             } |             } | ||||||
| @@ -1540,44 +1532,34 @@ static int lfs_dir_commitcrc(lfs_t *lfs, struct lfs_commit *commit) { | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     // successful commit, check checksums to make sure |     // successful commit, check checksums to make sure | ||||||
|  |     // | ||||||
|  |     // note that we don't need to check padding commits, worst | ||||||
|  |     // case if they are corrupted we would have had to compact anyways | ||||||
|     lfs_off_t off = commit->begin; |     lfs_off_t off = commit->begin; | ||||||
|     lfs_off_t noff = off1; |     uint32_t crc = 0xffffffff; | ||||||
|     while (off < end) { |     err = lfs_bd_crc(lfs, | ||||||
|         // TODO restructure to use lfs_bd_crc? |             NULL, &lfs->rcache, off1+sizeof(uint32_t), | ||||||
|         uint32_t crc = 0xffffffff; |             commit->block, off, off1-off, &crc); | ||||||
|         for (lfs_off_t i = off; i < noff+sizeof(uint32_t); i++) { |     if (err) { | ||||||
|             // check against written crc, may catch blocks that |         return err; | ||||||
|             // become readonly and match our commit size exactly |     } | ||||||
|             if (i == off1 && crc != crc1) { |  | ||||||
|                 return LFS_ERR_CORRUPT; |  | ||||||
|             } |  | ||||||
|  |  | ||||||
|             uint8_t dat; |     // check against known crc for non-padding commits | ||||||
|             err = lfs_bd_read(lfs, |     if (crc != crc1) { | ||||||
|                     NULL, &lfs->rcache, noff+sizeof(uint32_t)-i, |         return LFS_ERR_CORRUPT; | ||||||
|                     commit->block, i, &dat, 1); |     } | ||||||
|             if (err) { |  | ||||||
|                 return err; |  | ||||||
|             } |  | ||||||
|  |  | ||||||
|             crc = lfs_crc(crc, &dat, 1); |     // make sure to check crc in case we happened to pick | ||||||
|         } |     // up an unrelated crc (frozen block?) | ||||||
|  |     err = lfs_bd_crc(lfs, | ||||||
|  |             NULL, &lfs->rcache, sizeof(uint32_t), | ||||||
|  |             commit->block, off1, sizeof(uint32_t), &crc); | ||||||
|  |     if (err) { | ||||||
|  |         return err; | ||||||
|  |     } | ||||||
|  |  | ||||||
|         // detected write error? |     if (crc != 0) { | ||||||
|         if (crc != 0) { |         return LFS_ERR_CORRUPT; | ||||||
|             return LFS_ERR_CORRUPT; |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         // skip padding, note that these always contain estate |  | ||||||
|         off = noff - 3*sizeof(uint32_t); |  | ||||||
|         off = lfs_min(end - off, 0x3fe) + off; |  | ||||||
|         if (off < end) { |  | ||||||
|             off = lfs_min(off, end - 2*sizeof(uint32_t)); |  | ||||||
|         } |  | ||||||
|         noff = off + sizeof(uint32_t); |  | ||||||
|         if (noff <= lfs->cfg->block_size - esize) { |  | ||||||
|             noff += 2*sizeof(uint32_t); |  | ||||||
|         } |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     return 0; |     return 0; | ||||||
|   | |||||||
| @@ -84,6 +84,7 @@ code = ''' | |||||||
| ''' | ''' | ||||||
|  |  | ||||||
| [[case]] # partial prog, may not be byte in order! | [[case]] # partial prog, may not be byte in order! | ||||||
|  | if = "LFS_PROG_SIZE < LFS_BLOCK_SIZE" | ||||||
| define.BYTE_OFF = ["0", "LFS_PROG_SIZE-1", "LFS_PROG_SIZE/2"] | define.BYTE_OFF = ["0", "LFS_PROG_SIZE-1", "LFS_PROG_SIZE/2"] | ||||||
| define.BYTE_VALUE = [0x33, 0xcc] | define.BYTE_VALUE = [0x33, 0xcc] | ||||||
| in = "lfs.c" | in = "lfs.c" | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user