mirror of
https://github.com/eledio-devices/thirdparty-littlefs.git
synced 2025-11-01 08:48:31 +01:00
Compare commits
8 Commits
v2.2.0
...
fix-alloc-
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0aba71d0d6 | ||
|
|
0ea2871e24 | ||
|
|
f215027fd4 | ||
|
|
1ae4b36f2a | ||
|
|
480cdd9f81 | ||
|
|
4c9146ea53 | ||
|
|
5a9f38df01 | ||
|
|
1b033e9ab6 |
2
Makefile
2
Makefile
@@ -26,8 +26,6 @@ endif
|
|||||||
override CFLAGS += -I.
|
override CFLAGS += -I.
|
||||||
override CFLAGS += -std=c99 -Wall -pedantic
|
override CFLAGS += -std=c99 -Wall -pedantic
|
||||||
override CFLAGS += -Wextra -Wshadow -Wjump-misses-init -Wundef
|
override CFLAGS += -Wextra -Wshadow -Wjump-misses-init -Wundef
|
||||||
# Remove missing-field-initializers because of GCC bug
|
|
||||||
override CFLAGS += -Wno-missing-field-initializers
|
|
||||||
|
|
||||||
ifdef VERBOSE
|
ifdef VERBOSE
|
||||||
override TFLAGS += -v
|
override TFLAGS += -v
|
||||||
|
|||||||
61
lfs.c
61
lfs.c
@@ -452,14 +452,16 @@ static int lfs_alloc_lookahead(void *p, lfs_block_t block) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// indicate allocated blocks have been committed into the filesystem, this
|
||||||
|
// is to prevent blocks from being garbage collected in the middle of a
|
||||||
|
// commit operation
|
||||||
static void lfs_alloc_ack(lfs_t *lfs) {
|
static void lfs_alloc_ack(lfs_t *lfs) {
|
||||||
lfs->free.ack = lfs->cfg->block_count;
|
lfs->free.ack = lfs->cfg->block_count;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Invalidate the lookahead buffer. This is done during mounting and
|
// drop the lookahead buffer, this is done during mounting and failed
|
||||||
// failed traversals
|
// traversals in order to avoid invalid lookahead state
|
||||||
static void lfs_alloc_reset(lfs_t *lfs) {
|
static void lfs_alloc_drop(lfs_t *lfs) {
|
||||||
lfs->free.off = lfs->seed % lfs->cfg->block_size;
|
|
||||||
lfs->free.size = 0;
|
lfs->free.size = 0;
|
||||||
lfs->free.i = 0;
|
lfs->free.i = 0;
|
||||||
lfs_alloc_ack(lfs);
|
lfs_alloc_ack(lfs);
|
||||||
@@ -505,7 +507,7 @@ static int lfs_alloc(lfs_t *lfs, lfs_block_t *block) {
|
|||||||
memset(lfs->free.buffer, 0, lfs->cfg->lookahead_size);
|
memset(lfs->free.buffer, 0, lfs->cfg->lookahead_size);
|
||||||
int err = lfs_fs_traverseraw(lfs, lfs_alloc_lookahead, lfs, true);
|
int err = lfs_fs_traverseraw(lfs, lfs_alloc_lookahead, lfs, true);
|
||||||
if (err) {
|
if (err) {
|
||||||
lfs_alloc_reset(lfs);
|
lfs_alloc_drop(lfs);
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -870,8 +872,10 @@ static lfs_stag_t lfs_dir_fetchmatch(lfs_t *lfs,
|
|||||||
ptag ^= (lfs_tag_t)(lfs_tag_chunk(tag) & 1U) << 31;
|
ptag ^= (lfs_tag_t)(lfs_tag_chunk(tag) & 1U) << 31;
|
||||||
|
|
||||||
// toss our crc into the filesystem seed for
|
// toss our crc into the filesystem seed for
|
||||||
// pseudorandom numbers
|
// pseudorandom numbers, note we use another crc here
|
||||||
lfs->seed ^= crc;
|
// as a collection function because it is sufficiently
|
||||||
|
// random and convenient
|
||||||
|
lfs->seed = lfs_crc(lfs->seed, &crc, sizeof(crc));
|
||||||
|
|
||||||
// update with what's found so far
|
// update with what's found so far
|
||||||
besttag = tempbesttag;
|
besttag = tempbesttag;
|
||||||
@@ -1261,12 +1265,13 @@ static int lfs_dir_commitattr(lfs_t *lfs, struct lfs_commit *commit,
|
|||||||
}
|
}
|
||||||
|
|
||||||
static int lfs_dir_commitcrc(lfs_t *lfs, struct lfs_commit *commit) {
|
static int lfs_dir_commitcrc(lfs_t *lfs, struct lfs_commit *commit) {
|
||||||
const lfs_off_t off1 = commit->off;
|
|
||||||
const uint32_t crc1 = commit->crc;
|
|
||||||
// align to program units
|
// align to program units
|
||||||
const lfs_off_t end = lfs_alignup(off1 + 2*sizeof(uint32_t),
|
const lfs_off_t end = lfs_alignup(commit->off + 2*sizeof(uint32_t),
|
||||||
lfs->cfg->prog_size);
|
lfs->cfg->prog_size);
|
||||||
|
|
||||||
|
lfs_off_t off1 = 0;
|
||||||
|
uint32_t crc1 = 0;
|
||||||
|
|
||||||
// create crc tags to fill up remainder of commit, note that
|
// create crc tags to fill up remainder of commit, note that
|
||||||
// padding is not crced, which lets fetches skip padding but
|
// padding is not crced, which lets fetches skip padding but
|
||||||
// makes committing a bit more complicated
|
// makes committing a bit more complicated
|
||||||
@@ -1302,6 +1307,12 @@ static int lfs_dir_commitcrc(lfs_t *lfs, struct lfs_commit *commit) {
|
|||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// keep track of non-padding checksum to verify
|
||||||
|
if (off1 == 0) {
|
||||||
|
off1 = commit->off + sizeof(uint32_t);
|
||||||
|
crc1 = commit->crc;
|
||||||
|
}
|
||||||
|
|
||||||
commit->off += sizeof(tag)+lfs_tag_size(tag);
|
commit->off += sizeof(tag)+lfs_tag_size(tag);
|
||||||
commit->ptag = tag ^ ((lfs_tag_t)reset << 31);
|
commit->ptag = tag ^ ((lfs_tag_t)reset << 31);
|
||||||
commit->crc = 0xffffffff; // reset crc for next "commit"
|
commit->crc = 0xffffffff; // reset crc for next "commit"
|
||||||
@@ -1315,7 +1326,7 @@ 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
|
||||||
lfs_off_t off = commit->begin;
|
lfs_off_t off = commit->begin;
|
||||||
lfs_off_t noff = off1 + sizeof(uint32_t);
|
lfs_off_t noff = off1;
|
||||||
while (off < end) {
|
while (off < end) {
|
||||||
uint32_t crc = 0xffffffff;
|
uint32_t crc = 0xffffffff;
|
||||||
for (lfs_off_t i = off; i < noff+sizeof(uint32_t); i++) {
|
for (lfs_off_t i = off; i < noff+sizeof(uint32_t); i++) {
|
||||||
@@ -2432,9 +2443,9 @@ int lfs_file_opencfg(lfs_t *lfs, lfs_file_t *file,
|
|||||||
|
|
||||||
// get next slot and create entry to remember name
|
// get next slot and create entry to remember name
|
||||||
err = lfs_dir_commit(lfs, &file->m, LFS_MKATTRS(
|
err = lfs_dir_commit(lfs, &file->m, LFS_MKATTRS(
|
||||||
{LFS_MKTAG(LFS_TYPE_CREATE, file->id, 0)},
|
{LFS_MKTAG(LFS_TYPE_CREATE, file->id, 0), NULL},
|
||||||
{LFS_MKTAG(LFS_TYPE_REG, file->id, nlen), path},
|
{LFS_MKTAG(LFS_TYPE_REG, file->id, nlen), path},
|
||||||
{LFS_MKTAG(LFS_TYPE_INLINESTRUCT, file->id, 0)}));
|
{LFS_MKTAG(LFS_TYPE_INLINESTRUCT, file->id, 0), NULL}));
|
||||||
if (err) {
|
if (err) {
|
||||||
err = LFS_ERR_NAMETOOLONG;
|
err = LFS_ERR_NAMETOOLONG;
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
@@ -3200,7 +3211,7 @@ int lfs_remove(lfs_t *lfs, const char *path) {
|
|||||||
|
|
||||||
// delete the entry
|
// delete the entry
|
||||||
err = lfs_dir_commit(lfs, &cwd, LFS_MKATTRS(
|
err = lfs_dir_commit(lfs, &cwd, LFS_MKATTRS(
|
||||||
{LFS_MKTAG(LFS_TYPE_DELETE, lfs_tag_id(tag), 0)}));
|
{LFS_MKTAG(LFS_TYPE_DELETE, lfs_tag_id(tag), 0), NULL}));
|
||||||
if (err) {
|
if (err) {
|
||||||
lfs->mlist = dir.next;
|
lfs->mlist = dir.next;
|
||||||
LFS_TRACE("lfs_remove -> %d", err);
|
LFS_TRACE("lfs_remove -> %d", err);
|
||||||
@@ -3326,12 +3337,12 @@ int lfs_rename(lfs_t *lfs, const char *oldpath, const char *newpath) {
|
|||||||
// move over all attributes
|
// move over all attributes
|
||||||
err = lfs_dir_commit(lfs, &newcwd, LFS_MKATTRS(
|
err = lfs_dir_commit(lfs, &newcwd, LFS_MKATTRS(
|
||||||
{LFS_MKTAG_IF(prevtag != LFS_ERR_NOENT,
|
{LFS_MKTAG_IF(prevtag != LFS_ERR_NOENT,
|
||||||
LFS_TYPE_DELETE, newid, 0)},
|
LFS_TYPE_DELETE, newid, 0), NULL},
|
||||||
{LFS_MKTAG(LFS_TYPE_CREATE, newid, 0)},
|
{LFS_MKTAG(LFS_TYPE_CREATE, newid, 0), NULL},
|
||||||
{LFS_MKTAG(lfs_tag_type3(oldtag), newid, strlen(newpath)), newpath},
|
{LFS_MKTAG(lfs_tag_type3(oldtag), newid, strlen(newpath)), newpath},
|
||||||
{LFS_MKTAG(LFS_FROM_MOVE, newid, lfs_tag_id(oldtag)), &oldcwd},
|
{LFS_MKTAG(LFS_FROM_MOVE, newid, lfs_tag_id(oldtag)), &oldcwd},
|
||||||
{LFS_MKTAG_IF(samepair,
|
{LFS_MKTAG_IF(samepair,
|
||||||
LFS_TYPE_DELETE, newoldid, 0)}));
|
LFS_TYPE_DELETE, newoldid, 0), NULL}));
|
||||||
if (err) {
|
if (err) {
|
||||||
lfs->mlist = prevdir.next;
|
lfs->mlist = prevdir.next;
|
||||||
LFS_TRACE("lfs_rename -> %d", err);
|
LFS_TRACE("lfs_rename -> %d", err);
|
||||||
@@ -3344,7 +3355,7 @@ int lfs_rename(lfs_t *lfs, const char *oldpath, const char *newpath) {
|
|||||||
// prep gstate and delete move id
|
// prep gstate and delete move id
|
||||||
lfs_fs_prepmove(lfs, 0x3ff, NULL);
|
lfs_fs_prepmove(lfs, 0x3ff, NULL);
|
||||||
err = lfs_dir_commit(lfs, &oldcwd, LFS_MKATTRS(
|
err = lfs_dir_commit(lfs, &oldcwd, LFS_MKATTRS(
|
||||||
{LFS_MKTAG(LFS_TYPE_DELETE, lfs_tag_id(oldtag), 0)}));
|
{LFS_MKTAG(LFS_TYPE_DELETE, lfs_tag_id(oldtag), 0), NULL}));
|
||||||
if (err) {
|
if (err) {
|
||||||
lfs->mlist = prevdir.next;
|
lfs->mlist = prevdir.next;
|
||||||
LFS_TRACE("lfs_rename -> %d", err);
|
LFS_TRACE("lfs_rename -> %d", err);
|
||||||
@@ -3636,7 +3647,7 @@ int lfs_format(lfs_t *lfs, const struct lfs_config *cfg) {
|
|||||||
|
|
||||||
lfs_superblock_tole32(&superblock);
|
lfs_superblock_tole32(&superblock);
|
||||||
err = lfs_dir_commit(lfs, &root, LFS_MKATTRS(
|
err = lfs_dir_commit(lfs, &root, LFS_MKATTRS(
|
||||||
{LFS_MKTAG(LFS_TYPE_CREATE, 0, 0)},
|
{LFS_MKTAG(LFS_TYPE_CREATE, 0, 0), NULL},
|
||||||
{LFS_MKTAG(LFS_TYPE_SUPERBLOCK, 0, 8), "littlefs"},
|
{LFS_MKTAG(LFS_TYPE_SUPERBLOCK, 0, 8), "littlefs"},
|
||||||
{LFS_MKTAG(LFS_TYPE_INLINESTRUCT, 0, sizeof(superblock)),
|
{LFS_MKTAG(LFS_TYPE_INLINESTRUCT, 0, sizeof(superblock)),
|
||||||
&superblock}));
|
&superblock}));
|
||||||
@@ -3797,8 +3808,10 @@ int lfs_mount(lfs_t *lfs, const struct lfs_config *cfg) {
|
|||||||
lfs->gstate.tag += !lfs_tag_isvalid(lfs->gstate.tag);
|
lfs->gstate.tag += !lfs_tag_isvalid(lfs->gstate.tag);
|
||||||
lfs->gdisk = lfs->gstate;
|
lfs->gdisk = lfs->gstate;
|
||||||
|
|
||||||
// setup free lookahead
|
// setup free lookahead, to distribute allocations uniformly across
|
||||||
lfs_alloc_reset(lfs);
|
// boots, we start the allocator at a random location
|
||||||
|
lfs->free.off = lfs->seed % lfs->cfg->block_count;
|
||||||
|
lfs_alloc_drop(lfs);
|
||||||
|
|
||||||
LFS_TRACE("lfs_mount -> %d", 0);
|
LFS_TRACE("lfs_mount -> %d", 0);
|
||||||
return 0;
|
return 0;
|
||||||
@@ -4050,7 +4063,7 @@ static int lfs_fs_relocate(lfs_t *lfs,
|
|||||||
lfs_pair_tole32(newpair);
|
lfs_pair_tole32(newpair);
|
||||||
int err = lfs_dir_commit(lfs, &parent, LFS_MKATTRS(
|
int err = lfs_dir_commit(lfs, &parent, LFS_MKATTRS(
|
||||||
{LFS_MKTAG_IF(moveid != 0x3ff,
|
{LFS_MKTAG_IF(moveid != 0x3ff,
|
||||||
LFS_TYPE_DELETE, moveid, 0)},
|
LFS_TYPE_DELETE, moveid, 0), NULL},
|
||||||
{tag, newpair}));
|
{tag, newpair}));
|
||||||
lfs_pair_fromle32(newpair);
|
lfs_pair_fromle32(newpair);
|
||||||
if (err) {
|
if (err) {
|
||||||
@@ -4084,7 +4097,7 @@ static int lfs_fs_relocate(lfs_t *lfs,
|
|||||||
lfs_pair_tole32(newpair);
|
lfs_pair_tole32(newpair);
|
||||||
err = lfs_dir_commit(lfs, &parent, LFS_MKATTRS(
|
err = lfs_dir_commit(lfs, &parent, LFS_MKATTRS(
|
||||||
{LFS_MKTAG_IF(moveid != 0x3ff,
|
{LFS_MKTAG_IF(moveid != 0x3ff,
|
||||||
LFS_TYPE_DELETE, moveid, 0)},
|
LFS_TYPE_DELETE, moveid, 0), NULL},
|
||||||
{LFS_MKTAG(LFS_TYPE_TAIL + parent.split, 0x3ff, 8), newpair}));
|
{LFS_MKTAG(LFS_TYPE_TAIL + parent.split, 0x3ff, 8), newpair}));
|
||||||
lfs_pair_fromle32(newpair);
|
lfs_pair_fromle32(newpair);
|
||||||
if (err) {
|
if (err) {
|
||||||
@@ -4132,7 +4145,7 @@ static int lfs_fs_demove(lfs_t *lfs) {
|
|||||||
uint16_t moveid = lfs_tag_id(lfs->gdisk.tag);
|
uint16_t moveid = lfs_tag_id(lfs->gdisk.tag);
|
||||||
lfs_fs_prepmove(lfs, 0x3ff, NULL);
|
lfs_fs_prepmove(lfs, 0x3ff, NULL);
|
||||||
err = lfs_dir_commit(lfs, &movedir, LFS_MKATTRS(
|
err = lfs_dir_commit(lfs, &movedir, LFS_MKATTRS(
|
||||||
{LFS_MKTAG(LFS_TYPE_DELETE, moveid, 0)}));
|
{LFS_MKTAG(LFS_TYPE_DELETE, moveid, 0), NULL}));
|
||||||
if (err) {
|
if (err) {
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -106,7 +106,7 @@ def main(args):
|
|||||||
struct.unpack('<HH', superblock[1].data[0:4].ljust(4, b'\xff'))))
|
struct.unpack('<HH', superblock[1].data[0:4].ljust(4, b'\xff'))))
|
||||||
print("%-47s%s" % ("littlefs v%s.%s" % version,
|
print("%-47s%s" % ("littlefs v%s.%s" % version,
|
||||||
"data (truncated, if it fits)"
|
"data (truncated, if it fits)"
|
||||||
if not any([args.no_truncate, args.tags, args.log, args.all]) else ""))
|
if not any([args.no_truncate, args.log, args.all]) else ""))
|
||||||
|
|
||||||
# print gstate
|
# print gstate
|
||||||
print("gstate 0x%s" % ''.join('%02x' % c for c in gstate))
|
print("gstate 0x%s" % ''.join('%02x' % c for c in gstate))
|
||||||
|
|||||||
Reference in New Issue
Block a user