xref: /linux/fs/xfs/xfs_aops.c (revision 27dd2772e6c62cf0627d97fe03950710df334cf4)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4  * Copyright (c) 2016-2025 Christoph Hellwig.
5  * All Rights Reserved.
6  */
7 #include "xfs_platform.h"
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_log_format.h"
11 #include "xfs_trans_resv.h"
12 #include "xfs_mount.h"
13 #include "xfs_inode.h"
14 #include "xfs_trans.h"
15 #include "xfs_iomap.h"
16 #include "xfs_trace.h"
17 #include "xfs_bmap.h"
18 #include "xfs_bmap_util.h"
19 #include "xfs_reflink.h"
20 #include "xfs_errortag.h"
21 #include "xfs_error.h"
22 #include "xfs_icache.h"
23 #include "xfs_ioend.h"
24 #include "xfs_zone_alloc.h"
25 #include "xfs_rtgroup.h"
26 
27 struct xfs_writepage_ctx {
28 	struct iomap_writepage_ctx ctx;
29 	unsigned int		data_seq;
30 	unsigned int		cow_seq;
31 };
32 
33 static inline struct xfs_writepage_ctx *
XFS_WPC(struct iomap_writepage_ctx * ctx)34 XFS_WPC(struct iomap_writepage_ctx *ctx)
35 {
36 	return container_of(ctx, struct xfs_writepage_ctx, ctx);
37 }
38 
39 /*
40  * Update on-disk file size now that data has been written to disk.
41  */
42 int
xfs_setfilesize(struct xfs_inode * ip,xfs_off_t offset,size_t size)43 xfs_setfilesize(
44 	struct xfs_inode	*ip,
45 	xfs_off_t		offset,
46 	size_t			size)
47 {
48 	struct xfs_mount	*mp = ip->i_mount;
49 	struct xfs_trans	*tp;
50 	xfs_fsize_t		isize;
51 	int			error;
52 
53 	error = xfs_trans_alloc(mp, &M_RES(mp)->tr_fsyncts, 0, 0, 0, &tp);
54 	if (error)
55 		return error;
56 
57 	xfs_ilock(ip, XFS_ILOCK_EXCL);
58 	isize = xfs_new_eof(ip, offset + size);
59 	if (!isize) {
60 		xfs_iunlock(ip, XFS_ILOCK_EXCL);
61 		xfs_trans_cancel(tp);
62 		return 0;
63 	}
64 
65 	trace_xfs_setfilesize(ip, offset, size);
66 
67 	ip->i_disk_size = isize;
68 	xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
69 	xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
70 
71 	return xfs_trans_commit(tp);
72 }
73 
74 /*
75  * We cannot cancel the ioend directly on error.  We may have already set other
76  * pages under writeback and hence we have to run I/O completion to mark the
77  * error state of the pages under writeback appropriately.
78  *
79  * If the folio has delalloc blocks on it, the caller is asking us to punch them
80  * out. If we don't, we can leave a stale delalloc mapping covered by a clean
81  * page that needs to be dirtied again before the delalloc mapping can be
82  * converted. This stale delalloc mapping can trip up a later direct I/O read
83  * operation on the same region.
84  *
85  * We prevent this by truncating away the delalloc regions on the folio. Because
86  * they are delalloc, we can do this without needing a transaction. Indeed - if
87  * we get ENOSPC errors, we have to be able to do this truncation without a
88  * transaction as there is no space left for block reservation (typically why
89  * we see a ENOSPC in writeback).
90  */
91 static void
xfs_discard_folio(struct folio * folio,loff_t pos)92 xfs_discard_folio(
93 	struct folio		*folio,
94 	loff_t			pos)
95 {
96 	struct xfs_inode	*ip = XFS_I(folio->mapping->host);
97 	struct xfs_mount	*mp = ip->i_mount;
98 
99 	if (xfs_is_shutdown(mp))
100 		return;
101 
102 	xfs_alert_ratelimited(mp,
103 		"page discard on page "PTR_FMT", inode 0x%llx, pos %llu.",
104 			folio, I_INO(ip), pos);
105 
106 	/*
107 	 * The end of the punch range is always the offset of the first
108 	 * byte of the next folio. Hence the end offset is only dependent on the
109 	 * folio itself and not the start offset that is passed in.
110 	 */
111 	xfs_bmap_punch_delalloc_range(ip, XFS_DATA_FORK, pos,
112 				folio_next_pos(folio), NULL);
113 }
114 
115 /*
116  * Fast revalidation of the cached writeback mapping. Return true if the current
117  * mapping is valid, false otherwise.
118  */
119 static bool
xfs_imap_valid(struct iomap_writepage_ctx * wpc,struct xfs_inode * ip,loff_t offset)120 xfs_imap_valid(
121 	struct iomap_writepage_ctx	*wpc,
122 	struct xfs_inode		*ip,
123 	loff_t				offset)
124 {
125 	if (offset < wpc->iomap.offset ||
126 	    offset >= wpc->iomap.offset + wpc->iomap.length)
127 		return false;
128 	/*
129 	 * If this is a COW mapping, it is sufficient to check that the mapping
130 	 * covers the offset. Be careful to check this first because the caller
131 	 * can revalidate a COW mapping without updating the data seqno.
132 	 */
133 	if (wpc->iomap.flags & IOMAP_F_SHARED)
134 		return true;
135 
136 	/*
137 	 * This is not a COW mapping. Check the sequence number of the data fork
138 	 * because concurrent changes could have invalidated the extent. Check
139 	 * the COW fork because concurrent changes since the last time we
140 	 * checked (and found nothing at this offset) could have added
141 	 * overlapping blocks.
142 	 */
143 	if (XFS_WPC(wpc)->data_seq != READ_ONCE(ip->i_df.if_seq)) {
144 		trace_xfs_wb_data_iomap_invalid(ip, &wpc->iomap,
145 				XFS_WPC(wpc)->data_seq, XFS_DATA_FORK);
146 		return false;
147 	}
148 	if (xfs_inode_has_cow_data(ip) &&
149 	    XFS_WPC(wpc)->cow_seq != READ_ONCE(ip->i_cowfp->if_seq)) {
150 		trace_xfs_wb_cow_iomap_invalid(ip, &wpc->iomap,
151 				XFS_WPC(wpc)->cow_seq, XFS_COW_FORK);
152 		return false;
153 	}
154 	return true;
155 }
156 
157 static int
xfs_map_blocks(struct iomap_writepage_ctx * wpc,loff_t offset,unsigned int len)158 xfs_map_blocks(
159 	struct iomap_writepage_ctx *wpc,
160 	loff_t			offset,
161 	unsigned int		len)
162 {
163 	struct xfs_inode	*ip = XFS_I(wpc->inode);
164 	struct xfs_mount	*mp = ip->i_mount;
165 	ssize_t			count = i_blocksize(wpc->inode);
166 	xfs_fileoff_t		offset_fsb = XFS_B_TO_FSBT(mp, offset);
167 	xfs_fileoff_t		end_fsb = XFS_B_TO_FSB(mp, offset + count);
168 	xfs_fileoff_t		cow_fsb;
169 	int			whichfork;
170 	struct xfs_bmbt_irec	imap;
171 	struct xfs_iext_cursor	icur;
172 	int			retries = 0;
173 	int			error = 0;
174 	unsigned int		*seq;
175 
176 	if (xfs_is_shutdown(mp))
177 		return -EIO;
178 
179 	XFS_ERRORTAG_DELAY(mp, XFS_ERRTAG_WB_DELAY_MS);
180 
181 	/*
182 	 * COW fork blocks can overlap data fork blocks even if the blocks
183 	 * aren't shared.  COW I/O always takes precedent, so we must always
184 	 * check for overlap on reflink inodes unless the mapping is already a
185 	 * COW one, or the COW fork hasn't changed from the last time we looked
186 	 * at it.
187 	 *
188 	 * It's safe to check the COW fork if_seq here without the ILOCK because
189 	 * we've indirectly protected against concurrent updates: writeback has
190 	 * the page locked, which prevents concurrent invalidations by reflink
191 	 * and directio and prevents concurrent buffered writes to the same
192 	 * page.  Changes to if_seq always happen under i_lock, which protects
193 	 * against concurrent updates and provides a memory barrier on the way
194 	 * out that ensures that we always see the current value.
195 	 */
196 	if (xfs_imap_valid(wpc, ip, offset))
197 		return 0;
198 
199 	/*
200 	 * If we don't have a valid map, now it's time to get a new one for this
201 	 * offset.  This will convert delayed allocations (including COW ones)
202 	 * into real extents.  If we return without a valid map, it means we
203 	 * landed in a hole and we skip the block.
204 	 */
205 retry:
206 	cow_fsb = NULLFILEOFF;
207 	whichfork = XFS_DATA_FORK;
208 	xfs_ilock(ip, XFS_ILOCK_SHARED);
209 	ASSERT(!xfs_need_iread_extents(&ip->i_df));
210 
211 	/*
212 	 * Check if this is offset is covered by a COW extents, and if yes use
213 	 * it directly instead of looking up anything in the data fork.
214 	 */
215 	if (xfs_inode_has_cow_data(ip) &&
216 	    xfs_iext_lookup_extent(ip, ip->i_cowfp, offset_fsb, &icur, &imap))
217 		cow_fsb = imap.br_startoff;
218 	if (cow_fsb != NULLFILEOFF && cow_fsb <= offset_fsb) {
219 		XFS_WPC(wpc)->cow_seq = READ_ONCE(ip->i_cowfp->if_seq);
220 		xfs_iunlock(ip, XFS_ILOCK_SHARED);
221 
222 		whichfork = XFS_COW_FORK;
223 		goto allocate_blocks;
224 	}
225 
226 	/*
227 	 * No COW extent overlap. Revalidate now that we may have updated
228 	 * ->cow_seq. If the data mapping is still valid, we're done.
229 	 */
230 	if (xfs_imap_valid(wpc, ip, offset)) {
231 		xfs_iunlock(ip, XFS_ILOCK_SHARED);
232 		return 0;
233 	}
234 
235 	/*
236 	 * If we don't have a valid map, now it's time to get a new one for this
237 	 * offset.  This will convert delayed allocations (including COW ones)
238 	 * into real extents.
239 	 */
240 	if (!xfs_iext_lookup_extent(ip, &ip->i_df, offset_fsb, &icur, &imap))
241 		imap.br_startoff = end_fsb;	/* fake a hole past EOF */
242 	XFS_WPC(wpc)->data_seq = READ_ONCE(ip->i_df.if_seq);
243 	xfs_iunlock(ip, XFS_ILOCK_SHARED);
244 
245 	/* landed in a hole or beyond EOF? */
246 	if (imap.br_startoff > offset_fsb) {
247 		imap.br_blockcount = imap.br_startoff - offset_fsb;
248 		imap.br_startoff = offset_fsb;
249 		imap.br_startblock = HOLESTARTBLOCK;
250 		imap.br_state = XFS_EXT_NORM;
251 	}
252 
253 	/*
254 	 * Truncate to the next COW extent if there is one.  This is the only
255 	 * opportunity to do this because we can skip COW fork lookups for the
256 	 * subsequent blocks in the mapping; however, the requirement to treat
257 	 * the COW range separately remains.
258 	 */
259 	if (cow_fsb != NULLFILEOFF &&
260 	    cow_fsb < imap.br_startoff + imap.br_blockcount)
261 		imap.br_blockcount = cow_fsb - imap.br_startoff;
262 
263 	/* got a delalloc extent? */
264 	if (imap.br_startblock != HOLESTARTBLOCK &&
265 	    isnullstartblock(imap.br_startblock))
266 		goto allocate_blocks;
267 
268 	xfs_bmbt_to_iomap(ip, &wpc->iomap, &imap, 0, 0, XFS_WPC(wpc)->data_seq);
269 	trace_xfs_map_blocks_found(ip, offset, count, whichfork, &imap);
270 	return 0;
271 allocate_blocks:
272 	/*
273 	 * Convert a dellalloc extent to a real one. The current page is held
274 	 * locked so nothing could have removed the block backing offset_fsb,
275 	 * although it could have moved from the COW to the data fork by another
276 	 * thread.
277 	 */
278 	if (whichfork == XFS_COW_FORK)
279 		seq = &XFS_WPC(wpc)->cow_seq;
280 	else
281 		seq = &XFS_WPC(wpc)->data_seq;
282 
283 	error = xfs_bmapi_convert_delalloc(ip, whichfork, offset,
284 				&wpc->iomap, seq);
285 	if (error) {
286 		/*
287 		 * If we failed to find the extent in the COW fork we might have
288 		 * raced with a COW to data fork conversion or truncate.
289 		 * Restart the lookup to catch the extent in the data fork for
290 		 * the former case, but prevent additional retries to avoid
291 		 * looping forever for the latter case.
292 		 */
293 		if (error == -EAGAIN && whichfork == XFS_COW_FORK && !retries++)
294 			goto retry;
295 		ASSERT(error != -EAGAIN);
296 		return error;
297 	}
298 
299 	/*
300 	 * Due to merging the return real extent might be larger than the
301 	 * original delalloc one.  Trim the return extent to the next COW
302 	 * boundary again to force a re-lookup.
303 	 */
304 	if (whichfork != XFS_COW_FORK && cow_fsb != NULLFILEOFF) {
305 		loff_t		cow_offset = XFS_FSB_TO_B(mp, cow_fsb);
306 
307 		if (cow_offset < wpc->iomap.offset + wpc->iomap.length)
308 			wpc->iomap.length = cow_offset - wpc->iomap.offset;
309 	}
310 
311 	ASSERT(wpc->iomap.offset <= offset);
312 	ASSERT(wpc->iomap.offset + wpc->iomap.length > offset);
313 	trace_xfs_map_blocks_alloc(ip, offset, count, whichfork, &imap);
314 	return 0;
315 }
316 
317 static ssize_t
xfs_writeback_range(struct iomap_writepage_ctx * wpc,struct folio * folio,u64 offset,unsigned int len,u64 end_pos)318 xfs_writeback_range(
319 	struct iomap_writepage_ctx *wpc,
320 	struct folio		*folio,
321 	u64			offset,
322 	unsigned int		len,
323 	u64			end_pos)
324 {
325 	ssize_t			ret;
326 
327 	ret = xfs_map_blocks(wpc, offset, len);
328 	if (!ret)
329 		ret = iomap_add_to_ioend(wpc, folio, offset, end_pos, len);
330 	if (ret < 0)
331 		xfs_discard_folio(folio, offset);
332 	return ret;
333 }
334 
335 static bool
xfs_ioend_needs_wq_completion(struct iomap_ioend * ioend)336 xfs_ioend_needs_wq_completion(
337 	struct iomap_ioend	*ioend)
338 {
339 	/* Changing inode size requires a transaction. */
340 	if (xfs_ioend_is_append(ioend))
341 		return true;
342 
343 	/* Extent manipulation requires a transaction. */
344 	if (ioend->io_flags & (IOMAP_IOEND_UNWRITTEN | IOMAP_IOEND_SHARED))
345 		return true;
346 
347 	return false;
348 }
349 
350 static int
xfs_writeback_submit(struct iomap_writepage_ctx * wpc,int error)351 xfs_writeback_submit(
352 	struct iomap_writepage_ctx	*wpc,
353 	int				error)
354 {
355 	struct iomap_ioend		*ioend = wpc->wb_ctx;
356 
357 	/*
358 	 * Convert CoW extents to regular.
359 	 *
360 	 * We can allocate memory here while doing writeback on behalf of memory
361 	 * reclaim.  To avoid memory allocation deadlocks, set the task-wide
362 	 * nofs context.
363 	 */
364 	if (!error && (ioend->io_flags & IOMAP_IOEND_SHARED)) {
365 		unsigned int		nofs_flag;
366 
367 		nofs_flag = memalloc_nofs_save();
368 		error = xfs_reflink_convert_cow(XFS_I(ioend->io_inode),
369 				ioend->io_offset, ioend->io_size);
370 		memalloc_nofs_restore(nofs_flag);
371 	}
372 
373 	/*
374 	 * Send ioends that might require a transaction to the completion wq,
375 	 * and disable the block layer task completion for them as there is no
376 	 * need to defer twice.
377 	 */
378 	if (xfs_ioend_needs_wq_completion(ioend)) {
379 		ioend->io_bio.bi_end_io = xfs_end_bio;
380 		bio_clear_flag(&ioend->io_bio, BIO_COMPLETE_IN_TASK);
381 	}
382 
383 	return iomap_ioend_writeback_submit(wpc, error);
384 }
385 
386 static const struct iomap_writeback_ops xfs_writeback_ops = {
387 	.writeback_range	= xfs_writeback_range,
388 	.writeback_submit	= xfs_writeback_submit,
389 };
390 
391 struct xfs_zoned_writepage_ctx {
392 	struct iomap_writepage_ctx	ctx;
393 	struct xfs_open_zone		*open_zone;
394 };
395 
396 static inline struct xfs_zoned_writepage_ctx *
XFS_ZWPC(struct iomap_writepage_ctx * ctx)397 XFS_ZWPC(struct iomap_writepage_ctx *ctx)
398 {
399 	return container_of(ctx, struct xfs_zoned_writepage_ctx, ctx);
400 }
401 
402 static int
xfs_zoned_map_blocks(struct iomap_writepage_ctx * wpc,loff_t offset,unsigned int len)403 xfs_zoned_map_blocks(
404 	struct iomap_writepage_ctx *wpc,
405 	loff_t			offset,
406 	unsigned int		len)
407 {
408 	struct xfs_inode	*ip = XFS_I(wpc->inode);
409 	struct xfs_mount	*mp = ip->i_mount;
410 	xfs_fileoff_t		offset_fsb = XFS_B_TO_FSBT(mp, offset);
411 	xfs_fileoff_t		end_fsb = XFS_B_TO_FSB(mp, offset + len);
412 	xfs_filblks_t		count_fsb;
413 	struct xfs_bmbt_irec	imap, del;
414 	struct xfs_iext_cursor	icur;
415 
416 	if (xfs_is_shutdown(mp))
417 		return -EIO;
418 
419 	XFS_ERRORTAG_DELAY(mp, XFS_ERRTAG_WB_DELAY_MS);
420 
421 	/*
422 	 * All dirty data must be covered by delalloc extents.  But truncate can
423 	 * remove delalloc extents underneath us or reduce their size.
424 	 * Returning a hole tells iomap to not write back any data from this
425 	 * range, which is the right thing to do in that case.
426 	 *
427 	 * Otherwise just tell iomap to treat ranges previously covered by a
428 	 * delalloc extent as mapped.  The actual block allocation will be done
429 	 * just before submitting the bio.
430 	 *
431 	 * This implies we never map outside folios that are locked or marked
432 	 * as under writeback, and thus there is no need check the fork sequence
433 	 * count here.
434 	 */
435 	xfs_ilock(ip, XFS_ILOCK_EXCL);
436 	if (!xfs_iext_lookup_extent(ip, ip->i_cowfp, offset_fsb, &icur, &imap))
437 		imap.br_startoff = end_fsb;	/* fake a hole past EOF */
438 	if (imap.br_startoff > offset_fsb) {
439 		imap.br_blockcount = imap.br_startoff - offset_fsb;
440 		imap.br_startoff = offset_fsb;
441 		imap.br_startblock = HOLESTARTBLOCK;
442 		imap.br_state = XFS_EXT_NORM;
443 		xfs_iunlock(ip, XFS_ILOCK_EXCL);
444 		xfs_bmbt_to_iomap(ip, &wpc->iomap, &imap, 0, 0, 0);
445 		return 0;
446 	}
447 	end_fsb = min(end_fsb, imap.br_startoff + imap.br_blockcount);
448 	count_fsb = end_fsb - offset_fsb;
449 
450 	del = imap;
451 	xfs_trim_extent(&del, offset_fsb, count_fsb);
452 	xfs_bmap_del_extent_delay(ip, XFS_COW_FORK, &icur, &imap, &del,
453 			XFS_BMAPI_REMAP);
454 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
455 
456 	xfs_iomap_set_anon_write(ip, &wpc->iomap, offset,
457 			XFS_FSB_TO_B(mp, count_fsb));
458 	trace_xfs_zoned_map_blocks(ip, offset, wpc->iomap.length);
459 	return 0;
460 }
461 
462 static ssize_t
xfs_zoned_writeback_range(struct iomap_writepage_ctx * wpc,struct folio * folio,u64 offset,unsigned int len,u64 end_pos)463 xfs_zoned_writeback_range(
464 	struct iomap_writepage_ctx *wpc,
465 	struct folio		*folio,
466 	u64			offset,
467 	unsigned int		len,
468 	u64			end_pos)
469 {
470 	ssize_t			ret;
471 
472 	ret = xfs_zoned_map_blocks(wpc, offset, len);
473 	if (!ret)
474 		ret = iomap_add_to_ioend(wpc, folio, offset, end_pos, len);
475 	if (ret < 0)
476 		xfs_discard_folio(folio, offset);
477 	return ret;
478 }
479 
480 static int
xfs_zoned_writeback_submit(struct iomap_writepage_ctx * wpc,int error)481 xfs_zoned_writeback_submit(
482 	struct iomap_writepage_ctx	*wpc,
483 	int				error)
484 {
485 	struct iomap_ioend		*ioend = wpc->wb_ctx;
486 
487 	/*
488 	 * Defer all completions to our workqueue as all zoned writes require a
489 	 * transaction to be persisted. This also means we never need the block
490 	 * layer in-task completion for a task context.
491 	 */
492 	ioend->io_bio.bi_end_io = xfs_end_bio;
493 	bio_clear_flag(&ioend->io_bio, BIO_COMPLETE_IN_TASK);
494 
495 	if (error) {
496 		ioend->io_bio.bi_status = errno_to_blk_status(error);
497 		bio_endio(&ioend->io_bio);
498 		return error;
499 	}
500 
501 	xfs_zone_alloc_and_submit(ioend, &XFS_ZWPC(wpc)->open_zone);
502 	return 0;
503 }
504 
505 static const struct iomap_writeback_ops xfs_zoned_writeback_ops = {
506 	.writeback_range	= xfs_zoned_writeback_range,
507 	.writeback_submit	= xfs_zoned_writeback_submit,
508 };
509 
510 STATIC int
xfs_vm_writepages(struct address_space * mapping,struct writeback_control * wbc)511 xfs_vm_writepages(
512 	struct address_space	*mapping,
513 	struct writeback_control *wbc)
514 {
515 	struct xfs_inode	*ip = XFS_I(mapping->host);
516 
517 	xfs_iflags_clear(ip, XFS_ITRUNCATED);
518 
519 	if (xfs_is_zoned_inode(ip)) {
520 		struct xfs_zoned_writepage_ctx	xc = {
521 			.ctx = {
522 				.inode	= mapping->host,
523 				.wbc	= wbc,
524 				.ops	= &xfs_zoned_writeback_ops
525 			},
526 		};
527 		int				error;
528 
529 		error = iomap_writepages(&xc.ctx);
530 		if (xc.open_zone)
531 			xfs_open_zone_put(xc.open_zone);
532 		return error;
533 	} else {
534 		struct xfs_writepage_ctx	wpc = {
535 			.ctx = {
536 				.inode	= mapping->host,
537 				.wbc	= wbc,
538 				.ops	= &xfs_writeback_ops
539 			},
540 		};
541 
542 		return iomap_writepages(&wpc.ctx);
543 	}
544 }
545 
546 STATIC int
xfs_dax_writepages(struct address_space * mapping,struct writeback_control * wbc)547 xfs_dax_writepages(
548 	struct address_space	*mapping,
549 	struct writeback_control *wbc)
550 {
551 	struct xfs_inode	*ip = XFS_I(mapping->host);
552 
553 	xfs_iflags_clear(ip, XFS_ITRUNCATED);
554 	return dax_writeback_mapping_range(mapping,
555 			xfs_inode_buftarg(ip)->bt_daxdev, wbc);
556 }
557 
558 STATIC sector_t
xfs_vm_bmap(struct address_space * mapping,sector_t block)559 xfs_vm_bmap(
560 	struct address_space	*mapping,
561 	sector_t		block)
562 {
563 	struct xfs_inode	*ip = XFS_I(mapping->host);
564 
565 	trace_xfs_vm_bmap(ip);
566 
567 	/*
568 	 * The swap code (ab-)uses ->bmap to get a block mapping and then
569 	 * bypasses the file system for actual I/O.  We really can't allow
570 	 * that on reflinks inodes, so we have to skip out here.  And yes,
571 	 * 0 is the magic code for a bmap error.
572 	 *
573 	 * Since we don't pass back blockdev info, we can't return bmap
574 	 * information for rt files either.
575 	 */
576 	if (xfs_is_cow_inode(ip) || XFS_IS_REALTIME_INODE(ip))
577 		return 0;
578 	return iomap_bmap(mapping, block, &xfs_read_iomap_ops);
579 }
580 
581 static void
xfs_bio_submit_read(const struct iomap_iter * iter,struct iomap_read_folio_ctx * ctx)582 xfs_bio_submit_read(
583 	const struct iomap_iter		*iter,
584 	struct iomap_read_folio_ctx	*ctx)
585 {
586 	xfs_ioend_submit_read(iter->inode, ctx->read_ctx,
587 			ctx->read_ctx_file_offset,
588 			iomap_ioend_flags(&iter->iomap));
589 	ctx->read_ctx = NULL;
590 }
591 
592 static const struct iomap_read_ops xfs_iomap_read_ops = {
593 	.read_folio_range	= iomap_bio_read_folio_range,
594 	.submit_read		= xfs_bio_submit_read,
595 	.bio_set		= &iomap_ioend_bioset,
596 };
597 
598 static inline const struct iomap_read_ops *
xfs_get_iomap_read_ops(const struct address_space * mapping)599 xfs_get_iomap_read_ops(
600 	const struct address_space	*mapping)
601 {
602 	struct xfs_inode		*ip = XFS_I(mapping->host);
603 
604 	if (bdev_has_integrity_csum(xfs_inode_buftarg(ip)->bt_bdev))
605 		return &xfs_iomap_read_ops;
606 	return &iomap_bio_read_ops;
607 }
608 
609 STATIC int
xfs_vm_read_folio(struct file * file,struct folio * folio)610 xfs_vm_read_folio(
611 	struct file			*file,
612 	struct folio			*folio)
613 {
614 	struct iomap_read_folio_ctx	ctx = { .cur_folio = folio };
615 
616 	ctx.ops = xfs_get_iomap_read_ops(folio->mapping);
617 	iomap_read_folio(&xfs_read_iomap_ops, &ctx, NULL);
618 	return 0;
619 }
620 
621 STATIC void
xfs_vm_readahead(struct readahead_control * rac)622 xfs_vm_readahead(
623 	struct readahead_control	*rac)
624 {
625 	struct iomap_read_folio_ctx	ctx = { .rac = rac };
626 
627 	ctx.ops = xfs_get_iomap_read_ops(rac->mapping),
628 	iomap_readahead(&xfs_read_iomap_ops, &ctx, NULL);
629 }
630 
631 static int
xfs_vm_swap_activate(struct swap_info_struct * sis,struct file * swap_file,sector_t * span)632 xfs_vm_swap_activate(
633 	struct swap_info_struct		*sis,
634 	struct file			*swap_file,
635 	sector_t			*span)
636 {
637 	struct xfs_inode		*ip = XFS_I(file_inode(swap_file));
638 
639 	if (xfs_is_zoned_inode(ip))
640 		return -EINVAL;
641 
642 	/*
643 	 * Swap file activation can race against concurrent shared extent
644 	 * removal in files that have been cloned.  If this happens,
645 	 * iomap_swapfile_iter() can fail because it encountered a shared
646 	 * extent even though an operation is in progress to remove those
647 	 * shared extents.
648 	 *
649 	 * This race becomes problematic when we defer extent removal
650 	 * operations beyond the end of a syscall (i.e. use async background
651 	 * processing algorithms).  Users think the extents are no longer
652 	 * shared, but iomap_swapfile_iter() still sees them as shared
653 	 * because the refcountbt entries for the extents being removed have
654 	 * not yet been updated.  Hence the swapon call fails unexpectedly.
655 	 *
656 	 * The race condition is currently most obvious from the unlink()
657 	 * operation as extent removal is deferred until after the last
658 	 * reference to the inode goes away.  We then process the extent
659 	 * removal asynchronously, hence triggers the "syscall completed but
660 	 * work not done" condition mentioned above.  To close this race
661 	 * window, we need to flush any pending inodegc operations to ensure
662 	 * they have updated the refcountbt records before we try to map the
663 	 * swapfile.
664 	 */
665 	xfs_inodegc_flush(ip->i_mount);
666 
667 	/*
668 	 * Direct the swap code to the correct block device when this file
669 	 * sits on the RT device.
670 	 */
671 	sis->bdev = xfs_inode_buftarg(ip)->bt_bdev;
672 
673 	return iomap_swapfile_activate(sis, swap_file, span,
674 			&xfs_read_iomap_ops);
675 }
676 
677 const struct address_space_operations xfs_address_space_operations = {
678 	.read_folio		= xfs_vm_read_folio,
679 	.readahead		= xfs_vm_readahead,
680 	.writepages		= xfs_vm_writepages,
681 	.dirty_folio		= iomap_dirty_folio,
682 	.release_folio		= iomap_release_folio,
683 	.invalidate_folio	= iomap_invalidate_folio,
684 	.bmap			= xfs_vm_bmap,
685 	.migrate_folio		= filemap_migrate_folio,
686 	.is_partially_uptodate  = iomap_is_partially_uptodate,
687 	.error_remove_folio	= generic_error_remove_folio,
688 	.swap_activate		= xfs_vm_swap_activate,
689 };
690 
691 const struct address_space_operations xfs_dax_aops = {
692 	.writepages		= xfs_dax_writepages,
693 	.dirty_folio		= noop_dirty_folio,
694 	.swap_activate		= xfs_vm_swap_activate,
695 };
696