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