xref: /illumos-gate/usr/src/uts/common/fs/ufs/ufs_bmap.c (revision 33c22cb3)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*33c22cb3Smishra  * Common Development and Distribution License (the "License").
6*33c22cb3Smishra  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*33c22cb3Smishra  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
277c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
317c478bd9Sstevel@tonic-gate  * The Regents of the University of California
327c478bd9Sstevel@tonic-gate  * All Rights Reserved
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
357c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
367c478bd9Sstevel@tonic-gate  * contributors.
377c478bd9Sstevel@tonic-gate  */
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #include <sys/types.h>
437c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
447c478bd9Sstevel@tonic-gate #include <sys/param.h>
457c478bd9Sstevel@tonic-gate #include <sys/systm.h>
467c478bd9Sstevel@tonic-gate #include <sys/signal.h>
477c478bd9Sstevel@tonic-gate #include <sys/user.h>
487c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
497c478bd9Sstevel@tonic-gate #include <sys/buf.h>
507c478bd9Sstevel@tonic-gate #include <sys/disp.h>
517c478bd9Sstevel@tonic-gate #include <sys/proc.h>
527c478bd9Sstevel@tonic-gate #include <sys/conf.h>
537c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_inode.h>
547c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_fs.h>
557c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_quota.h>
567c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_trans.h>
577c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_bio.h>
587c478bd9Sstevel@tonic-gate #include <vm/seg.h>
597c478bd9Sstevel@tonic-gate #include <sys/errno.h>
607c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
617c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
627c478bd9Sstevel@tonic-gate #include <sys/debug.h>
637c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
64303bf60bSsdebnath #include <sys/cmn_err.h>
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate /*
677c478bd9Sstevel@tonic-gate  * This structure is used to track blocks as we allocate them, so that
687c478bd9Sstevel@tonic-gate  * we can free them if we encounter an error during allocation.  We
697c478bd9Sstevel@tonic-gate  * keep track of five pieces of information for each allocated block:
707c478bd9Sstevel@tonic-gate  *   - The number of the newly allocated block
717c478bd9Sstevel@tonic-gate  *   - The size of the block (lets us deal with fragments if we want)
727c478bd9Sstevel@tonic-gate  *   - The number of the block containing a pointer to it; or whether
737c478bd9Sstevel@tonic-gate  *     the pointer is in the inode
747c478bd9Sstevel@tonic-gate  *   - The offset within the block (or inode) containing a pointer to it.
757c478bd9Sstevel@tonic-gate  *   - A flag indicating the usage of the block.  (Logging needs to know
767c478bd9Sstevel@tonic-gate  *     this to avoid overwriting a data block if it was previously used
777c478bd9Sstevel@tonic-gate  *     for metadata.)
787c478bd9Sstevel@tonic-gate  */
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate enum ufs_owner_type {
817c478bd9Sstevel@tonic-gate 	ufs_no_owner,		/* Owner has not yet been updated */
827c478bd9Sstevel@tonic-gate 	ufs_inode_direct,	/* Listed in inode's direct block table */
837c478bd9Sstevel@tonic-gate 	ufs_inode_indirect,	/* Listed in inode's indirect block table */
847c478bd9Sstevel@tonic-gate 	ufs_indirect_block	/* Listed in an indirect block */
857c478bd9Sstevel@tonic-gate };
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate struct ufs_allocated_block {
887c478bd9Sstevel@tonic-gate 	daddr_t this_block;	    /* Number of this block */
897c478bd9Sstevel@tonic-gate 	off_t block_size;	    /* Size of this block, in bytes */
907c478bd9Sstevel@tonic-gate 	enum ufs_owner_type owner;  /* Who points to this block? */
917c478bd9Sstevel@tonic-gate 	daddr_t owner_block;	    /* Number of the owning block */
927c478bd9Sstevel@tonic-gate 	uint_t owner_offset;	    /* Offset within that block or inode */
937c478bd9Sstevel@tonic-gate 	int usage_flags;	    /* Usage flags, as expected by free() */
947c478bd9Sstevel@tonic-gate };
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate static int findextent(struct fs *fs, daddr32_t *sbp, int n, int *lenp,
987c478bd9Sstevel@tonic-gate 		int maxtrans);
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate static void ufs_undo_allocation(inode_t *ip, int block_count,
1017c478bd9Sstevel@tonic-gate 	struct ufs_allocated_block table[], int inode_sector_adjust);
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate /*
1047c478bd9Sstevel@tonic-gate  * Find the extent and the matching block number.
1057c478bd9Sstevel@tonic-gate  *
1067c478bd9Sstevel@tonic-gate  * bsize > PAGESIZE
1077c478bd9Sstevel@tonic-gate  *	boff indicates that we want a page in the middle
1087c478bd9Sstevel@tonic-gate  *	min expression is supposed to make sure no extra page[s] after EOF
1097c478bd9Sstevel@tonic-gate  * PAGESIZE >= bsize
1107c478bd9Sstevel@tonic-gate  *	we assume that a page is a multiple of bsize, i.e.,
1117c478bd9Sstevel@tonic-gate  *	boff always == 0
1127c478bd9Sstevel@tonic-gate  *
1137c478bd9Sstevel@tonic-gate  * We always return a length that is suitable for a disk transfer.
1147c478bd9Sstevel@tonic-gate  */
1157c478bd9Sstevel@tonic-gate #define	DOEXTENT(fs, lbn, boff, bnp, lenp, size, tblp, n, chkfrag, maxtrans) {\
1167c478bd9Sstevel@tonic-gate 	register daddr32_t *dp = (tblp);				\
1177c478bd9Sstevel@tonic-gate 	register int _chkfrag = chkfrag; /* for lint. sigh */		\
1187c478bd9Sstevel@tonic-gate 									\
1197c478bd9Sstevel@tonic-gate 	if (*dp == 0) {							\
1207c478bd9Sstevel@tonic-gate 		*(bnp) = UFS_HOLE;					\
1217c478bd9Sstevel@tonic-gate 	} else {							\
1227c478bd9Sstevel@tonic-gate 		register int len;					\
1237c478bd9Sstevel@tonic-gate 									\
1247c478bd9Sstevel@tonic-gate 		len = findextent(fs, dp, (int)(n), lenp, maxtrans) << 	\
1257c478bd9Sstevel@tonic-gate 			(fs)->fs_bshift; 				\
1267c478bd9Sstevel@tonic-gate 		if (_chkfrag) {						\
1277c478bd9Sstevel@tonic-gate 			register u_offset_t tmp;			\
1287c478bd9Sstevel@tonic-gate 									\
1297c478bd9Sstevel@tonic-gate 			tmp = fragroundup((fs), size) -			\
1307c478bd9Sstevel@tonic-gate 			    (((u_offset_t)lbn) << fs->fs_bshift);	\
1317c478bd9Sstevel@tonic-gate 			len = (int)MIN(tmp, len);			\
1327c478bd9Sstevel@tonic-gate 		}							\
1337c478bd9Sstevel@tonic-gate 		len -= (boff);						\
1347c478bd9Sstevel@tonic-gate 		if (len <= 0) {						\
1357c478bd9Sstevel@tonic-gate 			*(bnp) = UFS_HOLE;				\
1367c478bd9Sstevel@tonic-gate 		} else {						\
1377c478bd9Sstevel@tonic-gate 			*(bnp) = fsbtodb(fs, *dp) + btodb(boff);	\
1387c478bd9Sstevel@tonic-gate 			*(lenp) = len;					\
1397c478bd9Sstevel@tonic-gate 		}							\
1407c478bd9Sstevel@tonic-gate 	}								\
1417c478bd9Sstevel@tonic-gate }
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate /*
1447c478bd9Sstevel@tonic-gate  * The maximum supported file size is actually somewhat less that 1
1457c478bd9Sstevel@tonic-gate  * terabyte.  This is because the total number of blocks used for the
1467c478bd9Sstevel@tonic-gate  * file and its metadata must fit into the ic_blocks field of the
1477c478bd9Sstevel@tonic-gate  * inode, which is a signed 32-bit quantity.  The metadata allocated
1487c478bd9Sstevel@tonic-gate  * for a file (that is, the single, double, and triple indirect blocks
1497c478bd9Sstevel@tonic-gate  * used to reference the file blocks) is actually quite small,
1507c478bd9Sstevel@tonic-gate  * but just to make sure, we check for overflow in the ic_blocks
1517c478bd9Sstevel@tonic-gate  * ic_blocks fields for all files whose total block count is
1527c478bd9Sstevel@tonic-gate  * within 1 GB of a terabyte.  VERYLARGEFILESIZE below is the number of
1537c478bd9Sstevel@tonic-gate  * 512-byte blocks in a terabyte (2^31), minus the number of 512-byte blocks
1547c478bd9Sstevel@tonic-gate  * in a gigabyte (2^21).  We only check for overflow in the ic_blocks
1557c478bd9Sstevel@tonic-gate  * field if the number of blocks currently allocated to the file is
1567c478bd9Sstevel@tonic-gate  * greater than VERYLARGEFILESIZE.
1577c478bd9Sstevel@tonic-gate  *
1587c478bd9Sstevel@tonic-gate  * Note that file "size" is the not the same as file "length".  A
1597c478bd9Sstevel@tonic-gate  * file's "size" is the number of blocks allocated to it.  A file's
1607c478bd9Sstevel@tonic-gate  * "length" is the maximum offset in the file.  A UFS FILE can have a
1617c478bd9Sstevel@tonic-gate  * length of a terabyte, but the size is limited to somewhat less than
1627c478bd9Sstevel@tonic-gate  * a terabyte, as described above.
1637c478bd9Sstevel@tonic-gate  */
1647c478bd9Sstevel@tonic-gate #define	VERYLARGEFILESIZE	0x7FE00000
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate /*
167303bf60bSsdebnath  * bmap{read,write} define the structure of file system storage by mapping
1687c478bd9Sstevel@tonic-gate  * a logical offset in a file to a physical block number on the device.
1697c478bd9Sstevel@tonic-gate  * It should be called with a locked inode when allocation is to be
170303bf60bSsdebnath  * done (bmap_write).  Note this strangeness: bmap_write is always called from
1717c478bd9Sstevel@tonic-gate  * getpage(), not putpage(), since getpage() is where all the allocation
1727c478bd9Sstevel@tonic-gate  * is done.
1737c478bd9Sstevel@tonic-gate  *
174303bf60bSsdebnath  * S_READ, S_OTHER -> bmap_read; S_WRITE -> bmap_write.
1757c478bd9Sstevel@tonic-gate  *
1767c478bd9Sstevel@tonic-gate  * NOTICE: the block number returned is the disk block number, not the
1777c478bd9Sstevel@tonic-gate  * file system block number.  All the worries about block offsets and
1787c478bd9Sstevel@tonic-gate  * page/block sizes are hidden inside of bmap.  Well, not quite,
1797c478bd9Sstevel@tonic-gate  * unfortunately.  It's impossible to find one place to hide all this
1807c478bd9Sstevel@tonic-gate  * mess.  There are 3 cases:
1817c478bd9Sstevel@tonic-gate  *
1827c478bd9Sstevel@tonic-gate  * PAGESIZE < bsize
1837c478bd9Sstevel@tonic-gate  *	In this case, the {get,put}page routines will attempt to align to
1847c478bd9Sstevel@tonic-gate  *	a file system block boundry (XXX - maybe this is a mistake?).  Since
1857c478bd9Sstevel@tonic-gate  *	the kluster routines may be out of memory, we don't always get all
1867c478bd9Sstevel@tonic-gate  *	the pages we wanted.  If we called bmap first, to find out how much
1877c478bd9Sstevel@tonic-gate  *	to kluster, we handed in the block aligned offset.  If we didn't get
1887c478bd9Sstevel@tonic-gate  *	all the pages, we have to chop off the amount we didn't get from the
1897c478bd9Sstevel@tonic-gate  *	amount handed back by bmap.
1907c478bd9Sstevel@tonic-gate  *
1917c478bd9Sstevel@tonic-gate  * PAGESIZE == bsize
1927c478bd9Sstevel@tonic-gate  *	Life is quite pleasant here, no extra work needed, mainly because we
1937c478bd9Sstevel@tonic-gate  *	(probably?) won't kluster backwards, just forwards.
1947c478bd9Sstevel@tonic-gate  *
1957c478bd9Sstevel@tonic-gate  * PAGESIZE > bsize
1967c478bd9Sstevel@tonic-gate  *	This one has a different set of problems, specifically, we may have to
1977c478bd9Sstevel@tonic-gate  *	do N reads to fill one page.  Let us hope that Sun will stay with small
1987c478bd9Sstevel@tonic-gate  *	pages.
1997c478bd9Sstevel@tonic-gate  *
2007c478bd9Sstevel@tonic-gate  * Returns 0 on success, or a non-zero errno if an error occurs.
2017c478bd9Sstevel@tonic-gate  *
2027c478bd9Sstevel@tonic-gate  * TODO
2037c478bd9Sstevel@tonic-gate  *	LMXXX - add a bmap cache.  This could be a couple of extents in the
2047c478bd9Sstevel@tonic-gate  *	inode.  Two is nice for PAGESIZE > bsize.
2057c478bd9Sstevel@tonic-gate  */
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate int
2087c478bd9Sstevel@tonic-gate bmap_read(struct inode *ip, u_offset_t off, daddr_t *bnp, int *lenp)
2097c478bd9Sstevel@tonic-gate {
2107c478bd9Sstevel@tonic-gate 	daddr_t lbn;
2117c478bd9Sstevel@tonic-gate 	ufsvfs_t *ufsvfsp = ip->i_ufsvfs;
2127c478bd9Sstevel@tonic-gate 	struct	fs *fs = ufsvfsp->vfs_fs;
2137c478bd9Sstevel@tonic-gate 	struct	buf *bp;
2147c478bd9Sstevel@tonic-gate 	int	i, j, boff;
2157c478bd9Sstevel@tonic-gate 	int	shft;			/* we maintain sh = 1 << shft */
2167c478bd9Sstevel@tonic-gate 	daddr_t	ob, nb, tbn;
2177c478bd9Sstevel@tonic-gate 	daddr32_t *bap;
2187c478bd9Sstevel@tonic-gate 	int	nindirshift, nindiroffset;
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	ASSERT(RW_LOCK_HELD(&ip->i_contents));
2217c478bd9Sstevel@tonic-gate 	lbn = (daddr_t)lblkno(fs, off);
2227c478bd9Sstevel@tonic-gate 	boff = (int)blkoff(fs, off);
2237c478bd9Sstevel@tonic-gate 	if (lbn < 0)
2247c478bd9Sstevel@tonic-gate 		return (EFBIG);
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 	/*
2277c478bd9Sstevel@tonic-gate 	 * The first NDADDR blocks are direct blocks.
2287c478bd9Sstevel@tonic-gate 	 */
2297c478bd9Sstevel@tonic-gate 	if (lbn < NDADDR) {
2307c478bd9Sstevel@tonic-gate 		DOEXTENT(fs, lbn, boff, bnp, lenp,
2317c478bd9Sstevel@tonic-gate 		    ip->i_size, &ip->i_db[lbn], NDADDR - lbn, 1,
2327c478bd9Sstevel@tonic-gate 			ufsvfsp->vfs_iotransz);
2337c478bd9Sstevel@tonic-gate 		return (0);
2347c478bd9Sstevel@tonic-gate 	}
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 	nindirshift = ufsvfsp->vfs_nindirshift;
2377c478bd9Sstevel@tonic-gate 	nindiroffset = ufsvfsp->vfs_nindiroffset;
2387c478bd9Sstevel@tonic-gate 	/*
2397c478bd9Sstevel@tonic-gate 	 * Determine how many levels of indirection.
2407c478bd9Sstevel@tonic-gate 	 */
2417c478bd9Sstevel@tonic-gate 	shft = 0;				/* sh = 1 */
2427c478bd9Sstevel@tonic-gate 	tbn = lbn - NDADDR;
2437c478bd9Sstevel@tonic-gate 	for (j = NIADDR; j > 0; j--) {
2447c478bd9Sstevel@tonic-gate 		longlong_t	sh;
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 		shft += nindirshift;		/* sh *= nindir */
2477c478bd9Sstevel@tonic-gate 		sh = 1LL << shft;
2487c478bd9Sstevel@tonic-gate 		if (tbn < sh)
2497c478bd9Sstevel@tonic-gate 			break;
2507c478bd9Sstevel@tonic-gate 		tbn -= sh;
2517c478bd9Sstevel@tonic-gate 	}
2527c478bd9Sstevel@tonic-gate 	if (j == 0)
2537c478bd9Sstevel@tonic-gate 		return (EFBIG);
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 	/*
2567c478bd9Sstevel@tonic-gate 	 * Fetch the first indirect block.
2577c478bd9Sstevel@tonic-gate 	 */
2587c478bd9Sstevel@tonic-gate 	nb = ip->i_ib[NIADDR - j];
2597c478bd9Sstevel@tonic-gate 	if (nb == 0) {
2607c478bd9Sstevel@tonic-gate 		*bnp = UFS_HOLE;
2617c478bd9Sstevel@tonic-gate 		return (0);
2627c478bd9Sstevel@tonic-gate 	}
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 	/*
2657c478bd9Sstevel@tonic-gate 	 * Fetch through the indirect blocks.
2667c478bd9Sstevel@tonic-gate 	 */
2677c478bd9Sstevel@tonic-gate 	for (; j <= NIADDR; j++) {
2687c478bd9Sstevel@tonic-gate 		ob = nb;
2697c478bd9Sstevel@tonic-gate 		bp = UFS_BREAD(ufsvfsp,
2707c478bd9Sstevel@tonic-gate 				ip->i_dev, fsbtodb(fs, ob), fs->fs_bsize);
2717c478bd9Sstevel@tonic-gate 		if (bp->b_flags & B_ERROR) {
2727c478bd9Sstevel@tonic-gate 			brelse(bp);
2737c478bd9Sstevel@tonic-gate 			return (EIO);
2747c478bd9Sstevel@tonic-gate 		}
2757c478bd9Sstevel@tonic-gate 		bap = bp->b_un.b_daddr;
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 		ASSERT(!ufs_indir_badblock(ip, bap));
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 		shft -= nindirshift;		/* sh / nindir */
2807c478bd9Sstevel@tonic-gate 		i = (tbn >> shft) & nindiroffset; /* (tbn / sh) % nindir */
2817c478bd9Sstevel@tonic-gate 		nb = bap[i];
2827c478bd9Sstevel@tonic-gate 		if (nb == 0) {
2837c478bd9Sstevel@tonic-gate 			*bnp = UFS_HOLE;
2847c478bd9Sstevel@tonic-gate 			brelse(bp);
2857c478bd9Sstevel@tonic-gate 			return (0);
2867c478bd9Sstevel@tonic-gate 		}
2877c478bd9Sstevel@tonic-gate 		if (j != NIADDR)
2887c478bd9Sstevel@tonic-gate 			brelse(bp);
2897c478bd9Sstevel@tonic-gate 	}
2907c478bd9Sstevel@tonic-gate 	DOEXTENT(fs, lbn, boff, bnp, lenp, ip->i_size, &bap[i],
2917c478bd9Sstevel@tonic-gate 	    MIN(NINDIR(fs) - i, (daddr_t)lblkno(fs, ip->i_size - 1) - lbn + 1),
2927c478bd9Sstevel@tonic-gate 		0, ufsvfsp->vfs_iotransz);
2937c478bd9Sstevel@tonic-gate 	brelse(bp);
2947c478bd9Sstevel@tonic-gate 	return (0);
2957c478bd9Sstevel@tonic-gate }
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate /*
298303bf60bSsdebnath  * See bmap_read for general notes.
2997c478bd9Sstevel@tonic-gate  *
3007c478bd9Sstevel@tonic-gate  * The block must be at least size bytes and will be extended or
301303bf60bSsdebnath  * allocated as needed.  If alloc_type is of type BI_ALLOC_ONLY, then bmap
302303bf60bSsdebnath  * will not create any in-core pages that correspond to the new disk allocation.
303303bf60bSsdebnath  * If alloc_type is of BI_FALLOCATE, blocks will be stored as (-1) * block addr
304303bf60bSsdebnath  * and security is maintained b/c upon reading a negative block number pages
305303bf60bSsdebnath  * are zeroed. For all other allocation types (BI_NORMAL) the in-core pages will
306303bf60bSsdebnath  * be created and initialized as needed.
3077c478bd9Sstevel@tonic-gate  *
3087c478bd9Sstevel@tonic-gate  * Returns 0 on success, or a non-zero errno if an error occurs.
3097c478bd9Sstevel@tonic-gate  */
3107c478bd9Sstevel@tonic-gate int
311303bf60bSsdebnath bmap_write(struct inode	*ip, u_offset_t	off, int size,
312303bf60bSsdebnath     enum bi_type alloc_type, daddr_t *allocblk, struct cred *cr)
3137c478bd9Sstevel@tonic-gate {
3147c478bd9Sstevel@tonic-gate 	struct	fs *fs;
3157c478bd9Sstevel@tonic-gate 	struct	buf *bp;
3167c478bd9Sstevel@tonic-gate 	int	i;
3177c478bd9Sstevel@tonic-gate 	struct	buf *nbp;
3187c478bd9Sstevel@tonic-gate 	int	j;
3197c478bd9Sstevel@tonic-gate 	int	shft;				/* we maintain sh = 1 << shft */
3207c478bd9Sstevel@tonic-gate 	daddr_t	ob, nb, pref, lbn, llbn, tbn;
3217c478bd9Sstevel@tonic-gate 	daddr32_t *bap;
3227c478bd9Sstevel@tonic-gate 	struct	vnode *vp = ITOV(ip);
3237c478bd9Sstevel@tonic-gate 	long	bsize = VBSIZE(vp);
3247c478bd9Sstevel@tonic-gate 	long	osize, nsize;
3257c478bd9Sstevel@tonic-gate 	int	issync, metaflag, isdirquota;
3267c478bd9Sstevel@tonic-gate 	int	err;
3277c478bd9Sstevel@tonic-gate 	dev_t	dev;
3287c478bd9Sstevel@tonic-gate 	struct	fbuf *fbp;
3297c478bd9Sstevel@tonic-gate 	int	nindirshift;
3307c478bd9Sstevel@tonic-gate 	int	nindiroffset;
3317c478bd9Sstevel@tonic-gate 	struct	ufsvfs	*ufsvfsp;
3327c478bd9Sstevel@tonic-gate 	int	added_sectors;		/* sectors added to this inode */
3337c478bd9Sstevel@tonic-gate 	int	alloced_blocks;		/* fs blocks newly allocated */
3347c478bd9Sstevel@tonic-gate 	struct  ufs_allocated_block undo_table[NIADDR+1];
3357c478bd9Sstevel@tonic-gate 	int	verylargefile = 0;
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 	ASSERT(RW_WRITE_HELD(&ip->i_contents));
3387c478bd9Sstevel@tonic-gate 
339303bf60bSsdebnath 	if (allocblk)
340303bf60bSsdebnath 		*allocblk = 0;
341303bf60bSsdebnath 
3427c478bd9Sstevel@tonic-gate 	ufsvfsp = ip->i_ufsvfs;
3437c478bd9Sstevel@tonic-gate 	fs = ufsvfsp->vfs_bufp->b_un.b_fs;
3447c478bd9Sstevel@tonic-gate 	lbn = (daddr_t)lblkno(fs, off);
3457c478bd9Sstevel@tonic-gate 	if (lbn < 0)
3467c478bd9Sstevel@tonic-gate 		return (EFBIG);
3477c478bd9Sstevel@tonic-gate 	if (ip->i_blocks >= VERYLARGEFILESIZE)
3487c478bd9Sstevel@tonic-gate 		verylargefile = 1;
3497c478bd9Sstevel@tonic-gate 	llbn = (daddr_t)((ip->i_size) ? lblkno(fs, ip->i_size - 1) : 0);
3507c478bd9Sstevel@tonic-gate 	metaflag = isdirquota = 0;
3517c478bd9Sstevel@tonic-gate 	if (((ip->i_mode & IFMT) == IFDIR) ||
3527c478bd9Sstevel@tonic-gate 	    ((ip->i_mode & IFMT) == IFATTRDIR))
3537c478bd9Sstevel@tonic-gate 		isdirquota = metaflag = I_DIR;
3547c478bd9Sstevel@tonic-gate 	else if ((ip->i_mode & IFMT) == IFSHAD)
3557c478bd9Sstevel@tonic-gate 		metaflag = I_SHAD;
3567c478bd9Sstevel@tonic-gate 	else if (ip->i_ufsvfs->vfs_qinod == ip)
3577c478bd9Sstevel@tonic-gate 		isdirquota = metaflag = I_QUOTA;
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 	issync = ((ip->i_flag & ISYNC) != 0);
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 	if (isdirquota || issync) {
362303bf60bSsdebnath 		alloc_type = BI_NORMAL;	/* make sure */
3637c478bd9Sstevel@tonic-gate 	}
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 	/*
3667c478bd9Sstevel@tonic-gate 	 * If the next write will extend the file into a new block,
3677c478bd9Sstevel@tonic-gate 	 * and the file is currently composed of a fragment
3687c478bd9Sstevel@tonic-gate 	 * this fragment has to be extended to be a full block.
3697c478bd9Sstevel@tonic-gate 	 */
3707c478bd9Sstevel@tonic-gate 	if (llbn < NDADDR && llbn < lbn && (ob = ip->i_db[llbn]) != 0) {
3717c478bd9Sstevel@tonic-gate 		osize = blksize(fs, ip, llbn);
3727c478bd9Sstevel@tonic-gate 		if (osize < bsize && osize > 0) {
3737c478bd9Sstevel@tonic-gate 			/*
3747c478bd9Sstevel@tonic-gate 			 * Check to see if doing this will make the file too
3757c478bd9Sstevel@tonic-gate 			 * big.  Only check if we are dealing with a very
3767c478bd9Sstevel@tonic-gate 			 * large file.
3777c478bd9Sstevel@tonic-gate 			 */
3787c478bd9Sstevel@tonic-gate 			if (verylargefile == 1) {
3797c478bd9Sstevel@tonic-gate 				if (((unsigned)ip->i_blocks +
3807c478bd9Sstevel@tonic-gate 				    btodb(bsize - osize)) > INT_MAX) {
3817c478bd9Sstevel@tonic-gate 					return (EFBIG);
3827c478bd9Sstevel@tonic-gate 				}
3837c478bd9Sstevel@tonic-gate 			}
3847c478bd9Sstevel@tonic-gate 			/*
3857c478bd9Sstevel@tonic-gate 			 * Make sure we have all needed pages setup correctly.
3867c478bd9Sstevel@tonic-gate 			 *
3877c478bd9Sstevel@tonic-gate 			 * We pass S_OTHER to fbread here because we want
3887c478bd9Sstevel@tonic-gate 			 * an exclusive lock on the page in question
3897c478bd9Sstevel@tonic-gate 			 * (see ufs_getpage). I/O to the old block location
3907c478bd9Sstevel@tonic-gate 			 * may still be in progress and we are about to free
3917c478bd9Sstevel@tonic-gate 			 * the old block. We don't want anyone else to get
3927c478bd9Sstevel@tonic-gate 			 * a hold of the old block once we free it until
3937c478bd9Sstevel@tonic-gate 			 * the I/O is complete.
3947c478bd9Sstevel@tonic-gate 			 */
3957c478bd9Sstevel@tonic-gate 			err = fbread(ITOV(ip),
3967c478bd9Sstevel@tonic-gate 				    ((offset_t)llbn << fs->fs_bshift),
3977c478bd9Sstevel@tonic-gate 					(uint_t)bsize, S_OTHER, &fbp);
3987c478bd9Sstevel@tonic-gate 			if (err)
3997c478bd9Sstevel@tonic-gate 				return (err);
4007c478bd9Sstevel@tonic-gate 			pref = blkpref(ip, llbn, (int)llbn, &ip->i_db[0]);
4017c478bd9Sstevel@tonic-gate 			err = realloccg(ip, ob, pref, (int)osize, (int)bsize,
4027c478bd9Sstevel@tonic-gate 					&nb, cr);
4037c478bd9Sstevel@tonic-gate 			if (err) {
4047c478bd9Sstevel@tonic-gate 				if (fbp)
4057c478bd9Sstevel@tonic-gate 					fbrelse(fbp, S_OTHER);
4067c478bd9Sstevel@tonic-gate 				return (err);
4077c478bd9Sstevel@tonic-gate 			}
4087c478bd9Sstevel@tonic-gate 			ASSERT(!ufs_badblock(ip, nb));
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 			/*
4117c478bd9Sstevel@tonic-gate 			 * Update the inode before releasing the
4127c478bd9Sstevel@tonic-gate 			 * lock on the page. If we released the page
4137c478bd9Sstevel@tonic-gate 			 * lock first, the data could be written to it's
4147c478bd9Sstevel@tonic-gate 			 * old address and then destroyed.
4157c478bd9Sstevel@tonic-gate 			 */
4167c478bd9Sstevel@tonic-gate 			TRANS_MATA_ALLOC(ufsvfsp, ip, nb, bsize, 0);
4177c478bd9Sstevel@tonic-gate 			ip->i_db[llbn] = nb;
4187c478bd9Sstevel@tonic-gate 			UFS_SET_ISIZE(((u_offset_t)(llbn + 1)) << fs->fs_bshift,
4197c478bd9Sstevel@tonic-gate 			    ip);
4207c478bd9Sstevel@tonic-gate 			ip->i_blocks += btodb(bsize - osize);
4217c478bd9Sstevel@tonic-gate 			ASSERT((unsigned)ip->i_blocks <= INT_MAX);
4227c478bd9Sstevel@tonic-gate 			TRANS_INODE(ufsvfsp, ip);
4237c478bd9Sstevel@tonic-gate 			ip->i_flag |= IUPD | ICHG | IATTCHG;
424303bf60bSsdebnath 
4257c478bd9Sstevel@tonic-gate 			/* Caller is responsible for updating i_seq */
4267c478bd9Sstevel@tonic-gate 			/*
4277c478bd9Sstevel@tonic-gate 			 * Don't check metaflag here, directories won't do this
4287c478bd9Sstevel@tonic-gate 			 *
4297c478bd9Sstevel@tonic-gate 			 */
4307c478bd9Sstevel@tonic-gate 			if (issync) {
4317c478bd9Sstevel@tonic-gate 				(void) ufs_fbiwrite(fbp, ip, nb, fs->fs_fsize);
4327c478bd9Sstevel@tonic-gate 			} else {
4337c478bd9Sstevel@tonic-gate 				ASSERT(fbp);
4347c478bd9Sstevel@tonic-gate 				fbrelse(fbp, S_WRITE);
4357c478bd9Sstevel@tonic-gate 			}
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate 			if (nb != ob) {
4387c478bd9Sstevel@tonic-gate 				(void) free(ip, ob, (off_t)osize, metaflag);
4397c478bd9Sstevel@tonic-gate 			}
4407c478bd9Sstevel@tonic-gate 		}
4417c478bd9Sstevel@tonic-gate 	}
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate 	/*
4447c478bd9Sstevel@tonic-gate 	 * The first NDADDR blocks are direct blocks.
4457c478bd9Sstevel@tonic-gate 	 */
4467c478bd9Sstevel@tonic-gate 	if (lbn < NDADDR) {
4477c478bd9Sstevel@tonic-gate 		nb = ip->i_db[lbn];
4487c478bd9Sstevel@tonic-gate 		if (nb == 0 ||
4497c478bd9Sstevel@tonic-gate 		    ip->i_size < ((u_offset_t)(lbn + 1)) << fs->fs_bshift) {
4507c478bd9Sstevel@tonic-gate 			if (nb != 0) {
4517c478bd9Sstevel@tonic-gate 				/* consider need to reallocate a frag */
4527c478bd9Sstevel@tonic-gate 				osize = fragroundup(fs, blkoff(fs, ip->i_size));
4537c478bd9Sstevel@tonic-gate 				nsize = fragroundup(fs, size);
4547c478bd9Sstevel@tonic-gate 				if (nsize <= osize)
4557c478bd9Sstevel@tonic-gate 					goto gotit;
4567c478bd9Sstevel@tonic-gate 				/*
4577c478bd9Sstevel@tonic-gate 				 * Check to see if doing this will make the
4587c478bd9Sstevel@tonic-gate 				 * file too big.  Only check if we are dealing
4597c478bd9Sstevel@tonic-gate 				 * with a very large file.
4607c478bd9Sstevel@tonic-gate 				 */
4617c478bd9Sstevel@tonic-gate 				if (verylargefile == 1) {
4627c478bd9Sstevel@tonic-gate 					if (((unsigned)ip->i_blocks +
4637c478bd9Sstevel@tonic-gate 					    btodb(nsize - osize)) > INT_MAX) {
4647c478bd9Sstevel@tonic-gate 						return (EFBIG);
4657c478bd9Sstevel@tonic-gate 					}
4667c478bd9Sstevel@tonic-gate 				}
4677c478bd9Sstevel@tonic-gate 				/*
468303bf60bSsdebnath 				 * need to re-allocate a block or frag
4697c478bd9Sstevel@tonic-gate 				 */
4707c478bd9Sstevel@tonic-gate 				ob = nb;
4717c478bd9Sstevel@tonic-gate 				pref = blkpref(ip, lbn, (int)lbn,
4727c478bd9Sstevel@tonic-gate 								&ip->i_db[0]);
4737c478bd9Sstevel@tonic-gate 				err = realloccg(ip, ob, pref, (int)osize,
4747c478bd9Sstevel@tonic-gate 						(int)nsize, &nb, cr);
4757c478bd9Sstevel@tonic-gate 				if (err)
4767c478bd9Sstevel@tonic-gate 					return (err);
477303bf60bSsdebnath 				if (allocblk)
478303bf60bSsdebnath 					*allocblk = nb;
4797c478bd9Sstevel@tonic-gate 				ASSERT(!ufs_badblock(ip, nb));
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 			} else {
4827c478bd9Sstevel@tonic-gate 				/*
4837c478bd9Sstevel@tonic-gate 				 * need to allocate a block or frag
4847c478bd9Sstevel@tonic-gate 				 */
4857c478bd9Sstevel@tonic-gate 				osize = 0;
4867c478bd9Sstevel@tonic-gate 				if (ip->i_size <
4877c478bd9Sstevel@tonic-gate 				    ((u_offset_t)(lbn + 1)) << fs->fs_bshift)
4887c478bd9Sstevel@tonic-gate 					nsize = fragroundup(fs, size);
4897c478bd9Sstevel@tonic-gate 				else
4907c478bd9Sstevel@tonic-gate 					nsize = bsize;
4917c478bd9Sstevel@tonic-gate 				/*
4927c478bd9Sstevel@tonic-gate 				 * Check to see if doing this will make the
4937c478bd9Sstevel@tonic-gate 				 * file too big.  Only check if we are dealing
4947c478bd9Sstevel@tonic-gate 				 * with a very large file.
4957c478bd9Sstevel@tonic-gate 				 */
4967c478bd9Sstevel@tonic-gate 				if (verylargefile == 1) {
4977c478bd9Sstevel@tonic-gate 					if (((unsigned)ip->i_blocks +
4987c478bd9Sstevel@tonic-gate 					    btodb(nsize - osize)) > INT_MAX) {
4997c478bd9Sstevel@tonic-gate 						return (EFBIG);
5007c478bd9Sstevel@tonic-gate 					}
5017c478bd9Sstevel@tonic-gate 				}
5027c478bd9Sstevel@tonic-gate 				pref = blkpref(ip, lbn, (int)lbn, &ip->i_db[0]);
5037c478bd9Sstevel@tonic-gate 				err = alloc(ip, pref, (int)nsize, &nb, cr);
5047c478bd9Sstevel@tonic-gate 				if (err)
5057c478bd9Sstevel@tonic-gate 					return (err);
506303bf60bSsdebnath 				if (allocblk)
507303bf60bSsdebnath 					*allocblk = nb;
5087c478bd9Sstevel@tonic-gate 				ASSERT(!ufs_badblock(ip, nb));
5097c478bd9Sstevel@tonic-gate 				ob = nb;
5107c478bd9Sstevel@tonic-gate 			}
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate 			/*
5137c478bd9Sstevel@tonic-gate 			 * Read old/create new zero pages
5147c478bd9Sstevel@tonic-gate 			 */
5157c478bd9Sstevel@tonic-gate 			fbp = NULL;
5167c478bd9Sstevel@tonic-gate 			if (osize == 0) {
5177c478bd9Sstevel@tonic-gate 				/*
5187c478bd9Sstevel@tonic-gate 				 * mmap S_WRITE faults always enter here
5197c478bd9Sstevel@tonic-gate 				 */
520303bf60bSsdebnath 				/*
521303bf60bSsdebnath 				 * We zero it if its also BI_FALLOCATE, but
522303bf60bSsdebnath 				 * only for direct blocks!
523303bf60bSsdebnath 				 */
524303bf60bSsdebnath 				if (alloc_type == BI_NORMAL ||
525303bf60bSsdebnath 				    alloc_type == BI_FALLOCATE ||
526303bf60bSsdebnath 				    P2ROUNDUP_TYPED(size,
5277c478bd9Sstevel@tonic-gate 				    PAGESIZE, u_offset_t) < nsize) {
5287c478bd9Sstevel@tonic-gate 					/* fbzero doesn't cause a pagefault */
5297c478bd9Sstevel@tonic-gate 					fbzero(ITOV(ip),
5307c478bd9Sstevel@tonic-gate 					    ((offset_t)lbn << fs->fs_bshift),
5317c478bd9Sstevel@tonic-gate 					    (uint_t)nsize, &fbp);
5327c478bd9Sstevel@tonic-gate 				}
5337c478bd9Sstevel@tonic-gate 			} else {
5347c478bd9Sstevel@tonic-gate 				err = fbread(vp,
5357c478bd9Sstevel@tonic-gate 				    ((offset_t)lbn << fs->fs_bshift),
5367c478bd9Sstevel@tonic-gate 				    (uint_t)nsize, S_OTHER, &fbp);
5377c478bd9Sstevel@tonic-gate 				if (err) {
5387c478bd9Sstevel@tonic-gate 					if (nb != ob) {
5397c478bd9Sstevel@tonic-gate 						(void) free(ip, nb,
5407c478bd9Sstevel@tonic-gate 						    (off_t)nsize, metaflag);
5417c478bd9Sstevel@tonic-gate 					} else {
5427c478bd9Sstevel@tonic-gate 						(void) free(ip,
5437c478bd9Sstevel@tonic-gate 						    ob + numfrags(fs, osize),
5447c478bd9Sstevel@tonic-gate 						    (off_t)(nsize - osize),
5457c478bd9Sstevel@tonic-gate 						    metaflag);
5467c478bd9Sstevel@tonic-gate 					}
5477c478bd9Sstevel@tonic-gate 					ASSERT(nsize >= osize);
5487c478bd9Sstevel@tonic-gate 					(void) chkdq(ip,
5497c478bd9Sstevel@tonic-gate 						-(long)btodb(nsize - osize),
5507c478bd9Sstevel@tonic-gate 						0, cr, (char **)NULL,
5517c478bd9Sstevel@tonic-gate 						(size_t *)NULL);
5527c478bd9Sstevel@tonic-gate 					return (err);
5537c478bd9Sstevel@tonic-gate 				}
5547c478bd9Sstevel@tonic-gate 			}
5557c478bd9Sstevel@tonic-gate 			TRANS_MATA_ALLOC(ufsvfsp, ip, nb, nsize, 0);
5567c478bd9Sstevel@tonic-gate 			ip->i_db[lbn] = nb;
5577c478bd9Sstevel@tonic-gate 			ip->i_blocks += btodb(nsize - osize);
5587c478bd9Sstevel@tonic-gate 			ASSERT((unsigned)ip->i_blocks <= INT_MAX);
5597c478bd9Sstevel@tonic-gate 			TRANS_INODE(ufsvfsp, ip);
5607c478bd9Sstevel@tonic-gate 			ip->i_flag |= IUPD | ICHG | IATTCHG;
561303bf60bSsdebnath 
5627c478bd9Sstevel@tonic-gate 			/* Caller is responsible for updating i_seq */
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate 			/*
5657c478bd9Sstevel@tonic-gate 			 * Write directory and shadow blocks synchronously so
5667c478bd9Sstevel@tonic-gate 			 * that they never appear with garbage in them on the
5677c478bd9Sstevel@tonic-gate 			 * disk.
5687c478bd9Sstevel@tonic-gate 			 *
5697c478bd9Sstevel@tonic-gate 			 */
5707c478bd9Sstevel@tonic-gate 			if (isdirquota && (ip->i_size ||
5717c478bd9Sstevel@tonic-gate 			    TRANS_ISTRANS(ufsvfsp))) {
5727c478bd9Sstevel@tonic-gate 			/*
5737c478bd9Sstevel@tonic-gate 			 * XXX man not be necessary with harpy trans
5747c478bd9Sstevel@tonic-gate 			 * bug id 1130055
5757c478bd9Sstevel@tonic-gate 			 */
5767c478bd9Sstevel@tonic-gate 				(void) ufs_fbiwrite(fbp, ip, nb, fs->fs_fsize);
5777c478bd9Sstevel@tonic-gate 			} else if (fbp) {
5787c478bd9Sstevel@tonic-gate 				fbrelse(fbp, S_WRITE);
5797c478bd9Sstevel@tonic-gate 			}
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate 			if (nb != ob)
5827c478bd9Sstevel@tonic-gate 				(void) free(ip, ob, (off_t)osize, metaflag);
5837c478bd9Sstevel@tonic-gate 		}
5847c478bd9Sstevel@tonic-gate gotit:
5857c478bd9Sstevel@tonic-gate 		return (0);
5867c478bd9Sstevel@tonic-gate 	}
5877c478bd9Sstevel@tonic-gate 
5887c478bd9Sstevel@tonic-gate 	added_sectors = alloced_blocks = 0;	/* No blocks alloced yet */
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate 	/*
5917c478bd9Sstevel@tonic-gate 	 * Determine how many levels of indirection.
5927c478bd9Sstevel@tonic-gate 	 */
5937c478bd9Sstevel@tonic-gate 	nindirshift = ip->i_ufsvfs->vfs_nindirshift;
5947c478bd9Sstevel@tonic-gate 	nindiroffset = ip->i_ufsvfs->vfs_nindiroffset;
5957c478bd9Sstevel@tonic-gate 	pref = 0;
5967c478bd9Sstevel@tonic-gate 	shft = 0;				/* sh = 1 */
5977c478bd9Sstevel@tonic-gate 	tbn = lbn - NDADDR;
5987c478bd9Sstevel@tonic-gate 	for (j = NIADDR; j > 0; j--) {
5997c478bd9Sstevel@tonic-gate 		longlong_t	sh;
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate 		shft += nindirshift;		/* sh *= nindir */
6027c478bd9Sstevel@tonic-gate 		sh = 1LL << shft;
6037c478bd9Sstevel@tonic-gate 		if (tbn < sh)
6047c478bd9Sstevel@tonic-gate 			break;
6057c478bd9Sstevel@tonic-gate 		tbn -= sh;
6067c478bd9Sstevel@tonic-gate 	}
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate 	if (j == 0)
6097c478bd9Sstevel@tonic-gate 		return (EFBIG);
6107c478bd9Sstevel@tonic-gate 
6117c478bd9Sstevel@tonic-gate 	/*
6127c478bd9Sstevel@tonic-gate 	 * Fetch the first indirect block.
6137c478bd9Sstevel@tonic-gate 	 */
6147c478bd9Sstevel@tonic-gate 	dev = ip->i_dev;
6157c478bd9Sstevel@tonic-gate 	nb = ip->i_ib[NIADDR - j];
6167c478bd9Sstevel@tonic-gate 	if (nb == 0) {
6177c478bd9Sstevel@tonic-gate 		/*
6187c478bd9Sstevel@tonic-gate 		 * Check to see if doing this will make the
6197c478bd9Sstevel@tonic-gate 		 * file too big.  Only check if we are dealing
6207c478bd9Sstevel@tonic-gate 		 * with a very large file.
6217c478bd9Sstevel@tonic-gate 		 */
6227c478bd9Sstevel@tonic-gate 		if (verylargefile == 1) {
6237c478bd9Sstevel@tonic-gate 			if (((unsigned)ip->i_blocks + btodb(bsize))
6247c478bd9Sstevel@tonic-gate 			    > INT_MAX) {
6257c478bd9Sstevel@tonic-gate 				return (EFBIG);
6267c478bd9Sstevel@tonic-gate 			}
6277c478bd9Sstevel@tonic-gate 		}
6287c478bd9Sstevel@tonic-gate 		/*
6297c478bd9Sstevel@tonic-gate 		 * Need to allocate an indirect block.
6307c478bd9Sstevel@tonic-gate 		 */
6317c478bd9Sstevel@tonic-gate 		pref = blkpref(ip, lbn, 0, (daddr32_t *)0);
6327c478bd9Sstevel@tonic-gate 		err = alloc(ip, pref, (int)bsize, &nb, cr);
6337c478bd9Sstevel@tonic-gate 		if (err)
6347c478bd9Sstevel@tonic-gate 			return (err);
6357c478bd9Sstevel@tonic-gate 		TRANS_MATA_ALLOC(ufsvfsp, ip, nb, bsize, 1);
6367c478bd9Sstevel@tonic-gate 		ASSERT(!ufs_badblock(ip, nb));
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 		/*
6397c478bd9Sstevel@tonic-gate 		 * Keep track of this allocation so we can undo it if we
6407c478bd9Sstevel@tonic-gate 		 * get an error later.
6417c478bd9Sstevel@tonic-gate 		 */
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate 		ASSERT(alloced_blocks <= NIADDR);
6447c478bd9Sstevel@tonic-gate 
6457c478bd9Sstevel@tonic-gate 		undo_table[alloced_blocks].this_block = nb;
6467c478bd9Sstevel@tonic-gate 		undo_table[alloced_blocks].block_size = bsize;
6477c478bd9Sstevel@tonic-gate 		undo_table[alloced_blocks].owner = ufs_no_owner;
6487c478bd9Sstevel@tonic-gate 		undo_table[alloced_blocks].usage_flags = metaflag | I_IBLK;
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate 		alloced_blocks++;
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate 		/*
6537c478bd9Sstevel@tonic-gate 		 * Write zero block synchronously so that
6547c478bd9Sstevel@tonic-gate 		 * indirect blocks never point at garbage.
6557c478bd9Sstevel@tonic-gate 		 */
6567c478bd9Sstevel@tonic-gate 		bp = UFS_GETBLK(ufsvfsp, dev, fsbtodb(fs, nb), bsize);
6577c478bd9Sstevel@tonic-gate 
6587c478bd9Sstevel@tonic-gate 		clrbuf(bp);
6597c478bd9Sstevel@tonic-gate 		/* XXX Maybe special-case this? */
6607c478bd9Sstevel@tonic-gate 		TRANS_BUF(ufsvfsp, 0, bsize, bp, DT_ABZERO);
6617c478bd9Sstevel@tonic-gate 		UFS_BWRITE2(ufsvfsp, bp);
6627c478bd9Sstevel@tonic-gate 		if (bp->b_flags & B_ERROR) {
6637c478bd9Sstevel@tonic-gate 			err = geterror(bp);
6647c478bd9Sstevel@tonic-gate 			brelse(bp);
6657c478bd9Sstevel@tonic-gate 			ufs_undo_allocation(ip, alloced_blocks,
6667c478bd9Sstevel@tonic-gate 			    undo_table, added_sectors);
6677c478bd9Sstevel@tonic-gate 			return (err);
6687c478bd9Sstevel@tonic-gate 		}
6697c478bd9Sstevel@tonic-gate 		brelse(bp);
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate 		ip->i_ib[NIADDR - j] = nb;
6727c478bd9Sstevel@tonic-gate 		added_sectors += btodb(bsize);
6737c478bd9Sstevel@tonic-gate 		ip->i_blocks += btodb(bsize);
6747c478bd9Sstevel@tonic-gate 		ASSERT((unsigned)ip->i_blocks <= INT_MAX);
6757c478bd9Sstevel@tonic-gate 		TRANS_INODE(ufsvfsp, ip);
6767c478bd9Sstevel@tonic-gate 		ip->i_flag |= IUPD | ICHG | IATTCHG;
6777c478bd9Sstevel@tonic-gate 		/* Caller is responsible for updating i_seq */
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate 		/*
6807c478bd9Sstevel@tonic-gate 		 * Update the 'undo table' now that we've linked this block
6817c478bd9Sstevel@tonic-gate 		 * to an inode.
6827c478bd9Sstevel@tonic-gate 		 */
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate 		undo_table[alloced_blocks-1].owner = ufs_inode_indirect;
6857c478bd9Sstevel@tonic-gate 		undo_table[alloced_blocks-1].owner_offset = NIADDR - j;
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate 		/*
6887c478bd9Sstevel@tonic-gate 		 * In the ISYNC case, wrip will notice that the block
6897c478bd9Sstevel@tonic-gate 		 * count on the inode has changed and will be sure to
6907c478bd9Sstevel@tonic-gate 		 * ufs_iupdat the inode at the end of wrip.
6917c478bd9Sstevel@tonic-gate 		 */
6927c478bd9Sstevel@tonic-gate 	}
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate 	/*
6957c478bd9Sstevel@tonic-gate 	 * Fetch through the indirect blocks.
6967c478bd9Sstevel@tonic-gate 	 */
6977c478bd9Sstevel@tonic-gate 	for (; j <= NIADDR; j++) {
6987c478bd9Sstevel@tonic-gate 		ob = nb;
6997c478bd9Sstevel@tonic-gate 		bp = UFS_BREAD(ufsvfsp, ip->i_dev, fsbtodb(fs, ob), bsize);
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate 		if (bp->b_flags & B_ERROR) {
7027c478bd9Sstevel@tonic-gate 			err = geterror(bp);
7037c478bd9Sstevel@tonic-gate 			brelse(bp);
7047c478bd9Sstevel@tonic-gate 			/*
7057c478bd9Sstevel@tonic-gate 			 * Return any partial allocations.
7067c478bd9Sstevel@tonic-gate 			 *
7077c478bd9Sstevel@tonic-gate 			 * It is possible that we have not yet made any
7087c478bd9Sstevel@tonic-gate 			 * allocations at this point (if this is the first
7097c478bd9Sstevel@tonic-gate 			 * pass through the loop and we didn't have to
7107c478bd9Sstevel@tonic-gate 			 * allocate the first indirect block, above).
7117c478bd9Sstevel@tonic-gate 			 * In this case, alloced_blocks and added_sectors will
7127c478bd9Sstevel@tonic-gate 			 * be zero, and ufs_undo_allocation will do nothing.
7137c478bd9Sstevel@tonic-gate 			 */
7147c478bd9Sstevel@tonic-gate 			ufs_undo_allocation(ip, alloced_blocks,
7157c478bd9Sstevel@tonic-gate 			    undo_table, added_sectors);
7167c478bd9Sstevel@tonic-gate 			return (err);
7177c478bd9Sstevel@tonic-gate 		}
7187c478bd9Sstevel@tonic-gate 		bap = bp->b_un.b_daddr;
7197c478bd9Sstevel@tonic-gate 		shft -= nindirshift;		/* sh /= nindir */
7207c478bd9Sstevel@tonic-gate 		i = (tbn >> shft) & nindiroffset; /* (tbn / sh) % nindir */
7217c478bd9Sstevel@tonic-gate 		nb = bap[i];
722303bf60bSsdebnath 
7237c478bd9Sstevel@tonic-gate 		if (nb == 0) {
7247c478bd9Sstevel@tonic-gate 			/*
7257c478bd9Sstevel@tonic-gate 			 * Check to see if doing this will make the
7267c478bd9Sstevel@tonic-gate 			 * file too big.  Only check if we are dealing
7277c478bd9Sstevel@tonic-gate 			 * with a very large file.
7287c478bd9Sstevel@tonic-gate 			 */
7297c478bd9Sstevel@tonic-gate 			if (verylargefile == 1) {
7307c478bd9Sstevel@tonic-gate 				if (((unsigned)ip->i_blocks + btodb(bsize))
7317c478bd9Sstevel@tonic-gate 				    > INT_MAX) {
7327c478bd9Sstevel@tonic-gate 					brelse(bp);
7337c478bd9Sstevel@tonic-gate 					ufs_undo_allocation(ip, alloced_blocks,
7347c478bd9Sstevel@tonic-gate 					    undo_table, added_sectors);
7357c478bd9Sstevel@tonic-gate 					return (EFBIG);
7367c478bd9Sstevel@tonic-gate 				}
7377c478bd9Sstevel@tonic-gate 			}
7387c478bd9Sstevel@tonic-gate 			if (pref == 0) {
7397c478bd9Sstevel@tonic-gate 				if (j < NIADDR) {
7407c478bd9Sstevel@tonic-gate 					/* Indirect block */
7417c478bd9Sstevel@tonic-gate 					pref = blkpref(ip, lbn, 0,
7427c478bd9Sstevel@tonic-gate 						(daddr32_t *)0);
7437c478bd9Sstevel@tonic-gate 				} else {
7447c478bd9Sstevel@tonic-gate 					/* Data block */
7457c478bd9Sstevel@tonic-gate 					pref = blkpref(ip, lbn, i, &bap[0]);
7467c478bd9Sstevel@tonic-gate 				}
7477c478bd9Sstevel@tonic-gate 			}
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate 			/*
7507c478bd9Sstevel@tonic-gate 			 * release "bp" buf to avoid deadlock (re-bread later)
7517c478bd9Sstevel@tonic-gate 			 */
7527c478bd9Sstevel@tonic-gate 			brelse(bp);
7537c478bd9Sstevel@tonic-gate 
7547c478bd9Sstevel@tonic-gate 			err = alloc(ip, pref, (int)bsize, &nb, cr);
7557c478bd9Sstevel@tonic-gate 			if (err) {
7567c478bd9Sstevel@tonic-gate 				/*
7577c478bd9Sstevel@tonic-gate 				 * Return any partial allocations.
7587c478bd9Sstevel@tonic-gate 				 */
7597c478bd9Sstevel@tonic-gate 				ufs_undo_allocation(ip, alloced_blocks,
7607c478bd9Sstevel@tonic-gate 				    undo_table, added_sectors);
7617c478bd9Sstevel@tonic-gate 				return (err);
7627c478bd9Sstevel@tonic-gate 			}
7637c478bd9Sstevel@tonic-gate 
7647c478bd9Sstevel@tonic-gate 			ASSERT(!ufs_badblock(ip, nb));
7657c478bd9Sstevel@tonic-gate 			ASSERT(alloced_blocks <= NIADDR);
7667c478bd9Sstevel@tonic-gate 
767303bf60bSsdebnath 			if (allocblk)
768303bf60bSsdebnath 				*allocblk = nb;
769303bf60bSsdebnath 
7707c478bd9Sstevel@tonic-gate 			undo_table[alloced_blocks].this_block = nb;
7717c478bd9Sstevel@tonic-gate 			undo_table[alloced_blocks].block_size = bsize;
7727c478bd9Sstevel@tonic-gate 			undo_table[alloced_blocks].owner = ufs_no_owner;
7737c478bd9Sstevel@tonic-gate 			undo_table[alloced_blocks].usage_flags = metaflag |
7747c478bd9Sstevel@tonic-gate 			    ((j < NIADDR) ? I_IBLK : 0);
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate 			alloced_blocks++;
7777c478bd9Sstevel@tonic-gate 
7787c478bd9Sstevel@tonic-gate 			if (j < NIADDR) {
7797c478bd9Sstevel@tonic-gate 				TRANS_MATA_ALLOC(ufsvfsp, ip, nb, bsize, 1);
7807c478bd9Sstevel@tonic-gate 				/*
7817c478bd9Sstevel@tonic-gate 				 * Write synchronously so indirect
7827c478bd9Sstevel@tonic-gate 				 * blocks never point at garbage.
7837c478bd9Sstevel@tonic-gate 				 */
7847c478bd9Sstevel@tonic-gate 				nbp = UFS_GETBLK(
7857c478bd9Sstevel@tonic-gate 					ufsvfsp, dev, fsbtodb(fs, nb), bsize);
7867c478bd9Sstevel@tonic-gate 
7877c478bd9Sstevel@tonic-gate 				clrbuf(nbp);
7887c478bd9Sstevel@tonic-gate 				/* XXX Maybe special-case this? */
7897c478bd9Sstevel@tonic-gate 				TRANS_BUF(ufsvfsp, 0, bsize, nbp, DT_ABZERO);
7907c478bd9Sstevel@tonic-gate 				UFS_BWRITE2(ufsvfsp, nbp);
7917c478bd9Sstevel@tonic-gate 				if (nbp->b_flags & B_ERROR) {
7927c478bd9Sstevel@tonic-gate 					err = geterror(nbp);
7937c478bd9Sstevel@tonic-gate 					brelse(nbp);
7947c478bd9Sstevel@tonic-gate 					/*
7957c478bd9Sstevel@tonic-gate 					 * Return any partial
7967c478bd9Sstevel@tonic-gate 					 * allocations.
7977c478bd9Sstevel@tonic-gate 					 */
7987c478bd9Sstevel@tonic-gate 					ufs_undo_allocation(ip,
7997c478bd9Sstevel@tonic-gate 					    alloced_blocks,
8007c478bd9Sstevel@tonic-gate 					    undo_table, added_sectors);
8017c478bd9Sstevel@tonic-gate 					return (err);
8027c478bd9Sstevel@tonic-gate 				}
8037c478bd9Sstevel@tonic-gate 				brelse(nbp);
804303bf60bSsdebnath 			} else if (alloc_type == BI_NORMAL ||
805303bf60bSsdebnath 			    P2ROUNDUP_TYPED(size,
8067c478bd9Sstevel@tonic-gate 			    PAGESIZE, u_offset_t) < bsize) {
8077c478bd9Sstevel@tonic-gate 				TRANS_MATA_ALLOC(ufsvfsp, ip, nb, bsize, 0);
8087c478bd9Sstevel@tonic-gate 				fbzero(ITOV(ip),
8097c478bd9Sstevel@tonic-gate 				    ((offset_t)lbn << fs->fs_bshift),
8107c478bd9Sstevel@tonic-gate 				    (uint_t)bsize, &fbp);
8117c478bd9Sstevel@tonic-gate 
8127c478bd9Sstevel@tonic-gate 				/*
8137c478bd9Sstevel@tonic-gate 				 * Cases which we need to do a synchronous
8147c478bd9Sstevel@tonic-gate 				 * write of the zeroed data pages:
8157c478bd9Sstevel@tonic-gate 				 *
8167c478bd9Sstevel@tonic-gate 				 * 1) If we are writing a directory then we
8177c478bd9Sstevel@tonic-gate 				 * want to write synchronously so blocks in
8187c478bd9Sstevel@tonic-gate 				 * directories never contain garbage.
8197c478bd9Sstevel@tonic-gate 				 *
8207c478bd9Sstevel@tonic-gate 				 * 2) If we are filling in a hole and the
8217c478bd9Sstevel@tonic-gate 				 * indirect block is going to be synchronously
8227c478bd9Sstevel@tonic-gate 				 * written back below we need to make sure
8237c478bd9Sstevel@tonic-gate 				 * that the zeroes are written here before
8247c478bd9Sstevel@tonic-gate 				 * the indirect block is updated so that if
8257c478bd9Sstevel@tonic-gate 				 * we crash before the real data is pushed
8267c478bd9Sstevel@tonic-gate 				 * we will not end up with random data is
8277c478bd9Sstevel@tonic-gate 				 * the middle of the file.
8287c478bd9Sstevel@tonic-gate 				 *
8297c478bd9Sstevel@tonic-gate 				 * 3) If the size of the request rounded up
8307c478bd9Sstevel@tonic-gate 				 * to the system page size is smaller than
8317c478bd9Sstevel@tonic-gate 				 * the file system block size, we want to
8327c478bd9Sstevel@tonic-gate 				 * write out all the pages now so that
8337c478bd9Sstevel@tonic-gate 				 * they are not aborted before they actually
8347c478bd9Sstevel@tonic-gate 				 * make it to ufs_putpage since the length
8357c478bd9Sstevel@tonic-gate 				 * of the inode will not include the pages.
8367c478bd9Sstevel@tonic-gate 				 */
8377c478bd9Sstevel@tonic-gate 
8387c478bd9Sstevel@tonic-gate 				if (isdirquota || (issync &&
8397c478bd9Sstevel@tonic-gate 				    lbn < llbn))
8407c478bd9Sstevel@tonic-gate 					(void) ufs_fbiwrite(fbp, ip, nb,
8417c478bd9Sstevel@tonic-gate 						fs->fs_fsize);
8427c478bd9Sstevel@tonic-gate 				else
8437c478bd9Sstevel@tonic-gate 					fbrelse(fbp, S_WRITE);
8447c478bd9Sstevel@tonic-gate 			}
8457c478bd9Sstevel@tonic-gate 
8467c478bd9Sstevel@tonic-gate 			/*
8477c478bd9Sstevel@tonic-gate 			 * re-acquire "bp" buf
8487c478bd9Sstevel@tonic-gate 			 */
8497c478bd9Sstevel@tonic-gate 			bp = UFS_BREAD(ufsvfsp,
8507c478bd9Sstevel@tonic-gate 					ip->i_dev, fsbtodb(fs, ob), bsize);
8517c478bd9Sstevel@tonic-gate 			if (bp->b_flags & B_ERROR) {
8527c478bd9Sstevel@tonic-gate 				err = geterror(bp);
8537c478bd9Sstevel@tonic-gate 				brelse(bp);
8547c478bd9Sstevel@tonic-gate 				/*
8557c478bd9Sstevel@tonic-gate 				 * Return any partial allocations.
8567c478bd9Sstevel@tonic-gate 				 */
8577c478bd9Sstevel@tonic-gate 				ufs_undo_allocation(ip,
8587c478bd9Sstevel@tonic-gate 				    alloced_blocks,
8597c478bd9Sstevel@tonic-gate 				    undo_table, added_sectors);
8607c478bd9Sstevel@tonic-gate 				return (err);
8617c478bd9Sstevel@tonic-gate 			}
8627c478bd9Sstevel@tonic-gate 			bap = bp->b_un.b_daddr;
8637c478bd9Sstevel@tonic-gate 			bap[i] = nb;
864303bf60bSsdebnath 
865303bf60bSsdebnath 			/*
866303bf60bSsdebnath 			 * The magic explained: j will be equal to NIADDR
867303bf60bSsdebnath 			 * when we are at the lowest level, this is where the
868303bf60bSsdebnath 			 * array entries point directly to data blocks. Since
869303bf60bSsdebnath 			 * we will be 'fallocate'ing we will go ahead and negate
870303bf60bSsdebnath 			 * the addresses.
871303bf60bSsdebnath 			 */
872303bf60bSsdebnath 			if (alloc_type == BI_FALLOCATE && j == NIADDR)
873303bf60bSsdebnath 				bap[i] = -bap[i];
874303bf60bSsdebnath 
8757c478bd9Sstevel@tonic-gate 			TRANS_BUF_ITEM_128(ufsvfsp, bap[i], bap, bp, DT_AB);
8767c478bd9Sstevel@tonic-gate 			added_sectors += btodb(bsize);
8777c478bd9Sstevel@tonic-gate 			ip->i_blocks += btodb(bsize);
8787c478bd9Sstevel@tonic-gate 			ASSERT((unsigned)ip->i_blocks <= INT_MAX);
8797c478bd9Sstevel@tonic-gate 			TRANS_INODE(ufsvfsp, ip);
8807c478bd9Sstevel@tonic-gate 			ip->i_flag |= IUPD | ICHG | IATTCHG;
881303bf60bSsdebnath 
8827c478bd9Sstevel@tonic-gate 			/* Caller is responsible for updating i_seq */
8837c478bd9Sstevel@tonic-gate 
8847c478bd9Sstevel@tonic-gate 			undo_table[alloced_blocks-1].owner =
8857c478bd9Sstevel@tonic-gate 			    ufs_indirect_block;
8867c478bd9Sstevel@tonic-gate 			undo_table[alloced_blocks-1].owner_block = ob;
8877c478bd9Sstevel@tonic-gate 			undo_table[alloced_blocks-1].owner_offset = i;
8887c478bd9Sstevel@tonic-gate 
8897c478bd9Sstevel@tonic-gate 			if (issync) {
8907c478bd9Sstevel@tonic-gate 				UFS_BWRITE2(ufsvfsp, bp);
8917c478bd9Sstevel@tonic-gate 				if (bp->b_flags & B_ERROR) {
8927c478bd9Sstevel@tonic-gate 					err = geterror(bp);
8937c478bd9Sstevel@tonic-gate 					brelse(bp);
8947c478bd9Sstevel@tonic-gate 					/*
8957c478bd9Sstevel@tonic-gate 					 * Return any partial
8967c478bd9Sstevel@tonic-gate 					 * allocations.
8977c478bd9Sstevel@tonic-gate 					 */
8987c478bd9Sstevel@tonic-gate 					ufs_undo_allocation(ip,
8997c478bd9Sstevel@tonic-gate 					    alloced_blocks,
9007c478bd9Sstevel@tonic-gate 					    undo_table, added_sectors);
9017c478bd9Sstevel@tonic-gate 					return (err);
9027c478bd9Sstevel@tonic-gate 				}
9037c478bd9Sstevel@tonic-gate 				brelse(bp);
9047c478bd9Sstevel@tonic-gate 			} else {
9057c478bd9Sstevel@tonic-gate 				bdrwrite(bp);
9067c478bd9Sstevel@tonic-gate 			}
9077c478bd9Sstevel@tonic-gate 		} else {
9087c478bd9Sstevel@tonic-gate 			brelse(bp);
9097c478bd9Sstevel@tonic-gate 		}
9107c478bd9Sstevel@tonic-gate 	}
9117c478bd9Sstevel@tonic-gate 	return (0);
9127c478bd9Sstevel@tonic-gate }
9137c478bd9Sstevel@tonic-gate 
9147c478bd9Sstevel@tonic-gate /*
9157c478bd9Sstevel@tonic-gate  * Return 1 if inode has unmapped blocks (UFS holes).
9167c478bd9Sstevel@tonic-gate  */
9177c478bd9Sstevel@tonic-gate int
9187c478bd9Sstevel@tonic-gate bmap_has_holes(struct inode *ip)
9197c478bd9Sstevel@tonic-gate {
9207c478bd9Sstevel@tonic-gate 	struct fs *fs = ip->i_fs;
9217c478bd9Sstevel@tonic-gate 	uint_t	dblks; 			/* # of data blocks */
9227c478bd9Sstevel@tonic-gate 	uint_t	mblks;			/* # of data + metadata blocks */
9237c478bd9Sstevel@tonic-gate 	int	nindirshift;
9247c478bd9Sstevel@tonic-gate 	int	nindiroffset;
9257c478bd9Sstevel@tonic-gate 	uint_t	cnt;
9267c478bd9Sstevel@tonic-gate 	int	n, j, shft;
9277c478bd9Sstevel@tonic-gate 	uint_t nindirblks;
9287c478bd9Sstevel@tonic-gate 
9297c478bd9Sstevel@tonic-gate 	int	fsbshift = fs->fs_bshift;
9307c478bd9Sstevel@tonic-gate 	int	fsboffset = (1 << fsbshift) - 1;
9317c478bd9Sstevel@tonic-gate 
9327c478bd9Sstevel@tonic-gate 	dblks = (ip->i_size + fsboffset) >> fsbshift;
9337c478bd9Sstevel@tonic-gate 	mblks = (ldbtob((u_offset_t)ip->i_blocks) + fsboffset) >> fsbshift;
9347c478bd9Sstevel@tonic-gate 
9357c478bd9Sstevel@tonic-gate 	/*
9367c478bd9Sstevel@tonic-gate 	 * File has only direct blocks.
9377c478bd9Sstevel@tonic-gate 	 */
9387c478bd9Sstevel@tonic-gate 	if (dblks <= NDADDR)
9397c478bd9Sstevel@tonic-gate 		return (mblks < dblks);
9407c478bd9Sstevel@tonic-gate 	nindirshift = ip->i_ufsvfs->vfs_nindirshift;
941303bf60bSsdebnath 
9427c478bd9Sstevel@tonic-gate 	nindiroffset = ip->i_ufsvfs->vfs_nindiroffset;
9437c478bd9Sstevel@tonic-gate 	nindirblks = nindiroffset + 1;
9447c478bd9Sstevel@tonic-gate 
9457c478bd9Sstevel@tonic-gate 	dblks -= NDADDR;
9467c478bd9Sstevel@tonic-gate 	shft = 0;
9477c478bd9Sstevel@tonic-gate 	/*
9487c478bd9Sstevel@tonic-gate 	 * Determine how many levels of indirection.
9497c478bd9Sstevel@tonic-gate 	 */
9507c478bd9Sstevel@tonic-gate 	for (j = NIADDR; j > 0; j--) {
9517c478bd9Sstevel@tonic-gate 		longlong_t	sh;
9527c478bd9Sstevel@tonic-gate 
9537c478bd9Sstevel@tonic-gate 		shft += nindirshift;	/* sh *= nindir */
9547c478bd9Sstevel@tonic-gate 		sh = 1LL << shft;
9557c478bd9Sstevel@tonic-gate 		if (dblks <= sh)
9567c478bd9Sstevel@tonic-gate 			break;
9577c478bd9Sstevel@tonic-gate 		dblks -= sh;
9587c478bd9Sstevel@tonic-gate 	}
9597c478bd9Sstevel@tonic-gate 	/* LINTED: warning: logical expression always true: op "||" */
9607c478bd9Sstevel@tonic-gate 	ASSERT(NIADDR <= 3);
9617c478bd9Sstevel@tonic-gate 	ASSERT(j <= NIADDR);
9627c478bd9Sstevel@tonic-gate 	if (j == NIADDR)	/* single level indirection */
9637c478bd9Sstevel@tonic-gate 		cnt = NDADDR + 1 + dblks;
9647c478bd9Sstevel@tonic-gate 	else if (j == NIADDR-1) /* double indirection */
9657c478bd9Sstevel@tonic-gate 		cnt = NDADDR + 1 + nindirblks +
9667c478bd9Sstevel@tonic-gate 			1 + (dblks + nindiroffset)/nindirblks + dblks;
9677c478bd9Sstevel@tonic-gate 	else if (j == NIADDR-2) { /* triple indirection */
9687c478bd9Sstevel@tonic-gate 		n = (dblks + nindiroffset)/nindirblks;
9697c478bd9Sstevel@tonic-gate 		cnt = NDADDR + 1 + nindirblks +
9707c478bd9Sstevel@tonic-gate 			1 + nindirblks + nindirblks*nindirblks +
9717c478bd9Sstevel@tonic-gate 			1 + (n + nindiroffset)/nindirblks + n + dblks;
9727c478bd9Sstevel@tonic-gate 	}
9737c478bd9Sstevel@tonic-gate 
9747c478bd9Sstevel@tonic-gate 	return (mblks < cnt);
9757c478bd9Sstevel@tonic-gate }
9767c478bd9Sstevel@tonic-gate 
9777c478bd9Sstevel@tonic-gate /*
9787c478bd9Sstevel@tonic-gate  * find some contig blocks starting at *sbp and going for min(n, max_contig)
9797c478bd9Sstevel@tonic-gate  * return the number of blocks (not frags) found.
9807c478bd9Sstevel@tonic-gate  * The array passed in must be at least [0..n-1].
9817c478bd9Sstevel@tonic-gate  */
9827c478bd9Sstevel@tonic-gate static int
9837c478bd9Sstevel@tonic-gate findextent(struct fs *fs, daddr32_t *sbp, int n, int *lenp, int maxtransfer)
9847c478bd9Sstevel@tonic-gate {
9857c478bd9Sstevel@tonic-gate 	register daddr_t bn, nextbn;
9867c478bd9Sstevel@tonic-gate 	register daddr32_t *bp;
9877c478bd9Sstevel@tonic-gate 	register int diff;
9887c478bd9Sstevel@tonic-gate 	int maxtransblk;
9897c478bd9Sstevel@tonic-gate 
9907c478bd9Sstevel@tonic-gate 	if (n <= 0)
9917c478bd9Sstevel@tonic-gate 		return (0);
9927c478bd9Sstevel@tonic-gate 	bn = *sbp;
9937c478bd9Sstevel@tonic-gate 	if (bn == 0)
9947c478bd9Sstevel@tonic-gate 		return (0);
995303bf60bSsdebnath 
9967c478bd9Sstevel@tonic-gate 	diff = fs->fs_frag;
9977c478bd9Sstevel@tonic-gate 	if (*lenp) {
9987c478bd9Sstevel@tonic-gate 		n = MIN(n, lblkno(fs, *lenp));
9997c478bd9Sstevel@tonic-gate 	} else {
10007c478bd9Sstevel@tonic-gate 		/*
10017c478bd9Sstevel@tonic-gate 		 * If the user has set the value for maxcontig lower than
10027c478bd9Sstevel@tonic-gate 		 * the drive transfer size, then assume they want this
10037c478bd9Sstevel@tonic-gate 		 * to be the maximum value for the size of the data transfer.
10047c478bd9Sstevel@tonic-gate 		 */
10057c478bd9Sstevel@tonic-gate 		maxtransblk = maxtransfer >> DEV_BSHIFT;
10067c478bd9Sstevel@tonic-gate 		if (fs->fs_maxcontig < maxtransblk) {
10077c478bd9Sstevel@tonic-gate 			n = MIN(n, fs->fs_maxcontig);
10087c478bd9Sstevel@tonic-gate 		} else {
10097c478bd9Sstevel@tonic-gate 			n = MIN(n, maxtransblk);
10107c478bd9Sstevel@tonic-gate 		}
10117c478bd9Sstevel@tonic-gate 	}
10127c478bd9Sstevel@tonic-gate 	bp = sbp;
10137c478bd9Sstevel@tonic-gate 	while (--n > 0) {
10147c478bd9Sstevel@tonic-gate 		nextbn = *(bp + 1);
10157c478bd9Sstevel@tonic-gate 		if (nextbn == 0 || bn + diff != nextbn)
10167c478bd9Sstevel@tonic-gate 			break;
10177c478bd9Sstevel@tonic-gate 		bn = nextbn;
10187c478bd9Sstevel@tonic-gate 		bp++;
10197c478bd9Sstevel@tonic-gate 	}
10207c478bd9Sstevel@tonic-gate 	return ((int)(bp - sbp) + 1);
10217c478bd9Sstevel@tonic-gate }
10227c478bd9Sstevel@tonic-gate 
10237c478bd9Sstevel@tonic-gate /*
10247c478bd9Sstevel@tonic-gate  * Free any blocks which had been successfully allocated.  Always called
10257c478bd9Sstevel@tonic-gate  * as a result of an error, so we don't bother returning an error code
10267c478bd9Sstevel@tonic-gate  * from here.
10277c478bd9Sstevel@tonic-gate  *
10287c478bd9Sstevel@tonic-gate  * If block_count and inode_sector_adjust are both zero, we'll do nothing.
10297c478bd9Sstevel@tonic-gate  * Thus it is safe to call this as part of error handling, whether or not
10307c478bd9Sstevel@tonic-gate  * any blocks have been allocated.
10317c478bd9Sstevel@tonic-gate  *
10327c478bd9Sstevel@tonic-gate  * The ufs_inode_direct case is currently unused.
10337c478bd9Sstevel@tonic-gate  */
10347c478bd9Sstevel@tonic-gate 
10357c478bd9Sstevel@tonic-gate static void
10367c478bd9Sstevel@tonic-gate ufs_undo_allocation(
10377c478bd9Sstevel@tonic-gate 	inode_t *ip,
10387c478bd9Sstevel@tonic-gate 	int block_count,
10397c478bd9Sstevel@tonic-gate 	struct ufs_allocated_block table[],
10407c478bd9Sstevel@tonic-gate 	int inode_sector_adjust)
10417c478bd9Sstevel@tonic-gate {
10427c478bd9Sstevel@tonic-gate 	int i;
10437c478bd9Sstevel@tonic-gate 	int inode_changed;
10447c478bd9Sstevel@tonic-gate 	int error_updating_pointers;
10457c478bd9Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp;
10467c478bd9Sstevel@tonic-gate 
10477c478bd9Sstevel@tonic-gate 	inode_changed = 0;
10487c478bd9Sstevel@tonic-gate 	error_updating_pointers = 0;
10497c478bd9Sstevel@tonic-gate 
10507c478bd9Sstevel@tonic-gate 	ufsvfsp = ip->i_ufsvfs;
10517c478bd9Sstevel@tonic-gate 
10527c478bd9Sstevel@tonic-gate 	/*
10537c478bd9Sstevel@tonic-gate 	 * Update pointers on disk before freeing blocks.  If we fail,
10547c478bd9Sstevel@tonic-gate 	 * some blocks may remain busy; but they will be reclaimed by
10557c478bd9Sstevel@tonic-gate 	 * an fsck.  (This is better than letting a block wind up with
10567c478bd9Sstevel@tonic-gate 	 * two owners if we successfully freed it but could not remove
10577c478bd9Sstevel@tonic-gate 	 * the pointer to it.)
10587c478bd9Sstevel@tonic-gate 	 */
10597c478bd9Sstevel@tonic-gate 
10607c478bd9Sstevel@tonic-gate 	for (i = 0; i < block_count; i++) {
10617c478bd9Sstevel@tonic-gate 		switch (table[i].owner) {
10627c478bd9Sstevel@tonic-gate 		case ufs_no_owner:
10637c478bd9Sstevel@tonic-gate 			/* Nothing to do here, nobody points to us */
10647c478bd9Sstevel@tonic-gate 			break;
10657c478bd9Sstevel@tonic-gate 		case ufs_inode_direct:
10667c478bd9Sstevel@tonic-gate 			ASSERT(table[i].owner_offset < NDADDR);
10677c478bd9Sstevel@tonic-gate 			ip->i_db[table[i].owner_offset] = 0;
10687c478bd9Sstevel@tonic-gate 			inode_changed = 1;
10697c478bd9Sstevel@tonic-gate 			break;
10707c478bd9Sstevel@tonic-gate 		case ufs_inode_indirect:
10717c478bd9Sstevel@tonic-gate 			ASSERT(table[i].owner_offset < NIADDR);
10727c478bd9Sstevel@tonic-gate 			ip->i_ib[table[i].owner_offset] = 0;
10737c478bd9Sstevel@tonic-gate 			inode_changed = 1;
10747c478bd9Sstevel@tonic-gate 			break;
10757c478bd9Sstevel@tonic-gate 		case ufs_indirect_block: {
10767c478bd9Sstevel@tonic-gate 			buf_t *bp;
10777c478bd9Sstevel@tonic-gate 			daddr32_t *block_data;
10787c478bd9Sstevel@tonic-gate 
10797c478bd9Sstevel@tonic-gate 			/* Read/modify/log/write. */
10807c478bd9Sstevel@tonic-gate 
10817c478bd9Sstevel@tonic-gate 			ASSERT(table[i].owner_offset <
10827c478bd9Sstevel@tonic-gate 			    (VBSIZE(ITOV(ip)) / sizeof (daddr32_t)));
10837c478bd9Sstevel@tonic-gate 
10847c478bd9Sstevel@tonic-gate 			bp = UFS_BREAD(ufsvfsp, ip->i_dev,
10857c478bd9Sstevel@tonic-gate 			    fsbtodb(ufsvfsp->vfs_fs, table[i].owner_block),
10867c478bd9Sstevel@tonic-gate 			    VBSIZE(ITOV(ip)));
10877c478bd9Sstevel@tonic-gate 
10887c478bd9Sstevel@tonic-gate 			if (bp->b_flags & B_ERROR) {
10897c478bd9Sstevel@tonic-gate 				/* Couldn't read this block; give up. */
10907c478bd9Sstevel@tonic-gate 				error_updating_pointers = 1;
10917c478bd9Sstevel@tonic-gate 				brelse(bp);
10927c478bd9Sstevel@tonic-gate 				break;		/* out of SWITCH */
10937c478bd9Sstevel@tonic-gate 			}
10947c478bd9Sstevel@tonic-gate 
10957c478bd9Sstevel@tonic-gate 			block_data = bp->b_un.b_daddr;
10967c478bd9Sstevel@tonic-gate 			block_data[table[i].owner_offset] = 0;
10977c478bd9Sstevel@tonic-gate 
10987c478bd9Sstevel@tonic-gate 			/* Write a log entry which includes the zero. */
10997c478bd9Sstevel@tonic-gate 			/* It might be possible to optimize this by using */
11007c478bd9Sstevel@tonic-gate 			/* TRANS_BUF directly and zeroing only the four */
11017c478bd9Sstevel@tonic-gate 			/* bytes involved, but an attempt to do that led */
11027c478bd9Sstevel@tonic-gate 			/* to panics in the logging code.  The attempt was */
11037c478bd9Sstevel@tonic-gate 			/* TRANS_BUF(ufsvfsp,				  */
11047c478bd9Sstevel@tonic-gate 			/*    table[i].owner_offset * sizeof (daddr32_t), */
11057c478bd9Sstevel@tonic-gate 			/*    sizeof (daddr32_t),			  */
11067c478bd9Sstevel@tonic-gate 			/*    bp,					  */
11077c478bd9Sstevel@tonic-gate 			/*    DT_ABZERO);				  */
11087c478bd9Sstevel@tonic-gate 
11097c478bd9Sstevel@tonic-gate 			TRANS_BUF_ITEM_128(ufsvfsp,
11107c478bd9Sstevel@tonic-gate 			    block_data[table[i].owner_offset],
11117c478bd9Sstevel@tonic-gate 			    block_data, bp, DT_AB);
11127c478bd9Sstevel@tonic-gate 
11137c478bd9Sstevel@tonic-gate 			/* Now we can write the buffer itself. */
11147c478bd9Sstevel@tonic-gate 
11157c478bd9Sstevel@tonic-gate 			UFS_BWRITE2(ufsvfsp, bp);
11167c478bd9Sstevel@tonic-gate 
11177c478bd9Sstevel@tonic-gate 			if (bp->b_flags & B_ERROR) {
11187c478bd9Sstevel@tonic-gate 				error_updating_pointers = 1;
11197c478bd9Sstevel@tonic-gate 			}
11207c478bd9Sstevel@tonic-gate 
11217c478bd9Sstevel@tonic-gate 			brelse(bp);
11227c478bd9Sstevel@tonic-gate 			break;
11237c478bd9Sstevel@tonic-gate 		}
11247c478bd9Sstevel@tonic-gate 		default:
11257c478bd9Sstevel@tonic-gate 			(void) ufs_fault(ITOV(ip),
11267c478bd9Sstevel@tonic-gate 			    "ufs_undo_allocation failure\n");
11277c478bd9Sstevel@tonic-gate 			break;
11287c478bd9Sstevel@tonic-gate 		}
11297c478bd9Sstevel@tonic-gate 	}
11307c478bd9Sstevel@tonic-gate 
11317c478bd9Sstevel@tonic-gate 	/*
11327c478bd9Sstevel@tonic-gate 	 * If the inode changed, or if we need to update its block count,
11337c478bd9Sstevel@tonic-gate 	 * then do that now.  We update the inode synchronously on disk
11347c478bd9Sstevel@tonic-gate 	 * to ensure that it won't transiently point at a block we've
11357c478bd9Sstevel@tonic-gate 	 * freed (only necessary if we're not logging).
11367c478bd9Sstevel@tonic-gate 	 *
11377c478bd9Sstevel@tonic-gate 	 * NOTE: Currently ufs_iupdat() does not check for errors.  When
11387c478bd9Sstevel@tonic-gate 	 * it is fixed, we should verify that we successfully updated the
11397c478bd9Sstevel@tonic-gate 	 * inode before freeing blocks below.
11407c478bd9Sstevel@tonic-gate 	 */
11417c478bd9Sstevel@tonic-gate 
11427c478bd9Sstevel@tonic-gate 	if (inode_changed || (inode_sector_adjust != 0)) {
11437c478bd9Sstevel@tonic-gate 		ip->i_blocks -= inode_sector_adjust;
11447c478bd9Sstevel@tonic-gate 		ASSERT((unsigned)ip->i_blocks <= INT_MAX);
11457c478bd9Sstevel@tonic-gate 		TRANS_INODE(ufsvfsp, ip);
11467c478bd9Sstevel@tonic-gate 		ip->i_flag |= IUPD | ICHG | IATTCHG;
11477c478bd9Sstevel@tonic-gate 		ip->i_seq++;
11487c478bd9Sstevel@tonic-gate 		if (!TRANS_ISTRANS(ufsvfsp))
11497c478bd9Sstevel@tonic-gate 			ufs_iupdat(ip, I_SYNC);
11507c478bd9Sstevel@tonic-gate 	}
11517c478bd9Sstevel@tonic-gate 
11527c478bd9Sstevel@tonic-gate 	/*
11537c478bd9Sstevel@tonic-gate 	 * Now we go through and actually free the blocks, but only if we
11547c478bd9Sstevel@tonic-gate 	 * successfully removed the pointers to them.
11557c478bd9Sstevel@tonic-gate 	 */
11567c478bd9Sstevel@tonic-gate 
11577c478bd9Sstevel@tonic-gate 	if (!error_updating_pointers) {
11587c478bd9Sstevel@tonic-gate 		for (i = 0; i < block_count; i++) {
11597c478bd9Sstevel@tonic-gate 			free(ip, table[i].this_block, table[i].block_size,
11607c478bd9Sstevel@tonic-gate 			    table[i].usage_flags);
11617c478bd9Sstevel@tonic-gate 		}
11627c478bd9Sstevel@tonic-gate 	}
11637c478bd9Sstevel@tonic-gate }
11647c478bd9Sstevel@tonic-gate 
11657c478bd9Sstevel@tonic-gate /*
11667c478bd9Sstevel@tonic-gate  * Find the next hole or data block in file starting at *off
1167a6595b40Sperrin  * Return found offset in *off, which can be less than the
1168a6595b40Sperrin  * starting offset if not block aligned.
11697c478bd9Sstevel@tonic-gate  * This code is based on bmap_read().
11707c478bd9Sstevel@tonic-gate  * Errors: ENXIO for end of file
11717c478bd9Sstevel@tonic-gate  *         EIO for block read error.
11727c478bd9Sstevel@tonic-gate  */
11737c478bd9Sstevel@tonic-gate int
11747c478bd9Sstevel@tonic-gate bmap_find(struct inode *ip, boolean_t hole, u_offset_t *off)
11757c478bd9Sstevel@tonic-gate {
11767c478bd9Sstevel@tonic-gate 	ufsvfs_t *ufsvfsp = ip->i_ufsvfs;
11777c478bd9Sstevel@tonic-gate 	struct fs *fs = ufsvfsp->vfs_fs;
11787c478bd9Sstevel@tonic-gate 	buf_t *bp[NIADDR];
11797c478bd9Sstevel@tonic-gate 	int i, j;
11807c478bd9Sstevel@tonic-gate 	int shft;			/* we maintain sh = 1 << shft */
11817c478bd9Sstevel@tonic-gate 	int nindirshift, nindiroffset;
11827c478bd9Sstevel@tonic-gate 	daddr_t	ob, nb, tbn, lbn, skip;
11837c478bd9Sstevel@tonic-gate 	daddr32_t *bap;
11847c478bd9Sstevel@tonic-gate 	u_offset_t isz = (offset_t)ip->i_size;
11857c478bd9Sstevel@tonic-gate 	int32_t bs = fs->fs_bsize; /* file system block size */
11867c478bd9Sstevel@tonic-gate 	int32_t nindir = fs->fs_nindir;
11877c478bd9Sstevel@tonic-gate 	dev_t dev;
11887c478bd9Sstevel@tonic-gate 	int error = 0;
11897c478bd9Sstevel@tonic-gate 	daddr_t limits[NIADDR];
11907c478bd9Sstevel@tonic-gate 
11917c478bd9Sstevel@tonic-gate 	ASSERT(*off < isz);
11927c478bd9Sstevel@tonic-gate 	ASSERT(RW_LOCK_HELD(&ip->i_contents));
11937c478bd9Sstevel@tonic-gate 	lbn = (daddr_t)lblkno(fs, *off);
11947c478bd9Sstevel@tonic-gate 	ASSERT(lbn >= 0);
11957c478bd9Sstevel@tonic-gate 
11967c478bd9Sstevel@tonic-gate 	for (i = 0; i < NIADDR; i++)
11977c478bd9Sstevel@tonic-gate 		bp[i] = NULL;
11987c478bd9Sstevel@tonic-gate 
11997c478bd9Sstevel@tonic-gate 	/*
12007c478bd9Sstevel@tonic-gate 	 * The first NDADDR blocks are direct blocks.
12017c478bd9Sstevel@tonic-gate 	 */
12027c478bd9Sstevel@tonic-gate 	if (lbn < NDADDR) {
12037c478bd9Sstevel@tonic-gate 		for (; lbn < NDADDR; lbn++) {
12047c478bd9Sstevel@tonic-gate 			if ((hole && (ip->i_db[lbn] == 0)) ||
12057c478bd9Sstevel@tonic-gate 			    (!hole && (ip->i_db[lbn] != 0))) {
12067c478bd9Sstevel@tonic-gate 				goto out;
12077c478bd9Sstevel@tonic-gate 			}
12087c478bd9Sstevel@tonic-gate 		}
12097c478bd9Sstevel@tonic-gate 		if ((u_offset_t)lbn << fs->fs_bshift >= isz)
12107c478bd9Sstevel@tonic-gate 			goto out;
12117c478bd9Sstevel@tonic-gate 	}
12127c478bd9Sstevel@tonic-gate 
12137c478bd9Sstevel@tonic-gate 	nindir = fs->fs_nindir;
12147c478bd9Sstevel@tonic-gate 	nindirshift = ufsvfsp->vfs_nindirshift;
12157c478bd9Sstevel@tonic-gate 	nindiroffset = ufsvfsp->vfs_nindiroffset;
12167c478bd9Sstevel@tonic-gate 	dev = ip->i_dev;
12177c478bd9Sstevel@tonic-gate 
12187c478bd9Sstevel@tonic-gate 	/* Set up limits array */
12197c478bd9Sstevel@tonic-gate 	for (limits[0] = NDADDR, j = 1; j  < NIADDR; j++)
12207c478bd9Sstevel@tonic-gate 		limits[j] = limits[j-1] + (1ULL << (nindirshift * j));
12217c478bd9Sstevel@tonic-gate 
12227c478bd9Sstevel@tonic-gate loop:
12237c478bd9Sstevel@tonic-gate 	/*
12247c478bd9Sstevel@tonic-gate 	 * Determine how many levels of indirection.
12257c478bd9Sstevel@tonic-gate 	 */
12267c478bd9Sstevel@tonic-gate 	shft = 0;				/* sh = 1 */
12277c478bd9Sstevel@tonic-gate 	tbn = lbn - NDADDR;
12287c478bd9Sstevel@tonic-gate 	for (j = NIADDR; j > 0; j--) {
12297c478bd9Sstevel@tonic-gate 		longlong_t sh;
12307c478bd9Sstevel@tonic-gate 
12317c478bd9Sstevel@tonic-gate 		shft += nindirshift;		/* sh *= nindir */
12327c478bd9Sstevel@tonic-gate 		sh = 1LL << shft;
12337c478bd9Sstevel@tonic-gate 		if (tbn < sh)
12347c478bd9Sstevel@tonic-gate 			break;
12357c478bd9Sstevel@tonic-gate 		tbn -= sh;
12367c478bd9Sstevel@tonic-gate 	}
12377c478bd9Sstevel@tonic-gate 	if (j == 0) {
12387c478bd9Sstevel@tonic-gate 		/* must have passed end of file */
12397c478bd9Sstevel@tonic-gate 		ASSERT(((u_offset_t)lbn << fs->fs_bshift) >= isz);
12407c478bd9Sstevel@tonic-gate 		goto out;
12417c478bd9Sstevel@tonic-gate 	}
12427c478bd9Sstevel@tonic-gate 
12437c478bd9Sstevel@tonic-gate 	/*
12447c478bd9Sstevel@tonic-gate 	 * Fetch the first indirect block.
12457c478bd9Sstevel@tonic-gate 	 */
12467c478bd9Sstevel@tonic-gate 	nb = ip->i_ib[NIADDR - j];
12477c478bd9Sstevel@tonic-gate 	if (nb == 0) {
12487c478bd9Sstevel@tonic-gate 		if (hole) {
12497c478bd9Sstevel@tonic-gate 			lbn = limits[NIADDR - j];
12507c478bd9Sstevel@tonic-gate 			goto out;
12517c478bd9Sstevel@tonic-gate 		} else {
12527c478bd9Sstevel@tonic-gate 			lbn = limits[NIADDR - j + 1];
12537c478bd9Sstevel@tonic-gate 			if ((u_offset_t)lbn << fs->fs_bshift >= isz)
12547c478bd9Sstevel@tonic-gate 				goto out;
12557c478bd9Sstevel@tonic-gate 			goto loop;
12567c478bd9Sstevel@tonic-gate 		}
12577c478bd9Sstevel@tonic-gate 	}
12587c478bd9Sstevel@tonic-gate 
12597c478bd9Sstevel@tonic-gate 	/*
12607c478bd9Sstevel@tonic-gate 	 * Fetch through the indirect blocks.
12617c478bd9Sstevel@tonic-gate 	 */
12627c478bd9Sstevel@tonic-gate 	for (; ((j <= NIADDR) && (nb != 0)); j++) {
12637c478bd9Sstevel@tonic-gate 		ob = nb;
12647c478bd9Sstevel@tonic-gate 		/*
12657c478bd9Sstevel@tonic-gate 		 * if there's a different block at this level then release
12667c478bd9Sstevel@tonic-gate 		 * the old one and in with the new.
12677c478bd9Sstevel@tonic-gate 		 */
12687c478bd9Sstevel@tonic-gate 		if ((bp[j-1] == NULL) || bp[j-1]->b_blkno != fsbtodb(fs, ob)) {
12697c478bd9Sstevel@tonic-gate 			if (bp[j-1] != NULL)
12707c478bd9Sstevel@tonic-gate 				brelse(bp[j-1]);
12717c478bd9Sstevel@tonic-gate 			bp[j-1] = UFS_BREAD(ufsvfsp, dev, fsbtodb(fs, ob), bs);
12727c478bd9Sstevel@tonic-gate 			if (bp[j-1]->b_flags & B_ERROR) {
12737c478bd9Sstevel@tonic-gate 				error = EIO;
12747c478bd9Sstevel@tonic-gate 				goto out;
12757c478bd9Sstevel@tonic-gate 			}
12767c478bd9Sstevel@tonic-gate 		}
12777c478bd9Sstevel@tonic-gate 		bap = bp[j-1]->b_un.b_daddr;
12787c478bd9Sstevel@tonic-gate 
12797c478bd9Sstevel@tonic-gate 		shft -= nindirshift;		/* sh / nindir */
12807c478bd9Sstevel@tonic-gate 		i = (tbn >> shft) & nindiroffset; /* (tbn / sh) % nindir */
12817c478bd9Sstevel@tonic-gate 		nb = bap[i];
12827c478bd9Sstevel@tonic-gate 		skip = 1LL << (nindirshift * (NIADDR - j));
12837c478bd9Sstevel@tonic-gate 	}
12847c478bd9Sstevel@tonic-gate 
12857c478bd9Sstevel@tonic-gate 	/*
12867c478bd9Sstevel@tonic-gate 	 * Scan through the blocks in this array.
12877c478bd9Sstevel@tonic-gate 	 */
12887c478bd9Sstevel@tonic-gate 	for (; i < nindir; i++, lbn += skip) {
12897c478bd9Sstevel@tonic-gate 		if (hole && (bap[i] == 0))
12907c478bd9Sstevel@tonic-gate 			goto out;
12917c478bd9Sstevel@tonic-gate 		if (!hole && (bap[i] != 0)) {
12927c478bd9Sstevel@tonic-gate 			if (skip == 1) {
12937c478bd9Sstevel@tonic-gate 				/* we're at the lowest level */
12947c478bd9Sstevel@tonic-gate 				goto out;
12957c478bd9Sstevel@tonic-gate 			} else {
12967c478bd9Sstevel@tonic-gate 				goto loop;
12977c478bd9Sstevel@tonic-gate 			}
12987c478bd9Sstevel@tonic-gate 		}
12997c478bd9Sstevel@tonic-gate 	}
13007c478bd9Sstevel@tonic-gate 	if (((u_offset_t)lbn << fs->fs_bshift) < isz)
13017c478bd9Sstevel@tonic-gate 		goto loop;
13027c478bd9Sstevel@tonic-gate out:
13037c478bd9Sstevel@tonic-gate 	for (i = 0; i < NIADDR; i++) {
13047c478bd9Sstevel@tonic-gate 		if (bp[i])
13057c478bd9Sstevel@tonic-gate 			brelse(bp[i]);
13067c478bd9Sstevel@tonic-gate 	}
13077c478bd9Sstevel@tonic-gate 	if (error == 0) {
13087c478bd9Sstevel@tonic-gate 		if (((u_offset_t)lbn << fs->fs_bshift) >= isz) {
13097c478bd9Sstevel@tonic-gate 			error = ENXIO;
13107c478bd9Sstevel@tonic-gate 		} else {
13117c478bd9Sstevel@tonic-gate 			/* success */
13127c478bd9Sstevel@tonic-gate 			*off = (u_offset_t)lbn << fs->fs_bshift;
13137c478bd9Sstevel@tonic-gate 		}
13147c478bd9Sstevel@tonic-gate 	}
13157c478bd9Sstevel@tonic-gate 	return (error);
13167c478bd9Sstevel@tonic-gate }
1317303bf60bSsdebnath 
1318303bf60bSsdebnath /*
1319303bf60bSsdebnath  * Set a particular offset in the inode list to be a certain block.
1320303bf60bSsdebnath  * User is responsible for calling TRANS* functions
1321303bf60bSsdebnath  */
1322303bf60bSsdebnath int
1323303bf60bSsdebnath bmap_set_bn(struct vnode *vp, u_offset_t off, daddr32_t bn)
1324303bf60bSsdebnath {
1325303bf60bSsdebnath 	daddr_t lbn;
1326303bf60bSsdebnath 	struct inode *ip;
1327303bf60bSsdebnath 	ufsvfs_t *ufsvfsp;
1328303bf60bSsdebnath 	struct	fs *fs;
1329303bf60bSsdebnath 	struct	buf *bp;
1330303bf60bSsdebnath 	int	i, j;
1331303bf60bSsdebnath 	int	shft;			/* we maintain sh = 1 << shft */
1332303bf60bSsdebnath 	int err;
1333303bf60bSsdebnath 	daddr_t	ob, nb, tbn;
1334303bf60bSsdebnath 	daddr32_t *bap;
1335303bf60bSsdebnath 	int	nindirshift, nindiroffset;
1336303bf60bSsdebnath 
1337303bf60bSsdebnath 	ip = VTOI(vp);
1338303bf60bSsdebnath 	ufsvfsp = ip->i_ufsvfs;
1339303bf60bSsdebnath 	fs = ufsvfsp->vfs_fs;
1340303bf60bSsdebnath 	lbn = (daddr_t)lblkno(fs, off);
1341303bf60bSsdebnath 
1342303bf60bSsdebnath 	ASSERT(RW_LOCK_HELD(&ip->i_contents));
1343303bf60bSsdebnath 
1344303bf60bSsdebnath 	if (lbn < 0)
1345303bf60bSsdebnath 		return (EFBIG);
1346303bf60bSsdebnath 
1347303bf60bSsdebnath 	/*
1348303bf60bSsdebnath 	 * Take care of direct block assignment
1349303bf60bSsdebnath 	 */
1350303bf60bSsdebnath 	if (lbn < NDADDR) {
1351303bf60bSsdebnath 		ip->i_db[lbn] = bn;
1352303bf60bSsdebnath 		return (0);
1353303bf60bSsdebnath 	}
1354303bf60bSsdebnath 
1355303bf60bSsdebnath 	nindirshift = ip->i_ufsvfs->vfs_nindirshift;
1356303bf60bSsdebnath 	nindiroffset = ip->i_ufsvfs->vfs_nindiroffset;
1357303bf60bSsdebnath 	/*
1358303bf60bSsdebnath 	 * Determine how many levels of indirection.
1359303bf60bSsdebnath 	 */
1360303bf60bSsdebnath 	shft = 0;				/* sh = 1 */
1361303bf60bSsdebnath 	tbn = lbn - NDADDR;
1362303bf60bSsdebnath 	for (j = NIADDR; j > 0; j--) {
1363303bf60bSsdebnath 		longlong_t	sh;
1364303bf60bSsdebnath 
1365303bf60bSsdebnath 		shft += nindirshift;		/* sh *= nindir */
1366303bf60bSsdebnath 		sh = 1LL << shft;
1367303bf60bSsdebnath 		if (tbn < sh)
1368303bf60bSsdebnath 			break;
1369303bf60bSsdebnath 		tbn -= sh;
1370303bf60bSsdebnath 	}
1371303bf60bSsdebnath 	if (j == 0)
1372303bf60bSsdebnath 		return (EFBIG);
1373303bf60bSsdebnath 
1374303bf60bSsdebnath 	/*
1375303bf60bSsdebnath 	 * Fetch the first indirect block.
1376303bf60bSsdebnath 	 */
1377303bf60bSsdebnath 	nb = ip->i_ib[NIADDR - j];
1378*33c22cb3Smishra 	if (nb == 0) {
1379303bf60bSsdebnath 		err = ufs_fault(ITOV(ip), "ufs_set_bn: nb == UFS_HOLE");
1380*33c22cb3Smishra 		return (err);
1381*33c22cb3Smishra 	}
1382303bf60bSsdebnath 
1383303bf60bSsdebnath 	/*
1384303bf60bSsdebnath 	 * Fetch through the indirect blocks.
1385303bf60bSsdebnath 	 */
1386303bf60bSsdebnath 	for (; j <= NIADDR; j++) {
1387303bf60bSsdebnath 		ob = nb;
1388303bf60bSsdebnath 		bp = UFS_BREAD(ufsvfsp,
1389303bf60bSsdebnath 				ip->i_dev, fsbtodb(fs, ob), fs->fs_bsize);
1390303bf60bSsdebnath 		if (bp->b_flags & B_ERROR) {
1391303bf60bSsdebnath 			err = geterror(bp);
1392303bf60bSsdebnath 			brelse(bp);
1393303bf60bSsdebnath 			return (err);
1394303bf60bSsdebnath 		}
1395303bf60bSsdebnath 		bap = bp->b_un.b_daddr;
1396303bf60bSsdebnath 
1397303bf60bSsdebnath 		ASSERT(!ufs_indir_badblock(ip, bap));
1398303bf60bSsdebnath 
1399303bf60bSsdebnath 		shft -= nindirshift;		/* sh / nindir */
1400303bf60bSsdebnath 		i = (tbn >> shft) & nindiroffset; /* (tbn / sh) % nindir */
1401303bf60bSsdebnath 
1402*33c22cb3Smishra 		nb = bap[i];
1403*33c22cb3Smishra 		if (nb == 0) {
1404*33c22cb3Smishra 			err = ufs_fault(ITOV(ip), "ufs_set_bn: nb == UFS_HOLE");
1405*33c22cb3Smishra 			return (err);
1406*33c22cb3Smishra 		}
1407*33c22cb3Smishra 
1408303bf60bSsdebnath 		if (j == NIADDR) {
1409303bf60bSsdebnath 			bap[i] = bn;
1410303bf60bSsdebnath 			bdrwrite(bp);
1411303bf60bSsdebnath 			return (0);
1412303bf60bSsdebnath 		}
1413*33c22cb3Smishra 
1414303bf60bSsdebnath 		brelse(bp);
1415303bf60bSsdebnath 	}
1416303bf60bSsdebnath 	return (0);
1417303bf60bSsdebnath }
1418