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.65 (Berkeley) 05/14/92 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 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 main() 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), M_MOUNT, M_WAITOK); 66 mp->mnt_op = &ufs_vfsops; 67 mp->mnt_flag = MNT_RDONLY; 68 mp->mnt_mounth = NULLVP; 69 if (error = ffs_mountfs(rootvp, mp, p)) { 70 free(mp, M_MOUNT); 71 return (error); 72 } 73 if (error = vfs_lock(mp)) { 74 (void)ffs_unmount(mp, 0, p); 75 free(mp, M_MOUNT); 76 return (error); 77 } 78 rootfs = mp; 79 mp->mnt_next = mp; 80 mp->mnt_prev = mp; 81 mp->mnt_vnodecovered = NULLVP; 82 ump = VFSTOUFS(mp); 83 fs = ump->um_fs; 84 bzero(fs->fs_fsmnt, sizeof(fs->fs_fsmnt)); 85 fs->fs_fsmnt[0] = '/'; 86 bcopy((caddr_t)fs->fs_fsmnt, (caddr_t)mp->mnt_stat.f_mntonname, 87 MNAMELEN); 88 (void) copystr(ROOTNAME, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, 89 &size); 90 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size); 91 (void)ffs_statfs(mp, &mp->mnt_stat, p); 92 vfs_unlock(mp); 93 inittodr(fs->fs_time); 94 return (0); 95 } 96 97 /* 98 * VFS Operations. 99 * 100 * mount system call 101 */ 102 int 103 ffs_mount(mp, path, data, ndp, p) 104 register struct mount *mp; 105 char *path; 106 caddr_t data; 107 struct nameidata *ndp; 108 struct proc *p; 109 { 110 struct vnode *devvp; 111 struct ufs_args args; 112 struct ufsmount *ump; 113 register struct fs *fs; 114 u_int size; 115 int error; 116 117 if (error = copyin(data, (caddr_t)&args, sizeof (struct ufs_args))) 118 return (error); 119 /* 120 * If updating, check whether changing from read-only to 121 * read/write; if there is no device name, that's all we do. 122 */ 123 if (mp->mnt_flag & MNT_UPDATE) { 124 ump = VFSTOUFS(mp); 125 fs = ump->um_fs; 126 if (fs->fs_ronly && (mp->mnt_flag & MNT_RDONLY) == 0) 127 fs->fs_ronly = 0; 128 if (args.fspec == 0) { 129 /* 130 * Process export requests. 131 */ 132 if (args.exflags & MNT_EXPORTED) { 133 if (error = hang_addrlist(mp, &args)) 134 return (error); 135 mp->mnt_flag |= MNT_EXPORTED; 136 } 137 if (args.exflags & MNT_DELEXPORT) { 138 free_addrlist(ump); 139 mp->mnt_flag &= 140 ~(MNT_EXPORTED | MNT_DEFEXPORTED); 141 } 142 return (0); 143 } 144 } 145 /* 146 * Not an update, or updating the name: look up the name 147 * and verify that it refers to a sensible block device. 148 */ 149 NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p); 150 if (error = namei(ndp)) 151 return (error); 152 devvp = ndp->ni_vp; 153 154 if (devvp->v_type != VBLK) { 155 vrele(devvp); 156 return (ENOTBLK); 157 } 158 if (major(devvp->v_rdev) >= nblkdev) { 159 vrele(devvp); 160 return (ENXIO); 161 } 162 if ((mp->mnt_flag & MNT_UPDATE) == 0) 163 error = ffs_mountfs(devvp, mp, p); 164 else { 165 if (devvp != ump->um_devvp) 166 error = EINVAL; /* needs translation */ 167 else 168 vrele(devvp); 169 } 170 if (error) { 171 vrele(devvp); 172 return (error); 173 } 174 ump = VFSTOUFS(mp); 175 fs = ump->um_fs; 176 (void) copyinstr(path, fs->fs_fsmnt, sizeof(fs->fs_fsmnt) - 1, &size); 177 bzero(fs->fs_fsmnt + size, sizeof(fs->fs_fsmnt) - size); 178 bcopy((caddr_t)fs->fs_fsmnt, (caddr_t)mp->mnt_stat.f_mntonname, 179 MNAMELEN); 180 (void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, 181 &size); 182 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size); 183 (void)ffs_statfs(mp, &mp->mnt_stat, p); 184 return (0); 185 } 186 187 /* 188 * Common code for mount and mountroot 189 */ 190 int 191 ffs_mountfs(devvp, mp, p) 192 register struct vnode *devvp; 193 struct mount *mp; 194 struct proc *p; 195 { 196 USES_VOP_CLOSE; 197 USES_VOP_IOCTL; 198 USES_VOP_OPEN; 199 register struct ufsmount *ump = (struct ufsmount *)0; 200 struct buf *bp = NULL; 201 register struct fs *fs; 202 dev_t dev = devvp->v_rdev; 203 struct partinfo dpart; 204 caddr_t base, space; 205 int havepart = 0, blks; 206 int error, i, size; 207 int needclose = 0; 208 int ronly = (mp->mnt_flag & MNT_RDONLY) != 0; 209 extern struct vnode *rootvp; 210 211 /* 212 * Disallow multiple mounts of the same device. 213 * Disallow mounting of a device that is currently in use 214 * (except for root, which might share swap device for miniroot). 215 * Flush out any old buffers remaining from a previous use. 216 */ 217 if (error = ufs_mountedon(devvp)) 218 return (error); 219 if (vcount(devvp) > 1 && devvp != rootvp) 220 return (EBUSY); 221 vinvalbuf(devvp, 1); 222 if (error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, NOCRED, p)) 223 return (error); 224 needclose = 1; 225 if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, NOCRED, p) != 0) 226 size = DEV_BSIZE; 227 else { 228 havepart = 1; 229 size = dpart.disklab->d_secsize; 230 } 231 if (error = bread(devvp, SBLOCK, SBSIZE, NOCRED, &bp)) 232 goto out; 233 fs = bp->b_un.b_fs; 234 if (fs->fs_magic != FS_MAGIC || fs->fs_bsize > MAXBSIZE || 235 fs->fs_bsize < sizeof(struct fs)) { 236 error = EINVAL; /* XXX needs translation */ 237 goto out; 238 } 239 ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK); 240 bzero((caddr_t)ump, sizeof *ump); 241 ump->um_fs = malloc((u_long)fs->fs_sbsize, M_UFSMNT, 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_UFSMNT, 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_UFSMNT); 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_UFSMNT); 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 USES_VOP_CLOSE; 318 extern int doforce; 319 register struct ufsmount *ump; 320 register struct fs *fs; 321 int i, error, ronly, flags = 0; 322 323 if (mntflags & MNT_FORCE) { 324 if (!doforce || mp == rootfs) 325 return (EINVAL); 326 flags |= FORCECLOSE; 327 } 328 mntflushbuf(mp, 0); 329 if (mntinvalbuf(mp)) 330 return (EBUSY); 331 ump = VFSTOUFS(mp); 332 #ifdef QUOTA 333 if (mp->mnt_flag & MNT_QUOTA) { 334 if (error = vflush(mp, NULLVP, SKIPSYSTEM|flags)) 335 return (error); 336 for (i = 0; i < MAXQUOTAS; i++) { 337 if (ump->um_quotas[i] == NULLVP) 338 continue; 339 quotaoff(p, mp, i); 340 } 341 /* 342 * Here we fall through to vflush again to ensure 343 * that we have gotten rid of all the system vnodes. 344 */ 345 } 346 #endif 347 if (error = vflush(mp, NULLVP, flags)) 348 return (error); 349 fs = ump->um_fs; 350 ronly = !fs->fs_ronly; 351 ump->um_devvp->v_specflags &= ~SI_MOUNTEDON; 352 error = VOP_CLOSE(ump->um_devvp, ronly ? FREAD : FREAD|FWRITE, 353 NOCRED, p); 354 vrele(ump->um_devvp); 355 free(fs->fs_csp[0], M_UFSMNT); 356 free(fs, M_UFSMNT); 357 free(ump, M_UFSMNT); 358 mp->mnt_data = (qaddr_t)0; 359 mp->mnt_flag &= ~MNT_LOCAL; 360 return (error); 361 } 362 363 /* 364 * Return root of a filesystem 365 */ 366 int 367 ffs_root(mp, vpp) 368 struct mount *mp; 369 struct vnode **vpp; 370 { 371 USES_VOP_VGET; 372 struct vnode *nvp; 373 int error; 374 375 if (error = FFS_VGET(mp, (ino_t)ROOTINO, &nvp)) 376 return (error); 377 *vpp = nvp; 378 return (0); 379 } 380 381 /* 382 * Get file system statistics. 383 */ 384 int 385 ffs_statfs(mp, sbp, p) 386 struct mount *mp; 387 register struct statfs *sbp; 388 struct proc *p; 389 { 390 register struct ufsmount *ump; 391 register struct fs *fs; 392 393 ump = VFSTOUFS(mp); 394 fs = ump->um_fs; 395 if (fs->fs_magic != FS_MAGIC) 396 panic("ffs_statfs"); 397 sbp->f_type = MOUNT_UFS; 398 sbp->f_bsize = fs->fs_fsize; 399 sbp->f_iosize = fs->fs_bsize; 400 sbp->f_blocks = fs->fs_dsize; 401 sbp->f_bfree = fs->fs_cstotal.cs_nbfree * fs->fs_frag + 402 fs->fs_cstotal.cs_nffree; 403 sbp->f_bavail = (fs->fs_dsize * (100 - fs->fs_minfree) / 100) - 404 (fs->fs_dsize - sbp->f_bfree); 405 sbp->f_files = fs->fs_ncg * fs->fs_ipg - ROOTINO; 406 sbp->f_ffree = fs->fs_cstotal.cs_nifree; 407 if (sbp != &mp->mnt_stat) { 408 bcopy((caddr_t)mp->mnt_stat.f_mntonname, 409 (caddr_t)&sbp->f_mntonname[0], MNAMELEN); 410 bcopy((caddr_t)mp->mnt_stat.f_mntfromname, 411 (caddr_t)&sbp->f_mntfromname[0], MNAMELEN); 412 } 413 return (0); 414 } 415 416 /* 417 * Go through the disk queues to initiate sandbagged IO; 418 * go through the inodes to write those that have been modified; 419 * initiate the writing of the super block if it has been modified. 420 * 421 * Note: we are always called with the filesystem marked `MPBUSY'. 422 */ 423 int 424 ffs_sync(mp, waitfor) 425 struct mount *mp; 426 int waitfor; 427 { 428 USES_VOP_ISLOCKED; 429 USES_VOP_UPDATE; 430 extern int syncprt; 431 register struct vnode *vp; 432 register struct inode *ip; 433 register struct ufsmount *ump = VFSTOUFS(mp); 434 register struct fs *fs; 435 int error, allerror = 0; 436 437 if (syncprt) 438 ufs_bufstats(); 439 fs = ump->um_fs; 440 /* 441 * Write back modified superblock. 442 * Consistency check that the superblock 443 * is still in the buffer cache. 444 */ 445 if (fs->fs_fmod != 0) { 446 if (fs->fs_ronly != 0) { /* XXX */ 447 printf("fs = %s\n", fs->fs_fsmnt); 448 panic("update: rofs mod"); 449 } 450 fs->fs_fmod = 0; 451 fs->fs_time = time.tv_sec; 452 allerror = ffs_sbupdate(ump, waitfor); 453 } 454 /* 455 * Write back each (modified) inode. 456 */ 457 loop: 458 for (vp = mp->mnt_mounth; vp; vp = vp->v_mountf) { 459 /* 460 * If the vnode that we are about to sync is no longer 461 * associated with this mount point, start over. 462 */ 463 if (vp->v_mount != mp) 464 goto loop; 465 if (VOP_ISLOCKED(vp)) 466 continue; 467 ip = VTOI(vp); 468 if ((ip->i_flag & (IMOD|IACC|IUPD|ICHG)) == 0 && 469 vp->v_dirtyblkhd == NULL) 470 continue; 471 if (vget(vp)) 472 goto loop; 473 if (vp->v_dirtyblkhd) 474 vflushbuf(vp, 0); 475 if ((ip->i_flag & (IMOD|IACC|IUPD|ICHG)) && 476 (error = VOP_UPDATE(vp, &time, &time, 0))) 477 allerror = error; 478 vput(vp); 479 } 480 /* 481 * Force stale file system control information to be flushed. 482 */ 483 vflushbuf(ump->um_devvp, waitfor == MNT_WAIT ? B_SYNC : 0); 484 #ifdef QUOTA 485 qsync(mp); 486 #endif 487 return (allerror); 488 } 489 490 /* 491 * File handle to vnode 492 * 493 * Have to be really careful about stale file handles: 494 * - check that the inode number is valid 495 * - call ffs_vget() to get the locked inode 496 * - check for an unallocated inode (i_mode == 0) 497 * - check that the generation number matches unless setgen true 498 */ 499 int 500 ffs_fhtovp(mp, fhp, setgen, vpp) 501 register struct mount *mp; 502 struct fid *fhp; 503 int setgen; 504 struct vnode **vpp; 505 { 506 USES_VOP_VGET; 507 register struct inode *ip; 508 register struct ufid *ufhp; 509 struct fs *fs; 510 struct vnode *nvp; 511 int error; 512 513 ufhp = (struct ufid *)fhp; 514 fs = VFSTOUFS(mp)->um_fs; 515 if (ufhp->ufid_ino < ROOTINO || 516 ufhp->ufid_ino >= fs->fs_ncg * fs->fs_ipg) 517 return (EINVAL); 518 if (error = FFS_VGET(mp, ufhp->ufid_ino, &nvp)) { 519 *vpp = NULLVP; 520 return (error); 521 } 522 ip = VTOI(nvp); 523 if (ip->i_mode == 0) { 524 ufs_iput(ip); 525 *vpp = NULLVP; 526 return (EINVAL); 527 } 528 if (ip->i_gen != ufhp->ufid_gen) { 529 if (setgen) 530 ufhp->ufid_gen = ip->i_gen; 531 else { 532 ufs_iput(ip); 533 *vpp = NULLVP; 534 return (EINVAL); 535 } 536 } 537 *vpp = nvp; 538 return (0); 539 } 540 541 /* 542 * Vnode pointer to File handle 543 */ 544 /* ARGSUSED */ 545 ffs_vptofh(vp, fhp) 546 struct vnode *vp; 547 struct fid *fhp; 548 { 549 register struct inode *ip; 550 register struct ufid *ufhp; 551 552 ip = VTOI(vp); 553 ufhp = (struct ufid *)fhp; 554 ufhp->ufid_len = sizeof(struct ufid); 555 ufhp->ufid_ino = ip->i_number; 556 ufhp->ufid_gen = ip->i_gen; 557 return (0); 558 } 559 560 /* 561 * Write a superblock and associated information back to disk. 562 */ 563 int 564 ffs_sbupdate(mp, waitfor) 565 struct ufsmount *mp; 566 int waitfor; 567 { 568 register struct fs *fs = mp->um_fs; 569 register struct buf *bp; 570 int blks; 571 caddr_t space; 572 int i, size, error = 0; 573 574 bp = getblk(mp->um_devvp, SBLOCK, (int)fs->fs_sbsize); 575 bcopy((caddr_t)fs, bp->b_un.b_addr, (u_int)fs->fs_sbsize); 576 /* Restore compatibility to old file systems. XXX */ 577 if (fs->fs_postblformat == FS_42POSTBLFMT) /* XXX */ 578 bp->b_un.b_fs->fs_nrpos = -1; /* XXX */ 579 if (waitfor == MNT_WAIT) 580 error = bwrite(bp); 581 else 582 bawrite(bp); 583 blks = howmany(fs->fs_cssize, fs->fs_fsize); 584 space = (caddr_t)fs->fs_csp[0]; 585 for (i = 0; i < blks; i += fs->fs_frag) { 586 size = fs->fs_bsize; 587 if (i + fs->fs_frag > blks) 588 size = (blks - i) * fs->fs_fsize; 589 bp = getblk(mp->um_devvp, fsbtodb(fs, fs->fs_csaddr + i), size); 590 bcopy(space, bp->b_un.b_addr, (u_int)size); 591 space += size; 592 if (waitfor == MNT_WAIT) 593 error = bwrite(bp); 594 else 595 bawrite(bp); 596 } 597 return (error); 598 } 599