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