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