1 /*	$OpenBSD: ffs_alloc.c,v 1.54 2005/05/02 13:13:21 pedro Exp $	*/
2 /*	$NetBSD: ffs_alloc.c,v 1.11 1996/05/11 18:27:09 mycroft Exp $	*/
3 
4 /*
5  * Copyright (c) 1982, 1986, 1989, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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  *	@(#)ffs_alloc.c	8.11 (Berkeley) 10/27/94
33  */
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/buf.h>
38 #include <sys/proc.h>
39 #include <sys/vnode.h>
40 #include <sys/mount.h>
41 #include <sys/kernel.h>
42 #include <sys/syslog.h>
43 
44 #include <uvm/uvm_extern.h>
45 
46 #include <dev/rndvar.h>
47 
48 #include <ufs/ufs/extattr.h>
49 #include <ufs/ufs/quota.h>
50 #include <ufs/ufs/inode.h>
51 #include <ufs/ufs/ufsmount.h>
52 #include <ufs/ufs/ufs_extern.h>
53 
54 #include <ufs/ffs/fs.h>
55 #include <ufs/ffs/ffs_extern.h>
56 
57 extern u_long nextgennumber;
58 
59 static daddr_t	ffs_alloccg(struct inode *, int, daddr_t, int);
60 static daddr_t	ffs_alloccgblk(struct inode *, struct buf *, daddr_t);
61 static daddr_t	ffs_clusteralloc(struct inode *, int, daddr_t, int);
62 static ino_t	ffs_dirpref(struct inode *);
63 static daddr_t	ffs_fragextend(struct inode *, int, long, int, int);
64 static void	ffs_fserr(struct fs *, u_int, char *);
65 static u_long	ffs_hashalloc(struct inode *, int, long, int,
66 		    daddr_t (*)(struct inode *, int, daddr_t, int));
67 static daddr_t	ffs_nodealloccg(struct inode *, int, daddr_t, int);
68 static daddr_t	ffs_mapsearch(struct fs *, struct cg *, daddr_t, int);
69 
70 #ifdef DIAGNOSTIC
71 static int      ffs_checkblk(struct inode *, daddr_t, long);
72 #endif
73 
74 /*
75  * Allocate a block in the file system.
76  *
77  * The size of the requested block is given, which must be some
78  * multiple of fs_fsize and <= fs_bsize.
79  * A preference may be optionally specified. If a preference is given
80  * the following hierarchy is used to allocate a block:
81  *   1) allocate the requested block.
82  *   2) allocate a rotationally optimal block in the same cylinder.
83  *   3) allocate a block in the same cylinder group.
84  *   4) quadratically rehash into other cylinder groups, until an
85  *      available block is located.
86  * If no block preference is given the following hierarchy is used
87  * to allocate a block:
88  *   1) allocate a block in the cylinder group that contains the
89  *      inode for the file.
90  *   2) quadratically rehash into other cylinder groups, until an
91  *      available block is located.
92  */
93 int
ffs_alloc(ip,lbn,bpref,size,cred,bnp)94 ffs_alloc(ip, lbn, bpref, size, cred, bnp)
95 	register struct inode *ip;
96 	daddr_t lbn, bpref;
97 	int size;
98 	struct ucred *cred;
99 	daddr_t *bnp;
100 {
101 	register struct fs *fs;
102 	daddr_t bno;
103 	int cg;
104 	int error;
105 
106 	*bnp = 0;
107 	fs = ip->i_fs;
108 #ifdef DIAGNOSTIC
109 	if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0) {
110 		printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n",
111 		    ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt);
112 		panic("ffs_alloc: bad size");
113 	}
114 	if (cred == NOCRED)
115 		panic("ffs_alloc: missing credential");
116 #endif /* DIAGNOSTIC */
117 	if (size == fs->fs_bsize && fs->fs_cstotal.cs_nbfree == 0)
118 		goto nospace;
119 	if (cred->cr_uid != 0 && freespace(fs, fs->fs_minfree) <= 0)
120 		goto nospace;
121 
122 	if ((error = ufs_quota_alloc_blocks(ip, btodb(size), cred)) != 0)
123 		return (error);
124 
125 	if (bpref >= fs->fs_size)
126 		bpref = 0;
127 	if (bpref == 0)
128 		cg = ino_to_cg(fs, ip->i_number);
129 	else
130 		cg = dtog(fs, bpref);
131 	bno = (daddr_t)ffs_hashalloc(ip, cg, (long)bpref, size, ffs_alloccg);
132 	if (bno > 0) {
133 		ip->i_ffs_blocks += btodb(size);
134 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
135 		*bnp = bno;
136 		return (0);
137 	}
138 
139 	/*
140 	 * Restore user's disk quota because allocation failed.
141 	 */
142 	(void) ufs_quota_free_blocks(ip, btodb(size), cred);
143 
144 nospace:
145 	ffs_fserr(fs, cred->cr_uid, "file system full");
146 	uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt);
147 	return (ENOSPC);
148 }
149 
150 /*
151  * Reallocate a fragment to a bigger size
152  *
153  * The number and size of the old block is given, and a preference
154  * and new size is also specified. The allocator attempts to extend
155  * the original block. Failing that, the regular block allocator is
156  * invoked to get an appropriate block.
157  */
158 int
ffs_realloccg(ip,lbprev,bpref,osize,nsize,cred,bpp,blknop)159 ffs_realloccg(ip, lbprev, bpref, osize, nsize, cred, bpp, blknop)
160 	register struct inode *ip;
161 	daddr_t lbprev;
162 	daddr_t bpref;
163 	int osize, nsize;
164 	struct ucred *cred;
165 	struct buf **bpp;
166 	daddr_t *blknop;
167 {
168 	register struct fs *fs;
169 	struct buf *bp = NULL;
170 	daddr_t quota_updated = 0;
171 	int cg, request, error;
172 	daddr_t bprev, bno;
173 
174 	if (bpp != NULL)
175 		*bpp = NULL;
176 	fs = ip->i_fs;
177 #ifdef DIAGNOSTIC
178 	if ((u_int)osize > fs->fs_bsize || fragoff(fs, osize) != 0 ||
179 	    (u_int)nsize > fs->fs_bsize || fragoff(fs, nsize) != 0) {
180 		printf(
181 		    "dev = 0x%x, bsize = %d, osize = %d, nsize = %d, fs = %s\n",
182 		    ip->i_dev, fs->fs_bsize, osize, nsize, fs->fs_fsmnt);
183 		panic("ffs_realloccg: bad size");
184 	}
185 	if (cred == NOCRED)
186 		panic("ffs_realloccg: missing credential");
187 #endif /* DIAGNOSTIC */
188 	if (cred->cr_uid != 0 && freespace(fs, fs->fs_minfree) <= 0)
189 		goto nospace;
190 	if ((bprev = ip->i_ffs_db[lbprev]) == 0) {
191 		printf("dev = 0x%x, bsize = %d, bprev = %d, fs = %s\n",
192 		    ip->i_dev, fs->fs_bsize, bprev, fs->fs_fsmnt);
193 		panic("ffs_realloccg: bad bprev");
194 	}
195 	/*
196 	 * Allocate the extra space in the buffer.
197 	 */
198 	if (bpp != NULL &&
199 	    (error = bread(ITOV(ip), lbprev, osize, NOCRED, &bp)) != 0)
200 		goto error;
201 
202 	if ((error = ufs_quota_alloc_blocks(ip, btodb(nsize - osize), cred))
203 	    != 0)
204 		goto error;
205 
206 	quota_updated = btodb(nsize - osize);
207 
208 	/*
209 	 * Check for extension in the existing location.
210 	 */
211 	cg = dtog(fs, bprev);
212 	if ((bno = ffs_fragextend(ip, cg, (long)bprev, osize, nsize)) != 0) {
213 		ip->i_ffs_blocks += btodb(nsize - osize);
214 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
215 		if (bpp != NULL) {
216 			if (bp->b_blkno != fsbtodb(fs, bno))
217 				panic("ffs_realloccg: bad blockno");
218 			allocbuf(bp, nsize);
219 			bp->b_flags |= B_DONE;
220 			bzero((char *)bp->b_data + osize, (u_int)nsize - osize);
221 			*bpp = bp;
222 		}
223 		if (blknop != NULL) {
224 			*blknop = bno;
225 		}
226 		return (0);
227 	}
228 	/*
229 	 * Allocate a new disk location.
230 	 */
231 	if (bpref >= fs->fs_size)
232 		bpref = 0;
233 	switch ((int)fs->fs_optim) {
234 	case FS_OPTSPACE:
235 		/*
236 		 * Allocate an exact sized fragment. Although this makes
237 		 * best use of space, we will waste time relocating it if
238 		 * the file continues to grow. If the fragmentation is
239 		 * less than half of the minimum free reserve, we choose
240 		 * to begin optimizing for time.
241 		 */
242 		request = nsize;
243 		if (fs->fs_minfree < 5 ||
244 		    fs->fs_cstotal.cs_nffree >
245 		    fs->fs_dsize * fs->fs_minfree / (2 * 100))
246 			break;
247 		log(LOG_NOTICE, "%s: optimization changed from SPACE to TIME\n",
248 			fs->fs_fsmnt);
249 		fs->fs_optim = FS_OPTTIME;
250 		break;
251 	case FS_OPTTIME:
252 		/*
253 		 * At this point we have discovered a file that is trying to
254 		 * grow a small fragment to a larger fragment. To save time,
255 		 * we allocate a full sized block, then free the unused portion.
256 		 * If the file continues to grow, the `ffs_fragextend' call
257 		 * above will be able to grow it in place without further
258 		 * copying. If aberrant programs cause disk fragmentation to
259 		 * grow within 2% of the free reserve, we choose to begin
260 		 * optimizing for space.
261 		 */
262 		request = fs->fs_bsize;
263 		if (fs->fs_cstotal.cs_nffree <
264 		    fs->fs_dsize * (fs->fs_minfree - 2) / 100)
265 			break;
266 		log(LOG_NOTICE, "%s: optimization changed from TIME to SPACE\n",
267 			fs->fs_fsmnt);
268 		fs->fs_optim = FS_OPTSPACE;
269 		break;
270 	default:
271 		printf("dev = 0x%x, optim = %d, fs = %s\n",
272 		    ip->i_dev, fs->fs_optim, fs->fs_fsmnt);
273 		panic("ffs_realloccg: bad optim");
274 		/* NOTREACHED */
275 	}
276 	bno = (daddr_t)ffs_hashalloc(ip, cg, (long)bpref, request,
277 	    			     ffs_alloccg);
278 	if (bno <= 0)
279 		goto nospace;
280 
281 	(void) uvm_vnp_uncache(ITOV(ip));
282 	if (!DOINGSOFTDEP(ITOV(ip)))
283 		ffs_blkfree(ip, bprev, (long)osize);
284 	if (nsize < request)
285 		ffs_blkfree(ip, bno + numfrags(fs, nsize),
286 		    (long)(request - nsize));
287 	ip->i_ffs_blocks += btodb(nsize - osize);
288 	ip->i_flag |= IN_CHANGE | IN_UPDATE;
289 	if (bpp != NULL) {
290 		bp->b_blkno = fsbtodb(fs, bno);
291 		allocbuf(bp, nsize);
292 		bp->b_flags |= B_DONE;
293 		bzero((char *)bp->b_data + osize, (u_int)nsize - osize);
294 		*bpp = bp;
295 	}
296 	if (blknop != NULL) {
297 		*blknop = bno;
298 	}
299 	return (0);
300 
301 nospace:
302 	/*
303 	 * no space available
304 	 */
305 	ffs_fserr(fs, cred->cr_uid, "file system full");
306 	uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt);
307 	error = ENOSPC;
308 
309 error:
310 	if (bp != NULL) {
311 		brelse(bp);
312 		bp = NULL;
313 	}
314 
315  	/*
316 	 * Restore user's disk quota because allocation failed.
317 	 */
318 	if (quota_updated != 0)
319 		(void)ufs_quota_free_blocks(ip, quota_updated, cred);
320 
321 	return error;
322 }
323 
324 /*
325  * Reallocate a sequence of blocks into a contiguous sequence of blocks.
326  *
327  * The vnode and an array of buffer pointers for a range of sequential
328  * logical blocks to be made contiguous are given. The allocator attempts
329  * to find a range of sequential blocks starting as close as possible to
330  * an fs_rotdelay offset from the end of the allocation for the logical
331  * block immediately preceding the current range. If successful, the
332  * physical block numbers in the buffer pointers and in the inode are
333  * changed to reflect the new allocation. If unsuccessful, the allocation
334  * is left unchanged. The success in doing the reallocation is returned.
335  * Note that the error return is not reflected back to the user. Rather
336  * the previous block allocation will be used.
337  */
338 
339 int doasyncfree = 1;
340 int doreallocblks = 1;
341 int prtrealloc = 0;
342 
343 int
ffs_reallocblks(v)344 ffs_reallocblks(v)
345 	void *v;
346 {
347 	struct vop_reallocblks_args /* {
348 		struct vnode *a_vp;
349 		struct cluster_save *a_buflist;
350 	} */ *ap = v;
351 	struct fs *fs;
352 	struct inode *ip;
353 	struct vnode *vp;
354 	struct buf *sbp, *ebp;
355 	daddr_t *bap, *sbap, *ebap = NULL;
356 	struct cluster_save *buflist;
357 	daddr_t start_lbn, end_lbn, soff, newblk, blkno;
358 	struct indir start_ap[NIADDR + 1], end_ap[NIADDR + 1], *idp;
359 	int i, len, start_lvl, end_lvl, pref, ssize;
360 
361 	if (doreallocblks == 0)
362 		return (ENOSPC);
363 
364 	vp = ap->a_vp;
365 	ip = VTOI(vp);
366 	fs = ip->i_fs;
367 	if (fs->fs_contigsumsize <= 0)
368 		return (ENOSPC);
369 	buflist = ap->a_buflist;
370 	len = buflist->bs_nchildren;
371 	start_lbn = buflist->bs_children[0]->b_lblkno;
372 	end_lbn = start_lbn + len - 1;
373 
374 #ifdef DIAGNOSTIC
375 	for (i = 0; i < len; i++)
376 		if (!ffs_checkblk(ip,
377 		   dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
378 			panic("ffs_reallocblks: unallocated block 1");
379 
380 	for (i = 1; i < len; i++)
381 		if (buflist->bs_children[i]->b_lblkno != start_lbn + i)
382 			panic("ffs_reallocblks: non-logical cluster");
383 
384 	blkno = buflist->bs_children[0]->b_blkno;
385 	ssize = fsbtodb(fs, fs->fs_frag);
386 	for (i = 1; i < len - 1; i++)
387 		if (buflist->bs_children[i]->b_blkno != blkno + (i * ssize))
388 			panic("ffs_reallocblks: non-physical cluster %d", i);
389 #endif
390 	/*
391 	 * If the latest allocation is in a new cylinder group, assume that
392 	 * the filesystem has decided to move and do not force it back to
393 	 * the previous cylinder group.
394 	 */
395 	if (dtog(fs, dbtofsb(fs, buflist->bs_children[0]->b_blkno)) !=
396 	    dtog(fs, dbtofsb(fs, buflist->bs_children[len - 1]->b_blkno)))
397 		return (ENOSPC);
398 	if (ufs_getlbns(vp, start_lbn, start_ap, &start_lvl) ||
399 	    ufs_getlbns(vp, end_lbn, end_ap, &end_lvl))
400 		return (ENOSPC);
401 	/*
402 	 * Get the starting offset and block map for the first block.
403 	 */
404 	if (start_lvl == 0) {
405 		sbap = &ip->i_ffs_db[0];
406 		soff = start_lbn;
407 	} else {
408 		idp = &start_ap[start_lvl - 1];
409 		if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &sbp)) {
410 			brelse(sbp);
411 			return (ENOSPC);
412 		}
413 		sbap = (daddr_t *)sbp->b_data;
414 		soff = idp->in_off;
415 	}
416 	/*
417 	 * Find the preferred location for the cluster.
418 	 */
419 	pref = ffs_blkpref(ip, start_lbn, soff, sbap);
420 	/*
421 	 * If the block range spans two block maps, get the second map.
422 	 */
423 	if (end_lvl == 0 || (idp = &end_ap[end_lvl - 1])->in_off + 1 >= len) {
424 		ssize = len;
425 	} else {
426 #ifdef DIAGNOSTIC
427 		if (start_lvl > 1 &&
428 		    start_ap[start_lvl-1].in_lbn == idp->in_lbn)
429 			panic("ffs_reallocblk: start == end");
430 #endif
431 		ssize = len - (idp->in_off + 1);
432 		if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &ebp))
433 			goto fail;
434 		ebap = (daddr_t *)ebp->b_data;
435 	}
436 	/*
437 	 * Search the block map looking for an allocation of the desired size.
438 	 */
439 	if ((newblk = (daddr_t)ffs_hashalloc(ip, dtog(fs, pref), (long)pref,
440 	    len, ffs_clusteralloc)) == 0)
441 		goto fail;
442 	/*
443 	 * We have found a new contiguous block.
444 	 *
445 	 * First we have to replace the old block pointers with the new
446 	 * block pointers in the inode and indirect blocks associated
447 	 * with the file.
448 	 */
449 #ifdef DEBUG
450 	if (prtrealloc)
451 		printf("realloc: ino %d, lbns %d-%d\n\told:", ip->i_number,
452 		    start_lbn, end_lbn);
453 #endif
454 	blkno = newblk;
455 	for (bap = &sbap[soff], i = 0; i < len; i++, blkno += fs->fs_frag) {
456 		if (i == ssize) {
457 			bap = ebap;
458 			soff = -i;
459 		}
460 #ifdef DIAGNOSTIC
461 		if (!ffs_checkblk(ip,
462 		   dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
463 			panic("ffs_reallocblks: unallocated block 2");
464 		if (dbtofsb(fs, buflist->bs_children[i]->b_blkno) != *bap)
465 			panic("ffs_reallocblks: alloc mismatch");
466 #endif
467 #ifdef DEBUG
468 		if (prtrealloc)
469 			printf(" %d,", *bap);
470 #endif
471 		if (DOINGSOFTDEP(vp)) {
472 			if (sbap == &ip->i_ffs_db[0] && i < ssize)
473 				softdep_setup_allocdirect(ip, start_lbn + i,
474 				    blkno, *bap, fs->fs_bsize, fs->fs_bsize,
475 				    buflist->bs_children[i]);
476 			else
477 				softdep_setup_allocindir_page(ip, start_lbn + i,
478 				    i < ssize ? sbp : ebp, soff + i, blkno,
479 				    *bap, buflist->bs_children[i]);
480 		}
481 
482 		*bap++ = blkno;
483 	}
484 	/*
485 	 * Next we must write out the modified inode and indirect blocks.
486 	 * For strict correctness, the writes should be synchronous since
487 	 * the old block values may have been written to disk. In practise
488 	 * they are almost never written, but if we are concerned about
489 	 * strict correctness, the `doasyncfree' flag should be set to zero.
490 	 *
491 	 * The test on `doasyncfree' should be changed to test a flag
492 	 * that shows whether the associated buffers and inodes have
493 	 * been written. The flag should be set when the cluster is
494 	 * started and cleared whenever the buffer or inode is flushed.
495 	 * We can then check below to see if it is set, and do the
496 	 * synchronous write only when it has been cleared.
497 	 */
498 	if (sbap != &ip->i_ffs_db[0]) {
499 		if (doasyncfree)
500 			bdwrite(sbp);
501 		else
502 			bwrite(sbp);
503 	} else {
504 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
505 		if (!doasyncfree) {
506 			UFS_UPDATE(ip, MNT_WAIT);
507 		}
508 	}
509 	if (ssize < len) {
510 		if (doasyncfree)
511 			bdwrite(ebp);
512 		else
513 			bwrite(ebp);
514 	}
515 	/*
516 	 * Last, free the old blocks and assign the new blocks to the buffers.
517 	 */
518 #ifdef DEBUG
519 	if (prtrealloc)
520 		printf("\n\tnew:");
521 #endif
522 	for (blkno = newblk, i = 0; i < len; i++, blkno += fs->fs_frag) {
523 		if (!DOINGSOFTDEP(vp))
524 			ffs_blkfree(ip,
525 			    dbtofsb(fs, buflist->bs_children[i]->b_blkno),
526 			    fs->fs_bsize);
527 		buflist->bs_children[i]->b_blkno = fsbtodb(fs, blkno);
528 #ifdef DIAGNOSTIC
529 		if (!ffs_checkblk(ip,
530 		   dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
531 			panic("ffs_reallocblks: unallocated block 3");
532 		if (prtrealloc)
533 			printf(" %d,", blkno);
534 #endif
535 	}
536 #ifdef DEBUG
537 	if (prtrealloc) {
538 		prtrealloc--;
539 		printf("\n");
540 	}
541 #endif
542 	return (0);
543 
544 fail:
545 	if (ssize < len)
546 		brelse(ebp);
547 	if (sbap != &ip->i_ffs_db[0])
548 		brelse(sbp);
549 	return (ENOSPC);
550 }
551 
552 /*
553  * Allocate an inode in the file system.
554  *
555  * If allocating a directory, use ffs_dirpref to select the inode.
556  * If allocating in a directory, the following hierarchy is followed:
557  *   1) allocate the preferred inode.
558  *   2) allocate an inode in the same cylinder group.
559  *   3) quadratically rehash into other cylinder groups, until an
560  *      available inode is located.
561  * If no inode preference is given the following hierarchy is used
562  * to allocate an inode:
563  *   1) allocate an inode in cylinder group 0.
564  *   2) quadratically rehash into other cylinder groups, until an
565  *      available inode is located.
566  */
567 int
ffs_inode_alloc(struct inode * pip,mode_t mode,struct ucred * cred,struct vnode ** vpp)568 ffs_inode_alloc(struct inode *pip, mode_t mode, struct ucred *cred,
569     struct vnode **vpp)
570 {
571 	struct vnode *pvp = ITOV(pip);
572 	struct fs *fs;
573 	struct inode *ip;
574 	ino_t ino, ipref;
575 	int cg, error;
576 
577 	*vpp = NULL;
578 	fs = pip->i_fs;
579 	if (fs->fs_cstotal.cs_nifree == 0)
580 		goto noinodes;
581 
582 	if ((mode & IFMT) == IFDIR)
583 		ipref = ffs_dirpref(pip);
584 	else
585 		ipref = pip->i_number;
586 	if (ipref >= fs->fs_ncg * fs->fs_ipg)
587 		ipref = 0;
588 	cg = ino_to_cg(fs, ipref);
589 
590 	/*
591 	 * Track number of dirs created one after another
592 	 * in a same cg without intervening by files.
593 	 */
594 	if ((mode & IFMT) == IFDIR) {
595 		if (fs->fs_contigdirs[cg] < 255)
596 			fs->fs_contigdirs[cg]++;
597 	} else {
598 		if (fs->fs_contigdirs[cg] > 0)
599 			fs->fs_contigdirs[cg]--;
600 	}
601 	ino = (ino_t)ffs_hashalloc(pip, cg, (long)ipref, mode, ffs_nodealloccg);
602 	if (ino == 0)
603 		goto noinodes;
604 	error = VFS_VGET(pvp->v_mount, ino, vpp);
605 	if (error) {
606 		ffs_inode_free(pip, ino, mode);
607 		return (error);
608 	}
609 	ip = VTOI(*vpp);
610 	if (ip->i_ffs_mode) {
611 		printf("mode = 0%o, inum = %d, fs = %s\n",
612 		    ip->i_ffs_mode, ip->i_number, fs->fs_fsmnt);
613 		panic("ffs_valloc: dup alloc");
614 	}
615 	if (ip->i_ffs_blocks) {				/* XXX */
616 		printf("free inode %s/%d had %d blocks\n",
617 		    fs->fs_fsmnt, ino, ip->i_ffs_blocks);
618 		ip->i_ffs_blocks = 0;
619 	}
620 	ip->i_ffs_flags = 0;
621 	/*
622 	 * Set up a new generation number for this inode.
623 	 * XXX - just increment for now, this is wrong! (millert)
624 	 *       Need a way to preserve randomization.
625 	 */
626 	if (ip->i_ffs_gen == 0 || ++(ip->i_ffs_gen) == 0)
627 		ip->i_ffs_gen = arc4random() & INT_MAX;
628 	if (ip->i_ffs_gen == 0 || ip->i_ffs_gen == -1)
629 		ip->i_ffs_gen = 1;			/* shouldn't happen */
630 	return (0);
631 noinodes:
632 	ffs_fserr(fs, cred->cr_uid, "out of inodes");
633 	uprintf("\n%s: create/symlink failed, no inodes free\n", fs->fs_fsmnt);
634 	return (ENOSPC);
635 }
636 
637 /*
638  * Find a cylinder group to place a directory.
639  *
640  * The policy implemented by this algorithm is to allocate a
641  * directory inode in the same cylinder group as its parent
642  * directory, but also to reserve space for its files inodes
643  * and data. Restrict the number of directories which may be
644  * allocated one after another in the same cylinder group
645  * without intervening allocation of files.
646  *
647  * If we allocate a first level directory then force allocation
648  * in another cylinder group.
649  */
650 static ino_t
ffs_dirpref(pip)651 ffs_dirpref(pip)
652 	struct inode *pip;
653 {
654 	register struct fs *fs;
655 	int	cg, prefcg, dirsize, cgsize;
656 	int	avgifree, avgbfree, avgndir, curdirsize;
657 	int	minifree, minbfree, maxndir;
658 	int	mincg, minndir;
659 	int	maxcontigdirs;
660 
661 	fs = pip->i_fs;
662 
663 	avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg;
664 	avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
665 	avgndir = fs->fs_cstotal.cs_ndir / fs->fs_ncg;
666 #if 1
667 
668 	/*
669 	 * Force allocation in another cg if creating a first level dir.
670 	 */
671 	if (ITOV(pip)->v_flag & VROOT) {
672 		prefcg = (arc4random() & INT_MAX) % fs->fs_ncg;
673 		mincg = prefcg;
674 		minndir = fs->fs_ipg;
675 		for (cg = prefcg; cg < fs->fs_ncg; cg++)
676 			if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
677 			    fs->fs_cs(fs, cg).cs_nifree >= avgifree &&
678 			    fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
679 				mincg = cg;
680 				minndir = fs->fs_cs(fs, cg).cs_ndir;
681 			}
682 		for (cg = 0; cg < prefcg; cg++)
683 			if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
684 			    fs->fs_cs(fs, cg).cs_nifree >= avgifree &&
685 			    fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
686 				mincg = cg;
687 				minndir = fs->fs_cs(fs, cg).cs_ndir;
688 			}
689 		cg = mincg;
690 		goto end;
691 	} else
692 		prefcg = ino_to_cg(fs, pip->i_number);
693 #else
694 	prefcg = ino_to_cg(fs, pip->i_number);
695 #endif
696 
697 	/*
698 	 * Count various limits which used for
699 	 * optimal allocation of a directory inode.
700 	 */
701 #if 1
702 	maxndir = min(avgndir + fs->fs_ipg / 16, fs->fs_ipg);
703 	minifree = avgifree - fs->fs_ipg / 4;
704 	if (minifree < 0)
705 		minifree = 0;
706 	minbfree = avgbfree - fs->fs_fpg / fs->fs_frag / 4;
707 	if (minbfree < 0)
708 		minbfree = 0;
709 #else
710 	maxndir = avgndir + (fs->fs_ipg - avgndir) / 16;
711 	minifree = avgifree * 3 / 4;
712 	minbfree = avgbfree * 3 / 4;
713 #endif
714 	cgsize = fs->fs_fsize * fs->fs_fpg;
715 	dirsize = fs->fs_avgfilesize * fs->fs_avgfpdir;
716 	curdirsize = avgndir ? (cgsize - avgbfree * fs->fs_bsize) / avgndir : 0;
717 	if (dirsize < curdirsize)
718 		dirsize = curdirsize;
719 	maxcontigdirs = min(cgsize / dirsize, 255);
720 	if (fs->fs_avgfpdir > 0)
721 		maxcontigdirs = min(maxcontigdirs,
722 				    fs->fs_ipg / fs->fs_avgfpdir);
723 	if (maxcontigdirs == 0)
724 		maxcontigdirs = 1;
725 
726 	/*
727 	 * Limit number of dirs in one cg and reserve space for
728 	 * regular files, but only if we have no deficit in
729 	 * inodes or space.
730 	 */
731 	for (cg = prefcg; cg < fs->fs_ncg; cg++)
732 		if (fs->fs_cs(fs, cg).cs_ndir < maxndir &&
733 		    fs->fs_cs(fs, cg).cs_nifree >= minifree &&
734 	    	    fs->fs_cs(fs, cg).cs_nbfree >= minbfree) {
735 			if (fs->fs_contigdirs[cg] < maxcontigdirs)
736 				goto end;
737 		}
738 	for (cg = 0; cg < prefcg; cg++)
739 		if (fs->fs_cs(fs, cg).cs_ndir < maxndir &&
740 		    fs->fs_cs(fs, cg).cs_nifree >= minifree &&
741 	    	    fs->fs_cs(fs, cg).cs_nbfree >= minbfree) {
742 			if (fs->fs_contigdirs[cg] < maxcontigdirs)
743 				goto end;
744 		}
745 	/*
746 	 * This is a backstop when we have deficit in space.
747 	 */
748 	for (cg = prefcg; cg < fs->fs_ncg; cg++)
749 		if (fs->fs_cs(fs, cg).cs_nifree >= avgifree)
750 			goto end;
751 	for (cg = 0; cg < prefcg; cg++)
752 		if (fs->fs_cs(fs, cg).cs_nifree >= avgifree)
753 			goto end;
754 end:
755 	return ((ino_t)(fs->fs_ipg * cg));
756 }
757 
758 /*
759  * Select the desired position for the next block in a file.  The file is
760  * logically divided into sections. The first section is composed of the
761  * direct blocks. Each additional section contains fs_maxbpg blocks.
762  *
763  * If no blocks have been allocated in the first section, the policy is to
764  * request a block in the same cylinder group as the inode that describes
765  * the file. If no blocks have been allocated in any other section, the
766  * policy is to place the section in a cylinder group with a greater than
767  * average number of free blocks.  An appropriate cylinder group is found
768  * by using a rotor that sweeps the cylinder groups. When a new group of
769  * blocks is needed, the sweep begins in the cylinder group following the
770  * cylinder group from which the previous allocation was made. The sweep
771  * continues until a cylinder group with greater than the average number
772  * of free blocks is found. If the allocation is for the first block in an
773  * indirect block, the information on the previous allocation is unavailable;
774  * here a best guess is made based upon the logical block number being
775  * allocated.
776  *
777  * If a section is already partially allocated, the policy is to
778  * contiguously allocate fs_maxcontig blocks.  The end of one of these
779  * contiguous blocks and the beginning of the next is physically separated
780  * so that the disk head will be in transit between them for at least
781  * fs_rotdelay milliseconds.  This is to allow time for the processor to
782  * schedule another I/O transfer.
783  */
784 daddr_t
ffs_blkpref(ip,lbn,indx,bap)785 ffs_blkpref(ip, lbn, indx, bap)
786 	struct inode *ip;
787 	daddr_t lbn;
788 	int indx;
789 	daddr_t *bap;
790 {
791 	register struct fs *fs;
792 	register int cg;
793 	int avgbfree, startcg;
794 	daddr_t nextblk;
795 
796 	fs = ip->i_fs;
797 	if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
798 		if (lbn < NDADDR + NINDIR(fs)) {
799 			cg = ino_to_cg(fs, ip->i_number);
800 			return (fs->fs_fpg * cg + fs->fs_frag);
801 		}
802 		/*
803 		 * Find a cylinder with greater than average number of
804 		 * unused data blocks.
805 		 */
806 		if (indx == 0 || bap[indx - 1] == 0)
807 			startcg =
808 			    ino_to_cg(fs, ip->i_number) + lbn / fs->fs_maxbpg;
809 		else
810 			startcg = dtog(fs, bap[indx - 1]) + 1;
811 		startcg %= fs->fs_ncg;
812 		avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
813 		for (cg = startcg; cg < fs->fs_ncg; cg++)
814 			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
815 				fs->fs_cgrotor = cg;
816 				return (fs->fs_fpg * cg + fs->fs_frag);
817 			}
818 		for (cg = 0; cg <= startcg; cg++)
819 			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
820 				fs->fs_cgrotor = cg;
821 				return (fs->fs_fpg * cg + fs->fs_frag);
822 			}
823 		return (0);
824 	}
825 	/*
826 	 * One or more previous blocks have been laid out. If less
827 	 * than fs_maxcontig previous blocks are contiguous, the
828 	 * next block is requested contiguously, otherwise it is
829 	 * requested rotationally delayed by fs_rotdelay milliseconds.
830 	 */
831 	nextblk = bap[indx - 1] + fs->fs_frag;
832 	if (indx < fs->fs_maxcontig || bap[indx - fs->fs_maxcontig] +
833 	    blkstofrags(fs, fs->fs_maxcontig) != nextblk)
834 		return (nextblk);
835 	if (fs->fs_rotdelay != 0)
836 		/*
837 		 * Here we convert ms of delay to frags as:
838 		 * (frags) = (ms) * (rev/sec) * (sect/rev) /
839 		 *	((sect/frag) * (ms/sec))
840 		 * then round up to the next block.
841 		 */
842 		nextblk += roundup(fs->fs_rotdelay * fs->fs_rps * fs->fs_nsect /
843 		    (NSPF(fs) * 1000), fs->fs_frag);
844 	return (nextblk);
845 }
846 
847 /*
848  * Implement the cylinder overflow algorithm.
849  *
850  * The policy implemented by this algorithm is:
851  *   1) allocate the block in its requested cylinder group.
852  *   2) quadratically rehash on the cylinder group number.
853  *   3) brute force search for a free block.
854  */
855 /*VARARGS5*/
856 static u_long
ffs_hashalloc(ip,cg,pref,size,allocator)857 ffs_hashalloc(ip, cg, pref, size, allocator)
858 	struct inode *ip;
859 	int cg;
860 	long pref;
861 	int size;	/* size for data blocks, mode for inodes */
862 	daddr_t (*allocator)(struct inode *, int, daddr_t, int);
863 {
864 	register struct fs *fs;
865 	long result;
866 	int i, icg = cg;
867 
868 	fs = ip->i_fs;
869 	/*
870 	 * 1: preferred cylinder group
871 	 */
872 	result = (*allocator)(ip, cg, pref, size);
873 	if (result)
874 		return (result);
875 	/*
876 	 * 2: quadratic rehash
877 	 */
878 	for (i = 1; i < fs->fs_ncg; i *= 2) {
879 		cg += i;
880 		if (cg >= fs->fs_ncg)
881 			cg -= fs->fs_ncg;
882 		result = (*allocator)(ip, cg, 0, size);
883 		if (result)
884 			return (result);
885 	}
886 	/*
887 	 * 3: brute force search
888 	 * Note that we start at i == 2, since 0 was checked initially,
889 	 * and 1 is always checked in the quadratic rehash.
890 	 */
891 	cg = (icg + 2) % fs->fs_ncg;
892 	for (i = 2; i < fs->fs_ncg; i++) {
893 		result = (*allocator)(ip, cg, 0, size);
894 		if (result)
895 			return (result);
896 		cg++;
897 		if (cg == fs->fs_ncg)
898 			cg = 0;
899 	}
900 	return (0);
901 }
902 
903 /*
904  * Determine whether a fragment can be extended.
905  *
906  * Check to see if the necessary fragments are available, and
907  * if they are, allocate them.
908  */
909 static daddr_t
ffs_fragextend(ip,cg,bprev,osize,nsize)910 ffs_fragextend(ip, cg, bprev, osize, nsize)
911 	struct inode *ip;
912 	int cg;
913 	long bprev;
914 	int osize, nsize;
915 {
916 	register struct fs *fs;
917 	register struct cg *cgp;
918 	struct buf *bp;
919 	long bno;
920 	int frags, bbase;
921 	int i, error;
922 
923 	fs = ip->i_fs;
924 	if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize))
925 		return (0);
926 	frags = numfrags(fs, nsize);
927 	bbase = fragnum(fs, bprev);
928 	if (bbase > fragnum(fs, (bprev + frags - 1))) {
929 		/* cannot extend across a block boundary */
930 		return (0);
931 	}
932 	error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
933 		(int)fs->fs_cgsize, NOCRED, &bp);
934 	if (error) {
935 		brelse(bp);
936 		return (0);
937 	}
938 	cgp = (struct cg *)bp->b_data;
939 	if (!cg_chkmagic(cgp)) {
940 		brelse(bp);
941 		return (0);
942 	}
943 	cgp->cg_time = time.tv_sec;
944 	bno = dtogd(fs, bprev);
945 	for (i = numfrags(fs, osize); i < frags; i++)
946 		if (isclr(cg_blksfree(cgp), bno + i)) {
947 			brelse(bp);
948 			return (0);
949 		}
950 	/*
951 	 * the current fragment can be extended
952 	 * deduct the count on fragment being extended into
953 	 * increase the count on the remaining fragment (if any)
954 	 * allocate the extended piece
955 	 */
956 	for (i = frags; i < fs->fs_frag - bbase; i++)
957 		if (isclr(cg_blksfree(cgp), bno + i))
958 			break;
959 	cgp->cg_frsum[i - numfrags(fs, osize)]--;
960 	if (i != frags)
961 		cgp->cg_frsum[i - frags]++;
962 	for (i = numfrags(fs, osize); i < frags; i++) {
963 		clrbit(cg_blksfree(cgp), bno + i);
964 		cgp->cg_cs.cs_nffree--;
965 		fs->fs_cstotal.cs_nffree--;
966 		fs->fs_cs(fs, cg).cs_nffree--;
967 	}
968 	fs->fs_fmod = 1;
969 	if (DOINGSOFTDEP(ITOV(ip)))
970 		softdep_setup_blkmapdep(bp, fs, bprev);
971 
972 	bdwrite(bp);
973 	return (bprev);
974 }
975 
976 /*
977  * Determine whether a block can be allocated.
978  *
979  * Check to see if a block of the appropriate size is available,
980  * and if it is, allocate it.
981  */
982 static daddr_t
ffs_alloccg(ip,cg,bpref,size)983 ffs_alloccg(ip, cg, bpref, size)
984 	struct inode *ip;
985 	int cg;
986 	daddr_t bpref;
987 	int size;
988 {
989 	register struct fs *fs;
990 	register struct cg *cgp;
991 	struct buf *bp;
992 	daddr_t bno, blkno;
993 	int error, i, frags, allocsiz;
994 
995 	fs = ip->i_fs;
996 	if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize)
997 		return (0);
998 	error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
999 		(int)fs->fs_cgsize, NOCRED, &bp);
1000 	if (error) {
1001 		brelse(bp);
1002 		return (0);
1003 	}
1004 	cgp = (struct cg *)bp->b_data;
1005 	if (!cg_chkmagic(cgp) ||
1006 	    (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) {
1007 		brelse(bp);
1008 		return (0);
1009 	}
1010 	cgp->cg_time = time.tv_sec;
1011 	if (size == fs->fs_bsize) {
1012 		bno = ffs_alloccgblk(ip, bp, bpref);
1013 		bdwrite(bp);
1014 		return (bno);
1015 	}
1016 	/*
1017 	 * check to see if any fragments are already available
1018 	 * allocsiz is the size which will be allocated, hacking
1019 	 * it down to a smaller size if necessary
1020 	 */
1021 	frags = numfrags(fs, size);
1022 	for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++)
1023 		if (cgp->cg_frsum[allocsiz] != 0)
1024 			break;
1025 	if (allocsiz == fs->fs_frag) {
1026 		/*
1027 		 * no fragments were available, so a block will be
1028 		 * allocated, and hacked up
1029 		 */
1030 		if (cgp->cg_cs.cs_nbfree == 0) {
1031 			brelse(bp);
1032 			return (0);
1033 		}
1034 		bno = ffs_alloccgblk(ip, bp, bpref);
1035 		bpref = dtogd(fs, bno);
1036 		for (i = frags; i < fs->fs_frag; i++)
1037 			setbit(cg_blksfree(cgp), bpref + i);
1038 		i = fs->fs_frag - frags;
1039 		cgp->cg_cs.cs_nffree += i;
1040 		fs->fs_cstotal.cs_nffree += i;
1041 		fs->fs_cs(fs, cg).cs_nffree += i;
1042 		fs->fs_fmod = 1;
1043 		cgp->cg_frsum[i]++;
1044 		bdwrite(bp);
1045 		return (bno);
1046 	}
1047 	bno = ffs_mapsearch(fs, cgp, bpref, allocsiz);
1048 	if (bno < 0) {
1049 		brelse(bp);
1050 		return (0);
1051 	}
1052 
1053 	for (i = 0; i < frags; i++)
1054 		clrbit(cg_blksfree(cgp), bno + i);
1055 	cgp->cg_cs.cs_nffree -= frags;
1056 	fs->fs_cstotal.cs_nffree -= frags;
1057 	fs->fs_cs(fs, cg).cs_nffree -= frags;
1058 	fs->fs_fmod = 1;
1059 	cgp->cg_frsum[allocsiz]--;
1060 	if (frags != allocsiz)
1061 		cgp->cg_frsum[allocsiz - frags]++;
1062 
1063 	blkno = cg * fs->fs_fpg + bno;
1064 	if (DOINGSOFTDEP(ITOV(ip)))
1065 		softdep_setup_blkmapdep(bp, fs, blkno);
1066 	bdwrite(bp);
1067 	return ((u_long)blkno);
1068 }
1069 
1070 /*
1071  * Allocate a block in a cylinder group.
1072  *
1073  * This algorithm implements the following policy:
1074  *   1) allocate the requested block.
1075  *   2) allocate a rotationally optimal block in the same cylinder.
1076  *   3) allocate the next available block on the block rotor for the
1077  *      specified cylinder group.
1078  * Note that this routine only allocates fs_bsize blocks; these
1079  * blocks may be fragmented by the routine that allocates them.
1080  */
1081 static daddr_t
ffs_alloccgblk(ip,bp,bpref)1082 ffs_alloccgblk(ip, bp, bpref)
1083 	struct inode *ip;
1084 	struct buf *bp;
1085 	daddr_t bpref;
1086 {
1087 	struct fs *fs;
1088 	struct cg *cgp;
1089 	daddr_t bno, blkno;
1090 	int cylno, pos, delta;
1091 	short *cylbp;
1092 	register int i;
1093 
1094 	fs = ip->i_fs;
1095 	cgp = (struct cg *)bp->b_data;
1096 	if (bpref == 0 || dtog(fs, bpref) != cgp->cg_cgx) {
1097 		bpref = cgp->cg_rotor;
1098 		goto norot;
1099 	}
1100 	bpref = blknum(fs, bpref);
1101 	bpref = dtogd(fs, bpref);
1102 	/*
1103 	 * if the requested block is available, use it
1104 	 */
1105 	if (ffs_isblock(fs, cg_blksfree(cgp), fragstoblks(fs, bpref))) {
1106 		bno = bpref;
1107 		goto gotit;
1108 	}
1109 	if (fs->fs_cpc == 0 || fs->fs_nrpos <= 1) {
1110 		/*
1111 		 * Block layout information is not available.
1112 		 * Leaving bpref unchanged means we take the
1113 		 * next available free block following the one
1114 		 * we just allocated. Hopefully this will at
1115 		 * least hit a track cache on drives of unknown
1116 		 * geometry (e.g. SCSI).
1117 		 */
1118 		goto norot;
1119 	}
1120 	/*
1121 	 * check for a block available on the same cylinder
1122 	 */
1123 	cylno = cbtocylno(fs, bpref);
1124 	if (cg_blktot(cgp)[cylno] == 0)
1125 		goto norot;
1126 	/*
1127 	 * check the summary information to see if a block is
1128 	 * available in the requested cylinder starting at the
1129 	 * requested rotational position and proceeding around.
1130 	 */
1131 	cylbp = cg_blks(fs, cgp, cylno);
1132 	pos = cbtorpos(fs, bpref);
1133 	for (i = pos; i < fs->fs_nrpos; i++)
1134 		if (cylbp[i] > 0)
1135 			break;
1136 	if (i == fs->fs_nrpos)
1137 		for (i = 0; i < pos; i++)
1138 			if (cylbp[i] > 0)
1139 				break;
1140 	if (cylbp[i] > 0) {
1141 		/*
1142 		 * found a rotational position, now find the actual
1143 		 * block. A panic if none is actually there.
1144 		 */
1145 		pos = cylno % fs->fs_cpc;
1146 		bno = (cylno - pos) * fs->fs_spc / NSPB(fs);
1147 		if (fs_postbl(fs, pos)[i] == -1) {
1148 			printf("pos = %d, i = %d, fs = %s\n",
1149 			    pos, i, fs->fs_fsmnt);
1150 			panic("ffs_alloccgblk: cyl groups corrupted");
1151 		}
1152 		for (i = fs_postbl(fs, pos)[i];; ) {
1153 			if (ffs_isblock(fs, cg_blksfree(cgp), bno + i)) {
1154 				bno = blkstofrags(fs, (bno + i));
1155 				goto gotit;
1156 			}
1157 			delta = fs_rotbl(fs)[i];
1158 			if (delta <= 0 ||
1159 			    delta + i > fragstoblks(fs, fs->fs_fpg))
1160 				break;
1161 			i += delta;
1162 		}
1163 		printf("pos = %d, i = %d, fs = %s\n", pos, i, fs->fs_fsmnt);
1164 		panic("ffs_alloccgblk: can't find blk in cyl");
1165 	}
1166 norot:
1167 	/*
1168 	 * no blocks in the requested cylinder, so take next
1169 	 * available one in this cylinder group.
1170 	 */
1171 	bno = ffs_mapsearch(fs, cgp, bpref, (int)fs->fs_frag);
1172 	if (bno < 0)
1173 		return (0);
1174 	cgp->cg_rotor = bno;
1175 gotit:
1176 	blkno = fragstoblks(fs, bno);
1177 	ffs_clrblock(fs, cg_blksfree(cgp), (long)blkno);
1178 	ffs_clusteracct(fs, cgp, blkno, -1);
1179 	cgp->cg_cs.cs_nbfree--;
1180 	fs->fs_cstotal.cs_nbfree--;
1181 	fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--;
1182 	cylno = cbtocylno(fs, bno);
1183 	cg_blks(fs, cgp, cylno)[cbtorpos(fs, bno)]--;
1184 	cg_blktot(cgp)[cylno]--;
1185 	fs->fs_fmod = 1;
1186 	blkno = cgp->cg_cgx * fs->fs_fpg + bno;
1187 	if (DOINGSOFTDEP(ITOV(ip)))
1188 		softdep_setup_blkmapdep(bp, fs, blkno);
1189 	return (blkno);
1190 }
1191 
1192 /*
1193  * Determine whether a cluster can be allocated.
1194  *
1195  * We do not currently check for optimal rotational layout if there
1196  * are multiple choices in the same cylinder group. Instead we just
1197  * take the first one that we find following bpref.
1198  */
1199 static daddr_t
ffs_clusteralloc(ip,cg,bpref,len)1200 ffs_clusteralloc(ip, cg, bpref, len)
1201 	struct inode *ip;
1202 	int cg;
1203 	daddr_t bpref;
1204 	int len;
1205 {
1206 	register struct fs *fs;
1207 	register struct cg *cgp;
1208 	struct buf *bp;
1209 	int i, got, run, bno, bit, map;
1210 	u_char *mapp;
1211 	int32_t *lp;
1212 
1213 	fs = ip->i_fs;
1214 	if (fs->fs_maxcluster[cg] < len)
1215 		return (0);
1216 	if (bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize,
1217 	    NOCRED, &bp))
1218 		goto fail;
1219 	cgp = (struct cg *)bp->b_data;
1220 	if (!cg_chkmagic(cgp))
1221 		goto fail;
1222 	/*
1223 	 * Check to see if a cluster of the needed size (or bigger) is
1224 	 * available in this cylinder group.
1225 	 */
1226 	lp = &cg_clustersum(cgp)[len];
1227 	for (i = len; i <= fs->fs_contigsumsize; i++)
1228 		if (*lp++ > 0)
1229 			break;
1230 	if (i > fs->fs_contigsumsize) {
1231 		/*
1232 		 * This is the first time looking for a cluster in this
1233 		 * cylinder group. Update the cluster summary information
1234 		 * to reflect the true maximum sized cluster so that
1235 		 * future cluster allocation requests can avoid reading
1236 		 * the cylinder group map only to find no clusters.
1237 		 */
1238 		lp = &cg_clustersum(cgp)[len - 1];
1239 		for (i = len - 1; i > 0; i--)
1240 			if (*lp-- > 0)
1241 				break;
1242 		fs->fs_maxcluster[cg] = i;
1243 		goto fail;
1244 	}
1245 	/*
1246 	 * Search the cluster map to find a big enough cluster.
1247 	 * We take the first one that we find, even if it is larger
1248 	 * than we need as we prefer to get one close to the previous
1249 	 * block allocation. We do not search before the current
1250 	 * preference point as we do not want to allocate a block
1251 	 * that is allocated before the previous one (as we will
1252 	 * then have to wait for another pass of the elevator
1253 	 * algorithm before it will be read). We prefer to fail and
1254 	 * be recalled to try an allocation in the next cylinder group.
1255 	 */
1256 	if (dtog(fs, bpref) != cg)
1257 		bpref = 0;
1258 	else
1259 		bpref = fragstoblks(fs, dtogd(fs, blknum(fs, bpref)));
1260 	mapp = &cg_clustersfree(cgp)[bpref / NBBY];
1261 	map = *mapp++;
1262 	bit = 1 << (bpref % NBBY);
1263 	for (run = 0, got = bpref; got < cgp->cg_nclusterblks; got++) {
1264 		if ((map & bit) == 0) {
1265 			run = 0;
1266 		} else {
1267 			run++;
1268 			if (run == len)
1269 				break;
1270 		}
1271 		if ((got & (NBBY - 1)) != (NBBY - 1)) {
1272 			bit <<= 1;
1273 		} else {
1274 			map = *mapp++;
1275 			bit = 1;
1276 		}
1277 	}
1278 	if (got >= cgp->cg_nclusterblks)
1279 		goto fail;
1280 	/*
1281 	 * Allocate the cluster that we have found.
1282 	 */
1283 #ifdef DIAGNOSTIC
1284 	for (i = 1; i <= len; i++)
1285 		if (!ffs_isblock(fs, cg_blksfree(cgp), got - run + i))
1286 			panic("ffs_clusteralloc: map mismatch");
1287 #endif
1288 	bno = cg * fs->fs_fpg + blkstofrags(fs, got - run + 1);
1289 #ifdef DIAGNOSTIC
1290 	if (dtog(fs, bno) != cg)
1291 		panic("ffs_clusteralloc: allocated out of group");
1292 #endif
1293 
1294 	len = blkstofrags(fs, len);
1295 	for (i = 0; i < len; i += fs->fs_frag)
1296 		if (ffs_alloccgblk(ip, bp, bno + i) != bno + i)
1297 			panic("ffs_clusteralloc: lost block");
1298 	bdwrite(bp);
1299 	return (bno);
1300 
1301 fail:
1302 	brelse(bp);
1303 	return (0);
1304 }
1305 
1306 /*
1307  * Determine whether an inode can be allocated.
1308  *
1309  * Check to see if an inode is available, and if it is,
1310  * allocate it using the following policy:
1311  *   1) allocate the requested inode.
1312  *   2) allocate the next available inode after the requested
1313  *      inode in the specified cylinder group.
1314  */
1315 static daddr_t
ffs_nodealloccg(ip,cg,ipref,mode)1316 ffs_nodealloccg(ip, cg, ipref, mode)
1317 	struct inode *ip;
1318 	int cg;
1319 	daddr_t ipref;
1320 	int mode;
1321 {
1322 	register struct fs *fs;
1323 	register struct cg *cgp;
1324 	struct buf *bp;
1325 	int error, start, len, loc, map, i;
1326 
1327 	fs = ip->i_fs;
1328 	if (fs->fs_cs(fs, cg).cs_nifree == 0)
1329 		return (0);
1330 	error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
1331 		(int)fs->fs_cgsize, NOCRED, &bp);
1332 	if (error) {
1333 		brelse(bp);
1334 		return (0);
1335 	}
1336 	cgp = (struct cg *)bp->b_data;
1337 	if (!cg_chkmagic(cgp) || cgp->cg_cs.cs_nifree == 0) {
1338 		brelse(bp);
1339 		return (0);
1340 	}
1341 	cgp->cg_time = time.tv_sec;
1342 	if (ipref) {
1343 		ipref %= fs->fs_ipg;
1344 		if (isclr(cg_inosused(cgp), ipref))
1345 			goto gotit;
1346 	}
1347 	start = cgp->cg_irotor / NBBY;
1348 	len = howmany(fs->fs_ipg - cgp->cg_irotor, NBBY);
1349 	loc = skpc(0xff, len, &cg_inosused(cgp)[start]);
1350 	if (loc == 0) {
1351 		len = start + 1;
1352 		start = 0;
1353 		loc = skpc(0xff, len, &cg_inosused(cgp)[0]);
1354 		if (loc == 0) {
1355 			printf("cg = %d, irotor = %d, fs = %s\n",
1356 			    cg, cgp->cg_irotor, fs->fs_fsmnt);
1357 			panic("ffs_nodealloccg: map corrupted");
1358 			/* NOTREACHED */
1359 		}
1360 	}
1361 	i = start + len - loc;
1362 	map = cg_inosused(cgp)[i];
1363 	ipref = i * NBBY;
1364 	for (i = 1; i < (1 << NBBY); i <<= 1, ipref++) {
1365 		if ((map & i) == 0) {
1366 			cgp->cg_irotor = ipref;
1367 			goto gotit;
1368 		}
1369 	}
1370 	printf("fs = %s\n", fs->fs_fsmnt);
1371 	panic("ffs_nodealloccg: block not in map");
1372 	/* NOTREACHED */
1373 gotit:
1374 	if (DOINGSOFTDEP(ITOV(ip)))
1375 		softdep_setup_inomapdep(bp, ip, cg * fs->fs_ipg + ipref);
1376 
1377 	setbit(cg_inosused(cgp), ipref);
1378 	cgp->cg_cs.cs_nifree--;
1379 	fs->fs_cstotal.cs_nifree--;
1380 	fs->fs_cs(fs, cg).cs_nifree--;
1381 	fs->fs_fmod = 1;
1382 	if ((mode & IFMT) == IFDIR) {
1383 		cgp->cg_cs.cs_ndir++;
1384 		fs->fs_cstotal.cs_ndir++;
1385 		fs->fs_cs(fs, cg).cs_ndir++;
1386 	}
1387 	bdwrite(bp);
1388 	return (cg * fs->fs_ipg + ipref);
1389 }
1390 
1391 /*
1392  * Free a block or fragment.
1393  *
1394  * The specified block or fragment is placed back in the
1395  * free map. If a fragment is deallocated, a possible
1396  * block reassembly is checked.
1397  */
1398 void
ffs_blkfree(ip,bno,size)1399 ffs_blkfree(ip, bno, size)
1400 	register struct inode *ip;
1401 	daddr_t bno;
1402 	long size;
1403 {
1404 	register struct fs *fs;
1405 	register struct cg *cgp;
1406 	struct buf *bp;
1407 	daddr_t blkno;
1408 	int i, error, cg, blk, frags, bbase;
1409 
1410 	fs = ip->i_fs;
1411 	if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0 ||
1412 	    fragnum(fs, bno) + numfrags(fs, size) > fs->fs_frag) {
1413 		printf("dev = 0x%x, bsize = %d, size = %ld, fs = %s\n",
1414 		    ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt);
1415 		panic("ffs_blkfree: bad size");
1416 	}
1417 	cg = dtog(fs, bno);
1418 	if ((u_int)bno >= fs->fs_size) {
1419 		printf("bad block %d, ino %u\n", bno, ip->i_number);
1420 		ffs_fserr(fs, ip->i_ffs_uid, "bad block");
1421 		return;
1422 	}
1423 	error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
1424 		(int)fs->fs_cgsize, NOCRED, &bp);
1425 	if (error) {
1426 		brelse(bp);
1427 		return;
1428 	}
1429 	cgp = (struct cg *)bp->b_data;
1430 	if (!cg_chkmagic(cgp)) {
1431 		brelse(bp);
1432 		return;
1433 	}
1434 	cgp->cg_time = time.tv_sec;
1435 	bno = dtogd(fs, bno);
1436 	if (size == fs->fs_bsize) {
1437 		blkno = fragstoblks(fs, bno);
1438 		if (!ffs_isfreeblock(fs, cg_blksfree(cgp), blkno)) {
1439 			printf("dev = 0x%x, block = %d, fs = %s\n",
1440 			    ip->i_dev, bno, fs->fs_fsmnt);
1441 			panic("ffs_blkfree: freeing free block");
1442 		}
1443 		ffs_setblock(fs, cg_blksfree(cgp), blkno);
1444 		ffs_clusteracct(fs, cgp, blkno, 1);
1445 		cgp->cg_cs.cs_nbfree++;
1446 		fs->fs_cstotal.cs_nbfree++;
1447 		fs->fs_cs(fs, cg).cs_nbfree++;
1448 		i = cbtocylno(fs, bno);
1449 		cg_blks(fs, cgp, i)[cbtorpos(fs, bno)]++;
1450 		cg_blktot(cgp)[i]++;
1451 	} else {
1452 		bbase = bno - fragnum(fs, bno);
1453 		/*
1454 		 * decrement the counts associated with the old frags
1455 		 */
1456 		blk = blkmap(fs, cg_blksfree(cgp), bbase);
1457 		ffs_fragacct(fs, blk, cgp->cg_frsum, -1);
1458 		/*
1459 		 * deallocate the fragment
1460 		 */
1461 		frags = numfrags(fs, size);
1462 		for (i = 0; i < frags; i++) {
1463 			if (isset(cg_blksfree(cgp), bno + i)) {
1464 				printf("dev = 0x%x, block = %d, fs = %s\n",
1465 				    ip->i_dev, bno + i, fs->fs_fsmnt);
1466 				panic("ffs_blkfree: freeing free frag");
1467 			}
1468 			setbit(cg_blksfree(cgp), bno + i);
1469 		}
1470 		cgp->cg_cs.cs_nffree += i;
1471 		fs->fs_cstotal.cs_nffree += i;
1472 		fs->fs_cs(fs, cg).cs_nffree += i;
1473 		/*
1474 		 * add back in counts associated with the new frags
1475 		 */
1476 		blk = blkmap(fs, cg_blksfree(cgp), bbase);
1477 		ffs_fragacct(fs, blk, cgp->cg_frsum, 1);
1478 		/*
1479 		 * if a complete block has been reassembled, account for it
1480 		 */
1481 		blkno = fragstoblks(fs, bbase);
1482 		if (ffs_isblock(fs, cg_blksfree(cgp), blkno)) {
1483 			cgp->cg_cs.cs_nffree -= fs->fs_frag;
1484 			fs->fs_cstotal.cs_nffree -= fs->fs_frag;
1485 			fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag;
1486 			ffs_clusteracct(fs, cgp, blkno, 1);
1487 			cgp->cg_cs.cs_nbfree++;
1488 			fs->fs_cstotal.cs_nbfree++;
1489 			fs->fs_cs(fs, cg).cs_nbfree++;
1490 			i = cbtocylno(fs, bbase);
1491 			cg_blks(fs, cgp, i)[cbtorpos(fs, bbase)]++;
1492 			cg_blktot(cgp)[i]++;
1493 		}
1494 	}
1495 	fs->fs_fmod = 1;
1496 	bdwrite(bp);
1497 }
1498 
1499 int
ffs_inode_free(struct inode * pip,ino_t ino,mode_t mode)1500 ffs_inode_free(struct inode *pip, ino_t ino, mode_t mode)
1501 {
1502 	struct vnode *pvp = ITOV(pip);
1503 
1504 	if (DOINGSOFTDEP(pvp)) {
1505 		softdep_freefile(pvp, ino, mode);
1506 		return (0);
1507 	}
1508 
1509 	return (ffs_freefile(pip, ino, mode));
1510 }
1511 
1512 /*
1513  * Do the actual free operation.
1514  * The specified inode is placed back in the free map.
1515  */
1516 int
ffs_freefile(struct inode * pip,ino_t ino,mode_t mode)1517 ffs_freefile(struct inode *pip, ino_t ino, mode_t mode)
1518 {
1519 	struct fs *fs;
1520 	struct cg *cgp;
1521 	struct buf *bp;
1522 	int error, cg;
1523 
1524 	fs = pip->i_fs;
1525 	if ((u_int)ino >= fs->fs_ipg * fs->fs_ncg)
1526 		panic("ffs_freefile: range: dev = 0x%x, ino = %d, fs = %s",
1527 		    pip->i_dev, ino, fs->fs_fsmnt);
1528 	cg = ino_to_cg(fs, ino);
1529 	error = bread(pip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
1530 		(int)fs->fs_cgsize, NOCRED, &bp);
1531 	if (error) {
1532 		brelse(bp);
1533 		return (error);
1534 	}
1535 	cgp = (struct cg *)bp->b_data;
1536 	if (!cg_chkmagic(cgp)) {
1537 		brelse(bp);
1538 		return (0);
1539 	}
1540 	cgp->cg_time = time.tv_sec;
1541 	ino %= fs->fs_ipg;
1542 	if (isclr(cg_inosused(cgp), ino)) {
1543 		printf("dev = 0x%x, ino = %u, fs = %s\n",
1544 		    pip->i_dev, ino, fs->fs_fsmnt);
1545 		if (fs->fs_ronly == 0)
1546 			panic("ffs_freefile: freeing free inode");
1547 	}
1548 	clrbit(cg_inosused(cgp), ino);
1549 	if (ino < cgp->cg_irotor)
1550 		cgp->cg_irotor = ino;
1551 	cgp->cg_cs.cs_nifree++;
1552 	fs->fs_cstotal.cs_nifree++;
1553 	fs->fs_cs(fs, cg).cs_nifree++;
1554 	if ((mode & IFMT) == IFDIR) {
1555 		cgp->cg_cs.cs_ndir--;
1556 		fs->fs_cstotal.cs_ndir--;
1557 		fs->fs_cs(fs, cg).cs_ndir--;
1558 	}
1559 	fs->fs_fmod = 1;
1560 	bdwrite(bp);
1561 	return (0);
1562 }
1563 
1564 #ifdef DIAGNOSTIC
1565 /*
1566  * Verify allocation of a block or fragment. Returns true if block or
1567  * fragment is allocated, false if it is free.
1568  */
1569 static int
ffs_checkblk(ip,bno,size)1570 ffs_checkblk(ip, bno, size)
1571 	struct inode *ip;
1572 	daddr_t bno;
1573 	long size;
1574 {
1575 	struct fs *fs;
1576 	struct cg *cgp;
1577 	struct buf *bp;
1578 	int i, error, frags, free;
1579 
1580 	fs = ip->i_fs;
1581 	if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0) {
1582 		printf("bsize = %d, size = %ld, fs = %s\n",
1583 		    fs->fs_bsize, size, fs->fs_fsmnt);
1584 		panic("ffs_checkblk: bad size");
1585 	}
1586 	if ((u_int)bno >= fs->fs_size)
1587 		panic("ffs_checkblk: bad block %d", bno);
1588 	error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, dtog(fs, bno))),
1589 		(int)fs->fs_cgsize, NOCRED, &bp);
1590 	if (error) {
1591 		brelse(bp);
1592 		return (0);
1593 	}
1594 
1595 	cgp = (struct cg *)bp->b_data;
1596 	if (!cg_chkmagic(cgp)) {
1597 		brelse(bp);
1598 		return (0);
1599 	}
1600 
1601 	bno = dtogd(fs, bno);
1602 	if (size == fs->fs_bsize) {
1603 		free = ffs_isblock(fs, cg_blksfree(cgp), fragstoblks(fs, bno));
1604 	} else {
1605 		frags = numfrags(fs, size);
1606 		for (free = 0, i = 0; i < frags; i++)
1607 			if (isset(cg_blksfree(cgp), bno + i))
1608 				free++;
1609 		if (free != 0 && free != frags)
1610 			panic("ffs_checkblk: partially free fragment");
1611 	}
1612 	brelse(bp);
1613 	return (!free);
1614 }
1615 #endif /* DIAGNOSTIC */
1616 
1617 
1618 /*
1619  * Find a block of the specified size in the specified cylinder group.
1620  *
1621  * It is a panic if a request is made to find a block if none are
1622  * available.
1623  */
1624 static daddr_t
ffs_mapsearch(fs,cgp,bpref,allocsiz)1625 ffs_mapsearch(fs, cgp, bpref, allocsiz)
1626 	register struct fs *fs;
1627 	register struct cg *cgp;
1628 	daddr_t bpref;
1629 	int allocsiz;
1630 {
1631 	daddr_t bno;
1632 	int start, len, loc, i;
1633 	int blk, field, subfield, pos;
1634 
1635 	/*
1636 	 * find the fragment by searching through the free block
1637 	 * map for an appropriate bit pattern
1638 	 */
1639 	if (bpref)
1640 		start = dtogd(fs, bpref) / NBBY;
1641 	else
1642 		start = cgp->cg_frotor / NBBY;
1643 	len = howmany(fs->fs_fpg, NBBY) - start;
1644 	loc = scanc((u_int)len, (u_char *)&cg_blksfree(cgp)[start],
1645 		(u_char *)fragtbl[fs->fs_frag],
1646 		(u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
1647 	if (loc == 0) {
1648 		len = start + 1;
1649 		start = 0;
1650 		loc = scanc((u_int)len, (u_char *)&cg_blksfree(cgp)[0],
1651 			(u_char *)fragtbl[fs->fs_frag],
1652 			(u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
1653 		if (loc == 0) {
1654 			printf("start = %d, len = %d, fs = %s\n",
1655 			    start, len, fs->fs_fsmnt);
1656 			panic("ffs_alloccg: map corrupted");
1657 			/* NOTREACHED */
1658 		}
1659 	}
1660 	bno = (start + len - loc) * NBBY;
1661 	cgp->cg_frotor = bno;
1662 	/*
1663 	 * found the byte in the map
1664 	 * sift through the bits to find the selected frag
1665 	 */
1666 	for (i = bno + NBBY; bno < i; bno += fs->fs_frag) {
1667 		blk = blkmap(fs, cg_blksfree(cgp), bno);
1668 		blk <<= 1;
1669 		field = around[allocsiz];
1670 		subfield = inside[allocsiz];
1671 		for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) {
1672 			if ((blk & field) == subfield)
1673 				return (bno + pos);
1674 			field <<= 1;
1675 			subfield <<= 1;
1676 		}
1677 	}
1678 	printf("bno = %d, fs = %s\n", bno, fs->fs_fsmnt);
1679 	panic("ffs_alloccg: block not in map");
1680 	return (-1);
1681 }
1682 
1683 /*
1684  * Update the cluster map because of an allocation or free.
1685  *
1686  * Cnt == 1 means free; cnt == -1 means allocating.
1687  */
1688 void
ffs_clusteracct(fs,cgp,blkno,cnt)1689 ffs_clusteracct(fs, cgp, blkno, cnt)
1690 	struct fs *fs;
1691 	struct cg *cgp;
1692 	daddr_t blkno;
1693 	int cnt;
1694 {
1695 	int32_t *sump;
1696 	int32_t *lp;
1697 	u_char *freemapp, *mapp;
1698 	int i, start, end, forw, back, map, bit;
1699 
1700 	if (fs->fs_contigsumsize <= 0)
1701 		return;
1702 	freemapp = cg_clustersfree(cgp);
1703 	sump = cg_clustersum(cgp);
1704 	/*
1705 	 * Allocate or clear the actual block.
1706 	 */
1707 	if (cnt > 0)
1708 		setbit(freemapp, blkno);
1709 	else
1710 		clrbit(freemapp, blkno);
1711 	/*
1712 	 * Find the size of the cluster going forward.
1713 	 */
1714 	start = blkno + 1;
1715 	end = start + fs->fs_contigsumsize;
1716 	if (end >= cgp->cg_nclusterblks)
1717 		end = cgp->cg_nclusterblks;
1718 	mapp = &freemapp[start / NBBY];
1719 	map = *mapp++;
1720 	bit = 1 << (start % NBBY);
1721 	for (i = start; i < end; i++) {
1722 		if ((map & bit) == 0)
1723 			break;
1724 		if ((i & (NBBY - 1)) != (NBBY - 1)) {
1725 			bit <<= 1;
1726 		} else {
1727 			map = *mapp++;
1728 			bit = 1;
1729 		}
1730 	}
1731 	forw = i - start;
1732 	/*
1733 	 * Find the size of the cluster going backward.
1734 	 */
1735 	start = blkno - 1;
1736 	end = start - fs->fs_contigsumsize;
1737 	if (end < 0)
1738 		end = -1;
1739 	mapp = &freemapp[start / NBBY];
1740 	map = *mapp--;
1741 	bit = 1 << (start % NBBY);
1742 	for (i = start; i > end; i--) {
1743 		if ((map & bit) == 0)
1744 			break;
1745 		if ((i & (NBBY - 1)) != 0) {
1746 			bit >>= 1;
1747 		} else {
1748 			map = *mapp--;
1749 			bit = 1 << (NBBY - 1);
1750 		}
1751 	}
1752 	back = start - i;
1753 	/*
1754 	 * Account for old cluster and the possibly new forward and
1755 	 * back clusters.
1756 	 */
1757 	i = back + forw + 1;
1758 	if (i > fs->fs_contigsumsize)
1759 		i = fs->fs_contigsumsize;
1760 	sump[i] += cnt;
1761 	if (back > 0)
1762 		sump[back] -= cnt;
1763 	if (forw > 0)
1764 		sump[forw] -= cnt;
1765 	/*
1766 	 * Update cluster summary information.
1767 	 */
1768 	lp = &sump[fs->fs_contigsumsize];
1769 	for (i = fs->fs_contigsumsize; i > 0; i--)
1770 		if (*lp-- > 0)
1771 			break;
1772 	fs->fs_maxcluster[cgp->cg_cgx] = i;
1773 }
1774 
1775 /*
1776  * Fserr prints the name of a file system with an error diagnostic.
1777  *
1778  * The form of the error message is:
1779  *	fs: error message
1780  */
1781 static void
ffs_fserr(fs,uid,cp)1782 ffs_fserr(fs, uid, cp)
1783 	struct fs *fs;
1784 	u_int uid;
1785 	char *cp;
1786 {
1787 
1788 	log(LOG_ERR, "uid %u on %s: %s\n", uid, fs->fs_fsmnt, cp);
1789 }
1790