From nobody Mon May 25 05:12:30 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B4A5A1D5147; Mon, 18 May 2026 11:48:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779104917; cv=none; b=JT9Kv1myKk/i5uyzEfaYa8ClmT+FNXLMeLQ66JW7/1Se5v3KXbcJ2+pL3ThbBQdJYaT49eemHBDJrx6LRIZrsO9va4Kaw1sXs1Zt4HLLG7KMge5nwqwbM2ZyQM4OsPOC91oV80Ede5Xr66CKrenrp92loGAK9BK2MjJELZoCJsw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779104917; c=relaxed/simple; bh=Tznz0eNh767FZfB9iN4IZkkSAhVO25nd3+3iQePiJzo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=EnIsRnhgtp/sNxHbSH+ii4xTm87JZhTsaJe4UZR8kfZ/yUy28h96SQIoVOGdAzx06n7sj2YbeV7yXXCVN2TN2ZmqZGOt02rTZAuQbIlz6Kjp/SFjpG7mTmEVQy1Y9v0g/zk0KCgwuT3SPbMxaV1oTDtdGbphPIKNdPoAUCHsrIg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=hsfRIDFs; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="hsfRIDFs" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7D6A6C2BCB7; Mon, 18 May 2026 11:48:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1779104917; bh=Tznz0eNh767FZfB9iN4IZkkSAhVO25nd3+3iQePiJzo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=hsfRIDFsD/jWaT83GnZP0NxG8BDbyUwH56V/qhboZEjteIAe7Cqr77XGo7ga01V1w uC8Ks1/4eaGIV4mrw19LsV3HZNE/6EFNmpGZwPb1VNlKGYjfBRWD+6rz0hr2gLOT5I se72mCbJYRsQ1SWKmrmj5NXhQgxuOeLE4niZefqqGbXz/QxZeC9mKnWE3BOS66Vj6q Mb8RG8hhkeRjEuI34j/giJUxyc4VN6r2nWAD7a5mnu4OmS8r3H0xeWp/Rf0NkYsQul jnp3Z3cvfFEDbFllLUUHmzGlopuaLhyV67cQQ7wU+J84neSIywZcEENRJ3TP/v2ARY a1u0Qo0r7aOkQ== From: Namjae Jeon To: sj1557.seo@samsung.com, yuezhang.mo@sony.com, brauner@kernel.org, djwong@kernel.org, hch@lst.de Cc: linux-fsdevel@vger.kernel.org, anmuxixixi@gmail.com, dxdt@dev.snart.me, chizhiling@kylinos.cn, chizhiling@163.com, linux-kernel@vger.kernel.org, charsyam@gmail.com, Namjae Jeon Subject: [PATCH v4 01/11] iomap: introduce IOMAP_F_ZERO_TAIL flag Date: Mon, 18 May 2026 20:46:55 +0900 Message-Id: <20260518114705.9601-2-linkinjeon@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20260518114705.9601-1-linkinjeon@kernel.org> References: <20260518114705.9601-1-linkinjeon@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" In filesystems that maintain a separate Valid Data Length, such as exFAT and NTFS, a partial write may start at or beyond the current valid_size and extend it. In this case, the region after the previous valid_size but within the same filesystem block is considered unwritten. This patch introduces IOMAP_F_ZERO_TAIL. When this flag is set in iomap, __iomap_write_begin() will zero only the tail portion while preserving any valid data before it in the same block. Without this tail zeroing, stale data in the unwritten portion of the block can remain in the page cache. Subsequent reads can then return incorrect contents from that region. Acked-by: Christoph Hellwig Signed-off-by: Namjae Jeon Acked-by: "Darrick J. Wong" Reviewed-by: "Darrick J. Wong" --- fs/iomap/buffered-io.c | 4 ++++ include/linux/iomap.h | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/fs/iomap/buffered-io.c b/fs/iomap/buffered-io.c index d7b648421a70..44046c648df4 100644 --- a/fs/iomap/buffered-io.c +++ b/fs/iomap/buffered-io.c @@ -836,6 +836,7 @@ static int __iomap_write_begin(const struct iomap_iter = *iter, return -EIO; folio_zero_segments(folio, poff, from, to, poff + plen); } else { + const struct iomap *iomap =3D iomap_iter_srcmap(iter); int status; =20 if (iter->flags & IOMAP_NOWAIT) @@ -853,6 +854,9 @@ static int __iomap_write_begin(const struct iomap_iter = *iter, len, status, GFP_NOFS); if (status) return status; + + if (iomap->flags & IOMAP_F_ZERO_TAIL) + folio_zero_segment(folio, to, poff + plen); } iomap_set_range_uptodate(folio, poff, plen); } while ((block_start +=3D plen) < block_end); diff --git a/include/linux/iomap.h b/include/linux/iomap.h index 2c5685adf3a9..750602e18750 100644 --- a/include/linux/iomap.h +++ b/include/linux/iomap.h @@ -67,6 +67,9 @@ struct vm_fault; * bio, i.e. set REQ_ATOMIC. * * IOMAP_F_INTEGRITY indicates that the filesystems handles integrity meta= data. + * + * IOMAP_F_ZERO_TAIL indicates the remainder of the block after the data + * written should be zeroed. */ #define IOMAP_F_NEW (1U << 0) #define IOMAP_F_DIRTY (1U << 1) @@ -86,6 +89,7 @@ struct vm_fault; #else #define IOMAP_F_INTEGRITY 0 #endif /* CONFIG_BLK_DEV_INTEGRITY */ +#define IOMAP_F_ZERO_TAIL (1U << 10) =20 /* * Flag reserved for file system specific usage --=20 2.25.1 From nobody Mon May 25 05:12:30 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6ABE73F787A; Mon, 18 May 2026 11:48:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779104921; cv=none; b=Fpea9GX/wGS+mw8v0VtIxNnypI6K37UYpmmUicsFgiPBkZXhvg8vJsjkpPvBS69LgF+KeEbsghwIzN1uQPA+nP3rykCV20neRhFKqOV47BTH/CyQiXICZsBNyNEMHVhJwQtDUDCzyifxSeixUTEw8YxzqXc+YuCQxYfKi8Yb3Hg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779104921; c=relaxed/simple; bh=HpLATnmuruuoytqsqGQPcLDKW8Bu48YzPOmGDsePKL8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=s4j7Z20O5i+lCXlsPK62tIykWAWcCzRC0uwYSgPJQehbd0Id+1iznESw6kQnkr8KDLw5O31aw7zWE8ICf+AEN6y0RU2uu/Bd8MPKlsRVEHpC7PAN8kXVKLcVI7CZXxWAOMTy2m+A1RtZiimjY6Mr+1oU2JfHzQ5I1+nfrPJH2mY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=LLDwVgkd; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="LLDwVgkd" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 333CAC2BCC6; Mon, 18 May 2026 11:48:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1779104921; bh=HpLATnmuruuoytqsqGQPcLDKW8Bu48YzPOmGDsePKL8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=LLDwVgkdTsUerFTxGOWMrABi36kPthkQ/XioNwX6aPuKVp8rVsJdY3IzobgVlFw6g vKZZAIeWe/aNCjUGq4ALnD5nUgVoSHfbgHNXw370cfD7kO1PunEbpdQtb2YX+cPi7/ NJ0Uz5p2PyaGPl+8MGX0xHbRSNgysOm0Te3amYhry5QF6SJta8IxSPkJAQNg/vVWFz xUMrwhvvhkcCF2YGmnuoxYVouoFEaPaKEcO1gSvu0QdkveMrqwAXgcZ6MJ3fiDBwIZ oGXAq+MfFK6rvDYVNzczq7xm74OHg67QI3+v364Mm849tpNZH4PTpuDoiSIdphg/Hd GuduFJsbGga+A== From: Namjae Jeon To: sj1557.seo@samsung.com, yuezhang.mo@sony.com, brauner@kernel.org, djwong@kernel.org, hch@lst.de Cc: linux-fsdevel@vger.kernel.org, anmuxixixi@gmail.com, dxdt@dev.snart.me, chizhiling@kylinos.cn, chizhiling@163.com, linux-kernel@vger.kernel.org, charsyam@gmail.com, Namjae Jeon Subject: [PATCH v4 02/11] exfat: replace unsafe macros with static inline functions Date: Mon, 18 May 2026 20:46:56 +0900 Message-Id: <20260518114705.9601-3-linkinjeon@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20260518114705.9601-1-linkinjeon@kernel.org> References: <20260518114705.9601-1-linkinjeon@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The current exFAT driver relies on various macros for unit conversions between clusters, blocks, sectors, and directory entries. These macros are structurally unsafe as they lack type enforcement and are prone to potential integer overflows during bit-shift operations, especially on 64-bit architectures. Replace all arithmetic macros with static inline functions to provide strict type checking and explicit casting. Acked-by: Christoph Hellwig Signed-off-by: Namjae Jeon Acked-by: "Darrick J. Wong" --- fs/exfat/balloc.c | 2 +- fs/exfat/dir.c | 48 ++++++++--------- fs/exfat/exfat_fs.h | 122 ++++++++++++++++++++++++++++++++------------ fs/exfat/fatent.c | 4 +- fs/exfat/file.c | 8 +-- fs/exfat/inode.c | 19 +++---- fs/exfat/namei.c | 26 +++++----- fs/exfat/super.c | 4 +- 8 files changed, 147 insertions(+), 86 deletions(-) diff --git a/fs/exfat/balloc.c b/fs/exfat/balloc.c index 625f2f14d4fe..e66ebf899778 100644 --- a/fs/exfat/balloc.c +++ b/fs/exfat/balloc.c @@ -112,7 +112,7 @@ static int exfat_allocate_bitmap(struct super_block *sb, } =20 if (exfat_test_bitmap_range(sb, sbi->map_clu, - EXFAT_B_TO_CLU_ROUND_UP(map_size, sbi)) =3D=3D false) + exfat_bytes_to_cluster_round_up(sbi, map_size)) =3D=3D false) goto err_out; =20 return 0; diff --git a/fs/exfat/dir.c b/fs/exfat/dir.c index ac008ccaa97d..ca9d707220df 100644 --- a/fs/exfat/dir.c +++ b/fs/exfat/dir.c @@ -76,7 +76,7 @@ static int exfat_readdir(struct inode *inode, loff_t *cpo= s, struct exfat_dir_ent struct super_block *sb =3D inode->i_sb; struct exfat_sb_info *sbi =3D EXFAT_SB(sb); struct exfat_inode_info *ei =3D EXFAT_I(inode); - unsigned int dentry =3D EXFAT_B_TO_DEN(*cpos) & 0xFFFFFFFF; + unsigned int dentry =3D exfat_bytes_to_dentries(*cpos) & 0xFFFFFFFF; struct buffer_head *bh; =20 /* check if the given file ID is opened */ @@ -84,13 +84,13 @@ static int exfat_readdir(struct inode *inode, loff_t *c= pos, struct exfat_dir_ent return -EPERM; =20 exfat_chain_set(&dir, ei->start_clu, - EXFAT_B_TO_CLU(i_size_read(inode), sbi), ei->flags); + exfat_bytes_to_cluster(sbi, i_size_read(inode)), ei->flags); =20 dentries_per_clu =3D sbi->dentries_per_clu; - max_dentries =3D (unsigned int)min_t(u64, MAX_EXFAT_DENTRIES, - (u64)EXFAT_CLU_TO_DEN(sbi->num_clusters, sbi)); + max_dentries =3D min(MAX_EXFAT_DENTRIES, + exfat_cluster_to_dentries(sbi, sbi->num_clusters)); =20 - clu_offset =3D EXFAT_DEN_TO_CLU(dentry, sbi); + clu_offset =3D exfat_dentries_to_cluster(sbi, dentry); exfat_chain_dup(&clu, &dir); =20 if (clu.flags =3D=3D ALLOC_FAT_CHAIN) { @@ -147,10 +147,10 @@ static int exfat_readdir(struct inode *inode, loff_t = *cpos, struct exfat_dir_ent dir_entry->dir =3D clu; brelse(bh); =20 - ei->hint_bmap.off =3D EXFAT_DEN_TO_CLU(dentry, sbi); + ei->hint_bmap.off =3D exfat_dentries_to_cluster(sbi, dentry); ei->hint_bmap.clu =3D clu.dir; =20 - *cpos =3D EXFAT_DEN_TO_B(dentry + 1 + num_ext); + *cpos =3D exfat_dentries_to_bytes(dentry + 1 + num_ext); return 0; } =20 @@ -160,7 +160,7 @@ static int exfat_readdir(struct inode *inode, loff_t *c= pos, struct exfat_dir_ent =20 out: dir_entry->namebuf.lfn[0] =3D '\0'; - *cpos =3D EXFAT_DEN_TO_B(dentry); + *cpos =3D exfat_dentries_to_bytes(dentry); return 0; } =20 @@ -465,7 +465,7 @@ static void exfat_free_benign_secondary_clusters(struct= inode *inode, return; =20 exfat_chain_set(&dir, start_clu, - EXFAT_B_TO_CLU_ROUND_UP(size, EXFAT_SB(sb)), + exfat_bytes_to_cluster_round_up(EXFAT_SB(sb), size), flags); exfat_free_cluster(inode, &dir); } @@ -556,10 +556,11 @@ static int exfat_find_location(struct super_block *sb= , struct exfat_chain *p_dir unsigned int off, clu =3D 0; struct exfat_sb_info *sbi =3D EXFAT_SB(sb); =20 - off =3D EXFAT_DEN_TO_B(entry); + off =3D exfat_dentries_to_bytes(entry); =20 clu =3D p_dir->dir; - ret =3D exfat_cluster_walk(sb, &clu, EXFAT_B_TO_CLU(off, sbi), p_dir->fla= gs); + ret =3D exfat_cluster_walk(sb, &clu, exfat_bytes_to_cluster(sbi, off), + p_dir->flags); if (ret) return ret; =20 @@ -567,7 +568,7 @@ static int exfat_find_location(struct super_block *sb, = struct exfat_chain *p_dir exfat_fs_error(sb, "unexpected early break in cluster chain (clu : %u, len : %d)", p_dir->dir, - EXFAT_B_TO_CLU(off, sbi)); + exfat_bytes_to_cluster(sbi, off)); return -EIO; } =20 @@ -577,13 +578,13 @@ static int exfat_find_location(struct super_block *sb= , struct exfat_chain *p_dir } =20 /* byte offset in cluster */ - off =3D EXFAT_CLU_OFFSET(off, sbi); + off =3D exfat_cluster_offset(sbi, off); =20 /* byte offset in sector */ - *offset =3D EXFAT_BLK_OFFSET(off, sb); + *offset =3D exfat_block_offset(sb, off); =20 /* sector offset in cluster */ - *sector =3D EXFAT_B_TO_BLK(off, sb); + *sector =3D exfat_bytes_to_block(sb, off); *sector +=3D exfat_cluster_to_sector(sbi, clu); return 0; } @@ -593,7 +594,7 @@ struct exfat_dentry *exfat_get_dentry(struct super_bloc= k *sb, { struct exfat_sb_info *sbi =3D EXFAT_SB(sb); unsigned int sect_per_clus =3D sbi->sect_per_clus; - unsigned int dentries_per_page =3D EXFAT_B_TO_DEN(PAGE_SIZE); + unsigned int dentries_per_page =3D exfat_bytes_to_dentries(PAGE_SIZE); int off; sector_t sec; =20 @@ -672,8 +673,8 @@ struct exfat_dentry *exfat_get_dentry_cached( struct exfat_entry_set_cache *es, int num) { int off =3D es->start_off + num * DENTRY_SIZE; - struct buffer_head *bh =3D es->bh[EXFAT_B_TO_BLK(off, es->sb)]; - char *p =3D bh->b_data + EXFAT_BLK_OFFSET(off, es->sb); + struct buffer_head *bh =3D es->bh[exfat_bytes_to_block(es->sb, off)]; + char *p =3D bh->b_data + exfat_block_offset(es->sb, off); =20 return (struct exfat_dentry *)p; } @@ -741,7 +742,7 @@ static int __exfat_get_dentry_set(struct exfat_entry_se= t_cache *es, =20 es->num_entries =3D num_entries; =20 - num_bh =3D EXFAT_B_TO_BLK_ROUND_UP(off + num_entries * DENTRY_SIZE, sb); + num_bh =3D exfat_bytes_to_block_round_up(sb, off + num_entries * DENTRY_S= IZE); if (num_bh > ARRAY_SIZE(es->__bh)) { es->bh =3D kmalloc_objs(*es->bh, num_bh, GFP_NOFS); if (!es->bh) { @@ -830,7 +831,7 @@ static int exfat_validate_empty_dentry_set(struct exfat= _entry_set_cache *es) =20 err_used_follow_unused: off =3D es->start_off + (i << DENTRY_SIZE_BITS); - bh =3D es->bh[EXFAT_B_TO_BLK(off, es->sb)]; + bh =3D es->bh[exfat_bytes_to_block(es->sb, off)]; =20 exfat_fs_error(es->sb, "in sector %lld, dentry %d should be unused, but 0x%x", @@ -839,7 +840,8 @@ static int exfat_validate_empty_dentry_set(struct exfat= _entry_set_cache *es) return -EIO; =20 count_skip_entries: - es->num_entries =3D EXFAT_B_TO_DEN(EXFAT_BLK_TO_B(es->num_bh, es->sb) - e= s->start_off); + es->num_entries =3D + exfat_bytes_to_dentries(exfat_block_to_bytes(es->sb, es->num_bh) - es->s= tart_off); for (; i < es->num_entries; i++) { ep =3D exfat_get_dentry_cached(es, i); if (IS_EXFAT_DELETED(ep->type)) @@ -892,7 +894,7 @@ static inline void exfat_set_empty_hint(struct exfat_in= ode_info *ei, { if (ei->hint_femp.eidx =3D=3D EXFAT_HINT_NONE || ei->hint_femp.eidx > dentry) { - int total_entries =3D EXFAT_B_TO_DEN(i_size_read(&ei->vfs_inode)); + int total_entries =3D exfat_bytes_to_dentries(i_size_read(&ei->vfs_inode= )); =20 if (candi_empty->count =3D=3D 0) { candi_empty->cur =3D *clu; @@ -1215,7 +1217,7 @@ static int exfat_get_volume_label_dentry(struct super= _block *sb, es->bh =3D es->__bh; es->bh[0] =3D bh; es->num_bh =3D 1; - es->start_off =3D EXFAT_DEN_TO_B(i) % sb->s_blocksize; + es->start_off =3D exfat_dentries_to_bytes(i) % sb->s_blocksize; =20 return 0; } diff --git a/fs/exfat/exfat_fs.h b/fs/exfat/exfat_fs.h index 89ef5368277f..9c8ab3df7a42 100644 --- a/fs/exfat/exfat_fs.h +++ b/fs/exfat/exfat_fs.h @@ -84,38 +84,6 @@ enum { (min_t(blkcnt_t, (sb)->s_bdi->ra_pages, (sb)->s_bdi->io_pages) \ << (PAGE_SHIFT - (sb)->s_blocksize_bits)) =20 -/* - * helpers for cluster size to byte conversion. - */ -#define EXFAT_CLU_TO_B(b, sbi) ((b) << (sbi)->cluster_size_bits) -#define EXFAT_B_TO_CLU(b, sbi) ((b) >> (sbi)->cluster_size_bits) -#define EXFAT_B_TO_CLU_ROUND_UP(b, sbi) \ - (((b - 1) >> (sbi)->cluster_size_bits) + 1) -#define EXFAT_CLU_OFFSET(off, sbi) ((off) & ((sbi)->cluster_size - 1)) - -/* - * helpers for block size to byte conversion. - */ -#define EXFAT_BLK_TO_B(b, sb) ((b) << (sb)->s_blocksize_bits) -#define EXFAT_B_TO_BLK(b, sb) ((b) >> (sb)->s_blocksize_bits) -#define EXFAT_B_TO_BLK_ROUND_UP(b, sb) \ - (((b - 1) >> (sb)->s_blocksize_bits) + 1) -#define EXFAT_BLK_OFFSET(off, sb) ((off) & ((sb)->s_blocksize - 1)) - -/* - * helpers for block size to dentry size conversion. - */ -#define EXFAT_B_TO_DEN(b) ((b) >> DENTRY_SIZE_BITS) -#define EXFAT_DEN_TO_B(b) ((b) << DENTRY_SIZE_BITS) - -/* - * helpers for cluster size to dentry size conversion. - */ -#define EXFAT_CLU_TO_DEN(clu, sbi) \ - ((clu) << ((sbi)->cluster_size_bits - DENTRY_SIZE_BITS)) -#define EXFAT_DEN_TO_CLU(dentry, sbi) \ - ((dentry) >> ((sbi)->cluster_size_bits - DENTRY_SIZE_BITS)) - /* * helpers for fat entry. */ @@ -149,7 +117,7 @@ enum { * The 608 bytes are in 3 sectors at most (even 512 Byte sector). */ #define DIR_CACHE_SIZE \ - (DIV_ROUND_UP(EXFAT_DEN_TO_B(ES_MAX_ENTRY_NUM), SECTOR_SIZE) + 1) + (DIV_ROUND_UP(ES_MAX_ENTRY_NUM << DENTRY_SIZE_BITS, SECTOR_SIZE) + 1) =20 /* Superblock flags */ #define EXFAT_FLAGS_SHUTDOWN 1 @@ -432,6 +400,94 @@ static inline loff_t exfat_ondisk_size(const struct in= ode *inode) return ((loff_t)inode->i_blocks) << 9; } =20 +/* + * helpers for cluster size to byte conversion. + */ +static inline loff_t exfat_cluster_to_bytes(struct exfat_sb_info *sbi, + u32 nr_clusters) +{ + return (loff_t)nr_clusters << sbi->cluster_size_bits; +} + +static inline blkcnt_t exfat_cluster_to_sectors(struct exfat_sb_info *sbi, + u32 nr_clusters) +{ + return (blkcnt_t)nr_clusters << (sbi->cluster_size_bits - 9); +} + +static inline u32 exfat_bytes_to_cluster(struct exfat_sb_info *sbi, loff_t= size) +{ + return (u32)(size >> sbi->cluster_size_bits); +} + +static inline u32 exfat_bytes_to_cluster_round_up(struct exfat_sb_info *sb= i, + loff_t size) +{ + if (size <=3D 0) + return 0; + return (u32)((size - 1) >> sbi->cluster_size_bits) + 1; +} + +static inline u32 exfat_cluster_offset(struct exfat_sb_info *sbi, loff_t o= ff) +{ + return off & (sbi->cluster_size - 1); +} + +/* + * helpers for block size to byte conversion. + */ +static inline loff_t exfat_block_to_bytes(struct super_block *sb, + sector_t block) +{ + return (loff_t)block << sb->s_blocksize_bits; +} + +static inline sector_t exfat_bytes_to_block(struct super_block *sb, loff_t= size) +{ + return (sector_t)(size >> sb->s_blocksize_bits); +} + +static inline sector_t exfat_bytes_to_block_round_up(struct super_block *s= b, + loff_t size) +{ + if (size <=3D 0) + return 0; + return (sector_t)(((size - 1) >> sb->s_blocksize_bits) + 1); +} + +static inline u32 exfat_block_offset(struct super_block *sb, loff_t off) +{ + return (u32)(off & (sb->s_blocksize - 1)); +} + +/* + * helpers for block size to dentry size conversion. + */ +static inline u32 exfat_bytes_to_dentries(loff_t b) +{ + return (u32)(b >> DENTRY_SIZE_BITS); +} + +static inline u32 exfat_dentries_to_bytes(u32 dentry) +{ + return dentry << DENTRY_SIZE_BITS; +} + +/* + * helpers for cluster size to dentry size conversion. + */ +static inline u32 exfat_cluster_to_dentries(struct exfat_sb_info *sbi, + u32 nr_clusters) +{ + return nr_clusters << (sbi->cluster_size_bits - DENTRY_SIZE_BITS); +} + +static inline u32 exfat_dentries_to_cluster(struct exfat_sb_info *sbi, + u32 dentry) +{ + return dentry >> (sbi->cluster_size_bits - DENTRY_SIZE_BITS); +} + /* super.c */ int exfat_set_volume_dirty(struct super_block *sb); int exfat_clear_volume_dirty(struct super_block *sb); diff --git a/fs/exfat/fatent.c b/fs/exfat/fatent.c index dce0955e689a..45b0b754a2e4 100644 --- a/fs/exfat/fatent.c +++ b/fs/exfat/fatent.c @@ -412,8 +412,8 @@ int exfat_zeroed_cluster(struct inode *dir, unsigned in= t clu) =20 if (IS_DIRSYNC(dir)) return sync_blockdev_range(sb->s_bdev, - EXFAT_BLK_TO_B(blknr, sb), - EXFAT_BLK_TO_B(last_blknr, sb) - 1); + exfat_block_to_bytes(sb, blknr), + exfat_block_to_bytes(sb, last_blknr) - 1); =20 return 0; } diff --git a/fs/exfat/file.c b/fs/exfat/file.c index 354bdcfe4abc..29a36a80e29b 100644 --- a/fs/exfat/file.c +++ b/fs/exfat/file.c @@ -33,9 +33,9 @@ static int exfat_cont_expand(struct inode *inode, loff_t = size) if (ret) return ret; =20 - num_clusters =3D EXFAT_B_TO_CLU(exfat_ondisk_size(inode), sbi); + num_clusters =3D exfat_bytes_to_cluster(sbi, exfat_ondisk_size(inode)); /* integer overflow is already checked in inode_newsize_ok(). */ - new_num_clusters =3D EXFAT_B_TO_CLU_ROUND_UP(size, sbi); + new_num_clusters =3D exfat_bytes_to_cluster_round_up(sbi, size); =20 if (new_num_clusters =3D=3D num_clusters) goto out; @@ -200,8 +200,8 @@ int __exfat_truncate(struct inode *inode) =20 exfat_set_volume_dirty(sb); =20 - num_clusters_new =3D EXFAT_B_TO_CLU_ROUND_UP(i_size_read(inode), sbi); - num_clusters_phys =3D EXFAT_B_TO_CLU(exfat_ondisk_size(inode), sbi); + num_clusters_new =3D exfat_bytes_to_cluster_round_up(sbi, i_size_read(ino= de)); + num_clusters_phys =3D exfat_bytes_to_cluster(sbi, exfat_ondisk_size(inode= )); =20 exfat_chain_set(&clu, ei->start_clu, num_clusters_phys, ei->flags); =20 diff --git a/fs/exfat/inode.c b/fs/exfat/inode.c index 1ea4c740fef9..249a35e2b4b2 100644 --- a/fs/exfat/inode.c +++ b/fs/exfat/inode.c @@ -135,7 +135,7 @@ static int exfat_map_cluster(struct inode *inode, unsig= ned int clu_offset, unsigned int local_clu_offset =3D clu_offset; unsigned int num_to_be_allocated =3D 0, num_clusters; =20 - num_clusters =3D EXFAT_B_TO_CLU(exfat_ondisk_size(inode), sbi); + num_clusters =3D exfat_bytes_to_cluster(sbi, exfat_ondisk_size(inode)); =20 if (clu_offset >=3D num_clusters) num_to_be_allocated =3D clu_offset - num_clusters + 1; @@ -216,7 +216,8 @@ static int exfat_map_cluster(struct inode *inode, unsig= ned int clu_offset, =20 *clu =3D new_clu.dir; =20 - inode->i_blocks +=3D EXFAT_CLU_TO_B(num_to_be_allocated, sbi) >> 9; + inode->i_blocks +=3D + exfat_cluster_to_sectors(sbi, num_to_be_allocated); =20 /* * Move *clu pointer along FAT chains (hole care) because the @@ -254,12 +255,12 @@ static int exfat_get_block(struct inode *inode, secto= r_t iblock, =20 mutex_lock(&sbi->s_lock); i_size =3D i_size_read(inode); - last_block =3D EXFAT_B_TO_BLK_ROUND_UP(i_size, sb); + last_block =3D exfat_bytes_to_block_round_up(sb, i_size); if (iblock >=3D last_block && !create) goto done; =20 /* Is this block already allocated? */ - count =3D EXFAT_B_TO_CLU_ROUND_UP(bh_result->b_size, sbi); + count =3D exfat_bytes_to_cluster_round_up(sbi, bh_result->b_size); err =3D exfat_map_cluster(inode, iblock >> sbi->sect_per_clus_bits, &cluster, &count, create); if (err) { @@ -296,9 +297,9 @@ static int exfat_get_block(struct inode *inode, sector_= t iblock, * care the last nested block if valid_size is not equal to i_size. */ if (i_size =3D=3D ei->valid_size || create || !bh_result->b_folio) - valid_blks =3D EXFAT_B_TO_BLK_ROUND_UP(ei->valid_size, sb); + valid_blks =3D exfat_bytes_to_block_round_up(sb, ei->valid_size); else - valid_blks =3D EXFAT_B_TO_BLK(ei->valid_size, sb); + valid_blks =3D exfat_bytes_to_block(sb, ei->valid_size); =20 /* The range has been fully written, map it */ if (iblock + max_blocks < valid_blks) @@ -313,7 +314,7 @@ static int exfat_get_block(struct inode *inode, sector_= t iblock, /* The area has not been written, map and mark as new for create case */ if (create) { set_buffer_new(bh_result); - ei->valid_size =3D EXFAT_BLK_TO_B(iblock + max_blocks, sb); + ei->valid_size =3D exfat_block_to_bytes(sb, iblock + max_blocks); mark_inode_dirty(inode); goto done; } @@ -343,7 +344,7 @@ static int exfat_get_block(struct inode *inode, sector_= t iblock, goto done; } =20 - pos =3D EXFAT_BLK_TO_B(iblock, sb); + pos =3D exfat_block_to_bytes(sb, iblock); size =3D ei->valid_size - pos; addr =3D folio_address(bh_result->b_folio) + offset_in_folio(bh_result->b_folio, pos); @@ -374,7 +375,7 @@ static int exfat_get_block(struct inode *inode, sector_= t iblock, */ clear_buffer_mapped(bh_result); done: - bh_result->b_size =3D EXFAT_BLK_TO_B(max_blocks, sb); + bh_result->b_size =3D exfat_block_to_bytes(sb, max_blocks); if (err < 0) clear_buffer_mapped(bh_result); unlock_ret: diff --git a/fs/exfat/namei.c b/fs/exfat/namei.c index 2c5636634b4a..752fbec9316b 100644 --- a/fs/exfat/namei.c +++ b/fs/exfat/namei.c @@ -208,7 +208,7 @@ static int exfat_search_empty_slot(struct super_block *= sb, int dentries_per_clu; struct exfat_chain clu; struct exfat_sb_info *sbi =3D EXFAT_SB(sb); - int total_entries =3D EXFAT_CLU_TO_DEN(p_dir->size, sbi); + unsigned int total_entries =3D exfat_cluster_to_dentries(sbi, p_dir->size= ); =20 dentries_per_clu =3D sbi->dentries_per_clu; =20 @@ -266,7 +266,7 @@ static int exfat_search_empty_slot(struct super_block *= sb, =20 static int exfat_check_max_dentries(struct inode *inode) { - if (EXFAT_B_TO_DEN(i_size_read(inode)) >=3D MAX_EXFAT_DENTRIES) { + if (exfat_bytes_to_dentries(i_size_read(inode)) >=3D MAX_EXFAT_DENTRIES) { /* * exFAT spec allows a dir to grow up to 8388608(256MB) * dentries @@ -314,7 +314,8 @@ int exfat_find_empty_entry(struct inode *inode, } =20 exfat_chain_set(p_dir, ei->start_clu, - EXFAT_B_TO_CLU(i_size_read(inode), sbi), ei->flags); + exfat_bytes_to_cluster(sbi, i_size_read(inode)), + ei->flags); =20 while ((dentry =3D exfat_search_empty_slot(sb, &hint_femp, p_dir, num_entries, es)) < 0) { @@ -375,7 +376,7 @@ int exfat_find_empty_entry(struct inode *inode, =20 hint_femp.cur.size++; p_dir->size++; - size =3D EXFAT_CLU_TO_B(p_dir->size, sbi); + size =3D exfat_cluster_to_bytes(sbi, p_dir->size); =20 /* directory inode should be updated in here */ i_size_write(inode, size); @@ -604,7 +605,7 @@ static int exfat_find(struct inode *dir, const struct q= str *qname, return ret; =20 exfat_chain_set(&cdir, ei->start_clu, - EXFAT_B_TO_CLU(i_size_read(dir), sbi), ei->flags); + exfat_bytes_to_cluster(sbi, i_size_read(dir)), ei->flags); =20 /* check the validation of hint_stat and initialize it if required */ if (ei->version !=3D (inode_peek_iversion_raw(dir) & 0xffffffff)) { @@ -681,7 +682,7 @@ static int exfat_find(struct inode *dir, const struct q= str *qname, return -EIO; } =20 - if (unlikely(EXFAT_B_TO_CLU_ROUND_UP(info->size, sbi) > sbi->used_cluster= s)) { + if (unlikely(exfat_bytes_to_cluster_round_up(sbi, info->size) > sbi->used= _clusters)) { exfat_fs_error(sb, "data size is invalid(%lld)", info->size); return -EIO; } @@ -695,7 +696,8 @@ static int exfat_find(struct inode *dir, const struct q= str *qname, =20 if (info->type =3D=3D TYPE_DIR) { exfat_chain_set(&cdir, info->start_clu, - EXFAT_B_TO_CLU(info->size, sbi), info->flags); + exfat_bytes_to_cluster(sbi, info->size), + info->flags); count =3D exfat_count_dir_entries(sb, &cdir); if (count < 0) return -EIO; @@ -951,7 +953,7 @@ static int exfat_rmdir(struct inode *dir, struct dentry= *dentry) } =20 exfat_chain_set(&clu_to_free, ei->start_clu, - EXFAT_B_TO_CLU_ROUND_UP(i_size_read(inode), sbi), ei->flags); + exfat_bytes_to_cluster_round_up(sbi, i_size_read(inode)), ei->flags); =20 err =3D exfat_check_dir_empty(sb, &clu_to_free); if (err) { @@ -1158,8 +1160,8 @@ static int __exfat_rename(struct inode *old_parent_in= ode, =20 new_clu.dir =3D new_ei->start_clu; new_clu.size =3D - EXFAT_B_TO_CLU_ROUND_UP(i_size_read(new_inode), - sbi); + exfat_bytes_to_cluster_round_up(sbi, + i_size_read(new_inode)); new_clu.flags =3D new_ei->flags; =20 ret =3D exfat_check_dir_empty(sb, &new_clu); @@ -1203,8 +1205,8 @@ static int __exfat_rename(struct inode *old_parent_in= ode, struct exfat_chain new_clu_to_free; =20 exfat_chain_set(&new_clu_to_free, new_ei->start_clu, - EXFAT_B_TO_CLU_ROUND_UP(i_size_read(new_inode), - sbi), new_ei->flags); + exfat_bytes_to_cluster_round_up(sbi, i_size_read(new_inode)), + new_ei->flags); =20 if (exfat_free_cluster(new_inode, &new_clu_to_free)) { /* just set I/O error only */ diff --git a/fs/exfat/super.c b/fs/exfat/super.c index 95d87e2d7717..cb2f8eefff99 100644 --- a/fs/exfat/super.c +++ b/fs/exfat/super.c @@ -369,7 +369,7 @@ static int exfat_read_root(struct inode *inode, struct = exfat_chain *root_clu) ei->hint_stat.clu =3D sbi->root_dir; ei->hint_femp.eidx =3D EXFAT_HINT_NONE; =20 - i_size_write(inode, EXFAT_CLU_TO_B(root_clu->size, sbi)); + i_size_write(inode, exfat_cluster_to_bytes(sbi, root_clu->size)); =20 num_subdirs =3D exfat_count_dir_entries(sb, root_clu); if (num_subdirs < 0) @@ -538,7 +538,7 @@ static int exfat_read_boot_sector(struct super_block *s= b) * machines. */ sb->s_maxbytes =3D min(MAX_LFS_FILESIZE, - EXFAT_CLU_TO_B((loff_t)EXFAT_MAX_NUM_CLUSTER, sbi)); + exfat_cluster_to_bytes(sbi, (loff_t)EXFAT_MAX_NUM_CLUSTER)); =20 /* check logical sector size */ if (exfat_calibrate_blocksize(sb, 1 << p_boot->sect_size_bits)) --=20 2.25.1 From nobody Mon May 25 05:12:30 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CD9163F39C7; Mon, 18 May 2026 11:48:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779104925; cv=none; b=iID4rRa4qLAOu/jniRkktunsd/vZ7EVStSrlsL11OXdMPBY0eCs2AHDtRQu7blngZvPu1NNwWmWSLNJ6UZY6+psWy0QnLJhu9pFp8t5JSagz3BhXbOFLIhl6iAyvFaEdfQ6YpGSwmInEguOhuELLiqDSb0S8yGQYSLZY1/2Dbu0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779104925; c=relaxed/simple; bh=cd+4hZhTTrTQU+72c3MpfuD0R2Y61+Br4OnP+AIFKkM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=AajJbGYlTs6KbN+Susq0CMKQEFVbwrLoai3D46u8B4MbSc72le3MajJwtecOInuYX1l/mC2yuG2m8lATNP9DBPU6fEqbDCdTTkFs/QA+muHXg7UHbfAZXld4Adi3g6O/6t0+SjhoFzJEjmRmvT2kLeRYhRIEYi2byDa1p4V9zEA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=jFFFOFWf; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="jFFFOFWf" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5C3E9C2BCC7; Mon, 18 May 2026 11:48:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1779104925; bh=cd+4hZhTTrTQU+72c3MpfuD0R2Y61+Br4OnP+AIFKkM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=jFFFOFWfUtlPvZQLKdEpDs/qBw1ww3Wt6ASnNbK8BBgwAU848kwbMq3WFc03tzKGi dc2//NeY1fkzr4IH/CEy7oLHkLMFv76Ngz/qj9fejA3iakT58OTJ/aMXikyL/s+TQB Satxb0AEHgbZKj9/znvAPNo6vjSyRL5vBmvObmfE25eoVdv6JZ6vZeQoDFyNOOBKNV pvoh/tYQqgexkJXHQHhANZq7qWWnlJ1VNjtcc+YEYbaOCB9S9rsYIzi99O7DRBlOWG nbEOS5A1N7/3/6bja91hGqB2tDJ+liknXP8AiFKSphORsXfZuO3dAPsVHB5EiYD5eK 60GqkJW7ZFtqQ== From: Namjae Jeon To: sj1557.seo@samsung.com, yuezhang.mo@sony.com, brauner@kernel.org, djwong@kernel.org, hch@lst.de Cc: linux-fsdevel@vger.kernel.org, anmuxixixi@gmail.com, dxdt@dev.snart.me, chizhiling@kylinos.cn, chizhiling@163.com, linux-kernel@vger.kernel.org, charsyam@gmail.com, Namjae Jeon Subject: [PATCH v4 03/11] exfat: add balloc parameter to exfat_map_cluster() for iomap support Date: Mon, 18 May 2026 20:46:57 +0900 Message-Id: <20260518114705.9601-4-linkinjeon@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20260518114705.9601-1-linkinjeon@kernel.org> References: <20260518114705.9601-1-linkinjeon@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" In preparation for supporting the iomap infrastructure, we need to know whether a new cluster was allocated or not in exfat_map_cluster(). Add an optional 'bool *balloc' output parameter. When a new cluster is allocated, *balloc is set to true. Pass NULL from exfat_get_block() to preserve the existing behavior. Acked-by: Christoph Hellwig Signed-off-by: Namjae Jeon Acked-by: "Darrick J. Wong" --- fs/exfat/inode.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/fs/exfat/inode.c b/fs/exfat/inode.c index 249a35e2b4b2..a10d4f3c66a1 100644 --- a/fs/exfat/inode.c +++ b/fs/exfat/inode.c @@ -124,7 +124,8 @@ void exfat_sync_inode(struct inode *inode) * *clu =3D (~0), if it's unable to allocate a new cluster */ static int exfat_map_cluster(struct inode *inode, unsigned int clu_offset, - unsigned int *clu, unsigned int *count, int create) + unsigned int *clu, unsigned int *count, int create, + bool *balloc) { int ret; unsigned int last_clu; @@ -229,6 +230,8 @@ static int exfat_map_cluster(struct inode *inode, unsig= ned int clu_offset, if (exfat_cluster_walk(sb, clu, num_to_be_allocated - 1, ei->flags)) return -EIO; *count =3D 1; + if (balloc) + *balloc =3D true; } =20 /* hint information */ @@ -262,7 +265,7 @@ static int exfat_get_block(struct inode *inode, sector_= t iblock, /* Is this block already allocated? */ count =3D exfat_bytes_to_cluster_round_up(sbi, bh_result->b_size); err =3D exfat_map_cluster(inode, iblock >> sbi->sect_per_clus_bits, - &cluster, &count, create); + &cluster, &count, create, NULL); if (err) { if (err !=3D -ENOSPC) exfat_fs_error_ratelimit(sb, --=20 2.25.1 From nobody Mon May 25 05:12:30 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C2BD43F39C7; Mon, 18 May 2026 11:48:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779104929; cv=none; b=qkOLH0jTUTWnqjJfjndT521G7hJ2bY8M8K6Y2wVQZXGQYJ1Giv1CBOlRxpYCDOvaCZg7CpJ60HiSPFVuYksd8ej36Mtm1hQPPh7P6/pfWH1VZz691Mdud+DU3ynv6s0Z1AGfCRT1sGEkv73au0GRbPS66dnsv0HufpFtnLaNI3Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779104929; c=relaxed/simple; bh=t4+ZhTxjSol8437hYmjv+2tCSfNJXDixTSxLSc+KpA8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=q1quw/oVXSEPjdmT0RWBkbgTULkvEbK0VIdlqBAFeSQpM9TIJrX42c4iqxzCopvpmTy29JaNdMlFK6L27lMrPIsknO+jMywgIpcDvumSB5ZVzindE97wOfEV/BJKQUX7khDbEtN+r2l4kby+VbbqG2iyR5WxNAf3dcLpRQ82z2w= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=rf6MH5uD; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="rf6MH5uD" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 35387C2BCB7; Mon, 18 May 2026 11:48:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1779104929; bh=t4+ZhTxjSol8437hYmjv+2tCSfNJXDixTSxLSc+KpA8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=rf6MH5uDZZlQTWjsvtDnUyAuW5zdVyZ8Shut9rveIQVMtHaKW9+uccKncSJ3yJDY7 giosaVwPcN43QTefdVuyb4ZdUB66CPlHzOerheRr0IrUArp6D/8K7wHfL0fXS+5jzG BZBMc2dBebrlGrUhY88y3bDzZWsKPuXjQ34CZdJ3u3PbcYb1hmouAj+sjdbf1Qa+EF cqQNX/SrznkjjxclwQNb0LxKmT45MEYfDjjERiaxyRnB9IYqGIMiXvyhhxaQS7ie4j FngODPX8CCYzccr74l4Tbkn4kMNf/ySKywncpM97mQJpwDN/bxq3jvVWQVPNxFF3gn xhIidnUP8wqSg== From: Namjae Jeon To: sj1557.seo@samsung.com, yuezhang.mo@sony.com, brauner@kernel.org, djwong@kernel.org, hch@lst.de Cc: linux-fsdevel@vger.kernel.org, anmuxixixi@gmail.com, dxdt@dev.snart.me, chizhiling@kylinos.cn, chizhiling@163.com, linux-kernel@vger.kernel.org, charsyam@gmail.com, Namjae Jeon Subject: [PATCH v4 04/11] exfat: add exfat_file_open() Date: Mon, 18 May 2026 20:46:58 +0900 Message-Id: <20260518114705.9601-5-linkinjeon@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20260518114705.9601-1-linkinjeon@kernel.org> References: <20260518114705.9601-1-linkinjeon@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add exfat_file_open() to handle file open operation for exFAT. This change is a preparation step before introducing iomap-based direct IO support. Acked-by: Christoph Hellwig Signed-off-by: Namjae Jeon Acked-by: "Darrick J. Wong" --- fs/exfat/file.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/fs/exfat/file.c b/fs/exfat/file.c index 29a36a80e29b..d0ecdcb4bf7a 100644 --- a/fs/exfat/file.c +++ b/fs/exfat/file.c @@ -800,7 +800,16 @@ static ssize_t exfat_splice_read(struct file *in, loff= _t *ppos, return filemap_splice_read(in, ppos, pipe, len, flags); } =20 +static int exfat_file_open(struct inode *inode, struct file *filp) +{ + if (unlikely(exfat_forced_shutdown(inode->i_sb))) + return -EIO; + + return generic_file_open(inode, filp); +} + const struct file_operations exfat_file_operations =3D { + .open =3D exfat_file_open, .llseek =3D generic_file_llseek, .read_iter =3D exfat_file_read_iter, .write_iter =3D exfat_file_write_iter, --=20 2.25.1 From nobody Mon May 25 05:12:30 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 17A5F3F88BE; Mon, 18 May 2026 11:48:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779104933; cv=none; b=bPkN1JMX/+KwnDKw5LBdBE/YujvI0tyHi5IWkwjvhHv7bckC0CPHe+14F+okKWs8kex9gdk4XD8ciZrfuEFLmtIOkO/Zz8ap5rt+5ATwsbUOH3507YkcYNRGn2J+Oc0gF9yxDNjdt6MkKi/LT8faRso3TVGp3VI4LVN5EllNuz4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779104933; c=relaxed/simple; bh=ePwFt6WZQWGzpjDM/17T2euZQqvsXII9mFNbn60lCqU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=koTE5d8/T0NtKuukQqYRxYJO6yxPoK/KpxbIFWXN7BTk5enPKEKNTcQxLpeuctpGPFnZO1Zs/ZIyl7gVB0e12oButr4pcKRL+WitJddUN25Gw85Afq408iwyBQBCLqtzao2SH4XOwa/+ryaDcJ3BkdBW5UIaNwsI/jcluHZb7rc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=shaQCEg+; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="shaQCEg+" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8F13DC2BCB7; Mon, 18 May 2026 11:48:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1779104932; bh=ePwFt6WZQWGzpjDM/17T2euZQqvsXII9mFNbn60lCqU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=shaQCEg+Qt/GKoggDdoDTFZ5ys0eAKEhplXL1bdLIKjbi4l/PZEk127ojeiFdIq10 YHni3q/ntJvVvW1RrWLQ3VQBIA1VRNwxyC6iP9YDgODaJIRjXAT1ROeXmUjFGusPSZ a0A0RH62+GsZi0lPXEttnm1dY9pL9yWkNedRe5GwPedJQtMoPdOaLeFzM10ipRJp+d xr77GTiGm+LMBgKsKmGjfcGRWUfN3BxbQ58ObyBoUGb81mQL0jAexDr1ZiXI+XZyRj Ir7HeFFRICCUYSI9lo3H/YU7R+DwjGwt0WGEvwUZOacVnzhdpiU51d85swwx7tUQWb cR1kwxeNiiVZw== From: Namjae Jeon To: sj1557.seo@samsung.com, yuezhang.mo@sony.com, brauner@kernel.org, djwong@kernel.org, hch@lst.de Cc: linux-fsdevel@vger.kernel.org, anmuxixixi@gmail.com, dxdt@dev.snart.me, chizhiling@kylinos.cn, chizhiling@163.com, linux-kernel@vger.kernel.org, charsyam@gmail.com, Namjae Jeon Subject: [PATCH v4 05/11] exfat: add support for multi-cluster allocation Date: Mon, 18 May 2026 20:46:59 +0900 Message-Id: <20260518114705.9601-6-linkinjeon@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20260518114705.9601-1-linkinjeon@kernel.org> References: <20260518114705.9601-1-linkinjeon@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Currently exfat_map_cluster() allocates and returns only one cluster at a time even when more clusters are needed. This causes multiple FAT walks and repeated allocation calls during large sequential writes or when using iomap for writes. This change exfat_map_cluster() and exfat_alloc_cluster() to be able to allocate multiple contiguous clusters. Acked-by: Christoph Hellwig Signed-off-by: Namjae Jeon Acked-by: "Darrick J. Wong" --- fs/exfat/dir.c | 2 +- fs/exfat/exfat_fs.h | 2 +- fs/exfat/fatent.c | 26 ++++++++++++++++---------- fs/exfat/file.c | 2 +- fs/exfat/inode.c | 23 ++++++----------------- fs/exfat/namei.c | 2 +- 6 files changed, 26 insertions(+), 31 deletions(-) diff --git a/fs/exfat/dir.c b/fs/exfat/dir.c index ca9d707220df..37f324399b6b 100644 --- a/fs/exfat/dir.c +++ b/fs/exfat/dir.c @@ -295,7 +295,7 @@ int exfat_alloc_new_dir(struct inode *inode, struct exf= at_chain *clu) =20 exfat_chain_set(clu, EXFAT_EOF_CLUSTER, 0, ALLOC_NO_FAT_CHAIN); =20 - ret =3D exfat_alloc_cluster(inode, 1, clu, IS_DIRSYNC(inode)); + ret =3D exfat_alloc_cluster(inode, 1, clu, IS_DIRSYNC(inode), false); if (ret) return ret; =20 diff --git a/fs/exfat/exfat_fs.h b/fs/exfat/exfat_fs.h index 9c8ab3df7a42..6e7fd6822b01 100644 --- a/fs/exfat/exfat_fs.h +++ b/fs/exfat/exfat_fs.h @@ -497,7 +497,7 @@ int exfat_clear_volume_dirty(struct super_block *sb); exfat_cluster_walk(sb, (pclu), 1, ALLOC_FAT_CHAIN) =20 int exfat_alloc_cluster(struct inode *inode, unsigned int num_alloc, - struct exfat_chain *p_chain, bool sync_bmap); + struct exfat_chain *p_chain, bool sync_bmap, bool contig); int exfat_free_cluster(struct inode *inode, struct exfat_chain *p_chain); int exfat_ent_get(struct super_block *sb, unsigned int loc, unsigned int *content, struct buffer_head **last); diff --git a/fs/exfat/fatent.c b/fs/exfat/fatent.c index 45b0b754a2e4..a917c954bd23 100644 --- a/fs/exfat/fatent.c +++ b/fs/exfat/fatent.c @@ -419,7 +419,7 @@ int exfat_zeroed_cluster(struct inode *dir, unsigned in= t clu) } =20 int exfat_alloc_cluster(struct inode *inode, unsigned int num_alloc, - struct exfat_chain *p_chain, bool sync_bmap) + struct exfat_chain *p_chain, bool sync_bmap, bool contig) { int ret =3D -ENOSPC; unsigned int total_cnt; @@ -470,14 +470,20 @@ int exfat_alloc_cluster(struct inode *inode, unsigned= int num_alloc, =20 while ((new_clu =3D exfat_find_free_bitmap(sb, hint_clu)) !=3D EXFAT_EOF_CLUSTER) { - if (new_clu !=3D hint_clu && - p_chain->flags =3D=3D ALLOC_NO_FAT_CHAIN) { - if (exfat_chain_cont_cluster(sb, p_chain->dir, - p_chain->size)) { - ret =3D -EIO; - goto free_cluster; + if (new_clu !=3D hint_clu) { + if (p_chain->flags =3D=3D ALLOC_NO_FAT_CHAIN) { + if (exfat_chain_cont_cluster(sb, p_chain->dir, + p_chain->size)) { + ret =3D -EIO; + goto free_cluster; + } + p_chain->flags =3D ALLOC_FAT_CHAIN; + } + + if (contig && p_chain->size > 0) { + hint_clu--; + goto done; } - p_chain->flags =3D ALLOC_FAT_CHAIN; } =20 /* update allocation bitmap */ @@ -507,9 +513,9 @@ int exfat_alloc_cluster(struct inode *inode, unsigned i= nt num_alloc, last_clu =3D new_clu; =20 if (p_chain->size =3D=3D num_alloc) { +done: sbi->clu_srch_ptr =3D hint_clu; - sbi->used_clusters +=3D num_alloc; - + sbi->used_clusters +=3D p_chain->size; mutex_unlock(&sbi->bitmap_lock); return 0; } diff --git a/fs/exfat/file.c b/fs/exfat/file.c index d0ecdcb4bf7a..1effdf08ab69 100644 --- a/fs/exfat/file.c +++ b/fs/exfat/file.c @@ -56,7 +56,7 @@ static int exfat_cont_expand(struct inode *inode, loff_t = size) clu.flags =3D ei->flags; =20 ret =3D exfat_alloc_cluster(inode, new_num_clusters - num_clusters, - &clu, inode_needs_sync(inode)); + &clu, inode_needs_sync(inode), false); if (ret) return ret; =20 diff --git a/fs/exfat/inode.c b/fs/exfat/inode.c index a10d4f3c66a1..7b09d94ac464 100644 --- a/fs/exfat/inode.c +++ b/fs/exfat/inode.c @@ -137,9 +137,9 @@ static int exfat_map_cluster(struct inode *inode, unsig= ned int clu_offset, unsigned int num_to_be_allocated =3D 0, num_clusters; =20 num_clusters =3D exfat_bytes_to_cluster(sbi, exfat_ondisk_size(inode)); - - if (clu_offset >=3D num_clusters) - num_to_be_allocated =3D clu_offset - num_clusters + 1; + if (clu_offset > num_clusters || + *count > num_clusters - clu_offset) + num_to_be_allocated =3D clu_offset + *count - num_clusters; =20 if (!create && (num_to_be_allocated > 0)) { *clu =3D EXFAT_EOF_CLUSTER; @@ -182,7 +182,7 @@ static int exfat_map_cluster(struct inode *inode, unsig= ned int clu_offset, } =20 ret =3D exfat_alloc_cluster(inode, num_to_be_allocated, &new_clu, - inode_needs_sync(inode)); + inode_needs_sync(inode), true); if (ret) return ret; =20 @@ -216,20 +216,9 @@ static int exfat_map_cluster(struct inode *inode, unsi= gned int clu_offset, } =20 *clu =3D new_clu.dir; + *count =3D new_clu.size; =20 - inode->i_blocks +=3D - exfat_cluster_to_sectors(sbi, num_to_be_allocated); - - /* - * Move *clu pointer along FAT chains (hole care) because the - * caller of this function expect *clu to be the last cluster. - * This only works when num_to_be_allocated >=3D 2, - * *clu =3D (the first cluster of the allocated chain) =3D> - * (the last cluster of ...) - */ - if (exfat_cluster_walk(sb, clu, num_to_be_allocated - 1, ei->flags)) - return -EIO; - *count =3D 1; + inode->i_blocks +=3D exfat_cluster_to_sectors(sbi, new_clu.size); if (balloc) *balloc =3D true; } diff --git a/fs/exfat/namei.c b/fs/exfat/namei.c index 752fbec9316b..87e55c5c1bf4 100644 --- a/fs/exfat/namei.c +++ b/fs/exfat/namei.c @@ -341,7 +341,7 @@ int exfat_find_empty_entry(struct inode *inode, } =20 /* allocate a cluster */ - ret =3D exfat_alloc_cluster(inode, 1, &clu, IS_DIRSYNC(inode)); + ret =3D exfat_alloc_cluster(inode, 1, &clu, IS_DIRSYNC(inode), false); if (ret) return ret; =20 --=20 2.25.1 From nobody Mon May 25 05:12:30 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5E7A63F88B6; Mon, 18 May 2026 11:48:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779104936; cv=none; b=t70LKW2vhjqc71r73Kf9FxyHkt5pD14BEI4/7zyHwxowCN69Lb6P9zgXeCTLBb9bCqFgtP1eraIMM0kC86vYFend+Z7RokGZgMBMJx0Us76dPQaXtnsTgKE7oygjMIYRV1mKKSd7ddJjguj4XUIM93XxMLSkxU2itZ1JPgj3g3A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779104936; c=relaxed/simple; bh=HtrVfA5bNSbExzd+ImfH9F8YbOrLboCObnFy+jSpZCk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=LIYRr4DnVXcUYgZ5OCYbixYcstsXmDqf4eSBJ/JJxzvpH289wTK7vyXFIkvWyodqY2B24aLuuXV5sm/BdUrNSqp8KSIO/H4PvLd2giKod7BWa15ZgZweTbc2q2vyuwSrHN0kT1d29p8wkr7c7gDpmoD1KzWisympl+wDgOSuaEE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=u/LbNs4a; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="u/LbNs4a" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0C5B8C2BCC6; Mon, 18 May 2026 11:48:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1779104936; bh=HtrVfA5bNSbExzd+ImfH9F8YbOrLboCObnFy+jSpZCk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=u/LbNs4aNm8385PzdNZ9veN3jI6uRyetBS0iSvyJC8xHRqB77YTOoHzOr/QnulcfR uWBPf9taWk211G+IZPCBClAqbS1G8ZxnAJKu5VKFvUe9VGhRNfkUUnj0qNSfpVec8z 83V7EwipVO0HOGoFWBPaJMCjqcwgIoH5+hCTv73jSx2ePT+Ejr8vq7mkEQeaLqmPB6 OHk1fesg7fAM1u5b0uy2bie2ktpiK3dMbL0PUmZl9Yxxs9L6f5TkuOQNvGsc54nxlO PWJL3iGkupTxl0kQd59NxMKa/fSm7KoyUlm3AMe1wXkgfgiat0+Cr0GrePHmItLWH1 NpZr5yT7RYrUQ== From: Namjae Jeon To: sj1557.seo@samsung.com, yuezhang.mo@sony.com, brauner@kernel.org, djwong@kernel.org, hch@lst.de Cc: linux-fsdevel@vger.kernel.org, anmuxixixi@gmail.com, dxdt@dev.snart.me, chizhiling@kylinos.cn, chizhiling@163.com, linux-kernel@vger.kernel.org, charsyam@gmail.com, Namjae Jeon Subject: [PATCH v4 06/11] exfat: add data_start_bytes and exfat_cluster_to_phys_bytes() helper Date: Mon, 18 May 2026 20:47:00 +0900 Message-Id: <20260518114705.9601-7-linkinjeon@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20260518114705.9601-1-linkinjeon@kernel.org> References: <20260518114705.9601-1-linkinjeon@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" This caches the data area start offset in bytes (data_start_bytes) and introduces a helper function exfat_cluster_to_phys_bytes() to compute the physical byte position of a given cluster. Acked-by: Christoph Hellwig Signed-off-by: Namjae Jeon Acked-by: "Darrick J. Wong" --- fs/exfat/exfat_fs.h | 8 ++++++++ fs/exfat/super.c | 1 + 2 files changed, 9 insertions(+) diff --git a/fs/exfat/exfat_fs.h b/fs/exfat/exfat_fs.h index 6e7fd6822b01..cd38e71d3e8a 100644 --- a/fs/exfat/exfat_fs.h +++ b/fs/exfat/exfat_fs.h @@ -227,6 +227,7 @@ struct exfat_sb_info { unsigned long long FAT1_start_sector; /* FAT1 start sector */ unsigned long long FAT2_start_sector; /* FAT2 start sector */ unsigned long long data_start_sector; /* data area start sector */ + unsigned long long data_start_bytes; unsigned int num_FAT_sectors; /* num of FAT sectors */ unsigned int root_dir; /* root dir cluster */ unsigned int dentries_per_clu; /* num of dentries per cluster */ @@ -400,6 +401,13 @@ static inline loff_t exfat_ondisk_size(const struct in= ode *inode) return ((loff_t)inode->i_blocks) << 9; } =20 +static inline loff_t exfat_cluster_to_phys_bytes(struct exfat_sb_info *sbi, + unsigned int clus) +{ + return ((loff_t)(clus - EXFAT_RESERVED_CLUSTERS) << sbi->cluster_size_bit= s) + + sbi->data_start_bytes; +} + /* * helpers for cluster size to byte conversion. */ diff --git a/fs/exfat/super.c b/fs/exfat/super.c index cb2f8eefff99..388db271c6bf 100644 --- a/fs/exfat/super.c +++ b/fs/exfat/super.c @@ -499,6 +499,7 @@ static int exfat_read_boot_sector(struct super_block *s= b) if (p_boot->num_fats =3D=3D 2) sbi->FAT2_start_sector +=3D sbi->num_FAT_sectors; sbi->data_start_sector =3D le32_to_cpu(p_boot->clu_offset); + sbi->data_start_bytes =3D sbi->data_start_sector << p_boot->sect_size_bit= s; sbi->num_sectors =3D le64_to_cpu(p_boot->vol_length); /* because the cluster index starts with 2 */ sbi->num_clusters =3D le32_to_cpu(p_boot->clu_count) + --=20 2.25.1 From nobody Mon May 25 05:12:30 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C99A73F7860; Mon, 18 May 2026 11:48:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779104939; cv=none; b=CbK/q0Wpb7m9ZNyYTB40BfsFzUCLiEWqxch0CEKSZKMlTQo8mWCh0/dlA5RZUt0t0RmwQbI9RdTPyhDPKj57kJwu2+bwbST1Z4UnZOZAjNlg8pYFdqH1b1vo9ARcfEZ48uPU5D2tIMSsKLZfrSkF0E3QGD/Pr0BaVqBT2eWZ4v4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779104939; c=relaxed/simple; bh=U204ERnUanMPne1YFbGJvFhKXeqrR5VmCmE1lCpidZI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=E/JmVQBJA2pKTONNH2e8HYz6SsBgKcnIx3Wa/wilYBn2WKQKZFKE/U9MPZ88rAJ+Sn5hJB2Wzp00QWfKbeiiWvfMLtDq/KAceLiwqLIYUPiHwCQo/0ngpwQ4DgOuXlz/68xp3+Ayxl2LApw3C7a8FOlD18CBaAE/57AcRaBqYTc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=rJcBMV1o; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="rJcBMV1o" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 943E6C2BCB8; Mon, 18 May 2026 11:48:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1779104939; bh=U204ERnUanMPne1YFbGJvFhKXeqrR5VmCmE1lCpidZI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=rJcBMV1oXQw5SIP+uJwSHQyxGmdaZl1X0ikMbJrzn1d/9lDOtL9slHMAfVm5iwgqh j9ORHFk8RZJtz56zAi903+BFSqJEmevkVaK/7AUWYBfJKkSHc4gjWfyieJWheFMzXN GlngS7iPMYxyBslFjg8xwdiLNTob7L0/ZAq8EfPRwN5BmbhJ1I2C98XHxeTlQGUijb fx5MCkAwmyV/Yt95B7MXgthhqm/ry4u7UO/tpgHOQ8gvueAKmIve+h6jdrsa4S3UYd 8LaInOahjAgPBX52RA9hjfbQXuMydeDH/4ALjbzcswvJ7k9FvzGObCXWkPgLkBvSVX Hgh1ZOaDF+eJQ== From: Namjae Jeon To: sj1557.seo@samsung.com, yuezhang.mo@sony.com, brauner@kernel.org, djwong@kernel.org, hch@lst.de Cc: linux-fsdevel@vger.kernel.org, anmuxixixi@gmail.com, dxdt@dev.snart.me, chizhiling@kylinos.cn, chizhiling@163.com, linux-kernel@vger.kernel.org, charsyam@gmail.com, Namjae Jeon Subject: [PATCH v4 07/11] exfat: fix implicit declaration of brelse() Date: Mon, 18 May 2026 20:47:01 +0900 Message-Id: <20260518114705.9601-8-linkinjeon@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20260518114705.9601-1-linkinjeon@kernel.org> References: <20260518114705.9601-1-linkinjeon@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable exfat_cluster_walk() calls brelse(bh) without including the header that declares the function, causing the following build error: fs/exfat/exfat_fs.h:542:9: error: implicit declaration of function =E2= =80=98brelse=E2=80=99 [-Werror=3Dimplicit-function-declaration] Fix this by adding the missing buffer_head.h in exfat_fs.h. Acked-by: Christoph Hellwig Signed-off-by: Namjae Jeon Acked-by: "Darrick J. Wong" --- fs/exfat/exfat_fs.h | 1 + 1 file changed, 1 insertion(+) diff --git a/fs/exfat/exfat_fs.h b/fs/exfat/exfat_fs.h index cd38e71d3e8a..08d22d2581da 100644 --- a/fs/exfat/exfat_fs.h +++ b/fs/exfat/exfat_fs.h @@ -12,6 +12,7 @@ #include #include #include +#include =20 #define EXFAT_ROOT_INO 1 =20 --=20 2.25.1 From nobody Mon May 25 05:12:30 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 54FF53F926D; Mon, 18 May 2026 11:49:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779104943; cv=none; b=dis9jh/1+FeZd/o0I0fH809zAnNc9HMss4tboyH79LacG4XmMAsQPqbfxxeU+m2qU41YWB3EsspSxJmsxotUHIQKHMrn96OZerNiX7N24nQzjP5DBuvP2L/hTjlIb+JXHolMhB5Qi1OqrqYJIbz5X415SGdNS4khJpiyFVi4GtY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779104943; c=relaxed/simple; bh=4KEQ6/szQeIXJ4u2Mzaadi5hjOcYyTzhOZBTaUQvtVI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=NmDo8nMRm1Wy74U7yuOCC7NudQiabBpcR0t3TR3v+VeF8tUGzrIOlQ7+f3ljiZgJse1niEoRkMCeK/RVAkur3CML2BuFn1KnEZlkTbbHv/dEJbJOQjoXVmGliQy1RmAruRuf+5MTiJXZMTAcmZeW0ZP3nRYNIXL9rScsrcqQXNw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=F7IGUjCU; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="F7IGUjCU" Received: by smtp.kernel.org (Postfix) with ESMTPSA id EF07BC2BCB7; Mon, 18 May 2026 11:49:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1779104943; bh=4KEQ6/szQeIXJ4u2Mzaadi5hjOcYyTzhOZBTaUQvtVI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=F7IGUjCUC1eAybkUWmXXb7iBYtQHyawLBB1f9FhLFSwQCcChMQX3YwPY0vz9ZhQkZ upHSMGr6uxHJs9XQOcVtCB3FuXFEpzDrzchwwbroSD7Qhh06eKBze5HICWwfPkx0k8 LVR80EQi5p/25VLWYUbf7wXNbYX+vMZJ6II5zLcrh+2wRQ4Fqw3zCOgg56qK6Hzlvn rmNMJebRgUnBTMF3T2iO4D9sEGkq2encEBGrqevf8/u414yKTnd6qxQWROQRxCxd4p LdY2Y2KRI5BsZAL5sByb522YXK3czgjIX85t2km8zIU/yx1xXpJshABfej+A07BY3J hztz2gNjIWPaQ== From: Namjae Jeon To: sj1557.seo@samsung.com, yuezhang.mo@sony.com, brauner@kernel.org, djwong@kernel.org, hch@lst.de Cc: linux-fsdevel@vger.kernel.org, anmuxixixi@gmail.com, dxdt@dev.snart.me, chizhiling@kylinos.cn, chizhiling@163.com, linux-kernel@vger.kernel.org, charsyam@gmail.com, Namjae Jeon Subject: [PATCH v4 08/11] exfat: add iomap buffered I/O support Date: Mon, 18 May 2026 20:47:02 +0900 Message-Id: <20260518114705.9601-9-linkinjeon@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20260518114705.9601-1-linkinjeon@kernel.org> References: <20260518114705.9601-1-linkinjeon@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add full buffered I/O support using the iomap framework to the exfat filesystem. This will replaces the old exfat_get_block(), exfat_write_begin(), exfat_write_end(), and exfat_block_truncate_page() with their iomap equivalents. Buffered writes now use iomap_file_buffered_write(), read uses iomap_bio_read_folio() and iomap_bio_readahead(), and writeback is handled through iomap_writepages(). Acked-by: Christoph Hellwig Signed-off-by: Namjae Jeon Acked-by: "Darrick J. Wong" --- fs/exfat/Kconfig | 1 + fs/exfat/Makefile | 2 +- fs/exfat/exfat_fs.h | 6 +- fs/exfat/file.c | 142 +++++++++++++++++++---------- fs/exfat/inode.c | 117 ++++++++---------------- fs/exfat/iomap.c | 213 ++++++++++++++++++++++++++++++++++++++++++++ fs/exfat/iomap.h | 14 +++ 7 files changed, 365 insertions(+), 130 deletions(-) create mode 100644 fs/exfat/iomap.c create mode 100644 fs/exfat/iomap.h diff --git a/fs/exfat/Kconfig b/fs/exfat/Kconfig index cbeca8e44d9b..e0b200902253 100644 --- a/fs/exfat/Kconfig +++ b/fs/exfat/Kconfig @@ -5,6 +5,7 @@ config EXFAT_FS select BUFFER_HEAD select NLS select LEGACY_DIRECT_IO + select FS_IOMAP help This allows you to mount devices formatted with the exFAT file system. exFAT is typically used on SD-Cards or USB sticks. diff --git a/fs/exfat/Makefile b/fs/exfat/Makefile index ed51926a4971..e06bf85870ae 100644 --- a/fs/exfat/Makefile +++ b/fs/exfat/Makefile @@ -5,4 +5,4 @@ obj-$(CONFIG_EXFAT_FS) +=3D exfat.o =20 exfat-y :=3D inode.o namei.o dir.o super.o fatent.o cache.o nls.o misc.o \ - file.o balloc.o + file.o balloc.o iomap.o diff --git a/fs/exfat/exfat_fs.h b/fs/exfat/exfat_fs.h index 08d22d2581da..2d482858bfaf 100644 --- a/fs/exfat/exfat_fs.h +++ b/fs/exfat/exfat_fs.h @@ -294,6 +294,8 @@ struct exfat_inode_info { /* on-disk position of directory entry or 0 */ loff_t i_pos; loff_t valid_size; + /* page-aligned size that has been zeroed out for mmap */ + loff_t zeroed_size; /* hash by i_location */ struct hlist_node i_hash_fat; /* protect bmap against truncate */ @@ -649,7 +651,9 @@ struct inode *exfat_iget(struct super_block *sb, loff_t= i_pos); int __exfat_write_inode(struct inode *inode, int sync); int exfat_write_inode(struct inode *inode, struct writeback_control *wbc); void exfat_evict_inode(struct inode *inode); -int exfat_block_truncate_page(struct inode *inode, loff_t from); +int exfat_map_cluster(struct inode *inode, unsigned int clu_offset, + unsigned int *clu, unsigned int *count, int create, + bool *balloc); =20 /* exfat/nls.c */ unsigned short exfat_toupper(struct super_block *sb, unsigned short a); diff --git a/fs/exfat/file.c b/fs/exfat/file.c index 1effdf08ab69..389ef7b36ed0 100644 --- a/fs/exfat/file.c +++ b/fs/exfat/file.c @@ -14,9 +14,11 @@ #include #include #include +#include =20 #include "exfat_raw.h" #include "exfat_fs.h" +#include "iomap.h" =20 static int exfat_cont_expand(struct inode *inode, loff_t size) { @@ -26,8 +28,9 @@ static int exfat_cont_expand(struct inode *inode, loff_t = size) struct super_block *sb =3D inode->i_sb; struct exfat_sb_info *sbi =3D EXFAT_SB(sb); struct exfat_chain clu; + loff_t oldsize =3D i_size_read(inode); =20 - truncate_pagecache(inode, i_size_read(inode)); + truncate_pagecache(inode, oldsize); =20 ret =3D inode_newsize_ok(inode, size); if (ret) @@ -78,6 +81,13 @@ static int exfat_cont_expand(struct inode *inode, loff_t= size) inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode)); /* Expanded range not zeroed, do not update valid_size */ i_size_write(inode, size); + /* + * When extending file size, call truncate_pagecache() first, + * then update i_size, and call pagecache_isize_extended() + * to ensures the straddling folio is properly marked RO so + * page_mkwrite() is called and post-EOF area is zeroed. + */ + pagecache_isize_extended(inode, oldsize, inode->i_size); =20 inode->i_blocks =3D round_up(size, sbi->cluster_size) >> 9; mark_inode_dirty(inode); @@ -236,7 +246,7 @@ int __exfat_truncate(struct inode *inode) } =20 if (i_size_read(inode) < ei->valid_size) - ei->valid_size =3D i_size_read(inode); + ei->valid_size =3D ei->zeroed_size =3D i_size_read(inode); =20 if (ei->type =3D=3D TYPE_FILE) ei->attr |=3D EXFAT_ATTR_ARCHIVE; @@ -383,10 +393,6 @@ int exfat_setattr(struct mnt_idmap *idmap, struct dent= ry *dentry, exfat_truncate_inode_atime(inode); =20 if (attr->ia_valid & ATTR_SIZE) { - error =3D exfat_block_truncate_page(inode, attr->ia_size); - if (error) - goto out; - down_write(&EXFAT_I(inode)->truncate_lock); truncate_setsize(inode, attr->ia_size); =20 @@ -631,42 +637,26 @@ int exfat_file_fsync(struct file *filp, loff_t start,= loff_t end, int datasync) =20 static int exfat_extend_valid_size(struct inode *inode, loff_t new_valid_s= ize) { - int err; - loff_t pos; struct exfat_inode_info *ei =3D EXFAT_I(inode); - struct address_space *mapping =3D inode->i_mapping; - const struct address_space_operations *ops =3D mapping->a_ops; - - pos =3D ei->valid_size; - while (pos < new_valid_size) { - u32 len; - struct folio *folio; - unsigned long off; - - len =3D PAGE_SIZE - (pos & (PAGE_SIZE - 1)); - if (pos + len > new_valid_size) - len =3D new_valid_size - pos; - - err =3D ops->write_begin(NULL, mapping, pos, len, &folio, NULL); - if (err) - goto out; - - off =3D offset_in_folio(folio, pos); - folio_zero_new_buffers(folio, off, off + len); + loff_t old_valid_size =3D ei->valid_size; + int ret =3D 0; =20 - err =3D ops->write_end(NULL, mapping, pos, len, len, folio, NULL); - if (err < 0) - goto out; - pos +=3D len; + if (old_valid_size < new_valid_size) { + if (i_size_read(inode) < new_valid_size) { + i_size_write(inode, new_valid_size); + mark_inode_dirty(inode); + } =20 - balance_dirty_pages_ratelimited(mapping); - cond_resched(); + ret =3D iomap_zero_range(inode, old_valid_size, + new_valid_size - old_valid_size, NULL, + &exfat_write_iomap_ops, NULL, NULL); + if (ret) { + truncate_setsize(inode, old_valid_size); + exfat_truncate(inode); + } } =20 - return 0; - -out: - return err; + return ret; } =20 static ssize_t exfat_file_write_iter(struct kiocb *iocb, struct iov_iter *= iter) @@ -677,6 +667,7 @@ static ssize_t exfat_file_write_iter(struct kiocb *iocb= , struct iov_iter *iter) struct exfat_inode_info *ei =3D EXFAT_I(inode); loff_t pos =3D iocb->ki_pos; loff_t valid_size; + int err; =20 if (unlikely(exfat_forced_shutdown(inode->i_sb))) return -EIO; @@ -702,6 +693,12 @@ static ssize_t exfat_file_write_iter(struct kiocb *ioc= b, struct iov_iter *iter) } } =20 + err =3D file_modified(iocb->ki_filp); + if (err) { + ret =3D err; + goto unlock; + } + if (pos > valid_size) { ret =3D exfat_extend_valid_size(inode, pos); if (ret < 0 && ret !=3D -ENOSPC) { @@ -713,7 +710,11 @@ static ssize_t exfat_file_write_iter(struct kiocb *ioc= b, struct iov_iter *iter) goto unlock; } =20 - ret =3D __generic_file_write_iter(iocb, iter); + if (iocb->ki_flags & IOCB_DIRECT) + ret =3D __generic_file_write_iter(iocb, iter); + else + ret =3D iomap_file_buffered_write(iocb, iter, + &exfat_write_iomap_ops, NULL, NULL); if (ret < 0) goto unlock; =20 @@ -749,28 +750,56 @@ static ssize_t exfat_file_read_iter(struct kiocb *ioc= b, struct iov_iter *iter) =20 static vm_fault_t exfat_page_mkwrite(struct vm_fault *vmf) { - int err; struct inode *inode =3D file_inode(vmf->vma->vm_file); struct exfat_inode_info *ei =3D EXFAT_I(inode); - loff_t new_valid_size; + vm_fault_t ret; + loff_t new_valid_size, mmap_valid_size; =20 if (!inode_trylock(inode)) return VM_FAULT_RETRY; =20 - new_valid_size =3D ((loff_t)vmf->pgoff + 1) << PAGE_SHIFT; - new_valid_size =3D min(new_valid_size, i_size_read(inode)); + mmap_valid_size =3D ((loff_t)vmf->pgoff + 1) << PAGE_SHIFT; + new_valid_size =3D min(mmap_valid_size, i_size_read(inode)); =20 if (ei->valid_size < new_valid_size) { - err =3D exfat_extend_valid_size(inode, new_valid_size); - if (err < 0) { - inode_unlock(inode); - return vmf_fs_error(err); + if (ei->zeroed_size < mmap_valid_size) { + int err; + + /* + * Only zero the range that hasn't been zeroed yet for + * this mmap write path. zeroed_size tracks the largest + * page-aligned offset that has already been zeroed. + * + * This prevents unnecessarily zeroing out the entire + * tail page on every page fault when userspace writes + * data byte-by-byte through mmap (after a small + * fallocate). It fixes data corruption in the tail page + * while preserving the existing valid_size semantics. + */ + err =3D iomap_zero_range(inode, ei->zeroed_size, + mmap_valid_size - ei->zeroed_size, NULL, + &exfat_iomap_ops, NULL, NULL); + if (err < 0) { + inode_unlock(inode); + return vmf_fs_error(err); + } + ei->zeroed_size =3D mmap_valid_size; } + + ei->valid_size =3D new_valid_size; + mark_inode_dirty(inode); } =20 + sb_start_pagefault(inode->i_sb); + file_update_time(vmf->vma->vm_file); + + filemap_invalidate_lock_shared(inode->i_mapping); + ret =3D iomap_page_mkwrite(vmf, &exfat_write_iomap_ops, NULL); + filemap_invalidate_unlock_shared(inode->i_mapping); + sb_end_pagefault(inode->i_sb); inode_unlock(inode); =20 - return filemap_page_mkwrite(vmf); + return ret; } =20 static const struct vm_operations_struct exfat_file_vm_ops =3D { @@ -786,6 +815,25 @@ static int exfat_file_mmap_prepare(struct vm_area_desc= *desc) if (unlikely(exfat_forced_shutdown(file_inode(desc->file)->i_sb))) return -EIO; =20 + if (vma_desc_test_all(desc, VMA_SHARED_BIT, VMA_MAYWRITE_BIT)) { + struct inode *inode =3D file_inode(file); + loff_t from, to; + int err; + + inode_lock(inode); + from =3D ((loff_t)desc->pgoff << PAGE_SHIFT); + to =3D min_t(loff_t, i_size_read(inode), + from + vma_desc_size(desc)); + if (EXFAT_I(inode)->valid_size < to) { + err =3D exfat_extend_valid_size(inode, to); + if (err) { + inode_unlock(inode); + return err; + } + } + inode_unlock(inode); + } + file_accessed(file); desc->vm_ops =3D &exfat_file_vm_ops; return 0; diff --git a/fs/exfat/inode.c b/fs/exfat/inode.c index 7b09d94ac464..6083ccef9408 100644 --- a/fs/exfat/inode.c +++ b/fs/exfat/inode.c @@ -13,9 +13,11 @@ #include #include #include +#include =20 #include "exfat_raw.h" #include "exfat_fs.h" +#include "iomap.h" =20 int __exfat_write_inode(struct inode *inode, int sync) { @@ -76,15 +78,7 @@ int __exfat_write_inode(struct inode *inode, int sync) on_disk_size =3D 0; =20 ep2->dentry.stream.size =3D cpu_to_le64(on_disk_size); - /* - * mmap write does not use exfat_write_end(), valid_size may be - * extended to the sector-aligned length in exfat_get_block(). - * So we need to fixup valid_size to the writren length. - */ - if (on_disk_size < ei->valid_size) - ep2->dentry.stream.valid_size =3D ep2->dentry.stream.size; - else - ep2->dentry.stream.valid_size =3D cpu_to_le64(ei->valid_size); + ep2->dentry.stream.valid_size =3D cpu_to_le64(ei->valid_size); =20 if (on_disk_size) { ep2->dentry.stream.flags =3D ei->flags; @@ -123,7 +117,7 @@ void exfat_sync_inode(struct inode *inode) * Output: errcode, cluster number * *clu =3D (~0), if it's unable to allocate a new cluster */ -static int exfat_map_cluster(struct inode *inode, unsigned int clu_offset, +int exfat_map_cluster(struct inode *inode, unsigned int clu_offset, unsigned int *clu, unsigned int *count, int create, bool *balloc) { @@ -377,7 +371,13 @@ static int exfat_get_block(struct inode *inode, sector= _t iblock, =20 static int exfat_read_folio(struct file *file, struct folio *folio) { - return mpage_read_folio(folio, exfat_get_block); + struct iomap_read_folio_ctx ctx =3D { + .cur_folio =3D folio, + .ops =3D &exfat_iomap_bio_read_ops, + }; + + iomap_read_folio(&exfat_iomap_ops, &ctx, NULL); + return 0; } =20 static void exfat_readahead(struct readahead_control *rac) @@ -386,6 +386,10 @@ static void exfat_readahead(struct readahead_control *= rac) struct inode *inode =3D mapping->host; struct exfat_inode_info *ei =3D EXFAT_I(inode); loff_t pos =3D readahead_pos(rac); + struct iomap_read_folio_ctx ctx =3D { + .ops =3D &exfat_iomap_bio_read_ops, + .rac =3D rac, + }; =20 /* Range cross valid_size, read it page by page. */ if (ei->valid_size < i_size_read(inode) && @@ -393,16 +397,22 @@ static void exfat_readahead(struct readahead_control = *rac) ei->valid_size < pos + readahead_length(rac)) return; =20 - mpage_readahead(rac, exfat_get_block); + iomap_readahead(&exfat_iomap_ops, &ctx, NULL); } =20 static int exfat_writepages(struct address_space *mapping, struct writeback_control *wbc) { + struct iomap_writepage_ctx wpc =3D { + .inode =3D mapping->host, + .wbc =3D wbc, + .ops =3D &exfat_writeback_ops, + }; + if (unlikely(exfat_forced_shutdown(mapping->host->i_sb))) return -EIO; =20 - return mpage_writepages(mapping, wbc, exfat_get_block); + return iomap_writepages(&wpc); } =20 static void exfat_write_failed(struct address_space *mapping, loff_t to) @@ -416,51 +426,6 @@ static void exfat_write_failed(struct address_space *m= apping, loff_t to) } } =20 -static int exfat_write_begin(const struct kiocb *iocb, - struct address_space *mapping, - loff_t pos, unsigned int len, - struct folio **foliop, void **fsdata) -{ - int ret; - - if (unlikely(exfat_forced_shutdown(mapping->host->i_sb))) - return -EIO; - - ret =3D block_write_begin(mapping, pos, len, foliop, exfat_get_block); - - if (ret < 0) - exfat_write_failed(mapping, pos+len); - - return ret; -} - -static int exfat_write_end(const struct kiocb *iocb, - struct address_space *mapping, - loff_t pos, unsigned int len, unsigned int copied, - struct folio *folio, void *fsdata) -{ - struct inode *inode =3D mapping->host; - struct exfat_inode_info *ei =3D EXFAT_I(inode); - int err; - - err =3D generic_write_end(iocb, mapping, pos, len, copied, folio, fsdata); - if (err < len) - exfat_write_failed(mapping, pos+len); - - if (!(err < 0) && pos + err > ei->valid_size) { - ei->valid_size =3D pos + err; - mark_inode_dirty(inode); - } - - if (!(err < 0) && !(ei->attr & EXFAT_ATTR_ARCHIVE)) { - inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode)); - ei->attr |=3D EXFAT_ATTR_ARCHIVE; - mark_inode_dirty(inode); - } - - return err; -} - static ssize_t exfat_direct_IO(struct kiocb *iocb, struct iov_iter *iter) { struct address_space *mapping =3D iocb->ki_filp->f_mapping; @@ -510,34 +475,23 @@ static sector_t exfat_aop_bmap(struct address_space *= mapping, sector_t block) =20 /* exfat_get_cluster() assumes the requested blocknr isn't truncated. */ down_read(&EXFAT_I(mapping->host)->truncate_lock); - blocknr =3D generic_block_bmap(mapping, block, exfat_get_block); + blocknr =3D iomap_bmap(mapping, block, &exfat_iomap_ops); up_read(&EXFAT_I(mapping->host)->truncate_lock); return blocknr; } =20 -/* - * exfat_block_truncate_page() zeroes out a mapping from file offset `from' - * up to the end of the block which corresponds to `from'. - * This is required during truncate to physically zeroout the tail end - * of that block so it doesn't yield old data if the file is later grown. - * Also, avoid causing failure from fsx for cases of "data past EOF" - */ -int exfat_block_truncate_page(struct inode *inode, loff_t from) -{ - return block_truncate_page(inode->i_mapping, from, exfat_get_block); -} - static const struct address_space_operations exfat_aops =3D { - .dirty_folio =3D block_dirty_folio, - .invalidate_folio =3D block_invalidate_folio, - .read_folio =3D exfat_read_folio, - .readahead =3D exfat_readahead, - .writepages =3D exfat_writepages, - .write_begin =3D exfat_write_begin, - .write_end =3D exfat_write_end, - .direct_IO =3D exfat_direct_IO, - .bmap =3D exfat_aop_bmap, - .migrate_folio =3D buffer_migrate_folio, + .read_folio =3D exfat_read_folio, + .readahead =3D exfat_readahead, + .writepages =3D exfat_writepages, + .dirty_folio =3D iomap_dirty_folio, + .bmap =3D exfat_aop_bmap, + .migrate_folio =3D filemap_migrate_folio, + .is_partially_uptodate =3D iomap_is_partially_uptodate, + .error_remove_folio =3D generic_error_remove_folio, + .release_folio =3D iomap_release_folio, + .invalidate_folio =3D iomap_invalidate_folio, + .direct_IO =3D exfat_direct_IO, }; =20 static inline unsigned long exfat_hash(loff_t i_pos) @@ -601,6 +555,7 @@ static int exfat_fill_inode(struct inode *inode, struct= exfat_dir_entry *info) ei->flags =3D info->flags; ei->type =3D info->type; ei->valid_size =3D info->valid_size; + ei->zeroed_size =3D info->valid_size; =20 ei->version =3D 0; ei->hint_stat.eidx =3D 0; diff --git a/fs/exfat/iomap.c b/fs/exfat/iomap.c new file mode 100644 index 000000000000..b360fadc9d58 --- /dev/null +++ b/fs/exfat/iomap.c @@ -0,0 +1,213 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * iomap callack functions + * + * Copyright (C) 2026 Namjae Jeon + */ + +#include +#include + +#include "exfat_raw.h" +#include "exfat_fs.h" +#include "iomap.h" + +static int __exfat_iomap_begin(struct inode *inode, loff_t offset, loff_t = length, + unsigned int flags, struct iomap *iomap, bool may_alloc) +{ + struct super_block *sb =3D inode->i_sb; + struct exfat_sb_info *sbi =3D EXFAT_SB(sb); + struct exfat_inode_info *ei =3D EXFAT_I(inode); + unsigned int cluster, num_clusters; + loff_t cluster_offset, cluster_length; + int err; + bool balloc =3D false; + + if (!may_alloc) { + /* Completely beyond EOF. Treat as hole */ + if (i_size_read(inode) <=3D offset) { + iomap->type =3D IOMAP_HOLE; + iomap->addr =3D IOMAP_NULL_ADDR; + iomap->offset =3D offset; + iomap->length =3D length; + return 0; + } + + /* Clamp length if the requested range goes beyond i_size */ + if (offset + length > i_size_read(inode)) + length =3D round_up(i_size_read(inode), + i_blocksize(inode)) - offset; + } + + num_clusters =3D exfat_bytes_to_cluster_round_up(sbi, + offset + length) - exfat_bytes_to_cluster(sbi, offset); + + mutex_lock(&sbi->s_lock); + iomap->bdev =3D inode->i_sb->s_bdev; + iomap->offset =3D offset; + + err =3D exfat_map_cluster(inode, exfat_bytes_to_cluster(sbi, offset), + &cluster, &num_clusters, may_alloc, &balloc); + if (err) + goto out; + + cluster_offset =3D exfat_cluster_offset(sbi, offset); + cluster_length =3D exfat_cluster_to_bytes(sbi, num_clusters); + + iomap->length =3D min_t(loff_t, length, cluster_length - cluster_offset); + iomap->addr =3D exfat_cluster_to_phys_bytes(sbi, cluster) + cluster_offse= t; + iomap->type =3D IOMAP_MAPPED; + if (may_alloc) { + if (balloc) + iomap->flags =3D IOMAP_F_NEW; + else if (iomap->offset + iomap->length >=3D ei->valid_size) { + /* + * This is a write that starts at or extends beyond + * the current valid_size. The region between the old + * valid_size and the end of this write needs to be + * zeroed in the page cache to prevent stale data + * exposure (see IOMAP_F_ZERO_TAIL handling in + * __iomap_write_begin()). + */ + iomap->flags =3D IOMAP_F_ZERO_TAIL; + } + } else { + if (offset >=3D ei->valid_size) { + iomap->type =3D IOMAP_UNWRITTEN; + } else if (offset + iomap->length > ei->valid_size) { + iomap->length =3D round_up(ei->valid_size, + i_blocksize(inode)) - + iomap->offset; + } + } + + iomap->flags |=3D IOMAP_F_MERGED; +out: + mutex_unlock(&sbi->s_lock); + return err; +} + +static int exfat_iomap_begin(struct inode *inode, loff_t offset, loff_t le= ngth, + unsigned int flags, struct iomap *iomap, struct iomap *srcmap) +{ + return __exfat_iomap_begin(inode, offset, length, flags, iomap, false); +} + +static int exfat_write_iomap_begin(struct inode *inode, loff_t offset, lof= f_t length, + unsigned int flags, struct iomap *iomap, struct iomap *srcmap) +{ + return __exfat_iomap_begin(inode, offset, length, flags, iomap, true); +} + +const struct iomap_ops exfat_iomap_ops =3D { + .iomap_begin =3D exfat_iomap_begin, +}; + +/* + * exfat_write_iomap_end - Update the state after write + * + * Extends ->valid_size to cover the newly written range. + * Marks the inode dirty if metadata was changed. + */ +static int exfat_write_iomap_end(struct inode *inode, loff_t pos, loff_t l= ength, + ssize_t written, unsigned int flags, struct iomap *iomap) +{ + struct exfat_inode_info *ei =3D EXFAT_I(inode); + bool dirtied =3D false; + loff_t end; + + if (!written) + return 0; + + end =3D pos + written; + + if (ei->valid_size < end) { + ei->valid_size =3D end; + if (ei->zeroed_size < end) + ei->zeroed_size =3D end; + dirtied =3D true; + } + + if (dirtied || iomap->flags & IOMAP_F_SIZE_CHANGED) + mark_inode_dirty(inode); + + return written; +} + +const struct iomap_ops exfat_write_iomap_ops =3D { + .iomap_begin =3D exfat_write_iomap_begin, + .iomap_end =3D exfat_write_iomap_end, +}; + +/* + * exfat_writeback_range - Map folio during writeback + * + * Called for each folio during writeback. If the folio falls outside the + * current iomap, remaps by calling read_iomap_begin. + */ +static ssize_t exfat_writeback_range(struct iomap_writepage_ctx *wpc, + struct folio *folio, u64 offset, unsigned int len, u64 end_pos) +{ + if (offset < wpc->iomap.offset || + offset >=3D wpc->iomap.offset + wpc->iomap.length) { + int error; + + error =3D __exfat_iomap_begin(wpc->inode, offset, len, + 0, &wpc->iomap, false); + if (error) + return error; + } + + return iomap_add_to_ioend(wpc, folio, offset, end_pos, len); +} + +const struct iomap_writeback_ops exfat_writeback_ops =3D { + .writeback_range =3D exfat_writeback_range, + .writeback_submit =3D iomap_ioend_writeback_submit, +}; + +/** + * exfat_iomap_read_end_io - iomap read bio completion handler for exFAT + * @bio: bio that has completed reading + * + * exfat_iomap_begin() rounds up MAPPED extents to the block boundary of + * valid_size. This ensures that any subsequent blocks are treated as + * IOMAP_UNWRITTEN, but it also causes the "straddle block" containing + * valid_size to be read from disk. The disk data beyond valid_size in + * this block is stale and must be zeroed to prevent data leakage. + */ +static void exfat_iomap_read_end_io(struct bio *bio) +{ + int error =3D blk_status_to_errno(bio->bi_status); + struct folio_iter iter; + + bio_for_each_folio_all(iter, bio) { + struct folio *folio =3D iter.folio; + struct exfat_inode_info *ei =3D EXFAT_I(folio->mapping->host); + s64 valid_size; + loff_t pos =3D folio_pos(folio); + + valid_size =3D ei->valid_size; + if (pos + iter.offset < valid_size && + pos + iter.offset + iter.length > valid_size) + folio_zero_segment(folio, offset_in_folio(folio, valid_size), + iter.offset + iter.length); + + iomap_finish_folio_read(folio, iter.offset, iter.length, error); + } + bio_put(bio); +} + +static void exfat_iomap_bio_submit_read(const struct iomap_iter *iter, + struct iomap_read_folio_ctx *ctx) +{ + struct bio *bio =3D ctx->read_ctx; + + bio->bi_end_io =3D exfat_iomap_read_end_io; + submit_bio(bio); +} + +const struct iomap_read_ops exfat_iomap_bio_read_ops =3D { + .read_folio_range =3D iomap_bio_read_folio_range, + .submit_read =3D exfat_iomap_bio_submit_read, +}; diff --git a/fs/exfat/iomap.h b/fs/exfat/iomap.h new file mode 100644 index 000000000000..7f8dcbe20a17 --- /dev/null +++ b/fs/exfat/iomap.h @@ -0,0 +1,14 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright (c) 2026 Namjae Jeon + */ + +#ifndef _LINUX_EXFAT_IOMAP_H +#define _LINUX_EXFAT_IOMAP_H + +extern const struct iomap_ops exfat_iomap_ops; +extern const struct iomap_ops exfat_write_iomap_ops; +extern const struct iomap_writeback_ops exfat_writeback_ops; +extern const struct iomap_read_ops exfat_iomap_bio_read_ops; + +#endif /* _LINUX_EXFAT_IOMAP_H */ --=20 2.25.1 From nobody Mon May 25 05:12:30 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2E0943F789E; Mon, 18 May 2026 11:49:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779104947; cv=none; b=GAMEWr+eGKwepZ24opHLkSVk+vQ3WOjnWSSB5K014Bh1RU5ql8Iria+NMz/9WYFjyjXOcwxF8Izb84O0aRTn92sV71A0eEh2yVTIO8qdJS95cDMAgI9LBmzHbeT5r9Lyv2HzvA50Gdf8P1D4gq/UVAE6KmBo+4ZjgihFai7oer4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779104947; c=relaxed/simple; bh=ReA3VULZ6W5n5p/INWzLqgQOaamQGeifWJDf2OLvB8c=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=NUcdkmgY+vodw84CAqmcsr8d6HsqwtpQ1iNH73mx+1U693X2jH+lGyxpGmaNsopDgni6Wi2+MTsYU7J2BK3Dvdol8dlhj+xgDFR6MjwBOBCyoGF5BCNkmTrR4IlAxpHa996OqgZGX7pNGiB4QBModDNWhorLAynQ2wzyVR6CK54= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=cP3jtI25; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="cP3jtI25" Received: by smtp.kernel.org (Postfix) with ESMTPSA id A1166C2BCB7; Mon, 18 May 2026 11:49:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1779104946; bh=ReA3VULZ6W5n5p/INWzLqgQOaamQGeifWJDf2OLvB8c=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=cP3jtI25eIoMC/o6loSLCqs0x4N0p+U6xs9V5MlxlKVurdgD1qZVQ1MnchR0hwOUI Xf70Ny/k1f93gxU4NvazpStXNX/31pMA9euQq5g8yxpIMksnp01jmjRhciO3GW6KDm qIN6kBly3w2AE6P+GFVkGCQNp9WsBC/RpnZO1PxKcCVcHhb2gWWr75RbFk0CA1bOLG bz9ma4l26BXGnJvC3KRoIPPf2gi24QRO+y8n2juOzdVsBoXG/WiuCk6MCZAB4zP1pT 7VkM5CzbVNymwXEB8daZvS/DDFWs86abmQME84rI+E4+4C2L9ercGlkAKUqRNfG9dA y3rv+RAtN8ULg== From: Namjae Jeon To: sj1557.seo@samsung.com, yuezhang.mo@sony.com, brauner@kernel.org, djwong@kernel.org, hch@lst.de Cc: linux-fsdevel@vger.kernel.org, anmuxixixi@gmail.com, dxdt@dev.snart.me, chizhiling@kylinos.cn, chizhiling@163.com, linux-kernel@vger.kernel.org, charsyam@gmail.com, Namjae Jeon Subject: [PATCH v4 09/11] exfat: add iomap direct I/O support Date: Mon, 18 May 2026 20:47:03 +0900 Message-Id: <20260518114705.9601-10-linkinjeon@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20260518114705.9601-1-linkinjeon@kernel.org> References: <20260518114705.9601-1-linkinjeon@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add iomap-based direct I/O support to the exfat filesystem. This replaces the previous exfat_direct_IO() implementation that used blockdev_direct_IO() with iomap_dio_rw() interface. Reviewed-by: Christoph Hellwig Acked-by: Christoph Hellwig Signed-off-by: Namjae Jeon Acked-by: "Darrick J. Wong" --- fs/exfat/Kconfig | 1 - fs/exfat/exfat_fs.h | 1 - fs/exfat/file.c | 89 ++++++++++++++++---- fs/exfat/inode.c | 200 -------------------------------------------- fs/exfat/iomap.c | 26 ++++++ fs/exfat/iomap.h | 1 + 6 files changed, 102 insertions(+), 216 deletions(-) diff --git a/fs/exfat/Kconfig b/fs/exfat/Kconfig index e0b200902253..1fcb10c8d7bc 100644 --- a/fs/exfat/Kconfig +++ b/fs/exfat/Kconfig @@ -4,7 +4,6 @@ config EXFAT_FS tristate "exFAT filesystem support" select BUFFER_HEAD select NLS - select LEGACY_DIRECT_IO select FS_IOMAP help This allows you to mount devices formatted with the exFAT file system. diff --git a/fs/exfat/exfat_fs.h b/fs/exfat/exfat_fs.h index 2d482858bfaf..174728904dc1 100644 --- a/fs/exfat/exfat_fs.h +++ b/fs/exfat/exfat_fs.h @@ -557,7 +557,6 @@ int exfat_trim_fs(struct inode *inode, struct fstrim_ra= nge *range); /* file.c */ extern const struct file_operations exfat_file_operations; int __exfat_truncate(struct inode *inode); -void exfat_truncate(struct inode *inode); int exfat_setattr(struct mnt_idmap *idmap, struct dentry *dentry, struct iattr *attr); int exfat_getattr(struct mnt_idmap *idmap, const struct path *path, diff --git a/fs/exfat/file.c b/fs/exfat/file.c index 389ef7b36ed0..e5529d0698db 100644 --- a/fs/exfat/file.c +++ b/fs/exfat/file.c @@ -292,7 +292,7 @@ int __exfat_truncate(struct inode *inode) return 0; } =20 -void exfat_truncate(struct inode *inode) +static void exfat_truncate(struct inode *inode) { struct super_block *sb =3D inode->i_sb; struct exfat_sb_info *sbi =3D EXFAT_SB(sb); @@ -659,6 +659,56 @@ static int exfat_extend_valid_size(struct inode *inode= , loff_t new_valid_size) return ret; } =20 +static ssize_t exfat_fallback_buffered_write(struct kiocb *iocb, + struct iov_iter *from) +{ + loff_t offset =3D iocb->ki_pos, end; + ssize_t written; + int ret; + + iocb->ki_flags &=3D ~IOCB_DIRECT; + + written =3D iomap_file_buffered_write(iocb, from, &exfat_write_iomap_ops, + NULL, NULL); + if (written < 0) + return written; + + end =3D iocb->ki_pos + written - 1; + ret =3D filemap_write_and_wait_range(iocb->ki_filp->f_mapping, + offset, end); + if (ret) + return -EIO; + + invalidate_mapping_pages(iocb->ki_filp->f_mapping, + offset >> PAGE_SHIFT, + end >> PAGE_SHIFT); + + return written; +} + +static ssize_t exfat_dio_write_iter(struct kiocb *iocb, struct iov_iter *f= rom) +{ + ssize_t ret; + + ret =3D iomap_dio_rw(iocb, from, &exfat_write_iomap_ops, + &exfat_write_dio_ops, 0, NULL, 0); + if (ret =3D=3D -ENOTBLK) + ret =3D 0; + else if (ret < 0) + return ret; + + if (iov_iter_count(from)) { + ssize_t written; + + written =3D exfat_fallback_buffered_write(iocb, from); + if (written < 0) + return written; + ret +=3D written; + } + + return ret; +} + static ssize_t exfat_file_write_iter(struct kiocb *iocb, struct iov_iter *= iter) { ssize_t ret; @@ -683,16 +733,6 @@ static ssize_t exfat_file_write_iter(struct kiocb *ioc= b, struct iov_iter *iter) if (ret <=3D 0) goto unlock; =20 - if (iocb->ki_flags & IOCB_DIRECT) { - unsigned long align =3D pos | iov_iter_alignment(iter); - - if (!IS_ALIGNED(align, i_blocksize(inode)) && - !IS_ALIGNED(align, bdev_logical_block_size(inode->i_sb->s_bdev))) { - ret =3D -EINVAL; - goto unlock; - } - } - err =3D file_modified(iocb->ki_filp); if (err) { ret =3D err; @@ -711,7 +751,7 @@ static ssize_t exfat_file_write_iter(struct kiocb *iocb= , struct iov_iter *iter) } =20 if (iocb->ki_flags & IOCB_DIRECT) - ret =3D __generic_file_write_iter(iocb, iter); + ret =3D exfat_dio_write_iter(iocb, iter); else ret =3D iomap_file_buffered_write(iocb, iter, &exfat_write_iomap_ops, NULL, NULL); @@ -741,11 +781,24 @@ static ssize_t exfat_file_write_iter(struct kiocb *io= cb, struct iov_iter *iter) static ssize_t exfat_file_read_iter(struct kiocb *iocb, struct iov_iter *i= ter) { struct inode *inode =3D file_inode(iocb->ki_filp); + ssize_t ret; =20 if (unlikely(exfat_forced_shutdown(inode->i_sb))) return -EIO; =20 - return generic_file_read_iter(iocb, iter); + inode_lock_shared(inode); + + if (iocb->ki_flags & IOCB_DIRECT) { + file_accessed(iocb->ki_filp); + ret =3D iomap_dio_rw(iocb, iter, &exfat_iomap_ops, NULL, 0, + NULL, 0); + } else { + ret =3D generic_file_read_iter(iocb, iter); + } + + inode_unlock_shared(inode); + + return ret; } =20 static vm_fault_t exfat_page_mkwrite(struct vm_fault *vmf) @@ -850,10 +903,18 @@ static ssize_t exfat_splice_read(struct file *in, lof= f_t *ppos, =20 static int exfat_file_open(struct inode *inode, struct file *filp) { + int err; + if (unlikely(exfat_forced_shutdown(inode->i_sb))) return -EIO; =20 - return generic_file_open(inode, filp); + err =3D generic_file_open(inode, filp); + if (err) + return err; + + filp->f_mode |=3D FMODE_CAN_ODIRECT; + + return 0; } =20 const struct file_operations exfat_file_operations =3D { diff --git a/fs/exfat/inode.c b/fs/exfat/inode.c index 6083ccef9408..e58561d65294 100644 --- a/fs/exfat/inode.c +++ b/fs/exfat/inode.c @@ -224,151 +224,6 @@ int exfat_map_cluster(struct inode *inode, unsigned i= nt clu_offset, return 0; } =20 -static int exfat_get_block(struct inode *inode, sector_t iblock, - struct buffer_head *bh_result, int create) -{ - struct exfat_inode_info *ei =3D EXFAT_I(inode); - struct super_block *sb =3D inode->i_sb; - struct exfat_sb_info *sbi =3D EXFAT_SB(sb); - unsigned long max_blocks =3D bh_result->b_size >> inode->i_blkbits; - int err =3D 0; - unsigned long mapped_blocks =3D 0; - unsigned int cluster, sec_offset, count; - sector_t last_block; - sector_t phys =3D 0; - sector_t valid_blks; - loff_t i_size; - - mutex_lock(&sbi->s_lock); - i_size =3D i_size_read(inode); - last_block =3D exfat_bytes_to_block_round_up(sb, i_size); - if (iblock >=3D last_block && !create) - goto done; - - /* Is this block already allocated? */ - count =3D exfat_bytes_to_cluster_round_up(sbi, bh_result->b_size); - err =3D exfat_map_cluster(inode, iblock >> sbi->sect_per_clus_bits, - &cluster, &count, create, NULL); - if (err) { - if (err !=3D -ENOSPC) - exfat_fs_error_ratelimit(sb, - "failed to bmap (inode : %p iblock : %llu, err : %d)", - inode, (unsigned long long)iblock, err); - goto unlock_ret; - } - - if (cluster =3D=3D EXFAT_EOF_CLUSTER) - goto done; - - /* sector offset in cluster */ - sec_offset =3D iblock & (sbi->sect_per_clus - 1); - - phys =3D exfat_cluster_to_sector(sbi, cluster) + sec_offset; - mapped_blocks =3D ((unsigned long)count << sbi->sect_per_clus_bits) - sec= _offset; - max_blocks =3D min(mapped_blocks, max_blocks); - - map_bh(bh_result, sb, phys); - if (buffer_delay(bh_result)) - clear_buffer_delay(bh_result); - - /* - * In most cases, we just need to set bh_result to mapped, unmapped - * or new status as follows: - * 1. i_size =3D=3D valid_size - * 2. write case (create =3D=3D 1) - * 3. direct_read (!bh_result->b_folio) - * -> the unwritten part will be zeroed in exfat_direct_IO() - * - * Otherwise, in the case of buffered read, it is necessary to take - * care the last nested block if valid_size is not equal to i_size. - */ - if (i_size =3D=3D ei->valid_size || create || !bh_result->b_folio) - valid_blks =3D exfat_bytes_to_block_round_up(sb, ei->valid_size); - else - valid_blks =3D exfat_bytes_to_block(sb, ei->valid_size); - - /* The range has been fully written, map it */ - if (iblock + max_blocks < valid_blks) - goto done; - - /* The range has been partially written, map the written part */ - if (iblock < valid_blks) { - max_blocks =3D valid_blks - iblock; - goto done; - } - - /* The area has not been written, map and mark as new for create case */ - if (create) { - set_buffer_new(bh_result); - ei->valid_size =3D exfat_block_to_bytes(sb, iblock + max_blocks); - mark_inode_dirty(inode); - goto done; - } - - /* - * The area has just one block partially written. - * In that case, we should read and fill the unwritten part of - * a block with zero. - */ - if (bh_result->b_folio && iblock =3D=3D valid_blks && - (ei->valid_size & (sb->s_blocksize - 1))) { - loff_t size, pos; - void *addr; - - max_blocks =3D 1; - - /* - * No buffer_head is allocated. - * (1) bmap: It's enough to set blocknr without I/O. - * (2) read: The unwritten part should be filled with zero. - * If a folio does not have any buffers, - * let's returns -EAGAIN to fallback to - * block_read_full_folio() for per-bh IO. - */ - if (!folio_buffers(bh_result->b_folio)) { - err =3D -EAGAIN; - goto done; - } - - pos =3D exfat_block_to_bytes(sb, iblock); - size =3D ei->valid_size - pos; - addr =3D folio_address(bh_result->b_folio) + - offset_in_folio(bh_result->b_folio, pos); - - /* Check if bh->b_data points to proper addr in folio */ - if (bh_result->b_data !=3D addr) { - exfat_fs_error_ratelimit(sb, - "b_data(%p) !=3D folio_addr(%p)", - bh_result->b_data, addr); - err =3D -EINVAL; - goto done; - } - - /* Read a block */ - err =3D bh_read(bh_result, 0); - if (err < 0) - goto done; - - /* Zero unwritten part of a block */ - memset(bh_result->b_data + size, 0, bh_result->b_size - size); - err =3D 0; - goto done; - } - - /* - * The area has not been written, clear mapped for read/bmap cases. - * If so, it will be filled with zero without reading from disk. - */ - clear_buffer_mapped(bh_result); -done: - bh_result->b_size =3D exfat_block_to_bytes(sb, max_blocks); - if (err < 0) - clear_buffer_mapped(bh_result); -unlock_ret: - mutex_unlock(&sbi->s_lock); - return err; -} - static int exfat_read_folio(struct file *file, struct folio *folio) { struct iomap_read_folio_ctx ctx =3D { @@ -415,60 +270,6 @@ static int exfat_writepages(struct address_space *mapp= ing, return iomap_writepages(&wpc); } =20 -static void exfat_write_failed(struct address_space *mapping, loff_t to) -{ - struct inode *inode =3D mapping->host; - - if (to > i_size_read(inode)) { - truncate_pagecache(inode, i_size_read(inode)); - inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode)); - exfat_truncate(inode); - } -} - -static ssize_t exfat_direct_IO(struct kiocb *iocb, struct iov_iter *iter) -{ - struct address_space *mapping =3D iocb->ki_filp->f_mapping; - struct inode *inode =3D mapping->host; - struct exfat_inode_info *ei =3D EXFAT_I(inode); - loff_t pos =3D iocb->ki_pos; - loff_t size =3D pos + iov_iter_count(iter); - int rw =3D iov_iter_rw(iter); - ssize_t ret; - - /* - * Need to use the DIO_LOCKING for avoiding the race - * condition of exfat_get_block() and ->truncate(). - */ - ret =3D blockdev_direct_IO(iocb, inode, iter, exfat_get_block); - if (ret < 0) { - if (rw =3D=3D WRITE && ret !=3D -EIOCBQUEUED) - exfat_write_failed(mapping, size); - - return ret; - } - - size =3D pos + ret; - - if (rw =3D=3D WRITE) { - /* - * If the block had been partially written before this write, - * ->valid_size will not be updated in exfat_get_block(), - * update it here. - */ - if (ei->valid_size < size) { - ei->valid_size =3D size; - mark_inode_dirty(inode); - } - } else if (pos < ei->valid_size && ei->valid_size < size) { - /* zero the unwritten part in the partially written block */ - iov_iter_revert(iter, size - ei->valid_size); - iov_iter_zero(size - ei->valid_size, iter); - } - - return ret; -} - static sector_t exfat_aop_bmap(struct address_space *mapping, sector_t blo= ck) { sector_t blocknr; @@ -491,7 +292,6 @@ static const struct address_space_operations exfat_aops= =3D { .error_remove_folio =3D generic_error_remove_folio, .release_folio =3D iomap_release_folio, .invalidate_folio =3D iomap_invalidate_folio, - .direct_IO =3D exfat_direct_IO, }; =20 static inline unsigned long exfat_hash(loff_t i_pos) diff --git a/fs/exfat/iomap.c b/fs/exfat/iomap.c index b360fadc9d58..c6841ad0b1cc 100644 --- a/fs/exfat/iomap.c +++ b/fs/exfat/iomap.c @@ -12,6 +12,32 @@ #include "exfat_fs.h" #include "iomap.h" =20 +/* + * exfat_file_write_dio_end_io - Direct I/O write completion handler + * + * Updates i_size if the write extended the file. Called from the dio layer + * after I/O completion. + */ +static int exfat_file_write_dio_end_io(struct kiocb *iocb, ssize_t size, + int error, unsigned int flags) +{ + struct inode *inode =3D file_inode(iocb->ki_filp); + + if (error) + return error; + + if (size && i_size_read(inode) < iocb->ki_pos + size) { + i_size_write(inode, iocb->ki_pos + size); + mark_inode_dirty(inode); + } + + return 0; +} + +const struct iomap_dio_ops exfat_write_dio_ops =3D { + .end_io =3D exfat_file_write_dio_end_io, +}; + static int __exfat_iomap_begin(struct inode *inode, loff_t offset, loff_t = length, unsigned int flags, struct iomap *iomap, bool may_alloc) { diff --git a/fs/exfat/iomap.h b/fs/exfat/iomap.h index 7f8dcbe20a17..830388f386f4 100644 --- a/fs/exfat/iomap.h +++ b/fs/exfat/iomap.h @@ -6,6 +6,7 @@ #ifndef _LINUX_EXFAT_IOMAP_H #define _LINUX_EXFAT_IOMAP_H =20 +extern const struct iomap_dio_ops exfat_write_dio_ops; extern const struct iomap_ops exfat_iomap_ops; extern const struct iomap_ops exfat_write_iomap_ops; extern const struct iomap_writeback_ops exfat_writeback_ops; --=20 2.25.1 From nobody Mon May 25 05:12:30 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8136F3F9299; Mon, 18 May 2026 11:49:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779104950; cv=none; b=aKubFBD4IHLSBekqW4+rg7yz1fkxIClR3pzNlus1cxYa7vfpPMk/dFfRtcNTQx4mNs7NLj4wwQYzVKj+s+mqOFhp4V50gVORVlNuJexpJpL6WQf0FsQ5Qaa9n/TBGlvx5pLRF8IZPmSNF3CmNKqCkdRp8yrUmQ/gQixRP+9J0nM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779104950; c=relaxed/simple; bh=u4ifEVE/cXM9Cx9fsWHu7TnRtG9B3S8T13PH0eZWAGE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=hQPETv74fW69NPLuM/JMyg5mhLTA/zt2OmZDAThlI2bZPQY9TSJRVbqvPGQQiYHKml/mBJ2atqiputLEKJI4hHSS8/1FLuYHVxnCRva4Qiu2o/hTab5wCeTeuUgJVEfyWPO/qWy5pFOgwqiJq1wG/2H0TXUdpzPFMwxJurWy0G4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=VhL9Nmtz; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="VhL9Nmtz" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 507EFC2BCC6; Mon, 18 May 2026 11:49:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1779104950; bh=u4ifEVE/cXM9Cx9fsWHu7TnRtG9B3S8T13PH0eZWAGE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=VhL9NmtzFN7vdkYNfcFuBZ7IwWJMsoGi1AnBvWp9JWwM/Pcq2YJ7VwaX63h9LfRPD cor/Ffjmul0FAsv/5v7dMoCXg+9QKNlcWifR3l9umBEs0djHVmGovPBmtvvOTQWXez TMGmpPue8DPSPTys39svpvt1xQx7srMHOVxJjwjjtV3upjwPSGKaTEM2p6ro7G7VaJ wVVFJOdG2+ME5pBBND9hw9qygOdhqfWc62KjZPvAYylDpgrJX9DJ0Hpli0Nq3vqbYM BR0l73nfW8YJhUA+DdV2TfrP+rrgOBcgeHmgnY5+gRQfxLbxHEuYntqzmZ+9mDGUJi wHCsZVOpXBxKA== From: Namjae Jeon To: sj1557.seo@samsung.com, yuezhang.mo@sony.com, brauner@kernel.org, djwong@kernel.org, hch@lst.de Cc: linux-fsdevel@vger.kernel.org, anmuxixixi@gmail.com, dxdt@dev.snart.me, chizhiling@kylinos.cn, chizhiling@163.com, linux-kernel@vger.kernel.org, charsyam@gmail.com, Namjae Jeon Subject: [PATCH v4 10/11] exfat: add support for SEEK_HOLE and SEEK_DATA in llseek Date: Mon, 18 May 2026 20:47:04 +0900 Message-Id: <20260518114705.9601-11-linkinjeon@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20260518114705.9601-1-linkinjeon@kernel.org> References: <20260518114705.9601-1-linkinjeon@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Adds exfat_file_llseek() that implements these whence values via the iomap layer (iomap_seek_hole() and iomap_seek_data()) using the existing exfat_read_iomap_ops. Unlike many other modern filesystems, exFAT does not support sparse files with unallocated clusters (holes). In exFAT, clusters are always fully allocated once they are written or preallocated. In addition, exFAT maintains a separate "Valid Data Length" (valid_size) that is distinct from the logical file size. This affects how holes are reported during seeking. In exfat_iomap_begin(), ranges where the offset is greater than or equal to ei->valid_size are mapped as IOMAP_UNWRITTEN, while ranges below valid_size are mapped as IOMAP_MAPPED. This mapping behavior is used by the iomap seek functions to correctly report SEEK_HOLE and SEEK_DATA positions. - Ranges with offset >=3D ei->valid_size are mapped as IOMAP_HOLE. - Ranges with offset < ei->valid_size are mapped as IOMAP_MAPPED. Reviewed-by: Christoph Hellwig Acked-by: Christoph Hellwig Signed-off-by: Namjae Jeon Acked-by: "Darrick J. Wong" --- fs/exfat/file.c | 25 ++++++++++++++++++++++++- fs/exfat/iomap.c | 32 ++++++++++++++++++++++++++++---- 2 files changed, 52 insertions(+), 5 deletions(-) diff --git a/fs/exfat/file.c b/fs/exfat/file.c index e5529d0698db..d9c4e8dd9fd8 100644 --- a/fs/exfat/file.c +++ b/fs/exfat/file.c @@ -917,9 +917,32 @@ static int exfat_file_open(struct inode *inode, struct= file *filp) return 0; } =20 +static loff_t exfat_file_llseek(struct file *file, loff_t offset, int when= ce) +{ + struct inode *inode =3D file->f_mapping->host; + + switch (whence) { + case SEEK_HOLE: + inode_lock_shared(inode); + offset =3D iomap_seek_hole(inode, offset, &exfat_iomap_ops); + inode_unlock_shared(inode); + break; + case SEEK_DATA: + inode_lock_shared(inode); + offset =3D iomap_seek_data(inode, offset, &exfat_iomap_ops); + inode_unlock_shared(inode); + break; + default: + return generic_file_llseek(file, offset, whence); + } + if (offset < 0) + return offset; + return vfs_setpos(file, offset, inode->i_sb->s_maxbytes); +} + const struct file_operations exfat_file_operations =3D { .open =3D exfat_file_open, - .llseek =3D generic_file_llseek, + .llseek =3D exfat_file_llseek, .read_iter =3D exfat_file_read_iter, .write_iter =3D exfat_file_write_iter, .unlocked_ioctl =3D exfat_ioctl, diff --git a/fs/exfat/iomap.c b/fs/exfat/iomap.c index c6841ad0b1cc..61a1b6aba860 100644 --- a/fs/exfat/iomap.c +++ b/fs/exfat/iomap.c @@ -98,12 +98,36 @@ static int __exfat_iomap_begin(struct inode *inode, lof= f_t offset, loff_t length iomap->flags =3D IOMAP_F_ZERO_TAIL; } } else { + /* + * valid_size is tracked in byte granularity and + * marks the exact boundary between valid data and + * holes (or unwritten space). + * + * When IOMAP_REPORT is set (used by lseek(SEEK_HOLE) + * and SEEK_DATA), we return IOMAP_HOLE. This allows + * iomap_seek_hole_iter() to directly return the + * precise byte position. + * + * For normal I/O paths (without IOMAP_REPORT) we + * return IOMAP_UNWRITTEN so the write path can + * distinguish it from a real hole. + */ if (offset >=3D ei->valid_size) { - iomap->type =3D IOMAP_UNWRITTEN; + iomap->type =3D flags & IOMAP_REPORT ? + IOMAP_HOLE : IOMAP_UNWRITTEN; } else if (offset + iomap->length > ei->valid_size) { - iomap->length =3D round_up(ei->valid_size, - i_blocksize(inode)) - - iomap->offset; + if (flags & IOMAP_REPORT) { + /* + * For SEEK_HOLE/SEEK_DATA, clip the length + * to the exact byte boundary (valid_size). + * This ensures the caller gets the precise + * hole position in byte units. + */ + iomap->length =3D ei->valid_size - iomap->offset; + } else + iomap->length =3D round_up(ei->valid_size, + i_blocksize(inode)) - + iomap->offset; } } =20 --=20 2.25.1 From nobody Mon May 25 05:12:30 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E60CC3F8889; Mon, 18 May 2026 11:49:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779104955; cv=none; b=APQHlq+ziyWVvygxEMDTcotc0rc/q0CqSUMkJnaMRCP9K3wc1daEhxj4TBjt65kmVcw1y1AttIo5z4DZ3yUWphxMsqyc4Uj0sou4BIEqHnwLdtK1PxGOxLM2TwhAuBuUedtMxgTqzuvPjq4McLhEqIGwbo3eWVUY+mseDtXS0V8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779104955; c=relaxed/simple; bh=hzb+y/Rb6afK82CIBGhJEoM0p7JFocu3IrIsx6m+2DE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=cNZShiPNtng3TwbK5inppJcJTAroRV5/as8Ny8PXG8skb3GqeQ5s0DnS9wTt7e/mMOf7BEX/VP8TfUSUPqRPgA71kPbBK0UL7UL5nZzqH/X/oY4QfLteQKI5J89gN0FUI3R8wco6bqMvb/+JnmTkEbKFpLYGteWIqIlBv39G12o= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=fhMr9JQg; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="fhMr9JQg" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 78092C2BCB7; Mon, 18 May 2026 11:49:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1779104954; bh=hzb+y/Rb6afK82CIBGhJEoM0p7JFocu3IrIsx6m+2DE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=fhMr9JQgJgWJhidgEFNHtqL3ezIyWIMClg4X8XS3vFVWfTcBkYqeRQcstHKj/UMI+ THxQs/VOaFejNYpF3J9O99OLvseA4XrttnYlgtzjOrup7wGOMh6OEYj4OgeKBLX5tT 6QBG/7z4zbNnhneDazlouaPL/y6WHqri3/dNSEyjdOzUcp6CeEsmErwAGshWrZtfHj cuV+vpRDwLxJc4/PmQa5ZLPp/qYmNXAXJlOT/O0Kprxui/DVxJannylK1L/mbzNdxX xCh0d/WJpURdOucudonoYay6X19o6UOL6/BZ2aQIAIz/AaganvdAl/M4liX+R6/ftb BQVWem2hMYT+w== From: Namjae Jeon To: sj1557.seo@samsung.com, yuezhang.mo@sony.com, brauner@kernel.org, djwong@kernel.org, hch@lst.de Cc: linux-fsdevel@vger.kernel.org, anmuxixixi@gmail.com, dxdt@dev.snart.me, chizhiling@kylinos.cn, chizhiling@163.com, linux-kernel@vger.kernel.org, charsyam@gmail.com, Namjae Jeon Subject: [PATCH v4 11/11] exfat: make exfat_truncate() return error code Date: Mon, 18 May 2026 20:47:05 +0900 Message-Id: <20260518114705.9601-12-linkinjeon@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20260518114705.9601-1-linkinjeon@kernel.org> References: <20260518114705.9601-1-linkinjeon@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Convert exfat_truncate() to return int, and make it return the error code on failure. Update exfat_setattr() to handle the returned error appropriately. Acked-by: Christoph Hellwig Signed-off-by: Namjae Jeon Acked-by: "Darrick J. Wong" --- fs/exfat/file.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/fs/exfat/file.c b/fs/exfat/file.c index d9c4e8dd9fd8..1271202b35ed 100644 --- a/fs/exfat/file.c +++ b/fs/exfat/file.c @@ -292,7 +292,7 @@ int __exfat_truncate(struct inode *inode) return 0; } =20 -static void exfat_truncate(struct inode *inode) +static int exfat_truncate(struct inode *inode) { struct super_block *sb =3D inode->i_sb; struct exfat_sb_info *sbi =3D EXFAT_SB(sb); @@ -315,6 +315,8 @@ static void exfat_truncate(struct inode *inode) inode->i_blocks =3D round_up(i_size_read(inode), sbi->cluster_size) >> 9; write_size: mutex_unlock(&sbi->s_lock); + + return err; } =20 int exfat_getattr(struct mnt_idmap *idmap, const struct path *path, @@ -400,7 +402,7 @@ int exfat_setattr(struct mnt_idmap *idmap, struct dentr= y *dentry, * __exfat_write_inode() is called from exfat_truncate(), inode * is already written by it, so mark_inode_dirty() is unneeded. */ - exfat_truncate(inode); + error =3D exfat_truncate(inode); up_write(&EXFAT_I(inode)->truncate_lock); } else mark_inode_dirty(inode); --=20 2.25.1