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