1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4 * Copyright (c) 2016-2018 Christoph Hellwig.
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"
18 #include "xfs_bmap_util.h"
19 #include "xfs_reflink.h"
22 * structure owned by writepages passed to individual writepage calls
24 struct xfs_writepage_ctx {
25 struct xfs_bmbt_irec imap;
27 unsigned int data_seq;
29 struct xfs_ioend *ioend;
33 xfs_find_bdev_for_inode(
36 struct xfs_inode *ip = XFS_I(inode);
37 struct xfs_mount *mp = ip->i_mount;
39 if (XFS_IS_REALTIME_INODE(ip))
40 return mp->m_rtdev_targp->bt_bdev;
42 return mp->m_ddev_targp->bt_bdev;
46 xfs_find_daxdev_for_inode(
49 struct xfs_inode *ip = XFS_I(inode);
50 struct xfs_mount *mp = ip->i_mount;
52 if (XFS_IS_REALTIME_INODE(ip))
53 return mp->m_rtdev_targp->bt_daxdev;
55 return mp->m_ddev_targp->bt_daxdev;
59 xfs_finish_page_writeback(
64 struct iomap_page *iop = to_iomap_page(bvec->bv_page);
67 SetPageError(bvec->bv_page);
68 mapping_set_error(inode->i_mapping, -EIO);
71 ASSERT(iop || i_blocksize(inode) == PAGE_SIZE);
72 ASSERT(!iop || atomic_read(&iop->write_count) > 0);
74 if (!iop || atomic_dec_and_test(&iop->write_count))
75 end_page_writeback(bvec->bv_page);
79 * We're now finished for good with this ioend structure. Update the page
80 * state, release holds on bios, and finally free up memory. Do not use the
85 struct xfs_ioend *ioend,
88 struct inode *inode = ioend->io_inode;
89 struct bio *bio = &ioend->io_inline_bio;
90 struct bio *last = ioend->io_bio, *next;
91 u64 start = bio->bi_iter.bi_sector;
92 bool quiet = bio_flagged(bio, BIO_QUIET);
94 for (bio = &ioend->io_inline_bio; bio; bio = next) {
96 struct bvec_iter_all iter_all;
99 * For the last bio, bi_private points to the ioend, so we
100 * need to explicitly end the iteration here.
105 next = bio->bi_private;
107 /* walk each page on bio, ending page IO on them */
108 bio_for_each_segment_all(bvec, bio, iter_all)
109 xfs_finish_page_writeback(inode, bvec, error);
113 if (unlikely(error && !quiet)) {
114 xfs_err_ratelimited(XFS_I(inode)->i_mount,
115 "writeback error on sector %llu", start);
120 * Fast and loose check if this write could update the on-disk inode size.
122 static inline bool xfs_ioend_is_append(struct xfs_ioend *ioend)
124 return ioend->io_offset + ioend->io_size >
125 XFS_I(ioend->io_inode)->i_d.di_size;
129 xfs_setfilesize_trans_alloc(
130 struct xfs_ioend *ioend)
132 struct xfs_mount *mp = XFS_I(ioend->io_inode)->i_mount;
133 struct xfs_trans *tp;
136 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_fsyncts, 0, 0, 0, &tp);
140 ioend->io_append_trans = tp;
143 * We may pass freeze protection with a transaction. So tell lockdep
146 __sb_writers_release(ioend->io_inode->i_sb, SB_FREEZE_FS);
148 * We hand off the transaction to the completion thread now, so
149 * clear the flag here.
151 current_restore_flags_nested(&tp->t_pflags, PF_MEMALLOC_NOFS);
156 * Update on-disk file size now that data has been written to disk.
160 struct xfs_inode *ip,
161 struct xfs_trans *tp,
167 xfs_ilock(ip, XFS_ILOCK_EXCL);
168 isize = xfs_new_eof(ip, offset + size);
170 xfs_iunlock(ip, XFS_ILOCK_EXCL);
171 xfs_trans_cancel(tp);
175 trace_xfs_setfilesize(ip, offset, size);
177 ip->i_d.di_size = isize;
178 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
179 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
181 return xfs_trans_commit(tp);
186 struct xfs_inode *ip,
190 struct xfs_mount *mp = ip->i_mount;
191 struct xfs_trans *tp;
194 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_fsyncts, 0, 0, 0, &tp);
198 return __xfs_setfilesize(ip, tp, offset, size);
202 xfs_setfilesize_ioend(
203 struct xfs_ioend *ioend,
206 struct xfs_inode *ip = XFS_I(ioend->io_inode);
207 struct xfs_trans *tp = ioend->io_append_trans;
210 * The transaction may have been allocated in the I/O submission thread,
211 * thus we need to mark ourselves as being in a transaction manually.
212 * Similarly for freeze protection.
214 current_set_flags_nested(&tp->t_pflags, PF_MEMALLOC_NOFS);
215 __sb_writers_acquired(VFS_I(ip)->i_sb, SB_FREEZE_FS);
217 /* we abort the update if there was an IO error */
219 xfs_trans_cancel(tp);
223 return __xfs_setfilesize(ip, tp, ioend->io_offset, ioend->io_size);
227 * IO write completion.
231 struct xfs_ioend *ioend)
233 struct list_head ioend_list;
234 struct xfs_inode *ip = XFS_I(ioend->io_inode);
235 xfs_off_t offset = ioend->io_offset;
236 size_t size = ioend->io_size;
237 unsigned int nofs_flag;
241 * We can allocate memory here while doing writeback on behalf of
242 * memory reclaim. To avoid memory allocation deadlocks set the
243 * task-wide nofs context for the following operations.
245 nofs_flag = memalloc_nofs_save();
248 * Just clean up the in-memory strutures if the fs has been shut down.
250 if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
256 * Clean up any COW blocks on an I/O error.
258 error = blk_status_to_errno(ioend->io_bio->bi_status);
259 if (unlikely(error)) {
260 if (ioend->io_fork == XFS_COW_FORK)
261 xfs_reflink_cancel_cow_range(ip, offset, size, true);
266 * Success: commit the COW or unwritten blocks if needed.
268 if (ioend->io_fork == XFS_COW_FORK)
269 error = xfs_reflink_end_cow(ip, offset, size);
270 else if (ioend->io_state == XFS_EXT_UNWRITTEN)
271 error = xfs_iomap_write_unwritten(ip, offset, size, false);
273 ASSERT(!xfs_ioend_is_append(ioend) || ioend->io_append_trans);
276 if (ioend->io_append_trans)
277 error = xfs_setfilesize_ioend(ioend, error);
278 list_replace_init(&ioend->io_list, &ioend_list);
279 xfs_destroy_ioend(ioend, error);
281 while (!list_empty(&ioend_list)) {
282 ioend = list_first_entry(&ioend_list, struct xfs_ioend,
284 list_del_init(&ioend->io_list);
285 xfs_destroy_ioend(ioend, error);
288 memalloc_nofs_restore(nofs_flag);
292 * We can merge two adjacent ioends if they have the same set of work to do.
296 struct xfs_ioend *ioend,
297 struct xfs_ioend *next)
299 if (ioend->io_bio->bi_status != next->io_bio->bi_status)
301 if ((ioend->io_fork == XFS_COW_FORK) ^ (next->io_fork == XFS_COW_FORK))
303 if ((ioend->io_state == XFS_EXT_UNWRITTEN) ^
304 (next->io_state == XFS_EXT_UNWRITTEN))
306 if (ioend->io_offset + ioend->io_size != next->io_offset)
312 * If the to be merged ioend has a preallocated transaction for file
313 * size updates we need to ensure the ioend it is merged into also
314 * has one. If it already has one we can simply cancel the transaction
315 * as it is guaranteed to be clean.
318 xfs_ioend_merge_append_transactions(
319 struct xfs_ioend *ioend,
320 struct xfs_ioend *next)
322 if (!ioend->io_append_trans) {
323 ioend->io_append_trans = next->io_append_trans;
324 next->io_append_trans = NULL;
326 xfs_setfilesize_ioend(next, -ECANCELED);
330 /* Try to merge adjacent completions. */
333 struct xfs_ioend *ioend,
334 struct list_head *more_ioends)
336 struct xfs_ioend *next_ioend;
338 while (!list_empty(more_ioends)) {
339 next_ioend = list_first_entry(more_ioends, struct xfs_ioend,
341 if (!xfs_ioend_can_merge(ioend, next_ioend))
343 list_move_tail(&next_ioend->io_list, &ioend->io_list);
344 ioend->io_size += next_ioend->io_size;
345 if (next_ioend->io_append_trans)
346 xfs_ioend_merge_append_transactions(ioend, next_ioend);
350 /* list_sort compare function for ioends */
357 struct xfs_ioend *ia;
358 struct xfs_ioend *ib;
360 ia = container_of(a, struct xfs_ioend, io_list);
361 ib = container_of(b, struct xfs_ioend, io_list);
362 if (ia->io_offset < ib->io_offset)
364 else if (ia->io_offset > ib->io_offset)
369 /* Finish all pending io completions. */
372 struct work_struct *work)
374 struct xfs_inode *ip;
375 struct xfs_ioend *ioend;
376 struct list_head completion_list;
379 ip = container_of(work, struct xfs_inode, i_ioend_work);
381 spin_lock_irqsave(&ip->i_ioend_lock, flags);
382 list_replace_init(&ip->i_ioend_list, &completion_list);
383 spin_unlock_irqrestore(&ip->i_ioend_lock, flags);
385 list_sort(NULL, &completion_list, xfs_ioend_compare);
387 while (!list_empty(&completion_list)) {
388 ioend = list_first_entry(&completion_list, struct xfs_ioend,
390 list_del_init(&ioend->io_list);
391 xfs_ioend_try_merge(ioend, &completion_list);
392 xfs_end_ioend(ioend);
400 struct xfs_ioend *ioend = bio->bi_private;
401 struct xfs_inode *ip = XFS_I(ioend->io_inode);
402 struct xfs_mount *mp = ip->i_mount;
405 if (ioend->io_fork == XFS_COW_FORK ||
406 ioend->io_state == XFS_EXT_UNWRITTEN ||
407 ioend->io_append_trans != NULL) {
408 spin_lock_irqsave(&ip->i_ioend_lock, flags);
409 if (list_empty(&ip->i_ioend_list))
410 WARN_ON_ONCE(!queue_work(mp->m_unwritten_workqueue,
412 list_add_tail(&ioend->io_list, &ip->i_ioend_list);
413 spin_unlock_irqrestore(&ip->i_ioend_lock, flags);
415 xfs_destroy_ioend(ioend, blk_status_to_errno(bio->bi_status));
419 * Fast revalidation of the cached writeback mapping. Return true if the current
420 * mapping is valid, false otherwise.
424 struct xfs_writepage_ctx *wpc,
425 struct xfs_inode *ip,
426 xfs_fileoff_t offset_fsb)
428 if (offset_fsb < wpc->imap.br_startoff ||
429 offset_fsb >= wpc->imap.br_startoff + wpc->imap.br_blockcount)
432 * If this is a COW mapping, it is sufficient to check that the mapping
433 * covers the offset. Be careful to check this first because the caller
434 * can revalidate a COW mapping without updating the data seqno.
436 if (wpc->fork == XFS_COW_FORK)
440 * This is not a COW mapping. Check the sequence number of the data fork
441 * because concurrent changes could have invalidated the extent. Check
442 * the COW fork because concurrent changes since the last time we
443 * checked (and found nothing at this offset) could have added
444 * overlapping blocks.
446 if (wpc->data_seq != READ_ONCE(ip->i_df.if_seq))
448 if (xfs_inode_has_cow_data(ip) &&
449 wpc->cow_seq != READ_ONCE(ip->i_cowfp->if_seq))
455 * Pass in a dellalloc extent and convert it to real extents, return the real
456 * extent that maps offset_fsb in wpc->imap.
458 * The current page is held locked so nothing could have removed the block
459 * backing offset_fsb, although it could have moved from the COW to the data
460 * fork by another thread.
464 struct xfs_writepage_ctx *wpc,
465 struct xfs_inode *ip,
466 xfs_fileoff_t offset_fsb)
471 * Attempt to allocate whatever delalloc extent currently backs
472 * offset_fsb and put the result into wpc->imap. Allocate in a loop
473 * because it may take several attempts to allocate real blocks for a
474 * contiguous delalloc extent if free space is sufficiently fragmented.
477 error = xfs_bmapi_convert_delalloc(ip, wpc->fork, offset_fsb,
478 &wpc->imap, wpc->fork == XFS_COW_FORK ?
479 &wpc->cow_seq : &wpc->data_seq);
482 } while (wpc->imap.br_startoff + wpc->imap.br_blockcount <= offset_fsb);
489 struct xfs_writepage_ctx *wpc,
493 struct xfs_inode *ip = XFS_I(inode);
494 struct xfs_mount *mp = ip->i_mount;
495 ssize_t count = i_blocksize(inode);
496 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
497 xfs_fileoff_t end_fsb = XFS_B_TO_FSB(mp, offset + count);
498 xfs_fileoff_t cow_fsb = NULLFILEOFF;
499 struct xfs_bmbt_irec imap;
500 struct xfs_iext_cursor icur;
504 if (XFS_FORCED_SHUTDOWN(mp))
508 * COW fork blocks can overlap data fork blocks even if the blocks
509 * aren't shared. COW I/O always takes precedent, so we must always
510 * check for overlap on reflink inodes unless the mapping is already a
511 * COW one, or the COW fork hasn't changed from the last time we looked
514 * It's safe to check the COW fork if_seq here without the ILOCK because
515 * we've indirectly protected against concurrent updates: writeback has
516 * the page locked, which prevents concurrent invalidations by reflink
517 * and directio and prevents concurrent buffered writes to the same
518 * page. Changes to if_seq always happen under i_lock, which protects
519 * against concurrent updates and provides a memory barrier on the way
520 * out that ensures that we always see the current value.
522 if (xfs_imap_valid(wpc, ip, offset_fsb))
526 * If we don't have a valid map, now it's time to get a new one for this
527 * offset. This will convert delayed allocations (including COW ones)
528 * into real extents. If we return without a valid map, it means we
529 * landed in a hole and we skip the block.
532 xfs_ilock(ip, XFS_ILOCK_SHARED);
533 ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
534 (ip->i_df.if_flags & XFS_IFEXTENTS));
537 * Check if this is offset is covered by a COW extents, and if yes use
538 * it directly instead of looking up anything in the data fork.
540 if (xfs_inode_has_cow_data(ip) &&
541 xfs_iext_lookup_extent(ip, ip->i_cowfp, offset_fsb, &icur, &imap))
542 cow_fsb = imap.br_startoff;
543 if (cow_fsb != NULLFILEOFF && cow_fsb <= offset_fsb) {
544 wpc->cow_seq = READ_ONCE(ip->i_cowfp->if_seq);
545 xfs_iunlock(ip, XFS_ILOCK_SHARED);
547 wpc->fork = XFS_COW_FORK;
548 goto allocate_blocks;
552 * No COW extent overlap. Revalidate now that we may have updated
553 * ->cow_seq. If the data mapping is still valid, we're done.
555 if (xfs_imap_valid(wpc, ip, offset_fsb)) {
556 xfs_iunlock(ip, XFS_ILOCK_SHARED);
561 * If we don't have a valid map, now it's time to get a new one for this
562 * offset. This will convert delayed allocations (including COW ones)
565 if (!xfs_iext_lookup_extent(ip, &ip->i_df, offset_fsb, &icur, &imap))
566 imap.br_startoff = end_fsb; /* fake a hole past EOF */
567 wpc->data_seq = READ_ONCE(ip->i_df.if_seq);
568 xfs_iunlock(ip, XFS_ILOCK_SHARED);
570 wpc->fork = XFS_DATA_FORK;
572 /* landed in a hole or beyond EOF? */
573 if (imap.br_startoff > offset_fsb) {
574 imap.br_blockcount = imap.br_startoff - offset_fsb;
575 imap.br_startoff = offset_fsb;
576 imap.br_startblock = HOLESTARTBLOCK;
577 imap.br_state = XFS_EXT_NORM;
581 * Truncate to the next COW extent if there is one. This is the only
582 * opportunity to do this because we can skip COW fork lookups for the
583 * subsequent blocks in the mapping; however, the requirement to treat
584 * the COW range separately remains.
586 if (cow_fsb != NULLFILEOFF &&
587 cow_fsb < imap.br_startoff + imap.br_blockcount)
588 imap.br_blockcount = cow_fsb - imap.br_startoff;
590 /* got a delalloc extent? */
591 if (imap.br_startblock != HOLESTARTBLOCK &&
592 isnullstartblock(imap.br_startblock))
593 goto allocate_blocks;
596 trace_xfs_map_blocks_found(ip, offset, count, wpc->fork, &imap);
599 error = xfs_convert_blocks(wpc, ip, offset_fsb);
602 * If we failed to find the extent in the COW fork we might have
603 * raced with a COW to data fork conversion or truncate.
604 * Restart the lookup to catch the extent in the data fork for
605 * the former case, but prevent additional retries to avoid
606 * looping forever for the latter case.
608 if (error == -EAGAIN && wpc->fork == XFS_COW_FORK && !retries++)
610 ASSERT(error != -EAGAIN);
615 * Due to merging the return real extent might be larger than the
616 * original delalloc one. Trim the return extent to the next COW
617 * boundary again to force a re-lookup.
619 if (wpc->fork != XFS_COW_FORK && cow_fsb != NULLFILEOFF &&
620 cow_fsb < wpc->imap.br_startoff + wpc->imap.br_blockcount)
621 wpc->imap.br_blockcount = cow_fsb - wpc->imap.br_startoff;
623 ASSERT(wpc->imap.br_startoff <= offset_fsb);
624 ASSERT(wpc->imap.br_startoff + wpc->imap.br_blockcount > offset_fsb);
625 trace_xfs_map_blocks_alloc(ip, offset, count, wpc->fork, &imap);
630 * Submit the bio for an ioend. We are passed an ioend with a bio attached to
631 * it, and we submit that bio. The ioend may be used for multiple bio
632 * submissions, so we only want to allocate an append transaction for the ioend
633 * once. In the case of multiple bio submission, each bio will take an IO
634 * reference to the ioend to ensure that the ioend completion is only done once
635 * all bios have been submitted and the ioend is really done.
637 * If @status is non-zero, it means that we have a situation where some part of
638 * the submission process has failed after we have marked paged for writeback
639 * and unlocked them. In this situation, we need to fail the bio and ioend
640 * rather than submit it to IO. This typically only happens on a filesystem
645 struct writeback_control *wbc,
646 struct xfs_ioend *ioend,
649 unsigned int nofs_flag;
652 * We can allocate memory here while doing writeback on behalf of
653 * memory reclaim. To avoid memory allocation deadlocks set the
654 * task-wide nofs context for the following operations.
656 nofs_flag = memalloc_nofs_save();
658 /* Convert CoW extents to regular */
659 if (!status && ioend->io_fork == XFS_COW_FORK) {
660 status = xfs_reflink_convert_cow(XFS_I(ioend->io_inode),
661 ioend->io_offset, ioend->io_size);
664 /* Reserve log space if we might write beyond the on-disk inode size. */
666 (ioend->io_fork == XFS_COW_FORK ||
667 ioend->io_state != XFS_EXT_UNWRITTEN) &&
668 xfs_ioend_is_append(ioend) &&
669 !ioend->io_append_trans)
670 status = xfs_setfilesize_trans_alloc(ioend);
672 memalloc_nofs_restore(nofs_flag);
674 ioend->io_bio->bi_private = ioend;
675 ioend->io_bio->bi_end_io = xfs_end_bio;
678 * If we are failing the IO now, just mark the ioend with an
679 * error and finish it. This will run IO completion immediately
680 * as there is only one reference to the ioend at this point in
684 ioend->io_bio->bi_status = errno_to_blk_status(status);
685 bio_endio(ioend->io_bio);
689 submit_bio(ioend->io_bio);
693 static struct xfs_ioend *
699 struct block_device *bdev,
701 struct writeback_control *wbc)
703 struct xfs_ioend *ioend;
706 bio = bio_alloc_bioset(GFP_NOFS, BIO_MAX_PAGES, &xfs_ioend_bioset);
707 bio_set_dev(bio, bdev);
708 bio->bi_iter.bi_sector = sector;
709 bio->bi_opf = REQ_OP_WRITE | wbc_to_write_flags(wbc);
710 bio->bi_write_hint = inode->i_write_hint;
711 wbc_init_bio(wbc, bio);
713 ioend = container_of(bio, struct xfs_ioend, io_inline_bio);
714 INIT_LIST_HEAD(&ioend->io_list);
715 ioend->io_fork = fork;
716 ioend->io_state = state;
717 ioend->io_inode = inode;
719 ioend->io_offset = offset;
720 ioend->io_append_trans = NULL;
726 * Allocate a new bio, and chain the old bio to the new one.
728 * Note that we have to do perform the chaining in this unintuitive order
729 * so that the bi_private linkage is set up in the right direction for the
730 * traversal in xfs_destroy_ioend().
738 new = bio_alloc(GFP_NOFS, BIO_MAX_PAGES);
739 bio_copy_dev(new, prev);/* also copies over blkcg information */
740 new->bi_iter.bi_sector = bio_end_sector(prev);
741 new->bi_opf = prev->bi_opf;
742 new->bi_write_hint = prev->bi_write_hint;
744 bio_chain(prev, new);
745 bio_get(prev); /* for xfs_destroy_ioend */
751 * Test to see if we have an existing ioend structure that we could append to
752 * first, otherwise finish off the current ioend and start another.
759 struct iomap_page *iop,
760 struct xfs_writepage_ctx *wpc,
761 struct writeback_control *wbc,
762 struct list_head *iolist)
764 struct xfs_inode *ip = XFS_I(inode);
765 struct xfs_mount *mp = ip->i_mount;
766 struct block_device *bdev = xfs_find_bdev_for_inode(inode);
767 unsigned len = i_blocksize(inode);
768 unsigned poff = offset & (PAGE_SIZE - 1);
769 bool merged, same_page = false;
772 sector = xfs_fsb_to_db(ip, wpc->imap.br_startblock) +
773 ((offset - XFS_FSB_TO_B(mp, wpc->imap.br_startoff)) >> 9);
776 wpc->fork != wpc->ioend->io_fork ||
777 wpc->imap.br_state != wpc->ioend->io_state ||
778 sector != bio_end_sector(wpc->ioend->io_bio) ||
779 offset != wpc->ioend->io_offset + wpc->ioend->io_size) {
781 list_add(&wpc->ioend->io_list, iolist);
782 wpc->ioend = xfs_alloc_ioend(inode, wpc->fork,
783 wpc->imap.br_state, offset, bdev, sector, wbc);
786 merged = __bio_try_merge_page(wpc->ioend->io_bio, page, len, poff,
789 if (iop && !same_page)
790 atomic_inc(&iop->write_count);
793 if (bio_full(wpc->ioend->io_bio, len))
794 wpc->ioend->io_bio = xfs_chain_bio(wpc->ioend->io_bio);
795 bio_add_page(wpc->ioend->io_bio, page, len, poff);
798 wpc->ioend->io_size += len;
799 wbc_account_cgroup_owner(wbc, page, len);
803 xfs_vm_invalidatepage(
808 trace_xfs_invalidatepage(page->mapping->host, page, offset, length);
809 iomap_invalidatepage(page, offset, length);
813 * If the page has delalloc blocks on it, we need to punch them out before we
814 * invalidate the page. If we don't, we leave a stale delalloc mapping on the
815 * inode that can trip up a later direct I/O read operation on the same region.
817 * We prevent this by truncating away the delalloc regions on the page. Because
818 * they are delalloc, we can do this without needing a transaction. Indeed - if
819 * we get ENOSPC errors, we have to be able to do this truncation without a
820 * transaction as there is no space left for block reservation (typically why we
821 * see a ENOSPC in writeback).
824 xfs_aops_discard_page(
827 struct inode *inode = page->mapping->host;
828 struct xfs_inode *ip = XFS_I(inode);
829 struct xfs_mount *mp = ip->i_mount;
830 loff_t offset = page_offset(page);
831 xfs_fileoff_t start_fsb = XFS_B_TO_FSBT(mp, offset);
834 if (XFS_FORCED_SHUTDOWN(mp))
838 "page discard on page "PTR_FMT", inode 0x%llx, offset %llu.",
839 page, ip->i_ino, offset);
841 error = xfs_bmap_punch_delalloc_range(ip, start_fsb,
842 PAGE_SIZE / i_blocksize(inode));
843 if (error && !XFS_FORCED_SHUTDOWN(mp))
844 xfs_alert(mp, "page discard unable to remove delalloc mapping.");
846 xfs_vm_invalidatepage(page, 0, PAGE_SIZE);
850 * We implement an immediate ioend submission policy here to avoid needing to
851 * chain multiple ioends and hence nest mempool allocations which can violate
852 * forward progress guarantees we need to provide. The current ioend we are
853 * adding blocks to is cached on the writepage context, and if the new block
854 * does not append to the cached ioend it will create a new ioend and cache that
857 * If a new ioend is created and cached, the old ioend is returned and queued
858 * locally for submission once the entire page is processed or an error has been
859 * detected. While ioends are submitted immediately after they are completed,
860 * batching optimisations are provided by higher level block plugging.
862 * At the end of a writeback pass, there will be a cached ioend remaining on the
863 * writepage context that the caller will need to submit.
867 struct xfs_writepage_ctx *wpc,
868 struct writeback_control *wbc,
873 LIST_HEAD(submit_list);
874 struct iomap_page *iop = to_iomap_page(page);
875 unsigned len = i_blocksize(inode);
876 struct xfs_ioend *ioend, *next;
877 uint64_t file_offset; /* file offset of page */
878 int error = 0, count = 0, i;
880 ASSERT(iop || i_blocksize(inode) == PAGE_SIZE);
881 ASSERT(!iop || atomic_read(&iop->write_count) == 0);
884 * Walk through the page to find areas to write back. If we run off the
885 * end of the current map or find the current map invalid, grab a new
888 for (i = 0, file_offset = page_offset(page);
889 i < (PAGE_SIZE >> inode->i_blkbits) && file_offset < end_offset;
890 i++, file_offset += len) {
891 if (iop && !test_bit(i, iop->uptodate))
894 error = xfs_map_blocks(wpc, inode, file_offset);
897 if (wpc->imap.br_startblock == HOLESTARTBLOCK)
899 xfs_add_to_ioend(inode, file_offset, page, iop, wpc, wbc,
904 ASSERT(wpc->ioend || list_empty(&submit_list));
905 ASSERT(PageLocked(page));
906 ASSERT(!PageWriteback(page));
909 * On error, we have to fail the ioend here because we may have set
910 * pages under writeback, we have to make sure we run IO completion to
911 * mark the error state of the IO appropriately, so we can't cancel the
912 * ioend directly here. That means we have to mark this page as under
913 * writeback if we included any blocks from it in the ioend chain so
914 * that completion treats it correctly.
916 * If we didn't include the page in the ioend, the on error we can
917 * simply discard and unlock it as there are no other users of the page
918 * now. The caller will still need to trigger submission of outstanding
919 * ioends on the writepage context so they are treated correctly on
922 if (unlikely(error)) {
924 xfs_aops_discard_page(page);
925 ClearPageUptodate(page);
931 * If the page was not fully cleaned, we need to ensure that the
932 * higher layers come back to it correctly. That means we need
933 * to keep the page dirty, and for WB_SYNC_ALL writeback we need
934 * to ensure the PAGECACHE_TAG_TOWRITE index mark is not removed
935 * so another attempt to write this page in this writeback sweep
938 set_page_writeback_keepwrite(page);
940 clear_page_dirty_for_io(page);
941 set_page_writeback(page);
947 * Preserve the original error if there was one, otherwise catch
948 * submission errors here and propagate into subsequent ioend
951 list_for_each_entry_safe(ioend, next, &submit_list, io_list) {
954 list_del_init(&ioend->io_list);
955 error2 = xfs_submit_ioend(wbc, ioend, error);
956 if (error2 && !error)
961 * We can end up here with no error and nothing to write only if we race
962 * with a partial page truncate on a sub-page block sized filesystem.
965 end_page_writeback(page);
967 mapping_set_error(page->mapping, error);
972 * Write out a dirty page.
974 * For delalloc space on the page we need to allocate space and flush it.
975 * For unwritten space on the page we need to start the conversion to
976 * regular allocated space.
981 struct writeback_control *wbc,
984 struct xfs_writepage_ctx *wpc = data;
985 struct inode *inode = page->mapping->host;
990 trace_xfs_writepage(inode, page, 0, 0);
993 * Refuse to write the page out if we are called from reclaim context.
995 * This avoids stack overflows when called from deeply used stacks in
996 * random callers for direct reclaim or memcg reclaim. We explicitly
997 * allow reclaim from kswapd as the stack usage there is relatively low.
999 * This should never happen except in the case of a VM regression so
1002 if (WARN_ON_ONCE((current->flags & (PF_MEMALLOC|PF_KSWAPD)) ==
1007 * Given that we do not allow direct reclaim to call us, we should
1008 * never be called while in a filesystem transaction.
1010 if (WARN_ON_ONCE(current->flags & PF_MEMALLOC_NOFS))
1014 * Is this page beyond the end of the file?
1016 * The page index is less than the end_index, adjust the end_offset
1017 * to the highest offset that this page should represent.
1018 * -----------------------------------------------------
1019 * | file mapping | <EOF> |
1020 * -----------------------------------------------------
1021 * | Page ... | Page N-2 | Page N-1 | Page N | |
1022 * ^--------------------------------^----------|--------
1023 * | desired writeback range | see else |
1024 * ---------------------------------^------------------|
1026 offset = i_size_read(inode);
1027 end_index = offset >> PAGE_SHIFT;
1028 if (page->index < end_index)
1029 end_offset = (xfs_off_t)(page->index + 1) << PAGE_SHIFT;
1032 * Check whether the page to write out is beyond or straddles
1034 * -------------------------------------------------------
1035 * | file mapping | <EOF> |
1036 * -------------------------------------------------------
1037 * | Page ... | Page N-2 | Page N-1 | Page N | Beyond |
1038 * ^--------------------------------^-----------|---------
1040 * ---------------------------------^-----------|--------|
1042 unsigned offset_into_page = offset & (PAGE_SIZE - 1);
1045 * Skip the page if it is fully outside i_size, e.g. due to a
1046 * truncate operation that is in progress. We must redirty the
1047 * page so that reclaim stops reclaiming it. Otherwise
1048 * xfs_vm_releasepage() is called on it and gets confused.
1050 * Note that the end_index is unsigned long, it would overflow
1051 * if the given offset is greater than 16TB on 32-bit system
1052 * and if we do check the page is fully outside i_size or not
1053 * via "if (page->index >= end_index + 1)" as "end_index + 1"
1054 * will be evaluated to 0. Hence this page will be redirtied
1055 * and be written out repeatedly which would result in an
1056 * infinite loop, the user program that perform this operation
1057 * will hang. Instead, we can verify this situation by checking
1058 * if the page to write is totally beyond the i_size or if it's
1059 * offset is just equal to the EOF.
1061 if (page->index > end_index ||
1062 (page->index == end_index && offset_into_page == 0))
1066 * The page straddles i_size. It must be zeroed out on each
1067 * and every writepage invocation because it may be mmapped.
1068 * "A file is mapped in multiples of the page size. For a file
1069 * that is not a multiple of the page size, the remaining
1070 * memory is zeroed when mapped, and writes to that region are
1071 * not written out to the file."
1073 zero_user_segment(page, offset_into_page, PAGE_SIZE);
1075 /* Adjust the end_offset to the end of file */
1076 end_offset = offset;
1079 return xfs_writepage_map(wpc, wbc, inode, page, end_offset);
1082 redirty_page_for_writepage(wbc, page);
1090 struct writeback_control *wbc)
1092 struct xfs_writepage_ctx wpc = { };
1095 ret = xfs_do_writepage(page, wbc, &wpc);
1097 ret = xfs_submit_ioend(wbc, wpc.ioend, ret);
1103 struct address_space *mapping,
1104 struct writeback_control *wbc)
1106 struct xfs_writepage_ctx wpc = { };
1109 xfs_iflags_clear(XFS_I(mapping->host), XFS_ITRUNCATED);
1110 ret = write_cache_pages(mapping, wbc, xfs_do_writepage, &wpc);
1112 ret = xfs_submit_ioend(wbc, wpc.ioend, ret);
1118 struct address_space *mapping,
1119 struct writeback_control *wbc)
1121 xfs_iflags_clear(XFS_I(mapping->host), XFS_ITRUNCATED);
1122 return dax_writeback_mapping_range(mapping,
1123 xfs_find_bdev_for_inode(mapping->host), wbc);
1131 trace_xfs_releasepage(page->mapping->host, page, 0, 0);
1132 return iomap_releasepage(page, gfp_mask);
1137 struct address_space *mapping,
1140 struct xfs_inode *ip = XFS_I(mapping->host);
1142 trace_xfs_vm_bmap(ip);
1145 * The swap code (ab-)uses ->bmap to get a block mapping and then
1146 * bypasses the file system for actual I/O. We really can't allow
1147 * that on reflinks inodes, so we have to skip out here. And yes,
1148 * 0 is the magic code for a bmap error.
1150 * Since we don't pass back blockdev info, we can't return bmap
1151 * information for rt files either.
1153 if (xfs_is_cow_inode(ip) || XFS_IS_REALTIME_INODE(ip))
1155 return iomap_bmap(mapping, block, &xfs_iomap_ops);
1160 struct file *unused,
1163 trace_xfs_vm_readpage(page->mapping->host, 1);
1164 return iomap_readpage(page, &xfs_iomap_ops);
1169 struct file *unused,
1170 struct address_space *mapping,
1171 struct list_head *pages,
1174 trace_xfs_vm_readpages(mapping->host, nr_pages);
1175 return iomap_readpages(mapping, pages, nr_pages, &xfs_iomap_ops);
1179 xfs_iomap_swapfile_activate(
1180 struct swap_info_struct *sis,
1181 struct file *swap_file,
1184 sis->bdev = xfs_find_bdev_for_inode(file_inode(swap_file));
1185 return iomap_swapfile_activate(sis, swap_file, span, &xfs_iomap_ops);
1188 const struct address_space_operations xfs_address_space_operations = {
1189 .readpage = xfs_vm_readpage,
1190 .readpages = xfs_vm_readpages,
1191 .writepage = xfs_vm_writepage,
1192 .writepages = xfs_vm_writepages,
1193 .set_page_dirty = iomap_set_page_dirty,
1194 .releasepage = xfs_vm_releasepage,
1195 .invalidatepage = xfs_vm_invalidatepage,
1196 .bmap = xfs_vm_bmap,
1197 .direct_IO = noop_direct_IO,
1198 .migratepage = iomap_migrate_page,
1199 .is_partially_uptodate = iomap_is_partially_uptodate,
1200 .error_remove_page = generic_error_remove_page,
1201 .swap_activate = xfs_iomap_swapfile_activate,
1204 const struct address_space_operations xfs_dax_aops = {
1205 .writepages = xfs_dax_writepages,
1206 .direct_IO = noop_direct_IO,
1207 .set_page_dirty = noop_set_page_dirty,
1208 .invalidatepage = noop_invalidatepage,
1209 .swap_activate = xfs_iomap_swapfile_activate,