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