exfat: add EXFAT_IOC_COMBINE to append a file's clusters to another
Subsystems (4)
PATCH v4 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.
As with EXT4_IOC_MOVE_EXT and F2FS_IOC_MOVE_RANGE, the source must be open for reading and writing, the destination for writing, and 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, 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. 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 v4, following the Sashiko review of v3 [1]:
- Refuse with -EIO a file whose chain layout is neither of the two, or a contiguous file running past the end of the volume, as checked for vendor allocation entries; Step 1 would have written FAT entries out of bounds, or left the FAT alone and orphaned the clusters of the source.
- Refuse to combine with -EUCLEAN on a volume that was not cleanly unmounted, until fsck.exfat has run. v3 only recognized a retry in the same direction after a crash; combining the files the other way round, after a crash with the FAT or both entries updated, made a loop in the FAT. exfat already asks to run fsck in that case, and ntfs3 refuses to mount such a volume read-write. The check walking the clusters of the destination is kept.
- On failure, try to write back the destination before giving up the initialized padding, so that stores through mmap that have already completed are not lost when padding or writing back the source failed; valid_size is only rewound if the destination itself cannot be written back.
- Clamp valid_size and zeroed_size of the destination when undoing the padding instead of relying on exfat_truncate(), which stops early after an error, and do not write any entry or FAT entry once the volume is in an emergency state.
- Send modify events for both files whenever the clusters moved, also when an error follows, and not for a source that was empty; update the times and the archive attribute of the emptied source; write it as an empty file only under its inode lock and while it is still empty.
Testing (linux-next 20261118 + this patch, arm64):
- Each change was first reproduced on v3 and then checked on v4, with images edited offline and test-only knobs as for v3:
- contiguous destination past the end of the volume: v3 wrote two FAT entries past the last cluster, v4 -EIO with nothing changed; corrupt chain layout: v3 orphaned the source's clusters, v4 -EIO;
- combining the other way round after a crash with the FAT or both entries updated, on a volume left dirty: v3 made a loop in the FAT, v4 -EUCLEAN with nothing changed; after fsck.exfat -y the call succeeds;
- store through a shared mapping into the padding, then writing back the source or the padding fails: v3 lost the stored data, v4 keeps it; if the destination cannot be written back at all, no stale data shows;
- source moved out of the bind mount before it is unlinked: v4 updates its mtime, sends one modify event per file, none for an empty source, and keeps data written to the source meanwhile.
- Functional tests on 4 KiB clusters, Landlock, permission, dead parent and mount point tests, the crash states of v3 and fstests exfat/001 and exfat/002 pass.
- 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 v4: the long-running tests reported for v2. Not tested:
the modify events after a failed flush at the end of Step 2.
- Known limit: if the dirty flag is cleared without running fsck.exfat, e.g. by another system, combining the files the other way round after a crash that updated only the FAT can still make a loop.
[1] https://sashiko.dev/#/patchset/20261123132742.743416-1-shaopei_1012%40163.com
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 | 641 ++++++++++++++++++ include/uapi/linux/exfat.h | 48 ++ 3 files changed, 690 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 +641 -0
661 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)
Quoted 5 lines · Shaopei Zhu
As with
EXT4_IOC_MOVE_EXTandF2FS_IOC_MOVE_RANGE, the source must be open for reading and writing, the destination for writing, and both must be regular files on the same mount. Files whose allocation, chain layout orvalid_sizeare inconsistent, as after a failed truncate or on a corrupt volume, are refused with-EIObefore anything is changed.
Why are you designing your own ioctl instead of implementing EXT4_IOC_MOVE_EXT in exfat? That's the normal way of doing things as it lets us later hoist the ioctl from all the filesystems implementing it to the VFS.
Quoted 11 lines · Matthew Wilcox
Quoted 5 lines · Shaopei Zhu
As with
EXT4_IOC_MOVE_EXTandF2FS_IOC_MOVE_RANGE, the source must be open for reading and writing, the destination for writing, and both must be regular files on the same mount. Files whose allocation, chain layout orvalid_sizeare inconsistent, as after a failed truncate or on a corrupt volume, are refused with-EIObefore anything is changed.Why are you designing your own ioctl instead of implementing
EXT4_IOC_MOVE_EXTin exfat? That's the normal way of doing things as it lets us later hoist the ioctl from all the filesystems implementing it to the VFS.
Hi Matthew,
Thanks for looking at this. That paragraph was badly worded: I only meant that the permission rules are the same as for those ioctls, not that the operation is similar.
EXT4_IOC_MOVE_EXT is a defragmentation primitive: it copies the data of the original file into the donor's blocks and exchanges the block mappings, so the contents and sizes of both files stay the same, and the range has to lie within both files. It cannot append one file to another, which is what is needed here: the clusters of the source are linked onto the end of the destination, the destination grows and the source is unlinked, without copying any data.
I will reword that paragraph in the next version so that it states the rules without suggesting that the operations are related.
Thanks,
Shaopei
Quoted 11 lines · Matthew Wilcox
Quoted 5 lines · Shaopei Zhu
As with
EXT4_IOC_MOVE_EXTandF2FS_IOC_MOVE_RANGE, the source must be open for reading and writing, the destination for writing, and both must be regular files on the same mount. Files whose allocation, chain layout orvalid_sizeare inconsistent, as after a failed truncate or on a corrupt volume, are refused with-EIObefore anything is changed.Why are you designing your own ioctl instead of implementing
EXT4_IOC_MOVE_EXTin exfat? That's the normal way of doing things as it lets us later hoist the ioctl from all the filesystems implementing it to the VFS.
Hi Matthew,
Thanks for looking at this. That paragraph was badly worded: I only meant that the permission rules are the same as for those ioctls, not that the operation is similar.
EXT4_IOC_MOVE_EXT is a defragmentation primitive: it copies the data of the original file into the donor's blocks and exchanges the block mappings, so the contents and sizes of both files stay the same, and the range has to lie within both files. It cannot append one file to another, which is what is needed here: the clusters of the source are linked onto the end of the destination, the destination grows and the source is unlinked, without copying any data.
I will reword that paragraph in the next version so that it states the rules without suggesting that the operations are related.
Thanks,
Shaopei