Skip to content

Commit 1941bbd

Browse files
committed
Cleaned up config options
- Updated documentation where needed - Added asserts which take into account relationships with the new cache_size configuration - Restructured ordering to be consistent for the three main configurables: LFS_ATTR_MAX, LFS_NAME_MAX, and LFS_INLINE_MAX
1 parent 3cfa086 commit 1941bbd

File tree

2 files changed

+54
-56
lines changed

2 files changed

+54
-56
lines changed

lfs.c

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2703,6 +2703,16 @@ static inline void lfs_superblocktole32(lfs_superblock_t *superblock) {
27032703
static int lfs_init(lfs_t *lfs, const struct lfs_config *cfg) {
27042704
lfs->cfg = cfg;
27052705

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+
27062716
// setup read cache
27072717
lfs->rcache.block = 0xffffffff;
27082718
if (lfs->cfg->read_buffer) {
@@ -2725,7 +2735,7 @@ static int lfs_init(lfs_t *lfs, const struct lfs_config *cfg) {
27252735
}
27262736
}
27272737

2728-
// setup lookahead, round down to nearest 32-bits
2738+
// setup lookahead, must be multiple of 32-bits
27292739
LFS_ASSERT(lfs->cfg->lookahead % 32 == 0);
27302740
LFS_ASSERT(lfs->cfg->lookahead > 0);
27312741
if (lfs->cfg->lookahead_buffer) {
@@ -2737,22 +2747,12 @@ static int lfs_init(lfs_t *lfs, const struct lfs_config *cfg) {
27372747
}
27382748
}
27392749

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-
27502750
// check that the size limits are sane
27512751
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);
27532753
lfs->inline_size = lfs->cfg->inline_size;
27542754
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);
27562756
}
27572757

27582758
LFS_ASSERT(lfs->cfg->attr_size <= LFS_ATTR_MAX);
@@ -2841,9 +2841,9 @@ int lfs_format(lfs_t *lfs, const struct lfs_config *cfg) {
28412841

28422842
.block_size = lfs->cfg->block_size,
28432843
.block_count = lfs->cfg->block_count,
2844-
.inline_size = lfs->inline_size,
28452844
.attr_size = lfs->attr_size,
28462845
.name_size = lfs->name_size,
2846+
.inline_size = lfs->inline_size,
28472847
};
28482848

