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