xfs_aops.c 18.2 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);
}

C
Christoph Hellwig 已提交
33 34 35
/*
 * Fast and loose check if this write could update the on-disk inode size.
 */
36
static inline bool xfs_ioend_is_append(struct iomap_ioend *ioend)
C
Christoph Hellwig 已提交
37 38 39 40 41
{
	return ioend->io_offset + ioend->io_size >
		XFS_I(ioend->io_inode)->i_d.di_size;
}

42 43
STATIC int
xfs_setfilesize_trans_alloc(
44
	struct iomap_ioend	*ioend)
45 46 47 48 49
{
	struct xfs_mount	*mp = XFS_I(ioend->io_inode)->i_mount;
	struct xfs_trans	*tp;
	int			error;

C
Christoph Hellwig 已提交
50
	error = xfs_trans_alloc(mp, &M_RES(mp)->tr_fsyncts, 0, 0, 0, &tp);
51
	if (error)
52 53
		return error;

54
	ioend->io_private = tp;
55

J
Jan Kara 已提交
56
	/*
57
	 * We may pass freeze protection with a transaction.  So tell lockdep
J
Jan Kara 已提交
58 59
	 * we released it.
	 */
60
	__sb_writers_release(ioend->io_inode->i_sb, SB_FREEZE_FS);
61 62 63 64
	/*
	 * We hand off the transaction to the completion thread now, so
	 * clear the flag here.
	 */
65
	current_restore_flags_nested(&tp->t_pflags, PF_MEMALLOC_NOFS);
66 67 68
	return 0;
}

69
/*
70
 * Update on-disk file size now that data has been written to disk.
71
 */
72
STATIC int
73
__xfs_setfilesize(
74 75 76 77
	struct xfs_inode	*ip,
	struct xfs_trans	*tp,
	xfs_off_t		offset,
	size_t			size)
78 79 80
{
	xfs_fsize_t		isize;

81
	xfs_ilock(ip, XFS_ILOCK_EXCL);
82
	isize = xfs_new_eof(ip, offset + size);
83 84
	if (!isize) {
		xfs_iunlock(ip, XFS_ILOCK_EXCL);
85
		xfs_trans_cancel(tp);
86
		return 0;
87 88
	}

89
	trace_xfs_setfilesize(ip, offset, size);
90 91 92 93 94

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

95
	return xfs_trans_commit(tp);
96 97
}

98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
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);
}

115 116
STATIC int
xfs_setfilesize_ioend(
117
	struct iomap_ioend	*ioend,
118
	int			error)
119 120
{
	struct xfs_inode	*ip = XFS_I(ioend->io_inode);
121
	struct xfs_trans	*tp = ioend->io_private;
122 123 124 125 126 127

	/*
	 * 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.
	 */
128
	current_set_flags_nested(&tp->t_pflags, PF_MEMALLOC_NOFS);
129
	__sb_writers_acquired(VFS_I(ip)->i_sb, SB_FREEZE_FS);
130

131
	/* we abort the update if there was an IO error */
132
	if (error) {
133
		xfs_trans_cancel(tp);
134
		return error;
135 136
	}

137
	return __xfs_setfilesize(ip, tp, ioend->io_offset, ioend->io_size);
138 139
}

140
/*
141
 * IO write completion.
142 143
 */
STATIC void
144
xfs_end_ioend(
145
	struct iomap_ioend	*ioend)
146
{
147
	struct xfs_inode	*ip = XFS_I(ioend->io_inode);
148 149
	xfs_off_t		offset = ioend->io_offset;
	size_t			size = ioend->io_size;
C
Christoph Hellwig 已提交
150
	unsigned int		nofs_flag;
151
	int			error;
152

C
Christoph Hellwig 已提交
153 154 155 156 157 158 159
	/*
	 * 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();

160
	/*
161
	 * Just clean up the in-memory strutures if the fs has been shut down.
162
	 */
163
	if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
164
		error = -EIO;
165 166
		goto done;
	}
167

168
	/*
169
	 * Clean up any COW blocks on an I/O error.
170
	 */
171
	error = blk_status_to_errno(ioend->io_bio->bi_status);
172
	if (unlikely(error)) {
173
		if (ioend->io_flags & IOMAP_F_SHARED)
174 175
			xfs_reflink_cancel_cow_range(ip, offset, size, true);
		goto done;
176 177
	}

