Repository navigation
fix: persist media and virtual-device changes across a power cut - #987
Merged
Merged
Conversation
…r cut Deleting an image only touched the page cache: the unlink was not followed by an fsync of the directory, and the data partition is exFAT, which has no journal. A power cut before writeback rolled the directory entry back, so the image the user deleted was back after the reboot - it kept its original inode and timestamps, which is what a rolled back dirent looks like. The same gap applied to the rename that installs an uploaded or downloaded image and to mkdir: the name pointing at the entries was never synced. On top of that both transfer paths closed their temporary file without an fsync, so even a correct rename could publish an image whose bytes were still only in the page cache. Sync the directory after every entry change, sync the transferred image before the rename that publishes it, and do the two transfer flushes outside the media lock: on exFAT an inode fsync also writes the device's dirty metadata and flushes its cache, and an image is multi-gigabyte. A sync failure on the delete paths is logged rather than returned: the entry is already gone from the running system, so the caller can only be told that it may come back.
The virtual USB disk and the virtual network are switched on and off by a marker file on /boot that the boot script reads. The toggle created or removed that marker and restarted the gadget, but never synced /boot: it is FAT, without a journal, so the entry stayed in the page cache and a power cut right after the switch brought the marker back. A device the user turned off was on again after the reboot, and one turned on could come up off. Sync the /boot directory once the marker commands ran.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Two user actions on the device were not durable on disk:
success, but the image/file was back after a power cut.
the device was on again after the next boot.
Root cause
Both are directory entry changes on filesystems without a journal:
/data, which is exFAT;/boot, which is FAT.The code never fsynced the directory after the entry change (
os.Remove/os.RemoveAll,os.Rename,os.Mkdir, or therm/touchof the marker). Theentry stayed in the page cache, so a power cut before writeback rolled it back: the
file reappeared with its original inode and timestamps, and the boot script saw the
marker and re-created the virtual disk.
The two image transfer paths had the same gap: they closed their temporary file
without an fsync, so even a correct rename could publish an image whose bytes were
still only in the page cache.
Changes
diskfs.SyncDir(), called after every entry change:removeFile,writeFile's rename,mkdirDir,deleteMediumandinstallImage;deleteMediumsyncs the file's parent directory, images may live insubdirectories;
Sync()the temporaryfile before the rename that publishes it, outside the media lock;
/bootafter the marker commands.Testing
go build ./service/storage/... ./service/download/... ./service/vm/...,go vet,go test(addedTestSyncDir);(with the previous binary the same sequence brings it back);
came back on);
after the reboot;
there are covered too.
Notes
already gone from the running system, so the caller can only be told that it may
come back.
its cache, so a folder upload costs one sync per file. This is noted in the
helper's doc comment.