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