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