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