178
	/*
179
	 * Success: commit the COW or unwritten blocks if needed.
180
	 */
181
	if (ioend->io_flags & IOMAP_F_SHARED)
182
		error = xfs_reflink_end_cow(ip, offset, size);
183
	else if (ioend->io_type == IOMAP_UNWRITTEN)
184
		error = xfs_iomap_write_unwritten(ip, offset, size, false);
185
	else
186
		ASSERT(!xfs_ioend_is_append(ioend) || ioend->io_private);
187

188
done:
189
	if (ioend->io_private)
190
		error = xfs_setfilesize_ioend(ioend, error);
191
	iomap_finish_ioends(ioend, error);
C
Christoph Hellwig 已提交
192
	memalloc_nofs_restore(nofs_flag);
193 194
}

195 196 197 198 199 200 201
/*
 * 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
202
xfs_ioend_merge_private(
203 204
	struct iomap_ioend	*ioend,
	struct iomap_ioend	*next)
205
{
206 207 208
	if (!ioend->io_private) {
		ioend->io_private = next->io_private;
		next->io_private = NULL;
209 210 211 212 213
	} else {
		xfs_setfilesize_ioend(next, -ECANCELED);
	}
}

214 215 216 217 218
/* Finish all pending io completions. */
void
xfs_end_io(
	struct work_struct	*work)
{
219 220
	struct xfs_inode	*ip =
		container_of(work, struct xfs_inode, i_ioend_work);
221
	struct iomap_ioend	*ioend;
222
	struct list_head	tmp;
223 224 225
	unsigned long		flags;

	spin_lock_irqsave(&ip->i_ioend_lock, flags);
226
	list_replace_init(&ip->i_ioend_list, &tmp);
227 228
	spin_unlock_irqrestore(&ip->i_ioend_lock, flags);

229 230
	iomap_sort_ioends(&tmp);
	while ((ioend = list_first_entry_or_null(&tmp, struct iomap_ioend,
231
			io_list))) {
232
		list_del_init(&ioend->io_list);
233
		iomap_ioend_try_merge(ioend, &tmp, xfs_ioend_merge_private);
234 235 236 237
		xfs_end_ioend(ioend);
	}
}

238
static inline bool xfs_ioend_needs_workqueue(struct iomap_ioend *ioend)
239 240 241 242 243 244
{
	return ioend->io_private ||
		ioend->io_type == IOMAP_UNWRITTEN ||
		(ioend->io_flags & IOMAP_F_SHARED);
}

245 246 247
STATIC void
xfs_end_bio(
	struct bio		*bio)
248
{
249
	struct iomap_ioend	*ioend = bio->bi_private;
250 251
	struct xfs_inode	*ip = XFS_I(ioend->io_inode);
	unsigned long		flags;
252

253 254 255 256 257 258 259 260
	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);
261 262
}

263 264 265 266 267 268
/*
 * Fast revalidation of the cached writeback mapping. Return true if the current
 * mapping is valid, false otherwise.
 */
static bool
xfs_imap_valid(
269
	struct iomap_writepage_ctx	*wpc,
270
	struct xfs_inode		*ip,
271
	loff_t				offset)
272
{
273 274
	if (offset < wpc->iomap.offset ||
	    offset >= wpc->iomap.offset + wpc->iomap.length)
275 276 277 278 279 280
		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.
	 */
281
	if (wpc->iomap.flags & IOMAP_F_SHARED)
282 283 284 285 286 287 288 289 290
		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.
	 */
291
	if (XFS_WPC(wpc)->data_seq != READ_ONCE(ip->i_df.if_seq))
292 293
		return false;
	if (xfs_inode_has_cow_data(ip) &&
294
	    XFS_WPC(wpc)->cow_seq != READ_ONCE(ip->i_cowfp->if_seq))
295 296 297 298
		return false;
	return true;
}

299 300
/*
 * Pass in a dellalloc extent and convert it to real extents, return the real
301
 * extent that maps offset_fsb in wpc->iomap.
302 303
 *
 * The current page is held locked so nothing could have removed the block
304 305
 * backing offset_fsb, although it could have moved from the COW to the data
 * fork by another thread.
306 307 308
 */
