summaryrefslogtreecommitdiff
path: root/fs/xfs/libxfs/xfs_swapext.c
blob: 186d20dc2f155223a87dcef4ca274840cf74de76 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Copyright (C) 2020 Oracle.  All Rights Reserved.
 * Author: Darrick J. Wong <darrick.wong@oracle.com>
 */
#include "xfs.h"
#include "xfs_fs.h"
#include "xfs_shared.h"
#include "xfs_format.h"
#include "xfs_log_format.h"
#include "xfs_trans_resv.h"
#include "xfs_mount.h"
#include "xfs_defer.h"
#include "xfs_inode.h"
#include "xfs_trans.h"
#include "xfs_bmap.h"
#include "xfs_icache.h"
#include "xfs_quota.h"
#include "xfs_swapext.h"
#include "xfs_trace.h"
#include "xfs_bmap_btree.h"
#include "xfs_trans_space.h"
#include "xfs_errortag.h"
#include "xfs_error.h"
#include "xfs_da_format.h"
#include "xfs_da_btree.h"
#include "xfs_attr_leaf.h"
#include "xfs_dir2_priv.h"

/* Information to help us reset reflink flag / CoW fork state after a swap. */

/* Are we swapping the data fork? */
#define XFS_SX_REFLINK_DATAFORK		(1U << 0)

/* Can we swap the flags? */
#define XFS_SX_REFLINK_SWAPFLAGS	(1U << 1)

/* Previous state of the two inodes' reflink flags. */
#define XFS_SX_REFLINK_IP1_REFLINK	(1U << 2)
#define XFS_SX_REFLINK_IP2_REFLINK	(1U << 3)


/*
 * Prepare both inodes' reflink state for an extent swap, and return our
 * findings so that xfs_swapext_reflink_finish can deal with the aftermath.
 */
unsigned int
xfs_swapext_reflink_prep(
	const struct xfs_swapext_req	*req)
{
	struct xfs_mount		*mp = req->ip1->i_mount;
	unsigned int			rs = 0;

	if (req->whichfork != XFS_DATA_FORK)
		return 0;

	/*
	 * If either file has shared blocks and we're swapping data forks, we
	 * must flag the other file as having shared blocks so that we get the
	 * shared-block rmap functions if we need to fix up the rmaps.  The
	 * flags will be switched for real by xfs_swapext_reflink_finish.
	 */
	if (xfs_is_reflink_inode(req->ip1))
		rs |= XFS_SX_REFLINK_IP1_REFLINK;
	if (xfs_is_reflink_inode(req->ip2))
		rs |= XFS_SX_REFLINK_IP2_REFLINK;

	if (rs & XFS_SX_REFLINK_IP1_REFLINK)
		req->ip2->i_d.di_flags2 |= XFS_DIFLAG2_REFLINK;
	if (rs & XFS_SX_REFLINK_IP2_REFLINK)
		req->ip1->i_d.di_flags2 |= XFS_DIFLAG2_REFLINK;

	/*
	 * If either file had the reflink flag set before; and the two files'
	 * reflink state was different; and we're swapping the entirety of both
	 * files, then we can exchange the reflink flags at the end.
	 * Otherwise, we propagate the reflink flag from either file to the
	 * other file.
	 *
	 * Note that we've only set the _REFLINK flags of the reflink state, so
	 * we can cheat and use hweight32 for the reflink flag test.
	 *
	 */
	if (hweight32(rs) == 1 && req->startoff1 == 0 && req->startoff2 == 0 &&
	    req->blockcount == XFS_B_TO_FSB(mp, req->ip1->i_d.di_size) &&
	    req->blockcount == XFS_B_TO_FSB(mp, req->ip2->i_d.di_size))
		rs |= XFS_SX_REFLINK_SWAPFLAGS;

	rs |= XFS_SX_REFLINK_DATAFORK;
	return rs;
}

