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