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