sys/vfs/devfs/devfs_vfsops.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 | /* * (MPSAFE) * * Copyright (c) 2009 The DragonFly Project. All rights reserved. * * This code is derived from software contributed to The DragonFly Project * by Alex Hornung <ahornung@gmail.com> * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * 3. Neither the name of The DragonFly Project nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific, prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include <sys/param.h> #include <sys/systm.h> #include <sys/kernel.h> #include <sys/malloc.h> #include <sys/mount.h> #include <sys/namecache.h> #include <sys/vnode.h> #include <sys/jail.h> #include <sys/lock.h> #include <sys/devfs.h> MALLOC_DECLARE(M_DEVFS); extern struct vop_ops devfs_vnode_norm_vops; extern struct vop_ops devfs_vnode_dev_vops; extern struct lock devfs_lock; static int devfs_vfs_mount (struct mount *mp, char *path, caddr_t data, struct ucred *cred); static int devfs_vfs_statfs (struct mount *mp, struct statfs *sbp, struct ucred *cred); static int devfs_vfs_unmount (struct mount *mp, int mntflags); int devfs_vfs_root(struct mount *mp, struct vnode **vpp); static int devfs_vfs_vget(struct mount *mp, struct vnode *dvp, ino_t ino, struct vnode **vpp); static int devfs_vfs_fhtovp(struct mount *mp, struct vnode *rootvp, struct fid *fhp, struct vnode **vpp); static int devfs_vfs_vptofh(struct vnode *vp, struct fid *fhp); /* * VFS Operations. * * mount system call */ static int devfs_vfs_mount(struct mount *mp, char *path, caddr_t data, struct ucred *cred) { struct devfs_mount_info info; struct devfs_mnt_data *mnt; size_t size; int error; devfs_debug(DEVFS_DEBUG_DEBUG, "(vfsops) devfs_mount() called!\n"); if (mp->mnt_flag & MNT_UPDATE) return (EOPNOTSUPP); if (data == NULL) { bzero(&info, sizeof(info)); } else { if ((error = copyin(data, &info, sizeof(info))) != 0) return (error); } mp->mnt_flag |= MNT_LOCAL; mp->mnt_kern_flag |= MNTK_NOSTKMNT | MNTK_ALL_MPSAFE; mp->mnt_kern_flag |= MNTK_QUICKHALT; /* no teardown needed on halt */ mp->mnt_data = NULL; vfs_getnewfsid(mp); size = sizeof("devfs") - 1; bcopy("devfs", mp->mnt_stat.f_mntfromname, size); bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size); copyinstr(path, mp->mnt_stat.f_mntonname, sizeof(mp->mnt_stat.f_mntonname) -1, &size); devfs_vfs_statfs(mp, &mp->mnt_stat, cred); /* * XXX: save other mount info passed from userland or so. */ mnt = kmalloc(sizeof(*mnt), M_DEVFS, M_WAITOK | M_ZERO); lockmgr(&devfs_lock, LK_EXCLUSIVE); mp->mnt_data = (qaddr_t)mnt; if (info.flags & DEVFS_MNT_JAIL) mnt->jailed = 1; else mnt->jailed = jailed(cred); mnt->leak_count = 0; mnt->file_count = 0; mnt->mp = mp; TAILQ_INIT(&mnt->orphan_list); mnt->root_node = devfs_allocp(Nroot, "", NULL, mp, NULL); KKASSERT(mnt->root_node); lockmgr(&devfs_lock, LK_RELEASE); vfs_add_vnodeops(mp, &devfs_vnode_norm_vops, &mp->mnt_vn_norm_ops); vfs_add_vnodeops(mp, &devfs_vnode_dev_vops, &mp->mnt_vn_spec_ops); devfs_debug(DEVFS_DEBUG_DEBUG, "calling devfs_mount_add\n"); devfs_mount_add(mnt); return (0); } /* * unmount system call */ static int devfs_vfs_unmount(struct mount *mp, int mntflags) { int error = 0; int flags = 0; devfs_debug(DEVFS_DEBUG_DEBUG, "(vfsops) devfs_unmount() called!\n"); if (mntflags & MNT_FORCE) flags |= FORCECLOSE; error = vflush(mp, 0, flags); if (error) return (error); lockmgr(&devfs_lock, LK_EXCLUSIVE); devfs_tracer_orphan_count(mp, 1); lockmgr(&devfs_lock, LK_RELEASE); devfs_mount_del(DEVFS_MNTDATA(mp)); kfree(mp->mnt_data, M_DEVFS); mp->mnt_data = NULL; return (0); } /* * Sets *vpp to the root procfs vnode, referenced and exclusively locked */ int devfs_vfs_root(struct mount *mp, struct vnode **vpp) { int ret; devfs_debug(DEVFS_DEBUG_DEBUG, "(vfsops) devfs_root() called!\n"); lockmgr(&devfs_lock, LK_EXCLUSIVE); ret = devfs_allocv(vpp, DEVFS_MNTDATA(mp)->root_node); lockmgr(&devfs_lock, LK_RELEASE); return ret; } /* * Get file system statistics. */ static int devfs_vfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred) { devfs_debug(DEVFS_DEBUG_DEBUG, "(vfsops) devfs_stat() called!\n"); sbp->f_bsize = DEV_BSIZE; sbp->f_iosize = DEV_BSIZE; sbp->f_blocks = 2; /* avoid divide by zero in some df's */ sbp->f_bfree = 0; sbp->f_bavail = 0; sbp->f_files = (DEVFS_MNTDATA(mp))?(DEVFS_MNTDATA(mp)->file_count):0; sbp->f_ffree = 0; if (sbp != &mp->mnt_stat) { sbp->f_type = mp->mnt_vfc->vfc_typenum; bcopy(&mp->mnt_stat.f_fsid, &sbp->f_fsid, sizeof(sbp->f_fsid)); bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN); } return (0); } static int devfs_vfs_fhtovp(struct mount *mp, struct vnode *rootvp, struct fid *fhp, struct vnode **vpp) { struct vnode *vp; struct devfs_fid *dfhp; dfhp = (struct devfs_fid *)fhp; if (dfhp->fid_gen != boottime.tv_sec) return EINVAL; vp = devfs_inode_to_vnode(mp, dfhp->fid_ino); if (vp == NULL) return ENOENT; *vpp = vp; return 0; } /* * Vnode pointer to File handle */ static int devfs_vfs_vptofh(struct vnode *vp, struct fid *fhp) { struct devfs_node *node; struct devfs_fid *dfhp; if ((node = DEVFS_NODE(vp)) != NULL) { dfhp = (struct devfs_fid *)fhp; dfhp->fid_len = sizeof(struct devfs_fid); dfhp->fid_ino = node->d_dir.d_ino; dfhp->fid_gen = boottime.tv_sec; } else { return ENOENT; } return (0); } static int devfs_vfs_vget(struct mount *mp, struct vnode *dvp, ino_t ino, struct vnode **vpp) { struct vnode *vp; vp = devfs_inode_to_vnode(mp, ino); if (vp == NULL) return ENOENT; *vpp = vp; return 0; } static void devfs_vfs_ncpgen_set(struct mount *mp, struct namecache *ncp) { ncp->nc_namecache_gen = mp->mnt_namecache_gen; } static int devfs_vfs_ncpgen_test(struct mount *mp, struct namecache *ncp) { return (ncp->nc_namecache_gen != mp->mnt_namecache_gen); } static struct vfsops devfs_vfsops = { .vfs_flags = 0, .vfs_mount = devfs_vfs_mount, .vfs_unmount = devfs_vfs_unmount, .vfs_root = devfs_vfs_root, .vfs_statfs = devfs_vfs_statfs, .vfs_vget = devfs_vfs_vget, .vfs_vptofh = devfs_vfs_vptofh, .vfs_fhtovp = devfs_vfs_fhtovp, .vfs_ncpgen_set = devfs_vfs_ncpgen_set, .vfs_ncpgen_test = devfs_vfs_ncpgen_test }; VFS_SET(devfs_vfsops, devfs, VFCF_SYNTHETIC | VFCF_MPSAFE); MODULE_VERSION(devfs, 1); |