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