xref: /linux/fs/xfs/libxfs/xfs_bmap.c (revision ba199dc909a20fe62270ae4e93f263987bb9d119)
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, 0) == 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 #define XFS_ALLOC_GAP_UNITS	4
3116 
3117 /* returns true if ap->blkno was modified */
3118 bool
3119 xfs_bmap_adjacent(
3120 	struct xfs_bmalloca	*ap)	/* bmap alloc argument struct */
3121 {
3122 	xfs_fsblock_t	adjust;		/* adjustment to block numbers */
3123 	xfs_mount_t	*mp;		/* mount point structure */
3124 	int		rt;		/* true if inode is realtime */
3125 
3126 #define	ISVALID(x,y)	\
3127 	(rt ? \
3128 		(x) < mp->m_sb.sb_rblocks : \
3129 		XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3130 		XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3131 		XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3132 
3133 	mp = ap->ip->i_mount;
3134 	rt = XFS_IS_REALTIME_INODE(ap->ip) &&
3135 		(ap->datatype & XFS_ALLOC_USERDATA);
3136 	/*
3137 	 * If allocating at eof, and there's a previous real block,
3138 	 * try to use its last block as our starting point.
3139 	 */
3140 	if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
3141 	    !isnullstartblock(ap->prev.br_startblock) &&
3142 	    ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount,
3143 		    ap->prev.br_startblock)) {
3144 		ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
3145 		/*
3146 		 * Adjust for the gap between prevp and us.
3147 		 */
3148 		adjust = ap->offset -
3149 			(ap->prev.br_startoff + ap->prev.br_blockcount);
3150 		if (adjust &&
3151 		    ISVALID(ap->blkno + adjust, ap->prev.br_startblock))
3152 			ap->blkno += adjust;
3153 		return true;
3154 	}
3155 	/*
3156 	 * If not at eof, then compare the two neighbor blocks.
3157 	 * Figure out whether either one gives us a good starting point,
3158 	 * and pick the better one.
3159 	 */
3160 	if (!ap->eof) {
3161 		xfs_fsblock_t	gotbno;		/* right side block number */
3162 		xfs_fsblock_t	gotdiff=0;	/* right side difference */
3163 		xfs_fsblock_t	prevbno;	/* left side block number */
3164 		xfs_fsblock_t	prevdiff=0;	/* left side difference */
3165 
3166 		/*
3167 		 * If there's a previous (left) block, select a requested
3168 		 * start block based on it.
3169 		 */
3170 		if (ap->prev.br_startoff != NULLFILEOFF &&
3171 		    !isnullstartblock(ap->prev.br_startblock) &&
3172 		    (prevbno = ap->prev.br_startblock +
3173 			       ap->prev.br_blockcount) &&
3174 		    ISVALID(prevbno, ap->prev.br_startblock)) {
3175 			/*
3176 			 * Calculate gap to end of previous block.
3177 			 */
3178 			adjust = prevdiff = ap->offset -
3179 				(ap->prev.br_startoff +
3180 				 ap->prev.br_blockcount);
3181 			/*
3182 			 * Figure the startblock based on the previous block's
3183 			 * end and the gap size.
3184 			 * Heuristic!
3185 			 * If the gap is large relative to the piece we're
3186 			 * allocating, or using it gives us an invalid block
3187 			 * number, then just use the end of the previous block.
3188 			 */
3189 			if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
3190 			    ISVALID(prevbno + prevdiff,
3191 				    ap->prev.br_startblock))
3192 				prevbno += adjust;
3193 			else
3194 				prevdiff += adjust;
3195 		}
3196 		/*
3197 		 * No previous block or can't follow it, just default.
3198 		 */
3199 		else
3200 			prevbno = NULLFSBLOCK;
3201 		/*
3202 		 * If there's a following (right) block, select a requested
3203 		 * start block based on it.
3204 		 */
3205 		if (!isnullstartblock(ap->got.br_startblock)) {
3206 			/*
3207 			 * Calculate gap to start of next block.
3208 			 */
3209 			adjust = gotdiff = ap->got.br_startoff - ap->offset;
3210 			/*
3211 			 * Figure the startblock based on the next block's
3212 			 * start and the gap size.
3213 			 */
3214 			gotbno = ap->got.br_startblock;
3215 			/*
3216 			 * Heuristic!
3217 			 * If the gap is large relative to the piece we're
3218 			 * allocating, or using it gives us an invalid block
3219 			 * number, then just use the start of the next block
3220 			 * offset by our length.
3221 			 */
3222 			if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
3223 			    ISVALID(gotbno - gotdiff, gotbno))
3224 				gotbno -= adjust;
3225 			else if (ISVALID(gotbno - ap->length, gotbno)) {
3226 				gotbno -= ap->length;
3227 				gotdiff += adjust - ap->length;
3228 			} else
3229 				gotdiff += adjust;
3230 		}
3231 		/*
3232 		 * No next block, just default.
3233 		 */
3234 		else
3235 			gotbno = NULLFSBLOCK;
3236 		/*
3237 		 * If both valid, pick the better one, else the only good
3238 		 * one, else ap->blkno is already set (to 0 or the inode block).
3239 		 */
3240 		if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK) {
3241 			ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
3242 			return true;
3243 		}
3244 		if (prevbno != NULLFSBLOCK) {
3245 			ap->blkno = prevbno;
3246 			return true;
3247 		}
3248 		if (gotbno != NULLFSBLOCK) {
3249 			ap->blkno = gotbno;
3250 			return true;
3251 		}
3252 	}
3253 #undef ISVALID
3254 	return false;
3255 }
3256 
3257 int
3258 xfs_bmap_longest_free_extent(
3259 	struct xfs_perag	*pag,
3260 	struct xfs_trans	*tp,
3261 	xfs_extlen_t		*blen)
3262 {
3263 	xfs_extlen_t		longest;
3264 	int			error = 0;
3265 
3266 	if (!xfs_perag_initialised_agf(pag)) {
3267 		error = xfs_alloc_read_agf(pag, tp, XFS_ALLOC_FLAG_TRYLOCK,
3268 				NULL);
3269 		if (error)
3270 			return error;
3271 	}
3272 
3273 	longest = xfs_alloc_longest_free_extent(pag,
3274 				xfs_alloc_min_freelist(pag->pag_mount, pag),
3275 				xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE));
3276 	if (*blen < longest)
3277 		*blen = longest;
3278 
3279 	return 0;
3280 }
3281 
3282 static xfs_extlen_t
3283 xfs_bmap_select_minlen(
3284 	struct xfs_bmalloca	*ap,
3285 	struct xfs_alloc_arg	*args,
3286 	xfs_extlen_t		blen)
3287 {
3288 
3289 	/*
3290 	 * Since we used XFS_ALLOC_FLAG_TRYLOCK in _longest_free_extent(), it is
3291 	 * possible that there is enough contiguous free space for this request.
3292 	 */
3293 	if (blen < ap->minlen)
3294 		return ap->minlen;
3295 
3296 	/*
3297 	 * If the best seen length is less than the request length,
3298 	 * use the best as the minimum, otherwise we've got the maxlen we
3299 	 * were asked for.
3300 	 */
3301 	if (blen < args->maxlen)
3302 		return blen;
3303 	return args->maxlen;
3304 }
3305 
3306 static int
3307 xfs_bmap_btalloc_select_lengths(
3308 	struct xfs_bmalloca	*ap,
3309 	struct xfs_alloc_arg	*args,
3310 	xfs_extlen_t		*blen)
3311 {
3312 	struct xfs_mount	*mp = args->mp;
3313 	struct xfs_perag	*pag;
3314 	xfs_agnumber_t		agno, startag;
3315 	int			error = 0;
3316 
3317 	if (ap->tp->t_flags & XFS_TRANS_LOWMODE) {
3318 		args->total = ap->minlen;
3319 		args->minlen = ap->minlen;
3320 		return 0;
3321 	}
3322 
3323 	args->total = ap->total;
3324 	startag = XFS_FSB_TO_AGNO(mp, ap->blkno);
3325 	if (startag == NULLAGNUMBER)
3326 		startag = 0;
3327 
3328 	*blen = 0;
3329 	for_each_perag_wrap(mp, startag, agno, pag) {
3330 		error = xfs_bmap_longest_free_extent(pag, args->tp, blen);
3331 		if (error && error != -EAGAIN)
3332 			break;
3333 		error = 0;
3334 		if (*blen >= args->maxlen)
3335 			break;
3336 	}
3337 	if (pag)
3338 		xfs_perag_rele(pag);
3339 
3340 	args->minlen = xfs_bmap_select_minlen(ap, args, *blen);
3341 	return error;
3342 }
3343 
3344 /* Update all inode and quota accounting for the allocation we just did. */
3345 void
3346 xfs_bmap_alloc_account(
3347 	struct xfs_bmalloca	*ap)
3348 {
3349 	bool			isrt = XFS_IS_REALTIME_INODE(ap->ip) &&
3350 					!(ap->flags & XFS_BMAPI_ATTRFORK);
3351 	uint			fld;
3352 
3353 	if (ap->flags & XFS_BMAPI_COWFORK) {
3354 		/*
3355 		 * COW fork blocks are in-core only and thus are treated as
3356 		 * in-core quota reservation (like delalloc blocks) even when
3357 		 * converted to real blocks. The quota reservation is not
3358 		 * accounted to disk until blocks are remapped to the data
3359 		 * fork. So if these blocks were previously delalloc, we
3360 		 * already have quota reservation and there's nothing to do
3361 		 * yet.
3362 		 */
3363 		if (ap->wasdel) {
3364 			xfs_mod_delalloc(ap->ip, -(int64_t)ap->length, 0);
3365 			return;
3366 		}
3367 
3368 		/*
3369 		 * Otherwise, we've allocated blocks in a hole. The transaction
3370 		 * has acquired in-core quota reservation for this extent.
3371 		 * Rather than account these as real blocks, however, we reduce
3372 		 * the transaction quota reservation based on the allocation.
3373 		 * This essentially transfers the transaction quota reservation
3374 		 * to that of a delalloc extent.
3375 		 */
3376 		ap->ip->i_delayed_blks += ap->length;
3377 		xfs_trans_mod_dquot_byino(ap->tp, ap->ip, isrt ?
3378 				XFS_TRANS_DQ_RES_RTBLKS : XFS_TRANS_DQ_RES_BLKS,
3379 				-(long)ap->length);
3380 		return;
3381 	}
3382 
3383 	/* data/attr fork only */
3384 	ap->ip->i_nblocks += ap->length;
3385 	xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
3386 	if (ap->wasdel) {
3387 		ap->ip->i_delayed_blks -= ap->length;
3388 		xfs_mod_delalloc(ap->ip, -(int64_t)ap->length, 0);
3389 		fld = isrt ? XFS_TRANS_DQ_DELRTBCOUNT : XFS_TRANS_DQ_DELBCOUNT;
3390 	} else {
3391 		fld = isrt ? XFS_TRANS_DQ_RTBCOUNT : XFS_TRANS_DQ_BCOUNT;
3392 	}
3393 
3394 	xfs_trans_mod_dquot_byino(ap->tp, ap->ip, fld, ap->length);
3395 }
3396 
3397 static int
3398 xfs_bmap_compute_alignments(
3399 	struct xfs_bmalloca	*ap,
3400 	struct xfs_alloc_arg	*args)
3401 {
3402 	struct xfs_mount	*mp = args->mp;
3403 	xfs_extlen_t		align = 0; /* minimum allocation alignment */
3404 	int			stripe_align = 0;
3405 
3406 	/* stripe alignment for allocation is determined by mount parameters */
3407 	if (mp->m_swidth && xfs_has_swalloc(mp))
3408 		stripe_align = mp->m_swidth;
3409 	else if (mp->m_dalign)
3410 		stripe_align = mp->m_dalign;
3411 
3412 	if (ap->flags & XFS_BMAPI_COWFORK)
3413 		align = xfs_get_cowextsz_hint(ap->ip);
3414 	else if (ap->datatype & XFS_ALLOC_USERDATA)
3415 		align = xfs_get_extsz_hint(ap->ip);
3416 	if (align) {
3417 		if (xfs_bmap_extsize_align(mp, &ap->got, &ap->prev, align, 0,
3418 					ap->eof, 0, ap->conv, &ap->offset,
3419 					&ap->length))
3420 			ASSERT(0);
3421 		ASSERT(ap->length);
3422 	}
3423 
3424 	/* apply extent size hints if obtained earlier */
3425 	if (align) {
3426 		args->prod = align;
3427 		div_u64_rem(ap->offset, args->prod, &args->mod);
3428 		if (args->mod)
3429 			args->mod = args->prod - args->mod;
3430 	} else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) {
3431 		args->prod = 1;
3432 		args->mod = 0;
3433 	} else {
3434 		args->prod = PAGE_SIZE >> mp->m_sb.sb_blocklog;
3435 		div_u64_rem(ap->offset, args->prod, &args->mod);
3436 		if (args->mod)
3437 			args->mod = args->prod - args->mod;
3438 	}
3439 
3440 	return stripe_align;
3441 }
3442 
3443 static void
3444 xfs_bmap_process_allocated_extent(
3445 	struct xfs_bmalloca	*ap,
3446 	struct xfs_alloc_arg	*args,
3447 	xfs_fileoff_t		orig_offset,
3448 	xfs_extlen_t		orig_length)
3449 {
3450 	ap->blkno = args->fsbno;
3451 	ap->length = args->len;
3452 	/*
3453 	 * If the extent size hint is active, we tried to round the
3454 	 * caller's allocation request offset down to extsz and the
3455 	 * length up to another extsz boundary.  If we found a free
3456 	 * extent we mapped it in starting at this new offset.  If the
3457 	 * newly mapped space isn't long enough to cover any of the
3458 	 * range of offsets that was originally requested, move the
3459 	 * mapping up so that we can fill as much of the caller's
3460 	 * original request as possible.  Free space is apparently
3461 	 * very fragmented so we're unlikely to be able to satisfy the
3462 	 * hints anyway.
3463 	 */
3464 	if (ap->length <= orig_length)
3465 		ap->offset = orig_offset;
3466 	else if (ap->offset + ap->length < orig_offset + orig_length)
3467 		ap->offset = orig_offset + orig_length - ap->length;
3468 	xfs_bmap_alloc_account(ap);
3469 }
3470 
3471 #ifdef DEBUG
3472 static int
3473 xfs_bmap_exact_minlen_extent_alloc(
3474 	struct xfs_bmalloca	*ap)
3475 {
3476 	struct xfs_mount	*mp = ap->ip->i_mount;
3477 	struct xfs_alloc_arg	args = { .tp = ap->tp, .mp = mp };
3478 	xfs_fileoff_t		orig_offset;
3479 	xfs_extlen_t		orig_length;
3480 	int			error;
3481 
3482 	ASSERT(ap->length);
3483 
3484 	if (ap->minlen != 1) {
3485 		ap->blkno = NULLFSBLOCK;
3486 		ap->length = 0;
3487 		return 0;
3488 	}
3489 
3490 	orig_offset = ap->offset;
3491 	orig_length = ap->length;
3492 
3493 	args.alloc_minlen_only = 1;
3494 
3495 	xfs_bmap_compute_alignments(ap, &args);
3496 
3497 	/*
3498 	 * Unlike the longest extent available in an AG, we don't track
3499 	 * the length of an AG's shortest extent.
3500 	 * XFS_ERRTAG_BMAP_ALLOC_MINLEN_EXTENT is a debug only knob and
3501 	 * hence we can afford to start traversing from the 0th AG since
3502 	 * we need not be concerned about a drop in performance in
3503 	 * "debug only" code paths.
3504 	 */
3505 	ap->blkno = XFS_AGB_TO_FSB(mp, 0, 0);
3506 
3507 	args.oinfo = XFS_RMAP_OINFO_SKIP_UPDATE;
3508 	args.minlen = args.maxlen = ap->minlen;
3509 	args.total = ap->total;
3510 
3511 	args.alignment = 1;
3512 	args.minalignslop = 0;
3513 
3514 	args.minleft = ap->minleft;
3515 	args.wasdel = ap->wasdel;
3516 	args.resv = XFS_AG_RESV_NONE;
3517 	args.datatype = ap->datatype;
3518 
3519 	error = xfs_alloc_vextent_first_ag(&args, ap->blkno);
3520 	if (error)
3521 		return error;
3522 
3523 	if (args.fsbno != NULLFSBLOCK) {
3524 		xfs_bmap_process_allocated_extent(ap, &args, orig_offset,
3525 			orig_length);
3526 	} else {
3527 		ap->blkno = NULLFSBLOCK;
3528 		ap->length = 0;
3529 	}
3530 
3531 	return 0;
3532 }
3533 #else
3534 
3535 #define xfs_bmap_exact_minlen_extent_alloc(bma) (-EFSCORRUPTED)
3536 
3537 #endif
3538 
3539 /*
3540  * If we are not low on available data blocks and we are allocating at
3541  * EOF, optimise allocation for contiguous file extension and/or stripe
3542  * alignment of the new extent.
3543  *
3544  * NOTE: ap->aeof is only set if the allocation length is >= the
3545  * stripe unit and the allocation offset is at the end of file.
3546  */
3547 static int
3548 xfs_bmap_btalloc_at_eof(
3549 	struct xfs_bmalloca	*ap,
3550 	struct xfs_alloc_arg	*args,
3551 	xfs_extlen_t		blen,
3552 	int			stripe_align,
3553 	bool			ag_only)
3554 {
3555 	struct xfs_mount	*mp = args->mp;
3556 	struct xfs_perag	*caller_pag = args->pag;
3557 	int			error;
3558 
3559 	/*
3560 	 * If there are already extents in the file, try an exact EOF block
3561 	 * allocation to extend the file as a contiguous extent. If that fails,
3562 	 * or it's the first allocation in a file, just try for a stripe aligned
3563 	 * allocation.
3564 	 */
3565 	if (ap->offset) {
3566 		xfs_extlen_t	nextminlen = 0;
3567 
3568 		/*
3569 		 * Compute the minlen+alignment for the next case.  Set slop so
3570 		 * that the value of minlen+alignment+slop doesn't go up between
3571 		 * the calls.
3572 		 */
3573 		args->alignment = 1;
3574 		if (blen > stripe_align && blen <= args->maxlen)
3575 			nextminlen = blen - stripe_align;
3576 		else
3577 			nextminlen = args->minlen;
3578 		if (nextminlen + stripe_align > args->minlen + 1)
3579 			args->minalignslop = nextminlen + stripe_align -
3580 					args->minlen - 1;
3581 		else
3582 			args->minalignslop = 0;
3583 
3584 		if (!caller_pag)
3585 			args->pag = xfs_perag_get(mp, XFS_FSB_TO_AGNO(mp, ap->blkno));
3586 		error = xfs_alloc_vextent_exact_bno(args, ap->blkno);
3587 		if (!caller_pag) {
3588 			xfs_perag_put(args->pag);
3589 			args->pag = NULL;
3590 		}
3591 		if (error)
3592 			return error;
3593 
3594 		if (args->fsbno != NULLFSBLOCK)
3595 			return 0;
3596 		/*
3597 		 * Exact allocation failed. Reset to try an aligned allocation
3598 		 * according to the original allocation specification.
3599 		 */
3600 		args->alignment = stripe_align;
3601 		args->minlen = nextminlen;
3602 		args->minalignslop = 0;
3603 	} else {
3604 		/*
3605 		 * Adjust minlen to try and preserve alignment if we
3606 		 * can't guarantee an aligned maxlen extent.
3607 		 */
3608 		args->alignment = stripe_align;
3609 		if (blen > args->alignment &&
3610 		    blen <= args->maxlen + args->alignment)
3611 			args->minlen = blen - args->alignment;
3612 		args->minalignslop = 0;
3613 	}
3614 
3615 	if (ag_only) {
3616 		error = xfs_alloc_vextent_near_bno(args, ap->blkno);
3617 	} else {
3618 		args->pag = NULL;
3619 		error = xfs_alloc_vextent_start_ag(args, ap->blkno);
3620 		ASSERT(args->pag == NULL);
3621 		args->pag = caller_pag;
3622 	}
3623 	if (error)
3624 		return error;
3625 
3626 	if (args->fsbno != NULLFSBLOCK)
3627 		return 0;
3628 
3629 	/*
3630 	 * Allocation failed, so turn return the allocation args to their
3631 	 * original non-aligned state so the caller can proceed on allocation
3632 	 * failure as if this function was never called.
3633 	 */
3634 	args->alignment = 1;
3635 	return 0;
3636 }
3637 
3638 /*
3639  * We have failed multiple allocation attempts so now are in a low space
3640  * allocation situation. Try a locality first full filesystem minimum length
3641  * allocation whilst still maintaining necessary total block reservation
3642  * requirements.
3643  *
3644  * If that fails, we are now critically low on space, so perform a last resort
3645  * allocation attempt: no reserve, no locality, blocking, minimum length, full
3646  * filesystem free space scan. We also indicate to future allocations in this
3647  * transaction that we are critically low on space so they don't waste time on
3648  * allocation modes that are unlikely to succeed.
3649  */
3650 int
3651 xfs_bmap_btalloc_low_space(
3652 	struct xfs_bmalloca	*ap,
3653 	struct xfs_alloc_arg	*args)
3654 {
3655 	int			error;
3656 
3657 	if (args->minlen > ap->minlen) {
3658 		args->minlen = ap->minlen;
3659 		error = xfs_alloc_vextent_start_ag(args, ap->blkno);
3660 		if (error || args->fsbno != NULLFSBLOCK)
3661 			return error;
3662 	}
3663 
3664 	/* Last ditch attempt before failure is declared. */
3665 	args->total = ap->minlen;
3666 	error = xfs_alloc_vextent_first_ag(args, 0);
3667 	if (error)
3668 		return error;
3669 	ap->tp->t_flags |= XFS_TRANS_LOWMODE;
3670 	return 0;
3671 }
3672 
3673 static int
3674 xfs_bmap_btalloc_filestreams(
3675 	struct xfs_bmalloca	*ap,
3676 	struct xfs_alloc_arg	*args,
3677 	int			stripe_align)
3678 {
3679 	xfs_extlen_t		blen = 0;
3680 	int			error = 0;
3681 
3682 
3683 	error = xfs_filestream_select_ag(ap, args, &blen);
3684 	if (error)
3685 		return error;
3686 	ASSERT(args->pag);
3687 
3688 	/*
3689 	 * If we are in low space mode, then optimal allocation will fail so
3690 	 * prepare for minimal allocation and jump to the low space algorithm
3691 	 * immediately.
3692 	 */
3693 	if (ap->tp->t_flags & XFS_TRANS_LOWMODE) {
3694 		args->minlen = ap->minlen;
3695 		ASSERT(args->fsbno == NULLFSBLOCK);
3696 		goto out_low_space;
3697 	}
3698 
3699 	args->minlen = xfs_bmap_select_minlen(ap, args, blen);
3700 	if (ap->aeof)
3701 		error = xfs_bmap_btalloc_at_eof(ap, args, blen, stripe_align,
3702 				true);
3703 
3704 	if (!error && args->fsbno == NULLFSBLOCK)
3705 		error = xfs_alloc_vextent_near_bno(args, ap->blkno);
3706 
3707 out_low_space:
3708 	/*
3709 	 * We are now done with the perag reference for the filestreams
3710 	 * association provided by xfs_filestream_select_ag(). Release it now as
3711 	 * we've either succeeded, had a fatal error or we are out of space and
3712 	 * need to do a full filesystem scan for free space which will take it's
3713 	 * own references.
3714 	 */
3715 	xfs_perag_rele(args->pag);
3716 	args->pag = NULL;
3717 	if (error || args->fsbno != NULLFSBLOCK)
3718 		return error;
3719 
3720 	return xfs_bmap_btalloc_low_space(ap, args);
3721 }
3722 
3723 static int
3724 xfs_bmap_btalloc_best_length(
3725 	struct xfs_bmalloca	*ap,
3726 	struct xfs_alloc_arg	*args,
3727 	int			stripe_align)
3728 {
3729 	xfs_extlen_t		blen = 0;
3730 	int			error;
3731 
3732 	ap->blkno = XFS_INO_TO_FSB(args->mp, ap->ip->i_ino);
3733 	xfs_bmap_adjacent(ap);
3734 
3735 	/*
3736 	 * Search for an allocation group with a single extent large enough for
3737 	 * the request.  If one isn't found, then adjust the minimum allocation
3738 	 * size to the largest space found.
3739 	 */
3740 	error = xfs_bmap_btalloc_select_lengths(ap, args, &blen);
3741 	if (error)
3742 		return error;
3743 
3744 	/*
3745 	 * Don't attempt optimal EOF allocation if previous allocations barely
3746 	 * succeeded due to being near ENOSPC. It is highly unlikely we'll get
3747 	 * optimal or even aligned allocations in this case, so don't waste time
3748 	 * trying.
3749 	 */
3750 	if (ap->aeof && !(ap->tp->t_flags & XFS_TRANS_LOWMODE)) {
3751 		error = xfs_bmap_btalloc_at_eof(ap, args, blen, stripe_align,
3752 				false);
3753 		if (error || args->fsbno != NULLFSBLOCK)
3754 			return error;
3755 	}
3756 
3757 	error = xfs_alloc_vextent_start_ag(args, ap->blkno);
3758 	if (error || args->fsbno != NULLFSBLOCK)
3759 		return error;
3760 
3761 	return xfs_bmap_btalloc_low_space(ap, args);
3762 }
3763 
3764 static int
3765 xfs_bmap_btalloc(
3766 	struct xfs_bmalloca	*ap)
3767 {
3768 	struct xfs_mount	*mp = ap->ip->i_mount;
3769 	struct xfs_alloc_arg	args = {
3770 		.tp		= ap->tp,
3771 		.mp		= mp,
3772 		.fsbno		= NULLFSBLOCK,
3773 		.oinfo		= XFS_RMAP_OINFO_SKIP_UPDATE,
3774 		.minleft	= ap->minleft,
3775 		.wasdel		= ap->wasdel,
3776 		.resv		= XFS_AG_RESV_NONE,
3777 		.datatype	= ap->datatype,
3778 		.alignment	= 1,
3779 		.minalignslop	= 0,
3780 	};
3781 	xfs_fileoff_t		orig_offset;
3782 	xfs_extlen_t		orig_length;
3783 	int			error;
3784 	int			stripe_align;
3785 
3786 	ASSERT(ap->length);
3787 	orig_offset = ap->offset;
3788 	orig_length = ap->length;
3789 
3790 	stripe_align = xfs_bmap_compute_alignments(ap, &args);
3791 
3792 	/* Trim the allocation back to the maximum an AG can fit. */
3793 	args.maxlen = min(ap->length, mp->m_ag_max_usable);
3794 
3795 	if ((ap->datatype & XFS_ALLOC_USERDATA) &&
3796 	    xfs_inode_is_filestream(ap->ip))
3797 		error = xfs_bmap_btalloc_filestreams(ap, &args, stripe_align);
3798 	else
3799 		error = xfs_bmap_btalloc_best_length(ap, &args, stripe_align);
3800 	if (error)
3801 		return error;
3802 
3803 	if (args.fsbno != NULLFSBLOCK) {
3804 		xfs_bmap_process_allocated_extent(ap, &args, orig_offset,
3805 			orig_length);
3806 	} else {
3807 		ap->blkno = NULLFSBLOCK;
3808 		ap->length = 0;
3809 	}
3810 	return 0;
3811 }
3812 
3813 /* Trim extent to fit a logical block range. */
3814 void
3815 xfs_trim_extent(
3816 	struct xfs_bmbt_irec	*irec,
3817 	xfs_fileoff_t		bno,
3818 	xfs_filblks_t		len)
3819 {
3820 	xfs_fileoff_t		distance;
3821 	xfs_fileoff_t		end = bno + len;
3822 
3823 	if (irec->br_startoff + irec->br_blockcount <= bno ||
3824 	    irec->br_startoff >= end) {
3825 		irec->br_blockcount = 0;
3826 		return;
3827 	}
3828 
3829 	if (irec->br_startoff < bno) {
3830 		distance = bno - irec->br_startoff;
3831 		if (isnullstartblock(irec->br_startblock))
3832 			irec->br_startblock = DELAYSTARTBLOCK;
3833 		if (irec->br_startblock != DELAYSTARTBLOCK &&
3834 		    irec->br_startblock != HOLESTARTBLOCK)
3835 			irec->br_startblock += distance;
3836 		irec->br_startoff += distance;
3837 		irec->br_blockcount -= distance;
3838 	}
3839 
3840 	if (end < irec->br_startoff + irec->br_blockcount) {
3841 		distance = irec->br_startoff + irec->br_blockcount - end;
3842 		irec->br_blockcount -= distance;
3843 	}
3844 }
3845 
3846 /*
3847  * Trim the returned map to the required bounds
3848  */
3849 STATIC void
3850 xfs_bmapi_trim_map(
3851 	struct xfs_bmbt_irec	*mval,
3852 	struct xfs_bmbt_irec	*got,
3853 	xfs_fileoff_t		*bno,
3854 	xfs_filblks_t		len,
3855 	xfs_fileoff_t		obno,
3856 	xfs_fileoff_t		end,
3857 	int			n,
3858 	uint32_t		flags)
3859 {
3860 	if ((flags & XFS_BMAPI_ENTIRE) ||
3861 	    got->br_startoff + got->br_blockcount <= obno) {
3862 		*mval = *got;
3863 		if (isnullstartblock(got->br_startblock))
3864 			mval->br_startblock = DELAYSTARTBLOCK;
3865 		return;
3866 	}
3867 
3868 	if (obno > *bno)
3869 		*bno = obno;
3870 	ASSERT((*bno >= obno) || (n == 0));
3871 	ASSERT(*bno < end);
3872 	mval->br_startoff = *bno;
3873 	if (isnullstartblock(got->br_startblock))
3874 		mval->br_startblock = DELAYSTARTBLOCK;
3875 	else
3876 		mval->br_startblock = got->br_startblock +
3877 					(*bno - got->br_startoff);
3878 	/*
3879 	 * Return the minimum of what we got and what we asked for for
3880 	 * the length.  We can use the len variable here because it is
3881 	 * modified below and we could have been there before coming
3882 	 * here if the first part of the allocation didn't overlap what
3883 	 * was asked for.
3884 	 */
3885 	mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
3886 			got->br_blockcount - (*bno - got->br_startoff));
3887 	mval->br_state = got->br_state;
3888 	ASSERT(mval->br_blockcount <= len);
3889 	return;
3890 }
3891 
3892 /*
3893  * Update and validate the extent map to return
3894  */
3895 STATIC void
3896 xfs_bmapi_update_map(
3897 	struct xfs_bmbt_irec	**map,
3898 	xfs_fileoff_t		*bno,
3899 	xfs_filblks_t		*len,
3900 	xfs_fileoff_t		obno,
3901 	xfs_fileoff_t		end,
3902 	int			*n,
3903 	uint32_t		flags)
3904 {
3905 	xfs_bmbt_irec_t	*mval = *map;
3906 
3907 	ASSERT((flags & XFS_BMAPI_ENTIRE) ||
3908 	       ((mval->br_startoff + mval->br_blockcount) <= end));
3909 	ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
3910 	       (mval->br_startoff < obno));
3911 
3912 	*bno = mval->br_startoff + mval->br_blockcount;
3913 	*len = end - *bno;
3914 	if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
3915 		/* update previous map with new information */
3916 		ASSERT(mval->br_startblock == mval[-1].br_startblock);
3917 		ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
3918 		ASSERT(mval->br_state == mval[-1].br_state);
3919 		mval[-1].br_blockcount = mval->br_blockcount;
3920 		mval[-1].br_state = mval->br_state;
3921 	} else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
3922 		   mval[-1].br_startblock != DELAYSTARTBLOCK &&
3923 		   mval[-1].br_startblock != HOLESTARTBLOCK &&
3924 		   mval->br_startblock == mval[-1].br_startblock +
3925 					  mval[-1].br_blockcount &&
3926 		   mval[-1].br_state == mval->br_state) {
3927 		ASSERT(mval->br_startoff ==
3928 		       mval[-1].br_startoff + mval[-1].br_blockcount);
3929 		mval[-1].br_blockcount += mval->br_blockcount;
3930 	} else if (*n > 0 &&
3931 		   mval->br_startblock == DELAYSTARTBLOCK &&
3932 		   mval[-1].br_startblock == DELAYSTARTBLOCK &&
3933 		   mval->br_startoff ==
3934 		   mval[-1].br_startoff + mval[-1].br_blockcount) {
3935 		mval[-1].br_blockcount += mval->br_blockcount;
3936 		mval[-1].br_state = mval->br_state;
3937 	} else if (!((*n == 0) &&
3938 		     ((mval->br_startoff + mval->br_blockcount) <=
3939 		      obno))) {
3940 		mval++;
3941 		(*n)++;
3942 	}
3943 	*map = mval;
3944 }
3945 
3946 /*
3947  * Map file blocks to filesystem blocks without allocation.
3948  */
3949 int
3950 xfs_bmapi_read(
3951 	struct xfs_inode	*ip,
3952 	xfs_fileoff_t		bno,
3953 	xfs_filblks_t		len,
3954 	struct xfs_bmbt_irec	*mval,
3955 	int			*nmap,
3956 	uint32_t		flags)
3957 {
3958 	struct xfs_mount	*mp = ip->i_mount;
3959 	int			whichfork = xfs_bmapi_whichfork(flags);
3960 	struct xfs_ifork	*ifp = xfs_ifork_ptr(ip, whichfork);
3961 	struct xfs_bmbt_irec	got;
3962 	xfs_fileoff_t		obno;
3963 	xfs_fileoff_t		end;
3964 	struct xfs_iext_cursor	icur;
3965 	int			error;
3966 	bool			eof = false;
3967 	int			n = 0;
3968 
3969 	ASSERT(*nmap >= 1);
3970 	ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK | XFS_BMAPI_ENTIRE)));
3971 	xfs_assert_ilocked(ip, XFS_ILOCK_SHARED | XFS_ILOCK_EXCL);
3972 
3973 	if (WARN_ON_ONCE(!ifp)) {
3974 		xfs_bmap_mark_sick(ip, whichfork);
3975 		return -EFSCORRUPTED;
3976 	}
3977 
3978 	if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
3979 	    XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
3980 		xfs_bmap_mark_sick(ip, whichfork);
3981 		return -EFSCORRUPTED;
3982 	}
3983 
3984 	if (xfs_is_shutdown(mp))
3985 		return -EIO;
3986 
3987 	XFS_STATS_INC(mp, xs_blk_mapr);
3988 
3989 	error = xfs_iread_extents(NULL, ip, whichfork);
3990 	if (error)
3991 		return error;
3992 
3993 	if (!xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got))
3994 		eof = true;
3995 	end = bno + len;
3996 	obno = bno;
3997 
3998 	while (bno < end && n < *nmap) {
3999 		/* Reading past eof, act as though there's a hole up to end. */
4000 		if (eof)
4001 			got.br_startoff = end;
4002 		if (got.br_startoff > bno) {
4003 			/* Reading in a hole.  */
4004 			mval->br_startoff = bno;
4005 			mval->br_startblock = HOLESTARTBLOCK;
4006 			mval->br_blockcount =
4007 				XFS_FILBLKS_MIN(len, got.br_startoff - bno);
4008 			mval->br_state = XFS_EXT_NORM;
4009 			bno += mval->br_blockcount;
4010 			len -= mval->br_blockcount;
4011 			mval++;
4012 			n++;
4013 			continue;
4014 		}
4015 
4016 		/* set up the extent map to return. */
4017 		xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
4018 		xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4019 
4020 		/* If we're done, stop now. */
4021 		if (bno >= end || n >= *nmap)
4022 			break;
4023 
4024 		/* Else go on to the next record. */
4025 		if (!xfs_iext_next_extent(ifp, &icur, &got))
4026 			eof = true;
4027 	}
4028 	*nmap = n;
4029 	return 0;
4030 }
4031 
4032 /*
4033  * Add a delayed allocation extent to an inode. Blocks are reserved from the
4034  * global pool and the extent inserted into the inode in-core extent tree.
4035  *
4036  * On entry, got refers to the first extent beyond the offset of the extent to
4037  * allocate or eof is specified if no such extent exists. On return, got refers
4038  * to the extent record that was inserted to the inode fork.
4039  *
4040  * Note that the allocated extent may have been merged with contiguous extents
4041  * during insertion into the inode fork. Thus, got does not reflect the current
4042  * state of the inode fork on return. If necessary, the caller can use lastx to
4043  * look up the updated record in the inode fork.
4044  */
4045 int
4046 xfs_bmapi_reserve_delalloc(
4047 	struct xfs_inode	*ip,
4048 	int			whichfork,
4049 	xfs_fileoff_t		off,
4050 	xfs_filblks_t		len,
4051 	xfs_filblks_t		prealloc,
4052 	struct xfs_bmbt_irec	*got,
4053 	struct xfs_iext_cursor	*icur,
4054 	int			eof)
4055 {
4056 	struct xfs_mount	*mp = ip->i_mount;
4057 	struct xfs_ifork	*ifp = xfs_ifork_ptr(ip, whichfork);
4058 	xfs_extlen_t		alen;
4059 	xfs_extlen_t		indlen;
4060 	uint64_t		fdblocks;
4061 	int			error;
4062 	xfs_fileoff_t		aoff;
4063 	bool			use_cowextszhint =
4064 					whichfork == XFS_COW_FORK && !prealloc;
4065 
4066 retry:
4067 	/*
4068 	 * Cap the alloc length. Keep track of prealloc so we know whether to
4069 	 * tag the inode before we return.
4070 	 */
4071 	aoff = off;
4072 	alen = XFS_FILBLKS_MIN(len + prealloc, XFS_MAX_BMBT_EXTLEN);
4073 	if (!eof)
4074 		alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
4075 	if (prealloc && alen >= len)
4076 		prealloc = alen - len;
4077 
4078 	/*
4079 	 * If we're targetting the COW fork but aren't creating a speculative
4080 	 * posteof preallocation, try to expand the reservation to align with
4081 	 * the COW extent size hint if there's sufficient free space.
4082 	 *
4083 	 * Unlike the data fork, the CoW cancellation functions will free all
4084 	 * the reservations at inactivation, so we don't require that every
4085 	 * delalloc reservation have a dirty pagecache.
4086 	 */
4087 	if (use_cowextszhint) {
4088 		struct xfs_bmbt_irec	prev;
4089 		xfs_extlen_t		extsz = xfs_get_cowextsz_hint(ip);
4090 
4091 		if (!xfs_iext_peek_prev_extent(ifp, icur, &prev))
4092 			prev.br_startoff = NULLFILEOFF;
4093 
4094 		error = xfs_bmap_extsize_align(mp, got, &prev, extsz, 0, eof,
4095 					       1, 0, &aoff, &alen);
4096 		ASSERT(!error);
4097 	}
4098 
4099 	/*
4100 	 * Make a transaction-less quota reservation for delayed allocation
4101 	 * blocks.  This number gets adjusted later.  We return if we haven't
4102 	 * allocated blocks already inside this loop.
4103 	 */
4104 	error = xfs_quota_reserve_blkres(ip, alen);
4105 	if (error)
4106 		goto out;
4107 
4108 	/*
4109 	 * Split changing sb for alen and indlen since they could be coming
4110 	 * from different places.
4111 	 */
4112 	indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
4113 	ASSERT(indlen > 0);
4114 
4115 	fdblocks = indlen;
4116 	if (XFS_IS_REALTIME_INODE(ip)) {
4117 		error = xfs_dec_frextents(mp, xfs_rtb_to_rtx(mp, alen));
4118 		if (error)
4119 			goto out_unreserve_quota;
4120 	} else {
4121 		fdblocks += alen;
4122 	}
4123 
4124 	error = xfs_dec_fdblocks(mp, fdblocks, false);
4125 	if (error)
4126 		goto out_unreserve_frextents;
4127 
4128 	ip->i_delayed_blks += alen;
4129 	xfs_mod_delalloc(ip, alen, indlen);
4130 
4131 	got->br_startoff = aoff;
4132 	got->br_startblock = nullstartblock(indlen);
4133 	got->br_blockcount = alen;
4134 	got->br_state = XFS_EXT_NORM;
4135 
4136 	xfs_bmap_add_extent_hole_delay(ip, whichfork, icur, got);
4137 
4138 	/*
4139 	 * Tag the inode if blocks were preallocated. Note that COW fork
4140 	 * preallocation can occur at the start or end of the extent, even when
4141 	 * prealloc == 0, so we must also check the aligned offset and length.
4142 	 */
4143 	if (whichfork == XFS_DATA_FORK && prealloc)
4144 		xfs_inode_set_eofblocks_tag(ip);
4145 	if (whichfork == XFS_COW_FORK && (prealloc || aoff < off || alen > len))
4146 		xfs_inode_set_cowblocks_tag(ip);
4147 
4148 	return 0;
4149 
4150 out_unreserve_frextents:
4151 	if (XFS_IS_REALTIME_INODE(ip))
4152 		xfs_add_frextents(mp, xfs_rtb_to_rtx(mp, alen));
4153 out_unreserve_quota:
4154 	if (XFS_IS_QUOTA_ON(mp))
4155 		xfs_quota_unreserve_blkres(ip, alen);
4156 out:
4157 	if (error == -ENOSPC || error == -EDQUOT) {
4158 		trace_xfs_delalloc_enospc(ip, off, len);
4159 
4160 		if (prealloc || use_cowextszhint) {
4161 			/* retry without any preallocation */
4162 			use_cowextszhint = false;
4163 			prealloc = 0;
4164 			goto retry;
4165 		}
4166 	}
4167 	return error;
4168 }
4169 
4170 static int
4171 xfs_bmap_alloc_userdata(
4172 	struct xfs_bmalloca	*bma)
4173 {
4174 	struct xfs_mount	*mp = bma->ip->i_mount;
4175 	int			whichfork = xfs_bmapi_whichfork(bma->flags);
4176 	int			error;
4177 
4178 	/*
4179 	 * Set the data type being allocated. For the data fork, the first data
4180 	 * in the file is treated differently to all other allocations. For the
4181 	 * attribute fork, we only need to ensure the allocated range is not on
4182 	 * the busy list.
4183 	 */
4184 	bma->datatype = XFS_ALLOC_NOBUSY;
4185 	if (whichfork == XFS_DATA_FORK || whichfork == XFS_COW_FORK) {
4186 		bma->datatype |= XFS_ALLOC_USERDATA;
4187 		if (bma->offset == 0)
4188 			bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA;
4189 
4190 		if (mp->m_dalign && bma->length >= mp->m_dalign) {
4191 			error = xfs_bmap_isaeof(bma, whichfork);
4192 			if (error)
4193 				return error;
4194 		}
4195 
4196 		if (XFS_IS_REALTIME_INODE(bma->ip))
4197 			return xfs_bmap_rtalloc(bma);
4198 	}
4199 
4200 	if (unlikely(XFS_TEST_ERROR(false, mp,
4201 			XFS_ERRTAG_BMAP_ALLOC_MINLEN_EXTENT)))
4202 		return xfs_bmap_exact_minlen_extent_alloc(bma);
4203 
4204 	return xfs_bmap_btalloc(bma);
4205 }
4206 
4207 static int
4208 xfs_bmapi_allocate(
4209 	struct xfs_bmalloca	*bma)
4210 {
4211 	struct xfs_mount	*mp = bma->ip->i_mount;
4212 	int			whichfork = xfs_bmapi_whichfork(bma->flags);
4213 	struct xfs_ifork	*ifp = xfs_ifork_ptr(bma->ip, whichfork);
4214 	int			error;
4215 
4216 	ASSERT(bma->length > 0);
4217 	ASSERT(bma->length <= XFS_MAX_BMBT_EXTLEN);
4218 
4219 	if (bma->flags & XFS_BMAPI_CONTIG)
4220 		bma->minlen = bma->length;
4221 	else
4222 		bma->minlen = 1;
4223 
4224 	if (bma->flags & XFS_BMAPI_METADATA) {
4225 		if (unlikely(XFS_TEST_ERROR(false, mp,
4226 				XFS_ERRTAG_BMAP_ALLOC_MINLEN_EXTENT)))
4227 			error = xfs_bmap_exact_minlen_extent_alloc(bma);
4228 		else
4229 			error = xfs_bmap_btalloc(bma);
4230 	} else {
4231 		error = xfs_bmap_alloc_userdata(bma);
4232 	}
4233 	if (error)
4234 		return error;
4235 	if (bma->blkno == NULLFSBLOCK)
4236 		return -ENOSPC;
4237 
4238 	if (WARN_ON_ONCE(!xfs_valid_startblock(bma->ip, bma->blkno))) {
4239 		xfs_bmap_mark_sick(bma->ip, whichfork);
4240 		return -EFSCORRUPTED;
4241 	}
4242 
4243 	if (bma->flags & XFS_BMAPI_ZERO) {
4244 		error = xfs_zero_extent(bma->ip, bma->blkno, bma->length);
4245 		if (error)
4246 			return error;
4247 	}
4248 
4249 	if (ifp->if_format == XFS_DINODE_FMT_BTREE && !bma->cur)
4250 		bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
4251 	/*
4252 	 * Bump the number of extents we've allocated
4253 	 * in this call.
4254 	 */
4255 	bma->nallocs++;
4256 
4257 	if (bma->cur && bma->wasdel)
4258 		bma->cur->bc_flags |= XFS_BTREE_BMBT_WASDEL;
4259 
4260 	bma->got.br_startoff = bma->offset;
4261 	bma->got.br_startblock = bma->blkno;
4262 	bma->got.br_blockcount = bma->length;
4263 	bma->got.br_state = XFS_EXT_NORM;
4264 
4265 	if (bma->flags & XFS_BMAPI_PREALLOC)
4266 		bma->got.br_state = XFS_EXT_UNWRITTEN;
4267 
4268 	if (bma->wasdel)
4269 		error = xfs_bmap_add_extent_delay_real(bma, whichfork);
4270 	else
4271 		error = xfs_bmap_add_extent_hole_real(bma->tp, bma->ip,
4272 				whichfork, &bma->icur, &bma->cur, &bma->got,
4273 				&bma->logflags, bma->flags);
4274 	if (error)
4275 		return error;
4276 
4277 	/*
4278 	 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4279 	 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4280 	 * the neighbouring ones.
4281 	 */
4282 	xfs_iext_get_extent(ifp, &bma->icur, &bma->got);
4283 
4284 	ASSERT(bma->got.br_startoff <= bma->offset);
4285 	ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
4286 	       bma->offset + bma->length);
4287 	ASSERT(bma->got.br_state == XFS_EXT_NORM ||
4288 	       bma->got.br_state == XFS_EXT_UNWRITTEN);
4289 	return 0;
4290 }
4291 
4292 STATIC int
4293 xfs_bmapi_convert_unwritten(
4294 	struct xfs_bmalloca	*bma,
4295 	struct xfs_bmbt_irec	*mval,
4296 	xfs_filblks_t		len,
4297 	uint32_t		flags)
4298 {
4299 	int			whichfork = xfs_bmapi_whichfork(flags);
4300 	struct xfs_ifork	*ifp = xfs_ifork_ptr(bma->ip, whichfork);
4301 	int			tmp_logflags = 0;
4302 	int			error;
4303 
4304 	/* check if we need to do unwritten->real conversion */
4305 	if (mval->br_state == XFS_EXT_UNWRITTEN &&
4306 	    (flags & XFS_BMAPI_PREALLOC))
4307 		return 0;
4308 
4309 	/* check if we need to do real->unwritten conversion */
4310 	if (mval->br_state == XFS_EXT_NORM &&
4311 	    (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
4312 			(XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
4313 		return 0;
4314 
4315 	/*
4316 	 * Modify (by adding) the state flag, if writing.
4317 	 */
4318 	ASSERT(mval->br_blockcount <= len);
4319 	if (ifp->if_format == XFS_DINODE_FMT_BTREE && !bma->cur) {
4320 		bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
4321 					bma->ip, whichfork);
4322 	}
4323 	mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4324 				? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
4325 
4326 	/*
4327 	 * Before insertion into the bmbt, zero the range being converted
4328 	 * if required.
4329 	 */
4330 	if (flags & XFS_BMAPI_ZERO) {
4331 		error = xfs_zero_extent(bma->ip, mval->br_startblock,
4332 					mval->br_blockcount);
4333 		if (error)
4334 			return error;
4335 	}
4336 
4337 	error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, whichfork,
4338 			&bma->icur, &bma->cur, mval, &tmp_logflags);
4339 	/*
4340 	 * Log the inode core unconditionally in the unwritten extent conversion
4341 	 * path because the conversion might not have done so (e.g., if the
4342 	 * extent count hasn't changed). We need to make sure the inode is dirty
4343 	 * in the transaction for the sake of fsync(), even if nothing has
4344 	 * changed, because fsync() will not force the log for this transaction
4345 	 * unless it sees the inode pinned.
4346 	 *
4347 	 * Note: If we're only converting cow fork extents, there aren't
4348 	 * any on-disk updates to make, so we don't need to log anything.
4349 	 */
4350 	if (whichfork != XFS_COW_FORK)
4351 		bma->logflags |= tmp_logflags | XFS_ILOG_CORE;
4352 	if (error)
4353 		return error;
4354 
4355 	/*
4356 	 * Update our extent pointer, given that
4357 	 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4358 	 * of the neighbouring ones.
4359 	 */
4360 	xfs_iext_get_extent(ifp, &bma->icur, &bma->got);
4361 
4362 	/*
4363 	 * We may have combined previously unwritten space with written space,
4364 	 * so generate another request.
4365 	 */
4366 	if (mval->br_blockcount < len)
4367 		return -EAGAIN;
4368 	return 0;
4369 }
4370 
4371 xfs_extlen_t
4372 xfs_bmapi_minleft(
4373 	struct xfs_trans	*tp,
4374 	struct xfs_inode	*ip,
4375 	int			fork)
4376 {
4377 	struct xfs_ifork	*ifp = xfs_ifork_ptr(ip, fork);
4378 
4379 	if (tp && tp->t_highest_agno != NULLAGNUMBER)
4380 		return 0;
4381 	if (ifp->if_format != XFS_DINODE_FMT_BTREE)
4382 		return 1;
4383 	return be16_to_cpu(ifp->if_broot->bb_level) + 1;
4384 }
4385 
4386 /*
4387  * Log whatever the flags say, even if error.  Otherwise we might miss detecting
4388  * a case where the data is changed, there's an error, and it's not logged so we
4389  * don't shutdown when we should.  Don't bother logging extents/btree changes if
4390  * we converted to the other format.
4391  */
4392 static void
4393 xfs_bmapi_finish(
4394 	struct xfs_bmalloca	*bma,
4395 	int			whichfork,
4396 	int			error)
4397 {
4398 	struct xfs_ifork	*ifp = xfs_ifork_ptr(bma->ip, whichfork);
4399 
4400 	if ((bma->logflags & xfs_ilog_fext(whichfork)) &&
4401 	    ifp->if_format != XFS_DINODE_FMT_EXTENTS)
4402 		bma->logflags &= ~xfs_ilog_fext(whichfork);
4403 	else if ((bma->logflags & xfs_ilog_fbroot(whichfork)) &&
4404 		 ifp->if_format != XFS_DINODE_FMT_BTREE)
4405 		bma->logflags &= ~xfs_ilog_fbroot(whichfork);
4406 
4407 	if (bma->logflags)
4408 		xfs_trans_log_inode(bma->tp, bma->ip, bma->logflags);
4409 	if (bma->cur)
4410 		xfs_btree_del_cursor(bma->cur, error);
4411 }
4412 
4413 /*
4414  * Map file blocks to filesystem blocks, and allocate blocks or convert the
4415  * extent state if necessary.  Details behaviour is controlled by the flags
4416  * parameter.  Only allocates blocks from a single allocation group, to avoid
4417  * locking problems.
4418  *
4419  * Returns 0 on success and places the extent mappings in mval.  nmaps is used
4420  * as an input/output parameter where the caller specifies the maximum number
4421  * of mappings that may be returned and xfs_bmapi_write passes back the number
4422  * of mappings (including existing mappings) it found.
4423  *
4424  * Returns a negative error code on failure, including -ENOSPC when it could not
4425  * allocate any blocks and -ENOSR when it did allocate blocks to convert a
4426  * delalloc range, but those blocks were before the passed in range.
4427  */
4428 int
4429 xfs_bmapi_write(
4430 	struct xfs_trans	*tp,		/* transaction pointer */
4431 	struct xfs_inode	*ip,		/* incore inode */
4432 	xfs_fileoff_t		bno,		/* starting file offs. mapped */
4433 	xfs_filblks_t		len,		/* length to map in file */
4434 	uint32_t		flags,		/* XFS_BMAPI_... */
4435 	xfs_extlen_t		total,		/* total blocks needed */
4436 	struct xfs_bmbt_irec	*mval,		/* output: map values */
4437 	int			*nmap)		/* i/o: mval size/count */
4438 {
4439 	struct xfs_bmalloca	bma = {
4440 		.tp		= tp,
4441 		.ip		= ip,
4442 		.total		= total,
4443 	};
4444 	struct xfs_mount	*mp = ip->i_mount;
4445 	int			whichfork = xfs_bmapi_whichfork(flags);
4446 	struct xfs_ifork	*ifp = xfs_ifork_ptr(ip, whichfork);
4447 	xfs_fileoff_t		end;		/* end of mapped file region */
4448 	bool			eof = false;	/* after the end of extents */
4449 	int			error;		/* error return */
4450 	int			n;		/* current extent index */
4451 	xfs_fileoff_t		obno;		/* old block number (offset) */
4452 
4453 #ifdef DEBUG
4454 	xfs_fileoff_t		orig_bno;	/* original block number value */
4455 	int			orig_flags;	/* original flags arg value */
4456 	xfs_filblks_t		orig_len;	/* original value of len arg */
4457 	struct xfs_bmbt_irec	*orig_mval;	/* original value of mval */
4458 	int			orig_nmap;	/* original value of *nmap */
4459 
4460 	orig_bno = bno;
4461 	orig_len = len;
4462 	orig_flags = flags;
4463 	orig_mval = mval;
4464 	orig_nmap = *nmap;
4465 #endif
4466 
4467 	ASSERT(*nmap >= 1);
4468 	ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4469 	ASSERT(tp != NULL);
4470 	ASSERT(len > 0);
4471 	ASSERT(ifp->if_format != XFS_DINODE_FMT_LOCAL);
4472 	xfs_assert_ilocked(ip, XFS_ILOCK_EXCL);
4473 	ASSERT(!(flags & XFS_BMAPI_REMAP));
4474 
4475 	/* zeroing is for currently only for data extents, not metadata */
4476 	ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) !=
4477 			(XFS_BMAPI_METADATA | XFS_BMAPI_ZERO));
4478 	/*
4479 	 * we can allocate unwritten extents or pre-zero allocated blocks,
4480 	 * but it makes no sense to do both at once. This would result in
4481 	 * zeroing the unwritten extent twice, but it still being an
4482 	 * unwritten extent....
4483 	 */
4484 	ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) !=
4485 			(XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO));
4486 
4487 	if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
4488 	    XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
4489 		xfs_bmap_mark_sick(ip, whichfork);
4490 		return -EFSCORRUPTED;
4491 	}
4492 
4493 	if (xfs_is_shutdown(mp))
4494 		return -EIO;
4495 
4496 	XFS_STATS_INC(mp, xs_blk_mapw);
4497 
4498 	error = xfs_iread_extents(tp, ip, whichfork);
4499 	if (error)
4500 		goto error0;
4501 
4502 	if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.icur, &bma.got))
4503 		eof = true;
4504 	if (!xfs_iext_peek_prev_extent(ifp, &bma.icur, &bma.prev))
4505 		bma.prev.br_startoff = NULLFILEOFF;
4506 	bma.minleft = xfs_bmapi_minleft(tp, ip, whichfork);
4507 
4508 	n = 0;
4509 	end = bno + len;
4510 	obno = bno;
4511 	while (bno < end && n < *nmap) {
4512 		bool			need_alloc = false, wasdelay = false;
4513 
4514 		/* in hole or beyond EOF? */
4515 		if (eof || bma.got.br_startoff > bno) {
4516 			/*
4517 			 * CoW fork conversions should /never/ hit EOF or
4518 			 * holes.  There should always be something for us
4519 			 * to work on.
4520 			 */
4521 			ASSERT(!((flags & XFS_BMAPI_CONVERT) &&
4522 			         (flags & XFS_BMAPI_COWFORK)));
4523 
4524 			need_alloc = true;
4525 		} else if (isnullstartblock(bma.got.br_startblock)) {
4526 			wasdelay = true;
4527 		}
4528 
4529 		/*
4530 		 * First, deal with the hole before the allocated space
4531 		 * that we found, if any.
4532 		 */
4533 		if (need_alloc || wasdelay) {
4534 			bma.eof = eof;
4535 			bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4536 			bma.wasdel = wasdelay;
4537 			bma.offset = bno;
4538 			bma.flags = flags;
4539 
4540 			/*
4541 			 * There's a 32/64 bit type mismatch between the
4542 			 * allocation length request (which can be 64 bits in
4543 			 * length) and the bma length request, which is
4544 			 * xfs_extlen_t and therefore 32 bits. Hence we have to
4545 			 * be careful and do the min() using the larger type to
4546 			 * avoid overflows.
4547 			 */
4548 			bma.length = XFS_FILBLKS_MIN(len, XFS_MAX_BMBT_EXTLEN);
4549 
4550 			if (wasdelay) {
4551 				bma.length = XFS_FILBLKS_MIN(bma.length,
4552 					bma.got.br_blockcount -
4553 					(bno - bma.got.br_startoff));
4554 			} else {
4555 				if (!eof)
4556 					bma.length = XFS_FILBLKS_MIN(bma.length,
4557 						bma.got.br_startoff - bno);
4558 			}
4559 
4560 			ASSERT(bma.length > 0);
4561 			error = xfs_bmapi_allocate(&bma);
4562 			if (error) {
4563 				/*
4564 				 * If we already allocated space in a previous
4565 				 * iteration return what we go so far when
4566 				 * running out of space.
4567 				 */
4568 				if (error == -ENOSPC && bma.nallocs)
4569 					break;
4570 				goto error0;
4571 			}
4572 
4573 			/*
4574 			 * If this is a CoW allocation, record the data in
4575 			 * the refcount btree for orphan recovery.
4576 			 */
4577 			if (whichfork == XFS_COW_FORK)
4578 				xfs_refcount_alloc_cow_extent(tp, bma.blkno,
4579 						bma.length);
4580 		}
4581 
4582 		/* Deal with the allocated space we found.  */
4583 		xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
4584 							end, n, flags);
4585 
4586 		/* Execute unwritten extent conversion if necessary */
4587 		error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
4588 		if (error == -EAGAIN)
4589 			continue;
4590 		if (error)
4591 			goto error0;
4592 
4593 		/* update the extent map to return */
4594 		xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4595 
4596 		/*
4597 		 * If we're done, stop now.  Stop when we've allocated
4598 		 * XFS_BMAP_MAX_NMAP extents no matter what.  Otherwise
4599 		 * the transaction may get too big.
4600 		 */
4601 		if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
4602 			break;
4603 
4604 		/* Else go on to the next record. */
4605 		bma.prev = bma.got;
4606 		if (!xfs_iext_next_extent(ifp, &bma.icur, &bma.got))
4607 			eof = true;
4608 	}
4609 
4610 	error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, &bma.logflags,
4611 			whichfork);
4612 	if (error)
4613 		goto error0;
4614 
4615 	ASSERT(ifp->if_format != XFS_DINODE_FMT_BTREE ||
4616 	       ifp->if_nextents > XFS_IFORK_MAXEXT(ip, whichfork));
4617 	xfs_bmapi_finish(&bma, whichfork, 0);
4618 	xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4619 		orig_nmap, n);
4620 
4621 	/*
4622 	 * When converting delayed allocations, xfs_bmapi_allocate ignores
4623 	 * the passed in bno and always converts from the start of the found
4624 	 * delalloc extent.
4625 	 *
4626 	 * To avoid a successful return with *nmap set to 0, return the magic
4627 	 * -ENOSR error code for this particular case so that the caller can
4628 	 * handle it.
4629 	 */
4630 	if (!n) {
4631 		ASSERT(bma.nallocs >= *nmap);
4632 		return -ENOSR;
4633 	}
4634 	*nmap = n;
4635 	return 0;
4636 error0:
4637 	xfs_bmapi_finish(&bma, whichfork, error);
4638 	return error;
4639 }
4640 
4641 /*
4642  * Convert an existing delalloc extent to real blocks based on file offset. This
4643  * attempts to allocate the entire delalloc extent and may require multiple
4644  * invocations to allocate the target offset if a large enough physical extent
4645  * is not available.
4646  */
4647 static int
4648 xfs_bmapi_convert_one_delalloc(
4649 	struct xfs_inode	*ip,
4650 	int			whichfork,
4651 	xfs_off_t		offset,
4652 	struct iomap		*iomap,
4653 	unsigned int		*seq)
4654 {
4655 	struct xfs_ifork	*ifp = xfs_ifork_ptr(ip, whichfork);
4656 	struct xfs_mount	*mp = ip->i_mount;
4657 	xfs_fileoff_t		offset_fsb = XFS_B_TO_FSBT(mp, offset);
4658 	struct xfs_bmalloca	bma = { NULL };
4659 	uint16_t		flags = 0;
4660 	struct xfs_trans	*tp;
4661 	int			error;
4662 
4663 	if (whichfork == XFS_COW_FORK)
4664 		flags |= IOMAP_F_SHARED;
4665 
4666 	/*
4667 	 * Space for the extent and indirect blocks was reserved when the
4668 	 * delalloc extent was created so there's no need to do so here.
4669 	 */
4670 	error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0,
4671 				XFS_TRANS_RESERVE, &tp);
4672 	if (error)
4673 		return error;
4674 
4675 	xfs_ilock(ip, XFS_ILOCK_EXCL);
4676 	xfs_trans_ijoin(tp, ip, 0);
4677 
4678 	error = xfs_iext_count_extend(tp, ip, whichfork,
4679 			XFS_IEXT_ADD_NOSPLIT_CNT);
4680 	if (error)
4681 		goto out_trans_cancel;
4682 
4683 	if (!xfs_iext_lookup_extent(ip, ifp, offset_fsb, &bma.icur, &bma.got) ||
4684 	    bma.got.br_startoff > offset_fsb) {
4685 		/*
4686 		 * No extent found in the range we are trying to convert.  This
4687 		 * should only happen for the COW fork, where another thread
4688 		 * might have moved the extent to the data fork in the meantime.
4689 		 */
4690 		WARN_ON_ONCE(whichfork != XFS_COW_FORK);
4691 		error = -EAGAIN;
4692 		goto out_trans_cancel;
4693 	}
4694 
4695 	/*
4696 	 * If we find a real extent here we raced with another thread converting
4697 	 * the extent.  Just return the real extent at this offset.
4698 	 */
4699 	if (!isnullstartblock(bma.got.br_startblock)) {
4700 		xfs_bmbt_to_iomap(ip, iomap, &bma.got, 0, flags,
4701 				xfs_iomap_inode_sequence(ip, flags));
4702 		if (seq)
4703 			*seq = READ_ONCE(ifp->if_seq);
4704 		goto out_trans_cancel;
4705 	}
4706 
4707 	bma.tp = tp;
4708 	bma.ip = ip;
4709 	bma.wasdel = true;
4710 	bma.minleft = xfs_bmapi_minleft(tp, ip, whichfork);
4711 
4712 	/*
4713 	 * Always allocate convert from the start of the delalloc extent even if
4714 	 * that is outside the passed in range to create large contiguous
4715 	 * extents on disk.
4716 	 */
4717 	bma.offset = bma.got.br_startoff;
4718 	bma.length = bma.got.br_blockcount;
4719 
4720 	/*
4721 	 * When we're converting the delalloc reservations backing dirty pages
4722 	 * in the page cache, we must be careful about how we create the new
4723 	 * extents:
4724 	 *
4725 	 * New CoW fork extents are created unwritten, turned into real extents
4726 	 * when we're about to write the data to disk, and mapped into the data
4727 	 * fork after the write finishes.  End of story.
4728 	 *
4729 	 * New data fork extents must be mapped in as unwritten and converted
4730 	 * to real extents after the write succeeds to avoid exposing stale
4731 	 * disk contents if we crash.
4732 	 */
4733 	bma.flags = XFS_BMAPI_PREALLOC;
4734 	if (whichfork == XFS_COW_FORK)
4735 		bma.flags |= XFS_BMAPI_COWFORK;
4736 
4737 	if (!xfs_iext_peek_prev_extent(ifp, &bma.icur, &bma.prev))
4738 		bma.prev.br_startoff = NULLFILEOFF;
4739 
4740 	error = xfs_bmapi_allocate(&bma);
4741 	if (error)
4742 		goto out_finish;
4743 
4744 	XFS_STATS_ADD(mp, xs_xstrat_bytes, XFS_FSB_TO_B(mp, bma.length));
4745 	XFS_STATS_INC(mp, xs_xstrat_quick);
4746 
4747 	ASSERT(!isnullstartblock(bma.got.br_startblock));
4748 	xfs_bmbt_to_iomap(ip, iomap, &bma.got, 0, flags,
4749 				xfs_iomap_inode_sequence(ip, flags));
4750 	if (seq)
4751 		*seq = READ_ONCE(ifp->if_seq);
4752 
4753 	if (whichfork == XFS_COW_FORK)
4754 		xfs_refcount_alloc_cow_extent(tp, bma.blkno, bma.length);
4755 
4756 	error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, &bma.logflags,
4757 			whichfork);
4758 	if (error)
4759 		goto out_finish;
4760 
4761 	xfs_bmapi_finish(&bma, whichfork, 0);
4762 	error = xfs_trans_commit(tp);
4763 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
4764 	return error;
4765 
4766 out_finish:
4767 	xfs_bmapi_finish(&bma, whichfork, error);
4768 out_trans_cancel:
4769 	xfs_trans_cancel(tp);
4770 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
4771 	return error;
4772 }
4773 
4774 /*
4775  * Pass in a dellalloc extent and convert it to real extents, return the real
4776  * extent that maps offset_fsb in iomap.
4777  */
4778 int
4779 xfs_bmapi_convert_delalloc(
4780 	struct xfs_inode	*ip,
4781 	int			whichfork,
4782 	loff_t			offset,
4783 	struct iomap		*iomap,
4784 	unsigned int		*seq)
4785 {
4786 	int			error;
4787 
4788 	/*
4789 	 * Attempt to allocate whatever delalloc extent currently backs offset
4790 	 * and put the result into iomap.  Allocate in a loop because it may
4791 	 * take several attempts to allocate real blocks for a contiguous
4792 	 * delalloc extent if free space is sufficiently fragmented.
4793 	 */
4794 	do {
4795 		error = xfs_bmapi_convert_one_delalloc(ip, whichfork, offset,
4796 					iomap, seq);
4797 		if (error)
4798 			return error;
4799 	} while (iomap->offset + iomap->length <= offset);
4800 
4801 	return 0;
4802 }
4803 
4804 int
4805 xfs_bmapi_remap(
4806 	struct xfs_trans	*tp,
4807 	struct xfs_inode	*ip,
4808 	xfs_fileoff_t		bno,
4809 	xfs_filblks_t		len,
4810 	xfs_fsblock_t		startblock,
4811 	uint32_t		flags)
4812 {
4813 	struct xfs_mount	*mp = ip->i_mount;
4814 	struct xfs_ifork	*ifp;
4815 	struct xfs_btree_cur	*cur = NULL;
4816 	struct xfs_bmbt_irec	got;
4817 	struct xfs_iext_cursor	icur;
4818 	int			whichfork = xfs_bmapi_whichfork(flags);
4819 	int			logflags = 0, error;
4820 
4821 	ifp = xfs_ifork_ptr(ip, whichfork);
4822 	ASSERT(len > 0);
4823 	ASSERT(len <= (xfs_filblks_t)XFS_MAX_BMBT_EXTLEN);
4824 	xfs_assert_ilocked(ip, XFS_ILOCK_EXCL);
4825 	ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC |
4826 			   XFS_BMAPI_NORMAP)));
4827 	ASSERT((flags & (XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC)) !=
4828 			(XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC));
4829 
4830 	if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
4831 	    XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
4832 		xfs_bmap_mark_sick(ip, whichfork);
4833 		return -EFSCORRUPTED;
4834 	}
4835 
4836 	if (xfs_is_shutdown(mp))
4837 		return -EIO;
4838 
4839 	error = xfs_iread_extents(tp, ip, whichfork);
4840 	if (error)
4841 		return error;
4842 
4843 	if (xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got)) {
4844 		/* make sure we only reflink into a hole. */
4845 		ASSERT(got.br_startoff > bno);
4846 		ASSERT(got.br_startoff - bno >= len);
4847 	}
4848 
4849 	ip->i_nblocks += len;
4850 	xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
4851 
4852 	if (ifp->if_format == XFS_DINODE_FMT_BTREE)
4853 		cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
4854 
4855 	got.br_startoff = bno;
4856 	got.br_startblock = startblock;
4857 	got.br_blockcount = len;
4858 	if (flags & XFS_BMAPI_PREALLOC)
4859 		got.br_state = XFS_EXT_UNWRITTEN;
4860 	else
4861 		got.br_state = XFS_EXT_NORM;
4862 
4863 	error = xfs_bmap_add_extent_hole_real(tp, ip, whichfork, &icur,
4864 			&cur, &got, &logflags, flags);
4865 	if (error)
4866 		goto error0;
4867 
4868 	error = xfs_bmap_btree_to_extents(tp, ip, cur, &logflags, whichfork);
4869 
4870 error0:
4871 	if (ip->i_df.if_format != XFS_DINODE_FMT_EXTENTS)
4872 		logflags &= ~XFS_ILOG_DEXT;
4873 	else if (ip->i_df.if_format != XFS_DINODE_FMT_BTREE)
4874 		logflags &= ~XFS_ILOG_DBROOT;
4875 
4876 	if (logflags)
4877 		xfs_trans_log_inode(tp, ip, logflags);
4878 	if (cur)
4879 		xfs_btree_del_cursor(cur, error);
4880 	return error;
4881 }
4882 
4883 /*
4884  * When a delalloc extent is split (e.g., due to a hole punch), the original
4885  * indlen reservation must be shared across the two new extents that are left
4886  * behind.
4887  *
4888  * Given the original reservation and the worst case indlen for the two new
4889  * extents (as calculated by xfs_bmap_worst_indlen()), split the original
4890  * reservation fairly across the two new extents. If necessary, steal available
4891  * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4892  * ores == 1). The number of stolen blocks is returned. The availability and
4893  * subsequent accounting of stolen blocks is the responsibility of the caller.
4894  */
4895 static void
4896 xfs_bmap_split_indlen(
4897 	xfs_filblks_t			ores,		/* original res. */
4898 	xfs_filblks_t			*indlen1,	/* ext1 worst indlen */
4899 	xfs_filblks_t			*indlen2)	/* ext2 worst indlen */
4900 {
4901 	xfs_filblks_t			len1 = *indlen1;
4902 	xfs_filblks_t			len2 = *indlen2;
4903 	xfs_filblks_t			nres = len1 + len2; /* new total res. */
4904 	xfs_filblks_t			resfactor;
4905 
4906 	/*
4907 	 * We can't meet the total required reservation for the two extents.
4908 	 * Calculate the percent of the overall shortage between both extents
4909 	 * and apply this percentage to each of the requested indlen values.
4910 	 * This distributes the shortage fairly and reduces the chances that one
4911 	 * of the two extents is left with nothing when extents are repeatedly
4912 	 * split.
4913 	 */
4914 	resfactor = (ores * 100);
4915 	do_div(resfactor, nres);
4916 	len1 *= resfactor;
4917 	do_div(len1, 100);
4918 	len2 *= resfactor;
4919 	do_div(len2, 100);
4920 	ASSERT(len1 + len2 <= ores);
4921 	ASSERT(len1 < *indlen1 && len2 < *indlen2);
4922 
4923 	/*
4924 	 * Hand out the remainder to each extent. If one of the two reservations
4925 	 * is zero, we want to make sure that one gets a block first. The loop
4926 	 * below starts with len1, so hand len2 a block right off the bat if it
4927 	 * is zero.
4928 	 */
4929 	ores -= (len1 + len2);
4930 	ASSERT((*indlen1 - len1) + (*indlen2 - len2) >= ores);
4931 	if (ores && !len2 && *indlen2) {
4932 		len2++;
4933 		ores--;
4934 	}
4935 	while (ores) {
4936 		if (len1 < *indlen1) {
4937 			len1++;
4938 			ores--;
4939 		}
4940 		if (!ores)
4941 			break;
4942 		if (len2 < *indlen2) {
4943 			len2++;
4944 			ores--;
4945 		}
4946 	}
4947 
4948 	*indlen1 = len1;
4949 	*indlen2 = len2;
4950 }
4951 
4952 void
4953 xfs_bmap_del_extent_delay(
4954 	struct xfs_inode	*ip,
4955 	int			whichfork,
4956 	struct xfs_iext_cursor	*icur,
4957 	struct xfs_bmbt_irec	*got,
4958 	struct xfs_bmbt_irec	*del)
4959 {
4960 	struct xfs_mount	*mp = ip->i_mount;
4961 	struct xfs_ifork	*ifp = xfs_ifork_ptr(ip, whichfork);
4962 	struct xfs_bmbt_irec	new;
4963 	int64_t			da_old, da_new, da_diff = 0;
4964 	xfs_fileoff_t		del_endoff, got_endoff;
4965 	xfs_filblks_t		got_indlen, new_indlen, stolen = 0;
4966 	uint32_t		state = xfs_bmap_fork_to_state(whichfork);
4967 	uint64_t		fdblocks;
4968 	bool			isrt;
4969 
4970 	XFS_STATS_INC(mp, xs_del_exlist);
4971 
4972 	isrt = xfs_ifork_is_realtime(ip, whichfork);
4973 	del_endoff = del->br_startoff + del->br_blockcount;
4974 	got_endoff = got->br_startoff + got->br_blockcount;
4975 	da_old = startblockval(got->br_startblock);
4976 	da_new = 0;
4977 
4978 	ASSERT(del->br_blockcount > 0);
4979 	ASSERT(got->br_startoff <= del->br_startoff);
4980 	ASSERT(got_endoff >= del_endoff);
4981 
4982 	/*
4983 	 * Update the inode delalloc counter now and wait to update the
4984 	 * sb counters as we might have to borrow some blocks for the
4985 	 * indirect block accounting.
4986 	 */
4987 	xfs_quota_unreserve_blkres(ip, del->br_blockcount);
4988 	ip->i_delayed_blks -= del->br_blockcount;
4989 
4990 	if (got->br_startoff == del->br_startoff)
4991 		state |= BMAP_LEFT_FILLING;
4992 	if (got_endoff == del_endoff)
4993 		state |= BMAP_RIGHT_FILLING;
4994 
4995 	switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4996 	case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
4997 		/*
4998 		 * Matches the whole extent.  Delete the entry.
4999 		 */
5000 		xfs_iext_remove(ip, icur, state);
5001 		xfs_iext_prev(ifp, icur);
5002 		break;
5003 	case BMAP_LEFT_FILLING:
5004 		/*
5005 		 * Deleting the first part of the extent.
5006 		 */
5007 		got->br_startoff = del_endoff;
5008 		got->br_blockcount -= del->br_blockcount;
5009 		da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
5010 				got->br_blockcount), da_old);
5011 		got->br_startblock = nullstartblock((int)da_new);
5012 		xfs_iext_update_extent(ip, state, icur, got);
5013 		break;
5014 	case BMAP_RIGHT_FILLING:
5015 		/*
5016 		 * Deleting the last part of the extent.
5017 		 */
5018 		got->br_blockcount = 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 0:
5025 		/*
5026 		 * Deleting the middle of the extent.
5027 		 *
5028 		 * Distribute the original indlen reservation across the two new
5029 		 * extents.  Steal blocks from the deleted extent if necessary.
5030 		 * Stealing blocks simply fudges the fdblocks accounting below.
5031 		 * Warn if either of the new indlen reservations is zero as this
5032 		 * can lead to delalloc problems.
5033 		 */
5034 		got->br_blockcount = del->br_startoff - got->br_startoff;
5035 		got_indlen = xfs_bmap_worst_indlen(ip, got->br_blockcount);
5036 
5037 		new.br_blockcount = got_endoff - del_endoff;
5038 		new_indlen = xfs_bmap_worst_indlen(ip, new.br_blockcount);
5039 
5040 		WARN_ON_ONCE(!got_indlen || !new_indlen);
5041 		/*
5042 		 * Steal as many blocks as we can to try and satisfy the worst
5043 		 * case indlen for both new extents.
5044 		 *
5045 		 * However, we can't just steal reservations from the data
5046 		 * blocks if this is an RT inodes as the data and metadata
5047 		 * blocks come from different pools.  We'll have to live with
5048 		 * under-filled indirect reservation in this case.
5049 		 */
5050 		da_new = got_indlen + new_indlen;
5051 		if (da_new > da_old && !isrt) {
5052 			stolen = XFS_FILBLKS_MIN(da_new - da_old,
5053 						 del->br_blockcount);
5054 			da_old += stolen;
5055 		}
5056 		if (da_new > da_old)
5057 			xfs_bmap_split_indlen(da_old, &got_indlen, &new_indlen);
5058 		da_new = got_indlen + new_indlen;
5059 
5060 		got->br_startblock = nullstartblock((int)got_indlen);
5061 
5062 		new.br_startoff = del_endoff;
5063 		new.br_state = got->br_state;
5064 		new.br_startblock = nullstartblock((int)new_indlen);
5065 
5066 		xfs_iext_update_extent(ip, state, icur, got);
5067 		xfs_iext_next(ifp, icur);
5068 		xfs_iext_insert(ip, icur, &new, state);
5069 
5070 		del->br_blockcount -= stolen;
5071 		break;
5072 	}
5073 
5074 	ASSERT(da_old >= da_new);
5075 	da_diff = da_old - da_new;
5076 	fdblocks = da_diff;
5077 
5078 	if (isrt)
5079 		xfs_add_frextents(mp, xfs_rtb_to_rtx(mp, del->br_blockcount));
5080 	else
5081 		fdblocks += del->br_blockcount;
5082 
5083 	xfs_add_fdblocks(mp, fdblocks);
5084 	xfs_mod_delalloc(ip, -(int64_t)del->br_blockcount, -da_diff);
5085 }
5086 
5087 void
5088 xfs_bmap_del_extent_cow(
5089 	struct xfs_inode	*ip,
5090 	struct xfs_iext_cursor	*icur,
5091 	struct xfs_bmbt_irec	*got,
5092 	struct xfs_bmbt_irec	*del)
5093 {
5094 	struct xfs_mount	*mp = ip->i_mount;
5095 	struct xfs_ifork	*ifp = xfs_ifork_ptr(ip, XFS_COW_FORK);
5096 	struct xfs_bmbt_irec	new;
5097 	xfs_fileoff_t		del_endoff, got_endoff;
5098 	uint32_t		state = BMAP_COWFORK;
5099 
5100 	XFS_STATS_INC(mp, xs_del_exlist);
5101 
5102 	del_endoff = del->br_startoff + del->br_blockcount;
5103 	got_endoff = got->br_startoff + got->br_blockcount;
5104 
5105 	ASSERT(del->br_blockcount > 0);
5106 	ASSERT(got->br_startoff <= del->br_startoff);
5107 	ASSERT(got_endoff >= del_endoff);
5108 	ASSERT(!isnullstartblock(got->br_startblock));
5109 
5110 	if (got->br_startoff == del->br_startoff)
5111 		state |= BMAP_LEFT_FILLING;
5112 	if (got_endoff == del_endoff)
5113 		state |= BMAP_RIGHT_FILLING;
5114 
5115 	switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
5116 	case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
5117 		/*
5118 		 * Matches the whole extent.  Delete the entry.
5119 		 */
5120 		xfs_iext_remove(ip, icur, state);
5121 		xfs_iext_prev(ifp, icur);
5122 		break;
5123 	case BMAP_LEFT_FILLING:
5124 		/*
5125 		 * Deleting the first part of the extent.
5126 		 */
5127 		got->br_startoff = del_endoff;
5128 		got->br_blockcount -= del->br_blockcount;
5129 		got->br_startblock = del->br_startblock + del->br_blockcount;
5130 		xfs_iext_update_extent(ip, state, icur, got);
5131 		break;
5132 	case BMAP_RIGHT_FILLING:
5133 		/*
5134 		 * Deleting the last part of the extent.
5135 		 */
5136 		got->br_blockcount -= del->br_blockcount;
5137 		xfs_iext_update_extent(ip, state, icur, got);
5138 		break;
5139 	case 0:
5140 		/*
5141 		 * Deleting the middle of the extent.
5142 		 */
5143 		got->br_blockcount = del->br_startoff - got->br_startoff;
5144 
5145 		new.br_startoff = del_endoff;
5146 		new.br_blockcount = got_endoff - del_endoff;
5147 		new.br_state = got->br_state;
5148 		new.br_startblock = del->br_startblock + del->br_blockcount;
5149 
5150 		xfs_iext_update_extent(ip, state, icur, got);
5151 		xfs_iext_next(ifp, icur);
5152 		xfs_iext_insert(ip, icur, &new, state);
5153 		break;
5154 	}
5155 	ip->i_delayed_blks -= del->br_blockcount;
5156 }
5157 
5158 /*
5159  * Called by xfs_bmapi to update file extent records and the btree
5160  * after removing space.
5161  */
5162 STATIC int				/* error */
5163 xfs_bmap_del_extent_real(
5164 	xfs_inode_t		*ip,	/* incore inode pointer */
5165 	xfs_trans_t		*tp,	/* current transaction pointer */
5166 	struct xfs_iext_cursor	*icur,
5167 	struct xfs_btree_cur	*cur,	/* if null, not a btree */
5168 	xfs_bmbt_irec_t		*del,	/* data to remove from extents */
5169 	int			*logflagsp, /* inode logging flags */
5170 	int			whichfork, /* data or attr fork */
5171 	uint32_t		bflags)	/* bmapi flags */
5172 {
5173 	xfs_fsblock_t		del_endblock=0;	/* first block past del */
5174 	xfs_fileoff_t		del_endoff;	/* first offset past del */
5175 	int			error = 0;	/* error return value */
5176 	struct xfs_bmbt_irec	got;	/* current extent entry */
5177 	xfs_fileoff_t		got_endoff;	/* first offset past got */
5178 	int			i;	/* temp state */
5179 	struct xfs_ifork	*ifp;	/* inode fork pointer */
5180 	xfs_mount_t		*mp;	/* mount structure */
5181 	xfs_filblks_t		nblks;	/* quota/sb block count */
5182 	xfs_bmbt_irec_t		new;	/* new record to be inserted */
5183 	/* REFERENCED */
5184 	uint			qfield;	/* quota field to update */
5185 	uint32_t		state = xfs_bmap_fork_to_state(whichfork);
5186 	struct xfs_bmbt_irec	old;
5187 
5188 	*logflagsp = 0;
5189 
5190 	mp = ip->i_mount;
5191 	XFS_STATS_INC(mp, xs_del_exlist);
5192 
5193 	ifp = xfs_ifork_ptr(ip, whichfork);
5194 	ASSERT(del->br_blockcount > 0);
5195 	xfs_iext_get_extent(ifp, icur, &got);
5196 	ASSERT(got.br_startoff <= del->br_startoff);
5197 	del_endoff = del->br_startoff + del->br_blockcount;
5198 	got_endoff = got.br_startoff + got.br_blockcount;
5199 	ASSERT(got_endoff >= del_endoff);
5200 	ASSERT(!isnullstartblock(got.br_startblock));
5201 	qfield = 0;
5202 
5203 	/*
5204 	 * If it's the case where the directory code is running with no block
5205 	 * reservation, and the deleted block is in the middle of its extent,
5206 	 * and the resulting insert of an extent would cause transformation to
5207 	 * btree format, then reject it.  The calling code will then swap blocks
5208 	 * around instead.  We have to do this now, rather than waiting for the
5209 	 * conversion to btree format, since the transaction will be dirty then.
5210 	 */
5211 	if (tp->t_blk_res == 0 &&
5212 	    ifp->if_format == XFS_DINODE_FMT_EXTENTS &&
5213 	    ifp->if_nextents >= XFS_IFORK_MAXEXT(ip, whichfork) &&
5214 	    del->br_startoff > got.br_startoff && del_endoff < got_endoff)
5215 		return -ENOSPC;
5216 
5217 	*logflagsp = XFS_ILOG_CORE;
5218 	if (xfs_ifork_is_realtime(ip, whichfork))
5219 		qfield = XFS_TRANS_DQ_RTBCOUNT;
5220 	else
5221 		qfield = XFS_TRANS_DQ_BCOUNT;
5222 	nblks = del->br_blockcount;
5223 
5224 	del_endblock = del->br_startblock + del->br_blockcount;
5225 	if (cur) {
5226 		error = xfs_bmbt_lookup_eq(cur, &got, &i);
5227 		if (error)
5228 			return error;
5229 		if (XFS_IS_CORRUPT(mp, i != 1)) {
5230 			xfs_btree_mark_sick(cur);
5231 			return -EFSCORRUPTED;
5232 		}
5233 	}
5234 
5235 	if (got.br_startoff == del->br_startoff)
5236 		state |= BMAP_LEFT_FILLING;
5237 	if (got_endoff == del_endoff)
5238 		state |= BMAP_RIGHT_FILLING;
5239 
5240 	switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
5241 	case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
5242 		/*
5243 		 * Matches the whole extent.  Delete the entry.
5244 		 */
5245 		xfs_iext_remove(ip, icur, state);
5246 		xfs_iext_prev(ifp, icur);
5247 		ifp->if_nextents--;
5248 
5249 		*logflagsp |= XFS_ILOG_CORE;
5250 		if (!cur) {
5251 			*logflagsp |= xfs_ilog_fext(whichfork);
5252 			break;
5253 		}
5254 		if ((error = xfs_btree_delete(cur, &i)))
5255 			return error;
5256 		if (XFS_IS_CORRUPT(mp, i != 1)) {
5257 			xfs_btree_mark_sick(cur);
5258 			return -EFSCORRUPTED;
5259 		}
5260 		break;
5261 	case BMAP_LEFT_FILLING:
5262 		/*
5263 		 * Deleting the first part of the extent.
5264 		 */
5265 		got.br_startoff = del_endoff;
5266 		got.br_startblock = del_endblock;
5267 		got.br_blockcount -= del->br_blockcount;
5268 		xfs_iext_update_extent(ip, state, icur, &got);
5269 		if (!cur) {
5270 			*logflagsp |= xfs_ilog_fext(whichfork);
5271 			break;
5272 		}
5273 		error = xfs_bmbt_update(cur, &got);
5274 		if (error)
5275 			return error;
5276 		break;
5277 	case BMAP_RIGHT_FILLING:
5278 		/*
5279 		 * Deleting the last part of the extent.
5280 		 */
5281 		got.br_blockcount -= del->br_blockcount;
5282 		xfs_iext_update_extent(ip, state, icur, &got);
5283 		if (!cur) {
5284 			*logflagsp |= xfs_ilog_fext(whichfork);
5285 			break;
5286 		}
5287 		error = xfs_bmbt_update(cur, &got);
5288 		if (error)
5289 			return error;
5290 		break;
5291 	case 0:
5292 		/*
5293 		 * Deleting the middle of the extent.
5294 		 */
5295 
5296 		old = got;
5297 
5298 		got.br_blockcount = del->br_startoff - got.br_startoff;
5299 		xfs_iext_update_extent(ip, state, icur, &got);
5300 
5301 		new.br_startoff = del_endoff;
5302 		new.br_blockcount = got_endoff - del_endoff;
5303 		new.br_state = got.br_state;
5304 		new.br_startblock = del_endblock;
5305 
5306 		*logflagsp |= XFS_ILOG_CORE;
5307 		if (cur) {
5308 			error = xfs_bmbt_update(cur, &got);
5309 			if (error)
5310 				return error;
5311 			error = xfs_btree_increment(cur, 0, &i);
5312 			if (error)
5313 				return error;
5314 			cur->bc_rec.b = new;
5315 			error = xfs_btree_insert(cur, &i);
5316 			if (error && error != -ENOSPC)
5317 				return error;
5318 			/*
5319 			 * If get no-space back from btree insert, it tried a
5320 			 * split, and we have a zero block reservation.  Fix up
5321 			 * our state and return the error.
5322 			 */
5323 			if (error == -ENOSPC) {
5324 				/*
5325 				 * Reset the cursor, don't trust it after any
5326 				 * insert operation.
5327 				 */
5328 				error = xfs_bmbt_lookup_eq(cur, &got, &i);
5329 				if (error)
5330 					return error;
5331 				if (XFS_IS_CORRUPT(mp, i != 1)) {
5332 					xfs_btree_mark_sick(cur);
5333 					return -EFSCORRUPTED;
5334 				}
5335 				/*
5336 				 * Update the btree record back
5337 				 * to the original value.
5338 				 */
5339 				error = xfs_bmbt_update(cur, &old);
5340 				if (error)
5341 					return error;
5342 				/*
5343 				 * Reset the extent record back
5344 				 * to the original value.
5345 				 */
5346 				xfs_iext_update_extent(ip, state, icur, &old);
5347 				*logflagsp = 0;
5348 				return -ENOSPC;
5349 			}
5350 			if (XFS_IS_CORRUPT(mp, i != 1)) {
5351 				xfs_btree_mark_sick(cur);
5352 				return -EFSCORRUPTED;
5353 			}
5354 		} else
5355 			*logflagsp |= xfs_ilog_fext(whichfork);
5356 
5357 		ifp->if_nextents++;
5358 		xfs_iext_next(ifp, icur);
5359 		xfs_iext_insert(ip, icur, &new, state);
5360 		break;
5361 	}
5362 
5363 	/* remove reverse mapping */
5364 	xfs_rmap_unmap_extent(tp, ip, whichfork, del);
5365 
5366 	/*
5367 	 * If we need to, add to list of extents to delete.
5368 	 */
5369 	if (!(bflags & XFS_BMAPI_REMAP)) {
5370 		if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) {
5371 			xfs_refcount_decrease_extent(tp, del);
5372 		} else if (xfs_ifork_is_realtime(ip, whichfork)) {
5373 			/*
5374 			 * Ensure the bitmap and summary inodes are locked
5375 			 * and joined to the transaction before modifying them.
5376 			 */
5377 			if (!(tp->t_flags & XFS_TRANS_RTBITMAP_LOCKED)) {
5378 				tp->t_flags |= XFS_TRANS_RTBITMAP_LOCKED;
5379 				xfs_rtbitmap_lock(tp, mp);
5380 			}
5381 			error = xfs_rtfree_blocks(tp, del->br_startblock,
5382 					del->br_blockcount);
5383 		} else {
5384 			unsigned int	efi_flags = 0;
5385 
5386 			if ((bflags & XFS_BMAPI_NODISCARD) ||
5387 			    del->br_state == XFS_EXT_UNWRITTEN)
5388 				efi_flags |= XFS_FREE_EXTENT_SKIP_DISCARD;
5389 
5390 			error = xfs_free_extent_later(tp, del->br_startblock,
5391 					del->br_blockcount, NULL,
5392 					XFS_AG_RESV_NONE, efi_flags);
5393 		}
5394 		if (error)
5395 			return error;
5396 	}
5397 
5398 	/*
5399 	 * Adjust inode # blocks in the file.
5400 	 */
5401 	if (nblks)
5402 		ip->i_nblocks -= nblks;
5403 	/*
5404 	 * Adjust quota data.
5405 	 */
5406 	if (qfield && !(bflags & XFS_BMAPI_REMAP))
5407 		xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
5408 
5409 	return 0;
5410 }
5411 
5412 /*
5413  * Unmap (remove) blocks from a file.
5414  * If nexts is nonzero then the number of extents to remove is limited to
5415  * that value.  If not all extents in the block range can be removed then
5416  * *done is set.
5417  */
5418 static int
5419 __xfs_bunmapi(
5420 	struct xfs_trans	*tp,		/* transaction pointer */
5421 	struct xfs_inode	*ip,		/* incore inode */
5422 	xfs_fileoff_t		start,		/* first file offset deleted */
5423 	xfs_filblks_t		*rlen,		/* i/o: amount remaining */
5424 	uint32_t		flags,		/* misc flags */
5425 	xfs_extnum_t		nexts)		/* number of extents max */
5426 {
5427 	struct xfs_btree_cur	*cur;		/* bmap btree cursor */
5428 	struct xfs_bmbt_irec	del;		/* extent being deleted */
5429 	int			error;		/* error return value */
5430 	xfs_extnum_t		extno;		/* extent number in list */
5431 	struct xfs_bmbt_irec	got;		/* current extent record */
5432 	struct xfs_ifork	*ifp;		/* inode fork pointer */
5433 	int			isrt;		/* freeing in rt area */
5434 	int			logflags;	/* transaction logging flags */
5435 	xfs_extlen_t		mod;		/* rt extent offset */
5436 	struct xfs_mount	*mp = ip->i_mount;
5437 	int			tmp_logflags;	/* partial logging flags */
5438 	int			wasdel;		/* was a delayed alloc extent */
5439 	int			whichfork;	/* data or attribute fork */
5440 	xfs_filblks_t		len = *rlen;	/* length to unmap in file */
5441 	xfs_fileoff_t		end;
5442 	struct xfs_iext_cursor	icur;
5443 	bool			done = false;
5444 
5445 	trace_xfs_bunmap(ip, start, len, flags, _RET_IP_);
5446 
5447 	whichfork = xfs_bmapi_whichfork(flags);
5448 	ASSERT(whichfork != XFS_COW_FORK);
5449 	ifp = xfs_ifork_ptr(ip, whichfork);
5450 	if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp))) {
5451 		xfs_bmap_mark_sick(ip, whichfork);
5452 		return -EFSCORRUPTED;
5453 	}
5454 	if (xfs_is_shutdown(mp))
5455 		return -EIO;
5456 
5457 	xfs_assert_ilocked(ip, XFS_ILOCK_EXCL);
5458 	ASSERT(len > 0);
5459 	ASSERT(nexts >= 0);
5460 
5461 	error = xfs_iread_extents(tp, ip, whichfork);
5462 	if (error)
5463 		return error;
5464 
5465 	if (xfs_iext_count(ifp) == 0) {
5466 		*rlen = 0;
5467 		return 0;
5468 	}
5469 	XFS_STATS_INC(mp, xs_blk_unmap);
5470 	isrt = xfs_ifork_is_realtime(ip, whichfork);
5471 	end = start + len;
5472 
5473 	if (!xfs_iext_lookup_extent_before(ip, ifp, &end, &icur, &got)) {
5474 		*rlen = 0;
5475 		return 0;
5476 	}
5477 	end--;
5478 
5479 	logflags = 0;
5480 	if (ifp->if_format == XFS_DINODE_FMT_BTREE) {
5481 		ASSERT(ifp->if_format == XFS_DINODE_FMT_BTREE);
5482 		cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5483 	} else
5484 		cur = NULL;
5485 
5486 	extno = 0;
5487 	while (end != (xfs_fileoff_t)-1 && end >= start &&
5488 	       (nexts == 0 || extno < nexts)) {
5489 		/*
5490 		 * Is the found extent after a hole in which end lives?
5491 		 * Just back up to the previous extent, if so.
5492 		 */
5493 		if (got.br_startoff > end &&
5494 		    !xfs_iext_prev_extent(ifp, &icur, &got)) {
5495 			done = true;
5496 			break;
5497 		}
5498 		/*
5499 		 * Is the last block of this extent before the range
5500 		 * we're supposed to delete?  If so, we're done.
5501 		 */
5502 		end = XFS_FILEOFF_MIN(end,
5503 			got.br_startoff + got.br_blockcount - 1);
5504 		if (end < start)
5505 			break;
5506 		/*
5507 		 * Then deal with the (possibly delayed) allocated space
5508 		 * we found.
5509 		 */
5510 		del = got;
5511 		wasdel = isnullstartblock(del.br_startblock);
5512 
5513 		if (got.br_startoff < start) {
5514 			del.br_startoff = start;
5515 			del.br_blockcount -= start - got.br_startoff;
5516 			if (!wasdel)
5517 				del.br_startblock += start - got.br_startoff;
5518 		}
5519 		if (del.br_startoff + del.br_blockcount > end + 1)
5520 			del.br_blockcount = end + 1 - del.br_startoff;
5521 
5522 		if (!isrt || (flags & XFS_BMAPI_REMAP))
5523 			goto delete;
5524 
5525 		mod = xfs_rtb_to_rtxoff(mp,
5526 				del.br_startblock + del.br_blockcount);
5527 		if (mod) {
5528 			/*
5529 			 * Realtime extent not lined up at the end.
5530 			 * The extent could have been split into written
5531 			 * and unwritten pieces, or we could just be
5532 			 * unmapping part of it.  But we can't really
5533 			 * get rid of part of a realtime extent.
5534 			 */
5535 			if (del.br_state == XFS_EXT_UNWRITTEN) {
5536 				/*
5537 				 * This piece is unwritten, or we're not
5538 				 * using unwritten extents.  Skip over it.
5539 				 */
5540 				ASSERT((flags & XFS_BMAPI_REMAP) || end >= mod);
5541 				end -= mod > del.br_blockcount ?
5542 					del.br_blockcount : mod;
5543 				if (end < got.br_startoff &&
5544 				    !xfs_iext_prev_extent(ifp, &icur, &got)) {
5545 					done = true;
5546 					break;
5547 				}
5548 				continue;
5549 			}
5550 			/*
5551 			 * It's written, turn it unwritten.
5552 			 * This is better than zeroing it.
5553 			 */
5554 			ASSERT(del.br_state == XFS_EXT_NORM);
5555 			ASSERT(tp->t_blk_res > 0);
5556 			/*
5557 			 * If this spans a realtime extent boundary,
5558 			 * chop it back to the start of the one we end at.
5559 			 */
5560 			if (del.br_blockcount > mod) {
5561 				del.br_startoff += del.br_blockcount - mod;
5562 				del.br_startblock += del.br_blockcount - mod;
5563 				del.br_blockcount = mod;
5564 			}
5565 			del.br_state = XFS_EXT_UNWRITTEN;
5566 			error = xfs_bmap_add_extent_unwritten_real(tp, ip,
5567 					whichfork, &icur, &cur, &del,
5568 					&logflags);
5569 			if (error)
5570 				goto error0;
5571 			goto nodelete;
5572 		}
5573 
5574 		mod = xfs_rtb_to_rtxoff(mp, del.br_startblock);
5575 		if (mod) {
5576 			xfs_extlen_t off = mp->m_sb.sb_rextsize - mod;
5577 
5578 			/*
5579 			 * Realtime extent is lined up at the end but not
5580 			 * at the front.  We'll get rid of full extents if
5581 			 * we can.
5582 			 */
5583 			if (del.br_blockcount > off) {
5584 				del.br_blockcount -= off;
5585 				del.br_startoff += off;
5586 				del.br_startblock += off;
5587 			} else if (del.br_startoff == start &&
5588 				   (del.br_state == XFS_EXT_UNWRITTEN ||
5589 				    tp->t_blk_res == 0)) {
5590 				/*
5591 				 * Can't make it unwritten.  There isn't
5592 				 * a full extent here so just skip it.
5593 				 */
5594 				ASSERT(end >= del.br_blockcount);
5595 				end -= del.br_blockcount;
5596 				if (got.br_startoff > end &&
5597 				    !xfs_iext_prev_extent(ifp, &icur, &got)) {
5598 					done = true;
5599 					break;
5600 				}
5601 				continue;
5602 			} else if (del.br_state == XFS_EXT_UNWRITTEN) {
5603 				struct xfs_bmbt_irec	prev;
5604 				xfs_fileoff_t		unwrite_start;
5605 
5606 				/*
5607 				 * This one is already unwritten.
5608 				 * It must have a written left neighbor.
5609 				 * Unwrite the killed part of that one and
5610 				 * try again.
5611 				 */
5612 				if (!xfs_iext_prev_extent(ifp, &icur, &prev))
5613 					ASSERT(0);
5614 				ASSERT(prev.br_state == XFS_EXT_NORM);
5615 				ASSERT(!isnullstartblock(prev.br_startblock));
5616 				ASSERT(del.br_startblock ==
5617 				       prev.br_startblock + prev.br_blockcount);
5618 				unwrite_start = max3(start,
5619 						     del.br_startoff - mod,
5620 						     prev.br_startoff);
5621 				mod = unwrite_start - prev.br_startoff;
5622 				prev.br_startoff = unwrite_start;
5623 				prev.br_startblock += mod;
5624 				prev.br_blockcount -= mod;
5625 				prev.br_state = XFS_EXT_UNWRITTEN;
5626 				error = xfs_bmap_add_extent_unwritten_real(tp,
5627 						ip, whichfork, &icur, &cur,
5628 						&prev, &logflags);
5629 				if (error)
5630 					goto error0;
5631 				goto nodelete;
5632 			} else {
5633 				ASSERT(del.br_state == XFS_EXT_NORM);
5634 				del.br_state = XFS_EXT_UNWRITTEN;
5635 				error = xfs_bmap_add_extent_unwritten_real(tp,
5636 						ip, whichfork, &icur, &cur,
5637 						&del, &logflags);
5638 				if (error)
5639 					goto error0;
5640 				goto nodelete;
5641 			}
5642 		}
5643 
5644 delete:
5645 		if (wasdel) {
5646 			xfs_bmap_del_extent_delay(ip, whichfork, &icur, &got, &del);
5647 		} else {
5648 			error = xfs_bmap_del_extent_real(ip, tp, &icur, cur,
5649 					&del, &tmp_logflags, whichfork,
5650 					flags);
5651 			logflags |= tmp_logflags;
5652 			if (error)
5653 				goto error0;
5654 		}
5655 
5656 		end = del.br_startoff - 1;
5657 nodelete:
5658 		/*
5659 		 * If not done go on to the next (previous) record.
5660 		 */
5661 		if (end != (xfs_fileoff_t)-1 && end >= start) {
5662 			if (!xfs_iext_get_extent(ifp, &icur, &got) ||
5663 			    (got.br_startoff > end &&
5664 			     !xfs_iext_prev_extent(ifp, &icur, &got))) {
5665 				done = true;
5666 				break;
5667 			}
5668 			extno++;
5669 		}
5670 	}
5671 	if (done || end == (xfs_fileoff_t)-1 || end < start)
5672 		*rlen = 0;
5673 	else
5674 		*rlen = end - start + 1;
5675 
5676 	/*
5677 	 * Convert to a btree if necessary.
5678 	 */
5679 	if (xfs_bmap_needs_btree(ip, whichfork)) {
5680 		ASSERT(cur == NULL);
5681 		error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0,
5682 				&tmp_logflags, whichfork);
5683 		logflags |= tmp_logflags;
5684 	} else {
5685 		error = xfs_bmap_btree_to_extents(tp, ip, cur, &logflags,
5686 			whichfork);
5687 	}
5688 
5689 error0:
5690 	/*
5691 	 * Log everything.  Do this after conversion, there's no point in
5692 	 * logging the extent records if we've converted to btree format.
5693 	 */
5694 	if ((logflags & xfs_ilog_fext(whichfork)) &&
5695 	    ifp->if_format != XFS_DINODE_FMT_EXTENTS)
5696 		logflags &= ~xfs_ilog_fext(whichfork);
5697 	else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
5698 		 ifp->if_format != XFS_DINODE_FMT_BTREE)
5699 		logflags &= ~xfs_ilog_fbroot(whichfork);
5700 	/*
5701 	 * Log inode even in the error case, if the transaction
5702 	 * is dirty we'll need to shut down the filesystem.
5703 	 */
5704 	if (logflags)
5705 		xfs_trans_log_inode(tp, ip, logflags);
5706 	if (cur) {
5707 		if (!error)
5708 			cur->bc_bmap.allocated = 0;
5709 		xfs_btree_del_cursor(cur, error);
5710 	}
5711 	return error;
5712 }
5713 
5714 /* Unmap a range of a file. */
5715 int
5716 xfs_bunmapi(
5717 	xfs_trans_t		*tp,
5718 	struct xfs_inode	*ip,
5719 	xfs_fileoff_t		bno,
5720 	xfs_filblks_t		len,
5721 	uint32_t		flags,
5722 	xfs_extnum_t		nexts,
5723 	int			*done)
5724 {
5725 	int			error;
5726 
5727 	error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts);
5728 	*done = (len == 0);
5729 	return error;
5730 }
5731 
5732 /*
5733  * Determine whether an extent shift can be accomplished by a merge with the
5734  * extent that precedes the target hole of the shift.
5735  */
5736 STATIC bool
5737 xfs_bmse_can_merge(
5738 	struct xfs_bmbt_irec	*left,	/* preceding extent */
5739 	struct xfs_bmbt_irec	*got,	/* current extent to shift */
5740 	xfs_fileoff_t		shift)	/* shift fsb */
5741 {
5742 	xfs_fileoff_t		startoff;
5743 
5744 	startoff = got->br_startoff - shift;
5745 
5746 	/*
5747 	 * The extent, once shifted, must be adjacent in-file and on-disk with
5748 	 * the preceding extent.
5749 	 */
5750 	if ((left->br_startoff + left->br_blockcount != startoff) ||
5751 	    (left->br_startblock + left->br_blockcount != got->br_startblock) ||
5752 	    (left->br_state != got->br_state) ||
5753 	    (left->br_blockcount + got->br_blockcount > XFS_MAX_BMBT_EXTLEN))
5754 		return false;
5755 
5756 	return true;
5757 }
5758 
5759 /*
5760  * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5761  * hole in the file. If an extent shift would result in the extent being fully
5762  * adjacent to the extent that currently precedes the hole, we can merge with
5763  * the preceding extent rather than do the shift.
5764  *
5765  * This function assumes the caller has verified a shift-by-merge is possible
5766  * with the provided extents via xfs_bmse_can_merge().
5767  */
5768 STATIC int
5769 xfs_bmse_merge(
5770 	struct xfs_trans		*tp,
5771 	struct xfs_inode		*ip,
5772 	int				whichfork,
5773 	xfs_fileoff_t			shift,		/* shift fsb */
5774 	struct xfs_iext_cursor		*icur,
5775 	struct xfs_bmbt_irec		*got,		/* extent to shift */
5776 	struct xfs_bmbt_irec		*left,		/* preceding extent */
5777 	struct xfs_btree_cur		*cur,
5778 	int				*logflags)	/* output */
5779 {
5780 	struct xfs_ifork		*ifp = xfs_ifork_ptr(ip, whichfork);
5781 	struct xfs_bmbt_irec		new;
5782 	xfs_filblks_t			blockcount;
5783 	int				error, i;
5784 	struct xfs_mount		*mp = ip->i_mount;
5785 
5786 	blockcount = left->br_blockcount + got->br_blockcount;
5787 
5788 	xfs_assert_ilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
5789 	ASSERT(xfs_bmse_can_merge(left, got, shift));
5790 
5791 	new = *left;
5792 	new.br_blockcount = blockcount;
5793 
5794 	/*
5795 	 * Update the on-disk extent count, the btree if necessary and log the
5796 	 * inode.
5797 	 */
5798 	ifp->if_nextents--;
5799 	*logflags |= XFS_ILOG_CORE;
5800 	if (!cur) {
5801 		*logflags |= XFS_ILOG_DEXT;
5802 		goto done;
5803 	}
5804 
5805 	/* lookup and remove the extent to merge */
5806 	error = xfs_bmbt_lookup_eq(cur, got, &i);
5807 	if (error)
5808 		return error;
5809 	if (XFS_IS_CORRUPT(mp, i != 1)) {
5810 		xfs_btree_mark_sick(cur);
5811 		return -EFSCORRUPTED;
5812 	}
5813 
5814 	error = xfs_btree_delete(cur, &i);
5815 	if (error)
5816 		return error;
5817 	if (XFS_IS_CORRUPT(mp, i != 1)) {
5818 		xfs_btree_mark_sick(cur);
5819 		return -EFSCORRUPTED;
5820 	}
5821 
5822 	/* lookup and update size of the previous extent */
5823 	error = xfs_bmbt_lookup_eq(cur, left, &i);
5824 	if (error)
5825 		return error;
5826 	if (XFS_IS_CORRUPT(mp, i != 1)) {
5827 		xfs_btree_mark_sick(cur);
5828 		return -EFSCORRUPTED;
5829 	}
5830 
5831 	error = xfs_bmbt_update(cur, &new);
5832 	if (error)
5833 		return error;
5834 
5835 	/* change to extent format if required after extent removal */
5836 	error = xfs_bmap_btree_to_extents(tp, ip, cur, logflags, whichfork);
5837 	if (error)
5838 		return error;
5839 
5840 done:
5841 	xfs_iext_remove(ip, icur, 0);
5842 	xfs_iext_prev(ifp, icur);
5843 	xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur,
5844 			&new);
5845 
5846 	/* update reverse mapping. rmap functions merge the rmaps for us */
5847 	xfs_rmap_unmap_extent(tp, ip, whichfork, got);
5848 	memcpy(&new, got, sizeof(new));
5849 	new.br_startoff = left->br_startoff + left->br_blockcount;
5850 	xfs_rmap_map_extent(tp, ip, whichfork, &new);
5851 	return 0;
5852 }
5853 
5854 static int
5855 xfs_bmap_shift_update_extent(
5856 	struct xfs_trans	*tp,
5857 	struct xfs_inode	*ip,
5858 	int			whichfork,
5859 	struct xfs_iext_cursor	*icur,
5860 	struct xfs_bmbt_irec	*got,
5861 	struct xfs_btree_cur	*cur,
5862 	int			*logflags,
5863 	xfs_fileoff_t		startoff)
5864 {
5865 	struct xfs_mount	*mp = ip->i_mount;
5866 	struct xfs_bmbt_irec	prev = *got;
5867 	int			error, i;
5868 
5869 	*logflags |= XFS_ILOG_CORE;
5870 
5871 	got->br_startoff = startoff;
5872 
5873 	if (cur) {
5874 		error = xfs_bmbt_lookup_eq(cur, &prev, &i);
5875 		if (error)
5876 			return error;
5877 		if (XFS_IS_CORRUPT(mp, i != 1)) {
5878 			xfs_btree_mark_sick(cur);
5879 			return -EFSCORRUPTED;
5880 		}
5881 
5882 		error = xfs_bmbt_update(cur, got);
5883 		if (error)
5884 			return error;
5885 	} else {
5886 		*logflags |= XFS_ILOG_DEXT;
5887 	}
5888 
5889 	xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur,
5890 			got);
5891 
5892 	/* update reverse mapping */
5893 	xfs_rmap_unmap_extent(tp, ip, whichfork, &prev);
5894 	xfs_rmap_map_extent(tp, ip, whichfork, got);
5895 	return 0;
5896 }
5897 
5898 int
5899 xfs_bmap_collapse_extents(
5900 	struct xfs_trans	*tp,
5901 	struct xfs_inode	*ip,
5902 	xfs_fileoff_t		*next_fsb,
5903 	xfs_fileoff_t		offset_shift_fsb,
5904 	bool			*done)
5905 {
5906 	int			whichfork = XFS_DATA_FORK;
5907 	struct xfs_mount	*mp = ip->i_mount;
5908 	struct xfs_ifork	*ifp = xfs_ifork_ptr(ip, whichfork);
5909 	struct xfs_btree_cur	*cur = NULL;
5910 	struct xfs_bmbt_irec	got, prev;
5911 	struct xfs_iext_cursor	icur;
5912 	xfs_fileoff_t		new_startoff;
5913 	int			error = 0;
5914 	int			logflags = 0;
5915 
5916 	if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
5917 	    XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
5918 		xfs_bmap_mark_sick(ip, whichfork);
5919 		return -EFSCORRUPTED;
5920 	}
5921 
5922 	if (xfs_is_shutdown(mp))
5923 		return -EIO;
5924 
5925 	xfs_assert_ilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
5926 
5927 	error = xfs_iread_extents(tp, ip, whichfork);
5928 	if (error)
5929 		return error;
5930 
5931 	if (ifp->if_format == XFS_DINODE_FMT_BTREE)
5932 		cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5933 
5934 	if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) {
5935 		*done = true;
5936 		goto del_cursor;
5937 	}
5938 	if (XFS_IS_CORRUPT(mp, isnullstartblock(got.br_startblock))) {
5939 		xfs_bmap_mark_sick(ip, whichfork);
5940 		error = -EFSCORRUPTED;
5941 		goto del_cursor;
5942 	}
5943 
5944 	new_startoff = got.br_startoff - offset_shift_fsb;
5945 	if (xfs_iext_peek_prev_extent(ifp, &icur, &prev)) {
5946 		if (new_startoff < prev.br_startoff + prev.br_blockcount) {
5947 			error = -EINVAL;
5948 			goto del_cursor;
5949 		}
5950 
5951 		if (xfs_bmse_can_merge(&prev, &got, offset_shift_fsb)) {
5952 			error = xfs_bmse_merge(tp, ip, whichfork,
5953 					offset_shift_fsb, &icur, &got, &prev,
5954 					cur, &logflags);
5955 			if (error)
5956 				goto del_cursor;
5957 			goto done;
5958 		}
5959 	} else {
5960 		if (got.br_startoff < offset_shift_fsb) {
5961 			error = -EINVAL;
5962 			goto del_cursor;
5963 		}
5964 	}
5965 
5966 	error = xfs_bmap_shift_update_extent(tp, ip, whichfork, &icur, &got,
5967 			cur, &logflags, new_startoff);
5968 	if (error)
5969 		goto del_cursor;
5970 
5971 done:
5972 	if (!xfs_iext_next_extent(ifp, &icur, &got)) {
5973 		*done = true;
5974 		goto del_cursor;
5975 	}
5976 
5977 	*next_fsb = got.br_startoff;
5978 del_cursor:
5979 	if (cur)
5980 		xfs_btree_del_cursor(cur, error);
5981 	if (logflags)
5982 		xfs_trans_log_inode(tp, ip, logflags);
5983 	return error;
5984 }
5985 
5986 /* Make sure we won't be right-shifting an extent past the maximum bound. */
5987 int
5988 xfs_bmap_can_insert_extents(
5989 	struct xfs_inode	*ip,
5990 	xfs_fileoff_t		off,
5991 	xfs_fileoff_t		shift)
5992 {
5993 	struct xfs_bmbt_irec	got;
5994 	int			is_empty;
5995 	int			error = 0;
5996 
5997 	xfs_assert_ilocked(ip, XFS_IOLOCK_EXCL);
5998 
5999 	if (xfs_is_shutdown(ip->i_mount))
6000 		return -EIO;
6001 
6002 	xfs_ilock(ip, XFS_ILOCK_EXCL);
6003 	error = xfs_bmap_last_extent(NULL, ip, XFS_DATA_FORK, &got, &is_empty);
6004 	if (!error && !is_empty && got.br_startoff >= off &&
6005 	    ((got.br_startoff + shift) & BMBT_STARTOFF_MASK) < got.br_startoff)
6006 		error = -EINVAL;
6007 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
6008 
6009 	return error;
6010 }
6011 
6012 int
6013 xfs_bmap_insert_extents(
6014 	struct xfs_trans	*tp,
6015 	struct xfs_inode	*ip,
6016 	xfs_fileoff_t		*next_fsb,
6017 	xfs_fileoff_t		offset_shift_fsb,
6018 	bool			*done,
6019 	xfs_fileoff_t		stop_fsb)
6020 {
6021 	int			whichfork = XFS_DATA_FORK;
6022 	struct xfs_mount	*mp = ip->i_mount;
6023 	struct xfs_ifork	*ifp = xfs_ifork_ptr(ip, whichfork);
6024 	struct xfs_btree_cur	*cur = NULL;
6025 	struct xfs_bmbt_irec	got, next;
6026 	struct xfs_iext_cursor	icur;
6027 	xfs_fileoff_t		new_startoff;
6028 	int			error = 0;
6029 	int			logflags = 0;
6030 
6031 	if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
6032 	    XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
6033 		xfs_bmap_mark_sick(ip, whichfork);
6034 		return -EFSCORRUPTED;
6035 	}
6036 
6037 	if (xfs_is_shutdown(mp))
6038 		return -EIO;
6039 
6040 	xfs_assert_ilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
6041 
6042 	error = xfs_iread_extents(tp, ip, whichfork);
6043 	if (error)
6044 		return error;
6045 
6046 	if (ifp->if_format == XFS_DINODE_FMT_BTREE)
6047 		cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
6048 
6049 	if (*next_fsb == NULLFSBLOCK) {
6050 		xfs_iext_last(ifp, &icur);
6051 		if (!xfs_iext_get_extent(ifp, &icur, &got) ||
6052 		    stop_fsb > got.br_startoff) {
6053 			*done = true;
6054 			goto del_cursor;
6055 		}
6056 	} else {
6057 		if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) {
6058 			*done = true;
6059 			goto del_cursor;
6060 		}
6061 	}
6062 	if (XFS_IS_CORRUPT(mp, isnullstartblock(got.br_startblock))) {
6063 		xfs_bmap_mark_sick(ip, whichfork);
6064 		error = -EFSCORRUPTED;
6065 		goto del_cursor;
6066 	}
6067 
6068 	if (XFS_IS_CORRUPT(mp, stop_fsb > got.br_startoff)) {
6069 		xfs_bmap_mark_sick(ip, whichfork);
6070 		error = -EFSCORRUPTED;
6071 		goto del_cursor;
6072 	}
6073 
6074 	new_startoff = got.br_startoff + offset_shift_fsb;
6075 	if (xfs_iext_peek_next_extent(ifp, &icur, &next)) {
6076 		if (new_startoff + got.br_blockcount > next.br_startoff) {
6077 			error = -EINVAL;
6078 			goto del_cursor;
6079 		}
6080 
6081 		/*
6082 		 * Unlike a left shift (which involves a hole punch), a right
6083 		 * shift does not modify extent neighbors in any way.  We should
6084 		 * never find mergeable extents in this scenario.  Check anyways
6085 		 * and warn if we encounter two extents that could be one.
6086 		 */
6087 		if (xfs_bmse_can_merge(&got, &next, offset_shift_fsb))
6088 			WARN_ON_ONCE(1);
6089 	}
6090 
6091 	error = xfs_bmap_shift_update_extent(tp, ip, whichfork, &icur, &got,
6092 			cur, &logflags, new_startoff);
6093 	if (error)
6094 		goto del_cursor;
6095 
6096 	if (!xfs_iext_prev_extent(ifp, &icur, &got) ||
6097 	    stop_fsb >= got.br_startoff + got.br_blockcount) {
6098 		*done = true;
6099 		goto del_cursor;
6100 	}
6101 
6102 	*next_fsb = got.br_startoff;
6103 del_cursor:
6104 	if (cur)
6105 		xfs_btree_del_cursor(cur, error);
6106 	if (logflags)
6107 		xfs_trans_log_inode(tp, ip, logflags);
6108 	return error;
6109 }
6110 
6111 /*
6112  * Splits an extent into two extents at split_fsb block such that it is the
6113  * first block of the current_ext. @ext is a target extent to be split.
6114  * @split_fsb is a block where the extents is split.  If split_fsb lies in a
6115  * hole or the first block of extents, just return 0.
6116  */
6117 int
6118 xfs_bmap_split_extent(
6119 	struct xfs_trans	*tp,
6120 	struct xfs_inode	*ip,
6121 	xfs_fileoff_t		split_fsb)
6122 {
6123 	int				whichfork = XFS_DATA_FORK;
6124 	struct xfs_ifork		*ifp = xfs_ifork_ptr(ip, whichfork);
6125 	struct xfs_btree_cur		*cur = NULL;
6126 	struct xfs_bmbt_irec		got;
6127 	struct xfs_bmbt_irec		new; /* split extent */
6128 	struct xfs_mount		*mp = ip->i_mount;
6129 	xfs_fsblock_t			gotblkcnt; /* new block count for got */
6130 	struct xfs_iext_cursor		icur;
6131 	int				error = 0;
6132 	int				logflags = 0;
6133 	int				i = 0;
6134 
6135 	if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
6136 	    XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
6137 		xfs_bmap_mark_sick(ip, whichfork);
6138 		return -EFSCORRUPTED;
6139 	}
6140 
6141 	if (xfs_is_shutdown(mp))
6142 		return -EIO;
6143 
6144 	/* Read in all the extents */
6145 	error = xfs_iread_extents(tp, ip, whichfork);
6146 	if (error)
6147 		return error;
6148 
6149 	/*
6150 	 * If there are not extents, or split_fsb lies in a hole we are done.
6151 	 */
6152 	if (!xfs_iext_lookup_extent(ip, ifp, split_fsb, &icur, &got) ||
6153 	    got.br_startoff >= split_fsb)
6154 		return 0;
6155 
6156 	gotblkcnt = split_fsb - got.br_startoff;
6157 	new.br_startoff = split_fsb;
6158 	new.br_startblock = got.br_startblock + gotblkcnt;
6159 	new.br_blockcount = got.br_blockcount - gotblkcnt;
6160 	new.br_state = got.br_state;
6161 
6162 	if (ifp->if_format == XFS_DINODE_FMT_BTREE) {
6163 		cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
6164 		error = xfs_bmbt_lookup_eq(cur, &got, &i);
6165 		if (error)
6166 			goto del_cursor;
6167 		if (XFS_IS_CORRUPT(mp, i != 1)) {
6168 			xfs_btree_mark_sick(cur);
6169 			error = -EFSCORRUPTED;
6170 			goto del_cursor;
6171 		}
6172 	}
6173 
6174 	got.br_blockcount = gotblkcnt;
6175 	xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), &icur,
6176 			&got);
6177 
6178 	logflags = XFS_ILOG_CORE;
6179 	if (cur) {
6180 		error = xfs_bmbt_update(cur, &got);
6181 		if (error)
6182 			goto del_cursor;
6183 	} else
6184 		logflags |= XFS_ILOG_DEXT;
6185 
6186 	/* Add new extent */
6187 	xfs_iext_next(ifp, &icur);
6188 	xfs_iext_insert(ip, &icur, &new, 0);
6189 	ifp->if_nextents++;
6190 
6191 	if (cur) {
6192 		error = xfs_bmbt_lookup_eq(cur, &new, &i);
6193 		if (error)
6194 			goto del_cursor;
6195 		if (XFS_IS_CORRUPT(mp, i != 0)) {
6196 			xfs_btree_mark_sick(cur);
6197 			error = -EFSCORRUPTED;
6198 			goto del_cursor;
6199 		}
6200 		error = xfs_btree_insert(cur, &i);
6201 		if (error)
6202 			goto del_cursor;
6203 		if (XFS_IS_CORRUPT(mp, i != 1)) {
6204 			xfs_btree_mark_sick(cur);
6205 			error = -EFSCORRUPTED;
6206 			goto del_cursor;
6207 		}
6208 	}
6209 
6210 	/*
6211 	 * Convert to a btree if necessary.
6212 	 */
6213 	if (xfs_bmap_needs_btree(ip, whichfork)) {
6214 		int tmp_logflags; /* partial log flag return val */
6215 
6216 		ASSERT(cur == NULL);
6217 		error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0,
6218 				&tmp_logflags, whichfork);
6219 		logflags |= tmp_logflags;
6220 	}
6221 
6222 del_cursor:
6223 	if (cur) {
6224 		cur->bc_bmap.allocated = 0;
6225 		xfs_btree_del_cursor(cur, error);
6226 	}
6227 
6228 	if (logflags)
6229 		xfs_trans_log_inode(tp, ip, logflags);
6230 	return error;
6231 }
6232 
6233 /* Record a bmap intent. */
6234 static inline void
6235 __xfs_bmap_add(
6236 	struct xfs_trans		*tp,
6237 	enum xfs_bmap_intent_type	type,
6238 	struct xfs_inode		*ip,
6239 	int				whichfork,
6240 	struct xfs_bmbt_irec		*bmap)
6241 {
6242 	struct xfs_bmap_intent		*bi;
6243 
6244 	if ((whichfork != XFS_DATA_FORK && whichfork != XFS_ATTR_FORK) ||
6245 	    bmap->br_startblock == HOLESTARTBLOCK ||
6246 	    bmap->br_startblock == DELAYSTARTBLOCK)
6247 		return;
6248 
6249 	bi = kmem_cache_alloc(xfs_bmap_intent_cache, GFP_KERNEL | __GFP_NOFAIL);
6250 	INIT_LIST_HEAD(&bi->bi_list);
6251 	bi->bi_type = type;
6252 	bi->bi_owner = ip;
6253 	bi->bi_whichfork = whichfork;
6254 	bi->bi_bmap = *bmap;
6255 
6256 	xfs_bmap_defer_add(tp, bi);
6257 }
6258 
6259 /* Map an extent into a file. */
6260 void
6261 xfs_bmap_map_extent(
6262 	struct xfs_trans	*tp,
6263 	struct xfs_inode	*ip,
6264 	int			whichfork,
6265 	struct xfs_bmbt_irec	*PREV)
6266 {
6267 	__xfs_bmap_add(tp, XFS_BMAP_MAP, ip, whichfork, PREV);
6268 }
6269 
6270 /* Unmap an extent out of a file. */
6271 void
6272 xfs_bmap_unmap_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_UNMAP, ip, whichfork, PREV);
6279 }
6280 
6281 /*
6282  * Process one of the deferred bmap operations.  We pass back the
6283  * btree cursor to maintain our lock on the bmapbt between calls.
6284  */
6285 int
6286 xfs_bmap_finish_one(
6287 	struct xfs_trans		*tp,
6288 	struct xfs_bmap_intent		*bi)
6289 {
6290 	struct xfs_bmbt_irec		*bmap = &bi->bi_bmap;
6291 	int				error = 0;
6292 	int				flags = 0;
6293 
6294 	if (bi->bi_whichfork == XFS_ATTR_FORK)
6295 		flags |= XFS_BMAPI_ATTRFORK;
6296 
6297 	ASSERT(tp->t_highest_agno == NULLAGNUMBER);
6298 
6299 	trace_xfs_bmap_deferred(bi);
6300 
6301 	if (XFS_TEST_ERROR(false, tp->t_mountp, XFS_ERRTAG_BMAP_FINISH_ONE))
6302 		return -EIO;
6303 
6304 	switch (bi->bi_type) {
6305 	case XFS_BMAP_MAP:
6306 		if (bi->bi_bmap.br_state == XFS_EXT_UNWRITTEN)
6307 			flags |= XFS_BMAPI_PREALLOC;
6308 		error = xfs_bmapi_remap(tp, bi->bi_owner, bmap->br_startoff,
6309 				bmap->br_blockcount, bmap->br_startblock,
6310 				flags);
6311 		bmap->br_blockcount = 0;
6312 		break;
6313 	case XFS_BMAP_UNMAP:
6314 		error = __xfs_bunmapi(tp, bi->bi_owner, bmap->br_startoff,
6315 				&bmap->br_blockcount, flags | XFS_BMAPI_REMAP,
6316 				1);
6317 		break;
6318 	default:
6319 		ASSERT(0);
6320 		xfs_bmap_mark_sick(bi->bi_owner, bi->bi_whichfork);
6321 		error = -EFSCORRUPTED;
6322 	}
6323 
6324 	return error;
6325 }
6326 
6327 /* Check that an extent does not have invalid flags or bad ranges. */
6328 xfs_failaddr_t
6329 xfs_bmap_validate_extent_raw(
6330 	struct xfs_mount	*mp,
6331 	bool			rtfile,
6332 	int			whichfork,
6333 	struct xfs_bmbt_irec	*irec)
6334 {
6335 	if (!xfs_verify_fileext(mp, irec->br_startoff, irec->br_blockcount))
6336 		return __this_address;
6337 
6338 	if (rtfile && whichfork == XFS_DATA_FORK) {
6339 		if (!xfs_verify_rtbext(mp, irec->br_startblock,
6340 					   irec->br_blockcount))
6341 			return __this_address;
6342 	} else {
6343 		if (!xfs_verify_fsbext(mp, irec->br_startblock,
6344 					   irec->br_blockcount))
6345 			return __this_address;
6346 	}
6347 	if (irec->br_state != XFS_EXT_NORM && whichfork != XFS_DATA_FORK)
6348 		return __this_address;
6349 	return NULL;
6350 }
6351 
6352 int __init
6353 xfs_bmap_intent_init_cache(void)
6354 {
6355 	xfs_bmap_intent_cache = kmem_cache_create("xfs_bmap_intent",
6356 			sizeof(struct xfs_bmap_intent),
6357 			0, 0, NULL);
6358 
6359 	return xfs_bmap_intent_cache != NULL ? 0 : -ENOMEM;
6360 }
6361 
6362 void
6363 xfs_bmap_intent_destroy_cache(void)
6364 {
6365 	kmem_cache_destroy(xfs_bmap_intent_cache);
6366 	xfs_bmap_intent_cache = NULL;
6367 }
6368 
6369 /* Check that an inode's extent does not have invalid flags or bad ranges. */
6370 xfs_failaddr_t
6371 xfs_bmap_validate_extent(
6372 	struct xfs_inode	*ip,
6373 	int			whichfork,
6374 	struct xfs_bmbt_irec	*irec)
6375 {
6376 	return xfs_bmap_validate_extent_raw(ip->i_mount,
6377 			XFS_IS_REALTIME_INODE(ip), whichfork, irec);
6378 }
6379 
6380 /*
6381  * Used in xfs_itruncate_extents().  This is the maximum number of extents
6382  * freed from a file in a single transaction.
6383  */
6384 #define	XFS_ITRUNC_MAX_EXTENTS	2
6385 
6386 /*
6387  * Unmap every extent in part of an inode's fork.  We don't do any higher level
6388  * invalidation work at all.
6389  */
6390 int
6391 xfs_bunmapi_range(
6392 	struct xfs_trans	**tpp,
6393 	struct xfs_inode	*ip,
6394 	uint32_t		flags,
6395 	xfs_fileoff_t		startoff,
6396 	xfs_fileoff_t		endoff)
6397 {
6398 	xfs_filblks_t		unmap_len = endoff - startoff + 1;
6399 	int			error = 0;
6400 
6401 	xfs_assert_ilocked(ip, XFS_ILOCK_EXCL);
6402 
6403 	while (unmap_len > 0) {
6404 		ASSERT((*tpp)->t_highest_agno == NULLAGNUMBER);
6405 		error = __xfs_bunmapi(*tpp, ip, startoff, &unmap_len, flags,
6406 				XFS_ITRUNC_MAX_EXTENTS);
6407 		if (error)
6408 			goto out;
6409 
6410 		/* free the just unmapped extents */
6411 		error = xfs_defer_finish(tpp);
6412 		if (error)
6413 			goto out;
6414 		cond_resched();
6415 	}
6416 out:
6417 	return error;
6418 }
6419 
6420 struct xfs_bmap_query_range {
6421 	xfs_bmap_query_range_fn	fn;
6422 	void			*priv;
6423 };
6424 
6425 /* Format btree record and pass to our callback. */
6426 STATIC int
6427 xfs_bmap_query_range_helper(
6428 	struct xfs_btree_cur		*cur,
6429 	const union xfs_btree_rec	*rec,
6430 	void				*priv)
6431 {
6432 	struct xfs_bmap_query_range	*query = priv;
6433 	struct xfs_bmbt_irec		irec;
6434 	xfs_failaddr_t			fa;
6435 
6436 	xfs_bmbt_disk_get_all(&rec->bmbt, &irec);
6437 	fa = xfs_bmap_validate_extent(cur->bc_ino.ip, cur->bc_ino.whichfork,
6438 			&irec);
6439 	if (fa) {
6440 		xfs_btree_mark_sick(cur);
6441 		return xfs_bmap_complain_bad_rec(cur->bc_ino.ip,
6442 				cur->bc_ino.whichfork, fa, &irec);
6443 	}
6444 
6445 	return query->fn(cur, &irec, query->priv);
6446 }
6447 
6448 /* Find all bmaps. */
6449 int
6450 xfs_bmap_query_all(
6451 	struct xfs_btree_cur		*cur,
6452 	xfs_bmap_query_range_fn		fn,
6453 	void				*priv)
6454 {
6455 	struct xfs_bmap_query_range	query = {
6456 		.priv			= priv,
6457 		.fn			= fn,
6458 	};
6459 
6460 	return xfs_btree_query_all(cur, xfs_bmap_query_range_helper, &query);
6461 }
6462 
6463 /* Helper function to extract extent size hint from inode */
6464 xfs_extlen_t
6465 xfs_get_extsz_hint(
6466 	struct xfs_inode	*ip)
6467 {
6468 	/*
6469 	 * No point in aligning allocations if we need to COW to actually
6470 	 * write to them.
6471 	 */
6472 	if (xfs_is_always_cow_inode(ip))
6473 		return 0;
6474 	if ((ip->i_diflags & XFS_DIFLAG_EXTSIZE) && ip->i_extsize)
6475 		return ip->i_extsize;
6476 	if (XFS_IS_REALTIME_INODE(ip) &&
6477 	    ip->i_mount->m_sb.sb_rextsize > 1)
6478 		return ip->i_mount->m_sb.sb_rextsize;
6479 	return 0;
6480 }
6481 
6482 /*
6483  * Helper function to extract CoW extent size hint from inode.
6484  * Between the extent size hint and the CoW extent size hint, we
6485  * return the greater of the two.  If the value is zero (automatic),
6486  * use the default size.
6487  */
6488 xfs_extlen_t
6489 xfs_get_cowextsz_hint(
6490 	struct xfs_inode	*ip)
6491 {
6492 	xfs_extlen_t		a, b;
6493 
6494 	a = 0;
6495 	if (ip->i_diflags2 & XFS_DIFLAG2_COWEXTSIZE)
6496 		a = ip->i_cowextsize;
6497 	b = xfs_get_extsz_hint(ip);
6498 
6499 	a = max(a, b);
6500 	if (a == 0)
6501 		return XFS_DEFAULT_COWEXTSZ_HINT;
6502 	return a;
6503 }
6504