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