xfs_aops.c 18.7 KB
Newer Older
D
Dave Chinner 已提交
1
// SPDX-License-Identifier: GPL-2.0
L
Linus Torvalds 已提交
2
/*
3
 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4
 * Copyright (c) 2016-2018 Christoph Hellwig.
5
 * All Rights Reserved.
L
Linus Torvalds 已提交
6 7
 */
#include "xfs.h"
8
#include "xfs_shared.h"
9 10 11
#include "xfs_format.h"
#include "xfs_log_format.h"
#include "xfs_trans_resv.h"
L
Linus Torvalds 已提交
12 13
#include "xfs_mount.h"
#include "xfs_inode.h"
14
#include "xfs_trans.h"
L
Linus Torvalds 已提交
15
#include "xfs_iomap.h"
C
Christoph Hellwig 已提交
16
#include "xfs_trace.h"
17
#include "xfs_bmap.h"
D
Dave Chinner 已提交
18
#include "xfs_bmap_util.h"
19
#include "xfs_reflink.h"
L
Linus Torvalds 已提交
20

21
struct xfs_writepage_ctx {
22
	struct iomap_writepage_ctx ctx;
23
	unsigned int		data_seq;
24
	unsigned int		cow_seq;
25 26
};

27 28 29 30 31 32
static inline struct xfs_writepage_ctx *
XFS_WPC(struct iomap_writepage_ctx *ctx)
{
	return container_of(ctx, struct xfs_writepage_ctx, ctx);
}

33
struct block_device *
C
Christoph Hellwig 已提交
34
xfs_find_bdev_for_inode(
C
Christoph Hellwig 已提交
35
	struct inode		*inode)
C
Christoph Hellwig 已提交
36
{
C
Christoph Hellwig 已提交
37
	struct xfs_inode	*ip = XFS_I(inode);
C
Christoph Hellwig 已提交
38 39
	struct xfs_mount	*mp = ip->i_mount;

40
	if (XFS_IS_REALTIME_INODE(ip))
C
Christoph Hellwig 已提交
41 42 43 44 45
		return mp->m_rtdev_targp->bt_bdev;
	else
		return mp->m_ddev_targp->bt_bdev;
}

46 47 48 49 50 51 52 53 54 55 56 57 58
struct dax_device *
xfs_find_daxdev_for_inode(
	struct inode		*inode)
{
	struct xfs_inode	*ip = XFS_I(inode);
	struct xfs_mount	*mp = ip->i_mount;

	if (XFS_IS_REALTIME_INODE(ip))
		return mp->m_rtdev_targp->bt_daxdev;
	else
		return mp->m_ddev_targp->bt_daxdev;
}

C
Christoph Hellwig 已提交
59 60 61
/*
 * Fast and loose check if this write could update the on-disk inode size.
 */
62
static inline bool xfs_ioend_is_append(struct iomap_ioend *ioend)
C
Christoph Hellwig 已提交
63 64 65 66 67
{
	return ioend->io_offset + ioend->io_size >
		XFS_I(ioend->io_inode)->i_d.di_size;
}

68 69
STATIC int
xfs_setfilesize_trans_alloc(
70
	struct iomap_ioend	*ioend)
71 72 73 74 75
{
	struct xfs_mount	*mp = XFS_I(ioend->io_inode)->i_mount;
	struct xfs_trans	*tp;
	int			error;

C
Christoph Hellwig 已提交
76
	error = xfs_trans_alloc(mp, &M_RES(mp)->tr_fsyncts, 0, 0, 0, &tp);
77
	if (error)
78 79
		return error;

80
	ioend->io_private = tp;
81

J
Jan Kara 已提交
82
	/*
83
	 * We may pass freeze protection with a transaction.  So tell lockdep
J
Jan Kara 已提交
84 85
	 * we released it.
	 */
86
	__sb_writers_release(ioend->io_inode->i_sb, SB_FREEZE_FS);
87 88 89 90
	/*
	 * We hand off the transaction to the completion thread now, so
	 * clear the flag here.
	 */
91
	current_restore_flags_nested(&tp->t_pflags, PF_MEMALLOC_NOFS);
92 93 94
	return 0;
}

95
/*
96
 * Update on-disk file size now that data has been written to disk.
97
 */
