Re: [PATCH v7] exfat: add EXFAT_IOC_COMBINE to append a file's clusters to another
From: Shaopei Zhu
Date: Thu Nov 26 2026 - 10:27:32 EST
Hi Davo,
Thanks for taking a look, both here and at the exfatprogs pull request.
On 11/26/26 14:26, David Timber wrote:
>> A sidecar file is lost in practice: users copy, share or import only
>> the video file, and matching the two by name breaks once either is
>> renamed or moved.
> I think I understand the the problem being addressed here, but adding
> something to the kernel to solve that particular user error sounds like
> a lot of trouble to go through...
The remark about sidecar files in my previous mail was part of the
discussion with Namjae about whether the kernel or user space should
unlink the source: it explained why the sensor file should not be left
next to the video. Following Namjae's suggestion, v8 [1] leaves the
unlink to user space, so that point is settled, and it was never the
reason for the ioctl itself.
The reason for merging is that the camera records more than the video:
custom data such as GPS and per-frame exposure data, which is used in
post-processing. It is written to separate files while recording, but
the product has to hand the user a single file, so when recording stops
these files are merged into the main MP4 file. They grow with the
length of the recording, from tens to hundreds of MiB.
On an SD card, merging them by copying takes time proportional to their
size. With the write speed dropping to around 10 MB/s under jitter, a
few hundred MiB take tens of seconds, which users waiting for the
camera do not accept. Relinking the clusters makes the time depend on
the length of the FAT chain instead of 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:
source EXFAT_IOC_COMBINE copy_file_range() + fsync()
50 MiB 13.7 ms 2.2 s
500 MiB 14.3 ms 22.3 s
1 GiB 13.6 ms 45.7 s
The ioctl stays at 12 to 15 ms whatever the size; the time to build a
FAT chain for a contiguous file grows with its number of clusters, but
stays far below that of copying the data.
> Also, there's issue of block alignment: Windows formats exfat volumes up
> to 32MB cluster size(I myself unsure of this MS's decision, but it is
> what it is). The release of 4TB SDUC cards are just around the corner.
> At most 2MB of space lost just to padding, just to append a few MB of
> metadata to a monolithic file to idiotproof the product.
On the padding: it does not take any extra space on the volume, but it
is worth explaining why it is there at all.
Clusters can only be moved whole. The data of the source starts at the
beginning of its first cluster on disk, so once that cluster is linked
after the last cluster of the destination, the appended data starts at
the next cluster boundary of the destination, i.e. at
round_up(old size, cluster size). If the size of the destination is
not a multiple of the cluster size, the unused tail of its last cluster
ends up in the middle of the combined file.
That tail has never been written by the destination and may still hold
stale data, e.g. from a file deleted earlier, so it must not become
readable. exFAT can only mark a range as uninitialized at the end of a
file: each file has a single valid_size, and everything below it is
read from the disk. A gap in the middle cannot be described, so the
only way to keep the stale data out is to write zeroes into it. For
the same reason, any range of the destination below its size that was
never written, e.g. after fallocate(), is zeroed as well before the
clusters are appended.
The zeroes go into the tail of the last cluster, which is already
allocated to the destination. No cluster is allocated or freed, so the
combined file occupies exactly the clusters the two files occupied
before, whatever the cluster size.
What the padding does cost is that the gap becomes part of the file: up
to one cluster less one byte of zeroes is written, and the file format
has to skip it. A trailer located from the end of the file is not
affected. A writer that prefers its own padding, e.g. an MP4 'free'
box, can pad the destination up to the cluster boundary itself before
the call, and then nothing is zeroed. The cluster size is reported in
st_blksize.
> I think most of the products on the market today just use multiple files
> for multiple streams at different rate(LRV and WAV - fs/exfat have long
> supported preallocation to alleviate the classical fragmentation issue
> inherent in linked-list based filesystems).
[...]
> However,
> if you don't mind elaborating, I'd like to see some actual
> implementations in the wild that may benefit from the ioctl.
On implementations in the wild: a similar implementation, though not
this version, is already used in mature camera products. I would
rather not name the vendor for now, as that involves other matters, but
the use is real. The open source user is exfatcombine in the
exfatprogs pull request.
Products that deliver several files, like LRV proxies or separate
audio, do not need this, and preallocation already serves them well.
The ioctl is for the case where data written to separate files during
recording has to end up in one file, and where it is too large to copy
quickly, as described above.
> I also have
> a bunch of ideas I'd share with the kernel devs, but I just keep them
> all to myself because the kernel development is hard and the ideas are,
> most of the times, not worth the trouble of maintaining it in the long
> run. I don't think it's not fair for the maintainers who have to stay
> behind for years to maintain things.
On the maintenance burden: that is a fair concern, and it is the
maintainers' call to make. To keep it small, the change is
self-contained in fs/exfat/file.c: it touches neither the VFS nor the
on-disk format, and only uses structures exFAT already has. The
interface is a single ioctl with an 8-byte argument, whose flags field
is reserved for later extensions. v8 dropped the unlink from the
kernel, as Namjae suggested, which removed about 170 lines. It is
covered by a test for fstests (exfat/002), and exfatcombine exercises
it from user space.
Thanks for taking the time to write this; the feedback is welcome, and
I don't take it personally.
Shaopei
[1] https://lore.kernel.org/all/20261126125216.927053-1-shaopei_1012@xxxxxxx/