xref: /linux/fs/xfs/xfs_bmap_util.c (revision 1bba82fe1afac69c85c1f5ea137c8e73de3c8032)
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 	/*
12295f0b95eSChandan 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
12595f0b95eSChandan 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 	 */
12895f0b95eSChandan Babu R 	if (ralen * mp->m_sb.sb_rextsize >= XFS_MAX_BMBT_EXTLEN)
12995f0b95eSChandan 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;
259732436efSDarrick 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 		/*
3178ee81ed5SYe Bin 		 * Take the flush completion as being a point-in-time snapshot
3188ee81ed5SYe Bin 		 * where there are no delalloc extents, and if any new ones
3198ee81ed5SYe Bin 		 * have been created racily, just skip them as being 'after'
3208ee81ed5SYe Bin 		 * the flush and so don't get reported.
321f86f4037SDarrick J. Wong 		 */
3228ee81ed5SYe Bin 		if (!(bmv->bmv_iflags & BMV_IF_DELALLOC))
3238ee81ed5SYe Bin 			return 0;
324abbf9e8aSChristoph Hellwig 
325abbf9e8aSChristoph Hellwig 		p->bmv_oflags |= BMV_OF_DELALLOC;
326abbf9e8aSChristoph Hellwig 		p->bmv_block = -2;
327f86f4037SDarrick J. Wong 	} else {
328abbf9e8aSChristoph Hellwig 		p->bmv_block = xfs_fsb_to_db(ip, got->br_startblock);
329f86f4037SDarrick J. Wong 	}
330f86f4037SDarrick J. Wong 
331abbf9e8aSChristoph Hellwig 	if (got->br_state == XFS_EXT_UNWRITTEN &&
332abbf9e8aSChristoph Hellwig 	    (bmv->bmv_iflags & BMV_IF_PREALLOC))
333abbf9e8aSChristoph Hellwig 		p->bmv_oflags |= BMV_OF_PREALLOC;
334abbf9e8aSChristoph Hellwig 
335abbf9e8aSChristoph Hellwig 	if (shared)
336abbf9e8aSChristoph Hellwig 		p->bmv_oflags |= BMV_OF_SHARED;
337abbf9e8aSChristoph Hellwig 
338abbf9e8aSChristoph Hellwig 	p->bmv_offset = XFS_FSB_TO_BB(ip->i_mount, got->br_startoff);
339abbf9e8aSChristoph Hellwig 	p->bmv_length = XFS_FSB_TO_BB(ip->i_mount, got->br_blockcount);
340abbf9e8aSChristoph Hellwig 
341abbf9e8aSChristoph Hellwig 	bmv->bmv_offset = p->bmv_offset + p->bmv_length;
342abbf9e8aSChristoph Hellwig 	bmv->bmv_length = max(0LL, bmv_end - bmv->bmv_offset);
343abbf9e8aSChristoph Hellwig 	bmv->bmv_entries++;
344f86f4037SDarrick J. Wong 	return 0;
345f86f4037SDarrick J. Wong }
346f86f4037SDarrick J. Wong 
347abbf9e8aSChristoph Hellwig static void
348abbf9e8aSChristoph Hellwig xfs_getbmap_report_hole(
349abbf9e8aSChristoph Hellwig 	struct xfs_inode	*ip,
350abbf9e8aSChristoph Hellwig 	struct getbmapx		*bmv,
351232b5194SChristoph Hellwig 	struct kgetbmap		*out,
352abbf9e8aSChristoph Hellwig 	int64_t			bmv_end,
353abbf9e8aSChristoph Hellwig 	xfs_fileoff_t		bno,
354abbf9e8aSChristoph Hellwig 	xfs_fileoff_t		end)
355abbf9e8aSChristoph Hellwig {
356232b5194SChristoph Hellwig 	struct kgetbmap		*p = out + bmv->bmv_entries;
357abbf9e8aSChristoph Hellwig 
358abbf9e8aSChristoph Hellwig 	if (bmv->bmv_iflags & BMV_IF_NO_HOLES)
359abbf9e8aSChristoph Hellwig 		return;
360abbf9e8aSChristoph Hellwig 
361abbf9e8aSChristoph Hellwig 	p->bmv_block = -1;
362abbf9e8aSChristoph Hellwig 	p->bmv_offset = XFS_FSB_TO_BB(ip->i_mount, bno);
363abbf9e8aSChristoph Hellwig 	p->bmv_length = XFS_FSB_TO_BB(ip->i_mount, end - bno);
364abbf9e8aSChristoph Hellwig 
365abbf9e8aSChristoph Hellwig 	bmv->bmv_offset = p->bmv_offset + p->bmv_length;
366abbf9e8aSChristoph Hellwig 	bmv->bmv_length = max(0LL, bmv_end - bmv->bmv_offset);
367abbf9e8aSChristoph Hellwig 	bmv->bmv_entries++;
368abbf9e8aSChristoph Hellwig }
369abbf9e8aSChristoph Hellwig 
370abbf9e8aSChristoph Hellwig static inline bool
371abbf9e8aSChristoph Hellwig xfs_getbmap_full(
372abbf9e8aSChristoph Hellwig 	struct getbmapx		*bmv)
373abbf9e8aSChristoph Hellwig {
374abbf9e8aSChristoph Hellwig 	return bmv->bmv_length == 0 || bmv->bmv_entries >= bmv->bmv_count - 1;
375abbf9e8aSChristoph Hellwig }
376abbf9e8aSChristoph Hellwig 
377abbf9e8aSChristoph Hellwig static bool
378abbf9e8aSChristoph Hellwig xfs_getbmap_next_rec(
379abbf9e8aSChristoph Hellwig 	struct xfs_bmbt_irec	*rec,
380abbf9e8aSChristoph Hellwig 	xfs_fileoff_t		total_end)
381abbf9e8aSChristoph Hellwig {
382abbf9e8aSChristoph Hellwig 	xfs_fileoff_t		end = rec->br_startoff + rec->br_blockcount;
383abbf9e8aSChristoph Hellwig 
384abbf9e8aSChristoph Hellwig 	if (end == total_end)
385abbf9e8aSChristoph Hellwig 		return false;
386abbf9e8aSChristoph Hellwig 
387abbf9e8aSChristoph Hellwig 	rec->br_startoff += rec->br_blockcount;
388abbf9e8aSChristoph Hellwig 	if (!isnullstartblock(rec->br_startblock) &&
389abbf9e8aSChristoph Hellwig 	    rec->br_startblock != DELAYSTARTBLOCK)
390abbf9e8aSChristoph Hellwig 		rec->br_startblock += rec->br_blockcount;
391abbf9e8aSChristoph Hellwig 	rec->br_blockcount = total_end - end;
392abbf9e8aSChristoph Hellwig 	return true;
393abbf9e8aSChristoph Hellwig }
394abbf9e8aSChristoph Hellwig 
39568988114SDave Chinner /*
39668988114SDave Chinner  * Get inode's extents as described in bmv, and format for output.
39768988114SDave Chinner  * Calls formatter to fill the user's buffer until all extents
39868988114SDave Chinner  * are mapped, until the passed-in bmv->bmv_count slots have
39968988114SDave Chinner  * been filled, or until the formatter short-circuits the loop,
40068988114SDave Chinner  * if it is tracking filled-in extents on its own.
40168988114SDave Chinner  */
40268988114SDave Chinner int						/* error code */
40368988114SDave Chinner xfs_getbmap(
404232b5194SChristoph Hellwig 	struct xfs_inode	*ip,
40568988114SDave Chinner 	struct getbmapx		*bmv,		/* user bmap structure */
406232b5194SChristoph Hellwig 	struct kgetbmap		*out)
40768988114SDave Chinner {
408abbf9e8aSChristoph Hellwig 	struct xfs_mount	*mp = ip->i_mount;
409abbf9e8aSChristoph Hellwig 	int			iflags = bmv->bmv_iflags;
410232b5194SChristoph Hellwig 	int			whichfork, lock, error = 0;
411abbf9e8aSChristoph Hellwig 	int64_t			bmv_end, max_len;
412abbf9e8aSChristoph Hellwig 	xfs_fileoff_t		bno, first_bno;
413abbf9e8aSChristoph Hellwig 	struct xfs_ifork	*ifp;
414abbf9e8aSChristoph Hellwig 	struct xfs_bmbt_irec	got, rec;
415abbf9e8aSChristoph Hellwig 	xfs_filblks_t		len;
416b2b1712aSChristoph Hellwig 	struct xfs_iext_cursor	icur;
41768988114SDave Chinner 
418232b5194SChristoph Hellwig 	if (bmv->bmv_iflags & ~BMV_IF_VALID)
419232b5194SChristoph Hellwig 		return -EINVAL;
420f86f4037SDarrick J. Wong #ifndef DEBUG
421f86f4037SDarrick J. Wong 	/* Only allow CoW fork queries if we're debugging. */
422f86f4037SDarrick J. Wong 	if (iflags & BMV_IF_COWFORK)
423f86f4037SDarrick J. Wong 		return -EINVAL;
424f86f4037SDarrick J. Wong #endif
425f86f4037SDarrick J. Wong 	if ((iflags & BMV_IF_ATTRFORK) && (iflags & BMV_IF_COWFORK))
426f86f4037SDarrick J. Wong 		return -EINVAL;
427f86f4037SDarrick J. Wong 
428abbf9e8aSChristoph Hellwig 	if (bmv->bmv_length < -1)
429abbf9e8aSChristoph Hellwig 		return -EINVAL;
430abbf9e8aSChristoph Hellwig 	bmv->bmv_entries = 0;
431abbf9e8aSChristoph Hellwig 	if (bmv->bmv_length == 0)
432abbf9e8aSChristoph Hellwig 		return 0;
433abbf9e8aSChristoph Hellwig 
434f86f4037SDarrick J. Wong 	if (iflags & BMV_IF_ATTRFORK)
435f86f4037SDarrick J. Wong 		whichfork = XFS_ATTR_FORK;
436f86f4037SDarrick J. Wong 	else if (iflags & BMV_IF_COWFORK)
437f86f4037SDarrick J. Wong 		whichfork = XFS_COW_FORK;
438f86f4037SDarrick J. Wong 	else
439f86f4037SDarrick J. Wong 		whichfork = XFS_DATA_FORK;
44068988114SDave Chinner 
44168988114SDave Chinner 	xfs_ilock(ip, XFS_IOLOCK_SHARED);
442f86f4037SDarrick J. Wong 	switch (whichfork) {
443abbf9e8aSChristoph Hellwig 	case XFS_ATTR_FORK:
444001c179cSChenXiaoSong 		lock = xfs_ilock_attr_map_shared(ip);
445932b42c6SDarrick J. Wong 		if (!xfs_inode_has_attr_fork(ip))
446001c179cSChenXiaoSong 			goto out_unlock_ilock;
447abbf9e8aSChristoph Hellwig 
448abbf9e8aSChristoph Hellwig 		max_len = 1LL << 32;
449abbf9e8aSChristoph Hellwig 		break;
450abbf9e8aSChristoph Hellwig 	case XFS_COW_FORK:
451001c179cSChenXiaoSong 		lock = XFS_ILOCK_SHARED;
452001c179cSChenXiaoSong 		xfs_ilock(ip, lock);
453001c179cSChenXiaoSong 
454abbf9e8aSChristoph Hellwig 		/* No CoW fork? Just return */
455001c179cSChenXiaoSong 		if (!xfs_ifork_ptr(ip, whichfork))
456001c179cSChenXiaoSong 			goto out_unlock_ilock;
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 		break;
463f86f4037SDarrick J. Wong 	case XFS_DATA_FORK:
464efa70be1SChristoph Hellwig 		if (!(iflags & BMV_IF_DELALLOC) &&
46513d2c10bSChristoph Hellwig 		    (ip->i_delayed_blks || XFS_ISIZE(ip) > ip->i_disk_size)) {
4662451337dSDave Chinner 			error = filemap_write_and_wait(VFS_I(ip)->i_mapping);
46768988114SDave Chinner 			if (error)
46868988114SDave Chinner 				goto out_unlock_iolock;
469efa70be1SChristoph Hellwig 
47068988114SDave Chinner 			/*
471efa70be1SChristoph Hellwig 			 * Even after flushing the inode, there can still be
472efa70be1SChristoph Hellwig 			 * delalloc blocks on the inode beyond EOF due to
473efa70be1SChristoph Hellwig 			 * speculative preallocation.  These are not removed
474efa70be1SChristoph Hellwig 			 * until the release function is called or the inode
475efa70be1SChristoph Hellwig 			 * is inactivated.  Hence we cannot assert here that
476efa70be1SChristoph Hellwig 			 * ip->i_delayed_blks == 0.
47768988114SDave Chinner 			 */
47868988114SDave Chinner 		}
47968988114SDave Chinner 
480abbf9e8aSChristoph Hellwig 		if (xfs_get_extsz_hint(ip) ||
481db07349dSChristoph Hellwig 		    (ip->i_diflags &
482abbf9e8aSChristoph Hellwig 		     (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)))
483abbf9e8aSChristoph Hellwig 			max_len = mp->m_super->s_maxbytes;
484abbf9e8aSChristoph Hellwig 		else
485abbf9e8aSChristoph Hellwig 			max_len = XFS_ISIZE(ip);
486abbf9e8aSChristoph Hellwig 
487309ecac8SChristoph Hellwig 		lock = xfs_ilock_data_map_shared(ip);
488f86f4037SDarrick J. Wong 		break;
489efa70be1SChristoph Hellwig 	}
49068988114SDave Chinner 
491001c179cSChenXiaoSong 	ifp = xfs_ifork_ptr(ip, whichfork);
492001c179cSChenXiaoSong 
493f7e67b20SChristoph Hellwig 	switch (ifp->if_format) {
494abbf9e8aSChristoph Hellwig 	case XFS_DINODE_FMT_EXTENTS:
495abbf9e8aSChristoph Hellwig 	case XFS_DINODE_FMT_BTREE:
496abbf9e8aSChristoph Hellwig 		break;
497abbf9e8aSChristoph Hellwig 	case XFS_DINODE_FMT_LOCAL:
498abbf9e8aSChristoph Hellwig 		/* Local format inode forks report no extents. */
49968988114SDave Chinner 		goto out_unlock_ilock;
500abbf9e8aSChristoph Hellwig 	default:
501abbf9e8aSChristoph Hellwig 		error = -EINVAL;
502abbf9e8aSChristoph Hellwig 		goto out_unlock_ilock;
50368988114SDave Chinner 	}
50468988114SDave Chinner 
505abbf9e8aSChristoph Hellwig 	if (bmv->bmv_length == -1) {
506abbf9e8aSChristoph Hellwig 		max_len = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, max_len));
507abbf9e8aSChristoph Hellwig 		bmv->bmv_length = max(0LL, max_len - bmv->bmv_offset);
508abbf9e8aSChristoph Hellwig 	}
509abbf9e8aSChristoph Hellwig 
510abbf9e8aSChristoph Hellwig 	bmv_end = bmv->bmv_offset + bmv->bmv_length;
511abbf9e8aSChristoph Hellwig 
512abbf9e8aSChristoph Hellwig 	first_bno = bno = XFS_BB_TO_FSBT(mp, bmv->bmv_offset);
513abbf9e8aSChristoph Hellwig 	len = XFS_BB_TO_FSB(mp, bmv->bmv_length);
514abbf9e8aSChristoph Hellwig 
515abbf9e8aSChristoph Hellwig 	error = xfs_iread_extents(NULL, ip, whichfork);
516abbf9e8aSChristoph Hellwig 	if (error)
517abbf9e8aSChristoph Hellwig 		goto out_unlock_ilock;
518abbf9e8aSChristoph Hellwig 
519b2b1712aSChristoph Hellwig 	if (!xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got)) {
520abbf9e8aSChristoph Hellwig 		/*
521abbf9e8aSChristoph Hellwig 		 * Report a whole-file hole if the delalloc flag is set to
522abbf9e8aSChristoph Hellwig 		 * stay compatible with the old implementation.
523abbf9e8aSChristoph Hellwig 		 */
524abbf9e8aSChristoph Hellwig 		if (iflags & BMV_IF_DELALLOC)
525abbf9e8aSChristoph Hellwig 			xfs_getbmap_report_hole(ip, bmv, out, bmv_end, bno,
526abbf9e8aSChristoph Hellwig 					XFS_B_TO_FSB(mp, XFS_ISIZE(ip)));
527abbf9e8aSChristoph Hellwig 		goto out_unlock_ilock;
528abbf9e8aSChristoph Hellwig 	}
529abbf9e8aSChristoph Hellwig 
530abbf9e8aSChristoph Hellwig 	while (!xfs_getbmap_full(bmv)) {
531abbf9e8aSChristoph Hellwig 		xfs_trim_extent(&got, first_bno, len);
532abbf9e8aSChristoph Hellwig 
533abbf9e8aSChristoph Hellwig 		/*
534abbf9e8aSChristoph Hellwig 		 * Report an entry for a hole if this extent doesn't directly
535abbf9e8aSChristoph Hellwig 		 * follow the previous one.
536abbf9e8aSChristoph Hellwig 		 */
537abbf9e8aSChristoph Hellwig 		if (got.br_startoff > bno) {
538abbf9e8aSChristoph Hellwig 			xfs_getbmap_report_hole(ip, bmv, out, bmv_end, bno,
539abbf9e8aSChristoph Hellwig 					got.br_startoff);
540abbf9e8aSChristoph Hellwig 			if (xfs_getbmap_full(bmv))
541abbf9e8aSChristoph Hellwig 				break;
542abbf9e8aSChristoph Hellwig 		}
543abbf9e8aSChristoph Hellwig 
544abbf9e8aSChristoph Hellwig 		/*
545abbf9e8aSChristoph Hellwig 		 * In order to report shared extents accurately, we report each
546abbf9e8aSChristoph Hellwig 		 * distinct shared / unshared part of a single bmbt record with
547abbf9e8aSChristoph Hellwig 		 * an individual getbmapx record.
548abbf9e8aSChristoph Hellwig 		 */
549abbf9e8aSChristoph Hellwig 		bno = got.br_startoff + got.br_blockcount;
550abbf9e8aSChristoph Hellwig 		rec = got;
55168988114SDave Chinner 		do {
552abbf9e8aSChristoph Hellwig 			error = xfs_getbmap_report_one(ip, bmv, out, bmv_end,
553abbf9e8aSChristoph Hellwig 					&rec);
554abbf9e8aSChristoph Hellwig 			if (error || xfs_getbmap_full(bmv))
555abbf9e8aSChristoph Hellwig 				goto out_unlock_ilock;
556abbf9e8aSChristoph Hellwig 		} while (xfs_getbmap_next_rec(&rec, bno));
55768988114SDave Chinner 
558b2b1712aSChristoph Hellwig 		if (!xfs_iext_next_extent(ifp, &icur, &got)) {
559abbf9e8aSChristoph Hellwig 			xfs_fileoff_t	end = XFS_B_TO_FSB(mp, XFS_ISIZE(ip));
56068988114SDave Chinner 
561*1bba82feSDarrick J. Wong 			if (bmv->bmv_entries > 0)
562*1bba82feSDarrick J. Wong 				out[bmv->bmv_entries - 1].bmv_oflags |=
563*1bba82feSDarrick J. Wong 								BMV_OF_LAST;
56468988114SDave Chinner 
565abbf9e8aSChristoph Hellwig 			if (whichfork != XFS_ATTR_FORK && bno < end &&
566abbf9e8aSChristoph Hellwig 			    !xfs_getbmap_full(bmv)) {
567abbf9e8aSChristoph Hellwig 				xfs_getbmap_report_hole(ip, bmv, out, bmv_end,
568abbf9e8aSChristoph Hellwig 						bno, end);
569abbf9e8aSChristoph Hellwig 			}
570abbf9e8aSChristoph Hellwig 			break;
57168988114SDave Chinner 		}
57268988114SDave Chinner 
573abbf9e8aSChristoph Hellwig 		if (bno >= first_bno + len)
574abbf9e8aSChristoph Hellwig 			break;
57568988114SDave Chinner 	}
57668988114SDave Chinner 
57768988114SDave Chinner out_unlock_ilock:
57801f4f327SChristoph Hellwig 	xfs_iunlock(ip, lock);
57968988114SDave Chinner out_unlock_iolock:
58068988114SDave Chinner 	xfs_iunlock(ip, XFS_IOLOCK_SHARED);
58168988114SDave Chinner 	return error;
58268988114SDave Chinner }
58368988114SDave Chinner 
58468988114SDave Chinner /*
585e2ac8363SChristoph Hellwig  * Dead simple method of punching delalyed allocation blocks from a range in
586e2ac8363SChristoph Hellwig  * the inode.  This will always punch out both the start and end blocks, even
587e2ac8363SChristoph Hellwig  * if the ranges only partially overlap them, so it is up to the caller to
588e2ac8363SChristoph Hellwig  * ensure that partial blocks are not passed in.
58968988114SDave Chinner  */
59068988114SDave Chinner int
59168988114SDave Chinner xfs_bmap_punch_delalloc_range(
59268988114SDave Chinner 	struct xfs_inode	*ip,
5937348b322SDave Chinner 	xfs_off_t		start_byte,
5947348b322SDave Chinner 	xfs_off_t		end_byte)
59568988114SDave Chinner {
5967348b322SDave Chinner 	struct xfs_mount	*mp = ip->i_mount;
597e2ac8363SChristoph Hellwig 	struct xfs_ifork	*ifp = &ip->i_df;
5987348b322SDave Chinner 	xfs_fileoff_t		start_fsb = XFS_B_TO_FSBT(mp, start_byte);
5997348b322SDave Chinner 	xfs_fileoff_t		end_fsb = XFS_B_TO_FSB(mp, end_byte);
600e2ac8363SChristoph Hellwig 	struct xfs_bmbt_irec	got, del;
601e2ac8363SChristoph Hellwig 	struct xfs_iext_cursor	icur;
60268988114SDave Chinner 	int			error = 0;
60368988114SDave Chinner 
604b2197a36SChristoph Hellwig 	ASSERT(!xfs_need_iread_extents(ifp));
60568988114SDave Chinner 
6060065b541SChristoph Hellwig 	xfs_ilock(ip, XFS_ILOCK_EXCL);
607e2ac8363SChristoph Hellwig 	if (!xfs_iext_lookup_extent_before(ip, ifp, &end_fsb, &icur, &got))
608d4380177SChristoph Hellwig 		goto out_unlock;
609e2ac8363SChristoph Hellwig 
610e2ac8363SChristoph Hellwig 	while (got.br_startoff + got.br_blockcount > start_fsb) {
611e2ac8363SChristoph Hellwig 		del = got;
6127348b322SDave Chinner 		xfs_trim_extent(&del, start_fsb, end_fsb - start_fsb);
613e2ac8363SChristoph Hellwig 
614e2ac8363SChristoph Hellwig 		/*
615e2ac8363SChristoph Hellwig 		 * A delete can push the cursor forward. Step back to the
616e2ac8363SChristoph Hellwig 		 * previous extent on non-delalloc or extents outside the
617e2ac8363SChristoph Hellwig 		 * target range.
618e2ac8363SChristoph Hellwig 		 */
619e2ac8363SChristoph Hellwig 		if (!del.br_blockcount ||
620e2ac8363SChristoph Hellwig 		    !isnullstartblock(del.br_startblock)) {
621e2ac8363SChristoph Hellwig 			if (!xfs_iext_prev_extent(ifp, &icur, &got))
622e2ac8363SChristoph Hellwig 				break;
623e2ac8363SChristoph Hellwig 			continue;
624e2ac8363SChristoph Hellwig 		}
625e2ac8363SChristoph Hellwig 
626e2ac8363SChristoph Hellwig 		error = xfs_bmap_del_extent_delay(ip, XFS_DATA_FORK, &icur,
627e2ac8363SChristoph Hellwig 						  &got, &del);
628e2ac8363SChristoph Hellwig 		if (error || !xfs_iext_get_extent(ifp, &icur, &got))
629e2ac8363SChristoph Hellwig 			break;
630e2ac8363SChristoph Hellwig 	}
63168988114SDave Chinner 
632d4380177SChristoph Hellwig out_unlock:
633d4380177SChristoph Hellwig 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
63468988114SDave Chinner 	return error;
63568988114SDave Chinner }
636c24b5dfaSDave Chinner 
637c24b5dfaSDave Chinner /*
638c24b5dfaSDave Chinner  * Test whether it is appropriate to check an inode for and free post EOF
639c24b5dfaSDave Chinner  * blocks. The 'force' parameter determines whether we should also consider
640c24b5dfaSDave Chinner  * regular files that are marked preallocated or append-only.
641c24b5dfaSDave Chinner  */
642c24b5dfaSDave Chinner bool
6437d88329eSDarrick J. Wong xfs_can_free_eofblocks(
6447d88329eSDarrick J. Wong 	struct xfs_inode	*ip,
6457d88329eSDarrick J. Wong 	bool			force)
646c24b5dfaSDave Chinner {
6477d88329eSDarrick J. Wong 	struct xfs_bmbt_irec	imap;
6487d88329eSDarrick J. Wong 	struct xfs_mount	*mp = ip->i_mount;
6497d88329eSDarrick J. Wong 	xfs_fileoff_t		end_fsb;
6507d88329eSDarrick J. Wong 	xfs_fileoff_t		last_fsb;
6517d88329eSDarrick J. Wong 	int			nimaps = 1;
6527d88329eSDarrick J. Wong 	int			error;
6537d88329eSDarrick J. Wong 
6547d88329eSDarrick J. Wong 	/*
6557d88329eSDarrick J. Wong 	 * Caller must either hold the exclusive io lock; or be inactivating
6567d88329eSDarrick J. Wong 	 * the inode, which guarantees there are no other users of the inode.
6577d88329eSDarrick J. Wong 	 */
6587d88329eSDarrick J. Wong 	ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL) ||
6597d88329eSDarrick J. Wong 	       (VFS_I(ip)->i_state & I_FREEING));
6607d88329eSDarrick J. Wong 
661c24b5dfaSDave Chinner 	/* prealloc/delalloc exists only on regular files */
662c19b3b05SDave Chinner 	if (!S_ISREG(VFS_I(ip)->i_mode))
663c24b5dfaSDave Chinner 		return false;
664c24b5dfaSDave Chinner 
665c24b5dfaSDave Chinner 	/*
666c24b5dfaSDave Chinner 	 * Zero sized files with no cached pages and delalloc blocks will not
667c24b5dfaSDave Chinner 	 * have speculative prealloc/delalloc blocks to remove.
668c24b5dfaSDave Chinner 	 */
669c24b5dfaSDave Chinner 	if (VFS_I(ip)->i_size == 0 &&
6702667c6f9SDave Chinner 	    VFS_I(ip)->i_mapping->nrpages == 0 &&
671c24b5dfaSDave Chinner 	    ip->i_delayed_blks == 0)
672c24b5dfaSDave Chinner 		return false;
673c24b5dfaSDave Chinner 
674c24b5dfaSDave Chinner 	/* If we haven't read in the extent list, then don't do it now. */
675b2197a36SChristoph Hellwig 	if (xfs_need_iread_extents(&ip->i_df))
676c24b5dfaSDave Chinner 		return false;
677c24b5dfaSDave Chinner 
678c24b5dfaSDave Chinner 	/*
679c24b5dfaSDave Chinner 	 * Do not free real preallocated or append-only files unless the file
680c24b5dfaSDave Chinner 	 * has delalloc blocks and we are forced to remove them.
681c24b5dfaSDave Chinner 	 */
682db07349dSChristoph Hellwig 	if (ip->i_diflags & (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND))
683c24b5dfaSDave Chinner 		if (!force || ip->i_delayed_blks == 0)
684c24b5dfaSDave Chinner 			return false;
685c24b5dfaSDave Chinner 
6867d88329eSDarrick J. Wong 	/*
6877d88329eSDarrick J. Wong 	 * Do not try to free post-EOF blocks if EOF is beyond the end of the
6887d88329eSDarrick J. Wong 	 * range supported by the page cache, because the truncation will loop
6897d88329eSDarrick J. Wong 	 * forever.
6907d88329eSDarrick J. Wong 	 */
6917d88329eSDarrick J. Wong 	end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_ISIZE(ip));
6928944c6fbSDarrick J. Wong 	if (XFS_IS_REALTIME_INODE(ip) && mp->m_sb.sb_rextsize > 1)
6938944c6fbSDarrick J. Wong 		end_fsb = roundup_64(end_fsb, mp->m_sb.sb_rextsize);
6947d88329eSDarrick J. Wong 	last_fsb = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
6957d88329eSDarrick J. Wong 	if (last_fsb <= end_fsb)
6967d88329eSDarrick J. Wong 		return false;
6977d88329eSDarrick J. Wong 
6987d88329eSDarrick J. Wong 	/*
6997d88329eSDarrick J. Wong 	 * Look up the mapping for the first block past EOF.  If we can't find
7007d88329eSDarrick J. Wong 	 * it, there's nothing to free.
7017d88329eSDarrick J. Wong 	 */
7027d88329eSDarrick J. Wong 	xfs_ilock(ip, XFS_ILOCK_SHARED);
7037d88329eSDarrick J. Wong 	error = xfs_bmapi_read(ip, end_fsb, last_fsb - end_fsb, &imap, &nimaps,
7047d88329eSDarrick J. Wong 			0);
7057d88329eSDarrick J. Wong 	xfs_iunlock(ip, XFS_ILOCK_SHARED);
7067d88329eSDarrick J. Wong 	if (error || nimaps == 0)
7077d88329eSDarrick J. Wong 		return false;
7087d88329eSDarrick J. Wong 
7097d88329eSDarrick J. Wong 	/*
7107d88329eSDarrick J. Wong 	 * If there's a real mapping there or there are delayed allocation
7117d88329eSDarrick J. Wong 	 * reservations, then we have post-EOF blocks to try to free.
7127d88329eSDarrick J. Wong 	 */
7137d88329eSDarrick J. Wong 	return imap.br_startblock != HOLESTARTBLOCK || ip->i_delayed_blks;
714c24b5dfaSDave Chinner }
715c24b5dfaSDave Chinner 
716c24b5dfaSDave Chinner /*
7173b4683c2SBrian Foster  * This is called to free any blocks beyond eof. The caller must hold
7183b4683c2SBrian Foster  * IOLOCK_EXCL unless we are in the inode reclaim path and have the only
7193b4683c2SBrian Foster  * reference to the inode.
720c24b5dfaSDave Chinner  */
721c24b5dfaSDave Chinner int
722c24b5dfaSDave Chinner xfs_free_eofblocks(
723a36b9261SBrian Foster 	struct xfs_inode	*ip)
724c24b5dfaSDave Chinner {
725a36b9261SBrian Foster 	struct xfs_trans	*tp;
726a36b9261SBrian Foster 	struct xfs_mount	*mp = ip->i_mount;
7277d88329eSDarrick J. Wong 	int			error;
728a36b9261SBrian Foster 
7297d88329eSDarrick J. Wong 	/* Attach the dquots to the inode up front. */
730c14cfccaSDarrick J. Wong 	error = xfs_qm_dqattach(ip);
731c24b5dfaSDave Chinner 	if (error)
732c24b5dfaSDave Chinner 		return error;
733c24b5dfaSDave Chinner 
7347d88329eSDarrick J. Wong 	/* Wait on dio to ensure i_size has settled. */
735e4229d6bSBrian Foster 	inode_dio_wait(VFS_I(ip));
736e4229d6bSBrian Foster 
7377d88329eSDarrick J. Wong 	error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
738c24b5dfaSDave Chinner 	if (error) {
73975c8c50fSDave Chinner 		ASSERT(xfs_is_shutdown(mp));
740c24b5dfaSDave Chinner 		return error;
741c24b5dfaSDave Chinner 	}
742c24b5dfaSDave Chinner 
743c24b5dfaSDave Chinner 	xfs_ilock(ip, XFS_ILOCK_EXCL);
744c24b5dfaSDave Chinner 	xfs_trans_ijoin(tp, ip, 0);
745c24b5dfaSDave Chinner 
746c24b5dfaSDave Chinner 	/*
7477d88329eSDarrick J. Wong 	 * Do not update the on-disk file size.  If we update the on-disk file
7487d88329eSDarrick J. Wong 	 * size and then the system crashes before the contents of the file are
7497d88329eSDarrick J. Wong 	 * flushed to disk then the files may be full of holes (ie NULL files
7507d88329eSDarrick J. Wong 	 * bug).
751c24b5dfaSDave Chinner 	 */
7524e529339SBrian Foster 	error = xfs_itruncate_extents_flags(&tp, ip, XFS_DATA_FORK,
7534e529339SBrian Foster 				XFS_ISIZE(ip), XFS_BMAPI_NODISCARD);
7547d88329eSDarrick J. Wong 	if (error)
7557d88329eSDarrick J. Wong 		goto err_cancel;
7567d88329eSDarrick J. Wong 
7577d88329eSDarrick J. Wong 	error = xfs_trans_commit(tp);
7587d88329eSDarrick J. Wong 	if (error)
7597d88329eSDarrick J. Wong 		goto out_unlock;
7607d88329eSDarrick J. Wong 
7617d88329eSDarrick J. Wong 	xfs_inode_clear_eofblocks_tag(ip);
7627d88329eSDarrick J. Wong 	goto out_unlock;
7637d88329eSDarrick J. Wong 
7647d88329eSDarrick J. Wong err_cancel:
765c24b5dfaSDave Chinner 	/*
766c24b5dfaSDave Chinner 	 * If we get an error at this point we simply don't
767c24b5dfaSDave Chinner 	 * bother truncating the file.
768c24b5dfaSDave Chinner 	 */
7694906e215SChristoph Hellwig 	xfs_trans_cancel(tp);
7707d88329eSDarrick J. Wong out_unlock:
771c24b5dfaSDave Chinner 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
772c24b5dfaSDave Chinner 	return error;
773c24b5dfaSDave Chinner }
774c24b5dfaSDave Chinner 
77583aee9e4SChristoph Hellwig int
776c24b5dfaSDave Chinner xfs_alloc_file_space(
77783aee9e4SChristoph Hellwig 	struct xfs_inode	*ip,
778c24b5dfaSDave Chinner 	xfs_off_t		offset,
7794d1b97f9SDarrick J. Wong 	xfs_off_t		len)
780c24b5dfaSDave Chinner {
781c24b5dfaSDave Chinner 	xfs_mount_t		*mp = ip->i_mount;
782c24b5dfaSDave Chinner 	xfs_off_t		count;
783c24b5dfaSDave Chinner 	xfs_filblks_t		allocated_fsb;
784c24b5dfaSDave Chinner 	xfs_filblks_t		allocatesize_fsb;
785c24b5dfaSDave Chinner 	xfs_extlen_t		extsz, temp;
786c24b5dfaSDave Chinner 	xfs_fileoff_t		startoffset_fsb;
787e093c4beSMax Reitz 	xfs_fileoff_t		endoffset_fsb;
788c24b5dfaSDave Chinner 	int			nimaps;
789c24b5dfaSDave Chinner 	int			rt;
790c24b5dfaSDave Chinner 	xfs_trans_t		*tp;
791c24b5dfaSDave Chinner 	xfs_bmbt_irec_t		imaps[1], *imapp;
792c24b5dfaSDave Chinner 	int			error;
793c24b5dfaSDave Chinner 
794c24b5dfaSDave Chinner 	trace_xfs_alloc_file_space(ip);
795c24b5dfaSDave Chinner 
79675c8c50fSDave Chinner 	if (xfs_is_shutdown(mp))
7972451337dSDave Chinner 		return -EIO;
798c24b5dfaSDave Chinner 
799c14cfccaSDarrick J. Wong 	error = xfs_qm_dqattach(ip);
800c24b5dfaSDave Chinner 	if (error)
801c24b5dfaSDave Chinner 		return error;
802c24b5dfaSDave Chinner 
803c24b5dfaSDave Chinner 	if (len <= 0)
8042451337dSDave Chinner 		return -EINVAL;
805c24b5dfaSDave Chinner 
806c24b5dfaSDave Chinner 	rt = XFS_IS_REALTIME_INODE(ip);
807c24b5dfaSDave Chinner 	extsz = xfs_get_extsz_hint(ip);
808c24b5dfaSDave Chinner 
809c24b5dfaSDave Chinner 	count = len;
810c24b5dfaSDave Chinner 	imapp = &imaps[0];
811c24b5dfaSDave Chinner 	nimaps = 1;
812c24b5dfaSDave Chinner 	startoffset_fsb	= XFS_B_TO_FSBT(mp, offset);
813e093c4beSMax Reitz 	endoffset_fsb = XFS_B_TO_FSB(mp, offset + count);
814e093c4beSMax Reitz 	allocatesize_fsb = endoffset_fsb - startoffset_fsb;
815c24b5dfaSDave Chinner 
816c24b5dfaSDave Chinner 	/*
817c24b5dfaSDave Chinner 	 * Allocate file space until done or until there is an error
818c24b5dfaSDave Chinner 	 */
819c24b5dfaSDave Chinner 	while (allocatesize_fsb && !error) {
820c24b5dfaSDave Chinner 		xfs_fileoff_t	s, e;
8213de4eb10SDarrick J. Wong 		unsigned int	dblocks, rblocks, resblks;
822c24b5dfaSDave Chinner 
823c24b5dfaSDave Chinner 		/*
824c24b5dfaSDave Chinner 		 * Determine space reservations for data/realtime.
825c24b5dfaSDave Chinner 		 */
826c24b5dfaSDave Chinner 		if (unlikely(extsz)) {
827c24b5dfaSDave Chinner 			s = startoffset_fsb;
828c24b5dfaSDave Chinner 			do_div(s, extsz);
829c24b5dfaSDave Chinner 			s *= extsz;
830c24b5dfaSDave Chinner 			e = startoffset_fsb + allocatesize_fsb;
8310703a8e1SDave Chinner 			div_u64_rem(startoffset_fsb, extsz, &temp);
8320703a8e1SDave Chinner 			if (temp)
833c24b5dfaSDave Chinner 				e += temp;
8340703a8e1SDave Chinner 			div_u64_rem(e, extsz, &temp);
8350703a8e1SDave Chinner 			if (temp)
836c24b5dfaSDave Chinner 				e += extsz - temp;
837c24b5dfaSDave Chinner 		} else {
838c24b5dfaSDave Chinner 			s = 0;
839c24b5dfaSDave Chinner 			e = allocatesize_fsb;
840c24b5dfaSDave Chinner 		}
841c24b5dfaSDave Chinner 
842c24b5dfaSDave Chinner 		/*
843c24b5dfaSDave Chinner 		 * The transaction reservation is limited to a 32-bit block
844c24b5dfaSDave Chinner 		 * count, hence we need to limit the number of blocks we are
845c24b5dfaSDave Chinner 		 * trying to reserve to avoid an overflow. We can't allocate
846c24b5dfaSDave Chinner 		 * more than @nimaps extents, and an extent is limited on disk
84795f0b95eSChandan Babu R 		 * to XFS_BMBT_MAX_EXTLEN (21 bits), so use that to enforce the
84895f0b95eSChandan Babu R 		 * limit.
849c24b5dfaSDave Chinner 		 */
85095f0b95eSChandan Babu R 		resblks = min_t(xfs_fileoff_t, (e - s),
85195f0b95eSChandan Babu R 				(XFS_MAX_BMBT_EXTLEN * nimaps));
852c24b5dfaSDave Chinner 		if (unlikely(rt)) {
85302b7ee4eSDarrick J. Wong 			dblocks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
85402b7ee4eSDarrick J. Wong 			rblocks = resblks;
855c24b5dfaSDave Chinner 		} else {
85602b7ee4eSDarrick J. Wong 			dblocks = XFS_DIOSTRAT_SPACE_RES(mp, resblks);
85702b7ee4eSDarrick J. Wong 			rblocks = 0;
858c24b5dfaSDave Chinner 		}
859c24b5dfaSDave Chinner 
8603de4eb10SDarrick J. Wong 		error = xfs_trans_alloc_inode(ip, &M_RES(mp)->tr_write,
8613de4eb10SDarrick J. Wong 				dblocks, rblocks, false, &tp);
862c24b5dfaSDave Chinner 		if (error)
8633de4eb10SDarrick J. Wong 			break;
864c24b5dfaSDave Chinner 
865727e1acdSChandan Babu R 		error = xfs_iext_count_may_overflow(ip, XFS_DATA_FORK,
866727e1acdSChandan Babu R 				XFS_IEXT_ADD_NOSPLIT_CNT);
8674f86bb4bSChandan Babu R 		if (error == -EFBIG)
8684f86bb4bSChandan Babu R 			error = xfs_iext_count_upgrade(tp, ip,
8694f86bb4bSChandan Babu R 					XFS_IEXT_ADD_NOSPLIT_CNT);
870727e1acdSChandan Babu R 		if (error)
87135b11010SDarrick J. Wong 			goto error;
872727e1acdSChandan Babu R 
873c24b5dfaSDave Chinner 		error = xfs_bmapi_write(tp, ip, startoffset_fsb,
8744d1b97f9SDarrick J. Wong 				allocatesize_fsb, XFS_BMAPI_PREALLOC, 0, imapp,
875da781e64SBrian Foster 				&nimaps);
876f6106efaSEric Sandeen 		if (error)
87735b11010SDarrick J. Wong 			goto error;
878c24b5dfaSDave Chinner 
8790b02c8c0SDave Chinner 		ip->i_diflags |= XFS_DIFLAG_PREALLOC;
8800b02c8c0SDave Chinner 		xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
8810b02c8c0SDave Chinner 
88270393313SChristoph Hellwig 		error = xfs_trans_commit(tp);
883c24b5dfaSDave Chinner 		xfs_iunlock(ip, XFS_ILOCK_EXCL);
884f6106efaSEric Sandeen 		if (error)
885c24b5dfaSDave Chinner 			break;
886c24b5dfaSDave Chinner 
887c24b5dfaSDave Chinner 		allocated_fsb = imapp->br_blockcount;
888c24b5dfaSDave Chinner 
889c24b5dfaSDave Chinner 		if (nimaps == 0) {
8902451337dSDave Chinner 			error = -ENOSPC;
891c24b5dfaSDave Chinner 			break;
892c24b5dfaSDave Chinner 		}
893c24b5dfaSDave Chinner 
894c24b5dfaSDave Chinner 		startoffset_fsb += allocated_fsb;
895c24b5dfaSDave Chinner 		allocatesize_fsb -= allocated_fsb;
896c24b5dfaSDave Chinner 	}
897c24b5dfaSDave Chinner 
898c24b5dfaSDave Chinner 	return error;
899c24b5dfaSDave Chinner 
90035b11010SDarrick J. Wong error:
9014906e215SChristoph Hellwig 	xfs_trans_cancel(tp);
902c24b5dfaSDave Chinner 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
903c24b5dfaSDave Chinner 	return error;
904c24b5dfaSDave Chinner }
905c24b5dfaSDave Chinner 
906bdb0d04fSChristoph Hellwig static int
907bdb0d04fSChristoph Hellwig xfs_unmap_extent(
90883aee9e4SChristoph Hellwig 	struct xfs_inode	*ip,
909bdb0d04fSChristoph Hellwig 	xfs_fileoff_t		startoffset_fsb,
910bdb0d04fSChristoph Hellwig 	xfs_filblks_t		len_fsb,
911bdb0d04fSChristoph Hellwig 	int			*done)
912c24b5dfaSDave Chinner {
913bdb0d04fSChristoph Hellwig 	struct xfs_mount	*mp = ip->i_mount;
914bdb0d04fSChristoph Hellwig 	struct xfs_trans	*tp;
915bdb0d04fSChristoph Hellwig 	uint			resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
916bdb0d04fSChristoph Hellwig 	int			error;
917c24b5dfaSDave Chinner 
9183de4eb10SDarrick J. Wong 	error = xfs_trans_alloc_inode(ip, &M_RES(mp)->tr_write, resblks, 0,
9193a1af6c3SDarrick J. Wong 			false, &tp);
920bdb0d04fSChristoph Hellwig 	if (error)
9213a1af6c3SDarrick J. Wong 		return error;
922c24b5dfaSDave Chinner 
92385ef08b5SChandan Babu R 	error = xfs_iext_count_may_overflow(ip, XFS_DATA_FORK,
92485ef08b5SChandan Babu R 			XFS_IEXT_PUNCH_HOLE_CNT);
9254f86bb4bSChandan Babu R 	if (error == -EFBIG)
9264f86bb4bSChandan Babu R 		error = xfs_iext_count_upgrade(tp, ip, XFS_IEXT_PUNCH_HOLE_CNT);
92785ef08b5SChandan Babu R 	if (error)
92885ef08b5SChandan Babu R 		goto out_trans_cancel;
92985ef08b5SChandan Babu R 
9302af52842SBrian Foster 	error = xfs_bunmapi(tp, ip, startoffset_fsb, len_fsb, 0, 2, done);
931bdb0d04fSChristoph Hellwig 	if (error)
932c8eac49eSBrian Foster 		goto out_trans_cancel;
933bdb0d04fSChristoph Hellwig 
934bdb0d04fSChristoph Hellwig 	error = xfs_trans_commit(tp);
935bdb0d04fSChristoph Hellwig out_unlock:
936bdb0d04fSChristoph Hellwig 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
937bdb0d04fSChristoph Hellwig 	return error;
938bdb0d04fSChristoph Hellwig 
939bdb0d04fSChristoph Hellwig out_trans_cancel:
940bdb0d04fSChristoph Hellwig 	xfs_trans_cancel(tp);
941bdb0d04fSChristoph Hellwig 	goto out_unlock;
942bdb0d04fSChristoph Hellwig }
943bdb0d04fSChristoph Hellwig 
944249bd908SDave Chinner /* Caller must first wait for the completion of any pending DIOs if required. */
9452c307174SDave Chinner int
946bdb0d04fSChristoph Hellwig xfs_flush_unmap_range(
947bdb0d04fSChristoph Hellwig 	struct xfs_inode	*ip,
948bdb0d04fSChristoph Hellwig 	xfs_off_t		offset,
949bdb0d04fSChristoph Hellwig 	xfs_off_t		len)
950bdb0d04fSChristoph Hellwig {
951bdb0d04fSChristoph Hellwig 	struct xfs_mount	*mp = ip->i_mount;
952bdb0d04fSChristoph Hellwig 	struct inode		*inode = VFS_I(ip);
953bdb0d04fSChristoph Hellwig 	xfs_off_t		rounding, start, end;
954bdb0d04fSChristoph Hellwig 	int			error;
955bdb0d04fSChristoph Hellwig 
95620bd8e63SDarrick J. Wong 	rounding = max_t(xfs_off_t, mp->m_sb.sb_blocksize, PAGE_SIZE);
957bdb0d04fSChristoph Hellwig 	start = round_down(offset, rounding);
958bdb0d04fSChristoph Hellwig 	end = round_up(offset + len, rounding) - 1;
959bdb0d04fSChristoph Hellwig 
960bdb0d04fSChristoph Hellwig 	error = filemap_write_and_wait_range(inode->i_mapping, start, end);
961c24b5dfaSDave Chinner 	if (error)
962c24b5dfaSDave Chinner 		return error;
963bdb0d04fSChristoph Hellwig 	truncate_pagecache_range(inode, start, end);
964bdb0d04fSChristoph Hellwig 	return 0;
965c24b5dfaSDave Chinner }
966c24b5dfaSDave Chinner 
967c24b5dfaSDave Chinner int
968c24b5dfaSDave Chinner xfs_free_file_space(
969c24b5dfaSDave Chinner 	struct xfs_inode	*ip,
970c24b5dfaSDave Chinner 	xfs_off_t		offset,
971c24b5dfaSDave Chinner 	xfs_off_t		len)
972c24b5dfaSDave Chinner {
973bdb0d04fSChristoph Hellwig 	struct xfs_mount	*mp = ip->i_mount;
974c24b5dfaSDave Chinner 	xfs_fileoff_t		startoffset_fsb;
975bdb0d04fSChristoph Hellwig 	xfs_fileoff_t		endoffset_fsb;
9763c2bdc91SChristoph Hellwig 	int			done = 0, error;
977c24b5dfaSDave Chinner 
978c24b5dfaSDave Chinner 	trace_xfs_free_file_space(ip);
979c24b5dfaSDave Chinner 
980c14cfccaSDarrick J. Wong 	error = xfs_qm_dqattach(ip);
981c24b5dfaSDave Chinner 	if (error)
982c24b5dfaSDave Chinner 		return error;
983c24b5dfaSDave Chinner 
984c24b5dfaSDave Chinner 	if (len <= 0)	/* if nothing being freed */
985bdb0d04fSChristoph Hellwig 		return 0;
986bdb0d04fSChristoph Hellwig 
987c24b5dfaSDave Chinner 	startoffset_fsb = XFS_B_TO_FSB(mp, offset);
988c24b5dfaSDave Chinner 	endoffset_fsb = XFS_B_TO_FSBT(mp, offset + len);
989c24b5dfaSDave Chinner 
990fe341eb1SDarrick J. Wong 	/* We can only free complete realtime extents. */
99125219dbfSDarrick J. Wong 	if (XFS_IS_REALTIME_INODE(ip) && mp->m_sb.sb_rextsize > 1) {
99225219dbfSDarrick J. Wong 		startoffset_fsb = roundup_64(startoffset_fsb,
99325219dbfSDarrick J. Wong 					     mp->m_sb.sb_rextsize);
99425219dbfSDarrick J. Wong 		endoffset_fsb = rounddown_64(endoffset_fsb,
99525219dbfSDarrick J. Wong 					     mp->m_sb.sb_rextsize);
996fe341eb1SDarrick J. Wong 	}
997fe341eb1SDarrick J. Wong 
998bdb0d04fSChristoph Hellwig 	/*
999daa79baeSChristoph Hellwig 	 * Need to zero the stuff we're not freeing, on disk.
1000bdb0d04fSChristoph Hellwig 	 */
10013c2bdc91SChristoph Hellwig 	if (endoffset_fsb > startoffset_fsb) {
10023c2bdc91SChristoph Hellwig 		while (!done) {
1003bdb0d04fSChristoph Hellwig 			error = xfs_unmap_extent(ip, startoffset_fsb,
1004bdb0d04fSChristoph Hellwig 					endoffset_fsb - startoffset_fsb, &done);
10053c2bdc91SChristoph Hellwig 			if (error)
10063c2bdc91SChristoph Hellwig 				return error;
10073c2bdc91SChristoph Hellwig 		}
1008c24b5dfaSDave Chinner 	}
1009c24b5dfaSDave Chinner 
10103c2bdc91SChristoph Hellwig 	/*
10113c2bdc91SChristoph Hellwig 	 * Now that we've unmap all full blocks we'll have to zero out any
1012f1ba5fafSShiyang Ruan 	 * partial block at the beginning and/or end.  xfs_zero_range is smart
1013f5c54717SChristoph Hellwig 	 * enough to skip any holes, including those we just created, but we
1014f5c54717SChristoph Hellwig 	 * must take care not to zero beyond EOF and enlarge i_size.
10153c2bdc91SChristoph Hellwig 	 */
10163dd09d5aSCalvin Owens 	if (offset >= XFS_ISIZE(ip))
10173dd09d5aSCalvin Owens 		return 0;
10183dd09d5aSCalvin Owens 	if (offset + len > XFS_ISIZE(ip))
10193dd09d5aSCalvin Owens 		len = XFS_ISIZE(ip) - offset;
1020f1ba5fafSShiyang Ruan 	error = xfs_zero_range(ip, offset, len, NULL);
1021e53c4b59SDarrick J. Wong 	if (error)
1022e53c4b59SDarrick J. Wong 		return error;
1023e53c4b59SDarrick J. Wong 
1024e53c4b59SDarrick J. Wong 	/*
1025e53c4b59SDarrick J. Wong 	 * If we zeroed right up to EOF and EOF straddles a page boundary we
1026e53c4b59SDarrick J. Wong 	 * must make sure that the post-EOF area is also zeroed because the
1027f1ba5fafSShiyang Ruan 	 * page could be mmap'd and xfs_zero_range doesn't do that for us.
1028e53c4b59SDarrick J. Wong 	 * Writeback of the eof page will do this, albeit clumsily.
1029e53c4b59SDarrick J. Wong 	 */
1030a579121fSDarrick J. Wong 	if (offset + len >= XFS_ISIZE(ip) && offset_in_page(offset + len) > 0) {
1031e53c4b59SDarrick J. Wong 		error = filemap_write_and_wait_range(VFS_I(ip)->i_mapping,
1032a579121fSDarrick J. Wong 				round_down(offset + len, PAGE_SIZE), LLONG_MAX);
1033e53c4b59SDarrick J. Wong 	}
1034e53c4b59SDarrick J. Wong 
1035e53c4b59SDarrick J. Wong 	return error;
1036c24b5dfaSDave Chinner }
1037c24b5dfaSDave Chinner 
103872c1a739Skbuild test robot static int
10394ed36c6bSChristoph Hellwig xfs_prepare_shift(
1040e1d8fb88SNamjae Jeon 	struct xfs_inode	*ip,
10414ed36c6bSChristoph Hellwig 	loff_t			offset)
1042e1d8fb88SNamjae Jeon {
1043d0c22041SBrian Foster 	struct xfs_mount	*mp = ip->i_mount;
1044e1d8fb88SNamjae Jeon 	int			error;
1045f71721d0SBrian Foster 
1046f71721d0SBrian Foster 	/*
1047f71721d0SBrian Foster 	 * Trim eofblocks to avoid shifting uninitialized post-eof preallocation
1048f71721d0SBrian Foster 	 * into the accessible region of the file.
1049f71721d0SBrian Foster 	 */
105041b9d726SBrian Foster 	if (xfs_can_free_eofblocks(ip, true)) {
1051a36b9261SBrian Foster 		error = xfs_free_eofblocks(ip);
105241b9d726SBrian Foster 		if (error)
105341b9d726SBrian Foster 			return error;
105441b9d726SBrian Foster 	}
10551669a8caSDave Chinner 
1056f71721d0SBrian Foster 	/*
1057d0c22041SBrian Foster 	 * Shift operations must stabilize the start block offset boundary along
1058d0c22041SBrian Foster 	 * with the full range of the operation. If we don't, a COW writeback
1059d0c22041SBrian Foster 	 * completion could race with an insert, front merge with the start
1060d0c22041SBrian Foster 	 * extent (after split) during the shift and corrupt the file. Start
1061d0c22041SBrian Foster 	 * with the block just prior to the start to stabilize the boundary.
1062d0c22041SBrian Foster 	 */
106320bd8e63SDarrick J. Wong 	offset = round_down(offset, mp->m_sb.sb_blocksize);
1064d0c22041SBrian Foster 	if (offset)
106520bd8e63SDarrick J. Wong 		offset -= mp->m_sb.sb_blocksize;
1066d0c22041SBrian Foster 
1067d0c22041SBrian Foster 	/*
1068f71721d0SBrian Foster 	 * Writeback and invalidate cache for the remainder of the file as we're
1069a904b1caSNamjae Jeon 	 * about to shift down every extent from offset to EOF.
1070f71721d0SBrian Foster 	 */
10717f9f71beSDave Chinner 	error = xfs_flush_unmap_range(ip, offset, XFS_ISIZE(ip));
10721749d1eaSBrian Foster 	if (error)
10731749d1eaSBrian Foster 		return error;
1074e1d8fb88SNamjae Jeon 
1075a904b1caSNamjae Jeon 	/*
10763af423b0SDarrick J. Wong 	 * Clean out anything hanging around in the cow fork now that
10773af423b0SDarrick J. Wong 	 * we've flushed all the dirty data out to disk to avoid having
10783af423b0SDarrick J. Wong 	 * CoW extents at the wrong offsets.
10793af423b0SDarrick J. Wong 	 */
108051d62690SChristoph Hellwig 	if (xfs_inode_has_cow_data(ip)) {
10813af423b0SDarrick J. Wong 		error = xfs_reflink_cancel_cow_range(ip, offset, NULLFILEOFF,
10823af423b0SDarrick J. Wong 				true);
10833af423b0SDarrick J. Wong 		if (error)
10843af423b0SDarrick J. Wong 			return error;
10853af423b0SDarrick J. Wong 	}
10863af423b0SDarrick J. Wong 
10874ed36c6bSChristoph Hellwig 	return 0;
1088e1d8fb88SNamjae Jeon }
1089e1d8fb88SNamjae Jeon 
1090e1d8fb88SNamjae Jeon /*
1091a904b1caSNamjae Jeon  * xfs_collapse_file_space()
1092a904b1caSNamjae Jeon  *	This routine frees disk space and shift extent for the given file.
1093a904b1caSNamjae Jeon  *	The first thing we do is to free data blocks in the specified range
1094a904b1caSNamjae Jeon  *	by calling xfs_free_file_space(). It would also sync dirty data
1095a904b1caSNamjae Jeon  *	and invalidate page cache over the region on which collapse range
1096a904b1caSNamjae Jeon  *	is working. And Shift extent records to the left to cover a hole.
1097a904b1caSNamjae Jeon  * RETURNS:
1098a904b1caSNamjae Jeon  *	0 on success
1099a904b1caSNamjae Jeon  *	errno on error
1100a904b1caSNamjae Jeon  *
1101a904b1caSNamjae Jeon  */
1102a904b1caSNamjae Jeon int
1103a904b1caSNamjae Jeon xfs_collapse_file_space(
1104a904b1caSNamjae Jeon 	struct xfs_inode	*ip,
1105a904b1caSNamjae Jeon 	xfs_off_t		offset,
1106a904b1caSNamjae Jeon 	xfs_off_t		len)
1107a904b1caSNamjae Jeon {
11084ed36c6bSChristoph Hellwig 	struct xfs_mount	*mp = ip->i_mount;
11094ed36c6bSChristoph Hellwig 	struct xfs_trans	*tp;
1110a904b1caSNamjae Jeon 	int			error;
11114ed36c6bSChristoph Hellwig 	xfs_fileoff_t		next_fsb = XFS_B_TO_FSB(mp, offset + len);
11124ed36c6bSChristoph Hellwig 	xfs_fileoff_t		shift_fsb = XFS_B_TO_FSB(mp, len);
1113ecfea3f0SChristoph Hellwig 	bool			done = false;
1114a904b1caSNamjae Jeon 
1115a904b1caSNamjae Jeon 	ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
11169ad1a23aSChristoph Hellwig 	ASSERT(xfs_isilocked(ip, XFS_MMAPLOCK_EXCL));
11179ad1a23aSChristoph Hellwig 
1118a904b1caSNamjae Jeon 	trace_xfs_collapse_file_space(ip);
1119a904b1caSNamjae Jeon 
1120a904b1caSNamjae Jeon 	error = xfs_free_file_space(ip, offset, len);
1121a904b1caSNamjae Jeon 	if (error)
1122a904b1caSNamjae Jeon 		return error;
1123a904b1caSNamjae Jeon 
11244ed36c6bSChristoph Hellwig 	error = xfs_prepare_shift(ip, offset);
11254ed36c6bSChristoph Hellwig 	if (error)
11264ed36c6bSChristoph Hellwig 		return error;
11274ed36c6bSChristoph Hellwig 
1128211683b2SBrian Foster 	error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0, 0, &tp);
11294ed36c6bSChristoph Hellwig 	if (error)
1130211683b2SBrian Foster 		return error;
11314ed36c6bSChristoph Hellwig 
11324ed36c6bSChristoph Hellwig 	xfs_ilock(ip, XFS_ILOCK_EXCL);
1133211683b2SBrian Foster 	xfs_trans_ijoin(tp, ip, 0);
11344ed36c6bSChristoph Hellwig 
1135211683b2SBrian Foster 	while (!done) {
1136ecfea3f0SChristoph Hellwig 		error = xfs_bmap_collapse_extents(tp, ip, &next_fsb, shift_fsb,
1137333f950cSBrian Foster 				&done);
11384ed36c6bSChristoph Hellwig 		if (error)
1139c8eac49eSBrian Foster 			goto out_trans_cancel;
1140211683b2SBrian Foster 		if (done)
1141211683b2SBrian Foster 			break;
11424ed36c6bSChristoph Hellwig 
1143211683b2SBrian Foster 		/* finish any deferred frees and roll the transaction */
1144211683b2SBrian Foster 		error = xfs_defer_finish(&tp);
1145211683b2SBrian Foster 		if (error)
1146211683b2SBrian Foster 			goto out_trans_cancel;
11474ed36c6bSChristoph Hellwig 	}
11484ed36c6bSChristoph Hellwig 
1149211683b2SBrian Foster 	error = xfs_trans_commit(tp);
1150211683b2SBrian Foster 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
11514ed36c6bSChristoph Hellwig 	return error;
11524ed36c6bSChristoph Hellwig 
11534ed36c6bSChristoph Hellwig out_trans_cancel:
11544ed36c6bSChristoph Hellwig 	xfs_trans_cancel(tp);
1155211683b2SBrian Foster 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
11564ed36c6bSChristoph Hellwig 	return error;
1157a904b1caSNamjae Jeon }
1158a904b1caSNamjae Jeon 
1159a904b1caSNamjae Jeon /*
1160a904b1caSNamjae Jeon  * xfs_insert_file_space()
1161a904b1caSNamjae Jeon  *	This routine create hole space by shifting extents for the given file.
1162a904b1caSNamjae Jeon  *	The first thing we do is to sync dirty data and invalidate page cache
1163a904b1caSNamjae Jeon  *	over the region on which insert range is working. And split an extent
1164a904b1caSNamjae Jeon  *	to two extents at given offset by calling xfs_bmap_split_extent.
1165a904b1caSNamjae Jeon  *	And shift all extent records which are laying between [offset,
1166a904b1caSNamjae Jeon  *	last allocated extent] to the right to reserve hole range.
1167a904b1caSNamjae Jeon  * RETURNS:
1168a904b1caSNamjae Jeon  *	0 on success
1169a904b1caSNamjae Jeon  *	errno on error
1170a904b1caSNamjae Jeon  */
1171a904b1caSNamjae Jeon int
1172a904b1caSNamjae Jeon xfs_insert_file_space(
1173a904b1caSNamjae Jeon 	struct xfs_inode	*ip,
1174a904b1caSNamjae Jeon 	loff_t			offset,
1175a904b1caSNamjae Jeon 	loff_t			len)
1176a904b1caSNamjae Jeon {
11774ed36c6bSChristoph Hellwig 	struct xfs_mount	*mp = ip->i_mount;
11784ed36c6bSChristoph Hellwig 	struct xfs_trans	*tp;
11794ed36c6bSChristoph Hellwig 	int			error;
11804ed36c6bSChristoph Hellwig 	xfs_fileoff_t		stop_fsb = XFS_B_TO_FSB(mp, offset);
11814ed36c6bSChristoph Hellwig 	xfs_fileoff_t		next_fsb = NULLFSBLOCK;
11824ed36c6bSChristoph Hellwig 	xfs_fileoff_t		shift_fsb = XFS_B_TO_FSB(mp, len);
1183ecfea3f0SChristoph Hellwig 	bool			done = false;
11844ed36c6bSChristoph Hellwig 
1185a904b1caSNamjae Jeon 	ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
11869ad1a23aSChristoph Hellwig 	ASSERT(xfs_isilocked(ip, XFS_MMAPLOCK_EXCL));
11879ad1a23aSChristoph Hellwig 
1188a904b1caSNamjae Jeon 	trace_xfs_insert_file_space(ip);
1189a904b1caSNamjae Jeon 
1190f62cb48eSDarrick J. Wong 	error = xfs_bmap_can_insert_extents(ip, stop_fsb, shift_fsb);
1191f62cb48eSDarrick J. Wong 	if (error)
1192f62cb48eSDarrick J. Wong 		return error;
1193f62cb48eSDarrick J. Wong 
11944ed36c6bSChristoph Hellwig 	error = xfs_prepare_shift(ip, offset);
11954ed36c6bSChristoph Hellwig 	if (error)
11964ed36c6bSChristoph Hellwig 		return error;
11974ed36c6bSChristoph Hellwig 
1198b73df17eSBrian Foster 	error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write,
1199b73df17eSBrian Foster 			XFS_DIOSTRAT_SPACE_RES(mp, 0), 0, 0, &tp);
1200b73df17eSBrian Foster 	if (error)
1201b73df17eSBrian Foster 		return error;
1202b73df17eSBrian Foster 
1203b73df17eSBrian Foster 	xfs_ilock(ip, XFS_ILOCK_EXCL);
1204dd87f87dSBrian Foster 	xfs_trans_ijoin(tp, ip, 0);
1205b73df17eSBrian Foster 
120685ef08b5SChandan Babu R 	error = xfs_iext_count_may_overflow(ip, XFS_DATA_FORK,
120785ef08b5SChandan Babu R 			XFS_IEXT_PUNCH_HOLE_CNT);
12084f86bb4bSChandan Babu R 	if (error == -EFBIG)
12094f86bb4bSChandan Babu R 		error = xfs_iext_count_upgrade(tp, ip, XFS_IEXT_PUNCH_HOLE_CNT);
121085ef08b5SChandan Babu R 	if (error)
121185ef08b5SChandan Babu R 		goto out_trans_cancel;
121285ef08b5SChandan Babu R 
1213dd87f87dSBrian Foster 	/*
1214dd87f87dSBrian Foster 	 * The extent shifting code works on extent granularity. So, if stop_fsb
1215dd87f87dSBrian Foster 	 * is not the starting block of extent, we need to split the extent at
1216dd87f87dSBrian Foster 	 * stop_fsb.
1217dd87f87dSBrian Foster 	 */
1218b73df17eSBrian Foster 	error = xfs_bmap_split_extent(tp, ip, stop_fsb);
1219b73df17eSBrian Foster 	if (error)
1220b73df17eSBrian Foster 		goto out_trans_cancel;
1221b73df17eSBrian Foster 
1222dd87f87dSBrian Foster 	do {
12239c516e0eSBrian Foster 		error = xfs_defer_finish(&tp);
12244ed36c6bSChristoph Hellwig 		if (error)
1225dd87f87dSBrian Foster 			goto out_trans_cancel;
12264ed36c6bSChristoph Hellwig 
1227ecfea3f0SChristoph Hellwig 		error = xfs_bmap_insert_extents(tp, ip, &next_fsb, shift_fsb,
1228333f950cSBrian Foster 				&done, stop_fsb);
12294ed36c6bSChristoph Hellwig 		if (error)
1230c8eac49eSBrian Foster 			goto out_trans_cancel;
1231dd87f87dSBrian Foster 	} while (!done);
12324ed36c6bSChristoph Hellwig 
12334ed36c6bSChristoph Hellwig 	error = xfs_trans_commit(tp);
1234dd87f87dSBrian Foster 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
12354ed36c6bSChristoph Hellwig 	return error;
12364ed36c6bSChristoph Hellwig 
1237c8eac49eSBrian Foster out_trans_cancel:
12384ed36c6bSChristoph Hellwig 	xfs_trans_cancel(tp);
1239dd87f87dSBrian Foster 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
12404ed36c6bSChristoph Hellwig 	return error;
1241a904b1caSNamjae Jeon }
1242a904b1caSNamjae Jeon 
1243a904b1caSNamjae Jeon /*
1244a133d952SDave Chinner  * We need to check that the format of the data fork in the temporary inode is
1245a133d952SDave Chinner  * valid for the target inode before doing the swap. This is not a problem with
1246a133d952SDave Chinner  * attr1 because of the fixed fork offset, but attr2 has a dynamically sized
1247a133d952SDave Chinner  * data fork depending on the space the attribute fork is taking so we can get
1248a133d952SDave Chinner  * invalid formats on the target inode.
1249a133d952SDave Chinner  *
1250a133d952SDave Chinner  * E.g. target has space for 7 extents in extent format, temp inode only has
1251a133d952SDave Chinner  * space for 6.  If we defragment down to 7 extents, then the tmp format is a
1252a133d952SDave Chinner  * btree, but when swapped it needs to be in extent format. Hence we can't just
1253a133d952SDave Chinner  * blindly swap data forks on attr2 filesystems.
1254a133d952SDave Chinner  *
1255a133d952SDave Chinner  * Note that we check the swap in both directions so that we don't end up with
1256a133d952SDave Chinner  * a corrupt temporary inode, either.
1257a133d952SDave Chinner  *
1258a133d952SDave Chinner  * Note that fixing the way xfs_fsr sets up the attribute fork in the source
1259a133d952SDave Chinner  * inode will prevent this situation from occurring, so all we do here is
1260a133d952SDave Chinner  * reject and log the attempt. basically we are putting the responsibility on
1261a133d952SDave Chinner  * userspace to get this right.
1262a133d952SDave Chinner  */
1263a133d952SDave Chinner static int
1264a133d952SDave Chinner xfs_swap_extents_check_format(
1265e06259aaSDarrick J. Wong 	struct xfs_inode	*ip,	/* target inode */
1266e06259aaSDarrick J. Wong 	struct xfs_inode	*tip)	/* tmp inode */
1267a133d952SDave Chinner {
1268f7e67b20SChristoph Hellwig 	struct xfs_ifork	*ifp = &ip->i_df;
1269f7e67b20SChristoph Hellwig 	struct xfs_ifork	*tifp = &tip->i_df;
1270a133d952SDave Chinner 
1271765d3c39SDarrick J. Wong 	/* User/group/project quota ids must match if quotas are enforced. */
1272765d3c39SDarrick J. Wong 	if (XFS_IS_QUOTA_ON(ip->i_mount) &&
1273765d3c39SDarrick J. Wong 	    (!uid_eq(VFS_I(ip)->i_uid, VFS_I(tip)->i_uid) ||
1274765d3c39SDarrick J. Wong 	     !gid_eq(VFS_I(ip)->i_gid, VFS_I(tip)->i_gid) ||
1275ceaf603cSChristoph Hellwig 	     ip->i_projid != tip->i_projid))
1276765d3c39SDarrick J. Wong 		return -EINVAL;
1277765d3c39SDarrick J. Wong 
1278a133d952SDave Chinner 	/* Should never get a local format */
1279f7e67b20SChristoph Hellwig 	if (ifp->if_format == XFS_DINODE_FMT_LOCAL ||
1280f7e67b20SChristoph Hellwig 	    tifp->if_format == XFS_DINODE_FMT_LOCAL)
12812451337dSDave Chinner 		return -EINVAL;
1282a133d952SDave Chinner 
1283a133d952SDave Chinner 	/*
1284a133d952SDave Chinner 	 * if the target inode has less extents that then temporary inode then
1285a133d952SDave Chinner 	 * why did userspace call us?
1286a133d952SDave Chinner 	 */
1287f7e67b20SChristoph Hellwig 	if (ifp->if_nextents < tifp->if_nextents)
12882451337dSDave Chinner 		return -EINVAL;
1289a133d952SDave Chinner 
1290a133d952SDave Chinner 	/*
12911f08af52SDarrick J. Wong 	 * If we have to use the (expensive) rmap swap method, we can
12921f08af52SDarrick J. Wong 	 * handle any number of extents and any format.
12931f08af52SDarrick J. Wong 	 */
129438c26bfdSDave Chinner 	if (xfs_has_rmapbt(ip->i_mount))
12951f08af52SDarrick J. Wong 		return 0;
12961f08af52SDarrick J. Wong 
12971f08af52SDarrick J. Wong 	/*
1298a133d952SDave Chinner 	 * if the target inode is in extent form and the temp inode is in btree
1299a133d952SDave Chinner 	 * form then we will end up with the target inode in the wrong format
1300a133d952SDave Chinner 	 * as we already know there are less extents in the temp inode.
1301a133d952SDave Chinner 	 */
1302f7e67b20SChristoph Hellwig 	if (ifp->if_format == XFS_DINODE_FMT_EXTENTS &&
1303f7e67b20SChristoph Hellwig 	    tifp->if_format == XFS_DINODE_FMT_BTREE)
13042451337dSDave Chinner 		return -EINVAL;
1305a133d952SDave Chinner 
1306a133d952SDave Chinner 	/* Check temp in extent form to max in target */
1307f7e67b20SChristoph Hellwig 	if (tifp->if_format == XFS_DINODE_FMT_EXTENTS &&
1308f7e67b20SChristoph Hellwig 	    tifp->if_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK))
13092451337dSDave Chinner 		return -EINVAL;
1310a133d952SDave Chinner 
1311a133d952SDave Chinner 	/* Check target in extent form to max in temp */
1312f7e67b20SChristoph Hellwig 	if (ifp->if_format == XFS_DINODE_FMT_EXTENTS &&
1313f7e67b20SChristoph Hellwig 	    ifp->if_nextents > XFS_IFORK_MAXEXT(tip, XFS_DATA_FORK))
13142451337dSDave Chinner 		return -EINVAL;
1315a133d952SDave Chinner 
1316a133d952SDave Chinner 	/*
1317a133d952SDave Chinner 	 * If we are in a btree format, check that the temp root block will fit
1318a133d952SDave Chinner 	 * in the target and that it has enough extents to be in btree format
1319a133d952SDave Chinner 	 * in the target.
1320a133d952SDave Chinner 	 *
1321a133d952SDave Chinner 	 * Note that we have to be careful to allow btree->extent conversions
1322a133d952SDave Chinner 	 * (a common defrag case) which will occur when the temp inode is in
1323a133d952SDave Chinner 	 * extent format...
1324a133d952SDave Chinner 	 */
1325f7e67b20SChristoph Hellwig 	if (tifp->if_format == XFS_DINODE_FMT_BTREE) {
1326932b42c6SDarrick J. Wong 		if (xfs_inode_has_attr_fork(ip) &&
1327c01147d9SDarrick J. Wong 		    XFS_BMAP_BMDR_SPACE(tifp->if_broot) > xfs_inode_fork_boff(ip))
13282451337dSDave Chinner 			return -EINVAL;
1329f7e67b20SChristoph Hellwig 		if (tifp->if_nextents <= XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK))
13302451337dSDave Chinner 			return -EINVAL;
1331a133d952SDave Chinner 	}
1332a133d952SDave Chinner 
1333a133d952SDave Chinner 	/* Reciprocal target->temp btree format checks */
1334f7e67b20SChristoph Hellwig 	if (ifp->if_format == XFS_DINODE_FMT_BTREE) {
1335932b42c6SDarrick J. Wong 		if (xfs_inode_has_attr_fork(tip) &&
1336c01147d9SDarrick J. Wong 		    XFS_BMAP_BMDR_SPACE(ip->i_df.if_broot) > xfs_inode_fork_boff(tip))
13372451337dSDave Chinner 			return -EINVAL;
1338f7e67b20SChristoph Hellwig 		if (ifp->if_nextents <= XFS_IFORK_MAXEXT(tip, XFS_DATA_FORK))
13392451337dSDave Chinner 			return -EINVAL;
1340a133d952SDave Chinner 	}
1341a133d952SDave Chinner 
1342a133d952SDave Chinner 	return 0;
1343a133d952SDave Chinner }
1344a133d952SDave Chinner 
13457abbb8f9SDave Chinner static int
13464ef897a2SDave Chinner xfs_swap_extent_flush(
13474ef897a2SDave Chinner 	struct xfs_inode	*ip)
13484ef897a2SDave Chinner {
13494ef897a2SDave Chinner 	int	error;
13504ef897a2SDave Chinner 
13514ef897a2SDave Chinner 	error = filemap_write_and_wait(VFS_I(ip)->i_mapping);
13524ef897a2SDave Chinner 	if (error)
13534ef897a2SDave Chinner 		return error;
13544ef897a2SDave Chinner 	truncate_pagecache_range(VFS_I(ip), 0, -1);
13554ef897a2SDave Chinner 
13564ef897a2SDave Chinner 	/* Verify O_DIRECT for ftmp */
13574ef897a2SDave Chinner 	if (VFS_I(ip)->i_mapping->nrpages)
13584ef897a2SDave Chinner 		return -EINVAL;
13594ef897a2SDave Chinner 	return 0;
13604ef897a2SDave Chinner }
13614ef897a2SDave Chinner 
13621f08af52SDarrick J. Wong /*
13631f08af52SDarrick J. Wong  * Move extents from one file to another, when rmap is enabled.
13641f08af52SDarrick J. Wong  */
13651f08af52SDarrick J. Wong STATIC int
13661f08af52SDarrick J. Wong xfs_swap_extent_rmap(
13671f08af52SDarrick J. Wong 	struct xfs_trans		**tpp,
13681f08af52SDarrick J. Wong 	struct xfs_inode		*ip,
13691f08af52SDarrick J. Wong 	struct xfs_inode		*tip)
13701f08af52SDarrick J. Wong {
13717a7943c7SBrian Foster 	struct xfs_trans		*tp = *tpp;
13721f08af52SDarrick J. Wong 	struct xfs_bmbt_irec		irec;
13731f08af52SDarrick J. Wong 	struct xfs_bmbt_irec		uirec;
13741f08af52SDarrick J. Wong 	struct xfs_bmbt_irec		tirec;
13751f08af52SDarrick J. Wong 	xfs_fileoff_t			offset_fsb;
13761f08af52SDarrick J. Wong 	xfs_fileoff_t			end_fsb;
13771f08af52SDarrick J. Wong 	xfs_filblks_t			count_fsb;
13781f08af52SDarrick J. Wong 	int				error;
13791f08af52SDarrick J. Wong 	xfs_filblks_t			ilen;
13801f08af52SDarrick J. Wong 	xfs_filblks_t			rlen;
13811f08af52SDarrick J. Wong 	int				nimaps;
1382c8ce540dSDarrick J. Wong 	uint64_t			tip_flags2;
13831f08af52SDarrick J. Wong 
13841f08af52SDarrick J. Wong 	/*
13851f08af52SDarrick J. Wong 	 * If the source file has shared blocks, we must flag the donor
13861f08af52SDarrick J. Wong 	 * file as having shared blocks so that we get the shared-block
13871f08af52SDarrick J. Wong 	 * rmap functions when we go to fix up the rmaps.  The flags
13881f08af52SDarrick J. Wong 	 * will be switch for reals later.
13891f08af52SDarrick J. Wong 	 */
13903e09ab8fSChristoph Hellwig 	tip_flags2 = tip->i_diflags2;
13913e09ab8fSChristoph Hellwig 	if (ip->i_diflags2 & XFS_DIFLAG2_REFLINK)
13923e09ab8fSChristoph Hellwig 		tip->i_diflags2 |= XFS_DIFLAG2_REFLINK;
13931f08af52SDarrick J. Wong 
13941f08af52SDarrick J. Wong 	offset_fsb = 0;
13951f08af52SDarrick J. Wong 	end_fsb = XFS_B_TO_FSB(ip->i_mount, i_size_read(VFS_I(ip)));
13961f08af52SDarrick J. Wong 	count_fsb = (xfs_filblks_t)(end_fsb - offset_fsb);
13971f08af52SDarrick J. Wong 
13981f08af52SDarrick J. Wong 	while (count_fsb) {
13991f08af52SDarrick J. Wong 		/* Read extent from the donor file */
14001f08af52SDarrick J. Wong 		nimaps = 1;
14011f08af52SDarrick J. Wong 		error = xfs_bmapi_read(tip, offset_fsb, count_fsb, &tirec,
14021f08af52SDarrick J. Wong 				&nimaps, 0);
14031f08af52SDarrick J. Wong 		if (error)
14041f08af52SDarrick J. Wong 			goto out;
14051f08af52SDarrick J. Wong 		ASSERT(nimaps == 1);
14061f08af52SDarrick J. Wong 		ASSERT(tirec.br_startblock != DELAYSTARTBLOCK);
14071f08af52SDarrick J. Wong 
14081f08af52SDarrick J. Wong 		trace_xfs_swap_extent_rmap_remap(tip, &tirec);
14091f08af52SDarrick J. Wong 		ilen = tirec.br_blockcount;
14101f08af52SDarrick J. Wong 
14111f08af52SDarrick J. Wong 		/* Unmap the old blocks in the source file. */
14121f08af52SDarrick J. Wong 		while (tirec.br_blockcount) {
1413692b6cddSDave Chinner 			ASSERT(tp->t_highest_agno == NULLAGNUMBER);
14141f08af52SDarrick J. Wong 			trace_xfs_swap_extent_rmap_remap_piece(tip, &tirec);
14151f08af52SDarrick J. Wong 
14161f08af52SDarrick J. Wong 			/* Read extent from the source file */
14171f08af52SDarrick J. Wong 			nimaps = 1;
14181f08af52SDarrick J. Wong 			error = xfs_bmapi_read(ip, tirec.br_startoff,
14191f08af52SDarrick J. Wong 					tirec.br_blockcount, &irec,
14201f08af52SDarrick J. Wong 					&nimaps, 0);
14211f08af52SDarrick J. Wong 			if (error)
1422d5a2e289SBrian Foster 				goto out;
14231f08af52SDarrick J. Wong 			ASSERT(nimaps == 1);
14241f08af52SDarrick J. Wong 			ASSERT(tirec.br_startoff == irec.br_startoff);
14251f08af52SDarrick J. Wong 			trace_xfs_swap_extent_rmap_remap_piece(ip, &irec);
14261f08af52SDarrick J. Wong 
14271f08af52SDarrick J. Wong 			/* Trim the extent. */
14281f08af52SDarrick J. Wong 			uirec = tirec;
14291f08af52SDarrick J. Wong 			uirec.br_blockcount = rlen = min_t(xfs_filblks_t,
14301f08af52SDarrick J. Wong 					tirec.br_blockcount,
14311f08af52SDarrick J. Wong 					irec.br_blockcount);
14321f08af52SDarrick J. Wong 			trace_xfs_swap_extent_rmap_remap_piece(tip, &uirec);
14331f08af52SDarrick J. Wong 
1434bcc561f2SChandan Babu R 			if (xfs_bmap_is_real_extent(&uirec)) {
1435bcc561f2SChandan Babu R 				error = xfs_iext_count_may_overflow(ip,
1436bcc561f2SChandan Babu R 						XFS_DATA_FORK,
1437bcc561f2SChandan Babu R 						XFS_IEXT_SWAP_RMAP_CNT);
14384f86bb4bSChandan Babu R 				if (error == -EFBIG)
14394f86bb4bSChandan Babu R 					error = xfs_iext_count_upgrade(tp, ip,
14404f86bb4bSChandan Babu R 							XFS_IEXT_SWAP_RMAP_CNT);
1441bcc561f2SChandan Babu R 				if (error)
1442bcc561f2SChandan Babu R 					goto out;
1443bcc561f2SChandan Babu R 			}
1444bcc561f2SChandan Babu R 
1445bcc561f2SChandan Babu R 			if (xfs_bmap_is_real_extent(&irec)) {
1446bcc561f2SChandan Babu R 				error = xfs_iext_count_may_overflow(tip,
1447bcc561f2SChandan Babu R 						XFS_DATA_FORK,
1448bcc561f2SChandan Babu R 						XFS_IEXT_SWAP_RMAP_CNT);
14494f86bb4bSChandan Babu R 				if (error == -EFBIG)
14504f86bb4bSChandan Babu R 					error = xfs_iext_count_upgrade(tp, ip,
14514f86bb4bSChandan Babu R 							XFS_IEXT_SWAP_RMAP_CNT);
1452bcc561f2SChandan Babu R 				if (error)
1453bcc561f2SChandan Babu R 					goto out;
1454bcc561f2SChandan Babu R 			}
1455bcc561f2SChandan Babu R 
14561f08af52SDarrick J. Wong 			/* Remove the mapping from the donor file. */
14573e08f42aSDarrick J. Wong 			xfs_bmap_unmap_extent(tp, tip, &uirec);
14581f08af52SDarrick J. Wong 
14591f08af52SDarrick J. Wong 			/* Remove the mapping from the source file. */
14603e08f42aSDarrick J. Wong 			xfs_bmap_unmap_extent(tp, ip, &irec);
14611f08af52SDarrick J. Wong 
14621f08af52SDarrick J. Wong 			/* Map the donor file's blocks into the source file. */
14633e08f42aSDarrick J. Wong 			xfs_bmap_map_extent(tp, ip, &uirec);
14641f08af52SDarrick J. Wong 
14651f08af52SDarrick J. Wong 			/* Map the source file's blocks into the donor file. */
14663e08f42aSDarrick J. Wong 			xfs_bmap_map_extent(tp, tip, &irec);
14671f08af52SDarrick J. Wong 
14689e28a242SBrian Foster 			error = xfs_defer_finish(tpp);
14697a7943c7SBrian Foster 			tp = *tpp;
14701f08af52SDarrick J. Wong 			if (error)
14719b1f4e98SBrian Foster 				goto out;
14721f08af52SDarrick J. Wong 
14731f08af52SDarrick J. Wong 			tirec.br_startoff += rlen;
14741f08af52SDarrick J. Wong 			if (tirec.br_startblock != HOLESTARTBLOCK &&
14751f08af52SDarrick J. Wong 			    tirec.br_startblock != DELAYSTARTBLOCK)
14761f08af52SDarrick J. Wong 				tirec.br_startblock += rlen;
14771f08af52SDarrick J. Wong 			tirec.br_blockcount -= rlen;
14781f08af52SDarrick J. Wong 		}
14791f08af52SDarrick J. Wong 
14801f08af52SDarrick J. Wong 		/* Roll on... */
14811f08af52SDarrick J. Wong 		count_fsb -= ilen;
14821f08af52SDarrick J. Wong 		offset_fsb += ilen;
14831f08af52SDarrick J. Wong 	}
14841f08af52SDarrick J. Wong 
14853e09ab8fSChristoph Hellwig 	tip->i_diflags2 = tip_flags2;
14861f08af52SDarrick J. Wong 	return 0;
14871f08af52SDarrick J. Wong 
14881f08af52SDarrick J. Wong out:
14891f08af52SDarrick J. Wong 	trace_xfs_swap_extent_rmap_error(ip, error, _RET_IP_);
14903e09ab8fSChristoph Hellwig 	tip->i_diflags2 = tip_flags2;
14911f08af52SDarrick J. Wong 	return error;
14921f08af52SDarrick J. Wong }
14931f08af52SDarrick J. Wong 
149439aff5fdSDarrick J. Wong /* Swap the extents of two files by swapping data forks. */
149539aff5fdSDarrick J. Wong STATIC int
149639aff5fdSDarrick J. Wong xfs_swap_extent_forks(
149739aff5fdSDarrick J. Wong 	struct xfs_trans	*tp,
149839aff5fdSDarrick J. Wong 	struct xfs_inode	*ip,
149939aff5fdSDarrick J. Wong 	struct xfs_inode	*tip,
150039aff5fdSDarrick J. Wong 	int			*src_log_flags,
150139aff5fdSDarrick J. Wong 	int			*target_log_flags)
150239aff5fdSDarrick J. Wong {
1503e7f5d5caSDarrick J. Wong 	xfs_filblks_t		aforkblks = 0;
1504e7f5d5caSDarrick J. Wong 	xfs_filblks_t		taforkblks = 0;
1505e7f5d5caSDarrick J. Wong 	xfs_extnum_t		junk;
1506c8ce540dSDarrick J. Wong 	uint64_t		tmp;
150739aff5fdSDarrick J. Wong 	int			error;
150839aff5fdSDarrick J. Wong 
150939aff5fdSDarrick J. Wong 	/*
151039aff5fdSDarrick J. Wong 	 * Count the number of extended attribute blocks
151139aff5fdSDarrick J. Wong 	 */
1512932b42c6SDarrick J. Wong 	if (xfs_inode_has_attr_fork(ip) && ip->i_af.if_nextents > 0 &&
15132ed5b09bSDarrick J. Wong 	    ip->i_af.if_format != XFS_DINODE_FMT_LOCAL) {
1514e7f5d5caSDarrick J. Wong 		error = xfs_bmap_count_blocks(tp, ip, XFS_ATTR_FORK, &junk,
151539aff5fdSDarrick J. Wong 				&aforkblks);
151639aff5fdSDarrick J. Wong 		if (error)
151739aff5fdSDarrick J. Wong 			return error;
151839aff5fdSDarrick J. Wong 	}
1519932b42c6SDarrick J. Wong 	if (xfs_inode_has_attr_fork(tip) && tip->i_af.if_nextents > 0 &&
15202ed5b09bSDarrick J. Wong 	    tip->i_af.if_format != XFS_DINODE_FMT_LOCAL) {
1521e7f5d5caSDarrick J. Wong 		error = xfs_bmap_count_blocks(tp, tip, XFS_ATTR_FORK, &junk,
152239aff5fdSDarrick J. Wong 				&taforkblks);
152339aff5fdSDarrick J. Wong 		if (error)
152439aff5fdSDarrick J. Wong 			return error;
152539aff5fdSDarrick J. Wong 	}
152639aff5fdSDarrick J. Wong 
152739aff5fdSDarrick J. Wong 	/*
15286fb10d6dSBrian Foster 	 * Btree format (v3) inodes have the inode number stamped in the bmbt
15296fb10d6dSBrian Foster 	 * block headers. We can't start changing the bmbt blocks until the
15306fb10d6dSBrian Foster 	 * inode owner change is logged so recovery does the right thing in the
15316fb10d6dSBrian Foster 	 * event of a crash. Set the owner change log flags now and leave the
15326fb10d6dSBrian Foster 	 * bmbt scan as the last step.
153339aff5fdSDarrick J. Wong 	 */
153438c26bfdSDave Chinner 	if (xfs_has_v3inodes(ip->i_mount)) {
1535f7e67b20SChristoph Hellwig 		if (ip->i_df.if_format == XFS_DINODE_FMT_BTREE)
153639aff5fdSDarrick J. Wong 			(*target_log_flags) |= XFS_ILOG_DOWNER;
1537f7e67b20SChristoph Hellwig 		if (tip->i_df.if_format == XFS_DINODE_FMT_BTREE)
153839aff5fdSDarrick J. Wong 			(*src_log_flags) |= XFS_ILOG_DOWNER;
15396471e9c5SChristoph Hellwig 	}
154039aff5fdSDarrick J. Wong 
154139aff5fdSDarrick J. Wong 	/*
154239aff5fdSDarrick J. Wong 	 * Swap the data forks of the inodes
154339aff5fdSDarrick J. Wong 	 */
1544897992b7SGustavo A. R. Silva 	swap(ip->i_df, tip->i_df);
154539aff5fdSDarrick J. Wong 
154639aff5fdSDarrick J. Wong 	/*
154739aff5fdSDarrick J. Wong 	 * Fix the on-disk inode values
154839aff5fdSDarrick J. Wong 	 */
15496e73a545SChristoph Hellwig 	tmp = (uint64_t)ip->i_nblocks;
15506e73a545SChristoph Hellwig 	ip->i_nblocks = tip->i_nblocks - taforkblks + aforkblks;
15516e73a545SChristoph Hellwig 	tip->i_nblocks = tmp + taforkblks - aforkblks;
155239aff5fdSDarrick J. Wong 
155339aff5fdSDarrick J. Wong 	/*
155439aff5fdSDarrick J. Wong 	 * The extents in the source inode could still contain speculative
155539aff5fdSDarrick J. Wong 	 * preallocation beyond EOF (e.g. the file is open but not modified
155639aff5fdSDarrick J. Wong 	 * while defrag is in progress). In that case, we need to copy over the
155739aff5fdSDarrick J. Wong 	 * number of delalloc blocks the data fork in the source inode is
155839aff5fdSDarrick J. Wong 	 * tracking beyond EOF so that when the fork is truncated away when the
155939aff5fdSDarrick J. Wong 	 * temporary inode is unlinked we don't underrun the i_delayed_blks
156039aff5fdSDarrick J. Wong 	 * counter on that inode.
156139aff5fdSDarrick J. Wong 	 */
156239aff5fdSDarrick J. Wong 	ASSERT(tip->i_delayed_blks == 0);
156339aff5fdSDarrick J. Wong 	tip->i_delayed_blks = ip->i_delayed_blks;
156439aff5fdSDarrick J. Wong 	ip->i_delayed_blks = 0;
156539aff5fdSDarrick J. Wong 
1566f7e67b20SChristoph Hellwig 	switch (ip->i_df.if_format) {
156739aff5fdSDarrick J. Wong 	case XFS_DINODE_FMT_EXTENTS:
156839aff5fdSDarrick J. Wong 		(*src_log_flags) |= XFS_ILOG_DEXT;
156939aff5fdSDarrick J. Wong 		break;
157039aff5fdSDarrick J. Wong 	case XFS_DINODE_FMT_BTREE:
157138c26bfdSDave Chinner 		ASSERT(!xfs_has_v3inodes(ip->i_mount) ||
157239aff5fdSDarrick J. Wong 		       (*src_log_flags & XFS_ILOG_DOWNER));
157339aff5fdSDarrick J. Wong 		(*src_log_flags) |= XFS_ILOG_DBROOT;
157439aff5fdSDarrick J. Wong 		break;
157539aff5fdSDarrick J. Wong 	}
157639aff5fdSDarrick J. Wong 
1577f7e67b20SChristoph Hellwig 	switch (tip->i_df.if_format) {
157839aff5fdSDarrick J. Wong 	case XFS_DINODE_FMT_EXTENTS:
157939aff5fdSDarrick J. Wong 		(*target_log_flags) |= XFS_ILOG_DEXT;
158039aff5fdSDarrick J. Wong 		break;
158139aff5fdSDarrick J. Wong 	case XFS_DINODE_FMT_BTREE:
158239aff5fdSDarrick J. Wong 		(*target_log_flags) |= XFS_ILOG_DBROOT;
158338c26bfdSDave Chinner 		ASSERT(!xfs_has_v3inodes(ip->i_mount) ||
158439aff5fdSDarrick J. Wong 		       (*target_log_flags & XFS_ILOG_DOWNER));
158539aff5fdSDarrick J. Wong 		break;
158639aff5fdSDarrick J. Wong 	}
158739aff5fdSDarrick J. Wong 
158839aff5fdSDarrick J. Wong 	return 0;
158939aff5fdSDarrick J. Wong }
159039aff5fdSDarrick J. Wong 
15912dd3d709SBrian Foster /*
15922dd3d709SBrian Foster  * Fix up the owners of the bmbt blocks to refer to the current inode. The
15932dd3d709SBrian Foster  * change owner scan attempts to order all modified buffers in the current
15942dd3d709SBrian Foster  * transaction. In the event of ordered buffer failure, the offending buffer is
15952dd3d709SBrian Foster  * physically logged as a fallback and the scan returns -EAGAIN. We must roll
15962dd3d709SBrian Foster  * the transaction in this case to replenish the fallback log reservation and
15972dd3d709SBrian Foster  * restart the scan. This process repeats until the scan completes.
15982dd3d709SBrian Foster  */
15992dd3d709SBrian Foster static int
16002dd3d709SBrian Foster xfs_swap_change_owner(
16012dd3d709SBrian Foster 	struct xfs_trans	**tpp,
16022dd3d709SBrian Foster 	struct xfs_inode	*ip,
16032dd3d709SBrian Foster 	struct xfs_inode	*tmpip)
16042dd3d709SBrian Foster {
16052dd3d709SBrian Foster 	int			error;
16062dd3d709SBrian Foster 	struct xfs_trans	*tp = *tpp;
16072dd3d709SBrian Foster 
16082dd3d709SBrian Foster 	do {
16092dd3d709SBrian Foster 		error = xfs_bmbt_change_owner(tp, ip, XFS_DATA_FORK, ip->i_ino,
16102dd3d709SBrian Foster 					      NULL);
16112dd3d709SBrian Foster 		/* success or fatal error */
16122dd3d709SBrian Foster 		if (error != -EAGAIN)
16132dd3d709SBrian Foster 			break;
16142dd3d709SBrian Foster 
16152dd3d709SBrian Foster 		error = xfs_trans_roll(tpp);
16162dd3d709SBrian Foster 		if (error)
16172dd3d709SBrian Foster 			break;
16182dd3d709SBrian Foster 		tp = *tpp;
16192dd3d709SBrian Foster 
16202dd3d709SBrian Foster 		/*
16212dd3d709SBrian Foster 		 * Redirty both inodes so they can relog and keep the log tail
16222dd3d709SBrian Foster 		 * moving forward.
16232dd3d709SBrian Foster 		 */
16242dd3d709SBrian Foster 		xfs_trans_ijoin(tp, ip, 0);
16252dd3d709SBrian Foster 		xfs_trans_ijoin(tp, tmpip, 0);
16262dd3d709SBrian Foster 		xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
16272dd3d709SBrian Foster 		xfs_trans_log_inode(tp, tmpip, XFS_ILOG_CORE);
16282dd3d709SBrian Foster 	} while (true);
16292dd3d709SBrian Foster 
16302dd3d709SBrian Foster 	return error;
16312dd3d709SBrian Foster }
16322dd3d709SBrian Foster 
16334ef897a2SDave Chinner int
1634a133d952SDave Chinner xfs_swap_extents(
1635e06259aaSDarrick J. Wong 	struct xfs_inode	*ip,	/* target inode */
1636e06259aaSDarrick J. Wong 	struct xfs_inode	*tip,	/* tmp inode */
1637e06259aaSDarrick J. Wong 	struct xfs_swapext	*sxp)
1638a133d952SDave Chinner {
1639e06259aaSDarrick J. Wong 	struct xfs_mount	*mp = ip->i_mount;
1640e06259aaSDarrick J. Wong 	struct xfs_trans	*tp;
1641e06259aaSDarrick J. Wong 	struct xfs_bstat	*sbp = &sxp->sx_stat;
1642a133d952SDave Chinner 	int			src_log_flags, target_log_flags;
1643a133d952SDave Chinner 	int			error = 0;
1644c8ce540dSDarrick J. Wong 	uint64_t		f;
16452dd3d709SBrian Foster 	int			resblks = 0;
1646f74681baSBrian Foster 	unsigned int		flags = 0;
1647a133d952SDave Chinner 
1648a133d952SDave Chinner 	/*
1649723cac48SDave Chinner 	 * Lock the inodes against other IO, page faults and truncate to
1650723cac48SDave Chinner 	 * begin with.  Then we can ensure the inodes are flushed and have no
1651723cac48SDave Chinner 	 * page cache safely. Once we have done this we can take the ilocks and
1652723cac48SDave Chinner 	 * do the rest of the checks.
1653a133d952SDave Chinner 	 */
165465523218SChristoph Hellwig 	lock_two_nondirectories(VFS_I(ip), VFS_I(tip));
1655d2c292d8SJan Kara 	filemap_invalidate_lock_two(VFS_I(ip)->i_mapping,
1656d2c292d8SJan Kara 				    VFS_I(tip)->i_mapping);
1657a133d952SDave Chinner 
1658a133d952SDave Chinner 	/* Verify that both files have the same format */
1659c19b3b05SDave Chinner 	if ((VFS_I(ip)->i_mode & S_IFMT) != (VFS_I(tip)->i_mode & S_IFMT)) {
16602451337dSDave Chinner 		error = -EINVAL;
1661a133d952SDave Chinner 		goto out_unlock;
1662a133d952SDave Chinner 	}
1663a133d952SDave Chinner 
1664a133d952SDave Chinner 	/* Verify both files are either real-time or non-realtime */
1665a133d952SDave Chinner 	if (XFS_IS_REALTIME_INODE(ip) != XFS_IS_REALTIME_INODE(tip)) {
16662451337dSDave Chinner 		error = -EINVAL;
1667a133d952SDave Chinner 		goto out_unlock;
1668a133d952SDave Chinner 	}
1669a133d952SDave Chinner 
16702713fefaSDarrick J. Wong 	error = xfs_qm_dqattach(ip);
16712713fefaSDarrick J. Wong 	if (error)
16722713fefaSDarrick J. Wong 		goto out_unlock;
16732713fefaSDarrick J. Wong 
16742713fefaSDarrick J. Wong 	error = xfs_qm_dqattach(tip);
16752713fefaSDarrick J. Wong 	if (error)
16762713fefaSDarrick J. Wong 		goto out_unlock;
16772713fefaSDarrick J. Wong 
16784ef897a2SDave Chinner 	error = xfs_swap_extent_flush(ip);
1679a133d952SDave Chinner 	if (error)
1680a133d952SDave Chinner 		goto out_unlock;
16814ef897a2SDave Chinner 	error = xfs_swap_extent_flush(tip);
16824ef897a2SDave Chinner 	if (error)
16834ef897a2SDave Chinner 		goto out_unlock;
1684a133d952SDave Chinner 
168596987eeaSChristoph Hellwig 	if (xfs_inode_has_cow_data(tip)) {
168696987eeaSChristoph Hellwig 		error = xfs_reflink_cancel_cow_range(tip, 0, NULLFILEOFF, true);
168796987eeaSChristoph Hellwig 		if (error)
16888bc3b5e4SDarrick J. Wong 			goto out_unlock;
168996987eeaSChristoph Hellwig 	}
169096987eeaSChristoph Hellwig 
16911f08af52SDarrick J. Wong 	/*
16921f08af52SDarrick J. Wong 	 * Extent "swapping" with rmap requires a permanent reservation and
16931f08af52SDarrick J. Wong 	 * a block reservation because it's really just a remap operation
16941f08af52SDarrick J. Wong 	 * performed with log redo items!
16951f08af52SDarrick J. Wong 	 */
169638c26bfdSDave Chinner 	if (xfs_has_rmapbt(mp)) {
1697b3fed434SBrian Foster 		int		w = XFS_DATA_FORK;
1698daf83964SChristoph Hellwig 		uint32_t	ipnext = ip->i_df.if_nextents;
1699daf83964SChristoph Hellwig 		uint32_t	tipnext	= tip->i_df.if_nextents;
1700b3fed434SBrian Foster 
17011f08af52SDarrick J. Wong 		/*
1702b3fed434SBrian Foster 		 * Conceptually this shouldn't affect the shape of either bmbt,
1703b3fed434SBrian Foster 		 * but since we atomically move extents one by one, we reserve
1704b3fed434SBrian Foster 		 * enough space to rebuild both trees.
17051f08af52SDarrick J. Wong 		 */
1706b3fed434SBrian Foster 		resblks = XFS_SWAP_RMAP_SPACE_RES(mp, ipnext, w);
1707b3fed434SBrian Foster 		resblks +=  XFS_SWAP_RMAP_SPACE_RES(mp, tipnext, w);
1708b3fed434SBrian Foster 
1709b3fed434SBrian Foster 		/*
1710f74681baSBrian Foster 		 * If either inode straddles a bmapbt block allocation boundary,
1711f74681baSBrian Foster 		 * the rmapbt algorithm triggers repeated allocs and frees as
1712f74681baSBrian Foster 		 * extents are remapped. This can exhaust the block reservation
1713f74681baSBrian Foster 		 * prematurely and cause shutdown. Return freed blocks to the
1714f74681baSBrian Foster 		 * transaction reservation to counter this behavior.
1715b3fed434SBrian Foster 		 */
1716f74681baSBrian Foster 		flags |= XFS_TRANS_RES_FDBLKS;
17172dd3d709SBrian Foster 	}
1718f74681baSBrian Foster 	error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0, flags,
1719f74681baSBrian Foster 				&tp);
1720253f4911SChristoph Hellwig 	if (error)
1721a133d952SDave Chinner 		goto out_unlock;
1722723cac48SDave Chinner 
1723723cac48SDave Chinner 	/*
1724723cac48SDave Chinner 	 * Lock and join the inodes to the tansaction so that transaction commit
1725723cac48SDave Chinner 	 * or cancel will unlock the inodes from this point onwards.
1726723cac48SDave Chinner 	 */
17277c2d238aSDarrick J. Wong 	xfs_lock_two_inodes(ip, XFS_ILOCK_EXCL, tip, XFS_ILOCK_EXCL);
172839aff5fdSDarrick J. Wong 	xfs_trans_ijoin(tp, ip, 0);
172939aff5fdSDarrick J. Wong 	xfs_trans_ijoin(tp, tip, 0);
1730723cac48SDave Chinner 
1731a133d952SDave Chinner 
1732a133d952SDave Chinner 	/* Verify all data are being swapped */
1733a133d952SDave Chinner 	if (sxp->sx_offset != 0 ||
173413d2c10bSChristoph Hellwig 	    sxp->sx_length != ip->i_disk_size ||
173513d2c10bSChristoph Hellwig 	    sxp->sx_length != tip->i_disk_size) {
17362451337dSDave Chinner 		error = -EFAULT;
17374ef897a2SDave Chinner 		goto out_trans_cancel;
1738a133d952SDave Chinner 	}
1739a133d952SDave Chinner 
1740a133d952SDave Chinner 	trace_xfs_swap_extent_before(ip, 0);
1741a133d952SDave Chinner 	trace_xfs_swap_extent_before(tip, 1);
1742a133d952SDave Chinner 
1743a133d952SDave Chinner 	/* check inode formats now that data is flushed */
1744a133d952SDave Chinner 	error = xfs_swap_extents_check_format(ip, tip);
1745a133d952SDave Chinner 	if (error) {
1746a133d952SDave Chinner 		xfs_notice(mp,
1747a133d952SDave Chinner 		    "%s: inode 0x%llx format is incompatible for exchanging.",
1748a133d952SDave Chinner 				__func__, ip->i_ino);
17494ef897a2SDave Chinner 		goto out_trans_cancel;
1750a133d952SDave Chinner 	}
1751a133d952SDave Chinner 
1752a133d952SDave Chinner 	/*
1753a133d952SDave Chinner 	 * Compare the current change & modify times with that
1754a133d952SDave Chinner 	 * passed in.  If they differ, we abort this swap.
1755a133d952SDave Chinner 	 * This is the mechanism used to ensure the calling
1756a133d952SDave Chinner 	 * process that the file was not changed out from
1757a133d952SDave Chinner 	 * under it.
1758a133d952SDave Chinner 	 */
1759a133d952SDave Chinner 	if ((sbp->bs_ctime.tv_sec != VFS_I(ip)->i_ctime.tv_sec) ||
1760a133d952SDave Chinner 	    (sbp->bs_ctime.tv_nsec != VFS_I(ip)->i_ctime.tv_nsec) ||
1761a133d952SDave Chinner 	    (sbp->bs_mtime.tv_sec != VFS_I(ip)->i_mtime.tv_sec) ||
1762a133d952SDave Chinner 	    (sbp->bs_mtime.tv_nsec != VFS_I(ip)->i_mtime.tv_nsec)) {
17632451337dSDave Chinner 		error = -EBUSY;
176481217683SDave Chinner 		goto out_trans_cancel;
1765a133d952SDave Chinner 	}
1766a133d952SDave Chinner 
176721b5c978SDave Chinner 	/*
176821b5c978SDave Chinner 	 * Note the trickiness in setting the log flags - we set the owner log
176921b5c978SDave Chinner 	 * flag on the opposite inode (i.e. the inode we are setting the new
177021b5c978SDave Chinner 	 * owner to be) because once we swap the forks and log that, log
177121b5c978SDave Chinner 	 * recovery is going to see the fork as owned by the swapped inode,
177221b5c978SDave Chinner 	 * not the pre-swapped inodes.
177321b5c978SDave Chinner 	 */
177421b5c978SDave Chinner 	src_log_flags = XFS_ILOG_CORE;
177521b5c978SDave Chinner 	target_log_flags = XFS_ILOG_CORE;
177639aff5fdSDarrick J. Wong 
177738c26bfdSDave Chinner 	if (xfs_has_rmapbt(mp))
17781f08af52SDarrick J. Wong 		error = xfs_swap_extent_rmap(&tp, ip, tip);
17791f08af52SDarrick J. Wong 	else
178039aff5fdSDarrick J. Wong 		error = xfs_swap_extent_forks(tp, ip, tip, &src_log_flags,
178139aff5fdSDarrick J. Wong 				&target_log_flags);
178221b5c978SDave Chinner 	if (error)
178321b5c978SDave Chinner 		goto out_trans_cancel;
1784a133d952SDave Chinner 
1785f0bc4d13SDarrick J. Wong 	/* Do we have to swap reflink flags? */
17863e09ab8fSChristoph Hellwig 	if ((ip->i_diflags2 & XFS_DIFLAG2_REFLINK) ^
17873e09ab8fSChristoph Hellwig 	    (tip->i_diflags2 & XFS_DIFLAG2_REFLINK)) {
17883e09ab8fSChristoph Hellwig 		f = ip->i_diflags2 & XFS_DIFLAG2_REFLINK;
17893e09ab8fSChristoph Hellwig 		ip->i_diflags2 &= ~XFS_DIFLAG2_REFLINK;
17903e09ab8fSChristoph Hellwig 		ip->i_diflags2 |= tip->i_diflags2 & XFS_DIFLAG2_REFLINK;
17913e09ab8fSChristoph Hellwig 		tip->i_diflags2 &= ~XFS_DIFLAG2_REFLINK;
17923e09ab8fSChristoph Hellwig 		tip->i_diflags2 |= f & XFS_DIFLAG2_REFLINK;
179352bfcdd7SDarrick J. Wong 	}
179452bfcdd7SDarrick J. Wong 
179552bfcdd7SDarrick J. Wong 	/* Swap the cow forks. */
179638c26bfdSDave Chinner 	if (xfs_has_reflink(mp)) {
1797f7e67b20SChristoph Hellwig 		ASSERT(!ip->i_cowfp ||
1798f7e67b20SChristoph Hellwig 		       ip->i_cowfp->if_format == XFS_DINODE_FMT_EXTENTS);
1799f7e67b20SChristoph Hellwig 		ASSERT(!tip->i_cowfp ||
1800f7e67b20SChristoph Hellwig 		       tip->i_cowfp->if_format == XFS_DINODE_FMT_EXTENTS);
180152bfcdd7SDarrick J. Wong 
1802897992b7SGustavo A. R. Silva 		swap(ip->i_cowfp, tip->i_cowfp);
180352bfcdd7SDarrick J. Wong 
18045bcffe30SChristoph Hellwig 		if (ip->i_cowfp && ip->i_cowfp->if_bytes)
180583104d44SDarrick J. Wong 			xfs_inode_set_cowblocks_tag(ip);
180652bfcdd7SDarrick J. Wong 		else
180752bfcdd7SDarrick J. Wong 			xfs_inode_clear_cowblocks_tag(ip);
18085bcffe30SChristoph Hellwig 		if (tip->i_cowfp && tip->i_cowfp->if_bytes)
180983104d44SDarrick J. Wong 			xfs_inode_set_cowblocks_tag(tip);
181052bfcdd7SDarrick J. Wong 		else
181152bfcdd7SDarrick J. Wong 			xfs_inode_clear_cowblocks_tag(tip);
1812f0bc4d13SDarrick J. Wong 	}
1813f0bc4d13SDarrick J. Wong 
1814a133d952SDave Chinner 	xfs_trans_log_inode(tp, ip,  src_log_flags);
1815a133d952SDave Chinner 	xfs_trans_log_inode(tp, tip, target_log_flags);
1816a133d952SDave Chinner 
1817a133d952SDave Chinner 	/*
18186fb10d6dSBrian Foster 	 * The extent forks have been swapped, but crc=1,rmapbt=0 filesystems
18196fb10d6dSBrian Foster 	 * have inode number owner values in the bmbt blocks that still refer to
18206fb10d6dSBrian Foster 	 * the old inode. Scan each bmbt to fix up the owner values with the
18216fb10d6dSBrian Foster 	 * inode number of the current inode.
18226fb10d6dSBrian Foster 	 */
18236fb10d6dSBrian Foster 	if (src_log_flags & XFS_ILOG_DOWNER) {
18242dd3d709SBrian Foster 		error = xfs_swap_change_owner(&tp, ip, tip);
18256fb10d6dSBrian Foster 		if (error)
18266fb10d6dSBrian Foster 			goto out_trans_cancel;
18276fb10d6dSBrian Foster 	}
18286fb10d6dSBrian Foster 	if (target_log_flags & XFS_ILOG_DOWNER) {
18292dd3d709SBrian Foster 		error = xfs_swap_change_owner(&tp, tip, ip);
18306fb10d6dSBrian Foster 		if (error)
18316fb10d6dSBrian Foster 			goto out_trans_cancel;
18326fb10d6dSBrian Foster 	}
18336fb10d6dSBrian Foster 
18346fb10d6dSBrian Foster 	/*
1835a133d952SDave Chinner 	 * If this is a synchronous mount, make sure that the
1836a133d952SDave Chinner 	 * transaction goes to disk before returning to the user.
1837a133d952SDave Chinner 	 */
18380560f31aSDave Chinner 	if (xfs_has_wsync(mp))
1839a133d952SDave Chinner 		xfs_trans_set_sync(tp);
1840a133d952SDave Chinner 
184170393313SChristoph Hellwig 	error = xfs_trans_commit(tp);
1842a133d952SDave Chinner 
1843a133d952SDave Chinner 	trace_xfs_swap_extent_after(ip, 0);
1844a133d952SDave Chinner 	trace_xfs_swap_extent_after(tip, 1);
184539aff5fdSDarrick J. Wong 
1846d2c292d8SJan Kara out_unlock_ilock:
1847d2c292d8SJan Kara 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
1848d2c292d8SJan Kara 	xfs_iunlock(tip, XFS_ILOCK_EXCL);
184965523218SChristoph Hellwig out_unlock:
1850d2c292d8SJan Kara 	filemap_invalidate_unlock_two(VFS_I(ip)->i_mapping,
1851d2c292d8SJan Kara 				      VFS_I(tip)->i_mapping);
185265523218SChristoph Hellwig 	unlock_two_nondirectories(VFS_I(ip), VFS_I(tip));
1853a133d952SDave Chinner 	return error;
1854a133d952SDave Chinner 
185539aff5fdSDarrick J. Wong out_trans_cancel:
185639aff5fdSDarrick J. Wong 	xfs_trans_cancel(tp);
1857d2c292d8SJan Kara 	goto out_unlock_ilock;
1858a133d952SDave Chinner }
1859