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