From 3778759979a1c6bc141e9ced8c28df2a1d4a9848 Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Fri, 10 Nov 2017 11:02:50 -0600 Subject: [PATCH] Squashed 'littlefs/' changes from 2ab150c..3f31c8c 3f31c8c Fixed corner case with immediate exhaustion and lookahead==block_count f4aeb83 Fixed issue with aggressively rounding down lookahead configuration db51a39 Removed stray newline in LFS_ERROR for version git-subtree-dir: littlefs git-subtree-split: 3f31c8cba31e0f6cef5b02dba2e050d8df1168b7 --- .travis.yml | 2 +- lfs.c | 28 ++++++++++++++-------------- lfs.h | 1 - 3 files changed, 15 insertions(+), 16 deletions(-) diff --git a/.travis.yml b/.travis.yml index f2322761b0..552f7cc9a7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -15,7 +15,7 @@ script: - CFLAGS="-DLFS_READ_SIZE=1 -DLFS_PROG_SIZE=1" make test - CFLAGS="-DLFS_READ_SIZE=512 -DLFS_PROG_SIZE=512" make test - CFLAGS="-DLFS_BLOCK_COUNT=1023" make test - - CFLAGS="-DLFS_LOOKAHEAD=2047" make test + - CFLAGS="-DLFS_LOOKAHEAD=2048" make test # self-host with littlefs-fuse for fuzz test - make -C littlefs-fuse diff --git a/lfs.c b/lfs.c index f602197778..b043bd907d 100644 --- a/lfs.c +++ b/lfs.c @@ -278,7 +278,7 @@ static int lfs_alloc_lookahead(void *p, lfs_block_t block) { % (lfs_soff_t)(lfs->cfg->block_count)) + lfs->cfg->block_count) % lfs->cfg->block_count; - if (off < lfs->free.lookahead) { + if (off < lfs->cfg->lookahead) { lfs->free.buffer[off / 32] |= 1U << (off % 32); } @@ -294,7 +294,8 @@ static int lfs_alloc(lfs_t *lfs, lfs_block_t *block) { return LFS_ERR_NOSPC; } - if (lfs->free.off >= lfs->free.lookahead) { + if (lfs->free.off >= lfs_min( + lfs->cfg->lookahead, lfs->cfg->block_count)) { break; } @@ -308,11 +309,11 @@ static int lfs_alloc(lfs_t *lfs, lfs_block_t *block) { } } - lfs->free.begin += lfs->free.lookahead; + lfs->free.begin += lfs_min(lfs->cfg->lookahead, lfs->cfg->block_count); lfs->free.off = 0; // find mask of free blocks from tree - memset(lfs->free.buffer, 0, lfs->free.lookahead/8); + memset(lfs->free.buffer, 0, lfs->cfg->lookahead/8); int err = lfs_traverse(lfs, lfs_alloc_lookahead, lfs); if (err) { return err; @@ -1870,13 +1871,12 @@ static int lfs_init(lfs_t *lfs, const struct lfs_config *cfg) { } // setup lookahead, round down to nearest 32-bits - lfs->free.lookahead = lfs_min(lfs->cfg->lookahead, lfs->cfg->block_count); - lfs->free.lookahead = 32 * (lfs->free.lookahead / 32); - assert(lfs->free.lookahead > 0); + assert(lfs->cfg->lookahead % 32 == 0); + assert(lfs->cfg->lookahead > 0); if (lfs->cfg->lookahead_buffer) { lfs->free.buffer = lfs->cfg->lookahead_buffer; } else { - lfs->free.buffer = malloc(lfs->free.lookahead/8); + lfs->free.buffer = malloc(lfs->cfg->lookahead/8); if (!lfs->free.buffer) { return LFS_ERR_NOMEM; } @@ -1919,10 +1919,10 @@ int lfs_format(lfs_t *lfs, const struct lfs_config *cfg) { } // create free lookahead - memset(lfs->free.buffer, 0, lfs->free.lookahead/8); + memset(lfs->free.buffer, 0, lfs->cfg->lookahead/8); lfs->free.begin = 0; lfs->free.off = 0; - lfs->free.end = lfs->free.begin + lfs->cfg->block_count; + lfs->free.end = lfs->free.begin + lfs->free.off + lfs->cfg->block_count; // create superblock dir lfs_alloc_ack(lfs); @@ -1998,9 +1998,9 @@ int lfs_mount(lfs_t *lfs, const struct lfs_config *cfg) { } // setup free lookahead - lfs->free.begin = -lfs->free.lookahead; - lfs->free.off = lfs->free.lookahead; - lfs->free.end = lfs->free.begin + lfs->cfg->block_count; + lfs->free.begin = -lfs->cfg->lookahead; + lfs->free.off = lfs->cfg->lookahead; + lfs->free.end = lfs->free.begin + lfs->free.off + lfs->cfg->block_count; // load superblock lfs_dir_t dir; @@ -2027,7 +2027,7 @@ int lfs_mount(lfs_t *lfs, const struct lfs_config *cfg) { } if (superblock.d.version > (0x00010001 | 0x0000ffff)) { - LFS_ERROR("Invalid version %d.%d\n", + LFS_ERROR("Invalid version %d.%d", 0xffff & (superblock.d.version >> 16), 0xffff & (superblock.d.version >> 0)); return LFS_ERR_INVAL; diff --git a/lfs.h b/lfs.h index 270a65efb9..e4aab0e4b2 100644 --- a/lfs.h +++ b/lfs.h @@ -236,7 +236,6 @@ typedef struct lfs_superblock { } lfs_superblock_t; typedef struct lfs_free { - lfs_size_t lookahead; lfs_block_t begin; lfs_block_t end; lfs_block_t off;