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