1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * iomap callack functions 4 * 5 * Copyright (C) 2026 Namjae Jeon <linkinjeon@kernel.org> 6 */ 7 8 #include <linux/iomap.h> 9 #include <linux/pagemap.h> 10 11 #include "exfat_raw.h" 12 #include "exfat_fs.h" 13 #include "iomap.h" 14 15 /* 16 * exfat_file_write_dio_end_io - Direct I/O write completion handler 17 * 18 * Updates i_size if the write extended the file. Called from the dio layer 19 * after I/O completion. 20 */ 21 static int exfat_file_write_dio_end_io(struct kiocb *iocb, ssize_t size, 22 int error, unsigned int flags) 23 { 24 struct inode *inode = file_inode(iocb->ki_filp); 25 26 if (error) 27 return error; 28 29 if (size && i_size_read(inode) < iocb->ki_pos + size) { 30 i_size_write(inode, iocb->ki_pos + size); 31 mark_inode_dirty(inode); 32 } 33 34 return 0; 35 } 36 37 const struct iomap_dio_ops exfat_write_dio_ops = { 38 .end_io = exfat_file_write_dio_end_io, 39 }; 40 41 static int __exfat_iomap_begin(struct inode *inode, loff_t offset, loff_t length, 42 unsigned int flags, struct iomap *iomap, bool may_alloc) 43 { 44 struct super_block *sb = inode->i_sb; 45 struct exfat_sb_info *sbi = EXFAT_SB(sb); 46 struct exfat_inode_info *ei = EXFAT_I(inode); 47 unsigned int cluster, num_clusters; 48 loff_t cluster_offset, cluster_length; 49 int err; 50 bool balloc = false; 51 52 if (!may_alloc) { 53 /* Completely beyond EOF. Treat as hole */ 54 if (i_size_read(inode) <= offset) { 55 iomap->type = IOMAP_HOLE; 56 iomap->addr = IOMAP_NULL_ADDR; 57 iomap->offset = offset; 58 iomap->length = length; 59 return 0; 60 } 61 62 /* Clamp length if the requested range goes beyond i_size */ 63 if (offset + length > i_size_read(inode)) 64 length = round_up(i_size_read(inode), 65 i_blocksize(inode)) - offset; 66 } 67 68 num_clusters = exfat_bytes_to_cluster_round_up(sbi, 69 offset + length) - exfat_bytes_to_cluster(sbi, offset); 70 71 mutex_lock(&sbi->s_lock); 72 iomap->bdev = inode->i_sb->s_bdev; 73 iomap->offset = offset; 74 75 err = exfat_map_cluster(inode, exfat_bytes_to_cluster(sbi, offset), 76 &cluster, &num_clusters, may_alloc, &balloc); 77 if (err) 78 goto out; 79 80 cluster_offset = exfat_cluster_offset(sbi, offset); 81 cluster_length = exfat_cluster_to_bytes(sbi, num_clusters); 82 83 iomap->length = min_t(loff_t, length, cluster_length - cluster_offset); 84 iomap->addr = exfat_cluster_to_phys_bytes(sbi, cluster) + cluster_offset; 85 iomap->type = IOMAP_MAPPED; 86 iomap->flags = IOMAP_F_MERGED; 87 88 if (may_alloc || flags & IOMAP_ZERO) { 89 if (balloc) 90 iomap->flags |= IOMAP_F_NEW; 91 else if (iomap->offset + iomap->length >= ei->valid_size) { 92 /* 93 * This is a write that starts at or extends beyond 94 * the current valid_size. The region between the old 95 * valid_size and the end of this write needs to be 96 * zeroed in the page cache to prevent stale data 97 * exposure (see IOMAP_F_ZERO_TAIL handling in 98 * __iomap_write_begin()). 99 */ 100 iomap->flags |= IOMAP_F_ZERO_TAIL; 101 } 102 } else { 103 /* 104 * valid_size is tracked in byte granularity and 105 * marks the exact boundary between valid data and 106 * holes (or unwritten space). 107 * 108 * When IOMAP_REPORT is set (used by lseek(SEEK_HOLE) 109 * and SEEK_DATA), we return IOMAP_HOLE. This allows 110 * iomap_seek_hole_iter() to directly return the 111 * precise byte position. 112 * 113 * For normal I/O paths (without IOMAP_REPORT) we 114 * return IOMAP_UNWRITTEN so the write path can 115 * distinguish it from a real hole. 116 */ 117 if (offset >= ei->valid_size) { 118 iomap->type = flags & IOMAP_REPORT ? 119 IOMAP_HOLE : IOMAP_UNWRITTEN; 120 } else if (offset + iomap->length > ei->valid_size) { 121 if (flags & IOMAP_REPORT) { 122 /* 123 * For SEEK_HOLE/SEEK_DATA, clip the length 124 * to the exact byte boundary (valid_size). 125 * This ensures the caller gets the precise 126 * hole position in byte units. 127 */ 128 iomap->length = ei->valid_size - iomap->offset; 129 } else 130 iomap->length = round_up(ei->valid_size, 131 i_blocksize(inode)) - 132 iomap->offset; 133 } 134 } 135 136 iomap->flags |= IOMAP_F_MERGED; 137 out: 138 mutex_unlock(&sbi->s_lock); 139 return err; 140 } 141 142 static int exfat_iomap_begin(struct inode *inode, loff_t offset, loff_t length, 143 unsigned int flags, struct iomap *iomap, struct iomap *srcmap) 144 { 145 return __exfat_iomap_begin(inode, offset, length, flags, iomap, false); 146 } 147 148 static int exfat_write_iomap_begin(struct inode *inode, loff_t offset, loff_t length, 149 unsigned int flags, struct iomap *iomap, struct iomap *srcmap) 150 { 151 return __exfat_iomap_begin(inode, offset, length, flags, iomap, true); 152 } 153 154 const struct iomap_ops exfat_iomap_ops = { 155 .iomap_begin = exfat_iomap_begin, 156 }; 157 158 /* 159 * exfat_write_iomap_end - Update the state after write 160 * 161 * Extends ->valid_size to cover the newly written range. 162 * Marks the inode dirty if metadata was changed. 163 */ 164 static int exfat_write_iomap_end(struct inode *inode, loff_t pos, loff_t length, 165 ssize_t written, unsigned int flags, struct iomap *iomap) 166 { 167 struct exfat_inode_info *ei = EXFAT_I(inode); 168 bool dirtied = false; 169 loff_t end; 170 171 if (!written) 172 return 0; 173 174 end = pos + written; 175 176 if (ei->valid_size < end) { 177 ei->valid_size = end; 178 if (ei->zeroed_size < end) 179 ei->zeroed_size = end; 180 dirtied = true; 181 } 182 183 if (dirtied || iomap->flags & IOMAP_F_SIZE_CHANGED) 184 mark_inode_dirty(inode); 185 186 return written; 187 } 188 189 const struct iomap_ops exfat_write_iomap_ops = { 190 .iomap_begin = exfat_write_iomap_begin, 191 .iomap_end = exfat_write_iomap_end, 192 }; 193 194 /* 195 * exfat_writeback_range - Map folio during writeback 196 * 197 * Called for each folio during writeback. If the folio falls outside the 198 * current iomap, remaps by calling read_iomap_begin. 199 */ 200 static ssize_t exfat_writeback_range(struct iomap_writepage_ctx *wpc, 201 struct folio *folio, u64 offset, unsigned int len, u64 end_pos) 202 { 203 if (offset < wpc->iomap.offset || 204 offset >= wpc->iomap.offset + wpc->iomap.length) { 205 int error; 206 207 error = __exfat_iomap_begin(wpc->inode, offset, len, 208 0, &wpc->iomap, false); 209 if (error) 210 return error; 211 } 212 213 return iomap_add_to_ioend(wpc, folio, offset, end_pos, len); 214 } 215 216 const struct iomap_writeback_ops exfat_writeback_ops = { 217 .writeback_range = exfat_writeback_range, 218 .writeback_submit = iomap_ioend_writeback_submit, 219 }; 220 221 /** 222 * exfat_iomap_read_end_io - iomap read bio completion handler for exFAT 223 * @bio: bio that has completed reading 224 * 225 * exfat_iomap_begin() rounds up MAPPED extents to the block boundary of 226 * valid_size. This ensures that any subsequent blocks are treated as 227 * IOMAP_UNWRITTEN, but it also causes the "straddle block" containing 228 * valid_size to be read from disk. The disk data beyond valid_size in 229 * this block is stale and must be zeroed to prevent data leakage. 230 */ 231 static void exfat_iomap_read_end_io(struct bio *bio) 232 { 233 int error = blk_status_to_errno(bio->bi_status); 234 struct folio_iter iter; 235 236 bio_for_each_folio_all(iter, bio) { 237 struct folio *folio = iter.folio; 238 struct exfat_inode_info *ei = EXFAT_I(folio->mapping->host); 239 s64 valid_size; 240 loff_t pos = folio_pos(folio); 241 242 valid_size = ei->valid_size; 243 if (pos + iter.offset < valid_size && 244 pos + iter.offset + iter.length > valid_size) 245 folio_zero_segment(folio, offset_in_folio(folio, valid_size), 246 iter.offset + iter.length); 247 248 iomap_finish_folio_read(folio, iter.offset, iter.length, error); 249 } 250 bio_put(bio); 251 } 252 253 static void exfat_iomap_bio_submit_read(const struct iomap_iter *iter, 254 struct iomap_read_folio_ctx *ctx) 255 { 256 struct bio *bio = ctx->read_ctx; 257 258 bio->bi_end_io = exfat_iomap_read_end_io; 259 submit_bio(bio); 260 } 261 262 const struct iomap_read_ops exfat_iomap_bio_read_ops = { 263 .read_folio_range = iomap_bio_read_folio_range, 264 .submit_read = exfat_iomap_bio_submit_read, 265 }; 266 267 int exfat_iomap_swap_activate(struct swap_info_struct *sis, 268 struct file *file, sector_t *span) 269 { 270 return iomap_swapfile_activate(sis, file, span, &exfat_iomap_ops); 271 } 272