4 * Copyright (c) 2009 The DragonFly Project. All rights reserved.
6 * This code is derived from software contributed to The DragonFly Project
7 * by Alex Hornung <ahornung@gmail.com>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
19 * 3. Neither the name of The DragonFly Project nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific, prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
31 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/malloc.h>
40 #include <sys/mount.h>
41 #include <sys/namecache.h>
42 #include <sys/vnode.h>
45 #include <sys/devfs.h>
47 MALLOC_DECLARE(M_DEVFS);
49 extern struct vop_ops devfs_vnode_norm_vops;
50 extern struct vop_ops devfs_vnode_dev_vops;
51 extern struct lock devfs_lock;
53 static int devfs_vfs_mount (struct mount *mp, char *path, caddr_t data,
55 static int devfs_vfs_statfs (struct mount *mp, struct statfs *sbp,
57 static int devfs_vfs_unmount (struct mount *mp, int mntflags);
58 int devfs_vfs_root(struct mount *mp, struct vnode **vpp);
59 static int devfs_vfs_vget(struct mount *mp, struct vnode *dvp,
60 ino_t ino, struct vnode **vpp);
61 static int devfs_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
62 struct fid *fhp, struct vnode **vpp);
63 static int devfs_vfs_vptofh(struct vnode *vp, struct fid *fhp);
72 devfs_vfs_mount(struct mount *mp, char *path, caddr_t data, struct ucred *cred)
74 struct devfs_mount_info info;
75 struct devfs_mnt_data *mnt;
79 devfs_debug(DEVFS_DEBUG_DEBUG, "(vfsops) devfs_mount() called!\n");
81 if (mp->mnt_flag & MNT_UPDATE)
85 bzero(&info, sizeof(info));
87 if ((error = copyin(data, &info, sizeof(info))) != 0)
91 mp->mnt_flag |= MNT_LOCAL;
92 mp->mnt_kern_flag |= MNTK_NOSTKMNT | MNTK_ALL_MPSAFE;
93 mp->mnt_kern_flag |= MNTK_QUICKHALT; /* no teardown needed on halt */
97 size = sizeof("devfs") - 1;
98 bcopy("devfs", mp->mnt_stat.f_mntfromname, size);
99 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
100 copyinstr(path, mp->mnt_stat.f_mntonname,
101 sizeof(mp->mnt_stat.f_mntonname) -1, &size);
102 devfs_vfs_statfs(mp, &mp->mnt_stat, cred);
105 * XXX: save other mount info passed from userland or so.
107 mnt = kmalloc(sizeof(*mnt), M_DEVFS, M_WAITOK | M_ZERO);
109 lockmgr(&devfs_lock, LK_EXCLUSIVE);
110 mp->mnt_data = (qaddr_t)mnt;
112 if (info.flags & DEVFS_MNT_JAIL)
115 mnt->jailed = jailed(cred);
120 TAILQ_INIT(&mnt->orphan_list);
121 mnt->root_node = devfs_allocp(Nroot, "", NULL, mp, NULL);
122 KKASSERT(mnt->root_node);
123 lockmgr(&devfs_lock, LK_RELEASE);
125 vfs_add_vnodeops(mp, &devfs_vnode_norm_vops, &mp->mnt_vn_norm_ops);
126 vfs_add_vnodeops(mp, &devfs_vnode_dev_vops, &mp->mnt_vn_spec_ops);
128 devfs_debug(DEVFS_DEBUG_DEBUG, "calling devfs_mount_add\n");
129 devfs_mount_add(mnt);
135 * unmount system call
138 devfs_vfs_unmount(struct mount *mp, int mntflags)
143 devfs_debug(DEVFS_DEBUG_DEBUG, "(vfsops) devfs_unmount() called!\n");
145 if (mntflags & MNT_FORCE)
148 error = vflush(mp, 0, flags);
152 lockmgr(&devfs_lock, LK_EXCLUSIVE);
153 devfs_tracer_orphan_count(mp, 1);
154 lockmgr(&devfs_lock, LK_RELEASE);
155 devfs_mount_del(DEVFS_MNTDATA(mp));
156 kfree(mp->mnt_data, M_DEVFS);
163 * Sets *vpp to the root procfs vnode, referenced and exclusively locked
166 devfs_vfs_root(struct mount *mp, struct vnode **vpp)
170 devfs_debug(DEVFS_DEBUG_DEBUG, "(vfsops) devfs_root() called!\n");
171 lockmgr(&devfs_lock, LK_EXCLUSIVE);
172 ret = devfs_allocv(vpp, DEVFS_MNTDATA(mp)->root_node);
173 lockmgr(&devfs_lock, LK_RELEASE);
179 * Get file system statistics.
182 devfs_vfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
184 devfs_debug(DEVFS_DEBUG_DEBUG, "(vfsops) devfs_stat() called!\n");
185 sbp->f_bsize = DEV_BSIZE;
186 sbp->f_iosize = DEV_BSIZE;
187 sbp->f_blocks = 2; /* avoid divide by zero in some df's */
190 sbp->f_files = (DEVFS_MNTDATA(mp))?(DEVFS_MNTDATA(mp)->file_count):0;
193 if (sbp != &mp->mnt_stat) {
194 sbp->f_type = mp->mnt_vfc->vfc_typenum;
195 bcopy(&mp->mnt_stat.f_fsid, &sbp->f_fsid, sizeof(sbp->f_fsid));
196 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
203 devfs_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
204 struct fid *fhp, struct vnode **vpp)
207 struct devfs_fid *dfhp;
209 dfhp = (struct devfs_fid *)fhp;
211 if (dfhp->fid_gen != boottime.tv_sec)
214 vp = devfs_inode_to_vnode(mp, dfhp->fid_ino);
224 * Vnode pointer to File handle
227 devfs_vfs_vptofh(struct vnode *vp, struct fid *fhp)
229 struct devfs_node *node;
230 struct devfs_fid *dfhp;
232 if ((node = DEVFS_NODE(vp)) != NULL) {
233 dfhp = (struct devfs_fid *)fhp;
234 dfhp->fid_len = sizeof(struct devfs_fid);
235 dfhp->fid_ino = node->d_dir.d_ino;
236 dfhp->fid_gen = boottime.tv_sec;
245 devfs_vfs_vget(struct mount *mp, struct vnode *dvp,
246 ino_t ino, struct vnode **vpp)
249 vp = devfs_inode_to_vnode(mp, ino);
259 devfs_vfs_ncpgen_set(struct mount *mp, struct namecache *ncp)
261 ncp->nc_namecache_gen = mp->mnt_namecache_gen;
265 devfs_vfs_ncpgen_test(struct mount *mp, struct namecache *ncp)
267 return (ncp->nc_namecache_gen != mp->mnt_namecache_gen);
270 static struct vfsops devfs_vfsops = {
272 .vfs_mount = devfs_vfs_mount,
273 .vfs_unmount = devfs_vfs_unmount,
274 .vfs_root = devfs_vfs_root,
275 .vfs_statfs = devfs_vfs_statfs,
276 .vfs_vget = devfs_vfs_vget,
277 .vfs_vptofh = devfs_vfs_vptofh,
278 .vfs_fhtovp = devfs_vfs_fhtovp,
279 .vfs_ncpgen_set = devfs_vfs_ncpgen_set,
280 .vfs_ncpgen_test = devfs_vfs_ncpgen_test
283 VFS_SET(devfs_vfsops, devfs, VFCF_SYNTHETIC | VFCF_MPSAFE);
284 MODULE_VERSION(devfs, 1);