Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question about file overwriting and big files #123

Open
davidefer opened this issue Oct 29, 2018 · 1 comment
Open

Question about file overwriting and big files #123

davidefer opened this issue Oct 29, 2018 · 1 comment

Comments

@davidefer
Copy link

Hi
I have a question about overwriting big files in LittleFS.
My file system consists of 32KB flash blocks with the following dimension (after format):
Total space 2064384
Used space 131072
Free space 1933312

If I write a 1MB (1024*1024) bytes file everything works.
The state is as follow:
Total space 2064384
Used space 1212416
Free space 851968

Now if I re-write the same file (LFS_O_WRONLY | LFS_O_CREAT | LFS_O_TRUNC) without deleting it, then I get an error "no more space" while writing.
The state is as follow:
Total space 2064384
Used space 2064384
Free space 0

Please note that if I delete the file before overwriting it, then everything works.

I wanted to know what is the reason for that and if there is a general rule for the restrictions one must cope with when dealing with big files.

Thanks in advance

@geky
Copy link
Member

geky commented Jan 28, 2019

Hi @davidefer, sorry for the very late response.

In littlefs, all files are copy-on-write (COW). This means that during a write a copy of the data is made until you call either sync or close. This gives us a copy to fall back on if we lose power. Unfortunately this means writes to a file can cause the file to inflate up to 2x the original size.

Deleting the file before writing is a bit different. If we lose power after the delete call, there will be no file. So the filesystem doesn't need to worry about creating a copy since "no file" is a valid state if we lose power.

We can mimic the delete behaviour if we manually call lfs_file_sync after we open with LFS_O_TRUNC, since this also establishes that "no file" is a valid state.

What to do with O_TRUNC on a COW filesystem doesn't have an obvious answer. I went with keeping a copy because it's safer if a user expects power resilience and you can always call lfs_file_sync to avoid the space error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants