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); 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) { 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) { 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, *nvp, *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, nvp) { 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 goto loop; 551 } 552 if (vinvalbuf(vp, 0, td, 0, 0)) 553 panic("ffs_reload: dirty2"); 554 /* 555 * Step 5: re-read inode data for all active vnodes. 556 */ 557 ip = VTOI(vp); 558 error = 559 bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)), 560 (int)fs->fs_bsize, NOCRED, &bp); 561 if (error) { 562 VOP_UNLOCK(vp, 0, td); 563 vrele(vp); 564 return (error); 565 } 566 ffs_load_inode(bp, ip, fs, ip->i_number); 567 ip->i_effnlink = ip->i_nlink; 568 brelse(bp); 569 VOP_UNLOCK(vp, 0, td); 570 vrele(vp); 571 MNT_ILOCK(mp); 572 } 573 MNT_IUNLOCK(mp); 574 return (0); 575 } 576 577 /* 578 * Possible superblock locations ordered from most to least likely. 579 */ 580 static int sblock_try[] = SBLOCKSEARCH; 581 582 /* 583 * Common code for mount and mountroot 584 */ 585 static int 586 ffs_mountfs(devvp, mp, td) 587 struct vnode *devvp; 588 struct mount *mp; 589 struct thread *td; 590 { 591 struct ufsmount *ump; 592 struct buf *bp; 593 struct fs *fs; 594 struct cdev *dev; 595 void *space; 596 ufs2_daddr_t sblockloc; 597 int error, i, blks, size, ronly; 598 int32_t *lp; 599 struct ucred *cred; 600 struct g_consumer *cp; 601 602 dev = devvp->v_rdev; 603 cred = td ? td->td_ucred : NOCRED; 604 605 ronly = (mp->mnt_flag & MNT_RDONLY) != 0; 606 DROP_GIANT(); 607 g_topology_lock(); 608 error = g_vfs_open(devvp, &cp, "ffs", ronly ? 0 : 1); 609 610 /* 611 * If we are a root mount, drop the E flag so fsck can do its magic. 612 * We will pick it up again when we remount R/W. 613 */ 614 if (error == 0 && ronly && (mp->mnt_flag & MNT_ROOTFS)) 615 error = g_access(cp, 0, 0, -1); 616 g_topology_unlock(); 617 PICKUP_GIANT(); 618 VOP_UNLOCK(devvp, 0, td); 619 if (error) 620 return (error); 621 if (devvp->v_rdev->si_iosize_max != 0) 622 mp->mnt_iosize_max = devvp->v_rdev->si_iosize_max; 623 if (mp->mnt_iosize_max > MAXPHYS) 624 mp->mnt_iosize_max = MAXPHYS; 625 626 devvp->v_bufobj.bo_private = cp; 627 devvp->v_bufobj.bo_ops = &ffs_ops; 628 629 bp = NULL; 630 ump = NULL; 631 fs = NULL; 632 sblockloc = 0; 633 /* 634 * Try reading the superblock in each of its possible locations. 635 */ 636 for (i = 0; sblock_try[i] != -1; i++) { 637 if ((error = bread(devvp, sblock_try[i] / DEV_BSIZE, SBLOCKSIZE, 638 cred, &bp)) != 0) 639 goto out; 640 fs = (struct fs *)bp->b_data; 641 sblockloc = sblock_try[i]; 642 if ((fs->fs_magic == FS_UFS1_MAGIC || 643 (fs->fs_magic == FS_UFS2_MAGIC && 644 (fs->fs_sblockloc == sblockloc || 645 (fs->fs_old_flags & FS_FLAGS_UPDATED) == 0))) && 646 fs->fs_bsize <= MAXBSIZE && 647 fs->fs_bsize >= sizeof(struct fs)) 648 break; 649 brelse(bp); 650 bp = NULL; 651 } 652 if (sblock_try[i] == -1) { 653 error = EINVAL; /* XXX needs translation */ 654 goto out; 655 } 656 fs->fs_fmod = 0; 657 fs->fs_flags &= ~FS_INDEXDIRS; /* no support for directory indicies */ 658 fs->fs_flags &= ~FS_UNCLEAN; 659 if (fs->fs_clean == 0) { 660 fs->fs_flags |= FS_UNCLEAN; 661 if (ronly || (mp->mnt_flag & MNT_FORCE) || 662 ((fs->fs_flags & FS_NEEDSFSCK) == 0 && 663 (fs->fs_flags & FS_DOSOFTDEP))) { 664 printf( 665 "WARNING: %s was not properly dismounted\n", 666 fs->fs_fsmnt); 667 } else { 668 printf( 669 "WARNING: R/W mount of %s denied. Filesystem is not clean - run fsck\n", 670 fs->fs_fsmnt); 671 error = EPERM; 672 goto out; 673 } 674 if ((fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) && 675 (mp->mnt_flag & MNT_FORCE)) { 676 printf("%s: lost blocks %jd files %d\n", fs->fs_fsmnt, 677 (intmax_t)fs->fs_pendingblocks, 678 fs->fs_pendinginodes); 679 fs->fs_pendingblocks = 0; 680 fs->fs_pendinginodes = 0; 681 } 682 } 683 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) { 684 printf("%s: mount pending error: blocks %jd files %d\n", 685 fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks, 686 fs->fs_pendinginodes); 687 fs->fs_pendingblocks = 0; 688 fs->fs_pendinginodes = 0; 689 } 690 ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK | M_ZERO); 691 ump->um_cp = cp; 692 ump->um_bo = &devvp->v_bufobj; 693 ump->um_fs = malloc((u_long)fs->fs_sbsize, M_UFSMNT, M_WAITOK); 694 if (fs->fs_magic == FS_UFS1_MAGIC) { 695 ump->um_fstype = UFS1; 696 ump->um_balloc = ffs_balloc_ufs1; 697 } else { 698 ump->um_fstype = UFS2; 699 ump->um_balloc = ffs_balloc_ufs2; 700 } 701 ump->um_blkatoff = ffs_blkatoff; 702 ump->um_truncate = ffs_truncate; 703 ump->um_update = ffs_update; 704 ump->um_valloc = ffs_valloc; 705 ump->um_vfree = ffs_vfree; 706 ump->um_ifree = ffs_ifree; 707 mtx_init(UFS_MTX(ump), "FFS", "FFS Lock", MTX_DEF); 708 bcopy(bp->b_data, ump->um_fs, (u_int)fs->fs_sbsize); 709 if (fs->fs_sbsize < SBLOCKSIZE) 710 bp->b_flags |= B_INVAL | B_NOCACHE; 711 brelse(bp); 712 bp = NULL; 713 fs = ump->um_fs; 714 ffs_oldfscompat_read(fs, ump, sblockloc); 715 fs->fs_ronly = ronly; 716 size = fs->fs_cssize; 717 blks = howmany(size, fs->fs_fsize); 718 if (fs->fs_contigsumsize > 0) 719 size += fs->fs_ncg * sizeof(int32_t); 720 size += fs->fs_ncg * sizeof(u_int8_t); 721 space = malloc((u_long)size, M_UFSMNT, M_WAITOK); 722 fs->fs_csp = space; 723 for (i = 0; i < blks; i += fs->fs_frag) { 724 size = fs->fs_bsize; 725 if (i + fs->fs_frag > blks) 726 size = (blks - i) * fs->fs_fsize; 727 if ((error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size, 728 cred, &bp)) != 0) { 729 free(fs->fs_csp, M_UFSMNT); 730 goto out; 731 } 732 bcopy(bp->b_data, space, (u_int)size); 733 space = (char *)space + size; 734 brelse(bp); 735 bp = NULL; 736 } 737 if (fs->fs_contigsumsize > 0) { 738 fs->fs_maxcluster = lp = space; 739 for (i = 0; i < fs->fs_ncg; i++) 740 *lp++ = fs->fs_contigsumsize; 741 space = lp; 742 } 743 size = fs->fs_ncg * sizeof(u_int8_t); 744 fs->fs_contigdirs = (u_int8_t *)space; 745 bzero(fs->fs_contigdirs, size); 746 fs->fs_active = NULL; 747 mp->mnt_data = (qaddr_t)ump; 748 mp->mnt_stat.f_fsid.val[0] = fs->fs_id[0]; 749 mp->mnt_stat.f_fsid.val[1] = fs->fs_id[1]; 750 if (fs->fs_id[0] == 0 || fs->fs_id[1] == 0 || 751 vfs_getvfs(&mp->mnt_stat.f_fsid)) 752 vfs_getnewfsid(mp); 753 mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen; 754 mp->mnt_flag |= MNT_LOCAL; 755 if ((fs->fs_flags & FS_MULTILABEL) != 0) 756 #ifdef MAC 757 mp->mnt_flag |= MNT_MULTILABEL; 758 #else 759 printf( 760 "WARNING: %s: multilabel flag on fs but no MAC support\n", 761 fs->fs_fsmnt); 762 #endif 763 if ((fs->fs_flags & FS_ACLS) != 0) 764 #ifdef UFS_ACL 765 mp->mnt_flag |= MNT_ACLS; 766 #else 767 printf( 768 "WARNING: %s: ACLs flag on fs but no ACLs support\n", 769 fs->fs_fsmnt); 770 #endif 771 ump->um_mountp = mp; 772 ump->um_dev = dev; 773 ump->um_devvp = devvp; 774 ump->um_nindir = fs->fs_nindir; 775 ump->um_bptrtodb = fs->fs_fsbtodb; 776 ump->um_seqinc = fs->fs_frag; 777 for (i = 0; i < MAXQUOTAS; i++) 778 ump->um_quotas[i] = NULLVP; 779 #ifdef UFS_EXTATTR 780 ufs_extattr_uepm_init(&ump->um_extattr); 781 #endif 782 /* 783 * Set FS local "last mounted on" information (NULL pad) 784 */ 785 bzero(fs->fs_fsmnt, MAXMNTLEN); 786 strlcpy(fs->fs_fsmnt, mp->mnt_stat.f_mntonname, MAXMNTLEN); 787 788 if( mp->mnt_flag & MNT_ROOTFS) { 789 /* 790 * Root mount; update timestamp in mount structure. 791 * this will be used by the common root mount code 792 * to update the system clock. 793 */ 794 mp->mnt_time = fs->fs_time; 795 } 796 797 if (ronly == 0) { 798 if ((fs->fs_flags & FS_DOSOFTDEP) && 799 (error = softdep_mount(devvp, mp, fs, cred)) != 0) { 800 free(fs->fs_csp, M_UFSMNT); 801 goto out; 802 } 803 if (fs->fs_snapinum[0] != 0) 804 ffs_snapshot_mount(mp); 805 fs->fs_fmod = 1; 806 fs->fs_clean = 0; 807 (void) ffs_sbupdate(ump, MNT_WAIT); 808 } 809 /* 810 * Initialize filesystem stat information in mount struct. 811 */ 812 #ifdef UFS_EXTATTR 813 #ifdef UFS_EXTATTR_AUTOSTART 814 /* 815 * 816 * Auto-starting does the following: 817 * - check for /.attribute in the fs, and extattr_start if so 818 * - for each file in .attribute, enable that file with 819 * an attribute of the same name. 820 * Not clear how to report errors -- probably eat them. 821 * This would all happen while the filesystem was busy/not 822 * available, so would effectively be "atomic". 823 */ 824 (void) ufs_extattr_autostart(mp, td); 825 #endif /* !UFS_EXTATTR_AUTOSTART */ 826 #endif /* !UFS_EXTATTR */ 827 #ifndef QUOTA 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); 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 /* 1046 * Here we fall through to vflush again to ensure 1047 * that we have gotten rid of all the system vnodes. 1048 */ 1049 } 1050 /* 1051 * Flush all the files. 1052 */ 1053 if ((error = vflush(mp, 0, flags, td)) != 0) 1054 return (error); 1055 /* 1056 * Flush filesystem metadata. 1057 */ 1058 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY, td); 1059 error = VOP_FSYNC(ump->um_devvp, MNT_WAIT, td); 1060 VOP_UNLOCK(ump->um_devvp, 0, td); 1061 return (error); 1062 } 1063 1064 /* 1065 * Get filesystem statistics. 1066 */ 1067 static int 1068 ffs_statfs(mp, sbp, td) 1069 struct mount *mp; 1070 struct statfs *sbp; 1071 struct thread *td; 1072 { 1073 struct ufsmount *ump; 1074 struct fs *fs; 1075 1076 ump = VFSTOUFS(mp); 1077 fs = ump->um_fs; 1078 if (fs->fs_magic != FS_UFS1_MAGIC && fs->fs_magic != FS_UFS2_MAGIC) 1079 panic("ffs_statfs"); 1080 sbp->f_version = STATFS_VERSION; 1081 sbp->f_bsize = fs->fs_fsize; 1082 sbp->f_iosize = fs->fs_bsize; 1083 sbp->f_blocks = fs->fs_dsize; 1084 UFS_LOCK(ump); 1085 sbp->f_bfree = fs->fs_cstotal.cs_nbfree * fs->fs_frag + 1086 fs->fs_cstotal.cs_nffree + dbtofsb(fs, fs->fs_pendingblocks); 1087 sbp->f_bavail = freespace(fs, fs->fs_minfree) + 1088 dbtofsb(fs, fs->fs_pendingblocks); 1089 sbp->f_files = fs->fs_ncg * fs->fs_ipg - ROOTINO; 1090 sbp->f_ffree = fs->fs_cstotal.cs_nifree + fs->fs_pendinginodes; 1091 UFS_UNLOCK(ump); 1092 sbp->f_namemax = NAME_MAX; 1093 return (0); 1094 } 1095 1096 /* 1097 * Go through the disk queues to initiate sandbagged IO; 1098 * go through the inodes to write those that have been modified; 1099 * initiate the writing of the super block if it has been modified. 1100 * 1101 * Note: we are always called with the filesystem marked `MPBUSY'. 1102 */ 1103 static int 1104 ffs_sync(mp, waitfor, td) 1105 struct mount *mp; 1106 int waitfor; 1107 struct thread *td; 1108 { 1109 struct vnode *nvp, *vp, *devvp; 1110 struct inode *ip; 1111 struct ufsmount *ump = VFSTOUFS(mp); 1112 struct fs *fs; 1113 int error, count, wait, lockreq, allerror = 0; 1114 struct bufobj *bo; 1115 1116 fs = ump->um_fs; 1117 if (fs->fs_fmod != 0 && fs->fs_ronly != 0) { /* XXX */ 1118 printf("fs = %s\n", fs->fs_fsmnt); 1119 panic("ffs_sync: rofs mod"); 1120 } 1121 /* 1122 * Write back each (modified) inode. 1123 */ 1124 wait = 0; 1125 lockreq = LK_EXCLUSIVE | LK_NOWAIT; 1126 if (waitfor == MNT_WAIT) { 1127 wait = 1; 1128 lockreq = LK_EXCLUSIVE; 1129 } 1130 lockreq |= LK_INTERLOCK | LK_SLEEPFAIL; 1131 MNT_ILOCK(mp); 1132 loop: 1133 MNT_VNODE_FOREACH(vp, mp, nvp) { 1134 /* 1135 * Depend on the mntvnode_slock to keep things stable enough 1136 * for a quick test. Since there might be hundreds of 1137 * thousands of vnodes, we cannot afford even a subroutine 1138 * call unless there's a good chance that we have work to do. 1139 */ 1140 VI_LOCK(vp); 1141 if (vp->v_iflag & VI_DOOMED) { 1142 VI_UNLOCK(vp); 1143 continue; 1144 } 1145 ip = VTOI(vp); 1146 if (vp->v_type == VNON || ((ip->i_flag & 1147 (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 && 1148 vp->v_bufobj.bo_dirty.bv_cnt == 0)) { 1149 VI_UNLOCK(vp); 1150 continue; 1151 } 1152 MNT_IUNLOCK(mp); 1153 if ((error = vget(vp, lockreq, td)) != 0) { 1154 MNT_ILOCK(mp); 1155 if (error == ENOENT || error == ENOLCK) 1156 goto loop; 1157 continue; 1158 } 1159 if ((error = ffs_syncvnode(vp, waitfor)) != 0) 1160 allerror = error; 1161 vput(vp); 1162 MNT_ILOCK(mp); 1163 } 1164 MNT_IUNLOCK(mp); 1165 /* 1166 * Force stale filesystem control information to be flushed. 1167 */ 1168 if (waitfor == MNT_WAIT) { 1169 if ((error = softdep_flushworklist(ump->um_mountp, &count, td))) 1170 allerror = error; 1171 /* Flushed work items may create new vnodes to clean */ 1172 if (allerror == 0 && count) { 1173 MNT_ILOCK(mp); 1174 goto loop; 1175 } 1176 } 1177 #ifdef QUOTA 1178 qsync(mp); 1179 #endif 1180 devvp = ump->um_devvp; 1181 VI_LOCK(devvp); 1182 bo = &devvp->v_bufobj; 1183 if (waitfor != MNT_LAZY && 1184 (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0)) { 1185 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY | LK_INTERLOCK, td); 1186 if ((error = VOP_FSYNC(devvp, waitfor, td)) != 0) 1187 allerror = error; 1188 VOP_UNLOCK(devvp, 0, td); 1189 if (allerror == 0 && waitfor == MNT_WAIT) { 1190 MNT_ILOCK(mp); 1191 goto loop; 1192 } 1193 } else 1194 VI_UNLOCK(devvp); 1195 /* 1196 * Write back modified superblock. 1197 */ 1198 if (fs->fs_fmod != 0 && (error = ffs_sbupdate(ump, waitfor)) != 0) 1199 allerror = error; 1200 return (allerror); 1201 } 1202 1203 int 1204 ffs_vget(mp, ino, flags, vpp) 1205 struct mount *mp; 1206 ino_t ino; 1207 int flags; 1208 struct vnode **vpp; 1209 { 1210 struct fs *fs; 1211 struct inode *ip; 1212 struct ufsmount *ump; 1213 struct buf *bp; 1214 struct vnode *vp; 1215 struct cdev *dev; 1216 int error; 1217 1218 error = vfs_hash_get(mp, ino, flags, curthread, vpp, NULL, NULL); 1219 if (error || *vpp != NULL) 1220 return (error); 1221 1222 /* 1223 * We must promote to an exclusive lock for vnode creation. This 1224 * can happen if lookup is passed LOCKSHARED. 1225 */ 1226 if ((flags & LK_TYPE_MASK) == LK_SHARED) { 1227 flags &= ~LK_TYPE_MASK; 1228 flags |= LK_EXCLUSIVE; 1229 } 1230 1231 /* 1232 * We do not lock vnode creation as it is believed to be too 1233 * expensive for such rare case as simultaneous creation of vnode 1234 * for same ino by different processes. We just allow them to race 1235 * and check later to decide who wins. Let the race begin! 1236 */ 1237 1238 ump = VFSTOUFS(mp); 1239 dev = ump->um_dev; 1240 fs = ump->um_fs; 1241 1242 /* 1243 * If this MALLOC() is performed after the getnewvnode() 1244 * it might block, leaving a vnode with a NULL v_data to be 1245 * found by ffs_sync() if a sync happens to fire right then, 1246 * which will cause a panic because ffs_sync() blindly 1247 * dereferences vp->v_data (as well it should). 1248 */ 1249 ip = uma_zalloc(uma_inode, M_WAITOK | M_ZERO); 1250 1251 /* Allocate a new vnode/inode. */ 1252 if (fs->fs_magic == FS_UFS1_MAGIC) 1253 error = getnewvnode("ufs", mp, &ffs_vnodeops1, &vp); 1254 else 1255 error = getnewvnode("ufs", mp, &ffs_vnodeops2, &vp); 1256 if (error) { 1257 *vpp = NULL; 1258 uma_zfree(uma_inode, ip); 1259 return (error); 1260 } 1261 /* 1262 * FFS supports recursive and shared locking. 1263 */ 1264 vp->v_vnlock->lk_flags |= LK_CANRECURSE; 1265 vp->v_vnlock->lk_flags &= ~LK_NOSHARE; 1266 vp->v_data = ip; 1267 vp->v_bufobj.bo_bsize = fs->fs_bsize; 1268 ip->i_vnode = vp; 1269 ip->i_ump = ump; 1270 ip->i_fs = fs; 1271 ip->i_dev = dev; 1272 ip->i_number = ino; 1273 #ifdef QUOTA 1274 { 1275 int i; 1276 for (i = 0; i < MAXQUOTAS; i++) 1277 ip->i_dquot[i] = NODQUOT; 1278 } 1279 #endif 1280 1281 error = vfs_hash_insert(vp, ino, flags, curthread, vpp, NULL, NULL); 1282 if (error || *vpp != NULL) 1283 return (error); 1284 1285 /* Read in the disk contents for the inode, copy into the inode. */ 1286 error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)), 1287 (int)fs->fs_bsize, NOCRED, &bp); 1288 if (error) { 1289 /* 1290 * The inode does not contain anything useful, so it would 1291 * be misleading to leave it on its hash chain. With mode 1292 * still zero, it will be unlinked and returned to the free 1293 * list by vput(). 1294 */ 1295 brelse(bp); 1296 vput(vp); 1297 *vpp = NULL; 1298 return (error); 1299 } 1300 if (ip->i_ump->um_fstype == UFS1) 1301 ip->i_din1 = uma_zalloc(uma_ufs1, M_WAITOK); 1302 else 1303 ip->i_din2 = uma_zalloc(uma_ufs2, M_WAITOK); 1304 ffs_load_inode(bp, ip, fs, ino); 1305 if (DOINGSOFTDEP(vp)) 1306 softdep_load_inodeblock(ip); 1307 else 1308 ip->i_effnlink = ip->i_nlink; 1309 bqrelse(bp); 1310 1311 /* 1312 * Initialize the vnode from the inode, check for aliases. 1313 * Note that the underlying vnode may have changed. 1314 */ 1315 if (ip->i_ump->um_fstype == UFS1) 1316 error = ufs_vinit(mp, &ffs_fifoops1, &vp); 1317 else 1318 error = ufs_vinit(mp, &ffs_fifoops2, &vp); 1319 if (error) { 1320 vput(vp); 1321 *vpp = NULL; 1322 return (error); 1323 } 1324 1325 /* 1326 * Finish inode initialization. 1327 */ 1328 1329 /* 1330 * Set up a generation number for this inode if it does not 1331 * already have one. This should only happen on old filesystems. 1332 */ 1333 if (ip->i_gen == 0) { 1334 ip->i_gen = arc4random() / 2 + 1; 1335 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) { 1336 ip->i_flag |= IN_MODIFIED; 1337 DIP_SET(ip, i_gen, ip->i_gen); 1338 } 1339 } 1340 /* 1341 * Ensure that uid and gid are correct. This is a temporary 1342 * fix until fsck has been changed to do the update. 1343 */ 1344 if (fs->fs_magic == FS_UFS1_MAGIC && /* XXX */ 1345 fs->fs_old_inodefmt < FS_44INODEFMT) { /* XXX */ 1346 ip->i_uid = ip->i_din1->di_ouid; /* XXX */ 1347 ip->i_gid = ip->i_din1->di_ogid; /* XXX */ 1348 } /* XXX */ 1349 1350 #ifdef MAC 1351 if ((mp->mnt_flag & MNT_MULTILABEL) && ip->i_mode) { 1352 /* 1353 * If this vnode is already allocated, and we're running 1354 * multi-label, attempt to perform a label association 1355 * from the extended attributes on the inode. 1356 */ 1357 error = mac_associate_vnode_extattr(mp, vp); 1358 if (error) { 1359 /* ufs_inactive will release ip->i_devvp ref. */ 1360 vput(vp); 1361 *vpp = NULL; 1362 return (error); 1363 } 1364 } 1365 #endif 1366 1367 *vpp = vp; 1368 return (0); 1369 } 1370 1371 /* 1372 * File handle to vnode 1373 * 1374 * Have to be really careful about stale file handles: 1375 * - check that the inode number is valid 1376 * - call ffs_vget() to get the locked inode 1377 * - check for an unallocated inode (i_mode == 0) 1378 * - check that the given client host has export rights and return 1379 * those rights via. exflagsp and credanonp 1380 */ 1381 static int 1382 ffs_fhtovp(mp, fhp, vpp) 1383 struct mount *mp; 1384 struct fid *fhp; 1385 struct vnode **vpp; 1386 { 1387 struct ufid *ufhp; 1388 struct fs *fs; 1389 1390 ufhp = (struct ufid *)fhp; 1391 fs = VFSTOUFS(mp)->um_fs; 1392 if (ufhp->ufid_ino < ROOTINO || 1393 ufhp->ufid_ino >= fs->fs_ncg * fs->fs_ipg) 1394 return (ESTALE); 1395 return (ufs_fhtovp(mp, ufhp, vpp)); 1396 } 1397 1398 /* 1399 * Vnode pointer to File handle 1400 */ 1401 /* ARGSUSED */ 1402 static int 1403 ffs_vptofh(vp, fhp) 1404 struct vnode *vp; 1405 struct fid *fhp; 1406 { 1407 struct inode *ip; 1408 struct ufid *ufhp; 1409 1410 ip = VTOI(vp); 1411 ufhp = (struct ufid *)fhp; 1412 ufhp->ufid_len = sizeof(struct ufid); 1413 ufhp->ufid_ino = ip->i_number; 1414 ufhp->ufid_gen = ip->i_gen; 1415 return (0); 1416 } 1417 1418 /* 1419 * Initialize the filesystem. 1420 */ 1421 static int 1422 ffs_init(vfsp) 1423 struct vfsconf *vfsp; 1424 { 1425 1426 softdep_initialize(); 1427 return (ufs_init(vfsp)); 1428 } 1429 1430 /* 1431 * Undo the work of ffs_init(). 1432 */ 1433 static int 1434 ffs_uninit(vfsp) 1435 struct vfsconf *vfsp; 1436 { 1437 int ret; 1438 1439 ret = ufs_uninit(vfsp); 1440 softdep_uninitialize(); 1441 return (ret); 1442 } 1443 1444 /* 1445 * Write a superblock and associated information back to disk. 1446 */ 1447 static int 1448 ffs_sbupdate(mp, waitfor) 1449 struct ufsmount *mp; 1450 int waitfor; 1451 { 1452 struct fs *fs = mp->um_fs; 1453 struct buf *sbbp; 1454 struct buf *bp; 1455 int blks; 1456 void *space; 1457 int i, size, error, allerror = 0; 1458 1459 if (fs->fs_ronly == 1 && 1460 (mp->um_mountp->mnt_flag & (MNT_RDONLY | MNT_UPDATE)) != 1461 (MNT_RDONLY | MNT_UPDATE)) 1462 panic("ffs_sbupdate: write read-only filesystem"); 1463 /* 1464 * We use the superblock's buf to serialize calls to ffs_sbupdate(). 1465 */ 1466 sbbp = getblk(mp->um_devvp, btodb(fs->fs_sblockloc), (int)fs->fs_sbsize, 1467 0, 0, 0); 1468 /* 1469 * First write back the summary information. 1470 */ 1471 blks = howmany(fs->fs_cssize, fs->fs_fsize); 1472 space = fs->fs_csp; 1473 for (i = 0; i < blks; i += fs->fs_frag) { 1474 size = fs->fs_bsize; 1475 if (i + fs->fs_frag > blks) 1476 size = (blks - i) * fs->fs_fsize; 1477 bp = getblk(mp->um_devvp, fsbtodb(fs, fs->fs_csaddr + i), 1478 size, 0, 0, 0); 1479 bcopy(space, bp->b_data, (u_int)size); 1480 space = (char *)space + size; 1481 if (waitfor != MNT_WAIT) 1482 bawrite(bp); 1483 else if ((error = bwrite(bp)) != 0) 1484 allerror = error; 1485 } 1486 /* 1487 * Now write back the superblock itself. If any errors occurred 1488 * up to this point, then fail so that the superblock avoids 1489 * being written out as clean. 1490 */ 1491 if (allerror) { 1492 brelse(sbbp); 1493 return (allerror); 1494 } 1495 bp = sbbp; 1496 if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_sblockloc != SBLOCK_UFS1 && 1497 (fs->fs_flags & FS_FLAGS_UPDATED) == 0) { 1498 printf("%s: correcting fs_sblockloc from %jd to %d\n", 1499 fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS1); 1500 fs->fs_sblockloc = SBLOCK_UFS1; 1501 } 1502 if (fs->fs_magic == FS_UFS2_MAGIC && fs->fs_sblockloc != SBLOCK_UFS2 && 1503 (fs->fs_flags & FS_FLAGS_UPDATED) == 0) { 1504 printf("%s: correcting fs_sblockloc from %jd to %d\n", 1505 fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS2); 1506 fs->fs_sblockloc = SBLOCK_UFS2; 1507 } 1508 fs->fs_fmod = 0; 1509 fs->fs_time = time_second; 1510 bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize); 1511 ffs_oldfscompat_write((struct fs *)bp->b_data, mp); 1512 if (waitfor != MNT_WAIT) 1513 bawrite(bp); 1514 else if ((error = bwrite(bp)) != 0) 1515 allerror = error; 1516 return (allerror); 1517 } 1518 1519 static int 1520 ffs_extattrctl(struct mount *mp, int cmd, struct vnode *filename_vp, 1521 int attrnamespace, const char *attrname, struct thread *td) 1522 { 1523 1524 #ifdef UFS_EXTATTR 1525 return (ufs_extattrctl(mp, cmd, filename_vp, attrnamespace, 1526 attrname, td)); 1527 #else 1528 return (vfs_stdextattrctl(mp, cmd, filename_vp, attrnamespace, 1529 attrname, td)); 1530 #endif 1531 } 1532 1533 static void 1534 ffs_ifree(struct ufsmount *ump, struct inode *ip) 1535 { 1536 1537 if (ump->um_fstype == UFS1 && ip->i_din1 != NULL) 1538 uma_zfree(uma_ufs1, ip->i_din1); 1539 else if (ip->i_din2 != NULL) 1540 uma_zfree(uma_ufs2, ip->i_din2); 1541 uma_zfree(uma_inode, ip); 1542 } 1543 1544 static int dobkgrdwrite = 1; 1545 SYSCTL_INT(_debug, OID_AUTO, dobkgrdwrite, CTLFLAG_RW, &dobkgrdwrite, 0, 1546 "Do background writes (honoring the BV_BKGRDWRITE flag)?"); 1547 1548 /* 1549 * Complete a background write started from bwrite. 1550 */ 1551 static void 1552 ffs_backgroundwritedone(struct buf *bp) 1553 { 1554 struct bufobj *bufobj; 1555 struct buf *origbp; 1556 1557 /* 1558 * Find the original buffer that we are writing. 1559 */ 1560 bufobj = bp->b_bufobj; 1561 BO_LOCK(bufobj); 1562 if ((origbp = gbincore(bp->b_bufobj, bp->b_lblkno)) == NULL) 1563 panic("backgroundwritedone: lost buffer"); 1564 /* Grab an extra reference to be dropped by the bufdone() below. */ 1565 bufobj_wrefl(bufobj); 1566 BO_UNLOCK(bufobj); 1567 /* 1568 * Process dependencies then return any unfinished ones. 1569 */ 1570 if (LIST_FIRST(&bp->b_dep) != NULL) 1571 buf_complete(bp); 1572 #ifdef SOFTUPDATES 1573 if (LIST_FIRST(&bp->b_dep) != NULL) 1574 softdep_move_dependencies(bp, origbp); 1575 #endif 1576 /* 1577 * This buffer is marked B_NOCACHE so when it is released 1578 * by biodone it will be tossed. 1579 */ 1580 bp->b_flags |= B_NOCACHE; 1581 bp->b_flags &= ~B_CACHE; 1582 bufdone(bp); 1583 BO_LOCK(bufobj); 1584 /* 1585 * Clear the BV_BKGRDINPROG flag in the original buffer 1586 * and awaken it if it is waiting for the write to complete. 1587 * If BV_BKGRDINPROG is not set in the original buffer it must 1588 * have been released and re-instantiated - which is not legal. 1589 */ 1590 KASSERT((origbp->b_vflags & BV_BKGRDINPROG), 1591 ("backgroundwritedone: lost buffer2")); 1592 origbp->b_vflags &= ~BV_BKGRDINPROG; 1593 if (origbp->b_vflags & BV_BKGRDWAIT) { 1594 origbp->b_vflags &= ~BV_BKGRDWAIT; 1595 wakeup(&origbp->b_xflags); 1596 } 1597 BO_UNLOCK(bufobj); 1598 } 1599 1600 1601 /* 1602 * Write, release buffer on completion. (Done by iodone 1603 * if async). Do not bother writing anything if the buffer 1604 * is invalid. 1605 * 1606 * Note that we set B_CACHE here, indicating that buffer is 1607 * fully valid and thus cacheable. This is true even of NFS 1608 * now so we set it generally. This could be set either here 1609 * or in biodone() since the I/O is synchronous. We put it 1610 * here. 1611 */ 1612 static int 1613 ffs_bufwrite(struct buf *bp) 1614 { 1615 int oldflags, s; 1616 struct buf *newbp; 1617 1618 CTR3(KTR_BUF, "bufwrite(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags); 1619 if (bp->b_flags & B_INVAL) { 1620 brelse(bp); 1621 return (0); 1622 } 1623 1624 oldflags = bp->b_flags; 1625 1626 if (BUF_REFCNT(bp) == 0) 1627 panic("bufwrite: buffer is not busy???"); 1628 s = splbio(); 1629 /* 1630 * If a background write is already in progress, delay 1631 * writing this block if it is asynchronous. Otherwise 1632 * wait for the background write to complete. 1633 */ 1634 BO_LOCK(bp->b_bufobj); 1635 if (bp->b_vflags & BV_BKGRDINPROG) { 1636 if (bp->b_flags & B_ASYNC) { 1637 BO_UNLOCK(bp->b_bufobj); 1638 splx(s); 1639 bdwrite(bp); 1640 return (0); 1641 } 1642 bp->b_vflags |= BV_BKGRDWAIT; 1643 msleep(&bp->b_xflags, BO_MTX(bp->b_bufobj), PRIBIO, "bwrbg", 0); 1644 if (bp->b_vflags & BV_BKGRDINPROG) 1645 panic("bufwrite: still writing"); 1646 } 1647 BO_UNLOCK(bp->b_bufobj); 1648 1649 /* Mark the buffer clean */ 1650 bundirty(bp); 1651 1652 /* 1653 * If this buffer is marked for background writing and we 1654 * do not have to wait for it, make a copy and write the 1655 * copy so as to leave this buffer ready for further use. 1656 * 1657 * This optimization eats a lot of memory. If we have a page 1658 * or buffer shortfall we can't do it. 1659 */ 1660 if (dobkgrdwrite && (bp->b_xflags & BX_BKGRDWRITE) && 1661 (bp->b_flags & B_ASYNC) && 1662 !vm_page_count_severe() && 1663 !buf_dirty_count_severe()) { 1664 KASSERT(bp->b_iodone == NULL, 1665 ("bufwrite: needs chained iodone (%p)", bp->b_iodone)); 1666 1667 /* get a new block */ 1668 newbp = geteblk(bp->b_bufsize); 1669 1670 /* 1671 * set it to be identical to the old block. We have to 1672 * set b_lblkno and BKGRDMARKER before calling bgetvp() 1673 * to avoid confusing the splay tree and gbincore(). 1674 */ 1675 memcpy(newbp->b_data, bp->b_data, bp->b_bufsize); 1676 newbp->b_lblkno = bp->b_lblkno; 1677 newbp->b_xflags |= BX_BKGRDMARKER; 1678 BO_LOCK(bp->b_bufobj); 1679 bp->b_vflags |= BV_BKGRDINPROG; 1680 bgetvp(bp->b_vp, newbp); 1681 BO_UNLOCK(bp->b_bufobj); 1682 newbp->b_bufobj = &bp->b_vp->v_bufobj; 1683 newbp->b_blkno = bp->b_blkno; 1684 newbp->b_offset = bp->b_offset; 1685 newbp->b_iodone = ffs_backgroundwritedone; 1686 newbp->b_flags |= B_ASYNC; 1687 newbp->b_flags &= ~B_INVAL; 1688 1689 #ifdef SOFTUPDATES 1690 /* move over the dependencies */ 1691 if (LIST_FIRST(&bp->b_dep) != NULL) 1692 softdep_move_dependencies(bp, newbp); 1693 #endif 1694 1695 /* 1696 * Initiate write on the copy, release the original to 1697 * the B_LOCKED queue so that it cannot go away until 1698 * the background write completes. If not locked it could go 1699 * away and then be reconstituted while it was being written. 1700 * If the reconstituted buffer were written, we could end up 1701 * with two background copies being written at the same time. 1702 */ 1703 bqrelse(bp); 1704 bp = newbp; 1705 } 1706 1707 /* Let the normal bufwrite do the rest for us */ 1708 return (bufwrite(bp)); 1709 } 1710 1711 1712 static void 1713 ffs_geom_strategy(struct bufobj *bo, struct buf *bp) 1714 { 1715 struct vnode *vp; 1716 int error; 1717 1718 vp = bo->__bo_vnode; 1719 if (bp->b_iocmd == BIO_WRITE) { 1720 #ifdef SOFTUPDATES 1721 if (LIST_FIRST(&bp->b_dep) != NULL) 1722 buf_start(bp); 1723 #endif 1724 if ((bp->b_flags & B_VALIDSUSPWRT) == 0 && 1725 bp->b_vp != NULL && bp->b_vp->v_mount != NULL && 1726 (bp->b_vp->v_mount->mnt_kern_flag & MNTK_SUSPENDED) != 0) 1727 panic("ffs_geom_strategy: bad I/O"); 1728 bp->b_flags &= ~B_VALIDSUSPWRT; 1729 if ((vp->v_vflag & VV_COPYONWRITE) && 1730 vp->v_rdev->si_snapdata != NULL && 1731 (error = (ffs_copyonwrite)(vp, bp)) != 0 && 1732 error != EOPNOTSUPP) { 1733 bp->b_error = error; 1734 bp->b_ioflags |= BIO_ERROR; 1735 bufdone(bp); 1736 return; 1737 } 1738 } 1739 g_vfs_strategy(bo, bp); 1740 } 1741