/*
 * If the reflink flag is set on either inode, make sure it has an incore CoW
 * fork, since all reflink inodes must have them.  If there's a CoW fork and it
 * has extents in it, make sure the inodes are tagged appropriately so that
 * speculative preallocations can be GC'd if we run low of space.
 */
static inline void
xfs_swapext_ensure_cowfork(
	struct xfs_inode	*ip)
{
	struct xfs_ifork	*cfork;

	if (xfs_is_reflink_inode(ip))
		xfs_ifork_init_cow(ip);

	cfork = XFS_IFORK_PTR(ip, XFS_COW_FORK);
	if (!cfork)
		return;
	if (cfork->if_bytes > 0)
		xfs_inode_set_cowblocks_tag(ip);
	else
		xfs_inode_clear_cowblocks_tag(ip);
}

/*
 * Set both inodes' ondisk reflink flags to their final state and ensure that
 * the incore state is ready to go.
 */
void
xfs_swapext_reflink_finish(
	struct xfs_trans		*tp,
	const struct xfs_swapext_req	*req,
	unsigned int			rs)
{
	if (!(rs & XFS_SX_REFLINK_DATAFORK))
		return;

	if (rs & XFS_SX_REFLINK_SWAPFLAGS) {
		/* Exchange the reflink inode flags and log them. */
		req->ip1->i_d.di_flags2 &= ~XFS_DIFLAG2_REFLINK;
		if (rs & XFS_SX_REFLINK_IP2_REFLINK)
			req->ip1->i_d.di_flags2 |= XFS_DIFLAG2_REFLINK;

		req->ip2->i_d.di_flags2 &= ~XFS_DIFLAG2_REFLINK;
		if (rs & XFS_SX_REFLINK_IP1_REFLINK)
			req->ip2->i_d.di_flags2 |= XFS_DIFLAG2_REFLINK;

		xfs_trans_log_inode(tp, req->ip1, XFS_ILOG_CORE);
		xfs_trans_log_inode(tp, req->ip2, XFS_ILOG_CORE);
	}

	xfs_swapext_ensure_cowfork(req->ip1);
	xfs_swapext_ensure_cowfork(req->ip2);
}

/* Schedule an atomic extent swap. */
static inline void
xfs_swapext_schedule(
	struct xfs_trans		*tp,
	struct xfs_swapext_intent	*sxi)
{
	trace_xfs_swapext_defer(tp->t_mountp, sxi);
	xfs_defer_add(tp, XFS_DEFER_OPS_TYPE_SWAPEXT, &sxi->sxi_list);
}

/* Reschedule an atomic extent swap on behalf of log recovery. */
void
xfs_swapext_reschedule(
	struct xfs_trans		*tp,
	const struct xfs_swapext_intent	*sxi)
{
	struct xfs_swapext_intent	*new_sxi;

	new_sxi = kmem_alloc(sizeof(struct xfs_swapext_intent), KM_NOFS);
	memcpy(new_sxi, sxi, sizeof(*new_sxi));
	INIT_LIST_HEAD(&new_sxi->sxi_list);

	xfs_swapext_schedule(tp, new_sxi);
}

/*
 * Adjust the on-disk inode size upwards if needed so that we never map extents
 * into the file past EOF.  This is crucial so that log recovery won't get
 * confused by the sudden appearance of post-eof extents.
 */
STATIC void
xfs_swapext_update_size(
	struct xfs_trans	*tp,
	struct xfs_inode	*ip,
	struct xfs_bmbt_irec	*imap,
	xfs_fsize_t		new_isize)
{
	struct xfs_mount	*mp = tp->t_mountp;
	xfs_fsize_t		len;

	if (new_isize < 0)
		return;

	len = min(XFS_FSB_TO_B(mp, imap->br_startoff + imap->br_blockcount),
		  new_isize);

	if (len <= ip->i_d.di_size)
		return;

	trace_xfs_swapext_update_inode_size(ip, len);

	ip->i_d.di_size = len;
	xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
}

