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