exfat: add EXFAT_IOC_COMBINE to append a file's clusters to another
Subsystems (4)
PATCH v7 1/1 exfat: add EXFAT_IOC_COMBINE to append a file's clusters to another
Cameras and dashcams recording to exFAT SD cards often write the video stream and auxiliary data (e.g. IMU samples) to separate files and merge them when recording stops. Doing that with read()/write() or copy_file_range() copies all the data of the source, which takes time proportional to its size on such media, wears the flash and needs free space for a second copy.
Filesystems supporting reflink can already avoid the copy by cloning the source to the end of the destination with FICLONERANGE and then unlinking it. 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 then unlinks the source, so that only metadata changes: the FAT, the stream extension entry of the destination and the entry set of the source:
struct exfat_combine_args args = { .src_fd = src_fd };
ioctl(dst_fd, EXFAT_IOC_COMBINE, &args);The source is unlinked as unlink(2) does it: start_removing_dentry() revalidates the dentry of the open file and locks its parent, and security_path_unlink() and vfs_unlink() check permissions and security modules, break leases and delegations, and handle mount points; vfs_unlink() calls exfat_unlink(), which deletes the entry set. Since the in-memory inode no longer owns the clusters, evicting it frees nothing. The checks that can be made without unlinking are made before anything is changed, and 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 is documented in the uapi header, and keeps stale on-disk data out of the file.
exFAT has no journal, so the data and the FAT, then the destination entry, then the deletion of the source entry set 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. 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. 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.
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 v7, following the Sashiko review of v6 [1]:
- When flushing the metadata, after Step 1 and after unlinking the source, also check the errseq of the block device mapping, not only the return value of sync_blockdev(): its error flags go to whoever checks them first, an fsync() of any file on the volume included. On a volume mounted with -o sync, a failed write of the FAT in Step 1 is only recorded there, so v6 could write the entry of the destination over a chain that never reached the disk. This also catches a failed write of the dirty flag at the start of Step 1, which exfat_set_volume_dirty() does not report, and without which a crash during the call would not lead to -EUCLEAN. The errseq is advanced, so that an error nobody had seen before the call fails one call only.
This applies to linux-next (tested on 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 the exfat dev branch until that is merged there. I can rebase it onto exfat dev then.
Testing (linux-next 20261118 + this patch, arm64):
- Reproduced on v6 and checked on v7 with fail_make_request and a test-only delay between Step 1 and its flush: on a volume mounted with -o sync, the write of the FAT sector holding the chain of the source fails and an fsync() of another file takes the error. v6 reported success and unlinked the source, leaving the destination with a broken chain (exfat set the volume read-only when reading it, fsck.exfat truncates it); v7 fails with -EIO and leaves both files unchanged. The same when the write of the dirty flag fails instead:
v6 combined the files with the volume still marked clean on disk.
- All tests reported for v6 were rerun and pass, including the cases that use test-only knobs, and fstests exfat/001 and exfat/002.
- Builds without warnings with W=1 for arm64 (gcc, clang), arm and i386, built-in and as a module; no new sparse warnings.
- Not rerun for v7: the long-running tests reported for v2.
- Known limits, as for v6: a source chain running through the destination is not found if the volume has enough other clusters in use; after a forced shutdown, unmounting still clears the dirty flag (an existing exfat behaviour), so a volume left with only the FAT updated is not refused afterwards.
[1] https://sashiko.dev/#/patchset/20261124212236.830224-1-shaopei_1012%40163.com
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 | 740 ++++++++++++++++++ include/uapi/linux/exfat.h | 48 ++ 3 files changed, 789 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 +740 -0
760 lines not on this page · raw message
include/uapi/linux/exfat.h +48 -0
58 lines not on this page · raw message
Signature · 1 line
2.50.1 (Apple Git-155)
Namjae Jeon
maintainerQuoted 7 lines · Shaopei Zhu
Cameras and dashcams recording to exFAT SD cards often write the video stream and auxiliary data (e.g. IMU samples) to separate files and merge them when recording stops. Doing that with read()/write() or
copy_file_range()copies all the data of the source, which takes time proportional to its size on such media, wears the flash and needs free space for a second copy.
Is there a specific device or product expected to use this feature, or is it being added as a potentially useful capability without a concrete use case? If there is a target device or workflow, could you share some details?
Quoted 9 lines
Filesystems supporting reflink can already avoid the copy by cloning the source to the end of the destination with FICLONERANGE and then unlinking it. 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_sizeper 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.
Quoted 4 lines
Add an ioctl that moves the cluster chain of a source file to the end of a destination file and then unlinks the source, so that only metadata changes: the FAT, the stream extension entry of the destination and the entry set of the source:
I suggest changing EXFAT_IOC_COMBINE to transfer only the source’s cluster chain to the destination, leaving the source linked as an empty file. Userspace can unlink it afterward if desired.
Thanks for the patch.