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