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