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/capability.h> 69 #include <sys/systm.h> 70 #include <sys/bio.h> 71 #include <sys/buf.h> 72 #include <sys/conf.h> 73 #include <sys/fcntl.h> 74 #include <sys/file.h> 75 #include <sys/filedesc.h> 76 #include <sys/priv.h> 77 #include <sys/proc.h> 78 #include <sys/vnode.h> 79 #include <sys/mount.h> 80 #include <sys/kernel.h> 81 #include <sys/syscallsubr.h> 82 #include <sys/sysctl.h> 83 #include <sys/syslog.h> 84 #include <sys/taskqueue.h> 85 86 #include <security/audit/audit.h> 87 88 #include <geom/geom.h> 89 90 #include <ufs/ufs/dir.h> 91 #include <ufs/ufs/extattr.h> 92 #include <ufs/ufs/quota.h> 93 #include <ufs/ufs/inode.h> 94 #include <ufs/ufs/ufs_extern.h> 95 #include <ufs/ufs/ufsmount.h> 96 97 #include <ufs/ffs/fs.h> 98 #include <ufs/ffs/ffs_extern.h> 99 #include <ufs/ffs/softdep.h> 100 101 typedef ufs2_daddr_t allocfcn_t(struct inode *ip, u_int cg, ufs2_daddr_t bpref, 102 int size, int rsize); 103 104 static ufs2_daddr_t ffs_alloccg(struct inode *, u_int, ufs2_daddr_t, int, int); 105 static ufs2_daddr_t 106 ffs_alloccgblk(struct inode *, struct buf *, ufs2_daddr_t, int); 107 static void ffs_blkfree_cg(struct ufsmount *, struct fs *, 108 struct vnode *, ufs2_daddr_t, long, ino_t, 109 struct workhead *); 110 static void ffs_blkfree_trim_completed(struct bio *); 111 static void ffs_blkfree_trim_task(void *ctx, int pending __unused); 112 #ifdef INVARIANTS 113 static int ffs_checkblk(struct inode *, ufs2_daddr_t, long); 114 #endif 115 static ufs2_daddr_t ffs_clusteralloc(struct inode *, u_int, ufs2_daddr_t, int, 116 int); 117 static ino_t ffs_dirpref(struct inode *); 118 static ufs2_daddr_t ffs_fragextend(struct inode *, u_int, ufs2_daddr_t, 119 int, int); 120 static ufs2_daddr_t ffs_hashalloc 121 (struct inode *, u_int, ufs2_daddr_t, int, int, allocfcn_t *); 122 static ufs2_daddr_t ffs_nodealloccg(struct inode *, u_int, ufs2_daddr_t, int, 123 int); 124 static ufs1_daddr_t ffs_mapsearch(struct fs *, struct cg *, ufs2_daddr_t, int); 125 static int ffs_reallocblks_ufs1(struct vop_reallocblks_args *); 126 static int ffs_reallocblks_ufs2(struct vop_reallocblks_args *); 127 128 /* 129 * Allocate a block in the filesystem. 130 * 131 * The size of the requested block is given, which must be some 132 * multiple of fs_fsize and <= fs_bsize. 133 * A preference may be optionally specified. If a preference is given 134 * the following hierarchy is used to allocate a block: 135 * 1) allocate the requested block. 136 * 2) allocate a rotationally optimal block in the same cylinder. 137 * 3) allocate a block in the same cylinder group. 138 * 4) quadradically rehash into other cylinder groups, until an 139 * available block is located. 140 * If no block preference is given the following hierarchy is used 141 * to allocate a block: 142 * 1) allocate a block in the cylinder group that contains the 143 * inode for the file. 144 * 2) quadradically rehash into other cylinder groups, until an 145 * available block is located. 146 */ 147 int 148 ffs_alloc(ip, lbn, bpref, size, flags, cred, bnp) 149 struct inode *ip; 150 ufs2_daddr_t lbn, bpref; 151 int size, flags; 152 struct ucred *cred; 153 ufs2_daddr_t *bnp; 154 { 155 struct fs *fs; 156 struct ufsmount *ump; 157 ufs2_daddr_t bno; 158 u_int cg, reclaimed; 159 static struct timeval lastfail; 160 static int curfail; 161 int64_t delta; 162 #ifdef QUOTA 163 int error; 164 #endif 165 166 *bnp = 0; 167 fs = ip->i_fs; 168 ump = ip->i_ump; 169 mtx_assert(UFS_MTX(ump), MA_OWNED); 170 #ifdef INVARIANTS 171 if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0) { 172 printf("dev = %s, bsize = %ld, size = %d, fs = %s\n", 173 devtoname(ip->i_dev), (long)fs->fs_bsize, size, 174 fs->fs_fsmnt); 175 panic("ffs_alloc: bad size"); 176 } 177 if (cred == NOCRED) 178 panic("ffs_alloc: missing credential"); 179 #endif /* INVARIANTS */ 180 reclaimed = 0; 181 retry: 182 #ifdef QUOTA 183 UFS_UNLOCK(ump); 184 error = chkdq(ip, btodb(size), cred, 0); 185 if (error) 186 return (error); 187 UFS_LOCK(ump); 188 #endif 189 if (size == fs->fs_bsize && fs->fs_cstotal.cs_nbfree == 0) 190 goto nospace; 191 if (priv_check_cred(cred, PRIV_VFS_BLOCKRESERVE, 0) && 192 freespace(fs, fs->fs_minfree) - numfrags(fs, size) < 0) 193 goto nospace; 194 if (bpref >= fs->fs_size) 195 bpref = 0; 196 if (bpref == 0) 197 cg = ino_to_cg(fs, ip->i_number); 198 else 199 cg = dtog(fs, bpref); 200 bno = ffs_hashalloc(ip, cg, bpref, size, size, ffs_alloccg); 201 if (bno > 0) { 202 delta = btodb(size); 203 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) + delta); 204 if (flags & IO_EXT) 205 ip->i_flag |= IN_CHANGE; 206 else 207 ip->i_flag |= IN_CHANGE | IN_UPDATE; 208 *bnp = bno; 209 return (0); 210 } 211 nospace: 212 #ifdef QUOTA 213 UFS_UNLOCK(ump); 214 /* 215 * Restore user's disk quota because allocation failed. 216 */ 217 (void) chkdq(ip, -btodb(size), cred, FORCE); 218 UFS_LOCK(ump); 219 #endif 220 if (reclaimed == 0 && (flags & IO_BUFLOCKED) == 0) { 221 reclaimed = 1; 222 softdep_request_cleanup(fs, ITOV(ip), cred, FLUSH_BLOCKS_WAIT); 223 goto retry; 224 } 225 UFS_UNLOCK(ump); 226 if (reclaimed > 0 && ppsratecheck(&lastfail, &curfail, 1)) { 227 ffs_fserr(fs, ip->i_number, "filesystem full"); 228 uprintf("\n%s: write failed, filesystem is full\n", 229 fs->fs_fsmnt); 230 } 231 return (ENOSPC); 232 } 233 234 /* 235 * Reallocate a fragment to a bigger size 236 * 237 * The number and size of the old block is given, and a preference 238 * and new size is also specified. The allocator attempts to extend 239 * the original block. Failing that, the regular block allocator is 240 * invoked to get an appropriate block. 241 */ 242 int 243 ffs_realloccg(ip, lbprev, bprev, bpref, osize, nsize, flags, cred, bpp) 244 struct inode *ip; 245 ufs2_daddr_t lbprev; 246 ufs2_daddr_t bprev; 247 ufs2_daddr_t bpref; 248 int osize, nsize, flags; 249 struct ucred *cred; 250 struct buf **bpp; 251 { 252 struct vnode *vp; 253 struct fs *fs; 254 struct buf *bp; 255 struct ufsmount *ump; 256 u_int cg, request, reclaimed; 257 int error; 258 ufs2_daddr_t bno; 259 static struct timeval lastfail; 260 static int curfail; 261 int64_t delta; 262 263 *bpp = 0; 264 vp = ITOV(ip); 265 fs = ip->i_fs; 266 bp = NULL; 267 ump = ip->i_ump; 268 mtx_assert(UFS_MTX(ump), MA_OWNED); 269 #ifdef INVARIANTS 270 if (vp->v_mount->mnt_kern_flag & MNTK_SUSPENDED) 271 panic("ffs_realloccg: allocation on suspended filesystem"); 272 if ((u_int)osize > fs->fs_bsize || fragoff(fs, osize) != 0 || 273 (u_int)nsize > fs->fs_bsize || fragoff(fs, nsize) != 0) { 274 printf( 275 "dev = %s, bsize = %ld, osize = %d, nsize = %d, fs = %s\n", 276 devtoname(ip->i_dev), (long)fs->fs_bsize, osize, 277 nsize, fs->fs_fsmnt); 278 panic("ffs_realloccg: bad size"); 279 } 280 if (cred == NOCRED) 281 panic("ffs_realloccg: missing credential"); 282 #endif /* INVARIANTS */ 283 reclaimed = 0; 284 retry: 285 if (priv_check_cred(cred, PRIV_VFS_BLOCKRESERVE, 0) && 286 freespace(fs, fs->fs_minfree) - numfrags(fs, nsize - osize) < 0) { 287 goto nospace; 288 } 289 if (bprev == 0) { 290 printf("dev = %s, bsize = %ld, bprev = %jd, fs = %s\n", 291 devtoname(ip->i_dev), (long)fs->fs_bsize, (intmax_t)bprev, 292 fs->fs_fsmnt); 293 panic("ffs_realloccg: bad bprev"); 294 } 295 UFS_UNLOCK(ump); 296 /* 297 * Allocate the extra space in the buffer. 298 */ 299 error = bread(vp, lbprev, osize, NOCRED, &bp); 300 if (error) { 301 brelse(bp); 302 return (error); 303 } 304 305 if (bp->b_blkno == bp->b_lblkno) { 306 if (lbprev >= NDADDR) 307 panic("ffs_realloccg: lbprev out of range"); 308 bp->b_blkno = fsbtodb(fs, bprev); 309 } 310 311 #ifdef QUOTA 312 error = chkdq(ip, btodb(nsize - osize), cred, 0); 313 if (error) { 314 brelse(bp); 315 return (error); 316 } 317 #endif 318 /* 319 * Check for extension in the existing location. 320 */ 321 cg = dtog(fs, bprev); 322 UFS_LOCK(ump); 323 bno = ffs_fragextend(ip, cg, bprev, osize, nsize); 324 if (bno) { 325 if (bp->b_blkno != fsbtodb(fs, bno)) 326 panic("ffs_realloccg: bad blockno"); 327 delta = btodb(nsize - osize); 328 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) + delta); 329 if (flags & IO_EXT) 330 ip->i_flag |= IN_CHANGE; 331 else 332 ip->i_flag |= IN_CHANGE | IN_UPDATE; 333 allocbuf(bp, nsize); 334 bp->b_flags |= B_DONE; 335 bzero(bp->b_data + osize, nsize - osize); 336 if ((bp->b_flags & (B_MALLOC | B_VMIO)) == B_VMIO) 337 vfs_bio_set_valid(bp, osize, nsize - osize); 338 *bpp = bp; 339 return (0); 340 } 341 /* 342 * Allocate a new disk location. 343 */ 344 if (bpref >= fs->fs_size) 345 bpref = 0; 346 switch ((int)fs->fs_optim) { 347 case FS_OPTSPACE: 348 /* 349 * Allocate an exact sized fragment. Although this makes 350 * best use of space, we will waste time relocating it if 351 * the file continues to grow. If the fragmentation is 352 * less than half of the minimum free reserve, we choose 353 * to begin optimizing for time. 354 */ 355 request = nsize; 356 if (fs->fs_minfree <= 5 || 357 fs->fs_cstotal.cs_nffree > 358 (off_t)fs->fs_dsize * fs->fs_minfree / (2 * 100)) 359 break; 360 log(LOG_NOTICE, "%s: optimization changed from SPACE to TIME\n", 361 fs->fs_fsmnt); 362 fs->fs_optim = FS_OPTTIME; 363 break; 364 case FS_OPTTIME: 365 /* 366 * At this point we have discovered a file that is trying to 367 * grow a small fragment to a larger fragment. To save time, 368 * we allocate a full sized block, then free the unused portion. 369 * If the file continues to grow, the `ffs_fragextend' call 370 * above will be able to grow it in place without further 371 * copying. If aberrant programs cause disk fragmentation to 372 * grow within 2% of the free reserve, we choose to begin 373 * optimizing for space. 374 */ 375 request = fs->fs_bsize; 376 if (fs->fs_cstotal.cs_nffree < 377 (off_t)fs->fs_dsize * (fs->fs_minfree - 2) / 100) 378 break; 379 log(LOG_NOTICE, "%s: optimization changed from TIME to SPACE\n", 380 fs->fs_fsmnt); 381 fs->fs_optim = FS_OPTSPACE; 382 break; 383 default: 384 printf("dev = %s, optim = %ld, fs = %s\n", 385 devtoname(ip->i_dev), (long)fs->fs_optim, fs->fs_fsmnt); 386 panic("ffs_realloccg: bad optim"); 387 /* NOTREACHED */ 388 } 389 bno = ffs_hashalloc(ip, cg, bpref, request, nsize, ffs_alloccg); 390 if (bno > 0) { 391 bp->b_blkno = fsbtodb(fs, bno); 392 if (!DOINGSOFTDEP(vp)) 393 ffs_blkfree(ump, fs, ip->i_devvp, bprev, (long)osize, 394 ip->i_number, vp->v_type, NULL); 395 delta = btodb(nsize - osize); 396 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) + delta); 397 if (flags & IO_EXT) 398 ip->i_flag |= IN_CHANGE; 399 else 400 ip->i_flag |= IN_CHANGE | IN_UPDATE; 401 allocbuf(bp, nsize); 402 bp->b_flags |= B_DONE; 403 bzero(bp->b_data + osize, nsize - osize); 404 if ((bp->b_flags & (B_MALLOC | B_VMIO)) == B_VMIO) 405 vfs_bio_set_valid(bp, osize, nsize - osize); 406 *bpp = bp; 407 return (0); 408 } 409 #ifdef QUOTA 410 UFS_UNLOCK(ump); 411 /* 412 * Restore user's disk quota because allocation failed. 413 */ 414 (void) chkdq(ip, -btodb(nsize - osize), cred, FORCE); 415 UFS_LOCK(ump); 416 #endif 417 nospace: 418 /* 419 * no space available 420 */ 421 if (reclaimed == 0 && (flags & IO_BUFLOCKED) == 0) { 422 reclaimed = 1; 423 UFS_UNLOCK(ump); 424 if (bp) { 425 brelse(bp); 426 bp = NULL; 427 } 428 UFS_LOCK(ump); 429 softdep_request_cleanup(fs, vp, cred, FLUSH_BLOCKS_WAIT); 430 goto retry; 431 } 432 UFS_UNLOCK(ump); 433 if (bp) 434 brelse(bp); 435 if (reclaimed > 0 && ppsratecheck(&lastfail, &curfail, 1)) { 436 ffs_fserr(fs, ip->i_number, "filesystem full"); 437 uprintf("\n%s: write failed, filesystem is full\n", 438 fs->fs_fsmnt); 439 } 440 return (ENOSPC); 441 } 442 443 /* 444 * Reallocate a sequence of blocks into a contiguous sequence of blocks. 445 * 446 * The vnode and an array of buffer pointers for a range of sequential 447 * logical blocks to be made contiguous is given. The allocator attempts 448 * to find a range of sequential blocks starting as close as possible 449 * from the end of the allocation for the logical block immediately 450 * preceding the current range. If successful, the physical block numbers 451 * in the buffer pointers and in the inode are changed to reflect the new 452 * allocation. If unsuccessful, the allocation is left unchanged. The 453 * success in doing the reallocation is returned. Note that the error 454 * return is not reflected back to the user. Rather the previous block 455 * allocation will be used. 456 */ 457 458 SYSCTL_NODE(_vfs, OID_AUTO, ffs, CTLFLAG_RW, 0, "FFS filesystem"); 459 460 static int doasyncfree = 1; 461 SYSCTL_INT(_vfs_ffs, OID_AUTO, doasyncfree, CTLFLAG_RW, &doasyncfree, 0, ""); 462 463 static int doreallocblks = 1; 464 SYSCTL_INT(_vfs_ffs, OID_AUTO, doreallocblks, CTLFLAG_RW, &doreallocblks, 0, ""); 465 466 #ifdef DEBUG 467 static volatile int prtrealloc = 0; 468 #endif 469 470 int 471 ffs_reallocblks(ap) 472 struct vop_reallocblks_args /* { 473 struct vnode *a_vp; 474 struct cluster_save *a_buflist; 475 } */ *ap; 476 { 477 478 if (doreallocblks == 0) 479 return (ENOSPC); 480 /* 481 * We can't wait in softdep prealloc as it may fsync and recurse 482 * here. Instead we simply fail to reallocate blocks if this 483 * rare condition arises. 484 */ 485 if (DOINGSOFTDEP(ap->a_vp)) 486 if (softdep_prealloc(ap->a_vp, MNT_NOWAIT) != 0) 487 return (ENOSPC); 488 if (VTOI(ap->a_vp)->i_ump->um_fstype == UFS1) 489 return (ffs_reallocblks_ufs1(ap)); 490 return (ffs_reallocblks_ufs2(ap)); 491 } 492 493 static int 494 ffs_reallocblks_ufs1(ap) 495 struct vop_reallocblks_args /* { 496 struct vnode *a_vp; 497 struct cluster_save *a_buflist; 498 } */ *ap; 499 { 500 struct fs *fs; 501 struct inode *ip; 502 struct vnode *vp; 503 struct buf *sbp, *ebp; 504 ufs1_daddr_t *bap, *sbap, *ebap = 0; 505 struct cluster_save *buflist; 506 struct ufsmount *ump; 507 ufs_lbn_t start_lbn, end_lbn; 508 ufs1_daddr_t soff, newblk, blkno; 509 ufs2_daddr_t pref; 510 struct indir start_ap[NIADDR + 1], end_ap[NIADDR + 1], *idp; 511 int i, len, start_lvl, end_lvl, ssize; 512 513 vp = ap->a_vp; 514 ip = VTOI(vp); 515 fs = ip->i_fs; 516 ump = ip->i_ump; 517 if (fs->fs_contigsumsize <= 0) 518 return (ENOSPC); 519 buflist = ap->a_buflist; 520 len = buflist->bs_nchildren; 521 start_lbn = buflist->bs_children[0]->b_lblkno; 522 end_lbn = start_lbn + len - 1; 523 #ifdef INVARIANTS 524 for (i = 0; i < len; i++) 525 if (!ffs_checkblk(ip, 526 dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize)) 527 panic("ffs_reallocblks: unallocated block 1"); 528 for (i = 1; i < len; i++) 529 if (buflist->bs_children[i]->b_lblkno != start_lbn + i) 530 panic("ffs_reallocblks: non-logical cluster"); 531 blkno = buflist->bs_children[0]->b_blkno; 532 ssize = fsbtodb(fs, fs->fs_frag); 533 for (i = 1; i < len - 1; i++) 534 if (buflist->bs_children[i]->b_blkno != blkno + (i * ssize)) 535 panic("ffs_reallocblks: non-physical cluster %d", i); 536 #endif 537 /* 538 * If the latest allocation is in a new cylinder group, assume that 539 * the filesystem has decided to move and do not force it back to 540 * the previous cylinder group. 541 */ 542 if (dtog(fs, dbtofsb(fs, buflist->bs_children[0]->b_blkno)) != 543 dtog(fs, dbtofsb(fs, buflist->bs_children[len - 1]->b_blkno))) 544 return (ENOSPC); 545 if (ufs_getlbns(vp, start_lbn, start_ap, &start_lvl) || 546 ufs_getlbns(vp, end_lbn, end_ap, &end_lvl)) 547 return (ENOSPC); 548 /* 549 * Get the starting offset and block map for the first block. 550 */ 551 if (start_lvl == 0) { 552 sbap = &ip->i_din1->di_db[0]; 553 soff = start_lbn; 554 } else { 555 idp = &start_ap[start_lvl - 1]; 556 if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &sbp)) { 557 brelse(sbp); 558 return (ENOSPC); 559 } 560 sbap = (ufs1_daddr_t *)sbp->b_data; 561 soff = idp->in_off; 562 } 563 /* 564 * If the block range spans two block maps, get the second map. 565 */ 566 if (end_lvl == 0 || (idp = &end_ap[end_lvl - 1])->in_off + 1 >= len) { 567 ssize = len; 568 } else { 569 #ifdef INVARIANTS 570 if (start_lvl > 0 && 571 start_ap[start_lvl - 1].in_lbn == idp->in_lbn) 572 panic("ffs_reallocblk: start == end"); 573 #endif 574 ssize = len - (idp->in_off + 1); 575 if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &ebp)) 576 goto fail; 577 ebap = (ufs1_daddr_t *)ebp->b_data; 578 } 579 /* 580 * Find the preferred location for the cluster. 581 */ 582 UFS_LOCK(ump); 583 pref = ffs_blkpref_ufs1(ip, start_lbn, soff, sbap); 584 /* 585 * Search the block map looking for an allocation of the desired size. 586 */ 587 if ((newblk = ffs_hashalloc(ip, dtog(fs, pref), pref, 588 len, len, ffs_clusteralloc)) == 0) { 589 UFS_UNLOCK(ump); 590 goto fail; 591 } 592 /* 593 * We have found a new contiguous block. 594 * 595 * First we have to replace the old block pointers with the new 596 * block pointers in the inode and indirect blocks associated 597 * with the file. 598 */ 599 #ifdef DEBUG 600 if (prtrealloc) 601 printf("realloc: ino %ju, lbns %jd-%jd\n\told:", 602 (uintmax_t)ip->i_number, 603 (intmax_t)start_lbn, (intmax_t)end_lbn); 604 #endif 605 blkno = newblk; 606 for (bap = &sbap[soff], i = 0; i < len; i++, blkno += fs->fs_frag) { 607 if (i == ssize) { 608 bap = ebap; 609 soff = -i; 610 } 611 #ifdef INVARIANTS 612 if (!ffs_checkblk(ip, 613 dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize)) 614 panic("ffs_reallocblks: unallocated block 2"); 615 if (dbtofsb(fs, buflist->bs_children[i]->b_blkno) != *bap) 616 panic("ffs_reallocblks: alloc mismatch"); 617 #endif 618 #ifdef DEBUG 619 if (prtrealloc) 620 printf(" %d,", *bap); 621 #endif 622 if (DOINGSOFTDEP(vp)) { 623 if (sbap == &ip->i_din1->di_db[0] && i < ssize) 624 softdep_setup_allocdirect(ip, start_lbn + i, 625 blkno, *bap, fs->fs_bsize, fs->fs_bsize, 626 buflist->bs_children[i]); 627 else 628 softdep_setup_allocindir_page(ip, start_lbn + i, 629 i < ssize ? sbp : ebp, soff + i, blkno, 630 *bap, buflist->bs_children[i]); 631 } 632 *bap++ = blkno; 633 } 634 /* 635 * Next we must write out the modified inode and indirect blocks. 636 * For strict correctness, the writes should be synchronous since 637 * the old block values may have been written to disk. In practise 638 * they are almost never written, but if we are concerned about 639 * strict correctness, the `doasyncfree' flag should be set to zero. 640 * 641 * The test on `doasyncfree' should be changed to test a flag 642 * that shows whether the associated buffers and inodes have 643 * been written. The flag should be set when the cluster is 644 * started and cleared whenever the buffer or inode is flushed. 645 * We can then check below to see if it is set, and do the 646 * synchronous write only when it has been cleared. 647 */ 648 if (sbap != &ip->i_din1->di_db[0]) { 649 if (doasyncfree) 650 bdwrite(sbp); 651 else 652 bwrite(sbp); 653 } else { 654 ip->i_flag |= IN_CHANGE | IN_UPDATE; 655 if (!doasyncfree) 656 ffs_update(vp, 1); 657 } 658 if (ssize < len) { 659 if (doasyncfree) 660 bdwrite(ebp); 661 else 662 bwrite(ebp); 663 } 664 /* 665 * Last, free the old blocks and assign the new blocks to the buffers. 666 */ 667 #ifdef DEBUG 668 if (prtrealloc) 669 printf("\n\tnew:"); 670 #endif 671 for (blkno = newblk, i = 0; i < len; i++, blkno += fs->fs_frag) { 672 if (!DOINGSOFTDEP(vp)) 673 ffs_blkfree(ump, fs, ip->i_devvp, 674 dbtofsb(fs, buflist->bs_children[i]->b_blkno), 675 fs->fs_bsize, ip->i_number, vp->v_type, NULL); 676 buflist->bs_children[i]->b_blkno = fsbtodb(fs, blkno); 677 #ifdef INVARIANTS 678 if (!ffs_checkblk(ip, 679 dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize)) 680 panic("ffs_reallocblks: unallocated block 3"); 681 #endif 682 #ifdef DEBUG 683 if (prtrealloc) 684 printf(" %d,", blkno); 685 #endif 686 } 687 #ifdef DEBUG 688 if (prtrealloc) { 689 prtrealloc--; 690 printf("\n"); 691 } 692 #endif 693 return (0); 694 695 fail: 696 if (ssize < len) 697 brelse(ebp); 698 if (sbap != &ip->i_din1->di_db[0]) 699 brelse(sbp); 700 return (ENOSPC); 701 } 702 703 static int 704 ffs_reallocblks_ufs2(ap) 705 struct vop_reallocblks_args /* { 706 struct vnode *a_vp; 707 struct cluster_save *a_buflist; 708 } */ *ap; 709 { 710 struct fs *fs; 711 struct inode *ip; 712 struct vnode *vp; 713 struct buf *sbp, *ebp; 714 ufs2_daddr_t *bap, *sbap, *ebap = 0; 715 struct cluster_save *buflist; 716 struct ufsmount *ump; 717 ufs_lbn_t start_lbn, end_lbn; 718 ufs2_daddr_t soff, newblk, blkno, pref; 719 struct indir start_ap[NIADDR + 1], end_ap[NIADDR + 1], *idp; 720 int i, len, start_lvl, end_lvl, ssize; 721 722 vp = ap->a_vp; 723 ip = VTOI(vp); 724 fs = ip->i_fs; 725 ump = ip->i_ump; 726 if (fs->fs_contigsumsize <= 0) 727 return (ENOSPC); 728 buflist = ap->a_buflist; 729 len = buflist->bs_nchildren; 730 start_lbn = buflist->bs_children[0]->b_lblkno; 731 end_lbn = start_lbn + len - 1; 732 #ifdef INVARIANTS 733 for (i = 0; i < len; i++) 734 if (!ffs_checkblk(ip, 735 dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize)) 736 panic("ffs_reallocblks: unallocated block 1"); 737 for (i = 1; i < len; i++) 738 if (buflist->bs_children[i]->b_lblkno != start_lbn + i) 739 panic("ffs_reallocblks: non-logical cluster"); 740 blkno = buflist->bs_children[0]->b_blkno; 741 ssize = fsbtodb(fs, fs->fs_frag); 742 for (i = 1; i < len - 1; i++) 743 if (buflist->bs_children[i]->b_blkno != blkno + (i * ssize)) 744 panic("ffs_reallocblks: non-physical cluster %d", i); 745 #endif 746 /* 747 * If the latest allocation is in a new cylinder group, assume that 748 * the filesystem has decided to move and do not force it back to 749 * the previous cylinder group. 750 */ 751 if (dtog(fs, dbtofsb(fs, buflist->bs_children[0]->b_blkno)) != 752 dtog(fs, dbtofsb(fs, buflist->bs_children[len - 1]->b_blkno))) 753 return (ENOSPC); 754 if (ufs_getlbns(vp, start_lbn, start_ap, &start_lvl) || 755 ufs_getlbns(vp, end_lbn, end_ap, &end_lvl)) 756 return (ENOSPC); 757 /* 758 * Get the starting offset and block map for the first block. 759 */ 760 if (start_lvl == 0) { 761 sbap = &ip->i_din2->di_db[0]; 762 soff = start_lbn; 763 } else { 764 idp = &start_ap[start_lvl - 1]; 765 if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &sbp)) { 766 brelse(sbp); 767 return (ENOSPC); 768 } 769 sbap = (ufs2_daddr_t *)sbp->b_data; 770 soff = idp->in_off; 771 } 772 /* 773 * If the block range spans two block maps, get the second map. 774 */ 775 if (end_lvl == 0 || (idp = &end_ap[end_lvl - 1])->in_off + 1 >= len) { 776 ssize = len; 777 } else { 778 #ifdef INVARIANTS 779 if (start_lvl > 0 && 780 start_ap[start_lvl - 1].in_lbn == idp->in_lbn) 781 panic("ffs_reallocblk: start == end"); 782 #endif 783 ssize = len - (idp->in_off + 1); 784 if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &ebp)) 785 goto fail; 786 ebap = (ufs2_daddr_t *)ebp->b_data; 787 } 788 /* 789 * Find the preferred location for the cluster. 790 */ 791 UFS_LOCK(ump); 792 pref = ffs_blkpref_ufs2(ip, start_lbn, soff, sbap); 793 /* 794 * Search the block map looking for an allocation of the desired size. 795 */ 796 if ((newblk = ffs_hashalloc(ip, dtog(fs, pref), pref, 797 len, len, ffs_clusteralloc)) == 0) { 798 UFS_UNLOCK(ump); 799 goto fail; 800 } 801 /* 802 * We have found a new contiguous block. 803 * 804 * First we have to replace the old block pointers with the new 805 * block pointers in the inode and indirect blocks associated 806 * with the file. 807 */ 808 #ifdef DEBUG 809 if (prtrealloc) 810 printf("realloc: ino %d, lbns %jd-%jd\n\told:", ip->i_number, 811 (intmax_t)start_lbn, (intmax_t)end_lbn); 812 #endif 813 blkno = newblk; 814 for (bap = &sbap[soff], i = 0; i < len; i++, blkno += fs->fs_frag) { 815 if (i == ssize) { 816 bap = ebap; 817 soff = -i; 818 } 819 #ifdef INVARIANTS 820 if (!ffs_checkblk(ip, 821 dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize)) 822 panic("ffs_reallocblks: unallocated block 2"); 823 if (dbtofsb(fs, buflist->bs_children[i]->b_blkno) != *bap) 824 panic("ffs_reallocblks: alloc mismatch"); 825 #endif 826 #ifdef DEBUG 827 if (prtrealloc) 828 printf(" %jd,", (intmax_t)*bap); 829 #endif 830 if (DOINGSOFTDEP(vp)) { 831 if (sbap == &ip->i_din2->di_db[0] && i < ssize) 832 softdep_setup_allocdirect(ip, start_lbn + i, 833 blkno, *bap, fs->fs_bsize, fs->fs_bsize, 834 buflist->bs_children[i]); 835 else 836 softdep_setup_allocindir_page(ip, start_lbn + i, 837 i < ssize ? sbp : ebp, soff + i, blkno, 838 *bap, buflist->bs_children[i]); 839 } 840 *bap++ = blkno; 841 } 842 /* 843 * Next we must write out the modified inode and indirect blocks. 844 * For strict correctness, the writes should be synchronous since 845 * the old block values may have been written to disk. In practise 846 * they are almost never written, but if we are concerned about 847 * strict correctness, the `doasyncfree' flag should be set to zero. 848 * 849 * The test on `doasyncfree' should be changed to test a flag 850 * that shows whether the associated buffers and inodes have 851 * been written. The flag should be set when the cluster is 852 * started and cleared whenever the buffer or inode is flushed. 853 * We can then check below to see if it is set, and do the 854 * synchronous write only when it has been cleared. 855 */ 856 if (sbap != &ip->i_din2->di_db[0]) { 857 if (doasyncfree) 858 bdwrite(sbp); 859 else 860 bwrite(sbp); 861 } else { 862 ip->i_flag |= IN_CHANGE | IN_UPDATE; 863 if (!doasyncfree) 864 ffs_update(vp, 1); 865 } 866 if (ssize < len) { 867 if (doasyncfree) 868 bdwrite(ebp); 869 else 870 bwrite(ebp); 871 } 872 /* 873 * Last, free the old blocks and assign the new blocks to the buffers. 874 */ 875 #ifdef DEBUG 876 if (prtrealloc) 877 printf("\n\tnew:"); 878 #endif 879 for (blkno = newblk, i = 0; i < len; i++, blkno += fs->fs_frag) { 880 if (!DOINGSOFTDEP(vp)) 881 ffs_blkfree(ump, fs, ip->i_devvp, 882 dbtofsb(fs, buflist->bs_children[i]->b_blkno), 883 fs->fs_bsize, ip->i_number, vp->v_type, NULL); 884 buflist->bs_children[i]->b_blkno = fsbtodb(fs, blkno); 885 #ifdef INVARIANTS 886 if (!ffs_checkblk(ip, 887 dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize)) 888 panic("ffs_reallocblks: unallocated block 3"); 889 #endif 890 #ifdef DEBUG 891 if (prtrealloc) 892 printf(" %jd,", (intmax_t)blkno); 893 #endif 894 } 895 #ifdef DEBUG 896 if (prtrealloc) { 897 prtrealloc--; 898 printf("\n"); 899 } 900 #endif 901 return (0); 902 903 fail: 904 if (ssize < len) 905 brelse(ebp); 906 if (sbap != &ip->i_din2->di_db[0]) 907 brelse(sbp); 908 return (ENOSPC); 909 } 910 911 /* 912 * Allocate an inode in the filesystem. 913 * 914 * If allocating a directory, use ffs_dirpref to select the inode. 915 * If allocating in a directory, the following hierarchy is followed: 916 * 1) allocate the preferred inode. 917 * 2) allocate an inode in the same cylinder group. 918 * 3) quadradically rehash into other cylinder groups, until an 919 * available inode is located. 920 * If no inode preference is given the following hierarchy is used 921 * to allocate an inode: 922 * 1) allocate an inode in cylinder group 0. 923 * 2) quadradically rehash into other cylinder groups, until an 924 * available inode is located. 925 */ 926 int 927 ffs_valloc(pvp, mode, cred, vpp) 928 struct vnode *pvp; 929 int mode; 930 struct ucred *cred; 931 struct vnode **vpp; 932 { 933 struct inode *pip; 934 struct fs *fs; 935 struct inode *ip; 936 struct timespec ts; 937 struct ufsmount *ump; 938 ino_t ino, ipref; 939 u_int cg; 940 int error, error1, reclaimed; 941 static struct timeval lastfail; 942 static int curfail; 943 944 *vpp = NULL; 945 pip = VTOI(pvp); 946 fs = pip->i_fs; 947 ump = pip->i_ump; 948 949 UFS_LOCK(ump); 950 reclaimed = 0; 951 retry: 952 if (fs->fs_cstotal.cs_nifree == 0) 953 goto noinodes; 954 955 if ((mode & IFMT) == IFDIR) 956 ipref = ffs_dirpref(pip); 957 else 958 ipref = pip->i_number; 959 if (ipref >= fs->fs_ncg * fs->fs_ipg) 960 ipref = 0; 961 cg = ino_to_cg(fs, ipref); 962 /* 963 * Track number of dirs created one after another 964 * in a same cg without intervening by files. 965 */ 966 if ((mode & IFMT) == IFDIR) { 967 if (fs->fs_contigdirs[cg] < 255) 968 fs->fs_contigdirs[cg]++; 969 } else { 970 if (fs->fs_contigdirs[cg] > 0) 971 fs->fs_contigdirs[cg]--; 972 } 973 ino = (ino_t)ffs_hashalloc(pip, cg, ipref, mode, 0, 974 (allocfcn_t *)ffs_nodealloccg); 975 if (ino == 0) 976 goto noinodes; 977 error = ffs_vget(pvp->v_mount, ino, LK_EXCLUSIVE, vpp); 978 if (error) { 979 error1 = ffs_vgetf(pvp->v_mount, ino, LK_EXCLUSIVE, vpp, 980 FFSV_FORCEINSMQ); 981 ffs_vfree(pvp, ino, mode); 982 if (error1 == 0) { 983 ip = VTOI(*vpp); 984 if (ip->i_mode) 985 goto dup_alloc; 986 ip->i_flag |= IN_MODIFIED; 987 vput(*vpp); 988 } 989 return (error); 990 } 991 ip = VTOI(*vpp); 992 if (ip->i_mode) { 993 dup_alloc: 994 printf("mode = 0%o, inum = %lu, fs = %s\n", 995 ip->i_mode, (u_long)ip->i_number, fs->fs_fsmnt); 996 panic("ffs_valloc: dup alloc"); 997 } 998 if (DIP(ip, i_blocks) && (fs->fs_flags & FS_UNCLEAN) == 0) { /* XXX */ 999 printf("free inode %s/%lu had %ld blocks\n", 1000 fs->fs_fsmnt, (u_long)ino, (long)DIP(ip, i_blocks)); 1001 DIP_SET(ip, i_blocks, 0); 1002 } 1003 ip->i_flags = 0; 1004 DIP_SET(ip, i_flags, 0); 1005 /* 1006 * Set up a new generation number for this inode. 1007 */ 1008 if (ip->i_gen == 0 || ++ip->i_gen == 0) 1009 ip->i_gen = arc4random() / 2 + 1; 1010 DIP_SET(ip, i_gen, ip->i_gen); 1011 if (fs->fs_magic == FS_UFS2_MAGIC) { 1012 vfs_timestamp(&ts); 1013 ip->i_din2->di_birthtime = ts.tv_sec; 1014 ip->i_din2->di_birthnsec = ts.tv_nsec; 1015 } 1016 ufs_prepare_reclaim(*vpp); 1017 ip->i_flag = 0; 1018 (*vpp)->v_vflag = 0; 1019 (*vpp)->v_type = VNON; 1020 if (fs->fs_magic == FS_UFS2_MAGIC) 1021 (*vpp)->v_op = &ffs_vnodeops2; 1022 else 1023 (*vpp)->v_op = &ffs_vnodeops1; 1024 return (0); 1025 noinodes: 1026 if (reclaimed == 0) { 1027 reclaimed = 1; 1028 softdep_request_cleanup(fs, pvp, cred, FLUSH_INODES_WAIT); 1029 goto retry; 1030 } 1031 UFS_UNLOCK(ump); 1032 if (ppsratecheck(&lastfail, &curfail, 1)) { 1033 ffs_fserr(fs, pip->i_number, "out of inodes"); 1034 uprintf("\n%s: create/symlink failed, no inodes free\n", 1035 fs->fs_fsmnt); 1036 } 1037 return (ENOSPC); 1038 } 1039 1040 /* 1041 * Find a cylinder group to place a directory. 1042 * 1043 * The policy implemented by this algorithm is to allocate a 1044 * directory inode in the same cylinder group as its parent 1045 * directory, but also to reserve space for its files inodes 1046 * and data. Restrict the number of directories which may be 1047 * allocated one after another in the same cylinder group 1048 * without intervening allocation of files. 1049 * 1050 * If we allocate a first level directory then force allocation 1051 * in another cylinder group. 1052 */ 1053 static ino_t 1054 ffs_dirpref(pip) 1055 struct inode *pip; 1056 { 1057 struct fs *fs; 1058 u_int cg, prefcg, dirsize, cgsize; 1059 u_int avgifree, avgbfree, avgndir, curdirsize; 1060 u_int minifree, minbfree, maxndir; 1061 u_int mincg, minndir; 1062 u_int maxcontigdirs; 1063 1064 mtx_assert(UFS_MTX(pip->i_ump), MA_OWNED); 1065 fs = pip->i_fs; 1066 1067 avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg; 1068 avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg; 1069 avgndir = fs->fs_cstotal.cs_ndir / fs->fs_ncg; 1070 1071 /* 1072 * Force allocation in another cg if creating a first level dir. 1073 */ 1074 ASSERT_VOP_LOCKED(ITOV(pip), "ffs_dirpref"); 1075 if (ITOV(pip)->v_vflag & VV_ROOT) { 1076 prefcg = arc4random() % fs->fs_ncg; 1077 mincg = prefcg; 1078 minndir = fs->fs_ipg; 1079 for (cg = prefcg; cg < fs->fs_ncg; cg++) 1080 if (fs->fs_cs(fs, cg).cs_ndir < minndir && 1081 fs->fs_cs(fs, cg).cs_nifree >= avgifree && 1082 fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { 1083 mincg = cg; 1084 minndir = fs->fs_cs(fs, cg).cs_ndir; 1085 } 1086 for (cg = 0; cg < prefcg; cg++) 1087 if (fs->fs_cs(fs, cg).cs_ndir < minndir && 1088 fs->fs_cs(fs, cg).cs_nifree >= avgifree && 1089 fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { 1090 mincg = cg; 1091 minndir = fs->fs_cs(fs, cg).cs_ndir; 1092 } 1093 return ((ino_t)(fs->fs_ipg * mincg)); 1094 } 1095 1096 /* 1097 * Count various limits which used for 1098 * optimal allocation of a directory inode. 1099 */ 1100 maxndir = min(avgndir + fs->fs_ipg / 16, fs->fs_ipg); 1101 minifree = avgifree - avgifree / 4; 1102 if (minifree < 1) 1103 minifree = 1; 1104 minbfree = avgbfree - avgbfree / 4; 1105 if (minbfree < 1) 1106 minbfree = 1; 1107 cgsize = fs->fs_fsize * fs->fs_fpg; 1108 dirsize = fs->fs_avgfilesize * fs->fs_avgfpdir; 1109 curdirsize = avgndir ? (cgsize - avgbfree * fs->fs_bsize) / avgndir : 0; 1110 if (dirsize < curdirsize) 1111 dirsize = curdirsize; 1112 if (dirsize <= 0) 1113 maxcontigdirs = 0; /* dirsize overflowed */ 1114 else 1115 maxcontigdirs = min((avgbfree * fs->fs_bsize) / dirsize, 255); 1116 if (fs->fs_avgfpdir > 0) 1117 maxcontigdirs = min(maxcontigdirs, 1118 fs->fs_ipg / fs->fs_avgfpdir); 1119 if (maxcontigdirs == 0) 1120 maxcontigdirs = 1; 1121 1122 /* 1123 * Limit number of dirs in one cg and reserve space for 1124 * regular files, but only if we have no deficit in 1125 * inodes or space. 1126 */ 1127 prefcg = ino_to_cg(fs, pip->i_number); 1128 for (cg = prefcg; cg < fs->fs_ncg; cg++) 1129 if (fs->fs_cs(fs, cg).cs_ndir < maxndir && 1130 fs->fs_cs(fs, cg).cs_nifree >= minifree && 1131 fs->fs_cs(fs, cg).cs_nbfree >= minbfree) { 1132 if (fs->fs_contigdirs[cg] < maxcontigdirs) 1133 return ((ino_t)(fs->fs_ipg * cg)); 1134 } 1135 for (cg = 0; cg < prefcg; cg++) 1136 if (fs->fs_cs(fs, cg).cs_ndir < maxndir && 1137 fs->fs_cs(fs, cg).cs_nifree >= minifree && 1138 fs->fs_cs(fs, cg).cs_nbfree >= minbfree) { 1139 if (fs->fs_contigdirs[cg] < maxcontigdirs) 1140 return ((ino_t)(fs->fs_ipg * cg)); 1141 } 1142 /* 1143 * This is a backstop when we have deficit in space. 1144 */ 1145 for (cg = prefcg; cg < fs->fs_ncg; cg++) 1146 if (fs->fs_cs(fs, cg).cs_nifree >= avgifree) 1147 return ((ino_t)(fs->fs_ipg * cg)); 1148 for (cg = 0; cg < prefcg; cg++) 1149 if (fs->fs_cs(fs, cg).cs_nifree >= avgifree) 1150 break; 1151 return ((ino_t)(fs->fs_ipg * cg)); 1152 } 1153 1154 /* 1155 * Select the desired position for the next block in a file. The file is 1156 * logically divided into sections. The first section is composed of the 1157 * direct blocks. Each additional section contains fs_maxbpg blocks. 1158 * 1159 * If no blocks have been allocated in the first section, the policy is to 1160 * request a block in the same cylinder group as the inode that describes 1161 * the file. If no blocks have been allocated in any other section, the 1162 * policy is to place the section in a cylinder group with a greater than 1163 * average number of free blocks. An appropriate cylinder group is found 1164 * by using a rotor that sweeps the cylinder groups. When a new group of 1165 * blocks is needed, the sweep begins in the cylinder group following the 1166 * cylinder group from which the previous allocation was made. The sweep 1167 * continues until a cylinder group with greater than the average number 1168 * of free blocks is found. If the allocation is for the first block in an 1169 * indirect block, the information on the previous allocation is unavailable; 1170 * here a best guess is made based upon the logical block number being 1171 * allocated. 1172 * 1173 * If a section is already partially allocated, the policy is to 1174 * contiguously allocate fs_maxcontig blocks. The end of one of these 1175 * contiguous blocks and the beginning of the next is laid out 1176 * contiguously if possible. 1177 */ 1178 ufs2_daddr_t 1179 ffs_blkpref_ufs1(ip, lbn, indx, bap) 1180 struct inode *ip; 1181 ufs_lbn_t lbn; 1182 int indx; 1183 ufs1_daddr_t *bap; 1184 { 1185 struct fs *fs; 1186 u_int cg; 1187 u_int avgbfree, startcg; 1188 1189 mtx_assert(UFS_MTX(ip->i_ump), MA_OWNED); 1190 fs = ip->i_fs; 1191 if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) { 1192 if (lbn < NDADDR + NINDIR(fs)) { 1193 cg = ino_to_cg(fs, ip->i_number); 1194 return (cgbase(fs, cg) + fs->fs_frag); 1195 } 1196 /* 1197 * Find a cylinder with greater than average number of 1198 * unused data blocks. 1199 */ 1200 if (indx == 0 || bap[indx - 1] == 0) 1201 startcg = 1202 ino_to_cg(fs, ip->i_number) + lbn / fs->fs_maxbpg; 1203 else 1204 startcg = dtog(fs, bap[indx - 1]) + 1; 1205 startcg %= fs->fs_ncg; 1206 avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg; 1207 for (cg = startcg; cg < fs->fs_ncg; cg++) 1208 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { 1209 fs->fs_cgrotor = cg; 1210 return (cgbase(fs, cg) + fs->fs_frag); 1211 } 1212 for (cg = 0; cg <= startcg; cg++) 1213 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { 1214 fs->fs_cgrotor = cg; 1215 return (cgbase(fs, cg) + fs->fs_frag); 1216 } 1217 return (0); 1218 } 1219 /* 1220 * We just always try to lay things out contiguously. 1221 */ 1222 return (bap[indx - 1] + fs->fs_frag); 1223 } 1224 1225 /* 1226 * Same as above, but for UFS2 1227 */ 1228 ufs2_daddr_t 1229 ffs_blkpref_ufs2(ip, lbn, indx, bap) 1230 struct inode *ip; 1231 ufs_lbn_t lbn; 1232 int indx; 1233 ufs2_daddr_t *bap; 1234 { 1235 struct fs *fs; 1236 u_int cg; 1237 u_int avgbfree, startcg; 1238 1239 mtx_assert(UFS_MTX(ip->i_ump), MA_OWNED); 1240 fs = ip->i_fs; 1241 if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) { 1242 if (lbn < NDADDR + NINDIR(fs)) { 1243 cg = ino_to_cg(fs, ip->i_number); 1244 return (cgbase(fs, cg) + fs->fs_frag); 1245 } 1246 /* 1247 * Find a cylinder with greater than average number of 1248 * unused data blocks. 1249 */ 1250 if (indx == 0 || bap[indx - 1] == 0) 1251 startcg = 1252 ino_to_cg(fs, ip->i_number) + lbn / fs->fs_maxbpg; 1253 else 1254 startcg = dtog(fs, bap[indx - 1]) + 1; 1255 startcg %= fs->fs_ncg; 1256 avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg; 1257 for (cg = startcg; cg < fs->fs_ncg; cg++) 1258 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { 1259 fs->fs_cgrotor = cg; 1260 return (cgbase(fs, cg) + fs->fs_frag); 1261 } 1262 for (cg = 0; cg <= startcg; cg++) 1263 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { 1264 fs->fs_cgrotor = cg; 1265 return (cgbase(fs, cg) + fs->fs_frag); 1266 } 1267 return (0); 1268 } 1269 /* 1270 * We just always try to lay things out contiguously. 1271 */ 1272 return (bap[indx - 1] + fs->fs_frag); 1273 } 1274 1275 /* 1276 * Implement the cylinder overflow algorithm. 1277 * 1278 * The policy implemented by this algorithm is: 1279 * 1) allocate the block in its requested cylinder group. 1280 * 2) quadradically rehash on the cylinder group number. 1281 * 3) brute force search for a free block. 1282 * 1283 * Must be called with the UFS lock held. Will release the lock on success 1284 * and return with it held on failure. 1285 */ 1286 /*VARARGS5*/ 1287 static ufs2_daddr_t 1288 ffs_hashalloc(ip, cg, pref, size, rsize, allocator) 1289 struct inode *ip; 1290 u_int cg; 1291 ufs2_daddr_t pref; 1292 int size; /* Search size for data blocks, mode for inodes */ 1293 int rsize; /* Real allocated size. */ 1294 allocfcn_t *allocator; 1295 { 1296 struct fs *fs; 1297 ufs2_daddr_t result; 1298 u_int i, icg = cg; 1299 1300 mtx_assert(UFS_MTX(ip->i_ump), MA_OWNED); 1301 #ifdef INVARIANTS 1302 if (ITOV(ip)->v_mount->mnt_kern_flag & MNTK_SUSPENDED) 1303 panic("ffs_hashalloc: allocation on suspended filesystem"); 1304 #endif 1305 fs = ip->i_fs; 1306 /* 1307 * 1: preferred cylinder group 1308 */ 1309 result = (*allocator)(ip, cg, pref, size, rsize); 1310 if (result) 1311 return (result); 1312 /* 1313 * 2: quadratic rehash 1314 */ 1315 for (i = 1; i < fs->fs_ncg; i *= 2) { 1316 cg += i; 1317 if (cg >= fs->fs_ncg) 1318 cg -= fs->fs_ncg; 1319 result = (*allocator)(ip, cg, 0, size, rsize); 1320 if (result) 1321 return (result); 1322 } 1323 /* 1324 * 3: brute force search 1325 * Note that we start at i == 2, since 0 was checked initially, 1326 * and 1 is always checked in the quadratic rehash. 1327 */ 1328 cg = (icg + 2) % fs->fs_ncg; 1329 for (i = 2; i < fs->fs_ncg; i++) { 1330 result = (*allocator)(ip, cg, 0, size, rsize); 1331 if (result) 1332 return (result); 1333 cg++; 1334 if (cg == fs->fs_ncg) 1335 cg = 0; 1336 } 1337 return (0); 1338 } 1339 1340 /* 1341 * Determine whether a fragment can be extended. 1342 * 1343 * Check to see if the necessary fragments are available, and 1344 * if they are, allocate them. 1345 */ 1346 static ufs2_daddr_t 1347 ffs_fragextend(ip, cg, bprev, osize, nsize) 1348 struct inode *ip; 1349 u_int cg; 1350 ufs2_daddr_t bprev; 1351 int osize, nsize; 1352 { 1353 struct fs *fs; 1354 struct cg *cgp; 1355 struct buf *bp; 1356 struct ufsmount *ump; 1357 int nffree; 1358 long bno; 1359 int frags, bbase; 1360 int i, error; 1361 u_int8_t *blksfree; 1362 1363 ump = ip->i_ump; 1364 fs = ip->i_fs; 1365 if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize)) 1366 return (0); 1367 frags = numfrags(fs, nsize); 1368 bbase = fragnum(fs, bprev); 1369 if (bbase > fragnum(fs, (bprev + frags - 1))) { 1370 /* cannot extend across a block boundary */ 1371 return (0); 1372 } 1373 UFS_UNLOCK(ump); 1374 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 1375 (int)fs->fs_cgsize, NOCRED, &bp); 1376 if (error) 1377 goto fail; 1378 cgp = (struct cg *)bp->b_data; 1379 if (!cg_chkmagic(cgp)) 1380 goto fail; 1381 bp->b_xflags |= BX_BKGRDWRITE; 1382 cgp->cg_old_time = cgp->cg_time = time_second; 1383 bno = dtogd(fs, bprev); 1384 blksfree = cg_blksfree(cgp); 1385 for (i = numfrags(fs, osize); i < frags; i++) 1386 if (isclr(blksfree, bno + i)) 1387 goto fail; 1388 /* 1389 * the current fragment can be extended 1390 * deduct the count on fragment being extended into 1391 * increase the count on the remaining fragment (if any) 1392 * allocate the extended piece 1393 */ 1394 for (i = frags; i < fs->fs_frag - bbase; i++) 1395 if (isclr(blksfree, bno + i)) 1396 break; 1397 cgp->cg_frsum[i - numfrags(fs, osize)]--; 1398 if (i != frags) 1399 cgp->cg_frsum[i - frags]++; 1400 for (i = numfrags(fs, osize), nffree = 0; i < frags; i++) { 1401 clrbit(blksfree, bno + i); 1402 cgp->cg_cs.cs_nffree--; 1403 nffree++; 1404 } 1405 UFS_LOCK(ump); 1406 fs->fs_cstotal.cs_nffree -= nffree; 1407 fs->fs_cs(fs, cg).cs_nffree -= nffree; 1408 fs->fs_fmod = 1; 1409 ACTIVECLEAR(fs, cg); 1410 UFS_UNLOCK(ump); 1411 if (DOINGSOFTDEP(ITOV(ip))) 1412 softdep_setup_blkmapdep(bp, UFSTOVFS(ump), bprev, 1413 frags, numfrags(fs, osize)); 1414 bdwrite(bp); 1415 return (bprev); 1416 1417 fail: 1418 brelse(bp); 1419 UFS_LOCK(ump); 1420 return (0); 1421 1422 } 1423 1424 /* 1425 * Determine whether a block can be allocated. 1426 * 1427 * Check to see if a block of the appropriate size is available, 1428 * and if it is, allocate it. 1429 */ 1430 static ufs2_daddr_t 1431 ffs_alloccg(ip, cg, bpref, size, rsize) 1432 struct inode *ip; 1433 u_int cg; 1434 ufs2_daddr_t bpref; 1435 int size; 1436 int rsize; 1437 { 1438 struct fs *fs; 1439 struct cg *cgp; 1440 struct buf *bp; 1441 struct ufsmount *ump; 1442 ufs1_daddr_t bno; 1443 ufs2_daddr_t blkno; 1444 int i, allocsiz, error, frags; 1445 u_int8_t *blksfree; 1446 1447 ump = ip->i_ump; 1448 fs = ip->i_fs; 1449 if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize) 1450 return (0); 1451 UFS_UNLOCK(ump); 1452 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 1453 (int)fs->fs_cgsize, NOCRED, &bp); 1454 if (error) 1455 goto fail; 1456 cgp = (struct cg *)bp->b_data; 1457 if (!cg_chkmagic(cgp) || 1458 (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) 1459 goto fail; 1460 bp->b_xflags |= BX_BKGRDWRITE; 1461 cgp->cg_old_time = cgp->cg_time = time_second; 1462 if (size == fs->fs_bsize) { 1463 UFS_LOCK(ump); 1464 blkno = ffs_alloccgblk(ip, bp, bpref, rsize); 1465 ACTIVECLEAR(fs, cg); 1466 UFS_UNLOCK(ump); 1467 bdwrite(bp); 1468 return (blkno); 1469 } 1470 /* 1471 * check to see if any fragments are already available 1472 * allocsiz is the size which will be allocated, hacking 1473 * it down to a smaller size if necessary 1474 */ 1475 blksfree = cg_blksfree(cgp); 1476 frags = numfrags(fs, size); 1477 for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++) 1478 if (cgp->cg_frsum[allocsiz] != 0) 1479 break; 1480 if (allocsiz == fs->fs_frag) { 1481 /* 1482 * no fragments were available, so a block will be 1483 * allocated, and hacked up 1484 */ 1485 if (cgp->cg_cs.cs_nbfree == 0) 1486 goto fail; 1487 UFS_LOCK(ump); 1488 blkno = ffs_alloccgblk(ip, bp, bpref, rsize); 1489 ACTIVECLEAR(fs, cg); 1490 UFS_UNLOCK(ump); 1491 bdwrite(bp); 1492 return (blkno); 1493 } 1494 KASSERT(size == rsize, 1495 ("ffs_alloccg: size(%d) != rsize(%d)", size, rsize)); 1496 bno = ffs_mapsearch(fs, cgp, bpref, allocsiz); 1497 if (bno < 0) 1498 goto fail; 1499 for (i = 0; i < frags; i++) 1500 clrbit(blksfree, bno + i); 1501 cgp->cg_cs.cs_nffree -= frags; 1502 cgp->cg_frsum[allocsiz]--; 1503 if (frags != allocsiz) 1504 cgp->cg_frsum[allocsiz - frags]++; 1505 UFS_LOCK(ump); 1506 fs->fs_cstotal.cs_nffree -= frags; 1507 fs->fs_cs(fs, cg).cs_nffree -= frags; 1508 fs->fs_fmod = 1; 1509 blkno = cgbase(fs, cg) + bno; 1510 ACTIVECLEAR(fs, cg); 1511 UFS_UNLOCK(ump); 1512 if (DOINGSOFTDEP(ITOV(ip))) 1513 softdep_setup_blkmapdep(bp, UFSTOVFS(ump), blkno, frags, 0); 1514 bdwrite(bp); 1515 return (blkno); 1516 1517 fail: 1518 brelse(bp); 1519 UFS_LOCK(ump); 1520 return (0); 1521 } 1522 1523 /* 1524 * Allocate a block in a cylinder group. 1525 * 1526 * This algorithm implements the following policy: 1527 * 1) allocate the requested block. 1528 * 2) allocate a rotationally optimal block in the same cylinder. 1529 * 3) allocate the next available block on the block rotor for the 1530 * specified cylinder group. 1531 * Note that this routine only allocates fs_bsize blocks; these 1532 * blocks may be fragmented by the routine that allocates them. 1533 */ 1534 static ufs2_daddr_t 1535 ffs_alloccgblk(ip, bp, bpref, size) 1536 struct inode *ip; 1537 struct buf *bp; 1538 ufs2_daddr_t bpref; 1539 int size; 1540 { 1541 struct fs *fs; 1542 struct cg *cgp; 1543 struct ufsmount *ump; 1544 ufs1_daddr_t bno; 1545 ufs2_daddr_t blkno; 1546 u_int8_t *blksfree; 1547 int i; 1548 1549 fs = ip->i_fs; 1550 ump = ip->i_ump; 1551 mtx_assert(UFS_MTX(ump), MA_OWNED); 1552 cgp = (struct cg *)bp->b_data; 1553 blksfree = cg_blksfree(cgp); 1554 if (bpref == 0 || dtog(fs, bpref) != cgp->cg_cgx) { 1555 bpref = cgp->cg_rotor; 1556 } else { 1557 bpref = blknum(fs, bpref); 1558 bno = dtogd(fs, bpref); 1559 /* 1560 * if the requested block is available, use it 1561 */ 1562 if (ffs_isblock(fs, blksfree, fragstoblks(fs, bno))) 1563 goto gotit; 1564 } 1565 /* 1566 * Take the next available block in this cylinder group. 1567 */ 1568 bno = ffs_mapsearch(fs, cgp, bpref, (int)fs->fs_frag); 1569 if (bno < 0) 1570 return (0); 1571 cgp->cg_rotor = bno; 1572 gotit: 1573 blkno = fragstoblks(fs, bno); 1574 ffs_clrblock(fs, blksfree, (long)blkno); 1575 ffs_clusteracct(fs, cgp, blkno, -1); 1576 cgp->cg_cs.cs_nbfree--; 1577 fs->fs_cstotal.cs_nbfree--; 1578 fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--; 1579 fs->fs_fmod = 1; 1580 blkno = cgbase(fs, cgp->cg_cgx) + bno; 1581 /* 1582 * If the caller didn't want the whole block free the frags here. 1583 */ 1584 size = numfrags(fs, size); 1585 if (size != fs->fs_frag) { 1586 bno = dtogd(fs, blkno); 1587 for (i = size; i < fs->fs_frag; i++) 1588 setbit(blksfree, bno + i); 1589 i = fs->fs_frag - size; 1590 cgp->cg_cs.cs_nffree += i; 1591 fs->fs_cstotal.cs_nffree += i; 1592 fs->fs_cs(fs, cgp->cg_cgx).cs_nffree += i; 1593 fs->fs_fmod = 1; 1594 cgp->cg_frsum[i]++; 1595 } 1596 /* XXX Fixme. */ 1597 UFS_UNLOCK(ump); 1598 if (DOINGSOFTDEP(ITOV(ip))) 1599 softdep_setup_blkmapdep(bp, UFSTOVFS(ump), blkno, 1600 size, 0); 1601 UFS_LOCK(ump); 1602 return (blkno); 1603 } 1604 1605 /* 1606 * Determine whether a cluster can be allocated. 1607 * 1608 * We do not currently check for optimal rotational layout if there 1609 * are multiple choices in the same cylinder group. Instead we just 1610 * take the first one that we find following bpref. 1611 */ 1612 static ufs2_daddr_t 1613 ffs_clusteralloc(ip, cg, bpref, len, unused) 1614 struct inode *ip; 1615 u_int cg; 1616 ufs2_daddr_t bpref; 1617 int len; 1618 int unused; 1619 { 1620 struct fs *fs; 1621 struct cg *cgp; 1622 struct buf *bp; 1623 struct ufsmount *ump; 1624 int i, run, bit, map, got; 1625 ufs2_daddr_t bno; 1626 u_char *mapp; 1627 int32_t *lp; 1628 u_int8_t *blksfree; 1629 1630 fs = ip->i_fs; 1631 ump = ip->i_ump; 1632 if (fs->fs_maxcluster[cg] < len) 1633 return (0); 1634 UFS_UNLOCK(ump); 1635 if (bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize, 1636 NOCRED, &bp)) 1637 goto fail_lock; 1638 cgp = (struct cg *)bp->b_data; 1639 if (!cg_chkmagic(cgp)) 1640 goto fail_lock; 1641 bp->b_xflags |= BX_BKGRDWRITE; 1642 /* 1643 * Check to see if a cluster of the needed size (or bigger) is 1644 * available in this cylinder group. 1645 */ 1646 lp = &cg_clustersum(cgp)[len]; 1647 for (i = len; i <= fs->fs_contigsumsize; i++) 1648 if (*lp++ > 0) 1649 break; 1650 if (i > fs->fs_contigsumsize) { 1651 /* 1652 * This is the first time looking for a cluster in this 1653 * cylinder group. Update the cluster summary information 1654 * to reflect the true maximum sized cluster so that 1655 * future cluster allocation requests can avoid reading 1656 * the cylinder group map only to find no clusters. 1657 */ 1658 lp = &cg_clustersum(cgp)[len - 1]; 1659 for (i = len - 1; i > 0; i--) 1660 if (*lp-- > 0) 1661 break; 1662 UFS_LOCK(ump); 1663 fs->fs_maxcluster[cg] = i; 1664 goto fail; 1665 } 1666 /* 1667 * Search the cluster map to find a big enough cluster. 1668 * We take the first one that we find, even if it is larger 1669 * than we need as we prefer to get one close to the previous 1670 * block allocation. We do not search before the current 1671 * preference point as we do not want to allocate a block 1672 * that is allocated before the previous one (as we will 1673 * then have to wait for another pass of the elevator 1674 * algorithm before it will be read). We prefer to fail and 1675 * be recalled to try an allocation in the next cylinder group. 1676 */ 1677 if (dtog(fs, bpref) != cg) 1678 bpref = 0; 1679 else 1680 bpref = fragstoblks(fs, dtogd(fs, blknum(fs, bpref))); 1681 mapp = &cg_clustersfree(cgp)[bpref / NBBY]; 1682 map = *mapp++; 1683 bit = 1 << (bpref % NBBY); 1684 for (run = 0, got = bpref; got < cgp->cg_nclusterblks; got++) { 1685 if ((map & bit) == 0) { 1686 run = 0; 1687 } else { 1688 run++; 1689 if (run == len) 1690 break; 1691 } 1692 if ((got & (NBBY - 1)) != (NBBY - 1)) { 1693 bit <<= 1; 1694 } else { 1695 map = *mapp++; 1696 bit = 1; 1697 } 1698 } 1699 if (got >= cgp->cg_nclusterblks) 1700 goto fail_lock; 1701 /* 1702 * Allocate the cluster that we have found. 1703 */ 1704 blksfree = cg_blksfree(cgp); 1705 for (i = 1; i <= len; i++) 1706 if (!ffs_isblock(fs, blksfree, got - run + i)) 1707 panic("ffs_clusteralloc: map mismatch"); 1708 bno = cgbase(fs, cg) + blkstofrags(fs, got - run + 1); 1709 if (dtog(fs, bno) != cg) 1710 panic("ffs_clusteralloc: allocated out of group"); 1711 len = blkstofrags(fs, len); 1712 UFS_LOCK(ump); 1713 for (i = 0; i < len; i += fs->fs_frag) 1714 if (ffs_alloccgblk(ip, bp, bno + i, fs->fs_bsize) != bno + i) 1715 panic("ffs_clusteralloc: lost block"); 1716 ACTIVECLEAR(fs, cg); 1717 UFS_UNLOCK(ump); 1718 bdwrite(bp); 1719 return (bno); 1720 1721 fail_lock: 1722 UFS_LOCK(ump); 1723 fail: 1724 brelse(bp); 1725 return (0); 1726 } 1727 1728 /* 1729 * Determine whether an inode can be allocated. 1730 * 1731 * Check to see if an inode is available, and if it is, 1732 * allocate it using the following policy: 1733 * 1) allocate the requested inode. 1734 * 2) allocate the next available inode after the requested 1735 * inode in the specified cylinder group. 1736 */ 1737 static ufs2_daddr_t 1738 ffs_nodealloccg(ip, cg, ipref, mode, unused) 1739 struct inode *ip; 1740 u_int cg; 1741 ufs2_daddr_t ipref; 1742 int mode; 1743 int unused; 1744 { 1745 struct fs *fs; 1746 struct cg *cgp; 1747 struct buf *bp, *ibp; 1748 struct ufsmount *ump; 1749 u_int8_t *inosused, *loc; 1750 struct ufs2_dinode *dp2; 1751 int error, start, len, i; 1752 1753 fs = ip->i_fs; 1754 ump = ip->i_ump; 1755 if (fs->fs_cs(fs, cg).cs_nifree == 0) 1756 return (0); 1757 UFS_UNLOCK(ump); 1758 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 1759 (int)fs->fs_cgsize, NOCRED, &bp); 1760 if (error) { 1761 brelse(bp); 1762 UFS_LOCK(ump); 1763 return (0); 1764 } 1765 cgp = (struct cg *)bp->b_data; 1766 if (!cg_chkmagic(cgp) || cgp->cg_cs.cs_nifree == 0) { 1767 brelse(bp); 1768 UFS_LOCK(ump); 1769 return (0); 1770 } 1771 bp->b_xflags |= BX_BKGRDWRITE; 1772 cgp->cg_old_time = cgp->cg_time = time_second; 1773 inosused = cg_inosused(cgp); 1774 if (ipref) { 1775 ipref %= fs->fs_ipg; 1776 if (isclr(inosused, ipref)) 1777 goto gotit; 1778 } 1779 start = cgp->cg_irotor / NBBY; 1780 len = howmany(fs->fs_ipg - cgp->cg_irotor, NBBY); 1781 loc = memcchr(&inosused[start], 0xff, len); 1782 if (loc == NULL) { 1783 len = start + 1; 1784 start = 0; 1785 loc = memcchr(&inosused[start], 0xff, len); 1786 if (loc == NULL) { 1787 printf("cg = %d, irotor = %ld, fs = %s\n", 1788 cg, (long)cgp->cg_irotor, fs->fs_fsmnt); 1789 panic("ffs_nodealloccg: map corrupted"); 1790 /* NOTREACHED */ 1791 } 1792 } 1793 ipref = (loc - inosused) * NBBY + ffs(~*loc) - 1; 1794 cgp->cg_irotor = ipref; 1795 gotit: 1796 /* 1797 * Check to see if we need to initialize more inodes. 1798 */ 1799 ibp = NULL; 1800 if (fs->fs_magic == FS_UFS2_MAGIC && 1801 ipref + INOPB(fs) > cgp->cg_initediblk && 1802 cgp->cg_initediblk < cgp->cg_niblk) { 1803 ibp = getblk(ip->i_devvp, fsbtodb(fs, 1804 ino_to_fsba(fs, cg * fs->fs_ipg + cgp->cg_initediblk)), 1805 (int)fs->fs_bsize, 0, 0, 0); 1806 bzero(ibp->b_data, (int)fs->fs_bsize); 1807 dp2 = (struct ufs2_dinode *)(ibp->b_data); 1808 for (i = 0; i < INOPB(fs); i++) { 1809 dp2->di_gen = arc4random() / 2 + 1; 1810 dp2++; 1811 } 1812 cgp->cg_initediblk += INOPB(fs); 1813 } 1814 UFS_LOCK(ump); 1815 ACTIVECLEAR(fs, cg); 1816 setbit(inosused, ipref); 1817 cgp->cg_cs.cs_nifree--; 1818 fs->fs_cstotal.cs_nifree--; 1819 fs->fs_cs(fs, cg).cs_nifree--; 1820 fs->fs_fmod = 1; 1821 if ((mode & IFMT) == IFDIR) { 1822 cgp->cg_cs.cs_ndir++; 1823 fs->fs_cstotal.cs_ndir++; 1824 fs->fs_cs(fs, cg).cs_ndir++; 1825 } 1826 UFS_UNLOCK(ump); 1827 if (DOINGSOFTDEP(ITOV(ip))) 1828 softdep_setup_inomapdep(bp, ip, cg * fs->fs_ipg + ipref, mode); 1829 bdwrite(bp); 1830 if (ibp != NULL) 1831 bawrite(ibp); 1832 return ((ino_t)(cg * fs->fs_ipg + ipref)); 1833 } 1834 1835 /* 1836 * Free a block or fragment. 1837 * 1838 * The specified block or fragment is placed back in the 1839 * free map. If a fragment is deallocated, a possible 1840 * block reassembly is checked. 1841 */ 1842 static void 1843 ffs_blkfree_cg(ump, fs, devvp, bno, size, inum, dephd) 1844 struct ufsmount *ump; 1845 struct fs *fs; 1846 struct vnode *devvp; 1847 ufs2_daddr_t bno; 1848 long size; 1849 ino_t inum; 1850 struct workhead *dephd; 1851 { 1852 struct mount *mp; 1853 struct cg *cgp; 1854 struct buf *bp; 1855 ufs1_daddr_t fragno, cgbno; 1856 ufs2_daddr_t cgblkno; 1857 int i, blk, frags, bbase; 1858 u_int cg; 1859 u_int8_t *blksfree; 1860 struct cdev *dev; 1861 1862 cg = dtog(fs, bno); 1863 if (devvp->v_type == VREG) { 1864 /* devvp is a snapshot */ 1865 dev = VTOI(devvp)->i_devvp->v_rdev; 1866 cgblkno = fragstoblks(fs, cgtod(fs, cg)); 1867 } else { 1868 /* devvp is a normal disk device */ 1869 dev = devvp->v_rdev; 1870 cgblkno = fsbtodb(fs, cgtod(fs, cg)); 1871 ASSERT_VOP_LOCKED(devvp, "ffs_blkfree_cg"); 1872 } 1873 #ifdef INVARIANTS 1874 if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0 || 1875 fragnum(fs, bno) + numfrags(fs, size) > fs->fs_frag) { 1876 printf("dev=%s, bno = %jd, bsize = %ld, size = %ld, fs = %s\n", 1877 devtoname(dev), (intmax_t)bno, (long)fs->fs_bsize, 1878 size, fs->fs_fsmnt); 1879 panic("ffs_blkfree_cg: bad size"); 1880 } 1881 #endif 1882 if ((u_int)bno >= fs->fs_size) { 1883 printf("bad block %jd, ino %lu\n", (intmax_t)bno, 1884 (u_long)inum); 1885 ffs_fserr(fs, inum, "bad block"); 1886 return; 1887 } 1888 if (bread(devvp, cgblkno, (int)fs->fs_cgsize, NOCRED, &bp)) { 1889 brelse(bp); 1890 return; 1891 } 1892 cgp = (struct cg *)bp->b_data; 1893 if (!cg_chkmagic(cgp)) { 1894 brelse(bp); 1895 return; 1896 } 1897 bp->b_xflags |= BX_BKGRDWRITE; 1898 cgp->cg_old_time = cgp->cg_time = time_second; 1899 cgbno = dtogd(fs, bno); 1900 blksfree = cg_blksfree(cgp); 1901 UFS_LOCK(ump); 1902 if (size == fs->fs_bsize) { 1903 fragno = fragstoblks(fs, cgbno); 1904 if (!ffs_isfreeblock(fs, blksfree, fragno)) { 1905 if (devvp->v_type == VREG) { 1906 UFS_UNLOCK(ump); 1907 /* devvp is a snapshot */ 1908 brelse(bp); 1909 return; 1910 } 1911 printf("dev = %s, block = %jd, fs = %s\n", 1912 devtoname(dev), (intmax_t)bno, fs->fs_fsmnt); 1913 panic("ffs_blkfree_cg: freeing free block"); 1914 } 1915 ffs_setblock(fs, blksfree, fragno); 1916 ffs_clusteracct(fs, cgp, fragno, 1); 1917 cgp->cg_cs.cs_nbfree++; 1918 fs->fs_cstotal.cs_nbfree++; 1919 fs->fs_cs(fs, cg).cs_nbfree++; 1920 } else { 1921 bbase = cgbno - fragnum(fs, cgbno); 1922 /* 1923 * decrement the counts associated with the old frags 1924 */ 1925 blk = blkmap(fs, blksfree, bbase); 1926 ffs_fragacct(fs, blk, cgp->cg_frsum, -1); 1927 /* 1928 * deallocate the fragment 1929 */ 1930 frags = numfrags(fs, size); 1931 for (i = 0; i < frags; i++) { 1932 if (isset(blksfree, cgbno + i)) { 1933 printf("dev = %s, block = %jd, fs = %s\n", 1934 devtoname(dev), (intmax_t)(bno + i), 1935 fs->fs_fsmnt); 1936 panic("ffs_blkfree_cg: freeing free frag"); 1937 } 1938 setbit(blksfree, cgbno + i); 1939 } 1940 cgp->cg_cs.cs_nffree += i; 1941 fs->fs_cstotal.cs_nffree += i; 1942 fs->fs_cs(fs, cg).cs_nffree += i; 1943 /* 1944 * add back in counts associated with the new frags 1945 */ 1946 blk = blkmap(fs, blksfree, bbase); 1947 ffs_fragacct(fs, blk, cgp->cg_frsum, 1); 1948 /* 1949 * if a complete block has been reassembled, account for it 1950 */ 1951 fragno = fragstoblks(fs, bbase); 1952 if (ffs_isblock(fs, blksfree, fragno)) { 1953 cgp->cg_cs.cs_nffree -= fs->fs_frag; 1954 fs->fs_cstotal.cs_nffree -= fs->fs_frag; 1955 fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag; 1956 ffs_clusteracct(fs, cgp, fragno, 1); 1957 cgp->cg_cs.cs_nbfree++; 1958 fs->fs_cstotal.cs_nbfree++; 1959 fs->fs_cs(fs, cg).cs_nbfree++; 1960 } 1961 } 1962 fs->fs_fmod = 1; 1963 ACTIVECLEAR(fs, cg); 1964 UFS_UNLOCK(ump); 1965 mp = UFSTOVFS(ump); 1966 if (MOUNTEDSOFTDEP(mp) && devvp->v_type != VREG) 1967 softdep_setup_blkfree(UFSTOVFS(ump), bp, bno, 1968 numfrags(fs, size), dephd); 1969 bdwrite(bp); 1970 } 1971 1972 TASKQUEUE_DEFINE_THREAD(ffs_trim); 1973 1974 struct ffs_blkfree_trim_params { 1975 struct task task; 1976 struct ufsmount *ump; 1977 struct vnode *devvp; 1978 ufs2_daddr_t bno; 1979 long size; 1980 ino_t inum; 1981 struct workhead *pdephd; 1982 struct workhead dephd; 1983 }; 1984 1985 static void 1986 ffs_blkfree_trim_task(ctx, pending) 1987 void *ctx; 1988 int pending; 1989 { 1990 struct ffs_blkfree_trim_params *tp; 1991 1992 tp = ctx; 1993 ffs_blkfree_cg(tp->ump, tp->ump->um_fs, tp->devvp, tp->bno, tp->size, 1994 tp->inum, tp->pdephd); 1995 vn_finished_secondary_write(UFSTOVFS(tp->ump)); 1996 free(tp, M_TEMP); 1997 } 1998 1999 static void 2000 ffs_blkfree_trim_completed(bip) 2001 struct bio *bip; 2002 { 2003 struct ffs_blkfree_trim_params *tp; 2004 2005 tp = bip->bio_caller2; 2006 g_destroy_bio(bip); 2007 TASK_INIT(&tp->task, 0, ffs_blkfree_trim_task, tp); 2008 taskqueue_enqueue(taskqueue_ffs_trim, &tp->task); 2009 } 2010 2011 void 2012 ffs_blkfree(ump, fs, devvp, bno, size, inum, vtype, dephd) 2013 struct ufsmount *ump; 2014 struct fs *fs; 2015 struct vnode *devvp; 2016 ufs2_daddr_t bno; 2017 long size; 2018 ino_t inum; 2019 enum vtype vtype; 2020 struct workhead *dephd; 2021 { 2022 struct mount *mp; 2023 struct bio *bip; 2024 struct ffs_blkfree_trim_params *tp; 2025 2026 /* 2027 * Check to see if a snapshot wants to claim the block. 2028 * Check that devvp is a normal disk device, not a snapshot, 2029 * it has a snapshot(s) associated with it, and one of the 2030 * snapshots wants to claim the block. 2031 */ 2032 if (devvp->v_type != VREG && 2033 (devvp->v_vflag & VV_COPYONWRITE) && 2034 ffs_snapblkfree(fs, devvp, bno, size, inum, vtype, dephd)) { 2035 return; 2036 } 2037 /* 2038 * Nothing to delay if TRIM is disabled, or the operation is 2039 * performed on the snapshot. 2040 */ 2041 if (!ump->um_candelete || devvp->v_type == VREG) { 2042 ffs_blkfree_cg(ump, fs, devvp, bno, size, inum, dephd); 2043 return; 2044 } 2045 2046 /* 2047 * Postpone the set of the free bit in the cg bitmap until the 2048 * BIO_DELETE is completed. Otherwise, due to disk queue 2049 * reordering, TRIM might be issued after we reuse the block 2050 * and write some new data into it. 2051 */ 2052 tp = malloc(sizeof(struct ffs_blkfree_trim_params), M_TEMP, M_WAITOK); 2053 tp->ump = ump; 2054 tp->devvp = devvp; 2055 tp->bno = bno; 2056 tp->size = size; 2057 tp->inum = inum; 2058 if (dephd != NULL) { 2059 LIST_INIT(&tp->dephd); 2060 LIST_SWAP(dephd, &tp->dephd, worklist, wk_list); 2061 tp->pdephd = &tp->dephd; 2062 } else 2063 tp->pdephd = NULL; 2064 2065 bip = g_alloc_bio(); 2066 bip->bio_cmd = BIO_DELETE; 2067 bip->bio_offset = dbtob(fsbtodb(fs, bno)); 2068 bip->bio_done = ffs_blkfree_trim_completed; 2069 bip->bio_length = size; 2070 bip->bio_caller2 = tp; 2071 2072 mp = UFSTOVFS(ump); 2073 vn_start_secondary_write(NULL, &mp, 0); 2074 g_io_request(bip, (struct g_consumer *)devvp->v_bufobj.bo_private); 2075 } 2076 2077 #ifdef INVARIANTS 2078 /* 2079 * Verify allocation of a block or fragment. Returns true if block or 2080 * fragment is allocated, false if it is free. 2081 */ 2082 static int 2083 ffs_checkblk(ip, bno, size) 2084 struct inode *ip; 2085 ufs2_daddr_t bno; 2086 long size; 2087 { 2088 struct fs *fs; 2089 struct cg *cgp; 2090 struct buf *bp; 2091 ufs1_daddr_t cgbno; 2092 int i, error, frags, free; 2093 u_int8_t *blksfree; 2094 2095 fs = ip->i_fs; 2096 if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0) { 2097 printf("bsize = %ld, size = %ld, fs = %s\n", 2098 (long)fs->fs_bsize, size, fs->fs_fsmnt); 2099 panic("ffs_checkblk: bad size"); 2100 } 2101 if ((u_int)bno >= fs->fs_size) 2102 panic("ffs_checkblk: bad block %jd", (intmax_t)bno); 2103 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, dtog(fs, bno))), 2104 (int)fs->fs_cgsize, NOCRED, &bp); 2105 if (error) 2106 panic("ffs_checkblk: cg bread failed"); 2107 cgp = (struct cg *)bp->b_data; 2108 if (!cg_chkmagic(cgp)) 2109 panic("ffs_checkblk: cg magic mismatch"); 2110 bp->b_xflags |= BX_BKGRDWRITE; 2111 blksfree = cg_blksfree(cgp); 2112 cgbno = dtogd(fs, bno); 2113 if (size == fs->fs_bsize) { 2114 free = ffs_isblock(fs, blksfree, fragstoblks(fs, cgbno)); 2115 } else { 2116 frags = numfrags(fs, size); 2117 for (free = 0, i = 0; i < frags; i++) 2118 if (isset(blksfree, cgbno + i)) 2119 free++; 2120 if (free != 0 && free != frags) 2121 panic("ffs_checkblk: partially free fragment"); 2122 } 2123 brelse(bp); 2124 return (!free); 2125 } 2126 #endif /* INVARIANTS */ 2127 2128 /* 2129 * Free an inode. 2130 */ 2131 int 2132 ffs_vfree(pvp, ino, mode) 2133 struct vnode *pvp; 2134 ino_t ino; 2135 int mode; 2136 { 2137 struct inode *ip; 2138 2139 if (DOINGSOFTDEP(pvp)) { 2140 softdep_freefile(pvp, ino, mode); 2141 return (0); 2142 } 2143 ip = VTOI(pvp); 2144 return (ffs_freefile(ip->i_ump, ip->i_fs, ip->i_devvp, ino, mode, 2145 NULL)); 2146 } 2147 2148 /* 2149 * Do the actual free operation. 2150 * The specified inode is placed back in the free map. 2151 */ 2152 int 2153 ffs_freefile(ump, fs, devvp, ino, mode, wkhd) 2154 struct ufsmount *ump; 2155 struct fs *fs; 2156 struct vnode *devvp; 2157 ino_t ino; 2158 int mode; 2159 struct workhead *wkhd; 2160 { 2161 struct cg *cgp; 2162 struct buf *bp; 2163 ufs2_daddr_t cgbno; 2164 int error; 2165 u_int cg; 2166 u_int8_t *inosused; 2167 struct cdev *dev; 2168 2169 cg = ino_to_cg(fs, ino); 2170 if (devvp->v_type == VREG) { 2171 /* devvp is a snapshot */ 2172 dev = VTOI(devvp)->i_devvp->v_rdev; 2173 cgbno = fragstoblks(fs, cgtod(fs, cg)); 2174 } else { 2175 /* devvp is a normal disk device */ 2176 dev = devvp->v_rdev; 2177 cgbno = fsbtodb(fs, cgtod(fs, cg)); 2178 } 2179 if (ino >= fs->fs_ipg * fs->fs_ncg) 2180 panic("ffs_freefile: range: dev = %s, ino = %ju, fs = %s", 2181 devtoname(dev), (uintmax_t)ino, fs->fs_fsmnt); 2182 if ((error = bread(devvp, cgbno, (int)fs->fs_cgsize, NOCRED, &bp))) { 2183 brelse(bp); 2184 return (error); 2185 } 2186 cgp = (struct cg *)bp->b_data; 2187 if (!cg_chkmagic(cgp)) { 2188 brelse(bp); 2189 return (0); 2190 } 2191 bp->b_xflags |= BX_BKGRDWRITE; 2192 cgp->cg_old_time = cgp->cg_time = time_second; 2193 inosused = cg_inosused(cgp); 2194 ino %= fs->fs_ipg; 2195 if (isclr(inosused, ino)) { 2196 printf("dev = %s, ino = %ju, fs = %s\n", devtoname(dev), 2197 (uintmax_t)(ino + cg * fs->fs_ipg), fs->fs_fsmnt); 2198 if (fs->fs_ronly == 0) 2199 panic("ffs_freefile: freeing free inode"); 2200 } 2201 clrbit(inosused, ino); 2202 if (ino < cgp->cg_irotor) 2203 cgp->cg_irotor = ino; 2204 cgp->cg_cs.cs_nifree++; 2205 UFS_LOCK(ump); 2206 fs->fs_cstotal.cs_nifree++; 2207 fs->fs_cs(fs, cg).cs_nifree++; 2208 if ((mode & IFMT) == IFDIR) { 2209 cgp->cg_cs.cs_ndir--; 2210 fs->fs_cstotal.cs_ndir--; 2211 fs->fs_cs(fs, cg).cs_ndir--; 2212 } 2213 fs->fs_fmod = 1; 2214 ACTIVECLEAR(fs, cg); 2215 UFS_UNLOCK(ump); 2216 if (MOUNTEDSOFTDEP(UFSTOVFS(ump)) && devvp->v_type != VREG) 2217 softdep_setup_inofree(UFSTOVFS(ump), bp, 2218 ino + cg * fs->fs_ipg, wkhd); 2219 bdwrite(bp); 2220 return (0); 2221 } 2222 2223 /* 2224 * Check to see if a file is free. 2225 */ 2226 int 2227 ffs_checkfreefile(fs, devvp, ino) 2228 struct fs *fs; 2229 struct vnode *devvp; 2230 ino_t ino; 2231 { 2232 struct cg *cgp; 2233 struct buf *bp; 2234 ufs2_daddr_t cgbno; 2235 int ret; 2236 u_int cg; 2237 u_int8_t *inosused; 2238 2239 cg = ino_to_cg(fs, ino); 2240 if (devvp->v_type == VREG) { 2241 /* devvp is a snapshot */ 2242 cgbno = fragstoblks(fs, cgtod(fs, cg)); 2243 } else { 2244 /* devvp is a normal disk device */ 2245 cgbno = fsbtodb(fs, cgtod(fs, cg)); 2246 } 2247 if (ino >= fs->fs_ipg * fs->fs_ncg) 2248 return (1); 2249 if (bread(devvp, cgbno, (int)fs->fs_cgsize, NOCRED, &bp)) { 2250 brelse(bp); 2251 return (1); 2252 } 2253 cgp = (struct cg *)bp->b_data; 2254 if (!cg_chkmagic(cgp)) { 2255 brelse(bp); 2256 return (1); 2257 } 2258 inosused = cg_inosused(cgp); 2259 ino %= fs->fs_ipg; 2260 ret = isclr(inosused, ino); 2261 brelse(bp); 2262 return (ret); 2263 } 2264 2265 /* 2266 * Find a block of the specified size in the specified cylinder group. 2267 * 2268 * It is a panic if a request is made to find a block if none are 2269 * available. 2270 */ 2271 static ufs1_daddr_t 2272 ffs_mapsearch(fs, cgp, bpref, allocsiz) 2273 struct fs *fs; 2274 struct cg *cgp; 2275 ufs2_daddr_t bpref; 2276 int allocsiz; 2277 { 2278 ufs1_daddr_t bno; 2279 int start, len, loc, i; 2280 int blk, field, subfield, pos; 2281 u_int8_t *blksfree; 2282 2283 /* 2284 * find the fragment by searching through the free block 2285 * map for an appropriate bit pattern 2286 */ 2287 if (bpref) 2288 start = dtogd(fs, bpref) / NBBY; 2289 else 2290 start = cgp->cg_frotor / NBBY; 2291 blksfree = cg_blksfree(cgp); 2292 len = howmany(fs->fs_fpg, NBBY) - start; 2293 loc = scanc((u_int)len, (u_char *)&blksfree[start], 2294 fragtbl[fs->fs_frag], 2295 (u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY)))); 2296 if (loc == 0) { 2297 len = start + 1; 2298 start = 0; 2299 loc = scanc((u_int)len, (u_char *)&blksfree[0], 2300 fragtbl[fs->fs_frag], 2301 (u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY)))); 2302 if (loc == 0) { 2303 printf("start = %d, len = %d, fs = %s\n", 2304 start, len, fs->fs_fsmnt); 2305 panic("ffs_alloccg: map corrupted"); 2306 /* NOTREACHED */ 2307 } 2308 } 2309 bno = (start + len - loc) * NBBY; 2310 cgp->cg_frotor = bno; 2311 /* 2312 * found the byte in the map 2313 * sift through the bits to find the selected frag 2314 */ 2315 for (i = bno + NBBY; bno < i; bno += fs->fs_frag) { 2316 blk = blkmap(fs, blksfree, bno); 2317 blk <<= 1; 2318 field = around[allocsiz]; 2319 subfield = inside[allocsiz]; 2320 for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) { 2321 if ((blk & field) == subfield) 2322 return (bno + pos); 2323 field <<= 1; 2324 subfield <<= 1; 2325 } 2326 } 2327 printf("bno = %lu, fs = %s\n", (u_long)bno, fs->fs_fsmnt); 2328 panic("ffs_alloccg: block not in map"); 2329 return (-1); 2330 } 2331 2332 /* 2333 * Fserr prints the name of a filesystem with an error diagnostic. 2334 * 2335 * The form of the error message is: 2336 * fs: error message 2337 */ 2338 void 2339 ffs_fserr(fs, inum, cp) 2340 struct fs *fs; 2341 ino_t inum; 2342 char *cp; 2343 { 2344 struct thread *td = curthread; /* XXX */ 2345 struct proc *p = td->td_proc; 2346 2347 log(LOG_ERR, "pid %d (%s), uid %d inumber %ju on %s: %s\n", 2348 p->p_pid, p->p_comm, td->td_ucred->cr_uid, (uintmax_t)inum, 2349 fs->fs_fsmnt, cp); 2350 } 2351 2352 /* 2353 * This function provides the capability for the fsck program to 2354 * update an active filesystem. Fourteen operations are provided: 2355 * 2356 * adjrefcnt(inode, amt) - adjusts the reference count on the 2357 * specified inode by the specified amount. Under normal 2358 * operation the count should always go down. Decrementing 2359 * the count to zero will cause the inode to be freed. 2360 * adjblkcnt(inode, amt) - adjust the number of blocks used by the 2361 * inode by the specified amount. 2362 * adjndir, adjbfree, adjifree, adjffree, adjnumclusters(amt) - 2363 * adjust the superblock summary. 2364 * freedirs(inode, count) - directory inodes [inode..inode + count - 1] 2365 * are marked as free. Inodes should never have to be marked 2366 * as in use. 2367 * freefiles(inode, count) - file inodes [inode..inode + count - 1] 2368 * are marked as free. Inodes should never have to be marked 2369 * as in use. 2370 * freeblks(blockno, size) - blocks [blockno..blockno + size - 1] 2371 * are marked as free. Blocks should never have to be marked 2372 * as in use. 2373 * setflags(flags, set/clear) - the fs_flags field has the specified 2374 * flags set (second parameter +1) or cleared (second parameter -1). 2375 * setcwd(dirinode) - set the current directory to dirinode in the 2376 * filesystem associated with the snapshot. 2377 * setdotdot(oldvalue, newvalue) - Verify that the inode number for ".." 2378 * in the current directory is oldvalue then change it to newvalue. 2379 * unlink(nameptr, oldvalue) - Verify that the inode number associated 2380 * with nameptr in the current directory is oldvalue then unlink it. 2381 * 2382 * The following functions may only be used on a quiescent filesystem 2383 * by the soft updates journal. They are not safe to be run on an active 2384 * filesystem. 2385 * 2386 * setinode(inode, dip) - the specified disk inode is replaced with the 2387 * contents pointed to by dip. 2388 * setbufoutput(fd, flags) - output associated with the specified file 2389 * descriptor (which must reference the character device supporting 2390 * the filesystem) switches from using physio to running through the 2391 * buffer cache when flags is set to 1. The descriptor reverts to 2392 * physio for output when flags is set to zero. 2393 */ 2394 2395 static int sysctl_ffs_fsck(SYSCTL_HANDLER_ARGS); 2396 2397 SYSCTL_PROC(_vfs_ffs, FFS_ADJ_REFCNT, adjrefcnt, CTLFLAG_WR|CTLTYPE_STRUCT, 2398 0, 0, sysctl_ffs_fsck, "S,fsck", "Adjust Inode Reference Count"); 2399 2400 static SYSCTL_NODE(_vfs_ffs, FFS_ADJ_BLKCNT, adjblkcnt, CTLFLAG_WR, 2401 sysctl_ffs_fsck, "Adjust Inode Used Blocks Count"); 2402 2403 static SYSCTL_NODE(_vfs_ffs, FFS_ADJ_NDIR, adjndir, CTLFLAG_WR, 2404 sysctl_ffs_fsck, "Adjust number of directories"); 2405 2406 static SYSCTL_NODE(_vfs_ffs, FFS_ADJ_NBFREE, adjnbfree, CTLFLAG_WR, 2407 sysctl_ffs_fsck, "Adjust number of free blocks"); 2408 2409 static SYSCTL_NODE(_vfs_ffs, FFS_ADJ_NIFREE, adjnifree, CTLFLAG_WR, 2410 sysctl_ffs_fsck, "Adjust number of free inodes"); 2411 2412 static SYSCTL_NODE(_vfs_ffs, FFS_ADJ_NFFREE, adjnffree, CTLFLAG_WR, 2413 sysctl_ffs_fsck, "Adjust number of free frags"); 2414 2415 static SYSCTL_NODE(_vfs_ffs, FFS_ADJ_NUMCLUSTERS, adjnumclusters, CTLFLAG_WR, 2416 sysctl_ffs_fsck, "Adjust number of free clusters"); 2417 2418 static SYSCTL_NODE(_vfs_ffs, FFS_DIR_FREE, freedirs, CTLFLAG_WR, 2419 sysctl_ffs_fsck, "Free Range of Directory Inodes"); 2420 2421 static SYSCTL_NODE(_vfs_ffs, FFS_FILE_FREE, freefiles, CTLFLAG_WR, 2422 sysctl_ffs_fsck, "Free Range of File Inodes"); 2423 2424 static SYSCTL_NODE(_vfs_ffs, FFS_BLK_FREE, freeblks, CTLFLAG_WR, 2425 sysctl_ffs_fsck, "Free Range of Blocks"); 2426 2427 static SYSCTL_NODE(_vfs_ffs, FFS_SET_FLAGS, setflags, CTLFLAG_WR, 2428 sysctl_ffs_fsck, "Change Filesystem Flags"); 2429 2430 static SYSCTL_NODE(_vfs_ffs, FFS_SET_CWD, setcwd, CTLFLAG_WR, 2431 sysctl_ffs_fsck, "Set Current Working Directory"); 2432 2433 static SYSCTL_NODE(_vfs_ffs, FFS_SET_DOTDOT, setdotdot, CTLFLAG_WR, 2434 sysctl_ffs_fsck, "Change Value of .. Entry"); 2435 2436 static SYSCTL_NODE(_vfs_ffs, FFS_UNLINK, unlink, CTLFLAG_WR, 2437 sysctl_ffs_fsck, "Unlink a Duplicate Name"); 2438 2439 static SYSCTL_NODE(_vfs_ffs, FFS_SET_INODE, setinode, CTLFLAG_WR, 2440 sysctl_ffs_fsck, "Update an On-Disk Inode"); 2441 2442 static SYSCTL_NODE(_vfs_ffs, FFS_SET_BUFOUTPUT, setbufoutput, CTLFLAG_WR, 2443 sysctl_ffs_fsck, "Set Buffered Writing for Descriptor"); 2444 2445 #define DEBUG 1 2446 #ifdef DEBUG 2447 static int fsckcmds = 0; 2448 SYSCTL_INT(_debug, OID_AUTO, fsckcmds, CTLFLAG_RW, &fsckcmds, 0, ""); 2449 #endif /* DEBUG */ 2450 2451 static int buffered_write(struct file *, struct uio *, struct ucred *, 2452 int, struct thread *); 2453 2454 static int 2455 sysctl_ffs_fsck(SYSCTL_HANDLER_ARGS) 2456 { 2457 struct thread *td = curthread; 2458 struct fsck_cmd cmd; 2459 struct ufsmount *ump; 2460 struct vnode *vp, *vpold, *dvp, *fdvp; 2461 struct inode *ip, *dp; 2462 struct mount *mp; 2463 struct fs *fs; 2464 ufs2_daddr_t blkno; 2465 long blkcnt, blksize; 2466 struct filedesc *fdp; 2467 struct file *fp, *vfp; 2468 int filetype, error; 2469 static struct fileops *origops, bufferedops; 2470 2471 if (req->newlen > sizeof cmd) 2472 return (EBADRPC); 2473 if ((error = SYSCTL_IN(req, &cmd, sizeof cmd)) != 0) 2474 return (error); 2475 if (cmd.version != FFS_CMD_VERSION) 2476 return (ERPCMISMATCH); 2477 if ((error = getvnode(td->td_proc->p_fd, cmd.handle, CAP_FSCK, 2478 &fp)) != 0) 2479 return (error); 2480 vp = fp->f_data; 2481 if (vp->v_type != VREG && vp->v_type != VDIR) { 2482 fdrop(fp, td); 2483 return (EINVAL); 2484 } 2485 vn_start_write(vp, &mp, V_WAIT); 2486 if (mp == 0 || strncmp(mp->mnt_stat.f_fstypename, "ufs", MFSNAMELEN)) { 2487 vn_finished_write(mp); 2488 fdrop(fp, td); 2489 return (EINVAL); 2490 } 2491 ump = VFSTOUFS(mp); 2492 if ((mp->mnt_flag & MNT_RDONLY) && 2493 ump->um_fsckpid != td->td_proc->p_pid) { 2494 vn_finished_write(mp); 2495 fdrop(fp, td); 2496 return (EROFS); 2497 } 2498 fs = ump->um_fs; 2499 filetype = IFREG; 2500 2501 switch (oidp->oid_number) { 2502 2503 case FFS_SET_FLAGS: 2504 #ifdef DEBUG 2505 if (fsckcmds) 2506 printf("%s: %s flags\n", mp->mnt_stat.f_mntonname, 2507 cmd.size > 0 ? "set" : "clear"); 2508 #endif /* DEBUG */ 2509 if (cmd.size > 0) 2510 fs->fs_flags |= (long)cmd.value; 2511 else 2512 fs->fs_flags &= ~(long)cmd.value; 2513 break; 2514 2515 case FFS_ADJ_REFCNT: 2516 #ifdef DEBUG 2517 if (fsckcmds) { 2518 printf("%s: adjust inode %jd link count by %jd\n", 2519 mp->mnt_stat.f_mntonname, (intmax_t)cmd.value, 2520 (intmax_t)cmd.size); 2521 } 2522 #endif /* DEBUG */ 2523 if ((error = ffs_vget(mp, (ino_t)cmd.value, LK_EXCLUSIVE, &vp))) 2524 break; 2525 ip = VTOI(vp); 2526 ip->i_nlink += cmd.size; 2527 DIP_SET(ip, i_nlink, ip->i_nlink); 2528 ip->i_effnlink += cmd.size; 2529 ip->i_flag |= IN_CHANGE | IN_MODIFIED; 2530 error = ffs_update(vp, 1); 2531 if (DOINGSOFTDEP(vp)) 2532 softdep_change_linkcnt(ip); 2533 vput(vp); 2534 break; 2535 2536 case FFS_ADJ_BLKCNT: 2537 #ifdef DEBUG 2538 if (fsckcmds) { 2539 printf("%s: adjust inode %jd block count by %jd\n", 2540 mp->mnt_stat.f_mntonname, (intmax_t)cmd.value, 2541 (intmax_t)cmd.size); 2542 } 2543 #endif /* DEBUG */ 2544 if ((error = ffs_vget(mp, (ino_t)cmd.value, LK_EXCLUSIVE, &vp))) 2545 break; 2546 ip = VTOI(vp); 2547 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) + cmd.size); 2548 ip->i_flag |= IN_CHANGE | IN_MODIFIED; 2549 error = ffs_update(vp, 1); 2550 vput(vp); 2551 break; 2552 2553 case FFS_DIR_FREE: 2554 filetype = IFDIR; 2555 /* fall through */ 2556 2557 case FFS_FILE_FREE: 2558 #ifdef DEBUG 2559 if (fsckcmds) { 2560 if (cmd.size == 1) 2561 printf("%s: free %s inode %ju\n", 2562 mp->mnt_stat.f_mntonname, 2563 filetype == IFDIR ? "directory" : "file", 2564 (uintmax_t)cmd.value); 2565 else 2566 printf("%s: free %s inodes %ju-%ju\n", 2567 mp->mnt_stat.f_mntonname, 2568 filetype == IFDIR ? "directory" : "file", 2569 (uintmax_t)cmd.value, 2570 (uintmax_t)(cmd.value + cmd.size - 1)); 2571 } 2572 #endif /* DEBUG */ 2573 while (cmd.size > 0) { 2574 if ((error = ffs_freefile(ump, fs, ump->um_devvp, 2575 cmd.value, filetype, NULL))) 2576 break; 2577 cmd.size -= 1; 2578 cmd.value += 1; 2579 } 2580 break; 2581 2582 case FFS_BLK_FREE: 2583 #ifdef DEBUG 2584 if (fsckcmds) { 2585 if (cmd.size == 1) 2586 printf("%s: free block %jd\n", 2587 mp->mnt_stat.f_mntonname, 2588 (intmax_t)cmd.value); 2589 else 2590 printf("%s: free blocks %jd-%jd\n", 2591 mp->mnt_stat.f_mntonname, 2592 (intmax_t)cmd.value, 2593 (intmax_t)cmd.value + cmd.size - 1); 2594 } 2595 #endif /* DEBUG */ 2596 blkno = cmd.value; 2597 blkcnt = cmd.size; 2598 blksize = fs->fs_frag - (blkno % fs->fs_frag); 2599 while (blkcnt > 0) { 2600 if (blksize > blkcnt) 2601 blksize = blkcnt; 2602 ffs_blkfree(ump, fs, ump->um_devvp, blkno, 2603 blksize * fs->fs_fsize, ROOTINO, VDIR, NULL); 2604 blkno += blksize; 2605 blkcnt -= blksize; 2606 blksize = fs->fs_frag; 2607 } 2608 break; 2609 2610 /* 2611 * Adjust superblock summaries. fsck(8) is expected to 2612 * submit deltas when necessary. 2613 */ 2614 case FFS_ADJ_NDIR: 2615 #ifdef DEBUG 2616 if (fsckcmds) { 2617 printf("%s: adjust number of directories by %jd\n", 2618 mp->mnt_stat.f_mntonname, (intmax_t)cmd.value); 2619 } 2620 #endif /* DEBUG */ 2621 fs->fs_cstotal.cs_ndir += cmd.value; 2622 break; 2623 2624 case FFS_ADJ_NBFREE: 2625 #ifdef DEBUG 2626 if (fsckcmds) { 2627 printf("%s: adjust number of free blocks by %+jd\n", 2628 mp->mnt_stat.f_mntonname, (intmax_t)cmd.value); 2629 } 2630 #endif /* DEBUG */ 2631 fs->fs_cstotal.cs_nbfree += cmd.value; 2632 break; 2633 2634 case FFS_ADJ_NIFREE: 2635 #ifdef DEBUG 2636 if (fsckcmds) { 2637 printf("%s: adjust number of free inodes by %+jd\n", 2638 mp->mnt_stat.f_mntonname, (intmax_t)cmd.value); 2639 } 2640 #endif /* DEBUG */ 2641 fs->fs_cstotal.cs_nifree += cmd.value; 2642 break; 2643 2644 case FFS_ADJ_NFFREE: 2645 #ifdef DEBUG 2646 if (fsckcmds) { 2647 printf("%s: adjust number of free frags by %+jd\n", 2648 mp->mnt_stat.f_mntonname, (intmax_t)cmd.value); 2649 } 2650 #endif /* DEBUG */ 2651 fs->fs_cstotal.cs_nffree += cmd.value; 2652 break; 2653 2654 case FFS_ADJ_NUMCLUSTERS: 2655 #ifdef DEBUG 2656 if (fsckcmds) { 2657 printf("%s: adjust number of free clusters by %+jd\n", 2658 mp->mnt_stat.f_mntonname, (intmax_t)cmd.value); 2659 } 2660 #endif /* DEBUG */ 2661 fs->fs_cstotal.cs_numclusters += cmd.value; 2662 break; 2663 2664 case FFS_SET_CWD: 2665 #ifdef DEBUG 2666 if (fsckcmds) { 2667 printf("%s: set current directory to inode %jd\n", 2668 mp->mnt_stat.f_mntonname, (intmax_t)cmd.value); 2669 } 2670 #endif /* DEBUG */ 2671 if ((error = ffs_vget(mp, (ino_t)cmd.value, LK_SHARED, &vp))) 2672 break; 2673 AUDIT_ARG_VNODE1(vp); 2674 if ((error = change_dir(vp, td)) != 0) { 2675 vput(vp); 2676 break; 2677 } 2678 VOP_UNLOCK(vp, 0); 2679 fdp = td->td_proc->p_fd; 2680 FILEDESC_XLOCK(fdp); 2681 vpold = fdp->fd_cdir; 2682 fdp->fd_cdir = vp; 2683 FILEDESC_XUNLOCK(fdp); 2684 vrele(vpold); 2685 break; 2686 2687 case FFS_SET_DOTDOT: 2688 #ifdef DEBUG 2689 if (fsckcmds) { 2690 printf("%s: change .. in cwd from %jd to %jd\n", 2691 mp->mnt_stat.f_mntonname, (intmax_t)cmd.value, 2692 (intmax_t)cmd.size); 2693 } 2694 #endif /* DEBUG */ 2695 /* 2696 * First we have to get and lock the parent directory 2697 * to which ".." points. 2698 */ 2699 error = ffs_vget(mp, (ino_t)cmd.value, LK_EXCLUSIVE, &fdvp); 2700 if (error) 2701 break; 2702 /* 2703 * Now we get and lock the child directory containing "..". 2704 */ 2705 FILEDESC_SLOCK(td->td_proc->p_fd); 2706 dvp = td->td_proc->p_fd->fd_cdir; 2707 FILEDESC_SUNLOCK(td->td_proc->p_fd); 2708 if ((error = vget(dvp, LK_EXCLUSIVE, td)) != 0) { 2709 vput(fdvp); 2710 break; 2711 } 2712 dp = VTOI(dvp); 2713 dp->i_offset = 12; /* XXX mastertemplate.dot_reclen */ 2714 error = ufs_dirrewrite(dp, VTOI(fdvp), (ino_t)cmd.size, 2715 DT_DIR, 0); 2716 cache_purge(fdvp); 2717 cache_purge(dvp); 2718 vput(dvp); 2719 vput(fdvp); 2720 break; 2721 2722 case FFS_UNLINK: 2723 #ifdef DEBUG 2724 if (fsckcmds) { 2725 char buf[32]; 2726 2727 if (copyinstr((char *)(intptr_t)cmd.value, buf,32,NULL)) 2728 strncpy(buf, "Name_too_long", 32); 2729 printf("%s: unlink %s (inode %jd)\n", 2730 mp->mnt_stat.f_mntonname, buf, (intmax_t)cmd.size); 2731 } 2732 #endif /* DEBUG */ 2733 /* 2734 * kern_unlinkat will do its own start/finish writes and 2735 * they do not nest, so drop ours here. Setting mp == NULL 2736 * indicates that vn_finished_write is not needed down below. 2737 */ 2738 vn_finished_write(mp); 2739 mp = NULL; 2740 error = kern_unlinkat(td, AT_FDCWD, (char *)(intptr_t)cmd.value, 2741 UIO_USERSPACE, (ino_t)cmd.size); 2742 break; 2743 2744 case FFS_SET_INODE: 2745 if (ump->um_fsckpid != td->td_proc->p_pid) { 2746 error = EPERM; 2747 break; 2748 } 2749 #ifdef DEBUG 2750 if (fsckcmds) { 2751 printf("%s: update inode %jd\n", 2752 mp->mnt_stat.f_mntonname, (intmax_t)cmd.value); 2753 } 2754 #endif /* DEBUG */ 2755 if ((error = ffs_vget(mp, (ino_t)cmd.value, LK_EXCLUSIVE, &vp))) 2756 break; 2757 AUDIT_ARG_VNODE1(vp); 2758 ip = VTOI(vp); 2759 if (ip->i_ump->um_fstype == UFS1) 2760 error = copyin((void *)(intptr_t)cmd.size, ip->i_din1, 2761 sizeof(struct ufs1_dinode)); 2762 else 2763 error = copyin((void *)(intptr_t)cmd.size, ip->i_din2, 2764 sizeof(struct ufs2_dinode)); 2765 if (error) { 2766 vput(vp); 2767 break; 2768 } 2769 ip->i_flag |= IN_CHANGE | IN_MODIFIED; 2770 error = ffs_update(vp, 1); 2771 vput(vp); 2772 break; 2773 2774 case FFS_SET_BUFOUTPUT: 2775 if (ump->um_fsckpid != td->td_proc->p_pid) { 2776 error = EPERM; 2777 break; 2778 } 2779 if (VTOI(vp)->i_ump != ump) { 2780 error = EINVAL; 2781 break; 2782 } 2783 #ifdef DEBUG 2784 if (fsckcmds) { 2785 printf("%s: %s buffered output for descriptor %jd\n", 2786 mp->mnt_stat.f_mntonname, 2787 cmd.size == 1 ? "enable" : "disable", 2788 (intmax_t)cmd.value); 2789 } 2790 #endif /* DEBUG */ 2791 if ((error = getvnode(td->td_proc->p_fd, cmd.value, 2792 CAP_FSCK, &vfp)) != 0) 2793 break; 2794 if (vfp->f_vnode->v_type != VCHR) { 2795 fdrop(vfp, td); 2796 error = EINVAL; 2797 break; 2798 } 2799 if (origops == NULL) { 2800 origops = vfp->f_ops; 2801 bcopy((void *)origops, (void *)&bufferedops, 2802 sizeof(bufferedops)); 2803 bufferedops.fo_write = buffered_write; 2804 } 2805 if (cmd.size == 1) 2806 atomic_store_rel_ptr((volatile uintptr_t *)&vfp->f_ops, 2807 (uintptr_t)&bufferedops); 2808 else 2809 atomic_store_rel_ptr((volatile uintptr_t *)&vfp->f_ops, 2810 (uintptr_t)origops); 2811 fdrop(vfp, td); 2812 break; 2813 2814 default: 2815 #ifdef DEBUG 2816 if (fsckcmds) { 2817 printf("Invalid request %d from fsck\n", 2818 oidp->oid_number); 2819 } 2820 #endif /* DEBUG */ 2821 error = EINVAL; 2822 break; 2823 2824 } 2825 fdrop(fp, td); 2826 vn_finished_write(mp); 2827 return (error); 2828 } 2829 2830 /* 2831 * Function to switch a descriptor to use the buffer cache to stage 2832 * its I/O. This is needed so that writes to the filesystem device 2833 * will give snapshots a chance to copy modified blocks for which it 2834 * needs to retain copies. 2835 */ 2836 static int 2837 buffered_write(fp, uio, active_cred, flags, td) 2838 struct file *fp; 2839 struct uio *uio; 2840 struct ucred *active_cred; 2841 int flags; 2842 struct thread *td; 2843 { 2844 struct vnode *devvp; 2845 struct inode *ip; 2846 struct buf *bp; 2847 struct fs *fs; 2848 int error; 2849 daddr_t lbn; 2850 2851 /* 2852 * The devvp is associated with the /dev filesystem. To discover 2853 * the filesystem with which the device is associated, we depend 2854 * on the application setting the current directory to a location 2855 * within the filesystem being written. Yes, this is an ugly hack. 2856 */ 2857 devvp = fp->f_vnode; 2858 ip = VTOI(td->td_proc->p_fd->fd_cdir); 2859 if (ip->i_devvp != devvp) 2860 return (EINVAL); 2861 fs = ip->i_fs; 2862 foffset_lock_uio(fp, uio, flags); 2863 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); 2864 #ifdef DEBUG 2865 if (fsckcmds) { 2866 printf("%s: buffered write for block %jd\n", 2867 fs->fs_fsmnt, (intmax_t)btodb(uio->uio_offset)); 2868 } 2869 #endif /* DEBUG */ 2870 /* 2871 * All I/O must be contained within a filesystem block, start on 2872 * a fragment boundary, and be a multiple of fragments in length. 2873 */ 2874 if (uio->uio_resid > fs->fs_bsize - (uio->uio_offset % fs->fs_bsize) || 2875 fragoff(fs, uio->uio_offset) != 0 || 2876 fragoff(fs, uio->uio_resid) != 0) { 2877 error = EINVAL; 2878 goto out; 2879 } 2880 lbn = numfrags(fs, uio->uio_offset); 2881 bp = getblk(devvp, lbn, uio->uio_resid, 0, 0, 0); 2882 bp->b_flags |= B_RELBUF; 2883 if ((error = uiomove((char *)bp->b_data, uio->uio_resid, uio)) != 0) { 2884 brelse(bp); 2885 goto out; 2886 } 2887 error = bwrite(bp); 2888 out: 2889 VOP_UNLOCK(devvp, 0); 2890 foffset_unlock_uio(fp, uio, flags | FOF_NEXTOFF); 2891 return (error); 2892 } 2893