/* Convert inode2's leaf attr fork back to shortform, if possible.. */
STATIC int
xfs_swapext_attr_to_shortform2(
	struct xfs_trans		*tp,
	struct xfs_swapext_intent	*sxi)
{
	struct xfs_da_args	args = {
		.dp		= sxi->sxi_ip2,
		.geo		= tp->t_mountp->m_attr_geo,
		.whichfork	= XFS_ATTR_FORK,
		.trans		= tp,
	};
	struct xfs_buf		*bp;
	int			forkoff;
	int			error;

	if (!xfs_bmap_one_block(sxi->sxi_ip2, XFS_ATTR_FORK))
		return 0;

	error = xfs_attr3_leaf_read(tp, sxi->sxi_ip2, 0, &bp);
	if (error)
		return error;

	forkoff = xfs_attr_shortform_allfit(bp, sxi->sxi_ip2);
	if (forkoff == 0)
		return 0;

	return xfs_attr3_leaf_to_shortform(bp, &args, forkoff);
}

#define XFS_SWAP_EXTENT_POST_PROCESSING (XFS_SWAP_EXTENT_INO2_SHORTFORM)

/* Convert inode2's block dir fork back to shortform, if possible.. */
STATIC int
xfs_swapext_dir_to_shortform2(
	struct xfs_trans		*tp,
	struct xfs_swapext_intent	*sxi)
{
	struct xfs_da_args	args = {
		.dp		= sxi->sxi_ip2,
		.geo		= tp->t_mountp->m_dir_geo,
		.whichfork	= XFS_DATA_FORK,
		.trans		= tp,
	};
	struct xfs_dir2_sf_hdr	sfh;
	struct xfs_buf		*bp;
	int			size;
	int			error;

	if (!xfs_bmap_one_block(sxi->sxi_ip2, XFS_DATA_FORK))
		return 0;

	error = xfs_dir3_block_read(tp, sxi->sxi_ip2, &bp);
	if (error)
		return error;

	size = xfs_dir2_block_sfsize(sxi->sxi_ip2, bp->b_addr, &sfh);
	if (size > XFS_IFORK_DSIZE(sxi->sxi_ip2))
		return 0;

	return xfs_dir2_block_to_sf(&args, bp, size, &sfh);
}

/* Do we have more work to do to finish this operation? */
bool
xfs_swapext_has_more_work(
	struct xfs_swapext_intent	*sxi)
{
	return sxi->sxi_blockcount > 0 ||
		(sxi->sxi_flags & XFS_SWAP_EXTENT_POST_PROCESSING);
}

/* Finish one extent swap, possibly log more. */
int
xfs_swapext_finish_one(
	struct xfs_trans		*tp,
	struct xfs_swapext_intent	*sxi)
{
	struct xfs_bmbt_irec		irec1, irec2;
	int				whichfork;
	int				nimaps;
	int				bmap_flags;
	int				error = 0;

	whichfork = (sxi->sxi_flags & XFS_SWAP_EXTENT_ATTR_FORK) ?
			XFS_ATTR_FORK : XFS_DATA_FORK;
	bmap_flags = xfs_bmapi_aflag(whichfork);

	/* Do any post-processing work that we requires a transaction roll. */
	if (sxi->sxi_blockcount == 0) {
		if (sxi->sxi_flags & XFS_SWAP_EXTENT_INO2_SHORTFORM) {
			if (sxi->sxi_flags & XFS_SWAP_EXTENT_ATTR_FORK)
				error = xfs_swapext_attr_to_shortform2(tp, sxi);
			else if (S_ISDIR(VFS_I(sxi->sxi_ip2)->i_mode))
				error = xfs_swapext_dir_to_shortform2(tp, sxi);
			sxi->sxi_flags &= ~XFS_SWAP_EXTENT_INO2_SHORTFORM;
			return error;
		}
		return 0;
	}

