| 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 | * @(#)lfs_vfsops.c 7.88 (Berkeley) %G% |
| 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/mount.h> |
| 17 | #include <sys/buf.h> |
| 18 | #include <sys/mbuf.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 | #include <sys/socket.h> |
| 25 | |
| 26 | #include <miscfs/specfs/specdev.h> |
| 27 | #include "ioctl.h" |
| 28 | #include "disklabel.h" |
| 29 | #include "stat.h" |
| 30 | |
| 31 | #include <ufs/ufs/quota.h> |
| 32 | #include <ufs/ufs/inode.h> |
| 33 | #include <ufs/ufs/ufsmount.h> |
| 34 | #include <ufs/ufs/ufs_extern.h> |
| 35 | |
| 36 | #include <ufs/lfs/lfs.h> |
| 37 | #include <ufs/lfs/lfs_extern.h> |
| 38 | |
| 39 | int lfs_mountfs __P((struct vnode *, struct mount *, struct proc *)); |
| 40 | |
| 41 | struct vfsops lfs_vfsops = { |
| 42 | lfs_mount, |
| 43 | ufs_start, |
| 44 | lfs_unmount, |
| 45 | lfs_root, |
| 46 | ufs_quotactl, |
| 47 | lfs_statfs, |
| 48 | lfs_sync, |
| 49 | lfs_vget, |
| 50 | lfs_fhtovp, |
| 51 | lfs_vptofh, |
| 52 | lfs_init, |
| 53 | }; |
| 54 | |
| 55 | int |
| 56 | lfs_mountroot() |
| 57 | { |
| 58 | panic("lfs_mountroot"); /* XXX -- implement */ |
| 59 | } |
| 60 | |
| 61 | /* |
| 62 | * VFS Operations. |
| 63 | * |
| 64 | * mount system call |
| 65 | */ |
| 66 | lfs_mount(mp, path, data, ndp, p) |
| 67 | register struct mount *mp; |
| 68 | char *path; |
| 69 | caddr_t data; |
| 70 | struct nameidata *ndp; |
| 71 | struct proc *p; |
| 72 | { |
| 73 | struct vnode *devvp; |
| 74 | struct ufs_args args; |
| 75 | struct ufsmount *ump; |
| 76 | register struct lfs *fs; /* LFS */ |
| 77 | u_int size; |
| 78 | int error; |
| 79 | |
| 80 | if (error = copyin(data, (caddr_t)&args, sizeof (struct ufs_args))) |
| 81 | return (error); |
| 82 | |
| 83 | /* Until LFS can do NFS right. XXX */ |
| 84 | if (args.exflags & MNT_EXPORTED) |
| 85 | return (EINVAL); |
| 86 | /* |
| 87 | * If updating, check whether changing from read-only to |
| 88 | * read/write; if there is no device name, that's all we do. |
| 89 | */ |
| 90 | if (mp->mnt_flag & MNT_UPDATE) { |
| 91 | ump = VFSTOUFS(mp); |
| 92 | #ifdef NOTLFS /* LFS */ |
| 93 | fs = ump->um_fs; |
| 94 | if (fs->fs_ronly && (mp->mnt_flag & MNT_RDONLY) == 0) |
| 95 | fs->fs_ronly = 0; |
| 96 | #else |
| 97 | fs = ump->um_lfs; |
| 98 | if (fs->lfs_ronly && (mp->mnt_flag & MNT_RDONLY) == 0) |
| 99 | fs->lfs_ronly = 0; |
| 100 | #endif |
| 101 | if (args.fspec == 0) { |
| 102 | /* |
| 103 | * Process export requests. |
| 104 | */ |
| 105 | if (args.exflags & MNT_EXPORTED) { |
| 106 | if (error = ufs_hang_addrlist(mp, &args)) |
| 107 | return (error); |
| 108 | mp->mnt_flag |= MNT_EXPORTED; |
| 109 | } |
| 110 | if (args.exflags & MNT_DELEXPORT) { |
| 111 | ufs_free_addrlist(ump); |
| 112 | mp->mnt_flag &= |
| 113 | ~(MNT_EXPORTED | MNT_DEFEXPORTED); |
| 114 | } |
| 115 | return (0); |
| 116 | } |
| 117 | } |
| 118 | /* |
| 119 | * Not an update, or updating the name: look up the name |
| 120 | * and verify that it refers to a sensible block device. |
| 121 | */ |
| 122 | NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p); |
| 123 | if (error = namei(ndp)) |
| 124 | return (error); |
| 125 | devvp = ndp->ni_vp; |
| 126 | if (devvp->v_type != VBLK) { |
| 127 | vrele(devvp); |
| 128 | return (ENOTBLK); |
| 129 | } |
| 130 | if (major(devvp->v_rdev) >= nblkdev) { |
| 131 | vrele(devvp); |
| 132 | return (ENXIO); |
| 133 | } |
| 134 | if ((mp->mnt_flag & MNT_UPDATE) == 0) |
| 135 | error = lfs_mountfs(devvp, mp, p); /* LFS */ |
| 136 | else { |
| 137 | if (devvp != ump->um_devvp) |
| 138 | error = EINVAL; /* needs translation */ |
| 139 | else |
| 140 | vrele(devvp); |
| 141 | } |
| 142 | if (error) { |
| 143 | vrele(devvp); |
| 144 | return (error); |
| 145 | } |
| 146 | ump = VFSTOUFS(mp); |
| 147 | fs = ump->um_lfs; /* LFS */ |
| 148 | #ifdef NOTLFS /* LFS */ |
| 149 | (void) copyinstr(path, fs->fs_fsmnt, sizeof(fs->fs_fsmnt) - 1, &size); |
| 150 | bzero(fs->fs_fsmnt + size, sizeof(fs->fs_fsmnt) - size); |
| 151 | bcopy((caddr_t)fs->fs_fsmnt, (caddr_t)mp->mnt_stat.f_mntonname, |
| 152 | MNAMELEN); |
| 153 | (void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, |
| 154 | &size); |
| 155 | bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size); |
| 156 | (void) ufs_statfs(mp, &mp->mnt_stat, p); |
| 157 | #else |
| 158 | (void)copyinstr(path, fs->lfs_fsmnt, sizeof(fs->lfs_fsmnt) - 1, &size); |
| 159 | bzero(fs->lfs_fsmnt + size, sizeof(fs->lfs_fsmnt) - size); |
| 160 | bcopy((caddr_t)fs->lfs_fsmnt, (caddr_t)mp->mnt_stat.f_mntonname, |
| 161 | MNAMELEN); |
| 162 | (void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, |
| 163 | &size); |
| 164 | bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size); |
| 165 | (void) lfs_statfs(mp, &mp->mnt_stat, p); |
| 166 | #endif |
| 167 | return (0); |
| 168 | } |
| 169 | |
| 170 | /* |
| 171 | * Common code for mount and mountroot |
| 172 | * LFS specific |
| 173 | */ |
| 174 | int |
| 175 | lfs_mountfs(devvp, mp, p) |
| 176 | register struct vnode *devvp; |
| 177 | struct mount *mp; |
| 178 | struct proc *p; |
| 179 | { |
| 180 | extern struct vnode *rootvp; |
| 181 | register struct lfs *fs; |
| 182 | register struct ufsmount *ump; |
| 183 | struct vnode *vp; |
| 184 | struct buf *bp; |
| 185 | struct partinfo dpart; |
| 186 | dev_t dev; |
| 187 | int error, i, ronly, size; |
| 188 | |
| 189 | if (error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, NOCRED, p)) |
| 190 | return (error); |
| 191 | |
| 192 | if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, NOCRED, p) != 0) |
| 193 | size = DEV_BSIZE; |
| 194 | else { |
| 195 | size = dpart.disklab->d_secsize; |
| 196 | #ifdef NEVER_USED |
| 197 | dpart.part->p_fstype = FS_LFS; |
| 198 | dpart.part->p_fsize = fs->lfs_fsize; /* frag size */ |
| 199 | dpart.part->p_frag = fs->lfs_frag; /* frags per block */ |
| 200 | dpart.part->p_cpg = fs->lfs_segshift; /* segment shift */ |
| 201 | #endif |
| 202 | } |
| 203 | |
| 204 | /* Don't free random space on error. */ |
| 205 | bp = NULL; |
| 206 | ump = NULL; |
| 207 | |
| 208 | /* Read in the superblock. */ |
| 209 | if (error = bread(devvp, LFS_LABELPAD / size, LFS_SBPAD, NOCRED, &bp)) |
| 210 | goto out; |
| 211 | error = EINVAL; /* XXX needs translation */ |
| 212 | goto out; |
| 213 | } |
| 214 | #ifdef DEBUG |
| 215 | lfs_dump_super(fs); |
| 216 | #endif |
| 217 | |
| 218 | /* Allocate the mount structure, copy the superblock into it. */ |
| 219 | ump = (struct ufsmount *)malloc(sizeof *ump, M_UFSMNT, M_WAITOK); |
| 220 | fs = ump->um_lfs = malloc(sizeof(struct lfs), M_UFSMNT, M_WAITOK); |
| 221 | bcopy(bp->b_un.b_addr, fs, sizeof(struct lfs)); |
| 222 | if (sizeof(struct lfs) < LFS_SBPAD) /* XXX why? */ |
| 223 | bp->b_flags |= B_INVAL; |
| 224 | brelse(bp); |
| 225 | bp = NULL; |
| 226 | |
| 227 | /* Set up the I/O information */ |
| 228 | fs->lfs_iocount = 0; |
| 229 | |
| 230 | /* Set up the ifile and lock aflags */ |
| 231 | fs->lfs_doifile = 0; |
| 232 | fs->lfs_writer = 0; |
| 233 | fs->lfs_dirops = 0; |
| 234 | fs->lfs_seglock = 0; |
| 235 | |
| 236 | /* Set the file system readonly/modify bits. */ |
| 237 | fs->lfs_ronly = ronly; |
| 238 | if (ronly == 0) |
| 239 | fs->lfs_fmod = 1; |
| 240 | |
| 241 | /* Initialize the mount structure. */ |
| 242 | dev = devvp->v_rdev; |
| 243 | mp->mnt_data = (qaddr_t)ump; |
| 244 | mp->mnt_stat.f_fsid.val[0] = (long)dev; |
| 245 | mp->mnt_stat.f_fsid.val[1] = MOUNT_LFS; |
| 246 | mp->mnt_flag |= MNT_LOCAL; |
| 247 | ump->um_mountp = mp; |
| 248 | ump->um_dev = dev; |
| 249 | ump->um_devvp = devvp; |
| 250 | ump->um_bptrtodb = 0; |
| 251 | ump->um_seqinc = 1 << fs->lfs_fsbtodb; |
| 252 | ump->um_nindir = fs->lfs_nindir; |
| 253 | for (i = 0; i < MAXQUOTAS; i++) |
| 254 | ump->um_quotas[i] = NULLVP; |
| 255 | devvp->v_specflags |= SI_MOUNTEDON; |
| 256 | |
| 257 | /* |
| 258 | * We use the ifile vnode for almost every operation. Instead of |
| 259 | * retrieving it from the hash table each time we retrieve it here, |
| 260 | * artificially increment the reference count and keep a pointer |
| 261 | * to it in the incore copy of the superblock. |
| 262 | */ |
| 263 | if (error = VFS_VGET(mp, LFS_IFILE_INUM, &vp)) |
| 264 | goto out; |
| 265 | fs->lfs_ivnode = vp; |
| 266 | VREF(vp); |
| 267 | vput(vp); |
| 268 | |
| 269 | |
| 270 | return (0); |
| 271 | out: |
| 272 | if (bp) |
| 273 | brelse(bp); |
| 274 | (void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, NOCRED, p); |
| 275 | if (ump) { |
| 276 | free(ump->um_lfs, M_UFSMNT); |
| 277 | free(ump, M_UFSMNT); |
| 278 | mp->mnt_data = (qaddr_t)0; |
| 279 | } |
| 280 | return (error); |
| 281 | } |
| 282 | |
| 283 | /* |
| 284 | * unmount system call |
| 285 | */ |
| 286 | lfs_unmount(mp, mntflags, p) |
| 287 | struct mount *mp; |
| 288 | int mntflags; |
| 289 | struct proc *p; |
| 290 | { |
| 291 | extern int doforce; |
| 292 | register struct ufsmount *ump; |
| 293 | register struct lfs *fs; |
| 294 | int i, error, flags, ronly; |
| 295 | |
| 296 | flags = 0; |
| 297 | if (mntflags & MNT_FORCE) { |
| 298 | if (!doforce || mp == rootfs) |
| 299 | return (EINVAL); |
| 300 | flags |= FORCECLOSE; |
| 301 | } |
| 302 | |
| 303 | ump = VFSTOUFS(mp); |
| 304 | fs = ump->um_lfs; |
| 305 | return (error); |
| 306 | #ifdef QUOTA |
| 307 | if (mp->mnt_flag & MNT_QUOTA) { |
| 308 | if (error = vflush(mp, fs->lfs_ivnode, SKIPSYSTEM|flags)) |
| 309 | return (error); |
| 310 | for (i = 0; i < MAXQUOTAS; i++) { |
| 311 | if (ump->um_quotas[i] == NULLVP) |
| 312 | continue; |
| 313 | quotaoff(p, mp, i); |
| 314 | } |
| 315 | /* |
| 316 | * Here we fall through to vflush again to ensure |
| 317 | * that we have gotten rid of all the system vnodes. |
| 318 | */ |
| 319 | } |
| 320 | #endif |
| 321 | if (error = vflush(mp, fs->lfs_ivnode, flags)) |
| 322 | return (error); |
| 323 | fs->lfs_clean = 1; |
| 324 | if (error = VFS_SYNC(mp, 1, p->p_ucred, p)) |
| 325 | return (error); |
| 326 | if (fs->lfs_ivnode->v_dirtyblkhd.le_next) |
| 327 | panic("lfs_unmount: still dirty blocks on ifile vnode\n"); |
| 328 | vrele(fs->lfs_ivnode); |
| 329 | vgone(fs->lfs_ivnode); |
| 330 | |
| 331 | ronly = !fs->lfs_ronly; |
| 332 | * Get file system statistics. |
| 333 | */ |
| 334 | lfs_statfs(mp, sbp, p) |
| 335 | struct mount *mp; |
| 336 | register struct statfs *sbp; |
| 337 | struct proc *p; |
| 338 | { |
| 339 | register struct lfs *fs; |
| 340 | register struct ufsmount *ump; |
| 341 | |
| 342 | ump = VFSTOUFS(mp); |
| 343 | fs = ump->um_lfs; |
| 344 | if (fs->lfs_magic != LFS_MAGIC) |
| 345 | panic("lfs_statfs: magic"); |
| 346 | sbp->f_type = MOUNT_LFS; |
| 347 | sbp->f_bsize = fs->lfs_bsize; |
| 348 | sbp->f_iosize = fs->lfs_bsize; |
| 349 | sbp->f_blocks = dbtofsb(fs,fs->lfs_dsize); |
| 350 | sbp->f_bfree = dbtofsb(fs, fs->lfs_bfree); |
| 351 | sbp->f_bavail = (fs->lfs_dsize * (100 - fs->lfs_minfree) / 100) - |
| 352 | (fs->lfs_dsize - fs->lfs_bfree); |
| 353 | sbp->f_bavail = dbtofsb(fs, sbp->f_bavail); |
| 354 | sbp->f_files = fs->lfs_nfiles; |
| 355 | sbp->f_ffree = sbp->f_bfree * INOPB(fs); |
| 356 | if (sbp != &mp->mnt_stat) { |
| 357 | bcopy((caddr_t)mp->mnt_stat.f_mntonname, |
| 358 | (caddr_t)&sbp->f_mntonname[0], MNAMELEN); |
| 359 | bcopy((caddr_t)mp->mnt_stat.f_mntfromname, |
| 360 | (caddr_t)&sbp->f_mntfromname[0], MNAMELEN); |
| 361 | } |
| 362 | return (0); |
| 363 | } |
| 364 | |
| 365 | /* |
| 366 | * Go through the disk queues to initiate sandbagged IO; |
| 367 | * go through the inodes to write those that have been modified; |
| 368 | * initiate the writing of the super block if it has been modified. |
| 369 | * |
| 370 | * Note: we are always called with the filesystem marked `MPBUSY'. |
| 371 | */ |
| 372 | lfs_sync(mp, waitfor, cred, p) |
| 373 | struct mount *mp; |
| 374 | int waitfor; |
| 375 | struct ucred *cred; |
| 376 | struct proc *p; |
| 377 | { |
| 378 | extern int syncprt; |
| 379 | int error; |
| 380 | |
| 381 | /* All syncs must be checkpoints until roll-forward is implemented. */ |
| 382 | error = lfs_segwrite(mp, 1); |
| 383 | #ifdef QUOTA |
| 384 | qsync(mp); |
| 385 | #endif |
| 386 | return (error); |
| 387 | } |
| 388 | |
| 389 | /* |
| 390 | * Look up an LFS dinode number to find its incore vnode. If not already |
| 391 | * in core, read it in from the specified device. Return the inode locked. |
| 392 | * Detection and handling of mount points must be done by the calling routine. |
| 393 | */ |
| 394 | int |
| 395 | lfs_vget(mp, ino, vpp) |
| 396 | struct mount *mp; |
| 397 | ino_t ino; |
| 398 | struct vnode **vpp; |
| 399 | { |
| 400 | register struct lfs *fs; |
| 401 | register struct inode *ip; |
| 402 | struct buf *bp; |
| 403 | struct ifile *ifp; |
| 404 | struct vnode *vp; |
| 405 | struct ufsmount *ump; |
| 406 | daddr_t daddr; |
| 407 | dev_t dev; |
| 408 | int error; |
| 409 | |
| 410 | ump = VFSTOUFS(mp); |
| 411 | dev = ump->um_dev; |
| 412 | if ((*vpp = ufs_ihashget(dev, ino)) != NULL) |
| 413 | return (0); |
| 414 | |
| 415 | /* Translate the inode number to a disk address. */ |
| 416 | fs = ump->um_lfs; |
| 417 | if (ino == LFS_IFILE_INUM) |
| 418 | daddr = fs->lfs_idaddr; |
| 419 | else { |
| 420 | LFS_IENTRY(ifp, fs, ino, bp); |
| 421 | daddr = ifp->if_daddr; |
| 422 | brelse(bp); |
| 423 | if (daddr == LFS_UNUSED_DADDR) |
| 424 | return (ENOENT); |
| 425 | } |
| 426 | |
| 427 | /* Allocate new vnode/inode. */ |
| 428 | if (error = lfs_vcreate(mp, ino, &vp)) { |
| 429 | *vpp = NULL; |
| 430 | return (error); |
| 431 | } |
| 432 | |
| 433 | /* |
| 434 | * Put it onto its hash chain and lock it so that other requests for |
| 435 | * this inode will block if they arrive while we are sleeping waiting |
| 436 | * for old data structures to be purged or for the contents of the |
| 437 | * disk portion of this inode to be read. |
| 438 | */ |
| 439 | ip = VTOI(vp); |
| 440 | ufs_ihashins(ip); |
| 441 | |
| 442 | /* |
| 443 | * XXX |
| 444 | * This may not need to be here, logically it should go down with |
| 445 | * the i_devvp initialization. |
| 446 | * Ask Kirk. |
| 447 | */ |
| 448 | ip->i_lfs = ump->um_lfs; |
| 449 | |
| 450 | /* Read in the disk contents for the inode, copy into the inode. */ |
| 451 | if (error = |
| 452 | bread(ump->um_devvp, daddr, (int)fs->lfs_bsize, NOCRED, &bp)) { |
| 453 | /* |
| 454 | * The inode does not contain anything useful, so it |
| 455 | * would be misleading to leave it on its hash chain. |
| 456 | * Iput() will return it to the free list. |
| 457 | */ |
| 458 | ufs_ihashrem(ip); |
| 459 | |
| 460 | /* Unlock and discard unneeded inode. */ |
| 461 | vput(vp); |
| 462 | brelse(bp); |
| 463 | *vpp = NULL; |
| 464 | return (error); |
| 465 | } |
| 466 | ip->i_din = *lfs_ifind(fs, ino, bp->b_un.b_dino); |
| 467 | brelse(bp); |
| 468 | |
| 469 | /* |
| 470 | * Initialize the vnode from the inode, check for aliases. In all |
| 471 | * cases re-init ip, the underlying vnode/inode may have changed. |
| 472 | */ |
| 473 | if (error = ufs_vinit(mp, lfs_specop_p, LFS_FIFOOPS, &vp)) { |
| 474 | vput(vp); |
| 475 | *vpp = NULL; |
| 476 | return (error); |
| 477 | } |
| 478 | /* |
| 479 | * Finish inode initialization now that aliasing has been resolved. |
| 480 | */ |
| 481 | ip->i_devvp = ump->um_devvp; |
| 482 | VREF(ip->i_devvp); |
| 483 | *vpp = vp; |
| 484 | return (0); |
| 485 | } |
| 486 | |
| 487 | /* |
| 488 | * File handle to vnode |
| 489 | * |
| 490 | * Have to be really careful about stale file handles: |
| 491 | * - check that the inode number is valid |
| 492 | * - call lfs_vget() to get the locked inode |
| 493 | * - check for an unallocated inode (i_mode == 0) |
| 494 | * - check that the given client host has export rights and return |
| 495 | * those rights via. exflagsp and credanonp |
| 496 | * |
| 497 | * XXX |
| 498 | * use ifile to see if inode is allocated instead of reading off disk |
| 499 | * what is the relationship between my generational number and the NFS |
| 500 | * generational number. |
| 501 | */ |
| 502 | int |
| 503 | lfs_fhtovp(mp, fhp, nam, vpp, exflagsp, credanonp) |
| 504 | register struct mount *mp; |
| 505 | struct fid *fhp; |
| 506 | struct mbuf *nam; |
| 507 | struct vnode **vpp; |
| 508 | int *exflagsp; |
| 509 | struct ucred **credanonp; |
| 510 | { |
| 511 | register struct ufid *ufhp; |
| 512 | |
| 513 | ufhp = (struct ufid *)fhp; |
| 514 | if (ufhp->ufid_ino < ROOTINO) |
| 515 | return (ESTALE); |
| 516 | return (ufs_check_export(mp, ufhp, nam, vpp, exflagsp, credanonp)); |
| 517 | } |
| 518 | |
| 519 | /* |
| 520 | * Vnode pointer to File handle |
| 521 | */ |
| 522 | /* ARGSUSED */ |
| 523 | lfs_vptofh(vp, fhp) |
| 524 | struct vnode *vp; |
| 525 | struct fid *fhp; |
| 526 | { |
| 527 | register struct inode *ip; |
| 528 | register struct ufid *ufhp; |
| 529 | |
| 530 | ip = VTOI(vp); |
| 531 | ufhp = (struct ufid *)fhp; |
| 532 | ufhp->ufid_len = sizeof(struct ufid); |
| 533 | ufhp->ufid_ino = ip->i_number; |
| 534 | ufhp->ufid_gen = ip->i_gen; |
| 535 | return (0); |
| 536 | } |