xref: /freebsd/sys/ufs/ffs/ffs_alloc.c (revision b28624fde638caadd4a89f50c9b7e7da0f98c4d2)
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 DIAGNOSTIC
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 DIAGNOSTIC
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 /* DIAGNOSTIC */
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 DIAGNOSTIC
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 /* DIAGNOSTIC */
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 DIAGNOSTIC
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 DIAGNOSTIC
556 		if (start_ap[start_lvl-1].in_lbn == idp->in_lbn)
557 			panic("ffs_reallocblk: start == end");
558 #endif
559 		ssize = len - (idp->in_off + 1);
560 		if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &ebp))
561 			goto fail;
562 		ebap = (ufs1_daddr_t *)ebp->b_data;
563 	}
564 	/*
565 	 * Find the preferred location for the cluster.
566 	 */
567 	UFS_LOCK(ump);
568 	pref = ffs_blkpref_ufs1(ip, start_lbn, soff, sbap);
569 	/*
570 	 * Search the block map looking for an allocation of the desired size.
571 	 */
572 	if ((newblk = ffs_hashalloc(ip, dtog(fs, pref), pref,
573 	    len, ffs_clusteralloc)) == 0) {
574 		UFS_UNLOCK(ump);
575 		goto fail;
576 	}
577 	/*
578 	 * We have found a new contiguous block.
579 	 *
580 	 * First we have to replace the old block pointers with the new
581 	 * block pointers in the inode and indirect blocks associated
582 	 * with the file.
583 	 */
584 #ifdef DEBUG
585 	if (prtrealloc)
586 		printf("realloc: ino %d, lbns %jd-%jd\n\told:", ip->i_number,
587 		    (intmax_t)start_lbn, (intmax_t)end_lbn);
588 #endif
589 	blkno = newblk;
590 	for (bap = &sbap[soff], i = 0; i < len; i++, blkno += fs->fs_frag) {
591 		if (i == ssize) {
592 			bap = ebap;
593 			soff = -i;
594 		}
595 #ifdef DIAGNOSTIC
596 		if (!ffs_checkblk(ip,
597 		   dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
598 			panic("ffs_reallocblks: unallocated block 2");
599 		if (dbtofsb(fs, buflist->bs_children[i]->b_blkno) != *bap)
600 			panic("ffs_reallocblks: alloc mismatch");
601 #endif
602 #ifdef DEBUG
603 		if (prtrealloc)
604 			printf(" %d,", *bap);
605 #endif
606 		if (DOINGSOFTDEP(vp)) {
607 			if (sbap == &ip->i_din1->di_db[0] && i < ssize)
608 				softdep_setup_allocdirect(ip, start_lbn + i,
609 				    blkno, *bap, fs->fs_bsize, fs->fs_bsize,
610 				    buflist->bs_children[i]);
611 			else
612 				softdep_setup_allocindir_page(ip, start_lbn + i,
613 				    i < ssize ? sbp : ebp, soff + i, blkno,
614 				    *bap, buflist->bs_children[i]);
615 		}
616 		*bap++ = blkno;
617 	}
618 	/*
619 	 * Next we must write out the modified inode and indirect blocks.
620 	 * For strict correctness, the writes should be synchronous since
621 	 * the old block values may have been written to disk. In practise
622 	 * they are almost never written, but if we are concerned about
623 	 * strict correctness, the `doasyncfree' flag should be set to zero.
624 	 *
625 	 * The test on `doasyncfree' should be changed to test a flag
626 	 * that shows whether the associated buffers and inodes have
627 	 * been written. The flag should be set when the cluster is
628 	 * started and cleared whenever the buffer or inode is flushed.
629 	 * We can then check below to see if it is set, and do the
630 	 * synchronous write only when it has been cleared.
631 	 */
632 	if (sbap != &ip->i_din1->di_db[0]) {
633 		if (doasyncfree)
634 			bdwrite(sbp);
635 		else
636 			bwrite(sbp);
637 	} else {
638 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
639 		if (!doasyncfree)
640 			ffs_update(vp, 1);
641 	}
642 	if (ssize < len) {
643 		if (doasyncfree)
644 			bdwrite(ebp);
645 		else
646 			bwrite(ebp);
647 	}
648 	/*
649 	 * Last, free the old blocks and assign the new blocks to the buffers.
650 	 */
651 #ifdef DEBUG
652 	if (prtrealloc)
653 		printf("\n\tnew:");
654 #endif
655 	for (blkno = newblk, i = 0; i < len; i++, blkno += fs->fs_frag) {
656 		if (!DOINGSOFTDEP(vp))
657 			ffs_blkfree(ump, fs, ip->i_devvp,
658 			    dbtofsb(fs, buflist->bs_children[i]->b_blkno),
659 			    fs->fs_bsize, ip->i_number);
660 		buflist->bs_children[i]->b_blkno = fsbtodb(fs, blkno);
661 #ifdef DIAGNOSTIC
662 		if (!ffs_checkblk(ip,
663 		   dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
664 			panic("ffs_reallocblks: unallocated block 3");
665 #endif
666 #ifdef DEBUG
667 		if (prtrealloc)
668 			printf(" %d,", blkno);
669 #endif
670 	}
671 #ifdef DEBUG
672 	if (prtrealloc) {
673 		prtrealloc--;
674 		printf("\n");
675 	}
676 #endif
677 	return (0);
678 
679 fail:
680 	if (ssize < len)
681 		brelse(ebp);
682 	if (sbap != &ip->i_din1->di_db[0])
683 		brelse(sbp);
684 	return (ENOSPC);
685 }
686 
687 static int
688 ffs_reallocblks_ufs2(ap)
689 	struct vop_reallocblks_args /* {
690 		struct vnode *a_vp;
691 		struct cluster_save *a_buflist;
692 	} */ *ap;
693 {
694 	struct fs *fs;
695 	struct inode *ip;
696 	struct vnode *vp;
697 	struct buf *sbp, *ebp;
698 	ufs2_daddr_t *bap, *sbap, *ebap = 0;
699 	struct cluster_save *buflist;
700 	struct ufsmount *ump;
701 	ufs_lbn_t start_lbn, end_lbn;
702 	ufs2_daddr_t soff, newblk, blkno, pref;
703 	struct indir start_ap[NIADDR + 1], end_ap[NIADDR + 1], *idp;
704 	int i, len, start_lvl, end_lvl, ssize;
705 
706 	vp = ap->a_vp;
707 	ip = VTOI(vp);
708 	fs = ip->i_fs;
709 	ump = ip->i_ump;
710 	if (fs->fs_contigsumsize <= 0)
711 		return (ENOSPC);
712 	buflist = ap->a_buflist;
713 	len = buflist->bs_nchildren;
714 	start_lbn = buflist->bs_children[0]->b_lblkno;
715 	end_lbn = start_lbn + len - 1;
716 #ifdef DIAGNOSTIC
717 	for (i = 0; i < len; i++)
718 		if (!ffs_checkblk(ip,
719 		   dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
720 			panic("ffs_reallocblks: unallocated block 1");
721 	for (i = 1; i < len; i++)
722 		if (buflist->bs_children[i]->b_lblkno != start_lbn + i)
723 			panic("ffs_reallocblks: non-logical cluster");
724 	blkno = buflist->bs_children[0]->b_blkno;
725 	ssize = fsbtodb(fs, fs->fs_frag);
726 	for (i = 1; i < len - 1; i++)
727 		if (buflist->bs_children[i]->b_blkno != blkno + (i * ssize))
728 			panic("ffs_reallocblks: non-physical cluster %d", i);
729 #endif
730 	/*
731 	 * If the latest allocation is in a new cylinder group, assume that
732 	 * the filesystem has decided to move and do not force it back to
733 	 * the previous cylinder group.
734 	 */
735 	if (dtog(fs, dbtofsb(fs, buflist->bs_children[0]->b_blkno)) !=
736 	    dtog(fs, dbtofsb(fs, buflist->bs_children[len - 1]->b_blkno)))
737 		return (ENOSPC);
738 	if (ufs_getlbns(vp, start_lbn, start_ap, &start_lvl) ||
739 	    ufs_getlbns(vp, end_lbn, end_ap, &end_lvl))
740 		return (ENOSPC);
741 	/*
742 	 * Get the starting offset and block map for the first block.
743 	 */
744 	if (start_lvl == 0) {
745 		sbap = &ip->i_din2->di_db[0];
746 		soff = start_lbn;
747 	} else {
748 		idp = &start_ap[start_lvl - 1];
749 		if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &sbp)) {
750 			brelse(sbp);
751 			return (ENOSPC);
752 		}
753 		sbap = (ufs2_daddr_t *)sbp->b_data;
754 		soff = idp->in_off;
755 	}
756 	/*
757 	 * If the block range spans two block maps, get the second map.
758 	 */
759 	if (end_lvl == 0 || (idp = &end_ap[end_lvl - 1])->in_off + 1 >= len) {
760 		ssize = len;
761 	} else {
762 #ifdef DIAGNOSTIC
763 		if (start_ap[start_lvl-1].in_lbn == idp->in_lbn)
764 			panic("ffs_reallocblk: start == end");
765 #endif
766 		ssize = len - (idp->in_off + 1);
767 		if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &ebp))
768 			goto fail;
769 		ebap = (ufs2_daddr_t *)ebp->b_data;
770 	}
771 	/*
772 	 * Find the preferred location for the cluster.
773 	 */
774 	UFS_LOCK(ump);
775 	pref = ffs_blkpref_ufs2(ip, start_lbn, soff, sbap);
776 	/*
777 	 * Search the block map looking for an allocation of the desired size.
778 	 */
779 	if ((newblk = ffs_hashalloc(ip, dtog(fs, pref), pref,
780 	    len, ffs_clusteralloc)) == 0) {
781 		UFS_UNLOCK(ump);
782 		goto fail;
783 	}
784 	/*
785 	 * We have found a new contiguous block.
786 	 *
787 	 * First we have to replace the old block pointers with the new
788 	 * block pointers in the inode and indirect blocks associated
789 	 * with the file.
790 	 */
791 #ifdef DEBUG
792 	if (prtrealloc)
793 		printf("realloc: ino %d, lbns %jd-%jd\n\told:", ip->i_number,
794 		    (intmax_t)start_lbn, (intmax_t)end_lbn);
795 #endif
796 	blkno = newblk;
797 	for (bap = &sbap[soff], i = 0; i < len; i++, blkno += fs->fs_frag) {
798 		if (i == ssize) {
799 			bap = ebap;
800 			soff = -i;
801 		}
802 #ifdef DIAGNOSTIC
803 		if (!ffs_checkblk(ip,
804 		   dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
805 			panic("ffs_reallocblks: unallocated block 2");
806 		if (dbtofsb(fs, buflist->bs_children[i]->b_blkno) != *bap)
807 			panic("ffs_reallocblks: alloc mismatch");
808 #endif
809 #ifdef DEBUG
810 		if (prtrealloc)
811 			printf(" %jd,", (intmax_t)*bap);
812 #endif
813 		if (DOINGSOFTDEP(vp)) {
814 			if (sbap == &ip->i_din2->di_db[0] && i < ssize)
815 				softdep_setup_allocdirect(ip, start_lbn + i,
816 				    blkno, *bap, fs->fs_bsize, fs->fs_bsize,
817 				    buflist->bs_children[i]);
818 			else
819 				softdep_setup_allocindir_page(ip, start_lbn + i,
820 				    i < ssize ? sbp : ebp, soff + i, blkno,
821 				    *bap, buflist->bs_children[i]);
822 		}
823 		*bap++ = blkno;
824 	}
825 	/*
826 	 * Next we must write out the modified inode and indirect blocks.
827 	 * For strict correctness, the writes should be synchronous since
828 	 * the old block values may have been written to disk. In practise
829 	 * they are almost never written, but if we are concerned about
830 	 * strict correctness, the `doasyncfree' flag should be set to zero.
831 	 *
832 	 * The test on `doasyncfree' should be changed to test a flag
833 	 * that shows whether the associated buffers and inodes have
834 	 * been written. The flag should be set when the cluster is
835 	 * started and cleared whenever the buffer or inode is flushed.
836 	 * We can then check below to see if it is set, and do the
837 	 * synchronous write only when it has been cleared.
838 	 */
839 	if (sbap != &ip->i_din2->di_db[0]) {
840 		if (doasyncfree)
841 			bdwrite(sbp);
842 		else
843 			bwrite(sbp);
844 	} else {
845 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
846 		if (!doasyncfree)
847 			ffs_update(vp, 1);
848 	}
849 	if (ssize < len) {
850 		if (doasyncfree)
851 			bdwrite(ebp);
852 		else
853 			bwrite(ebp);
854 	}
855 	/*
856 	 * Last, free the old blocks and assign the new blocks to the buffers.
857 	 */
858 #ifdef DEBUG
859 	if (prtrealloc)
860 		printf("\n\tnew:");
861 #endif
862 	for (blkno = newblk, i = 0; i < len; i++, blkno += fs->fs_frag) {
863 		if (!DOINGSOFTDEP(vp))
864 			ffs_blkfree(ump, fs, ip->i_devvp,
865 			    dbtofsb(fs, buflist->bs_children[i]->b_blkno),
866 			    fs->fs_bsize, ip->i_number);
867 		buflist->bs_children[i]->b_blkno = fsbtodb(fs, blkno);
868 #ifdef DIAGNOSTIC
869 		if (!ffs_checkblk(ip,
870 		   dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
871 			panic("ffs_reallocblks: unallocated block 3");
872 #endif
873 #ifdef DEBUG
874 		if (prtrealloc)
875 			printf(" %jd,", (intmax_t)blkno);
876 #endif
877 	}
878 #ifdef DEBUG
879 	if (prtrealloc) {
880 		prtrealloc--;
881 		printf("\n");
882 	}
883 #endif
884 	return (0);
885 
886 fail:
887 	if (ssize < len)
888 		brelse(ebp);
889 	if (sbap != &ip->i_din2->di_db[0])
890 		brelse(sbp);
891 	return (ENOSPC);
892 }
893 
894 /*
895  * Allocate an inode in the filesystem.
896  *
897  * If allocating a directory, use ffs_dirpref to select the inode.
898  * If allocating in a directory, the following hierarchy is followed:
899  *   1) allocate the preferred inode.
900  *   2) allocate an inode in the same cylinder group.
901  *   3) quadradically rehash into other cylinder groups, until an
902  *      available inode is located.
903  * If no inode preference is given the following hierarchy is used
904  * to allocate an inode:
905  *   1) allocate an inode in cylinder group 0.
906  *   2) quadradically rehash into other cylinder groups, until an
907  *      available inode is located.
908  */
909 int
910 ffs_valloc(pvp, mode, cred, vpp)
911 	struct vnode *pvp;
912 	int mode;
913 	struct ucred *cred;
914 	struct vnode **vpp;
915 {
916 	struct inode *pip;
917 	struct fs *fs;
918 	struct inode *ip;
919 	struct timespec ts;
920 	struct ufsmount *ump;
921 	ino_t ino, ipref;
922 	int cg, error;
923 	static struct timeval lastfail;
924 	static int curfail;
925 
926 	*vpp = NULL;
927 	pip = VTOI(pvp);
928 	fs = pip->i_fs;
929 	ump = pip->i_ump;
930 
931 	UFS_LOCK(ump);
932 	if (fs->fs_cstotal.cs_nifree == 0)
933 		goto noinodes;
934 
935 	if ((mode & IFMT) == IFDIR)
936 		ipref = ffs_dirpref(pip);
937 	else
938 		ipref = pip->i_number;
939 	if (ipref >= fs->fs_ncg * fs->fs_ipg)
940 		ipref = 0;
941 	cg = ino_to_cg(fs, ipref);
942 	/*
943 	 * Track number of dirs created one after another
944 	 * in a same cg without intervening by files.
945 	 */
946 	if ((mode & IFMT) == IFDIR) {
947 		if (fs->fs_contigdirs[cg] < 255)
948 			fs->fs_contigdirs[cg]++;
949 	} else {
950 		if (fs->fs_contigdirs[cg] > 0)
951 			fs->fs_contigdirs[cg]--;
952 	}
953 	ino = (ino_t)ffs_hashalloc(pip, cg, ipref, mode,
954 					(allocfcn_t *)ffs_nodealloccg);
955 	if (ino == 0)
956 		goto noinodes;
957 	error = ffs_vget(pvp->v_mount, ino, LK_EXCLUSIVE, vpp);
958 	if (error) {
959 		ffs_vfree(pvp, ino, mode);
960 		return (error);
961 	}
962 	ip = VTOI(*vpp);
963 	if (ip->i_mode) {
964 		printf("mode = 0%o, inum = %lu, fs = %s\n",
965 		    ip->i_mode, (u_long)ip->i_number, fs->fs_fsmnt);
966 		panic("ffs_valloc: dup alloc");
967 	}
968 	if (DIP(ip, i_blocks) && (fs->fs_flags & FS_UNCLEAN) == 0) {  /* XXX */
969 		printf("free inode %s/%lu had %ld blocks\n",
970 		    fs->fs_fsmnt, (u_long)ino, (long)DIP(ip, i_blocks));
971 		DIP_SET(ip, i_blocks, 0);
972 	}
973 	ip->i_flags = 0;
974 	DIP_SET(ip, i_flags, 0);
975 	/*
976 	 * Set up a new generation number for this inode.
977 	 */
978 	if (ip->i_gen == 0 || ++ip->i_gen == 0)
979 		ip->i_gen = arc4random() / 2 + 1;
980 	DIP_SET(ip, i_gen, ip->i_gen);
981 	if (fs->fs_magic == FS_UFS2_MAGIC) {
982 		vfs_timestamp(&ts);
983 		ip->i_din2->di_birthtime = ts.tv_sec;
984 		ip->i_din2->di_birthnsec = ts.tv_nsec;
985 	}
986 	ip->i_flag = 0;
987 	vnode_destroy_vobject(*vpp);
988 	(*vpp)->v_type = VNON;
989 	if (fs->fs_magic == FS_UFS2_MAGIC)
990 		(*vpp)->v_op = &ffs_vnodeops2;
991 	else
992 		(*vpp)->v_op = &ffs_vnodeops1;
993 	return (0);
994 noinodes:
995 	UFS_UNLOCK(ump);
996 	if (ppsratecheck(&lastfail, &curfail, 1)) {
997 		ffs_fserr(fs, pip->i_number, "out of inodes");
998 		uprintf("\n%s: create/symlink failed, no inodes free\n",
999 		    fs->fs_fsmnt);
1000 	}
1001 	return (ENOSPC);
1002 }
1003 
1004 /*
1005  * Find a cylinder group to place a directory.
1006  *
1007  * The policy implemented by this algorithm is to allocate a
1008  * directory inode in the same cylinder group as its parent
1009  * directory, but also to reserve space for its files inodes
1010  * and data. Restrict the number of directories which may be
1011  * allocated one after another in the same cylinder group
1012  * without intervening allocation of files.
1013  *
1014  * If we allocate a first level directory then force allocation
1015  * in another cylinder group.
1016  */
1017 static ino_t
1018 ffs_dirpref(pip)
1019 	struct inode *pip;
1020 {
1021 	struct fs *fs;
1022 	int cg, prefcg, dirsize, cgsize;
1023 	int avgifree, avgbfree, avgndir, curdirsize;
1024 	int minifree, minbfree, maxndir;
1025 	int mincg, minndir;
1026 	int maxcontigdirs;
1027 
1028 	mtx_assert(UFS_MTX(pip->i_ump), MA_OWNED);
1029 	fs = pip->i_fs;
1030 
1031 	avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg;
1032 	avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
1033 	avgndir = fs->fs_cstotal.cs_ndir / fs->fs_ncg;
1034 
1035 	/*
1036 	 * Force allocation in another cg if creating a first level dir.
1037 	 */
1038 	ASSERT_VOP_LOCKED(ITOV(pip), "ffs_dirpref");
1039 	if (ITOV(pip)->v_vflag & VV_ROOT) {
1040 		prefcg = arc4random() % fs->fs_ncg;
1041 		mincg = prefcg;
1042 		minndir = fs->fs_ipg;
1043 		for (cg = prefcg; cg < fs->fs_ncg; cg++)
1044 			if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
1045 			    fs->fs_cs(fs, cg).cs_nifree >= avgifree &&
1046 			    fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
1047 				mincg = cg;
1048 				minndir = fs->fs_cs(fs, cg).cs_ndir;
1049 			}
1050 		for (cg = 0; cg < prefcg; cg++)
1051 			if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
1052 			    fs->fs_cs(fs, cg).cs_nifree >= avgifree &&
1053 			    fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
1054 				mincg = cg;
1055 				minndir = fs->fs_cs(fs, cg).cs_ndir;
1056 			}
1057 		return ((ino_t)(fs->fs_ipg * mincg));
1058 	}
1059 
1060 	/*
1061 	 * Count various limits which used for
1062 	 * optimal allocation of a directory inode.
1063 	 */
1064 	maxndir = min(avgndir + fs->fs_ipg / 16, fs->fs_ipg);
1065 	minifree = avgifree - avgifree / 4;
1066 	if (minifree < 1)
1067 		minifree = 1;
1068 	minbfree = avgbfree - avgbfree / 4;
1069 	if (minbfree < 1)
1070 		minbfree = 1;
1071 	cgsize = fs->fs_fsize * fs->fs_fpg;
1072 	dirsize = fs->fs_avgfilesize * fs->fs_avgfpdir;
1073 	curdirsize = avgndir ? (cgsize - avgbfree * fs->fs_bsize) / avgndir : 0;
1074 	if (dirsize < curdirsize)
1075 		dirsize = curdirsize;
1076 	if (dirsize <= 0)
1077 		maxcontigdirs = 0;		/* dirsize overflowed */
1078 	else
1079 		maxcontigdirs = min((avgbfree * fs->fs_bsize) / dirsize, 255);
1080 	if (fs->fs_avgfpdir > 0)
1081 		maxcontigdirs = min(maxcontigdirs,
1082 				    fs->fs_ipg / fs->fs_avgfpdir);
1083 	if (maxcontigdirs == 0)
1084 		maxcontigdirs = 1;
1085 
1086 	/*
1087 	 * Limit number of dirs in one cg and reserve space for
1088 	 * regular files, but only if we have no deficit in
1089 	 * inodes or space.
1090 	 */
1091 	prefcg = ino_to_cg(fs, pip->i_number);
1092 	for (cg = prefcg; cg < fs->fs_ncg; cg++)
1093 		if (fs->fs_cs(fs, cg).cs_ndir < maxndir &&
1094 		    fs->fs_cs(fs, cg).cs_nifree >= minifree &&
1095 	    	    fs->fs_cs(fs, cg).cs_nbfree >= minbfree) {
1096 			if (fs->fs_contigdirs[cg] < maxcontigdirs)
1097 				return ((ino_t)(fs->fs_ipg * cg));
1098 		}
1099 	for (cg = 0; cg < prefcg; cg++)
1100 		if (fs->fs_cs(fs, cg).cs_ndir < maxndir &&
1101 		    fs->fs_cs(fs, cg).cs_nifree >= minifree &&
1102 	    	    fs->fs_cs(fs, cg).cs_nbfree >= minbfree) {
1103 			if (fs->fs_contigdirs[cg] < maxcontigdirs)
1104 				return ((ino_t)(fs->fs_ipg * cg));
1105 		}
1106 	/*
1107 	 * This is a backstop when we have deficit in space.
1108 	 */
1109 	for (cg = prefcg; cg < fs->fs_ncg; cg++)
1110 		if (fs->fs_cs(fs, cg).cs_nifree >= avgifree)
1111 			return ((ino_t)(fs->fs_ipg * cg));
1112 	for (cg = 0; cg < prefcg; cg++)
1113 		if (fs->fs_cs(fs, cg).cs_nifree >= avgifree)
1114 			break;
1115 	return ((ino_t)(fs->fs_ipg * cg));
1116 }
1117 
1118 /*
1119  * Select the desired position for the next block in a file.  The file is
1120  * logically divided into sections. The first section is composed of the
1121  * direct blocks. Each additional section contains fs_maxbpg blocks.
1122  *
1123  * If no blocks have been allocated in the first section, the policy is to
1124  * request a block in the same cylinder group as the inode that describes
1125  * the file. If no blocks have been allocated in any other section, the
1126  * policy is to place the section in a cylinder group with a greater than
1127  * average number of free blocks.  An appropriate cylinder group is found
1128  * by using a rotor that sweeps the cylinder groups. When a new group of
1129  * blocks is needed, the sweep begins in the cylinder group following the
1130  * cylinder group from which the previous allocation was made. The sweep
1131  * continues until a cylinder group with greater than the average number
1132  * of free blocks is found. If the allocation is for the first block in an
1133  * indirect block, the information on the previous allocation is unavailable;
1134  * here a best guess is made based upon the logical block number being
1135  * allocated.
1136  *
1137  * If a section is already partially allocated, the policy is to
1138  * contiguously allocate fs_maxcontig blocks. The end of one of these
1139  * contiguous blocks and the beginning of the next is laid out
1140  * contiguously if possible.
1141  */
1142 ufs2_daddr_t
1143 ffs_blkpref_ufs1(ip, lbn, indx, bap)
1144 	struct inode *ip;
1145 	ufs_lbn_t lbn;
1146 	int indx;
1147 	ufs1_daddr_t *bap;
1148 {
1149 	struct fs *fs;
1150 	int cg;
1151 	int avgbfree, startcg;
1152 
1153 	mtx_assert(UFS_MTX(ip->i_ump), MA_OWNED);
1154 	fs = ip->i_fs;
1155 	if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
1156 		if (lbn < NDADDR + NINDIR(fs)) {
1157 			cg = ino_to_cg(fs, ip->i_number);
1158 			return (cgbase(fs, cg) + fs->fs_frag);
1159 		}
1160 		/*
1161 		 * Find a cylinder with greater than average number of
1162 		 * unused data blocks.
1163 		 */
1164 		if (indx == 0 || bap[indx - 1] == 0)
1165 			startcg =
1166 			    ino_to_cg(fs, ip->i_number) + lbn / fs->fs_maxbpg;
1167 		else
1168 			startcg = dtog(fs, bap[indx - 1]) + 1;
1169 		startcg %= fs->fs_ncg;
1170 		avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
1171 		for (cg = startcg; cg < fs->fs_ncg; cg++)
1172 			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
1173 				fs->fs_cgrotor = cg;
1174 				return (cgbase(fs, cg) + fs->fs_frag);
1175 			}
1176 		for (cg = 0; cg <= startcg; cg++)
1177 			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
1178 				fs->fs_cgrotor = cg;
1179 				return (cgbase(fs, cg) + fs->fs_frag);
1180 			}
1181 		return (0);
1182 	}
1183 	/*
1184 	 * We just always try to lay things out contiguously.
1185 	 */
1186 	return (bap[indx - 1] + fs->fs_frag);
1187 }
1188 
1189 /*
1190  * Same as above, but for UFS2
1191  */
1192 ufs2_daddr_t
1193 ffs_blkpref_ufs2(ip, lbn, indx, bap)
1194 	struct inode *ip;
1195 	ufs_lbn_t lbn;
1196 	int indx;
1197 	ufs2_daddr_t *bap;
1198 {
1199 	struct fs *fs;
1200 	int cg;
1201 	int avgbfree, startcg;
1202 
1203 	mtx_assert(UFS_MTX(ip->i_ump), MA_OWNED);
1204 	fs = ip->i_fs;
1205 	if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
1206 		if (lbn < NDADDR + NINDIR(fs)) {
1207 			cg = ino_to_cg(fs, ip->i_number);
1208 			return (cgbase(fs, cg) + fs->fs_frag);
1209 		}
1210 		/*
1211 		 * Find a cylinder with greater than average number of
1212 		 * unused data blocks.
1213 		 */
1214 		if (indx == 0 || bap[indx - 1] == 0)
1215 			startcg =
1216 			    ino_to_cg(fs, ip->i_number) + lbn / fs->fs_maxbpg;
1217 		else
1218 			startcg = dtog(fs, bap[indx - 1]) + 1;
1219 		startcg %= fs->fs_ncg;
1220 		avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
1221 		for (cg = startcg; cg < fs->fs_ncg; cg++)
1222 			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
1223 				fs->fs_cgrotor = cg;
1224 				return (cgbase(fs, cg) + fs->fs_frag);
1225 			}
1226 		for (cg = 0; cg <= startcg; cg++)
1227 			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
1228 				fs->fs_cgrotor = cg;
1229 				return (cgbase(fs, cg) + fs->fs_frag);
1230 			}
1231 		return (0);
1232 	}
1233 	/*
1234 	 * We just always try to lay things out contiguously.
1235 	 */
1236 	return (bap[indx - 1] + fs->fs_frag);
1237 }
1238 
1239 /*
1240  * Implement the cylinder overflow algorithm.
1241  *
1242  * The policy implemented by this algorithm is:
1243  *   1) allocate the block in its requested cylinder group.
1244  *   2) quadradically rehash on the cylinder group number.
1245  *   3) brute force search for a free block.
1246  *
1247  * Must be called with the UFS lock held.  Will release the lock on success
1248  * and return with it held on failure.
1249  */
1250 /*VARARGS5*/
1251 static ufs2_daddr_t
1252 ffs_hashalloc(ip, cg, pref, size, allocator)
1253 	struct inode *ip;
1254 	int cg;
1255 	ufs2_daddr_t pref;
1256 	int size;	/* size for data blocks, mode for inodes */
1257 	allocfcn_t *allocator;
1258 {
1259 	struct fs *fs;
1260 	ufs2_daddr_t result;
1261 	int i, icg = cg;
1262 
1263 	mtx_assert(UFS_MTX(ip->i_ump), MA_OWNED);
1264 #ifdef DIAGNOSTIC
1265 	if (ITOV(ip)->v_mount->mnt_kern_flag & MNTK_SUSPENDED)
1266 		panic("ffs_hashalloc: allocation on suspended filesystem");
1267 #endif
1268 	fs = ip->i_fs;
1269 	/*
1270 	 * 1: preferred cylinder group
1271 	 */
1272 	result = (*allocator)(ip, cg, pref, size);
1273 	if (result)
1274 		return (result);
1275 	/*
1276 	 * 2: quadratic rehash
1277 	 */
1278 	for (i = 1; i < fs->fs_ncg; i *= 2) {
1279 		cg += i;
1280 		if (cg >= fs->fs_ncg)
1281 			cg -= fs->fs_ncg;
1282 		result = (*allocator)(ip, cg, 0, size);
1283 		if (result)
1284 			return (result);
1285 	}
1286 	/*
1287 	 * 3: brute force search
1288 	 * Note that we start at i == 2, since 0 was checked initially,
1289 	 * and 1 is always checked in the quadratic rehash.
1290 	 */
1291 	cg = (icg + 2) % fs->fs_ncg;
1292 	for (i = 2; i < fs->fs_ncg; i++) {
1293 		result = (*allocator)(ip, cg, 0, size);
1294 		if (result)
1295 			return (result);
1296 		cg++;
1297 		if (cg == fs->fs_ncg)
1298 			cg = 0;
1299 	}
1300 	return (0);
1301 }
1302 
1303 /*
1304  * Determine whether a fragment can be extended.
1305  *
1306  * Check to see if the necessary fragments are available, and
1307  * if they are, allocate them.
1308  */
1309 static ufs2_daddr_t
1310 ffs_fragextend(ip, cg, bprev, osize, nsize)
1311 	struct inode *ip;
1312 	int cg;
1313 	ufs2_daddr_t bprev;
1314 	int osize, nsize;
1315 {
1316 	struct fs *fs;
1317 	struct cg *cgp;
1318 	struct buf *bp;
1319 	struct ufsmount *ump;
1320 	int nffree;
1321 	long bno;
1322 	int frags, bbase;
1323 	int i, error;
1324 	u_int8_t *blksfree;
1325 
1326 	ump = ip->i_ump;
1327 	fs = ip->i_fs;
1328 	if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize))
1329 		return (0);
1330 	frags = numfrags(fs, nsize);
1331 	bbase = fragnum(fs, bprev);
1332 	if (bbase > fragnum(fs, (bprev + frags - 1))) {
1333 		/* cannot extend across a block boundary */
1334 		return (0);
1335 	}
1336 	UFS_UNLOCK(ump);
1337 	error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
1338 		(int)fs->fs_cgsize, NOCRED, &bp);
1339 	if (error)
1340 		goto fail;
1341 	cgp = (struct cg *)bp->b_data;
1342 	if (!cg_chkmagic(cgp))
1343 		goto fail;
1344 	bp->b_xflags |= BX_BKGRDWRITE;
1345 	cgp->cg_old_time = cgp->cg_time = time_second;
1346 	bno = dtogd(fs, bprev);
1347 	blksfree = cg_blksfree(cgp);
1348 	for (i = numfrags(fs, osize); i < frags; i++)
1349 		if (isclr(blksfree, bno + i))
1350 			goto fail;
1351 	/*
1352 	 * the current fragment can be extended
1353 	 * deduct the count on fragment being extended into
1354 	 * increase the count on the remaining fragment (if any)
1355 	 * allocate the extended piece
1356 	 */
1357 	for (i = frags; i < fs->fs_frag - bbase; i++)
1358 		if (isclr(blksfree, bno + i))
1359 			break;
1360 	cgp->cg_frsum[i - numfrags(fs, osize)]--;
1361 	if (i != frags)
1362 		cgp->cg_frsum[i - frags]++;
1363 	for (i = numfrags(fs, osize), nffree = 0; i < frags; i++) {
1364 		clrbit(blksfree, bno + i);
1365 		cgp->cg_cs.cs_nffree--;
1366 		nffree++;
1367 	}
1368 	UFS_LOCK(ump);
1369 	fs->fs_cstotal.cs_nffree -= nffree;
1370 	fs->fs_cs(fs, cg).cs_nffree -= nffree;
1371 	fs->fs_fmod = 1;
1372 	ACTIVECLEAR(fs, cg);
1373 	UFS_UNLOCK(ump);
1374 	if (DOINGSOFTDEP(ITOV(ip)))
1375 		softdep_setup_blkmapdep(bp, UFSTOVFS(ump), bprev);
1376 	bdwrite(bp);
1377 	return (bprev);
1378 
1379 fail:
1380 	brelse(bp);
1381 	UFS_LOCK(ump);
1382 	return (0);
1383 
1384 }
1385 
1386 /*
1387  * Determine whether a block can be allocated.
1388  *
1389  * Check to see if a block of the appropriate size is available,
1390  * and if it is, allocate it.
1391  */
1392 static ufs2_daddr_t
1393 ffs_alloccg(ip, cg, bpref, size)
1394 	struct inode *ip;
1395 	int cg;
1396 	ufs2_daddr_t bpref;
1397 	int size;
1398 {
1399 	struct fs *fs;
1400 	struct cg *cgp;
1401 	struct buf *bp;
1402 	struct ufsmount *ump;
1403 	ufs1_daddr_t bno;
1404 	ufs2_daddr_t blkno;
1405 	int i, allocsiz, error, frags;
1406 	u_int8_t *blksfree;
1407 
1408 	ump = ip->i_ump;
1409 	fs = ip->i_fs;
1410 	if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize)
1411 		return (0);
1412 	UFS_UNLOCK(ump);
1413 	error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
1414 		(int)fs->fs_cgsize, NOCRED, &bp);
1415 	if (error)
1416 		goto fail;
1417 	cgp = (struct cg *)bp->b_data;
1418 	if (!cg_chkmagic(cgp) ||
1419 	    (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize))
1420 		goto fail;
1421 	bp->b_xflags |= BX_BKGRDWRITE;
1422 	cgp->cg_old_time = cgp->cg_time = time_second;
1423 	if (size == fs->fs_bsize) {
1424 		UFS_LOCK(ump);
1425 		blkno = ffs_alloccgblk(ip, bp, bpref);
1426 		ACTIVECLEAR(fs, cg);
1427 		UFS_UNLOCK(ump);
1428 		bdwrite(bp);
1429 		return (blkno);
1430 	}
1431 	/*
1432 	 * check to see if any fragments are already available
1433 	 * allocsiz is the size which will be allocated, hacking
1434 	 * it down to a smaller size if necessary
1435 	 */
1436 	blksfree = cg_blksfree(cgp);
1437 	frags = numfrags(fs, size);
1438 	for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++)
1439 		if (cgp->cg_frsum[allocsiz] != 0)
1440 			break;
1441 	if (allocsiz == fs->fs_frag) {
1442 		/*
1443 		 * no fragments were available, so a block will be
1444 		 * allocated, and hacked up
1445 		 */
1446 		if (cgp->cg_cs.cs_nbfree == 0)
1447 			goto fail;
1448 		UFS_LOCK(ump);
1449 		blkno = ffs_alloccgblk(ip, bp, bpref);
1450 		bno = dtogd(fs, blkno);
1451 		for (i = frags; i < fs->fs_frag; i++)
1452 			setbit(blksfree, bno + i);
1453 		i = fs->fs_frag - frags;
1454 		cgp->cg_cs.cs_nffree += i;
1455 		fs->fs_cstotal.cs_nffree += i;
1456 		fs->fs_cs(fs, cg).cs_nffree += i;
1457 		fs->fs_fmod = 1;
1458 		cgp->cg_frsum[i]++;
1459 		ACTIVECLEAR(fs, cg);
1460 		UFS_UNLOCK(ump);
1461 		bdwrite(bp);
1462 		return (blkno);
1463 	}
1464 	bno = ffs_mapsearch(fs, cgp, bpref, allocsiz);
1465 	if (bno < 0)
1466 		goto fail;
1467 	for (i = 0; i < frags; i++)
1468 		clrbit(blksfree, bno + i);
1469 	cgp->cg_cs.cs_nffree -= frags;
1470 	cgp->cg_frsum[allocsiz]--;
1471 	if (frags != allocsiz)
1472 		cgp->cg_frsum[allocsiz - frags]++;
1473 	UFS_LOCK(ump);
1474 	fs->fs_cstotal.cs_nffree -= frags;
1475 	fs->fs_cs(fs, cg).cs_nffree -= frags;
1476 	fs->fs_fmod = 1;
1477 	blkno = cgbase(fs, cg) + bno;
1478 	ACTIVECLEAR(fs, cg);
1479 	UFS_UNLOCK(ump);
1480 	if (DOINGSOFTDEP(ITOV(ip)))
1481 		softdep_setup_blkmapdep(bp, UFSTOVFS(ump), blkno);
1482 	bdwrite(bp);
1483 	return (blkno);
1484 
1485 fail:
1486 	brelse(bp);
1487 	UFS_LOCK(ump);
1488 	return (0);
1489 }
1490 
1491 /*
1492  * Allocate a block in a cylinder group.
1493  *
1494  * This algorithm implements the following policy:
1495  *   1) allocate the requested block.
1496  *   2) allocate a rotationally optimal block in the same cylinder.
1497  *   3) allocate the next available block on the block rotor for the
1498  *      specified cylinder group.
1499  * Note that this routine only allocates fs_bsize blocks; these
1500  * blocks may be fragmented by the routine that allocates them.
1501  */
1502 static ufs2_daddr_t
1503 ffs_alloccgblk(ip, bp, bpref)
1504 	struct inode *ip;
1505 	struct buf *bp;
1506 	ufs2_daddr_t bpref;
1507 {
1508 	struct fs *fs;
1509 	struct cg *cgp;
1510 	struct ufsmount *ump;
1511 	ufs1_daddr_t bno;
1512 	ufs2_daddr_t blkno;
1513 	u_int8_t *blksfree;
1514 
1515 	fs = ip->i_fs;
1516 	ump = ip->i_ump;
1517 	mtx_assert(UFS_MTX(ump), MA_OWNED);
1518 	cgp = (struct cg *)bp->b_data;
1519 	blksfree = cg_blksfree(cgp);
1520 	if (bpref == 0 || dtog(fs, bpref) != cgp->cg_cgx) {
1521 		bpref = cgp->cg_rotor;
1522 	} else {
1523 		bpref = blknum(fs, bpref);
1524 		bno = dtogd(fs, bpref);
1525 		/*
1526 		 * if the requested block is available, use it
1527 		 */
1528 		if (ffs_isblock(fs, blksfree, fragstoblks(fs, bno)))
1529 			goto gotit;
1530 	}
1531 	/*
1532 	 * Take the next available block in this cylinder group.
1533 	 */
1534 	bno = ffs_mapsearch(fs, cgp, bpref, (int)fs->fs_frag);
1535 	if (bno < 0)
1536 		return (0);
1537 	cgp->cg_rotor = bno;
1538 gotit:
1539 	blkno = fragstoblks(fs, bno);
1540 	ffs_clrblock(fs, blksfree, (long)blkno);
1541 	ffs_clusteracct(ump, fs, cgp, blkno, -1);
1542 	cgp->cg_cs.cs_nbfree--;
1543 	fs->fs_cstotal.cs_nbfree--;
1544 	fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--;
1545 	fs->fs_fmod = 1;
1546 	blkno = cgbase(fs, cgp->cg_cgx) + bno;
1547 	/* XXX Fixme. */
1548 	UFS_UNLOCK(ump);
1549 	if (DOINGSOFTDEP(ITOV(ip)))
1550 		softdep_setup_blkmapdep(bp, UFSTOVFS(ump), blkno);
1551 	UFS_LOCK(ump);
1552 	return (blkno);
1553 }
1554 
1555 /*
1556  * Determine whether a cluster can be allocated.
1557  *
1558  * We do not currently check for optimal rotational layout if there
1559  * are multiple choices in the same cylinder group. Instead we just
1560  * take the first one that we find following bpref.
1561  */
1562 static ufs2_daddr_t
1563 ffs_clusteralloc(ip, cg, bpref, len)
1564 	struct inode *ip;
1565 	int cg;
1566 	ufs2_daddr_t bpref;
1567 	int len;
1568 {
1569 	struct fs *fs;
1570 	struct cg *cgp;
1571 	struct buf *bp;
1572 	struct ufsmount *ump;
1573 	int i, run, bit, map, got;
1574 	ufs2_daddr_t bno;
1575 	u_char *mapp;
1576 	int32_t *lp;
1577 	u_int8_t *blksfree;
1578 
1579 	fs = ip->i_fs;
1580 	ump = ip->i_ump;
1581 	if (fs->fs_maxcluster[cg] < len)
1582 		return (0);
1583 	UFS_UNLOCK(ump);
1584 	if (bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize,
1585 	    NOCRED, &bp))
1586 		goto fail_lock;
1587 	cgp = (struct cg *)bp->b_data;
1588 	if (!cg_chkmagic(cgp))
1589 		goto fail_lock;
1590 	bp->b_xflags |= BX_BKGRDWRITE;
1591 	/*
1592 	 * Check to see if a cluster of the needed size (or bigger) is
1593 	 * available in this cylinder group.
1594 	 */
1595 	lp = &cg_clustersum(cgp)[len];
1596 	for (i = len; i <= fs->fs_contigsumsize; i++)
1597 		if (*lp++ > 0)
1598 			break;
1599 	if (i > fs->fs_contigsumsize) {
1600 		/*
1601 		 * This is the first time looking for a cluster in this
1602 		 * cylinder group. Update the cluster summary information
1603 		 * to reflect the true maximum sized cluster so that
1604 		 * future cluster allocation requests can avoid reading
1605 		 * the cylinder group map only to find no clusters.
1606 		 */
1607 		lp = &cg_clustersum(cgp)[len - 1];
1608 		for (i = len - 1; i > 0; i--)
1609 			if (*lp-- > 0)
1610 				break;
1611 		UFS_LOCK(ump);
1612 		fs->fs_maxcluster[cg] = i;
1613 		goto fail;
1614 	}
1615 	/*
1616 	 * Search the cluster map to find a big enough cluster.
1617 	 * We take the first one that we find, even if it is larger
1618 	 * than we need as we prefer to get one close to the previous
1619 	 * block allocation. We do not search before the current
1620 	 * preference point as we do not want to allocate a block
1621 	 * that is allocated before the previous one (as we will
1622 	 * then have to wait for another pass of the elevator
1623 	 * algorithm before it will be read). We prefer to fail and
1624 	 * be recalled to try an allocation in the next cylinder group.
1625 	 */
1626 	if (dtog(fs, bpref) != cg)
1627 		bpref = 0;
1628 	else
1629 		bpref = fragstoblks(fs, dtogd(fs, blknum(fs, bpref)));
1630 	mapp = &cg_clustersfree(cgp)[bpref / NBBY];
1631 	map = *mapp++;
1632 	bit = 1 << (bpref % NBBY);
1633 	for (run = 0, got = bpref; got < cgp->cg_nclusterblks; got++) {
1634 		if ((map & bit) == 0) {
1635 			run = 0;
1636 		} else {
1637 			run++;
1638 			if (run == len)
1639 				break;
1640 		}
1641 		if ((got & (NBBY - 1)) != (NBBY - 1)) {
1642 			bit <<= 1;
1643 		} else {
1644 			map = *mapp++;
1645 			bit = 1;
1646 		}
1647 	}
1648 	if (got >= cgp->cg_nclusterblks)
1649 		goto fail_lock;
1650 	/*
1651 	 * Allocate the cluster that we have found.
1652 	 */
1653 	blksfree = cg_blksfree(cgp);
1654 	for (i = 1; i <= len; i++)
1655 		if (!ffs_isblock(fs, blksfree, got - run + i))
1656 			panic("ffs_clusteralloc: map mismatch");
1657 	bno = cgbase(fs, cg) + blkstofrags(fs, got - run + 1);
1658 	if (dtog(fs, bno) != cg)
1659 		panic("ffs_clusteralloc: allocated out of group");
1660 	len = blkstofrags(fs, len);
1661 	UFS_LOCK(ump);
1662 	for (i = 0; i < len; i += fs->fs_frag)
1663 		if (ffs_alloccgblk(ip, bp, bno + i) != bno + i)
1664 			panic("ffs_clusteralloc: lost block");
1665 	ACTIVECLEAR(fs, cg);
1666 	UFS_UNLOCK(ump);
1667 	bdwrite(bp);
1668 	return (bno);
1669 
1670 fail_lock:
1671 	UFS_LOCK(ump);
1672 fail:
1673 	brelse(bp);
1674 	return (0);
1675 }
1676 
1677 /*
1678  * Determine whether an inode can be allocated.
1679  *
1680  * Check to see if an inode is available, and if it is,
1681  * allocate it using the following policy:
1682  *   1) allocate the requested inode.
1683  *   2) allocate the next available inode after the requested
1684  *      inode in the specified cylinder group.
1685  */
1686 static ufs2_daddr_t
1687 ffs_nodealloccg(ip, cg, ipref, mode)
1688 	struct inode *ip;
1689 	int cg;
1690 	ufs2_daddr_t ipref;
1691 	int mode;
1692 {
1693 	struct fs *fs;
1694 	struct cg *cgp;
1695 	struct buf *bp, *ibp;
1696 	struct ufsmount *ump;
1697 	u_int8_t *inosused;
1698 	struct ufs2_dinode *dp2;
1699 	int error, start, len, loc, map, i;
1700 
1701 	fs = ip->i_fs;
1702 	ump = ip->i_ump;
1703 	if (fs->fs_cs(fs, cg).cs_nifree == 0)
1704 		return (0);
1705 	UFS_UNLOCK(ump);
1706 	error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
1707 		(int)fs->fs_cgsize, NOCRED, &bp);
1708 	if (error) {
1709 		brelse(bp);
1710 		UFS_LOCK(ump);
1711 		return (0);
1712 	}
1713 	cgp = (struct cg *)bp->b_data;
1714 	if (!cg_chkmagic(cgp) || cgp->cg_cs.cs_nifree == 0) {
1715 		brelse(bp);
1716 		UFS_LOCK(ump);
1717 		return (0);
1718 	}
1719 	bp->b_xflags |= BX_BKGRDWRITE;
1720 	cgp->cg_old_time = cgp->cg_time = time_second;
1721 	inosused = cg_inosused(cgp);
1722 	if (ipref) {
1723 		ipref %= fs->fs_ipg;
1724 		if (isclr(inosused, ipref))
1725 			goto gotit;
1726 	}
1727 	start = cgp->cg_irotor / NBBY;
1728 	len = howmany(fs->fs_ipg - cgp->cg_irotor, NBBY);
1729 	loc = skpc(0xff, len, &inosused[start]);
1730 	if (loc == 0) {
1731 		len = start + 1;
1732 		start = 0;
1733 		loc = skpc(0xff, len, &inosused[0]);
1734 		if (loc == 0) {
1735 			printf("cg = %d, irotor = %ld, fs = %s\n",
1736 			    cg, (long)cgp->cg_irotor, fs->fs_fsmnt);
1737 			panic("ffs_nodealloccg: map corrupted");
1738 			/* NOTREACHED */
1739 		}
1740 	}
1741 	i = start + len - loc;
1742 	map = inosused[i];
1743 	ipref = i * NBBY;
1744 	for (i = 1; i < (1 << NBBY); i <<= 1, ipref++) {
1745 		if ((map & i) == 0) {
1746 			cgp->cg_irotor = ipref;
1747 			goto gotit;
1748 		}
1749 	}
1750 	printf("fs = %s\n", fs->fs_fsmnt);
1751 	panic("ffs_nodealloccg: block not in map");
1752 	/* NOTREACHED */
1753 gotit:
1754 	/*
1755 	 * Check to see if we need to initialize more inodes.
1756 	 */
1757 	ibp = NULL;
1758 	if (fs->fs_magic == FS_UFS2_MAGIC &&
1759 	    ipref + INOPB(fs) > cgp->cg_initediblk &&
1760 	    cgp->cg_initediblk < cgp->cg_niblk) {
1761 		ibp = getblk(ip->i_devvp, fsbtodb(fs,
1762 		    ino_to_fsba(fs, cg * fs->fs_ipg + cgp->cg_initediblk)),
1763 		    (int)fs->fs_bsize, 0, 0, 0);
1764 		bzero(ibp->b_data, (int)fs->fs_bsize);
1765 		dp2 = (struct ufs2_dinode *)(ibp->b_data);
1766 		for (i = 0; i < INOPB(fs); i++) {
1767 			dp2->di_gen = arc4random() / 2 + 1;
1768 			dp2++;
1769 		}
1770 		cgp->cg_initediblk += INOPB(fs);
1771 	}
1772 	UFS_LOCK(ump);
1773 	ACTIVECLEAR(fs, cg);
1774 	setbit(inosused, ipref);
1775 	cgp->cg_cs.cs_nifree--;
1776 	fs->fs_cstotal.cs_nifree--;
1777 	fs->fs_cs(fs, cg).cs_nifree--;
1778 	fs->fs_fmod = 1;
1779 	if ((mode & IFMT) == IFDIR) {
1780 		cgp->cg_cs.cs_ndir++;
1781 		fs->fs_cstotal.cs_ndir++;
1782 		fs->fs_cs(fs, cg).cs_ndir++;
1783 	}
1784 	UFS_UNLOCK(ump);
1785 	if (DOINGSOFTDEP(ITOV(ip)))
1786 		softdep_setup_inomapdep(bp, ip, cg * fs->fs_ipg + ipref);
1787 	bdwrite(bp);
1788 	if (ibp != NULL)
1789 		bawrite(ibp);
1790 	return (cg * fs->fs_ipg + ipref);
1791 }
1792 
1793 /*
1794  * check if a block is free
1795  */
1796 static int
1797 ffs_isfreeblock(struct fs *fs, u_char *cp, ufs1_daddr_t h)
1798 {
1799 
1800 	switch ((int)fs->fs_frag) {
1801 	case 8:
1802 		return (cp[h] == 0);
1803 	case 4:
1804 		return ((cp[h >> 1] & (0x0f << ((h & 0x1) << 2))) == 0);
1805 	case 2:
1806 		return ((cp[h >> 2] & (0x03 << ((h & 0x3) << 1))) == 0);
1807 	case 1:
1808 		return ((cp[h >> 3] & (0x01 << (h & 0x7))) == 0);
1809 	default:
1810 		panic("ffs_isfreeblock");
1811 	}
1812 	return (0);
1813 }
1814 
1815 /*
1816  * Free a block or fragment.
1817  *
1818  * The specified block or fragment is placed back in the
1819  * free map. If a fragment is deallocated, a possible
1820  * block reassembly is checked.
1821  */
1822 void
1823 ffs_blkfree(ump, fs, devvp, bno, size, inum)
1824 	struct ufsmount *ump;
1825 	struct fs *fs;
1826 	struct vnode *devvp;
1827 	ufs2_daddr_t bno;
1828 	long size;
1829 	ino_t inum;
1830 {
1831 	struct cg *cgp;
1832 	struct buf *bp;
1833 	ufs1_daddr_t fragno, cgbno;
1834 	ufs2_daddr_t cgblkno;
1835 	int i, cg, blk, frags, bbase;
1836 	u_int8_t *blksfree;
1837 	struct cdev *dev;
1838 
1839 	cg = dtog(fs, bno);
1840 	if (devvp->v_type != VCHR) {
1841 		/* devvp is a snapshot */
1842 		dev = VTOI(devvp)->i_devvp->v_rdev;
1843 		cgblkno = fragstoblks(fs, cgtod(fs, cg));
1844 	} else {
1845 		/* devvp is a normal disk device */
1846 		dev = devvp->v_rdev;
1847 		cgblkno = fsbtodb(fs, cgtod(fs, cg));
1848 		ASSERT_VOP_LOCKED(devvp, "ffs_blkfree");
1849 		if ((devvp->v_vflag & VV_COPYONWRITE) &&
1850 		    ffs_snapblkfree(fs, devvp, bno, size, inum))
1851 			return;
1852 	}
1853 #ifdef DIAGNOSTIC
1854 	if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0 ||
1855 	    fragnum(fs, bno) + numfrags(fs, size) > fs->fs_frag) {
1856 		printf("dev=%s, bno = %jd, bsize = %ld, size = %ld, fs = %s\n",
1857 		    devtoname(dev), (intmax_t)bno, (long)fs->fs_bsize,
1858 		    size, fs->fs_fsmnt);
1859 		panic("ffs_blkfree: bad size");
1860 	}
1861 #endif
1862 	if ((u_int)bno >= fs->fs_size) {
1863 		printf("bad block %jd, ino %lu\n", (intmax_t)bno,
1864 		    (u_long)inum);
1865 		ffs_fserr(fs, inum, "bad block");
1866 		return;
1867 	}
1868 	if (bread(devvp, cgblkno, (int)fs->fs_cgsize, NOCRED, &bp)) {
1869 		brelse(bp);
1870 		return;
1871 	}
1872 	cgp = (struct cg *)bp->b_data;
1873 	if (!cg_chkmagic(cgp)) {
1874 		brelse(bp);
1875 		return;
1876 	}
1877 	bp->b_xflags |= BX_BKGRDWRITE;
1878 	cgp->cg_old_time = cgp->cg_time = time_second;
1879 	cgbno = dtogd(fs, bno);
1880 	blksfree = cg_blksfree(cgp);
1881 	UFS_LOCK(ump);
1882 	if (size == fs->fs_bsize) {
1883 		fragno = fragstoblks(fs, cgbno);
1884 		if (!ffs_isfreeblock(fs, blksfree, fragno)) {
1885 			if (devvp->v_type != VCHR) {
1886 				UFS_UNLOCK(ump);
1887 				/* devvp is a snapshot */
1888 				brelse(bp);
1889 				return;
1890 			}
1891 			printf("dev = %s, block = %jd, fs = %s\n",
1892 			    devtoname(dev), (intmax_t)bno, fs->fs_fsmnt);
1893 			panic("ffs_blkfree: freeing free block");
1894 		}
1895 		ffs_setblock(fs, blksfree, fragno);
1896 		ffs_clusteracct(ump, fs, cgp, fragno, 1);
1897 		cgp->cg_cs.cs_nbfree++;
1898 		fs->fs_cstotal.cs_nbfree++;
1899 		fs->fs_cs(fs, cg).cs_nbfree++;
1900 	} else {
1901 		bbase = cgbno - fragnum(fs, cgbno);
1902 		/*
1903 		 * decrement the counts associated with the old frags
1904 		 */
1905 		blk = blkmap(fs, blksfree, bbase);
1906 		ffs_fragacct(fs, blk, cgp->cg_frsum, -1);
1907 		/*
1908 		 * deallocate the fragment
1909 		 */
1910 		frags = numfrags(fs, size);
1911 		for (i = 0; i < frags; i++) {
1912 			if (isset(blksfree, cgbno + i)) {
1913 				printf("dev = %s, block = %jd, fs = %s\n",
1914 				    devtoname(dev), (intmax_t)(bno + i),
1915 				    fs->fs_fsmnt);
1916 				panic("ffs_blkfree: freeing free frag");
1917 			}
1918 			setbit(blksfree, cgbno + i);
1919 		}
1920 		cgp->cg_cs.cs_nffree += i;
1921 		fs->fs_cstotal.cs_nffree += i;
1922 		fs->fs_cs(fs, cg).cs_nffree += i;
1923 		/*
1924 		 * add back in counts associated with the new frags
1925 		 */
1926 		blk = blkmap(fs, blksfree, bbase);
1927 		ffs_fragacct(fs, blk, cgp->cg_frsum, 1);
1928 		/*
1929 		 * if a complete block has been reassembled, account for it
1930 		 */
1931 		fragno = fragstoblks(fs, bbase);
1932 		if (ffs_isblock(fs, blksfree, fragno)) {
1933 			cgp->cg_cs.cs_nffree -= fs->fs_frag;
1934 			fs->fs_cstotal.cs_nffree -= fs->fs_frag;
1935 			fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag;
1936 			ffs_clusteracct(ump, fs, cgp, fragno, 1);
1937 			cgp->cg_cs.cs_nbfree++;
1938 			fs->fs_cstotal.cs_nbfree++;
1939 			fs->fs_cs(fs, cg).cs_nbfree++;
1940 		}
1941 	}
1942 	fs->fs_fmod = 1;
1943 	ACTIVECLEAR(fs, cg);
1944 	UFS_UNLOCK(ump);
1945 	bdwrite(bp);
1946 }
1947 
1948 #ifdef DIAGNOSTIC
1949 /*
1950  * Verify allocation of a block or fragment. Returns true if block or
1951  * fragment is allocated, false if it is free.
1952  */
1953 static int
1954 ffs_checkblk(ip, bno, size)
1955 	struct inode *ip;
1956 	ufs2_daddr_t bno;
1957 	long size;
1958 {
1959 	struct fs *fs;
1960 	struct cg *cgp;
1961 	struct buf *bp;
1962 	ufs1_daddr_t cgbno;
1963 	int i, error, frags, free;
1964 	u_int8_t *blksfree;
1965 
1966 	fs = ip->i_fs;
1967 	if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0) {
1968 		printf("bsize = %ld, size = %ld, fs = %s\n",
1969 		    (long)fs->fs_bsize, size, fs->fs_fsmnt);
1970 		panic("ffs_checkblk: bad size");
1971 	}
1972 	if ((u_int)bno >= fs->fs_size)
1973 		panic("ffs_checkblk: bad block %jd", (intmax_t)bno);
1974 	error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, dtog(fs, bno))),
1975 		(int)fs->fs_cgsize, NOCRED, &bp);
1976 	if (error)
1977 		panic("ffs_checkblk: cg bread failed");
1978 	cgp = (struct cg *)bp->b_data;
1979 	if (!cg_chkmagic(cgp))
1980 		panic("ffs_checkblk: cg magic mismatch");
1981 	bp->b_xflags |= BX_BKGRDWRITE;
1982 	blksfree = cg_blksfree(cgp);
1983 	cgbno = dtogd(fs, bno);
1984 	if (size == fs->fs_bsize) {
1985 		free = ffs_isblock(fs, blksfree, fragstoblks(fs, cgbno));
1986 	} else {
1987 		frags = numfrags(fs, size);
1988 		for (free = 0, i = 0; i < frags; i++)
1989 			if (isset(blksfree, cgbno + i))
1990 				free++;
1991 		if (free != 0 && free != frags)
1992 			panic("ffs_checkblk: partially free fragment");
1993 	}
1994 	brelse(bp);
1995 	return (!free);
1996 }
1997 #endif /* DIAGNOSTIC */
1998 
1999 /*
2000  * Free an inode.
2001  */
2002 int
2003 ffs_vfree(pvp, ino, mode)
2004 	struct vnode *pvp;
2005 	ino_t ino;
2006 	int mode;
2007 {
2008 	struct inode *ip;
2009 
2010 	if (DOINGSOFTDEP(pvp)) {
2011 		softdep_freefile(pvp, ino, mode);
2012 		return (0);
2013 	}
2014 	ip = VTOI(pvp);
2015 	return (ffs_freefile(ip->i_ump, ip->i_fs, ip->i_devvp, ino, mode));
2016 }
2017 
2018 /*
2019  * Do the actual free operation.
2020  * The specified inode is placed back in the free map.
2021  */
2022 int
2023 ffs_freefile(ump, fs, devvp, ino, mode)
2024 	struct ufsmount *ump;
2025 	struct fs *fs;
2026 	struct vnode *devvp;
2027 	ino_t ino;
2028 	int mode;
2029 {
2030 	struct cg *cgp;
2031 	struct buf *bp;
2032 	ufs2_daddr_t cgbno;
2033 	int error, cg;
2034 	u_int8_t *inosused;
2035 	struct cdev *dev;
2036 
2037 	cg = ino_to_cg(fs, ino);
2038 	if (devvp->v_type != VCHR) {
2039 		/* devvp is a snapshot */
2040 		dev = VTOI(devvp)->i_devvp->v_rdev;
2041 		cgbno = fragstoblks(fs, cgtod(fs, cg));
2042 	} else {
2043 		/* devvp is a normal disk device */
2044 		dev = devvp->v_rdev;
2045 		cgbno = fsbtodb(fs, cgtod(fs, cg));
2046 	}
2047 	if ((u_int)ino >= fs->fs_ipg * fs->fs_ncg)
2048 		panic("ffs_freefile: range: dev = %s, ino = %lu, fs = %s",
2049 		    devtoname(dev), (u_long)ino, fs->fs_fsmnt);
2050 	if ((error = bread(devvp, cgbno, (int)fs->fs_cgsize, NOCRED, &bp))) {
2051 		brelse(bp);
2052 		return (error);
2053 	}
2054 	cgp = (struct cg *)bp->b_data;
2055 	if (!cg_chkmagic(cgp)) {
2056 		brelse(bp);
2057 		return (0);
2058 	}
2059 	bp->b_xflags |= BX_BKGRDWRITE;
2060 	cgp->cg_old_time = cgp->cg_time = time_second;
2061 	inosused = cg_inosused(cgp);
2062 	ino %= fs->fs_ipg;
2063 	if (isclr(inosused, ino)) {
2064 		printf("dev = %s, ino = %lu, fs = %s\n", devtoname(dev),
2065 		    (u_long)ino + cg * fs->fs_ipg, fs->fs_fsmnt);
2066 		if (fs->fs_ronly == 0)
2067 			panic("ffs_freefile: freeing free inode");
2068 	}
2069 	clrbit(inosused, ino);
2070 	if (ino < cgp->cg_irotor)
2071 		cgp->cg_irotor = ino;
2072 	cgp->cg_cs.cs_nifree++;
2073 	UFS_LOCK(ump);
2074 	fs->fs_cstotal.cs_nifree++;
2075 	fs->fs_cs(fs, cg).cs_nifree++;
2076 	if ((mode & IFMT) == IFDIR) {
2077 		cgp->cg_cs.cs_ndir--;
2078 		fs->fs_cstotal.cs_ndir--;
2079 		fs->fs_cs(fs, cg).cs_ndir--;
2080 	}
2081 	fs->fs_fmod = 1;
2082 	ACTIVECLEAR(fs, cg);
2083 	UFS_UNLOCK(ump);
2084 	bdwrite(bp);
2085 	return (0);
2086 }
2087 
2088 /*
2089  * Check to see if a file is free.
2090  */
2091 int
2092 ffs_checkfreefile(fs, devvp, ino)
2093 	struct fs *fs;
2094 	struct vnode *devvp;
2095 	ino_t ino;
2096 {
2097 	struct cg *cgp;
2098 	struct buf *bp;
2099 	ufs2_daddr_t cgbno;
2100 	int ret, cg;
2101 	u_int8_t *inosused;
2102 
2103 	cg = ino_to_cg(fs, ino);
2104 	if (devvp->v_type != VCHR) {
2105 		/* devvp is a snapshot */
2106 		cgbno = fragstoblks(fs, cgtod(fs, cg));
2107 	} else {
2108 		/* devvp is a normal disk device */
2109 		cgbno = fsbtodb(fs, cgtod(fs, cg));
2110 	}
2111 	if ((u_int)ino >= fs->fs_ipg * fs->fs_ncg)
2112 		return (1);
2113 	if (bread(devvp, cgbno, (int)fs->fs_cgsize, NOCRED, &bp)) {
2114 		brelse(bp);
2115 		return (1);
2116 	}
2117 	cgp = (struct cg *)bp->b_data;
2118 	if (!cg_chkmagic(cgp)) {
2119 		brelse(bp);
2120 		return (1);
2121 	}
2122 	inosused = cg_inosused(cgp);
2123 	ino %= fs->fs_ipg;
2124 	ret = isclr(inosused, ino);
2125 	brelse(bp);
2126 	return (ret);
2127 }
2128 
2129 /*
2130  * Find a block of the specified size in the specified cylinder group.
2131  *
2132  * It is a panic if a request is made to find a block if none are
2133  * available.
2134  */
2135 static ufs1_daddr_t
2136 ffs_mapsearch(fs, cgp, bpref, allocsiz)
2137 	struct fs *fs;
2138 	struct cg *cgp;
2139 	ufs2_daddr_t bpref;
2140 	int allocsiz;
2141 {
2142 	ufs1_daddr_t bno;
2143 	int start, len, loc, i;
2144 	int blk, field, subfield, pos;
2145 	u_int8_t *blksfree;
2146 
2147 	/*
2148 	 * find the fragment by searching through the free block
2149 	 * map for an appropriate bit pattern
2150 	 */
2151 	if (bpref)
2152 		start = dtogd(fs, bpref) / NBBY;
2153 	else
2154 		start = cgp->cg_frotor / NBBY;
2155 	blksfree = cg_blksfree(cgp);
2156 	len = howmany(fs->fs_fpg, NBBY) - start;
2157 	loc = scanc((u_int)len, (u_char *)&blksfree[start],
2158 		fragtbl[fs->fs_frag],
2159 		(u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
2160 	if (loc == 0) {
2161 		len = start + 1;
2162 		start = 0;
2163 		loc = scanc((u_int)len, (u_char *)&blksfree[0],
2164 			fragtbl[fs->fs_frag],
2165 			(u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
2166 		if (loc == 0) {
2167 			printf("start = %d, len = %d, fs = %s\n",
2168 			    start, len, fs->fs_fsmnt);
2169 			panic("ffs_alloccg: map corrupted");
2170 			/* NOTREACHED */
2171 		}
2172 	}
2173 	bno = (start + len - loc) * NBBY;
2174 	cgp->cg_frotor = bno;
2175 	/*
2176 	 * found the byte in the map
2177 	 * sift through the bits to find the selected frag
2178 	 */
2179 	for (i = bno + NBBY; bno < i; bno += fs->fs_frag) {
2180 		blk = blkmap(fs, blksfree, bno);
2181 		blk <<= 1;
2182 		field = around[allocsiz];
2183 		subfield = inside[allocsiz];
2184 		for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) {
2185 			if ((blk & field) == subfield)
2186 				return (bno + pos);
2187 			field <<= 1;
2188 			subfield <<= 1;
2189 		}
2190 	}
2191 	printf("bno = %lu, fs = %s\n", (u_long)bno, fs->fs_fsmnt);
2192 	panic("ffs_alloccg: block not in map");
2193 	return (-1);
2194 }
2195 
2196 /*
2197  * Update the cluster map because of an allocation or free.
2198  *
2199  * Cnt == 1 means free; cnt == -1 means allocating.
2200  */
2201 void
2202 ffs_clusteracct(ump, fs, cgp, blkno, cnt)
2203 	struct ufsmount *ump;
2204 	struct fs *fs;
2205 	struct cg *cgp;
2206 	ufs1_daddr_t blkno;
2207 	int cnt;
2208 {
2209 	int32_t *sump;
2210 	int32_t *lp;
2211 	u_char *freemapp, *mapp;
2212 	int i, start, end, forw, back, map, bit;
2213 
2214 	mtx_assert(UFS_MTX(ump), MA_OWNED);
2215 
2216 	if (fs->fs_contigsumsize <= 0)
2217 		return;
2218 	freemapp = cg_clustersfree(cgp);
2219 	sump = cg_clustersum(cgp);
2220 	/*
2221 	 * Allocate or clear the actual block.
2222 	 */
2223 	if (cnt > 0)
2224 		setbit(freemapp, blkno);
2225 	else
2226 		clrbit(freemapp, blkno);
2227 	/*
2228 	 * Find the size of the cluster going forward.
2229 	 */
2230 	start = blkno + 1;
2231 	end = start + fs->fs_contigsumsize;
2232 	if (end >= cgp->cg_nclusterblks)
2233 		end = cgp->cg_nclusterblks;
2234 	mapp = &freemapp[start / NBBY];
2235 	map = *mapp++;
2236 	bit = 1 << (start % NBBY);
2237 	for (i = start; i < end; i++) {
2238 		if ((map & bit) == 0)
2239 			break;
2240 		if ((i & (NBBY - 1)) != (NBBY - 1)) {
2241 			bit <<= 1;
2242 		} else {
2243 			map = *mapp++;
2244 			bit = 1;
2245 		}
2246 	}
2247 	forw = i - start;
2248 	/*
2249 	 * Find the size of the cluster going backward.
2250 	 */
2251 	start = blkno - 1;
2252 	end = start - fs->fs_contigsumsize;
2253 	if (end < 0)
2254 		end = -1;
2255 	mapp = &freemapp[start / NBBY];
2256 	map = *mapp--;
2257 	bit = 1 << (start % NBBY);
2258 	for (i = start; i > end; i--) {
2259 		if ((map & bit) == 0)
2260 			break;
2261 		if ((i & (NBBY - 1)) != 0) {
2262 			bit >>= 1;
2263 		} else {
2264 			map = *mapp--;
2265 			bit = 1 << (NBBY - 1);
2266 		}
2267 	}
2268 	back = start - i;
2269 	/*
2270 	 * Account for old cluster and the possibly new forward and
2271 	 * back clusters.
2272 	 */
2273 	i = back + forw + 1;
2274 	if (i > fs->fs_contigsumsize)
2275 		i = fs->fs_contigsumsize;
2276 	sump[i] += cnt;
2277 	if (back > 0)
2278 		sump[back] -= cnt;
2279 	if (forw > 0)
2280 		sump[forw] -= cnt;
2281 	/*
2282 	 * Update cluster summary information.
2283 	 */
2284 	lp = &sump[fs->fs_contigsumsize];
2285 	for (i = fs->fs_contigsumsize; i > 0; i--)
2286 		if (*lp-- > 0)
2287 			break;
2288 	fs->fs_maxcluster[cgp->cg_cgx] = i;
2289 }
2290 
2291 /*
2292  * Fserr prints the name of a filesystem with an error diagnostic.
2293  *
2294  * The form of the error message is:
2295  *	fs: error message
2296  */
2297 static void
2298 ffs_fserr(fs, inum, cp)
2299 	struct fs *fs;
2300 	ino_t inum;
2301 	char *cp;
2302 {
2303 	struct thread *td = curthread;	/* XXX */
2304 	struct proc *p = td->td_proc;
2305 
2306 	log(LOG_ERR, "pid %d (%s), uid %d inumber %d on %s: %s\n",
2307 	    p->p_pid, p->p_comm, td->td_ucred->cr_uid, inum, fs->fs_fsmnt, cp);
2308 }
2309 
2310 /*
2311  * This function provides the capability for the fsck program to
2312  * update an active filesystem. Eleven operations are provided:
2313  *
2314  * adjrefcnt(inode, amt) - adjusts the reference count on the
2315  *	specified inode by the specified amount. Under normal
2316  *	operation the count should always go down. Decrementing
2317  *	the count to zero will cause the inode to be freed.
2318  * adjblkcnt(inode, amt) - adjust the number of blocks used to
2319  *	by the specifed amount.
2320  * adjndir, adjbfree, adjifree, adjffree, adjnumclusters(amt) -
2321  *	adjust the superblock summary.
2322  * freedirs(inode, count) - directory inodes [inode..inode + count - 1]
2323  *	are marked as free. Inodes should never have to be marked
2324  *	as in use.
2325  * freefiles(inode, count) - file inodes [inode..inode + count - 1]
2326  *	are marked as free. Inodes should never have to be marked
2327  *	as in use.
2328  * freeblks(blockno, size) - blocks [blockno..blockno + size - 1]
2329  *	are marked as free. Blocks should never have to be marked
2330  *	as in use.
2331  * setflags(flags, set/clear) - the fs_flags field has the specified
2332  *	flags set (second parameter +1) or cleared (second parameter -1).
2333  */
2334 
2335 static int sysctl_ffs_fsck(SYSCTL_HANDLER_ARGS);
2336 
2337 SYSCTL_PROC(_vfs_ffs, FFS_ADJ_REFCNT, adjrefcnt, CTLFLAG_WR|CTLTYPE_STRUCT,
2338 	0, 0, sysctl_ffs_fsck, "S,fsck", "Adjust Inode Reference Count");
2339 
2340 static SYSCTL_NODE(_vfs_ffs, FFS_ADJ_BLKCNT, adjblkcnt, CTLFLAG_WR,
2341 	sysctl_ffs_fsck, "Adjust Inode Used Blocks Count");
2342 
2343 static SYSCTL_NODE(_vfs_ffs, FFS_ADJ_NDIR, adjndir, CTLFLAG_WR,
2344 	sysctl_ffs_fsck, "Adjust number of directories");
2345 
2346 static SYSCTL_NODE(_vfs_ffs, FFS_ADJ_NBFREE, adjnbfree, CTLFLAG_WR,
2347 	sysctl_ffs_fsck, "Adjust number of free blocks");
2348 
2349 static SYSCTL_NODE(_vfs_ffs, FFS_ADJ_NIFREE, adjnifree, CTLFLAG_WR,
2350 	sysctl_ffs_fsck, "Adjust number of free inodes");
2351 
2352 static SYSCTL_NODE(_vfs_ffs, FFS_ADJ_NFFREE, adjnffree, CTLFLAG_WR,
2353 	sysctl_ffs_fsck, "Adjust number of free frags");
2354 
2355 static SYSCTL_NODE(_vfs_ffs, FFS_ADJ_NUMCLUSTERS, adjnumclusters, CTLFLAG_WR,
2356 	sysctl_ffs_fsck, "Adjust number of free clusters");
2357 
2358 static SYSCTL_NODE(_vfs_ffs, FFS_DIR_FREE, freedirs, CTLFLAG_WR,
2359 	sysctl_ffs_fsck, "Free Range of Directory Inodes");
2360 
2361 static SYSCTL_NODE(_vfs_ffs, FFS_FILE_FREE, freefiles, CTLFLAG_WR,
2362 	sysctl_ffs_fsck, "Free Range of File Inodes");
2363 
2364 static SYSCTL_NODE(_vfs_ffs, FFS_BLK_FREE, freeblks, CTLFLAG_WR,
2365 	sysctl_ffs_fsck, "Free Range of Blocks");
2366 
2367 static SYSCTL_NODE(_vfs_ffs, FFS_SET_FLAGS, setflags, CTLFLAG_WR,
2368 	sysctl_ffs_fsck, "Change Filesystem Flags");
2369 
2370 #ifdef DEBUG
2371 static int fsckcmds = 0;
2372 SYSCTL_INT(_debug, OID_AUTO, fsckcmds, CTLFLAG_RW, &fsckcmds, 0, "");
2373 #endif /* DEBUG */
2374 
2375 static int
2376 sysctl_ffs_fsck(SYSCTL_HANDLER_ARGS)
2377 {
2378 	struct fsck_cmd cmd;
2379 	struct ufsmount *ump;
2380 	struct vnode *vp;
2381 	struct inode *ip;
2382 	struct mount *mp;
2383 	struct fs *fs;
2384 	ufs2_daddr_t blkno;
2385 	long blkcnt, blksize;
2386 	struct file *fp;
2387 	int filetype, error;
2388 
2389 	if (req->newlen > sizeof cmd)
2390 		return (EBADRPC);
2391 	if ((error = SYSCTL_IN(req, &cmd, sizeof cmd)) != 0)
2392 		return (error);
2393 	if (cmd.version != FFS_CMD_VERSION)
2394 		return (ERPCMISMATCH);
2395 	if ((error = getvnode(curproc->p_fd, cmd.handle, &fp)) != 0)
2396 		return (error);
2397 	vn_start_write(fp->f_data, &mp, V_WAIT);
2398 	if (mp == 0 || strncmp(mp->mnt_stat.f_fstypename, "ufs", MFSNAMELEN)) {
2399 		vn_finished_write(mp);
2400 		fdrop(fp, curthread);
2401 		return (EINVAL);
2402 	}
2403 	if (mp->mnt_flag & MNT_RDONLY) {
2404 		vn_finished_write(mp);
2405 		fdrop(fp, curthread);
2406 		return (EROFS);
2407 	}
2408 	ump = VFSTOUFS(mp);
2409 	fs = ump->um_fs;
2410 	filetype = IFREG;
2411 
2412 	switch (oidp->oid_number) {
2413 
2414 	case FFS_SET_FLAGS:
2415 #ifdef DEBUG
2416 		if (fsckcmds)
2417 			printf("%s: %s flags\n", mp->mnt_stat.f_mntonname,
2418 			    cmd.size > 0 ? "set" : "clear");
2419 #endif /* DEBUG */
2420 		if (cmd.size > 0)
2421 			fs->fs_flags |= (long)cmd.value;
2422 		else
2423 			fs->fs_flags &= ~(long)cmd.value;
2424 		break;
2425 
2426 	case FFS_ADJ_REFCNT:
2427 #ifdef DEBUG
2428 		if (fsckcmds) {
2429 			printf("%s: adjust inode %jd count by %jd\n",
2430 			    mp->mnt_stat.f_mntonname, (intmax_t)cmd.value,
2431 			    (intmax_t)cmd.size);
2432 		}
2433 #endif /* DEBUG */
2434 		if ((error = ffs_vget(mp, (ino_t)cmd.value, LK_EXCLUSIVE, &vp)))
2435 			break;
2436 		ip = VTOI(vp);
2437 		ip->i_nlink += cmd.size;
2438 		DIP_SET(ip, i_nlink, ip->i_nlink);
2439 		ip->i_effnlink += cmd.size;
2440 		ip->i_flag |= IN_CHANGE;
2441 		if (DOINGSOFTDEP(vp))
2442 			softdep_change_linkcnt(ip);
2443 		vput(vp);
2444 		break;
2445 
2446 	case FFS_ADJ_BLKCNT:
2447 #ifdef DEBUG
2448 		if (fsckcmds) {
2449 			printf("%s: adjust inode %jd block count by %jd\n",
2450 			    mp->mnt_stat.f_mntonname, (intmax_t)cmd.value,
2451 			    (intmax_t)cmd.size);
2452 		}
2453 #endif /* DEBUG */
2454 		if ((error = ffs_vget(mp, (ino_t)cmd.value, LK_EXCLUSIVE, &vp)))
2455 			break;
2456 		ip = VTOI(vp);
2457 		if (ip->i_flag & IN_SPACECOUNTED) {
2458 			UFS_LOCK(ump);
2459 			fs->fs_pendingblocks += cmd.size;
2460 			UFS_UNLOCK(ump);
2461 		}
2462 		DIP_SET(ip, i_blocks, DIP(ip, i_blocks) + cmd.size);
2463 		ip->i_flag |= IN_CHANGE;
2464 		vput(vp);
2465 		break;
2466 
2467 	case FFS_DIR_FREE:
2468 		filetype = IFDIR;
2469 		/* fall through */
2470 
2471 	case FFS_FILE_FREE:
2472 #ifdef DEBUG
2473 		if (fsckcmds) {
2474 			if (cmd.size == 1)
2475 				printf("%s: free %s inode %d\n",
2476 				    mp->mnt_stat.f_mntonname,
2477 				    filetype == IFDIR ? "directory" : "file",
2478 				    (ino_t)cmd.value);
2479 			else
2480 				printf("%s: free %s inodes %d-%d\n",
2481 				    mp->mnt_stat.f_mntonname,
2482 				    filetype == IFDIR ? "directory" : "file",
2483 				    (ino_t)cmd.value,
2484 				    (ino_t)(cmd.value + cmd.size - 1));
2485 		}
2486 #endif /* DEBUG */
2487 		while (cmd.size > 0) {
2488 			if ((error = ffs_freefile(ump, fs, ump->um_devvp,
2489 			    cmd.value, filetype)))
2490 				break;
2491 			cmd.size -= 1;
2492 			cmd.value += 1;
2493 		}
2494 		break;
2495 
2496 	case FFS_BLK_FREE:
2497 #ifdef DEBUG
2498 		if (fsckcmds) {
2499 			if (cmd.size == 1)
2500 				printf("%s: free block %jd\n",
2501 				    mp->mnt_stat.f_mntonname,
2502 				    (intmax_t)cmd.value);
2503 			else
2504 				printf("%s: free blocks %jd-%jd\n",
2505 				    mp->mnt_stat.f_mntonname,
2506 				    (intmax_t)cmd.value,
2507 				    (intmax_t)cmd.value + cmd.size - 1);
2508 		}
2509 #endif /* DEBUG */
2510 		blkno = cmd.value;
2511 		blkcnt = cmd.size;
2512 		blksize = fs->fs_frag - (blkno % fs->fs_frag);
2513 		while (blkcnt > 0) {
2514 			if (blksize > blkcnt)
2515 				blksize = blkcnt;
2516 			ffs_blkfree(ump, fs, ump->um_devvp, blkno,
2517 			    blksize * fs->fs_fsize, ROOTINO);
2518 			blkno += blksize;
2519 			blkcnt -= blksize;
2520 			blksize = fs->fs_frag;
2521 		}
2522 		break;
2523 
2524 	/*
2525 	 * Adjust superblock summaries.  fsck(8) is expected to
2526 	 * submit deltas when necessary.
2527 	 */
2528 	case FFS_ADJ_NDIR:
2529 #ifdef DEBUG
2530 		if (fsckcmds) {
2531 			printf("%s: adjust number of directories by %jd\n",
2532 			    mp->mnt_stat.f_mntonname, (intmax_t)cmd.value);
2533 		}
2534 #endif /* DEBUG */
2535 		fs->fs_cstotal.cs_ndir += cmd.value;
2536 		break;
2537 	case FFS_ADJ_NBFREE:
2538 #ifdef DEBUG
2539 		if (fsckcmds) {
2540 			printf("%s: adjust number of free blocks by %+jd\n",
2541 			    mp->mnt_stat.f_mntonname, (intmax_t)cmd.value);
2542 		}
2543 #endif /* DEBUG */
2544 		fs->fs_cstotal.cs_nbfree += cmd.value;
2545 		break;
2546 	case FFS_ADJ_NIFREE:
2547 #ifdef DEBUG
2548 		if (fsckcmds) {
2549 			printf("%s: adjust number of free inodes by %+jd\n",
2550 			    mp->mnt_stat.f_mntonname, (intmax_t)cmd.value);
2551 		}
2552 #endif /* DEBUG */
2553 		fs->fs_cstotal.cs_nifree += cmd.value;
2554 		break;
2555 	case FFS_ADJ_NFFREE:
2556 #ifdef DEBUG
2557 		if (fsckcmds) {
2558 			printf("%s: adjust number of free frags by %+jd\n",
2559 			    mp->mnt_stat.f_mntonname, (intmax_t)cmd.value);
2560 		}
2561 #endif /* DEBUG */
2562 		fs->fs_cstotal.cs_nffree += cmd.value;
2563 		break;
2564 	case FFS_ADJ_NUMCLUSTERS:
2565 #ifdef DEBUG
2566 		if (fsckcmds) {
2567 			printf("%s: adjust number of free clusters by %+jd\n",
2568 			    mp->mnt_stat.f_mntonname, (intmax_t)cmd.value);
2569 		}
2570 #endif /* DEBUG */
2571 		fs->fs_cstotal.cs_numclusters += cmd.value;
2572 		break;
2573 
2574 	default:
2575 #ifdef DEBUG
2576 		if (fsckcmds) {
2577 			printf("Invalid request %d from fsck\n",
2578 			    oidp->oid_number);
2579 		}
2580 #endif /* DEBUG */
2581 		error = EINVAL;
2582 		break;
2583 
2584 	}
2585 	fdrop(fp, curthread);
2586 	vn_finished_write(mp);
2587 	return (error);
2588 }
2589