2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software donated to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
36 * @(#)portal_vnops.c 8.14 (Berkeley) 5/21/95
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.8 2003/09/23 05:03:53 dillon Exp $
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/sysproto.h>
49 #include <sys/kernel.h>
52 #include <sys/filedesc.h>
53 #include <sys/vnode.h>
54 #include <sys/fcntl.h>
57 #include <sys/mount.h>
58 #include <sys/malloc.h>
59 #include <sys/namei.h>
61 #include <sys/socket.h>
62 #include <sys/socketvar.h>
64 #include <sys/unpcb.h>
67 static int portal_fileid = PORTAL_ROOTFILEID+1;
69 static int portal_badop (void);
70 static void portal_closefd (struct thread *td, int fd);
71 static int portal_connect (struct socket *so, struct socket *so2);
72 static int portal_getattr (struct vop_getattr_args *ap);
73 static int portal_inactive (struct vop_inactive_args *ap);
74 static int portal_lookup (struct vop_lookup_args *ap);
75 static int portal_open (struct vop_open_args *ap);
76 static int portal_print (struct vop_print_args *ap);
77 static int portal_readdir (struct vop_readdir_args *ap);
78 static int portal_reclaim (struct vop_reclaim_args *ap);
79 static int portal_setattr (struct vop_setattr_args *ap);
82 portal_closefd(td, fd)
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.
96 printf("portal_closefd: error = %d\n", error);
100 * vp is the current namei directory
101 * cnp is the name to locate in that directory...
105 struct vop_lookup_args /* {
106 struct vnode * a_dvp;
107 struct vnode ** a_vpp;
108 struct componentname * a_cnp;
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;
117 struct vnode *fvp = 0;
123 if (cnp->cn_nameiop == NAMEI_DELETE || cnp->cn_nameiop == NAMEI_RENAME)
126 if (cnp->cn_namelen == 1 && *pname == '.') {
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.
138 MALLOC(pt, struct portalnode *, sizeof(struct portalnode),
141 error = getnewvnode(VT_PORTAL, dvp->v_mount, portal_vnodeop_p, &fvp);
149 * Save all of the remaining pathname and
150 * advance the namei next pointer to the end
153 for (size = 0, path = pname; *path; path++)
155 cnp->cn_consume = size - cnp->cn_namelen;
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++;
173 portal_connect(so, so2)
177 /* from unp_connect, bypassing the namei stuff... */
183 return (ECONNREFUSED);
185 if (so->so_type != so2->so_type)
188 if ((so2->so_options & SO_ACCEPTCONN) == 0)
189 return (ECONNREFUSED);
191 if ((so3 = sonewconn(so2, 0)) == 0)
192 return (ECONNREFUSED);
194 unp2 = sotounpcb(so2);
195 unp3 = sotounpcb(so3);
197 unp3->unp_addr = (struct sockaddr_un *)
198 dup_sockaddr((struct sockaddr *)unp2->unp_addr, 0);
201 return (unp_connect2(so, so2));
206 struct vop_open_args /* {
209 struct ucred *a_cred;
213 struct socket *so = 0;
214 struct portalnode *pt;
215 struct thread *td = ap->a_td;
216 struct vnode *vp = ap->a_vp;
219 struct iovec aiov[2];
222 struct cmsghdr *cmsg;
228 struct portalmount *fmp;
230 struct portal_cred pcred;
233 * Nothing to do when opening the root node.
235 if (vp->v_flag & VROOT)
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.
243 KKASSERT(td->td_proc);
244 if (td->td_proc->p_dupfd >= 0)
248 fmp = VFSTOPORTAL(vp->v_mount);
251 * Create a new socket.
253 error = socreate(AF_UNIX, &so, SOCK_STREAM, 0, ap->a_td);
258 * Reserve some buffer space
260 res = pt->pt_size + sizeof(pcred) + 512; /* XXX */
261 error = soreserve(so, res, res);
266 * Kick off connection
268 error = portal_connect(so, (struct socket *)fmp->pm_server->f_data);
273 * Wait for connection to complete
276 * XXX: Since the mount point is holding a reference on the
277 * underlying server socket, it is not easy to find out whether
278 * the server process is still running. To handle this problem
279 * we loop waiting for the new socket to be connected (something
280 * which will only happen if the server is still running) or for
281 * the reference count on the server socket to drop to 1, which
282 * will happen if the server dies. Sleep for 5 second intervals
283 * and keep polling the reference count. XXX.
286 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
287 if (fmp->pm_server->f_count == 1) {
288 error = ECONNREFUSED;
292 (void) tsleep((caddr_t) &so->so_timeo, 0, "portalcon", 5 * hz);
297 error = so->so_error;
302 * Set miscellaneous flags
304 so->so_rcv.sb_timeo = 0;
305 so->so_snd.sb_timeo = 0;
306 so->so_rcv.sb_flags |= SB_NOINTR;
307 so->so_snd.sb_flags |= SB_NOINTR;
310 pcred.pcr_flag = ap->a_mode;
311 pcred.pcr_uid = ap->a_cred->cr_uid;
312 pcred.pcr_ngroups = ap->a_cred->cr_ngroups;
313 bcopy(ap->a_cred->cr_groups, pcred.pcr_groups, NGROUPS * sizeof(gid_t));
314 aiov[0].iov_base = (caddr_t) &pcred;
315 aiov[0].iov_len = sizeof(pcred);
316 aiov[1].iov_base = pt->pt_arg;
317 aiov[1].iov_len = pt->pt_size;
320 auio.uio_rw = UIO_WRITE;
321 auio.uio_segflg = UIO_SYSSPACE;
324 auio.uio_resid = aiov[0].iov_len + aiov[1].iov_len;
326 error = sosend(so, (struct sockaddr *) 0, &auio,
327 (struct mbuf *) 0, (struct mbuf *) 0, 0, td);
331 len = auio.uio_resid = sizeof(int);
334 int flags = MSG_WAITALL;
335 error = soreceive(so, (struct sockaddr **) 0, &auio,
341 * Grab an error code from the mbuf.
344 m = m_pullup(m, sizeof(int)); /* Needed? */
346 error = *(mtod(m, int *));
353 error = ECONNRESET; /* XXX */
359 } while (cm == 0 && auio.uio_resid == len && !error);
364 if (auio.uio_resid) {
373 * XXX: Break apart the control message, and retrieve the
374 * received file descriptor. Note that more than one descriptor
375 * may have been received, or that the rights chain may have more
376 * than a single mbuf in it. What to do?
378 cmsg = mtod(cm, struct cmsghdr *);
379 newfds = (cmsg->cmsg_len - sizeof(*cmsg)) / sizeof (int);
381 error = ECONNREFUSED;
385 * At this point the rights message consists of a control message
386 * header, followed by a data region containing a vector of
387 * integer file descriptors. The fds were allocated by the action
388 * of receiving the control message.
390 ip = (int *) (cmsg + 1);
397 printf("portal_open: %d extra fds\n", newfds - 1);
398 for (i = 1; i < newfds; i++) {
399 portal_closefd(td, *ip);
405 * Check that the mode the file is being opened for is a subset
406 * of the mode of the existing descriptor.
408 KKASSERT(td->td_proc);
409 fp = td->td_proc->p_fd->fd_ofiles[fd];
410 if (((ap->a_mode & (FREAD|FWRITE)) | fp->f_flag) != fp->f_flag) {
411 portal_closefd(td, fd);
417 * Save the dup fd in the proc structure then return the
418 * special error code (ENXIO) which causes magic things to
419 * happen in vn_open. The whole concept is, well, hmmm.
421 td->td_proc->p_dupfd = fd;
426 * And discard the control message.
441 struct vop_getattr_args /* {
444 struct ucred *a_cred;
448 struct vnode *vp = ap->a_vp;
449 struct vattr *vap = ap->a_vap;
451 bzero(vap, sizeof(*vap));
455 vap->va_size = DEV_BSIZE;
456 vap->va_blocksize = DEV_BSIZE;
457 nanotime(&vap->va_atime);
458 vap->va_mtime = vap->va_atime;
459 vap->va_ctime = vap->va_mtime;
463 /* vap->va_qbytes = 0; */
465 /* vap->va_qsize = 0; */
466 if (vp->v_flag & VROOT) {
468 vap->va_mode = S_IRUSR|S_IWUSR|S_IXUSR|
469 S_IRGRP|S_IWGRP|S_IXGRP|
470 S_IROTH|S_IWOTH|S_IXOTH;
475 vap->va_mode = S_IRUSR|S_IWUSR|
479 vap->va_fileid = VTOPORTAL(vp)->pt_fileid;
486 struct vop_setattr_args /* {
489 struct ucred *a_cred;
495 * Can't mess with the root vnode
497 if (ap->a_vp->v_flag & VROOT)
500 if (ap->a_vap->va_flags != VNOVAL)
507 * Fake readdir, just return empty directory.
508 * It is hard to deal with '.' and '..' so don't bother.
512 struct vop_readdir_args /* {
515 struct ucred *a_cred;
523 * We don't allow exporting portal mounts, and currently local
524 * requests do not need cookies.
527 panic("portal_readdir: not hungry");
534 struct vop_inactive_args /* {
540 VOP_UNLOCK(ap->a_vp, 0, ap->a_td);
546 struct vop_reclaim_args /* {
550 struct portalnode *pt = VTOPORTAL(ap->a_vp);
553 free((caddr_t) pt->pt_arg, M_TEMP);
556 FREE(ap->a_vp->v_data, M_TEMP);
557 ap->a_vp->v_data = 0;
564 * Print out the contents of a Portal vnode.
569 struct vop_print_args /* {
574 printf("tag VT_PORTAL, portal vnode\n");
580 * Portal "should never get here" operation
586 panic("portal: bad op");
590 vop_t **portal_vnodeop_p;
591 static struct vnodeopv_entry_desc portal_vnodeop_entries[] = {
592 { &vop_default_desc, (vop_t *) vop_defaultop },
593 { &vop_access_desc, (vop_t *) vop_null },
594 { &vop_bmap_desc, (vop_t *) portal_badop },
595 { &vop_getattr_desc, (vop_t *) portal_getattr },
596 { &vop_inactive_desc, (vop_t *) portal_inactive },
597 { &vop_lookup_desc, (vop_t *) portal_lookup },
598 { &vop_open_desc, (vop_t *) portal_open },
599 { &vop_pathconf_desc, (vop_t *) vop_stdpathconf },
600 { &vop_print_desc, (vop_t *) portal_print },
601 { &vop_readdir_desc, (vop_t *) portal_readdir },
602 { &vop_reclaim_desc, (vop_t *) portal_reclaim },
603 { &vop_setattr_desc, (vop_t *) portal_setattr },
606 static struct vnodeopv_desc portal_vnodeop_opv_desc =
607 { &portal_vnodeop_p, portal_vnodeop_entries };
609 VNODEOP_SET(portal_vnodeop_opv_desc);