Skip to content

Commit ad96fca

Browse files
committed
Changed attr_max to be specific to custom attributes
While technically, both system and user attributes share the same disk limitations, that's not what attr_max represents when considered from the user's perspective. To the user, attr_max applies only to custom attributes. This means attr_max should not impact other configurable limitations, such as inline files, and the ordering should be reconsidered with what the user finds most important.
1 parent f010d2a commit ad96fca

File tree

2 files changed

+53
-49
lines changed

2 files changed

+53
-49
lines changed

lfs.c

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -388,18 +388,18 @@ static inline void lfs_superblock_fromle32(lfs_superblock_t *superblock) {
388388
superblock->version = lfs_fromle32(superblock->version);
389389
superblock->block_size = lfs_fromle32(superblock->block_size);
390390
superblock->block_count = lfs_fromle32(superblock->block_count);
391+
superblock->name_max = lfs_fromle32(superblock->name_max);
391392
superblock->inline_max = lfs_fromle32(superblock->inline_max);
392393
superblock->attr_max = lfs_fromle32(superblock->attr_max);
393-
superblock->name_max = lfs_fromle32(superblock->name_max);
394394
}
395395

396396
static inline void lfs_superblock_tole32(lfs_superblock_t *superblock) {
397397
superblock->version = lfs_tole32(superblock->version);
398398
superblock->block_size = lfs_tole32(superblock->block_size);
399399
superblock->block_count = lfs_tole32(superblock->block_count);
400+
superblock->name_max = lfs_tole32(superblock->name_max);
400401
superblock->inline_max = lfs_tole32(superblock->inline_max);
401402
superblock->attr_max = lfs_tole32(superblock->attr_max);
402-
superblock->name_max = lfs_tole32(superblock->name_max);
403403
}
404404

405405

@@ -3054,6 +3054,12 @@ static int lfs_init(lfs_t *lfs, const struct lfs_config *cfg) {
30543054
}
30553055

30563056
// check that the size limits are sane
3057+
LFS_ASSERT(lfs->cfg->name_max <= LFS_NAME_MAX);
3058+
lfs->name_max = lfs->cfg->name_max;
3059+
if (!lfs->name_max) {
3060+
lfs->name_max = LFS_NAME_MAX;
3061+
}
3062+
30573063
LFS_ASSERT(lfs->cfg->inline_max <= LFS_INLINE_MAX);
30583064
LFS_ASSERT(lfs->cfg->inline_max <= lfs->cfg->cache_size);
30593065
lfs->inline_max = lfs->cfg->inline_max;
@@ -3067,12 +3073,6 @@ static int lfs_init(lfs_t *lfs, const struct lfs_config *cfg) {
30673073
lfs->attr_max = LFS_ATTR_MAX;
30683074
}
30693075

3070-
LFS_ASSERT(lfs->cfg->name_max <= LFS_NAME_MAX);
3071-
lfs->name_max = lfs->cfg->name_max;
3072-
if (!lfs->name_max) {
3073-
lfs->name_max = LFS_NAME_MAX;
3074-
}
3075-
30763076
// setup default state
30773077
lfs->root[0] = 0xffffffff;
30783078
lfs->root[1] = 0xffffffff;
@@ -3132,9 +3132,9 @@ int lfs_format(lfs_t *lfs, const struct lfs_config *cfg) {
31323132

31333133
.block_size = lfs->cfg->block_size,
31343134
.block_count = lfs->cfg->block_count,
3135-
.attr_max = lfs->attr_max,
31363135
.name_max = lfs->name_max,
31373136
.inline_max = lfs->inline_max,
3137+
.attr_max = lfs->attr_max,
31383138
};
31393139

31403140
lfs_superblock_tole32(&superblock);
@@ -3204,17 +3204,6 @@ int lfs_mount(lfs_t *lfs, const struct lfs_config *cfg) {
32043204
}
32053205

