blob: 4304c6416fbbc1e30b77d655bd3678c2815c5df9 [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4 * Copyright (c) 2016-2018 Christoph Hellwig.
5 * All Rights Reserved.
6 */
7#include "xfs.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"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000015#include "xfs_iomap.h"
16#include "xfs_trace.h"
17#include "xfs_bmap.h"
18#include "xfs_bmap_util.h"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000019#include "xfs_reflink.h"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000020
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000021struct xfs_writepage_ctx {
Olivier Deprez157378f2022-04-04 15:47:50 +020022 struct iomap_writepage_ctx ctx;
David Brazdil0f672f62019-12-10 10:32:29 +000023 unsigned int data_seq;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000024 unsigned int cow_seq;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000025};
26
Olivier Deprez157378f2022-04-04 15:47:50 +020027static inline struct xfs_writepage_ctx *
28XFS_WPC(struct iomap_writepage_ctx *ctx)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000029{
Olivier Deprez157378f2022-04-04 15:47:50 +020030 return container_of(ctx, struct xfs_writepage_ctx, ctx);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000031}
32
33/*
34 * Fast and loose check if this write could update the on-disk inode size.
35 */
Olivier Deprez157378f2022-04-04 15:47:50 +020036static inline bool xfs_ioend_is_append(struct iomap_ioend *ioend)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000037{
38 return ioend->io_offset + ioend->io_size >
39 XFS_I(ioend->io_inode)->i_d.di_size;
40}
41
42STATIC int
43xfs_setfilesize_trans_alloc(
Olivier Deprez157378f2022-04-04 15:47:50 +020044 struct iomap_ioend *ioend)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000045{
46 struct xfs_mount *mp = XFS_I(ioend->io_inode)->i_mount;
47 struct xfs_trans *tp;
48 int error;
49
David Brazdil0f672f62019-12-10 10:32:29 +000050 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_fsyncts, 0, 0, 0, &tp);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000051 if (error)
52 return error;
53
Olivier Deprez157378f2022-04-04 15:47:50 +020054 ioend->io_private = tp;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000055
56 /*
57 * We may pass freeze protection with a transaction. So tell lockdep
58 * we released it.
59 */
60 __sb_writers_release(ioend->io_inode->i_sb, SB_FREEZE_FS);
61 /*
62 * We hand off the transaction to the completion thread now, so
63 * clear the flag here.
64 */
65 current_restore_flags_nested(&tp->t_pflags, PF_MEMALLOC_NOFS);
66 return 0;
67}
68
69/*
70 * Update on-disk file size now that data has been written to disk.
71 */
72STATIC int
73__xfs_setfilesize(
74 struct xfs_inode *ip,
75 struct xfs_trans *tp,
76 xfs_off_t offset,
77 size_t size)
78{
79 xfs_fsize_t isize;
80
81 xfs_ilock(ip, XFS_ILOCK_EXCL);
82 isize = xfs_new_eof(ip, offset + size);
83 if (!isize) {
84 xfs_iunlock(ip, XFS_ILOCK_EXCL);
85 xfs_trans_cancel(tp);
86 return 0;
87 }
88
89 trace_xfs_setfilesize(ip, offset, size);
90
91 ip->i_d.di_size = isize;
92 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
93 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
94
95 return xfs_trans_commit(tp);
96}
97
98int
99xfs_setfilesize(
100 struct xfs_inode *ip,
101 xfs_off_t offset,
102 size_t size)
103{
104 struct xfs_mount *mp = ip->i_mount;
105 struct xfs_trans *tp;
106 int error;
107
108 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_fsyncts, 0, 0, 0, &tp);
109 if (error)
110 return error;
111
112 return __xfs_setfilesize(ip, tp, offset, size);
113}
114
115STATIC int
116xfs_setfilesize_ioend(
Olivier Deprez157378f2022-04-04 15:47:50 +0200117 struct iomap_ioend *ioend,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000118 int error)
119{
120 struct xfs_inode *ip = XFS_I(ioend->io_inode);
Olivier Deprez157378f2022-04-04 15:47:50 +0200121 struct xfs_trans *tp = ioend->io_private;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000122
123 /*
124 * The transaction may have been allocated in the I/O submission thread,
125 * thus we need to mark ourselves as being in a transaction manually.
126 * Similarly for freeze protection.
127 */
128 current_set_flags_nested(&tp->t_pflags, PF_MEMALLOC_NOFS);
129 __sb_writers_acquired(VFS_I(ip)->i_sb, SB_FREEZE_FS);
130
131 /* we abort the update if there was an IO error */
132 if (error) {
133 xfs_trans_cancel(tp);
134 return error;
135 }
136
137 return __xfs_setfilesize(ip, tp, ioend->io_offset, ioend->io_size);
138}
139
140/*
141 * IO write completion.
142 */
143STATIC void
David Brazdil0f672f62019-12-10 10:32:29 +0000144xfs_end_ioend(
Olivier Deprez157378f2022-04-04 15:47:50 +0200145 struct iomap_ioend *ioend)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000146{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000147 struct xfs_inode *ip = XFS_I(ioend->io_inode);
148 xfs_off_t offset = ioend->io_offset;
149 size_t size = ioend->io_size;
David Brazdil0f672f62019-12-10 10:32:29 +0000150 unsigned int nofs_flag;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000151 int error;
152
153 /*
David Brazdil0f672f62019-12-10 10:32:29 +0000154 * We can allocate memory here while doing writeback on behalf of
155 * memory reclaim. To avoid memory allocation deadlocks set the
156 * task-wide nofs context for the following operations.
157 */
158 nofs_flag = memalloc_nofs_save();
159
160 /*
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000161 * Just clean up the in-memory strutures if the fs has been shut down.
162 */
163 if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
164 error = -EIO;
165 goto done;
166 }
167
168 /*
169 * Clean up any COW blocks on an I/O error.
170 */
171 error = blk_status_to_errno(ioend->io_bio->bi_status);
172 if (unlikely(error)) {
Olivier Deprez157378f2022-04-04 15:47:50 +0200173 if (ioend->io_flags & IOMAP_F_SHARED)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000174 xfs_reflink_cancel_cow_range(ip, offset, size, true);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000175 goto done;
176 }
177
178 /*
David Brazdil0f672f62019-12-10 10:32:29 +0000179 * Success: commit the COW or unwritten blocks if needed.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000180 */
Olivier Deprez157378f2022-04-04 15:47:50 +0200181 if (ioend->io_flags & IOMAP_F_SHARED)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000182 error = xfs_reflink_end_cow(ip, offset, size);
Olivier Deprez157378f2022-04-04 15:47:50 +0200183 else if (ioend->io_type == IOMAP_UNWRITTEN)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000184 error = xfs_iomap_write_unwritten(ip, offset, size, false);
David Brazdil0f672f62019-12-10 10:32:29 +0000185 else
Olivier Deprez157378f2022-04-04 15:47:50 +0200186 ASSERT(!xfs_ioend_is_append(ioend) || ioend->io_private);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000187
188done:
Olivier Deprez157378f2022-04-04 15:47:50 +0200189 if (ioend->io_private)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000190 error = xfs_setfilesize_ioend(ioend, error);
Olivier Deprez157378f2022-04-04 15:47:50 +0200191 iomap_finish_ioends(ioend, error);
David Brazdil0f672f62019-12-10 10:32:29 +0000192 memalloc_nofs_restore(nofs_flag);
193}
194
195/*
David Brazdil0f672f62019-12-10 10:32:29 +0000196 * If the to be merged ioend has a preallocated transaction for file
197 * size updates we need to ensure the ioend it is merged into also
198 * has one. If it already has one we can simply cancel the transaction
199 * as it is guaranteed to be clean.
200 */
201static void
Olivier Deprez157378f2022-04-04 15:47:50 +0200202xfs_ioend_merge_private(
203 struct iomap_ioend *ioend,
204 struct iomap_ioend *next)
David Brazdil0f672f62019-12-10 10:32:29 +0000205{
Olivier Deprez157378f2022-04-04 15:47:50 +0200206 if (!ioend->io_private) {
207 ioend->io_private = next->io_private;
208 next->io_private = NULL;
David Brazdil0f672f62019-12-10 10:32:29 +0000209 } else {
210 xfs_setfilesize_ioend(next, -ECANCELED);
211 }
212}
213
David Brazdil0f672f62019-12-10 10:32:29 +0000214/* Finish all pending io completions. */
215void
216xfs_end_io(
217 struct work_struct *work)
218{
Olivier Deprez157378f2022-04-04 15:47:50 +0200219 struct xfs_inode *ip =
220 container_of(work, struct xfs_inode, i_ioend_work);
221 struct iomap_ioend *ioend;
222 struct list_head tmp;
David Brazdil0f672f62019-12-10 10:32:29 +0000223 unsigned long flags;
224
David Brazdil0f672f62019-12-10 10:32:29 +0000225 spin_lock_irqsave(&ip->i_ioend_lock, flags);
Olivier Deprez157378f2022-04-04 15:47:50 +0200226 list_replace_init(&ip->i_ioend_list, &tmp);
David Brazdil0f672f62019-12-10 10:32:29 +0000227 spin_unlock_irqrestore(&ip->i_ioend_lock, flags);
228
Olivier Deprez157378f2022-04-04 15:47:50 +0200229 iomap_sort_ioends(&tmp);
230 while ((ioend = list_first_entry_or_null(&tmp, struct iomap_ioend,
231 io_list))) {
David Brazdil0f672f62019-12-10 10:32:29 +0000232 list_del_init(&ioend->io_list);
Olivier Deprez157378f2022-04-04 15:47:50 +0200233 iomap_ioend_try_merge(ioend, &tmp, xfs_ioend_merge_private);
David Brazdil0f672f62019-12-10 10:32:29 +0000234 xfs_end_ioend(ioend);
235 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000236}
237
Olivier Deprez157378f2022-04-04 15:47:50 +0200238static inline bool xfs_ioend_needs_workqueue(struct iomap_ioend *ioend)
239{
240 return ioend->io_private ||
241 ioend->io_type == IOMAP_UNWRITTEN ||
242 (ioend->io_flags & IOMAP_F_SHARED);
243}
244
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000245STATIC void
246xfs_end_bio(
247 struct bio *bio)
248{
Olivier Deprez157378f2022-04-04 15:47:50 +0200249 struct iomap_ioend *ioend = bio->bi_private;
David Brazdil0f672f62019-12-10 10:32:29 +0000250 struct xfs_inode *ip = XFS_I(ioend->io_inode);
David Brazdil0f672f62019-12-10 10:32:29 +0000251 unsigned long flags;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000252
Olivier Deprez157378f2022-04-04 15:47:50 +0200253 ASSERT(xfs_ioend_needs_workqueue(ioend));
254
255 spin_lock_irqsave(&ip->i_ioend_lock, flags);
256 if (list_empty(&ip->i_ioend_list))
257 WARN_ON_ONCE(!queue_work(ip->i_mount->m_unwritten_workqueue,
258 &ip->i_ioend_work));
259 list_add_tail(&ioend->io_list, &ip->i_ioend_list);
260 spin_unlock_irqrestore(&ip->i_ioend_lock, flags);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000261}
262
David Brazdil0f672f62019-12-10 10:32:29 +0000263/*
264 * Fast revalidation of the cached writeback mapping. Return true if the current
265 * mapping is valid, false otherwise.
266 */
267static bool
268xfs_imap_valid(
Olivier Deprez157378f2022-04-04 15:47:50 +0200269 struct iomap_writepage_ctx *wpc,
David Brazdil0f672f62019-12-10 10:32:29 +0000270 struct xfs_inode *ip,
Olivier Deprez157378f2022-04-04 15:47:50 +0200271 loff_t offset)
David Brazdil0f672f62019-12-10 10:32:29 +0000272{
Olivier Deprez157378f2022-04-04 15:47:50 +0200273 if (offset < wpc->iomap.offset ||
274 offset >= wpc->iomap.offset + wpc->iomap.length)
David Brazdil0f672f62019-12-10 10:32:29 +0000275 return false;
276 /*
277 * If this is a COW mapping, it is sufficient to check that the mapping
278 * covers the offset. Be careful to check this first because the caller
279 * can revalidate a COW mapping without updating the data seqno.
280 */
Olivier Deprez157378f2022-04-04 15:47:50 +0200281 if (wpc->iomap.flags & IOMAP_F_SHARED)
David Brazdil0f672f62019-12-10 10:32:29 +0000282 return true;
283
284 /*
285 * This is not a COW mapping. Check the sequence number of the data fork
286 * because concurrent changes could have invalidated the extent. Check
287 * the COW fork because concurrent changes since the last time we
288 * checked (and found nothing at this offset) could have added
289 * overlapping blocks.
290 */
Olivier Deprez157378f2022-04-04 15:47:50 +0200291 if (XFS_WPC(wpc)->data_seq != READ_ONCE(ip->i_df.if_seq))
David Brazdil0f672f62019-12-10 10:32:29 +0000292 return false;
293 if (xfs_inode_has_cow_data(ip) &&
Olivier Deprez157378f2022-04-04 15:47:50 +0200294 XFS_WPC(wpc)->cow_seq != READ_ONCE(ip->i_cowfp->if_seq))
David Brazdil0f672f62019-12-10 10:32:29 +0000295 return false;
296 return true;
297}
298
299/*
300 * Pass in a dellalloc extent and convert it to real extents, return the real
Olivier Deprez157378f2022-04-04 15:47:50 +0200301 * extent that maps offset_fsb in wpc->iomap.
David Brazdil0f672f62019-12-10 10:32:29 +0000302 *
303 * The current page is held locked so nothing could have removed the block
304 * backing offset_fsb, although it could have moved from the COW to the data
305 * fork by another thread.
306 */
307static int
308xfs_convert_blocks(
Olivier Deprez157378f2022-04-04 15:47:50 +0200309 struct iomap_writepage_ctx *wpc,
David Brazdil0f672f62019-12-10 10:32:29 +0000310 struct xfs_inode *ip,
Olivier Deprez157378f2022-04-04 15:47:50 +0200311 int whichfork,
312 loff_t offset)
David Brazdil0f672f62019-12-10 10:32:29 +0000313{
314 int error;
Olivier Deprez157378f2022-04-04 15:47:50 +0200315 unsigned *seq;
316
317 if (whichfork == XFS_COW_FORK)
318 seq = &XFS_WPC(wpc)->cow_seq;
319 else
320 seq = &XFS_WPC(wpc)->data_seq;
David Brazdil0f672f62019-12-10 10:32:29 +0000321
322 /*
Olivier Deprez157378f2022-04-04 15:47:50 +0200323 * Attempt to allocate whatever delalloc extent currently backs offset
324 * and put the result into wpc->iomap. Allocate in a loop because it
325 * may take several attempts to allocate real blocks for a contiguous
326 * delalloc extent if free space is sufficiently fragmented.
David Brazdil0f672f62019-12-10 10:32:29 +0000327 */
328 do {
Olivier Deprez157378f2022-04-04 15:47:50 +0200329 error = xfs_bmapi_convert_delalloc(ip, whichfork, offset,
330 &wpc->iomap, seq);
David Brazdil0f672f62019-12-10 10:32:29 +0000331 if (error)
332 return error;
Olivier Deprez157378f2022-04-04 15:47:50 +0200333 } while (wpc->iomap.offset + wpc->iomap.length <= offset);
David Brazdil0f672f62019-12-10 10:32:29 +0000334
335 return 0;
336}
337
Olivier Deprez157378f2022-04-04 15:47:50 +0200338static int
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000339xfs_map_blocks(
Olivier Deprez157378f2022-04-04 15:47:50 +0200340 struct iomap_writepage_ctx *wpc,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000341 struct inode *inode,
342 loff_t offset)
343{
344 struct xfs_inode *ip = XFS_I(inode);
345 struct xfs_mount *mp = ip->i_mount;
346 ssize_t count = i_blocksize(inode);
David Brazdil0f672f62019-12-10 10:32:29 +0000347 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
348 xfs_fileoff_t end_fsb = XFS_B_TO_FSB(mp, offset + count);
Olivier Deprez157378f2022-04-04 15:47:50 +0200349 xfs_fileoff_t cow_fsb;
350 int whichfork;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000351 struct xfs_bmbt_irec imap;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000352 struct xfs_iext_cursor icur;
David Brazdil0f672f62019-12-10 10:32:29 +0000353 int retries = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000354 int error = 0;
355
David Brazdil0f672f62019-12-10 10:32:29 +0000356 if (XFS_FORCED_SHUTDOWN(mp))
357 return -EIO;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000358
359 /*
360 * COW fork blocks can overlap data fork blocks even if the blocks
361 * aren't shared. COW I/O always takes precedent, so we must always
362 * check for overlap on reflink inodes unless the mapping is already a
363 * COW one, or the COW fork hasn't changed from the last time we looked
364 * at it.
365 *
366 * It's safe to check the COW fork if_seq here without the ILOCK because
367 * we've indirectly protected against concurrent updates: writeback has
368 * the page locked, which prevents concurrent invalidations by reflink
369 * and directio and prevents concurrent buffered writes to the same
370 * page. Changes to if_seq always happen under i_lock, which protects
371 * against concurrent updates and provides a memory barrier on the way
372 * out that ensures that we always see the current value.
373 */
Olivier Deprez157378f2022-04-04 15:47:50 +0200374 if (xfs_imap_valid(wpc, ip, offset))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000375 return 0;
376
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000377 /*
378 * If we don't have a valid map, now it's time to get a new one for this
379 * offset. This will convert delayed allocations (including COW ones)
380 * into real extents. If we return without a valid map, it means we
381 * landed in a hole and we skip the block.
382 */
David Brazdil0f672f62019-12-10 10:32:29 +0000383retry:
Olivier Deprez157378f2022-04-04 15:47:50 +0200384 cow_fsb = NULLFILEOFF;
385 whichfork = XFS_DATA_FORK;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000386 xfs_ilock(ip, XFS_ILOCK_SHARED);
Olivier Deprez157378f2022-04-04 15:47:50 +0200387 ASSERT(ip->i_df.if_format != XFS_DINODE_FMT_BTREE ||
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000388 (ip->i_df.if_flags & XFS_IFEXTENTS));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000389
390 /*
391 * Check if this is offset is covered by a COW extents, and if yes use
392 * it directly instead of looking up anything in the data fork.
393 */
394 if (xfs_inode_has_cow_data(ip) &&
395 xfs_iext_lookup_extent(ip, ip->i_cowfp, offset_fsb, &icur, &imap))
396 cow_fsb = imap.br_startoff;
397 if (cow_fsb != NULLFILEOFF && cow_fsb <= offset_fsb) {
Olivier Deprez157378f2022-04-04 15:47:50 +0200398 XFS_WPC(wpc)->cow_seq = READ_ONCE(ip->i_cowfp->if_seq);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000399 xfs_iunlock(ip, XFS_ILOCK_SHARED);
David Brazdil0f672f62019-12-10 10:32:29 +0000400
Olivier Deprez157378f2022-04-04 15:47:50 +0200401 whichfork = XFS_COW_FORK;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000402 goto allocate_blocks;
403 }
404
405 /*
David Brazdil0f672f62019-12-10 10:32:29 +0000406 * No COW extent overlap. Revalidate now that we may have updated
407 * ->cow_seq. If the data mapping is still valid, we're done.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000408 */
Olivier Deprez157378f2022-04-04 15:47:50 +0200409 if (xfs_imap_valid(wpc, ip, offset)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000410 xfs_iunlock(ip, XFS_ILOCK_SHARED);
411 return 0;
412 }
413
414 /*
415 * If we don't have a valid map, now it's time to get a new one for this
416 * offset. This will convert delayed allocations (including COW ones)
417 * into real extents.
418 */
419 if (!xfs_iext_lookup_extent(ip, &ip->i_df, offset_fsb, &icur, &imap))
420 imap.br_startoff = end_fsb; /* fake a hole past EOF */
Olivier Deprez157378f2022-04-04 15:47:50 +0200421 XFS_WPC(wpc)->data_seq = READ_ONCE(ip->i_df.if_seq);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000422 xfs_iunlock(ip, XFS_ILOCK_SHARED);
423
David Brazdil0f672f62019-12-10 10:32:29 +0000424 /* landed in a hole or beyond EOF? */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000425 if (imap.br_startoff > offset_fsb) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000426 imap.br_blockcount = imap.br_startoff - offset_fsb;
427 imap.br_startoff = offset_fsb;
428 imap.br_startblock = HOLESTARTBLOCK;
David Brazdil0f672f62019-12-10 10:32:29 +0000429 imap.br_state = XFS_EXT_NORM;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000430 }
431
David Brazdil0f672f62019-12-10 10:32:29 +0000432 /*
433 * Truncate to the next COW extent if there is one. This is the only
434 * opportunity to do this because we can skip COW fork lookups for the
435 * subsequent blocks in the mapping; however, the requirement to treat
436 * the COW range separately remains.
437 */
438 if (cow_fsb != NULLFILEOFF &&
439 cow_fsb < imap.br_startoff + imap.br_blockcount)
440 imap.br_blockcount = cow_fsb - imap.br_startoff;
441
442 /* got a delalloc extent? */
443 if (imap.br_startblock != HOLESTARTBLOCK &&
444 isnullstartblock(imap.br_startblock))
445 goto allocate_blocks;
446
Olivier Deprez157378f2022-04-04 15:47:50 +0200447 xfs_bmbt_to_iomap(ip, &wpc->iomap, &imap, 0);
448 trace_xfs_map_blocks_found(ip, offset, count, whichfork, &imap);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000449 return 0;
450allocate_blocks:
Olivier Deprez157378f2022-04-04 15:47:50 +0200451 error = xfs_convert_blocks(wpc, ip, whichfork, offset);
David Brazdil0f672f62019-12-10 10:32:29 +0000452 if (error) {
453 /*
454 * If we failed to find the extent in the COW fork we might have
455 * raced with a COW to data fork conversion or truncate.
456 * Restart the lookup to catch the extent in the data fork for
457 * the former case, but prevent additional retries to avoid
458 * looping forever for the latter case.
459 */
Olivier Deprez157378f2022-04-04 15:47:50 +0200460 if (error == -EAGAIN && whichfork == XFS_COW_FORK && !retries++)
David Brazdil0f672f62019-12-10 10:32:29 +0000461 goto retry;
462 ASSERT(error != -EAGAIN);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000463 return error;
David Brazdil0f672f62019-12-10 10:32:29 +0000464 }
465
466 /*
467 * Due to merging the return real extent might be larger than the
468 * original delalloc one. Trim the return extent to the next COW
469 * boundary again to force a re-lookup.
470 */
Olivier Deprez157378f2022-04-04 15:47:50 +0200471 if (whichfork != XFS_COW_FORK && cow_fsb != NULLFILEOFF) {
472 loff_t cow_offset = XFS_FSB_TO_B(mp, cow_fsb);
David Brazdil0f672f62019-12-10 10:32:29 +0000473
Olivier Deprez157378f2022-04-04 15:47:50 +0200474 if (cow_offset < wpc->iomap.offset + wpc->iomap.length)
475 wpc->iomap.length = cow_offset - wpc->iomap.offset;
476 }
477
478 ASSERT(wpc->iomap.offset <= offset);
479 ASSERT(wpc->iomap.offset + wpc->iomap.length > offset);
480 trace_xfs_map_blocks_alloc(ip, offset, count, whichfork, &imap);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000481 return 0;
482}
483
Olivier Deprez157378f2022-04-04 15:47:50 +0200484static int
485xfs_prepare_ioend(
486 struct iomap_ioend *ioend,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000487 int status)
488{
David Brazdil0f672f62019-12-10 10:32:29 +0000489 unsigned int nofs_flag;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000490
David Brazdil0f672f62019-12-10 10:32:29 +0000491 /*
492 * We can allocate memory here while doing writeback on behalf of
493 * memory reclaim. To avoid memory allocation deadlocks set the
494 * task-wide nofs context for the following operations.
495 */
496 nofs_flag = memalloc_nofs_save();
497
498 /* Convert CoW extents to regular */
Olivier Deprez157378f2022-04-04 15:47:50 +0200499 if (!status && (ioend->io_flags & IOMAP_F_SHARED)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000500 status = xfs_reflink_convert_cow(XFS_I(ioend->io_inode),
501 ioend->io_offset, ioend->io_size);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000502 }
503
504 /* Reserve log space if we might write beyond the on-disk inode size. */
505 if (!status &&
Olivier Deprez157378f2022-04-04 15:47:50 +0200506 ((ioend->io_flags & IOMAP_F_SHARED) ||
507 ioend->io_type != IOMAP_UNWRITTEN) &&
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000508 xfs_ioend_is_append(ioend) &&
Olivier Deprez157378f2022-04-04 15:47:50 +0200509 !ioend->io_private)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000510 status = xfs_setfilesize_trans_alloc(ioend);
511
David Brazdil0f672f62019-12-10 10:32:29 +0000512 memalloc_nofs_restore(nofs_flag);
513
Olivier Deprez157378f2022-04-04 15:47:50 +0200514 if (xfs_ioend_needs_workqueue(ioend))
515 ioend->io_bio->bi_end_io = xfs_end_bio;
516 return status;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000517}
518
519/*
520 * If the page has delalloc blocks on it, we need to punch them out before we
521 * invalidate the page. If we don't, we leave a stale delalloc mapping on the
522 * inode that can trip up a later direct I/O read operation on the same region.
523 *
524 * We prevent this by truncating away the delalloc regions on the page. Because
525 * they are delalloc, we can do this without needing a transaction. Indeed - if
526 * we get ENOSPC errors, we have to be able to do this truncation without a
527 * transaction as there is no space left for block reservation (typically why we
528 * see a ENOSPC in writeback).
529 */
Olivier Deprez157378f2022-04-04 15:47:50 +0200530static void
531xfs_discard_page(
532 struct page *page,
533 loff_t fileoff)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000534{
535 struct inode *inode = page->mapping->host;
536 struct xfs_inode *ip = XFS_I(inode);
537 struct xfs_mount *mp = ip->i_mount;
Olivier Deprez157378f2022-04-04 15:47:50 +0200538 unsigned int pageoff = offset_in_page(fileoff);
539 xfs_fileoff_t start_fsb = XFS_B_TO_FSBT(mp, fileoff);
540 xfs_fileoff_t pageoff_fsb = XFS_B_TO_FSBT(mp, pageoff);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000541 int error;
542
543 if (XFS_FORCED_SHUTDOWN(mp))
544 goto out_invalidate;
545
Olivier Deprez157378f2022-04-04 15:47:50 +0200546 xfs_alert_ratelimited(mp,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000547 "page discard on page "PTR_FMT", inode 0x%llx, offset %llu.",
Olivier Deprez157378f2022-04-04 15:47:50 +0200548 page, ip->i_ino, fileoff);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000549
550 error = xfs_bmap_punch_delalloc_range(ip, start_fsb,
Olivier Deprez157378f2022-04-04 15:47:50 +0200551 i_blocks_per_page(inode, page) - pageoff_fsb);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000552 if (error && !XFS_FORCED_SHUTDOWN(mp))
553 xfs_alert(mp, "page discard unable to remove delalloc mapping.");
554out_invalidate:
Olivier Deprez157378f2022-04-04 15:47:50 +0200555 iomap_invalidatepage(page, pageoff, PAGE_SIZE - pageoff);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000556}
557
Olivier Deprez157378f2022-04-04 15:47:50 +0200558static const struct iomap_writeback_ops xfs_writeback_ops = {
559 .map_blocks = xfs_map_blocks,
560 .prepare_ioend = xfs_prepare_ioend,
561 .discard_page = xfs_discard_page,
562};
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000563
564STATIC int
565xfs_vm_writepage(
566 struct page *page,
567 struct writeback_control *wbc)
568{
David Brazdil0f672f62019-12-10 10:32:29 +0000569 struct xfs_writepage_ctx wpc = { };
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000570
Olivier Deprez157378f2022-04-04 15:47:50 +0200571 return iomap_writepage(page, wbc, &wpc.ctx, &xfs_writeback_ops);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000572}
573
574STATIC int
575xfs_vm_writepages(
576 struct address_space *mapping,
577 struct writeback_control *wbc)
578{
David Brazdil0f672f62019-12-10 10:32:29 +0000579 struct xfs_writepage_ctx wpc = { };
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000580
581 xfs_iflags_clear(XFS_I(mapping->host), XFS_ITRUNCATED);
Olivier Deprez157378f2022-04-04 15:47:50 +0200582 return iomap_writepages(mapping, wbc, &wpc.ctx, &xfs_writeback_ops);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000583}
584
585STATIC int
586xfs_dax_writepages(
587 struct address_space *mapping,
588 struct writeback_control *wbc)
589{
Olivier Deprez157378f2022-04-04 15:47:50 +0200590 struct xfs_inode *ip = XFS_I(mapping->host);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000591
Olivier Deprez157378f2022-04-04 15:47:50 +0200592 xfs_iflags_clear(ip, XFS_ITRUNCATED);
593 return dax_writeback_mapping_range(mapping,
594 xfs_inode_buftarg(ip)->bt_daxdev, wbc);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000595}
596
597STATIC sector_t
598xfs_vm_bmap(
599 struct address_space *mapping,
600 sector_t block)
601{
602 struct xfs_inode *ip = XFS_I(mapping->host);
603
604 trace_xfs_vm_bmap(ip);
605
606 /*
607 * The swap code (ab-)uses ->bmap to get a block mapping and then
608 * bypasses the file system for actual I/O. We really can't allow
609 * that on reflinks inodes, so we have to skip out here. And yes,
610 * 0 is the magic code for a bmap error.
611 *
612 * Since we don't pass back blockdev info, we can't return bmap
613 * information for rt files either.
614 */
David Brazdil0f672f62019-12-10 10:32:29 +0000615 if (xfs_is_cow_inode(ip) || XFS_IS_REALTIME_INODE(ip))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000616 return 0;
Olivier Deprez157378f2022-04-04 15:47:50 +0200617 return iomap_bmap(mapping, block, &xfs_read_iomap_ops);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000618}
619
620STATIC int
621xfs_vm_readpage(
622 struct file *unused,
623 struct page *page)
624{
Olivier Deprez157378f2022-04-04 15:47:50 +0200625 return iomap_readpage(page, &xfs_read_iomap_ops);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000626}
627
Olivier Deprez157378f2022-04-04 15:47:50 +0200628STATIC void
629xfs_vm_readahead(
630 struct readahead_control *rac)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000631{
Olivier Deprez157378f2022-04-04 15:47:50 +0200632 iomap_readahead(rac, &xfs_read_iomap_ops);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000633}
634
635static int
636xfs_iomap_swapfile_activate(
637 struct swap_info_struct *sis,
638 struct file *swap_file,
639 sector_t *span)
640{
Olivier Deprez157378f2022-04-04 15:47:50 +0200641 sis->bdev = xfs_inode_buftarg(XFS_I(file_inode(swap_file)))->bt_bdev;
642 return iomap_swapfile_activate(sis, swap_file, span,
643 &xfs_read_iomap_ops);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000644}
645
646const struct address_space_operations xfs_address_space_operations = {
647 .readpage = xfs_vm_readpage,
Olivier Deprez157378f2022-04-04 15:47:50 +0200648 .readahead = xfs_vm_readahead,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000649 .writepage = xfs_vm_writepage,
650 .writepages = xfs_vm_writepages,
651 .set_page_dirty = iomap_set_page_dirty,
Olivier Deprez157378f2022-04-04 15:47:50 +0200652 .releasepage = iomap_releasepage,
653 .invalidatepage = iomap_invalidatepage,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000654 .bmap = xfs_vm_bmap,
655 .direct_IO = noop_direct_IO,
656 .migratepage = iomap_migrate_page,
657 .is_partially_uptodate = iomap_is_partially_uptodate,
658 .error_remove_page = generic_error_remove_page,
659 .swap_activate = xfs_iomap_swapfile_activate,
660};
661
662const struct address_space_operations xfs_dax_aops = {
663 .writepages = xfs_dax_writepages,
664 .direct_IO = noop_direct_IO,
665 .set_page_dirty = noop_set_page_dirty,
666 .invalidatepage = noop_invalidatepage,
667 .swap_activate = xfs_iomap_swapfile_activate,
668};