portal_vfsops.c revision 162647
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: head/sys/fs/portalfs/portal_vfsops.c 162647 2006-09-26 04:12:49Z tegge $
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
59static MALLOC_DEFINE(M_PORTALFSMNT, "portal_mount", "PORTAL mount structure");
60
61static vfs_unmount_t	portal_unmount;
62static vfs_root_t	portal_root;
63static vfs_statfs_t	portal_statfs;
64
65static const char *portal_opts[] = {
66	"socket", "config",
67	NULL
68};
69
70static int
71portal_cmount(struct mntarg *ma, void *data, int flags, struct thread *td)
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 */
92static int
93portal_mount(struct mount *mp, struct thread *td)
94{
95	struct file *fp;
96	struct portalmount *fmp;
97	struct socket *so;
98	struct vnode *rvp;
99	struct portalnode *pn;
100	int error, v;
101	char *p;
102
103	if (vfs_filteropt(mp->mnt_optnew, portal_opts))
104		return (EINVAL);
105
106	error = vfs_scanopt(mp->mnt_optnew, "socket", "%d", &v);
107	if (error != 1)
108		return (EINVAL);
109	error = vfs_getopt(mp->mnt_optnew, "config", (void **)&p, NULL);
110	if (error)
111		return (error);
112
113	if ((error = fget(td, v, &fp)) != 0)
114		return (error);
115        if (fp->f_type != DTYPE_SOCKET) {
116		fdrop(fp, td);
117                return(ENOTSOCK);
118	}
119	so = fp->f_data;	/* XXX race against userland */
120	if (so->so_proto->pr_domain->dom_family != AF_UNIX) {
121		fdrop(fp, td);
122		return (ESOCKTNOSUPPORT);
123	}
124
125	MALLOC(pn, struct portalnode *, sizeof(struct portalnode),
126		M_TEMP, M_WAITOK);
127
128	MALLOC(fmp, struct portalmount *, sizeof(struct portalmount),
129		M_PORTALFSMNT, M_WAITOK);	/* XXX */
130
131	error = getnewvnode("portal", mp, &portal_vnodeops, &rvp); /* XXX */
132	if (error) {
133		FREE(fmp, M_PORTALFSMNT);
134		FREE(pn, M_TEMP);
135		fdrop(fp, td);
136		return (error);
137	}
138
139	rvp->v_data = pn;
140	rvp->v_type = VDIR;
141	rvp->v_vflag |= VV_ROOT;
142	VTOPORTAL(rvp)->pt_arg = 0;
143	VTOPORTAL(rvp)->pt_size = 0;
144	VTOPORTAL(rvp)->pt_fileid = PORTAL_ROOTFILEID;
145	fmp->pm_root = rvp;
146	fhold(fp);
147	fmp->pm_server = fp;
148
149	MNT_ILOCK(mp);
150	mp->mnt_flag |= MNT_LOCAL;
151	MNT_IUNLOCK(mp);
152	mp->mnt_data = (qaddr_t) fmp;
153	vfs_getnewfsid(mp);
154
155	vfs_mountedfrom(mp, p);
156	fdrop(fp, td);
157	return (0);
158}
159
160static int
161portal_unmount(mp, mntflags, td)
162	struct mount *mp;
163	int mntflags;
164	struct thread *td;
165{
166	int error, flags = 0;
167
168
169	if (mntflags & MNT_FORCE)
170		flags |= FORCECLOSE;
171
172	/*
173	 * Clear out buffer cache.  I don't think we
174	 * ever get anything cached at this level at the
175	 * moment, but who knows...
176	 */
177#ifdef notyet
178	mntflushbuf(mp, 0);
179	if (mntinvalbuf(mp, 1))
180		return (EBUSY);
181#endif
182	/* There is 1 extra root vnode reference (pm_root). */
183	error = vflush(mp, 1, flags, td);
184	if (error)
185		return (error);
186
187	/*
188	 * Shutdown the socket.  This will cause the select in the
189	 * daemon to wake up, and then the accept will get ECONNABORTED
190	 * which it interprets as a request to go and bury itself.
191	 */
192	soshutdown(VFSTOPORTAL(mp)->pm_server->f_data, 2);
193	/*
194	 * Discard reference to underlying file.  Must call closef because
195	 * this may be the last reference.
196	 */
197	closef(VFSTOPORTAL(mp)->pm_server, (struct thread *) 0);
198	/*
199	 * Finally, throw away the portalmount structure
200	 */
201	free(mp->mnt_data, M_PORTALFSMNT);	/* XXX */
202	mp->mnt_data = 0;
203	return (0);
204}
205
206static int
207portal_root(mp, flags, vpp, td)
208	struct mount *mp;
209	int flags;
210	struct vnode **vpp;
211	struct thread *td;
212{
213	struct vnode *vp;
214
215	/*
216	 * Return locked reference to root.
217	 */
218	vp = VFSTOPORTAL(mp)->pm_root;
219	VREF(vp);
220	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
221	*vpp = vp;
222	return (0);
223}
224
225static int
226portal_statfs(mp, sbp, td)
227	struct mount *mp;
228	struct statfs *sbp;
229	struct thread *td;
230{
231
232	sbp->f_flags = 0;
233	sbp->f_bsize = DEV_BSIZE;
234	sbp->f_iosize = DEV_BSIZE;
235	sbp->f_blocks = 2;		/* 1K to keep df happy */
236	sbp->f_bfree = 0;
237	sbp->f_bavail = 0;
238	sbp->f_files = 1;		/* Allow for "." */
239	sbp->f_ffree = 0;		/* See comments above */
240	return (0);
241}
242
243static struct vfsops portal_vfsops = {
244	.vfs_cmount =		portal_cmount,
245	.vfs_mount =		portal_mount,
246	.vfs_root =		portal_root,
247	.vfs_statfs =		portal_statfs,
248	.vfs_unmount =		portal_unmount,
249};
250
251VFS_SET(portal_vfsops, portalfs, VFCF_SYNTHETIC);
252