1 /* lfs_alloc.c 6.8 85/01/07 */ 2 3 #include "param.h" 4 #include "systm.h" 5 #include "mount.h" 6 #include "fs.h" 7 #include "conf.h" 8 #include "buf.h" 9 #include "inode.h" 10 #include "dir.h" 11 #include "user.h" 12 #include "quota.h" 13 #include "kernel.h" 14 15 extern u_long hashalloc(); 16 extern ino_t ialloccg(); 17 extern daddr_t alloccg(); 18 extern daddr_t alloccgblk(); 19 extern daddr_t fragextend(); 20 extern daddr_t blkpref(); 21 extern daddr_t mapsearch(); 22 extern int inside[], around[]; 23 extern unsigned char *fragtbl[]; 24 25 /* 26 * Allocate a block in the file system. 27 * 28 * The size of the requested block is given, which must be some 29 * multiple of fs_fsize and <= fs_bsize. 30 * A preference may be optionally specified. If a preference is given 31 * the following hierarchy is used to allocate a block: 32 * 1) allocate the requested block. 33 * 2) allocate a rotationally optimal block in the same cylinder. 34 * 3) allocate a block in the same cylinder group. 35 * 4) quadradically rehash into other cylinder groups, until an 36 * available block is located. 37 * If no block preference is given the following heirarchy is used 38 * to allocate a block: 39 * 1) allocate a block in the cylinder group that contains the 40 * inode for the file. 41 * 2) quadradically rehash into other cylinder groups, until an 42 * available block is located. 43 */ 44 struct buf * 45 alloc(ip, bpref, size) 46 register struct inode *ip; 47 daddr_t bpref; 48 int size; 49 { 50 daddr_t bno; 51 register struct fs *fs; 52 register struct buf *bp; 53 int cg; 54 55 fs = ip->i_fs; 56 if ((unsigned)size > fs->fs_bsize || fragoff(fs, size) != 0) { 57 printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n", 58 ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt); 59 panic("alloc: bad size"); 60 } 61 if (size == fs->fs_bsize && fs->fs_cstotal.cs_nbfree == 0) 62 goto nospace; 63 if (u.u_uid != 0 && freespace(fs, fs->fs_minfree) <= 0) 64 goto nospace; 65 #ifdef QUOTA 66 u.u_error = chkdq(ip, (long)btodb(size), 0); 67 if (u.u_error) 68 return (NULL); 69 #endif 70 if (bpref >= fs->fs_size) 71 bpref = 0; 72 if (bpref == 0) 73 cg = itog(fs, ip->i_number); 74 else 75 cg = dtog(fs, bpref); 76 bno = (daddr_t)hashalloc(ip, cg, (long)bpref, size, 77 (u_long (*)())alloccg); 78 if (bno <= 0) 79 goto nospace; 80 ip->i_blocks += btodb(size); 81 ip->i_flag |= IUPD|ICHG; 82 bp = getblk(ip->i_dev, fsbtodb(fs, bno), size); 83 clrbuf(bp); 84 return (bp); 85 nospace: 86 fserr(fs, "file system full"); 87 uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt); 88 u.u_error = ENOSPC; 89 return (NULL); 90 } 91 92 /* 93 * Reallocate a fragment to a bigger size 94 * 95 * The number and size of the old block is given, and a preference 96 * and new size is also specified. The allocator attempts to extend 97 * the original block. Failing that, the regular block allocator is 98 * invoked to get an appropriate block. 99 */ 100 struct buf * 101 realloccg(ip, bprev, bpref, osize, nsize) 102 register struct inode *ip; 103 daddr_t bprev, bpref; 104 int osize, nsize; 105 { 106 daddr_t bno; 107 register struct fs *fs; 108 register struct buf *bp, *obp; 109 int cg; 110 111 fs = ip->i_fs; 112 if ((unsigned)osize > fs->fs_bsize || fragoff(fs, osize) != 0 || 113 (unsigned)nsize > fs->fs_bsize || fragoff(fs, nsize) != 0) { 114 printf("dev = 0x%x, bsize = %d, osize = %d, nsize = %d, fs = %s\n", 115 ip->i_dev, fs->fs_bsize, osize, nsize, fs->fs_fsmnt); 116 panic("realloccg: bad size"); 117 } 118 if (u.u_uid != 0 && freespace(fs, fs->fs_minfree) <= 0) 119 goto nospace; 120 if (bprev == 0) { 121 printf("dev = 0x%x, bsize = %d, bprev = %d, fs = %s\n", 122 ip->i_dev, fs->fs_bsize, bprev, fs->fs_fsmnt); 123 panic("realloccg: bad bprev"); 124 } 125 #ifdef QUOTA 126 u.u_error = chkdq(ip, (long)btodb(nsize - osize), 0); 127 if (u.u_error) 128 return (NULL); 129 #endif 130 cg = dtog(fs, bprev); 131 bno = fragextend(ip, cg, (long)bprev, osize, nsize); 132 if (bno != 0) { 133 do { 134 bp = bread(ip->i_dev, fsbtodb(fs, bno), osize); 135 if (bp->b_flags & B_ERROR) { 136 brelse(bp); 137 return (NULL); 138 } 139 } while (brealloc(bp, nsize) == 0); 140 bp->b_flags |= B_DONE; 141 bzero(bp->b_un.b_addr + osize, (unsigned)nsize - osize); 142 ip->i_blocks += btodb(nsize - osize); 143 ip->i_flag |= IUPD|ICHG; 144 return (bp); 145 } 146 if (bpref >= fs->fs_size) 147 bpref = 0; 148 bno = (daddr_t)hashalloc(ip, cg, (long)bpref, nsize, 149 (u_long (*)())alloccg); 150 if (bno > 0) { 151 obp = bread(ip->i_dev, fsbtodb(fs, bprev), osize); 152 if (obp->b_flags & B_ERROR) { 153 brelse(obp); 154 return (NULL); 155 } 156 bp = getblk(ip->i_dev, fsbtodb(fs, bno), nsize); 157 bcopy(obp->b_un.b_addr, bp->b_un.b_addr, (u_int)osize); 158 bzero(bp->b_un.b_addr + osize, (unsigned)nsize - osize); 159 if (obp->b_flags & B_DELWRI) { 160 obp->b_flags &= ~B_DELWRI; 161 u.u_ru.ru_oublock--; /* delete charge */ 162 } 163 brelse(obp); 164 free(ip, bprev, (off_t)osize); 165 ip->i_blocks += btodb(nsize - osize); 166 ip->i_flag |= IUPD|ICHG; 167 return (bp); 168 } 169 nospace: 170 /* 171 * no space available 172 */ 173 fserr(fs, "file system full"); 174 uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt); 175 u.u_error = ENOSPC; 176 return (NULL); 177 } 178 179 /* 180 * Allocate an inode in the file system. 181 * 182 * A preference may be optionally specified. If a preference is given 183 * the following hierarchy is used to allocate an inode: 184 * 1) allocate the requested inode. 185 * 2) allocate an inode in the same cylinder group. 186 * 3) quadradically rehash into other cylinder groups, until an 187 * available inode is located. 188 * If no inode preference is given the following heirarchy is used 189 * to allocate an inode: 190 * 1) allocate an inode in cylinder group 0. 191 * 2) quadradically rehash into other cylinder groups, until an 192 * available inode is located. 193 */ 194 struct inode * 195 ialloc(pip, ipref, mode) 196 register struct inode *pip; 197 ino_t ipref; 198 int mode; 199 { 200 ino_t ino; 201 register struct fs *fs; 202 register struct inode *ip; 203 int cg; 204 205 fs = pip->i_fs; 206 if (fs->fs_cstotal.cs_nifree == 0) 207 goto noinodes; 208 #ifdef QUOTA 209 u.u_error = chkiq(pip->i_dev, (struct inode *)NULL, u.u_uid, 0); 210 if (u.u_error) 211 return (NULL); 212 #endif 213 if (ipref >= fs->fs_ncg * fs->fs_ipg) 214 ipref = 0; 215 cg = itog(fs, ipref); 216 ino = (ino_t)hashalloc(pip, cg, (long)ipref, mode, ialloccg); 217 if (ino == 0) 218 goto noinodes; 219 ip = iget(pip->i_dev, pip->i_fs, ino); 220 if (ip == NULL) { 221 ifree(pip, ino, 0); 222 return (NULL); 223 } 224 if (ip->i_mode) { 225 printf("mode = 0%o, inum = %d, fs = %s\n", 226 ip->i_mode, ip->i_number, fs->fs_fsmnt); 227 panic("ialloc: dup alloc"); 228 } 229 if (ip->i_blocks) { /* XXX */ 230 printf("free inode %s/%d had %d blocks\n", 231 fs->fs_fsmnt, ino, ip->i_blocks); 232 ip->i_blocks = 0; 233 } 234 return (ip); 235 noinodes: 236 fserr(fs, "out of inodes"); 237 uprintf("\n%s: create/symlink failed, no inodes free\n", fs->fs_fsmnt); 238 u.u_error = ENOSPC; 239 return (NULL); 240 } 241 242 /* 243 * Find a cylinder to place a directory. 244 * 245 * The policy implemented by this algorithm is to select from 246 * among those cylinder groups with above the average number of 247 * free inodes, the one with the smallest number of directories. 248 */ 249 ino_t 250 dirpref(fs) 251 register struct fs *fs; 252 { 253 int cg, minndir, mincg, avgifree; 254 255 avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg; 256 minndir = fs->fs_ipg; 257 mincg = 0; 258 for (cg = 0; cg < fs->fs_ncg; cg++) 259 if (fs->fs_cs(fs, cg).cs_ndir < minndir && 260 fs->fs_cs(fs, cg).cs_nifree >= avgifree) { 261 mincg = cg; 262 minndir = fs->fs_cs(fs, cg).cs_ndir; 263 } 264 return ((ino_t)(fs->fs_ipg * mincg)); 265 } 266 267 /* 268 * Select the desired position for the next block in a file. The file is 269 * logically divided into sections. The first section is composed of the 270 * direct blocks. Each additional section contains fs_maxbpg blocks. 271 * 272 * If no blocks have been allocated in the first section, the policy is to 273 * request a block in the same cylinder group as the inode that describes 274 * the file. If no blocks have been allocated in any other section, the 275 * policy is to place the section in a cylinder group with a greater than 276 * average number of free blocks. An appropriate cylinder group is found 277 * by maintaining a rotor that sweeps the cylinder groups. When a new 278 * group of blocks is needed, the rotor is advanced until a cylinder group 279 * with greater than the average number of free blocks is found. 280 * 281 * If a section is already partially allocated, the policy is to 282 * contiguously allocate fs_maxcontig blocks. The end of one of these 283 * contiguous blocks and the beginning of the next is physically separated 284 * so that the disk head will be in transit between them for at least 285 * fs_rotdelay milliseconds. This is to allow time for the processor to 286 * schedule another I/O transfer. 287 */ 288 daddr_t 289 blkpref(ip, lbn, indx, bap) 290 struct inode *ip; 291 daddr_t lbn; 292 int indx; 293 daddr_t *bap; 294 { 295 register struct fs *fs; 296 int cg, avgbfree; 297 daddr_t nextblk; 298 299 fs = ip->i_fs; 300 if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) { 301 if (lbn < NDADDR) { 302 cg = itog(fs, ip->i_number); 303 return (fs->fs_fpg * cg + fs->fs_frag); 304 } 305 /* 306 * Find a cylinder with greater than average number of 307 * unused data blocks. 308 */ 309 avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg; 310 for (cg = fs->fs_cgrotor + 1; cg < fs->fs_ncg; cg++) 311 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { 312 fs->fs_cgrotor = cg; 313 return (fs->fs_fpg * cg + fs->fs_frag); 314 } 315 for (cg = 0; cg <= fs->fs_cgrotor; cg++) 316 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { 317 fs->fs_cgrotor = cg; 318 return (fs->fs_fpg * cg + fs->fs_frag); 319 } 320 return (NULL); 321 } 322 /* 323 * One or more previous blocks have been laid out. If less 324 * than fs_maxcontig previous blocks are contiguous, the 325 * next block is requested contiguously, otherwise it is 326 * requested rotationally delayed by fs_rotdelay milliseconds. 327 */ 328 nextblk = bap[indx - 1] + fs->fs_frag; 329 if (indx > fs->fs_maxcontig && 330 bap[indx - fs->fs_maxcontig] + blkstofrags(fs, fs->fs_maxcontig) 331 != nextblk) 332 return (nextblk); 333 if (fs->fs_rotdelay != 0) 334 /* 335 * Here we convert ms of delay to frags as: 336 * (frags) = (ms) * (rev/sec) * (sect/rev) / 337 * ((sect/frag) * (ms/sec)) 338 * then round up to the next block. 339 */ 340 nextblk += roundup(fs->fs_rotdelay * fs->fs_rps * fs->fs_nsect / 341 (NSPF(fs) * 1000), fs->fs_frag); 342 return (nextblk); 343 } 344 345 /* 346 * Implement the cylinder overflow algorithm. 347 * 348 * The policy implemented by this algorithm is: 349 * 1) allocate the block in its requested cylinder group. 350 * 2) quadradically rehash on the cylinder group number. 351 * 3) brute force search for a free block. 352 */ 353 /*VARARGS5*/ 354 u_long 355 hashalloc(ip, cg, pref, size, allocator) 356 struct inode *ip; 357 int cg; 358 long pref; 359 int size; /* size for data blocks, mode for inodes */ 360 u_long (*allocator)(); 361 { 362 register struct fs *fs; 363 long result; 364 int i, icg = cg; 365 366 fs = ip->i_fs; 367 /* 368 * 1: preferred cylinder group 369 */ 370 result = (*allocator)(ip, cg, pref, size); 371 if (result) 372 return (result); 373 /* 374 * 2: quadratic rehash 375 */ 376 for (i = 1; i < fs->fs_ncg; i *= 2) { 377 cg += i; 378 if (cg >= fs->fs_ncg) 379 cg -= fs->fs_ncg; 380 result = (*allocator)(ip, cg, 0, size); 381 if (result) 382 return (result); 383 } 384 /* 385 * 3: brute force search 386 * Note that we start at i == 2, since 0 was checked initially, 387 * and 1 is always checked in the quadratic rehash. 388 */ 389 cg = (icg + 2) % fs->fs_ncg; 390 for (i = 2; i < fs->fs_ncg; i++) { 391 result = (*allocator)(ip, cg, 0, size); 392 if (result) 393 return (result); 394 cg++; 395 if (cg == fs->fs_ncg) 396 cg = 0; 397 } 398 return (NULL); 399 } 400 401 /* 402 * Determine whether a fragment can be extended. 403 * 404 * Check to see if the necessary fragments are available, and 405 * if they are, allocate them. 406 */ 407 daddr_t 408 fragextend(ip, cg, bprev, osize, nsize) 409 struct inode *ip; 410 int cg; 411 long bprev; 412 int osize, nsize; 413 { 414 register struct fs *fs; 415 register struct buf *bp; 416 register struct cg *cgp; 417 long bno; 418 int frags, bbase; 419 int i; 420 421 fs = ip->i_fs; 422 if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize)) 423 return (NULL); 424 frags = numfrags(fs, nsize); 425 bbase = fragnum(fs, bprev); 426 if (bbase > fragnum(fs, (bprev + frags - 1))) { 427 /* cannot extend across a block boundry */ 428 return (NULL); 429 } 430 bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize); 431 cgp = bp->b_un.b_cg; 432 if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC) { 433 brelse(bp); 434 return (NULL); 435 } 436 cgp->cg_time = time.tv_sec; 437 bno = dtogd(fs, bprev); 438 for (i = numfrags(fs, osize); i < frags; i++) 439 if (isclr(cgp->cg_free, bno + i)) { 440 brelse(bp); 441 return (NULL); 442 } 443 /* 444 * the current fragment can be extended 445 * deduct the count on fragment being extended into 446 * increase the count on the remaining fragment (if any) 447 * allocate the extended piece 448 */ 449 for (i = frags; i < fs->fs_frag - bbase; i++) 450 if (isclr(cgp->cg_free, bno + i)) 451 break; 452 cgp->cg_frsum[i - numfrags(fs, osize)]--; 453 if (i != frags) 454 cgp->cg_frsum[i - frags]++; 455 for (i = numfrags(fs, osize); i < frags; i++) { 456 clrbit(cgp->cg_free, bno + i); 457 cgp->cg_cs.cs_nffree--; 458 fs->fs_cstotal.cs_nffree--; 459 fs->fs_cs(fs, cg).cs_nffree--; 460 } 461 fs->fs_fmod++; 462 bdwrite(bp); 463 return (bprev); 464 } 465 466 /* 467 * Determine whether a block can be allocated. 468 * 469 * Check to see if a block of the apprpriate size is available, 470 * and if it is, allocate it. 471 */ 472 daddr_t 473 alloccg(ip, cg, bpref, size) 474 struct inode *ip; 475 int cg; 476 daddr_t bpref; 477 int size; 478 { 479 register struct fs *fs; 480 register struct buf *bp; 481 register struct cg *cgp; 482 int bno, frags; 483 int allocsiz; 484 register int i; 485 486 fs = ip->i_fs; 487 if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize) 488 return (NULL); 489 bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize); 490 cgp = bp->b_un.b_cg; 491 if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC || 492 (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) { 493 brelse(bp); 494 return (NULL); 495 } 496 cgp->cg_time = time.tv_sec; 497 if (size == fs->fs_bsize) { 498 bno = alloccgblk(fs, cgp, bpref); 499 bdwrite(bp); 500 return (bno); 501 } 502 /* 503 * check to see if any fragments are already available 504 * allocsiz is the size which will be allocated, hacking 505 * it down to a smaller size if necessary 506 */ 507 frags = numfrags(fs, size); 508 for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++) 509 if (cgp->cg_frsum[allocsiz] != 0) 510 break; 511 if (allocsiz == fs->fs_frag) { 512 /* 513 * no fragments were available, so a block will be 514 * allocated, and hacked up 515 */ 516 if (cgp->cg_cs.cs_nbfree == 0) { 517 brelse(bp); 518 return (NULL); 519 } 520 bno = alloccgblk(fs, cgp, bpref); 521 bpref = dtogd(fs, bno); 522 for (i = frags; i < fs->fs_frag; i++) 523 setbit(cgp->cg_free, bpref + i); 524 i = fs->fs_frag - frags; 525 cgp->cg_cs.cs_nffree += i; 526 fs->fs_cstotal.cs_nffree += i; 527 fs->fs_cs(fs, cg).cs_nffree += i; 528 fs->fs_fmod++; 529 cgp->cg_frsum[i]++; 530 bdwrite(bp); 531 return (bno); 532 } 533 bno = mapsearch(fs, cgp, bpref, allocsiz); 534 if (bno < 0) { 535 brelse(bp); 536 return (NULL); 537 } 538 for (i = 0; i < frags; i++) 539 clrbit(cgp->cg_free, bno + i); 540 cgp->cg_cs.cs_nffree -= frags; 541 fs->fs_cstotal.cs_nffree -= frags; 542 fs->fs_cs(fs, cg).cs_nffree -= frags; 543 fs->fs_fmod++; 544 cgp->cg_frsum[allocsiz]--; 545 if (frags != allocsiz) 546 cgp->cg_frsum[allocsiz - frags]++; 547 bdwrite(bp); 548 return (cg * fs->fs_fpg + bno); 549 } 550 551 /* 552 * Allocate a block in a cylinder group. 553 * 554 * This algorithm implements the following policy: 555 * 1) allocate the requested block. 556 * 2) allocate a rotationally optimal block in the same cylinder. 557 * 3) allocate the next available block on the block rotor for the 558 * specified cylinder group. 559 * Note that this routine only allocates fs_bsize blocks; these 560 * blocks may be fragmented by the routine that allocates them. 561 */ 562 daddr_t 563 alloccgblk(fs, cgp, bpref) 564 register struct fs *fs; 565 register struct cg *cgp; 566 daddr_t bpref; 567 { 568 daddr_t bno; 569 int cylno, pos, delta; 570 short *cylbp; 571 register int i; 572 573 if (bpref == 0) { 574 bpref = cgp->cg_rotor; 575 goto norot; 576 } 577 bpref = blknum(fs, bpref); 578 bpref = dtogd(fs, bpref); 579 /* 580 * if the requested block is available, use it 581 */ 582 if (isblock(fs, cgp->cg_free, fragstoblks(fs, bpref))) { 583 bno = bpref; 584 goto gotit; 585 } 586 /* 587 * check for a block available on the same cylinder 588 */ 589 cylno = cbtocylno(fs, bpref); 590 if (cgp->cg_btot[cylno] == 0) 591 goto norot; 592 if (fs->fs_cpc == 0) { 593 /* 594 * block layout info is not available, so just have 595 * to take any block in this cylinder. 596 */ 597 bpref = howmany(fs->fs_spc * cylno, NSPF(fs)); 598 goto norot; 599 } 600 /* 601 * check the summary information to see if a block is 602 * available in the requested cylinder starting at the 603 * requested rotational position and proceeding around. 604 */ 605 cylbp = cgp->cg_b[cylno]; 606 pos = cbtorpos(fs, bpref); 607 for (i = pos; i < NRPOS; i++) 608 if (cylbp[i] > 0) 609 break; 610 if (i == NRPOS) 611 for (i = 0; i < pos; i++) 612 if (cylbp[i] > 0) 613 break; 614 if (cylbp[i] > 0) { 615 /* 616 * found a rotational position, now find the actual 617 * block. A panic if none is actually there. 618 */ 619 pos = cylno % fs->fs_cpc; 620 bno = (cylno - pos) * fs->fs_spc / NSPB(fs); 621 if (fs->fs_postbl[pos][i] == -1) { 622 printf("pos = %d, i = %d, fs = %s\n", 623 pos, i, fs->fs_fsmnt); 624 panic("alloccgblk: cyl groups corrupted"); 625 } 626 for (i = fs->fs_postbl[pos][i];; ) { 627 if (isblock(fs, cgp->cg_free, bno + i)) { 628 bno = blkstofrags(fs, (bno + i)); 629 goto gotit; 630 } 631 delta = fs->fs_rotbl[i]; 632 if (delta <= 0 || delta > MAXBPC - i) 633 break; 634 i += delta; 635 } 636 printf("pos = %d, i = %d, fs = %s\n", pos, i, fs->fs_fsmnt); 637 panic("alloccgblk: can't find blk in cyl"); 638 } 639 norot: 640 /* 641 * no blocks in the requested cylinder, so take next 642 * available one in this cylinder group. 643 */ 644 bno = mapsearch(fs, cgp, bpref, (int)fs->fs_frag); 645 if (bno < 0) 646 return (NULL); 647 cgp->cg_rotor = bno; 648 gotit: 649 clrblock(fs, cgp->cg_free, (long)fragstoblks(fs, bno)); 650 cgp->cg_cs.cs_nbfree--; 651 fs->fs_cstotal.cs_nbfree--; 652 fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--; 653 cylno = cbtocylno(fs, bno); 654 cgp->cg_b[cylno][cbtorpos(fs, bno)]--; 655 cgp->cg_btot[cylno]--; 656 fs->fs_fmod++; 657 return (cgp->cg_cgx * fs->fs_fpg + bno); 658 } 659 660 /* 661 * Determine whether an inode can be allocated. 662 * 663 * Check to see if an inode is available, and if it is, 664 * allocate it using the following policy: 665 * 1) allocate the requested inode. 666 * 2) allocate the next available inode after the requested 667 * inode in the specified cylinder group. 668 */ 669 ino_t 670 ialloccg(ip, cg, ipref, mode) 671 struct inode *ip; 672 int cg; 673 daddr_t ipref; 674 int mode; 675 { 676 register struct fs *fs; 677 register struct cg *cgp; 678 struct buf *bp; 679 int start, len, loc, map, i; 680 681 fs = ip->i_fs; 682 if (fs->fs_cs(fs, cg).cs_nifree == 0) 683 return (NULL); 684 bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize); 685 cgp = bp->b_un.b_cg; 686 if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC || 687 cgp->cg_cs.cs_nifree == 0) { 688 brelse(bp); 689 return (NULL); 690 } 691 cgp->cg_time = time.tv_sec; 692 if (ipref) { 693 ipref %= fs->fs_ipg; 694 if (isclr(cgp->cg_iused, ipref)) 695 goto gotit; 696 } 697 start = cgp->cg_irotor / NBBY; 698 len = howmany(fs->fs_ipg - cgp->cg_irotor, NBBY); 699 loc = skpc(0xff, len, &cgp->cg_iused[start]); 700 if (loc == 0) { 701 printf("cg = %s, irotor = %d, fs = %s\n", 702 cg, cgp->cg_irotor, fs->fs_fsmnt); 703 panic("ialloccg: map corrupted"); 704 /* NOTREACHED */ 705 } 706 i = start + len - loc; 707 map = cgp->cg_iused[i]; 708 ipref = i * NBBY; 709 for (i = 1; i < (1 << NBBY); i <<= 1, ipref++) { 710 if ((map & i) == 0) { 711 cgp->cg_irotor = ipref; 712 goto gotit; 713 } 714 } 715 printf("fs = %s\n", fs->fs_fsmnt); 716 panic("ialloccg: block not in map"); 717 /* NOTREACHED */ 718 gotit: 719 setbit(cgp->cg_iused, ipref); 720 cgp->cg_cs.cs_nifree--; 721 fs->fs_cstotal.cs_nifree--; 722 fs->fs_cs(fs, cg).cs_nifree--; 723 fs->fs_fmod++; 724 if ((mode & IFMT) == IFDIR) { 725 cgp->cg_cs.cs_ndir++; 726 fs->fs_cstotal.cs_ndir++; 727 fs->fs_cs(fs, cg).cs_ndir++; 728 } 729 bdwrite(bp); 730 return (cg * fs->fs_ipg + ipref); 731 } 732 733 /* 734 * Free a block or fragment. 735 * 736 * The specified block or fragment is placed back in the 737 * free map. If a fragment is deallocated, a possible 738 * block reassembly is checked. 739 */ 740 free(ip, bno, size) 741 register struct inode *ip; 742 daddr_t bno; 743 off_t size; 744 { 745 register struct fs *fs; 746 register struct cg *cgp; 747 register struct buf *bp; 748 int cg, blk, frags, bbase; 749 register int i; 750 751 fs = ip->i_fs; 752 if ((unsigned)size > fs->fs_bsize || fragoff(fs, size) != 0) { 753 printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n", 754 ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt); 755 panic("free: bad size"); 756 } 757 cg = dtog(fs, bno); 758 if (badblock(fs, bno)) { 759 printf("bad block %d, ino %d\n", bno, ip->i_number); 760 return; 761 } 762 bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize); 763 cgp = bp->b_un.b_cg; 764 if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC) { 765 brelse(bp); 766 return; 767 } 768 cgp->cg_time = time.tv_sec; 769 bno = dtogd(fs, bno); 770 if (size == fs->fs_bsize) { 771 if (isblock(fs, cgp->cg_free, fragstoblks(fs, bno))) { 772 printf("dev = 0x%x, block = %d, fs = %s\n", 773 ip->i_dev, bno, fs->fs_fsmnt); 774 panic("free: freeing free block"); 775 } 776 setblock(fs, cgp->cg_free, fragstoblks(fs, bno)); 777 cgp->cg_cs.cs_nbfree++; 778 fs->fs_cstotal.cs_nbfree++; 779 fs->fs_cs(fs, cg).cs_nbfree++; 780 i = cbtocylno(fs, bno); 781 cgp->cg_b[i][cbtorpos(fs, bno)]++; 782 cgp->cg_btot[i]++; 783 } else { 784 bbase = bno - fragnum(fs, bno); 785 /* 786 * decrement the counts associated with the old frags 787 */ 788 blk = blkmap(fs, cgp->cg_free, bbase); 789 fragacct(fs, blk, cgp->cg_frsum, -1); 790 /* 791 * deallocate the fragment 792 */ 793 frags = numfrags(fs, size); 794 for (i = 0; i < frags; i++) { 795 if (isset(cgp->cg_free, bno + i)) { 796 printf("dev = 0x%x, block = %d, fs = %s\n", 797 ip->i_dev, bno + i, fs->fs_fsmnt); 798 panic("free: freeing free frag"); 799 } 800 setbit(cgp->cg_free, bno + i); 801 } 802 cgp->cg_cs.cs_nffree += i; 803 fs->fs_cstotal.cs_nffree += i; 804 fs->fs_cs(fs, cg).cs_nffree += i; 805 /* 806 * add back in counts associated with the new frags 807 */ 808 blk = blkmap(fs, cgp->cg_free, bbase); 809 fragacct(fs, blk, cgp->cg_frsum, 1); 810 /* 811 * if a complete block has been reassembled, account for it 812 */ 813 if (isblock(fs, cgp->cg_free, fragstoblks(fs, bbase))) { 814 cgp->cg_cs.cs_nffree -= fs->fs_frag; 815 fs->fs_cstotal.cs_nffree -= fs->fs_frag; 816 fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag; 817 cgp->cg_cs.cs_nbfree++; 818 fs->fs_cstotal.cs_nbfree++; 819 fs->fs_cs(fs, cg).cs_nbfree++; 820 i = cbtocylno(fs, bbase); 821 cgp->cg_b[i][cbtorpos(fs, bbase)]++; 822 cgp->cg_btot[i]++; 823 } 824 } 825 fs->fs_fmod++; 826 bdwrite(bp); 827 } 828 829 /* 830 * Free an inode. 831 * 832 * The specified inode is placed back in the free map. 833 */ 834 ifree(ip, ino, mode) 835 struct inode *ip; 836 ino_t ino; 837 int mode; 838 { 839 register struct fs *fs; 840 register struct cg *cgp; 841 register struct buf *bp; 842 int cg; 843 844 fs = ip->i_fs; 845 if ((unsigned)ino >= fs->fs_ipg*fs->fs_ncg) { 846 printf("dev = 0x%x, ino = %d, fs = %s\n", 847 ip->i_dev, ino, fs->fs_fsmnt); 848 panic("ifree: range"); 849 } 850 cg = itog(fs, ino); 851 bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize); 852 cgp = bp->b_un.b_cg; 853 if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC) { 854 brelse(bp); 855 return; 856 } 857 cgp->cg_time = time.tv_sec; 858 ino %= fs->fs_ipg; 859 if (isclr(cgp->cg_iused, ino)) { 860 printf("dev = 0x%x, ino = %d, fs = %s\n", 861 ip->i_dev, ino, fs->fs_fsmnt); 862 panic("ifree: freeing free inode"); 863 } 864 clrbit(cgp->cg_iused, ino); 865 if (ino < cgp->cg_irotor) 866 cgp->cg_irotor = ino; 867 cgp->cg_cs.cs_nifree++; 868 fs->fs_cstotal.cs_nifree++; 869 fs->fs_cs(fs, cg).cs_nifree++; 870 if ((mode & IFMT) == IFDIR) { 871 cgp->cg_cs.cs_ndir--; 872 fs->fs_cstotal.cs_ndir--; 873 fs->fs_cs(fs, cg).cs_ndir--; 874 } 875 fs->fs_fmod++; 876 bdwrite(bp); 877 } 878 879 /* 880 * Find a block of the specified size in the specified cylinder group. 881 * 882 * It is a panic if a request is made to find a block if none are 883 * available. 884 */ 885 daddr_t 886 mapsearch(fs, cgp, bpref, allocsiz) 887 register struct fs *fs; 888 register struct cg *cgp; 889 daddr_t bpref; 890 int allocsiz; 891 { 892 daddr_t bno; 893 int start, len, loc, i; 894 int blk, field, subfield, pos; 895 896 /* 897 * find the fragment by searching through the free block 898 * map for an appropriate bit pattern 899 */ 900 if (bpref) 901 start = dtogd(fs, bpref) / NBBY; 902 else 903 start = cgp->cg_frotor / NBBY; 904 len = howmany(fs->fs_fpg, NBBY) - start; 905 loc = scanc((unsigned)len, (caddr_t)&cgp->cg_free[start], 906 (caddr_t)fragtbl[fs->fs_frag], 907 (int)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY)))); 908 if (loc == 0) { 909 len = start + 1; 910 start = 0; 911 loc = scanc((unsigned)len, (caddr_t)&cgp->cg_free[start], 912 (caddr_t)fragtbl[fs->fs_frag], 913 (int)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY)))); 914 if (loc == 0) { 915 printf("start = %d, len = %d, fs = %s\n", 916 start, len, fs->fs_fsmnt); 917 panic("alloccg: map corrupted"); 918 return (-1); 919 } 920 } 921 bno = (start + len - loc) * NBBY; 922 cgp->cg_frotor = bno; 923 /* 924 * found the byte in the map 925 * sift through the bits to find the selected frag 926 */ 927 for (i = bno + NBBY; bno < i; bno += fs->fs_frag) { 928 blk = blkmap(fs, cgp->cg_free, bno); 929 blk <<= 1; 930 field = around[allocsiz]; 931 subfield = inside[allocsiz]; 932 for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) { 933 if ((blk & field) == subfield) 934 return (bno + pos); 935 field <<= 1; 936 subfield <<= 1; 937 } 938 } 939 printf("bno = %d, fs = %s\n", bno, fs->fs_fsmnt); 940 panic("alloccg: block not in map"); 941 return (-1); 942 } 943 944 /* 945 * Fserr prints the name of a file system with an error diagnostic. 946 * 947 * The form of the error message is: 948 * fs: error message 949 */ 950 fserr(fs, cp) 951 struct fs *fs; 952 char *cp; 953 { 954 955 printf("%s: %s\n", fs->fs_fsmnt, cp); 956 } 957