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