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