Re: [PATCH v13] exfat: add EXFAT_IOC_COMBINE to append a file's clusters to another
From: Namjae Jeon
Date: Sat Nov 28 2026 - 21:11:04 EST
> +static int __exfat_combine(struct inode *dst, struct inode *src,
> + unsigned int last_clu, errseq_t *meta_since)
> +{
> + struct super_block *sb = dst->i_sb;
> + struct exfat_sb_info *sbi = EXFAT_SB(sb);
> + struct exfat_inode_info *dst_ei = EXFAT_I(dst);
> + struct exfat_inode_info *src_ei = EXFAT_I(src);
> + loff_t dst_size = i_size_read(dst);
> + loff_t src_size = i_size_read(src);
> + loff_t new_valid_size;
> + unsigned int dst_clus, src_clus, dst_start, dst_start_old;
> + unsigned char dst_flags, dst_flags_old;
> + bool linked = false;
> + int err = 0, ret;
> +
> + dst_clus = exfat_bytes_to_cluster(sbi, exfat_ondisk_size(dst));
> + src_clus = exfat_bytes_to_cluster(sbi, exfat_ondisk_size(src));
> +
> + /* Ensured by exfat_ioctl_combine() and stable under its locks. */
> + if (exfat_cluster_to_bytes(sbi, dst_clus) != dst_size ||
> + (!dst_clus && dst_ei->start_clu != EXFAT_EOF_CLUSTER) ||
> + (dst_clus && dst_ei->start_clu == EXFAT_EOF_CLUSTER) ||
> + exfat_get_valid_size(dst_ei) != dst_size ||
> + src_ei->start_clu == EXFAT_EOF_CLUSTER ||
> + exfat_bytes_to_cluster_round_up(sbi, src_size) != src_clus)
> + return -EIO;
> +
> + /* Step 1: build the new chain of @dst in the FAT. */
> + mutex_lock(&sbi->s_lock);
> + err = exfat_emergency_state(sb);
> + if (err)
> + goto unlock;
> + /* A concurrent call may have failed since the caller checked. */
> + if (sbi->vol_flags_persistent & VOLUME_DIRTY) {
> + err = -EUCLEAN;
> + goto unlock;
> + }
> + exfat_set_volume_dirty(sb);
> + dst_start_old = dst_ei->start_clu;
> + dst_flags_old = dst_ei->flags;
> + dst_start = dst_start_old;
> + dst_flags = dst_flags_old;
> +
> + if (!dst_clus) {
> + dst_start = src_ei->start_clu;
> + dst_flags = src_ei->flags;
> + goto unlock;
> + }
> +
> + /*
> + * A contiguous @dst can stay contiguous only if @src is contiguous
> + * and starts right after it. Otherwise build its FAT chain first.
> + */
> + if (dst_flags == ALLOC_NO_FAT_CHAIN &&
> + (src_ei->flags != ALLOC_NO_FAT_CHAIN ||
> + src_ei->start_clu != last_clu + 1)) {
> + err = exfat_chain_cont_cluster(sb, dst_start, dst_clus);
> + if (err)
> + goto unlock;
> + dst_flags = ALLOC_FAT_CHAIN;
> + }
> +
> + if (dst_flags == ALLOC_FAT_CHAIN) {
> + if (src_ei->flags == ALLOC_NO_FAT_CHAIN) {
> + err = exfat_chain_cont_cluster(sb, src_ei->start_clu,
> + src_clus);
> + if (err)
> + goto unlock;
> + }
> + err = exfat_ent_set(sb, last_clu, src_ei->start_clu);
> + linked = !err;
> + }
> +unlock:
> + mutex_unlock(&sbi->s_lock);
> + if (err)
> + return err;
> +
> + err = exfat_combine_flush(sb, meta_since);
> + if (err)
> + goto undo;
> +
> + /*
> + * Step 2: publish the new size and chain of @dst. As the rest of
> + * exfat, write nothing more once the volume is in an emergency
> + * state, which leaves what a crash after step 1 leaves. If it
> + * enters that state after the entry of @dst is written, @src keeps
> + * its entry, as after a crash after step 2.
> + */
> + mutex_lock(&sbi->s_lock);
> + err = exfat_emergency_state(sb);
> + if (err) {
> + mutex_unlock(&sbi->s_lock);
> + goto undo;
> + }
> + dst_ei->start_clu = dst_start;
> + dst_ei->flags = dst_flags;
> + new_valid_size = dst_size + exfat_get_valid_size(src_ei);
> + dst->i_blocks = exfat_cluster_to_sectors(sbi, dst_clus + src_clus);
> + i_size_write(dst, dst_size + src_size);
> + exfat_set_valid_size(dst_ei, new_valid_size);
> + exfat_set_zeroed_size(dst_ei, new_valid_size);
> + dst_ei->attr |= EXFAT_ATTR_ARCHIVE;
> + inode_set_mtime_to_ts(dst, inode_set_ctime_current(dst));
> + exfat_combine_reset_hints(dst);
> + err = __exfat_write_inode(dst, 1);
> + if (err) {
> + /*
> + * The error is returned, mark it seen in the errseq of the
> + * block device mapping, which no flush checks now, so that it
> + * does not fail later calls. Its error flags are left alone:
> + * they are shared by every file on the volume.
> + */
> + errseq_check_and_advance(&sb->s_bdev->bd_mapping->wb_err,
> + meta_since);
> + /*
> + * Keep @src the only owner of its clusters: give @dst back its
> + * old state, which writeback retries to put on disk.
> + */
> + dst_ei->start_clu = dst_start_old;
> + dst_ei->flags = dst_flags_old;
> + dst->i_blocks = exfat_cluster_to_sectors(sbi, dst_clus);
> + i_size_write(dst, dst_size);
> + exfat_set_valid_size(dst_ei, dst_size);
> + exfat_set_zeroed_size(dst_ei, dst_size);
> + exfat_combine_reset_hints(dst);
> + mark_inode_dirty(dst);
> + }
> + mutex_unlock(&sbi->s_lock);
> + if (err)
> + goto undo;
> +
> + err = blkdev_issue_flush(sb->s_bdev);
Please return the error without updating the source entry when
blkdev_issue_flush() fails.
> +
> + /*
> + * Step 3: the clusters now belong to @dst. Write @src as an empty
> + * file. If the flush failed, the entry of @dst was still written,
> + * so do it anyway. If writing @src fails, its in-memory state no
> + * longer owns the clusters, so evicting it frees nothing, and
> + * writeback retries the entry; until then both entries reference
> + * the clusters, as after a crash.
> + */
> + mutex_lock(&sbi->s_lock);
> + src_ei->start_clu = EXFAT_EOF_CLUSTER;
> + src_ei->flags = ALLOC_NO_FAT_CHAIN;
> + src->i_blocks = 0;
> + i_size_write(src, 0);
> + exfat_set_valid_size(src_ei, 0);
> + exfat_set_zeroed_size(src_ei, 0);
> + exfat_combine_reset_hints(src);
> + src_ei->attr |= EXFAT_ATTR_ARCHIVE;
> + inode_set_mtime_to_ts(src, inode_set_ctime_current(src));
> + ret = exfat_emergency_state(sb);
> + if (!ret) {
> + ret = __exfat_write_inode(src, 1);
> + if (ret) /* as for @dst in step 2 */
> + errseq_check_and_advance(&sb->s_bdev->bd_mapping->wb_err,
> + meta_since);
> + }
> + if (ret) {
> + /*
> + * Both entries own the clusters on disk until @src is
> + * written, and freeing one file would free clusters of the
> + * other. Keep the volume dirty, also across unmount, and
> + * stop writing to it if the mount options say so.
> + */
> + sbi->vol_flags_persistent |= VOLUME_DIRTY;
> + exfat_fs_error(sb, "clusters are still referenced by two files");
> + mark_inode_dirty(src);
> + }
> + mutex_unlock(&sbi->s_lock);
> + return err ? err : ret;
> +
> +undo:
> + /*
> + * @dst does not own the clusters of @src, cut its chain again,
> + * unless the volume is in an emergency state.
> + */
> + if (linked) {
> + mutex_lock(&sbi->s_lock);
> + ret = exfat_emergency_state(sb);
> + if (!ret)
> + ret = exfat_ent_set(sb, last_clu, EXFAT_EOF_CLUSTER);
> + mutex_unlock(&sbi->s_lock);
> + /* That only changed the buffer: the link may be on disk. */
> + if (!ret)
> + ret = exfat_combine_flush(sb, meta_since);
> + if (ret) {
> + /*
> + * The chain of @dst still runs into the clusters of
> + * @src, which freeing @dst would free as well. Keep
> + * the volume dirty, also across unmount, and stop
> + * writing to it if the mount options say so.
> + */
> + mutex_lock(&sbi->s_lock);
> + sbi->vol_flags_persistent |= VOLUME_DIRTY;
> + exfat_fs_error(sb,
> + "cluster chain still linked to another file (last cluster 0x%08x)",
> + last_clu);
> + mutex_unlock(&sbi->s_lock);
> + }
> + }
> + return err;
> +}