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/pseudofs/pseudofs_vnops.c

Version: -  FREEBSD  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 2001 Dag-Erling Co´dan Sm°rgrav
    3  * All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer
   10  *    in this position and unchanged.
   11  * 2. Redistributions in binary form must reproduce the above copyright
   12  *    notice, this list of conditions and the following disclaimer in the
   13  *    documentation and/or other materials provided with the distribution.
   14  * 3. The name of the author may not be used to endorse or promote products
   15  *    derived from this software without specific prior written permission.
   16  *
   17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
   18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
   21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   27  */
   28 
   29 #include <sys/cdefs.h>
   30 __FBSDID("$FreeBSD: stable/8/sys/fs/pseudofs/pseudofs_vnops.c 233171 2012-03-19 11:27:54Z kib $");
   31 
   32 #include "opt_pseudofs.h"
   33 
   34 #include <sys/param.h>
   35 #include <sys/kernel.h>
   36 #include <sys/systm.h>
   37 #include <sys/ctype.h>
   38 #include <sys/dirent.h>
   39 #include <sys/fcntl.h>
   40 #include <sys/limits.h>
   41 #include <sys/lock.h>
   42 #include <sys/malloc.h>
   43 #include <sys/mount.h>
   44 #include <sys/mutex.h>
   45 #include <sys/namei.h>
   46 #include <sys/proc.h>
   47 #include <sys/sbuf.h>
   48 #include <sys/sx.h>
   49 #include <sys/sysctl.h>
   50 #include <sys/vnode.h>
   51 
   52 #include <fs/pseudofs/pseudofs.h>
   53 #include <fs/pseudofs/pseudofs_internal.h>
   54 
   55 #define KASSERT_PN_IS_DIR(pn)                                           \
   56         KASSERT((pn)->pn_type == pfstype_root ||                        \
   57             (pn)->pn_type == pfstype_dir ||                             \
   58             (pn)->pn_type == pfstype_procdir,                           \
   59             ("%s(): VDIR vnode refers to non-directory pfs_node", __func__))
   60 
   61 #define KASSERT_PN_IS_FILE(pn)                                          \
   62         KASSERT((pn)->pn_type == pfstype_file,                          \
   63             ("%s(): VREG vnode refers to non-file pfs_node", __func__))
   64 
   65 #define KASSERT_PN_IS_LINK(pn)                                          \
   66         KASSERT((pn)->pn_type == pfstype_symlink,                       \
   67             ("%s(): VLNK vnode refers to non-link pfs_node", __func__))
   68 
   69 /*
   70  * Returns the fileno, adjusted for target pid
   71  */
   72 static uint32_t
   73 pn_fileno(struct pfs_node *pn, pid_t pid)
   74 {
   75 
   76         KASSERT(pn->pn_fileno > 0,
   77             ("%s(): no fileno allocated", __func__));
   78         if (pid != NO_PID)
   79                 return (pn->pn_fileno * NO_PID + pid);
   80         return (pn->pn_fileno);
   81 }
   82 
   83 /*
   84  * Returns non-zero if given file is visible to given thread.
   85  */
   86 static int
   87 pfs_visible_proc(struct thread *td, struct pfs_node *pn, struct proc *proc)
   88 {
   89         int visible;
   90 
   91         if (proc == NULL)
   92                 return (0);
   93 
   94         PROC_LOCK_ASSERT(proc, MA_OWNED);
   95 
   96         visible = ((proc->p_flag & P_WEXIT) == 0);
   97         if (visible)
   98                 visible = (p_cansee(td, proc) == 0);
   99         if (visible && pn->pn_vis != NULL)
  100                 visible = pn_vis(td, proc, pn);
  101         if (!visible)
  102                 return (0);
  103         return (1);
  104 }
  105 
  106 static int
  107 pfs_visible(struct thread *td, struct pfs_node *pn, pid_t pid, struct proc **p)
  108 {
  109         struct proc *proc;
  110 
  111         PFS_TRACE(("%s (pid: %d, req: %d)",
  112             pn->pn_name, pid, td->td_proc->p_pid));
  113 
  114         if (p)
  115                 *p = NULL;
  116         if (pid == NO_PID)
  117                 PFS_RETURN (1);
  118         if ((proc = pfind(pid)) == NULL)
  119                 PFS_RETURN (0);
  120         if (pfs_visible_proc(td, pn, proc)) {
  121                 if (p)
  122                         *p = proc;
  123                 else
  124                         PROC_UNLOCK(proc);
  125                 PFS_RETURN (1);
  126         }
  127         PROC_UNLOCK(proc);
  128         PFS_RETURN (0);
  129 }
  130 
  131 /*
  132  * Verify permissions
  133  */
  134 static int
  135 pfs_access(struct vop_access_args *va)
  136 {
  137         struct vnode *vn = va->a_vp;
  138         struct pfs_vdata *pvd = vn->v_data;
  139         struct vattr vattr;
  140         int error;
  141 
  142         PFS_TRACE(("%s", pvd->pvd_pn->pn_name));
  143         (void)pvd;
  144 
  145         error = VOP_GETATTR(vn, &vattr, va->a_cred);
  146         if (error)
  147                 PFS_RETURN (error);
  148         error = vaccess(vn->v_type, vattr.va_mode, vattr.va_uid,
  149             vattr.va_gid, va->a_accmode, va->a_cred, NULL);
  150         PFS_RETURN (error);
  151 }
  152 
  153 /*
  154  * Close a file or directory
  155  */
  156 static int
  157 pfs_close(struct vop_close_args *va)
  158 {
  159         struct vnode *vn = va->a_vp;
  160         struct pfs_vdata *pvd = vn->v_data;
  161         struct pfs_node *pn = pvd->pvd_pn;
  162         struct proc *proc;
  163         int error;
  164 
  165         PFS_TRACE(("%s", pn->pn_name));
  166         pfs_assert_not_owned(pn);
  167 
  168         /*
  169          * Do nothing unless this is the last close and the node has a
  170          * last-close handler.
  171          */
  172         if (vrefcnt(vn) > 1 || pn->pn_close == NULL)
  173                 PFS_RETURN (0);
  174 
  175         if (pvd->pvd_pid != NO_PID) {
  176                 proc = pfind(pvd->pvd_pid);
  177         } else {
  178                 proc = NULL;
  179         }
  180 
  181         error = pn_close(va->a_td, proc, pn);
  182 
  183         if (proc != NULL)
  184                 PROC_UNLOCK(proc);
  185 
  186         PFS_RETURN (error);
  187 }
  188 
  189 /*
  190  * Get file attributes
  191  */
  192 static int
  193 pfs_getattr(struct vop_getattr_args *va)
  194 {
  195         struct vnode *vn = va->a_vp;
  196         struct pfs_vdata *pvd = vn->v_data;
  197         struct pfs_node *pn = pvd->pvd_pn;
  198         struct vattr *vap = va->a_vap;
  199         struct proc *proc;
  200         int error = 0;
  201 
  202         PFS_TRACE(("%s", pn->pn_name));
  203         pfs_assert_not_owned(pn);
  204 
  205         if (!pfs_visible(curthread, pn, pvd->pvd_pid, &proc))
  206                 PFS_RETURN (ENOENT);
  207 
  208         vap->va_type = vn->v_type;
  209         vap->va_fileid = pn_fileno(pn, pvd->pvd_pid);
  210         vap->va_flags = 0;
  211         vap->va_blocksize = PAGE_SIZE;
  212         vap->va_bytes = vap->va_size = 0;
  213         vap->va_filerev = 0;
  214         vap->va_fsid = vn->v_mount->mnt_stat.f_fsid.val[0];
  215         vap->va_nlink = 1;
  216         nanotime(&vap->va_ctime);
  217         vap->va_atime = vap->va_mtime = vap->va_ctime;
  218 
  219         switch (pn->pn_type) {
  220         case pfstype_procdir:
  221         case pfstype_root:
  222         case pfstype_dir:
  223 #if 0
  224                 pfs_lock(pn);
  225                 /* compute link count */
  226                 pfs_unlock(pn);
  227 #endif
  228                 vap->va_mode = 0555;
  229                 break;
  230         case pfstype_file:
  231         case pfstype_symlink:
  232                 vap->va_mode = 0444;
  233                 break;
  234         default:
  235                 printf("shouldn't be here!\n");
  236                 vap->va_mode = 0;
  237                 break;
  238         }
  239 
  240         if (proc != NULL) {
  241                 vap->va_uid = proc->p_ucred->cr_ruid;
  242                 vap->va_gid = proc->p_ucred->cr_rgid;
  243         } else {
  244                 vap->va_uid = 0;
  245                 vap->va_gid = 0;
  246         }
  247 
  248         if (pn->pn_attr != NULL)
  249                 error = pn_attr(curthread, proc, pn, vap);
  250 
  251         if(proc != NULL)
  252                 PROC_UNLOCK(proc);
  253 
  254         PFS_RETURN (error);
  255 }
  256 
  257 /*
  258  * Perform an ioctl
  259  */
  260 static int
  261 pfs_ioctl(struct vop_ioctl_args *va)
  262 {
  263         struct vnode *vn;
  264         struct pfs_vdata *pvd;
  265         struct pfs_node *pn;
  266         struct proc *proc;
  267         int error;
  268 
  269         vn = va->a_vp;
  270         vn_lock(vn, LK_SHARED | LK_RETRY);
  271         if (vn->v_iflag & VI_DOOMED) {
  272                 VOP_UNLOCK(vn, 0);
  273                 return (EBADF);
  274         }
  275         pvd = vn->v_data;
  276         pn = pvd->pvd_pn;
  277 
  278         PFS_TRACE(("%s: %lx", pn->pn_name, va->a_command));
  279         pfs_assert_not_owned(pn);
  280 
  281         if (vn->v_type != VREG) {
  282                 VOP_UNLOCK(vn, 0);
  283                 PFS_RETURN (EINVAL);
  284         }
  285         KASSERT_PN_IS_FILE(pn);
  286 
  287         if (pn->pn_ioctl == NULL) {
  288                 VOP_UNLOCK(vn, 0);
  289                 PFS_RETURN (ENOTTY);
  290         }
  291 
  292         /*
  293          * This is necessary because process' privileges may
  294          * have changed since the open() call.
  295          */
  296         if (!pfs_visible(curthread, pn, pvd->pvd_pid, &proc)) {
  297                 VOP_UNLOCK(vn, 0);
  298                 PFS_RETURN (EIO);
  299         }
  300 
  301         error = pn_ioctl(curthread, proc, pn, va->a_command, va->a_data);
  302 
  303         if (proc != NULL)
  304                 PROC_UNLOCK(proc);
  305 
  306         VOP_UNLOCK(vn, 0);
  307         PFS_RETURN (error);
  308 }
  309 
  310 /*
  311  * Perform getextattr
  312  */
  313 static int
  314 pfs_getextattr(struct vop_getextattr_args *va)
  315 {
  316         struct vnode *vn = va->a_vp;
  317         struct pfs_vdata *pvd = vn->v_data;
  318         struct pfs_node *pn = pvd->pvd_pn;
  319         struct proc *proc;
  320         int error;
  321 
  322         PFS_TRACE(("%s", pn->pn_name));
  323         pfs_assert_not_owned(pn);
  324 
  325         /*
  326          * This is necessary because either process' privileges may
  327          * have changed since the open() call.
  328          */
  329         if (!pfs_visible(curthread, pn, pvd->pvd_pid, &proc))
  330                 PFS_RETURN (EIO);
  331 
  332         if (pn->pn_getextattr == NULL)
  333                 error = EOPNOTSUPP;
  334         else
  335                 error = pn_getextattr(curthread, proc, pn,
  336                     va->a_attrnamespace, va->a_name, va->a_uio,
  337                     va->a_size, va->a_cred);
  338 
  339         if (proc != NULL)
  340                 PROC_UNLOCK(proc);
  341 
  342         PFS_RETURN (error);
  343 }
  344 
  345 /*
  346  * Convert a vnode to its component name
  347  */
  348 static int
  349 pfs_vptocnp(struct vop_vptocnp_args *ap)
  350 {
  351         struct vnode *vp = ap->a_vp;
  352         struct vnode **dvp = ap->a_vpp;
  353         struct pfs_vdata *pvd = vp->v_data;
  354         struct pfs_node *pd = pvd->pvd_pn;
  355         struct pfs_node *pn;
  356         struct mount *mp;
  357         char *buf = ap->a_buf;
  358         int *buflen = ap->a_buflen;
  359         char pidbuf[PFS_NAMELEN];
  360         pid_t pid = pvd->pvd_pid;
  361         int len, i, error, locked;
  362 
  363         i = *buflen;
  364         error = 0;
  365 
  366         pfs_lock(pd);
  367 
  368         if (vp->v_type == VDIR && pd->pn_type == pfstype_root) {
  369                 *dvp = vp;
  370                 vhold(*dvp);
  371                 pfs_unlock(pd);
  372                 PFS_RETURN (0);
  373         } else if (vp->v_type == VDIR && pd->pn_type == pfstype_procdir) {
  374                 len = snprintf(pidbuf, sizeof(pidbuf), "%d", pid);
  375                 i -= len;
  376                 if (i < 0) {
  377                         error = ENOMEM;
  378                         goto failed;
  379                 }
  380                 bcopy(pidbuf, buf + i, len);
  381         } else {
  382                 len = strlen(pd->pn_name);
  383                 i -= len;
  384                 if (i < 0) {
  385                         error = ENOMEM;
  386                         goto failed;
  387                 }
  388                 bcopy(pd->pn_name, buf + i, len);
  389         }
  390 
  391         pn = pd->pn_parent;
  392         pfs_unlock(pd);
  393 
  394         mp = vp->v_mount;
  395         error = vfs_busy(mp, 0);
  396         if (error)
  397                 return (error);
  398 
  399         /*
  400          * vp is held by caller.
  401          */
  402         locked = VOP_ISLOCKED(vp);
  403         VOP_UNLOCK(vp, 0);
  404 
  405         error = pfs_vncache_alloc(mp, dvp, pn, pid);
  406         if (error) {
  407                 vn_lock(vp, locked | LK_RETRY);
  408                 vfs_unbusy(mp);
  409                 PFS_RETURN(error);
  410         }
  411 
  412         *buflen = i;
  413         vhold(*dvp);
  414         vput(*dvp);
  415         vn_lock(vp, locked | LK_RETRY);
  416         vfs_unbusy(mp);
  417 
  418         PFS_RETURN (0);
  419 failed:
  420         pfs_unlock(pd);
  421         PFS_RETURN(error);
  422 }
  423 
  424 /*
  425  * Look up a file or directory
  426  */
  427 static int
  428 pfs_lookup(struct vop_cachedlookup_args *va)
  429 {
  430         struct vnode *vn = va->a_dvp;
  431         struct vnode **vpp = va->a_vpp;
  432         struct componentname *cnp = va->a_cnp;
  433         struct pfs_vdata *pvd = vn->v_data;
  434         struct pfs_node *pd = pvd->pvd_pn;
  435         struct pfs_node *pn, *pdn = NULL;
  436         struct mount *mp;
  437         pid_t pid = pvd->pvd_pid;
  438         char *pname;
  439         int error, i, namelen, visible;
  440 
  441         PFS_TRACE(("%.*s", (int)cnp->cn_namelen, cnp->cn_nameptr));
  442         pfs_assert_not_owned(pd);
  443 
  444         if (vn->v_type != VDIR)
  445                 PFS_RETURN (ENOTDIR);
  446         KASSERT_PN_IS_DIR(pd);
  447 
  448         error = VOP_ACCESS(vn, VEXEC, cnp->cn_cred, cnp->cn_thread);
  449         if (error)
  450                 PFS_RETURN (error);
  451 
  452         /*
  453          * Don't support DELETE or RENAME.  CREATE is supported so
  454          * that O_CREAT will work, but the lookup will still fail if
  455          * the file does not exist.
  456          */
  457         if ((cnp->cn_flags & ISLASTCN) &&
  458             (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
  459                 PFS_RETURN (EOPNOTSUPP);
  460 
  461         /* shortcut: check if the name is too long */
  462         if (cnp->cn_namelen >= PFS_NAMELEN)
  463                 PFS_RETURN (ENOENT);
  464 
  465         /* check that parent directory is visible... */
  466         if (!pfs_visible(curthread, pd, pvd->pvd_pid, NULL))
  467                 PFS_RETURN (ENOENT);
  468 
  469         /* self */
  470         namelen = cnp->cn_namelen;
  471         pname = cnp->cn_nameptr;
  472         if (namelen == 1 && pname[0] == '.') {
  473                 pn = pd;
  474                 *vpp = vn;
  475                 VREF(vn);
  476                 PFS_RETURN (0);
  477         }
  478 
  479         mp = vn->v_mount;
  480 
  481         /* parent */
  482         if (cnp->cn_flags & ISDOTDOT) {
  483                 if (pd->pn_type == pfstype_root)
  484                         PFS_RETURN (EIO);
  485                 error = vfs_busy(mp, MBF_NOWAIT);
  486                 if (error != 0) {
  487                         vfs_ref(mp);
  488                         VOP_UNLOCK(vn, 0);
  489                         error = vfs_busy(mp, 0);
  490                         vn_lock(vn, LK_EXCLUSIVE | LK_RETRY);
  491                         vfs_rel(mp);
  492                         if (error != 0)
  493                                 PFS_RETURN(ENOENT);
  494                         if (vn->v_iflag & VI_DOOMED) {
  495                                 vfs_unbusy(mp);
  496                                 PFS_RETURN(ENOENT);
  497                         }
  498                 }
  499                 VOP_UNLOCK(vn, 0);
  500                 KASSERT(pd->pn_parent != NULL,
  501                     ("%s(): non-root directory has no parent", __func__));
  502                 /*
  503                  * This one is tricky.  Descendents of procdir nodes
  504                  * inherit their parent's process affinity, but
  505                  * there's no easy reverse mapping.  For simplicity,
  506                  * we assume that if this node is a procdir, its
  507                  * parent isn't (which is correct as long as
  508                  * descendents of procdir nodes are never procdir
  509                  * nodes themselves)
  510                  */
  511                 if (pd->pn_type == pfstype_procdir)
  512                         pid = NO_PID;
  513                 pfs_lock(pd);
  514                 pn = pd->pn_parent;
  515                 pfs_unlock(pd);
  516                 goto got_pnode;
  517         }
  518 
  519         pfs_lock(pd);
  520 
  521         /* named node */
  522         for (pn = pd->pn_nodes; pn != NULL; pn = pn->pn_next)
  523                 if (pn->pn_type == pfstype_procdir)
  524                         pdn = pn;
  525                 else if (pn->pn_name[namelen] == '\0' &&
  526                     bcmp(pname, pn->pn_name, namelen) == 0) {
  527                         pfs_unlock(pd);
  528                         goto got_pnode;
  529                 }
  530 
  531         /* process dependent node */
  532         if ((pn = pdn) != NULL) {
  533                 pid = 0;
  534                 for (pid = 0, i = 0; i < namelen && isdigit(pname[i]); ++i)
  535                         if ((pid = pid * 10 + pname[i] - '') > PID_MAX)
  536                                 break;
  537                 if (i == cnp->cn_namelen) {
  538                         pfs_unlock(pd);
  539                         goto got_pnode;
  540                 }
  541         }
  542 
  543         pfs_unlock(pd);
  544 
  545         PFS_RETURN (ENOENT);
  546 
  547  got_pnode:
  548         pfs_assert_not_owned(pd);
  549         pfs_assert_not_owned(pn);
  550         visible = pfs_visible(curthread, pn, pid, NULL);
  551         if (!visible) {
  552                 error = ENOENT;
  553                 goto failed;
  554         }
  555 
  556         error = pfs_vncache_alloc(mp, vpp, pn, pid);
  557         if (error)
  558                 goto failed;
  559 
  560         if (cnp->cn_flags & ISDOTDOT) {
  561                 vfs_unbusy(mp);
  562                 vn_lock(vn, LK_EXCLUSIVE | LK_RETRY);
  563                 if (vn->v_iflag & VI_DOOMED) {
  564                         vput(*vpp);
  565                         *vpp = NULL;
  566                         PFS_RETURN(ENOENT);
  567                 }
  568         }
  569         if (cnp->cn_flags & MAKEENTRY && !(vn->v_iflag & VI_DOOMED))
  570                 cache_enter(vn, *vpp, cnp);
  571         PFS_RETURN (0);
  572  failed:
  573         if (cnp->cn_flags & ISDOTDOT) {
  574                 vfs_unbusy(mp);
  575                 vn_lock(vn, LK_EXCLUSIVE | LK_RETRY);
  576                 *vpp = NULL;
  577         }
  578         PFS_RETURN(error);
  579 }
  580 
  581 /*
  582  * Open a file or directory.
  583  */
  584 static int
  585 pfs_open(struct vop_open_args *va)
  586 {
  587         struct vnode *vn = va->a_vp;
  588         struct pfs_vdata *pvd = vn->v_data;
  589         struct pfs_node *pn = pvd->pvd_pn;
  590         int mode = va->a_mode;
  591 
  592         PFS_TRACE(("%s (mode 0x%x)", pn->pn_name, mode));
  593         pfs_assert_not_owned(pn);
  594 
  595         /* check if the requested mode is permitted */
  596         if (((mode & FREAD) && !(mode & PFS_RD)) ||
  597             ((mode & FWRITE) && !(mode & PFS_WR)))
  598                 PFS_RETURN (EPERM);
  599 
  600         /* we don't support locking */
  601         if ((mode & O_SHLOCK) || (mode & O_EXLOCK))
  602                 PFS_RETURN (EOPNOTSUPP);
  603 
  604         PFS_RETURN (0);
  605 }
  606 
  607 /*
  608  * Read from a file
  609  */
  610 static int
  611 pfs_read(struct vop_read_args *va)
  612 {
  613         struct vnode *vn = va->a_vp;
  614         struct pfs_vdata *pvd = vn->v_data;
  615         struct pfs_node *pn = pvd->pvd_pn;
  616         struct uio *uio = va->a_uio;
  617         struct proc *proc;
  618         struct sbuf *sb = NULL;
  619         int error, locked;
  620         unsigned int buflen, offset, resid;
  621 
  622         PFS_TRACE(("%s", pn->pn_name));
  623         pfs_assert_not_owned(pn);
  624 
  625         if (vn->v_type != VREG)
  626                 PFS_RETURN (EINVAL);
  627         KASSERT_PN_IS_FILE(pn);
  628 
  629         if (!(pn->pn_flags & PFS_RD))
  630                 PFS_RETURN (EBADF);
  631 
  632         if (pn->pn_fill == NULL)
  633                 PFS_RETURN (EIO);
  634 
  635         /*
  636          * This is necessary because either process' privileges may
  637          * have changed since the open() call.
  638          */
  639         if (!pfs_visible(curthread, pn, pvd->pvd_pid, &proc))
  640                 PFS_RETURN (EIO);
  641         if (proc != NULL) {
  642                 _PHOLD(proc);
  643                 PROC_UNLOCK(proc);
  644         }
  645 
  646         vhold(vn);
  647         locked = VOP_ISLOCKED(vn);
  648         VOP_UNLOCK(vn, 0);
  649 
  650         if (pn->pn_flags & PFS_RAWRD) {
  651                 PFS_TRACE(("%zd resid", uio->uio_resid));
  652                 error = pn_fill(curthread, proc, pn, NULL, uio);
  653                 PFS_TRACE(("%zd resid", uio->uio_resid));
  654                 goto ret;
  655         }
  656 
  657         /* beaucoup sanity checks so we don't ask for bogus allocation */
  658         if (uio->uio_offset < 0 || uio->uio_resid < 0 ||
  659             (offset = uio->uio_offset) != uio->uio_offset ||
  660             (resid = uio->uio_resid) != uio->uio_resid ||
  661             (buflen = offset + resid + 1) < offset || buflen > INT_MAX) {
  662                 error = EINVAL;
  663                 goto ret;
  664         }
  665         if (buflen > MAXPHYS + 1)
  666                 buflen = MAXPHYS + 1;
  667 
  668         sb = sbuf_new(sb, NULL, buflen, 0);
  669         if (sb == NULL) {
  670                 error = EIO;
  671                 goto ret;
  672         }
  673 
  674         error = pn_fill(curthread, proc, pn, sb, uio);
  675 
  676         if (error) {
  677                 sbuf_delete(sb);
  678                 goto ret;
  679         }
  680 
  681         sbuf_finish(sb);
  682         error = uiomove_frombuf(sbuf_data(sb), sbuf_len(sb), uio);
  683         sbuf_delete(sb);
  684 ret:
  685         vn_lock(vn, locked | LK_RETRY);
  686         vdrop(vn);
  687         if (proc != NULL)
  688                 PRELE(proc);
  689         PFS_RETURN (error);
  690 }
  691 
  692 /*
  693  * Iterate through directory entries
  694  */
  695 static int
  696 pfs_iterate(struct thread *td, struct proc *proc, struct pfs_node *pd,
  697             struct pfs_node **pn, struct proc **p)
  698 {
  699         int visible;
  700 
  701         sx_assert(&allproc_lock, SX_SLOCKED);
  702         pfs_assert_owned(pd);
  703  again:
  704         if (*pn == NULL) {
  705                 /* first node */
  706                 *pn = pd->pn_nodes;
  707         } else if ((*pn)->pn_type != pfstype_procdir) {
  708                 /* next node */
  709                 *pn = (*pn)->pn_next;
  710         }
  711         if (*pn != NULL && (*pn)->pn_type == pfstype_procdir) {
  712                 /* next process */
  713                 if (*p == NULL)
  714                         *p = LIST_FIRST(&allproc);
  715                 else
  716                         *p = LIST_NEXT(*p, p_list);
  717                 /* out of processes: next node */
  718                 if (*p == NULL)
  719                         *pn = (*pn)->pn_next;
  720                 else
  721                         PROC_LOCK(*p);
  722         }
  723 
  724         if ((*pn) == NULL)
  725                 return (-1);
  726 
  727         if (*p != NULL) {
  728                 visible = pfs_visible_proc(td, *pn, *p);
  729                 PROC_UNLOCK(*p);
  730         } else if (proc != NULL) {
  731                 visible = pfs_visible_proc(td, *pn, proc);
  732         } else {
  733                 visible = 1;
  734         }
  735         if (!visible)
  736                 goto again;
  737 
  738         return (0);
  739 }
  740 
  741 /* Directory entry list */
  742 struct pfsentry {
  743         STAILQ_ENTRY(pfsentry)  link;
  744         struct dirent           entry;
  745 };
  746 STAILQ_HEAD(pfsdirentlist, pfsentry);
  747 
  748 /*
  749  * Return directory entries.
  750  */
  751 static int
  752 pfs_readdir(struct vop_readdir_args *va)
  753 {
  754         struct vnode *vn = va->a_vp;
  755         struct pfs_vdata *pvd = vn->v_data;
  756         struct pfs_node *pd = pvd->pvd_pn;
  757         pid_t pid = pvd->pvd_pid;
  758         struct proc *p, *proc;
  759         struct pfs_node *pn;
  760         struct uio *uio;
  761         struct pfsentry *pfsent, *pfsent2;
  762         struct pfsdirentlist lst;
  763         off_t offset;
  764         int error, i, resid;
  765 
  766         STAILQ_INIT(&lst);
  767         error = 0;
  768         KASSERT(pd->pn_info == vn->v_mount->mnt_data,
  769             ("%s(): pn_info does not match mountpoint", __func__));
  770         PFS_TRACE(("%s pid %lu", pd->pn_name, (unsigned long)pid));
  771         pfs_assert_not_owned(pd);
  772 
  773         if (vn->v_type != VDIR)
  774                 PFS_RETURN (ENOTDIR);
  775         KASSERT_PN_IS_DIR(pd);
  776         uio = va->a_uio;
  777 
  778         /* only allow reading entire entries */
  779         offset = uio->uio_offset;
  780         resid = uio->uio_resid;
  781         if (offset < 0 || offset % PFS_DELEN != 0 ||
  782             (resid && resid < PFS_DELEN))
  783                 PFS_RETURN (EINVAL);
  784         if (resid == 0)
  785                 PFS_RETURN (0);
  786 
  787         sx_slock(&allproc_lock);
  788         pfs_lock(pd);
  789 
  790         /* check if the directory is visible to the caller */
  791         if (!pfs_visible(curthread, pd, pid, &proc)) {
  792                 sx_sunlock(&allproc_lock);
  793                 pfs_unlock(pd);
  794                 PFS_RETURN (ENOENT);
  795         }
  796         KASSERT(pid == NO_PID || proc != NULL,
  797             ("%s(): no process for pid %lu", __func__, (unsigned long)pid));
  798 
  799         /* skip unwanted entries */
  800         for (pn = NULL, p = NULL; offset > 0; offset -= PFS_DELEN) {
  801                 if (pfs_iterate(curthread, proc, pd, &pn, &p) == -1) {
  802                         /* nothing left... */
  803                         if (proc != NULL)
  804                                 PROC_UNLOCK(proc);
  805                         pfs_unlock(pd);
  806                         sx_sunlock(&allproc_lock);
  807                         PFS_RETURN (0);
  808                 }
  809         }
  810 
  811         /* fill in entries */
  812         while (pfs_iterate(curthread, proc, pd, &pn, &p) != -1 &&
  813             resid >= PFS_DELEN) {
  814                 if ((pfsent = malloc(sizeof(struct pfsentry), M_IOV,
  815                     M_NOWAIT | M_ZERO)) == NULL) {
  816                         error = ENOMEM;
  817                         break;
  818                 }
  819                 pfsent->entry.d_reclen = PFS_DELEN;
  820                 pfsent->entry.d_fileno = pn_fileno(pn, pid);
  821                 /* PFS_DELEN was picked to fit PFS_NAMLEN */
  822                 for (i = 0; i < PFS_NAMELEN - 1 && pn->pn_name[i] != '\0'; ++i)
  823                         pfsent->entry.d_name[i] = pn->pn_name[i];
  824                 pfsent->entry.d_name[i] = 0;
  825                 pfsent->entry.d_namlen = i;
  826                 switch (pn->pn_type) {
  827                 case pfstype_procdir:
  828                         KASSERT(p != NULL,
  829                             ("reached procdir node with p == NULL"));
  830                         pfsent->entry.d_namlen = snprintf(pfsent->entry.d_name,
  831                             PFS_NAMELEN, "%d", p->p_pid);
  832                         /* fall through */
  833                 case pfstype_root:
  834                 case pfstype_dir:
  835                 case pfstype_this:
  836                 case pfstype_parent:
  837                         pfsent->entry.d_type = DT_DIR;
  838                         break;
  839                 case pfstype_file:
  840                         pfsent->entry.d_type = DT_REG;
  841                         break;
  842                 case pfstype_symlink:
  843                         pfsent->entry.d_type = DT_LNK;
  844                         break;
  845                 default:
  846                         panic("%s has unexpected node type: %d", pn->pn_name, pn->pn_type);
  847                 }
  848                 PFS_TRACE(("%s", pfsent->entry.d_name));
  849                 STAILQ_INSERT_TAIL(&lst, pfsent, link);
  850                 offset += PFS_DELEN;
  851                 resid -= PFS_DELEN;
  852         }
  853         if (proc != NULL)
  854                 PROC_UNLOCK(proc);
  855         pfs_unlock(pd);
  856         sx_sunlock(&allproc_lock);
  857         i = 0;
  858         STAILQ_FOREACH_SAFE(pfsent, &lst, link, pfsent2) {
  859                 if (error == 0)
  860                         error = uiomove(&pfsent->entry, PFS_DELEN, uio);
  861                 free(pfsent, M_IOV);
  862                 i++;
  863         }
  864         PFS_TRACE(("%d bytes", i * PFS_DELEN));
  865         PFS_RETURN (error);
  866 }
  867 
  868 /*
  869  * Read a symbolic link
  870  */
  871 static int
  872 pfs_readlink(struct vop_readlink_args *va)
  873 {
  874         struct vnode *vn = va->a_vp;
  875         struct pfs_vdata *pvd = vn->v_data;
  876         struct pfs_node *pn = pvd->pvd_pn;
  877         struct uio *uio = va->a_uio;
  878         struct proc *proc = NULL;
  879         char buf[PATH_MAX];
  880         struct sbuf sb;
  881         int error, locked;
  882 
  883         PFS_TRACE(("%s", pn->pn_name));
  884         pfs_assert_not_owned(pn);
  885 
  886         if (vn->v_type != VLNK)
  887                 PFS_RETURN (EINVAL);
  888         KASSERT_PN_IS_LINK(pn);
  889 
  890         if (pn->pn_fill == NULL)
  891                 PFS_RETURN (EIO);
  892 
  893         if (pvd->pvd_pid != NO_PID) {
  894                 if ((proc = pfind(pvd->pvd_pid)) == NULL)
  895                         PFS_RETURN (EIO);
  896                 if (proc->p_flag & P_WEXIT) {
  897                         PROC_UNLOCK(proc);
  898                         PFS_RETURN (EIO);
  899                 }
  900                 _PHOLD(proc);
  901                 PROC_UNLOCK(proc);
  902         }
  903         vhold(vn);
  904         locked = VOP_ISLOCKED(vn);
  905         VOP_UNLOCK(vn, 0);
  906 
  907         /* sbuf_new() can't fail with a static buffer */
  908         sbuf_new(&sb, buf, sizeof buf, 0);
  909 
  910         error = pn_fill(curthread, proc, pn, &sb, NULL);
  911 
  912         if (proc != NULL)
  913                 PRELE(proc);
  914         vn_lock(vn, locked | LK_RETRY);
  915         vdrop(vn);
  916 
  917         if (error) {
  918                 sbuf_delete(&sb);
  919                 PFS_RETURN (error);
  920         }
  921 
  922         if (sbuf_finish(&sb) != 0) {
  923                 sbuf_delete(&sb);
  924                 PFS_RETURN (ENAMETOOLONG);
  925         }
  926 
  927         error = uiomove_frombuf(sbuf_data(&sb), sbuf_len(&sb), uio);
  928         sbuf_delete(&sb);
  929         PFS_RETURN (error);
  930 }
  931 
  932 /*
  933  * Reclaim a vnode
  934  */
  935 static int
  936 pfs_reclaim(struct vop_reclaim_args *va)
  937 {
  938         struct vnode *vn = va->a_vp;
  939         struct pfs_vdata *pvd = vn->v_data;
  940         struct pfs_node *pn = pvd->pvd_pn;
  941 
  942         PFS_TRACE(("%s", pn->pn_name));
  943         pfs_assert_not_owned(pn);
  944 
  945         return (pfs_vncache_free(va->a_vp));
  946 }
  947 
  948 /*
  949  * Set attributes
  950  */
  951 static int
  952 pfs_setattr(struct vop_setattr_args *va)
  953 {
  954         struct vnode *vn = va->a_vp;
  955         struct pfs_vdata *pvd = vn->v_data;
  956         struct pfs_node *pn = pvd->pvd_pn;
  957 
  958         PFS_TRACE(("%s", pn->pn_name));
  959         pfs_assert_not_owned(pn);
  960 
  961         PFS_RETURN (EOPNOTSUPP);
  962 }
  963 
  964 /*
  965  * Write to a file
  966  */
  967 static int
  968 pfs_write(struct vop_write_args *va)
  969 {
  970         struct vnode *vn = va->a_vp;
  971         struct pfs_vdata *pvd = vn->v_data;
  972         struct pfs_node *pn = pvd->pvd_pn;
  973         struct uio *uio = va->a_uio;
  974         struct proc *proc;
  975         struct sbuf sb;
  976         int error;
  977 
  978         PFS_TRACE(("%s", pn->pn_name));
  979         pfs_assert_not_owned(pn);
  980 
  981         if (vn->v_type != VREG)
  982                 PFS_RETURN (EINVAL);
  983         KASSERT_PN_IS_FILE(pn);
  984 
  985         if (!(pn->pn_flags & PFS_WR))
  986                 PFS_RETURN (EBADF);
  987 
  988         if (pn->pn_fill == NULL)
  989                 PFS_RETURN (EIO);
  990 
  991         /*
  992          * This is necessary because either process' privileges may
  993          * have changed since the open() call.
  994          */
  995         if (!pfs_visible(curthread, pn, pvd->pvd_pid, &proc))
  996                 PFS_RETURN (EIO);
  997         if (proc != NULL) {
  998                 _PHOLD(proc);
  999                 PROC_UNLOCK(proc);
 1000         }
 1001 
 1002         if (pn->pn_flags & PFS_RAWWR) {
 1003                 error = pn_fill(curthread, proc, pn, NULL, uio);
 1004                 if (proc != NULL)
 1005                         PRELE(proc);
 1006                 PFS_RETURN (error);
 1007         }
 1008 
 1009         sbuf_uionew(&sb, uio, &error);
 1010         if (error) {
 1011                 if (proc != NULL)
 1012                         PRELE(proc);
 1013                 PFS_RETURN (error);
 1014         }
 1015 
 1016         error = pn_fill(curthread, proc, pn, &sb, uio);
 1017 
 1018         sbuf_delete(&sb);
 1019         if (proc != NULL)
 1020                 PRELE(proc);
 1021         PFS_RETURN (error);
 1022 }
 1023 
 1024 /*
 1025  * Vnode operations
 1026  */
 1027 struct vop_vector pfs_vnodeops = {
 1028         .vop_default =          &default_vnodeops,
 1029 
 1030         .vop_access =           pfs_access,
 1031         .vop_cachedlookup =     pfs_lookup,
 1032         .vop_close =            pfs_close,
 1033         .vop_create =           VOP_EOPNOTSUPP,
 1034         .vop_getattr =          pfs_getattr,
 1035         .vop_getextattr =       pfs_getextattr,
 1036         .vop_ioctl =            pfs_ioctl,
 1037         .vop_link =             VOP_EOPNOTSUPP,
 1038         .vop_lookup =           vfs_cache_lookup,
 1039         .vop_mkdir =            VOP_EOPNOTSUPP,
 1040         .vop_mknod =            VOP_EOPNOTSUPP,
 1041         .vop_open =             pfs_open,
 1042         .vop_read =             pfs_read,
 1043         .vop_readdir =          pfs_readdir,
 1044         .vop_readlink =         pfs_readlink,
 1045         .vop_reclaim =          pfs_reclaim,
 1046         .vop_remove =           VOP_EOPNOTSUPP,
 1047         .vop_rename =           VOP_EOPNOTSUPP,
 1048         .vop_rmdir =            VOP_EOPNOTSUPP,
 1049         .vop_setattr =          pfs_setattr,
 1050         .vop_symlink =          VOP_EOPNOTSUPP,
 1051         .vop_vptocnp =          pfs_vptocnp,
 1052         .vop_write =            pfs_write,
 1053         /* XXX I've probably forgotten a few that need VOP_EOPNOTSUPP */
 1054 };

Cache object: 162d96e80bdb3641027939da92b334b2


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