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