Repository navigation
Keep the volume consistent if the driver is killed during truncate or a node flush - #220
gowtham0992 wants to merge 2 commits into
Conversation
If the process dies between cutting the chain and writing the file's entry, the entry records more clusters than the chain has, which fsck cannot repair and which makes the file unreadable. Writing the entry first leaves only unused clusters behind. When the file becomes empty, the entry written first also drops the start cluster, as an empty file must.
exfat_flush_node() wrote the entry and then the bitmap. If the process dies in between, the entry points to clusters the bitmap marks free, which fsck reports as not allocated and cannot repair. Writing the bitmap first leaves only clusters marked used that nothing refers to yet.
relan/exfat#220 offers the two reorderings QFuse carries in patches/exfat-crash-order.patch; relan/exfat#221 reports the rename window.
|
A correction to the description, and one gap I should have stated. Shrinking now writes the entry twice. I wrote that "the same writes No rollback if The rename window mentioned at the end is now #221. |
These two commits change the order of two pairs of writes, so that a driver
killed between them leaves a volume
exfatfsckaccepts. Nothing else changes:the same writes happen, with the same sizes.
1. Write the smaller size before cutting the cluster chain. When a
non-contiguous file is shrunk,
shrink_file()writes the new end of the chainto the FAT straight away, and the smaller size reaches the directory entry only
when
fuse_exfat_truncate()callsexfat_flush_node()afterwards. A driverkilled in between leaves an entry that records more clusters than its chain
has.
exfatfsckreportsfile 'a' has invalid cluster 0xffffffffand-pcannot fix it, and reading the file fails. With the change,
exfat_truncate()writes the entry with the new size first; dying before the chain is cut then
leaves only clusters that nothing uses. When the new size is 0 that first entry
also has no start cluster, since an entry for an empty file that keeps one
(
'a' is empty but start cluster is 0x6) stops fsck and the mount; the chain isthen freed from the saved start cluster as before. Truncating to 0 on master
has the same problem as any other shrink (
invalid cluster 0x0).2. Write the clusters bitmap before the entry that uses it.
exfat_flush_node()wrote the entry and then calledexfat_flush(). A driverkilled in between leaves an entry that points to clusters the bitmap marks
free:
cluster 0x6 of file 'new.bin' is not allocated, which-pcannot fixeither. The change moves the existing
exfat_flush()call beforewrite_entries(). I checked with strace that creating a file still makes thesame four device writes (entry, data, bitmap, entry instead of entry, data,
entry, bitmap), so it costs nothing.
Reproducing it deterministically on Linux. Both windows can be hit every
time with gdb, by stopping the driver between the two writes and killing it.
On Debian 12 (Linux 6.8, libfuse 2.9.9), master
a3ce789, an 8 MiB image on aloop device:
exfatfsck -nthen-pfile 'a' has invalid cluster 0xffffffff, ERRORS FOUND: 1, FIXED: 0; readingafailsareads back intactfile 'a' has invalid cluster 0x0, ERRORS FOUND: 1, FIXED: 0; readingafailsareads back intactcluster ... of file 'new.bin' is not allocated, ERRORS FOUND: 5, FIXED: 0With the commits the same breakpoints fall one write earlier: in 1 before the
entry is written, in 2 after the bitmap. I also stopped the patched driver at the
shrink_file()call, after the new entry is written and before the chain iscut, for both sizes: no errors, and
areads back at 4096 and at 0 bytes. The CItest script passes.
How it was found. I maintain QFuse,
which runs this driver on QNX 8.0. A test there kills the driver with SIGKILL at
random points, 200 times, while a client creates, truncates, renames and
removes files, and checks the volume with
exfatfsckafter each kill. On QNXx86_64 and aarch64 together, 10 of 400 kills left a volume
exfatfsck -pcouldnot repair (8 interrupted shrinks, 2 entries ahead of the bitmap); with these
two commits, 0 of 400. The same shrink failure also showed up under Linux FUSE
in a random-kill loop (2 of 52 interrupted truncates). Test and transcripts:
tests/test_exfat_campaign.canddocs/proof/exfat-campaign.txtin thatrepository.
One more window that this does not touch:
rename_entry()erases the old entrybefore writing the new one, so a rename interrupted between the two loses the
file. I'll open a separate issue for that, since the obvious reordering would
leave two entries sharing one chain.