1 /* 2 * Copyright (c) 1989, 1991 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 * 7 * @(#)ffs_vfsops.c 7.58 (Berkeley) 11/05/91 8 */ 9 10 #include <sys/param.h> 11 #include <sys/systm.h> 12 #include <sys/namei.h> 13 #include <sys/proc.h> 14 #include <sys/kernel.h> 15 #include <sys/vnode.h> 16 #include <sys/specdev.h> 17 #include <sys/mount.h> 18 #include <sys/buf.h> 19 #include <sys/file.h> 20 #include <sys/disklabel.h> 21 #include <sys/ioctl.h> 22 #include <sys/errno.h> 23 #include <sys/malloc.h> 24 25 #include <ufs/ufs/quota.h> 26 #include <ufs/ufs/ufsmount.h> 27 #include <ufs/ufs/inode.h> 28 #include <ufs/ufs/ufs_extern.h> 29 30 #include <ufs/ffs/fs.h> 31 #include <ufs/ffs/ffs_extern.h> 32 33 static int ffs_sbupdate __P((struct ufsmount *, int)); 34 35 struct vfsops ufs_vfsops = { 36 ffs_mount, 37 ufs_start, 38 ffs_unmount, 39 ffs_root, 40 ufs_quotactl, 41 ffs_statfs, 42 ffs_sync, 43 ffs_fhtovp, 44 ffs_vptofh, 45 ffs_init, 46 }; 47 48 /* 49 * Called by vfs_mountroot when ufs is going to be mounted as root. 50 * 51 * Name is updated by mount(8) after booting. 52 */ 53 #define ROOTNAME "root_device" 54 55 ffs_mountroot() 56 { 57 extern struct vnode *rootvp; 58 register struct fs *fs; 59 register struct mount *mp; 60 struct proc *p = curproc; /* XXX */ 61 struct ufsmount *ump; 62 u_int size; 63 int error; 64 65 mp = malloc((u_long)sizeof(struct mount), 66 M_MOUNT, M_WAITOK); 67 mp->mnt_op = &ufs_vfsops; 68 mp->mnt_flag = MNT_RDONLY; 69 mp->mnt_exroot = 0; 70 mp->mnt_mounth = NULLVP; 71 if (error = ffs_mountfs(rootvp, mp, p)) { 72 free(mp, M_MOUNT); 73 return (error); 74 } 75 if (error = vfs_lock(mp)) { 76 (void)ffs_unmount(mp, 0, p); 77 free(mp, M_MOUNT); 78 return (error); 79 } 80 rootfs = mp; 81 mp->mnt_next = mp; 82 mp->mnt_prev = mp; 83 mp->mnt_vnodecovered = NULLVP; 84 ump = VFSTOUFS(mp); 85 fs = ump->um_fs; 86 bzero(fs->fs_fsmnt, sizeof(fs->fs_fsmnt)); 87 fs->fs_fsmnt[0] = '/'; 88 bcopy((caddr_t)fs->fs_fsmnt, (caddr_t)mp->mnt_stat.f_mntonname, 89 MNAMELEN); 90 (void) copystr(ROOTNAME, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, 91 &size); 92 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size); 93 (void)ffs_statfs(mp, &mp->mnt_stat, p); 94 vfs_unlock(mp); 95 inittodr(fs->fs_time); 96 return (0); 97 } 98 99 /* 100 * VFS Operations. 101 * 102 * mount system call 103 */ 104 static int 105 ffs_mount(mp, path, data, ndp, p) 106 register struct mount *mp; 107 char *path; 108 caddr_t data; 109 struct nameidata *ndp; 110 struct proc *p; 111 { 112 struct vnode *devvp; 113 struct ufs_args args; 114 struct ufsmount *ump; 115 register struct fs *fs; 116 u_int size; 117 int error; 118 119 if (error = copyin(data, (caddr_t)&args, sizeof (struct ufs_args))) 120 return (error); 121 /* 122 * Process export requests. 123 */ 124 if ((args.exflags & MNT_EXPORTED) || (mp->mnt_flag & MNT_EXPORTED)) { 125 if (args.exflags & MNT_EXPORTED) 126 mp->mnt_flag |= MNT_EXPORTED; 127 else 128 mp->mnt_flag &= ~MNT_EXPORTED; 129 if (args.exflags & MNT_EXRDONLY) 130 mp->mnt_flag |= MNT_EXRDONLY; 131 else 132 mp->mnt_flag &= ~MNT_EXRDONLY; 133 mp->mnt_exroot = args.exroot; 134 } 135 /* 136 * If updating, check whether changing from read-only to 137 * read/write; if there is no device name, that's all we do. 138 */ 139 if (mp->mnt_flag & MNT_UPDATE) { 140 ump = VFSTOUFS(mp); 141 fs = ump->um_fs; 142 if (fs->fs_ronly && (mp->mnt_flag & MNT_RDONLY) == 0) 143 fs->fs_ronly = 0; 144 if (args.fspec == 0) 145 return (0); 146 } 147 /* 148 * Not an update, or updating the name: look up the name 149 * and verify that it refers to a sensible block device. 150 */ 151 ndp->ni_nameiop = LOOKUP | FOLLOW; 152 ndp->ni_segflg = UIO_USERSPACE; 153 ndp->ni_dirp = args.fspec; 154 if (error = namei(ndp, p)) 155 return (error); 156 devvp = ndp->ni_vp; 157 158 if (devvp->v_type != VBLK) { 159 vrele(devvp); 160 return (ENOTBLK); 161 } 162 if (major(devvp->v_rdev) >= nblkdev) { 163 vrele(devvp); 164 return (ENXIO); 165 } 166 if ((mp->mnt_flag & MNT_UPDATE) == 0) 167 error = ffs_mountfs(devvp, mp, p); 168 else { 169 if (devvp != ump->um_devvp) 170 error = EINVAL; /* needs translation */ 171 else 172 vrele(devvp); 173 } 174 if (error) { 175 vrele(devvp); 176 return (error); 177 } 178 ump = VFSTOUFS(mp); 179 fs = ump->um_fs; 180 (void) copyinstr(path, fs->fs_fsmnt, sizeof(fs->fs_fsmnt) - 1, &size); 181 bzero(fs->fs_fsmnt + size, sizeof(fs->fs_fsmnt) - size); 182 bcopy((caddr_t)fs->fs_fsmnt, (caddr_t)mp->mnt_stat.f_mntonname, 183 MNAMELEN); 184 (void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, 185 &size); 186 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size); 187 (void)ffs_statfs(mp, &mp->mnt_stat, p); 188 return (0); 189 } 190 191 /* 192 * Common code for mount and mountroot 193 */ 194 int 195 ffs_mountfs(devvp, mp, p) 196 register struct vnode *devvp; 197 struct mount *mp; 198 struct proc *p; 199 { 200 register struct ufsmount *ump = (struct ufsmount *)0; 201 struct buf *bp = NULL; 202 register struct fs *fs; 203 dev_t dev = devvp->v_rdev; 204 struct partinfo dpart; 205 caddr_t base, space; 206 int havepart = 0, blks; 207 int error, i, size; 208 int needclose = 0; 209 int ronly = (mp->mnt_flag & MNT_RDONLY) != 0; 210 extern struct vnode *rootvp; 211 212 /* 213 * Disallow multiple mounts of the same device. 214 * Disallow mounting of a device that is currently in use 215 * (except for root, which might share swap device for miniroot). 216 * Flush out any old buffers remaining from a previous use. 217 */ 218 if (error = ufs_mountedon(devvp)) 219 return (error); 220 if (vcount(devvp) > 1 && devvp != rootvp) 221 return (EBUSY); 222 vinvalbuf(devvp, 1); 223 if (error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, NOCRED, p)) 224 return (error); 225 needclose = 1; 226 if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, NOCRED, p) != 0) 227 size = DEV_BSIZE; 228 else { 229 havepart = 1; 230 size = dpart.disklab->d_secsize; 231 } 232 if (error = bread(devvp, SBLOCK, SBSIZE, NOCRED, &bp)) 233 goto out; 234 fs = bp->b_un.b_fs; 235 if (fs->fs_magic != FS_MAGIC || fs->fs_bsize > MAXBSIZE || 236 fs->fs_bsize < sizeof(struct fs)) { 237 error = EINVAL; /* XXX needs translation */ 238 goto out; 239 } 240 ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK); 241 ump->um_fs = malloc((u_long)fs->fs_sbsize, M_SUPERBLK, 242 M_WAITOK); 243 bcopy((caddr_t)bp->b_un.b_addr, (caddr_t)ump->um_fs, 244 (u_int)fs->fs_sbsize); 245 if (fs->fs_sbsize < SBSIZE) 246 bp->b_flags |= B_INVAL; 247 brelse(bp); 248 bp = NULL; 249 fs = ump->um_fs; 250 fs->fs_ronly = ronly; 251 if (ronly == 0) 252 fs->fs_fmod = 1; 253 if (havepart) { 254 dpart.part->p_fstype = FS_BSDFFS; 255 dpart.part->p_fsize = fs->fs_fsize; 256 dpart.part->p_frag = fs->fs_frag; 257 dpart.part->p_cpg = fs->fs_cpg; 258 } 259 blks = howmany(fs->fs_cssize, fs->fs_fsize); 260 base = space = malloc((u_long)fs->fs_cssize, M_SUPERBLK, 261 M_WAITOK); 262 for (i = 0; i < blks; i += fs->fs_frag) { 263 size = fs->fs_bsize; 264 if (i + fs->fs_frag > blks) 265 size = (blks - i) * fs->fs_fsize; 266 error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size, 267 NOCRED, &bp); 268 if (error) { 269 free(base, M_SUPERBLK); 270 goto out; 271 } 272 bcopy((caddr_t)bp->b_un.b_addr, space, (u_int)size); 273 fs->fs_csp[fragstoblks(fs, i)] = (struct csum *)space; 274 space += size; 275 brelse(bp); 276 bp = NULL; 277 } 278 mp->mnt_data = (qaddr_t)ump; 279 mp->mnt_stat.f_fsid.val[0] = (long)dev; 280 mp->mnt_stat.f_fsid.val[1] = MOUNT_UFS; 281 mp->mnt_flag |= MNT_LOCAL; 282 ump->um_mountp = mp; 283 ump->um_dev = dev; 284 ump->um_devvp = devvp; 285 for (i = 0; i < MAXQUOTAS; i++) 286 ump->um_quotas[i] = NULLVP; 287 devvp->v_specflags |= SI_MOUNTEDON; 288 289 /* Sanity checks for old file systems. XXX */ 290 fs->fs_npsect = MAX(fs->fs_npsect, fs->fs_nsect); /* XXX */ 291 fs->fs_interleave = MAX(fs->fs_interleave, 1); /* XXX */ 292 if (fs->fs_postblformat == FS_42POSTBLFMT) /* XXX */ 293 fs->fs_nrpos = 8; /* XXX */ 294 return (0); 295 out: 296 if (bp) 297 brelse(bp); 298 if (needclose) 299 (void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, NOCRED, p); 300 if (ump) { 301 free(ump->um_fs, M_SUPERBLK); 302 free(ump, M_UFSMNT); 303 mp->mnt_data = (qaddr_t)0; 304 } 305 return (error); 306 } 307 308 /* 309 * unmount system call 310 */ 311 int 312 ffs_unmount(mp, mntflags, p) 313 struct mount *mp; 314 int mntflags; 315 struct proc *p; 316 { 317 extern int doforce; 318 register struct ufsmount *ump; 319 register struct fs *fs; 320 int i, error, ronly, flags = 0; 321 322 if (mntflags & MNT_FORCE) { 323 if (!doforce || mp == rootfs) 324 return (EINVAL); 325 flags |= FORCECLOSE; 326 } 327 mntflushbuf(mp, 0); 328 if (mntinvalbuf(mp)) 329 return (EBUSY); 330 ump = VFSTOUFS(mp); 331 #ifdef QUOTA 332 if (mp->mnt_flag & MNT_QUOTA) { 333 if (error = vflush(mp, NULLVP, SKIPSYSTEM|flags)) 334 return (error); 335 for (i = 0; i < MAXQUOTAS; i++) { 336 if (ump->um_quotas[i] == NULLVP) 337 continue; 338 quotaoff(p, mp, i); 339 } 340 /* 341 * Here we fall through to vflush again to ensure 342 * that we have gotten rid of all the system vnodes. 343 */ 344 } 345 #endif 346 if (error = vflush(mp, NULLVP, flags)) 347 return (error); 348 fs = ump->um_fs; 349 ronly = !fs->fs_ronly; 350 ump->um_devvp->v_specflags &= ~SI_MOUNTEDON; 351 error = VOP_CLOSE(ump->um_devvp, ronly ? FREAD : FREAD|FWRITE, 352 NOCRED, p); 353 vrele(ump->um_devvp); 354 free(fs->fs_csp[0], M_SUPERBLK); 355 free(fs, M_SUPERBLK); 356 free(ump, M_UFSMNT); 357 mp->mnt_data = (qaddr_t)0; 358 mp->mnt_flag &= ~MNT_LOCAL; 359 return (error); 360 } 361 362 /* 363 * Return root of a filesystem 364 */ 365 int 366 ffs_root(mp, vpp) 367 struct mount *mp; 368 struct vnode **vpp; 369 { 370 struct vnode *nvp; 371 int error; 372 373 if (error = ffs_vget(mp, (ino_t)ROOTINO, &nvp)) 374 return (error); 375 *vpp = nvp; 376 return (0); 377 } 378 379 /* 380 * Get file system statistics. 381 */ 382 int 383 ffs_statfs(mp, sbp, p) 384 struct mount *mp; 385 register struct statfs *sbp; 386 struct proc *p; 387 { 388 register struct ufsmount *ump; 389 register struct fs *fs; 390 391 ump = VFSTOUFS(mp); 392 fs = ump->um_fs; 393 if (fs->fs_magic != FS_MAGIC) 394 panic("ffs_statfs"); 395 sbp->f_type = MOUNT_UFS; 396 sbp->f_fsize = fs->fs_fsize; 397 sbp->f_bsize = fs->fs_bsize; 398 sbp->f_blocks = fs->fs_dsize; 399 sbp->f_bfree = fs->fs_cstotal.cs_nbfree * fs->fs_frag + 400 fs->fs_cstotal.cs_nffree; 401 sbp->f_bavail = (fs->fs_dsize * (100 - fs->fs_minfree) / 100) - 402 (fs->fs_dsize - sbp->f_bfree); 403 sbp->f_files = fs->fs_ncg * fs->fs_ipg - ROOTINO; 404 sbp->f_ffree = fs->fs_cstotal.cs_nifree; 405 if (sbp != &mp->mnt_stat) { 406 bcopy((caddr_t)mp->mnt_stat.f_mntonname, 407 (caddr_t)&sbp->f_mntonname[0], MNAMELEN); 408 bcopy((caddr_t)mp->mnt_stat.f_mntfromname, 409 (caddr_t)&sbp->f_mntfromname[0], MNAMELEN); 410 } 411 return (0); 412 } 413 414 /* 415 * Go through the disk queues to initiate sandbagged IO; 416 * go through the inodes to write those that have been modified; 417 * initiate the writing of the super block if it has been modified. 418 * 419 * Note: we are always called with the filesystem marked `MPBUSY'. 420 */ 421 int 422 ffs_sync(mp, waitfor) 423 struct mount *mp; 424 int waitfor; 425 { 426 extern int syncprt; 427 register struct vnode *vp; 428 register struct inode *ip; 429 register struct ufsmount *ump = VFSTOUFS(mp); 430 register struct fs *fs; 431 int error, allerror = 0; 432 433 if (syncprt) 434 ufs_bufstats(); 435 fs = ump->um_fs; 436 /* 437 * Write back modified superblock. 438 * Consistency check that the superblock 439 * is still in the buffer cache. 440 */ 441 if (fs->fs_fmod != 0) { 442 if (fs->fs_ronly != 0) { /* XXX */ 443 printf("fs = %s\n", fs->fs_fsmnt); 444 panic("update: rofs mod"); 445 } 446 fs->fs_fmod = 0; 447 fs->fs_time = time.tv_sec; 448 allerror = ffs_sbupdate(ump, waitfor); 449 } 450 /* 451 * Write back each (modified) inode. 452 */ 453 loop: 454 for (vp = mp->mnt_mounth; vp; vp = vp->v_mountf) { 455 /* 456 * If the vnode that we are about to sync is no longer 457 * associated with this mount point, start over. 458 */ 459 if (vp->v_mount != mp) 460 goto loop; 461 if (VOP_ISLOCKED(vp)) 462 continue; 463 ip = VTOI(vp); 464 if ((ip->i_flag & (IMOD|IACC|IUPD|ICHG)) == 0 && 465 vp->v_dirtyblkhd == NULL) 466 continue; 467 if (vget(vp)) 468 goto loop; 469 if (vp->v_dirtyblkhd) 470 vflushbuf(vp, 0); 471 if ((ip->i_flag & (IMOD|IACC|IUPD|ICHG)) && 472 (error = ffs_update(vp, &time, &time, 0))) 473 allerror = error; 474 vput(vp); 475 } 476 /* 477 * Force stale file system control information to be flushed. 478 */ 479 vflushbuf(ump->um_devvp, waitfor == MNT_WAIT ? B_SYNC : 0); 480 #ifdef QUOTA 481 qsync(mp); 482 #endif 483 return (allerror); 484 } 485 486 /* 487 * File handle to vnode 488 * 489 * Have to be really careful about stale file handles: 490 * - check that the inode number is valid 491 * - call ffs_vget() to get the locked inode 492 * - check for an unallocated inode (i_mode == 0) 493 * - check that the generation number matches 494 */ 495 int 496 ffs_fhtovp(mp, fhp, vpp) 497 register struct mount *mp; 498 struct fid *fhp; 499 struct vnode **vpp; 500 { 501 register struct inode *ip; 502 register struct ufid *ufhp; 503 struct fs *fs; 504 struct vnode *nvp; 505 int error; 506 507 ufhp = (struct ufid *)fhp; 508 fs = VFSTOUFS(mp)->um_fs; 509 if (ufhp->ufid_ino < ROOTINO || 510 ufhp->ufid_ino >= fs->fs_ncg * fs->fs_ipg) 511 return (EINVAL); 512 if (error = ffs_vget(mp, ufhp->ufid_ino, &nvp)) { 513 *vpp = NULLVP; 514 return (error); 515 } 516 ip = VTOI(nvp); 517 if (ip->i_mode == 0) { 518 ufs_iput(ip); 519 *vpp = NULLVP; 520 return (EINVAL); 521 } 522 if (ip->i_gen != ufhp->ufid_gen) { 523 ufs_iput(ip); 524 *vpp = NULLVP; 525 return (EINVAL); 526 } 527 *vpp = nvp; 528 return (0); 529 } 530 531 /* 532 * Vnode pointer to File handle 533 */ 534 /* ARGSUSED */ 535 ffs_vptofh(vp, fhp) 536 struct vnode *vp; 537 struct fid *fhp; 538 { 539 register struct inode *ip; 540 register struct ufid *ufhp; 541 542 ip = VTOI(vp); 543 ufhp = (struct ufid *)fhp; 544 ufhp->ufid_len = sizeof(struct ufid); 545 ufhp->ufid_ino = ip->i_number; 546 ufhp->ufid_gen = ip->i_gen; 547 return (0); 548 } 549 550 /* 551 * Write a superblock and associated information back to disk. 552 */ 553 static int 554 ffs_sbupdate(mp, waitfor) 555 struct ufsmount *mp; 556 int waitfor; 557 { 558 register struct fs *fs = mp->um_fs; 559 register struct buf *bp; 560 int blks; 561 caddr_t space; 562 int i, size, error = 0; 563 564 bp = getblk(mp->um_devvp, SBLOCK, (int)fs->fs_sbsize); 565 bcopy((caddr_t)fs, bp->b_un.b_addr, (u_int)fs->fs_sbsize); 566 /* Restore compatibility to old file systems. XXX */ 567 if (fs->fs_postblformat == FS_42POSTBLFMT) /* XXX */ 568 bp->b_un.b_fs->fs_nrpos = -1; /* XXX */ 569 if (waitfor == MNT_WAIT) 570 error = bwrite(bp); 571 else 572 bawrite(bp); 573 blks = howmany(fs->fs_cssize, fs->fs_fsize); 574 space = (caddr_t)fs->fs_csp[0]; 575 for (i = 0; i < blks; i += fs->fs_frag) { 576 size = fs->fs_bsize; 577 if (i + fs->fs_frag > blks) 578 size = (blks - i) * fs->fs_fsize; 579 bp = getblk(mp->um_devvp, fsbtodb(fs, fs->fs_csaddr + i), size); 580 bcopy(space, bp->b_un.b_addr, (u_int)size); 581 space += size; 582 if (waitfor == MNT_WAIT) 583 error = bwrite(bp); 584 else 585 bawrite(bp); 586 } 587 return (error); 588 } 589