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