xref: /dflybsd-src/sys/vfs/devfs/devfs_vfsops.c (revision fa7e6f37f8dfe18303d3d7be2f1242cd19146e7a)
1 /*
2  * Copyright (c) 2009 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Alex Hornung <ahornung@gmail.com>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/mount.h>
38 #include <sys/vnode.h>
39 #include <sys/jail.h>
40 #include <sys/lock.h>
41 #include <vfs/devfs/devfs.h>
42 
43 MALLOC_DECLARE(M_DEVFS);
44 
45 extern struct vop_ops devfs_vnode_norm_vops;
46 extern struct vop_ops devfs_vnode_dev_vops;
47 extern struct lock 	devfs_lock;
48 
49 static int	devfs_mount (struct mount *mp, char *path, caddr_t data,
50 				  struct ucred *cred);
51 static int	devfs_statfs (struct mount *mp, struct statfs *sbp,
52 				struct ucred *cred);
53 static int	devfs_unmount (struct mount *mp, int mntflags);
54 int			devfs_root(struct mount *mp, struct vnode **vpp);
55 static int	devfs_vget(struct mount *mp, struct vnode *dvp,
56 				ino_t ino, struct vnode **vpp);
57 static int	devfs_fhtovp(struct mount *mp, struct vnode *rootvp,
58 				struct fid *fhp, struct vnode **vpp);
59 static int	devfs_vptofh(struct vnode *vp, struct fid *fhp);
60 
61 
62 /*
63  * VFS Operations.
64  *
65  * mount system call
66  */
67 /* ARGSUSED */
68 static int
69 devfs_mount(struct mount *mp, char *path, caddr_t data, struct ucred *cred)
70 {
71 	struct devfs_mnt_data *mnt;
72 	size_t size;
73 
74 	devfs_debug(DEVFS_DEBUG_DEBUG, "(vfsops) devfs_mount() called!\n");
75 
76 	if (mp->mnt_flag & MNT_UPDATE)
77 		return (EOPNOTSUPP);
78 
79 
80 	mp->mnt_flag |= MNT_LOCAL;
81 	mp->mnt_kern_flag |= MNTK_NOSTKMNT;
82 	mp->mnt_data = NULL;
83 	vfs_getnewfsid(mp);
84 
85 	size = sizeof("devfs") - 1;
86 	bcopy("devfs", mp->mnt_stat.f_mntfromname, size);
87 	bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
88 	devfs_statfs(mp, &mp->mnt_stat, cred);
89 
90 	//XXX: save other mount info passed from userland or so.
91 	mnt = kmalloc(sizeof(*mnt), M_DEVFS, M_WAITOK | M_ZERO);
92 
93 	lockmgr(&devfs_lock, LK_EXCLUSIVE);
94 	mp->mnt_data = (qaddr_t)mnt;
95 	mnt->jailed = jailed(cred);
96 	mnt->leak_count = 0;
97 	mnt->mp = mp;
98 	TAILQ_INIT(&mnt->orphan_list);
99 	mnt->mntonnamelen = strlen(mp->mnt_stat.f_mntonname);
100 	mnt->root_node = devfs_allocp(Proot, "", NULL, mp, NULL);
101 	KKASSERT(mnt->root_node);
102 	lockmgr(&devfs_lock, LK_RELEASE);
103 
104 	vfs_add_vnodeops(mp, &devfs_vnode_norm_vops, &mp->mnt_vn_norm_ops);
105 	vfs_add_vnodeops(mp, &devfs_vnode_dev_vops, &mp->mnt_vn_spec_ops);
106 
107 	devfs_debug(DEVFS_DEBUG_DEBUG, "calling devfs_mount_add\n");
108 	devfs_mount_add(mnt);
109 
110 	return (0);
111 }
112 
113 /*
114  * unmount system call
115  */
116 static int
117 devfs_unmount(struct mount *mp, int mntflags)
118 {
119 	int error = 0;
120 	int flags = 0;
121 
122 	devfs_debug(DEVFS_DEBUG_DEBUG, "(vfsops) devfs_unmount() called!\n");
123 
124 	if (mntflags & MNT_FORCE)
125 		flags |= FORCECLOSE;
126 
127 	error = vflush(mp, 0, flags);
128 
129 	if (error)
130 		return (error);
131 	devfs_tracer_orphan_count(mp, 1);
132 	devfs_mount_del(DEVFS_MNTDATA(mp));
133 	kfree(mp->mnt_data, M_DEVFS);
134 	mp->mnt_data = NULL;
135 
136 	return (0);
137 }
138 
139 /*
140  * Sets *vpp to the root procfs vnode, referenced and exclusively locked
141  */
142 int
143 devfs_root(struct mount *mp, struct vnode **vpp)
144 {
145 	int ret;
146 	devfs_debug(DEVFS_DEBUG_DEBUG, "(vfsops) devfs_root() called!\n");
147 	lockmgr(&devfs_lock, LK_EXCLUSIVE);
148 	ret = devfs_allocv(vpp, DEVFS_MNTDATA(mp)->root_node);
149 	lockmgr(&devfs_lock, LK_RELEASE);
150 	return ret;
151 }
152 
153 /*
154  * Get file system statistics.
155  */
156 static int
157 devfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
158 {
159 	devfs_debug(DEVFS_DEBUG_DEBUG, "(vfsops) devfs_stat() called!\n");
160 	sbp->f_bsize = DEV_BSIZE;
161 	sbp->f_iosize = DEV_BSIZE;
162 	sbp->f_blocks = 2;	/* avoid divide by zero in some df's */
163 	sbp->f_bfree = 0;
164 	sbp->f_bavail = 0;
165 	sbp->f_files = 0;
166 	sbp->f_ffree = 0;
167 
168 	if (sbp != &mp->mnt_stat) {
169 		sbp->f_type = mp->mnt_vfc->vfc_typenum;
170 		bcopy(&mp->mnt_stat.f_fsid, &sbp->f_fsid, sizeof(sbp->f_fsid));
171 		bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
172 	}
173 
174 	return (0);
175 }
176 
177 static int
178 devfs_fhtovp(struct mount *mp, struct vnode *rootvp,
179 	   struct fid *fhp, struct vnode **vpp)
180 {
181 	struct vnode		*vp;
182 	struct devfs_fid	*dfhp;
183 
184 	dfhp = (struct devfs_fid *)fhp;
185 
186 	if (dfhp->fid_gen != boottime.tv_sec)
187 		return EINVAL;
188 
189 	vp = devfs_inode_to_vnode(mp, dfhp->fid_ino);
190 
191 	if (vp) {
192 		*vpp = vp;
193 	} else {
194 		return ENOENT;
195 	}
196 
197 	return 0;
198 }
199 
200 /*
201  * Vnode pointer to File handle
202  */
203 /* ARGSUSED */
204 static int
205 devfs_vptofh(struct vnode *vp, struct fid *fhp)
206 {
207 	struct devfs_node	*node;
208 	struct devfs_fid	*dfhp;
209 
210 	if ((node = DEVFS_NODE(vp)) != NULL) {
211 		dfhp = (struct devfs_fid *)fhp;
212 		dfhp->fid_len = sizeof(struct devfs_fid);
213 		dfhp->fid_ino = node->d_dir.d_ino;
214 		dfhp->fid_gen = boottime.tv_sec;
215 	} else {
216 		return ENOENT;
217 	}
218 
219 	return (0);
220 }
221 
222 static int
223 devfs_vget(struct mount *mp, struct vnode *dvp, ino_t ino, struct vnode **vpp)
224 {
225 	struct vnode *vp;
226 	vp = devfs_inode_to_vnode(mp, ino);
227 
228 	if (vp) {
229 		*vpp = vp;
230 		/* XXX: Maybe lock or ref? */
231 	} else {
232 		return ENOENT;
233 	}
234 
235 	return 0;
236 }
237 
238 
239 static struct vfsops devfs_vfsops = {
240 	.vfs_mount 	=   devfs_mount,
241 	.vfs_unmount =  devfs_unmount,
242 	.vfs_root 	=   devfs_root,
243 	.vfs_statfs =   devfs_statfs,
244 	.vfs_sync 	=   vfs_stdsync,
245 	.vfs_vget	= 	devfs_vget,
246 	.vfs_vptofh	= 	devfs_vptofh,
247 	.vfs_fhtovp	= 	devfs_fhtovp
248 };
249 
250 VFS_SET(devfs_vfsops, devfs, VFCF_SYNTHETIC);
251 MODULE_VERSION(devfs, 1);
252