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