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