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