Skip to content

Commit 9dbae39

Browse files
DvdGiessendpgeorge
authored andcommitted
lib/littlefs: Update LittleFS to v2.11.
Signed-off-by: Daniël van de Giessen <[email protected]>
1 parent 670b7c9 commit 9dbae39

File tree

3 files changed

+68
-36
lines changed

3 files changed

+68
-36
lines changed

lib/littlefs/lfs2.c

Lines changed: 54 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3932,7 +3932,9 @@ static int lfs2_remove_(lfs2_t *lfs2, const char *path) {
39323932
}
39333933

39343934
lfs2->mlist = dir.next;
3935-
if (lfs2_tag_type3(tag) == LFS2_TYPE_DIR) {
3935+
if (lfs2_gstate_hasorphans(&lfs2->gstate)) {
3936+
LFS2_ASSERT(lfs2_tag_type3(tag) == LFS2_TYPE_DIR);
3937+
39363938
// fix orphan
39373939
err = lfs2_fs_preporphans(lfs2, -1);
39383940
if (err) {
@@ -4076,8 +4078,10 @@ static int lfs2_rename_(lfs2_t *lfs2, const char *oldpath, const char *newpath)
40764078
}
40774079

40784080
lfs2->mlist = prevdir.next;
4079-
if (prevtag != LFS2_ERR_NOENT
4080-
&& lfs2_tag_type3(prevtag) == LFS2_TYPE_DIR) {
4081+
if (lfs2_gstate_hasorphans(&lfs2->gstate)) {
4082+
LFS2_ASSERT(prevtag != LFS2_ERR_NOENT
4083+
&& lfs2_tag_type3(prevtag) == LFS2_TYPE_DIR);
4084+
40814085
// fix orphan
40824086
err = lfs2_fs_preporphans(lfs2, -1);
40834087
if (err) {
@@ -5233,40 +5237,64 @@ static int lfs2_fs_gc_(lfs2_t *lfs2) {
52335237
#endif
52345238

52355239
#ifndef LFS2_READONLY
5240+
#ifdef LFS2_SHRINKNONRELOCATING
5241+
static int lfs2_shrink_checkblock(void *data, lfs2_block_t block) {
5242+
lfs2_size_t threshold = *((lfs2_size_t*)data);
5243+
if (block >= threshold) {
5244+
return LFS2_ERR_NOTEMPTY;
5245+
}
5246+
return 0;
5247+
}
5248+
#endif
5249+
52365250
static int lfs2_fs_grow_(lfs2_t *lfs2, lfs2_size_t block_count) {
5237-
// shrinking is not supported
5238-
LFS2_ASSERT(block_count >= lfs2->block_count);
5251+
int err;
52395252

5240-
if (block_count > lfs2->block_count) {
5241-
lfs2->block_count = block_count;
5253+
if (block_count == lfs2->block_count) {
5254+
return 0;
5255+
}
52425256

5243-
// fetch the root
5244-
lfs2_mdir_t root;
5245-
int err = lfs2_dir_fetch(lfs2, &root, lfs2->root);
5257+
5258+
#ifndef LFS2_SHRINKNONRELOCATING
5259+
// shrinking is not supported
5260+
LFS2_ASSERT(block_count >= lfs2->block_count);
5261+
#endif
5262+
#ifdef LFS2_SHRINKNONRELOCATING
5263+
if (block_count < lfs2->block_count) {
5264+
err = lfs2_fs_traverse_(lfs2, lfs2_shrink_checkblock, &block_count, true);
52465265
if (err) {
52475266
return err;
52485267
}
5268+
}
5269+
#endif
52495270

5250-
// update the superblock
5251-
lfs2_superblock_t superblock;
5252-
lfs2_stag_t tag = lfs2_dir_get(lfs2, &root, LFS2_MKTAG(0x7ff, 0x3ff, 0),
5253-
LFS2_MKTAG(LFS2_TYPE_INLINESTRUCT, 0, sizeof(superblock)),
5254-
&superblock);
5255-
if (tag < 0) {
5256-
return tag;
5257-
}
5258-
lfs2_superblock_fromle32(&superblock);
5271+
lfs2->block_count = block_count;
52595272

5260-
superblock.block_count = lfs2->block_count;
5273+
// fetch the root
5274+
lfs2_mdir_t root;
5275+
err = lfs2_dir_fetch(lfs2, &root, lfs2->root);
5276+
if (err) {
5277+
return err;
5278+
}
52615279

5262-
lfs2_superblock_tole32(&superblock);
5263-
err = lfs2_dir_commit(lfs2, &root, LFS2_MKATTRS(
5264-
{tag, &superblock}));
5265-
if (err) {
5266-
return err;
5267-
}
5280+
// update the superblock
5281+
lfs2_superblock_t superblock;
5282+
lfs2_stag_t tag = lfs2_dir_get(lfs2, &root, LFS2_MKTAG(0x7ff, 0x3ff, 0),
5283+
LFS2_MKTAG(LFS2_TYPE_INLINESTRUCT, 0, sizeof(superblock)),
5284+
&superblock);
5285+
if (tag < 0) {
5286+
return tag;
52685287
}
5288+
lfs2_superblock_fromle32(&superblock);
5289+
5290+
superblock.block_count = lfs2->block_count;
52695291

5292+
lfs2_superblock_tole32(&superblock);
5293+
err = lfs2_dir_commit(lfs2, &root, LFS2_MKATTRS(
5294+
{tag, &superblock}));
5295+
if (err) {
5296+
return err;
5297+
}
52705298
return 0;
52715299
}
52725300
#endif

lib/littlefs/lfs2.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ extern "C"
2121
// Software library version
2222
// Major (top-nibble), incremented on backwards incompatible changes
2323
// Minor (bottom-nibble), incremented on feature additions
24-
#define LFS2_VERSION 0x0002000a
24+
#define LFS2_VERSION 0x0002000b
2525
#define LFS2_VERSION_MAJOR (0xffff & (LFS2_VERSION >> 16))
2626
#define LFS2_VERSION_MINOR (0xffff & (LFS2_VERSION >> 0))
2727

@@ -766,7 +766,11 @@ int lfs2_fs_gc(lfs2_t *lfs2);
766766
// Grows the filesystem to a new size, updating the superblock with the new
767767
// block count.
768768
//
769-
// Note: This is irreversible.
769+
// If LFS2_SHRINKNONRELOCATING is defined, this function will also accept
770+
// block_counts smaller than the current configuration, after checking
771+
// that none of the blocks that are being removed are in use.
772+
// Note that littlefs's pseudorandom block allocation means that
773+
// this is very unlikely to work in the general case.
770774
//
771775
// Returns a negative error code on failure.
772776
int lfs2_fs_grow(lfs2_t *lfs2, lfs2_size_t block_count);

lib/littlefs/lfs2_util.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,10 @@ static inline uint32_t lfs2_fromle32(uint32_t a) {
195195
(defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__))
196196
return __builtin_bswap32(a);
197197
#else
198-
return (((uint8_t*)&a)[0] << 0) |
199-
(((uint8_t*)&a)[1] << 8) |
200-
(((uint8_t*)&a)[2] << 16) |
201-
(((uint8_t*)&a)[3] << 24);
198+
return ((uint32_t)((uint8_t*)&a)[0] << 0) |
199+
((uint32_t)((uint8_t*)&a)[1] << 8) |
200+
((uint32_t)((uint8_t*)&a)[2] << 16) |
201+
((uint32_t)((uint8_t*)&a)[3] << 24);
202202
#endif
203203
}
204204

@@ -218,10 +218,10 @@ static inline uint32_t lfs2_frombe32(uint32_t a) {
218218
(defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
219219
return a;
220220
#else
221-
return (((uint8_t*)&a)[0] << 24) |
222-
(((uint8_t*)&a)[1] << 16) |
223-
(((uint8_t*)&a)[2] << 8) |
224-
(((uint8_t*)&a)[3] << 0);
221+
return ((uint32_t)((uint8_t*)&a)[0] << 24) |
222+
((uint32_t)((uint8_t*)&a)[1] << 16) |
223+
((uint32_t)((uint8_t*)&a)[2] << 8) |
224+
((uint32_t)((uint8_t*)&a)[3] << 0);
225225
#endif
226226
}
227227

0 commit comments

Comments
 (0)