98
STATIC int
99
__xfs_setfilesize(
100 101 102 103
	struct xfs_inode	*ip,
	struct xfs_trans	*tp,
	xfs_off_t		offset,
	size_t			size)
104 105 106
{
	xfs_fsize_t		isize;

107
	xfs_ilock(ip, XFS_ILOCK_EXCL);
108
	isize = xfs_new_eof(ip, offset + size);
109 110
	if (!isize) {
		xfs_iunlock(ip, XFS_ILOCK_EXCL);
111
		xfs_trans_cancel(tp);
112
		return 0;
113 114
	}

115
	trace_xfs_setfilesize(ip, offset, size);
116 117 118 119 120

	ip->i_d.di_size = isize;
	xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
	xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);

121
	return xfs_trans_commit(tp);
122 123
}

124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
int
xfs_setfilesize(
	struct xfs_inode	*ip,
	xfs_off_t		offset,
	size_t			size)
{
	struct xfs_mount	*mp = ip->i_mount;
	struct xfs_trans	*tp;
	int			error;

	error = xfs_trans_alloc(mp, &M_RES(mp)->tr_fsyncts, 0, 0, 0, &tp);
	if (error)
		return error;

	return __xfs_setfilesize(ip, tp, offset, size);
}

141 142
STATIC int
xfs_setfilesize_ioend(
143
	struct iomap_ioend	*ioend,
144
	int			error)
145 146
{
	struct xfs_inode	*ip = XFS_I(ioend->io_inode);
147
	struct xfs_trans	*tp = ioend->io_private;
148 149 150 151 152 153

	/*
	 * The transaction may have been allocated in the I/O submission thread,
	 * thus we need to mark ourselves as being in a transaction manually.
	 * Similarly for freeze protection.
	 */
154
	current_set_flags_nested(&tp->t_pflags, PF_MEMALLOC_NOFS);
155
	__sb_writers_acquired(VFS_I(ip)->i_sb, SB_FREEZE_FS);
156

157
	/* we abort the update if there was an IO error */
158
	if (error) {
159
		xfs_trans_cancel(tp);
160
		return error;
161 162
	}

163
	return __xfs_setfilesize(ip, tp, ioend->io_offset, ioend->io_size);
164 165
}

166
/*
167
 * IO write completion.
168 169
 */
STATIC void
170
xfs_end_ioend(
171
	struct iomap_ioend	*ioend)
172
{
173
	struct xfs_inode	*ip = XFS_I(ioend->io_inode);
174 175
	xfs_off_t		offset = ioend->io_offset;
	size_t			size = ioend->io_size;
C
Christoph Hellwig 已提交
176
	unsigned int		nofs_flag;
177
	int			error;
178

C
Christoph Hellwig 已提交
179 180 181 182 183 184 185
	/*
	 * We can allocate memory here while doing writeback on behalf of
	 * memory reclaim.  To avoid memory allocation deadlocks set the
	 * task-wide nofs context for the following operations.
	 */
	nofs_flag = memalloc_nofs_save();

186
	/*
187
	 * Just clean up the in-memory strutures if the fs has been shut down.
188
	 */
189
	if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
190
		error = -EIO;
191 192
		goto done;
	}
193

194
	/*
195
	 * Clean up any COW blocks on an I/O error.
196
	 */
197
	error = blk_status_to_errno(ioend->io_bio->bi_status);
198
	if (unlikely(error)) {
199
		if (ioend->io_flags & IOMAP_F_SHARED)
200 201
			xfs_reflink_cancel_cow_range(ip, offset, size, true);
		goto done;
202 203
	}

204
	/*
205
	 * Success: commit the COW or unwritten blocks if needed.
206
	 */
207
	if (ioend->io_flags & IOMAP_F_SHARED)
208
		error = xfs_reflink_end_cow(ip, offset, size);
209
	else if (ioend->io_type == IOMAP_UNWRITTEN)
210
		error = xfs_iomap_write_unwritten(ip, offset, size, false);
211
	else
212
		ASSERT(!xfs_ioend_is_append(ioend) || ioend->io_private);
213

214
done:
215
	if (ioend->io_private)
216
		error = xfs_setfilesize_ioend(ioend, error);
217
	iomap_finish_ioends(ioend, error);
C
Christoph Hellwig 已提交
218
	memalloc_nofs_restore(nofs_flag);
