Skip to content

fix: persist media and virtual-device changes across a power cut - #987

Merged
916BGAI merged 2 commits into
sipeed:mainfrom
liangzhuohua:fix/media-delete-durability
Nov 12, 2026
Merged

916BGAI merged 2 commits into
sipeed:mainfrom
liangzhuohua:fix/media-delete-durability

Conversation

@liangzhuohua

Copy link
Copy Markdown
Collaborator

Problem

Two user actions on the device were not durable on disk:

  1. Deleting an image in the storage page, or a file in the file manager, reported
    success, but the image/file was back after a power cut.
  2. Turning the virtual USB disk (or the virtual network) off reported success, but
    the device was on again after the next boot.

Root cause

Both are directory entry changes on filesystems without a journal:

  • the media library lives on /data, which is exFAT;
  • the virtual device switch is a marker file on /boot, which is FAT.

The code never fsynced the directory after the entry change (os.Remove /
os.RemoveAll, os.Rename, os.Mkdir, or the rm/touch of the marker). The
entry 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

  • new diskfs.SyncDir(), called after every entry change: removeFile,
    writeFile's rename, mkdirDir, deleteMedium and installImage;
  • deleteMedium syncs the file's parent directory, images may live in
    subdirectories;
  • both transfer paths (browser upload, remote URL download) Sync() the temporary
    file before the rename that publishes it, outside the media lock;
  • the virtual device toggle syncs /boot after the marker commands.

Testing

  • go build ./service/storage/... ./service/download/... ./service/vm/...,
    go vet, go test (added TestSyncDir);
  • on a NanoKVM Cube, with this build:
    • delete an image, cut the power within 3 seconds, boot: the image stays deleted
      (with the previous binary the same sequence brings it back);
    • turn the virtual USB disk off, cut the power, boot: it stays off (previously it
      came back on);
    • upload an image and cut the power right after the transfer: the file is complete
      after the reboot;
  • the shared partition file manager uses the same helpers, so upload/mkdir/delete
    there are covered too.

Notes

  • A directory sync failure on the delete paths is logged, not returned: the entry is
    already gone from the running system, so the caller can only be told that it may
    come back.
  • On exFAT one directory fsync also writes the device's dirty metadata and flushes
    its cache, so a folder upload costs one sync per file. This is noted in the
    helper's doc comment.

…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.
@916BGAI
916BGAI merged commit abb83f3 into sipeed:main Nov 12, 2026
1 check passed
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

Successfully merging this pull request may close these issues.

2 participants