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