Skip to content

[Feature Request] Improve seed generation / allow for platform-provided entropy/seeds. #488

@gtaska

Description

@gtaska

I am in the process of evaluating littlefs for our platform and wanted to get a better understanding of how well the wear-levelling works. I spent the last couple of days playing around with the littlefs-js demo (http://littlefs.geky.net/demo.html), and noticed that wear levelling does not appear to work in the demo - it always starts wearing out the start of the emulated device first.

(This lead me to checking the code for possible issues and ended up finding issue #484).

I haven't had much luck getting littlefs-js running locally, so added some debug logs to our platform, and got output similar to the following:

lfs_mount
lfs_init: seed = 0
lfs_dir_fetchmatch()
lfs_dir_fetchmatch: seed = 00000000 ^ 83F4A2A4 = 83F4A2A4
lfs_alloc_reset()
lfs_dir_fetchmatch()
lfs_dir_fetchmatch: seed = 83F4A2A4 ^ 83F4A2A4 = 00000000
lfs_dir_fetchmatch()
lfs_dir_fetchmatch: seed = 00000000 ^ 83F4A2A4 = 83F4A2A4
lfs_alloc(): 676

(Note: I know that this doesn't show the problem that the littlefs-js demo exhibits, as the seed value is non-zero when lfs_alloc_reset is called. In this log, the first lfs_alloc does in fact pick a block from a relatively random location in flash).

What is alarming to see, is the fact that the seed gets updated multiple times - and that (in this case) every second instance causes the result to be 0x00000000 (an obvious side-effect of using the XOR operation - a repeat of the operation will undo the previous operation). Now, this is only a highly contrived example with only minimal FS usage (essentially, the boot cycle count example mentioned in the main README.md file), but it is a worry that the seed keeps flip-flopping with 0. Whether or not this is a problem depends on when lfs_alloc_reset() is called, relative to the number of times lfs_dir_fetchmatch gets called. IMHO, the current solution is fragile - it wouldn't be hard for a second innocuous call to be added within the callflow of lfs_mount - and wear levelling to become broken.

A better option might be to use these crc values as input into a separate, on-going hash algorithm (which, ironically, could be a crc32). Pretty much any hash algorithm other than XOR would not suffer this problem.

An even better option, at least for some users, would be to allow the entropy/seed to be provided via a separate (optional) callback function. This would allow platforms using littlefs to leverage platform-specific entropy sources, such as a hardware-based random number generator, or even a real-time clock.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions