Initial import from FreeBSD RELENG_4:
[games.git] / sys / vfs / portal / 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  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by the University of
19  *      California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *      @(#)portal_vnops.c      8.14 (Berkeley) 5/21/95
37  *
38  * $FreeBSD: src/sys/miscfs/portal/portal_vnops.c,v 1.38 1999/12/21 06:29:00 chris Exp $
39  */
40
41 /*
42  * Portal Filesystem
43  */
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/sysproto.h>
48 #include <sys/kernel.h>
49 #include <sys/time.h>
50 #include <sys/proc.h>
51 #include <sys/filedesc.h>
52 #include <sys/vnode.h>
53 #include <sys/fcntl.h>
54 #include <sys/file.h>
55 #include <sys/stat.h>
56 #include <sys/mount.h>
57 #include <sys/malloc.h>
58 #include <sys/namei.h>
59 #include <sys/mbuf.h>
60 #include <sys/socket.h>
61 #include <sys/socketvar.h>
62 #include <sys/un.h>
63 #include <sys/unpcb.h>
64 #include <miscfs/portal/portal.h>
65
66 static int portal_fileid = PORTAL_ROOTFILEID+1;
67
68 static int      portal_badop __P((void));
69 static void     portal_closefd __P((struct proc *p, int fd));
70 static int      portal_connect __P((struct socket *so, struct socket *so2));
71 static int      portal_getattr __P((struct vop_getattr_args *ap));
72 static int      portal_inactive __P((struct vop_inactive_args *ap));
73 static int      portal_lookup __P((struct vop_lookup_args *ap));
74 static int      portal_open __P((struct vop_open_args *ap));
75 static int      portal_print __P((struct vop_print_args *ap));
76 static int      portal_readdir __P((struct vop_readdir_args *ap));
77 static int      portal_reclaim __P((struct vop_reclaim_args *ap));
78 static int      portal_setattr __P((struct vop_setattr_args *ap));
79
80 static void
81 portal_closefd(p, fd)
82         struct proc *p;
83         int fd;
84 {
85         int error;
86         struct close_args ua;
87
88         ua.fd = fd;
89         error = close(p, &ua);
90         /*
91          * We should never get an error, and there isn't anything
92          * we could do if we got one, so just print a message.
93          */
94         if (error)
95                 printf("portal_closefd: error = %d\n", error);
96 }
97
98 /*
99  * vp is the current namei directory
100  * cnp is the name to locate in that directory...
101  */
102 static int
103 portal_lookup(ap)
104         struct vop_lookup_args /* {
105                 struct vnode * a_dvp;
106                 struct vnode ** a_vpp;
107                 struct componentname * a_cnp;
108         } */ *ap;
109 {
110         struct componentname *cnp = ap->a_cnp;
111         struct vnode **vpp = ap->a_vpp;
112         struct vnode *dvp = ap->a_dvp;
113         char *pname = cnp->cn_nameptr;
114         struct portalnode *pt;
115         int error;
116         struct vnode *fvp = 0;
117         char *path;
118         int size;
119
120         *vpp = NULLVP;
121
122         if (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)
123                 return (EROFS);
124
125         if (cnp->cn_namelen == 1 && *pname == '.') {
126                 *vpp = dvp;
127                 VREF(dvp);
128                 /*VOP_LOCK(dvp);*/
129                 return (0);
130         }
131
132         /*
133          * Do the MALLOC before the getnewvnode since doing so afterward
134          * might cause a bogus v_data pointer to get dereferenced
135          * elsewhere if MALLOC should block.
136          */
137         MALLOC(pt, struct portalnode *, sizeof(struct portalnode),
138                 M_TEMP, M_WAITOK);
139
140         error = getnewvnode(VT_PORTAL, dvp->v_mount, portal_vnodeop_p, &fvp);
141         if (error) {
142                 FREE(pt, M_TEMP);
143                 goto bad;
144         }
145         fvp->v_type = VREG;
146         fvp->v_data = pt;
147         /*
148          * Save all of the remaining pathname and
149          * advance the namei next pointer to the end
150          * of the string.
151          */
152         for (size = 0, path = pname; *path; path++)
153                 size++;
154         cnp->cn_consume = size - cnp->cn_namelen;
155
156         pt->pt_arg = malloc(size+1, M_TEMP, M_WAITOK);
157         pt->pt_size = size+1;
158         bcopy(pname, pt->pt_arg, pt->pt_size);
159         pt->pt_fileid = portal_fileid++;
160
161         *vpp = fvp;
162         /*VOP_LOCK(fvp);*/
163         return (0);
164
165 bad:;
166         if (fvp)
167                 vrele(fvp);
168         return (error);
169 }
170
171 static int
172 portal_connect(so, so2)
173         struct socket *so;
174         struct socket *so2;
175 {
176         /* from unp_connect, bypassing the namei stuff... */
177         struct socket *so3;
178         struct unpcb *unp2;
179         struct unpcb *unp3;
180
181         if (so2 == 0)
182                 return (ECONNREFUSED);
183
184         if (so->so_type != so2->so_type)
185                 return (EPROTOTYPE);
186
187         if ((so2->so_options & SO_ACCEPTCONN) == 0)
188                 return (ECONNREFUSED);
189
190         if ((so3 = sonewconn(so2, 0)) == 0)
191                 return (ECONNREFUSED);
192
193         unp2 = sotounpcb(so2);
194         unp3 = sotounpcb(so3);
195         if (unp2->unp_addr)
196                 unp3->unp_addr = (struct sockaddr_un *)
197                         dup_sockaddr((struct sockaddr *)unp2->unp_addr, 0);
198         so2 = so3;
199
200         return (unp_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 proc *a_p;
210         } */ *ap;
211 {
212         struct socket *so = 0;
213         struct portalnode *pt;
214         struct proc *p = ap->a_p;
215         struct vnode *vp = ap->a_vp;
216         int s;
217         struct uio auio;
218         struct iovec aiov[2];
219         int res;
220         struct mbuf *cm = 0;
221         struct cmsghdr *cmsg;
222         int newfds;
223         int *ip;
224         int fd;
225         int error;
226         int len;
227         struct portalmount *fmp;
228         struct file *fp;
229         struct portal_cred pcred;
230
231         /*
232          * Nothing to do when opening the root node.
233          */
234         if (vp->v_flag & VROOT)
235                 return (0);
236
237         /*
238          * Can't be opened unless the caller is set up
239          * to deal with the side effects.  Check for this
240          * by testing whether the p_dupfd has been set.
241          */
242         if (p->p_dupfd >= 0)
243                 return (ENODEV);
244
245         pt = VTOPORTAL(vp);
246         fmp = VFSTOPORTAL(vp->v_mount);
247
248         /*
249          * Create a new socket.
250          */
251         error = socreate(AF_UNIX, &so, SOCK_STREAM, 0, ap->a_p);
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, (struct socket *)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         s = splnet();
284         while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
285                 if (fmp->pm_server->f_count == 1) {
286                         error = ECONNREFUSED;
287                         splx(s);
288                         goto bad;
289                 }
290                 (void) tsleep((caddr_t) &so->so_timeo, PSOCK, "portalcon", 5 * hz);
291         }
292         splx(s);
293
294         if (so->so_error) {
295                 error = so->so_error;
296                 goto bad;
297         }
298
299         /*
300          * Set miscellaneous flags
301          */
302         so->so_rcv.sb_timeo = 0;
303         so->so_snd.sb_timeo = 0;
304         so->so_rcv.sb_flags |= SB_NOINTR;
305         so->so_snd.sb_flags |= SB_NOINTR;
306
307
308         pcred.pcr_flag = ap->a_mode;
309         pcred.pcr_uid = ap->a_cred->cr_uid;
310         pcred.pcr_ngroups = ap->a_cred->cr_ngroups;
311         bcopy(ap->a_cred->cr_groups, pcred.pcr_groups, NGROUPS * sizeof(gid_t));
312         aiov[0].iov_base = (caddr_t) &pcred;
313         aiov[0].iov_len = sizeof(pcred);
314         aiov[1].iov_base = pt->pt_arg;
315         aiov[1].iov_len = pt->pt_size;
316         auio.uio_iov = aiov;
317         auio.uio_iovcnt = 2;
318         auio.uio_rw = UIO_WRITE;
319         auio.uio_segflg = UIO_SYSSPACE;
320         auio.uio_procp = p;
321         auio.uio_offset = 0;
322         auio.uio_resid = aiov[0].iov_len + aiov[1].iov_len;
323
324         error = sosend(so, (struct sockaddr *) 0, &auio,
325                         (struct mbuf *) 0, (struct mbuf *) 0, 0, p);
326         if (error)
327                 goto bad;
328
329         len = auio.uio_resid = sizeof(int);
330         do {
331                 struct mbuf *m = 0;
332                 int flags = MSG_WAITALL;
333                 error = soreceive(so, (struct sockaddr **) 0, &auio,
334                                         &m, &cm, &flags);
335                 if (error)
336                         goto bad;
337
338                 /*
339                  * Grab an error code from the mbuf.
340                  */
341                 if (m) {
342                         m = m_pullup(m, sizeof(int));   /* Needed? */
343                         if (m) {
344                                 error = *(mtod(m, int *));
345                                 m_freem(m);
346                         } else {
347                                 error = EINVAL;
348                         }
349                 } else {
350                         if (cm == 0) {
351                                 error = ECONNRESET;      /* XXX */
352 #ifdef notdef
353                                 break;
354 #endif
355                         }
356                 }
357         } while (cm == 0 && auio.uio_resid == len && !error);
358
359         if (cm == 0)
360                 goto bad;
361
362         if (auio.uio_resid) {
363                 error = 0;
364 #ifdef notdef
365                 error = EMSGSIZE;
366                 goto bad;
367 #endif
368         }
369
370         /*
371          * XXX: Break apart the control message, and retrieve the
372          * received file descriptor.  Note that more than one descriptor
373          * may have been received, or that the rights chain may have more
374          * than a single mbuf in it.  What to do?
375          */
376         cmsg = mtod(cm, struct cmsghdr *);
377         newfds = (cmsg->cmsg_len - sizeof(*cmsg)) / sizeof (int);
378         if (newfds == 0) {
379                 error = ECONNREFUSED;
380                 goto bad;
381         }
382         /*
383          * At this point the rights message consists of a control message
384          * header, followed by a data region containing a vector of
385          * integer file descriptors.  The fds were allocated by the action
386          * of receiving the control message.
387          */
388         ip = (int *) (cmsg + 1);
389         fd = *ip++;
390         if (newfds > 1) {
391                 /*
392                  * Close extra fds.
393                  */
394                 int i;
395                 printf("portal_open: %d extra fds\n", newfds - 1);
396                 for (i = 1; i < newfds; i++) {
397                         portal_closefd(p, *ip);
398                         ip++;
399                 }
400         }
401
402         /*
403          * Check that the mode the file is being opened for is a subset
404          * of the mode of the existing descriptor.
405          */
406         fp = p->p_fd->fd_ofiles[fd];
407         if (((ap->a_mode & (FREAD|FWRITE)) | fp->f_flag) != fp->f_flag) {
408                 portal_closefd(p, fd);
409                 error = EACCES;
410                 goto bad;
411         }
412
413         /*
414          * Save the dup fd in the proc structure then return the
415          * special error code (ENXIO) which causes magic things to
416          * happen in vn_open.  The whole concept is, well, hmmm.
417          */
418         p->p_dupfd = fd;
419         error = ENXIO;
420
421 bad:;
422         /*
423          * And discard the control message.
424          */
425         if (cm) {
426                 m_freem(cm);
427         }
428
429         if (so) {
430                 soshutdown(so, 2);
431                 soclose(so);
432         }
433         return (error);
434 }
435
436 static int
437 portal_getattr(ap)
438         struct vop_getattr_args /* {
439                 struct vnode *a_vp;
440                 struct vattr *a_vap;
441                 struct ucred *a_cred;
442                 struct proc *a_p;
443         } */ *ap;
444 {
445         struct vnode *vp = ap->a_vp;
446         struct vattr *vap = ap->a_vap;
447
448         bzero(vap, sizeof(*vap));
449         vattr_null(vap);
450         vap->va_uid = 0;
451         vap->va_gid = 0;
452         vap->va_size = DEV_BSIZE;
453         vap->va_blocksize = DEV_BSIZE;
454         nanotime(&vap->va_atime);
455         vap->va_mtime = vap->va_atime;
456         vap->va_ctime = vap->va_mtime;
457         vap->va_gen = 0;
458         vap->va_flags = 0;
459         vap->va_rdev = 0;
460         /* vap->va_qbytes = 0; */
461         vap->va_bytes = 0;
462         /* vap->va_qsize = 0; */
463         if (vp->v_flag & VROOT) {
464                 vap->va_type = VDIR;
465                 vap->va_mode = S_IRUSR|S_IWUSR|S_IXUSR|
466                                 S_IRGRP|S_IWGRP|S_IXGRP|
467                                 S_IROTH|S_IWOTH|S_IXOTH;
468                 vap->va_nlink = 2;
469                 vap->va_fileid = 2;
470         } else {
471                 vap->va_type = VREG;
472                 vap->va_mode = S_IRUSR|S_IWUSR|
473                                 S_IRGRP|S_IWGRP|
474                                 S_IROTH|S_IWOTH;
475                 vap->va_nlink = 1;
476                 vap->va_fileid = VTOPORTAL(vp)->pt_fileid;
477         }
478         return (0);
479 }
480
481 static int
482 portal_setattr(ap)
483         struct vop_setattr_args /* {
484                 struct vnode *a_vp;
485                 struct vattr *a_vap;
486                 struct ucred *a_cred;
487                 struct proc *a_p;
488         } */ *ap;
489 {
490
491         /*
492          * Can't mess with the root vnode
493          */
494         if (ap->a_vp->v_flag & VROOT)
495                 return (EACCES);
496
497         if (ap->a_vap->va_flags != VNOVAL)
498                 return (EOPNOTSUPP);
499
500         return (0);
501 }
502
503 /*
504  * Fake readdir, just return empty directory.
505  * It is hard to deal with '.' and '..' so don't bother.
506  */
507 static int
508 portal_readdir(ap)
509         struct vop_readdir_args /* {
510                 struct vnode *a_vp;
511                 struct uio *a_uio;
512                 struct ucred *a_cred;
513                 int *a_eofflag;
514                 u_long *a_cookies;
515                 int a_ncookies;
516         } */ *ap;
517 {
518
519         /*
520          * We don't allow exporting portal mounts, and currently local
521          * requests do not need cookies.
522          */
523         if (ap->a_ncookies)
524                 panic("portal_readdir: not hungry");
525
526         return (0);
527 }
528
529 static int
530 portal_inactive(ap)
531         struct vop_inactive_args /* {
532                 struct vnode *a_vp;
533                 struct proc *a_p;
534         } */ *ap;
535 {
536
537         VOP_UNLOCK(ap->a_vp, 0, ap->a_p);
538         return (0);
539 }
540
541 static int
542 portal_reclaim(ap)
543         struct vop_reclaim_args /* {
544                 struct vnode *a_vp;
545         } */ *ap;
546 {
547         struct portalnode *pt = VTOPORTAL(ap->a_vp);
548
549         if (pt->pt_arg) {
550                 free((caddr_t) pt->pt_arg, M_TEMP);
551                 pt->pt_arg = 0;
552         }
553         FREE(ap->a_vp->v_data, M_TEMP);
554         ap->a_vp->v_data = 0;
555
556         return (0);
557 }
558
559
560 /*
561  * Print out the contents of a Portal vnode.
562  */
563 /* ARGSUSED */
564 static int
565 portal_print(ap)
566         struct vop_print_args /* {
567                 struct vnode *a_vp;
568         } */ *ap;
569 {
570
571         printf("tag VT_PORTAL, portal vnode\n");
572         return (0);
573 }
574
575
576 /*
577  * Portal "should never get here" operation
578  */
579 static int
580 portal_badop()
581 {
582
583         panic("portal: bad op");
584         /* NOTREACHED */
585 }
586
587 vop_t **portal_vnodeop_p;
588 static struct vnodeopv_entry_desc portal_vnodeop_entries[] = {
589         { &vop_default_desc,            (vop_t *) vop_defaultop },
590         { &vop_access_desc,             (vop_t *) vop_null },
591         { &vop_bmap_desc,               (vop_t *) portal_badop },
592         { &vop_getattr_desc,            (vop_t *) portal_getattr },
593         { &vop_inactive_desc,           (vop_t *) portal_inactive },
594         { &vop_lookup_desc,             (vop_t *) portal_lookup },
595         { &vop_open_desc,               (vop_t *) portal_open },
596         { &vop_pathconf_desc,           (vop_t *) vop_stdpathconf },
597         { &vop_print_desc,              (vop_t *) portal_print },
598         { &vop_readdir_desc,            (vop_t *) portal_readdir },
599         { &vop_reclaim_desc,            (vop_t *) portal_reclaim },
600         { &vop_setattr_desc,            (vop_t *) portal_setattr },
601         { NULL, NULL }
602 };
603 static struct vnodeopv_desc portal_vnodeop_opv_desc =
604         { &portal_vnodeop_p, portal_vnodeop_entries };
605
606 VNODEOP_SET(portal_vnodeop_opv_desc);