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