littlefs-project / littlefs

A little fail-safe filesystem designed for microcontrollers
BSD 3-Clause "New" or "Revised" License
4.92k stars 774 forks source link

What is the difference between `gstate`, `gdisk` and `gdelta` in `struct lfs`? #902

Open andriyndev opened 7 months ago

andriyndev commented 7 months ago

From what I understand, they're related to the "global state" but the documentation mentions only one field responsible for global state while the lfs structure contains three. What is each of these fields responsible for?

geky commented 6 months ago

Hi @andriyndev, thanks for creating an issue.

Ah yes... These variables...

They sort of evolved during development, and during several attempts to reduce the number of copies, so the names aren't the greatest.

They become important during commits, when we need to keep track of what gstate will be before and after.

Roughly:

During a commit, gdelta starts off as gstate xor gdisk, but if littlefs drops or relocates any mdirs, gdelta is "steals" the gdelta that was on that mdir.

For drops this is necessary, since we are removing the mdir and our gstate would become corrupted otherwise, for relocations it's an attempt to move gdelta upwards to try to clean up the amount of gdeltas on the system.

The whole gstate system is rather fragile, and I haven't found a way to avoid all three copies.