ffs_alloc.c revision 1.1.1.1 1 /* $NetBSD: ffs_alloc.c,v 1.1.1.1 2001/10/26 06:21:35 lukem Exp $ */
2 /* From: NetBSD: ffs_alloc.c,v 1.50 2001/09/06 02:16:01 lukem 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. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * @(#)ffs_alloc.c 8.19 (Berkeley) 7/13/95
37 */
38
39 #include <sys/param.h>
40 #include <sys/time.h>
41
42 #include <err.h>
43 #include <errno.h>
44
45 #include <ufs/ufs/ufs_bswap.h>
46 #include <ufs/ufs/inode.h>
47 #include <ufs/ffs/fs.h>
48
49 #include "ffs/buf.h"
50 #include "ffs/ffs_extern.h"
51
52
53 static int scanc(u_int, const u_char *, const u_char *, int);
54
55 static ufs_daddr_t ffs_alloccg(struct inode *, int, ufs_daddr_t, int);
56 static ufs_daddr_t ffs_alloccgblk(struct inode *, struct buf *, ufs_daddr_t);
57 static u_long ffs_hashalloc(struct inode *, int, long, int,
58 ufs_daddr_t (*)(struct inode *, int, ufs_daddr_t, int));
59 static ufs_daddr_t ffs_mapsearch(struct fs *, struct cg *, ufs_daddr_t, int);
60
61 /* in ffs_tables.c */
62 extern const int inside[], around[];
63 extern const u_char * const fragtbl[];
64
65 /*
66 * Allocate a block in the file system.
67 *
68 * The size of the requested block is given, which must be some
69 * multiple of fs_fsize and <= fs_bsize.
70 * A preference may be optionally specified. If a preference is given
71 * the following hierarchy is used to allocate a block:
72 * 1) allocate the requested block.
73 * 2) allocate a rotationally optimal block in the same cylinder.
74 * 3) allocate a block in the same cylinder group.
75 * 4) quadradically rehash into other cylinder groups, until an
76 * available block is located.
77 * If no block preference is given the following hierarchy is used
78 * to allocate a block:
79 * 1) allocate a block in the cylinder group that contains the
80 * inode for the file.
81 * 2) quadradically rehash into other cylinder groups, until an
82 * available block is located.
83 */
84 int
85 ffs_alloc(struct inode *ip, ufs_daddr_t lbn, ufs_daddr_t bpref, int size,
86 ufs_daddr_t *bnp)
87 {
88 struct fs *fs = ip->i_fs;
89 ufs_daddr_t bno;
90 int cg;
91
92 *bnp = 0;
93 if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0) {
94 errx(1, "ffs_alloc: bad size: bsize %d size %d",
95 fs->fs_bsize, size);
96 }
97 if (size == fs->fs_bsize && fs->fs_cstotal.cs_nbfree == 0)
98 goto nospace;
99 if (bpref >= fs->fs_size)
100 bpref = 0;
101 if (bpref == 0)
102 cg = ino_to_cg(fs, ip->i_number);
103 else
104 cg = dtog(fs, bpref);
105 bno = (ufs_daddr_t)ffs_hashalloc(ip, cg, (long)bpref, size,
106 ffs_alloccg);
107 if (bno > 0) {
108 ip->i_ffs_blocks += btodb(size);
109 *bnp = bno;
110 return (0);
111 }
112 nospace:
113 return (ENOSPC);
114 }
115
116 /*
117 * Select the desired position for the next block in a file. The file is
118 * logically divided into sections. The first section is composed of the
119 * direct blocks. Each additional section contains fs_maxbpg blocks.
120 *
121 * If no blocks have been allocated in the first section, the policy is to
122 * request a block in the same cylinder group as the inode that describes
123 * the file. If no blocks have been allocated in any other section, the
124 * policy is to place the section in a cylinder group with a greater than
125 * average number of free blocks. An appropriate cylinder group is found
126 * by using a rotor that sweeps the cylinder groups. When a new group of
127 * blocks is needed, the sweep begins in the cylinder group following the
128 * cylinder group from which the previous allocation was made. The sweep
129 * continues until a cylinder group with greater than the average number
130 * of free blocks is found. If the allocation is for the first block in an
131 * indirect block, the information on the previous allocation is unavailable;
132 * here a best guess is made based upon the logical block number being
133 * allocated.
134 *
135 * If a section is already partially allocated, the policy is to
136 * contiguously allocate fs_maxcontig blocks. The end of one of these
137 * contiguous blocks and the beginning of the next is physically separated
138 * so that the disk head will be in transit between them for at least
139 * fs_rotdelay milliseconds. This is to allow time for the processor to
140 * schedule another I/O transfer.
141 */
142 ufs_daddr_t
143 ffs_blkpref(struct inode *ip, ufs_daddr_t lbn, int indx, ufs_daddr_t *bap)
144 {
145 struct fs *fs;
146 int cg;
147 int avgbfree, startcg;
148 ufs_daddr_t nextblk;
149
150 fs = ip->i_fs;
151 if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
152 if (lbn < NDADDR + NINDIR(fs)) {
153 cg = ino_to_cg(fs, ip->i_number);
154 return (fs->fs_fpg * cg + fs->fs_frag);
155 }
156 /*
157 * Find a cylinder with greater than average number of
158 * unused data blocks.
159 */
160 if (indx == 0 || bap[indx - 1] == 0)
161 startcg =
162 ino_to_cg(fs, ip->i_number) + lbn / fs->fs_maxbpg;
163 else
164 startcg = dtog(fs,
165 ufs_rw32(bap[indx - 1], UFS_FSNEEDSWAP(fs)) + 1);
166 startcg %= fs->fs_ncg;
167 avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
168 for (cg = startcg; cg < fs->fs_ncg; cg++)
169 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
170 fs->fs_cgrotor = cg;
171 return (fs->fs_fpg * cg + fs->fs_frag);
172 }
173 for (cg = 0; cg <= startcg; cg++)
174 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
175 fs->fs_cgrotor = cg;
176 return (fs->fs_fpg * cg + fs->fs_frag);
177 }
178 return (0);
179 }
180 /*
181 * One or more previous blocks have been laid out. If less
182 * than fs_maxcontig previous blocks are contiguous, the
183 * next block is requested contiguously, otherwise it is
184 * requested rotationally delayed by fs_rotdelay milliseconds.
185 */
186 nextblk = ufs_rw32(bap[indx - 1], UFS_FSNEEDSWAP(fs)) + fs->fs_frag;
187 if (indx < fs->fs_maxcontig ||
188 ufs_rw32(bap[indx - fs->fs_maxcontig], UFS_FSNEEDSWAP(fs)) +
189 blkstofrags(fs, fs->fs_maxcontig) != nextblk)
190 return (nextblk);
191 if (fs->fs_rotdelay != 0)
192 /*
193 * Here we convert ms of delay to frags as:
194 * (frags) = (ms) * (rev/sec) * (sect/rev) /
195 * ((sect/frag) * (ms/sec))
196 * then round up to the next block.
197 */
198 nextblk += roundup(fs->fs_rotdelay * fs->fs_rps * fs->fs_nsect /
199 (NSPF(fs) * 1000), fs->fs_frag);
200 return (nextblk);
201 }
202
203 /*
204 * Implement the cylinder overflow algorithm.
205 *
206 * The policy implemented by this algorithm is:
207 * 1) allocate the block in its requested cylinder group.
208 * 2) quadradically rehash on the cylinder group number.
209 * 3) brute force search for a free block.
210 *
211 * `size': size for data blocks, mode for inodes
212 */
213 /*VARARGS5*/
214 static u_long
215 ffs_hashalloc(struct inode *ip, int cg, long pref, int size,
216 ufs_daddr_t (*allocator)(struct inode *, int, ufs_daddr_t, int))
217 {
218 struct fs *fs;
219 long result;
220 int i, icg = cg;
221
222 fs = ip->i_fs;
223 /*
224 * 1: preferred cylinder group
225 */
226 result = (*allocator)(ip, cg, pref, size);
227 if (result)
228 return (result);
229 /*
230 * 2: quadratic rehash
231 */
232 for (i = 1; i < fs->fs_ncg; i *= 2) {
233 cg += i;
234 if (cg >= fs->fs_ncg)
235 cg -= fs->fs_ncg;
236 result = (*allocator)(ip, cg, 0, size);
237 if (result)
238 return (result);
239 }
240 /*
241 * 3: brute force search
242 * Note that we start at i == 2, since 0 was checked initially,
243 * and 1 is always checked in the quadratic rehash.
244 */
245 cg = (icg + 2) % fs->fs_ncg;
246 for (i = 2; i < fs->fs_ncg; i++) {
247 result = (*allocator)(ip, cg, 0, size);
248 if (result)
249 return (result);
250 cg++;
251 if (cg == fs->fs_ncg)
252 cg = 0;
253 }
254 return (0);
255 }
256
257 /*
258 * Determine whether a block can be allocated.
259 *
260 * Check to see if a block of the appropriate size is available,
261 * and if it is, allocate it.
262 */
263 static ufs_daddr_t
264 ffs_alloccg(struct inode *ip, int cg, ufs_daddr_t bpref, int size)
265 {
266 struct cg *cgp;
267 struct buf *bp;
268 ufs_daddr_t bno, blkno;
269 int error, frags, allocsiz, i;
270 struct fs *fs = ip->i_fs;
271 const int needswap = UFS_FSNEEDSWAP(fs);
272
273 if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize)
274 return (0);
275 error = bread(ip->i_fd, ip->i_fs, fsbtodb(fs, cgtod(fs, cg)),
276 (int)fs->fs_cgsize, &bp);
277 if (error) {
278 brelse(bp);
279 return (0);
280 }
281 cgp = (struct cg *)bp->b_data;
282 if (!cg_chkmagic(cgp, needswap) ||
283 (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) {
284 brelse(bp);
285 return (0);
286 }
287 if (size == fs->fs_bsize) {
288 bno = ffs_alloccgblk(ip, bp, bpref);
289 bdwrite(bp);
290 return (bno);
291 }
292 /*
293 * check to see if any fragments are already available
294 * allocsiz is the size which will be allocated, hacking
295 * it down to a smaller size if necessary
296 */
297 frags = numfrags(fs, size);
298 for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++)
299 if (cgp->cg_frsum[allocsiz] != 0)
300 break;
301 if (allocsiz == fs->fs_frag) {
302 /*
303 * no fragments were available, so a block will be
304 * allocated, and hacked up
305 */
306 if (cgp->cg_cs.cs_nbfree == 0) {
307 brelse(bp);
308 return (0);
309 }
310 bno = ffs_alloccgblk(ip, bp, bpref);
311 bpref = dtogd(fs, bno);
312 for (i = frags; i < fs->fs_frag; i++)
313 setbit(cg_blksfree(cgp, needswap), bpref + i);
314 i = fs->fs_frag - frags;
315 ufs_add32(cgp->cg_cs.cs_nffree, i, needswap);
316 fs->fs_cstotal.cs_nffree += i;
317 fs->fs_cs(fs, cg).cs_nffree += i;
318 fs->fs_fmod = 1;
319 ufs_add32(cgp->cg_frsum[i], 1, needswap);
320 bdwrite(bp);
321 return (bno);
322 }
323 bno = ffs_mapsearch(fs, cgp, bpref, allocsiz);
324 for (i = 0; i < frags; i++)
325 clrbit(cg_blksfree(cgp, needswap), bno + i);
326 ufs_add32(cgp->cg_cs.cs_nffree, -frags, needswap);
327 fs->fs_cstotal.cs_nffree -= frags;
328 fs->fs_cs(fs, cg).cs_nffree -= frags;
329 fs->fs_fmod = 1;
330 ufs_add32(cgp->cg_frsum[allocsiz], -1, needswap);
331 if (frags != allocsiz)
332 ufs_add32(cgp->cg_frsum[allocsiz - frags], 1, needswap);
333 blkno = cg * fs->fs_fpg + bno;
334 bdwrite(bp);
335 return blkno;
336 }
337
338 /*
339 * Allocate a block in a cylinder group.
340 *
341 * This algorithm implements the following policy:
342 * 1) allocate the requested block.
343 * 2) allocate a rotationally optimal block in the same cylinder.
344 * 3) allocate the next available block on the block rotor for the
345 * specified cylinder group.
346 * Note that this routine only allocates fs_bsize blocks; these
347 * blocks may be fragmented by the routine that allocates them.
348 */
349 static ufs_daddr_t
350 ffs_alloccgblk(struct inode *ip, struct buf *bp, ufs_daddr_t bpref)
351 {
352 struct cg *cgp;
353 ufs_daddr_t bno, blkno;
354 int cylno, pos, delta;
355 short *cylbp;
356 int i;
357 struct fs *fs = ip->i_fs;
358 const int needswap = UFS_FSNEEDSWAP(fs);
359
360 cgp = (struct cg *)bp->b_data;
361 if (bpref == 0 || dtog(fs, bpref) != ufs_rw32(cgp->cg_cgx, needswap)) {
362 bpref = ufs_rw32(cgp->cg_rotor, needswap);
363 goto norot;
364 }
365 bpref = blknum(fs, bpref);
366 bpref = dtogd(fs, bpref);
367 /*
368 * if the requested block is available, use it
369 */
370 if (ffs_isblock(fs, cg_blksfree(cgp, needswap),
371 fragstoblks(fs, bpref))) {
372 bno = bpref;
373 goto gotit;
374 }
375 if (fs->fs_nrpos <= 1 || fs->fs_cpc == 0) {
376 /*
377 * Block layout information is not available.
378 * Leaving bpref unchanged means we take the
379 * next available free block following the one
380 * we just allocated. Hopefully this will at
381 * least hit a track cache on drives of unknown
382 * geometry (e.g. SCSI).
383 */
384 goto norot;
385 }
386 /*
387 * check for a block available on the same cylinder
388 */
389 cylno = cbtocylno(fs, bpref);
390 if (cg_blktot(cgp, needswap)[cylno] == 0)
391 goto norot;
392 /*
393 * check the summary information to see if a block is
394 * available in the requested cylinder starting at the
395 * requested rotational position and proceeding around.
396 */
397 cylbp = cg_blks(fs, cgp, cylno, needswap);
398 pos = cbtorpos(fs, bpref);
399 for (i = pos; i < fs->fs_nrpos; i++)
400 if (ufs_rw16(cylbp[i], needswap) > 0)
401 break;
402 if (i == fs->fs_nrpos)
403 for (i = 0; i < pos; i++)
404 if (ufs_rw16(cylbp[i], needswap) > 0)
405 break;
406 if (ufs_rw16(cylbp[i], needswap) > 0) {
407 /*
408 * found a rotational position, now find the actual
409 * block. A panic if none is actually there.
410 */
411 pos = cylno % fs->fs_cpc;
412 bno = (cylno - pos) * fs->fs_spc / NSPB(fs);
413 if (fs_postbl(fs, pos)[i] == -1) {
414 errx(1,
415 "ffs_alloccgblk: cyl groups corrupted: pos %d i %d",
416 pos, i);
417 }
418 for (i = fs_postbl(fs, pos)[i];; ) {
419 if (ffs_isblock(fs, cg_blksfree(cgp, needswap), bno + i)) {
420 bno = blkstofrags(fs, (bno + i));
421 goto gotit;
422 }
423 delta = fs_rotbl(fs)[i];
424 if (delta <= 0 ||
425 delta + i > fragstoblks(fs, fs->fs_fpg))
426 break;
427 i += delta;
428 }
429 errx(1, "ffs_alloccgblk: can't find blk in cyl: pos %d i %d",
430 pos, i);
431 }
432 norot:
433 /*
434 * no blocks in the requested cylinder, so take next
435 * available one in this cylinder group.
436 */
437 bno = ffs_mapsearch(fs, cgp, bpref, (int)fs->fs_frag);
438 if (bno < 0)
439 return (0);
440 cgp->cg_rotor = ufs_rw32(bno, needswap);
441 gotit:
442 blkno = fragstoblks(fs, bno);
443 ffs_clrblock(fs, cg_blksfree(cgp, needswap), (long)blkno);
444 ffs_clusteracct(fs, cgp, blkno, -1);
445 ufs_add32(cgp->cg_cs.cs_nbfree, -1, needswap);
446 fs->fs_cstotal.cs_nbfree--;
447 fs->fs_cs(fs, ufs_rw32(cgp->cg_cgx, needswap)).cs_nbfree--;
448 cylno = cbtocylno(fs, bno);
449 ufs_add16(cg_blks(fs, cgp, cylno, needswap)[cbtorpos(fs, bno)], -1,
450 needswap);
451 ufs_add32(cg_blktot(cgp, needswap)[cylno], -1, needswap);
452 fs->fs_fmod = 1;
453 blkno = ufs_rw32(cgp->cg_cgx, needswap) * fs->fs_fpg + bno;
454 return (blkno);
455 }
456
457 /*
458 * Free a block or fragment.
459 *
460 * The specified block or fragment is placed back in the
461 * free map. If a fragment is deallocated, a possible
462 * block reassembly is checked.
463 */
464 void
465 ffs_blkfree(struct inode *ip, ufs_daddr_t bno, long size)
466 {
467 struct cg *cgp;
468 struct buf *bp;
469 ufs_daddr_t blkno;
470 int i, error, cg, blk, frags, bbase;
471 struct fs *fs = ip->i_fs;
472 const int needswap = UFS_FSNEEDSWAP(fs);
473
474 if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0 ||
475 fragnum(fs, bno) + numfrags(fs, size) > fs->fs_frag) {
476 errx(1, "blkfree: bad size: bno %u bsize %d size %ld",
477 bno, fs->fs_bsize, size);
478 }
479 cg = dtog(fs, bno);
480 if ((u_int)bno >= fs->fs_size) {
481 warnx("bad block %d, ino %d\n", bno, ip->i_number);
482 return;
483 }
484 error = bread(ip->i_fd, ip->i_fs, fsbtodb(fs, cgtod(fs, cg)),
485 (int)fs->fs_cgsize, &bp);
486 if (error) {
487 brelse(bp);
488 return;
489 }
490 cgp = (struct cg *)bp->b_data;
491 if (!cg_chkmagic(cgp, needswap)) {
492 brelse(bp);
493 return;
494 }
495 bno = dtogd(fs, bno);
496 if (size == fs->fs_bsize) {
497 blkno = fragstoblks(fs, bno);
498 if (!ffs_isfreeblock(fs, cg_blksfree(cgp, needswap), blkno)) {
499 errx(1, "blkfree: freeing free block %d", bno);
500 }
501 ffs_setblock(fs, cg_blksfree(cgp, needswap), blkno);
502 ffs_clusteracct(fs, cgp, blkno, 1);
503 ufs_add32(cgp->cg_cs.cs_nbfree, 1, needswap);
504 fs->fs_cstotal.cs_nbfree++;
505 fs->fs_cs(fs, cg).cs_nbfree++;
506 i = cbtocylno(fs, bno);
507 ufs_add16(cg_blks(fs, cgp, i, needswap)[cbtorpos(fs, bno)], 1,
508 needswap);
509 ufs_add32(cg_blktot(cgp, needswap)[i], 1, needswap);
510 } else {
511 bbase = bno - fragnum(fs, bno);
512 /*
513 * decrement the counts associated with the old frags
514 */
515 blk = blkmap(fs, cg_blksfree(cgp, needswap), bbase);
516 ffs_fragacct(fs, blk, cgp->cg_frsum, -1, needswap);
517 /*
518 * deallocate the fragment
519 */
520 frags = numfrags(fs, size);
521 for (i = 0; i < frags; i++) {
522 if (isset(cg_blksfree(cgp, needswap), bno + i)) {
523 errx(1, "blkfree: freeing free frag: block %d",
524 bno + i);
525 }
526 setbit(cg_blksfree(cgp, needswap), bno + i);
527 }
528 ufs_add32(cgp->cg_cs.cs_nffree, i, needswap);
529 fs->fs_cstotal.cs_nffree += i;
530 fs->fs_cs(fs, cg).cs_nffree += i;
531 /*
532 * add back in counts associated with the new frags
533 */
534 blk = blkmap(fs, cg_blksfree(cgp, needswap), bbase);
535 ffs_fragacct(fs, blk, cgp->cg_frsum, 1, needswap);
536 /*
537 * if a complete block has been reassembled, account for it
538 */
539 blkno = fragstoblks(fs, bbase);
540 if (ffs_isblock(fs, cg_blksfree(cgp, needswap), blkno)) {
541 ufs_add32(cgp->cg_cs.cs_nffree, -fs->fs_frag, needswap);
542 fs->fs_cstotal.cs_nffree -= fs->fs_frag;
543 fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag;
544 ffs_clusteracct(fs, cgp, blkno, 1);
545 ufs_add32(cgp->cg_cs.cs_nbfree, 1, needswap);
546 fs->fs_cstotal.cs_nbfree++;
547 fs->fs_cs(fs, cg).cs_nbfree++;
548 i = cbtocylno(fs, bbase);
549 ufs_add16(cg_blks(fs, cgp, i, needswap)[cbtorpos(fs,
550 bbase)], 1,
551 needswap);
552 ufs_add32(cg_blktot(cgp, needswap)[i], 1, needswap);
553 }
554 }
555 fs->fs_fmod = 1;
556 bdwrite(bp);
557 }
558
559
560 static int
561 scanc(u_int size, const u_char *cp, const u_char table[], int mask)
562 {
563 const u_char *end = &cp[size];
564
565 while (cp < end && (table[*cp] & mask) == 0)
566 cp++;
567 return (end - cp);
568 }
569
570 /*
571 * Find a block of the specified size in the specified cylinder group.
572 *
573 * It is a panic if a request is made to find a block if none are
574 * available.
575 */
576 static ufs_daddr_t
577 ffs_mapsearch(struct fs *fs, struct cg *cgp, ufs_daddr_t bpref, int allocsiz)
578 {
579 ufs_daddr_t bno;
580 int start, len, loc, i;
581 int blk, field, subfield, pos;
582 int ostart, olen;
583 const int needswap = UFS_FSNEEDSWAP(fs);
584
585 /*
586 * find the fragment by searching through the free block
587 * map for an appropriate bit pattern
588 */
589 if (bpref)
590 start = dtogd(fs, bpref) / NBBY;
591 else
592 start = ufs_rw32(cgp->cg_frotor, needswap) / NBBY;
593 len = howmany(fs->fs_fpg, NBBY) - start;
594 ostart = start;
595 olen = len;
596 loc = scanc((u_int)len,
597 (const u_char *)&cg_blksfree(cgp, needswap)[start],
598 (const u_char *)fragtbl[fs->fs_frag],
599 (1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
600 if (loc == 0) {
601 len = start + 1;
602 start = 0;
603 loc = scanc((u_int)len,
604 (const u_char *)&cg_blksfree(cgp, needswap)[0],
605 (const u_char *)fragtbl[fs->fs_frag],
606 (1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
607 if (loc == 0) {
608 errx(1,
609 "ffs_alloccg: map corrupted: start %d len %d offset %d %ld",
610 ostart, olen,
611 ufs_rw32(cgp->cg_freeoff, needswap),
612 (long)cg_blksfree(cgp, needswap) - (long)cgp);
613 /* NOTREACHED */
614 }
615 }
616 bno = (start + len - loc) * NBBY;
617 cgp->cg_frotor = ufs_rw32(bno, needswap);
618 /*
619 * found the byte in the map
620 * sift through the bits to find the selected frag
621 */
622 for (i = bno + NBBY; bno < i; bno += fs->fs_frag) {
623 blk = blkmap(fs, cg_blksfree(cgp, needswap), bno);
624 blk <<= 1;
625 field = around[allocsiz];
626 subfield = inside[allocsiz];
627 for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) {
628 if ((blk & field) == subfield)
629 return (bno + pos);
630 field <<= 1;
631 subfield <<= 1;
632 }
633 }
634 errx(1, "ffs_alloccg: block not in map: bno %d", bno);
635 return (-1);
636 }
637
638 /*
639 * Update the cluster map because of an allocation or free.
640 *
641 * Cnt == 1 means free; cnt == -1 means allocating.
642 */
643 void
644 ffs_clusteracct(struct fs *fs, struct cg *cgp, ufs_daddr_t blkno, int cnt)
645 {
646 int32_t *sump;
647 int32_t *lp;
648 u_char *freemapp, *mapp;
649 int i, start, end, forw, back, map, bit;
650 const int needswap = UFS_FSNEEDSWAP(fs);
651
652 if (fs->fs_contigsumsize <= 0)
653 return;
654 freemapp = cg_clustersfree(cgp, needswap);
655 sump = cg_clustersum(cgp, needswap);
656 /*
657 * Allocate or clear the actual block.
658 */
659 if (cnt > 0)
660 setbit(freemapp, blkno);
661 else
662 clrbit(freemapp, blkno);
663 /*
664 * Find the size of the cluster going forward.
665 */
666 start = blkno + 1;
667 end = start + fs->fs_contigsumsize;
668 if (end >= ufs_rw32(cgp->cg_nclusterblks, needswap))
669 end = ufs_rw32(cgp->cg_nclusterblks, needswap);
670 mapp = &freemapp[start / NBBY];
671 map = *mapp++;
672 bit = 1 << (start % NBBY);
673 for (i = start; i < end; i++) {
674 if ((map & bit) == 0)
675 break;
676 if ((i & (NBBY - 1)) != (NBBY - 1)) {
677 bit <<= 1;
678 } else {
679 map = *mapp++;
680 bit = 1;
681 }
682 }
683 forw = i - start;
684 /*
685 * Find the size of the cluster going backward.
686 */
687 start = blkno - 1;
688 end = start - fs->fs_contigsumsize;
689 if (end < 0)
690 end = -1;
691 mapp = &freemapp[start / NBBY];
692 map = *mapp--;
693 bit = 1 << (start % NBBY);
694 for (i = start; i > end; i--) {
695 if ((map & bit) == 0)
696 break;
697 if ((i & (NBBY - 1)) != 0) {
698 bit >>= 1;
699 } else {
700 map = *mapp--;
701 bit = 1 << (NBBY - 1);
702 }
703 }
704 back = start - i;
705 /*
706 * Account for old cluster and the possibly new forward and
707 * back clusters.
708 */
709 i = back + forw + 1;
710 if (i > fs->fs_contigsumsize)
711 i = fs->fs_contigsumsize;
712 ufs_add32(sump[i], cnt, needswap);
713 if (back > 0)
714 ufs_add32(sump[back], -cnt, needswap);
715 if (forw > 0)
716 ufs_add32(sump[forw], -cnt, needswap);
717
718 /*
719 * Update cluster summary information.
720 */
721 lp = &sump[fs->fs_contigsumsize];
722 for (i = fs->fs_contigsumsize; i > 0; i--)
723 if (ufs_rw32(*lp--, needswap) > 0)
724 break;
725 fs->fs_maxcluster[ufs_rw32(cgp->cg_cgx, needswap)] = i;
726 }
727