xref: /onnv-gate/usr/src/uts/common/fs/ufs/ufs_alloc.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
28*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate /*
31*0Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
32*0Sstevel@tonic-gate  * The Regents of the University of California
33*0Sstevel@tonic-gate  * All Rights Reserved
34*0Sstevel@tonic-gate  *
35*0Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
36*0Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
37*0Sstevel@tonic-gate  * contributors.
38*0Sstevel@tonic-gate  */
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate 
41*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
42*0Sstevel@tonic-gate 
43*0Sstevel@tonic-gate #include <sys/types.h>
44*0Sstevel@tonic-gate #include <sys/t_lock.h>
45*0Sstevel@tonic-gate #include <sys/debug.h>
46*0Sstevel@tonic-gate #include <sys/param.h>
47*0Sstevel@tonic-gate #include <sys/systm.h>
48*0Sstevel@tonic-gate #include <sys/signal.h>
49*0Sstevel@tonic-gate #include <sys/cred.h>
50*0Sstevel@tonic-gate #include <sys/proc.h>
51*0Sstevel@tonic-gate #include <sys/disp.h>
52*0Sstevel@tonic-gate #include <sys/user.h>
53*0Sstevel@tonic-gate #include <sys/buf.h>
54*0Sstevel@tonic-gate #include <sys/vfs.h>
55*0Sstevel@tonic-gate #include <sys/vnode.h>
56*0Sstevel@tonic-gate #include <sys/acl.h>
57*0Sstevel@tonic-gate #include <sys/fs/ufs_fs.h>
58*0Sstevel@tonic-gate #include <sys/fs/ufs_inode.h>
59*0Sstevel@tonic-gate #include <sys/fs/ufs_acl.h>
60*0Sstevel@tonic-gate #include <sys/fs/ufs_bio.h>
61*0Sstevel@tonic-gate #include <sys/fs/ufs_quota.h>
62*0Sstevel@tonic-gate #include <sys/kmem.h>
63*0Sstevel@tonic-gate #include <sys/fs/ufs_trans.h>
64*0Sstevel@tonic-gate #include <sys/fs/ufs_panic.h>
65*0Sstevel@tonic-gate #include <sys/errno.h>
66*0Sstevel@tonic-gate #include <sys/time.h>
67*0Sstevel@tonic-gate #include <sys/sysmacros.h>
68*0Sstevel@tonic-gate #include <sys/file.h>
69*0Sstevel@tonic-gate #include <sys/fcntl.h>
70*0Sstevel@tonic-gate #include <sys/flock.h>
71*0Sstevel@tonic-gate #include <fs/fs_subr.h>
72*0Sstevel@tonic-gate #include <sys/cmn_err.h>
73*0Sstevel@tonic-gate #include <sys/policy.h>
74*0Sstevel@tonic-gate 
75*0Sstevel@tonic-gate static ino_t	hashalloc();
76*0Sstevel@tonic-gate static daddr_t	fragextend();
77*0Sstevel@tonic-gate static daddr_t	alloccg();
78*0Sstevel@tonic-gate static daddr_t	alloccgblk();
79*0Sstevel@tonic-gate static ino_t	ialloccg();
80*0Sstevel@tonic-gate static daddr_t	mapsearch();
81*0Sstevel@tonic-gate 
82*0Sstevel@tonic-gate extern int	inside[], around[];
83*0Sstevel@tonic-gate extern uchar_t	*fragtbl[];
84*0Sstevel@tonic-gate void delay();
85*0Sstevel@tonic-gate 
86*0Sstevel@tonic-gate /*
87*0Sstevel@tonic-gate  * Allocate a block in the file system.
88*0Sstevel@tonic-gate  *
89*0Sstevel@tonic-gate  * The size of the requested block is given, which must be some
90*0Sstevel@tonic-gate  * multiple of fs_fsize and <= fs_bsize.
91*0Sstevel@tonic-gate  * A preference may be optionally specified. If a preference is given
92*0Sstevel@tonic-gate  * the following hierarchy is used to allocate a block:
93*0Sstevel@tonic-gate  *   1) allocate the requested block.
94*0Sstevel@tonic-gate  *   2) allocate a rotationally optimal block in the same cylinder.
95*0Sstevel@tonic-gate  *   3) allocate a block in the same cylinder group.
96*0Sstevel@tonic-gate  *   4) quadratically rehash into other cylinder groups, until an
97*0Sstevel@tonic-gate  *	available block is located.
98*0Sstevel@tonic-gate  * If no block preference is given the following hierarchy is used
99*0Sstevel@tonic-gate  * to allocate a block:
100*0Sstevel@tonic-gate  *   1) allocate a block in the cylinder group that contains the
101*0Sstevel@tonic-gate  *	inode for the file.
102*0Sstevel@tonic-gate  *   2) quadratically rehash into other cylinder groups, until an
103*0Sstevel@tonic-gate  *	available block is located.
104*0Sstevel@tonic-gate  */
105*0Sstevel@tonic-gate int
106*0Sstevel@tonic-gate alloc(struct inode *ip, daddr_t bpref, int size, daddr_t *bnp, cred_t *cr)
107*0Sstevel@tonic-gate {
108*0Sstevel@tonic-gate 	struct fs *fs;
109*0Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp;
110*0Sstevel@tonic-gate 	daddr_t bno;
111*0Sstevel@tonic-gate 	int cg;
112*0Sstevel@tonic-gate 	int err;
113*0Sstevel@tonic-gate 	char *errmsg = NULL;
114*0Sstevel@tonic-gate 	size_t len;
115*0Sstevel@tonic-gate 
116*0Sstevel@tonic-gate 	ufsvfsp = ip->i_ufsvfs;
117*0Sstevel@tonic-gate 	fs = ufsvfsp->vfs_fs;
118*0Sstevel@tonic-gate 	if ((unsigned)size > fs->fs_bsize || fragoff(fs, size) != 0) {
119*0Sstevel@tonic-gate 		err = ufs_fault(ITOV(ip),
120*0Sstevel@tonic-gate 	    "alloc: bad size, dev = 0x%lx, bsize = %d, size = %d, fs = %s\n",
121*0Sstevel@tonic-gate 	    ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt);
122*0Sstevel@tonic-gate 		return (err);
123*0Sstevel@tonic-gate 	}
124*0Sstevel@tonic-gate 	if (size == fs->fs_bsize && fs->fs_cstotal.cs_nbfree == 0)
125*0Sstevel@tonic-gate 		goto nospace;
126*0Sstevel@tonic-gate 	if (freespace(fs, ufsvfsp) <= 0 &&
127*0Sstevel@tonic-gate 	    secpolicy_fs_minfree(cr, ufsvfsp->vfs_vfs) != 0)
128*0Sstevel@tonic-gate 		goto nospace;
129*0Sstevel@tonic-gate 	err = chkdq(ip, (long)btodb(size), 0, cr, &errmsg, &len);
130*0Sstevel@tonic-gate 	/* Note that may not have err, but may have errmsg */
131*0Sstevel@tonic-gate 	if (errmsg != NULL) {
132*0Sstevel@tonic-gate 		uprintf(errmsg);
133*0Sstevel@tonic-gate 		kmem_free(errmsg, len);
134*0Sstevel@tonic-gate 		errmsg = NULL;
135*0Sstevel@tonic-gate 	}
136*0Sstevel@tonic-gate 	if (err)
137*0Sstevel@tonic-gate 		return (err);
138*0Sstevel@tonic-gate 	if (bpref >= fs->fs_size)
139*0Sstevel@tonic-gate 		bpref = 0;
140*0Sstevel@tonic-gate 	if (bpref == 0)
141*0Sstevel@tonic-gate 		cg = (int)itog(fs, ip->i_number);
142*0Sstevel@tonic-gate 	else
143*0Sstevel@tonic-gate 		cg = dtog(fs, bpref);
144*0Sstevel@tonic-gate 
145*0Sstevel@tonic-gate 	bno = (daddr_t)hashalloc(ip, cg, (long)bpref, size,
146*0Sstevel@tonic-gate 	    (ulong_t (*)())alloccg);
147*0Sstevel@tonic-gate 	if (bno > 0) {
148*0Sstevel@tonic-gate 		*bnp = bno;
149*0Sstevel@tonic-gate 		return (0);
150*0Sstevel@tonic-gate 	}
151*0Sstevel@tonic-gate 
152*0Sstevel@tonic-gate 	/*
153*0Sstevel@tonic-gate 	 * hashalloc() failed because some other thread grabbed
154*0Sstevel@tonic-gate 	 * the last block so unwind the quota operation.  We can
155*0Sstevel@tonic-gate 	 * ignore the return because subtractions don't fail and
156*0Sstevel@tonic-gate 	 * size is guaranteed to be >= zero by our caller.
157*0Sstevel@tonic-gate 	 */
158*0Sstevel@tonic-gate 	(void) chkdq(ip, -(long)btodb(size), 0, cr, (char **)NULL,
159*0Sstevel@tonic-gate 		(size_t *)NULL);
160*0Sstevel@tonic-gate 
161*0Sstevel@tonic-gate nospace:
162*0Sstevel@tonic-gate 	mutex_enter(&ufsvfsp->vfs_lock);
163*0Sstevel@tonic-gate 	if ((lbolt - ufsvfsp->vfs_lastwhinetime) > (hz << 2) &&
164*0Sstevel@tonic-gate 		(!(TRANS_ISTRANS(ufsvfsp)) || !(ip->i_flag & IQUIET))) {
165*0Sstevel@tonic-gate 		ufsvfsp->vfs_lastwhinetime = lbolt;
166*0Sstevel@tonic-gate 		cmn_err(CE_NOTE, "alloc: %s: file system full", fs->fs_fsmnt);
167*0Sstevel@tonic-gate 	}
168*0Sstevel@tonic-gate 	mutex_exit(&ufsvfsp->vfs_lock);
169*0Sstevel@tonic-gate 	return (ENOSPC);
170*0Sstevel@tonic-gate }
171*0Sstevel@tonic-gate 
172*0Sstevel@tonic-gate /*
173*0Sstevel@tonic-gate  * Reallocate a fragment to a bigger size
174*0Sstevel@tonic-gate  *
175*0Sstevel@tonic-gate  * The number and size of the old block is given, and a preference
176*0Sstevel@tonic-gate  * and new size is also specified.  The allocator attempts to extend
177*0Sstevel@tonic-gate  * the original block.  Failing that, the regular block allocator is
178*0Sstevel@tonic-gate  * invoked to get an appropriate block.
179*0Sstevel@tonic-gate  */
180*0Sstevel@tonic-gate int
181*0Sstevel@tonic-gate realloccg(struct inode *ip, daddr_t bprev, daddr_t bpref, int osize,
182*0Sstevel@tonic-gate     int nsize, daddr_t *bnp, cred_t *cr)
183*0Sstevel@tonic-gate {
184*0Sstevel@tonic-gate 	daddr_t bno;
185*0Sstevel@tonic-gate 	struct fs *fs;
186*0Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp;
187*0Sstevel@tonic-gate 	int cg, request;
188*0Sstevel@tonic-gate 	int err;
189*0Sstevel@tonic-gate 	char *errmsg = NULL;
190*0Sstevel@tonic-gate 	size_t len;
191*0Sstevel@tonic-gate 
192*0Sstevel@tonic-gate 	ufsvfsp = ip->i_ufsvfs;
193*0Sstevel@tonic-gate 	fs = ufsvfsp->vfs_fs;
194*0Sstevel@tonic-gate 	if ((unsigned)osize > fs->fs_bsize || fragoff(fs, osize) != 0 ||
195*0Sstevel@tonic-gate 	    (unsigned)nsize > fs->fs_bsize || fragoff(fs, nsize) != 0) {
196*0Sstevel@tonic-gate 		err = ufs_fault(ITOV(ip),
197*0Sstevel@tonic-gate 	"realloccg: bad size, dev=0x%lx, bsize=%d, osize=%d, nsize=%d, fs=%s\n",
198*0Sstevel@tonic-gate 		    ip->i_dev, fs->fs_bsize, osize, nsize,
199*0Sstevel@tonic-gate 		    fs->fs_fsmnt);
200*0Sstevel@tonic-gate 		return (err);
201*0Sstevel@tonic-gate 	}
202*0Sstevel@tonic-gate 	if (freespace(fs, ufsvfsp) <= 0 &&
203*0Sstevel@tonic-gate 	    secpolicy_fs_minfree(cr, ufsvfsp->vfs_vfs) != 0)
204*0Sstevel@tonic-gate 		goto nospace;
205*0Sstevel@tonic-gate 	if (bprev == 0) {
206*0Sstevel@tonic-gate 		err = ufs_fault(ITOV(ip),
207*0Sstevel@tonic-gate 	"realloccg: bad bprev, dev = 0x%lx, bsize = %d, bprev = %ld, fs = %s\n",
208*0Sstevel@tonic-gate 		    ip->i_dev, fs->fs_bsize, bprev,
209*0Sstevel@tonic-gate 		    fs->fs_fsmnt);
210*0Sstevel@tonic-gate 		return (err);
211*0Sstevel@tonic-gate 	}
212*0Sstevel@tonic-gate 	err = chkdq(ip, (long)btodb(nsize - osize), 0, cr, &errmsg, &len);
213*0Sstevel@tonic-gate 	/* Note that may not have err, but may have errmsg */
214*0Sstevel@tonic-gate 	if (errmsg != NULL) {
215*0Sstevel@tonic-gate 		uprintf(errmsg);
216*0Sstevel@tonic-gate 		kmem_free(errmsg, len);
217*0Sstevel@tonic-gate 		errmsg = NULL;
218*0Sstevel@tonic-gate 	}
219*0Sstevel@tonic-gate 	if (err)
220*0Sstevel@tonic-gate 		return (err);
221*0Sstevel@tonic-gate 	cg = dtog(fs, bprev);
222*0Sstevel@tonic-gate 	bno = fragextend(ip, cg, (long)bprev, osize, nsize);
223*0Sstevel@tonic-gate 	if (bno != 0) {
224*0Sstevel@tonic-gate 		*bnp = bno;
225*0Sstevel@tonic-gate 		return (0);
226*0Sstevel@tonic-gate 	}
227*0Sstevel@tonic-gate 	if (bpref >= fs->fs_size)
228*0Sstevel@tonic-gate 		bpref = 0;
229*0Sstevel@tonic-gate 
230*0Sstevel@tonic-gate 	/*
231*0Sstevel@tonic-gate 	 * When optimizing for time we allocate a full block and
232*0Sstevel@tonic-gate 	 * then only use the upper portion for this request. When
233*0Sstevel@tonic-gate 	 * this file grows again it will grow into the unused portion
234*0Sstevel@tonic-gate 	 * of the block (See fragextend() above).  This saves time
235*0Sstevel@tonic-gate 	 * because an extra disk write would be needed if the frags
236*0Sstevel@tonic-gate 	 * following the current allocation were not free. The extra
237*0Sstevel@tonic-gate 	 * disk write is needed to move the data from its current
238*0Sstevel@tonic-gate 	 * location into the newly allocated position.
239*0Sstevel@tonic-gate 	 *
240*0Sstevel@tonic-gate 	 * When optimizing for space we allocate a run of frags
241*0Sstevel@tonic-gate 	 * that is just the right size for this request.
242*0Sstevel@tonic-gate 	 */
243*0Sstevel@tonic-gate 	request = (fs->fs_optim == FS_OPTTIME) ? fs->fs_bsize : nsize;
244*0Sstevel@tonic-gate 	bno = (daddr_t)hashalloc(ip, cg, (long)bpref, request,
245*0Sstevel@tonic-gate 		(ulong_t (*)())alloccg);
246*0Sstevel@tonic-gate 	if (bno > 0) {
247*0Sstevel@tonic-gate 		*bnp = bno;
248*0Sstevel@tonic-gate 		if (nsize < request)
249*0Sstevel@tonic-gate 			(void) free(ip, bno + numfrags(fs, nsize),
250*0Sstevel@tonic-gate 			    (off_t)(request - nsize), I_NOCANCEL);
251*0Sstevel@tonic-gate 		return (0);
252*0Sstevel@tonic-gate 	}
253*0Sstevel@tonic-gate 
254*0Sstevel@tonic-gate 	/*
255*0Sstevel@tonic-gate 	 * hashalloc() failed because some other thread grabbed
256*0Sstevel@tonic-gate 	 * the last block so unwind the quota operation.  We can
257*0Sstevel@tonic-gate 	 * ignore the return because subtractions don't fail, and
258*0Sstevel@tonic-gate 	 * our caller guarantees nsize >= osize.
259*0Sstevel@tonic-gate 	 */
260*0Sstevel@tonic-gate 	(void) chkdq(ip, -(long)btodb(nsize - osize), 0, cr, (char **)NULL,
261*0Sstevel@tonic-gate 		(size_t *)NULL);
262*0Sstevel@tonic-gate 
263*0Sstevel@tonic-gate nospace:
264*0Sstevel@tonic-gate 	mutex_enter(&ufsvfsp->vfs_lock);
265*0Sstevel@tonic-gate 	if ((lbolt - ufsvfsp->vfs_lastwhinetime) > (hz << 2) &&
266*0Sstevel@tonic-gate 		(!(TRANS_ISTRANS(ufsvfsp)) || !(ip->i_flag & IQUIET))) {
267*0Sstevel@tonic-gate 		ufsvfsp->vfs_lastwhinetime = lbolt;
268*0Sstevel@tonic-gate 		cmn_err(CE_NOTE,
269*0Sstevel@tonic-gate 			"realloccg %s: file system full", fs->fs_fsmnt);
270*0Sstevel@tonic-gate 	}
271*0Sstevel@tonic-gate 	mutex_exit(&ufsvfsp->vfs_lock);
272*0Sstevel@tonic-gate 	return (ENOSPC);
273*0Sstevel@tonic-gate }
274*0Sstevel@tonic-gate 
275*0Sstevel@tonic-gate /*
276*0Sstevel@tonic-gate  * Allocate an inode in the file system.
277*0Sstevel@tonic-gate  *
278*0Sstevel@tonic-gate  * A preference may be optionally specified. If a preference is given
279*0Sstevel@tonic-gate  * the following hierarchy is used to allocate an inode:
280*0Sstevel@tonic-gate  *   1) allocate the requested inode.
281*0Sstevel@tonic-gate  *   2) allocate an inode in the same cylinder group.
282*0Sstevel@tonic-gate  *   3) quadratically rehash into other cylinder groups, until an
283*0Sstevel@tonic-gate  *	available inode is located.
284*0Sstevel@tonic-gate  * If no inode preference is given the following hierarchy is used
285*0Sstevel@tonic-gate  * to allocate an inode:
286*0Sstevel@tonic-gate  *   1) allocate an inode in cylinder group 0.
287*0Sstevel@tonic-gate  *   2) quadratically rehash into other cylinder groups, until an
288*0Sstevel@tonic-gate  *	available inode is located.
289*0Sstevel@tonic-gate  */
290*0Sstevel@tonic-gate int
291*0Sstevel@tonic-gate ufs_ialloc(struct inode *pip,
292*0Sstevel@tonic-gate     ino_t ipref, mode_t mode, struct inode **ipp, cred_t *cr)
293*0Sstevel@tonic-gate {
294*0Sstevel@tonic-gate 	struct inode *ip;
295*0Sstevel@tonic-gate 	struct fs *fs;
296*0Sstevel@tonic-gate 	int cg;
297*0Sstevel@tonic-gate 	ino_t ino;
298*0Sstevel@tonic-gate 	int err;
299*0Sstevel@tonic-gate 	int nifree;
300*0Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp = pip->i_ufsvfs;
301*0Sstevel@tonic-gate 	char *errmsg = NULL;
302*0Sstevel@tonic-gate 	size_t len;
303*0Sstevel@tonic-gate 
304*0Sstevel@tonic-gate 	ASSERT(RW_WRITE_HELD(&pip->i_rwlock));
305*0Sstevel@tonic-gate 	fs = pip->i_fs;
306*0Sstevel@tonic-gate loop:
307*0Sstevel@tonic-gate 	nifree = fs->fs_cstotal.cs_nifree;
308*0Sstevel@tonic-gate 
309*0Sstevel@tonic-gate 	if (nifree == 0)
310*0Sstevel@tonic-gate 		goto noinodes;
311*0Sstevel@tonic-gate 	/*
312*0Sstevel@tonic-gate 	 * Shadow inodes don't count against a user's inode allocation.
313*0Sstevel@tonic-gate 	 * They are an implementation method and not a resource.
314*0Sstevel@tonic-gate 	 */
315*0Sstevel@tonic-gate 	if ((mode != IFSHAD) && (mode != IFATTRDIR)) {
316*0Sstevel@tonic-gate 		err = chkiq((struct ufsvfs *)ITOV(pip)->v_vfsp->vfs_data,
317*0Sstevel@tonic-gate 			/* change */ 1, (struct inode *)NULL, crgetuid(cr), 0,
318*0Sstevel@tonic-gate 			cr, &errmsg, &len);
319*0Sstevel@tonic-gate 		/*
320*0Sstevel@tonic-gate 		 * As we haven't acquired any locks yet, dump the message
321*0Sstevel@tonic-gate 		 * now.
322*0Sstevel@tonic-gate 		 */
323*0Sstevel@tonic-gate 		if (errmsg != NULL) {
324*0Sstevel@tonic-gate 			uprintf(errmsg);
325*0Sstevel@tonic-gate 			kmem_free(errmsg, len);
326*0Sstevel@tonic-gate 			errmsg = NULL;
327*0Sstevel@tonic-gate 		}
328*0Sstevel@tonic-gate 		if (err)
329*0Sstevel@tonic-gate 			return (err);
330*0Sstevel@tonic-gate 	}
331*0Sstevel@tonic-gate 
332*0Sstevel@tonic-gate 	if (ipref >= (ulong_t)(fs->fs_ncg * fs->fs_ipg))
333*0Sstevel@tonic-gate 		ipref = 0;
334*0Sstevel@tonic-gate 	cg = (int)itog(fs, ipref);
335*0Sstevel@tonic-gate 	ino = (ino_t)hashalloc(pip, cg, (long)ipref, (int)mode,
336*0Sstevel@tonic-gate 	    (ulong_t (*)())ialloccg);
337*0Sstevel@tonic-gate 	if (ino == 0) {
338*0Sstevel@tonic-gate 		if ((mode != IFSHAD) && (mode != IFATTRDIR)) {
339*0Sstevel@tonic-gate 			/*
340*0Sstevel@tonic-gate 			 * We can safely ignore the return from chkiq()
341*0Sstevel@tonic-gate 			 * because deallocations can only fail if we
342*0Sstevel@tonic-gate 			 * can't get the user's quota info record off
343*0Sstevel@tonic-gate 			 * the disk due to an I/O error.  In that case,
344*0Sstevel@tonic-gate 			 * the quota subsystem is already messed up.
345*0Sstevel@tonic-gate 			 */
346*0Sstevel@tonic-gate 			(void) chkiq(ufsvfsp, /* change */ -1,
347*0Sstevel@tonic-gate 				(struct inode *)NULL, crgetuid(cr), 0, cr,
348*0Sstevel@tonic-gate 				(char **)NULL, (size_t *)NULL);
349*0Sstevel@tonic-gate 		}
350*0Sstevel@tonic-gate 		goto noinodes;
351*0Sstevel@tonic-gate 	}
352*0Sstevel@tonic-gate 	err = ufs_iget(pip->i_vfs, ino, ipp, cr);
353*0Sstevel@tonic-gate 	if (err) {
354*0Sstevel@tonic-gate 		if ((mode != IFSHAD) && (mode != IFATTRDIR)) {
355*0Sstevel@tonic-gate 			/*
356*0Sstevel@tonic-gate 			 * See above comment about why it is safe to ignore an
357*0Sstevel@tonic-gate 			 * error return here.
358*0Sstevel@tonic-gate 			 */
359*0Sstevel@tonic-gate 			(void) chkiq(ufsvfsp, /* change */ -1,
360*0Sstevel@tonic-gate 				(struct inode *)NULL, crgetuid(cr), 0, cr,
361*0Sstevel@tonic-gate 				(char **)NULL, (size_t *)NULL);
362*0Sstevel@tonic-gate 		}
363*0Sstevel@tonic-gate 		ufs_ifree(pip, ino, 0);
364*0Sstevel@tonic-gate 		return (err);
365*0Sstevel@tonic-gate 	}
366*0Sstevel@tonic-gate 	ip = *ipp;
367*0Sstevel@tonic-gate 	ASSERT(!ip->i_ufs_acl);
368*0Sstevel@tonic-gate 	ASSERT(!ip->i_dquot);
369*0Sstevel@tonic-gate 	rw_enter(&ip->i_contents, RW_WRITER);
370*0Sstevel@tonic-gate 
371*0Sstevel@tonic-gate 	/*
372*0Sstevel@tonic-gate 	 * Check if we really got a free inode, if not then complain
373*0Sstevel@tonic-gate 	 * and mark the inode ISTALE so that it will be freed by the
374*0Sstevel@tonic-gate 	 * ufs idle thread eventually and will not be sent to ufs_delete().
375*0Sstevel@tonic-gate 	 */
376*0Sstevel@tonic-gate 	if (ip->i_mode || (ip->i_nlink > 0)) {
377*0Sstevel@tonic-gate 		ip->i_flag |= ISTALE;
378*0Sstevel@tonic-gate 		rw_exit(&ip->i_contents);
379*0Sstevel@tonic-gate 		VN_RELE(ITOV(ip));
380*0Sstevel@tonic-gate 		cmn_err(CE_WARN,
381*0Sstevel@tonic-gate 			"%s: unexpected allocated inode %d, run fsck(1M)%s",
382*0Sstevel@tonic-gate 			fs->fs_fsmnt, (int)ino,
383*0Sstevel@tonic-gate 			(TRANS_ISTRANS(ufsvfsp) ? " -o f" : ""));
384*0Sstevel@tonic-gate 		goto loop;
385*0Sstevel@tonic-gate 	}
386*0Sstevel@tonic-gate 
387*0Sstevel@tonic-gate 	/*
388*0Sstevel@tonic-gate 	 * Check the inode has no size or data blocks.
389*0Sstevel@tonic-gate 	 * This could have happened if the truncation failed when
390*0Sstevel@tonic-gate 	 * deleting the inode. It used to be possible for this to occur
391*0Sstevel@tonic-gate 	 * if a block allocation failed when iteratively truncating a
392*0Sstevel@tonic-gate 	 * large file using logging and with a full file system.
393*0Sstevel@tonic-gate 	 * This was fixed with bug fix 4348738. However, truncation may
394*0Sstevel@tonic-gate 	 * still fail on an IO error. So in all cases for safety and
395*0Sstevel@tonic-gate 	 * security we clear out the size; the blocks allocated; and
396*0Sstevel@tonic-gate 	 * pointers to the blocks. This will ultimately cause a fsck
397*0Sstevel@tonic-gate 	 * error of un-accounted for blocks, but its a fairly benign error,
398*0Sstevel@tonic-gate 	 * and possibly the correct thing to do anyway as accesssing those
399*0Sstevel@tonic-gate 	 * blocks agains may lead to more IO errors.
400*0Sstevel@tonic-gate 	 */
401*0Sstevel@tonic-gate 	if (ip->i_size || ip->i_blocks) {
402*0Sstevel@tonic-gate 		int i;
403*0Sstevel@tonic-gate 
404*0Sstevel@tonic-gate 		if (ip->i_size) {
405*0Sstevel@tonic-gate 			cmn_err(CE_WARN,
406*0Sstevel@tonic-gate 			"%s: free inode %d had size 0x%llx, run fsck(1M)%s",
407*0Sstevel@tonic-gate 			fs->fs_fsmnt, (int)ino, ip->i_size,
408*0Sstevel@tonic-gate 			(TRANS_ISTRANS(ufsvfsp) ? " -o f" : ""));
409*0Sstevel@tonic-gate 		}
410*0Sstevel@tonic-gate 		/*
411*0Sstevel@tonic-gate 		 * Clear any garbage left behind.
412*0Sstevel@tonic-gate 		 */
413*0Sstevel@tonic-gate 		ip->i_size = (u_offset_t)0;
414*0Sstevel@tonic-gate 		ip->i_blocks = 0;
415*0Sstevel@tonic-gate 		for (i = 0; i < NDADDR; i++)
416*0Sstevel@tonic-gate 			ip->i_db[i] = 0;
417*0Sstevel@tonic-gate 		for (i = 0; i < NIADDR; i++)
418*0Sstevel@tonic-gate 			ip->i_ib[i] = 0;
419*0Sstevel@tonic-gate 	}
420*0Sstevel@tonic-gate 
421*0Sstevel@tonic-gate 	/*
422*0Sstevel@tonic-gate 	 * Initialize the link count
423*0Sstevel@tonic-gate 	 */
424*0Sstevel@tonic-gate 	ip->i_nlink = 0;
425*0Sstevel@tonic-gate 
426*0Sstevel@tonic-gate 	/*
427*0Sstevel@tonic-gate 	 * Clear the old flags
428*0Sstevel@tonic-gate 	 */
429*0Sstevel@tonic-gate 	ip->i_flag &= IREF;
430*0Sstevel@tonic-gate 
431*0Sstevel@tonic-gate 	/*
432*0Sstevel@tonic-gate 	 * Access times are not really defined if the fs is mounted
433*0Sstevel@tonic-gate 	 * with 'noatime'. But it can cause nfs clients to fail
434*0Sstevel@tonic-gate 	 * open() if the atime is not a legal value. Set a legal value
435*0Sstevel@tonic-gate 	 * here when the inode is allocated.
436*0Sstevel@tonic-gate 	 */
437*0Sstevel@tonic-gate 	if (ufsvfsp->vfs_noatime) {
438*0Sstevel@tonic-gate 		mutex_enter(&ufs_iuniqtime_lock);
439*0Sstevel@tonic-gate 		ip->i_atime = iuniqtime;
440*0Sstevel@tonic-gate 		mutex_exit(&ufs_iuniqtime_lock);
441*0Sstevel@tonic-gate 	}
442*0Sstevel@tonic-gate 	rw_exit(&ip->i_contents);
443*0Sstevel@tonic-gate 	return (0);
444*0Sstevel@tonic-gate noinodes:
445*0Sstevel@tonic-gate 	if (!(TRANS_ISTRANS(ufsvfsp)) || !(pip->i_flag & IQUIET))
446*0Sstevel@tonic-gate 		cmn_err(CE_NOTE, "%s: out of inodes\n", fs->fs_fsmnt);
447*0Sstevel@tonic-gate 	return (ENOSPC);
448*0Sstevel@tonic-gate }
449*0Sstevel@tonic-gate 
450*0Sstevel@tonic-gate /*
451*0Sstevel@tonic-gate  * Find a cylinder group to place a directory.
452*0Sstevel@tonic-gate  * Returns an inumber within the selected cylinder group.
453*0Sstevel@tonic-gate  * Note, the vfs_lock is not needed as we don't require exact cg summary info.
454*0Sstevel@tonic-gate  *
455*0Sstevel@tonic-gate  * If the switch ufs_close_dirs is set, then the policy is to use
456*0Sstevel@tonic-gate  * the current cg if it has more than 25% free inodes and more
457*0Sstevel@tonic-gate  * than 25% free blocks. Otherwise the cgs are searched from
458*0Sstevel@tonic-gate  * the beginning and the first cg with the same criteria is
459*0Sstevel@tonic-gate  * used. If that is also null then we revert to the old algorithm.
460*0Sstevel@tonic-gate  * This tends to cluster files at the beginning of the disk
461*0Sstevel@tonic-gate  * until the disk gets full.
462*0Sstevel@tonic-gate  *
463*0Sstevel@tonic-gate  * Otherwise if ufs_close_dirs is not set then the original policy is
464*0Sstevel@tonic-gate  * used which is to select from among those cylinder groups with
465*0Sstevel@tonic-gate  * above the average number of free inodes, the one with the smallest
466*0Sstevel@tonic-gate  * number of directories.
467*0Sstevel@tonic-gate  */
468*0Sstevel@tonic-gate 
469*0Sstevel@tonic-gate int ufs_close_dirs = 1;	/* allocate directories close as possible */
470*0Sstevel@tonic-gate 
471*0Sstevel@tonic-gate ino_t
472*0Sstevel@tonic-gate dirpref(inode_t *dp)
473*0Sstevel@tonic-gate {
474*0Sstevel@tonic-gate 	int cg, minndir, mincg, avgifree, mininode, minbpg, ifree;
475*0Sstevel@tonic-gate 	struct fs *fs = dp->i_fs;
476*0Sstevel@tonic-gate 
477*0Sstevel@tonic-gate 	cg = itog(fs, dp->i_number);
478*0Sstevel@tonic-gate 	mininode = fs->fs_ipg >> 2;
479*0Sstevel@tonic-gate 	minbpg = fs->fs_maxbpg >> 2;
480*0Sstevel@tonic-gate 	if (ufs_close_dirs &&
481*0Sstevel@tonic-gate 	    (fs->fs_cs(fs, cg).cs_nifree > mininode) &&
482*0Sstevel@tonic-gate 	    (fs->fs_cs(fs, cg).cs_nbfree > minbpg)) {
483*0Sstevel@tonic-gate 		return (dp->i_number);
484*0Sstevel@tonic-gate 	}
485*0Sstevel@tonic-gate 
486*0Sstevel@tonic-gate 	avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg;
487*0Sstevel@tonic-gate 	minndir = fs->fs_ipg;
488*0Sstevel@tonic-gate 	mincg = 0;
489*0Sstevel@tonic-gate 	for (cg = 0; cg < fs->fs_ncg; cg++) {
490*0Sstevel@tonic-gate 		ifree = fs->fs_cs(fs, cg).cs_nifree;
491*0Sstevel@tonic-gate 		if (ufs_close_dirs &&
492*0Sstevel@tonic-gate 		    (ifree > mininode) &&
493*0Sstevel@tonic-gate 		    (fs->fs_cs(fs, cg).cs_nbfree > minbpg)) {
494*0Sstevel@tonic-gate 			return ((ino_t)(fs->fs_ipg * cg));
495*0Sstevel@tonic-gate 		}
496*0Sstevel@tonic-gate 		if ((fs->fs_cs(fs, cg).cs_ndir < minndir) &&
497*0Sstevel@tonic-gate 		    (ifree >= avgifree)) {
498*0Sstevel@tonic-gate 			mincg = cg;
499*0Sstevel@tonic-gate 			minndir = fs->fs_cs(fs, cg).cs_ndir;
500*0Sstevel@tonic-gate 		}
501*0Sstevel@tonic-gate 	}
502*0Sstevel@tonic-gate 	return ((ino_t)(fs->fs_ipg * mincg));
503*0Sstevel@tonic-gate }
504*0Sstevel@tonic-gate 
505*0Sstevel@tonic-gate /*
506*0Sstevel@tonic-gate  * Select the desired position for the next block in a file.  The file is
507*0Sstevel@tonic-gate  * logically divided into sections. The first section is composed of the
508*0Sstevel@tonic-gate  * direct blocks. Each additional section contains fs_maxbpg blocks.
509*0Sstevel@tonic-gate  *
510*0Sstevel@tonic-gate  * If no blocks have been allocated in the first section, the policy is to
511*0Sstevel@tonic-gate  * request a block in the same cylinder group as the inode that describes
512*0Sstevel@tonic-gate  * the file. If no blocks have been allocated in any other section, the
513*0Sstevel@tonic-gate  * policy is to place the section in a cylinder group with a greater than
514*0Sstevel@tonic-gate  * average number of free blocks.  An appropriate cylinder group is found
515*0Sstevel@tonic-gate  * by using a rotor that sweeps the cylinder groups. When a new group of
516*0Sstevel@tonic-gate  * blocks is needed, the sweep begins in the cylinder group following the
517*0Sstevel@tonic-gate  * cylinder group from which the previous allocation was made. The sweep
518*0Sstevel@tonic-gate  * continues until a cylinder group with greater than the average number
519*0Sstevel@tonic-gate  * of free blocks is found. If the allocation is for the first block in an
520*0Sstevel@tonic-gate  * indirect block, the information on the previous allocation is unavailable;
521*0Sstevel@tonic-gate  * here a best guess is made based upon the logical block number being
522*0Sstevel@tonic-gate  * allocated.
523*0Sstevel@tonic-gate  *
524*0Sstevel@tonic-gate  * If a section is already partially allocated, the policy is to
525*0Sstevel@tonic-gate  * contiguously allocate fs_maxcontig blocks.  The end of one of these
526*0Sstevel@tonic-gate  * contiguous blocks and the beginning of the next is physically separated
527*0Sstevel@tonic-gate  * so that the disk head will be in transit between them for at least
528*0Sstevel@tonic-gate  * fs_rotdelay milliseconds.  This is to allow time for the processor to
529*0Sstevel@tonic-gate  * schedule another I/O transfer.
530*0Sstevel@tonic-gate  */
531*0Sstevel@tonic-gate daddr_t
532*0Sstevel@tonic-gate blkpref(struct inode *ip, daddr_t lbn, int indx, daddr32_t *bap)
533*0Sstevel@tonic-gate {
534*0Sstevel@tonic-gate 	struct fs *fs;
535*0Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp;
536*0Sstevel@tonic-gate 	int cg;
537*0Sstevel@tonic-gate 	int avgbfree, startcg;
538*0Sstevel@tonic-gate 	daddr_t nextblk;
539*0Sstevel@tonic-gate 
540*0Sstevel@tonic-gate 	ufsvfsp = ip->i_ufsvfs;
541*0Sstevel@tonic-gate 	fs = ip->i_fs;
542*0Sstevel@tonic-gate 	if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
543*0Sstevel@tonic-gate 		if (lbn < NDADDR) {
544*0Sstevel@tonic-gate 			cg = itog(fs, ip->i_number);
545*0Sstevel@tonic-gate 			return (fs->fs_fpg * cg + fs->fs_frag);
546*0Sstevel@tonic-gate 		}
547*0Sstevel@tonic-gate 		/*
548*0Sstevel@tonic-gate 		 * Find a cylinder with greater than average
549*0Sstevel@tonic-gate 		 * number of unused data blocks.
550*0Sstevel@tonic-gate 		 */
551*0Sstevel@tonic-gate 		if (indx == 0 || bap[indx - 1] == 0)
552*0Sstevel@tonic-gate 			startcg = itog(fs, ip->i_number) + lbn / fs->fs_maxbpg;
553*0Sstevel@tonic-gate 		else
554*0Sstevel@tonic-gate 			startcg = dtog(fs, bap[indx - 1]) + 1;
555*0Sstevel@tonic-gate 		startcg %= fs->fs_ncg;
556*0Sstevel@tonic-gate 
557*0Sstevel@tonic-gate 		mutex_enter(&ufsvfsp->vfs_lock);
558*0Sstevel@tonic-gate 		avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
559*0Sstevel@tonic-gate 		/*
560*0Sstevel@tonic-gate 		 * used for computing log space for writes/truncs
561*0Sstevel@tonic-gate 		 */
562*0Sstevel@tonic-gate 		ufsvfsp->vfs_avgbfree = avgbfree;
563*0Sstevel@tonic-gate 		for (cg = startcg; cg < fs->fs_ncg; cg++)
564*0Sstevel@tonic-gate 			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
565*0Sstevel@tonic-gate 				fs->fs_cgrotor = cg;
566*0Sstevel@tonic-gate 				mutex_exit(&ufsvfsp->vfs_lock);
567*0Sstevel@tonic-gate 				return (fs->fs_fpg * cg + fs->fs_frag);
568*0Sstevel@tonic-gate 			}
569*0Sstevel@tonic-gate 		for (cg = 0; cg <= startcg; cg++)
570*0Sstevel@tonic-gate 			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
571*0Sstevel@tonic-gate 				fs->fs_cgrotor = cg;
572*0Sstevel@tonic-gate 				mutex_exit(&ufsvfsp->vfs_lock);
573*0Sstevel@tonic-gate 				return (fs->fs_fpg * cg + fs->fs_frag);
574*0Sstevel@tonic-gate 			}
575*0Sstevel@tonic-gate 		mutex_exit(&ufsvfsp->vfs_lock);
576*0Sstevel@tonic-gate 		return (NULL);
577*0Sstevel@tonic-gate 	}
578*0Sstevel@tonic-gate 	/*
579*0Sstevel@tonic-gate 	 * One or more previous blocks have been laid out. If less
580*0Sstevel@tonic-gate 	 * than fs_maxcontig previous blocks are contiguous, the
581*0Sstevel@tonic-gate 	 * next block is requested contiguously, otherwise it is
582*0Sstevel@tonic-gate 	 * requested rotationally delayed by fs_rotdelay milliseconds.
583*0Sstevel@tonic-gate 	 */
584*0Sstevel@tonic-gate 	nextblk = bap[indx - 1] + fs->fs_frag;
585*0Sstevel@tonic-gate 	if (indx > fs->fs_maxcontig &&
586*0Sstevel@tonic-gate 	    bap[indx - fs->fs_maxcontig] + blkstofrags(fs, fs->fs_maxcontig)
587*0Sstevel@tonic-gate 	    != nextblk)
588*0Sstevel@tonic-gate 		return (nextblk);
589*0Sstevel@tonic-gate 	if (fs->fs_rotdelay != 0)
590*0Sstevel@tonic-gate 		/*
591*0Sstevel@tonic-gate 		 * Here we convert ms of delay to frags as:
592*0Sstevel@tonic-gate 		 * (frags) = (ms) * (rev/sec) * (sect/rev) /
593*0Sstevel@tonic-gate 		 *	((sect/frag) * (ms/sec))
594*0Sstevel@tonic-gate 		 * then round up to the next block.
595*0Sstevel@tonic-gate 		 */
596*0Sstevel@tonic-gate 		nextblk += roundup(fs->fs_rotdelay * fs->fs_rps * fs->fs_nsect /
597*0Sstevel@tonic-gate 		    (NSPF(fs) * 1000), fs->fs_frag);
598*0Sstevel@tonic-gate 	return (nextblk);
599*0Sstevel@tonic-gate }
600*0Sstevel@tonic-gate 
601*0Sstevel@tonic-gate /*
602*0Sstevel@tonic-gate  * Free a block or fragment.
603*0Sstevel@tonic-gate  *
604*0Sstevel@tonic-gate  * The specified block or fragment is placed back in the
605*0Sstevel@tonic-gate  * free map. If a fragment is deallocated, a possible
606*0Sstevel@tonic-gate  * block reassembly is checked.
607*0Sstevel@tonic-gate  */
608*0Sstevel@tonic-gate void
609*0Sstevel@tonic-gate free(struct inode *ip, daddr_t bno, off_t size, int flags)
610*0Sstevel@tonic-gate {
611*0Sstevel@tonic-gate 	struct fs *fs = ip->i_fs;
612*0Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp = ip->i_ufsvfs;
613*0Sstevel@tonic-gate 	struct cg *cgp;
614*0Sstevel@tonic-gate 	struct buf *bp;
615*0Sstevel@tonic-gate 	int cg, bmap, bbase;
616*0Sstevel@tonic-gate 	int i;
617*0Sstevel@tonic-gate 	uchar_t *blksfree;
618*0Sstevel@tonic-gate 	int *blktot;
619*0Sstevel@tonic-gate 	short *blks;
620*0Sstevel@tonic-gate 	daddr_t blkno, cylno, rpos;
621*0Sstevel@tonic-gate 
622*0Sstevel@tonic-gate 	if ((unsigned long)size > fs->fs_bsize || fragoff(fs, size) != 0) {
623*0Sstevel@tonic-gate 		(void) ufs_fault(ITOV(ip),
624*0Sstevel@tonic-gate 		"free: bad size, dev = 0x%lx, bsize = %d, size = %d, fs = %s\n",
625*0Sstevel@tonic-gate 		    ip->i_dev, fs->fs_bsize, (int)size, fs->fs_fsmnt);
626*0Sstevel@tonic-gate 		return;
627*0Sstevel@tonic-gate 	}
628*0Sstevel@tonic-gate 	cg = dtog(fs, bno);
629*0Sstevel@tonic-gate 	ASSERT(!ufs_badblock(ip, bno));
630*0Sstevel@tonic-gate 	bp = UFS_BREAD(ufsvfsp, ip->i_dev, (daddr_t)fsbtodb(fs, cgtod(fs, cg)),
631*0Sstevel@tonic-gate 		    (int)fs->fs_cgsize);
632*0Sstevel@tonic-gate 
633*0Sstevel@tonic-gate 	cgp = bp->b_un.b_cg;
634*0Sstevel@tonic-gate 	if (bp->b_flags & B_ERROR || !cg_chkmagic(cgp)) {
635*0Sstevel@tonic-gate 		brelse(bp);
636*0Sstevel@tonic-gate 		return;
637*0Sstevel@tonic-gate 	}
638*0Sstevel@tonic-gate 
639*0Sstevel@tonic-gate 	if (!(flags & I_NOCANCEL))
640*0Sstevel@tonic-gate 		TRANS_CANCEL(ufsvfsp, ldbtob(fsbtodb(fs, bno)), size, flags);
641*0Sstevel@tonic-gate 	if (flags & (I_DIR|I_IBLK|I_SHAD|I_QUOTA)) {
642*0Sstevel@tonic-gate 		TRANS_MATA_FREE(ufsvfsp, ldbtob(fsbtodb(fs, bno)), size);
643*0Sstevel@tonic-gate 	}
644*0Sstevel@tonic-gate 	blksfree = cg_blksfree(cgp);
645*0Sstevel@tonic-gate 	blktot = cg_blktot(cgp);
646*0Sstevel@tonic-gate 	mutex_enter(&ufsvfsp->vfs_lock);
647*0Sstevel@tonic-gate 	cgp->cg_time = gethrestime_sec();
648*0Sstevel@tonic-gate 	bno = dtogd(fs, bno);
649*0Sstevel@tonic-gate 	if (size == fs->fs_bsize) {
650*0Sstevel@tonic-gate 		blkno = fragstoblks(fs, bno);
651*0Sstevel@tonic-gate 		cylno = cbtocylno(fs, bno);
652*0Sstevel@tonic-gate 		rpos = cbtorpos(ufsvfsp, bno);
653*0Sstevel@tonic-gate 		blks = cg_blks(ufsvfsp, cgp, cylno);
654*0Sstevel@tonic-gate 		if (!isclrblock(fs, blksfree, blkno)) {
655*0Sstevel@tonic-gate 			mutex_exit(&ufsvfsp->vfs_lock);
656*0Sstevel@tonic-gate 			brelse(bp);
657*0Sstevel@tonic-gate 			(void) ufs_fault(ITOV(ip), "free: freeing free block, "
658*0Sstevel@tonic-gate 			    "dev:0x%lx, block:%ld, ino:%lu, fs:%s",
659*0Sstevel@tonic-gate 			    ip->i_dev, bno, ip->i_number, fs->fs_fsmnt);
660*0Sstevel@tonic-gate 			return;
661*0Sstevel@tonic-gate 		}
662*0Sstevel@tonic-gate 		setblock(fs, blksfree, blkno);
663*0Sstevel@tonic-gate 		blks[rpos]++;
664*0Sstevel@tonic-gate 		blktot[cylno]++;
665*0Sstevel@tonic-gate 		cgp->cg_cs.cs_nbfree++;		/* Log below */
666*0Sstevel@tonic-gate 		fs->fs_cstotal.cs_nbfree++;
667*0Sstevel@tonic-gate 		fs->fs_cs(fs, cg).cs_nbfree++;
668*0Sstevel@tonic-gate 	} else {
669*0Sstevel@tonic-gate 		bbase = bno - fragnum(fs, bno);
670*0Sstevel@tonic-gate 		/*
671*0Sstevel@tonic-gate 		 * Decrement the counts associated with the old frags
672*0Sstevel@tonic-gate 		 */
673*0Sstevel@tonic-gate 		bmap = blkmap(fs, blksfree, bbase);
674*0Sstevel@tonic-gate 		fragacct(fs, bmap, cgp->cg_frsum, -1);
675*0Sstevel@tonic-gate 		/*
676*0Sstevel@tonic-gate 		 * Deallocate the fragment
677*0Sstevel@tonic-gate 		 */
678*0Sstevel@tonic-gate 		for (i = 0; i < numfrags(fs, size); i++) {
679*0Sstevel@tonic-gate 			if (isset(blksfree, bno + i)) {
680*0Sstevel@tonic-gate 				brelse(bp);
681*0Sstevel@tonic-gate 				mutex_exit(&ufsvfsp->vfs_lock);
682*0Sstevel@tonic-gate 				(void) ufs_fault(ITOV(ip),
683*0Sstevel@tonic-gate 				    "free: freeing free frag, "
684*0Sstevel@tonic-gate 				    "dev:0x%lx, blk:%ld, cg:%d, "
685*0Sstevel@tonic-gate 				    "ino:%lu, fs:%s",
686*0Sstevel@tonic-gate 				    ip->i_dev,
687*0Sstevel@tonic-gate 				    bno + i,
688*0Sstevel@tonic-gate 				    cgp->cg_cgx,
689*0Sstevel@tonic-gate 				    ip->i_number,
690*0Sstevel@tonic-gate 				    fs->fs_fsmnt);
691*0Sstevel@tonic-gate 				return;
692*0Sstevel@tonic-gate 			}
693*0Sstevel@tonic-gate 			setbit(blksfree, bno + i);
694*0Sstevel@tonic-gate 		}
695*0Sstevel@tonic-gate 		cgp->cg_cs.cs_nffree += i;
696*0Sstevel@tonic-gate 		fs->fs_cstotal.cs_nffree += i;
697*0Sstevel@tonic-gate 		fs->fs_cs(fs, cg).cs_nffree += i;
698*0Sstevel@tonic-gate 		/*
699*0Sstevel@tonic-gate 		 * Add back in counts associated with the new frags
700*0Sstevel@tonic-gate 		 */
701*0Sstevel@tonic-gate 		bmap = blkmap(fs, blksfree, bbase);
702*0Sstevel@tonic-gate 		fragacct(fs, bmap, cgp->cg_frsum, 1);
703*0Sstevel@tonic-gate 		/*
704*0Sstevel@tonic-gate 		 * If a complete block has been reassembled, account for it
705*0Sstevel@tonic-gate 		 */
706*0Sstevel@tonic-gate 		blkno = fragstoblks(fs, bbase);
707*0Sstevel@tonic-gate 		if (isblock(fs, blksfree, blkno)) {
708*0Sstevel@tonic-gate 			cylno = cbtocylno(fs, bbase);
709*0Sstevel@tonic-gate 			rpos = cbtorpos(ufsvfsp, bbase);
710*0Sstevel@tonic-gate 			blks = cg_blks(ufsvfsp, cgp, cylno);
711*0Sstevel@tonic-gate 			blks[rpos]++;
712*0Sstevel@tonic-gate 			blktot[cylno]++;
713*0Sstevel@tonic-gate 			cgp->cg_cs.cs_nffree -= fs->fs_frag;
714*0Sstevel@tonic-gate 			fs->fs_cstotal.cs_nffree -= fs->fs_frag;
715*0Sstevel@tonic-gate 			fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag;
716*0Sstevel@tonic-gate 			cgp->cg_cs.cs_nbfree++;
717*0Sstevel@tonic-gate 			fs->fs_cstotal.cs_nbfree++;
718*0Sstevel@tonic-gate 			fs->fs_cs(fs, cg).cs_nbfree++;
719*0Sstevel@tonic-gate 		}
720*0Sstevel@tonic-gate 	}
721*0Sstevel@tonic-gate 	fs->fs_fmod = 1;
722*0Sstevel@tonic-gate 	ufs_notclean(ufsvfsp);
723*0Sstevel@tonic-gate 	TRANS_BUF(ufsvfsp, 0, fs->fs_cgsize, bp, DT_CG);
724*0Sstevel@tonic-gate 	TRANS_SI(ufsvfsp, fs, cg);
725*0Sstevel@tonic-gate 	bdrwrite(bp);
726*0Sstevel@tonic-gate }
727*0Sstevel@tonic-gate 
728*0Sstevel@tonic-gate /*
729*0Sstevel@tonic-gate  * Free an inode.
730*0Sstevel@tonic-gate  *
731*0Sstevel@tonic-gate  * The specified inode is placed back in the free map.
732*0Sstevel@tonic-gate  */
733*0Sstevel@tonic-gate void
734*0Sstevel@tonic-gate ufs_ifree(struct inode *ip, ino_t ino, mode_t mode)
735*0Sstevel@tonic-gate {
736*0Sstevel@tonic-gate 	struct fs *fs = ip->i_fs;
737*0Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp = ip->i_ufsvfs;
738*0Sstevel@tonic-gate 	struct cg *cgp;
739*0Sstevel@tonic-gate 	struct buf *bp;
740*0Sstevel@tonic-gate 	unsigned int inot;
741*0Sstevel@tonic-gate 	int cg;
742*0Sstevel@tonic-gate 	char *iused;
743*0Sstevel@tonic-gate 
744*0Sstevel@tonic-gate 	if (ip->i_number == ino && ip->i_mode != 0) {
745*0Sstevel@tonic-gate 		(void) ufs_fault(ITOV(ip),
746*0Sstevel@tonic-gate 		    "ufs_ifree: illegal mode: (imode) %o, (omode) %o, ino %d, "
747*0Sstevel@tonic-gate 		    "fs = %s\n",
748*0Sstevel@tonic-gate 		    ip->i_mode, mode, (int)ip->i_number, fs->fs_fsmnt);
749*0Sstevel@tonic-gate 		return;
750*0Sstevel@tonic-gate 	}
751*0Sstevel@tonic-gate 	if (ino >= fs->fs_ipg * fs->fs_ncg) {
752*0Sstevel@tonic-gate 		(void) ufs_fault(ITOV(ip),
753*0Sstevel@tonic-gate 		    "ifree: range, dev = 0x%x, ino = %d, fs = %s\n",
754*0Sstevel@tonic-gate 		    (int)ip->i_dev, (int)ino, fs->fs_fsmnt);
755*0Sstevel@tonic-gate 		return;
756*0Sstevel@tonic-gate 	}
757*0Sstevel@tonic-gate 	cg = (int)itog(fs, ino);
758*0Sstevel@tonic-gate 	bp = UFS_BREAD(ufsvfsp, ip->i_dev, (daddr_t)fsbtodb(fs, cgtod(fs, cg)),
759*0Sstevel@tonic-gate 		    (int)fs->fs_cgsize);
760*0Sstevel@tonic-gate 
761*0Sstevel@tonic-gate 	cgp = bp->b_un.b_cg;
762*0Sstevel@tonic-gate 	if (bp->b_flags & B_ERROR || !cg_chkmagic(cgp)) {
763*0Sstevel@tonic-gate 		brelse(bp);
764*0Sstevel@tonic-gate 		return;
765*0Sstevel@tonic-gate 	}
766*0Sstevel@tonic-gate 	mutex_enter(&ufsvfsp->vfs_lock);
767*0Sstevel@tonic-gate 	cgp->cg_time = gethrestime_sec();
768*0Sstevel@tonic-gate 	iused = cg_inosused(cgp);
769*0Sstevel@tonic-gate 	inot = (unsigned int)(ino % (ulong_t)fs->fs_ipg);
770*0Sstevel@tonic-gate 	if (isclr(iused, inot)) {
771*0Sstevel@tonic-gate 		mutex_exit(&ufsvfsp->vfs_lock);
772*0Sstevel@tonic-gate 		brelse(bp);
773*0Sstevel@tonic-gate 		(void) ufs_fault(ITOV(ip), "ufs_ifree: freeing free inode, "
774*0Sstevel@tonic-gate 				    "mode: (imode) %o, (omode) %o, ino:%d, "
775*0Sstevel@tonic-gate 				    "fs:%s",
776*0Sstevel@tonic-gate 				    ip->i_mode, mode, (int)ino, fs->fs_fsmnt);
777*0Sstevel@tonic-gate 		return;
778*0Sstevel@tonic-gate 	}
779*0Sstevel@tonic-gate 	clrbit(iused, inot);
780*0Sstevel@tonic-gate 
781*0Sstevel@tonic-gate 	if (inot < (ulong_t)cgp->cg_irotor)
782*0Sstevel@tonic-gate 		cgp->cg_irotor = inot;
783*0Sstevel@tonic-gate 	cgp->cg_cs.cs_nifree++;
784*0Sstevel@tonic-gate 	fs->fs_cstotal.cs_nifree++;
785*0Sstevel@tonic-gate 	fs->fs_cs(fs, cg).cs_nifree++;
786*0Sstevel@tonic-gate 	if (((mode & IFMT) == IFDIR) || ((mode & IFMT) == IFATTRDIR)) {
787*0Sstevel@tonic-gate 		cgp->cg_cs.cs_ndir--;
788*0Sstevel@tonic-gate 		fs->fs_cstotal.cs_ndir--;
789*0Sstevel@tonic-gate 		fs->fs_cs(fs, cg).cs_ndir--;
790*0Sstevel@tonic-gate 	}
791*0Sstevel@tonic-gate 	fs->fs_fmod = 1;
792*0Sstevel@tonic-gate 	ufs_notclean(ufsvfsp);
793*0Sstevel@tonic-gate 	TRANS_BUF(ufsvfsp, 0, fs->fs_cgsize, bp, DT_CG);
794*0Sstevel@tonic-gate 	TRANS_SI(ufsvfsp, fs, cg);
795*0Sstevel@tonic-gate 	bdrwrite(bp);
796*0Sstevel@tonic-gate }
797*0Sstevel@tonic-gate 
798*0Sstevel@tonic-gate /*
799*0Sstevel@tonic-gate  * Implement the cylinder overflow algorithm.
800*0Sstevel@tonic-gate  *
801*0Sstevel@tonic-gate  * The policy implemented by this algorithm is:
802*0Sstevel@tonic-gate  *   1) allocate the block in its requested cylinder group.
803*0Sstevel@tonic-gate  *   2) quadratically rehash on the cylinder group number.
804*0Sstevel@tonic-gate  *   3) brute force search for a free block.
805*0Sstevel@tonic-gate  * The size parameter means size for data blocks, mode for inodes.
806*0Sstevel@tonic-gate  */
807*0Sstevel@tonic-gate static ino_t
808*0Sstevel@tonic-gate hashalloc(struct inode *ip, int cg, long pref, int size, ulong_t (*allocator)())
809*0Sstevel@tonic-gate {
810*0Sstevel@tonic-gate 	struct fs *fs;
811*0Sstevel@tonic-gate 	int i;
812*0Sstevel@tonic-gate 	long result;
813*0Sstevel@tonic-gate 	int icg = cg;
814*0Sstevel@tonic-gate 
815*0Sstevel@tonic-gate 	fs = ip->i_fs;
816*0Sstevel@tonic-gate 	/*
817*0Sstevel@tonic-gate 	 * 1: preferred cylinder group
818*0Sstevel@tonic-gate 	 */
819*0Sstevel@tonic-gate 	result = (*allocator)(ip, cg, pref, size);
820*0Sstevel@tonic-gate 	if (result)
821*0Sstevel@tonic-gate 		return (result);
822*0Sstevel@tonic-gate 	/*
823*0Sstevel@tonic-gate 	 * 2: quadratic rehash
824*0Sstevel@tonic-gate 	 */
825*0Sstevel@tonic-gate 	for (i = 1; i < fs->fs_ncg; i *= 2) {
826*0Sstevel@tonic-gate 		cg += i;
827*0Sstevel@tonic-gate 		if (cg >= fs->fs_ncg)
828*0Sstevel@tonic-gate 			cg -= fs->fs_ncg;
829*0Sstevel@tonic-gate 		result = (*allocator)(ip, cg, 0, size);
830*0Sstevel@tonic-gate 		if (result)
831*0Sstevel@tonic-gate 			return (result);
832*0Sstevel@tonic-gate 	}
833*0Sstevel@tonic-gate 	/*
834*0Sstevel@tonic-gate 	 * 3: brute force search
835*0Sstevel@tonic-gate 	 * Note that we start at i == 2, since 0 was checked initially,
836*0Sstevel@tonic-gate 	 * and 1 is always checked in the quadratic rehash.
837*0Sstevel@tonic-gate 	 */
838*0Sstevel@tonic-gate 	cg = (icg + 2) % fs->fs_ncg;
839*0Sstevel@tonic-gate 	for (i = 2; i < fs->fs_ncg; i++) {
840*0Sstevel@tonic-gate 		result = (*allocator)(ip, cg, 0, size);
841*0Sstevel@tonic-gate 		if (result)
842*0Sstevel@tonic-gate 			return (result);
843*0Sstevel@tonic-gate 		cg++;
844*0Sstevel@tonic-gate 		if (cg == fs->fs_ncg)
845*0Sstevel@tonic-gate 			cg = 0;
846*0Sstevel@tonic-gate 	}
847*0Sstevel@tonic-gate 	return (NULL);
848*0Sstevel@tonic-gate }
849*0Sstevel@tonic-gate 
850*0Sstevel@tonic-gate /*
851*0Sstevel@tonic-gate  * Determine whether a fragment can be extended.
852*0Sstevel@tonic-gate  *
853*0Sstevel@tonic-gate  * Check to see if the necessary fragments are available, and
854*0Sstevel@tonic-gate  * if they are, allocate them.
855*0Sstevel@tonic-gate  */
856*0Sstevel@tonic-gate static daddr_t
857*0Sstevel@tonic-gate fragextend(struct inode *ip, int cg, long bprev, int osize, int nsize)
858*0Sstevel@tonic-gate {
859*0Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp = ip->i_ufsvfs;
860*0Sstevel@tonic-gate 	struct fs *fs = ip->i_fs;
861*0Sstevel@tonic-gate 	struct buf *bp;
862*0Sstevel@tonic-gate 	struct cg *cgp;
863*0Sstevel@tonic-gate 	uchar_t *blksfree;
864*0Sstevel@tonic-gate 	long bno;
865*0Sstevel@tonic-gate 	int frags, bbase;
866*0Sstevel@tonic-gate 	int i, j;
867*0Sstevel@tonic-gate 
868*0Sstevel@tonic-gate 	if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize))
869*0Sstevel@tonic-gate 		return (NULL);
870*0Sstevel@tonic-gate 	frags = numfrags(fs, nsize);
871*0Sstevel@tonic-gate 	bbase = (int)fragnum(fs, bprev);
872*0Sstevel@tonic-gate 	if (bbase > fragnum(fs, (bprev + frags - 1))) {
873*0Sstevel@tonic-gate 		/* cannot extend across a block boundary */
874*0Sstevel@tonic-gate 		return (NULL);
875*0Sstevel@tonic-gate 	}
876*0Sstevel@tonic-gate 
877*0Sstevel@tonic-gate 	bp = UFS_BREAD(ufsvfsp, ip->i_dev, (daddr_t)fsbtodb(fs, cgtod(fs, cg)),
878*0Sstevel@tonic-gate 		    (int)fs->fs_cgsize);
879*0Sstevel@tonic-gate 	cgp = bp->b_un.b_cg;
880*0Sstevel@tonic-gate 	if (bp->b_flags & B_ERROR || !cg_chkmagic(cgp)) {
881*0Sstevel@tonic-gate 		brelse(bp);
882*0Sstevel@tonic-gate 		return (NULL);
883*0Sstevel@tonic-gate 	}
884*0Sstevel@tonic-gate 
885*0Sstevel@tonic-gate 	blksfree = cg_blksfree(cgp);
886*0Sstevel@tonic-gate 	mutex_enter(&ufsvfsp->vfs_lock);
887*0Sstevel@tonic-gate 	bno = dtogd(fs, bprev);
888*0Sstevel@tonic-gate 	for (i = numfrags(fs, osize); i < frags; i++) {
889*0Sstevel@tonic-gate 		if (isclr(blksfree, bno + i)) {
890*0Sstevel@tonic-gate 			mutex_exit(&ufsvfsp->vfs_lock);
891*0Sstevel@tonic-gate 			brelse(bp);
892*0Sstevel@tonic-gate 			return (NULL);
893*0Sstevel@tonic-gate 		}
894*0Sstevel@tonic-gate 		if ((TRANS_ISCANCEL(ufsvfsp, ldbtob(fsbtodb(fs, bprev + i)),
895*0Sstevel@tonic-gate 			fs->fs_fsize))) {
896*0Sstevel@tonic-gate 			mutex_exit(&ufsvfsp->vfs_lock);
897*0Sstevel@tonic-gate 			brelse(bp);
898*0Sstevel@tonic-gate 			return (NULL);
899*0Sstevel@tonic-gate 		}
900*0Sstevel@tonic-gate 	}
901*0Sstevel@tonic-gate 
902*0Sstevel@tonic-gate 	cgp->cg_time = gethrestime_sec();
903*0Sstevel@tonic-gate 	/*
904*0Sstevel@tonic-gate 	 * The current fragment can be extended,
905*0Sstevel@tonic-gate 	 * deduct the count on fragment being extended into
906*0Sstevel@tonic-gate 	 * increase the count on the remaining fragment (if any)
907*0Sstevel@tonic-gate 	 * allocate the extended piece.
908*0Sstevel@tonic-gate 	 */
909*0Sstevel@tonic-gate 	for (i = frags; i < fs->fs_frag - bbase; i++)
910*0Sstevel@tonic-gate 		if (isclr(blksfree, bno + i))
911*0Sstevel@tonic-gate 			break;
912*0Sstevel@tonic-gate 	j = i - numfrags(fs, osize);
913*0Sstevel@tonic-gate 	cgp->cg_frsum[j]--;
914*0Sstevel@tonic-gate 	ASSERT(cgp->cg_frsum[j] >= 0);
915*0Sstevel@tonic-gate 	if (i != frags)
916*0Sstevel@tonic-gate 		cgp->cg_frsum[i - frags]++;
917*0Sstevel@tonic-gate 	for (i = numfrags(fs, osize); i < frags; i++) {
918*0Sstevel@tonic-gate 		clrbit(blksfree, bno + i);
919*0Sstevel@tonic-gate 		cgp->cg_cs.cs_nffree--;
920*0Sstevel@tonic-gate 		fs->fs_cs(fs, cg).cs_nffree--;
921*0Sstevel@tonic-gate 		fs->fs_cstotal.cs_nffree--;
922*0Sstevel@tonic-gate 	}
923*0Sstevel@tonic-gate 	fs->fs_fmod = 1;
924*0Sstevel@tonic-gate 	ufs_notclean(ufsvfsp);
925*0Sstevel@tonic-gate 	TRANS_BUF(ufsvfsp, 0, fs->fs_cgsize, bp, DT_CG);
926*0Sstevel@tonic-gate 	TRANS_SI(ufsvfsp, fs, cg);
927*0Sstevel@tonic-gate 	bdrwrite(bp);
928*0Sstevel@tonic-gate 	return ((daddr_t)bprev);
929*0Sstevel@tonic-gate }
930*0Sstevel@tonic-gate 
931*0Sstevel@tonic-gate /*
932*0Sstevel@tonic-gate  * Determine whether a block can be allocated.
933*0Sstevel@tonic-gate  *
934*0Sstevel@tonic-gate  * Check to see if a block of the apprpriate size
935*0Sstevel@tonic-gate  * is available, and if it is, allocate it.
936*0Sstevel@tonic-gate  */
937*0Sstevel@tonic-gate static daddr_t
938*0Sstevel@tonic-gate alloccg(struct inode *ip, int cg, daddr_t bpref, int size)
939*0Sstevel@tonic-gate {
940*0Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp = ip->i_ufsvfs;
941*0Sstevel@tonic-gate 	struct fs *fs = ip->i_fs;
942*0Sstevel@tonic-gate 	struct buf *bp;
943*0Sstevel@tonic-gate 	struct cg *cgp;
944*0Sstevel@tonic-gate 	uchar_t *blksfree;
945*0Sstevel@tonic-gate 	int bno, frags;
946*0Sstevel@tonic-gate 	int allocsiz;
947*0Sstevel@tonic-gate 	int i;
948*0Sstevel@tonic-gate 
949*0Sstevel@tonic-gate 	if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize)
950*0Sstevel@tonic-gate 		return (0);
951*0Sstevel@tonic-gate 	bp = UFS_BREAD(ufsvfsp, ip->i_dev, (daddr_t)fsbtodb(fs, cgtod(fs, cg)),
952*0Sstevel@tonic-gate 		    (int)fs->fs_cgsize);
953*0Sstevel@tonic-gate 
954*0Sstevel@tonic-gate 	cgp = bp->b_un.b_cg;
955*0Sstevel@tonic-gate 	if (bp->b_flags & B_ERROR || !cg_chkmagic(cgp) ||
956*0Sstevel@tonic-gate 	    (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) {
957*0Sstevel@tonic-gate 		brelse(bp);
958*0Sstevel@tonic-gate 		return (0);
959*0Sstevel@tonic-gate 	}
960*0Sstevel@tonic-gate 	blksfree = cg_blksfree(cgp);
961*0Sstevel@tonic-gate 	mutex_enter(&ufsvfsp->vfs_lock);
962*0Sstevel@tonic-gate 	cgp->cg_time = gethrestime_sec();
963*0Sstevel@tonic-gate 	if (size == fs->fs_bsize) {
964*0Sstevel@tonic-gate 		if ((bno = alloccgblk(ufsvfsp, cgp, bpref, bp)) == 0)
965*0Sstevel@tonic-gate 			goto errout;
966*0Sstevel@tonic-gate 		fs->fs_fmod = 1;
967*0Sstevel@tonic-gate 		ufs_notclean(ufsvfsp);
968*0Sstevel@tonic-gate 		TRANS_SI(ufsvfsp, fs, cg);
969*0Sstevel@tonic-gate 		bdrwrite(bp);
970*0Sstevel@tonic-gate 		return (bno);
971*0Sstevel@tonic-gate 	}
972*0Sstevel@tonic-gate 	/*
973*0Sstevel@tonic-gate 	 * Check to see if any fragments are already available
974*0Sstevel@tonic-gate 	 * allocsiz is the size which will be allocated, hacking
975*0Sstevel@tonic-gate 	 * it down to a smaller size if necessary.
976*0Sstevel@tonic-gate 	 */
977*0Sstevel@tonic-gate 	frags = numfrags(fs, size);
978*0Sstevel@tonic-gate 	for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++)
979*0Sstevel@tonic-gate 		if (cgp->cg_frsum[allocsiz] != 0)
980*0Sstevel@tonic-gate 			break;
981*0Sstevel@tonic-gate 
982*0Sstevel@tonic-gate 	if (allocsiz != fs->fs_frag)
983*0Sstevel@tonic-gate 		bno = mapsearch(ufsvfsp, cgp, bpref, allocsiz);
984*0Sstevel@tonic-gate 
985*0Sstevel@tonic-gate 	if (allocsiz == fs->fs_frag || bno < 0) {
986*0Sstevel@tonic-gate 		/*
987*0Sstevel@tonic-gate 		 * No fragments were available, so a block
988*0Sstevel@tonic-gate 		 * will be allocated and hacked up.
989*0Sstevel@tonic-gate 		 */
990*0Sstevel@tonic-gate 		if (cgp->cg_cs.cs_nbfree == 0)
991*0Sstevel@tonic-gate 			goto errout;
992*0Sstevel@tonic-gate 		if ((bno = alloccgblk(ufsvfsp, cgp, bpref, bp)) == 0)
993*0Sstevel@tonic-gate 			goto errout;
994*0Sstevel@tonic-gate 		bpref = dtogd(fs, bno);
995*0Sstevel@tonic-gate 		for (i = frags; i < fs->fs_frag; i++)
996*0Sstevel@tonic-gate 			setbit(blksfree, bpref + i);
997*0Sstevel@tonic-gate 		i = fs->fs_frag - frags;
998*0Sstevel@tonic-gate 		cgp->cg_cs.cs_nffree += i;
999*0Sstevel@tonic-gate 		fs->fs_cstotal.cs_nffree += i;
1000*0Sstevel@tonic-gate 		fs->fs_cs(fs, cg).cs_nffree += i;
1001*0Sstevel@tonic-gate 		cgp->cg_frsum[i]++;
1002*0Sstevel@tonic-gate 		fs->fs_fmod = 1;
1003*0Sstevel@tonic-gate 		ufs_notclean(ufsvfsp);
1004*0Sstevel@tonic-gate 		TRANS_SI(ufsvfsp, fs, cg);
1005*0Sstevel@tonic-gate 		bdrwrite(bp);
1006*0Sstevel@tonic-gate 		return (bno);
1007*0Sstevel@tonic-gate 	}
1008*0Sstevel@tonic-gate 
1009*0Sstevel@tonic-gate 	for (i = 0; i < frags; i++)
1010*0Sstevel@tonic-gate 		clrbit(blksfree, bno + i);
1011*0Sstevel@tonic-gate 	cgp->cg_cs.cs_nffree -= frags;
1012*0Sstevel@tonic-gate 	fs->fs_cstotal.cs_nffree -= frags;
1013*0Sstevel@tonic-gate 	fs->fs_cs(fs, cg).cs_nffree -= frags;
1014*0Sstevel@tonic-gate 	cgp->cg_frsum[allocsiz]--;
1015*0Sstevel@tonic-gate 	ASSERT(cgp->cg_frsum[allocsiz] >= 0);
1016*0Sstevel@tonic-gate 	if (frags != allocsiz) {
1017*0Sstevel@tonic-gate 		cgp->cg_frsum[allocsiz - frags]++;
1018*0Sstevel@tonic-gate 	}
1019*0Sstevel@tonic-gate 	fs->fs_fmod = 1;
1020*0Sstevel@tonic-gate 	ufs_notclean(ufsvfsp);
1021*0Sstevel@tonic-gate 	TRANS_BUF(ufsvfsp, 0, fs->fs_cgsize, bp, DT_CG);
1022*0Sstevel@tonic-gate 	TRANS_SI(ufsvfsp, fs, cg);
1023*0Sstevel@tonic-gate 	bdrwrite(bp);
1024*0Sstevel@tonic-gate 	return (cg * fs->fs_fpg + bno);
1025*0Sstevel@tonic-gate errout:
1026*0Sstevel@tonic-gate 	mutex_exit(&ufsvfsp->vfs_lock);
1027*0Sstevel@tonic-gate 	brelse(bp);
1028*0Sstevel@tonic-gate 	return (0);
1029*0Sstevel@tonic-gate }
1030*0Sstevel@tonic-gate 
1031*0Sstevel@tonic-gate /*
1032*0Sstevel@tonic-gate  * Allocate a block in a cylinder group.
1033*0Sstevel@tonic-gate  *
1034*0Sstevel@tonic-gate  * This algorithm implements the following policy:
1035*0Sstevel@tonic-gate  *   1) allocate the requested block.
1036*0Sstevel@tonic-gate  *   2) allocate a rotationally optimal block in the same cylinder.
1037*0Sstevel@tonic-gate  *   3) allocate the next available block on the block rotor for the
1038*0Sstevel@tonic-gate  *	specified cylinder group.
1039*0Sstevel@tonic-gate  * Note that this routine only allocates fs_bsize blocks; these
1040*0Sstevel@tonic-gate  * blocks may be fragmented by the routine that allocates them.
1041*0Sstevel@tonic-gate  */
1042*0Sstevel@tonic-gate static daddr_t
1043*0Sstevel@tonic-gate alloccgblk(
1044*0Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp,
1045*0Sstevel@tonic-gate 	struct cg *cgp,
1046*0Sstevel@tonic-gate 	daddr_t bpref,
1047*0Sstevel@tonic-gate 	struct buf *bp)
1048*0Sstevel@tonic-gate {
1049*0Sstevel@tonic-gate 	daddr_t bno;
1050*0Sstevel@tonic-gate 	int cylno, pos, delta, rotbl_size;
1051*0Sstevel@tonic-gate 	short *cylbp;
1052*0Sstevel@tonic-gate 	int i;
1053*0Sstevel@tonic-gate 	struct fs *fs;
1054*0Sstevel@tonic-gate 	uchar_t *blksfree;
1055*0Sstevel@tonic-gate 	daddr_t blkno, rpos, frag;
1056*0Sstevel@tonic-gate 	short *blks;
1057*0Sstevel@tonic-gate 	int32_t *blktot;
1058*0Sstevel@tonic-gate 
1059*0Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ufsvfsp->vfs_lock));
1060*0Sstevel@tonic-gate 	fs = ufsvfsp->vfs_fs;
1061*0Sstevel@tonic-gate 	blksfree = cg_blksfree(cgp);
1062*0Sstevel@tonic-gate 	if (bpref == 0) {
1063*0Sstevel@tonic-gate 		bpref = cgp->cg_rotor;
1064*0Sstevel@tonic-gate 		goto norot;
1065*0Sstevel@tonic-gate 	}
1066*0Sstevel@tonic-gate 	bpref = blknum(fs, bpref);
1067*0Sstevel@tonic-gate 	bpref = dtogd(fs, bpref);
1068*0Sstevel@tonic-gate 	/*
1069*0Sstevel@tonic-gate 	 * If the requested block is available, use it.
1070*0Sstevel@tonic-gate 	 */
1071*0Sstevel@tonic-gate 	if (isblock(fs, blksfree, (daddr_t)fragstoblks(fs, bpref))) {
1072*0Sstevel@tonic-gate 		bno = bpref;
1073*0Sstevel@tonic-gate 		goto gotit;
1074*0Sstevel@tonic-gate 	}
1075*0Sstevel@tonic-gate 	/*
1076*0Sstevel@tonic-gate 	 * Check for a block available on the same cylinder.
1077*0Sstevel@tonic-gate 	 */
1078*0Sstevel@tonic-gate 	cylno = cbtocylno(fs, bpref);
1079*0Sstevel@tonic-gate 	if (cg_blktot(cgp)[cylno] == 0)
1080*0Sstevel@tonic-gate 		goto norot;
1081*0Sstevel@tonic-gate 	if (fs->fs_cpc == 0) {
1082*0Sstevel@tonic-gate 		/*
1083*0Sstevel@tonic-gate 		 * Block layout info is not available, so just
1084*0Sstevel@tonic-gate 		 * have to take any block in this cylinder.
1085*0Sstevel@tonic-gate 		 */
1086*0Sstevel@tonic-gate 		bpref = howmany(fs->fs_spc * cylno, NSPF(fs));
1087*0Sstevel@tonic-gate 		goto norot;
1088*0Sstevel@tonic-gate 	}
1089*0Sstevel@tonic-gate 	/*
1090*0Sstevel@tonic-gate 	 * Check the summary information to see if a block is
1091*0Sstevel@tonic-gate 	 * available in the requested cylinder starting at the
1092*0Sstevel@tonic-gate 	 * requested rotational position and proceeding around.
1093*0Sstevel@tonic-gate 	 */
1094*0Sstevel@tonic-gate 	cylbp = cg_blks(ufsvfsp, cgp, cylno);
1095*0Sstevel@tonic-gate 	pos = cbtorpos(ufsvfsp, bpref);
1096*0Sstevel@tonic-gate 	for (i = pos; i < ufsvfsp->vfs_nrpos; i++)
1097*0Sstevel@tonic-gate 		if (cylbp[i] > 0)
1098*0Sstevel@tonic-gate 			break;
1099*0Sstevel@tonic-gate 	if (i == ufsvfsp->vfs_nrpos)
1100*0Sstevel@tonic-gate 		for (i = 0; i < pos; i++)
1101*0Sstevel@tonic-gate 			if (cylbp[i] > 0)
1102*0Sstevel@tonic-gate 				break;
1103*0Sstevel@tonic-gate 	if (cylbp[i] > 0) {
1104*0Sstevel@tonic-gate 		/*
1105*0Sstevel@tonic-gate 		 * Found a rotational position, now find the actual
1106*0Sstevel@tonic-gate 		 * block.  A "panic" if none is actually there.
1107*0Sstevel@tonic-gate 		 */
1108*0Sstevel@tonic-gate 
1109*0Sstevel@tonic-gate 		/*
1110*0Sstevel@tonic-gate 		 * Up to this point, "pos" has referred to the rotational
1111*0Sstevel@tonic-gate 		 * position of the desired block.  From now on, it holds
1112*0Sstevel@tonic-gate 		 * the offset of the current cylinder within a cylinder
1113*0Sstevel@tonic-gate 		 * cycle.  (A cylinder cycle refers to a set of cylinders
1114*0Sstevel@tonic-gate 		 * which are described by a single rotational table; the
1115*0Sstevel@tonic-gate 		 * size of the cycle is fs_cpc.)
1116*0Sstevel@tonic-gate 		 *
1117*0Sstevel@tonic-gate 		 * bno is set to the block number of the first block within
1118*0Sstevel@tonic-gate 		 * the current cylinder cycle.
1119*0Sstevel@tonic-gate 		 */
1120*0Sstevel@tonic-gate 
1121*0Sstevel@tonic-gate 		pos = cylno % fs->fs_cpc;
1122*0Sstevel@tonic-gate 		bno = (cylno - pos) * fs->fs_spc / NSPB(fs);
1123*0Sstevel@tonic-gate 
1124*0Sstevel@tonic-gate 		/*
1125*0Sstevel@tonic-gate 		 * The blocks within a cylinder are grouped into equivalence
1126*0Sstevel@tonic-gate 		 * classes according to their "rotational position."  There
1127*0Sstevel@tonic-gate 		 * are two tables used to determine these classes.
1128*0Sstevel@tonic-gate 		 *
1129*0Sstevel@tonic-gate 		 * The positional offset table (fs_postbl) has an entry for
1130*0Sstevel@tonic-gate 		 * each rotational position of each cylinder in a cylinder
1131*0Sstevel@tonic-gate 		 * cycle.  This entry contains the relative block number
1132*0Sstevel@tonic-gate 		 * (counting from the start of the cylinder cycle) of the
1133*0Sstevel@tonic-gate 		 * first block in the equivalence class for that position
1134*0Sstevel@tonic-gate 		 * and that cylinder.  Positions for which no blocks exist
1135*0Sstevel@tonic-gate 		 * are indicated by a -1.
1136*0Sstevel@tonic-gate 		 *
1137*0Sstevel@tonic-gate 		 * The rotational delta table (fs_rotbl) has an entry for
1138*0Sstevel@tonic-gate 		 * each block in a cylinder cycle.  This entry contains
1139*0Sstevel@tonic-gate 		 * the offset from that block to the next block in the
1140*0Sstevel@tonic-gate 		 * same equivalence class.  The last block in the class
1141*0Sstevel@tonic-gate 		 * is indicated by a zero in the table.
1142*0Sstevel@tonic-gate 		 *
1143*0Sstevel@tonic-gate 		 * The following code, then, walks through all of the blocks
1144*0Sstevel@tonic-gate 		 * in the cylinder (cylno) which we're allocating within
1145*0Sstevel@tonic-gate 		 * which are in the equivalence class for the rotational
1146*0Sstevel@tonic-gate 		 * position (i) which we're allocating within.
1147*0Sstevel@tonic-gate 		 */
1148*0Sstevel@tonic-gate 
1149*0Sstevel@tonic-gate 		if (fs_postbl(ufsvfsp, pos)[i] == -1) {
1150*0Sstevel@tonic-gate 			(void) ufs_fault(ufsvfsp->vfs_root,
1151*0Sstevel@tonic-gate 	    "alloccgblk: cyl groups corrupted, pos = %d, i = %d, fs = %s\n",
1152*0Sstevel@tonic-gate 				    pos, i, fs->fs_fsmnt);
1153*0Sstevel@tonic-gate 			return (0);
1154*0Sstevel@tonic-gate 		}
1155*0Sstevel@tonic-gate 
1156*0Sstevel@tonic-gate 		/*
1157*0Sstevel@tonic-gate 		 * There is one entry in the rotational table for each block
1158*0Sstevel@tonic-gate 		 * in the cylinder cycle.  These are whole blocks, not frags.
1159*0Sstevel@tonic-gate 		 */
1160*0Sstevel@tonic-gate 
1161*0Sstevel@tonic-gate 		rotbl_size = (fs->fs_cpc * fs->fs_spc) >>
1162*0Sstevel@tonic-gate 		    (fs->fs_fragshift + fs->fs_fsbtodb);
1163*0Sstevel@tonic-gate 
1164*0Sstevel@tonic-gate 		/*
1165*0Sstevel@tonic-gate 		 * As we start, "i" is the rotational position within which
1166*0Sstevel@tonic-gate 		 * we're searching.  After the next line, it will be a block
1167*0Sstevel@tonic-gate 		 * number (relative to the start of the cylinder cycle)
1168*0Sstevel@tonic-gate 		 * within the equivalence class of that rotational position.
1169*0Sstevel@tonic-gate 		 */
1170*0Sstevel@tonic-gate 
1171*0Sstevel@tonic-gate 		i = fs_postbl(ufsvfsp, pos)[i];
1172*0Sstevel@tonic-gate 
1173*0Sstevel@tonic-gate 		for (;;) {
1174*0Sstevel@tonic-gate 			if (isblock(fs, blksfree, (daddr_t)(bno + i))) {
1175*0Sstevel@tonic-gate 				bno = blkstofrags(fs, (bno + i));
1176*0Sstevel@tonic-gate 				goto gotit;
1177*0Sstevel@tonic-gate 			}
1178*0Sstevel@tonic-gate 			delta = fs_rotbl(fs)[i];
1179*0Sstevel@tonic-gate 			if (delta <= 0 ||		/* End of chain, or */
1180*0Sstevel@tonic-gate 			    delta + i > rotbl_size)	/* end of table? */
1181*0Sstevel@tonic-gate 				break;			/* If so, panic. */
1182*0Sstevel@tonic-gate 			i += delta;
1183*0Sstevel@tonic-gate 		}
1184*0Sstevel@tonic-gate 		(void) ufs_fault(ufsvfsp->vfs_root,
1185*0Sstevel@tonic-gate 	"alloccgblk: can't find blk in cyl, pos:%d, i:%d, fs:%s bno: %x\n",
1186*0Sstevel@tonic-gate 		    pos, i, fs->fs_fsmnt, (int)bno);
1187*0Sstevel@tonic-gate 		return (0);
1188*0Sstevel@tonic-gate 	}
1189*0Sstevel@tonic-gate norot:
1190*0Sstevel@tonic-gate 	/*
1191*0Sstevel@tonic-gate 	 * No blocks in the requested cylinder, so take
1192*0Sstevel@tonic-gate 	 * next available one in this cylinder group.
1193*0Sstevel@tonic-gate 	 */
1194*0Sstevel@tonic-gate 	bno = mapsearch(ufsvfsp, cgp, bpref, (int)fs->fs_frag);
1195*0Sstevel@tonic-gate 	if (bno < 0)
1196*0Sstevel@tonic-gate 		return (0);
1197*0Sstevel@tonic-gate 	cgp->cg_rotor = bno;
1198*0Sstevel@tonic-gate gotit:
1199*0Sstevel@tonic-gate 	blkno = fragstoblks(fs, bno);
1200*0Sstevel@tonic-gate 	frag = (cgp->cg_cgx * fs->fs_fpg) + bno;
1201*0Sstevel@tonic-gate 	if (TRANS_ISCANCEL(ufsvfsp, ldbtob(fsbtodb(fs, frag)), fs->fs_bsize))
1202*0Sstevel@tonic-gate 		goto norot;
1203*0Sstevel@tonic-gate 	clrblock(fs, blksfree, (long)blkno);
1204*0Sstevel@tonic-gate 	/*
1205*0Sstevel@tonic-gate 	 * the other cg/sb/si fields are TRANS'ed by the caller
1206*0Sstevel@tonic-gate 	 */
1207*0Sstevel@tonic-gate 	cgp->cg_cs.cs_nbfree--;
1208*0Sstevel@tonic-gate 	fs->fs_cstotal.cs_nbfree--;
1209*0Sstevel@tonic-gate 	fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--;
1210*0Sstevel@tonic-gate 	cylno = cbtocylno(fs, bno);
1211*0Sstevel@tonic-gate 	blks = cg_blks(ufsvfsp, cgp, cylno);
1212*0Sstevel@tonic-gate 	rpos = cbtorpos(ufsvfsp, bno);
1213*0Sstevel@tonic-gate 	blktot = cg_blktot(cgp);
1214*0Sstevel@tonic-gate 	blks[rpos]--;
1215*0Sstevel@tonic-gate 	blktot[cylno]--;
1216*0Sstevel@tonic-gate 	TRANS_BUF(ufsvfsp, 0, fs->fs_cgsize, bp, DT_CG);
1217*0Sstevel@tonic-gate 	fs->fs_fmod = 1;
1218*0Sstevel@tonic-gate 	return (frag);
1219*0Sstevel@tonic-gate }
1220*0Sstevel@tonic-gate 
1221*0Sstevel@tonic-gate /*
1222*0Sstevel@tonic-gate  * Determine whether an inode can be allocated.
1223*0Sstevel@tonic-gate  *
1224*0Sstevel@tonic-gate  * Check to see if an inode is available, and if it is,
1225*0Sstevel@tonic-gate  * allocate it using the following policy:
1226*0Sstevel@tonic-gate  *   1) allocate the requested inode.
1227*0Sstevel@tonic-gate  *   2) allocate the next available inode after the requested
1228*0Sstevel@tonic-gate  *	inode in the specified cylinder group.
1229*0Sstevel@tonic-gate  */
1230*0Sstevel@tonic-gate static ino_t
1231*0Sstevel@tonic-gate ialloccg(struct inode *ip, int cg, daddr_t ipref, int mode)
1232*0Sstevel@tonic-gate {
1233*0Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp = ip->i_ufsvfs;
1234*0Sstevel@tonic-gate 	struct fs *fs = ip->i_fs;
1235*0Sstevel@tonic-gate 	struct cg *cgp;
1236*0Sstevel@tonic-gate 	struct buf *bp;
1237*0Sstevel@tonic-gate 	int start, len, loc, map, i;
1238*0Sstevel@tonic-gate 	char *iused;
1239*0Sstevel@tonic-gate 
1240*0Sstevel@tonic-gate 	if (fs->fs_cs(fs, cg).cs_nifree == 0)
1241*0Sstevel@tonic-gate 		return (0);
1242*0Sstevel@tonic-gate 	bp = UFS_BREAD(ufsvfsp, ip->i_dev, (daddr_t)fsbtodb(fs, cgtod(fs, cg)),
1243*0Sstevel@tonic-gate 		    (int)fs->fs_cgsize);
1244*0Sstevel@tonic-gate 
1245*0Sstevel@tonic-gate 	cgp = bp->b_un.b_cg;
1246*0Sstevel@tonic-gate 	if (bp->b_flags & B_ERROR || !cg_chkmagic(cgp) ||
1247*0Sstevel@tonic-gate 	    cgp->cg_cs.cs_nifree == 0) {
1248*0Sstevel@tonic-gate 		brelse(bp);
1249*0Sstevel@tonic-gate 		return (0);
1250*0Sstevel@tonic-gate 	}
1251*0Sstevel@tonic-gate 	iused = cg_inosused(cgp);
1252*0Sstevel@tonic-gate 	mutex_enter(&ufsvfsp->vfs_lock);
1253*0Sstevel@tonic-gate 	/*
1254*0Sstevel@tonic-gate 	 * While we are waiting for the mutex, someone may have taken
1255*0Sstevel@tonic-gate 	 * the last available inode.  Need to recheck.
1256*0Sstevel@tonic-gate 	 */
1257*0Sstevel@tonic-gate 	if (cgp->cg_cs.cs_nifree == 0) {
1258*0Sstevel@tonic-gate 		mutex_exit(&ufsvfsp->vfs_lock);
1259*0Sstevel@tonic-gate 		brelse(bp);
1260*0Sstevel@tonic-gate 		return (0);
1261*0Sstevel@tonic-gate 	}
1262*0Sstevel@tonic-gate 
1263*0Sstevel@tonic-gate 	cgp->cg_time = gethrestime_sec();
1264*0Sstevel@tonic-gate 	if (ipref) {
1265*0Sstevel@tonic-gate 		ipref %= fs->fs_ipg;
1266*0Sstevel@tonic-gate 		if (isclr(iused, ipref))
1267*0Sstevel@tonic-gate 			goto gotit;
1268*0Sstevel@tonic-gate 	}
1269*0Sstevel@tonic-gate 	start = cgp->cg_irotor / NBBY;
1270*0Sstevel@tonic-gate 	len = howmany(fs->fs_ipg - cgp->cg_irotor, NBBY);
1271*0Sstevel@tonic-gate 	loc = skpc(0xff, (uint_t)len, &iused[start]);
1272*0Sstevel@tonic-gate 	if (loc == 0) {
1273*0Sstevel@tonic-gate 		len = start + 1;
1274*0Sstevel@tonic-gate 		start = 0;
1275*0Sstevel@tonic-gate 		loc = skpc(0xff, (uint_t)len, &iused[0]);
1276*0Sstevel@tonic-gate 		if (loc == 0) {
1277*0Sstevel@tonic-gate 			mutex_exit(&ufsvfsp->vfs_lock);
1278*0Sstevel@tonic-gate 			(void) ufs_fault(ITOV(ip),
1279*0Sstevel@tonic-gate 		    "ialloccg: map corrupted, cg = %d, irotor = %d, fs = %s\n",
1280*0Sstevel@tonic-gate 				    cg, (int)cgp->cg_irotor, fs->fs_fsmnt);
1281*0Sstevel@tonic-gate 			return (0);
1282*0Sstevel@tonic-gate 		}
1283*0Sstevel@tonic-gate 	}
1284*0Sstevel@tonic-gate 	i = start + len - loc;
1285*0Sstevel@tonic-gate 	map = iused[i];
1286*0Sstevel@tonic-gate 	ipref = i * NBBY;
1287*0Sstevel@tonic-gate 	for (i = 1; i < (1 << NBBY); i <<= 1, ipref++) {
1288*0Sstevel@tonic-gate 		if ((map & i) == 0) {
1289*0Sstevel@tonic-gate 			cgp->cg_irotor = ipref;
1290*0Sstevel@tonic-gate 			goto gotit;
1291*0Sstevel@tonic-gate 		}
1292*0Sstevel@tonic-gate 	}
1293*0Sstevel@tonic-gate 
1294*0Sstevel@tonic-gate 	mutex_exit(&ufsvfsp->vfs_lock);
1295*0Sstevel@tonic-gate 	(void) ufs_fault(ITOV(ip), "ialloccg: block not in mapfs = %s",
1296*0Sstevel@tonic-gate 							    fs->fs_fsmnt);
1297*0Sstevel@tonic-gate 	return (0);
1298*0Sstevel@tonic-gate gotit:
1299*0Sstevel@tonic-gate 	setbit(iused, ipref);
1300*0Sstevel@tonic-gate 	cgp->cg_cs.cs_nifree--;
1301*0Sstevel@tonic-gate 	fs->fs_cstotal.cs_nifree--;
1302*0Sstevel@tonic-gate 	fs->fs_cs(fs, cg).cs_nifree--;
1303*0Sstevel@tonic-gate 	if (((mode & IFMT) == IFDIR) || ((mode & IFMT) == IFATTRDIR)) {
1304*0Sstevel@tonic-gate 		cgp->cg_cs.cs_ndir++;
1305*0Sstevel@tonic-gate 		fs->fs_cstotal.cs_ndir++;
1306*0Sstevel@tonic-gate 		fs->fs_cs(fs, cg).cs_ndir++;
1307*0Sstevel@tonic-gate 	}
1308*0Sstevel@tonic-gate 	fs->fs_fmod = 1;
1309*0Sstevel@tonic-gate 	ufs_notclean(ufsvfsp);
1310*0Sstevel@tonic-gate 	TRANS_BUF(ufsvfsp, 0, fs->fs_cgsize, bp, DT_CG);
1311*0Sstevel@tonic-gate 	TRANS_SI(ufsvfsp, fs, cg);
1312*0Sstevel@tonic-gate 	bdrwrite(bp);
1313*0Sstevel@tonic-gate 	return (cg * fs->fs_ipg + ipref);
1314*0Sstevel@tonic-gate }
1315*0Sstevel@tonic-gate 
1316*0Sstevel@tonic-gate /*
1317*0Sstevel@tonic-gate  * Find a block of the specified size in the specified cylinder group.
1318*0Sstevel@tonic-gate  *
1319*0Sstevel@tonic-gate  * It is a panic if a request is made to find a block if none are
1320*0Sstevel@tonic-gate  * available.
1321*0Sstevel@tonic-gate  */
1322*0Sstevel@tonic-gate static daddr_t
1323*0Sstevel@tonic-gate mapsearch(struct ufsvfs *ufsvfsp, struct cg *cgp, daddr_t bpref,
1324*0Sstevel@tonic-gate 	int allocsiz)
1325*0Sstevel@tonic-gate {
1326*0Sstevel@tonic-gate 	struct fs *fs	= ufsvfsp->vfs_fs;
1327*0Sstevel@tonic-gate 	daddr_t bno, cfrag;
1328*0Sstevel@tonic-gate 	int start, len, loc, i, last, first, secondtime;
1329*0Sstevel@tonic-gate 	int blk, field, subfield, pos;
1330*0Sstevel@tonic-gate 	int gotit;
1331*0Sstevel@tonic-gate 
1332*0Sstevel@tonic-gate 	/*
1333*0Sstevel@tonic-gate 	 * ufsvfs->vfs_lock is held when calling this.
1334*0Sstevel@tonic-gate 	 */
1335*0Sstevel@tonic-gate 	/*
1336*0Sstevel@tonic-gate 	 * Find the fragment by searching through the
1337*0Sstevel@tonic-gate 	 * free block map for an appropriate bit pattern.
1338*0Sstevel@tonic-gate 	 */
1339*0Sstevel@tonic-gate 	if (bpref)
1340*0Sstevel@tonic-gate 		start = dtogd(fs, bpref) / NBBY;
1341*0Sstevel@tonic-gate 	else
1342*0Sstevel@tonic-gate 		start = cgp->cg_frotor / NBBY;
1343*0Sstevel@tonic-gate 	/*
1344*0Sstevel@tonic-gate 	 * the following loop performs two scans -- the first scan
1345*0Sstevel@tonic-gate 	 * searches the bottom half of the array for a match and the
1346*0Sstevel@tonic-gate 	 * second scan searches the top half of the array.  The loops
1347*0Sstevel@tonic-gate 	 * have been merged just to make things difficult.
1348*0Sstevel@tonic-gate 	 */
1349*0Sstevel@tonic-gate 	first = start;
1350*0Sstevel@tonic-gate 	last = howmany(fs->fs_fpg, NBBY);
1351*0Sstevel@tonic-gate 	secondtime = 0;
1352*0Sstevel@tonic-gate 	cfrag = cgp->cg_cgx * fs->fs_fpg;
1353*0Sstevel@tonic-gate 	while (first < last) {
1354*0Sstevel@tonic-gate 		len = last - first;
1355*0Sstevel@tonic-gate 		/*
1356*0Sstevel@tonic-gate 		 * search the array for a match
1357*0Sstevel@tonic-gate 		 */
1358*0Sstevel@tonic-gate 		loc = scanc((unsigned)len, (uchar_t *)&cg_blksfree(cgp)[first],
1359*0Sstevel@tonic-gate 			(uchar_t *)fragtbl[fs->fs_frag],
1360*0Sstevel@tonic-gate 			(int)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
1361*0Sstevel@tonic-gate 		/*
1362*0Sstevel@tonic-gate 		 * match found
1363*0Sstevel@tonic-gate 		 */
1364*0Sstevel@tonic-gate 		if (loc) {
1365*0Sstevel@tonic-gate 			bno = (last - loc) * NBBY;
1366*0Sstevel@tonic-gate 
1367*0Sstevel@tonic-gate 			/*
1368*0Sstevel@tonic-gate 			 * Found the byte in the map, sift
1369*0Sstevel@tonic-gate 			 * through the bits to find the selected frag
1370*0Sstevel@tonic-gate 			 */
1371*0Sstevel@tonic-gate 			cgp->cg_frotor = bno;
1372*0Sstevel@tonic-gate 			gotit = 0;
1373*0Sstevel@tonic-gate 			for (i = bno + NBBY; bno < i; bno += fs->fs_frag) {
1374*0Sstevel@tonic-gate 				blk = blkmap(fs, cg_blksfree(cgp), bno);
1375*0Sstevel@tonic-gate 				blk <<= 1;
1376*0Sstevel@tonic-gate 				field = around[allocsiz];
1377*0Sstevel@tonic-gate 				subfield = inside[allocsiz];
1378*0Sstevel@tonic-gate 				for (pos = 0;
1379*0Sstevel@tonic-gate 				    pos <= fs->fs_frag - allocsiz;
1380*0Sstevel@tonic-gate 				    pos++) {
1381*0Sstevel@tonic-gate 					if ((blk & field) == subfield) {
1382*0Sstevel@tonic-gate 						gotit++;
1383*0Sstevel@tonic-gate 						break;
1384*0Sstevel@tonic-gate 					}
1385*0Sstevel@tonic-gate 					field <<= 1;
1386*0Sstevel@tonic-gate 					subfield <<= 1;
1387*0Sstevel@tonic-gate 				}
1388*0Sstevel@tonic-gate 				if (gotit)
1389*0Sstevel@tonic-gate 					break;
1390*0Sstevel@tonic-gate 			}
1391*0Sstevel@tonic-gate 			bno += pos;
1392*0Sstevel@tonic-gate 
1393*0Sstevel@tonic-gate 			/*
1394*0Sstevel@tonic-gate 			 * success if block is *not* being converted from
1395*0Sstevel@tonic-gate 			 * metadata into userdata (harpy).  If so, ignore.
1396*0Sstevel@tonic-gate 			 */
1397*0Sstevel@tonic-gate 			if (!TRANS_ISCANCEL(ufsvfsp,
1398*0Sstevel@tonic-gate 				ldbtob(fsbtodb(fs, (cfrag+bno))),
1399*0Sstevel@tonic-gate 				allocsiz * fs->fs_fsize))
1400*0Sstevel@tonic-gate 				return (bno);
1401*0Sstevel@tonic-gate 			/*
1402*0Sstevel@tonic-gate 			 * keep looking -- this block is being converted
1403*0Sstevel@tonic-gate 			 */
1404*0Sstevel@tonic-gate 			first = (last - loc) + 1;
1405*0Sstevel@tonic-gate 			loc = 0;
1406*0Sstevel@tonic-gate 			if (first < last)
1407*0Sstevel@tonic-gate 				continue;
1408*0Sstevel@tonic-gate 		}
1409*0Sstevel@tonic-gate 		/*
1410*0Sstevel@tonic-gate 		 * no usable matches in bottom half -- now search the top half
1411*0Sstevel@tonic-gate 		 */
1412*0Sstevel@tonic-gate 		if (secondtime)
1413*0Sstevel@tonic-gate 			/*
1414*0Sstevel@tonic-gate 			 * no usable matches in top half -- all done
1415*0Sstevel@tonic-gate 			 */
1416*0Sstevel@tonic-gate 			break;
1417*0Sstevel@tonic-gate 		secondtime = 1;
1418*0Sstevel@tonic-gate 		last = start + 1;
1419*0Sstevel@tonic-gate 		first = 0;
1420*0Sstevel@tonic-gate 	}
1421*0Sstevel@tonic-gate 	/*
1422*0Sstevel@tonic-gate 	 * no usable matches
1423*0Sstevel@tonic-gate 	 */
1424*0Sstevel@tonic-gate 	return ((daddr_t)-1);
1425*0Sstevel@tonic-gate }
1426*0Sstevel@tonic-gate 
1427*0Sstevel@tonic-gate #define	UFSNADDR (NDADDR + NIADDR)	/* NADDR applies to (obsolete) S5FS */
1428*0Sstevel@tonic-gate #define	IB(i)	(NDADDR + (i))	/* index of i'th indirect block ptr */
1429*0Sstevel@tonic-gate #define	SINGLE	0		/* single indirect block ptr */
1430*0Sstevel@tonic-gate #define	DOUBLE	1		/* double indirect block ptr */
1431*0Sstevel@tonic-gate #define	TRIPLE	2		/* triple indirect block ptr */
1432*0Sstevel@tonic-gate 
1433*0Sstevel@tonic-gate /*
1434*0Sstevel@tonic-gate  * Free storage space associated with the specified inode.  The portion
1435*0Sstevel@tonic-gate  * to be freed is specified by lp->l_start and lp->l_len (already
1436*0Sstevel@tonic-gate  * normalized to a "whence" of 0).
1437*0Sstevel@tonic-gate  *
1438*0Sstevel@tonic-gate  * This is an experimental facility whose continued existence is not
1439*0Sstevel@tonic-gate  * guaranteed.  Currently, we only support the special case
1440*0Sstevel@tonic-gate  * of l_len == 0, meaning free to end of file.
1441*0Sstevel@tonic-gate  *
1442*0Sstevel@tonic-gate  * Blocks are freed in reverse order.  This FILO algorithm will tend to
1443*0Sstevel@tonic-gate  * maintain a contiguous free list much longer than FIFO.
1444*0Sstevel@tonic-gate  * See also ufs_itrunc() in ufs_inode.c.
1445*0Sstevel@tonic-gate  *
1446*0Sstevel@tonic-gate  * Bug: unused bytes in the last retained block are not cleared.
1447*0Sstevel@tonic-gate  * This may result in a "hole" in the file that does not read as zeroes.
1448*0Sstevel@tonic-gate  */
1449*0Sstevel@tonic-gate /* ARGSUSED */
1450*0Sstevel@tonic-gate int
1451*0Sstevel@tonic-gate ufs_freesp(struct vnode *vp, struct flock64 *lp, int flag, cred_t *cr)
1452*0Sstevel@tonic-gate {
1453*0Sstevel@tonic-gate 	int i;
1454*0Sstevel@tonic-gate 	struct inode *ip = VTOI(vp);
1455*0Sstevel@tonic-gate 	int error;
1456*0Sstevel@tonic-gate 
1457*0Sstevel@tonic-gate 	ASSERT(vp->v_type == VREG);
1458*0Sstevel@tonic-gate 	ASSERT(lp->l_start >= 0);	/* checked by convoff */
1459*0Sstevel@tonic-gate 
1460*0Sstevel@tonic-gate 	if (lp->l_len != 0)
1461*0Sstevel@tonic-gate 		return (EINVAL);
1462*0Sstevel@tonic-gate 
1463*0Sstevel@tonic-gate 	rw_enter(&ip->i_contents, RW_READER);
1464*0Sstevel@tonic-gate 	if (ip->i_size == (u_offset_t)lp->l_start) {
1465*0Sstevel@tonic-gate 		rw_exit(&ip->i_contents);
1466*0Sstevel@tonic-gate 		return (0);
1467*0Sstevel@tonic-gate 	}
1468*0Sstevel@tonic-gate 
1469*0Sstevel@tonic-gate 	/*
1470*0Sstevel@tonic-gate 	 * Check if there is any active mandatory lock on the
1471*0Sstevel@tonic-gate 	 * range that will be truncated/expanded.
1472*0Sstevel@tonic-gate 	 */
1473*0Sstevel@tonic-gate 	if (MANDLOCK(vp, ip->i_mode)) {
1474*0Sstevel@tonic-gate 		offset_t save_start;
1475*0Sstevel@tonic-gate 
1476*0Sstevel@tonic-gate 		save_start = lp->l_start;
1477*0Sstevel@tonic-gate 
1478*0Sstevel@tonic-gate 		if (ip->i_size < lp->l_start) {
1479*0Sstevel@tonic-gate 			/*
1480*0Sstevel@tonic-gate 			 * "Truncate up" case: need to make sure there
1481*0Sstevel@tonic-gate 			 * is no lock beyond current end-of-file. To
1482*0Sstevel@tonic-gate 			 * do so, we need to set l_start to the size
1483*0Sstevel@tonic-gate 			 * of the file temporarily.
1484*0Sstevel@tonic-gate 			 */
1485*0Sstevel@tonic-gate 			lp->l_start = ip->i_size;
1486*0Sstevel@tonic-gate 		}
1487*0Sstevel@tonic-gate 		lp->l_type = F_WRLCK;
1488*0Sstevel@tonic-gate 		lp->l_sysid = 0;
1489*0Sstevel@tonic-gate 		lp->l_pid = ttoproc(curthread)->p_pid;
1490*0Sstevel@tonic-gate 		i = (flag & (FNDELAY|FNONBLOCK)) ? 0 : SLPFLCK;
1491*0Sstevel@tonic-gate 		rw_exit(&ip->i_contents);
1492*0Sstevel@tonic-gate 		if ((i = reclock(vp, lp, i, 0, lp->l_start, NULL)) != 0 ||
1493*0Sstevel@tonic-gate 		    lp->l_type != F_UNLCK) {
1494*0Sstevel@tonic-gate 			return (i ? i : EAGAIN);
1495*0Sstevel@tonic-gate 		}
1496*0Sstevel@tonic-gate 		rw_enter(&ip->i_contents, RW_READER);
1497*0Sstevel@tonic-gate 
1498*0Sstevel@tonic-gate 		lp->l_start = save_start;
1499*0Sstevel@tonic-gate 	}
1500*0Sstevel@tonic-gate 
1501*0Sstevel@tonic-gate 	/*
1502*0Sstevel@tonic-gate 	 * Make sure a write isn't in progress (allocating blocks)
1503*0Sstevel@tonic-gate 	 * by acquiring i_rwlock (we promised ufs_bmap we wouldn't
1504*0Sstevel@tonic-gate 	 * truncate while it was allocating blocks).
1505*0Sstevel@tonic-gate 	 * Grab the locks in the right order.
1506*0Sstevel@tonic-gate 	 */
1507*0Sstevel@tonic-gate 	rw_exit(&ip->i_contents);
1508*0Sstevel@tonic-gate 	rw_enter(&ip->i_rwlock, RW_WRITER);
1509*0Sstevel@tonic-gate 	error = TRANS_ITRUNC(ip, (u_offset_t)lp->l_start, 0, cr);
1510*0Sstevel@tonic-gate 	rw_exit(&ip->i_rwlock);
1511*0Sstevel@tonic-gate 	return (error);
1512*0Sstevel@tonic-gate }
1513*0Sstevel@tonic-gate 
1514*0Sstevel@tonic-gate /*
1515*0Sstevel@tonic-gate  * Find a cg with as close to nb contiguous bytes as possible
1516*0Sstevel@tonic-gate  *	THIS MAY TAKE MANY DISK READS!
1517*0Sstevel@tonic-gate  *
1518*0Sstevel@tonic-gate  * Implemented in an attempt to allocate contiguous blocks for
1519*0Sstevel@tonic-gate  * writing the ufs log file to, minimizing future disk head seeking
1520*0Sstevel@tonic-gate  */
1521*0Sstevel@tonic-gate daddr_t
1522*0Sstevel@tonic-gate contigpref(ufsvfs_t *ufsvfsp, size_t nb)
1523*0Sstevel@tonic-gate {
1524*0Sstevel@tonic-gate 	struct fs	*fs	= ufsvfsp->vfs_fs;
1525*0Sstevel@tonic-gate 	daddr_t		nblk	= lblkno(fs, blkroundup(fs, nb));
1526*0Sstevel@tonic-gate 	daddr_t		savebno, curbno, cgbno;
1527*0Sstevel@tonic-gate 	int		cg, cgblks, savecg, savenblk, curnblk;
1528*0Sstevel@tonic-gate 	uchar_t		*blksfree;
1529*0Sstevel@tonic-gate 	buf_t		*bp;
1530*0Sstevel@tonic-gate 	struct cg	*cgp;
1531*0Sstevel@tonic-gate 
1532*0Sstevel@tonic-gate 	savenblk = 0;
1533*0Sstevel@tonic-gate 	savecg = 0;
1534*0Sstevel@tonic-gate 	savebno = 0;
1535*0Sstevel@tonic-gate 	for (cg = 0; cg < fs->fs_ncg; ++cg) {
1536*0Sstevel@tonic-gate 
1537*0Sstevel@tonic-gate 		/* not enough free blks for a contig check */
1538*0Sstevel@tonic-gate 		if (fs->fs_cs(fs, cg).cs_nbfree < nblk)
1539*0Sstevel@tonic-gate 			continue;
1540*0Sstevel@tonic-gate 
1541*0Sstevel@tonic-gate 		/*
1542*0Sstevel@tonic-gate 		 * find the largest contiguous range in this cg
1543*0Sstevel@tonic-gate 		 */
1544*0Sstevel@tonic-gate 		bp = UFS_BREAD(ufsvfsp, ufsvfsp->vfs_dev,
1545*0Sstevel@tonic-gate 			(daddr_t)fsbtodb(fs, cgtod(fs, cg)),
1546*0Sstevel@tonic-gate 			(int)fs->fs_cgsize);
1547*0Sstevel@tonic-gate 		cgp = bp->b_un.b_cg;
1548*0Sstevel@tonic-gate 		if (bp->b_flags & B_ERROR || !cg_chkmagic(cgp)) {
1549*0Sstevel@tonic-gate 			brelse(bp);
1550*0Sstevel@tonic-gate 			continue;
1551*0Sstevel@tonic-gate 		}
1552*0Sstevel@tonic-gate 		blksfree = cg_blksfree(cgp);	    /* free array */
1553*0Sstevel@tonic-gate 		cgblks = fragstoblks(fs, fs->fs_fpg); /* blks in free array */
1554*0Sstevel@tonic-gate 		cgbno = 0;
1555*0Sstevel@tonic-gate 		while (cgbno < cgblks && savenblk < nblk) {
1556*0Sstevel@tonic-gate 			/* find a free block */
1557*0Sstevel@tonic-gate 			for (; cgbno < cgblks; ++cgbno)
1558*0Sstevel@tonic-gate 				if (isblock(fs, blksfree, cgbno))
1559*0Sstevel@tonic-gate 					break;
1560*0Sstevel@tonic-gate 			curbno = cgbno;
1561*0Sstevel@tonic-gate 			/* count the number of free blocks */
1562*0Sstevel@tonic-gate 			for (curnblk = 0; cgbno < cgblks; ++cgbno) {
1563*0Sstevel@tonic-gate 				if (!isblock(fs, blksfree, cgbno))
1564*0Sstevel@tonic-gate 					break;
1565*0Sstevel@tonic-gate 				if (++curnblk >= nblk)
1566*0Sstevel@tonic-gate 					break;
1567*0Sstevel@tonic-gate 			}
1568*0Sstevel@tonic-gate 			if (curnblk > savenblk) {
1569*0Sstevel@tonic-gate 				savecg = cg;
1570*0Sstevel@tonic-gate 				savenblk = curnblk;
1571*0Sstevel@tonic-gate 				savebno = curbno;
1572*0Sstevel@tonic-gate 			}
1573*0Sstevel@tonic-gate 		}
1574*0Sstevel@tonic-gate 		brelse(bp);
1575*0Sstevel@tonic-gate 		if (savenblk >= nblk)
1576*0Sstevel@tonic-gate 			break;
1577*0Sstevel@tonic-gate 	}
1578*0Sstevel@tonic-gate 
1579*0Sstevel@tonic-gate 	/* convert block offset in cg to frag offset in cg */
1580*0Sstevel@tonic-gate 	savebno = blkstofrags(fs, savebno);
1581*0Sstevel@tonic-gate 
1582*0Sstevel@tonic-gate 	/* convert frag offset in cg to frag offset in fs */
1583*0Sstevel@tonic-gate 	savebno += (savecg * fs->fs_fpg);
1584*0Sstevel@tonic-gate 
1585*0Sstevel@tonic-gate 	return (savebno);
1586*0Sstevel@tonic-gate }
1587