blob: f16d5f196c6b15cfb7cd84807984a76cf10b52dd [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 +110021/*
22 * structure owned by writepages passed to individual writepage calls
23 */
24struct xfs_writepage_ctx {
25 struct xfs_bmbt_irec imap;
Christoph Hellwigbe225fe2019-02-15 08:02:46 -080026 int fork;
Brian Fosterd9252d52019-02-01 09:14:23 -080027 unsigned int data_seq;
Christoph Hellwige666aa32018-07-17 16:51:52 -070028 unsigned int cow_seq;
Dave Chinnerfbcc0252016-02-15 17:21:19 +110029 struct xfs_ioend *ioend;
Dave Chinnerfbcc0252016-02-15 17:21:19 +110030};
31
Ross Zwisler20a90f52016-02-26 15:19:52 -080032struct block_device *
Christoph Hellwig6214ed42007-09-14 15:23:17 +100033xfs_find_bdev_for_inode(
Christoph Hellwig046f1682010-04-28 12:28:52 +000034 struct inode *inode)
Christoph Hellwig6214ed42007-09-14 15:23:17 +100035{
Christoph Hellwig046f1682010-04-28 12:28:52 +000036 struct xfs_inode *ip = XFS_I(inode);
Christoph Hellwig6214ed42007-09-14 15:23:17 +100037 struct xfs_mount *mp = ip->i_mount;
38
Eric Sandeen71ddabb2007-11-23 16:29:42 +110039 if (XFS_IS_REALTIME_INODE(ip))
Christoph Hellwig6214ed42007-09-14 15:23:17 +100040 return mp->m_rtdev_targp->bt_bdev;
41 else
42 return mp->m_ddev_targp->bt_bdev;
43}
44
Dan Williams486aff52017-08-24 15:12:50 -070045struct dax_device *
46xfs_find_daxdev_for_inode(
47 struct inode *inode)
48{
49 struct xfs_inode *ip = XFS_I(inode);
50 struct xfs_mount *mp = ip->i_mount;
51
52 if (XFS_IS_REALTIME_INODE(ip))
53 return mp->m_rtdev_targp->bt_daxdev;
54 else
55 return mp->m_ddev_targp->bt_daxdev;
56}
57
Christoph Hellwigac8ee542018-07-11 22:26:04 -070058static void
59xfs_finish_page_writeback(
60 struct inode *inode,
Ming Lei6dc4f102019-02-15 19:13:19 +080061 struct bio_vec *bvec,
Christoph Hellwigac8ee542018-07-11 22:26:04 -070062 int error)
63{
Christoph Hellwig82cb1412018-07-11 22:26:05 -070064 struct iomap_page *iop = to_iomap_page(bvec->bv_page);
65
Christoph Hellwigac8ee542018-07-11 22:26:04 -070066 if (error) {
67 SetPageError(bvec->bv_page);
68 mapping_set_error(inode->i_mapping, -EIO);
69 }
Christoph Hellwigac8ee542018-07-11 22:26:04 -070070
Christoph Hellwig82cb1412018-07-11 22:26:05 -070071 ASSERT(iop || i_blocksize(inode) == PAGE_SIZE);
72 ASSERT(!iop || atomic_read(&iop->write_count) > 0);
Dave Chinner37992c12016-04-06 08:12:28 +100073
Christoph Hellwig82cb1412018-07-11 22:26:05 -070074 if (!iop || atomic_dec_and_test(&iop->write_count))
Christoph Hellwig8353a812017-09-02 09:53:41 -070075 end_page_writeback(bvec->bv_page);
Dave Chinner37992c12016-04-06 08:12:28 +100076}
77
78/*
79 * We're now finished for good with this ioend structure. Update the page
80 * state, release holds on bios, and finally free up memory. Do not use the
81 * ioend after this.
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +110082 */
Christoph Hellwig0829c362005-09-02 16:58:49 +100083STATIC void
84xfs_destroy_ioend(
Christoph Hellwig0e51a8e2016-04-06 08:34:30 +100085 struct xfs_ioend *ioend,
86 int error)
Christoph Hellwig0829c362005-09-02 16:58:49 +100087{
Dave Chinner37992c12016-04-06 08:12:28 +100088 struct inode *inode = ioend->io_inode;
Christoph Hellwig8353a812017-09-02 09:53:41 -070089 struct bio *bio = &ioend->io_inline_bio;
90 struct bio *last = ioend->io_bio, *next;
91 u64 start = bio->bi_iter.bi_sector;
92 bool quiet = bio_flagged(bio, BIO_QUIET);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +110093
Christoph Hellwig0e51a8e2016-04-06 08:34:30 +100094 for (bio = &ioend->io_inline_bio; bio; bio = next) {
Dave Chinner37992c12016-04-06 08:12:28 +100095 struct bio_vec *bvec;
Ming Lei6dc4f102019-02-15 19:13:19 +080096 struct bvec_iter_all iter_all;
Dave Chinner37992c12016-04-06 08:12:28 +100097
Christoph Hellwig0e51a8e2016-04-06 08:34:30 +100098 /*
99 * For the last bio, bi_private points to the ioend, so we
100 * need to explicitly end the iteration here.
101 */
102 if (bio == last)
103 next = NULL;
104 else
105 next = bio->bi_private;
Dave Chinner37992c12016-04-06 08:12:28 +1000106
107 /* walk each page on bio, ending page IO on them */
Christoph Hellwig2b070cf2019-04-25 09:03:00 +0200108 bio_for_each_segment_all(bvec, bio, iter_all)
Christoph Hellwig82cb1412018-07-11 22:26:05 -0700109 xfs_finish_page_writeback(inode, bvec, error);
Dave Chinner37992c12016-04-06 08:12:28 +1000110 bio_put(bio);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100111 }
Christoph Hellwig8353a812017-09-02 09:53:41 -0700112
113 if (unlikely(error && !quiet)) {
114 xfs_err_ratelimited(XFS_I(inode)->i_mount,
115 "writeback error on sector %llu", start);
116 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117}
118
119/*
Christoph Hellwigfc0063c2011-08-23 08:28:11 +0000120 * Fast and loose check if this write could update the on-disk inode size.
121 */
122static inline bool xfs_ioend_is_append(struct xfs_ioend *ioend)
123{
124 return ioend->io_offset + ioend->io_size >
125 XFS_I(ioend->io_inode)->i_d.di_size;
126}
127
Christoph Hellwig281627d2012-03-13 08:41:05 +0000128STATIC int
129xfs_setfilesize_trans_alloc(
130 struct xfs_ioend *ioend)
131{
132 struct xfs_mount *mp = XFS_I(ioend->io_inode)->i_mount;
133 struct xfs_trans *tp;
134 int error;
135
Christoph Hellwig73d30d42019-06-28 19:31:38 -0700136 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_fsyncts, 0, 0, 0, &tp);
Christoph Hellwig253f4912016-04-06 09:19:55 +1000137 if (error)
Christoph Hellwig281627d2012-03-13 08:41:05 +0000138 return error;
Christoph Hellwig281627d2012-03-13 08:41:05 +0000139
140 ioend->io_append_trans = tp;
141
142 /*
Dave Chinner437a2552012-11-28 13:01:00 +1100143 * We may pass freeze protection with a transaction. So tell lockdep
Jan Karad9457dc2012-06-12 16:20:39 +0200144 * we released it.
145 */
Oleg Nesterovbee91822015-07-19 23:48:20 +0200146 __sb_writers_release(ioend->io_inode->i_sb, SB_FREEZE_FS);
Jan Karad9457dc2012-06-12 16:20:39 +0200147 /*
Christoph Hellwig281627d2012-03-13 08:41:05 +0000148 * We hand off the transaction to the completion thread now, so
149 * clear the flag here.
150 */
Michal Hocko90707332017-05-03 14:53:12 -0700151 current_restore_flags_nested(&tp->t_pflags, PF_MEMALLOC_NOFS);
Christoph Hellwig281627d2012-03-13 08:41:05 +0000152 return 0;
153}
154
Christoph Hellwigfc0063c2011-08-23 08:28:11 +0000155/*
Christoph Hellwig2813d682011-12-18 20:00:12 +0000156 * Update on-disk file size now that data has been written to disk.
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000157 */
Christoph Hellwig281627d2012-03-13 08:41:05 +0000158STATIC int
Christoph Hellwige3728432016-09-19 11:26:41 +1000159__xfs_setfilesize(
Christoph Hellwig2ba66232015-02-02 10:02:09 +1100160 struct xfs_inode *ip,
161 struct xfs_trans *tp,
162 xfs_off_t offset,
163 size_t size)
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000164{
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000165 xfs_fsize_t isize;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000166
Christoph Hellwigaa6bf012012-02-29 09:53:48 +0000167 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig2ba66232015-02-02 10:02:09 +1100168 isize = xfs_new_eof(ip, offset + size);
Christoph Hellwig281627d2012-03-13 08:41:05 +0000169 if (!isize) {
170 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig4906e212015-06-04 13:47:56 +1000171 xfs_trans_cancel(tp);
Christoph Hellwig281627d2012-03-13 08:41:05 +0000172 return 0;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000173 }
174
Christoph Hellwig2ba66232015-02-02 10:02:09 +1100175 trace_xfs_setfilesize(ip, offset, size);
Christoph Hellwig281627d2012-03-13 08:41:05 +0000176
177 ip->i_d.di_size = isize;
178 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
179 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
180
Christoph Hellwig70393312015-06-04 13:48:08 +1000181 return xfs_trans_commit(tp);
Christoph Hellwig0829c362005-09-02 16:58:49 +1000182}
183
Christoph Hellwige3728432016-09-19 11:26:41 +1000184int
185xfs_setfilesize(
186 struct xfs_inode *ip,
187 xfs_off_t offset,
188 size_t size)
189{
190 struct xfs_mount *mp = ip->i_mount;
191 struct xfs_trans *tp;
192 int error;
193
194 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_fsyncts, 0, 0, 0, &tp);
195 if (error)
196 return error;
197
198 return __xfs_setfilesize(ip, tp, offset, size);
199}
200
Christoph Hellwig2ba66232015-02-02 10:02:09 +1100201STATIC int
202xfs_setfilesize_ioend(
Christoph Hellwig0e51a8e2016-04-06 08:34:30 +1000203 struct xfs_ioend *ioend,
204 int error)
Christoph Hellwig2ba66232015-02-02 10:02:09 +1100205{
206 struct xfs_inode *ip = XFS_I(ioend->io_inode);
207 struct xfs_trans *tp = ioend->io_append_trans;
208
209 /*
210 * The transaction may have been allocated in the I/O submission thread,
211 * thus we need to mark ourselves as being in a transaction manually.
212 * Similarly for freeze protection.
213 */
Michal Hocko90707332017-05-03 14:53:12 -0700214 current_set_flags_nested(&tp->t_pflags, PF_MEMALLOC_NOFS);
Oleg Nesterovbee91822015-07-19 23:48:20 +0200215 __sb_writers_acquired(VFS_I(ip)->i_sb, SB_FREEZE_FS);
Christoph Hellwig2ba66232015-02-02 10:02:09 +1100216
Zhaohongjiang5cb13dc2015-10-12 15:28:39 +1100217 /* we abort the update if there was an IO error */
Christoph Hellwig0e51a8e2016-04-06 08:34:30 +1000218 if (error) {
Zhaohongjiang5cb13dc2015-10-12 15:28:39 +1100219 xfs_trans_cancel(tp);
Christoph Hellwig0e51a8e2016-04-06 08:34:30 +1000220 return error;
Zhaohongjiang5cb13dc2015-10-12 15:28:39 +1100221 }
222
Christoph Hellwige3728432016-09-19 11:26:41 +1000223 return __xfs_setfilesize(ip, tp, ioend->io_offset, ioend->io_size);
Christoph Hellwig2ba66232015-02-02 10:02:09 +1100224}
225
Christoph Hellwig0829c362005-09-02 16:58:49 +1000226/*
Dave Chinner77d7a0c2010-02-17 05:36:29 +0000227 * IO write completion.
228 */
229STATIC void
Darrick J. Wongcb357bf2019-04-15 13:13:20 -0700230xfs_end_ioend(
231 struct xfs_ioend *ioend)
Dave Chinner77d7a0c2010-02-17 05:36:29 +0000232{
Darrick J. Wong3994fc42019-04-15 13:13:21 -0700233 struct list_head ioend_list;
Christoph Hellwig0e51a8e2016-04-06 08:34:30 +1000234 struct xfs_inode *ip = XFS_I(ioend->io_inode);
Christoph Hellwig787eb482017-03-02 15:02:51 -0800235 xfs_off_t offset = ioend->io_offset;
236 size_t size = ioend->io_size;
Christoph Hellwig73d30d42019-06-28 19:31:38 -0700237 unsigned int nofs_flag;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200238 int error;
Dave Chinner77d7a0c2010-02-17 05:36:29 +0000239
Brian Fosteraf055e32016-02-08 15:00:02 +1100240 /*
Christoph Hellwig73d30d42019-06-28 19:31:38 -0700241 * We can allocate memory here while doing writeback on behalf of
242 * memory reclaim. To avoid memory allocation deadlocks set the
243 * task-wide nofs context for the following operations.
244 */
245 nofs_flag = memalloc_nofs_save();
246
247 /*
Christoph Hellwig787eb482017-03-02 15:02:51 -0800248 * Just clean up the in-memory strutures if the fs has been shut down.
Brian Fosteraf055e32016-02-08 15:00:02 +1100249 */
Christoph Hellwig787eb482017-03-02 15:02:51 -0800250 if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
Christoph Hellwig0e51a8e2016-04-06 08:34:30 +1000251 error = -EIO;
Christoph Hellwig787eb482017-03-02 15:02:51 -0800252 goto done;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700253 }
254
255 /*
Christoph Hellwig787eb482017-03-02 15:02:51 -0800256 * Clean up any COW blocks on an I/O error.
Dave Chinner77d7a0c2010-02-17 05:36:29 +0000257 */
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200258 error = blk_status_to_errno(ioend->io_bio->bi_status);
Christoph Hellwig787eb482017-03-02 15:02:51 -0800259 if (unlikely(error)) {
Christoph Hellwigbe225fe2019-02-15 08:02:46 -0800260 if (ioend->io_fork == XFS_COW_FORK)
Christoph Hellwig787eb482017-03-02 15:02:51 -0800261 xfs_reflink_cancel_cow_range(ip, offset, size, true);
Christoph Hellwig787eb482017-03-02 15:02:51 -0800262 goto done;
263 }
264
265 /*
Christoph Hellwigbe225fe2019-02-15 08:02:46 -0800266 * Success: commit the COW or unwritten blocks if needed.
Christoph Hellwig787eb482017-03-02 15:02:51 -0800267 */
Christoph Hellwigbe225fe2019-02-15 08:02:46 -0800268 if (ioend->io_fork == XFS_COW_FORK)
Christoph Hellwig787eb482017-03-02 15:02:51 -0800269 error = xfs_reflink_end_cow(ip, offset, size);
Christoph Hellwigbe225fe2019-02-15 08:02:46 -0800270 else if (ioend->io_state == XFS_EXT_UNWRITTEN)
Eryu Guanee70daa2017-09-21 11:26:18 -0700271 error = xfs_iomap_write_unwritten(ip, offset, size, false);
Christoph Hellwigbe225fe2019-02-15 08:02:46 -0800272 else
Christoph Hellwig787eb482017-03-02 15:02:51 -0800273 ASSERT(!xfs_ioend_is_append(ioend) || ioend->io_append_trans);
Dave Chinner77d7a0c2010-02-17 05:36:29 +0000274
Christoph Hellwig04f658e2011-08-24 05:59:25 +0000275done:
Christoph Hellwig787eb482017-03-02 15:02:51 -0800276 if (ioend->io_append_trans)
277 error = xfs_setfilesize_ioend(ioend, error);
Darrick J. Wong3994fc42019-04-15 13:13:21 -0700278 list_replace_init(&ioend->io_list, &ioend_list);
Christoph Hellwig0e51a8e2016-04-06 08:34:30 +1000279 xfs_destroy_ioend(ioend, error);
Darrick J. Wong3994fc42019-04-15 13:13:21 -0700280
281 while (!list_empty(&ioend_list)) {
282 ioend = list_first_entry(&ioend_list, struct xfs_ioend,
283 io_list);
284 list_del_init(&ioend->io_list);
285 xfs_destroy_ioend(ioend, error);
286 }
Christoph Hellwig73d30d42019-06-28 19:31:38 -0700287
288 memalloc_nofs_restore(nofs_flag);
Darrick J. Wong3994fc42019-04-15 13:13:21 -0700289}
290
291/*
292 * We can merge two adjacent ioends if they have the same set of work to do.
293 */
294static bool
295xfs_ioend_can_merge(
296 struct xfs_ioend *ioend,
Darrick J. Wong3994fc42019-04-15 13:13:21 -0700297 struct xfs_ioend *next)
298{
Christoph Hellwigfe64e0d2019-06-28 19:31:37 -0700299 if (ioend->io_bio->bi_status != next->io_bio->bi_status)
Darrick J. Wong3994fc42019-04-15 13:13:21 -0700300 return false;
301 if ((ioend->io_fork == XFS_COW_FORK) ^ (next->io_fork == XFS_COW_FORK))
302 return false;
303 if ((ioend->io_state == XFS_EXT_UNWRITTEN) ^
304 (next->io_state == XFS_EXT_UNWRITTEN))
305 return false;
306 if (ioend->io_offset + ioend->io_size != next->io_offset)
307 return false;
Darrick J. Wong3994fc42019-04-15 13:13:21 -0700308 return true;
309}
310
Christoph Hellwig7dbae9f2019-06-28 19:31:37 -0700311/*
312 * If the to be merged ioend has a preallocated transaction for file
313 * size updates we need to ensure the ioend it is merged into also
314 * has one. If it already has one we can simply cancel the transaction
315 * as it is guaranteed to be clean.
316 */
317static void
318xfs_ioend_merge_append_transactions(
319 struct xfs_ioend *ioend,
320 struct xfs_ioend *next)
321{
322 if (!ioend->io_append_trans) {
323 ioend->io_append_trans = next->io_append_trans;
324 next->io_append_trans = NULL;
325 } else {
326 xfs_setfilesize_ioend(next, -ECANCELED);
327 }
328}
329
Darrick J. Wong3994fc42019-04-15 13:13:21 -0700330/* Try to merge adjacent completions. */
331STATIC void
332xfs_ioend_try_merge(
333 struct xfs_ioend *ioend,
334 struct list_head *more_ioends)
335{
336 struct xfs_ioend *next_ioend;
Darrick J. Wong3994fc42019-04-15 13:13:21 -0700337
338 while (!list_empty(more_ioends)) {
339 next_ioend = list_first_entry(more_ioends, struct xfs_ioend,
340 io_list);
Christoph Hellwigfe64e0d2019-06-28 19:31:37 -0700341 if (!xfs_ioend_can_merge(ioend, next_ioend))
Darrick J. Wong3994fc42019-04-15 13:13:21 -0700342 break;
343 list_move_tail(&next_ioend->io_list, &ioend->io_list);
344 ioend->io_size += next_ioend->io_size;
Christoph Hellwig7dbae9f2019-06-28 19:31:37 -0700345 if (next_ioend->io_append_trans)
346 xfs_ioend_merge_append_transactions(ioend, next_ioend);
Darrick J. Wong3994fc42019-04-15 13:13:21 -0700347 }
348}
349
350/* list_sort compare function for ioends */
351static int
352xfs_ioend_compare(
353 void *priv,
354 struct list_head *a,
355 struct list_head *b)
356{
357 struct xfs_ioend *ia;
358 struct xfs_ioend *ib;
359
360 ia = container_of(a, struct xfs_ioend, io_list);
361 ib = container_of(b, struct xfs_ioend, io_list);
362 if (ia->io_offset < ib->io_offset)
363 return -1;
364 else if (ia->io_offset > ib->io_offset)
365 return 1;
366 return 0;
Dave Chinner77d7a0c2010-02-17 05:36:29 +0000367}
368
Darrick J. Wongcb357bf2019-04-15 13:13:20 -0700369/* Finish all pending io completions. */
370void
371xfs_end_io(
372 struct work_struct *work)
373{
374 struct xfs_inode *ip;
375 struct xfs_ioend *ioend;
376 struct list_head completion_list;
377 unsigned long flags;
378
379 ip = container_of(work, struct xfs_inode, i_ioend_work);
380
381 spin_lock_irqsave(&ip->i_ioend_lock, flags);
382 list_replace_init(&ip->i_ioend_list, &completion_list);
383 spin_unlock_irqrestore(&ip->i_ioend_lock, flags);
384
Darrick J. Wong3994fc42019-04-15 13:13:21 -0700385 list_sort(NULL, &completion_list, xfs_ioend_compare);
386
Darrick J. Wongcb357bf2019-04-15 13:13:20 -0700387 while (!list_empty(&completion_list)) {
388 ioend = list_first_entry(&completion_list, struct xfs_ioend,
389 io_list);
390 list_del_init(&ioend->io_list);
Darrick J. Wong3994fc42019-04-15 13:13:21 -0700391 xfs_ioend_try_merge(ioend, &completion_list);
Darrick J. Wongcb357bf2019-04-15 13:13:20 -0700392 xfs_end_ioend(ioend);
393 }
394}
395
Christoph Hellwig0e51a8e2016-04-06 08:34:30 +1000396STATIC void
397xfs_end_bio(
398 struct bio *bio)
Christoph Hellwig0829c362005-09-02 16:58:49 +1000399{
Christoph Hellwig0e51a8e2016-04-06 08:34:30 +1000400 struct xfs_ioend *ioend = bio->bi_private;
Darrick J. Wongcb357bf2019-04-15 13:13:20 -0700401 struct xfs_inode *ip = XFS_I(ioend->io_inode);
402 struct xfs_mount *mp = ip->i_mount;
403 unsigned long flags;
Christoph Hellwig0829c362005-09-02 16:58:49 +1000404
Christoph Hellwigbe225fe2019-02-15 08:02:46 -0800405 if (ioend->io_fork == XFS_COW_FORK ||
Darrick J. Wongcb357bf2019-04-15 13:13:20 -0700406 ioend->io_state == XFS_EXT_UNWRITTEN ||
407 ioend->io_append_trans != NULL) {
408 spin_lock_irqsave(&ip->i_ioend_lock, flags);
409 if (list_empty(&ip->i_ioend_list))
410 WARN_ON_ONCE(!queue_work(mp->m_unwritten_workqueue,
411 &ip->i_ioend_work));
412 list_add_tail(&ioend->io_list, &ip->i_ioend_list);
413 spin_unlock_irqrestore(&ip->i_ioend_lock, flags);
414 } else
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200415 xfs_destroy_ioend(ioend, blk_status_to_errno(bio->bi_status));
Christoph Hellwig0829c362005-09-02 16:58:49 +1000416}
417
Brian Fosterd9252d52019-02-01 09:14:23 -0800418/*
419 * Fast revalidation of the cached writeback mapping. Return true if the current
420 * mapping is valid, false otherwise.
421 */
422static bool
423xfs_imap_valid(
424 struct xfs_writepage_ctx *wpc,
425 struct xfs_inode *ip,
426 xfs_fileoff_t offset_fsb)
427{
428 if (offset_fsb < wpc->imap.br_startoff ||
429 offset_fsb >= wpc->imap.br_startoff + wpc->imap.br_blockcount)
430 return false;
431 /*
432 * If this is a COW mapping, it is sufficient to check that the mapping
433 * covers the offset. Be careful to check this first because the caller
434 * can revalidate a COW mapping without updating the data seqno.
435 */
Christoph Hellwigbe225fe2019-02-15 08:02:46 -0800436 if (wpc->fork == XFS_COW_FORK)
Brian Fosterd9252d52019-02-01 09:14:23 -0800437 return true;
438
439 /*
440 * This is not a COW mapping. Check the sequence number of the data fork
441 * because concurrent changes could have invalidated the extent. Check
442 * the COW fork because concurrent changes since the last time we
443 * checked (and found nothing at this offset) could have added
444 * overlapping blocks.
445 */
446 if (wpc->data_seq != READ_ONCE(ip->i_df.if_seq))
447 return false;
448 if (xfs_inode_has_cow_data(ip) &&
449 wpc->cow_seq != READ_ONCE(ip->i_cowfp->if_seq))
450 return false;
451 return true;
452}
453
Christoph Hellwig4ad765e2019-02-15 08:02:49 -0800454/*
455 * Pass in a dellalloc extent and convert it to real extents, return the real
456 * extent that maps offset_fsb in wpc->imap.
457 *
458 * The current page is held locked so nothing could have removed the block
Christoph Hellwig7588cbe2019-02-15 08:02:50 -0800459 * backing offset_fsb, although it could have moved from the COW to the data
460 * fork by another thread.
Christoph Hellwig4ad765e2019-02-15 08:02:49 -0800461 */
462static int
463xfs_convert_blocks(
464 struct xfs_writepage_ctx *wpc,
465 struct xfs_inode *ip,
466 xfs_fileoff_t offset_fsb)
467{
468 int error;
469
470 /*
471 * Attempt to allocate whatever delalloc extent currently backs
472 * offset_fsb and put the result into wpc->imap. Allocate in a loop
473 * because it may take several attempts to allocate real blocks for a
474 * contiguous delalloc extent if free space is sufficiently fragmented.
475 */
476 do {
477 error = xfs_bmapi_convert_delalloc(ip, wpc->fork, offset_fsb,
478 &wpc->imap, wpc->fork == XFS_COW_FORK ?
479 &wpc->cow_seq : &wpc->data_seq);
480 if (error)
481 return error;
482 } while (wpc->imap.br_startoff + wpc->imap.br_blockcount <= offset_fsb);
483
484 return 0;
485}
486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487STATIC int
488xfs_map_blocks(
Christoph Hellwig5c665e52018-07-11 22:25:59 -0700489 struct xfs_writepage_ctx *wpc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 struct inode *inode,
Christoph Hellwig5c665e52018-07-11 22:25:59 -0700491 loff_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
Christoph Hellwiga206c812010-12-10 08:42:20 +0000493 struct xfs_inode *ip = XFS_I(inode);
494 struct xfs_mount *mp = ip->i_mount;
Fabian Frederick93407472017-02-27 14:28:32 -0800495 ssize_t count = i_blocksize(inode);
Christoph Hellwigb4e29032019-02-15 08:02:47 -0800496 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
497 xfs_fileoff_t end_fsb = XFS_B_TO_FSB(mp, offset + count);
Christoph Hellwige666aa32018-07-17 16:51:52 -0700498 xfs_fileoff_t cow_fsb = NULLFILEOFF;
Christoph Hellwig5c665e52018-07-11 22:25:59 -0700499 struct xfs_bmbt_irec imap;
Christoph Hellwig060d4ea2018-07-11 22:26:01 -0700500 struct xfs_iext_cursor icur;
Christoph Hellwig7588cbe2019-02-15 08:02:50 -0800501 int retries = 0;
Christoph Hellwiga206c812010-12-10 08:42:20 +0000502 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Brian Fosterd9252d52019-02-01 09:14:23 -0800504 if (XFS_FORCED_SHUTDOWN(mp))
505 return -EIO;
506
Christoph Hellwig889c65b2018-07-11 22:26:02 -0700507 /*
Christoph Hellwig889c65b2018-07-11 22:26:02 -0700508 * COW fork blocks can overlap data fork blocks even if the blocks
509 * aren't shared. COW I/O always takes precedent, so we must always
510 * check for overlap on reflink inodes unless the mapping is already a
Christoph Hellwige666aa32018-07-17 16:51:52 -0700511 * COW one, or the COW fork hasn't changed from the last time we looked
512 * at it.
513 *
514 * It's safe to check the COW fork if_seq here without the ILOCK because
515 * we've indirectly protected against concurrent updates: writeback has
516 * the page locked, which prevents concurrent invalidations by reflink
517 * and directio and prevents concurrent buffered writes to the same
518 * page. Changes to if_seq always happen under i_lock, which protects
519 * against concurrent updates and provides a memory barrier on the way
520 * out that ensures that we always see the current value.
Christoph Hellwig889c65b2018-07-11 22:26:02 -0700521 */
Brian Fosterd9252d52019-02-01 09:14:23 -0800522 if (xfs_imap_valid(wpc, ip, offset_fsb))
Christoph Hellwig889c65b2018-07-11 22:26:02 -0700523 return 0;
524
Christoph Hellwig889c65b2018-07-11 22:26:02 -0700525 /*
526 * If we don't have a valid map, now it's time to get a new one for this
527 * offset. This will convert delayed allocations (including COW ones)
528 * into real extents. If we return without a valid map, it means we
529 * landed in a hole and we skip the block.
530 */
Christoph Hellwig7588cbe2019-02-15 08:02:50 -0800531retry:
Dave Chinner988ef922016-02-15 17:20:50 +1100532 xfs_ilock(ip, XFS_ILOCK_SHARED);
Christoph Hellwig8ff29572010-12-10 08:42:21 +0000533 ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
534 (ip->i_df.if_flags & XFS_IFEXTENTS));
Christoph Hellwig060d4ea2018-07-11 22:26:01 -0700535
536 /*
537 * Check if this is offset is covered by a COW extents, and if yes use
538 * it directly instead of looking up anything in the data fork.
539 */
Christoph Hellwig51d62692018-07-17 16:51:51 -0700540 if (xfs_inode_has_cow_data(ip) &&
Christoph Hellwige666aa32018-07-17 16:51:52 -0700541 xfs_iext_lookup_extent(ip, ip->i_cowfp, offset_fsb, &icur, &imap))
542 cow_fsb = imap.br_startoff;
543 if (cow_fsb != NULLFILEOFF && cow_fsb <= offset_fsb) {
Christoph Hellwig2ba090d2018-08-07 10:57:12 -0700544 wpc->cow_seq = READ_ONCE(ip->i_cowfp->if_seq);
Christoph Hellwig5c665e52018-07-11 22:25:59 -0700545 xfs_iunlock(ip, XFS_ILOCK_SHARED);
Christoph Hellwigbe225fe2019-02-15 08:02:46 -0800546
547 wpc->fork = XFS_COW_FORK;
Christoph Hellwig5c665e52018-07-11 22:25:59 -0700548 goto allocate_blocks;
549 }
550
551 /*
Brian Fosterd9252d52019-02-01 09:14:23 -0800552 * No COW extent overlap. Revalidate now that we may have updated
553 * ->cow_seq. If the data mapping is still valid, we're done.
Christoph Hellwig5c665e52018-07-11 22:25:59 -0700554 */
Brian Fosterd9252d52019-02-01 09:14:23 -0800555 if (xfs_imap_valid(wpc, ip, offset_fsb)) {
Christoph Hellwig5c665e52018-07-11 22:25:59 -0700556 xfs_iunlock(ip, XFS_ILOCK_SHARED);
557 return 0;
558 }
559
560 /*
561 * If we don't have a valid map, now it's time to get a new one for this
562 * offset. This will convert delayed allocations (including COW ones)
563 * into real extents.
564 */
Christoph Hellwig33457462018-07-11 22:26:02 -0700565 if (!xfs_iext_lookup_extent(ip, &ip->i_df, offset_fsb, &icur, &imap))
566 imap.br_startoff = end_fsb; /* fake a hole past EOF */
Brian Fosterd9252d52019-02-01 09:14:23 -0800567 wpc->data_seq = READ_ONCE(ip->i_df.if_seq);
Christoph Hellwig8ff29572010-12-10 08:42:21 +0000568 xfs_iunlock(ip, XFS_ILOCK_SHARED);
Christoph Hellwiga206c812010-12-10 08:42:20 +0000569
Christoph Hellwigbe225fe2019-02-15 08:02:46 -0800570 wpc->fork = XFS_DATA_FORK;
571
Christoph Hellwig12df89f2019-02-18 09:38:47 -0800572 /* landed in a hole or beyond EOF? */
Christoph Hellwig33457462018-07-11 22:26:02 -0700573 if (imap.br_startoff > offset_fsb) {
Christoph Hellwig33457462018-07-11 22:26:02 -0700574 imap.br_blockcount = imap.br_startoff - offset_fsb;
Christoph Hellwig5c665e52018-07-11 22:25:59 -0700575 imap.br_startoff = offset_fsb;
Christoph Hellwig5c665e52018-07-11 22:25:59 -0700576 imap.br_startblock = HOLESTARTBLOCK;
Christoph Hellwigbe225fe2019-02-15 08:02:46 -0800577 imap.br_state = XFS_EXT_NORM;
Christoph Hellwiga206c812010-12-10 08:42:20 +0000578 }
579
Christoph Hellwig12df89f2019-02-18 09:38:47 -0800580 /*
581 * Truncate to the next COW extent if there is one. This is the only
582 * opportunity to do this because we can skip COW fork lookups for the
583 * subsequent blocks in the mapping; however, the requirement to treat
584 * the COW range separately remains.
585 */
586 if (cow_fsb != NULLFILEOFF &&
587 cow_fsb < imap.br_startoff + imap.br_blockcount)
588 imap.br_blockcount = cow_fsb - imap.br_startoff;
589
590 /* got a delalloc extent? */
591 if (imap.br_startblock != HOLESTARTBLOCK &&
592 isnullstartblock(imap.br_startblock))
593 goto allocate_blocks;
594
Christoph Hellwig5c665e52018-07-11 22:25:59 -0700595 wpc->imap = imap;
Christoph Hellwigbe225fe2019-02-15 08:02:46 -0800596 trace_xfs_map_blocks_found(ip, offset, count, wpc->fork, &imap);
Christoph Hellwig5c665e52018-07-11 22:25:59 -0700597 return 0;
598allocate_blocks:
Christoph Hellwig4ad765e2019-02-15 08:02:49 -0800599 error = xfs_convert_blocks(wpc, ip, offset_fsb);
Christoph Hellwig7588cbe2019-02-15 08:02:50 -0800600 if (error) {
601 /*
602 * If we failed to find the extent in the COW fork we might have
603 * raced with a COW to data fork conversion or truncate.
604 * Restart the lookup to catch the extent in the data fork for
605 * the former case, but prevent additional retries to avoid
606 * looping forever for the latter case.
607 */
608 if (error == -EAGAIN && wpc->fork == XFS_COW_FORK && !retries++)
609 goto retry;
610 ASSERT(error != -EAGAIN);
Christoph Hellwig5c665e52018-07-11 22:25:59 -0700611 return error;
Christoph Hellwig7588cbe2019-02-15 08:02:50 -0800612 }
Christoph Hellwig4ad765e2019-02-15 08:02:49 -0800613
614 /*
615 * Due to merging the return real extent might be larger than the
616 * original delalloc one. Trim the return extent to the next COW
617 * boundary again to force a re-lookup.
618 */
619 if (wpc->fork != XFS_COW_FORK && cow_fsb != NULLFILEOFF &&
620 cow_fsb < wpc->imap.br_startoff + wpc->imap.br_blockcount)
621 wpc->imap.br_blockcount = cow_fsb - wpc->imap.br_startoff;
622
623 ASSERT(wpc->imap.br_startoff <= offset_fsb);
624 ASSERT(wpc->imap.br_startoff + wpc->imap.br_blockcount > offset_fsb);
Christoph Hellwigbe225fe2019-02-15 08:02:46 -0800625 trace_xfs_map_blocks_alloc(ip, offset, count, wpc->fork, &imap);
Christoph Hellwig8ff29572010-12-10 08:42:21 +0000626 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627}
628
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100629/*
Dave Chinnerbb187822016-04-06 08:11:25 +1000630 * Submit the bio for an ioend. We are passed an ioend with a bio attached to
631 * it, and we submit that bio. The ioend may be used for multiple bio
632 * submissions, so we only want to allocate an append transaction for the ioend
633 * once. In the case of multiple bio submission, each bio will take an IO
634 * reference to the ioend to ensure that the ioend completion is only done once
635 * all bios have been submitted and the ioend is really done.
Dave Chinner7bf7f352012-11-12 22:09:45 +1100636 *
Christoph Hellwig0290d9c2019-06-28 19:31:36 -0700637 * If @status is non-zero, it means that we have a situation where some part of
Dave Chinner7bf7f352012-11-12 22:09:45 +1100638 * the submission process has failed after we have marked paged for writeback
Dave Chinnerbb187822016-04-06 08:11:25 +1000639 * and unlocked them. In this situation, we need to fail the bio and ioend
640 * rather than submit it to IO. This typically only happens on a filesystem
641 * shutdown.
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100642 */
Dave Chinnere10de372016-02-15 17:23:12 +1100643STATIC int
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100644xfs_submit_ioend(
Christoph Hellwig06342cf2009-10-30 09:09:15 +0000645 struct writeback_control *wbc,
Christoph Hellwig0e51a8e2016-04-06 08:34:30 +1000646 struct xfs_ioend *ioend,
Dave Chinnere10de372016-02-15 17:23:12 +1100647 int status)
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100648{
Christoph Hellwig73d30d42019-06-28 19:31:38 -0700649 unsigned int nofs_flag;
650
651 /*
652 * We can allocate memory here while doing writeback on behalf of
653 * memory reclaim. To avoid memory allocation deadlocks set the
654 * task-wide nofs context for the following operations.
655 */
656 nofs_flag = memalloc_nofs_save();
657
Darrick J. Wong5eda4302017-02-02 15:14:02 -0800658 /* Convert CoW extents to regular */
Christoph Hellwigbe225fe2019-02-15 08:02:46 -0800659 if (!status && ioend->io_fork == XFS_COW_FORK) {
Darrick J. Wong5eda4302017-02-02 15:14:02 -0800660 status = xfs_reflink_convert_cow(XFS_I(ioend->io_inode),
661 ioend->io_offset, ioend->io_size);
662 }
663
Dave Chinnere10de372016-02-15 17:23:12 +1100664 /* Reserve log space if we might write beyond the on-disk inode size. */
665 if (!status &&
Christoph Hellwigbe225fe2019-02-15 08:02:46 -0800666 (ioend->io_fork == XFS_COW_FORK ||
667 ioend->io_state != XFS_EXT_UNWRITTEN) &&
Dave Chinnerbb187822016-04-06 08:11:25 +1000668 xfs_ioend_is_append(ioend) &&
669 !ioend->io_append_trans)
Dave Chinnere10de372016-02-15 17:23:12 +1100670 status = xfs_setfilesize_trans_alloc(ioend);
Dave Chinnerbb187822016-04-06 08:11:25 +1000671
Christoph Hellwig73d30d42019-06-28 19:31:38 -0700672 memalloc_nofs_restore(nofs_flag);
673
Christoph Hellwig0e51a8e2016-04-06 08:34:30 +1000674 ioend->io_bio->bi_private = ioend;
675 ioend->io_bio->bi_end_io = xfs_end_bio;
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600676
Dave Chinnere10de372016-02-15 17:23:12 +1100677 /*
678 * If we are failing the IO now, just mark the ioend with an
679 * error and finish it. This will run IO completion immediately
680 * as there is only one reference to the ioend at this point in
681 * time.
682 */
683 if (status) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200684 ioend->io_bio->bi_status = errno_to_blk_status(status);
Christoph Hellwig0e51a8e2016-04-06 08:34:30 +1000685 bio_endio(ioend->io_bio);
Dave Chinnere10de372016-02-15 17:23:12 +1100686 return status;
687 }
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100688
Mike Christie4e49ea42016-06-05 14:31:41 -0500689 submit_bio(ioend->io_bio);
Dave Chinnere10de372016-02-15 17:23:12 +1100690 return 0;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100691}
692
Christoph Hellwig0e51a8e2016-04-06 08:34:30 +1000693static struct xfs_ioend *
694xfs_alloc_ioend(
695 struct inode *inode,
Christoph Hellwigbe225fe2019-02-15 08:02:46 -0800696 int fork,
697 xfs_exntst_t state,
Christoph Hellwig0e51a8e2016-04-06 08:34:30 +1000698 xfs_off_t offset,
Christoph Hellwig3faed662018-07-11 22:26:02 -0700699 struct block_device *bdev,
Christoph Hellwiga2473732019-06-28 19:30:22 -0700700 sector_t sector,
701 struct writeback_control *wbc)
Christoph Hellwig0e51a8e2016-04-06 08:34:30 +1000702{
703 struct xfs_ioend *ioend;
704 struct bio *bio;
705
Kent Overstreete292d7b2018-05-20 18:25:57 -0400706 bio = bio_alloc_bioset(GFP_NOFS, BIO_MAX_PAGES, &xfs_ioend_bioset);
Christoph Hellwig3faed662018-07-11 22:26:02 -0700707 bio_set_dev(bio, bdev);
708 bio->bi_iter.bi_sector = sector;
Christoph Hellwiga2473732019-06-28 19:30:22 -0700709 bio->bi_opf = REQ_OP_WRITE | wbc_to_write_flags(wbc);
710 bio->bi_write_hint = inode->i_write_hint;
Christoph Hellwigadfb5fb2019-06-28 19:30:22 -0700711 wbc_init_bio(wbc, bio);
Christoph Hellwig0e51a8e2016-04-06 08:34:30 +1000712
713 ioend = container_of(bio, struct xfs_ioend, io_inline_bio);
714 INIT_LIST_HEAD(&ioend->io_list);
Christoph Hellwigbe225fe2019-02-15 08:02:46 -0800715 ioend->io_fork = fork;
716 ioend->io_state = state;
Christoph Hellwig0e51a8e2016-04-06 08:34:30 +1000717 ioend->io_inode = inode;
718 ioend->io_size = 0;
719 ioend->io_offset = offset;
Christoph Hellwig0e51a8e2016-04-06 08:34:30 +1000720 ioend->io_append_trans = NULL;
721 ioend->io_bio = bio;
722 return ioend;
723}
724
725/*
726 * Allocate a new bio, and chain the old bio to the new one.
727 *
728 * Note that we have to do perform the chaining in this unintuitive order
729 * so that the bi_private linkage is set up in the right direction for the
730 * traversal in xfs_destroy_ioend().
731 */
Christoph Hellwiga2473732019-06-28 19:30:22 -0700732static struct bio *
Christoph Hellwig0e51a8e2016-04-06 08:34:30 +1000733xfs_chain_bio(
Christoph Hellwiga2473732019-06-28 19:30:22 -0700734 struct bio *prev)
Christoph Hellwig0e51a8e2016-04-06 08:34:30 +1000735{
736 struct bio *new;
737
738 new = bio_alloc(GFP_NOFS, BIO_MAX_PAGES);
Christoph Hellwigadfb5fb2019-06-28 19:30:22 -0700739 bio_copy_dev(new, prev);/* also copies over blkcg information */
Christoph Hellwiga2473732019-06-28 19:30:22 -0700740 new->bi_iter.bi_sector = bio_end_sector(prev);
741 new->bi_opf = prev->bi_opf;
742 new->bi_write_hint = prev->bi_write_hint;
743
744 bio_chain(prev, new);
745 bio_get(prev); /* for xfs_destroy_ioend */
746 submit_bio(prev);
747 return new;
Christoph Hellwig0e51a8e2016-04-06 08:34:30 +1000748}
749
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100750/*
Christoph Hellwig3faed662018-07-11 22:26:02 -0700751 * Test to see if we have an existing ioend structure that we could append to
752 * first, otherwise finish off the current ioend and start another.
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100753 */
754STATIC void
755xfs_add_to_ioend(
756 struct inode *inode,
Christoph Hellwig7336cea2006-01-11 20:49:16 +1100757 xfs_off_t offset,
Christoph Hellwig3faed662018-07-11 22:26:02 -0700758 struct page *page,
Christoph Hellwig82cb1412018-07-11 22:26:05 -0700759 struct iomap_page *iop,
Dave Chinnere10de372016-02-15 17:23:12 +1100760 struct xfs_writepage_ctx *wpc,
Dave Chinnerbb187822016-04-06 08:11:25 +1000761 struct writeback_control *wbc,
Dave Chinnere10de372016-02-15 17:23:12 +1100762 struct list_head *iolist)
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100763{
Christoph Hellwig3faed662018-07-11 22:26:02 -0700764 struct xfs_inode *ip = XFS_I(inode);
765 struct xfs_mount *mp = ip->i_mount;
766 struct block_device *bdev = xfs_find_bdev_for_inode(inode);
767 unsigned len = i_blocksize(inode);
768 unsigned poff = offset & (PAGE_SIZE - 1);
Christoph Hellwigff896732019-06-17 11:14:11 +0200769 bool merged, same_page = false;
Christoph Hellwig3faed662018-07-11 22:26:02 -0700770 sector_t sector;
771
772 sector = xfs_fsb_to_db(ip, wpc->imap.br_startblock) +
773 ((offset - XFS_FSB_TO_B(mp, wpc->imap.br_startoff)) >> 9);
774
Christoph Hellwigbe225fe2019-02-15 08:02:46 -0800775 if (!wpc->ioend ||
776 wpc->fork != wpc->ioend->io_fork ||
777 wpc->imap.br_state != wpc->ioend->io_state ||
Christoph Hellwig3faed662018-07-11 22:26:02 -0700778 sector != bio_end_sector(wpc->ioend->io_bio) ||
Darrick J. Wong0df61da2016-03-07 09:32:14 +1100779 offset != wpc->ioend->io_offset + wpc->ioend->io_size) {
Dave Chinnere10de372016-02-15 17:23:12 +1100780 if (wpc->ioend)
781 list_add(&wpc->ioend->io_list, iolist);
Christoph Hellwigbe225fe2019-02-15 08:02:46 -0800782 wpc->ioend = xfs_alloc_ioend(inode, wpc->fork,
Christoph Hellwiga2473732019-06-28 19:30:22 -0700783 wpc->imap.br_state, offset, bdev, sector, wbc);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100784 }
785
Christoph Hellwigff896732019-06-17 11:14:11 +0200786 merged = __bio_try_merge_page(wpc->ioend->io_bio, page, len, poff,
787 &same_page);
788
789 if (iop && !same_page)
790 atomic_inc(&iop->write_count);
791
792 if (!merged) {
Ming Lei79d08f82019-07-01 15:14:46 +0800793 if (bio_full(wpc->ioend->io_bio, len))
Christoph Hellwiga2473732019-06-28 19:30:22 -0700794 wpc->ioend->io_bio = xfs_chain_bio(wpc->ioend->io_bio);
Ming Lei07173c32019-02-15 19:13:20 +0800795 bio_add_page(wpc->ioend->io_bio, page, len, poff);
Christoph Hellwig82cb1412018-07-11 22:26:05 -0700796 }
Dave Chinnerbb187822016-04-06 08:11:25 +1000797
Christoph Hellwig3faed662018-07-11 22:26:02 -0700798 wpc->ioend->io_size += len;
Linus Torvalds9637d512019-07-15 21:20:52 -0700799 wbc_account_cgroup_owner(wbc, page, len);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100800}
801
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802STATIC void
Dave Chinner3ed3a432010-03-05 02:00:42 +0000803xfs_vm_invalidatepage(
804 struct page *page,
Lukas Czernerd47992f2013-05-21 23:17:23 -0400805 unsigned int offset,
806 unsigned int length)
Dave Chinner3ed3a432010-03-05 02:00:42 +0000807{
Christoph Hellwig82cb1412018-07-11 22:26:05 -0700808 trace_xfs_invalidatepage(page->mapping->host, page, offset, length);
809 iomap_invalidatepage(page, offset, length);
Dave Chinner3ed3a432010-03-05 02:00:42 +0000810}
811
812/*
Christoph Hellwig82cb1412018-07-11 22:26:05 -0700813 * If the page has delalloc blocks on it, we need to punch them out before we
814 * invalidate the page. If we don't, we leave a stale delalloc mapping on the
815 * inode that can trip up a later direct I/O read operation on the same region.
Dave Chinner3ed3a432010-03-05 02:00:42 +0000816 *
Christoph Hellwig82cb1412018-07-11 22:26:05 -0700817 * We prevent this by truncating away the delalloc regions on the page. Because
818 * they are delalloc, we can do this without needing a transaction. Indeed - if
819 * we get ENOSPC errors, we have to be able to do this truncation without a
820 * transaction as there is no space left for block reservation (typically why we
821 * see a ENOSPC in writeback).
Dave Chinner3ed3a432010-03-05 02:00:42 +0000822 */
823STATIC void
824xfs_aops_discard_page(
825 struct page *page)
826{
827 struct inode *inode = page->mapping->host;
828 struct xfs_inode *ip = XFS_I(inode);
Christoph Hellwig03625722018-07-11 22:25:57 -0700829 struct xfs_mount *mp = ip->i_mount;
Dave Chinner3ed3a432010-03-05 02:00:42 +0000830 loff_t offset = page_offset(page);
Christoph Hellwig03625722018-07-11 22:25:57 -0700831 xfs_fileoff_t start_fsb = XFS_B_TO_FSBT(mp, offset);
832 int error;
Dave Chinner3ed3a432010-03-05 02:00:42 +0000833
Christoph Hellwig03625722018-07-11 22:25:57 -0700834 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner3ed3a432010-03-05 02:00:42 +0000835 goto out_invalidate;
836
Christoph Hellwig03625722018-07-11 22:25:57 -0700837 xfs_alert(mp,
Darrick J. Wongc9690042018-01-09 12:02:55 -0800838 "page discard on page "PTR_FMT", inode 0x%llx, offset %llu.",
Dave Chinner3ed3a432010-03-05 02:00:42 +0000839 page, ip->i_ino, offset);
840
Christoph Hellwig03625722018-07-11 22:25:57 -0700841 error = xfs_bmap_punch_delalloc_range(ip, start_fsb,
842 PAGE_SIZE / i_blocksize(inode));
Christoph Hellwig03625722018-07-11 22:25:57 -0700843 if (error && !XFS_FORCED_SHUTDOWN(mp))
844 xfs_alert(mp, "page discard unable to remove delalloc mapping.");
Dave Chinner3ed3a432010-03-05 02:00:42 +0000845out_invalidate:
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300846 xfs_vm_invalidatepage(page, 0, PAGE_SIZE);
Dave Chinner3ed3a432010-03-05 02:00:42 +0000847}
848
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849/*
Dave Chinnere10de372016-02-15 17:23:12 +1100850 * We implement an immediate ioend submission policy here to avoid needing to
851 * chain multiple ioends and hence nest mempool allocations which can violate
852 * forward progress guarantees we need to provide. The current ioend we are
Christoph Hellwig82cb1412018-07-11 22:26:05 -0700853 * adding blocks to is cached on the writepage context, and if the new block
Dave Chinnere10de372016-02-15 17:23:12 +1100854 * does not append to the cached ioend it will create a new ioend and cache that
855 * instead.
856 *
857 * If a new ioend is created and cached, the old ioend is returned and queued
858 * locally for submission once the entire page is processed or an error has been
859 * detected. While ioends are submitted immediately after they are completed,
860 * batching optimisations are provided by higher level block plugging.
861 *
862 * At the end of a writeback pass, there will be a cached ioend remaining on the
863 * writepage context that the caller will need to submit.
864 */
Dave Chinnerbfce7d22016-02-15 17:21:37 +1100865static int
866xfs_writepage_map(
867 struct xfs_writepage_ctx *wpc,
Dave Chinnere10de372016-02-15 17:23:12 +1100868 struct writeback_control *wbc,
Dave Chinnerbfce7d22016-02-15 17:21:37 +1100869 struct inode *inode,
870 struct page *page,
Darrick J. Wong2d5f4b52017-11-27 09:50:22 -0800871 uint64_t end_offset)
Dave Chinnerbfce7d22016-02-15 17:21:37 +1100872{
Dave Chinnere10de372016-02-15 17:23:12 +1100873 LIST_HEAD(submit_list);
Christoph Hellwig82cb1412018-07-11 22:26:05 -0700874 struct iomap_page *iop = to_iomap_page(page);
875 unsigned len = i_blocksize(inode);
Dave Chinnere10de372016-02-15 17:23:12 +1100876 struct xfs_ioend *ioend, *next;
Christoph Hellwig6a4c9502018-07-11 22:26:00 -0700877 uint64_t file_offset; /* file offset of page */
Christoph Hellwig82cb1412018-07-11 22:26:05 -0700878 int error = 0, count = 0, i;
Dave Chinnerbfce7d22016-02-15 17:21:37 +1100879
Christoph Hellwig82cb1412018-07-11 22:26:05 -0700880 ASSERT(iop || i_blocksize(inode) == PAGE_SIZE);
881 ASSERT(!iop || atomic_read(&iop->write_count) == 0);
Christoph Hellwigac8ee542018-07-11 22:26:04 -0700882
Dave Chinnere2f6ad42018-07-11 22:26:00 -0700883 /*
Christoph Hellwig82cb1412018-07-11 22:26:05 -0700884 * Walk through the page to find areas to write back. If we run off the
885 * end of the current map or find the current map invalid, grab a new
886 * one.
Dave Chinnere2f6ad42018-07-11 22:26:00 -0700887 */
Christoph Hellwig82cb1412018-07-11 22:26:05 -0700888 for (i = 0, file_offset = page_offset(page);
889 i < (PAGE_SIZE >> inode->i_blkbits) && file_offset < end_offset;
890 i++, file_offset += len) {
891 if (iop && !test_bit(i, iop->uptodate))
Dave Chinnerbfce7d22016-02-15 17:21:37 +1100892 continue;
Dave Chinnerbfce7d22016-02-15 17:21:37 +1100893
Christoph Hellwig889c65b2018-07-11 22:26:02 -0700894 error = xfs_map_blocks(wpc, inode, file_offset);
895 if (error)
896 break;
Christoph Hellwigbe225fe2019-02-15 08:02:46 -0800897 if (wpc->imap.br_startblock == HOLESTARTBLOCK)
Christoph Hellwigac8ee542018-07-11 22:26:04 -0700898 continue;
Christoph Hellwig82cb1412018-07-11 22:26:05 -0700899 xfs_add_to_ioend(inode, file_offset, page, iop, wpc, wbc,
900 &submit_list);
Christoph Hellwig5c665e52018-07-11 22:25:59 -0700901 count++;
Dave Chinnere2f6ad42018-07-11 22:26:00 -0700902 }
Dave Chinnerbfce7d22016-02-15 17:21:37 +1100903
Dave Chinnere10de372016-02-15 17:23:12 +1100904 ASSERT(wpc->ioend || list_empty(&submit_list));
Christoph Hellwig1b65d3dd2018-07-11 22:26:03 -0700905 ASSERT(PageLocked(page));
906 ASSERT(!PageWriteback(page));
Dave Chinnerbfce7d22016-02-15 17:21:37 +1100907
Dave Chinnerbfce7d22016-02-15 17:21:37 +1100908 /*
Christoph Hellwig82cb1412018-07-11 22:26:05 -0700909 * On error, we have to fail the ioend here because we may have set
910 * pages under writeback, we have to make sure we run IO completion to
911 * mark the error state of the IO appropriately, so we can't cancel the
912 * ioend directly here. That means we have to mark this page as under
913 * writeback if we included any blocks from it in the ioend chain so
914 * that completion treats it correctly.
Dave Chinnerbfce7d22016-02-15 17:21:37 +1100915 *
Dave Chinnere10de372016-02-15 17:23:12 +1100916 * If we didn't include the page in the ioend, the on error we can
917 * simply discard and unlock it as there are no other users of the page
Christoph Hellwig82cb1412018-07-11 22:26:05 -0700918 * now. The caller will still need to trigger submission of outstanding
919 * ioends on the writepage context so they are treated correctly on
920 * error.
Dave Chinnerbfce7d22016-02-15 17:21:37 +1100921 */
Christoph Hellwig8e1f0652018-07-11 22:26:04 -0700922 if (unlikely(error)) {
923 if (!count) {
924 xfs_aops_discard_page(page);
925 ClearPageUptodate(page);
926 unlock_page(page);
927 goto done;
928 }
929
Christoph Hellwig1b65d3dd2018-07-11 22:26:03 -0700930 /*
931 * If the page was not fully cleaned, we need to ensure that the
932 * higher layers come back to it correctly. That means we need
933 * to keep the page dirty, and for WB_SYNC_ALL writeback we need
934 * to ensure the PAGECACHE_TAG_TOWRITE index mark is not removed
935 * so another attempt to write this page in this writeback sweep
936 * will be made.
937 */
Christoph Hellwig8e1f0652018-07-11 22:26:04 -0700938 set_page_writeback_keepwrite(page);
Dave Chinnere10de372016-02-15 17:23:12 +1100939 } else {
Christoph Hellwig1b65d3dd2018-07-11 22:26:03 -0700940 clear_page_dirty_for_io(page);
941 set_page_writeback(page);
Dave Chinnerbfce7d22016-02-15 17:21:37 +1100942 }
Dave Chinnere10de372016-02-15 17:23:12 +1100943
Christoph Hellwig8e1f0652018-07-11 22:26:04 -0700944 unlock_page(page);
945
946 /*
947 * Preserve the original error if there was one, otherwise catch
948 * submission errors here and propagate into subsequent ioend
949 * submissions.
950 */
951 list_for_each_entry_safe(ioend, next, &submit_list, io_list) {
952 int error2;
953
954 list_del_init(&ioend->io_list);
955 error2 = xfs_submit_ioend(wbc, ioend, error);
956 if (error2 && !error)
957 error = error2;
958 }
959
960 /*
Christoph Hellwig82cb1412018-07-11 22:26:05 -0700961 * We can end up here with no error and nothing to write only if we race
962 * with a partial page truncate on a sub-page block sized filesystem.
Christoph Hellwig8e1f0652018-07-11 22:26:04 -0700963 */
964 if (!count)
965 end_page_writeback(page);
966done:
Dave Chinnerbfce7d22016-02-15 17:21:37 +1100967 mapping_set_error(page->mapping, error);
968 return error;
969}
970
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971/*
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000972 * Write out a dirty page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 *
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000974 * For delalloc space on the page we need to allocate space and flush it.
975 * For unwritten space on the page we need to start the conversion to
976 * regular allocated space.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978STATIC int
Dave Chinnerfbcc0252016-02-15 17:21:19 +1100979xfs_do_writepage(
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000980 struct page *page,
Dave Chinnerfbcc0252016-02-15 17:21:19 +1100981 struct writeback_control *wbc,
982 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983{
Dave Chinnerfbcc0252016-02-15 17:21:19 +1100984 struct xfs_writepage_ctx *wpc = data;
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000985 struct inode *inode = page->mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 loff_t offset;
Darrick J. Wongc8ce5402017-06-16 11:00:05 -0700987 uint64_t end_offset;
Dave Chinnerad689722016-02-15 17:21:31 +1100988 pgoff_t end_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
Lukas Czerner34097df2013-05-21 23:58:01 -0400990 trace_xfs_writepage(inode, page, 0, 0);
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000991
992 /*
993 * Refuse to write the page out if we are called from reclaim context.
994 *
Christoph Hellwigd4f7a5c2010-06-28 10:34:44 -0400995 * This avoids stack overflows when called from deeply used stacks in
996 * random callers for direct reclaim or memcg reclaim. We explicitly
997 * allow reclaim from kswapd as the stack usage there is relatively low.
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000998 *
Mel Gorman94054fa2011-10-31 17:07:45 -0700999 * This should never happen except in the case of a VM regression so
1000 * warn about it.
Christoph Hellwig89f3b3632010-06-24 09:45:48 +10001001 */
Mel Gorman94054fa2011-10-31 17:07:45 -07001002 if (WARN_ON_ONCE((current->flags & (PF_MEMALLOC|PF_KSWAPD)) ==
1003 PF_MEMALLOC))
Christoph Hellwigb5420f22010-08-24 11:47:51 +10001004 goto redirty;
Christoph Hellwig89f3b3632010-06-24 09:45:48 +10001005
1006 /*
Christoph Hellwig680a6472011-07-08 14:34:05 +02001007 * Given that we do not allow direct reclaim to call us, we should
1008 * never be called while in a filesystem transaction.
Christoph Hellwig89f3b3632010-06-24 09:45:48 +10001009 */
Michal Hocko90707332017-05-03 14:53:12 -07001010 if (WARN_ON_ONCE(current->flags & PF_MEMALLOC_NOFS))
Christoph Hellwigb5420f22010-08-24 11:47:51 +10001011 goto redirty;
Christoph Hellwig89f3b3632010-06-24 09:45:48 +10001012
Jie Liu8695d272014-05-20 08:24:26 +10001013 /*
Dave Chinnerad689722016-02-15 17:21:31 +11001014 * Is this page beyond the end of the file?
1015 *
Jie Liu8695d272014-05-20 08:24:26 +10001016 * The page index is less than the end_index, adjust the end_offset
1017 * to the highest offset that this page should represent.
1018 * -----------------------------------------------------
1019 * | file mapping | <EOF> |
1020 * -----------------------------------------------------
1021 * | Page ... | Page N-2 | Page N-1 | Page N | |
1022 * ^--------------------------------^----------|--------
1023 * | desired writeback range | see else |
1024 * ---------------------------------^------------------|
1025 */
Dave Chinnerad689722016-02-15 17:21:31 +11001026 offset = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001027 end_index = offset >> PAGE_SHIFT;
Jie Liu8695d272014-05-20 08:24:26 +10001028 if (page->index < end_index)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001029 end_offset = (xfs_off_t)(page->index + 1) << PAGE_SHIFT;
Jie Liu8695d272014-05-20 08:24:26 +10001030 else {
1031 /*
1032 * Check whether the page to write out is beyond or straddles
1033 * i_size or not.
1034 * -------------------------------------------------------
1035 * | file mapping | <EOF> |
1036 * -------------------------------------------------------
1037 * | Page ... | Page N-2 | Page N-1 | Page N | Beyond |
1038 * ^--------------------------------^-----------|---------
1039 * | | Straddles |
1040 * ---------------------------------^-----------|--------|
1041 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001042 unsigned offset_into_page = offset & (PAGE_SIZE - 1);
Christoph Hellwig6b7a03f2012-07-03 12:20:00 -04001043
1044 /*
Jan Karaff9a28f2013-03-14 14:30:54 +01001045 * Skip the page if it is fully outside i_size, e.g. due to a
1046 * truncate operation that is in progress. We must redirty the
1047 * page so that reclaim stops reclaiming it. Otherwise
1048 * xfs_vm_releasepage() is called on it and gets confused.
Jie Liu8695d272014-05-20 08:24:26 +10001049 *
1050 * Note that the end_index is unsigned long, it would overflow
1051 * if the given offset is greater than 16TB on 32-bit system
1052 * and if we do check the page is fully outside i_size or not
1053 * via "if (page->index >= end_index + 1)" as "end_index + 1"
1054 * will be evaluated to 0. Hence this page will be redirtied
1055 * and be written out repeatedly which would result in an
1056 * infinite loop, the user program that perform this operation
1057 * will hang. Instead, we can verify this situation by checking
1058 * if the page to write is totally beyond the i_size or if it's
1059 * offset is just equal to the EOF.
Christoph Hellwig6b7a03f2012-07-03 12:20:00 -04001060 */
Jie Liu8695d272014-05-20 08:24:26 +10001061 if (page->index > end_index ||
1062 (page->index == end_index && offset_into_page == 0))
Jan Karaff9a28f2013-03-14 14:30:54 +01001063 goto redirty;
Christoph Hellwig6b7a03f2012-07-03 12:20:00 -04001064
1065 /*
1066 * The page straddles i_size. It must be zeroed out on each
1067 * and every writepage invocation because it may be mmapped.
1068 * "A file is mapped in multiples of the page size. For a file
Jie Liu8695d272014-05-20 08:24:26 +10001069 * that is not a multiple of the page size, the remaining
Christoph Hellwig6b7a03f2012-07-03 12:20:00 -04001070 * memory is zeroed when mapped, and writes to that region are
1071 * not written out to the file."
1072 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001073 zero_user_segment(page, offset_into_page, PAGE_SIZE);
Jie Liu8695d272014-05-20 08:24:26 +10001074
1075 /* Adjust the end_offset to the end of file */
1076 end_offset = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 }
1078
Darrick J. Wong2d5f4b52017-11-27 09:50:22 -08001079 return xfs_writepage_map(wpc, wbc, inode, page, end_offset);
Nathan Scottf51623b2006-03-14 13:26:27 +11001080
Christoph Hellwigb5420f22010-08-24 11:47:51 +10001081redirty:
Nathan Scottf51623b2006-03-14 13:26:27 +11001082 redirty_page_for_writepage(wbc, page);
1083 unlock_page(page);
1084 return 0;
Nathan Scottf51623b2006-03-14 13:26:27 +11001085}
1086
Nathan Scott7d4fb402006-06-09 15:27:16 +10001087STATIC int
Dave Chinnerfbcc0252016-02-15 17:21:19 +11001088xfs_vm_writepage(
1089 struct page *page,
1090 struct writeback_control *wbc)
1091{
Christoph Hellwigbe225fe2019-02-15 08:02:46 -08001092 struct xfs_writepage_ctx wpc = { };
Dave Chinnerfbcc0252016-02-15 17:21:19 +11001093 int ret;
1094
1095 ret = xfs_do_writepage(page, wbc, &wpc);
Dave Chinnere10de372016-02-15 17:23:12 +11001096 if (wpc.ioend)
1097 ret = xfs_submit_ioend(wbc, wpc.ioend, ret);
1098 return ret;
Dave Chinnerfbcc0252016-02-15 17:21:19 +11001099}
1100
1101STATIC int
Nathan Scott7d4fb402006-06-09 15:27:16 +10001102xfs_vm_writepages(
1103 struct address_space *mapping,
1104 struct writeback_control *wbc)
1105{
Christoph Hellwigbe225fe2019-02-15 08:02:46 -08001106 struct xfs_writepage_ctx wpc = { };
Dave Chinnerfbcc0252016-02-15 17:21:19 +11001107 int ret;
1108
Christoph Hellwigb3aea4e2007-08-29 11:44:37 +10001109 xfs_iflags_clear(XFS_I(mapping->host), XFS_ITRUNCATED);
Dave Chinnerfbcc0252016-02-15 17:21:19 +11001110 ret = write_cache_pages(mapping, wbc, xfs_do_writepage, &wpc);
Dave Chinnere10de372016-02-15 17:23:12 +11001111 if (wpc.ioend)
1112 ret = xfs_submit_ioend(wbc, wpc.ioend, ret);
1113 return ret;
Nathan Scott7d4fb402006-06-09 15:27:16 +10001114}
1115
Dan Williams6e2608d2018-03-07 15:26:44 -08001116STATIC int
1117xfs_dax_writepages(
1118 struct address_space *mapping,
1119 struct writeback_control *wbc)
1120{
1121 xfs_iflags_clear(XFS_I(mapping->host), XFS_ITRUNCATED);
1122 return dax_writeback_mapping_range(mapping,
1123 xfs_find_bdev_for_inode(mapping->host), wbc);
1124}
1125
Nathan Scottf51623b2006-03-14 13:26:27 +11001126STATIC int
Nathan Scott238f4c52006-03-17 17:26:25 +11001127xfs_vm_releasepage(
Nathan Scottf51623b2006-03-14 13:26:27 +11001128 struct page *page,
1129 gfp_t gfp_mask)
1130{
Lukas Czerner34097df2013-05-21 23:58:01 -04001131 trace_xfs_releasepage(page->mapping->host, page, 0, 0);
Christoph Hellwig82cb1412018-07-11 22:26:05 -07001132 return iomap_releasepage(page, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133}
1134
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135STATIC sector_t
Nathan Scotte4c573b2006-03-14 13:54:26 +11001136xfs_vm_bmap(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 struct address_space *mapping,
1138 sector_t block)
1139{
Christoph Hellwigb84e7722018-06-01 09:03:09 -07001140 struct xfs_inode *ip = XFS_I(mapping->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
Christoph Hellwigb84e7722018-06-01 09:03:09 -07001142 trace_xfs_vm_bmap(ip);
Darrick J. Wongdb1327b2016-10-03 09:11:36 -07001143
1144 /*
1145 * The swap code (ab-)uses ->bmap to get a block mapping and then
Ingo Molnar793057e2018-02-28 09:39:48 +01001146 * bypasses the file system for actual I/O. We really can't allow
Darrick J. Wongdb1327b2016-10-03 09:11:36 -07001147 * that on reflinks inodes, so we have to skip out here. And yes,
Darrick J. Wongeb5e2482017-06-21 20:27:35 -07001148 * 0 is the magic code for a bmap error.
1149 *
1150 * Since we don't pass back blockdev info, we can't return bmap
1151 * information for rt files either.
Darrick J. Wongdb1327b2016-10-03 09:11:36 -07001152 */
Christoph Hellwig66ae56a2019-02-18 09:38:49 -08001153 if (xfs_is_cow_inode(ip) || XFS_IS_REALTIME_INODE(ip))
Darrick J. Wongdb1327b2016-10-03 09:11:36 -07001154 return 0;
Christoph Hellwigb84e7722018-06-01 09:03:09 -07001155 return iomap_bmap(mapping, block, &xfs_iomap_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156}
1157
1158STATIC int
Nathan Scotte4c573b2006-03-14 13:54:26 +11001159xfs_vm_readpage(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 struct file *unused,
1161 struct page *page)
1162{
Dave Chinner121e2132016-01-08 11:28:35 +11001163 trace_xfs_vm_readpage(page->mapping->host, 1);
Christoph Hellwig82cb1412018-07-11 22:26:05 -07001164 return iomap_readpage(page, &xfs_iomap_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165}
1166
1167STATIC int
Nathan Scotte4c573b2006-03-14 13:54:26 +11001168xfs_vm_readpages(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 struct file *unused,
1170 struct address_space *mapping,
1171 struct list_head *pages,
1172 unsigned nr_pages)
1173{
Dave Chinner121e2132016-01-08 11:28:35 +11001174 trace_xfs_vm_readpages(mapping->host, nr_pages);
Christoph Hellwig82cb1412018-07-11 22:26:05 -07001175 return iomap_readpages(mapping, pages, nr_pages, &xfs_iomap_ops);
Dave Chinner22e757a2014-09-02 12:12:51 +10001176}
1177
Darrick J. Wong67482122018-05-10 08:38:15 -07001178static int
1179xfs_iomap_swapfile_activate(
1180 struct swap_info_struct *sis,
1181 struct file *swap_file,
1182 sector_t *span)
1183{
1184 sis->bdev = xfs_find_bdev_for_inode(file_inode(swap_file));
1185 return iomap_swapfile_activate(sis, swap_file, span, &xfs_iomap_ops);
1186}
1187
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07001188const struct address_space_operations xfs_address_space_operations = {
Nathan Scotte4c573b2006-03-14 13:54:26 +11001189 .readpage = xfs_vm_readpage,
1190 .readpages = xfs_vm_readpages,
1191 .writepage = xfs_vm_writepage,
Nathan Scott7d4fb402006-06-09 15:27:16 +10001192 .writepages = xfs_vm_writepages,
Christoph Hellwig82cb1412018-07-11 22:26:05 -07001193 .set_page_dirty = iomap_set_page_dirty,
Nathan Scott238f4c52006-03-17 17:26:25 +11001194 .releasepage = xfs_vm_releasepage,
1195 .invalidatepage = xfs_vm_invalidatepage,
Nathan Scotte4c573b2006-03-14 13:54:26 +11001196 .bmap = xfs_vm_bmap,
Dan Williams6e2608d2018-03-07 15:26:44 -08001197 .direct_IO = noop_direct_IO,
Christoph Hellwig82cb1412018-07-11 22:26:05 -07001198 .migratepage = iomap_migrate_page,
1199 .is_partially_uptodate = iomap_is_partially_uptodate,
Andi Kleenaa261f52009-09-16 11:50:16 +02001200 .error_remove_page = generic_error_remove_page,
Darrick J. Wong67482122018-05-10 08:38:15 -07001201 .swap_activate = xfs_iomap_swapfile_activate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202};
Dan Williams6e2608d2018-03-07 15:26:44 -08001203
1204const struct address_space_operations xfs_dax_aops = {
1205 .writepages = xfs_dax_writepages,
1206 .direct_IO = noop_direct_IO,
1207 .set_page_dirty = noop_set_page_dirty,
1208 .invalidatepage = noop_invalidatepage,
Darrick J. Wong67482122018-05-10 08:38:15 -07001209 .swap_activate = xfs_iomap_swapfile_activate,
Dan Williams6e2608d2018-03-07 15:26:44 -08001210};