1 /*-
2 * Copyright (c) 1992, 1993, 1995
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software donated to Berkeley by
6 * Jan-Simon Pendry.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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 the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 * @(#)portal_vfsops.c 8.11 (Berkeley) 5/14/95
33 *
34 * $FreeBSD$
35 */
36
37 /*
38 * Portal Filesystem
39 */
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/domain.h>
44 #include <sys/filedesc.h>
45 #include <sys/kernel.h>
46 #include <sys/lock.h>
47 #include <sys/mutex.h>
48 #include <sys/malloc.h>
49 #include <sys/file.h> /* Must come after sys/malloc.h */
50 #include <sys/mount.h>
51 #include <sys/proc.h>
52 #include <sys/protosw.h>
53 #include <sys/socket.h>
54 #include <sys/socketvar.h>
55 #include <sys/vnode.h>
56
57 #include <fs/portalfs/portal.h>
58
59 static MALLOC_DEFINE(M_PORTALFSMNT, "portal_mount", "PORTAL mount structure");
60
61 static vfs_unmount_t portal_unmount;
62 static vfs_root_t portal_root;
63 static vfs_statfs_t portal_statfs;
64
65 static const char *portal_opts[] = {
66 "socket", "config",
67 NULL
68 };
69
70 static int
71 portal_cmount(struct mntarg *ma, void *data, int flags)
72 {
73 struct portal_args args;
74 int error;
75
76 if (data == NULL)
77 return (EINVAL);
78 error = copyin(data, &args, sizeof args);
79 if (error)
80 return (error);
81
82 ma = mount_argf(ma, "socket", "%d", args.pa_socket);
83 ma = mount_argsu(ma, "config", args.pa_config, MAXPATHLEN);
84 error = kernel_mount(ma, flags);
85
86 return (error);
87 }
88
89 /*
90 * Mount the per-process file descriptors (/dev/fd)
91 */
92 static int
93 portal_mount(struct mount *mp)
94 {
95 struct file *fp;
96 struct portalmount *fmp;
97 struct socket *so;
98 struct vnode *rvp;
99 struct thread *td;
100 struct portalnode *pn;
101 int error, v;
102 char *p;
103
104 td = curthread;
105 if (vfs_filteropt(mp->mnt_optnew, portal_opts))
106 return (EINVAL);
107
108 error = vfs_scanopt(mp->mnt_optnew, "socket", "%d", &v);
109 if (error != 1)
110 return (EINVAL);
111 error = vfs_getopt(mp->mnt_optnew, "config", (void **)&p, NULL);
112 if (error)
113 return (error);
114
115 if ((error = fget(td, v, &fp)) != 0)
116 return (error);
117 if (fp->f_type != DTYPE_SOCKET) {
118 fdrop(fp, td);
119 return(ENOTSOCK);
120 }
121 so = fp->f_data; /* XXX race against userland */
122 if (so->so_proto->pr_domain->dom_family != AF_UNIX) {
123 fdrop(fp, td);
124 return (ESOCKTNOSUPPORT);
125 }
126
127 pn = malloc(sizeof(struct portalnode),
128 M_TEMP, M_WAITOK);
129
130 fmp = malloc(sizeof(struct portalmount),
131 M_PORTALFSMNT, M_WAITOK); /* XXX */
132
133 error = getnewvnode("portal", mp, &portal_vnodeops, &rvp); /* XXX */
134 if (error) {
135 free(fmp, M_PORTALFSMNT);
136 free(pn, M_TEMP);
137 fdrop(fp, td);
138 return (error);
139 }
140
141 error = insmntque(rvp, mp); /* XXX: Too early for mpsafe fs */
142 if (error != 0) {
143 free(fmp, M_PORTALFSMNT);
144 free(pn, M_TEMP);
145 fdrop(fp, td);
146 return (error);
147 }
148 rvp->v_data = pn;
149 rvp->v_type = VDIR;
150 rvp->v_vflag |= VV_ROOT;
151 VTOPORTAL(rvp)->pt_arg = 0;
152 VTOPORTAL(rvp)->pt_size = 0;
153 VTOPORTAL(rvp)->pt_fileid = PORTAL_ROOTFILEID;
154 fmp->pm_root = rvp;
155 fhold(fp);
156 fmp->pm_server = fp;
157
158 MNT_ILOCK(mp);
159 mp->mnt_flag |= MNT_LOCAL;
160 MNT_IUNLOCK(mp);
161 mp->mnt_data = fmp;
162 vfs_getnewfsid(mp);
163
164 vfs_mountedfrom(mp, p);
165 fdrop(fp, td);
166 return (0);
167 }
168
169 static int
170 portal_unmount(mp, mntflags)
171 struct mount *mp;
172 int mntflags;
173 {
174 int error, flags = 0;
175
176
177 if (mntflags & MNT_FORCE)
178 flags |= FORCECLOSE;
179
180 /*
181 * Clear out buffer cache. I don't think we
182 * ever get anything cached at this level at the
183 * moment, but who knows...
184 */
185 #ifdef notyet
186 mntflushbuf(mp, 0);
187 if (mntinvalbuf(mp, 1))
188 return (EBUSY);
189 #endif
190 /* There is 1 extra root vnode reference (pm_root). */
191 error = vflush(mp, 1, flags, curthread);
192 if (error)
193 return (error);
194
195 /*
196 * Shutdown the socket. This will cause the select in the
197 * daemon to wake up, and then the accept will get ECONNABORTED
198 * which it interprets as a request to go and bury itself.
199 */
200 soshutdown(VFSTOPORTAL(mp)->pm_server->f_data, 2);
201 /*
202 * Discard reference to underlying file. Must call closef because
203 * this may be the last reference.
204 */
205 closef(VFSTOPORTAL(mp)->pm_server, (struct thread *) 0);
206 /*
207 * Finally, throw away the portalmount structure
208 */
209 free(mp->mnt_data, M_PORTALFSMNT); /* XXX */
210 mp->mnt_data = 0;
211 return (0);
212 }
213
214 static int
215 portal_root(mp, flags, vpp)
216 struct mount *mp;
217 int flags;
218 struct vnode **vpp;
219 {
220 struct vnode *vp;
221
222 /*
223 * Return locked reference to root.
224 */
225 vp = VFSTOPORTAL(mp)->pm_root;
226 VREF(vp);
227 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
228 *vpp = vp;
229 return (0);
230 }
231
232 static int
233 portal_statfs(mp, sbp)
234 struct mount *mp;
235 struct statfs *sbp;
236 {
237
238 sbp->f_flags = 0;
239 sbp->f_bsize = DEV_BSIZE;
240 sbp->f_iosize = DEV_BSIZE;
241 sbp->f_blocks = 2; /* 1K to keep df happy */
242 sbp->f_bfree = 0;
243 sbp->f_bavail = 0;
244 sbp->f_files = 1; /* Allow for "." */
245 sbp->f_ffree = 0; /* See comments above */
246 return (0);
247 }
248
249 static struct vfsops portal_vfsops = {
250 .vfs_cmount = portal_cmount,
251 .vfs_mount = portal_mount,
252 .vfs_root = portal_root,
253 .vfs_statfs = portal_statfs,
254 .vfs_unmount = portal_unmount,
255 };
256
257 VFS_SET(portal_vfsops, portalfs, VFCF_SYNTHETIC);
Cache object: f692298cc72a5accb6eee7d74efe6aac
|