1 /*	$NetBSD: ffs_alloc.c,v 1.17 2006/12/18 21:03:29 christos Exp $	*/
2 /* From: NetBSD: ffs_alloc.c,v 1.50 2001/09/06 02:16:01 lukem 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.19 (Berkeley) 7/13/95
42  */
43 
44 #if HAVE_NBTOOL_CONFIG_H
45 #include "nbtool_config.h"
46 #endif
47 
48 #include <sys/cdefs.h>
49 #if defined(__RCSID) && !defined(__lint)
50 __RCSID("$NetBSD: ffs_alloc.c,v 1.17 2006/12/18 21:03:29 christos Exp $");
51 __IDSTRING(mbsdid, "$MirOS: src/usr.sbin/makefs/ffs/ffs_alloc.c,v 1.6 2010/03/07 00:11:19 tg Exp $");
52 #endif	/* !__lint */
53 
54 #include <sys/param.h>
55 #include <sys/time.h>
56 
57 #include <errno.h>
58 
59 #include "makefs.h"
60 
61 #include <ufs/ufs/dinode.h>
62 #include <ufs/ufs/ufs_bswap.h>
63 #include <ufs/ffs/fs.h>
64 
65 #include "ffs/buf.h"
66 #include "ffs/ufs_inode.h"
67 #include "ffs/ffs_extern.h"
68 
69 
70 static int scanc(u_int, const u_char *, const u_char *, int);
71 
72 static daddr_t ffs_alloccg(struct inode *, int, daddr_t, int);
73 static daddr_t ffs_alloccgblk(struct inode *, struct buf *, daddr_t);
74 static daddr_t ffs_hashalloc(struct inode *, int, daddr_t, int,
75 		     daddr_t (*)(struct inode *, int, daddr_t, int));
76 static int32_t ffs_mapsearch(struct fs *, struct cg *, daddr_t, int);
77 
78 /* in ffs_tables.c */
79 extern const int inside[], around[];
80 extern const u_char * const fragtbl[];
81 
82 /*
83  * Allocate a block in the file system.
84  *
85  * The size of the requested block is given, which must be some
86  * multiple of fs_fsize and <= fs_bsize.
87  * A preference may be optionally specified. If a preference is given
88  * the following hierarchy is used to allocate a block:
89  *   1) allocate the requested block.
90  *   2) allocate a rotationally optimal block in the same cylinder.
91  *   3) allocate a block in the same cylinder group.
92  *   4) quadradically rehash into other cylinder groups, until an
93  *      available block is located.
94  * If no block preference is given the following hierarchy is used
95  * to allocate a block:
96  *   1) allocate a block in the cylinder group that contains the
97  *      inode for the file.
98  *   2) quadradically rehash into other cylinder groups, until an
99  *      available block is located.
100  */
101 int
ffs_alloc(struct inode * ip,daddr_t lbn __unused,daddr_t bpref,int size,daddr_t * bnp)102 ffs_alloc(struct inode *ip, daddr_t lbn __unused, daddr_t bpref, int size,
103     daddr_t *bnp)
104 {
105 	struct fs *fs = ip->i_fs;
106 	daddr_t bno;
107 	int cg;
108 
109 	*bnp = 0;
110 	if (size > fs->fs_bsize || fragoff(fs, size) != 0) {
111 		errx(1, "ffs_alloc: bad size: bsize %d size %d",
112 		    fs->fs_bsize, size);
113 	}
114 	if (size == fs->fs_bsize && fs->fs_cstotal.cs_nbfree == 0)
115 		goto nospace;
116 	if (bpref >= fs->fs_size)
117 		bpref = 0;
118 	if (bpref == 0)
119 		cg = ino_to_cg(fs, ip->i_number);
120 	else
121 		cg = dtog(fs, bpref);
122 	bno = ffs_hashalloc(ip, cg, bpref, size, ffs_alloccg);
123 	if (bno > 0) {
124 		DIP_ADD(ip, blocks, size / 512);
125 		*bnp = bno;
126 		return (0);
127 	}
128 nospace:
129 	return (ENOSPC);
130 }
131 
132 /*
133  * Select the desired position for the next block in a file.  The file is
134  * logically divided into sections. The first section is composed of the
135  * direct blocks. Each additional section contains fs_maxbpg blocks.
136  *
137  * If no blocks have been allocated in the first section, the policy is to
138  * request a block in the same cylinder group as the inode that describes
139  * the file. If no blocks have been allocated in any other section, the
140  * policy is to place the section in a cylinder group with a greater than
141  * average number of free blocks.  An appropriate cylinder group is found
142  * by using a rotor that sweeps the cylinder groups. When a new group of
143  * blocks is needed, the sweep begins in the cylinder group following the
144  * cylinder group from which the previous allocation was made. The sweep
145  * continues until a cylinder group with greater than the average number
146  * of free blocks is found. If the allocation is for the first block in an
147  * indirect block, the information on the previous allocation is unavailable;
148  * here a best guess is made based upon the logical block number being
149  * allocated.
150  *
151  * If a section is already partially allocated, the policy is to
152  * contiguously allocate fs_maxcontig blocks.  The end of one of these
153  * contiguous blocks and the beginning of the next is physically separated
154  * so that the disk head will be in transit between them for at least
155  * fs_rotdelay milliseconds.  This is to allow time for the processor to
156  * schedule another I/O transfer.
157  */
158 /* XXX ondisk32 */
159 daddr_t
ffs_blkpref_ufs1(struct inode * ip,daddr_t lbn,int indx,int32_t * bap)160 ffs_blkpref_ufs1(struct inode *ip, daddr_t lbn, int indx, int32_t *bap)
161 {
162 	struct fs *fs;
163 	int cg;
164 	int avgbfree, startcg;
165 
166 	fs = ip->i_fs;
167 	if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
168 		if (lbn < NDADDR + NINDIR(fs)) {
169 			cg = ino_to_cg(fs, ip->i_number);
170 			return (fs->fs_fpg * cg + fs->fs_frag);
171 		}
172 		/*
173 		 * Find a cylinder with greater than average number of
174 		 * unused data blocks.
175 		 */
176 		if (indx == 0 || bap[indx - 1] == 0)
177 			startcg =
178 			    ino_to_cg(fs, ip->i_number) + lbn / fs->fs_maxbpg;
179 		else
180 			startcg = dtog(fs,
181 				ufs_rw32(bap[indx - 1], UFS_FSNEEDSWAP(fs)) + 1);
182 		startcg %= fs->fs_ncg;
183 		avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
184 		for (cg = startcg; cg < fs->fs_ncg; cg++)
185 			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree)
186 				return (fs->fs_fpg * cg + fs->fs_frag);
187 		for (cg = 0; cg <= startcg; cg++)
188 			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree)
189 				return (fs->fs_fpg * cg + fs->fs_frag);
190 		return (0);
191 	}
192 	/*
193 	 * We just always try to lay things out contiguously.
194 	 */
195 	return ufs_rw32(bap[indx - 1], UFS_FSNEEDSWAP(fs)) + fs->fs_frag;
196 }
197 
198 daddr_t
ffs_blkpref_ufs2(struct inode * ip,daddr_t lbn,int indx,int64_t * bap)199 ffs_blkpref_ufs2(struct inode *ip, daddr_t lbn, int indx, int64_t *bap)
200 {
201 	struct fs *fs;
202 	int cg;
203 	int avgbfree, startcg;
204 
205 	fs = ip->i_fs;
206 	if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
207 		if (lbn < NDADDR + NINDIR(fs)) {
208 			cg = ino_to_cg(fs, ip->i_number);
209 			return (fs->fs_fpg * cg + fs->fs_frag);
210 		}
211 		/*
212 		 * Find a cylinder with greater than average number of
213 		 * unused data blocks.
214 		 */
215 		if (indx == 0 || bap[indx - 1] == 0)
216 			startcg =
217 			    ino_to_cg(fs, ip->i_number) + lbn / fs->fs_maxbpg;
218 		else
219 			startcg = dtog(fs,
220 				ufs_rw64(bap[indx - 1], UFS_FSNEEDSWAP(fs)) + 1);
221 		startcg %= fs->fs_ncg;
222 		avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
223 		for (cg = startcg; cg < fs->fs_ncg; cg++)
224 			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
225 				return (fs->fs_fpg * cg + fs->fs_frag);
226 			}
227 		for (cg = 0; cg < startcg; cg++)
228 			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
229 				return (fs->fs_fpg * cg + fs->fs_frag);
230 			}
231 		return (0);
232 	}
233 	/*
234 	 * We just always try to lay things out contiguously.
235 	 */
236 	return ufs_rw64(bap[indx - 1], UFS_FSNEEDSWAP(fs)) + fs->fs_frag;
237 }
238 
239 /*
240  * Implement the cylinder overflow algorithm.
241  *
242  * The policy implemented by this algorithm is:
243  *   1) allocate the block in its requested cylinder group.
244  *   2) quadradically rehash on the cylinder group number.
245  *   3) brute force search for a free block.
246  *
247  * `size':	size for data blocks, mode for inodes
248  */
249 /*VARARGS5*/
250 static daddr_t
ffs_hashalloc(struct inode * ip,int cg,daddr_t pref,int size,daddr_t (* allocator)(struct inode *,int,daddr_t,int))251 ffs_hashalloc(struct inode *ip, int cg, daddr_t pref, int size,
252     daddr_t (*allocator)(struct inode *, int, daddr_t, int))
253 {
254 	struct fs *fs;
255 	daddr_t result;
256 	int i, icg = cg;
257 
258 	fs = ip->i_fs;
259 	/*
260 	 * 1: preferred cylinder group
261 	 */
262 	result = (*allocator)(ip, cg, pref, size);
263 	if (result)
264 		return (result);
265 	/*
266 	 * 2: quadratic rehash
267 	 */
268 	for (i = 1; i < fs->fs_ncg; i *= 2) {
269 		cg += i;
270 		if (cg >= fs->fs_ncg)
271 			cg -= fs->fs_ncg;
272 		result = (*allocator)(ip, cg, 0, size);
273 		if (result)
274 			return (result);
275 	}
276 	/*
277 	 * 3: brute force search
278 	 * Note that we start at i == 2, since 0 was checked initially,
279 	 * and 1 is always checked in the quadratic rehash.
280 	 */
281 	cg = (icg + 2) % fs->fs_ncg;
282 	for (i = 2; i < fs->fs_ncg; i++) {
283 		result = (*allocator)(ip, cg, 0, size);
284 		if (result)
285 			return (result);
286 		cg++;
287 		if (cg == fs->fs_ncg)
288 			cg = 0;
289 	}
290 	return (0);
291 }
292 
293 /*
294  * Determine whether a block can be allocated.
295  *
296  * Check to see if a block of the appropriate size is available,
297  * and if it is, allocate it.
298  */
299 static daddr_t
ffs_alloccg(struct inode * ip,int cg,daddr_t bpref,int size)300 ffs_alloccg(struct inode *ip, int cg, daddr_t bpref, int size)
301 {
302 	struct cg *cgp;
303 	struct buf *bp;
304 	daddr_t bno, blkno;
305 	int error, frags, allocsiz, i;
306 	struct fs *fs = ip->i_fs;
307 	const int needswap = UFS_FSNEEDSWAP(fs);
308 
309 	if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize)
310 		return (0);
311 	error = bread(ip->i_fd, ip->i_fs, fsbtodb(fs, cgtod(fs, cg)),
312 		(int)fs->fs_cgsize, &bp);
313 	if (error) {
314 		brelse(bp);
315 		return (0);
316 	}
317 	cgp = (struct cg *)bp->b_data;
318 	if (!cg_chkmagic(cgp, needswap) ||
319 	    (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) {
320 		brelse(bp);
321 		return (0);
322 	}
323 	if (size == fs->fs_bsize) {
324 		bno = ffs_alloccgblk(ip, bp, bpref);
325 		bdwrite(bp);
326 		return (bno);
327 	}
328 	/*
329 	 * check to see if any fragments are already available
330 	 * allocsiz is the size which will be allocated, hacking
331 	 * it down to a smaller size if necessary
332 	 */
333 	frags = numfrags(fs, size);
334 	for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++)
335 		if (cgp->cg_frsum[allocsiz] != 0)
336 			break;
337 	if (allocsiz == fs->fs_frag) {
338 		/*
339 		 * no fragments were available, so a block will be
340 		 * allocated, and hacked up
341 		 */
342 		if (cgp->cg_cs.cs_nbfree == 0) {
343 			brelse(bp);
344 			return (0);
345 		}
346 		bno = ffs_alloccgblk(ip, bp, bpref);
347 		bpref = dtogd(fs, bno);
348 		for (i = frags; i < fs->fs_frag; i++)
349 			setbit(cg_blksfree(cgp, needswap), bpref + i);
350 		i = fs->fs_frag - frags;
351 		ufs_add32(cgp->cg_cs.cs_nffree, i, needswap);
352 		fs->fs_cstotal.cs_nffree += i;
353 		fs->fs_cs(fs, cg).cs_nffree += i;
354 		fs->fs_fmod = 1;
355 		ufs_add32(cgp->cg_frsum[i], 1, needswap);
356 		bdwrite(bp);
357 		return (bno);
358 	}
359 	bno = ffs_mapsearch(fs, cgp, bpref, allocsiz);
360 	for (i = 0; i < frags; i++)
361 		clrbit(cg_blksfree(cgp, needswap), bno + i);
362 	ufs_add32(cgp->cg_cs.cs_nffree, -frags, needswap);
363 	fs->fs_cstotal.cs_nffree -= frags;
364 	fs->fs_cs(fs, cg).cs_nffree -= frags;
365 	fs->fs_fmod = 1;
366 	ufs_add32(cgp->cg_frsum[allocsiz], -1, needswap);
367 	if (frags != allocsiz)
368 		ufs_add32(cgp->cg_frsum[allocsiz - frags], 1, needswap);
369 	blkno = cg * fs->fs_fpg + bno;
370 	bdwrite(bp);
371 	return blkno;
372 }
373 
374 /*
375  * Allocate a block in a cylinder group.
376  *
377  * This algorithm implements the following policy:
378  *   1) allocate the requested block.
379  *   2) allocate a rotationally optimal block in the same cylinder.
380  *   3) allocate the next available block on the block rotor for the
381  *      specified cylinder group.
382  * Note that this routine only allocates fs_bsize blocks; these
383  * blocks may be fragmented by the routine that allocates them.
384  */
385 static daddr_t
ffs_alloccgblk(struct inode * ip,struct buf * bp,daddr_t bpref)386 ffs_alloccgblk(struct inode *ip, struct buf *bp, daddr_t bpref)
387 {
388 	struct cg *cgp;
389 	daddr_t blkno;
390 	int32_t bno;
391 	struct fs *fs = ip->i_fs;
392 	const int needswap = UFS_FSNEEDSWAP(fs);
393 	u_int8_t *blksfree;
394 
395 	cgp = (struct cg *)bp->b_data;
396 	blksfree = cg_blksfree(cgp, needswap);
397 	if (bpref == 0 || (uint32_t)dtog(fs, bpref) != ufs_rw32(cgp->cg_cgx, needswap)) {
398 		bpref = ufs_rw32(cgp->cg_rotor, needswap);
399 	} else {
400 		bpref = blknum(fs, bpref);
401 		bno = dtogd(fs, bpref);
402 		/*
403 		 * if the requested block is available, use it
404 		 */
405 		if (ffs_isblock(fs, blksfree, fragstoblks(fs, bno)))
406 			goto gotit;
407 	}
408 	/*
409 	 * Take the next available one in this cylinder group.
410 	 */
411 	bno = ffs_mapsearch(fs, cgp, bpref, (int)fs->fs_frag);
412 	if (bno < 0)
413 		return (0);
414 	cgp->cg_rotor = ufs_rw32(bno, needswap);
415 gotit:
416 	blkno = fragstoblks(fs, bno);
417 	ffs_clrblock(fs, blksfree, (long)blkno);
418 	ffs_clusteracct(fs, cgp, blkno, -1);
419 	ufs_add32(cgp->cg_cs.cs_nbfree, -1, needswap);
420 	fs->fs_cstotal.cs_nbfree--;
421 	fs->fs_cs(fs, ufs_rw32(cgp->cg_cgx, needswap)).cs_nbfree--;
422 	fs->fs_fmod = 1;
423 	blkno = ufs_rw32(cgp->cg_cgx, needswap) * fs->fs_fpg + bno;
424 	return (blkno);
425 }
426 
427 /*
428  * Free a block or fragment.
429  *
430  * The specified block or fragment is placed back in the
431  * free map. If a fragment is deallocated, a possible
432  * block reassembly is checked.
433  */
434 void
ffs_blkfree(struct inode * ip,daddr_t bno,long size)435 ffs_blkfree(struct inode *ip, daddr_t bno, long size)
436 {
437 	struct cg *cgp;
438 	struct buf *bp;
439 	int32_t fragno, cgbno;
440 	int i, error, cg, blk, frags, bbase;
441 	struct fs *fs = ip->i_fs;
442 	const int needswap = UFS_FSNEEDSWAP(fs);
443 
444 	if (size > fs->fs_bsize || fragoff(fs, size) != 0 ||
445 	    fragnum(fs, bno) + numfrags(fs, size) > fs->fs_frag) {
446 		errx(1, "blkfree: bad size: bno %lld bsize %d size %ld",
447 		    (long long)bno, fs->fs_bsize, size);
448 	}
449 	cg = dtog(fs, bno);
450 	if (bno >= fs->fs_size) {
451 		warnx("bad block %lld, ino %llu", (long long)bno,
452 		    (unsigned long long)ip->i_number);
453 		return;
454 	}
455 	error = bread(ip->i_fd, ip->i_fs, fsbtodb(fs, cgtod(fs, cg)),
456 		(int)fs->fs_cgsize, &bp);
457 	if (error) {
458 		brelse(bp);
459 		return;
460 	}
461 	cgp = (struct cg *)bp->b_data;
462 	if (!cg_chkmagic(cgp, needswap)) {
463 		brelse(bp);
464 		return;
465 	}
466 	cgbno = dtogd(fs, bno);
467 	if (size == fs->fs_bsize) {
468 		fragno = fragstoblks(fs, cgbno);
469 		if (!ffs_isfreeblock(fs, cg_blksfree(cgp, needswap), fragno)) {
470 			errx(1, "blkfree: freeing free block %lld",
471 			    (long long)bno);
472 		}
473 		ffs_setblock(fs, cg_blksfree(cgp, needswap), fragno);
474 		ffs_clusteracct(fs, cgp, fragno, 1);
475 		ufs_add32(cgp->cg_cs.cs_nbfree, 1, needswap);
476 		fs->fs_cstotal.cs_nbfree++;
477 		fs->fs_cs(fs, cg).cs_nbfree++;
478 	} else {
479 		bbase = cgbno - fragnum(fs, cgbno);
480 		/*
481 		 * decrement the counts associated with the old frags
482 		 */
483 		blk = blkmap(fs, cg_blksfree(cgp, needswap), bbase);
484 		ffs_fragacct(fs, blk, cgp->cg_frsum, -1, needswap);
485 		/*
486 		 * deallocate the fragment
487 		 */
488 		frags = numfrags(fs, size);
489 		for (i = 0; i < frags; i++) {
490 			if (isset(cg_blksfree(cgp, needswap), cgbno + i)) {
491 				errx(1, "blkfree: freeing free frag: block %lld",
492 				    (long long)(cgbno + i));
493 			}
494 			setbit(cg_blksfree(cgp, needswap), cgbno + i);
495 		}
496 		ufs_add32(cgp->cg_cs.cs_nffree, i, needswap);
497 		fs->fs_cstotal.cs_nffree += i;
498 		fs->fs_cs(fs, cg).cs_nffree += i;
499 		/*
500 		 * add back in counts associated with the new frags
501 		 */
502 		blk = blkmap(fs, cg_blksfree(cgp, needswap), bbase);
503 		ffs_fragacct(fs, blk, cgp->cg_frsum, 1, needswap);
504 		/*
505 		 * if a complete block has been reassembled, account for it
506 		 */
507 		fragno = fragstoblks(fs, bbase);
508 		if (ffs_isblock(fs, cg_blksfree(cgp, needswap), fragno)) {
509 			ufs_add32(cgp->cg_cs.cs_nffree, -fs->fs_frag, needswap);
510 			fs->fs_cstotal.cs_nffree -= fs->fs_frag;
511 			fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag;
512 			ffs_clusteracct(fs, cgp, fragno, 1);
513 			ufs_add32(cgp->cg_cs.cs_nbfree, 1, needswap);
514 			fs->fs_cstotal.cs_nbfree++;
515 			fs->fs_cs(fs, cg).cs_nbfree++;
516 		}
517 	}
518 	fs->fs_fmod = 1;
519 	bdwrite(bp);
520 }
521 
522 
523 static int
scanc(u_int size,const u_char * cp,const u_char table[],int mask)524 scanc(u_int size, const u_char *cp, const u_char table[], int mask)
525 {
526 	const u_char *end = &cp[size];
527 
528 	while (cp < end && (table[*cp] & mask) == 0)
529 		cp++;
530 	return (end - cp);
531 }
532 
533 /*
534  * Find a block of the specified size in the specified cylinder group.
535  *
536  * It is a panic if a request is made to find a block if none are
537  * available.
538  */
539 static int32_t
ffs_mapsearch(struct fs * fs,struct cg * cgp,daddr_t bpref,int allocsiz)540 ffs_mapsearch(struct fs *fs, struct cg *cgp, daddr_t bpref, int allocsiz)
541 {
542 	int32_t bno;
543 	int start, len, loc, i;
544 	int blk, field, subfield, pos;
545 	int ostart, olen;
546 	const int needswap = UFS_FSNEEDSWAP(fs);
547 
548 	/*
549 	 * find the fragment by searching through the free block
550 	 * map for an appropriate bit pattern
551 	 */
552 	if (bpref)
553 		start = dtogd(fs, bpref) / NBBY;
554 	else
555 		start = ufs_rw32(cgp->cg_frotor, needswap) / NBBY;
556 	len = howmany(fs->fs_fpg, NBBY) - start;
557 	ostart = start;
558 	olen = len;
559 	loc = scanc((u_int)len,
560 		(const u_char *)&cg_blksfree(cgp, needswap)[start],
561 		(const u_char *)fragtbl[fs->fs_frag],
562 		(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
563 	if (loc == 0) {
564 		len = start + 1;
565 		start = 0;
566 		loc = scanc((u_int)len,
567 			(const u_char *)&cg_blksfree(cgp, needswap)[0],
568 			(const u_char *)fragtbl[fs->fs_frag],
569 			(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
570 		if (loc == 0) {
571 			errx(1,
572     "ffs_alloccg: map corrupted: start %d len %d offset %d %ld",
573 				ostart, olen,
574 				ufs_rw32(cgp->cg_freeoff, needswap),
575 				(long)cg_blksfree(cgp, needswap) - (long)cgp);
576 			/* NOTREACHED */
577 		}
578 	}
579 	bno = (start + len - loc) * NBBY;
580 	cgp->cg_frotor = ufs_rw32(bno, needswap);
581 	/*
582 	 * found the byte in the map
583 	 * sift through the bits to find the selected frag
584 	 */
585 	for (i = bno + NBBY; bno < i; bno += fs->fs_frag) {
586 		blk = blkmap(fs, cg_blksfree(cgp, needswap), bno);
587 		blk <<= 1;
588 		field = around[allocsiz];
589 		subfield = inside[allocsiz];
590 		for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) {
591 			if ((blk & field) == subfield)
592 				return (bno + pos);
593 			field <<= 1;
594 			subfield <<= 1;
595 		}
596 	}
597 	errx(1, "ffs_alloccg: block not in map: bno %lld", (long long)bno);
598 	return (-1);
599 }
600 
601 /*
602  * Update the cluster map because of an allocation or free.
603  *
604  * Cnt == 1 means free; cnt == -1 means allocating.
605  */
606 void
ffs_clusteracct(struct fs * fs,struct cg * cgp,int32_t blkno,int cnt)607 ffs_clusteracct(struct fs *fs, struct cg *cgp, int32_t blkno, int cnt)
608 {
609 	int32_t *sump;
610 	int32_t *lp;
611 	u_char *freemapp, *mapp;
612 	int i, start, end, forw, back, map, bit;
613 	const int needswap = UFS_FSNEEDSWAP(fs);
614 
615 	if (fs->fs_contigsumsize <= 0)
616 		return;
617 	freemapp = cg_clustersfree(cgp, needswap);
618 	sump = cg_clustersum(cgp, needswap);
619 	/*
620 	 * Allocate or clear the actual block.
621 	 */
622 	if (cnt > 0)
623 		setbit(freemapp, blkno);
624 	else
625 		clrbit(freemapp, blkno);
626 	/*
627 	 * Find the size of the cluster going forward.
628 	 */
629 	start = blkno + 1;
630 	end = start + fs->fs_contigsumsize;
631 	if ((unsigned)end >= ufs_rw32(cgp->cg_nclusterblks, needswap))
632 		end = ufs_rw32(cgp->cg_nclusterblks, needswap);
633 	mapp = &freemapp[start / NBBY];
634 	map = *mapp++;
635 	bit = 1 << (start % NBBY);
636 	for (i = start; i < end; i++) {
637 		if ((map & bit) == 0)
638 			break;
639 		if ((i & (NBBY - 1)) != (NBBY - 1)) {
640 			bit <<= 1;
641 		} else {
642 			map = *mapp++;
643 			bit = 1;
644 		}
645 	}
646 	forw = i - start;
647 	/*
648 	 * Find the size of the cluster going backward.
649 	 */
650 	start = blkno - 1;
651 	end = start - fs->fs_contigsumsize;
652 	if (end < 0)
653 		end = -1;
654 	mapp = &freemapp[start / NBBY];
655 	map = *mapp--;
656 	bit = 1 << (start % NBBY);
657 	for (i = start; i > end; i--) {
658 		if ((map & bit) == 0)
659 			break;
660 		if ((i & (NBBY - 1)) != 0) {
661 			bit >>= 1;
662 		} else {
663 			map = *mapp--;
664 			bit = 1 << (NBBY - 1);
665 		}
666 	}
667 	back = start - i;
668 	/*
669 	 * Account for old cluster and the possibly new forward and
670 	 * back clusters.
671 	 */
672 	i = back + forw + 1;
673 	if (i > fs->fs_contigsumsize)
674 		i = fs->fs_contigsumsize;
675 	ufs_add32(sump[i], cnt, needswap);
676 	if (back > 0)
677 		ufs_add32(sump[back], -cnt, needswap);
678 	if (forw > 0)
679 		ufs_add32(sump[forw], -cnt, needswap);
680 
681 	/*
682 	 * Update cluster summary information.
683 	 */
684 	lp = &sump[fs->fs_contigsumsize];
685 	for (i = fs->fs_contigsumsize; i > 0; i--)
686 		if (ufs_rw32(*lp--, needswap) > 0)
687 			break;
688 	fs->fs_maxcluster[ufs_rw32(cgp->cg_cgx, needswap)] = i;
689 }
690