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