xref: /linux/fs/exfat/iomap.c (revision 9e8fc16dc6b474b9a87843c3d3ee12fc83f2bdcc)
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 	loff_t valid_size = exfat_get_valid_size(ei);
50 	int err;
51 	bool balloc = false;
52 
53 	if (!may_alloc) {
54 		/* Completely beyond EOF. Treat as hole */
55 		if (i_size_read(inode) <= offset) {
56 			iomap->type = IOMAP_HOLE;
57 			iomap->addr = IOMAP_NULL_ADDR;
58 			iomap->offset = offset;
59 			iomap->length = length;
60 			return 0;
61 		}
62 
63 		/* Clamp length if the requested range goes beyond i_size */
64 		if (offset + length > i_size_read(inode))
65 			length = round_up(i_size_read(inode),
66 					  i_blocksize(inode)) - offset;
67 	}
68 
69 	num_clusters = exfat_bytes_to_cluster_round_up(sbi,
70 			offset + length) - exfat_bytes_to_cluster(sbi, offset);
71 
72 	mutex_lock(&sbi->s_lock);
73 	iomap->bdev = inode->i_sb->s_bdev;
74 	iomap->offset = offset;
75 
76 	err = exfat_map_cluster(inode, exfat_bytes_to_cluster(sbi, offset),
77 			&cluster, &num_clusters, may_alloc, &balloc);
78 	if (err == -ENOSPC && !(flags & IOMAP_DIRECT)) {
79 		/*
80 		 * There are not enough free clusters for the whole range. A
81 		 * buffered write can be short, so map what still fits.
82 		 */
83 		num_clusters = 1;
84 		err = exfat_map_cluster(inode,
85 					exfat_bytes_to_cluster(sbi, offset),
86 					&cluster, &num_clusters, may_alloc,
87 					&balloc);
88 	}
89 	if (err)
90 		goto out;
91 
92 	cluster_offset = exfat_cluster_offset(sbi, offset);
93 	cluster_length = exfat_cluster_to_bytes(sbi, num_clusters);
94 
95 	iomap->length = min_t(loff_t, length, cluster_length - cluster_offset);
96 	iomap->addr = exfat_cluster_to_phys_bytes(sbi, cluster) + cluster_offset;
97 	iomap->type = IOMAP_MAPPED;
98 	iomap->flags = IOMAP_F_MERGED;
99 
100 	if (may_alloc || flags & IOMAP_ZERO) {
101 		if (balloc)
102 			iomap->flags |= IOMAP_F_NEW;
103 		else if (iomap->offset + iomap->length >= valid_size) {
104 			/*
105 			 * This is a write that starts at or extends beyond
106 			 * the current valid_size. The region between the old
107 			 * valid_size and the end of this write needs to be
108 			 * zeroed in the page cache to prevent stale data
109 			 * exposure (see IOMAP_F_ZERO_TAIL handling in
110 			 * __iomap_write_begin()).
111 			 */
112 			iomap->flags |= IOMAP_F_ZERO_TAIL;
113 		}
114 	} else {
115 		/*
116 		 * valid_size is tracked in byte granularity and
117 		 * marks the exact boundary between valid data and
118 		 * holes (or unwritten space).
119 		 *
120 		 * When IOMAP_REPORT is set (used by lseek(SEEK_HOLE)
121 		 * and SEEK_DATA), we return IOMAP_HOLE. This allows
122 		 * iomap_seek_hole_iter() to directly return the
123 		 * precise byte position.
124 		 *
125 		 * For normal I/O paths (without IOMAP_REPORT) we
126 		 * return IOMAP_UNWRITTEN so the write path can
127 		 * distinguish it from a real hole.
128 		 */
129 		if (offset >= valid_size) {
130 			iomap->type = flags & IOMAP_REPORT ?
131 				IOMAP_HOLE : IOMAP_UNWRITTEN;
132 		} else if (offset + iomap->length > valid_size) {
133 			if (flags & IOMAP_REPORT) {
134 				/*
135 				 * For SEEK_HOLE/SEEK_DATA, clip the length
136 				 * to the exact byte boundary (valid_size).
137 				 * This ensures the caller gets the precise
138 				 * hole position in byte units.
139 				 */
140 				iomap->length = valid_size - iomap->offset;
141 			} else
142 				iomap->length = round_up(valid_size,
143 							 i_blocksize(inode)) -
144 								iomap->offset;
145 		}
146 	}
147 
148 	iomap->flags |= IOMAP_F_MERGED;
149 out:
150 	mutex_unlock(&sbi->s_lock);
151 	return err;
152 }
153 
154 static int exfat_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
155 		unsigned int flags, struct iomap *iomap, struct iomap *srcmap)
156 {
157 	return __exfat_iomap_begin(inode, offset, length, flags, iomap, false);
158 }
159 
160 static int exfat_write_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
161 		unsigned int flags, struct iomap *iomap, struct iomap *srcmap)
162 {
163 	struct exfat_inode_info *ei = EXFAT_I(inode);
164 	loff_t valid_size = exfat_get_valid_size(ei);
165 	loff_t end;
166 	int err;
167 
168 	err = __exfat_iomap_begin(inode, offset, length, flags, iomap, true);
169 	if (err || flags & (IOMAP_DIRECT | IOMAP_FAULT) ||
170 	    iomap->flags & IOMAP_F_NEW)
171 		return err;
172 
173 	end = iomap->offset + iomap->length;
174 	if (end >= valid_size ||
175 	    round_up(end, i_blocksize(inode)) <= valid_size)
176 		return 0;
177 
178 	/*
179 	 * Zero the invalid bytes from valid_size to the end of the block
180 	 * to prevent stale data from being exposed through the page cache.
181 	 */
182 	return iomap_truncate_page(inode, valid_size, NULL, exfat_iomap_next,
183 				   NULL, NULL);
184 }
185 
186 DEFINE_IOMAP_ITER_NEXT(exfat_iomap_next, exfat_iomap_begin);
187 
188 #ifdef CONFIG_SWAP
189 static int exfat_swap_iomap_begin(struct inode *inode, loff_t offset,
190 		loff_t length, unsigned int flags, struct iomap *iomap,
191 		struct iomap *srcmap)
192 {
193 	/*
194 	 * Swap activation needs the physical mappings of preallocated
195 	 * ranges. Do not report the VDL tail as a hole.
196 	 */
197 	return __exfat_iomap_begin(inode, offset, length,
198 			flags & ~IOMAP_REPORT, iomap, false);
199 }
200 
201 static DEFINE_IOMAP_ITER_NEXT(exfat_swap_iomap_next,
202 		exfat_swap_iomap_begin);
203 #endif
204 
205 /*
206  * exfat_write_iomap_end - Update the state after write
207  *
208  * Extends ->valid_size to cover the newly written range.
209  * Marks the inode dirty if metadata was changed.
210  */
211 static int exfat_write_iomap_end(struct inode *inode, loff_t pos, loff_t length,
212 		ssize_t written, unsigned int flags, struct iomap *iomap)
213 {
214 	struct exfat_inode_info *ei = EXFAT_I(inode);
215 	bool dirtied = false;
216 	loff_t end;
217 
218 	if (!written)
219 		return 0;
220 
221 	end = pos + written;
222 
223 	if (exfat_advance_valid_size(ei, end))
224 		dirtied = true;
225 
226 	/*
227 	 * IOMAP_F_ZERO_TAIL zeroes the remainder of the last block. Track that
228 	 * block as zeroed so later valid_size extensions do not zero it again.
229 	 */
230 	if (iomap->flags & IOMAP_F_ZERO_TAIL)
231 		end = round_up(end, i_blocksize(inode));
232 	exfat_advance_zeroed_size(ei, end);
233 
234 	if (dirtied || iomap->flags & IOMAP_F_SIZE_CHANGED)
235 		mark_inode_dirty(inode);
236 
237 	return written;
238 }
239 
240 DEFINE_IOMAP_ITER_NEXT_END(exfat_write_iomap_next, exfat_write_iomap_begin,
241 		exfat_write_iomap_end);
242 
243 /*
244  * exfat_writeback_range - Map folio during writeback
245  *
246  * Called for each folio during writeback. If the folio falls outside the
247  * current iomap, remaps by calling read_iomap_begin.
248  */
249 static ssize_t exfat_writeback_range(struct iomap_writepage_ctx *wpc,
250 		struct folio *folio, u64 offset, unsigned int len, u64 end_pos)
251 {
252 	if (offset < wpc->iomap.offset ||
253 	    offset >= wpc->iomap.offset + wpc->iomap.length) {
254 		int error;
255 
256 		error = __exfat_iomap_begin(wpc->inode, offset, len,
257 				0, &wpc->iomap, false);
258 		if (error)
259 			return error;
260 	}
261 
262 	return iomap_add_to_ioend(wpc, folio, offset, end_pos, len);
263 }
264 
265 const struct iomap_writeback_ops exfat_writeback_ops = {
266 	.writeback_range	= exfat_writeback_range,
267 	.writeback_submit	= iomap_ioend_writeback_submit,
268 };
269 
270 /**
271  * exfat_iomap_read_end_io - iomap read bio completion handler for exFAT
272  * @bio: bio that has completed reading
273  *
274  * exfat_iomap_begin() rounds up MAPPED extents to the block boundary of
275  * valid_size. This ensures that any subsequent blocks are treated as
276  * IOMAP_UNWRITTEN, but it also causes the "straddle block" containing
277  * valid_size to be read from disk. The disk data beyond valid_size in
278  * this block is stale and must be zeroed to prevent data leakage.
279  */
280 static void exfat_iomap_read_end_io(struct bio *bio)
281 {
282 	int error = blk_status_to_errno(bio->bi_status);
283 	struct folio_iter iter;
284 
285 	bio_for_each_folio_all(iter, bio) {
286 		struct folio *folio = iter.folio;
287 		struct exfat_inode_info *ei = EXFAT_I(folio->mapping->host);
288 		s64 valid_size;
289 		loff_t pos = folio_pos(folio);
290 
291 		valid_size = exfat_get_valid_size(ei);
292 		if (pos + iter.offset < valid_size &&
293 		    pos + iter.offset + iter.length > valid_size)
294 			folio_zero_segment(folio, offset_in_folio(folio, valid_size),
295 					   iter.offset + iter.length);
296 
297 		iomap_finish_folio_read(folio, iter.offset, iter.length, error);
298 	}
299 	bio_put(bio);
300 }
301 
302 static void exfat_iomap_bio_submit_read(const struct iomap_iter *iter,
303 		struct iomap_read_folio_ctx *ctx)
304 {
305 	iomap_bio_submit_read_endio(iter, ctx, exfat_iomap_read_end_io);
306 }
307 
308 const struct iomap_read_ops exfat_iomap_bio_read_ops = {
309 	.read_folio_range	= iomap_bio_read_folio_range,
310 	.submit_read		= exfat_iomap_bio_submit_read,
311 };
312 
313 int exfat_iomap_swap_activate(struct swap_info_struct *sis,
314 			       struct file *file, sector_t *span)
315 {
316 #ifdef CONFIG_SWAP
317 	return iomap_swapfile_activate(sis, file, span,
318 				       exfat_swap_iomap_next);
319 #else
320 	return -EIO;
321 #endif
322 }
323