	while (sxi->sxi_blockcount > 0) {
		int64_t		ip1_delta = 0, ip2_delta = 0;

		/* Read extent from the first file */
		nimaps = 1;
		error = xfs_bmapi_read(sxi->sxi_ip1, sxi->sxi_startoff1,
				sxi->sxi_blockcount, &irec1, &nimaps,
				bmap_flags);
		if (error)
			return error;
		if (nimaps != 1 ||
		    irec1.br_startblock == DELAYSTARTBLOCK ||
		    irec1.br_startoff != sxi->sxi_startoff1) {
			/*
			 * We should never get no mapping or a delalloc extent
			 * or something that doesn't match what we asked for,
			 * since the caller flushed both inodes and we hold the
			 * ILOCKs for both inodes.
			 */
			ASSERT(0);
			return -EINVAL;
		}

		/* Read extent from the second file */
		nimaps = 1;
		error = xfs_bmapi_read(sxi->sxi_ip2, sxi->sxi_startoff2,
				irec1.br_blockcount, &irec2, &nimaps,
				bmap_flags);
		if (error)
			return error;
		if (nimaps != 1 ||
		    irec2.br_startblock == DELAYSTARTBLOCK ||
		    irec2.br_startoff != sxi->sxi_startoff2) {
			/*
			 * We should never get no mapping or a delalloc extent
			 * or something that doesn't match what we asked for,
			 * since the caller flushed both inodes and we hold the
			 * ILOCKs for both inodes.
			 */
			ASSERT(0);
			return -EINVAL;
		}

		/*
		 * We can only swap as many blocks as the smaller of the two
		 * extent maps.
		 */
		irec1.br_blockcount = min(irec1.br_blockcount,
					  irec2.br_blockcount);

		trace_xfs_swapext_extent1(sxi->sxi_ip1, &irec1);
		trace_xfs_swapext_extent2(sxi->sxi_ip2, &irec2);

		/*
		 * Two extents mapped to the same physical block must not have
		 * different states; that's filesystem corruption.  Move on to
		 * the next extent if they're both holes or both the same
		 * physical extent.
		 */
		if (irec1.br_startblock == irec2.br_startblock) {
			if (irec1.br_state != irec2.br_state)
				return -EFSCORRUPTED;

			sxi->sxi_startoff1 += irec1.br_blockcount;
			sxi->sxi_startoff2 += irec1.br_blockcount;
			sxi->sxi_blockcount -= irec1.br_blockcount;
			continue;
		}

		/* Update quota accounting. */
		if (xfs_bmap_is_mapped_extent(&irec1)) {
			ip1_delta -= irec1.br_blockcount;
			ip2_delta += irec1.br_blockcount;
		}
		if (xfs_bmap_is_mapped_extent(&irec2)) {
			ip1_delta += irec2.br_blockcount;
			ip2_delta -= irec2.br_blockcount;
		}

		if (ip1_delta)
			xfs_trans_mod_dquot_byino(tp, sxi->sxi_ip1,
					XFS_TRANS_DQ_BCOUNT, ip1_delta);
		if (ip2_delta)
			xfs_trans_mod_dquot_byino(tp, sxi->sxi_ip2,
					XFS_TRANS_DQ_BCOUNT, ip2_delta);

		/* Remove both mappings. */
		xfs_bmap_unmap_extent(tp, sxi->sxi_ip1, whichfork, &irec1);
		xfs_bmap_unmap_extent(tp, sxi->sxi_ip2, whichfork, &irec2);

		/*
		 * Re-add both mappings.  We swap the file offsets between the
		 * two maps and add the opposite map, which has the effect of
		 * filling the logical offsets we just unmapped, but with with
		 * the physical mapping information swapped.
		 */
		swap(irec1.br_startoff, irec2.br_startoff);
		xfs_bmap_map_extent(tp, sxi->sxi_ip1, whichfork, &irec2);
		xfs_bmap_map_extent(tp, sxi->sxi_ip2, whichfork, &irec1);

		/* Make sure we're not mapping extents past EOF. */
		if (whichfork == XFS_DATA_FORK) {
			xfs_swapext_update_size(tp, sxi->sxi_ip1, &irec2,
					sxi->sxi_isize1);
			xfs_swapext_update_size(tp, sxi->sxi_ip2, &irec1,
					sxi->sxi_isize2);
		}

		/*
		 * Advance our cursor and exit.   The caller (either defer ops
		 * or log recovery) will log the SXD item, and if *blockcount
		 * is nonzero, it will log a new SXI item for the remainder
		 * and call us back.
		 */
		sxi->sxi_startoff1 += irec1.br_blockcount;
		sxi->sxi_startoff2 += irec1.br_blockcount;
		sxi->sxi_blockcount -= irec1.br_blockcount;
		break;
	}

