Skip to content

Keep the volume consistent if the driver is killed during truncate or a node flush - #220

Open
gowtham0992 wants to merge 2 commits into
relan:masterfrom
gowtham0992:crash-order
Open

gowtham0992 wants to merge 2 commits into
relan:masterfrom
gowtham0992:crash-order

Conversation

@gowtham0992

Copy link
Copy Markdown
Contributor

These two commits change the order of two pairs of writes, so that a driver
killed between them leaves a volume exfatfsck accepts. 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 chain
to the FAT straight away, and the smaller size reaches the directory entry only
when fuse_exfat_truncate() calls exfat_flush_node() afterwards. A driver
killed in between leaves an entry that records more clusters than its chain
has. exfatfsck reports file 'a' has invalid cluster 0xffffffff and -p
cannot 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 is
then 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 called exfat_flush(). A driver
killed in between leaves an entry that points to clusters the bitmap marks
free: cluster 0x6 of file 'new.bin' is not allocated, which -p cannot fix
either. The change moves the existing exfat_flush() call before
write_entries(). I checked with strace that creating a file still makes the
same 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 a
loop device:

# 1: make a file non-contiguous, then truncate it while stopped in exfat_flush_node()
head -c 4096 /dev/urandom > mnt/a
head -c 4096 /dev/urandom > mnt/b
head -c 65536 /dev/urandom >> mnt/a
gdb -batch -p $(pgrep mount.exfat) -ex 'break exfat_flush_node' -ex continue -ex kill &
truncate -s 4096 mnt/a

# 2: create a file while stopped at node.c:677, between the entry and the bitmap
gdb -batch -p $(pgrep mount.exfat) -ex 'break node.c:677' -ex continue -ex kill &
cp 20k-file mnt/new.bin
Case, exfatfsck -n then -p master with these commits
1, truncate to 4096 file 'a' has invalid cluster 0xffffffff, ERRORS FOUND: 1, FIXED: 0; reading a fails No errors found; a reads back intact
1, truncate to 0 file 'a' has invalid cluster 0x0, ERRORS FOUND: 1, FIXED: 0; reading a fails No errors found; a reads back intact
2, new file five cluster ... of file 'new.bin' is not allocated, ERRORS FOUND: 5, FIXED: 0 No errors found

With 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 is
cut, for both sizes: no errors, and a reads back at 4096 and at 0 bytes. The CI
test 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 exfatfsck after each kill. On QNX
x86_64 and aarch64 together, 10 of 400 kills left a volume exfatfsck -p could
not 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.c and docs/proof/exfat-campaign.txt in that
repository.

One more window that this does not touch: rename_entry() erases the old entry
before 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.

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.
gowtham0992 added a commit to gowtham0992/qfuse that referenced this pull request Nov 28, 2026
relan/exfat#220 offers the two reorderings QFuse carries in
patches/exfat-crash-order.patch; relan/exfat#221 reports the rename window.
@gowtham0992

Copy link
Copy Markdown
Contributor Author

A correction to the description, and one gap I should have stated.

Shrinking now writes the entry twice. I wrote that "the same writes
happen, with the same sizes". That holds for creating a file, which I checked
with strace, but not for a shrink. The first commit writes the entry with the
new size before shrink_file(); exfat_truncate() then marks the node dirty
again (node->is_dirty = true after node->size = size), and
fuse_exfat_truncate() flushes it a second time. So each shrink costs one
more directory-entry write than on master. I found this by reading the code
and have not measured it. If you'd rather avoid it, exfat_truncate() could
skip marking the node dirty when nothing changed after the early flush; I can
add that as a third commit.

No rollback if shrink_file() fails. If the early flush succeeds and
shrink_file() then returns an I/O error, the entry already records the
smaller size while the chain is still the old length, and node->size keeps
the new value. The result is clusters that nothing uses, the same state as a
kill in that window. exfatfsck accepts it; I have not checked whether other
checkers report such clusters. Restoring the old size there would mean writing
the entry again after a write has just failed, so I left it as is, but I
should have said so.

The rename window mentioned at the end is now #221.

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.

1 participant