xref: /dragonfly/sys/vfs/ufs/ffs_alloc.c (revision 984263bc)
1 /*
2  * Copyright (c) 1982, 1986, 1989, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *	@(#)ffs_alloc.c	8.18 (Berkeley) 5/26/95
34  * $FreeBSD: src/sys/ufs/ffs/ffs_alloc.c,v 1.64.2.2 2001/09/21 19:15:21 dillon Exp $
35  */
36 
37 #include "opt_quota.h"
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/buf.h>
42 #include <sys/conf.h>
43 #include <sys/proc.h>
44 #include <sys/vnode.h>
45 #include <sys/mount.h>
46 #include <sys/kernel.h>
47 #include <sys/sysctl.h>
48 #include <sys/syslog.h>
49 
50 #include <ufs/ufs/quota.h>
51 #include <ufs/ufs/inode.h>
52 #include <ufs/ufs/ufs_extern.h>
53 #include <ufs/ufs/ufsmount.h>
54 
55 #include <ufs/ffs/fs.h>
56 #include <ufs/ffs/ffs_extern.h>
57 
58 typedef ufs_daddr_t allocfcn_t __P((struct inode *ip, int cg, ufs_daddr_t bpref,
59 				  int size));
60 
61 static ufs_daddr_t ffs_alloccg __P((struct inode *, int, ufs_daddr_t, int));
62 static ufs_daddr_t
63 	      ffs_alloccgblk __P((struct inode *, struct buf *, ufs_daddr_t));
64 #ifdef DIAGNOSTIC
65 static int	ffs_checkblk __P((struct inode *, ufs_daddr_t, long));
66 #endif
67 static void	ffs_clusteracct	__P((struct fs *, struct cg *, ufs_daddr_t,
68 				     int));
69 static ufs_daddr_t ffs_clusteralloc __P((struct inode *, int, ufs_daddr_t,
70 	    int));
71 static ino_t	ffs_dirpref __P((struct inode *));
72 static ufs_daddr_t ffs_fragextend __P((struct inode *, int, long, int, int));
73 static void	ffs_fserr __P((struct fs *, u_int, char *));
74 static u_long	ffs_hashalloc
75 		    __P((struct inode *, int, long, int, allocfcn_t *));
76 static ino_t	ffs_nodealloccg __P((struct inode *, int, ufs_daddr_t, int));
77 static ufs_daddr_t ffs_mapsearch __P((struct fs *, struct cg *, ufs_daddr_t,
78 	    int));
79 
80 /*
81  * Allocate a block in the file system.
82  *
83  * The size of the requested block is given, which must be some
84  * multiple of fs_fsize and <= fs_bsize.
85  * A preference may be optionally specified. If a preference is given
86  * the following hierarchy is used to allocate a block:
87  *   1) allocate the requested block.
88  *   2) allocate a rotationally optimal block in the same cylinder.
89  *   3) allocate a block in the same cylinder group.
90  *   4) quadradically rehash into other cylinder groups, until an
91  *      available block is located.
92  * If no block preference is given the following heirarchy is used
93  * to allocate a block:
94  *   1) allocate a block in the cylinder group that contains the
95  *      inode for the file.
96  *   2) quadradically rehash into other cylinder groups, until an
97  *      available block is located.
98  */
99 int
100 ffs_alloc(ip, lbn, bpref, size, cred, bnp)
101 	register struct inode *ip;
102 	ufs_daddr_t lbn, bpref;
103 	int size;
104 	struct ucred *cred;
105 	ufs_daddr_t *bnp;
106 {
107 	register struct fs *fs;
108 	ufs_daddr_t bno;
109 	int cg;
110 #ifdef QUOTA
111 	int error;
112 #endif
113 
114 	*bnp = 0;
115 	fs = ip->i_fs;
116 #ifdef DIAGNOSTIC
117 	if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0) {
118 		printf("dev = %s, bsize = %ld, size = %d, fs = %s\n",
119 		    devtoname(ip->i_dev), (long)fs->fs_bsize, size,
120 		    fs->fs_fsmnt);
121 		panic("ffs_alloc: bad size");
122 	}
123 	if (cred == NOCRED)
124 		panic("ffs_alloc: missing credential");
125 #endif /* DIAGNOSTIC */
126 	if (size == fs->fs_bsize && fs->fs_cstotal.cs_nbfree == 0)
127 		goto nospace;
128 	if (cred->cr_uid != 0 &&
129 	    freespace(fs, fs->fs_minfree) - numfrags(fs, size) < 0)
130 		goto nospace;
131 #ifdef QUOTA
132 	error = chkdq(ip, (long)btodb(size), cred, 0);
133 	if (error)
134 		return (error);
135 #endif
136 	if (bpref >= fs->fs_size)
137 		bpref = 0;
138 	if (bpref == 0)
139 		cg = ino_to_cg(fs, ip->i_number);
140 	else
141 		cg = dtog(fs, bpref);
142 	bno = (ufs_daddr_t)ffs_hashalloc(ip, cg, (long)bpref, size,
143 					 ffs_alloccg);
144 	if (bno > 0) {
145 		ip->i_blocks += btodb(size);
146 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
147 		*bnp = bno;
148 		return (0);
149 	}
150 #ifdef QUOTA
151 	/*
152 	 * Restore user's disk quota because allocation failed.
153 	 */
154 	(void) chkdq(ip, (long)-btodb(size), cred, FORCE);
155 #endif
156 nospace:
157 	ffs_fserr(fs, cred->cr_uid, "file system full");
158 	uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt);
159 	return (ENOSPC);
160 }
161 
162 /*
163  * Reallocate a fragment to a bigger size
164  *
165  * The number and size of the old block is given, and a preference
166  * and new size is also specified. The allocator attempts to extend
167  * the original block. Failing that, the regular block allocator is
168  * invoked to get an appropriate block.
169  */
170 int
171 ffs_realloccg(ip, lbprev, bpref, osize, nsize, cred, bpp)
172 	register struct inode *ip;
173 	ufs_daddr_t lbprev;
174 	ufs_daddr_t bpref;
175 	int osize, nsize;
176 	struct ucred *cred;
177 	struct buf **bpp;
178 {
179 	register struct fs *fs;
180 	struct buf *bp;
181 	int cg, request, error;
182 	ufs_daddr_t bprev, bno;
183 
184 	*bpp = 0;
185 	fs = ip->i_fs;
186 #ifdef DIAGNOSTIC
187 	if ((u_int)osize > fs->fs_bsize || fragoff(fs, osize) != 0 ||
188 	    (u_int)nsize > fs->fs_bsize || fragoff(fs, nsize) != 0) {
189 		printf(
190 		"dev = %s, bsize = %ld, osize = %d, nsize = %d, fs = %s\n",
191 		    devtoname(ip->i_dev), (long)fs->fs_bsize, osize,
192 		    nsize, fs->fs_fsmnt);
193 		panic("ffs_realloccg: bad size");
194 	}
195 	if (cred == NOCRED)
196 		panic("ffs_realloccg: missing credential");
197 #endif /* DIAGNOSTIC */
198 	if (cred->cr_uid != 0 &&
199 	    freespace(fs, fs->fs_minfree) -  numfrags(fs, nsize - osize) < 0)
200 		goto nospace;
201 	if ((bprev = ip->i_db[lbprev]) == 0) {
202 		printf("dev = %s, bsize = %ld, bprev = %ld, fs = %s\n",
203 		    devtoname(ip->i_dev), (long)fs->fs_bsize, (long)bprev,
204 		    fs->fs_fsmnt);
205 		panic("ffs_realloccg: bad bprev");
206 	}
207 	/*
208 	 * Allocate the extra space in the buffer.
209 	 */
210 	error = bread(ITOV(ip), lbprev, osize, NOCRED, &bp);
211 	if (error) {
212 		brelse(bp);
213 		return (error);
214 	}
215 
216 	if( bp->b_blkno == bp->b_lblkno) {
217 		if( lbprev >= NDADDR)
218 			panic("ffs_realloccg: lbprev out of range");
219 		bp->b_blkno = fsbtodb(fs, bprev);
220 	}
221 
222 #ifdef QUOTA
223 	error = chkdq(ip, (long)btodb(nsize - osize), cred, 0);
224 	if (error) {
225 		brelse(bp);
226 		return (error);
227 	}
228 #endif
229 	/*
230 	 * Check for extension in the existing location.
231 	 */
232 	cg = dtog(fs, bprev);
233 	bno = ffs_fragextend(ip, cg, (long)bprev, osize, nsize);
234 	if (bno) {
235 		if (bp->b_blkno != fsbtodb(fs, bno))
236 			panic("ffs_realloccg: bad blockno");
237 		ip->i_blocks += btodb(nsize - osize);
238 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
239 		allocbuf(bp, nsize);
240 		bp->b_flags |= B_DONE;
241 		bzero((char *)bp->b_data + osize, (u_int)nsize - osize);
242 		*bpp = bp;
243 		return (0);
244 	}
245 	/*
246 	 * Allocate a new disk location.
247 	 */
248 	if (bpref >= fs->fs_size)
249 		bpref = 0;
250 	switch ((int)fs->fs_optim) {
251 	case FS_OPTSPACE:
252 		/*
253 		 * Allocate an exact sized fragment. Although this makes
254 		 * best use of space, we will waste time relocating it if
255 		 * the file continues to grow. If the fragmentation is
256 		 * less than half of the minimum free reserve, we choose
257 		 * to begin optimizing for time.
258 		 */
259 		request = nsize;
260 		if (fs->fs_minfree <= 5 ||
261 		    fs->fs_cstotal.cs_nffree >
262 		    (off_t)fs->fs_dsize * fs->fs_minfree / (2 * 100))
263 			break;
264 		log(LOG_NOTICE, "%s: optimization changed from SPACE to TIME\n",
265 			fs->fs_fsmnt);
266 		fs->fs_optim = FS_OPTTIME;
267 		break;
268 	case FS_OPTTIME:
269 		/*
270 		 * At this point we have discovered a file that is trying to
271 		 * grow a small fragment to a larger fragment. To save time,
272 		 * we allocate a full sized block, then free the unused portion.
273 		 * If the file continues to grow, the `ffs_fragextend' call
274 		 * above will be able to grow it in place without further
275 		 * copying. If aberrant programs cause disk fragmentation to
276 		 * grow within 2% of the free reserve, we choose to begin
277 		 * optimizing for space.
278 		 */
279 		request = fs->fs_bsize;
280 		if (fs->fs_cstotal.cs_nffree <
281 		    (off_t)fs->fs_dsize * (fs->fs_minfree - 2) / 100)
282 			break;
283 		log(LOG_NOTICE, "%s: optimization changed from TIME to SPACE\n",
284 			fs->fs_fsmnt);
285 		fs->fs_optim = FS_OPTSPACE;
286 		break;
287 	default:
288 		printf("dev = %s, optim = %ld, fs = %s\n",
289 		    devtoname(ip->i_dev), (long)fs->fs_optim, fs->fs_fsmnt);
290 		panic("ffs_realloccg: bad optim");
291 		/* NOTREACHED */
292 	}
293 	bno = (ufs_daddr_t)ffs_hashalloc(ip, cg, (long)bpref, request,
294 					 ffs_alloccg);
295 	if (bno > 0) {
296 		bp->b_blkno = fsbtodb(fs, bno);
297 		if (!DOINGSOFTDEP(ITOV(ip)))
298 			ffs_blkfree(ip, bprev, (long)osize);
299 		if (nsize < request)
300 			ffs_blkfree(ip, bno + numfrags(fs, nsize),
301 			    (long)(request - nsize));
302 		ip->i_blocks += btodb(nsize - osize);
303 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
304 		allocbuf(bp, nsize);
305 		bp->b_flags |= B_DONE;
306 		bzero((char *)bp->b_data + osize, (u_int)nsize - osize);
307 		*bpp = bp;
308 		return (0);
309 	}
310 #ifdef QUOTA
311 	/*
312 	 * Restore user's disk quota because allocation failed.
313 	 */
314 	(void) chkdq(ip, (long)-btodb(nsize - osize), cred, FORCE);
315 #endif
316 	brelse(bp);
317 nospace:
318 	/*
319 	 * no space available
320 	 */
321 	ffs_fserr(fs, cred->cr_uid, "file system full");
322 	uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt);
323 	return (ENOSPC);
324 }
325 
326 SYSCTL_NODE(_vfs, OID_AUTO, ffs, CTLFLAG_RW, 0, "FFS filesystem");
327 
328 /*
329  * Reallocate a sequence of blocks into a contiguous sequence of blocks.
330  *
331  * The vnode and an array of buffer pointers for a range of sequential
332  * logical blocks to be made contiguous is given. The allocator attempts
333  * to find a range of sequential blocks starting as close as possible to
334  * an fs_rotdelay offset from the end of the allocation for the logical
335  * block immediately preceeding the current range. If successful, the
336  * physical block numbers in the buffer pointers and in the inode are
337  * changed to reflect the new allocation. If unsuccessful, the allocation
338  * is left unchanged. The success in doing the reallocation is returned.
339  * Note that the error return is not reflected back to the user. Rather
340  * the previous block allocation will be used.
341  */
342 static int doasyncfree = 1;
343 SYSCTL_INT(_vfs_ffs, FFS_ASYNCFREE, doasyncfree, CTLFLAG_RW, &doasyncfree, 0, "");
344 
345 static int doreallocblks = 1;
346 SYSCTL_INT(_vfs_ffs, FFS_REALLOCBLKS, doreallocblks, CTLFLAG_RW, &doreallocblks, 0, "");
347 
348 #ifdef DEBUG
349 static volatile int prtrealloc = 0;
350 #endif
351 
352 int
353 ffs_reallocblks(ap)
354 	struct vop_reallocblks_args /* {
355 		struct vnode *a_vp;
356 		struct cluster_save *a_buflist;
357 	} */ *ap;
358 {
359 	struct fs *fs;
360 	struct inode *ip;
361 	struct vnode *vp;
362 	struct buf *sbp, *ebp;
363 	ufs_daddr_t *bap, *sbap, *ebap = 0;
364 	struct cluster_save *buflist;
365 	ufs_daddr_t start_lbn, end_lbn, soff, newblk, blkno;
366 	struct indir start_ap[NIADDR + 1], end_ap[NIADDR + 1], *idp;
367 	int i, len, start_lvl, end_lvl, pref, ssize;
368 
369 	if (doreallocblks == 0)
370 		return (ENOSPC);
371 	vp = ap->a_vp;
372 	ip = VTOI(vp);
373 	fs = ip->i_fs;
374 	if (fs->fs_contigsumsize <= 0)
375 		return (ENOSPC);
376 	buflist = ap->a_buflist;
377 	len = buflist->bs_nchildren;
378 	start_lbn = buflist->bs_children[0]->b_lblkno;
379 	end_lbn = start_lbn + len - 1;
380 #ifdef DIAGNOSTIC
381 	for (i = 0; i < len; i++)
382 		if (!ffs_checkblk(ip,
383 		   dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
384 			panic("ffs_reallocblks: unallocated block 1");
385 	for (i = 1; i < len; i++)
386 		if (buflist->bs_children[i]->b_lblkno != start_lbn + i)
387 			panic("ffs_reallocblks: non-logical cluster");
388 	blkno = buflist->bs_children[0]->b_blkno;
389 	ssize = fsbtodb(fs, fs->fs_frag);
390 	for (i = 1; i < len - 1; i++)
391 		if (buflist->bs_children[i]->b_blkno != blkno + (i * ssize))
392 			panic("ffs_reallocblks: non-physical cluster %d", i);
393 #endif
394 	/*
395 	 * If the latest allocation is in a new cylinder group, assume that
396 	 * the filesystem has decided to move and do not force it back to
397 	 * the previous cylinder group.
398 	 */
399 	if (dtog(fs, dbtofsb(fs, buflist->bs_children[0]->b_blkno)) !=
400 	    dtog(fs, dbtofsb(fs, buflist->bs_children[len - 1]->b_blkno)))
401 		return (ENOSPC);
402 	if (ufs_getlbns(vp, start_lbn, start_ap, &start_lvl) ||
403 	    ufs_getlbns(vp, end_lbn, end_ap, &end_lvl))
404 		return (ENOSPC);
405 	/*
406 	 * Get the starting offset and block map for the first block.
407 	 */
408 	if (start_lvl == 0) {
409 		sbap = &ip->i_db[0];
410 		soff = start_lbn;
411 	} else {
412 		idp = &start_ap[start_lvl - 1];
413 		if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &sbp)) {
414 			brelse(sbp);
415 			return (ENOSPC);
416 		}
417 		sbap = (ufs_daddr_t *)sbp->b_data;
418 		soff = idp->in_off;
419 	}
420 	/*
421 	 * Find the preferred location for the cluster.
422 	 */
423 	pref = ffs_blkpref(ip, start_lbn, soff, sbap);
424 	/*
425 	 * If the block range spans two block maps, get the second map.
426 	 */
427 	if (end_lvl == 0 || (idp = &end_ap[end_lvl - 1])->in_off + 1 >= len) {
428 		ssize = len;
429 	} else {
430 #ifdef DIAGNOSTIC
431 		if (start_ap[start_lvl-1].in_lbn == idp->in_lbn)
432 			panic("ffs_reallocblk: start == end");
433 #endif
434 		ssize = len - (idp->in_off + 1);
435 		if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &ebp))
436 			goto fail;
437 		ebap = (ufs_daddr_t *)ebp->b_data;
438 	}
439 	/*
440 	 * Search the block map looking for an allocation of the desired size.
441 	 */
442 	if ((newblk = (ufs_daddr_t)ffs_hashalloc(ip, dtog(fs, pref), (long)pref,
443 	    len, ffs_clusteralloc)) == 0)
444 		goto fail;
445 	/*
446 	 * We have found a new contiguous block.
447 	 *
448 	 * First we have to replace the old block pointers with the new
449 	 * block pointers in the inode and indirect blocks associated
450 	 * with the file.
451 	 */
452 #ifdef DEBUG
453 	if (prtrealloc)
454 		printf("realloc: ino %d, lbns %d-%d\n\told:", ip->i_number,
455 		    start_lbn, end_lbn);
456 #endif
457 	blkno = newblk;
458 	for (bap = &sbap[soff], i = 0; i < len; i++, blkno += fs->fs_frag) {
459 		if (i == ssize) {
460 			bap = ebap;
461 			soff = -i;
462 		}
463 #ifdef DIAGNOSTIC
464 		if (!ffs_checkblk(ip,
465 		   dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
466 			panic("ffs_reallocblks: unallocated block 2");
467 		if (dbtofsb(fs, buflist->bs_children[i]->b_blkno) != *bap)
468 			panic("ffs_reallocblks: alloc mismatch");
469 #endif
470 #ifdef DEBUG
471 		if (prtrealloc)
472 			printf(" %d,", *bap);
473 #endif
474 		if (DOINGSOFTDEP(vp)) {
475 			if (sbap == &ip->i_db[0] && i < ssize)
476 				softdep_setup_allocdirect(ip, start_lbn + i,
477 				    blkno, *bap, fs->fs_bsize, fs->fs_bsize,
478 				    buflist->bs_children[i]);
479 			else
480 				softdep_setup_allocindir_page(ip, start_lbn + i,
481 				    i < ssize ? sbp : ebp, soff + i, blkno,
482 				    *bap, buflist->bs_children[i]);
483 		}
484 		*bap++ = blkno;
485 	}
486 	/*
487 	 * Next we must write out the modified inode and indirect blocks.
488 	 * For strict correctness, the writes should be synchronous since
489 	 * the old block values may have been written to disk. In practise
490 	 * they are almost never written, but if we are concerned about
491 	 * strict correctness, the `doasyncfree' flag should be set to zero.
492 	 *
493 	 * The test on `doasyncfree' should be changed to test a flag
494 	 * that shows whether the associated buffers and inodes have
495 	 * been written. The flag should be set when the cluster is
496 	 * started and cleared whenever the buffer or inode is flushed.
497 	 * We can then check below to see if it is set, and do the
498 	 * synchronous write only when it has been cleared.
499 	 */
500 	if (sbap != &ip->i_db[0]) {
501 		if (doasyncfree)
502 			bdwrite(sbp);
503 		else
504 			bwrite(sbp);
505 	} else {
506 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
507 		if (!doasyncfree)
508 			UFS_UPDATE(vp, 1);
509 	}
510 	if (ssize < len) {
511 		if (doasyncfree)
512 			bdwrite(ebp);
513 		else
514 			bwrite(ebp);
515 	}
516 	/*
517 	 * Last, free the old blocks and assign the new blocks to the buffers.
518 	 */
519 #ifdef DEBUG
520 	if (prtrealloc)
521 		printf("\n\tnew:");
522 #endif
523 	for (blkno = newblk, i = 0; i < len; i++, blkno += fs->fs_frag) {
524 		if (!DOINGSOFTDEP(vp))
525 			ffs_blkfree(ip,
526 			    dbtofsb(fs, buflist->bs_children[i]->b_blkno),
527 			    fs->fs_bsize);
528 		buflist->bs_children[i]->b_blkno = fsbtodb(fs, blkno);
529 #ifdef DIAGNOSTIC
530 		if (!ffs_checkblk(ip,
531 		   dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
532 			panic("ffs_reallocblks: unallocated block 3");
533 #endif
534 #ifdef DEBUG
535 		if (prtrealloc)
536 			printf(" %d,", blkno);
537 #endif
538 	}
539 #ifdef DEBUG
540 	if (prtrealloc) {
541 		prtrealloc--;
542 		printf("\n");
543 	}
544 #endif
545 	return (0);
546 
547 fail:
548 	if (ssize < len)
549 		brelse(ebp);
550 	if (sbap != &ip->i_db[0])
551 		brelse(sbp);
552 	return (ENOSPC);
553 }
554 
555 /*
556  * Allocate an inode in the file system.
557  *
558  * If allocating a directory, use ffs_dirpref to select the inode.
559  * If allocating in a directory, the following hierarchy is followed:
560  *   1) allocate the preferred inode.
561  *   2) allocate an inode in the same cylinder group.
562  *   3) quadradically rehash into other cylinder groups, until an
563  *      available inode is located.
564  * If no inode preference is given the following heirarchy is used
565  * to allocate an inode:
566  *   1) allocate an inode in cylinder group 0.
567  *   2) quadradically rehash into other cylinder groups, until an
568  *      available inode is located.
569  */
570 int
571 ffs_valloc(pvp, mode, cred, vpp)
572 	struct vnode *pvp;
573 	int mode;
574 	struct ucred *cred;
575 	struct vnode **vpp;
576 {
577 	register struct inode *pip;
578 	register struct fs *fs;
579 	register struct inode *ip;
580 	ino_t ino, ipref;
581 	int cg, error;
582 
583 	*vpp = NULL;
584 	pip = VTOI(pvp);
585 	fs = pip->i_fs;
586 	if (fs->fs_cstotal.cs_nifree == 0)
587 		goto noinodes;
588 
589 	if ((mode & IFMT) == IFDIR)
590 		ipref = ffs_dirpref(pip);
591 	else
592 		ipref = pip->i_number;
593 	if (ipref >= fs->fs_ncg * fs->fs_ipg)
594 		ipref = 0;
595 	cg = ino_to_cg(fs, ipref);
596 	/*
597 	 * Track number of dirs created one after another
598 	 * in a same cg without intervening by files.
599 	 */
600 	if ((mode & IFMT) == IFDIR) {
601 		if (fs->fs_contigdirs[cg] < 255)
602 			fs->fs_contigdirs[cg]++;
603 	} else {
604 		if (fs->fs_contigdirs[cg] > 0)
605 			fs->fs_contigdirs[cg]--;
606 	}
607 	ino = (ino_t)ffs_hashalloc(pip, cg, (long)ipref, mode,
608 					(allocfcn_t *)ffs_nodealloccg);
609 	if (ino == 0)
610 		goto noinodes;
611 	error = VFS_VGET(pvp->v_mount, ino, vpp);
612 	if (error) {
613 		UFS_VFREE(pvp, ino, mode);
614 		return (error);
615 	}
616 	ip = VTOI(*vpp);
617 	if (ip->i_mode) {
618 		printf("mode = 0%o, inum = %lu, fs = %s\n",
619 		    ip->i_mode, (u_long)ip->i_number, fs->fs_fsmnt);
620 		panic("ffs_valloc: dup alloc");
621 	}
622 	if (ip->i_blocks) {				/* XXX */
623 		printf("free inode %s/%lu had %ld blocks\n",
624 		    fs->fs_fsmnt, (u_long)ino, (long)ip->i_blocks);
625 		ip->i_blocks = 0;
626 	}
627 	ip->i_flags = 0;
628 	/*
629 	 * Set up a new generation number for this inode.
630 	 */
631 	if (ip->i_gen == 0 || ++ip->i_gen == 0)
632 		ip->i_gen = random() / 2 + 1;
633 	return (0);
634 noinodes:
635 	ffs_fserr(fs, cred->cr_uid, "out of inodes");
636 	uprintf("\n%s: create/symlink failed, no inodes free\n", fs->fs_fsmnt);
637 	return (ENOSPC);
638 }
639 
640 /*
641  * Find a cylinder group to place a directory.
642  *
643  * The policy implemented by this algorithm is to allocate a
644  * directory inode in the same cylinder group as its parent
645  * directory, but also to reserve space for its files inodes
646  * and data. Restrict the number of directories which may be
647  * allocated one after another in the same cylinder group
648  * without intervening allocation of files.
649  *
650  * If we allocate a first level directory then force allocation
651  * in another cylinder group.
652  */
653 static ino_t
654 ffs_dirpref(pip)
655 	struct inode *pip;
656 {
657 	register struct fs *fs;
658 	int cg, prefcg, dirsize, cgsize;
659 	int avgifree, avgbfree, avgndir, curdirsize;
660 	int minifree, minbfree, maxndir;
661 	int mincg, minndir;
662 	int maxcontigdirs;
663 
664 	fs = pip->i_fs;
665 
666 	avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg;
667 	avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
668 	avgndir = fs->fs_cstotal.cs_ndir / fs->fs_ncg;
669 
670 	/*
671 	 * Force allocation in another cg if creating a first level dir.
672 	 */
673 	if (ITOV(pip)->v_flag & VROOT) {
674 		prefcg = arc4random() % fs->fs_ncg;
675 		mincg = prefcg;
676 		minndir = fs->fs_ipg;
677 		for (cg = prefcg; cg < fs->fs_ncg; cg++)
678 			if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
679 			    fs->fs_cs(fs, cg).cs_nifree >= avgifree &&
680 			    fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
681 				mincg = cg;
682 				minndir = fs->fs_cs(fs, cg).cs_ndir;
683 			}
684 		for (cg = 0; cg < prefcg; cg++)
685 			if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
686 			    fs->fs_cs(fs, cg).cs_nifree >= avgifree &&
687 			    fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
688 				mincg = cg;
689 				minndir = fs->fs_cs(fs, cg).cs_ndir;
690 			}
691 		return ((ino_t)(fs->fs_ipg * mincg));
692 	}
693 
694 	/*
695 	 * Count various limits which used for
696 	 * optimal allocation of a directory inode.
697 	 */
698 	maxndir = min(avgndir + fs->fs_ipg / 16, fs->fs_ipg);
699 	minifree = avgifree - fs->fs_ipg / 4;
700 	if (minifree < 0)
701 		minifree = 0;
702 	minbfree = avgbfree - fs->fs_fpg / fs->fs_frag / 4;
703 	if (minbfree < 0)
704 		minbfree = 0;
705 	cgsize = fs->fs_fsize * fs->fs_fpg;
706 	dirsize = fs->fs_avgfilesize * fs->fs_avgfpdir;
707 	curdirsize = avgndir ? (cgsize - avgbfree * fs->fs_bsize) / avgndir : 0;
708 	if (dirsize < curdirsize)
709 		dirsize = curdirsize;
710 	maxcontigdirs = min(cgsize / dirsize, 255);
711 	if (fs->fs_avgfpdir > 0)
712 		maxcontigdirs = min(maxcontigdirs,
713 				    fs->fs_ipg / fs->fs_avgfpdir);
714 	if (maxcontigdirs == 0)
715 		maxcontigdirs = 1;
716 
717 	/*
718 	 * Limit number of dirs in one cg and reserve space for
719 	 * regular files, but only if we have no deficit in
720 	 * inodes or space.
721 	 */
722 	prefcg = ino_to_cg(fs, pip->i_number);
723 	for (cg = prefcg; cg < fs->fs_ncg; cg++)
724 		if (fs->fs_cs(fs, cg).cs_ndir < maxndir &&
725 		    fs->fs_cs(fs, cg).cs_nifree >= minifree &&
726 	    	    fs->fs_cs(fs, cg).cs_nbfree >= minbfree) {
727 			if (fs->fs_contigdirs[cg] < maxcontigdirs)
728 				return ((ino_t)(fs->fs_ipg * cg));
729 		}
730 	for (cg = 0; cg < prefcg; cg++)
731 		if (fs->fs_cs(fs, cg).cs_ndir < maxndir &&
732 		    fs->fs_cs(fs, cg).cs_nifree >= minifree &&
733 	    	    fs->fs_cs(fs, cg).cs_nbfree >= minbfree) {
734 			if (fs->fs_contigdirs[cg] < maxcontigdirs)
735 				return ((ino_t)(fs->fs_ipg * cg));
736 		}
737 	/*
738 	 * This is a backstop when we have deficit in space.
739 	 */
740 	for (cg = prefcg; cg < fs->fs_ncg; cg++)
741 		if (fs->fs_cs(fs, cg).cs_nifree >= avgifree)
742 			return ((ino_t)(fs->fs_ipg * cg));
743 	for (cg = 0; cg < prefcg; cg++)
744 		if (fs->fs_cs(fs, cg).cs_nifree >= avgifree)
745 			break;
746 	return ((ino_t)(fs->fs_ipg * cg));
747 }
748 
749 /*
750  * Select the desired position for the next block in a file.  The file is
751  * logically divided into sections. The first section is composed of the
752  * direct blocks. Each additional section contains fs_maxbpg blocks.
753  *
754  * If no blocks have been allocated in the first section, the policy is to
755  * request a block in the same cylinder group as the inode that describes
756  * the file. If no blocks have been allocated in any other section, the
757  * policy is to place the section in a cylinder group with a greater than
758  * average number of free blocks.  An appropriate cylinder group is found
759  * by using a rotor that sweeps the cylinder groups. When a new group of
760  * blocks is needed, the sweep begins in the cylinder group following the
761  * cylinder group from which the previous allocation was made. The sweep
762  * continues until a cylinder group with greater than the average number
763  * of free blocks is found. If the allocation is for the first block in an
764  * indirect block, the information on the previous allocation is unavailable;
765  * here a best guess is made based upon the logical block number being
766  * allocated.
767  *
768  * If a section is already partially allocated, the policy is to
769  * contiguously allocate fs_maxcontig blocks.  The end of one of these
770  * contiguous blocks and the beginning of the next is physically separated
771  * so that the disk head will be in transit between them for at least
772  * fs_rotdelay milliseconds.  This is to allow time for the processor to
773  * schedule another I/O transfer.
774  */
775 ufs_daddr_t
776 ffs_blkpref(ip, lbn, indx, bap)
777 	struct inode *ip;
778 	ufs_daddr_t lbn;
779 	int indx;
780 	ufs_daddr_t *bap;
781 {
782 	register struct fs *fs;
783 	register int cg;
784 	int avgbfree, startcg;
785 	ufs_daddr_t nextblk;
786 
787 	fs = ip->i_fs;
788 	if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
789 		if (lbn < NDADDR + NINDIR(fs)) {
790 			cg = ino_to_cg(fs, ip->i_number);
791 			return (fs->fs_fpg * cg + fs->fs_frag);
792 		}
793 		/*
794 		 * Find a cylinder with greater than average number of
795 		 * unused data blocks.
796 		 */
797 		if (indx == 0 || bap[indx - 1] == 0)
798 			startcg =
799 			    ino_to_cg(fs, ip->i_number) + lbn / fs->fs_maxbpg;
800 		else
801 			startcg = dtog(fs, bap[indx - 1]) + 1;
802 		startcg %= fs->fs_ncg;
803 		avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
804 		for (cg = startcg; cg < fs->fs_ncg; cg++)
805 			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
806 				fs->fs_cgrotor = cg;
807 				return (fs->fs_fpg * cg + fs->fs_frag);
808 			}
809 		for (cg = 0; cg <= startcg; cg++)
810 			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
811 				fs->fs_cgrotor = cg;
812 				return (fs->fs_fpg * cg + fs->fs_frag);
813 			}
814 		return (0);
815 	}
816 	/*
817 	 * One or more previous blocks have been laid out. If less
818 	 * than fs_maxcontig previous blocks are contiguous, the
819 	 * next block is requested contiguously, otherwise it is
820 	 * requested rotationally delayed by fs_rotdelay milliseconds.
821 	 */
822 	nextblk = bap[indx - 1] + fs->fs_frag;
823 	if (fs->fs_rotdelay == 0 || indx < fs->fs_maxcontig ||
824 	    bap[indx - fs->fs_maxcontig] +
825 	    blkstofrags(fs, fs->fs_maxcontig) != nextblk)
826 		return (nextblk);
827 	/*
828 	 * Here we convert ms of delay to frags as:
829 	 * (frags) = (ms) * (rev/sec) * (sect/rev) /
830 	 *	((sect/frag) * (ms/sec))
831 	 * then round up to the next block.
832 	 */
833 	nextblk += roundup(fs->fs_rotdelay * fs->fs_rps * fs->fs_nsect /
834 	    (NSPF(fs) * 1000), fs->fs_frag);
835 	return (nextblk);
836 }
837 
838 /*
839  * Implement the cylinder overflow algorithm.
840  *
841  * The policy implemented by this algorithm is:
842  *   1) allocate the block in its requested cylinder group.
843  *   2) quadradically rehash on the cylinder group number.
844  *   3) brute force search for a free block.
845  */
846 /*VARARGS5*/
847 static u_long
848 ffs_hashalloc(ip, cg, pref, size, allocator)
849 	struct inode *ip;
850 	int cg;
851 	long pref;
852 	int size;	/* size for data blocks, mode for inodes */
853 	allocfcn_t *allocator;
854 {
855 	register struct fs *fs;
856 	long result;	/* XXX why not same type as we return? */
857 	int i, icg = cg;
858 
859 	fs = ip->i_fs;
860 	/*
861 	 * 1: preferred cylinder group
862 	 */
863 	result = (*allocator)(ip, cg, pref, size);
864 	if (result)
865 		return (result);
866 	/*
867 	 * 2: quadratic rehash
868 	 */
869 	for (i = 1; i < fs->fs_ncg; i *= 2) {
870 		cg += i;
871 		if (cg >= fs->fs_ncg)
872 			cg -= fs->fs_ncg;
873 		result = (*allocator)(ip, cg, 0, size);
874 		if (result)
875 			return (result);
876 	}
877 	/*
878 	 * 3: brute force search
879 	 * Note that we start at i == 2, since 0 was checked initially,
880 	 * and 1 is always checked in the quadratic rehash.
881 	 */
882 	cg = (icg + 2) % fs->fs_ncg;
883 	for (i = 2; i < fs->fs_ncg; i++) {
884 		result = (*allocator)(ip, cg, 0, size);
885 		if (result)
886 			return (result);
887 		cg++;
888 		if (cg == fs->fs_ncg)
889 			cg = 0;
890 	}
891 	return (0);
892 }
893 
894 /*
895  * Determine whether a fragment can be extended.
896  *
897  * Check to see if the necessary fragments are available, and
898  * if they are, allocate them.
899  */
900 static ufs_daddr_t
901 ffs_fragextend(ip, cg, bprev, osize, nsize)
902 	struct inode *ip;
903 	int cg;
904 	long bprev;
905 	int osize, nsize;
906 {
907 	register struct fs *fs;
908 	register struct cg *cgp;
909 	struct buf *bp;
910 	long bno;
911 	int frags, bbase;
912 	int i, error;
913 	u_int8_t *blksfree;
914 
915 	fs = ip->i_fs;
916 	if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize))
917 		return (0);
918 	frags = numfrags(fs, nsize);
919 	bbase = fragnum(fs, bprev);
920 	if (bbase > fragnum(fs, (bprev + frags - 1))) {
921 		/* cannot extend across a block boundary */
922 		return (0);
923 	}
924 	error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
925 		(int)fs->fs_cgsize, NOCRED, &bp);
926 	if (error) {
927 		brelse(bp);
928 		return (0);
929 	}
930 	cgp = (struct cg *)bp->b_data;
931 	if (!cg_chkmagic(cgp)) {
932 		brelse(bp);
933 		return (0);
934 	}
935 	bp->b_xflags |= BX_BKGRDWRITE;
936 	cgp->cg_time = time_second;
937 	bno = dtogd(fs, bprev);
938 	blksfree = cg_blksfree(cgp);
939 	for (i = numfrags(fs, osize); i < frags; i++)
940 		if (isclr(blksfree, bno + i)) {
941 			brelse(bp);
942 			return (0);
943 		}
944 	/*
945 	 * the current fragment can be extended
946 	 * deduct the count on fragment being extended into
947 	 * increase the count on the remaining fragment (if any)
948 	 * allocate the extended piece
949 	 */
950 	for (i = frags; i < fs->fs_frag - bbase; i++)
951 		if (isclr(blksfree, bno + i))
952 			break;
953 	cgp->cg_frsum[i - numfrags(fs, osize)]--;
954 	if (i != frags)
955 		cgp->cg_frsum[i - frags]++;
956 	for (i = numfrags(fs, osize); i < frags; i++) {
957 		clrbit(blksfree, bno + i);
958 		cgp->cg_cs.cs_nffree--;
959 		fs->fs_cstotal.cs_nffree--;
960 		fs->fs_cs(fs, cg).cs_nffree--;
961 	}
962 	fs->fs_fmod = 1;
963 	if (DOINGSOFTDEP(ITOV(ip)))
964 		softdep_setup_blkmapdep(bp, fs, bprev);
965 	bdwrite(bp);
966 	return (bprev);
967 }
968 
969 /*
970  * Determine whether a block can be allocated.
971  *
972  * Check to see if a block of the appropriate size is available,
973  * and if it is, allocate it.
974  */
975 static ufs_daddr_t
976 ffs_alloccg(ip, cg, bpref, size)
977 	struct inode *ip;
978 	int cg;
979 	ufs_daddr_t bpref;
980 	int size;
981 {
982 	register struct fs *fs;
983 	register struct cg *cgp;
984 	struct buf *bp;
985 	register int i;
986 	ufs_daddr_t bno, blkno;
987 	int allocsiz, error, frags;
988 	u_int8_t *blksfree;
989 
990 	fs = ip->i_fs;
991 	if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize)
992 		return (0);
993 	error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
994 		(int)fs->fs_cgsize, NOCRED, &bp);
995 	if (error) {
996 		brelse(bp);
997 		return (0);
998 	}
999 	cgp = (struct cg *)bp->b_data;
1000 	if (!cg_chkmagic(cgp) ||
1001 	    (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) {
1002 		brelse(bp);
1003 		return (0);
1004 	}
1005 	bp->b_xflags |= BX_BKGRDWRITE;
1006 	cgp->cg_time = time_second;
1007 	if (size == fs->fs_bsize) {
1008 		bno = ffs_alloccgblk(ip, bp, bpref);
1009 		bdwrite(bp);
1010 		return (bno);
1011 	}
1012 	/*
1013 	 * check to see if any fragments are already available
1014 	 * allocsiz is the size which will be allocated, hacking
1015 	 * it down to a smaller size if necessary
1016 	 */
1017 	blksfree = cg_blksfree(cgp);
1018 	frags = numfrags(fs, size);
1019 	for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++)
1020 		if (cgp->cg_frsum[allocsiz] != 0)
1021 			break;
1022 	if (allocsiz == fs->fs_frag) {
1023 		/*
1024 		 * no fragments were available, so a block will be
1025 		 * allocated, and hacked up
1026 		 */
1027 		if (cgp->cg_cs.cs_nbfree == 0) {
1028 			brelse(bp);
1029 			return (0);
1030 		}
1031 		bno = ffs_alloccgblk(ip, bp, bpref);
1032 		bpref = dtogd(fs, bno);
1033 		for (i = frags; i < fs->fs_frag; i++)
1034 			setbit(blksfree, bpref + i);
1035 		i = fs->fs_frag - frags;
1036 		cgp->cg_cs.cs_nffree += i;
1037 		fs->fs_cstotal.cs_nffree += i;
1038 		fs->fs_cs(fs, cg).cs_nffree += i;
1039 		fs->fs_fmod = 1;
1040 		cgp->cg_frsum[i]++;
1041 		bdwrite(bp);
1042 		return (bno);
1043 	}
1044 	bno = ffs_mapsearch(fs, cgp, bpref, allocsiz);
1045 	if (bno < 0) {
1046 		brelse(bp);
1047 		return (0);
1048 	}
1049 	for (i = 0; i < frags; i++)
1050 		clrbit(blksfree, bno + i);
1051 	cgp->cg_cs.cs_nffree -= frags;
1052 	fs->fs_cstotal.cs_nffree -= frags;
1053 	fs->fs_cs(fs, cg).cs_nffree -= frags;
1054 	fs->fs_fmod = 1;
1055 	cgp->cg_frsum[allocsiz]--;
1056 	if (frags != allocsiz)
1057 		cgp->cg_frsum[allocsiz - frags]++;
1058 	blkno = cg * fs->fs_fpg + bno;
1059 	if (DOINGSOFTDEP(ITOV(ip)))
1060 		softdep_setup_blkmapdep(bp, fs, blkno);
1061 	bdwrite(bp);
1062 	return ((u_long)blkno);
1063 }
1064 
1065 /*
1066  * Allocate a block in a cylinder group.
1067  *
1068  * This algorithm implements the following policy:
1069  *   1) allocate the requested block.
1070  *   2) allocate a rotationally optimal block in the same cylinder.
1071  *   3) allocate the next available block on the block rotor for the
1072  *      specified cylinder group.
1073  * Note that this routine only allocates fs_bsize blocks; these
1074  * blocks may be fragmented by the routine that allocates them.
1075  */
1076 static ufs_daddr_t
1077 ffs_alloccgblk(ip, bp, bpref)
1078 	struct inode *ip;
1079 	struct buf *bp;
1080 	ufs_daddr_t bpref;
1081 {
1082 	struct fs *fs;
1083 	struct cg *cgp;
1084 	ufs_daddr_t bno, blkno;
1085 	int cylno, pos, delta;
1086 	short *cylbp;
1087 	register int i;
1088 	u_int8_t *blksfree;
1089 
1090 	fs = ip->i_fs;
1091 	cgp = (struct cg *)bp->b_data;
1092 	blksfree = cg_blksfree(cgp);
1093 	if (bpref == 0 || dtog(fs, bpref) != cgp->cg_cgx) {
1094 		bpref = cgp->cg_rotor;
1095 		goto norot;
1096 	}
1097 	bpref = blknum(fs, bpref);
1098 	bpref = dtogd(fs, bpref);
1099 	/*
1100 	 * if the requested block is available, use it
1101 	 */
1102 	if (ffs_isblock(fs, blksfree, fragstoblks(fs, bpref))) {
1103 		bno = bpref;
1104 		goto gotit;
1105 	}
1106 	if (fs->fs_nrpos <= 1 || fs->fs_cpc == 0) {
1107 		/*
1108 		 * Block layout information is not available.
1109 		 * Leaving bpref unchanged means we take the
1110 		 * next available free block following the one
1111 		 * we just allocated. Hopefully this will at
1112 		 * least hit a track cache on drives of unknown
1113 		 * geometry (e.g. SCSI).
1114 		 */
1115 		goto norot;
1116 	}
1117 	/*
1118 	 * check for a block available on the same cylinder
1119 	 */
1120 	cylno = cbtocylno(fs, bpref);
1121 	if (cg_blktot(cgp)[cylno] == 0)
1122 		goto norot;
1123 	/*
1124 	 * check the summary information to see if a block is
1125 	 * available in the requested cylinder starting at the
1126 	 * requested rotational position and proceeding around.
1127 	 */
1128 	cylbp = cg_blks(fs, cgp, cylno);
1129 	pos = cbtorpos(fs, bpref);
1130 	for (i = pos; i < fs->fs_nrpos; i++)
1131 		if (cylbp[i] > 0)
1132 			break;
1133 	if (i == fs->fs_nrpos)
1134 		for (i = 0; i < pos; i++)
1135 			if (cylbp[i] > 0)
1136 				break;
1137 	if (cylbp[i] > 0) {
1138 		/*
1139 		 * found a rotational position, now find the actual
1140 		 * block. A panic if none is actually there.
1141 		 */
1142 		pos = cylno % fs->fs_cpc;
1143 		bno = (cylno - pos) * fs->fs_spc / NSPB(fs);
1144 		if (fs_postbl(fs, pos)[i] == -1) {
1145 			printf("pos = %d, i = %d, fs = %s\n",
1146 			    pos, i, fs->fs_fsmnt);
1147 			panic("ffs_alloccgblk: cyl groups corrupted");
1148 		}
1149 		for (i = fs_postbl(fs, pos)[i];; ) {
1150 			if (ffs_isblock(fs, blksfree, bno + i)) {
1151 				bno = blkstofrags(fs, (bno + i));
1152 				goto gotit;
1153 			}
1154 			delta = fs_rotbl(fs)[i];
1155 			if (delta <= 0 ||
1156 			    delta + i > fragstoblks(fs, fs->fs_fpg))
1157 				break;
1158 			i += delta;
1159 		}
1160 		printf("pos = %d, i = %d, fs = %s\n", pos, i, fs->fs_fsmnt);
1161 		panic("ffs_alloccgblk: can't find blk in cyl");
1162 	}
1163 norot:
1164 	/*
1165 	 * no blocks in the requested cylinder, so take next
1166 	 * available one in this cylinder group.
1167 	 */
1168 	bno = ffs_mapsearch(fs, cgp, bpref, (int)fs->fs_frag);
1169 	if (bno < 0)
1170 		return (0);
1171 	cgp->cg_rotor = bno;
1172 gotit:
1173 	blkno = fragstoblks(fs, bno);
1174 	ffs_clrblock(fs, blksfree, (long)blkno);
1175 	ffs_clusteracct(fs, cgp, blkno, -1);
1176 	cgp->cg_cs.cs_nbfree--;
1177 	fs->fs_cstotal.cs_nbfree--;
1178 	fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--;
1179 	cylno = cbtocylno(fs, bno);
1180 	cg_blks(fs, cgp, cylno)[cbtorpos(fs, bno)]--;
1181 	cg_blktot(cgp)[cylno]--;
1182 	fs->fs_fmod = 1;
1183 	blkno = cgp->cg_cgx * fs->fs_fpg + bno;
1184 	if (DOINGSOFTDEP(ITOV(ip)))
1185 		softdep_setup_blkmapdep(bp, fs, blkno);
1186 	return (blkno);
1187 }
1188 
1189 /*
1190  * Determine whether a cluster can be allocated.
1191  *
1192  * We do not currently check for optimal rotational layout if there
1193  * are multiple choices in the same cylinder group. Instead we just
1194  * take the first one that we find following bpref.
1195  */
1196 static ufs_daddr_t
1197 ffs_clusteralloc(ip, cg, bpref, len)
1198 	struct inode *ip;
1199 	int cg;
1200 	ufs_daddr_t bpref;
1201 	int len;
1202 {
1203 	register struct fs *fs;
1204 	register struct cg *cgp;
1205 	struct buf *bp;
1206 	int i, got, run, bno, bit, map;
1207 	u_char *mapp;
1208 	int32_t *lp;
1209 	u_int8_t *blksfree;
1210 
1211 	fs = ip->i_fs;
1212 	if (fs->fs_maxcluster[cg] < len)
1213 		return (0);
1214 	if (bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize,
1215 	    NOCRED, &bp))
1216 		goto fail;
1217 	cgp = (struct cg *)bp->b_data;
1218 	if (!cg_chkmagic(cgp))
1219 		goto fail;
1220 	bp->b_xflags |= BX_BKGRDWRITE;
1221 	/*
1222 	 * Check to see if a cluster of the needed size (or bigger) is
1223 	 * available in this cylinder group.
1224 	 */
1225 	lp = &cg_clustersum(cgp)[len];
1226 	for (i = len; i <= fs->fs_contigsumsize; i++)
1227 		if (*lp++ > 0)
1228 			break;
1229 	if (i > fs->fs_contigsumsize) {
1230 		/*
1231 		 * This is the first time looking for a cluster in this
1232 		 * cylinder group. Update the cluster summary information
1233 		 * to reflect the true maximum sized cluster so that
1234 		 * future cluster allocation requests can avoid reading
1235 		 * the cylinder group map only to find no clusters.
1236 		 */
1237 		lp = &cg_clustersum(cgp)[len - 1];
1238 		for (i = len - 1; i > 0; i--)
1239 			if (*lp-- > 0)
1240 				break;
1241 		fs->fs_maxcluster[cg] = i;
1242 		goto fail;
1243 	}
1244 	/*
1245 	 * Search the cluster map to find a big enough cluster.
1246 	 * We take the first one that we find, even if it is larger
1247 	 * than we need as we prefer to get one close to the previous
1248 	 * block allocation. We do not search before the current
1249 	 * preference point as we do not want to allocate a block
1250 	 * that is allocated before the previous one (as we will
1251 	 * then have to wait for another pass of the elevator
1252 	 * algorithm before it will be read). We prefer to fail and
1253 	 * be recalled to try an allocation in the next cylinder group.
1254 	 */
1255 	if (dtog(fs, bpref) != cg)
1256 		bpref = 0;
1257 	else
1258 		bpref = fragstoblks(fs, dtogd(fs, blknum(fs, bpref)));
1259 	mapp = &cg_clustersfree(cgp)[bpref / NBBY];
1260 	map = *mapp++;
1261 	bit = 1 << (bpref % NBBY);
1262 	for (run = 0, got = bpref; got < cgp->cg_nclusterblks; got++) {
1263 		if ((map & bit) == 0) {
1264 			run = 0;
1265 		} else {
1266 			run++;
1267 			if (run == len)
1268 				break;
1269 		}
1270 		if ((got & (NBBY - 1)) != (NBBY - 1)) {
1271 			bit <<= 1;
1272 		} else {
1273 			map = *mapp++;
1274 			bit = 1;
1275 		}
1276 	}
1277 	if (got >= cgp->cg_nclusterblks)
1278 		goto fail;
1279 	/*
1280 	 * Allocate the cluster that we have found.
1281 	 */
1282 	blksfree = cg_blksfree(cgp);
1283 	for (i = 1; i <= len; i++)
1284 		if (!ffs_isblock(fs, blksfree, got - run + i))
1285 			panic("ffs_clusteralloc: map mismatch");
1286 	bno = cg * fs->fs_fpg + blkstofrags(fs, got - run + 1);
1287 	if (dtog(fs, bno) != cg)
1288 		panic("ffs_clusteralloc: allocated out of group");
1289 	len = blkstofrags(fs, len);
1290 	for (i = 0; i < len; i += fs->fs_frag)
1291 		if ((got = ffs_alloccgblk(ip, bp, bno + i)) != bno + i)
1292 			panic("ffs_clusteralloc: lost block");
1293 	bdwrite(bp);
1294 	return (bno);
1295 
1296 fail:
1297 	brelse(bp);
1298 	return (0);
1299 }
1300 
1301 /*
1302  * Determine whether an inode can be allocated.
1303  *
1304  * Check to see if an inode is available, and if it is,
1305  * allocate it using the following policy:
1306  *   1) allocate the requested inode.
1307  *   2) allocate the next available inode after the requested
1308  *      inode in the specified cylinder group.
1309  */
1310 static ino_t
1311 ffs_nodealloccg(ip, cg, ipref, mode)
1312 	struct inode *ip;
1313 	int cg;
1314 	ufs_daddr_t ipref;
1315 	int mode;
1316 {
1317 	register struct fs *fs;
1318 	register struct cg *cgp;
1319 	struct buf *bp;
1320 	u_int8_t *inosused;
1321 	int error, start, len, loc, map, i;
1322 
1323 	fs = ip->i_fs;
1324 	if (fs->fs_cs(fs, cg).cs_nifree == 0)
1325 		return (0);
1326 	error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
1327 		(int)fs->fs_cgsize, NOCRED, &bp);
1328 	if (error) {
1329 		brelse(bp);
1330 		return (0);
1331 	}
1332 	cgp = (struct cg *)bp->b_data;
1333 	if (!cg_chkmagic(cgp) || cgp->cg_cs.cs_nifree == 0) {
1334 		brelse(bp);
1335 		return (0);
1336 	}
1337 	bp->b_xflags |= BX_BKGRDWRITE;
1338 	cgp->cg_time = time_second;
1339 	inosused = cg_inosused(cgp);
1340 	if (ipref) {
1341 		ipref %= fs->fs_ipg;
1342 		if (isclr(inosused, ipref))
1343 			goto gotit;
1344 	}
1345 	start = cgp->cg_irotor / NBBY;
1346 	len = howmany(fs->fs_ipg - cgp->cg_irotor, NBBY);
1347 	loc = skpc(0xff, len, &inosused[start]);
1348 	if (loc == 0) {
1349 		len = start + 1;
1350 		start = 0;
1351 		loc = skpc(0xff, len, &inosused[0]);
1352 		if (loc == 0) {
1353 			printf("cg = %d, irotor = %ld, fs = %s\n",
1354 			    cg, (long)cgp->cg_irotor, fs->fs_fsmnt);
1355 			panic("ffs_nodealloccg: map corrupted");
1356 			/* NOTREACHED */
1357 		}
1358 	}
1359 	i = start + len - loc;
1360 	map = inosused[i];
1361 	ipref = i * NBBY;
1362 	for (i = 1; i < (1 << NBBY); i <<= 1, ipref++) {
1363 		if ((map & i) == 0) {
1364 			cgp->cg_irotor = ipref;
1365 			goto gotit;
1366 		}
1367 	}
1368 	printf("fs = %s\n", fs->fs_fsmnt);
1369 	panic("ffs_nodealloccg: block not in map");
1370 	/* NOTREACHED */
1371 gotit:
1372 	if (DOINGSOFTDEP(ITOV(ip)))
1373 		softdep_setup_inomapdep(bp, ip, cg * fs->fs_ipg + ipref);
1374 	setbit(inosused, ipref);
1375 	cgp->cg_cs.cs_nifree--;
1376 	fs->fs_cstotal.cs_nifree--;
1377 	fs->fs_cs(fs, cg).cs_nifree--;
1378 	fs->fs_fmod = 1;
1379 	if ((mode & IFMT) == IFDIR) {
1380 		cgp->cg_cs.cs_ndir++;
1381 		fs->fs_cstotal.cs_ndir++;
1382 		fs->fs_cs(fs, cg).cs_ndir++;
1383 	}
1384 	bdwrite(bp);
1385 	return (cg * fs->fs_ipg + ipref);
1386 }
1387 
1388 /*
1389  * Free a block or fragment.
1390  *
1391  * The specified block or fragment is placed back in the
1392  * free map. If a fragment is deallocated, a possible
1393  * block reassembly is checked.
1394  */
1395 void
1396 ffs_blkfree(ip, bno, size)
1397 	register struct inode *ip;
1398 	ufs_daddr_t bno;
1399 	long size;
1400 {
1401 	register struct fs *fs;
1402 	register struct cg *cgp;
1403 	struct buf *bp;
1404 	ufs_daddr_t blkno;
1405 	int i, error, cg, blk, frags, bbase;
1406 	u_int8_t *blksfree;
1407 
1408 	fs = ip->i_fs;
1409 	VOP_FREEBLKS(ip->i_devvp, fsbtodb(fs, bno), size);
1410 	if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0 ||
1411 	    fragnum(fs, bno) + numfrags(fs, size) > fs->fs_frag) {
1412 		printf("dev=%s, bno = %ld, bsize = %ld, size = %ld, fs = %s\n",
1413 		    devtoname(ip->i_dev), (long)bno, (long)fs->fs_bsize, size,
1414 		    fs->fs_fsmnt);
1415 		panic("ffs_blkfree: bad size");
1416 	}
1417 	cg = dtog(fs, bno);
1418 	if ((u_int)bno >= fs->fs_size) {
1419 		printf("bad block %ld, ino %lu\n",
1420 		    (long)bno, (u_long)ip->i_number);
1421 		ffs_fserr(fs, ip->i_uid, "bad block");
1422 		return;
1423 	}
1424 	error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
1425 		(int)fs->fs_cgsize, NOCRED, &bp);
1426 	if (error) {
1427 		brelse(bp);
1428 		return;
1429 	}
1430 	cgp = (struct cg *)bp->b_data;
1431 	if (!cg_chkmagic(cgp)) {
1432 		brelse(bp);
1433 		return;
1434 	}
1435 	bp->b_xflags |= BX_BKGRDWRITE;
1436 	cgp->cg_time = time_second;
1437 	bno = dtogd(fs, bno);
1438 	blksfree = cg_blksfree(cgp);
1439 	if (size == fs->fs_bsize) {
1440 		blkno = fragstoblks(fs, bno);
1441 		if (!ffs_isfreeblock(fs, blksfree, blkno)) {
1442 			printf("dev = %s, block = %ld, fs = %s\n",
1443 			    devtoname(ip->i_dev), (long)bno, fs->fs_fsmnt);
1444 			panic("ffs_blkfree: freeing free block");
1445 		}
1446 		ffs_setblock(fs, blksfree, blkno);
1447 		ffs_clusteracct(fs, cgp, blkno, 1);
1448 		cgp->cg_cs.cs_nbfree++;
1449 		fs->fs_cstotal.cs_nbfree++;
1450 		fs->fs_cs(fs, cg).cs_nbfree++;
1451 		i = cbtocylno(fs, bno);
1452 		cg_blks(fs, cgp, i)[cbtorpos(fs, bno)]++;
1453 		cg_blktot(cgp)[i]++;
1454 	} else {
1455 		bbase = bno - fragnum(fs, bno);
1456 		/*
1457 		 * decrement the counts associated with the old frags
1458 		 */
1459 		blk = blkmap(fs, blksfree, bbase);
1460 		ffs_fragacct(fs, blk, cgp->cg_frsum, -1);
1461 		/*
1462 		 * deallocate the fragment
1463 		 */
1464 		frags = numfrags(fs, size);
1465 		for (i = 0; i < frags; i++) {
1466 			if (isset(blksfree, bno + i)) {
1467 				printf("dev = %s, block = %ld, fs = %s\n",
1468 				    devtoname(ip->i_dev), (long)(bno + i),
1469 				    fs->fs_fsmnt);
1470 				panic("ffs_blkfree: freeing free frag");
1471 			}
1472 			setbit(blksfree, bno + i);
1473 		}
1474 		cgp->cg_cs.cs_nffree += i;
1475 		fs->fs_cstotal.cs_nffree += i;
1476 		fs->fs_cs(fs, cg).cs_nffree += i;
1477 		/*
1478 		 * add back in counts associated with the new frags
1479 		 */
1480 		blk = blkmap(fs, blksfree, bbase);
1481 		ffs_fragacct(fs, blk, cgp->cg_frsum, 1);
1482 		/*
1483 		 * if a complete block has been reassembled, account for it
1484 		 */
1485 		blkno = fragstoblks(fs, bbase);
1486 		if (ffs_isblock(fs, blksfree, blkno)) {
1487 			cgp->cg_cs.cs_nffree -= fs->fs_frag;
1488 			fs->fs_cstotal.cs_nffree -= fs->fs_frag;
1489 			fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag;
1490 			ffs_clusteracct(fs, cgp, blkno, 1);
1491 			cgp->cg_cs.cs_nbfree++;
1492 			fs->fs_cstotal.cs_nbfree++;
1493 			fs->fs_cs(fs, cg).cs_nbfree++;
1494 			i = cbtocylno(fs, bbase);
1495 			cg_blks(fs, cgp, i)[cbtorpos(fs, bbase)]++;
1496 			cg_blktot(cgp)[i]++;
1497 		}
1498 	}
1499 	fs->fs_fmod = 1;
1500 	bdwrite(bp);
1501 }
1502 
1503 #ifdef DIAGNOSTIC
1504 /*
1505  * Verify allocation of a block or fragment. Returns true if block or
1506  * fragment is allocated, false if it is free.
1507  */
1508 static int
1509 ffs_checkblk(ip, bno, size)
1510 	struct inode *ip;
1511 	ufs_daddr_t bno;
1512 	long size;
1513 {
1514 	struct fs *fs;
1515 	struct cg *cgp;
1516 	struct buf *bp;
1517 	int i, error, frags, free;
1518 	u_int8_t *blksfree;
1519 
1520 	fs = ip->i_fs;
1521 	if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0) {
1522 		printf("bsize = %ld, size = %ld, fs = %s\n",
1523 		    (long)fs->fs_bsize, size, fs->fs_fsmnt);
1524 		panic("ffs_checkblk: bad size");
1525 	}
1526 	if ((u_int)bno >= fs->fs_size)
1527 		panic("ffs_checkblk: bad block %d", bno);
1528 	error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, dtog(fs, bno))),
1529 		(int)fs->fs_cgsize, NOCRED, &bp);
1530 	if (error)
1531 		panic("ffs_checkblk: cg bread failed");
1532 	cgp = (struct cg *)bp->b_data;
1533 	if (!cg_chkmagic(cgp))
1534 		panic("ffs_checkblk: cg magic mismatch");
1535 	bp->b_xflags |= BX_BKGRDWRITE;
1536 	blksfree = cg_blksfree(cgp);
1537 	bno = dtogd(fs, bno);
1538 	if (size == fs->fs_bsize) {
1539 		free = ffs_isblock(fs, blksfree, fragstoblks(fs, bno));
1540 	} else {
1541 		frags = numfrags(fs, size);
1542 		for (free = 0, i = 0; i < frags; i++)
1543 			if (isset(blksfree, bno + i))
1544 				free++;
1545 		if (free != 0 && free != frags)
1546 			panic("ffs_checkblk: partially free fragment");
1547 	}
1548 	brelse(bp);
1549 	return (!free);
1550 }
1551 #endif /* DIAGNOSTIC */
1552 
1553 /*
1554  * Free an inode.
1555  */
1556 int
1557 ffs_vfree( pvp, ino, mode)
1558 	struct vnode *pvp;
1559 	ino_t ino;
1560 	int mode;
1561 {
1562 	if (DOINGSOFTDEP(pvp)) {
1563 		softdep_freefile(pvp, ino, mode);
1564 		return (0);
1565 	}
1566 	return (ffs_freefile(pvp, ino, mode));
1567 }
1568 
1569 /*
1570  * Do the actual free operation.
1571  * The specified inode is placed back in the free map.
1572  */
1573  int
1574  ffs_freefile( pvp, ino, mode)
1575 	struct vnode *pvp;
1576 	ino_t ino;
1577 	int mode;
1578 {
1579 	register struct fs *fs;
1580 	register struct cg *cgp;
1581 	register struct inode *pip;
1582 	struct buf *bp;
1583 	int error, cg;
1584 	u_int8_t *inosused;
1585 
1586 	pip = VTOI(pvp);
1587 	fs = pip->i_fs;
1588 	if ((u_int)ino >= fs->fs_ipg * fs->fs_ncg)
1589 		panic("ffs_vfree: range: dev = (%d,%d), ino = %d, fs = %s",
1590 		    major(pip->i_dev), minor(pip->i_dev), ino, fs->fs_fsmnt);
1591 	cg = ino_to_cg(fs, ino);
1592 	error = bread(pip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
1593 		(int)fs->fs_cgsize, NOCRED, &bp);
1594 	if (error) {
1595 		brelse(bp);
1596 		return (error);
1597 	}
1598 	cgp = (struct cg *)bp->b_data;
1599 	if (!cg_chkmagic(cgp)) {
1600 		brelse(bp);
1601 		return (0);
1602 	}
1603 	bp->b_xflags |= BX_BKGRDWRITE;
1604 	cgp->cg_time = time_second;
1605 	inosused = cg_inosused(cgp);
1606 	ino %= fs->fs_ipg;
1607 	if (isclr(inosused, ino)) {
1608 		printf("dev = %s, ino = %lu, fs = %s\n",
1609 		    devtoname(pip->i_dev), (u_long)ino, fs->fs_fsmnt);
1610 		if (fs->fs_ronly == 0)
1611 			panic("ffs_vfree: freeing free inode");
1612 	}
1613 	clrbit(inosused, ino);
1614 	if (ino < cgp->cg_irotor)
1615 		cgp->cg_irotor = ino;
1616 	cgp->cg_cs.cs_nifree++;
1617 	fs->fs_cstotal.cs_nifree++;
1618 	fs->fs_cs(fs, cg).cs_nifree++;
1619 	if ((mode & IFMT) == IFDIR) {
1620 		cgp->cg_cs.cs_ndir--;
1621 		fs->fs_cstotal.cs_ndir--;
1622 		fs->fs_cs(fs, cg).cs_ndir--;
1623 	}
1624 	fs->fs_fmod = 1;
1625 	bdwrite(bp);
1626 	return (0);
1627 }
1628 
1629 /*
1630  * Find a block of the specified size in the specified cylinder group.
1631  *
1632  * It is a panic if a request is made to find a block if none are
1633  * available.
1634  */
1635 static ufs_daddr_t
1636 ffs_mapsearch(fs, cgp, bpref, allocsiz)
1637 	register struct fs *fs;
1638 	register struct cg *cgp;
1639 	ufs_daddr_t bpref;
1640 	int allocsiz;
1641 {
1642 	ufs_daddr_t bno;
1643 	int start, len, loc, i;
1644 	int blk, field, subfield, pos;
1645 	u_int8_t *blksfree;
1646 
1647 	/*
1648 	 * find the fragment by searching through the free block
1649 	 * map for an appropriate bit pattern
1650 	 */
1651 	if (bpref)
1652 		start = dtogd(fs, bpref) / NBBY;
1653 	else
1654 		start = cgp->cg_frotor / NBBY;
1655 	blksfree = cg_blksfree(cgp);
1656 	len = howmany(fs->fs_fpg, NBBY) - start;
1657 	loc = scanc((u_int)len, (u_char *)&blksfree[start],
1658 		(u_char *)fragtbl[fs->fs_frag],
1659 		(u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
1660 	if (loc == 0) {
1661 		len = start + 1;
1662 		start = 0;
1663 		loc = scanc((u_int)len, (u_char *)&blksfree[0],
1664 			(u_char *)fragtbl[fs->fs_frag],
1665 			(u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
1666 		if (loc == 0) {
1667 			printf("start = %d, len = %d, fs = %s\n",
1668 			    start, len, fs->fs_fsmnt);
1669 			panic("ffs_alloccg: map corrupted");
1670 			/* NOTREACHED */
1671 		}
1672 	}
1673 	bno = (start + len - loc) * NBBY;
1674 	cgp->cg_frotor = bno;
1675 	/*
1676 	 * found the byte in the map
1677 	 * sift through the bits to find the selected frag
1678 	 */
1679 	for (i = bno + NBBY; bno < i; bno += fs->fs_frag) {
1680 		blk = blkmap(fs, blksfree, bno);
1681 		blk <<= 1;
1682 		field = around[allocsiz];
1683 		subfield = inside[allocsiz];
1684 		for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) {
1685 			if ((blk & field) == subfield)
1686 				return (bno + pos);
1687 			field <<= 1;
1688 			subfield <<= 1;
1689 		}
1690 	}
1691 	printf("bno = %lu, fs = %s\n", (u_long)bno, fs->fs_fsmnt);
1692 	panic("ffs_alloccg: block not in map");
1693 	return (-1);
1694 }
1695 
1696 /*
1697  * Update the cluster map because of an allocation or free.
1698  *
1699  * Cnt == 1 means free; cnt == -1 means allocating.
1700  */
1701 static void
1702 ffs_clusteracct(fs, cgp, blkno, cnt)
1703 	struct fs *fs;
1704 	struct cg *cgp;
1705 	ufs_daddr_t blkno;
1706 	int cnt;
1707 {
1708 	int32_t *sump;
1709 	int32_t *lp;
1710 	u_char *freemapp, *mapp;
1711 	int i, start, end, forw, back, map, bit;
1712 
1713 	if (fs->fs_contigsumsize <= 0)
1714 		return;
1715 	freemapp = cg_clustersfree(cgp);
1716 	sump = cg_clustersum(cgp);
1717 	/*
1718 	 * Allocate or clear the actual block.
1719 	 */
1720 	if (cnt > 0)
1721 		setbit(freemapp, blkno);
1722 	else
1723 		clrbit(freemapp, blkno);
1724 	/*
1725 	 * Find the size of the cluster going forward.
1726 	 */
1727 	start = blkno + 1;
1728 	end = start + fs->fs_contigsumsize;
1729 	if (end >= cgp->cg_nclusterblks)
1730 		end = cgp->cg_nclusterblks;
1731 	mapp = &freemapp[start / NBBY];
1732 	map = *mapp++;
1733 	bit = 1 << (start % NBBY);
1734 	for (i = start; i < end; i++) {
1735 		if ((map & bit) == 0)
1736 			break;
1737 		if ((i & (NBBY - 1)) != (NBBY - 1)) {
1738 			bit <<= 1;
1739 		} else {
1740 			map = *mapp++;
1741 			bit = 1;
1742 		}
1743 	}
1744 	forw = i - start;
1745 	/*
1746 	 * Find the size of the cluster going backward.
1747 	 */
1748 	start = blkno - 1;
1749 	end = start - fs->fs_contigsumsize;
1750 	if (end < 0)
1751 		end = -1;
1752 	mapp = &freemapp[start / NBBY];
1753 	map = *mapp--;
1754 	bit = 1 << (start % NBBY);
1755 	for (i = start; i > end; i--) {
1756 		if ((map & bit) == 0)
1757 			break;
1758 		if ((i & (NBBY - 1)) != 0) {
1759 			bit >>= 1;
1760 		} else {
1761 			map = *mapp--;
1762 			bit = 1 << (NBBY - 1);
1763 		}
1764 	}
1765 	back = start - i;
1766 	/*
1767 	 * Account for old cluster and the possibly new forward and
1768 	 * back clusters.
1769 	 */
1770 	i = back + forw + 1;
1771 	if (i > fs->fs_contigsumsize)
1772 		i = fs->fs_contigsumsize;
1773 	sump[i] += cnt;
1774 	if (back > 0)
1775 		sump[back] -= cnt;
1776 	if (forw > 0)
1777 		sump[forw] -= cnt;
1778 	/*
1779 	 * Update cluster summary information.
1780 	 */
1781 	lp = &sump[fs->fs_contigsumsize];
1782 	for (i = fs->fs_contigsumsize; i > 0; i--)
1783 		if (*lp-- > 0)
1784 			break;
1785 	fs->fs_maxcluster[cgp->cg_cgx] = i;
1786 }
1787 
1788 /*
1789  * Fserr prints the name of a file system with an error diagnostic.
1790  *
1791  * The form of the error message is:
1792  *	fs: error message
1793  */
1794 static void
1795 ffs_fserr(fs, uid, cp)
1796 	struct fs *fs;
1797 	u_int uid;
1798 	char *cp;
1799 {
1800 	struct proc *p = curproc;	/* XXX */
1801 
1802 	log(LOG_ERR, "pid %d (%s), uid %d on %s: %s\n", p ? p->p_pid : -1,
1803 			p ? p->p_comm : "-", uid, fs->fs_fsmnt, cp);
1804 }
1805