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