Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
6 years, 10 months ago.
How to functioning this power loss protection
How is the integrity of the file system ensured, if the hardware fails or the power is suddenly turned off?
Question relating to:
1 Answer
6 years, 10 months ago.
Hi Vyacheslav,
The technical details on how LittleFS works can be found here:
https://github.com/ARMmbed/mbed-os/blob/master/features/filesystem/littlefs/littlefs/DESIGN.md
The section on metadata pairs might be a good place to start.
-
For a quick, high-level explanation:
Anytime LittleFS modifies data on disk, it keeps a backup of the old data until it is sure the write completed successfully.
For metadata blocks, this is accomplished by storing pairs of blocks protected by a checksum and revision count. For a write to have completed successfully, the checksum must be correct. If power is lost during a write, the checksum will fail and cause LittleFS to fall back to the other block in the pair. If both pairs have valid checksums, the revision count determines which write is the most recent.
These metadata pairs give LittleFS the ability to atomically update specific blocks at the cost of higher storage usage for metadata. Since it would be very costly to duplicate all files on the filesystem, for non-meta-data we instead use a copy-on-write data structure. Any time we need to modify a file, we create a partial copy, and use the atomic updates of metadata pairs to replace the old version.
block 1 block 2 block 1 block 2 block 1 block 2 .---------.---------. .---------.---------. .---------.---------. | rev: 1 | rev: 0 | | rev: 1 | rev: 0 | | rev: 1 | rev: 2 | | file: 4 | file: 0 | -> | file: 4 | file: 0 | -> | file: 4 | file: 5 | | xor: 5 | xor: 0 | | xor: 5 | xor: 0 | | xor: 5 | xor: 7 | '---------'---------' '---------'---------' '---------'---------' | | | v v v block 4 block 4 block 5 block 4 block 5 .--------. .--------. .--------. .--------. .--------. | old | | old | | new | | old | | new | | data | | data | | data | | data | | data | | | | | | | | | | | '--------' '--------' '--------' '--------' '--------' update data in file update metadata pair
That's the basic gist of how LittleFS implements power resilience. There's a few other details around how the copy-on-write data structure reuses previously written data, but most of that can be found in the DESIGN.md.
Feel free to comment if you have any questions.