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