]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - sys/fs/portalfs/portal_vfsops.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / sys / fs / portalfs / portal_vfsops.c
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/capability.h>
44 #include <sys/domain.h>
45 #include <sys/filedesc.h>
46 #include <sys/kernel.h>
47 #include <sys/lock.h>
48 #include <sys/mutex.h>
49 #include <sys/malloc.h>
50 #include <sys/file.h>           /* Must come after sys/malloc.h */
51 #include <sys/mount.h>
52 #include <sys/proc.h>
53 #include <sys/protosw.h>
54 #include <sys/socket.h>
55 #include <sys/socketvar.h>
56 #include <sys/vnode.h>
57
58 #include <fs/portalfs/portal.h>
59
60 static MALLOC_DEFINE(M_PORTALFSMNT, "portal_mount", "PORTAL mount structure");
61
62 static vfs_unmount_t    portal_unmount;
63 static vfs_root_t       portal_root;
64 static vfs_statfs_t     portal_statfs;
65
66 static const char *portal_opts[] = {
67         "socket", "config",
68         NULL
69 };
70
71 static int
72 portal_cmount(struct mntarg *ma, void *data, uint64_t flags)
73 {
74         struct portal_args args;
75         int error;
76
77         if (data == NULL)
78                 return (EINVAL);
79         error = copyin(data, &args, sizeof args);
80         if (error)
81                 return (error);
82
83         ma = mount_argf(ma, "socket", "%d", args.pa_socket);
84         ma = mount_argsu(ma, "config", args.pa_config, MAXPATHLEN);
85         error = kernel_mount(ma, flags);
86
87         return (error);
88 }
89
90 /*
91  * Mount the per-process file descriptors (/dev/fd)
92  */
93 static int
94 portal_mount(struct mount *mp)
95 {
96         struct file *fp;
97         struct portalmount *fmp;
98         struct socket *so;
99         struct vnode *rvp;
100         struct thread *td;
101         struct portalnode *pn;
102         int error, v;
103         char *p;
104
105         td = curthread;
106         if (vfs_filteropt(mp->mnt_optnew, portal_opts))
107                 return (EINVAL);
108
109         error = vfs_scanopt(mp->mnt_optnew, "socket", "%d", &v);
110         if (error != 1)
111                 return (EINVAL);
112         error = vfs_getopt(mp->mnt_optnew, "config", (void **)&p, NULL);
113         if (error)
114                 return (error);
115
116         /*
117          * Capsicum is not incompatible with portalfs, but we don't really
118          * know what rights are required. In the spirit of "better safe than
119          * sorry", pretend that all rights are required for now.
120          */
121         if ((error = fget(td, v, CAP_MASK_VALID, &fp)) != 0)
122                 return (error);
123         if (fp->f_type != DTYPE_SOCKET) {
124                 fdrop(fp, td);
125                 return(ENOTSOCK);
126         }
127         so = fp->f_data;        /* XXX race against userland */
128         if (so->so_proto->pr_domain->dom_family != AF_UNIX) {
129                 fdrop(fp, td);
130                 return (ESOCKTNOSUPPORT);
131         }
132
133         pn = malloc(sizeof(struct portalnode),
134                 M_TEMP, M_WAITOK);
135
136         fmp = malloc(sizeof(struct portalmount),
137                 M_PORTALFSMNT, M_WAITOK);       /* XXX */
138
139         error = getnewvnode("portal", mp, &portal_vnodeops, &rvp); /* XXX */
140         if (error) {
141                 free(fmp, M_PORTALFSMNT);
142                 free(pn, M_TEMP);
143                 fdrop(fp, td);
144                 return (error);
145         }
146
147         error = insmntque(rvp, mp);     /* XXX: Too early for mpsafe fs */
148         if (error != 0) {
149                 free(fmp, M_PORTALFSMNT);
150                 free(pn, M_TEMP);
151                 fdrop(fp, td);
152                 return (error);
153         }
154         rvp->v_data = pn;
155         rvp->v_type = VDIR;
156         rvp->v_vflag |= VV_ROOT;
157         VTOPORTAL(rvp)->pt_arg = 0;
158         VTOPORTAL(rvp)->pt_size = 0;
159         VTOPORTAL(rvp)->pt_fileid = PORTAL_ROOTFILEID;
160         fmp->pm_root = rvp;
161         fhold(fp);
162         fmp->pm_server = fp;
163
164         MNT_ILOCK(mp);
165         mp->mnt_flag |= MNT_LOCAL;
166         MNT_IUNLOCK(mp);
167         mp->mnt_data =  fmp;
168         vfs_getnewfsid(mp);
169
170         vfs_mountedfrom(mp, p);
171         fdrop(fp, td);
172         return (0);
173 }
174
175 static int
176 portal_unmount(mp, mntflags)
177         struct mount *mp;
178         int mntflags;
179 {
180         int error, flags = 0;
181
182
183         if (mntflags & MNT_FORCE)
184                 flags |= FORCECLOSE;
185
186         /*
187          * Clear out buffer cache.  I don't think we
188          * ever get anything cached at this level at the
189          * moment, but who knows...
190          */
191 #ifdef notyet
192         mntflushbuf(mp, 0);
193         if (mntinvalbuf(mp, 1))
194                 return (EBUSY);
195 #endif
196         /* There is 1 extra root vnode reference (pm_root). */
197         error = vflush(mp, 1, flags, curthread);
198         if (error)
199                 return (error);
200
201         /*
202          * Shutdown the socket.  This will cause the select in the
203          * daemon to wake up, and then the accept will get ECONNABORTED
204          * which it interprets as a request to go and bury itself.
205          */
206         soshutdown(VFSTOPORTAL(mp)->pm_server->f_data, 2);
207         /*
208          * Discard reference to underlying file.  Must call closef because
209          * this may be the last reference.
210          */
211         closef(VFSTOPORTAL(mp)->pm_server, (struct thread *) 0);
212         /*
213          * Finally, throw away the portalmount structure
214          */
215         free(mp->mnt_data, M_PORTALFSMNT);      /* XXX */
216         mp->mnt_data = 0;
217         return (0);
218 }
219
220 static int
221 portal_root(mp, flags, vpp)
222         struct mount *mp;
223         int flags;
224         struct vnode **vpp;
225 {
226         struct vnode *vp;
227
228         /*
229          * Return locked reference to root.
230          */
231         vp = VFSTOPORTAL(mp)->pm_root;
232         VREF(vp);
233         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
234         *vpp = vp;
235         return (0);
236 }
237
238 static int
239 portal_statfs(mp, sbp)
240         struct mount *mp;
241         struct statfs *sbp;
242 {
243
244         sbp->f_flags = 0;
245         sbp->f_bsize = DEV_BSIZE;
246         sbp->f_iosize = DEV_BSIZE;
247         sbp->f_blocks = 2;              /* 1K to keep df happy */
248         sbp->f_bfree = 0;
249         sbp->f_bavail = 0;
250         sbp->f_files = 1;               /* Allow for "." */
251         sbp->f_ffree = 0;               /* See comments above */
252         return (0);
253 }
254
255 static struct vfsops portal_vfsops = {
256         .vfs_cmount =           portal_cmount,
257         .vfs_mount =            portal_mount,
258         .vfs_root =             portal_root,
259         .vfs_statfs =           portal_statfs,
260         .vfs_unmount =          portal_unmount,
261 };
262
263 VFS_SET(portal_vfsops, portalfs, VFCF_SYNTHETIC);