121864bc5SMatthew Dillon /* 221864bc5SMatthew Dillon * Copyright (c) 2009 The DragonFly Project. All rights reserved. 321864bc5SMatthew Dillon * 421864bc5SMatthew Dillon * This code is derived from software contributed to The DragonFly Project 521864bc5SMatthew Dillon * by Alex Hornung <ahornung@gmail.com> 621864bc5SMatthew Dillon * 721864bc5SMatthew Dillon * Redistribution and use in source and binary forms, with or without 821864bc5SMatthew Dillon * modification, are permitted provided that the following conditions 921864bc5SMatthew Dillon * are met: 1021864bc5SMatthew Dillon * 1121864bc5SMatthew Dillon * 1. Redistributions of source code must retain the above copyright 1221864bc5SMatthew Dillon * notice, this list of conditions and the following disclaimer. 1321864bc5SMatthew Dillon * 2. Redistributions in binary form must reproduce the above copyright 1421864bc5SMatthew Dillon * notice, this list of conditions and the following disclaimer in 1521864bc5SMatthew Dillon * the documentation and/or other materials provided with the 1621864bc5SMatthew Dillon * distribution. 1721864bc5SMatthew Dillon * 3. Neither the name of The DragonFly Project nor the names of its 1821864bc5SMatthew Dillon * contributors may be used to endorse or promote products derived 1921864bc5SMatthew Dillon * from this software without specific, prior written permission. 2021864bc5SMatthew Dillon * 2121864bc5SMatthew Dillon * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 2221864bc5SMatthew Dillon * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 2321864bc5SMatthew Dillon * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 2421864bc5SMatthew Dillon * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 2521864bc5SMatthew Dillon * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 2621864bc5SMatthew Dillon * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 2721864bc5SMatthew Dillon * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 2821864bc5SMatthew Dillon * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 2921864bc5SMatthew Dillon * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 3021864bc5SMatthew Dillon * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 3121864bc5SMatthew Dillon * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3221864bc5SMatthew Dillon * SUCH DAMAGE. 3321864bc5SMatthew Dillon */ 3421864bc5SMatthew Dillon #include <sys/param.h> 3521864bc5SMatthew Dillon #include <sys/systm.h> 3621864bc5SMatthew Dillon #include <sys/kernel.h> 3721864bc5SMatthew Dillon #include <sys/mount.h> 3821864bc5SMatthew Dillon #include <sys/vnode.h> 3921864bc5SMatthew Dillon #include <sys/jail.h> 40*fa7e6f37SAlex Hornung #include <sys/lock.h> 4121864bc5SMatthew Dillon #include <vfs/devfs/devfs.h> 4221864bc5SMatthew Dillon 4321864bc5SMatthew Dillon MALLOC_DECLARE(M_DEVFS); 4421864bc5SMatthew Dillon 4521864bc5SMatthew Dillon extern struct vop_ops devfs_vnode_norm_vops; 4621864bc5SMatthew Dillon extern struct vop_ops devfs_vnode_dev_vops; 4721864bc5SMatthew Dillon extern struct lock devfs_lock; 4821864bc5SMatthew Dillon 4921864bc5SMatthew Dillon static int devfs_mount (struct mount *mp, char *path, caddr_t data, 5021864bc5SMatthew Dillon struct ucred *cred); 5121864bc5SMatthew Dillon static int devfs_statfs (struct mount *mp, struct statfs *sbp, 5221864bc5SMatthew Dillon struct ucred *cred); 5321864bc5SMatthew Dillon static int devfs_unmount (struct mount *mp, int mntflags); 5421864bc5SMatthew Dillon int devfs_root(struct mount *mp, struct vnode **vpp); 55*fa7e6f37SAlex Hornung static int devfs_vget(struct mount *mp, struct vnode *dvp, 56*fa7e6f37SAlex Hornung ino_t ino, struct vnode **vpp); 57*fa7e6f37SAlex Hornung static int devfs_fhtovp(struct mount *mp, struct vnode *rootvp, 58*fa7e6f37SAlex Hornung struct fid *fhp, struct vnode **vpp); 59*fa7e6f37SAlex Hornung static int devfs_vptofh(struct vnode *vp, struct fid *fhp); 60*fa7e6f37SAlex Hornung 6121864bc5SMatthew Dillon 6221864bc5SMatthew Dillon /* 6321864bc5SMatthew Dillon * VFS Operations. 6421864bc5SMatthew Dillon * 6521864bc5SMatthew Dillon * mount system call 6621864bc5SMatthew Dillon */ 6721864bc5SMatthew Dillon /* ARGSUSED */ 6821864bc5SMatthew Dillon static int 6921864bc5SMatthew Dillon devfs_mount(struct mount *mp, char *path, caddr_t data, struct ucred *cred) 7021864bc5SMatthew Dillon { 71ca8d7677SMatthew Dillon struct devfs_mnt_data *mnt; 7221864bc5SMatthew Dillon size_t size; 7321864bc5SMatthew Dillon 7421864bc5SMatthew Dillon devfs_debug(DEVFS_DEBUG_DEBUG, "(vfsops) devfs_mount() called!\n"); 7521864bc5SMatthew Dillon 7621864bc5SMatthew Dillon if (mp->mnt_flag & MNT_UPDATE) 7721864bc5SMatthew Dillon return (EOPNOTSUPP); 7821864bc5SMatthew Dillon 7921864bc5SMatthew Dillon 8021864bc5SMatthew Dillon mp->mnt_flag |= MNT_LOCAL; 8121864bc5SMatthew Dillon mp->mnt_kern_flag |= MNTK_NOSTKMNT; 82ca8d7677SMatthew Dillon mp->mnt_data = NULL; 8321864bc5SMatthew Dillon vfs_getnewfsid(mp); 8421864bc5SMatthew Dillon 8521864bc5SMatthew Dillon size = sizeof("devfs") - 1; 8621864bc5SMatthew Dillon bcopy("devfs", mp->mnt_stat.f_mntfromname, size); 8721864bc5SMatthew Dillon bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size); 8821864bc5SMatthew Dillon devfs_statfs(mp, &mp->mnt_stat, cred); 8921864bc5SMatthew Dillon 9021864bc5SMatthew Dillon //XXX: save other mount info passed from userland or so. 91ca8d7677SMatthew Dillon mnt = kmalloc(sizeof(*mnt), M_DEVFS, M_WAITOK | M_ZERO); 9221864bc5SMatthew Dillon 9321864bc5SMatthew Dillon lockmgr(&devfs_lock, LK_EXCLUSIVE); 94ca8d7677SMatthew Dillon mp->mnt_data = (qaddr_t)mnt; 95ca8d7677SMatthew Dillon mnt->jailed = jailed(cred); 96ca8d7677SMatthew Dillon mnt->leak_count = 0; 97ca8d7677SMatthew Dillon mnt->mp = mp; 98ca8d7677SMatthew Dillon TAILQ_INIT(&mnt->orphan_list); 99ca8d7677SMatthew Dillon mnt->mntonnamelen = strlen(mp->mnt_stat.f_mntonname); 100ca8d7677SMatthew Dillon mnt->root_node = devfs_allocp(Proot, "", NULL, mp, NULL); 101ca8d7677SMatthew Dillon KKASSERT(mnt->root_node); 10221864bc5SMatthew Dillon lockmgr(&devfs_lock, LK_RELEASE); 10321864bc5SMatthew Dillon 10421864bc5SMatthew Dillon vfs_add_vnodeops(mp, &devfs_vnode_norm_vops, &mp->mnt_vn_norm_ops); 10521864bc5SMatthew Dillon vfs_add_vnodeops(mp, &devfs_vnode_dev_vops, &mp->mnt_vn_spec_ops); 10621864bc5SMatthew Dillon 10721864bc5SMatthew Dillon devfs_debug(DEVFS_DEBUG_DEBUG, "calling devfs_mount_add\n"); 108ca8d7677SMatthew Dillon devfs_mount_add(mnt); 10921864bc5SMatthew Dillon 11021864bc5SMatthew Dillon return (0); 11121864bc5SMatthew Dillon } 11221864bc5SMatthew Dillon 11321864bc5SMatthew Dillon /* 11421864bc5SMatthew Dillon * unmount system call 11521864bc5SMatthew Dillon */ 11621864bc5SMatthew Dillon static int 11721864bc5SMatthew Dillon devfs_unmount(struct mount *mp, int mntflags) 11821864bc5SMatthew Dillon { 11921864bc5SMatthew Dillon int error = 0; 12021864bc5SMatthew Dillon int flags = 0; 12121864bc5SMatthew Dillon 12221864bc5SMatthew Dillon devfs_debug(DEVFS_DEBUG_DEBUG, "(vfsops) devfs_unmount() called!\n"); 12321864bc5SMatthew Dillon 12421864bc5SMatthew Dillon if (mntflags & MNT_FORCE) 12521864bc5SMatthew Dillon flags |= FORCECLOSE; 12621864bc5SMatthew Dillon 12721864bc5SMatthew Dillon error = vflush(mp, 0, flags); 12821864bc5SMatthew Dillon 12921864bc5SMatthew Dillon if (error) 13021864bc5SMatthew Dillon return (error); 13189bf2026SMatthew Dillon devfs_tracer_orphan_count(mp, 1); 13221864bc5SMatthew Dillon devfs_mount_del(DEVFS_MNTDATA(mp)); 13321864bc5SMatthew Dillon kfree(mp->mnt_data, M_DEVFS); 134ca8d7677SMatthew Dillon mp->mnt_data = NULL; 13521864bc5SMatthew Dillon 13621864bc5SMatthew Dillon return (0); 13721864bc5SMatthew Dillon } 13821864bc5SMatthew Dillon 13921864bc5SMatthew Dillon /* 14021864bc5SMatthew Dillon * Sets *vpp to the root procfs vnode, referenced and exclusively locked 14121864bc5SMatthew Dillon */ 14221864bc5SMatthew Dillon int 14321864bc5SMatthew Dillon devfs_root(struct mount *mp, struct vnode **vpp) 14421864bc5SMatthew Dillon { 14521864bc5SMatthew Dillon int ret; 14621864bc5SMatthew Dillon devfs_debug(DEVFS_DEBUG_DEBUG, "(vfsops) devfs_root() called!\n"); 14721864bc5SMatthew Dillon lockmgr(&devfs_lock, LK_EXCLUSIVE); 14821864bc5SMatthew Dillon ret = devfs_allocv(vpp, DEVFS_MNTDATA(mp)->root_node); 14921864bc5SMatthew Dillon lockmgr(&devfs_lock, LK_RELEASE); 15021864bc5SMatthew Dillon return ret; 15121864bc5SMatthew Dillon } 15221864bc5SMatthew Dillon 15321864bc5SMatthew Dillon /* 15421864bc5SMatthew Dillon * Get file system statistics. 15521864bc5SMatthew Dillon */ 15621864bc5SMatthew Dillon static int 15721864bc5SMatthew Dillon devfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred) 15821864bc5SMatthew Dillon { 15921864bc5SMatthew Dillon devfs_debug(DEVFS_DEBUG_DEBUG, "(vfsops) devfs_stat() called!\n"); 16021864bc5SMatthew Dillon sbp->f_bsize = DEV_BSIZE; 16121864bc5SMatthew Dillon sbp->f_iosize = DEV_BSIZE; 16221864bc5SMatthew Dillon sbp->f_blocks = 2; /* avoid divide by zero in some df's */ 16321864bc5SMatthew Dillon sbp->f_bfree = 0; 16421864bc5SMatthew Dillon sbp->f_bavail = 0; 16521864bc5SMatthew Dillon sbp->f_files = 0; 16621864bc5SMatthew Dillon sbp->f_ffree = 0; 16721864bc5SMatthew Dillon 16821864bc5SMatthew Dillon if (sbp != &mp->mnt_stat) { 16921864bc5SMatthew Dillon sbp->f_type = mp->mnt_vfc->vfc_typenum; 17021864bc5SMatthew Dillon bcopy(&mp->mnt_stat.f_fsid, &sbp->f_fsid, sizeof(sbp->f_fsid)); 17121864bc5SMatthew Dillon bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN); 17221864bc5SMatthew Dillon } 17321864bc5SMatthew Dillon 17421864bc5SMatthew Dillon return (0); 17521864bc5SMatthew Dillon } 17621864bc5SMatthew Dillon 177*fa7e6f37SAlex Hornung static int 178*fa7e6f37SAlex Hornung devfs_fhtovp(struct mount *mp, struct vnode *rootvp, 179*fa7e6f37SAlex Hornung struct fid *fhp, struct vnode **vpp) 180*fa7e6f37SAlex Hornung { 181*fa7e6f37SAlex Hornung struct vnode *vp; 182*fa7e6f37SAlex Hornung struct devfs_fid *dfhp; 183*fa7e6f37SAlex Hornung 184*fa7e6f37SAlex Hornung dfhp = (struct devfs_fid *)fhp; 185*fa7e6f37SAlex Hornung 186*fa7e6f37SAlex Hornung if (dfhp->fid_gen != boottime.tv_sec) 187*fa7e6f37SAlex Hornung return EINVAL; 188*fa7e6f37SAlex Hornung 189*fa7e6f37SAlex Hornung vp = devfs_inode_to_vnode(mp, dfhp->fid_ino); 190*fa7e6f37SAlex Hornung 191*fa7e6f37SAlex Hornung if (vp) { 192*fa7e6f37SAlex Hornung *vpp = vp; 193*fa7e6f37SAlex Hornung } else { 194*fa7e6f37SAlex Hornung return ENOENT; 195*fa7e6f37SAlex Hornung } 196*fa7e6f37SAlex Hornung 197*fa7e6f37SAlex Hornung return 0; 198*fa7e6f37SAlex Hornung } 199*fa7e6f37SAlex Hornung 200*fa7e6f37SAlex Hornung /* 201*fa7e6f37SAlex Hornung * Vnode pointer to File handle 202*fa7e6f37SAlex Hornung */ 203*fa7e6f37SAlex Hornung /* ARGSUSED */ 204*fa7e6f37SAlex Hornung static int 205*fa7e6f37SAlex Hornung devfs_vptofh(struct vnode *vp, struct fid *fhp) 206*fa7e6f37SAlex Hornung { 207*fa7e6f37SAlex Hornung struct devfs_node *node; 208*fa7e6f37SAlex Hornung struct devfs_fid *dfhp; 209*fa7e6f37SAlex Hornung 210*fa7e6f37SAlex Hornung if ((node = DEVFS_NODE(vp)) != NULL) { 211*fa7e6f37SAlex Hornung dfhp = (struct devfs_fid *)fhp; 212*fa7e6f37SAlex Hornung dfhp->fid_len = sizeof(struct devfs_fid); 213*fa7e6f37SAlex Hornung dfhp->fid_ino = node->d_dir.d_ino; 214*fa7e6f37SAlex Hornung dfhp->fid_gen = boottime.tv_sec; 215*fa7e6f37SAlex Hornung } else { 216*fa7e6f37SAlex Hornung return ENOENT; 217*fa7e6f37SAlex Hornung } 218*fa7e6f37SAlex Hornung 219*fa7e6f37SAlex Hornung return (0); 220*fa7e6f37SAlex Hornung } 221*fa7e6f37SAlex Hornung 222*fa7e6f37SAlex Hornung static int 223*fa7e6f37SAlex Hornung devfs_vget(struct mount *mp, struct vnode *dvp, ino_t ino, struct vnode **vpp) 224*fa7e6f37SAlex Hornung { 225*fa7e6f37SAlex Hornung struct vnode *vp; 226*fa7e6f37SAlex Hornung vp = devfs_inode_to_vnode(mp, ino); 227*fa7e6f37SAlex Hornung 228*fa7e6f37SAlex Hornung if (vp) { 229*fa7e6f37SAlex Hornung *vpp = vp; 230*fa7e6f37SAlex Hornung /* XXX: Maybe lock or ref? */ 231*fa7e6f37SAlex Hornung } else { 232*fa7e6f37SAlex Hornung return ENOENT; 233*fa7e6f37SAlex Hornung } 234*fa7e6f37SAlex Hornung 235*fa7e6f37SAlex Hornung return 0; 236*fa7e6f37SAlex Hornung } 237*fa7e6f37SAlex Hornung 238*fa7e6f37SAlex Hornung 23921864bc5SMatthew Dillon static struct vfsops devfs_vfsops = { 24021864bc5SMatthew Dillon .vfs_mount = devfs_mount, 24121864bc5SMatthew Dillon .vfs_unmount = devfs_unmount, 24221864bc5SMatthew Dillon .vfs_root = devfs_root, 24321864bc5SMatthew Dillon .vfs_statfs = devfs_statfs, 244*fa7e6f37SAlex Hornung .vfs_sync = vfs_stdsync, 245*fa7e6f37SAlex Hornung .vfs_vget = devfs_vget, 246*fa7e6f37SAlex Hornung .vfs_vptofh = devfs_vptofh, 247*fa7e6f37SAlex Hornung .vfs_fhtovp = devfs_fhtovp 24821864bc5SMatthew Dillon }; 24921864bc5SMatthew Dillon 25021864bc5SMatthew Dillon VFS_SET(devfs_vfsops, devfs, VFCF_SYNTHETIC); 25121864bc5SMatthew Dillon MODULE_VERSION(devfs, 1); 252