@@ -265,7 +265,6 @@ static int32_t lfs_fs_parent(lfs_t *lfs, const lfs_block_t dir[2],
265
265
lfs_mdir_t * parent );
266
266
static int lfs_fs_relocate (lfs_t * lfs ,
267
267
const lfs_block_t oldpair [2 ], lfs_block_t newpair [2 ]);
268
- static int lfs_fs_scan (lfs_t * lfs );
269
268
static int lfs_fs_forceconsistency (lfs_t * lfs );
270
269
271
270
@@ -410,80 +409,50 @@ static inline lfs_size_t lfs_tagsize(uint32_t tag) {
410
409
411
410
// operations on set of globals
412
411
static inline void lfs_globalxor (lfs_global_t * a , const lfs_global_t * b ) {
413
- for (int i = 0 ; i < sizeof (lfs_global_t )/2 ; i ++ ) {
414
- a -> u16 [i ] ^= b -> u16 [i ];
412
+ for (int i = 0 ; i < sizeof (lfs_global_t )/4 ; i ++ ) {
413
+ a -> u32 [i ] ^= b -> u32 [i ];
415
414
}
416
415
}
417
416
418
417
static inline bool lfs_globaliszero (const lfs_global_t * a ) {
419
- for (int i = 0 ; i < sizeof (lfs_global_t )/2 ; i ++ ) {
420
- if (a -> u16 [i ] != 0 ) {
418
+ for (int i = 0 ; i < sizeof (lfs_global_t )/4 ; i ++ ) {
419
+ if (a -> u32 [i ] != 0 ) {
421
420
return false;
422
421
}
423
422
}
424
423
return true;
425
424
}
426
425
427
426
static inline void lfs_globalzero (lfs_global_t * a ) {
428
- memset (a -> u16 , 0x00 , sizeof (lfs_global_t ));
429
- }
430
-
431
- static inline void lfs_globalones (lfs_global_t * a ) {
432
- memset (a -> u16 , 0xff , sizeof (lfs_global_t ));
433
- }
434
-
435
- static inline void lfs_globalxormove (lfs_global_t * a ,
436
- const lfs_block_t pair [2 ], uint16_t id ) {
437
- a -> u16 [0 ] ^= id ;
438
- for (int i = 0 ; i < sizeof (lfs_block_t [2 ])/2 ; i ++ ) {
439
- a -> u16 [1 + i ] ^= ((uint16_t * )pair )[i ];
440
- }
441
- }
442
-
443
- static inline void lfs_globalxordeorphaned (lfs_global_t * a , bool deorphaned ) {
444
- a -> u16 [0 ] ^= deorphaned << 15 ;
427
+ lfs_globalxor (a , a );
445
428
}
446
429
447
430
static inline void lfs_globalfromle32 (lfs_global_t * a ) {
448
- a -> u16 [ 0 ] = lfs_fromle16 (a -> u16 [ 0 ] );
449
- lfs_pairfromle32 (( lfs_block_t * ) & a -> u16 [ 1 ] );
431
+ lfs_pairfromle32 (a -> s . movepair );
432
+ a -> s . moveid = lfs_fromle16 ( a -> s . moveid );
450
433
}
451
434
452
435
static inline void lfs_globaltole32 (lfs_global_t * a ) {
453
- a -> u16 [0 ] = lfs_tole16 (a -> u16 [0 ]);
454
- lfs_pairtole32 ((lfs_block_t * )& a -> u16 [1 ]);
455
- }
456
-
457
- static inline const lfs_block_t * lfs_globalmovepair (const lfs_t * lfs ) {
458
- return (const lfs_block_t * )& lfs -> globals .u16 [1 ];
459
- }
460
-
461
- static inline uint16_t lfs_globalmoveid (const lfs_t * lfs ) {
462
- return 0x3ff & lfs -> globals .u16 [0 ];
463
- }
464
-
465
- static inline bool lfs_globalisdeorphaned (const lfs_t * lfs ) {
466
- return 0x8000 & lfs -> globals .u16 [0 ];
436
+ lfs_pairtole32 (a -> s .movepair );
437
+ a -> s .moveid = lfs_tole16 (a -> s .moveid );
467
438
}
468
439
469
440
static inline void lfs_globalmove (lfs_t * lfs ,
470
441
const lfs_block_t pair [2 ], uint16_t id ) {
471
442
lfs_global_t diff ;
472
443
lfs_globalzero (& diff );
473
- lfs_globalxormove (& diff , lfs_globalmovepair (lfs ), lfs_globalmoveid (lfs ));
474
- lfs_globalxormove (& diff , pair , id );
444
+ diff .s .movepair [0 ] ^= lfs -> globals .s .movepair [0 ] ^ pair [0 ];
445
+ diff .s .movepair [1 ] ^= lfs -> globals .s .movepair [1 ] ^ pair [1 ];
446
+ diff .s .moveid ^= lfs -> globals .s .moveid ^ id ;
475
447
lfs_globalfromle32 (& lfs -> locals );
476
448
lfs_globalxor (& lfs -> locals , & diff );
477
449
lfs_globaltole32 (& lfs -> locals );
478
450
lfs_globalxor (& lfs -> globals , & diff );
479
451
}
480
452
481
453
static inline void lfs_globaldeorphaned (lfs_t * lfs , bool deorphaned ) {
482
- deorphaned ^= lfs_globalisdeorphaned (lfs );
483
- lfs_globalfromle32 (& lfs -> locals );
484
- lfs_globalxordeorphaned (& lfs -> locals , deorphaned );
485
- lfs_globaltole32 (& lfs -> locals );
486
- lfs_globalxordeorphaned (& lfs -> globals , deorphaned );
454
+ lfs -> locals .s .deorphaned ^= lfs -> globals .s .deorphaned ^ deorphaned ;
455
+ lfs -> globals .s .deorphaned ^= lfs -> globals .s .deorphaned ^ deorphaned ;
487
456
}
488
457
489
458
@@ -696,7 +665,8 @@ static int lfs_commitglobals(lfs_t *lfs, struct lfs_commit *commit,
696
665
697
666
lfs_globalxor (locals , & lfs -> locals );
698
667
int err = lfs_commitattr (lfs , commit ,
699
- LFS_MKTAG (LFS_TYPE_GLOBALS , 0x3ff , sizeof (lfs_global_t )), locals );
668
+ LFS_MKTAG (LFS_TYPE_GLOBALS + locals -> s .deorphaned ,
669
+ 0x3ff , sizeof (lfs_global_t )), locals );
700
670
lfs_globalxor (locals , & lfs -> locals );
701
671
return err ;
702
672
}
@@ -893,6 +863,7 @@ static int32_t lfs_dir_find(lfs_t *lfs,
893
863
}
894
864
lfs_pairfromle32 (temptail );
895
865
} else if (lfs_tagsubtype (tag ) == LFS_TYPE_GLOBALS ) {
866
+ templocals .s .deorphaned = (lfs_tagtype (tag ) & 1 );
896
867
err = lfs_bd_read (lfs , dir -> pair [0 ], off + sizeof (tag ),
897
868
& templocals , sizeof (templocals ));
898
869
if (err ) {
@@ -929,11 +900,11 @@ static int32_t lfs_dir_find(lfs_t *lfs,
929
900
// consider what we have good enough
930
901
if (dir -> off > 0 ) {
931
902
// synthetic move
932
- if (lfs_paircmp (dir -> pair , lfs_globalmovepair ( lfs ) ) == 0 ) {
933
- if (lfs_globalmoveid ( lfs ) == lfs_tagid (foundtag )) {
903
+ if (lfs_paircmp (dir -> pair , lfs -> globals . s . movepair ) == 0 ) {
904
+ if (lfs -> globals . s . moveid == lfs_tagid (foundtag )) {
934
905
foundtag = LFS_ERR_NOENT ;
935
906
} else if (lfs_tagisvalid (foundtag ) &&
936
- lfs_globalmoveid ( lfs ) < lfs_tagid (foundtag )) {
907
+ lfs -> globals . s . moveid < lfs_tagid (foundtag )) {
937
908
foundtag -= LFS_MKTAG (0 , 1 , 0 );
938
909
}
939
910
}
@@ -963,8 +934,8 @@ static int lfs_dir_fetch(lfs_t *lfs,
963
934
static int32_t lfs_dir_get (lfs_t * lfs , lfs_mdir_t * dir ,
964
935
uint32_t getmask , uint32_t gettag , void * buffer ) {
965
936
int32_t getdiff = 0 ;
966
- if (lfs_paircmp (dir -> pair , lfs_globalmovepair ( lfs ) ) == 0 &&
967
- lfs_tagid (gettag ) <= lfs_globalmoveid ( lfs ) ) {
937
+ if (lfs_paircmp (dir -> pair , lfs -> globals . s . movepair ) == 0 &&
938
+ lfs_tagid (gettag ) <= lfs -> globals . s . moveid ) {
968
939
// synthetic moves
969
940
getdiff = LFS_MKTAG (0 , 1 , 0 );
970
941
}
@@ -1166,18 +1137,17 @@ static int lfs_dir_commit(lfs_t *lfs, lfs_mdir_t *dir,
1166
1137
lfs_mattr_t cancelattr ;
1167
1138
lfs_global_t canceldiff ;
1168
1139
lfs_globalzero (& canceldiff );
1169
- if (lfs_paircmp (dir -> pair , lfs_globalmovepair ( lfs ) ) == 0 ) {
1140
+ if (lfs_paircmp (dir -> pair , lfs -> globals . s . movepair ) == 0 ) {
1170
1141
// Wait, we have the move? Just cancel this out here
1171
1142
// We need to, or else the move can become outdated
1172
- lfs_globalxormove (& canceldiff ,
1173
- lfs_globalmovepair (lfs ), lfs_globalmoveid (lfs ));
1174
- lfs_globalxormove (& canceldiff ,
1175
- (lfs_block_t [2 ]){0xffffffff , 0xffffffff }, 0x3ff );
1143
+ canceldiff .s .movepair [0 ] ^= lfs -> globals .s .movepair [0 ] ^ 0xffffffff ;
1144
+ canceldiff .s .movepair [1 ] ^= lfs -> globals .s .movepair [1 ] ^ 0xffffffff ;
1145
+ canceldiff .s .moveid ^= lfs -> globals .s .moveid ^ 0x3ff ;
1176
1146
lfs_globalfromle32 (& lfs -> locals );
1177
1147
lfs_globalxor (& lfs -> locals , & canceldiff );
1178
1148
lfs_globaltole32 (& lfs -> locals );
1179
1149
1180
- cancelattr .tag = LFS_MKTAG (LFS_TYPE_DELETE , lfs_globalmoveid ( lfs ) , 0 );
1150
+ cancelattr .tag = LFS_MKTAG (LFS_TYPE_DELETE , lfs -> globals . s . moveid , 0 );
1181
1151
cancelattr .next = attrs ;
1182
1152
attrs = & cancelattr ;
1183
1153
}
@@ -2814,7 +2784,10 @@ static int lfs_init(lfs_t *lfs, const struct lfs_config *cfg) {
2814
2784
lfs -> root [0 ] = 0xffffffff ;
2815
2785
lfs -> root [1 ] = 0xffffffff ;
2816
2786
lfs -> mlist = NULL ;
2817
- lfs_globalones (& lfs -> globals );
2787
+ lfs -> globals .s .movepair [0 ] = 0xffffffff ;
2788
+ lfs -> globals .s .movepair [1 ] = 0xffffffff ;
2789
+ lfs -> globals .s .moveid = 0x3ff ;
2790
+ lfs -> globals .s .deorphaned = true;
2818
2791
lfs_globalzero (& lfs -> locals );
2819
2792
2820
2793
return 0 ;
@@ -2920,8 +2893,8 @@ int lfs_mount(lfs_t *lfs, const struct lfs_config *cfg) {
2920
2893
lfs_alloc_ack (lfs );
2921
2894
2922
2895
// load superblock
2923
- lfs_mdir_t dir ;
2924
- err = lfs_dir_fetch (lfs , & dir , (const lfs_block_t [2 ]){0 , 1 });
2896
+ lfs_mdir_t superdir ;
2897
+ err = lfs_dir_fetch (lfs , & superdir , (const lfs_block_t [2 ]){0 , 1 });
2925
2898
if (err ) {
2926
2899
if (err == LFS_ERR_CORRUPT ) {
2927
2900
LFS_ERROR ("Invalid superblock at %d %d" , 0 , 1 );
@@ -2930,7 +2903,7 @@ int lfs_mount(lfs_t *lfs, const struct lfs_config *cfg) {
2930
2903
}
2931
2904
2932
2905
lfs_superblock_t superblock ;
2933
- int32_t res = lfs_dir_get (lfs , & dir , 0x7ffff000 ,
2906
+ int32_t res = lfs_dir_get (lfs , & superdir , 0x7ffff000 ,
2934
2907
LFS_MKTAG (LFS_TYPE_SUPERBLOCK , 0 , sizeof (superblock )),
2935
2908
& superblock );
2936
2909
if (res < 0 ) {
@@ -2951,7 +2924,7 @@ int lfs_mount(lfs_t *lfs, const struct lfs_config *cfg) {
2951
2924
return LFS_ERR_INVAL ;
2952
2925
}
2953
2926
2954
- res = lfs_dir_get (lfs , & dir , 0x7ffff000 ,
2927
+ res = lfs_dir_get (lfs , & superdir , 0x7ffff000 ,
2955
2928
LFS_MKTAG (LFS_TYPE_DIRSTRUCT , 0 , sizeof (lfs -> root )),
2956
2929
& lfs -> root );
2957
2930
if (res < 0 ) {
@@ -2990,9 +2963,26 @@ int lfs_mount(lfs_t *lfs, const struct lfs_config *cfg) {
2990
2963
}
2991
2964
2992
2965
// scan for any global updates
2993
- err = lfs_fs_scan (lfs );
2994
- if (err ) {
2995
- return err ;
2966
+ lfs_mdir_t dir = {.tail = {0 , 1 }};
2967
+ while (!lfs_pairisnull (dir .tail )) {
2968
+ int err = lfs_dir_fetch (lfs , & dir , dir .tail );
2969
+ if (err ) {
2970
+ return err ;
2971
+ }
2972
+
2973
+ // xor together indirect deletes
2974
+ lfs_globalxor (& lfs -> locals , & dir .locals );
2975
+ }
2976
+
2977
+ // update littlefs with globals
2978
+ lfs_globalfromle32 (& lfs -> locals );
2979
+ lfs_globalxor (& lfs -> globals , & lfs -> locals );
2980
+ lfs_globalzero (& lfs -> locals );
2981
+ if (!lfs_pairisnull (lfs -> globals .s .movepair )) {
2982
+ LFS_DEBUG ("Found move %d %d %d" ,
2983
+ lfs -> globals .s .movepair [0 ],
2984
+ lfs -> globals .s .movepair [1 ],
2985
+ lfs -> globals .s .moveid );
2996
2986
}
2997
2987
2998
2988
return 0 ;
@@ -3186,41 +3176,10 @@ static int lfs_fs_relocate(lfs_t *lfs,
3186
3176
return 0 ;
3187
3177
}
3188
3178
3189
- static int lfs_fs_scan (lfs_t * lfs ) {
3190
- if (lfs_pairisnull (lfs -> root )) {
3191
- return 0 ;
3192
- }
3193
-
3194
- // iterate over all directory directory entries
3195
- lfs_mdir_t dir = {.tail = {0 , 1 }};
3196
- while (!lfs_pairisnull (dir .tail )) {
3197
- int err = lfs_dir_fetch (lfs , & dir , dir .tail );
3198
- if (err ) {
3199
- return err ;
3200
- }
3201
-
3202
- // xor together indirect deletes
3203
- lfs_globalxor (& lfs -> locals , & dir .locals );
3204
- }
3205
-
3206
- // update littlefs with globals
3207
- lfs_globalfromle32 (& lfs -> locals );
3208
- lfs_globalxor (& lfs -> globals , & lfs -> locals );
3209
- lfs_globalzero (& lfs -> locals );
3210
- if (!lfs_pairisnull (lfs_globalmovepair (lfs ))) {
3211
- LFS_DEBUG ("Found move %d %d %d" ,
3212
- lfs_globalmovepair (lfs )[0 ],
3213
- lfs_globalmovepair (lfs )[1 ],
3214
- lfs_globalmoveid (lfs ));
3215
- }
3216
-
3217
- return 0 ;
3218
- }
3219
-
3220
3179
static int lfs_fs_forceconsistency (lfs_t * lfs ) {
3221
- if (!lfs_globalisdeorphaned ( lfs ) ) {
3180
+ if (!lfs -> globals . s . deorphaned ) {
3222
3181
LFS_DEBUG ("Found orphans %d" ,
3223
- lfs_globalisdeorphaned ( lfs ) );
3182
+ lfs -> globals . s . deorphaned );
3224
3183
3225
3184
// Fix any orphans
3226
3185
lfs_mdir_t pdir = {.split = true};
@@ -3292,16 +3251,16 @@ static int lfs_fs_forceconsistency(lfs_t *lfs) {
3292
3251
lfs_globaldeorphaned (lfs , false);
3293
3252
}
3294
3253
3295
- if (lfs_globalmoveid ( lfs ) != 0x3ff ) {
3254
+ if (lfs -> globals . s . moveid != 0x3ff ) {
3296
3255
// Fix bad moves
3297
3256
LFS_DEBUG ("Fixing move %d %d %d" ,
3298
- lfs_globalmovepair ( lfs ) [0 ],
3299
- lfs_globalmovepair ( lfs ) [1 ],
3300
- lfs_globalmoveid ( lfs ) );
3257
+ lfs -> globals . s . movepair [0 ],
3258
+ lfs -> globals . s . movepair [1 ],
3259
+ lfs -> globals . s . moveid );
3301
3260
3302
3261
// fetch and delete the moved entry
3303
3262
lfs_mdir_t movedir ;
3304
- int err = lfs_dir_fetch (lfs , & movedir , lfs_globalmovepair ( lfs ) );
3263
+ int err = lfs_dir_fetch (lfs , & movedir , lfs -> globals . s . movepair );
3305
3264
if (err ) {
3306
3265
return err ;
3307
3266
}
0 commit comments