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