Re: [PATCH] exfat: add EXFAT_IOC_COMBINE to append a file's clusters to another

From: Namjae Jeon

Date: Sat Nov 21 2026 - 19:18:21 EST


On Sun, Nov 22, 2026 at 2:43 AM Shaopei Zhu <shaopei_1012@xxxxxxx> wrote:
>
> 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() rewrites the whole payload, which is slow 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 the way btrfs_ioctl_snap_destroy() removes a
> subvolume: start_removing_dentry() revalidates the dentry of the open
> file and locks its parent, may_delete_dentry() checks the permission as
> unlink(2) does, the entry set is deleted by the same code as
> exfat_unlink(), and d_delete_notify() updates the dcache and notifies
> watchers, without re-entering exfat through vfs_unlink(). Since the
> in-memory inode no longer owns the clusters, evicting it frees nothing.
> The permission is checked before anything is changed, and an already
> unlinked source is accepted.
>
> 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.
>
> 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.
> If the call fails before the clusters are moved, the zero padding is
> removed again. 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.
>
> Signed-off-by: Shaopei Zhu <shaopei_1012@xxxxxxx>
First, Please check Sashiko's review comments.

https://sashiko.dev/#/patchset/20261121174315.500249-1-shaopei_1012%40163.com

And please send exfatcombine's PR to exfatprogs.

Thanks.