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: releng/6.4/sys/fs/portalfs/portal_vnops.c 144208 2005-03-28 09:34:36Z jeff $
   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/sysproto.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 static int portal_fileid = PORTAL_ROOTFILEID+1;
   64 
   65 static void     portal_closefd(struct thread *td, int fd);
   66 static int      portal_connect(struct socket *so, struct socket *so2);
   67 static vop_getattr_t    portal_getattr;
   68 static vop_lookup_t     portal_lookup;
   69 static vop_open_t       portal_open;
   70 static vop_readdir_t    portal_readdir;
   71 static vop_reclaim_t    portal_reclaim;
   72 static vop_setattr_t    portal_setattr;
   73 
   74 static void
   75 portal_closefd(td, fd)
   76         struct thread *td;
   77         int fd;
   78 {
   79         int error;
   80         struct close_args ua;
   81 
   82         ua.fd = fd;
   83         error = close(td, &ua);
   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 thread *td = cnp->cn_thread;
  109         struct portalnode *pt;
  110         int error;
  111         struct vnode *fvp = 0;
  112         char *path;
  113         int size;
  114 
  115         *vpp = NULLVP;
  116 
  117         if (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)
  118                 return (EROFS);
  119 
  120         if (cnp->cn_namelen == 1 && *pname == '.') {
  121                 *vpp = dvp;
  122                 VREF(dvp);
  123                 return (0);
  124         }
  125         KASSERT((cnp->cn_flags & ISDOTDOT) == 0,
  126             ("portal_lookup: Can not handle dotdot lookups."));
  127 
  128         /*
  129          * Do the MALLOC before the getnewvnode since doing so afterward
  130          * might cause a bogus v_data pointer to get dereferenced
  131          * elsewhere if MALLOC should block.
  132          */
  133         MALLOC(pt, struct portalnode *, sizeof(struct portalnode),
  134                 M_TEMP, M_WAITOK);
  135 
  136         error = getnewvnode("portal", dvp->v_mount, &portal_vnodeops, &fvp);
  137         if (error) {
  138                 FREE(pt, M_TEMP);
  139                 goto bad;
  140         }
  141         fvp->v_type = VREG;
  142         fvp->v_data = pt;
  143         /*
  144          * Save all of the remaining pathname and
  145          * advance the namei next pointer to the end
  146          * of the string.
  147          */
  148         for (size = 0, path = pname; *path; path++)
  149                 size++;
  150         cnp->cn_consume = size - cnp->cn_namelen;
  151 
  152         pt->pt_arg = malloc(size+1, M_TEMP, M_WAITOK);
  153         pt->pt_size = size+1;
  154         bcopy(pname, pt->pt_arg, pt->pt_size);
  155         pt->pt_fileid = portal_fileid++;
  156 
  157         *vpp = fvp;
  158         vn_lock(fvp, LK_EXCLUSIVE | LK_RETRY, td);
  159         return (0);
  160 
  161 bad:;
  162         if (fvp)
  163                 vrele(fvp);
  164         return (error);
  165 }
  166 
  167 static int
  168 portal_connect(so, so2)
  169         struct socket *so;
  170         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 == 0)
  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)) == 0)
  187                 return (ECONNREFUSED);
  188 
  189         unp2 = sotounpcb(so2);
  190         unp3 = sotounpcb(so3);
  191         if (unp2->unp_addr)
  192                 unp3->unp_addr = (struct sockaddr_un *)
  193                     sodupsockaddr((struct sockaddr *)unp2->unp_addr,
  194                     M_NOWAIT);
  195         so2 = so3;
  196 
  197         return (uipc_connect2(so, so2));
  198 }
  199 
  200 static int
  201 portal_open(ap)
  202         struct vop_open_args /* {
  203                 struct vnode *a_vp;
  204                 int  a_mode;
  205                 struct ucred *a_cred;
  206                 struct thread *a_td;
  207         } */ *ap;
  208 {
  209         struct socket *so = 0;
  210         struct portalnode *pt;
  211         struct thread *td = ap->a_td;
  212         struct vnode *vp = ap->a_vp;
  213         struct uio auio;
  214         struct iovec aiov[2];
  215         int res;
  216         struct mbuf *cm = 0;
  217         struct cmsghdr *cmsg;
  218         int newfds;
  219         int *ip;
  220         int fd;
  221         int error;
  222         int len;
  223         struct portalmount *fmp;
  224         struct file *fp;
  225         struct portal_cred pcred;
  226 
  227         /*
  228          * Nothing to do when opening the root node.
  229          */
  230         if (vp->v_vflag & VV_ROOT)
  231                 return (0);
  232 
  233         /*
  234          * Can't be opened unless the caller is set up
  235          * to deal with the side effects.  Check for this
  236          * by testing whether the p_dupfd has been set.
  237          */
  238         if (td->td_dupfd >= 0)
  239                 return (ENODEV);
  240 
  241         pt = VTOPORTAL(vp);
  242         fmp = VFSTOPORTAL(vp->v_mount);
  243 
  244         /*
  245          * Create a new socket.
  246          */
  247         error = socreate(AF_UNIX, &so, SOCK_STREAM, 0, ap->a_td->td_ucred,
  248             ap->a_td);
  249         if (error)
  250                 goto bad;
  251 
  252         /*
  253          * Reserve some buffer space
  254          */
  255         res = pt->pt_size + sizeof(pcred) + 512;        /* XXX */
  256         error = soreserve(so, res, res);
  257         if (error)
  258                 goto bad;
  259 
  260         /*
  261          * Kick off connection
  262          */
  263         error = portal_connect(so, fmp->pm_server->f_data);
  264         if (error)
  265                 goto bad;
  266 
  267         /*
  268          * Wait for connection to complete
  269          */
  270         /*
  271          * XXX: Since the mount point is holding a reference on the
  272          * underlying server socket, it is not easy to find out whether
  273          * the server process is still running.  To handle this problem
  274          * we loop waiting for the new socket to be connected (something
  275          * which will only happen if the server is still running) or for
  276          * the reference count on the server socket to drop to 1, which
  277          * will happen if the server dies.  Sleep for 5 second intervals
  278          * and keep polling the reference count.   XXX.
  279          */
  280         /* XXXRW: Locking? */
  281         SOCK_LOCK(so);
  282         while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
  283                 if (fmp->pm_server->f_count == 1) {
  284                         SOCK_UNLOCK(so);
  285                         error = ECONNREFUSED;
  286                         goto bad;
  287                 }
  288                 (void) msleep((caddr_t) &so->so_timeo, SOCK_MTX(so), PSOCK,
  289                     "portalcon", 5 * hz);
  290         }
  291         SOCK_UNLOCK(so);
  292 
  293         if (so->so_error) {
  294                 error = so->so_error;
  295                 goto bad;
  296         }
  297 
  298         /*
  299          * Set miscellaneous flags
  300          */
  301         SOCKBUF_LOCK(&so->so_rcv);
  302         so->so_rcv.sb_timeo = 0;
  303         so->so_rcv.sb_flags |= SB_NOINTR;
  304         SOCKBUF_UNLOCK(&so->so_rcv);
  305         SOCKBUF_LOCK(&so->so_snd);
  306         so->so_snd.sb_timeo = 0;
  307         so->so_snd.sb_flags |= SB_NOINTR;
  308         SOCKBUF_UNLOCK(&so->so_snd);
  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;
  323         auio.uio_td = td;
  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,
  328                         (struct mbuf *) 0, (struct mbuf *) 0, 0 , td);
  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++) {
  400                         portal_closefd(td, *ip);
  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          */
  409         if ((error = fget(td, fd, &fp)) != 0)
  410                 goto bad;
  411         if (((ap->a_mode & (FREAD|FWRITE)) | fp->f_flag) != fp->f_flag) {
  412                 fdrop(fp, td);
  413                 portal_closefd(td, fd);
  414                 error = EACCES;
  415                 goto bad;
  416         }
  417         fdrop(fp, td);
  418 
  419         /*
  420          * Save the dup fd in the proc structure then return the
  421          * special error code (ENXIO) which causes magic things to
  422          * happen in vn_open.  The whole concept is, well, hmmm.
  423          */
  424         td->td_dupfd = fd;
  425         error = ENXIO;
  426 
  427 bad:;
  428         /*
  429          * And discard the control message.
  430          */
  431         if (cm) {
  432                 m_freem(cm);
  433         }
  434 
  435         if (so) {
  436                 soshutdown(so, 2);
  437                 soclose(so);
  438         }
  439         return (error);
  440 }
  441 
  442 static int
  443 portal_getattr(ap)
  444         struct vop_getattr_args /* {
  445                 struct vnode *a_vp;
  446                 struct vattr *a_vap;
  447                 struct ucred *a_cred;
  448                 struct thread *a_td;
  449         } */ *ap;
  450 {
  451         struct vnode *vp = ap->a_vp;
  452         struct vattr *vap = ap->a_vap;
  453 
  454         bzero(vap, sizeof(*vap));
  455         vattr_null(vap);
  456         vap->va_uid = 0;
  457         vap->va_gid = 0;
  458         vap->va_size = DEV_BSIZE;
  459         vap->va_blocksize = DEV_BSIZE;
  460         nanotime(&vap->va_atime);
  461         vap->va_mtime = vap->va_atime;
  462         vap->va_ctime = vap->va_mtime;
  463         vap->va_gen = 0;
  464         vap->va_flags = 0;
  465         vap->va_rdev = 0;
  466         /* vap->va_qbytes = 0; */
  467         vap->va_bytes = 0;
  468         /* vap->va_qsize = 0; */
  469         if (vp->v_vflag & VV_ROOT) {
  470                 vap->va_type = VDIR;
  471                 vap->va_mode = S_IRUSR|S_IWUSR|S_IXUSR|
  472                                 S_IRGRP|S_IWGRP|S_IXGRP|
  473                                 S_IROTH|S_IWOTH|S_IXOTH;
  474                 vap->va_nlink = 2;
  475                 vap->va_fileid = 2;
  476         } else {
  477                 vap->va_type = VREG;
  478                 vap->va_mode = S_IRUSR|S_IWUSR|
  479                                 S_IRGRP|S_IWGRP|
  480                                 S_IROTH|S_IWOTH;
  481                 vap->va_nlink = 1;
  482                 vap->va_fileid = VTOPORTAL(vp)->pt_fileid;
  483         }
  484         return (0);
  485 }
  486 
  487 static int
  488 portal_setattr(ap)
  489         struct vop_setattr_args /* {
  490                 struct vnode *a_vp;
  491                 struct vattr *a_vap;
  492                 struct ucred *a_cred;
  493                 struct thread *a_td;
  494         } */ *ap;
  495 {
  496 
  497         /*
  498          * Can't mess with the root vnode
  499          */
  500         if (ap->a_vp->v_vflag & VV_ROOT)
  501                 return (EACCES);
  502 
  503         if (ap->a_vap->va_flags != VNOVAL)
  504                 return (EOPNOTSUPP);
  505 
  506         return (0);
  507 }
  508 
  509 /*
  510  * Fake readdir, just return empty directory.
  511  * It is hard to deal with '.' and '..' so don't bother.
  512  */
  513 static int
  514 portal_readdir(ap)
  515         struct vop_readdir_args /* {
  516                 struct vnode *a_vp;
  517                 struct uio *a_uio;
  518                 struct ucred *a_cred;
  519                 int *a_eofflag;
  520                 u_long *a_cookies;
  521                 int a_ncookies;
  522         } */ *ap;
  523 {
  524 
  525         /*
  526          * We don't allow exporting portal mounts, and currently local
  527          * requests do not need cookies.
  528          */
  529         if (ap->a_ncookies)
  530                 panic("portal_readdir: not hungry");
  531 
  532         return (0);
  533 }
  534 
  535 static int
  536 portal_reclaim(ap)
  537         struct vop_reclaim_args /* {
  538                 struct vnode *a_vp;
  539         } */ *ap;
  540 {
  541         struct portalnode *pt = VTOPORTAL(ap->a_vp);
  542 
  543         if (pt->pt_arg) {
  544                 free((caddr_t) pt->pt_arg, M_TEMP);
  545                 pt->pt_arg = 0;
  546         }
  547         FREE(ap->a_vp->v_data, M_TEMP);
  548         ap->a_vp->v_data = 0;
  549 
  550         return (0);
  551 }
  552 
  553 struct vop_vector portal_vnodeops = {
  554         .vop_default =          &default_vnodeops,
  555 
  556         .vop_access =           VOP_NULL,
  557         .vop_getattr =          portal_getattr,
  558         .vop_lookup =           portal_lookup,
  559         .vop_open =             portal_open,
  560         .vop_pathconf =         vop_stdpathconf,
  561         .vop_readdir =          portal_readdir,
  562         .vop_reclaim =          portal_reclaim,
  563         .vop_setattr =          portal_setattr,
  564 };

Cache object: 355bab24f924892ca8ff85b88e9d9a46


[ 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.