blob: b35611882ff9c565d740c8b80e17633a0096a41e [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 >
39 XFS_I(ioend->io_inode)->i_d.di_size;
40}
41
Christoph Hellwig281627d2012-03-13 08:41:05 +000042STATIC int
43xfs_setfilesize_trans_alloc(
Christoph Hellwig598ecfb2019-10-17 13:12:15 -070044 struct iomap_ioend *ioend)
Christoph Hellwig281627d2012-03-13 08:41:05 +000045{
46 struct xfs_mount *mp = XFS_I(ioend->io_inode)->i_mount;
47 struct xfs_trans *tp;
48 int error;
49
Christoph Hellwig73d30d42019-06-28 19:31:38 -070050 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_fsyncts, 0, 0, 0, &tp);
Christoph Hellwig253f4912016-04-06 09:19:55 +100051 if (error)
Christoph Hellwig281627d2012-03-13 08:41:05 +000052 return error;
Christoph Hellwig281627d2012-03-13 08:41:05 +000053
Christoph Hellwig56530172019-10-17 13:12:09 -070054 ioend->io_private = tp;
Christoph Hellwig281627d2012-03-13 08:41:05 +000055
56 /*
Dave Chinner437a2552012-11-28 13:01:00 +110057 * We may pass freeze protection with a transaction. So tell lockdep
Jan Karad9457dc2012-06-12 16:20:39 +020058 * we released it.
59 */
Oleg Nesterovbee91822015-07-19 23:48:20 +020060 __sb_writers_release(ioend->io_inode->i_sb, SB_FREEZE_FS);
Jan Karad9457dc2012-06-12 16:20:39 +020061 /*
Christoph Hellwig281627d2012-03-13 08:41:05 +000062 * We hand off the transaction to the completion thread now, so
63 * clear the flag here.
64 */
Michal Hocko90707332017-05-03 14:53:12 -070065 current_restore_flags_nested(&tp->t_pflags, PF_MEMALLOC_NOFS);
Christoph Hellwig281627d2012-03-13 08:41:05 +000066 return 0;
67}
68
Christoph Hellwigfc0063c2011-08-23 08:28:11 +000069/*
Christoph Hellwig2813d682011-12-18 20:00:12 +000070 * Update on-disk file size now that data has been written to disk.
Lachlan McIlroyba87ea62007-05-08 13:49:46 +100071 */
Christoph Hellwig281627d2012-03-13 08:41:05 +000072STATIC int
Christoph Hellwige3728432016-09-19 11:26:41 +100073__xfs_setfilesize(
Christoph Hellwig2ba66232015-02-02 10:02:09 +110074 struct xfs_inode *ip,
75 struct xfs_trans *tp,
76 xfs_off_t offset,
77 size_t size)
Lachlan McIlroyba87ea62007-05-08 13:49:46 +100078{
Lachlan McIlroyba87ea62007-05-08 13:49:46 +100079 xfs_fsize_t isize;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +100080
Christoph Hellwigaa6bf012012-02-29 09:53:48 +000081 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig2ba66232015-02-02 10:02:09 +110082 isize = xfs_new_eof(ip, offset + size);
Christoph Hellwig281627d2012-03-13 08:41:05 +000083 if (!isize) {
84 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig4906e212015-06-04 13:47:56 +100085 xfs_trans_cancel(tp);
Christoph Hellwig281627d2012-03-13 08:41:05 +000086 return 0;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +100087 }
88
Christoph Hellwig2ba66232015-02-02 10:02:09 +110089 trace_xfs_setfilesize(ip, offset, size);
Christoph Hellwig281627d2012-03-13 08:41:05 +000090
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
Christoph Hellwig70393312015-06-04 13:48:08 +100095 return xfs_trans_commit(tp);
Christoph Hellwig0829c362005-09-02 16:58:49 +100096}
97
Christoph Hellwige3728432016-09-19 11:26:41 +100098int
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
Christoph Hellwig2ba66232015-02-02 10:02:09 +1100115STATIC int
116xfs_setfilesize_ioend(
Christoph Hellwig598ecfb2019-10-17 13:12:15 -0700117 struct iomap_ioend *ioend,
Christoph Hellwig0e51a8e2016-04-06 08:34:30 +1000118 int error)
Christoph Hellwig2ba66232015-02-02 10:02:09 +1100119{
120 struct xfs_inode *ip = XFS_I(ioend->io_inode);
Christoph Hellwig56530172019-10-17 13:12:09 -0700121 struct xfs_trans *tp = ioend->io_private;
Christoph Hellwig2ba66232015-02-02 10:02:09 +1100122
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 */
Michal Hocko90707332017-05-03 14:53:12 -0700128 current_set_flags_nested(&tp->t_pflags, PF_MEMALLOC_NOFS);
Oleg Nesterovbee91822015-07-19 23:48:20 +0200129 __sb_writers_acquired(VFS_I(ip)->i_sb, SB_FREEZE_FS);
Christoph Hellwig2ba66232015-02-02 10:02:09 +1100130
Zhaohongjiang5cb13dc2015-10-12 15:28:39 +1100131 /* we abort the update if there was an IO error */
Christoph Hellwig0e51a8e2016-04-06 08:34:30 +1000132 if (error) {
Zhaohongjiang5cb13dc2015-10-12 15:28:39 +1100133 xfs_trans_cancel(tp);
Christoph Hellwig0e51a8e2016-04-06 08:34:30 +1000134 return error;
Zhaohongjiang5cb13dc2015-10-12 15:28:39 +1100135 }
136
Christoph Hellwige3728432016-09-19 11:26:41 +1000137 return __xfs_setfilesize(ip, tp, ioend->io_offset, ioend->io_size);
Christoph Hellwig2ba66232015-02-02 10:02:09 +1100138}
139
Christoph Hellwig0829c362005-09-02 16:58:49 +1000140/*
Dave Chinner77d7a0c2010-02-17 05:36:29 +0000141 * IO write completion.
142 */
143STATIC void
Darrick J. Wongcb357bf2019-04-15 13:13:20 -0700144xfs_end_ioend(
Christoph Hellwig598ecfb2019-10-17 13:12:15 -0700145 struct iomap_ioend *ioend)
Dave Chinner77d7a0c2010-02-17 05:36:29 +0000146{
Christoph Hellwig0e51a8e2016-04-06 08:34:30 +1000147 struct xfs_inode *ip = XFS_I(ioend->io_inode);
Christoph Hellwig787eb482017-03-02 15:02:51 -0800148 xfs_off_t offset = ioend->io_offset;
149 size_t size = ioend->io_size;
Christoph Hellwig73d30d42019-06-28 19:31:38 -0700150 unsigned int nofs_flag;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200151 int error;
Dave Chinner77d7a0c2010-02-17 05:36:29 +0000152
Brian Fosteraf055e32016-02-08 15:00:02 +1100153 /*
Christoph Hellwig73d30d42019-06-28 19:31:38 -0700154 * 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 /*
Christoph Hellwig787eb482017-03-02 15:02:51 -0800161 * Just clean up the in-memory strutures if the fs has been shut down.
Brian Fosteraf055e32016-02-08 15:00:02 +1100162 */
Christoph Hellwig787eb482017-03-02 15:02:51 -0800163 if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
Christoph Hellwig0e51a8e2016-04-06 08:34:30 +1000164 error = -EIO;
Christoph Hellwig787eb482017-03-02 15:02:51 -0800165 goto done;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700166 }
167
168 /*
Christoph Hellwig787eb482017-03-02 15:02:51 -0800169 * Clean up any COW blocks on an I/O error.
Dave Chinner77d7a0c2010-02-17 05:36:29 +0000170 */
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200171 error = blk_status_to_errno(ioend->io_bio->bi_status);
Christoph Hellwig787eb482017-03-02 15:02:51 -0800172 if (unlikely(error)) {
Christoph Hellwig760fea82019-10-17 13:12:10 -0700173 if (ioend->io_flags & IOMAP_F_SHARED)
Christoph Hellwig787eb482017-03-02 15:02:51 -0800174 xfs_reflink_cancel_cow_range(ip, offset, size, true);
Christoph Hellwig787eb482017-03-02 15:02:51 -0800175 goto done;
176 }
177
178 /*
Christoph Hellwigbe225fe2019-02-15 08:02:46 -0800179 * Success: commit the COW or unwritten blocks if needed.
Christoph Hellwig787eb482017-03-02 15:02:51 -0800180 */
Christoph Hellwig760fea82019-10-17 13:12:10 -0700181 if (ioend->io_flags & IOMAP_F_SHARED)
Christoph Hellwig787eb482017-03-02 15:02:51 -0800182 error = xfs_reflink_end_cow(ip, offset, size);
Christoph Hellwig4e087a32019-10-17 13:12:06 -0700183 else if (ioend->io_type == IOMAP_UNWRITTEN)
Eryu Guanee70daa2017-09-21 11:26:18 -0700184 error = xfs_iomap_write_unwritten(ip, offset, size, false);
Christoph Hellwigbe225fe2019-02-15 08:02:46 -0800185 else
Christoph Hellwig56530172019-10-17 13:12:09 -0700186 ASSERT(!xfs_ioend_is_append(ioend) || ioend->io_private);
Dave Chinner77d7a0c2010-02-17 05:36:29 +0000187
Christoph Hellwig04f658e2011-08-24 05:59:25 +0000188done:
Christoph Hellwig56530172019-10-17 13:12:09 -0700189 if (ioend->io_private)
Christoph Hellwig787eb482017-03-02 15:02:51 -0800190 error = xfs_setfilesize_ioend(ioend, error);
Christoph Hellwig598ecfb2019-10-17 13:12:15 -0700191 iomap_finish_ioends(ioend, error);
Christoph Hellwig73d30d42019-06-28 19:31:38 -0700192 memalloc_nofs_restore(nofs_flag);
Darrick J. Wong3994fc42019-04-15 13:13:21 -0700193}
194
195/*
Christoph Hellwig7dbae9f2019-06-28 19:31:37 -0700196 * 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
Christoph Hellwig56530172019-10-17 13:12:09 -0700202xfs_ioend_merge_private(
Christoph Hellwig598ecfb2019-10-17 13:12:15 -0700203 struct iomap_ioend *ioend,
204 struct iomap_ioend *next)
Christoph Hellwig7dbae9f2019-06-28 19:31:37 -0700205{
Christoph Hellwig56530172019-10-17 13:12:09 -0700206 if (!ioend->io_private) {
207 ioend->io_private = next->io_private;
208 next->io_private = NULL;
Christoph Hellwig7dbae9f2019-06-28 19:31:37 -0700209 } else {
210 xfs_setfilesize_ioend(next, -ECANCELED);
211 }
212}
213
Darrick J. Wongcb357bf2019-04-15 13:13:20 -0700214/* Finish all pending io completions. */
215void
216xfs_end_io(
217 struct work_struct *work)
218{
Christoph Hellwig433dad92019-10-17 13:12:07 -0700219 struct xfs_inode *ip =
220 container_of(work, struct xfs_inode, i_ioend_work);
Christoph Hellwig598ecfb2019-10-17 13:12:15 -0700221 struct iomap_ioend *ioend;
Christoph Hellwig433dad92019-10-17 13:12:07 -0700222 struct list_head tmp;
Darrick J. Wongcb357bf2019-04-15 13:13:20 -0700223 unsigned long flags;
224
Darrick J. Wongcb357bf2019-04-15 13:13:20 -0700225 spin_lock_irqsave(&ip->i_ioend_lock, flags);
Christoph Hellwig433dad92019-10-17 13:12:07 -0700226 list_replace_init(&ip->i_ioend_list, &tmp);
Darrick J. Wongcb357bf2019-04-15 13:13:20 -0700227 spin_unlock_irqrestore(&ip->i_ioend_lock, flags);
228
Christoph Hellwig598ecfb2019-10-17 13:12:15 -0700229 iomap_sort_ioends(&tmp);
230 while ((ioend = list_first_entry_or_null(&tmp, struct iomap_ioend,
Christoph Hellwig433dad92019-10-17 13:12:07 -0700231 io_list))) {
Darrick J. Wongcb357bf2019-04-15 13:13:20 -0700232 list_del_init(&ioend->io_list);
Christoph Hellwig598ecfb2019-10-17 13:12:15 -0700233 iomap_ioend_try_merge(ioend, &tmp, xfs_ioend_merge_private);
Darrick J. Wongcb357bf2019-04-15 13:13:20 -0700234 xfs_end_ioend(ioend);
235 }
236}
237
Christoph Hellwig598ecfb2019-10-17 13:12:15 -0700238static inline bool xfs_ioend_needs_workqueue(struct iomap_ioend *ioend)
Christoph Hellwig760fea82019-10-17 13:12:10 -0700239{
240 return ioend->io_private ||
241 ioend->io_type == IOMAP_UNWRITTEN ||
242 (ioend->io_flags & IOMAP_F_SHARED);
243}
244
Christoph Hellwig0e51a8e2016-04-06 08:34:30 +1000245STATIC void
246xfs_end_bio(
247 struct bio *bio)
Christoph Hellwig0829c362005-09-02 16:58:49 +1000248{
Christoph Hellwig598ecfb2019-10-17 13:12:15 -0700249 struct iomap_ioend *ioend = bio->bi_private;
Darrick J. Wongcb357bf2019-04-15 13:13:20 -0700250 struct xfs_inode *ip = XFS_I(ioend->io_inode);
Darrick J. Wongcb357bf2019-04-15 13:13:20 -0700251 unsigned long flags;
Christoph Hellwig0829c362005-09-02 16:58:49 +1000252
Christoph Hellwig598ecfb2019-10-17 13:12:15 -0700253 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);
Christoph Hellwig0829c362005-09-02 16:58:49 +1000261}
262
Brian Fosterd9252d52019-02-01 09:14:23 -0800263/*
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(
Christoph Hellwig598ecfb2019-10-17 13:12:15 -0700269 struct iomap_writepage_ctx *wpc,
Brian Fosterd9252d52019-02-01 09:14:23 -0800270 struct xfs_inode *ip,
Christoph Hellwig4e087a32019-10-17 13:12:06 -0700271 loff_t offset)
Brian Fosterd9252d52019-02-01 09:14:23 -0800272{
Christoph Hellwig4e087a32019-10-17 13:12:06 -0700273 if (offset < wpc->iomap.offset ||
274 offset >= wpc->iomap.offset + wpc->iomap.length)
Brian Fosterd9252d52019-02-01 09:14:23 -0800275 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 */
Christoph Hellwig760fea82019-10-17 13:12:10 -0700281 if (wpc->iomap.flags & IOMAP_F_SHARED)
Brian Fosterd9252d52019-02-01 09:14:23 -0800282 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 */
Christoph Hellwig598ecfb2019-10-17 13:12:15 -0700291 if (XFS_WPC(wpc)->data_seq != READ_ONCE(ip->i_df.if_seq))
Brian Fosterd9252d52019-02-01 09:14:23 -0800292 return false;
293 if (xfs_inode_has_cow_data(ip) &&
Christoph Hellwig598ecfb2019-10-17 13:12:15 -0700294 XFS_WPC(wpc)->cow_seq != READ_ONCE(ip->i_cowfp->if_seq))
Brian Fosterd9252d52019-02-01 09:14:23 -0800295 return false;
296 return true;
297}
298
Christoph Hellwig4ad765e2019-02-15 08:02:49 -0800299/*
300 * Pass in a dellalloc extent and convert it to real extents, return the real
Christoph Hellwig4e087a32019-10-17 13:12:06 -0700301 * extent that maps offset_fsb in wpc->iomap.
Christoph Hellwig4ad765e2019-02-15 08:02:49 -0800302 *
303 * The current page is held locked so nothing could have removed the block
Christoph Hellwig7588cbe2019-02-15 08:02:50 -0800304 * backing offset_fsb, although it could have moved from the COW to the data
305 * fork by another thread.
Christoph Hellwig4ad765e2019-02-15 08:02:49 -0800306 */
307static int
308xfs_convert_blocks(
Christoph Hellwig598ecfb2019-10-17 13:12:15 -0700309 struct iomap_writepage_ctx *wpc,
Christoph Hellwig4ad765e2019-02-15 08:02:49 -0800310 struct xfs_inode *ip,
Christoph Hellwig760fea82019-10-17 13:12:10 -0700311 int whichfork,
Christoph Hellwig4e087a32019-10-17 13:12:06 -0700312 loff_t offset)
Christoph Hellwig4ad765e2019-02-15 08:02:49 -0800313{
314 int error;
Christoph Hellwig598ecfb2019-10-17 13:12:15 -0700315 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;
Christoph Hellwig4ad765e2019-02-15 08:02:49 -0800321
322 /*
Christoph Hellwig4e087a32019-10-17 13:12:06 -0700323 * 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.
Christoph Hellwig4ad765e2019-02-15 08:02:49 -0800327 */
328 do {
Christoph Hellwig760fea82019-10-17 13:12:10 -0700329 error = xfs_bmapi_convert_delalloc(ip, whichfork, offset,
Christoph Hellwig598ecfb2019-10-17 13:12:15 -0700330 &wpc->iomap, seq);
Christoph Hellwig4ad765e2019-02-15 08:02:49 -0800331 if (error)
332 return error;
Christoph Hellwig4e087a32019-10-17 13:12:06 -0700333 } while (wpc->iomap.offset + wpc->iomap.length <= offset);
Christoph Hellwig4ad765e2019-02-15 08:02:49 -0800334
335 return 0;
336}
337
Christoph Hellwig598ecfb2019-10-17 13:12:15 -0700338static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339xfs_map_blocks(
Christoph Hellwig598ecfb2019-10-17 13:12:15 -0700340 struct iomap_writepage_ctx *wpc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 struct inode *inode,
Christoph Hellwig5c665e52018-07-11 22:25:59 -0700342 loff_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343{
Christoph Hellwiga206c812010-12-10 08:42:20 +0000344 struct xfs_inode *ip = XFS_I(inode);
345 struct xfs_mount *mp = ip->i_mount;
Fabian Frederick93407472017-02-27 14:28:32 -0800346 ssize_t count = i_blocksize(inode);
Christoph Hellwigb4e29032019-02-15 08:02:47 -0800347 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
348 xfs_fileoff_t end_fsb = XFS_B_TO_FSB(mp, offset + count);
Christoph Hellwige666aa32018-07-17 16:51:52 -0700349 xfs_fileoff_t cow_fsb = NULLFILEOFF;
Christoph Hellwig760fea82019-10-17 13:12:10 -0700350 int whichfork = XFS_DATA_FORK;
Christoph Hellwig5c665e52018-07-11 22:25:59 -0700351 struct xfs_bmbt_irec imap;
Christoph Hellwig060d4ea2018-07-11 22:26:01 -0700352 struct xfs_iext_cursor icur;
Christoph Hellwig7588cbe2019-02-15 08:02:50 -0800353 int retries = 0;
Christoph Hellwiga206c812010-12-10 08:42:20 +0000354 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Brian Fosterd9252d52019-02-01 09:14:23 -0800356 if (XFS_FORCED_SHUTDOWN(mp))
357 return -EIO;
358
Christoph Hellwig889c65b2018-07-11 22:26:02 -0700359 /*
Christoph Hellwig889c65b2018-07-11 22:26:02 -0700360 * 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
Christoph Hellwige666aa32018-07-17 16:51:52 -0700363 * 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.
Christoph Hellwig889c65b2018-07-11 22:26:02 -0700373 */
Christoph Hellwig4e087a32019-10-17 13:12:06 -0700374 if (xfs_imap_valid(wpc, ip, offset))
Christoph Hellwig889c65b2018-07-11 22:26:02 -0700375 return 0;
376
Christoph Hellwig889c65b2018-07-11 22:26:02 -0700377 /*
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 */
Christoph Hellwig7588cbe2019-02-15 08:02:50 -0800383retry:
Dave Chinner988ef922016-02-15 17:20:50 +1100384 xfs_ilock(ip, XFS_ILOCK_SHARED);
Christoph Hellwigf7e67b22020-05-18 10:28:05 -0700385 ASSERT(ip->i_df.if_format != XFS_DINODE_FMT_BTREE ||
Christoph Hellwig8ff29572010-12-10 08:42:21 +0000386 (ip->i_df.if_flags & XFS_IFEXTENTS));
Christoph Hellwig060d4ea2018-07-11 22:26:01 -0700387
388 /*
389 * Check if this is offset is covered by a COW extents, and if yes use
390 * it directly instead of looking up anything in the data fork.
391 */
Christoph Hellwig51d62692018-07-17 16:51:51 -0700392 if (xfs_inode_has_cow_data(ip) &&
Christoph Hellwige666aa32018-07-17 16:51:52 -0700393 xfs_iext_lookup_extent(ip, ip->i_cowfp, offset_fsb, &icur, &imap))
394 cow_fsb = imap.br_startoff;
395 if (cow_fsb != NULLFILEOFF && cow_fsb <= offset_fsb) {
Christoph Hellwig598ecfb2019-10-17 13:12:15 -0700396 XFS_WPC(wpc)->cow_seq = READ_ONCE(ip->i_cowfp->if_seq);
Christoph Hellwig5c665e52018-07-11 22:25:59 -0700397 xfs_iunlock(ip, XFS_ILOCK_SHARED);
Christoph Hellwigbe225fe2019-02-15 08:02:46 -0800398
Christoph Hellwig760fea82019-10-17 13:12:10 -0700399 whichfork = XFS_COW_FORK;
Christoph Hellwig5c665e52018-07-11 22:25:59 -0700400 goto allocate_blocks;
401 }
402
403 /*
Brian Fosterd9252d52019-02-01 09:14:23 -0800404 * No COW extent overlap. Revalidate now that we may have updated
405 * ->cow_seq. If the data mapping is still valid, we're done.
Christoph Hellwig5c665e52018-07-11 22:25:59 -0700406 */
Christoph Hellwig4e087a32019-10-17 13:12:06 -0700407 if (xfs_imap_valid(wpc, ip, offset)) {
Christoph Hellwig5c665e52018-07-11 22:25:59 -0700408 xfs_iunlock(ip, XFS_ILOCK_SHARED);
409 return 0;
410 }
411
412 /*
413 * If we don't have a valid map, now it's time to get a new one for this
414 * offset. This will convert delayed allocations (including COW ones)
415 * into real extents.
416 */
Christoph Hellwig33457462018-07-11 22:26:02 -0700417 if (!xfs_iext_lookup_extent(ip, &ip->i_df, offset_fsb, &icur, &imap))
418 imap.br_startoff = end_fsb; /* fake a hole past EOF */
Christoph Hellwig598ecfb2019-10-17 13:12:15 -0700419 XFS_WPC(wpc)->data_seq = READ_ONCE(ip->i_df.if_seq);
Christoph Hellwig8ff29572010-12-10 08:42:21 +0000420 xfs_iunlock(ip, XFS_ILOCK_SHARED);
Christoph Hellwiga206c812010-12-10 08:42:20 +0000421
Christoph Hellwig12df89f2019-02-18 09:38:47 -0800422 /* landed in a hole or beyond EOF? */
Christoph Hellwig33457462018-07-11 22:26:02 -0700423 if (imap.br_startoff > offset_fsb) {
Christoph Hellwig33457462018-07-11 22:26:02 -0700424 imap.br_blockcount = imap.br_startoff - offset_fsb;
Christoph Hellwig5c665e52018-07-11 22:25:59 -0700425 imap.br_startoff = offset_fsb;
Christoph Hellwig5c665e52018-07-11 22:25:59 -0700426 imap.br_startblock = HOLESTARTBLOCK;
Christoph Hellwigbe225fe2019-02-15 08:02:46 -0800427 imap.br_state = XFS_EXT_NORM;
Christoph Hellwiga206c812010-12-10 08:42:20 +0000428 }
429
Christoph Hellwig12df89f2019-02-18 09:38:47 -0800430 /*
431 * Truncate to the next COW extent if there is one. This is the only
432 * opportunity to do this because we can skip COW fork lookups for the
433 * subsequent blocks in the mapping; however, the requirement to treat
434 * the COW range separately remains.
435 */
436 if (cow_fsb != NULLFILEOFF &&
437 cow_fsb < imap.br_startoff + imap.br_blockcount)
438 imap.br_blockcount = cow_fsb - imap.br_startoff;
439
440 /* got a delalloc extent? */
441 if (imap.br_startblock != HOLESTARTBLOCK &&
442 isnullstartblock(imap.br_startblock))
443 goto allocate_blocks;
444
Christoph Hellwig4e087a32019-10-17 13:12:06 -0700445 xfs_bmbt_to_iomap(ip, &wpc->iomap, &imap, 0);
Christoph Hellwig760fea82019-10-17 13:12:10 -0700446 trace_xfs_map_blocks_found(ip, offset, count, whichfork, &imap);
Christoph Hellwig5c665e52018-07-11 22:25:59 -0700447 return 0;
448allocate_blocks:
Christoph Hellwig760fea82019-10-17 13:12:10 -0700449 error = xfs_convert_blocks(wpc, ip, whichfork, offset);
Christoph Hellwig7588cbe2019-02-15 08:02:50 -0800450 if (error) {
451 /*
452 * If we failed to find the extent in the COW fork we might have
453 * raced with a COW to data fork conversion or truncate.
454 * Restart the lookup to catch the extent in the data fork for
455 * the former case, but prevent additional retries to avoid
456 * looping forever for the latter case.
457 */
Christoph Hellwig760fea82019-10-17 13:12:10 -0700458 if (error == -EAGAIN && whichfork == XFS_COW_FORK && !retries++)
Christoph Hellwig7588cbe2019-02-15 08:02:50 -0800459 goto retry;
460 ASSERT(error != -EAGAIN);
Christoph Hellwig5c665e52018-07-11 22:25:59 -0700461 return error;
Christoph Hellwig7588cbe2019-02-15 08:02:50 -0800462 }
Christoph Hellwig4ad765e2019-02-15 08:02:49 -0800463
464 /*
465 * Due to merging the return real extent might be larger than the
466 * original delalloc one. Trim the return extent to the next COW
467 * boundary again to force a re-lookup.
468 */
Christoph Hellwig760fea82019-10-17 13:12:10 -0700469 if (whichfork != XFS_COW_FORK && cow_fsb != NULLFILEOFF) {
Christoph Hellwig4e087a32019-10-17 13:12:06 -0700470 loff_t cow_offset = XFS_FSB_TO_B(mp, cow_fsb);
Christoph Hellwig4ad765e2019-02-15 08:02:49 -0800471
Christoph Hellwig4e087a32019-10-17 13:12:06 -0700472 if (cow_offset < wpc->iomap.offset + wpc->iomap.length)
473 wpc->iomap.length = cow_offset - wpc->iomap.offset;
474 }
475
476 ASSERT(wpc->iomap.offset <= offset);
477 ASSERT(wpc->iomap.offset + wpc->iomap.length > offset);
Christoph Hellwig760fea82019-10-17 13:12:10 -0700478 trace_xfs_map_blocks_alloc(ip, offset, count, whichfork, &imap);
Christoph Hellwig8ff29572010-12-10 08:42:21 +0000479 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480}
481
Christoph Hellwig598ecfb2019-10-17 13:12:15 -0700482static int
483xfs_prepare_ioend(
484 struct iomap_ioend *ioend,
Dave Chinnere10de372016-02-15 17:23:12 +1100485 int status)
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100486{
Christoph Hellwig73d30d42019-06-28 19:31:38 -0700487 unsigned int nofs_flag;
488
489 /*
490 * We can allocate memory here while doing writeback on behalf of
491 * memory reclaim. To avoid memory allocation deadlocks set the
492 * task-wide nofs context for the following operations.
493 */
494 nofs_flag = memalloc_nofs_save();
495
Darrick J. Wong5eda4302017-02-02 15:14:02 -0800496 /* Convert CoW extents to regular */
Christoph Hellwig760fea82019-10-17 13:12:10 -0700497 if (!status && (ioend->io_flags & IOMAP_F_SHARED)) {
Darrick J. Wong5eda4302017-02-02 15:14:02 -0800498 status = xfs_reflink_convert_cow(XFS_I(ioend->io_inode),
499 ioend->io_offset, ioend->io_size);
500 }
501
Dave Chinnere10de372016-02-15 17:23:12 +1100502 /* Reserve log space if we might write beyond the on-disk inode size. */
503 if (!status &&
Christoph Hellwig760fea82019-10-17 13:12:10 -0700504 ((ioend->io_flags & IOMAP_F_SHARED) ||
Christoph Hellwig4e087a32019-10-17 13:12:06 -0700505 ioend->io_type != IOMAP_UNWRITTEN) &&
Dave Chinnerbb187822016-04-06 08:11:25 +1000506 xfs_ioend_is_append(ioend) &&
Christoph Hellwig56530172019-10-17 13:12:09 -0700507 !ioend->io_private)
Dave Chinnere10de372016-02-15 17:23:12 +1100508 status = xfs_setfilesize_trans_alloc(ioend);
Dave Chinnerbb187822016-04-06 08:11:25 +1000509
Christoph Hellwig73d30d42019-06-28 19:31:38 -0700510 memalloc_nofs_restore(nofs_flag);
511
Christoph Hellwig598ecfb2019-10-17 13:12:15 -0700512 if (xfs_ioend_needs_workqueue(ioend))
513 ioend->io_bio->bi_end_io = xfs_end_bio;
514 return status;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100515}
516
Dave Chinner3ed3a432010-03-05 02:00:42 +0000517/*
Christoph Hellwig82cb1412018-07-11 22:26:05 -0700518 * If the page has delalloc blocks on it, we need to punch them out before we
519 * invalidate the page. If we don't, we leave a stale delalloc mapping on the
520 * inode that can trip up a later direct I/O read operation on the same region.
Dave Chinner3ed3a432010-03-05 02:00:42 +0000521 *
Christoph Hellwig82cb1412018-07-11 22:26:05 -0700522 * We prevent this by truncating away the delalloc regions on the page. Because
523 * they are delalloc, we can do this without needing a transaction. Indeed - if
524 * we get ENOSPC errors, we have to be able to do this truncation without a
525 * transaction as there is no space left for block reservation (typically why we
526 * see a ENOSPC in writeback).
Dave Chinner3ed3a432010-03-05 02:00:42 +0000527 */
Christoph Hellwig598ecfb2019-10-17 13:12:15 -0700528static void
529xfs_discard_page(
Dave Chinner3ed3a432010-03-05 02:00:42 +0000530 struct page *page)
531{
532 struct inode *inode = page->mapping->host;
533 struct xfs_inode *ip = XFS_I(inode);
Christoph Hellwig03625722018-07-11 22:25:57 -0700534 struct xfs_mount *mp = ip->i_mount;
Dave Chinner3ed3a432010-03-05 02:00:42 +0000535 loff_t offset = page_offset(page);
Christoph Hellwig03625722018-07-11 22:25:57 -0700536 xfs_fileoff_t start_fsb = XFS_B_TO_FSBT(mp, offset);
537 int error;
Dave Chinner3ed3a432010-03-05 02:00:42 +0000538
Christoph Hellwig03625722018-07-11 22:25:57 -0700539 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner3ed3a432010-03-05 02:00:42 +0000540 goto out_invalidate;
541
Christoph Hellwig4ab45e22020-02-21 07:34:48 -0800542 xfs_alert_ratelimited(mp,
Darrick J. Wongc9690042018-01-09 12:02:55 -0800543 "page discard on page "PTR_FMT", inode 0x%llx, offset %llu.",
Dave Chinner3ed3a432010-03-05 02:00:42 +0000544 page, ip->i_ino, offset);
545
Christoph Hellwig03625722018-07-11 22:25:57 -0700546 error = xfs_bmap_punch_delalloc_range(ip, start_fsb,
547 PAGE_SIZE / i_blocksize(inode));
Christoph Hellwig03625722018-07-11 22:25:57 -0700548 if (error && !XFS_FORCED_SHUTDOWN(mp))
549 xfs_alert(mp, "page discard unable to remove delalloc mapping.");
Dave Chinner3ed3a432010-03-05 02:00:42 +0000550out_invalidate:
Christoph Hellwig9e91c572019-10-17 13:12:13 -0700551 iomap_invalidatepage(page, 0, PAGE_SIZE);
Dave Chinner3ed3a432010-03-05 02:00:42 +0000552}
553
Christoph Hellwig598ecfb2019-10-17 13:12:15 -0700554static const struct iomap_writeback_ops xfs_writeback_ops = {
555 .map_blocks = xfs_map_blocks,
556 .prepare_ioend = xfs_prepare_ioend,
557 .discard_page = xfs_discard_page,
558};
Nathan Scottf51623b2006-03-14 13:26:27 +1100559
Nathan Scott7d4fb402006-06-09 15:27:16 +1000560STATIC int
Dave Chinnerfbcc0252016-02-15 17:21:19 +1100561xfs_vm_writepage(
562 struct page *page,
563 struct writeback_control *wbc)
564{
Christoph Hellwigbe225fe2019-02-15 08:02:46 -0800565 struct xfs_writepage_ctx wpc = { };
Dave Chinnerfbcc0252016-02-15 17:21:19 +1100566
Christoph Hellwig598ecfb2019-10-17 13:12:15 -0700567 return iomap_writepage(page, wbc, &wpc.ctx, &xfs_writeback_ops);
Dave Chinnerfbcc0252016-02-15 17:21:19 +1100568}
569
570STATIC int
Nathan Scott7d4fb402006-06-09 15:27:16 +1000571xfs_vm_writepages(
572 struct address_space *mapping,
573 struct writeback_control *wbc)
574{
Christoph Hellwigbe225fe2019-02-15 08:02:46 -0800575 struct xfs_writepage_ctx wpc = { };
Dave Chinnerfbcc0252016-02-15 17:21:19 +1100576
Christoph Hellwigb3aea4e2007-08-29 11:44:37 +1000577 xfs_iflags_clear(XFS_I(mapping->host), XFS_ITRUNCATED);
Christoph Hellwig598ecfb2019-10-17 13:12:15 -0700578 return iomap_writepages(mapping, wbc, &wpc.ctx, &xfs_writeback_ops);
Nathan Scott7d4fb402006-06-09 15:27:16 +1000579}
580
Dan Williams6e2608d2018-03-07 15:26:44 -0800581STATIC int
582xfs_dax_writepages(
583 struct address_space *mapping,
584 struct writeback_control *wbc)
585{
Christoph Hellwig30fa5292019-10-24 22:25:38 -0700586 struct xfs_inode *ip = XFS_I(mapping->host);
587
588 xfs_iflags_clear(ip, XFS_ITRUNCATED);
Dan Williams6e2608d2018-03-07 15:26:44 -0800589 return dax_writeback_mapping_range(mapping,
Vivek Goyal3f666c52020-01-03 13:33:07 -0500590 xfs_inode_buftarg(ip)->bt_daxdev, wbc);
Dan Williams6e2608d2018-03-07 15:26:44 -0800591}
592
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593STATIC sector_t
Nathan Scotte4c573b2006-03-14 13:54:26 +1100594xfs_vm_bmap(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 struct address_space *mapping,
596 sector_t block)
597{
Christoph Hellwigb84e7722018-06-01 09:03:09 -0700598 struct xfs_inode *ip = XFS_I(mapping->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Christoph Hellwigb84e7722018-06-01 09:03:09 -0700600 trace_xfs_vm_bmap(ip);
Darrick J. Wongdb1327b2016-10-03 09:11:36 -0700601
602 /*
603 * The swap code (ab-)uses ->bmap to get a block mapping and then
Ingo Molnar793057e2018-02-28 09:39:48 +0100604 * bypasses the file system for actual I/O. We really can't allow
Darrick J. Wongdb1327b2016-10-03 09:11:36 -0700605 * that on reflinks inodes, so we have to skip out here. And yes,
Darrick J. Wongeb5e2482017-06-21 20:27:35 -0700606 * 0 is the magic code for a bmap error.
607 *
608 * Since we don't pass back blockdev info, we can't return bmap
609 * information for rt files either.
Darrick J. Wongdb1327b2016-10-03 09:11:36 -0700610 */
Christoph Hellwig66ae56a2019-02-18 09:38:49 -0800611 if (xfs_is_cow_inode(ip) || XFS_IS_REALTIME_INODE(ip))
Darrick J. Wongdb1327b2016-10-03 09:11:36 -0700612 return 0;
Christoph Hellwig690c2a32019-10-19 09:09:45 -0700613 return iomap_bmap(mapping, block, &xfs_read_iomap_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614}
615
616STATIC int
Nathan Scotte4c573b2006-03-14 13:54:26 +1100617xfs_vm_readpage(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 struct file *unused,
619 struct page *page)
620{
Christoph Hellwig690c2a32019-10-19 09:09:45 -0700621 return iomap_readpage(page, &xfs_read_iomap_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622}
623
Matthew Wilcox (Oracle)9d24a132020-06-01 21:47:34 -0700624STATIC void
625xfs_vm_readahead(
626 struct readahead_control *rac)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627{
Matthew Wilcox (Oracle)9d24a132020-06-01 21:47:34 -0700628 iomap_readahead(rac, &xfs_read_iomap_ops);
Dave Chinner22e757a2014-09-02 12:12:51 +1000629}
630
Darrick J. Wong67482122018-05-10 08:38:15 -0700631static int
632xfs_iomap_swapfile_activate(
633 struct swap_info_struct *sis,
634 struct file *swap_file,
635 sector_t *span)
636{
Christoph Hellwig30fa5292019-10-24 22:25:38 -0700637 sis->bdev = xfs_inode_buftarg(XFS_I(file_inode(swap_file)))->bt_bdev;
Christoph Hellwig690c2a32019-10-19 09:09:45 -0700638 return iomap_swapfile_activate(sis, swap_file, span,
639 &xfs_read_iomap_ops);
Darrick J. Wong67482122018-05-10 08:38:15 -0700640}
641
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700642const struct address_space_operations xfs_address_space_operations = {
Nathan Scotte4c573b2006-03-14 13:54:26 +1100643 .readpage = xfs_vm_readpage,
Matthew Wilcox (Oracle)9d24a132020-06-01 21:47:34 -0700644 .readahead = xfs_vm_readahead,
Nathan Scotte4c573b2006-03-14 13:54:26 +1100645 .writepage = xfs_vm_writepage,
Nathan Scott7d4fb402006-06-09 15:27:16 +1000646 .writepages = xfs_vm_writepages,
Christoph Hellwig82cb1412018-07-11 22:26:05 -0700647 .set_page_dirty = iomap_set_page_dirty,
Christoph Hellwig9e91c572019-10-17 13:12:13 -0700648 .releasepage = iomap_releasepage,
649 .invalidatepage = iomap_invalidatepage,
Nathan Scotte4c573b2006-03-14 13:54:26 +1100650 .bmap = xfs_vm_bmap,
Dan Williams6e2608d2018-03-07 15:26:44 -0800651 .direct_IO = noop_direct_IO,
Christoph Hellwig82cb1412018-07-11 22:26:05 -0700652 .migratepage = iomap_migrate_page,
653 .is_partially_uptodate = iomap_is_partially_uptodate,
Andi Kleenaa261f52009-09-16 11:50:16 +0200654 .error_remove_page = generic_error_remove_page,
Darrick J. Wong67482122018-05-10 08:38:15 -0700655 .swap_activate = xfs_iomap_swapfile_activate,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656};
Dan Williams6e2608d2018-03-07 15:26:44 -0800657
658const struct address_space_operations xfs_dax_aops = {
659 .writepages = xfs_dax_writepages,
660 .direct_IO = noop_direct_IO,
661 .set_page_dirty = noop_set_page_dirty,
662 .invalidatepage = noop_invalidatepage,
Darrick J. Wong67482122018-05-10 08:38:15 -0700663 .swap_activate = xfs_iomap_swapfile_activate,
Dan Williams6e2608d2018-03-07 15:26:44 -0800664};