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