static int
xfs_convert_blocks(
309
	struct iomap_writepage_ctx *wpc,
310
	struct xfs_inode	*ip,
311
	int			whichfork,
312
	loff_t			offset)
313 314
{
	int			error;
315 316 317 318 319 320
	unsigned		*seq;

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

	/*
323 324 325 326
	 * 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.
327 328
	 */
	do {
329
		error = xfs_bmapi_convert_delalloc(ip, whichfork, offset,
330
				&wpc->iomap, seq);
331 332
		if (error)
			return error;
333
	} while (wpc->iomap.offset + wpc->iomap.length <= offset);
334 335 336 337

	return 0;
}

338
static int
L
Linus Torvalds 已提交
339
xfs_map_blocks(
340
	struct iomap_writepage_ctx *wpc,
L
Linus Torvalds 已提交
341
	struct inode		*inode,
C
Christoph Hellwig 已提交
342
	loff_t			offset)
L
Linus Torvalds 已提交
343
{
C
Christoph Hellwig 已提交
344 345
	struct xfs_inode	*ip = XFS_I(inode);
	struct xfs_mount	*mp = ip->i_mount;
F
Fabian Frederick 已提交
346
	ssize_t			count = i_blocksize(inode);
347 348
	xfs_fileoff_t		offset_fsb = XFS_B_TO_FSBT(mp, offset);
	xfs_fileoff_t		end_fsb = XFS_B_TO_FSB(mp, offset + count);
349
	xfs_fileoff_t		cow_fsb = NULLFILEOFF;
350
	int			whichfork = XFS_DATA_FORK;
C
Christoph Hellwig 已提交
351
	struct xfs_bmbt_irec	imap;
352
	struct xfs_iext_cursor	icur;
353
	int			retries = 0;
C
Christoph Hellwig 已提交
354 355
	int			error = 0;

356 357 358
	if (XFS_FORCED_SHUTDOWN(mp))
		return -EIO;

359 360 361 362
	/*
	 * 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
363 364 365 366 367 368 369 370 371 372
	 * 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.
373
	 */
374
	if (xfs_imap_valid(wpc, ip, offset))
375 376 377 378 379 380 381 382
		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.
	 */
383
retry:
384
	xfs_ilock(ip, XFS_ILOCK_SHARED);
385
	ASSERT(ip->i_df.if_format != XFS_DINODE_FMT_BTREE ||
C
Christoph Hellwig 已提交
386
	       (ip->i_df.if_flags & XFS_IFEXTENTS));
387 388 389 390 391

	/*
	 * 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.
	 */
392
	if (xfs_inode_has_cow_data(ip) &&
393 394 395
	    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) {
396
		XFS_WPC(wpc)->cow_seq = READ_ONCE(ip->i_cowfp->if_seq);
C
Christoph Hellwig 已提交
397
		xfs_iunlock(ip, XFS_ILOCK_SHARED);
398

399
		whichfork = XFS_COW_FORK;
C
Christoph Hellwig 已提交
400 401 402 403
		goto allocate_blocks;
	}

	/*
404 405
	 * 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 已提交
406
	 */
407
	if (xfs_imap_valid(wpc, ip, offset)) {
C
Christoph Hellwig 已提交
408 409 410 411 412 413 414 415 416
		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.
	 */
417 418
	if (!xfs_iext_lookup_extent(ip, &ip->i_df, offset_fsb, &icur, &imap))
		imap.br_startoff = end_fsb;	/* fake a hole past EOF */
419
	XFS_WPC(wpc)->data_seq = READ_ONCE(ip->i_df.if_seq);
C
Christoph Hellwig 已提交
420
	xfs_iunlock(ip, XFS_ILOCK_SHARED);
C
Christoph Hellwig 已提交
421

422
	/* landed in a hole or beyond EOF? */
423 424
	if (imap.br_startoff > offset_fsb) {
		imap.br_blockcount = imap.br_startoff - offset_fsb;
C
Christoph Hellwig 已提交
425 426
		imap.br_startoff = offset_fsb;
		imap.br_startblock = HOLESTARTBLOCK;
427
		imap.br_state = XFS_EXT_NORM;
C
Christoph Hellwig 已提交
428
	}
429

430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
	/*
	 * 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;

445
	xfs_bmbt_to_iomap(ip, &wpc->iomap, &imap, 0);
446
	trace_xfs_map_blocks_found(ip, offset, count, whichfork, &imap);
C
Christoph Hellwig 已提交
447 448
	return 0;
allocate_blocks:
449
	error = xfs_convert_blocks(wpc, ip, whichfork, offset);
450 451 452 453 454 455 456 457
	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.
		 */
458
		if (error == -EAGAIN && whichfork == XFS_COW_FORK && !retries++)
459 460
			goto retry;
		ASSERT(error != -EAGAIN);
C
Christoph Hellwig 已提交
461
		return error;
462
	}
463 464 465 466 467 468

	/*
	 * 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.
	 */
469
	if (whichfork != XFS_COW_FORK && cow_fsb != NULLFILEOFF) {
470 471 472 473 474
		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;
	}
475

476 477
	ASSERT(wpc->iomap.offset <= offset);
	ASSERT(wpc->iomap.offset + wpc->iomap.length > offset);
478
	trace_xfs_map_blocks_alloc(ip, offset, count, whichfork, &imap);
C
Christoph Hellwig 已提交
479
	return 0;
L
Linus Torvalds 已提交
480 481
}

