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