	/*
	 * If we've reached the end of the remap operation and the caller
	 * wanted us to exchange the sizes, do that now.
	 */
	if (sxi->sxi_blockcount == 0 &&
	    (sxi->sxi_flags & XFS_SWAP_EXTENT_SET_SIZES)) {
		sxi->sxi_ip1->i_d.di_size = sxi->sxi_isize1;
		sxi->sxi_ip2->i_d.di_size = sxi->sxi_isize2;
		xfs_trans_log_inode(tp, sxi->sxi_ip1, XFS_ILOG_CORE);
		xfs_trans_log_inode(tp, sxi->sxi_ip2, XFS_ILOG_CORE);
	}

	if (XFS_TEST_ERROR(false, tp->t_mountp, XFS_ERRTAG_SWAPEXT_FINISH_ONE))
		return -EIO;

	if (xfs_swapext_has_more_work(sxi))
		trace_xfs_swapext_defer(tp->t_mountp, sxi);
	return 0;
}

/* Estimate the bmbt and rmapbt overhead required to exchange extents. */
int
xfs_swapext_estimate_overhead(
	const struct xfs_swapext_req	*req,
	struct xfs_swapext_res		*res)
{
	struct xfs_mount		*mp = req->ip1->i_mount;
	unsigned int			t1, t2;
	unsigned int			bmbt_overhead;

	/*
	 * Compute the amount of bmbt blocks we should reserve for each file.
	 *
	 * Conceptually this shouldn't affect the shape of either bmbt, but
	 * since we atomically move extents one by one, we reserve enough space
	 * to handle a bmbt split for each remap operation (t1).
	 *
	 * However, we must be careful to handle a corner case where the
	 * repeated unmap and map activities could result in ping-ponging of
	 * the btree shape.  This behavior can come from one of two sources:
	 *
	 * An inode's extent list could have just enough records to straddle
	 * the btree format boundary. If so, the inode could bounce between
	 * btree <-> extent format on unmap -> remap cycles, freeing and
	 * allocating a bmapbt block each time.
	 *
	 * The same thing can happen if we have just enough records in a block
	 * to bounce between one and two leaf blocks. If there aren't enough
	 * sibling blocks to absorb or donate some records, we end up reshaping
	 * the tree with every remap operation.  This doesn't seem to happen if
	 * we have more than four bmbt leaf blocks, so we'll make that the
	 * lower bound on the pingponging (t2).
	 *
	 * Therefore, we estimate the bmbt overhead to be the max of both.
	 */
	t1 = XFS_NEXTENTADD_SPACE_RES(mp, res->nr_exchanges, req->whichfork);
	t2 = mp->m_bmap_dmxr[0] * 4;
	bmbt_overhead = max(t1, t2);

	res->ip1_bcount += bmbt_overhead;
	res->ip2_bcount += bmbt_overhead;
	res->resblks += 2 * bmbt_overhead;

	/* Apply similar logic to rmapbt reservations. */
	if (xfs_sb_version_hasrmapbt(&mp->m_sb)) {
		unsigned int	rmapbt_overhead;

		if (!XFS_IS_REALTIME_INODE(req->ip1)) {
			t1 = XFS_NRMAPADD_SPACE_RES(mp, res->nr_exchanges);
			t2 = mp->m_rmap_mxr[0] * 4;
			rmapbt_overhead = max(t1, t2);
		} else
			rmapbt_overhead = 0;
		res->resblks += 2 * rmapbt_overhead;
	}

