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