blob: 796fc4b48585e545b73ed1a04e7e295a0eef9045 [file] [log] [blame]
Dave Chinner0b61f8a2018-06-05 19:42:14 -07001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Nathan Scott7b718762005-11-02 14:58:39 +11003 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
Christoph Hellwig98c1a7c02018-07-11 22:26:06 -07004 * Copyright (c) 2016-2018 Christoph Hellwig.
Nathan Scott7b718762005-11-02 14:58:39 +11005 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include "xfs.h"
Dave Chinner70a98832013-10-23 10:36:05 +11008#include "xfs_shared.h"
Dave Chinner239880e2013-10-23 10:50:10 +11009#include "xfs_format.h"
10#include "xfs_log_format.h"
11#include "xfs_trans_resv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include "xfs_inode.h"
Dave Chinner239880e2013-10-23 10:50:10 +110014#include "xfs_trans.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include "xfs_iomap.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000016#include "xfs_trace.h"
Dave Chinner3ed3a432010-03-05 02:00:42 +000017#include "xfs_bmap.h"
Dave Chinner68988112013-08-12 20:49:42 +100018#include "xfs_bmap_util.h"
Darrick J. Wongef473662016-10-03 09:11:34 -070019#include "xfs_reflink.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Dave Chinnerfbcc0252016-02-15 17:21:19 +110021struct xfs_writepage_ctx {
Christoph Hellwig598ecfb2019-10-17 13:12:15 -070022 struct iomap_writepage_ctx ctx;
Brian Fosterd9252d52019-02-01 09:14:23 -080023 unsigned int data_seq;
Christoph Hellwige666aa32018-07-17 16:51:52 -070024 unsigned int cow_seq;
Dave Chinnerfbcc0252016-02-15 17:21:19 +110025};
26
Christoph Hellwig598ecfb2019-10-17 13:12:15 -070027static inline struct xfs_writepage_ctx *
28XFS_WPC(struct iomap_writepage_ctx *ctx)
29{
30 return container_of(ctx, struct xfs_writepage_ctx, ctx);
31}
32
Linus Torvalds1da177e2005-04-16 15:20:36 -070033/*
Christoph Hellwigfc0063c2011-08-23 08:28:11 +000034 * Fast and loose check if this write could update the on-disk inode size.
35 */
Christoph Hellwig598ecfb2019-10-17 13:12:15 -070036static inline bool xfs_ioend_is_append(struct iomap_ioend *ioend)
Christoph Hellwigfc0063c2011-08-23 08:28:11 +000037{
38 return ioend->io_offset + ioend->io_size >
Christoph Hellwig13d2c102021-03-29 11:11:40 -070039 XFS_I(ioend->io_inode)->i_disk_size;
Christoph Hellwigfc0063c2011-08-23 08:28:11 +000040}
41
42/*
Christoph Hellwig2813d682011-12-18 20:00:12 +000043 * Update on-disk file size now that data has been written to disk.
Lachlan McIlroyba87ea62007-05-08 13:49:46 +100044 */
Christoph Hellwig281627d2012-03-13 08:41:05 +000045STATIC int
Christoph Hellwige3728432016-09-19 11:26:41 +100046__xfs_setfilesize(
Christoph Hellwig2ba66232015-02-02 10:02:09 +110047 struct xfs_inode *ip,
48 struct xfs_trans *tp,
49 xfs_off_t offset,
50 size_t size)
Lachlan McIlroyba87ea62007-05-08 13:49:46 +100051{
Lachlan McIlroyba87ea62007-05-08 13:49:46 +100052 xfs_fsize_t isize;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +100053
Christoph Hellwigaa6bf012012-02-29 09:53:48 +000054 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig2ba66232015-02-02 10:02:09 +110055 isize = xfs_new_eof(ip, offset + size);
Christoph Hellwig281627d2012-03-13 08:41:05 +000056 if (!isize) {
57 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig4906e212015-06-04 13:47:56 +100058 xfs_trans_cancel(tp);
Christoph Hellwig281627d2012-03-13 08:41:05 +000059 return 0;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +100060 }
61
Christoph Hellwig2ba66232015-02-02 10:02:09 +110062 trace_xfs_setfilesize(ip, offset, size);
Christoph Hellwig281627d2012-03-13 08:41:05 +000063
Christoph Hellwig13d2c102021-03-29 11:11:40 -070064 ip->i_disk_size = isize;
Christoph Hellwig281627d2012-03-13 08:41:05 +000065 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
66 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
67
Christoph Hellwig70393312015-06-04 13:48:08 +100068 return xfs_trans_commit(tp);
Christoph Hellwig0829c362005-09-02 16:58:49 +100069}
70
Christoph Hellwige3728432016-09-19 11:26:41 +100071int
72xfs_setfilesize(
73 struct xfs_inode *ip,
74 xfs_off_t offset,
75 size_t size)
76{
77 struct xfs_mount *mp = ip->i_mount;
78 struct xfs_trans *tp;
79 int error;
80
81 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_fsyncts, 0, 0, 0, &tp);
82 if (error)
83 return error;
84
85 return __xfs_setfilesize(ip, tp, offset, size);
86}
87
Christoph Hellwig0829c362005-09-02 16:58:49 +100088/*
Dave Chinner77d7a0c2010-02-17 05:36:29 +000089 * IO write completion.
90 */
91STATIC void
Darrick J. Wongcb357bf2019-04-15 13:13:20 -070092xfs_end_ioend(
Christoph Hellwig598ecfb2019-10-17 13:12:15 -070093 struct iomap_ioend *ioend)
Dave Chinner77d7a0c2010-02-17 05:36:29 +000094{
Christoph Hellwig0e51a8e2016-04-06 08:34:30 +100095 struct xfs_inode *ip = XFS_I(ioend->io_inode);
Christoph Hellwig787eb482017-03-02 15:02:51 -080096 xfs_off_t offset = ioend->io_offset;
97 size_t size = ioend->io_size;
Christoph Hellwig73d30d42019-06-28 19:31:38 -070098 unsigned int nofs_flag;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +020099 int error;
Dave Chinner77d7a0c2010-02-17 05:36:29 +0000100
Brian Fosteraf055e32016-02-08 15:00:02 +1100101 /*
Christoph Hellwig73d30d42019-06-28 19:31:38 -0700102 * We can allocate memory here while doing writeback on behalf of
103 * memory reclaim. To avoid memory allocation deadlocks set the
104 * task-wide nofs context for the following operations.
105 */
106 nofs_flag = memalloc_nofs_save();
107
108 /*
Bhaskar Chowdhuryf9dd7ba2021-03-23 16:59:30 -0700109 * Just clean up the in-memory structures if the fs has been shut down.
Brian Fosteraf055e32016-02-08 15:00:02 +1100110 */
Christoph Hellwig787eb482017-03-02 15:02:51 -0800111 if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
Christoph Hellwig0e51a8e2016-04-06 08:34:30 +1000112 error = -EIO;
Christoph Hellwig787eb482017-03-02 15:02:51 -0800113 goto done;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700114 }
115
116 /*
Christoph Hellwig787eb482017-03-02 15:02:51 -0800117 * Clean up any COW blocks on an I/O error.
Dave Chinner77d7a0c2010-02-17 05:36:29 +0000118 */
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200119 error = blk_status_to_errno(ioend->io_bio->bi_status);
Christoph Hellwig787eb482017-03-02 15:02:51 -0800120 if (unlikely(error)) {
Christoph Hellwig760fea82019-10-17 13:12:10 -0700121 if (ioend->io_flags & IOMAP_F_SHARED)
Christoph Hellwig787eb482017-03-02 15:02:51 -0800122 xfs_reflink_cancel_cow_range(ip, offset, size, true);
Christoph Hellwig787eb482017-03-02 15:02:51 -0800123 goto done;
124 }
125
126 /*
Christoph Hellwigbe225fe2019-02-15 08:02:46 -0800127 * Success: commit the COW or unwritten blocks if needed.
Christoph Hellwig787eb482017-03-02 15:02:51 -0800128 */
Christoph Hellwig760fea82019-10-17 13:12:10 -0700129 if (ioend->io_flags & IOMAP_F_SHARED)
Christoph Hellwig787eb482017-03-02 15:02:51 -0800130 error = xfs_reflink_end_cow(ip, offset, size);
Christoph Hellwig4e087a32019-10-17 13:12:06 -0700131 else if (ioend->io_type == IOMAP_UNWRITTEN)
Eryu Guanee70daa2017-09-21 11:26:18 -0700132 error = xfs_iomap_write_unwritten(ip, offset, size, false);
Dave Chinner77d7a0c2010-02-17 05:36:29 +0000133
Brian Foster7cd30992021-04-09 10:27:43 -0700134 if (!error && xfs_ioend_is_append(ioend))
135 error = xfs_setfilesize(ip, ioend->io_offset, ioend->io_size);
Christoph Hellwig04f658e2011-08-24 05:59:25 +0000136done:
Christoph Hellwig598ecfb2019-10-17 13:12:15 -0700137 iomap_finish_ioends(ioend, error);
Christoph Hellwig73d30d42019-06-28 19:31:38 -0700138 memalloc_nofs_restore(nofs_flag);
Darrick J. Wong3994fc42019-04-15 13:13:21 -0700139}
140
Darrick J. Wongcb357bf2019-04-15 13:13:20 -0700141/* Finish all pending io completions. */
142void
143xfs_end_io(
144 struct work_struct *work)
145{
Christoph Hellwig433dad92019-10-17 13:12:07 -0700146 struct xfs_inode *ip =
147 container_of(work, struct xfs_inode, i_ioend_work);
Christoph Hellwig598ecfb2019-10-17 13:12:15 -0700148 struct iomap_ioend *ioend;
Christoph Hellwig433dad92019-10-17 13:12:07 -0700149 struct list_head tmp;
Darrick J. Wongcb357bf2019-04-15 13:13:20 -0700150 unsigned long flags;
151
Darrick J. Wongcb357bf2019-04-15 13:13:20 -0700152 spin_lock_irqsave(&ip->i_ioend_lock, flags);
Christoph Hellwig433dad92019-10-17 13:12:07 -0700153 list_replace_init(&ip->i_ioend_list, &tmp);
Darrick J. Wongcb357bf2019-04-15 13:13:20 -0700154 spin_unlock_irqrestore(&ip->i_ioend_lock, flags);
155
Christoph Hellwig598ecfb2019-10-17 13:12:15 -0700156 iomap_sort_ioends(&tmp);
157 while ((ioend = list_first_entry_or_null(&tmp, struct iomap_ioend,
Christoph Hellwig433dad92019-10-17 13:12:07 -0700158 io_list))) {
Darrick J. Wongcb357bf2019-04-15 13:13:20 -0700159 list_del_init(&ioend->io_list);
Brian Foster044c6442021-04-09 10:27:55 -0700160 iomap_ioend_try_merge(ioend, &tmp, NULL);
Darrick J. Wongcb357bf2019-04-15 13:13:20 -0700161 xfs_end_ioend(ioend);
162 }
163}
164
Christoph Hellwig0e51a8e2016-04-06 08:34:30 +1000165STATIC void
166xfs_end_bio(
167 struct bio *bio)
Christoph Hellwig0829c362005-09-02 16:58:49 +1000168{
Christoph Hellwig598ecfb2019-10-17 13:12:15 -0700169 struct iomap_ioend *ioend = bio->bi_private;
Darrick J. Wongcb357bf2019-04-15 13:13:20 -0700170 struct xfs_inode *ip = XFS_I(ioend->io_inode);
Darrick J. Wongcb357bf2019-04-15 13:13:20 -0700171 unsigned long flags;
Christoph Hellwig0829c362005-09-02 16:58:49 +1000172
Christoph Hellwig598ecfb2019-10-17 13:12:15 -0700173 spin_lock_irqsave(&ip->i_ioend_lock, flags);
174 if (list_empty(&ip->i_ioend_list))
175 WARN_ON_ONCE(!queue_work(ip->i_mount->m_unwritten_workqueue,
176 &ip->i_ioend_work));
177 list_add_tail(&ioend->io_list, &ip->i_ioend_list);
178 spin_unlock_irqrestore(&ip->i_ioend_lock, flags);
Christoph Hellwig0829c362005-09-02 16:58:49 +1000179}
180
Brian Fosterd9252d52019-02-01 09:14:23 -0800181/*
182 * Fast revalidation of the cached writeback mapping. Return true if the current
183 * mapping is valid, false otherwise.
184 */
185static bool
186xfs_imap_valid(
Christoph Hellwig598ecfb2019-10-17 13:12:15 -0700187 struct iomap_writepage_ctx *wpc,
Brian Fosterd9252d52019-02-01 09:14:23 -0800188 struct xfs_inode *ip,
Christoph Hellwig4e087a32019-10-17 13:12:06 -0700189 loff_t offset)
Brian Fosterd9252d52019-02-01 09:14:23 -0800190{
Christoph Hellwig4e087a32019-10-17 13:12:06 -0700191 if (offset < wpc->iomap.offset ||
192 offset >= wpc->iomap.offset + wpc->iomap.length)
Brian Fosterd9252d52019-02-01 09:14:23 -0800193 return false;
194 /*
195 * If this is a COW mapping, it is sufficient to check that the mapping
196 * covers the offset. Be careful to check this first because the caller
197 * can revalidate a COW mapping without updating the data seqno.
198 */
Christoph Hellwig760fea82019-10-17 13:12:10 -0700199 if (wpc->iomap.flags & IOMAP_F_SHARED)
Brian Fosterd9252d52019-02-01 09:14:23 -0800200 return true;
201
202 /*
203 * This is not a COW mapping. Check the sequence number of the data fork
204 * because concurrent changes could have invalidated the extent. Check
205 * the COW fork because concurrent changes since the last time we
206 * checked (and found nothing at this offset) could have added
207 * overlapping blocks.
208 */
Christoph Hellwig598ecfb2019-10-17 13:12:15 -0700209 if (XFS_WPC(wpc)->data_seq != READ_ONCE(ip->i_df.if_seq))
Brian Fosterd9252d52019-02-01 09:14:23 -0800210 return false;
211 if (xfs_inode_has_cow_data(ip) &&
Christoph Hellwig598ecfb2019-10-17 13:12:15 -0700212 XFS_WPC(wpc)->cow_seq != READ_ONCE(ip->i_cowfp->if_seq))
Brian Fosterd9252d52019-02-01 09:14:23 -0800213 return false;
214 return true;
215}
216
Christoph Hellwig4ad765e2019-02-15 08:02:49 -0800217/*
218 * Pass in a dellalloc extent and convert it to real extents, return the real
Christoph Hellwig4e087a32019-10-17 13:12:06 -0700219 * extent that maps offset_fsb in wpc->iomap.
Christoph Hellwig4ad765e2019-02-15 08:02:49 -0800220 *
221 * The current page is held locked so nothing could have removed the block
Christoph Hellwig7588cbe2019-02-15 08:02:50 -0800222 * backing offset_fsb, although it could have moved from the COW to the data
223 * fork by another thread.
Christoph Hellwig4ad765e2019-02-15 08:02:49 -0800224 */
225static int
226xfs_convert_blocks(
Christoph Hellwig598ecfb2019-10-17 13:12:15 -0700227 struct iomap_writepage_ctx *wpc,
Christoph Hellwig4ad765e2019-02-15 08:02:49 -0800228 struct xfs_inode *ip,
Christoph Hellwig760fea82019-10-17 13:12:10 -0700229 int whichfork,
Christoph Hellwig4e087a32019-10-17 13:12:06 -0700230 loff_t offset)
Christoph Hellwig4ad765e2019-02-15 08:02:49 -0800231{
232 int error;
Christoph Hellwig598ecfb2019-10-17 13:12:15 -0700233 unsigned *seq;
234
235 if (whichfork == XFS_COW_FORK)
236 seq = &XFS_WPC(wpc)->cow_seq;
237 else
238 seq = &XFS_WPC(wpc)->data_seq;
Christoph Hellwig4ad765e2019-02-15 08:02:49 -0800239
240 /*
Christoph Hellwig4e087a32019-10-17 13:12:06 -0700241 * Attempt to allocate whatever delalloc extent currently backs offset
242 * and put the result into wpc->iomap. Allocate in a loop because it
243 * may take several attempts to allocate real blocks for a contiguous
244 * delalloc extent if free space is sufficiently fragmented.
Christoph Hellwig4ad765e2019-02-15 08:02:49 -0800245 */
246 do {
Christoph Hellwig760fea82019-10-17 13:12:10 -0700247 error = xfs_bmapi_convert_delalloc(ip, whichfork, offset,
Christoph Hellwig598ecfb2019-10-17 13:12:15 -0700248 &wpc->iomap, seq);
Christoph Hellwig4ad765e2019-02-15 08:02:49 -0800249 if (error)
250 return error;
Christoph Hellwig4e087a32019-10-17 13:12:06 -0700251 } while (wpc->iomap.offset + wpc->iomap.length <= offset);
Christoph Hellwig4ad765e2019-02-15 08:02:49 -0800252
253 return 0;
254}
255
Christoph Hellwig598ecfb2019-10-17 13:12:15 -0700256static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257xfs_map_blocks(
Christoph Hellwig598ecfb2019-10-17 13:12:15 -0700258 struct iomap_writepage_ctx *wpc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 struct inode *inode,
Christoph Hellwig5c665e52018-07-11 22:25:59 -0700260 loff_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
Christoph Hellwiga206c812010-12-10 08:42:20 +0000262 struct xfs_inode *ip = XFS_I(inode);
263 struct xfs_mount *mp = ip->i_mount;
Fabian Frederick93407472017-02-27 14:28:32 -0800264 ssize_t count = i_blocksize(inode);
Christoph Hellwigb4e29032019-02-15 08:02:47 -0800265 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
266 xfs_fileoff_t end_fsb = XFS_B_TO_FSB(mp, offset + count);
Darrick J. Wongc2f09212020-11-02 17:14:06 -0800267 xfs_fileoff_t cow_fsb;
268 int whichfork;
Christoph Hellwig5c665e52018-07-11 22:25:59 -0700269 struct xfs_bmbt_irec imap;
Christoph Hellwig060d4ea2018-07-11 22:26:01 -0700270 struct xfs_iext_cursor icur;
Christoph Hellwig7588cbe2019-02-15 08:02:50 -0800271 int retries = 0;
Christoph Hellwiga206c812010-12-10 08:42:20 +0000272 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Brian Fosterd9252d52019-02-01 09:14:23 -0800274 if (XFS_FORCED_SHUTDOWN(mp))
275 return -EIO;
276
Christoph Hellwig889c65b2018-07-11 22:26:02 -0700277 /*
Christoph Hellwig889c65b2018-07-11 22:26:02 -0700278 * COW fork blocks can overlap data fork blocks even if the blocks
279 * aren't shared. COW I/O always takes precedent, so we must always
280 * check for overlap on reflink inodes unless the mapping is already a
Christoph Hellwige666aa32018-07-17 16:51:52 -0700281 * COW one, or the COW fork hasn't changed from the last time we looked
282 * at it.
283 *
284 * It's safe to check the COW fork if_seq here without the ILOCK because
285 * we've indirectly protected against concurrent updates: writeback has
286 * the page locked, which prevents concurrent invalidations by reflink
287 * and directio and prevents concurrent buffered writes to the same
288 * page. Changes to if_seq always happen under i_lock, which protects
289 * against concurrent updates and provides a memory barrier on the way
290 * out that ensures that we always see the current value.
Christoph Hellwig889c65b2018-07-11 22:26:02 -0700291 */
Christoph Hellwig4e087a32019-10-17 13:12:06 -0700292 if (xfs_imap_valid(wpc, ip, offset))
Christoph Hellwig889c65b2018-07-11 22:26:02 -0700293 return 0;
294
Christoph Hellwig889c65b2018-07-11 22:26:02 -0700295 /*
296 * If we don't have a valid map, now it's time to get a new one for this
297 * offset. This will convert delayed allocations (including COW ones)
298 * into real extents. If we return without a valid map, it means we
299 * landed in a hole and we skip the block.
300 */
Christoph Hellwig7588cbe2019-02-15 08:02:50 -0800301retry:
Darrick J. Wongc2f09212020-11-02 17:14:06 -0800302 cow_fsb = NULLFILEOFF;
303 whichfork = XFS_DATA_FORK;
Dave Chinner988ef922016-02-15 17:20:50 +1100304 xfs_ilock(ip, XFS_ILOCK_SHARED);
Christoph Hellwigf7e67b22020-05-18 10:28:05 -0700305 ASSERT(ip->i_df.if_format != XFS_DINODE_FMT_BTREE ||
Christoph Hellwig8ff29572010-12-10 08:42:21 +0000306 (ip->i_df.if_flags & XFS_IFEXTENTS));
Christoph Hellwig060d4ea2018-07-11 22:26:01 -0700307
308 /*
309 * Check if this is offset is covered by a COW extents, and if yes use
310 * it directly instead of looking up anything in the data fork.
311 */
Christoph Hellwig51d62692018-07-17 16:51:51 -0700312 if (xfs_inode_has_cow_data(ip) &&
Christoph Hellwige666aa32018-07-17 16:51:52 -0700313 xfs_iext_lookup_extent(ip, ip->i_cowfp, offset_fsb, &icur, &imap))
314 cow_fsb = imap.br_startoff;
315 if (cow_fsb != NULLFILEOFF && cow_fsb <= offset_fsb) {
Christoph Hellwig598ecfb2019-10-17 13:12:15 -0700316 XFS_WPC(wpc)->cow_seq = READ_ONCE(ip->i_cowfp->if_seq);
Christoph Hellwig5c665e52018-07-11 22:25:59 -0700317 xfs_iunlock(ip, XFS_ILOCK_SHARED);
Christoph Hellwigbe225fe2019-02-15 08:02:46 -0800318
Christoph Hellwig760fea82019-10-17 13:12:10 -0700319 whichfork = XFS_COW_FORK;
Christoph Hellwig5c665e52018-07-11 22:25:59 -0700320 goto allocate_blocks;
321 }
322
323 /*
Brian Fosterd9252d52019-02-01 09:14:23 -0800324 * No COW extent overlap. Revalidate now that we may have updated
325 * ->cow_seq. If the data mapping is still valid, we're done.
Christoph Hellwig5c665e52018-07-11 22:25:59 -0700326 */
Christoph Hellwig4e087a32019-10-17 13:12:06 -0700327 if (xfs_imap_valid(wpc, ip, offset)) {
Christoph Hellwig5c665e52018-07-11 22:25:59 -0700328 xfs_iunlock(ip, XFS_ILOCK_SHARED);
329 return 0;
330 }
331
332 /*
333 * If we don't have a valid map, now it's time to get a new one for this
334 * offset. This will convert delayed allocations (including COW ones)
335 * into real extents.
336 */
Christoph Hellwig33457462018-07-11 22:26:02 -0700337 if (!xfs_iext_lookup_extent(ip, &ip->i_df, offset_fsb, &icur, &imap))
338 imap.br_startoff = end_fsb; /* fake a hole past EOF */
Christoph Hellwig598ecfb2019-10-17 13:12:15 -0700339 XFS_WPC(wpc)->data_seq = READ_ONCE(ip->i_df.if_seq);
Christoph Hellwig8ff29572010-12-10 08:42:21 +0000340 xfs_iunlock(ip, XFS_ILOCK_SHARED);
Christoph Hellwiga206c812010-12-10 08:42:20 +0000341
Christoph Hellwig12df89f2019-02-18 09:38:47 -0800342 /* landed in a hole or beyond EOF? */
Christoph Hellwig33457462018-07-11 22:26:02 -0700343 if (imap.br_startoff > offset_fsb) {
Christoph Hellwig33457462018-07-11 22:26:02 -0700344 imap.br_blockcount = imap.br_startoff - offset_fsb;
Christoph Hellwig5c665e52018-07-11 22:25:59 -0700345 imap.br_startoff = offset_fsb;
Christoph Hellwig5c665e52018-07-11 22:25:59 -0700346 imap.br_startblock = HOLESTARTBLOCK;
Christoph Hellwigbe225fe2019-02-15 08:02:46 -0800347 imap.br_state = XFS_EXT_NORM;
Christoph Hellwiga206c812010-12-10 08:42:20 +0000348 }
349
Christoph Hellwig12df89f2019-02-18 09:38:47 -0800350 /*
351 * Truncate to the next COW extent if there is one. This is the only
352 * opportunity to do this because we can skip COW fork lookups for the
353 * subsequent blocks in the mapping; however, the requirement to treat
354 * the COW range separately remains.
355 */
356 if (cow_fsb != NULLFILEOFF &&
357 cow_fsb < imap.br_startoff + imap.br_blockcount)
358 imap.br_blockcount = cow_fsb - imap.br_startoff;
359
360 /* got a delalloc extent? */
361 if (imap.br_startblock != HOLESTARTBLOCK &&
362 isnullstartblock(imap.br_startblock))
363 goto allocate_blocks;
364
Christoph Hellwig4e087a32019-10-17 13:12:06 -0700365 xfs_bmbt_to_iomap(ip, &wpc->iomap, &imap, 0);
Christoph Hellwig760fea82019-10-17 13:12:10 -0700366 trace_xfs_map_blocks_found(ip, offset, count, whichfork, &imap);
Christoph Hellwig5c665e52018-07-11 22:25:59 -0700367 return 0;
368allocate_blocks:
Christoph Hellwig760fea82019-10-17 13:12:10 -0700369 error = xfs_convert_blocks(wpc, ip, whichfork, offset);
Christoph Hellwig7588cbe2019-02-15 08:02:50 -0800370 if (error) {
371 /*
372 * If we failed to find the extent in the COW fork we might have
373 * raced with a COW to data fork conversion or truncate.
374 * Restart the lookup to catch the extent in the data fork for
375 * the former case, but prevent additional retries to avoid
376 * looping forever for the latter case.
377 */
Christoph Hellwig760fea82019-10-17 13:12:10 -0700378 if (error == -EAGAIN && whichfork == XFS_COW_FORK && !retries++)
Christoph Hellwig7588cbe2019-02-15 08:02:50 -0800379 goto retry;
380 ASSERT(error != -EAGAIN);
Christoph Hellwig5c665e52018-07-11 22:25:59 -0700381 return error;
Christoph Hellwig7588cbe2019-02-15 08:02:50 -0800382 }
Christoph Hellwig4ad765e2019-02-15 08:02:49 -0800383
384 /*
385 * Due to merging the return real extent might be larger than the
386 * original delalloc one. Trim the return extent to the next COW
387 * boundary again to force a re-lookup.
388 */
Christoph Hellwig760fea82019-10-17 13:12:10 -0700389 if (whichfork != XFS_COW_FORK && cow_fsb != NULLFILEOFF) {
Christoph Hellwig4e087a32019-10-17 13:12:06 -0700390 loff_t cow_offset = XFS_FSB_TO_B(mp, cow_fsb);
Christoph Hellwig4ad765e2019-02-15 08:02:49 -0800391
Christoph Hellwig4e087a32019-10-17 13:12:06 -0700392 if (cow_offset < wpc->iomap.offset + wpc->iomap.length)
393 wpc->iomap.length = cow_offset - wpc->iomap.offset;
394 }
395
396 ASSERT(wpc->iomap.offset <= offset);
397 ASSERT(wpc->iomap.offset + wpc->iomap.length > offset);
Christoph Hellwig760fea82019-10-17 13:12:10 -0700398 trace_xfs_map_blocks_alloc(ip, offset, count, whichfork, &imap);
Christoph Hellwig8ff29572010-12-10 08:42:21 +0000399 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400}
401
Christoph Hellwig598ecfb2019-10-17 13:12:15 -0700402static int
403xfs_prepare_ioend(
404 struct iomap_ioend *ioend,
Dave Chinnere10de372016-02-15 17:23:12 +1100405 int status)
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100406{
Christoph Hellwig73d30d42019-06-28 19:31:38 -0700407 unsigned int nofs_flag;
408
409 /*
410 * We can allocate memory here while doing writeback on behalf of
411 * memory reclaim. To avoid memory allocation deadlocks set the
412 * task-wide nofs context for the following operations.
413 */
414 nofs_flag = memalloc_nofs_save();
415
Darrick J. Wong5eda4302017-02-02 15:14:02 -0800416 /* Convert CoW extents to regular */
Christoph Hellwig760fea82019-10-17 13:12:10 -0700417 if (!status && (ioend->io_flags & IOMAP_F_SHARED)) {
Darrick J. Wong5eda4302017-02-02 15:14:02 -0800418 status = xfs_reflink_convert_cow(XFS_I(ioend->io_inode),
419 ioend->io_offset, ioend->io_size);
420 }
421
Christoph Hellwig73d30d42019-06-28 19:31:38 -0700422 memalloc_nofs_restore(nofs_flag);
423
Brian Foster7adb8f12021-04-09 10:27:55 -0700424 /* send ioends that might require a transaction to the completion wq */
425 if (xfs_ioend_is_append(ioend) || ioend->io_type == IOMAP_UNWRITTEN ||
426 (ioend->io_flags & IOMAP_F_SHARED))
Christoph Hellwig598ecfb2019-10-17 13:12:15 -0700427 ioend->io_bio->bi_end_io = xfs_end_bio;
428 return status;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100429}
430
Dave Chinner3ed3a432010-03-05 02:00:42 +0000431/*
Christoph Hellwig82cb1412018-07-11 22:26:05 -0700432 * If the page has delalloc blocks on it, we need to punch them out before we
433 * invalidate the page. If we don't, we leave a stale delalloc mapping on the
434 * inode that can trip up a later direct I/O read operation on the same region.
Dave Chinner3ed3a432010-03-05 02:00:42 +0000435 *
Christoph Hellwig82cb1412018-07-11 22:26:05 -0700436 * We prevent this by truncating away the delalloc regions on the page. Because
437 * they are delalloc, we can do this without needing a transaction. Indeed - if
438 * we get ENOSPC errors, we have to be able to do this truncation without a
439 * transaction as there is no space left for block reservation (typically why we
440 * see a ENOSPC in writeback).
Dave Chinner3ed3a432010-03-05 02:00:42 +0000441 */
Christoph Hellwig598ecfb2019-10-17 13:12:15 -0700442static void
443xfs_discard_page(
Brian Foster763e4cd2020-10-29 14:30:48 -0700444 struct page *page,
445 loff_t fileoff)
Dave Chinner3ed3a432010-03-05 02:00:42 +0000446{
447 struct inode *inode = page->mapping->host;
448 struct xfs_inode *ip = XFS_I(inode);
Christoph Hellwig03625722018-07-11 22:25:57 -0700449 struct xfs_mount *mp = ip->i_mount;
Brian Foster763e4cd2020-10-29 14:30:48 -0700450 unsigned int pageoff = offset_in_page(fileoff);
451 xfs_fileoff_t start_fsb = XFS_B_TO_FSBT(mp, fileoff);
452 xfs_fileoff_t pageoff_fsb = XFS_B_TO_FSBT(mp, pageoff);
Christoph Hellwig03625722018-07-11 22:25:57 -0700453 int error;
Dave Chinner3ed3a432010-03-05 02:00:42 +0000454
Christoph Hellwig03625722018-07-11 22:25:57 -0700455 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner3ed3a432010-03-05 02:00:42 +0000456 goto out_invalidate;
457
Christoph Hellwig4ab45e22020-02-21 07:34:48 -0800458 xfs_alert_ratelimited(mp,
Darrick J. Wongc9690042018-01-09 12:02:55 -0800459 "page discard on page "PTR_FMT", inode 0x%llx, offset %llu.",
Brian Foster763e4cd2020-10-29 14:30:48 -0700460 page, ip->i_ino, fileoff);
Dave Chinner3ed3a432010-03-05 02:00:42 +0000461
Christoph Hellwig03625722018-07-11 22:25:57 -0700462 error = xfs_bmap_punch_delalloc_range(ip, start_fsb,
Brian Foster763e4cd2020-10-29 14:30:48 -0700463 i_blocks_per_page(inode, page) - pageoff_fsb);
Christoph Hellwig03625722018-07-11 22:25:57 -0700464 if (error && !XFS_FORCED_SHUTDOWN(mp))
465 xfs_alert(mp, "page discard unable to remove delalloc mapping.");
Dave Chinner3ed3a432010-03-05 02:00:42 +0000466out_invalidate:
Brian Foster763e4cd2020-10-29 14:30:48 -0700467 iomap_invalidatepage(page, pageoff, PAGE_SIZE - pageoff);
Dave Chinner3ed3a432010-03-05 02:00:42 +0000468}
469
Christoph Hellwig598ecfb2019-10-17 13:12:15 -0700470static const struct iomap_writeback_ops xfs_writeback_ops = {
471 .map_blocks = xfs_map_blocks,
472 .prepare_ioend = xfs_prepare_ioend,
473 .discard_page = xfs_discard_page,
474};
Nathan Scottf51623b2006-03-14 13:26:27 +1100475
Nathan Scott7d4fb402006-06-09 15:27:16 +1000476STATIC int
Dave Chinnerfbcc0252016-02-15 17:21:19 +1100477xfs_vm_writepage(
478 struct page *page,
479 struct writeback_control *wbc)
480{
Christoph Hellwigbe225fe2019-02-15 08:02:46 -0800481 struct xfs_writepage_ctx wpc = { };
Dave Chinnerfbcc0252016-02-15 17:21:19 +1100482
Dave Chinner756b1c32021-02-23 10:26:06 -0800483 if (WARN_ON_ONCE(current->journal_info)) {
484 redirty_page_for_writepage(wbc, page);
485 unlock_page(page);
486 return 0;
487 }
488
Christoph Hellwig598ecfb2019-10-17 13:12:15 -0700489 return iomap_writepage(page, wbc, &wpc.ctx, &xfs_writeback_ops);
Dave Chinnerfbcc0252016-02-15 17:21:19 +1100490}
491
492STATIC int
Nathan Scott7d4fb402006-06-09 15:27:16 +1000493xfs_vm_writepages(
494 struct address_space *mapping,
495 struct writeback_control *wbc)
496{
Christoph Hellwigbe225fe2019-02-15 08:02:46 -0800497 struct xfs_writepage_ctx wpc = { };
Dave Chinnerfbcc0252016-02-15 17:21:19 +1100498
Dave Chinner756b1c32021-02-23 10:26:06 -0800499 /*
500 * Writing back data in a transaction context can result in recursive
501 * transactions. This is bad, so issue a warning and get out of here.
502 */
503 if (WARN_ON_ONCE(current->journal_info))
504 return 0;
505
Christoph Hellwigb3aea4e2007-08-29 11:44:37 +1000506 xfs_iflags_clear(XFS_I(mapping->host), XFS_ITRUNCATED);
Christoph Hellwig598ecfb2019-10-17 13:12:15 -0700507 return iomap_writepages(mapping, wbc, &wpc.ctx, &xfs_writeback_ops);
Nathan Scott7d4fb402006-06-09 15:27:16 +1000508}
509
Dan Williams6e2608d2018-03-07 15:26:44 -0800510STATIC int
511xfs_dax_writepages(
512 struct address_space *mapping,
513 struct writeback_control *wbc)
514{
Christoph Hellwig30fa5292019-10-24 22:25:38 -0700515 struct xfs_inode *ip = XFS_I(mapping->host);
516
517 xfs_iflags_clear(ip, XFS_ITRUNCATED);
Dan Williams6e2608d2018-03-07 15:26:44 -0800518 return dax_writeback_mapping_range(mapping,
Vivek Goyal3f666c52020-01-03 13:33:07 -0500519 xfs_inode_buftarg(ip)->bt_daxdev, wbc);
Dan Williams6e2608d2018-03-07 15:26:44 -0800520}
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522STATIC sector_t
Nathan Scotte4c573b2006-03-14 13:54:26 +1100523xfs_vm_bmap(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 struct address_space *mapping,
525 sector_t block)
526{
Christoph Hellwigb84e7722018-06-01 09:03:09 -0700527 struct xfs_inode *ip = XFS_I(mapping->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Christoph Hellwigb84e7722018-06-01 09:03:09 -0700529 trace_xfs_vm_bmap(ip);
Darrick J. Wongdb1327b2016-10-03 09:11:36 -0700530
531 /*
532 * The swap code (ab-)uses ->bmap to get a block mapping and then
Ingo Molnar793057e2018-02-28 09:39:48 +0100533 * bypasses the file system for actual I/O. We really can't allow
Darrick J. Wongdb1327b2016-10-03 09:11:36 -0700534 * that on reflinks inodes, so we have to skip out here. And yes,
Darrick J. Wongeb5e2482017-06-21 20:27:35 -0700535 * 0 is the magic code for a bmap error.
536 *
537 * Since we don't pass back blockdev info, we can't return bmap
538 * information for rt files either.
Darrick J. Wongdb1327b2016-10-03 09:11:36 -0700539 */
Christoph Hellwig66ae56a2019-02-18 09:38:49 -0800540 if (xfs_is_cow_inode(ip) || XFS_IS_REALTIME_INODE(ip))
Darrick J. Wongdb1327b2016-10-03 09:11:36 -0700541 return 0;
Christoph Hellwig690c2a32019-10-19 09:09:45 -0700542 return iomap_bmap(mapping, block, &xfs_read_iomap_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543}
544
545STATIC int
Nathan Scotte4c573b2006-03-14 13:54:26 +1100546xfs_vm_readpage(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 struct file *unused,
548 struct page *page)
549{
Christoph Hellwig690c2a32019-10-19 09:09:45 -0700550 return iomap_readpage(page, &xfs_read_iomap_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551}
552
Matthew Wilcox (Oracle)9d24a132020-06-01 21:47:34 -0700553STATIC void
554xfs_vm_readahead(
555 struct readahead_control *rac)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
Matthew Wilcox (Oracle)9d24a132020-06-01 21:47:34 -0700557 iomap_readahead(rac, &xfs_read_iomap_ops);
Dave Chinner22e757a2014-09-02 12:12:51 +1000558}
559
Darrick J. Wong67482122018-05-10 08:38:15 -0700560static int
561xfs_iomap_swapfile_activate(
562 struct swap_info_struct *sis,
563 struct file *swap_file,
564 sector_t *span)
565{
Christoph Hellwig30fa5292019-10-24 22:25:38 -0700566 sis->bdev = xfs_inode_buftarg(XFS_I(file_inode(swap_file)))->bt_bdev;
Christoph Hellwig690c2a32019-10-19 09:09:45 -0700567 return iomap_swapfile_activate(sis, swap_file, span,
568 &xfs_read_iomap_ops);
Darrick J. Wong67482122018-05-10 08:38:15 -0700569}
570
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700571const struct address_space_operations xfs_address_space_operations = {
Nathan Scotte4c573b2006-03-14 13:54:26 +1100572 .readpage = xfs_vm_readpage,
Matthew Wilcox (Oracle)9d24a132020-06-01 21:47:34 -0700573 .readahead = xfs_vm_readahead,
Nathan Scotte4c573b2006-03-14 13:54:26 +1100574 .writepage = xfs_vm_writepage,
Nathan Scott7d4fb402006-06-09 15:27:16 +1000575 .writepages = xfs_vm_writepages,
Christoph Hellwig82cb1412018-07-11 22:26:05 -0700576 .set_page_dirty = iomap_set_page_dirty,
Christoph Hellwig9e91c572019-10-17 13:12:13 -0700577 .releasepage = iomap_releasepage,
578 .invalidatepage = iomap_invalidatepage,
Nathan Scotte4c573b2006-03-14 13:54:26 +1100579 .bmap = xfs_vm_bmap,
Dan Williams6e2608d2018-03-07 15:26:44 -0800580 .direct_IO = noop_direct_IO,
Christoph Hellwig82cb1412018-07-11 22:26:05 -0700581 .migratepage = iomap_migrate_page,
582 .is_partially_uptodate = iomap_is_partially_uptodate,
Andi Kleenaa261f52009-09-16 11:50:16 +0200583 .error_remove_page = generic_error_remove_page,
Darrick J. Wong67482122018-05-10 08:38:15 -0700584 .swap_activate = xfs_iomap_swapfile_activate,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585};
Dan Williams6e2608d2018-03-07 15:26:44 -0800586
587const struct address_space_operations xfs_dax_aops = {
588 .writepages = xfs_dax_writepages,
589 .direct_IO = noop_direct_IO,
590 .set_page_dirty = noop_set_page_dirty,
591 .invalidatepage = noop_invalidatepage,
Darrick J. Wong67482122018-05-10 08:38:15 -0700592 .swap_activate = xfs_iomap_swapfile_activate,
Dan Williams6e2608d2018-03-07 15:26:44 -0800593};