exfat: add EXFAT_IOC_COMBINE to append a file's clusters to another
Subsystems (4)
PATCH v11 1/1 exfat: add EXFAT_IOC_COMBINE to append a file's clusters to another
Cameras recording to exFAT SD cards often write auxiliary data, such as GPS, motion or per-frame exposure data, to separate files next to the video stream, and merge them into the video file when recording stops, so that playback, editing and post-processing work on a single file. That data grows with the length of the recording, to hundreds of MiB.
Merging it with read()/write() or copy_file_range() copies all of that data, which takes time proportional to its size on such media, wears the flash and needs free space for a second copy. On a battery-powered device with a removable card, a copy that takes tens of seconds is also a long window in which the card can be pulled or the power lost, leaving a partially merged file.
Filesystems supporting reflink can already avoid the copy by cloning the source to the end of the destination with FICLONERANGE. exFAT cannot share clusters between files, so it needs an operation that moves them instead. Its semantics follow from the exFAT on-disk format (cluster granularity, a single valid_size per file), so it is added as an exfat ioctl rather than a VFS interface; fallocate() does not fit either, as it only takes a single file.
Add an ioctl that moves the cluster chain of a source file to the end of a destination file and leaves the source as an empty file, so that no file data is copied: only the FAT and the stream extension entries of both files change:
struct exfat_combine_args args = { .src_fd = src_fd };
ioctl(dst_fd, EXFAT_IOC_COMBINE, &args);As with the other interfaces that move or share data between files, removing the source is left to the caller, which unlinks it afterwards if it is not needed. An already unlinked source is accepted. An unlinked destination is refused with -ENOENT, as its entry is no longer written and the data of the source would be lost with it.
The source must be open for reading and writing, as it is emptied and its data becomes readable through the destination, and the destination for writing; both must be regular files on the same mount. Files whose allocation, chain layout or valid_size are inconsistent, as after a failed truncate or on a corrupt volume, or that are found to share clusters, are refused with -EIO before anything is changed.
exFAT has a single size and valid_size per file, so clusters can only be appended at a cluster boundary of a fully initialized file. The destination is therefore first zero-filled up to the end of its last cluster with exfat_extend_valid_size(), leaving a zero-filled gap between the original and the appended data when its size is not cluster aligned. This keeps stale on-disk data out of the file and is documented in the uapi header. The gap lies in the last cluster, which is already allocated to the destination, so it takes no extra space.
exFAT has no journal, so the data and the FAT, then the destination entry, then the source entry as an empty file are written and flushed to the device in that order, as recommended by section 8.1 of the exFAT specification; the allocation bitmap is not touched. A crash at any point leaves all data reachable from at least one entry. In the worst case both entries share the appended clusters; fsck.exfat keeps them in the file it checks first and truncates the other one, so either the combine has completed or the source still holds its data. A crash between the first two writes leaves the chain of the destination linked to the clusters of the source while its size does not cover them, which fsck.exfat cuts off again. In both cases neither file may be truncated or removed before fsck.exfat has run, as freeing the clusters of one would free clusters the other still uses; the volume is marked dirty before the FAT is changed. On a volume that was not cleanly unmounted, the call fails with -EUCLEAN until fsck.exfat has run, so that it never works on clusters that two files may already share or that may already be linked. Once the volume is in an emergency state, nothing more is written, which leaves what a crash at that point would. If the call fails before the clusters are moved, the padding is removed again; what was already written back is kept initialized, as exfat_extend_valid_size() does on failure. Write back errors are checked through the errseq of the mappings, as their error flags may be taken by a concurrent fsync(). s_lock is only held while updating the FAT and the entries, not while waiting for the device, so that writers to other files on the volume are not stalled.
No file data is copied, so the cost depends on the number of clusters (when a FAT chain has to be built for a contiguous file) rather than on the amount of data. Measured on a disk throttled to SD card speeds (30 MiB/s write), appending to a 300 MiB file with 128 KiB clusters, until the result is on disk:
source EXFAT_IOC_COMBINE copy_file_range() + fsync()
50 MiB 12.3 ms 2.2 s 500 MiB 13.3 ms 22.3 s 1 GiB 13.0 ms 45.7 s
The files of this test were contiguous. Building the FAT chain of a contiguous file, when it is needed, takes about 100 ms per GiB with 4 KiB clusters, and 32 times less with 128 KiB clusters.
The ioctl number is taken from the 'r' range next to the FAT ioctls. A user space tool for exfatprogs (exfatcombine) and a test for fstests (exfat/002) are posted separately.
This patch was written with the help of an AI coding assistant (Claude Code, Opus 5.5); I have reviewed and tested the result.
---
Notes:
Changes in v11, comments and commit message only, no functional change:
- Document that after a crash neither file, not only the source, may be truncated or removed before fsck.exfat has run, and describe the state a crash between the FAT update and the destination entry leaves, in the uapi header, the comment of __exfat_combine() and the commit message.
This follows the review of v10 by Sashiko [1], which pointed out that the chain of the destination can be left linked to the clusters of the source. I reproduced it with images edited offline (4 KiB clusters, destination with a FAT chain of 3 clusters, source of 3 clusters, volume dirty), mounted without running fsck, doing one operation and then writing a new file:
state on disk operation result
A rm dst src loses its clusters
A truncate dst src silently corrupted
A append to dst both files intact
A rm src dst intact, but its chain runs
into the new file
B rm dst src corrupted
B rm src dst silently corrupted
A, contiguous dst rm dst src intact
A: the FAT links the destination to the source, both entries are
still the old ones (crash between the first two writes)
B: both entries own the clusters (crash before the source entry
is written)
fsck.exfat run before the operation repairs both states without losing data, and the call itself fails with -EUCLEAN on a dirty volume, so the rule is the one that already applies to a dirty exFAT volume; v11 only states it for both files.
The cause is that __exfat_free_cluster() follows a FAT chain to its end rather than for the number of clusters of the file. If you would rather have the kernel cope with the first state than rely on fsck, I can add a patch that makes it stop after the size of the chain; please let me know.
Sashiko's other comment, about a source opened with O_APPEND, is unchanged from v10: not addressed, and I can refuse such a source if you prefer that.
This applies to linux-next (next-20261118). It uses exfat_emergency_state(), which comes with "exfat: don't set SB_RDONLY after filesystem errors" in the vfs tree (vfs-7.5.super.ro), so it does not build on mainline or on the exfat dev branch until that is merged there. I can rebase it onto exfat dev then.
All tests reported for v8 apply unchanged. v10 was a resend of v9 with the base stated; v9 only changed the commit message (use case, the gap taking no space, measurements).
[1] https://sashiko.dev/#/patchset/20261126200717.939328-1-shaopei_1012%40163.com
v10: https://lore.kernel.org/all/[email protected]/
v9: https://lore.kernel.org/all/[email protected]/
v8: https://lore.kernel.org/all/[email protected]/
v7: https://lore.kernel.org/all/[email protected]/
v6: https://lore.kernel.org/all/[email protected]/
v5: https://lore.kernel.org/all/[email protected]/
v4: https://lore.kernel.org/all/[email protected]/
v3: https://lore.kernel.org/all/[email protected]/
v2: https://lore.kernel.org/all/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/
.../userspace-api/ioctl/ioctl-number.rst | 1 + fs/exfat/file.c | 616 ++++++++++++++++++ include/uapi/linux/exfat.h | 47 ++ 3 files changed, 664 insertions(+)
Documentation/userspace-api/ioctl/ioctl-number.rst +1 -0
@@ -292,6 +292,7 @@ Code Seq# Include File Comments 'q' 80-FF linux/telephony.h Internet PhoneJACK, Internet LineJACK linux/ixjuser.h <http://web.archive.org/web/%2A/http://www.quicknet.net> 'r' 00-1F linux/msdos_fs.h and fs/fat/dir.c +'r' 20-2F linux/exfat.h 's' all linux/cdk.h 't' 00-7F linux/ppp-ioctl.h 't' 80-8F linux/isdn_ppp.h
fs/exfat/file.c +616 -0
634 lines not on this page · raw message
include/uapi/linux/exfat.h +47 -0
57 lines not on this page · raw message
base-commit: e8017d29067fd1310a65c3494ce1a1b492adbac6
Signature · 1 line
2.53.0