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