From nobody Sat Jun 13 13:31:33 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 229213AEF36; Thu, 7 May 2026 12:44:34 +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=1778157875; cv=none; b=Wo5/QF1IzHmSGVMV7cI4Y7BZqK20n3qFS7ILB5iDbsRZVd7zELqDOfQ7Sqes9y/+BTzhovMOajueE7rQj18YL4IJAnoALJYhMpOtXCYZBA0rbgrYgP4asdjDnywrLf+4APxiu29X98ikIqw0ZYMybjnoG6qciTDm6BzdWF2ydwc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778157875; c=relaxed/simple; bh=yk1t2K2HkfYHd7TYDa5TUKJOcAKUGH6qjuMuPu7iFmU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=VVFJL3NkEewdHa/Ch8bFlVNluEUlFNZbibK+YMOZgkGS1rRLjXuh+icMqejPHFM/efztWVPrnv5QHKb885PmP6eCRFdx8hyiJXQkfijnPUZTN6qmpCAJDFi7OXCff2VJ/Y3GHVXApw+Qc0NRGqHIjUk+ZkgwkmGmaURVnXFWTnM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=sV3vJ5s8; 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="sV3vJ5s8" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2A18EC2BCB2; Thu, 7 May 2026 12:44:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1778157872; bh=yk1t2K2HkfYHd7TYDa5TUKJOcAKUGH6qjuMuPu7iFmU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=sV3vJ5s82tNs6A1CFJWXZ50f4LVlezTToQl9XJCDz/7uMfPOsSIN+7moC1S4oiAur zwvyaCpedZ6N0Oz+y+V3WWuPVhEkI6w8GAondZJSn9M1JiSY09hJqwlzzveMZdtpbE cnr4gGS4ICdZFTeoBgS9/E3t+F1hxCCmG6h5OZehDi/snD8VTe8ulZMZ0WHDPBA5Lk zvsVoy9X6MUt4+aad2ZzKE3cMv7wTi6v8dK+9QDgcTRcVrJlXQyMC1s6/iUz27qTxu /G5mfP3roGVb5kyVALVsxwWpXPYmCTiYRxTXnVVL9J+b7tLEV2oZsE5KHpx/My0HGK XcNp/bS/MW0jw== 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, linux-kernel@vger.kernel.org, Namjae Jeon Subject: [PATCH v2 1/9] exfat: replace unsafe macros with static inline functions Date: Thu, 7 May 2026 21:42:30 +0900 Message-Id: <20260507124238.7313-2-linkinjeon@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20260507124238.7313-1-linkinjeon@kernel.org> References: <20260507124238.7313-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 --- 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..d25e0a69865b 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) >> 9; =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 Sat Jun 13 13:31:33 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 F02F533E345; Thu, 7 May 2026 12:44:42 +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=1778157883; cv=none; b=jNj4a1Q/KLaUGsV0ADi4+QtFK+PHgkyuwhFz1MmJ6XneJv6lUB9Mox5lga3Mc/jueK3nfVVi34Q4oKMHWIX6KUI+9qApUq455WxoJP6UQCp5AnV48YVHOQXXjoBBuF6CTbs8o0vtluwi4ibY6RihLS7E+2UqEYYmok03metkFD8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778157883; c=relaxed/simple; bh=q7iu0mK6wq0YpbKSRRtPEDTqgZmAcPHb4MPNaT5DtPQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=HBsHUrL7hBFJ9eszx5b/Y+eBGB0tkVYjSLexNooS771SqDTdvhSya6tmf88GNLdzNff4pvPtzTARnMXJ82rM4nfgRaN9FCasBa/Nsg6Gq3ypKIqzWFHFkS5uan5CdmQvCv40yqN34F9/yuOlagvfhRkUpsMnY6qLBovu+4mAyr8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=FX/RQVk1; 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="FX/RQVk1" Received: by smtp.kernel.org (Postfix) with ESMTPSA id C0722C2BCB8; Thu, 7 May 2026 12:44:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1778157881; bh=q7iu0mK6wq0YpbKSRRtPEDTqgZmAcPHb4MPNaT5DtPQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=FX/RQVk1xHQhetl4dL6RcESpJqC9i868f0B4Vxzs2Y3nAXJh157DWRt5JKEPqthBG FGpfy/uHK5ck+/by9ZoV0xU3Uj0hmDkmtRhC2iAtzcAyU7QRvJMzKg0pzNW1c3OpGC obVF7PRAsqAce898j8FQr5v7eQqA6fpnZeo94J1/2tlG8PE2YSah2k7vbuzGLlUU7D lpLesmb1bfhg10Q67Ttb6sIeTN88EaYgIMdlN0h8sh2TdoHy5NhxyUt1gi7YrxQKIs MEAEv3xkaqSbDjhDn2babuR7roLBubDSeQGlAmBSXiEa9ORUaiwvuG3QGiWKtJYYDQ mr1vES0Dl3Wkg== 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, linux-kernel@vger.kernel.org, Namjae Jeon Subject: [PATCH v2 2/9] exfat: add balloc parameter to exfat_map_cluster() for iomap support Date: Thu, 7 May 2026 21:42:31 +0900 Message-Id: <20260507124238.7313-3-linkinjeon@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20260507124238.7313-1-linkinjeon@kernel.org> References: <20260507124238.7313-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 --- 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 d25e0a69865b..1246713567a5 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 Sat Jun 13 13:31:33 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 D979B3F166D; Thu, 7 May 2026 12:44:47 +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=1778157888; cv=none; b=TLdPI3Icn97UcXMkgLcebPekcEXIH5SPPhst2DalHwYqkZmf3b8wxFYommuvRJ/APCJpzPSnBuHBC+qlTHrhpwgZOpka0ieFBsuWkBs0nfV0IR0Lxo8TEo0n7OqvZa4XV8YBwehevc1uCXJrwNyg7DQVe0+ScZ+evdo4kexP7KE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778157888; c=relaxed/simple; bh=dVYEwiT/vQkj9bceBjC7oEzVsWkx7VDYr7J0MQQAjXw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=BNtTV8qdXjmUvFrt4mAUQkPz9axK2AxxY1wjtvCPUcMBoI0Ph/R2kw30II9TRJzXq6HesFWyZS4vkXScNknxkKx1a8Upm97G/76FKoCNjmo58sC8sw7EthvmFA7vZS0t8vJakWe3kp8r+ZlBg0gErHpV25ohCJspN07zMo/tJjo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=HIuqQJth; 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="HIuqQJth" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 557C8C2BCB2; Thu, 7 May 2026 12:44:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1778157887; bh=dVYEwiT/vQkj9bceBjC7oEzVsWkx7VDYr7J0MQQAjXw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=HIuqQJthR4++uR/BPrpjfDxwXJFbVB1LJEApdW6C5g9BcByrwpdSUNPUop765VlDJ Wlgfq6iSBtkrLxE3VWBcLowUkcRhu1AyK9ZDihytnsGET5junrctSqbGWlY2rKqfPs IEo2m0Ffkg69r7+hoGPFum6RXMmKQyLhAhn3WP+V0gm7DtphjNu5jOlHWfRpcmalEc Chtx4mzXJvL4SY2dwi9Vd9FbS2DZ8ytDjcZEpzhtFmkx+1nRZvJmhrEwmGZd2Fs6M3 ImyRIi+99Z3lXW1VSo06w5/NXA8I7JuzcLx3T507x542HYs8gqc4c1xQaMN8ZSx+cf Q07Wxwcinlspw== 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, linux-kernel@vger.kernel.org, Namjae Jeon Subject: [PATCH v2 3/9] exfat: add exfat_file_open() Date: Thu, 7 May 2026 21:42:32 +0900 Message-Id: <20260507124238.7313-4-linkinjeon@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20260507124238.7313-1-linkinjeon@kernel.org> References: <20260507124238.7313-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 --- fs/exfat/file.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/fs/exfat/file.c b/fs/exfat/file.c index 29a36a80e29b..b7a4964631a0 100644 --- a/fs/exfat/file.c +++ b/fs/exfat/file.c @@ -800,7 +800,22 @@ 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) +{ + int err; + + if (unlikely(exfat_forced_shutdown(inode->i_sb))) + return -EIO; + + err =3D generic_file_open(inode, filp); + if (err) + return err; + + return 0; +} + 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 Sat Jun 13 13:31:33 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 190963F23B5; Thu, 7 May 2026 12:44:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778157893; cv=none; b=PQ/p7YClh79OBOYulqKac5/WlU4L4xl49rPBTiupHjyU4HySSmczYBmOcs3/eALS/BF0bxEAxvtBSVX0zY9KSJxuwhaY4gTn/MabEB+RlLLYMf0yLqq5CVRZFF5t6FSZaLh6dnptHPh2QSevO6K63tE+oU/hUj3CKvP6C6lkHiI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778157893; c=relaxed/simple; bh=Xu++F6Z7qdPxSxqU0I6aX/vLNEqe/DVy+x9vNrgKX5g=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=E7hR/5XKH9qL5Lb3c9gNt+prog964aFUVPD74l/V+4UfuRkNtJuH09+ni8dGLaG3WpvlpmnngG4oSoeJkuwHzu0S/KPlJSMMNRc3mQVIObkoRFLuHeV0bjK6lBN5Cru5yWLPZOVF6hF8ztWrDoCFlOkJod2Jb/a9o6JPUm/druk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=UWarwLTv; 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="UWarwLTv" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 205A7C2BCC7; Thu, 7 May 2026 12:44:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1778157891; bh=Xu++F6Z7qdPxSxqU0I6aX/vLNEqe/DVy+x9vNrgKX5g=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=UWarwLTvEmEPWwKXEfO1hmPYT3yx802vWgF38nE8m2Y5DqywzJGjOtO54AP4VBJD5 /HbQvMj4+y//JzSqqrU/In32Jnef/r86CHDahMQfjN9K1n0funf2c3Z+r/XbjUmTT4 5sZ/La5wTnmfbrZp8giaIokic+1jS8KTLUykRgbUo1o6BIGuakZipsVr1aptyWl0J3 bzqAY4o6MYk3gtf8pJAKYCzZeOz6y2JPzhkcnL571ypYMqGzXfAprjrIomEByU7KBw pdo5yrYTYF7soIHxfULa6ykvkHCMDhbuwY2PntFczS1nipS6F+c6j7ph7bXorQw9Kl eGxFQ9+0poprQ== 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, linux-kernel@vger.kernel.org, Namjae Jeon Subject: [PATCH v2 4/9] exfat: add support for multi-cluster allocation Date: Thu, 7 May 2026 21:42:33 +0900 Message-Id: <20260507124238.7313-5-linkinjeon@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20260507124238.7313-1-linkinjeon@kernel.org> References: <20260507124238.7313-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 --- 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 b7a4964631a0..15b9d6a1766a 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 1246713567a5..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) >> 9; - - /* - * 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 Sat Jun 13 13:31:33 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 BA10E3AB262; Thu, 7 May 2026 12:44:57 +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=1778157897; cv=none; b=qPBao5e8h738BxrkC+e92yovJkOruQC8srx9d5ZpMaLde6eqlwiH9yHqo8Pcluwv0OC/e/6SV0z7CAHpsEp0FrpgrUL5s8smNE+c/NrVbNchfp+jLZ0KiVZ7pe0URmch7wP3wh8Vpd94DW9Mn8fDpqAoTtBJbkKXlwk+Q4sb+us= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778157897; c=relaxed/simple; bh=CVxGxcnAW5/o9BqN0DaMgSSTzT0Xe9tR5fS9KdRLbLs=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=nwcT3Ayaq+ipR29rdGTZVaMrY0u2TQPSeBNqnNTgfN2sTf+SHXVKkgfZWOjFsegiykfZDV2/bLbq74hiVz0bjhb17YCRPLozeYbUz4d0FwRcLVX2iy1NQv5LRiqCccJOU9/bnwm4COyZebIZU2rt+WCAEXZNjLXlWYCEPuU1ZVA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Y1HCw/VH; 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="Y1HCw/VH" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 20785C2BCB8; Thu, 7 May 2026 12:44:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1778157896; bh=CVxGxcnAW5/o9BqN0DaMgSSTzT0Xe9tR5fS9KdRLbLs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Y1HCw/VH7msnp8F/8RPA6UmS6E6aHtliR0znmZ5R25UqJBfb5BuYaZsYFwDZREO9n 5tKkRv7NsMct1DnvcLykQRgeP4dJpndHNtWfoOAsLAs71D/jDkBIq8sIJM8/XZAOB1 4EiADMIBjz2PAfxQNqTfD6aUZ7UZZ0tPpc7YHsKuaWjgwCayFaRujWV9Q9fiIaHaaV Jbb2sXoVNaRIh5RRp3f9kVoxmbjmsL7N8PRuz5jt/Lg/qwHeEauWWd6qjKJZgsNTAR DBsp1rWUEsUmJfq2NbDqeJOGy6W/i8CuuKYgjt0RIY3FwrR/o84gmDAgNxiPRSSMYb 8apuufMQlqRcQ== 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, linux-kernel@vger.kernel.org, Namjae Jeon Subject: [PATCH v2 5/9] iomap: introduce IOMAP_F_ZERO_TAIL flag Date: Thu, 7 May 2026 21:42:34 +0900 Message-Id: <20260507124238.7313-6-linkinjeon@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20260507124238.7313-1-linkinjeon@kernel.org> References: <20260507124238.7313-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 --- 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..e3e7ad72b29e 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 only the unwritten tail of the block 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 Sat Jun 13 13:31:33 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 CAE923BED23; Thu, 7 May 2026 12:45:01 +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=1778157901; cv=none; b=T5pa+U4x72KTEKRYHZkHDgDZoCj2SaZ8j4VxqbDxpvoh9qPExdrFH/DxgcwMHqEDhYfIldMY46ZP4n7E3ydJrQ2mAS/ot//vnUAM8C/UbLcLYv0xwYX4UySXCInzJBLlM2Yps0K3gtrQBEicGjwdiJBR07uVuz3O3qOqEm3LYU8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778157901; c=relaxed/simple; bh=57ehCUb8ln12GUYleL0SvSEDRAcrNUcK94TBctYsESs=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=JYuD/pDu/xYmFBN8MpOOy+Ai9DjxvQV7x/IHzmO6SSMjJuyOPY8k03I+ib19VXq5YdMLZdMRwX6gubPLGD8H2wdueN4tHzs0Ncc0QBRHFkA8++pfD3oOpbEaXAyv+kXpkCGYNKR8XoJDjR2QrL0topFj4KmgWe1Ns39s9uzCuHA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ViP6UUUG; 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="ViP6UUUG" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 804A5C2BCB2; Thu, 7 May 2026 12:44:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1778157901; bh=57ehCUb8ln12GUYleL0SvSEDRAcrNUcK94TBctYsESs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ViP6UUUG4sP46/QVb3dSqK/snAMd51xAfe3tl3oyELPfM7IhymmgNoI89Yl+WNrr7 YZzKYCKWpBC7j+9d88SXh41/0Uc1jwi4arqIxcbKFmOpSi6Xa2RW50znJmfGz+lxE8 CuDyFlbKrP1Oh05pFJHeFqG+2uD9q1A+YX+jNFhM4NnuKoVIX6Vw54yO45Q2nJU3LO JPhBBAjaQVHd6Ot/gmj1ae7NAdVPc8wOp2wP88nXIxg4PLCtXT9YJUNAimcpu0MUTh rWYwDtAgOGk7rzeu4Mp60tUX+6mZo6HSNvKoYnFXayDsVP6HfMpqiVUwZ8pJUA9Ryy Im+an/m9Ydo7Q== 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, linux-kernel@vger.kernel.org, Namjae Jeon Subject: [PATCH v2 6/9] exfat: add data_start_bytes and exfat_cluster_to_phys() helper Date: Thu, 7 May 2026 21:42:35 +0900 Message-Id: <20260507124238.7313-7-linkinjeon@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20260507124238.7313-1-linkinjeon@kernel.org> References: <20260507124238.7313-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 --- 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 Sat Jun 13 13:31:33 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 A1BA53F0AB7; Thu, 7 May 2026 12:45:07 +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=1778157907; cv=none; b=hN9/DOBKXrCIsiH9p+2F3C40NUqirNN7L/jyJZlgxpLOgE+fhvTAAmsRRhVlpwumI+lUGx7tSS3HgJwBei75vcWKhm5duUb+YoL7sA+GWxsrKuYZW0IoxwxYcrxbKlMu8HCRzsxDqx1PtPD2r7u1wyDxr6ZpvWM1Vbz6ul7QVLY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778157907; c=relaxed/simple; bh=BXeKpcXHUMxJBOF2Ttkp2hd4HX0qq9vhDwqwZeWLR6c=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=clWQHs958WvhiUn01wVMaMCf0zXWxPKaUjR04WSUIOFM57fIr4DHJLxxtxGypkwTjwEK+5EZpkK5LqcMTti2Jv4a4nrIm149ut/3bBWLemldKDF2hoVHt8cX/y6q0C2O3fGhtWcBH3rzf+TXIsvtdX2DcjbAdjIt4P2C3xCgos8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=SYYYPnqv; 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="SYYYPnqv" Received: by smtp.kernel.org (Postfix) with ESMTPSA id CB397C2BCB2; Thu, 7 May 2026 12:45:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1778157906; bh=BXeKpcXHUMxJBOF2Ttkp2hd4HX0qq9vhDwqwZeWLR6c=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=SYYYPnqvkjXP8RJRQnqzBepczJTR+t+dp6e1FX1ZrkXVUj3Ss1wwQAqBba/zTvHew N41xA2TLYckgeBl690W/e86DPCynxE740hr3mzQMaRepURN/1HZeabkoXfQsaySF6H mQgzU6w0lrWPR+qC89dSZVfB8fbx58ZSeC8ZMVmAn5R1aeQGRD1gK5tdy3rVTqdW7M 4GbuK0FFSFF2WhHYXCkfIitj/UUKGV1puybb7SpXZuB2RLKWAHZcnqIQT7KaMFawiG 9iTB9COqoEJ5nzmbWs7K3YJKp5mLNdu58CYe0Il1chGr0vngGY/b7wJzpP9iKVo/Dj XhKeX2NMNszcQ== 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, linux-kernel@vger.kernel.org, Namjae Jeon Subject: [PATCH v2 7/9] exfat: add iomap buffered I/O support Date: Thu, 7 May 2026 21:42:36 +0900 Message-Id: <20260507124238.7313-8-linkinjeon@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20260507124238.7313-1-linkinjeon@kernel.org> References: <20260507124238.7313-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 --- fs/exfat/Kconfig | 1 + fs/exfat/Makefile | 2 +- fs/exfat/exfat_fs.h | 7 +- fs/exfat/file.c | 141 ++++++++++++++++++++----------- fs/exfat/inode.c | 117 ++++++++------------------ fs/exfat/iomap.c | 197 ++++++++++++++++++++++++++++++++++++++++++++ fs/exfat/iomap.h | 14 ++++ 7 files changed, 350 insertions(+), 129 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 415f987afa9a..448857d4b70f 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 @@ -293,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 */ @@ -648,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 15b9d6a1766a..6033e8ae4628 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,31 @@ 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; + struct exfat_sb_info *sbi =3D EXFAT_SB(inode->i_sb); + loff_t old_valid_size; + int ret =3D 0; =20 - off =3D offset_in_folio(folio, pos); - folio_zero_new_buffers(folio, off, off + len); + mutex_lock(&sbi->s_lock); + old_valid_size =3D ei->valid_size; + mutex_unlock(&sbi->s_lock); =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 +672,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 +698,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 +715,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 +755,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_write_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 +820,21 @@ 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; + + 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) + return err; + } + } + 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..0c5aadfd4132 --- /dev/null +++ b/fs/exfat/iomap.c @@ -0,0 +1,197 @@ +// 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) + num_clusters =3D exfat_bytes_to_cluster_round_up(sbi, + offset + length) - exfat_bytes_to_cluster(sbi, offset); + else + num_clusters =3D exfat_bytes_to_cluster_round_up(sbi, length); + + 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); + if (length > cluster_length - cluster_offset) + iomap->length =3D cluster_length - cluster_offset; + else + iomap->length =3D length; + + 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) + 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, + 1 << inode->i_blkbits) - + 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) +{ + if (written) { + struct exfat_sb_info *sbi =3D EXFAT_SB(inode->i_sb); + struct exfat_inode_info *ei =3D EXFAT_I(inode); + bool dirtied =3D false; + loff_t end =3D pos + written; + + mutex_lock(&sbi->s_lock); + if (ei->valid_size < end) { + ei->valid_size =3D end; + if (ei->zeroed_size < end) + ei->zeroed_size =3D end; + dirtied =3D true; + } + mutex_unlock(&sbi->s_lock); + + 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 Sat Jun 13 13:31:33 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 DDF793F7AA4; Thu, 7 May 2026 12:45:12 +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=1778157913; cv=none; b=ZIy/5EKcwhGvNcjaqppBbNWNtCjt228HX/PmdyuCd+hNJw+mhFGgTbUpNgTAL08pzPUpfroyNQm4M2d6kITipjsogouz51UZrccwu8yPyEjoybYSuqIdaXUUO3h82wCQgmEYUgj1wFHpUZMd7tvUtmBZewwiEB/p0cQgrt39Zyw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778157913; c=relaxed/simple; bh=xUSF8ATGO+IsMU5xx6TXDQ+/Kmnlnd/FJT57kImIa/E=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=L3BICc1K2AZgK7UDRQX41QEUJ8fPngvLMh4KQmEMg4aqW+zfC+14h1sp9WJedlSD5xtTjiXL9TGGqAkY3PgsrtLZPO6+b3LZwmTV5xPNpXC2zkjcZlbiVIylteGnIiLYdi/6he1706Unc05VcQPBAHz2PcP/mNxwCO+Gcd3MfwA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=FQpbiBGT; 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="FQpbiBGT" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9F8CEC2BCB8; Thu, 7 May 2026 12:45:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1778157910; bh=xUSF8ATGO+IsMU5xx6TXDQ+/Kmnlnd/FJT57kImIa/E=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=FQpbiBGT8jxHUgWTM8US1n4gBX7r0C2XBpup+nGZIrEB+icAwwHudbhD6TtvNl+Ir /gAiDqqI1C/ekLHdow5MTP5MEYFC3jl08z3faAAwaIpvowCFzA2nKxt0b5Sj5QJ2fh R/kGb4lREYOUZXQYk0hirgPBu/bLMppYEEAc7+HQJT7b3IKcY4hXbTcIODko5mI91w VCZW9VPIrbQsm6/GxAEOuDEL6qQFQaso1f1nVB9aH9wG0d1TfN5Txu855I/K0cnLa9 GSPrxt/agav3sSmSwctTUOkbkiIEPEd/Fqw0HFRZy/zmvfcUmEU3B2+4NoAkpAmRQk 3Daxwe+nivC0Q== 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, linux-kernel@vger.kernel.org, Namjae Jeon Subject: [PATCH v2 8/9] exfat: add iomap direct I/O support Date: Thu, 7 May 2026 21:42:37 +0900 Message-Id: <20260507124238.7313-9-linkinjeon@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20260507124238.7313-1-linkinjeon@kernel.org> References: <20260507124238.7313-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. Signed-off-by: Namjae Jeon Reviewed-by: Christoph Hellwig --- fs/exfat/Kconfig | 1 - fs/exfat/exfat_fs.h | 1 - fs/exfat/file.c | 76 +++++++++++++---- fs/exfat/inode.c | 200 -------------------------------------------- fs/exfat/iomap.c | 26 ++++++ fs/exfat/iomap.h | 1 + 6 files changed, 89 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 6033e8ae4628..c4e6afc21bfe 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 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 @@ 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); @@ -664,6 +666,47 @@ static int exfat_extend_valid_size(struct inode *inode= , loff_t new_valid_size) return ret; } =20 +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) + goto out; + + if (iov_iter_count(from)) { + loff_t offset, end; + ssize_t written; + int ret2; + + offset =3D iocb->ki_pos; + iocb->ki_flags &=3D ~IOCB_DIRECT; + written =3D iomap_file_buffered_write(iocb, from, + &exfat_write_iomap_ops, NULL, NULL); + if (written < 0) { + ret =3D written; + goto out; + } + + ret +=3D written; + end =3D iocb->ki_pos + written - 1; + ret2 =3D filemap_write_and_wait_range(iocb->ki_filp->f_mapping, + offset, end); + if (ret2) { + ret =3D -EIO; + goto out; + } + invalidate_mapping_pages(iocb->ki_filp->f_mapping, + offset >> PAGE_SHIFT, + end >> PAGE_SHIFT); + } +out: + return ret; +} + static ssize_t exfat_file_write_iter(struct kiocb *iocb, struct iov_iter *= iter) { ssize_t ret; @@ -688,16 +731,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; @@ -716,7 +749,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); @@ -746,11 +779,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) @@ -860,6 +906,8 @@ static int exfat_file_open(struct inode *inode, struct = file *filp) if (err) return err; =20 + filp->f_mode |=3D FMODE_CAN_ODIRECT; + return 0; } =20 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 0c5aadfd4132..69308d66c55a 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 Sat Jun 13 13:31:33 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 C599D39A04C; Thu, 7 May 2026 12:45:15 +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=1778157915; cv=none; b=T8qzyvLa2Vvl7d5NkziSafOmwLUa3kUX1NW6DnM2uJLIV1ySdcqHw5Q9yGi2TCzR95EOmIOwU/YpoWWQlG1Ga+HiXIM2XgPLfG1AjvKLR198ZTUtIaEjj3HOauBv1pFghXpF13lUUgKaIlwMZPKGsZDGibQ5FpvgMTlFHlABmME= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778157915; c=relaxed/simple; bh=+/ypl/zNHou/Jp8yX+qXMJHwTHS775TiCo6L63Xpsz8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=A+Gros6xqrbwM0TMXoheENKZ/rHCDmX1/GHJULkhLDJYL0vmwntRGspdwcz2sWc9D6iNQEloOKC2CkZ9LPIQ6QbtpvuHXfpsMvQtWu044SHbzaQ0QFZDZk6YOe7TItT1W7WbcplKiorB/zuat41l1tHhvasQ97sbTbRBnF7+Dmk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=p5XNcgn8; 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="p5XNcgn8" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 93D46C2BCC4; Thu, 7 May 2026 12:45:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1778157914; bh=+/ypl/zNHou/Jp8yX+qXMJHwTHS775TiCo6L63Xpsz8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=p5XNcgn8PV3zlubWnHz7+WMNKBPyZumzdCEpEGqrF+WkCd7GlIHVyOfLNyVCJk7S/ /c0CqKPhe2XaUOD56LW/sW1fL/THWv3MfOHj/9zQ5BO8y4WIBCW3KkgZQj70Brj0UK ZNQFCxbnYLvrcsz7tc+rcIr6FzmRpuDEsDukld411ViEQm+YVCVYWh8p5hBYeNckuY 0MjKwapu7B1JCQjGqYrTH3N2JxfxTGY/+F1LZloNUsNmr42N/y77nwb675GU0p86Jz n8nJ8qBqGeWkOF9Rea8pFQXftlMSJ1iamj1VF4yLJMwfV/sHowacTbcvz9zA84lF/9 KlfUrlf/dAkxA== 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, linux-kernel@vger.kernel.org, Namjae Jeon Subject: [PATCH v2 9/9] exfat: add support for SEEK_HOLE and SEEK_DATA in llseek Date: Thu, 7 May 2026 21:42:38 +0900 Message-Id: <20260507124238.7313-10-linkinjeon@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20260507124238.7313-1-linkinjeon@kernel.org> References: <20260507124238.7313-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. Signed-off-by: Namjae Jeon Reviewed-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 c4e6afc21bfe..c1567d206253 100644 --- a/fs/exfat/file.c +++ b/fs/exfat/file.c @@ -911,9 +911,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 69308d66c55a..9b1499a30d39 100644 --- a/fs/exfat/iomap.c +++ b/fs/exfat/iomap.c @@ -79,15 +79,39 @@ static int __exfat_iomap_begin(struct inode *inode, lof= f_t offset, loff_t length else if (iomap->offset + iomap->length >=3D ei->valid_size) 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, - 1 << inode->i_blkbits) - - 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, + 1 << inode->i_blkbits) - + iomap->offset; } } =20 --=20 2.25.1