123400Smckusick /* 266805Sbostic * Copyright (c) 1989, 1991, 1993, 1994 363375Sbostic * The Regents of the University of California. All rights reserved. 423400Smckusick * 544539Sbostic * %sccs.include.redist.c% 637737Smckusick * 7*69373Smckusick * @(#)lfs_vfsops.c 8.17 (Berkeley) 05/10/95 823400Smckusick */ 912795Ssam 1051483Sbostic #include <sys/param.h> 1151483Sbostic #include <sys/systm.h> 1251483Sbostic #include <sys/namei.h> 1351483Sbostic #include <sys/proc.h> 1451483Sbostic #include <sys/kernel.h> 1551483Sbostic #include <sys/vnode.h> 1651483Sbostic #include <sys/mount.h> 1751483Sbostic #include <sys/buf.h> 1854734Smckusick #include <sys/mbuf.h> 1951483Sbostic #include <sys/file.h> 2051483Sbostic #include <sys/disklabel.h> 2151483Sbostic #include <sys/ioctl.h> 2251483Sbostic #include <sys/errno.h> 2351483Sbostic #include <sys/malloc.h> 2454734Smckusick #include <sys/socket.h> 2512795Ssam 2655047Smckusick #include <miscfs/specfs/specdev.h> 2755047Smckusick 2851501Sbostic #include <ufs/ufs/quota.h> 2951501Sbostic #include <ufs/ufs/inode.h> 3051501Sbostic #include <ufs/ufs/ufsmount.h> 3151501Sbostic #include <ufs/ufs/ufs_extern.h> 3247571Skarels 3351501Sbostic #include <ufs/lfs/lfs.h> 3451501Sbostic #include <ufs/lfs/lfs_extern.h> 3551155Sbostic 3651991Sbostic int lfs_mountfs __P((struct vnode *, struct mount *, struct proc *)); 3751215Sbostic 3851155Sbostic struct vfsops lfs_vfsops = { 3951155Sbostic lfs_mount, 4039043Smckusick ufs_start, 4151155Sbostic lfs_unmount, 4266805Sbostic ufs_root, 4341314Smckusick ufs_quotactl, 4451155Sbostic lfs_statfs, 4551155Sbostic lfs_sync, 4654693Sbostic lfs_vget, 4751559Smckusick lfs_fhtovp, 4851559Smckusick lfs_vptofh, 4951483Sbostic lfs_init, 5068676Smckusick lfs_sysctl, 5137737Smckusick }; 5237737Smckusick 5369369Smckusick /* 5469369Smckusick * Called by main() when ufs is going to be mounted as root. 5569369Smckusick */ 5651155Sbostic lfs_mountroot() 5712795Ssam { 5869369Smckusick extern struct vnode *rootvp; 5969369Smckusick struct fs *fs; 6069369Smckusick struct mount *mp; 6169369Smckusick struct proc *p = curproc; /* XXX */ 6269369Smckusick int error; 6369369Smckusick 6469369Smckusick /* 6569369Smckusick * Get vnodes for swapdev and rootdev. 6669369Smckusick */ 67*69373Smckusick if ((error = bdevvp(swapdev, &swapdev_vp)) || 68*69373Smckusick (error = bdevvp(rootdev, &rootvp))) { 69*69373Smckusick printf("lfs_mountroot: can't setup bdevvp's"); 70*69373Smckusick return (error); 71*69373Smckusick } 7269369Smckusick if (error = vfs_rootmountalloc("lfs", "root_device", &mp)) 7369369Smckusick return (error); 7469369Smckusick if (error = lfs_mountfs(rootvp, mp, p)) { 7569369Smckusick mp->mnt_vfc->vfc_refcount--; 7669369Smckusick free(mp, M_MOUNT); 7769369Smckusick return (error); 7869369Smckusick } 7969369Smckusick if (error = vfs_lock(mp)) { 8069369Smckusick (void)lfs_unmount(mp, 0, p); 8169369Smckusick mp->mnt_vfc->vfc_refcount--; 8269369Smckusick free(mp, M_MOUNT); 8369369Smckusick return (error); 8469369Smckusick } 8569369Smckusick CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list); 8669369Smckusick (void)lfs_statfs(mp, &mp->mnt_stat, p); 8769369Smckusick vfs_unlock(mp); 8869369Smckusick return (0); 8937737Smckusick } 9037737Smckusick 9137737Smckusick /* 9237737Smckusick * VFS Operations. 9337737Smckusick * 9437737Smckusick * mount system call 9537737Smckusick */ 9651155Sbostic lfs_mount(mp, path, data, ndp, p) 9740346Smckusick register struct mount *mp; 9837737Smckusick char *path; 9937737Smckusick caddr_t data; 10037737Smckusick struct nameidata *ndp; 10148036Smckusick struct proc *p; 10237737Smckusick { 10337737Smckusick struct vnode *devvp; 10437737Smckusick struct ufs_args args; 10537737Smckusick struct ufsmount *ump; 10651501Sbostic register struct lfs *fs; /* LFS */ 10737737Smckusick u_int size; 10837737Smckusick int error; 10967533Smckusick mode_t accessmode; 11037737Smckusick 11137737Smckusick if (error = copyin(data, (caddr_t)&args, sizeof (struct ufs_args))) 11237737Smckusick return (error); 11351483Sbostic 11451483Sbostic /* Until LFS can do NFS right. XXX */ 11565674Shibler if (args.export.ex_flags & MNT_EXPORTED) 11651483Sbostic return (EINVAL); 11765674Shibler 11840371Smckusick /* 11950264Skarels * If updating, check whether changing from read-only to 12050264Skarels * read/write; if there is no device name, that's all we do. 12150264Skarels */ 12250264Skarels if (mp->mnt_flag & MNT_UPDATE) { 12339336Smckusick ump = VFSTOUFS(mp); 12467533Smckusick if (fs->lfs_ronly && (mp->mnt_flag & MNT_WANTRDWR)) { 12567533Smckusick /* 12667533Smckusick * If upgrade to read-write by non-root, then verify 12767533Smckusick * that user has necessary permissions on the device. 12867533Smckusick */ 12967533Smckusick if (p->p_ucred->cr_uid != 0) { 13067533Smckusick VOP_LOCK(ump->um_devvp); 13167533Smckusick if (error = VOP_ACCESS(ump->um_devvp, 13267533Smckusick VREAD | VWRITE, p->p_ucred, p)) { 13367533Smckusick VOP_UNLOCK(ump->um_devvp); 13467533Smckusick return (error); 13567533Smckusick } 13667533Smckusick VOP_UNLOCK(ump->um_devvp); 13767533Smckusick } 13851155Sbostic fs->lfs_ronly = 0; 13967533Smckusick } 14052175Smckusick if (args.fspec == 0) { 14152175Smckusick /* 14252175Smckusick * Process export requests. 14352175Smckusick */ 14465674Shibler return (vfs_export(mp, &ump->um_export, &args.export)); 14552175Smckusick } 14650264Skarels } 14750264Skarels /* 14850264Skarels * Not an update, or updating the name: look up the name 14950264Skarels * and verify that it refers to a sensible block device. 15050264Skarels */ 15152333Smckusick NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p); 15252333Smckusick if (error = namei(ndp)) 15350264Skarels return (error); 15450264Skarels devvp = ndp->ni_vp; 15550264Skarels if (devvp->v_type != VBLK) { 15650264Skarels vrele(devvp); 15750264Skarels return (ENOTBLK); 15850264Skarels } 15950264Skarels if (major(devvp->v_rdev) >= nblkdev) { 16050264Skarels vrele(devvp); 16150264Skarels return (ENXIO); 16250264Skarels } 16367533Smckusick /* 16467533Smckusick * If mount by non-root, then verify that user has necessary 16567533Smckusick * permissions on the device. 16667533Smckusick */ 16767533Smckusick if (p->p_ucred->cr_uid != 0) { 16867533Smckusick accessmode = VREAD; 16967533Smckusick if ((mp->mnt_flag & MNT_RDONLY) == 0) 17067533Smckusick accessmode |= VWRITE; 17167533Smckusick VOP_LOCK(devvp); 17267533Smckusick if (error = VOP_ACCESS(devvp, accessmode, p->p_ucred, p)) { 17367533Smckusick vput(devvp); 17467533Smckusick return (error); 17567533Smckusick } 17667533Smckusick VOP_UNLOCK(devvp); 17767533Smckusick } 17850264Skarels if ((mp->mnt_flag & MNT_UPDATE) == 0) 17951155Sbostic error = lfs_mountfs(devvp, mp, p); /* LFS */ 18050264Skarels else { 18139336Smckusick if (devvp != ump->um_devvp) 18239336Smckusick error = EINVAL; /* needs translation */ 18342858Smckusick else 18442858Smckusick vrele(devvp); 18539336Smckusick } 18637737Smckusick if (error) { 18737737Smckusick vrele(devvp); 18837737Smckusick return (error); 18932721Smckusick } 19037737Smckusick ump = VFSTOUFS(mp); 19151155Sbostic fs = ump->um_lfs; /* LFS */ 19251155Sbostic #ifdef NOTLFS /* LFS */ 19337737Smckusick (void) copyinstr(path, fs->fs_fsmnt, sizeof(fs->fs_fsmnt) - 1, &size); 19437737Smckusick bzero(fs->fs_fsmnt + size, sizeof(fs->fs_fsmnt) - size); 19541397Smckusick bcopy((caddr_t)fs->fs_fsmnt, (caddr_t)mp->mnt_stat.f_mntonname, 19641397Smckusick MNAMELEN); 19751991Sbostic (void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, 19841397Smckusick &size); 19941397Smckusick bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size); 20048036Smckusick (void) ufs_statfs(mp, &mp->mnt_stat, p); 20151155Sbostic #else 20251155Sbostic (void)copyinstr(path, fs->lfs_fsmnt, sizeof(fs->lfs_fsmnt) - 1, &size); 20351155Sbostic bzero(fs->lfs_fsmnt + size, sizeof(fs->lfs_fsmnt) - size); 20451155Sbostic bcopy((caddr_t)fs->lfs_fsmnt, (caddr_t)mp->mnt_stat.f_mntonname, 20551155Sbostic MNAMELEN); 20651991Sbostic (void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, 20751155Sbostic &size); 20851155Sbostic bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size); 20951155Sbostic (void) lfs_statfs(mp, &mp->mnt_stat, p); 21051155Sbostic #endif 21137737Smckusick return (0); 21212795Ssam } 21312795Ssam 21437737Smckusick /* 21537737Smckusick * Common code for mount and mountroot 21651155Sbostic * LFS specific 21737737Smckusick */ 21851991Sbostic int 21951155Sbostic lfs_mountfs(devvp, mp, p) 22040376Smckusick register struct vnode *devvp; 22137737Smckusick struct mount *mp; 22248036Smckusick struct proc *p; 22312795Ssam { 22451155Sbostic extern struct vnode *rootvp; 22551501Sbostic register struct lfs *fs; 22651155Sbostic register struct ufsmount *ump; 22751155Sbostic struct vnode *vp; 22851155Sbostic struct buf *bp; 22930749Skarels struct partinfo dpart; 23051155Sbostic dev_t dev; 23151155Sbostic int error, i, ronly, size; 23267949Smckusick struct ucred *cred; 23312795Ssam 23467949Smckusick cred = p ? p->p_ucred : NOCRED; 23540376Smckusick /* 23640376Smckusick * Disallow multiple mounts of the same device. 23745652Smckusick * Disallow mounting of a device that is currently in use 23845652Smckusick * (except for root, which might share swap device for miniroot). 23940376Smckusick * Flush out any old buffers remaining from a previous use. 24040376Smckusick */ 24165674Shibler if (error = vfs_mountedon(devvp)) 24240376Smckusick return (error); 24345652Smckusick if (vcount(devvp) > 1 && devvp != rootvp) 24440376Smckusick return (EBUSY); 24567949Smckusick if (error = vinvalbuf(devvp, V_SAVE, cred, p, 0, 0)) 24654693Sbostic return (error); 24751155Sbostic 24851155Sbostic ronly = (mp->mnt_flag & MNT_RDONLY) != 0; 24959473Smckusick if (error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p)) 25037737Smckusick return (error); 25151155Sbostic 25267949Smckusick if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, cred, p) != 0) 25337737Smckusick size = DEV_BSIZE; 25448036Smckusick else { 25530749Skarels size = dpart.disklab->d_secsize; 25651155Sbostic #ifdef NEVER_USED 25751155Sbostic dpart.part->p_fstype = FS_LFS; 25851155Sbostic dpart.part->p_fsize = fs->lfs_fsize; /* frag size */ 25951155Sbostic dpart.part->p_frag = fs->lfs_frag; /* frags per block */ 26051155Sbostic dpart.part->p_cpg = fs->lfs_segshift; /* segment shift */ 26151155Sbostic #endif 26237737Smckusick } 26351155Sbostic 26451155Sbostic /* Don't free random space on error. */ 26551155Sbostic bp = NULL; 26651155Sbostic ump = NULL; 26751155Sbostic 26851155Sbostic /* Read in the superblock. */ 26967949Smckusick if (error = bread(devvp, LFS_LABELPAD / size, LFS_SBPAD, cred, &bp)) 27012795Ssam goto out; 27164523Sbostic fs = (struct lfs *)bp->b_data; 27251155Sbostic 27351155Sbostic /* Check the basics. */ 27451155Sbostic if (fs->lfs_magic != LFS_MAGIC || fs->lfs_bsize > MAXBSIZE || 27551501Sbostic fs->lfs_bsize < sizeof(struct lfs)) { 27641314Smckusick error = EINVAL; /* XXX needs translation */ 27716639Skarels goto out; 27816639Skarels } 27951155Sbostic 28051155Sbostic /* Allocate the mount structure, copy the superblock into it. */ 28141314Smckusick ump = (struct ufsmount *)malloc(sizeof *ump, M_UFSMNT, M_WAITOK); 28255548Sbostic fs = ump->um_lfs = malloc(sizeof(struct lfs), M_UFSMNT, M_WAITOK); 28364523Sbostic bcopy(bp->b_data, fs, sizeof(struct lfs)); 28451501Sbostic if (sizeof(struct lfs) < LFS_SBPAD) /* XXX why? */ 28539675Smckusick bp->b_flags |= B_INVAL; 28634421Skarels brelse(bp); 28734421Skarels bp = NULL; 28851155Sbostic 28951183Sbostic /* Set up the I/O information */ 29051183Sbostic fs->lfs_iocount = 0; 29151183Sbostic 29255548Sbostic /* Set up the ifile and lock aflags */ 29354264Sbostic fs->lfs_doifile = 0; 29454264Sbostic fs->lfs_writer = 0; 29554264Sbostic fs->lfs_dirops = 0; 29655548Sbostic fs->lfs_seglock = 0; 29754264Sbostic 29851155Sbostic /* Set the file system readonly/modify bits. */ 29951155Sbostic fs->lfs_ronly = ronly; 30012795Ssam if (ronly == 0) 30151155Sbostic fs->lfs_fmod = 1; 30251155Sbostic 30351155Sbostic /* Initialize the mount structure. */ 30451155Sbostic dev = devvp->v_rdev; 30541397Smckusick mp->mnt_data = (qaddr_t)ump; 30641397Smckusick mp->mnt_stat.f_fsid.val[0] = (long)dev; 30768676Smckusick mp->mnt_stat.f_fsid.val[1] = lfs_mount_type; 30867395Smkm mp->mnt_maxsymlinklen = fs->lfs_maxsymlinklen; 30941397Smckusick mp->mnt_flag |= MNT_LOCAL; 31037737Smckusick ump->um_mountp = mp; 31137737Smckusick ump->um_dev = dev; 31237737Smckusick ump->um_devvp = devvp; 31356475Smargo ump->um_bptrtodb = 0; 31456475Smargo ump->um_seqinc = 1 << fs->lfs_fsbtodb; 31556475Smargo ump->um_nindir = fs->lfs_nindir; 31641314Smckusick for (i = 0; i < MAXQUOTAS; i++) 31741314Smckusick ump->um_quotas[i] = NULLVP; 31852221Sbostic devvp->v_specflags |= SI_MOUNTEDON; 31951155Sbostic 32052221Sbostic /* 32152221Sbostic * We use the ifile vnode for almost every operation. Instead of 32252221Sbostic * retrieving it from the hash table each time we retrieve it here, 32352221Sbostic * artificially increment the reference count and keep a pointer 32452221Sbostic * to it in the incore copy of the superblock. 32552221Sbostic */ 32654693Sbostic if (error = VFS_VGET(mp, LFS_IFILE_INUM, &vp)) 32751155Sbostic goto out; 32851155Sbostic fs->lfs_ivnode = vp; 32952221Sbostic VREF(vp); 33052221Sbostic vput(vp); 33151155Sbostic 33237737Smckusick return (0); 33312795Ssam out: 33440872Smckusick if (bp) 33540872Smckusick brelse(bp); 33667949Smckusick (void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, cred, p); 33741314Smckusick if (ump) { 33851989Smckusick free(ump->um_lfs, M_UFSMNT); 33951501Sbostic free(ump, M_UFSMNT); 34041397Smckusick mp->mnt_data = (qaddr_t)0; 34132721Smckusick } 34237737Smckusick return (error); 34312795Ssam } 34412795Ssam 34539043Smckusick /* 34637737Smckusick * unmount system call 34737737Smckusick */ 34851155Sbostic lfs_unmount(mp, mntflags, p) 34937737Smckusick struct mount *mp; 35041314Smckusick int mntflags; 35148036Smckusick struct proc *p; 35212795Ssam { 35351501Sbostic extern int doforce; 35437737Smckusick register struct ufsmount *ump; 35555548Sbostic register struct lfs *fs; 35654693Sbostic int i, error, flags, ronly; 35712795Ssam 35854693Sbostic flags = 0; 35969344Smckusick if (mntflags & MNT_FORCE) 36041314Smckusick flags |= FORCECLOSE; 36154264Sbostic 36254264Sbostic ump = VFSTOUFS(mp); 36354264Sbostic fs = ump->um_lfs; 36412795Ssam #ifdef QUOTA 36541397Smckusick if (mp->mnt_flag & MNT_QUOTA) { 36654264Sbostic if (error = vflush(mp, fs->lfs_ivnode, SKIPSYSTEM|flags)) 36739898Smckusick return (error); 36841314Smckusick for (i = 0; i < MAXQUOTAS; i++) { 36941314Smckusick if (ump->um_quotas[i] == NULLVP) 37041314Smckusick continue; 37150114Smckusick quotaoff(p, mp, i); 37241314Smckusick } 37339898Smckusick /* 37441314Smckusick * Here we fall through to vflush again to ensure 37541314Smckusick * that we have gotten rid of all the system vnodes. 37639898Smckusick */ 37741314Smckusick } 37812795Ssam #endif 37954693Sbostic if (error = vflush(mp, fs->lfs_ivnode, flags)) 38039898Smckusick return (error); 38155548Sbostic fs->lfs_clean = 1; 38254693Sbostic if (error = VFS_SYNC(mp, 1, p->p_ucred, p)) 38354693Sbostic return (error); 38465240Smckusick if (fs->lfs_ivnode->v_dirtyblkhd.lh_first) 38554693Sbostic panic("lfs_unmount: still dirty blocks on ifile vnode\n"); 38655806Sbostic vrele(fs->lfs_ivnode); 38754693Sbostic vgone(fs->lfs_ivnode); 38854693Sbostic 38951155Sbostic ronly = !fs->lfs_ronly; 39040653Smckusick ump->um_devvp->v_specflags &= ~SI_MOUNTEDON; 39154693Sbostic error = VOP_CLOSE(ump->um_devvp, 39254693Sbostic ronly ? FREAD : FREAD|FWRITE, NOCRED, p); 39337737Smckusick vrele(ump->um_devvp); 39451989Smckusick free(fs, M_UFSMNT); 39551501Sbostic free(ump, M_UFSMNT); 39641397Smckusick mp->mnt_data = (qaddr_t)0; 39741397Smckusick mp->mnt_flag &= ~MNT_LOCAL; 39830749Skarels return (error); 39912795Ssam } 40012795Ssam 40137737Smckusick /* 40237737Smckusick * Get file system statistics. 40337737Smckusick */ 40451155Sbostic lfs_statfs(mp, sbp, p) 40537737Smckusick struct mount *mp; 40637737Smckusick register struct statfs *sbp; 40748036Smckusick struct proc *p; 40837737Smckusick { 40951501Sbostic register struct lfs *fs; 41037737Smckusick register struct ufsmount *ump; 41137737Smckusick 41237737Smckusick ump = VFSTOUFS(mp); 41351155Sbostic fs = ump->um_lfs; 41451155Sbostic if (fs->lfs_magic != LFS_MAGIC) 41551155Sbostic panic("lfs_statfs: magic"); 41651155Sbostic sbp->f_bsize = fs->lfs_bsize; 41751943Smckusick sbp->f_iosize = fs->lfs_bsize; 41856156Smargo sbp->f_blocks = dbtofsb(fs,fs->lfs_dsize); 41955588Sbostic sbp->f_bfree = dbtofsb(fs, fs->lfs_bfree); 42069293Smckusick /* 42169293Smckusick * To compute the available space. Subtract the minimum free 42269293Smckusick * from the total number of blocks in the file system. Set avail 42369293Smckusick * to the smaller of this number and fs->lfs_bfree. 42469293Smckusick */ 42569293Smckusick sbp->f_bavail = fs->lfs_dsize * (100 - fs->lfs_minfree) / 100; 42669293Smckusick sbp->f_bavail = 42769293Smckusick sbp->f_bavail > fs->lfs_bfree ? fs->lfs_bfree : sbp->f_bavail; 42856156Smargo sbp->f_bavail = dbtofsb(fs, sbp->f_bavail); 42951155Sbostic sbp->f_files = fs->lfs_nfiles; 43055588Sbostic sbp->f_ffree = sbp->f_bfree * INOPB(fs); 43141397Smckusick if (sbp != &mp->mnt_stat) { 43268676Smckusick sbp->f_type = mp->mnt_vfc->vfc_typenum; 43341397Smckusick bcopy((caddr_t)mp->mnt_stat.f_mntonname, 43440346Smckusick (caddr_t)&sbp->f_mntonname[0], MNAMELEN); 43541397Smckusick bcopy((caddr_t)mp->mnt_stat.f_mntfromname, 43640346Smckusick (caddr_t)&sbp->f_mntfromname[0], MNAMELEN); 43740346Smckusick } 43837737Smckusick return (0); 43937737Smckusick } 44037737Smckusick 44137737Smckusick /* 44237737Smckusick * Go through the disk queues to initiate sandbagged IO; 44337737Smckusick * go through the inodes to write those that have been modified; 44437737Smckusick * initiate the writing of the super block if it has been modified. 44541314Smckusick * 44641314Smckusick * Note: we are always called with the filesystem marked `MPBUSY'. 44737737Smckusick */ 44854693Sbostic lfs_sync(mp, waitfor, cred, p) 44937737Smckusick struct mount *mp; 45037737Smckusick int waitfor; 45154693Sbostic struct ucred *cred; 45254693Sbostic struct proc *p; 45337737Smckusick { 45451215Sbostic int error; 45537737Smckusick 45651310Sbostic /* All syncs must be checkpoints until roll-forward is implemented. */ 45757068Smargo error = lfs_segwrite(mp, SEGM_CKP | (waitfor ? SEGM_SYNC : 0)); 45841314Smckusick #ifdef QUOTA 45941314Smckusick qsync(mp); 46041314Smckusick #endif 46151215Sbostic return (error); 46237737Smckusick } 46351559Smckusick 46451559Smckusick /* 46554693Sbostic * Look up an LFS dinode number to find its incore vnode. If not already 46654693Sbostic * in core, read it in from the specified device. Return the inode locked. 46754693Sbostic * Detection and handling of mount points must be done by the calling routine. 46854693Sbostic */ 46954693Sbostic int 47054693Sbostic lfs_vget(mp, ino, vpp) 47154693Sbostic struct mount *mp; 47254693Sbostic ino_t ino; 47354693Sbostic struct vnode **vpp; 47454693Sbostic { 47554693Sbostic register struct lfs *fs; 47654693Sbostic register struct inode *ip; 47754693Sbostic struct buf *bp; 47854693Sbostic struct ifile *ifp; 47954693Sbostic struct vnode *vp; 48054693Sbostic struct ufsmount *ump; 48168550Smckusick ufs_daddr_t daddr; 48254693Sbostic dev_t dev; 48354693Sbostic int error; 48454693Sbostic 48554693Sbostic ump = VFSTOUFS(mp); 48654693Sbostic dev = ump->um_dev; 48754693Sbostic if ((*vpp = ufs_ihashget(dev, ino)) != NULL) 48854693Sbostic return (0); 48954693Sbostic 49054693Sbostic /* Translate the inode number to a disk address. */ 49154693Sbostic fs = ump->um_lfs; 49254693Sbostic if (ino == LFS_IFILE_INUM) 49354693Sbostic daddr = fs->lfs_idaddr; 49454693Sbostic else { 49554693Sbostic LFS_IENTRY(ifp, fs, ino, bp); 49654693Sbostic daddr = ifp->if_daddr; 49754693Sbostic brelse(bp); 49854693Sbostic if (daddr == LFS_UNUSED_DADDR) 49954693Sbostic return (ENOENT); 50054693Sbostic } 50154693Sbostic 50254693Sbostic /* Allocate new vnode/inode. */ 50354693Sbostic if (error = lfs_vcreate(mp, ino, &vp)) { 50454693Sbostic *vpp = NULL; 50554693Sbostic return (error); 50654693Sbostic } 50754693Sbostic 50854693Sbostic /* 50954693Sbostic * Put it onto its hash chain and lock it so that other requests for 51054693Sbostic * this inode will block if they arrive while we are sleeping waiting 51154693Sbostic * for old data structures to be purged or for the contents of the 51254693Sbostic * disk portion of this inode to be read. 51354693Sbostic */ 51454693Sbostic ip = VTOI(vp); 51554693Sbostic ufs_ihashins(ip); 51654693Sbostic 51754693Sbostic /* 51854693Sbostic * XXX 51954693Sbostic * This may not need to be here, logically it should go down with 52054693Sbostic * the i_devvp initialization. 52154693Sbostic * Ask Kirk. 52254693Sbostic */ 52354693Sbostic ip->i_lfs = ump->um_lfs; 52454693Sbostic 52554693Sbostic /* Read in the disk contents for the inode, copy into the inode. */ 52654693Sbostic if (error = 52754693Sbostic bread(ump->um_devvp, daddr, (int)fs->lfs_bsize, NOCRED, &bp)) { 52854693Sbostic /* 52959820Smckusick * The inode does not contain anything useful, so it would 53059820Smckusick * be misleading to leave it on its hash chain. With mode 53159820Smckusick * still zero, it will be unlinked and returned to the free 53259820Smckusick * list by vput(). 53354693Sbostic */ 53456799Smckusick vput(vp); 53554693Sbostic brelse(bp); 53654693Sbostic *vpp = NULL; 53754693Sbostic return (error); 53854693Sbostic } 53964523Sbostic ip->i_din = *lfs_ifind(fs, ino, (struct dinode *)bp->b_data); 54054693Sbostic brelse(bp); 54154693Sbostic 54254693Sbostic /* 54354693Sbostic * Initialize the vnode from the inode, check for aliases. In all 54454693Sbostic * cases re-init ip, the underlying vnode/inode may have changed. 54554693Sbostic */ 54654693Sbostic if (error = ufs_vinit(mp, lfs_specop_p, LFS_FIFOOPS, &vp)) { 54756799Smckusick vput(vp); 54854693Sbostic *vpp = NULL; 54954693Sbostic return (error); 55054693Sbostic } 55154693Sbostic /* 55254693Sbostic * Finish inode initialization now that aliasing has been resolved. 55354693Sbostic */ 55454693Sbostic ip->i_devvp = ump->um_devvp; 55554693Sbostic VREF(ip->i_devvp); 55654693Sbostic *vpp = vp; 55754693Sbostic return (0); 55854693Sbostic } 55954693Sbostic 56054693Sbostic /* 56151559Smckusick * File handle to vnode 56251559Smckusick * 56351559Smckusick * Have to be really careful about stale file handles: 56451559Smckusick * - check that the inode number is valid 56551559Smckusick * - call lfs_vget() to get the locked inode 56651559Smckusick * - check for an unallocated inode (i_mode == 0) 56755891Smckusick * - check that the given client host has export rights and return 56855891Smckusick * those rights via. exflagsp and credanonp 56951559Smckusick * 57051559Smckusick * XXX 57151559Smckusick * use ifile to see if inode is allocated instead of reading off disk 57251559Smckusick * what is the relationship between my generational number and the NFS 57351559Smckusick * generational number. 57451559Smckusick */ 57551559Smckusick int 57654734Smckusick lfs_fhtovp(mp, fhp, nam, vpp, exflagsp, credanonp) 57751559Smckusick register struct mount *mp; 57851559Smckusick struct fid *fhp; 57954734Smckusick struct mbuf *nam; 58051559Smckusick struct vnode **vpp; 58154734Smckusick int *exflagsp; 58254734Smckusick struct ucred **credanonp; 58351559Smckusick { 58451559Smckusick register struct ufid *ufhp; 58551559Smckusick 58651559Smckusick ufhp = (struct ufid *)fhp; 58751559Smckusick if (ufhp->ufid_ino < ROOTINO) 58854734Smckusick return (ESTALE); 58956246Smckusick return (ufs_check_export(mp, ufhp, nam, vpp, exflagsp, credanonp)); 59051559Smckusick } 59151559Smckusick 59251559Smckusick /* 59351559Smckusick * Vnode pointer to File handle 59451559Smckusick */ 59551559Smckusick /* ARGSUSED */ 59651559Smckusick lfs_vptofh(vp, fhp) 59751559Smckusick struct vnode *vp; 59851559Smckusick struct fid *fhp; 59951559Smckusick { 60051559Smckusick register struct inode *ip; 60151559Smckusick register struct ufid *ufhp; 60251559Smckusick 60351559Smckusick ip = VTOI(vp); 60451559Smckusick ufhp = (struct ufid *)fhp; 60551559Smckusick ufhp->ufid_len = sizeof(struct ufid); 60651559Smckusick ufhp->ufid_ino = ip->i_number; 60751559Smckusick ufhp->ufid_gen = ip->i_gen; 60851559Smckusick return (0); 60951559Smckusick } 61068676Smckusick 61168676Smckusick /* 61268676Smckusick * Initialize the filesystem, most work done by ufs_init. 61368676Smckusick */ 61468676Smckusick int lfs_mount_type; 61568676Smckusick 61668676Smckusick int 61768676Smckusick lfs_init(vfsp) 61868676Smckusick struct vfsconf *vfsp; 61968676Smckusick { 62068676Smckusick 62168676Smckusick lfs_mount_type = vfsp->vfc_typenum; 62268676Smckusick return (ufs_init(vfsp)); 62368676Smckusick } 624