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