17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
533c22cbmishra * Common Development and Distribution License (the "License").
633c22cbmishra * You may not use this file except in compliance with the License.
77c478bdstevel@tonic-gate *
87c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bdstevel@tonic-gate * See the License for the specific language governing permissions
117c478bdstevel@tonic-gate * and limitations under the License.
127c478bdstevel@tonic-gate *
137c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bdstevel@tonic-gate *
197c478bdstevel@tonic-gate * CDDL HEADER END
207c478bdstevel@tonic-gate */
217c478bdstevel@tonic-gate/*
22e7da395Owen Roberts * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bdstevel@tonic-gate * Use is subject to license terms.
247c478bdstevel@tonic-gate */
257c478bdstevel@tonic-gate
267c478bdstevel@tonic-gate/*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
271f563ebToomas Soome/*	  All Rights Reserved	*/
287c478bdstevel@tonic-gate
297c478bdstevel@tonic-gate/*
307c478bdstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988
317c478bdstevel@tonic-gate * The Regents of the University of California
327c478bdstevel@tonic-gate * All Rights Reserved
337c478bdstevel@tonic-gate *
347c478bdstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from
357c478bdstevel@tonic-gate * software developed by the University of California, Berkeley, and its
367c478bdstevel@tonic-gate * contributors.
377c478bdstevel@tonic-gate */
387c478bdstevel@tonic-gate
39303bf60sdebnath#include <sys/condvar_impl.h>
407c478bdstevel@tonic-gate#include <sys/types.h>
417c478bdstevel@tonic-gate#include <sys/t_lock.h>
427c478bdstevel@tonic-gate#include <sys/debug.h>
437c478bdstevel@tonic-gate#include <sys/param.h>
447c478bdstevel@tonic-gate#include <sys/systm.h>
457c478bdstevel@tonic-gate#include <sys/signal.h>
467c478bdstevel@tonic-gate#include <sys/cred.h>
477c478bdstevel@tonic-gate#include <sys/proc.h>
487c478bdstevel@tonic-gate#include <sys/disp.h>
497c478bdstevel@tonic-gate#include <sys/user.h>
507c478bdstevel@tonic-gate#include <sys/buf.h>
517c478bdstevel@tonic-gate#include <sys/vfs.h>
527c478bdstevel@tonic-gate#include <sys/vnode.h>
537c478bdstevel@tonic-gate#include <sys/acl.h>
547c478bdstevel@tonic-gate#include <sys/fs/ufs_fs.h>
557c478bdstevel@tonic-gate#include <sys/fs/ufs_inode.h>
567c478bdstevel@tonic-gate#include <sys/fs/ufs_acl.h>
577c478bdstevel@tonic-gate#include <sys/fs/ufs_bio.h>
587c478bdstevel@tonic-gate#include <sys/fs/ufs_quota.h>
597c478bdstevel@tonic-gate#include <sys/kmem.h>
607c478bdstevel@tonic-gate#include <sys/fs/ufs_trans.h>
617c478bdstevel@tonic-gate#include <sys/fs/ufs_panic.h>
627c478bdstevel@tonic-gate#include <sys/errno.h>
637c478bdstevel@tonic-gate#include <sys/time.h>
647c478bdstevel@tonic-gate#include <sys/sysmacros.h>
657c478bdstevel@tonic-gate#include <sys/file.h>
667c478bdstevel@tonic-gate#include <sys/fcntl.h>
677c478bdstevel@tonic-gate#include <sys/flock.h>
687c478bdstevel@tonic-gate#include <fs/fs_subr.h>
697c478bdstevel@tonic-gate#include <sys/cmn_err.h>
707c478bdstevel@tonic-gate#include <sys/policy.h>
71e7da395Owen Roberts#include <sys/fs/ufs_log.h>
727c478bdstevel@tonic-gate
737c478bdstevel@tonic-gatestatic ino_t	hashalloc();
747c478bdstevel@tonic-gatestatic daddr_t	fragextend();
757c478bdstevel@tonic-gatestatic daddr_t	alloccg();
767c478bdstevel@tonic-gatestatic daddr_t	alloccgblk();
777c478bdstevel@tonic-gatestatic ino_t	ialloccg();
787c478bdstevel@tonic-gatestatic daddr_t	mapsearch();
79e7da395Owen Robertsstatic int	findlogstartcg();
807c478bdstevel@tonic-gate
817c478bdstevel@tonic-gateextern int	inside[], around[];
827c478bdstevel@tonic-gateextern uchar_t	*fragtbl[];
837c478bdstevel@tonic-gatevoid delay();
847c478bdstevel@tonic-gate
857c478bdstevel@tonic-gate/*
867c478bdstevel@tonic-gate * Allocate a block in the file system.
877c478bdstevel@tonic-gate *
887c478bdstevel@tonic-gate * The size of the requested block is given, which must be some
897c478bdstevel@tonic-gate * multiple of fs_fsize and <= fs_bsize.
907c478bdstevel@tonic-gate * A preference may be optionally specified. If a preference is given
917c478bdstevel@tonic-gate * the following hierarchy is used to allocate a block:
927c478bdstevel@tonic-gate *   1) allocate the requested block.
937c478bdstevel@tonic-gate *   2) allocate a rotationally optimal block in the same cylinder.
947c478bdstevel@tonic-gate *   3) allocate a block in the same cylinder group.
957c478bdstevel@tonic-gate *   4) quadratically rehash into other cylinder groups, until an
967c478bdstevel@tonic-gate *	available block is located.
977c478bdstevel@tonic-gate * If no block preference is given the following hierarchy is used
987c478bdstevel@tonic-gate * to allocate a block:
997c478bdstevel@tonic-gate *   1) allocate a block in the cylinder group that contains the
1007c478bdstevel@tonic-gate *	inode for the file.
1017c478bdstevel@tonic-gate *   2) quadratically rehash into other cylinder groups, until an
1027c478bdstevel@tonic-gate *	available block is located.
1037c478bdstevel@tonic-gate */
1047c478bdstevel@tonic-gateint
1057c478bdstevel@tonic-gatealloc(struct inode *ip, daddr_t bpref, int size, daddr_t *bnp, cred_t *cr)
1067c478bdstevel@tonic-gate{
1077c478bdstevel@tonic-gate	struct fs *fs;
1087c478bdstevel@tonic-gate	struct ufsvfs *ufsvfsp;
1097c478bdstevel@tonic-gate	daddr_t bno;
1107c478bdstevel@tonic-gate	int cg;
1117c478bdstevel@tonic-gate	int err;
1127c478bdstevel@tonic-gate	char *errmsg = NULL;
1137c478bdstevel@tonic-gate	size_t len;
114d3d5073Rafael Vanoni	clock_t	now;
1157c478bdstevel@tonic-gate
1167c478bdstevel@tonic-gate	ufsvfsp = ip->i_ufsvfs;
1177c478bdstevel@tonic-gate	fs = ufsvfsp->vfs_fs;
1187c478bdstevel@tonic-gate	if ((unsigned)size > fs->fs_bsize || fragoff(fs, size) != 0) {
119303bf60sdebnath		err = ufs_fault(ITOV(ip), "alloc: bad size, dev = 0x%lx,"
120303bf60sdebnath		    " bsize = %d, size = %d, fs = %s\n",
121303bf60sdebnath		    ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt);
1227c478bdstevel@tonic-gate		return (err);
1237c478bdstevel@tonic-gate	}
1247c478bdstevel@tonic-gate	if (size == fs->fs_bsize && fs->fs_cstotal.cs_nbfree == 0)
1257c478bdstevel@tonic-gate		goto nospace;
1267c478bdstevel@tonic-gate	if (freespace(fs, ufsvfsp) <= 0 &&
1277c478bdstevel@tonic-gate	    secpolicy_fs_minfree(cr, ufsvfsp->vfs_vfs) != 0)
1287c478bdstevel@tonic-gate		goto nospace;
1297c478bdstevel@tonic-gate	err = chkdq(ip, (long)btodb(size), 0, cr, &errmsg, &len);
1307c478bdstevel@tonic-gate	/* Note that may not have err, but may have errmsg */
1317c478bdstevel@tonic-gate	if (errmsg != NULL) {
1327c478bdstevel@tonic-gate		uprintf(errmsg);
1337c478bdstevel@tonic-gate		kmem_free(errmsg, len);
1347c478bdstevel@tonic-gate		errmsg = NULL;
1357c478bdstevel@tonic-gate	}
1367c478bdstevel@tonic-gate	if (err)
1377c478bdstevel@tonic-gate		return (err);
1387c478bdstevel@tonic-gate	if (bpref >= fs->fs_size)
1397c478bdstevel@tonic-gate		bpref = 0;
1407c478bdstevel@tonic-gate	if (bpref == 0)
1417c478bdstevel@tonic-gate		cg = (int)itog(fs, ip->i_number);
1427c478bdstevel@tonic-gate	else
1437c478bdstevel@tonic-gate		cg = dtog(fs, bpref);
1447c478bdstevel@tonic-gate
1457c478bdstevel@tonic-gate	bno = (daddr_t)hashalloc(ip, cg, (long)bpref, size,
1467c478bdstevel@tonic-gate	    (ulong_t (*)())alloccg);
1477c478bdstevel@tonic-gate	if (bno > 0) {
1487c478bdstevel@tonic-gate		*bnp = bno;
1497c478bdstevel@tonic-gate		return (0);
1507c478bdstevel@tonic-gate	}
1517c478bdstevel@tonic-gate
1527c478bdstevel@tonic-gate	/*
1537c478bdstevel@tonic-gate	 * hashalloc() failed because some other thread grabbed
1547c478bdstevel@tonic-gate	 * the last block so unwind the quota operation.  We can
1557c478bdstevel@tonic-gate	 * ignore the return because subtractions don't fail and
1567c478bdstevel@tonic-gate	 * size is guaranteed to be >= zero by our caller.
1577c478bdstevel@tonic-gate	 */
1587c478bdstevel@tonic-gate	(void) chkdq(ip, -(long)btodb(size), 0, cr, (char **)NULL,
15980d3443frankho	    (size_t *)NULL);
1607c478bdstevel@tonic-gate
1617c478bdstevel@tonic-gatenospace:
162d3d5073Rafael Vanoni	now = ddi_get_lbolt();
1637c478bdstevel@tonic-gate	mutex_enter(&ufsvfsp->vfs_lock);
164d3d5073Rafael Vanoni	if ((now - ufsvfsp->vfs_lastwhinetime) > (hz << 2) &&
16580d3443frankho	    (!(TRANS_ISTRANS(ufsvfsp)) || !(ip->i_flag & IQUIET))) {
166d3d5073Rafael Vanoni		ufsvfsp->vfs_lastwhinetime = now;
1677c478bdstevel@tonic-gate		cmn_err(CE_NOTE, "alloc: %s: file system full", fs->fs_fsmnt);
1687c478bdstevel@tonic-gate	}
1697c478bdstevel@tonic-gate	mutex_exit(&ufsvfsp->vfs_lock);
1707c478bdstevel@tonic-gate	return (ENOSPC);
1717c478bdstevel@tonic-gate}
1727c478bdstevel@tonic-gate
1737c478bdstevel@tonic-gate/*
1747c478bdstevel@tonic-gate * Reallocate a fragment to a bigger size
1757c478bdstevel@tonic-gate *
1767c478bdstevel@tonic-gate * The number and size of the old block is given, and a preference
1777c478bdstevel@tonic-gate * and new size is also specified.  The allocator attempts to extend
1787c478bdstevel@tonic-gate * the original block.  Failing that, the regular block allocator is
1797c478bdstevel@tonic-gate * invoked to get an appropriate block.
1807c478bdstevel@tonic-gate */
1817c478bdstevel@tonic-gateint
1827c478bdstevel@tonic-gaterealloccg(struct inode *ip, daddr_t bprev, daddr_t bpref, int osize,
1837c478bdstevel@tonic-gate    int nsize, daddr_t *bnp, cred_t *cr)
1847c478bdstevel@tonic-gate{
1857c478bdstevel@tonic-gate	daddr_t bno;
1867c478bdstevel@tonic-gate	struct fs *fs;
1877c478bdstevel@tonic-gate	struct ufsvfs *ufsvfsp;
1887c478bdstevel@tonic-gate	int cg, request;
1897c478bdstevel@tonic-gate	int err;
1907c478bdstevel@tonic-gate	char *errmsg = NULL;
1917c478bdstevel@tonic-gate	size_t len;
192d3d5073Rafael Vanoni	clock_t	now;
1937c478bdstevel@tonic-gate
1947c478bdstevel@tonic-gate	ufsvfsp = ip->i_ufsvfs;
1957c478bdstevel@tonic-gate	fs = ufsvfsp->vfs_fs;
1967c478bdstevel@tonic-gate	if ((unsigned)osize > fs->fs_bsize || fragoff(fs, osize) != 0 ||
1977c478bdstevel@tonic-gate	    (unsigned)nsize > fs->fs_bsize || fragoff(fs, nsize) != 0) {
1987c478bdstevel@tonic-gate		err = ufs_fault(ITOV(ip),
199303bf60sdebnath		    "realloccg: bad size, dev=0x%lx, bsize=%d, "
200303bf60sdebnath		    "osize=%d, nsize=%d, fs=%s\n",
201303bf60sdebnath		    ip->i_dev, fs->fs_bsize, osize, nsize, fs->fs_fsmnt);
2027c478bdstevel@tonic-gate		return (err);
2037c478bdstevel@tonic-gate	}
2047c478bdstevel@tonic-gate	if (freespace(fs, ufsvfsp) <= 0 &&
2057c478bdstevel@tonic-gate	    secpolicy_fs_minfree(cr, ufsvfsp->vfs_vfs) != 0)
2067c478bdstevel@tonic-gate		goto nospace;
2077c478bdstevel@tonic-gate	if (bprev == 0) {
2087c478bdstevel@tonic-gate		err = ufs_fault(ITOV(ip),
209303bf60sdebnath		    "realloccg: bad bprev, dev = 0x%lx, bsize = %d,"
210303bf60sdebnath		    " bprev = %ld, fs = %s\n", ip->i_dev, fs->fs_bsize, bprev,
2117c478bdstevel@tonic-gate		    fs->fs_fsmnt);
2127c478bdstevel@tonic-gate		return (err);
2137c478bdstevel@tonic-gate	}
2147c478bdstevel@tonic-gate	err = chkdq(ip, (long)btodb(nsize - osize), 0, cr, &errmsg, &len);
2157c478bdstevel@tonic-gate	/* Note that may not have err, but may have errmsg */
2167c478bdstevel@tonic-gate	if (errmsg != NULL) {
2177c478bdstevel@tonic-gate		uprintf(errmsg);
2187c478bdstevel@tonic-gate		kmem_free(errmsg, len);
2197c478bdstevel@tonic-gate		errmsg = NULL;
2207c478bdstevel@tonic-gate	}
2217c478bdstevel@tonic-gate	if (err)
2227c478bdstevel@tonic-gate		return (err);
2237c478bdstevel@tonic-gate	cg = dtog(fs, bprev);
2247c478bdstevel@tonic-gate	bno = fragextend(ip, cg, (long)bprev, osize, nsize);
2257c478bdstevel@tonic-gate	if (bno != 0) {
2267c478bdstevel@tonic-gate		*bnp = bno;
2277c478bdstevel@tonic-gate		return (0);
2287c478bdstevel@tonic-gate	}
2297c478bdstevel@tonic-gate	if (bpref >= fs->fs_size)
2307c478bdstevel@tonic-gate		bpref = 0;
2317c478bdstevel@tonic-gate
2327c478bdstevel@tonic-gate	/*
2337c478bdstevel@tonic-gate	 * When optimizing for time we allocate a full block and
2347c478bdstevel@tonic-gate	 * then only use the upper portion for this request. When
2357c478bdstevel@tonic-gate	 * this file grows again it will grow into the unused portion
2367c478bdstevel@tonic-gate	 * of the block (See fragextend() above).  This saves time
2377c478bdstevel@tonic-gate	 * because an extra disk write would be needed if the frags
2387c478bdstevel@tonic-gate	 * following the current allocation were not free. The extra
2397c478bdstevel@tonic-gate	 * disk write is needed to move the data from its current
2407c478bdstevel@tonic-gate	 * location into the newly allocated position.
2417c478bdstevel@tonic-gate	 *
2427c478bdstevel@tonic-gate	 * When optimizing for space we allocate a run of frags
2437c478bdstevel@tonic-gate	 * that is just the right size for this request.
2447c478bdstevel@tonic-gate	 */
2457c478bdstevel@tonic-gate	request = (fs->fs_optim == FS_OPTTIME) ? fs->fs_bsize : nsize;
2467c478bdstevel@tonic-gate	bno = (daddr_t)hashalloc(ip, cg, (long)bpref, request,
24780d3443frankho	    (ulong_t (*)())alloccg);
2487c478bdstevel@tonic-gate	if (bno > 0) {
2497c478bdstevel@tonic-gate		*bnp = bno;
2507c478bdstevel@tonic-gate		if (nsize < request)
2517c478bdstevel@tonic-gate			(void) free(ip, bno + numfrags(fs, nsize),
2527c478bdstevel@tonic-gate			    (off_t)(request - nsize), I_NOCANCEL);
2537c478bdstevel@tonic-gate		return (0);
2547c478bdstevel@tonic-gate	}
2557c478bdstevel@tonic-gate
2567c478bdstevel@tonic-gate	/*
2577c478bdstevel@tonic-gate	 * hashalloc() failed because some other thread grabbed
2587c478bdstevel@tonic-gate	 * the last block so unwind the quota operation.  We can
2597c478bdstevel@tonic-gate	 * ignore the return because subtractions don't fail, and
2607c478bdstevel@tonic-gate	 * our caller guarantees nsize >= osize.
2617c478bdstevel@tonic-gate	 */
2627c478bdstevel@tonic-gate	(void) chkdq(ip, -(long)btodb(nsize - osize), 0, cr, (char **)NULL,
26380d3443frankho	    (size_t *)NULL);
2647c478bdstevel@tonic-gate
2657c478bdstevel@tonic-gatenospace:
266d3d5073Rafael Vanoni	now = ddi_get_lbolt();
2677c478bdstevel@tonic-gate	mutex_enter(&ufsvfsp->vfs_lock);
268d3d5073Rafael Vanoni	if ((now - ufsvfsp->vfs_lastwhinetime) > (hz << 2) &&
26980d3443frankho	    (!(TRANS_ISTRANS(ufsvfsp)) || !(ip->i_flag & IQUIET))) {
270d3d5073Rafael Vanoni		ufsvfsp->vfs_lastwhinetime = now;
2717c478bdstevel@tonic-gate		cmn_err(CE_NOTE,
27280d3443frankho		    "realloccg %s: file system full", fs->fs_fsmnt);
2737c478bdstevel@tonic-gate	}
2747c478bdstevel@tonic-gate	mutex_exit(&ufsvfsp->vfs_lock);
2757c478bdstevel@tonic-gate	return (ENOSPC);
2767c478bdstevel@tonic-gate}
2777c478bdstevel@tonic-gate
2787c478bdstevel@tonic-gate/*
2797c478bdstevel@tonic-gate * Allocate an inode in the file system.
2807c478bdstevel@tonic-gate *
2817c478bdstevel@tonic-gate * A preference may be optionally specified. If a preference is given
2827c478bdstevel@tonic-gate * the following hierarchy is used to allocate an inode:
2837c478bdstevel@tonic-gate *   1) allocate the requested inode.
2847c478bdstevel@tonic-gate *   2) allocate an inode in the same cylinder group.
2857c478bdstevel@tonic-gate *   3) quadratically rehash into other cylinder groups, until an
2867c478bdstevel@tonic-gate *	available inode is located.
2877c478bdstevel@tonic-gate * If no inode preference is given the following hierarchy is used
2887c478bdstevel@tonic-gate * to allocate an inode:
2897c478bdstevel@tonic-gate *   1) allocate an inode in cylinder group 0.
2907c478bdstevel@tonic-gate *   2) quadratically rehash into other cylinder groups, until an
2917c478bdstevel@tonic-gate *	available inode is located.
2927c478bdstevel@tonic-gate */
2937c478bdstevel@tonic-gateint
2947c478bdstevel@tonic-gateufs_ialloc(struct inode *pip,
2957c478bdstevel@tonic-gate    ino_t ipref, mode_t mode, struct inode **ipp, cred_t *cr)
2967c478bdstevel@tonic-gate{
2977c478bdstevel@tonic-gate	struct inode *ip;
2987c478bdstevel@tonic-gate	struct fs *fs;
2997c478bdstevel@tonic-gate	int cg;
3007c478bdstevel@tonic-gate	ino_t ino;
3017c478bdstevel@tonic-gate	int err;
3027c478bdstevel@tonic-gate	int nifree;
3037c478bdstevel@tonic-gate	struct ufsvfs *ufsvfsp = pip->i_ufsvfs;
3047c478bdstevel@tonic-gate	char *errmsg = NULL;
3057c478bdstevel@tonic-gate	size_t len;
3067c478bdstevel@tonic-gate
3077c478bdstevel@tonic-gate	ASSERT(RW_WRITE_HELD(&pip->i_rwlock));
3087c478bdstevel@tonic-gate	fs = pip->i_fs;
3097c478bdstevel@tonic-gateloop:
3107c478bdstevel@tonic-gate	nifree = fs->fs_cstotal.cs_nifree;
3117c478bdstevel@tonic-gate
3127c478bdstevel@tonic-gate	if (nifree == 0)
3137c478bdstevel@tonic-gate		goto noinodes;
3147c478bdstevel@tonic-gate	/*
3157c478bdstevel@tonic-gate	 * Shadow inodes don't count against a user's inode allocation.
3167c478bdstevel@tonic-gate	 * They are an implementation method and not a resource.
3177c478bdstevel@tonic-gate	 */
3187c478bdstevel@tonic-gate	if ((mode != IFSHAD) && (mode != IFATTRDIR)) {
3197c478bdstevel@tonic-gate		err = chkiq((struct ufsvfs *)ITOV(pip)->v_vfsp->vfs_data,
32080d3443frankho		    /* change */ 1, (struct inode *)NULL, crgetuid(cr), 0,
32180d3443frankho		    cr, &errmsg, &len);
3227c478bdstevel@tonic-gate		/*
3237c478bdstevel@tonic-gate		 * As we haven't acquired any locks yet, dump the message
3247c478bdstevel@tonic-gate		 * now.
3257c478bdstevel@tonic-gate		 */
3267c478bdstevel@tonic-gate		if (errmsg != NULL) {
3277c478bdstevel@tonic-gate			uprintf(errmsg);
3287c478bdstevel@tonic-gate			kmem_free(errmsg, len);
3297c478bdstevel@tonic-gate			errmsg = NULL;
3307c478bdstevel@tonic-gate		}
3317c478bdstevel@tonic-gate		if (err)
3327c478bdstevel@tonic-gate			return (err);
3337c478bdstevel@tonic-gate	}
3347c478bdstevel@tonic-gate
3357c478bdstevel@tonic-gate	if (ipref >= (ulong_t)(fs->fs_ncg * fs->fs_ipg))
3367c478bdstevel@tonic-gate		ipref = 0;
3377c478bdstevel@tonic-gate	cg = (int)itog(fs, ipref);
3387c478bdstevel@tonic-gate	ino = (ino_t)hashalloc(pip, cg, (long)ipref, (int)mode,
3397c478bdstevel@tonic-gate	    (ulong_t (*)())ialloccg);
3407c478bdstevel@tonic-gate	if (ino == 0) {
3417c478bdstevel@tonic-gate		if ((mode != IFSHAD) && (mode != IFATTRDIR)) {
3427c478bdstevel@tonic-gate			/*
3437c478bdstevel@tonic-gate			 * We can safely ignore the return from chkiq()
3447c478bdstevel@tonic-gate			 * because deallocations can only fail if we
3457c478bdstevel@tonic-gate			 * can't get the user's quota info record off
3467c478bdstevel@tonic-gate			 * the disk due to an I/O error.  In that case,
3477c478bdstevel@tonic-gate			 * the quota subsystem is already messed up.
3487c478bdstevel@tonic-gate			 */
3497c478bdstevel@tonic-gate			(void) chkiq(ufsvfsp, /* change */ -1,
35080d3443frankho			    (struct inode *)NULL, crgetuid(cr), 0, cr,
35180d3443frankho			    (char **)NULL, (size_t *)NULL);
3527c478bdstevel@tonic-gate		}
3537c478bdstevel@tonic-gate		goto noinodes;
3547c478bdstevel@tonic-gate	}
3557c478bdstevel@tonic-gate	err = ufs_iget(pip->i_vfs, ino, ipp, cr);
3567c478bdstevel@tonic-gate	if (err) {
3577c478bdstevel@tonic-gate		if ((mode != IFSHAD) && (mode != IFATTRDIR)) {
3587c478bdstevel@tonic-gate			/*
3597c478bdstevel@tonic-gate			 * See above comment about why it is safe to ignore an
3607c478bdstevel@tonic-gate			 * error return here.
3617c478bdstevel@tonic-gate			 */
3627c478bdstevel@tonic-gate			(void) chkiq(ufsvfsp, /* change */ -1,
36380d3443frankho			    (struct inode *)NULL, crgetuid(cr), 0, cr,
36480d3443frankho			    (char **)NULL, (size_t *)NULL);
3657c478bdstevel@tonic-gate		}
3667c478bdstevel@tonic-gate		ufs_ifree(pip, ino, 0);
3677c478bdstevel@tonic-gate		return (err);
3687c478bdstevel@tonic-gate	}
3697c478bdstevel@tonic-gate	ip = *ipp;
3707c478bdstevel@tonic-gate	ASSERT(!ip->i_ufs_acl);
3717c478bdstevel@tonic-gate	ASSERT(!ip->i_dquot);
3727c478bdstevel@tonic-gate	rw_enter(&ip->i_contents, RW_WRITER);
3737c478bdstevel@tonic-gate
3747c478bdstevel@tonic-gate	/*
3757c478bdstevel@tonic-gate	 * Check if we really got a free inode, if not then complain
3767c478bdstevel@tonic-gate	 * and mark the inode ISTALE so that it will be freed by the
3777c478bdstevel@tonic-gate	 * ufs idle thread eventually and will not be sent to ufs_delete().
3787c478bdstevel@tonic-gate	 */
3797c478bdstevel@tonic-gate	if (ip->i_mode || (ip->i_nlink > 0)) {
3807c478bdstevel@tonic-gate		ip->i_flag |= ISTALE;
3817c478bdstevel@tonic-gate		rw_exit(&ip->i_contents);
3827c478bdstevel@tonic-gate		VN_RELE(ITOV(ip));
3837c478bdstevel@tonic-gate		cmn_err(CE_WARN,
38480d3443frankho		    "%s: unexpected allocated inode %d, run fsck(1M)%s",
38580d3443frankho		    fs->fs_fsmnt, (int)ino,
38680d3443frankho		    (TRANS_ISTRANS(ufsvfsp) ? " -o f" : ""));
3877c478bdstevel@tonic-gate		goto loop;
3887c478bdstevel@tonic-gate	}
3897c478bdstevel@tonic-gate
3907c478bdstevel@tonic-gate	/*
3917c478bdstevel@tonic-gate	 * Check the inode has no size or data blocks.
3927c478bdstevel@tonic-gate	 * This could have happened if the truncation failed when
3937c478bdstevel@tonic-gate	 * deleting the inode. It used to be possible for this to occur
3947c478bdstevel@tonic-gate	 * if a block allocation failed when iteratively truncating a
3957c478bdstevel@tonic-gate	 * large file using logging and with a full file system.
3967c478bdstevel@tonic-gate	 * This was fixed with bug fix 4348738. However, truncation may
3977c478bdstevel@tonic-gate	 * still fail on an IO error. So in all cases for safety and
3987c478bdstevel@tonic-gate	 * security we clear out the size; the blocks allocated; and
3997c478bdstevel@tonic-gate	 * pointers to the blocks. This will ultimately cause a fsck
4007c478bdstevel@tonic-gate	 * error of un-accounted for blocks, but its a fairly benign error,
4017c478bdstevel@tonic-gate	 * and possibly the correct thing to do anyway as accesssing those
4027c478bdstevel@tonic-gate	 * blocks agains may lead to more IO errors.
4037c478bdstevel@tonic-gate	 */
4047c478bdstevel@tonic-gate	if (ip->i_size || ip->i_blocks) {
4057c478bdstevel@tonic-gate		int i;
4067c478bdstevel@tonic-gate
4077c478bdstevel@tonic-gate		if (ip->i_size) {
4087c478bdstevel@tonic-gate			cmn_err(CE_WARN,
409303bf60sdebnath			    "%s: free inode %d had size 0x%llx, run fsck(1M)%s",
410303bf60sdebnath			    fs->fs_fsmnt, (int)ino, ip->i_size,
411303bf60sdebnath			    (TRANS_ISTRANS(ufsvfsp) ? " -o f" : ""));
4127c478bdstevel@tonic-gate		}
4137c478bdstevel@tonic-gate		/*
4147c478bdstevel@tonic-gate		 * Clear any garbage left behind.
4157c478bdstevel@tonic-gate		 */
4167c478bdstevel@tonic-gate		ip->i_size = (u_offset_t)0;
4177c478bdstevel@tonic-gate		ip->i_blocks = 0;
4187c478bdstevel@tonic-gate		for (i = 0; i < NDADDR; i++)
4197c478bdstevel@tonic-gate			ip->i_db[i] = 0;
4207c478bdstevel@tonic-gate		for (i = 0; i < NIADDR; i++)
4217c478bdstevel@tonic-gate			ip->i_ib[i] = 0;
4227c478bdstevel@tonic-gate	}
4237c478bdstevel@tonic-gate
4247c478bdstevel@tonic-gate	/*
4257c478bdstevel@tonic-gate	 * Initialize the link count
4267c478bdstevel@tonic-gate	 */
4277c478bdstevel@tonic-gate	ip->i_nlink = 0;
4287c478bdstevel@tonic-gate
4297c478bdstevel@tonic-gate	/*
4307c478bdstevel@tonic-gate	 * Clear the old flags
4317c478bdstevel@tonic-gate	 */
4327c478bdstevel@tonic-gate	ip->i_flag &= IREF;
4337c478bdstevel@tonic-gate
4347c478bdstevel@tonic-gate	/*
4357c478bdstevel@tonic-gate	 * Access times are not really defined if the fs is mounted
4367c478bdstevel@tonic-gate	 * with 'noatime'. But it can cause nfs clients to fail
4377c478bdstevel@tonic-gate	 * open() if the atime is not a legal value. Set a legal value
4387c478bdstevel@tonic-gate	 * here when the inode is allocated.
4397c478bdstevel@tonic-gate	 */
4407c478bdstevel@tonic-gate	if (ufsvfsp->vfs_noatime) {
4417c478bdstevel@tonic-gate		mutex_enter(&ufs_iuniqtime_lock);
4427c478bdstevel@tonic-gate		ip->i_atime = iuniqtime;
4437c478bdstevel@tonic-gate		mutex_exit(&ufs_iuniqtime_lock);
4447c478bdstevel@tonic-gate	}
4457c478bdstevel@tonic-gate	rw_exit(&ip->i_contents);
4467c478bdstevel@tonic-gate	return (0);
4477c478bdstevel@tonic-gatenoinodes:
4487c478bdstevel@tonic-gate	if (!(TRANS_ISTRANS(ufsvfsp)) || !(pip->i_flag & IQUIET))
4497c478bdstevel@tonic-gate		cmn_err(CE_NOTE, "%s: out of inodes\n", fs->fs_fsmnt);
4507c478bdstevel@tonic-gate	return (ENOSPC);
4517c478bdstevel@tonic-gate}
4527c478bdstevel@tonic-gate
4537c478bdstevel@tonic-gate/*
4547c478bdstevel@tonic-gate * Find a cylinder group to place a directory.
4557c478bdstevel@tonic-gate * Returns an inumber within the selected cylinder group.
4567c478bdstevel@tonic-gate * Note, the vfs_lock is not needed as we don't require exact cg summary info.
4577c478bdstevel@tonic-gate *
4587c478bdstevel@tonic-gate * If the switch ufs_close_dirs is set, then the policy is to use
4597c478bdstevel@tonic-gate * the current cg if it has more than 25% free inodes and more
4607c478bdstevel@tonic-gate * than 25% free blocks. Otherwise the cgs are searched from
4617c478bdstevel@tonic-gate * the beginning and the first cg with the same criteria is
4627c478bdstevel@tonic-gate * used. If that is also null then we revert to the old algorithm.
4637c478bdstevel@tonic-gate * This tends to cluster files at the beginning of the disk
4647c478bdstevel@tonic-gate * until the disk gets full.
4657c478bdstevel@tonic-gate *
4667c478bdstevel@tonic-gate * Otherwise if ufs_close_dirs is not set then the original policy is
4677c478bdstevel@tonic-gate * used which is to select from among those cylinder groups with
4687c478bdstevel@tonic-gate * above the average number of free inodes, the one with the smallest
4697c478bdstevel@tonic-gate * number of directories.
4707c478bdstevel@tonic-gate */
4717c478bdstevel@tonic-gate
4727c478bdstevel@tonic-gateint ufs_close_dirs = 1;	/* allocate directories close as possible */
4737c478bdstevel@tonic-gate
4747c478bdstevel@tonic-gateino_t
4757c478bdstevel@tonic-gatedirpref(inode_t *dp)
4767c478bdstevel@tonic-gate{
4777c478bdstevel@tonic-gate	int cg, minndir, mincg, avgifree, mininode, minbpg, ifree;
4787c478bdstevel@tonic-gate	struct fs *fs = dp->i_fs;
4797c478bdstevel@tonic-gate
4807c478bdstevel@tonic-gate	cg = itog(fs, dp->i_number);
4817c478bdstevel@tonic-gate	mininode = fs->fs_ipg >> 2;
4827c478bdstevel@tonic-gate	minbpg = fs->fs_maxbpg >> 2;
4837c478bdstevel@tonic-gate	if (ufs_close_dirs &&
4847c478bdstevel@tonic-gate	    (fs->fs_cs(fs, cg).cs_nifree > mininode) &&
4857c478bdstevel@tonic-gate	    (fs->fs_cs(fs, cg).cs_nbfree > minbpg)) {
4867c478bdstevel@tonic-gate		return (dp->i_number);
4877c478bdstevel@tonic-gate	}
4887c478bdstevel@tonic-gate
4897c478bdstevel@tonic-gate	avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg;
4907c478bdstevel@tonic-gate	minndir = fs->fs_ipg;
4917c478bdstevel@tonic-gate	mincg = 0;
4927c478bdstevel@tonic-gate	for (cg = 0; cg < fs->fs_ncg; cg++) {
4937c478bdstevel@tonic-gate		ifree = fs->fs_cs(fs, cg).cs_nifree;
4947c478bdstevel@tonic-gate		if (ufs_close_dirs &&
4957c478bdstevel@tonic-gate		    (ifree > mininode) &&
4967c478bdstevel@tonic-gate		    (fs->fs_cs(fs, cg).cs_nbfree > minbpg)) {
4977c478bdstevel@tonic-gate			return ((ino_t)(fs->fs_ipg * cg));
4987c478bdstevel@tonic-gate		}
4997c478bdstevel@tonic-gate		if ((fs->fs_cs(fs, cg).cs_ndir < minndir) &&
5007c478bdstevel@tonic-gate		    (ifree >= avgifree)) {
5017c478bdstevel@tonic-gate			mincg = cg;
5027c478bdstevel@tonic-gate			minndir = fs->fs_cs(fs, cg).cs_ndir;
5037c478bdstevel@tonic-gate		}
5047c478bdstevel@tonic-gate	}
5057c478bdstevel@tonic-gate	return ((ino_t)(fs->fs_ipg * mincg));
5067c478bdstevel@tonic-gate}
5077c478bdstevel@tonic-gate
5087c478bdstevel@tonic-gate/*
5097c478bdstevel@tonic-gate * Select the desired position for the next block in a file.  The file is
5107c478bdstevel@tonic-gate * logically divided into sections. The first section is composed of the
5117c478bdstevel@tonic-gate * direct blocks. Each additional section contains fs_maxbpg blocks.
5127c478bdstevel@tonic-gate *
5137c478bdstevel@tonic-gate * If no blocks have been allocated in the first section, the policy is to
5147c478bdstevel@tonic-gate * request a block in the same cylinder group as the inode that describes
5157c478bdstevel@tonic-gate * the file. If no blocks have been allocated in any other section, the
5167c478bdstevel@tonic-gate * policy is to place the section in a cylinder group with a greater than
5177c478bdstevel@tonic-gate * average number of free blocks.  An appropriate cylinder group is found
5187c478bdstevel@tonic-gate * by using a rotor that sweeps the cylinder groups. When a new group of
5197c478bdstevel@tonic-gate * blocks is needed, the sweep begins in the cylinder group following the
5207c478bdstevel@tonic-gate * cylinder group from which the previous allocation was made. The sweep
5217c478bdstevel@tonic-gate * continues until a cylinder group with greater than the average number
5227c478bdstevel@tonic-gate * of free blocks is found. If the allocation is for the first block in an
5237c478bdstevel@tonic-gate * indirect block, the information on the previous allocation is unavailable;
5247c478bdstevel@tonic-gate * here a best guess is made based upon the logical block number being
5257c478bdstevel@tonic-gate * allocated.
5267c478bdstevel@tonic-gate *
5277c478bdstevel@tonic-gate * If a section is already partially allocated, the policy is to
5287c478bdstevel@tonic-gate * contiguously allocate fs_maxcontig blocks.  The end of one of these
5297c478bdstevel@tonic-gate * contiguous blocks and the beginning of the next is physically separated
5307c478bdstevel@tonic-gate * so that the disk head will be in transit between them for at least
5317c478bdstevel@tonic-gate * fs_rotdelay milliseconds.  This is to allow time for the processor to
5327c478bdstevel@tonic-gate * schedule another I/O transfer.
5337c478bdstevel@tonic-gate */
5347c478bdstevel@tonic-gatedaddr_t
5357c478bdstevel@tonic-gateblkpref(struct inode *ip, daddr_t lbn, int indx, daddr32_t *bap)
5367c478bdstevel@tonic-gate{
5377c478bdstevel@tonic-gate	struct fs *fs;
5387c478bdstevel@tonic-gate	struct ufsvfs *ufsvfsp;
5397c478bdstevel@tonic-gate	int cg;
5407c478bdstevel@tonic-gate	int avgbfree, startcg;
5417c478bdstevel@tonic-gate	daddr_t nextblk;
5427c478bdstevel@tonic-gate
5437c478bdstevel@tonic-gate	ufsvfsp = ip->i_ufsvfs;
5447c478bdstevel@tonic-gate	fs = ip->i_fs;
5457c478bdstevel@tonic-gate	if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
5467c478bdstevel@tonic-gate		if (lbn < NDADDR) {
5477c478bdstevel@tonic-gate			cg = itog(fs, ip->i_number);
5487c478bdstevel@tonic-gate			return (fs->fs_fpg * cg + fs->fs_frag);
5497c478bdstevel@tonic-gate		}
5507c478bdstevel@tonic-gate		/*
5517c478bdstevel@tonic-gate		 * Find a cylinder with greater than average
5527c478bdstevel@tonic-gate		 * number of unused data blocks.
5537c478bdstevel@tonic-gate		 */
5547c478bdstevel@tonic-gate		if (indx == 0 || bap[indx - 1] == 0)
5557c478bdstevel@tonic-gate			startcg = itog(fs, ip->i_number) + lbn / fs->fs_maxbpg;
5567c478bdstevel@tonic-gate		else
5577c478bdstevel@tonic-gate			startcg = dtog(fs, bap[indx - 1]) + 1;
5587c478bdstevel@tonic-gate		startcg %= fs->fs_ncg;
5597c478bdstevel@tonic-gate
5607c478bdstevel@tonic-gate		mutex_enter(&ufsvfsp->vfs_lock);
5617c478bdstevel@tonic-gate		avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
5627c478bdstevel@tonic-gate		/*
5637c478bdstevel@tonic-gate		 * used for computing log space for writes/truncs
5647c478bdstevel@tonic-gate		 */
5657c478bdstevel@tonic-gate		ufsvfsp->vfs_avgbfree = avgbfree;
5667c478bdstevel@tonic-gate		for (cg = startcg; cg < fs->fs_ncg; cg++)
5677c478bdstevel@tonic-gate			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
5687c478bdstevel@tonic-gate				fs->fs_cgrotor = cg;
5697c478bdstevel@tonic-gate				mutex_exit(&ufsvfsp->vfs_lock);
5707c478bdstevel@tonic-gate				return (fs->fs_fpg * cg + fs->fs_frag);
5717c478bdstevel@tonic-gate			}
5727c478bdstevel@tonic-gate		for (cg = 0; cg <= startcg; cg++)
5737c478bdstevel@tonic-gate			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
5747c478bdstevel@tonic-gate				fs->fs_cgrotor = cg;
5757c478bdstevel@tonic-gate				mutex_exit(&ufsvfsp->vfs_lock);
5767c478bdstevel@tonic-gate				return (fs->fs_fpg * cg + fs->fs_frag);
5777c478bdstevel@tonic-gate			}
5787c478bdstevel@tonic-gate		mutex_exit(&ufsvfsp->vfs_lock);
5791f563ebToomas Soome		return (0);
5807c478bdstevel@tonic-gate	}
5817c478bdstevel@tonic-gate	/*
5827c478bdstevel@tonic-gate	 * One or more previous blocks have been laid out. If less
5837c478bdstevel@tonic-gate	 * than fs_maxcontig previous blocks are contiguous, the
5847c478bdstevel@tonic-gate	 * next block is requested contiguously, otherwise it is
5857c478bdstevel@tonic-gate	 * requested rotationally delayed by fs_rotdelay milliseconds.
5867c478bdstevel@tonic-gate	 */
587303bf60sdebnath
588303bf60sdebnath	nextblk = bap[indx - 1];
589303bf60sdebnath	/*
590303bf60sdebnath	 * Provision for fallocate to return positive
591303bf60sdebnath	 * blk preference based on last allocation
592303bf60sdebnath	 */
593303bf60sdebnath	if (nextblk < 0 && nextblk != UFS_HOLE) {
594303bf60sdebnath		nextblk = (-bap[indx - 1]) + fs->fs_frag;
595303bf60sdebnath	} else {
596303bf60sdebnath		nextblk = bap[indx - 1] + fs->fs_frag;
597303bf60sdebnath	}
598303bf60sdebnath
599303bf60sdebnath	if (indx > fs->fs_maxcontig && bap[indx - fs->fs_maxcontig] +
600303bf60sdebnath	    blkstofrags(fs, fs->fs_maxcontig) != nextblk) {
6017c478bdstevel@tonic-gate		return (nextblk);
602303bf60sdebnath	}
6037c478bdstevel@tonic-gate	if (fs->fs_rotdelay != 0)
6047c478bdstevel@tonic-gate		/*
6057c478bdstevel@tonic-gate		 * Here we convert ms of delay to frags as:
6067c478bdstevel@tonic-gate		 * (frags) = (ms) * (rev/sec) * (sect/rev) /
6071f563ebToomas Soome		 *	((sect/frag) * (ms/sec))
6087c478bdstevel@tonic-gate		 * then round up to the next block.
6097c478bdstevel@tonic-gate		 */
6107c478bdstevel@tonic-gate		nextblk += roundup(fs->fs_rotdelay * fs->fs_rps * fs->fs_nsect /
6117c478bdstevel@tonic-gate		    (NSPF(fs) * 1000), fs->fs_frag);
6127c478bdstevel@tonic-gate	return (nextblk);
6137c478bdstevel@tonic-gate}
6147c478bdstevel@tonic-gate
6157c478bdstevel@tonic-gate/*
6167c478bdstevel@tonic-gate * Free a block or fragment.
6177c478bdstevel@tonic-gate *
6187c478bdstevel@tonic-gate * The specified block or fragment is placed back in the
6197c478bdstevel@tonic-gate * free map. If a fragment is deallocated, a possible
6207c478bdstevel@tonic-gate * block reassembly is checked.
6217c478bdstevel@tonic-gate */
6227c478bdstevel@tonic-gatevoid
6237c478bdstevel@tonic-gatefree(struct inode *ip, daddr_t bno, off_t size, int flags)
6247c478bdstevel@tonic-gate{
6257c478bdstevel@tonic-gate	struct fs *fs = ip->i_fs;
6267c478bdstevel@tonic-gate	struct ufsvfs *ufsvfsp = ip->i_ufsvfs;
627121be23jkennedy	struct ufs_q *delq = &ufsvfsp->vfs_delete;
628121be23jkennedy	struct ufs_delq_info *delq_info = &ufsvfsp->vfs_delete_info;
6297c478bdstevel@tonic-gate	struct cg *cgp;
6307c478bdstevel@tonic-gate	struct buf *bp;
6317c478bdstevel@tonic-gate	int cg, bmap, bbase;
6327c478bdstevel@tonic-gate	int i;
6337c478bdstevel@tonic-gate	uchar_t *blksfree;
6347c478bdstevel@tonic-gate	int *blktot;
6357c478bdstevel@tonic-gate	short *blks;
6367c478bdstevel@tonic-gate	daddr_t blkno, cylno, rpos;
6377c478bdstevel@tonic-gate
638303bf60sdebnath	/*
639303bf60sdebnath	 * fallocate'd files will have negative block address.
640303bf60sdebnath	 * So negate it again to get original block address.
641303bf60sdebnath	 */
64233c22cbmishra	if (bno < 0 && (bno % fs->fs_frag == 0) && bno != UFS_HOLE) {
643303bf60sdebnath		bno = -bno;
644303bf60sdebnath	}
645303bf60sdebnath
6467c478bdstevel@tonic-gate	if ((unsigned long)size > fs->fs_bsize || fragoff(fs, size) != 0) {
6477c478bdstevel@tonic-gate		(void) ufs_fault(ITOV(ip),
648303bf60sdebnath		    "free: bad size, dev = 0x%lx, bsize = %d, size = %d, "
649303bf60sdebnath		    "fs = %s\n", ip->i_dev, fs->fs_bsize,
650303bf60sdebnath		    (int)size, fs->fs_fsmnt);
6517c478bdstevel@tonic-gate		return;
6527c478bdstevel@tonic-gate	}
6537c478bdstevel@tonic-gate	cg = dtog(fs, bno);
6547c478bdstevel@tonic-gate	ASSERT(!ufs_badblock(ip, bno));
6557c478bdstevel@tonic-gate	bp = UFS_BREAD(ufsvfsp, ip->i_dev, (daddr_t)fsbtodb(fs, cgtod(fs, cg)),
656303bf60sdebnath	    (int)fs->fs_cgsize);
6577c478bdstevel@tonic-gate
6587c478bdstevel@tonic-gate	cgp = bp->b_un.b_cg;
6597c478bdstevel@tonic-gate	if (bp->b_flags & B_ERROR || !cg_chkmagic(cgp)) {
6607c478bdstevel@tonic-gate		brelse(bp);
6617c478bdstevel@tonic-gate		return;
6627c478bdstevel@tonic-gate	}
6637c478bdstevel@tonic-gate
6647c478bdstevel@tonic-gate	if (!(flags & I_NOCANCEL))
6657c478bdstevel@tonic-gate		TRANS_CANCEL(ufsvfsp, ldbtob(fsbtodb(fs, bno)), size, flags);
6667c478bdstevel@tonic-gate	if (flags & (I_DIR|I_IBLK|I_SHAD|I_QUOTA)) {
6677c478bdstevel@tonic-gate		TRANS_MATA_FREE(ufsvfsp, ldbtob(fsbtodb(fs, bno)), size);
6687c478bdstevel@tonic-gate	}
6697c478bdstevel@tonic-gate	blksfree = cg_blksfree(cgp);
6707c478bdstevel@tonic-gate	blktot = cg_blktot(cgp);
6717c478bdstevel@tonic-gate	mutex_enter(&ufsvfsp->vfs_lock);
6727c478bdstevel@tonic-gate	cgp->cg_time = gethrestime_sec();
6737c478bdstevel@tonic-gate	bno = dtogd(fs, bno);
6747c478bdstevel@tonic-gate	if (size == fs->fs_bsize) {
6757c478bdstevel@tonic-gate		blkno = fragstoblks(fs, bno);
6767c478bdstevel@tonic-gate		cylno = cbtocylno(fs, bno);
6777c478bdstevel@tonic-gate		rpos = cbtorpos(ufsvfsp, bno);
6787c478bdstevel@tonic-gate		blks = cg_blks(ufsvfsp, cgp, cylno);
6797c478bdstevel@tonic-gate		if (!isclrblock(fs, blksfree, blkno)) {
6807c478bdstevel@tonic-gate			mutex_exit(&ufsvfsp->vfs_lock);
6817c478bdstevel@tonic-gate			brelse(bp);
6827c478bdstevel@tonic-gate			(void) ufs_fault(ITOV(ip), "free: freeing free block, "
6837c478bdstevel@tonic-gate			    "dev:0x%lx, block:%ld, ino:%lu, fs:%s",
6847c478bdstevel@tonic-gate			    ip->i_dev, bno, ip->i_number, fs->fs_fsmnt);
6857c478bdstevel@tonic-gate			return;
6867c478bdstevel@tonic-gate		}
6877c478bdstevel@tonic-gate		setblock(fs, blksfree, blkno);
6887c478bdstevel@tonic-gate		blks[rpos]++;
6897c478bdstevel@tonic-gate		blktot[cylno]++;
6907c478bdstevel@tonic-gate		cgp->cg_cs.cs_nbfree++;		/* Log below */
6917c478bdstevel@tonic-gate		fs->fs_cstotal.cs_nbfree++;
6927c478bdstevel@tonic-gate		fs->fs_cs(fs, cg).cs_nbfree++;
693121be23jkennedy		if (TRANS_ISTRANS(ufsvfsp) && (flags & I_ACCT)) {
694121be23jkennedy			mutex_enter(&delq->uq_mutex);
695121be23jkennedy			delq_info->delq_unreclaimed_blocks -=
696121be23jkennedy			    btodb(fs->fs_bsize);
697121be23jkennedy			mutex_exit(&delq->uq_mutex);
698121be23jkennedy		}
6997c478bdstevel@tonic-gate	} else {
7007c478bdstevel@tonic-gate		bbase = bno - fragnum(fs, bno);
7017c478bdstevel@tonic-gate		/*
7027c478bdstevel@tonic-gate		 * Decrement the counts associated with the old frags
7037c478bdstevel@tonic-gate		 */
7047c478bdstevel@tonic-gate		bmap = blkmap(fs, blksfree, bbase);
7057c478bdstevel@tonic-gate		fragacct(fs, bmap, cgp->cg_frsum, -1);
7067c478bdstevel@tonic-gate		/*
7077c478bdstevel@tonic-gate		 * Deallocate the fragment
7087c478bdstevel@tonic-gate		 */
7097c478bdstevel@tonic-gate		for (i = 0; i < numfrags(fs, size); i++) {
7107c478bdstevel@tonic-gate			if (isset(blksfree, bno + i)) {
7117c478bdstevel@tonic-gate				brelse(bp);
7127c478bdstevel@tonic-gate				mutex_exit(&ufsvfsp->vfs_lock);
7137c478bdstevel@tonic-gate				(void) ufs_fault(ITOV(ip),
7147c478bdstevel@tonic-gate				    "free: freeing free frag, "
7157c478bdstevel@tonic-gate				    "dev:0x%lx, blk:%ld, cg:%d, "
7167c478bdstevel@tonic-gate				    "ino:%lu, fs:%s",
7177c478bdstevel@tonic-gate				    ip->i_dev,
7187c478bdstevel@tonic-gate				    bno + i,
7197c478bdstevel@tonic-gate				    cgp->cg_cgx,
7207c478bdstevel@tonic-gate				    ip->i_number,
7217c478bdstevel@tonic-gate				    fs->fs_fsmnt);
7227c478bdstevel@tonic-gate				return;
7237c478bdstevel@tonic-gate			}
7247c478bdstevel@tonic-gate			setbit(blksfree, bno + i);
7257c478bdstevel@tonic-gate		}
7267c478bdstevel@tonic-gate		cgp->cg_cs.cs_nffree += i;
7277c478bdstevel@tonic-gate		fs->fs_cstotal.cs_nffree += i;
7287c478bdstevel@tonic-gate		fs->fs_cs(fs, cg).cs_nffree += i;
729121be23jkennedy		if (TRANS_ISTRANS(ufsvfsp) && (flags & I_ACCT)) {
730121be23jkennedy			mutex_enter(&delq->uq_mutex);
731121be23jkennedy			delq_info->delq_unreclaimed_blocks -=
732121be23jkennedy			    btodb(i * fs->fs_fsize);
733121be23jkennedy			mutex_exit(&delq->uq_mutex);
734121be23jkennedy		}
7357c478bdstevel@tonic-gate		/*
7367c478bdstevel@tonic-gate		 * Add back in counts associated with the new frags
7377c478bdstevel@tonic-gate		 */
7387c478bdstevel@tonic-gate		bmap = blkmap(fs, blksfree, bbase);
7397c478bdstevel@tonic-gate		fragacct(fs, bmap, cgp->cg_frsum, 1);
7407c478bdstevel@tonic-gate		/*
7417c478bdstevel@tonic-gate		 * If a complete block has been reassembled, account for it
7427c478bdstevel@tonic-gate		 */
7437c478bdstevel@tonic-gate		blkno = fragstoblks(fs, bbase);
7447c478bdstevel@tonic-gate		if (isblock(fs, blksfree, blkno)) {
7457c478bdstevel@tonic-gate			cylno = cbtocylno(fs, bbase);
7467c478bdstevel@tonic-gate			rpos = cbtorpos(ufsvfsp, bbase);
7477c478bdstevel@tonic-gate			blks = cg_blks(ufsvfsp, cgp, cylno);
7487c478bdstevel@tonic-gate			blks[rpos]++;
7497c478bdstevel@tonic-gate			blktot[cylno]++;
7507c478bdstevel@tonic-gate			cgp->cg_cs.cs_nffree -= fs->fs_frag;
7517c478bdstevel@tonic-gate			fs->fs_cstotal.cs_nffree -= fs->fs_frag;
7527c478bdstevel@tonic-gate			fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag;
7537c478bdstevel@tonic-gate			cgp->cg_cs.cs_nbfree++;
7547c478bdstevel@tonic-gate			fs->fs_cstotal.cs_nbfree++;
7557c478bdstevel@tonic-gate			fs->fs_cs(fs, cg).cs_nbfree++;
7567c478bdstevel@tonic-gate		}
7577c478bdstevel@tonic-gate	}
7587c478bdstevel@tonic-gate	fs->fs_fmod = 1;
7597c478bdstevel@tonic-gate	ufs_notclean(ufsvfsp);
7607c478bdstevel@tonic-gate	TRANS_BUF(ufsvfsp, 0, fs->fs_cgsize, bp, DT_CG);
7617c478bdstevel@tonic-gate	TRANS_SI(ufsvfsp, fs, cg);
7627c478bdstevel@tonic-gate	bdrwrite(bp);
7637c478bdstevel@tonic-gate}
7647c478bdstevel@tonic-gate
7657c478bdstevel@tonic-gate/*
7667c478bdstevel@tonic-gate * Free an inode.
7677c478bdstevel@tonic-gate *
7687c478bdstevel@tonic-gate * The specified inode is placed back in the free map.
7697c478bdstevel@tonic-gate */
7707c478bdstevel@tonic-gatevoid
7717c478bdstevel@tonic-gateufs_ifree(struct inode *ip, ino_t ino, mode_t mode)
7727c478bdstevel@tonic-gate{
7737c478bdstevel@tonic-gate	struct fs *fs = ip->i_fs;
7747c478bdstevel@tonic-gate	struct ufsvfs *ufsvfsp = ip->i_ufsvfs;
7757c478bdstevel@tonic-gate	struct cg *cgp;
7767c478bdstevel@tonic-gate	struct buf *bp;
7777c478bdstevel@tonic-gate	unsigned int inot;
7787c478bdstevel@tonic-gate	int cg;
7797c478bdstevel@tonic-gate	char *iused;
7807c478bdstevel@tonic-gate
7817c478bdstevel@tonic-gate	if (ip->i_number == ino && ip->i_mode != 0) {
7827c478bdstevel@tonic-gate		(void) ufs_fault(ITOV(ip),
7837c478bdstevel@tonic-gate		    "ufs_ifree: illegal mode: (imode) %o, (omode) %o, ino %d, "
7847c478bdstevel@tonic-gate		    "fs = %s\n",
7857c478bdstevel@tonic-gate		    ip->i_mode, mode, (int)ip->i_number, fs->fs_fsmnt);
7867c478bdstevel@tonic-gate		return;
7877c478bdstevel@tonic-gate	}
7887c478bdstevel@tonic-gate	if (ino >= fs->fs_ipg * fs->fs_ncg) {
7897c478bdstevel@tonic-gate		(void) ufs_fault(ITOV(ip),
7907c478bdstevel@tonic-gate		    "ifree: range, dev = 0x%x, ino = %d, fs = %s\n",
7917c478bdstevel@tonic-gate		    (int)ip->i_dev, (int)ino, fs->fs_fsmnt);
7927c478bdstevel@tonic-gate		return;
7937c478bdstevel@tonic-gate	}
7947c478bdstevel@tonic-gate	cg = (int)itog(fs, ino);
7957c478bdstevel@tonic-gate	bp = UFS_BREAD(ufsvfsp, ip->i_dev, (daddr_t)fsbtodb(fs, cgtod(fs, cg)),
796303bf60sdebnath	    (int)fs->fs_cgsize);
7977c478bdstevel@tonic-gate
7987c478bdstevel@tonic-gate	cgp = bp->b_un.b_cg;
7997c478bdstevel@tonic-gate	if (bp->b_flags & B_ERROR || !cg_chkmagic(cgp)) {
8007c478bdstevel@tonic-gate		brelse(bp);
8017c478bdstevel@tonic-gate		return;
8027c478bdstevel@tonic-gate	}
8037c478bdstevel@tonic-gate	mutex_enter(&ufsvfsp->vfs_lock);
8047c478bdstevel@tonic-gate	cgp->cg_time = gethrestime_sec();
8057c478bdstevel@tonic-gate	iused = cg_inosused(cgp);
8067c478bdstevel@tonic-gate	inot = (unsigned int)(ino % (ulong_t)fs->fs_ipg);
8077c478bdstevel@tonic-gate	if (isclr(iused, inot)) {
8087c478bdstevel@tonic-gate		mutex_exit(&ufsvfsp->vfs_lock);
8097c478bdstevel@tonic-gate		brelse(bp);
8107c478bdstevel@tonic-gate		(void) ufs_fault(ITOV(ip), "ufs_ifree: freeing free inode, "
811303bf60sdebnath		    "mode: (imode) %o, (omode) %o, ino:%d, "
812303bf60sdebnath		    "fs:%s",
813303bf60sdebnath		    ip->i_mode, mode, (int)ino, fs->fs_fsmnt);
8147c478bdstevel@tonic-gate		return;
8157c478bdstevel@tonic-gate	}
8167c478bdstevel@tonic-gate	clrbit(iused, inot);
8177c478bdstevel@tonic-gate
8187c478bdstevel@tonic-gate	if (inot < (ulong_t)cgp->cg_irotor)
8197c478bdstevel@tonic-gate		cgp->cg_irotor = inot;
8207c478bdstevel@tonic-gate	cgp->cg_cs.cs_nifree++;
8217c478bdstevel@tonic-gate	fs->fs_cstotal.cs_nifree++;
8227c478bdstevel@tonic-gate	fs->fs_cs(fs, cg).cs_nifree++;
8237c478bdstevel@tonic-gate	if (((mode & IFMT) == IFDIR) || ((mode & IFMT) == IFATTRDIR)) {
8247c478bdstevel@tonic-gate		cgp->cg_cs.cs_ndir--;
8257c478bdstevel@tonic-gate		fs->fs_cstotal.cs_ndir--;
8267c478bdstevel@tonic-gate		fs->fs_cs(fs, cg).cs_ndir--;
8277c478bdstevel@tonic-gate	}
8287c478bdstevel@tonic-gate	fs->fs_fmod = 1;
8297c478bdstevel@tonic-gate	ufs_notclean(ufsvfsp);
8307c478bdstevel@tonic-gate	TRANS_BUF(ufsvfsp, 0, fs->fs_cgsize, bp, DT_CG);
8317c478bdstevel@tonic-gate	TRANS_SI(ufsvfsp, fs, cg);
8327c478bdstevel@tonic-gate	bdrwrite(bp);
8337c478bdstevel@tonic-gate}
8347c478bdstevel@tonic-gate
8357c478bdstevel@tonic-gate/*
8367c478bdstevel@tonic-gate * Implement the cylinder overflow algorithm.
8377c478bdstevel@tonic-gate *
8387c478bdstevel@tonic-gate * The policy implemented by this algorithm is:
8397c478bdstevel@tonic-gate *   1) allocate the block in its requested cylinder group.
8407c478bdstevel@tonic-gate *   2) quadratically rehash on the cylinder group number.
8417c478bdstevel@tonic-gate *   3) brute force search for a free block.
8427c478bdstevel@tonic-gate * The size parameter means size for data blocks, mode for inodes.
8437c478bdstevel@tonic-gate */
8447c478bdstevel@tonic-gatestatic ino_t
8457c478bdstevel@tonic-gatehashalloc(struct inode *ip, int cg, long pref, int size, ulong_t (*allocator)())
8467c478bdstevel@tonic-gate{
8477c478bdstevel@tonic-gate	struct fs *fs;
8487c478bdstevel@tonic-gate	int i;
8497c478bdstevel@tonic-gate	long result;
8507c478bdstevel@tonic-gate	int icg = cg;
8517c478bdstevel@tonic-gate
8527c478bdstevel@tonic-gate	fs = ip->i_fs;
8537c478bdstevel@tonic-gate	/*
8547c478bdstevel@tonic-gate	 * 1: preferred cylinder group
8557c478bdstevel@tonic-gate	 */
8567c478bdstevel@tonic-gate	result = (*allocator)(ip, cg, pref, size);
8577c478bdstevel@tonic-gate	if (result)
8587c478bdstevel@tonic-gate		return (result);
8597c478bdstevel@tonic-gate	/*
8607c478bdstevel@tonic-gate	 * 2: quadratic rehash
8617c478bdstevel@tonic-gate	 */
8627c478bdstevel@tonic-gate	for (i = 1; i < fs->fs_ncg; i *= 2) {
8637c478bdstevel@tonic-gate		cg += i;
8647c478bdstevel@tonic-gate		if (cg >= fs->fs_ncg)
8657c478bdstevel@tonic-gate			cg -= fs->fs_ncg;
8667c478bdstevel@tonic-gate		result = (*allocator)(ip, cg, 0, size);
8677c478bdstevel@tonic-gate		if (result)
8687c478bdstevel@tonic-gate			return (result);
8697c478bdstevel@tonic-gate	}
8707c478bdstevel@tonic-gate	/*
8717c478bdstevel@tonic-gate	 * 3: brute force search
8727c478bdstevel@tonic-gate	 * Note that we start at i == 2, since 0 was checked initially,
8737c478bdstevel@tonic-gate	 * and 1 is always checked in the quadratic rehash.
8747c478bdstevel@tonic-gate	 */
8757c478bdstevel@tonic-gate	cg = (icg + 2) % fs->fs_ncg;
8767c478bdstevel@tonic-gate	for (i = 2; i < fs->fs_ncg; i++) {
8777c478bdstevel@tonic-gate		result = (*allocator)(ip, cg, 0, size);
8787c478bdstevel@tonic-gate		if (result)
8797c478bdstevel@tonic-gate			return (result);
8807c478bdstevel@tonic-gate		cg++;
8817c478bdstevel@tonic-gate		if (cg == fs->fs_ncg)
8827c478bdstevel@tonic-gate			cg = 0;
8837c478bdstevel@tonic-gate	}
8841f563ebToomas Soome	return (0);
8857c478bdstevel@tonic-gate}
8867c478bdstevel@tonic-gate
8877c478bdstevel@tonic-gate/*
8887c478bdstevel@tonic-gate * Determine whether a fragment can be extended.
8897c478bdstevel@tonic-gate *
8907c478bdstevel@tonic-gate * Check to see if the necessary fragments are available, and
8917c478bdstevel@tonic-gate * if they are, allocate them.
8927c478bdstevel@tonic-gate */
8937c478bdstevel@tonic-gatestatic daddr_t
8947c478bdstevel@tonic-gatefragextend(struct inode *ip, int cg, long bprev, int osize, int nsize)
8957c478bdstevel@tonic-gate{
8967c478bdstevel@tonic-gate	struct ufsvfs *ufsvfsp = ip->i_ufsvfs;
8977c478bdstevel@tonic-gate	struct fs *fs = ip->i_fs;
8987c478bdstevel@tonic-gate	struct buf *bp;
8997c478bdstevel@tonic-gate	struct cg *cgp;
9007c478bdstevel@tonic-gate	uchar_t *blksfree;
9017c478bdstevel@tonic-gate	long bno;
9027c478bdstevel@tonic-gate	int frags, bbase;
9037c478bdstevel@tonic-gate	int i, j;
9047c478bdstevel@tonic-gate
9057c478bdstevel@tonic-gate	if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize))
9061f563ebToomas Soome		return (0);
9077c478bdstevel@tonic-gate	frags = numfrags(fs, nsize);
9087c478bdstevel@tonic-gate	bbase = (int)fragnum(fs, bprev);
9097c478bdstevel@tonic-gate	if (bbase > fragnum(fs, (bprev + frags - 1))) {
9107c478bdstevel@tonic-gate		/* cannot extend across a block boundary */
9111f563ebToomas Soome		return (0);
9127c478bdstevel@tonic-gate	}
9137c478bdstevel@tonic-gate
9147c478bdstevel@tonic-gate	bp = UFS_BREAD(ufsvfsp, ip->i_dev, (daddr_t)fsbtodb(fs, cgtod(fs, cg)),
915303bf60sdebnath	    (int)fs->fs_cgsize);
9167c478bdstevel@tonic-gate	cgp = bp->b_un.b_cg;
9177c478bdstevel@tonic-gate	if (bp->b_flags & B_ERROR || !cg_chkmagic(cgp)) {
9187c478bdstevel@tonic-gate		brelse(bp);
9191f563ebToomas Soome		return (0);
9207c478bdstevel@tonic-gate	}
9217c478bdstevel@tonic-gate
9227c478bdstevel@tonic-gate	blksfree = cg_blksfree(cgp);
9237c478bdstevel@tonic-gate	mutex_enter(&ufsvfsp->vfs_lock);
9247c478bdstevel@tonic-gate	bno = dtogd(fs, bprev);
9257c478bdstevel@tonic-gate	for (i = numfrags(fs, osize); i < frags; i++) {
9267c478bdstevel@tonic-gate		if (isclr(blksfree, bno + i)) {
9277c478bdstevel@tonic-gate			mutex_exit(&ufsvfsp->vfs_lock);
9287c478bdstevel@tonic-gate			brelse(bp);
9291f563ebToomas Soome			return (0);
9307c478bdstevel@tonic-gate		}
9317c478bdstevel@tonic-gate		if ((TRANS_ISCANCEL(ufsvfsp, ldbtob(fsbtodb(fs, bprev + i)),
93280d3443frankho		    fs->fs_fsize))) {
9337c478bdstevel@tonic-gate			mutex_exit(&ufsvfsp->vfs_lock);
9347c478bdstevel@tonic-gate			brelse(bp);
9351f563ebToomas Soome			return (0);
9367c478bdstevel@tonic-gate		}
9377c478bdstevel@tonic-gate	}
9387c478bdstevel@tonic-gate
9397c478bdstevel@tonic-gate	cgp->cg_time = gethrestime_sec();
9407c478bdstevel@tonic-gate	/*
9417c478bdstevel@tonic-gate	 * The current fragment can be extended,
9427c478bdstevel@tonic-gate	 * deduct the count on fragment being extended into
9437c478bdstevel@tonic-gate	 * increase the count on the remaining fragment (if any)
9447c478bdstevel@tonic-gate	 * allocate the extended piece.
9457c478bdstevel@tonic-gate	 */
9467c478bdstevel@tonic-gate	for (i = frags; i < fs->fs_frag - bbase; i++)
9477c478bdstevel@tonic-gate		if (isclr(blksfree, bno + i))
9487c478bdstevel@tonic-gate			break;
9497c478bdstevel@tonic-gate	j = i - numfrags(fs, osize);
9507c478bdstevel@tonic-gate	cgp->cg_frsum[j]--;
9517c478bdstevel@tonic-gate	ASSERT(cgp->cg_frsum[j] >= 0);
9527c478bdstevel@tonic-gate	if (i != frags)
9537c478bdstevel@tonic-gate		cgp->cg_frsum[i - frags]++;
9547c478bdstevel@tonic-gate	for (i = numfrags(fs, osize); i < frags; i++) {
9557c478bdstevel@tonic-gate		clrbit(blksfree, bno + i);
9567c478bdstevel@tonic-gate		cgp->cg_cs.cs_nffree--;
9577c478bdstevel@tonic-gate		fs->fs_cs(fs, cg).cs_nffree--;
9587c478bdstevel@tonic-gate		fs->fs_cstotal.cs_nffree--;
9597c478bdstevel@tonic-gate	}
9607c478bdstevel@tonic-gate	fs->fs_fmod = 1;
9617c478bdstevel@tonic-gate	ufs_notclean(ufsvfsp);
9627c478bdstevel@tonic-gate	TRANS_BUF(ufsvfsp, 0, fs->fs_cgsize, bp, DT_CG);
9637c478bdstevel@tonic-gate	TRANS_SI(ufsvfsp, fs, cg);
9647c478bdstevel@tonic-gate	bdrwrite(bp);
9657c478bdstevel@tonic-gate	return ((daddr_t)bprev);
9667c478bdstevel@tonic-gate}
9677c478bdstevel@tonic-gate
9687c478bdstevel@tonic-gate/*
9697c478bdstevel@tonic-gate * Determine whether a block can be allocated.
9707c478bdstevel@tonic-gate *
9717c478bdstevel@tonic-gate * Check to see if a block of the apprpriate size
9727c478bdstevel@tonic-gate * is available, and if it is, allocate it.
9737c478bdstevel@tonic-gate */
9747c478bdstevel@tonic-gatestatic daddr_t
9757c478bdstevel@tonic-gatealloccg(struct inode *ip, int cg, daddr_t bpref, int size)
9767c478bdstevel@tonic-gate{
9777c478bdstevel@tonic-gate	struct ufsvfs *ufsvfsp = ip->i_ufsvfs;
9787c478bdstevel@tonic-gate	struct fs *fs = ip->i_fs;
9797c478bdstevel@tonic-gate	struct buf *bp;
9807c478bdstevel@tonic-gate	struct cg *cgp;
9817c478bdstevel@tonic-gate	uchar_t *blksfree;
9827c478bdstevel@tonic-gate	int bno, frags;
9837c478bdstevel@tonic-gate	int allocsiz;
9847c478bdstevel@tonic-gate	int i;
9857c478bdstevel@tonic-gate
986b314310mishra	/*
987b314310mishra	 * Searching for space could be time expensive so do some
988b314310mishra	 * up front checking to verify that there is actually space
989b314310mishra	 * available (free blocks or free frags).
990b314310mishra	 */
991b314310mishra	if (fs->fs_cs(fs, cg).cs_nbfree == 0) {
992b314310mishra		if (size == fs->fs_bsize)
993b314310mishra			return (0);
994b314310mishra
995b314310mishra		/*
996b314310mishra		 * If there are not enough free frags then return.
997b314310mishra		 */
998b314310mishra		if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, size))
999b314310mishra			return (0);
1000b314310mishra	}
1001b314310mishra
10027c478bdstevel@tonic-gate	bp = UFS_BREAD(ufsvfsp, ip->i_dev, (daddr_t)fsbtodb(fs, cgtod(fs, cg)),
1003303bf60sdebnath	    (int)fs->fs_cgsize);
10047c478bdstevel@tonic-gate
10057c478bdstevel@tonic-gate	cgp = bp->b_un.b_cg;
10067c478bdstevel@tonic-gate	if (bp->b_flags & B_ERROR || !cg_chkmagic(cgp) ||
10077c478bdstevel@tonic-gate	    (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) {
10087c478bdstevel@tonic-gate		brelse(bp);
10097c478bdstevel@tonic-gate		return (0);
10107c478bdstevel@tonic-gate	}
10117c478bdstevel@tonic-gate	blksfree = cg_blksfree(cgp);
10127c478bdstevel@tonic-gate	mutex_enter(&ufsvfsp->vfs_lock);
10137c478bdstevel@tonic-gate	cgp->cg_time = gethrestime_sec();
10147c478bdstevel@tonic-gate	if (size == fs->fs_bsize) {
10157c478bdstevel@tonic-gate		if ((bno = alloccgblk(ufsvfsp, cgp, bpref, bp)) == 0)
10167c478bdstevel@tonic-gate			goto errout;
10177c478bdstevel@tonic-gate		fs->fs_fmod = 1;
10187c478bdstevel@tonic-gate		ufs_notclean(ufsvfsp);
10197c478bdstevel@tonic-gate		TRANS_SI(ufsvfsp, fs, cg);
10207c478bdstevel@tonic-gate		bdrwrite(bp);
10217c478bdstevel@tonic-gate		return (bno);
10227c478bdstevel@tonic-gate	}
10237c478bdstevel@tonic-gate	/*
10240d5abb8Viswanathan Kannappan	 * Check fragment bitmap to see if any fragments are already available.
10250d5abb8Viswanathan Kannappan	 * mapsearch() may fail because the fragment that fits this request
10260d5abb8Viswanathan Kannappan	 * might still be on the cancel list and not available for re-use yet.
10270d5abb8Viswanathan Kannappan	 * Look for a bigger sized fragment to allocate first before we have
10280d5abb8Viswanathan Kannappan	 * to give up and fragment a whole new block eventually.
10297c478bdstevel@tonic-gate	 */
10307c478bdstevel@tonic-gate	frags = numfrags(fs, size);
10310d5abb8Viswanathan Kannappan	allocsiz = frags;
10320d5abb8Viswanathan Kannappannext_size:
10330d5abb8Viswanathan Kannappan	for (; allocsiz < fs->fs_frag; allocsiz++)
10347c478bdstevel@tonic-gate		if (cgp->cg_frsum[allocsiz] != 0)
10357c478bdstevel@tonic-gate			break;
10367c478bdstevel@tonic-gate
10370d5abb8Viswanathan Kannappan	if (allocsiz != fs->fs_frag) {
10387c478bdstevel@tonic-gate		bno = mapsearch(ufsvfsp, cgp, bpref, allocsiz);
10390d5abb8Viswanathan Kannappan		if (bno < 0 && allocsiz < (fs->fs_frag - 1)) {
10400d5abb8Viswanathan Kannappan			allocsiz++;
10410d5abb8Viswanathan Kannappan			goto next_size;
10420d5abb8Viswanathan Kannappan		}
10430d5abb8Viswanathan Kannappan	}
10447c478bdstevel@tonic-gate
10457c478bdstevel@tonic-gate	if (allocsiz == fs->fs_frag || bno < 0) {
10467c478bdstevel@tonic-gate		/*
10477c478bdstevel@tonic-gate		 * No fragments were available, so a block
10487c478bdstevel@tonic-gate		 * will be allocated and hacked up.
10497c478bdstevel@tonic-gate		 */
10507c478bdstevel@tonic-gate		if (cgp->cg_cs.cs_nbfree == 0)
10517c478bdstevel@tonic-gate			goto errout;
10527c478bdstevel@tonic-gate		if ((bno = alloccgblk(ufsvfsp, cgp, bpref, bp)) == 0)
10537c478bdstevel@tonic-gate			goto errout;
10547c478bdstevel@tonic-gate		bpref = dtogd(fs, bno);
10557c478bdstevel@tonic-gate		for (i = frags; i < fs->fs_frag; i++)
10567c478bdstevel@tonic-gate			setbit(blksfree, bpref + i);
10577c478bdstevel@tonic-gate		i = fs->fs_frag - frags;
10587c478bdstevel@tonic-gate		cgp->cg_cs.cs_nffree += i;
10597c478bdstevel@tonic-gate		fs->fs_cstotal.cs_nffree += i;
10607c478bdstevel@tonic-gate		fs->fs_cs(fs, cg).cs_nffree += i;
10617c478bdstevel@tonic-gate		cgp->cg_frsum[i]++;
10627c478bdstevel@tonic-gate		fs->fs_fmod = 1;
10637c478bdstevel@tonic-gate		ufs_notclean(ufsvfsp);
10647c478bdstevel@tonic-gate		TRANS_SI(ufsvfsp, fs, cg);
10657c478bdstevel@tonic-gate		bdrwrite(bp);
10667c478bdstevel@tonic-gate		return (bno);
10677c478bdstevel@tonic-gate	}
10687c478bdstevel@tonic-gate
10697c478bdstevel@tonic-gate	for (i = 0; i < frags; i++)
10707c478bdstevel@tonic-gate		clrbit(blksfree, bno + i);
10717c478bdstevel@tonic-gate	cgp->cg_cs.cs_nffree -= frags;
10727c478bdstevel@tonic-gate	fs->fs_cstotal.cs_nffree -= frags;
10737c478bdstevel@tonic-gate	fs->fs_cs(fs, cg).cs_nffree -= frags;
10747c478bdstevel@tonic-gate	cgp->cg_frsum[allocsiz]--;
10757c478bdstevel@tonic-gate	ASSERT(cgp->cg_frsum[allocsiz] >= 0);
10767c478bdstevel@tonic-gate	if (frags != allocsiz) {
10777c478bdstevel@tonic-gate		cgp->cg_frsum[allocsiz - frags]++;
10787c478bdstevel@tonic-gate	}
10797c478bdstevel@tonic-gate	fs->fs_fmod = 1;
10807c478bdstevel@tonic-gate	ufs_notclean(ufsvfsp);
10817c478bdstevel@tonic-gate	TRANS_BUF(ufsvfsp, 0, fs->fs_cgsize, bp, DT_CG);
10827c478bdstevel@tonic-gate	TRANS_SI(ufsvfsp, fs, cg);
10837c478bdstevel@tonic-gate	bdrwrite(bp);
10847c478bdstevel@tonic-gate	return (cg * fs->fs_fpg + bno);
10857c478bdstevel@tonic-gateerrout:
10867c478bdstevel@tonic-gate	mutex_exit(&ufsvfsp->vfs_lock);
10877c478bdstevel@tonic-gate	brelse(bp);
10887c478bdstevel@tonic-gate	return (0);
10897c478bdstevel@tonic-gate}
10907c478bdstevel@tonic-gate
10917c478bdstevel@tonic-gate/*
10927c478bdstevel@tonic-gate * Allocate a block in a cylinder group.
10937c478bdstevel@tonic-gate *
10947c478bdstevel@tonic-gate * This algorithm implements the following policy:
10957c478bdstevel@tonic-gate *   1) allocate the requested block.
10967c478bdstevel@tonic-gate *   2) allocate a rotationally optimal block in the same cylinder.
10977c478bdstevel@tonic-gate *   3) allocate the next available block on the block rotor for the
10987c478bdstevel@tonic-gate *	specified cylinder group.
10997c478bdstevel@tonic-gate * Note that this routine only allocates fs_bsize blocks; these
11007c478bdstevel@tonic-gate * blocks may be fragmented by the routine that allocates them.
11017c478bdstevel@tonic-gate */
11027c478bdstevel@tonic-gatestatic daddr_t
11037c478bdstevel@tonic-gatealloccgblk(
11047c478bdstevel@tonic-gate	struct ufsvfs *ufsvfsp,
11057c478bdstevel@tonic-gate	struct cg *cgp,
11067c478bdstevel@tonic-gate	daddr_t bpref,
11077c478bdstevel@tonic-gate	struct buf *bp)
11087c478bdstevel@tonic-gate{
11097c478bdstevel@tonic-gate	daddr_t bno;
11107c478bdstevel@tonic-gate	int cylno, pos, delta, rotbl_size;
11117c478bdstevel@tonic-gate	short *cylbp;
11127c478bdstevel@tonic-gate	int i;
11137c478bdstevel@tonic-gate	struct fs *fs;
11147c478bdstevel@tonic-gate	uchar_t *blksfree;
11157c478bdstevel@tonic-gate	daddr_t blkno, rpos, frag;
11167c478bdstevel@tonic-gate	short *blks;
11177c478bdstevel@tonic-gate	int32_t *blktot;
11187c478bdstevel@tonic-gate
11197c478bdstevel@tonic-gate	ASSERT(MUTEX_HELD(&ufsvfsp->vfs_lock));
11207c478bdstevel@tonic-gate	fs = ufsvfsp->vfs_fs;
11217c478bdstevel@tonic-gate	blksfree = cg_blksfree(cgp);
11227c478bdstevel@tonic-gate	if (bpref == 0) {
11237c478bdstevel@tonic-gate		bpref = cgp->cg_rotor;
11247c478bdstevel@tonic-gate		goto norot;
11257c478bdstevel@tonic-gate	}
11267c478bdstevel@tonic-gate	bpref = blknum(fs, bpref);
11277c478bdstevel@tonic-gate	bpref = dtogd(fs, bpref);
11287c478bdstevel@tonic-gate	/*
11297c478bdstevel@tonic-gate	 * If the requested block is available, use it.
11307c478bdstevel@tonic-gate	 */
11317c478bdstevel@tonic-gate	if (isblock(fs, blksfree, (daddr_t)fragstoblks(fs, bpref))) {
11327c478bdstevel@tonic-gate		bno = bpref;
11337c478bdstevel@tonic-gate		goto gotit;
11347c478bdstevel@tonic-gate	}
11357c478bdstevel@tonic-gate	/*
11367c478bdstevel@tonic-gate	 * Check for a block available on the same cylinder.
11377c478bdstevel@tonic-gate	 */
11387c478bdstevel@tonic-gate	cylno = cbtocylno(fs, bpref);
11397c478bdstevel@tonic-gate	if (cg_blktot(cgp)[cylno] == 0)
11407c478bdstevel@tonic-gate		goto norot;
11417c478bdstevel@tonic-gate	if (fs->fs_cpc == 0) {
11427c478bdstevel@tonic-gate		/*
11437c478bdstevel@tonic-gate		 * Block layout info is not available, so just
11447c478bdstevel@tonic-gate		 * have to take any block in this cylinder.
11457c478bdstevel@tonic-gate		 */
11467c478bdstevel@tonic-gate		bpref = howmany(fs->fs_spc * cylno, NSPF(fs));
11477c478bdstevel@tonic-gate		goto norot;
11487c478bdstevel@tonic-gate	}
11497c478bdstevel@tonic-gate	/*
11507c478bdstevel@tonic-gate	 * Check the summary information to see if a block is
11517c478bdstevel@tonic-gate	 * available in the requested cylinder starting at the
11527c478bdstevel@tonic-gate	 * requested rotational position and proceeding around.
11537c478bdstevel@tonic-gate	 */
11547c478bdstevel@tonic-gate	cylbp = cg_blks(ufsvfsp, cgp, cylno);
11557c478bdstevel@tonic-gate	pos = cbtorpos(ufsvfsp, bpref);
11567c478bdstevel@tonic-gate	for (i = pos; i < ufsvfsp->vfs_nrpos; i++)
11577c478bdstevel@tonic-gate		if (cylbp[i] > 0)
11587c478bdstevel@tonic-gate			break;
11597c478bdstevel@tonic-gate	if (i == ufsvfsp->vfs_nrpos)
11607c478bdstevel@tonic-gate		for (i = 0; i < pos; i++)
11617c478bdstevel@tonic-gate			if (cylbp[i] > 0)
11627c478bdstevel@tonic-gate				break;
11637c478bdstevel@tonic-gate	if (cylbp[i] > 0) {
11647c478bdstevel@tonic-gate		/*
11657c478bdstevel@tonic-gate		 * Found a rotational position, now find the actual
11667c478bdstevel@tonic-gate		 * block.  A "panic" if none is actually there.
11677c478bdstevel@tonic-gate		 */
11687c478bdstevel@tonic-gate
11697c478bdstevel@tonic-gate		/*
11707c478bdstevel@tonic-gate		 * Up to this point, "pos" has referred to the rotational
11717c478bdstevel@tonic-gate		 * position of the desired block.  From now on, it holds
11727c478bdstevel@tonic-gate		 * the offset of the current cylinder within a cylinder
11737c478bdstevel@tonic-gate		 * cycle.  (A cylinder cycle refers to a set of cylinders
11747c478bdstevel@tonic-gate		 * which are described by a single rotational table; the
11757c478bdstevel@tonic-gate		 * size of the cycle is fs_cpc.)
11767c478bdstevel@tonic-gate		 *
11777c478bdstevel@tonic-gate		 * bno is set to the block number of the first block within
11787c478bdstevel@tonic-gate		 * the current cylinder cycle.
11797c478bdstevel@tonic-gate		 */
11807c478bdstevel@tonic-gate
11817c478bdstevel@tonic-gate		pos = cylno % fs->fs_cpc;
11827c478bdstevel@tonic-gate		bno = (cylno - pos) * fs->fs_spc / NSPB(fs);
11837c478bdstevel@tonic-gate
11847c478bdstevel@tonic-gate		/*
11857c478bdstevel@tonic-gate		 * The blocks within a cylinder are grouped into equivalence
11867c478bdstevel@tonic-gate		 * classes according to their "rotational position."  There
11877c478bdstevel@tonic-gate		 * are two tables used to determine these classes.
11887c478bdstevel@tonic-gate		 *
11897c478bdstevel@tonic-gate		 * The positional offset table (fs_postbl) has an entry for
11907c478bdstevel@tonic-gate		 * each rotational position of each cylinder in a cylinder
11917c478bdstevel@tonic-gate		 * cycle.  This entry contains the relative block number
11927c478bdstevel@tonic-gate		 * (counting from the start of the cylinder cycle) of the
11937c478bdstevel@tonic-gate		 * first block in the equivalence class for that position
11947c478bdstevel@tonic-gate		 * and that cylinder.  Positions for which no blocks exist
11957c478bdstevel@tonic-gate		 * are indicated by a -1.
11967c478bdstevel@tonic-gate		 *
11977c478bdstevel@tonic-gate		 * The rotational delta table (fs_rotbl) has an entry for
11987c478bdstevel@tonic-gate		 * each block in a cylinder cycle.  This entry contains
11997c478bdstevel@tonic-gate		 * the offset from that block to the next block in the
12007c478bdstevel@tonic-gate		 * same equivalence class.  The last block in the class
12017c478bdstevel@tonic-gate		 * is indicated by a zero in the table.
12027c478bdstevel@tonic-gate		 *
12037c478bdstevel@tonic-gate		 * The following code, then, walks through all of the blocks
12047c478bdstevel@tonic-gate		 * in the cylinder (cylno) which we're allocating within
12057c478bdstevel@tonic-gate		 * which are in the equivalence class for the rotational
12067c478bdstevel@tonic-gate		 * position (i) which we're allocating within.
12077c478bdstevel@tonic-gate		 */
12087c478bdstevel@tonic-gate
12097c478bdstevel@tonic-gate		if (fs_postbl(ufsvfsp, pos)[i] == -1) {
12107c478bdstevel@tonic-gate			(void) ufs_fault(ufsvfsp->vfs_root,
1211303bf60sdebnath			    "alloccgblk: cyl groups corrupted, pos = %d, "
1212303bf60sdebnath			    "i = %d, fs = %s\n", pos, i, fs->fs_fsmnt);
12137c478bdstevel@tonic-gate			return (0);
12147c478bdstevel@tonic-gate		}
12157c478bdstevel@tonic-gate
12167c478bdstevel@tonic-gate		/*
12177c478bdstevel@tonic-gate		 * There is one entry in the rotational table for each block
12187c478bdstevel@tonic-gate		 * in the cylinder cycle.  These are whole blocks, not frags.
12197c478bdstevel@tonic-gate		 */
12207c478bdstevel@tonic-gate
12217c478bdstevel@tonic-gate		rotbl_size = (fs->fs_cpc * fs->fs_spc) >>
12227c478bdstevel@tonic-gate		    (fs->fs_fragshift + fs->fs_fsbtodb);
12237c478bdstevel@tonic-gate
12247c478bdstevel@tonic-gate		/*
12257c478bdstevel@tonic-gate		 * As we start, "i" is the rotational position within which
12267c478bdstevel@tonic-gate		 * we're searching.  After the next line, it will be a block
12277c478bdstevel@tonic-gate		 * number (relative to the start of the cylinder cycle)
12287c478bdstevel@tonic-gate		 * within the equivalence class of that rotational position.
12297c478bdstevel@tonic-gate		 */
12307c478bdstevel@tonic-gate
12317c478bdstevel@tonic-gate		i = fs_postbl(ufsvfsp, pos)[i];
12327c478bdstevel@tonic-gate
12337c478bdstevel@tonic-gate		for (;;) {
12347c478bdstevel@tonic-gate			if (isblock(fs, blksfree, (daddr_t)(bno + i))) {
12357c478bdstevel@tonic-gate				bno = blkstofrags(fs, (bno + i));
12367c478bdstevel@tonic-gate				goto gotit;
12377c478bdstevel@tonic-gate			}
12387c478bdstevel@tonic-gate			delta = fs_rotbl(fs)[i];
12397c478bdstevel@tonic-gate			if (delta <= 0 ||		/* End of chain, or */
12407c478bdstevel@tonic-gate			    delta + i > rotbl_size)	/* end of table? */
12417c478bdstevel@tonic-gate				break;			/* If so, panic. */
12427c478bdstevel@tonic-gate			i += delta;
12437c478bdstevel@tonic-gate		}
12447c478bdstevel@tonic-gate		(void) ufs_fault(ufsvfsp->vfs_root,
1245303bf60sdebnath		    "alloccgblk: can't find blk in cyl, pos:%d, i:%d, "
1246303bf60sdebnath		    "fs:%s bno: %x\n", pos, i, fs->fs_fsmnt, (int)bno);
12477c478bdstevel@tonic-gate		return (0);
12487c478bdstevel@tonic-gate	}
12497c478bdstevel@tonic-gatenorot:
12507c478bdstevel@tonic-gate	/*
12517c478bdstevel@tonic-gate	 * No blocks in the requested cylinder, so take
12527c478bdstevel@tonic-gate	 * next available one in this cylinder group.
12537c478bdstevel@tonic-gate	 */
12547c478bdstevel@tonic-gate	bno = mapsearch(ufsvfsp, cgp, bpref, (int)fs->fs_frag);
12557c478bdstevel@tonic-gate	if (bno < 0)
12567c478bdstevel@tonic-gate		return (0);
12577c478bdstevel@tonic-gate	cgp->cg_rotor = bno;
12587c478bdstevel@tonic-gategotit:
12597c478bdstevel@tonic-gate	blkno = fragstoblks(fs, bno);
12607c478bdstevel@tonic-gate	frag = (cgp->cg_cgx * fs->fs_fpg) + bno;
12617c478bdstevel@tonic-gate	if (TRANS_ISCANCEL(ufsvfsp, ldbtob(fsbtodb(fs, frag)), fs->fs_bsize))
12627c478bdstevel@tonic-gate		goto norot;
12637c478bdstevel@tonic-gate	clrblock(fs, blksfree, (long)blkno);
12647c478bdstevel@tonic-gate	/*
12657c478bdstevel@tonic-gate	 * the other cg/sb/si fields are TRANS'ed by the caller
12667c478bdstevel@tonic-gate	 */
12677c478bdstevel@tonic-gate	cgp->cg_cs.cs_nbfree--;
12687c478bdstevel@tonic-gate	fs->fs_cstotal.cs_nbfree--;
12697c478bdstevel@tonic-gate	fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--;
12707c478bdstevel@tonic-gate	cylno = cbtocylno(fs, bno);
12717c478bdstevel@tonic-gate	blks = cg_blks(ufsvfsp, cgp, cylno);
12727c478bdstevel@tonic-gate	rpos = cbtorpos(ufsvfsp, bno);
12737c478bdstevel@tonic-gate	blktot = cg_blktot(cgp);
12747c478bdstevel@tonic-gate	blks[rpos]--;
12757c478bdstevel@tonic-gate	blktot[cylno]--;
12767c478bdstevel@tonic-gate	TRANS_BUF(ufsvfsp, 0, fs->fs_cgsize, bp, DT_CG);
12777c478bdstevel@tonic-gate	fs->fs_fmod = 1;
12787c478bdstevel@tonic-gate	return (frag);
12797c478bdstevel@tonic-gate}
12807c478bdstevel@tonic-gate
12817c478bdstevel@tonic-gate/*
12827c478bdstevel@tonic-gate * Determine whether an inode can be allocated.
12837c478bdstevel@tonic-gate *
12847c478bdstevel@tonic-gate * Check to see if an inode is available, and if it is,
12857c478bdstevel@tonic-gate * allocate it using the following policy:
12867c478bdstevel@tonic-gate *   1) allocate the requested inode.
12877c478bdstevel@tonic-gate *   2) allocate the next available inode after the requested
12887c478bdstevel@tonic-gate *	inode in the specified cylinder group.
12897c478bdstevel@tonic-gate */
12907c478bdstevel@tonic-gatestatic ino_t
12917c478bdstevel@tonic-gateialloccg(struct inode *ip, int cg, daddr_t ipref, int mode)
12927c478bdstevel@tonic-gate{
12937c478bdstevel@tonic-gate	struct ufsvfs *ufsvfsp = ip->i_ufsvfs;
12947c478bdstevel@tonic-gate	struct fs *fs = ip->i_fs;
12957c478bdstevel@tonic-gate	struct cg *cgp;
12967c478bdstevel@tonic-gate	struct buf *bp;
12977c478bdstevel@tonic-gate	int start, len, loc, map, i;
12987c478bdstevel@tonic-gate	char *iused;
12997c478bdstevel@tonic-gate
13007c478bdstevel@tonic-gate	if (fs->fs_cs(fs, cg).cs_nifree == 0)
13017c478bdstevel@tonic-gate		return (0);
13027c478bdstevel@tonic-gate	bp = UFS_BREAD(ufsvfsp, ip->i_dev, (daddr_t)fsbtodb(fs, cgtod(fs, cg)),
130380d3443frankho	    (int)fs->fs_cgsize);
13047c478bdstevel@tonic-gate
13057c478bdstevel@tonic-gate	cgp = bp->b_un.b_cg;
13067c478bdstevel@tonic-gate	if (bp->b_flags & B_ERROR || !cg_chkmagic(cgp) ||
13077c478bdstevel@tonic-gate	    cgp->cg_cs.cs_nifree == 0) {
13087c478bdstevel@tonic-gate		brelse(bp);
13097c478bdstevel@tonic-gate		return (0);
13107c478bdstevel@tonic-gate	}
13117c478bdstevel@tonic-gate	iused = cg_inosused(cgp);
13127c478bdstevel@tonic-gate	mutex_enter(&ufsvfsp->vfs_lock);
13137c478bdstevel@tonic-gate	/*
13147c478bdstevel@tonic-gate	 * While we are waiting for the mutex, someone may have taken
13157c478bdstevel@tonic-gate	 * the last available inode.  Need to recheck.
13167c478bdstevel@tonic-gate	 */
13177c478bdstevel@tonic-gate	if (cgp->cg_cs.cs_nifree == 0) {
13187c478bdstevel@tonic-gate		mutex_exit(&ufsvfsp->vfs_lock);
13197c478bdstevel@tonic-gate		brelse(bp);
13207c478bdstevel@tonic-gate		return (0);
13217c478bdstevel@tonic-gate	}
13227c478bdstevel@tonic-gate
13237c478bdstevel@tonic-gate	cgp->cg_time = gethrestime_sec();
13247c478bdstevel@tonic-gate	if (ipref) {
13257c478bdstevel@tonic-gate		ipref %= fs->fs_ipg;
13267c478bdstevel@tonic-gate		if (isclr(iused, ipref))
13277c478bdstevel@tonic-gate			goto gotit;
13287c478bdstevel@tonic-gate	}
13297c478bdstevel@tonic-gate	start = cgp->cg_irotor / NBBY;
13307c478bdstevel@tonic-gate	len = howmany(fs->fs_ipg - cgp->cg_irotor, NBBY);
13317c478bdstevel@tonic-gate	loc = skpc(0xff, (uint_t)len, &iused[start]);
13327c478bdstevel@tonic-gate	if (loc == 0) {
13337c478bdstevel@tonic-gate		len = start + 1;
13347c478bdstevel@tonic-gate		start = 0;
13357c478bdstevel@tonic-gate		loc = skpc(0xff, (uint_t)len, &iused[0]);
13367c478bdstevel@tonic-gate		if (loc == 0) {
13377c478bdstevel@tonic-gate			mutex_exit(&ufsvfsp->vfs_lock);
13387c478bdstevel@tonic-gate			(void) ufs_fault(ITOV(ip),
1339303bf60sdebnath			    "ialloccg: map corrupted, cg = %d, irotor = %d, "
1340303bf60sdebnath			    "fs = %s\n", cg, (int)cgp->cg_irotor, fs->fs_fsmnt);
13417c478bdstevel@tonic-gate			return (0);
13427c478bdstevel@tonic-gate		}
13437c478bdstevel@tonic-gate	}
13447c478bdstevel@tonic-gate	i = start + len - loc;
13457c478bdstevel@tonic-gate	map = iused[i];
13467c478bdstevel@tonic-gate	ipref = i * NBBY;
13477c478bdstevel@tonic-gate	for (i = 1; i < (1 << NBBY); i <<= 1, ipref++) {
13487c478bdstevel@tonic-gate		if ((map & i) == 0) {
13497c478bdstevel@tonic-gate			cgp->cg_irotor = ipref;
13507c478bdstevel@tonic-gate			goto gotit;
13517c478bdstevel@tonic-gate		}
13527c478bdstevel@tonic-gate	}
13537c478bdstevel@tonic-gate
13547c478bdstevel@tonic-gate	mutex_exit(&ufsvfsp->vfs_lock);
13557c478bdstevel@tonic-gate	(void) ufs_fault(ITOV(ip), "ialloccg: block not in mapfs = %s",
135680d3443frankho	    fs->fs_fsmnt);
13577c478bdstevel@tonic-gate	return (0);
13587c478bdstevel@tonic-gategotit:
13597c478bdstevel@tonic-gate	setbit(iused, ipref);
13607c478bdstevel@tonic-gate	cgp->cg_cs.cs_nifree--;
13617c478bdstevel@tonic-gate	fs->fs_cstotal.cs_nifree--;
13627c478bdstevel@tonic-gate	fs->fs_cs(fs, cg).cs_nifree--;
13637c478bdstevel@tonic-gate	if (((mode & IFMT) == IFDIR) || ((mode & IFMT) == IFATTRDIR)) {
13647c478bdstevel@tonic-gate		cgp->cg_cs.cs_ndir++;
13657c478bdstevel@tonic-gate		fs->fs_cstotal.cs_ndir++;
13667c478bdstevel@tonic-gate		fs->fs_cs(fs, cg).cs_ndir++;
13677c478bdstevel@tonic-gate	}
13687c478bdstevel@tonic-gate	fs->fs_fmod = 1;
13697c478bdstevel@tonic-gate	ufs_notclean(ufsvfsp);
13707c478bdstevel@tonic-gate	TRANS_BUF(ufsvfsp, 0, fs->fs_cgsize, bp, DT_CG);
13717c478bdstevel@tonic-gate	TRANS_SI(ufsvfsp, fs, cg);
13727c478bdstevel@tonic-gate	bdrwrite(bp);
13737c478bdstevel@tonic-gate	return (cg * fs->fs_ipg + ipref);
13747c478bdstevel@tonic-gate}
13757c478bdstevel@tonic-gate
13767c478bdstevel@tonic-gate/*
13777c478bdstevel@tonic-gate * Find a block of the specified size in the specified cylinder group.
13787c478bdstevel@tonic-gate *
13797c478bdstevel@tonic-gate * It is a panic if a request is made to find a block if none are
13807c478bdstevel@tonic-gate * available.
13817c478bdstevel@tonic-gate */
13827c478bdstevel@tonic-gatestatic daddr_t
13831f563ebToomas Soomemapsearch(struct ufsvfs *ufsvfsp, struct cg *cgp, daddr_t bpref, int allocsiz)
13847c478bdstevel@tonic-gate{
13857c478bdstevel@tonic-gate	struct fs *fs	= ufsvfsp->vfs_fs;
13867c478bdstevel@tonic-gate	daddr_t bno, cfrag;
13877c478bdstevel@tonic-gate	int start, len, loc, i, last, first, secondtime;
13887c478bdstevel@tonic-gate	int blk, field, subfield, pos;
13897c478bdstevel@tonic-gate	int gotit;
13907c478bdstevel@tonic-gate
13917c478bdstevel@tonic-gate	/*
13927c478bdstevel@tonic-gate	 * ufsvfs->vfs_lock is held when calling this.
13937c478bdstevel@tonic-gate	 */
13947c478bdstevel@tonic-gate	/*
13957c478bdstevel@tonic-gate	 * Find the fragment by searching through the
13967c478bdstevel@tonic-gate	 * free block map for an appropriate bit pattern.
13977c478bdstevel@tonic-gate	 */
13987c478bdstevel@tonic-gate	if (bpref)
13997c478bdstevel@tonic-gate		start = dtogd(fs, bpref) / NBBY;
14007c478bdstevel@tonic-gate	else
14017c478bdstevel@tonic-gate		start = cgp->cg_frotor / NBBY;
14027c478bdstevel@tonic-gate	/*
14037c478bdstevel@tonic-gate	 * the following loop performs two scans -- the first scan
14047c478bdstevel@tonic-gate	 * searches the bottom half of the array for a match and the
14057c478bdstevel@tonic-gate	 * second scan searches the top half of the array.  The loops
14067c478bdstevel@tonic-gate	 * have been merged just to make things difficult.
14077c478bdstevel@tonic-gate	 */
14087c478bdstevel@tonic-gate	first = start;
14097c478bdstevel@tonic-gate	last = howmany(fs->fs_fpg, NBBY);
14107c478bdstevel@tonic-gate	secondtime = 0;
14117c478bdstevel@tonic-gate	cfrag = cgp->cg_cgx * fs->fs_fpg;
14127c478bdstevel@tonic-gate	while (first < last) {
14137c478bdstevel@tonic-gate		len = last - first;
14147c478bdstevel@tonic-gate		/*
14157c478bdstevel@tonic-gate		 * search the array for a match
14167c478bdstevel@tonic-gate		 */
14177c478bdstevel@tonic-gate		loc = scanc((unsigned)len, (uchar_t *)&cg_blksfree(cgp)[first],
141880d3443frankho		    (uchar_t *)fragtbl[fs->fs_frag],
141980d3443frankho		    (int)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
14207c478bdstevel@tonic-gate		/*
14217c478bdstevel@tonic-gate		 * match found
14227c478bdstevel@tonic-gate		 */
14237c478bdstevel@tonic-gate		if (loc) {
14247c478bdstevel@tonic-gate			bno = (last - loc) * NBBY;
14257c478bdstevel@tonic-gate
14267c478bdstevel@tonic-gate			/*
14277c478bdstevel@tonic-gate			 * Found the byte in the map, sift
14287c478bdstevel@tonic-gate			 * through the bits to find the selected frag
14297c478bdstevel@tonic-gate			 */
14307c478bdstevel@tonic-gate			cgp->cg_frotor = bno;
14317c478bdstevel@tonic-gate			gotit = 0;
14327c478bdstevel@tonic-gate			for (i = bno + NBBY; bno < i; bno += fs->fs_frag) {
14337c478bdstevel@tonic-gate				blk = blkmap(fs, cg_blksfree(cgp), bno);
14347c478bdstevel@tonic-gate				blk <<= 1;
14357c478bdstevel@tonic-gate				field = around[allocsiz];
14367c478bdstevel@tonic-gate				subfield = inside[allocsiz];
14377c478bdstevel@tonic-gate				for (pos = 0;
14387c478bdstevel@tonic-gate				    pos <= fs->fs_frag - allocsiz;
14397c478bdstevel@tonic-gate				    pos++) {
14407c478bdstevel@tonic-gate					if ((blk & field) == subfield) {
14417c478bdstevel@tonic-gate						gotit++;
14427c478bdstevel@tonic-gate						break;
14437c478bdstevel@tonic-gate					}
14447c478bdstevel@tonic-gate					field <<= 1;
14457c478bdstevel@tonic-gate					subfield <<= 1;
14467c478bdstevel@tonic-gate				}
14477c478bdstevel@tonic-gate				if (gotit)
14487c478bdstevel@tonic-gate					break;
14497c478bdstevel@tonic-gate			}
14507c478bdstevel@tonic-gate			bno += pos;
14517c478bdstevel@tonic-gate
14527c478bdstevel@tonic-gate			/*
14537c478bdstevel@tonic-gate			 * success if block is *not* being converted from
14547c478bdstevel@tonic-gate			 * metadata into userdata (harpy).  If so, ignore.
14557c478bdstevel@tonic-gate			 */
14567c478bdstevel@tonic-gate			if (!TRANS_ISCANCEL(ufsvfsp,
1457303bf60sdebnath			    ldbtob(fsbtodb(fs, (cfrag+bno))),
1458303bf60sdebnath			    allocsiz * fs->fs_fsize))
14597c478bdstevel@tonic-gate				return (bno);
1460303bf60sdebnath
14617c478bdstevel@tonic-gate			/*
14627c478bdstevel@tonic-gate			 * keep looking -- this block is being converted
14637c478bdstevel@tonic-gate			 */
14647c478bdstevel@tonic-gate			first = (last - loc) + 1;
14657c478bdstevel@tonic-gate			loc = 0;
14667c478bdstevel@tonic-gate			if (first < last)
14677c478bdstevel@tonic-gate				continue;
14687c478bdstevel@tonic-gate		}
14697c478bdstevel@tonic-gate		/*
14707c478bdstevel@tonic-gate		 * no usable matches in bottom half -- now search the top half
14717c478bdstevel@tonic-gate		 */
14727c478bdstevel@tonic-gate		if (secondtime)
14737c478bdstevel@tonic-gate			/*
14747c478bdstevel@tonic-gate			 * no usable matches in top half -- all done
14757c478bdstevel@tonic-gate			 */
14767c478bdstevel@tonic-gate			break;
14777c478bdstevel@tonic-gate		secondtime = 1;
14787c478bdstevel@tonic-gate		last = start + 1;
14797c478bdstevel@tonic-gate		first = 0;
14807c478bdstevel@tonic-gate	}
14817c478bdstevel@tonic-gate	/*
14827c478bdstevel@tonic-gate	 * no usable matches
14837c478bdstevel@tonic-gate	 */
14847c478bdstevel@tonic-gate	return ((daddr_t)-1);
14857c478bdstevel@tonic-gate}
14867c478bdstevel@tonic-gate
14877c478bdstevel@tonic-gate#define	UFSNADDR (NDADDR + NIADDR)	/* NADDR applies to (obsolete) S5FS */
14887c478bdstevel@tonic-gate#define	IB(i)	(NDADDR + (i))	/* index of i'th indirect block ptr */
14897c478bdstevel@tonic-gate#define	SINGLE	0		/* single indirect block ptr */
14907c478bdstevel@tonic-gate#define	DOUBLE	1		/* double indirect block ptr */
14917c478bdstevel@tonic-gate#define	TRIPLE	2		/* triple indirect block ptr */
14927c478bdstevel@tonic-gate
14937c478bdstevel@tonic-gate/*
1494303bf60sdebnath * Acquire a write lock, and keep trying till we get it
1495303bf60sdebnath */
1496303bf60sdebnathstatic int
1497303bf60sdebnathallocsp_wlockfs(struct vnode *vp, struct lockfs *lf)
1498303bf60sdebnath{
1499303bf60sdebnath	int err = 0;
1500303bf60sdebnath
1501303bf60sdebnathlockagain:
1502303bf60sdebnath	do {
1503303bf60sdebnath		err = ufs_fiolfss(vp, lf);
1504303bf60sdebnath		if (err)
1505303bf60sdebnath			return (err);
1506303bf60sdebnath	} while (!LOCKFS_IS_ULOCK(lf));
1507303bf60sdebnath
1508303bf60sdebnath	lf->lf_lock = LOCKFS_WLOCK;
1509303bf60sdebnath	lf->lf_flags = 0;
1510303bf60sdebnath	lf->lf_comment = NULL;
1511303bf60sdebnath	err = ufs__fiolfs(vp, lf, 1, 0);
1512303bf60sdebnath
1513303bf60sdebnath	if (err == EBUSY || err == EINVAL)
1514303bf60sdebnath		goto lockagain;
1515303bf60sdebnath
1516303bf60sdebnath	return (err);
1517303bf60sdebnath}
1518303bf60sdebnath
1519303bf60sdebnath/*
1520303bf60sdebnath * Release the write lock
1521303bf60sdebnath */
1522303bf60sdebnathstatic int
1523303bf60sdebnathallocsp_unlockfs(struct vnode *vp, struct lockfs *lf)
1524303bf60sdebnath{
1525303bf60sdebnath	int err = 0;
1526303bf60sdebnath
1527303bf60sdebnath	lf->lf_lock = LOCKFS_ULOCK;
1528303bf60sdebnath	lf->lf_flags = 0;
1529303bf60sdebnath	err = ufs__fiolfs(vp, lf, 1, 0);
1530303bf60sdebnath	return (err);
1531303bf60sdebnath}
1532303bf60sdebnath
1533303bf60sdebnathstruct allocsp_undo {
1534303bf60sdebnath	daddr_t offset;
1535303bf60sdebnath	daddr_t blk;
1536303bf60sdebnath	struct allocsp_undo *next;
1537303bf60sdebnath};
1538303bf60sdebnath
1539303bf60sdebnath/*
1540303bf60sdebnath * ufs_allocsp() can be used to pre-allocate blocks for a file on a given
15414f21de4jr * file system. For direct blocks, the blocks are allocated from the offset
15424f21de4jr * requested to the block boundary, then any full blocks are allocated,
15434f21de4jr * and finally any remainder.
15444f21de4jr * For indirect blocks the blocks are not initialized and are
15454f21de4jr * only marked as allocated. These addresses are then stored as negative
15464f21de4jr * block numbers in the inode to imply special handling. UFS has been modified
15474f21de4jr * where necessary to understand this new notion.
15484f21de4jr * Successfully fallocated files will have IFALLOCATE cflag set in the inode.
1549303bf60sdebnath */
1550303bf60sdebnathint
1551303bf60sdebnathufs_allocsp(struct vnode *vp, struct flock64 *lp, cred_t *cr)
1552303bf60sdebnath{
1553303bf60sdebnath	struct lockfs lf;
1554303bf60sdebnath	int berr, err, resv, issync;
15554f21de4jr	off_t istart, len; /* istart, special for idb */
1556303bf60sdebnath	struct inode *ip;
1557303bf60sdebnath	struct fs *fs;
1558303bf60sdebnath	struct ufsvfs *ufsvfsp;
15594f21de4jr	u_offset_t resid, i, uoff;
1560303bf60sdebnath	daddr32_t db_undo[NDADDR];	/* old direct blocks */
1561303bf60sdebnath	struct allocsp_undo *ib_undo = NULL;	/* ib undo */
1562303bf60sdebnath	struct allocsp_undo *undo = NULL;
1563303bf60sdebnath	u_offset_t osz;			/* old file size */
1564303bf60sdebnath	int chunkblks = 0;		/* # of blocks in 1 allocation */
1565303bf60sdebnath	int cnt = 0;
1566303bf60sdebnath	daddr_t allocblk;
1567303bf60sdebnath	daddr_t totblks = 0;
1568303bf60sdebnath	struct ulockfs	*ulp;
15694f21de4jr	size_t done_len;
15704f21de4jr	int nbytes, offsetn;
15714f21de4jr
1572303bf60sdebnath
1573303bf60sdebnath	ASSERT(vp->v_type == VREG);
1574303bf60sdebnath
1575303bf60sdebnath	ip = VTOI(vp);
1576303bf60sdebnath	fs = ip->i_fs;
1577303bf60sdebnath	if ((ufsvfsp = ip->i_ufsvfs) == NULL) {
1578303bf60sdebnath		err = EIO;
1579303bf60sdebnath		goto out_allocsp;
1580303bf60sdebnath	}
1581303bf60sdebnath
15824f21de4jr	istart = blkroundup(fs, (lp->l_start));
1583303bf60sdebnath	len = blkroundup(fs, (lp->l_len));
1584303bf60sdebnath	chunkblks = blkroundup(fs, ufsvfsp->vfs_iotransz) / fs->fs_bsize;
1585303bf60sdebnath	ulp = &ufsvfsp->vfs_ulockfs;
1586303bf60sdebnath
1587303bf60sdebnath	if (lp->l_start < 0 || lp->l_len <= 0)
1588303bf60sdebnath		return (EINVAL);
1589303bf60sdebnath
1590303bf60sdebnath	/* Quickly check to make sure we have space before we proceed */
1591303bf60sdebnath	if (lblkno(fs, len) > fs->fs_cstotal.cs_nbfree) {
1592303bf60sdebnath		if (TRANS_ISTRANS(ufsvfsp)) {
1593303bf60sdebnath			ufs_delete_drain_wait(ufsvfsp, 1);
1594303bf60sdebnath			if (lblkno(fs, len) > fs->fs_cstotal.cs_nbfree)
1595303bf60sdebnath				return (ENOSPC);
1596303bf60sdebnath		} else
1597303bf60sdebnath			return (ENOSPC);
1598303bf60sdebnath	}
1599303bf60sdebnath
1600303bf60sdebnath	/*
1601303bf60sdebnath	 * We will keep i_rwlock locked as WRITER through out the function
1602303bf60sdebnath	 * since we don't want anyone else reading or writing to the inode
1603303bf60sdebnath	 * while we are in the middle of fallocating the file.
1604303bf60sdebnath	 */
1605303bf60sdebnath	rw_enter(&ip->i_rwlock, RW_WRITER);
1606303bf60sdebnath
1607303bf60sdebnath	/* Back up the direct block list, used for undo later if necessary */
1608303bf60sdebnath	rw_enter(&ip->i_contents, RW_READER);
1609303bf60sdebnath	for (i = 0; i < NDADDR; i++)
1610303bf60sdebnath		db_undo[i] = ip->i_db[i];
1611303bf60sdebnath	osz = ip->i_size;
1612303bf60sdebnath	rw_exit(&ip->i_contents);
1613303bf60sdebnath
16144f21de4jr	/* Write lock the file system */
16154f21de4jr	if (err = allocsp_wlockfs(vp, &lf))
16164f21de4jr		goto exit;
16174f21de4jr
16184f21de4jr	/*
16194f21de4jr	 * Allocate any direct blocks now.
16204f21de4jr	 * Blocks are allocated from the offset requested to the block
16214f21de4jr	 * boundary, then any full blocks are allocated, and finally any
16224f21de4jr	 * remainder.
16234f21de4jr	 */
16244f21de4jr	if (lblkno(fs, lp->l_start) < NDADDR) {
1625303bf60sdebnath		ufs_trans_trunc_resv(ip, ip->i_size + (NDADDR * fs->fs_bsize),
1626303bf60sdebnath		    &resv, &resid);
1627303bf60sdebnath		TRANS_BEGIN_CSYNC(ufsvfsp, issync, TOP_ALLOCSP, resv);
1628303bf60sdebnath
1629303bf60sdebnath		rw_enter(&ufsvfsp->vfs_dqrwlock, RW_READER);
1630303bf60sdebnath		rw_enter(&ip->i_contents, RW_WRITER);
1631303bf60sdebnath
16324f21de4jr		done_len = 0;
16334f21de4jr		while ((done_len < lp->l_len) &&
16344f21de4jr		    (lblkno(fs, lp->l_start + done_len) < NDADDR)) {
16354f21de4jr			uoff = (offset_t)(lp->l_start + done_len);
16364f21de4jr			offsetn = (int)blkoff(fs, uoff);
16374f21de4jr			nbytes = (int)MIN(fs->fs_bsize - offsetn,
16384f21de4jr			    lp->l_len - done_len);
16394f21de4jr
16404f21de4jr			berr = bmap_write(ip, uoff, offsetn + nbytes,
16414f21de4jr			    BI_FALLOCATE, &allocblk, cr);
1642303bf60sdebnath			/* Yikes error, quit */
1643303bf60sdebnath			if (berr) {
1644303bf60sdebnath				TRANS_INODE(ufsvfsp, ip);
1645303bf60sdebnath				rw_exit(&ip->i_contents);
1646303bf60sdebnath				rw_exit(&ufsvfsp->vfs_dqrwlock);
1647303bf60sdebnath				TRANS_END_CSYNC(ufsvfsp, err, issync,
1648303bf60sdebnath				    TOP_ALLOCSP, resv);
16494f21de4jr				err = allocsp_unlockfs(vp, &lf);
1650303bf60sdebnath				goto exit;
1651303bf60sdebnath			}
1652303bf60sdebnath
1653303bf60sdebnath			if (allocblk) {
1654303bf60sdebnath				totblks++;
16554f21de4jr				if ((uoff + nbytes) > ip->i_size)
16564f21de4jr					ip->i_size = (uoff + nbytes);
1657303bf60sdebnath			}
16584f21de4jr			done_len += nbytes;
1659303bf60sdebnath		}
1660303bf60sdebnath
1661303bf60sdebnath		TRANS_INODE(ufsvfsp, ip);
1662303bf60sdebnath		rw_exit(&ip->i_contents);
1663303bf60sdebnath		rw_exit(&ufsvfsp->vfs_dqrwlock);
1664303bf60sdebnath		TRANS_END_CSYNC(ufsvfsp, err, issync, TOP_ALLOCSP, resv);
1665303bf60sdebnath
16664f21de4jr		/* start offset for indirect allocation */
16674f21de4jr		istart =  (uoff + nbytes);
1668303bf60sdebnath	}
1669303bf60sdebnath
1670303bf60sdebnath	/* Break the transactions into vfs_iotransz units */
1671303bf60sdebnath	ufs_trans_trunc_resv(ip, ip->i_size +
1672303bf60sdebnath	    blkroundup(fs, ufsvfsp->vfs_iotransz), &resv, &resid);
1673303bf60sdebnath	TRANS_BEGIN_CSYNC(ufsvfsp, issync, TOP_ALLOCSP, resv);
1674303bf60sdebnath
1675303bf60sdebnath	rw_enter(&ufsvfsp->vfs_dqrwlock, RW_READER);
1676303bf60sdebnath	rw_enter(&ip->i_contents, RW_WRITER);
1677303bf60sdebnath
1678303bf60sdebnath	/* Now go about fallocating necessary indirect blocks */
16794f21de4jr	for (i = istart; i < (lp->l_start + lp->l_len); i += fs->fs_bsize) {
1680303bf60sdebnath		berr = bmap_write(ip, i, fs->fs_bsize, BI_FALLOCATE,
1681303bf60sdebnath		    &allocblk, cr);
1682303bf60sdebnath		if (berr) {
1683303bf60sdebnath			TRANS_INODE(ufsvfsp, ip);
1684303bf60sdebnath			rw_exit(&ip->i_contents);
1685303bf60sdebnath			rw_exit(&ufsvfsp->vfs_dqrwlock);
1686303bf60sdebnath			TRANS_END_CSYNC(ufsvfsp, err, issync,
1687303bf60sdebnath			    TOP_ALLOCSP, resv);
1688303bf60sdebnath			err = allocsp_unlockfs(vp, &lf);
1689303bf60sdebnath			goto exit;
1690303bf60sdebnath		}
1691303bf60sdebnath
1692303bf60sdebnath		/* Update the blk counter only if new block was added */
1693303bf60sdebnath		if (allocblk) {
1694303bf60sdebnath			/* Save undo information */
1695303bf60sdebnath			undo = kmem_alloc(sizeof (struct allocsp_undo),
1696303bf60sdebnath			    KM_SLEEP);
1697303bf60sdebnath			undo->offset = i;
1698303bf60sdebnath			undo->blk = allocblk;
1699303bf60sdebnath			undo->next = ib_undo;
1700303bf60sdebnath			ib_undo = undo;
1701303bf60sdebnath			totblks++;
170233c22cbmishra
170333c22cbmishra			if (i >= ip->i_size)
170433c22cbmishra				ip->i_size += fs->fs_bsize;
1705303bf60sdebnath		}
1706303bf60sdebnath		cnt++;
1707303bf60sdebnath
1708303bf60sdebnath		/* Being a good UFS citizen, let others get a share */
1709303bf60sdebnath		if (cnt == chunkblks) {
1710303bf60sdebnath			/*
1711303bf60sdebnath			 * If there are waiters or the fs is hard locked,
1712303bf60sdebnath			 * error locked, or read-only error locked,
1713303bf60sdebnath			 * quit with EIO
1714303bf60sdebnath			 */
1715303bf60sdebnath			if (ULOCKFS_IS_HLOCK(ulp) || ULOCKFS_IS_ELOCK(ulp) ||
1716303bf60sdebnath			    ULOCKFS_IS_ROELOCK(ulp)) {
1717303bf60sdebnath				ip->i_cflags |= IFALLOCATE;
1718303bf60sdebnath				TRANS_INODE(ufsvfsp, ip);
1719303bf60sdebnath				rw_exit(&ip->i_contents);
1720303bf60sdebnath				rw_exit(&ufsvfsp->vfs_dqrwlock);
1721303bf60sdebnath
1722303bf60sdebnath				TRANS_END_CSYNC(ufsvfsp, err, issync,
1723303bf60sdebnath				    TOP_ALLOCSP, resv);
1724303bf60sdebnath				rw_exit(&ip->i_rwlock);
17254f21de4jr				(void) allocsp_unlockfs(vp, &lf);
1726303bf60sdebnath				return (EIO);
1727303bf60sdebnath			}
1728303bf60sdebnath
1729303bf60sdebnath			TRANS_INODE(ufsvfsp, ip);
1730303bf60sdebnath			rw_exit(&ip->i_contents);
1731303bf60sdebnath			rw_exit(&ufsvfsp->vfs_dqrwlock);
1732303bf60sdebnath
1733303bf60sdebnath			/* End the current transaction */
1734303bf60sdebnath			TRANS_END_CSYNC(ufsvfsp, err, issync,
1735303bf60sdebnath			    TOP_ALLOCSP, resv);
1736303bf60sdebnath
1737303bf60sdebnath			if (CV_HAS_WAITERS(&ulp->ul_cv)) {
1738303bf60sdebnath				/* Release the write lock */
1739303bf60sdebnath				if (err = allocsp_unlockfs(vp, &lf))
1740303bf60sdebnath					goto exit;
1741303bf60sdebnath
1742303bf60sdebnath				/* Wake up others waiting to do operations */
1743303bf60sdebnath				mutex_enter(&ulp->ul_lock);
1744303bf60sdebnath				cv_broadcast(&ulp->ul_cv);
1745303bf60sdebnath				mutex_exit(&ulp->ul_lock);
1746303bf60sdebnath
1747303bf60sdebnath				/* Grab the write lock again */
1748303bf60sdebnath				if (err = allocsp_wlockfs(vp, &lf))
1749303bf60sdebnath					goto exit;
1750303bf60sdebnath			} /* end of CV_HAS_WAITERS(&ulp->ul_cv) */
1751303bf60sdebnath
1752303bf60sdebnath			/* Reserve more space in log for this file */
1753303bf60sdebnath			ufs_trans_trunc_resv(ip,
1754303bf60sdebnath			    ip->i_size + blkroundup(fs, ufsvfsp->vfs_iotransz),
1755303bf60sdebnath			    &resv, &resid);
1756303bf60sdebnath			TRANS_BEGIN_CSYNC(ufsvfsp, issync, TOP_ALLOCSP, resv);
1757303bf60sdebnath
1758303bf60sdebnath			rw_enter(&ufsvfsp->vfs_dqrwlock, RW_READER);
1759303bf60sdebnath			rw_enter(&ip->i_contents, RW_WRITER);
1760303bf60sdebnath
1761303bf60sdebnath			cnt = 0;	/* reset cnt b/c of new transaction */
1762303bf60sdebnath		}
1763303bf60sdebnath	}
1764303bf60sdebnath
1765303bf60sdebnath	if (!err && !berr)
1766303bf60sdebnath		ip->i_cflags |= IFALLOCATE;
1767303bf60sdebnath
17684f21de4jr	/* If the file has grown then correct the file size */
17694f21de4jr	if (osz < (lp->l_start + lp->l_len))
17704f21de4jr		ip->i_size = (lp->l_start + lp->l_len);
17714f21de4jr
1772303bf60sdebnath	/* Release locks, end log transaction and unlock fs */
1773303bf60sdebnath	TRANS_INODE(ufsvfsp, ip);
1774303bf60sdebnath	rw_exit(&ip->i_contents);
1775303bf60sdebnath	rw_exit(&ufsvfsp->vfs_dqrwlock);
1776303bf60sdebnath
1777303bf60sdebnath	TRANS_END_CSYNC(ufsvfsp, err, issync, TOP_ALLOCSP, resv);
1778303bf60sdebnath	err = allocsp_unlockfs(vp, &lf);
1779303bf60sdebnath
1780303bf60sdebnath	/*
1781303bf60sdebnath	 * @ exit label, we should no longer be holding the fs write lock, and
1782303bf60sdebnath	 * all logging transactions should have been ended. We still hold
1783303bf60sdebnath	 * ip->i_rwlock.
1784303bf60sdebnath	 */
1785303bf60sdebnathexit:
1786303bf60sdebnath	/*
1787303bf60sdebnath	 * File has grown larger than 2GB. Set flag
1788303bf60sdebnath	 * in superblock to indicate this, if it
1789303bf60sdebnath	 * is not already set.
1790303bf60sdebnath	 */
1791303bf60sdebnath	if ((ip->i_size > MAXOFF32_T) &&
179280d3443frankho	    !(fs->fs_flags & FSLARGEFILES)) {
1793303bf60sdebnath		ASSERT(ufsvfsp->vfs_lfflags & UFS_LARGEFILES);
1794303bf60sdebnath		mutex_enter(&ufsvfsp->vfs_lock);
1795303bf60sdebnath		fs->fs_flags |= FSLARGEFILES;
1796303bf60sdebnath		ufs_sbwrite(ufsvfsp);
1797303bf60sdebnath		mutex_exit(&ufsvfsp->vfs_lock);
1798303bf60sdebnath	}
1799303bf60sdebnath
1800303bf60sdebnath	/*
1801303bf60sdebnath	 * Since we couldn't allocate completely, we will undo the allocations.
1802303bf60sdebnath	 */
1803303bf60sdebnath	if (berr) {
1804303bf60sdebnath		ufs_trans_trunc_resv(ip, totblks * fs->fs_bsize, &resv, &resid);
1805303bf60sdebnath		TRANS_BEGIN_CSYNC(ufsvfsp, issync, TOP_ALLOCSP, resv);
1806303bf60sdebnath
1807303bf60sdebnath		rw_enter(&ufsvfsp->vfs_dqrwlock, RW_READER);
1808303bf60sdebnath		rw_enter(&ip->i_contents, RW_WRITER);
1809303bf60sdebnath
1810303bf60sdebnath		/* Direct blocks */
1811303bf60sdebnath		for (i = 0; i < NDADDR; i++) {
1812303bf60sdebnath			/*
1813303bf60sdebnath			 * Only free the block if they are not same, and
1814303bf60sdebnath			 * the old one isn't zero (the fragment was
1815303bf60sdebnath			 * re-allocated).
1816303bf60sdebnath			 */
1817303bf60sdebnath			if (db_undo[i] != ip->i_db[i] && db_undo[i] == 0) {
1818303bf60sdebnath				free(ip, ip->i_db[i], fs->fs_bsize, 0);
1819303bf60sdebnath				ip->i_db[i] = 0;
1820303bf60sdebnath			}
1821303bf60sdebnath		}
1822303bf60sdebnath
1823303bf60sdebnath		/* Undo the indirect blocks */
1824303bf60sdebnath		while (ib_undo != NULL) {
1825303bf60sdebnath			undo = ib_undo;
1826303bf60sdebnath			err = bmap_set_bn(vp, undo->offset, 0);
1827303bf60sdebnath			if (err)
1828303bf60sdebnath				cmn_err(CE_PANIC, "ufs_allocsp(): failed to "
1829303bf60sdebnath				    "undo allocation of block %ld",
1830303bf60sdebnath				    undo->offset);
1831303bf60sdebnath			free(ip, undo->blk, fs->fs_bsize, I_IBLK);
1832303bf60sdebnath			ib_undo = undo->next;
1833303bf60sdebnath			kmem_free(undo, sizeof (struct allocsp_undo));
1834303bf60sdebnath		}
1835303bf60sdebnath
1836303bf60sdebnath		ip->i_size = osz;
1837303bf60sdebnath		TRANS_INODE(ufsvfsp, ip);
1838303bf60sdebnath
1839303bf60sdebnath		rw_exit(&ip->i_contents);
1840303bf60sdebnath		rw_exit(&ufsvfsp->vfs_dqrwlock);
1841303bf60sdebnath
1842303bf60sdebnath		TRANS_END_CSYNC(ufsvfsp, err, issync, TOP_ALLOCSP, resv);
1843303bf60sdebnath
1844303bf60sdebnath		rw_exit(&ip->i_rwlock);
1845303bf60sdebnath		return (berr);
1846303bf60sdebnath	}
1847303bf60sdebnath
1848303bf60sdebnath	/*
1849303bf60sdebnath	 * Don't forget to free the undo chain :)
1850303bf60sdebnath	 */
1851303bf60sdebnath	while (ib_undo != NULL) {
1852303bf60sdebnath		undo = ib_undo;
1853303bf60sdebnath		ib_undo = undo->next;
1854303bf60sdebnath		kmem_free(undo, sizeof (struct allocsp_undo));
1855303bf60sdebnath	}
1856303bf60sdebnath
1857303bf60sdebnath	rw_exit(&ip->i_rwlock);
1858303bf60sdebnath
1859303bf60sdebnathout_allocsp:
1860303bf60sdebnath	return (err);
1861303bf60sdebnath}
1862303bf60sdebnath
1863303bf60sdebnath/*
18647c478bdstevel@tonic-gate * Free storage space associated with the specified inode.  The portion
18657c478bdstevel@tonic-gate * to be freed is specified by lp->l_start and lp->l_len (already
18667c478bdstevel@tonic-gate * normalized to a "whence" of 0).
18677c478bdstevel@tonic-gate *
18687c478bdstevel@tonic-gate * This is an experimental facility whose continued existence is not
18697c478bdstevel@tonic-gate * guaranteed.  Currently, we only support the special case
18707c478bdstevel@tonic-gate * of l_len == 0, meaning free to end of file.
18717c478bdstevel@tonic-gate *
18727c478bdstevel@tonic-gate * Blocks are freed in reverse order.  This FILO algorithm will tend to
18737c478bdstevel@tonic-gate * maintain a contiguous free list much longer than FIFO.
18747c478bdstevel@tonic-gate * See also ufs_itrunc() in ufs_inode.c.
18757c478bdstevel@tonic-gate *
18767c478bdstevel@tonic-gate * Bug: unused bytes in the last retained block are not cleared.
18777c478bdstevel@tonic-gate * This may result in a "hole" in the file that does not read as zeroes.
18787c478bdstevel@tonic-gate */
18797c478bdstevel@tonic-gate/* ARGSUSED */
18807c478bdstevel@tonic-gateint
18817c478bdstevel@tonic-gateufs_freesp(struct vnode *vp, struct flock64 *lp, int flag, cred_t *cr)
18827c478bdstevel@tonic-gate{
18837c478bdstevel@tonic-gate	int i;
18847c478bdstevel@tonic-gate	struct inode *ip = VTOI(vp);
18857c478bdstevel@tonic-gate	int error;
18867c478bdstevel@tonic-gate
18877c478bdstevel@tonic-gate	ASSERT(vp->v_type == VREG);
18887c478bdstevel@tonic-gate	ASSERT(lp->l_start >= 0);	/* checked by convoff */
18897c478bdstevel@tonic-gate
18907c478bdstevel@tonic-gate	if (lp->l_len != 0)
18917c478bdstevel@tonic-gate		return (EINVAL);
18927c478bdstevel@tonic-gate
18937c478bdstevel@tonic-gate	rw_enter(&ip->i_contents, RW_READER);
18947c478bdstevel@tonic-gate	if (ip->i_size == (u_offset_t)lp->l_start) {
18957c478bdstevel@tonic-gate		rw_exit(&ip->i_contents);
18967c478bdstevel@tonic-gate		return (0);
18977c478bdstevel@tonic-gate	}
18987c478bdstevel@tonic-gate
18997c478bdstevel@tonic-gate	/*
19007c478bdstevel@tonic-gate	 * Check if there is any active mandatory lock on the
19017c478bdstevel@tonic-gate	 * range that will be truncated/expanded.
19027c478bdstevel@tonic-gate	 */
19037c478bdstevel@tonic-gate	if (MANDLOCK(vp, ip->i_mode)) {
19047c478bdstevel@tonic-gate		offset_t save_start;
19057c478bdstevel@tonic-gate
19067c478bdstevel@tonic-gate		save_start = lp->l_start;
19077c478bdstevel@tonic-gate
19087c478bdstevel@tonic-gate		if (ip->i_size < lp->l_start) {
19097c478bdstevel@tonic-gate			/*
19107c478bdstevel@tonic-gate			 * "Truncate up" case: need to make sure there
19117c478bdstevel@tonic-gate			 * is no lock beyond current end-of-file. To
19127c478bdstevel@tonic-gate			 * do so, we need to set l_start to the size
19137c478bdstevel@tonic-gate			 * of the file temporarily.
19147c478bdstevel@tonic-gate			 */
19157c478bdstevel@tonic-gate			lp->l_start = ip->i_size;
19167c478bdstevel@tonic-gate		}
19177c478bdstevel@tonic-gate		lp->l_type = F_WRLCK;
19187c478bdstevel@tonic-gate		lp->l_sysid = 0;
19197c478bdstevel@tonic-gate		lp->l_pid = ttoproc(curthread)->p_pid;
19207c478bdstevel@tonic-gate		i = (flag & (FNDELAY|FNONBLOCK)) ? 0 : SLPFLCK;
19217c478bdstevel@tonic-gate		rw_exit(&ip->i_contents);
19227c478bdstevel@tonic-gate		if ((i = reclock(vp, lp, i, 0, lp->l_start, NULL)) != 0 ||
19237c478bdstevel@tonic-gate		    lp->l_type != F_UNLCK) {
19247c478bdstevel@tonic-gate			return (i ? i : EAGAIN);
19257c478bdstevel@tonic-gate		}
19267c478bdstevel@tonic-gate		rw_enter(&ip->i_contents, RW_READER);
19277c478bdstevel@tonic-gate
19287c478bdstevel@tonic-gate		lp->l_start = save_start;
19297c478bdstevel@tonic-gate	}
19307c478bdstevel@tonic-gate
19317c478bdstevel@tonic-gate	/*
19327c478bdstevel@tonic-gate	 * Make sure a write isn't in progress (allocating blocks)
19337c478bdstevel@tonic-gate	 * by acquiring i_rwlock (we promised ufs_bmap we wouldn't
19347c478bdstevel@tonic-gate	 * truncate while it was allocating blocks).
19357c478bdstevel@tonic-gate	 * Grab the locks in the right order.
19367c478bdstevel@tonic-gate	 */
19377c478bdstevel@tonic-gate	rw_exit(&ip->i_contents);
19387c478bdstevel@tonic-gate	rw_enter(&ip->i_rwlock, RW_WRITER);
19397c478bdstevel@tonic-gate	error = TRANS_ITRUNC(ip, (u_offset_t)lp->l_start, 0, cr);
19407c478bdstevel@tonic-gate	rw_exit(&ip->i_rwlock);
19417c478bdstevel@tonic-gate	return (error);
19427c478bdstevel@tonic-gate}
19437c478bdstevel@tonic-gate
19447c478bdstevel@tonic-gate/*
19457c478bdstevel@tonic-gate * Find a cg with as close to nb contiguous bytes as possible
19467c478bdstevel@tonic-gate *	THIS MAY TAKE MANY DISK READS!
19477c478bdstevel@tonic-gate *
19487c478bdstevel@tonic-gate * Implemented in an attempt to allocate contiguous blocks for
19497c478bdstevel@tonic-gate * writing the ufs log file to, minimizing future disk head seeking
19507c478bdstevel@tonic-gate */
19517c478bdstevel@tonic-gatedaddr_t
1952e7da395Owen Robertscontigpref(ufsvfs_t *ufsvfsp, size_t nb, size_t minb)
19537c478bdstevel@tonic-gate{
19547c478bdstevel@tonic-gate	struct fs	*fs	= ufsvfsp->vfs_fs;
19557c478bdstevel@tonic-gate	daddr_t		nblk	= lblkno(fs, blkroundup(fs, nb));
1956e7da395Owen Roberts	daddr_t		minblk	= lblkno(fs, blkroundup(fs, minb));
19577c478bdstevel@tonic-gate	daddr_t		savebno, curbno, cgbno;
1958e7da395Owen Roberts	int		cg, cgblks, savecg, savenblk, curnblk, startcg;
19597c478bdstevel@tonic-gate	uchar_t		*blksfree;
19607c478bdstevel@tonic-gate	buf_t		*bp;
19617c478bdstevel@tonic-gate	struct cg	*cgp;
19627c478bdstevel@tonic-gate
19637c478bdstevel@tonic-gate	savenblk = 0;
19647c478bdstevel@tonic-gate	savecg = 0;
19657c478bdstevel@tonic-gate	savebno = 0;
19667c478bdstevel@tonic-gate
1967e7da395Owen Roberts	if ((startcg = findlogstartcg(fs, nblk, minblk)) == -1)
1968e7da395Owen Roberts		cg = 0;	/* Nothing suitable found */
1969e7da395Owen Roberts	else
1970e7da395Owen Roberts		cg = startcg;
19717c478bdstevel@tonic-gate
1972e7da395Owen Roberts	for (; cg < fs->fs_ncg; ++cg) {
19737c478bdstevel@tonic-gate		/*
19747c478bdstevel@tonic-gate		 * find the largest contiguous range in this cg
19757c478bdstevel@tonic-gate		 */
19767c478bdstevel@tonic-gate		bp = UFS_BREAD(ufsvfsp, ufsvfsp->vfs_dev,
1977303bf60sdebnath		    (daddr_t)fsbtodb(fs, cgtod(fs, cg)),
1978303bf60sdebnath		    (int)fs->fs_cgsize);
19797c478bdstevel@tonic-gate		cgp = bp->b_un.b_cg;
19807c478bdstevel@tonic-gate		if (bp->b_flags & B_ERROR || !cg_chkmagic(cgp)) {
19817c478bdstevel@tonic-gate			brelse(bp);
19827c478bdstevel@tonic-gate			continue;
19837c478bdstevel@tonic-gate		}
19847c478bdstevel@tonic-gate		blksfree = cg_blksfree(cgp);	    /* free array */
19857c478bdstevel@tonic-gate		cgblks = fragstoblks(fs, fs->fs_fpg); /* blks in free array */
19867c478bdstevel@tonic-gate		cgbno = 0;
19877c478bdstevel@tonic-gate		while (cgbno < cgblks && savenblk < nblk) {
19887c478bdstevel@tonic-gate			/* find a free block */
1989