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