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