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.18 (Berkeley) 5/26/95 34 * $FreeBSD$ 35 */ 36 37 #include "opt_quota.h" 38 39 #include <sys/param.h> 40 #include <sys/systm.h> 41 #include <sys/bio.h> 42 #include <sys/buf.h> 43 #include <sys/conf.h> 44 #include <sys/file.h> 45 #include <sys/proc.h> 46 #include <sys/vnode.h> 47 #include <sys/mount.h> 48 #include <sys/kernel.h> 49 #include <sys/sysctl.h> 50 #include <sys/syslog.h> 51 52 #include <ufs/ufs/extattr.h> 53 #include <ufs/ufs/quota.h> 54 #include <ufs/ufs/inode.h> 55 #include <ufs/ufs/ufs_extern.h> 56 #include <ufs/ufs/ufsmount.h> 57 58 #include <ufs/ffs/fs.h> 59 #include <ufs/ffs/ffs_extern.h> 60 61 typedef ufs_daddr_t allocfcn_t __P((struct inode *ip, int cg, ufs_daddr_t bpref, 62 int size)); 63 64 static ufs_daddr_t ffs_alloccg __P((struct inode *, int, ufs_daddr_t, int)); 65 static ufs_daddr_t 66 ffs_alloccgblk __P((struct inode *, struct buf *, ufs_daddr_t)); 67 #ifdef DIAGNOSTIC 68 static int ffs_checkblk __P((struct inode *, ufs_daddr_t, long)); 69 #endif 70 static ufs_daddr_t ffs_clusteralloc __P((struct inode *, int, ufs_daddr_t, 71 int)); 72 static ino_t ffs_dirpref __P((struct inode *)); 73 static ufs_daddr_t ffs_fragextend __P((struct inode *, int, long, int, int)); 74 static void ffs_fserr __P((struct fs *, ino_t, char *)); 75 static u_long ffs_hashalloc 76 __P((struct inode *, int, long, int, allocfcn_t *)); 77 static ino_t ffs_nodealloccg __P((struct inode *, int, ufs_daddr_t, int)); 78 static ufs_daddr_t ffs_mapsearch __P((struct fs *, struct cg *, ufs_daddr_t, 79 int)); 80 81 /* 82 * Allocate a block in the file system. 83 * 84 * The size of the requested block is given, which must be some 85 * multiple of fs_fsize and <= fs_bsize. 86 * A preference may be optionally specified. If a preference is given 87 * the following hierarchy is used to allocate a block: 88 * 1) allocate the requested block. 89 * 2) allocate a rotationally optimal block in the same cylinder. 90 * 3) allocate a block in the same cylinder group. 91 * 4) quadradically rehash into other cylinder groups, until an 92 * available block is located. 93 * If no block preference is given the following heirarchy is used 94 * to allocate a block: 95 * 1) allocate a block in the cylinder group that contains the 96 * inode for the file. 97 * 2) quadradically rehash into other cylinder groups, until an 98 * available block is located. 99 */ 100 int 101 ffs_alloc(ip, lbn, bpref, size, cred, bnp) 102 register struct inode *ip; 103 ufs_daddr_t lbn, bpref; 104 int size; 105 struct ucred *cred; 106 ufs_daddr_t *bnp; 107 { 108 register struct fs *fs; 109 ufs_daddr_t bno; 110 int cg, reclaimed; 111 #ifdef QUOTA 112 int error; 113 #endif 114 115 *bnp = 0; 116 fs = ip->i_fs; 117 #ifdef DIAGNOSTIC 118 if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0) { 119 printf("dev = %s, bsize = %ld, size = %d, fs = %s\n", 120 devtoname(ip->i_dev), (long)fs->fs_bsize, size, 121 fs->fs_fsmnt); 122 panic("ffs_alloc: bad size"); 123 } 124 if (cred == NOCRED) 125 panic("ffs_alloc: missing credential"); 126 #endif /* DIAGNOSTIC */ 127 reclaimed = 0; 128 retry: 129 if (size == fs->fs_bsize && fs->fs_cstotal.cs_nbfree == 0) 130 goto nospace; 131 if (suser_xxx(cred, NULL, PRISON_ROOT) && 132 freespace(fs, fs->fs_minfree) - numfrags(fs, size) < 0) 133 goto nospace; 134 #ifdef QUOTA 135 error = chkdq(ip, (long)btodb(size), cred, 0); 136 if (error) 137 return (error); 138 #endif 139 if (bpref >= fs->fs_size) 140 bpref = 0; 141 if (bpref == 0) 142 cg = ino_to_cg(fs, ip->i_number); 143 else 144 cg = dtog(fs, bpref); 145 bno = (ufs_daddr_t)ffs_hashalloc(ip, cg, (long)bpref, size, 146 ffs_alloccg); 147 if (bno > 0) { 148 ip->i_blocks += btodb(size); 149 ip->i_flag |= IN_CHANGE | IN_UPDATE; 150 *bnp = bno; 151 return (0); 152 } 153 #ifdef QUOTA 154 /* 155 * Restore user's disk quota because allocation failed. 156 */ 157 (void) chkdq(ip, (long)-btodb(size), cred, FORCE); 158 #endif 159 nospace: 160 if (fs->fs_pendingblocks > 0 && reclaimed == 0) { 161 reclaimed = 1; 162 softdep_request_cleanup(fs, ITOV(ip)); 163 goto retry; 164 } 165 ffs_fserr(fs, ip->i_number, "file system full"); 166 uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt); 167 return (ENOSPC); 168 } 169 170 /* 171 * Reallocate a fragment to a bigger size 172 * 173 * The number and size of the old block is given, and a preference 174 * and new size is also specified. The allocator attempts to extend 175 * the original block. Failing that, the regular block allocator is 176 * invoked to get an appropriate block. 177 */ 178 int 179 ffs_realloccg(ip, lbprev, bpref, osize, nsize, cred, bpp) 180 register struct inode *ip; 181 ufs_daddr_t lbprev; 182 ufs_daddr_t bpref; 183 int osize, nsize; 184 struct ucred *cred; 185 struct buf **bpp; 186 { 187 struct vnode *vp; 188 struct fs *fs; 189 struct buf *bp; 190 int cg, request, error, reclaimed; 191 ufs_daddr_t bprev, bno; 192 193 *bpp = 0; 194 vp = ITOV(ip); 195 fs = ip->i_fs; 196 #ifdef DIAGNOSTIC 197 if (vp->v_mount->mnt_kern_flag & MNTK_SUSPENDED) 198 panic("ffs_realloccg: allocation on suspended filesystem"); 199 if ((u_int)osize > fs->fs_bsize || fragoff(fs, osize) != 0 || 200 (u_int)nsize > fs->fs_bsize || fragoff(fs, nsize) != 0) { 201 printf( 202 "dev = %s, bsize = %ld, osize = %d, nsize = %d, fs = %s\n", 203 devtoname(ip->i_dev), (long)fs->fs_bsize, osize, 204 nsize, fs->fs_fsmnt); 205 panic("ffs_realloccg: bad size"); 206 } 207 if (cred == NOCRED) 208 panic("ffs_realloccg: missing credential"); 209 #endif /* DIAGNOSTIC */ 210 reclaimed = 0; 211 retry: 212 if (suser_xxx(cred, NULL, PRISON_ROOT) && 213 freespace(fs, fs->fs_minfree) - numfrags(fs, nsize - osize) < 0) 214 goto nospace; 215 if ((bprev = ip->i_db[lbprev]) == 0) { 216 printf("dev = %s, bsize = %ld, bprev = %ld, fs = %s\n", 217 devtoname(ip->i_dev), (long)fs->fs_bsize, (long)bprev, 218 fs->fs_fsmnt); 219 panic("ffs_realloccg: bad bprev"); 220 } 221 /* 222 * Allocate the extra space in the buffer. 223 */ 224 error = bread(vp, lbprev, osize, NOCRED, &bp); 225 if (error) { 226 brelse(bp); 227 return (error); 228 } 229 230 if( bp->b_blkno == bp->b_lblkno) { 231 if( lbprev >= NDADDR) 232 panic("ffs_realloccg: lbprev out of range"); 233 bp->b_blkno = fsbtodb(fs, bprev); 234 } 235 236 #ifdef QUOTA 237 error = chkdq(ip, (long)btodb(nsize - osize), cred, 0); 238 if (error) { 239 brelse(bp); 240 return (error); 241 } 242 #endif 243 /* 244 * Check for extension in the existing location. 245 */ 246 cg = dtog(fs, bprev); 247 bno = ffs_fragextend(ip, cg, (long)bprev, osize, nsize); 248 if (bno) { 249 if (bp->b_blkno != fsbtodb(fs, bno)) 250 panic("ffs_realloccg: bad blockno"); 251 ip->i_blocks += btodb(nsize - osize); 252 ip->i_flag |= IN_CHANGE | IN_UPDATE; 253 allocbuf(bp, nsize); 254 bp->b_flags |= B_DONE; 255 bzero((char *)bp->b_data + osize, (u_int)nsize - osize); 256 *bpp = bp; 257 return (0); 258 } 259 /* 260 * Allocate a new disk location. 261 */ 262 if (bpref >= fs->fs_size) 263 bpref = 0; 264 switch ((int)fs->fs_optim) { 265 case FS_OPTSPACE: 266 /* 267 * Allocate an exact sized fragment. Although this makes 268 * best use of space, we will waste time relocating it if 269 * the file continues to grow. If the fragmentation is 270 * less than half of the minimum free reserve, we choose 271 * to begin optimizing for time. 272 */ 273 request = nsize; 274 if (fs->fs_minfree <= 5 || 275 fs->fs_cstotal.cs_nffree > 276 (off_t)fs->fs_dsize * fs->fs_minfree / (2 * 100)) 277 break; 278 log(LOG_NOTICE, "%s: optimization changed from SPACE to TIME\n", 279 fs->fs_fsmnt); 280 fs->fs_optim = FS_OPTTIME; 281 break; 282 case FS_OPTTIME: 283 /* 284 * At this point we have discovered a file that is trying to 285 * grow a small fragment to a larger fragment. To save time, 286 * we allocate a full sized block, then free the unused portion. 287 * If the file continues to grow, the `ffs_fragextend' call 288 * above will be able to grow it in place without further 289 * copying. If aberrant programs cause disk fragmentation to 290 * grow within 2% of the free reserve, we choose to begin 291 * optimizing for space. 292 */ 293 request = fs->fs_bsize; 294 if (fs->fs_cstotal.cs_nffree < 295 (off_t)fs->fs_dsize * (fs->fs_minfree - 2) / 100) 296 break; 297 log(LOG_NOTICE, "%s: optimization changed from TIME to SPACE\n", 298 fs->fs_fsmnt); 299 fs->fs_optim = FS_OPTSPACE; 300 break; 301 default: 302 printf("dev = %s, optim = %ld, fs = %s\n", 303 devtoname(ip->i_dev), (long)fs->fs_optim, fs->fs_fsmnt); 304 panic("ffs_realloccg: bad optim"); 305 /* NOTREACHED */ 306 } 307 bno = (ufs_daddr_t)ffs_hashalloc(ip, cg, (long)bpref, request, 308 ffs_alloccg); 309 if (bno > 0) { 310 bp->b_blkno = fsbtodb(fs, bno); 311 if (!DOINGSOFTDEP(vp)) 312 ffs_blkfree(fs, ip->i_devvp, bprev, (long)osize, 313 ip->i_number); 314 if (nsize < request) 315 ffs_blkfree(fs, ip->i_devvp, bno + numfrags(fs, nsize), 316 (long)(request - nsize), ip->i_number); 317 ip->i_blocks += btodb(nsize - osize); 318 ip->i_flag |= IN_CHANGE | IN_UPDATE; 319 allocbuf(bp, nsize); 320 bp->b_flags |= B_DONE; 321 bzero((char *)bp->b_data + osize, (u_int)nsize - osize); 322 *bpp = bp; 323 return (0); 324 } 325 #ifdef QUOTA 326 /* 327 * Restore user's disk quota because allocation failed. 328 */ 329 (void) chkdq(ip, (long)-btodb(nsize - osize), cred, FORCE); 330 #endif 331 brelse(bp); 332 nospace: 333 /* 334 * no space available 335 */ 336 if (fs->fs_pendingblocks > 0 && reclaimed == 0) { 337 reclaimed = 1; 338 softdep_request_cleanup(fs, vp); 339 goto retry; 340 } 341 ffs_fserr(fs, ip->i_number, "file system full"); 342 uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt); 343 return (ENOSPC); 344 } 345 346 /* 347 * Reallocate a sequence of blocks into a contiguous sequence of blocks. 348 * 349 * The vnode and an array of buffer pointers for a range of sequential 350 * logical blocks to be made contiguous is given. The allocator attempts 351 * to find a range of sequential blocks starting as close as possible to 352 * an fs_rotdelay offset from the end of the allocation for the logical 353 * block immediately preceding the current range. If successful, the 354 * physical block numbers in the buffer pointers and in the inode are 355 * changed to reflect the new allocation. If unsuccessful, the allocation 356 * is left unchanged. The success in doing the reallocation is returned. 357 * Note that the error return is not reflected back to the user. Rather 358 * the previous block allocation will be used. 359 */ 360 361 SYSCTL_NODE(_vfs, OID_AUTO, ffs, CTLFLAG_RW, 0, "FFS filesystem"); 362 363 static int doasyncfree = 1; 364 SYSCTL_INT(_vfs_ffs, OID_AUTO, doasyncfree, CTLFLAG_RW, &doasyncfree, 0, ""); 365 366 static int doreallocblks = 1; 367 SYSCTL_INT(_vfs_ffs, OID_AUTO, doreallocblks, CTLFLAG_RW, &doreallocblks, 0, ""); 368 369 #ifdef DEBUG 370 static volatile int prtrealloc = 0; 371 #endif 372 373 int 374 ffs_reallocblks(ap) 375 struct vop_reallocblks_args /* { 376 struct vnode *a_vp; 377 struct cluster_save *a_buflist; 378 } */ *ap; 379 { 380 struct fs *fs; 381 struct inode *ip; 382 struct vnode *vp; 383 struct buf *sbp, *ebp; 384 ufs_daddr_t *bap, *sbap, *ebap = 0; 385 struct cluster_save *buflist; 386 ufs_daddr_t start_lbn, end_lbn, soff, newblk, blkno; 387 struct indir start_ap[NIADDR + 1], end_ap[NIADDR + 1], *idp; 388 int i, len, start_lvl, end_lvl, pref, ssize; 389 390 if (doreallocblks == 0) 391 return (ENOSPC); 392 vp = ap->a_vp; 393 ip = VTOI(vp); 394 fs = ip->i_fs; 395 if (fs->fs_contigsumsize <= 0) 396 return (ENOSPC); 397 buflist = ap->a_buflist; 398 len = buflist->bs_nchildren; 399 start_lbn = buflist->bs_children[0]->b_lblkno; 400 end_lbn = start_lbn + len - 1; 401 #ifdef DIAGNOSTIC 402 for (i = 0; i < len; i++) 403 if (!ffs_checkblk(ip, 404 dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize)) 405 panic("ffs_reallocblks: unallocated block 1"); 406 for (i = 1; i < len; i++) 407 if (buflist->bs_children[i]->b_lblkno != start_lbn + i) 408 panic("ffs_reallocblks: non-logical cluster"); 409 blkno = buflist->bs_children[0]->b_blkno; 410 ssize = fsbtodb(fs, fs->fs_frag); 411 for (i = 1; i < len - 1; i++) 412 if (buflist->bs_children[i]->b_blkno != blkno + (i * ssize)) 413 panic("ffs_reallocblks: non-physical cluster %d", i); 414 #endif 415 /* 416 * If the latest allocation is in a new cylinder group, assume that 417 * the filesystem has decided to move and do not force it back to 418 * the previous cylinder group. 419 */ 420 if (dtog(fs, dbtofsb(fs, buflist->bs_children[0]->b_blkno)) != 421 dtog(fs, dbtofsb(fs, buflist->bs_children[len - 1]->b_blkno))) 422 return (ENOSPC); 423 if (ufs_getlbns(vp, start_lbn, start_ap, &start_lvl) || 424 ufs_getlbns(vp, end_lbn, end_ap, &end_lvl)) 425 return (ENOSPC); 426 /* 427 * Get the starting offset and block map for the first block. 428 */ 429 if (start_lvl == 0) { 430 sbap = &ip->i_db[0]; 431 soff = start_lbn; 432 } else { 433 idp = &start_ap[start_lvl - 1]; 434 if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &sbp)) { 435 brelse(sbp); 436 return (ENOSPC); 437 } 438 sbap = (ufs_daddr_t *)sbp->b_data; 439 soff = idp->in_off; 440 } 441 /* 442 * Find the preferred location for the cluster. 443 */ 444 pref = ffs_blkpref(ip, start_lbn, soff, sbap); 445 /* 446 * If the block range spans two block maps, get the second map. 447 */ 448 if (end_lvl == 0 || (idp = &end_ap[end_lvl - 1])->in_off + 1 >= len) { 449 ssize = len; 450 } else { 451 #ifdef DIAGNOSTIC 452 if (start_ap[start_lvl-1].in_lbn == idp->in_lbn) 453 panic("ffs_reallocblk: start == end"); 454 #endif 455 ssize = len - (idp->in_off + 1); 456 if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &ebp)) 457 goto fail; 458 ebap = (ufs_daddr_t *)ebp->b_data; 459 } 460 /* 461 * Search the block map looking for an allocation of the desired size. 462 */ 463 if ((newblk = (ufs_daddr_t)ffs_hashalloc(ip, dtog(fs, pref), (long)pref, 464 len, ffs_clusteralloc)) == 0) 465 goto fail; 466 /* 467 * We have found a new contiguous block. 468 * 469 * First we have to replace the old block pointers with the new 470 * block pointers in the inode and indirect blocks associated 471 * with the file. 472 */ 473 #ifdef DEBUG 474 if (prtrealloc) 475 printf("realloc: ino %d, lbns %d-%d\n\told:", ip->i_number, 476 start_lbn, end_lbn); 477 #endif 478 blkno = newblk; 479 for (bap = &sbap[soff], i = 0; i < len; i++, blkno += fs->fs_frag) { 480 if (i == ssize) { 481 bap = ebap; 482 soff = -i; 483 } 484 #ifdef DIAGNOSTIC 485 if (!ffs_checkblk(ip, 486 dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize)) 487 panic("ffs_reallocblks: unallocated block 2"); 488 if (dbtofsb(fs, buflist->bs_children[i]->b_blkno) != *bap) 489 panic("ffs_reallocblks: alloc mismatch"); 490 #endif 491 #ifdef DEBUG 492 if (prtrealloc) 493 printf(" %d,", *bap); 494 #endif 495 if (DOINGSOFTDEP(vp)) { 496 if (sbap == &ip->i_db[0] && i < ssize) 497 softdep_setup_allocdirect(ip, start_lbn + i, 498 blkno, *bap, fs->fs_bsize, fs->fs_bsize, 499 buflist->bs_children[i]); 500 else 501 softdep_setup_allocindir_page(ip, start_lbn + i, 502 i < ssize ? sbp : ebp, soff + i, blkno, 503 *bap, buflist->bs_children[i]); 504 } 505 *bap++ = blkno; 506 } 507 /* 508 * Next we must write out the modified inode and indirect blocks. 509 * For strict correctness, the writes should be synchronous since 510 * the old block values may have been written to disk. In practise 511 * they are almost never written, but if we are concerned about 512 * strict correctness, the `doasyncfree' flag should be set to zero. 513 * 514 * The test on `doasyncfree' should be changed to test a flag 515 * that shows whether the associated buffers and inodes have 516 * been written. The flag should be set when the cluster is 517 * started and cleared whenever the buffer or inode is flushed. 518 * We can then check below to see if it is set, and do the 519 * synchronous write only when it has been cleared. 520 */ 521 if (sbap != &ip->i_db[0]) { 522 if (doasyncfree) 523 bdwrite(sbp); 524 else 525 bwrite(sbp); 526 } else { 527 ip->i_flag |= IN_CHANGE | IN_UPDATE; 528 if (!doasyncfree) 529 UFS_UPDATE(vp, 1); 530 } 531 if (ssize < len) { 532 if (doasyncfree) 533 bdwrite(ebp); 534 else 535 bwrite(ebp); 536 } 537 /* 538 * Last, free the old blocks and assign the new blocks to the buffers. 539 */ 540 #ifdef DEBUG 541 if (prtrealloc) 542 printf("\n\tnew:"); 543 #endif 544 for (blkno = newblk, i = 0; i < len; i++, blkno += fs->fs_frag) { 545 if (!DOINGSOFTDEP(vp)) 546 ffs_blkfree(fs, ip->i_devvp, 547 dbtofsb(fs, buflist->bs_children[i]->b_blkno), 548 fs->fs_bsize, ip->i_number); 549 buflist->bs_children[i]->b_blkno = fsbtodb(fs, blkno); 550 #ifdef DIAGNOSTIC 551 if (!ffs_checkblk(ip, 552 dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize)) 553 panic("ffs_reallocblks: unallocated block 3"); 554 #endif 555 #ifdef DEBUG 556 if (prtrealloc) 557 printf(" %d,", blkno); 558 #endif 559 } 560 #ifdef DEBUG 561 if (prtrealloc) { 562 prtrealloc--; 563 printf("\n"); 564 } 565 #endif 566 return (0); 567 568 fail: 569 if (ssize < len) 570 brelse(ebp); 571 if (sbap != &ip->i_db[0]) 572 brelse(sbp); 573 return (ENOSPC); 574 } 575 576 /* 577 * Allocate an inode in the file system. 578 * 579 * If allocating a directory, use ffs_dirpref to select the inode. 580 * If allocating in a directory, the following hierarchy is followed: 581 * 1) allocate the preferred inode. 582 * 2) allocate an inode in the same cylinder group. 583 * 3) quadradically rehash into other cylinder groups, until an 584 * available inode is located. 585 * If no inode preference is given the following heirarchy is used 586 * to allocate an inode: 587 * 1) allocate an inode in cylinder group 0. 588 * 2) quadradically rehash into other cylinder groups, until an 589 * available inode is located. 590 */ 591 int 592 ffs_valloc(pvp, mode, cred, vpp) 593 struct vnode *pvp; 594 int mode; 595 struct ucred *cred; 596 struct vnode **vpp; 597 { 598 register struct inode *pip; 599 register struct fs *fs; 600 register struct inode *ip; 601 ino_t ino, ipref; 602 int cg, error; 603 604 *vpp = NULL; 605 pip = VTOI(pvp); 606 fs = pip->i_fs; 607 if (fs->fs_cstotal.cs_nifree == 0) 608 goto noinodes; 609 610 if ((mode & IFMT) == IFDIR) 611 ipref = ffs_dirpref(pip); 612 else 613 ipref = pip->i_number; 614 if (ipref >= fs->fs_ncg * fs->fs_ipg) 615 ipref = 0; 616 cg = ino_to_cg(fs, ipref); 617 /* 618 * Track number of dirs created one after another 619 * in a same cg without intervening by files. 620 */ 621 if ((mode & IFMT) == IFDIR) { 622 if (fs->fs_contigdirs[cg] < 255) 623 fs->fs_contigdirs[cg]++; 624 } else { 625 if (fs->fs_contigdirs[cg] > 0) 626 fs->fs_contigdirs[cg]--; 627 } 628 ino = (ino_t)ffs_hashalloc(pip, cg, (long)ipref, mode, 629 (allocfcn_t *)ffs_nodealloccg); 630 if (ino == 0) 631 goto noinodes; 632 error = VFS_VGET(pvp->v_mount, ino, vpp); 633 if (error) { 634 UFS_VFREE(pvp, ino, mode); 635 return (error); 636 } 637 ip = VTOI(*vpp); 638 if (ip->i_mode) { 639 printf("mode = 0%o, inum = %lu, fs = %s\n", 640 ip->i_mode, (u_long)ip->i_number, fs->fs_fsmnt); 641 panic("ffs_valloc: dup alloc"); 642 } 643 if (ip->i_blocks && (fs->fs_flags & FS_UNCLEAN) == 0) { /* XXX */ 644 printf("free inode %s/%lu had %ld blocks\n", 645 fs->fs_fsmnt, (u_long)ino, (long)ip->i_blocks); 646 ip->i_blocks = 0; 647 } 648 ip->i_flags = 0; 649 /* 650 * Set up a new generation number for this inode. 651 */ 652 if (ip->i_gen == 0 || ++ip->i_gen == 0) 653 ip->i_gen = random() / 2 + 1; 654 return (0); 655 noinodes: 656 ffs_fserr(fs, pip->i_number, "out of inodes"); 657 uprintf("\n%s: create/symlink failed, no inodes free\n", fs->fs_fsmnt); 658 return (ENOSPC); 659 } 660 661 /* 662 * Find a cylinder group to place a directory. 663 * 664 * The policy implemented by this algorithm is to allocate a 665 * directory inode in the same cylinder group as its parent 666 * directory, but also to reserve space for its files inodes 667 * and data. Restrict the number of directories which may be 668 * allocated one after another in the same cylinder group 669 * without intervening allocation of files. 670 * 671 * If we allocate a first level directory then force allocation 672 * in another cylinder group. 673 */ 674 static ino_t 675 ffs_dirpref(pip) 676 struct inode *pip; 677 { 678 register struct fs *fs; 679 int cg, prefcg, dirsize, cgsize; 680 int avgifree, avgbfree, avgndir, curdirsize; 681 int minifree, minbfree, maxndir; 682 int mincg, minndir; 683 int maxcontigdirs; 684 685 fs = pip->i_fs; 686 687 avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg; 688 avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg; 689 avgndir = fs->fs_cstotal.cs_ndir / fs->fs_ncg; 690 691 /* 692 * Force allocation in another cg if creating a first level dir. 693 */ 694 if (ITOV(pip)->v_flag & VROOT) { 695 prefcg = arc4random() % fs->fs_ncg; 696 mincg = prefcg; 697 minndir = fs->fs_ipg; 698 for (cg = prefcg; cg < fs->fs_ncg; cg++) 699 if (fs->fs_cs(fs, cg).cs_ndir < minndir && 700 fs->fs_cs(fs, cg).cs_nifree >= avgifree && 701 fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { 702 mincg = cg; 703 minndir = fs->fs_cs(fs, cg).cs_ndir; 704 } 705 for (cg = 0; cg < prefcg; cg++) 706 if (fs->fs_cs(fs, cg).cs_ndir < minndir && 707 fs->fs_cs(fs, cg).cs_nifree >= avgifree && 708 fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { 709 mincg = cg; 710 minndir = fs->fs_cs(fs, cg).cs_ndir; 711 } 712 return ((ino_t)(fs->fs_ipg * mincg)); 713 } 714 715 /* 716 * Count various limits which used for 717 * optimal allocation of a directory inode. 718 */ 719 maxndir = min(avgndir + fs->fs_ipg / 16, fs->fs_ipg); 720 minifree = avgifree - fs->fs_ipg / 4; 721 if (minifree < 0) 722 minifree = 0; 723 minbfree = avgbfree - fs->fs_fpg / fs->fs_frag / 4; 724 if (minbfree < 0) 725 minbfree = 0; 726 cgsize = fs->fs_fsize * fs->fs_fpg; 727 dirsize = fs->fs_avgfilesize * fs->fs_avgfpdir; 728 curdirsize = avgndir ? (cgsize - avgbfree * fs->fs_bsize) / avgndir : 0; 729 if (dirsize < curdirsize) 730 dirsize = curdirsize; 731 maxcontigdirs = min(cgsize / dirsize, 255); 732 if (fs->fs_avgfpdir > 0) 733 maxcontigdirs = min(maxcontigdirs, 734 fs->fs_ipg / fs->fs_avgfpdir); 735 if (maxcontigdirs == 0) 736 maxcontigdirs = 1; 737 738 /* 739 * Limit number of dirs in one cg and reserve space for 740 * regular files, but only if we have no deficit in 741 * inodes or space. 742 */ 743 prefcg = ino_to_cg(fs, pip->i_number); 744 for (cg = prefcg; cg < fs->fs_ncg; cg++) 745 if (fs->fs_cs(fs, cg).cs_ndir < maxndir && 746 fs->fs_cs(fs, cg).cs_nifree >= minifree && 747 fs->fs_cs(fs, cg).cs_nbfree >= minbfree) { 748 if (fs->fs_contigdirs[cg] < maxcontigdirs) 749 return ((ino_t)(fs->fs_ipg * cg)); 750 } 751 for (cg = 0; cg < prefcg; cg++) 752 if (fs->fs_cs(fs, cg).cs_ndir < maxndir && 753 fs->fs_cs(fs, cg).cs_nifree >= minifree && 754 fs->fs_cs(fs, cg).cs_nbfree >= minbfree) { 755 if (fs->fs_contigdirs[cg] < maxcontigdirs) 756 return ((ino_t)(fs->fs_ipg * cg)); 757 } 758 /* 759 * This is a backstop when we have deficit in space. 760 */ 761 for (cg = prefcg; cg < fs->fs_ncg; cg++) 762 if (fs->fs_cs(fs, cg).cs_nifree >= avgifree) 763 return ((ino_t)(fs->fs_ipg * cg)); 764 for (cg = 0; cg < prefcg; cg++) 765 if (fs->fs_cs(fs, cg).cs_nifree >= avgifree) 766 break; 767 return ((ino_t)(fs->fs_ipg * cg)); 768 } 769 770 /* 771 * Select the desired position for the next block in a file. The file is 772 * logically divided into sections. The first section is composed of the 773 * direct blocks. Each additional section contains fs_maxbpg blocks. 774 * 775 * If no blocks have been allocated in the first section, the policy is to 776 * request a block in the same cylinder group as the inode that describes 777 * the file. If no blocks have been allocated in any other section, the 778 * policy is to place the section in a cylinder group with a greater than 779 * average number of free blocks. An appropriate cylinder group is found 780 * by using a rotor that sweeps the cylinder groups. When a new group of 781 * blocks is needed, the sweep begins in the cylinder group following the 782 * cylinder group from which the previous allocation was made. The sweep 783 * continues until a cylinder group with greater than the average number 784 * of free blocks is found. If the allocation is for the first block in an 785 * indirect block, the information on the previous allocation is unavailable; 786 * here a best guess is made based upon the logical block number being 787 * allocated. 788 * 789 * If a section is already partially allocated, the policy is to 790 * contiguously allocate fs_maxcontig blocks. The end of one of these 791 * contiguous blocks and the beginning of the next is physically separated 792 * so that the disk head will be in transit between them for at least 793 * fs_rotdelay milliseconds. This is to allow time for the processor to 794 * schedule another I/O transfer. 795 */ 796 ufs_daddr_t 797 ffs_blkpref(ip, lbn, indx, bap) 798 struct inode *ip; 799 ufs_daddr_t lbn; 800 int indx; 801 ufs_daddr_t *bap; 802 { 803 register struct fs *fs; 804 register int cg; 805 int avgbfree, startcg; 806 ufs_daddr_t nextblk; 807 808 fs = ip->i_fs; 809 if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) { 810 if (lbn < NDADDR + NINDIR(fs)) { 811 cg = ino_to_cg(fs, ip->i_number); 812 return (fs->fs_fpg * cg + fs->fs_frag); 813 } 814 /* 815 * Find a cylinder with greater than average number of 816 * unused data blocks. 817 */ 818 if (indx == 0 || bap[indx - 1] == 0) 819 startcg = 820 ino_to_cg(fs, ip->i_number) + lbn / fs->fs_maxbpg; 821 else 822 startcg = dtog(fs, bap[indx - 1]) + 1; 823 startcg %= fs->fs_ncg; 824 avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg; 825 for (cg = startcg; cg < fs->fs_ncg; cg++) 826 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { 827 fs->fs_cgrotor = cg; 828 return (fs->fs_fpg * cg + fs->fs_frag); 829 } 830 for (cg = 0; cg <= startcg; cg++) 831 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { 832 fs->fs_cgrotor = cg; 833 return (fs->fs_fpg * cg + fs->fs_frag); 834 } 835 return (0); 836 } 837 /* 838 * One or more previous blocks have been laid out. If less 839 * than fs_maxcontig previous blocks are contiguous, the 840 * next block is requested contiguously, otherwise it is 841 * requested rotationally delayed by fs_rotdelay milliseconds. 842 */ 843 nextblk = bap[indx - 1] + fs->fs_frag; 844 if (fs->fs_rotdelay == 0 || indx < fs->fs_maxcontig || 845 bap[indx - fs->fs_maxcontig] + 846 blkstofrags(fs, fs->fs_maxcontig) != nextblk) 847 return (nextblk); 848 /* 849 * Here we convert ms of delay to frags as: 850 * (frags) = (ms) * (rev/sec) * (sect/rev) / 851 * ((sect/frag) * (ms/sec)) 852 * then round up to the next block. 853 */ 854 nextblk += roundup(fs->fs_rotdelay * fs->fs_rps * fs->fs_nsect / 855 (NSPF(fs) * 1000), fs->fs_frag); 856 return (nextblk); 857 } 858 859 /* 860 * Implement the cylinder overflow algorithm. 861 * 862 * The policy implemented by this algorithm is: 863 * 1) allocate the block in its requested cylinder group. 864 * 2) quadradically rehash on the cylinder group number. 865 * 3) brute force search for a free block. 866 */ 867 /*VARARGS5*/ 868 static u_long 869 ffs_hashalloc(ip, cg, pref, size, allocator) 870 struct inode *ip; 871 int cg; 872 long pref; 873 int size; /* size for data blocks, mode for inodes */ 874 allocfcn_t *allocator; 875 { 876 register struct fs *fs; 877 long result; /* XXX why not same type as we return? */ 878 int i, icg = cg; 879 880 #ifdef DIAGNOSTIC 881 if (ITOV(ip)->v_mount->mnt_kern_flag & MNTK_SUSPENDED) 882 panic("ffs_hashalloc: allocation on suspended filesystem"); 883 #endif 884 fs = ip->i_fs; 885 /* 886 * 1: preferred cylinder group 887 */ 888 result = (*allocator)(ip, cg, pref, size); 889 if (result) 890 return (result); 891 /* 892 * 2: quadratic rehash 893 */ 894 for (i = 1; i < fs->fs_ncg; i *= 2) { 895 cg += i; 896 if (cg >= fs->fs_ncg) 897 cg -= fs->fs_ncg; 898 result = (*allocator)(ip, cg, 0, size); 899 if (result) 900 return (result); 901 } 902 /* 903 * 3: brute force search 904 * Note that we start at i == 2, since 0 was checked initially, 905 * and 1 is always checked in the quadratic rehash. 906 */ 907 cg = (icg + 2) % fs->fs_ncg; 908 for (i = 2; i < fs->fs_ncg; i++) { 909 result = (*allocator)(ip, cg, 0, size); 910 if (result) 911 return (result); 912 cg++; 913 if (cg == fs->fs_ncg) 914 cg = 0; 915 } 916 return (0); 917 } 918 919 /* 920 * Determine whether a fragment can be extended. 921 * 922 * Check to see if the necessary fragments are available, and 923 * if they are, allocate them. 924 */ 925 static ufs_daddr_t 926 ffs_fragextend(ip, cg, bprev, osize, nsize) 927 struct inode *ip; 928 int cg; 929 long bprev; 930 int osize, nsize; 931 { 932 register struct fs *fs; 933 register struct cg *cgp; 934 struct buf *bp; 935 long bno; 936 int frags, bbase; 937 int i, error; 938 u_int8_t *blksfree; 939 940 fs = ip->i_fs; 941 if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize)) 942 return (0); 943 frags = numfrags(fs, nsize); 944 bbase = fragnum(fs, bprev); 945 if (bbase > fragnum(fs, (bprev + frags - 1))) { 946 /* cannot extend across a block boundary */ 947 return (0); 948 } 949 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 950 (int)fs->fs_cgsize, NOCRED, &bp); 951 if (error) { 952 brelse(bp); 953 return (0); 954 } 955 cgp = (struct cg *)bp->b_data; 956 if (!cg_chkmagic(cgp)) { 957 brelse(bp); 958 return (0); 959 } 960 bp->b_xflags |= BX_BKGRDWRITE; 961 cgp->cg_time = time_second; 962 bno = dtogd(fs, bprev); 963 blksfree = cg_blksfree(cgp); 964 for (i = numfrags(fs, osize); i < frags; i++) 965 if (isclr(blksfree, bno + i)) { 966 brelse(bp); 967 return (0); 968 } 969 /* 970 * the current fragment can be extended 971 * deduct the count on fragment being extended into 972 * increase the count on the remaining fragment (if any) 973 * allocate the extended piece 974 */ 975 for (i = frags; i < fs->fs_frag - bbase; i++) 976 if (isclr(blksfree, bno + i)) 977 break; 978 cgp->cg_frsum[i - numfrags(fs, osize)]--; 979 if (i != frags) 980 cgp->cg_frsum[i - frags]++; 981 for (i = numfrags(fs, osize); i < frags; i++) { 982 clrbit(blksfree, bno + i); 983 cgp->cg_cs.cs_nffree--; 984 fs->fs_cstotal.cs_nffree--; 985 fs->fs_cs(fs, cg).cs_nffree--; 986 } 987 fs->fs_fmod = 1; 988 if (DOINGSOFTDEP(ITOV(ip))) 989 softdep_setup_blkmapdep(bp, fs, bprev); 990 if (fs->fs_active != 0) 991 atomic_clear_int(&ACTIVECGNUM(fs, cg), ACTIVECGOFF(cg)); 992 bdwrite(bp); 993 return (bprev); 994 } 995 996 /* 997 * Determine whether a block can be allocated. 998 * 999 * Check to see if a block of the appropriate size is available, 1000 * and if it is, allocate it. 1001 */ 1002 static ufs_daddr_t 1003 ffs_alloccg(ip, cg, bpref, size) 1004 struct inode *ip; 1005 int cg; 1006 ufs_daddr_t bpref; 1007 int size; 1008 { 1009 register struct fs *fs; 1010 register struct cg *cgp; 1011 struct buf *bp; 1012 register int i; 1013 ufs_daddr_t bno, blkno; 1014 int allocsiz, error, frags; 1015 u_int8_t *blksfree; 1016 1017 fs = ip->i_fs; 1018 if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize) 1019 return (0); 1020 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 1021 (int)fs->fs_cgsize, NOCRED, &bp); 1022 if (error) { 1023 brelse(bp); 1024 return (0); 1025 } 1026 cgp = (struct cg *)bp->b_data; 1027 if (!cg_chkmagic(cgp) || 1028 (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) { 1029 brelse(bp); 1030 return (0); 1031 } 1032 bp->b_xflags |= BX_BKGRDWRITE; 1033 cgp->cg_time = time_second; 1034 if (size == fs->fs_bsize) { 1035 bno = ffs_alloccgblk(ip, bp, bpref); 1036 if (fs->fs_active != 0) 1037 atomic_clear_int(&ACTIVECGNUM(fs, cg), ACTIVECGOFF(cg)); 1038 bdwrite(bp); 1039 return (bno); 1040 } 1041 /* 1042 * check to see if any fragments are already available 1043 * allocsiz is the size which will be allocated, hacking 1044 * it down to a smaller size if necessary 1045 */ 1046 blksfree = cg_blksfree(cgp); 1047 frags = numfrags(fs, size); 1048 for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++) 1049 if (cgp->cg_frsum[allocsiz] != 0) 1050 break; 1051 if (allocsiz == fs->fs_frag) { 1052 /* 1053 * no fragments were available, so a block will be 1054 * allocated, and hacked up 1055 */ 1056 if (cgp->cg_cs.cs_nbfree == 0) { 1057 brelse(bp); 1058 return (0); 1059 } 1060 bno = ffs_alloccgblk(ip, bp, bpref); 1061 bpref = dtogd(fs, bno); 1062 for (i = frags; i < fs->fs_frag; i++) 1063 setbit(blksfree, bpref + i); 1064 i = fs->fs_frag - frags; 1065 cgp->cg_cs.cs_nffree += i; 1066 fs->fs_cstotal.cs_nffree += i; 1067 fs->fs_cs(fs, cg).cs_nffree += i; 1068 fs->fs_fmod = 1; 1069 cgp->cg_frsum[i]++; 1070 if (fs->fs_active != 0) 1071 atomic_clear_int(&ACTIVECGNUM(fs, cg), ACTIVECGOFF(cg)); 1072 bdwrite(bp); 1073 return (bno); 1074 } 1075 bno = ffs_mapsearch(fs, cgp, bpref, allocsiz); 1076 if (bno < 0) { 1077 brelse(bp); 1078 return (0); 1079 } 1080 for (i = 0; i < frags; i++) 1081 clrbit(blksfree, bno + i); 1082 cgp->cg_cs.cs_nffree -= frags; 1083 fs->fs_cstotal.cs_nffree -= frags; 1084 fs->fs_cs(fs, cg).cs_nffree -= frags; 1085 fs->fs_fmod = 1; 1086 cgp->cg_frsum[allocsiz]--; 1087 if (frags != allocsiz) 1088 cgp->cg_frsum[allocsiz - frags]++; 1089 blkno = cg * fs->fs_fpg + bno; 1090 if (DOINGSOFTDEP(ITOV(ip))) 1091 softdep_setup_blkmapdep(bp, fs, blkno); 1092 if (fs->fs_active != 0) 1093 atomic_clear_int(&ACTIVECGNUM(fs, cg), ACTIVECGOFF(cg)); 1094 bdwrite(bp); 1095 return ((u_long)blkno); 1096 } 1097 1098 /* 1099 * Allocate a block in a cylinder group. 1100 * 1101 * This algorithm implements the following policy: 1102 * 1) allocate the requested block. 1103 * 2) allocate a rotationally optimal block in the same cylinder. 1104 * 3) allocate the next available block on the block rotor for the 1105 * specified cylinder group. 1106 * Note that this routine only allocates fs_bsize blocks; these 1107 * blocks may be fragmented by the routine that allocates them. 1108 */ 1109 static ufs_daddr_t 1110 ffs_alloccgblk(ip, bp, bpref) 1111 struct inode *ip; 1112 struct buf *bp; 1113 ufs_daddr_t bpref; 1114 { 1115 struct fs *fs; 1116 struct cg *cgp; 1117 ufs_daddr_t bno, blkno; 1118 int cylno, pos, delta; 1119 short *cylbp; 1120 register int i; 1121 u_int8_t *blksfree; 1122 1123 fs = ip->i_fs; 1124 cgp = (struct cg *)bp->b_data; 1125 blksfree = cg_blksfree(cgp); 1126 if (bpref == 0 || dtog(fs, bpref) != cgp->cg_cgx) { 1127 bpref = cgp->cg_rotor; 1128 goto norot; 1129 } 1130 bpref = blknum(fs, bpref); 1131 bpref = dtogd(fs, bpref); 1132 /* 1133 * if the requested block is available, use it 1134 */ 1135 if (ffs_isblock(fs, blksfree, fragstoblks(fs, bpref))) { 1136 bno = bpref; 1137 goto gotit; 1138 } 1139 if (fs->fs_nrpos <= 1 || fs->fs_cpc == 0) { 1140 /* 1141 * Block layout information is not available. 1142 * Leaving bpref unchanged means we take the 1143 * next available free block following the one 1144 * we just allocated. Hopefully this will at 1145 * least hit a track cache on drives of unknown 1146 * geometry (e.g. SCSI). 1147 */ 1148 goto norot; 1149 } 1150 /* 1151 * check for a block available on the same cylinder 1152 */ 1153 cylno = cbtocylno(fs, bpref); 1154 if (cg_blktot(cgp)[cylno] == 0) 1155 goto norot; 1156 /* 1157 * check the summary information to see if a block is 1158 * available in the requested cylinder starting at the 1159 * requested rotational position and proceeding around. 1160 */ 1161 cylbp = cg_blks(fs, cgp, cylno); 1162 pos = cbtorpos(fs, bpref); 1163 for (i = pos; i < fs->fs_nrpos; i++) 1164 if (cylbp[i] > 0) 1165 break; 1166 if (i == fs->fs_nrpos) 1167 for (i = 0; i < pos; i++) 1168 if (cylbp[i] > 0) 1169 break; 1170 if (cylbp[i] > 0) { 1171 /* 1172 * found a rotational position, now find the actual 1173 * block. A panic if none is actually there. 1174 */ 1175 pos = cylno % fs->fs_cpc; 1176 bno = (cylno - pos) * fs->fs_spc / NSPB(fs); 1177 if (fs_postbl(fs, pos)[i] == -1) { 1178 printf("pos = %d, i = %d, fs = %s\n", 1179 pos, i, fs->fs_fsmnt); 1180 panic("ffs_alloccgblk: cyl groups corrupted"); 1181 } 1182 for (i = fs_postbl(fs, pos)[i];; ) { 1183 if (ffs_isblock(fs, blksfree, bno + i)) { 1184 bno = blkstofrags(fs, (bno + i)); 1185 goto gotit; 1186 } 1187 delta = fs_rotbl(fs)[i]; 1188 if (delta <= 0 || 1189 delta + i > fragstoblks(fs, fs->fs_fpg)) 1190 break; 1191 i += delta; 1192 } 1193 printf("pos = %d, i = %d, fs = %s\n", pos, i, fs->fs_fsmnt); 1194 panic("ffs_alloccgblk: can't find blk in cyl"); 1195 } 1196 norot: 1197 /* 1198 * no blocks in the requested cylinder, so take next 1199 * available one in this cylinder group. 1200 */ 1201 bno = ffs_mapsearch(fs, cgp, bpref, (int)fs->fs_frag); 1202 if (bno < 0) 1203 return (0); 1204 cgp->cg_rotor = bno; 1205 gotit: 1206 blkno = fragstoblks(fs, bno); 1207 ffs_clrblock(fs, blksfree, (long)blkno); 1208 ffs_clusteracct(fs, cgp, blkno, -1); 1209 cgp->cg_cs.cs_nbfree--; 1210 fs->fs_cstotal.cs_nbfree--; 1211 fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--; 1212 cylno = cbtocylno(fs, bno); 1213 cg_blks(fs, cgp, cylno)[cbtorpos(fs, bno)]--; 1214 cg_blktot(cgp)[cylno]--; 1215 fs->fs_fmod = 1; 1216 blkno = cgp->cg_cgx * fs->fs_fpg + bno; 1217 if (DOINGSOFTDEP(ITOV(ip))) 1218 softdep_setup_blkmapdep(bp, fs, blkno); 1219 return (blkno); 1220 } 1221 1222 /* 1223 * Determine whether a cluster can be allocated. 1224 * 1225 * We do not currently check for optimal rotational layout if there 1226 * are multiple choices in the same cylinder group. Instead we just 1227 * take the first one that we find following bpref. 1228 */ 1229 static ufs_daddr_t 1230 ffs_clusteralloc(ip, cg, bpref, len) 1231 struct inode *ip; 1232 int cg; 1233 ufs_daddr_t bpref; 1234 int len; 1235 { 1236 register struct fs *fs; 1237 register struct cg *cgp; 1238 struct buf *bp; 1239 int i, got, run, bno, bit, map; 1240 u_char *mapp; 1241 int32_t *lp; 1242 u_int8_t *blksfree; 1243 1244 fs = ip->i_fs; 1245 if (fs->fs_maxcluster[cg] < len) 1246 return (0); 1247 if (bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize, 1248 NOCRED, &bp)) 1249 goto fail; 1250 cgp = (struct cg *)bp->b_data; 1251 if (!cg_chkmagic(cgp)) 1252 goto fail; 1253 bp->b_xflags |= BX_BKGRDWRITE; 1254 /* 1255 * Check to see if a cluster of the needed size (or bigger) is 1256 * available in this cylinder group. 1257 */ 1258 lp = &cg_clustersum(cgp)[len]; 1259 for (i = len; i <= fs->fs_contigsumsize; i++) 1260 if (*lp++ > 0) 1261 break; 1262 if (i > fs->fs_contigsumsize) { 1263 /* 1264 * This is the first time looking for a cluster in this 1265 * cylinder group. Update the cluster summary information 1266 * to reflect the true maximum sized cluster so that 1267 * future cluster allocation requests can avoid reading 1268 * the cylinder group map only to find no clusters. 1269 */ 1270 lp = &cg_clustersum(cgp)[len - 1]; 1271 for (i = len - 1; i > 0; i--) 1272 if (*lp-- > 0) 1273 break; 1274 fs->fs_maxcluster[cg] = i; 1275 goto fail; 1276 } 1277 /* 1278 * Search the cluster map to find a big enough cluster. 1279 * We take the first one that we find, even if it is larger 1280 * than we need as we prefer to get one close to the previous 1281 * block allocation. We do not search before the current 1282 * preference point as we do not want to allocate a block 1283 * that is allocated before the previous one (as we will 1284 * then have to wait for another pass of the elevator 1285 * algorithm before it will be read). We prefer to fail and 1286 * be recalled to try an allocation in the next cylinder group. 1287 */ 1288 if (dtog(fs, bpref) != cg) 1289 bpref = 0; 1290 else 1291 bpref = fragstoblks(fs, dtogd(fs, blknum(fs, bpref))); 1292 mapp = &cg_clustersfree(cgp)[bpref / NBBY]; 1293 map = *mapp++; 1294 bit = 1 << (bpref % NBBY); 1295 for (run = 0, got = bpref; got < cgp->cg_nclusterblks; got++) { 1296 if ((map & bit) == 0) { 1297 run = 0; 1298 } else { 1299 run++; 1300 if (run == len) 1301 break; 1302 } 1303 if ((got & (NBBY - 1)) != (NBBY - 1)) { 1304 bit <<= 1; 1305 } else { 1306 map = *mapp++; 1307 bit = 1; 1308 } 1309 } 1310 if (got >= cgp->cg_nclusterblks) 1311 goto fail; 1312 /* 1313 * Allocate the cluster that we have found. 1314 */ 1315 blksfree = cg_blksfree(cgp); 1316 for (i = 1; i <= len; i++) 1317 if (!ffs_isblock(fs, blksfree, got - run + i)) 1318 panic("ffs_clusteralloc: map mismatch"); 1319 bno = cg * fs->fs_fpg + blkstofrags(fs, got - run + 1); 1320 if (dtog(fs, bno) != cg) 1321 panic("ffs_clusteralloc: allocated out of group"); 1322 len = blkstofrags(fs, len); 1323 for (i = 0; i < len; i += fs->fs_frag) 1324 if ((got = ffs_alloccgblk(ip, bp, bno + i)) != bno + i) 1325 panic("ffs_clusteralloc: lost block"); 1326 if (fs->fs_active != 0) 1327 atomic_clear_int(&ACTIVECGNUM(fs, cg), ACTIVECGOFF(cg)); 1328 bdwrite(bp); 1329 return (bno); 1330 1331 fail: 1332 brelse(bp); 1333 return (0); 1334 } 1335 1336 /* 1337 * Determine whether an inode can be allocated. 1338 * 1339 * Check to see if an inode is available, and if it is, 1340 * allocate it using the following policy: 1341 * 1) allocate the requested inode. 1342 * 2) allocate the next available inode after the requested 1343 * inode in the specified cylinder group. 1344 */ 1345 static ino_t 1346 ffs_nodealloccg(ip, cg, ipref, mode) 1347 struct inode *ip; 1348 int cg; 1349 ufs_daddr_t ipref; 1350 int mode; 1351 { 1352 register struct fs *fs; 1353 register struct cg *cgp; 1354 struct buf *bp; 1355 u_int8_t *inosused; 1356 int error, start, len, loc, map, i; 1357 1358 fs = ip->i_fs; 1359 if (fs->fs_cs(fs, cg).cs_nifree == 0) 1360 return (0); 1361 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 1362 (int)fs->fs_cgsize, NOCRED, &bp); 1363 if (error) { 1364 brelse(bp); 1365 return (0); 1366 } 1367 cgp = (struct cg *)bp->b_data; 1368 if (!cg_chkmagic(cgp) || cgp->cg_cs.cs_nifree == 0) { 1369 brelse(bp); 1370 return (0); 1371 } 1372 bp->b_xflags |= BX_BKGRDWRITE; 1373 cgp->cg_time = time_second; 1374 inosused = cg_inosused(cgp); 1375 if (ipref) { 1376 ipref %= fs->fs_ipg; 1377 if (isclr(inosused, ipref)) 1378 goto gotit; 1379 } 1380 start = cgp->cg_irotor / NBBY; 1381 len = howmany(fs->fs_ipg - cgp->cg_irotor, NBBY); 1382 loc = skpc(0xff, len, &inosused[start]); 1383 if (loc == 0) { 1384 len = start + 1; 1385 start = 0; 1386 loc = skpc(0xff, len, &inosused[0]); 1387 if (loc == 0) { 1388 printf("cg = %d, irotor = %ld, fs = %s\n", 1389 cg, (long)cgp->cg_irotor, fs->fs_fsmnt); 1390 panic("ffs_nodealloccg: map corrupted"); 1391 /* NOTREACHED */ 1392 } 1393 } 1394 i = start + len - loc; 1395 map = inosused[i]; 1396 ipref = i * NBBY; 1397 for (i = 1; i < (1 << NBBY); i <<= 1, ipref++) { 1398 if ((map & i) == 0) { 1399 cgp->cg_irotor = ipref; 1400 goto gotit; 1401 } 1402 } 1403 printf("fs = %s\n", fs->fs_fsmnt); 1404 panic("ffs_nodealloccg: block not in map"); 1405 /* NOTREACHED */ 1406 gotit: 1407 if (DOINGSOFTDEP(ITOV(ip))) 1408 softdep_setup_inomapdep(bp, ip, cg * fs->fs_ipg + ipref); 1409 setbit(inosused, ipref); 1410 cgp->cg_cs.cs_nifree--; 1411 fs->fs_cstotal.cs_nifree--; 1412 fs->fs_cs(fs, cg).cs_nifree--; 1413 fs->fs_fmod = 1; 1414 if ((mode & IFMT) == IFDIR) { 1415 cgp->cg_cs.cs_ndir++; 1416 fs->fs_cstotal.cs_ndir++; 1417 fs->fs_cs(fs, cg).cs_ndir++; 1418 } 1419 if (fs->fs_active != 0) 1420 atomic_clear_int(&ACTIVECGNUM(fs, cg), ACTIVECGOFF(cg)); 1421 bdwrite(bp); 1422 return (cg * fs->fs_ipg + ipref); 1423 } 1424 1425 /* 1426 * Free a block or fragment. 1427 * 1428 * The specified block or fragment is placed back in the 1429 * free map. If a fragment is deallocated, a possible 1430 * block reassembly is checked. 1431 */ 1432 void 1433 ffs_blkfree(fs, devvp, bno, size, inum) 1434 struct fs *fs; 1435 struct vnode *devvp; 1436 ufs_daddr_t bno; 1437 long size; 1438 ino_t inum; 1439 { 1440 struct cg *cgp; 1441 struct buf *bp; 1442 ufs_daddr_t fragno, cgbno; 1443 int i, error, cg, blk, frags, bbase; 1444 u_int8_t *blksfree; 1445 dev_t dev; 1446 1447 cg = dtog(fs, bno); 1448 if (devvp->v_type != VCHR) { 1449 /* devvp is a snapshot */ 1450 dev = VTOI(devvp)->i_devvp->v_rdev; 1451 cgbno = fragstoblks(fs, cgtod(fs, cg)); 1452 } else { 1453 /* devvp is a normal disk device */ 1454 dev = devvp->v_rdev; 1455 cgbno = fsbtodb(fs, cgtod(fs, cg)); 1456 if ((devvp->v_flag & VCOPYONWRITE) && 1457 ffs_snapblkfree(fs, devvp, bno, size, inum)) 1458 return; 1459 VOP_FREEBLKS(devvp, fsbtodb(fs, bno), size); 1460 } 1461 #ifdef DIAGNOSTIC 1462 if (dev->si_mountpoint && 1463 (dev->si_mountpoint->mnt_kern_flag & MNTK_SUSPENDED)) 1464 panic("ffs_blkfree: deallocation on suspended filesystem"); 1465 if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0 || 1466 fragnum(fs, bno) + numfrags(fs, size) > fs->fs_frag) { 1467 printf("dev=%s, bno = %ld, bsize = %ld, size = %ld, fs = %s\n", 1468 devtoname(dev), (long)bno, (long)fs->fs_bsize, 1469 size, fs->fs_fsmnt); 1470 panic("ffs_blkfree: bad size"); 1471 } 1472 #endif 1473 if ((u_int)bno >= fs->fs_size) { 1474 printf("bad block %ld, ino %lu\n", (long)bno, (u_long)inum); 1475 ffs_fserr(fs, inum, "bad block"); 1476 return; 1477 } 1478 if ((error = bread(devvp, cgbno, (int)fs->fs_cgsize, NOCRED, &bp))) { 1479 brelse(bp); 1480 return; 1481 } 1482 cgp = (struct cg *)bp->b_data; 1483 if (!cg_chkmagic(cgp)) { 1484 brelse(bp); 1485 return; 1486 } 1487 bp->b_xflags |= BX_BKGRDWRITE; 1488 cgp->cg_time = time_second; 1489 cgbno = dtogd(fs, bno); 1490 blksfree = cg_blksfree(cgp); 1491 if (size == fs->fs_bsize) { 1492 fragno = fragstoblks(fs, cgbno); 1493 if (!ffs_isfreeblock(fs, blksfree, fragno)) { 1494 if (devvp->v_type != VCHR) { 1495 /* devvp is a snapshot */ 1496 brelse(bp); 1497 return; 1498 } 1499 printf("dev = %s, block = %ld, fs = %s\n", 1500 devtoname(dev), (long)bno, fs->fs_fsmnt); 1501 panic("ffs_blkfree: freeing free block"); 1502 } 1503 ffs_setblock(fs, blksfree, fragno); 1504 ffs_clusteracct(fs, cgp, fragno, 1); 1505 cgp->cg_cs.cs_nbfree++; 1506 fs->fs_cstotal.cs_nbfree++; 1507 fs->fs_cs(fs, cg).cs_nbfree++; 1508 i = cbtocylno(fs, cgbno); 1509 cg_blks(fs, cgp, i)[cbtorpos(fs, cgbno)]++; 1510 cg_blktot(cgp)[i]++; 1511 } else { 1512 bbase = cgbno - fragnum(fs, cgbno); 1513 /* 1514 * decrement the counts associated with the old frags 1515 */ 1516 blk = blkmap(fs, blksfree, bbase); 1517 ffs_fragacct(fs, blk, cgp->cg_frsum, -1); 1518 /* 1519 * deallocate the fragment 1520 */ 1521 frags = numfrags(fs, size); 1522 for (i = 0; i < frags; i++) { 1523 if (isset(blksfree, cgbno + i)) { 1524 printf("dev = %s, block = %ld, fs = %s\n", 1525 devtoname(dev), (long)(bno + i), 1526 fs->fs_fsmnt); 1527 panic("ffs_blkfree: freeing free frag"); 1528 } 1529 setbit(blksfree, cgbno + i); 1530 } 1531 cgp->cg_cs.cs_nffree += i; 1532 fs->fs_cstotal.cs_nffree += i; 1533 fs->fs_cs(fs, cg).cs_nffree += i; 1534 /* 1535 * add back in counts associated with the new frags 1536 */ 1537 blk = blkmap(fs, blksfree, bbase); 1538 ffs_fragacct(fs, blk, cgp->cg_frsum, 1); 1539 /* 1540 * if a complete block has been reassembled, account for it 1541 */ 1542 fragno = fragstoblks(fs, bbase); 1543 if (ffs_isblock(fs, blksfree, fragno)) { 1544 cgp->cg_cs.cs_nffree -= fs->fs_frag; 1545 fs->fs_cstotal.cs_nffree -= fs->fs_frag; 1546 fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag; 1547 ffs_clusteracct(fs, cgp, fragno, 1); 1548 cgp->cg_cs.cs_nbfree++; 1549 fs->fs_cstotal.cs_nbfree++; 1550 fs->fs_cs(fs, cg).cs_nbfree++; 1551 i = cbtocylno(fs, bbase); 1552 cg_blks(fs, cgp, i)[cbtorpos(fs, bbase)]++; 1553 cg_blktot(cgp)[i]++; 1554 } 1555 } 1556 fs->fs_fmod = 1; 1557 if (fs->fs_active != 0) 1558 atomic_clear_int(&ACTIVECGNUM(fs, cg), ACTIVECGOFF(cg)); 1559 bdwrite(bp); 1560 } 1561 1562 #ifdef DIAGNOSTIC 1563 /* 1564 * Verify allocation of a block or fragment. Returns true if block or 1565 * fragment is allocated, false if it is free. 1566 */ 1567 static int 1568 ffs_checkblk(ip, bno, size) 1569 struct inode *ip; 1570 ufs_daddr_t bno; 1571 long size; 1572 { 1573 struct fs *fs; 1574 struct cg *cgp; 1575 struct buf *bp; 1576 int i, error, frags, free; 1577 u_int8_t *blksfree; 1578 1579 fs = ip->i_fs; 1580 if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0) { 1581 printf("bsize = %ld, size = %ld, fs = %s\n", 1582 (long)fs->fs_bsize, size, fs->fs_fsmnt); 1583 panic("ffs_checkblk: bad size"); 1584 } 1585 if ((u_int)bno >= fs->fs_size) 1586 panic("ffs_checkblk: bad block %d", bno); 1587 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, dtog(fs, bno))), 1588 (int)fs->fs_cgsize, NOCRED, &bp); 1589 if (error) 1590 panic("ffs_checkblk: cg bread failed"); 1591 cgp = (struct cg *)bp->b_data; 1592 if (!cg_chkmagic(cgp)) 1593 panic("ffs_checkblk: cg magic mismatch"); 1594 bp->b_xflags |= BX_BKGRDWRITE; 1595 blksfree = cg_blksfree(cgp); 1596 bno = dtogd(fs, bno); 1597 if (size == fs->fs_bsize) { 1598 free = ffs_isblock(fs, blksfree, fragstoblks(fs, bno)); 1599 } else { 1600 frags = numfrags(fs, size); 1601 for (free = 0, i = 0; i < frags; i++) 1602 if (isset(blksfree, bno + i)) 1603 free++; 1604 if (free != 0 && free != frags) 1605 panic("ffs_checkblk: partially free fragment"); 1606 } 1607 brelse(bp); 1608 return (!free); 1609 } 1610 #endif /* DIAGNOSTIC */ 1611 1612 /* 1613 * Free an inode. 1614 */ 1615 int 1616 ffs_vfree(pvp, ino, mode) 1617 struct vnode *pvp; 1618 ino_t ino; 1619 int mode; 1620 { 1621 if (DOINGSOFTDEP(pvp)) { 1622 softdep_freefile(pvp, ino, mode); 1623 return (0); 1624 } 1625 return (ffs_freefile(VTOI(pvp)->i_fs, VTOI(pvp)->i_devvp, ino, mode)); 1626 } 1627 1628 /* 1629 * Do the actual free operation. 1630 * The specified inode is placed back in the free map. 1631 */ 1632 int 1633 ffs_freefile(fs, devvp, ino, mode) 1634 struct fs *fs; 1635 struct vnode *devvp; 1636 ino_t ino; 1637 int mode; 1638 { 1639 struct cg *cgp; 1640 struct buf *bp; 1641 int error, cgbno, cg; 1642 u_int8_t *inosused; 1643 dev_t dev; 1644 1645 cg = ino_to_cg(fs, ino); 1646 if (devvp->v_type != VCHR) { 1647 /* devvp is a snapshot */ 1648 dev = VTOI(devvp)->i_devvp->v_rdev; 1649 cgbno = fragstoblks(fs, cgtod(fs, cg)); 1650 } else { 1651 /* devvp is a normal disk device */ 1652 dev = devvp->v_rdev; 1653 cgbno = fsbtodb(fs, cgtod(fs, cg)); 1654 } 1655 if ((u_int)ino >= fs->fs_ipg * fs->fs_ncg) 1656 panic("ffs_vfree: range: dev = %s, ino = %d, fs = %s", 1657 devtoname(dev), ino, fs->fs_fsmnt); 1658 if ((error = bread(devvp, cgbno, (int)fs->fs_cgsize, NOCRED, &bp))) { 1659 brelse(bp); 1660 return (error); 1661 } 1662 cgp = (struct cg *)bp->b_data; 1663 if (!cg_chkmagic(cgp)) { 1664 brelse(bp); 1665 return (0); 1666 } 1667 bp->b_xflags |= BX_BKGRDWRITE; 1668 cgp->cg_time = time_second; 1669 inosused = cg_inosused(cgp); 1670 ino %= fs->fs_ipg; 1671 if (isclr(inosused, ino)) { 1672 printf("dev = %s, ino = %lu, fs = %s\n", devtoname(dev), 1673 (u_long)ino + cg * fs->fs_ipg, fs->fs_fsmnt); 1674 if (fs->fs_ronly == 0) 1675 panic("ffs_vfree: freeing free inode"); 1676 } 1677 clrbit(inosused, ino); 1678 if (ino < cgp->cg_irotor) 1679 cgp->cg_irotor = ino; 1680 cgp->cg_cs.cs_nifree++; 1681 fs->fs_cstotal.cs_nifree++; 1682 fs->fs_cs(fs, cg).cs_nifree++; 1683 if ((mode & IFMT) == IFDIR) { 1684 cgp->cg_cs.cs_ndir--; 1685 fs->fs_cstotal.cs_ndir--; 1686 fs->fs_cs(fs, cg).cs_ndir--; 1687 } 1688 fs->fs_fmod = 1; 1689 if (fs->fs_active != 0) 1690 atomic_clear_int(&ACTIVECGNUM(fs, cg), ACTIVECGOFF(cg)); 1691 bdwrite(bp); 1692 return (0); 1693 } 1694 1695 /* 1696 * Find a block of the specified size in the specified cylinder group. 1697 * 1698 * It is a panic if a request is made to find a block if none are 1699 * available. 1700 */ 1701 static ufs_daddr_t 1702 ffs_mapsearch(fs, cgp, bpref, allocsiz) 1703 register struct fs *fs; 1704 register struct cg *cgp; 1705 ufs_daddr_t bpref; 1706 int allocsiz; 1707 { 1708 ufs_daddr_t bno; 1709 int start, len, loc, i; 1710 int blk, field, subfield, pos; 1711 u_int8_t *blksfree; 1712 1713 /* 1714 * find the fragment by searching through the free block 1715 * map for an appropriate bit pattern 1716 */ 1717 if (bpref) 1718 start = dtogd(fs, bpref) / NBBY; 1719 else 1720 start = cgp->cg_frotor / NBBY; 1721 blksfree = cg_blksfree(cgp); 1722 len = howmany(fs->fs_fpg, NBBY) - start; 1723 loc = scanc((u_int)len, (u_char *)&blksfree[start], 1724 (u_char *)fragtbl[fs->fs_frag], 1725 (u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY)))); 1726 if (loc == 0) { 1727 len = start + 1; 1728 start = 0; 1729 loc = scanc((u_int)len, (u_char *)&blksfree[0], 1730 (u_char *)fragtbl[fs->fs_frag], 1731 (u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY)))); 1732 if (loc == 0) { 1733 printf("start = %d, len = %d, fs = %s\n", 1734 start, len, fs->fs_fsmnt); 1735 panic("ffs_alloccg: map corrupted"); 1736 /* NOTREACHED */ 1737 } 1738 } 1739 bno = (start + len - loc) * NBBY; 1740 cgp->cg_frotor = bno; 1741 /* 1742 * found the byte in the map 1743 * sift through the bits to find the selected frag 1744 */ 1745 for (i = bno + NBBY; bno < i; bno += fs->fs_frag) { 1746 blk = blkmap(fs, blksfree, bno); 1747 blk <<= 1; 1748 field = around[allocsiz]; 1749 subfield = inside[allocsiz]; 1750 for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) { 1751 if ((blk & field) == subfield) 1752 return (bno + pos); 1753 field <<= 1; 1754 subfield <<= 1; 1755 } 1756 } 1757 printf("bno = %lu, fs = %s\n", (u_long)bno, fs->fs_fsmnt); 1758 panic("ffs_alloccg: block not in map"); 1759 return (-1); 1760 } 1761 1762 /* 1763 * Update the cluster map because of an allocation or free. 1764 * 1765 * Cnt == 1 means free; cnt == -1 means allocating. 1766 */ 1767 void 1768 ffs_clusteracct(fs, cgp, blkno, cnt) 1769 struct fs *fs; 1770 struct cg *cgp; 1771 ufs_daddr_t blkno; 1772 int cnt; 1773 { 1774 int32_t *sump; 1775 int32_t *lp; 1776 u_char *freemapp, *mapp; 1777 int i, start, end, forw, back, map, bit; 1778 1779 if (fs->fs_contigsumsize <= 0) 1780 return; 1781 freemapp = cg_clustersfree(cgp); 1782 sump = cg_clustersum(cgp); 1783 /* 1784 * Allocate or clear the actual block. 1785 */ 1786 if (cnt > 0) 1787 setbit(freemapp, blkno); 1788 else 1789 clrbit(freemapp, blkno); 1790 /* 1791 * Find the size of the cluster going forward. 1792 */ 1793 start = blkno + 1; 1794 end = start + fs->fs_contigsumsize; 1795 if (end >= cgp->cg_nclusterblks) 1796 end = cgp->cg_nclusterblks; 1797 mapp = &freemapp[start / NBBY]; 1798 map = *mapp++; 1799 bit = 1 << (start % NBBY); 1800 for (i = start; i < end; i++) { 1801 if ((map & bit) == 0) 1802 break; 1803 if ((i & (NBBY - 1)) != (NBBY - 1)) { 1804 bit <<= 1; 1805 } else { 1806 map = *mapp++; 1807 bit = 1; 1808 } 1809 } 1810 forw = i - start; 1811 /* 1812 * Find the size of the cluster going backward. 1813 */ 1814 start = blkno - 1; 1815 end = start - fs->fs_contigsumsize; 1816 if (end < 0) 1817 end = -1; 1818 mapp = &freemapp[start / NBBY]; 1819 map = *mapp--; 1820 bit = 1 << (start % NBBY); 1821 for (i = start; i > end; i--) { 1822 if ((map & bit) == 0) 1823 break; 1824 if ((i & (NBBY - 1)) != 0) { 1825 bit >>= 1; 1826 } else { 1827 map = *mapp--; 1828 bit = 1 << (NBBY - 1); 1829 } 1830 } 1831 back = start - i; 1832 /* 1833 * Account for old cluster and the possibly new forward and 1834 * back clusters. 1835 */ 1836 i = back + forw + 1; 1837 if (i > fs->fs_contigsumsize) 1838 i = fs->fs_contigsumsize; 1839 sump[i] += cnt; 1840 if (back > 0) 1841 sump[back] -= cnt; 1842 if (forw > 0) 1843 sump[forw] -= cnt; 1844 /* 1845 * Update cluster summary information. 1846 */ 1847 lp = &sump[fs->fs_contigsumsize]; 1848 for (i = fs->fs_contigsumsize; i > 0; i--) 1849 if (*lp-- > 0) 1850 break; 1851 fs->fs_maxcluster[cgp->cg_cgx] = i; 1852 } 1853 1854 /* 1855 * Fserr prints the name of a file system with an error diagnostic. 1856 * 1857 * The form of the error message is: 1858 * fs: error message 1859 */ 1860 static void 1861 ffs_fserr(fs, inum, cp) 1862 struct fs *fs; 1863 ino_t inum; 1864 char *cp; 1865 { 1866 struct proc *p = curproc; /* XXX */ 1867 1868 log(LOG_ERR, "pid %d (%s), uid %d inumber %d on %s: %s\n", 1869 p ? p->p_pid : -1, p ? p->p_comm : "-", 1870 p ? p->p_ucred->cr_uid : 0, inum, fs->fs_fsmnt, cp); 1871 } 1872 1873 /* 1874 * This function provides the capability for the fsck program to 1875 * update an active filesystem. Six operations are provided: 1876 * 1877 * adjrefcnt(inode, amt) - adjusts the reference count on the 1878 * specified inode by the specified amount. Under normal 1879 * operation the count should always go down. Decrementing 1880 * the count to zero will cause the inode to be freed. 1881 * adjblkcnt(inode, amt) - adjust the number of blocks used to 1882 * by the specifed amount. 1883 * freedirs(inode, count) - directory inodes [inode..inode + count - 1] 1884 * are marked as free. Inodes should never have to be marked 1885 * as in use. 1886 * freefiles(inode, count) - file inodes [inode..inode + count - 1] 1887 * are marked as free. Inodes should never have to be marked 1888 * as in use. 1889 * freeblks(blockno, size) - blocks [blockno..blockno + size - 1] 1890 * are marked as free. Blocks should never have to be marked 1891 * as in use. 1892 * setflags(flags, set/clear) - the fs_flags field has the specified 1893 * flags set (second parameter +1) or cleared (second parameter -1). 1894 */ 1895 1896 static int sysctl_ffs_fsck __P((SYSCTL_HANDLER_ARGS)); 1897 1898 SYSCTL_PROC(_vfs_ffs, FFS_ADJ_REFCNT, adjrefcnt, CTLFLAG_WR|CTLTYPE_STRUCT, 1899 0, 0, sysctl_ffs_fsck, "S,fsck", "Adjust Inode Reference Count"); 1900 1901 SYSCTL_NODE(_vfs_ffs, FFS_ADJ_BLKCNT, adjblkcnt, CTLFLAG_WR, 1902 sysctl_ffs_fsck, "Adjust Inode Used Blocks Count"); 1903 1904 SYSCTL_NODE(_vfs_ffs, FFS_DIR_FREE, freedirs, CTLFLAG_WR, 1905 sysctl_ffs_fsck, "Free Range of Directory Inodes"); 1906 1907 SYSCTL_NODE(_vfs_ffs, FFS_FILE_FREE, freefiles, CTLFLAG_WR, 1908 sysctl_ffs_fsck, "Free Range of File Inodes"); 1909 1910 SYSCTL_NODE(_vfs_ffs, FFS_BLK_FREE, freeblks, CTLFLAG_WR, 1911 sysctl_ffs_fsck, "Free Range of Blocks"); 1912 1913 SYSCTL_NODE(_vfs_ffs, FFS_SET_FLAGS, setflags, CTLFLAG_WR, 1914 sysctl_ffs_fsck, "Change Filesystem Flags"); 1915 1916 #ifdef DEBUG 1917 static int fsckcmds = 0; 1918 SYSCTL_INT(_debug, OID_AUTO, fsckcmds, CTLFLAG_RW, &fsckcmds, 0, ""); 1919 #endif /* DEBUG */ 1920 1921 static int 1922 sysctl_ffs_fsck(SYSCTL_HANDLER_ARGS) 1923 { 1924 struct fsck_cmd cmd; 1925 struct ufsmount *ump; 1926 struct vnode *vp; 1927 struct inode *ip; 1928 struct mount *mp; 1929 struct fs *fs; 1930 ufs_daddr_t blkno; 1931 long blkcnt, blksize; 1932 struct file *fp; 1933 int filetype, error; 1934 1935 if (req->newlen > sizeof cmd) 1936 return (EBADRPC); 1937 if ((error = SYSCTL_IN(req, &cmd, sizeof cmd)) != 0) 1938 return (error); 1939 if (cmd.version != FFS_CMD_VERSION) 1940 return (ERPCMISMATCH); 1941 if ((error = getvnode(curproc->p_fd, cmd.handle, &fp)) != 0) 1942 return (error); 1943 vn_start_write((struct vnode *)fp->f_data, &mp, V_WAIT); 1944 if (mp == 0 || strncmp(mp->mnt_stat.f_fstypename, "ufs", MFSNAMELEN)) { 1945 vn_finished_write(mp); 1946 fdrop(fp, curthread); 1947 return (EINVAL); 1948 } 1949 if (mp->mnt_flag & MNT_RDONLY) { 1950 vn_finished_write(mp); 1951 fdrop(fp, curthread); 1952 return (EROFS); 1953 } 1954 ump = VFSTOUFS(mp); 1955 fs = ump->um_fs; 1956 filetype = IFREG; 1957 1958 switch (oidp->oid_number) { 1959 1960 case FFS_SET_FLAGS: 1961 #ifdef DEBUG 1962 if (fsckcmds) 1963 printf("%s: %s flags\n", mp->mnt_stat.f_mntonname, 1964 cmd.size > 0 ? "set" : "clear"); 1965 #endif /* DEBUG */ 1966 if (cmd.size > 0) 1967 fs->fs_flags |= (long)cmd.value; 1968 else 1969 fs->fs_flags &= ~(long)cmd.value; 1970 break; 1971 1972 case FFS_ADJ_REFCNT: 1973 #ifdef DEBUG 1974 if (fsckcmds) { 1975 printf("%s: adjust inode %d count by %ld\n", 1976 mp->mnt_stat.f_mntonname, (ino_t)cmd.value, 1977 cmd.size); 1978 } 1979 #endif /* DEBUG */ 1980 if ((error = VFS_VGET(mp, (ino_t)cmd.value, &vp)) != 0) 1981 break; 1982 ip = VTOI(vp); 1983 ip->i_nlink += cmd.size; 1984 ip->i_effnlink += cmd.size; 1985 ip->i_flag |= IN_CHANGE; 1986 if (DOINGSOFTDEP(vp)) 1987 softdep_change_linkcnt(ip); 1988 vput(vp); 1989 break; 1990 1991 case FFS_ADJ_BLKCNT: 1992 #ifdef DEBUG 1993 if (fsckcmds) { 1994 printf("%s: adjust inode %d block count by %ld\n", 1995 mp->mnt_stat.f_mntonname, (ino_t)cmd.value, 1996 cmd.size); 1997 } 1998 #endif /* DEBUG */ 1999 if ((error = VFS_VGET(mp, (ino_t)cmd.value, &vp)) != 0) 2000 break; 2001 ip = VTOI(vp); 2002 ip->i_blocks += cmd.size; 2003 ip->i_flag |= IN_CHANGE; 2004 vput(vp); 2005 break; 2006 2007 case FFS_DIR_FREE: 2008 filetype = IFDIR; 2009 /* fall through */ 2010 2011 case FFS_FILE_FREE: 2012 #ifdef DEBUG 2013 if (fsckcmds) { 2014 if (cmd.size == 1) 2015 printf("%s: free %s inode %d\n", 2016 mp->mnt_stat.f_mntonname, 2017 filetype == IFDIR ? "directory" : "file", 2018 (ino_t)cmd.value); 2019 else 2020 printf("%s: free %s inodes %d-%d\n", 2021 mp->mnt_stat.f_mntonname, 2022 filetype == IFDIR ? "directory" : "file", 2023 (ino_t)cmd.value, 2024 (ino_t)(cmd.value + cmd.size - 1)); 2025 } 2026 #endif /* DEBUG */ 2027 while (cmd.size > 0) { 2028 if ((error = ffs_freefile(fs, ump->um_devvp, cmd.value, 2029 filetype))) 2030 break; 2031 cmd.size -= 1; 2032 cmd.value += 1; 2033 } 2034 break; 2035 2036 case FFS_BLK_FREE: 2037 #ifdef DEBUG 2038 if (fsckcmds) { 2039 if (cmd.size == 1) 2040 printf("%s: free block %d\n", 2041 mp->mnt_stat.f_mntonname, 2042 (ufs_daddr_t)cmd.value); 2043 else 2044 printf("%s: free blocks %d-%ld\n", 2045 mp->mnt_stat.f_mntonname, 2046 (ufs_daddr_t)cmd.value, 2047 (ufs_daddr_t)cmd.value + cmd.size - 1); 2048 } 2049 #endif /* DEBUG */ 2050 blkno = (ufs_daddr_t)cmd.value; 2051 blkcnt = cmd.size; 2052 blksize = fs->fs_frag - (blkno % fs->fs_frag); 2053 while (blkcnt > 0) { 2054 if (blksize > blkcnt) 2055 blksize = blkcnt; 2056 ffs_blkfree(fs, ump->um_devvp, blkno, 2057 blksize * fs->fs_fsize, ROOTINO); 2058 blkno += blksize; 2059 blkcnt -= blksize; 2060 blksize = fs->fs_frag; 2061 } 2062 break; 2063 2064 default: 2065 #ifdef DEBUG 2066 if (fsckcmds) { 2067 printf("Invalid request %d from fsck\n", 2068 oidp->oid_number); 2069 } 2070 #endif /* DEBUG */ 2071 error = EINVAL; 2072 break; 2073 2074 } 2075 fdrop(fp, curthread); 2076 vn_finished_write(mp); 2077 return (error); 2078 } 2079