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