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