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