1 /*- 2 * Copyright (c) 1989, 1991, 1993, 1994 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 4. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * @(#)ffs_vfsops.c 8.31 (Berkeley) 5/20/95 30 */ 31 32 #include <sys/cdefs.h> 33 __FBSDID("$FreeBSD$"); 34 35 #include "opt_quota.h" 36 #include "opt_ufs.h" 37 #include "opt_ffs.h" 38 #include "opt_ddb.h" 39 40 #include <sys/param.h> 41 #include <sys/systm.h> 42 #include <sys/namei.h> 43 #include <sys/priv.h> 44 #include <sys/proc.h> 45 #include <sys/kernel.h> 46 #include <sys/vnode.h> 47 #include <sys/mount.h> 48 #include <sys/bio.h> 49 #include <sys/buf.h> 50 #include <sys/conf.h> 51 #include <sys/fcntl.h> 52 #include <sys/malloc.h> 53 #include <sys/mutex.h> 54 55 #include <security/mac/mac_framework.h> 56 57 #include <ufs/ufs/extattr.h> 58 #include <ufs/ufs/gjournal.h> 59 #include <ufs/ufs/quota.h> 60 #include <ufs/ufs/ufsmount.h> 61 #include <ufs/ufs/inode.h> 62 #include <ufs/ufs/ufs_extern.h> 63 64 #include <ufs/ffs/fs.h> 65 #include <ufs/ffs/ffs_extern.h> 66 67 #include <vm/vm.h> 68 #include <vm/uma.h> 69 #include <vm/vm_page.h> 70 71 #include <geom/geom.h> 72 #include <geom/geom_vfs.h> 73 74 #include <ddb/ddb.h> 75 76 static uma_zone_t uma_inode, uma_ufs1, uma_ufs2; 77 78 static int ffs_reload(struct mount *, struct thread *); 79 static int ffs_mountfs(struct vnode *, struct mount *, struct thread *); 80 static void ffs_oldfscompat_read(struct fs *, struct ufsmount *, 81 ufs2_daddr_t); 82 static void ffs_ifree(struct ufsmount *ump, struct inode *ip); 83 static vfs_init_t ffs_init; 84 static vfs_uninit_t ffs_uninit; 85 static vfs_extattrctl_t ffs_extattrctl; 86 static vfs_cmount_t ffs_cmount; 87 static vfs_unmount_t ffs_unmount; 88 static vfs_mount_t ffs_mount; 89 static vfs_statfs_t ffs_statfs; 90 static vfs_fhtovp_t ffs_fhtovp; 91 static vfs_sync_t ffs_sync; 92 93 static struct vfsops ufs_vfsops = { 94 .vfs_extattrctl = ffs_extattrctl, 95 .vfs_fhtovp = ffs_fhtovp, 96 .vfs_init = ffs_init, 97 .vfs_mount = ffs_mount, 98 .vfs_cmount = ffs_cmount, 99 .vfs_quotactl = ufs_quotactl, 100 .vfs_root = ufs_root, 101 .vfs_statfs = ffs_statfs, 102 .vfs_sync = ffs_sync, 103 .vfs_uninit = ffs_uninit, 104 .vfs_unmount = ffs_unmount, 105 .vfs_vget = ffs_vget, 106 .vfs_susp_clean = process_deferred_inactive, 107 }; 108 109 VFS_SET(ufs_vfsops, ufs, 0); 110 MODULE_VERSION(ufs, 1); 111 112 static b_strategy_t ffs_geom_strategy; 113 static b_write_t ffs_bufwrite; 114 115 static struct buf_ops ffs_ops = { 116 .bop_name = "FFS", 117 .bop_write = ffs_bufwrite, 118 .bop_strategy = ffs_geom_strategy, 119 .bop_sync = bufsync, 120 #ifdef NO_FFS_SNAPSHOT 121 .bop_bdflush = bufbdflush, 122 #else 123 .bop_bdflush = ffs_bdflush, 124 #endif 125 }; 126 127 /* 128 * Note that userquota and groupquota options are not currently used 129 * by UFS/FFS code and generally mount(8) does not pass those options 130 * from userland, but they can be passed by loader(8) via 131 * vfs.root.mountfrom.options. 132 */ 133 static const char *ffs_opts[] = { "acls", "async", "noatime", "noclusterr", 134 "noclusterw", "noexec", "export", "force", "from", "groupquota", 135 "multilabel", "nfsv4acls", "snapshot", "nosuid", "suiddir", "nosymfollow", 136 "sync", "union", "userquota", NULL }; 137 138 static int 139 ffs_mount(struct mount *mp) 140 { 141 struct vnode *devvp; 142 struct thread *td; 143 struct ufsmount *ump = 0; 144 struct fs *fs; 145 int error, flags; 146 u_int mntorflags; 147 accmode_t accmode; 148 struct nameidata ndp; 149 char *fspec; 150 151 td = curthread; 152 if (vfs_filteropt(mp->mnt_optnew, ffs_opts)) 153 return (EINVAL); 154 if (uma_inode == NULL) { 155 uma_inode = uma_zcreate("FFS inode", 156 sizeof(struct inode), NULL, NULL, NULL, NULL, 157 UMA_ALIGN_PTR, 0); 158 uma_ufs1 = uma_zcreate("FFS1 dinode", 159 sizeof(struct ufs1_dinode), NULL, NULL, NULL, NULL, 160 UMA_ALIGN_PTR, 0); 161 uma_ufs2 = uma_zcreate("FFS2 dinode", 162 sizeof(struct ufs2_dinode), NULL, NULL, NULL, NULL, 163 UMA_ALIGN_PTR, 0); 164 } 165 166 vfs_deleteopt(mp->mnt_optnew, "groupquota"); 167 vfs_deleteopt(mp->mnt_optnew, "userquota"); 168 169 fspec = vfs_getopts(mp->mnt_optnew, "from", &error); 170 if (error) 171 return (error); 172 173 mntorflags = 0; 174 if (vfs_getopt(mp->mnt_optnew, "acls", NULL, NULL) == 0) 175 mntorflags |= MNT_ACLS; 176 177 if (vfs_getopt(mp->mnt_optnew, "snapshot", NULL, NULL) == 0) { 178 mntorflags |= MNT_SNAPSHOT; 179 /* 180 * Once we have set the MNT_SNAPSHOT flag, do not 181 * persist "snapshot" in the options list. 182 */ 183 vfs_deleteopt(mp->mnt_optnew, "snapshot"); 184 vfs_deleteopt(mp->mnt_opt, "snapshot"); 185 } 186 187 if (vfs_getopt(mp->mnt_optnew, "nfsv4acls", NULL, NULL) == 0) { 188 if (mntorflags & MNT_ACLS) { 189 printf("WARNING: \"acls\" and \"nfsv4acls\" " 190 "options are mutually exclusive\n"); 191 return (EINVAL); 192 } 193 mntorflags |= MNT_NFS4ACLS; 194 } 195 196 MNT_ILOCK(mp); 197 mp->mnt_flag |= mntorflags; 198 MNT_IUNLOCK(mp); 199 /* 200 * If updating, check whether changing from read-only to 201 * read/write; if there is no device name, that's all we do. 202 */ 203 if (mp->mnt_flag & MNT_UPDATE) { 204 ump = VFSTOUFS(mp); 205 fs = ump->um_fs; 206 devvp = ump->um_devvp; 207 if (fs->fs_ronly == 0 && 208 vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) { 209 /* 210 * Flush any dirty data and suspend filesystem. 211 */ 212 if ((error = vn_start_write(NULL, &mp, V_WAIT)) != 0) 213 return (error); 214 for (;;) { 215 vn_finished_write(mp); 216 if ((error = vfs_write_suspend(mp)) != 0) 217 return (error); 218 MNT_ILOCK(mp); 219 if (mp->mnt_kern_flag & MNTK_SUSPENDED) { 220 /* 221 * Allow the secondary writes 222 * to proceed. 223 */ 224 mp->mnt_kern_flag &= ~(MNTK_SUSPENDED | 225 MNTK_SUSPEND2); 226 wakeup(&mp->mnt_flag); 227 MNT_IUNLOCK(mp); 228 /* 229 * Allow the curthread to 230 * ignore the suspension to 231 * synchronize on-disk state. 232 */ 233 td->td_pflags |= TDP_IGNSUSP; 234 break; 235 } 236 MNT_IUNLOCK(mp); 237 vn_start_write(NULL, &mp, V_WAIT); 238 } 239 /* 240 * Check for and optionally get rid of files open 241 * for writing. 242 */ 243 flags = WRITECLOSE; 244 if (mp->mnt_flag & MNT_FORCE) 245 flags |= FORCECLOSE; 246 if (mp->mnt_flag & MNT_SOFTDEP) { 247 error = softdep_flushfiles(mp, flags, td); 248 } else { 249 error = ffs_flushfiles(mp, flags, td); 250 } 251 if (error) { 252 vfs_write_resume(mp); 253 return (error); 254 } 255 if (fs->fs_pendingblocks != 0 || 256 fs->fs_pendinginodes != 0) { 257 printf("%s: %s: blocks %jd files %d\n", 258 fs->fs_fsmnt, "update error", 259 (intmax_t)fs->fs_pendingblocks, 260 fs->fs_pendinginodes); 261 fs->fs_pendingblocks = 0; 262 fs->fs_pendinginodes = 0; 263 } 264 if ((fs->fs_flags & (FS_UNCLEAN | FS_NEEDSFSCK)) == 0) 265 fs->fs_clean = 1; 266 if ((error = ffs_sbupdate(ump, MNT_WAIT, 0)) != 0) { 267 fs->fs_ronly = 0; 268 fs->fs_clean = 0; 269 vfs_write_resume(mp); 270 return (error); 271 } 272 if (mp->mnt_flag & MNT_SOFTDEP) 273 softdep_unmount(mp); 274 DROP_GIANT(); 275 g_topology_lock(); 276 g_access(ump->um_cp, 0, -1, 0); 277 g_topology_unlock(); 278 PICKUP_GIANT(); 279 fs->fs_ronly = 1; 280 MNT_ILOCK(mp); 281 mp->mnt_flag |= MNT_RDONLY; 282 MNT_IUNLOCK(mp); 283 /* 284 * Allow the writers to note that filesystem 285 * is ro now. 286 */ 287 vfs_write_resume(mp); 288 } 289 if ((mp->mnt_flag & MNT_RELOAD) && 290 (error = ffs_reload(mp, td)) != 0) 291 return (error); 292 if (fs->fs_ronly && 293 !vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) { 294 /* 295 * If upgrade to read-write by non-root, then verify 296 * that user has necessary permissions on the device. 297 */ 298 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); 299 error = VOP_ACCESS(devvp, VREAD | VWRITE, 300 td->td_ucred, td); 301 if (error) 302 error = priv_check(td, PRIV_VFS_MOUNT_PERM); 303 if (error) { 304 VOP_UNLOCK(devvp, 0); 305 return (error); 306 } 307 VOP_UNLOCK(devvp, 0); 308 fs->fs_flags &= ~FS_UNCLEAN; 309 if (fs->fs_clean == 0) { 310 fs->fs_flags |= FS_UNCLEAN; 311 if ((mp->mnt_flag & MNT_FORCE) || 312 ((fs->fs_flags & 313 (FS_SUJ | FS_NEEDSFSCK)) == 0 && 314 (fs->fs_flags & FS_DOSOFTDEP))) { 315 printf("WARNING: %s was not %s\n", 316 fs->fs_fsmnt, "properly dismounted"); 317 } else { 318 printf( 319 "WARNING: R/W mount of %s denied. Filesystem is not clean - run fsck\n", 320 fs->fs_fsmnt); 321 if (fs->fs_flags & FS_SUJ) 322 printf( 323 "WARNING: Forced mount will invalidate journal contents\n"); 324 return (EPERM); 325 } 326 } 327 DROP_GIANT(); 328 g_topology_lock(); 329 /* 330 * If we're the root device, we may not have an E count 331 * yet, get it now. 332 */ 333 if (ump->um_cp->ace == 0) 334 error = g_access(ump->um_cp, 0, 1, 1); 335 else 336 error = g_access(ump->um_cp, 0, 1, 0); 337 g_topology_unlock(); 338 PICKUP_GIANT(); 339 if (error) 340 return (error); 341 if ((error = vn_start_write(NULL, &mp, V_WAIT)) != 0) 342 return (error); 343 fs->fs_ronly = 0; 344 MNT_ILOCK(mp); 345 mp->mnt_flag &= ~MNT_RDONLY; 346 MNT_IUNLOCK(mp); 347 fs->fs_mtime = time_second; 348 /* check to see if we need to start softdep */ 349 if ((fs->fs_flags & FS_DOSOFTDEP) && 350 (error = softdep_mount(devvp, mp, fs, td->td_ucred))){ 351 vn_finished_write(mp); 352 return (error); 353 } 354 fs->fs_clean = 0; 355 if ((error = ffs_sbupdate(ump, MNT_WAIT, 0)) != 0) { 356 vn_finished_write(mp); 357 return (error); 358 } 359 if (fs->fs_snapinum[0] != 0) 360 ffs_snapshot_mount(mp); 361 vn_finished_write(mp); 362 } 363 /* 364 * Soft updates is incompatible with "async", 365 * so if we are doing softupdates stop the user 366 * from setting the async flag in an update. 367 * Softdep_mount() clears it in an initial mount 368 * or ro->rw remount. 369 */ 370 if (mp->mnt_flag & MNT_SOFTDEP) { 371 /* XXX: Reset too late ? */ 372 MNT_ILOCK(mp); 373 mp->mnt_flag &= ~MNT_ASYNC; 374 MNT_IUNLOCK(mp); 375 } 376 /* 377 * Keep MNT_ACLS flag if it is stored in superblock. 378 */ 379 if ((fs->fs_flags & FS_ACLS) != 0) { 380 /* XXX: Set too late ? */ 381 MNT_ILOCK(mp); 382 mp->mnt_flag |= MNT_ACLS; 383 MNT_IUNLOCK(mp); 384 } 385 386 if ((fs->fs_flags & FS_NFS4ACLS) != 0) { 387 /* XXX: Set too late ? */ 388 MNT_ILOCK(mp); 389 mp->mnt_flag |= MNT_NFS4ACLS; 390 MNT_IUNLOCK(mp); 391 } 392 393 /* 394 * If this is a snapshot request, take the snapshot. 395 */ 396 if (mp->mnt_flag & MNT_SNAPSHOT) 397 return (ffs_snapshot(mp, fspec)); 398 } 399 400 /* 401 * Not an update, or updating the name: look up the name 402 * and verify that it refers to a sensible disk device. 403 */ 404 NDINIT(&ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, fspec, td); 405 if ((error = namei(&ndp)) != 0) 406 return (error); 407 NDFREE(&ndp, NDF_ONLY_PNBUF); 408 devvp = ndp.ni_vp; 409 if (!vn_isdisk(devvp, &error)) { 410 vput(devvp); 411 return (error); 412 } 413 414 /* 415 * If mount by non-root, then verify that user has necessary 416 * permissions on the device. 417 */ 418 accmode = VREAD; 419 if ((mp->mnt_flag & MNT_RDONLY) == 0) 420 accmode |= VWRITE; 421 error = VOP_ACCESS(devvp, accmode, td->td_ucred, td); 422 if (error) 423 error = priv_check(td, PRIV_VFS_MOUNT_PERM); 424 if (error) { 425 vput(devvp); 426 return (error); 427 } 428 429 if (mp->mnt_flag & MNT_UPDATE) { 430 /* 431 * Update only 432 * 433 * If it's not the same vnode, or at least the same device 434 * then it's not correct. 435 */ 436 437 if (devvp->v_rdev != ump->um_devvp->v_rdev) 438 error = EINVAL; /* needs translation */ 439 vput(devvp); 440 if (error) 441 return (error); 442 } else { 443 /* 444 * New mount 445 * 446 * We need the name for the mount point (also used for 447 * "last mounted on") copied in. If an error occurs, 448 * the mount point is discarded by the upper level code. 449 * Note that vfs_mount() populates f_mntonname for us. 450 */ 451 if ((error = ffs_mountfs(devvp, mp, td)) != 0) { 452 vrele(devvp); 453 return (error); 454 } 455 } 456 vfs_mountedfrom(mp, fspec); 457 return (0); 458 } 459 460 /* 461 * Compatibility with old mount system call. 462 */ 463 464 static int 465 ffs_cmount(struct mntarg *ma, void *data, int flags) 466 { 467 struct ufs_args args; 468 struct export_args exp; 469 int error; 470 471 if (data == NULL) 472 return (EINVAL); 473 error = copyin(data, &args, sizeof args); 474 if (error) 475 return (error); 476 vfs_oexport_conv(&args.export, &exp); 477 478 ma = mount_argsu(ma, "from", args.fspec, MAXPATHLEN); 479 ma = mount_arg(ma, "export", &exp, sizeof(exp)); 480 error = kernel_mount(ma, flags); 481 482 return (error); 483 } 484 485 /* 486 * Reload all incore data for a filesystem (used after running fsck on 487 * the root filesystem and finding things to fix). The filesystem must 488 * be mounted read-only. 489 * 490 * Things to do to update the mount: 491 * 1) invalidate all cached meta-data. 492 * 2) re-read superblock from disk. 493 * 3) re-read summary information from disk. 494 * 4) invalidate all inactive vnodes. 495 * 5) invalidate all cached file data. 496 * 6) re-read inode data for all active vnodes. 497 */ 498 static int 499 ffs_reload(struct mount *mp, struct thread *td) 500 { 501 struct vnode *vp, *mvp, *devvp; 502 struct inode *ip; 503 void *space; 504 struct buf *bp; 505 struct fs *fs, *newfs; 506 struct ufsmount *ump; 507 ufs2_daddr_t sblockloc; 508 int i, blks, size, error; 509 int32_t *lp; 510 511 if ((mp->mnt_flag & MNT_RDONLY) == 0) 512 return (EINVAL); 513 ump = VFSTOUFS(mp); 514 /* 515 * Step 1: invalidate all cached meta-data. 516 */ 517 devvp = VFSTOUFS(mp)->um_devvp; 518 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); 519 if (vinvalbuf(devvp, 0, 0, 0) != 0) 520 panic("ffs_reload: dirty1"); 521 VOP_UNLOCK(devvp, 0); 522 523 /* 524 * Step 2: re-read superblock from disk. 525 */ 526 fs = VFSTOUFS(mp)->um_fs; 527 if ((error = bread(devvp, btodb(fs->fs_sblockloc), fs->fs_sbsize, 528 NOCRED, &bp)) != 0) 529 return (error); 530 newfs = (struct fs *)bp->b_data; 531 if ((newfs->fs_magic != FS_UFS1_MAGIC && 532 newfs->fs_magic != FS_UFS2_MAGIC) || 533 newfs->fs_bsize > MAXBSIZE || 534 newfs->fs_bsize < sizeof(struct fs)) { 535 brelse(bp); 536 return (EIO); /* XXX needs translation */ 537 } 538 /* 539 * Copy pointer fields back into superblock before copying in XXX 540 * new superblock. These should really be in the ufsmount. XXX 541 * Note that important parameters (eg fs_ncg) are unchanged. 542 */ 543 newfs->fs_csp = fs->fs_csp; 544 newfs->fs_maxcluster = fs->fs_maxcluster; 545 newfs->fs_contigdirs = fs->fs_contigdirs; 546 newfs->fs_active = fs->fs_active; 547 /* The file system is still read-only. */ 548 newfs->fs_ronly = 1; 549 sblockloc = fs->fs_sblockloc; 550 bcopy(newfs, fs, (u_int)fs->fs_sbsize); 551 brelse(bp); 552 mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen; 553 ffs_oldfscompat_read(fs, VFSTOUFS(mp), sblockloc); 554 UFS_LOCK(ump); 555 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) { 556 printf("%s: reload pending error: blocks %jd files %d\n", 557 fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks, 558 fs->fs_pendinginodes); 559 fs->fs_pendingblocks = 0; 560 fs->fs_pendinginodes = 0; 561 } 562 UFS_UNLOCK(ump); 563 564 /* 565 * Step 3: re-read summary information from disk. 566 */ 567 blks = howmany(fs->fs_cssize, fs->fs_fsize); 568 space = fs->fs_csp; 569 for (i = 0; i < blks; i += fs->fs_frag) { 570 size = fs->fs_bsize; 571 if (i + fs->fs_frag > blks) 572 size = (blks - i) * fs->fs_fsize; 573 error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size, 574 NOCRED, &bp); 575 if (error) 576 return (error); 577 bcopy(bp->b_data, space, (u_int)size); 578 space = (char *)space + size; 579 brelse(bp); 580 } 581 /* 582 * We no longer know anything about clusters per cylinder group. 583 */ 584 if (fs->fs_contigsumsize > 0) { 585 lp = fs->fs_maxcluster; 586 for (i = 0; i < fs->fs_ncg; i++) 587 *lp++ = fs->fs_contigsumsize; 588 } 589 590 loop: 591 MNT_ILOCK(mp); 592 MNT_VNODE_FOREACH(vp, mp, mvp) { 593 VI_LOCK(vp); 594 if (vp->v_iflag & VI_DOOMED) { 595 VI_UNLOCK(vp); 596 continue; 597 } 598 MNT_IUNLOCK(mp); 599 /* 600 * Step 4: invalidate all cached file data. 601 */ 602 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td)) { 603 MNT_VNODE_FOREACH_ABORT(mp, mvp); 604 goto loop; 605 } 606 if (vinvalbuf(vp, 0, 0, 0)) 607 panic("ffs_reload: dirty2"); 608 /* 609 * Step 5: re-read inode data for all active vnodes. 610 */ 611 ip = VTOI(vp); 612 error = 613 bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)), 614 (int)fs->fs_bsize, NOCRED, &bp); 615 if (error) { 616 VOP_UNLOCK(vp, 0); 617 vrele(vp); 618 MNT_VNODE_FOREACH_ABORT(mp, mvp); 619 return (error); 620 } 621 ffs_load_inode(bp, ip, fs, ip->i_number); 622 ip->i_effnlink = ip->i_nlink; 623 brelse(bp); 624 VOP_UNLOCK(vp, 0); 625 vrele(vp); 626 MNT_ILOCK(mp); 627 } 628 MNT_IUNLOCK(mp); 629 return (0); 630 } 631 632 /* 633 * Possible superblock locations ordered from most to least likely. 634 */ 635 static int sblock_try[] = SBLOCKSEARCH; 636 637 /* 638 * Common code for mount and mountroot 639 */ 640 static int 641 ffs_mountfs(devvp, mp, td) 642 struct vnode *devvp; 643 struct mount *mp; 644 struct thread *td; 645 { 646 struct ufsmount *ump; 647 struct buf *bp; 648 struct fs *fs; 649 struct cdev *dev; 650 void *space; 651 ufs2_daddr_t sblockloc; 652 int error, i, blks, size, ronly; 653 int32_t *lp; 654 struct ucred *cred; 655 struct g_consumer *cp; 656 struct mount *nmp; 657 658 bp = NULL; 659 ump = NULL; 660 cred = td ? td->td_ucred : NOCRED; 661 ronly = (mp->mnt_flag & MNT_RDONLY) != 0; 662 663 dev = devvp->v_rdev; 664 dev_ref(dev); 665 DROP_GIANT(); 666 g_topology_lock(); 667 error = g_vfs_open(devvp, &cp, "ffs", ronly ? 0 : 1); 668 669 /* 670 * If we are a root mount, drop the E flag so fsck can do its magic. 671 * We will pick it up again when we remount R/W. 672 */ 673 if (error == 0 && ronly && (mp->mnt_flag & MNT_ROOTFS)) 674 error = g_access(cp, 0, 0, -1); 675 g_topology_unlock(); 676 PICKUP_GIANT(); 677 VOP_UNLOCK(devvp, 0); 678 if (error) 679 goto out; 680 if (devvp->v_rdev->si_iosize_max != 0) 681 mp->mnt_iosize_max = devvp->v_rdev->si_iosize_max; 682 if (mp->mnt_iosize_max > MAXPHYS) 683 mp->mnt_iosize_max = MAXPHYS; 684 685 devvp->v_bufobj.bo_ops = &ffs_ops; 686 687 fs = NULL; 688 sblockloc = 0; 689 /* 690 * Try reading the superblock in each of its possible locations. 691 */ 692 for (i = 0; sblock_try[i] != -1; i++) { 693 if ((SBLOCKSIZE % cp->provider->sectorsize) != 0) { 694 error = EINVAL; 695 vfs_mount_error(mp, 696 "Invalid sectorsize %d for superblock size %d", 697 cp->provider->sectorsize, SBLOCKSIZE); 698 goto out; 699 } 700 if ((error = bread(devvp, btodb(sblock_try[i]), SBLOCKSIZE, 701 cred, &bp)) != 0) 702 goto out; 703 fs = (struct fs *)bp->b_data; 704 sblockloc = sblock_try[i]; 705 if ((fs->fs_magic == FS_UFS1_MAGIC || 706 (fs->fs_magic == FS_UFS2_MAGIC && 707 (fs->fs_sblockloc == sblockloc || 708 (fs->fs_old_flags & FS_FLAGS_UPDATED) == 0))) && 709 fs->fs_bsize <= MAXBSIZE && 710 fs->fs_bsize >= sizeof(struct fs)) 711 break; 712 brelse(bp); 713 bp = NULL; 714 } 715 if (sblock_try[i] == -1) { 716 error = EINVAL; /* XXX needs translation */ 717 goto out; 718 } 719 fs->fs_fmod = 0; 720 fs->fs_flags &= ~FS_INDEXDIRS; /* no support for directory indicies */ 721 fs->fs_flags &= ~FS_UNCLEAN; 722 if (fs->fs_clean == 0) { 723 fs->fs_flags |= FS_UNCLEAN; 724 if (ronly || (mp->mnt_flag & MNT_FORCE) || 725 ((fs->fs_flags & (FS_SUJ | FS_NEEDSFSCK)) == 0 && 726 (fs->fs_flags & FS_DOSOFTDEP))) { 727 printf("WARNING: %s was not properly dismounted\n", 728 fs->fs_fsmnt); 729 } else { 730 printf( 731 "WARNING: R/W mount of %s denied. Filesystem is not clean - run fsck\n", 732 fs->fs_fsmnt); 733 if (fs->fs_flags & FS_SUJ) 734 printf( 735 "WARNING: Forced mount will invalidate journal contents\n"); 736 error = EPERM; 737 goto out; 738 } 739 if ((fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) && 740 (mp->mnt_flag & MNT_FORCE)) { 741 printf("%s: lost blocks %jd files %d\n", fs->fs_fsmnt, 742 (intmax_t)fs->fs_pendingblocks, 743 fs->fs_pendinginodes); 744 fs->fs_pendingblocks = 0; 745 fs->fs_pendinginodes = 0; 746 } 747 } 748 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) { 749 printf("%s: mount pending error: blocks %jd files %d\n", 750 fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks, 751 fs->fs_pendinginodes); 752 fs->fs_pendingblocks = 0; 753 fs->fs_pendinginodes = 0; 754 } 755 if ((fs->fs_flags & FS_GJOURNAL) != 0) { 756 #ifdef UFS_GJOURNAL 757 /* 758 * Get journal provider name. 759 */ 760 size = 1024; 761 mp->mnt_gjprovider = malloc(size, M_UFSMNT, M_WAITOK); 762 if (g_io_getattr("GJOURNAL::provider", cp, &size, 763 mp->mnt_gjprovider) == 0) { 764 mp->mnt_gjprovider = realloc(mp->mnt_gjprovider, size, 765 M_UFSMNT, M_WAITOK); 766 MNT_ILOCK(mp); 767 mp->mnt_flag |= MNT_GJOURNAL; 768 MNT_IUNLOCK(mp); 769 } else { 770 printf( 771 "WARNING: %s: GJOURNAL flag on fs but no gjournal provider below\n", 772 mp->mnt_stat.f_mntonname); 773 free(mp->mnt_gjprovider, M_UFSMNT); 774 mp->mnt_gjprovider = NULL; 775 } 776 #else 777 printf( 778 "WARNING: %s: GJOURNAL flag on fs but no UFS_GJOURNAL support\n", 779 mp->mnt_stat.f_mntonname); 780 #endif 781 } else { 782 mp->mnt_gjprovider = NULL; 783 } 784 ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK | M_ZERO); 785 ump->um_cp = cp; 786 ump->um_bo = &devvp->v_bufobj; 787 ump->um_fs = malloc((u_long)fs->fs_sbsize, M_UFSMNT, M_WAITOK); 788 if (fs->fs_magic == FS_UFS1_MAGIC) { 789 ump->um_fstype = UFS1; 790 ump->um_balloc = ffs_balloc_ufs1; 791 } else { 792 ump->um_fstype = UFS2; 793 ump->um_balloc = ffs_balloc_ufs2; 794 } 795 ump->um_blkatoff = ffs_blkatoff; 796 ump->um_truncate = ffs_truncate; 797 ump->um_update = ffs_update; 798 ump->um_valloc = ffs_valloc; 799 ump->um_vfree = ffs_vfree; 800 ump->um_ifree = ffs_ifree; 801 ump->um_rdonly = ffs_rdonly; 802 ump->um_snapgone = ffs_snapgone; 803 mtx_init(UFS_MTX(ump), "FFS", "FFS Lock", MTX_DEF); 804 bcopy(bp->b_data, ump->um_fs, (u_int)fs->fs_sbsize); 805 if (fs->fs_sbsize < SBLOCKSIZE) 806 bp->b_flags |= B_INVAL | B_NOCACHE; 807 brelse(bp); 808 bp = NULL; 809 fs = ump->um_fs; 810 ffs_oldfscompat_read(fs, ump, sblockloc); 811 fs->fs_ronly = ronly; 812 size = fs->fs_cssize; 813 blks = howmany(size, fs->fs_fsize); 814 if (fs->fs_contigsumsize > 0) 815 size += fs->fs_ncg * sizeof(int32_t); 816 size += fs->fs_ncg * sizeof(u_int8_t); 817 space = malloc((u_long)size, M_UFSMNT, M_WAITOK); 818 fs->fs_csp = space; 819 for (i = 0; i < blks; i += fs->fs_frag) { 820 size = fs->fs_bsize; 821 if (i + fs->fs_frag > blks) 822 size = (blks - i) * fs->fs_fsize; 823 if ((error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size, 824 cred, &bp)) != 0) { 825 free(fs->fs_csp, M_UFSMNT); 826 goto out; 827 } 828 bcopy(bp->b_data, space, (u_int)size); 829 space = (char *)space + size; 830 brelse(bp); 831 bp = NULL; 832 } 833 if (fs->fs_contigsumsize > 0) { 834 fs->fs_maxcluster = lp = space; 835 for (i = 0; i < fs->fs_ncg; i++) 836 *lp++ = fs->fs_contigsumsize; 837 space = lp; 838 } 839 size = fs->fs_ncg * sizeof(u_int8_t); 840 fs->fs_contigdirs = (u_int8_t *)space; 841 bzero(fs->fs_contigdirs, size); 842 fs->fs_active = NULL; 843 mp->mnt_data = ump; 844 mp->mnt_stat.f_fsid.val[0] = fs->fs_id[0]; 845 mp->mnt_stat.f_fsid.val[1] = fs->fs_id[1]; 846 nmp = NULL; 847 if (fs->fs_id[0] == 0 || fs->fs_id[1] == 0 || 848 (nmp = vfs_getvfs(&mp->mnt_stat.f_fsid))) { 849 if (nmp) 850 vfs_rel(nmp); 851 vfs_getnewfsid(mp); 852 } 853 mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen; 854 MNT_ILOCK(mp); 855 mp->mnt_flag |= MNT_LOCAL; 856 MNT_IUNLOCK(mp); 857 if ((fs->fs_flags & FS_MULTILABEL) != 0) { 858 #ifdef MAC 859 MNT_ILOCK(mp); 860 mp->mnt_flag |= MNT_MULTILABEL; 861 MNT_IUNLOCK(mp); 862 #else 863 printf( 864 "WARNING: %s: multilabel flag on fs but no MAC support\n", 865 mp->mnt_stat.f_mntonname); 866 #endif 867 } 868 if ((fs->fs_flags & FS_ACLS) != 0) { 869 #ifdef UFS_ACL 870 MNT_ILOCK(mp); 871 872 if (mp->mnt_flag & MNT_NFS4ACLS) 873 printf("WARNING: ACLs flag on fs conflicts with " 874 "\"nfsv4acls\" mount option; option ignored\n"); 875 mp->mnt_flag &= ~MNT_NFS4ACLS; 876 mp->mnt_flag |= MNT_ACLS; 877 878 MNT_IUNLOCK(mp); 879 #else 880 printf("WARNING: %s: ACLs flag on fs but no ACLs support\n", 881 mp->mnt_stat.f_mntonname); 882 #endif 883 } 884 if ((fs->fs_flags & FS_NFS4ACLS) != 0) { 885 #ifdef UFS_ACL 886 MNT_ILOCK(mp); 887 888 if (mp->mnt_flag & MNT_ACLS) 889 printf("WARNING: NFSv4 ACLs flag on fs conflicts with " 890 "\"acls\" mount option; option ignored\n"); 891 mp->mnt_flag &= ~MNT_ACLS; 892 mp->mnt_flag |= MNT_NFS4ACLS; 893 894 MNT_IUNLOCK(mp); 895 #else 896 printf( 897 "WARNING: %s: NFSv4 ACLs flag on fs but no ACLs support\n", 898 mp->mnt_stat.f_mntonname); 899 #endif 900 } 901 if ((fs->fs_flags & FS_TRIM) != 0) { 902 size = sizeof(int); 903 if (g_io_getattr("GEOM::candelete", cp, &size, 904 &ump->um_candelete) == 0) { 905 if (!ump->um_candelete) 906 printf( 907 "WARNING: %s: TRIM flag on fs but disk does not support TRIM\n", 908 mp->mnt_stat.f_mntonname); 909 } else { 910 printf( 911 "WARNING: %s: TRIM flag on fs but cannot get whether disk supports TRIM\n", 912 mp->mnt_stat.f_mntonname); 913 ump->um_candelete = 0; 914 } 915 } 916 917 ump->um_mountp = mp; 918 ump->um_dev = dev; 919 ump->um_devvp = devvp; 920 ump->um_nindir = fs->fs_nindir; 921 ump->um_bptrtodb = fs->fs_fsbtodb; 922 ump->um_seqinc = fs->fs_frag; 923 for (i = 0; i < MAXQUOTAS; i++) 924 ump->um_quotas[i] = NULLVP; 925 #ifdef UFS_EXTATTR 926 ufs_extattr_uepm_init(&ump->um_extattr); 927 #endif 928 /* 929 * Set FS local "last mounted on" information (NULL pad) 930 */ 931 bzero(fs->fs_fsmnt, MAXMNTLEN); 932 strlcpy(fs->fs_fsmnt, mp->mnt_stat.f_mntonname, MAXMNTLEN); 933 mp->mnt_stat.f_iosize = fs->fs_bsize; 934 935 if( mp->mnt_flag & MNT_ROOTFS) { 936 /* 937 * Root mount; update timestamp in mount structure. 938 * this will be used by the common root mount code 939 * to update the system clock. 940 */ 941 mp->mnt_time = fs->fs_time; 942 } 943 944 if (ronly == 0) { 945 fs->fs_mtime = time_second; 946 if ((fs->fs_flags & FS_DOSOFTDEP) && 947 (error = softdep_mount(devvp, mp, fs, cred)) != 0) { 948 free(fs->fs_csp, M_UFSMNT); 949 ffs_flushfiles(mp, FORCECLOSE, td); 950 goto out; 951 } 952 if (fs->fs_snapinum[0] != 0) 953 ffs_snapshot_mount(mp); 954 fs->fs_fmod = 1; 955 fs->fs_clean = 0; 956 (void) ffs_sbupdate(ump, MNT_WAIT, 0); 957 } 958 /* 959 * Initialize filesystem stat information in mount struct. 960 */ 961 MNT_ILOCK(mp); 962 mp->mnt_kern_flag |= MNTK_MPSAFE | MNTK_LOOKUP_SHARED | 963 MNTK_EXTENDED_SHARED; 964 MNT_IUNLOCK(mp); 965 #ifdef UFS_EXTATTR 966 #ifdef UFS_EXTATTR_AUTOSTART 967 /* 968 * 969 * Auto-starting does the following: 970 * - check for /.attribute in the fs, and extattr_start if so 971 * - for each file in .attribute, enable that file with 972 * an attribute of the same name. 973 * Not clear how to report errors -- probably eat them. 974 * This would all happen while the filesystem was busy/not 975 * available, so would effectively be "atomic". 976 */ 977 (void) ufs_extattr_autostart(mp, td); 978 #endif /* !UFS_EXTATTR_AUTOSTART */ 979 #endif /* !UFS_EXTATTR */ 980 return (0); 981 out: 982 if (bp) 983 brelse(bp); 984 if (cp != NULL) { 985 DROP_GIANT(); 986 g_topology_lock(); 987 g_vfs_close(cp); 988 g_topology_unlock(); 989 PICKUP_GIANT(); 990 } 991 if (ump) { 992 mtx_destroy(UFS_MTX(ump)); 993 if (mp->mnt_gjprovider != NULL) { 994 free(mp->mnt_gjprovider, M_UFSMNT); 995 mp->mnt_gjprovider = NULL; 996 } 997 free(ump->um_fs, M_UFSMNT); 998 free(ump, M_UFSMNT); 999 mp->mnt_data = NULL; 1000 } 1001 dev_rel(dev); 1002 return (error); 1003 } 1004 1005 #include <sys/sysctl.h> 1006 static int bigcgs = 0; 1007 SYSCTL_INT(_debug, OID_AUTO, bigcgs, CTLFLAG_RW, &bigcgs, 0, ""); 1008 1009 /* 1010 * Sanity checks for loading old filesystem superblocks. 1011 * See ffs_oldfscompat_write below for unwound actions. 1012 * 1013 * XXX - Parts get retired eventually. 1014 * Unfortunately new bits get added. 1015 */ 1016 static void 1017 ffs_oldfscompat_read(fs, ump, sblockloc) 1018 struct fs *fs; 1019 struct ufsmount *ump; 1020 ufs2_daddr_t sblockloc; 1021 { 1022 off_t maxfilesize; 1023 1024 /* 1025 * If not yet done, update fs_flags location and value of fs_sblockloc. 1026 */ 1027 if ((fs->fs_old_flags & FS_FLAGS_UPDATED) == 0) { 1028 fs->fs_flags = fs->fs_old_flags; 1029 fs->fs_old_flags |= FS_FLAGS_UPDATED; 1030 fs->fs_sblockloc = sblockloc; 1031 } 1032 /* 1033 * If not yet done, update UFS1 superblock with new wider fields. 1034 */ 1035 if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_maxbsize != fs->fs_bsize) { 1036 fs->fs_maxbsize = fs->fs_bsize; 1037 fs->fs_time = fs->fs_old_time; 1038 fs->fs_size = fs->fs_old_size; 1039 fs->fs_dsize = fs->fs_old_dsize; 1040 fs->fs_csaddr = fs->fs_old_csaddr; 1041 fs->fs_cstotal.cs_ndir = fs->fs_old_cstotal.cs_ndir; 1042 fs->fs_cstotal.cs_nbfree = fs->fs_old_cstotal.cs_nbfree; 1043 fs->fs_cstotal.cs_nifree = fs->fs_old_cstotal.cs_nifree; 1044 fs->fs_cstotal.cs_nffree = fs->fs_old_cstotal.cs_nffree; 1045 } 1046 if (fs->fs_magic == FS_UFS1_MAGIC && 1047 fs->fs_old_inodefmt < FS_44INODEFMT) { 1048 fs->fs_maxfilesize = ((uint64_t)1 << 31) - 1; 1049 fs->fs_qbmask = ~fs->fs_bmask; 1050 fs->fs_qfmask = ~fs->fs_fmask; 1051 } 1052 if (fs->fs_magic == FS_UFS1_MAGIC) { 1053 ump->um_savedmaxfilesize = fs->fs_maxfilesize; 1054 maxfilesize = (uint64_t)0x80000000 * fs->fs_bsize - 1; 1055 if (fs->fs_maxfilesize > maxfilesize) 1056 fs->fs_maxfilesize = maxfilesize; 1057 } 1058 /* Compatibility for old filesystems */ 1059 if (fs->fs_avgfilesize <= 0) 1060 fs->fs_avgfilesize = AVFILESIZ; 1061 if (fs->fs_avgfpdir <= 0) 1062 fs->fs_avgfpdir = AFPDIR; 1063 if (bigcgs) { 1064 fs->fs_save_cgsize = fs->fs_cgsize; 1065 fs->fs_cgsize = fs->fs_bsize; 1066 } 1067 } 1068 1069 /* 1070 * Unwinding superblock updates for old filesystems. 1071 * See ffs_oldfscompat_read above for details. 1072 * 1073 * XXX - Parts get retired eventually. 1074 * Unfortunately new bits get added. 1075 */ 1076 void 1077 ffs_oldfscompat_write(fs, ump) 1078 struct fs *fs; 1079 struct ufsmount *ump; 1080 { 1081 1082 /* 1083 * Copy back UFS2 updated fields that UFS1 inspects. 1084 */ 1085 if (fs->fs_magic == FS_UFS1_MAGIC) { 1086 fs->fs_old_time = fs->fs_time; 1087 fs->fs_old_cstotal.cs_ndir = fs->fs_cstotal.cs_ndir; 1088 fs->fs_old_cstotal.cs_nbfree = fs->fs_cstotal.cs_nbfree; 1089 fs->fs_old_cstotal.cs_nifree = fs->fs_cstotal.cs_nifree; 1090 fs->fs_old_cstotal.cs_nffree = fs->fs_cstotal.cs_nffree; 1091 fs->fs_maxfilesize = ump->um_savedmaxfilesize; 1092 } 1093 if (bigcgs) { 1094 fs->fs_cgsize = fs->fs_save_cgsize; 1095 fs->fs_save_cgsize = 0; 1096 } 1097 } 1098 1099 /* 1100 * unmount system call 1101 */ 1102 static int 1103 ffs_unmount(mp, mntflags) 1104 struct mount *mp; 1105 int mntflags; 1106 { 1107 struct thread *td; 1108 struct ufsmount *ump = VFSTOUFS(mp); 1109 struct fs *fs; 1110 int error, flags, susp; 1111 #ifdef UFS_EXTATTR 1112 int e_restart; 1113 #endif 1114 1115 flags = 0; 1116 td = curthread; 1117 fs = ump->um_fs; 1118 if (mntflags & MNT_FORCE) { 1119 flags |= FORCECLOSE; 1120 susp = fs->fs_ronly != 0; 1121 } else 1122 susp = 0; 1123 #ifdef UFS_EXTATTR 1124 if ((error = ufs_extattr_stop(mp, td))) { 1125 if (error != EOPNOTSUPP) 1126 printf("ffs_unmount: ufs_extattr_stop returned %d\n", 1127 error); 1128 e_restart = 0; 1129 } else { 1130 ufs_extattr_uepm_destroy(&ump->um_extattr); 1131 e_restart = 1; 1132 } 1133 #endif 1134 if (susp) { 1135 /* 1136 * dounmount already called vn_start_write(). 1137 */ 1138 for (;;) { 1139 vn_finished_write(mp); 1140 if ((error = vfs_write_suspend(mp)) != 0) 1141 return (error); 1142 MNT_ILOCK(mp); 1143 if (mp->mnt_kern_flag & MNTK_SUSPENDED) { 1144 mp->mnt_kern_flag &= ~(MNTK_SUSPENDED | 1145 MNTK_SUSPEND2); 1146 wakeup(&mp->mnt_flag); 1147 MNT_IUNLOCK(mp); 1148 td->td_pflags |= TDP_IGNSUSP; 1149 break; 1150 } 1151 MNT_IUNLOCK(mp); 1152 vn_start_write(NULL, &mp, V_WAIT); 1153 } 1154 } 1155 if (mp->mnt_flag & MNT_SOFTDEP) 1156 error = softdep_flushfiles(mp, flags, td); 1157 else 1158 error = ffs_flushfiles(mp, flags, td); 1159 if (error != 0 && error != ENXIO) 1160 goto fail; 1161 1162 UFS_LOCK(ump); 1163 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) { 1164 printf("%s: unmount pending error: blocks %jd files %d\n", 1165 fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks, 1166 fs->fs_pendinginodes); 1167 fs->fs_pendingblocks = 0; 1168 fs->fs_pendinginodes = 0; 1169 } 1170 UFS_UNLOCK(ump); 1171 softdep_unmount(mp); 1172 if (fs->fs_ronly == 0) { 1173 fs->fs_clean = fs->fs_flags & (FS_UNCLEAN|FS_NEEDSFSCK) ? 0 : 1; 1174 error = ffs_sbupdate(ump, MNT_WAIT, 0); 1175 if (error && error != ENXIO) { 1176 fs->fs_clean = 0; 1177 goto fail; 1178 } 1179 } 1180 if (susp) { 1181 vfs_write_resume(mp); 1182 vn_start_write(NULL, &mp, V_WAIT); 1183 } 1184 DROP_GIANT(); 1185 g_topology_lock(); 1186 g_vfs_close(ump->um_cp); 1187 g_topology_unlock(); 1188 PICKUP_GIANT(); 1189 vrele(ump->um_devvp); 1190 dev_rel(ump->um_dev); 1191 mtx_destroy(UFS_MTX(ump)); 1192 if (mp->mnt_gjprovider != NULL) { 1193 free(mp->mnt_gjprovider, M_UFSMNT); 1194 mp->mnt_gjprovider = NULL; 1195 } 1196 free(fs->fs_csp, M_UFSMNT); 1197 free(fs, M_UFSMNT); 1198 free(ump, M_UFSMNT); 1199 mp->mnt_data = NULL; 1200 MNT_ILOCK(mp); 1201 mp->mnt_flag &= ~MNT_LOCAL; 1202 MNT_IUNLOCK(mp); 1203 return (error); 1204 1205 fail: 1206 if (susp) { 1207 vfs_write_resume(mp); 1208 vn_start_write(NULL, &mp, V_WAIT); 1209 } 1210 #ifdef UFS_EXTATTR 1211 if (e_restart) { 1212 ufs_extattr_uepm_init(&ump->um_extattr); 1213 #ifdef UFS_EXTATTR_AUTOSTART 1214 (void) ufs_extattr_autostart(mp, td); 1215 #endif 1216 } 1217 #endif 1218 1219 return (error); 1220 } 1221 1222 /* 1223 * Flush out all the files in a filesystem. 1224 */ 1225 int 1226 ffs_flushfiles(mp, flags, td) 1227 struct mount *mp; 1228 int flags; 1229 struct thread *td; 1230 { 1231 struct ufsmount *ump; 1232 int error; 1233 1234 ump = VFSTOUFS(mp); 1235 #ifdef QUOTA 1236 if (mp->mnt_flag & MNT_QUOTA) { 1237 int i; 1238 error = vflush(mp, 0, SKIPSYSTEM|flags, td); 1239 if (error) 1240 return (error); 1241 for (i = 0; i < MAXQUOTAS; i++) { 1242 quotaoff(td, mp, i); 1243 } 1244 /* 1245 * Here we fall through to vflush again to ensure 1246 * that we have gotten rid of all the system vnodes. 1247 */ 1248 } 1249 #endif 1250 ASSERT_VOP_LOCKED(ump->um_devvp, "ffs_flushfiles"); 1251 if (ump->um_devvp->v_vflag & VV_COPYONWRITE) { 1252 if ((error = vflush(mp, 0, SKIPSYSTEM | flags, td)) != 0) 1253 return (error); 1254 ffs_snapshot_unmount(mp); 1255 flags |= FORCECLOSE; 1256 /* 1257 * Here we fall through to vflush again to ensure 1258 * that we have gotten rid of all the system vnodes. 1259 */ 1260 } 1261 /* 1262 * Flush all the files. 1263 */ 1264 if ((error = vflush(mp, 0, flags, td)) != 0) 1265 return (error); 1266 /* 1267 * Flush filesystem metadata. 1268 */ 1269 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY); 1270 error = VOP_FSYNC(ump->um_devvp, MNT_WAIT, td); 1271 VOP_UNLOCK(ump->um_devvp, 0); 1272 return (error); 1273 } 1274 1275 /* 1276 * Get filesystem statistics. 1277 */ 1278 static int 1279 ffs_statfs(mp, sbp) 1280 struct mount *mp; 1281 struct statfs *sbp; 1282 { 1283 struct ufsmount *ump; 1284 struct fs *fs; 1285 1286 ump = VFSTOUFS(mp); 1287 fs = ump->um_fs; 1288 if (fs->fs_magic != FS_UFS1_MAGIC && fs->fs_magic != FS_UFS2_MAGIC) 1289 panic("ffs_statfs"); 1290 sbp->f_version = STATFS_VERSION; 1291 sbp->f_bsize = fs->fs_fsize; 1292 sbp->f_iosize = fs->fs_bsize; 1293 sbp->f_blocks = fs->fs_dsize; 1294 UFS_LOCK(ump); 1295 sbp->f_bfree = fs->fs_cstotal.cs_nbfree * fs->fs_frag + 1296 fs->fs_cstotal.cs_nffree + dbtofsb(fs, fs->fs_pendingblocks); 1297 sbp->f_bavail = freespace(fs, fs->fs_minfree) + 1298 dbtofsb(fs, fs->fs_pendingblocks); 1299 sbp->f_files = fs->fs_ncg * fs->fs_ipg - ROOTINO; 1300 sbp->f_ffree = fs->fs_cstotal.cs_nifree + fs->fs_pendinginodes; 1301 UFS_UNLOCK(ump); 1302 sbp->f_namemax = NAME_MAX; 1303 return (0); 1304 } 1305 1306 /* 1307 * Go through the disk queues to initiate sandbagged IO; 1308 * go through the inodes to write those that have been modified; 1309 * initiate the writing of the super block if it has been modified. 1310 * 1311 * Note: we are always called with the filesystem marked `MPBUSY'. 1312 */ 1313 static int 1314 ffs_sync(mp, waitfor) 1315 struct mount *mp; 1316 int waitfor; 1317 { 1318 struct vnode *mvp, *vp, *devvp; 1319 struct thread *td; 1320 struct inode *ip; 1321 struct ufsmount *ump = VFSTOUFS(mp); 1322 struct fs *fs; 1323 int error, count, wait, lockreq, allerror = 0; 1324 int suspend; 1325 int suspended; 1326 int secondary_writes; 1327 int secondary_accwrites; 1328 int softdep_deps; 1329 int softdep_accdeps; 1330 struct bufobj *bo; 1331 1332 td = curthread; 1333 fs = ump->um_fs; 1334 if (fs->fs_fmod != 0 && fs->fs_ronly != 0) { /* XXX */ 1335 printf("fs = %s\n", fs->fs_fsmnt); 1336 panic("ffs_sync: rofs mod"); 1337 } 1338 /* 1339 * Write back each (modified) inode. 1340 */ 1341 wait = 0; 1342 suspend = 0; 1343 suspended = 0; 1344 lockreq = LK_EXCLUSIVE | LK_NOWAIT; 1345 if (waitfor == MNT_SUSPEND) { 1346 suspend = 1; 1347 waitfor = MNT_WAIT; 1348 } 1349 if (waitfor == MNT_WAIT) { 1350 wait = 1; 1351 lockreq = LK_EXCLUSIVE; 1352 } 1353 lockreq |= LK_INTERLOCK | LK_SLEEPFAIL; 1354 MNT_ILOCK(mp); 1355 loop: 1356 /* Grab snapshot of secondary write counts */ 1357 secondary_writes = mp->mnt_secondary_writes; 1358 secondary_accwrites = mp->mnt_secondary_accwrites; 1359 1360 /* Grab snapshot of softdep dependency counts */ 1361 MNT_IUNLOCK(mp); 1362 softdep_get_depcounts(mp, &softdep_deps, &softdep_accdeps); 1363 MNT_ILOCK(mp); 1364 1365 MNT_VNODE_FOREACH(vp, mp, mvp) { 1366 /* 1367 * Depend on the mntvnode_slock to keep things stable enough 1368 * for a quick test. Since there might be hundreds of 1369 * thousands of vnodes, we cannot afford even a subroutine 1370 * call unless there's a good chance that we have work to do. 1371 */ 1372 VI_LOCK(vp); 1373 if (vp->v_iflag & VI_DOOMED) { 1374 VI_UNLOCK(vp); 1375 continue; 1376 } 1377 ip = VTOI(vp); 1378 if (vp->v_type == VNON || ((ip->i_flag & 1379 (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 && 1380 vp->v_bufobj.bo_dirty.bv_cnt == 0)) { 1381 VI_UNLOCK(vp); 1382 continue; 1383 } 1384 MNT_IUNLOCK(mp); 1385 if ((error = vget(vp, lockreq, td)) != 0) { 1386 MNT_ILOCK(mp); 1387 if (error == ENOENT || error == ENOLCK) { 1388 MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp); 1389 goto loop; 1390 } 1391 continue; 1392 } 1393 if ((error = ffs_syncvnode(vp, waitfor)) != 0) 1394 allerror = error; 1395 vput(vp); 1396 MNT_ILOCK(mp); 1397 } 1398 MNT_IUNLOCK(mp); 1399 /* 1400 * Force stale filesystem control information to be flushed. 1401 */ 1402 if (waitfor == MNT_WAIT) { 1403 if ((error = softdep_flushworklist(ump->um_mountp, &count, td))) 1404 allerror = error; 1405 /* Flushed work items may create new vnodes to clean */ 1406 if (allerror == 0 && count) { 1407 MNT_ILOCK(mp); 1408 goto loop; 1409 } 1410 } 1411 #ifdef QUOTA 1412 qsync(mp); 1413 #endif 1414 devvp = ump->um_devvp; 1415 bo = &devvp->v_bufobj; 1416 BO_LOCK(bo); 1417 if (waitfor != MNT_LAZY && 1418 (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0)) { 1419 BO_UNLOCK(bo); 1420 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); 1421 if ((error = VOP_FSYNC(devvp, waitfor, td)) != 0) 1422 allerror = error; 1423 VOP_UNLOCK(devvp, 0); 1424 if (allerror == 0 && waitfor == MNT_WAIT) { 1425 MNT_ILOCK(mp); 1426 goto loop; 1427 } 1428 } else if (suspend != 0) { 1429 if (softdep_check_suspend(mp, 1430 devvp, 1431 softdep_deps, 1432 softdep_accdeps, 1433 secondary_writes, 1434 secondary_accwrites) != 0) 1435 goto loop; /* More work needed */ 1436 mtx_assert(MNT_MTX(mp), MA_OWNED); 1437 mp->mnt_kern_flag |= MNTK_SUSPEND2 | MNTK_SUSPENDED; 1438 MNT_IUNLOCK(mp); 1439 suspended = 1; 1440 } else 1441 BO_UNLOCK(bo); 1442 /* 1443 * Write back modified superblock. 1444 */ 1445 if (fs->fs_fmod != 0 && 1446 (error = ffs_sbupdate(ump, waitfor, suspended)) != 0) 1447 allerror = error; 1448 return (allerror); 1449 } 1450 1451 int 1452 ffs_vget(mp, ino, flags, vpp) 1453 struct mount *mp; 1454 ino_t ino; 1455 int flags; 1456 struct vnode **vpp; 1457 { 1458 return (ffs_vgetf(mp, ino, flags, vpp, 0)); 1459 } 1460 1461 int 1462 ffs_vgetf(mp, ino, flags, vpp, ffs_flags) 1463 struct mount *mp; 1464 ino_t ino; 1465 int flags; 1466 struct vnode **vpp; 1467 int ffs_flags; 1468 { 1469 struct fs *fs; 1470 struct inode *ip; 1471 struct ufsmount *ump; 1472 struct buf *bp; 1473 struct vnode *vp; 1474 struct cdev *dev; 1475 int error; 1476 1477 error = vfs_hash_get(mp, ino, flags, curthread, vpp, NULL, NULL); 1478 if (error || *vpp != NULL) 1479 return (error); 1480 1481 /* 1482 * We must promote to an exclusive lock for vnode creation. This 1483 * can happen if lookup is passed LOCKSHARED. 1484 */ 1485 if ((flags & LK_TYPE_MASK) == LK_SHARED) { 1486 flags &= ~LK_TYPE_MASK; 1487 flags |= LK_EXCLUSIVE; 1488 } 1489 1490 /* 1491 * We do not lock vnode creation as it is believed to be too 1492 * expensive for such rare case as simultaneous creation of vnode 1493 * for same ino by different processes. We just allow them to race 1494 * and check later to decide who wins. Let the race begin! 1495 */ 1496 1497 ump = VFSTOUFS(mp); 1498 dev = ump->um_dev; 1499 fs = ump->um_fs; 1500 1501 /* 1502 * If this malloc() is performed after the getnewvnode() 1503 * it might block, leaving a vnode with a NULL v_data to be 1504 * found by ffs_sync() if a sync happens to fire right then, 1505 * which will cause a panic because ffs_sync() blindly 1506 * dereferences vp->v_data (as well it should). 1507 */ 1508 ip = uma_zalloc(uma_inode, M_WAITOK | M_ZERO); 1509 1510 /* Allocate a new vnode/inode. */ 1511 if (fs->fs_magic == FS_UFS1_MAGIC) 1512 error = getnewvnode("ufs", mp, &ffs_vnodeops1, &vp); 1513 else 1514 error = getnewvnode("ufs", mp, &ffs_vnodeops2, &vp); 1515 if (error) { 1516 *vpp = NULL; 1517 uma_zfree(uma_inode, ip); 1518 return (error); 1519 } 1520 /* 1521 * FFS supports recursive locking. 1522 */ 1523 lockmgr(vp->v_vnlock, LK_EXCLUSIVE, NULL); 1524 VN_LOCK_AREC(vp); 1525 vp->v_data = ip; 1526 vp->v_bufobj.bo_bsize = fs->fs_bsize; 1527 ip->i_vnode = vp; 1528 ip->i_ump = ump; 1529 ip->i_fs = fs; 1530 ip->i_dev = dev; 1531 ip->i_number = ino; 1532 ip->i_ea_refs = 0; 1533 #ifdef QUOTA 1534 { 1535 int i; 1536 for (i = 0; i < MAXQUOTAS; i++) 1537 ip->i_dquot[i] = NODQUOT; 1538 } 1539 #endif 1540 1541 if (ffs_flags & FFSV_FORCEINSMQ) 1542 vp->v_vflag |= VV_FORCEINSMQ; 1543 error = insmntque(vp, mp); 1544 if (error != 0) { 1545 uma_zfree(uma_inode, ip); 1546 *vpp = NULL; 1547 return (error); 1548 } 1549 vp->v_vflag &= ~VV_FORCEINSMQ; 1550 error = vfs_hash_insert(vp, ino, flags, curthread, vpp, NULL, NULL); 1551 if (error || *vpp != NULL) 1552 return (error); 1553 1554 /* Read in the disk contents for the inode, copy into the inode. */ 1555 error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)), 1556 (int)fs->fs_bsize, NOCRED, &bp); 1557 if (error) { 1558 /* 1559 * The inode does not contain anything useful, so it would 1560 * be misleading to leave it on its hash chain. With mode 1561 * still zero, it will be unlinked and returned to the free 1562 * list by vput(). 1563 */ 1564 brelse(bp); 1565 vput(vp); 1566 *vpp = NULL; 1567 return (error); 1568 } 1569 if (ip->i_ump->um_fstype == UFS1) 1570 ip->i_din1 = uma_zalloc(uma_ufs1, M_WAITOK); 1571 else 1572 ip->i_din2 = uma_zalloc(uma_ufs2, M_WAITOK); 1573 ffs_load_inode(bp, ip, fs, ino); 1574 if (DOINGSOFTDEP(vp)) 1575 softdep_load_inodeblock(ip); 1576 else 1577 ip->i_effnlink = ip->i_nlink; 1578 bqrelse(bp); 1579 1580 /* 1581 * Initialize the vnode from the inode, check for aliases. 1582 * Note that the underlying vnode may have changed. 1583 */ 1584 if (ip->i_ump->um_fstype == UFS1) 1585 error = ufs_vinit(mp, &ffs_fifoops1, &vp); 1586 else 1587 error = ufs_vinit(mp, &ffs_fifoops2, &vp); 1588 if (error) { 1589 vput(vp); 1590 *vpp = NULL; 1591 return (error); 1592 } 1593 1594 /* 1595 * Finish inode initialization. 1596 */ 1597 if (vp->v_type != VFIFO) { 1598 /* FFS supports shared locking for all files except fifos. */ 1599 VN_LOCK_ASHARE(vp); 1600 } 1601 1602 /* 1603 * Set up a generation number for this inode if it does not 1604 * already have one. This should only happen on old filesystems. 1605 */ 1606 if (ip->i_gen == 0) { 1607 ip->i_gen = arc4random() / 2 + 1; 1608 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) { 1609 ip->i_flag |= IN_MODIFIED; 1610 DIP_SET(ip, i_gen, ip->i_gen); 1611 } 1612 } 1613 #ifdef MAC 1614 if ((mp->mnt_flag & MNT_MULTILABEL) && ip->i_mode) { 1615 /* 1616 * If this vnode is already allocated, and we're running 1617 * multi-label, attempt to perform a label association 1618 * from the extended attributes on the inode. 1619 */ 1620 error = mac_vnode_associate_extattr(mp, vp); 1621 if (error) { 1622 /* ufs_inactive will release ip->i_devvp ref. */ 1623 vput(vp); 1624 *vpp = NULL; 1625 return (error); 1626 } 1627 } 1628 #endif 1629 1630 *vpp = vp; 1631 return (0); 1632 } 1633 1634 /* 1635 * File handle to vnode 1636 * 1637 * Have to be really careful about stale file handles: 1638 * - check that the inode number is valid 1639 * - call ffs_vget() to get the locked inode 1640 * - check for an unallocated inode (i_mode == 0) 1641 * - check that the given client host has export rights and return 1642 * those rights via. exflagsp and credanonp 1643 */ 1644 static int 1645 ffs_fhtovp(mp, fhp, flags, vpp) 1646 struct mount *mp; 1647 struct fid *fhp; 1648 int flags; 1649 struct vnode **vpp; 1650 { 1651 struct ufid *ufhp; 1652 struct fs *fs; 1653 1654 ufhp = (struct ufid *)fhp; 1655 fs = VFSTOUFS(mp)->um_fs; 1656 if (ufhp->ufid_ino < ROOTINO || 1657 ufhp->ufid_ino >= fs->fs_ncg * fs->fs_ipg) 1658 return (ESTALE); 1659 return (ufs_fhtovp(mp, ufhp, flags, vpp)); 1660 } 1661 1662 /* 1663 * Initialize the filesystem. 1664 */ 1665 static int 1666 ffs_init(vfsp) 1667 struct vfsconf *vfsp; 1668 { 1669 1670 softdep_initialize(); 1671 return (ufs_init(vfsp)); 1672 } 1673 1674 /* 1675 * Undo the work of ffs_init(). 1676 */ 1677 static int 1678 ffs_uninit(vfsp) 1679 struct vfsconf *vfsp; 1680 { 1681 int ret; 1682 1683 ret = ufs_uninit(vfsp); 1684 softdep_uninitialize(); 1685 return (ret); 1686 } 1687 1688 /* 1689 * Write a superblock and associated information back to disk. 1690 */ 1691 int 1692 ffs_sbupdate(mp, waitfor, suspended) 1693 struct ufsmount *mp; 1694 int waitfor; 1695 int suspended; 1696 { 1697 struct fs *fs = mp->um_fs; 1698 struct buf *sbbp; 1699 struct buf *bp; 1700 int blks; 1701 void *space; 1702 int i, size, error, allerror = 0; 1703 1704 if (fs->fs_ronly == 1 && 1705 (mp->um_mountp->mnt_flag & (MNT_RDONLY | MNT_UPDATE)) != 1706 (MNT_RDONLY | MNT_UPDATE)) 1707 panic("ffs_sbupdate: write read-only filesystem"); 1708 /* 1709 * We use the superblock's buf to serialize calls to ffs_sbupdate(). 1710 */ 1711 sbbp = getblk(mp->um_devvp, btodb(fs->fs_sblockloc), (int)fs->fs_sbsize, 1712 0, 0, 0); 1713 /* 1714 * First write back the summary information. 1715 */ 1716 blks = howmany(fs->fs_cssize, fs->fs_fsize); 1717 space = fs->fs_csp; 1718 for (i = 0; i < blks; i += fs->fs_frag) { 1719 size = fs->fs_bsize; 1720 if (i + fs->fs_frag > blks) 1721 size = (blks - i) * fs->fs_fsize; 1722 bp = getblk(mp->um_devvp, fsbtodb(fs, fs->fs_csaddr + i), 1723 size, 0, 0, 0); 1724 bcopy(space, bp->b_data, (u_int)size); 1725 space = (char *)space + size; 1726 if (suspended) 1727 bp->b_flags |= B_VALIDSUSPWRT; 1728 if (waitfor != MNT_WAIT) 1729 bawrite(bp); 1730 else if ((error = bwrite(bp)) != 0) 1731 allerror = error; 1732 } 1733 /* 1734 * Now write back the superblock itself. If any errors occurred 1735 * up to this point, then fail so that the superblock avoids 1736 * being written out as clean. 1737 */ 1738 if (allerror) { 1739 brelse(sbbp); 1740 return (allerror); 1741 } 1742 bp = sbbp; 1743 if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_sblockloc != SBLOCK_UFS1 && 1744 (fs->fs_flags & FS_FLAGS_UPDATED) == 0) { 1745 printf("%s: correcting fs_sblockloc from %jd to %d\n", 1746 fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS1); 1747 fs->fs_sblockloc = SBLOCK_UFS1; 1748 } 1749 if (fs->fs_magic == FS_UFS2_MAGIC && fs->fs_sblockloc != SBLOCK_UFS2 && 1750 (fs->fs_flags & FS_FLAGS_UPDATED) == 0) { 1751 printf("%s: correcting fs_sblockloc from %jd to %d\n", 1752 fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS2); 1753 fs->fs_sblockloc = SBLOCK_UFS2; 1754 } 1755 fs->fs_fmod = 0; 1756 fs->fs_time = time_second; 1757 if (fs->fs_flags & FS_DOSOFTDEP) 1758 softdep_setup_sbupdate(mp, (struct fs *)bp->b_data, bp); 1759 bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize); 1760 ffs_oldfscompat_write((struct fs *)bp->b_data, mp); 1761 if (suspended) 1762 bp->b_flags |= B_VALIDSUSPWRT; 1763 if (waitfor != MNT_WAIT) 1764 bawrite(bp); 1765 else if ((error = bwrite(bp)) != 0) 1766 allerror = error; 1767 return (allerror); 1768 } 1769 1770 static int 1771 ffs_extattrctl(struct mount *mp, int cmd, struct vnode *filename_vp, 1772 int attrnamespace, const char *attrname) 1773 { 1774 1775 #ifdef UFS_EXTATTR 1776 return (ufs_extattrctl(mp, cmd, filename_vp, attrnamespace, 1777 attrname)); 1778 #else 1779 return (vfs_stdextattrctl(mp, cmd, filename_vp, attrnamespace, 1780 attrname)); 1781 #endif 1782 } 1783 1784 static void 1785 ffs_ifree(struct ufsmount *ump, struct inode *ip) 1786 { 1787 1788 if (ump->um_fstype == UFS1 && ip->i_din1 != NULL) 1789 uma_zfree(uma_ufs1, ip->i_din1); 1790 else if (ip->i_din2 != NULL) 1791 uma_zfree(uma_ufs2, ip->i_din2); 1792 uma_zfree(uma_inode, ip); 1793 } 1794 1795 static int dobkgrdwrite = 1; 1796 SYSCTL_INT(_debug, OID_AUTO, dobkgrdwrite, CTLFLAG_RW, &dobkgrdwrite, 0, 1797 "Do background writes (honoring the BV_BKGRDWRITE flag)?"); 1798 1799 /* 1800 * Complete a background write started from bwrite. 1801 */ 1802 static void 1803 ffs_backgroundwritedone(struct buf *bp) 1804 { 1805 struct bufobj *bufobj; 1806 struct buf *origbp; 1807 1808 /* 1809 * Find the original buffer that we are writing. 1810 */ 1811 bufobj = bp->b_bufobj; 1812 BO_LOCK(bufobj); 1813 if ((origbp = gbincore(bp->b_bufobj, bp->b_lblkno)) == NULL) 1814 panic("backgroundwritedone: lost buffer"); 1815 /* Grab an extra reference to be dropped by the bufdone() below. */ 1816 bufobj_wrefl(bufobj); 1817 BO_UNLOCK(bufobj); 1818 /* 1819 * Process dependencies then return any unfinished ones. 1820 */ 1821 if (!LIST_EMPTY(&bp->b_dep)) 1822 buf_complete(bp); 1823 #ifdef SOFTUPDATES 1824 if (!LIST_EMPTY(&bp->b_dep)) 1825 softdep_move_dependencies(bp, origbp); 1826 #endif 1827 /* 1828 * This buffer is marked B_NOCACHE so when it is released 1829 * by biodone it will be tossed. 1830 */ 1831 bp->b_flags |= B_NOCACHE; 1832 bp->b_flags &= ~B_CACHE; 1833 bufdone(bp); 1834 BO_LOCK(bufobj); 1835 /* 1836 * Clear the BV_BKGRDINPROG flag in the original buffer 1837 * and awaken it if it is waiting for the write to complete. 1838 * If BV_BKGRDINPROG is not set in the original buffer it must 1839 * have been released and re-instantiated - which is not legal. 1840 */ 1841 KASSERT((origbp->b_vflags & BV_BKGRDINPROG), 1842 ("backgroundwritedone: lost buffer2")); 1843 origbp->b_vflags &= ~BV_BKGRDINPROG; 1844 if (origbp->b_vflags & BV_BKGRDWAIT) { 1845 origbp->b_vflags &= ~BV_BKGRDWAIT; 1846 wakeup(&origbp->b_xflags); 1847 } 1848 BO_UNLOCK(bufobj); 1849 } 1850 1851 1852 /* 1853 * Write, release buffer on completion. (Done by iodone 1854 * if async). Do not bother writing anything if the buffer 1855 * is invalid. 1856 * 1857 * Note that we set B_CACHE here, indicating that buffer is 1858 * fully valid and thus cacheable. This is true even of NFS 1859 * now so we set it generally. This could be set either here 1860 * or in biodone() since the I/O is synchronous. We put it 1861 * here. 1862 */ 1863 static int 1864 ffs_bufwrite(struct buf *bp) 1865 { 1866 int oldflags, s; 1867 struct buf *newbp; 1868 1869 CTR3(KTR_BUF, "bufwrite(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags); 1870 if (bp->b_flags & B_INVAL) { 1871 brelse(bp); 1872 return (0); 1873 } 1874 1875 oldflags = bp->b_flags; 1876 1877 if (!BUF_ISLOCKED(bp)) 1878 panic("bufwrite: buffer is not busy???"); 1879 s = splbio(); 1880 /* 1881 * If a background write is already in progress, delay 1882 * writing this block if it is asynchronous. Otherwise 1883 * wait for the background write to complete. 1884 */ 1885 BO_LOCK(bp->b_bufobj); 1886 if (bp->b_vflags & BV_BKGRDINPROG) { 1887 if (bp->b_flags & B_ASYNC) { 1888 BO_UNLOCK(bp->b_bufobj); 1889 splx(s); 1890 bdwrite(bp); 1891 return (0); 1892 } 1893 bp->b_vflags |= BV_BKGRDWAIT; 1894 msleep(&bp->b_xflags, BO_MTX(bp->b_bufobj), PRIBIO, "bwrbg", 0); 1895 if (bp->b_vflags & BV_BKGRDINPROG) 1896 panic("bufwrite: still writing"); 1897 } 1898 BO_UNLOCK(bp->b_bufobj); 1899 1900 /* 1901 * If this buffer is marked for background writing and we 1902 * do not have to wait for it, make a copy and write the 1903 * copy so as to leave this buffer ready for further use. 1904 * 1905 * This optimization eats a lot of memory. If we have a page 1906 * or buffer shortfall we can't do it. 1907 */ 1908 if (dobkgrdwrite && (bp->b_xflags & BX_BKGRDWRITE) && 1909 (bp->b_flags & B_ASYNC) && 1910 !vm_page_count_severe() && 1911 !buf_dirty_count_severe()) { 1912 KASSERT(bp->b_iodone == NULL, 1913 ("bufwrite: needs chained iodone (%p)", bp->b_iodone)); 1914 1915 /* get a new block */ 1916 newbp = geteblk(bp->b_bufsize, GB_NOWAIT_BD); 1917 if (newbp == NULL) 1918 goto normal_write; 1919 1920 /* 1921 * set it to be identical to the old block. We have to 1922 * set b_lblkno and BKGRDMARKER before calling bgetvp() 1923 * to avoid confusing the splay tree and gbincore(). 1924 */ 1925 memcpy(newbp->b_data, bp->b_data, bp->b_bufsize); 1926 newbp->b_lblkno = bp->b_lblkno; 1927 newbp->b_xflags |= BX_BKGRDMARKER; 1928 BO_LOCK(bp->b_bufobj); 1929 bp->b_vflags |= BV_BKGRDINPROG; 1930 bgetvp(bp->b_vp, newbp); 1931 BO_UNLOCK(bp->b_bufobj); 1932 newbp->b_bufobj = &bp->b_vp->v_bufobj; 1933 newbp->b_blkno = bp->b_blkno; 1934 newbp->b_offset = bp->b_offset; 1935 newbp->b_iodone = ffs_backgroundwritedone; 1936 newbp->b_flags |= B_ASYNC; 1937 newbp->b_flags &= ~B_INVAL; 1938 1939 #ifdef SOFTUPDATES 1940 /* 1941 * Move over the dependencies. If there are rollbacks, 1942 * leave the parent buffer dirtied as it will need to 1943 * be written again. 1944 */ 1945 if (LIST_EMPTY(&bp->b_dep) || 1946 softdep_move_dependencies(bp, newbp) == 0) 1947 bundirty(bp); 1948 #else 1949 bundirty(bp); 1950 #endif 1951 1952 /* 1953 * Initiate write on the copy, release the original to 1954 * the B_LOCKED queue so that it cannot go away until 1955 * the background write completes. If not locked it could go 1956 * away and then be reconstituted while it was being written. 1957 * If the reconstituted buffer were written, we could end up 1958 * with two background copies being written at the same time. 1959 */ 1960 bqrelse(bp); 1961 bp = newbp; 1962 } else 1963 /* Mark the buffer clean */ 1964 bundirty(bp); 1965 1966 1967 /* Let the normal bufwrite do the rest for us */ 1968 normal_write: 1969 return (bufwrite(bp)); 1970 } 1971 1972 1973 static void 1974 ffs_geom_strategy(struct bufobj *bo, struct buf *bp) 1975 { 1976 struct vnode *vp; 1977 int error; 1978 struct buf *tbp; 1979 int nocopy; 1980 1981 vp = bo->__bo_vnode; 1982 if (bp->b_iocmd == BIO_WRITE) { 1983 if ((bp->b_flags & B_VALIDSUSPWRT) == 0 && 1984 bp->b_vp != NULL && bp->b_vp->v_mount != NULL && 1985 (bp->b_vp->v_mount->mnt_kern_flag & MNTK_SUSPENDED) != 0) 1986 panic("ffs_geom_strategy: bad I/O"); 1987 nocopy = bp->b_flags & B_NOCOPY; 1988 bp->b_flags &= ~(B_VALIDSUSPWRT | B_NOCOPY); 1989 if ((vp->v_vflag & VV_COPYONWRITE) && nocopy == 0 && 1990 vp->v_rdev->si_snapdata != NULL) { 1991 if ((bp->b_flags & B_CLUSTER) != 0) { 1992 runningbufwakeup(bp); 1993 TAILQ_FOREACH(tbp, &bp->b_cluster.cluster_head, 1994 b_cluster.cluster_entry) { 1995 error = ffs_copyonwrite(vp, tbp); 1996 if (error != 0 && 1997 error != EOPNOTSUPP) { 1998 bp->b_error = error; 1999 bp->b_ioflags |= BIO_ERROR; 2000 bufdone(bp); 2001 return; 2002 } 2003 } 2004 bp->b_runningbufspace = bp->b_bufsize; 2005 atomic_add_long(&runningbufspace, 2006 bp->b_runningbufspace); 2007 } else { 2008 error = ffs_copyonwrite(vp, bp); 2009 if (error != 0 && error != EOPNOTSUPP) { 2010 bp->b_error = error; 2011 bp->b_ioflags |= BIO_ERROR; 2012 bufdone(bp); 2013 return; 2014 } 2015 } 2016 } 2017 #ifdef SOFTUPDATES 2018 if ((bp->b_flags & B_CLUSTER) != 0) { 2019 TAILQ_FOREACH(tbp, &bp->b_cluster.cluster_head, 2020 b_cluster.cluster_entry) { 2021 if (!LIST_EMPTY(&tbp->b_dep)) 2022 buf_start(tbp); 2023 } 2024 } else { 2025 if (!LIST_EMPTY(&bp->b_dep)) 2026 buf_start(bp); 2027 } 2028 2029 #endif 2030 } 2031 g_vfs_strategy(bo, bp); 2032 } 2033 2034 #ifdef DDB 2035 2036 static void 2037 db_print_ffs(struct ufsmount *ump) 2038 { 2039 db_printf("mp %p %s devvp %p fs %p su_wl %d su_deps %d su_req %d\n", 2040 ump->um_mountp, ump->um_mountp->mnt_stat.f_mntonname, 2041 ump->um_devvp, ump->um_fs, ump->softdep_on_worklist, 2042 ump->softdep_deps, ump->softdep_req); 2043 } 2044 2045 DB_SHOW_COMMAND(ffs, db_show_ffs) 2046 { 2047 struct mount *mp; 2048 struct ufsmount *ump; 2049 2050 if (have_addr) { 2051 ump = VFSTOUFS((struct mount *)addr); 2052 db_print_ffs(ump); 2053 return; 2054 } 2055 2056 TAILQ_FOREACH(mp, &mountlist, mnt_list) { 2057 if (!strcmp(mp->mnt_stat.f_fstypename, ufs_vfsconf.vfc_name)) 2058 db_print_ffs(VFSTOUFS(mp)); 2059 } 2060 } 2061 2062 #endif /* DDB */ 2063