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