From nobody Fri Jun 12 18:34:01 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 E651238D6A9; Wed, 13 May 2026 11:22:08 +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=1778671329; cv=none; b=AEoVlatlZLaYYtNWwItkaTQ+a+5Ba3aUvQy3GSwQVnf+YOtthZFNp4jfP8rI/7lmQls0V2Gwjh6dTieaSj1AuPSB/lufFxxCNk+e9uS01O+sXx5JbJvokz07hT4WXXHqLXXZOWi/BI/CYFg48U2cXbbb7dwQJ/i6ywx7XQU2QGs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778671329; c=relaxed/simple; bh=yK0uhnIEaI92euI5l0VwJVWOD3p/CtLYSGo569dfZRw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Yzn0g1YdSQqCub0qWhy8HDMrfVHqDcudr3JGs6eklqAPchsGIcas+Yn/374SoqRa2yrJ4k2jzLmItW3gHMWBZMthVAG0ggN22BV71TrSAdFBbeLoItMYTbzsvPCVOSAZWh5bxo8Rpj3yrkP0hR98bMbylik4iGJ/mGadiEFWEn8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=cc3nrS2p; 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="cc3nrS2p" Received: by smtp.kernel.org (Postfix) with ESMTPSA id C1CD8C2BCB8; Wed, 13 May 2026 11:22:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1778671328; bh=yK0uhnIEaI92euI5l0VwJVWOD3p/CtLYSGo569dfZRw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=cc3nrS2p32IV5JHv0JzA2mcvH7prc2N6yKrAcSBkcmIUFpFuGPPzO7VL6yTWNIS46 0mBtgp/1ohI4Fs309iNG8kc7bKSl4r178G70xAleoEvdHaPY4zJE8/vVJBoAdovm+g CcP464nAz8DBIEXGi3WAhIQ8kRNy8COGIPrMPkwiMDRCydq4a6/JVxQRpMKiX4trf+ S/KrSnD/wCIg6YFkbfbuBfVLHFsCWHZYEUdSXR5t9HrKbwgcMhBh6b+Rb6oqTuV8vH uKMyW/J193HvBJLHNv/HR79vgI5LeaxMq+XjnVaFCPOkn3ufINrTQ0RjH79OfxLAVv 4tbMUiPXR7bdA== 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, Namjae Jeon Subject: [PATCH v3 01/11] iomap: introduce IOMAP_F_ZERO_TAIL flag Date: Wed, 13 May 2026 20:21:46 +0900 Message-Id: <20260513112156.9122-2-linkinjeon@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20260513112156.9122-1-linkinjeon@kernel.org> References: <20260513112156.9122-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. Signed-off-by: Namjae Jeon Acked-by: Christoph Hellwig Reviewed-by: Christoph Hellwig --- 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..9ae136a9fcd4 100644 --- a/fs/iomap/buffered-io.c +++ b/fs/iomap/buffered-io.c @@ -837,6 +837,7 @@ static int __iomap_write_begin(const struct iomap_iter = *iter, folio_zero_segments(folio, poff, from, to, poff + plen); } else { int status; + const struct iomap *iomap =3D iomap_iter_srcmap(iter); =20 if (iter->flags & IOMAP_NOWAIT) return -EAGAIN; @@ -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 Fri Jun 12 18:34:01 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 658CC37DAA8; Wed, 13 May 2026 11:22:13 +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=1778671333; cv=none; b=DtWzygXqaIBPBHU1W0cmbwSiOI02RLOkYHl1KgnkvHKSG3j6jHwnV/kuonV+6d0LV0vo/vATUpfpd3An0KC+WeOh+q53UQnWq5Smxude28u+yKUT3ROebFncAkOhEDeyEw4CitStmaVW0wz0svXlmsLYhTkjrEcSaJ/6T2vm7Js= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778671333; c=relaxed/simple; bh=BunD0IGd8E7s7nKx/wPCkJ3Jkj26qFG0YM1kuyPhM80=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=oAZ0tRqnjBQXUt3FdtFMd3HA8yXdCrwIhZjZExsHcJLaXz714PdCd1kl0B3GQV61rLMyQbtx6C6k+eS0I6HX8UVim7KHE9tpUd9VfyczgdvUMI5U7NFGAK6iAnNFsRkPnPNxauJu9t/IWoCZQKcZ//huO6QX+NwtyhcMX2SGgc0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=pj9+cC5a; 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="pj9+cC5a" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4CA43C2BCFA; Wed, 13 May 2026 11:22:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1778671333; bh=BunD0IGd8E7s7nKx/wPCkJ3Jkj26qFG0YM1kuyPhM80=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=pj9+cC5amO4yWYmoRnoglKm1JVa3T9+Jt1Hw3kaLo+grNwXj+KXv3V8Bx6PKcxzEx DPPXQbmUuuaarwtSlLKLNyGVzD14yGhOkNmTvqmxYSge2aOvMmTfT6hNgKgF9MpIyk bw6A7SXPxSoGamrMTsfSAjIeVADpFQ9u6jzp1OmdKNnXq3Vv8AozFWjf1tyBdZEkyk jinFGoWOJnMsgexsCJddU+hw7s/0qy9NlkII9h4Y/lhZ3eQz6c7y/fnrRfTVh/fQZP Cx1dPx3O8DwOM5x/iTugg6T++OrQzwF9gfF/Or+vkGll9gR8SlFZoK1dlA92p7L8bE XGGnWC9kGcHRg== 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, Namjae Jeon Subject: [PATCH v3 02/11] exfat: replace unsafe macros with static inline functions Date: Wed, 13 May 2026 20:21:47 +0900 Message-Id: <20260513112156.9122-3-linkinjeon@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20260513112156.9122-1-linkinjeon@kernel.org> References: <20260513112156.9122-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. Signed-off-by: Namjae Jeon Acked-by: Christoph Hellwig --- 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 Fri Jun 12 18:34:01 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 0601534751B; Wed, 13 May 2026 11:22:16 +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=1778671337; cv=none; b=s3kc9olP8JrCOKp783/drO/TjkSHUhtr6wu92Y5yi1FQJmGStkm4U+R6hRsfeGjmknfugQLXwmMQwkpL9c0SxezYHepiuEGKMdCJ5VBfWQfu3Jmrot3u+3xTrvPb1Ut2fdkjijJq9eWggfLL2vWq3f1PX3sL2qmJX+I8iRognIU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778671337; c=relaxed/simple; bh=GAmD2Yh1HN4OUfgVgM4w/GzKMor/Yl/+uXcUhBDJYqU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=SUpmAxgftTGUbY0Wr5lnOXLDv6U4ZWy07GCkf8dYXirpgi+6uWNbD9fRzCuYkCqL/wg6mn5tgWgdiW7x7K1iB6kO5bYDNOoZ2cJdn+n5bb9bk0iO3lqLiYhgEwtWuSz0RyvGNwlggzF1mIgIjCQW/mG/dFgUfiuFA9CF6lpNAhA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=lM8YUzdN; 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="lM8YUzdN" Received: by smtp.kernel.org (Postfix) with ESMTPSA id DD3D6C2BCB7; Wed, 13 May 2026 11:22:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1778671336; bh=GAmD2Yh1HN4OUfgVgM4w/GzKMor/Yl/+uXcUhBDJYqU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=lM8YUzdNqlP7/PLP1h1AwVPCxJb7ZLfoNXb10jpyBbfULQyU5A5Idw5gig5HbM58z SG0W00VRL48iSI5AJDnqUjHmz4z/ZVRc5lqrHsZ8+/NNXvzUNv5J58EJnn1TQ33v3C kglvivWujJektI0DQIscaVKltxoTCxpoN/LGHR9Hg54R9nI0Qh1mlR+yGbFdd5zli+ wlr0boHsEKdMzKnqUi9fLIfSZ8GjX0CLLiBRTxgzdKGw8Nkn51Ndgqj/9o0SKPBMvX NfKejBDFT8CpZLsnrAJoyCOp9hn0Z+4LPpCbrQYVrNGIK8opVozw8QvGY/H83sFp9F VCSEV02aICR3A== 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, Namjae Jeon Subject: [PATCH v3 03/11] exfat: add balloc parameter to exfat_map_cluster() for iomap support Date: Wed, 13 May 2026 20:21:48 +0900 Message-Id: <20260513112156.9122-4-linkinjeon@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20260513112156.9122-1-linkinjeon@kernel.org> References: <20260513112156.9122-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. Signed-off-by: Namjae Jeon Acked-by: Christoph Hellwig --- 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 Fri Jun 12 18:34:01 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 1B4B62C21FD; Wed, 13 May 2026 11:22:19 +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=1778671342; cv=none; b=MJrX9h5NFq3IaAD0IZP86VMonHCn+sUdsANUc7bityDaOxPTZOFrZVqHzUv80iQibTva5JObsOFmkg3u++8O1fxIla9N/+sUjRODOEqB9o33ne6fnnwDgfekUSITIxZNrsWv8lncY0/5fp+350BESmWD5UGv7FirLceQMxcMSfw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778671342; c=relaxed/simple; bh=ZSAbrFmW3LCkx122pNqtv4L5RxOn49pWRO7AYEhhoTc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=D54Y6h31Om4888w8lbM3uB10NVN/53stJhDUYYa4Np3LMm5eigstJkqkynkbojKj0rT7fnKFhNzZpPd9ZViqjLNwd9ZAcC56th5eRJla80Gp1jj8qohSfRestNw8gexrJVcxe52VbWtMKGv2hAWCLvJA0PL7iVJdaAnW3kdJeDg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=r4tztykk; 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="r4tztykk" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 04338C2BCFB; Wed, 13 May 2026 11:22:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1778671339; bh=ZSAbrFmW3LCkx122pNqtv4L5RxOn49pWRO7AYEhhoTc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=r4tztykkAOJoMj5MVPbGjiiJL4h8ozh9IRrUwIBiZyWUQFgZCd24IP8ci/0cbvGGW vehIsy7DyhpFxJYHTsi+DJg/3dIJl+94B3HcH4JZuiHhDAxosIEU0TMad5j5qZ2bHE whgoSCfOluw0xBZ6GScvqFpVu1gpaRFFdzcz8yWNt23gINIeRFr3MV0ROZckKgzyx3 MVpLn/c+p5SRzvA5CkKTaPPPljX66EMXYo0PsYEPrBrI31k+Qk+l1L8d6wEIPE2+KF kZjtsnfKFV3abLxRTV5D6H6lgG0ZQ9BjrKmTNs081YLM4eYE59Mmv95/Z26jU0y9A6 g95Edw0qprs2g== 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, Namjae Jeon Subject: [PATCH v3 04/11] exfat: add exfat_file_open() Date: Wed, 13 May 2026 20:21:49 +0900 Message-Id: <20260513112156.9122-5-linkinjeon@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20260513112156.9122-1-linkinjeon@kernel.org> References: <20260513112156.9122-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. Signed-off-by: Namjae Jeon Acked-by: Christoph Hellwig --- 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 Fri Jun 12 18:34:01 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 DEDA03EFD0C; Wed, 13 May 2026 11:22:22 +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=1778671343; cv=none; b=HBWZkjtJGu1ixg3RHZjEcCb/AfI6ACuswojl2QSWIKNSUxJOU9Fq/INNQO57/71aTQLVlFFO32j3bjHtJQqN0k/i+KQqIy6iduqI6Nu5WgniJhhOPgUjW2edZv5dG27tAaNnAp4RlLh742GGijUEpORQu1O7C5cNfgJaX6sTirA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778671343; c=relaxed/simple; bh=K/BCNpKLGFxnmuFMCfi3bztuoQKlyjzP4tCl9NIDCyM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Y2WDZwc0RRY990q5XCHyPrg6ZncFupYzqSwmcUn/i3nwd6Xy4Or9jRYHerpiQZRRAnz7bxh/5mZC7aTBqt0a6gH22zmxg75xtl50n1Fsy9JAfqReTP9wn5VUbhx1KhzNogeCfVcG+xTL3rRvPBUn6UqTwBs7jc2x6P+z1925JsE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=EOqw4BUt; 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="EOqw4BUt" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 13EC5C2BCB7; Wed, 13 May 2026 11:22:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1778671342; bh=K/BCNpKLGFxnmuFMCfi3bztuoQKlyjzP4tCl9NIDCyM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=EOqw4BUtpUFzD7Hrz6tdR7fDDtODlbcoRtWbOrDpmsjbgsxhFiwIN3Dr2FiqJD+ZF zh/EsHZ7iO7bJiQ5Eb91r6ESi50C1hIU49xH2Rdfbl+j5iHXo5Z2AUAc9wGS2hDWWI tPNn0Uk8mlCreMonyyRF8VV1O87gvLlHtUSyhZaWbCdSfpwpOHJv6kR3DWoNvxURLu Qz2b4hadaxRAWwJPrBeAhdCHIkWt/9IgzG/E53qQ5OKp5gKE67kDacmPXw1fzLR98f lO0E982KC4lmiICxU1aB/iilI0+WsLyQ/gLzA9gglTceWDMNjTVpK2o2zJiVFeh6XL XlmJrWkqYBUPw== 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, Namjae Jeon Subject: [PATCH v3 05/11] exfat: add support for multi-cluster allocation Date: Wed, 13 May 2026 20:21:50 +0900 Message-Id: <20260513112156.9122-6-linkinjeon@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20260513112156.9122-1-linkinjeon@kernel.org> References: <20260513112156.9122-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. Signed-off-by: Namjae Jeon Acked-by: Christoph Hellwig --- 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 Fri Jun 12 18:34:01 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 390992C21FD; Wed, 13 May 2026 11:22:26 +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=1778671348; cv=none; b=mpvwrerdezBhSyLy08qlZLvay77QsVRj7p3Idf6Bc7N5u7So59Qbhk9pRv2+nFVxuR9VXFfcWf69nhG+B6pI5FvXJRs464tjDXNojCWlgckyonhvUDZcE762Wb2d5zBhasCflzmqPYgNeiKtAEOCho9wH13DgyrSgpM6jCZj670= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778671348; c=relaxed/simple; bh=57ehCUb8ln12GUYleL0SvSEDRAcrNUcK94TBctYsESs=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Ej8eyNwKNHMCyMpd9MlshX0rLLt/q56KhoWckUCv6TasIjL9QSUt14SvsCOyZZ+R17HBv96FUmCdnwhf50xfZceKI85eivYguxCuewt2+Jlx+gnnn0Hk/rteUvWEFnM+KuYNZ337lEsUivW7+ggZOKXFtLiS9hAjIvPoUk9MriQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Gm8X53Wf; 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="Gm8X53Wf" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2D650C2BCB7; Wed, 13 May 2026 11:22:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1778671345; bh=57ehCUb8ln12GUYleL0SvSEDRAcrNUcK94TBctYsESs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Gm8X53Wf36vOYsBC+l31Qh15FVkQJKYRrzjLcMxw4k3EHvLYA3bCA08QKhe6G8mxv j/Fko8529+mtezaGInUrD+3yuesncJf9Gbp71d+o1+m6qJi22qpXL8AoFKoiZODOSl yW3Wc99jLS8x9adyNV1Gs1SBdRfwG1kmnHBUJR84bNbvcvouYY6O72A9RzcAZRH45P qTUMtNt5GjiW2WeeC9SejwGEFInfaYdd5RJAXJBpFea33xDnD0JcvDcrjS+M8/okdL h/75OYbFyc/cNZIENiitiTeuKiu6QckcbSPaWD8X1Krk+k97YsSP2ybOFXw1NVtcsJ tyOCUFOyCJrGQ== 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, Namjae Jeon Subject: [PATCH v3 06/11] exfat: add data_start_bytes and exfat_cluster_to_phys() helper Date: Wed, 13 May 2026 20:21:51 +0900 Message-Id: <20260513112156.9122-7-linkinjeon@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20260513112156.9122-1-linkinjeon@kernel.org> References: <20260513112156.9122-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() to compute the physical byte position of a given cluster. Signed-off-by: Namjae Jeon Acked-by: Christoph Hellwig --- 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..415f987afa9a 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(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 Fri Jun 12 18:34:01 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 4633C3793D4; Wed, 13 May 2026 11:22:29 +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=1778671349; cv=none; b=at+zrB84WQo2cKwvGLSUUGWPGs4M99o5BXj0tQBHBvOekV86qRCRbeo0Pch/BSE+NEfV8N0Yyp4swRA/fG2Jqfn6R2QzZmxhCiei8oaJzKvkdAClxAC4dKC2G4stu8hx9by4pPNdHor7AEG1seZyJk77Ol9xljLpjGX4SSCzK1E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778671349; c=relaxed/simple; bh=rApi22cpqSxyEsurK1XKbL52SxF+OhNqJ9qmRlG3i78=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=kj60YrKoBx8AmhyYFUPxmYtrrWnAP3kClnw5Ekf2r5bzNBEt3TVYkf32FocZRc/TWD0/sZWIXCWxzp2G+TxOX5+/qK9+9t5Fo6tN4EuSf2Ba5MLNwUo3rpCy9tAokVh9Yr5IuCCIKA7ynVRRTKY6MOoNhBwouQGCO9NBTnEryxs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=kIPjMdfY; 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="kIPjMdfY" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5CB19C2BCB8; Wed, 13 May 2026 11:22:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1778671349; bh=rApi22cpqSxyEsurK1XKbL52SxF+OhNqJ9qmRlG3i78=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=kIPjMdfYL3NVyW4EshnRtMEyw5EkshmU9Q9mfJ91qnHAkarRO5fhV7LkXYZkjF8MJ LXzCsAfMCfOq7oKEQS6JC/Ndvf74budzYYUpO7Hy3EJnlfvBjzeGUbHJePNcKyzlMa DIiI9cOkjL2lCa7KeQcguKfOattvu2zL3npfVkK36zCpv11oy7h5uOZz7lvlQQduMH u0WIun2VwbLRP8xdz+M9rkgmyl9Exul6kDmyeF4MtVVhRUkxrSDQdYFf+n+JiP/pJA 0D9hMk0SXn9G87ruyglBes5t2wlqzviIAOrGyisXDPhINH++R2GW6y0A+obTdmfzyV CcYjQsTP/Mtvw== 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, Namjae Jeon Subject: [PATCH v3 07/11] exfat: fix implicit declaration of brelse() Date: Wed, 13 May 2026 20:21:52 +0900 Message-Id: <20260513112156.9122-8-linkinjeon@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20260513112156.9122-1-linkinjeon@kernel.org> References: <20260513112156.9122-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. Signed-off-by: Namjae Jeon Acked-by: Christoph Hellwig --- 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 415f987afa9a..5ac52e9079b9 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 Fri Jun 12 18:34:01 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 A2F173FF8BF; Wed, 13 May 2026 11:22:32 +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=1778671352; cv=none; b=kXIEWiZmX06Ikgjl0Nxfzx4Qr6S/faUTkqYDacMC7K2KyGACzdUjd+YYivOoDJwCNx2h49wIOnc+aQYFsPp55r6Hxj9wHl4YTY8LOl/EuFmH+nutwnwZniYqndGW2xflUegd1aqy6dyYIoy5hH4rLTusgDfuOhjkzfUgZFY0vEM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778671352; c=relaxed/simple; bh=q2cvOxtjCNRThJbrI2nPqWMQVtJQREhWlzRExai7VN8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=kY5D0hJsG7PkYaEaBCuf+BDK9w3au42ydBlvDeoTZrVD65NZjTIKIDaQ0+MV1b8kWZvfbPTemkOMq/IxAsHGIdoqG9ADQENnIS8yl6nRzfZiFeEPNAu+cDv69lnC2I+0wsEbsw5F6IY3MTVeh1D71BKnyfDKpVwAqL9QlId+OlA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=H3py23Oe; 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="H3py23Oe" Received: by smtp.kernel.org (Postfix) with ESMTPSA id B897AC2BCB7; Wed, 13 May 2026 11:22:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1778671352; bh=q2cvOxtjCNRThJbrI2nPqWMQVtJQREhWlzRExai7VN8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=H3py23OeJ1g6WoAd49ueVuYrQi19egHXYyBOlKd7pO31CfeMhMKgmOcwI99TwV/SH UUYc8fAvaNKv0Ty5mH51KR4kKkuD7E+jeaBSBV8utWCisgB3sj8GN4hINLhWNhmmi/ YRUI6AXdfQmEZO36ZYdTuc02K3vNq6EREePXp1u2CFVzLGXZLh1J+v9+edOZcdeHqK 89l1pF/JZRDErtW+W8GpN0UwommnrVQilMM7fAGzrvitATEVgzISH3ZEdNmgpYPn2Z Pwt+yI7HBcuePdl6SFM4yY47W9meOGxxaBmHFE2k5I+YczNnfRGHpPzped896df+AA B05amO76x+g+g== 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, Namjae Jeon Subject: [PATCH v3 08/11] exfat: add iomap buffered I/O support Date: Wed, 13 May 2026 20:21:53 +0900 Message-Id: <20260513112156.9122-9-linkinjeon@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20260513112156.9122-1-linkinjeon@kernel.org> References: <20260513112156.9122-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(). Signed-off-by: Namjae Jeon Acked-by: Christoph Hellwig --- 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 | 216 ++++++++++++++++++++++++++++++++++++++++++++ fs/exfat/iomap.h | 14 +++ 7 files changed, 368 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 5ac52e9079b9..448857d4b70f 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..f7e66a4061fb --- /dev/null +++ b/fs/exfat/iomap.c @@ -0,0 +1,216 @@ +// 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(sbi, cluster) + cluster_offset; + 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; + + if (iomap->type =3D=3D IOMAP_MAPPED && + iomap->offset < ei->valid_size && + iomap->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 Fri Jun 12 18:34:01 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 829A634751B; Wed, 13 May 2026 11:22:36 +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=1778671356; cv=none; b=Ox5jdiTyGssSCreqgXEg1/fWr+AzxXjbAK+m32AhckyTfngsny6c8+I4yVVv3IDoG5kapoDzRo/c3meKvdIn7VgW6Pi0ylkEONHTGDOupA7nJP8o8RHb2c33bfMXSh0bcC/c5RI/Ms7LiGlH+P+pybHkR2O1NBrBOi0xd+1+qhg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778671356; c=relaxed/simple; bh=2qIjAfomMaFbyoT130Ipe4Dz2JPaqvxaAfe4xWCctGU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=UNqlNqveoX/jASml3vAzDuJR3wx/0mzGlUnZ4AE/plRxU6tsOere/o6ZvjUzXW0gOMuT66/jn8orfxo5xA99m9SMpM5YGBNDrqOewqkogs7P6pBLMWourFUCtdFb6pMkIeEZy6OP6WcBOv1j/RhQgUhxXK1hc3qO3ZdzO1W3SdE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=jWly4NCV; 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="jWly4NCV" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 56A66C2BCC7; Wed, 13 May 2026 11:22:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1778671356; bh=2qIjAfomMaFbyoT130Ipe4Dz2JPaqvxaAfe4xWCctGU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=jWly4NCVd8wQ3DXBzRONVogOweMtRUIYarnG2mDGMdKVbYCUIuqdrxtbBTVWSBOQz LIFuivGqVRw61raeD251k0xOUpIHL63fPOURuXsiyy+s6fMiFr2l5NQ3dDz9IJSeGS dJrtoNnQ4T0WifFSlc81u3cxshkGMNo1nyc3TFL5ypaZP2+/+sF7U1O5T61NHyQk5C Sj3OtKXQVQfgE0A3lo7/xdr/pXJTZ2RypvP6NsquU49dxfvrD4FvjsmvBWTARHhBnc Bw2LpovoS2Q6f4VwAlN+mJquM4cFyeVHig2ROJcdWNuaj7jEuhwL4KnYS/JPtnAKeC gYFGU7mkbYgfg== 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, Namjae Jeon Subject: [PATCH v3 09/11] exfat: add iomap direct I/O support Date: Wed, 13 May 2026 20:21:54 +0900 Message-Id: <20260513112156.9122-10-linkinjeon@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20260513112156.9122-1-linkinjeon@kernel.org> References: <20260513112156.9122-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 Signed-off-by: Namjae Jeon Acked-by: Christoph Hellwig --- fs/exfat/Kconfig | 1 - fs/exfat/exfat_fs.h | 1 - fs/exfat/file.c | 88 +++++++++++++++---- fs/exfat/inode.c | 200 -------------------------------------------- fs/exfat/iomap.c | 26 ++++++ fs/exfat/iomap.h | 1 + 6 files changed, 101 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 448857d4b70f..6f3ad1586261 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..bd947e963f93 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,55 @@ 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; + 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; + + ret =3D filemap_write_and_wait_range(iocb->ki_filp->f_mapping, + offset, iocb->ki_pos + written - 1); + if (ret) + return -EIO; + + invalidate_mapping_pages(iocb->ki_filp->f_mapping, + offset >> PAGE_SHIFT, + (iocb->ki_pos + written - 1) >> 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 +732,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 +750,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 +780,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 +902,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 f7e66a4061fb..8d3c95d00a01 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 Fri Jun 12 18:34:01 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 69A3E3FF897; Wed, 13 May 2026 11:22:40 +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=1778671360; cv=none; b=CtwqKGdJdR9qvO9j0oD7vVODkYKOSrUkQe9e4G/Rg2o8MRKb45cUpuMBp9UeOwy/d0wueR2Qe8ii3Q9aU46A2CFqXOApor7um2MWpywv08EMPN+QQZl6HtNWBLjrNzEb9FRAnCb4b+3kDC6oIeAL2Xt+8UT4zHBhR/vsBY8WiTs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778671360; c=relaxed/simple; bh=2Wp49jonsXT4d/xI0857x2+N5hYGuVtlyJkXOcpgMW4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=iPcBUxC+bcZAMaS3Gkdhm/ryNpe8+9f0LjHaetHgtFrgu22VbcIlj08ogz2GTiTzEBO3SDP1HkIqkXrWccUXEURyn4LyxxhcDiC83uSYE4PAn68ga5lue9q3K7O9koofdRoj+aNOePyTqlpzcFYN/oKpB6ZAdrmO96O+f3MaL0o= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Bg0RVugE; 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="Bg0RVugE" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4D27EC2BCB7; Wed, 13 May 2026 11:22:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1778671360; bh=2Wp49jonsXT4d/xI0857x2+N5hYGuVtlyJkXOcpgMW4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Bg0RVugEtJRgzSrQOU8Swi5HXdoXXX59JPIyXKNm0JyVzrQqTFJ32DtDlxS3RArfy Tz61KNmrSpapDd7AENvumes019FopDVw0Us3m2qey5P3gkAEdL5w1aZ/6uidTIXD99 vtLYATEOp7BKDoDDSiNZROGA+qw1FAvc8NuyhpwoSTomgIhp4j3C9+3dqwUTpHXQhy Dbmj/T3DDW1UrispPzvj5NOFYTgHOwHomXfkhIhd/UYm7s7Ozs6w8tWPDFFABR3l1l Vi/PqKVLmMClAnYXTL+oaA7PxgiG6VX7GZPSn4A9l4S3tuO/KSRWF8FnOkEMIwYbQK msWQmuJLbpaKQ== 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, Namjae Jeon Subject: [PATCH v3 10/11] exfat: add support for SEEK_HOLE and SEEK_DATA in llseek Date: Wed, 13 May 2026 20:21:55 +0900 Message-Id: <20260513112156.9122-11-linkinjeon@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20260513112156.9122-1-linkinjeon@kernel.org> References: <20260513112156.9122-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 Signed-off-by: Namjae Jeon Acked-by: Christoph Hellwig --- 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 bd947e963f93..6ae2f72621ab 100644 --- a/fs/exfat/file.c +++ b/fs/exfat/file.c @@ -916,9 +916,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 8d3c95d00a01..a17c39c18c0a 100644 --- a/fs/exfat/iomap.c +++ b/fs/exfat/iomap.c @@ -98,15 +98,39 @@ 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; =20 if (iomap->type =3D=3D IOMAP_MAPPED && iomap->offset < ei->valid_size && iomap->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 Fri Jun 12 18:34:01 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 AE1103F9F56; Wed, 13 May 2026 11:22:43 +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=1778671363; cv=none; b=Jbw4MxZpQ+7CQghAbHEwUk6x+cc7iQ24X4uidzg6TbSlcT97NWKLnNH0dPktwbIAgtDararwBs5J3zY0Xy59xX9E/mUqN3F3cMIi6oK5pv8YwwySosfT7MGzg3ZIaiTBDaPAxNDU/X+jawEcaAcguRsFMZbNLaG3sdQeHFSTTnM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778671363; c=relaxed/simple; bh=p0NCUxLW5/lFSHG9MNpThep+4bYlHQO7IRftbA4CWE4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=DfYRYxw+zPMqDxnExGA5BjFV0JLVvxYH0orN/xIrlnuh3dD5k0G6awFdSQrbak3lnBVs1s2G0KwIMfzNjvypJS4e0P6D5fl2giKi1t2FAARE/6oxM+iWIAVxYdSEAe2n541wlzRpPpkRdPvmwqa/TDEOrZs3og/TuDUAJROLpvE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=mYb1P8hJ; 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="mYb1P8hJ" Received: by smtp.kernel.org (Postfix) with ESMTPSA id CA055C2BCC7; Wed, 13 May 2026 11:22:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1778671363; bh=p0NCUxLW5/lFSHG9MNpThep+4bYlHQO7IRftbA4CWE4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=mYb1P8hJMAF8kcoFifJ/FBIJWNqTXFWvdtJHtKF4oOmOXN1QDdUIZ4fMiiXVrQ1w9 0p8NY46480LolmXaMGtkwNBBIMx6EFYFnJwZfNUGpu2libvPbdm8xC4Xnw6rWIYslK jBT0vy+h/VKslimZYl9i94JI45WPEE7cLfnOdd4c15quqs06kQ7UM87zpjbNo2b7q5 y2Y07FtUJUFGZjM5hkC6EBZ7tf01u81ahk+xEY/k+E+i3GQfSh7avFsSTBqAAm3fcD Cbt907k39UeZLMXmhQHulOX8FKRVutac8j5FhWeKJtpaah67jx/iAeb+Vv3fVEN5NJ sHs8cN3bws8Yw== 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, Namjae Jeon Subject: [PATCH v3 11/11] exfat: make exfat_truncate() return error code Date: Wed, 13 May 2026 20:21:56 +0900 Message-Id: <20260513112156.9122-12-linkinjeon@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20260513112156.9122-1-linkinjeon@kernel.org> References: <20260513112156.9122-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. Signed-off-by: Namjae Jeon Acked-by: Christoph Hellwig --- 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 6ae2f72621ab..2b650a73c5c8 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