xref: /linux/fs/xfs/libxfs/xfs_bmap.c (revision 8751b21ad9dc33f31dff20297dcae2063cbbcfc9)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2000-2006 Silicon Graphics, Inc.
4  * All Rights Reserved.
5  */
6 #include "xfs.h"
7 #include "xfs_fs.h"
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_log_format.h"
11 #include "xfs_trans_resv.h"
12 #include "xfs_bit.h"
13 #include "xfs_sb.h"
14 #include "xfs_mount.h"
15 #include "xfs_defer.h"
16 #include "xfs_dir2.h"
17 #include "xfs_inode.h"
18 #include "xfs_btree.h"
19 #include "xfs_trans.h"
20 #include "xfs_alloc.h"
21 #include "xfs_bmap.h"
22 #include "xfs_bmap_util.h"
23 #include "xfs_bmap_btree.h"
24 #include "xfs_rtbitmap.h"
25 #include "xfs_errortag.h"
26 #include "xfs_error.h"
27 #include "xfs_quota.h"
28 #include "xfs_trans_space.h"
29 #include "xfs_buf_item.h"
30 #include "xfs_trace.h"
31 #include "xfs_attr_leaf.h"
32 #include "xfs_filestream.h"
33 #include "xfs_rmap.h"
34 #include "xfs_ag.h"
35 #include "xfs_ag_resv.h"
36 #include "xfs_refcount.h"
37 #include "xfs_icache.h"
38 #include "xfs_iomap.h"
39 #include "xfs_health.h"
40 #include "xfs_bmap_item.h"
41 #include "xfs_symlink_remote.h"
42 #include "xfs_inode_util.h"
43 
44 struct kmem_cache		*xfs_bmap_intent_cache;
45 
46 /*
47  * Miscellaneous helper functions
48  */
49 
50 /*
51  * Compute and fill in the value of the maximum depth of a bmap btree
52  * in this filesystem.  Done once, during mount.
53  */
54 void
55 xfs_bmap_compute_maxlevels(
56 	xfs_mount_t	*mp,		/* file system mount structure */
57 	int		whichfork)	/* data or attr fork */
58 {
59 	uint64_t	maxblocks;	/* max blocks at this level */
60 	xfs_extnum_t	maxleafents;	/* max leaf entries possible */
61 	int		level;		/* btree level */
62 	int		maxrootrecs;	/* max records in root block */
63 	int		minleafrecs;	/* min records in leaf block */
64 	int		minnoderecs;	/* min records in node block */
65 	int		sz;		/* root block size */
66 
67 	/*
68 	 * The maximum number of extents in a fork, hence the maximum number of
69 	 * leaf entries, is controlled by the size of the on-disk extent count.
70 	 *
71 	 * Note that we can no longer assume that if we are in ATTR1 that the
72 	 * fork offset of all the inodes will be
73 	 * (xfs_default_attroffset(ip) >> 3) because we could have mounted with
74 	 * ATTR2 and then mounted back with ATTR1, keeping the i_forkoff's fixed
75 	 * but probably at various positions. Therefore, for both ATTR1 and
76 	 * ATTR2 we have to assume the worst case scenario of a minimum size
77 	 * available.
78 	 */
79 	maxleafents = xfs_iext_max_nextents(xfs_has_large_extent_counts(mp),
80 				whichfork);
81 	if (whichfork == XFS_DATA_FORK)
82 		sz = xfs_bmdr_space_calc(MINDBTPTRS);
83 	else
84 		sz = xfs_bmdr_space_calc(MINABTPTRS);
85 
86 	maxrootrecs = xfs_bmdr_maxrecs(sz, 0);
87 	minleafrecs = mp->m_bmap_dmnr[0];
88 	minnoderecs = mp->m_bmap_dmnr[1];
89 	maxblocks = howmany_64(maxleafents, minleafrecs);
90 	for (level = 1; maxblocks > 1; level++) {
91 		if (maxblocks <= maxrootrecs)
92 			maxblocks = 1;
93 		else
94 			maxblocks = howmany_64(maxblocks, minnoderecs);
95 	}
96 	mp->m_bm_maxlevels[whichfork] = level;
97 	ASSERT(mp->m_bm_maxlevels[whichfork] <= xfs_bmbt_maxlevels_ondisk());
98 }
99 
100 unsigned int
101 xfs_bmap_compute_attr_offset(
102 	struct xfs_mount	*mp)
103 {
104 	if (mp->m_sb.sb_inodesize == 256)
105 		return XFS_LITINO(mp) - xfs_bmdr_space_calc(MINABTPTRS);
106 	return xfs_bmdr_space_calc(6 * MINABTPTRS);
107 }
108 
109 STATIC int				/* error */
110 xfs_bmbt_lookup_eq(
111 	struct xfs_btree_cur	*cur,
112 	struct xfs_bmbt_irec	*irec,
113 	int			*stat)	/* success/failure */
114 {
115 	cur->bc_rec.b = *irec;
116 	return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
117 }
118 
119 STATIC int				/* error */
120 xfs_bmbt_lookup_first(
121 	struct xfs_btree_cur	*cur,
122 	int			*stat)	/* success/failure */
123 {
124 	cur->bc_rec.b.br_startoff = 0;
125 	cur->bc_rec.b.br_startblock = 0;
126 	cur->bc_rec.b.br_blockcount = 0;
127 	return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
128 }
129 
130 /*
131  * Check if the inode needs to be converted to btree format.
132  */
133 static inline bool xfs_bmap_needs_btree(struct xfs_inode *ip, int whichfork)
134 {
135 	struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
136 
137 	return whichfork != XFS_COW_FORK &&
138 		ifp->if_format == XFS_DINODE_FMT_EXTENTS &&
139 		ifp->if_nextents > XFS_IFORK_MAXEXT(ip, whichfork);
140 }
141 
142 /*
143  * Check if the inode should be converted to extent format.
144  */
145 static inline bool xfs_bmap_wants_extents(struct xfs_inode *ip, int whichfork)
146 {
147 	struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
148 
149 	return whichfork != XFS_COW_FORK &&
150 		ifp->if_format == XFS_DINODE_FMT_BTREE &&
151 		ifp->if_nextents <= XFS_IFORK_MAXEXT(ip, whichfork);
152 }
153 
154 /*
155  * Update the record referred to by cur to the value given by irec
156  * This either works (return 0) or gets an EFSCORRUPTED error.
157  */
158 STATIC int
159 xfs_bmbt_update(
160 	struct xfs_btree_cur	*cur,
161 	struct xfs_bmbt_irec	*irec)
162 {
163 	union xfs_btree_rec	rec;
164 
165 	xfs_bmbt_disk_set_all(&rec.bmbt, irec);
166 	return xfs_btree_update(cur, &rec);
167 }
168 
169 /*
170  * Compute the worst-case number of indirect blocks that will be used
171  * for ip's delayed extent of length "len".
172  */
173 STATIC xfs_filblks_t
174 xfs_bmap_worst_indlen(
175 	xfs_inode_t	*ip,		/* incore inode pointer */
176 	xfs_filblks_t	len)		/* delayed extent length */
177 {
178 	int		level;		/* btree level number */
179 	int		maxrecs;	/* maximum record count at this level */
180 	xfs_mount_t	*mp;		/* mount structure */
181 	xfs_filblks_t	rval;		/* return value */
182 
183 	mp = ip->i_mount;
184 	maxrecs = mp->m_bmap_dmxr[0];
185 	for (level = 0, rval = 0;
186 	     level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
187 	     level++) {
188 		len += maxrecs - 1;
189 		do_div(len, maxrecs);
190 		rval += len;
191 		if (len == 1)
192 			return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
193 				level - 1;
194 		if (level == 0)
195 			maxrecs = mp->m_bmap_dmxr[1];
196 	}
197 	return rval;
198 }
199 
200 /*
201  * Calculate the default attribute fork offset for newly created inodes.
202  */
203 uint
204 xfs_default_attroffset(
205 	struct xfs_inode	*ip)
206 {
207 	if (ip->i_df.if_format == XFS_DINODE_FMT_DEV)
208 		return roundup(sizeof(xfs_dev_t), 8);
209 	return M_IGEO(ip->i_mount)->attr_fork_offset;
210 }
211 
212 /*
213  * Helper routine to reset inode i_forkoff field when switching attribute fork
214  * from local to extent format - we reset it where possible to make space
215  * available for inline data fork extents.
216  */
217 STATIC void
218 xfs_bmap_forkoff_reset(
219 	xfs_inode_t	*ip,
220 	int		whichfork)
221 {
222 	if (whichfork == XFS_ATTR_FORK &&
223 	    ip->i_df.if_format != XFS_DINODE_FMT_DEV &&
224 	    ip->i_df.if_format != XFS_DINODE_FMT_BTREE) {
225 		uint	dfl_forkoff = xfs_default_attroffset(ip) >> 3;
226 
227 		if (dfl_forkoff > ip->i_forkoff)
228 			ip->i_forkoff = dfl_forkoff;
229 	}
230 }
231 
232 static int
233 xfs_bmap_read_buf(
234 	struct xfs_mount	*mp,		/* file system mount point */
235 	struct xfs_trans	*tp,		/* transaction pointer */
236 	xfs_fsblock_t		fsbno,		/* file system block number */
237 	struct xfs_buf		**bpp)		/* buffer for fsbno */
238 {
239 	struct xfs_buf		*bp;		/* return value */
240 	int			error;
241 
242 	if (!xfs_verify_fsbno(mp, fsbno))
243 		return -EFSCORRUPTED;
244 	error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
245 			XFS_FSB_TO_DADDR(mp, fsbno), mp->m_bsize, 0, &bp,
246 			&xfs_bmbt_buf_ops);
247 	if (!error) {
248 		xfs_buf_set_ref(bp, XFS_BMAP_BTREE_REF);
249 		*bpp = bp;
250 	}
251 	return error;
252 }
253 
254 #ifdef DEBUG
255 STATIC struct xfs_buf *
256 xfs_bmap_get_bp(
257 	struct xfs_btree_cur	*cur,
258 	xfs_fsblock_t		bno)
259 {
260 	struct xfs_log_item	*lip;
261 	int			i;
262 
263 	if (!cur)
264 		return NULL;
265 
266 	for (i = 0; i < cur->bc_maxlevels; i++) {
267 		if (!cur->bc_levels[i].bp)
268 			break;
269 		if (xfs_buf_daddr(cur->bc_levels[i].bp) == bno)
270 			return cur->bc_levels[i].bp;
271 	}
272 
273 	/* Chase down all the log items to see if the bp is there */
274 	list_for_each_entry(lip, &cur->bc_tp->t_items, li_trans) {
275 		struct xfs_buf_log_item	*bip = (struct xfs_buf_log_item *)lip;
276 
277 		if (bip->bli_item.li_type == XFS_LI_BUF &&
278 		    xfs_buf_daddr(bip->bli_buf) == bno)
279 			return bip->bli_buf;
280 	}
281 
282 	return NULL;
283 }
284 
285 STATIC void
286 xfs_check_block(
287 	struct xfs_btree_block	*block,
288 	xfs_mount_t		*mp,
289 	int			root,
290 	short			sz)
291 {
292 	int			i, j, dmxr;
293 	__be64			*pp, *thispa;	/* pointer to block address */
294 	xfs_bmbt_key_t		*prevp, *keyp;
295 
296 	ASSERT(be16_to_cpu(block->bb_level) > 0);
297 
298 	prevp = NULL;
299 	for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
300 		dmxr = mp->m_bmap_dmxr[0];
301 		keyp = xfs_bmbt_key_addr(mp, block, i);
302 
303 		if (prevp) {
304 			ASSERT(be64_to_cpu(prevp->br_startoff) <
305 			       be64_to_cpu(keyp->br_startoff));
306 		}
307 		prevp = keyp;
308 
309 		/*
310 		 * Compare the block numbers to see if there are dups.
311 		 */
312 		if (root)
313 			pp = xfs_bmap_broot_ptr_addr(mp, block, i, sz);
314 		else
315 			pp = xfs_bmbt_ptr_addr(mp, block, i, dmxr);
316 
317 		for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
318 			if (root)
319 				thispa = xfs_bmap_broot_ptr_addr(mp, block, j, sz);
320 			else
321 				thispa = xfs_bmbt_ptr_addr(mp, block, j, dmxr);
322 			if (*thispa == *pp) {
323 				xfs_warn(mp, "%s: thispa(%d) == pp(%d) %lld",
324 					__func__, j, i,
325 					(unsigned long long)be64_to_cpu(*thispa));
326 				xfs_err(mp, "%s: ptrs are equal in node\n",
327 					__func__);
328 				xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
329 			}
330 		}
331 	}
332 }
333 
334 /*
335  * Check that the extents for the inode ip are in the right order in all
336  * btree leaves. THis becomes prohibitively expensive for large extent count
337  * files, so don't bother with inodes that have more than 10,000 extents in
338  * them. The btree record ordering checks will still be done, so for such large
339  * bmapbt constructs that is going to catch most corruptions.
340  */
341 STATIC void
342 xfs_bmap_check_leaf_extents(
343 	struct xfs_btree_cur	*cur,	/* btree cursor or null */
344 	xfs_inode_t		*ip,		/* incore inode pointer */
345 	int			whichfork)	/* data or attr fork */
346 {
347 	struct xfs_mount	*mp = ip->i_mount;
348 	struct xfs_ifork	*ifp = xfs_ifork_ptr(ip, whichfork);
349 	struct xfs_btree_block	*block;	/* current btree block */
350 	xfs_fsblock_t		bno;	/* block # of "block" */
351 	struct xfs_buf		*bp;	/* buffer for "block" */
352 	int			error;	/* error return value */
353 	xfs_extnum_t		i=0, j;	/* index into the extents list */
354 	int			level;	/* btree level, for checking */
355 	__be64			*pp;	/* pointer to block address */
356 	xfs_bmbt_rec_t		*ep;	/* pointer to current extent */
357 	xfs_bmbt_rec_t		last = {0, 0}; /* last extent in prev block */
358 	xfs_bmbt_rec_t		*nextp;	/* pointer to next extent */
359 	int			bp_release = 0;
360 
361 	if (ifp->if_format != XFS_DINODE_FMT_BTREE)
362 		return;
363 
364 	/* skip large extent count inodes */
365 	if (ip->i_df.if_nextents > 10000)
366 		return;
367 
368 	bno = NULLFSBLOCK;
369 	block = ifp->if_broot;
370 	/*
371 	 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
372 	 */
373 	level = be16_to_cpu(block->bb_level);
374 	ASSERT(level > 0);
375 	xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
376 	pp = xfs_bmap_broot_ptr_addr(mp, block, 1, ifp->if_broot_bytes);
377 	bno = be64_to_cpu(*pp);
378 
379 	ASSERT(bno != NULLFSBLOCK);
380 	ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
381 	ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
382 
383 	/*
384 	 * Go down the tree until leaf level is reached, following the first
385 	 * pointer (leftmost) at each level.
386 	 */
387 	while (level-- > 0) {
388 		/* See if buf is in cur first */
389 		bp_release = 0;
390 		bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
391 		if (!bp) {
392 			bp_release = 1;
393 			error = xfs_bmap_read_buf(mp, NULL, bno, &bp);
394 			if (xfs_metadata_is_sick(error))
395 				xfs_btree_mark_sick(cur);
396 			if (error)
397 				goto error_norelse;
398 		}
399 		block = XFS_BUF_TO_BLOCK(bp);
400 		if (level == 0)
401 			break;
402 
403 		/*
404 		 * Check this block for basic sanity (increasing keys and
405 		 * no duplicate blocks).
406 		 */
407 
408 		xfs_check_block(block, mp, 0, 0);
409 		pp = xfs_bmbt_ptr_addr(mp, block, 1, mp->m_bmap_dmxr[1]);
410 		bno = be64_to_cpu(*pp);
411 		if (XFS_IS_CORRUPT(mp, !xfs_verify_fsbno(mp, bno))) {
412 			xfs_btree_mark_sick(cur);
413 			error = -EFSCORRUPTED;
414 			goto error0;
415 		}
416 		if (bp_release) {
417 			bp_release = 0;
418 			xfs_trans_brelse(NULL, bp);
419 		}
420 	}
421 
422 	/*
423 	 * Here with bp and block set to the leftmost leaf node in the tree.
424 	 */
425 	i = 0;
426 
427 	/*
428 	 * Loop over all leaf nodes checking that all extents are in the right order.
429 	 */
430 	for (;;) {
431 		xfs_fsblock_t	nextbno;
432 		xfs_extnum_t	num_recs;
433 
434 
435 		num_recs = xfs_btree_get_numrecs(block);
436 
437 		/*
438 		 * Read-ahead the next leaf block, if any.
439 		 */
440 
441 		nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
442 
443 		/*
444 		 * Check all the extents to make sure they are OK.
445 		 * If we had a previous block, the last entry should
446 		 * conform with the first entry in this one.
447 		 */
448 
449 		ep = xfs_bmbt_rec_addr(mp, block, 1);
450 		if (i) {
451 			ASSERT(xfs_bmbt_disk_get_startoff(&last) +
452 			       xfs_bmbt_disk_get_blockcount(&last) <=
453 			       xfs_bmbt_disk_get_startoff(ep));
454 		}
455 		for (j = 1; j < num_recs; j++) {
456 			nextp = xfs_bmbt_rec_addr(mp, block, j + 1);
457 			ASSERT(xfs_bmbt_disk_get_startoff(ep) +
458 			       xfs_bmbt_disk_get_blockcount(ep) <=
459 			       xfs_bmbt_disk_get_startoff(nextp));
460 			ep = nextp;
461 		}
462 
463 		last = *ep;
464 		i += num_recs;
465 		if (bp_release) {
466 			bp_release = 0;
467 			xfs_trans_brelse(NULL, bp);
468 		}
469 		bno = nextbno;
470 		/*
471 		 * If we've reached the end, stop.
472 		 */
473 		if (bno == NULLFSBLOCK)
474 			break;
475 
476 		bp_release = 0;
477 		bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
478 		if (!bp) {
479 			bp_release = 1;
480 			error = xfs_bmap_read_buf(mp, NULL, bno, &bp);
481 			if (xfs_metadata_is_sick(error))
482 				xfs_btree_mark_sick(cur);
483 			if (error)
484 				goto error_norelse;
485 		}
486 		block = XFS_BUF_TO_BLOCK(bp);
487 	}
488 
489 	return;
490 
491 error0:
492 	xfs_warn(mp, "%s: at error0", __func__);
493 	if (bp_release)
494 		xfs_trans_brelse(NULL, bp);
495 error_norelse:
496 	xfs_warn(mp, "%s: BAD after btree leaves for %llu extents",
497 		__func__, i);
498 	xfs_err(mp, "%s: CORRUPTED BTREE OR SOMETHING", __func__);
499 	xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
500 	return;
501 }
502 
503 /*
504  * Validate that the bmbt_irecs being returned from bmapi are valid
505  * given the caller's original parameters.  Specifically check the
506  * ranges of the returned irecs to ensure that they only extend beyond
507  * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
508  */
509 STATIC void
510 xfs_bmap_validate_ret(
511 	xfs_fileoff_t		bno,
512 	xfs_filblks_t		len,
513 	uint32_t		flags,
514 	xfs_bmbt_irec_t		*mval,
515 	int			nmap,
516 	int			ret_nmap)
517 {
518 	int			i;		/* index to map values */
519 
520 	ASSERT(ret_nmap <= nmap);
521 
522 	for (i = 0; i < ret_nmap; i++) {
523 		ASSERT(mval[i].br_blockcount > 0);
524 		if (!(flags & XFS_BMAPI_ENTIRE)) {
525 			ASSERT(mval[i].br_startoff >= bno);
526 			ASSERT(mval[i].br_blockcount <= len);
527 			ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
528 			       bno + len);
529 		} else {
530 			ASSERT(mval[i].br_startoff < bno + len);
531 			ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
532 			       bno);
533 		}
534 		ASSERT(i == 0 ||
535 		       mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
536 		       mval[i].br_startoff);
537 		ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
538 		       mval[i].br_startblock != HOLESTARTBLOCK);
539 		ASSERT(mval[i].br_state == XFS_EXT_NORM ||
540 		       mval[i].br_state == XFS_EXT_UNWRITTEN);
541 	}
542 }
543 
544 #else
545 #define xfs_bmap_check_leaf_extents(cur, ip, whichfork)		do { } while (0)
546 #define	xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)	do { } while (0)
547 #endif /* DEBUG */
548 
549 /*
550  * Inode fork format manipulation functions
551  */
552 
553 /*
554  * Convert the inode format to extent format if it currently is in btree format,
555  * but the extent list is small enough that it fits into the extent format.
556  *
557  * Since the extents are already in-core, all we have to do is give up the space
558  * for the btree root and pitch the leaf block.
559  */
560 STATIC int				/* error */
561 xfs_bmap_btree_to_extents(
562 	struct xfs_trans	*tp,	/* transaction pointer */
563 	struct xfs_inode	*ip,	/* incore inode pointer */
564 	struct xfs_btree_cur	*cur,	/* btree cursor */
565 	int			*logflagsp, /* inode logging flags */
566 	int			whichfork)  /* data or attr fork */
567 {
568 	struct xfs_ifork	*ifp = xfs_ifork_ptr(ip, whichfork);
569 	struct xfs_mount	*mp = ip->i_mount;
570 	struct xfs_btree_block	*rblock = ifp->if_broot;
571 	struct xfs_btree_block	*cblock;/* child btree block */
572 	xfs_fsblock_t		cbno;	/* child block number */
573 	struct xfs_buf		*cbp;	/* child block's buffer */
574 	int			error;	/* error return value */
575 	__be64			*pp;	/* ptr to block address */
576 	struct xfs_owner_info	oinfo;
577 
578 	/* check if we actually need the extent format first: */
579 	if (!xfs_bmap_wants_extents(ip, whichfork))
580 		return 0;
581 
582 	ASSERT(cur);
583 	ASSERT(whichfork != XFS_COW_FORK);
584 	ASSERT(ifp->if_format == XFS_DINODE_FMT_BTREE);
585 	ASSERT(be16_to_cpu(rblock->bb_level) == 1);
586 	ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
587 	ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, false) == 1);
588 
589 	pp = xfs_bmap_broot_ptr_addr(mp, rblock, 1, ifp->if_broot_bytes);
590 	cbno = be64_to_cpu(*pp);
591 #ifdef DEBUG
592 	if (XFS_IS_CORRUPT(cur->bc_mp, !xfs_verify_fsbno(mp, cbno))) {
593 		xfs_btree_mark_sick(cur);
594 		return -EFSCORRUPTED;
595 	}
596 #endif
597 	error = xfs_bmap_read_buf(mp, tp, cbno, &cbp);
598 	if (xfs_metadata_is_sick(error))
599 		xfs_btree_mark_sick(cur);
600 	if (error)
601 		return error;
602 	cblock = XFS_BUF_TO_BLOCK(cbp);
603 	if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
604 		return error;
605 
606 	xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, whichfork);
607 	error = xfs_free_extent_later(cur->bc_tp, cbno, 1, &oinfo,
608 			XFS_AG_RESV_NONE, 0);
609 	if (error)
610 		return error;
611 
612 	ip->i_nblocks--;
613 	xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
614 	xfs_trans_binval(tp, cbp);
615 	if (cur->bc_levels[0].bp == cbp)
616 		cur->bc_levels[0].bp = NULL;
617 	xfs_iroot_realloc(ip, -1, whichfork);
618 	ASSERT(ifp->if_broot == NULL);
619 	ifp->if_format = XFS_DINODE_FMT_EXTENTS;
620 	*logflagsp |= XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
621 	return 0;
622 }
623 
624 /*
625  * Convert an extents-format file into a btree-format file.
626  * The new file will have a root block (in the inode) and a single child block.
627  */
628 STATIC int					/* error */
629 xfs_bmap_extents_to_btree(
630 	struct xfs_trans	*tp,		/* transaction pointer */
631 	struct xfs_inode	*ip,		/* incore inode pointer */
632 	struct xfs_btree_cur	**curp,		/* cursor returned to caller */
633 	int			wasdel,		/* converting a delayed alloc */
634 	int			*logflagsp,	/* inode logging flags */
635 	int			whichfork)	/* data or attr fork */
636 {
637 	struct xfs_btree_block	*ablock;	/* allocated (child) bt block */
638 	struct xfs_buf		*abp;		/* buffer for ablock */
639 	struct xfs_alloc_arg	args;		/* allocation arguments */
640 	struct xfs_bmbt_rec	*arp;		/* child record pointer */
641 	struct xfs_btree_block	*block;		/* btree root block */
642 	struct xfs_btree_cur	*cur;		/* bmap btree cursor */
643 	int			error;		/* error return value */
644 	struct xfs_ifork	*ifp;		/* inode fork pointer */
645 	struct xfs_bmbt_key	*kp;		/* root block key pointer */
646 	struct xfs_mount	*mp;		/* mount structure */
647 	xfs_bmbt_ptr_t		*pp;		/* root block address pointer */
648 	struct xfs_iext_cursor	icur;
649 	struct xfs_bmbt_irec	rec;
650 	xfs_extnum_t		cnt = 0;
651 
652 	mp = ip->i_mount;
653 	ASSERT(whichfork != XFS_COW_FORK);
654 	ifp = xfs_ifork_ptr(ip, whichfork);
655 	ASSERT(ifp->if_format == XFS_DINODE_FMT_EXTENTS);
656 
657 	/*
658 	 * Make space in the inode incore. This needs to be undone if we fail
659 	 * to expand the root.
660 	 */
661 	xfs_iroot_realloc(ip, 1, whichfork);
662 
663 	/*
664 	 * Fill in the root.
665 	 */
666 	block = ifp->if_broot;
667 	xfs_bmbt_init_block(ip, block, NULL, 1, 1);
668 	/*
669 	 * Need a cursor.  Can't allocate until bb_level is filled in.
670 	 */
671 	cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
672 	if (wasdel)
673 		cur->bc_flags |= XFS_BTREE_BMBT_WASDEL;
674 	/*
675 	 * Convert to a btree with two levels, one record in root.
676 	 */
677 	ifp->if_format = XFS_DINODE_FMT_BTREE;
678 	memset(&args, 0, sizeof(args));
679 	args.tp = tp;
680 	args.mp = mp;
681 	xfs_rmap_ino_bmbt_owner(&args.oinfo, ip->i_ino, whichfork);
682 
683 	args.minlen = args.maxlen = args.prod = 1;
684 	args.wasdel = wasdel;
685 	*logflagsp = 0;
686 	error = xfs_alloc_vextent_start_ag(&args,
687 				XFS_INO_TO_FSB(mp, ip->i_ino));
688 	if (error)
689 		goto out_root_realloc;
690 
691 	/*
692 	 * Allocation can't fail, the space was reserved.
693 	 */
694 	if (WARN_ON_ONCE(args.fsbno == NULLFSBLOCK)) {
695 		error = -ENOSPC;
696 		goto out_root_realloc;
697 	}
698 
699 	cur->bc_bmap.allocated++;
700 	ip->i_nblocks++;
701 	xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
702 	error = xfs_trans_get_buf(tp, mp->m_ddev_targp,
703 			XFS_FSB_TO_DADDR(mp, args.fsbno),
704 			mp->m_bsize, 0, &abp);
705 	if (error)
706 		goto out_unreserve_dquot;
707 
708 	/*
709 	 * Fill in the child block.
710 	 */
711 	ablock = XFS_BUF_TO_BLOCK(abp);
712 	xfs_bmbt_init_block(ip, ablock, abp, 0, 0);
713 
714 	for_each_xfs_iext(ifp, &icur, &rec) {
715 		if (isnullstartblock(rec.br_startblock))
716 			continue;
717 		arp = xfs_bmbt_rec_addr(mp, ablock, 1 + cnt);
718 		xfs_bmbt_disk_set_all(arp, &rec);
719 		cnt++;
720 	}
721 	ASSERT(cnt == ifp->if_nextents);
722 	xfs_btree_set_numrecs(ablock, cnt);
723 
724 	/*
725 	 * Fill in the root key and pointer.
726 	 */
727 	kp = xfs_bmbt_key_addr(mp, block, 1);
728 	arp = xfs_bmbt_rec_addr(mp, ablock, 1);
729 	kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
730 	pp = xfs_bmbt_ptr_addr(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
731 						be16_to_cpu(block->bb_level)));
732 	*pp = cpu_to_be64(args.fsbno);
733 
734 	/*
735 	 * Do all this logging at the end so that
736 	 * the root is at the right level.
737 	 */
738 	xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
739 	xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
740 	ASSERT(*curp == NULL);
741 	*curp = cur;
742 	*logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
743 	return 0;
744 
745 out_unreserve_dquot:
746 	xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
747 out_root_realloc:
748 	xfs_iroot_realloc(ip, -1, whichfork);
749 	ifp->if_format = XFS_DINODE_FMT_EXTENTS;
750 	ASSERT(ifp->if_broot == NULL);
751 	xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
752 
753 	return error;
754 }
755 
756 /*
757  * Convert a local file to an extents file.
758  * This code is out of bounds for data forks of regular files,
759  * since the file data needs to get logged so things will stay consistent.
760  * (The bmap-level manipulations are ok, though).
761  */
762 void
763 xfs_bmap_local_to_extents_empty(
764 	struct xfs_trans	*tp,
765 	struct xfs_inode	*ip,
766 	int			whichfork)
767 {
768 	struct xfs_ifork	*ifp = xfs_ifork_ptr(ip, whichfork);
769 
770 	ASSERT(whichfork != XFS_COW_FORK);
771 	ASSERT(ifp->if_format == XFS_DINODE_FMT_LOCAL);
772 	ASSERT(ifp->if_bytes == 0);
773 	ASSERT(ifp->if_nextents == 0);
774 
775 	xfs_bmap_forkoff_reset(ip, whichfork);
776 	ifp->if_data = NULL;
777 	ifp->if_height = 0;
778 	ifp->if_format = XFS_DINODE_FMT_EXTENTS;
779 	xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
780 }
781 
782 
783 int					/* error */
784 xfs_bmap_local_to_extents(
785 	xfs_trans_t	*tp,		/* transaction pointer */
786 	xfs_inode_t	*ip,		/* incore inode pointer */
787 	xfs_extlen_t	total,		/* total blocks needed by transaction */
788 	int		*logflagsp,	/* inode logging flags */
789 	int		whichfork,
790 	void		(*init_fn)(struct xfs_trans *tp,
791 				   struct xfs_buf *bp,
792 				   struct xfs_inode *ip,
793 				   struct xfs_ifork *ifp, void *priv),
794 	void		*priv)
795 {
796 	int		error = 0;
797 	int		flags;		/* logging flags returned */
798 	struct xfs_ifork *ifp;		/* inode fork pointer */
799 	xfs_alloc_arg_t	args;		/* allocation arguments */
800 	struct xfs_buf	*bp;		/* buffer for extent block */
801 	struct xfs_bmbt_irec rec;
802 	struct xfs_iext_cursor icur;
803 
804 	/*
805 	 * We don't want to deal with the case of keeping inode data inline yet.
806 	 * So sending the data fork of a regular inode is invalid.
807 	 */
808 	ASSERT(!(S_ISREG(VFS_I(ip)->i_mode) && whichfork == XFS_DATA_FORK));
809 	ifp = xfs_ifork_ptr(ip, whichfork);
810 	ASSERT(ifp->if_format == XFS_DINODE_FMT_LOCAL);
811 
812 	if (!ifp->if_bytes) {
813 		xfs_bmap_local_to_extents_empty(tp, ip, whichfork);
814 		flags = XFS_ILOG_CORE;
815 		goto done;
816 	}
817 
818 	flags = 0;
819 	error = 0;
820 	memset(&args, 0, sizeof(args));
821 	args.tp = tp;
822 	args.mp = ip->i_mount;
823 	args.total = total;
824 	args.minlen = args.maxlen = args.prod = 1;
825 	xfs_rmap_ino_owner(&args.oinfo, ip->i_ino, whichfork, 0);
826 
827 	/*
828 	 * Allocate a block.  We know we need only one, since the
829 	 * file currently fits in an inode.
830 	 */
831 	args.total = total;
832 	args.minlen = args.maxlen = args.prod = 1;
833 	error = xfs_alloc_vextent_start_ag(&args,
834 			XFS_INO_TO_FSB(args.mp, ip->i_ino));
835 	if (error)
836 		goto done;
837 
838 	/* Can't fail, the space was reserved. */
839 	ASSERT(args.fsbno != NULLFSBLOCK);
840 	ASSERT(args.len == 1);
841 	error = xfs_trans_get_buf(tp, args.mp->m_ddev_targp,
842 			XFS_FSB_TO_DADDR(args.mp, args.fsbno),
843 			args.mp->m_bsize, 0, &bp);
844 	if (error)
845 		goto done;
846 
847 	/*
848 	 * Initialize the block, copy the data and log the remote buffer.
849 	 *
850 	 * The callout is responsible for logging because the remote format
851 	 * might differ from the local format and thus we don't know how much to
852 	 * log here. Note that init_fn must also set the buffer log item type
853 	 * correctly.
854 	 */
855 	init_fn(tp, bp, ip, ifp, priv);
856 
857 	/* account for the change in fork size */
858 	xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
859 	xfs_bmap_local_to_extents_empty(tp, ip, whichfork);
860 	flags |= XFS_ILOG_CORE;
861 
862 	ifp->if_data = NULL;
863 	ifp->if_height = 0;
864 
865 	rec.br_startoff = 0;
866 	rec.br_startblock = args.fsbno;
867 	rec.br_blockcount = 1;
868 	rec.br_state = XFS_EXT_NORM;
869 	xfs_iext_first(ifp, &icur);
870 	xfs_iext_insert(ip, &icur, &rec, 0);
871 
872 	ifp->if_nextents = 1;
873 	ip->i_nblocks = 1;
874 	xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
875 	flags |= xfs_ilog_fext(whichfork);
876 
877 done:
878 	*logflagsp = flags;
879 	return error;
880 }
881 
882 /*
883  * Called from xfs_bmap_add_attrfork to handle btree format files.
884  */
885 STATIC int					/* error */
886 xfs_bmap_add_attrfork_btree(
887 	xfs_trans_t		*tp,		/* transaction pointer */
888 	xfs_inode_t		*ip,		/* incore inode pointer */
889 	int			*flags)		/* inode logging flags */
890 {
891 	struct xfs_btree_block	*block = ip->i_df.if_broot;
892 	struct xfs_btree_cur	*cur;		/* btree cursor */
893 	int			error;		/* error return value */
894 	xfs_mount_t		*mp;		/* file system mount struct */
895 	int			stat;		/* newroot status */
896 
897 	mp = ip->i_mount;
898 
899 	if (xfs_bmap_bmdr_space(block) <= xfs_inode_data_fork_size(ip))
900 		*flags |= XFS_ILOG_DBROOT;
901 	else {
902 		cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
903 		error = xfs_bmbt_lookup_first(cur, &stat);
904 		if (error)
905 			goto error0;
906 		/* must be at least one entry */
907 		if (XFS_IS_CORRUPT(mp, stat != 1)) {
908 			xfs_btree_mark_sick(cur);
909 			error = -EFSCORRUPTED;
910 			goto error0;
911 		}
912 		if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
913 			goto error0;
914 		if (stat == 0) {
915 			xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
916 			return -ENOSPC;
917 		}
918 		cur->bc_bmap.allocated = 0;
919 		xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
920 	}
921 	return 0;
922 error0:
923 	xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
924 	return error;
925 }
926 
927 /*
928  * Called from xfs_bmap_add_attrfork to handle extents format files.
929  */
930 STATIC int					/* error */
931 xfs_bmap_add_attrfork_extents(
932 	struct xfs_trans	*tp,		/* transaction pointer */
933 	struct xfs_inode	*ip,		/* incore inode pointer */
934 	int			*flags)		/* inode logging flags */
935 {
936 	struct xfs_btree_cur	*cur;		/* bmap btree cursor */
937 	int			error;		/* error return value */
938 
939 	if (ip->i_df.if_nextents * sizeof(struct xfs_bmbt_rec) <=
940 	    xfs_inode_data_fork_size(ip))
941 		return 0;
942 	cur = NULL;
943 	error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0, flags,
944 					  XFS_DATA_FORK);
945 	if (cur) {
946 		cur->bc_bmap.allocated = 0;
947 		xfs_btree_del_cursor(cur, error);
948 	}
949 	return error;
950 }
951 
952 /*
953  * Called from xfs_bmap_add_attrfork to handle local format files. Each
954  * different data fork content type needs a different callout to do the
955  * conversion. Some are basic and only require special block initialisation
956  * callouts for the data formating, others (directories) are so specialised they
957  * handle everything themselves.
958  *
959  * XXX (dgc): investigate whether directory conversion can use the generic
960  * formatting callout. It should be possible - it's just a very complex
961  * formatter.
962  */
963 STATIC int					/* error */
964 xfs_bmap_add_attrfork_local(
965 	struct xfs_trans	*tp,		/* transaction pointer */
966 	struct xfs_inode	*ip,		/* incore inode pointer */
967 	int			*flags)		/* inode logging flags */
968 {
969 	struct xfs_da_args	dargs;		/* args for dir/attr code */
970 
971 	if (ip->i_df.if_bytes <= xfs_inode_data_fork_size(ip))
972 		return 0;
973 
974 	if (S_ISDIR(VFS_I(ip)->i_mode)) {
975 		memset(&dargs, 0, sizeof(dargs));
976 		dargs.geo = ip->i_mount->m_dir_geo;
977 		dargs.dp = ip;
978 		dargs.total = dargs.geo->fsbcount;
979 		dargs.whichfork = XFS_DATA_FORK;
980 		dargs.trans = tp;
981 		dargs.owner = ip->i_ino;
982 		return xfs_dir2_sf_to_block(&dargs);
983 	}
984 
985 	if (S_ISLNK(VFS_I(ip)->i_mode))
986 		return xfs_bmap_local_to_extents(tp, ip, 1, flags,
987 				XFS_DATA_FORK, xfs_symlink_local_to_remote,
988 				NULL);
989 
990 	/* should only be called for types that support local format data */
991 	ASSERT(0);
992 	xfs_bmap_mark_sick(ip, XFS_ATTR_FORK);
993 	return -EFSCORRUPTED;
994 }
995 
996 /*
997  * Set an inode attr fork offset based on the format of the data fork.
998  */
999 static int
1000 xfs_bmap_set_attrforkoff(
1001 	struct xfs_inode	*ip,
1002 	int			size,
1003 	int			*version)
1004 {
1005 	int			default_size = xfs_default_attroffset(ip) >> 3;
1006 
1007 	switch (ip->i_df.if_format) {
1008 	case XFS_DINODE_FMT_DEV:
1009 		ip->i_forkoff = default_size;
1010 		break;
1011 	case XFS_DINODE_FMT_LOCAL:
1012 	case XFS_DINODE_FMT_EXTENTS:
1013 	case XFS_DINODE_FMT_BTREE:
1014 		ip->i_forkoff = xfs_attr_shortform_bytesfit(ip, size);
1015 		if (!ip->i_forkoff)
1016 			ip->i_forkoff = default_size;
1017 		else if (xfs_has_attr2(ip->i_mount) && version)
1018 			*version = 2;
1019 		break;
1020 	default:
1021 		ASSERT(0);
1022 		return -EINVAL;
1023 	}
1024 
1025 	return 0;
1026 }
1027 
1028 /*
1029  * Convert inode from non-attributed to attributed.  Caller must hold the
1030  * ILOCK_EXCL and the file cannot have an attr fork.
1031  */
1032 int						/* error code */
1033 xfs_bmap_add_attrfork(
1034 	struct xfs_trans	*tp,
1035 	struct xfs_inode	*ip,		/* incore inode pointer */
1036 	int			size,		/* space new attribute needs */
1037 	int			rsvd)		/* xact may use reserved blks */
1038 {
1039 	struct xfs_mount	*mp = tp->t_mountp;
1040 	int			version = 1;	/* superblock attr version */
1041 	int			logflags;	/* logging flags */
1042 	int			error;		/* error return value */
1043 
1044 	xfs_assert_ilocked(ip, XFS_ILOCK_EXCL);
1045 	ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
1046 	ASSERT(!xfs_inode_has_attr_fork(ip));
1047 
1048 	xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1049 	error = xfs_bmap_set_attrforkoff(ip, size, &version);
1050 	if (error)
1051 		return error;
1052 
1053 	xfs_ifork_init_attr(ip, XFS_DINODE_FMT_EXTENTS, 0);
1054 	logflags = 0;
1055 	switch (ip->i_df.if_format) {
1056 	case XFS_DINODE_FMT_LOCAL:
1057 		error = xfs_bmap_add_attrfork_local(tp, ip, &logflags);
1058 		break;
1059 	case XFS_DINODE_FMT_EXTENTS:
1060 		error = xfs_bmap_add_attrfork_extents(tp, ip, &logflags);
1061 		break;
1062 	case XFS_DINODE_FMT_BTREE:
1063 		error = xfs_bmap_add_attrfork_btree(tp, ip, &logflags);
1064 		break;
1065 	default:
1066 		error = 0;
1067 		break;
1068 	}
1069 	if (logflags)
1070 		xfs_trans_log_inode(tp, ip, logflags);
1071 	if (error)
1072 		return error;
1073 	if (!xfs_has_attr(mp) ||
1074 	   (!xfs_has_attr2(mp) && version == 2)) {
1075 		bool log_sb = false;
1076 
1077 		spin_lock(&mp->m_sb_lock);
1078 		if (!xfs_has_attr(mp)) {
1079 			xfs_add_attr(mp);
1080 			log_sb = true;
1081 		}
1082 		if (!xfs_has_attr2(mp) && version == 2) {
1083 			xfs_add_attr2(mp);
1084 			log_sb = true;
1085 		}
1086 		spin_unlock(&mp->m_sb_lock);
1087 		if (log_sb)
1088 			xfs_log_sb(tp);
1089 	}
1090 
1091 	return 0;
1092 }
1093 
1094 /*
1095  * Internal and external extent tree search functions.
1096  */
1097 
1098 struct xfs_iread_state {
1099 	struct xfs_iext_cursor	icur;
1100 	xfs_extnum_t		loaded;
1101 };
1102 
1103 int
1104 xfs_bmap_complain_bad_rec(
1105 	struct xfs_inode		*ip,
1106 	int				whichfork,
1107 	xfs_failaddr_t			fa,
1108 	const struct xfs_bmbt_irec	*irec)
1109 {
1110 	struct xfs_mount		*mp = ip->i_mount;
1111 	const char			*forkname;
1112 
1113 	switch (whichfork) {
1114 	case XFS_DATA_FORK:	forkname = "data"; break;
1115 	case XFS_ATTR_FORK:	forkname = "attr"; break;
1116 	case XFS_COW_FORK:	forkname = "CoW"; break;
1117 	default:		forkname = "???"; break;
1118 	}
1119 
1120 	xfs_warn(mp,
1121  "Bmap BTree record corruption in inode 0x%llx %s fork detected at %pS!",
1122 				ip->i_ino, forkname, fa);
1123 	xfs_warn(mp,
1124 		"Offset 0x%llx, start block 0x%llx, block count 0x%llx state 0x%x",
1125 		irec->br_startoff, irec->br_startblock, irec->br_blockcount,
1126 		irec->br_state);
1127 
1128 	return -EFSCORRUPTED;
1129 }
1130 
1131 /* Stuff every bmbt record from this block into the incore extent map. */
1132 static int
1133 xfs_iread_bmbt_block(
1134 	struct xfs_btree_cur	*cur,
1135 	int			level,
1136 	void			*priv)
1137 {
1138 	struct xfs_iread_state	*ir = priv;
1139 	struct xfs_mount	*mp = cur->bc_mp;
1140 	struct xfs_inode	*ip = cur->bc_ino.ip;
1141 	struct xfs_btree_block	*block;
1142 	struct xfs_buf		*bp;
1143 	struct xfs_bmbt_rec	*frp;
1144 	xfs_extnum_t		num_recs;
1145 	xfs_extnum_t		j;
1146 	int			whichfork = cur->bc_ino.whichfork;
1147 	struct xfs_ifork	*ifp = xfs_ifork_ptr(ip, whichfork);
1148 
1149 	block = xfs_btree_get_block(cur, level, &bp);
1150 
1151 	/* Abort if we find more records than nextents. */
1152 	num_recs = xfs_btree_get_numrecs(block);
1153 	if (unlikely(ir->loaded + num_recs > ifp->if_nextents)) {
1154 		xfs_warn(ip->i_mount, "corrupt dinode %llu, (btree extents).",
1155 				(unsigned long long)ip->i_ino);
1156 		xfs_inode_verifier_error(ip, -EFSCORRUPTED, __func__, block,
1157 				sizeof(*block), __this_address);
1158 		xfs_bmap_mark_sick(ip, whichfork);
1159 		return -EFSCORRUPTED;
1160 	}
1161 
1162 	/* Copy records into the incore cache. */
1163 	frp = xfs_bmbt_rec_addr(mp, block, 1);
1164 	for (j = 0; j < num_recs; j++, frp++, ir->loaded++) {
1165 		struct xfs_bmbt_irec	new;
1166 		xfs_failaddr_t		fa;
1167 
1168 		xfs_bmbt_disk_get_all(frp, &new);
1169 		fa = xfs_bmap_validate_extent(ip, whichfork, &new);
1170 		if (fa) {
1171 			xfs_inode_verifier_error(ip, -EFSCORRUPTED,
1172 					"xfs_iread_extents(2)", frp,
1173 					sizeof(*frp), fa);
1174 			xfs_bmap_mark_sick(ip, whichfork);
1175 			return xfs_bmap_complain_bad_rec(ip, whichfork, fa,
1176 					&new);
1177 		}
1178 		xfs_iext_insert(ip, &ir->icur, &new,
1179 				xfs_bmap_fork_to_state(whichfork));
1180 		trace_xfs_read_extent(ip, &ir->icur,
1181 				xfs_bmap_fork_to_state(whichfork), _THIS_IP_);
1182 		xfs_iext_next(ifp, &ir->icur);
1183 	}
1184 
1185 	return 0;
1186 }
1187 
1188 /*
1189  * Read in extents from a btree-format inode.
1190  */
1191 int
1192 xfs_iread_extents(
1193 	struct xfs_trans	*tp,
1194 	struct xfs_inode	*ip,
1195 	int			whichfork)
1196 {
1197 	struct xfs_iread_state	ir;
1198 	struct xfs_ifork	*ifp = xfs_ifork_ptr(ip, whichfork);
1199 	struct xfs_mount	*mp = ip->i_mount;
1200 	struct xfs_btree_cur	*cur;
1201 	int			error;
1202 
1203 	if (!xfs_need_iread_extents(ifp))
1204 		return 0;
1205 
1206 	xfs_assert_ilocked(ip, XFS_ILOCK_EXCL);
1207 
1208 	ir.loaded = 0;
1209 	xfs_iext_first(ifp, &ir.icur);
1210 	cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
1211 	error = xfs_btree_visit_blocks(cur, xfs_iread_bmbt_block,
1212 			XFS_BTREE_VISIT_RECORDS, &ir);
1213 	xfs_btree_del_cursor(cur, error);
1214 	if (error)
1215 		goto out;
1216 
1217 	if (XFS_IS_CORRUPT(mp, ir.loaded != ifp->if_nextents)) {
1218 		xfs_bmap_mark_sick(ip, whichfork);
1219 		error = -EFSCORRUPTED;
1220 		goto out;
1221 	}
1222 	ASSERT(ir.loaded == xfs_iext_count(ifp));
1223 	/*
1224 	 * Use release semantics so that we can use acquire semantics in
1225 	 * xfs_need_iread_extents and be guaranteed to see a valid mapping tree
1226 	 * after that load.
1227 	 */
1228 	smp_store_release(&ifp->if_needextents, 0);
1229 	return 0;
1230 out:
1231 	if (xfs_metadata_is_sick(error))
1232 		xfs_bmap_mark_sick(ip, whichfork);
1233 	xfs_iext_destroy(ifp);
1234 	return error;
1235 }
1236 
1237 /*
1238  * Returns the relative block number of the first unused block(s) in the given
1239  * fork with at least "len" logically contiguous blocks free.  This is the
1240  * lowest-address hole if the fork has holes, else the first block past the end
1241  * of fork.  Return 0 if the fork is currently local (in-inode).
1242  */
1243 int						/* error */
1244 xfs_bmap_first_unused(
1245 	struct xfs_trans	*tp,		/* transaction pointer */
1246 	struct xfs_inode	*ip,		/* incore inode */
1247 	xfs_extlen_t		len,		/* size of hole to find */
1248 	xfs_fileoff_t		*first_unused,	/* unused block */
1249 	int			whichfork)	/* data or attr fork */
1250 {
1251 	struct xfs_ifork	*ifp = xfs_ifork_ptr(ip, whichfork);
1252 	struct xfs_bmbt_irec	got;
1253 	struct xfs_iext_cursor	icur;
1254 	xfs_fileoff_t		lastaddr = 0;
1255 	xfs_fileoff_t		lowest, max;
1256 	int			error;
1257 
1258 	if (ifp->if_format == XFS_DINODE_FMT_LOCAL) {
1259 		*first_unused = 0;
1260 		return 0;
1261 	}
1262 
1263 	ASSERT(xfs_ifork_has_extents(ifp));
1264 
1265 	error = xfs_iread_extents(tp, ip, whichfork);
1266 	if (error)
1267 		return error;
1268 
1269 	lowest = max = *first_unused;
1270 	for_each_xfs_iext(ifp, &icur, &got) {
1271 		/*
1272 		 * See if the hole before this extent will work.
1273 		 */
1274 		if (got.br_startoff >= lowest + len &&
1275 		    got.br_startoff - max >= len)
1276 			break;
1277 		lastaddr = got.br_startoff + got.br_blockcount;
1278 		max = XFS_FILEOFF_MAX(lastaddr, lowest);
1279 	}
1280 
1281 	*first_unused = max;
1282 	return 0;
1283 }
1284 
1285 /*
1286  * Returns the file-relative block number of the last block - 1 before
1287  * last_block (input value) in the file.
1288  * This is not based on i_size, it is based on the extent records.
1289  * Returns 0 for local files, as they do not have extent records.
1290  */
1291 int						/* error */
1292 xfs_bmap_last_before(
1293 	struct xfs_trans	*tp,		/* transaction pointer */
1294 	struct xfs_inode	*ip,		/* incore inode */
1295 	xfs_fileoff_t		*last_block,	/* last block */
1296 	int			whichfork)	/* data or attr fork */
1297 {
1298 	struct xfs_ifork	*ifp = xfs_ifork_ptr(ip, whichfork);
1299 	struct xfs_bmbt_irec	got;
1300 	struct xfs_iext_cursor	icur;
1301 	int			error;
1302 
1303 	switch (ifp->if_format) {
1304 	case XFS_DINODE_FMT_LOCAL:
1305 		*last_block = 0;
1306 		return 0;
1307 	case XFS_DINODE_FMT_BTREE:
1308 	case XFS_DINODE_FMT_EXTENTS:
1309 		break;
1310 	default:
1311 		ASSERT(0);
1312 		xfs_bmap_mark_sick(ip, whichfork);
1313 		return -EFSCORRUPTED;
1314 	}
1315 
1316 	error = xfs_iread_extents(tp, ip, whichfork);
1317 	if (error)
1318 		return error;
1319 
1320 	if (!xfs_iext_lookup_extent_before(ip, ifp, last_block, &icur, &got))
1321 		*last_block = 0;
1322 	return 0;
1323 }
1324 
1325 int
1326 xfs_bmap_last_extent(
1327 	struct xfs_trans	*tp,
1328 	struct xfs_inode	*ip,
1329 	int			whichfork,
1330 	struct xfs_bmbt_irec	*rec,
1331 	int			*is_empty)
1332 {
1333 	struct xfs_ifork	*ifp = xfs_ifork_ptr(ip, whichfork);
1334 	struct xfs_iext_cursor	icur;
1335 	int			error;
1336 
1337 	error = xfs_iread_extents(tp, ip, whichfork);
1338 	if (error)
1339 		return error;
1340 
1341 	xfs_iext_last(ifp, &icur);
1342 	if (!xfs_iext_get_extent(ifp, &icur, rec))
1343 		*is_empty = 1;
1344 	else
1345 		*is_empty = 0;
1346 	return 0;
1347 }
1348 
1349 /*
1350  * Check the last inode extent to determine whether this allocation will result
1351  * in blocks being allocated at the end of the file. When we allocate new data
1352  * blocks at the end of the file which do not start at the previous data block,
1353  * we will try to align the new blocks at stripe unit boundaries.
1354  *
1355  * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
1356  * at, or past the EOF.
1357  */
1358 STATIC int
1359 xfs_bmap_isaeof(
1360 	struct xfs_bmalloca	*bma,
1361 	int			whichfork)
1362 {
1363 	struct xfs_bmbt_irec	rec;
1364 	int			is_empty;
1365 	int			error;
1366 
1367 	bma->aeof = false;
1368 	error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
1369 				     &is_empty);
1370 	if (error)
1371 		return error;
1372 
1373 	if (is_empty) {
1374 		bma->aeof = true;
1375 		return 0;
1376 	}
1377 
1378 	/*
1379 	 * Check if we are allocation or past the last extent, or at least into
1380 	 * the last delayed allocated extent.
1381 	 */
1382 	bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
1383 		(bma->offset >= rec.br_startoff &&
1384 		 isnullstartblock(rec.br_startblock));
1385 	return 0;
1386 }
1387 
1388 /*
1389  * Returns the file-relative block number of the first block past eof in
1390  * the file.  This is not based on i_size, it is based on the extent records.
1391  * Returns 0 for local files, as they do not have extent records.
1392  */
1393 int
1394 xfs_bmap_last_offset(
1395 	struct xfs_inode	*ip,
1396 	xfs_fileoff_t		*last_block,
1397 	int			whichfork)
1398 {
1399 	struct xfs_ifork	*ifp = xfs_ifork_ptr(ip, whichfork);
1400 	struct xfs_bmbt_irec	rec;
1401 	int			is_empty;
1402 	int			error;
1403 
1404 	*last_block = 0;
1405 
1406 	if (ifp->if_format == XFS_DINODE_FMT_LOCAL)
1407 		return 0;
1408 
1409 	if (XFS_IS_CORRUPT(ip->i_mount, !xfs_ifork_has_extents(ifp))) {
1410 		xfs_bmap_mark_sick(ip, whichfork);
1411 		return -EFSCORRUPTED;
1412 	}
1413 
1414 	error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
1415 	if (error || is_empty)
1416 		return error;
1417 
1418 	*last_block = rec.br_startoff + rec.br_blockcount;
1419 	return 0;
1420 }
1421 
1422 /*
1423  * Extent tree manipulation functions used during allocation.
1424  */
1425 
1426 /*
1427  * Convert a delayed allocation to a real allocation.
1428  */
1429 STATIC int				/* error */
1430 xfs_bmap_add_extent_delay_real(
1431 	struct xfs_bmalloca	*bma,
1432 	int			whichfork)
1433 {
1434 	struct xfs_mount	*mp = bma->ip->i_mount;
1435 	struct xfs_ifork	*ifp = xfs_ifork_ptr(bma->ip, whichfork);
1436 	struct xfs_bmbt_irec	*new = &bma->got;
1437 	int			error;	/* error return value */
1438 	int			i;	/* temp state */
1439 	xfs_fileoff_t		new_endoff;	/* end offset of new entry */
1440 	xfs_bmbt_irec_t		r[3];	/* neighbor extent entries */
1441 					/* left is 0, right is 1, prev is 2 */
1442 	int			rval=0;	/* return value (logging flags) */
1443 	uint32_t		state = xfs_bmap_fork_to_state(whichfork);
1444 	xfs_filblks_t		da_new; /* new count del alloc blocks used */
1445 	xfs_filblks_t		da_old; /* old count del alloc blocks used */
1446 	xfs_filblks_t		temp=0;	/* value for da_new calculations */
1447 	int			tmp_rval;	/* partial logging flags */
1448 	struct xfs_bmbt_irec	old;
1449 
1450 	ASSERT(whichfork != XFS_ATTR_FORK);
1451 	ASSERT(!isnullstartblock(new->br_startblock));
1452 	ASSERT(!bma->cur || (bma->cur->bc_flags & XFS_BTREE_BMBT_WASDEL));
1453 
1454 	XFS_STATS_INC(mp, xs_add_exlist);
1455 
1456 #define	LEFT		r[0]
1457 #define	RIGHT		r[1]
1458 #define	PREV		r[2]
1459 
1460 	/*
1461 	 * Set up a bunch of variables to make the tests simpler.
1462 	 */
1463 	xfs_iext_get_extent(ifp, &bma->icur, &PREV);
1464 	new_endoff = new->br_startoff + new->br_blockcount;
1465 	ASSERT(isnullstartblock(PREV.br_startblock));
1466 	ASSERT(PREV.br_startoff <= new->br_startoff);
1467 	ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
1468 
1469 	da_old = startblockval(PREV.br_startblock);
1470 	da_new = 0;
1471 
1472 	/*
1473 	 * Set flags determining what part of the previous delayed allocation
1474 	 * extent is being replaced by a real allocation.
1475 	 */
1476 	if (PREV.br_startoff == new->br_startoff)
1477 		state |= BMAP_LEFT_FILLING;
1478 	if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1479 		state |= BMAP_RIGHT_FILLING;
1480 
1481 	/*
1482 	 * Check and set flags if this segment has a left neighbor.
1483 	 * Don't set contiguous if the combined extent would be too large.
1484 	 */
1485 	if (xfs_iext_peek_prev_extent(ifp, &bma->icur, &LEFT)) {
1486 		state |= BMAP_LEFT_VALID;
1487 		if (isnullstartblock(LEFT.br_startblock))
1488 			state |= BMAP_LEFT_DELAY;
1489 	}
1490 
1491 	if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1492 	    LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1493 	    LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1494 	    LEFT.br_state == new->br_state &&
1495 	    LEFT.br_blockcount + new->br_blockcount <= XFS_MAX_BMBT_EXTLEN)
1496 		state |= BMAP_LEFT_CONTIG;
1497 
1498 	/*
1499 	 * Check and set flags if this segment has a right neighbor.
1500 	 * Don't set contiguous if the combined extent would be too large.
1501 	 * Also check for all-three-contiguous being too large.
1502 	 */
1503 	if (xfs_iext_peek_next_extent(ifp, &bma->icur, &RIGHT)) {
1504 		state |= BMAP_RIGHT_VALID;
1505 		if (isnullstartblock(RIGHT.br_startblock))
1506 			state |= BMAP_RIGHT_DELAY;
1507 	}
1508 
1509 	if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1510 	    new_endoff == RIGHT.br_startoff &&
1511 	    new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1512 	    new->br_state == RIGHT.br_state &&
1513 	    new->br_blockcount + RIGHT.br_blockcount <= XFS_MAX_BMBT_EXTLEN &&
1514 	    ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1515 		       BMAP_RIGHT_FILLING)) !=
1516 		      (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1517 		       BMAP_RIGHT_FILLING) ||
1518 	     LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1519 			<= XFS_MAX_BMBT_EXTLEN))
1520 		state |= BMAP_RIGHT_CONTIG;
1521 
1522 	error = 0;
1523 	/*
1524 	 * Switch out based on the FILLING and CONTIG state bits.
1525 	 */
1526 	switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1527 			 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1528 	case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1529 	     BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1530 		/*
1531 		 * Filling in all of a previously delayed allocation extent.
1532 		 * The left and right neighbors are both contiguous with new.
1533 		 */
1534 		LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
1535 
1536 		xfs_iext_remove(bma->ip, &bma->icur, state);
1537 		xfs_iext_remove(bma->ip, &bma->icur, state);
1538 		xfs_iext_prev(ifp, &bma->icur);
1539 		xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
1540 		ifp->if_nextents--;
1541 
1542 		if (bma->cur == NULL)
1543 			rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1544 		else {
1545 			rval = XFS_ILOG_CORE;
1546 			error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
1547 			if (error)
1548 				goto done;
1549 			if (XFS_IS_CORRUPT(mp, i != 1)) {
1550 				xfs_btree_mark_sick(bma->cur);
1551 				error = -EFSCORRUPTED;
1552 				goto done;
1553 			}
1554 			error = xfs_btree_delete(bma->cur, &i);
1555 			if (error)
1556 				goto done;
1557 			if (XFS_IS_CORRUPT(mp, i != 1)) {
1558 				xfs_btree_mark_sick(bma->cur);
1559 				error = -EFSCORRUPTED;
1560 				goto done;
1561 			}
1562 			error = xfs_btree_decrement(bma->cur, 0, &i);
1563 			if (error)
1564 				goto done;
1565 			if (XFS_IS_CORRUPT(mp, i != 1)) {
1566 				xfs_btree_mark_sick(bma->cur);
1567 				error = -EFSCORRUPTED;
1568 				goto done;
1569 			}
1570 			error = xfs_bmbt_update(bma->cur, &LEFT);
1571 			if (error)
1572 				goto done;
1573 		}
1574 		ASSERT(da_new <= da_old);
1575 		break;
1576 
1577 	case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1578 		/*
1579 		 * Filling in all of a previously delayed allocation extent.
1580 		 * The left neighbor is contiguous, the right is not.
1581 		 */
1582 		old = LEFT;
1583 		LEFT.br_blockcount += PREV.br_blockcount;
1584 
1585 		xfs_iext_remove(bma->ip, &bma->icur, state);
1586 		xfs_iext_prev(ifp, &bma->icur);
1587 		xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
1588 
1589 		if (bma->cur == NULL)
1590 			rval = XFS_ILOG_DEXT;
1591 		else {
1592 			rval = 0;
1593 			error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
1594 			if (error)
1595 				goto done;
1596 			if (XFS_IS_CORRUPT(mp, i != 1)) {
1597 				xfs_btree_mark_sick(bma->cur);
1598 				error = -EFSCORRUPTED;
1599 				goto done;
1600 			}
1601 			error = xfs_bmbt_update(bma->cur, &LEFT);
1602 			if (error)
1603 				goto done;
1604 		}
1605 		ASSERT(da_new <= da_old);
1606 		break;
1607 
1608 	case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1609 		/*
1610 		 * Filling in all of a previously delayed allocation extent.
1611 		 * The right neighbor is contiguous, the left is not. Take care
1612 		 * with delay -> unwritten extent allocation here because the
1613 		 * delalloc record we are overwriting is always written.
1614 		 */
1615 		PREV.br_startblock = new->br_startblock;
1616 		PREV.br_blockcount += RIGHT.br_blockcount;
1617 		PREV.br_state = new->br_state;
1618 
1619 		xfs_iext_next(ifp, &bma->icur);
1620 		xfs_iext_remove(bma->ip, &bma->icur, state);
1621 		xfs_iext_prev(ifp, &bma->icur);
1622 		xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1623 
1624 		if (bma->cur == NULL)
1625 			rval = XFS_ILOG_DEXT;
1626 		else {
1627 			rval = 0;
1628 			error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
1629 			if (error)
1630 				goto done;
1631 			if (XFS_IS_CORRUPT(mp, i != 1)) {
1632 				xfs_btree_mark_sick(bma->cur);
1633 				error = -EFSCORRUPTED;
1634 				goto done;
1635 			}
1636 			error = xfs_bmbt_update(bma->cur, &PREV);
1637 			if (error)
1638 				goto done;
1639 		}
1640 		ASSERT(da_new <= da_old);
1641 		break;
1642 
1643 	case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
1644 		/*
1645 		 * Filling in all of a previously delayed allocation extent.
1646 		 * Neither the left nor right neighbors are contiguous with
1647 		 * the new one.
1648 		 */
1649 		PREV.br_startblock = new->br_startblock;
1650 		PREV.br_state = new->br_state;
1651 		xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1652 		ifp->if_nextents++;
1653 
1654 		if (bma->cur == NULL)
1655 			rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1656 		else {
1657 			rval = XFS_ILOG_CORE;
1658 			error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
1659 			if (error)
1660 				goto done;
1661 			if (XFS_IS_CORRUPT(mp, i != 0)) {
1662 				xfs_btree_mark_sick(bma->cur);
1663 				error = -EFSCORRUPTED;
1664 				goto done;
1665 			}
1666 			error = xfs_btree_insert(bma->cur, &i);
1667 			if (error)
1668 				goto done;
1669 			if (XFS_IS_CORRUPT(mp, i != 1)) {
1670 				xfs_btree_mark_sick(bma->cur);
1671 				error = -EFSCORRUPTED;
1672 				goto done;
1673 			}
1674 		}
1675 		ASSERT(da_new <= da_old);
1676 		break;
1677 
1678 	case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
1679 		/*
1680 		 * Filling in the first part of a previous delayed allocation.
1681 		 * The left neighbor is contiguous.
1682 		 */
1683 		old = LEFT;
1684 		temp = PREV.br_blockcount - new->br_blockcount;
1685 		da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1686 				startblockval(PREV.br_startblock));
1687 
1688 		LEFT.br_blockcount += new->br_blockcount;
1689 
1690 		PREV.br_blockcount = temp;
1691 		PREV.br_startoff += new->br_blockcount;
1692 		PREV.br_startblock = nullstartblock(da_new);
1693 
1694 		xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1695 		xfs_iext_prev(ifp, &bma->icur);
1696 		xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
1697 
1698 		if (bma->cur == NULL)
1699 			rval = XFS_ILOG_DEXT;
1700 		else {
1701 			rval = 0;
1702 			error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
1703 			if (error)
1704 				goto done;
1705 			if (XFS_IS_CORRUPT(mp, i != 1)) {
1706 				xfs_btree_mark_sick(bma->cur);
1707 				error = -EFSCORRUPTED;
1708 				goto done;
1709 			}
1710 			error = xfs_bmbt_update(bma->cur, &LEFT);
1711 			if (error)
1712 				goto done;
1713 		}
1714 		ASSERT(da_new <= da_old);
1715 		break;
1716 
1717 	case BMAP_LEFT_FILLING:
1718 		/*
1719 		 * Filling in the first part of a previous delayed allocation.
1720 		 * The left neighbor is not contiguous.
1721 		 */
1722 		xfs_iext_update_extent(bma->ip, state, &bma->icur, new);
1723 		ifp->if_nextents++;
1724 
1725 		if (bma->cur == NULL)
1726 			rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1727 		else {
1728 			rval = XFS_ILOG_CORE;
1729 			error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
1730 			if (error)
1731 				goto done;
1732 			if (XFS_IS_CORRUPT(mp, i != 0)) {
1733 				xfs_btree_mark_sick(bma->cur);
1734 				error = -EFSCORRUPTED;
1735 				goto done;
1736 			}
1737 			error = xfs_btree_insert(bma->cur, &i);
1738 			if (error)
1739 				goto done;
1740 			if (XFS_IS_CORRUPT(mp, i != 1)) {
1741 				xfs_btree_mark_sick(bma->cur);
1742 				error = -EFSCORRUPTED;
1743 				goto done;
1744 			}
1745 		}
1746 
1747 		if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
1748 			error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
1749 					&bma->cur, 1, &tmp_rval, whichfork);
1750 			rval |= tmp_rval;
1751 			if (error)
1752 				goto done;
1753 		}
1754 
1755 		temp = PREV.br_blockcount - new->br_blockcount;
1756 		da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1757 			startblockval(PREV.br_startblock) -
1758 			(bma->cur ? bma->cur->bc_bmap.allocated : 0));
1759 
1760 		PREV.br_startoff = new_endoff;
1761 		PREV.br_blockcount = temp;
1762 		PREV.br_startblock = nullstartblock(da_new);
1763 		xfs_iext_next(ifp, &bma->icur);
1764 		xfs_iext_insert(bma->ip, &bma->icur, &PREV, state);
1765 		xfs_iext_prev(ifp, &bma->icur);
1766 		break;
1767 
1768 	case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1769 		/*
1770 		 * Filling in the last part of a previous delayed allocation.
1771 		 * The right neighbor is contiguous with the new allocation.
1772 		 */
1773 		old = RIGHT;
1774 		RIGHT.br_startoff = new->br_startoff;
1775 		RIGHT.br_startblock = new->br_startblock;
1776 		RIGHT.br_blockcount += new->br_blockcount;
1777 
1778 		if (bma->cur == NULL)
1779 			rval = XFS_ILOG_DEXT;
1780 		else {
1781 			rval = 0;
1782 			error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
1783 			if (error)
1784 				goto done;
1785 			if (XFS_IS_CORRUPT(mp, i != 1)) {
1786 				xfs_btree_mark_sick(bma->cur);
1787 				error = -EFSCORRUPTED;
1788 				goto done;
1789 			}
1790 			error = xfs_bmbt_update(bma->cur, &RIGHT);
1791 			if (error)
1792 				goto done;
1793 		}
1794 
1795 		temp = PREV.br_blockcount - new->br_blockcount;
1796 		da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1797 			startblockval(PREV.br_startblock));
1798 
1799 		PREV.br_blockcount = temp;
1800 		PREV.br_startblock = nullstartblock(da_new);
1801 
1802 		xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1803 		xfs_iext_next(ifp, &bma->icur);
1804 		xfs_iext_update_extent(bma->ip, state, &bma->icur, &RIGHT);
1805 		ASSERT(da_new <= da_old);
1806 		break;
1807 
1808 	case BMAP_RIGHT_FILLING:
1809 		/*
1810 		 * Filling in the last part of a previous delayed allocation.
1811 		 * The right neighbor is not contiguous.
1812 		 */
1813 		xfs_iext_update_extent(bma->ip, state, &bma->icur, new);
1814 		ifp->if_nextents++;
1815 
1816 		if (bma->cur == NULL)
1817 			rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1818 		else {
1819 			rval = XFS_ILOG_CORE;
1820 			error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
1821 			if (error)
1822 				goto done;
1823 			if (XFS_IS_CORRUPT(mp, i != 0)) {
1824 				xfs_btree_mark_sick(bma->cur);
1825 				error = -EFSCORRUPTED;
1826 				goto done;
1827 			}
1828 			error = xfs_btree_insert(bma->cur, &i);
1829 			if (error)
1830 				goto done;
1831 			if (XFS_IS_CORRUPT(mp, i != 1)) {
1832 				xfs_btree_mark_sick(bma->cur);
1833 				error = -EFSCORRUPTED;
1834 				goto done;
1835 			}
1836 		}
1837 
1838 		if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
1839 			error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
1840 				&bma->cur, 1, &tmp_rval, whichfork);
1841 			rval |= tmp_rval;
1842 			if (error)
1843 				goto done;
1844 		}
1845 
1846 		temp = PREV.br_blockcount - new->br_blockcount;
1847 		da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1848 			startblockval(PREV.br_startblock) -
1849 			(bma->cur ? bma->cur->bc_bmap.allocated : 0));
1850 
1851 		PREV.br_startblock = nullstartblock(da_new);
1852 		PREV.br_blockcount = temp;
1853 		xfs_iext_insert(bma->ip, &bma->icur, &PREV, state);
1854 		xfs_iext_next(ifp, &bma->icur);
1855 		ASSERT(da_new <= da_old);
1856 		break;
1857 
1858 	case 0:
1859 		/*
1860 		 * Filling in the middle part of a previous delayed allocation.
1861 		 * Contiguity is impossible here.
1862 		 * This case is avoided almost all the time.
1863 		 *
1864 		 * We start with a delayed allocation:
1865 		 *
1866 		 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
1867 		 *  PREV @ idx
1868 		 *
1869 	         * and we are allocating:
1870 		 *                     +rrrrrrrrrrrrrrrrr+
1871 		 *			      new
1872 		 *
1873 		 * and we set it up for insertion as:
1874 		 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
1875 		 *                            new
1876 		 *  PREV @ idx          LEFT              RIGHT
1877 		 *                      inserted at idx + 1
1878 		 */
1879 		old = PREV;
1880 
1881 		/* LEFT is the new middle */
1882 		LEFT = *new;
1883 
1884 		/* RIGHT is the new right */
1885 		RIGHT.br_state = PREV.br_state;
1886 		RIGHT.br_startoff = new_endoff;
1887 		RIGHT.br_blockcount =
1888 			PREV.br_startoff + PREV.br_blockcount - new_endoff;
1889 		RIGHT.br_startblock =
1890 			nullstartblock(xfs_bmap_worst_indlen(bma->ip,
1891 					RIGHT.br_blockcount));
1892 
1893 		/* truncate PREV */
1894 		PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
1895 		PREV.br_startblock =
1896 			nullstartblock(xfs_bmap_worst_indlen(bma->ip,
1897 					PREV.br_blockcount));
1898 		xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1899 
1900 		xfs_iext_next(ifp, &bma->icur);
1901 		xfs_iext_insert(bma->ip, &bma->icur, &RIGHT, state);
1902 		xfs_iext_insert(bma->ip, &bma->icur, &LEFT, state);
1903 		ifp->if_nextents++;
1904 
1905 		if (bma->cur == NULL)
1906 			rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1907 		else {
1908 			rval = XFS_ILOG_CORE;
1909 			error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
1910 			if (error)
1911 				goto done;
1912 			if (XFS_IS_CORRUPT(mp, i != 0)) {
1913 				xfs_btree_mark_sick(bma->cur);
1914 				error = -EFSCORRUPTED;
1915 				goto done;
1916 			}
1917 			error = xfs_btree_insert(bma->cur, &i);
1918 			if (error)
1919 				goto done;
1920 			if (XFS_IS_CORRUPT(mp, i != 1)) {
1921 				xfs_btree_mark_sick(bma->cur);
1922 				error = -EFSCORRUPTED;
1923 				goto done;
1924 			}
1925 		}
1926 
1927 		if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
1928 			error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
1929 					&bma->cur, 1, &tmp_rval, whichfork);
1930 			rval |= tmp_rval;
1931 			if (error)
1932 				goto done;
1933 		}
1934 
1935 		da_new = startblockval(PREV.br_startblock) +
1936 			 startblockval(RIGHT.br_startblock);
1937 		break;
1938 
1939 	case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1940 	case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1941 	case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1942 	case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1943 	case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1944 	case BMAP_LEFT_CONTIG:
1945 	case BMAP_RIGHT_CONTIG:
1946 		/*
1947 		 * These cases are all impossible.
1948 		 */
1949 		ASSERT(0);
1950 	}
1951 
1952 	/* add reverse mapping unless caller opted out */
1953 	if (!(bma->flags & XFS_BMAPI_NORMAP))
1954 		xfs_rmap_map_extent(bma->tp, bma->ip, whichfork, new);
1955 
1956 	/* convert to a btree if necessary */
1957 	if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
1958 		int	tmp_logflags;	/* partial log flag return val */
1959 
1960 		ASSERT(bma->cur == NULL);
1961 		error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
1962 				&bma->cur, da_old > 0, &tmp_logflags,
1963 				whichfork);
1964 		bma->logflags |= tmp_logflags;
1965 		if (error)
1966 			goto done;
1967 	}
1968 
1969 	if (da_new != da_old)
1970 		xfs_mod_delalloc(bma->ip, 0, (int64_t)da_new - da_old);
1971 
1972 	if (bma->cur) {
1973 		da_new += bma->cur->bc_bmap.allocated;
1974 		bma->cur->bc_bmap.allocated = 0;
1975 	}
1976 
1977 	/* adjust for changes in reserved delayed indirect blocks */
1978 	if (da_new < da_old)
1979 		xfs_add_fdblocks(mp, da_old - da_new);
1980 	else if (da_new > da_old)
1981 		error = xfs_dec_fdblocks(mp, da_new - da_old, true);
1982 
1983 	xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
1984 done:
1985 	if (whichfork != XFS_COW_FORK)
1986 		bma->logflags |= rval;
1987 	return error;
1988 #undef	LEFT
1989 #undef	RIGHT
1990 #undef	PREV
1991 }
1992 
1993 /*
1994  * Convert an unwritten allocation to a real allocation or vice versa.
1995  */
1996 int					/* error */
1997 xfs_bmap_add_extent_unwritten_real(
1998 	struct xfs_trans	*tp,
1999 	xfs_inode_t		*ip,	/* incore inode pointer */
2000 	int			whichfork,
2001 	struct xfs_iext_cursor	*icur,
2002 	struct xfs_btree_cur	**curp,	/* if *curp is null, not a btree */
2003 	xfs_bmbt_irec_t		*new,	/* new data to add to file extents */
2004 	int			*logflagsp) /* inode logging flags */
2005 {
2006 	struct xfs_btree_cur	*cur;	/* btree cursor */
2007 	int			error;	/* error return value */
2008 	int			i;	/* temp state */
2009 	struct xfs_ifork	*ifp;	/* inode fork pointer */
2010 	xfs_fileoff_t		new_endoff;	/* end offset of new entry */
2011 	xfs_bmbt_irec_t		r[3];	/* neighbor extent entries */
2012 					/* left is 0, right is 1, prev is 2 */
2013 	int			rval=0;	/* return value (logging flags) */
2014 	uint32_t		state = xfs_bmap_fork_to_state(whichfork);
2015 	struct xfs_mount	*mp = ip->i_mount;
2016 	struct xfs_bmbt_irec	old;
2017 
2018 	*logflagsp = 0;
2019 
2020 	cur = *curp;
2021 	ifp = xfs_ifork_ptr(ip, whichfork);
2022 
2023 	ASSERT(!isnullstartblock(new->br_startblock));
2024 
2025 	XFS_STATS_INC(mp, xs_add_exlist);
2026 
2027 #define	LEFT		r[0]
2028 #define	RIGHT		r[1]
2029 #define	PREV		r[2]
2030 
2031 	/*
2032 	 * Set up a bunch of variables to make the tests simpler.
2033 	 */
2034 	error = 0;
2035 	xfs_iext_get_extent(ifp, icur, &PREV);
2036 	ASSERT(new->br_state != PREV.br_state);
2037 	new_endoff = new->br_startoff + new->br_blockcount;
2038 	ASSERT(PREV.br_startoff <= new->br_startoff);
2039 	ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
2040 
2041 	/*
2042 	 * Set flags determining what part of the previous oldext allocation
2043 	 * extent is being replaced by a newext allocation.
2044 	 */
2045 	if (PREV.br_startoff == new->br_startoff)
2046 		state |= BMAP_LEFT_FILLING;
2047 	if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
2048 		state |= BMAP_RIGHT_FILLING;
2049 
2050 	/*
2051 	 * Check and set flags if this segment has a left neighbor.
2052 	 * Don't set contiguous if the combined extent would be too large.
2053 	 */
2054 	if (xfs_iext_peek_prev_extent(ifp, icur, &LEFT)) {
2055 		state |= BMAP_LEFT_VALID;
2056 		if (isnullstartblock(LEFT.br_startblock))
2057 			state |= BMAP_LEFT_DELAY;
2058 	}
2059 
2060 	if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2061 	    LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
2062 	    LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
2063 	    LEFT.br_state == new->br_state &&
2064 	    LEFT.br_blockcount + new->br_blockcount <= XFS_MAX_BMBT_EXTLEN)
2065 		state |= BMAP_LEFT_CONTIG;
2066 
2067 	/*
2068 	 * Check and set flags if this segment has a right neighbor.
2069 	 * Don't set contiguous if the combined extent would be too large.
2070 	 * Also check for all-three-contiguous being too large.
2071 	 */
2072 	if (xfs_iext_peek_next_extent(ifp, icur, &RIGHT)) {
2073 		state |= BMAP_RIGHT_VALID;
2074 		if (isnullstartblock(RIGHT.br_startblock))
2075 			state |= BMAP_RIGHT_DELAY;
2076 	}
2077 
2078 	if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2079 	    new_endoff == RIGHT.br_startoff &&
2080 	    new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
2081 	    new->br_state == RIGHT.br_state &&
2082 	    new->br_blockcount + RIGHT.br_blockcount <= XFS_MAX_BMBT_EXTLEN &&
2083 	    ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2084 		       BMAP_RIGHT_FILLING)) !=
2085 		      (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2086 		       BMAP_RIGHT_FILLING) ||
2087 	     LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
2088 			<= XFS_MAX_BMBT_EXTLEN))
2089 		state |= BMAP_RIGHT_CONTIG;
2090 
2091 	/*
2092 	 * Switch out based on the FILLING and CONTIG state bits.
2093 	 */
2094 	switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2095 			 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
2096 	case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2097 	     BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
2098 		/*
2099 		 * Setting all of a previous oldext extent to newext.
2100 		 * The left and right neighbors are both contiguous with new.
2101 		 */
2102 		LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
2103 
2104 		xfs_iext_remove(ip, icur, state);
2105 		xfs_iext_remove(ip, icur, state);
2106 		xfs_iext_prev(ifp, icur);
2107 		xfs_iext_update_extent(ip, state, icur, &LEFT);
2108 		ifp->if_nextents -= 2;
2109 		if (cur == NULL)
2110 			rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2111 		else {
2112 			rval = XFS_ILOG_CORE;
2113 			error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
2114 			if (error)
2115 				goto done;
2116 			if (XFS_IS_CORRUPT(mp, i != 1)) {
2117 				xfs_btree_mark_sick(cur);
2118 				error = -EFSCORRUPTED;
2119 				goto done;
2120 			}
2121 			if ((error = xfs_btree_delete(cur, &i)))
2122 				goto done;
2123 			if (XFS_IS_CORRUPT(mp, i != 1)) {
2124 				xfs_btree_mark_sick(cur);
2125 				error = -EFSCORRUPTED;
2126 				goto done;
2127 			}
2128 			if ((error = xfs_btree_decrement(cur, 0, &i)))
2129 				goto done;
2130 			if (XFS_IS_CORRUPT(mp, i != 1)) {
2131 				xfs_btree_mark_sick(cur);
2132 				error = -EFSCORRUPTED;
2133 				goto done;
2134 			}
2135 			if ((error = xfs_btree_delete(cur, &i)))
2136 				goto done;
2137 			if (XFS_IS_CORRUPT(mp, i != 1)) {
2138 				xfs_btree_mark_sick(cur);
2139 				error = -EFSCORRUPTED;
2140 				goto done;
2141 			}
2142 			if ((error = xfs_btree_decrement(cur, 0, &i)))
2143 				goto done;
2144 			if (XFS_IS_CORRUPT(mp, i != 1)) {
2145 				xfs_btree_mark_sick(cur);
2146 				error = -EFSCORRUPTED;
2147 				goto done;
2148 			}
2149 			error = xfs_bmbt_update(cur, &LEFT);
2150 			if (error)
2151 				goto done;
2152 		}
2153 		break;
2154 
2155 	case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2156 		/*
2157 		 * Setting all of a previous oldext extent to newext.
2158 		 * The left neighbor is contiguous, the right is not.
2159 		 */
2160 		LEFT.br_blockcount += PREV.br_blockcount;
2161 
2162 		xfs_iext_remove(ip, icur, state);
2163 		xfs_iext_prev(ifp, icur);
2164 		xfs_iext_update_extent(ip, state, icur, &LEFT);
2165 		ifp->if_nextents--;
2166 		if (cur == NULL)
2167 			rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2168 		else {
2169 			rval = XFS_ILOG_CORE;
2170 			error = xfs_bmbt_lookup_eq(cur, &PREV, &i);
2171 			if (error)
2172 				goto done;
2173 			if (XFS_IS_CORRUPT(mp, i != 1)) {
2174 				xfs_btree_mark_sick(cur);
2175 				error = -EFSCORRUPTED;
2176 				goto done;
2177 			}
2178 			if ((error = xfs_btree_delete(cur, &i)))
2179 				goto done;
2180 			if (XFS_IS_CORRUPT(mp, i != 1)) {
2181 				xfs_btree_mark_sick(cur);
2182 				error = -EFSCORRUPTED;
2183 				goto done;
2184 			}
2185 			if ((error = xfs_btree_decrement(cur, 0, &i)))
2186 				goto done;
2187 			if (XFS_IS_CORRUPT(mp, i != 1)) {
2188 				xfs_btree_mark_sick(cur);
2189 				error = -EFSCORRUPTED;
2190 				goto done;
2191 			}
2192 			error = xfs_bmbt_update(cur, &LEFT);
2193 			if (error)
2194 				goto done;
2195 		}
2196 		break;
2197 
2198 	case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
2199 		/*
2200 		 * Setting all of a previous oldext extent to newext.
2201 		 * The right neighbor is contiguous, the left is not.
2202 		 */
2203 		PREV.br_blockcount += RIGHT.br_blockcount;
2204 		PREV.br_state = new->br_state;
2205 
2206 		xfs_iext_next(ifp, icur);
2207 		xfs_iext_remove(ip, icur, state);
2208 		xfs_iext_prev(ifp, icur);
2209 		xfs_iext_update_extent(ip, state, icur, &PREV);
2210 		ifp->if_nextents--;
2211 
2212 		if (cur == NULL)
2213 			rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2214 		else {
2215 			rval = XFS_ILOG_CORE;
2216 			error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
2217 			if (error)
2218 				goto done;
2219 			if (XFS_IS_CORRUPT(mp, i != 1)) {
2220 				xfs_btree_mark_sick(cur);
2221 				error = -EFSCORRUPTED;
2222 				goto done;
2223 			}
2224 			if ((error = xfs_btree_delete(cur, &i)))
2225 				goto done;
2226 			if (XFS_IS_CORRUPT(mp, i != 1)) {
2227 				xfs_btree_mark_sick(cur);
2228 				error = -EFSCORRUPTED;
2229 				goto done;
2230 			}
2231 			if ((error = xfs_btree_decrement(cur, 0, &i)))
2232 				goto done;
2233 			if (XFS_IS_CORRUPT(mp, i != 1)) {
2234 				xfs_btree_mark_sick(cur);
2235 				error = -EFSCORRUPTED;
2236 				goto done;
2237 			}
2238 			error = xfs_bmbt_update(cur, &PREV);
2239 			if (error)
2240 				goto done;
2241 		}
2242 		break;
2243 
2244 	case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
2245 		/*
2246 		 * Setting all of a previous oldext extent to newext.
2247 		 * Neither the left nor right neighbors are contiguous with
2248 		 * the new one.
2249 		 */
2250 		PREV.br_state = new->br_state;
2251 		xfs_iext_update_extent(ip, state, icur, &PREV);
2252 
2253 		if (cur == NULL)
2254 			rval = XFS_ILOG_DEXT;
2255 		else {
2256 			rval = 0;
2257 			error = xfs_bmbt_lookup_eq(cur, new, &i);
2258 			if (error)
2259 				goto done;
2260 			if (XFS_IS_CORRUPT(mp, i != 1)) {
2261 				xfs_btree_mark_sick(cur);
2262 				error = -EFSCORRUPTED;
2263 				goto done;
2264 			}
2265 			error = xfs_bmbt_update(cur, &PREV);
2266 			if (error)
2267 				goto done;
2268 		}
2269 		break;
2270 
2271 	case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
2272 		/*
2273 		 * Setting the first part of a previous oldext extent to newext.
2274 		 * The left neighbor is contiguous.
2275 		 */
2276 		LEFT.br_blockcount += new->br_blockcount;
2277 
2278 		old = PREV;
2279 		PREV.br_startoff += new->br_blockcount;
2280 		PREV.br_startblock += new->br_blockcount;
2281 		PREV.br_blockcount -= new->br_blockcount;
2282 
2283 		xfs_iext_update_extent(ip, state, icur, &PREV);
2284 		xfs_iext_prev(ifp, icur);
2285 		xfs_iext_update_extent(ip, state, icur, &LEFT);
2286 
2287 		if (cur == NULL)
2288 			rval = XFS_ILOG_DEXT;
2289 		else {
2290 			rval = 0;
2291 			error = xfs_bmbt_lookup_eq(cur, &old, &i);
2292 			if (error)
2293 				goto done;
2294 			if (XFS_IS_CORRUPT(mp, i != 1)) {
2295 				xfs_btree_mark_sick(cur);
2296 				error = -EFSCORRUPTED;
2297 				goto done;
2298 			}
2299 			error = xfs_bmbt_update(cur, &PREV);
2300 			if (error)
2301 				goto done;
2302 			error = xfs_btree_decrement(cur, 0, &i);
2303 			if (error)
2304 				goto done;
2305 			error = xfs_bmbt_update(cur, &LEFT);
2306 			if (error)
2307 				goto done;
2308 		}
2309 		break;
2310 
2311 	case BMAP_LEFT_FILLING:
2312 		/*
2313 		 * Setting the first part of a previous oldext extent to newext.
2314 		 * The left neighbor is not contiguous.
2315 		 */
2316 		old = PREV;
2317 		PREV.br_startoff += new->br_blockcount;
2318 		PREV.br_startblock += new->br_blockcount;
2319 		PREV.br_blockcount -= new->br_blockcount;
2320 
2321 		xfs_iext_update_extent(ip, state, icur, &PREV);
2322 		xfs_iext_insert(ip, icur, new, state);
2323 		ifp->if_nextents++;
2324 
2325 		if (cur == NULL)
2326 			rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2327 		else {
2328 			rval = XFS_ILOG_CORE;
2329 			error = xfs_bmbt_lookup_eq(cur, &old, &i);
2330 			if (error)
2331 				goto done;
2332 			if (XFS_IS_CORRUPT(mp, i != 1)) {
2333 				xfs_btree_mark_sick(cur);
2334 				error = -EFSCORRUPTED;
2335 				goto done;
2336 			}
2337 			error = xfs_bmbt_update(cur, &PREV);
2338 			if (error)
2339 				goto done;
2340 			cur->bc_rec.b = *new;
2341 			if ((error = xfs_btree_insert(cur, &i)))
2342 				goto done;
2343 			if (XFS_IS_CORRUPT(mp, i != 1)) {
2344 				xfs_btree_mark_sick(cur);
2345 				error = -EFSCORRUPTED;
2346 				goto done;
2347 			}
2348 		}
2349 		break;
2350 
2351 	case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
2352 		/*
2353 		 * Setting the last part of a previous oldext extent to newext.
2354 		 * The right neighbor is contiguous with the new allocation.
2355 		 */
2356 		old = PREV;
2357 		PREV.br_blockcount -= new->br_blockcount;
2358 
2359 		RIGHT.br_startoff = new->br_startoff;
2360 		RIGHT.br_startblock = new->br_startblock;
2361 		RIGHT.br_blockcount += new->br_blockcount;
2362 
2363 		xfs_iext_update_extent(ip, state, icur, &PREV);
2364 		xfs_iext_next(ifp, icur);
2365 		xfs_iext_update_extent(ip, state, icur, &RIGHT);
2366 
2367 		if (cur == NULL)
2368 			rval = XFS_ILOG_DEXT;
2369 		else {
2370 			rval = 0;
2371 			error = xfs_bmbt_lookup_eq(cur, &old, &i);
2372 			if (error)
2373 				goto done;
2374 			if (XFS_IS_CORRUPT(mp, i != 1)) {
2375 				xfs_btree_mark_sick(cur);
2376 				error = -EFSCORRUPTED;
2377 				goto done;
2378 			}
2379 			error = xfs_bmbt_update(cur, &PREV);
2380 			if (error)
2381 				goto done;
2382 			error = xfs_btree_increment(cur, 0, &i);
2383 			if (error)
2384 				goto done;
2385 			error = xfs_bmbt_update(cur, &RIGHT);
2386 			if (error)
2387 				goto done;
2388 		}
2389 		break;
2390 
2391 	case BMAP_RIGHT_FILLING:
2392 		/*
2393 		 * Setting the last part of a previous oldext extent to newext.
2394 		 * The right neighbor is not contiguous.
2395 		 */
2396 		old = PREV;
2397 		PREV.br_blockcount -= new->br_blockcount;
2398 
2399 		xfs_iext_update_extent(ip, state, icur, &PREV);
2400 		xfs_iext_next(ifp, icur);
2401 		xfs_iext_insert(ip, icur, new, state);
2402 		ifp->if_nextents++;
2403 
2404 		if (cur == NULL)
2405 			rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2406 		else {
2407 			rval = XFS_ILOG_CORE;
2408 			error = xfs_bmbt_lookup_eq(cur, &old, &i);
2409 			if (error)
2410 				goto done;
2411 			if (XFS_IS_CORRUPT(mp, i != 1)) {
2412 				xfs_btree_mark_sick(cur);
2413 				error = -EFSCORRUPTED;
2414 				goto done;
2415 			}
2416 			error = xfs_bmbt_update(cur, &PREV);
2417 			if (error)
2418 				goto done;
2419 			error = xfs_bmbt_lookup_eq(cur, new, &i);
2420 			if (error)
2421 				goto done;
2422 			if (XFS_IS_CORRUPT(mp, i != 0)) {
2423 				xfs_btree_mark_sick(cur);
2424 				error = -EFSCORRUPTED;
2425 				goto done;
2426 			}
2427 			if ((error = xfs_btree_insert(cur, &i)))
2428 				goto done;
2429 			if (XFS_IS_CORRUPT(mp, i != 1)) {
2430 				xfs_btree_mark_sick(cur);
2431 				error = -EFSCORRUPTED;
2432 				goto done;
2433 			}
2434 		}
2435 		break;
2436 
2437 	case 0:
2438 		/*
2439 		 * Setting the middle part of a previous oldext extent to
2440 		 * newext.  Contiguity is impossible here.
2441 		 * One extent becomes three extents.
2442 		 */
2443 		old = PREV;
2444 		PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
2445 
2446 		r[0] = *new;
2447 		r[1].br_startoff = new_endoff;
2448 		r[1].br_blockcount =
2449 			old.br_startoff + old.br_blockcount - new_endoff;
2450 		r[1].br_startblock = new->br_startblock + new->br_blockcount;
2451 		r[1].br_state = PREV.br_state;
2452 
2453 		xfs_iext_update_extent(ip, state, icur, &PREV);
2454 		xfs_iext_next(ifp, icur);
2455 		xfs_iext_insert(ip, icur, &r[1], state);
2456 		xfs_iext_insert(ip, icur, &r[0], state);
2457 		ifp->if_nextents += 2;
2458 
2459 		if (cur == NULL)
2460 			rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2461 		else {
2462 			rval = XFS_ILOG_CORE;
2463 			error = xfs_bmbt_lookup_eq(cur, &old, &i);
2464 			if (error)
2465 				goto done;
2466 			if (XFS_IS_CORRUPT(mp, i != 1)) {
2467 				xfs_btree_mark_sick(cur);
2468 				error = -EFSCORRUPTED;
2469 				goto done;
2470 			}
2471 			/* new right extent - oldext */
2472 			error = xfs_bmbt_update(cur, &r[1]);
2473 			if (error)
2474 				goto done;
2475 			/* new left extent - oldext */
2476 			cur->bc_rec.b = PREV;
2477 			if ((error = xfs_btree_insert(cur, &i)))
2478 				goto done;
2479 			if (XFS_IS_CORRUPT(mp, i != 1)) {
2480 				xfs_btree_mark_sick(cur);
2481 				error = -EFSCORRUPTED;
2482 				goto done;
2483 			}
2484 			/*
2485 			 * Reset the cursor to the position of the new extent
2486 			 * we are about to insert as we can't trust it after
2487 			 * the previous insert.
2488 			 */
2489 			error = xfs_bmbt_lookup_eq(cur, new, &i);
2490 			if (error)
2491 				goto done;
2492 			if (XFS_IS_CORRUPT(mp, i != 0)) {
2493 				xfs_btree_mark_sick(cur);
2494 				error = -EFSCORRUPTED;
2495 				goto done;
2496 			}
2497 			/* new middle extent - newext */
2498 			if ((error = xfs_btree_insert(cur, &i)))
2499 				goto done;
2500 			if (XFS_IS_CORRUPT(mp, i != 1)) {
2501 				xfs_btree_mark_sick(cur);
2502 				error = -EFSCORRUPTED;
2503 				goto done;
2504 			}
2505 		}
2506 		break;
2507 
2508 	case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2509 	case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2510 	case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2511 	case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2512 	case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2513 	case BMAP_LEFT_CONTIG:
2514 	case BMAP_RIGHT_CONTIG:
2515 		/*
2516 		 * These cases are all impossible.
2517 		 */
2518 		ASSERT(0);
2519 	}
2520 
2521 	/* update reverse mappings */
2522 	xfs_rmap_convert_extent(mp, tp, ip, whichfork, new);
2523 
2524 	/* convert to a btree if necessary */
2525 	if (xfs_bmap_needs_btree(ip, whichfork)) {
2526 		int	tmp_logflags;	/* partial log flag return val */
2527 
2528 		ASSERT(cur == NULL);
2529 		error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0,
2530 				&tmp_logflags, whichfork);
2531 		*logflagsp |= tmp_logflags;
2532 		if (error)
2533 			goto done;
2534 	}
2535 
2536 	/* clear out the allocated field, done with it now in any case. */
2537 	if (cur) {
2538 		cur->bc_bmap.allocated = 0;
2539 		*curp = cur;
2540 	}
2541 
2542 	xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
2543 done:
2544 	*logflagsp |= rval;
2545 	return error;
2546 #undef	LEFT
2547 #undef	RIGHT
2548 #undef	PREV
2549 }
2550 
2551 /*
2552  * Convert a hole to a delayed allocation.
2553  */
2554 STATIC void
2555 xfs_bmap_add_extent_hole_delay(
2556 	xfs_inode_t		*ip,	/* incore inode pointer */
2557 	int			whichfork,
2558 	struct xfs_iext_cursor	*icur,
2559 	xfs_bmbt_irec_t		*new)	/* new data to add to file extents */
2560 {
2561 	struct xfs_ifork	*ifp;	/* inode fork pointer */
2562 	xfs_bmbt_irec_t		left;	/* left neighbor extent entry */
2563 	xfs_filblks_t		newlen=0;	/* new indirect size */
2564 	xfs_filblks_t		oldlen=0;	/* old indirect size */
2565 	xfs_bmbt_irec_t		right;	/* right neighbor extent entry */
2566 	uint32_t		state = xfs_bmap_fork_to_state(whichfork);
2567 	xfs_filblks_t		temp;	 /* temp for indirect calculations */
2568 
2569 	ifp = xfs_ifork_ptr(ip, whichfork);
2570 	ASSERT(isnullstartblock(new->br_startblock));
2571 
2572 	/*
2573 	 * Check and set flags if this segment has a left neighbor
2574 	 */
2575 	if (xfs_iext_peek_prev_extent(ifp, icur, &left)) {
2576 		state |= BMAP_LEFT_VALID;
2577 		if (isnullstartblock(left.br_startblock))
2578 			state |= BMAP_LEFT_DELAY;
2579 	}
2580 
2581 	/*
2582 	 * Check and set flags if the current (right) segment exists.
2583 	 * If it doesn't exist, we're converting the hole at end-of-file.
2584 	 */
2585 	if (xfs_iext_get_extent(ifp, icur, &right)) {
2586 		state |= BMAP_RIGHT_VALID;
2587 		if (isnullstartblock(right.br_startblock))
2588 			state |= BMAP_RIGHT_DELAY;
2589 	}
2590 
2591 	/*
2592 	 * Set contiguity flags on the left and right neighbors.
2593 	 * Don't let extents get too large, even if the pieces are contiguous.
2594 	 */
2595 	if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
2596 	    left.br_startoff + left.br_blockcount == new->br_startoff &&
2597 	    left.br_blockcount + new->br_blockcount <= XFS_MAX_BMBT_EXTLEN)
2598 		state |= BMAP_LEFT_CONTIG;
2599 
2600 	if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
2601 	    new->br_startoff + new->br_blockcount == right.br_startoff &&
2602 	    new->br_blockcount + right.br_blockcount <= XFS_MAX_BMBT_EXTLEN &&
2603 	    (!(state & BMAP_LEFT_CONTIG) ||
2604 	     (left.br_blockcount + new->br_blockcount +
2605 	      right.br_blockcount <= XFS_MAX_BMBT_EXTLEN)))
2606 		state |= BMAP_RIGHT_CONTIG;
2607 
2608 	/*
2609 	 * Switch out based on the contiguity flags.
2610 	 */
2611 	switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2612 	case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2613 		/*
2614 		 * New allocation is contiguous with delayed allocations
2615 		 * on the left and on the right.
2616 		 * Merge all three into a single extent record.
2617 		 */
2618 		temp = left.br_blockcount + new->br_blockcount +
2619 			right.br_blockcount;
2620 
2621 		oldlen = startblockval(left.br_startblock) +
2622 			startblockval(new->br_startblock) +
2623 			startblockval(right.br_startblock);
2624 		newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2625 					 oldlen);
2626 		left.br_startblock = nullstartblock(newlen);
2627 		left.br_blockcount = temp;
2628 
2629 		xfs_iext_remove(ip, icur, state);
2630 		xfs_iext_prev(ifp, icur);
2631 		xfs_iext_update_extent(ip, state, icur, &left);
2632 		break;
2633 
2634 	case BMAP_LEFT_CONTIG:
2635 		/*
2636 		 * New allocation is contiguous with a delayed allocation
2637 		 * on the left.
2638 		 * Merge the new allocation with the left neighbor.
2639 		 */
2640 		temp = left.br_blockcount + new->br_blockcount;
2641 
2642 		oldlen = startblockval(left.br_startblock) +
2643 			startblockval(new->br_startblock);
2644 		newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2645 					 oldlen);
2646 		left.br_blockcount = temp;
2647 		left.br_startblock = nullstartblock(newlen);
2648 
2649 		xfs_iext_prev(ifp, icur);
2650 		xfs_iext_update_extent(ip, state, icur, &left);
2651 		break;
2652 
2653 	case BMAP_RIGHT_CONTIG:
2654 		/*
2655 		 * New allocation is contiguous with a delayed allocation
2656 		 * on the right.
2657 		 * Merge the new allocation with the right neighbor.
2658 		 */
2659 		temp = new->br_blockcount + right.br_blockcount;
2660 		oldlen = startblockval(new->br_startblock) +
2661 			startblockval(right.br_startblock);
2662 		newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2663 					 oldlen);
2664 		right.br_startoff = new->br_startoff;
2665 		right.br_startblock = nullstartblock(newlen);
2666 		right.br_blockcount = temp;
2667 		xfs_iext_update_extent(ip, state, icur, &right);
2668 		break;
2669 
2670 	case 0:
2671 		/*
2672 		 * New allocation is not contiguous with another
2673 		 * delayed allocation.
2674 		 * Insert a new entry.
2675 		 */
2676 		oldlen = newlen = 0;
2677 		xfs_iext_insert(ip, icur, new, state);
2678 		break;
2679 	}
2680 	if (oldlen != newlen) {
2681 		ASSERT(oldlen > newlen);
2682 		xfs_add_fdblocks(ip->i_mount, oldlen - newlen);
2683 
2684 		/*
2685 		 * Nothing to do for disk quota accounting here.
2686 		 */
2687 		xfs_mod_delalloc(ip, 0, (int64_t)newlen - oldlen);
2688 	}
2689 }
2690 
2691 /*
2692  * Convert a hole to a real allocation.
2693  */
2694 STATIC int				/* error */
2695 xfs_bmap_add_extent_hole_real(
2696 	struct xfs_trans	*tp,
2697 	struct xfs_inode	*ip,
2698 	int			whichfork,
2699 	struct xfs_iext_cursor	*icur,
2700 	struct xfs_btree_cur	**curp,
2701 	struct xfs_bmbt_irec	*new,
2702 	int			*logflagsp,
2703 	uint32_t		flags)
2704 {
2705 	struct xfs_ifork	*ifp = xfs_ifork_ptr(ip, whichfork);
2706 	struct xfs_mount	*mp = ip->i_mount;
2707 	struct xfs_btree_cur	*cur = *curp;
2708 	int			error;	/* error return value */
2709 	int			i;	/* temp state */
2710 	xfs_bmbt_irec_t		left;	/* left neighbor extent entry */
2711 	xfs_bmbt_irec_t		right;	/* right neighbor extent entry */
2712 	int			rval=0;	/* return value (logging flags) */
2713 	uint32_t		state = xfs_bmap_fork_to_state(whichfork);
2714 	struct xfs_bmbt_irec	old;
2715 
2716 	ASSERT(!isnullstartblock(new->br_startblock));
2717 	ASSERT(!cur || !(cur->bc_flags & XFS_BTREE_BMBT_WASDEL));
2718 
2719 	XFS_STATS_INC(mp, xs_add_exlist);
2720 
2721 	/*
2722 	 * Check and set flags if this segment has a left neighbor.
2723 	 */
2724 	if (xfs_iext_peek_prev_extent(ifp, icur, &left)) {
2725 		state |= BMAP_LEFT_VALID;
2726 		if (isnullstartblock(left.br_startblock))
2727 			state |= BMAP_LEFT_DELAY;
2728 	}
2729 
2730 	/*
2731 	 * Check and set flags if this segment has a current value.
2732 	 * Not true if we're inserting into the "hole" at eof.
2733 	 */
2734 	if (xfs_iext_get_extent(ifp, icur, &right)) {
2735 		state |= BMAP_RIGHT_VALID;
2736 		if (isnullstartblock(right.br_startblock))
2737 			state |= BMAP_RIGHT_DELAY;
2738 	}
2739 
2740 	/*
2741 	 * We're inserting a real allocation between "left" and "right".
2742 	 * Set the contiguity flags.  Don't let extents get too large.
2743 	 */
2744 	if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2745 	    left.br_startoff + left.br_blockcount == new->br_startoff &&
2746 	    left.br_startblock + left.br_blockcount == new->br_startblock &&
2747 	    left.br_state == new->br_state &&
2748 	    left.br_blockcount + new->br_blockcount <= XFS_MAX_BMBT_EXTLEN)
2749 		state |= BMAP_LEFT_CONTIG;
2750 
2751 	if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2752 	    new->br_startoff + new->br_blockcount == right.br_startoff &&
2753 	    new->br_startblock + new->br_blockcount == right.br_startblock &&
2754 	    new->br_state == right.br_state &&
2755 	    new->br_blockcount + right.br_blockcount <= XFS_MAX_BMBT_EXTLEN &&
2756 	    (!(state & BMAP_LEFT_CONTIG) ||
2757 	     left.br_blockcount + new->br_blockcount +
2758 	     right.br_blockcount <= XFS_MAX_BMBT_EXTLEN))
2759 		state |= BMAP_RIGHT_CONTIG;
2760 
2761 	error = 0;
2762 	/*
2763 	 * Select which case we're in here, and implement it.
2764 	 */
2765 	switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2766 	case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2767 		/*
2768 		 * New allocation is contiguous with real allocations on the
2769 		 * left and on the right.
2770 		 * Merge all three into a single extent record.
2771 		 */
2772 		left.br_blockcount += new->br_blockcount + right.br_blockcount;
2773 
2774 		xfs_iext_remove(ip, icur, state);
2775 		xfs_iext_prev(ifp, icur);
2776 		xfs_iext_update_extent(ip, state, icur, &left);
2777 		ifp->if_nextents--;
2778 
2779 		if (cur == NULL) {
2780 			rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
2781 		} else {
2782 			rval = XFS_ILOG_CORE;
2783 			error = xfs_bmbt_lookup_eq(cur, &right, &i);
2784 			if (error)
2785 				goto done;
2786 			if (XFS_IS_CORRUPT(mp, i != 1)) {
2787 				xfs_btree_mark_sick(cur);
2788 				error = -EFSCORRUPTED;
2789 				goto done;
2790 			}
2791 			error = xfs_btree_delete(cur, &i);
2792 			if (error)
2793 				goto done;
2794 			if (XFS_IS_CORRUPT(mp, i != 1)) {
2795 				xfs_btree_mark_sick(cur);
2796 				error = -EFSCORRUPTED;
2797 				goto done;
2798 			}
2799 			error = xfs_btree_decrement(cur, 0, &i);
2800 			if (error)
2801 				goto done;
2802 			if (XFS_IS_CORRUPT(mp, i != 1)) {
2803 				xfs_btree_mark_sick(cur);
2804 				error = -EFSCORRUPTED;
2805 				goto done;
2806 			}
2807 			error = xfs_bmbt_update(cur, &left);
2808 			if (error)
2809 				goto done;
2810 		}
2811 		break;
2812 
2813 	case BMAP_LEFT_CONTIG:
2814 		/*
2815 		 * New allocation is contiguous with a real allocation
2816 		 * on the left.
2817 		 * Merge the new allocation with the left neighbor.
2818 		 */
2819 		old = left;
2820 		left.br_blockcount += new->br_blockcount;
2821 
2822 		xfs_iext_prev(ifp, icur);
2823 		xfs_iext_update_extent(ip, state, icur, &left);
2824 
2825 		if (cur == NULL) {
2826 			rval = xfs_ilog_fext(whichfork);
2827 		} else {
2828 			rval = 0;
2829 			error = xfs_bmbt_lookup_eq(cur, &old, &i);
2830 			if (error)
2831 				goto done;
2832 			if (XFS_IS_CORRUPT(mp, i != 1)) {
2833 				xfs_btree_mark_sick(cur);
2834 				error = -EFSCORRUPTED;
2835 				goto done;
2836 			}
2837 			error = xfs_bmbt_update(cur, &left);
2838 			if (error)
2839 				goto done;
2840 		}
2841 		break;
2842 
2843 	case BMAP_RIGHT_CONTIG:
2844 		/*
2845 		 * New allocation is contiguous with a real allocation
2846 		 * on the right.
2847 		 * Merge the new allocation with the right neighbor.
2848 		 */
2849 		old = right;
2850 
2851 		right.br_startoff = new->br_startoff;
2852 		right.br_startblock = new->br_startblock;
2853 		right.br_blockcount += new->br_blockcount;
2854 		xfs_iext_update_extent(ip, state, icur, &right);
2855 
2856 		if (cur == NULL) {
2857 			rval = xfs_ilog_fext(whichfork);
2858 		} else {
2859 			rval = 0;
2860 			error = xfs_bmbt_lookup_eq(cur, &old, &i);
2861 			if (error)
2862 				goto done;
2863 			if (XFS_IS_CORRUPT(mp, i != 1)) {
2864 				xfs_btree_mark_sick(cur);
2865 				error = -EFSCORRUPTED;
2866 				goto done;
2867 			}
2868 			error = xfs_bmbt_update(cur, &right);
2869 			if (error)
2870 				goto done;
2871 		}
2872 		break;
2873 
2874 	case 0:
2875 		/*
2876 		 * New allocation is not contiguous with another
2877 		 * real allocation.
2878 		 * Insert a new entry.
2879 		 */
2880 		xfs_iext_insert(ip, icur, new, state);
2881 		ifp->if_nextents++;
2882 
2883 		if (cur == NULL) {
2884 			rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
2885 		} else {
2886 			rval = XFS_ILOG_CORE;
2887 			error = xfs_bmbt_lookup_eq(cur, new, &i);
2888 			if (error)
2889 				goto done;
2890 			if (XFS_IS_CORRUPT(mp, i != 0)) {
2891 				xfs_btree_mark_sick(cur);
2892 				error = -EFSCORRUPTED;
2893 				goto done;
2894 			}
2895 			error = xfs_btree_insert(cur, &i);
2896 			if (error)
2897 				goto done;
2898 			if (XFS_IS_CORRUPT(mp, i != 1)) {
2899 				xfs_btree_mark_sick(cur);
2900 				error = -EFSCORRUPTED;
2901 				goto done;
2902 			}
2903 		}
2904 		break;
2905 	}
2906 
2907 	/* add reverse mapping unless caller opted out */
2908 	if (!(flags & XFS_BMAPI_NORMAP))
2909 		xfs_rmap_map_extent(tp, ip, whichfork, new);
2910 
2911 	/* convert to a btree if necessary */
2912 	if (xfs_bmap_needs_btree(ip, whichfork)) {
2913 		int	tmp_logflags;	/* partial log flag return val */
2914 
2915 		ASSERT(cur == NULL);
2916 		error = xfs_bmap_extents_to_btree(tp, ip, curp, 0,
2917 				&tmp_logflags, whichfork);
2918 		*logflagsp |= tmp_logflags;
2919 		cur = *curp;
2920 		if (error)
2921 			goto done;
2922 	}
2923 
2924 	/* clear out the allocated field, done with it now in any case. */
2925 	if (cur)
2926 		cur->bc_bmap.allocated = 0;
2927 
2928 	xfs_bmap_check_leaf_extents(cur, ip, whichfork);
2929 done:
2930 	*logflagsp |= rval;
2931 	return error;
2932 }
2933 
2934 /*
2935  * Functions used in the extent read, allocate and remove paths
2936  */
2937 
2938 /*
2939  * Adjust the size of the new extent based on i_extsize and rt extsize.
2940  */
2941 int
2942 xfs_bmap_extsize_align(
2943 	xfs_mount_t	*mp,
2944 	xfs_bmbt_irec_t	*gotp,		/* next extent pointer */
2945 	xfs_bmbt_irec_t	*prevp,		/* previous extent pointer */
2946 	xfs_extlen_t	extsz,		/* align to this extent size */
2947 	int		rt,		/* is this a realtime inode? */
2948 	int		eof,		/* is extent at end-of-file? */
2949 	int		delay,		/* creating delalloc extent? */
2950 	int		convert,	/* overwriting unwritten extent? */
2951 	xfs_fileoff_t	*offp,		/* in/out: aligned offset */
2952 	xfs_extlen_t	*lenp)		/* in/out: aligned length */
2953 {
2954 	xfs_fileoff_t	orig_off;	/* original offset */
2955 	xfs_extlen_t	orig_alen;	/* original length */
2956 	xfs_fileoff_t	orig_end;	/* original off+len */
2957 	xfs_fileoff_t	nexto;		/* next file offset */
2958 	xfs_fileoff_t	prevo;		/* previous file offset */
2959 	xfs_fileoff_t	align_off;	/* temp for offset */
2960 	xfs_extlen_t	align_alen;	/* temp for length */
2961 	xfs_extlen_t	temp;		/* temp for calculations */
2962 
2963 	if (convert)
2964 		return 0;
2965 
2966 	orig_off = align_off = *offp;
2967 	orig_alen = align_alen = *lenp;
2968 	orig_end = orig_off + orig_alen;
2969 
2970 	/*
2971 	 * If this request overlaps an existing extent, then don't
2972 	 * attempt to perform any additional alignment.
2973 	 */
2974 	if (!delay && !eof &&
2975 	    (orig_off >= gotp->br_startoff) &&
2976 	    (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
2977 		return 0;
2978 	}
2979 
2980 	/*
2981 	 * If the file offset is unaligned vs. the extent size
2982 	 * we need to align it.  This will be possible unless
2983 	 * the file was previously written with a kernel that didn't
2984 	 * perform this alignment, or if a truncate shot us in the
2985 	 * foot.
2986 	 */
2987 	div_u64_rem(orig_off, extsz, &temp);
2988 	if (temp) {
2989 		align_alen += temp;
2990 		align_off -= temp;
2991 	}
2992 
2993 	/* Same adjustment for the end of the requested area. */
2994 	temp = (align_alen % extsz);
2995 	if (temp)
2996 		align_alen += extsz - temp;
2997 
2998 	/*
2999 	 * For large extent hint sizes, the aligned extent might be larger than
3000 	 * XFS_BMBT_MAX_EXTLEN. In that case, reduce the size by an extsz so
3001 	 * that it pulls the length back under XFS_BMBT_MAX_EXTLEN. The outer
3002 	 * allocation loops handle short allocation just fine, so it is safe to
3003 	 * do this. We only want to do it when we are forced to, though, because
3004 	 * it means more allocation operations are required.
3005 	 */
3006 	while (align_alen > XFS_MAX_BMBT_EXTLEN)
3007 		align_alen -= extsz;
3008 	ASSERT(align_alen <= XFS_MAX_BMBT_EXTLEN);
3009 
3010 	/*
3011 	 * If the previous block overlaps with this proposed allocation
3012 	 * then move the start forward without adjusting the length.
3013 	 */
3014 	if (prevp->br_startoff != NULLFILEOFF) {
3015 		if (prevp->br_startblock == HOLESTARTBLOCK)
3016 			prevo = prevp->br_startoff;
3017 		else
3018 			prevo = prevp->br_startoff + prevp->br_blockcount;
3019 	} else
3020 		prevo = 0;
3021 	if (align_off != orig_off && align_off < prevo)
3022 		align_off = prevo;
3023 	/*
3024 	 * If the next block overlaps with this proposed allocation
3025 	 * then move the start back without adjusting the length,
3026 	 * but not before offset 0.
3027 	 * This may of course make the start overlap previous block,
3028 	 * and if we hit the offset 0 limit then the next block
3029 	 * can still overlap too.
3030 	 */
3031 	if (!eof && gotp->br_startoff != NULLFILEOFF) {
3032 		if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
3033 		    (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
3034 			nexto = gotp->br_startoff + gotp->br_blockcount;
3035 		else
3036 			nexto = gotp->br_startoff;
3037 	} else
3038 		nexto = NULLFILEOFF;
3039 	if (!eof &&
3040 	    align_off + align_alen != orig_end &&
3041 	    align_off + align_alen > nexto)
3042 		align_off = nexto > align_alen ? nexto - align_alen : 0;
3043 	/*
3044 	 * If we're now overlapping the next or previous extent that
3045 	 * means we can't fit an extsz piece in this hole.  Just move
3046 	 * the start forward to the first valid spot and set
3047 	 * the length so we hit the end.
3048 	 */
3049 	if (align_off != orig_off && align_off < prevo)
3050 		align_off = prevo;
3051 	if (align_off + align_alen != orig_end &&
3052 	    align_off + align_alen > nexto &&
3053 	    nexto != NULLFILEOFF) {
3054 		ASSERT(nexto > prevo);
3055 		align_alen = nexto - align_off;
3056 	}
3057 
3058 	/*
3059 	 * If realtime, and the result isn't a multiple of the realtime
3060 	 * extent size we need to remove blocks until it is.
3061 	 */
3062 	if (rt && (temp = xfs_extlen_to_rtxmod(mp, align_alen))) {
3063 		/*
3064 		 * We're not covering the original request, or
3065 		 * we won't be able to once we fix the length.
3066 		 */
3067 		if (orig_off < align_off ||
3068 		    orig_end > align_off + align_alen ||
3069 		    align_alen - temp < orig_alen)
3070 			return -EINVAL;
3071 		/*
3072 		 * Try to fix it by moving the start up.
3073 		 */
3074 		if (align_off + temp <= orig_off) {
3075 			align_alen -= temp;
3076 			align_off += temp;
3077 		}
3078 		/*
3079 		 * Try to fix it by moving the end in.
3080 		 */
3081 		else if (align_off + align_alen - temp >= orig_end)
3082 			align_alen -= temp;
3083 		/*
3084 		 * Set the start to the minimum then trim the length.
3085 		 */
3086 		else {
3087 			align_alen -= orig_off - align_off;
3088 			align_off = orig_off;
3089 			align_alen -= xfs_extlen_to_rtxmod(mp, align_alen);
3090 		}
3091 		/*
3092 		 * Result doesn't cover the request, fail it.
3093 		 */
3094 		if (orig_off < align_off || orig_end > align_off + align_alen)
3095 			return -EINVAL;
3096 	} else {
3097 		ASSERT(orig_off >= align_off);
3098 		/* see XFS_BMBT_MAX_EXTLEN handling above */
3099 		ASSERT(orig_end <= align_off + align_alen ||
3100 		       align_alen + extsz > XFS_MAX_BMBT_EXTLEN);
3101 	}
3102 
3103 #ifdef DEBUG
3104 	if (!eof && gotp->br_startoff != NULLFILEOFF)
3105 		ASSERT(align_off + align_alen <= gotp->br_startoff);
3106 	if (prevp->br_startoff != NULLFILEOFF)
3107 		ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
3108 #endif
3109 
3110 	*lenp = align_alen;
3111 	*offp = align_off;
3112 	return 0;
3113 }
3114 
3115 static inline bool
3116 xfs_bmap_adjacent_valid(
3117 	struct xfs_bmalloca	*ap,
3118 	xfs_fsblock_t		x,
3119 	xfs_fsblock_t		y)
3120 {
3121 	struct xfs_mount	*mp = ap->ip->i_mount;
3122 
3123 	if (XFS_IS_REALTIME_INODE(ap->ip) &&
3124 	    (ap->datatype & XFS_ALLOC_USERDATA))
3125 		return x < mp->m_sb.sb_rblocks;
3126 
3127 	return XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) &&
3128 		XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount &&
3129 		XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks;
3130 }
3131 
3132 #define XFS_ALLOC_GAP_UNITS	4
3133 
3134 /* returns true if ap->blkno was modified */
3135 bool
3136 xfs_bmap_adjacent(
3137 	struct xfs_bmalloca	*ap)	/* bmap alloc argument struct */
3138 {
3139 	xfs_fsblock_t		adjust;		/* adjustment to block numbers */
3140 
3141 	/*
3142 	 * If allocating at eof, and there's a previous real block,
3143 	 * try to use its last block as our starting point.
3144 	 */
3145 	if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
3146 	    !isnullstartblock(ap->prev.br_startblock) &&
3147 	    xfs_bmap_adjacent_valid(ap,
3148 			ap->prev.br_startblock + ap->prev.br_blockcount,
3149 			ap->prev.br_startblock)) {
3150 		ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
3151 		/*
3152 		 * Adjust for the gap between prevp and us.
3153 		 */
3154 		adjust = ap->offset -
3155 			(ap->prev.br_startoff + ap->prev.br_blockcount);
3156 		if (adjust && xfs_bmap_adjacent_valid(ap, ap->blkno + adjust,
3157 				ap->prev.br_startblock))
3158 			ap->blkno += adjust;
3159 		return true;
3160 	}
3161 	/*
3162 	 * If not at eof, then compare the two neighbor blocks.
3163 	 * Figure out whether either one gives us a good starting point,
3164 	 * and pick the better one.
3165 	 */
3166 	if (!ap->eof) {
3167 		xfs_fsblock_t	gotbno;		/* right side block number */
3168 		xfs_fsblock_t	gotdiff=0;	/* right side difference */
3169 		xfs_fsblock_t	prevbno;	/* left side block number */
3170 		xfs_fsblock_t	prevdiff=0;	/* left side difference */
3171 
3172 		/*
3173 		 * If there's a previous (left) block, select a requested
3174 		 * start block based on it.
3175 		 */
3176 		if (ap->prev.br_startoff != NULLFILEOFF &&
3177 		    !isnullstartblock(ap->prev.br_startblock) &&
3178 		    (prevbno = ap->prev.br_startblock +
3179 			       ap->prev.br_blockcount) &&
3180 		    xfs_bmap_adjacent_valid(ap, prevbno,
3181 				ap->prev.br_startblock)) {
3182 			/*
3183 			 * Calculate gap to end of previous block.
3184 			 */
3185 			adjust = prevdiff = ap->offset -
3186 				(ap->prev.br_startoff +
3187 				 ap->prev.br_blockcount);
3188 			/*
3189 			 * Figure the startblock based on the previous block's
3190 			 * end and the gap size.
3191 			 * Heuristic!
3192 			 * If the gap is large relative to the piece we're
3193 			 * allocating, or using it gives us an invalid block
3194 			 * number, then just use the end of the previous block.
3195 			 */
3196 			if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
3197 			    xfs_bmap_adjacent_valid(ap, prevbno + prevdiff,
3198 					ap->prev.br_startblock))
3199 				prevbno += adjust;
3200 			else
3201 				prevdiff += adjust;
3202 		}
3203 		/*
3204 		 * No previous block or can't follow it, just default.
3205 		 */
3206 		else
3207 			prevbno = NULLFSBLOCK;
3208 		/*
3209 		 * If there's a following (right) block, select a requested
3210 		 * start block based on it.
3211 		 */
3212 		if (!isnullstartblock(ap->got.br_startblock)) {
3213 			/*
3214 			 * Calculate gap to start of next block.
3215 			 */
3216 			adjust = gotdiff = ap->got.br_startoff - ap->offset;
3217 			/*
3218 			 * Figure the startblock based on the next block's
3219 			 * start and the gap size.
3220 			 */
3221 			gotbno = ap->got.br_startblock;
3222 			/*
3223 			 * Heuristic!
3224 			 * If the gap is large relative to the piece we're
3225 			 * allocating, or using it gives us an invalid block
3226 			 * number, then just use the start of the next block
3227 			 * offset by our length.
3228 			 */
3229 			if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
3230 			    xfs_bmap_adjacent_valid(ap, gotbno - gotdiff,
3231 					gotbno))
3232 				gotbno -= adjust;
3233 			else if (xfs_bmap_adjacent_valid(ap, gotbno - ap->length,
3234 					gotbno)) {
3235 				gotbno -= ap->length;
3236 				gotdiff += adjust - ap->length;
3237 			} else
3238 				gotdiff += adjust;
3239 		}
3240 		/*
3241 		 * No next block, just default.
3242 		 */
3243 		else
3244 			gotbno = NULLFSBLOCK;
3245 		/*
3246 		 * If both valid, pick the better one, else the only good
3247 		 * one, else ap->blkno is already set (to 0 or the inode block).
3248 		 */
3249 		if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK) {
3250 			ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
3251 			return true;
3252 		}
3253 		if (prevbno != NULLFSBLOCK) {
3254 			ap->blkno = prevbno;
3255 			return true;
3256 		}
3257 		if (gotbno != NULLFSBLOCK) {
3258 			ap->blkno = gotbno;
3259 			return true;
3260 		}
3261 	}
3262 
3263 	return false;
3264 }
3265 
3266 int
3267 xfs_bmap_longest_free_extent(
3268 	struct xfs_perag	*pag,
3269 	struct xfs_trans	*tp,
3270 	xfs_extlen_t		*blen)
3271 {
3272 	xfs_extlen_t		longest;
3273 	int			error = 0;
3274 
3275 	if (!xfs_perag_initialised_agf(pag)) {
3276 		error = xfs_alloc_read_agf(pag, tp, XFS_ALLOC_FLAG_TRYLOCK,
3277 				NULL);
3278 		if (error)
3279 			return error;
3280 	}
3281 
3282 	longest = xfs_alloc_longest_free_extent(pag,
3283 				xfs_alloc_min_freelist(pag->pag_mount, pag),
3284 				xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE));
3285 	if (*blen < longest)
3286 		*blen = longest;
3287 
3288 	return 0;
3289 }
3290 
3291 static xfs_extlen_t
3292 xfs_bmap_select_minlen(
3293 	struct xfs_bmalloca	*ap,
3294 	struct xfs_alloc_arg	*args,
3295 	xfs_extlen_t		blen)
3296 {
3297 
3298 	/*
3299 	 * Since we used XFS_ALLOC_FLAG_TRYLOCK in _longest_free_extent(), it is
3300 	 * possible that there is enough contiguous free space for this request.
3301 	 */
3302 	if (blen < ap->minlen)
3303 		return ap->minlen;
3304 
3305 	/*
3306 	 * If the best seen length is less than the request length,
3307 	 * use the best as the minimum, otherwise we've got the maxlen we
3308 	 * were asked for.
3309 	 */
3310 	if (blen < args->maxlen)
3311 		return blen;
3312 	return args->maxlen;
3313 }
3314 
3315 static int
3316 xfs_bmap_btalloc_select_lengths(
3317 	struct xfs_bmalloca	*ap,
3318 	struct xfs_alloc_arg	*args,
3319 	xfs_extlen_t		*blen)
3320 {
3321 	struct xfs_mount	*mp = args->mp;
3322 	struct xfs_perag	*pag;
3323 	xfs_agnumber_t		agno, startag;
3324 	int			error = 0;
3325 
3326 	if (ap->tp->t_flags & XFS_TRANS_LOWMODE) {
3327 		args->total = ap->minlen;
3328 		args->minlen = ap->minlen;
3329 		return 0;
3330 	}
3331 
3332 	args->total = ap->total;
3333 	startag = XFS_FSB_TO_AGNO(mp, ap->blkno);
3334 	if (startag == NULLAGNUMBER)
3335 		startag = 0;
3336 
3337 	*blen = 0;
3338 	for_each_perag_wrap(mp, startag, agno, pag) {
3339 		error = xfs_bmap_longest_free_extent(pag, args->tp, blen);
3340 		if (error && error != -EAGAIN)
3341 			break;
3342 		error = 0;
3343 		if (*blen >= args->maxlen)
3344 			break;
3345 	}
3346 	if (pag)
3347 		xfs_perag_rele(pag);
3348 
3349 	args->minlen = xfs_bmap_select_minlen(ap, args, *blen);
3350 	return error;
3351 }
3352 
3353 /* Update all inode and quota accounting for the allocation we just did. */
3354 void
3355 xfs_bmap_alloc_account(
3356 	struct xfs_bmalloca	*ap)
3357 {
3358 	bool			isrt = XFS_IS_REALTIME_INODE(ap->ip) &&
3359 					!(ap->flags & XFS_BMAPI_ATTRFORK);
3360 	uint			fld;
3361 
3362 	if (ap->flags & XFS_BMAPI_COWFORK) {
3363 		/*
3364 		 * COW fork blocks are in-core only and thus are treated as
3365 		 * in-core quota reservation (like delalloc blocks) even when
3366 		 * converted to real blocks. The quota reservation is not
3367 		 * accounted to disk until blocks are remapped to the data
3368 		 * fork. So if these blocks were previously delalloc, we
3369 		 * already have quota reservation and there's nothing to do
3370 		 * yet.
3371 		 */
3372 		if (ap->wasdel) {
3373 			xfs_mod_delalloc(ap->ip, -(int64_t)ap->length, 0);
3374 			return;
3375 		}
3376 
3377 		/*
3378 		 * Otherwise, we've allocated blocks in a hole. The transaction
3379 		 * has acquired in-core quota reservation for this extent.
3380 		 * Rather than account these as real blocks, however, we reduce
3381 		 * the transaction quota reservation based on the allocation.
3382 		 * This essentially transfers the transaction quota reservation
3383 		 * to that of a delalloc extent.
3384 		 */
3385 		ap->ip->i_delayed_blks += ap->length;
3386 		xfs_trans_mod_dquot_byino(ap->tp, ap->ip, isrt ?
3387 				XFS_TRANS_DQ_RES_RTBLKS : XFS_TRANS_DQ_RES_BLKS,
3388 				-(long)ap->length);
3389 		return;
3390 	}
3391 
3392 	/* data/attr fork only */
3393 	ap->ip->i_nblocks += ap->length;
3394 	xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
3395 	if (ap->wasdel) {
3396 		ap->ip->i_delayed_blks -= ap->length;
3397 		xfs_mod_delalloc(ap->ip, -(int64_t)ap->length, 0);
3398 		fld = isrt ? XFS_TRANS_DQ_DELRTBCOUNT : XFS_TRANS_DQ_DELBCOUNT;
3399 	} else {
3400 		fld = isrt ? XFS_TRANS_DQ_RTBCOUNT : XFS_TRANS_DQ_BCOUNT;
3401 	}
3402 
3403 	xfs_trans_mod_dquot_byino(ap->tp, ap->ip, fld, ap->length);
3404 }
3405 
3406 static int
3407 xfs_bmap_compute_alignments(
3408 	struct xfs_bmalloca	*ap,
3409 	struct xfs_alloc_arg	*args)
3410 {
3411 	struct xfs_mount	*mp = args->mp;
3412 	xfs_extlen_t		align = 0; /* minimum allocation alignment */
3413 	int			stripe_align = 0;
3414 
3415 	/* stripe alignment for allocation is determined by mount parameters */
3416 	if (mp->m_swidth && xfs_has_swalloc(mp))
3417 		stripe_align = mp->m_swidth;
3418 	else if (mp->m_dalign)
3419 		stripe_align = mp->m_dalign;
3420 
3421 	if (ap->flags & XFS_BMAPI_COWFORK)
3422 		align = xfs_get_cowextsz_hint(ap->ip);
3423 	else if (ap->datatype & XFS_ALLOC_USERDATA)
3424 		align = xfs_get_extsz_hint(ap->ip);
3425 	if (align) {
3426 		if (xfs_bmap_extsize_align(mp, &ap->got, &ap->prev, align, 0,
3427 					ap->eof, 0, ap->conv, &ap->offset,
3428 					&ap->length))
3429 			ASSERT(0);
3430 		ASSERT(ap->length);
3431 	}
3432 
3433 	/* apply extent size hints if obtained earlier */
3434 	if (align) {
3435 		args->prod = align;
3436 		div_u64_rem(ap->offset, args->prod, &args->mod);
3437 		if (args->mod)
3438 			args->mod = args->prod - args->mod;
3439 	} else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) {
3440 		args->prod = 1;
3441 		args->mod = 0;
3442 	} else {
3443 		args->prod = PAGE_SIZE >> mp->m_sb.sb_blocklog;
3444 		div_u64_rem(ap->offset, args->prod, &args->mod);
3445 		if (args->mod)
3446 			args->mod = args->prod - args->mod;
3447 	}
3448 
3449 	return stripe_align;
3450 }
3451 
3452 static void
3453 xfs_bmap_process_allocated_extent(
3454 	struct xfs_bmalloca	*ap,
3455 	struct xfs_alloc_arg	*args,
3456 	xfs_fileoff_t		orig_offset,
3457 	xfs_extlen_t		orig_length)
3458 {
3459 	ap->blkno = args->fsbno;
3460 	ap->length = args->len;
3461 	/*
3462 	 * If the extent size hint is active, we tried to round the
3463 	 * caller's allocation request offset down to extsz and the
3464 	 * length up to another extsz boundary.  If we found a free
3465 	 * extent we mapped it in starting at this new offset.  If the
3466 	 * newly mapped space isn't long enough to cover any of the
3467 	 * range of offsets that was originally requested, move the
3468 	 * mapping up so that we can fill as much of the caller's
3469 	 * original request as possible.  Free space is apparently
3470 	 * very fragmented so we're unlikely to be able to satisfy the
3471 	 * hints anyway.
3472 	 */
3473 	if (ap->length <= orig_length)
3474 		ap->offset = orig_offset;
3475 	else if (ap->offset + ap->length < orig_offset + orig_length)
3476 		ap->offset = orig_offset + orig_length - ap->length;
3477 	xfs_bmap_alloc_account(ap);
3478 }
3479 
3480 #ifdef DEBUG
3481 static int
3482 xfs_bmap_exact_minlen_extent_alloc(
3483 	struct xfs_bmalloca	*ap)
3484 {
3485 	struct xfs_mount	*mp = ap->ip->i_mount;
3486 	struct xfs_alloc_arg	args = { .tp = ap->tp, .mp = mp };
3487 	xfs_fileoff_t		orig_offset;
3488 	xfs_extlen_t		orig_length;
3489 	int			error;
3490 
3491 	ASSERT(ap->length);
3492 
3493 	if (ap->minlen != 1) {
3494 		ap->blkno = NULLFSBLOCK;
3495 		ap->length = 0;
3496 		return 0;
3497 	}
3498 
3499 	orig_offset = ap->offset;
3500 	orig_length = ap->length;
3501 
3502 	args.alloc_minlen_only = 1;
3503 
3504 	xfs_bmap_compute_alignments(ap, &args);
3505 
3506 	/*
3507 	 * Unlike the longest extent available in an AG, we don't track
3508 	 * the length of an AG's shortest extent.
3509 	 * XFS_ERRTAG_BMAP_ALLOC_MINLEN_EXTENT is a debug only knob and
3510 	 * hence we can afford to start traversing from the 0th AG since
3511 	 * we need not be concerned about a drop in performance in
3512 	 * "debug only" code paths.
3513 	 */
3514 	ap->blkno = XFS_AGB_TO_FSB(mp, 0, 0);
3515 
3516 	args.oinfo = XFS_RMAP_OINFO_SKIP_UPDATE;
3517 	args.minlen = args.maxlen = ap->minlen;
3518 	args.total = ap->total;
3519 
3520 	args.alignment = 1;
3521 	args.minalignslop = 0;
3522 
3523 	args.minleft = ap->minleft;
3524 	args.wasdel = ap->wasdel;
3525 	args.resv = XFS_AG_RESV_NONE;
3526 	args.datatype = ap->datatype;
3527 
3528 	error = xfs_alloc_vextent_first_ag(&args, ap->blkno);
3529 	if (error)
3530 		return error;
3531 
3532 	if (args.fsbno != NULLFSBLOCK) {
3533 		xfs_bmap_process_allocated_extent(ap, &args, orig_offset,
3534 			orig_length);
3535 	} else {
3536 		ap->blkno = NULLFSBLOCK;
3537 		ap->length = 0;
3538 	}
3539 
3540 	return 0;
3541 }
3542 #else
3543 
3544 #define xfs_bmap_exact_minlen_extent_alloc(bma) (-EFSCORRUPTED)
3545 
3546 #endif
3547 
3548 /*
3549  * If we are not low on available data blocks and we are allocating at
3550  * EOF, optimise allocation for contiguous file extension and/or stripe
3551  * alignment of the new extent.
3552  *
3553  * NOTE: ap->aeof is only set if the allocation length is >= the
3554  * stripe unit and the allocation offset is at the end of file.
3555  */
3556 static int
3557 xfs_bmap_btalloc_at_eof(
3558 	struct xfs_bmalloca	*ap,
3559 	struct xfs_alloc_arg	*args,
3560 	xfs_extlen_t		blen,
3561 	int			stripe_align,
3562 	bool			ag_only)
3563 {
3564 	struct xfs_mount	*mp = args->mp;
3565 	struct xfs_perag	*caller_pag = args->pag;
3566 	int			error;
3567 
3568 	/*
3569 	 * If there are already extents in the file, try an exact EOF block
3570 	 * allocation to extend the file as a contiguous extent. If that fails,
3571 	 * or it's the first allocation in a file, just try for a stripe aligned
3572 	 * allocation.
3573 	 */
3574 	if (ap->offset) {
3575 		xfs_extlen_t	nextminlen = 0;
3576 
3577 		/*
3578 		 * Compute the minlen+alignment for the next case.  Set slop so
3579 		 * that the value of minlen+alignment+slop doesn't go up between
3580 		 * the calls.
3581 		 */
3582 		args->alignment = 1;
3583 		if (blen > stripe_align && blen <= args->maxlen)
3584 			nextminlen = blen - stripe_align;
3585 		else
3586 			nextminlen = args->minlen;
3587 		if (nextminlen + stripe_align > args->minlen + 1)
3588 			args->minalignslop = nextminlen + stripe_align -
3589 					args->minlen - 1;
3590 		else
3591 			args->minalignslop = 0;
3592 
3593 		if (!caller_pag)
3594 			args->pag = xfs_perag_get(mp, XFS_FSB_TO_AGNO(mp, ap->blkno));
3595 		error = xfs_alloc_vextent_exact_bno(args, ap->blkno);
3596 		if (!caller_pag) {
3597 			xfs_perag_put(args->pag);
3598 			args->pag = NULL;
3599 		}
3600 		if (error)
3601 			return error;
3602 
3603 		if (args->fsbno != NULLFSBLOCK)
3604 			return 0;
3605 		/*
3606 		 * Exact allocation failed. Reset to try an aligned allocation
3607 		 * according to the original allocation specification.
3608 		 */
3609 		args->alignment = stripe_align;
3610 		args->minlen = nextminlen;
3611 		args->minalignslop = 0;
3612 	} else {
3613 		/*
3614 		 * Adjust minlen to try and preserve alignment if we
3615 		 * can't guarantee an aligned maxlen extent.
3616 		 */
3617 		args->alignment = stripe_align;
3618 		if (blen > args->alignment &&
3619 		    blen <= args->maxlen + args->alignment)
3620 			args->minlen = blen - args->alignment;
3621 		args->minalignslop = 0;
3622 	}
3623 
3624 	if (ag_only) {
3625 		error = xfs_alloc_vextent_near_bno(args, ap->blkno);
3626 	} else {
3627 		args->pag = NULL;
3628 		error = xfs_alloc_vextent_start_ag(args, ap->blkno);
3629 		ASSERT(args->pag == NULL);
3630 		args->pag = caller_pag;
3631 	}
3632 	if (error)
3633 		return error;
3634 
3635 	if (args->fsbno != NULLFSBLOCK)
3636 		return 0;
3637 
3638 	/*
3639 	 * Allocation failed, so turn return the allocation args to their
3640 	 * original non-aligned state so the caller can proceed on allocation
3641 	 * failure as if this function was never called.
3642 	 */
3643 	args->alignment = 1;
3644 	return 0;
3645 }
3646 
3647 /*
3648  * We have failed multiple allocation attempts so now are in a low space
3649  * allocation situation. Try a locality first full filesystem minimum length
3650  * allocation whilst still maintaining necessary total block reservation
3651  * requirements.
3652  *
3653  * If that fails, we are now critically low on space, so perform a last resort
3654  * allocation attempt: no reserve, no locality, blocking, minimum length, full
3655  * filesystem free space scan. We also indicate to future allocations in this
3656  * transaction that we are critically low on space so they don't waste time on
3657  * allocation modes that are unlikely to succeed.
3658  */
3659 int
3660 xfs_bmap_btalloc_low_space(
3661 	struct xfs_bmalloca	*ap,
3662 	struct xfs_alloc_arg	*args)
3663 {
3664 	int			error;
3665 
3666 	if (args->minlen > ap->minlen) {
3667 		args->minlen = ap->minlen;
3668 		error = xfs_alloc_vextent_start_ag(args, ap->blkno);
3669 		if (error || args->fsbno != NULLFSBLOCK)
3670 			return error;
3671 	}
3672 
3673 	/* Last ditch attempt before failure is declared. */
3674 	args->total = ap->minlen;
3675 	error = xfs_alloc_vextent_first_ag(args, 0);
3676 	if (error)
3677 		return error;
3678 	ap->tp->t_flags |= XFS_TRANS_LOWMODE;
3679 	return 0;
3680 }
3681 
3682 static int
3683 xfs_bmap_btalloc_filestreams(
3684 	struct xfs_bmalloca	*ap,
3685 	struct xfs_alloc_arg	*args,
3686 	int			stripe_align)
3687 {
3688 	xfs_extlen_t		blen = 0;
3689 	int			error = 0;
3690 
3691 
3692 	error = xfs_filestream_select_ag(ap, args, &blen);
3693 	if (error)
3694 		return error;
3695 	ASSERT(args->pag);
3696 
3697 	/*
3698 	 * If we are in low space mode, then optimal allocation will fail so
3699 	 * prepare for minimal allocation and jump to the low space algorithm
3700 	 * immediately.
3701 	 */
3702 	if (ap->tp->t_flags & XFS_TRANS_LOWMODE) {
3703 		args->minlen = ap->minlen;
3704 		ASSERT(args->fsbno == NULLFSBLOCK);
3705 		goto out_low_space;
3706 	}
3707 
3708 	args->minlen = xfs_bmap_select_minlen(ap, args, blen);
3709 	if (ap->aeof)
3710 		error = xfs_bmap_btalloc_at_eof(ap, args, blen, stripe_align,
3711 				true);
3712 
3713 	if (!error && args->fsbno == NULLFSBLOCK)
3714 		error = xfs_alloc_vextent_near_bno(args, ap->blkno);
3715 
3716 out_low_space:
3717 	/*
3718 	 * We are now done with the perag reference for the filestreams
3719 	 * association provided by xfs_filestream_select_ag(). Release it now as
3720 	 * we've either succeeded, had a fatal error or we are out of space and
3721 	 * need to do a full filesystem scan for free space which will take it's
3722 	 * own references.
3723 	 */
3724 	xfs_perag_rele(args->pag);
3725 	args->pag = NULL;
3726 	if (error || args->fsbno != NULLFSBLOCK)
3727 		return error;
3728 
3729 	return xfs_bmap_btalloc_low_space(ap, args);
3730 }
3731 
3732 static int
3733 xfs_bmap_btalloc_best_length(
3734 	struct xfs_bmalloca	*ap,
3735 	struct xfs_alloc_arg	*args,
3736 	int			stripe_align)
3737 {
3738 	xfs_extlen_t		blen = 0;
3739 	int			error;
3740 
3741 	ap->blkno = XFS_INO_TO_FSB(args->mp, ap->ip->i_ino);
3742 	xfs_bmap_adjacent(ap);
3743 
3744 	/*
3745 	 * Search for an allocation group with a single extent large enough for
3746 	 * the request.  If one isn't found, then adjust the minimum allocation
3747 	 * size to the largest space found.
3748 	 */
3749 	error = xfs_bmap_btalloc_select_lengths(ap, args, &blen);
3750 	if (error)
3751 		return error;
3752 
3753 	/*
3754 	 * Don't attempt optimal EOF allocation if previous allocations barely
3755 	 * succeeded due to being near ENOSPC. It is highly unlikely we'll get
3756 	 * optimal or even aligned allocations in this case, so don't waste time
3757 	 * trying.
3758 	 */
3759 	if (ap->aeof && !(ap->tp->t_flags & XFS_TRANS_LOWMODE)) {
3760 		error = xfs_bmap_btalloc_at_eof(ap, args, blen, stripe_align,
3761 				false);
3762 		if (error || args->fsbno != NULLFSBLOCK)
3763 			return error;
3764 	}
3765 
3766 	error = xfs_alloc_vextent_start_ag(args, ap->blkno);
3767 	if (error || args->fsbno != NULLFSBLOCK)
3768 		return error;
3769 
3770 	return xfs_bmap_btalloc_low_space(ap, args);
3771 }
3772 
3773 static int
3774 xfs_bmap_btalloc(
3775 	struct xfs_bmalloca	*ap)
3776 {
3777 	struct xfs_mount	*mp = ap->ip->i_mount;
3778 	struct xfs_alloc_arg	args = {
3779 		.tp		= ap->tp,
3780 		.mp		= mp,
3781 		.fsbno		= NULLFSBLOCK,
3782 		.oinfo		= XFS_RMAP_OINFO_SKIP_UPDATE,
3783 		.minleft	= ap->minleft,
3784 		.wasdel		= ap->wasdel,
3785 		.resv		= XFS_AG_RESV_NONE,
3786 		.datatype	= ap->datatype,
3787 		.alignment	= 1,
3788 		.minalignslop	= 0,
3789 	};
3790 	xfs_fileoff_t		orig_offset;
3791 	xfs_extlen_t		orig_length;
3792 	int			error;
3793 	int			stripe_align;
3794 
3795 	ASSERT(ap->length);
3796 	orig_offset = ap->offset;
3797 	orig_length = ap->length;
3798 
3799 	stripe_align = xfs_bmap_compute_alignments(ap, &args);
3800 
3801 	/* Trim the allocation back to the maximum an AG can fit. */
3802 	args.maxlen = min(ap->length, mp->m_ag_max_usable);
3803 
3804 	if ((ap->datatype & XFS_ALLOC_USERDATA) &&
3805 	    xfs_inode_is_filestream(ap->ip))
3806 		error = xfs_bmap_btalloc_filestreams(ap, &args, stripe_align);
3807 	else
3808 		error = xfs_bmap_btalloc_best_length(ap, &args, stripe_align);
3809 	if (error)
3810 		return error;
3811 
3812 	if (args.fsbno != NULLFSBLOCK) {
3813 		xfs_bmap_process_allocated_extent(ap, &args, orig_offset,
3814 			orig_length);
3815 	} else {
3816 		ap->blkno = NULLFSBLOCK;
3817 		ap->length = 0;
3818 	}
3819 	return 0;
3820 }
3821 
3822 /* Trim extent to fit a logical block range. */
3823 void
3824 xfs_trim_extent(
3825 	struct xfs_bmbt_irec	*irec,
3826 	xfs_fileoff_t		bno,
3827 	xfs_filblks_t		len)
3828 {
3829 	xfs_fileoff_t		distance;
3830 	xfs_fileoff_t		end = bno + len;
3831 
3832 	if (irec->br_startoff + irec->br_blockcount <= bno ||
3833 	    irec->br_startoff >= end) {
3834 		irec->br_blockcount = 0;
3835 		return;
3836 	}
3837 
3838 	if (irec->br_startoff < bno) {
3839 		distance = bno - irec->br_startoff;
3840 		if (isnullstartblock(irec->br_startblock))
3841 			irec->br_startblock = DELAYSTARTBLOCK;
3842 		if (irec->br_startblock != DELAYSTARTBLOCK &&
3843 		    irec->br_startblock != HOLESTARTBLOCK)
3844 			irec->br_startblock += distance;
3845 		irec->br_startoff += distance;
3846 		irec->br_blockcount -= distance;
3847 	}
3848 
3849 	if (end < irec->br_startoff + irec->br_blockcount) {
3850 		distance = irec->br_startoff + irec->br_blockcount - end;
3851 		irec->br_blockcount -= distance;
3852 	}
3853 }
3854 
3855 /*
3856  * Trim the returned map to the required bounds
3857  */
3858 STATIC void
3859 xfs_bmapi_trim_map(
3860 	struct xfs_bmbt_irec	*mval,
3861 	struct xfs_bmbt_irec	*got,
3862 	xfs_fileoff_t		*bno,
3863 	xfs_filblks_t		len,
3864 	xfs_fileoff_t		obno,
3865 	xfs_fileoff_t		end,
3866 	int			n,
3867 	uint32_t		flags)
3868 {
3869 	if ((flags & XFS_BMAPI_ENTIRE) ||
3870 	    got->br_startoff + got->br_blockcount <= obno) {
3871 		*mval = *got;
3872 		if (isnullstartblock(got->br_startblock))
3873 			mval->br_startblock = DELAYSTARTBLOCK;
3874 		return;
3875 	}
3876 
3877 	if (obno > *bno)
3878 		*bno = obno;
3879 	ASSERT((*bno >= obno) || (n == 0));
3880 	ASSERT(*bno < end);
3881 	mval->br_startoff = *bno;
3882 	if (isnullstartblock(got->br_startblock))
3883 		mval->br_startblock = DELAYSTARTBLOCK;
3884 	else
3885 		mval->br_startblock = got->br_startblock +
3886 					(*bno - got->br_startoff);
3887 	/*
3888 	 * Return the minimum of what we got and what we asked for for
3889 	 * the length.  We can use the len variable here because it is
3890 	 * modified below and we could have been there before coming
3891 	 * here if the first part of the allocation didn't overlap what
3892 	 * was asked for.
3893 	 */
3894 	mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
3895 			got->br_blockcount - (*bno - got->br_startoff));
3896 	mval->br_state = got->br_state;
3897 	ASSERT(mval->br_blockcount <= len);
3898 	return;
3899 }
3900 
3901 /*
3902  * Update and validate the extent map to return
3903  */
3904 STATIC void
3905 xfs_bmapi_update_map(
3906 	struct xfs_bmbt_irec	**map,
3907 	xfs_fileoff_t		*bno,
3908 	xfs_filblks_t		*len,
3909 	xfs_fileoff_t		obno,
3910 	xfs_fileoff_t		end,
3911 	int			*n,
3912 	uint32_t		flags)
3913 {
3914 	xfs_bmbt_irec_t	*mval = *map;
3915 
3916 	ASSERT((flags & XFS_BMAPI_ENTIRE) ||
3917 	       ((mval->br_startoff + mval->br_blockcount) <= end));
3918 	ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
3919 	       (mval->br_startoff < obno));
3920 
3921 	*bno = mval->br_startoff + mval->br_blockcount;
3922 	*len = end - *bno;
3923 	if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
3924 		/* update previous map with new information */
3925 		ASSERT(mval->br_startblock == mval[-1].br_startblock);
3926 		ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
3927 		ASSERT(mval->br_state == mval[-1].br_state);
3928 		mval[-1].br_blockcount = mval->br_blockcount;
3929 		mval[-1].br_state = mval->br_state;
3930 	} else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
3931 		   mval[-1].br_startblock != DELAYSTARTBLOCK &&
3932 		   mval[-1].br_startblock != HOLESTARTBLOCK &&
3933 		   mval->br_startblock == mval[-1].br_startblock +
3934 					  mval[-1].br_blockcount &&
3935 		   mval[-1].br_state == mval->br_state) {
3936 		ASSERT(mval->br_startoff ==
3937 		       mval[-1].br_startoff + mval[-1].br_blockcount);
3938 		mval[-1].br_blockcount += mval->br_blockcount;
3939 	} else if (*n > 0 &&
3940 		   mval->br_startblock == DELAYSTARTBLOCK &&
3941 		   mval[-1].br_startblock == DELAYSTARTBLOCK &&
3942 		   mval->br_startoff ==
3943 		   mval[-1].br_startoff + mval[-1].br_blockcount) {
3944 		mval[-1].br_blockcount += mval->br_blockcount;
3945 		mval[-1].br_state = mval->br_state;
3946 	} else if (!((*n == 0) &&
3947 		     ((mval->br_startoff + mval->br_blockcount) <=
3948 		      obno))) {
3949 		mval++;
3950 		(*n)++;
3951 	}
3952 	*map = mval;
3953 }
3954 
3955 /*
3956  * Map file blocks to filesystem blocks without allocation.
3957  */
3958 int
3959 xfs_bmapi_read(
3960 	struct xfs_inode	*ip,
3961 	xfs_fileoff_t		bno,
3962 	xfs_filblks_t		len,
3963 	struct xfs_bmbt_irec	*mval,
3964 	int			*nmap,
3965 	uint32_t		flags)
3966 {
3967 	struct xfs_mount	*mp = ip->i_mount;
3968 	int			whichfork = xfs_bmapi_whichfork(flags);
3969 	struct xfs_ifork	*ifp = xfs_ifork_ptr(ip, whichfork);
3970 	struct xfs_bmbt_irec	got;
3971 	xfs_fileoff_t		obno;
3972 	xfs_fileoff_t		end;
3973 	struct xfs_iext_cursor	icur;
3974 	int			error;
3975 	bool			eof = false;
3976 	int			n = 0;
3977 
3978 	ASSERT(*nmap >= 1);
3979 	ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK | XFS_BMAPI_ENTIRE)));
3980 	xfs_assert_ilocked(ip, XFS_ILOCK_SHARED | XFS_ILOCK_EXCL);
3981 
3982 	if (WARN_ON_ONCE(!ifp)) {
3983 		xfs_bmap_mark_sick(ip, whichfork);
3984 		return -EFSCORRUPTED;
3985 	}
3986 
3987 	if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
3988 	    XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
3989 		xfs_bmap_mark_sick(ip, whichfork);
3990 		return -EFSCORRUPTED;
3991 	}
3992 
3993 	if (xfs_is_shutdown(mp))
3994 		return -EIO;
3995 
3996 	XFS_STATS_INC(mp, xs_blk_mapr);
3997 
3998 	error = xfs_iread_extents(NULL, ip, whichfork);
3999 	if (error)
4000 		return error;
4001 
4002 	if (!xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got))
4003 		eof = true;
4004 	end = bno + len;
4005 	obno = bno;
4006 
4007 	while (bno < end && n < *nmap) {
4008 		/* Reading past eof, act as though there's a hole up to end. */
4009 		if (eof)
4010 			got.br_startoff = end;
4011 		if (got.br_startoff > bno) {
4012 			/* Reading in a hole.  */
4013 			mval->br_startoff = bno;
4014 			mval->br_startblock = HOLESTARTBLOCK;
4015 			mval->br_blockcount =
4016 				XFS_FILBLKS_MIN(len, got.br_startoff - bno);
4017 			mval->br_state = XFS_EXT_NORM;
4018 			bno += mval->br_blockcount;
4019 			len -= mval->br_blockcount;
4020 			mval++;
4021 			n++;
4022 			continue;
4023 		}
4024 
4025 		/* set up the extent map to return. */
4026 		xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
4027 		xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4028 
4029 		/* If we're done, stop now. */
4030 		if (bno >= end || n >= *nmap)
4031 			break;
4032 
4033 		/* Else go on to the next record. */
4034 		if (!xfs_iext_next_extent(ifp, &icur, &got))
4035 			eof = true;
4036 	}
4037 	*nmap = n;
4038 	return 0;
4039 }
4040 
4041 /*
4042  * Add a delayed allocation extent to an inode. Blocks are reserved from the
4043  * global pool and the extent inserted into the inode in-core extent tree.
4044  *
4045  * On entry, got refers to the first extent beyond the offset of the extent to
4046  * allocate or eof is specified if no such extent exists. On return, got refers
4047  * to the extent record that was inserted to the inode fork.
4048  *
4049  * Note that the allocated extent may have been merged with contiguous extents
4050  * during insertion into the inode fork. Thus, got does not reflect the current
4051  * state of the inode fork on return. If necessary, the caller can use lastx to
4052  * look up the updated record in the inode fork.
4053  */
4054 int
4055 xfs_bmapi_reserve_delalloc(
4056 	struct xfs_inode	*ip,
4057 	int			whichfork,
4058 	xfs_fileoff_t		off,
4059 	xfs_filblks_t		len,
4060 	xfs_filblks_t		prealloc,
4061 	struct xfs_bmbt_irec	*got,
4062 	struct xfs_iext_cursor	*icur,
4063 	int			eof)
4064 {
4065 	struct xfs_mount	*mp = ip->i_mount;
4066 	struct xfs_ifork	*ifp = xfs_ifork_ptr(ip, whichfork);
4067 	xfs_extlen_t		alen;
4068 	xfs_extlen_t		indlen;
4069 	uint64_t		fdblocks;
4070 	int			error;
4071 	xfs_fileoff_t		aoff;
4072 	bool			use_cowextszhint =
4073 					whichfork == XFS_COW_FORK && !prealloc;
4074 
4075 retry:
4076 	/*
4077 	 * Cap the alloc length. Keep track of prealloc so we know whether to
4078 	 * tag the inode before we return.
4079 	 */
4080 	aoff = off;
4081 	alen = XFS_FILBLKS_MIN(len + prealloc, XFS_MAX_BMBT_EXTLEN);
4082 	if (!eof)
4083 		alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
4084 	if (prealloc && alen >= len)
4085 		prealloc = alen - len;
4086 
4087 	/*
4088 	 * If we're targetting the COW fork but aren't creating a speculative
4089 	 * posteof preallocation, try to expand the reservation to align with
4090 	 * the COW extent size hint if there's sufficient free space.
4091 	 *
4092 	 * Unlike the data fork, the CoW cancellation functions will free all
4093 	 * the reservations at inactivation, so we don't require that every
4094 	 * delalloc reservation have a dirty pagecache.
4095 	 */
4096 	if (use_cowextszhint) {
4097 		struct xfs_bmbt_irec	prev;
4098 		xfs_extlen_t		extsz = xfs_get_cowextsz_hint(ip);
4099 
4100 		if (!xfs_iext_peek_prev_extent(ifp, icur, &prev))
4101 			prev.br_startoff = NULLFILEOFF;
4102 
4103 		error = xfs_bmap_extsize_align(mp, got, &prev, extsz, 0, eof,
4104 					       1, 0, &aoff, &alen);
4105 		ASSERT(!error);
4106 	}
4107 
4108 	/*
4109 	 * Make a transaction-less quota reservation for delayed allocation
4110 	 * blocks.  This number gets adjusted later.  We return if we haven't
4111 	 * allocated blocks already inside this loop.
4112 	 */
4113 	error = xfs_quota_reserve_blkres(ip, alen);
4114 	if (error)
4115 		goto out;
4116 
4117 	/*
4118 	 * Split changing sb for alen and indlen since they could be coming
4119 	 * from different places.
4120 	 */
4121 	indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
4122 	ASSERT(indlen > 0);
4123 
4124 	fdblocks = indlen;
4125 	if (XFS_IS_REALTIME_INODE(ip)) {
4126 		error = xfs_dec_frextents(mp, xfs_rtb_to_rtx(mp, alen));
4127 		if (error)
4128 			goto out_unreserve_quota;
4129 	} else {
4130 		fdblocks += alen;
4131 	}
4132 
4133 	error = xfs_dec_fdblocks(mp, fdblocks, false);
4134 	if (error)
4135 		goto out_unreserve_frextents;
4136 
4137 	ip->i_delayed_blks += alen;
4138 	xfs_mod_delalloc(ip, alen, indlen);
4139 
4140 	got->br_startoff = aoff;
4141 	got->br_startblock = nullstartblock(indlen);
4142 	got->br_blockcount = alen;
4143 	got->br_state = XFS_EXT_NORM;
4144 
4145 	xfs_bmap_add_extent_hole_delay(ip, whichfork, icur, got);
4146 
4147 	/*
4148 	 * Tag the inode if blocks were preallocated. Note that COW fork
4149 	 * preallocation can occur at the start or end of the extent, even when
4150 	 * prealloc == 0, so we must also check the aligned offset and length.
4151 	 */
4152 	if (whichfork == XFS_DATA_FORK && prealloc)
4153 		xfs_inode_set_eofblocks_tag(ip);
4154 	if (whichfork == XFS_COW_FORK && (prealloc || aoff < off || alen > len))
4155 		xfs_inode_set_cowblocks_tag(ip);
4156 
4157 	return 0;
4158 
4159 out_unreserve_frextents:
4160 	if (XFS_IS_REALTIME_INODE(ip))
4161 		xfs_add_frextents(mp, xfs_rtb_to_rtx(mp, alen));
4162 out_unreserve_quota:
4163 	if (XFS_IS_QUOTA_ON(mp))
4164 		xfs_quota_unreserve_blkres(ip, alen);
4165 out:
4166 	if (error == -ENOSPC || error == -EDQUOT) {
4167 		trace_xfs_delalloc_enospc(ip, off, len);
4168 
4169 		if (prealloc || use_cowextszhint) {
4170 			/* retry without any preallocation */
4171 			use_cowextszhint = false;
4172 			prealloc = 0;
4173 			goto retry;
4174 		}
4175 	}
4176 	return error;
4177 }
4178 
4179 static int
4180 xfs_bmap_alloc_userdata(
4181 	struct xfs_bmalloca	*bma)
4182 {
4183 	struct xfs_mount	*mp = bma->ip->i_mount;
4184 	int			whichfork = xfs_bmapi_whichfork(bma->flags);
4185 	int			error;
4186 
4187 	/*
4188 	 * Set the data type being allocated. For the data fork, the first data
4189 	 * in the file is treated differently to all other allocations. For the
4190 	 * attribute fork, we only need to ensure the allocated range is not on
4191 	 * the busy list.
4192 	 */
4193 	bma->datatype = XFS_ALLOC_NOBUSY;
4194 	if (whichfork == XFS_DATA_FORK || whichfork == XFS_COW_FORK) {
4195 		bma->datatype |= XFS_ALLOC_USERDATA;
4196 		if (bma->offset == 0)
4197 			bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA;
4198 
4199 		if (mp->m_dalign && bma->length >= mp->m_dalign) {
4200 			error = xfs_bmap_isaeof(bma, whichfork);
4201 			if (error)
4202 				return error;
4203 		}
4204 
4205 		if (XFS_IS_REALTIME_INODE(bma->ip))
4206 			return xfs_bmap_rtalloc(bma);
4207 	}
4208 
4209 	if (unlikely(XFS_TEST_ERROR(false, mp,
4210 			XFS_ERRTAG_BMAP_ALLOC_MINLEN_EXTENT)))
4211 		return xfs_bmap_exact_minlen_extent_alloc(bma);
4212 
4213 	return xfs_bmap_btalloc(bma);
4214 }
4215 
4216 static int
4217 xfs_bmapi_allocate(
4218 	struct xfs_bmalloca	*bma)
4219 {
4220 	struct xfs_mount	*mp = bma->ip->i_mount;
4221 	int			whichfork = xfs_bmapi_whichfork(bma->flags);
4222 	struct xfs_ifork	*ifp = xfs_ifork_ptr(bma->ip, whichfork);
4223 	int			error;
4224 
4225 	ASSERT(bma->length > 0);
4226 	ASSERT(bma->length <= XFS_MAX_BMBT_EXTLEN);
4227 
4228 	if (bma->flags & XFS_BMAPI_CONTIG)
4229 		bma->minlen = bma->length;
4230 	else
4231 		bma->minlen = 1;
4232 
4233 	if (bma->flags & XFS_BMAPI_METADATA) {
4234 		if (unlikely(XFS_TEST_ERROR(false, mp,
4235 				XFS_ERRTAG_BMAP_ALLOC_MINLEN_EXTENT)))
4236 			error = xfs_bmap_exact_minlen_extent_alloc(bma);
4237 		else
4238 			error = xfs_bmap_btalloc(bma);
4239 	} else {
4240 		error = xfs_bmap_alloc_userdata(bma);
4241 	}
4242 	if (error)
4243 		return error;
4244 	if (bma->blkno == NULLFSBLOCK)
4245 		return -ENOSPC;
4246 
4247 	if (WARN_ON_ONCE(!xfs_valid_startblock(bma->ip, bma->blkno))) {
4248 		xfs_bmap_mark_sick(bma->ip, whichfork);
4249 		return -EFSCORRUPTED;
4250 	}
4251 
4252 	if (bma->flags & XFS_BMAPI_ZERO) {
4253 		error = xfs_zero_extent(bma->ip, bma->blkno, bma->length);
4254 		if (error)
4255 			return error;
4256 	}
4257 
4258 	if (ifp->if_format == XFS_DINODE_FMT_BTREE && !bma->cur)
4259 		bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
4260 	/*
4261 	 * Bump the number of extents we've allocated
4262 	 * in this call.
4263 	 */
4264 	bma->nallocs++;
4265 
4266 	if (bma->cur && bma->wasdel)
4267 		bma->cur->bc_flags |= XFS_BTREE_BMBT_WASDEL;
4268 
4269 	bma->got.br_startoff = bma->offset;
4270 	bma->got.br_startblock = bma->blkno;
4271 	bma->got.br_blockcount = bma->length;
4272 	bma->got.br_state = XFS_EXT_NORM;
4273 
4274 	if (bma->flags & XFS_BMAPI_PREALLOC)
4275 		bma->got.br_state = XFS_EXT_UNWRITTEN;
4276 
4277 	if (bma->wasdel)
4278 		error = xfs_bmap_add_extent_delay_real(bma, whichfork);
4279 	else
4280 		error = xfs_bmap_add_extent_hole_real(bma->tp, bma->ip,
4281 				whichfork, &bma->icur, &bma->cur, &bma->got,
4282 				&bma->logflags, bma->flags);
4283 	if (error)
4284 		return error;
4285 
4286 	/*
4287 	 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4288 	 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4289 	 * the neighbouring ones.
4290 	 */
4291 	xfs_iext_get_extent(ifp, &bma->icur, &bma->got);
4292 
4293 	ASSERT(bma->got.br_startoff <= bma->offset);
4294 	ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
4295 	       bma->offset + bma->length);
4296 	ASSERT(bma->got.br_state == XFS_EXT_NORM ||
4297 	       bma->got.br_state == XFS_EXT_UNWRITTEN);
4298 	return 0;
4299 }
4300 
4301 STATIC int
4302 xfs_bmapi_convert_unwritten(
4303 	struct xfs_bmalloca	*bma,
4304 	struct xfs_bmbt_irec	*mval,
4305 	xfs_filblks_t		len,
4306 	uint32_t		flags)
4307 {
4308 	int			whichfork = xfs_bmapi_whichfork(flags);
4309 	struct xfs_ifork	*ifp = xfs_ifork_ptr(bma->ip, whichfork);
4310 	int			tmp_logflags = 0;
4311 	int			error;
4312 
4313 	/* check if we need to do unwritten->real conversion */
4314 	if (mval->br_state == XFS_EXT_UNWRITTEN &&
4315 	    (flags & XFS_BMAPI_PREALLOC))
4316 		return 0;
4317 
4318 	/* check if we need to do real->unwritten conversion */
4319 	if (mval->br_state == XFS_EXT_NORM &&
4320 	    (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
4321 			(XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
4322 		return 0;
4323 
4324 	/*
4325 	 * Modify (by adding) the state flag, if writing.
4326 	 */
4327 	ASSERT(mval->br_blockcount <= len);
4328 	if (ifp->if_format == XFS_DINODE_FMT_BTREE && !bma->cur) {
4329 		bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
4330 					bma->ip, whichfork);
4331 	}
4332 	mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4333 				? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
4334 
4335 	/*
4336 	 * Before insertion into the bmbt, zero the range being converted
4337 	 * if required.
4338 	 */
4339 	if (flags & XFS_BMAPI_ZERO) {
4340 		error = xfs_zero_extent(bma->ip, mval->br_startblock,
4341 					mval->br_blockcount);
4342 		if (error)
4343 			return error;
4344 	}
4345 
4346 	error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, whichfork,
4347 			&bma->icur, &bma->cur, mval, &tmp_logflags);
4348 	/*
4349 	 * Log the inode core unconditionally in the unwritten extent conversion
4350 	 * path because the conversion might not have done so (e.g., if the
4351 	 * extent count hasn't changed). We need to make sure the inode is dirty
4352 	 * in the transaction for the sake of fsync(), even if nothing has
4353 	 * changed, because fsync() will not force the log for this transaction
4354 	 * unless it sees the inode pinned.
4355 	 *
4356 	 * Note: If we're only converting cow fork extents, there aren't
4357 	 * any on-disk updates to make, so we don't need to log anything.
4358 	 */
4359 	if (whichfork != XFS_COW_FORK)
4360 		bma->logflags |= tmp_logflags | XFS_ILOG_CORE;
4361 	if (error)
4362 		return error;
4363 
4364 	/*
4365 	 * Update our extent pointer, given that
4366 	 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4367 	 * of the neighbouring ones.
4368 	 */
4369 	xfs_iext_get_extent(ifp, &bma->icur, &bma->got);
4370 
4371 	/*
4372 	 * We may have combined previously unwritten space with written space,
4373 	 * so generate another request.
4374 	 */
4375 	if (mval->br_blockcount < len)
4376 		return -EAGAIN;
4377 	return 0;
4378 }
4379 
4380 xfs_extlen_t
4381 xfs_bmapi_minleft(
4382 	struct xfs_trans	*tp,
4383 	struct xfs_inode	*ip,
4384 	int			fork)
4385 {
4386 	struct xfs_ifork	*ifp = xfs_ifork_ptr(ip, fork);
4387 
4388 	if (tp && tp->t_highest_agno != NULLAGNUMBER)
4389 		return 0;
4390 	if (ifp->if_format != XFS_DINODE_FMT_BTREE)
4391 		return 1;
4392 	return be16_to_cpu(ifp->if_broot->bb_level) + 1;
4393 }
4394 
4395 /*
4396  * Log whatever the flags say, even if error.  Otherwise we might miss detecting
4397  * a case where the data is changed, there's an error, and it's not logged so we
4398  * don't shutdown when we should.  Don't bother logging extents/btree changes if
4399  * we converted to the other format.
4400  */
4401 static void
4402 xfs_bmapi_finish(
4403 	struct xfs_bmalloca	*bma,
4404 	int			whichfork,
4405 	int			error)
4406 {
4407 	struct xfs_ifork	*ifp = xfs_ifork_ptr(bma->ip, whichfork);
4408 
4409 	if ((bma->logflags & xfs_ilog_fext(whichfork)) &&
4410 	    ifp->if_format != XFS_DINODE_FMT_EXTENTS)
4411 		bma->logflags &= ~xfs_ilog_fext(whichfork);
4412 	else if ((bma->logflags & xfs_ilog_fbroot(whichfork)) &&
4413 		 ifp->if_format != XFS_DINODE_FMT_BTREE)
4414 		bma->logflags &= ~xfs_ilog_fbroot(whichfork);
4415 
4416 	if (bma->logflags)
4417 		xfs_trans_log_inode(bma->tp, bma->ip, bma->logflags);
4418 	if (bma->cur)
4419 		xfs_btree_del_cursor(bma->cur, error);
4420 }
4421 
4422 /*
4423  * Map file blocks to filesystem blocks, and allocate blocks or convert the
4424  * extent state if necessary.  Details behaviour is controlled by the flags
4425  * parameter.  Only allocates blocks from a single allocation group, to avoid
4426  * locking problems.
4427  *
4428  * Returns 0 on success and places the extent mappings in mval.  nmaps is used
4429  * as an input/output parameter where the caller specifies the maximum number
4430  * of mappings that may be returned and xfs_bmapi_write passes back the number
4431  * of mappings (including existing mappings) it found.
4432  *
4433  * Returns a negative error code on failure, including -ENOSPC when it could not
4434  * allocate any blocks and -ENOSR when it did allocate blocks to convert a
4435  * delalloc range, but those blocks were before the passed in range.
4436  */
4437 int
4438 xfs_bmapi_write(
4439 	struct xfs_trans	*tp,		/* transaction pointer */
4440 	struct xfs_inode	*ip,		/* incore inode */
4441 	xfs_fileoff_t		bno,		/* starting file offs. mapped */
4442 	xfs_filblks_t		len,		/* length to map in file */
4443 	uint32_t		flags,		/* XFS_BMAPI_... */
4444 	xfs_extlen_t		total,		/* total blocks needed */
4445 	struct xfs_bmbt_irec	*mval,		/* output: map values */
4446 	int			*nmap)		/* i/o: mval size/count */
4447 {
4448 	struct xfs_bmalloca	bma = {
4449 		.tp		= tp,
4450 		.ip		= ip,
4451 		.total		= total,
4452 	};
4453 	struct xfs_mount	*mp = ip->i_mount;
4454 	int			whichfork = xfs_bmapi_whichfork(flags);
4455 	struct xfs_ifork	*ifp = xfs_ifork_ptr(ip, whichfork);
4456 	xfs_fileoff_t		end;		/* end of mapped file region */
4457 	bool			eof = false;	/* after the end of extents */
4458 	int			error;		/* error return */
4459 	int			n;		/* current extent index */
4460 	xfs_fileoff_t		obno;		/* old block number (offset) */
4461 
4462 #ifdef DEBUG
4463 	xfs_fileoff_t		orig_bno;	/* original block number value */
4464 	int			orig_flags;	/* original flags arg value */
4465 	xfs_filblks_t		orig_len;	/* original value of len arg */
4466 	struct xfs_bmbt_irec	*orig_mval;	/* original value of mval */
4467 	int			orig_nmap;	/* original value of *nmap */
4468 
4469 	orig_bno = bno;
4470 	orig_len = len;
4471 	orig_flags = flags;
4472 	orig_mval = mval;
4473 	orig_nmap = *nmap;
4474 #endif
4475 
4476 	ASSERT(*nmap >= 1);
4477 	ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4478 	ASSERT(tp != NULL);
4479 	ASSERT(len > 0);
4480 	ASSERT(ifp->if_format != XFS_DINODE_FMT_LOCAL);
4481 	xfs_assert_ilocked(ip, XFS_ILOCK_EXCL);
4482 	ASSERT(!(flags & XFS_BMAPI_REMAP));
4483 
4484 	/* zeroing is for currently only for data extents, not metadata */
4485 	ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) !=
4486 			(XFS_BMAPI_METADATA | XFS_BMAPI_ZERO));
4487 	/*
4488 	 * we can allocate unwritten extents or pre-zero allocated blocks,
4489 	 * but it makes no sense to do both at once. This would result in
4490 	 * zeroing the unwritten extent twice, but it still being an
4491 	 * unwritten extent....
4492 	 */
4493 	ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) !=
4494 			(XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO));
4495 
4496 	if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
4497 	    XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
4498 		xfs_bmap_mark_sick(ip, whichfork);
4499 		return -EFSCORRUPTED;
4500 	}
4501 
4502 	if (xfs_is_shutdown(mp))
4503 		return -EIO;
4504 
4505 	XFS_STATS_INC(mp, xs_blk_mapw);
4506 
4507 	error = xfs_iread_extents(tp, ip, whichfork);
4508 	if (error)
4509 		goto error0;
4510 
4511 	if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.icur, &bma.got))
4512 		eof = true;
4513 	if (!xfs_iext_peek_prev_extent(ifp, &bma.icur, &bma.prev))
4514 		bma.prev.br_startoff = NULLFILEOFF;
4515 	bma.minleft = xfs_bmapi_minleft(tp, ip, whichfork);
4516 
4517 	n = 0;
4518 	end = bno + len;
4519 	obno = bno;
4520 	while (bno < end && n < *nmap) {
4521 		bool			need_alloc = false, wasdelay = false;
4522 
4523 		/* in hole or beyond EOF? */
4524 		if (eof || bma.got.br_startoff > bno) {
4525 			/*
4526 			 * CoW fork conversions should /never/ hit EOF or
4527 			 * holes.  There should always be something for us
4528 			 * to work on.
4529 			 */
4530 			ASSERT(!((flags & XFS_BMAPI_CONVERT) &&
4531 			         (flags & XFS_BMAPI_COWFORK)));
4532 
4533 			need_alloc = true;
4534 		} else if (isnullstartblock(bma.got.br_startblock)) {
4535 			wasdelay = true;
4536 		}
4537 
4538 		/*
4539 		 * First, deal with the hole before the allocated space
4540 		 * that we found, if any.
4541 		 */
4542 		if (need_alloc || wasdelay) {
4543 			bma.eof = eof;
4544 			bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4545 			bma.wasdel = wasdelay;
4546 			bma.offset = bno;
4547 			bma.flags = flags;
4548 
4549 			/*
4550 			 * There's a 32/64 bit type mismatch between the
4551 			 * allocation length request (which can be 64 bits in
4552 			 * length) and the bma length request, which is
4553 			 * xfs_extlen_t and therefore 32 bits. Hence we have to
4554 			 * be careful and do the min() using the larger type to
4555 			 * avoid overflows.
4556 			 */
4557 			bma.length = XFS_FILBLKS_MIN(len, XFS_MAX_BMBT_EXTLEN);
4558 
4559 			if (wasdelay) {
4560 				bma.length = XFS_FILBLKS_MIN(bma.length,
4561 					bma.got.br_blockcount -
4562 					(bno - bma.got.br_startoff));
4563 			} else {
4564 				if (!eof)
4565 					bma.length = XFS_FILBLKS_MIN(bma.length,
4566 						bma.got.br_startoff - bno);
4567 			}
4568 
4569 			ASSERT(bma.length > 0);
4570 			error = xfs_bmapi_allocate(&bma);
4571 			if (error) {
4572 				/*
4573 				 * If we already allocated space in a previous
4574 				 * iteration return what we go so far when
4575 				 * running out of space.
4576 				 */
4577 				if (error == -ENOSPC && bma.nallocs)
4578 					break;
4579 				goto error0;
4580 			}
4581 
4582 			/*
4583 			 * If this is a CoW allocation, record the data in
4584 			 * the refcount btree for orphan recovery.
4585 			 */
4586 			if (whichfork == XFS_COW_FORK)
4587 				xfs_refcount_alloc_cow_extent(tp, bma.blkno,
4588 						bma.length);
4589 		}
4590 
4591 		/* Deal with the allocated space we found.  */
4592 		xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
4593 							end, n, flags);
4594 
4595 		/* Execute unwritten extent conversion if necessary */
4596 		error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
4597 		if (error == -EAGAIN)
4598 			continue;
4599 		if (error)
4600 			goto error0;
4601 
4602 		/* update the extent map to return */
4603 		xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4604 
4605 		/*
4606 		 * If we're done, stop now.  Stop when we've allocated
4607 		 * XFS_BMAP_MAX_NMAP extents no matter what.  Otherwise
4608 		 * the transaction may get too big.
4609 		 */
4610 		if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
4611 			break;
4612 
4613 		/* Else go on to the next record. */
4614 		bma.prev = bma.got;
4615 		if (!xfs_iext_next_extent(ifp, &bma.icur, &bma.got))
4616 			eof = true;
4617 	}
4618 
4619 	error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, &bma.logflags,
4620 			whichfork);
4621 	if (error)
4622 		goto error0;
4623 
4624 	ASSERT(ifp->if_format != XFS_DINODE_FMT_BTREE ||
4625 	       ifp->if_nextents > XFS_IFORK_MAXEXT(ip, whichfork));
4626 	xfs_bmapi_finish(&bma, whichfork, 0);
4627 	xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4628 		orig_nmap, n);
4629 
4630 	/*
4631 	 * When converting delayed allocations, xfs_bmapi_allocate ignores
4632 	 * the passed in bno and always converts from the start of the found
4633 	 * delalloc extent.
4634 	 *
4635 	 * To avoid a successful return with *nmap set to 0, return the magic
4636 	 * -ENOSR error code for this particular case so that the caller can
4637 	 * handle it.
4638 	 */
4639 	if (!n) {
4640 		ASSERT(bma.nallocs >= *nmap);
4641 		return -ENOSR;
4642 	}
4643 	*nmap = n;
4644 	return 0;
4645 error0:
4646 	xfs_bmapi_finish(&bma, whichfork, error);
4647 	return error;
4648 }
4649 
4650 /*
4651  * Convert an existing delalloc extent to real blocks based on file offset. This
4652  * attempts to allocate the entire delalloc extent and may require multiple
4653  * invocations to allocate the target offset if a large enough physical extent
4654  * is not available.
4655  */
4656 static int
4657 xfs_bmapi_convert_one_delalloc(
4658 	struct xfs_inode	*ip,
4659 	int			whichfork,
4660 	xfs_off_t		offset,
4661 	struct iomap		*iomap,
4662 	unsigned int		*seq)
4663 {
4664 	struct xfs_ifork	*ifp = xfs_ifork_ptr(ip, whichfork);
4665 	struct xfs_mount	*mp = ip->i_mount;
4666 	xfs_fileoff_t		offset_fsb = XFS_B_TO_FSBT(mp, offset);
4667 	struct xfs_bmalloca	bma = { NULL };
4668 	uint16_t		flags = 0;
4669 	struct xfs_trans	*tp;
4670 	int			error;
4671 
4672 	if (whichfork == XFS_COW_FORK)
4673 		flags |= IOMAP_F_SHARED;
4674 
4675 	/*
4676 	 * Space for the extent and indirect blocks was reserved when the
4677 	 * delalloc extent was created so there's no need to do so here.
4678 	 */
4679 	error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0,
4680 				XFS_TRANS_RESERVE, &tp);
4681 	if (error)
4682 		return error;
4683 
4684 	xfs_ilock(ip, XFS_ILOCK_EXCL);
4685 	xfs_trans_ijoin(tp, ip, 0);
4686 
4687 	error = xfs_iext_count_extend(tp, ip, whichfork,
4688 			XFS_IEXT_ADD_NOSPLIT_CNT);
4689 	if (error)
4690 		goto out_trans_cancel;
4691 
4692 	if (!xfs_iext_lookup_extent(ip, ifp, offset_fsb, &bma.icur, &bma.got) ||
4693 	    bma.got.br_startoff > offset_fsb) {
4694 		/*
4695 		 * No extent found in the range we are trying to convert.  This
4696 		 * should only happen for the COW fork, where another thread
4697 		 * might have moved the extent to the data fork in the meantime.
4698 		 */
4699 		WARN_ON_ONCE(whichfork != XFS_COW_FORK);
4700 		error = -EAGAIN;
4701 		goto out_trans_cancel;
4702 	}
4703 
4704 	/*
4705 	 * If we find a real extent here we raced with another thread converting
4706 	 * the extent.  Just return the real extent at this offset.
4707 	 */
4708 	if (!isnullstartblock(bma.got.br_startblock)) {
4709 		xfs_bmbt_to_iomap(ip, iomap, &bma.got, 0, flags,
4710 				xfs_iomap_inode_sequence(ip, flags));
4711 		if (seq)
4712 			*seq = READ_ONCE(ifp->if_seq);
4713 		goto out_trans_cancel;
4714 	}
4715 
4716 	bma.tp = tp;
4717 	bma.ip = ip;
4718 	bma.wasdel = true;
4719 	bma.minleft = xfs_bmapi_minleft(tp, ip, whichfork);
4720 
4721 	/*
4722 	 * Always allocate convert from the start of the delalloc extent even if
4723 	 * that is outside the passed in range to create large contiguous
4724 	 * extents on disk.
4725 	 */
4726 	bma.offset = bma.got.br_startoff;
4727 	bma.length = bma.got.br_blockcount;
4728 
4729 	/*
4730 	 * When we're converting the delalloc reservations backing dirty pages
4731 	 * in the page cache, we must be careful about how we create the new
4732 	 * extents:
4733 	 *
4734 	 * New CoW fork extents are created unwritten, turned into real extents
4735 	 * when we're about to write the data to disk, and mapped into the data
4736 	 * fork after the write finishes.  End of story.
4737 	 *
4738 	 * New data fork extents must be mapped in as unwritten and converted
4739 	 * to real extents after the write succeeds to avoid exposing stale
4740 	 * disk contents if we crash.
4741 	 */
4742 	bma.flags = XFS_BMAPI_PREALLOC;
4743 	if (whichfork == XFS_COW_FORK)
4744 		bma.flags |= XFS_BMAPI_COWFORK;
4745 
4746 	if (!xfs_iext_peek_prev_extent(ifp, &bma.icur, &bma.prev))
4747 		bma.prev.br_startoff = NULLFILEOFF;
4748 
4749 	error = xfs_bmapi_allocate(&bma);
4750 	if (error)
4751 		goto out_finish;
4752 
4753 	XFS_STATS_ADD(mp, xs_xstrat_bytes, XFS_FSB_TO_B(mp, bma.length));
4754 	XFS_STATS_INC(mp, xs_xstrat_quick);
4755 
4756 	ASSERT(!isnullstartblock(bma.got.br_startblock));
4757 	xfs_bmbt_to_iomap(ip, iomap, &bma.got, 0, flags,
4758 				xfs_iomap_inode_sequence(ip, flags));
4759 	if (seq)
4760 		*seq = READ_ONCE(ifp->if_seq);
4761 
4762 	if (whichfork == XFS_COW_FORK)
4763 		xfs_refcount_alloc_cow_extent(tp, bma.blkno, bma.length);
4764 
4765 	error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, &bma.logflags,
4766 			whichfork);
4767 	if (error)
4768 		goto out_finish;
4769 
4770 	xfs_bmapi_finish(&bma, whichfork, 0);
4771 	error = xfs_trans_commit(tp);
4772 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
4773 	return error;
4774 
4775 out_finish:
4776 	xfs_bmapi_finish(&bma, whichfork, error);
4777 out_trans_cancel:
4778 	xfs_trans_cancel(tp);
4779 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
4780 	return error;
4781 }
4782 
4783 /*
4784  * Pass in a dellalloc extent and convert it to real extents, return the real
4785  * extent that maps offset_fsb in iomap.
4786  */
4787 int
4788 xfs_bmapi_convert_delalloc(
4789 	struct xfs_inode	*ip,
4790 	int			whichfork,
4791 	loff_t			offset,
4792 	struct iomap		*iomap,
4793 	unsigned int		*seq)
4794 {
4795 	int			error;
4796 
4797 	/*
4798 	 * Attempt to allocate whatever delalloc extent currently backs offset
4799 	 * and put the result into iomap.  Allocate in a loop because it may
4800 	 * take several attempts to allocate real blocks for a contiguous
4801 	 * delalloc extent if free space is sufficiently fragmented.
4802 	 */
4803 	do {
4804 		error = xfs_bmapi_convert_one_delalloc(ip, whichfork, offset,
4805 					iomap, seq);
4806 		if (error)
4807 			return error;
4808 	} while (iomap->offset + iomap->length <= offset);
4809 
4810 	return 0;
4811 }
4812 
4813 int
4814 xfs_bmapi_remap(
4815 	struct xfs_trans	*tp,
4816 	struct xfs_inode	*ip,
4817 	xfs_fileoff_t		bno,
4818 	xfs_filblks_t		len,
4819 	xfs_fsblock_t		startblock,
4820 	uint32_t		flags)
4821 {
4822 	struct xfs_mount	*mp = ip->i_mount;
4823 	struct xfs_ifork	*ifp;
4824 	struct xfs_btree_cur	*cur = NULL;
4825 	struct xfs_bmbt_irec	got;
4826 	struct xfs_iext_cursor	icur;
4827 	int			whichfork = xfs_bmapi_whichfork(flags);
4828 	int			logflags = 0, error;
4829 
4830 	ifp = xfs_ifork_ptr(ip, whichfork);
4831 	ASSERT(len > 0);
4832 	ASSERT(len <= (xfs_filblks_t)XFS_MAX_BMBT_EXTLEN);
4833 	xfs_assert_ilocked(ip, XFS_ILOCK_EXCL);
4834 	ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC |
4835 			   XFS_BMAPI_NORMAP)));
4836 	ASSERT((flags & (XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC)) !=
4837 			(XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC));
4838 
4839 	if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
4840 	    XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
4841 		xfs_bmap_mark_sick(ip, whichfork);
4842 		return -EFSCORRUPTED;
4843 	}
4844 
4845 	if (xfs_is_shutdown(mp))
4846 		return -EIO;
4847 
4848 	error = xfs_iread_extents(tp, ip, whichfork);
4849 	if (error)
4850 		return error;
4851 
4852 	if (xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got)) {
4853 		/* make sure we only reflink into a hole. */
4854 		ASSERT(got.br_startoff > bno);
4855 		ASSERT(got.br_startoff - bno >= len);
4856 	}
4857 
4858 	ip->i_nblocks += len;
4859 	ip->i_delayed_blks -= len; /* see xfs_bmap_defer_add */
4860 	xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
4861 
4862 	if (ifp->if_format == XFS_DINODE_FMT_BTREE)
4863 		cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
4864 
4865 	got.br_startoff = bno;
4866 	got.br_startblock = startblock;
4867 	got.br_blockcount = len;
4868 	if (flags & XFS_BMAPI_PREALLOC)
4869 		got.br_state = XFS_EXT_UNWRITTEN;
4870 	else
4871 		got.br_state = XFS_EXT_NORM;
4872 
4873 	error = xfs_bmap_add_extent_hole_real(tp, ip, whichfork, &icur,
4874 			&cur, &got, &logflags, flags);
4875 	if (error)
4876 		goto error0;
4877 
4878 	error = xfs_bmap_btree_to_extents(tp, ip, cur, &logflags, whichfork);
4879 
4880 error0:
4881 	if (ip->i_df.if_format != XFS_DINODE_FMT_EXTENTS)
4882 		logflags &= ~XFS_ILOG_DEXT;
4883 	else if (ip->i_df.if_format != XFS_DINODE_FMT_BTREE)
4884 		logflags &= ~XFS_ILOG_DBROOT;
4885 
4886 	if (logflags)
4887 		xfs_trans_log_inode(tp, ip, logflags);
4888 	if (cur)
4889 		xfs_btree_del_cursor(cur, error);
4890 	return error;
4891 }
4892 
4893 /*
4894  * When a delalloc extent is split (e.g., due to a hole punch), the original
4895  * indlen reservation must be shared across the two new extents that are left
4896  * behind.
4897  *
4898  * Given the original reservation and the worst case indlen for the two new
4899  * extents (as calculated by xfs_bmap_worst_indlen()), split the original
4900  * reservation fairly across the two new extents. If necessary, steal available
4901  * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4902  * ores == 1). The number of stolen blocks is returned. The availability and
4903  * subsequent accounting of stolen blocks is the responsibility of the caller.
4904  */
4905 static void
4906 xfs_bmap_split_indlen(
4907 	xfs_filblks_t			ores,		/* original res. */
4908 	xfs_filblks_t			*indlen1,	/* ext1 worst indlen */
4909 	xfs_filblks_t			*indlen2)	/* ext2 worst indlen */
4910 {
4911 	xfs_filblks_t			len1 = *indlen1;
4912 	xfs_filblks_t			len2 = *indlen2;
4913 	xfs_filblks_t			nres = len1 + len2; /* new total res. */
4914 	xfs_filblks_t			resfactor;
4915 
4916 	/*
4917 	 * We can't meet the total required reservation for the two extents.
4918 	 * Calculate the percent of the overall shortage between both extents
4919 	 * and apply this percentage to each of the requested indlen values.
4920 	 * This distributes the shortage fairly and reduces the chances that one
4921 	 * of the two extents is left with nothing when extents are repeatedly
4922 	 * split.
4923 	 */
4924 	resfactor = (ores * 100);
4925 	do_div(resfactor, nres);
4926 	len1 *= resfactor;
4927 	do_div(len1, 100);
4928 	len2 *= resfactor;
4929 	do_div(len2, 100);
4930 	ASSERT(len1 + len2 <= ores);
4931 	ASSERT(len1 < *indlen1 && len2 < *indlen2);
4932 
4933 	/*
4934 	 * Hand out the remainder to each extent. If one of the two reservations
4935 	 * is zero, we want to make sure that one gets a block first. The loop
4936 	 * below starts with len1, so hand len2 a block right off the bat if it
4937 	 * is zero.
4938 	 */
4939 	ores -= (len1 + len2);
4940 	ASSERT((*indlen1 - len1) + (*indlen2 - len2) >= ores);
4941 	if (ores && !len2 && *indlen2) {
4942 		len2++;
4943 		ores--;
4944 	}
4945 	while (ores) {
4946 		if (len1 < *indlen1) {
4947 			len1++;
4948 			ores--;
4949 		}
4950 		if (!ores)
4951 			break;
4952 		if (len2 < *indlen2) {
4953 			len2++;
4954 			ores--;
4955 		}
4956 	}
4957 
4958 	*indlen1 = len1;
4959 	*indlen2 = len2;
4960 }
4961 
4962 void
4963 xfs_bmap_del_extent_delay(
4964 	struct xfs_inode	*ip,
4965 	int			whichfork,
4966 	struct xfs_iext_cursor	*icur,
4967 	struct xfs_bmbt_irec	*got,
4968 	struct xfs_bmbt_irec	*del)
4969 {
4970 	struct xfs_mount	*mp = ip->i_mount;
4971 	struct xfs_ifork	*ifp = xfs_ifork_ptr(ip, whichfork);
4972 	struct xfs_bmbt_irec	new;
4973 	int64_t			da_old, da_new, da_diff = 0;
4974 	xfs_fileoff_t		del_endoff, got_endoff;
4975 	xfs_filblks_t		got_indlen, new_indlen, stolen = 0;
4976 	uint32_t		state = xfs_bmap_fork_to_state(whichfork);
4977 	uint64_t		fdblocks;
4978 	bool			isrt;
4979 
4980 	XFS_STATS_INC(mp, xs_del_exlist);
4981 
4982 	isrt = xfs_ifork_is_realtime(ip, whichfork);
4983 	del_endoff = del->br_startoff + del->br_blockcount;
4984 	got_endoff = got->br_startoff + got->br_blockcount;
4985 	da_old = startblockval(got->br_startblock);
4986 	da_new = 0;
4987 
4988 	ASSERT(del->br_blockcount > 0);
4989 	ASSERT(got->br_startoff <= del->br_startoff);
4990 	ASSERT(got_endoff >= del_endoff);
4991 
4992 	/*
4993 	 * Update the inode delalloc counter now and wait to update the
4994 	 * sb counters as we might have to borrow some blocks for the
4995 	 * indirect block accounting.
4996 	 */
4997 	xfs_quota_unreserve_blkres(ip, del->br_blockcount);
4998 	ip->i_delayed_blks -= del->br_blockcount;
4999 
5000 	if (got->br_startoff == del->br_startoff)
5001 		state |= BMAP_LEFT_FILLING;
5002 	if (got_endoff == del_endoff)
5003 		state |= BMAP_RIGHT_FILLING;
5004 
5005 	switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
5006 	case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
5007 		/*
5008 		 * Matches the whole extent.  Delete the entry.
5009 		 */
5010 		xfs_iext_remove(ip, icur, state);
5011 		xfs_iext_prev(ifp, icur);
5012 		break;
5013 	case BMAP_LEFT_FILLING:
5014 		/*
5015 		 * Deleting the first part of the extent.
5016 		 */
5017 		got->br_startoff = del_endoff;
5018 		got->br_blockcount -= del->br_blockcount;
5019 		da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
5020 				got->br_blockcount), da_old);
5021 		got->br_startblock = nullstartblock((int)da_new);
5022 		xfs_iext_update_extent(ip, state, icur, got);
5023 		break;
5024 	case BMAP_RIGHT_FILLING:
5025 		/*
5026 		 * Deleting the last part of the extent.
5027 		 */
5028 		got->br_blockcount = got->br_blockcount - del->br_blockcount;
5029 		da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
5030 				got->br_blockcount), da_old);
5031 		got->br_startblock = nullstartblock((int)da_new);
5032 		xfs_iext_update_extent(ip, state, icur, got);
5033 		break;
5034 	case 0:
5035 		/*
5036 		 * Deleting the middle of the extent.
5037 		 *
5038 		 * Distribute the original indlen reservation across the two new
5039 		 * extents.  Steal blocks from the deleted extent if necessary.
5040 		 * Stealing blocks simply fudges the fdblocks accounting below.
5041 		 * Warn if either of the new indlen reservations is zero as this
5042 		 * can lead to delalloc problems.
5043 		 */
5044 		got->br_blockcount = del->br_startoff - got->br_startoff;
5045 		got_indlen = xfs_bmap_worst_indlen(ip, got->br_blockcount);
5046 
5047 		new.br_blockcount = got_endoff - del_endoff;
5048 		new_indlen = xfs_bmap_worst_indlen(ip, new.br_blockcount);
5049 
5050 		WARN_ON_ONCE(!got_indlen || !new_indlen);
5051 		/*
5052 		 * Steal as many blocks as we can to try and satisfy the worst
5053 		 * case indlen for both new extents.
5054 		 *
5055 		 * However, we can't just steal reservations from the data
5056 		 * blocks if this is an RT inodes as the data and metadata
5057 		 * blocks come from different pools.  We'll have to live with
5058 		 * under-filled indirect reservation in this case.
5059 		 */
5060 		da_new = got_indlen + new_indlen;
5061 		if (da_new > da_old && !isrt) {
5062 			stolen = XFS_FILBLKS_MIN(da_new - da_old,
5063 						 del->br_blockcount);
5064 			da_old += stolen;
5065 		}
5066 		if (da_new > da_old)
5067 			xfs_bmap_split_indlen(da_old, &got_indlen, &new_indlen);
5068 		da_new = got_indlen + new_indlen;
5069 
5070 		got->br_startblock = nullstartblock((int)got_indlen);
5071 
5072 		new.br_startoff = del_endoff;
5073 		new.br_state = got->br_state;
5074 		new.br_startblock = nullstartblock((int)new_indlen);
5075 
5076 		xfs_iext_update_extent(ip, state, icur, got);
5077 		xfs_iext_next(ifp, icur);
5078 		xfs_iext_insert(ip, icur, &new, state);
5079 
5080 		del->br_blockcount -= stolen;
5081 		break;
5082 	}
5083 
5084 	ASSERT(da_old >= da_new);
5085 	da_diff = da_old - da_new;
5086 	fdblocks = da_diff;
5087 
5088 	if (isrt)
5089 		xfs_add_frextents(mp, xfs_rtb_to_rtx(mp, del->br_blockcount));
5090 	else
5091 		fdblocks += del->br_blockcount;
5092 
5093 	xfs_add_fdblocks(mp, fdblocks);
5094 	xfs_mod_delalloc(ip, -(int64_t)del->br_blockcount, -da_diff);
5095 }
5096 
5097 void
5098 xfs_bmap_del_extent_cow(
5099 	struct xfs_inode	*ip,
5100 	struct xfs_iext_cursor	*icur,
5101 	struct xfs_bmbt_irec	*got,
5102 	struct xfs_bmbt_irec	*del)
5103 {
5104 	struct xfs_mount	*mp = ip->i_mount;
5105 	struct xfs_ifork	*ifp = xfs_ifork_ptr(ip, XFS_COW_FORK);
5106 	struct xfs_bmbt_irec	new;
5107 	xfs_fileoff_t		del_endoff, got_endoff;
5108 	uint32_t		state = BMAP_COWFORK;
5109 
5110 	XFS_STATS_INC(mp, xs_del_exlist);
5111 
5112 	del_endoff = del->br_startoff + del->br_blockcount;
5113 	got_endoff = got->br_startoff + got->br_blockcount;
5114 
5115 	ASSERT(del->br_blockcount > 0);
5116 	ASSERT(got->br_startoff <= del->br_startoff);
5117 	ASSERT(got_endoff >= del_endoff);
5118 	ASSERT(!isnullstartblock(got->br_startblock));
5119 
5120 	if (got->br_startoff == del->br_startoff)
5121 		state |= BMAP_LEFT_FILLING;
5122 	if (got_endoff == del_endoff)
5123 		state |= BMAP_RIGHT_FILLING;
5124 
5125 	switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
5126 	case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
5127 		/*
5128 		 * Matches the whole extent.  Delete the entry.
5129 		 */
5130 		xfs_iext_remove(ip, icur, state);
5131 		xfs_iext_prev(ifp, icur);
5132 		break;
5133 	case BMAP_LEFT_FILLING:
5134 		/*
5135 		 * Deleting the first part of the extent.
5136 		 */
5137 		got->br_startoff = del_endoff;
5138 		got->br_blockcount -= del->br_blockcount;
5139 		got->br_startblock = del->br_startblock + del->br_blockcount;
5140 		xfs_iext_update_extent(ip, state, icur, got);
5141 		break;
5142 	case BMAP_RIGHT_FILLING:
5143 		/*
5144 		 * Deleting the last part of the extent.
5145 		 */
5146 		got->br_blockcount -= del->br_blockcount;
5147 		xfs_iext_update_extent(ip, state, icur, got);
5148 		break;
5149 	case 0:
5150 		/*
5151 		 * Deleting the middle of the extent.
5152 		 */
5153 		got->br_blockcount = del->br_startoff - got->br_startoff;
5154 
5155 		new.br_startoff = del_endoff;
5156 		new.br_blockcount = got_endoff - del_endoff;
5157 		new.br_state = got->br_state;
5158 		new.br_startblock = del->br_startblock + del->br_blockcount;
5159 
5160 		xfs_iext_update_extent(ip, state, icur, got);
5161 		xfs_iext_next(ifp, icur);
5162 		xfs_iext_insert(ip, icur, &new, state);
5163 		break;
5164 	}
5165 	ip->i_delayed_blks -= del->br_blockcount;
5166 }
5167 
5168 /*
5169  * Called by xfs_bmapi to update file extent records and the btree
5170  * after removing space.
5171  */
5172 STATIC int				/* error */
5173 xfs_bmap_del_extent_real(
5174 	xfs_inode_t		*ip,	/* incore inode pointer */
5175 	xfs_trans_t		*tp,	/* current transaction pointer */
5176 	struct xfs_iext_cursor	*icur,
5177 	struct xfs_btree_cur	*cur,	/* if null, not a btree */
5178 	xfs_bmbt_irec_t		*del,	/* data to remove from extents */
5179 	int			*logflagsp, /* inode logging flags */
5180 	int			whichfork, /* data or attr fork */
5181 	uint32_t		bflags)	/* bmapi flags */
5182 {
5183 	xfs_fsblock_t		del_endblock=0;	/* first block past del */
5184 	xfs_fileoff_t		del_endoff;	/* first offset past del */
5185 	int			error = 0;	/* error return value */
5186 	struct xfs_bmbt_irec	got;	/* current extent entry */
5187 	xfs_fileoff_t		got_endoff;	/* first offset past got */
5188 	int			i;	/* temp state */
5189 	struct xfs_ifork	*ifp;	/* inode fork pointer */
5190 	xfs_mount_t		*mp;	/* mount structure */
5191 	xfs_filblks_t		nblks;	/* quota/sb block count */
5192 	xfs_bmbt_irec_t		new;	/* new record to be inserted */
5193 	/* REFERENCED */
5194 	uint			qfield;	/* quota field to update */
5195 	uint32_t		state = xfs_bmap_fork_to_state(whichfork);
5196 	struct xfs_bmbt_irec	old;
5197 
5198 	*logflagsp = 0;
5199 
5200 	mp = ip->i_mount;
5201 	XFS_STATS_INC(mp, xs_del_exlist);
5202 
5203 	ifp = xfs_ifork_ptr(ip, whichfork);
5204 	ASSERT(del->br_blockcount > 0);
5205 	xfs_iext_get_extent(ifp, icur, &got);
5206 	ASSERT(got.br_startoff <= del->br_startoff);
5207 	del_endoff = del->br_startoff + del->br_blockcount;
5208 	got_endoff = got.br_startoff + got.br_blockcount;
5209 	ASSERT(got_endoff >= del_endoff);
5210 	ASSERT(!isnullstartblock(got.br_startblock));
5211 	qfield = 0;
5212 
5213 	/*
5214 	 * If it's the case where the directory code is running with no block
5215 	 * reservation, and the deleted block is in the middle of its extent,
5216 	 * and the resulting insert of an extent would cause transformation to
5217 	 * btree format, then reject it.  The calling code will then swap blocks
5218 	 * around instead.  We have to do this now, rather than waiting for the
5219 	 * conversion to btree format, since the transaction will be dirty then.
5220 	 */
5221 	if (tp->t_blk_res == 0 &&
5222 	    ifp->if_format == XFS_DINODE_FMT_EXTENTS &&
5223 	    ifp->if_nextents >= XFS_IFORK_MAXEXT(ip, whichfork) &&
5224 	    del->br_startoff > got.br_startoff && del_endoff < got_endoff)
5225 		return -ENOSPC;
5226 
5227 	*logflagsp = XFS_ILOG_CORE;
5228 	if (xfs_ifork_is_realtime(ip, whichfork))
5229 		qfield = XFS_TRANS_DQ_RTBCOUNT;
5230 	else
5231 		qfield = XFS_TRANS_DQ_BCOUNT;
5232 	nblks = del->br_blockcount;
5233 
5234 	del_endblock = del->br_startblock + del->br_blockcount;
5235 	if (cur) {
5236 		error = xfs_bmbt_lookup_eq(cur, &got, &i);
5237 		if (error)
5238 			return error;
5239 		if (XFS_IS_CORRUPT(mp, i != 1)) {
5240 			xfs_btree_mark_sick(cur);
5241 			return -EFSCORRUPTED;
5242 		}
5243 	}
5244 
5245 	if (got.br_startoff == del->br_startoff)
5246 		state |= BMAP_LEFT_FILLING;
5247 	if (got_endoff == del_endoff)
5248 		state |= BMAP_RIGHT_FILLING;
5249 
5250 	switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
5251 	case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
5252 		/*
5253 		 * Matches the whole extent.  Delete the entry.
5254 		 */
5255 		xfs_iext_remove(ip, icur, state);
5256 		xfs_iext_prev(ifp, icur);
5257 		ifp->if_nextents--;
5258 
5259 		*logflagsp |= XFS_ILOG_CORE;
5260 		if (!cur) {
5261 			*logflagsp |= xfs_ilog_fext(whichfork);
5262 			break;
5263 		}
5264 		if ((error = xfs_btree_delete(cur, &i)))
5265 			return error;
5266 		if (XFS_IS_CORRUPT(mp, i != 1)) {
5267 			xfs_btree_mark_sick(cur);
5268 			return -EFSCORRUPTED;
5269 		}
5270 		break;
5271 	case BMAP_LEFT_FILLING:
5272 		/*
5273 		 * Deleting the first part of the extent.
5274 		 */
5275 		got.br_startoff = del_endoff;
5276 		got.br_startblock = del_endblock;
5277 		got.br_blockcount -= del->br_blockcount;
5278 		xfs_iext_update_extent(ip, state, icur, &got);
5279 		if (!cur) {
5280 			*logflagsp |= xfs_ilog_fext(whichfork);
5281 			break;
5282 		}
5283 		error = xfs_bmbt_update(cur, &got);
5284 		if (error)
5285 			return error;
5286 		break;
5287 	case BMAP_RIGHT_FILLING:
5288 		/*
5289 		 * Deleting the last part of the extent.
5290 		 */
5291 		got.br_blockcount -= del->br_blockcount;
5292 		xfs_iext_update_extent(ip, state, icur, &got);
5293 		if (!cur) {
5294 			*logflagsp |= xfs_ilog_fext(whichfork);
5295 			break;
5296 		}
5297 		error = xfs_bmbt_update(cur, &got);
5298 		if (error)
5299 			return error;
5300 		break;
5301 	case 0:
5302 		/*
5303 		 * Deleting the middle of the extent.
5304 		 */
5305 
5306 		old = got;
5307 
5308 		got.br_blockcount = del->br_startoff - got.br_startoff;
5309 		xfs_iext_update_extent(ip, state, icur, &got);
5310 
5311 		new.br_startoff = del_endoff;
5312 		new.br_blockcount = got_endoff - del_endoff;
5313 		new.br_state = got.br_state;
5314 		new.br_startblock = del_endblock;
5315 
5316 		*logflagsp |= XFS_ILOG_CORE;
5317 		if (cur) {
5318 			error = xfs_bmbt_update(cur, &got);
5319 			if (error)
5320 				return error;
5321 			error = xfs_btree_increment(cur, 0, &i);
5322 			if (error)
5323 				return error;
5324 			cur->bc_rec.b = new;
5325 			error = xfs_btree_insert(cur, &i);
5326 			if (error && error != -ENOSPC)
5327 				return error;
5328 			/*
5329 			 * If get no-space back from btree insert, it tried a
5330 			 * split, and we have a zero block reservation.  Fix up
5331 			 * our state and return the error.
5332 			 */
5333 			if (error == -ENOSPC) {
5334 				/*
5335 				 * Reset the cursor, don't trust it after any
5336 				 * insert operation.
5337 				 */
5338 				error = xfs_bmbt_lookup_eq(cur, &got, &i);
5339 				if (error)
5340 					return error;
5341 				if (XFS_IS_CORRUPT(mp, i != 1)) {
5342 					xfs_btree_mark_sick(cur);
5343 					return -EFSCORRUPTED;
5344 				}
5345 				/*
5346 				 * Update the btree record back
5347 				 * to the original value.
5348 				 */
5349 				error = xfs_bmbt_update(cur, &old);
5350 				if (error)
5351 					return error;
5352 				/*
5353 				 * Reset the extent record back
5354 				 * to the original value.
5355 				 */
5356 				xfs_iext_update_extent(ip, state, icur, &old);
5357 				*logflagsp = 0;
5358 				return -ENOSPC;
5359 			}
5360 			if (XFS_IS_CORRUPT(mp, i != 1)) {
5361 				xfs_btree_mark_sick(cur);
5362 				return -EFSCORRUPTED;
5363 			}
5364 		} else
5365 			*logflagsp |= xfs_ilog_fext(whichfork);
5366 
5367 		ifp->if_nextents++;
5368 		xfs_iext_next(ifp, icur);
5369 		xfs_iext_insert(ip, icur, &new, state);
5370 		break;
5371 	}
5372 
5373 	/* remove reverse mapping */
5374 	xfs_rmap_unmap_extent(tp, ip, whichfork, del);
5375 
5376 	/*
5377 	 * If we need to, add to list of extents to delete.
5378 	 */
5379 	if (!(bflags & XFS_BMAPI_REMAP)) {
5380 		if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) {
5381 			xfs_refcount_decrease_extent(tp, del);
5382 		} else if (xfs_ifork_is_realtime(ip, whichfork)) {
5383 			/*
5384 			 * Ensure the bitmap and summary inodes are locked
5385 			 * and joined to the transaction before modifying them.
5386 			 */
5387 			if (!(tp->t_flags & XFS_TRANS_RTBITMAP_LOCKED)) {
5388 				tp->t_flags |= XFS_TRANS_RTBITMAP_LOCKED;
5389 				xfs_rtbitmap_lock(mp);
5390 				xfs_rtbitmap_trans_join(tp);
5391 			}
5392 			error = xfs_rtfree_blocks(tp, del->br_startblock,
5393 					del->br_blockcount);
5394 		} else {
5395 			unsigned int	efi_flags = 0;
5396 
5397 			if ((bflags & XFS_BMAPI_NODISCARD) ||
5398 			    del->br_state == XFS_EXT_UNWRITTEN)
5399 				efi_flags |= XFS_FREE_EXTENT_SKIP_DISCARD;
5400 
5401 			error = xfs_free_extent_later(tp, del->br_startblock,
5402 					del->br_blockcount, NULL,
5403 					XFS_AG_RESV_NONE, efi_flags);
5404 		}
5405 		if (error)
5406 			return error;
5407 	}
5408 
5409 	/*
5410 	 * Adjust inode # blocks in the file.
5411 	 */
5412 	if (nblks)
5413 		ip->i_nblocks -= nblks;
5414 	/*
5415 	 * Adjust quota data.
5416 	 */
5417 	if (qfield && !(bflags & XFS_BMAPI_REMAP))
5418 		xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
5419 
5420 	return 0;
5421 }
5422 
5423 /*
5424  * Unmap (remove) blocks from a file.
5425  * If nexts is nonzero then the number of extents to remove is limited to
5426  * that value.  If not all extents in the block range can be removed then
5427  * *done is set.
5428  */
5429 static int
5430 __xfs_bunmapi(
5431 	struct xfs_trans	*tp,		/* transaction pointer */
5432 	struct xfs_inode	*ip,		/* incore inode */
5433 	xfs_fileoff_t		start,		/* first file offset deleted */
5434 	xfs_filblks_t		*rlen,		/* i/o: amount remaining */
5435 	uint32_t		flags,		/* misc flags */
5436 	xfs_extnum_t		nexts)		/* number of extents max */
5437 {
5438 	struct xfs_btree_cur	*cur;		/* bmap btree cursor */
5439 	struct xfs_bmbt_irec	del;		/* extent being deleted */
5440 	int			error;		/* error return value */
5441 	xfs_extnum_t		extno;		/* extent number in list */
5442 	struct xfs_bmbt_irec	got;		/* current extent record */
5443 	struct xfs_ifork	*ifp;		/* inode fork pointer */
5444 	int			isrt;		/* freeing in rt area */
5445 	int			logflags;	/* transaction logging flags */
5446 	xfs_extlen_t		mod;		/* rt extent offset */
5447 	struct xfs_mount	*mp = ip->i_mount;
5448 	int			tmp_logflags;	/* partial logging flags */
5449 	int			wasdel;		/* was a delayed alloc extent */
5450 	int			whichfork;	/* data or attribute fork */
5451 	xfs_filblks_t		len = *rlen;	/* length to unmap in file */
5452 	xfs_fileoff_t		end;
5453 	struct xfs_iext_cursor	icur;
5454 	bool			done = false;
5455 
5456 	trace_xfs_bunmap(ip, start, len, flags, _RET_IP_);
5457 
5458 	whichfork = xfs_bmapi_whichfork(flags);
5459 	ASSERT(whichfork != XFS_COW_FORK);
5460 	ifp = xfs_ifork_ptr(ip, whichfork);
5461 	if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp))) {
5462 		xfs_bmap_mark_sick(ip, whichfork);
5463 		return -EFSCORRUPTED;
5464 	}
5465 	if (xfs_is_shutdown(mp))
5466 		return -EIO;
5467 
5468 	xfs_assert_ilocked(ip, XFS_ILOCK_EXCL);
5469 	ASSERT(len > 0);
5470 	ASSERT(nexts >= 0);
5471 
5472 	error = xfs_iread_extents(tp, ip, whichfork);
5473 	if (error)
5474 		return error;
5475 
5476 	if (xfs_iext_count(ifp) == 0) {
5477 		*rlen = 0;
5478 		return 0;
5479 	}
5480 	XFS_STATS_INC(mp, xs_blk_unmap);
5481 	isrt = xfs_ifork_is_realtime(ip, whichfork);
5482 	end = start + len;
5483 
5484 	if (!xfs_iext_lookup_extent_before(ip, ifp, &end, &icur, &got)) {
5485 		*rlen = 0;
5486 		return 0;
5487 	}
5488 	end--;
5489 
5490 	logflags = 0;
5491 	if (ifp->if_format == XFS_DINODE_FMT_BTREE) {
5492 		ASSERT(ifp->if_format == XFS_DINODE_FMT_BTREE);
5493 		cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5494 	} else
5495 		cur = NULL;
5496 
5497 	extno = 0;
5498 	while (end != (xfs_fileoff_t)-1 && end >= start &&
5499 	       (nexts == 0 || extno < nexts)) {
5500 		/*
5501 		 * Is the found extent after a hole in which end lives?
5502 		 * Just back up to the previous extent, if so.
5503 		 */
5504 		if (got.br_startoff > end &&
5505 		    !xfs_iext_prev_extent(ifp, &icur, &got)) {
5506 			done = true;
5507 			break;
5508 		}
5509 		/*
5510 		 * Is the last block of this extent before the range
5511 		 * we're supposed to delete?  If so, we're done.
5512 		 */
5513 		end = XFS_FILEOFF_MIN(end,
5514 			got.br_startoff + got.br_blockcount - 1);
5515 		if (end < start)
5516 			break;
5517 		/*
5518 		 * Then deal with the (possibly delayed) allocated space
5519 		 * we found.
5520 		 */
5521 		del = got;
5522 		wasdel = isnullstartblock(del.br_startblock);
5523 
5524 		if (got.br_startoff < start) {
5525 			del.br_startoff = start;
5526 			del.br_blockcount -= start - got.br_startoff;
5527 			if (!wasdel)
5528 				del.br_startblock += start - got.br_startoff;
5529 		}
5530 		if (del.br_startoff + del.br_blockcount > end + 1)
5531 			del.br_blockcount = end + 1 - del.br_startoff;
5532 
5533 		if (!isrt || (flags & XFS_BMAPI_REMAP))
5534 			goto delete;
5535 
5536 		mod = xfs_rtb_to_rtxoff(mp,
5537 				del.br_startblock + del.br_blockcount);
5538 		if (mod) {
5539 			/*
5540 			 * Realtime extent not lined up at the end.
5541 			 * The extent could have been split into written
5542 			 * and unwritten pieces, or we could just be
5543 			 * unmapping part of it.  But we can't really
5544 			 * get rid of part of a realtime extent.
5545 			 */
5546 			if (del.br_state == XFS_EXT_UNWRITTEN) {
5547 				/*
5548 				 * This piece is unwritten, or we're not
5549 				 * using unwritten extents.  Skip over it.
5550 				 */
5551 				ASSERT((flags & XFS_BMAPI_REMAP) || end >= mod);
5552 				end -= mod > del.br_blockcount ?
5553 					del.br_blockcount : mod;
5554 				if (end < got.br_startoff &&
5555 				    !xfs_iext_prev_extent(ifp, &icur, &got)) {
5556 					done = true;
5557 					break;
5558 				}
5559 				continue;
5560 			}
5561 			/*
5562 			 * It's written, turn it unwritten.
5563 			 * This is better than zeroing it.
5564 			 */
5565 			ASSERT(del.br_state == XFS_EXT_NORM);
5566 			ASSERT(tp->t_blk_res > 0);
5567 			/*
5568 			 * If this spans a realtime extent boundary,
5569 			 * chop it back to the start of the one we end at.
5570 			 */
5571 			if (del.br_blockcount > mod) {
5572 				del.br_startoff += del.br_blockcount - mod;
5573 				del.br_startblock += del.br_blockcount - mod;
5574 				del.br_blockcount = mod;
5575 			}
5576 			del.br_state = XFS_EXT_UNWRITTEN;
5577 			error = xfs_bmap_add_extent_unwritten_real(tp, ip,
5578 					whichfork, &icur, &cur, &del,
5579 					&logflags);
5580 			if (error)
5581 				goto error0;
5582 			goto nodelete;
5583 		}
5584 
5585 		mod = xfs_rtb_to_rtxoff(mp, del.br_startblock);
5586 		if (mod) {
5587 			xfs_extlen_t off = mp->m_sb.sb_rextsize - mod;
5588 
5589 			/*
5590 			 * Realtime extent is lined up at the end but not
5591 			 * at the front.  We'll get rid of full extents if
5592 			 * we can.
5593 			 */
5594 			if (del.br_blockcount > off) {
5595 				del.br_blockcount -= off;
5596 				del.br_startoff += off;
5597 				del.br_startblock += off;
5598 			} else if (del.br_startoff == start &&
5599 				   (del.br_state == XFS_EXT_UNWRITTEN ||
5600 				    tp->t_blk_res == 0)) {
5601 				/*
5602 				 * Can't make it unwritten.  There isn't
5603 				 * a full extent here so just skip it.
5604 				 */
5605 				ASSERT(end >= del.br_blockcount);
5606 				end -= del.br_blockcount;
5607 				if (got.br_startoff > end &&
5608 				    !xfs_iext_prev_extent(ifp, &icur, &got)) {
5609 					done = true;
5610 					break;
5611 				}
5612 				continue;
5613 			} else if (del.br_state == XFS_EXT_UNWRITTEN) {
5614 				struct xfs_bmbt_irec	prev;
5615 				xfs_fileoff_t		unwrite_start;
5616 
5617 				/*
5618 				 * This one is already unwritten.
5619 				 * It must have a written left neighbor.
5620 				 * Unwrite the killed part of that one and
5621 				 * try again.
5622 				 */
5623 				if (!xfs_iext_prev_extent(ifp, &icur, &prev))
5624 					ASSERT(0);
5625 				ASSERT(prev.br_state == XFS_EXT_NORM);
5626 				ASSERT(!isnullstartblock(prev.br_startblock));
5627 				ASSERT(del.br_startblock ==
5628 				       prev.br_startblock + prev.br_blockcount);
5629 				unwrite_start = max3(start,
5630 						     del.br_startoff - mod,
5631 						     prev.br_startoff);
5632 				mod = unwrite_start - prev.br_startoff;
5633 				prev.br_startoff = unwrite_start;
5634 				prev.br_startblock += mod;
5635 				prev.br_blockcount -= mod;
5636 				prev.br_state = XFS_EXT_UNWRITTEN;
5637 				error = xfs_bmap_add_extent_unwritten_real(tp,
5638 						ip, whichfork, &icur, &cur,
5639 						&prev, &logflags);
5640 				if (error)
5641 					goto error0;
5642 				goto nodelete;
5643 			} else {
5644 				ASSERT(del.br_state == XFS_EXT_NORM);
5645 				del.br_state = XFS_EXT_UNWRITTEN;
5646 				error = xfs_bmap_add_extent_unwritten_real(tp,
5647 						ip, whichfork, &icur, &cur,
5648 						&del, &logflags);
5649 				if (error)
5650 					goto error0;
5651 				goto nodelete;
5652 			}
5653 		}
5654 
5655 delete:
5656 		if (wasdel) {
5657 			xfs_bmap_del_extent_delay(ip, whichfork, &icur, &got, &del);
5658 		} else {
5659 			error = xfs_bmap_del_extent_real(ip, tp, &icur, cur,
5660 					&del, &tmp_logflags, whichfork,
5661 					flags);
5662 			logflags |= tmp_logflags;
5663 			if (error)
5664 				goto error0;
5665 		}
5666 
5667 		end = del.br_startoff - 1;
5668 nodelete:
5669 		/*
5670 		 * If not done go on to the next (previous) record.
5671 		 */
5672 		if (end != (xfs_fileoff_t)-1 && end >= start) {
5673 			if (!xfs_iext_get_extent(ifp, &icur, &got) ||
5674 			    (got.br_startoff > end &&
5675 			     !xfs_iext_prev_extent(ifp, &icur, &got))) {
5676 				done = true;
5677 				break;
5678 			}
5679 			extno++;
5680 		}
5681 	}
5682 	if (done || end == (xfs_fileoff_t)-1 || end < start)
5683 		*rlen = 0;
5684 	else
5685 		*rlen = end - start + 1;
5686 
5687 	/*
5688 	 * Convert to a btree if necessary.
5689 	 */
5690 	if (xfs_bmap_needs_btree(ip, whichfork)) {
5691 		ASSERT(cur == NULL);
5692 		error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0,
5693 				&tmp_logflags, whichfork);
5694 		logflags |= tmp_logflags;
5695 	} else {
5696 		error = xfs_bmap_btree_to_extents(tp, ip, cur, &logflags,
5697 			whichfork);
5698 	}
5699 
5700 error0:
5701 	/*
5702 	 * Log everything.  Do this after conversion, there's no point in
5703 	 * logging the extent records if we've converted to btree format.
5704 	 */
5705 	if ((logflags & xfs_ilog_fext(whichfork)) &&
5706 	    ifp->if_format != XFS_DINODE_FMT_EXTENTS)
5707 		logflags &= ~xfs_ilog_fext(whichfork);
5708 	else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
5709 		 ifp->if_format != XFS_DINODE_FMT_BTREE)
5710 		logflags &= ~xfs_ilog_fbroot(whichfork);
5711 	/*
5712 	 * Log inode even in the error case, if the transaction
5713 	 * is dirty we'll need to shut down the filesystem.
5714 	 */
5715 	if (logflags)
5716 		xfs_trans_log_inode(tp, ip, logflags);
5717 	if (cur) {
5718 		if (!error)
5719 			cur->bc_bmap.allocated = 0;
5720 		xfs_btree_del_cursor(cur, error);
5721 	}
5722 	return error;
5723 }
5724 
5725 /* Unmap a range of a file. */
5726 int
5727 xfs_bunmapi(
5728 	xfs_trans_t		*tp,
5729 	struct xfs_inode	*ip,
5730 	xfs_fileoff_t		bno,
5731 	xfs_filblks_t		len,
5732 	uint32_t		flags,
5733 	xfs_extnum_t		nexts,
5734 	int			*done)
5735 {
5736 	int			error;
5737 
5738 	error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts);
5739 	*done = (len == 0);
5740 	return error;
5741 }
5742 
5743 /*
5744  * Determine whether an extent shift can be accomplished by a merge with the
5745  * extent that precedes the target hole of the shift.
5746  */
5747 STATIC bool
5748 xfs_bmse_can_merge(
5749 	struct xfs_bmbt_irec	*left,	/* preceding extent */
5750 	struct xfs_bmbt_irec	*got,	/* current extent to shift */
5751 	xfs_fileoff_t		shift)	/* shift fsb */
5752 {
5753 	xfs_fileoff_t		startoff;
5754 
5755 	startoff = got->br_startoff - shift;
5756 
5757 	/*
5758 	 * The extent, once shifted, must be adjacent in-file and on-disk with
5759 	 * the preceding extent.
5760 	 */
5761 	if ((left->br_startoff + left->br_blockcount != startoff) ||
5762 	    (left->br_startblock + left->br_blockcount != got->br_startblock) ||
5763 	    (left->br_state != got->br_state) ||
5764 	    (left->br_blockcount + got->br_blockcount > XFS_MAX_BMBT_EXTLEN))
5765 		return false;
5766 
5767 	return true;
5768 }
5769 
5770 /*
5771  * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5772  * hole in the file. If an extent shift would result in the extent being fully
5773  * adjacent to the extent that currently precedes the hole, we can merge with
5774  * the preceding extent rather than do the shift.
5775  *
5776  * This function assumes the caller has verified a shift-by-merge is possible
5777  * with the provided extents via xfs_bmse_can_merge().
5778  */
5779 STATIC int
5780 xfs_bmse_merge(
5781 	struct xfs_trans		*tp,
5782 	struct xfs_inode		*ip,
5783 	int				whichfork,
5784 	xfs_fileoff_t			shift,		/* shift fsb */
5785 	struct xfs_iext_cursor		*icur,
5786 	struct xfs_bmbt_irec		*got,		/* extent to shift */
5787 	struct xfs_bmbt_irec		*left,		/* preceding extent */
5788 	struct xfs_btree_cur		*cur,
5789 	int				*logflags)	/* output */
5790 {
5791 	struct xfs_ifork		*ifp = xfs_ifork_ptr(ip, whichfork);
5792 	struct xfs_bmbt_irec		new;
5793 	xfs_filblks_t			blockcount;
5794 	int				error, i;
5795 	struct xfs_mount		*mp = ip->i_mount;
5796 
5797 	blockcount = left->br_blockcount + got->br_blockcount;
5798 
5799 	xfs_assert_ilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
5800 	ASSERT(xfs_bmse_can_merge(left, got, shift));
5801 
5802 	new = *left;
5803 	new.br_blockcount = blockcount;
5804 
5805 	/*
5806 	 * Update the on-disk extent count, the btree if necessary and log the
5807 	 * inode.
5808 	 */
5809 	ifp->if_nextents--;
5810 	*logflags |= XFS_ILOG_CORE;
5811 	if (!cur) {
5812 		*logflags |= XFS_ILOG_DEXT;
5813 		goto done;
5814 	}
5815 
5816 	/* lookup and remove the extent to merge */
5817 	error = xfs_bmbt_lookup_eq(cur, got, &i);
5818 	if (error)
5819 		return error;
5820 	if (XFS_IS_CORRUPT(mp, i != 1)) {
5821 		xfs_btree_mark_sick(cur);
5822 		return -EFSCORRUPTED;
5823 	}
5824 
5825 	error = xfs_btree_delete(cur, &i);
5826 	if (error)
5827 		return error;
5828 	if (XFS_IS_CORRUPT(mp, i != 1)) {
5829 		xfs_btree_mark_sick(cur);
5830 		return -EFSCORRUPTED;
5831 	}
5832 
5833 	/* lookup and update size of the previous extent */
5834 	error = xfs_bmbt_lookup_eq(cur, left, &i);
5835 	if (error)
5836 		return error;
5837 	if (XFS_IS_CORRUPT(mp, i != 1)) {
5838 		xfs_btree_mark_sick(cur);
5839 		return -EFSCORRUPTED;
5840 	}
5841 
5842 	error = xfs_bmbt_update(cur, &new);
5843 	if (error)
5844 		return error;
5845 
5846 	/* change to extent format if required after extent removal */
5847 	error = xfs_bmap_btree_to_extents(tp, ip, cur, logflags, whichfork);
5848 	if (error)
5849 		return error;
5850 
5851 done:
5852 	xfs_iext_remove(ip, icur, 0);
5853 	xfs_iext_prev(ifp, icur);
5854 	xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur,
5855 			&new);
5856 
5857 	/* update reverse mapping. rmap functions merge the rmaps for us */
5858 	xfs_rmap_unmap_extent(tp, ip, whichfork, got);
5859 	memcpy(&new, got, sizeof(new));
5860 	new.br_startoff = left->br_startoff + left->br_blockcount;
5861 	xfs_rmap_map_extent(tp, ip, whichfork, &new);
5862 	return 0;
5863 }
5864 
5865 static int
5866 xfs_bmap_shift_update_extent(
5867 	struct xfs_trans	*tp,
5868 	struct xfs_inode	*ip,
5869 	int			whichfork,
5870 	struct xfs_iext_cursor	*icur,
5871 	struct xfs_bmbt_irec	*got,
5872 	struct xfs_btree_cur	*cur,
5873 	int			*logflags,
5874 	xfs_fileoff_t		startoff)
5875 {
5876 	struct xfs_mount	*mp = ip->i_mount;
5877 	struct xfs_bmbt_irec	prev = *got;
5878 	int			error, i;
5879 
5880 	*logflags |= XFS_ILOG_CORE;
5881 
5882 	got->br_startoff = startoff;
5883 
5884 	if (cur) {
5885 		error = xfs_bmbt_lookup_eq(cur, &prev, &i);
5886 		if (error)
5887 			return error;
5888 		if (XFS_IS_CORRUPT(mp, i != 1)) {
5889 			xfs_btree_mark_sick(cur);
5890 			return -EFSCORRUPTED;
5891 		}
5892 
5893 		error = xfs_bmbt_update(cur, got);
5894 		if (error)
5895 			return error;
5896 	} else {
5897 		*logflags |= XFS_ILOG_DEXT;
5898 	}
5899 
5900 	xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur,
5901 			got);
5902 
5903 	/* update reverse mapping */
5904 	xfs_rmap_unmap_extent(tp, ip, whichfork, &prev);
5905 	xfs_rmap_map_extent(tp, ip, whichfork, got);
5906 	return 0;
5907 }
5908 
5909 int
5910 xfs_bmap_collapse_extents(
5911 	struct xfs_trans	*tp,
5912 	struct xfs_inode	*ip,
5913 	xfs_fileoff_t		*next_fsb,
5914 	xfs_fileoff_t		offset_shift_fsb,
5915 	bool			*done)
5916 {
5917 	int			whichfork = XFS_DATA_FORK;
5918 	struct xfs_mount	*mp = ip->i_mount;
5919 	struct xfs_ifork	*ifp = xfs_ifork_ptr(ip, whichfork);
5920 	struct xfs_btree_cur	*cur = NULL;
5921 	struct xfs_bmbt_irec	got, prev;
5922 	struct xfs_iext_cursor	icur;
5923 	xfs_fileoff_t		new_startoff;
5924 	int			error = 0;
5925 	int			logflags = 0;
5926 
5927 	if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
5928 	    XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
5929 		xfs_bmap_mark_sick(ip, whichfork);
5930 		return -EFSCORRUPTED;
5931 	}
5932 
5933 	if (xfs_is_shutdown(mp))
5934 		return -EIO;
5935 
5936 	xfs_assert_ilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
5937 
5938 	error = xfs_iread_extents(tp, ip, whichfork);
5939 	if (error)
5940 		return error;
5941 
5942 	if (ifp->if_format == XFS_DINODE_FMT_BTREE)
5943 		cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5944 
5945 	if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) {
5946 		*done = true;
5947 		goto del_cursor;
5948 	}
5949 	if (XFS_IS_CORRUPT(mp, isnullstartblock(got.br_startblock))) {
5950 		xfs_bmap_mark_sick(ip, whichfork);
5951 		error = -EFSCORRUPTED;
5952 		goto del_cursor;
5953 	}
5954 
5955 	new_startoff = got.br_startoff - offset_shift_fsb;
5956 	if (xfs_iext_peek_prev_extent(ifp, &icur, &prev)) {
5957 		if (new_startoff < prev.br_startoff + prev.br_blockcount) {
5958 			error = -EINVAL;
5959 			goto del_cursor;
5960 		}
5961 
5962 		if (xfs_bmse_can_merge(&prev, &got, offset_shift_fsb)) {
5963 			error = xfs_bmse_merge(tp, ip, whichfork,
5964 					offset_shift_fsb, &icur, &got, &prev,
5965 					cur, &logflags);
5966 			if (error)
5967 				goto del_cursor;
5968 			goto done;
5969 		}
5970 	} else {
5971 		if (got.br_startoff < offset_shift_fsb) {
5972 			error = -EINVAL;
5973 			goto del_cursor;
5974 		}
5975 	}
5976 
5977 	error = xfs_bmap_shift_update_extent(tp, ip, whichfork, &icur, &got,
5978 			cur, &logflags, new_startoff);
5979 	if (error)
5980 		goto del_cursor;
5981 
5982 done:
5983 	if (!xfs_iext_next_extent(ifp, &icur, &got)) {
5984 		*done = true;
5985 		goto del_cursor;
5986 	}
5987 
5988 	*next_fsb = got.br_startoff;
5989 del_cursor:
5990 	if (cur)
5991 		xfs_btree_del_cursor(cur, error);
5992 	if (logflags)
5993 		xfs_trans_log_inode(tp, ip, logflags);
5994 	return error;
5995 }
5996 
5997 /* Make sure we won't be right-shifting an extent past the maximum bound. */
5998 int
5999 xfs_bmap_can_insert_extents(
6000 	struct xfs_inode	*ip,
6001 	xfs_fileoff_t		off,
6002 	xfs_fileoff_t		shift)
6003 {
6004 	struct xfs_bmbt_irec	got;
6005 	int			is_empty;
6006 	int			error = 0;
6007 
6008 	xfs_assert_ilocked(ip, XFS_IOLOCK_EXCL);
6009 
6010 	if (xfs_is_shutdown(ip->i_mount))
6011 		return -EIO;
6012 
6013 	xfs_ilock(ip, XFS_ILOCK_EXCL);
6014 	error = xfs_bmap_last_extent(NULL, ip, XFS_DATA_FORK, &got, &is_empty);
6015 	if (!error && !is_empty && got.br_startoff >= off &&
6016 	    ((got.br_startoff + shift) & BMBT_STARTOFF_MASK) < got.br_startoff)
6017 		error = -EINVAL;
6018 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
6019 
6020 	return error;
6021 }
6022 
6023 int
6024 xfs_bmap_insert_extents(
6025 	struct xfs_trans	*tp,
6026 	struct xfs_inode	*ip,
6027 	xfs_fileoff_t		*next_fsb,
6028 	xfs_fileoff_t		offset_shift_fsb,
6029 	bool			*done,
6030 	xfs_fileoff_t		stop_fsb)
6031 {
6032 	int			whichfork = XFS_DATA_FORK;
6033 	struct xfs_mount	*mp = ip->i_mount;
6034 	struct xfs_ifork	*ifp = xfs_ifork_ptr(ip, whichfork);
6035 	struct xfs_btree_cur	*cur = NULL;
6036 	struct xfs_bmbt_irec	got, next;
6037 	struct xfs_iext_cursor	icur;
6038 	xfs_fileoff_t		new_startoff;
6039 	int			error = 0;
6040 	int			logflags = 0;
6041 
6042 	if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
6043 	    XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
6044 		xfs_bmap_mark_sick(ip, whichfork);
6045 		return -EFSCORRUPTED;
6046 	}
6047 
6048 	if (xfs_is_shutdown(mp))
6049 		return -EIO;
6050 
6051 	xfs_assert_ilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
6052 
6053 	error = xfs_iread_extents(tp, ip, whichfork);
6054 	if (error)
6055 		return error;
6056 
6057 	if (ifp->if_format == XFS_DINODE_FMT_BTREE)
6058 		cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
6059 
6060 	if (*next_fsb == NULLFSBLOCK) {
6061 		xfs_iext_last(ifp, &icur);
6062 		if (!xfs_iext_get_extent(ifp, &icur, &got) ||
6063 		    stop_fsb > got.br_startoff) {
6064 			*done = true;
6065 			goto del_cursor;
6066 		}
6067 	} else {
6068 		if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) {
6069 			*done = true;
6070 			goto del_cursor;
6071 		}
6072 	}
6073 	if (XFS_IS_CORRUPT(mp, isnullstartblock(got.br_startblock))) {
6074 		xfs_bmap_mark_sick(ip, whichfork);
6075 		error = -EFSCORRUPTED;
6076 		goto del_cursor;
6077 	}
6078 
6079 	if (XFS_IS_CORRUPT(mp, stop_fsb > got.br_startoff)) {
6080 		xfs_bmap_mark_sick(ip, whichfork);
6081 		error = -EFSCORRUPTED;
6082 		goto del_cursor;
6083 	}
6084 
6085 	new_startoff = got.br_startoff + offset_shift_fsb;
6086 	if (xfs_iext_peek_next_extent(ifp, &icur, &next)) {
6087 		if (new_startoff + got.br_blockcount > next.br_startoff) {
6088 			error = -EINVAL;
6089 			goto del_cursor;
6090 		}
6091 
6092 		/*
6093 		 * Unlike a left shift (which involves a hole punch), a right
6094 		 * shift does not modify extent neighbors in any way.  We should
6095 		 * never find mergeable extents in this scenario.  Check anyways
6096 		 * and warn if we encounter two extents that could be one.
6097 		 */
6098 		if (xfs_bmse_can_merge(&got, &next, offset_shift_fsb))
6099 			WARN_ON_ONCE(1);
6100 	}
6101 
6102 	error = xfs_bmap_shift_update_extent(tp, ip, whichfork, &icur, &got,
6103 			cur, &logflags, new_startoff);
6104 	if (error)
6105 		goto del_cursor;
6106 
6107 	if (!xfs_iext_prev_extent(ifp, &icur, &got) ||
6108 	    stop_fsb >= got.br_startoff + got.br_blockcount) {
6109 		*done = true;
6110 		goto del_cursor;
6111 	}
6112 
6113 	*next_fsb = got.br_startoff;
6114 del_cursor:
6115 	if (cur)
6116 		xfs_btree_del_cursor(cur, error);
6117 	if (logflags)
6118 		xfs_trans_log_inode(tp, ip, logflags);
6119 	return error;
6120 }
6121 
6122 /*
6123  * Splits an extent into two extents at split_fsb block such that it is the
6124  * first block of the current_ext. @ext is a target extent to be split.
6125  * @split_fsb is a block where the extents is split.  If split_fsb lies in a
6126  * hole or the first block of extents, just return 0.
6127  */
6128 int
6129 xfs_bmap_split_extent(
6130 	struct xfs_trans	*tp,
6131 	struct xfs_inode	*ip,
6132 	xfs_fileoff_t		split_fsb)
6133 {
6134 	int				whichfork = XFS_DATA_FORK;
6135 	struct xfs_ifork		*ifp = xfs_ifork_ptr(ip, whichfork);
6136 	struct xfs_btree_cur		*cur = NULL;
6137 	struct xfs_bmbt_irec		got;
6138 	struct xfs_bmbt_irec		new; /* split extent */
6139 	struct xfs_mount		*mp = ip->i_mount;
6140 	xfs_fsblock_t			gotblkcnt; /* new block count for got */
6141 	struct xfs_iext_cursor		icur;
6142 	int				error = 0;
6143 	int				logflags = 0;
6144 	int				i = 0;
6145 
6146 	if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
6147 	    XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
6148 		xfs_bmap_mark_sick(ip, whichfork);
6149 		return -EFSCORRUPTED;
6150 	}
6151 
6152 	if (xfs_is_shutdown(mp))
6153 		return -EIO;
6154 
6155 	/* Read in all the extents */
6156 	error = xfs_iread_extents(tp, ip, whichfork);
6157 	if (error)
6158 		return error;
6159 
6160 	/*
6161 	 * If there are not extents, or split_fsb lies in a hole we are done.
6162 	 */
6163 	if (!xfs_iext_lookup_extent(ip, ifp, split_fsb, &icur, &got) ||
6164 	    got.br_startoff >= split_fsb)
6165 		return 0;
6166 
6167 	gotblkcnt = split_fsb - got.br_startoff;
6168 	new.br_startoff = split_fsb;
6169 	new.br_startblock = got.br_startblock + gotblkcnt;
6170 	new.br_blockcount = got.br_blockcount - gotblkcnt;
6171 	new.br_state = got.br_state;
6172 
6173 	if (ifp->if_format == XFS_DINODE_FMT_BTREE) {
6174 		cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
6175 		error = xfs_bmbt_lookup_eq(cur, &got, &i);
6176 		if (error)
6177 			goto del_cursor;
6178 		if (XFS_IS_CORRUPT(mp, i != 1)) {
6179 			xfs_btree_mark_sick(cur);
6180 			error = -EFSCORRUPTED;
6181 			goto del_cursor;
6182 		}
6183 	}
6184 
6185 	got.br_blockcount = gotblkcnt;
6186 	xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), &icur,
6187 			&got);
6188 
6189 	logflags = XFS_ILOG_CORE;
6190 	if (cur) {
6191 		error = xfs_bmbt_update(cur, &got);
6192 		if (error)
6193 			goto del_cursor;
6194 	} else
6195 		logflags |= XFS_ILOG_DEXT;
6196 
6197 	/* Add new extent */
6198 	xfs_iext_next(ifp, &icur);
6199 	xfs_iext_insert(ip, &icur, &new, 0);
6200 	ifp->if_nextents++;
6201 
6202 	if (cur) {
6203 		error = xfs_bmbt_lookup_eq(cur, &new, &i);
6204 		if (error)
6205 			goto del_cursor;
6206 		if (XFS_IS_CORRUPT(mp, i != 0)) {
6207 			xfs_btree_mark_sick(cur);
6208 			error = -EFSCORRUPTED;
6209 			goto del_cursor;
6210 		}
6211 		error = xfs_btree_insert(cur, &i);
6212 		if (error)
6213 			goto del_cursor;
6214 		if (XFS_IS_CORRUPT(mp, i != 1)) {
6215 			xfs_btree_mark_sick(cur);
6216 			error = -EFSCORRUPTED;
6217 			goto del_cursor;
6218 		}
6219 	}
6220 
6221 	/*
6222 	 * Convert to a btree if necessary.
6223 	 */
6224 	if (xfs_bmap_needs_btree(ip, whichfork)) {
6225 		int tmp_logflags; /* partial log flag return val */
6226 
6227 		ASSERT(cur == NULL);
6228 		error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0,
6229 				&tmp_logflags, whichfork);
6230 		logflags |= tmp_logflags;
6231 	}
6232 
6233 del_cursor:
6234 	if (cur) {
6235 		cur->bc_bmap.allocated = 0;
6236 		xfs_btree_del_cursor(cur, error);
6237 	}
6238 
6239 	if (logflags)
6240 		xfs_trans_log_inode(tp, ip, logflags);
6241 	return error;
6242 }
6243 
6244 /* Record a bmap intent. */
6245 static inline void
6246 __xfs_bmap_add(
6247 	struct xfs_trans		*tp,
6248 	enum xfs_bmap_intent_type	type,
6249 	struct xfs_inode		*ip,
6250 	int				whichfork,
6251 	struct xfs_bmbt_irec		*bmap)
6252 {
6253 	struct xfs_bmap_intent		*bi;
6254 
6255 	if ((whichfork != XFS_DATA_FORK && whichfork != XFS_ATTR_FORK) ||
6256 	    bmap->br_startblock == HOLESTARTBLOCK ||
6257 	    bmap->br_startblock == DELAYSTARTBLOCK)
6258 		return;
6259 
6260 	bi = kmem_cache_alloc(xfs_bmap_intent_cache, GFP_KERNEL | __GFP_NOFAIL);
6261 	INIT_LIST_HEAD(&bi->bi_list);
6262 	bi->bi_type = type;
6263 	bi->bi_owner = ip;
6264 	bi->bi_whichfork = whichfork;
6265 	bi->bi_bmap = *bmap;
6266 
6267 	xfs_bmap_defer_add(tp, bi);
6268 }
6269 
6270 /* Map an extent into a file. */
6271 void
6272 xfs_bmap_map_extent(
6273 	struct xfs_trans	*tp,
6274 	struct xfs_inode	*ip,
6275 	int			whichfork,
6276 	struct xfs_bmbt_irec	*PREV)
6277 {
6278 	__xfs_bmap_add(tp, XFS_BMAP_MAP, ip, whichfork, PREV);
6279 }
6280 
6281 /* Unmap an extent out of a file. */
6282 void
6283 xfs_bmap_unmap_extent(
6284 	struct xfs_trans	*tp,
6285 	struct xfs_inode	*ip,
6286 	int			whichfork,
6287 	struct xfs_bmbt_irec	*PREV)
6288 {
6289 	__xfs_bmap_add(tp, XFS_BMAP_UNMAP, ip, whichfork, PREV);
6290 }
6291 
6292 /*
6293  * Process one of the deferred bmap operations.  We pass back the
6294  * btree cursor to maintain our lock on the bmapbt between calls.
6295  */
6296 int
6297 xfs_bmap_finish_one(
6298 	struct xfs_trans		*tp,
6299 	struct xfs_bmap_intent		*bi)
6300 {
6301 	struct xfs_bmbt_irec		*bmap = &bi->bi_bmap;
6302 	int				error = 0;
6303 	int				flags = 0;
6304 
6305 	if (bi->bi_whichfork == XFS_ATTR_FORK)
6306 		flags |= XFS_BMAPI_ATTRFORK;
6307 
6308 	ASSERT(tp->t_highest_agno == NULLAGNUMBER);
6309 
6310 	trace_xfs_bmap_deferred(bi);
6311 
6312 	if (XFS_TEST_ERROR(false, tp->t_mountp, XFS_ERRTAG_BMAP_FINISH_ONE))
6313 		return -EIO;
6314 
6315 	switch (bi->bi_type) {
6316 	case XFS_BMAP_MAP:
6317 		if (bi->bi_bmap.br_state == XFS_EXT_UNWRITTEN)
6318 			flags |= XFS_BMAPI_PREALLOC;
6319 		error = xfs_bmapi_remap(tp, bi->bi_owner, bmap->br_startoff,
6320 				bmap->br_blockcount, bmap->br_startblock,
6321 				flags);
6322 		bmap->br_blockcount = 0;
6323 		break;
6324 	case XFS_BMAP_UNMAP:
6325 		error = __xfs_bunmapi(tp, bi->bi_owner, bmap->br_startoff,
6326 				&bmap->br_blockcount, flags | XFS_BMAPI_REMAP,
6327 				1);
6328 		break;
6329 	default:
6330 		ASSERT(0);
6331 		xfs_bmap_mark_sick(bi->bi_owner, bi->bi_whichfork);
6332 		error = -EFSCORRUPTED;
6333 	}
6334 
6335 	return error;
6336 }
6337 
6338 /* Check that an extent does not have invalid flags or bad ranges. */
6339 xfs_failaddr_t
6340 xfs_bmap_validate_extent_raw(
6341 	struct xfs_mount	*mp,
6342 	bool			rtfile,
6343 	int			whichfork,
6344 	struct xfs_bmbt_irec	*irec)
6345 {
6346 	if (!xfs_verify_fileext(mp, irec->br_startoff, irec->br_blockcount))
6347 		return __this_address;
6348 
6349 	if (rtfile && whichfork == XFS_DATA_FORK) {
6350 		if (!xfs_verify_rtbext(mp, irec->br_startblock,
6351 					   irec->br_blockcount))
6352 			return __this_address;
6353 	} else {
6354 		if (!xfs_verify_fsbext(mp, irec->br_startblock,
6355 					   irec->br_blockcount))
6356 			return __this_address;
6357 	}
6358 	if (irec->br_state != XFS_EXT_NORM && whichfork != XFS_DATA_FORK)
6359 		return __this_address;
6360 	return NULL;
6361 }
6362 
6363 int __init
6364 xfs_bmap_intent_init_cache(void)
6365 {
6366 	xfs_bmap_intent_cache = kmem_cache_create("xfs_bmap_intent",
6367 			sizeof(struct xfs_bmap_intent),
6368 			0, 0, NULL);
6369 
6370 	return xfs_bmap_intent_cache != NULL ? 0 : -ENOMEM;
6371 }
6372 
6373 void
6374 xfs_bmap_intent_destroy_cache(void)
6375 {
6376 	kmem_cache_destroy(xfs_bmap_intent_cache);
6377 	xfs_bmap_intent_cache = NULL;
6378 }
6379 
6380 /* Check that an inode's extent does not have invalid flags or bad ranges. */
6381 xfs_failaddr_t
6382 xfs_bmap_validate_extent(
6383 	struct xfs_inode	*ip,
6384 	int			whichfork,
6385 	struct xfs_bmbt_irec	*irec)
6386 {
6387 	return xfs_bmap_validate_extent_raw(ip->i_mount,
6388 			XFS_IS_REALTIME_INODE(ip), whichfork, irec);
6389 }
6390 
6391 /*
6392  * Used in xfs_itruncate_extents().  This is the maximum number of extents
6393  * freed from a file in a single transaction.
6394  */
6395 #define	XFS_ITRUNC_MAX_EXTENTS	2
6396 
6397 /*
6398  * Unmap every extent in part of an inode's fork.  We don't do any higher level
6399  * invalidation work at all.
6400  */
6401 int
6402 xfs_bunmapi_range(
6403 	struct xfs_trans	**tpp,
6404 	struct xfs_inode	*ip,
6405 	uint32_t		flags,
6406 	xfs_fileoff_t		startoff,
6407 	xfs_fileoff_t		endoff)
6408 {
6409 	xfs_filblks_t		unmap_len = endoff - startoff + 1;
6410 	int			error = 0;
6411 
6412 	xfs_assert_ilocked(ip, XFS_ILOCK_EXCL);
6413 
6414 	while (unmap_len > 0) {
6415 		ASSERT((*tpp)->t_highest_agno == NULLAGNUMBER);
6416 		error = __xfs_bunmapi(*tpp, ip, startoff, &unmap_len, flags,
6417 				XFS_ITRUNC_MAX_EXTENTS);
6418 		if (error)
6419 			goto out;
6420 
6421 		/* free the just unmapped extents */
6422 		error = xfs_defer_finish(tpp);
6423 		if (error)
6424 			goto out;
6425 		cond_resched();
6426 	}
6427 out:
6428 	return error;
6429 }
6430 
6431 struct xfs_bmap_query_range {
6432 	xfs_bmap_query_range_fn	fn;
6433 	void			*priv;
6434 };
6435 
6436 /* Format btree record and pass to our callback. */
6437 STATIC int
6438 xfs_bmap_query_range_helper(
6439 	struct xfs_btree_cur		*cur,
6440 	const union xfs_btree_rec	*rec,
6441 	void				*priv)
6442 {
6443 	struct xfs_bmap_query_range	*query = priv;
6444 	struct xfs_bmbt_irec		irec;
6445 	xfs_failaddr_t			fa;
6446 
6447 	xfs_bmbt_disk_get_all(&rec->bmbt, &irec);
6448 	fa = xfs_bmap_validate_extent(cur->bc_ino.ip, cur->bc_ino.whichfork,
6449 			&irec);
6450 	if (fa) {
6451 		xfs_btree_mark_sick(cur);
6452 		return xfs_bmap_complain_bad_rec(cur->bc_ino.ip,
6453 				cur->bc_ino.whichfork, fa, &irec);
6454 	}
6455 
6456 	return query->fn(cur, &irec, query->priv);
6457 }
6458 
6459 /* Find all bmaps. */
6460 int
6461 xfs_bmap_query_all(
6462 	struct xfs_btree_cur		*cur,
6463 	xfs_bmap_query_range_fn		fn,
6464 	void				*priv)
6465 {
6466 	struct xfs_bmap_query_range	query = {
6467 		.priv			= priv,
6468 		.fn			= fn,
6469 	};
6470 
6471 	return xfs_btree_query_all(cur, xfs_bmap_query_range_helper, &query);
6472 }
6473 
6474 /* Helper function to extract extent size hint from inode */
6475 xfs_extlen_t
6476 xfs_get_extsz_hint(
6477 	struct xfs_inode	*ip)
6478 {
6479 	/*
6480 	 * No point in aligning allocations if we need to COW to actually
6481 	 * write to them.
6482 	 */
6483 	if (xfs_is_always_cow_inode(ip))
6484 		return 0;
6485 	if ((ip->i_diflags & XFS_DIFLAG_EXTSIZE) && ip->i_extsize)
6486 		return ip->i_extsize;
6487 	if (XFS_IS_REALTIME_INODE(ip) &&
6488 	    ip->i_mount->m_sb.sb_rextsize > 1)
6489 		return ip->i_mount->m_sb.sb_rextsize;
6490 	return 0;
6491 }
6492 
6493 /*
6494  * Helper function to extract CoW extent size hint from inode.
6495  * Between the extent size hint and the CoW extent size hint, we
6496  * return the greater of the two.  If the value is zero (automatic),
6497  * use the default size.
6498  */
6499 xfs_extlen_t
6500 xfs_get_cowextsz_hint(
6501 	struct xfs_inode	*ip)
6502 {
6503 	xfs_extlen_t		a, b;
6504 
6505 	a = 0;
6506 	if (ip->i_diflags2 & XFS_DIFLAG2_COWEXTSIZE)
6507 		a = ip->i_cowextsize;
6508 	b = xfs_get_extsz_hint(ip);
6509 
6510 	a = max(a, b);
6511 	if (a == 0)
6512 		return XFS_DEFAULT_COWEXTSZ_HINT;
6513 	return a;
6514 }
6515