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