219 220
}

221 222 223 224 225 226 227
/*
 * If the to be merged ioend has a preallocated transaction for file
 * size updates we need to ensure the ioend it is merged into also
 * has one.  If it already has one we can simply cancel the transaction
 * as it is guaranteed to be clean.
 */
static void
228
xfs_ioend_merge_private(
229 230
	struct iomap_ioend	*ioend,
	struct iomap_ioend	*next)
231
{
232 233 234
	if (!ioend->io_private) {
		ioend->io_private = next->io_private;
		next->io_private = NULL;
235 236 237 238 239
	} else {
		xfs_setfilesize_ioend(next, -ECANCELED);
	}
}

240 241 242 243 244
/* Finish all pending io completions. */
void
xfs_end_io(
	struct work_struct	*work)
{
245 246
	struct xfs_inode	*ip =
		container_of(work, struct xfs_inode, i_ioend_work);
247
	struct iomap_ioend	*ioend;
248
	struct list_head	tmp;
249 250 251
	unsigned long		flags;

	spin_lock_irqsave(&ip->i_ioend_lock, flags);
252
	list_replace_init(&ip->i_ioend_list, &tmp);
253 254
	spin_unlock_irqrestore(&ip->i_ioend_lock, flags);

255 256
	iomap_sort_ioends(&tmp);
	while ((ioend = list_first_entry_or_null(&tmp, struct iomap_ioend,
257
			io_list))) {
258
		list_del_init(&ioend->io_list);
259
		iomap_ioend_try_merge(ioend, &tmp, xfs_ioend_merge_private);
260 261 262 263
		xfs_end_ioend(ioend);
	}
}

264
static inline bool xfs_ioend_needs_workqueue(struct iomap_ioend *ioend)
265 266 267 268 269 270
{
	return ioend->io_private ||
		ioend->io_type == IOMAP_UNWRITTEN ||
		(ioend->io_flags & IOMAP_F_SHARED);
}

271 272 273
STATIC void
xfs_end_bio(
	struct bio		*bio)
274
{
275
	struct iomap_ioend	*ioend = bio->bi_private;
276 277
	struct xfs_inode	*ip = XFS_I(ioend->io_inode);
	unsigned long		flags;
278

279 280 281 282 283 284 285 286
	ASSERT(xfs_ioend_needs_workqueue(ioend));

	spin_lock_irqsave(&ip->i_ioend_lock, flags);
	if (list_empty(&ip->i_ioend_list))
		WARN_ON_ONCE(!queue_work(ip->i_mount->m_unwritten_workqueue,
					 &ip->i_ioend_work));
	list_add_tail(&ioend->io_list, &ip->i_ioend_list);
	spin_unlock_irqrestore(&ip->i_ioend_lock, flags);
287 288
}

289 290 291 292 293 294
/*
 * Fast revalidation of the cached writeback mapping. Return true if the current
 * mapping is valid, false otherwise.
 */
static bool
xfs_imap_valid(
295
	struct iomap_writepage_ctx	*wpc,
296
	struct xfs_inode		*ip,
297
	loff_t				offset)
298
{
299 300
	if (offset < wpc->iomap.offset ||
	    offset >= wpc->iomap.offset + wpc->iomap.length)
301 302 303 304 305 306
		return false;
	/*
	 * If this is a COW mapping, it is sufficient to check that the mapping
	 * covers the offset. Be careful to check this first because the caller
	 * can revalidate a COW mapping without updating the data seqno.
	 */
307
	if (wpc->iomap.flags & IOMAP_F_SHARED)
308 309 310 311 312 313 314 315 316
		return true;

	/*
	 * This is not a COW mapping. Check the sequence number of the data fork
	 * because concurrent changes could have invalidated the extent. Check
	 * the COW fork because concurrent changes since the last time we
	 * checked (and found nothing at this offset) could have added
	 * overlapping blocks.
	 */
317
	if (XFS_WPC(wpc)->data_seq != READ_ONCE(ip->i_df.if_seq))
318 319
		return false;
	if (xfs_inode_has_cow_data(ip) &&
320
	    XFS_WPC(wpc)->cow_seq != READ_ONCE(ip->i_cowfp->if_seq))
321 322 323 324
		return false;
	return true;
}

