xref: /dragonfly/sys/vfs/devfs/devfs_vfsops.c (revision a1626531)
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/malloc.h>
40 #include <sys/mount.h>
41 #include <sys/namecache.h>
42 #include <sys/vnode.h>
43 #include <sys/jail.h>
44 #include <sys/lock.h>
45 #include <sys/devfs.h>
46 
47 MALLOC_DECLARE(M_DEVFS);
48 
49 extern struct vop_ops devfs_vnode_norm_vops;
50 extern struct vop_ops devfs_vnode_dev_vops;
51 extern struct lock 	devfs_lock;
52 
53 static int	devfs_vfs_mount (struct mount *mp, char *path, caddr_t data,
54 				  struct ucred *cred);
55 static int	devfs_vfs_statfs (struct mount *mp, struct statfs *sbp,
56 				struct ucred *cred);
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);
64 
65 
66 /*
67  * VFS Operations.
68  *
69  * mount system call
70  */
71 static int
72 devfs_vfs_mount(struct mount *mp, char *path, caddr_t data, struct ucred *cred)
73 {
74 	struct devfs_mount_info info;
75 	struct devfs_mnt_data *mnt;
76 	size_t size;
77 	int error;
78 
79 	devfs_debug(DEVFS_DEBUG_DEBUG, "(vfsops) devfs_mount() called!\n");
80 
81 	if (mp->mnt_flag & MNT_UPDATE)
82 		return (EOPNOTSUPP);
83 
84 	if (data == NULL) {
85 		bzero(&info, sizeof(info));
86 	} else {
87 		if ((error = copyin(data, &info, sizeof(info))) != 0)
88 			return (error);
89 	}
90 
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 */
94 	mp->mnt_data = NULL;
95 	vfs_getnewfsid(mp);
96 
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);
103 
104 	/*
105 	 * XXX: save other mount info passed from userland or so.
106 	 */
107 	mnt = kmalloc(sizeof(*mnt), M_DEVFS, M_WAITOK | M_ZERO);
108 
109 	lockmgr(&devfs_lock, LK_EXCLUSIVE);
110 	mp->mnt_data = (qaddr_t)mnt;
111 
112 	if (info.flags & DEVFS_MNT_JAIL)
113 		mnt->jailed = 1;
114 	else
115 		mnt->jailed = jailed(cred);
116 
117 	mnt->leak_count = 0;
118 	mnt->file_count = 0;
119 	mnt->mp = mp;
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);
124 
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);
127 
128 	devfs_debug(DEVFS_DEBUG_DEBUG, "calling devfs_mount_add\n");
129 	devfs_mount_add(mnt);
130 
131 	return (0);
132 }
133 
134 /*
135  * unmount system call
136  */
137 static int
138 devfs_vfs_unmount(struct mount *mp, int mntflags)
139 {
140 	int error = 0;
141 	int flags = 0;
142 
143 	devfs_debug(DEVFS_DEBUG_DEBUG, "(vfsops) devfs_unmount() called!\n");
144 
145 	if (mntflags & MNT_FORCE)
146 		flags |= FORCECLOSE;
147 
148 	error = vflush(mp, 0, flags);
149 
150 	if (error)
151 		return (error);
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);
157 	mp->mnt_data = NULL;
158 
159 	return (0);
160 }
161 
162 /*
163  * Sets *vpp to the root procfs vnode, referenced and exclusively locked
164  */
165 int
166 devfs_vfs_root(struct mount *mp, struct vnode **vpp)
167 {
168 	int ret;
169 
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);
174 
175 	return ret;
176 }
177 
178 /*
179  * Get file system statistics.
180  */
181 static int
182 devfs_vfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
183 {
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 */
188 	sbp->f_bfree = 0;
189 	sbp->f_bavail = 0;
190 	sbp->f_files = (DEVFS_MNTDATA(mp))?(DEVFS_MNTDATA(mp)->file_count):0;
191 	sbp->f_ffree = 0;
192 
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);
197 	}
198 
199 	return (0);
200 }
201 
202 static int
203 devfs_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
204 		 struct fid *fhp, struct vnode **vpp)
205 {
206 	struct vnode		*vp;
207 	struct devfs_fid	*dfhp;
208 
209 	dfhp = (struct devfs_fid *)fhp;
210 
211 	if (dfhp->fid_gen != boottime.tv_sec)
212 		return EINVAL;
213 
214 	vp = devfs_inode_to_vnode(mp, dfhp->fid_ino);
215 
216 	if (vp == NULL)
217 		return ENOENT;
218 
219 	*vpp = vp;
220 	return 0;
221 }
222 
223 /*
224  * Vnode pointer to File handle
225  */
226 static int
227 devfs_vfs_vptofh(struct vnode *vp, struct fid *fhp)
228 {
229 	struct devfs_node	*node;
230 	struct devfs_fid	*dfhp;
231 
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;
237 	} else {
238 		return ENOENT;
239 	}
240 
241 	return (0);
242 }
243 
244 static int
245 devfs_vfs_vget(struct mount *mp, struct vnode *dvp,
246 	       ino_t ino, struct vnode **vpp)
247 {
248 	struct vnode *vp;
249 	vp = devfs_inode_to_vnode(mp, ino);
250 
251 	if (vp == NULL)
252 		return ENOENT;
253 
254 	*vpp = vp;
255 	return 0;
256 }
257 
258 static void
259 devfs_vfs_ncpgen_set(struct mount *mp, struct namecache *ncp)
260 {
261 	ncp->nc_namecache_gen = mp->mnt_namecache_gen;
262 }
263 
264 static int
265 devfs_vfs_ncpgen_test(struct mount *mp, struct namecache *ncp)
266 {
267 	return (ncp->nc_namecache_gen != mp->mnt_namecache_gen);
268 }
269 
270 static struct vfsops devfs_vfsops = {
271 	.vfs_flags	= 0,
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
281 };
282 
283 VFS_SET(devfs_vfsops, devfs, VFCF_SYNTHETIC | VFCF_MPSAFE);
284 MODULE_VERSION(devfs, 1);
285