@@ -2703,6 +2703,16 @@ static inline void lfs_superblocktole32(lfs_superblock_t *superblock) {
2703
2703
static int lfs_init (lfs_t * lfs , const struct lfs_config * cfg ) {
2704
2704
lfs -> cfg = cfg ;
2705
2705
2706
+ // check that block size is a multiple of cache size is a multiple
2707
+ // of prog and read sizes
2708
+ LFS_ASSERT (lfs -> cfg -> cache_size % lfs -> cfg -> read_size == 0 );
2709
+ LFS_ASSERT (lfs -> cfg -> cache_size % lfs -> cfg -> prog_size == 0 );
2710
+ LFS_ASSERT (lfs -> cfg -> block_size % lfs -> cfg -> cache_size == 0 );
2711
+
2712
+ // check that the block size is large enough to fit ctz pointers
2713
+ LFS_ASSERT (4 * lfs_npw2 (0xffffffff / (lfs -> cfg -> block_size - 2 * 4 ))
2714
+ <= lfs -> cfg -> block_size );
2715
+
2706
2716
// setup read cache
2707
2717
lfs -> rcache .block = 0xffffffff ;
2708
2718
if (lfs -> cfg -> read_buffer ) {
@@ -2725,7 +2735,7 @@ static int lfs_init(lfs_t *lfs, const struct lfs_config *cfg) {
2725
2735
}
2726
2736
}
2727
2737
2728
- // setup lookahead, round down to nearest 32-bits
2738
+ // setup lookahead, must be multiple of 32-bits
2729
2739
LFS_ASSERT (lfs -> cfg -> lookahead % 32 == 0 );
2730
2740
LFS_ASSERT (lfs -> cfg -> lookahead > 0 );
2731
2741
if (lfs -> cfg -> lookahead_buffer ) {
@@ -2737,22 +2747,12 @@ static int lfs_init(lfs_t *lfs, const struct lfs_config *cfg) {
2737
2747
}
2738
2748
}
2739
2749
2740
- // check that block size is a multiple of cache size is a multiple
2741
- // of prog and read sizes
2742
- LFS_ASSERT (lfs -> cfg -> cache_size % lfs -> cfg -> read_size == 0 );
2743
- LFS_ASSERT (lfs -> cfg -> cache_size % lfs -> cfg -> prog_size == 0 );
2744
- LFS_ASSERT (lfs -> cfg -> block_size % lfs -> cfg -> cache_size == 0 );
2745
-
2746
- // check that the block size is large enough to fit ctz pointers
2747
- LFS_ASSERT (4 * lfs_npw2 (0xffffffff / (lfs -> cfg -> block_size - 2 * 4 ))
2748
- <= lfs -> cfg -> block_size );
2749
-
2750
2750
// check that the size limits are sane
2751
2751
LFS_ASSERT (lfs -> cfg -> inline_size <= LFS_INLINE_MAX );
2752
- LFS_ASSERT (lfs -> cfg -> inline_size <= lfs -> cfg -> read_size ); // TODO
2752
+ LFS_ASSERT (lfs -> cfg -> inline_size <= lfs -> cfg -> cache_size );
2753
2753
lfs -> inline_size = lfs -> cfg -> inline_size ;
2754
2754
if (!lfs -> inline_size ) {
2755
- lfs -> inline_size = lfs_min (LFS_INLINE_MAX , lfs -> cfg -> read_size );
2755
+ lfs -> inline_size = lfs_min (LFS_INLINE_MAX , lfs -> cfg -> cache_size );
2756
2756
}
2757
2757
2758
2758
LFS_ASSERT (lfs -> cfg -> attr_size <= LFS_ATTR_MAX );
@@ -2841,9 +2841,9 @@ int lfs_format(lfs_t *lfs, const struct lfs_config *cfg) {
2841
2841
2842
2842
.block_size = lfs -> cfg -> block_size ,
2843
2843
.block_count = lfs -> cfg -> block_count ,
2844
- .inline_size = lfs -> inline_size ,
2845
2844
.attr_size = lfs -> attr_size ,
2846
2845
.name_size = lfs -> name_size ,
2846
+ .inline_size = lfs -> inline_size ,
2847
2847
};
2848
2848
2849
2849
lfs_superblocktole32 (& superblock );
@@ -2883,9 +2883,6 @@ int lfs_mount(lfs_t *lfs, const struct lfs_config *cfg) {
2883
2883
lfs_mdir_t superdir ;
2884
2884
err = lfs_dir_fetch (lfs , & superdir , (const lfs_block_t [2 ]){0 , 1 });
2885
2885
if (err ) {
2886
- if (err == LFS_ERR_CORRUPT ) {
2887
- LFS_ERROR ("Invalid superblock at %d %d" , 0 , 1 );
2888
- }
2889
2886
return err ;
2890
2887
}
2891
2888
@@ -2899,8 +2896,8 @@ int lfs_mount(lfs_t *lfs, const struct lfs_config *cfg) {
2899
2896
lfs_superblockfromle32 (& superblock );
2900
2897
2901
2898
if (memcmp (superblock .magic , "littlefs" , 8 ) != 0 ) {
2902
- LFS_ERROR ("Invalid superblock at %d %d" , 0 , 1 );
2903
- return LFS_ERR_CORRUPT ;
2899
+ LFS_ERROR ("Invalid superblock \"%.8s\"" , superblock . magic );
2900
+ return LFS_ERR_INVAL ;
2904
2901
}
2905
2902
2906
2903
uint16_t major_version = (0xffff & (superblock .version >> 16 ));
@@ -2919,16 +2916,7 @@ int lfs_mount(lfs_t *lfs, const struct lfs_config *cfg) {
2919
2916
}
2920
2917
lfs_pairfromle32 (lfs -> root );
2921
2918
2922
- if (superblock .inline_size ) {
2923
- if (superblock .inline_size > lfs -> inline_size ) {
2924
- LFS_ERROR ("Unsupported inline size (%d > %d)" ,
2925
- superblock .inline_size , lfs -> inline_size );
2926
- return LFS_ERR_INVAL ;
2927
- }
2928
-
2929
- lfs -> inline_size = superblock .inline_size ;
2930
- }
2931
-
2919
+ // check superblock configuration
2932
2920
if (superblock .attr_size ) {
2933
2921
if (superblock .attr_size > lfs -> attr_size ) {
2934
2922
LFS_ERROR ("Unsupported attr size (%d > %d)" ,
@@ -2949,6 +2937,16 @@ int lfs_mount(lfs_t *lfs, const struct lfs_config *cfg) {
2949
2937
lfs -> name_size = superblock .name_size ;
2950
2938
}
2951
2939
2940
+ if (superblock .inline_size ) {
2941
+ if (superblock .inline_size > lfs -> inline_size ) {
2942
+ LFS_ERROR ("Unsupported inline size (%d > %d)" ,
2943
+ superblock .inline_size , lfs -> inline_size );
2944
+ return LFS_ERR_INVAL ;
2945
+ }
2946
+
2947
+ lfs -> inline_size = superblock .inline_size ;
2948
+ }
2949
+
2952
2950
// scan for any global updates
2953
2951
lfs_mdir_t dir = {.tail = {0 , 1 }};
2954
2952
while (!lfs_pairisnull (dir .tail )) {
0 commit comments