325 326
/*
 * Pass in a dellalloc extent and convert it to real extents, return the real
327
 * extent that maps offset_fsb in wpc->iomap.
328 329
 *
 * The current page is held locked so nothing could have removed the block
330 331
 * backing offset_fsb, although it could have moved from the COW to the data
 * fork by another thread.
332 333 334
 */
static int
xfs_convert_blocks(
335
	struct iomap_writepage_ctx *wpc,
336
	struct xfs_inode	*ip,
337
	int			whichfork,
338
	loff_t			offset)
339 340
{
	int			error;
341 342 343 344 345 346
	unsigned		*seq;

	if (whichfork == XFS_COW_FORK)
		seq = &XFS_WPC(wpc)->cow_seq;
	else
		seq = &XFS_WPC(wpc)->data_seq;
347 348

	/*
349 350 351 352
	 * Attempt to allocate whatever delalloc extent currently backs offset
	 * and put the result into wpc->iomap.  Allocate in a loop because it
	 * may take several attempts to allocate real blocks for a contiguous
	 * delalloc extent if free space is sufficiently fragmented.
353 354
	 */
	do {
355
		error = xfs_bmapi_convert_delalloc(ip, whichfork, offset,
356
				&wpc->iomap, seq);
357 358
		if (error)
			return error;
359
	} while (wpc->iomap.offset + wpc->iomap.length <= offset);
360 361 362 363

	return 0;
}

364
static int
L
Linus Torvalds 已提交
365
xfs_map_blocks(
366
	struct iomap_writepage_ctx *wpc,
L
Linus Torvalds 已提交
367
	struct inode		*inode,
C
Christoph Hellwig 已提交
368
	loff_t			offset)
L
Linus Torvalds 已提交
369
{
C
Christoph Hellwig 已提交
370 371
	struct xfs_inode	*ip = XFS_I(inode);
	struct xfs_mount	*mp = ip->i_mount;
F
Fabian Frederick 已提交
372
	ssize_t			count = i_blocksize(inode);
373 374
	xfs_fileoff_t		offset_fsb = XFS_B_TO_FSBT(mp, offset);
	xfs_fileoff_t		end_fsb = XFS_B_TO_FSB(mp, offset + count);
375
	xfs_fileoff_t		cow_fsb = NULLFILEOFF;
376
	int			whichfork = XFS_DATA_FORK;
C
Christoph Hellwig 已提交
377
	struct xfs_bmbt_irec	imap;
378
	struct xfs_iext_cursor	icur;
379
	int			retries = 0;
C
Christoph Hellwig 已提交
380 381
	int			error = 0;

382 383 384
	if (XFS_FORCED_SHUTDOWN(mp))
		return -EIO;

385 386 387 388
	/*
	 * COW fork blocks can overlap data fork blocks even if the blocks
	 * aren't shared.  COW I/O always takes precedent, so we must always
	 * check for overlap on reflink inodes unless the mapping is already a
389 390 391 392 393 394 395 396 397 398
	 * COW one, or the COW fork hasn't changed from the last time we looked
	 * at it.
	 *
	 * It's safe to check the COW fork if_seq here without the ILOCK because
	 * we've indirectly protected against concurrent updates: writeback has
	 * the page locked, which prevents concurrent invalidations by reflink
	 * and directio and prevents concurrent buffered writes to the same
	 * page.  Changes to if_seq always happen under i_lock, which protects
	 * against concurrent updates and provides a memory barrier on the way
	 * out that ensures that we always see the current value.
399
	 */
400
	if (xfs_imap_valid(wpc, ip, offset))
401 402 403 404 405 406 407 408
		return 0;

	/*
	 * If we don't have a valid map, now it's time to get a new one for this
	 * offset.  This will convert delayed allocations (including COW ones)
	 * into real extents.  If we return without a valid map, it means we
	 * landed in a hole and we skip the block.
	 */
409
retry:
410
	xfs_ilock(ip, XFS_ILOCK_SHARED);
C
Christoph Hellwig 已提交
411 412
	ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
	       (ip->i_df.if_flags & XFS_IFEXTENTS));
413 414 415 416 417

	/*
	 * Check if this is offset is covered by a COW extents, and if yes use
	 * it directly instead of looking up anything in the data fork.
	 */
