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