xref: /freebsd/sys/ufs/ffs/ffs_alloc.c (revision 09a53ad8f1318c5daae6cfb19d97f4f6459f0013)
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/capsicum.h>
69 #include <sys/systm.h>
70 #include <sys/bio.h>
71 #include <sys/buf.h>
72 #include <sys/conf.h>
73 #include <sys/fcntl.h>
74 #include <sys/file.h>
75 #include <sys/filedesc.h>
76 #include <sys/priv.h>
77 #include <sys/proc.h>
78 #include <sys/vnode.h>
79 #include <sys/mount.h>
80 #include <sys/kernel.h>
81 #include <sys/syscallsubr.h>
82 #include <sys/sysctl.h>
83 #include <sys/syslog.h>
84 #include <sys/taskqueue.h>
85 
86 #include <security/audit/audit.h>
87 
88 #include <geom/geom.h>
89 
90 #include <ufs/ufs/dir.h>
91 #include <ufs/ufs/extattr.h>
92 #include <ufs/ufs/quota.h>
93 #include <ufs/ufs/inode.h>
94 #include <ufs/ufs/ufs_extern.h>
95 #include <ufs/ufs/ufsmount.h>
96 
97 #include <ufs/ffs/fs.h>
98 #include <ufs/ffs/ffs_extern.h>
99 #include <ufs/ffs/softdep.h>
100 
101 typedef ufs2_daddr_t allocfcn_t(struct inode *ip, u_int cg, ufs2_daddr_t bpref,
102 				  int size, int rsize);
103 
104 static ufs2_daddr_t ffs_alloccg(struct inode *, u_int, ufs2_daddr_t, int, int);
105 static ufs2_daddr_t
106 	      ffs_alloccgblk(struct inode *, struct buf *, ufs2_daddr_t, int);
107 static void	ffs_blkfree_cg(struct ufsmount *, struct fs *,
108 		    struct vnode *, ufs2_daddr_t, long, ino_t,
109 		    struct workhead *);
110 static void	ffs_blkfree_trim_completed(struct bio *);
111 static void	ffs_blkfree_trim_task(void *ctx, int pending __unused);
112 #ifdef INVARIANTS
113 static int	ffs_checkblk(struct inode *, ufs2_daddr_t, long);
114 #endif
115 static ufs2_daddr_t ffs_clusteralloc(struct inode *, u_int, ufs2_daddr_t, int);
116 static ino_t	ffs_dirpref(struct inode *);
117 static ufs2_daddr_t ffs_fragextend(struct inode *, u_int, ufs2_daddr_t,
118 		    int, int);
119 static ufs2_daddr_t	ffs_hashalloc
120 		(struct inode *, u_int, ufs2_daddr_t, int, int, allocfcn_t *);
121 static ufs2_daddr_t ffs_nodealloccg(struct inode *, u_int, ufs2_daddr_t, int,
122 		    int);
123 static ufs1_daddr_t ffs_mapsearch(struct fs *, struct cg *, ufs2_daddr_t, int);
124 static int	ffs_reallocblks_ufs1(struct vop_reallocblks_args *);
125 static int	ffs_reallocblks_ufs2(struct vop_reallocblks_args *);
126 
127 /*
128  * Allocate a block in the filesystem.
129  *
130  * The size of the requested block is given, which must be some
131  * multiple of fs_fsize and <= fs_bsize.
132  * A preference may be optionally specified. If a preference is given
133  * the following hierarchy is used to allocate a block:
134  *   1) allocate the requested block.
135  *   2) allocate a rotationally optimal block in the same cylinder.
136  *   3) allocate a block in the same cylinder group.
137  *   4) quadradically rehash into other cylinder groups, until an
138  *      available block is located.
139  * If no block preference is given the following hierarchy is used
140  * to allocate a block:
141  *   1) allocate a block in the cylinder group that contains the
142  *      inode for the file.
143  *   2) quadradically rehash into other cylinder groups, until an
144  *      available block is located.
145  */
146 int
147 ffs_alloc(ip, lbn, bpref, size, flags, cred, bnp)
148 	struct inode *ip;
149 	ufs2_daddr_t lbn, bpref;
150 	int size, flags;
151 	struct ucred *cred;
152 	ufs2_daddr_t *bnp;
153 {
154 	struct fs *fs;
155 	struct ufsmount *ump;
156 	ufs2_daddr_t bno;
157 	u_int cg, reclaimed;
158 	static struct timeval lastfail;
159 	static int curfail;
160 	int64_t delta;
161 #ifdef QUOTA
162 	int error;
163 #endif
164 
165 	*bnp = 0;
166 	ump = ITOUMP(ip);
167 	fs = ump->um_fs;
168 	mtx_assert(UFS_MTX(ump), MA_OWNED);
169 #ifdef INVARIANTS
170 	if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0) {
171 		printf("dev = %s, bsize = %ld, size = %d, fs = %s\n",
172 		    devtoname(ump->um_dev), (long)fs->fs_bsize, size,
173 		    fs->fs_fsmnt);
174 		panic("ffs_alloc: bad size");
175 	}
176 	if (cred == NOCRED)
177 		panic("ffs_alloc: missing credential");
178 #endif /* INVARIANTS */
179 	reclaimed = 0;
180 retry:
181 #ifdef QUOTA
182 	UFS_UNLOCK(ump);
183 	error = chkdq(ip, btodb(size), cred, 0);
184 	if (error)
185 		return (error);
186 	UFS_LOCK(ump);
187 #endif
188 	if (size == fs->fs_bsize && fs->fs_cstotal.cs_nbfree == 0)
189 		goto nospace;
190 	if (priv_check_cred(cred, PRIV_VFS_BLOCKRESERVE, 0) &&
191 	    freespace(fs, fs->fs_minfree) - numfrags(fs, size) < 0)
192 		goto nospace;
193 	if (bpref >= fs->fs_size)
194 		bpref = 0;
195 	if (bpref == 0)
196 		cg = ino_to_cg(fs, ip->i_number);
197 	else
198 		cg = dtog(fs, bpref);
199 	bno = ffs_hashalloc(ip, cg, bpref, size, size, ffs_alloccg);
200 	if (bno > 0) {
201 		delta = btodb(size);
202 		DIP_SET(ip, i_blocks, DIP(ip, i_blocks) + delta);
203 		if (flags & IO_EXT)
204 			ip->i_flag |= IN_CHANGE;
205 		else
206 			ip->i_flag |= IN_CHANGE | IN_UPDATE;
207 		*bnp = bno;
208 		return (0);
209 	}
210 nospace:
211 #ifdef QUOTA
212 	UFS_UNLOCK(ump);
213 	/*
214 	 * Restore user's disk quota because allocation failed.
215 	 */
216 	(void) chkdq(ip, -btodb(size), cred, FORCE);
217 	UFS_LOCK(ump);
218 #endif
219 	if (reclaimed == 0 && (flags & IO_BUFLOCKED) == 0) {
220 		reclaimed = 1;
221 		softdep_request_cleanup(fs, ITOV(ip), cred, FLUSH_BLOCKS_WAIT);
222 		goto retry;
223 	}
224 	UFS_UNLOCK(ump);
225 	if (reclaimed > 0 && ppsratecheck(&lastfail, &curfail, 1)) {
226 		ffs_fserr(fs, ip->i_number, "filesystem full");
227 		uprintf("\n%s: write failed, filesystem is full\n",
228 		    fs->fs_fsmnt);
229 	}
230 	return (ENOSPC);
231 }
232 
233 /*
234  * Reallocate a fragment to a bigger size
235  *
236  * The number and size of the old block is given, and a preference
237  * and new size is also specified. The allocator attempts to extend
238  * the original block. Failing that, the regular block allocator is
239  * invoked to get an appropriate block.
240  */
241 int
242 ffs_realloccg(ip, lbprev, bprev, bpref, osize, nsize, flags, cred, bpp)
243 	struct inode *ip;
244 	ufs2_daddr_t lbprev;
245 	ufs2_daddr_t bprev;
246 	ufs2_daddr_t bpref;
247 	int osize, nsize, flags;
248 	struct ucred *cred;
249 	struct buf **bpp;
250 {
251 	struct vnode *vp;
252 	struct fs *fs;
253 	struct buf *bp;
254 	struct ufsmount *ump;
255 	u_int cg, request, reclaimed;
256 	int error, gbflags;
257 	ufs2_daddr_t bno;
258 	static struct timeval lastfail;
259 	static int curfail;
260 	int64_t delta;
261 
262 	vp = ITOV(ip);
263 	ump = ITOUMP(ip);
264 	fs = ump->um_fs;
265 	bp = NULL;
266 	gbflags = (flags & BA_UNMAPPED) != 0 ? GB_UNMAPPED : 0;
267 
268 	mtx_assert(UFS_MTX(ump), MA_OWNED);
269 #ifdef INVARIANTS
270 	if (vp->v_mount->mnt_kern_flag & MNTK_SUSPENDED)
271 		panic("ffs_realloccg: allocation on suspended filesystem");
272 	if ((u_int)osize > fs->fs_bsize || fragoff(fs, osize) != 0 ||
273 	    (u_int)nsize > fs->fs_bsize || fragoff(fs, nsize) != 0) {
274 		printf(
275 		"dev = %s, bsize = %ld, osize = %d, nsize = %d, fs = %s\n",
276 		    devtoname(ump->um_dev), (long)fs->fs_bsize, osize,
277 		    nsize, fs->fs_fsmnt);
278 		panic("ffs_realloccg: bad size");
279 	}
280 	if (cred == NOCRED)
281 		panic("ffs_realloccg: missing credential");
282 #endif /* INVARIANTS */
283 	reclaimed = 0;
284 retry:
285 	if (priv_check_cred(cred, PRIV_VFS_BLOCKRESERVE, 0) &&
286 	    freespace(fs, fs->fs_minfree) -  numfrags(fs, nsize - osize) < 0) {
287 		goto nospace;
288 	}
289 	if (bprev == 0) {
290 		printf("dev = %s, bsize = %ld, bprev = %jd, fs = %s\n",
291 		    devtoname(ump->um_dev), (long)fs->fs_bsize, (intmax_t)bprev,
292 		    fs->fs_fsmnt);
293 		panic("ffs_realloccg: bad bprev");
294 	}
295 	UFS_UNLOCK(ump);
296 	/*
297 	 * Allocate the extra space in the buffer.
298 	 */
299 	error = bread_gb(vp, lbprev, osize, NOCRED, gbflags, &bp);
300 	if (error) {
301 		brelse(bp);
302 		return (error);
303 	}
304 
305 	if (bp->b_blkno == bp->b_lblkno) {
306 		if (lbprev >= NDADDR)
307 			panic("ffs_realloccg: lbprev out of range");
308 		bp->b_blkno = fsbtodb(fs, bprev);
309 	}
310 
311 #ifdef QUOTA
312 	error = chkdq(ip, btodb(nsize - osize), cred, 0);
313 	if (error) {
314 		brelse(bp);
315 		return (error);
316 	}
317 #endif
318 	/*
319 	 * Check for extension in the existing location.
320 	 */
321 	*bpp = NULL;
322 	cg = dtog(fs, bprev);
323 	UFS_LOCK(ump);
324 	bno = ffs_fragextend(ip, cg, bprev, osize, nsize);
325 	if (bno) {
326 		if (bp->b_blkno != fsbtodb(fs, bno))
327 			panic("ffs_realloccg: bad blockno");
328 		delta = btodb(nsize - osize);
329 		DIP_SET(ip, i_blocks, DIP(ip, i_blocks) + delta);
330 		if (flags & IO_EXT)
331 			ip->i_flag |= IN_CHANGE;
332 		else
333 			ip->i_flag |= IN_CHANGE | IN_UPDATE;
334 		allocbuf(bp, nsize);
335 		bp->b_flags |= B_DONE;
336 		vfs_bio_bzero_buf(bp, osize, nsize - osize);
337 		if ((bp->b_flags & (B_MALLOC | B_VMIO)) == B_VMIO)
338 			vfs_bio_set_valid(bp, osize, nsize - osize);
339 		*bpp = bp;
340 		return (0);
341 	}
342 	/*
343 	 * Allocate a new disk location.
344 	 */
345 	if (bpref >= fs->fs_size)
346 		bpref = 0;
347 	switch ((int)fs->fs_optim) {
348 	case FS_OPTSPACE:
349 		/*
350 		 * Allocate an exact sized fragment. Although this makes
351 		 * best use of space, we will waste time relocating it if
352 		 * the file continues to grow. If the fragmentation is
353 		 * less than half of the minimum free reserve, we choose
354 		 * to begin optimizing for time.
355 		 */
356 		request = nsize;
357 		if (fs->fs_minfree <= 5 ||
358 		    fs->fs_cstotal.cs_nffree >
359 		    (off_t)fs->fs_dsize * fs->fs_minfree / (2 * 100))
360 			break;
361 		log(LOG_NOTICE, "%s: optimization changed from SPACE to TIME\n",
362 			fs->fs_fsmnt);
363 		fs->fs_optim = FS_OPTTIME;
364 		break;
365 	case FS_OPTTIME:
366 		/*
367 		 * At this point we have discovered a file that is trying to
368 		 * grow a small fragment to a larger fragment. To save time,
369 		 * we allocate a full sized block, then free the unused portion.
370 		 * If the file continues to grow, the `ffs_fragextend' call
371 		 * above will be able to grow it in place without further
372 		 * copying. If aberrant programs cause disk fragmentation to
373 		 * grow within 2% of the free reserve, we choose to begin
374 		 * optimizing for space.
375 		 */
376 		request = fs->fs_bsize;
377 		if (fs->fs_cstotal.cs_nffree <
378 		    (off_t)fs->fs_dsize * (fs->fs_minfree - 2) / 100)
379 			break;
380 		log(LOG_NOTICE, "%s: optimization changed from TIME to SPACE\n",
381 			fs->fs_fsmnt);
382 		fs->fs_optim = FS_OPTSPACE;
383 		break;
384 	default:
385 		printf("dev = %s, optim = %ld, fs = %s\n",
386 		    devtoname(ump->um_dev), (long)fs->fs_optim, fs->fs_fsmnt);
387 		panic("ffs_realloccg: bad optim");
388 		/* NOTREACHED */
389 	}
390 	bno = ffs_hashalloc(ip, cg, bpref, request, nsize, ffs_alloccg);
391 	if (bno > 0) {
392 		bp->b_blkno = fsbtodb(fs, bno);
393 		if (!DOINGSOFTDEP(vp))
394 			ffs_blkfree(ump, fs, ump->um_devvp, bprev, (long)osize,
395 			    ip->i_number, vp->v_type, NULL);
396 		delta = btodb(nsize - osize);
397 		DIP_SET(ip, i_blocks, DIP(ip, i_blocks) + delta);
398 		if (flags & IO_EXT)
399 			ip->i_flag |= IN_CHANGE;
400 		else
401 			ip->i_flag |= IN_CHANGE | IN_UPDATE;
402 		allocbuf(bp, nsize);
403 		bp->b_flags |= B_DONE;
404 		vfs_bio_bzero_buf(bp, osize, nsize - osize);
405 		if ((bp->b_flags & (B_MALLOC | B_VMIO)) == B_VMIO)
406 			vfs_bio_set_valid(bp, osize, nsize - osize);
407 		*bpp = bp;
408 		return (0);
409 	}
410 #ifdef QUOTA
411 	UFS_UNLOCK(ump);
412 	/*
413 	 * Restore user's disk quota because allocation failed.
414 	 */
415 	(void) chkdq(ip, -btodb(nsize - osize), cred, FORCE);
416 	UFS_LOCK(ump);
417 #endif
418 nospace:
419 	/*
420 	 * no space available
421 	 */
422 	if (reclaimed == 0 && (flags & IO_BUFLOCKED) == 0) {
423 		reclaimed = 1;
424 		UFS_UNLOCK(ump);
425 		if (bp) {
426 			brelse(bp);
427 			bp = NULL;
428 		}
429 		UFS_LOCK(ump);
430 		softdep_request_cleanup(fs, vp, cred, FLUSH_BLOCKS_WAIT);
431 		goto retry;
432 	}
433 	UFS_UNLOCK(ump);
434 	if (bp)
435 		brelse(bp);
436 	if (reclaimed > 0 && ppsratecheck(&lastfail, &curfail, 1)) {
437 		ffs_fserr(fs, ip->i_number, "filesystem full");
438 		uprintf("\n%s: write failed, filesystem is full\n",
439 		    fs->fs_fsmnt);
440 	}
441 	return (ENOSPC);
442 }
443 
444 /*
445  * Reallocate a sequence of blocks into a contiguous sequence of blocks.
446  *
447  * The vnode and an array of buffer pointers for a range of sequential
448  * logical blocks to be made contiguous is given. The allocator attempts
449  * to find a range of sequential blocks starting as close as possible
450  * from the end of the allocation for the logical block immediately
451  * preceding the current range. If successful, the physical block numbers
452  * in the buffer pointers and in the inode are changed to reflect the new
453  * allocation. If unsuccessful, the allocation is left unchanged. The
454  * success in doing the reallocation is returned. Note that the error
455  * return is not reflected back to the user. Rather the previous block
456  * allocation will be used.
457  */
458 
459 SYSCTL_NODE(_vfs, OID_AUTO, ffs, CTLFLAG_RW, 0, "FFS filesystem");
460 
461 static int doasyncfree = 1;
462 SYSCTL_INT(_vfs_ffs, OID_AUTO, doasyncfree, CTLFLAG_RW, &doasyncfree, 0,
463 "do not force synchronous writes when blocks are reallocated");
464 
465 static int doreallocblks = 1;
466 SYSCTL_INT(_vfs_ffs, OID_AUTO, doreallocblks, CTLFLAG_RW, &doreallocblks, 0,
467 "enable block reallocation");
468 
469 static int maxclustersearch = 10;
470 SYSCTL_INT(_vfs_ffs, OID_AUTO, maxclustersearch, CTLFLAG_RW, &maxclustersearch,
471 0, "max number of cylinder group to search for contigous blocks");
472 
473 #ifdef DEBUG
474 static volatile int prtrealloc = 0;
475 #endif
476 
477 int
478 ffs_reallocblks(ap)
479 	struct vop_reallocblks_args /* {
480 		struct vnode *a_vp;
481 		struct cluster_save *a_buflist;
482 	} */ *ap;
483 {
484 	struct ufsmount *ump;
485 
486 	/*
487 	 * If the underlying device can do deletes, then skip reallocating
488 	 * the blocks of this file into contiguous sequences. Devices that
489 	 * benefit from BIO_DELETE also benefit from not moving the data.
490 	 * These devices are flash and therefore work less well with this
491 	 * optimization. Also skip if reallocblks has been disabled globally.
492 	 */
493 	ump = ap->a_vp->v_mount->mnt_data;
494 	if (ump->um_candelete || doreallocblks == 0)
495 		return (ENOSPC);
496 
497 	/*
498 	 * We can't wait in softdep prealloc as it may fsync and recurse
499 	 * here.  Instead we simply fail to reallocate blocks if this
500 	 * rare condition arises.
501 	 */
502 	if (DOINGSOFTDEP(ap->a_vp))
503 		if (softdep_prealloc(ap->a_vp, MNT_NOWAIT) != 0)
504 			return (ENOSPC);
505 	if (ump->um_fstype == UFS1)
506 		return (ffs_reallocblks_ufs1(ap));
507 	return (ffs_reallocblks_ufs2(ap));
508 }
509 
510 static int
511 ffs_reallocblks_ufs1(ap)
512 	struct vop_reallocblks_args /* {
513 		struct vnode *a_vp;
514 		struct cluster_save *a_buflist;
515 	} */ *ap;
516 {
517 	struct fs *fs;
518 	struct inode *ip;
519 	struct vnode *vp;
520 	struct buf *sbp, *ebp;
521 	ufs1_daddr_t *bap, *sbap, *ebap;
522 	struct cluster_save *buflist;
523 	struct ufsmount *ump;
524 	ufs_lbn_t start_lbn, end_lbn;
525 	ufs1_daddr_t soff, newblk, blkno;
526 	ufs2_daddr_t pref;
527 	struct indir start_ap[NIADDR + 1], end_ap[NIADDR + 1], *idp;
528 	int i, cg, len, start_lvl, end_lvl, ssize;
529 
530 	vp = ap->a_vp;
531 	ip = VTOI(vp);
532 	ump = ITOUMP(ip);
533 	fs = ump->um_fs;
534 	/*
535 	 * If we are not tracking block clusters or if we have less than 4%
536 	 * free blocks left, then do not attempt to cluster. Running with
537 	 * less than 5% free block reserve is not recommended and those that
538 	 * choose to do so do not expect to have good file layout.
539 	 */
540 	if (fs->fs_contigsumsize <= 0 || freespace(fs, 4) < 0)
541 		return (ENOSPC);
542 	buflist = ap->a_buflist;
543 	len = buflist->bs_nchildren;
544 	start_lbn = buflist->bs_children[0]->b_lblkno;
545 	end_lbn = start_lbn + len - 1;
546 #ifdef INVARIANTS
547 	for (i = 0; i < len; i++)
548 		if (!ffs_checkblk(ip,
549 		   dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
550 			panic("ffs_reallocblks: unallocated block 1");
551 	for (i = 1; i < len; i++)
552 		if (buflist->bs_children[i]->b_lblkno != start_lbn + i)
553 			panic("ffs_reallocblks: non-logical cluster");
554 	blkno = buflist->bs_children[0]->b_blkno;
555 	ssize = fsbtodb(fs, fs->fs_frag);
556 	for (i = 1; i < len - 1; i++)
557 		if (buflist->bs_children[i]->b_blkno != blkno + (i * ssize))
558 			panic("ffs_reallocblks: non-physical cluster %d", i);
559 #endif
560 	/*
561 	 * If the cluster crosses the boundary for the first indirect
562 	 * block, leave space for the indirect block. Indirect blocks
563 	 * are initially laid out in a position after the last direct
564 	 * block. Block reallocation would usually destroy locality by
565 	 * moving the indirect block out of the way to make room for
566 	 * data blocks if we didn't compensate here. We should also do
567 	 * this for other indirect block boundaries, but it is only
568 	 * important for the first one.
569 	 */
570 	if (start_lbn < NDADDR && end_lbn >= NDADDR)
571 		return (ENOSPC);
572 	/*
573 	 * If the latest allocation is in a new cylinder group, assume that
574 	 * the filesystem has decided to move and do not force it back to
575 	 * the previous cylinder group.
576 	 */
577 	if (dtog(fs, dbtofsb(fs, buflist->bs_children[0]->b_blkno)) !=
578 	    dtog(fs, dbtofsb(fs, buflist->bs_children[len - 1]->b_blkno)))
579 		return (ENOSPC);
580 	if (ufs_getlbns(vp, start_lbn, start_ap, &start_lvl) ||
581 	    ufs_getlbns(vp, end_lbn, end_ap, &end_lvl))
582 		return (ENOSPC);
583 	/*
584 	 * Get the starting offset and block map for the first block.
585 	 */
586 	if (start_lvl == 0) {
587 		sbap = &ip->i_din1->di_db[0];
588 		soff = start_lbn;
589 	} else {
590 		idp = &start_ap[start_lvl - 1];
591 		if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &sbp)) {
592 			brelse(sbp);
593 			return (ENOSPC);
594 		}
595 		sbap = (ufs1_daddr_t *)sbp->b_data;
596 		soff = idp->in_off;
597 	}
598 	/*
599 	 * If the block range spans two block maps, get the second map.
600 	 */
601 	ebap = NULL;
602 	if (end_lvl == 0 || (idp = &end_ap[end_lvl - 1])->in_off + 1 >= len) {
603 		ssize = len;
604 	} else {
605 #ifdef INVARIANTS
606 		if (start_lvl > 0 &&
607 		    start_ap[start_lvl - 1].in_lbn == idp->in_lbn)
608 			panic("ffs_reallocblk: start == end");
609 #endif
610 		ssize = len - (idp->in_off + 1);
611 		if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &ebp))
612 			goto fail;
613 		ebap = (ufs1_daddr_t *)ebp->b_data;
614 	}
615 	/*
616 	 * Find the preferred location for the cluster. If we have not
617 	 * previously failed at this endeavor, then follow our standard
618 	 * preference calculation. If we have failed at it, then pick up
619 	 * where we last ended our search.
620 	 */
621 	UFS_LOCK(ump);
622 	if (ip->i_nextclustercg == -1)
623 		pref = ffs_blkpref_ufs1(ip, start_lbn, soff, sbap);
624 	else
625 		pref = cgdata(fs, ip->i_nextclustercg);
626 	/*
627 	 * Search the block map looking for an allocation of the desired size.
628 	 * To avoid wasting too much time, we limit the number of cylinder
629 	 * groups that we will search.
630 	 */
631 	cg = dtog(fs, pref);
632 	for (i = min(maxclustersearch, fs->fs_ncg); i > 0; i--) {
633 		if ((newblk = ffs_clusteralloc(ip, cg, pref, len)) != 0)
634 			break;
635 		cg += 1;
636 		if (cg >= fs->fs_ncg)
637 			cg = 0;
638 	}
639 	/*
640 	 * If we have failed in our search, record where we gave up for
641 	 * next time. Otherwise, fall back to our usual search citerion.
642 	 */
643 	if (newblk == 0) {
644 		ip->i_nextclustercg = cg;
645 		UFS_UNLOCK(ump);
646 		goto fail;
647 	}
648 	ip->i_nextclustercg = -1;
649 	/*
650 	 * We have found a new contiguous block.
651 	 *
652 	 * First we have to replace the old block pointers with the new
653 	 * block pointers in the inode and indirect blocks associated
654 	 * with the file.
655 	 */
656 #ifdef DEBUG
657 	if (prtrealloc)
658 		printf("realloc: ino %ju, lbns %jd-%jd\n\told:",
659 		    (uintmax_t)ip->i_number,
660 		    (intmax_t)start_lbn, (intmax_t)end_lbn);
661 #endif
662 	blkno = newblk;
663 	for (bap = &sbap[soff], i = 0; i < len; i++, blkno += fs->fs_frag) {
664 		if (i == ssize) {
665 			bap = ebap;
666 			soff = -i;
667 		}
668 #ifdef INVARIANTS
669 		if (!ffs_checkblk(ip,
670 		   dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
671 			panic("ffs_reallocblks: unallocated block 2");
672 		if (dbtofsb(fs, buflist->bs_children[i]->b_blkno) != *bap)
673 			panic("ffs_reallocblks: alloc mismatch");
674 #endif
675 #ifdef DEBUG
676 		if (prtrealloc)
677 			printf(" %d,", *bap);
678 #endif
679 		if (DOINGSOFTDEP(vp)) {
680 			if (sbap == &ip->i_din1->di_db[0] && i < ssize)
681 				softdep_setup_allocdirect(ip, start_lbn + i,
682 				    blkno, *bap, fs->fs_bsize, fs->fs_bsize,
683 				    buflist->bs_children[i]);
684 			else
685 				softdep_setup_allocindir_page(ip, start_lbn + i,
686 				    i < ssize ? sbp : ebp, soff + i, blkno,
687 				    *bap, buflist->bs_children[i]);
688 		}
689 		*bap++ = blkno;
690 	}
691 	/*
692 	 * Next we must write out the modified inode and indirect blocks.
693 	 * For strict correctness, the writes should be synchronous since
694 	 * the old block values may have been written to disk. In practise
695 	 * they are almost never written, but if we are concerned about
696 	 * strict correctness, the `doasyncfree' flag should be set to zero.
697 	 *
698 	 * The test on `doasyncfree' should be changed to test a flag
699 	 * that shows whether the associated buffers and inodes have
700 	 * been written. The flag should be set when the cluster is
701 	 * started and cleared whenever the buffer or inode is flushed.
702 	 * We can then check below to see if it is set, and do the
703 	 * synchronous write only when it has been cleared.
704 	 */
705 	if (sbap != &ip->i_din1->di_db[0]) {
706 		if (doasyncfree)
707 			bdwrite(sbp);
708 		else
709 			bwrite(sbp);
710 	} else {
711 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
712 		if (!doasyncfree)
713 			ffs_update(vp, 1);
714 	}
715 	if (ssize < len) {
716 		if (doasyncfree)
717 			bdwrite(ebp);
718 		else
719 			bwrite(ebp);
720 	}
721 	/*
722 	 * Last, free the old blocks and assign the new blocks to the buffers.
723 	 */
724 #ifdef DEBUG
725 	if (prtrealloc)
726 		printf("\n\tnew:");
727 #endif
728 	for (blkno = newblk, i = 0; i < len; i++, blkno += fs->fs_frag) {
729 		if (!DOINGSOFTDEP(vp))
730 			ffs_blkfree(ump, fs, ump->um_devvp,
731 			    dbtofsb(fs, buflist->bs_children[i]->b_blkno),
732 			    fs->fs_bsize, ip->i_number, vp->v_type, NULL);
733 		buflist->bs_children[i]->b_blkno = fsbtodb(fs, blkno);
734 #ifdef INVARIANTS
735 		if (!ffs_checkblk(ip,
736 		   dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
737 			panic("ffs_reallocblks: unallocated block 3");
738 #endif
739 #ifdef DEBUG
740 		if (prtrealloc)
741 			printf(" %d,", blkno);
742 #endif
743 	}
744 #ifdef DEBUG
745 	if (prtrealloc) {
746 		prtrealloc--;
747 		printf("\n");
748 	}
749 #endif
750 	return (0);
751 
752 fail:
753 	if (ssize < len)
754 		brelse(ebp);
755 	if (sbap != &ip->i_din1->di_db[0])
756 		brelse(sbp);
757 	return (ENOSPC);
758 }
759 
760 static int
761 ffs_reallocblks_ufs2(ap)
762 	struct vop_reallocblks_args /* {
763 		struct vnode *a_vp;
764 		struct cluster_save *a_buflist;
765 	} */ *ap;
766 {
767 	struct fs *fs;
768 	struct inode *ip;
769 	struct vnode *vp;
770 	struct buf *sbp, *ebp;
771 	ufs2_daddr_t *bap, *sbap, *ebap;
772 	struct cluster_save *buflist;
773 	struct ufsmount *ump;
774 	ufs_lbn_t start_lbn, end_lbn;
775 	ufs2_daddr_t soff, newblk, blkno, pref;
776 	struct indir start_ap[NIADDR + 1], end_ap[NIADDR + 1], *idp;
777 	int i, cg, len, start_lvl, end_lvl, ssize;
778 
779 	vp = ap->a_vp;
780 	ip = VTOI(vp);
781 	ump = ITOUMP(ip);
782 	fs = ump->um_fs;
783 	/*
784 	 * If we are not tracking block clusters or if we have less than 4%
785 	 * free blocks left, then do not attempt to cluster. Running with
786 	 * less than 5% free block reserve is not recommended and those that
787 	 * choose to do so do not expect to have good file layout.
788 	 */
789 	if (fs->fs_contigsumsize <= 0 || freespace(fs, 4) < 0)
790 		return (ENOSPC);
791 	buflist = ap->a_buflist;
792 	len = buflist->bs_nchildren;
793 	start_lbn = buflist->bs_children[0]->b_lblkno;
794 	end_lbn = start_lbn + len - 1;
795 #ifdef INVARIANTS
796 	for (i = 0; i < len; i++)
797 		if (!ffs_checkblk(ip,
798 		   dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
799 			panic("ffs_reallocblks: unallocated block 1");
800 	for (i = 1; i < len; i++)
801 		if (buflist->bs_children[i]->b_lblkno != start_lbn + i)
802 			panic("ffs_reallocblks: non-logical cluster");
803 	blkno = buflist->bs_children[0]->b_blkno;
804 	ssize = fsbtodb(fs, fs->fs_frag);
805 	for (i = 1; i < len - 1; i++)
806 		if (buflist->bs_children[i]->b_blkno != blkno + (i * ssize))
807 			panic("ffs_reallocblks: non-physical cluster %d", i);
808 #endif
809 	/*
810 	 * If the cluster crosses the boundary for the first indirect
811 	 * block, do not move anything in it. Indirect blocks are
812 	 * usually initially laid out in a position between the data
813 	 * blocks. Block reallocation would usually destroy locality by
814 	 * moving the indirect block out of the way to make room for
815 	 * data blocks if we didn't compensate here. We should also do
816 	 * this for other indirect block boundaries, but it is only
817 	 * important for the first one.
818 	 */
819 	if (start_lbn < NDADDR && end_lbn >= NDADDR)
820 		return (ENOSPC);
821 	/*
822 	 * If the latest allocation is in a new cylinder group, assume that
823 	 * the filesystem has decided to move and do not force it back to
824 	 * the previous cylinder group.
825 	 */
826 	if (dtog(fs, dbtofsb(fs, buflist->bs_children[0]->b_blkno)) !=
827 	    dtog(fs, dbtofsb(fs, buflist->bs_children[len - 1]->b_blkno)))
828 		return (ENOSPC);
829 	if (ufs_getlbns(vp, start_lbn, start_ap, &start_lvl) ||
830 	    ufs_getlbns(vp, end_lbn, end_ap, &end_lvl))
831 		return (ENOSPC);
832 	/*
833 	 * Get the starting offset and block map for the first block.
834 	 */
835 	if (start_lvl == 0) {
836 		sbap = &ip->i_din2->di_db[0];
837 		soff = start_lbn;
838 	} else {
839 		idp = &start_ap[start_lvl - 1];
840 		if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &sbp)) {
841 			brelse(sbp);
842 			return (ENOSPC);
843 		}
844 		sbap = (ufs2_daddr_t *)sbp->b_data;
845 		soff = idp->in_off;
846 	}
847 	/*
848 	 * If the block range spans two block maps, get the second map.
849 	 */
850 	ebap = NULL;
851 	if (end_lvl == 0 || (idp = &end_ap[end_lvl - 1])->in_off + 1 >= len) {
852 		ssize = len;
853 	} else {
854 #ifdef INVARIANTS
855 		if (start_lvl > 0 &&
856 		    start_ap[start_lvl - 1].in_lbn == idp->in_lbn)
857 			panic("ffs_reallocblk: start == end");
858 #endif
859 		ssize = len - (idp->in_off + 1);
860 		if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &ebp))
861 			goto fail;
862 		ebap = (ufs2_daddr_t *)ebp->b_data;
863 	}
864 	/*
865 	 * Find the preferred location for the cluster. If we have not
866 	 * previously failed at this endeavor, then follow our standard
867 	 * preference calculation. If we have failed at it, then pick up
868 	 * where we last ended our search.
869 	 */
870 	UFS_LOCK(ump);
871 	if (ip->i_nextclustercg == -1)
872 		pref = ffs_blkpref_ufs2(ip, start_lbn, soff, sbap);
873 	else
874 		pref = cgdata(fs, ip->i_nextclustercg);
875 	/*
876 	 * Search the block map looking for an allocation of the desired size.
877 	 * To avoid wasting too much time, we limit the number of cylinder
878 	 * groups that we will search.
879 	 */
880 	cg = dtog(fs, pref);
881 	for (i = min(maxclustersearch, fs->fs_ncg); i > 0; i--) {
882 		if ((newblk = ffs_clusteralloc(ip, cg, pref, len)) != 0)
883 			break;
884 		cg += 1;
885 		if (cg >= fs->fs_ncg)
886 			cg = 0;
887 	}
888 	/*
889 	 * If we have failed in our search, record where we gave up for
890 	 * next time. Otherwise, fall back to our usual search citerion.
891 	 */
892 	if (newblk == 0) {
893 		ip->i_nextclustercg = cg;
894 		UFS_UNLOCK(ump);
895 		goto fail;
896 	}
897 	ip->i_nextclustercg = -1;
898 	/*
899 	 * We have found a new contiguous block.
900 	 *
901 	 * First we have to replace the old block pointers with the new
902 	 * block pointers in the inode and indirect blocks associated
903 	 * with the file.
904 	 */
905 #ifdef DEBUG
906 	if (prtrealloc)
907 		printf("realloc: ino %ju, lbns %jd-%jd\n\told:", (uintmax_t)ip->i_number,
908 		    (intmax_t)start_lbn, (intmax_t)end_lbn);
909 #endif
910 	blkno = newblk;
911 	for (bap = &sbap[soff], i = 0; i < len; i++, blkno += fs->fs_frag) {
912 		if (i == ssize) {
913 			bap = ebap;
914 			soff = -i;
915 		}
916 #ifdef INVARIANTS
917 		if (!ffs_checkblk(ip,
918 		   dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
919 			panic("ffs_reallocblks: unallocated block 2");
920 		if (dbtofsb(fs, buflist->bs_children[i]->b_blkno) != *bap)
921 			panic("ffs_reallocblks: alloc mismatch");
922 #endif
923 #ifdef DEBUG
924 		if (prtrealloc)
925 			printf(" %jd,", (intmax_t)*bap);
926 #endif
927 		if (DOINGSOFTDEP(vp)) {
928 			if (sbap == &ip->i_din2->di_db[0] && i < ssize)
929 				softdep_setup_allocdirect(ip, start_lbn + i,
930 				    blkno, *bap, fs->fs_bsize, fs->fs_bsize,
931 				    buflist->bs_children[i]);
932 			else
933 				softdep_setup_allocindir_page(ip, start_lbn + i,
934 				    i < ssize ? sbp : ebp, soff + i, blkno,
935 				    *bap, buflist->bs_children[i]);
936 		}
937 		*bap++ = blkno;
938 	}
939 	/*
940 	 * Next we must write out the modified inode and indirect blocks.
941 	 * For strict correctness, the writes should be synchronous since
942 	 * the old block values may have been written to disk. In practise
943 	 * they are almost never written, but if we are concerned about
944 	 * strict correctness, the `doasyncfree' flag should be set to zero.
945 	 *
946 	 * The test on `doasyncfree' should be changed to test a flag
947 	 * that shows whether the associated buffers and inodes have
948 	 * been written. The flag should be set when the cluster is
949 	 * started and cleared whenever the buffer or inode is flushed.
950 	 * We can then check below to see if it is set, and do the
951 	 * synchronous write only when it has been cleared.
952 	 */
953 	if (sbap != &ip->i_din2->di_db[0]) {
954 		if (doasyncfree)
955 			bdwrite(sbp);
956 		else
957 			bwrite(sbp);
958 	} else {
959 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
960 		if (!doasyncfree)
961 			ffs_update(vp, 1);
962 	}
963 	if (ssize < len) {
964 		if (doasyncfree)
965 			bdwrite(ebp);
966 		else
967 			bwrite(ebp);
968 	}
969 	/*
970 	 * Last, free the old blocks and assign the new blocks to the buffers.
971 	 */
972 #ifdef DEBUG
973 	if (prtrealloc)
974 		printf("\n\tnew:");
975 #endif
976 	for (blkno = newblk, i = 0; i < len; i++, blkno += fs->fs_frag) {
977 		if (!DOINGSOFTDEP(vp))
978 			ffs_blkfree(ump, fs, ump->um_devvp,
979 			    dbtofsb(fs, buflist->bs_children[i]->b_blkno),
980 			    fs->fs_bsize, ip->i_number, vp->v_type, NULL);
981 		buflist->bs_children[i]->b_blkno = fsbtodb(fs, blkno);
982 #ifdef INVARIANTS
983 		if (!ffs_checkblk(ip,
984 		   dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
985 			panic("ffs_reallocblks: unallocated block 3");
986 #endif
987 #ifdef DEBUG
988 		if (prtrealloc)
989 			printf(" %jd,", (intmax_t)blkno);
990 #endif
991 	}
992 #ifdef DEBUG
993 	if (prtrealloc) {
994 		prtrealloc--;
995 		printf("\n");
996 	}
997 #endif
998 	return (0);
999 
1000 fail:
1001 	if (ssize < len)
1002 		brelse(ebp);
1003 	if (sbap != &ip->i_din2->di_db[0])
1004 		brelse(sbp);
1005 	return (ENOSPC);
1006 }
1007 
1008 /*
1009  * Allocate an inode in the filesystem.
1010  *
1011  * If allocating a directory, use ffs_dirpref to select the inode.
1012  * If allocating in a directory, the following hierarchy is followed:
1013  *   1) allocate the preferred inode.
1014  *   2) allocate an inode in the same cylinder group.
1015  *   3) quadradically rehash into other cylinder groups, until an
1016  *      available inode is located.
1017  * If no inode preference is given the following hierarchy is used
1018  * to allocate an inode:
1019  *   1) allocate an inode in cylinder group 0.
1020  *   2) quadradically rehash into other cylinder groups, until an
1021  *      available inode is located.
1022  */
1023 int
1024 ffs_valloc(pvp, mode, cred, vpp)
1025 	struct vnode *pvp;
1026 	int mode;
1027 	struct ucred *cred;
1028 	struct vnode **vpp;
1029 {
1030 	struct inode *pip;
1031 	struct fs *fs;
1032 	struct inode *ip;
1033 	struct timespec ts;
1034 	struct ufsmount *ump;
1035 	ino_t ino, ipref;
1036 	u_int cg;
1037 	int error, error1, reclaimed;
1038 	static struct timeval lastfail;
1039 	static int curfail;
1040 
1041 	*vpp = NULL;
1042 	pip = VTOI(pvp);
1043 	ump = ITOUMP(pip);
1044 	fs = ump->um_fs;
1045 
1046 	UFS_LOCK(ump);
1047 	reclaimed = 0;
1048 retry:
1049 	if (fs->fs_cstotal.cs_nifree == 0)
1050 		goto noinodes;
1051 
1052 	if ((mode & IFMT) == IFDIR)
1053 		ipref = ffs_dirpref(pip);
1054 	else
1055 		ipref = pip->i_number;
1056 	if (ipref >= fs->fs_ncg * fs->fs_ipg)
1057 		ipref = 0;
1058 	cg = ino_to_cg(fs, ipref);
1059 	/*
1060 	 * Track number of dirs created one after another
1061 	 * in a same cg without intervening by files.
1062 	 */
1063 	if ((mode & IFMT) == IFDIR) {
1064 		if (fs->fs_contigdirs[cg] < 255)
1065 			fs->fs_contigdirs[cg]++;
1066 	} else {
1067 		if (fs->fs_contigdirs[cg] > 0)
1068 			fs->fs_contigdirs[cg]--;
1069 	}
1070 	ino = (ino_t)ffs_hashalloc(pip, cg, ipref, mode, 0,
1071 					(allocfcn_t *)ffs_nodealloccg);
1072 	if (ino == 0)
1073 		goto noinodes;
1074 	error = ffs_vget(pvp->v_mount, ino, LK_EXCLUSIVE, vpp);
1075 	if (error) {
1076 		error1 = ffs_vgetf(pvp->v_mount, ino, LK_EXCLUSIVE, vpp,
1077 		    FFSV_FORCEINSMQ);
1078 		ffs_vfree(pvp, ino, mode);
1079 		if (error1 == 0) {
1080 			ip = VTOI(*vpp);
1081 			if (ip->i_mode)
1082 				goto dup_alloc;
1083 			ip->i_flag |= IN_MODIFIED;
1084 			vput(*vpp);
1085 		}
1086 		return (error);
1087 	}
1088 	ip = VTOI(*vpp);
1089 	if (ip->i_mode) {
1090 dup_alloc:
1091 		printf("mode = 0%o, inum = %ju, fs = %s\n",
1092 		    ip->i_mode, (uintmax_t)ip->i_number, fs->fs_fsmnt);
1093 		panic("ffs_valloc: dup alloc");
1094 	}
1095 	if (DIP(ip, i_blocks) && (fs->fs_flags & FS_UNCLEAN) == 0) {  /* XXX */
1096 		printf("free inode %s/%lu had %ld blocks\n",
1097 		    fs->fs_fsmnt, (u_long)ino, (long)DIP(ip, i_blocks));
1098 		DIP_SET(ip, i_blocks, 0);
1099 	}
1100 	ip->i_flags = 0;
1101 	DIP_SET(ip, i_flags, 0);
1102 	/*
1103 	 * Set up a new generation number for this inode.
1104 	 */
1105 	while (ip->i_gen == 0 || ++ip->i_gen == 0)
1106 		ip->i_gen = arc4random();
1107 	DIP_SET(ip, i_gen, ip->i_gen);
1108 	if (fs->fs_magic == FS_UFS2_MAGIC) {
1109 		vfs_timestamp(&ts);
1110 		ip->i_din2->di_birthtime = ts.tv_sec;
1111 		ip->i_din2->di_birthnsec = ts.tv_nsec;
1112 	}
1113 	ufs_prepare_reclaim(*vpp);
1114 	ip->i_flag = 0;
1115 	(*vpp)->v_vflag = 0;
1116 	(*vpp)->v_type = VNON;
1117 	if (fs->fs_magic == FS_UFS2_MAGIC) {
1118 		(*vpp)->v_op = &ffs_vnodeops2;
1119 		ip->i_flag |= IN_UFS2;
1120 	} else {
1121 		(*vpp)->v_op = &ffs_vnodeops1;
1122 	}
1123 	return (0);
1124 noinodes:
1125 	if (reclaimed == 0) {
1126 		reclaimed = 1;
1127 		softdep_request_cleanup(fs, pvp, cred, FLUSH_INODES_WAIT);
1128 		goto retry;
1129 	}
1130 	UFS_UNLOCK(ump);
1131 	if (ppsratecheck(&lastfail, &curfail, 1)) {
1132 		ffs_fserr(fs, pip->i_number, "out of inodes");
1133 		uprintf("\n%s: create/symlink failed, no inodes free\n",
1134 		    fs->fs_fsmnt);
1135 	}
1136 	return (ENOSPC);
1137 }
1138 
1139 /*
1140  * Find a cylinder group to place a directory.
1141  *
1142  * The policy implemented by this algorithm is to allocate a
1143  * directory inode in the same cylinder group as its parent
1144  * directory, but also to reserve space for its files inodes
1145  * and data. Restrict the number of directories which may be
1146  * allocated one after another in the same cylinder group
1147  * without intervening allocation of files.
1148  *
1149  * If we allocate a first level directory then force allocation
1150  * in another cylinder group.
1151  */
1152 static ino_t
1153 ffs_dirpref(pip)
1154 	struct inode *pip;
1155 {
1156 	struct fs *fs;
1157 	int cg, prefcg, dirsize, cgsize;
1158 	u_int avgifree, avgbfree, avgndir, curdirsize;
1159 	u_int minifree, minbfree, maxndir;
1160 	u_int mincg, minndir;
1161 	u_int maxcontigdirs;
1162 
1163 	mtx_assert(UFS_MTX(ITOUMP(pip)), MA_OWNED);
1164 	fs = ITOFS(pip);
1165 
1166 	avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg;
1167 	avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
1168 	avgndir = fs->fs_cstotal.cs_ndir / fs->fs_ncg;
1169 
1170 	/*
1171 	 * Force allocation in another cg if creating a first level dir.
1172 	 */
1173 	ASSERT_VOP_LOCKED(ITOV(pip), "ffs_dirpref");
1174 	if (ITOV(pip)->v_vflag & VV_ROOT) {
1175 		prefcg = arc4random() % fs->fs_ncg;
1176 		mincg = prefcg;
1177 		minndir = fs->fs_ipg;
1178 		for (cg = prefcg; cg < fs->fs_ncg; cg++)
1179 			if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
1180 			    fs->fs_cs(fs, cg).cs_nifree >= avgifree &&
1181 			    fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
1182 				mincg = cg;
1183 				minndir = fs->fs_cs(fs, cg).cs_ndir;
1184 			}
1185 		for (cg = 0; cg < prefcg; cg++)
1186 			if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
1187 			    fs->fs_cs(fs, cg).cs_nifree >= avgifree &&
1188 			    fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
1189 				mincg = cg;
1190 				minndir = fs->fs_cs(fs, cg).cs_ndir;
1191 			}
1192 		return ((ino_t)(fs->fs_ipg * mincg));
1193 	}
1194 
1195 	/*
1196 	 * Count various limits which used for
1197 	 * optimal allocation of a directory inode.
1198 	 */
1199 	maxndir = min(avgndir + fs->fs_ipg / 16, fs->fs_ipg);
1200 	minifree = avgifree - avgifree / 4;
1201 	if (minifree < 1)
1202 		minifree = 1;
1203 	minbfree = avgbfree - avgbfree / 4;
1204 	if (minbfree < 1)
1205 		minbfree = 1;
1206 	cgsize = fs->fs_fsize * fs->fs_fpg;
1207 	dirsize = fs->fs_avgfilesize * fs->fs_avgfpdir;
1208 	curdirsize = avgndir ? (cgsize - avgbfree * fs->fs_bsize) / avgndir : 0;
1209 	if (dirsize < curdirsize)
1210 		dirsize = curdirsize;
1211 	if (dirsize <= 0)
1212 		maxcontigdirs = 0;		/* dirsize overflowed */
1213 	else
1214 		maxcontigdirs = min((avgbfree * fs->fs_bsize) / dirsize, 255);
1215 	if (fs->fs_avgfpdir > 0)
1216 		maxcontigdirs = min(maxcontigdirs,
1217 				    fs->fs_ipg / fs->fs_avgfpdir);
1218 	if (maxcontigdirs == 0)
1219 		maxcontigdirs = 1;
1220 
1221 	/*
1222 	 * Limit number of dirs in one cg and reserve space for
1223 	 * regular files, but only if we have no deficit in
1224 	 * inodes or space.
1225 	 *
1226 	 * We are trying to find a suitable cylinder group nearby
1227 	 * our preferred cylinder group to place a new directory.
1228 	 * We scan from our preferred cylinder group forward looking
1229 	 * for a cylinder group that meets our criterion. If we get
1230 	 * to the final cylinder group and do not find anything,
1231 	 * we start scanning forwards from the beginning of the
1232 	 * filesystem. While it might seem sensible to start scanning
1233 	 * backwards or even to alternate looking forward and backward,
1234 	 * this approach fails badly when the filesystem is nearly full.
1235 	 * Specifically, we first search all the areas that have no space
1236 	 * and finally try the one preceding that. We repeat this on
1237 	 * every request and in the case of the final block end up
1238 	 * searching the entire filesystem. By jumping to the front
1239 	 * of the filesystem, our future forward searches always look
1240 	 * in new cylinder groups so finds every possible block after
1241 	 * one pass over the filesystem.
1242 	 */
1243 	prefcg = ino_to_cg(fs, pip->i_number);
1244 	for (cg = prefcg; cg < fs->fs_ncg; cg++)
1245 		if (fs->fs_cs(fs, cg).cs_ndir < maxndir &&
1246 		    fs->fs_cs(fs, cg).cs_nifree >= minifree &&
1247 		    fs->fs_cs(fs, cg).cs_nbfree >= minbfree) {
1248 			if (fs->fs_contigdirs[cg] < maxcontigdirs)
1249 				return ((ino_t)(fs->fs_ipg * cg));
1250 		}
1251 	for (cg = 0; cg < prefcg; cg++)
1252 		if (fs->fs_cs(fs, cg).cs_ndir < maxndir &&
1253 		    fs->fs_cs(fs, cg).cs_nifree >= minifree &&
1254 		    fs->fs_cs(fs, cg).cs_nbfree >= minbfree) {
1255 			if (fs->fs_contigdirs[cg] < maxcontigdirs)
1256 				return ((ino_t)(fs->fs_ipg * cg));
1257 		}
1258 	/*
1259 	 * This is a backstop when we have deficit in space.
1260 	 */
1261 	for (cg = prefcg; cg < fs->fs_ncg; cg++)
1262 		if (fs->fs_cs(fs, cg).cs_nifree >= avgifree)
1263 			return ((ino_t)(fs->fs_ipg * cg));
1264 	for (cg = 0; cg < prefcg; cg++)
1265 		if (fs->fs_cs(fs, cg).cs_nifree >= avgifree)
1266 			break;
1267 	return ((ino_t)(fs->fs_ipg * cg));
1268 }
1269 
1270 /*
1271  * Select the desired position for the next block in a file.  The file is
1272  * logically divided into sections. The first section is composed of the
1273  * direct blocks and the next fs_maxbpg blocks. Each additional section
1274  * contains fs_maxbpg blocks.
1275  *
1276  * If no blocks have been allocated in the first section, the policy is to
1277  * request a block in the same cylinder group as the inode that describes
1278  * the file. The first indirect is allocated immediately following the last
1279  * direct block and the data blocks for the first indirect immediately
1280  * follow it.
1281  *
1282  * If no blocks have been allocated in any other section, the indirect
1283  * block(s) are allocated in the same cylinder group as its inode in an
1284  * area reserved immediately following the inode blocks. The policy for
1285  * the data blocks is to place them in a cylinder group with a greater than
1286  * average number of free blocks. An appropriate cylinder group is found
1287  * by using a rotor that sweeps the cylinder groups. When a new group of
1288  * blocks is needed, the sweep begins in the cylinder group following the
1289  * cylinder group from which the previous allocation was made. The sweep
1290  * continues until a cylinder group with greater than the average number
1291  * of free blocks is found. If the allocation is for the first block in an
1292  * indirect block or the previous block is a hole, then the information on
1293  * the previous allocation is unavailable; here a best guess is made based
1294  * on the logical block number being allocated.
1295  *
1296  * If a section is already partially allocated, the policy is to
1297  * allocate blocks contiguously within the section if possible.
1298  */
1299 ufs2_daddr_t
1300 ffs_blkpref_ufs1(ip, lbn, indx, bap)
1301 	struct inode *ip;
1302 	ufs_lbn_t lbn;
1303 	int indx;
1304 	ufs1_daddr_t *bap;
1305 {
1306 	struct fs *fs;
1307 	u_int cg, inocg;
1308 	u_int avgbfree, startcg;
1309 	ufs2_daddr_t pref;
1310 
1311 	KASSERT(indx <= 0 || bap != NULL, ("need non-NULL bap"));
1312 	mtx_assert(UFS_MTX(ITOUMP(ip)), MA_OWNED);
1313 	fs = ITOFS(ip);
1314 	/*
1315 	 * Allocation of indirect blocks is indicated by passing negative
1316 	 * values in indx: -1 for single indirect, -2 for double indirect,
1317 	 * -3 for triple indirect. As noted below, we attempt to allocate
1318 	 * the first indirect inline with the file data. For all later
1319 	 * indirect blocks, the data is often allocated in other cylinder
1320 	 * groups. However to speed random file access and to speed up
1321 	 * fsck, the filesystem reserves the first fs_metaspace blocks
1322 	 * (typically half of fs_minfree) of the data area of each cylinder
1323 	 * group to hold these later indirect blocks.
1324 	 */
1325 	inocg = ino_to_cg(fs, ip->i_number);
1326 	if (indx < 0) {
1327 		/*
1328 		 * Our preference for indirect blocks is the zone at the
1329 		 * beginning of the inode's cylinder group data area that
1330 		 * we try to reserve for indirect blocks.
1331 		 */
1332 		pref = cgmeta(fs, inocg);
1333 		/*
1334 		 * If we are allocating the first indirect block, try to
1335 		 * place it immediately following the last direct block.
1336 		 */
1337 		if (indx == -1 && lbn < NDADDR + NINDIR(fs) &&
1338 		    ip->i_din1->di_db[NDADDR - 1] != 0)
1339 			pref = ip->i_din1->di_db[NDADDR - 1] + fs->fs_frag;
1340 		return (pref);
1341 	}
1342 	/*
1343 	 * If we are allocating the first data block in the first indirect
1344 	 * block and the indirect has been allocated in the data block area,
1345 	 * try to place it immediately following the indirect block.
1346 	 */
1347 	if (lbn == NDADDR) {
1348 		pref = ip->i_din1->di_ib[0];
1349 		if (pref != 0 && pref >= cgdata(fs, inocg) &&
1350 		    pref < cgbase(fs, inocg + 1))
1351 			return (pref + fs->fs_frag);
1352 	}
1353 	/*
1354 	 * If we are at the beginning of a file, or we have already allocated
1355 	 * the maximum number of blocks per cylinder group, or we do not
1356 	 * have a block allocated immediately preceding us, then we need
1357 	 * to decide where to start allocating new blocks.
1358 	 */
1359 	if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
1360 		/*
1361 		 * If we are allocating a directory data block, we want
1362 		 * to place it in the metadata area.
1363 		 */
1364 		if ((ip->i_mode & IFMT) == IFDIR)
1365 			return (cgmeta(fs, inocg));
1366 		/*
1367 		 * Until we fill all the direct and all the first indirect's
1368 		 * blocks, we try to allocate in the data area of the inode's
1369 		 * cylinder group.
1370 		 */
1371 		if (lbn < NDADDR + NINDIR(fs))
1372 			return (cgdata(fs, inocg));
1373 		/*
1374 		 * Find a cylinder with greater than average number of
1375 		 * unused data blocks.
1376 		 */
1377 		if (indx == 0 || bap[indx - 1] == 0)
1378 			startcg = inocg + lbn / fs->fs_maxbpg;
1379 		else
1380 			startcg = dtog(fs, bap[indx - 1]) + 1;
1381 		startcg %= fs->fs_ncg;
1382 		avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
1383 		for (cg = startcg; cg < fs->fs_ncg; cg++)
1384 			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
1385 				fs->fs_cgrotor = cg;
1386 				return (cgdata(fs, cg));
1387 			}
1388 		for (cg = 0; cg <= startcg; cg++)
1389 			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
1390 				fs->fs_cgrotor = cg;
1391 				return (cgdata(fs, cg));
1392 			}
1393 		return (0);
1394 	}
1395 	/*
1396 	 * Otherwise, we just always try to lay things out contiguously.
1397 	 */
1398 	return (bap[indx - 1] + fs->fs_frag);
1399 }
1400 
1401 /*
1402  * Same as above, but for UFS2
1403  */
1404 ufs2_daddr_t
1405 ffs_blkpref_ufs2(ip, lbn, indx, bap)
1406 	struct inode *ip;
1407 	ufs_lbn_t lbn;
1408 	int indx;
1409 	ufs2_daddr_t *bap;
1410 {
1411 	struct fs *fs;
1412 	u_int cg, inocg;
1413 	u_int avgbfree, startcg;
1414 	ufs2_daddr_t pref;
1415 
1416 	KASSERT(indx <= 0 || bap != NULL, ("need non-NULL bap"));
1417 	mtx_assert(UFS_MTX(ITOUMP(ip)), MA_OWNED);
1418 	fs = ITOFS(ip);
1419 	/*
1420 	 * Allocation of indirect blocks is indicated by passing negative
1421 	 * values in indx: -1 for single indirect, -2 for double indirect,
1422 	 * -3 for triple indirect. As noted below, we attempt to allocate
1423 	 * the first indirect inline with the file data. For all later
1424 	 * indirect blocks, the data is often allocated in other cylinder
1425 	 * groups. However to speed random file access and to speed up
1426 	 * fsck, the filesystem reserves the first fs_metaspace blocks
1427 	 * (typically half of fs_minfree) of the data area of each cylinder
1428 	 * group to hold these later indirect blocks.
1429 	 */
1430 	inocg = ino_to_cg(fs, ip->i_number);
1431 	if (indx < 0) {
1432 		/*
1433 		 * Our preference for indirect blocks is the zone at the
1434 		 * beginning of the inode's cylinder group data area that
1435 		 * we try to reserve for indirect blocks.
1436 		 */
1437 		pref = cgmeta(fs, inocg);
1438 		/*
1439 		 * If we are allocating the first indirect block, try to
1440 		 * place it immediately following the last direct block.
1441 		 */
1442 		if (indx == -1 && lbn < NDADDR + NINDIR(fs) &&
1443 		    ip->i_din2->di_db[NDADDR - 1] != 0)
1444 			pref = ip->i_din2->di_db[NDADDR - 1] + fs->fs_frag;
1445 		return (pref);
1446 	}
1447 	/*
1448 	 * If we are allocating the first data block in the first indirect
1449 	 * block and the indirect has been allocated in the data block area,
1450 	 * try to place it immediately following the indirect block.
1451 	 */
1452 	if (lbn == NDADDR) {
1453 		pref = ip->i_din2->di_ib[0];
1454 		if (pref != 0 && pref >= cgdata(fs, inocg) &&
1455 		    pref < cgbase(fs, inocg + 1))
1456 			return (pref + fs->fs_frag);
1457 	}
1458 	/*
1459 	 * If we are at the beginning of a file, or we have already allocated
1460 	 * the maximum number of blocks per cylinder group, or we do not
1461 	 * have a block allocated immediately preceding us, then we need
1462 	 * to decide where to start allocating new blocks.
1463 	 */
1464 	if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
1465 		/*
1466 		 * If we are allocating a directory data block, we want
1467 		 * to place it in the metadata area.
1468 		 */
1469 		if ((ip->i_mode & IFMT) == IFDIR)
1470 			return (cgmeta(fs, inocg));
1471 		/*
1472 		 * Until we fill all the direct and all the first indirect's
1473 		 * blocks, we try to allocate in the data area of the inode's
1474 		 * cylinder group.
1475 		 */
1476 		if (lbn < NDADDR + NINDIR(fs))
1477 			return (cgdata(fs, inocg));
1478 		/*
1479 		 * Find a cylinder with greater than average number of
1480 		 * unused data blocks.
1481 		 */
1482 		if (indx == 0 || bap[indx - 1] == 0)
1483 			startcg = inocg + lbn / fs->fs_maxbpg;
1484 		else
1485 			startcg = dtog(fs, bap[indx - 1]) + 1;
1486 		startcg %= fs->fs_ncg;
1487 		avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
1488 		for (cg = startcg; cg < fs->fs_ncg; cg++)
1489 			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
1490 				fs->fs_cgrotor = cg;
1491 				return (cgdata(fs, cg));
1492 			}
1493 		for (cg = 0; cg <= startcg; cg++)
1494 			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
1495 				fs->fs_cgrotor = cg;
1496 				return (cgdata(fs, cg));
1497 			}
1498 		return (0);
1499 	}
1500 	/*
1501 	 * Otherwise, we just always try to lay things out contiguously.
1502 	 */
1503 	return (bap[indx - 1] + fs->fs_frag);
1504 }
1505 
1506 /*
1507  * Implement the cylinder overflow algorithm.
1508  *
1509  * The policy implemented by this algorithm is:
1510  *   1) allocate the block in its requested cylinder group.
1511  *   2) quadradically rehash on the cylinder group number.
1512  *   3) brute force search for a free block.
1513  *
1514  * Must be called with the UFS lock held.  Will release the lock on success
1515  * and return with it held on failure.
1516  */
1517 /*VARARGS5*/
1518 static ufs2_daddr_t
1519 ffs_hashalloc(ip, cg, pref, size, rsize, allocator)
1520 	struct inode *ip;
1521 	u_int cg;
1522 	ufs2_daddr_t pref;
1523 	int size;	/* Search size for data blocks, mode for inodes */
1524 	int rsize;	/* Real allocated size. */
1525 	allocfcn_t *allocator;
1526 {
1527 	struct fs *fs;
1528 	ufs2_daddr_t result;
1529 	u_int i, icg = cg;
1530 
1531 	mtx_assert(UFS_MTX(ITOUMP(ip)), MA_OWNED);
1532 #ifdef INVARIANTS
1533 	if (ITOV(ip)->v_mount->mnt_kern_flag & MNTK_SUSPENDED)
1534 		panic("ffs_hashalloc: allocation on suspended filesystem");
1535 #endif
1536 	fs = ITOFS(ip);
1537 	/*
1538 	 * 1: preferred cylinder group
1539 	 */
1540 	result = (*allocator)(ip, cg, pref, size, rsize);
1541 	if (result)
1542 		return (result);
1543 	/*
1544 	 * 2: quadratic rehash
1545 	 */
1546 	for (i = 1; i < fs->fs_ncg; i *= 2) {
1547 		cg += i;
1548 		if (cg >= fs->fs_ncg)
1549 			cg -= fs->fs_ncg;
1550 		result = (*allocator)(ip, cg, 0, size, rsize);
1551 		if (result)
1552 			return (result);
1553 	}
1554 	/*
1555 	 * 3: brute force search
1556 	 * Note that we start at i == 2, since 0 was checked initially,
1557 	 * and 1 is always checked in the quadratic rehash.
1558 	 */
1559 	cg = (icg + 2) % fs->fs_ncg;
1560 	for (i = 2; i < fs->fs_ncg; i++) {
1561 		result = (*allocator)(ip, cg, 0, size, rsize);
1562 		if (result)
1563 			return (result);
1564 		cg++;
1565 		if (cg == fs->fs_ncg)
1566 			cg = 0;
1567 	}
1568 	return (0);
1569 }
1570 
1571 /*
1572  * Determine whether a fragment can be extended.
1573  *
1574  * Check to see if the necessary fragments are available, and
1575  * if they are, allocate them.
1576  */
1577 static ufs2_daddr_t
1578 ffs_fragextend(ip, cg, bprev, osize, nsize)
1579 	struct inode *ip;
1580 	u_int cg;
1581 	ufs2_daddr_t bprev;
1582 	int osize, nsize;
1583 {
1584 	struct fs *fs;
1585 	struct cg *cgp;
1586 	struct buf *bp;
1587 	struct ufsmount *ump;
1588 	int nffree;
1589 	long bno;
1590 	int frags, bbase;
1591 	int i, error;
1592 	u_int8_t *blksfree;
1593 
1594 	ump = ITOUMP(ip);
1595 	fs = ump->um_fs;
1596 	if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize))
1597 		return (0);
1598 	frags = numfrags(fs, nsize);
1599 	bbase = fragnum(fs, bprev);
1600 	if (bbase > fragnum(fs, (bprev + frags - 1))) {
1601 		/* cannot extend across a block boundary */
1602 		return (0);
1603 	}
1604 	UFS_UNLOCK(ump);
1605 	error = bread(ump->um_devvp, fsbtodb(fs, cgtod(fs, cg)),
1606 	    (int)fs->fs_cgsize, NOCRED, &bp);
1607 	if (error)
1608 		goto fail;
1609 	cgp = (struct cg *)bp->b_data;
1610 	if (!cg_chkmagic(cgp))
1611 		goto fail;
1612 	bp->b_xflags |= BX_BKGRDWRITE;
1613 	cgp->cg_old_time = cgp->cg_time = time_second;
1614 	bno = dtogd(fs, bprev);
1615 	blksfree = cg_blksfree(cgp);
1616 	for (i = numfrags(fs, osize); i < frags; i++)
1617 		if (isclr(blksfree, bno + i))
1618 			goto fail;
1619 	/*
1620 	 * the current fragment can be extended
1621 	 * deduct the count on fragment being extended into
1622 	 * increase the count on the remaining fragment (if any)
1623 	 * allocate the extended piece
1624 	 */
1625 	for (i = frags; i < fs->fs_frag - bbase; i++)
1626 		if (isclr(blksfree, bno + i))
1627 			break;
1628 	cgp->cg_frsum[i - numfrags(fs, osize)]--;
1629 	if (i != frags)
1630 		cgp->cg_frsum[i - frags]++;
1631 	for (i = numfrags(fs, osize), nffree = 0; i < frags; i++) {
1632 		clrbit(blksfree, bno + i);
1633 		cgp->cg_cs.cs_nffree--;
1634 		nffree++;
1635 	}
1636 	UFS_LOCK(ump);
1637 	fs->fs_cstotal.cs_nffree -= nffree;
1638 	fs->fs_cs(fs, cg).cs_nffree -= nffree;
1639 	fs->fs_fmod = 1;
1640 	ACTIVECLEAR(fs, cg);
1641 	UFS_UNLOCK(ump);
1642 	if (DOINGSOFTDEP(ITOV(ip)))
1643 		softdep_setup_blkmapdep(bp, UFSTOVFS(ump), bprev,
1644 		    frags, numfrags(fs, osize));
1645 	bdwrite(bp);
1646 	return (bprev);
1647 
1648 fail:
1649 	brelse(bp);
1650 	UFS_LOCK(ump);
1651 	return (0);
1652 
1653 }
1654 
1655 /*
1656  * Determine whether a block can be allocated.
1657  *
1658  * Check to see if a block of the appropriate size is available,
1659  * and if it is, allocate it.
1660  */
1661 static ufs2_daddr_t
1662 ffs_alloccg(ip, cg, bpref, size, rsize)
1663 	struct inode *ip;
1664 	u_int cg;
1665 	ufs2_daddr_t bpref;
1666 	int size;
1667 	int rsize;
1668 {
1669 	struct fs *fs;
1670 	struct cg *cgp;
1671 	struct buf *bp;
1672 	struct ufsmount *ump;
1673 	ufs1_daddr_t bno;
1674 	ufs2_daddr_t blkno;
1675 	int i, allocsiz, error, frags;
1676 	u_int8_t *blksfree;
1677 
1678 	ump = ITOUMP(ip);
1679 	fs = ump->um_fs;
1680 	if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize)
1681 		return (0);
1682 	UFS_UNLOCK(ump);
1683 	error = bread(ump->um_devvp, fsbtodb(fs, cgtod(fs, cg)),
1684 	    (int)fs->fs_cgsize, NOCRED, &bp);
1685 	if (error)
1686 		goto fail;
1687 	cgp = (struct cg *)bp->b_data;
1688 	if (!cg_chkmagic(cgp) ||
1689 	    (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize))
1690 		goto fail;
1691 	bp->b_xflags |= BX_BKGRDWRITE;
1692 	cgp->cg_old_time = cgp->cg_time = time_second;
1693 	if (size == fs->fs_bsize) {
1694 		UFS_LOCK(ump);
1695 		blkno = ffs_alloccgblk(ip, bp, bpref, rsize);
1696 		ACTIVECLEAR(fs, cg);
1697 		UFS_UNLOCK(ump);
1698 		bdwrite(bp);
1699 		return (blkno);
1700 	}
1701 	/*
1702 	 * check to see if any fragments are already available
1703 	 * allocsiz is the size which will be allocated, hacking
1704 	 * it down to a smaller size if necessary
1705 	 */
1706 	blksfree = cg_blksfree(cgp);
1707 	frags = numfrags(fs, size);
1708 	for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++)
1709 		if (cgp->cg_frsum[allocsiz] != 0)
1710 			break;
1711 	if (allocsiz == fs->fs_frag) {
1712 		/*
1713 		 * no fragments were available, so a block will be
1714 		 * allocated, and hacked up
1715 		 */
1716 		if (cgp->cg_cs.cs_nbfree == 0)
1717 			goto fail;
1718 		UFS_LOCK(ump);
1719 		blkno = ffs_alloccgblk(ip, bp, bpref, rsize);
1720 		ACTIVECLEAR(fs, cg);
1721 		UFS_UNLOCK(ump);
1722 		bdwrite(bp);
1723 		return (blkno);
1724 	}
1725 	KASSERT(size == rsize,
1726 	    ("ffs_alloccg: size(%d) != rsize(%d)", size, rsize));
1727 	bno = ffs_mapsearch(fs, cgp, bpref, allocsiz);
1728 	if (bno < 0)
1729 		goto fail;
1730 	for (i = 0; i < frags; i++)
1731 		clrbit(blksfree, bno + i);
1732 	cgp->cg_cs.cs_nffree -= frags;
1733 	cgp->cg_frsum[allocsiz]--;
1734 	if (frags != allocsiz)
1735 		cgp->cg_frsum[allocsiz - frags]++;
1736 	UFS_LOCK(ump);
1737 	fs->fs_cstotal.cs_nffree -= frags;
1738 	fs->fs_cs(fs, cg).cs_nffree -= frags;
1739 	fs->fs_fmod = 1;
1740 	blkno = cgbase(fs, cg) + bno;
1741 	ACTIVECLEAR(fs, cg);
1742 	UFS_UNLOCK(ump);
1743 	if (DOINGSOFTDEP(ITOV(ip)))
1744 		softdep_setup_blkmapdep(bp, UFSTOVFS(ump), blkno, frags, 0);
1745 	bdwrite(bp);
1746 	return (blkno);
1747 
1748 fail:
1749 	brelse(bp);
1750 	UFS_LOCK(ump);
1751 	return (0);
1752 }
1753 
1754 /*
1755  * Allocate a block in a cylinder group.
1756  *
1757  * This algorithm implements the following policy:
1758  *   1) allocate the requested block.
1759  *   2) allocate a rotationally optimal block in the same cylinder.
1760  *   3) allocate the next available block on the block rotor for the
1761  *      specified cylinder group.
1762  * Note that this routine only allocates fs_bsize blocks; these
1763  * blocks may be fragmented by the routine that allocates them.
1764  */
1765 static ufs2_daddr_t
1766 ffs_alloccgblk(ip, bp, bpref, size)
1767 	struct inode *ip;
1768 	struct buf *bp;
1769 	ufs2_daddr_t bpref;
1770 	int size;
1771 {
1772 	struct fs *fs;
1773 	struct cg *cgp;
1774 	struct ufsmount *ump;
1775 	ufs1_daddr_t bno;
1776 	ufs2_daddr_t blkno;
1777 	u_int8_t *blksfree;
1778 	int i, cgbpref;
1779 
1780 	ump = ITOUMP(ip);
1781 	fs = ump->um_fs;
1782 	mtx_assert(UFS_MTX(ump), MA_OWNED);
1783 	cgp = (struct cg *)bp->b_data;
1784 	blksfree = cg_blksfree(cgp);
1785 	if (bpref == 0) {
1786 		bpref = cgbase(fs, cgp->cg_cgx) + cgp->cg_rotor + fs->fs_frag;
1787 	} else if ((cgbpref = dtog(fs, bpref)) != cgp->cg_cgx) {
1788 		/* map bpref to correct zone in this cg */
1789 		if (bpref < cgdata(fs, cgbpref))
1790 			bpref = cgmeta(fs, cgp->cg_cgx);
1791 		else
1792 			bpref = cgdata(fs, cgp->cg_cgx);
1793 	}
1794 	/*
1795 	 * if the requested block is available, use it
1796 	 */
1797 	bno = dtogd(fs, blknum(fs, bpref));
1798 	if (ffs_isblock(fs, blksfree, fragstoblks(fs, bno)))
1799 		goto gotit;
1800 	/*
1801 	 * Take the next available block in this cylinder group.
1802 	 */
1803 	bno = ffs_mapsearch(fs, cgp, bpref, (int)fs->fs_frag);
1804 	if (bno < 0)
1805 		return (0);
1806 	/* Update cg_rotor only if allocated from the data zone */
1807 	if (bno >= dtogd(fs, cgdata(fs, cgp->cg_cgx)))
1808 		cgp->cg_rotor = bno;
1809 gotit:
1810 	blkno = fragstoblks(fs, bno);
1811 	ffs_clrblock(fs, blksfree, (long)blkno);
1812 	ffs_clusteracct(fs, cgp, blkno, -1);
1813 	cgp->cg_cs.cs_nbfree--;
1814 	fs->fs_cstotal.cs_nbfree--;
1815 	fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--;
1816 	fs->fs_fmod = 1;
1817 	blkno = cgbase(fs, cgp->cg_cgx) + bno;
1818 	/*
1819 	 * If the caller didn't want the whole block free the frags here.
1820 	 */
1821 	size = numfrags(fs, size);
1822 	if (size != fs->fs_frag) {
1823 		bno = dtogd(fs, blkno);
1824 		for (i = size; i < fs->fs_frag; i++)
1825 			setbit(blksfree, bno + i);
1826 		i = fs->fs_frag - size;
1827 		cgp->cg_cs.cs_nffree += i;
1828 		fs->fs_cstotal.cs_nffree += i;
1829 		fs->fs_cs(fs, cgp->cg_cgx).cs_nffree += i;
1830 		fs->fs_fmod = 1;
1831 		cgp->cg_frsum[i]++;
1832 	}
1833 	/* XXX Fixme. */
1834 	UFS_UNLOCK(ump);
1835 	if (DOINGSOFTDEP(ITOV(ip)))
1836 		softdep_setup_blkmapdep(bp, UFSTOVFS(ump), blkno,
1837 		    size, 0);
1838 	UFS_LOCK(ump);
1839 	return (blkno);
1840 }
1841 
1842 /*
1843  * Determine whether a cluster can be allocated.
1844  *
1845  * We do not currently check for optimal rotational layout if there
1846  * are multiple choices in the same cylinder group. Instead we just
1847  * take the first one that we find following bpref.
1848  */
1849 static ufs2_daddr_t
1850 ffs_clusteralloc(ip, cg, bpref, len)
1851 	struct inode *ip;
1852 	u_int cg;
1853 	ufs2_daddr_t bpref;
1854 	int len;
1855 {
1856 	struct fs *fs;
1857 	struct cg *cgp;
1858 	struct buf *bp;
1859 	struct ufsmount *ump;
1860 	int i, run, bit, map, got;
1861 	ufs2_daddr_t bno;
1862 	u_char *mapp;
1863 	int32_t *lp;
1864 	u_int8_t *blksfree;
1865 
1866 	ump = ITOUMP(ip);
1867 	fs = ump->um_fs;
1868 	if (fs->fs_maxcluster[cg] < len)
1869 		return (0);
1870 	UFS_UNLOCK(ump);
1871 	if (bread(ump->um_devvp, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize,
1872 	    NOCRED, &bp))
1873 		goto fail_lock;
1874 	cgp = (struct cg *)bp->b_data;
1875 	if (!cg_chkmagic(cgp))
1876 		goto fail_lock;
1877 	bp->b_xflags |= BX_BKGRDWRITE;
1878 	/*
1879 	 * Check to see if a cluster of the needed size (or bigger) is
1880 	 * available in this cylinder group.
1881 	 */
1882 	lp = &cg_clustersum(cgp)[len];
1883 	for (i = len; i <= fs->fs_contigsumsize; i++)
1884 		if (*lp++ > 0)
1885 			break;
1886 	if (i > fs->fs_contigsumsize) {
1887 		/*
1888 		 * This is the first time looking for a cluster in this
1889 		 * cylinder group. Update the cluster summary information
1890 		 * to reflect the true maximum sized cluster so that
1891 		 * future cluster allocation requests can avoid reading
1892 		 * the cylinder group map only to find no clusters.
1893 		 */
1894 		lp = &cg_clustersum(cgp)[len - 1];
1895 		for (i = len - 1; i > 0; i--)
1896 			if (*lp-- > 0)
1897 				break;
1898 		UFS_LOCK(ump);
1899 		fs->fs_maxcluster[cg] = i;
1900 		goto fail;
1901 	}
1902 	/*
1903 	 * Search the cluster map to find a big enough cluster.
1904 	 * We take the first one that we find, even if it is larger
1905 	 * than we need as we prefer to get one close to the previous
1906 	 * block allocation. We do not search before the current
1907 	 * preference point as we do not want to allocate a block
1908 	 * that is allocated before the previous one (as we will
1909 	 * then have to wait for another pass of the elevator
1910 	 * algorithm before it will be read). We prefer to fail and
1911 	 * be recalled to try an allocation in the next cylinder group.
1912 	 */
1913 	if (dtog(fs, bpref) != cg)
1914 		bpref = cgdata(fs, cg);
1915 	else
1916 		bpref = blknum(fs, bpref);
1917 	bpref = fragstoblks(fs, dtogd(fs, bpref));
1918 	mapp = &cg_clustersfree(cgp)[bpref / NBBY];
1919 	map = *mapp++;
1920 	bit = 1 << (bpref % NBBY);
1921 	for (run = 0, got = bpref; got < cgp->cg_nclusterblks; got++) {
1922 		if ((map & bit) == 0) {
1923 			run = 0;
1924 		} else {
1925 			run++;
1926 			if (run == len)
1927 				break;
1928 		}
1929 		if ((got & (NBBY - 1)) != (NBBY - 1)) {
1930 			bit <<= 1;
1931 		} else {
1932 			map = *mapp++;
1933 			bit = 1;
1934 		}
1935 	}
1936 	if (got >= cgp->cg_nclusterblks)
1937 		goto fail_lock;
1938 	/*
1939 	 * Allocate the cluster that we have found.
1940 	 */
1941 	blksfree = cg_blksfree(cgp);
1942 	for (i = 1; i <= len; i++)
1943 		if (!ffs_isblock(fs, blksfree, got - run + i))
1944 			panic("ffs_clusteralloc: map mismatch");
1945 	bno = cgbase(fs, cg) + blkstofrags(fs, got - run + 1);
1946 	if (dtog(fs, bno) != cg)
1947 		panic("ffs_clusteralloc: allocated out of group");
1948 	len = blkstofrags(fs, len);
1949 	UFS_LOCK(ump);
1950 	for (i = 0; i < len; i += fs->fs_frag)
1951 		if (ffs_alloccgblk(ip, bp, bno + i, fs->fs_bsize) != bno + i)
1952 			panic("ffs_clusteralloc: lost block");
1953 	ACTIVECLEAR(fs, cg);
1954 	UFS_UNLOCK(ump);
1955 	bdwrite(bp);
1956 	return (bno);
1957 
1958 fail_lock:
1959 	UFS_LOCK(ump);
1960 fail:
1961 	brelse(bp);
1962 	return (0);
1963 }
1964 
1965 static inline struct buf *
1966 getinobuf(struct inode *ip, u_int cg, u_int32_t cginoblk, int gbflags)
1967 {
1968 	struct fs *fs;
1969 
1970 	fs = ITOFS(ip);
1971 	return (getblk(ITODEVVP(ip), fsbtodb(fs, ino_to_fsba(fs,
1972 	    cg * fs->fs_ipg + cginoblk)), (int)fs->fs_bsize, 0, 0,
1973 	    gbflags));
1974 }
1975 
1976 /*
1977  * Determine whether an inode can be allocated.
1978  *
1979  * Check to see if an inode is available, and if it is,
1980  * allocate it using the following policy:
1981  *   1) allocate the requested inode.
1982  *   2) allocate the next available inode after the requested
1983  *      inode in the specified cylinder group.
1984  */
1985 static ufs2_daddr_t
1986 ffs_nodealloccg(ip, cg, ipref, mode, unused)
1987 	struct inode *ip;
1988 	u_int cg;
1989 	ufs2_daddr_t ipref;
1990 	int mode;
1991 	int unused;
1992 {
1993 	struct fs *fs;
1994 	struct cg *cgp;
1995 	struct buf *bp, *ibp;
1996 	struct ufsmount *ump;
1997 	u_int8_t *inosused, *loc;
1998 	struct ufs2_dinode *dp2;
1999 	int error, start, len, i;
2000 	u_int32_t old_initediblk;
2001 
2002 	ump = ITOUMP(ip);
2003 	fs = ump->um_fs;
2004 check_nifree:
2005 	if (fs->fs_cs(fs, cg).cs_nifree == 0)
2006 		return (0);
2007 	UFS_UNLOCK(ump);
2008 	error = bread(ump->um_devvp, fsbtodb(fs, cgtod(fs, cg)),
2009 		(int)fs->fs_cgsize, NOCRED, &bp);
2010 	if (error) {
2011 		brelse(bp);
2012 		UFS_LOCK(ump);
2013 		return (0);
2014 	}
2015 	cgp = (struct cg *)bp->b_data;
2016 restart:
2017 	if (!cg_chkmagic(cgp) || cgp->cg_cs.cs_nifree == 0) {
2018 		brelse(bp);
2019 		UFS_LOCK(ump);
2020 		return (0);
2021 	}
2022 	bp->b_xflags |= BX_BKGRDWRITE;
2023 	inosused = cg_inosused(cgp);
2024 	if (ipref) {
2025 		ipref %= fs->fs_ipg;
2026 		if (isclr(inosused, ipref))
2027 			goto gotit;
2028 	}
2029 	start = cgp->cg_irotor / NBBY;
2030 	len = howmany(fs->fs_ipg - cgp->cg_irotor, NBBY);
2031 	loc = memcchr(&inosused[start], 0xff, len);
2032 	if (loc == NULL) {
2033 		len = start + 1;
2034 		start = 0;
2035 		loc = memcchr(&inosused[start], 0xff, len);
2036 		if (loc == NULL) {
2037 			printf("cg = %d, irotor = %ld, fs = %s\n",
2038 			    cg, (long)cgp->cg_irotor, fs->fs_fsmnt);
2039 			panic("ffs_nodealloccg: map corrupted");
2040 			/* NOTREACHED */
2041 		}
2042 	}
2043 	ipref = (loc - inosused) * NBBY + ffs(~*loc) - 1;
2044 gotit:
2045 	/*
2046 	 * Check to see if we need to initialize more inodes.
2047 	 */
2048 	if (fs->fs_magic == FS_UFS2_MAGIC &&
2049 	    ipref + INOPB(fs) > cgp->cg_initediblk &&
2050 	    cgp->cg_initediblk < cgp->cg_niblk) {
2051 		old_initediblk = cgp->cg_initediblk;
2052 
2053 		/*
2054 		 * Free the cylinder group lock before writing the
2055 		 * initialized inode block.  Entering the
2056 		 * babarrierwrite() with the cylinder group lock
2057 		 * causes lock order violation between the lock and
2058 		 * snaplk.
2059 		 *
2060 		 * Another thread can decide to initialize the same
2061 		 * inode block, but whichever thread first gets the
2062 		 * cylinder group lock after writing the newly
2063 		 * allocated inode block will update it and the other
2064 		 * will realize that it has lost and leave the
2065 		 * cylinder group unchanged.
2066 		 */
2067 		ibp = getinobuf(ip, cg, old_initediblk, GB_LOCK_NOWAIT);
2068 		brelse(bp);
2069 		if (ibp == NULL) {
2070 			/*
2071 			 * The inode block buffer is already owned by
2072 			 * another thread, which must initialize it.
2073 			 * Wait on the buffer to allow another thread
2074 			 * to finish the updates, with dropped cg
2075 			 * buffer lock, then retry.
2076 			 */
2077 			ibp = getinobuf(ip, cg, old_initediblk, 0);
2078 			brelse(ibp);
2079 			UFS_LOCK(ump);
2080 			goto check_nifree;
2081 		}
2082 		bzero(ibp->b_data, (int)fs->fs_bsize);
2083 		dp2 = (struct ufs2_dinode *)(ibp->b_data);
2084 		for (i = 0; i < INOPB(fs); i++) {
2085 			while (dp2->di_gen == 0)
2086 				dp2->di_gen = arc4random();
2087 			dp2++;
2088 		}
2089 		/*
2090 		 * Rather than adding a soft updates dependency to ensure
2091 		 * that the new inode block is written before it is claimed
2092 		 * by the cylinder group map, we just do a barrier write
2093 		 * here. The barrier write will ensure that the inode block
2094 		 * gets written before the updated cylinder group map can be
2095 		 * written. The barrier write should only slow down bulk
2096 		 * loading of newly created filesystems.
2097 		 */
2098 		babarrierwrite(ibp);
2099 
2100 		/*
2101 		 * After the inode block is written, try to update the
2102 		 * cg initediblk pointer.  If another thread beat us
2103 		 * to it, then leave it unchanged as the other thread
2104 		 * has already set it correctly.
2105 		 */
2106 		error = bread(ump->um_devvp, fsbtodb(fs, cgtod(fs, cg)),
2107 		    (int)fs->fs_cgsize, NOCRED, &bp);
2108 		UFS_LOCK(ump);
2109 		ACTIVECLEAR(fs, cg);
2110 		UFS_UNLOCK(ump);
2111 		if (error != 0) {
2112 			brelse(bp);
2113 			return (error);
2114 		}
2115 		cgp = (struct cg *)bp->b_data;
2116 		if (cgp->cg_initediblk == old_initediblk)
2117 			cgp->cg_initediblk += INOPB(fs);
2118 		goto restart;
2119 	}
2120 	cgp->cg_old_time = cgp->cg_time = time_second;
2121 	cgp->cg_irotor = ipref;
2122 	UFS_LOCK(ump);
2123 	ACTIVECLEAR(fs, cg);
2124 	setbit(inosused, ipref);
2125 	cgp->cg_cs.cs_nifree--;
2126 	fs->fs_cstotal.cs_nifree--;
2127 	fs->fs_cs(fs, cg).cs_nifree--;
2128 	fs->fs_fmod = 1;
2129 	if ((mode & IFMT) == IFDIR) {
2130 		cgp->cg_cs.cs_ndir++;
2131 		fs->fs_cstotal.cs_ndir++;
2132 		fs->fs_cs(fs, cg).cs_ndir++;
2133 	}
2134 	UFS_UNLOCK(ump);
2135 	if (DOINGSOFTDEP(ITOV(ip)))
2136 		softdep_setup_inomapdep(bp, ip, cg * fs->fs_ipg + ipref, mode);
2137 	bdwrite(bp);
2138 	return ((ino_t)(cg * fs->fs_ipg + ipref));
2139 }
2140 
2141 /*
2142  * Free a block or fragment.
2143  *
2144  * The specified block or fragment is placed back in the
2145  * free map. If a fragment is deallocated, a possible
2146  * block reassembly is checked.
2147  */
2148 static void
2149 ffs_blkfree_cg(ump, fs, devvp, bno, size, inum, dephd)
2150 	struct ufsmount *ump;
2151 	struct fs *fs;
2152 	struct vnode *devvp;
2153 	ufs2_daddr_t bno;
2154 	long size;
2155 	ino_t inum;
2156 	struct workhead *dephd;
2157 {
2158 	struct mount *mp;
2159 	struct cg *cgp;
2160 	struct buf *bp;
2161 	ufs1_daddr_t fragno, cgbno;
2162 	ufs2_daddr_t cgblkno;
2163 	int i, blk, frags, bbase;
2164 	u_int cg;
2165 	u_int8_t *blksfree;
2166 	struct cdev *dev;
2167 
2168 	cg = dtog(fs, bno);
2169 	if (devvp->v_type == VREG) {
2170 		/* devvp is a snapshot */
2171 		MPASS(devvp->v_mount->mnt_data == ump);
2172 		dev = ump->um_devvp->v_rdev;
2173 		cgblkno = fragstoblks(fs, cgtod(fs, cg));
2174 	} else {
2175 		/* devvp is a normal disk device */
2176 		dev = devvp->v_rdev;
2177 		cgblkno = fsbtodb(fs, cgtod(fs, cg));
2178 		ASSERT_VOP_LOCKED(devvp, "ffs_blkfree_cg");
2179 	}
2180 #ifdef INVARIANTS
2181 	if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0 ||
2182 	    fragnum(fs, bno) + numfrags(fs, size) > fs->fs_frag) {
2183 		printf("dev=%s, bno = %jd, bsize = %ld, size = %ld, fs = %s\n",
2184 		    devtoname(dev), (intmax_t)bno, (long)fs->fs_bsize,
2185 		    size, fs->fs_fsmnt);
2186 		panic("ffs_blkfree_cg: bad size");
2187 	}
2188 #endif
2189 	if ((u_int)bno >= fs->fs_size) {
2190 		printf("bad block %jd, ino %lu\n", (intmax_t)bno,
2191 		    (u_long)inum);
2192 		ffs_fserr(fs, inum, "bad block");
2193 		return;
2194 	}
2195 	if (bread(devvp, cgblkno, (int)fs->fs_cgsize, NOCRED, &bp)) {
2196 		brelse(bp);
2197 		return;
2198 	}
2199 	cgp = (struct cg *)bp->b_data;
2200 	if (!cg_chkmagic(cgp)) {
2201 		brelse(bp);
2202 		return;
2203 	}
2204 	bp->b_xflags |= BX_BKGRDWRITE;
2205 	cgp->cg_old_time = cgp->cg_time = time_second;
2206 	cgbno = dtogd(fs, bno);
2207 	blksfree = cg_blksfree(cgp);
2208 	UFS_LOCK(ump);
2209 	if (size == fs->fs_bsize) {
2210 		fragno = fragstoblks(fs, cgbno);
2211 		if (!ffs_isfreeblock(fs, blksfree, fragno)) {
2212 			if (devvp->v_type == VREG) {
2213 				UFS_UNLOCK(ump);
2214 				/* devvp is a snapshot */
2215 				brelse(bp);
2216 				return;
2217 			}
2218 			printf("dev = %s, block = %jd, fs = %s\n",
2219 			    devtoname(dev), (intmax_t)bno, fs->fs_fsmnt);
2220 			panic("ffs_blkfree_cg: freeing free block");
2221 		}
2222 		ffs_setblock(fs, blksfree, fragno);
2223 		ffs_clusteracct(fs, cgp, fragno, 1);
2224 		cgp->cg_cs.cs_nbfree++;
2225 		fs->fs_cstotal.cs_nbfree++;
2226 		fs->fs_cs(fs, cg).cs_nbfree++;
2227 	} else {
2228 		bbase = cgbno - fragnum(fs, cgbno);
2229 		/*
2230 		 * decrement the counts associated with the old frags
2231 		 */
2232 		blk = blkmap(fs, blksfree, bbase);
2233 		ffs_fragacct(fs, blk, cgp->cg_frsum, -1);
2234 		/*
2235 		 * deallocate the fragment
2236 		 */
2237 		frags = numfrags(fs, size);
2238 		for (i = 0; i < frags; i++) {
2239 			if (isset(blksfree, cgbno + i)) {
2240 				printf("dev = %s, block = %jd, fs = %s\n",
2241 				    devtoname(dev), (intmax_t)(bno + i),
2242 				    fs->fs_fsmnt);
2243 				panic("ffs_blkfree_cg: freeing free frag");
2244 			}
2245 			setbit(blksfree, cgbno + i);
2246 		}
2247 		cgp->cg_cs.cs_nffree += i;
2248 		fs->fs_cstotal.cs_nffree += i;
2249 		fs->fs_cs(fs, cg).cs_nffree += i;
2250 		/*
2251 		 * add back in counts associated with the new frags
2252 		 */
2253 		blk = blkmap(fs, blksfree, bbase);
2254 		ffs_fragacct(fs, blk, cgp->cg_frsum, 1);
2255 		/*
2256 		 * if a complete block has been reassembled, account for it
2257 		 */
2258 		fragno = fragstoblks(fs, bbase);
2259 		if (ffs_isblock(fs, blksfree, fragno)) {
2260 			cgp->cg_cs.cs_nffree -= fs->fs_frag;
2261 			fs->fs_cstotal.cs_nffree -= fs->fs_frag;
2262 			fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag;
2263 			ffs_clusteracct(fs, cgp, fragno, 1);
2264 			cgp->cg_cs.cs_nbfree++;
2265 			fs->fs_cstotal.cs_nbfree++;
2266 			fs->fs_cs(fs, cg).cs_nbfree++;
2267 		}
2268 	}
2269 	fs->fs_fmod = 1;
2270 	ACTIVECLEAR(fs, cg);
2271 	UFS_UNLOCK(ump);
2272 	mp = UFSTOVFS(ump);
2273 	if (MOUNTEDSOFTDEP(mp) && devvp->v_type != VREG)
2274 		softdep_setup_blkfree(UFSTOVFS(ump), bp, bno,
2275 		    numfrags(fs, size), dephd);
2276 	bdwrite(bp);
2277 }
2278 
2279 struct ffs_blkfree_trim_params {
2280 	struct task task;
2281 	struct ufsmount *ump;
2282 	struct vnode *devvp;
2283 	ufs2_daddr_t bno;
2284 	long size;
2285 	ino_t inum;
2286 	struct workhead *pdephd;
2287 	struct workhead dephd;
2288 };
2289 
2290 static void
2291 ffs_blkfree_trim_task(ctx, pending)
2292 	void *ctx;
2293 	int pending;
2294 {
2295 	struct ffs_blkfree_trim_params *tp;
2296 
2297 	tp = ctx;
2298 	ffs_blkfree_cg(tp->ump, tp->ump->um_fs, tp->devvp, tp->bno, tp->size,
2299 	    tp->inum, tp->pdephd);
2300 	vn_finished_secondary_write(UFSTOVFS(tp->ump));
2301 	atomic_add_int(&tp->ump->um_trim_inflight, -1);
2302 	free(tp, M_TEMP);
2303 }
2304 
2305 static void
2306 ffs_blkfree_trim_completed(bip)
2307 	struct bio *bip;
2308 {
2309 	struct ffs_blkfree_trim_params *tp;
2310 
2311 	tp = bip->bio_caller2;
2312 	g_destroy_bio(bip);
2313 	TASK_INIT(&tp->task, 0, ffs_blkfree_trim_task, tp);
2314 	taskqueue_enqueue(tp->ump->um_trim_tq, &tp->task);
2315 }
2316 
2317 void
2318 ffs_blkfree(ump, fs, devvp, bno, size, inum, vtype, dephd)
2319 	struct ufsmount *ump;
2320 	struct fs *fs;
2321 	struct vnode *devvp;
2322 	ufs2_daddr_t bno;
2323 	long size;
2324 	ino_t inum;
2325 	enum vtype vtype;
2326 	struct workhead *dephd;
2327 {
2328 	struct mount *mp;
2329 	struct bio *bip;
2330 	struct ffs_blkfree_trim_params *tp;
2331 
2332 	/*
2333 	 * Check to see if a snapshot wants to claim the block.
2334 	 * Check that devvp is a normal disk device, not a snapshot,
2335 	 * it has a snapshot(s) associated with it, and one of the
2336 	 * snapshots wants to claim the block.
2337 	 */
2338 	if (devvp->v_type != VREG &&
2339 	    (devvp->v_vflag & VV_COPYONWRITE) &&
2340 	    ffs_snapblkfree(fs, devvp, bno, size, inum, vtype, dephd)) {
2341 		return;
2342 	}
2343 	/*
2344 	 * Nothing to delay if TRIM is disabled, or the operation is
2345 	 * performed on the snapshot.
2346 	 */
2347 	if (!ump->um_candelete || devvp->v_type == VREG) {
2348 		ffs_blkfree_cg(ump, fs, devvp, bno, size, inum, dephd);
2349 		return;
2350 	}
2351 
2352 	/*
2353 	 * Postpone the set of the free bit in the cg bitmap until the
2354 	 * BIO_DELETE is completed.  Otherwise, due to disk queue
2355 	 * reordering, TRIM might be issued after we reuse the block
2356 	 * and write some new data into it.
2357 	 */
2358 	atomic_add_int(&ump->um_trim_inflight, 1);
2359 	tp = malloc(sizeof(struct ffs_blkfree_trim_params), M_TEMP, M_WAITOK);
2360 	tp->ump = ump;
2361 	tp->devvp = devvp;
2362 	tp->bno = bno;
2363 	tp->size = size;
2364 	tp->inum = inum;
2365 	if (dephd != NULL) {
2366 		LIST_INIT(&tp->dephd);
2367 		LIST_SWAP(dephd, &tp->dephd, worklist, wk_list);
2368 		tp->pdephd = &tp->dephd;
2369 	} else
2370 		tp->pdephd = NULL;
2371 
2372 	bip = g_alloc_bio();
2373 	bip->bio_cmd = BIO_DELETE;
2374 	bip->bio_offset = dbtob(fsbtodb(fs, bno));
2375 	bip->bio_done = ffs_blkfree_trim_completed;
2376 	bip->bio_length = size;
2377 	bip->bio_caller2 = tp;
2378 
2379 	mp = UFSTOVFS(ump);
2380 	vn_start_secondary_write(NULL, &mp, 0);
2381 	g_io_request(bip, (struct g_consumer *)devvp->v_bufobj.bo_private);
2382 }
2383 
2384 #ifdef INVARIANTS
2385 /*
2386  * Verify allocation of a block or fragment. Returns true if block or
2387  * fragment is allocated, false if it is free.
2388  */
2389 static int
2390 ffs_checkblk(ip, bno, size)
2391 	struct inode *ip;
2392 	ufs2_daddr_t bno;
2393 	long size;
2394 {
2395 	struct fs *fs;
2396 	struct cg *cgp;
2397 	struct buf *bp;
2398 	ufs1_daddr_t cgbno;
2399 	int i, error, frags, free;
2400 	u_int8_t *blksfree;
2401 
2402 	fs = ITOFS(ip);
2403 	if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0) {
2404 		printf("bsize = %ld, size = %ld, fs = %s\n",
2405 		    (long)fs->fs_bsize, size, fs->fs_fsmnt);
2406 		panic("ffs_checkblk: bad size");
2407 	}
2408 	if ((u_int)bno >= fs->fs_size)
2409 		panic("ffs_checkblk: bad block %jd", (intmax_t)bno);
2410 	error = bread(ITODEVVP(ip), fsbtodb(fs, cgtod(fs, dtog(fs, bno))),
2411 		(int)fs->fs_cgsize, NOCRED, &bp);
2412 	if (error)
2413 		panic("ffs_checkblk: cg bread failed");
2414 	cgp = (struct cg *)bp->b_data;
2415 	if (!cg_chkmagic(cgp))
2416 		panic("ffs_checkblk: cg magic mismatch");
2417 	bp->b_xflags |= BX_BKGRDWRITE;
2418 	blksfree = cg_blksfree(cgp);
2419 	cgbno = dtogd(fs, bno);
2420 	if (size == fs->fs_bsize) {
2421 		free = ffs_isblock(fs, blksfree, fragstoblks(fs, cgbno));
2422 	} else {
2423 		frags = numfrags(fs, size);
2424 		for (free = 0, i = 0; i < frags; i++)
2425 			if (isset(blksfree, cgbno + i))
2426 				free++;
2427 		if (free != 0 && free != frags)
2428 			panic("ffs_checkblk: partially free fragment");
2429 	}
2430 	brelse(bp);
2431 	return (!free);
2432 }
2433 #endif /* INVARIANTS */
2434 
2435 /*
2436  * Free an inode.
2437  */
2438 int
2439 ffs_vfree(pvp, ino, mode)
2440 	struct vnode *pvp;
2441 	ino_t ino;
2442 	int mode;
2443 {
2444 	struct ufsmount *ump;
2445 	struct inode *ip;
2446 
2447 	if (DOINGSOFTDEP(pvp)) {
2448 		softdep_freefile(pvp, ino, mode);
2449 		return (0);
2450 	}
2451 	ip = VTOI(pvp);
2452 	ump = VFSTOUFS(pvp->v_mount);
2453 	return (ffs_freefile(ump, ump->um_fs, ump->um_devvp, ino, mode, NULL));
2454 }
2455 
2456 /*
2457  * Do the actual free operation.
2458  * The specified inode is placed back in the free map.
2459  */
2460 int
2461 ffs_freefile(ump, fs, devvp, ino, mode, wkhd)
2462 	struct ufsmount *ump;
2463 	struct fs *fs;
2464 	struct vnode *devvp;
2465 	ino_t ino;
2466 	int mode;
2467 	struct workhead *wkhd;
2468 {
2469 	struct cg *cgp;
2470 	struct buf *bp;
2471 	ufs2_daddr_t cgbno;
2472 	int error;
2473 	u_int cg;
2474 	u_int8_t *inosused;
2475 	struct cdev *dev;
2476 
2477 	cg = ino_to_cg(fs, ino);
2478 	if (devvp->v_type == VREG) {
2479 		/* devvp is a snapshot */
2480 		MPASS(devvp->v_mount->mnt_data == ump);
2481 		dev = ump->um_devvp->v_rdev;
2482 		cgbno = fragstoblks(fs, cgtod(fs, cg));
2483 	} else {
2484 		/* devvp is a normal disk device */
2485 		dev = devvp->v_rdev;
2486 		cgbno = fsbtodb(fs, cgtod(fs, cg));
2487 	}
2488 	if (ino >= fs->fs_ipg * fs->fs_ncg)
2489 		panic("ffs_freefile: range: dev = %s, ino = %ju, fs = %s",
2490 		    devtoname(dev), (uintmax_t)ino, fs->fs_fsmnt);
2491 	if ((error = bread(devvp, cgbno, (int)fs->fs_cgsize, NOCRED, &bp))) {
2492 		brelse(bp);
2493 		return (error);
2494 	}
2495 	cgp = (struct cg *)bp->b_data;
2496 	if (!cg_chkmagic(cgp)) {
2497 		brelse(bp);
2498 		return (0);
2499 	}
2500 	bp->b_xflags |= BX_BKGRDWRITE;
2501 	cgp->cg_old_time = cgp->cg_time = time_second;
2502 	inosused = cg_inosused(cgp);
2503 	ino %= fs->fs_ipg;
2504 	if (isclr(inosused, ino)) {
2505 		printf("dev = %s, ino = %ju, fs = %s\n", devtoname(dev),
2506 		    (uintmax_t)(ino + cg * fs->fs_ipg), fs->fs_fsmnt);
2507 		if (fs->fs_ronly == 0)
2508 			panic("ffs_freefile: freeing free inode");
2509 	}
2510 	clrbit(inosused, ino);
2511 	if (ino < cgp->cg_irotor)
2512 		cgp->cg_irotor = ino;
2513 	cgp->cg_cs.cs_nifree++;
2514 	UFS_LOCK(ump);
2515 	fs->fs_cstotal.cs_nifree++;
2516 	fs->fs_cs(fs, cg).cs_nifree++;
2517 	if ((mode & IFMT) == IFDIR) {
2518 		cgp->cg_cs.cs_ndir--;
2519 		fs->fs_cstotal.cs_ndir--;
2520 		fs->fs_cs(fs, cg).cs_ndir--;
2521 	}
2522 	fs->fs_fmod = 1;
2523 	ACTIVECLEAR(fs, cg);
2524 	UFS_UNLOCK(ump);
2525 	if (MOUNTEDSOFTDEP(UFSTOVFS(ump)) && devvp->v_type != VREG)
2526 		softdep_setup_inofree(UFSTOVFS(ump), bp,
2527 		    ino + cg * fs->fs_ipg, wkhd);
2528 	bdwrite(bp);
2529 	return (0);
2530 }
2531 
2532 /*
2533  * Check to see if a file is free.
2534  */
2535 int
2536 ffs_checkfreefile(fs, devvp, ino)
2537 	struct fs *fs;
2538 	struct vnode *devvp;
2539 	ino_t ino;
2540 {
2541 	struct cg *cgp;
2542 	struct buf *bp;
2543 	ufs2_daddr_t cgbno;
2544 	int ret;
2545 	u_int cg;
2546 	u_int8_t *inosused;
2547 
2548 	cg = ino_to_cg(fs, ino);
2549 	if (devvp->v_type == VREG) {
2550 		/* devvp is a snapshot */
2551 		cgbno = fragstoblks(fs, cgtod(fs, cg));
2552 	} else {
2553 		/* devvp is a normal disk device */
2554 		cgbno = fsbtodb(fs, cgtod(fs, cg));
2555 	}
2556 	if (ino >= fs->fs_ipg * fs->fs_ncg)
2557 		return (1);
2558 	if (bread(devvp, cgbno, (int)fs->fs_cgsize, NOCRED, &bp)) {
2559 		brelse(bp);
2560 		return (1);
2561 	}
2562 	cgp = (struct cg *)bp->b_data;
2563 	if (!cg_chkmagic(cgp)) {
2564 		brelse(bp);
2565 		return (1);
2566 	}
2567 	inosused = cg_inosused(cgp);
2568 	ino %= fs->fs_ipg;
2569 	ret = isclr(inosused, ino);
2570 	brelse(bp);
2571 	return (ret);
2572 }
2573 
2574 /*
2575  * Find a block of the specified size in the specified cylinder group.
2576  *
2577  * It is a panic if a request is made to find a block if none are
2578  * available.
2579  */
2580 static ufs1_daddr_t
2581 ffs_mapsearch(fs, cgp, bpref, allocsiz)
2582 	struct fs *fs;
2583 	struct cg *cgp;
2584 	ufs2_daddr_t bpref;
2585 	int allocsiz;
2586 {
2587 	ufs1_daddr_t bno;
2588 	int start, len, loc, i;
2589 	int blk, field, subfield, pos;
2590 	u_int8_t *blksfree;
2591 
2592 	/*
2593 	 * find the fragment by searching through the free block
2594 	 * map for an appropriate bit pattern
2595 	 */
2596 	if (bpref)
2597 		start = dtogd(fs, bpref) / NBBY;
2598 	else
2599 		start = cgp->cg_frotor / NBBY;
2600 	blksfree = cg_blksfree(cgp);
2601 	len = howmany(fs->fs_fpg, NBBY) - start;
2602 	loc = scanc((u_int)len, (u_char *)&blksfree[start],
2603 		fragtbl[fs->fs_frag],
2604 		(u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
2605 	if (loc == 0) {
2606 		len = start + 1;
2607 		start = 0;
2608 		loc = scanc((u_int)len, (u_char *)&blksfree[0],
2609 			fragtbl[fs->fs_frag],
2610 			(u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
2611 		if (loc == 0) {
2612 			printf("start = %d, len = %d, fs = %s\n",
2613 			    start, len, fs->fs_fsmnt);
2614 			panic("ffs_alloccg: map corrupted");
2615 			/* NOTREACHED */
2616 		}
2617 	}
2618 	bno = (start + len - loc) * NBBY;
2619 	cgp->cg_frotor = bno;
2620 	/*
2621 	 * found the byte in the map
2622 	 * sift through the bits to find the selected frag
2623 	 */
2624 	for (i = bno + NBBY; bno < i; bno += fs->fs_frag) {
2625 		blk = blkmap(fs, blksfree, bno);
2626 		blk <<= 1;
2627 		field = around[allocsiz];
2628 		subfield = inside[allocsiz];
2629 		for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) {
2630 			if ((blk & field) == subfield)
2631 				return (bno + pos);
2632 			field <<= 1;
2633 			subfield <<= 1;
2634 		}
2635 	}
2636 	printf("bno = %lu, fs = %s\n", (u_long)bno, fs->fs_fsmnt);
2637 	panic("ffs_alloccg: block not in map");
2638 	return (-1);
2639 }
2640 
2641 /*
2642  * Fserr prints the name of a filesystem with an error diagnostic.
2643  *
2644  * The form of the error message is:
2645  *	fs: error message
2646  */
2647 void
2648 ffs_fserr(fs, inum, cp)
2649 	struct fs *fs;
2650 	ino_t inum;
2651 	char *cp;
2652 {
2653 	struct thread *td = curthread;	/* XXX */
2654 	struct proc *p = td->td_proc;
2655 
2656 	log(LOG_ERR, "pid %d (%s), uid %d inumber %ju on %s: %s\n",
2657 	    p->p_pid, p->p_comm, td->td_ucred->cr_uid, (uintmax_t)inum,
2658 	    fs->fs_fsmnt, cp);
2659 }
2660 
2661 /*
2662  * This function provides the capability for the fsck program to
2663  * update an active filesystem. Fourteen operations are provided:
2664  *
2665  * adjrefcnt(inode, amt) - adjusts the reference count on the
2666  *	specified inode by the specified amount. Under normal
2667  *	operation the count should always go down. Decrementing
2668  *	the count to zero will cause the inode to be freed.
2669  * adjblkcnt(inode, amt) - adjust the number of blocks used by the
2670  *	inode by the specified amount.
2671  * adjndir, adjbfree, adjifree, adjffree, adjnumclusters(amt) -
2672  *	adjust the superblock summary.
2673  * freedirs(inode, count) - directory inodes [inode..inode + count - 1]
2674  *	are marked as free. Inodes should never have to be marked
2675  *	as in use.
2676  * freefiles(inode, count) - file inodes [inode..inode + count - 1]
2677  *	are marked as free. Inodes should never have to be marked
2678  *	as in use.
2679  * freeblks(blockno, size) - blocks [blockno..blockno + size - 1]
2680  *	are marked as free. Blocks should never have to be marked
2681  *	as in use.
2682  * setflags(flags, set/clear) - the fs_flags field has the specified
2683  *	flags set (second parameter +1) or cleared (second parameter -1).
2684  * setcwd(dirinode) - set the current directory to dirinode in the
2685  *	filesystem associated with the snapshot.
2686  * setdotdot(oldvalue, newvalue) - Verify that the inode number for ".."
2687  *	in the current directory is oldvalue then change it to newvalue.
2688  * unlink(nameptr, oldvalue) - Verify that the inode number associated
2689  *	with nameptr in the current directory is oldvalue then unlink it.
2690  *
2691  * The following functions may only be used on a quiescent filesystem
2692  * by the soft updates journal. They are not safe to be run on an active
2693  * filesystem.
2694  *
2695  * setinode(inode, dip) - the specified disk inode is replaced with the
2696  *	contents pointed to by dip.
2697  * setbufoutput(fd, flags) - output associated with the specified file
2698  *	descriptor (which must reference the character device supporting
2699  *	the filesystem) switches from using physio to running through the
2700  *	buffer cache when flags is set to 1. The descriptor reverts to
2701  *	physio for output when flags is set to zero.
2702  */
2703 
2704 static int sysctl_ffs_fsck(SYSCTL_HANDLER_ARGS);
2705 
2706 SYSCTL_PROC(_vfs_ffs, FFS_ADJ_REFCNT, adjrefcnt, CTLFLAG_WR|CTLTYPE_STRUCT,
2707 	0, 0, sysctl_ffs_fsck, "S,fsck", "Adjust Inode Reference Count");
2708 
2709 static SYSCTL_NODE(_vfs_ffs, FFS_ADJ_BLKCNT, adjblkcnt, CTLFLAG_WR,
2710 	sysctl_ffs_fsck, "Adjust Inode Used Blocks Count");
2711 
2712 static SYSCTL_NODE(_vfs_ffs, FFS_ADJ_NDIR, adjndir, CTLFLAG_WR,
2713 	sysctl_ffs_fsck, "Adjust number of directories");
2714 
2715 static SYSCTL_NODE(_vfs_ffs, FFS_ADJ_NBFREE, adjnbfree, CTLFLAG_WR,
2716 	sysctl_ffs_fsck, "Adjust number of free blocks");
2717 
2718 static SYSCTL_NODE(_vfs_ffs, FFS_ADJ_NIFREE, adjnifree, CTLFLAG_WR,
2719 	sysctl_ffs_fsck, "Adjust number of free inodes");
2720 
2721 static SYSCTL_NODE(_vfs_ffs, FFS_ADJ_NFFREE, adjnffree, CTLFLAG_WR,
2722 	sysctl_ffs_fsck, "Adjust number of free frags");
2723 
2724 static SYSCTL_NODE(_vfs_ffs, FFS_ADJ_NUMCLUSTERS, adjnumclusters, CTLFLAG_WR,
2725 	sysctl_ffs_fsck, "Adjust number of free clusters");
2726 
2727 static SYSCTL_NODE(_vfs_ffs, FFS_DIR_FREE, freedirs, CTLFLAG_WR,
2728 	sysctl_ffs_fsck, "Free Range of Directory Inodes");
2729 
2730 static SYSCTL_NODE(_vfs_ffs, FFS_FILE_FREE, freefiles, CTLFLAG_WR,
2731 	sysctl_ffs_fsck, "Free Range of File Inodes");
2732 
2733 static SYSCTL_NODE(_vfs_ffs, FFS_BLK_FREE, freeblks, CTLFLAG_WR,
2734 	sysctl_ffs_fsck, "Free Range of Blocks");
2735 
2736 static SYSCTL_NODE(_vfs_ffs, FFS_SET_FLAGS, setflags, CTLFLAG_WR,
2737 	sysctl_ffs_fsck, "Change Filesystem Flags");
2738 
2739 static SYSCTL_NODE(_vfs_ffs, FFS_SET_CWD, setcwd, CTLFLAG_WR,
2740 	sysctl_ffs_fsck, "Set Current Working Directory");
2741 
2742 static SYSCTL_NODE(_vfs_ffs, FFS_SET_DOTDOT, setdotdot, CTLFLAG_WR,
2743 	sysctl_ffs_fsck, "Change Value of .. Entry");
2744 
2745 static SYSCTL_NODE(_vfs_ffs, FFS_UNLINK, unlink, CTLFLAG_WR,
2746 	sysctl_ffs_fsck, "Unlink a Duplicate Name");
2747 
2748 static SYSCTL_NODE(_vfs_ffs, FFS_SET_INODE, setinode, CTLFLAG_WR,
2749 	sysctl_ffs_fsck, "Update an On-Disk Inode");
2750 
2751 static SYSCTL_NODE(_vfs_ffs, FFS_SET_BUFOUTPUT, setbufoutput, CTLFLAG_WR,
2752 	sysctl_ffs_fsck, "Set Buffered Writing for Descriptor");
2753 
2754 #define DEBUG 1
2755 #ifdef DEBUG
2756 static int fsckcmds = 0;
2757 SYSCTL_INT(_debug, OID_AUTO, fsckcmds, CTLFLAG_RW, &fsckcmds, 0, "");
2758 #endif /* DEBUG */
2759 
2760 static int buffered_write(struct file *, struct uio *, struct ucred *,
2761 	int, struct thread *);
2762 
2763 static int
2764 sysctl_ffs_fsck(SYSCTL_HANDLER_ARGS)
2765 {
2766 	struct thread *td = curthread;
2767 	struct fsck_cmd cmd;
2768 	struct ufsmount *ump;
2769 	struct vnode *vp, *dvp, *fdvp;
2770 	struct inode *ip, *dp;
2771 	struct mount *mp;
2772 	struct fs *fs;
2773 	ufs2_daddr_t blkno;
2774 	long blkcnt, blksize;
2775 	struct file *fp, *vfp;
2776 	cap_rights_t rights;
2777 	int filetype, error;
2778 	static struct fileops *origops, bufferedops;
2779 
2780 	if (req->newlen > sizeof cmd)
2781 		return (EBADRPC);
2782 	if ((error = SYSCTL_IN(req, &cmd, sizeof cmd)) != 0)
2783 		return (error);
2784 	if (cmd.version != FFS_CMD_VERSION)
2785 		return (ERPCMISMATCH);
2786 	if ((error = getvnode(td, cmd.handle,
2787 	    cap_rights_init(&rights, CAP_FSCK), &fp)) != 0)
2788 		return (error);
2789 	vp = fp->f_data;
2790 	if (vp->v_type != VREG && vp->v_type != VDIR) {
2791 		fdrop(fp, td);
2792 		return (EINVAL);
2793 	}
2794 	vn_start_write(vp, &mp, V_WAIT);
2795 	if (mp == NULL ||
2796 	    strncmp(mp->mnt_stat.f_fstypename, "ufs", MFSNAMELEN)) {
2797 		vn_finished_write(mp);
2798 		fdrop(fp, td);
2799 		return (EINVAL);
2800 	}
2801 	ump = VFSTOUFS(mp);
2802 	if ((mp->mnt_flag & MNT_RDONLY) &&
2803 	    ump->um_fsckpid != td->td_proc->p_pid) {
2804 		vn_finished_write(mp);
2805 		fdrop(fp, td);
2806 		return (EROFS);
2807 	}
2808 	fs = ump->um_fs;
2809 	filetype = IFREG;
2810 
2811 	switch (oidp->oid_number) {
2812 
2813 	case FFS_SET_FLAGS:
2814 #ifdef DEBUG
2815 		if (fsckcmds)
2816 			printf("%s: %s flags\n", mp->mnt_stat.f_mntonname,
2817 			    cmd.size > 0 ? "set" : "clear");
2818 #endif /* DEBUG */
2819 		if (cmd.size > 0)
2820 			fs->fs_flags |= (long)cmd.value;
2821 		else
2822 			fs->fs_flags &= ~(long)cmd.value;
2823 		break;
2824 
2825 	case FFS_ADJ_REFCNT:
2826 #ifdef DEBUG
2827 		if (fsckcmds) {
2828 			printf("%s: adjust inode %jd link count by %jd\n",
2829 			    mp->mnt_stat.f_mntonname, (intmax_t)cmd.value,
2830 			    (intmax_t)cmd.size);
2831 		}
2832 #endif /* DEBUG */
2833 		if ((error = ffs_vget(mp, (ino_t)cmd.value, LK_EXCLUSIVE, &vp)))
2834 			break;
2835 		ip = VTOI(vp);
2836 		ip->i_nlink += cmd.size;
2837 		DIP_SET(ip, i_nlink, ip->i_nlink);
2838 		ip->i_effnlink += cmd.size;
2839 		ip->i_flag |= IN_CHANGE | IN_MODIFIED;
2840 		error = ffs_update(vp, 1);
2841 		if (DOINGSOFTDEP(vp))
2842 			softdep_change_linkcnt(ip);
2843 		vput(vp);
2844 		break;
2845 
2846 	case FFS_ADJ_BLKCNT:
2847 #ifdef DEBUG
2848 		if (fsckcmds) {
2849 			printf("%s: adjust inode %jd block count by %jd\n",
2850 			    mp->mnt_stat.f_mntonname, (intmax_t)cmd.value,
2851 			    (intmax_t)cmd.size);
2852 		}
2853 #endif /* DEBUG */
2854 		if ((error = ffs_vget(mp, (ino_t)cmd.value, LK_EXCLUSIVE, &vp)))
2855 			break;
2856 		ip = VTOI(vp);
2857 		DIP_SET(ip, i_blocks, DIP(ip, i_blocks) + cmd.size);
2858 		ip->i_flag |= IN_CHANGE | IN_MODIFIED;
2859 		error = ffs_update(vp, 1);
2860 		vput(vp);
2861 		break;
2862 
2863 	case FFS_DIR_FREE:
2864 		filetype = IFDIR;
2865 		/* fall through */
2866 
2867 	case FFS_FILE_FREE:
2868 #ifdef DEBUG
2869 		if (fsckcmds) {
2870 			if (cmd.size == 1)
2871 				printf("%s: free %s inode %ju\n",
2872 				    mp->mnt_stat.f_mntonname,
2873 				    filetype == IFDIR ? "directory" : "file",
2874 				    (uintmax_t)cmd.value);
2875 			else
2876 				printf("%s: free %s inodes %ju-%ju\n",
2877 				    mp->mnt_stat.f_mntonname,
2878 				    filetype == IFDIR ? "directory" : "file",
2879 				    (uintmax_t)cmd.value,
2880 				    (uintmax_t)(cmd.value + cmd.size - 1));
2881 		}
2882 #endif /* DEBUG */
2883 		while (cmd.size > 0) {
2884 			if ((error = ffs_freefile(ump, fs, ump->um_devvp,
2885 			    cmd.value, filetype, NULL)))
2886 				break;
2887 			cmd.size -= 1;
2888 			cmd.value += 1;
2889 		}
2890 		break;
2891 
2892 	case FFS_BLK_FREE:
2893 #ifdef DEBUG
2894 		if (fsckcmds) {
2895 			if (cmd.size == 1)
2896 				printf("%s: free block %jd\n",
2897 				    mp->mnt_stat.f_mntonname,
2898 				    (intmax_t)cmd.value);
2899 			else
2900 				printf("%s: free blocks %jd-%jd\n",
2901 				    mp->mnt_stat.f_mntonname,
2902 				    (intmax_t)cmd.value,
2903 				    (intmax_t)cmd.value + cmd.size - 1);
2904 		}
2905 #endif /* DEBUG */
2906 		blkno = cmd.value;
2907 		blkcnt = cmd.size;
2908 		blksize = fs->fs_frag - (blkno % fs->fs_frag);
2909 		while (blkcnt > 0) {
2910 			if (blksize > blkcnt)
2911 				blksize = blkcnt;
2912 			ffs_blkfree(ump, fs, ump->um_devvp, blkno,
2913 			    blksize * fs->fs_fsize, ROOTINO, VDIR, NULL);
2914 			blkno += blksize;
2915 			blkcnt -= blksize;
2916 			blksize = fs->fs_frag;
2917 		}
2918 		break;
2919 
2920 	/*
2921 	 * Adjust superblock summaries.  fsck(8) is expected to
2922 	 * submit deltas when necessary.
2923 	 */
2924 	case FFS_ADJ_NDIR:
2925 #ifdef DEBUG
2926 		if (fsckcmds) {
2927 			printf("%s: adjust number of directories by %jd\n",
2928 			    mp->mnt_stat.f_mntonname, (intmax_t)cmd.value);
2929 		}
2930 #endif /* DEBUG */
2931 		fs->fs_cstotal.cs_ndir += cmd.value;
2932 		break;
2933 
2934 	case FFS_ADJ_NBFREE:
2935 #ifdef DEBUG
2936 		if (fsckcmds) {
2937 			printf("%s: adjust number of free blocks by %+jd\n",
2938 			    mp->mnt_stat.f_mntonname, (intmax_t)cmd.value);
2939 		}
2940 #endif /* DEBUG */
2941 		fs->fs_cstotal.cs_nbfree += cmd.value;
2942 		break;
2943 
2944 	case FFS_ADJ_NIFREE:
2945 #ifdef DEBUG
2946 		if (fsckcmds) {
2947 			printf("%s: adjust number of free inodes by %+jd\n",
2948 			    mp->mnt_stat.f_mntonname, (intmax_t)cmd.value);
2949 		}
2950 #endif /* DEBUG */
2951 		fs->fs_cstotal.cs_nifree += cmd.value;
2952 		break;
2953 
2954 	case FFS_ADJ_NFFREE:
2955 #ifdef DEBUG
2956 		if (fsckcmds) {
2957 			printf("%s: adjust number of free frags by %+jd\n",
2958 			    mp->mnt_stat.f_mntonname, (intmax_t)cmd.value);
2959 		}
2960 #endif /* DEBUG */
2961 		fs->fs_cstotal.cs_nffree += cmd.value;
2962 		break;
2963 
2964 	case FFS_ADJ_NUMCLUSTERS:
2965 #ifdef DEBUG
2966 		if (fsckcmds) {
2967 			printf("%s: adjust number of free clusters by %+jd\n",
2968 			    mp->mnt_stat.f_mntonname, (intmax_t)cmd.value);
2969 		}
2970 #endif /* DEBUG */
2971 		fs->fs_cstotal.cs_numclusters += cmd.value;
2972 		break;
2973 
2974 	case FFS_SET_CWD:
2975 #ifdef DEBUG
2976 		if (fsckcmds) {
2977 			printf("%s: set current directory to inode %jd\n",
2978 			    mp->mnt_stat.f_mntonname, (intmax_t)cmd.value);
2979 		}
2980 #endif /* DEBUG */
2981 		if ((error = ffs_vget(mp, (ino_t)cmd.value, LK_SHARED, &vp)))
2982 			break;
2983 		AUDIT_ARG_VNODE1(vp);
2984 		if ((error = change_dir(vp, td)) != 0) {
2985 			vput(vp);
2986 			break;
2987 		}
2988 		VOP_UNLOCK(vp, 0);
2989 		pwd_chdir(td, vp);
2990 		break;
2991 
2992 	case FFS_SET_DOTDOT:
2993 #ifdef DEBUG
2994 		if (fsckcmds) {
2995 			printf("%s: change .. in cwd from %jd to %jd\n",
2996 			    mp->mnt_stat.f_mntonname, (intmax_t)cmd.value,
2997 			    (intmax_t)cmd.size);
2998 		}
2999 #endif /* DEBUG */
3000 		/*
3001 		 * First we have to get and lock the parent directory
3002 		 * to which ".." points.
3003 		 */
3004 		error = ffs_vget(mp, (ino_t)cmd.value, LK_EXCLUSIVE, &fdvp);
3005 		if (error)
3006 			break;
3007 		/*
3008 		 * Now we get and lock the child directory containing "..".
3009 		 */
3010 		FILEDESC_SLOCK(td->td_proc->p_fd);
3011 		dvp = td->td_proc->p_fd->fd_cdir;
3012 		FILEDESC_SUNLOCK(td->td_proc->p_fd);
3013 		if ((error = vget(dvp, LK_EXCLUSIVE, td)) != 0) {
3014 			vput(fdvp);
3015 			break;
3016 		}
3017 		dp = VTOI(dvp);
3018 		dp->i_offset = 12;	/* XXX mastertemplate.dot_reclen */
3019 		error = ufs_dirrewrite(dp, VTOI(fdvp), (ino_t)cmd.size,
3020 		    DT_DIR, 0);
3021 		cache_purge(fdvp);
3022 		cache_purge(dvp);
3023 		vput(dvp);
3024 		vput(fdvp);
3025 		break;
3026 
3027 	case FFS_UNLINK:
3028 #ifdef DEBUG
3029 		if (fsckcmds) {
3030 			char buf[32];
3031 
3032 			if (copyinstr((char *)(intptr_t)cmd.value, buf,32,NULL))
3033 				strncpy(buf, "Name_too_long", 32);
3034 			printf("%s: unlink %s (inode %jd)\n",
3035 			    mp->mnt_stat.f_mntonname, buf, (intmax_t)cmd.size);
3036 		}
3037 #endif /* DEBUG */
3038 		/*
3039 		 * kern_unlinkat will do its own start/finish writes and
3040 		 * they do not nest, so drop ours here. Setting mp == NULL
3041 		 * indicates that vn_finished_write is not needed down below.
3042 		 */
3043 		vn_finished_write(mp);
3044 		mp = NULL;
3045 		error = kern_unlinkat(td, AT_FDCWD, (char *)(intptr_t)cmd.value,
3046 		    UIO_USERSPACE, (ino_t)cmd.size);
3047 		break;
3048 
3049 	case FFS_SET_INODE:
3050 		if (ump->um_fsckpid != td->td_proc->p_pid) {
3051 			error = EPERM;
3052 			break;
3053 		}
3054 #ifdef DEBUG
3055 		if (fsckcmds) {
3056 			printf("%s: update inode %jd\n",
3057 			    mp->mnt_stat.f_mntonname, (intmax_t)cmd.value);
3058 		}
3059 #endif /* DEBUG */
3060 		if ((error = ffs_vget(mp, (ino_t)cmd.value, LK_EXCLUSIVE, &vp)))
3061 			break;
3062 		AUDIT_ARG_VNODE1(vp);
3063 		ip = VTOI(vp);
3064 		if (I_IS_UFS1(ip))
3065 			error = copyin((void *)(intptr_t)cmd.size, ip->i_din1,
3066 			    sizeof(struct ufs1_dinode));
3067 		else
3068 			error = copyin((void *)(intptr_t)cmd.size, ip->i_din2,
3069 			    sizeof(struct ufs2_dinode));
3070 		if (error) {
3071 			vput(vp);
3072 			break;
3073 		}
3074 		ip->i_flag |= IN_CHANGE | IN_MODIFIED;
3075 		error = ffs_update(vp, 1);
3076 		vput(vp);
3077 		break;
3078 
3079 	case FFS_SET_BUFOUTPUT:
3080 		if (ump->um_fsckpid != td->td_proc->p_pid) {
3081 			error = EPERM;
3082 			break;
3083 		}
3084 		if (ITOUMP(VTOI(vp)) != ump) {
3085 			error = EINVAL;
3086 			break;
3087 		}
3088 #ifdef DEBUG
3089 		if (fsckcmds) {
3090 			printf("%s: %s buffered output for descriptor %jd\n",
3091 			    mp->mnt_stat.f_mntonname,
3092 			    cmd.size == 1 ? "enable" : "disable",
3093 			    (intmax_t)cmd.value);
3094 		}
3095 #endif /* DEBUG */
3096 		if ((error = getvnode(td, cmd.value,
3097 		    cap_rights_init(&rights, CAP_FSCK), &vfp)) != 0)
3098 			break;
3099 		if (vfp->f_vnode->v_type != VCHR) {
3100 			fdrop(vfp, td);
3101 			error = EINVAL;
3102 			break;
3103 		}
3104 		if (origops == NULL) {
3105 			origops = vfp->f_ops;
3106 			bcopy((void *)origops, (void *)&bufferedops,
3107 			    sizeof(bufferedops));
3108 			bufferedops.fo_write = buffered_write;
3109 		}
3110 		if (cmd.size == 1)
3111 			atomic_store_rel_ptr((volatile uintptr_t *)&vfp->f_ops,
3112 			    (uintptr_t)&bufferedops);
3113 		else
3114 			atomic_store_rel_ptr((volatile uintptr_t *)&vfp->f_ops,
3115 			    (uintptr_t)origops);
3116 		fdrop(vfp, td);
3117 		break;
3118 
3119 	default:
3120 #ifdef DEBUG
3121 		if (fsckcmds) {
3122 			printf("Invalid request %d from fsck\n",
3123 			    oidp->oid_number);
3124 		}
3125 #endif /* DEBUG */
3126 		error = EINVAL;
3127 		break;
3128 
3129 	}
3130 	fdrop(fp, td);
3131 	vn_finished_write(mp);
3132 	return (error);
3133 }
3134 
3135 /*
3136  * Function to switch a descriptor to use the buffer cache to stage
3137  * its I/O. This is needed so that writes to the filesystem device
3138  * will give snapshots a chance to copy modified blocks for which it
3139  * needs to retain copies.
3140  */
3141 static int
3142 buffered_write(fp, uio, active_cred, flags, td)
3143 	struct file *fp;
3144 	struct uio *uio;
3145 	struct ucred *active_cred;
3146 	int flags;
3147 	struct thread *td;
3148 {
3149 	struct vnode *devvp, *vp;
3150 	struct inode *ip;
3151 	struct buf *bp;
3152 	struct fs *fs;
3153 	struct filedesc *fdp;
3154 	int error;
3155 	daddr_t lbn;
3156 
3157 	/*
3158 	 * The devvp is associated with the /dev filesystem. To discover
3159 	 * the filesystem with which the device is associated, we depend
3160 	 * on the application setting the current directory to a location
3161 	 * within the filesystem being written. Yes, this is an ugly hack.
3162 	 */
3163 	devvp = fp->f_vnode;
3164 	if (!vn_isdisk(devvp, NULL))
3165 		return (EINVAL);
3166 	fdp = td->td_proc->p_fd;
3167 	FILEDESC_SLOCK(fdp);
3168 	vp = fdp->fd_cdir;
3169 	vref(vp);
3170 	FILEDESC_SUNLOCK(fdp);
3171 	vn_lock(vp, LK_SHARED | LK_RETRY);
3172 	/*
3173 	 * Check that the current directory vnode indeed belongs to
3174 	 * UFS before trying to dereference UFS-specific v_data fields.
3175 	 */
3176 	if (vp->v_op != &ffs_vnodeops1 && vp->v_op != &ffs_vnodeops2) {
3177 		vput(vp);
3178 		return (EINVAL);
3179 	}
3180 	ip = VTOI(vp);
3181 	if (ITODEVVP(ip) != devvp) {
3182 		vput(vp);
3183 		return (EINVAL);
3184 	}
3185 	fs = ITOFS(ip);
3186 	vput(vp);
3187 	foffset_lock_uio(fp, uio, flags);
3188 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
3189 #ifdef DEBUG
3190 	if (fsckcmds) {
3191 		printf("%s: buffered write for block %jd\n",
3192 		    fs->fs_fsmnt, (intmax_t)btodb(uio->uio_offset));
3193 	}
3194 #endif /* DEBUG */
3195 	/*
3196 	 * All I/O must be contained within a filesystem block, start on
3197 	 * a fragment boundary, and be a multiple of fragments in length.
3198 	 */
3199 	if (uio->uio_resid > fs->fs_bsize - (uio->uio_offset % fs->fs_bsize) ||
3200 	    fragoff(fs, uio->uio_offset) != 0 ||
3201 	    fragoff(fs, uio->uio_resid) != 0) {
3202 		error = EINVAL;
3203 		goto out;
3204 	}
3205 	lbn = numfrags(fs, uio->uio_offset);
3206 	bp = getblk(devvp, lbn, uio->uio_resid, 0, 0, 0);
3207 	bp->b_flags |= B_RELBUF;
3208 	if ((error = uiomove((char *)bp->b_data, uio->uio_resid, uio)) != 0) {
3209 		brelse(bp);
3210 		goto out;
3211 	}
3212 	error = bwrite(bp);
3213 out:
3214 	VOP_UNLOCK(devvp, 0);
3215 	foffset_unlock_uio(fp, uio, flags | FOF_NEXTOFF);
3216 	return (error);
3217 }
3218