418
	if (xfs_inode_has_cow_data(ip) &&
419 420 421
	    xfs_iext_lookup_extent(ip, ip->i_cowfp, offset_fsb, &icur, &imap))
		cow_fsb = imap.br_startoff;
	if (cow_fsb != NULLFILEOFF && cow_fsb <= offset_fsb) {
422
		XFS_WPC(wpc)->cow_seq = READ_ONCE(ip->i_cowfp->if_seq);
C
Christoph Hellwig 已提交
423
		xfs_iunlock(ip, XFS_ILOCK_SHARED);
424

425
		whichfork = XFS_COW_FORK;
C
Christoph Hellwig 已提交
426 427 428 429
		goto allocate_blocks;
	}

	/*
430 431
	 * No COW extent overlap. Revalidate now that we may have updated
	 * ->cow_seq. If the data mapping is still valid, we're done.
C
Christoph Hellwig 已提交
432
	 */
433
	if (xfs_imap_valid(wpc, ip, offset)) {
C
Christoph Hellwig 已提交
434 435 436 437 438 439 440 441 442
		xfs_iunlock(ip, XFS_ILOCK_SHARED);
		return 0;
	}

	/*
	 * If we don't have a valid map, now it's time to get a new one for this
	 * offset.  This will convert delayed allocations (including COW ones)
	 * into real extents.
	 */
443 444
	if (!xfs_iext_lookup_extent(ip, &ip->i_df, offset_fsb, &icur, &imap))
		imap.br_startoff = end_fsb;	/* fake a hole past EOF */
445
	XFS_WPC(wpc)->data_seq = READ_ONCE(ip->i_df.if_seq);
C
Christoph Hellwig 已提交
446
	xfs_iunlock(ip, XFS_ILOCK_SHARED);
C
Christoph Hellwig 已提交
447

448
	/* landed in a hole or beyond EOF? */
449 450
	if (imap.br_startoff > offset_fsb) {
		imap.br_blockcount = imap.br_startoff - offset_fsb;
C
Christoph Hellwig 已提交
451 452
		imap.br_startoff = offset_fsb;
		imap.br_startblock = HOLESTARTBLOCK;
453
		imap.br_state = XFS_EXT_NORM;
C
Christoph Hellwig 已提交
454
	}
455

456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
	/*
	 * Truncate to the next COW extent if there is one.  This is the only
	 * opportunity to do this because we can skip COW fork lookups for the
	 * subsequent blocks in the mapping; however, the requirement to treat
	 * the COW range separately remains.
	 */
	if (cow_fsb != NULLFILEOFF &&
	    cow_fsb < imap.br_startoff + imap.br_blockcount)
		imap.br_blockcount = cow_fsb - imap.br_startoff;

	/* got a delalloc extent? */
	if (imap.br_startblock != HOLESTARTBLOCK &&
	    isnullstartblock(imap.br_startblock))
		goto allocate_blocks;

471
	xfs_bmbt_to_iomap(ip, &wpc->iomap, &imap, 0);
472
	trace_xfs_map_blocks_found(ip, offset, count, whichfork, &imap);
C
Christoph Hellwig 已提交
473 474
	return 0;
allocate_blocks:
475
	error = xfs_convert_blocks(wpc, ip, whichfork, offset);
476 477 478 479 480 481 482 483
	if (error) {
		/*
		 * If we failed to find the extent in the COW fork we might have
		 * raced with a COW to data fork conversion or truncate.
		 * Restart the lookup to catch the extent in the data fork for
		 * the former case, but prevent additional retries to avoid
		 * looping forever for the latter case.
		 */
484
		if (error == -EAGAIN && whichfork == XFS_COW_FORK && !retries++)
485 486
			goto retry;
		ASSERT(error != -EAGAIN);
C
Christoph Hellwig 已提交
487
		return error;
488
	}
489 490 491 492 493 494

	/*
	 * Due to merging the return real extent might be larger than the
	 * original delalloc one.  Trim the return extent to the next COW
	 * boundary again to force a re-lookup.
	 */
495
	if (whichfork != XFS_COW_FORK && cow_fsb != NULLFILEOFF) {
496 497 498 499 500
		loff_t		cow_offset = XFS_FSB_TO_B(mp, cow_fsb);

		if (cow_offset < wpc->iomap.offset + wpc->iomap.length)
			wpc->iomap.length = cow_offset - wpc->iomap.offset;
	}
