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