482 483 484
static int
xfs_prepare_ioend(
	struct iomap_ioend	*ioend,
485
	int			status)
486
{
C
Christoph Hellwig 已提交
487 488 489 490 491 492 493 494 495
	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();

496
	/* Convert CoW extents to regular */
497
	if (!status && (ioend->io_flags & IOMAP_F_SHARED)) {
498 499 500 501
		status = xfs_reflink_convert_cow(XFS_I(ioend->io_inode),
				ioend->io_offset, ioend->io_size);
	}

502 503
	/* Reserve log space if we might write beyond the on-disk inode size. */
	if (!status &&
504
	    ((ioend->io_flags & IOMAP_F_SHARED) ||
505
	     ioend->io_type != IOMAP_UNWRITTEN) &&
506
	    xfs_ioend_is_append(ioend) &&
507
	    !ioend->io_private)
508
		status = xfs_setfilesize_trans_alloc(ioend);
509

C
Christoph Hellwig 已提交
510 511
	memalloc_nofs_restore(nofs_flag);

512 513 514
	if (xfs_ioend_needs_workqueue(ioend))
		ioend->io_bio->bi_end_io = xfs_end_bio;
	return status;
515 516
}

517
/*
518 519 520
 * 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.
521
 *
522 523 524 525 526
 * 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).
527
 */
528 529
static void
xfs_discard_page(
530 531
	struct page		*page,
	loff_t			fileoff)
532 533 534
{
	struct inode		*inode = page->mapping->host;
	struct xfs_inode	*ip = XFS_I(inode);
535
	struct xfs_mount	*mp = ip->i_mount;
536 537 538
	unsigned int		pageoff = offset_in_page(fileoff);
	xfs_fileoff_t		start_fsb = XFS_B_TO_FSBT(mp, fileoff);
	xfs_fileoff_t		pageoff_fsb = XFS_B_TO_FSBT(mp, pageoff);
539
	int			error;
540

541
	if (XFS_FORCED_SHUTDOWN(mp))
542 543
		goto out_invalidate;

544
	xfs_alert_ratelimited(mp,
545
		"page discard on page "PTR_FMT", inode 0x%llx, offset %llu.",
546
			page, ip->i_ino, fileoff);
547

548
	error = xfs_bmap_punch_delalloc_range(ip, start_fsb,
549
			i_blocks_per_page(inode, page) - pageoff_fsb);
550 551
	if (error && !XFS_FORCED_SHUTDOWN(mp))
		xfs_alert(mp, "page discard unable to remove delalloc mapping.");
552
out_invalidate:
553
	iomap_invalidatepage(page, pageoff, PAGE_SIZE - pageoff);
554 555
}

556 557 558 559 560
static const struct iomap_writeback_ops xfs_writeback_ops = {
	.map_blocks		= xfs_map_blocks,
	.prepare_ioend		= xfs_prepare_ioend,
	.discard_page		= xfs_discard_page,
};
561