501

502 503
	ASSERT(wpc->iomap.offset <= offset);
	ASSERT(wpc->iomap.offset + wpc->iomap.length > offset);
504
	trace_xfs_map_blocks_alloc(ip, offset, count, whichfork, &imap);
C
Christoph Hellwig 已提交
505
	return 0;
L
Linus Torvalds 已提交
506 507
}

508 509 510
static int
xfs_prepare_ioend(
	struct iomap_ioend	*ioend,
511
	int			status)
512
{
C
Christoph Hellwig 已提交
513 514 515 516 517 518 519 520 521
	unsigned int		nofs_flag;

	/*
	 * We can allocate memory here while doing writeback on behalf of
	 * memory reclaim.  To avoid memory allocation deadlocks set the
	 * task-wide nofs context for the following operations.
	 */
	nofs_flag = memalloc_nofs_save();

522
	/* Convert CoW extents to regular */
523
	if (!status && (ioend->io_flags & IOMAP_F_SHARED)) {
524 525 526 527
		status = xfs_reflink_convert_cow(XFS_I(ioend->io_inode),
				ioend->io_offset, ioend->io_size);
	}

528 529
	/* Reserve log space if we might write beyond the on-disk inode size. */
	if (!status &&
530
	    ((ioend->io_flags & IOMAP_F_SHARED) ||
531
	     ioend->io_type != IOMAP_UNWRITTEN) &&
532
	    xfs_ioend_is_append(ioend) &&
533
	    !ioend->io_private)
534
		status = xfs_setfilesize_trans_alloc(ioend);
535

C
Christoph Hellwig 已提交
536 537
	memalloc_nofs_restore(nofs_flag);

538 539 540
	if (xfs_ioend_needs_workqueue(ioend))
		ioend->io_bio->bi_end_io = xfs_end_bio;
	return status;
541 542
}

543
/*
544 545 546
 * If the page has delalloc blocks on it, we need to punch them out before we
 * invalidate the page.  If we don't, we leave a stale delalloc mapping on the
 * inode that can trip up a later direct I/O read operation on the same region.
547
 *
548 549 550 551 552
 * We prevent this by truncating away the delalloc regions on the page.  Because
 * they are delalloc, we can do this without needing a transaction. Indeed - if
 * we get ENOSPC errors, we have to be able to do this truncation without a
 * transaction as there is no space left for block reservation (typically why we
 * see a ENOSPC in writeback).
553
 */
554 555
static void
xfs_discard_page(
556 557 558 559
	struct page		*page)
{
	struct inode		*inode = page->mapping->host;
	struct xfs_inode	*ip = XFS_I(inode);
560
	struct xfs_mount	*mp = ip->i_mount;
561
	loff_t			offset = page_offset(page);
562 563
	xfs_fileoff_t		start_fsb = XFS_B_TO_FSBT(mp, offset);
	int			error;
564

565
	if (XFS_FORCED_SHUTDOWN(mp))
566 567
		goto out_invalidate;

568
	xfs_alert(mp,
569
		"page discard on page "PTR_FMT", inode 0x%llx, offset %llu.",
570 571
			page, ip->i_ino, offset);

572 573 574 575
	error = xfs_bmap_punch_delalloc_range(ip, start_fsb,
			PAGE_SIZE / i_blocksize(inode));
	if (error && !XFS_FORCED_SHUTDOWN(mp))
		xfs_alert(mp, "page discard unable to remove delalloc mapping.");
576
out_invalidate:
577
	iomap_invalidatepage(page, 0, PAGE_SIZE);
578 579
}

580 581 582 583 584
static const struct iomap_writeback_ops xfs_writeback_ops = {
	.map_blocks		= xfs_map_blocks,
	.prepare_ioend		= xfs_prepare_ioend,
	.discard_page		= xfs_discard_page,
};
585

586 587 588 589 590
STATIC int
xfs_vm_writepage(
	struct page		*page,
	struct writeback_control *wbc)
{
591
	struct xfs_writepage_ctx wpc = { };
592

593
	return iomap_writepage(page, wbc, &wpc.ctx, &xfs_writeback_ops);
594 595
}