	trace_xfs_swapext_estimate(req, res);

	if (res->resblks > UINT_MAX)
		return -ENOSPC;
	return 0;
}

/*
 * Estimate the number of exchange operations and the number of file blocks
 * in each file that will be affected by the exchange operation.
 */
int
xfs_swapext_estimate(
	const struct xfs_swapext_req	*req,
	struct xfs_swapext_res		*res)
{
	struct xfs_swapext_req		r = *req; /* struct copy */
	struct xfs_bmbt_irec		irec1, irec2;
	xfs_filblks_t			ip1_blocks = 0, ip2_blocks = 0;
	int				bmap_flags;
	int				nimaps;
	int				error;

	bmap_flags = xfs_bmapi_aflag(req->whichfork);
	memset(res, 0, sizeof(struct xfs_swapext_res));

	while (r.blockcount > 0) {
		/* Read extent from the first file */
		nimaps = 1;
		error = xfs_bmapi_read(r.ip1, r.startoff1, r.blockcount,
				&irec1, &nimaps, bmap_flags);
		if (error)
			return error;
		if (irec1.br_startblock == DELAYSTARTBLOCK ||
		    irec1.br_startoff != r.startoff1) {
			/*
			 * We should never get no mapping or a delalloc extent
			 * or something that doesn't match what we asked for,
			 * since the caller flushed both inodes and we hold the
			 * ILOCKs for both inodes.
			 */
			ASSERT(0);
			return -EINVAL;
		}

		/* Read extent from the second file */
		nimaps = 1;
		error = xfs_bmapi_read(r.ip2, r.startoff2,
				irec1.br_blockcount, &irec2, &nimaps,
				bmap_flags);
		if (error)
			return error;
		if (irec2.br_startblock == DELAYSTARTBLOCK ||
		    irec2.br_startoff != r.startoff2) {
			/*
			 * We should never get no mapping or a delalloc extent
			 * or something that doesn't match what we asked for,
			 * since the caller flushed both inodes and we hold the
			 * ILOCKs for both inodes.
			 */
			ASSERT(0);
			return -EINVAL;
		}

		/*
		 * We can only swap as many blocks as the smaller of the two
		 * extent maps.
		 */
		irec1.br_blockcount = min(irec1.br_blockcount,
					  irec2.br_blockcount);

		/*
		 * Two extents mapped to the same physical block must not have
		 * different states; that's filesystem corruption.  Move on to
		 * the next extent if they're both holes or both the same
		 * physical extent.
		 */
		if (irec1.br_startblock == irec2.br_startblock) {
			if (irec1.br_state != irec2.br_state)
				return -EFSCORRUPTED;

			r.startoff1 += irec1.br_blockcount;
			r.startoff2 += irec1.br_blockcount;
			r.blockcount -= irec1.br_blockcount;
			continue;
		}

		/* Update accounting. */
		if (xfs_bmap_is_mapped_extent(&irec1))
			ip1_blocks += irec1.br_blockcount;
		if (xfs_bmap_is_mapped_extent(&irec2))
			ip2_blocks += irec2.br_blockcount;
		res->nr_exchanges++;

		/* Advance our cursor and move on. */
		r.startoff1 += irec1.br_blockcount;
		r.startoff2 += irec1.br_blockcount;
		r.blockcount -= irec1.br_blockcount;
	}

	/* Account for the blocks that are being exchanged. */
	if (XFS_IS_REALTIME_INODE(req->ip1) &&
	    req->whichfork == XFS_DATA_FORK) {
		res->ip1_rtbcount = ip1_blocks;
		res->ip2_rtbcount = ip2_blocks;
	} else {
		res->ip1_bcount = ip1_blocks;
		res->ip2_bcount = ip2_blocks;
	}

	return xfs_swapext_estimate_overhead(req, res);
}

