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