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