exfat: add EXFAT_IOC_COMBINE to append a file's clusters to another
Subsystems (4)
PATCH v14 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 therefore has to end at a cluster boundary and to be written up to its size; otherwise the call fails with -EINVAL. Padding the destination is left to user space, which knows what its file format allows there, e.g. an MP4 'free' box, rather than having the kernel insert zeroes into the middle of the file. The cluster size is reported in st_blksize. This is documented in the uapi header.
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.
A step is only taken once the previous one has reached the medium. If the FAT cannot be flushed, the link is undone and the call fails with nothing changed. If the device fails a write after that, of an entry or of the cache flush that follows it, the call stops there: the source is not emptied unless the destination entry is known to be on the medium. The two files may then share clusters on disk, so the volume is kept dirty across unmount and a filesystem error is raised, which makes the volume read-only with the default errors=remount-ro, until fsck.exfat has run. Once the volume is in an emergency state, nothing more is written, which leaves what a crash at that point would. 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 9.9 ms 2.2 s
500 MiB 10.1 ms 22.3 s 1 GiB 10.3 ms 45.6 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 uses the magic number 0xEF, which fs/ntfs already uses for 00-0F, with the range 10-1F for exfat. 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 v14:
- Namjae: when the cache flush after writing the destination entry fails, return the error without updating the source entry.
- Andy: a destination that does not end at a cluster boundary, or is not written up to its size, is refused with -EINVAL instead of being zero-filled by the kernel. The padding and the code undoing it are gone; user space pads the file with what its format allows.
- David, Namjae: use the ioctl magic number 0xEF instead of 'r'. I took 10-1F, as 00-0F belongs to fs/ntfs; please tell me if you prefer another range.
- Sashiko on v13 [1]: after a failed write of the destination entry, write the old entry back at once instead of leaving it to writeback.
- Every write or flush failure after the FAT or an entry was changed is handled in one place: stop, keep the volume dirty across unmount and raise a filesystem error. That now covers the final flush of the source entry as well.
- A chain of the destination that does not end with its size is refused as a bogus chain; so far it was accepted if it ran into the source.
- Cleanups: helpers for setting a chain and for writing an entry, the two chain checks merged, the duplicate checks in __exfat_combine() dropped, inode_dio_wait() before the consistency checks, and the last flush only flushes the device cache.
Not changed: a source opened with O_APPEND is still accepted.
Testing (next-20261118 + this patch, arm64):
- Functional tests on 512 B, 4 KiB and 128 KiB clusters, checked again after a remount, including the refused destinations (unaligned, extended by truncate or fallocate), which are combined after the test pads them; unlinked source; no permission to remove the source, source that is a mount point.
- fstests exfat/001 and exfat/002 (002 updated for the new rule).
- Power cuts: 30 runs with fsync (18 inside the ioctl, 9 with shared clusters repaired by fsck.exfat) and 20 without; nothing synced lost.
- With a test-only knob that fails a write the way end_buffer_write_sync() does, or a flush: failures of the destination entry, of writing it back, of the flush after it, of the source entry, of the final flush and of the flush when undoing the link all leave the files with the expected contents and the volume read-only and dirty, and fsck.exfat repairs it; a failed flush of the FAT with a working undo leaves the volume clean and writable, and a retry succeeds.
- Builds without warnings with W=1 for arm64 (gcc, clang), arm and i386, built-in and as a module; no new sparse warnings.
- The measurements in the commit message were redone with this version.
- Not rerun: the long-running tests reported for v2.
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.
[1] https://sashiko.dev/#/patchset/20261128135710.961256-1-shaopei_1012%40163.com
v13: https://lore.kernel.org/all/[email protected]/
v12: https://lore.kernel.org/all/[email protected]/
v11: https://lore.kernel.org/all/[email protected]/
v8: https://lore.kernel.org/all/[email protected]/
v7: https://lore.kernel.org/all/[email protected]/
.../userspace-api/ioctl/ioctl-number.rst | 1 + fs/exfat/file.c | 537 ++++++++++++++++++ include/uapi/linux/exfat.h | 50 ++ 3 files changed, 588 insertions(+)
Documentation/userspace-api/ioctl/ioctl-number.rst +1 -0
@@ -403,6 +403,7 @@ Code Seq# Include File Comments 0xEC 00-01 linux/platform_data/cros_ec_chardev.h ChromeOS EC driver 0xEE 00-09 uapi/linux/pfrut.h Platform Firmware Runtime Update and Telemetry 0xEF 00-0F uapi/linux/ntfs.h NTFS +0xEF 10-1F uapi/linux/exfat.h exFAT 0xF3 00-3F drivers/usb/misc/sisusbvga/sisusb.h sisfb (in development) <mailto:[email protected]> 0xF6 all LTTng Linux Trace Toolkit Next Generation
fs/exfat/file.c +537 -0
555 lines not on this page · raw message
include/uapi/linux/exfat.h +50 -0
60 lines not on this page · raw message
base-commit: e8017d29067fd1310a65c3494ce1a1b492adbac6
Signature · 1 line
2.53.0