28492849
lfs_superblocktole32(&superblock);
@@ -2883,9 +2883,6 @@ int lfs_mount(lfs_t *lfs, const struct lfs_config *cfg) {
28832883
lfs_mdir_t superdir;
28842884
err = lfs_dir_fetch(lfs, &superdir, (const lfs_block_t[2]){0, 1});
28852885
if (err) {
2886-
if (err == LFS_ERR_CORRUPT) {
2887-
LFS_ERROR("Invalid superblock at %d %d", 0, 1);
2888-
}
28892886
return err;
28902887
}
28912888

@@ -2899,8 +2896,8 @@ int lfs_mount(lfs_t *lfs, const struct lfs_config *cfg) {
28992896
lfs_superblockfromle32(&superblock);
29002897

29012898
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;
29042901
}
29052902

29062903
uint16_t major_version = (0xffff & (superblock.version >> 16));
@@ -2919,16 +2916,7 @@ int lfs_mount(lfs_t *lfs, const struct lfs_config *cfg) {
29192916
}
29202917
lfs_pairfromle32(lfs->root);
29212918

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
29322920
if (superblock.attr_size) {
29332921
if (superblock.attr_size > lfs->attr_size) {
29342922
LFS_ERROR("Unsupported attr size (%d > %d)",
@@ -2949,6 +2937,16 @@ int lfs_mount(lfs_t *lfs, const struct lfs_config *cfg) {
29492937
lfs->name_size = superblock.name_size;
29502938
}
29512939

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+
29522950
// scan for any global updates
29532951
lfs_mdir_t dir = {.tail = {0, 1}};
29542952
while (!lfs_pairisnull(dir.tail)) {

lfs.h

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -50,31 +50,29 @@ typedef int32_t lfs_soff_t;
5050

5151
typedef uint32_t lfs_block_t;
5252

53-
// Maximum inline file size in bytes. Large inline files require a larger
54-
// read and prog cache, but if a file can be inline it does not need its own
55-
// data block. LFS_ATTR_MAX + LFS_INLINE_MAX must be <= 0xffff. Stored in
56-
// superblock and must be respected by other littlefs drivers.
57-
// TODO doc
58-
#ifndef LFS_INLINE_MAX
59-
#define LFS_INLINE_MAX 0xfff
60-
#endif
61-
6253
// Maximum size of all attributes per file in bytes, may be redefined but a
63-
// a smaller LFS_ATTR_MAX has no benefit. LFS_ATTR_MAX + LFS_INLINE_MAX
64-
// must be <= 0xffff. Stored in superblock and must be respected by other
54+
// a smaller LFS_ATTR_MAX has no benefit. Stored in 12-bits and limited
55+
// to <= 0xfff. Stored in superblock and must be respected by other
6556
// littlefs drivers.
66-
// TODO doc
6757
#ifndef LFS_ATTR_MAX
6858
#define LFS_ATTR_MAX 0xfff
6959
#endif
7060

71-
// Max name size in bytes, may be redefined to reduce the size of the
72-
// info struct. Stored in superblock and must be respected by other
73-
// littlefs drivers.
61+
// Maximum name size in bytes, may be redefined to reduce the size of the
62+
// info struct. Limited to <= LFS_ATTR_MAX. Stored in superblock and must
63+
// be respected by other littlefs drivers.
7464
#ifndef LFS_NAME_MAX
7565
#define LFS_NAME_MAX 0xff
7666
#endif
7767

68+
// Maximum inline file size in bytes. Large inline files require a larger
69+
// cache size, but if a file can be inline it does not need its own data
70+
// block. Limited to <= LFS_ATTR_MAX and <= cache_size. Stored in superblock
71+
// and must be respected by other littlefs drivers.
72+
#ifndef LFS_INLINE_MAX
73+
#define LFS_INLINE_MAX 0xfff
74+
#endif
75+
7876
// Possible error codes, these are negative to allow
7977
// valid positive return values
8078
enum lfs_error {
@@ -110,7 +108,7 @@ enum lfs_type {
110108
LFS_TYPE_TAIL = 0x0c0,
111109
LFS_TYPE_SOFTTAIL = 0x0c0,
112110
LFS_TYPE_HARDTAIL = 0x0c1,
113-
LFS_TYPE_CRC = 0x0f0, // TODO are trailing ones useful?
111+
LFS_TYPE_CRC = 0x0f0,
114112

115113
LFS_TYPE_INLINESTRUCT = 0x040,
116114
LFS_TYPE_CTZSTRUCT = 0x041,
@@ -216,15 +214,8 @@ struct lfs_config {
216214
// lookahead block.
217215
void *lookahead_buffer;
218216

219-
// Optional upper limit on inlined files in bytes. Large inline files
220-
// require a larger read and prog cache, but if a file can be inlined it
221-
// does not need its own data block. Must be smaller than the read size
222-
// and prog size. Defaults to min(LFS_INLINE_MAX, read_size) when zero.
223-
// Stored in superblock and must be respected by other littlefs drivers.
224-
lfs_size_t inline_size;
225-
226-
// Optional upper limit on attributes per file in bytes. No downside for
227-
// larger attributes size but must be less than LFS_ATTR_MAX. Defaults to
217+
// Optional upper limit on file attributes in bytes. No downside for larger
218+
// attributes size but must be less than LFS_ATTR_MAX. Defaults to
228219
// LFS_ATTR_MAX when zero.Stored in superblock and must be respected by
229220
// other littlefs drivers.
230221
lfs_size_t attr_size;
@@ -234,6 +225,13 @@ struct lfs_config {
234225
// the LFS_NAME_MAX define. Defaults to LFS_NAME_MAX when zero. Stored in
235226
// superblock and must be respected by other littlefs drivers.
236227
lfs_size_t name_size;
228+
229+
// Optional upper limit on inlined files in bytes. Large inline files
230+
// require a larger cache size, but if a file can be inlined it does not
231+
// need its own data block. Must be smaller than cache_size and less than
232+
// LFS_INLINE_MAX. Defaults to min(LFS_INLINE_MAX, read_size) when zero.
233+
// Stored in superblock and must be respected by other littlefs drivers.
234+
lfs_size_t inline_size;
237235
};
238236

239237
// File info structure
@@ -355,9 +353,9 @@ typedef struct lfs_superblock {
355353
lfs_size_t block_size;
356354
lfs_size_t block_count;
357355

358-
lfs_size_t inline_size;
359356
lfs_size_t attr_size;
360357
lfs_size_t name_size;
358+
lfs_size_t inline_size;
361359
} lfs_superblock_t;
362360

363361
typedef struct lfs_free {
@@ -381,9 +379,11 @@ typedef struct lfs {
381379
lfs_free_t free;
382380

383381
const struct lfs_config *cfg;
384-
lfs_size_t inline_size;
382+
lfs_size_t block_size;
383+
lfs_size_t block_count;
385384
lfs_size_t attr_size;
386385
lfs_size_t name_size;
386+
lfs_size_t inline_size;
387387
} lfs_t;
388388

389389

0 commit comments

Comments
 (0)