596 597 598 599 600
STATIC int
xfs_vm_writepages(
	struct address_space	*mapping,
	struct writeback_control *wbc)
{
601
	struct xfs_writepage_ctx wpc = { };
602

603
	xfs_iflags_clear(XFS_I(mapping->host), XFS_ITRUNCATED);
604
	return iomap_writepages(mapping, wbc, &wpc.ctx, &xfs_writeback_ops);
605 606
}

D
Dan Williams 已提交
607 608 609 610 611 612 613 614 615 616
STATIC int
xfs_dax_writepages(
	struct address_space	*mapping,
	struct writeback_control *wbc)
{
	xfs_iflags_clear(XFS_I(mapping->host), XFS_ITRUNCATED);
	return dax_writeback_mapping_range(mapping,
			xfs_find_bdev_for_inode(mapping->host), wbc);
}

L
Linus Torvalds 已提交
617
STATIC sector_t
618
xfs_vm_bmap(
L
Linus Torvalds 已提交
619 620 621
	struct address_space	*mapping,
	sector_t		block)
{
C
Christoph Hellwig 已提交
622
	struct xfs_inode	*ip = XFS_I(mapping->host);
L
Linus Torvalds 已提交
623

C
Christoph Hellwig 已提交
624
	trace_xfs_vm_bmap(ip);
625 626 627

	/*
	 * The swap code (ab-)uses ->bmap to get a block mapping and then
628
	 * bypasses the file system for actual I/O.  We really can't allow
629
	 * that on reflinks inodes, so we have to skip out here.  And yes,
630 631 632 633
	 * 0 is the magic code for a bmap error.
	 *
	 * Since we don't pass back blockdev info, we can't return bmap
	 * information for rt files either.
634
	 */
635
	if (xfs_is_cow_inode(ip) || XFS_IS_REALTIME_INODE(ip))
636
		return 0;
C
Christoph Hellwig 已提交
637
	return iomap_bmap(mapping, block, &xfs_iomap_ops);
L
Linus Torvalds 已提交
638 639 640
}

STATIC int
641
xfs_vm_readpage(
L
Linus Torvalds 已提交
642 643 644
	struct file		*unused,
	struct page		*page)
{
645
	return iomap_readpage(page, &xfs_iomap_ops);
L
Linus Torvalds 已提交
646 647 648
}

STATIC int
649
xfs_vm_readpages(
L
Linus Torvalds 已提交
650 651 652 653 654
	struct file		*unused,
	struct address_space	*mapping,
	struct list_head	*pages,
	unsigned		nr_pages)
{
655
	return iomap_readpages(mapping, pages, nr_pages, &xfs_iomap_ops);
656 657
}

658 659 660 661 662 663 664 665 666 667
static int
xfs_iomap_swapfile_activate(
	struct swap_info_struct		*sis,
	struct file			*swap_file,
	sector_t			*span)
{
	sis->bdev = xfs_find_bdev_for_inode(file_inode(swap_file));
	return iomap_swapfile_activate(sis, swap_file, span, &xfs_iomap_ops);
}

668
const struct address_space_operations xfs_address_space_operations = {
669 670 671
	.readpage		= xfs_vm_readpage,
	.readpages		= xfs_vm_readpages,
	.writepage		= xfs_vm_writepage,
672
	.writepages		= xfs_vm_writepages,
673
	.set_page_dirty		= iomap_set_page_dirty,
674 675
	.releasepage		= iomap_releasepage,
	.invalidatepage		= iomap_invalidatepage,
676
	.bmap			= xfs_vm_bmap,
D
Dan Williams 已提交
677
	.direct_IO		= noop_direct_IO,
678 679
	.migratepage		= iomap_migrate_page,
	.is_partially_uptodate  = iomap_is_partially_uptodate,
680
	.error_remove_page	= generic_error_remove_page,
681
	.swap_activate		= xfs_iomap_swapfile_activate,
L
Linus Torvalds 已提交
682
};
D
Dan Williams 已提交
683 684 685 686 687 688

const struct address_space_operations xfs_dax_aops = {
	.writepages		= xfs_dax_writepages,
	.direct_IO		= noop_direct_IO,
	.set_page_dirty		= noop_set_page_dirty,
	.invalidatepage		= noop_invalidatepage,
689
	.swap_activate		= xfs_iomap_swapfile_activate,
D
Dan Williams 已提交
690
};