xref: /freebsd/sys/ufs/ffs/ffs_alloc.c (revision 4b2eaea43fec8e8792be611dea204071a10b655a)
1 /*
2  * Copyright (c) 2002 Networks Associates Technology, Inc.
3  * All rights reserved.
4  *
5  * This software was developed for the FreeBSD Project by Marshall
6  * Kirk McKusick and Network Associates Laboratories, the Security
7  * Research Division of Network Associates, Inc. under DARPA/SPAWAR
8  * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS
9  * research program
10  *
11  * Copyright (c) 1982, 1989, 1993
12  *	The Regents of the University of California.  All rights reserved.
13  * (c) UNIX System Laboratories, Inc.
14  * Copyright (c) 1982, 1986, 1989, 1993
15  *	The Regents of the University of California.  All rights reserved.
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions
19  * are met:
20  * 1. Redistributions of source code must retain the above copyright
21  *    notice, this list of conditions and the following disclaimer.
22  * 2. Redistributions in binary form must reproduce the above copyright
23  *    notice, this list of conditions and the following disclaimer in the
24  *    documentation and/or other materials provided with the distribution.
25  * 3. All advertising materials mentioning features or use of this software
26  *    must display the following acknowledgement:
27  *	This product includes software developed by the University of
28  *	California, Berkeley and its contributors.
29  * 4. Neither the name of the University nor the names of its contributors
30  *    may be used to endorse or promote products derived from this software
31  *    without specific prior written permission.
32  *
33  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
34  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
35  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
37  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
38  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
39  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
41  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
42  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43  * SUCH DAMAGE.
44  *
45  *	@(#)ffs_alloc.c	8.18 (Berkeley) 5/26/95
46  * $FreeBSD$
47  */
48 
49 #include "opt_quota.h"
50 
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/bio.h>
54 #include <sys/buf.h>
55 #include <sys/conf.h>
56 #include <sys/file.h>
57 #include <sys/filedesc.h>
58 #include <sys/proc.h>
59 #include <sys/vnode.h>
60 #include <sys/mount.h>
61 #include <sys/kernel.h>
62 #include <sys/stdint.h>
63 #include <sys/sysctl.h>
64 #include <sys/syslog.h>
65 
66 #include <ufs/ufs/extattr.h>
67 #include <ufs/ufs/quota.h>
68 #include <ufs/ufs/inode.h>
69 #include <ufs/ufs/ufs_extern.h>
70 #include <ufs/ufs/ufsmount.h>
71 
72 #include <ufs/ffs/fs.h>
73 #include <ufs/ffs/ffs_extern.h>
74 
75 typedef ufs2_daddr_t allocfcn_t(struct inode *ip, int cg, ufs2_daddr_t bpref,
76 				  int size);
77 
78 static ufs2_daddr_t ffs_alloccg(struct inode *, int, ufs2_daddr_t, int);
79 static ufs2_daddr_t
80 	      ffs_alloccgblk(struct inode *, struct buf *, ufs2_daddr_t);
81 #ifdef DIAGNOSTIC
82 static int	ffs_checkblk(struct inode *, ufs2_daddr_t, long);
83 #endif
84 static ufs2_daddr_t ffs_clusteralloc(struct inode *, int, ufs2_daddr_t, int);
85 static ino_t	ffs_dirpref(struct inode *);
86 static ufs2_daddr_t ffs_fragextend(struct inode *, int, ufs2_daddr_t, int, int);
87 static void	ffs_fserr(struct fs *, ino_t, char *);
88 static ufs2_daddr_t	ffs_hashalloc
89 		(struct inode *, int, ufs2_daddr_t, int, allocfcn_t *);
90 static ufs2_daddr_t ffs_nodealloccg(struct inode *, int, ufs2_daddr_t, int);
91 static ufs1_daddr_t ffs_mapsearch(struct fs *, struct cg *, ufs2_daddr_t, int);
92 static int	ffs_reallocblks_ufs1(struct vop_reallocblks_args *);
93 static int	ffs_reallocblks_ufs2(struct vop_reallocblks_args *);
94 
95 /*
96  * Allocate a block in the filesystem.
97  *
98  * The size of the requested block is given, which must be some
99  * multiple of fs_fsize and <= fs_bsize.
100  * A preference may be optionally specified. If a preference is given
101  * the following hierarchy is used to allocate a block:
102  *   1) allocate the requested block.
103  *   2) allocate a rotationally optimal block in the same cylinder.
104  *   3) allocate a block in the same cylinder group.
105  *   4) quadradically rehash into other cylinder groups, until an
106  *      available block is located.
107  * If no block preference is given the following heirarchy is used
108  * to allocate a block:
109  *   1) allocate a block in the cylinder group that contains the
110  *      inode for the file.
111  *   2) quadradically rehash into other cylinder groups, until an
112  *      available block is located.
113  */
114 int
115 ffs_alloc(ip, lbn, bpref, size, cred, bnp)
116 	struct inode *ip;
117 	ufs2_daddr_t lbn, bpref;
118 	int size;
119 	struct ucred *cred;
120 	ufs2_daddr_t *bnp;
121 {
122 	struct fs *fs;
123 	ufs2_daddr_t bno;
124 	int cg, reclaimed;
125 #ifdef QUOTA
126 	int error;
127 #endif
128 
129 	*bnp = 0;
130 	fs = ip->i_fs;
131 #ifdef DIAGNOSTIC
132 	if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0) {
133 		printf("dev = %s, bsize = %ld, size = %d, fs = %s\n",
134 		    devtoname(ip->i_dev), (long)fs->fs_bsize, size,
135 		    fs->fs_fsmnt);
136 		panic("ffs_alloc: bad size");
137 	}
138 	if (cred == NOCRED)
139 		panic("ffs_alloc: missing credential");
140 #endif /* DIAGNOSTIC */
141 	reclaimed = 0;
142 retry:
143 	if (size == fs->fs_bsize && fs->fs_cstotal.cs_nbfree == 0)
144 		goto nospace;
145 	if (suser_cred(cred, PRISON_ROOT) &&
146 	    freespace(fs, fs->fs_minfree) - numfrags(fs, size) < 0)
147 		goto nospace;
148 #ifdef QUOTA
149 	error = chkdq(ip, btodb(size), cred, 0);
150 	if (error)
151 		return (error);
152 #endif
153 	if (bpref >= fs->fs_size)
154 		bpref = 0;
155 	if (bpref == 0)
156 		cg = ino_to_cg(fs, ip->i_number);
157 	else
158 		cg = dtog(fs, bpref);
159 	bno = ffs_hashalloc(ip, cg, bpref, size, ffs_alloccg);
160 	if (bno > 0) {
161 		DIP(ip, i_blocks) += btodb(size);
162 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
163 		*bnp = bno;
164 		return (0);
165 	}
166 #ifdef QUOTA
167 	/*
168 	 * Restore user's disk quota because allocation failed.
169 	 */
170 	(void) chkdq(ip, -btodb(size), cred, FORCE);
171 #endif
172 nospace:
173 	if (fs->fs_pendingblocks > 0 && reclaimed == 0) {
174 		reclaimed = 1;
175 		softdep_request_cleanup(fs, ITOV(ip));
176 		goto retry;
177 	}
178 	ffs_fserr(fs, ip->i_number, "filesystem full");
179 	uprintf("\n%s: write failed, filesystem is full\n", fs->fs_fsmnt);
180 	return (ENOSPC);
181 }
182 
183 /*
184  * Reallocate a fragment to a bigger size
185  *
186  * The number and size of the old block is given, and a preference
187  * and new size is also specified. The allocator attempts to extend
188  * the original block. Failing that, the regular block allocator is
189  * invoked to get an appropriate block.
190  */
191 int
192 ffs_realloccg(ip, lbprev, bprev, bpref, osize, nsize, cred, bpp)
193 	struct inode *ip;
194 	ufs2_daddr_t lbprev;
195 	ufs2_daddr_t bprev;
196 	ufs2_daddr_t bpref;
197 	int osize, nsize;
198 	struct ucred *cred;
199 	struct buf **bpp;
200 {
201 	struct vnode *vp;
202 	struct fs *fs;
203 	struct buf *bp;
204 	int cg, request, error, reclaimed;
205 	ufs2_daddr_t bno;
206 
207 	*bpp = 0;
208 	vp = ITOV(ip);
209 	fs = ip->i_fs;
210 #ifdef DIAGNOSTIC
211 	if (vp->v_mount->mnt_kern_flag & MNTK_SUSPENDED)
212 		panic("ffs_realloccg: allocation on suspended filesystem");
213 	if ((u_int)osize > fs->fs_bsize || fragoff(fs, osize) != 0 ||
214 	    (u_int)nsize > fs->fs_bsize || fragoff(fs, nsize) != 0) {
215 		printf(
216 		"dev = %s, bsize = %ld, osize = %d, nsize = %d, fs = %s\n",
217 		    devtoname(ip->i_dev), (long)fs->fs_bsize, osize,
218 		    nsize, fs->fs_fsmnt);
219 		panic("ffs_realloccg: bad size");
220 	}
221 	if (cred == NOCRED)
222 		panic("ffs_realloccg: missing credential");
223 #endif /* DIAGNOSTIC */
224 	reclaimed = 0;
225 retry:
226 	if (suser_cred(cred, PRISON_ROOT) &&
227 	    freespace(fs, fs->fs_minfree) -  numfrags(fs, nsize - osize) < 0)
228 		goto nospace;
229 	if (bprev == 0) {
230 		printf("dev = %s, bsize = %ld, bprev = %jd, fs = %s\n",
231 		    devtoname(ip->i_dev), (long)fs->fs_bsize, (intmax_t)bprev,
232 		    fs->fs_fsmnt);
233 		panic("ffs_realloccg: bad bprev");
234 	}
235 	/*
236 	 * Allocate the extra space in the buffer.
237 	 */
238 	error = bread(vp, lbprev, osize, NOCRED, &bp);
239 	if (error) {
240 		brelse(bp);
241 		return (error);
242 	}
243 
244 	if (bp->b_blkno == bp->b_lblkno) {
245 		if (lbprev >= NDADDR)
246 			panic("ffs_realloccg: lbprev out of range");
247 		bp->b_blkno = fsbtodb(fs, bprev);
248 	}
249 
250 #ifdef QUOTA
251 	error = chkdq(ip, btodb(nsize - osize), cred, 0);
252 	if (error) {
253 		brelse(bp);
254 		return (error);
255 	}
256 #endif
257 	/*
258 	 * Check for extension in the existing location.
259 	 */
260 	cg = dtog(fs, bprev);
261 	bno = ffs_fragextend(ip, cg, bprev, osize, nsize);
262 	if (bno) {
263 		if (bp->b_blkno != fsbtodb(fs, bno))
264 			panic("ffs_realloccg: bad blockno");
265 		DIP(ip, i_blocks) += btodb(nsize - osize);
266 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
267 		allocbuf(bp, nsize);
268 		bp->b_flags |= B_DONE;
269 		bzero((char *)bp->b_data + osize, (u_int)nsize - osize);
270 		*bpp = bp;
271 		return (0);
272 	}
273 	/*
274 	 * Allocate a new disk location.
275 	 */
276 	if (bpref >= fs->fs_size)
277 		bpref = 0;
278 	switch ((int)fs->fs_optim) {
279 	case FS_OPTSPACE:
280 		/*
281 		 * Allocate an exact sized fragment. Although this makes
282 		 * best use of space, we will waste time relocating it if
283 		 * the file continues to grow. If the fragmentation is
284 		 * less than half of the minimum free reserve, we choose
285 		 * to begin optimizing for time.
286 		 */
287 		request = nsize;
288 		if (fs->fs_minfree <= 5 ||
289 		    fs->fs_cstotal.cs_nffree >
290 		    (off_t)fs->fs_dsize * fs->fs_minfree / (2 * 100))
291 			break;
292 		log(LOG_NOTICE, "%s: optimization changed from SPACE to TIME\n",
293 			fs->fs_fsmnt);
294 		fs->fs_optim = FS_OPTTIME;
295 		break;
296 	case FS_OPTTIME:
297 		/*
298 		 * At this point we have discovered a file that is trying to
299 		 * grow a small fragment to a larger fragment. To save time,
300 		 * we allocate a full sized block, then free the unused portion.
301 		 * If the file continues to grow, the `ffs_fragextend' call
302 		 * above will be able to grow it in place without further
303 		 * copying. If aberrant programs cause disk fragmentation to
304 		 * grow within 2% of the free reserve, we choose to begin
305 		 * optimizing for space.
306 		 */
307 		request = fs->fs_bsize;
308 		if (fs->fs_cstotal.cs_nffree <
309 		    (off_t)fs->fs_dsize * (fs->fs_minfree - 2) / 100)
310 			break;
311 		log(LOG_NOTICE, "%s: optimization changed from TIME to SPACE\n",
312 			fs->fs_fsmnt);
313 		fs->fs_optim = FS_OPTSPACE;
314 		break;
315 	default:
316 		printf("dev = %s, optim = %ld, fs = %s\n",
317 		    devtoname(ip->i_dev), (long)fs->fs_optim, fs->fs_fsmnt);
318 		panic("ffs_realloccg: bad optim");
319 		/* NOTREACHED */
320 	}
321 	bno = ffs_hashalloc(ip, cg, bpref, request, ffs_alloccg);
322 	if (bno > 0) {
323 		bp->b_blkno = fsbtodb(fs, bno);
324 		if (!DOINGSOFTDEP(vp))
325 			ffs_blkfree(fs, ip->i_devvp, bprev, (long)osize,
326 			    ip->i_number);
327 		if (nsize < request)
328 			ffs_blkfree(fs, ip->i_devvp, bno + numfrags(fs, nsize),
329 			    (long)(request - nsize), ip->i_number);
330 		DIP(ip, i_blocks) += btodb(nsize - osize);
331 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
332 		allocbuf(bp, nsize);
333 		bp->b_flags |= B_DONE;
334 		bzero((char *)bp->b_data + osize, (u_int)nsize - osize);
335 		*bpp = bp;
336 		return (0);
337 	}
338 #ifdef QUOTA
339 	/*
340 	 * Restore user's disk quota because allocation failed.
341 	 */
342 	(void) chkdq(ip, -btodb(nsize - osize), cred, FORCE);
343 #endif
344 	brelse(bp);
345 nospace:
346 	/*
347 	 * no space available
348 	 */
349 	if (fs->fs_pendingblocks > 0 && reclaimed == 0) {
350 		reclaimed = 1;
351 		softdep_request_cleanup(fs, vp);
352 		goto retry;
353 	}
354 	ffs_fserr(fs, ip->i_number, "filesystem full");
355 	uprintf("\n%s: write failed, filesystem is full\n", fs->fs_fsmnt);
356 	return (ENOSPC);
357 }
358 
359 /*
360  * Reallocate a sequence of blocks into a contiguous sequence of blocks.
361  *
362  * The vnode and an array of buffer pointers for a range of sequential
363  * logical blocks to be made contiguous is given. The allocator attempts
364  * to find a range of sequential blocks starting as close as possible
365  * from the end of the allocation for the logical block immediately
366  * preceding the current range. If successful, the physical block numbers
367  * in the buffer pointers and in the inode are changed to reflect the new
368  * allocation. If unsuccessful, the allocation is left unchanged. The
369  * success in doing the reallocation is returned. Note that the error
370  * return is not reflected back to the user. Rather the previous block
371  * allocation will be used.
372  */
373 
374 SYSCTL_NODE(_vfs, OID_AUTO, ffs, CTLFLAG_RW, 0, "FFS filesystem");
375 
376 static int doasyncfree = 1;
377 SYSCTL_INT(_vfs_ffs, OID_AUTO, doasyncfree, CTLFLAG_RW, &doasyncfree, 0, "");
378 
379 static int doreallocblks = 1;
380 SYSCTL_INT(_vfs_ffs, OID_AUTO, doreallocblks, CTLFLAG_RW, &doreallocblks, 0, "");
381 
382 #ifdef DEBUG
383 static volatile int prtrealloc = 0;
384 #endif
385 
386 int
387 ffs_reallocblks(ap)
388 	struct vop_reallocblks_args /* {
389 		struct vnode *a_vp;
390 		struct cluster_save *a_buflist;
391 	} */ *ap;
392 {
393 
394 	if (doreallocblks == 0)
395 		return (ENOSPC);
396 	if (VTOI(ap->a_vp)->i_ump->um_fstype == UFS1)
397 		return (ffs_reallocblks_ufs1(ap));
398 	return (ffs_reallocblks_ufs2(ap));
399 }
400 
401 static int
402 ffs_reallocblks_ufs1(ap)
403 	struct vop_reallocblks_args /* {
404 		struct vnode *a_vp;
405 		struct cluster_save *a_buflist;
406 	} */ *ap;
407 {
408 	struct fs *fs;
409 	struct inode *ip;
410 	struct vnode *vp;
411 	struct buf *sbp, *ebp;
412 	ufs1_daddr_t *bap, *sbap, *ebap = 0;
413 	struct cluster_save *buflist;
414 	ufs_lbn_t start_lbn, end_lbn;
415 	ufs1_daddr_t soff, newblk, blkno;
416 	ufs2_daddr_t pref;
417 	struct indir start_ap[NIADDR + 1], end_ap[NIADDR + 1], *idp;
418 	int i, len, start_lvl, end_lvl, ssize;
419 
420 	vp = ap->a_vp;
421 	ip = VTOI(vp);
422 	fs = ip->i_fs;
423 	if (fs->fs_contigsumsize <= 0)
424 		return (ENOSPC);
425 	buflist = ap->a_buflist;
426 	len = buflist->bs_nchildren;
427 	start_lbn = buflist->bs_children[0]->b_lblkno;
428 	end_lbn = start_lbn + len - 1;
429 #ifdef DIAGNOSTIC
430 	for (i = 0; i < len; i++)
431 		if (!ffs_checkblk(ip,
432 		   dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
433 			panic("ffs_reallocblks: unallocated block 1");
434 	for (i = 1; i < len; i++)
435 		if (buflist->bs_children[i]->b_lblkno != start_lbn + i)
436 			panic("ffs_reallocblks: non-logical cluster");
437 	blkno = buflist->bs_children[0]->b_blkno;
438 	ssize = fsbtodb(fs, fs->fs_frag);
439 	for (i = 1; i < len - 1; i++)
440 		if (buflist->bs_children[i]->b_blkno != blkno + (i * ssize))
441 			panic("ffs_reallocblks: non-physical cluster %d", i);
442 #endif
443 	/*
444 	 * If the latest allocation is in a new cylinder group, assume that
445 	 * the filesystem has decided to move and do not force it back to
446 	 * the previous cylinder group.
447 	 */
448 	if (dtog(fs, dbtofsb(fs, buflist->bs_children[0]->b_blkno)) !=
449 	    dtog(fs, dbtofsb(fs, buflist->bs_children[len - 1]->b_blkno)))
450 		return (ENOSPC);
451 	if (ufs_getlbns(vp, start_lbn, start_ap, &start_lvl) ||
452 	    ufs_getlbns(vp, end_lbn, end_ap, &end_lvl))
453 		return (ENOSPC);
454 	/*
455 	 * Get the starting offset and block map for the first block.
456 	 */
457 	if (start_lvl == 0) {
458 		sbap = &ip->i_din1->di_db[0];
459 		soff = start_lbn;
460 	} else {
461 		idp = &start_ap[start_lvl - 1];
462 		if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &sbp)) {
463 			brelse(sbp);
464 			return (ENOSPC);
465 		}
466 		sbap = (ufs1_daddr_t *)sbp->b_data;
467 		soff = idp->in_off;
468 	}
469 	/*
470 	 * Find the preferred location for the cluster.
471 	 */
472 	pref = ffs_blkpref_ufs1(ip, start_lbn, soff, sbap);
473 	/*
474 	 * If the block range spans two block maps, get the second map.
475 	 */
476 	if (end_lvl == 0 || (idp = &end_ap[end_lvl - 1])->in_off + 1 >= len) {
477 		ssize = len;
478 	} else {
479 #ifdef DIAGNOSTIC
480 		if (start_ap[start_lvl-1].in_lbn == idp->in_lbn)
481 			panic("ffs_reallocblk: start == end");
482 #endif
483 		ssize = len - (idp->in_off + 1);
484 		if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &ebp))
485 			goto fail;
486 		ebap = (ufs1_daddr_t *)ebp->b_data;
487 	}
488 	/*
489 	 * Search the block map looking for an allocation of the desired size.
490 	 */
491 	if ((newblk = ffs_hashalloc(ip, dtog(fs, pref), pref,
492 	    len, ffs_clusteralloc)) == 0)
493 		goto fail;
494 	/*
495 	 * We have found a new contiguous block.
496 	 *
497 	 * First we have to replace the old block pointers with the new
498 	 * block pointers in the inode and indirect blocks associated
499 	 * with the file.
500 	 */
501 #ifdef DEBUG
502 	if (prtrealloc)
503 		printf("realloc: ino %d, lbns %jd-%jd\n\told:", ip->i_number,
504 		    (intmax_t)start_lbn, (intmax_t)end_lbn);
505 #endif
506 	blkno = newblk;
507 	for (bap = &sbap[soff], i = 0; i < len; i++, blkno += fs->fs_frag) {
508 		if (i == ssize) {
509 			bap = ebap;
510 			soff = -i;
511 		}
512 #ifdef DIAGNOSTIC
513 		if (!ffs_checkblk(ip,
514 		   dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
515 			panic("ffs_reallocblks: unallocated block 2");
516 		if (dbtofsb(fs, buflist->bs_children[i]->b_blkno) != *bap)
517 			panic("ffs_reallocblks: alloc mismatch");
518 #endif
519 #ifdef DEBUG
520 		if (prtrealloc)
521 			printf(" %d,", *bap);
522 #endif
523 		if (DOINGSOFTDEP(vp)) {
524 			if (sbap == &ip->i_din1->di_db[0] && i < ssize)
525 				softdep_setup_allocdirect(ip, start_lbn + i,
526 				    blkno, *bap, fs->fs_bsize, fs->fs_bsize,
527 				    buflist->bs_children[i]);
528 			else
529 				softdep_setup_allocindir_page(ip, start_lbn + i,
530 				    i < ssize ? sbp : ebp, soff + i, blkno,
531 				    *bap, buflist->bs_children[i]);
532 		}
533 		*bap++ = blkno;
534 	}
535 	/*
536 	 * Next we must write out the modified inode and indirect blocks.
537 	 * For strict correctness, the writes should be synchronous since
538 	 * the old block values may have been written to disk. In practise
539 	 * they are almost never written, but if we are concerned about
540 	 * strict correctness, the `doasyncfree' flag should be set to zero.
541 	 *
542 	 * The test on `doasyncfree' should be changed to test a flag
543 	 * that shows whether the associated buffers and inodes have
544 	 * been written. The flag should be set when the cluster is
545 	 * started and cleared whenever the buffer or inode is flushed.
546 	 * We can then check below to see if it is set, and do the
547 	 * synchronous write only when it has been cleared.
548 	 */
549 	if (sbap != &ip->i_din1->di_db[0]) {
550 		if (doasyncfree)
551 			bdwrite(sbp);
552 		else
553 			bwrite(sbp);
554 	} else {
555 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
556 		if (!doasyncfree)
557 			UFS_UPDATE(vp, 1);
558 	}
559 	if (ssize < len) {
560 		if (doasyncfree)
561 			bdwrite(ebp);
562 		else
563 			bwrite(ebp);
564 	}
565 	/*
566 	 * Last, free the old blocks and assign the new blocks to the buffers.
567 	 */
568 #ifdef DEBUG
569 	if (prtrealloc)
570 		printf("\n\tnew:");
571 #endif
572 	for (blkno = newblk, i = 0; i < len; i++, blkno += fs->fs_frag) {
573 		if (!DOINGSOFTDEP(vp))
574 			ffs_blkfree(fs, ip->i_devvp,
575 			    dbtofsb(fs, buflist->bs_children[i]->b_blkno),
576 			    fs->fs_bsize, ip->i_number);
577 		buflist->bs_children[i]->b_blkno = fsbtodb(fs, blkno);
578 #ifdef DIAGNOSTIC
579 		if (!ffs_checkblk(ip,
580 		   dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
581 			panic("ffs_reallocblks: unallocated block 3");
582 #endif
583 #ifdef DEBUG
584 		if (prtrealloc)
585 			printf(" %d,", blkno);
586 #endif
587 	}
588 #ifdef DEBUG
589 	if (prtrealloc) {
590 		prtrealloc--;
591 		printf("\n");
592 	}
593 #endif
594 	return (0);
595 
596 fail:
597 	if (ssize < len)
598 		brelse(ebp);
599 	if (sbap != &ip->i_din1->di_db[0])
600 		brelse(sbp);
601 	return (ENOSPC);
602 }
603 
604 static int
605 ffs_reallocblks_ufs2(ap)
606 	struct vop_reallocblks_args /* {
607 		struct vnode *a_vp;
608 		struct cluster_save *a_buflist;
609 	} */ *ap;
610 {
611 	struct fs *fs;
612 	struct inode *ip;
613 	struct vnode *vp;
614 	struct buf *sbp, *ebp;
615 	ufs2_daddr_t *bap, *sbap, *ebap = 0;
616 	struct cluster_save *buflist;
617 	ufs_lbn_t start_lbn, end_lbn;
618 	ufs2_daddr_t soff, newblk, blkno, pref;
619 	struct indir start_ap[NIADDR + 1], end_ap[NIADDR + 1], *idp;
620 	int i, len, start_lvl, end_lvl, ssize;
621 
622 	vp = ap->a_vp;
623 	ip = VTOI(vp);
624 	fs = ip->i_fs;
625 	if (fs->fs_contigsumsize <= 0)
626 		return (ENOSPC);
627 	buflist = ap->a_buflist;
628 	len = buflist->bs_nchildren;
629 	start_lbn = buflist->bs_children[0]->b_lblkno;
630 	end_lbn = start_lbn + len - 1;
631 #ifdef DIAGNOSTIC
632 	for (i = 0; i < len; i++)
633 		if (!ffs_checkblk(ip,
634 		   dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
635 			panic("ffs_reallocblks: unallocated block 1");
636 	for (i = 1; i < len; i++)
637 		if (buflist->bs_children[i]->b_lblkno != start_lbn + i)
638 			panic("ffs_reallocblks: non-logical cluster");
639 	blkno = buflist->bs_children[0]->b_blkno;
640 	ssize = fsbtodb(fs, fs->fs_frag);
641 	for (i = 1; i < len - 1; i++)
642 		if (buflist->bs_children[i]->b_blkno != blkno + (i * ssize))
643 			panic("ffs_reallocblks: non-physical cluster %d", i);
644 #endif
645 	/*
646 	 * If the latest allocation is in a new cylinder group, assume that
647 	 * the filesystem has decided to move and do not force it back to
648 	 * the previous cylinder group.
649 	 */
650 	if (dtog(fs, dbtofsb(fs, buflist->bs_children[0]->b_blkno)) !=
651 	    dtog(fs, dbtofsb(fs, buflist->bs_children[len - 1]->b_blkno)))
652 		return (ENOSPC);
653 	if (ufs_getlbns(vp, start_lbn, start_ap, &start_lvl) ||
654 	    ufs_getlbns(vp, end_lbn, end_ap, &end_lvl))
655 		return (ENOSPC);
656 	/*
657 	 * Get the starting offset and block map for the first block.
658 	 */
659 	if (start_lvl == 0) {
660 		sbap = &ip->i_din2->di_db[0];
661 		soff = start_lbn;
662 	} else {
663 		idp = &start_ap[start_lvl - 1];
664 		if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &sbp)) {
665 			brelse(sbp);
666 			return (ENOSPC);
667 		}
668 		sbap = (ufs2_daddr_t *)sbp->b_data;
669 		soff = idp->in_off;
670 	}
671 	/*
672 	 * Find the preferred location for the cluster.
673 	 */
674 	pref = ffs_blkpref_ufs2(ip, start_lbn, soff, sbap);
675 	/*
676 	 * If the block range spans two block maps, get the second map.
677 	 */
678 	if (end_lvl == 0 || (idp = &end_ap[end_lvl - 1])->in_off + 1 >= len) {
679 		ssize = len;
680 	} else {
681 #ifdef DIAGNOSTIC
682 		if (start_ap[start_lvl-1].in_lbn == idp->in_lbn)
683 			panic("ffs_reallocblk: start == end");
684 #endif
685 		ssize = len - (idp->in_off + 1);
686 		if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &ebp))
687 			goto fail;
688 		ebap = (ufs2_daddr_t *)ebp->b_data;
689 	}
690 	/*
691 	 * Search the block map looking for an allocation of the desired size.
692 	 */
693 	if ((newblk = ffs_hashalloc(ip, dtog(fs, pref), pref,
694 	    len, ffs_clusteralloc)) == 0)
695 		goto fail;
696 	/*
697 	 * We have found a new contiguous block.
698 	 *
699 	 * First we have to replace the old block pointers with the new
700 	 * block pointers in the inode and indirect blocks associated
701 	 * with the file.
702 	 */
703 #ifdef DEBUG
704 	if (prtrealloc)
705 		printf("realloc: ino %d, lbns %jd-%jd\n\told:", ip->i_number,
706 		    (intmax_t)start_lbn, (intmax_t)end_lbn);
707 #endif
708 	blkno = newblk;
709 	for (bap = &sbap[soff], i = 0; i < len; i++, blkno += fs->fs_frag) {
710 		if (i == ssize) {
711 			bap = ebap;
712 			soff = -i;
713 		}
714 #ifdef DIAGNOSTIC
715 		if (!ffs_checkblk(ip,
716 		   dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
717 			panic("ffs_reallocblks: unallocated block 2");
718 		if (dbtofsb(fs, buflist->bs_children[i]->b_blkno) != *bap)
719 			panic("ffs_reallocblks: alloc mismatch");
720 #endif
721 #ifdef DEBUG
722 		if (prtrealloc)
723 			printf(" %jd,", (intmax_t)*bap);
724 #endif
725 		if (DOINGSOFTDEP(vp)) {
726 			if (sbap == &ip->i_din2->di_db[0] && i < ssize)
727 				softdep_setup_allocdirect(ip, start_lbn + i,
728 				    blkno, *bap, fs->fs_bsize, fs->fs_bsize,
729 				    buflist->bs_children[i]);
730 			else
731 				softdep_setup_allocindir_page(ip, start_lbn + i,
732 				    i < ssize ? sbp : ebp, soff + i, blkno,
733 				    *bap, buflist->bs_children[i]);
734 		}
735 		*bap++ = blkno;
736 	}
737 	/*
738 	 * Next we must write out the modified inode and indirect blocks.
739 	 * For strict correctness, the writes should be synchronous since
740 	 * the old block values may have been written to disk. In practise
741 	 * they are almost never written, but if we are concerned about
742 	 * strict correctness, the `doasyncfree' flag should be set to zero.
743 	 *
744 	 * The test on `doasyncfree' should be changed to test a flag
745 	 * that shows whether the associated buffers and inodes have
746 	 * been written. The flag should be set when the cluster is
747 	 * started and cleared whenever the buffer or inode is flushed.
748 	 * We can then check below to see if it is set, and do the
749 	 * synchronous write only when it has been cleared.
750 	 */
751 	if (sbap != &ip->i_din2->di_db[0]) {
752 		if (doasyncfree)
753 			bdwrite(sbp);
754 		else
755 			bwrite(sbp);
756 	} else {
757 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
758 		if (!doasyncfree)
759 			UFS_UPDATE(vp, 1);
760 	}
761 	if (ssize < len) {
762 		if (doasyncfree)
763 			bdwrite(ebp);
764 		else
765 			bwrite(ebp);
766 	}
767 	/*
768 	 * Last, free the old blocks and assign the new blocks to the buffers.
769 	 */
770 #ifdef DEBUG
771 	if (prtrealloc)
772 		printf("\n\tnew:");
773 #endif
774 	for (blkno = newblk, i = 0; i < len; i++, blkno += fs->fs_frag) {
775 		if (!DOINGSOFTDEP(vp))
776 			ffs_blkfree(fs, ip->i_devvp,
777 			    dbtofsb(fs, buflist->bs_children[i]->b_blkno),
778 			    fs->fs_bsize, ip->i_number);
779 		buflist->bs_children[i]->b_blkno = fsbtodb(fs, blkno);
780 #ifdef DIAGNOSTIC
781 		if (!ffs_checkblk(ip,
782 		   dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
783 			panic("ffs_reallocblks: unallocated block 3");
784 #endif
785 #ifdef DEBUG
786 		if (prtrealloc)
787 			printf(" %jd,", (intmax_t)blkno);
788 #endif
789 	}
790 #ifdef DEBUG
791 	if (prtrealloc) {
792 		prtrealloc--;
793 		printf("\n");
794 	}
795 #endif
796 	return (0);
797 
798 fail:
799 	if (ssize < len)
800 		brelse(ebp);
801 	if (sbap != &ip->i_din2->di_db[0])
802 		brelse(sbp);
803 	return (ENOSPC);
804 }
805 
806 /*
807  * Allocate an inode in the filesystem.
808  *
809  * If allocating a directory, use ffs_dirpref to select the inode.
810  * If allocating in a directory, the following hierarchy is followed:
811  *   1) allocate the preferred inode.
812  *   2) allocate an inode in the same cylinder group.
813  *   3) quadradically rehash into other cylinder groups, until an
814  *      available inode is located.
815  * If no inode preference is given the following heirarchy is used
816  * to allocate an inode:
817  *   1) allocate an inode in cylinder group 0.
818  *   2) quadradically rehash into other cylinder groups, until an
819  *      available inode is located.
820  */
821 int
822 ffs_valloc(pvp, mode, cred, vpp)
823 	struct vnode *pvp;
824 	int mode;
825 	struct ucred *cred;
826 	struct vnode **vpp;
827 {
828 	struct inode *pip;
829 	struct fs *fs;
830 	struct inode *ip;
831 	struct timespec ts;
832 	ino_t ino, ipref;
833 	int cg, error;
834 
835 	*vpp = NULL;
836 	pip = VTOI(pvp);
837 	fs = pip->i_fs;
838 	if (fs->fs_cstotal.cs_nifree == 0)
839 		goto noinodes;
840 
841 	if ((mode & IFMT) == IFDIR)
842 		ipref = ffs_dirpref(pip);
843 	else
844 		ipref = pip->i_number;
845 	if (ipref >= fs->fs_ncg * fs->fs_ipg)
846 		ipref = 0;
847 	cg = ino_to_cg(fs, ipref);
848 	/*
849 	 * Track number of dirs created one after another
850 	 * in a same cg without intervening by files.
851 	 */
852 	if ((mode & IFMT) == IFDIR) {
853 		if (fs->fs_contigdirs[cg] < 255)
854 			fs->fs_contigdirs[cg]++;
855 	} else {
856 		if (fs->fs_contigdirs[cg] > 0)
857 			fs->fs_contigdirs[cg]--;
858 	}
859 	ino = (ino_t)ffs_hashalloc(pip, cg, ipref, mode,
860 					(allocfcn_t *)ffs_nodealloccg);
861 	if (ino == 0)
862 		goto noinodes;
863 	error = VFS_VGET(pvp->v_mount, ino, LK_EXCLUSIVE, vpp);
864 	if (error) {
865 		UFS_VFREE(pvp, ino, mode);
866 		return (error);
867 	}
868 	ip = VTOI(*vpp);
869 	if (ip->i_mode) {
870 		printf("mode = 0%o, inum = %lu, fs = %s\n",
871 		    ip->i_mode, (u_long)ip->i_number, fs->fs_fsmnt);
872 		panic("ffs_valloc: dup alloc");
873 	}
874 	if (DIP(ip, i_blocks) && (fs->fs_flags & FS_UNCLEAN) == 0) {  /* XXX */
875 		printf("free inode %s/%lu had %ld blocks\n",
876 		    fs->fs_fsmnt, (u_long)ino, (long)DIP(ip, i_blocks));
877 		DIP(ip, i_blocks) = 0;
878 	}
879 	ip->i_flags = 0;
880 	DIP(ip, i_flags) = 0;
881 	/*
882 	 * Set up a new generation number for this inode.
883 	 */
884 	if (ip->i_gen == 0 || ++ip->i_gen == 0)
885 		ip->i_gen = random() / 2 + 1;
886 	DIP(ip, i_gen) = ip->i_gen;
887 	if (fs->fs_magic == FS_UFS2_MAGIC) {
888 		vfs_timestamp(&ts);
889 		ip->i_din2->di_birthtime = ts.tv_sec;
890 		ip->i_din2->di_birthnsec = ts.tv_nsec;
891 	}
892 	return (0);
893 noinodes:
894 	ffs_fserr(fs, pip->i_number, "out of inodes");
895 	uprintf("\n%s: create/symlink failed, no inodes free\n", fs->fs_fsmnt);
896 	return (ENOSPC);
897 }
898 
899 /*
900  * Find a cylinder group to place a directory.
901  *
902  * The policy implemented by this algorithm is to allocate a
903  * directory inode in the same cylinder group as its parent
904  * directory, but also to reserve space for its files inodes
905  * and data. Restrict the number of directories which may be
906  * allocated one after another in the same cylinder group
907  * without intervening allocation of files.
908  *
909  * If we allocate a first level directory then force allocation
910  * in another cylinder group.
911  */
912 static ino_t
913 ffs_dirpref(pip)
914 	struct inode *pip;
915 {
916 	struct fs *fs;
917 	int cg, prefcg, dirsize, cgsize;
918 	int avgifree, avgbfree, avgndir, curdirsize;
919 	int minifree, minbfree, maxndir;
920 	int mincg, minndir;
921 	int maxcontigdirs;
922 
923 	fs = pip->i_fs;
924 
925 	avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg;
926 	avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
927 	avgndir = fs->fs_cstotal.cs_ndir / fs->fs_ncg;
928 
929 	/*
930 	 * Force allocation in another cg if creating a first level dir.
931 	 */
932 	ASSERT_VOP_LOCKED(ITOV(pip), "ffs_dirpref");
933 	if (ITOV(pip)->v_vflag & VV_ROOT) {
934 		prefcg = arc4random() % fs->fs_ncg;
935 		mincg = prefcg;
936 		minndir = fs->fs_ipg;
937 		for (cg = prefcg; cg < fs->fs_ncg; cg++)
938 			if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
939 			    fs->fs_cs(fs, cg).cs_nifree >= avgifree &&
940 			    fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
941 				mincg = cg;
942 				minndir = fs->fs_cs(fs, cg).cs_ndir;
943 			}
944 		for (cg = 0; cg < prefcg; cg++)
945 			if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
946 			    fs->fs_cs(fs, cg).cs_nifree >= avgifree &&
947 			    fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
948 				mincg = cg;
949 				minndir = fs->fs_cs(fs, cg).cs_ndir;
950 			}
951 		return ((ino_t)(fs->fs_ipg * mincg));
952 	}
953 
954 	/*
955 	 * Count various limits which used for
956 	 * optimal allocation of a directory inode.
957 	 */
958 	maxndir = min(avgndir + fs->fs_ipg / 16, fs->fs_ipg);
959 	minifree = avgifree - fs->fs_ipg / 4;
960 	if (minifree < 0)
961 		minifree = 0;
962 	minbfree = avgbfree - fs->fs_fpg / fs->fs_frag / 4;
963 	if (minbfree < 0)
964 		minbfree = 0;
965 	cgsize = fs->fs_fsize * fs->fs_fpg;
966 	dirsize = fs->fs_avgfilesize * fs->fs_avgfpdir;
967 	curdirsize = avgndir ? (cgsize - avgbfree * fs->fs_bsize) / avgndir : 0;
968 	if (dirsize < curdirsize)
969 		dirsize = curdirsize;
970 	maxcontigdirs = min(cgsize / dirsize, 255);
971 	if (fs->fs_avgfpdir > 0)
972 		maxcontigdirs = min(maxcontigdirs,
973 				    fs->fs_ipg / fs->fs_avgfpdir);
974 	if (maxcontigdirs == 0)
975 		maxcontigdirs = 1;
976 
977 	/*
978 	 * Limit number of dirs in one cg and reserve space for
979 	 * regular files, but only if we have no deficit in
980 	 * inodes or space.
981 	 */
982 	prefcg = ino_to_cg(fs, pip->i_number);
983 	for (cg = prefcg; cg < fs->fs_ncg; cg++)
984 		if (fs->fs_cs(fs, cg).cs_ndir < maxndir &&
985 		    fs->fs_cs(fs, cg).cs_nifree >= minifree &&
986 	    	    fs->fs_cs(fs, cg).cs_nbfree >= minbfree) {
987 			if (fs->fs_contigdirs[cg] < maxcontigdirs)
988 				return ((ino_t)(fs->fs_ipg * cg));
989 		}
990 	for (cg = 0; cg < prefcg; cg++)
991 		if (fs->fs_cs(fs, cg).cs_ndir < maxndir &&
992 		    fs->fs_cs(fs, cg).cs_nifree >= minifree &&
993 	    	    fs->fs_cs(fs, cg).cs_nbfree >= minbfree) {
994 			if (fs->fs_contigdirs[cg] < maxcontigdirs)
995 				return ((ino_t)(fs->fs_ipg * cg));
996 		}
997 	/*
998 	 * This is a backstop when we have deficit in space.
999 	 */
1000 	for (cg = prefcg; cg < fs->fs_ncg; cg++)
1001 		if (fs->fs_cs(fs, cg).cs_nifree >= avgifree)
1002 			return ((ino_t)(fs->fs_ipg * cg));
1003 	for (cg = 0; cg < prefcg; cg++)
1004 		if (fs->fs_cs(fs, cg).cs_nifree >= avgifree)
1005 			break;
1006 	return ((ino_t)(fs->fs_ipg * cg));
1007 }
1008 
1009 /*
1010  * Select the desired position for the next block in a file.  The file is
1011  * logically divided into sections. The first section is composed of the
1012  * direct blocks. Each additional section contains fs_maxbpg blocks.
1013  *
1014  * If no blocks have been allocated in the first section, the policy is to
1015  * request a block in the same cylinder group as the inode that describes
1016  * the file. If no blocks have been allocated in any other section, the
1017  * policy is to place the section in a cylinder group with a greater than
1018  * average number of free blocks.  An appropriate cylinder group is found
1019  * by using a rotor that sweeps the cylinder groups. When a new group of
1020  * blocks is needed, the sweep begins in the cylinder group following the
1021  * cylinder group from which the previous allocation was made. The sweep
1022  * continues until a cylinder group with greater than the average number
1023  * of free blocks is found. If the allocation is for the first block in an
1024  * indirect block, the information on the previous allocation is unavailable;
1025  * here a best guess is made based upon the logical block number being
1026  * allocated.
1027  *
1028  * If a section is already partially allocated, the policy is to
1029  * contiguously allocate fs_maxcontig blocks. The end of one of these
1030  * contiguous blocks and the beginning of the next is laid out
1031  * contiguously if possible.
1032  */
1033 ufs2_daddr_t
1034 ffs_blkpref_ufs1(ip, lbn, indx, bap)
1035 	struct inode *ip;
1036 	ufs_lbn_t lbn;
1037 	int indx;
1038 	ufs1_daddr_t *bap;
1039 {
1040 	struct fs *fs;
1041 	int cg;
1042 	int avgbfree, startcg;
1043 
1044 	fs = ip->i_fs;
1045 	if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
1046 		if (lbn < NDADDR + NINDIR(fs)) {
1047 			cg = ino_to_cg(fs, ip->i_number);
1048 			return (fs->fs_fpg * cg + fs->fs_frag);
1049 		}
1050 		/*
1051 		 * Find a cylinder with greater than average number of
1052 		 * unused data blocks.
1053 		 */
1054 		if (indx == 0 || bap[indx - 1] == 0)
1055 			startcg =
1056 			    ino_to_cg(fs, ip->i_number) + lbn / fs->fs_maxbpg;
1057 		else
1058 			startcg = dtog(fs, bap[indx - 1]) + 1;
1059 		startcg %= fs->fs_ncg;
1060 		avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
1061 		for (cg = startcg; cg < fs->fs_ncg; cg++)
1062 			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
1063 				fs->fs_cgrotor = cg;
1064 				return (fs->fs_fpg * cg + fs->fs_frag);
1065 			}
1066 		for (cg = 0; cg <= startcg; cg++)
1067 			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
1068 				fs->fs_cgrotor = cg;
1069 				return (fs->fs_fpg * cg + fs->fs_frag);
1070 			}
1071 		return (0);
1072 	}
1073 	/*
1074 	 * We just always try to lay things out contiguously.
1075 	 */
1076 	return (bap[indx - 1] + fs->fs_frag);
1077 }
1078 
1079 /*
1080  * Same as above, but for UFS2
1081  */
1082 ufs2_daddr_t
1083 ffs_blkpref_ufs2(ip, lbn, indx, bap)
1084 	struct inode *ip;
1085 	ufs_lbn_t lbn;
1086 	int indx;
1087 	ufs2_daddr_t *bap;
1088 {
1089 	struct fs *fs;
1090 	int cg;
1091 	int avgbfree, startcg;
1092 
1093 	fs = ip->i_fs;
1094 	if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
1095 		if (lbn < NDADDR + NINDIR(fs)) {
1096 			cg = ino_to_cg(fs, ip->i_number);
1097 			return (fs->fs_fpg * cg + fs->fs_frag);
1098 		}
1099 		/*
1100 		 * Find a cylinder with greater than average number of
1101 		 * unused data blocks.
1102 		 */
1103 		if (indx == 0 || bap[indx - 1] == 0)
1104 			startcg =
1105 			    ino_to_cg(fs, ip->i_number) + lbn / fs->fs_maxbpg;
1106 		else
1107 			startcg = dtog(fs, bap[indx - 1]) + 1;
1108 		startcg %= fs->fs_ncg;
1109 		avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
1110 		for (cg = startcg; cg < fs->fs_ncg; cg++)
1111 			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
1112 				fs->fs_cgrotor = cg;
1113 				return (fs->fs_fpg * cg + fs->fs_frag);
1114 			}
1115 		for (cg = 0; cg <= startcg; cg++)
1116 			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
1117 				fs->fs_cgrotor = cg;
1118 				return (fs->fs_fpg * cg + fs->fs_frag);
1119 			}
1120 		return (0);
1121 	}
1122 	/*
1123 	 * We just always try to lay things out contiguously.
1124 	 */
1125 	return (bap[indx - 1] + fs->fs_frag);
1126 }
1127 
1128 /*
1129  * Implement the cylinder overflow algorithm.
1130  *
1131  * The policy implemented by this algorithm is:
1132  *   1) allocate the block in its requested cylinder group.
1133  *   2) quadradically rehash on the cylinder group number.
1134  *   3) brute force search for a free block.
1135  */
1136 /*VARARGS5*/
1137 static ufs2_daddr_t
1138 ffs_hashalloc(ip, cg, pref, size, allocator)
1139 	struct inode *ip;
1140 	int cg;
1141 	ufs2_daddr_t pref;
1142 	int size;	/* size for data blocks, mode for inodes */
1143 	allocfcn_t *allocator;
1144 {
1145 	struct fs *fs;
1146 	ufs2_daddr_t result;
1147 	int i, icg = cg;
1148 
1149 #ifdef DIAGNOSTIC
1150 	if (ITOV(ip)->v_mount->mnt_kern_flag & MNTK_SUSPENDED)
1151 		panic("ffs_hashalloc: allocation on suspended filesystem");
1152 #endif
1153 	fs = ip->i_fs;
1154 	/*
1155 	 * 1: preferred cylinder group
1156 	 */
1157 	result = (*allocator)(ip, cg, pref, size);
1158 	if (result)
1159 		return (result);
1160 	/*
1161 	 * 2: quadratic rehash
1162 	 */
1163 	for (i = 1; i < fs->fs_ncg; i *= 2) {
1164 		cg += i;
1165 		if (cg >= fs->fs_ncg)
1166 			cg -= fs->fs_ncg;
1167 		result = (*allocator)(ip, cg, 0, size);
1168 		if (result)
1169 			return (result);
1170 	}
1171 	/*
1172 	 * 3: brute force search
1173 	 * Note that we start at i == 2, since 0 was checked initially,
1174 	 * and 1 is always checked in the quadratic rehash.
1175 	 */
1176 	cg = (icg + 2) % fs->fs_ncg;
1177 	for (i = 2; i < fs->fs_ncg; i++) {
1178 		result = (*allocator)(ip, cg, 0, size);
1179 		if (result)
1180 			return (result);
1181 		cg++;
1182 		if (cg == fs->fs_ncg)
1183 			cg = 0;
1184 	}
1185 	return (0);
1186 }
1187 
1188 /*
1189  * Determine whether a fragment can be extended.
1190  *
1191  * Check to see if the necessary fragments are available, and
1192  * if they are, allocate them.
1193  */
1194 static ufs2_daddr_t
1195 ffs_fragextend(ip, cg, bprev, osize, nsize)
1196 	struct inode *ip;
1197 	int cg;
1198 	ufs2_daddr_t bprev;
1199 	int osize, nsize;
1200 {
1201 	struct fs *fs;
1202 	struct cg *cgp;
1203 	struct buf *bp;
1204 	long bno;
1205 	int frags, bbase;
1206 	int i, error;
1207 	u_int8_t *blksfree;
1208 
1209 	fs = ip->i_fs;
1210 	if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize))
1211 		return (0);
1212 	frags = numfrags(fs, nsize);
1213 	bbase = fragnum(fs, bprev);
1214 	if (bbase > fragnum(fs, (bprev + frags - 1))) {
1215 		/* cannot extend across a block boundary */
1216 		return (0);
1217 	}
1218 	error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
1219 		(int)fs->fs_cgsize, NOCRED, &bp);
1220 	if (error) {
1221 		brelse(bp);
1222 		return (0);
1223 	}
1224 	cgp = (struct cg *)bp->b_data;
1225 	if (!cg_chkmagic(cgp)) {
1226 		brelse(bp);
1227 		return (0);
1228 	}
1229 	bp->b_xflags |= BX_BKGRDWRITE;
1230 	cgp->cg_old_time = cgp->cg_time = time_second;
1231 	bno = dtogd(fs, bprev);
1232 	blksfree = cg_blksfree(cgp);
1233 	for (i = numfrags(fs, osize); i < frags; i++)
1234 		if (isclr(blksfree, bno + i)) {
1235 			brelse(bp);
1236 			return (0);
1237 		}
1238 	/*
1239 	 * the current fragment can be extended
1240 	 * deduct the count on fragment being extended into
1241 	 * increase the count on the remaining fragment (if any)
1242 	 * allocate the extended piece
1243 	 */
1244 	for (i = frags; i < fs->fs_frag - bbase; i++)
1245 		if (isclr(blksfree, bno + i))
1246 			break;
1247 	cgp->cg_frsum[i - numfrags(fs, osize)]--;
1248 	if (i != frags)
1249 		cgp->cg_frsum[i - frags]++;
1250 	for (i = numfrags(fs, osize); i < frags; i++) {
1251 		clrbit(blksfree, bno + i);
1252 		cgp->cg_cs.cs_nffree--;
1253 		fs->fs_cstotal.cs_nffree--;
1254 		fs->fs_cs(fs, cg).cs_nffree--;
1255 	}
1256 	fs->fs_fmod = 1;
1257 	if (DOINGSOFTDEP(ITOV(ip)))
1258 		softdep_setup_blkmapdep(bp, fs, bprev);
1259 	if (fs->fs_active != 0)
1260 		atomic_clear_int(&ACTIVECGNUM(fs, cg), ACTIVECGOFF(cg));
1261 	bdwrite(bp);
1262 	return (bprev);
1263 }
1264 
1265 /*
1266  * Determine whether a block can be allocated.
1267  *
1268  * Check to see if a block of the appropriate size is available,
1269  * and if it is, allocate it.
1270  */
1271 static ufs2_daddr_t
1272 ffs_alloccg(ip, cg, bpref, size)
1273 	struct inode *ip;
1274 	int cg;
1275 	ufs2_daddr_t bpref;
1276 	int size;
1277 {
1278 	struct fs *fs;
1279 	struct cg *cgp;
1280 	struct buf *bp;
1281 	ufs1_daddr_t bno;
1282 	ufs2_daddr_t blkno;
1283 	int i, allocsiz, error, frags;
1284 	u_int8_t *blksfree;
1285 
1286 	fs = ip->i_fs;
1287 	if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize)
1288 		return (0);
1289 	error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
1290 		(int)fs->fs_cgsize, NOCRED, &bp);
1291 	if (error) {
1292 		brelse(bp);
1293 		return (0);
1294 	}
1295 	cgp = (struct cg *)bp->b_data;
1296 	if (!cg_chkmagic(cgp) ||
1297 	    (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) {
1298 		brelse(bp);
1299 		return (0);
1300 	}
1301 	bp->b_xflags |= BX_BKGRDWRITE;
1302 	cgp->cg_old_time = cgp->cg_time = time_second;
1303 	if (size == fs->fs_bsize) {
1304 		blkno = ffs_alloccgblk(ip, bp, bpref);
1305 		if (fs->fs_active != 0)
1306 			atomic_clear_int(&ACTIVECGNUM(fs, cg), ACTIVECGOFF(cg));
1307 		bdwrite(bp);
1308 		return (blkno);
1309 	}
1310 	/*
1311 	 * check to see if any fragments are already available
1312 	 * allocsiz is the size which will be allocated, hacking
1313 	 * it down to a smaller size if necessary
1314 	 */
1315 	blksfree = cg_blksfree(cgp);
1316 	frags = numfrags(fs, size);
1317 	for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++)
1318 		if (cgp->cg_frsum[allocsiz] != 0)
1319 			break;
1320 	if (allocsiz == fs->fs_frag) {
1321 		/*
1322 		 * no fragments were available, so a block will be
1323 		 * allocated, and hacked up
1324 		 */
1325 		if (cgp->cg_cs.cs_nbfree == 0) {
1326 			brelse(bp);
1327 			return (0);
1328 		}
1329 		blkno = ffs_alloccgblk(ip, bp, bpref);
1330 		bno = dtogd(fs, blkno);
1331 		for (i = frags; i < fs->fs_frag; i++)
1332 			setbit(blksfree, bno + i);
1333 		i = fs->fs_frag - frags;
1334 		cgp->cg_cs.cs_nffree += i;
1335 		fs->fs_cstotal.cs_nffree += i;
1336 		fs->fs_cs(fs, cg).cs_nffree += i;
1337 		fs->fs_fmod = 1;
1338 		cgp->cg_frsum[i]++;
1339 		if (fs->fs_active != 0)
1340 			atomic_clear_int(&ACTIVECGNUM(fs, cg), ACTIVECGOFF(cg));
1341 		bdwrite(bp);
1342 		return (blkno);
1343 	}
1344 	bno = ffs_mapsearch(fs, cgp, bpref, allocsiz);
1345 	if (bno < 0) {
1346 		brelse(bp);
1347 		return (0);
1348 	}
1349 	for (i = 0; i < frags; i++)
1350 		clrbit(blksfree, bno + i);
1351 	cgp->cg_cs.cs_nffree -= frags;
1352 	fs->fs_cstotal.cs_nffree -= frags;
1353 	fs->fs_cs(fs, cg).cs_nffree -= frags;
1354 	fs->fs_fmod = 1;
1355 	cgp->cg_frsum[allocsiz]--;
1356 	if (frags != allocsiz)
1357 		cgp->cg_frsum[allocsiz - frags]++;
1358 	blkno = cg * fs->fs_fpg + bno;
1359 	if (DOINGSOFTDEP(ITOV(ip)))
1360 		softdep_setup_blkmapdep(bp, fs, blkno);
1361 	if (fs->fs_active != 0)
1362 		atomic_clear_int(&ACTIVECGNUM(fs, cg), ACTIVECGOFF(cg));
1363 	bdwrite(bp);
1364 	return (blkno);
1365 }
1366 
1367 /*
1368  * Allocate a block in a cylinder group.
1369  *
1370  * This algorithm implements the following policy:
1371  *   1) allocate the requested block.
1372  *   2) allocate a rotationally optimal block in the same cylinder.
1373  *   3) allocate the next available block on the block rotor for the
1374  *      specified cylinder group.
1375  * Note that this routine only allocates fs_bsize blocks; these
1376  * blocks may be fragmented by the routine that allocates them.
1377  */
1378 static ufs2_daddr_t
1379 ffs_alloccgblk(ip, bp, bpref)
1380 	struct inode *ip;
1381 	struct buf *bp;
1382 	ufs2_daddr_t bpref;
1383 {
1384 	struct fs *fs;
1385 	struct cg *cgp;
1386 	ufs1_daddr_t bno;
1387 	ufs2_daddr_t blkno;
1388 	u_int8_t *blksfree;
1389 
1390 	fs = ip->i_fs;
1391 	cgp = (struct cg *)bp->b_data;
1392 	blksfree = cg_blksfree(cgp);
1393 	if (bpref == 0 || dtog(fs, bpref) != cgp->cg_cgx) {
1394 		bpref = cgp->cg_rotor;
1395 	} else {
1396 		bpref = blknum(fs, bpref);
1397 		bno = dtogd(fs, bpref);
1398 		/*
1399 		 * if the requested block is available, use it
1400 		 */
1401 		if (ffs_isblock(fs, blksfree, fragstoblks(fs, bno)))
1402 			goto gotit;
1403 	}
1404 	/*
1405 	 * Take the next available block in this cylinder group.
1406 	 */
1407 	bno = ffs_mapsearch(fs, cgp, bpref, (int)fs->fs_frag);
1408 	if (bno < 0)
1409 		return (0);
1410 	cgp->cg_rotor = bno;
1411 gotit:
1412 	blkno = fragstoblks(fs, bno);
1413 	ffs_clrblock(fs, blksfree, (long)blkno);
1414 	ffs_clusteracct(fs, cgp, blkno, -1);
1415 	cgp->cg_cs.cs_nbfree--;
1416 	fs->fs_cstotal.cs_nbfree--;
1417 	fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--;
1418 	fs->fs_fmod = 1;
1419 	blkno = cgp->cg_cgx * fs->fs_fpg + bno;
1420 	if (DOINGSOFTDEP(ITOV(ip)))
1421 		softdep_setup_blkmapdep(bp, fs, blkno);
1422 	return (blkno);
1423 }
1424 
1425 /*
1426  * Determine whether a cluster can be allocated.
1427  *
1428  * We do not currently check for optimal rotational layout if there
1429  * are multiple choices in the same cylinder group. Instead we just
1430  * take the first one that we find following bpref.
1431  */
1432 static ufs2_daddr_t
1433 ffs_clusteralloc(ip, cg, bpref, len)
1434 	struct inode *ip;
1435 	int cg;
1436 	ufs2_daddr_t bpref;
1437 	int len;
1438 {
1439 	struct fs *fs;
1440 	struct cg *cgp;
1441 	struct buf *bp;
1442 	int i, run, bit, map, got;
1443 	ufs2_daddr_t bno;
1444 	u_char *mapp;
1445 	int32_t *lp;
1446 	u_int8_t *blksfree;
1447 
1448 	fs = ip->i_fs;
1449 	if (fs->fs_maxcluster[cg] < len)
1450 		return (0);
1451 	if (bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize,
1452 	    NOCRED, &bp))
1453 		goto fail;
1454 	cgp = (struct cg *)bp->b_data;
1455 	if (!cg_chkmagic(cgp))
1456 		goto fail;
1457 	bp->b_xflags |= BX_BKGRDWRITE;
1458 	/*
1459 	 * Check to see if a cluster of the needed size (or bigger) is
1460 	 * available in this cylinder group.
1461 	 */
1462 	lp = &cg_clustersum(cgp)[len];
1463 	for (i = len; i <= fs->fs_contigsumsize; i++)
1464 		if (*lp++ > 0)
1465 			break;
1466 	if (i > fs->fs_contigsumsize) {
1467 		/*
1468 		 * This is the first time looking for a cluster in this
1469 		 * cylinder group. Update the cluster summary information
1470 		 * to reflect the true maximum sized cluster so that
1471 		 * future cluster allocation requests can avoid reading
1472 		 * the cylinder group map only to find no clusters.
1473 		 */
1474 		lp = &cg_clustersum(cgp)[len - 1];
1475 		for (i = len - 1; i > 0; i--)
1476 			if (*lp-- > 0)
1477 				break;
1478 		fs->fs_maxcluster[cg] = i;
1479 		goto fail;
1480 	}
1481 	/*
1482 	 * Search the cluster map to find a big enough cluster.
1483 	 * We take the first one that we find, even if it is larger
1484 	 * than we need as we prefer to get one close to the previous
1485 	 * block allocation. We do not search before the current
1486 	 * preference point as we do not want to allocate a block
1487 	 * that is allocated before the previous one (as we will
1488 	 * then have to wait for another pass of the elevator
1489 	 * algorithm before it will be read). We prefer to fail and
1490 	 * be recalled to try an allocation in the next cylinder group.
1491 	 */
1492 	if (dtog(fs, bpref) != cg)
1493 		bpref = 0;
1494 	else
1495 		bpref = fragstoblks(fs, dtogd(fs, blknum(fs, bpref)));
1496 	mapp = &cg_clustersfree(cgp)[bpref / NBBY];
1497 	map = *mapp++;
1498 	bit = 1 << (bpref % NBBY);
1499 	for (run = 0, got = bpref; got < cgp->cg_nclusterblks; got++) {
1500 		if ((map & bit) == 0) {
1501 			run = 0;
1502 		} else {
1503 			run++;
1504 			if (run == len)
1505 				break;
1506 		}
1507 		if ((got & (NBBY - 1)) != (NBBY - 1)) {
1508 			bit <<= 1;
1509 		} else {
1510 			map = *mapp++;
1511 			bit = 1;
1512 		}
1513 	}
1514 	if (got >= cgp->cg_nclusterblks)
1515 		goto fail;
1516 	/*
1517 	 * Allocate the cluster that we have found.
1518 	 */
1519 	blksfree = cg_blksfree(cgp);
1520 	for (i = 1; i <= len; i++)
1521 		if (!ffs_isblock(fs, blksfree, got - run + i))
1522 			panic("ffs_clusteralloc: map mismatch");
1523 	bno = cg * fs->fs_fpg + blkstofrags(fs, got - run + 1);
1524 	if (dtog(fs, bno) != cg)
1525 		panic("ffs_clusteralloc: allocated out of group");
1526 	len = blkstofrags(fs, len);
1527 	for (i = 0; i < len; i += fs->fs_frag)
1528 		if (ffs_alloccgblk(ip, bp, bno + i) != bno + i)
1529 			panic("ffs_clusteralloc: lost block");
1530 	if (fs->fs_active != 0)
1531 		atomic_clear_int(&ACTIVECGNUM(fs, cg), ACTIVECGOFF(cg));
1532 	bdwrite(bp);
1533 	return (bno);
1534 
1535 fail:
1536 	brelse(bp);
1537 	return (0);
1538 }
1539 
1540 /*
1541  * Determine whether an inode can be allocated.
1542  *
1543  * Check to see if an inode is available, and if it is,
1544  * allocate it using the following policy:
1545  *   1) allocate the requested inode.
1546  *   2) allocate the next available inode after the requested
1547  *      inode in the specified cylinder group.
1548  */
1549 static ufs2_daddr_t
1550 ffs_nodealloccg(ip, cg, ipref, mode)
1551 	struct inode *ip;
1552 	int cg;
1553 	ufs2_daddr_t ipref;
1554 	int mode;
1555 {
1556 	struct fs *fs;
1557 	struct cg *cgp;
1558 	struct buf *bp, *ibp;
1559 	u_int8_t *inosused;
1560 	struct ufs2_dinode *dp2;
1561 	int error, start, len, loc, map, i;
1562 
1563 	fs = ip->i_fs;
1564 	if (fs->fs_cs(fs, cg).cs_nifree == 0)
1565 		return (0);
1566 	error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
1567 		(int)fs->fs_cgsize, NOCRED, &bp);
1568 	if (error) {
1569 		brelse(bp);
1570 		return (0);
1571 	}
1572 	cgp = (struct cg *)bp->b_data;
1573 	if (!cg_chkmagic(cgp) || cgp->cg_cs.cs_nifree == 0) {
1574 		brelse(bp);
1575 		return (0);
1576 	}
1577 	bp->b_xflags |= BX_BKGRDWRITE;
1578 	cgp->cg_old_time = cgp->cg_time = time_second;
1579 	inosused = cg_inosused(cgp);
1580 	if (ipref) {
1581 		ipref %= fs->fs_ipg;
1582 		if (isclr(inosused, ipref))
1583 			goto gotit;
1584 	}
1585 	start = cgp->cg_irotor / NBBY;
1586 	len = howmany(fs->fs_ipg - cgp->cg_irotor, NBBY);
1587 	loc = skpc(0xff, len, &inosused[start]);
1588 	if (loc == 0) {
1589 		len = start + 1;
1590 		start = 0;
1591 		loc = skpc(0xff, len, &inosused[0]);
1592 		if (loc == 0) {
1593 			printf("cg = %d, irotor = %ld, fs = %s\n",
1594 			    cg, (long)cgp->cg_irotor, fs->fs_fsmnt);
1595 			panic("ffs_nodealloccg: map corrupted");
1596 			/* NOTREACHED */
1597 		}
1598 	}
1599 	i = start + len - loc;
1600 	map = inosused[i];
1601 	ipref = i * NBBY;
1602 	for (i = 1; i < (1 << NBBY); i <<= 1, ipref++) {
1603 		if ((map & i) == 0) {
1604 			cgp->cg_irotor = ipref;
1605 			goto gotit;
1606 		}
1607 	}
1608 	printf("fs = %s\n", fs->fs_fsmnt);
1609 	panic("ffs_nodealloccg: block not in map");
1610 	/* NOTREACHED */
1611 gotit:
1612 	if (DOINGSOFTDEP(ITOV(ip)))
1613 		softdep_setup_inomapdep(bp, ip, cg * fs->fs_ipg + ipref);
1614 	setbit(inosused, ipref);
1615 	cgp->cg_cs.cs_nifree--;
1616 	fs->fs_cstotal.cs_nifree--;
1617 	fs->fs_cs(fs, cg).cs_nifree--;
1618 	fs->fs_fmod = 1;
1619 	if ((mode & IFMT) == IFDIR) {
1620 		cgp->cg_cs.cs_ndir++;
1621 		fs->fs_cstotal.cs_ndir++;
1622 		fs->fs_cs(fs, cg).cs_ndir++;
1623 	}
1624 	/*
1625 	 * Check to see if we need to initialize more inodes.
1626 	 */
1627 	if (fs->fs_magic == FS_UFS2_MAGIC &&
1628 	    ipref + INOPB(fs) > cgp->cg_initediblk &&
1629 	    cgp->cg_initediblk < cgp->cg_niblk) {
1630 		ibp = getblk(ip->i_devvp, fsbtodb(fs,
1631 		    ino_to_fsba(fs, cg * fs->fs_ipg + cgp->cg_initediblk)),
1632 		    (int)fs->fs_bsize, 0, 0);
1633 		bzero(ibp->b_data, (int)fs->fs_bsize);
1634 		dp2 = (struct ufs2_dinode *)(ibp->b_data);
1635 		for (i = 0; i < INOPB(fs); i++) {
1636 			dp2->di_gen = random() / 2 + 1;
1637 			dp2++;
1638 		}
1639 		bawrite(ibp);
1640 		cgp->cg_initediblk += INOPB(fs);
1641 	}
1642 	if (fs->fs_active != 0)
1643 		atomic_clear_int(&ACTIVECGNUM(fs, cg), ACTIVECGOFF(cg));
1644 	bdwrite(bp);
1645 	return (cg * fs->fs_ipg + ipref);
1646 }
1647 
1648 /*
1649  * check if a block is free
1650  */
1651 static int
1652 ffs_isfreeblock(struct fs *fs, u_char *cp, ufs1_daddr_t h)
1653 {
1654 
1655 	switch ((int)fs->fs_frag) {
1656 	case 8:
1657 		return (cp[h] == 0);
1658 	case 4:
1659 		return ((cp[h >> 1] & (0x0f << ((h & 0x1) << 2))) == 0);
1660 	case 2:
1661 		return ((cp[h >> 2] & (0x03 << ((h & 0x3) << 1))) == 0);
1662 	case 1:
1663 		return ((cp[h >> 3] & (0x01 << (h & 0x7))) == 0);
1664 	default:
1665 		panic("ffs_isfreeblock");
1666 	}
1667 	return (0);
1668 }
1669 
1670 /*
1671  * Free a block or fragment.
1672  *
1673  * The specified block or fragment is placed back in the
1674  * free map. If a fragment is deallocated, a possible
1675  * block reassembly is checked.
1676  */
1677 void
1678 ffs_blkfree(fs, devvp, bno, size, inum)
1679 	struct fs *fs;
1680 	struct vnode *devvp;
1681 	ufs2_daddr_t bno;
1682 	long size;
1683 	ino_t inum;
1684 {
1685 	struct cg *cgp;
1686 	struct buf *bp;
1687 	ufs1_daddr_t fragno, cgbno;
1688 	ufs2_daddr_t cgblkno;
1689 	int i, error, cg, blk, frags, bbase;
1690 	u_int8_t *blksfree;
1691 	dev_t dev;
1692 
1693 	cg = dtog(fs, bno);
1694 	if (devvp->v_type != VCHR) {
1695 		/* devvp is a snapshot */
1696 		dev = VTOI(devvp)->i_devvp->v_rdev;
1697 		cgblkno = fragstoblks(fs, cgtod(fs, cg));
1698 	} else {
1699 		/* devvp is a normal disk device */
1700 		dev = devvp->v_rdev;
1701 		cgblkno = fsbtodb(fs, cgtod(fs, cg));
1702 		ASSERT_VOP_LOCKED(devvp, "ffs_blkfree");
1703 		if ((devvp->v_vflag & VV_COPYONWRITE) &&
1704 		    ffs_snapblkfree(fs, devvp, bno, size, inum))
1705 			return;
1706 		VOP_FREEBLKS(devvp, fsbtodb(fs, bno), size);
1707 	}
1708 #ifdef DIAGNOSTIC
1709 	if (dev->si_mountpoint &&
1710 	    (dev->si_mountpoint->mnt_kern_flag & MNTK_SUSPENDED))
1711 		panic("ffs_blkfree: deallocation on suspended filesystem");
1712 	if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0 ||
1713 	    fragnum(fs, bno) + numfrags(fs, size) > fs->fs_frag) {
1714 		printf("dev=%s, bno = %jd, bsize = %ld, size = %ld, fs = %s\n",
1715 		    devtoname(dev), (intmax_t)bno, (long)fs->fs_bsize,
1716 		    size, fs->fs_fsmnt);
1717 		panic("ffs_blkfree: bad size");
1718 	}
1719 #endif
1720 	if ((u_int)bno >= fs->fs_size) {
1721 		printf("bad block %jd, ino %lu\n", (intmax_t)bno,
1722 		    (u_long)inum);
1723 		ffs_fserr(fs, inum, "bad block");
1724 		return;
1725 	}
1726 	if ((error = bread(devvp, cgblkno, (int)fs->fs_cgsize, NOCRED, &bp))) {
1727 		brelse(bp);
1728 		return;
1729 	}
1730 	cgp = (struct cg *)bp->b_data;
1731 	if (!cg_chkmagic(cgp)) {
1732 		brelse(bp);
1733 		return;
1734 	}
1735 	bp->b_xflags |= BX_BKGRDWRITE;
1736 	cgp->cg_old_time = cgp->cg_time = time_second;
1737 	cgbno = dtogd(fs, bno);
1738 	blksfree = cg_blksfree(cgp);
1739 	if (size == fs->fs_bsize) {
1740 		fragno = fragstoblks(fs, cgbno);
1741 		if (!ffs_isfreeblock(fs, blksfree, fragno)) {
1742 			if (devvp->v_type != VCHR) {
1743 				/* devvp is a snapshot */
1744 				brelse(bp);
1745 				return;
1746 			}
1747 			printf("dev = %s, block = %jd, fs = %s\n",
1748 			    devtoname(dev), (intmax_t)bno, fs->fs_fsmnt);
1749 			panic("ffs_blkfree: freeing free block");
1750 		}
1751 		ffs_setblock(fs, blksfree, fragno);
1752 		ffs_clusteracct(fs, cgp, fragno, 1);
1753 		cgp->cg_cs.cs_nbfree++;
1754 		fs->fs_cstotal.cs_nbfree++;
1755 		fs->fs_cs(fs, cg).cs_nbfree++;
1756 	} else {
1757 		bbase = cgbno - fragnum(fs, cgbno);
1758 		/*
1759 		 * decrement the counts associated with the old frags
1760 		 */
1761 		blk = blkmap(fs, blksfree, bbase);
1762 		ffs_fragacct(fs, blk, cgp->cg_frsum, -1);
1763 		/*
1764 		 * deallocate the fragment
1765 		 */
1766 		frags = numfrags(fs, size);
1767 		for (i = 0; i < frags; i++) {
1768 			if (isset(blksfree, cgbno + i)) {
1769 				printf("dev = %s, block = %jd, fs = %s\n",
1770 				    devtoname(dev), (intmax_t)(bno + i),
1771 				    fs->fs_fsmnt);
1772 				panic("ffs_blkfree: freeing free frag");
1773 			}
1774 			setbit(blksfree, cgbno + i);
1775 		}
1776 		cgp->cg_cs.cs_nffree += i;
1777 		fs->fs_cstotal.cs_nffree += i;
1778 		fs->fs_cs(fs, cg).cs_nffree += i;
1779 		/*
1780 		 * add back in counts associated with the new frags
1781 		 */
1782 		blk = blkmap(fs, blksfree, bbase);
1783 		ffs_fragacct(fs, blk, cgp->cg_frsum, 1);
1784 		/*
1785 		 * if a complete block has been reassembled, account for it
1786 		 */
1787 		fragno = fragstoblks(fs, bbase);
1788 		if (ffs_isblock(fs, blksfree, fragno)) {
1789 			cgp->cg_cs.cs_nffree -= fs->fs_frag;
1790 			fs->fs_cstotal.cs_nffree -= fs->fs_frag;
1791 			fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag;
1792 			ffs_clusteracct(fs, cgp, fragno, 1);
1793 			cgp->cg_cs.cs_nbfree++;
1794 			fs->fs_cstotal.cs_nbfree++;
1795 			fs->fs_cs(fs, cg).cs_nbfree++;
1796 		}
1797 	}
1798 	fs->fs_fmod = 1;
1799 	if (fs->fs_active != 0)
1800 		atomic_clear_int(&ACTIVECGNUM(fs, cg), ACTIVECGOFF(cg));
1801 	bdwrite(bp);
1802 }
1803 
1804 #ifdef DIAGNOSTIC
1805 /*
1806  * Verify allocation of a block or fragment. Returns true if block or
1807  * fragment is allocated, false if it is free.
1808  */
1809 static int
1810 ffs_checkblk(ip, bno, size)
1811 	struct inode *ip;
1812 	ufs2_daddr_t bno;
1813 	long size;
1814 {
1815 	struct fs *fs;
1816 	struct cg *cgp;
1817 	struct buf *bp;
1818 	ufs1_daddr_t cgbno;
1819 	int i, error, frags, free;
1820 	u_int8_t *blksfree;
1821 
1822 	fs = ip->i_fs;
1823 	if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0) {
1824 		printf("bsize = %ld, size = %ld, fs = %s\n",
1825 		    (long)fs->fs_bsize, size, fs->fs_fsmnt);
1826 		panic("ffs_checkblk: bad size");
1827 	}
1828 	if ((u_int)bno >= fs->fs_size)
1829 		panic("ffs_checkblk: bad block %jd", (intmax_t)bno);
1830 	error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, dtog(fs, bno))),
1831 		(int)fs->fs_cgsize, NOCRED, &bp);
1832 	if (error)
1833 		panic("ffs_checkblk: cg bread failed");
1834 	cgp = (struct cg *)bp->b_data;
1835 	if (!cg_chkmagic(cgp))
1836 		panic("ffs_checkblk: cg magic mismatch");
1837 	bp->b_xflags |= BX_BKGRDWRITE;
1838 	blksfree = cg_blksfree(cgp);
1839 	cgbno = dtogd(fs, bno);
1840 	if (size == fs->fs_bsize) {
1841 		free = ffs_isblock(fs, blksfree, fragstoblks(fs, cgbno));
1842 	} else {
1843 		frags = numfrags(fs, size);
1844 		for (free = 0, i = 0; i < frags; i++)
1845 			if (isset(blksfree, cgbno + i))
1846 				free++;
1847 		if (free != 0 && free != frags)
1848 			panic("ffs_checkblk: partially free fragment");
1849 	}
1850 	brelse(bp);
1851 	return (!free);
1852 }
1853 #endif /* DIAGNOSTIC */
1854 
1855 /*
1856  * Free an inode.
1857  */
1858 int
1859 ffs_vfree(pvp, ino, mode)
1860 	struct vnode *pvp;
1861 	ino_t ino;
1862 	int mode;
1863 {
1864 	if (DOINGSOFTDEP(pvp)) {
1865 		softdep_freefile(pvp, ino, mode);
1866 		return (0);
1867 	}
1868 	return (ffs_freefile(VTOI(pvp)->i_fs, VTOI(pvp)->i_devvp, ino, mode));
1869 }
1870 
1871 /*
1872  * Do the actual free operation.
1873  * The specified inode is placed back in the free map.
1874  */
1875 int
1876 ffs_freefile(fs, devvp, ino, mode)
1877 	struct fs *fs;
1878 	struct vnode *devvp;
1879 	ino_t ino;
1880 	int mode;
1881 {
1882 	struct cg *cgp;
1883 	struct buf *bp;
1884 	ufs2_daddr_t cgbno;
1885 	int error, cg;
1886 	u_int8_t *inosused;
1887 	dev_t dev;
1888 
1889 	cg = ino_to_cg(fs, ino);
1890 	if (devvp->v_type != VCHR) {
1891 		/* devvp is a snapshot */
1892 		dev = VTOI(devvp)->i_devvp->v_rdev;
1893 		cgbno = fragstoblks(fs, cgtod(fs, cg));
1894 	} else {
1895 		/* devvp is a normal disk device */
1896 		dev = devvp->v_rdev;
1897 		cgbno = fsbtodb(fs, cgtod(fs, cg));
1898 	}
1899 	if ((u_int)ino >= fs->fs_ipg * fs->fs_ncg)
1900 		panic("ffs_freefile: range: dev = %s, ino = %lu, fs = %s",
1901 		    devtoname(dev), (u_long)ino, fs->fs_fsmnt);
1902 	if ((error = bread(devvp, cgbno, (int)fs->fs_cgsize, NOCRED, &bp))) {
1903 		brelse(bp);
1904 		return (error);
1905 	}
1906 	cgp = (struct cg *)bp->b_data;
1907 	if (!cg_chkmagic(cgp)) {
1908 		brelse(bp);
1909 		return (0);
1910 	}
1911 	bp->b_xflags |= BX_BKGRDWRITE;
1912 	cgp->cg_old_time = cgp->cg_time = time_second;
1913 	inosused = cg_inosused(cgp);
1914 	ino %= fs->fs_ipg;
1915 	if (isclr(inosused, ino)) {
1916 		printf("dev = %s, ino = %lu, fs = %s\n", devtoname(dev),
1917 		    (u_long)ino + cg * fs->fs_ipg, fs->fs_fsmnt);
1918 		if (fs->fs_ronly == 0)
1919 			panic("ffs_freefile: freeing free inode");
1920 	}
1921 	clrbit(inosused, ino);
1922 	if (ino < cgp->cg_irotor)
1923 		cgp->cg_irotor = ino;
1924 	cgp->cg_cs.cs_nifree++;
1925 	fs->fs_cstotal.cs_nifree++;
1926 	fs->fs_cs(fs, cg).cs_nifree++;
1927 	if ((mode & IFMT) == IFDIR) {
1928 		cgp->cg_cs.cs_ndir--;
1929 		fs->fs_cstotal.cs_ndir--;
1930 		fs->fs_cs(fs, cg).cs_ndir--;
1931 	}
1932 	fs->fs_fmod = 1;
1933 	if (fs->fs_active != 0)
1934 		atomic_clear_int(&ACTIVECGNUM(fs, cg), ACTIVECGOFF(cg));
1935 	bdwrite(bp);
1936 	return (0);
1937 }
1938 
1939 /*
1940  * Find a block of the specified size in the specified cylinder group.
1941  *
1942  * It is a panic if a request is made to find a block if none are
1943  * available.
1944  */
1945 static ufs1_daddr_t
1946 ffs_mapsearch(fs, cgp, bpref, allocsiz)
1947 	struct fs *fs;
1948 	struct cg *cgp;
1949 	ufs2_daddr_t bpref;
1950 	int allocsiz;
1951 {
1952 	ufs1_daddr_t bno;
1953 	int start, len, loc, i;
1954 	int blk, field, subfield, pos;
1955 	u_int8_t *blksfree;
1956 
1957 	/*
1958 	 * find the fragment by searching through the free block
1959 	 * map for an appropriate bit pattern
1960 	 */
1961 	if (bpref)
1962 		start = dtogd(fs, bpref) / NBBY;
1963 	else
1964 		start = cgp->cg_frotor / NBBY;
1965 	blksfree = cg_blksfree(cgp);
1966 	len = howmany(fs->fs_fpg, NBBY) - start;
1967 	loc = scanc((u_int)len, (u_char *)&blksfree[start],
1968 		(u_char *)fragtbl[fs->fs_frag],
1969 		(u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
1970 	if (loc == 0) {
1971 		len = start + 1;
1972 		start = 0;
1973 		loc = scanc((u_int)len, (u_char *)&blksfree[0],
1974 			(u_char *)fragtbl[fs->fs_frag],
1975 			(u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
1976 		if (loc == 0) {
1977 			printf("start = %d, len = %d, fs = %s\n",
1978 			    start, len, fs->fs_fsmnt);
1979 			panic("ffs_alloccg: map corrupted");
1980 			/* NOTREACHED */
1981 		}
1982 	}
1983 	bno = (start + len - loc) * NBBY;
1984 	cgp->cg_frotor = bno;
1985 	/*
1986 	 * found the byte in the map
1987 	 * sift through the bits to find the selected frag
1988 	 */
1989 	for (i = bno + NBBY; bno < i; bno += fs->fs_frag) {
1990 		blk = blkmap(fs, blksfree, bno);
1991 		blk <<= 1;
1992 		field = around[allocsiz];
1993 		subfield = inside[allocsiz];
1994 		for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) {
1995 			if ((blk & field) == subfield)
1996 				return (bno + pos);
1997 			field <<= 1;
1998 			subfield <<= 1;
1999 		}
2000 	}
2001 	printf("bno = %lu, fs = %s\n", (u_long)bno, fs->fs_fsmnt);
2002 	panic("ffs_alloccg: block not in map");
2003 	return (-1);
2004 }
2005 
2006 /*
2007  * Update the cluster map because of an allocation or free.
2008  *
2009  * Cnt == 1 means free; cnt == -1 means allocating.
2010  */
2011 void
2012 ffs_clusteracct(fs, cgp, blkno, cnt)
2013 	struct fs *fs;
2014 	struct cg *cgp;
2015 	ufs1_daddr_t blkno;
2016 	int cnt;
2017 {
2018 	int32_t *sump;
2019 	int32_t *lp;
2020 	u_char *freemapp, *mapp;
2021 	int i, start, end, forw, back, map, bit;
2022 
2023 	if (fs->fs_contigsumsize <= 0)
2024 		return;
2025 	freemapp = cg_clustersfree(cgp);
2026 	sump = cg_clustersum(cgp);
2027 	/*
2028 	 * Allocate or clear the actual block.
2029 	 */
2030 	if (cnt > 0)
2031 		setbit(freemapp, blkno);
2032 	else
2033 		clrbit(freemapp, blkno);
2034 	/*
2035 	 * Find the size of the cluster going forward.
2036 	 */
2037 	start = blkno + 1;
2038 	end = start + fs->fs_contigsumsize;
2039 	if (end >= cgp->cg_nclusterblks)
2040 		end = cgp->cg_nclusterblks;
2041 	mapp = &freemapp[start / NBBY];
2042 	map = *mapp++;
2043 	bit = 1 << (start % NBBY);
2044 	for (i = start; i < end; i++) {
2045 		if ((map & bit) == 0)
2046 			break;
2047 		if ((i & (NBBY - 1)) != (NBBY - 1)) {
2048 			bit <<= 1;
2049 		} else {
2050 			map = *mapp++;
2051 			bit = 1;
2052 		}
2053 	}
2054 	forw = i - start;
2055 	/*
2056 	 * Find the size of the cluster going backward.
2057 	 */
2058 	start = blkno - 1;
2059 	end = start - fs->fs_contigsumsize;
2060 	if (end < 0)
2061 		end = -1;
2062 	mapp = &freemapp[start / NBBY];
2063 	map = *mapp--;
2064 	bit = 1 << (start % NBBY);
2065 	for (i = start; i > end; i--) {
2066 		if ((map & bit) == 0)
2067 			break;
2068 		if ((i & (NBBY - 1)) != 0) {
2069 			bit >>= 1;
2070 		} else {
2071 			map = *mapp--;
2072 			bit = 1 << (NBBY - 1);
2073 		}
2074 	}
2075 	back = start - i;
2076 	/*
2077 	 * Account for old cluster and the possibly new forward and
2078 	 * back clusters.
2079 	 */
2080 	i = back + forw + 1;
2081 	if (i > fs->fs_contigsumsize)
2082 		i = fs->fs_contigsumsize;
2083 	sump[i] += cnt;
2084 	if (back > 0)
2085 		sump[back] -= cnt;
2086 	if (forw > 0)
2087 		sump[forw] -= cnt;
2088 	/*
2089 	 * Update cluster summary information.
2090 	 */
2091 	lp = &sump[fs->fs_contigsumsize];
2092 	for (i = fs->fs_contigsumsize; i > 0; i--)
2093 		if (*lp-- > 0)
2094 			break;
2095 	fs->fs_maxcluster[cgp->cg_cgx] = i;
2096 }
2097 
2098 /*
2099  * Fserr prints the name of a filesystem with an error diagnostic.
2100  *
2101  * The form of the error message is:
2102  *	fs: error message
2103  */
2104 static void
2105 ffs_fserr(fs, inum, cp)
2106 	struct fs *fs;
2107 	ino_t inum;
2108 	char *cp;
2109 {
2110 	struct proc *p = curproc;	/* XXX */
2111 
2112 	log(LOG_ERR, "pid %d (%s), uid %d inumber %d on %s: %s\n",
2113 	    p ? p->p_pid : -1, p ? p->p_comm : "-",
2114 	    p ? p->p_ucred->cr_uid : 0, inum, fs->fs_fsmnt, cp);
2115 }
2116 
2117 /*
2118  * This function provides the capability for the fsck program to
2119  * update an active filesystem. Six operations are provided:
2120  *
2121  * adjrefcnt(inode, amt) - adjusts the reference count on the
2122  *	specified inode by the specified amount. Under normal
2123  *	operation the count should always go down. Decrementing
2124  *	the count to zero will cause the inode to be freed.
2125  * adjblkcnt(inode, amt) - adjust the number of blocks used to
2126  *	by the specifed amount.
2127  * freedirs(inode, count) - directory inodes [inode..inode + count - 1]
2128  *	are marked as free. Inodes should never have to be marked
2129  *	as in use.
2130  * freefiles(inode, count) - file inodes [inode..inode + count - 1]
2131  *	are marked as free. Inodes should never have to be marked
2132  *	as in use.
2133  * freeblks(blockno, size) - blocks [blockno..blockno + size - 1]
2134  *	are marked as free. Blocks should never have to be marked
2135  *	as in use.
2136  * setflags(flags, set/clear) - the fs_flags field has the specified
2137  *	flags set (second parameter +1) or cleared (second parameter -1).
2138  */
2139 
2140 static int sysctl_ffs_fsck(SYSCTL_HANDLER_ARGS);
2141 
2142 SYSCTL_PROC(_vfs_ffs, FFS_ADJ_REFCNT, adjrefcnt, CTLFLAG_WR|CTLTYPE_STRUCT,
2143 	0, 0, sysctl_ffs_fsck, "S,fsck", "Adjust Inode Reference Count");
2144 
2145 SYSCTL_NODE(_vfs_ffs, FFS_ADJ_BLKCNT, adjblkcnt, CTLFLAG_WR,
2146 	sysctl_ffs_fsck, "Adjust Inode Used Blocks Count");
2147 
2148 SYSCTL_NODE(_vfs_ffs, FFS_DIR_FREE, freedirs, CTLFLAG_WR,
2149 	sysctl_ffs_fsck, "Free Range of Directory Inodes");
2150 
2151 SYSCTL_NODE(_vfs_ffs, FFS_FILE_FREE, freefiles, CTLFLAG_WR,
2152 	sysctl_ffs_fsck, "Free Range of File Inodes");
2153 
2154 SYSCTL_NODE(_vfs_ffs, FFS_BLK_FREE, freeblks, CTLFLAG_WR,
2155 	sysctl_ffs_fsck, "Free Range of Blocks");
2156 
2157 SYSCTL_NODE(_vfs_ffs, FFS_SET_FLAGS, setflags, CTLFLAG_WR,
2158 	sysctl_ffs_fsck, "Change Filesystem Flags");
2159 
2160 #ifdef DEBUG
2161 static int fsckcmds = 0;
2162 SYSCTL_INT(_debug, OID_AUTO, fsckcmds, CTLFLAG_RW, &fsckcmds, 0, "");
2163 #endif /* DEBUG */
2164 
2165 static int
2166 sysctl_ffs_fsck(SYSCTL_HANDLER_ARGS)
2167 {
2168 	struct fsck_cmd cmd;
2169 	struct ufsmount *ump;
2170 	struct vnode *vp;
2171 	struct inode *ip;
2172 	struct mount *mp;
2173 	struct fs *fs;
2174 	ufs2_daddr_t blkno;
2175 	long blkcnt, blksize;
2176 	struct file *fp;
2177 	int filetype, error;
2178 
2179 	if (req->newlen > sizeof cmd)
2180 		return (EBADRPC);
2181 	if ((error = SYSCTL_IN(req, &cmd, sizeof cmd)) != 0)
2182 		return (error);
2183 	if (cmd.version != FFS_CMD_VERSION)
2184 		return (ERPCMISMATCH);
2185 	if ((error = getvnode(curproc->p_fd, cmd.handle, &fp)) != 0)
2186 		return (error);
2187 	vn_start_write(fp->f_data, &mp, V_WAIT);
2188 	if (mp == 0 || strncmp(mp->mnt_stat.f_fstypename, "ufs", MFSNAMELEN)) {
2189 		vn_finished_write(mp);
2190 		fdrop(fp, curthread);
2191 		return (EINVAL);
2192 	}
2193 	if (mp->mnt_flag & MNT_RDONLY) {
2194 		vn_finished_write(mp);
2195 		fdrop(fp, curthread);
2196 		return (EROFS);
2197 	}
2198 	ump = VFSTOUFS(mp);
2199 	fs = ump->um_fs;
2200 	filetype = IFREG;
2201 
2202 	switch (oidp->oid_number) {
2203 
2204 	case FFS_SET_FLAGS:
2205 #ifdef DEBUG
2206 		if (fsckcmds)
2207 			printf("%s: %s flags\n", mp->mnt_stat.f_mntonname,
2208 			    cmd.size > 0 ? "set" : "clear");
2209 #endif /* DEBUG */
2210 		if (cmd.size > 0)
2211 			fs->fs_flags |= (long)cmd.value;
2212 		else
2213 			fs->fs_flags &= ~(long)cmd.value;
2214 		break;
2215 
2216 	case FFS_ADJ_REFCNT:
2217 #ifdef DEBUG
2218 		if (fsckcmds) {
2219 			printf("%s: adjust inode %jd count by %jd\n",
2220 			    mp->mnt_stat.f_mntonname, (intmax_t)cmd.value,
2221 			    (intmax_t)cmd.size);
2222 		}
2223 #endif /* DEBUG */
2224 		if ((error = VFS_VGET(mp, (ino_t)cmd.value, LK_EXCLUSIVE, &vp)))
2225 			break;
2226 		ip = VTOI(vp);
2227 		ip->i_nlink += cmd.size;
2228 		DIP(ip, i_nlink) = ip->i_nlink;
2229 		ip->i_effnlink += cmd.size;
2230 		ip->i_flag |= IN_CHANGE;
2231 		if (DOINGSOFTDEP(vp))
2232 			softdep_change_linkcnt(ip);
2233 		vput(vp);
2234 		break;
2235 
2236 	case FFS_ADJ_BLKCNT:
2237 #ifdef DEBUG
2238 		if (fsckcmds) {
2239 			printf("%s: adjust inode %jd block count by %jd\n",
2240 			    mp->mnt_stat.f_mntonname, (intmax_t)cmd.value,
2241 			    (intmax_t)cmd.size);
2242 		}
2243 #endif /* DEBUG */
2244 		if ((error = VFS_VGET(mp, (ino_t)cmd.value, LK_EXCLUSIVE, &vp)))
2245 			break;
2246 		ip = VTOI(vp);
2247 		DIP(ip, i_blocks) += cmd.size;
2248 		ip->i_flag |= IN_CHANGE;
2249 		vput(vp);
2250 		break;
2251 
2252 	case FFS_DIR_FREE:
2253 		filetype = IFDIR;
2254 		/* fall through */
2255 
2256 	case FFS_FILE_FREE:
2257 #ifdef DEBUG
2258 		if (fsckcmds) {
2259 			if (cmd.size == 1)
2260 				printf("%s: free %s inode %d\n",
2261 				    mp->mnt_stat.f_mntonname,
2262 				    filetype == IFDIR ? "directory" : "file",
2263 				    (ino_t)cmd.value);
2264 			else
2265 				printf("%s: free %s inodes %d-%d\n",
2266 				    mp->mnt_stat.f_mntonname,
2267 				    filetype == IFDIR ? "directory" : "file",
2268 				    (ino_t)cmd.value,
2269 				    (ino_t)(cmd.value + cmd.size - 1));
2270 		}
2271 #endif /* DEBUG */
2272 		while (cmd.size > 0) {
2273 			if ((error = ffs_freefile(fs, ump->um_devvp, cmd.value,
2274 			    filetype)))
2275 				break;
2276 			cmd.size -= 1;
2277 			cmd.value += 1;
2278 		}
2279 		break;
2280 
2281 	case FFS_BLK_FREE:
2282 #ifdef DEBUG
2283 		if (fsckcmds) {
2284 			if (cmd.size == 1)
2285 				printf("%s: free block %jd\n",
2286 				    mp->mnt_stat.f_mntonname,
2287 				    (intmax_t)cmd.value);
2288 			else
2289 				printf("%s: free blocks %jd-%jd\n",
2290 				    mp->mnt_stat.f_mntonname,
2291 				    (intmax_t)cmd.value,
2292 				    (intmax_t)cmd.value + cmd.size - 1);
2293 		}
2294 #endif /* DEBUG */
2295 		blkno = cmd.value;
2296 		blkcnt = cmd.size;
2297 		blksize = fs->fs_frag - (blkno % fs->fs_frag);
2298 		while (blkcnt > 0) {
2299 			if (blksize > blkcnt)
2300 				blksize = blkcnt;
2301 			ffs_blkfree(fs, ump->um_devvp, blkno,
2302 			    blksize * fs->fs_fsize, ROOTINO);
2303 			blkno += blksize;
2304 			blkcnt -= blksize;
2305 			blksize = fs->fs_frag;
2306 		}
2307 		break;
2308 
2309 	default:
2310 #ifdef DEBUG
2311 		if (fsckcmds) {
2312 			printf("Invalid request %d from fsck\n",
2313 			    oidp->oid_number);
2314 		}
2315 #endif /* DEBUG */
2316 		error = EINVAL;
2317 		break;
2318 
2319 	}
2320 	fdrop(fp, curthread);
2321 	vn_finished_write(mp);
2322 	return (error);
2323 }
2324