fix: add Sync() calls to persist filesystem changes to storage #27
fix: add Sync() calls to persist filesystem changes to storage #27marcelfarres wants to merge 1 commit intotinygo-org:devfrom
Conversation
- Add Sync() after Format, Unmount, Remove, Rename, Mkdir - Add Sync() after File.Close, File.Sync, File.Truncate - Sync on OpenFile with O_CREATE/O_TRUNC flags - Fix cache invalidation in lfs_bd_flush - Add TestDirectoryPersistence and NestedDirectories tests"
Fix filesystem persistence on SD cardsProblemI was working with LittleFS on a Grand Central M4 with an SD card and noticed that directories and files I created weren't surviving a power cycle. I'd run InvestigationI traced the data flow through the code:
The problem: the SD card driver holds writes in a buffer for efficiency. Without explicitly calling Looking at the original code, I noticed that mutating operations just returned after calling the C function—no sync. The block device interface in What I changed
|
| return nil, err | ||
| } | ||
|
|
||
| if flags&(os.O_CREATE|os.O_TRUNC) != 0 { |
There was a problem hiding this comment.
I do not think it makes sense to call Sync() on an Open().
| if err := errval(C.lfs_file_sync(f.lfs.lfs, f.fileptr())); err != nil { | ||
| return err | ||
| } | ||
| if syncer, ok := f.lfs.dev.(tinyfs.Syncer); ok { |
There was a problem hiding this comment.
Actually Sync() on a Sync()? Seems like a yes! 👍
|
Hello @marcelfarres thanks for the code submission. Please see my comments. |
|
I probably did not notice the issue due to using flash pretty much all of the time. |
|
I am still investigating the fix, somehow I can not reproduce the error or the fix all the time? Also it seems that using different -opt levels influence the results (with the unpatch branch opt 0 1 2 have the bug, but s and z pass the tests). Any idea or experience on that? I want to be sure that the change does not add to the unreliable behavior. It also seems that SPI freq plays a role too? A bit puzzle. |
I was trying to create folders & files in the SD card and it was not working.
This changes seem to make it functional again.
I test it on a M4 Gran Central, and seems to have dir persistent and creation even after mounting/un-moutning the file system.
Not an expert in any extend on TinyGo, hope it helps/you can test it/review it.
Should fix #9