xref: /linux/fs/xfs/xfs_bmap_util.c (revision 95f0b95e2b686ceaa3f465e9fa079f22e0fe7665)
10b61f8a4SDave Chinner // SPDX-License-Identifier: GPL-2.0
268988114SDave Chinner /*
368988114SDave Chinner  * Copyright (c) 2000-2006 Silicon Graphics, Inc.
4c24b5dfaSDave Chinner  * Copyright (c) 2012 Red Hat, Inc.
568988114SDave Chinner  * All Rights Reserved.
668988114SDave Chinner  */
768988114SDave Chinner #include "xfs.h"
868988114SDave Chinner #include "xfs_fs.h"
970a9883cSDave Chinner #include "xfs_shared.h"
10239880efSDave Chinner #include "xfs_format.h"
11239880efSDave Chinner #include "xfs_log_format.h"
12239880efSDave Chinner #include "xfs_trans_resv.h"
1368988114SDave Chinner #include "xfs_bit.h"
1468988114SDave Chinner #include "xfs_mount.h"
153ab78df2SDarrick J. Wong #include "xfs_defer.h"
1668988114SDave Chinner #include "xfs_inode.h"
1768988114SDave Chinner #include "xfs_btree.h"
18239880efSDave Chinner #include "xfs_trans.h"
1968988114SDave Chinner #include "xfs_alloc.h"
2068988114SDave Chinner #include "xfs_bmap.h"
2168988114SDave Chinner #include "xfs_bmap_util.h"
22a4fbe6abSDave Chinner #include "xfs_bmap_btree.h"
2368988114SDave Chinner #include "xfs_rtalloc.h"
2468988114SDave Chinner #include "xfs_error.h"
2568988114SDave Chinner #include "xfs_quota.h"
2668988114SDave Chinner #include "xfs_trans_space.h"
2768988114SDave Chinner #include "xfs_trace.h"
28c24b5dfaSDave Chinner #include "xfs_icache.h"
29f86f4037SDarrick J. Wong #include "xfs_iomap.h"
30f86f4037SDarrick J. Wong #include "xfs_reflink.h"
3168988114SDave Chinner 
3268988114SDave Chinner /* Kernel only BMAP related definitions and functions */
3368988114SDave Chinner 
3468988114SDave Chinner /*
3568988114SDave Chinner  * Convert the given file system block to a disk block.  We have to treat it
3668988114SDave Chinner  * differently based on whether the file is a real time file or not, because the
3768988114SDave Chinner  * bmap code does.
3868988114SDave Chinner  */
3968988114SDave Chinner xfs_daddr_t
4068988114SDave Chinner xfs_fsb_to_db(struct xfs_inode *ip, xfs_fsblock_t fsb)
4168988114SDave Chinner {
42ecfc28a4SChristoph Hellwig 	if (XFS_IS_REALTIME_INODE(ip))
43ecfc28a4SChristoph Hellwig 		return XFS_FSB_TO_BB(ip->i_mount, fsb);
44ecfc28a4SChristoph Hellwig 	return XFS_FSB_TO_DADDR(ip->i_mount, fsb);
4568988114SDave Chinner }
4668988114SDave Chinner 
4768988114SDave Chinner /*
483fbbbea3SDave Chinner  * Routine to zero an extent on disk allocated to the specific inode.
493fbbbea3SDave Chinner  *
503fbbbea3SDave Chinner  * The VFS functions take a linearised filesystem block offset, so we have to
513fbbbea3SDave Chinner  * convert the sparse xfs fsb to the right format first.
523fbbbea3SDave Chinner  * VFS types are real funky, too.
533fbbbea3SDave Chinner  */
543fbbbea3SDave Chinner int
553fbbbea3SDave Chinner xfs_zero_extent(
563fbbbea3SDave Chinner 	struct xfs_inode	*ip,
573fbbbea3SDave Chinner 	xfs_fsblock_t		start_fsb,
583fbbbea3SDave Chinner 	xfs_off_t		count_fsb)
593fbbbea3SDave Chinner {
603fbbbea3SDave Chinner 	struct xfs_mount	*mp = ip->i_mount;
6130fa529eSChristoph Hellwig 	struct xfs_buftarg	*target = xfs_inode_buftarg(ip);
623fbbbea3SDave Chinner 	xfs_daddr_t		sector = xfs_fsb_to_db(ip, start_fsb);
633fbbbea3SDave Chinner 	sector_t		block = XFS_BB_TO_FSBT(mp, sector);
643fbbbea3SDave Chinner 
6530fa529eSChristoph Hellwig 	return blkdev_issue_zeroout(target->bt_bdev,
663dc29161SMatthew Wilcox 		block << (mp->m_super->s_blocksize_bits - 9),
673dc29161SMatthew Wilcox 		count_fsb << (mp->m_super->s_blocksize_bits - 9),
68ee472d83SChristoph Hellwig 		GFP_NOFS, 0);
693fbbbea3SDave Chinner }
703fbbbea3SDave Chinner 
71bb9c2e54SDave Chinner #ifdef CONFIG_XFS_RT
7268988114SDave Chinner int
7368988114SDave Chinner xfs_bmap_rtalloc(
749d5e8492SDarrick J. Wong 	struct xfs_bmalloca	*ap)
7568988114SDave Chinner {
769d5e8492SDarrick J. Wong 	struct xfs_mount	*mp = ap->ip->i_mount;
779d5e8492SDarrick J. Wong 	xfs_fileoff_t		orig_offset = ap->offset;
789d5e8492SDarrick J. Wong 	xfs_rtblock_t		rtb;
7968988114SDave Chinner 	xfs_extlen_t		prod = 0;  /* product factor for allocators */
800703a8e1SDave Chinner 	xfs_extlen_t		mod = 0;   /* product factor for allocators */
8168988114SDave Chinner 	xfs_extlen_t		ralen = 0; /* realtime allocation length */
8268988114SDave Chinner 	xfs_extlen_t		align;     /* minimum allocation alignment */
839d5e8492SDarrick J. Wong 	xfs_extlen_t		orig_length = ap->length;
849d5e8492SDarrick J. Wong 	xfs_extlen_t		minlen = mp->m_sb.sb_rextsize;
859d5e8492SDarrick J. Wong 	xfs_extlen_t		raminlen;
869d5e8492SDarrick J. Wong 	bool			rtlocked = false;
87676a659bSDarrick J. Wong 	bool			ignore_locality = false;
889d5e8492SDarrick J. Wong 	int			error;
8968988114SDave Chinner 
9068988114SDave Chinner 	align = xfs_get_extsz_hint(ap->ip);
919d5e8492SDarrick J. Wong retry:
9268988114SDave Chinner 	prod = align / mp->m_sb.sb_rextsize;
9368988114SDave Chinner 	error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
9468988114SDave Chinner 					align, 1, ap->eof, 0,
9568988114SDave Chinner 					ap->conv, &ap->offset, &ap->length);
9668988114SDave Chinner 	if (error)
9768988114SDave Chinner 		return error;
9868988114SDave Chinner 	ASSERT(ap->length);
9968988114SDave Chinner 	ASSERT(ap->length % mp->m_sb.sb_rextsize == 0);
10068988114SDave Chinner 
10168988114SDave Chinner 	/*
1029d5e8492SDarrick J. Wong 	 * If we shifted the file offset downward to satisfy an extent size
1039d5e8492SDarrick J. Wong 	 * hint, increase minlen by that amount so that the allocator won't
1049d5e8492SDarrick J. Wong 	 * give us an allocation that's too short to cover at least one of the
1059d5e8492SDarrick J. Wong 	 * blocks that the caller asked for.
1069d5e8492SDarrick J. Wong 	 */
1079d5e8492SDarrick J. Wong 	if (ap->offset != orig_offset)
1089d5e8492SDarrick J. Wong 		minlen += orig_offset - ap->offset;
1099d5e8492SDarrick J. Wong 
1109d5e8492SDarrick J. Wong 	/*
11168988114SDave Chinner 	 * If the offset & length are not perfectly aligned
11268988114SDave Chinner 	 * then kill prod, it will just get us in trouble.
11368988114SDave Chinner 	 */
1140703a8e1SDave Chinner 	div_u64_rem(ap->offset, align, &mod);
1150703a8e1SDave Chinner 	if (mod || ap->length % align)
11668988114SDave Chinner 		prod = 1;
11768988114SDave Chinner 	/*
11868988114SDave Chinner 	 * Set ralen to be the actual requested length in rtextents.
11968988114SDave Chinner 	 */
12068988114SDave Chinner 	ralen = ap->length / mp->m_sb.sb_rextsize;
12168988114SDave Chinner 	/*
122*95f0b95eSChandan Babu R 	 * If the old value was close enough to XFS_BMBT_MAX_EXTLEN that
12368988114SDave Chinner 	 * we rounded up to it, cut it back so it's valid again.
12468988114SDave Chinner 	 * Note that if it's a really large request (bigger than
125*95f0b95eSChandan Babu R 	 * XFS_BMBT_MAX_EXTLEN), we don't hear about that number, and can't
12668988114SDave Chinner 	 * adjust the starting point to match it.
12768988114SDave Chinner 	 */
128*95f0b95eSChandan Babu R 	if (ralen * mp->m_sb.sb_rextsize >= XFS_MAX_BMBT_EXTLEN)
129*95f0b95eSChandan Babu R 		ralen = XFS_MAX_BMBT_EXTLEN / mp->m_sb.sb_rextsize;
13068988114SDave Chinner 
13168988114SDave Chinner 	/*
1324b680afbSDave Chinner 	 * Lock out modifications to both the RT bitmap and summary inodes
13368988114SDave Chinner 	 */
1349d5e8492SDarrick J. Wong 	if (!rtlocked) {
135f4a0660dSDarrick J. Wong 		xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP);
13668988114SDave Chinner 		xfs_trans_ijoin(ap->tp, mp->m_rbmip, XFS_ILOCK_EXCL);
137f4a0660dSDarrick J. Wong 		xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM);
1384b680afbSDave Chinner 		xfs_trans_ijoin(ap->tp, mp->m_rsumip, XFS_ILOCK_EXCL);
1399d5e8492SDarrick J. Wong 		rtlocked = true;
1409d5e8492SDarrick J. Wong 	}
14168988114SDave Chinner 
14268988114SDave Chinner 	/*
14368988114SDave Chinner 	 * If it's an allocation to an empty file at offset 0,
14468988114SDave Chinner 	 * pick an extent that will space things out in the rt area.
14568988114SDave Chinner 	 */
14668988114SDave Chinner 	if (ap->eof && ap->offset == 0) {
1473f649ab7SKees Cook 		xfs_rtblock_t rtx; /* realtime extent no */
14868988114SDave Chinner 
14968988114SDave Chinner 		error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx);
15068988114SDave Chinner 		if (error)
15168988114SDave Chinner 			return error;
15268988114SDave Chinner 		ap->blkno = rtx * mp->m_sb.sb_rextsize;
15368988114SDave Chinner 	} else {
15468988114SDave Chinner 		ap->blkno = 0;
15568988114SDave Chinner 	}
15668988114SDave Chinner 
15768988114SDave Chinner 	xfs_bmap_adjacent(ap);
15868988114SDave Chinner 
15968988114SDave Chinner 	/*
16068988114SDave Chinner 	 * Realtime allocation, done through xfs_rtallocate_extent.
16168988114SDave Chinner 	 */
162676a659bSDarrick J. Wong 	if (ignore_locality)
163676a659bSDarrick J. Wong 		ap->blkno = 0;
164676a659bSDarrick J. Wong 	else
16568988114SDave Chinner 		do_div(ap->blkno, mp->m_sb.sb_rextsize);
16668988114SDave Chinner 	rtb = ap->blkno;
16768988114SDave Chinner 	ap->length = ralen;
1689d5e8492SDarrick J. Wong 	raminlen = max_t(xfs_extlen_t, 1, minlen / mp->m_sb.sb_rextsize);
1699d5e8492SDarrick J. Wong 	error = xfs_rtallocate_extent(ap->tp, ap->blkno, raminlen, ap->length,
170089ec2f8SChristoph Hellwig 			&ralen, ap->wasdel, prod, &rtb);
171089ec2f8SChristoph Hellwig 	if (error)
17268988114SDave Chinner 		return error;
173089ec2f8SChristoph Hellwig 
1749d5e8492SDarrick J. Wong 	if (rtb != NULLRTBLOCK) {
1759d5e8492SDarrick J. Wong 		ap->blkno = rtb * mp->m_sb.sb_rextsize;
1769d5e8492SDarrick J. Wong 		ap->length = ralen * mp->m_sb.sb_rextsize;
1779d5e8492SDarrick J. Wong 		ap->ip->i_nblocks += ap->length;
17868988114SDave Chinner 		xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
17968988114SDave Chinner 		if (ap->wasdel)
1809d5e8492SDarrick J. Wong 			ap->ip->i_delayed_blks -= ap->length;
18168988114SDave Chinner 		/*
18268988114SDave Chinner 		 * Adjust the disk quota also. This was reserved
18368988114SDave Chinner 		 * earlier.
18468988114SDave Chinner 		 */
18568988114SDave Chinner 		xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
18668988114SDave Chinner 			ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT :
1879d5e8492SDarrick J. Wong 					XFS_TRANS_DQ_RTBCOUNT, ap->length);
1889d5e8492SDarrick J. Wong 		return 0;
18968988114SDave Chinner 	}
1909d5e8492SDarrick J. Wong 
1919d5e8492SDarrick J. Wong 	if (align > mp->m_sb.sb_rextsize) {
1929d5e8492SDarrick J. Wong 		/*
1939d5e8492SDarrick J. Wong 		 * We previously enlarged the request length to try to satisfy
1949d5e8492SDarrick J. Wong 		 * an extent size hint.  The allocator didn't return anything,
1959d5e8492SDarrick J. Wong 		 * so reset the parameters to the original values and try again
1969d5e8492SDarrick J. Wong 		 * without alignment criteria.
1979d5e8492SDarrick J. Wong 		 */
1989d5e8492SDarrick J. Wong 		ap->offset = orig_offset;
1999d5e8492SDarrick J. Wong 		ap->length = orig_length;
2009d5e8492SDarrick J. Wong 		minlen = align = mp->m_sb.sb_rextsize;
2019d5e8492SDarrick J. Wong 		goto retry;
2029d5e8492SDarrick J. Wong 	}
2039d5e8492SDarrick J. Wong 
204676a659bSDarrick J. Wong 	if (!ignore_locality && ap->blkno != 0) {
205676a659bSDarrick J. Wong 		/*
206676a659bSDarrick J. Wong 		 * If we can't allocate near a specific rt extent, try again
207676a659bSDarrick J. Wong 		 * without locality criteria.
208676a659bSDarrick J. Wong 		 */
209676a659bSDarrick J. Wong 		ignore_locality = true;
210676a659bSDarrick J. Wong 		goto retry;
211676a659bSDarrick J. Wong 	}
212676a659bSDarrick J. Wong 
2139d5e8492SDarrick J. Wong 	ap->blkno = NULLFSBLOCK;
2149d5e8492SDarrick J. Wong 	ap->length = 0;
21568988114SDave Chinner 	return 0;
21668988114SDave Chinner }
217bb9c2e54SDave Chinner #endif /* CONFIG_XFS_RT */
21868988114SDave Chinner 
21968988114SDave Chinner /*
22068988114SDave Chinner  * Extent tree block counting routines.
22168988114SDave Chinner  */
22268988114SDave Chinner 
22368988114SDave Chinner /*
224d29cb3e4SDarrick J. Wong  * Count leaf blocks given a range of extent records.  Delayed allocation
225d29cb3e4SDarrick J. Wong  * extents are not counted towards the totals.
22668988114SDave Chinner  */
227e17a5c6fSChristoph Hellwig xfs_extnum_t
22868988114SDave Chinner xfs_bmap_count_leaves(
229d29cb3e4SDarrick J. Wong 	struct xfs_ifork	*ifp,
230e7f5d5caSDarrick J. Wong 	xfs_filblks_t		*count)
23168988114SDave Chinner {
232b2b1712aSChristoph Hellwig 	struct xfs_iext_cursor	icur;
233e17a5c6fSChristoph Hellwig 	struct xfs_bmbt_irec	got;
234b2b1712aSChristoph Hellwig 	xfs_extnum_t		numrecs = 0;
23568988114SDave Chinner 
236b2b1712aSChristoph Hellwig 	for_each_xfs_iext(ifp, &icur, &got) {
237e17a5c6fSChristoph Hellwig 		if (!isnullstartblock(got.br_startblock)) {
238e17a5c6fSChristoph Hellwig 			*count += got.br_blockcount;
239e17a5c6fSChristoph Hellwig 			numrecs++;
24068988114SDave Chinner 		}
24168988114SDave Chinner 	}
242b2b1712aSChristoph Hellwig 
243e17a5c6fSChristoph Hellwig 	return numrecs;
244d29cb3e4SDarrick J. Wong }
24568988114SDave Chinner 
24668988114SDave Chinner /*
247d29cb3e4SDarrick J. Wong  * Count fsblocks of the given fork.  Delayed allocation extents are
248d29cb3e4SDarrick J. Wong  * not counted towards the totals.
24968988114SDave Chinner  */
250e7f5d5caSDarrick J. Wong int
25168988114SDave Chinner xfs_bmap_count_blocks(
252e7f5d5caSDarrick J. Wong 	struct xfs_trans	*tp,
253e7f5d5caSDarrick J. Wong 	struct xfs_inode	*ip,
254e7f5d5caSDarrick J. Wong 	int			whichfork,
255e7f5d5caSDarrick J. Wong 	xfs_extnum_t		*nextents,
256e7f5d5caSDarrick J. Wong 	xfs_filblks_t		*count)
25768988114SDave Chinner {
258fec40e22SDarrick J. Wong 	struct xfs_mount	*mp = ip->i_mount;
259fec40e22SDarrick J. Wong 	struct xfs_ifork	*ifp = XFS_IFORK_PTR(ip, whichfork);
260fec40e22SDarrick J. Wong 	struct xfs_btree_cur	*cur;
261fec40e22SDarrick J. Wong 	xfs_extlen_t		btblocks = 0;
262e7f5d5caSDarrick J. Wong 	int			error;
26368988114SDave Chinner 
264e7f5d5caSDarrick J. Wong 	*nextents = 0;
265e7f5d5caSDarrick J. Wong 	*count = 0;
266fec40e22SDarrick J. Wong 
267e7f5d5caSDarrick J. Wong 	if (!ifp)
26868988114SDave Chinner 		return 0;
269e7f5d5caSDarrick J. Wong 
270f7e67b20SChristoph Hellwig 	switch (ifp->if_format) {
271e7f5d5caSDarrick J. Wong 	case XFS_DINODE_FMT_BTREE:
272e7f5d5caSDarrick J. Wong 		error = xfs_iread_extents(tp, ip, whichfork);
273e7f5d5caSDarrick J. Wong 		if (error)
274e7f5d5caSDarrick J. Wong 			return error;
27568988114SDave Chinner 
276fec40e22SDarrick J. Wong 		cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
277fec40e22SDarrick J. Wong 		error = xfs_btree_count_blocks(cur, &btblocks);
278fec40e22SDarrick J. Wong 		xfs_btree_del_cursor(cur, error);
279fec40e22SDarrick J. Wong 		if (error)
280fec40e22SDarrick J. Wong 			return error;
28168988114SDave Chinner 
282fec40e22SDarrick J. Wong 		/*
283fec40e22SDarrick J. Wong 		 * xfs_btree_count_blocks includes the root block contained in
284fec40e22SDarrick J. Wong 		 * the inode fork in @btblocks, so subtract one because we're
285fec40e22SDarrick J. Wong 		 * only interested in allocated disk blocks.
286fec40e22SDarrick J. Wong 		 */
287fec40e22SDarrick J. Wong 		*count += btblocks - 1;
288fec40e22SDarrick J. Wong 
28953004ee7SGustavo A. R. Silva 		fallthrough;
290fec40e22SDarrick J. Wong 	case XFS_DINODE_FMT_EXTENTS:
291fec40e22SDarrick J. Wong 		*nextents = xfs_bmap_count_leaves(ifp, count);
292fec40e22SDarrick J. Wong 		break;
293e7f5d5caSDarrick J. Wong 	}
29468988114SDave Chinner 
29568988114SDave Chinner 	return 0;
29668988114SDave Chinner }
29768988114SDave Chinner 
298abbf9e8aSChristoph Hellwig static int
299abbf9e8aSChristoph Hellwig xfs_getbmap_report_one(
300f86f4037SDarrick J. Wong 	struct xfs_inode	*ip,
301abbf9e8aSChristoph Hellwig 	struct getbmapx		*bmv,
302232b5194SChristoph Hellwig 	struct kgetbmap		*out,
303abbf9e8aSChristoph Hellwig 	int64_t			bmv_end,
304abbf9e8aSChristoph Hellwig 	struct xfs_bmbt_irec	*got)
305f86f4037SDarrick J. Wong {
306232b5194SChristoph Hellwig 	struct kgetbmap		*p = out + bmv->bmv_entries;
307d392bc81SChristoph Hellwig 	bool			shared = false;
308f86f4037SDarrick J. Wong 	int			error;
309f86f4037SDarrick J. Wong 
310d392bc81SChristoph Hellwig 	error = xfs_reflink_trim_around_shared(ip, got, &shared);
311f86f4037SDarrick J. Wong 	if (error)
312f86f4037SDarrick J. Wong 		return error;
313f86f4037SDarrick J. Wong 
314abbf9e8aSChristoph Hellwig 	if (isnullstartblock(got->br_startblock) ||
315abbf9e8aSChristoph Hellwig 	    got->br_startblock == DELAYSTARTBLOCK) {
316f86f4037SDarrick J. Wong 		/*
317abbf9e8aSChristoph Hellwig 		 * Delalloc extents that start beyond EOF can occur due to
318abbf9e8aSChristoph Hellwig 		 * speculative EOF allocation when the delalloc extent is larger
319abbf9e8aSChristoph Hellwig 		 * than the largest freespace extent at conversion time.  These
320abbf9e8aSChristoph Hellwig 		 * extents cannot be converted by data writeback, so can exist
321abbf9e8aSChristoph Hellwig 		 * here even if we are not supposed to be finding delalloc
322abbf9e8aSChristoph Hellwig 		 * extents.
323f86f4037SDarrick J. Wong 		 */
324abbf9e8aSChristoph Hellwig 		if (got->br_startoff < XFS_B_TO_FSB(ip->i_mount, XFS_ISIZE(ip)))
325abbf9e8aSChristoph Hellwig 			ASSERT((bmv->bmv_iflags & BMV_IF_DELALLOC) != 0);
326abbf9e8aSChristoph Hellwig 
327abbf9e8aSChristoph Hellwig 		p->bmv_oflags |= BMV_OF_DELALLOC;
328abbf9e8aSChristoph Hellwig 		p->bmv_block = -2;
329f86f4037SDarrick J. Wong 	} else {
330abbf9e8aSChristoph Hellwig 		p->bmv_block = xfs_fsb_to_db(ip, got->br_startblock);
331f86f4037SDarrick J. Wong 	}
332f86f4037SDarrick J. Wong 
333abbf9e8aSChristoph Hellwig 	if (got->br_state == XFS_EXT_UNWRITTEN &&
334abbf9e8aSChristoph Hellwig 	    (bmv->bmv_iflags & BMV_IF_PREALLOC))
335abbf9e8aSChristoph Hellwig 		p->bmv_oflags |= BMV_OF_PREALLOC;
336abbf9e8aSChristoph Hellwig 
337abbf9e8aSChristoph Hellwig 	if (shared)
338abbf9e8aSChristoph Hellwig 		p->bmv_oflags |= BMV_OF_SHARED;
339abbf9e8aSChristoph Hellwig 
340abbf9e8aSChristoph Hellwig 	p->bmv_offset = XFS_FSB_TO_BB(ip->i_mount, got->br_startoff);
341abbf9e8aSChristoph Hellwig 	p->bmv_length = XFS_FSB_TO_BB(ip->i_mount, got->br_blockcount);
342abbf9e8aSChristoph Hellwig 
343abbf9e8aSChristoph Hellwig 	bmv->bmv_offset = p->bmv_offset + p->bmv_length;
344abbf9e8aSChristoph Hellwig 	bmv->bmv_length = max(0LL, bmv_end - bmv->bmv_offset);
345abbf9e8aSChristoph Hellwig 	bmv->bmv_entries++;
346f86f4037SDarrick J. Wong 	return 0;
347f86f4037SDarrick J. Wong }
348f86f4037SDarrick J. Wong 
349abbf9e8aSChristoph Hellwig static void
350abbf9e8aSChristoph Hellwig xfs_getbmap_report_hole(
351abbf9e8aSChristoph Hellwig 	struct xfs_inode	*ip,
352abbf9e8aSChristoph Hellwig 	struct getbmapx		*bmv,
353232b5194SChristoph Hellwig 	struct kgetbmap		*out,
354abbf9e8aSChristoph Hellwig 	int64_t			bmv_end,
355abbf9e8aSChristoph Hellwig 	xfs_fileoff_t		bno,
356abbf9e8aSChristoph Hellwig 	xfs_fileoff_t		end)
357abbf9e8aSChristoph Hellwig {
358232b5194SChristoph Hellwig 	struct kgetbmap		*p = out + bmv->bmv_entries;
359abbf9e8aSChristoph Hellwig 
360abbf9e8aSChristoph Hellwig 	if (bmv->bmv_iflags & BMV_IF_NO_HOLES)
361abbf9e8aSChristoph Hellwig 		return;
362abbf9e8aSChristoph Hellwig 
363abbf9e8aSChristoph Hellwig 	p->bmv_block = -1;
364abbf9e8aSChristoph Hellwig 	p->bmv_offset = XFS_FSB_TO_BB(ip->i_mount, bno);
365abbf9e8aSChristoph Hellwig 	p->bmv_length = XFS_FSB_TO_BB(ip->i_mount, end - bno);
366abbf9e8aSChristoph Hellwig 
367abbf9e8aSChristoph Hellwig 	bmv->bmv_offset = p->bmv_offset + p->bmv_length;
368abbf9e8aSChristoph Hellwig 	bmv->bmv_length = max(0LL, bmv_end - bmv->bmv_offset);
369abbf9e8aSChristoph Hellwig 	bmv->bmv_entries++;
370abbf9e8aSChristoph Hellwig }
371abbf9e8aSChristoph Hellwig 
372abbf9e8aSChristoph Hellwig static inline bool
373abbf9e8aSChristoph Hellwig xfs_getbmap_full(
374abbf9e8aSChristoph Hellwig 	struct getbmapx		*bmv)
375abbf9e8aSChristoph Hellwig {
376abbf9e8aSChristoph Hellwig 	return bmv->bmv_length == 0 || bmv->bmv_entries >= bmv->bmv_count - 1;
377abbf9e8aSChristoph Hellwig }
378abbf9e8aSChristoph Hellwig 
379abbf9e8aSChristoph Hellwig static bool
380abbf9e8aSChristoph Hellwig xfs_getbmap_next_rec(
381abbf9e8aSChristoph Hellwig 	struct xfs_bmbt_irec	*rec,
382abbf9e8aSChristoph Hellwig 	xfs_fileoff_t		total_end)
383abbf9e8aSChristoph Hellwig {
384abbf9e8aSChristoph Hellwig 	xfs_fileoff_t		end = rec->br_startoff + rec->br_blockcount;
385abbf9e8aSChristoph Hellwig 
386abbf9e8aSChristoph Hellwig 	if (end == total_end)
387abbf9e8aSChristoph Hellwig 		return false;
388abbf9e8aSChristoph Hellwig 
389abbf9e8aSChristoph Hellwig 	rec->br_startoff += rec->br_blockcount;
390abbf9e8aSChristoph Hellwig 	if (!isnullstartblock(rec->br_startblock) &&
391abbf9e8aSChristoph Hellwig 	    rec->br_startblock != DELAYSTARTBLOCK)
392abbf9e8aSChristoph Hellwig 		rec->br_startblock += rec->br_blockcount;
393abbf9e8aSChristoph Hellwig 	rec->br_blockcount = total_end - end;
394abbf9e8aSChristoph Hellwig 	return true;
395abbf9e8aSChristoph Hellwig }
396abbf9e8aSChristoph Hellwig 
39768988114SDave Chinner /*
39868988114SDave Chinner  * Get inode's extents as described in bmv, and format for output.
39968988114SDave Chinner  * Calls formatter to fill the user's buffer until all extents
40068988114SDave Chinner  * are mapped, until the passed-in bmv->bmv_count slots have
40168988114SDave Chinner  * been filled, or until the formatter short-circuits the loop,
40268988114SDave Chinner  * if it is tracking filled-in extents on its own.
40368988114SDave Chinner  */
40468988114SDave Chinner int						/* error code */
40568988114SDave Chinner xfs_getbmap(
406232b5194SChristoph Hellwig 	struct xfs_inode	*ip,
40768988114SDave Chinner 	struct getbmapx		*bmv,		/* user bmap structure */
408232b5194SChristoph Hellwig 	struct kgetbmap		*out)
40968988114SDave Chinner {
410abbf9e8aSChristoph Hellwig 	struct xfs_mount	*mp = ip->i_mount;
411abbf9e8aSChristoph Hellwig 	int			iflags = bmv->bmv_iflags;
412232b5194SChristoph Hellwig 	int			whichfork, lock, error = 0;
413abbf9e8aSChristoph Hellwig 	int64_t			bmv_end, max_len;
414abbf9e8aSChristoph Hellwig 	xfs_fileoff_t		bno, first_bno;
415abbf9e8aSChristoph Hellwig 	struct xfs_ifork	*ifp;
416abbf9e8aSChristoph Hellwig 	struct xfs_bmbt_irec	got, rec;
417abbf9e8aSChristoph Hellwig 	xfs_filblks_t		len;
418b2b1712aSChristoph Hellwig 	struct xfs_iext_cursor	icur;
41968988114SDave Chinner 
420232b5194SChristoph Hellwig 	if (bmv->bmv_iflags & ~BMV_IF_VALID)
421232b5194SChristoph Hellwig 		return -EINVAL;
422f86f4037SDarrick J. Wong #ifndef DEBUG
423f86f4037SDarrick J. Wong 	/* Only allow CoW fork queries if we're debugging. */
424f86f4037SDarrick J. Wong 	if (iflags & BMV_IF_COWFORK)
425f86f4037SDarrick J. Wong 		return -EINVAL;
426f86f4037SDarrick J. Wong #endif
427f86f4037SDarrick J. Wong 	if ((iflags & BMV_IF_ATTRFORK) && (iflags & BMV_IF_COWFORK))
428f86f4037SDarrick J. Wong 		return -EINVAL;
429f86f4037SDarrick J. Wong 
430abbf9e8aSChristoph Hellwig 	if (bmv->bmv_length < -1)
431abbf9e8aSChristoph Hellwig 		return -EINVAL;
432abbf9e8aSChristoph Hellwig 	bmv->bmv_entries = 0;
433abbf9e8aSChristoph Hellwig 	if (bmv->bmv_length == 0)
434abbf9e8aSChristoph Hellwig 		return 0;
435abbf9e8aSChristoph Hellwig 
436f86f4037SDarrick J. Wong 	if (iflags & BMV_IF_ATTRFORK)
437f86f4037SDarrick J. Wong 		whichfork = XFS_ATTR_FORK;
438f86f4037SDarrick J. Wong 	else if (iflags & BMV_IF_COWFORK)
439f86f4037SDarrick J. Wong 		whichfork = XFS_COW_FORK;
440f86f4037SDarrick J. Wong 	else
441f86f4037SDarrick J. Wong 		whichfork = XFS_DATA_FORK;
442abbf9e8aSChristoph Hellwig 	ifp = XFS_IFORK_PTR(ip, whichfork);
44368988114SDave Chinner 
44468988114SDave Chinner 	xfs_ilock(ip, XFS_IOLOCK_SHARED);
445f86f4037SDarrick J. Wong 	switch (whichfork) {
446abbf9e8aSChristoph Hellwig 	case XFS_ATTR_FORK:
447abbf9e8aSChristoph Hellwig 		if (!XFS_IFORK_Q(ip))
448abbf9e8aSChristoph Hellwig 			goto out_unlock_iolock;
449abbf9e8aSChristoph Hellwig 
450abbf9e8aSChristoph Hellwig 		max_len = 1LL << 32;
451abbf9e8aSChristoph Hellwig 		lock = xfs_ilock_attr_map_shared(ip);
452abbf9e8aSChristoph Hellwig 		break;
453abbf9e8aSChristoph Hellwig 	case XFS_COW_FORK:
454abbf9e8aSChristoph Hellwig 		/* No CoW fork? Just return */
455abbf9e8aSChristoph Hellwig 		if (!ifp)
456abbf9e8aSChristoph Hellwig 			goto out_unlock_iolock;
457abbf9e8aSChristoph Hellwig 
458abbf9e8aSChristoph Hellwig 		if (xfs_get_cowextsz_hint(ip))
459abbf9e8aSChristoph Hellwig 			max_len = mp->m_super->s_maxbytes;
460abbf9e8aSChristoph Hellwig 		else
461abbf9e8aSChristoph Hellwig 			max_len = XFS_ISIZE(ip);
462abbf9e8aSChristoph Hellwig 
463abbf9e8aSChristoph Hellwig 		lock = XFS_ILOCK_SHARED;
464abbf9e8aSChristoph Hellwig 		xfs_ilock(ip, lock);
465abbf9e8aSChristoph Hellwig 		break;
466f86f4037SDarrick J. Wong 	case XFS_DATA_FORK:
467efa70be1SChristoph Hellwig 		if (!(iflags & BMV_IF_DELALLOC) &&
46813d2c10bSChristoph Hellwig 		    (ip->i_delayed_blks || XFS_ISIZE(ip) > ip->i_disk_size)) {
4692451337dSDave Chinner 			error = filemap_write_and_wait(VFS_I(ip)->i_mapping);
47068988114SDave Chinner 			if (error)
47168988114SDave Chinner 				goto out_unlock_iolock;
472efa70be1SChristoph Hellwig 
47368988114SDave Chinner 			/*
474efa70be1SChristoph Hellwig 			 * Even after flushing the inode, there can still be
475efa70be1SChristoph Hellwig 			 * delalloc blocks on the inode beyond EOF due to
476efa70be1SChristoph Hellwig 			 * speculative preallocation.  These are not removed
477efa70be1SChristoph Hellwig 			 * until the release function is called or the inode
478efa70be1SChristoph Hellwig 			 * is inactivated.  Hence we cannot assert here that
479efa70be1SChristoph Hellwig 			 * ip->i_delayed_blks == 0.
48068988114SDave Chinner 			 */
48168988114SDave Chinner 		}
48268988114SDave Chinner 
483abbf9e8aSChristoph Hellwig 		if (xfs_get_extsz_hint(ip) ||
484db07349dSChristoph Hellwig 		    (ip->i_diflags &
485abbf9e8aSChristoph Hellwig 		     (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)))
486abbf9e8aSChristoph Hellwig 			max_len = mp->m_super->s_maxbytes;
487abbf9e8aSChristoph Hellwig 		else
488abbf9e8aSChristoph Hellwig 			max_len = XFS_ISIZE(ip);
489abbf9e8aSChristoph Hellwig 
490309ecac8SChristoph Hellwig 		lock = xfs_ilock_data_map_shared(ip);
491f86f4037SDarrick J. Wong 		break;
492efa70be1SChristoph Hellwig 	}
49368988114SDave Chinner 
494f7e67b20SChristoph Hellwig 	switch (ifp->if_format) {
495abbf9e8aSChristoph Hellwig 	case XFS_DINODE_FMT_EXTENTS:
496abbf9e8aSChristoph Hellwig 	case XFS_DINODE_FMT_BTREE:
497abbf9e8aSChristoph Hellwig 		break;
498abbf9e8aSChristoph Hellwig 	case XFS_DINODE_FMT_LOCAL:
499abbf9e8aSChristoph Hellwig 		/* Local format inode forks report no extents. */
50068988114SDave Chinner 		goto out_unlock_ilock;
501abbf9e8aSChristoph Hellwig 	default:
502abbf9e8aSChristoph Hellwig 		error = -EINVAL;
503abbf9e8aSChristoph Hellwig 		goto out_unlock_ilock;
50468988114SDave Chinner 	}
50568988114SDave Chinner 
506abbf9e8aSChristoph Hellwig 	if (bmv->bmv_length == -1) {
507abbf9e8aSChristoph Hellwig 		max_len = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, max_len));
508abbf9e8aSChristoph Hellwig 		bmv->bmv_length = max(0LL, max_len - bmv->bmv_offset);
509abbf9e8aSChristoph Hellwig 	}
510abbf9e8aSChristoph Hellwig 
511abbf9e8aSChristoph Hellwig 	bmv_end = bmv->bmv_offset + bmv->bmv_length;
512abbf9e8aSChristoph Hellwig 
513abbf9e8aSChristoph Hellwig 	first_bno = bno = XFS_BB_TO_FSBT(mp, bmv->bmv_offset);
514abbf9e8aSChristoph Hellwig 	len = XFS_BB_TO_FSB(mp, bmv->bmv_length);
515abbf9e8aSChristoph Hellwig 
516abbf9e8aSChristoph Hellwig 	error = xfs_iread_extents(NULL, ip, whichfork);
517abbf9e8aSChristoph Hellwig 	if (error)
518abbf9e8aSChristoph Hellwig 		goto out_unlock_ilock;
519abbf9e8aSChristoph Hellwig 
520b2b1712aSChristoph Hellwig 	if (!xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got)) {
521abbf9e8aSChristoph Hellwig 		/*
522abbf9e8aSChristoph Hellwig 		 * Report a whole-file hole if the delalloc flag is set to
523abbf9e8aSChristoph Hellwig 		 * stay compatible with the old implementation.
524abbf9e8aSChristoph Hellwig 		 */
525abbf9e8aSChristoph Hellwig 		if (iflags & BMV_IF_DELALLOC)
526abbf9e8aSChristoph Hellwig 			xfs_getbmap_report_hole(ip, bmv, out, bmv_end, bno,
527abbf9e8aSChristoph Hellwig 					XFS_B_TO_FSB(mp, XFS_ISIZE(ip)));
528abbf9e8aSChristoph Hellwig 		goto out_unlock_ilock;
529abbf9e8aSChristoph Hellwig 	}
530abbf9e8aSChristoph Hellwig 
531abbf9e8aSChristoph Hellwig 	while (!xfs_getbmap_full(bmv)) {
532abbf9e8aSChristoph Hellwig 		xfs_trim_extent(&got, first_bno, len);
533abbf9e8aSChristoph Hellwig 
534abbf9e8aSChristoph Hellwig 		/*
535abbf9e8aSChristoph Hellwig 		 * Report an entry for a hole if this extent doesn't directly
536abbf9e8aSChristoph Hellwig 		 * follow the previous one.
537abbf9e8aSChristoph Hellwig 		 */
538abbf9e8aSChristoph Hellwig 		if (got.br_startoff > bno) {
539abbf9e8aSChristoph Hellwig 			xfs_getbmap_report_hole(ip, bmv, out, bmv_end, bno,
540abbf9e8aSChristoph Hellwig 					got.br_startoff);
541abbf9e8aSChristoph Hellwig 			if (xfs_getbmap_full(bmv))
542abbf9e8aSChristoph Hellwig 				break;
543abbf9e8aSChristoph Hellwig 		}
544abbf9e8aSChristoph Hellwig 
545abbf9e8aSChristoph Hellwig 		/*
546abbf9e8aSChristoph Hellwig 		 * In order to report shared extents accurately, we report each
547abbf9e8aSChristoph Hellwig 		 * distinct shared / unshared part of a single bmbt record with
548abbf9e8aSChristoph Hellwig 		 * an individual getbmapx record.
549abbf9e8aSChristoph Hellwig 		 */
550abbf9e8aSChristoph Hellwig 		bno = got.br_startoff + got.br_blockcount;
551abbf9e8aSChristoph Hellwig 		rec = got;
55268988114SDave Chinner 		do {
553abbf9e8aSChristoph Hellwig 			error = xfs_getbmap_report_one(ip, bmv, out, bmv_end,
554abbf9e8aSChristoph Hellwig 					&rec);
555abbf9e8aSChristoph Hellwig 			if (error || xfs_getbmap_full(bmv))
556abbf9e8aSChristoph Hellwig 				goto out_unlock_ilock;
557abbf9e8aSChristoph Hellwig 		} while (xfs_getbmap_next_rec(&rec, bno));
55868988114SDave Chinner 
559b2b1712aSChristoph Hellwig 		if (!xfs_iext_next_extent(ifp, &icur, &got)) {
560abbf9e8aSChristoph Hellwig 			xfs_fileoff_t	end = XFS_B_TO_FSB(mp, XFS_ISIZE(ip));
56168988114SDave Chinner 
562abbf9e8aSChristoph Hellwig 			out[bmv->bmv_entries - 1].bmv_oflags |= BMV_OF_LAST;
56368988114SDave Chinner 
564abbf9e8aSChristoph Hellwig 			if (whichfork != XFS_ATTR_FORK && bno < end &&
565abbf9e8aSChristoph Hellwig 			    !xfs_getbmap_full(bmv)) {
566abbf9e8aSChristoph Hellwig 				xfs_getbmap_report_hole(ip, bmv, out, bmv_end,
567abbf9e8aSChristoph Hellwig 						bno, end);
568abbf9e8aSChristoph Hellwig 			}
569abbf9e8aSChristoph Hellwig 			break;
57068988114SDave Chinner 		}
57168988114SDave Chinner 
572abbf9e8aSChristoph Hellwig 		if (bno >= first_bno + len)
573abbf9e8aSChristoph Hellwig 			break;
57468988114SDave Chinner 	}
57568988114SDave Chinner 
57668988114SDave Chinner out_unlock_ilock:
57701f4f327SChristoph Hellwig 	xfs_iunlock(ip, lock);
57868988114SDave Chinner out_unlock_iolock:
57968988114SDave Chinner 	xfs_iunlock(ip, XFS_IOLOCK_SHARED);
58068988114SDave Chinner 	return error;
58168988114SDave Chinner }
58268988114SDave Chinner 
58368988114SDave Chinner /*
584e2ac8363SChristoph Hellwig  * Dead simple method of punching delalyed allocation blocks from a range in
585e2ac8363SChristoph Hellwig  * the inode.  This will always punch out both the start and end blocks, even
586e2ac8363SChristoph Hellwig  * if the ranges only partially overlap them, so it is up to the caller to
587e2ac8363SChristoph Hellwig  * ensure that partial blocks are not passed in.
58868988114SDave Chinner  */
58968988114SDave Chinner int
59068988114SDave Chinner xfs_bmap_punch_delalloc_range(
59168988114SDave Chinner 	struct xfs_inode	*ip,
59268988114SDave Chinner 	xfs_fileoff_t		start_fsb,
59368988114SDave Chinner 	xfs_fileoff_t		length)
59468988114SDave Chinner {
595e2ac8363SChristoph Hellwig 	struct xfs_ifork	*ifp = &ip->i_df;
596e2ac8363SChristoph Hellwig 	xfs_fileoff_t		end_fsb = start_fsb + length;
597e2ac8363SChristoph Hellwig 	struct xfs_bmbt_irec	got, del;
598e2ac8363SChristoph Hellwig 	struct xfs_iext_cursor	icur;
59968988114SDave Chinner 	int			error = 0;
60068988114SDave Chinner 
601b2197a36SChristoph Hellwig 	ASSERT(!xfs_need_iread_extents(ifp));
60268988114SDave Chinner 
6030065b541SChristoph Hellwig 	xfs_ilock(ip, XFS_ILOCK_EXCL);
604e2ac8363SChristoph Hellwig 	if (!xfs_iext_lookup_extent_before(ip, ifp, &end_fsb, &icur, &got))
605d4380177SChristoph Hellwig 		goto out_unlock;
606e2ac8363SChristoph Hellwig 
607e2ac8363SChristoph Hellwig 	while (got.br_startoff + got.br_blockcount > start_fsb) {
608e2ac8363SChristoph Hellwig 		del = got;
609e2ac8363SChristoph Hellwig 		xfs_trim_extent(&del, start_fsb, length);
610e2ac8363SChristoph Hellwig 
611e2ac8363SChristoph Hellwig 		/*
612e2ac8363SChristoph Hellwig 		 * A delete can push the cursor forward. Step back to the
613e2ac8363SChristoph Hellwig 		 * previous extent on non-delalloc or extents outside the
614e2ac8363SChristoph Hellwig 		 * target range.
615e2ac8363SChristoph Hellwig 		 */
616e2ac8363SChristoph Hellwig 		if (!del.br_blockcount ||
617e2ac8363SChristoph Hellwig 		    !isnullstartblock(del.br_startblock)) {
618e2ac8363SChristoph Hellwig 			if (!xfs_iext_prev_extent(ifp, &icur, &got))
619e2ac8363SChristoph Hellwig 				break;
620e2ac8363SChristoph Hellwig 			continue;
621e2ac8363SChristoph Hellwig 		}
622e2ac8363SChristoph Hellwig 
623e2ac8363SChristoph Hellwig 		error = xfs_bmap_del_extent_delay(ip, XFS_DATA_FORK, &icur,
624e2ac8363SChristoph Hellwig 						  &got, &del);
625e2ac8363SChristoph Hellwig 		if (error || !xfs_iext_get_extent(ifp, &icur, &got))
626e2ac8363SChristoph Hellwig 			break;
627e2ac8363SChristoph Hellwig 	}
62868988114SDave Chinner 
629d4380177SChristoph Hellwig out_unlock:
630d4380177SChristoph Hellwig 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
63168988114SDave Chinner 	return error;
63268988114SDave Chinner }
633c24b5dfaSDave Chinner 
634c24b5dfaSDave Chinner /*
635c24b5dfaSDave Chinner  * Test whether it is appropriate to check an inode for and free post EOF
636c24b5dfaSDave Chinner  * blocks. The 'force' parameter determines whether we should also consider
637c24b5dfaSDave Chinner  * regular files that are marked preallocated or append-only.
638c24b5dfaSDave Chinner  */
639c24b5dfaSDave Chinner bool
6407d88329eSDarrick J. Wong xfs_can_free_eofblocks(
6417d88329eSDarrick J. Wong 	struct xfs_inode	*ip,
6427d88329eSDarrick J. Wong 	bool			force)
643c24b5dfaSDave Chinner {
6447d88329eSDarrick J. Wong 	struct xfs_bmbt_irec	imap;
6457d88329eSDarrick J. Wong 	struct xfs_mount	*mp = ip->i_mount;
6467d88329eSDarrick J. Wong 	xfs_fileoff_t		end_fsb;
6477d88329eSDarrick J. Wong 	xfs_fileoff_t		last_fsb;
6487d88329eSDarrick J. Wong 	int			nimaps = 1;
6497d88329eSDarrick J. Wong 	int			error;
6507d88329eSDarrick J. Wong 
6517d88329eSDarrick J. Wong 	/*
6527d88329eSDarrick J. Wong 	 * Caller must either hold the exclusive io lock; or be inactivating
6537d88329eSDarrick J. Wong 	 * the inode, which guarantees there are no other users of the inode.
6547d88329eSDarrick J. Wong 	 */
6557d88329eSDarrick J. Wong 	ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL) ||
6567d88329eSDarrick J. Wong 	       (VFS_I(ip)->i_state & I_FREEING));
6577d88329eSDarrick J. Wong 
658c24b5dfaSDave Chinner 	/* prealloc/delalloc exists only on regular files */
659c19b3b05SDave Chinner 	if (!S_ISREG(VFS_I(ip)->i_mode))
660c24b5dfaSDave Chinner 		return false;
661c24b5dfaSDave Chinner 
662c24b5dfaSDave Chinner 	/*
663c24b5dfaSDave Chinner 	 * Zero sized files with no cached pages and delalloc blocks will not
664c24b5dfaSDave Chinner 	 * have speculative prealloc/delalloc blocks to remove.
665c24b5dfaSDave Chinner 	 */
666c24b5dfaSDave Chinner 	if (VFS_I(ip)->i_size == 0 &&
6672667c6f9SDave Chinner 	    VFS_I(ip)->i_mapping->nrpages == 0 &&
668c24b5dfaSDave Chinner 	    ip->i_delayed_blks == 0)
669c24b5dfaSDave Chinner 		return false;
670c24b5dfaSDave Chinner 
671c24b5dfaSDave Chinner 	/* If we haven't read in the extent list, then don't do it now. */
672b2197a36SChristoph Hellwig 	if (xfs_need_iread_extents(&ip->i_df))
673c24b5dfaSDave Chinner 		return false;
674c24b5dfaSDave Chinner 
675c24b5dfaSDave Chinner 	/*
676c24b5dfaSDave Chinner 	 * Do not free real preallocated or append-only files unless the file
677c24b5dfaSDave Chinner 	 * has delalloc blocks and we are forced to remove them.
678c24b5dfaSDave Chinner 	 */
679db07349dSChristoph Hellwig 	if (ip->i_diflags & (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND))
680c24b5dfaSDave Chinner 		if (!force || ip->i_delayed_blks == 0)
681c24b5dfaSDave Chinner 			return false;
682c24b5dfaSDave Chinner 
6837d88329eSDarrick J. Wong 	/*
6847d88329eSDarrick J. Wong 	 * Do not try to free post-EOF blocks if EOF is beyond the end of the
6857d88329eSDarrick J. Wong 	 * range supported by the page cache, because the truncation will loop
6867d88329eSDarrick J. Wong 	 * forever.
6877d88329eSDarrick J. Wong 	 */
6887d88329eSDarrick J. Wong 	end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_ISIZE(ip));
6897d88329eSDarrick J. Wong 	last_fsb = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
6907d88329eSDarrick J. Wong 	if (last_fsb <= end_fsb)
6917d88329eSDarrick J. Wong 		return false;
6927d88329eSDarrick J. Wong 
6937d88329eSDarrick J. Wong 	/*
6947d88329eSDarrick J. Wong 	 * Look up the mapping for the first block past EOF.  If we can't find
6957d88329eSDarrick J. Wong 	 * it, there's nothing to free.
6967d88329eSDarrick J. Wong 	 */
6977d88329eSDarrick J. Wong 	xfs_ilock(ip, XFS_ILOCK_SHARED);
6987d88329eSDarrick J. Wong 	error = xfs_bmapi_read(ip, end_fsb, last_fsb - end_fsb, &imap, &nimaps,
6997d88329eSDarrick J. Wong 			0);
7007d88329eSDarrick J. Wong 	xfs_iunlock(ip, XFS_ILOCK_SHARED);
7017d88329eSDarrick J. Wong 	if (error || nimaps == 0)
7027d88329eSDarrick J. Wong 		return false;
7037d88329eSDarrick J. Wong 
7047d88329eSDarrick J. Wong 	/*
7057d88329eSDarrick J. Wong 	 * If there's a real mapping there or there are delayed allocation
7067d88329eSDarrick J. Wong 	 * reservations, then we have post-EOF blocks to try to free.
7077d88329eSDarrick J. Wong 	 */
7087d88329eSDarrick J. Wong 	return imap.br_startblock != HOLESTARTBLOCK || ip->i_delayed_blks;
709c24b5dfaSDave Chinner }
710c24b5dfaSDave Chinner 
711c24b5dfaSDave Chinner /*
7123b4683c2SBrian Foster  * This is called to free any blocks beyond eof. The caller must hold
7133b4683c2SBrian Foster  * IOLOCK_EXCL unless we are in the inode reclaim path and have the only
7143b4683c2SBrian Foster  * reference to the inode.
715c24b5dfaSDave Chinner  */
716c24b5dfaSDave Chinner int
717c24b5dfaSDave Chinner xfs_free_eofblocks(
718a36b9261SBrian Foster 	struct xfs_inode	*ip)
719c24b5dfaSDave Chinner {
720a36b9261SBrian Foster 	struct xfs_trans	*tp;
721a36b9261SBrian Foster 	struct xfs_mount	*mp = ip->i_mount;
7227d88329eSDarrick J. Wong 	int			error;
723a36b9261SBrian Foster 
7247d88329eSDarrick J. Wong 	/* Attach the dquots to the inode up front. */
725c14cfccaSDarrick J. Wong 	error = xfs_qm_dqattach(ip);
726c24b5dfaSDave Chinner 	if (error)
727c24b5dfaSDave Chinner 		return error;
728c24b5dfaSDave Chinner 
7297d88329eSDarrick J. Wong 	/* Wait on dio to ensure i_size has settled. */
730e4229d6bSBrian Foster 	inode_dio_wait(VFS_I(ip));
731e4229d6bSBrian Foster 
7327d88329eSDarrick J. Wong 	error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
733c24b5dfaSDave Chinner 	if (error) {
73475c8c50fSDave Chinner 		ASSERT(xfs_is_shutdown(mp));
735c24b5dfaSDave Chinner 		return error;
736c24b5dfaSDave Chinner 	}
737c24b5dfaSDave Chinner 
738c24b5dfaSDave Chinner 	xfs_ilock(ip, XFS_ILOCK_EXCL);
739c24b5dfaSDave Chinner 	xfs_trans_ijoin(tp, ip, 0);
740c24b5dfaSDave Chinner 
741c24b5dfaSDave Chinner 	/*
7427d88329eSDarrick J. Wong 	 * Do not update the on-disk file size.  If we update the on-disk file
7437d88329eSDarrick J. Wong 	 * size and then the system crashes before the contents of the file are
7447d88329eSDarrick J. Wong 	 * flushed to disk then the files may be full of holes (ie NULL files
7457d88329eSDarrick J. Wong 	 * bug).
746c24b5dfaSDave Chinner 	 */
7474e529339SBrian Foster 	error = xfs_itruncate_extents_flags(&tp, ip, XFS_DATA_FORK,
7484e529339SBrian Foster 				XFS_ISIZE(ip), XFS_BMAPI_NODISCARD);
7497d88329eSDarrick J. Wong 	if (error)
7507d88329eSDarrick J. Wong 		goto err_cancel;
7517d88329eSDarrick J. Wong 
7527d88329eSDarrick J. Wong 	error = xfs_trans_commit(tp);
7537d88329eSDarrick J. Wong 	if (error)
7547d88329eSDarrick J. Wong 		goto out_unlock;
7557d88329eSDarrick J. Wong 
7567d88329eSDarrick J. Wong 	xfs_inode_clear_eofblocks_tag(ip);
7577d88329eSDarrick J. Wong 	goto out_unlock;
7587d88329eSDarrick J. Wong 
7597d88329eSDarrick J. Wong err_cancel:
760c24b5dfaSDave Chinner 	/*
761c24b5dfaSDave Chinner 	 * If we get an error at this point we simply don't
762c24b5dfaSDave Chinner 	 * bother truncating the file.
763c24b5dfaSDave Chinner 	 */
7644906e215SChristoph Hellwig 	xfs_trans_cancel(tp);
7657d88329eSDarrick J. Wong out_unlock:
766c24b5dfaSDave Chinner 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
767c24b5dfaSDave Chinner 	return error;
768c24b5dfaSDave Chinner }
769c24b5dfaSDave Chinner 
77083aee9e4SChristoph Hellwig int
771c24b5dfaSDave Chinner xfs_alloc_file_space(
77283aee9e4SChristoph Hellwig 	struct xfs_inode	*ip,
773c24b5dfaSDave Chinner 	xfs_off_t		offset,
7744d1b97f9SDarrick J. Wong 	xfs_off_t		len)
775c24b5dfaSDave Chinner {
776c24b5dfaSDave Chinner 	xfs_mount_t		*mp = ip->i_mount;
777c24b5dfaSDave Chinner 	xfs_off_t		count;
778c24b5dfaSDave Chinner 	xfs_filblks_t		allocated_fsb;
779c24b5dfaSDave Chinner 	xfs_filblks_t		allocatesize_fsb;
780c24b5dfaSDave Chinner 	xfs_extlen_t		extsz, temp;
781c24b5dfaSDave Chinner 	xfs_fileoff_t		startoffset_fsb;
782e093c4beSMax Reitz 	xfs_fileoff_t		endoffset_fsb;
783c24b5dfaSDave Chinner 	int			nimaps;
784c24b5dfaSDave Chinner 	int			rt;
785c24b5dfaSDave Chinner 	xfs_trans_t		*tp;
786c24b5dfaSDave Chinner 	xfs_bmbt_irec_t		imaps[1], *imapp;
787c24b5dfaSDave Chinner 	int			error;
788c24b5dfaSDave Chinner 
789c24b5dfaSDave Chinner 	trace_xfs_alloc_file_space(ip);
790c24b5dfaSDave Chinner 
79175c8c50fSDave Chinner 	if (xfs_is_shutdown(mp))
7922451337dSDave Chinner 		return -EIO;
793c24b5dfaSDave Chinner 
794c14cfccaSDarrick J. Wong 	error = xfs_qm_dqattach(ip);
795c24b5dfaSDave Chinner 	if (error)
796c24b5dfaSDave Chinner 		return error;
797c24b5dfaSDave Chinner 
798c24b5dfaSDave Chinner 	if (len <= 0)
7992451337dSDave Chinner 		return -EINVAL;
800c24b5dfaSDave Chinner 
801c24b5dfaSDave Chinner 	rt = XFS_IS_REALTIME_INODE(ip);
802c24b5dfaSDave Chinner 	extsz = xfs_get_extsz_hint(ip);
803c24b5dfaSDave Chinner 
804c24b5dfaSDave Chinner 	count = len;
805c24b5dfaSDave Chinner 	imapp = &imaps[0];
806c24b5dfaSDave Chinner 	nimaps = 1;
807c24b5dfaSDave Chinner 	startoffset_fsb	= XFS_B_TO_FSBT(mp, offset);
808e093c4beSMax Reitz 	endoffset_fsb = XFS_B_TO_FSB(mp, offset + count);
809e093c4beSMax Reitz 	allocatesize_fsb = endoffset_fsb - startoffset_fsb;
810c24b5dfaSDave Chinner 
811c24b5dfaSDave Chinner 	/*
812c24b5dfaSDave Chinner 	 * Allocate file space until done or until there is an error
813c24b5dfaSDave Chinner 	 */
814c24b5dfaSDave Chinner 	while (allocatesize_fsb && !error) {
815c24b5dfaSDave Chinner 		xfs_fileoff_t	s, e;
8163de4eb10SDarrick J. Wong 		unsigned int	dblocks, rblocks, resblks;
817c24b5dfaSDave Chinner 
818c24b5dfaSDave Chinner 		/*
819c24b5dfaSDave Chinner 		 * Determine space reservations for data/realtime.
820c24b5dfaSDave Chinner 		 */
821c24b5dfaSDave Chinner 		if (unlikely(extsz)) {
822c24b5dfaSDave Chinner 			s = startoffset_fsb;
823c24b5dfaSDave Chinner 			do_div(s, extsz);
824c24b5dfaSDave Chinner 			s *= extsz;
825c24b5dfaSDave Chinner 			e = startoffset_fsb + allocatesize_fsb;
8260703a8e1SDave Chinner 			div_u64_rem(startoffset_fsb, extsz, &temp);
8270703a8e1SDave Chinner 			if (temp)
828c24b5dfaSDave Chinner 				e += temp;
8290703a8e1SDave Chinner 			div_u64_rem(e, extsz, &temp);
8300703a8e1SDave Chinner 			if (temp)
831c24b5dfaSDave Chinner 				e += extsz - temp;
832c24b5dfaSDave Chinner 		} else {
833c24b5dfaSDave Chinner 			s = 0;
834c24b5dfaSDave Chinner 			e = allocatesize_fsb;
835c24b5dfaSDave Chinner 		}
836c24b5dfaSDave Chinner 
837c24b5dfaSDave Chinner 		/*
838c24b5dfaSDave Chinner 		 * The transaction reservation is limited to a 32-bit block
839c24b5dfaSDave Chinner 		 * count, hence we need to limit the number of blocks we are
840c24b5dfaSDave Chinner 		 * trying to reserve to avoid an overflow. We can't allocate
841c24b5dfaSDave Chinner 		 * more than @nimaps extents, and an extent is limited on disk
842*95f0b95eSChandan Babu R 		 * to XFS_BMBT_MAX_EXTLEN (21 bits), so use that to enforce the
843*95f0b95eSChandan Babu R 		 * limit.
844c24b5dfaSDave Chinner 		 */
845*95f0b95eSChandan Babu R 		resblks = min_t(xfs_fileoff_t, (e - s),
846*95f0b95eSChandan Babu R 				(XFS_MAX_BMBT_EXTLEN * nimaps));
847c24b5dfaSDave Chinner 		if (unlikely(rt)) {
84802b7ee4eSDarrick J. Wong 			dblocks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
84902b7ee4eSDarrick J. Wong 			rblocks = resblks;
850c24b5dfaSDave Chinner 		} else {
85102b7ee4eSDarrick J. Wong 			dblocks = XFS_DIOSTRAT_SPACE_RES(mp, resblks);
85202b7ee4eSDarrick J. Wong 			rblocks = 0;
853c24b5dfaSDave Chinner 		}
854c24b5dfaSDave Chinner 
8553de4eb10SDarrick J. Wong 		error = xfs_trans_alloc_inode(ip, &M_RES(mp)->tr_write,
8563de4eb10SDarrick J. Wong 				dblocks, rblocks, false, &tp);
857c24b5dfaSDave Chinner 		if (error)
8583de4eb10SDarrick J. Wong 			break;
859c24b5dfaSDave Chinner 
860727e1acdSChandan Babu R 		error = xfs_iext_count_may_overflow(ip, XFS_DATA_FORK,
861727e1acdSChandan Babu R 				XFS_IEXT_ADD_NOSPLIT_CNT);
862727e1acdSChandan Babu R 		if (error)
86335b11010SDarrick J. Wong 			goto error;
864727e1acdSChandan Babu R 
865c24b5dfaSDave Chinner 		error = xfs_bmapi_write(tp, ip, startoffset_fsb,
8664d1b97f9SDarrick J. Wong 				allocatesize_fsb, XFS_BMAPI_PREALLOC, 0, imapp,
867da781e64SBrian Foster 				&nimaps);
868f6106efaSEric Sandeen 		if (error)
86935b11010SDarrick J. Wong 			goto error;
870c24b5dfaSDave Chinner 
8710b02c8c0SDave Chinner 		ip->i_diflags |= XFS_DIFLAG_PREALLOC;
8720b02c8c0SDave Chinner 		xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
8730b02c8c0SDave Chinner 
87470393313SChristoph Hellwig 		error = xfs_trans_commit(tp);
875c24b5dfaSDave Chinner 		xfs_iunlock(ip, XFS_ILOCK_EXCL);
876f6106efaSEric Sandeen 		if (error)
877c24b5dfaSDave Chinner 			break;
878c24b5dfaSDave Chinner 
879c24b5dfaSDave Chinner 		allocated_fsb = imapp->br_blockcount;
880c24b5dfaSDave Chinner 
881c24b5dfaSDave Chinner 		if (nimaps == 0) {
8822451337dSDave Chinner 			error = -ENOSPC;
883c24b5dfaSDave Chinner 			break;
884c24b5dfaSDave Chinner 		}
885c24b5dfaSDave Chinner 
886c24b5dfaSDave Chinner 		startoffset_fsb += allocated_fsb;
887c24b5dfaSDave Chinner 		allocatesize_fsb -= allocated_fsb;
888c24b5dfaSDave Chinner 	}
889c24b5dfaSDave Chinner 
890c24b5dfaSDave Chinner 	return error;
891c24b5dfaSDave Chinner 
89235b11010SDarrick J. Wong error:
8934906e215SChristoph Hellwig 	xfs_trans_cancel(tp);
894c24b5dfaSDave Chinner 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
895c24b5dfaSDave Chinner 	return error;
896c24b5dfaSDave Chinner }
897c24b5dfaSDave Chinner 
898bdb0d04fSChristoph Hellwig static int
899bdb0d04fSChristoph Hellwig xfs_unmap_extent(
90083aee9e4SChristoph Hellwig 	struct xfs_inode	*ip,
901bdb0d04fSChristoph Hellwig 	xfs_fileoff_t		startoffset_fsb,
902bdb0d04fSChristoph Hellwig 	xfs_filblks_t		len_fsb,
903bdb0d04fSChristoph Hellwig 	int			*done)
904c24b5dfaSDave Chinner {
905bdb0d04fSChristoph Hellwig 	struct xfs_mount	*mp = ip->i_mount;
906bdb0d04fSChristoph Hellwig 	struct xfs_trans	*tp;
907bdb0d04fSChristoph Hellwig 	uint			resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
908bdb0d04fSChristoph Hellwig 	int			error;
909c24b5dfaSDave Chinner 
9103de4eb10SDarrick J. Wong 	error = xfs_trans_alloc_inode(ip, &M_RES(mp)->tr_write, resblks, 0,
9113a1af6c3SDarrick J. Wong 			false, &tp);
912bdb0d04fSChristoph Hellwig 	if (error)
9133a1af6c3SDarrick J. Wong 		return error;
914c24b5dfaSDave Chinner 
91585ef08b5SChandan Babu R 	error = xfs_iext_count_may_overflow(ip, XFS_DATA_FORK,
91685ef08b5SChandan Babu R 			XFS_IEXT_PUNCH_HOLE_CNT);
91785ef08b5SChandan Babu R 	if (error)
91885ef08b5SChandan Babu R 		goto out_trans_cancel;
91985ef08b5SChandan Babu R 
9202af52842SBrian Foster 	error = xfs_bunmapi(tp, ip, startoffset_fsb, len_fsb, 0, 2, done);
921bdb0d04fSChristoph Hellwig 	if (error)
922c8eac49eSBrian Foster 		goto out_trans_cancel;
923bdb0d04fSChristoph Hellwig 
924bdb0d04fSChristoph Hellwig 	error = xfs_trans_commit(tp);
925bdb0d04fSChristoph Hellwig out_unlock:
926bdb0d04fSChristoph Hellwig 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
927bdb0d04fSChristoph Hellwig 	return error;
928bdb0d04fSChristoph Hellwig 
929bdb0d04fSChristoph Hellwig out_trans_cancel:
930bdb0d04fSChristoph Hellwig 	xfs_trans_cancel(tp);
931bdb0d04fSChristoph Hellwig 	goto out_unlock;
932bdb0d04fSChristoph Hellwig }
933bdb0d04fSChristoph Hellwig 
934249bd908SDave Chinner /* Caller must first wait for the completion of any pending DIOs if required. */
9352c307174SDave Chinner int
936bdb0d04fSChristoph Hellwig xfs_flush_unmap_range(
937bdb0d04fSChristoph Hellwig 	struct xfs_inode	*ip,
938bdb0d04fSChristoph Hellwig 	xfs_off_t		offset,
939bdb0d04fSChristoph Hellwig 	xfs_off_t		len)
940bdb0d04fSChristoph Hellwig {
941bdb0d04fSChristoph Hellwig 	struct xfs_mount	*mp = ip->i_mount;
942bdb0d04fSChristoph Hellwig 	struct inode		*inode = VFS_I(ip);
943bdb0d04fSChristoph Hellwig 	xfs_off_t		rounding, start, end;
944bdb0d04fSChristoph Hellwig 	int			error;
945bdb0d04fSChristoph Hellwig 
94620bd8e63SDarrick J. Wong 	rounding = max_t(xfs_off_t, mp->m_sb.sb_blocksize, PAGE_SIZE);
947bdb0d04fSChristoph Hellwig 	start = round_down(offset, rounding);
948bdb0d04fSChristoph Hellwig 	end = round_up(offset + len, rounding) - 1;
949bdb0d04fSChristoph Hellwig 
950bdb0d04fSChristoph Hellwig 	error = filemap_write_and_wait_range(inode->i_mapping, start, end);
951c24b5dfaSDave Chinner 	if (error)
952c24b5dfaSDave Chinner 		return error;
953bdb0d04fSChristoph Hellwig 	truncate_pagecache_range(inode, start, end);
954bdb0d04fSChristoph Hellwig 	return 0;
955c24b5dfaSDave Chinner }
956c24b5dfaSDave Chinner 
957c24b5dfaSDave Chinner int
958c24b5dfaSDave Chinner xfs_free_file_space(
959c24b5dfaSDave Chinner 	struct xfs_inode	*ip,
960c24b5dfaSDave Chinner 	xfs_off_t		offset,
961c24b5dfaSDave Chinner 	xfs_off_t		len)
962c24b5dfaSDave Chinner {
963bdb0d04fSChristoph Hellwig 	struct xfs_mount	*mp = ip->i_mount;
964c24b5dfaSDave Chinner 	xfs_fileoff_t		startoffset_fsb;
965bdb0d04fSChristoph Hellwig 	xfs_fileoff_t		endoffset_fsb;
9663c2bdc91SChristoph Hellwig 	int			done = 0, error;
967c24b5dfaSDave Chinner 
968c24b5dfaSDave Chinner 	trace_xfs_free_file_space(ip);
969c24b5dfaSDave Chinner 
970c14cfccaSDarrick J. Wong 	error = xfs_qm_dqattach(ip);
971c24b5dfaSDave Chinner 	if (error)
972c24b5dfaSDave Chinner 		return error;
973c24b5dfaSDave Chinner 
974c24b5dfaSDave Chinner 	if (len <= 0)	/* if nothing being freed */
975bdb0d04fSChristoph Hellwig 		return 0;
976bdb0d04fSChristoph Hellwig 
977c24b5dfaSDave Chinner 	startoffset_fsb = XFS_B_TO_FSB(mp, offset);
978c24b5dfaSDave Chinner 	endoffset_fsb = XFS_B_TO_FSBT(mp, offset + len);
979c24b5dfaSDave Chinner 
980fe341eb1SDarrick J. Wong 	/* We can only free complete realtime extents. */
98125219dbfSDarrick J. Wong 	if (XFS_IS_REALTIME_INODE(ip) && mp->m_sb.sb_rextsize > 1) {
98225219dbfSDarrick J. Wong 		startoffset_fsb = roundup_64(startoffset_fsb,
98325219dbfSDarrick J. Wong 					     mp->m_sb.sb_rextsize);
98425219dbfSDarrick J. Wong 		endoffset_fsb = rounddown_64(endoffset_fsb,
98525219dbfSDarrick J. Wong 					     mp->m_sb.sb_rextsize);
986fe341eb1SDarrick J. Wong 	}
987fe341eb1SDarrick J. Wong 
988bdb0d04fSChristoph Hellwig 	/*
989daa79baeSChristoph Hellwig 	 * Need to zero the stuff we're not freeing, on disk.
990bdb0d04fSChristoph Hellwig 	 */
9913c2bdc91SChristoph Hellwig 	if (endoffset_fsb > startoffset_fsb) {
9923c2bdc91SChristoph Hellwig 		while (!done) {
993bdb0d04fSChristoph Hellwig 			error = xfs_unmap_extent(ip, startoffset_fsb,
994bdb0d04fSChristoph Hellwig 					endoffset_fsb - startoffset_fsb, &done);
9953c2bdc91SChristoph Hellwig 			if (error)
9963c2bdc91SChristoph Hellwig 				return error;
9973c2bdc91SChristoph Hellwig 		}
998c24b5dfaSDave Chinner 	}
999c24b5dfaSDave Chinner 
10003c2bdc91SChristoph Hellwig 	/*
10013c2bdc91SChristoph Hellwig 	 * Now that we've unmap all full blocks we'll have to zero out any
1002f1ba5fafSShiyang Ruan 	 * partial block at the beginning and/or end.  xfs_zero_range is smart
1003f5c54717SChristoph Hellwig 	 * enough to skip any holes, including those we just created, but we
1004f5c54717SChristoph Hellwig 	 * must take care not to zero beyond EOF and enlarge i_size.
10053c2bdc91SChristoph Hellwig 	 */
10063dd09d5aSCalvin Owens 	if (offset >= XFS_ISIZE(ip))
10073dd09d5aSCalvin Owens 		return 0;
10083dd09d5aSCalvin Owens 	if (offset + len > XFS_ISIZE(ip))
10093dd09d5aSCalvin Owens 		len = XFS_ISIZE(ip) - offset;
1010f1ba5fafSShiyang Ruan 	error = xfs_zero_range(ip, offset, len, NULL);
1011e53c4b59SDarrick J. Wong 	if (error)
1012e53c4b59SDarrick J. Wong 		return error;
1013e53c4b59SDarrick J. Wong 
1014e53c4b59SDarrick J. Wong 	/*
1015e53c4b59SDarrick J. Wong 	 * If we zeroed right up to EOF and EOF straddles a page boundary we
1016e53c4b59SDarrick J. Wong 	 * must make sure that the post-EOF area is also zeroed because the
1017f1ba5fafSShiyang Ruan 	 * page could be mmap'd and xfs_zero_range doesn't do that for us.
1018e53c4b59SDarrick J. Wong 	 * Writeback of the eof page will do this, albeit clumsily.
1019e53c4b59SDarrick J. Wong 	 */
1020a579121fSDarrick J. Wong 	if (offset + len >= XFS_ISIZE(ip) && offset_in_page(offset + len) > 0) {
1021e53c4b59SDarrick J. Wong 		error = filemap_write_and_wait_range(VFS_I(ip)->i_mapping,
1022a579121fSDarrick J. Wong 				round_down(offset + len, PAGE_SIZE), LLONG_MAX);
1023e53c4b59SDarrick J. Wong 	}
1024e53c4b59SDarrick J. Wong 
1025e53c4b59SDarrick J. Wong 	return error;
1026c24b5dfaSDave Chinner }
1027c24b5dfaSDave Chinner 
102872c1a739Skbuild test robot static int
10294ed36c6bSChristoph Hellwig xfs_prepare_shift(
1030e1d8fb88SNamjae Jeon 	struct xfs_inode	*ip,
10314ed36c6bSChristoph Hellwig 	loff_t			offset)
1032e1d8fb88SNamjae Jeon {
1033d0c22041SBrian Foster 	struct xfs_mount	*mp = ip->i_mount;
1034e1d8fb88SNamjae Jeon 	int			error;
1035f71721d0SBrian Foster 
1036f71721d0SBrian Foster 	/*
1037f71721d0SBrian Foster 	 * Trim eofblocks to avoid shifting uninitialized post-eof preallocation
1038f71721d0SBrian Foster 	 * into the accessible region of the file.
1039f71721d0SBrian Foster 	 */
104041b9d726SBrian Foster 	if (xfs_can_free_eofblocks(ip, true)) {
1041a36b9261SBrian Foster 		error = xfs_free_eofblocks(ip);
104241b9d726SBrian Foster 		if (error)
104341b9d726SBrian Foster 			return error;
104441b9d726SBrian Foster 	}
10451669a8caSDave Chinner 
1046f71721d0SBrian Foster 	/*
1047d0c22041SBrian Foster 	 * Shift operations must stabilize the start block offset boundary along
1048d0c22041SBrian Foster 	 * with the full range of the operation. If we don't, a COW writeback
1049d0c22041SBrian Foster 	 * completion could race with an insert, front merge with the start
1050d0c22041SBrian Foster 	 * extent (after split) during the shift and corrupt the file. Start
1051d0c22041SBrian Foster 	 * with the block just prior to the start to stabilize the boundary.
1052d0c22041SBrian Foster 	 */
105320bd8e63SDarrick J. Wong 	offset = round_down(offset, mp->m_sb.sb_blocksize);
1054d0c22041SBrian Foster 	if (offset)
105520bd8e63SDarrick J. Wong 		offset -= mp->m_sb.sb_blocksize;
1056d0c22041SBrian Foster 
1057d0c22041SBrian Foster 	/*
1058f71721d0SBrian Foster 	 * Writeback and invalidate cache for the remainder of the file as we're
1059a904b1caSNamjae Jeon 	 * about to shift down every extent from offset to EOF.
1060f71721d0SBrian Foster 	 */
10617f9f71beSDave Chinner 	error = xfs_flush_unmap_range(ip, offset, XFS_ISIZE(ip));
10621749d1eaSBrian Foster 	if (error)
10631749d1eaSBrian Foster 		return error;
1064e1d8fb88SNamjae Jeon 
1065a904b1caSNamjae Jeon 	/*
10663af423b0SDarrick J. Wong 	 * Clean out anything hanging around in the cow fork now that
10673af423b0SDarrick J. Wong 	 * we've flushed all the dirty data out to disk to avoid having
10683af423b0SDarrick J. Wong 	 * CoW extents at the wrong offsets.
10693af423b0SDarrick J. Wong 	 */
107051d62690SChristoph Hellwig 	if (xfs_inode_has_cow_data(ip)) {
10713af423b0SDarrick J. Wong 		error = xfs_reflink_cancel_cow_range(ip, offset, NULLFILEOFF,
10723af423b0SDarrick J. Wong 				true);
10733af423b0SDarrick J. Wong 		if (error)
10743af423b0SDarrick J. Wong 			return error;
10753af423b0SDarrick J. Wong 	}
10763af423b0SDarrick J. Wong 
10774ed36c6bSChristoph Hellwig 	return 0;
1078e1d8fb88SNamjae Jeon }
1079e1d8fb88SNamjae Jeon 
1080e1d8fb88SNamjae Jeon /*
1081a904b1caSNamjae Jeon  * xfs_collapse_file_space()
1082a904b1caSNamjae Jeon  *	This routine frees disk space and shift extent for the given file.
1083a904b1caSNamjae Jeon  *	The first thing we do is to free data blocks in the specified range
1084a904b1caSNamjae Jeon  *	by calling xfs_free_file_space(). It would also sync dirty data
1085a904b1caSNamjae Jeon  *	and invalidate page cache over the region on which collapse range
1086a904b1caSNamjae Jeon  *	is working. And Shift extent records to the left to cover a hole.
1087a904b1caSNamjae Jeon  * RETURNS:
1088a904b1caSNamjae Jeon  *	0 on success
1089a904b1caSNamjae Jeon  *	errno on error
1090a904b1caSNamjae Jeon  *
1091a904b1caSNamjae Jeon  */
1092a904b1caSNamjae Jeon int
1093a904b1caSNamjae Jeon xfs_collapse_file_space(
1094a904b1caSNamjae Jeon 	struct xfs_inode	*ip,
1095a904b1caSNamjae Jeon 	xfs_off_t		offset,
1096a904b1caSNamjae Jeon 	xfs_off_t		len)
1097a904b1caSNamjae Jeon {
10984ed36c6bSChristoph Hellwig 	struct xfs_mount	*mp = ip->i_mount;
10994ed36c6bSChristoph Hellwig 	struct xfs_trans	*tp;
1100a904b1caSNamjae Jeon 	int			error;
11014ed36c6bSChristoph Hellwig 	xfs_fileoff_t		next_fsb = XFS_B_TO_FSB(mp, offset + len);
11024ed36c6bSChristoph Hellwig 	xfs_fileoff_t		shift_fsb = XFS_B_TO_FSB(mp, len);
1103ecfea3f0SChristoph Hellwig 	bool			done = false;
1104a904b1caSNamjae Jeon 
1105a904b1caSNamjae Jeon 	ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
11069ad1a23aSChristoph Hellwig 	ASSERT(xfs_isilocked(ip, XFS_MMAPLOCK_EXCL));
11079ad1a23aSChristoph Hellwig 
1108a904b1caSNamjae Jeon 	trace_xfs_collapse_file_space(ip);
1109a904b1caSNamjae Jeon 
1110a904b1caSNamjae Jeon 	error = xfs_free_file_space(ip, offset, len);
1111a904b1caSNamjae Jeon 	if (error)
1112a904b1caSNamjae Jeon 		return error;
1113a904b1caSNamjae Jeon 
11144ed36c6bSChristoph Hellwig 	error = xfs_prepare_shift(ip, offset);
11154ed36c6bSChristoph Hellwig 	if (error)
11164ed36c6bSChristoph Hellwig 		return error;
11174ed36c6bSChristoph Hellwig 
1118211683b2SBrian Foster 	error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0, 0, &tp);
11194ed36c6bSChristoph Hellwig 	if (error)
1120211683b2SBrian Foster 		return error;
11214ed36c6bSChristoph Hellwig 
11224ed36c6bSChristoph Hellwig 	xfs_ilock(ip, XFS_ILOCK_EXCL);
1123211683b2SBrian Foster 	xfs_trans_ijoin(tp, ip, 0);
11244ed36c6bSChristoph Hellwig 
1125211683b2SBrian Foster 	while (!done) {
1126ecfea3f0SChristoph Hellwig 		error = xfs_bmap_collapse_extents(tp, ip, &next_fsb, shift_fsb,
1127333f950cSBrian Foster 				&done);
11284ed36c6bSChristoph Hellwig 		if (error)
1129c8eac49eSBrian Foster 			goto out_trans_cancel;
1130211683b2SBrian Foster 		if (done)
1131211683b2SBrian Foster 			break;
11324ed36c6bSChristoph Hellwig 
1133211683b2SBrian Foster 		/* finish any deferred frees and roll the transaction */
1134211683b2SBrian Foster 		error = xfs_defer_finish(&tp);
1135211683b2SBrian Foster 		if (error)
1136211683b2SBrian Foster 			goto out_trans_cancel;
11374ed36c6bSChristoph Hellwig 	}
11384ed36c6bSChristoph Hellwig 
1139211683b2SBrian Foster 	error = xfs_trans_commit(tp);
1140211683b2SBrian Foster 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
11414ed36c6bSChristoph Hellwig 	return error;
11424ed36c6bSChristoph Hellwig 
11434ed36c6bSChristoph Hellwig out_trans_cancel:
11444ed36c6bSChristoph Hellwig 	xfs_trans_cancel(tp);
1145211683b2SBrian Foster 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
11464ed36c6bSChristoph Hellwig 	return error;
1147a904b1caSNamjae Jeon }
1148a904b1caSNamjae Jeon 
1149a904b1caSNamjae Jeon /*
1150a904b1caSNamjae Jeon  * xfs_insert_file_space()
1151a904b1caSNamjae Jeon  *	This routine create hole space by shifting extents for the given file.
1152a904b1caSNamjae Jeon  *	The first thing we do is to sync dirty data and invalidate page cache
1153a904b1caSNamjae Jeon  *	over the region on which insert range is working. And split an extent
1154a904b1caSNamjae Jeon  *	to two extents at given offset by calling xfs_bmap_split_extent.
1155a904b1caSNamjae Jeon  *	And shift all extent records which are laying between [offset,
1156a904b1caSNamjae Jeon  *	last allocated extent] to the right to reserve hole range.
1157a904b1caSNamjae Jeon  * RETURNS:
1158a904b1caSNamjae Jeon  *	0 on success
1159a904b1caSNamjae Jeon  *	errno on error
1160a904b1caSNamjae Jeon  */
1161a904b1caSNamjae Jeon int
1162a904b1caSNamjae Jeon xfs_insert_file_space(
1163a904b1caSNamjae Jeon 	struct xfs_inode	*ip,
1164a904b1caSNamjae Jeon 	loff_t			offset,
1165a904b1caSNamjae Jeon 	loff_t			len)
1166a904b1caSNamjae Jeon {
11674ed36c6bSChristoph Hellwig 	struct xfs_mount	*mp = ip->i_mount;
11684ed36c6bSChristoph Hellwig 	struct xfs_trans	*tp;
11694ed36c6bSChristoph Hellwig 	int			error;
11704ed36c6bSChristoph Hellwig 	xfs_fileoff_t		stop_fsb = XFS_B_TO_FSB(mp, offset);
11714ed36c6bSChristoph Hellwig 	xfs_fileoff_t		next_fsb = NULLFSBLOCK;
11724ed36c6bSChristoph Hellwig 	xfs_fileoff_t		shift_fsb = XFS_B_TO_FSB(mp, len);
1173ecfea3f0SChristoph Hellwig 	bool			done = false;
11744ed36c6bSChristoph Hellwig 
1175a904b1caSNamjae Jeon 	ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
11769ad1a23aSChristoph Hellwig 	ASSERT(xfs_isilocked(ip, XFS_MMAPLOCK_EXCL));
11779ad1a23aSChristoph Hellwig 
1178a904b1caSNamjae Jeon 	trace_xfs_insert_file_space(ip);
1179a904b1caSNamjae Jeon 
1180f62cb48eSDarrick J. Wong 	error = xfs_bmap_can_insert_extents(ip, stop_fsb, shift_fsb);
1181f62cb48eSDarrick J. Wong 	if (error)
1182f62cb48eSDarrick J. Wong 		return error;
1183f62cb48eSDarrick J. Wong 
11844ed36c6bSChristoph Hellwig 	error = xfs_prepare_shift(ip, offset);
11854ed36c6bSChristoph Hellwig 	if (error)
11864ed36c6bSChristoph Hellwig 		return error;
11874ed36c6bSChristoph Hellwig 
1188b73df17eSBrian Foster 	error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write,
1189b73df17eSBrian Foster 			XFS_DIOSTRAT_SPACE_RES(mp, 0), 0, 0, &tp);
1190b73df17eSBrian Foster 	if (error)
1191b73df17eSBrian Foster 		return error;
1192b73df17eSBrian Foster 
1193b73df17eSBrian Foster 	xfs_ilock(ip, XFS_ILOCK_EXCL);
1194dd87f87dSBrian Foster 	xfs_trans_ijoin(tp, ip, 0);
1195b73df17eSBrian Foster 
119685ef08b5SChandan Babu R 	error = xfs_iext_count_may_overflow(ip, XFS_DATA_FORK,
119785ef08b5SChandan Babu R 			XFS_IEXT_PUNCH_HOLE_CNT);
119885ef08b5SChandan Babu R 	if (error)
119985ef08b5SChandan Babu R 		goto out_trans_cancel;
120085ef08b5SChandan Babu R 
1201dd87f87dSBrian Foster 	/*
1202dd87f87dSBrian Foster 	 * The extent shifting code works on extent granularity. So, if stop_fsb
1203dd87f87dSBrian Foster 	 * is not the starting block of extent, we need to split the extent at
1204dd87f87dSBrian Foster 	 * stop_fsb.
1205dd87f87dSBrian Foster 	 */
1206b73df17eSBrian Foster 	error = xfs_bmap_split_extent(tp, ip, stop_fsb);
1207b73df17eSBrian Foster 	if (error)
1208b73df17eSBrian Foster 		goto out_trans_cancel;
1209b73df17eSBrian Foster 
1210dd87f87dSBrian Foster 	do {
12119c516e0eSBrian Foster 		error = xfs_defer_finish(&tp);
12124ed36c6bSChristoph Hellwig 		if (error)
1213dd87f87dSBrian Foster 			goto out_trans_cancel;
12144ed36c6bSChristoph Hellwig 
1215ecfea3f0SChristoph Hellwig 		error = xfs_bmap_insert_extents(tp, ip, &next_fsb, shift_fsb,
1216333f950cSBrian Foster 				&done, stop_fsb);
12174ed36c6bSChristoph Hellwig 		if (error)
1218c8eac49eSBrian Foster 			goto out_trans_cancel;
1219dd87f87dSBrian Foster 	} while (!done);
12204ed36c6bSChristoph Hellwig 
12214ed36c6bSChristoph Hellwig 	error = xfs_trans_commit(tp);
1222dd87f87dSBrian Foster 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
12234ed36c6bSChristoph Hellwig 	return error;
12244ed36c6bSChristoph Hellwig 
1225c8eac49eSBrian Foster out_trans_cancel:
12264ed36c6bSChristoph Hellwig 	xfs_trans_cancel(tp);
1227dd87f87dSBrian Foster 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
12284ed36c6bSChristoph Hellwig 	return error;
1229a904b1caSNamjae Jeon }
1230a904b1caSNamjae Jeon 
1231a904b1caSNamjae Jeon /*
1232a133d952SDave Chinner  * We need to check that the format of the data fork in the temporary inode is
1233a133d952SDave Chinner  * valid for the target inode before doing the swap. This is not a problem with
1234a133d952SDave Chinner  * attr1 because of the fixed fork offset, but attr2 has a dynamically sized
1235a133d952SDave Chinner  * data fork depending on the space the attribute fork is taking so we can get
1236a133d952SDave Chinner  * invalid formats on the target inode.
1237a133d952SDave Chinner  *
1238a133d952SDave Chinner  * E.g. target has space for 7 extents in extent format, temp inode only has
1239a133d952SDave Chinner  * space for 6.  If we defragment down to 7 extents, then the tmp format is a
1240a133d952SDave Chinner  * btree, but when swapped it needs to be in extent format. Hence we can't just
1241a133d952SDave Chinner  * blindly swap data forks on attr2 filesystems.
1242a133d952SDave Chinner  *
1243a133d952SDave Chinner  * Note that we check the swap in both directions so that we don't end up with
1244a133d952SDave Chinner  * a corrupt temporary inode, either.
1245a133d952SDave Chinner  *
1246a133d952SDave Chinner  * Note that fixing the way xfs_fsr sets up the attribute fork in the source
1247a133d952SDave Chinner  * inode will prevent this situation from occurring, so all we do here is
1248a133d952SDave Chinner  * reject and log the attempt. basically we are putting the responsibility on
1249a133d952SDave Chinner  * userspace to get this right.
1250a133d952SDave Chinner  */
1251a133d952SDave Chinner static int
1252a133d952SDave Chinner xfs_swap_extents_check_format(
1253e06259aaSDarrick J. Wong 	struct xfs_inode	*ip,	/* target inode */
1254e06259aaSDarrick J. Wong 	struct xfs_inode	*tip)	/* tmp inode */
1255a133d952SDave Chinner {
1256f7e67b20SChristoph Hellwig 	struct xfs_ifork	*ifp = &ip->i_df;
1257f7e67b20SChristoph Hellwig 	struct xfs_ifork	*tifp = &tip->i_df;
1258a133d952SDave Chinner 
1259765d3c39SDarrick J. Wong 	/* User/group/project quota ids must match if quotas are enforced. */
1260765d3c39SDarrick J. Wong 	if (XFS_IS_QUOTA_ON(ip->i_mount) &&
1261765d3c39SDarrick J. Wong 	    (!uid_eq(VFS_I(ip)->i_uid, VFS_I(tip)->i_uid) ||
1262765d3c39SDarrick J. Wong 	     !gid_eq(VFS_I(ip)->i_gid, VFS_I(tip)->i_gid) ||
1263ceaf603cSChristoph Hellwig 	     ip->i_projid != tip->i_projid))
1264765d3c39SDarrick J. Wong 		return -EINVAL;
1265765d3c39SDarrick J. Wong 
1266a133d952SDave Chinner 	/* Should never get a local format */
1267f7e67b20SChristoph Hellwig 	if (ifp->if_format == XFS_DINODE_FMT_LOCAL ||
1268f7e67b20SChristoph Hellwig 	    tifp->if_format == XFS_DINODE_FMT_LOCAL)
12692451337dSDave Chinner 		return -EINVAL;
1270a133d952SDave Chinner 
1271a133d952SDave Chinner 	/*
1272a133d952SDave Chinner 	 * if the target inode has less extents that then temporary inode then
1273a133d952SDave Chinner 	 * why did userspace call us?
1274a133d952SDave Chinner 	 */
1275f7e67b20SChristoph Hellwig 	if (ifp->if_nextents < tifp->if_nextents)
12762451337dSDave Chinner 		return -EINVAL;
1277a133d952SDave Chinner 
1278a133d952SDave Chinner 	/*
12791f08af52SDarrick J. Wong 	 * If we have to use the (expensive) rmap swap method, we can
12801f08af52SDarrick J. Wong 	 * handle any number of extents and any format.
12811f08af52SDarrick J. Wong 	 */
128238c26bfdSDave Chinner 	if (xfs_has_rmapbt(ip->i_mount))
12831f08af52SDarrick J. Wong 		return 0;
12841f08af52SDarrick J. Wong 
12851f08af52SDarrick J. Wong 	/*
1286a133d952SDave Chinner 	 * if the target inode is in extent form and the temp inode is in btree
1287a133d952SDave Chinner 	 * form then we will end up with the target inode in the wrong format
1288a133d952SDave Chinner 	 * as we already know there are less extents in the temp inode.
1289a133d952SDave Chinner 	 */
1290f7e67b20SChristoph Hellwig 	if (ifp->if_format == XFS_DINODE_FMT_EXTENTS &&
1291f7e67b20SChristoph Hellwig 	    tifp->if_format == XFS_DINODE_FMT_BTREE)
12922451337dSDave Chinner 		return -EINVAL;
1293a133d952SDave Chinner 
1294a133d952SDave Chinner 	/* Check temp in extent form to max in target */
1295f7e67b20SChristoph Hellwig 	if (tifp->if_format == XFS_DINODE_FMT_EXTENTS &&
1296f7e67b20SChristoph Hellwig 	    tifp->if_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK))
12972451337dSDave Chinner 		return -EINVAL;
1298a133d952SDave Chinner 
1299a133d952SDave Chinner 	/* Check target in extent form to max in temp */
1300f7e67b20SChristoph Hellwig 	if (ifp->if_format == XFS_DINODE_FMT_EXTENTS &&
1301f7e67b20SChristoph Hellwig 	    ifp->if_nextents > XFS_IFORK_MAXEXT(tip, XFS_DATA_FORK))
13022451337dSDave Chinner 		return -EINVAL;
1303a133d952SDave Chinner 
1304a133d952SDave Chinner 	/*
1305a133d952SDave Chinner 	 * If we are in a btree format, check that the temp root block will fit
1306a133d952SDave Chinner 	 * in the target and that it has enough extents to be in btree format
1307a133d952SDave Chinner 	 * in the target.
1308a133d952SDave Chinner 	 *
1309a133d952SDave Chinner 	 * Note that we have to be careful to allow btree->extent conversions
1310a133d952SDave Chinner 	 * (a common defrag case) which will occur when the temp inode is in
1311a133d952SDave Chinner 	 * extent format...
1312a133d952SDave Chinner 	 */
1313f7e67b20SChristoph Hellwig 	if (tifp->if_format == XFS_DINODE_FMT_BTREE) {
13140cbe48ccSArnd Bergmann 		if (XFS_IFORK_Q(ip) &&
1315f7e67b20SChristoph Hellwig 		    XFS_BMAP_BMDR_SPACE(tifp->if_broot) > XFS_IFORK_BOFF(ip))
13162451337dSDave Chinner 			return -EINVAL;
1317f7e67b20SChristoph Hellwig 		if (tifp->if_nextents <= XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK))
13182451337dSDave Chinner 			return -EINVAL;
1319a133d952SDave Chinner 	}
1320a133d952SDave Chinner 
1321a133d952SDave Chinner 	/* Reciprocal target->temp btree format checks */
1322f7e67b20SChristoph Hellwig 	if (ifp->if_format == XFS_DINODE_FMT_BTREE) {
13230cbe48ccSArnd Bergmann 		if (XFS_IFORK_Q(tip) &&
1324a133d952SDave Chinner 		    XFS_BMAP_BMDR_SPACE(ip->i_df.if_broot) > XFS_IFORK_BOFF(tip))
13252451337dSDave Chinner 			return -EINVAL;
1326f7e67b20SChristoph Hellwig 		if (ifp->if_nextents <= XFS_IFORK_MAXEXT(tip, XFS_DATA_FORK))
13272451337dSDave Chinner 			return -EINVAL;
1328a133d952SDave Chinner 	}
1329a133d952SDave Chinner 
1330a133d952SDave Chinner 	return 0;
1331a133d952SDave Chinner }
1332a133d952SDave Chinner 
13337abbb8f9SDave Chinner static int
13344ef897a2SDave Chinner xfs_swap_extent_flush(
13354ef897a2SDave Chinner 	struct xfs_inode	*ip)
13364ef897a2SDave Chinner {
13374ef897a2SDave Chinner 	int	error;
13384ef897a2SDave Chinner 
13394ef897a2SDave Chinner 	error = filemap_write_and_wait(VFS_I(ip)->i_mapping);
13404ef897a2SDave Chinner 	if (error)
13414ef897a2SDave Chinner 		return error;
13424ef897a2SDave Chinner 	truncate_pagecache_range(VFS_I(ip), 0, -1);
13434ef897a2SDave Chinner 
13444ef897a2SDave Chinner 	/* Verify O_DIRECT for ftmp */
13454ef897a2SDave Chinner 	if (VFS_I(ip)->i_mapping->nrpages)
13464ef897a2SDave Chinner 		return -EINVAL;
13474ef897a2SDave Chinner 	return 0;
13484ef897a2SDave Chinner }
13494ef897a2SDave Chinner 
13501f08af52SDarrick J. Wong /*
13511f08af52SDarrick J. Wong  * Move extents from one file to another, when rmap is enabled.
13521f08af52SDarrick J. Wong  */
13531f08af52SDarrick J. Wong STATIC int
13541f08af52SDarrick J. Wong xfs_swap_extent_rmap(
13551f08af52SDarrick J. Wong 	struct xfs_trans		**tpp,
13561f08af52SDarrick J. Wong 	struct xfs_inode		*ip,
13571f08af52SDarrick J. Wong 	struct xfs_inode		*tip)
13581f08af52SDarrick J. Wong {
13597a7943c7SBrian Foster 	struct xfs_trans		*tp = *tpp;
13601f08af52SDarrick J. Wong 	struct xfs_bmbt_irec		irec;
13611f08af52SDarrick J. Wong 	struct xfs_bmbt_irec		uirec;
13621f08af52SDarrick J. Wong 	struct xfs_bmbt_irec		tirec;
13631f08af52SDarrick J. Wong 	xfs_fileoff_t			offset_fsb;
13641f08af52SDarrick J. Wong 	xfs_fileoff_t			end_fsb;
13651f08af52SDarrick J. Wong 	xfs_filblks_t			count_fsb;
13661f08af52SDarrick J. Wong 	int				error;
13671f08af52SDarrick J. Wong 	xfs_filblks_t			ilen;
13681f08af52SDarrick J. Wong 	xfs_filblks_t			rlen;
13691f08af52SDarrick J. Wong 	int				nimaps;
1370c8ce540dSDarrick J. Wong 	uint64_t			tip_flags2;
13711f08af52SDarrick J. Wong 
13721f08af52SDarrick J. Wong 	/*
13731f08af52SDarrick J. Wong 	 * If the source file has shared blocks, we must flag the donor
13741f08af52SDarrick J. Wong 	 * file as having shared blocks so that we get the shared-block
13751f08af52SDarrick J. Wong 	 * rmap functions when we go to fix up the rmaps.  The flags
13761f08af52SDarrick J. Wong 	 * will be switch for reals later.
13771f08af52SDarrick J. Wong 	 */
13783e09ab8fSChristoph Hellwig 	tip_flags2 = tip->i_diflags2;
13793e09ab8fSChristoph Hellwig 	if (ip->i_diflags2 & XFS_DIFLAG2_REFLINK)
13803e09ab8fSChristoph Hellwig 		tip->i_diflags2 |= XFS_DIFLAG2_REFLINK;
13811f08af52SDarrick J. Wong 
13821f08af52SDarrick J. Wong 	offset_fsb = 0;
13831f08af52SDarrick J. Wong 	end_fsb = XFS_B_TO_FSB(ip->i_mount, i_size_read(VFS_I(ip)));
13841f08af52SDarrick J. Wong 	count_fsb = (xfs_filblks_t)(end_fsb - offset_fsb);
13851f08af52SDarrick J. Wong 
13861f08af52SDarrick J. Wong 	while (count_fsb) {
13871f08af52SDarrick J. Wong 		/* Read extent from the donor file */
13881f08af52SDarrick J. Wong 		nimaps = 1;
13891f08af52SDarrick J. Wong 		error = xfs_bmapi_read(tip, offset_fsb, count_fsb, &tirec,
13901f08af52SDarrick J. Wong 				&nimaps, 0);
13911f08af52SDarrick J. Wong 		if (error)
13921f08af52SDarrick J. Wong 			goto out;
13931f08af52SDarrick J. Wong 		ASSERT(nimaps == 1);
13941f08af52SDarrick J. Wong 		ASSERT(tirec.br_startblock != DELAYSTARTBLOCK);
13951f08af52SDarrick J. Wong 
13961f08af52SDarrick J. Wong 		trace_xfs_swap_extent_rmap_remap(tip, &tirec);
13971f08af52SDarrick J. Wong 		ilen = tirec.br_blockcount;
13981f08af52SDarrick J. Wong 
13991f08af52SDarrick J. Wong 		/* Unmap the old blocks in the source file. */
14001f08af52SDarrick J. Wong 		while (tirec.br_blockcount) {
1401c8eac49eSBrian Foster 			ASSERT(tp->t_firstblock == NULLFSBLOCK);
14021f08af52SDarrick J. Wong 			trace_xfs_swap_extent_rmap_remap_piece(tip, &tirec);
14031f08af52SDarrick J. Wong 
14041f08af52SDarrick J. Wong 			/* Read extent from the source file */
14051f08af52SDarrick J. Wong 			nimaps = 1;
14061f08af52SDarrick J. Wong 			error = xfs_bmapi_read(ip, tirec.br_startoff,
14071f08af52SDarrick J. Wong 					tirec.br_blockcount, &irec,
14081f08af52SDarrick J. Wong 					&nimaps, 0);
14091f08af52SDarrick J. Wong 			if (error)
1410d5a2e289SBrian Foster 				goto out;
14111f08af52SDarrick J. Wong 			ASSERT(nimaps == 1);
14121f08af52SDarrick J. Wong 			ASSERT(tirec.br_startoff == irec.br_startoff);
14131f08af52SDarrick J. Wong 			trace_xfs_swap_extent_rmap_remap_piece(ip, &irec);
14141f08af52SDarrick J. Wong 
14151f08af52SDarrick J. Wong 			/* Trim the extent. */
14161f08af52SDarrick J. Wong 			uirec = tirec;
14171f08af52SDarrick J. Wong 			uirec.br_blockcount = rlen = min_t(xfs_filblks_t,
14181f08af52SDarrick J. Wong 					tirec.br_blockcount,
14191f08af52SDarrick J. Wong 					irec.br_blockcount);
14201f08af52SDarrick J. Wong 			trace_xfs_swap_extent_rmap_remap_piece(tip, &uirec);
14211f08af52SDarrick J. Wong 
1422bcc561f2SChandan Babu R 			if (xfs_bmap_is_real_extent(&uirec)) {
1423bcc561f2SChandan Babu R 				error = xfs_iext_count_may_overflow(ip,
1424bcc561f2SChandan Babu R 						XFS_DATA_FORK,
1425bcc561f2SChandan Babu R 						XFS_IEXT_SWAP_RMAP_CNT);
1426bcc561f2SChandan Babu R 				if (error)
1427bcc561f2SChandan Babu R 					goto out;
1428bcc561f2SChandan Babu R 			}
1429bcc561f2SChandan Babu R 
1430bcc561f2SChandan Babu R 			if (xfs_bmap_is_real_extent(&irec)) {
1431bcc561f2SChandan Babu R 				error = xfs_iext_count_may_overflow(tip,
1432bcc561f2SChandan Babu R 						XFS_DATA_FORK,
1433bcc561f2SChandan Babu R 						XFS_IEXT_SWAP_RMAP_CNT);
1434bcc561f2SChandan Babu R 				if (error)
1435bcc561f2SChandan Babu R 					goto out;
1436bcc561f2SChandan Babu R 			}
1437bcc561f2SChandan Babu R 
14381f08af52SDarrick J. Wong 			/* Remove the mapping from the donor file. */
14393e08f42aSDarrick J. Wong 			xfs_bmap_unmap_extent(tp, tip, &uirec);
14401f08af52SDarrick J. Wong 
14411f08af52SDarrick J. Wong 			/* Remove the mapping from the source file. */
14423e08f42aSDarrick J. Wong 			xfs_bmap_unmap_extent(tp, ip, &irec);
14431f08af52SDarrick J. Wong 
14441f08af52SDarrick J. Wong 			/* Map the donor file's blocks into the source file. */
14453e08f42aSDarrick J. Wong 			xfs_bmap_map_extent(tp, ip, &uirec);
14461f08af52SDarrick J. Wong 
14471f08af52SDarrick J. Wong 			/* Map the source file's blocks into the donor file. */
14483e08f42aSDarrick J. Wong 			xfs_bmap_map_extent(tp, tip, &irec);
14491f08af52SDarrick J. Wong 
14509e28a242SBrian Foster 			error = xfs_defer_finish(tpp);
14517a7943c7SBrian Foster 			tp = *tpp;
14521f08af52SDarrick J. Wong 			if (error)
14539b1f4e98SBrian Foster 				goto out;
14541f08af52SDarrick J. Wong 
14551f08af52SDarrick J. Wong 			tirec.br_startoff += rlen;
14561f08af52SDarrick J. Wong 			if (tirec.br_startblock != HOLESTARTBLOCK &&
14571f08af52SDarrick J. Wong 			    tirec.br_startblock != DELAYSTARTBLOCK)
14581f08af52SDarrick J. Wong 				tirec.br_startblock += rlen;
14591f08af52SDarrick J. Wong 			tirec.br_blockcount -= rlen;
14601f08af52SDarrick J. Wong 		}
14611f08af52SDarrick J. Wong 
14621f08af52SDarrick J. Wong 		/* Roll on... */
14631f08af52SDarrick J. Wong 		count_fsb -= ilen;
14641f08af52SDarrick J. Wong 		offset_fsb += ilen;
14651f08af52SDarrick J. Wong 	}
14661f08af52SDarrick J. Wong 
14673e09ab8fSChristoph Hellwig 	tip->i_diflags2 = tip_flags2;
14681f08af52SDarrick J. Wong 	return 0;
14691f08af52SDarrick J. Wong 
14701f08af52SDarrick J. Wong out:
14711f08af52SDarrick J. Wong 	trace_xfs_swap_extent_rmap_error(ip, error, _RET_IP_);
14723e09ab8fSChristoph Hellwig 	tip->i_diflags2 = tip_flags2;
14731f08af52SDarrick J. Wong 	return error;
14741f08af52SDarrick J. Wong }
14751f08af52SDarrick J. Wong 
147639aff5fdSDarrick J. Wong /* Swap the extents of two files by swapping data forks. */
147739aff5fdSDarrick J. Wong STATIC int
147839aff5fdSDarrick J. Wong xfs_swap_extent_forks(
147939aff5fdSDarrick J. Wong 	struct xfs_trans	*tp,
148039aff5fdSDarrick J. Wong 	struct xfs_inode	*ip,
148139aff5fdSDarrick J. Wong 	struct xfs_inode	*tip,
148239aff5fdSDarrick J. Wong 	int			*src_log_flags,
148339aff5fdSDarrick J. Wong 	int			*target_log_flags)
148439aff5fdSDarrick J. Wong {
1485e7f5d5caSDarrick J. Wong 	xfs_filblks_t		aforkblks = 0;
1486e7f5d5caSDarrick J. Wong 	xfs_filblks_t		taforkblks = 0;
1487e7f5d5caSDarrick J. Wong 	xfs_extnum_t		junk;
1488c8ce540dSDarrick J. Wong 	uint64_t		tmp;
148939aff5fdSDarrick J. Wong 	int			error;
149039aff5fdSDarrick J. Wong 
149139aff5fdSDarrick J. Wong 	/*
149239aff5fdSDarrick J. Wong 	 * Count the number of extended attribute blocks
149339aff5fdSDarrick J. Wong 	 */
1494daf83964SChristoph Hellwig 	if (XFS_IFORK_Q(ip) && ip->i_afp->if_nextents > 0 &&
1495f7e67b20SChristoph Hellwig 	    ip->i_afp->if_format != XFS_DINODE_FMT_LOCAL) {
1496e7f5d5caSDarrick J. Wong 		error = xfs_bmap_count_blocks(tp, ip, XFS_ATTR_FORK, &junk,
149739aff5fdSDarrick J. Wong 				&aforkblks);
149839aff5fdSDarrick J. Wong 		if (error)
149939aff5fdSDarrick J. Wong 			return error;
150039aff5fdSDarrick J. Wong 	}
1501daf83964SChristoph Hellwig 	if (XFS_IFORK_Q(tip) && tip->i_afp->if_nextents > 0 &&
1502f7e67b20SChristoph Hellwig 	    tip->i_afp->if_format != XFS_DINODE_FMT_LOCAL) {
1503e7f5d5caSDarrick J. Wong 		error = xfs_bmap_count_blocks(tp, tip, XFS_ATTR_FORK, &junk,
150439aff5fdSDarrick J. Wong 				&taforkblks);
150539aff5fdSDarrick J. Wong 		if (error)
150639aff5fdSDarrick J. Wong 			return error;
150739aff5fdSDarrick J. Wong 	}
150839aff5fdSDarrick J. Wong 
150939aff5fdSDarrick J. Wong 	/*
15106fb10d6dSBrian Foster 	 * Btree format (v3) inodes have the inode number stamped in the bmbt
15116fb10d6dSBrian Foster 	 * block headers. We can't start changing the bmbt blocks until the
15126fb10d6dSBrian Foster 	 * inode owner change is logged so recovery does the right thing in the
15136fb10d6dSBrian Foster 	 * event of a crash. Set the owner change log flags now and leave the
15146fb10d6dSBrian Foster 	 * bmbt scan as the last step.
151539aff5fdSDarrick J. Wong 	 */
151638c26bfdSDave Chinner 	if (xfs_has_v3inodes(ip->i_mount)) {
1517f7e67b20SChristoph Hellwig 		if (ip->i_df.if_format == XFS_DINODE_FMT_BTREE)
151839aff5fdSDarrick J. Wong 			(*target_log_flags) |= XFS_ILOG_DOWNER;
1519f7e67b20SChristoph Hellwig 		if (tip->i_df.if_format == XFS_DINODE_FMT_BTREE)
152039aff5fdSDarrick J. Wong 			(*src_log_flags) |= XFS_ILOG_DOWNER;
15216471e9c5SChristoph Hellwig 	}
152239aff5fdSDarrick J. Wong 
152339aff5fdSDarrick J. Wong 	/*
152439aff5fdSDarrick J. Wong 	 * Swap the data forks of the inodes
152539aff5fdSDarrick J. Wong 	 */
1526897992b7SGustavo A. R. Silva 	swap(ip->i_df, tip->i_df);
152739aff5fdSDarrick J. Wong 
152839aff5fdSDarrick J. Wong 	/*
152939aff5fdSDarrick J. Wong 	 * Fix the on-disk inode values
153039aff5fdSDarrick J. Wong 	 */
15316e73a545SChristoph Hellwig 	tmp = (uint64_t)ip->i_nblocks;
15326e73a545SChristoph Hellwig 	ip->i_nblocks = tip->i_nblocks - taforkblks + aforkblks;
15336e73a545SChristoph Hellwig 	tip->i_nblocks = tmp + taforkblks - aforkblks;
153439aff5fdSDarrick J. Wong 
153539aff5fdSDarrick J. Wong 	/*
153639aff5fdSDarrick J. Wong 	 * The extents in the source inode could still contain speculative
153739aff5fdSDarrick J. Wong 	 * preallocation beyond EOF (e.g. the file is open but not modified
153839aff5fdSDarrick J. Wong 	 * while defrag is in progress). In that case, we need to copy over the
153939aff5fdSDarrick J. Wong 	 * number of delalloc blocks the data fork in the source inode is
154039aff5fdSDarrick J. Wong 	 * tracking beyond EOF so that when the fork is truncated away when the
154139aff5fdSDarrick J. Wong 	 * temporary inode is unlinked we don't underrun the i_delayed_blks
154239aff5fdSDarrick J. Wong 	 * counter on that inode.
154339aff5fdSDarrick J. Wong 	 */
154439aff5fdSDarrick J. Wong 	ASSERT(tip->i_delayed_blks == 0);
154539aff5fdSDarrick J. Wong 	tip->i_delayed_blks = ip->i_delayed_blks;
154639aff5fdSDarrick J. Wong 	ip->i_delayed_blks = 0;
154739aff5fdSDarrick J. Wong 
1548f7e67b20SChristoph Hellwig 	switch (ip->i_df.if_format) {
154939aff5fdSDarrick J. Wong 	case XFS_DINODE_FMT_EXTENTS:
155039aff5fdSDarrick J. Wong 		(*src_log_flags) |= XFS_ILOG_DEXT;
155139aff5fdSDarrick J. Wong 		break;
155239aff5fdSDarrick J. Wong 	case XFS_DINODE_FMT_BTREE:
155338c26bfdSDave Chinner 		ASSERT(!xfs_has_v3inodes(ip->i_mount) ||
155439aff5fdSDarrick J. Wong 		       (*src_log_flags & XFS_ILOG_DOWNER));
155539aff5fdSDarrick J. Wong 		(*src_log_flags) |= XFS_ILOG_DBROOT;
155639aff5fdSDarrick J. Wong 		break;
155739aff5fdSDarrick J. Wong 	}
155839aff5fdSDarrick J. Wong 
1559f7e67b20SChristoph Hellwig 	switch (tip->i_df.if_format) {
156039aff5fdSDarrick J. Wong 	case XFS_DINODE_FMT_EXTENTS:
156139aff5fdSDarrick J. Wong 		(*target_log_flags) |= XFS_ILOG_DEXT;
156239aff5fdSDarrick J. Wong 		break;
156339aff5fdSDarrick J. Wong 	case XFS_DINODE_FMT_BTREE:
156439aff5fdSDarrick J. Wong 		(*target_log_flags) |= XFS_ILOG_DBROOT;
156538c26bfdSDave Chinner 		ASSERT(!xfs_has_v3inodes(ip->i_mount) ||
156639aff5fdSDarrick J. Wong 		       (*target_log_flags & XFS_ILOG_DOWNER));
156739aff5fdSDarrick J. Wong 		break;
156839aff5fdSDarrick J. Wong 	}
156939aff5fdSDarrick J. Wong 
157039aff5fdSDarrick J. Wong 	return 0;
157139aff5fdSDarrick J. Wong }
157239aff5fdSDarrick J. Wong 
15732dd3d709SBrian Foster /*
15742dd3d709SBrian Foster  * Fix up the owners of the bmbt blocks to refer to the current inode. The
15752dd3d709SBrian Foster  * change owner scan attempts to order all modified buffers in the current
15762dd3d709SBrian Foster  * transaction. In the event of ordered buffer failure, the offending buffer is
15772dd3d709SBrian Foster  * physically logged as a fallback and the scan returns -EAGAIN. We must roll
15782dd3d709SBrian Foster  * the transaction in this case to replenish the fallback log reservation and
15792dd3d709SBrian Foster  * restart the scan. This process repeats until the scan completes.
15802dd3d709SBrian Foster  */
15812dd3d709SBrian Foster static int
15822dd3d709SBrian Foster xfs_swap_change_owner(
15832dd3d709SBrian Foster 	struct xfs_trans	**tpp,
15842dd3d709SBrian Foster 	struct xfs_inode	*ip,
15852dd3d709SBrian Foster 	struct xfs_inode	*tmpip)
15862dd3d709SBrian Foster {
15872dd3d709SBrian Foster 	int			error;
15882dd3d709SBrian Foster 	struct xfs_trans	*tp = *tpp;
15892dd3d709SBrian Foster 
15902dd3d709SBrian Foster 	do {
15912dd3d709SBrian Foster 		error = xfs_bmbt_change_owner(tp, ip, XFS_DATA_FORK, ip->i_ino,
15922dd3d709SBrian Foster 					      NULL);
15932dd3d709SBrian Foster 		/* success or fatal error */
15942dd3d709SBrian Foster 		if (error != -EAGAIN)
15952dd3d709SBrian Foster 			break;
15962dd3d709SBrian Foster 
15972dd3d709SBrian Foster 		error = xfs_trans_roll(tpp);
15982dd3d709SBrian Foster 		if (error)
15992dd3d709SBrian Foster 			break;
16002dd3d709SBrian Foster 		tp = *tpp;
16012dd3d709SBrian Foster 
16022dd3d709SBrian Foster 		/*
16032dd3d709SBrian Foster 		 * Redirty both inodes so they can relog and keep the log tail
16042dd3d709SBrian Foster 		 * moving forward.
16052dd3d709SBrian Foster 		 */
16062dd3d709SBrian Foster 		xfs_trans_ijoin(tp, ip, 0);
16072dd3d709SBrian Foster 		xfs_trans_ijoin(tp, tmpip, 0);
16082dd3d709SBrian Foster 		xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
16092dd3d709SBrian Foster 		xfs_trans_log_inode(tp, tmpip, XFS_ILOG_CORE);
16102dd3d709SBrian Foster 	} while (true);
16112dd3d709SBrian Foster 
16122dd3d709SBrian Foster 	return error;
16132dd3d709SBrian Foster }
16142dd3d709SBrian Foster 
16154ef897a2SDave Chinner int
1616a133d952SDave Chinner xfs_swap_extents(
1617e06259aaSDarrick J. Wong 	struct xfs_inode	*ip,	/* target inode */
1618e06259aaSDarrick J. Wong 	struct xfs_inode	*tip,	/* tmp inode */
1619e06259aaSDarrick J. Wong 	struct xfs_swapext	*sxp)
1620a133d952SDave Chinner {
1621e06259aaSDarrick J. Wong 	struct xfs_mount	*mp = ip->i_mount;
1622e06259aaSDarrick J. Wong 	struct xfs_trans	*tp;
1623e06259aaSDarrick J. Wong 	struct xfs_bstat	*sbp = &sxp->sx_stat;
1624a133d952SDave Chinner 	int			src_log_flags, target_log_flags;
1625a133d952SDave Chinner 	int			error = 0;
1626c8ce540dSDarrick J. Wong 	uint64_t		f;
16272dd3d709SBrian Foster 	int			resblks = 0;
1628f74681baSBrian Foster 	unsigned int		flags = 0;
1629a133d952SDave Chinner 
1630a133d952SDave Chinner 	/*
1631723cac48SDave Chinner 	 * Lock the inodes against other IO, page faults and truncate to
1632723cac48SDave Chinner 	 * begin with.  Then we can ensure the inodes are flushed and have no
1633723cac48SDave Chinner 	 * page cache safely. Once we have done this we can take the ilocks and
1634723cac48SDave Chinner 	 * do the rest of the checks.
1635a133d952SDave Chinner 	 */
163665523218SChristoph Hellwig 	lock_two_nondirectories(VFS_I(ip), VFS_I(tip));
1637d2c292d8SJan Kara 	filemap_invalidate_lock_two(VFS_I(ip)->i_mapping,
1638d2c292d8SJan Kara 				    VFS_I(tip)->i_mapping);
1639a133d952SDave Chinner 
1640a133d952SDave Chinner 	/* Verify that both files have the same format */
1641c19b3b05SDave Chinner 	if ((VFS_I(ip)->i_mode & S_IFMT) != (VFS_I(tip)->i_mode & S_IFMT)) {
16422451337dSDave Chinner 		error = -EINVAL;
1643a133d952SDave Chinner 		goto out_unlock;
1644a133d952SDave Chinner 	}
1645a133d952SDave Chinner 
1646a133d952SDave Chinner 	/* Verify both files are either real-time or non-realtime */
1647a133d952SDave Chinner 	if (XFS_IS_REALTIME_INODE(ip) != XFS_IS_REALTIME_INODE(tip)) {
16482451337dSDave Chinner 		error = -EINVAL;
1649a133d952SDave Chinner 		goto out_unlock;
1650a133d952SDave Chinner 	}
1651a133d952SDave Chinner 
16522713fefaSDarrick J. Wong 	error = xfs_qm_dqattach(ip);
16532713fefaSDarrick J. Wong 	if (error)
16542713fefaSDarrick J. Wong 		goto out_unlock;
16552713fefaSDarrick J. Wong 
16562713fefaSDarrick J. Wong 	error = xfs_qm_dqattach(tip);
16572713fefaSDarrick J. Wong 	if (error)
16582713fefaSDarrick J. Wong 		goto out_unlock;
16592713fefaSDarrick J. Wong 
16604ef897a2SDave Chinner 	error = xfs_swap_extent_flush(ip);
1661a133d952SDave Chinner 	if (error)
1662a133d952SDave Chinner 		goto out_unlock;
16634ef897a2SDave Chinner 	error = xfs_swap_extent_flush(tip);
16644ef897a2SDave Chinner 	if (error)
16654ef897a2SDave Chinner 		goto out_unlock;
1666a133d952SDave Chinner 
166796987eeaSChristoph Hellwig 	if (xfs_inode_has_cow_data(tip)) {
166896987eeaSChristoph Hellwig 		error = xfs_reflink_cancel_cow_range(tip, 0, NULLFILEOFF, true);
166996987eeaSChristoph Hellwig 		if (error)
16708bc3b5e4SDarrick J. Wong 			goto out_unlock;
167196987eeaSChristoph Hellwig 	}
167296987eeaSChristoph Hellwig 
16731f08af52SDarrick J. Wong 	/*
16741f08af52SDarrick J. Wong 	 * Extent "swapping" with rmap requires a permanent reservation and
16751f08af52SDarrick J. Wong 	 * a block reservation because it's really just a remap operation
16761f08af52SDarrick J. Wong 	 * performed with log redo items!
16771f08af52SDarrick J. Wong 	 */
167838c26bfdSDave Chinner 	if (xfs_has_rmapbt(mp)) {
1679b3fed434SBrian Foster 		int		w = XFS_DATA_FORK;
1680daf83964SChristoph Hellwig 		uint32_t	ipnext = ip->i_df.if_nextents;
1681daf83964SChristoph Hellwig 		uint32_t	tipnext	= tip->i_df.if_nextents;
1682b3fed434SBrian Foster 
16831f08af52SDarrick J. Wong 		/*
1684b3fed434SBrian Foster 		 * Conceptually this shouldn't affect the shape of either bmbt,
1685b3fed434SBrian Foster 		 * but since we atomically move extents one by one, we reserve
1686b3fed434SBrian Foster 		 * enough space to rebuild both trees.
16871f08af52SDarrick J. Wong 		 */
1688b3fed434SBrian Foster 		resblks = XFS_SWAP_RMAP_SPACE_RES(mp, ipnext, w);
1689b3fed434SBrian Foster 		resblks +=  XFS_SWAP_RMAP_SPACE_RES(mp, tipnext, w);
1690b3fed434SBrian Foster 
1691b3fed434SBrian Foster 		/*
1692f74681baSBrian Foster 		 * If either inode straddles a bmapbt block allocation boundary,
1693f74681baSBrian Foster 		 * the rmapbt algorithm triggers repeated allocs and frees as
1694f74681baSBrian Foster 		 * extents are remapped. This can exhaust the block reservation
1695f74681baSBrian Foster 		 * prematurely and cause shutdown. Return freed blocks to the
1696f74681baSBrian Foster 		 * transaction reservation to counter this behavior.
1697b3fed434SBrian Foster 		 */
1698f74681baSBrian Foster 		flags |= XFS_TRANS_RES_FDBLKS;
16992dd3d709SBrian Foster 	}
1700f74681baSBrian Foster 	error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0, flags,
1701f74681baSBrian Foster 				&tp);
1702253f4911SChristoph Hellwig 	if (error)
1703a133d952SDave Chinner 		goto out_unlock;
1704723cac48SDave Chinner 
1705723cac48SDave Chinner 	/*
1706723cac48SDave Chinner 	 * Lock and join the inodes to the tansaction so that transaction commit
1707723cac48SDave Chinner 	 * or cancel will unlock the inodes from this point onwards.
1708723cac48SDave Chinner 	 */
17097c2d238aSDarrick J. Wong 	xfs_lock_two_inodes(ip, XFS_ILOCK_EXCL, tip, XFS_ILOCK_EXCL);
171039aff5fdSDarrick J. Wong 	xfs_trans_ijoin(tp, ip, 0);
171139aff5fdSDarrick J. Wong 	xfs_trans_ijoin(tp, tip, 0);
1712723cac48SDave Chinner 
1713a133d952SDave Chinner 
1714a133d952SDave Chinner 	/* Verify all data are being swapped */
1715a133d952SDave Chinner 	if (sxp->sx_offset != 0 ||
171613d2c10bSChristoph Hellwig 	    sxp->sx_length != ip->i_disk_size ||
171713d2c10bSChristoph Hellwig 	    sxp->sx_length != tip->i_disk_size) {
17182451337dSDave Chinner 		error = -EFAULT;
17194ef897a2SDave Chinner 		goto out_trans_cancel;
1720a133d952SDave Chinner 	}
1721a133d952SDave Chinner 
1722a133d952SDave Chinner 	trace_xfs_swap_extent_before(ip, 0);
1723a133d952SDave Chinner 	trace_xfs_swap_extent_before(tip, 1);
1724a133d952SDave Chinner 
1725a133d952SDave Chinner 	/* check inode formats now that data is flushed */
1726a133d952SDave Chinner 	error = xfs_swap_extents_check_format(ip, tip);
1727a133d952SDave Chinner 	if (error) {
1728a133d952SDave Chinner 		xfs_notice(mp,
1729a133d952SDave Chinner 		    "%s: inode 0x%llx format is incompatible for exchanging.",
1730a133d952SDave Chinner 				__func__, ip->i_ino);
17314ef897a2SDave Chinner 		goto out_trans_cancel;
1732a133d952SDave Chinner 	}
1733a133d952SDave Chinner 
1734a133d952SDave Chinner 	/*
1735a133d952SDave Chinner 	 * Compare the current change & modify times with that
1736a133d952SDave Chinner 	 * passed in.  If they differ, we abort this swap.
1737a133d952SDave Chinner 	 * This is the mechanism used to ensure the calling
1738a133d952SDave Chinner 	 * process that the file was not changed out from
1739a133d952SDave Chinner 	 * under it.
1740a133d952SDave Chinner 	 */
1741a133d952SDave Chinner 	if ((sbp->bs_ctime.tv_sec != VFS_I(ip)->i_ctime.tv_sec) ||
1742a133d952SDave Chinner 	    (sbp->bs_ctime.tv_nsec != VFS_I(ip)->i_ctime.tv_nsec) ||
1743a133d952SDave Chinner 	    (sbp->bs_mtime.tv_sec != VFS_I(ip)->i_mtime.tv_sec) ||
1744a133d952SDave Chinner 	    (sbp->bs_mtime.tv_nsec != VFS_I(ip)->i_mtime.tv_nsec)) {
17452451337dSDave Chinner 		error = -EBUSY;
174681217683SDave Chinner 		goto out_trans_cancel;
1747a133d952SDave Chinner 	}
1748a133d952SDave Chinner 
174921b5c978SDave Chinner 	/*
175021b5c978SDave Chinner 	 * Note the trickiness in setting the log flags - we set the owner log
175121b5c978SDave Chinner 	 * flag on the opposite inode (i.e. the inode we are setting the new
175221b5c978SDave Chinner 	 * owner to be) because once we swap the forks and log that, log
175321b5c978SDave Chinner 	 * recovery is going to see the fork as owned by the swapped inode,
175421b5c978SDave Chinner 	 * not the pre-swapped inodes.
175521b5c978SDave Chinner 	 */
175621b5c978SDave Chinner 	src_log_flags = XFS_ILOG_CORE;
175721b5c978SDave Chinner 	target_log_flags = XFS_ILOG_CORE;
175839aff5fdSDarrick J. Wong 
175938c26bfdSDave Chinner 	if (xfs_has_rmapbt(mp))
17601f08af52SDarrick J. Wong 		error = xfs_swap_extent_rmap(&tp, ip, tip);
17611f08af52SDarrick J. Wong 	else
176239aff5fdSDarrick J. Wong 		error = xfs_swap_extent_forks(tp, ip, tip, &src_log_flags,
176339aff5fdSDarrick J. Wong 				&target_log_flags);
176421b5c978SDave Chinner 	if (error)
176521b5c978SDave Chinner 		goto out_trans_cancel;
1766a133d952SDave Chinner 
1767f0bc4d13SDarrick J. Wong 	/* Do we have to swap reflink flags? */
17683e09ab8fSChristoph Hellwig 	if ((ip->i_diflags2 & XFS_DIFLAG2_REFLINK) ^
17693e09ab8fSChristoph Hellwig 	    (tip->i_diflags2 & XFS_DIFLAG2_REFLINK)) {
17703e09ab8fSChristoph Hellwig 		f = ip->i_diflags2 & XFS_DIFLAG2_REFLINK;
17713e09ab8fSChristoph Hellwig 		ip->i_diflags2 &= ~XFS_DIFLAG2_REFLINK;
17723e09ab8fSChristoph Hellwig 		ip->i_diflags2 |= tip->i_diflags2 & XFS_DIFLAG2_REFLINK;
17733e09ab8fSChristoph Hellwig 		tip->i_diflags2 &= ~XFS_DIFLAG2_REFLINK;
17743e09ab8fSChristoph Hellwig 		tip->i_diflags2 |= f & XFS_DIFLAG2_REFLINK;
177552bfcdd7SDarrick J. Wong 	}
177652bfcdd7SDarrick J. Wong 
177752bfcdd7SDarrick J. Wong 	/* Swap the cow forks. */
177838c26bfdSDave Chinner 	if (xfs_has_reflink(mp)) {
1779f7e67b20SChristoph Hellwig 		ASSERT(!ip->i_cowfp ||
1780f7e67b20SChristoph Hellwig 		       ip->i_cowfp->if_format == XFS_DINODE_FMT_EXTENTS);
1781f7e67b20SChristoph Hellwig 		ASSERT(!tip->i_cowfp ||
1782f7e67b20SChristoph Hellwig 		       tip->i_cowfp->if_format == XFS_DINODE_FMT_EXTENTS);
178352bfcdd7SDarrick J. Wong 
1784897992b7SGustavo A. R. Silva 		swap(ip->i_cowfp, tip->i_cowfp);
178552bfcdd7SDarrick J. Wong 
17865bcffe30SChristoph Hellwig 		if (ip->i_cowfp && ip->i_cowfp->if_bytes)
178783104d44SDarrick J. Wong 			xfs_inode_set_cowblocks_tag(ip);
178852bfcdd7SDarrick J. Wong 		else
178952bfcdd7SDarrick J. Wong 			xfs_inode_clear_cowblocks_tag(ip);
17905bcffe30SChristoph Hellwig 		if (tip->i_cowfp && tip->i_cowfp->if_bytes)
179183104d44SDarrick J. Wong 			xfs_inode_set_cowblocks_tag(tip);
179252bfcdd7SDarrick J. Wong 		else
179352bfcdd7SDarrick J. Wong 			xfs_inode_clear_cowblocks_tag(tip);
1794f0bc4d13SDarrick J. Wong 	}
1795f0bc4d13SDarrick J. Wong 
1796a133d952SDave Chinner 	xfs_trans_log_inode(tp, ip,  src_log_flags);
1797a133d952SDave Chinner 	xfs_trans_log_inode(tp, tip, target_log_flags);
1798a133d952SDave Chinner 
1799a133d952SDave Chinner 	/*
18006fb10d6dSBrian Foster 	 * The extent forks have been swapped, but crc=1,rmapbt=0 filesystems
18016fb10d6dSBrian Foster 	 * have inode number owner values in the bmbt blocks that still refer to
18026fb10d6dSBrian Foster 	 * the old inode. Scan each bmbt to fix up the owner values with the
18036fb10d6dSBrian Foster 	 * inode number of the current inode.
18046fb10d6dSBrian Foster 	 */
18056fb10d6dSBrian Foster 	if (src_log_flags & XFS_ILOG_DOWNER) {
18062dd3d709SBrian Foster 		error = xfs_swap_change_owner(&tp, ip, tip);
18076fb10d6dSBrian Foster 		if (error)
18086fb10d6dSBrian Foster 			goto out_trans_cancel;
18096fb10d6dSBrian Foster 	}
18106fb10d6dSBrian Foster 	if (target_log_flags & XFS_ILOG_DOWNER) {
18112dd3d709SBrian Foster 		error = xfs_swap_change_owner(&tp, tip, ip);
18126fb10d6dSBrian Foster 		if (error)
18136fb10d6dSBrian Foster 			goto out_trans_cancel;
18146fb10d6dSBrian Foster 	}
18156fb10d6dSBrian Foster 
18166fb10d6dSBrian Foster 	/*
1817a133d952SDave Chinner 	 * If this is a synchronous mount, make sure that the
1818a133d952SDave Chinner 	 * transaction goes to disk before returning to the user.
1819a133d952SDave Chinner 	 */
18200560f31aSDave Chinner 	if (xfs_has_wsync(mp))
1821a133d952SDave Chinner 		xfs_trans_set_sync(tp);
1822a133d952SDave Chinner 
182370393313SChristoph Hellwig 	error = xfs_trans_commit(tp);
1824a133d952SDave Chinner 
1825a133d952SDave Chinner 	trace_xfs_swap_extent_after(ip, 0);
1826a133d952SDave Chinner 	trace_xfs_swap_extent_after(tip, 1);
182739aff5fdSDarrick J. Wong 
1828d2c292d8SJan Kara out_unlock_ilock:
1829d2c292d8SJan Kara 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
1830d2c292d8SJan Kara 	xfs_iunlock(tip, XFS_ILOCK_EXCL);
183165523218SChristoph Hellwig out_unlock:
1832d2c292d8SJan Kara 	filemap_invalidate_unlock_two(VFS_I(ip)->i_mapping,
1833d2c292d8SJan Kara 				      VFS_I(tip)->i_mapping);
183465523218SChristoph Hellwig 	unlock_two_nondirectories(VFS_I(ip), VFS_I(tip));
1835a133d952SDave Chinner 	return error;
1836a133d952SDave Chinner 
183739aff5fdSDarrick J. Wong out_trans_cancel:
183839aff5fdSDarrick J. Wong 	xfs_trans_cancel(tp);
1839d2c292d8SJan Kara 	goto out_unlock_ilock;
1840a133d952SDave Chinner }
1841