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