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 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * @(#)ffs_vfsops.c 8.31 (Berkeley) 5/20/95 34 * $Id: ffs_vfsops.c,v 1.55 1997/09/07 16:20:59 bde Exp $ 35 */ 36 37 #include "opt_quota.h" 38 39 #include <sys/param.h> 40 #include <sys/systm.h> 41 #include <sys/namei.h> 42 #include <sys/proc.h> 43 #include <sys/kernel.h> 44 #include <sys/vnode.h> 45 #include <sys/mount.h> 46 #include <sys/buf.h> 47 #include <sys/conf.h> 48 #include <sys/fcntl.h> 49 #include <sys/disklabel.h> 50 #include <sys/malloc.h> 51 52 #include <miscfs/specfs/specdev.h> 53 54 #include <ufs/ufs/quota.h> 55 #include <ufs/ufs/ufsmount.h> 56 #include <ufs/ufs/inode.h> 57 #include <ufs/ufs/ufs_extern.h> 58 59 #include <ufs/ffs/fs.h> 60 #include <ufs/ffs/ffs_extern.h> 61 62 #include <vm/vm.h> 63 #include <vm/vm_prot.h> 64 #include <vm/vm_page.h> 65 #include <vm/vm_extern.h> 66 67 static int ffs_sbupdate __P((struct ufsmount *, int)); 68 static int ffs_reload __P((struct mount *,struct ucred *,struct proc *)); 69 static int ffs_oldfscompat __P((struct fs *)); 70 static int ffs_mount __P((struct mount *, char *, caddr_t, 71 struct nameidata *, struct proc *)); 72 static int ffs_init __P((struct vfsconf *)); 73 74 struct vfsops ufs_vfsops = { 75 ffs_mount, 76 ufs_start, 77 ffs_unmount, 78 ufs_root, 79 ufs_quotactl, 80 ffs_statfs, 81 ffs_sync, 82 ffs_vget, 83 ffs_fhtovp, 84 ffs_vptofh, 85 ffs_init, 86 }; 87 88 VFS_SET(ufs_vfsops, ufs, MOUNT_UFS, 0); 89 90 /* 91 * ffs_mount 92 * 93 * Called when mounting local physical media 94 * 95 * PARAMETERS: 96 * mountroot 97 * mp mount point structure 98 * path NULL (flag for root mount!!!) 99 * data <unused> 100 * ndp <unused> 101 * p process (user credentials check [statfs]) 102 * 103 * mount 104 * mp mount point structure 105 * path path to mount point 106 * data pointer to argument struct in user space 107 * ndp mount point namei() return (used for 108 * credentials on reload), reused to look 109 * up block device. 110 * p process (user credentials check) 111 * 112 * RETURNS: 0 Success 113 * !0 error number (errno.h) 114 * 115 * LOCK STATE: 116 * 117 * ENTRY 118 * mount point is locked 119 * EXIT 120 * mount point is locked 121 * 122 * NOTES: 123 * A NULL path can be used for a flag since the mount 124 * system call will fail with EFAULT in copyinstr in 125 * namei() if it is a genuine NULL from the user. 126 */ 127 static int 128 ffs_mount( mp, path, data, ndp, p) 129 struct mount *mp; /* mount struct pointer*/ 130 char *path; /* path to mount point*/ 131 caddr_t data; /* arguments to FS specific mount*/ 132 struct nameidata *ndp; /* mount point credentials*/ 133 struct proc *p; /* process requesting mount*/ 134 { 135 u_int size; 136 int err = 0; 137 struct vnode *devvp; 138 139 struct ufs_args args; 140 struct ufsmount *ump = 0; 141 register struct fs *fs; 142 int flags; 143 144 /* 145 * Use NULL path to flag a root mount 146 */ 147 if( path == NULL) { 148 /* 149 *** 150 * Mounting root file system 151 *** 152 */ 153 154 if ((err = bdevvp(rootdev, &rootvp))) { 155 printf("ffs_mountroot: can't find rootvp"); 156 return (err); 157 } 158 159 if (bdevsw[major(rootdev)]->d_flags & D_NOCLUSTERR) 160 mp->mnt_flag |= MNT_NOCLUSTERR; 161 if (bdevsw[major(rootdev)]->d_flags & D_NOCLUSTERW) 162 mp->mnt_flag |= MNT_NOCLUSTERW; 163 if( ( err = ffs_mountfs(rootvp, mp, p)) != 0) { 164 /* fs specific cleanup (if any)*/ 165 goto error_1; 166 } 167 168 goto dostatfs; /* success*/ 169 170 } 171 172 /* 173 *** 174 * Mounting non-root file system or updating a file system 175 *** 176 */ 177 178 /* copy in user arguments*/ 179 err = copyin(data, (caddr_t)&args, sizeof (struct ufs_args)); 180 if (err) 181 goto error_1; /* can't get arguments*/ 182 183 /* 184 * If updating, check whether changing from read-only to 185 * read/write; if there is no device name, that's all we do. 186 * Disallow clearing MNT_NOCLUSTERR and MNT_NOCLUSTERW flags, 187 * if block device requests. 188 */ 189 if (mp->mnt_flag & MNT_UPDATE) { 190 ump = VFSTOUFS(mp); 191 fs = ump->um_fs; 192 err = 0; 193 if (bdevsw[major(ump->um_dev)]->d_flags & D_NOCLUSTERR) 194 mp->mnt_flag |= MNT_NOCLUSTERR; 195 if (bdevsw[major(ump->um_dev)]->d_flags & D_NOCLUSTERW) 196 mp->mnt_flag |= MNT_NOCLUSTERW; 197 if (fs->fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) { 198 flags = WRITECLOSE; 199 if (mp->mnt_flag & MNT_FORCE) 200 flags |= FORCECLOSE; 201 err = ffs_flushfiles(mp, flags, p); 202 } 203 if (!err && (mp->mnt_flag & MNT_RELOAD)) 204 err = ffs_reload(mp, ndp->ni_cnd.cn_cred, p); 205 if (err) { 206 goto error_1; 207 } 208 if (fs->fs_ronly && (mp->mnt_flag & MNT_WANTRDWR)) { 209 if (!fs->fs_clean) { 210 if (mp->mnt_flag & MNT_FORCE) { 211 printf("WARNING: %s was not properly dismounted.\n",fs->fs_fsmnt); 212 } else { 213 printf("WARNING: R/W mount of %s denied. Filesystem is not clean - run fsck.\n", 214 fs->fs_fsmnt); 215 err = EPERM; 216 goto error_1; 217 } 218 } 219 fs->fs_ronly = 0; 220 } 221 if (fs->fs_ronly == 0) { 222 fs->fs_clean = 0; 223 ffs_sbupdate(ump, MNT_WAIT); 224 } 225 /* if not updating name...*/ 226 if (args.fspec == 0) { 227 /* 228 * Process export requests. Jumping to "success" 229 * will return the vfs_export() error code. 230 */ 231 err = vfs_export(mp, &ump->um_export, &args.export); 232 goto success; 233 } 234 } 235 236 /* 237 * Not an update, or updating the name: look up the name 238 * and verify that it refers to a sensible block device. 239 */ 240 NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p); 241 err = namei(ndp); 242 if (err) { 243 /* can't get devvp!*/ 244 goto error_1; 245 } 246 247 devvp = ndp->ni_vp; 248 249 if (devvp->v_type != VBLK) { 250 err = ENOTBLK; 251 goto error_2; 252 } 253 if (major(devvp->v_rdev) >= nblkdev) { 254 err = ENXIO; 255 goto error_2; 256 } 257 if (mp->mnt_flag & MNT_UPDATE) { 258 /* 259 ******************** 260 * UPDATE 261 ******************** 262 */ 263 264 if (devvp != ump->um_devvp) 265 err = EINVAL; /* needs translation */ 266 else 267 vrele(devvp); 268 /* 269 * Update device name only on success 270 */ 271 if( !err) { 272 /* Save "mounted from" info for mount point (NULL pad)*/ 273 copyinstr( args.fspec, 274 mp->mnt_stat.f_mntfromname, 275 MNAMELEN - 1, 276 &size); 277 bzero( mp->mnt_stat.f_mntfromname + size, MNAMELEN - size); 278 } 279 } else { 280 /* 281 ******************** 282 * NEW MOUNT 283 ******************** 284 */ 285 286 if (bdevsw[major(devvp->v_rdev)]->d_flags & D_NOCLUSTERR) 287 mp->mnt_flag |= MNT_NOCLUSTERR; 288 if (bdevsw[major(devvp->v_rdev)]->d_flags & D_NOCLUSTERW) 289 mp->mnt_flag |= MNT_NOCLUSTERW; 290 291 /* 292 * Since this is a new mount, we want the names for 293 * the device and the mount point copied in. If an 294 * error occurs, the mountpoint is discarded by the 295 * upper level code. 296 */ 297 /* Save "last mounted on" info for mount point (NULL pad)*/ 298 copyinstr( path, /* mount point*/ 299 mp->mnt_stat.f_mntonname, /* save area*/ 300 MNAMELEN - 1, /* max size*/ 301 &size); /* real size*/ 302 bzero( mp->mnt_stat.f_mntonname + size, MNAMELEN - size); 303 304 /* Save "mounted from" info for mount point (NULL pad)*/ 305 copyinstr( args.fspec, /* device name*/ 306 mp->mnt_stat.f_mntfromname, /* save area*/ 307 MNAMELEN - 1, /* max size*/ 308 &size); /* real size*/ 309 bzero( mp->mnt_stat.f_mntfromname + size, MNAMELEN - size); 310 311 err = ffs_mountfs(devvp, mp, p); 312 } 313 if (err) { 314 goto error_2; 315 } 316 317 dostatfs: 318 /* 319 * Initialize FS stat information in mount struct; uses both 320 * mp->mnt_stat.f_mntonname and mp->mnt_stat.f_mntfromname 321 * 322 * This code is common to root and non-root mounts 323 */ 324 (void)VFS_STATFS(mp, &mp->mnt_stat, p); 325 326 goto success; 327 328 329 error_2: /* error with devvp held*/ 330 331 /* release devvp before failing*/ 332 vrele(devvp); 333 334 error_1: /* no state to back out*/ 335 336 success: 337 return( err); 338 } 339 340 /* 341 * Reload all incore data for a filesystem (used after running fsck on 342 * the root filesystem and finding things to fix). The filesystem must 343 * be mounted read-only. 344 * 345 * Things to do to update the mount: 346 * 1) invalidate all cached meta-data. 347 * 2) re-read superblock from disk. 348 * 3) re-read summary information from disk. 349 * 4) invalidate all inactive vnodes. 350 * 5) invalidate all cached file data. 351 * 6) re-read inode data for all active vnodes. 352 */ 353 static int 354 ffs_reload(mp, cred, p) 355 register struct mount *mp; 356 struct ucred *cred; 357 struct proc *p; 358 { 359 register struct vnode *vp, *nvp, *devvp; 360 struct inode *ip; 361 struct csum *space; 362 struct buf *bp; 363 struct fs *fs, *newfs; 364 struct partinfo dpart; 365 int i, blks, size, error; 366 int32_t *lp; 367 368 if ((mp->mnt_flag & MNT_RDONLY) == 0) 369 return (EINVAL); 370 /* 371 * Step 1: invalidate all cached meta-data. 372 */ 373 devvp = VFSTOUFS(mp)->um_devvp; 374 if (vinvalbuf(devvp, 0, cred, p, 0, 0)) 375 panic("ffs_reload: dirty1"); 376 /* 377 * Step 2: re-read superblock from disk. 378 */ 379 if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, NOCRED, p) != 0) 380 size = DEV_BSIZE; 381 else 382 size = dpart.disklab->d_secsize; 383 if (error = bread(devvp, (ufs_daddr_t)(SBOFF/size), SBSIZE, NOCRED,&bp)) 384 return (error); 385 newfs = (struct fs *)bp->b_data; 386 if (newfs->fs_magic != FS_MAGIC || newfs->fs_bsize > MAXBSIZE || 387 newfs->fs_bsize < sizeof(struct fs)) { 388 brelse(bp); 389 return (EIO); /* XXX needs translation */ 390 } 391 fs = VFSTOUFS(mp)->um_fs; 392 /* 393 * Copy pointer fields back into superblock before copying in XXX 394 * new superblock. These should really be in the ufsmount. XXX 395 * Note that important parameters (eg fs_ncg) are unchanged. 396 */ 397 bcopy(&fs->fs_csp[0], &newfs->fs_csp[0], sizeof(fs->fs_csp)); 398 newfs->fs_maxcluster = fs->fs_maxcluster; 399 bcopy(newfs, fs, (u_int)fs->fs_sbsize); 400 if (fs->fs_sbsize < SBSIZE) 401 bp->b_flags |= B_INVAL; 402 brelse(bp); 403 mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen; 404 ffs_oldfscompat(fs); 405 406 /* 407 * Step 3: re-read summary information from disk. 408 */ 409 blks = howmany(fs->fs_cssize, fs->fs_fsize); 410 space = fs->fs_csp[0]; 411 for (i = 0; i < blks; i += fs->fs_frag) { 412 size = fs->fs_bsize; 413 if (i + fs->fs_frag > blks) 414 size = (blks - i) * fs->fs_fsize; 415 error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size, 416 NOCRED, &bp); 417 if (error) 418 return (error); 419 bcopy(bp->b_data, fs->fs_csp[fragstoblks(fs, i)], (u_int)size); 420 brelse(bp); 421 } 422 /* 423 * We no longer know anything about clusters per cylinder group. 424 */ 425 if (fs->fs_contigsumsize > 0) { 426 lp = fs->fs_maxcluster; 427 for (i = 0; i < fs->fs_ncg; i++) 428 *lp++ = fs->fs_contigsumsize; 429 } 430 431 loop: 432 simple_lock(&mntvnode_slock); 433 for (vp = mp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) { 434 if (vp->v_mount != mp) { 435 simple_unlock(&mntvnode_slock); 436 goto loop; 437 } 438 nvp = vp->v_mntvnodes.le_next; 439 /* 440 * Step 4: invalidate all inactive vnodes. 441 */ 442 if (vrecycle(vp, &mntvnode_slock, p)) 443 goto loop; 444 /* 445 * Step 5: invalidate all cached file data. 446 */ 447 simple_lock(&vp->v_interlock); 448 simple_unlock(&mntvnode_slock); 449 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, p)) { 450 goto loop; 451 } 452 if (vinvalbuf(vp, 0, cred, p, 0, 0)) 453 panic("ffs_reload: dirty2"); 454 /* 455 * Step 6: re-read inode data for all active vnodes. 456 */ 457 ip = VTOI(vp); 458 error = 459 bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)), 460 (int)fs->fs_bsize, NOCRED, &bp); 461 if (error) { 462 vput(vp); 463 return (error); 464 } 465 ip->i_din = *((struct dinode *)bp->b_data + 466 ino_to_fsbo(fs, ip->i_number)); 467 brelse(bp); 468 vput(vp); 469 simple_lock(&mntvnode_slock); 470 } 471 simple_unlock(&mntvnode_slock); 472 return (0); 473 } 474 475 /* 476 * Common code for mount and mountroot 477 */ 478 int 479 ffs_mountfs(devvp, mp, p) 480 register struct vnode *devvp; 481 struct mount *mp; 482 struct proc *p; 483 { 484 register struct ufsmount *ump; 485 struct buf *bp; 486 register struct fs *fs; 487 dev_t dev; 488 struct partinfo dpart; 489 caddr_t base, space; 490 int error, i, blks, size, ronly; 491 int32_t *lp; 492 struct ucred *cred; 493 u_int64_t maxfilesize; /* XXX */ 494 u_int strsize; 495 int ncount; 496 497 dev = devvp->v_rdev; 498 cred = p ? p->p_ucred : NOCRED; 499 /* 500 * Disallow multiple mounts of the same device. 501 * Disallow mounting of a device that is currently in use 502 * (except for root, which might share swap device for miniroot). 503 * Flush out any old buffers remaining from a previous use. 504 */ 505 error = vfs_mountedon(devvp); 506 if (error) 507 return (error); 508 ncount = vcount(devvp); 509 if (devvp->v_object) 510 ncount -= 1; 511 if (ncount > 1 && devvp != rootvp) 512 return (EBUSY); 513 if (error = vinvalbuf(devvp, V_SAVE, cred, p, 0, 0)) 514 return (error); 515 516 ronly = (mp->mnt_flag & MNT_RDONLY) != 0; 517 error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p); 518 if (error) 519 return (error); 520 if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, cred, p) != 0) 521 size = DEV_BSIZE; 522 else 523 size = dpart.disklab->d_secsize; 524 525 bp = NULL; 526 ump = NULL; 527 if (error = bread(devvp, SBLOCK, SBSIZE, cred, &bp)) 528 goto out; 529 fs = (struct fs *)bp->b_data; 530 if (fs->fs_magic != FS_MAGIC || fs->fs_bsize > MAXBSIZE || 531 fs->fs_bsize < sizeof(struct fs)) { 532 error = EINVAL; /* XXX needs translation */ 533 goto out; 534 } 535 fs->fs_fmod = 0; 536 if (!fs->fs_clean) { 537 if (ronly || (mp->mnt_flag & MNT_FORCE)) { 538 printf("WARNING: %s was not properly dismounted.\n",fs->fs_fsmnt); 539 } else { 540 printf("WARNING: R/W mount of %s denied. Filesystem is not clean - run fsck.\n",fs->fs_fsmnt); 541 error = EPERM; 542 goto out; 543 } 544 } 545 /* XXX updating 4.2 FFS superblocks trashes rotational layout tables */ 546 if (fs->fs_postblformat == FS_42POSTBLFMT && !ronly) { 547 error = EROFS; /* needs translation */ 548 goto out; 549 } 550 ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK); 551 bzero((caddr_t)ump, sizeof *ump); 552 ump->um_fs = malloc((u_long)fs->fs_sbsize, M_UFSMNT, 553 M_WAITOK); 554 bcopy(bp->b_data, ump->um_fs, (u_int)fs->fs_sbsize); 555 if (fs->fs_sbsize < SBSIZE) 556 bp->b_flags |= B_INVAL; 557 brelse(bp); 558 bp = NULL; 559 fs = ump->um_fs; 560 fs->fs_ronly = ronly; 561 if (ronly == 0) { 562 fs->fs_fmod = 1; 563 fs->fs_clean = 0; 564 } 565 size = fs->fs_cssize; 566 blks = howmany(size, fs->fs_fsize); 567 if (fs->fs_contigsumsize > 0) 568 size += fs->fs_ncg * sizeof(int32_t); 569 base = space = malloc((u_long)size, M_UFSMNT, M_WAITOK); 570 for (i = 0; i < blks; i += fs->fs_frag) { 571 size = fs->fs_bsize; 572 if (i + fs->fs_frag > blks) 573 size = (blks - i) * fs->fs_fsize; 574 if (error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size, 575 cred, &bp)) { 576 free(base, M_UFSMNT); 577 goto out; 578 } 579 bcopy(bp->b_data, space, (u_int)size); 580 fs->fs_csp[fragstoblks(fs, i)] = (struct csum *)space; 581 space += size; 582 brelse(bp); 583 bp = NULL; 584 } 585 if (fs->fs_contigsumsize > 0) { 586 fs->fs_maxcluster = lp = (int32_t *)space; 587 for (i = 0; i < fs->fs_ncg; i++) 588 *lp++ = fs->fs_contigsumsize; 589 } 590 mp->mnt_data = (qaddr_t)ump; 591 mp->mnt_stat.f_fsid.val[0] = (long)dev; 592 if (fs->fs_id[0] != 0 && fs->fs_id[1] != 0) 593 mp->mnt_stat.f_fsid.val[1] = fs->fs_id[1]; 594 else 595 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum; 596 mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen; 597 mp->mnt_flag |= MNT_LOCAL; 598 ump->um_mountp = mp; 599 ump->um_dev = dev; 600 ump->um_devvp = devvp; 601 ump->um_nindir = fs->fs_nindir; 602 ump->um_bptrtodb = fs->fs_fsbtodb; 603 ump->um_seqinc = fs->fs_frag; 604 for (i = 0; i < MAXQUOTAS; i++) 605 ump->um_quotas[i] = NULLVP; 606 devvp->v_specflags |= SI_MOUNTEDON; 607 ffs_oldfscompat(fs); 608 609 /* 610 * Set FS local "last mounted on" information (NULL pad) 611 */ 612 copystr( mp->mnt_stat.f_mntonname, /* mount point*/ 613 fs->fs_fsmnt, /* copy area*/ 614 sizeof(fs->fs_fsmnt) - 1, /* max size*/ 615 &strsize); /* real size*/ 616 bzero( fs->fs_fsmnt + strsize, sizeof(fs->fs_fsmnt) - strsize); 617 618 if( mp->mnt_flag & MNT_ROOTFS) { 619 /* 620 * Root mount; update timestamp in mount structure. 621 * this will be used by the common root mount code 622 * to update the system clock. 623 */ 624 mp->mnt_time = fs->fs_time; 625 } 626 627 ump->um_savedmaxfilesize = fs->fs_maxfilesize; /* XXX */ 628 maxfilesize = (u_int64_t)0x40000000 * fs->fs_bsize - 1; /* XXX */ 629 if (fs->fs_maxfilesize > maxfilesize) /* XXX */ 630 fs->fs_maxfilesize = maxfilesize; /* XXX */ 631 if (ronly == 0) { 632 fs->fs_clean = 0; 633 (void) ffs_sbupdate(ump, MNT_WAIT); 634 } 635 /* 636 * Only VMIO the backing device if the backing device is a real 637 * block device. This excludes the original MFS implementation. 638 * Note that it is optional that the backing device be VMIOed. This 639 * increases the opportunity for metadata caching. 640 */ 641 if ((devvp->v_type == VBLK) && (major(devvp->v_rdev) < nblkdev)) { 642 vfs_object_create(devvp, p, p->p_ucred, 0); 643 } 644 return (0); 645 out: 646 if (bp) 647 brelse(bp); 648 (void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, cred, p); 649 if (ump) { 650 free(ump->um_fs, M_UFSMNT); 651 free(ump, M_UFSMNT); 652 mp->mnt_data = (qaddr_t)0; 653 } 654 return (error); 655 } 656 657 /* 658 * Sanity checks for old file systems. 659 * 660 * XXX - goes away some day. 661 */ 662 static int 663 ffs_oldfscompat(fs) 664 struct fs *fs; 665 { 666 667 fs->fs_npsect = max(fs->fs_npsect, fs->fs_nsect); /* XXX */ 668 fs->fs_interleave = max(fs->fs_interleave, 1); /* XXX */ 669 if (fs->fs_postblformat == FS_42POSTBLFMT) /* XXX */ 670 fs->fs_nrpos = 8; /* XXX */ 671 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */ 672 #if 0 673 int i; /* XXX */ 674 u_int64_t sizepb = fs->fs_bsize; /* XXX */ 675 /* XXX */ 676 fs->fs_maxfilesize = fs->fs_bsize * NDADDR - 1; /* XXX */ 677 for (i = 0; i < NIADDR; i++) { /* XXX */ 678 sizepb *= NINDIR(fs); /* XXX */ 679 fs->fs_maxfilesize += sizepb; /* XXX */ 680 } /* XXX */ 681 #endif 682 fs->fs_maxfilesize = (u_quad_t) 1LL << 39; 683 fs->fs_qbmask = ~fs->fs_bmask; /* XXX */ 684 fs->fs_qfmask = ~fs->fs_fmask; /* XXX */ 685 } /* XXX */ 686 return (0); 687 } 688 689 /* 690 * unmount system call 691 */ 692 int 693 ffs_unmount(mp, mntflags, p) 694 struct mount *mp; 695 int mntflags; 696 struct proc *p; 697 { 698 register struct ufsmount *ump; 699 register struct fs *fs; 700 int error, flags; 701 702 flags = 0; 703 if (mntflags & MNT_FORCE) { 704 flags |= FORCECLOSE; 705 } 706 error = ffs_flushfiles(mp, flags, p); 707 if (error) 708 return (error); 709 ump = VFSTOUFS(mp); 710 fs = ump->um_fs; 711 if (fs->fs_ronly == 0) { 712 fs->fs_clean = 1; 713 error = ffs_sbupdate(ump, MNT_WAIT); 714 if (error) { 715 fs->fs_clean = 0; 716 return (error); 717 } 718 } 719 ump->um_devvp->v_specflags &= ~SI_MOUNTEDON; 720 721 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY, p); 722 vnode_pager_uncache(ump->um_devvp, p); 723 VOP_UNLOCK(ump->um_devvp, 0, p); 724 725 error = VOP_CLOSE(ump->um_devvp, fs->fs_ronly ? FREAD : FREAD|FWRITE, 726 NOCRED, p); 727 728 vrele(ump->um_devvp); 729 730 free(fs->fs_csp[0], M_UFSMNT); 731 free(fs, M_UFSMNT); 732 free(ump, M_UFSMNT); 733 mp->mnt_data = (qaddr_t)0; 734 mp->mnt_flag &= ~MNT_LOCAL; 735 return (error); 736 } 737 738 /* 739 * Flush out all the files in a filesystem. 740 */ 741 int 742 ffs_flushfiles(mp, flags, p) 743 register struct mount *mp; 744 int flags; 745 struct proc *p; 746 { 747 register struct ufsmount *ump; 748 int error; 749 750 ump = VFSTOUFS(mp); 751 #ifdef QUOTA 752 if (mp->mnt_flag & MNT_QUOTA) { 753 int i; 754 error = vflush(mp, NULLVP, SKIPSYSTEM|flags); 755 if (error) 756 return (error); 757 for (i = 0; i < MAXQUOTAS; i++) { 758 if (ump->um_quotas[i] == NULLVP) 759 continue; 760 quotaoff(p, mp, i); 761 } 762 /* 763 * Here we fall through to vflush again to ensure 764 * that we have gotten rid of all the system vnodes. 765 */ 766 } 767 #endif 768 error = vflush(mp, NULLVP, flags); 769 return (error); 770 } 771 772 /* 773 * Get file system statistics. 774 */ 775 int 776 ffs_statfs(mp, sbp, p) 777 struct mount *mp; 778 register struct statfs *sbp; 779 struct proc *p; 780 { 781 register struct ufsmount *ump; 782 register struct fs *fs; 783 784 ump = VFSTOUFS(mp); 785 fs = ump->um_fs; 786 if (fs->fs_magic != FS_MAGIC) 787 panic("ffs_statfs"); 788 sbp->f_bsize = fs->fs_fsize; 789 sbp->f_iosize = fs->fs_bsize; 790 sbp->f_blocks = fs->fs_dsize; 791 sbp->f_bfree = fs->fs_cstotal.cs_nbfree * fs->fs_frag + 792 fs->fs_cstotal.cs_nffree; 793 sbp->f_bavail = freespace(fs, fs->fs_minfree); 794 sbp->f_files = fs->fs_ncg * fs->fs_ipg - ROOTINO; 795 sbp->f_ffree = fs->fs_cstotal.cs_nifree; 796 if (sbp != &mp->mnt_stat) { 797 sbp->f_type = mp->mnt_vfc->vfc_typenum; 798 bcopy((caddr_t)mp->mnt_stat.f_mntonname, 799 (caddr_t)&sbp->f_mntonname[0], MNAMELEN); 800 bcopy((caddr_t)mp->mnt_stat.f_mntfromname, 801 (caddr_t)&sbp->f_mntfromname[0], MNAMELEN); 802 } 803 return (0); 804 } 805 806 /* 807 * Go through the disk queues to initiate sandbagged IO; 808 * go through the inodes to write those that have been modified; 809 * initiate the writing of the super block if it has been modified. 810 * 811 * Note: we are always called with the filesystem marked `MPBUSY'. 812 */ 813 int 814 ffs_sync(mp, waitfor, cred, p) 815 struct mount *mp; 816 int waitfor; 817 struct ucred *cred; 818 struct proc *p; 819 { 820 struct vnode *nvp, *vp; 821 struct inode *ip; 822 struct ufsmount *ump = VFSTOUFS(mp); 823 struct fs *fs; 824 struct timeval tv; 825 int error, allerror = 0; 826 827 fs = ump->um_fs; 828 if (fs->fs_fmod != 0 && fs->fs_ronly != 0) { /* XXX */ 829 printf("fs = %s\n", fs->fs_fsmnt); 830 panic("ffs_sync: rofs mod"); 831 } 832 /* 833 * Write back each (modified) inode. 834 */ 835 simple_lock(&mntvnode_slock); 836 loop: 837 for (vp = mp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) { 838 /* 839 * If the vnode that we are about to sync is no longer 840 * associated with this mount point, start over. 841 */ 842 if (vp->v_mount != mp) 843 goto loop; 844 simple_lock(&vp->v_interlock); 845 nvp = vp->v_mntvnodes.le_next; 846 ip = VTOI(vp); 847 if (((ip->i_flag & 848 (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0) && 849 vp->v_dirtyblkhd.lh_first == NULL) { 850 simple_unlock(&vp->v_interlock); 851 continue; 852 } 853 if (vp->v_type != VCHR) { 854 simple_unlock(&mntvnode_slock); 855 error = 856 vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, p); 857 if (error) { 858 simple_lock(&mntvnode_slock); 859 if (error == ENOENT) 860 goto loop; 861 continue; 862 } 863 if (error = VOP_FSYNC(vp, cred, waitfor, p)) 864 allerror = error; 865 VOP_UNLOCK(vp, 0, p); 866 vrele(vp); 867 simple_lock(&mntvnode_slock); 868 } else { 869 simple_unlock(&mntvnode_slock); 870 simple_unlock(&vp->v_interlock); 871 gettime(&tv); 872 /* VOP_UPDATE(vp, &tv, &tv, waitfor == MNT_WAIT); */ 873 VOP_UPDATE(vp, &tv, &tv, 0); 874 simple_lock(&mntvnode_slock); 875 } 876 } 877 simple_unlock(&mntvnode_slock); 878 /* 879 * Force stale file system control information to be flushed. 880 */ 881 error = VOP_FSYNC(ump->um_devvp, cred, waitfor, p); 882 if (error) 883 allerror = error; 884 #ifdef QUOTA 885 qsync(mp); 886 #endif 887 /* 888 * Write back modified superblock. 889 */ 890 if (fs->fs_fmod != 0) { 891 fs->fs_fmod = 0; 892 fs->fs_time = time.tv_sec; 893 if (error = ffs_sbupdate(ump, waitfor)) 894 allerror = error; 895 } 896 return (allerror); 897 } 898 899 /* 900 * Look up a FFS dinode number to find its incore vnode, otherwise read it 901 * in from disk. If it is in core, wait for the lock bit to clear, then 902 * return the inode locked. Detection and handling of mount points must be 903 * done by the calling routine. 904 */ 905 static int ffs_inode_hash_lock; 906 907 int 908 ffs_vget(mp, ino, vpp) 909 struct mount *mp; 910 ino_t ino; 911 struct vnode **vpp; 912 { 913 struct fs *fs; 914 struct inode *ip; 915 struct ufsmount *ump; 916 struct buf *bp; 917 struct vnode *vp; 918 dev_t dev; 919 int type, error; 920 921 ump = VFSTOUFS(mp); 922 dev = ump->um_dev; 923 restart: 924 if ((*vpp = ufs_ihashget(dev, ino)) != NULL) 925 return (0); 926 927 /* 928 * Lock out the creation of new entries in the FFS hash table in 929 * case getnewvnode() or MALLOC() blocks, otherwise a duplicate 930 * may occur! 931 */ 932 if (ffs_inode_hash_lock) { 933 while (ffs_inode_hash_lock) { 934 ffs_inode_hash_lock = -1; 935 tsleep(&ffs_inode_hash_lock, PVM, "ffsvgt", 0); 936 } 937 goto restart; 938 } 939 ffs_inode_hash_lock = 1; 940 941 /* 942 * If this MALLOC() is performed after the getnewvnode() 943 * it might block, leaving a vnode with a NULL v_data to be 944 * found by ffs_sync() if a sync happens to fire right then, 945 * which will cause a panic because ffs_sync() blindly 946 * dereferences vp->v_data (as well it should). 947 */ 948 type = ump->um_devvp->v_tag == VT_MFS ? M_MFSNODE : M_FFSNODE; /* XXX */ 949 MALLOC(ip, struct inode *, sizeof(struct inode), type, M_WAITOK); 950 951 /* Allocate a new vnode/inode. */ 952 error = getnewvnode(VT_UFS, mp, ffs_vnodeop_p, &vp); 953 if (error) { 954 if (ffs_inode_hash_lock < 0) 955 wakeup(&ffs_inode_hash_lock); 956 ffs_inode_hash_lock = 0; 957 *vpp = NULL; 958 FREE(ip, type); 959 return (error); 960 } 961 bzero((caddr_t)ip, sizeof(struct inode)); 962 lockinit(&ip->i_lock, PINOD, "inode", 0, 0); 963 vp->v_data = ip; 964 ip->i_vnode = vp; 965 ip->i_fs = fs = ump->um_fs; 966 ip->i_dev = dev; 967 ip->i_number = ino; 968 #ifdef QUOTA 969 { 970 int i; 971 for (i = 0; i < MAXQUOTAS; i++) 972 ip->i_dquot[i] = NODQUOT; 973 } 974 #endif 975 /* 976 * Put it onto its hash chain and lock it so that other requests for 977 * this inode will block if they arrive while we are sleeping waiting 978 * for old data structures to be purged or for the contents of the 979 * disk portion of this inode to be read. 980 */ 981 ufs_ihashins(ip); 982 983 if (ffs_inode_hash_lock < 0) 984 wakeup(&ffs_inode_hash_lock); 985 ffs_inode_hash_lock = 0; 986 987 /* Read in the disk contents for the inode, copy into the inode. */ 988 error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)), 989 (int)fs->fs_bsize, NOCRED, &bp); 990 if (error) { 991 /* 992 * The inode does not contain anything useful, so it would 993 * be misleading to leave it on its hash chain. With mode 994 * still zero, it will be unlinked and returned to the free 995 * list by vput(). 996 */ 997 brelse(bp); 998 vput(vp); 999 *vpp = NULL; 1000 return (error); 1001 } 1002 ip->i_din = *((struct dinode *)bp->b_data + ino_to_fsbo(fs, ino)); 1003 bqrelse(bp); 1004 1005 /* 1006 * Initialize the vnode from the inode, check for aliases. 1007 * Note that the underlying vnode may have changed. 1008 */ 1009 error = ufs_vinit(mp, ffs_specop_p, ffs_fifoop_p, &vp); 1010 if (error) { 1011 vput(vp); 1012 *vpp = NULL; 1013 return (error); 1014 } 1015 /* 1016 * Finish inode initialization now that aliasing has been resolved. 1017 */ 1018 ip->i_devvp = ump->um_devvp; 1019 VREF(ip->i_devvp); 1020 /* 1021 * Set up a generation number for this inode if it does not 1022 * already have one. This should only happen on old filesystems. 1023 */ 1024 if (ip->i_gen == 0) { 1025 ip->i_gen = random() / 2 + 1; 1026 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) 1027 ip->i_flag |= IN_MODIFIED; 1028 } 1029 /* 1030 * Ensure that uid and gid are correct. This is a temporary 1031 * fix until fsck has been changed to do the update. 1032 */ 1033 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */ 1034 ip->i_uid = ip->i_din.di_ouid; /* XXX */ 1035 ip->i_gid = ip->i_din.di_ogid; /* XXX */ 1036 } /* XXX */ 1037 1038 *vpp = vp; 1039 return (0); 1040 } 1041 1042 /* 1043 * File handle to vnode 1044 * 1045 * Have to be really careful about stale file handles: 1046 * - check that the inode number is valid 1047 * - call ffs_vget() to get the locked inode 1048 * - check for an unallocated inode (i_mode == 0) 1049 * - check that the given client host has export rights and return 1050 * those rights via. exflagsp and credanonp 1051 */ 1052 int 1053 ffs_fhtovp(mp, fhp, nam, vpp, exflagsp, credanonp) 1054 register struct mount *mp; 1055 struct fid *fhp; 1056 struct sockaddr *nam; 1057 struct vnode **vpp; 1058 int *exflagsp; 1059 struct ucred **credanonp; 1060 { 1061 register struct ufid *ufhp; 1062 struct fs *fs; 1063 1064 ufhp = (struct ufid *)fhp; 1065 fs = VFSTOUFS(mp)->um_fs; 1066 if (ufhp->ufid_ino < ROOTINO || 1067 ufhp->ufid_ino >= fs->fs_ncg * fs->fs_ipg) 1068 return (ESTALE); 1069 return (ufs_check_export(mp, ufhp, nam, vpp, exflagsp, credanonp)); 1070 } 1071 1072 /* 1073 * Vnode pointer to File handle 1074 */ 1075 /* ARGSUSED */ 1076 int 1077 ffs_vptofh(vp, fhp) 1078 struct vnode *vp; 1079 struct fid *fhp; 1080 { 1081 register struct inode *ip; 1082 register struct ufid *ufhp; 1083 1084 ip = VTOI(vp); 1085 ufhp = (struct ufid *)fhp; 1086 ufhp->ufid_len = sizeof(struct ufid); 1087 ufhp->ufid_ino = ip->i_number; 1088 ufhp->ufid_gen = ip->i_gen; 1089 return (0); 1090 } 1091 1092 /* 1093 * Initialize the filesystem; just use ufs_init. 1094 */ 1095 static int 1096 ffs_init(vfsp) 1097 struct vfsconf *vfsp; 1098 { 1099 1100 return (ufs_init(vfsp)); 1101 } 1102 1103 /* 1104 * Write a superblock and associated information back to disk. 1105 */ 1106 static int 1107 ffs_sbupdate(mp, waitfor) 1108 struct ufsmount *mp; 1109 int waitfor; 1110 { 1111 register struct fs *dfs, *fs = mp->um_fs; 1112 register struct buf *bp; 1113 int blks; 1114 caddr_t space; 1115 int i, size, error, allerror = 0; 1116 1117 /* 1118 * First write back the summary information. 1119 */ 1120 blks = howmany(fs->fs_cssize, fs->fs_fsize); 1121 space = (caddr_t)fs->fs_csp[0]; 1122 for (i = 0; i < blks; i += fs->fs_frag) { 1123 size = fs->fs_bsize; 1124 if (i + fs->fs_frag > blks) 1125 size = (blks - i) * fs->fs_fsize; 1126 bp = getblk(mp->um_devvp, fsbtodb(fs, fs->fs_csaddr + i), 1127 size, 0, 0); 1128 bcopy(space, bp->b_data, (u_int)size); 1129 space += size; 1130 if (waitfor != MNT_WAIT) 1131 bawrite(bp); 1132 else if (error = bwrite(bp)) 1133 allerror = error; 1134 } 1135 /* 1136 * Now write back the superblock itself. If any errors occurred 1137 * up to this point, then fail so that the superblock avoids 1138 * being written out as clean. 1139 */ 1140 if (allerror) 1141 return (allerror); 1142 bp = getblk(mp->um_devvp, SBLOCK, (int)fs->fs_sbsize, 0, 0); 1143 bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize); 1144 /* Restore compatibility to old file systems. XXX */ 1145 dfs = (struct fs *)bp->b_data; /* XXX */ 1146 if (fs->fs_postblformat == FS_42POSTBLFMT) /* XXX */ 1147 dfs->fs_nrpos = -1; /* XXX */ 1148 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */ 1149 int32_t *lp, tmp; /* XXX */ 1150 /* XXX */ 1151 lp = (int32_t *)&dfs->fs_qbmask; /* XXX */ 1152 tmp = lp[4]; /* XXX */ 1153 for (i = 4; i > 0; i--) /* XXX */ 1154 lp[i] = lp[i-1]; /* XXX */ 1155 lp[0] = tmp; /* XXX */ 1156 } /* XXX */ 1157 dfs->fs_maxfilesize = mp->um_savedmaxfilesize; /* XXX */ 1158 if (waitfor != MNT_WAIT) 1159 bawrite(bp); 1160 else if (error = bwrite(bp)) 1161 allerror = error; 1162 return (allerror); 1163 } 1164