PageRenderTime 369ms CodeModel.GetById 6ms app.highlight 321ms RepoModel.GetById 1ms app.codeStats 1ms

/fs/xfs/xfs_bmap.c

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