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