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