xref: /dflybsd-src/sys/vfs/devfs/devfs_vfsops.c (revision 9f889dc4a590f18343b80275521c3f50d7030272)
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 	devfs_vfs_statfs(mp, &mp->mnt_stat, cred);
98 
99 	/*
100 	 * XXX: save other mount info passed from userland or so.
101 	 */
102 	mnt = kmalloc(sizeof(*mnt), M_DEVFS, M_WAITOK | M_ZERO);
103 
104 	lockmgr(&devfs_lock, LK_EXCLUSIVE);
105 	mp->mnt_data = (qaddr_t)mnt;
106 
107 	if (info.flags & DEVFS_MNT_JAIL)
108 		mnt->jailed = 1;
109 	else
110 		mnt->jailed = jailed(cred);
111 
112 	mnt->leak_count = 0;
113 	mnt->file_count = 0;
114 	mnt->mp = mp;
115 	TAILQ_INIT(&mnt->orphan_list);
116 	mnt->root_node = devfs_allocp(Proot, "", NULL, mp, NULL);
117 	KKASSERT(mnt->root_node);
118 	lockmgr(&devfs_lock, LK_RELEASE);
119 
120 	vfs_add_vnodeops(mp, &devfs_vnode_norm_vops, &mp->mnt_vn_norm_ops);
121 	vfs_add_vnodeops(mp, &devfs_vnode_dev_vops, &mp->mnt_vn_spec_ops);
122 
123 	devfs_debug(DEVFS_DEBUG_DEBUG, "calling devfs_mount_add\n");
124 	devfs_mount_add(mnt);
125 
126 	return (0);
127 }
128 
129 /*
130  * unmount system call
131  */
132 static int
133 devfs_vfs_unmount(struct mount *mp, int mntflags)
134 {
135 	int error = 0;
136 	int flags = 0;
137 
138 	devfs_debug(DEVFS_DEBUG_DEBUG, "(vfsops) devfs_unmount() called!\n");
139 
140 	if (mntflags & MNT_FORCE)
141 		flags |= FORCECLOSE;
142 
143 	error = vflush(mp, 0, flags);
144 
145 	if (error)
146 		return (error);
147 	lockmgr(&devfs_lock, LK_EXCLUSIVE);
148 	devfs_tracer_orphan_count(mp, 1);
149 	lockmgr(&devfs_lock, LK_RELEASE);
150 	devfs_mount_del(DEVFS_MNTDATA(mp));
151 	kfree(mp->mnt_data, M_DEVFS);
152 	mp->mnt_data = NULL;
153 
154 	return (0);
155 }
156 
157 /*
158  * Sets *vpp to the root procfs vnode, referenced and exclusively locked
159  */
160 int
161 devfs_vfs_root(struct mount *mp, struct vnode **vpp)
162 {
163 	int ret;
164 
165 	devfs_debug(DEVFS_DEBUG_DEBUG, "(vfsops) devfs_root() called!\n");
166 	lockmgr(&devfs_lock, LK_EXCLUSIVE);
167 	ret = devfs_allocv(vpp, DEVFS_MNTDATA(mp)->root_node);
168 	lockmgr(&devfs_lock, LK_RELEASE);
169 
170 	return ret;
171 }
172 
173 /*
174  * Get file system statistics.
175  */
176 static int
177 devfs_vfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
178 {
179 	devfs_debug(DEVFS_DEBUG_DEBUG, "(vfsops) devfs_stat() called!\n");
180 	sbp->f_bsize = DEV_BSIZE;
181 	sbp->f_iosize = DEV_BSIZE;
182 	sbp->f_blocks = 2;	/* avoid divide by zero in some df's */
183 	sbp->f_bfree = 0;
184 	sbp->f_bavail = 0;
185 	sbp->f_files = (DEVFS_MNTDATA(mp))?(DEVFS_MNTDATA(mp)->file_count):0;
186 	sbp->f_ffree = 0;
187 
188 	if (sbp != &mp->mnt_stat) {
189 		sbp->f_type = mp->mnt_vfc->vfc_typenum;
190 		bcopy(&mp->mnt_stat.f_fsid, &sbp->f_fsid, sizeof(sbp->f_fsid));
191 		bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
192 	}
193 
194 	return (0);
195 }
196 
197 static int
198 devfs_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
199 		 struct fid *fhp, struct vnode **vpp)
200 {
201 	struct vnode		*vp;
202 	struct devfs_fid	*dfhp;
203 
204 	dfhp = (struct devfs_fid *)fhp;
205 
206 	if (dfhp->fid_gen != boottime.tv_sec)
207 		return EINVAL;
208 
209 	vp = devfs_inode_to_vnode(mp, dfhp->fid_ino);
210 
211 	if (vp == NULL)
212 		return ENOENT;
213 
214 	*vpp = vp;
215 	return 0;
216 }
217 
218 /*
219  * Vnode pointer to File handle
220  */
221 static int
222 devfs_vfs_vptofh(struct vnode *vp, struct fid *fhp)
223 {
224 	struct devfs_node	*node;
225 	struct devfs_fid	*dfhp;
226 
227 	if ((node = DEVFS_NODE(vp)) != NULL) {
228 		dfhp = (struct devfs_fid *)fhp;
229 		dfhp->fid_len = sizeof(struct devfs_fid);
230 		dfhp->fid_ino = node->d_dir.d_ino;
231 		dfhp->fid_gen = boottime.tv_sec;
232 	} else {
233 		return ENOENT;
234 	}
235 
236 	return (0);
237 }
238 
239 static int
240 devfs_vfs_vget(struct mount *mp, struct vnode *dvp,
241 	       ino_t ino, struct vnode **vpp)
242 {
243 	struct vnode *vp;
244 	vp = devfs_inode_to_vnode(mp, ino);
245 
246 	if (vp == NULL)
247 		return ENOENT;
248 
249 	*vpp = vp;
250 	return 0;
251 }
252 
253 
254 static struct vfsops devfs_vfsops = {
255 	.vfs_mount 	= devfs_vfs_mount,
256 	.vfs_unmount	= devfs_vfs_unmount,
257 	.vfs_root 	= devfs_vfs_root,
258 	.vfs_statfs	= devfs_vfs_statfs,
259 	.vfs_sync 	= vfs_stdsync,
260 	.vfs_vget	= devfs_vfs_vget,
261 	.vfs_vptofh	= devfs_vfs_vptofh,
262 	.vfs_fhtovp	= devfs_vfs_fhtovp
263 };
264 
265 VFS_SET(devfs_vfsops, devfs, VFCF_SYNTHETIC);
266 MODULE_VERSION(devfs, 1);
267