1 /* 2 * Copyright (c) 1982, 1986, 1989, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * @(#)ffs_alloc.c 8.8 (Berkeley) 2/21/94 34 */ 35 36 #include <sys/param.h> 37 #include <sys/systm.h> 38 #include <sys/buf.h> 39 #include <sys/proc.h> 40 #include <sys/vnode.h> 41 #include <sys/mount.h> 42 #include <sys/kernel.h> 43 #include <sys/syslog.h> 44 45 #include <vm/vm.h> 46 47 #include <ufs/ufs/quota.h> 48 #include <ufs/ufs/inode.h> 49 50 #include <ufs/ffs/fs.h> 51 #include <ufs/ffs/ffs_extern.h> 52 53 extern u_long nextgennumber; 54 55 static daddr_t ffs_alloccg __P((struct inode *, int, daddr_t, int)); 56 static daddr_t ffs_alloccgblk __P((struct fs *, struct cg *, daddr_t)); 57 static daddr_t ffs_clusteralloc __P((struct inode *, int, daddr_t, int)); 58 static ino_t ffs_dirpref __P((struct fs *)); 59 static daddr_t ffs_fragextend __P((struct inode *, int, long, int, int)); 60 static void ffs_fserr __P((struct fs *, u_int, char *)); 61 static u_long ffs_hashalloc 62 __P((struct inode *, int, long, int, u_long (*)())); 63 static ino_t ffs_nodealloccg __P((struct inode *, int, daddr_t, int)); 64 static daddr_t ffs_mapsearch __P((struct fs *, struct cg *, daddr_t, int)); 65 66 void ffs_clusteracct __P((struct fs *, struct cg *, daddr_t, int)); 67 68 /* 69 * Allocate a block in the file system. 70 * 71 * The size of the requested block is given, which must be some 72 * multiple of fs_fsize and <= fs_bsize. 73 * A preference may be optionally specified. If a preference is given 74 * the following hierarchy is used to allocate a block: 75 * 1) allocate the requested block. 76 * 2) allocate a rotationally optimal block in the same cylinder. 77 * 3) allocate a block in the same cylinder group. 78 * 4) quadradically rehash into other cylinder groups, until an 79 * available block is located. 80 * If no block preference is given the following heirarchy is used 81 * to allocate a block: 82 * 1) allocate a block in the cylinder group that contains the 83 * inode for the file. 84 * 2) quadradically rehash into other cylinder groups, until an 85 * available block is located. 86 */ 87 int 88 ffs_alloc(ip, lbn, bpref, size, cred, bnp) 89 register struct inode *ip; 90 daddr_t lbn, bpref; 91 int size; 92 struct ucred *cred; 93 daddr_t *bnp; 94 { 95 register struct fs *fs; 96 daddr_t bno; 97 int cg, error; 98 99 *bnp = 0; 100 fs = ip->i_fs; 101 #ifdef DIAGNOSTIC 102 if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0) { 103 printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n", 104 ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt); 105 panic("ffs_alloc: bad size"); 106 } 107 if (cred == NOCRED) 108 panic("ffs_alloc: missing credential\n"); 109 #endif /* DIAGNOSTIC */ 110 if (size == fs->fs_bsize && fs->fs_cstotal.cs_nbfree == 0) 111 goto nospace; 112 if (cred->cr_uid != 0 && freespace(fs, fs->fs_minfree) <= 0) 113 goto nospace; 114 #ifdef QUOTA 115 if (error = chkdq(ip, (long)btodb(size), cred, 0)) 116 return (error); 117 #endif 118 if (bpref >= fs->fs_size) 119 bpref = 0; 120 if (bpref == 0) 121 cg = ino_to_cg(fs, ip->i_number); 122 else 123 cg = dtog(fs, bpref); 124 bno = (daddr_t)ffs_hashalloc(ip, cg, (long)bpref, size, 125 (u_long (*)())ffs_alloccg); 126 if (bno > 0) { 127 ip->i_blocks += btodb(size); 128 ip->i_flag |= IN_CHANGE | IN_UPDATE; 129 *bnp = bno; 130 return (0); 131 } 132 #ifdef QUOTA 133 /* 134 * Restore user's disk quota because allocation failed. 135 */ 136 (void) chkdq(ip, (long)-btodb(size), cred, FORCE); 137 #endif 138 nospace: 139 ffs_fserr(fs, cred->cr_uid, "file system full"); 140 uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt); 141 return (ENOSPC); 142 } 143 144 /* 145 * Reallocate a fragment to a bigger size 146 * 147 * The number and size of the old block is given, and a preference 148 * and new size is also specified. The allocator attempts to extend 149 * the original block. Failing that, the regular block allocator is 150 * invoked to get an appropriate block. 151 */ 152 int 153 ffs_realloccg(ip, lbprev, bpref, osize, nsize, cred, bpp) 154 register struct inode *ip; 155 daddr_t lbprev; 156 daddr_t bpref; 157 int osize, nsize; 158 struct ucred *cred; 159 struct buf **bpp; 160 { 161 register struct fs *fs; 162 struct buf *bp; 163 int cg, request, error; 164 daddr_t bprev, bno; 165 166 *bpp = 0; 167 fs = ip->i_fs; 168 #ifdef DIAGNOSTIC 169 if ((u_int)osize > fs->fs_bsize || fragoff(fs, osize) != 0 || 170 (u_int)nsize > fs->fs_bsize || fragoff(fs, nsize) != 0) { 171 printf( 172 "dev = 0x%x, bsize = %d, osize = %d, nsize = %d, fs = %s\n", 173 ip->i_dev, fs->fs_bsize, osize, nsize, fs->fs_fsmnt); 174 panic("ffs_realloccg: bad size"); 175 } 176 if (cred == NOCRED) 177 panic("ffs_realloccg: missing credential\n"); 178 #endif /* DIAGNOSTIC */ 179 if (cred->cr_uid != 0 && freespace(fs, fs->fs_minfree) <= 0) 180 goto nospace; 181 if ((bprev = ip->i_db[lbprev]) == 0) { 182 printf("dev = 0x%x, bsize = %d, bprev = %d, fs = %s\n", 183 ip->i_dev, fs->fs_bsize, bprev, fs->fs_fsmnt); 184 panic("ffs_realloccg: bad bprev"); 185 } 186 /* 187 * Allocate the extra space in the buffer. 188 */ 189 if (error = bread(ITOV(ip), lbprev, osize, NOCRED, &bp)) { 190 brelse(bp); 191 return (error); 192 } 193 #ifdef QUOTA 194 if (error = chkdq(ip, (long)btodb(nsize - osize), cred, 0)) { 195 brelse(bp); 196 return (error); 197 } 198 #endif 199 /* 200 * Check for extension in the existing location. 201 */ 202 cg = dtog(fs, bprev); 203 if (bno = ffs_fragextend(ip, cg, (long)bprev, osize, nsize)) { 204 if (bp->b_blkno != fsbtodb(fs, bno)) 205 panic("bad blockno"); 206 ip->i_blocks += btodb(nsize - osize); 207 ip->i_flag |= IN_CHANGE | IN_UPDATE; 208 allocbuf(bp, nsize); 209 bp->b_flags |= B_DONE; 210 bzero((char *)bp->b_data + osize, (u_int)nsize - osize); 211 *bpp = bp; 212 return (0); 213 } 214 /* 215 * Allocate a new disk location. 216 */ 217 if (bpref >= fs->fs_size) 218 bpref = 0; 219 switch ((int)fs->fs_optim) { 220 case FS_OPTSPACE: 221 /* 222 * Allocate an exact sized fragment. Although this makes 223 * best use of space, we will waste time relocating it if 224 * the file continues to grow. If the fragmentation is 225 * less than half of the minimum free reserve, we choose 226 * to begin optimizing for time. 227 */ 228 request = nsize; 229 if (fs->fs_minfree < 5 || 230 fs->fs_cstotal.cs_nffree > 231 fs->fs_dsize * fs->fs_minfree / (2 * 100)) 232 break; 233 log(LOG_NOTICE, "%s: optimization changed from SPACE to TIME\n", 234 fs->fs_fsmnt); 235 fs->fs_optim = FS_OPTTIME; 236 break; 237 case FS_OPTTIME: 238 /* 239 * At this point we have discovered a file that is trying to 240 * grow a small fragment to a larger fragment. To save time, 241 * we allocate a full sized block, then free the unused portion. 242 * If the file continues to grow, the `ffs_fragextend' call 243 * above will be able to grow it in place without further 244 * copying. If aberrant programs cause disk fragmentation to 245 * grow within 2% of the free reserve, we choose to begin 246 * optimizing for space. 247 */ 248 request = fs->fs_bsize; 249 if (fs->fs_cstotal.cs_nffree < 250 fs->fs_dsize * (fs->fs_minfree - 2) / 100) 251 break; 252 log(LOG_NOTICE, "%s: optimization changed from TIME to SPACE\n", 253 fs->fs_fsmnt); 254 fs->fs_optim = FS_OPTSPACE; 255 break; 256 default: 257 printf("dev = 0x%x, optim = %d, fs = %s\n", 258 ip->i_dev, fs->fs_optim, fs->fs_fsmnt); 259 panic("ffs_realloccg: bad optim"); 260 /* NOTREACHED */ 261 } 262 bno = (daddr_t)ffs_hashalloc(ip, cg, (long)bpref, request, 263 (u_long (*)())ffs_alloccg); 264 if (bno > 0) { 265 bp->b_blkno = fsbtodb(fs, bno); 266 (void) vnode_pager_uncache(ITOV(ip)); 267 ffs_blkfree(ip, bprev, (long)osize); 268 if (nsize < request) 269 ffs_blkfree(ip, bno + numfrags(fs, nsize), 270 (long)(request - nsize)); 271 ip->i_blocks += btodb(nsize - osize); 272 ip->i_flag |= IN_CHANGE | IN_UPDATE; 273 allocbuf(bp, nsize); 274 bp->b_flags |= B_DONE; 275 bzero((char *)bp->b_data + osize, (u_int)nsize - osize); 276 *bpp = bp; 277 return (0); 278 } 279 #ifdef QUOTA 280 /* 281 * Restore user's disk quota because allocation failed. 282 */ 283 (void) chkdq(ip, (long)-btodb(nsize - osize), cred, FORCE); 284 #endif 285 brelse(bp); 286 nospace: 287 /* 288 * no space available 289 */ 290 ffs_fserr(fs, cred->cr_uid, "file system full"); 291 uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt); 292 return (ENOSPC); 293 } 294 295 /* 296 * Reallocate a sequence of blocks into a contiguous sequence of blocks. 297 * 298 * The vnode and an array of buffer pointers for a range of sequential 299 * logical blocks to be made contiguous is given. The allocator attempts 300 * to find a range of sequential blocks starting as close as possible to 301 * an fs_rotdelay offset from the end of the allocation for the logical 302 * block immediately preceeding the current range. If successful, the 303 * physical block numbers in the buffer pointers and in the inode are 304 * changed to reflect the new allocation. If unsuccessful, the allocation 305 * is left unchanged. The success in doing the reallocation is returned. 306 * Note that the error return is not reflected back to the user. Rather 307 * the previous block allocation will be used. 308 */ 309 #include <sys/sysctl.h> 310 int doasyncfree = 1; 311 #ifdef DEBUG 312 struct ctldebug debug14 = { "doasyncfree", &doasyncfree }; 313 #endif 314 int 315 ffs_reallocblks(ap) 316 struct vop_reallocblks_args /* { 317 struct vnode *a_vp; 318 struct cluster_save *a_buflist; 319 } */ *ap; 320 { 321 struct fs *fs; 322 struct inode *ip; 323 struct vnode *vp; 324 struct buf *sbp, *ebp; 325 daddr_t *bap, *sbap, *ebap = 0; 326 struct cluster_save *buflist; 327 daddr_t start_lbn, end_lbn, soff, eoff, newblk, blkno; 328 struct indir start_ap[NIADDR + 1], end_ap[NIADDR + 1], *idp; 329 int i, len, start_lvl, end_lvl, pref, ssize; 330 331 vp = ap->a_vp; 332 ip = VTOI(vp); 333 fs = ip->i_fs; 334 if (fs->fs_contigsumsize <= 0) 335 return (ENOSPC); 336 buflist = ap->a_buflist; 337 len = buflist->bs_nchildren; 338 start_lbn = buflist->bs_children[0]->b_lblkno; 339 end_lbn = start_lbn + len - 1; 340 #ifdef DIAGNOSTIC 341 for (i = 1; i < len; i++) 342 if (buflist->bs_children[i]->b_lblkno != start_lbn + i) 343 panic("ffs_reallocblks: non-cluster"); 344 #endif 345 /* 346 * If the latest allocation is in a new cylinder group, assume that 347 * the filesystem has decided to move and do not force it back to 348 * the previous cylinder group. 349 */ 350 if (dtog(fs, dbtofsb(fs, buflist->bs_children[0]->b_blkno)) != 351 dtog(fs, dbtofsb(fs, buflist->bs_children[len - 1]->b_blkno))) 352 return (ENOSPC); 353 if (ufs_getlbns(vp, start_lbn, start_ap, &start_lvl) || 354 ufs_getlbns(vp, end_lbn, end_ap, &end_lvl)) 355 return (ENOSPC); 356 /* 357 * Get the starting offset and block map for the first block. 358 */ 359 if (start_lvl == 0) { 360 sbap = &ip->i_db[0]; 361 soff = start_lbn; 362 } else { 363 idp = &start_ap[start_lvl - 1]; 364 if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &sbp)) { 365 brelse(sbp); 366 return (ENOSPC); 367 } 368 sbap = (daddr_t *)sbp->b_data; 369 soff = idp->in_off; 370 } 371 /* 372 * Find the preferred location for the cluster. 373 */ 374 pref = ffs_blkpref(ip, start_lbn, soff, sbap); 375 /* 376 * If the block range spans two block maps, get the second map. 377 */ 378 if (end_lvl == 0 || (idp = &end_ap[end_lvl - 1])->in_off + 1 >= len) { 379 ssize = len; 380 } else { 381 #ifdef DIAGNOSTIC 382 if (start_ap[start_lvl-1].in_lbn == idp->in_lbn) 383 panic("ffs_reallocblk: start == end"); 384 #endif 385 ssize = len - (idp->in_off + 1); 386 if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &ebp)) 387 goto fail; 388 ebap = (daddr_t *)ebp->b_data; 389 } 390 /* 391 * Search the block map looking for an allocation of the desired size. 392 */ 393 if ((newblk = (daddr_t)ffs_hashalloc(ip, dtog(fs, pref), (long)pref, 394 len, (u_long (*)())ffs_clusteralloc)) == 0) 395 goto fail; 396 /* 397 * We have found a new contiguous block. 398 * 399 * First we have to replace the old block pointers with the new 400 * block pointers in the inode and indirect blocks associated 401 * with the file. 402 */ 403 blkno = newblk; 404 for (bap = &sbap[soff], i = 0; i < len; i++, blkno += fs->fs_frag) { 405 if (i == ssize) 406 bap = ebap; 407 #ifdef DIAGNOSTIC 408 if (buflist->bs_children[i]->b_blkno != fsbtodb(fs, *bap)) 409 panic("ffs_reallocblks: alloc mismatch"); 410 #endif 411 *bap++ = blkno; 412 } 413 /* 414 * Next we must write out the modified inode and indirect blocks. 415 * For strict correctness, the writes should be synchronous since 416 * the old block values may have been written to disk. In practise 417 * they are almost never written, but if we are concerned about 418 * strict correctness, the `doasyncfree' flag should be set to zero. 419 * 420 * The test on `doasyncfree' should be changed to test a flag 421 * that shows whether the associated buffers and inodes have 422 * been written. The flag should be set when the cluster is 423 * started and cleared whenever the buffer or inode is flushed. 424 * We can then check below to see if it is set, and do the 425 * synchronous write only when it has been cleared. 426 */ 427 if (sbap != &ip->i_db[0]) { 428 if (doasyncfree) 429 bdwrite(sbp); 430 else 431 bwrite(sbp); 432 } else { 433 ip->i_flag |= IN_CHANGE | IN_UPDATE; 434 if (!doasyncfree) 435 VOP_UPDATE(vp, &time, &time, MNT_WAIT); 436 } 437 if (ssize < len) 438 if (doasyncfree) 439 bdwrite(ebp); 440 else 441 bwrite(ebp); 442 /* 443 * Last, free the old blocks and assign the new blocks to the buffers. 444 */ 445 for (blkno = newblk, i = 0; i < len; i++, blkno += fs->fs_frag) { 446 ffs_blkfree(ip, dbtofsb(fs, buflist->bs_children[i]->b_blkno), 447 fs->fs_bsize); 448 buflist->bs_children[i]->b_blkno = fsbtodb(fs, blkno); 449 } 450 return (0); 451 452 fail: 453 if (ssize < len) 454 brelse(ebp); 455 if (sbap != &ip->i_db[0]) 456 brelse(sbp); 457 return (ENOSPC); 458 } 459 460 /* 461 * Allocate an inode in the file system. 462 * 463 * If allocating a directory, use ffs_dirpref to select the inode. 464 * If allocating in a directory, the following hierarchy is followed: 465 * 1) allocate the preferred inode. 466 * 2) allocate an inode in the same cylinder group. 467 * 3) quadradically rehash into other cylinder groups, until an 468 * available inode is located. 469 * If no inode preference is given the following heirarchy is used 470 * to allocate an inode: 471 * 1) allocate an inode in cylinder group 0. 472 * 2) quadradically rehash into other cylinder groups, until an 473 * available inode is located. 474 */ 475 int 476 ffs_valloc(ap) 477 struct vop_valloc_args /* { 478 struct vnode *a_pvp; 479 int a_mode; 480 struct ucred *a_cred; 481 struct vnode **a_vpp; 482 } */ *ap; 483 { 484 register struct vnode *pvp = ap->a_pvp; 485 register struct inode *pip; 486 register struct fs *fs; 487 register struct inode *ip; 488 mode_t mode = ap->a_mode; 489 ino_t ino, ipref; 490 int cg, error; 491 492 *ap->a_vpp = NULL; 493 pip = VTOI(pvp); 494 fs = pip->i_fs; 495 if (fs->fs_cstotal.cs_nifree == 0) 496 goto noinodes; 497 498 if ((mode & IFMT) == IFDIR) 499 ipref = ffs_dirpref(fs); 500 else 501 ipref = pip->i_number; 502 if (ipref >= fs->fs_ncg * fs->fs_ipg) 503 ipref = 0; 504 cg = ino_to_cg(fs, ipref); 505 ino = (ino_t)ffs_hashalloc(pip, cg, (long)ipref, mode, ffs_nodealloccg); 506 if (ino == 0) 507 goto noinodes; 508 error = VFS_VGET(pvp->v_mount, ino, ap->a_vpp); 509 if (error) { 510 VOP_VFREE(pvp, ino, mode); 511 return (error); 512 } 513 ip = VTOI(*ap->a_vpp); 514 if (ip->i_mode) { 515 printf("mode = 0%o, inum = %d, fs = %s\n", 516 ip->i_mode, ip->i_number, fs->fs_fsmnt); 517 panic("ffs_valloc: dup alloc"); 518 } 519 if (ip->i_blocks) { /* XXX */ 520 printf("free inode %s/%d had %d blocks\n", 521 fs->fs_fsmnt, ino, ip->i_blocks); 522 ip->i_blocks = 0; 523 } 524 ip->i_flags = 0; 525 /* 526 * Set up a new generation number for this inode. 527 */ 528 if (++nextgennumber < (u_long)time.tv_sec) 529 nextgennumber = time.tv_sec; 530 ip->i_gen = nextgennumber; 531 return (0); 532 noinodes: 533 ffs_fserr(fs, ap->a_cred->cr_uid, "out of inodes"); 534 uprintf("\n%s: create/symlink failed, no inodes free\n", fs->fs_fsmnt); 535 return (ENOSPC); 536 } 537 538 /* 539 * Find a cylinder to place a directory. 540 * 541 * The policy implemented by this algorithm is to select from 542 * among those cylinder groups with above the average number of 543 * free inodes, the one with the smallest number of directories. 544 */ 545 static ino_t 546 ffs_dirpref(fs) 547 register struct fs *fs; 548 { 549 int cg, minndir, mincg, avgifree; 550 551 avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg; 552 minndir = fs->fs_ipg; 553 mincg = 0; 554 for (cg = 0; cg < fs->fs_ncg; cg++) 555 if (fs->fs_cs(fs, cg).cs_ndir < minndir && 556 fs->fs_cs(fs, cg).cs_nifree >= avgifree) { 557 mincg = cg; 558 minndir = fs->fs_cs(fs, cg).cs_ndir; 559 } 560 return ((ino_t)(fs->fs_ipg * mincg)); 561 } 562 563 /* 564 * Select the desired position for the next block in a file. The file is 565 * logically divided into sections. The first section is composed of the 566 * direct blocks. Each additional section contains fs_maxbpg blocks. 567 * 568 * If no blocks have been allocated in the first section, the policy is to 569 * request a block in the same cylinder group as the inode that describes 570 * the file. If no blocks have been allocated in any other section, the 571 * policy is to place the section in a cylinder group with a greater than 572 * average number of free blocks. An appropriate cylinder group is found 573 * by using a rotor that sweeps the cylinder groups. When a new group of 574 * blocks is needed, the sweep begins in the cylinder group following the 575 * cylinder group from which the previous allocation was made. The sweep 576 * continues until a cylinder group with greater than the average number 577 * of free blocks is found. If the allocation is for the first block in an 578 * indirect block, the information on the previous allocation is unavailable; 579 * here a best guess is made based upon the logical block number being 580 * allocated. 581 * 582 * If a section is already partially allocated, the policy is to 583 * contiguously allocate fs_maxcontig blocks. The end of one of these 584 * contiguous blocks and the beginning of the next is physically separated 585 * so that the disk head will be in transit between them for at least 586 * fs_rotdelay milliseconds. This is to allow time for the processor to 587 * schedule another I/O transfer. 588 */ 589 daddr_t 590 ffs_blkpref(ip, lbn, indx, bap) 591 struct inode *ip; 592 daddr_t lbn; 593 int indx; 594 daddr_t *bap; 595 { 596 register struct fs *fs; 597 register int cg; 598 int avgbfree, startcg; 599 daddr_t nextblk; 600 601 fs = ip->i_fs; 602 if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) { 603 if (lbn < NDADDR) { 604 cg = ino_to_cg(fs, ip->i_number); 605 return (fs->fs_fpg * cg + fs->fs_frag); 606 } 607 /* 608 * Find a cylinder with greater than average number of 609 * unused data blocks. 610 */ 611 if (indx == 0 || bap[indx - 1] == 0) 612 startcg = 613 ino_to_cg(fs, ip->i_number) + lbn / fs->fs_maxbpg; 614 else 615 startcg = dtog(fs, bap[indx - 1]) + 1; 616 startcg %= fs->fs_ncg; 617 avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg; 618 for (cg = startcg; cg < fs->fs_ncg; cg++) 619 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { 620 fs->fs_cgrotor = cg; 621 return (fs->fs_fpg * cg + fs->fs_frag); 622 } 623 for (cg = 0; cg <= startcg; cg++) 624 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { 625 fs->fs_cgrotor = cg; 626 return (fs->fs_fpg * cg + fs->fs_frag); 627 } 628 return (NULL); 629 } 630 /* 631 * One or more previous blocks have been laid out. If less 632 * than fs_maxcontig previous blocks are contiguous, the 633 * next block is requested contiguously, otherwise it is 634 * requested rotationally delayed by fs_rotdelay milliseconds. 635 */ 636 nextblk = bap[indx - 1] + fs->fs_frag; 637 if (indx < fs->fs_maxcontig || bap[indx - fs->fs_maxcontig] + 638 blkstofrags(fs, fs->fs_maxcontig) != nextblk) 639 return (nextblk); 640 if (fs->fs_rotdelay != 0) 641 /* 642 * Here we convert ms of delay to frags as: 643 * (frags) = (ms) * (rev/sec) * (sect/rev) / 644 * ((sect/frag) * (ms/sec)) 645 * then round up to the next block. 646 */ 647 nextblk += roundup(fs->fs_rotdelay * fs->fs_rps * fs->fs_nsect / 648 (NSPF(fs) * 1000), fs->fs_frag); 649 return (nextblk); 650 } 651 652 /* 653 * Implement the cylinder overflow algorithm. 654 * 655 * The policy implemented by this algorithm is: 656 * 1) allocate the block in its requested cylinder group. 657 * 2) quadradically rehash on the cylinder group number. 658 * 3) brute force search for a free block. 659 */ 660 /*VARARGS5*/ 661 static u_long 662 ffs_hashalloc(ip, cg, pref, size, allocator) 663 struct inode *ip; 664 int cg; 665 long pref; 666 int size; /* size for data blocks, mode for inodes */ 667 u_long (*allocator)(); 668 { 669 register struct fs *fs; 670 long result; 671 int i, icg = cg; 672 673 fs = ip->i_fs; 674 /* 675 * 1: preferred cylinder group 676 */ 677 result = (*allocator)(ip, cg, pref, size); 678 if (result) 679 return (result); 680 /* 681 * 2: quadratic rehash 682 */ 683 for (i = 1; i < fs->fs_ncg; i *= 2) { 684 cg += i; 685 if (cg >= fs->fs_ncg) 686 cg -= fs->fs_ncg; 687 result = (*allocator)(ip, cg, 0, size); 688 if (result) 689 return (result); 690 } 691 /* 692 * 3: brute force search 693 * Note that we start at i == 2, since 0 was checked initially, 694 * and 1 is always checked in the quadratic rehash. 695 */ 696 cg = (icg + 2) % fs->fs_ncg; 697 for (i = 2; i < fs->fs_ncg; i++) { 698 result = (*allocator)(ip, cg, 0, size); 699 if (result) 700 return (result); 701 cg++; 702 if (cg == fs->fs_ncg) 703 cg = 0; 704 } 705 return (NULL); 706 } 707 708 /* 709 * Determine whether a fragment can be extended. 710 * 711 * Check to see if the necessary fragments are available, and 712 * if they are, allocate them. 713 */ 714 static daddr_t 715 ffs_fragextend(ip, cg, bprev, osize, nsize) 716 struct inode *ip; 717 int cg; 718 long bprev; 719 int osize, nsize; 720 { 721 register struct fs *fs; 722 register struct cg *cgp; 723 struct buf *bp; 724 long bno; 725 int frags, bbase; 726 int i, error; 727 728 fs = ip->i_fs; 729 if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize)) 730 return (NULL); 731 frags = numfrags(fs, nsize); 732 bbase = fragnum(fs, bprev); 733 if (bbase > fragnum(fs, (bprev + frags - 1))) { 734 /* cannot extend across a block boundary */ 735 return (NULL); 736 } 737 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 738 (int)fs->fs_cgsize, NOCRED, &bp); 739 if (error) { 740 brelse(bp); 741 return (NULL); 742 } 743 cgp = (struct cg *)bp->b_data; 744 if (!cg_chkmagic(cgp)) { 745 brelse(bp); 746 return (NULL); 747 } 748 cgp->cg_time = time.tv_sec; 749 bno = dtogd(fs, bprev); 750 for (i = numfrags(fs, osize); i < frags; i++) 751 if (isclr(cg_blksfree(cgp), bno + i)) { 752 brelse(bp); 753 return (NULL); 754 } 755 /* 756 * the current fragment can be extended 757 * deduct the count on fragment being extended into 758 * increase the count on the remaining fragment (if any) 759 * allocate the extended piece 760 */ 761 for (i = frags; i < fs->fs_frag - bbase; i++) 762 if (isclr(cg_blksfree(cgp), bno + i)) 763 break; 764 cgp->cg_frsum[i - numfrags(fs, osize)]--; 765 if (i != frags) 766 cgp->cg_frsum[i - frags]++; 767 for (i = numfrags(fs, osize); i < frags; i++) { 768 clrbit(cg_blksfree(cgp), bno + i); 769 cgp->cg_cs.cs_nffree--; 770 fs->fs_cstotal.cs_nffree--; 771 fs->fs_cs(fs, cg).cs_nffree--; 772 } 773 fs->fs_fmod = 1; 774 bdwrite(bp); 775 return (bprev); 776 } 777 778 /* 779 * Determine whether a block can be allocated. 780 * 781 * Check to see if a block of the appropriate size is available, 782 * and if it is, allocate it. 783 */ 784 static daddr_t 785 ffs_alloccg(ip, cg, bpref, size) 786 struct inode *ip; 787 int cg; 788 daddr_t bpref; 789 int size; 790 { 791 register struct fs *fs; 792 register struct cg *cgp; 793 struct buf *bp; 794 register int i; 795 int error, bno, frags, allocsiz; 796 797 fs = ip->i_fs; 798 if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize) 799 return (NULL); 800 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 801 (int)fs->fs_cgsize, NOCRED, &bp); 802 if (error) { 803 brelse(bp); 804 return (NULL); 805 } 806 cgp = (struct cg *)bp->b_data; 807 if (!cg_chkmagic(cgp) || 808 (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) { 809 brelse(bp); 810 return (NULL); 811 } 812 cgp->cg_time = time.tv_sec; 813 if (size == fs->fs_bsize) { 814 bno = ffs_alloccgblk(fs, cgp, bpref); 815 bdwrite(bp); 816 return (bno); 817 } 818 /* 819 * check to see if any fragments are already available 820 * allocsiz is the size which will be allocated, hacking 821 * it down to a smaller size if necessary 822 */ 823 frags = numfrags(fs, size); 824 for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++) 825 if (cgp->cg_frsum[allocsiz] != 0) 826 break; 827 if (allocsiz == fs->fs_frag) { 828 /* 829 * no fragments were available, so a block will be 830 * allocated, and hacked up 831 */ 832 if (cgp->cg_cs.cs_nbfree == 0) { 833 brelse(bp); 834 return (NULL); 835 } 836 bno = ffs_alloccgblk(fs, cgp, bpref); 837 bpref = dtogd(fs, bno); 838 for (i = frags; i < fs->fs_frag; i++) 839 setbit(cg_blksfree(cgp), bpref + i); 840 i = fs->fs_frag - frags; 841 cgp->cg_cs.cs_nffree += i; 842 fs->fs_cstotal.cs_nffree += i; 843 fs->fs_cs(fs, cg).cs_nffree += i; 844 fs->fs_fmod = 1; 845 cgp->cg_frsum[i]++; 846 bdwrite(bp); 847 return (bno); 848 } 849 bno = ffs_mapsearch(fs, cgp, bpref, allocsiz); 850 if (bno < 0) { 851 brelse(bp); 852 return (NULL); 853 } 854 for (i = 0; i < frags; i++) 855 clrbit(cg_blksfree(cgp), bno + i); 856 cgp->cg_cs.cs_nffree -= frags; 857 fs->fs_cstotal.cs_nffree -= frags; 858 fs->fs_cs(fs, cg).cs_nffree -= frags; 859 fs->fs_fmod = 1; 860 cgp->cg_frsum[allocsiz]--; 861 if (frags != allocsiz) 862 cgp->cg_frsum[allocsiz - frags]++; 863 bdwrite(bp); 864 return (cg * fs->fs_fpg + bno); 865 } 866 867 /* 868 * Allocate a block in a cylinder group. 869 * 870 * This algorithm implements the following policy: 871 * 1) allocate the requested block. 872 * 2) allocate a rotationally optimal block in the same cylinder. 873 * 3) allocate the next available block on the block rotor for the 874 * specified cylinder group. 875 * Note that this routine only allocates fs_bsize blocks; these 876 * blocks may be fragmented by the routine that allocates them. 877 */ 878 static daddr_t 879 ffs_alloccgblk(fs, cgp, bpref) 880 register struct fs *fs; 881 register struct cg *cgp; 882 daddr_t bpref; 883 { 884 daddr_t bno, blkno; 885 int cylno, pos, delta; 886 short *cylbp; 887 register int i; 888 889 if (bpref == 0 || dtog(fs, bpref) != cgp->cg_cgx) { 890 bpref = cgp->cg_rotor; 891 goto norot; 892 } 893 bpref = blknum(fs, bpref); 894 bpref = dtogd(fs, bpref); 895 /* 896 * if the requested block is available, use it 897 */ 898 if (ffs_isblock(fs, cg_blksfree(cgp), fragstoblks(fs, bpref))) { 899 bno = bpref; 900 goto gotit; 901 } 902 /* 903 * check for a block available on the same cylinder 904 */ 905 cylno = cbtocylno(fs, bpref); 906 if (cg_blktot(cgp)[cylno] == 0) 907 goto norot; 908 if (fs->fs_cpc == 0) { 909 /* 910 * Block layout information is not available. 911 * Leaving bpref unchanged means we take the 912 * next available free block following the one 913 * we just allocated. Hopefully this will at 914 * least hit a track cache on drives of unknown 915 * geometry (e.g. SCSI). 916 */ 917 goto norot; 918 } 919 /* 920 * check the summary information to see if a block is 921 * available in the requested cylinder starting at the 922 * requested rotational position and proceeding around. 923 */ 924 cylbp = cg_blks(fs, cgp, cylno); 925 pos = cbtorpos(fs, bpref); 926 for (i = pos; i < fs->fs_nrpos; i++) 927 if (cylbp[i] > 0) 928 break; 929 if (i == fs->fs_nrpos) 930 for (i = 0; i < pos; i++) 931 if (cylbp[i] > 0) 932 break; 933 if (cylbp[i] > 0) { 934 /* 935 * found a rotational position, now find the actual 936 * block. A panic if none is actually there. 937 */ 938 pos = cylno % fs->fs_cpc; 939 bno = (cylno - pos) * fs->fs_spc / NSPB(fs); 940 if (fs_postbl(fs, pos)[i] == -1) { 941 printf("pos = %d, i = %d, fs = %s\n", 942 pos, i, fs->fs_fsmnt); 943 panic("ffs_alloccgblk: cyl groups corrupted"); 944 } 945 for (i = fs_postbl(fs, pos)[i];; ) { 946 if (ffs_isblock(fs, cg_blksfree(cgp), bno + i)) { 947 bno = blkstofrags(fs, (bno + i)); 948 goto gotit; 949 } 950 delta = fs_rotbl(fs)[i]; 951 if (delta <= 0 || 952 delta + i > fragstoblks(fs, fs->fs_fpg)) 953 break; 954 i += delta; 955 } 956 printf("pos = %d, i = %d, fs = %s\n", pos, i, fs->fs_fsmnt); 957 panic("ffs_alloccgblk: can't find blk in cyl"); 958 } 959 norot: 960 /* 961 * no blocks in the requested cylinder, so take next 962 * available one in this cylinder group. 963 */ 964 bno = ffs_mapsearch(fs, cgp, bpref, (int)fs->fs_frag); 965 if (bno < 0) 966 return (NULL); 967 cgp->cg_rotor = bno; 968 gotit: 969 blkno = fragstoblks(fs, bno); 970 ffs_clrblock(fs, cg_blksfree(cgp), (long)blkno); 971 ffs_clusteracct(fs, cgp, blkno, -1); 972 cgp->cg_cs.cs_nbfree--; 973 fs->fs_cstotal.cs_nbfree--; 974 fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--; 975 cylno = cbtocylno(fs, bno); 976 cg_blks(fs, cgp, cylno)[cbtorpos(fs, bno)]--; 977 cg_blktot(cgp)[cylno]--; 978 fs->fs_fmod = 1; 979 return (cgp->cg_cgx * fs->fs_fpg + bno); 980 } 981 982 /* 983 * Determine whether a cluster can be allocated. 984 * 985 * We do not currently check for optimal rotational layout if there 986 * are multiple choices in the same cylinder group. Instead we just 987 * take the first one that we find following bpref. 988 */ 989 static daddr_t 990 ffs_clusteralloc(ip, cg, bpref, len) 991 struct inode *ip; 992 int cg; 993 daddr_t bpref; 994 int len; 995 { 996 register struct fs *fs; 997 register struct cg *cgp; 998 struct buf *bp; 999 int i, run, bno, bit, map; 1000 u_char *mapp; 1001 1002 fs = ip->i_fs; 1003 if (fs->fs_cs(fs, cg).cs_nbfree < len) 1004 return (NULL); 1005 if (bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize, 1006 NOCRED, &bp)) 1007 goto fail; 1008 cgp = (struct cg *)bp->b_data; 1009 if (!cg_chkmagic(cgp)) 1010 goto fail; 1011 /* 1012 * Check to see if a cluster of the needed size (or bigger) is 1013 * available in this cylinder group. 1014 */ 1015 for (i = len; i <= fs->fs_contigsumsize; i++) 1016 if (cg_clustersum(cgp)[i] > 0) 1017 break; 1018 if (i > fs->fs_contigsumsize) 1019 goto fail; 1020 /* 1021 * Search the cluster map to find a big enough cluster. 1022 * We take the first one that we find, even if it is larger 1023 * than we need as we prefer to get one close to the previous 1024 * block allocation. We do not search before the current 1025 * preference point as we do not want to allocate a block 1026 * that is allocated before the previous one (as we will 1027 * then have to wait for another pass of the elevator 1028 * algorithm before it will be read). We prefer to fail and 1029 * be recalled to try an allocation in the next cylinder group. 1030 */ 1031 if (dtog(fs, bpref) != cg) 1032 bpref = 0; 1033 else 1034 bpref = fragstoblks(fs, dtogd(fs, blknum(fs, bpref))); 1035 mapp = &cg_clustersfree(cgp)[bpref / NBBY]; 1036 map = *mapp++; 1037 bit = 1 << (bpref % NBBY); 1038 for (run = 0, i = bpref; i < cgp->cg_nclusterblks; i++) { 1039 if ((map & bit) == 0) { 1040 run = 0; 1041 } else { 1042 run++; 1043 if (run == len) 1044 break; 1045 } 1046 if ((i & (NBBY - 1)) != (NBBY - 1)) { 1047 bit <<= 1; 1048 } else { 1049 map = *mapp++; 1050 bit = 1; 1051 } 1052 } 1053 if (i == cgp->cg_nclusterblks) 1054 goto fail; 1055 /* 1056 * Allocate the cluster that we have found. 1057 */ 1058 bno = cg * fs->fs_fpg + blkstofrags(fs, i - run + 1); 1059 len = blkstofrags(fs, len); 1060 for (i = 0; i < len; i += fs->fs_frag) 1061 if (ffs_alloccgblk(fs, cgp, bno + i) != bno + i) 1062 panic("ffs_clusteralloc: lost block"); 1063 brelse(bp); 1064 return (bno); 1065 1066 fail: 1067 brelse(bp); 1068 return (0); 1069 } 1070 1071 /* 1072 * Determine whether an inode can be allocated. 1073 * 1074 * Check to see if an inode is available, and if it is, 1075 * allocate it using the following policy: 1076 * 1) allocate the requested inode. 1077 * 2) allocate the next available inode after the requested 1078 * inode in the specified cylinder group. 1079 */ 1080 static ino_t 1081 ffs_nodealloccg(ip, cg, ipref, mode) 1082 struct inode *ip; 1083 int cg; 1084 daddr_t ipref; 1085 int mode; 1086 { 1087 register struct fs *fs; 1088 register struct cg *cgp; 1089 struct buf *bp; 1090 int error, start, len, loc, map, i; 1091 1092 fs = ip->i_fs; 1093 if (fs->fs_cs(fs, cg).cs_nifree == 0) 1094 return (NULL); 1095 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 1096 (int)fs->fs_cgsize, NOCRED, &bp); 1097 if (error) { 1098 brelse(bp); 1099 return (NULL); 1100 } 1101 cgp = (struct cg *)bp->b_data; 1102 if (!cg_chkmagic(cgp) || cgp->cg_cs.cs_nifree == 0) { 1103 brelse(bp); 1104 return (NULL); 1105 } 1106 cgp->cg_time = time.tv_sec; 1107 if (ipref) { 1108 ipref %= fs->fs_ipg; 1109 if (isclr(cg_inosused(cgp), ipref)) 1110 goto gotit; 1111 } 1112 start = cgp->cg_irotor / NBBY; 1113 len = howmany(fs->fs_ipg - cgp->cg_irotor, NBBY); 1114 loc = skpc(0xff, len, &cg_inosused(cgp)[start]); 1115 if (loc == 0) { 1116 len = start + 1; 1117 start = 0; 1118 loc = skpc(0xff, len, &cg_inosused(cgp)[0]); 1119 if (loc == 0) { 1120 printf("cg = %d, irotor = %d, fs = %s\n", 1121 cg, cgp->cg_irotor, fs->fs_fsmnt); 1122 panic("ffs_nodealloccg: map corrupted"); 1123 /* NOTREACHED */ 1124 } 1125 } 1126 i = start + len - loc; 1127 map = cg_inosused(cgp)[i]; 1128 ipref = i * NBBY; 1129 for (i = 1; i < (1 << NBBY); i <<= 1, ipref++) { 1130 if ((map & i) == 0) { 1131 cgp->cg_irotor = ipref; 1132 goto gotit; 1133 } 1134 } 1135 printf("fs = %s\n", fs->fs_fsmnt); 1136 panic("ffs_nodealloccg: block not in map"); 1137 /* NOTREACHED */ 1138 gotit: 1139 setbit(cg_inosused(cgp), ipref); 1140 cgp->cg_cs.cs_nifree--; 1141 fs->fs_cstotal.cs_nifree--; 1142 fs->fs_cs(fs, cg).cs_nifree--; 1143 fs->fs_fmod = 1; 1144 if ((mode & IFMT) == IFDIR) { 1145 cgp->cg_cs.cs_ndir++; 1146 fs->fs_cstotal.cs_ndir++; 1147 fs->fs_cs(fs, cg).cs_ndir++; 1148 } 1149 bdwrite(bp); 1150 return (cg * fs->fs_ipg + ipref); 1151 } 1152 1153 /* 1154 * Free a block or fragment. 1155 * 1156 * The specified block or fragment is placed back in the 1157 * free map. If a fragment is deallocated, a possible 1158 * block reassembly is checked. 1159 */ 1160 void 1161 ffs_blkfree(ip, bno, size) 1162 register struct inode *ip; 1163 daddr_t bno; 1164 long size; 1165 { 1166 register struct fs *fs; 1167 register struct cg *cgp; 1168 struct buf *bp; 1169 daddr_t blkno; 1170 int i, error, cg, blk, frags, bbase; 1171 1172 fs = ip->i_fs; 1173 if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0) { 1174 printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n", 1175 ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt); 1176 panic("blkfree: bad size"); 1177 } 1178 cg = dtog(fs, bno); 1179 if ((u_int)bno >= fs->fs_size) { 1180 printf("bad block %d, ino %d\n", bno, ip->i_number); 1181 ffs_fserr(fs, ip->i_uid, "bad block"); 1182 return; 1183 } 1184 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 1185 (int)fs->fs_cgsize, NOCRED, &bp); 1186 if (error) { 1187 brelse(bp); 1188 return; 1189 } 1190 cgp = (struct cg *)bp->b_data; 1191 if (!cg_chkmagic(cgp)) { 1192 brelse(bp); 1193 return; 1194 } 1195 cgp->cg_time = time.tv_sec; 1196 bno = dtogd(fs, bno); 1197 if (size == fs->fs_bsize) { 1198 blkno = fragstoblks(fs, bno); 1199 if (ffs_isblock(fs, cg_blksfree(cgp), blkno)) { 1200 printf("dev = 0x%x, block = %d, fs = %s\n", 1201 ip->i_dev, bno, fs->fs_fsmnt); 1202 panic("blkfree: freeing free block"); 1203 } 1204 ffs_setblock(fs, cg_blksfree(cgp), blkno); 1205 ffs_clusteracct(fs, cgp, blkno, 1); 1206 cgp->cg_cs.cs_nbfree++; 1207 fs->fs_cstotal.cs_nbfree++; 1208 fs->fs_cs(fs, cg).cs_nbfree++; 1209 i = cbtocylno(fs, bno); 1210 cg_blks(fs, cgp, i)[cbtorpos(fs, bno)]++; 1211 cg_blktot(cgp)[i]++; 1212 } else { 1213 bbase = bno - fragnum(fs, bno); 1214 /* 1215 * decrement the counts associated with the old frags 1216 */ 1217 blk = blkmap(fs, cg_blksfree(cgp), bbase); 1218 ffs_fragacct(fs, blk, cgp->cg_frsum, -1); 1219 /* 1220 * deallocate the fragment 1221 */ 1222 frags = numfrags(fs, size); 1223 for (i = 0; i < frags; i++) { 1224 if (isset(cg_blksfree(cgp), bno + i)) { 1225 printf("dev = 0x%x, block = %d, fs = %s\n", 1226 ip->i_dev, bno + i, fs->fs_fsmnt); 1227 panic("blkfree: freeing free frag"); 1228 } 1229 setbit(cg_blksfree(cgp), bno + i); 1230 } 1231 cgp->cg_cs.cs_nffree += i; 1232 fs->fs_cstotal.cs_nffree += i; 1233 fs->fs_cs(fs, cg).cs_nffree += i; 1234 /* 1235 * add back in counts associated with the new frags 1236 */ 1237 blk = blkmap(fs, cg_blksfree(cgp), bbase); 1238 ffs_fragacct(fs, blk, cgp->cg_frsum, 1); 1239 /* 1240 * if a complete block has been reassembled, account for it 1241 */ 1242 blkno = fragstoblks(fs, bbase); 1243 if (ffs_isblock(fs, cg_blksfree(cgp), blkno)) { 1244 cgp->cg_cs.cs_nffree -= fs->fs_frag; 1245 fs->fs_cstotal.cs_nffree -= fs->fs_frag; 1246 fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag; 1247 ffs_clusteracct(fs, cgp, blkno, 1); 1248 cgp->cg_cs.cs_nbfree++; 1249 fs->fs_cstotal.cs_nbfree++; 1250 fs->fs_cs(fs, cg).cs_nbfree++; 1251 i = cbtocylno(fs, bbase); 1252 cg_blks(fs, cgp, i)[cbtorpos(fs, bbase)]++; 1253 cg_blktot(cgp)[i]++; 1254 } 1255 } 1256 fs->fs_fmod = 1; 1257 bdwrite(bp); 1258 } 1259 1260 /* 1261 * Free an inode. 1262 * 1263 * The specified inode is placed back in the free map. 1264 */ 1265 int 1266 ffs_vfree(ap) 1267 struct vop_vfree_args /* { 1268 struct vnode *a_pvp; 1269 ino_t a_ino; 1270 int a_mode; 1271 } */ *ap; 1272 { 1273 register struct fs *fs; 1274 register struct cg *cgp; 1275 register struct inode *pip; 1276 ino_t ino = ap->a_ino; 1277 struct buf *bp; 1278 int error, cg; 1279 1280 pip = VTOI(ap->a_pvp); 1281 fs = pip->i_fs; 1282 if ((u_int)ino >= fs->fs_ipg * fs->fs_ncg) 1283 panic("ifree: range: dev = 0x%x, ino = %d, fs = %s\n", 1284 pip->i_dev, ino, fs->fs_fsmnt); 1285 cg = ino_to_cg(fs, ino); 1286 error = bread(pip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 1287 (int)fs->fs_cgsize, NOCRED, &bp); 1288 if (error) { 1289 brelse(bp); 1290 return (0); 1291 } 1292 cgp = (struct cg *)bp->b_data; 1293 if (!cg_chkmagic(cgp)) { 1294 brelse(bp); 1295 return (0); 1296 } 1297 cgp->cg_time = time.tv_sec; 1298 ino %= fs->fs_ipg; 1299 if (isclr(cg_inosused(cgp), ino)) { 1300 printf("dev = 0x%x, ino = %d, fs = %s\n", 1301 pip->i_dev, ino, fs->fs_fsmnt); 1302 if (fs->fs_ronly == 0) 1303 panic("ifree: freeing free inode"); 1304 } 1305 clrbit(cg_inosused(cgp), ino); 1306 if (ino < cgp->cg_irotor) 1307 cgp->cg_irotor = ino; 1308 cgp->cg_cs.cs_nifree++; 1309 fs->fs_cstotal.cs_nifree++; 1310 fs->fs_cs(fs, cg).cs_nifree++; 1311 if ((ap->a_mode & IFMT) == IFDIR) { 1312 cgp->cg_cs.cs_ndir--; 1313 fs->fs_cstotal.cs_ndir--; 1314 fs->fs_cs(fs, cg).cs_ndir--; 1315 } 1316 fs->fs_fmod = 1; 1317 bdwrite(bp); 1318 return (0); 1319 } 1320 1321 /* 1322 * Find a block of the specified size in the specified cylinder group. 1323 * 1324 * It is a panic if a request is made to find a block if none are 1325 * available. 1326 */ 1327 static daddr_t 1328 ffs_mapsearch(fs, cgp, bpref, allocsiz) 1329 register struct fs *fs; 1330 register struct cg *cgp; 1331 daddr_t bpref; 1332 int allocsiz; 1333 { 1334 daddr_t bno; 1335 int start, len, loc, i; 1336 int blk, field, subfield, pos; 1337 1338 /* 1339 * find the fragment by searching through the free block 1340 * map for an appropriate bit pattern 1341 */ 1342 if (bpref) 1343 start = dtogd(fs, bpref) / NBBY; 1344 else 1345 start = cgp->cg_frotor / NBBY; 1346 len = howmany(fs->fs_fpg, NBBY) - start; 1347 loc = scanc((u_int)len, (u_char *)&cg_blksfree(cgp)[start], 1348 (u_char *)fragtbl[fs->fs_frag], 1349 (u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY)))); 1350 if (loc == 0) { 1351 len = start + 1; 1352 start = 0; 1353 loc = scanc((u_int)len, (u_char *)&cg_blksfree(cgp)[0], 1354 (u_char *)fragtbl[fs->fs_frag], 1355 (u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY)))); 1356 if (loc == 0) { 1357 printf("start = %d, len = %d, fs = %s\n", 1358 start, len, fs->fs_fsmnt); 1359 panic("ffs_alloccg: map corrupted"); 1360 /* NOTREACHED */ 1361 } 1362 } 1363 bno = (start + len - loc) * NBBY; 1364 cgp->cg_frotor = bno; 1365 /* 1366 * found the byte in the map 1367 * sift through the bits to find the selected frag 1368 */ 1369 for (i = bno + NBBY; bno < i; bno += fs->fs_frag) { 1370 blk = blkmap(fs, cg_blksfree(cgp), bno); 1371 blk <<= 1; 1372 field = around[allocsiz]; 1373 subfield = inside[allocsiz]; 1374 for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) { 1375 if ((blk & field) == subfield) 1376 return (bno + pos); 1377 field <<= 1; 1378 subfield <<= 1; 1379 } 1380 } 1381 printf("bno = %d, fs = %s\n", bno, fs->fs_fsmnt); 1382 panic("ffs_alloccg: block not in map"); 1383 return (-1); 1384 } 1385 1386 /* 1387 * Update the cluster map because of an allocation or free. 1388 * 1389 * Cnt == 1 means free; cnt == -1 means allocating. 1390 */ 1391 void 1392 ffs_clusteracct(fs, cgp, blkno, cnt) 1393 struct fs *fs; 1394 struct cg *cgp; 1395 daddr_t blkno; 1396 int cnt; 1397 { 1398 long *sump; 1399 u_char *freemapp, *mapp; 1400 int i, start, end, forw, back, map, bit; 1401 1402 if (fs->fs_contigsumsize <= 0) 1403 return; 1404 freemapp = cg_clustersfree(cgp); 1405 sump = cg_clustersum(cgp); 1406 /* 1407 * Allocate or clear the actual block. 1408 */ 1409 if (cnt > 0) 1410 setbit(freemapp, blkno); 1411 else 1412 clrbit(freemapp, blkno); 1413 /* 1414 * Find the size of the cluster going forward. 1415 */ 1416 start = blkno + 1; 1417 end = start + fs->fs_contigsumsize; 1418 if (end >= cgp->cg_nclusterblks) 1419 end = cgp->cg_nclusterblks; 1420 mapp = &freemapp[start / NBBY]; 1421 map = *mapp++; 1422 bit = 1 << (start % NBBY); 1423 for (i = start; i < end; i++) { 1424 if ((map & bit) == 0) 1425 break; 1426 if ((i & (NBBY - 1)) != (NBBY - 1)) { 1427 bit <<= 1; 1428 } else { 1429 map = *mapp++; 1430 bit = 1; 1431 } 1432 } 1433 forw = i - start; 1434 /* 1435 * Find the size of the cluster going backward. 1436 */ 1437 start = blkno - 1; 1438 end = start - fs->fs_contigsumsize; 1439 if (end < 0) 1440 end = -1; 1441 mapp = &freemapp[start / NBBY]; 1442 map = *mapp--; 1443 bit = 1 << (start % NBBY); 1444 for (i = start; i > end; i--) { 1445 if ((map & bit) == 0) 1446 break; 1447 if ((i & (NBBY - 1)) != 0) { 1448 bit >>= 1; 1449 } else { 1450 map = *mapp--; 1451 bit = 1 << (NBBY - 1); 1452 } 1453 } 1454 back = start - i; 1455 /* 1456 * Account for old cluster and the possibly new forward and 1457 * back clusters. 1458 */ 1459 i = back + forw + 1; 1460 if (i > fs->fs_contigsumsize) 1461 i = fs->fs_contigsumsize; 1462 sump[i] += cnt; 1463 if (back > 0) 1464 sump[back] -= cnt; 1465 if (forw > 0) 1466 sump[forw] -= cnt; 1467 } 1468 1469 /* 1470 * Fserr prints the name of a file system with an error diagnostic. 1471 * 1472 * The form of the error message is: 1473 * fs: error message 1474 */ 1475 static void 1476 ffs_fserr(fs, uid, cp) 1477 struct fs *fs; 1478 u_int uid; 1479 char *cp; 1480 { 1481 1482 log(LOG_ERR, "uid %d on %s: %s\n", uid, fs->fs_fsmnt, cp); 1483 } 1484