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