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