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