Skip to content

Commit c2fab8f

Browse files
committed
Added asserts on geometry and updated config documentation
littlefs had an unwritten assumption that the block device's program size would be a multiple of the read size, and the block size would be a multiple of the program size. This has already caused confusion for users. Added a note and assert to catch unexpected geometries early. Also found that the prog/erase functions indicated they must return LFS_ERR_CORRUPT to catch bad blocks. This is no longer true as errors are found by CRC.
1 parent 472ccc4 commit c2fab8f

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

lfs.c

+4
Original file line numberDiff line numberDiff line change
@@ -1926,6 +1926,10 @@ static int lfs_init(lfs_t *lfs, const struct lfs_config *cfg) {
19261926
}
19271927
}
19281928

1929+
// check that program and read sizes are multiples of the block size
1930+
assert(lfs->cfg->prog_size % lfs->cfg->read_size == 0);
1931+
assert(lfs->cfg->block_size % lfs->cfg->prog_size == 0);
1932+
19291933
// check that the block size is large enough to fit ctz pointers
19301934
assert(4*lfs_npw2(0xffffffff / (lfs->cfg->block_size-2*4))
19311935
<= lfs->cfg->block_size);

lfs.h

+5-3
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,14 @@ struct lfs_config {
9999

100100
// Program a region in a block. The block must have previously
101101
// been erased. Negative error codes are propogated to the user.
102-
// The prog function must return LFS_ERR_CORRUPT if the block should
103-
// be considered bad.
102+
// May return LFS_ERR_CORRUPT if the block should be considered bad.
104103
int (*prog)(const struct lfs_config *c, lfs_block_t block,
105104
lfs_off_t off, const void *buffer, lfs_size_t size);
106105

107106
// Erase a block. A block must be erased before being programmed.
108107
// The state of an erased block is undefined. Negative error codes
109108
// are propogated to the user.
109+
// May return LFS_ERR_CORRUPT if the block should be considered bad.
110110
int (*erase)(const struct lfs_config *c, lfs_block_t block);
111111

112112
// Sync the state of the underlying block device. Negative error codes
@@ -121,11 +121,13 @@ struct lfs_config {
121121
// Minimum size of a block program. This determines the size of program
122122
// buffers. This may be larger than the physical program size to improve
123123
// performance by caching more of the block device.
124+
// Must be a multiple of the read size.
124125
lfs_size_t prog_size;
125126

126127
// Size of an erasable block. This does not impact ram consumption and
127128
// may be larger than the physical erase size. However, this should be
128-
// kept small as each file currently takes up an entire block .
129+
// kept small as each file currently takes up an entire block.
130+
// Must be a multiple of the program size.
129131
lfs_size_t block_size;
130132

131133
// Number of erasable blocks on the device.

0 commit comments

Comments
 (0)