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 vput(vp); 744 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp); 745 return (error); 746 } 747 if ((error = ffs_load_inode(bp, ip, fs, ip->i_number)) != 0) { 748 brelse(bp); 749 vput(vp); 750 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp); 751 return (error); 752 } 753 ip->i_effnlink = ip->i_nlink; 754 brelse(bp); 755 vput(vp); 756 } 757 return (0); 758 } 759 760 /* 761 * Common code for mount and mountroot 762 */ 763 static int 764 ffs_mountfs(devvp, mp, td) 765 struct vnode *devvp; 766 struct mount *mp; 767 struct thread *td; 768 { 769 struct ufsmount *ump; 770 struct fs *fs; 771 struct cdev *dev; 772 int error, i, len, ronly; 773 struct ucred *cred; 774 struct g_consumer *cp; 775 struct mount *nmp; 776 int candelete; 777 778 fs = NULL; 779 ump = NULL; 780 cred = td ? td->td_ucred : NOCRED; 781 ronly = (mp->mnt_flag & MNT_RDONLY) != 0; 782 783 KASSERT(devvp->v_type == VCHR, ("reclaimed devvp")); 784 dev = devvp->v_rdev; 785 if (atomic_cmpset_acq_ptr((uintptr_t *)&dev->si_mountpt, 0, 786 (uintptr_t)mp) == 0) { 787 VOP_UNLOCK(devvp, 0); 788 return (EBUSY); 789 } 790 g_topology_lock(); 791 error = g_vfs_open(devvp, &cp, "ffs", ronly ? 0 : 1); 792 g_topology_unlock(); 793 if (error != 0) { 794 atomic_store_rel_ptr((uintptr_t *)&dev->si_mountpt, 0); 795 VOP_UNLOCK(devvp, 0); 796 return (error); 797 } 798 dev_ref(dev); 799 devvp->v_bufobj.bo_ops = &ffs_ops; 800 VOP_UNLOCK(devvp, 0); 801 if (dev->si_iosize_max != 0) 802 mp->mnt_iosize_max = dev->si_iosize_max; 803 if (mp->mnt_iosize_max > MAXPHYS) 804 mp->mnt_iosize_max = MAXPHYS; 805 if ((SBLOCKSIZE % cp->provider->sectorsize) != 0) { 806 error = EINVAL; 807 vfs_mount_error(mp, 808 "Invalid sectorsize %d for superblock size %d", 809 cp->provider->sectorsize, SBLOCKSIZE); 810 goto out; 811 } 812 /* fetch the superblock and summary information */ 813 if ((error = ffs_sbget(devvp, &fs, -1, M_UFSMNT, ffs_use_bread)) != 0) 814 goto out; 815 fs->fs_fmod = 0; 816 /* none of these types of check-hashes are maintained by this kernel */ 817 fs->fs_metackhash &= ~(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 ump->um_trimhash = hashinit(MAXTRIMIO, M_TRIM, 982 &ump->um_trimlisthashsize); 983 } 984 } 985 986 ump->um_mountp = mp; 987 ump->um_dev = dev; 988 ump->um_devvp = devvp; 989 ump->um_nindir = fs->fs_nindir; 990 ump->um_bptrtodb = fs->fs_fsbtodb; 991 ump->um_seqinc = fs->fs_frag; 992 for (i = 0; i < MAXQUOTAS; i++) 993 ump->um_quotas[i] = NULLVP; 994 #ifdef UFS_EXTATTR 995 ufs_extattr_uepm_init(&ump->um_extattr); 996 #endif 997 /* 998 * Set FS local "last mounted on" information (NULL pad) 999 */ 1000 bzero(fs->fs_fsmnt, MAXMNTLEN); 1001 strlcpy(fs->fs_fsmnt, mp->mnt_stat.f_mntonname, MAXMNTLEN); 1002 mp->mnt_stat.f_iosize = fs->fs_bsize; 1003 1004 if (mp->mnt_flag & MNT_ROOTFS) { 1005 /* 1006 * Root mount; update timestamp in mount structure. 1007 * this will be used by the common root mount code 1008 * to update the system clock. 1009 */ 1010 mp->mnt_time = fs->fs_time; 1011 } 1012 1013 if (ronly == 0) { 1014 fs->fs_mtime = time_second; 1015 if ((fs->fs_flags & FS_DOSOFTDEP) && 1016 (error = softdep_mount(devvp, mp, fs, cred)) != 0) { 1017 ffs_flushfiles(mp, FORCECLOSE, td); 1018 goto out; 1019 } 1020 if (fs->fs_snapinum[0] != 0) 1021 ffs_snapshot_mount(mp); 1022 fs->fs_fmod = 1; 1023 fs->fs_clean = 0; 1024 (void) ffs_sbupdate(ump, MNT_WAIT, 0); 1025 } 1026 /* 1027 * Initialize filesystem state information in mount struct. 1028 */ 1029 MNT_ILOCK(mp); 1030 mp->mnt_kern_flag |= MNTK_LOOKUP_SHARED | MNTK_EXTENDED_SHARED | 1031 MNTK_NO_IOPF | MNTK_UNMAPPED_BUFS | MNTK_USES_BCACHE; 1032 MNT_IUNLOCK(mp); 1033 #ifdef UFS_EXTATTR 1034 #ifdef UFS_EXTATTR_AUTOSTART 1035 /* 1036 * 1037 * Auto-starting does the following: 1038 * - check for /.attribute in the fs, and extattr_start if so 1039 * - for each file in .attribute, enable that file with 1040 * an attribute of the same name. 1041 * Not clear how to report errors -- probably eat them. 1042 * This would all happen while the filesystem was busy/not 1043 * available, so would effectively be "atomic". 1044 */ 1045 (void) ufs_extattr_autostart(mp, td); 1046 #endif /* !UFS_EXTATTR_AUTOSTART */ 1047 #endif /* !UFS_EXTATTR */ 1048 return (0); 1049 out: 1050 if (fs != NULL) { 1051 free(fs->fs_csp, M_UFSMNT); 1052 free(fs, M_UFSMNT); 1053 } 1054 if (cp != NULL) { 1055 g_topology_lock(); 1056 g_vfs_close(cp); 1057 g_topology_unlock(); 1058 } 1059 if (ump) { 1060 mtx_destroy(UFS_MTX(ump)); 1061 if (mp->mnt_gjprovider != NULL) { 1062 free(mp->mnt_gjprovider, M_UFSMNT); 1063 mp->mnt_gjprovider = NULL; 1064 } 1065 free(ump, M_UFSMNT); 1066 mp->mnt_data = NULL; 1067 } 1068 atomic_store_rel_ptr((uintptr_t *)&dev->si_mountpt, 0); 1069 dev_rel(dev); 1070 return (error); 1071 } 1072 1073 /* 1074 * A read function for use by filesystem-layer routines. 1075 */ 1076 static int 1077 ffs_use_bread(void *devfd, off_t loc, void **bufp, int size) 1078 { 1079 struct buf *bp; 1080 int error; 1081 1082 KASSERT(*bufp == NULL, ("ffs_use_bread: non-NULL *bufp %p\n", *bufp)); 1083 *bufp = malloc(size, M_UFSMNT, M_WAITOK); 1084 if ((error = bread((struct vnode *)devfd, btodb(loc), size, NOCRED, 1085 &bp)) != 0) 1086 return (error); 1087 bcopy(bp->b_data, *bufp, size); 1088 bp->b_flags |= B_INVAL | B_NOCACHE; 1089 brelse(bp); 1090 return (0); 1091 } 1092 1093 #include <sys/sysctl.h> 1094 static int bigcgs = 0; 1095 SYSCTL_INT(_debug, OID_AUTO, bigcgs, CTLFLAG_RW, &bigcgs, 0, ""); 1096 1097 /* 1098 * Sanity checks for loading old filesystem superblocks. 1099 * See ffs_oldfscompat_write below for unwound actions. 1100 * 1101 * XXX - Parts get retired eventually. 1102 * Unfortunately new bits get added. 1103 */ 1104 static void 1105 ffs_oldfscompat_read(fs, ump, sblockloc) 1106 struct fs *fs; 1107 struct ufsmount *ump; 1108 ufs2_daddr_t sblockloc; 1109 { 1110 off_t maxfilesize; 1111 1112 /* 1113 * If not yet done, update fs_flags location and value of fs_sblockloc. 1114 */ 1115 if ((fs->fs_old_flags & FS_FLAGS_UPDATED) == 0) { 1116 fs->fs_flags = fs->fs_old_flags; 1117 fs->fs_old_flags |= FS_FLAGS_UPDATED; 1118 fs->fs_sblockloc = sblockloc; 1119 } 1120 /* 1121 * If not yet done, update UFS1 superblock with new wider fields. 1122 */ 1123 if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_maxbsize != fs->fs_bsize) { 1124 fs->fs_maxbsize = fs->fs_bsize; 1125 fs->fs_time = fs->fs_old_time; 1126 fs->fs_size = fs->fs_old_size; 1127 fs->fs_dsize = fs->fs_old_dsize; 1128 fs->fs_csaddr = fs->fs_old_csaddr; 1129 fs->fs_cstotal.cs_ndir = fs->fs_old_cstotal.cs_ndir; 1130 fs->fs_cstotal.cs_nbfree = fs->fs_old_cstotal.cs_nbfree; 1131 fs->fs_cstotal.cs_nifree = fs->fs_old_cstotal.cs_nifree; 1132 fs->fs_cstotal.cs_nffree = fs->fs_old_cstotal.cs_nffree; 1133 } 1134 if (fs->fs_magic == FS_UFS1_MAGIC && 1135 fs->fs_old_inodefmt < FS_44INODEFMT) { 1136 fs->fs_maxfilesize = ((uint64_t)1 << 31) - 1; 1137 fs->fs_qbmask = ~fs->fs_bmask; 1138 fs->fs_qfmask = ~fs->fs_fmask; 1139 } 1140 if (fs->fs_magic == FS_UFS1_MAGIC) { 1141 ump->um_savedmaxfilesize = fs->fs_maxfilesize; 1142 maxfilesize = (uint64_t)0x80000000 * fs->fs_bsize - 1; 1143 if (fs->fs_maxfilesize > maxfilesize) 1144 fs->fs_maxfilesize = maxfilesize; 1145 } 1146 /* Compatibility for old filesystems */ 1147 if (fs->fs_avgfilesize <= 0) 1148 fs->fs_avgfilesize = AVFILESIZ; 1149 if (fs->fs_avgfpdir <= 0) 1150 fs->fs_avgfpdir = AFPDIR; 1151 if (bigcgs) { 1152 fs->fs_save_cgsize = fs->fs_cgsize; 1153 fs->fs_cgsize = fs->fs_bsize; 1154 } 1155 } 1156 1157 /* 1158 * Unwinding superblock updates for old filesystems. 1159 * See ffs_oldfscompat_read above for details. 1160 * 1161 * XXX - Parts get retired eventually. 1162 * Unfortunately new bits get added. 1163 */ 1164 void 1165 ffs_oldfscompat_write(fs, ump) 1166 struct fs *fs; 1167 struct ufsmount *ump; 1168 { 1169 1170 /* 1171 * Copy back UFS2 updated fields that UFS1 inspects. 1172 */ 1173 if (fs->fs_magic == FS_UFS1_MAGIC) { 1174 fs->fs_old_time = fs->fs_time; 1175 fs->fs_old_cstotal.cs_ndir = fs->fs_cstotal.cs_ndir; 1176 fs->fs_old_cstotal.cs_nbfree = fs->fs_cstotal.cs_nbfree; 1177 fs->fs_old_cstotal.cs_nifree = fs->fs_cstotal.cs_nifree; 1178 fs->fs_old_cstotal.cs_nffree = fs->fs_cstotal.cs_nffree; 1179 fs->fs_maxfilesize = ump->um_savedmaxfilesize; 1180 } 1181 if (bigcgs) { 1182 fs->fs_cgsize = fs->fs_save_cgsize; 1183 fs->fs_save_cgsize = 0; 1184 } 1185 } 1186 1187 /* 1188 * unmount system call 1189 */ 1190 static int 1191 ffs_unmount(mp, mntflags) 1192 struct mount *mp; 1193 int mntflags; 1194 { 1195 struct thread *td; 1196 struct ufsmount *ump = VFSTOUFS(mp); 1197 struct fs *fs; 1198 int error, flags, susp; 1199 #ifdef UFS_EXTATTR 1200 int e_restart; 1201 #endif 1202 1203 flags = 0; 1204 td = curthread; 1205 fs = ump->um_fs; 1206 susp = 0; 1207 if (mntflags & MNT_FORCE) { 1208 flags |= FORCECLOSE; 1209 susp = fs->fs_ronly == 0; 1210 } 1211 #ifdef UFS_EXTATTR 1212 if ((error = ufs_extattr_stop(mp, td))) { 1213 if (error != EOPNOTSUPP) 1214 printf("WARNING: unmount %s: ufs_extattr_stop " 1215 "returned errno %d\n", mp->mnt_stat.f_mntonname, 1216 error); 1217 e_restart = 0; 1218 } else { 1219 ufs_extattr_uepm_destroy(&ump->um_extattr); 1220 e_restart = 1; 1221 } 1222 #endif 1223 if (susp) { 1224 error = vfs_write_suspend_umnt(mp); 1225 if (error != 0) 1226 goto fail1; 1227 } 1228 if (MOUNTEDSOFTDEP(mp)) 1229 error = softdep_flushfiles(mp, flags, td); 1230 else 1231 error = ffs_flushfiles(mp, flags, td); 1232 if (error != 0 && error != ENXIO) 1233 goto fail; 1234 1235 UFS_LOCK(ump); 1236 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) { 1237 printf("WARNING: unmount %s: pending error: blocks %jd " 1238 "files %d\n", fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks, 1239 fs->fs_pendinginodes); 1240 fs->fs_pendingblocks = 0; 1241 fs->fs_pendinginodes = 0; 1242 } 1243 UFS_UNLOCK(ump); 1244 if (MOUNTEDSOFTDEP(mp)) 1245 softdep_unmount(mp); 1246 if (fs->fs_ronly == 0 || ump->um_fsckpid > 0) { 1247 fs->fs_clean = fs->fs_flags & (FS_UNCLEAN|FS_NEEDSFSCK) ? 0 : 1; 1248 error = ffs_sbupdate(ump, MNT_WAIT, 0); 1249 if (error && error != ENXIO) { 1250 fs->fs_clean = 0; 1251 goto fail; 1252 } 1253 } 1254 if (susp) 1255 vfs_write_resume(mp, VR_START_WRITE); 1256 if (ump->um_trim_tq != NULL) { 1257 while (ump->um_trim_inflight != 0) 1258 pause("ufsutr", hz); 1259 taskqueue_drain_all(ump->um_trim_tq); 1260 taskqueue_free(ump->um_trim_tq); 1261 free (ump->um_trimhash, M_TRIM); 1262 } 1263 g_topology_lock(); 1264 if (ump->um_fsckpid > 0) { 1265 /* 1266 * Return to normal read-only mode. 1267 */ 1268 error = g_access(ump->um_cp, 0, -1, 0); 1269 ump->um_fsckpid = 0; 1270 } 1271 g_vfs_close(ump->um_cp); 1272 g_topology_unlock(); 1273 atomic_store_rel_ptr((uintptr_t *)&ump->um_dev->si_mountpt, 0); 1274 vrele(ump->um_devvp); 1275 dev_rel(ump->um_dev); 1276 mtx_destroy(UFS_MTX(ump)); 1277 if (mp->mnt_gjprovider != NULL) { 1278 free(mp->mnt_gjprovider, M_UFSMNT); 1279 mp->mnt_gjprovider = NULL; 1280 } 1281 free(fs->fs_csp, M_UFSMNT); 1282 free(fs, M_UFSMNT); 1283 free(ump, M_UFSMNT); 1284 mp->mnt_data = NULL; 1285 MNT_ILOCK(mp); 1286 mp->mnt_flag &= ~MNT_LOCAL; 1287 MNT_IUNLOCK(mp); 1288 if (td->td_su == mp) { 1289 td->td_su = NULL; 1290 vfs_rel(mp); 1291 } 1292 return (error); 1293 1294 fail: 1295 if (susp) 1296 vfs_write_resume(mp, VR_START_WRITE); 1297 fail1: 1298 #ifdef UFS_EXTATTR 1299 if (e_restart) { 1300 ufs_extattr_uepm_init(&ump->um_extattr); 1301 #ifdef UFS_EXTATTR_AUTOSTART 1302 (void) ufs_extattr_autostart(mp, td); 1303 #endif 1304 } 1305 #endif 1306 1307 return (error); 1308 } 1309 1310 /* 1311 * Flush out all the files in a filesystem. 1312 */ 1313 int 1314 ffs_flushfiles(mp, flags, td) 1315 struct mount *mp; 1316 int flags; 1317 struct thread *td; 1318 { 1319 struct ufsmount *ump; 1320 int qerror, error; 1321 1322 ump = VFSTOUFS(mp); 1323 qerror = 0; 1324 #ifdef QUOTA 1325 if (mp->mnt_flag & MNT_QUOTA) { 1326 int i; 1327 error = vflush(mp, 0, SKIPSYSTEM|flags, td); 1328 if (error) 1329 return (error); 1330 for (i = 0; i < MAXQUOTAS; i++) { 1331 error = quotaoff(td, mp, i); 1332 if (error != 0) { 1333 if ((flags & EARLYFLUSH) == 0) 1334 return (error); 1335 else 1336 qerror = error; 1337 } 1338 } 1339 1340 /* 1341 * Here we fall through to vflush again to ensure that 1342 * we have gotten rid of all the system vnodes, unless 1343 * quotas must not be closed. 1344 */ 1345 } 1346 #endif 1347 ASSERT_VOP_LOCKED(ump->um_devvp, "ffs_flushfiles"); 1348 if (ump->um_devvp->v_vflag & VV_COPYONWRITE) { 1349 if ((error = vflush(mp, 0, SKIPSYSTEM | flags, td)) != 0) 1350 return (error); 1351 ffs_snapshot_unmount(mp); 1352 flags |= FORCECLOSE; 1353 /* 1354 * Here we fall through to vflush again to ensure 1355 * that we have gotten rid of all the system vnodes. 1356 */ 1357 } 1358 1359 /* 1360 * Do not close system files if quotas were not closed, to be 1361 * able to sync the remaining dquots. The freeblks softupdate 1362 * workitems might hold a reference on a dquot, preventing 1363 * quotaoff() from completing. Next round of 1364 * softdep_flushworklist() iteration should process the 1365 * blockers, allowing the next run of quotaoff() to finally 1366 * flush held dquots. 1367 * 1368 * Otherwise, flush all the files. 1369 */ 1370 if (qerror == 0 && (error = vflush(mp, 0, flags, td)) != 0) 1371 return (error); 1372 1373 /* 1374 * Flush filesystem metadata. 1375 */ 1376 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY); 1377 error = VOP_FSYNC(ump->um_devvp, MNT_WAIT, td); 1378 VOP_UNLOCK(ump->um_devvp, 0); 1379 return (error); 1380 } 1381 1382 /* 1383 * Get filesystem statistics. 1384 */ 1385 static int 1386 ffs_statfs(mp, sbp) 1387 struct mount *mp; 1388 struct statfs *sbp; 1389 { 1390 struct ufsmount *ump; 1391 struct fs *fs; 1392 1393 ump = VFSTOUFS(mp); 1394 fs = ump->um_fs; 1395 if (fs->fs_magic != FS_UFS1_MAGIC && fs->fs_magic != FS_UFS2_MAGIC) 1396 panic("ffs_statfs"); 1397 sbp->f_version = STATFS_VERSION; 1398 sbp->f_bsize = fs->fs_fsize; 1399 sbp->f_iosize = fs->fs_bsize; 1400 sbp->f_blocks = fs->fs_dsize; 1401 UFS_LOCK(ump); 1402 sbp->f_bfree = fs->fs_cstotal.cs_nbfree * fs->fs_frag + 1403 fs->fs_cstotal.cs_nffree + dbtofsb(fs, fs->fs_pendingblocks); 1404 sbp->f_bavail = freespace(fs, fs->fs_minfree) + 1405 dbtofsb(fs, fs->fs_pendingblocks); 1406 sbp->f_files = fs->fs_ncg * fs->fs_ipg - UFS_ROOTINO; 1407 sbp->f_ffree = fs->fs_cstotal.cs_nifree + fs->fs_pendinginodes; 1408 UFS_UNLOCK(ump); 1409 sbp->f_namemax = UFS_MAXNAMLEN; 1410 return (0); 1411 } 1412 1413 static bool 1414 sync_doupdate(struct inode *ip) 1415 { 1416 1417 return ((ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_MODIFIED | 1418 IN_UPDATE)) != 0); 1419 } 1420 1421 /* 1422 * For a lazy sync, we only care about access times, quotas and the 1423 * superblock. Other filesystem changes are already converted to 1424 * cylinder group blocks or inode blocks updates and are written to 1425 * disk by syncer. 1426 */ 1427 static int 1428 ffs_sync_lazy(mp) 1429 struct mount *mp; 1430 { 1431 struct vnode *mvp, *vp; 1432 struct inode *ip; 1433 struct thread *td; 1434 int allerror, error; 1435 1436 allerror = 0; 1437 td = curthread; 1438 if ((mp->mnt_flag & MNT_NOATIME) != 0) 1439 goto qupdate; 1440 MNT_VNODE_FOREACH_ACTIVE(vp, mp, mvp) { 1441 if (vp->v_type == VNON) { 1442 VI_UNLOCK(vp); 1443 continue; 1444 } 1445 ip = VTOI(vp); 1446 1447 /* 1448 * The IN_ACCESS flag is converted to IN_MODIFIED by 1449 * ufs_close() and ufs_getattr() by the calls to 1450 * ufs_itimes_locked(), without subsequent UFS_UPDATE(). 1451 * Test also all the other timestamp flags too, to pick up 1452 * any other cases that could be missed. 1453 */ 1454 if (!sync_doupdate(ip) && (vp->v_iflag & VI_OWEINACT) == 0) { 1455 VI_UNLOCK(vp); 1456 continue; 1457 } 1458 if ((error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, 1459 td)) != 0) 1460 continue; 1461 if (sync_doupdate(ip)) 1462 error = ffs_update(vp, 0); 1463 if (error != 0) 1464 allerror = error; 1465 vput(vp); 1466 } 1467 1468 qupdate: 1469 #ifdef QUOTA 1470 qsync(mp); 1471 #endif 1472 1473 if (VFSTOUFS(mp)->um_fs->fs_fmod != 0 && 1474 (error = ffs_sbupdate(VFSTOUFS(mp), MNT_LAZY, 0)) != 0) 1475 allerror = error; 1476 return (allerror); 1477 } 1478 1479 /* 1480 * Go through the disk queues to initiate sandbagged IO; 1481 * go through the inodes to write those that have been modified; 1482 * initiate the writing of the super block if it has been modified. 1483 * 1484 * Note: we are always called with the filesystem marked busy using 1485 * vfs_busy(). 1486 */ 1487 static int 1488 ffs_sync(mp, waitfor) 1489 struct mount *mp; 1490 int waitfor; 1491 { 1492 struct vnode *mvp, *vp, *devvp; 1493 struct thread *td; 1494 struct inode *ip; 1495 struct ufsmount *ump = VFSTOUFS(mp); 1496 struct fs *fs; 1497 int error, count, lockreq, allerror = 0; 1498 int suspend; 1499 int suspended; 1500 int secondary_writes; 1501 int secondary_accwrites; 1502 int softdep_deps; 1503 int softdep_accdeps; 1504 struct bufobj *bo; 1505 1506 suspend = 0; 1507 suspended = 0; 1508 td = curthread; 1509 fs = ump->um_fs; 1510 if (fs->fs_fmod != 0 && fs->fs_ronly != 0 && ump->um_fsckpid == 0) 1511 panic("%s: ffs_sync: modification on read-only filesystem", 1512 fs->fs_fsmnt); 1513 if (waitfor == MNT_LAZY) { 1514 if (!rebooting) 1515 return (ffs_sync_lazy(mp)); 1516 waitfor = MNT_NOWAIT; 1517 } 1518 1519 /* 1520 * Write back each (modified) inode. 1521 */ 1522 lockreq = LK_EXCLUSIVE | LK_NOWAIT; 1523 if (waitfor == MNT_SUSPEND) { 1524 suspend = 1; 1525 waitfor = MNT_WAIT; 1526 } 1527 if (waitfor == MNT_WAIT) 1528 lockreq = LK_EXCLUSIVE; 1529 lockreq |= LK_INTERLOCK | LK_SLEEPFAIL; 1530 loop: 1531 /* Grab snapshot of secondary write counts */ 1532 MNT_ILOCK(mp); 1533 secondary_writes = mp->mnt_secondary_writes; 1534 secondary_accwrites = mp->mnt_secondary_accwrites; 1535 MNT_IUNLOCK(mp); 1536 1537 /* Grab snapshot of softdep dependency counts */ 1538 softdep_get_depcounts(mp, &softdep_deps, &softdep_accdeps); 1539 1540 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) { 1541 /* 1542 * Depend on the vnode interlock to keep things stable enough 1543 * for a quick test. Since there might be hundreds of 1544 * thousands of vnodes, we cannot afford even a subroutine 1545 * call unless there's a good chance that we have work to do. 1546 */ 1547 if (vp->v_type == VNON) { 1548 VI_UNLOCK(vp); 1549 continue; 1550 } 1551 ip = VTOI(vp); 1552 if ((ip->i_flag & 1553 (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 && 1554 vp->v_bufobj.bo_dirty.bv_cnt == 0) { 1555 VI_UNLOCK(vp); 1556 continue; 1557 } 1558 if ((error = vget(vp, lockreq, td)) != 0) { 1559 if (error == ENOENT || error == ENOLCK) { 1560 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp); 1561 goto loop; 1562 } 1563 continue; 1564 } 1565 if ((error = ffs_syncvnode(vp, waitfor, 0)) != 0) 1566 allerror = error; 1567 vput(vp); 1568 } 1569 /* 1570 * Force stale filesystem control information to be flushed. 1571 */ 1572 if (waitfor == MNT_WAIT || rebooting) { 1573 if ((error = softdep_flushworklist(ump->um_mountp, &count, td))) 1574 allerror = error; 1575 /* Flushed work items may create new vnodes to clean */ 1576 if (allerror == 0 && count) 1577 goto loop; 1578 } 1579 #ifdef QUOTA 1580 qsync(mp); 1581 #endif 1582 1583 devvp = ump->um_devvp; 1584 bo = &devvp->v_bufobj; 1585 BO_LOCK(bo); 1586 if (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0) { 1587 BO_UNLOCK(bo); 1588 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); 1589 error = VOP_FSYNC(devvp, waitfor, td); 1590 VOP_UNLOCK(devvp, 0); 1591 if (MOUNTEDSOFTDEP(mp) && (error == 0 || error == EAGAIN)) 1592 error = ffs_sbupdate(ump, waitfor, 0); 1593 if (error != 0) 1594 allerror = error; 1595 if (allerror == 0 && waitfor == MNT_WAIT) 1596 goto loop; 1597 } else if (suspend != 0) { 1598 if (softdep_check_suspend(mp, 1599 devvp, 1600 softdep_deps, 1601 softdep_accdeps, 1602 secondary_writes, 1603 secondary_accwrites) != 0) { 1604 MNT_IUNLOCK(mp); 1605 goto loop; /* More work needed */ 1606 } 1607 mtx_assert(MNT_MTX(mp), MA_OWNED); 1608 mp->mnt_kern_flag |= MNTK_SUSPEND2 | MNTK_SUSPENDED; 1609 MNT_IUNLOCK(mp); 1610 suspended = 1; 1611 } else 1612 BO_UNLOCK(bo); 1613 /* 1614 * Write back modified superblock. 1615 */ 1616 if (fs->fs_fmod != 0 && 1617 (error = ffs_sbupdate(ump, waitfor, suspended)) != 0) 1618 allerror = error; 1619 return (allerror); 1620 } 1621 1622 int 1623 ffs_vget(mp, ino, flags, vpp) 1624 struct mount *mp; 1625 ino_t ino; 1626 int flags; 1627 struct vnode **vpp; 1628 { 1629 return (ffs_vgetf(mp, ino, flags, vpp, 0)); 1630 } 1631 1632 int 1633 ffs_vgetf(mp, ino, flags, vpp, ffs_flags) 1634 struct mount *mp; 1635 ino_t ino; 1636 int flags; 1637 struct vnode **vpp; 1638 int ffs_flags; 1639 { 1640 struct fs *fs; 1641 struct inode *ip; 1642 struct ufsmount *ump; 1643 struct buf *bp; 1644 struct vnode *vp; 1645 int error; 1646 1647 error = vfs_hash_get(mp, ino, flags, curthread, vpp, NULL, NULL); 1648 if (error || *vpp != NULL) 1649 return (error); 1650 1651 /* 1652 * We must promote to an exclusive lock for vnode creation. This 1653 * can happen if lookup is passed LOCKSHARED. 1654 */ 1655 if ((flags & LK_TYPE_MASK) == LK_SHARED) { 1656 flags &= ~LK_TYPE_MASK; 1657 flags |= LK_EXCLUSIVE; 1658 } 1659 1660 /* 1661 * We do not lock vnode creation as it is believed to be too 1662 * expensive for such rare case as simultaneous creation of vnode 1663 * for same ino by different processes. We just allow them to race 1664 * and check later to decide who wins. Let the race begin! 1665 */ 1666 1667 ump = VFSTOUFS(mp); 1668 fs = ump->um_fs; 1669 ip = uma_zalloc(uma_inode, M_WAITOK | M_ZERO); 1670 1671 /* Allocate a new vnode/inode. */ 1672 error = getnewvnode("ufs", mp, fs->fs_magic == FS_UFS1_MAGIC ? 1673 &ffs_vnodeops1 : &ffs_vnodeops2, &vp); 1674 if (error) { 1675 *vpp = NULL; 1676 uma_zfree(uma_inode, ip); 1677 return (error); 1678 } 1679 /* 1680 * FFS supports recursive locking. 1681 */ 1682 lockmgr(vp->v_vnlock, LK_EXCLUSIVE, NULL); 1683 VN_LOCK_AREC(vp); 1684 vp->v_data = ip; 1685 vp->v_bufobj.bo_bsize = fs->fs_bsize; 1686 ip->i_vnode = vp; 1687 ip->i_ump = ump; 1688 ip->i_number = ino; 1689 ip->i_ea_refs = 0; 1690 ip->i_nextclustercg = -1; 1691 ip->i_flag = fs->fs_magic == FS_UFS1_MAGIC ? 0 : IN_UFS2; 1692 #ifdef QUOTA 1693 { 1694 int i; 1695 for (i = 0; i < MAXQUOTAS; i++) 1696 ip->i_dquot[i] = NODQUOT; 1697 } 1698 #endif 1699 1700 if (ffs_flags & FFSV_FORCEINSMQ) 1701 vp->v_vflag |= VV_FORCEINSMQ; 1702 error = insmntque(vp, mp); 1703 if (error != 0) { 1704 uma_zfree(uma_inode, ip); 1705 *vpp = NULL; 1706 return (error); 1707 } 1708 vp->v_vflag &= ~VV_FORCEINSMQ; 1709 error = vfs_hash_insert(vp, ino, flags, curthread, vpp, NULL, NULL); 1710 if (error || *vpp != NULL) 1711 return (error); 1712 1713 /* Read in the disk contents for the inode, copy into the inode. */ 1714 error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)), 1715 (int)fs->fs_bsize, NOCRED, &bp); 1716 if (error) { 1717 /* 1718 * The inode does not contain anything useful, so it would 1719 * be misleading to leave it on its hash chain. With mode 1720 * still zero, it will be unlinked and returned to the free 1721 * list by vput(). 1722 */ 1723 brelse(bp); 1724 vput(vp); 1725 *vpp = NULL; 1726 return (error); 1727 } 1728 if (I_IS_UFS1(ip)) 1729 ip->i_din1 = uma_zalloc(uma_ufs1, M_WAITOK); 1730 else 1731 ip->i_din2 = uma_zalloc(uma_ufs2, M_WAITOK); 1732 if ((error = ffs_load_inode(bp, ip, fs, ino)) != 0) { 1733 bqrelse(bp); 1734 vput(vp); 1735 *vpp = NULL; 1736 return (error); 1737 } 1738 if (DOINGSOFTDEP(vp)) 1739 softdep_load_inodeblock(ip); 1740 else 1741 ip->i_effnlink = ip->i_nlink; 1742 bqrelse(bp); 1743 1744 /* 1745 * Initialize the vnode from the inode, check for aliases. 1746 * Note that the underlying vnode may have changed. 1747 */ 1748 error = ufs_vinit(mp, I_IS_UFS1(ip) ? &ffs_fifoops1 : &ffs_fifoops2, 1749 &vp); 1750 if (error) { 1751 vput(vp); 1752 *vpp = NULL; 1753 return (error); 1754 } 1755 1756 /* 1757 * Finish inode initialization. 1758 */ 1759 if (vp->v_type != VFIFO) { 1760 /* FFS supports shared locking for all files except fifos. */ 1761 VN_LOCK_ASHARE(vp); 1762 } 1763 1764 /* 1765 * Set up a generation number for this inode if it does not 1766 * already have one. This should only happen on old filesystems. 1767 */ 1768 if (ip->i_gen == 0) { 1769 while (ip->i_gen == 0) 1770 ip->i_gen = arc4random(); 1771 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) { 1772 ip->i_flag |= IN_MODIFIED; 1773 DIP_SET(ip, i_gen, ip->i_gen); 1774 } 1775 } 1776 #ifdef MAC 1777 if ((mp->mnt_flag & MNT_MULTILABEL) && ip->i_mode) { 1778 /* 1779 * If this vnode is already allocated, and we're running 1780 * multi-label, attempt to perform a label association 1781 * from the extended attributes on the inode. 1782 */ 1783 error = mac_vnode_associate_extattr(mp, vp); 1784 if (error) { 1785 /* ufs_inactive will release ip->i_devvp ref. */ 1786 vput(vp); 1787 *vpp = NULL; 1788 return (error); 1789 } 1790 } 1791 #endif 1792 1793 *vpp = vp; 1794 return (0); 1795 } 1796 1797 /* 1798 * File handle to vnode 1799 * 1800 * Have to be really careful about stale file handles: 1801 * - check that the inode number is valid 1802 * - for UFS2 check that the inode number is initialized 1803 * - call ffs_vget() to get the locked inode 1804 * - check for an unallocated inode (i_mode == 0) 1805 * - check that the given client host has export rights and return 1806 * those rights via. exflagsp and credanonp 1807 */ 1808 static int 1809 ffs_fhtovp(mp, fhp, flags, vpp) 1810 struct mount *mp; 1811 struct fid *fhp; 1812 int flags; 1813 struct vnode **vpp; 1814 { 1815 struct ufid *ufhp; 1816 struct ufsmount *ump; 1817 struct fs *fs; 1818 struct cg *cgp; 1819 struct buf *bp; 1820 ino_t ino; 1821 u_int cg; 1822 int error; 1823 1824 ufhp = (struct ufid *)fhp; 1825 ino = ufhp->ufid_ino; 1826 ump = VFSTOUFS(mp); 1827 fs = ump->um_fs; 1828 if (ino < UFS_ROOTINO || ino >= fs->fs_ncg * fs->fs_ipg) 1829 return (ESTALE); 1830 /* 1831 * Need to check if inode is initialized because UFS2 does lazy 1832 * initialization and nfs_fhtovp can offer arbitrary inode numbers. 1833 */ 1834 if (fs->fs_magic != FS_UFS2_MAGIC) 1835 return (ufs_fhtovp(mp, ufhp, flags, vpp)); 1836 cg = ino_to_cg(fs, ino); 1837 if ((error = ffs_getcg(fs, ump->um_devvp, cg, &bp, &cgp)) != 0) 1838 return (error); 1839 if (ino >= cg * fs->fs_ipg + cgp->cg_initediblk) { 1840 brelse(bp); 1841 return (ESTALE); 1842 } 1843 brelse(bp); 1844 return (ufs_fhtovp(mp, ufhp, flags, vpp)); 1845 } 1846 1847 /* 1848 * Initialize the filesystem. 1849 */ 1850 static int 1851 ffs_init(vfsp) 1852 struct vfsconf *vfsp; 1853 { 1854 1855 ffs_susp_initialize(); 1856 softdep_initialize(); 1857 return (ufs_init(vfsp)); 1858 } 1859 1860 /* 1861 * Undo the work of ffs_init(). 1862 */ 1863 static int 1864 ffs_uninit(vfsp) 1865 struct vfsconf *vfsp; 1866 { 1867 int ret; 1868 1869 ret = ufs_uninit(vfsp); 1870 softdep_uninitialize(); 1871 ffs_susp_uninitialize(); 1872 return (ret); 1873 } 1874 1875 /* 1876 * Structure used to pass information from ffs_sbupdate to its 1877 * helper routine ffs_use_bwrite. 1878 */ 1879 struct devfd { 1880 struct ufsmount *ump; 1881 struct buf *sbbp; 1882 int waitfor; 1883 int suspended; 1884 int error; 1885 }; 1886 1887 /* 1888 * Write a superblock and associated information back to disk. 1889 */ 1890 int 1891 ffs_sbupdate(ump, waitfor, suspended) 1892 struct ufsmount *ump; 1893 int waitfor; 1894 int suspended; 1895 { 1896 struct fs *fs; 1897 struct buf *sbbp; 1898 struct devfd devfd; 1899 1900 fs = ump->um_fs; 1901 if (fs->fs_ronly == 1 && 1902 (ump->um_mountp->mnt_flag & (MNT_RDONLY | MNT_UPDATE)) != 1903 (MNT_RDONLY | MNT_UPDATE) && ump->um_fsckpid == 0) 1904 panic("ffs_sbupdate: write read-only filesystem"); 1905 /* 1906 * We use the superblock's buf to serialize calls to ffs_sbupdate(). 1907 */ 1908 sbbp = getblk(ump->um_devvp, btodb(fs->fs_sblockloc), 1909 (int)fs->fs_sbsize, 0, 0, 0); 1910 /* 1911 * Initialize info needed for write function. 1912 */ 1913 devfd.ump = ump; 1914 devfd.sbbp = sbbp; 1915 devfd.waitfor = waitfor; 1916 devfd.suspended = suspended; 1917 devfd.error = 0; 1918 return (ffs_sbput(&devfd, fs, fs->fs_sblockloc, ffs_use_bwrite)); 1919 } 1920 1921 /* 1922 * Write function for use by filesystem-layer routines. 1923 */ 1924 static int 1925 ffs_use_bwrite(void *devfd, off_t loc, void *buf, int size) 1926 { 1927 struct devfd *devfdp; 1928 struct ufsmount *ump; 1929 struct buf *bp; 1930 struct fs *fs; 1931 int error; 1932 1933 devfdp = devfd; 1934 ump = devfdp->ump; 1935 fs = ump->um_fs; 1936 /* 1937 * Writing the superblock summary information. 1938 */ 1939 if (loc != fs->fs_sblockloc) { 1940 bp = getblk(ump->um_devvp, btodb(loc), size, 0, 0, 0); 1941 bcopy(buf, bp->b_data, (u_int)size); 1942 if (devfdp->suspended) 1943 bp->b_flags |= B_VALIDSUSPWRT; 1944 if (devfdp->waitfor != MNT_WAIT) 1945 bawrite(bp); 1946 else if ((error = bwrite(bp)) != 0) 1947 devfdp->error = error; 1948 return (0); 1949 } 1950 /* 1951 * Writing the superblock itself. We need to do special checks for it. 1952 */ 1953 bp = devfdp->sbbp; 1954 if (devfdp->error != 0) { 1955 brelse(bp); 1956 return (devfdp->error); 1957 } 1958 if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_sblockloc != SBLOCK_UFS1 && 1959 (fs->fs_old_flags & FS_FLAGS_UPDATED) == 0) { 1960 printf("WARNING: %s: correcting fs_sblockloc from %jd to %d\n", 1961 fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS1); 1962 fs->fs_sblockloc = SBLOCK_UFS1; 1963 } 1964 if (fs->fs_magic == FS_UFS2_MAGIC && fs->fs_sblockloc != SBLOCK_UFS2 && 1965 (fs->fs_old_flags & FS_FLAGS_UPDATED) == 0) { 1966 printf("WARNING: %s: correcting fs_sblockloc from %jd to %d\n", 1967 fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS2); 1968 fs->fs_sblockloc = SBLOCK_UFS2; 1969 } 1970 if (MOUNTEDSOFTDEP(ump->um_mountp)) 1971 softdep_setup_sbupdate(ump, (struct fs *)bp->b_data, bp); 1972 bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize); 1973 ffs_oldfscompat_write((struct fs *)bp->b_data, ump); 1974 if (devfdp->suspended) 1975 bp->b_flags |= B_VALIDSUSPWRT; 1976 if (devfdp->waitfor != MNT_WAIT) 1977 bawrite(bp); 1978 else if ((error = bwrite(bp)) != 0) 1979 devfdp->error = error; 1980 return (devfdp->error); 1981 } 1982 1983 static int 1984 ffs_extattrctl(struct mount *mp, int cmd, struct vnode *filename_vp, 1985 int attrnamespace, const char *attrname) 1986 { 1987 1988 #ifdef UFS_EXTATTR 1989 return (ufs_extattrctl(mp, cmd, filename_vp, attrnamespace, 1990 attrname)); 1991 #else 1992 return (vfs_stdextattrctl(mp, cmd, filename_vp, attrnamespace, 1993 attrname)); 1994 #endif 1995 } 1996 1997 static void 1998 ffs_ifree(struct ufsmount *ump, struct inode *ip) 1999 { 2000 2001 if (ump->um_fstype == UFS1 && ip->i_din1 != NULL) 2002 uma_zfree(uma_ufs1, ip->i_din1); 2003 else if (ip->i_din2 != NULL) 2004 uma_zfree(uma_ufs2, ip->i_din2); 2005 uma_zfree(uma_inode, ip); 2006 } 2007 2008 static int dobkgrdwrite = 1; 2009 SYSCTL_INT(_debug, OID_AUTO, dobkgrdwrite, CTLFLAG_RW, &dobkgrdwrite, 0, 2010 "Do background writes (honoring the BV_BKGRDWRITE flag)?"); 2011 2012 /* 2013 * Complete a background write started from bwrite. 2014 */ 2015 static void 2016 ffs_backgroundwritedone(struct buf *bp) 2017 { 2018 struct bufobj *bufobj; 2019 struct buf *origbp; 2020 2021 /* 2022 * Find the original buffer that we are writing. 2023 */ 2024 bufobj = bp->b_bufobj; 2025 BO_LOCK(bufobj); 2026 if ((origbp = gbincore(bp->b_bufobj, bp->b_lblkno)) == NULL) 2027 panic("backgroundwritedone: lost buffer"); 2028 2029 /* 2030 * We should mark the cylinder group buffer origbp as 2031 * dirty, to not loose the failed write. 2032 */ 2033 if ((bp->b_ioflags & BIO_ERROR) != 0) 2034 origbp->b_vflags |= BV_BKGRDERR; 2035 BO_UNLOCK(bufobj); 2036 /* 2037 * Process dependencies then return any unfinished ones. 2038 */ 2039 if (!LIST_EMPTY(&bp->b_dep) && (bp->b_ioflags & BIO_ERROR) == 0) 2040 buf_complete(bp); 2041 #ifdef SOFTUPDATES 2042 if (!LIST_EMPTY(&bp->b_dep)) 2043 softdep_move_dependencies(bp, origbp); 2044 #endif 2045 /* 2046 * This buffer is marked B_NOCACHE so when it is released 2047 * by biodone it will be tossed. 2048 */ 2049 bp->b_flags |= B_NOCACHE; 2050 bp->b_flags &= ~B_CACHE; 2051 pbrelvp(bp); 2052 2053 /* 2054 * Prevent brelse() from trying to keep and re-dirtying bp on 2055 * errors. It causes b_bufobj dereference in 2056 * bdirty()/reassignbuf(), and b_bufobj was cleared in 2057 * pbrelvp() above. 2058 */ 2059 if ((bp->b_ioflags & BIO_ERROR) != 0) 2060 bp->b_flags |= B_INVAL; 2061 bufdone(bp); 2062 BO_LOCK(bufobj); 2063 /* 2064 * Clear the BV_BKGRDINPROG flag in the original buffer 2065 * and awaken it if it is waiting for the write to complete. 2066 * If BV_BKGRDINPROG is not set in the original buffer it must 2067 * have been released and re-instantiated - which is not legal. 2068 */ 2069 KASSERT((origbp->b_vflags & BV_BKGRDINPROG), 2070 ("backgroundwritedone: lost buffer2")); 2071 origbp->b_vflags &= ~BV_BKGRDINPROG; 2072 if (origbp->b_vflags & BV_BKGRDWAIT) { 2073 origbp->b_vflags &= ~BV_BKGRDWAIT; 2074 wakeup(&origbp->b_xflags); 2075 } 2076 BO_UNLOCK(bufobj); 2077 } 2078 2079 2080 /* 2081 * Write, release buffer on completion. (Done by iodone 2082 * if async). Do not bother writing anything if the buffer 2083 * is invalid. 2084 * 2085 * Note that we set B_CACHE here, indicating that buffer is 2086 * fully valid and thus cacheable. This is true even of NFS 2087 * now so we set it generally. This could be set either here 2088 * or in biodone() since the I/O is synchronous. We put it 2089 * here. 2090 */ 2091 static int 2092 ffs_bufwrite(struct buf *bp) 2093 { 2094 struct buf *newbp; 2095 struct cg *cgp; 2096 2097 CTR3(KTR_BUF, "bufwrite(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags); 2098 if (bp->b_flags & B_INVAL) { 2099 brelse(bp); 2100 return (0); 2101 } 2102 2103 if (!BUF_ISLOCKED(bp)) 2104 panic("bufwrite: buffer is not busy???"); 2105 /* 2106 * If a background write is already in progress, delay 2107 * writing this block if it is asynchronous. Otherwise 2108 * wait for the background write to complete. 2109 */ 2110 BO_LOCK(bp->b_bufobj); 2111 if (bp->b_vflags & BV_BKGRDINPROG) { 2112 if (bp->b_flags & B_ASYNC) { 2113 BO_UNLOCK(bp->b_bufobj); 2114 bdwrite(bp); 2115 return (0); 2116 } 2117 bp->b_vflags |= BV_BKGRDWAIT; 2118 msleep(&bp->b_xflags, BO_LOCKPTR(bp->b_bufobj), PRIBIO, 2119 "bwrbg", 0); 2120 if (bp->b_vflags & BV_BKGRDINPROG) 2121 panic("bufwrite: still writing"); 2122 } 2123 bp->b_vflags &= ~BV_BKGRDERR; 2124 BO_UNLOCK(bp->b_bufobj); 2125 2126 /* 2127 * If this buffer is marked for background writing and we 2128 * do not have to wait for it, make a copy and write the 2129 * copy so as to leave this buffer ready for further use. 2130 * 2131 * This optimization eats a lot of memory. If we have a page 2132 * or buffer shortfall we can't do it. 2133 */ 2134 if (dobkgrdwrite && (bp->b_xflags & BX_BKGRDWRITE) && 2135 (bp->b_flags & B_ASYNC) && 2136 !vm_page_count_severe() && 2137 !buf_dirty_count_severe()) { 2138 KASSERT(bp->b_iodone == NULL, 2139 ("bufwrite: needs chained iodone (%p)", bp->b_iodone)); 2140 2141 /* get a new block */ 2142 newbp = geteblk(bp->b_bufsize, GB_NOWAIT_BD); 2143 if (newbp == NULL) 2144 goto normal_write; 2145 2146 KASSERT(buf_mapped(bp), ("Unmapped cg")); 2147 memcpy(newbp->b_data, bp->b_data, bp->b_bufsize); 2148 BO_LOCK(bp->b_bufobj); 2149 bp->b_vflags |= BV_BKGRDINPROG; 2150 BO_UNLOCK(bp->b_bufobj); 2151 newbp->b_xflags |= 2152 (bp->b_xflags & BX_FSPRIV) | BX_BKGRDMARKER; 2153 newbp->b_lblkno = bp->b_lblkno; 2154 newbp->b_blkno = bp->b_blkno; 2155 newbp->b_offset = bp->b_offset; 2156 newbp->b_iodone = ffs_backgroundwritedone; 2157 newbp->b_flags |= B_ASYNC; 2158 newbp->b_flags &= ~B_INVAL; 2159 pbgetvp(bp->b_vp, newbp); 2160 2161 #ifdef SOFTUPDATES 2162 /* 2163 * Move over the dependencies. If there are rollbacks, 2164 * leave the parent buffer dirtied as it will need to 2165 * be written again. 2166 */ 2167 if (LIST_EMPTY(&bp->b_dep) || 2168 softdep_move_dependencies(bp, newbp) == 0) 2169 bundirty(bp); 2170 #else 2171 bundirty(bp); 2172 #endif 2173 2174 /* 2175 * Initiate write on the copy, release the original. The 2176 * BKGRDINPROG flag prevents it from going away until 2177 * the background write completes. We have to recalculate 2178 * its check hash in case the buffer gets freed and then 2179 * reconstituted from the buffer cache during a later read. 2180 */ 2181 if ((bp->b_xflags & BX_CYLGRP) != 0) { 2182 cgp = (struct cg *)bp->b_data; 2183 cgp->cg_ckhash = 0; 2184 cgp->cg_ckhash = 2185 calculate_crc32c(~0L, bp->b_data, bp->b_bcount); 2186 } 2187 bqrelse(bp); 2188 bp = newbp; 2189 } else 2190 /* Mark the buffer clean */ 2191 bundirty(bp); 2192 2193 2194 /* Let the normal bufwrite do the rest for us */ 2195 normal_write: 2196 /* 2197 * If we are writing a cylinder group, update its time. 2198 */ 2199 if ((bp->b_xflags & BX_CYLGRP) != 0) { 2200 cgp = (struct cg *)bp->b_data; 2201 cgp->cg_old_time = cgp->cg_time = time_second; 2202 } 2203 return (bufwrite(bp)); 2204 } 2205 2206 2207 static void 2208 ffs_geom_strategy(struct bufobj *bo, struct buf *bp) 2209 { 2210 struct vnode *vp; 2211 struct buf *tbp; 2212 int error, nocopy; 2213 2214 vp = bo2vnode(bo); 2215 if (bp->b_iocmd == BIO_WRITE) { 2216 if ((bp->b_flags & B_VALIDSUSPWRT) == 0 && 2217 bp->b_vp != NULL && bp->b_vp->v_mount != NULL && 2218 (bp->b_vp->v_mount->mnt_kern_flag & MNTK_SUSPENDED) != 0) 2219 panic("ffs_geom_strategy: bad I/O"); 2220 nocopy = bp->b_flags & B_NOCOPY; 2221 bp->b_flags &= ~(B_VALIDSUSPWRT | B_NOCOPY); 2222 if ((vp->v_vflag & VV_COPYONWRITE) && nocopy == 0 && 2223 vp->v_rdev->si_snapdata != NULL) { 2224 if ((bp->b_flags & B_CLUSTER) != 0) { 2225 runningbufwakeup(bp); 2226 TAILQ_FOREACH(tbp, &bp->b_cluster.cluster_head, 2227 b_cluster.cluster_entry) { 2228 error = ffs_copyonwrite(vp, tbp); 2229 if (error != 0 && 2230 error != EOPNOTSUPP) { 2231 bp->b_error = error; 2232 bp->b_ioflags |= BIO_ERROR; 2233 bufdone(bp); 2234 return; 2235 } 2236 } 2237 bp->b_runningbufspace = bp->b_bufsize; 2238 atomic_add_long(&runningbufspace, 2239 bp->b_runningbufspace); 2240 } else { 2241 error = ffs_copyonwrite(vp, bp); 2242 if (error != 0 && error != EOPNOTSUPP) { 2243 bp->b_error = error; 2244 bp->b_ioflags |= BIO_ERROR; 2245 bufdone(bp); 2246 return; 2247 } 2248 } 2249 } 2250 #ifdef SOFTUPDATES 2251 if ((bp->b_flags & B_CLUSTER) != 0) { 2252 TAILQ_FOREACH(tbp, &bp->b_cluster.cluster_head, 2253 b_cluster.cluster_entry) { 2254 if (!LIST_EMPTY(&tbp->b_dep)) 2255 buf_start(tbp); 2256 } 2257 } else { 2258 if (!LIST_EMPTY(&bp->b_dep)) 2259 buf_start(bp); 2260 } 2261 2262 #endif 2263 /* 2264 * Check for metadata that needs check-hashes and update them. 2265 */ 2266 switch (bp->b_xflags & BX_FSPRIV) { 2267 case BX_CYLGRP: 2268 ((struct cg *)bp->b_data)->cg_ckhash = 0; 2269 ((struct cg *)bp->b_data)->cg_ckhash = 2270 calculate_crc32c(~0L, bp->b_data, bp->b_bcount); 2271 break; 2272 2273 case BX_SUPERBLOCK: 2274 case BX_INODE: 2275 case BX_INDIR: 2276 case BX_DIR: 2277 printf("Check-hash write is unimplemented!!!\n"); 2278 break; 2279 2280 case 0: 2281 break; 2282 2283 default: 2284 printf("multiple buffer types 0x%b\n", 2285 (u_int)(bp->b_xflags & BX_FSPRIV), 2286 PRINT_UFS_BUF_XFLAGS); 2287 break; 2288 } 2289 } 2290 g_vfs_strategy(bo, bp); 2291 } 2292 2293 int 2294 ffs_own_mount(const struct mount *mp) 2295 { 2296 2297 if (mp->mnt_op == &ufs_vfsops) 2298 return (1); 2299 return (0); 2300 } 2301 2302 #ifdef DDB 2303 #ifdef SOFTUPDATES 2304 2305 /* defined in ffs_softdep.c */ 2306 extern void db_print_ffs(struct ufsmount *ump); 2307 2308 DB_SHOW_COMMAND(ffs, db_show_ffs) 2309 { 2310 struct mount *mp; 2311 struct ufsmount *ump; 2312 2313 if (have_addr) { 2314 ump = VFSTOUFS((struct mount *)addr); 2315 db_print_ffs(ump); 2316 return; 2317 } 2318 2319 TAILQ_FOREACH(mp, &mountlist, mnt_list) { 2320 if (!strcmp(mp->mnt_stat.f_fstypename, ufs_vfsconf.vfc_name)) 2321 db_print_ffs(VFSTOUFS(mp)); 2322 } 2323 } 2324 2325 #endif /* SOFTUPDATES */ 2326 #endif /* DDB */ 2327