]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/fs/portalfs/portal_vnops.c
This commit was generated by cvs2svn to compensate for changes in r172423,
[FreeBSD/FreeBSD.git] / sys / fs / portalfs / portal_vnops.c
1 /*-
2  * Copyright (c) 1992, 1993
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_vnops.c      8.14 (Berkeley) 5/21/95
33  *
34  * $FreeBSD$
35  */
36
37 /*
38  * Portal Filesystem
39  */
40
41 #include <sys/param.h>
42 #include <sys/fcntl.h>
43 #include <sys/file.h>
44 #include <sys/kernel.h>
45 #include <sys/lock.h>
46 #include <sys/malloc.h>
47 #include <sys/mbuf.h>
48 #include <sys/mount.h>
49 #include <sys/mutex.h>
50 #include <sys/namei.h>
51 #include <sys/proc.h>
52 #include <sys/socket.h>
53 #include <sys/socketvar.h>
54 #include <sys/stat.h>
55 #include <sys/syscallsubr.h>
56 #include <sys/systm.h>
57 #include <sys/un.h>
58 #include <sys/unpcb.h>
59 #include <sys/vnode.h>
60
61 #include <fs/portalfs/portal.h>
62
63 static int portal_fileid = PORTAL_ROOTFILEID+1;
64
65 static void     portal_closefd(struct thread *td, int fd);
66 static int      portal_connect(struct socket *so, struct socket *so2);
67 static vop_getattr_t    portal_getattr;
68 static vop_lookup_t     portal_lookup;
69 static vop_open_t       portal_open;
70 static vop_readdir_t    portal_readdir;
71 static vop_reclaim_t    portal_reclaim;
72 static vop_setattr_t    portal_setattr;
73
74 static void
75 portal_closefd(td, fd)
76         struct thread *td;
77         int fd;
78 {
79         int error;
80
81         error = kern_close(td, fd);
82         /*
83          * We should never get an error, and there isn't anything
84          * we could do if we got one, so just print a message.
85          */
86         if (error)
87                 printf("portal_closefd: error = %d\n", error);
88 }
89
90 /*
91  * vp is the current namei directory
92  * cnp is the name to locate in that directory...
93  */
94 static int
95 portal_lookup(ap)
96         struct vop_lookup_args /* {
97                 struct vnode * a_dvp;
98                 struct vnode ** a_vpp;
99                 struct componentname * a_cnp;
100         } */ *ap;
101 {
102         struct componentname *cnp = ap->a_cnp;
103         struct vnode **vpp = ap->a_vpp;
104         struct vnode *dvp = ap->a_dvp;
105         char *pname = cnp->cn_nameptr;
106         struct thread *td = cnp->cn_thread;
107         struct portalnode *pt;
108         int error;
109         struct vnode *fvp = 0;
110         char *path;
111         int size;
112
113         *vpp = NULLVP;
114
115         if (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)
116                 return (EROFS);
117
118         if (cnp->cn_namelen == 1 && *pname == '.') {
119                 *vpp = dvp;
120                 VREF(dvp);
121                 return (0);
122         }
123         KASSERT((cnp->cn_flags & ISDOTDOT) == 0,
124             ("portal_lookup: Can not handle dotdot lookups."));
125
126         /*
127          * Do the MALLOC before the getnewvnode since doing so afterward
128          * might cause a bogus v_data pointer to get dereferenced
129          * elsewhere if MALLOC should block.
130          */
131         MALLOC(pt, struct portalnode *, sizeof(struct portalnode),
132                 M_TEMP, M_WAITOK);
133
134         error = getnewvnode("portal", dvp->v_mount, &portal_vnodeops, &fvp);
135         if (error) {
136                 FREE(pt, M_TEMP);
137                 goto bad;
138         }
139         fvp->v_type = VREG;
140         fvp->v_data = pt;
141         /*
142          * Save all of the remaining pathname and
143          * advance the namei next pointer to the end
144          * of the string.
145          */
146         for (size = 0, path = pname; *path; path++)
147                 size++;
148         cnp->cn_consume = size - cnp->cn_namelen;
149
150         pt->pt_arg = malloc(size+1, M_TEMP, M_WAITOK);
151         pt->pt_size = size+1;
152         bcopy(pname, pt->pt_arg, pt->pt_size);
153         pt->pt_fileid = portal_fileid++;
154
155         *vpp = fvp;
156         vn_lock(fvp, LK_EXCLUSIVE | LK_RETRY, td);
157         error = insmntque(fvp, dvp->v_mount);
158         if (error != 0) {
159                 *vpp = NULLVP;
160                 return (error);
161         }
162         return (0);
163
164 bad:;
165         if (fvp)
166                 vrele(fvp);
167         return (error);
168 }
169
170 static int
171 portal_connect(so, so2)
172         struct socket *so;
173         struct socket *so2;
174 {
175         /* from unp_connect, bypassing the namei stuff... */
176         struct socket *so3;
177         struct unpcb *unp2;
178         struct unpcb *unp3;
179
180         if (so2 == 0)
181                 return (ECONNREFUSED);
182
183         if (so->so_type != so2->so_type)
184                 return (EPROTOTYPE);
185
186         if ((so2->so_options & SO_ACCEPTCONN) == 0)
187                 return (ECONNREFUSED);
188
189         if ((so3 = sonewconn(so2, 0)) == 0)
190                 return (ECONNREFUSED);
191
192         unp2 = sotounpcb(so2);
193         unp3 = sotounpcb(so3);
194         if (unp2->unp_addr)
195                 unp3->unp_addr = (struct sockaddr_un *)
196                     sodupsockaddr((struct sockaddr *)unp2->unp_addr,
197                     M_NOWAIT);
198         so2 = so3;
199
200         return (uipc_connect2(so, so2));
201 }
202
203 static int
204 portal_open(ap)
205         struct vop_open_args /* {
206                 struct vnode *a_vp;
207                 int  a_mode;
208                 struct ucred *a_cred;
209                 struct thread *a_td;
210         } */ *ap;
211 {
212         struct socket *so = 0;
213         struct portalnode *pt;
214         struct thread *td = ap->a_td;
215         struct vnode *vp = ap->a_vp;
216         struct uio auio;
217         struct iovec aiov[2];
218         int res;
219         struct mbuf *cm = 0;
220         struct cmsghdr *cmsg;
221         int newfds;
222         int *ip;
223         int fd;
224         int error;
225         int len;
226         struct portalmount *fmp;
227         struct file *fp;
228         struct portal_cred pcred;
229
230         /*
231          * Nothing to do when opening the root node.
232          */
233         if (vp->v_vflag & VV_ROOT)
234                 return (0);
235
236         /*
237          * Can't be opened unless the caller is set up
238          * to deal with the side effects.  Check for this
239          * by testing whether td_dupfd has been set.
240          */
241         if (td->td_dupfd >= 0)
242                 return (ENODEV);
243
244         pt = VTOPORTAL(vp);
245         fmp = VFSTOPORTAL(vp->v_mount);
246
247         /*
248          * Create a new socket.
249          */
250         error = socreate(AF_UNIX, &so, SOCK_STREAM, 0, ap->a_td->td_ucred,
251             ap->a_td);
252         if (error)
253                 goto bad;
254
255         /*
256          * Reserve some buffer space
257          */
258         res = pt->pt_size + sizeof(pcred) + 512;        /* XXX */
259         error = soreserve(so, res, res);
260         if (error)
261                 goto bad;
262
263         /*
264          * Kick off connection
265          */
266         error = portal_connect(so, fmp->pm_server->f_data);
267         if (error)
268                 goto bad;
269
270         /*
271          * Wait for connection to complete
272          */
273         /*
274          * XXX: Since the mount point is holding a reference on the
275          * underlying server socket, it is not easy to find out whether
276          * the server process is still running.  To handle this problem
277          * we loop waiting for the new socket to be connected (something
278          * which will only happen if the server is still running) or for
279          * the reference count on the server socket to drop to 1, which
280          * will happen if the server dies.  Sleep for 5 second intervals
281          * and keep polling the reference count.   XXX.
282          */
283         /* XXXRW: Locking? */
284         SOCK_LOCK(so);
285         while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
286                 if (fmp->pm_server->f_count == 1) {
287                         SOCK_UNLOCK(so);
288                         error = ECONNREFUSED;
289                         goto bad;
290                 }
291                 (void) msleep((caddr_t) &so->so_timeo, SOCK_MTX(so), PSOCK,
292                     "portalcon", 5 * hz);
293         }
294         SOCK_UNLOCK(so);
295
296         if (so->so_error) {
297                 error = so->so_error;
298                 goto bad;
299         }
300
301         /*
302          * Set miscellaneous flags
303          */
304         SOCKBUF_LOCK(&so->so_rcv);
305         so->so_rcv.sb_timeo = 0;
306         so->so_rcv.sb_flags |= SB_NOINTR;
307         SOCKBUF_UNLOCK(&so->so_rcv);
308         SOCKBUF_LOCK(&so->so_snd);
309         so->so_snd.sb_timeo = 0;
310         so->so_snd.sb_flags |= SB_NOINTR;
311         SOCKBUF_UNLOCK(&so->so_snd);
312
313
314         pcred.pcr_flag = ap->a_mode;
315         pcred.pcr_uid = ap->a_cred->cr_uid;
316         pcred.pcr_ngroups = ap->a_cred->cr_ngroups;
317         bcopy(ap->a_cred->cr_groups, pcred.pcr_groups, NGROUPS * sizeof(gid_t));
318         aiov[0].iov_base = (caddr_t) &pcred;
319         aiov[0].iov_len = sizeof(pcred);
320         aiov[1].iov_base = pt->pt_arg;
321         aiov[1].iov_len = pt->pt_size;
322         auio.uio_iov = aiov;
323         auio.uio_iovcnt = 2;
324         auio.uio_rw = UIO_WRITE;
325         auio.uio_segflg = UIO_SYSSPACE;
326         auio.uio_td = td;
327         auio.uio_offset = 0;
328         auio.uio_resid = aiov[0].iov_len + aiov[1].iov_len;
329
330         error = sosend(so, (struct sockaddr *) 0, &auio,
331                         (struct mbuf *) 0, (struct mbuf *) 0, 0 , td);
332         if (error)
333                 goto bad;
334
335         len = auio.uio_resid = sizeof(int);
336         do {
337                 struct mbuf *m = 0;
338                 int flags = MSG_WAITALL;
339                 error = soreceive(so, (struct sockaddr **) 0, &auio,
340                                         &m, &cm, &flags);
341                 if (error)
342                         goto bad;
343
344                 /*
345                  * Grab an error code from the mbuf.
346                  */
347                 if (m) {
348                         m = m_pullup(m, sizeof(int));   /* Needed? */
349                         if (m) {
350                                 error = *(mtod(m, int *));
351                                 m_freem(m);
352                         } else {
353                                 error = EINVAL;
354                         }
355                 } else {
356                         if (cm == 0) {
357                                 error = ECONNRESET;      /* XXX */
358 #ifdef notdef
359                                 break;
360 #endif
361                         }
362                 }
363         } while (cm == 0 && auio.uio_resid == len && !error);
364
365         if (cm == 0)
366                 goto bad;
367
368         if (auio.uio_resid) {
369                 error = 0;
370 #ifdef notdef
371                 error = EMSGSIZE;
372                 goto bad;
373 #endif
374         }
375
376         /*
377          * XXX: Break apart the control message, and retrieve the
378          * received file descriptor.  Note that more than one descriptor
379          * may have been received, or that the rights chain may have more
380          * than a single mbuf in it.  What to do?
381          */
382         cmsg = mtod(cm, struct cmsghdr *);
383         newfds = (cmsg->cmsg_len - sizeof(*cmsg)) / sizeof (int);
384         if (newfds == 0) {
385                 error = ECONNREFUSED;
386                 goto bad;
387         }
388         /*
389          * At this point the rights message consists of a control message
390          * header, followed by a data region containing a vector of
391          * integer file descriptors.  The fds were allocated by the action
392          * of receiving the control message.
393          */
394         ip = (int *) (cmsg + 1);
395         fd = *ip++;
396         if (newfds > 1) {
397                 /*
398                  * Close extra fds.
399                  */
400                 int i;
401                 printf("portal_open: %d extra fds\n", newfds - 1);
402                 for (i = 1; i < newfds; i++) {
403                         portal_closefd(td, *ip);
404                         ip++;
405                 }
406         }
407
408         /*
409          * Check that the mode the file is being opened for is a subset
410          * of the mode of the existing descriptor.
411          */
412         if ((error = fget(td, fd, &fp)) != 0)
413                 goto bad;
414         if (((ap->a_mode & (FREAD|FWRITE)) | fp->f_flag) != fp->f_flag) {
415                 fdrop(fp, td);
416                 portal_closefd(td, fd);
417                 error = EACCES;
418                 goto bad;
419         }
420         fdrop(fp, td);
421
422         /*
423          * Save the dup fd in the proc structure then return the
424          * special error code (ENXIO) which causes magic things to
425          * happen in vn_open.  The whole concept is, well, hmmm.
426          */
427         td->td_dupfd = fd;
428         error = ENXIO;
429
430 bad:;
431         /*
432          * And discard the control message.
433          */
434         if (cm) {
435                 m_freem(cm);
436         }
437
438         if (so) {
439                 soshutdown(so, 2);
440                 soclose(so);
441         }
442         return (error);
443 }
444
445 static int
446 portal_getattr(ap)
447         struct vop_getattr_args /* {
448                 struct vnode *a_vp;
449                 struct vattr *a_vap;
450                 struct ucred *a_cred;
451                 struct thread *a_td;
452         } */ *ap;
453 {
454         struct vnode *vp = ap->a_vp;
455         struct vattr *vap = ap->a_vap;
456
457         bzero(vap, sizeof(*vap));
458         vattr_null(vap);
459         vap->va_uid = 0;
460         vap->va_gid = 0;
461         vap->va_size = DEV_BSIZE;
462         vap->va_blocksize = DEV_BSIZE;
463         nanotime(&vap->va_atime);
464         vap->va_mtime = vap->va_atime;
465         vap->va_ctime = vap->va_mtime;
466         vap->va_gen = 0;
467         vap->va_flags = 0;
468         vap->va_rdev = 0;
469         /* vap->va_qbytes = 0; */
470         vap->va_bytes = 0;
471         /* vap->va_qsize = 0; */
472         if (vp->v_vflag & VV_ROOT) {
473                 vap->va_type = VDIR;
474                 vap->va_mode = S_IRUSR|S_IWUSR|S_IXUSR|
475                                 S_IRGRP|S_IWGRP|S_IXGRP|
476                                 S_IROTH|S_IWOTH|S_IXOTH;
477                 vap->va_nlink = 2;
478                 vap->va_fileid = 2;
479         } else {
480                 vap->va_type = VREG;
481                 vap->va_mode = S_IRUSR|S_IWUSR|
482                                 S_IRGRP|S_IWGRP|
483                                 S_IROTH|S_IWOTH;
484                 vap->va_nlink = 1;
485                 vap->va_fileid = VTOPORTAL(vp)->pt_fileid;
486         }
487         return (0);
488 }
489
490 static int
491 portal_setattr(ap)
492         struct vop_setattr_args /* {
493                 struct vnode *a_vp;
494                 struct vattr *a_vap;
495                 struct ucred *a_cred;
496                 struct thread *a_td;
497         } */ *ap;
498 {
499
500         /*
501          * Can't mess with the root vnode
502          */
503         if (ap->a_vp->v_vflag & VV_ROOT)
504                 return (EACCES);
505
506         if (ap->a_vap->va_flags != VNOVAL)
507                 return (EOPNOTSUPP);
508
509         return (0);
510 }
511
512 /*
513  * Fake readdir, just return empty directory.
514  * It is hard to deal with '.' and '..' so don't bother.
515  */
516 static int
517 portal_readdir(ap)
518         struct vop_readdir_args /* {
519                 struct vnode *a_vp;
520                 struct uio *a_uio;
521                 struct ucred *a_cred;
522                 int *a_eofflag;
523                 u_long *a_cookies;
524                 int a_ncookies;
525         } */ *ap;
526 {
527
528         /*
529          * We don't allow exporting portal mounts, and currently local
530          * requests do not need cookies.
531          */
532         if (ap->a_ncookies)
533                 panic("portal_readdir: not hungry");
534
535         return (0);
536 }
537
538 static int
539 portal_reclaim(ap)
540         struct vop_reclaim_args /* {
541                 struct vnode *a_vp;
542         } */ *ap;
543 {
544         struct portalnode *pt = VTOPORTAL(ap->a_vp);
545
546         if (pt->pt_arg) {
547                 free((caddr_t) pt->pt_arg, M_TEMP);
548                 pt->pt_arg = 0;
549         }
550         FREE(ap->a_vp->v_data, M_TEMP);
551         ap->a_vp->v_data = 0;
552
553         return (0);
554 }
555
556 struct vop_vector portal_vnodeops = {
557         .vop_default =          &default_vnodeops,
558
559         .vop_access =           VOP_NULL,
560         .vop_getattr =          portal_getattr,
561         .vop_lookup =           portal_lookup,
562         .vop_open =             portal_open,
563         .vop_pathconf =         vop_stdpathconf,
564         .vop_readdir =          portal_readdir,
565         .vop_reclaim =          portal_reclaim,
566         .vop_setattr =          portal_setattr,
567 };