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