0a2673902f38381c2ed2e713b97e5131ed01a5d8
[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  */
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/resourcevar.h>
61 #include <sys/socket.h>
62 #include <sys/socketvar.h>
63 #include <sys/un.h>
64 #include <sys/unpcb.h>
65 #include "portal.h"
66
67 #include <sys/thread2.h>
68
69 static int portal_fileid = PORTAL_ROOTFILEID+1;
70
71 static int      portal_badop (void);
72 static void     portal_closefd (struct thread *td, int fd);
73 static int      portal_connect (struct socket *so, struct socket *so2);
74 static int      portal_getattr (struct vop_getattr_args *ap);
75 static int      portal_inactive (struct vop_inactive_args *ap);
76 static int      portal_lookup (struct vop_old_lookup_args *ap);
77 static int      portal_open (struct vop_open_args *ap);
78 static int      portal_print (struct vop_print_args *ap);
79 static int      portal_readdir (struct vop_readdir_args *ap);
80 static int      portal_reclaim (struct vop_reclaim_args *ap);
81 static int      portal_setattr (struct vop_setattr_args *ap);
82
83 static void
84 portal_closefd(struct thread *td, int fd)
85 {
86         int error;
87         struct close_args ua;
88
89         ua.fd = fd;
90         error = sys_close(&ua);
91         /*
92          * We should never get an error, and there isn't anything
93          * we could do if we got one, so just print a message.
94          */
95         if (error)
96                 kprintf("portal_closefd: error = %d\n", error);
97 }
98
99 /*
100  * vp is the current namei directory
101  * cnp is the name to locate in that directory...
102  *
103  * portal_lookup(struct vnode *a_dvp, struct vnode **a_vpp,
104  *               struct componentname *a_cnp)
105  */
106 static int
107 portal_lookup(struct vop_old_lookup_args *ap)
108 {
109         struct componentname *cnp = ap->a_cnp;
110         struct vnode **vpp = ap->a_vpp;
111         struct vnode *dvp = ap->a_dvp;
112         char *pname = cnp->cn_nameptr;
113         struct portalnode *pt;
114         int error;
115         struct vnode *fvp = NULL;
116         char *path;
117         int size;
118
119         *vpp = NULLVP;
120
121         if (cnp->cn_nameiop == NAMEI_DELETE || cnp->cn_nameiop == NAMEI_RENAME)
122                 return (EROFS);
123
124         if (cnp->cn_namelen == 1 && *pname == '.') {
125                 *vpp = dvp;
126                 vref(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         pt = kmalloc(sizeof(struct portalnode), M_TEMP, M_WAITOK);
136
137         error = getnewvnode(VT_PORTAL, dvp->v_mount, &fvp, 0, 0);
138         if (error) {
139                 kfree(pt, M_TEMP);
140                 goto bad;
141         }
142         fvp->v_type = VREG;
143         fvp->v_data = pt;
144
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 = kmalloc(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         return (0);
162
163 bad:;
164         if (fvp)
165                 vrele(fvp);
166         return (error);
167 }
168
169 static int
170 portal_connect(struct socket *so, struct socket *so2)
171 {
172         /* from unp_connect, bypassing the namei stuff... */
173         struct socket *so3;
174         struct unpcb *unp2;
175         struct unpcb *unp3;
176
177         if (so2 == NULL)
178                 return (ECONNREFUSED);
179
180         if (so->so_type != so2->so_type)
181                 return (EPROTOTYPE);
182
183         if ((so2->so_options & SO_ACCEPTCONN) == 0)
184                 return (ECONNREFUSED);
185
186         if ((so3 = sonewconn(so2, 0)) == NULL)
187                 return (ECONNREFUSED);
188
189         unp2 = so2->so_pcb;
190         unp3 = so3->so_pcb;
191         if (unp2->unp_addr)
192                 unp3->unp_addr = (struct sockaddr_un *)
193                         dup_sockaddr((struct sockaddr *)unp2->unp_addr);
194         so2 = so3;
195
196         return (unp_connect2(so, so2));
197 }
198
199 /*
200  * portal_open(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
201  *             struct file *a_fp)
202  */
203 static int
204 portal_open(struct vop_open_args *ap)
205 {
206         struct socket *so = NULL;
207         struct portalnode *pt;
208         struct thread *td = curthread;
209         struct vnode *vp = ap->a_vp;
210         struct uio auio;
211         struct iovec aiov[2];
212         struct sockbuf sio;
213         int res;
214         struct mbuf *cm = NULL;
215         struct cmsghdr *cmsg;
216         int newfds;
217         int *ip;
218         int fd;
219         int error;
220         int len;
221         struct portalmount *fmp;
222         struct file *fp;
223         struct portal_cred pcred;
224
225         /*
226          * Nothing to do when opening the root node.
227          */
228         if (vp->v_flag & VROOT)
229                 return (vop_stdopen(ap));
230
231         /*
232          * Can't be opened unless the caller is set up
233          * to deal with the side effects.  Check for this
234          * by testing whether the p_dupfd has been set.
235          */
236         KKASSERT(td->td_proc);
237         if (td->td_lwp->lwp_dupfd >= 0)
238                 return (ENODEV);
239
240         pt = VTOPORTAL(vp);
241         fmp = VFSTOPORTAL(vp->v_mount);
242
243         /*
244          * Create a new socket.
245          */
246         error = socreate(AF_UNIX, &so, SOCK_STREAM, 0, td);
247         if (error)
248                 goto bad;
249
250         /*
251          * Reserve some buffer space
252          */
253         res = pt->pt_size + sizeof(pcred) + 512;        /* XXX */
254         error = soreserve(so, res, res, &td->td_proc->p_rlimit[RLIMIT_SBSIZE]);
255         if (error)
256                 goto bad;
257
258         /*
259          * Kick off connection
260          */
261         error = portal_connect(so, (struct socket *)fmp->pm_server->f_data);
262         if (error)
263                 goto bad;
264
265         /*
266          * Wait for connection to complete
267          */
268         /*
269          * XXX: Since the mount point is holding a reference on the
270          * underlying server socket, it is not easy to find out whether
271          * the server process is still running.  To handle this problem
272          * we loop waiting for the new socket to be connected (something
273          * which will only happen if the server is still running) or for
274          * the reference count on the server socket to drop to 1, which
275          * will happen if the server dies.  Sleep for 5 second intervals
276          * and keep polling the reference count.   XXX.
277          */
278         crit_enter();
279         while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
280                 if (fmp->pm_server->f_count == 1) {
281                         error = ECONNREFUSED;
282                         crit_exit();
283                         goto bad;
284                 }
285                 (void) tsleep((caddr_t) &so->so_timeo, 0, "portalcon", 5 * hz);
286         }
287         crit_exit();
288
289         if (so->so_error) {
290                 error = so->so_error;
291                 goto bad;
292         }
293
294         /*
295          * Set miscellaneous flags
296          */
297         so->so_rcv.ssb_timeo = 0;
298         so->so_snd.ssb_timeo = 0;
299         atomic_set_int(&so->so_rcv.ssb_flags, SSB_NOINTR);
300         atomic_set_int(&so->so_snd.ssb_flags, SSB_NOINTR);
301
302
303         pcred.pcr_flag = ap->a_mode;
304         pcred.pcr_uid = ap->a_cred->cr_uid;
305         pcred.pcr_ngroups = ap->a_cred->cr_ngroups;
306         bcopy(ap->a_cred->cr_groups, pcred.pcr_groups, NGROUPS * sizeof(gid_t));
307         aiov[0].iov_base = (caddr_t) &pcred;
308         aiov[0].iov_len = sizeof(pcred);
309         aiov[1].iov_base = pt->pt_arg;
310         aiov[1].iov_len = pt->pt_size;
311         auio.uio_iov = aiov;
312         auio.uio_iovcnt = 2;
313         auio.uio_rw = UIO_WRITE;
314         auio.uio_segflg = UIO_SYSSPACE;
315         auio.uio_td = td;
316         auio.uio_offset = 0;
317         auio.uio_resid = aiov[0].iov_len + aiov[1].iov_len;
318
319         error = sosend(so, NULL, &auio, NULL, NULL, 0, td);
320         if (error)
321                 goto bad;
322
323         len = sizeof(int);
324         sbinit(&sio, len);
325         do {
326                 struct mbuf *m;
327                 int flags;
328
329                 flags = MSG_WAITALL;
330                 error = soreceive(so, NULL, NULL, &sio, &cm, &flags);
331                 if (error)
332                         goto bad;
333
334                 /*
335                  * Grab an error code from the mbuf.
336                  */
337                 if ((m = sio.sb_mb) != NULL) {
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 == NULL) {
347                                 error = ECONNRESET;      /* XXX */
348 #ifdef notdef
349                                 break;
350 #endif
351                         }
352                 }
353         } while (cm == NULL && sio.sb_cc == 0 && !error);
354
355         if (cm == NULL)
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                 kprintf("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_files[fd].fp;
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_lwp->lwp_dupfd = fd;
416         vop_stdopen(ap);
417         error = ENXIO;
418
419 bad:;
420         /*
421          * And discard the control message.
422          */
423         if (cm) {
424                 m_freem(cm);
425         }
426
427         if (so) {
428                 soshutdown(so, SHUT_RDWR);
429                 soclose(so, FNONBLOCK);
430         }
431         return (error);
432 }
433
434 /*
435  * portal_getattr(struct vnode *a_vp, struct vattr *a_vap)
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_rmajor = VNOVAL;
455         vap->va_rminor = VNOVAL;
456         /* vap->va_qbytes = 0; */
457         vap->va_bytes = 0;
458         /* vap->va_qsize = 0; */
459         if (vp->v_flag & VROOT) {
460                 vap->va_type = VDIR;
461                 vap->va_mode = S_IRUSR|S_IWUSR|S_IXUSR|
462                                 S_IRGRP|S_IWGRP|S_IXGRP|
463                                 S_IROTH|S_IWOTH|S_IXOTH;
464                 vap->va_nlink = 2;
465                 vap->va_fileid = 2;
466         } else {
467                 vap->va_type = VREG;
468                 vap->va_mode = S_IRUSR|S_IWUSR|
469                                 S_IRGRP|S_IWGRP|
470                                 S_IROTH|S_IWOTH;
471                 vap->va_nlink = 1;
472                 vap->va_fileid = VTOPORTAL(vp)->pt_fileid;
473         }
474         return (0);
475 }
476
477 /*
478  * portal_setattr(struct vnode *a_vp, struct vattr *a_vap,
479  *                struct ucred *a_cred)
480  */
481 static int
482 portal_setattr(struct vop_setattr_args *ap)
483 {
484         /*
485          * Can't mess with the root vnode
486          */
487         if (ap->a_vp->v_flag & VROOT)
488                 return (EACCES);
489
490         if (ap->a_vap->va_flags != VNOVAL)
491                 return (EOPNOTSUPP);
492
493         return (0);
494 }
495
496 /*
497  * Fake readdir, just return empty directory.
498  * It is hard to deal with '.' and '..' so don't bother.
499  *
500  * portal_readdir(struct vnode *a_vp, struct uio *a_uio,
501  *                struct ucred *a_cred, int *a_eofflag,
502  *                off_t *a_cookies, int a_ncookies)
503  */
504 static int
505 portal_readdir(struct vop_readdir_args *ap)
506 {
507         /*
508          * We don't allow exporting portal mounts, and currently local
509          * requests do not need cookies.
510          */
511         if (ap->a_ncookies)
512                 panic("portal_readdir: not hungry");
513
514         return (0);
515 }
516
517 /*
518  * portal_inactive(struct vnode *a_vp)
519  */
520 static int
521 portal_inactive(struct vop_inactive_args *ap)
522 {
523         return (0);
524 }
525
526 /*
527  * portal_reclaim(struct vnode *a_vp)
528  */
529 static int
530 portal_reclaim(struct vop_reclaim_args *ap)
531 {
532         struct portalnode *pt = VTOPORTAL(ap->a_vp);
533
534         if (pt->pt_arg) {
535                 kfree((caddr_t) pt->pt_arg, M_TEMP);
536                 pt->pt_arg = 0;
537         }
538         kfree(ap->a_vp->v_data, M_TEMP);
539         ap->a_vp->v_data = 0;
540
541         return (0);
542 }
543
544
545 /*
546  * Print out the contents of a Portal vnode.
547  *
548  * portal_print(struct vnode *a_vp)
549  */
550 /* ARGSUSED */
551 static int
552 portal_print(struct vop_print_args *ap)
553 {
554         kprintf("tag VT_PORTAL, portal vnode\n");
555         return (0);
556 }
557
558
559 /*
560  * Portal "should never get here" operation
561  */
562 static int
563 portal_badop(void)
564 {
565
566         panic("portal: bad op");
567         /* NOTREACHED */
568 }
569
570 struct vop_ops portal_vnode_vops = {
571         .vop_default =          vop_defaultop,
572         .vop_access =           (void *)vop_null,
573         .vop_bmap =             (void *)portal_badop,
574         .vop_getattr =          portal_getattr,
575         .vop_inactive =         portal_inactive,
576         .vop_old_lookup =       portal_lookup,
577         .vop_open =             portal_open,
578         .vop_pathconf =         vop_stdpathconf,
579         .vop_print =            portal_print,
580         .vop_readdir =          portal_readdir,
581         .vop_reclaim =          portal_reclaim,
582         .vop_setattr =          portal_setattr
583 };
584