32063206
// check superblock configuration
3207-
if (superblock.attr_max) {
3208-
if (superblock.attr_max > lfs->attr_max) {
3209-
LFS_ERROR("Unsupported attr_max (%"PRIu32" > %"PRIu32")",
3210-
superblock.attr_max, lfs->attr_max);
3211-
err = LFS_ERR_INVAL;
3212-
goto cleanup;
3213-
}
3214-
3215-
lfs->attr_max = superblock.attr_max;
3216-
}
3217-
32183207
if (superblock.name_max) {
32193208
if (superblock.name_max > lfs->name_max) {
32203209
LFS_ERROR("Unsupported name_max (%"PRIu32" > %"PRIu32")",
@@ -3236,6 +3225,18 @@ int lfs_mount(lfs_t *lfs, const struct lfs_config *cfg) {
32363225

32373226
lfs->inline_max = superblock.inline_max;
32383227
}
3228+
3229+
if (superblock.attr_max) {
3230+
if (superblock.attr_max > lfs->attr_max) {
3231+
LFS_ERROR("Unsupported attr_max (%"PRIu32" > %"PRIu32")",
3232+
superblock.attr_max, lfs->attr_max);
3233+
err = LFS_ERR_INVAL;
3234+
goto cleanup;
3235+
}
3236+
3237+
lfs->attr_max = superblock.attr_max;
3238+
}
3239+
32393240
}
32403241

32413242
// has globals?

lfs.h

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -44,29 +44,28 @@ typedef int32_t lfs_soff_t;
4444

4545
typedef uint32_t lfs_block_t;
4646

47-
// Maximum size of all attributes per file in bytes, may be redefined but a
48-
// a smaller LFS_ATTR_MAX has no benefit. Stored in 12-bits and limited
49-
// to <= 0xfff. Stored in superblock and must be respected by other
50-
// littlefs drivers.
51-
#ifndef LFS_ATTR_MAX
52-
#define LFS_ATTR_MAX 0x1ffe
53-
#endif
54-
5547
// Maximum name size in bytes, may be redefined to reduce the size of the
56-
// info struct. Limited to <= LFS_ATTR_MAX. Stored in superblock and must
57-
// be respected by other littlefs drivers.
48+
// info struct. Limited to <= 8190. Stored in superblock and must be
49+
// respected by other littlefs drivers.
5850
#ifndef LFS_NAME_MAX
5951
#define LFS_NAME_MAX 0xff
6052
#endif
6153

62-
// Maximum inline file size in bytes. Large inline files require a larger
63-
// cache size, but if a file can be inline it does not need its own data
64-
// block. Limited to <= LFS_ATTR_MAX and <= cache_size. Stored in superblock
65-
// and must be respected by other littlefs drivers.
54+
// Maximum inline file size in bytes, may be redefined to limit RAM usage,
55+
// but littlefs will automatically limit the LFS_INLINE_MAX to the
56+
// configured cache_size. Limited to <= 8190. Stored in superblock and must
57+
// be respected by other littlefs drivers.
6658
#ifndef LFS_INLINE_MAX
6759
#define LFS_INLINE_MAX 0x1ffe
6860
#endif
6961

62+
// Maximum size of custom attributes in bytes, may be redefined, but there is
63+
// no real benefit to using a smaller LFS_ATTR_MAX. Limited to <= 8190. Stored
64+
// in superblock and must be respected by other littlefs drivers.
65+
#ifndef LFS_ATTR_MAX
66+
#define LFS_ATTR_MAX 0x1ffe
67+
#endif
68+
7069
// Possible error codes, these are negative to allow
7170
// valid positive return values
7271
enum lfs_error {
@@ -213,35 +212,39 @@ struct lfs_config {
213212
// lookahead block.
214213
void *lookahead_buffer;
215214

216-
// Optional upper limit on file attributes in bytes. No downside for larger
217-
// attributes size but must be less than LFS_ATTR_MAX. Defaults to
218-
// LFS_ATTR_MAX when zero.Stored in superblock and must be respected by
219-
// other littlefs drivers.
220-
lfs_size_t attr_max;
221-
222215
// Optional upper limit on length of file names in bytes. No downside for
223216
// larger names except the size of the info struct which is controlled by
224217
// the LFS_NAME_MAX define. Defaults to LFS_NAME_MAX when zero. Stored in
225218
// superblock and must be respected by other littlefs drivers.
226219
lfs_size_t name_max;
227220

228-
// Optional upper limit on inlined files in bytes. Large inline files
229-
// require a larger cache size, but if a file can be inlined it does not
230-
// need its own data block. Must be smaller than cache_size and less than
231-
// LFS_INLINE_MAX. Defaults to min(LFS_INLINE_MAX, read_size) when zero.
232-
// Stored in superblock and must be respected by other littlefs drivers.
221+
// Optional upper limit on inlined files in bytes. Inline files must be
222+
// backed by RAM, but if a file fits in RAM it can be inlined into its
223+
// directory block without needing its own data block. Must be <=
224+
// cache_size and LFS_INLINE_MAX. Defaults to min(LFS_INLINE_MAX,
225+
// cache_size) when zero. Stored in superblock and must be respected by
226+
// other littlefs drivers.
233227
lfs_size_t inline_max;
228+
229+
// Optional upper limit on custom attributes in bytes. No downside for
230+
// larger attributes size but must be <= LFS_ATTR_MAX. Defaults to
231+
// LFS_ATTR_MAX when zero. Stored in superblock and must be respected by
232+
// other littlefs drivers.
233+
lfs_size_t attr_max;
234234
};
235235

236236
// File info structure
237237
struct lfs_info {
238238
// Type of the file, either LFS_TYPE_REG or LFS_TYPE_DIR
239239
uint8_t type;
240240

241-
// Size of the file, only valid for REG files
241+
// Size of the file, only valid for REG files. Limited to 32-bits.
242242
lfs_size_t size;
243243

244-
// Name of the file stored as a null-terminated string
244+
// Name of the file stored as a null-terminated string. Limited to
245+
// LFS_NAME_MAX+1, which can be changed by redefining LFS_NAME_MAX to
246+
// reduce RAM. LFS_NAME_MAX is stored in superblock and must be
247+
// respected by other littlefs drivers.
245248
char name[LFS_NAME_MAX+1];
246249
};
247250

@@ -340,9 +343,9 @@ typedef struct lfs_superblock {
340343
lfs_size_t block_size;
341344
lfs_size_t block_count;
342345

343-
lfs_size_t attr_max;
344346
lfs_size_t name_max;
345347
lfs_size_t inline_max;
348+
lfs_size_t attr_max;
346349
} lfs_superblock_t;
347350

348351
// The littlefs filesystem type
@@ -377,9 +380,9 @@ typedef struct lfs {
377380
const struct lfs_config *cfg;
378381
lfs_size_t block_size;
379382
lfs_size_t block_count;
380-
lfs_size_t attr_max;
381383
lfs_size_t name_max;
382384
lfs_size_t inline_max;
385+
lfs_size_t attr_max;
383386
} lfs_t;
384387

385388

0 commit comments

Comments
 (0)