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