The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/fs/portalfs/portal_vnops.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    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  * 4. Neither the name of the University nor the names of its contributors
   17  *    may be used to endorse or promote products derived from this software
   18  *    without specific prior written permission.
   19  *
   20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   30  * SUCH DAMAGE.
   31  *
   32  *      @(#)portal_vnops.c      8.14 (Berkeley) 5/21/95
   33  *
   34  * $FreeBSD$
   35  */
   36 
   37 /*
   38  * Portal Filesystem
   39  */
   40 
   41 #include <sys/param.h>
   42 #include <sys/fcntl.h>
   43 #include <sys/file.h>
   44 #include <sys/kernel.h>
   45 #include <sys/lock.h>
   46 #include <sys/malloc.h>
   47 #include <sys/mbuf.h>
   48 #include <sys/mount.h>
   49 #include <sys/mutex.h>
   50 #include <sys/namei.h>
   51 #include <sys/proc.h>
   52 #include <sys/socket.h>
   53 #include <sys/socketvar.h>
   54 #include <sys/stat.h>
   55 #include <sys/syscallsubr.h>
   56 #include <sys/systm.h>
   57 #include <sys/un.h>
   58 #include <sys/unpcb.h>
   59 #include <sys/vnode.h>
   60 
   61 #include <fs/portalfs/portal.h>
   62 
   63 #include <net/vnet.h>
   64 
   65 static int portal_fileid = PORTAL_ROOTFILEID+1;
   66 
   67 static void     portal_closefd(struct thread *td, int fd);
   68 static int      portal_connect(struct socket *so, struct socket *so2);
   69 static vop_getattr_t    portal_getattr;
   70 static vop_lookup_t     portal_lookup;
   71 static vop_open_t       portal_open;
   72 static vop_readdir_t    portal_readdir;
   73 static vop_reclaim_t    portal_reclaim;
   74 static vop_setattr_t    portal_setattr;
   75 
   76 static void
   77 portal_closefd(td, fd)
   78         struct thread *td;
   79         int fd;
   80 {
   81         int error;
   82 
   83         error = kern_close(td, fd);
   84         /*
   85          * We should never get an error, and there isn't anything
   86          * we could do if we got one, so just print a message.
   87          */
   88         if (error)
   89                 printf("portal_closefd: error = %d\n", error);
   90 }
   91 
   92 /*
   93  * vp is the current namei directory
   94  * cnp is the name to locate in that directory...
   95  */
   96 static int
   97 portal_lookup(ap)
   98         struct vop_lookup_args /* {
   99                 struct vnode * a_dvp;
  100                 struct vnode ** a_vpp;
  101                 struct componentname * a_cnp;
  102         } */ *ap;
  103 {
  104         struct componentname *cnp = ap->a_cnp;
  105         struct vnode **vpp = ap->a_vpp;
  106         struct vnode *dvp = ap->a_dvp;
  107         char *pname = cnp->cn_nameptr;
  108         struct portalnode *pt;
  109         int error;
  110         struct vnode *fvp = 0;
  111         char *path;
  112         int size;
  113 
  114         *vpp = NULLVP;
  115 
  116         if (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)
  117                 return (EROFS);
  118 
  119         if (cnp->cn_namelen == 1 && *pname == '.') {
  120                 *vpp = dvp;
  121                 VREF(dvp);
  122                 return (0);
  123         }
  124         KASSERT((cnp->cn_flags & ISDOTDOT) == 0,
  125             ("portal_lookup: Can not handle dotdot lookups."));
  126 
  127         /*
  128          * Do the MALLOC before the getnewvnode since doing so afterward
  129          * might cause a bogus v_data pointer to get dereferenced
  130          * elsewhere if MALLOC should block.
  131          */
  132         pt = malloc(sizeof(struct portalnode),
  133                 M_TEMP, M_WAITOK);
  134 
  135         error = getnewvnode("portal", dvp->v_mount, &portal_vnodeops, &fvp);
  136         if (error) {
  137                 free(pt, M_TEMP);
  138                 goto bad;
  139         }
  140         fvp->v_type = VREG;
  141         fvp->v_data = pt;
  142         /*
  143          * Save all of the remaining pathname and
  144          * advance the namei next pointer to the end
  145          * of the string.
  146          */
  147         for (size = 0, path = pname; *path; path++)
  148                 size++;
  149         cnp->cn_consume = size - cnp->cn_namelen;
  150 
  151         pt->pt_arg = malloc(size+1, M_TEMP, M_WAITOK);
  152         pt->pt_size = size+1;
  153         bcopy(pname, pt->pt_arg, pt->pt_size);
  154         pt->pt_fileid = portal_fileid++;
  155 
  156         *vpp = fvp;
  157         vn_lock(fvp, LK_EXCLUSIVE | LK_RETRY);
  158         error = insmntque(fvp, dvp->v_mount);
  159         if (error != 0) {
  160                 *vpp = NULLVP;
  161                 return (error);
  162         }
  163         return (0);
  164 
  165 bad:;
  166         if (fvp)
  167                 vrele(fvp);
  168         return (error);
  169 }
  170 
  171 static int
  172 portal_connect(so, so2)
  173         struct socket *so;
  174         struct socket *so2;
  175 {
  176         /* from unp_connect, bypassing the namei stuff... */
  177         struct socket *so3;
  178         struct unpcb *unp2;
  179         struct unpcb *unp3;
  180 
  181         if (so2 == 0)
  182                 return (ECONNREFUSED);
  183 
  184         if (so->so_type != so2->so_type)
  185                 return (EPROTOTYPE);
  186 
  187         if ((so2->so_options & SO_ACCEPTCONN) == 0)
  188                 return (ECONNREFUSED);
  189 
  190         CURVNET_SET(so2->so_vnet);
  191         if ((so3 = sonewconn(so2, 0)) == 0) {
  192                 CURVNET_RESTORE();
  193                 return (ECONNREFUSED);
  194         }
  195         CURVNET_RESTORE();
  196 
  197         unp2 = sotounpcb(so2);
  198         unp3 = sotounpcb(so3);
  199         if (unp2->unp_addr)
  200                 unp3->unp_addr = (struct sockaddr_un *)
  201                     sodupsockaddr((struct sockaddr *)unp2->unp_addr,
  202                     M_NOWAIT);
  203         so2 = so3;
  204 
  205         return (soconnect2(so, so2));
  206 }
  207 
  208 static int
  209 portal_open(ap)
  210         struct vop_open_args /* {
  211                 struct vnode *a_vp;
  212                 int  a_mode;
  213                 struct ucred *a_cred;
  214                 struct thread *a_td;
  215         } */ *ap;
  216 {
  217         struct socket *so = 0;
  218         struct portalnode *pt;
  219         struct thread *td = ap->a_td;
  220         struct vnode *vp = ap->a_vp;
  221         struct uio auio;
  222         struct iovec aiov[2];
  223         int res;
  224         struct mbuf *cm = 0;
  225         struct cmsghdr *cmsg;
  226         int newfds;
  227         int *ip;
  228         int fd;
  229         int error;
  230         int len;
  231         struct portalmount *fmp;
  232         struct file *fp;
  233         struct portal_cred pcred;
  234 
  235         /*
  236          * Nothing to do when opening the root node.
  237          */
  238         if (vp->v_vflag & VV_ROOT)
  239                 return (0);
  240 
  241         /*
  242          * Can't be opened unless the caller is set up
  243          * to deal with the side effects.  Check for this
  244          * by testing whether td_dupfd has been set.
  245          */
  246         if (td->td_dupfd >= 0)
  247                 return (ENODEV);
  248 
  249         pt = VTOPORTAL(vp);
  250         fmp = VFSTOPORTAL(vp->v_mount);
  251 
  252         /*
  253          * Create a new socket.
  254          */
  255         error = socreate(AF_UNIX, &so, SOCK_STREAM, 0, ap->a_td->td_ucred,
  256             ap->a_td);
  257         if (error)
  258                 goto bad;
  259 
  260         /*
  261          * Reserve some buffer space
  262          */
  263         res = pt->pt_size + sizeof(pcred) + 512;        /* XXX */
  264         error = soreserve(so, res, res);
  265         if (error)
  266                 goto bad;
  267 
  268         /*
  269          * Kick off connection
  270          */
  271         error = portal_connect(so, fmp->pm_server->f_data);
  272         if (error)
  273                 goto bad;
  274 
  275         /*
  276          * Wait for connection to complete
  277          */
  278         /*
  279          * XXX: Since the mount point is holding a reference on the
  280          * underlying server socket, it is not easy to find out whether
  281          * the server process is still running.  To handle this problem
  282          * we loop waiting for the new socket to be connected (something
  283          * which will only happen if the server is still running) or for
  284          * the reference count on the server socket to drop to 1, which
  285          * will happen if the server dies.  Sleep for 5 second intervals
  286          * and keep polling the reference count.   XXX.
  287          */
  288         SOCK_LOCK(so);
  289         while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
  290                 if (fmp->pm_server->f_count == 1) {
  291                         SOCK_UNLOCK(so);
  292                         error = ECONNREFUSED;
  293                         goto bad;
  294                 }
  295                 (void) msleep((caddr_t) &so->so_timeo, SOCK_MTX(so), PSOCK,
  296                     "portalcon", 5 * hz);
  297         }
  298         SOCK_UNLOCK(so);
  299 
  300         if (so->so_error) {
  301                 error = so->so_error;
  302                 goto bad;
  303         }
  304 
  305         /*
  306          * Set miscellaneous flags
  307          */
  308         SOCKBUF_LOCK(&so->so_rcv);
  309         so->so_rcv.sb_timeo = 0;
  310         so->so_rcv.sb_flags |= SB_NOINTR;
  311         SOCKBUF_UNLOCK(&so->so_rcv);
  312         SOCKBUF_LOCK(&so->so_snd);
  313         so->so_snd.sb_timeo = 0;
  314         so->so_snd.sb_flags |= SB_NOINTR;
  315         SOCKBUF_UNLOCK(&so->so_snd);
  316 
  317 
  318         pcred.pcr_flag = ap->a_mode;
  319         pcred.pcr_uid = ap->a_cred->cr_uid;
  320         pcred.pcr_ngroups = MIN(ap->a_cred->cr_ngroups, XU_NGROUPS);
  321         bcopy(ap->a_cred->cr_groups, pcred.pcr_groups,
  322             pcred.pcr_ngroups * sizeof(gid_t));
  323         aiov[0].iov_base = (caddr_t) &pcred;
  324         aiov[0].iov_len = sizeof(pcred);
  325         aiov[1].iov_base = pt->pt_arg;
  326         aiov[1].iov_len = pt->pt_size;
  327         auio.uio_iov = aiov;
  328         auio.uio_iovcnt = 2;
  329         auio.uio_rw = UIO_WRITE;
  330         auio.uio_segflg = UIO_SYSSPACE;
  331         auio.uio_td = td;
  332         auio.uio_offset = 0;
  333         auio.uio_resid = aiov[0].iov_len + aiov[1].iov_len;
  334 
  335         error = sosend(so, (struct sockaddr *) 0, &auio,
  336                         (struct mbuf *) 0, (struct mbuf *) 0, 0 , td);
  337         if (error)
  338                 goto bad;
  339 
  340         len = auio.uio_resid = sizeof(int);
  341         do {
  342                 struct mbuf *m = 0;
  343                 int flags = MSG_WAITALL;
  344                 error = soreceive(so, (struct sockaddr **) 0, &auio,
  345                                         &m, &cm, &flags);
  346                 if (error)
  347                         goto bad;
  348 
  349                 /*
  350                  * Grab an error code from the mbuf.
  351                  */
  352                 if (m) {
  353                         m = m_pullup(m, sizeof(int));   /* Needed? */
  354                         if (m) {
  355                                 error = *(mtod(m, int *));
  356                                 m_freem(m);
  357                         } else {
  358                                 error = EINVAL;
  359                         }
  360                 } else {
  361                         if (cm == 0) {
  362                                 error = ECONNRESET;      /* XXX */
  363 #ifdef notdef
  364                                 break;
  365 #endif
  366                         }
  367                 }
  368         } while (cm == 0 && auio.uio_resid == len && !error);
  369 
  370         if (cm == 0)
  371                 goto bad;
  372 
  373         if (auio.uio_resid) {
  374                 error = 0;
  375 #ifdef notdef
  376                 error = EMSGSIZE;
  377                 goto bad;
  378 #endif
  379         }
  380 
  381         /*
  382          * XXX: Break apart the control message, and retrieve the
  383          * received file descriptor.  Note that more than one descriptor
  384          * may have been received, or that the rights chain may have more
  385          * than a single mbuf in it.  What to do?
  386          */
  387         cmsg = mtod(cm, struct cmsghdr *);
  388         newfds = (cmsg->cmsg_len - sizeof(*cmsg)) / sizeof (int);
  389         if (newfds == 0) {
  390                 error = ECONNREFUSED;
  391                 goto bad;
  392         }
  393         /*
  394          * At this point the rights message consists of a control message
  395          * header, followed by a data region containing a vector of
  396          * integer file descriptors.  The fds were allocated by the action
  397          * of receiving the control message.
  398          */
  399         ip = (int *) (cmsg + 1);
  400         fd = *ip++;
  401         if (newfds > 1) {
  402                 /*
  403                  * Close extra fds.
  404                  */
  405                 int i;
  406                 printf("portal_open: %d extra fds\n", newfds - 1);
  407                 for (i = 1; i < newfds; i++) {
  408                         portal_closefd(td, *ip);
  409                         ip++;
  410                 }
  411         }
  412 
  413         /*
  414          * Check that the mode the file is being opened for is a subset
  415          * of the mode of the existing descriptor.
  416          */
  417         if ((error = fget(td, fd, &fp)) != 0)
  418                 goto bad;
  419         if (((ap->a_mode & (FREAD|FWRITE)) | fp->f_flag) != fp->f_flag) {
  420                 fdrop(fp, td);
  421                 portal_closefd(td, fd);
  422                 error = EACCES;
  423                 goto bad;
  424         }
  425         fdrop(fp, td);
  426 
  427         /*
  428          * Save the dup fd in the proc structure then return the
  429          * special error code (ENXIO) which causes magic things to
  430          * happen in vn_open.  The whole concept is, well, hmmm.
  431          */
  432         td->td_dupfd = fd;
  433         error = ENXIO;
  434 
  435 bad:;
  436         /*
  437          * And discard the control message.
  438          */
  439         if (cm) {
  440                 m_freem(cm);
  441         }
  442 
  443         if (so) {
  444                 soshutdown(so, 2);
  445                 soclose(so);
  446         }
  447         return (error);
  448 }
  449 
  450 static int
  451 portal_getattr(ap)
  452         struct vop_getattr_args /* {
  453                 struct vnode *a_vp;
  454                 struct vattr *a_vap;
  455                 struct ucred *a_cred;
  456         } */ *ap;
  457 {
  458         struct vnode *vp = ap->a_vp;
  459         struct vattr *vap = ap->a_vap;
  460 
  461         vap->va_uid = 0;
  462         vap->va_gid = 0;
  463         vap->va_size = DEV_BSIZE;
  464         vap->va_blocksize = DEV_BSIZE;
  465         nanotime(&vap->va_atime);
  466         vap->va_mtime = vap->va_atime;
  467         vap->va_ctime = vap->va_mtime;
  468         vap->va_gen = 0;
  469         vap->va_flags = 0;
  470         vap->va_rdev = NODEV;
  471         /* vap->va_qbytes = 0; */
  472         vap->va_bytes = 0;
  473         vap->va_filerev = 0;
  474         /* vap->va_qsize = 0; */
  475         if (vp->v_vflag & VV_ROOT) {
  476                 vap->va_type = VDIR;
  477                 vap->va_mode = S_IRUSR|S_IWUSR|S_IXUSR|
  478                                 S_IRGRP|S_IWGRP|S_IXGRP|
  479                                 S_IROTH|S_IWOTH|S_IXOTH;
  480                 vap->va_nlink = 2;
  481                 vap->va_fileid = 2;
  482         } else {
  483                 vap->va_type = VREG;
  484                 vap->va_mode = S_IRUSR|S_IWUSR|
  485                                 S_IRGRP|S_IWGRP|
  486                                 S_IROTH|S_IWOTH;
  487                 vap->va_nlink = 1;
  488                 vap->va_fileid = VTOPORTAL(vp)->pt_fileid;
  489         }
  490         return (0);
  491 }
  492 
  493 static int
  494 portal_setattr(ap)
  495         struct vop_setattr_args /* {
  496                 struct vnode *a_vp;
  497                 struct vattr *a_vap;
  498                 struct ucred *a_cred;
  499         } */ *ap;
  500 {
  501 
  502         /*
  503          * Can't mess with the root vnode
  504          */
  505         if (ap->a_vp->v_vflag & VV_ROOT)
  506                 return (EACCES);
  507 
  508         if (ap->a_vap->va_flags != VNOVAL)
  509                 return (EOPNOTSUPP);
  510 
  511         return (0);
  512 }
  513 
  514 /*
  515  * Fake readdir, just return empty directory.
  516  * It is hard to deal with '.' and '..' so don't bother.
  517  */
  518 static int
  519 portal_readdir(ap)
  520         struct vop_readdir_args /* {
  521                 struct vnode *a_vp;
  522                 struct uio *a_uio;
  523                 struct ucred *a_cred;
  524                 int *a_eofflag;
  525                 u_long *a_cookies;
  526                 int a_ncookies;
  527         } */ *ap;
  528 {
  529 
  530         /*
  531          * We don't allow exporting portal mounts, and currently local
  532          * requests do not need cookies.
  533          */
  534         if (ap->a_ncookies)
  535                 panic("portal_readdir: not hungry");
  536 
  537         return (0);
  538 }
  539 
  540 static int
  541 portal_reclaim(ap)
  542         struct vop_reclaim_args /* {
  543                 struct vnode *a_vp;
  544         } */ *ap;
  545 {
  546         struct portalnode *pt = VTOPORTAL(ap->a_vp);
  547 
  548         if (pt->pt_arg) {
  549                 free((caddr_t) pt->pt_arg, M_TEMP);
  550                 pt->pt_arg = 0;
  551         }
  552         free(ap->a_vp->v_data, M_TEMP);
  553         ap->a_vp->v_data = 0;
  554 
  555         return (0);
  556 }
  557 
  558 struct vop_vector portal_vnodeops = {
  559         .vop_default =          &default_vnodeops,
  560 
  561         .vop_access =           VOP_NULL,
  562         .vop_getattr =          portal_getattr,
  563         .vop_lookup =           portal_lookup,
  564         .vop_open =             portal_open,
  565         .vop_pathconf =         vop_stdpathconf,
  566         .vop_readdir =          portal_readdir,
  567         .vop_reclaim =          portal_reclaim,
  568         .vop_setattr =          portal_setattr,
  569 };

Cache object: 2d01956feb06ee7f03f1e8766401466e


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.