562 563 564 565 566
STATIC int
xfs_vm_writepage(
	struct page		*page,
	struct writeback_control *wbc)
{
567
	struct xfs_writepage_ctx wpc = { };
568

569
	return iomap_writepage(page, wbc, &wpc.ctx, &xfs_writeback_ops);
570 571
}

572 573 574 575 576
STATIC int
xfs_vm_writepages(
	struct address_space	*mapping,
	struct writeback_control *wbc)
{
577
	struct xfs_writepage_ctx wpc = { };
578

579
	xfs_iflags_clear(XFS_I(mapping->host), XFS_ITRUNCATED);
580
	return iomap_writepages(mapping, wbc, &wpc.ctx, &xfs_writeback_ops);
581 582
}

D
Dan Williams 已提交
583 584 585 586 587
STATIC int
xfs_dax_writepages(
	struct address_space	*mapping,
	struct writeback_control *wbc)
{
588 589 590
	struct xfs_inode	*ip = XFS_I(mapping->host);

	xfs_iflags_clear(ip, XFS_ITRUNCATED);
D
Dan Williams 已提交
591
	return dax_writeback_mapping_range(mapping,
592
			xfs_inode_buftarg(ip)->bt_daxdev, wbc);
D
Dan Williams 已提交
593 594
}

L
Linus Torvalds 已提交
595
STATIC sector_t
596
xfs_vm_bmap(
L
Linus Torvalds 已提交
597 598 599
	struct address_space	*mapping,
	sector_t		block)
{
C
Christoph Hellwig 已提交
600
	struct xfs_inode	*ip = XFS_I(mapping->host);
L
Linus Torvalds 已提交
601

C
Christoph Hellwig 已提交
602
	trace_xfs_vm_bmap(ip);
603 604 605

	/*
	 * The swap code (ab-)uses ->bmap to get a block mapping and then
606
	 * bypasses the file system for actual I/O.  We really can't allow
607
	 * that on reflinks inodes, so we have to skip out here.  And yes,
608 609 610 611
	 * 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.
612
	 */
613
	if (xfs_is_cow_inode(ip) || XFS_IS_REALTIME_INODE(ip))
614
		return 0;
615
	return iomap_bmap(mapping, block, &xfs_read_iomap_ops);
L
Linus Torvalds 已提交
616 617 618
}

STATIC int
619
xfs_vm_readpage(
L
Linus Torvalds 已提交
620 621 622
	struct file		*unused,
	struct page		*page)
{
623
	return iomap_readpage(page, &xfs_read_iomap_ops);
L
Linus Torvalds 已提交
624 625
}

626 627 628
STATIC void
xfs_vm_readahead(
	struct readahead_control	*rac)
L
Linus Torvalds 已提交
629
{
630
	iomap_readahead(rac, &xfs_read_iomap_ops);
631 632
}

633 634 635 636 637 638
static int
xfs_iomap_swapfile_activate(
	struct swap_info_struct		*sis,
	struct file			*swap_file,
	sector_t			*span)
{
639
	sis->bdev = xfs_inode_buftarg(XFS_I(file_inode(swap_file)))->bt_bdev;
640 641
	return iomap_swapfile_activate(sis, swap_file, span,
			&xfs_read_iomap_ops);
642 643
}

644
const struct address_space_operations xfs_address_space_operations = {
645
	.readpage		= xfs_vm_readpage,
646
	.readahead		= xfs_vm_readahead,
647
	.writepage		= xfs_vm_writepage,
648
	.writepages		= xfs_vm_writepages,
649
	.set_page_dirty		= iomap_set_page_dirty,
650 651
	.releasepage		= iomap_releasepage,
	.invalidatepage		= iomap_invalidatepage,
652
	.bmap			= xfs_vm_bmap,
D
Dan Williams 已提交
653
	.direct_IO		= noop_direct_IO,
654 655
	.migratepage		= iomap_migrate_page,
	.is_partially_uptodate  = iomap_is_partially_uptodate,
656
	.error_remove_page	= generic_error_remove_page,
657
	.swap_activate		= xfs_iomap_swapfile_activate,
L
Linus Torvalds 已提交
658
};
D
Dan Williams 已提交
659 660 661 662 663 664

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,
665
	.swap_activate		= xfs_iomap_swapfile_activate,
D
Dan Williams 已提交
666
};