static void
xfs_swapext_init_intent(
	struct xfs_swapext_intent	*sxi,
	const struct xfs_swapext_req	*req)
{
	INIT_LIST_HEAD(&sxi->sxi_list);
	sxi->sxi_flags = 0;
	if (req->whichfork == XFS_ATTR_FORK)
		sxi->sxi_flags |= XFS_SWAP_EXTENT_ATTR_FORK;
	sxi->sxi_isize1 = sxi->sxi_isize2 = -1;
	if (req->whichfork == XFS_DATA_FORK &&
	    (req->flags & XFS_SWAPEXT_SET_SIZES)) {
		sxi->sxi_flags |= XFS_SWAP_EXTENT_SET_SIZES;
		sxi->sxi_isize1 = req->ip2->i_d.di_size;
		sxi->sxi_isize2 = req->ip1->i_d.di_size;
	}
	if (req->flags & XFS_SWAPEXT_INO2_SHORTFORM)
		sxi->sxi_flags |= XFS_SWAP_EXTENT_INO2_SHORTFORM;
	sxi->sxi_ip1 = req->ip1;
	sxi->sxi_ip2 = req->ip2;
	sxi->sxi_startoff1 = req->startoff1;
	sxi->sxi_startoff2 = req->startoff2;
	sxi->sxi_blockcount = req->blockcount;
}

/*
 * Atomically swap a range of extents from one inode to another.
 *
 * The caller must ensure the inodes must be joined to the transaction and
 * ILOCKd; they will still be joined to the transaction at exit.
 */
int
xfs_swapext_atomic(
	struct xfs_trans		**tpp,
	const struct xfs_swapext_req	*req)
{
	struct xfs_swapext_intent	*sxi;
	unsigned int			reflink_state;
	int				error;

	ASSERT(xfs_isilocked(req->ip1, XFS_ILOCK_EXCL));
	ASSERT(xfs_isilocked(req->ip2, XFS_ILOCK_EXCL));
	ASSERT(req->whichfork != XFS_COW_FORK);
	if (req->flags & XFS_SWAPEXT_SET_SIZES)
		ASSERT(req->whichfork == XFS_DATA_FORK);

	reflink_state = xfs_swapext_reflink_prep(req);

	sxi = kmem_alloc(sizeof(struct xfs_swapext_intent), KM_NOFS);
	xfs_swapext_init_intent(sxi, req);
	xfs_swapext_schedule(*tpp, sxi);

	error = xfs_defer_finish(tpp);
	if (error)
		return error;

	xfs_swapext_reflink_finish(*tpp, req, reflink_state);
	return 0;
}

/*
 * Swap a range of extents from one inode to another, non-atomically.
 *
 * Use deferred bmap log items swap a range of extents from one inode with
 * another.  Overall extent swap progress is /not/ tracked through the log,
 * which means that while log recovery can finish remapping a single extent,
 * it cannot finish the entire operation.
 */
int
xfs_swapext_deferred_bmap(
	struct xfs_trans		**tpp,
	const struct xfs_swapext_req	*req)
{
	struct xfs_swapext_intent	sxi;
	unsigned int			state;
	int				error;

	ASSERT(xfs_isilocked(req->ip1, XFS_ILOCK_EXCL));
	ASSERT(xfs_isilocked(req->ip2, XFS_ILOCK_EXCL));
	ASSERT(req->whichfork == XFS_DATA_FORK);
	if (req->flags & XFS_SWAPEXT_SET_SIZES)
		ASSERT(req->whichfork == XFS_DATA_FORK);

	state = xfs_swapext_reflink_prep(req);

	xfs_swapext_init_intent(&sxi, req);

	while (sxi.sxi_blockcount > 0) {
		error = xfs_swapext_finish_one(*tpp, &sxi);
		if (error)
			return error;
		error = xfs_defer_finish(tpp);
		if (error)
			return error;
	}

	xfs_swapext_reflink_finish(*tpp, req, state);
	return 0;
}