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