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-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) 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: releng/8.0/sys/fs/pseudofs/pseudofs_vnops.c 196859 2009-09-05 13:10: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         pid_t pid = pvd->pvd_pid;
  437         char *pname;
  438         int error, i, namelen, visible;
  439 
  440         PFS_TRACE(("%.*s", (int)cnp->cn_namelen, cnp->cn_nameptr));
  441         pfs_assert_not_owned(pd);
  442 
  443         if (vn->v_type != VDIR)
  444                 PFS_RETURN (ENOTDIR);
  445         KASSERT_PN_IS_DIR(pd);
  446 
  447         error = VOP_ACCESS(vn, VEXEC, cnp->cn_cred, cnp->cn_thread);
  448         if (error)
  449                 PFS_RETURN (error);
  450 
  451         /*
  452          * Don't support DELETE or RENAME.  CREATE is supported so
  453          * that O_CREAT will work, but the lookup will still fail if
  454          * the file does not exist.
  455          */
  456         if ((cnp->cn_flags & ISLASTCN) &&
  457             (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
  458                 PFS_RETURN (EOPNOTSUPP);
  459 
  460         /* shortcut: check if the name is too long */
  461         if (cnp->cn_namelen >= PFS_NAMELEN)
  462                 PFS_RETURN (ENOENT);
  463 
  464         /* check that parent directory is visible... */
  465         if (!pfs_visible(curthread, pd, pvd->pvd_pid, NULL))
  466                 PFS_RETURN (ENOENT);
  467 
  468         /* self */
  469         namelen = cnp->cn_namelen;
  470         pname = cnp->cn_nameptr;
  471         if (namelen == 1 && pname[0] == '.') {
  472                 pn = pd;
  473                 *vpp = vn;
  474                 VREF(vn);
  475                 PFS_RETURN (0);
  476         }
  477 
  478         /* parent */
  479         if (cnp->cn_flags & ISDOTDOT) {
  480                 if (pd->pn_type == pfstype_root)
  481                         PFS_RETURN (EIO);
  482                 VOP_UNLOCK(vn, 0);
  483                 KASSERT(pd->pn_parent != NULL,
  484                     ("%s(): non-root directory has no parent", __func__));
  485                 /*
  486                  * This one is tricky.  Descendents of procdir nodes
  487                  * inherit their parent's process affinity, but
  488                  * there's no easy reverse mapping.  For simplicity,
  489                  * we assume that if this node is a procdir, its
  490                  * parent isn't (which is correct as long as
  491                  * descendents of procdir nodes are never procdir
  492                  * nodes themselves)
  493                  */
  494                 if (pd->pn_type == pfstype_procdir)
  495                         pid = NO_PID;
  496                 pfs_lock(pd);
  497                 pn = pd->pn_parent;
  498                 pfs_unlock(pd);
  499                 goto got_pnode;
  500         }
  501 
  502         pfs_lock(pd);
  503 
  504         /* named node */
  505         for (pn = pd->pn_nodes; pn != NULL; pn = pn->pn_next)
  506                 if (pn->pn_type == pfstype_procdir)
  507                         pdn = pn;
  508                 else if (pn->pn_name[namelen] == '\0' &&
  509                     bcmp(pname, pn->pn_name, namelen) == 0) {
  510                         pfs_unlock(pd);
  511                         goto got_pnode;
  512                 }
  513 
  514         /* process dependent node */
  515         if ((pn = pdn) != NULL) {
  516                 pid = 0;
  517                 for (pid = 0, i = 0; i < namelen && isdigit(pname[i]); ++i)
  518                         if ((pid = pid * 10 + pname[i] - '') > PID_MAX)
  519                                 break;
  520                 if (i == cnp->cn_namelen) {
  521                         pfs_unlock(pd);
  522                         goto got_pnode;
  523                 }
  524         }
  525 
  526         pfs_unlock(pd);
  527 
  528         PFS_RETURN (ENOENT);
  529 
  530  got_pnode:
  531         pfs_assert_not_owned(pd);
  532         pfs_assert_not_owned(pn);
  533         visible = pfs_visible(curthread, pn, pid, NULL);
  534         if (!visible) {
  535                 error = ENOENT;
  536                 goto failed;
  537         }
  538 
  539         error = pfs_vncache_alloc(vn->v_mount, vpp, pn, pid);
  540         if (error)
  541                 goto failed;
  542 
  543         if (cnp->cn_flags & ISDOTDOT)
  544                 vn_lock(vn, LK_EXCLUSIVE|LK_RETRY);
  545         if (cnp->cn_flags & MAKEENTRY)
  546                 cache_enter(vn, *vpp, cnp);
  547         PFS_RETURN (0);
  548  failed:
  549         if (cnp->cn_flags & ISDOTDOT)
  550                 vn_lock(vn, LK_EXCLUSIVE|LK_RETRY);
  551         PFS_RETURN(error);
  552 }
  553 
  554 /*
  555  * Open a file or directory.
  556  */
  557 static int
  558 pfs_open(struct vop_open_args *va)
  559 {
  560         struct vnode *vn = va->a_vp;
  561         struct pfs_vdata *pvd = vn->v_data;
  562         struct pfs_node *pn = pvd->pvd_pn;
  563         int mode = va->a_mode;
  564 
  565         PFS_TRACE(("%s (mode 0x%x)", pn->pn_name, mode));
  566         pfs_assert_not_owned(pn);
  567 
  568         /* check if the requested mode is permitted */
  569         if (((mode & FREAD) && !(mode & PFS_RD)) ||
  570             ((mode & FWRITE) && !(mode & PFS_WR)))
  571                 PFS_RETURN (EPERM);
  572 
  573         /* we don't support locking */
  574         if ((mode & O_SHLOCK) || (mode & O_EXLOCK))
  575                 PFS_RETURN (EOPNOTSUPP);
  576 
  577         PFS_RETURN (0);
  578 }
  579 
  580 /*
  581  * Read from a file
  582  */
  583 static int
  584 pfs_read(struct vop_read_args *va)
  585 {
  586         struct vnode *vn = va->a_vp;
  587         struct pfs_vdata *pvd = vn->v_data;
  588         struct pfs_node *pn = pvd->pvd_pn;
  589         struct uio *uio = va->a_uio;
  590         struct proc *proc;
  591         struct sbuf *sb = NULL;
  592         int error, locked;
  593         unsigned int buflen, offset, resid;
  594 
  595         PFS_TRACE(("%s", pn->pn_name));
  596         pfs_assert_not_owned(pn);
  597 
  598         if (vn->v_type != VREG)
  599                 PFS_RETURN (EINVAL);
  600         KASSERT_PN_IS_FILE(pn);
  601 
  602         if (!(pn->pn_flags & PFS_RD))
  603                 PFS_RETURN (EBADF);
  604 
  605         if (pn->pn_fill == NULL)
  606                 PFS_RETURN (EIO);
  607 
  608         /*
  609          * This is necessary because either process' privileges may
  610          * have changed since the open() call.
  611          */
  612         if (!pfs_visible(curthread, pn, pvd->pvd_pid, &proc))
  613                 PFS_RETURN (EIO);
  614         if (proc != NULL) {
  615                 _PHOLD(proc);
  616                 PROC_UNLOCK(proc);
  617         }
  618 
  619         vhold(vn);
  620         locked = VOP_ISLOCKED(vn);
  621         VOP_UNLOCK(vn, 0);
  622 
  623         if (pn->pn_flags & PFS_RAWRD) {
  624                 PFS_TRACE(("%zd resid", uio->uio_resid));
  625                 error = pn_fill(curthread, proc, pn, NULL, uio);
  626                 PFS_TRACE(("%zd resid", uio->uio_resid));
  627                 goto ret;
  628         }
  629 
  630         /* beaucoup sanity checks so we don't ask for bogus allocation */
  631         if (uio->uio_offset < 0 || uio->uio_resid < 0 ||
  632             (offset = uio->uio_offset) != uio->uio_offset ||
  633             (resid = uio->uio_resid) != uio->uio_resid ||
  634             (buflen = offset + resid + 1) < offset || buflen > INT_MAX) {
  635                 if (proc != NULL)
  636                         PRELE(proc);
  637                 error = EINVAL;
  638                 goto ret;
  639         }
  640         if (buflen > MAXPHYS + 1) {
  641                 error = EIO;
  642                 goto ret;
  643         }
  644 
  645         sb = sbuf_new(sb, NULL, buflen, 0);
  646         if (sb == NULL) {
  647                 error = EIO;
  648                 goto ret;
  649         }
  650 
  651         error = pn_fill(curthread, proc, pn, sb, uio);
  652 
  653         if (error) {
  654                 sbuf_delete(sb);
  655                 goto ret;
  656         }
  657 
  658         sbuf_finish(sb);
  659         error = uiomove_frombuf(sbuf_data(sb), sbuf_len(sb), uio);
  660         sbuf_delete(sb);
  661 ret:
  662         vn_lock(vn, locked | LK_RETRY);
  663         vdrop(vn);
  664         if (proc != NULL)
  665                 PRELE(proc);
  666         PFS_RETURN (error);
  667 }
  668 
  669 /*
  670  * Iterate through directory entries
  671  */
  672 static int
  673 pfs_iterate(struct thread *td, struct proc *proc, struct pfs_node *pd,
  674             struct pfs_node **pn, struct proc **p)
  675 {
  676         int visible;
  677 
  678         sx_assert(&allproc_lock, SX_SLOCKED);
  679         pfs_assert_owned(pd);
  680  again:
  681         if (*pn == NULL) {
  682                 /* first node */
  683                 *pn = pd->pn_nodes;
  684         } else if ((*pn)->pn_type != pfstype_procdir) {
  685                 /* next node */
  686                 *pn = (*pn)->pn_next;
  687         }
  688         if (*pn != NULL && (*pn)->pn_type == pfstype_procdir) {
  689                 /* next process */
  690                 if (*p == NULL)
  691                         *p = LIST_FIRST(&allproc);
  692                 else
  693                         *p = LIST_NEXT(*p, p_list);
  694                 /* out of processes: next node */
  695                 if (*p == NULL)
  696                         *pn = (*pn)->pn_next;
  697                 else
  698                         PROC_LOCK(*p);
  699         }
  700 
  701         if ((*pn) == NULL)
  702                 return (-1);
  703 
  704         if (*p != NULL) {
  705                 visible = pfs_visible_proc(td, *pn, *p);
  706                 PROC_UNLOCK(*p);
  707         } else if (proc != NULL) {
  708                 visible = pfs_visible_proc(td, *pn, proc);
  709         } else {
  710                 visible = 1;
  711         }
  712         if (!visible)
  713                 goto again;
  714 
  715         return (0);
  716 }
  717 
  718 /*
  719  * Return directory entries.
  720  */
  721 static int
  722 pfs_readdir(struct vop_readdir_args *va)
  723 {
  724         struct vnode *vn = va->a_vp;
  725         struct pfs_vdata *pvd = vn->v_data;
  726         struct pfs_node *pd = pvd->pvd_pn;
  727         pid_t pid = pvd->pvd_pid;
  728         struct proc *p, *proc;
  729         struct pfs_node *pn;
  730         struct dirent *entry;
  731         struct uio *uio;
  732         off_t offset;
  733         int error, i, resid;
  734         char *buf, *ent;
  735 
  736         KASSERT(pd->pn_info == vn->v_mount->mnt_data,
  737             ("%s(): pn_info does not match mountpoint", __func__));
  738         PFS_TRACE(("%s pid %lu", pd->pn_name, (unsigned long)pid));
  739         pfs_assert_not_owned(pd);
  740 
  741         if (vn->v_type != VDIR)
  742                 PFS_RETURN (ENOTDIR);
  743         KASSERT_PN_IS_DIR(pd);
  744         uio = va->a_uio;
  745 
  746         /* only allow reading entire entries */
  747         offset = uio->uio_offset;
  748         resid = uio->uio_resid;
  749         if (offset < 0 || offset % PFS_DELEN != 0 ||
  750             (resid && resid < PFS_DELEN))
  751                 PFS_RETURN (EINVAL);
  752         if (resid == 0)
  753                 PFS_RETURN (0);
  754 
  755         /* can't do this while holding the proc lock... */
  756         buf = malloc(resid, M_IOV, M_WAITOK | M_ZERO);
  757         sx_slock(&allproc_lock);
  758         pfs_lock(pd);
  759 
  760         /* check if the directory is visible to the caller */
  761         if (!pfs_visible(curthread, pd, pid, &proc)) {
  762                 sx_sunlock(&allproc_lock);
  763                 pfs_unlock(pd);
  764                 free(buf, M_IOV);
  765                 PFS_RETURN (ENOENT);
  766         }
  767         KASSERT(pid == NO_PID || proc != NULL,
  768             ("%s(): no process for pid %lu", __func__, (unsigned long)pid));
  769 
  770         /* skip unwanted entries */
  771         for (pn = NULL, p = NULL; offset > 0; offset -= PFS_DELEN) {
  772                 if (pfs_iterate(curthread, proc, pd, &pn, &p) == -1) {
  773                         /* nothing left... */
  774                         if (proc != NULL)
  775                                 PROC_UNLOCK(proc);
  776                         pfs_unlock(pd);
  777                         sx_sunlock(&allproc_lock);
  778                         free(buf, M_IOV);
  779                         PFS_RETURN (0);
  780                 }
  781         }
  782 
  783         /* fill in entries */
  784         ent = buf;
  785         while (pfs_iterate(curthread, proc, pd, &pn, &p) != -1 &&
  786             resid >= PFS_DELEN) {
  787                 entry = (struct dirent *)ent;
  788                 entry->d_reclen = PFS_DELEN;
  789                 entry->d_fileno = pn_fileno(pn, pid);
  790                 /* PFS_DELEN was picked to fit PFS_NAMLEN */
  791                 for (i = 0; i < PFS_NAMELEN - 1 && pn->pn_name[i] != '\0'; ++i)
  792                         entry->d_name[i] = pn->pn_name[i];
  793                 entry->d_name[i] = 0;
  794                 entry->d_namlen = i;
  795                 switch (pn->pn_type) {
  796                 case pfstype_procdir:
  797                         KASSERT(p != NULL,
  798                             ("reached procdir node with p == NULL"));
  799                         entry->d_namlen = snprintf(entry->d_name,
  800                             PFS_NAMELEN, "%d", p->p_pid);
  801                         /* fall through */
  802                 case pfstype_root:
  803                 case pfstype_dir:
  804                 case pfstype_this:
  805                 case pfstype_parent:
  806                         entry->d_type = DT_DIR;
  807                         break;
  808                 case pfstype_file:
  809                         entry->d_type = DT_REG;
  810                         break;
  811                 case pfstype_symlink:
  812                         entry->d_type = DT_LNK;
  813                         break;
  814                 default:
  815                         panic("%s has unexpected node type: %d", pn->pn_name, pn->pn_type);
  816                 }
  817                 PFS_TRACE(("%s", entry->d_name));
  818                 offset += PFS_DELEN;
  819                 resid -= PFS_DELEN;
  820                 ent += PFS_DELEN;
  821         }
  822         if (proc != NULL)
  823                 PROC_UNLOCK(proc);
  824         pfs_unlock(pd);
  825         sx_sunlock(&allproc_lock);
  826         PFS_TRACE(("%zd bytes", ent - buf));
  827         error = uiomove(buf, ent - buf, uio);
  828         free(buf, M_IOV);
  829         PFS_RETURN (error);
  830 }
  831 
  832 /*
  833  * Read a symbolic link
  834  */
  835 static int
  836 pfs_readlink(struct vop_readlink_args *va)
  837 {
  838         struct vnode *vn = va->a_vp;
  839         struct pfs_vdata *pvd = vn->v_data;
  840         struct pfs_node *pn = pvd->pvd_pn;
  841         struct uio *uio = va->a_uio;
  842         struct proc *proc = NULL;
  843         char buf[PATH_MAX];
  844         struct sbuf sb;
  845         int error, locked;
  846 
  847         PFS_TRACE(("%s", pn->pn_name));
  848         pfs_assert_not_owned(pn);
  849 
  850         if (vn->v_type != VLNK)
  851                 PFS_RETURN (EINVAL);
  852         KASSERT_PN_IS_LINK(pn);
  853 
  854         if (pn->pn_fill == NULL)
  855                 PFS_RETURN (EIO);
  856 
  857         if (pvd->pvd_pid != NO_PID) {
  858                 if ((proc = pfind(pvd->pvd_pid)) == NULL)
  859                         PFS_RETURN (EIO);
  860                 if (proc->p_flag & P_WEXIT) {
  861                         PROC_UNLOCK(proc);
  862                         PFS_RETURN (EIO);
  863                 }
  864                 _PHOLD(proc);
  865                 PROC_UNLOCK(proc);
  866         }
  867         vhold(vn);
  868         locked = VOP_ISLOCKED(vn);
  869         VOP_UNLOCK(vn, 0);
  870 
  871         /* sbuf_new() can't fail with a static buffer */
  872         sbuf_new(&sb, buf, sizeof buf, 0);
  873 
  874         error = pn_fill(curthread, proc, pn, &sb, NULL);
  875 
  876         if (proc != NULL)
  877                 PRELE(proc);
  878         vn_lock(vn, locked | LK_RETRY);
  879         vdrop(vn);
  880 
  881         if (error) {
  882                 sbuf_delete(&sb);
  883                 PFS_RETURN (error);
  884         }
  885 
  886         sbuf_finish(&sb);
  887         error = uiomove_frombuf(sbuf_data(&sb), sbuf_len(&sb), uio);
  888         sbuf_delete(&sb);
  889         PFS_RETURN (error);
  890 }
  891 
  892 /*
  893  * Reclaim a vnode
  894  */
  895 static int
  896 pfs_reclaim(struct vop_reclaim_args *va)
  897 {
  898         struct vnode *vn = va->a_vp;
  899         struct pfs_vdata *pvd = vn->v_data;
  900         struct pfs_node *pn = pvd->pvd_pn;
  901 
  902         PFS_TRACE(("%s", pn->pn_name));
  903         pfs_assert_not_owned(pn);
  904 
  905         return (pfs_vncache_free(va->a_vp));
  906 }
  907 
  908 /*
  909  * Set attributes
  910  */
  911 static int
  912 pfs_setattr(struct vop_setattr_args *va)
  913 {
  914         struct vnode *vn = va->a_vp;
  915         struct pfs_vdata *pvd = vn->v_data;
  916         struct pfs_node *pn = pvd->pvd_pn;
  917 
  918         PFS_TRACE(("%s", pn->pn_name));
  919         pfs_assert_not_owned(pn);
  920 
  921         PFS_RETURN (EOPNOTSUPP);
  922 }
  923 
  924 /*
  925  * Write to a file
  926  */
  927 static int
  928 pfs_write(struct vop_write_args *va)
  929 {
  930         struct vnode *vn = va->a_vp;
  931         struct pfs_vdata *pvd = vn->v_data;
  932         struct pfs_node *pn = pvd->pvd_pn;
  933         struct uio *uio = va->a_uio;
  934         struct proc *proc;
  935         struct sbuf sb;
  936         int error;
  937 
  938         PFS_TRACE(("%s", pn->pn_name));
  939         pfs_assert_not_owned(pn);
  940 
  941         if (vn->v_type != VREG)
  942                 PFS_RETURN (EINVAL);
  943         KASSERT_PN_IS_FILE(pn);
  944 
  945         if (!(pn->pn_flags & PFS_WR))
  946                 PFS_RETURN (EBADF);
  947 
  948         if (pn->pn_fill == NULL)
  949                 PFS_RETURN (EIO);
  950 
  951         /*
  952          * This is necessary because either process' privileges may
  953          * have changed since the open() call.
  954          */
  955         if (!pfs_visible(curthread, pn, pvd->pvd_pid, &proc))
  956                 PFS_RETURN (EIO);
  957         if (proc != NULL) {
  958                 _PHOLD(proc);
  959                 PROC_UNLOCK(proc);
  960         }
  961 
  962         if (pn->pn_flags & PFS_RAWWR) {
  963                 error = pn_fill(curthread, proc, pn, NULL, uio);
  964                 if (proc != NULL)
  965                         PRELE(proc);
  966                 PFS_RETURN (error);
  967         }
  968 
  969         sbuf_uionew(&sb, uio, &error);
  970         if (error) {
  971                 if (proc != NULL)
  972                         PRELE(proc);
  973                 PFS_RETURN (error);
  974         }
  975 
  976         error = pn_fill(curthread, proc, pn, &sb, uio);
  977 
  978         sbuf_delete(&sb);
  979         if (proc != NULL)
  980                 PRELE(proc);
  981         PFS_RETURN (error);
  982 }
  983 
  984 /*
  985  * Vnode operations
  986  */
  987 struct vop_vector pfs_vnodeops = {
  988         .vop_default =          &default_vnodeops,
  989 
  990         .vop_access =           pfs_access,
  991         .vop_cachedlookup =     pfs_lookup,
  992         .vop_close =            pfs_close,
  993         .vop_create =           VOP_EOPNOTSUPP,
  994         .vop_getattr =          pfs_getattr,
  995         .vop_getextattr =       pfs_getextattr,
  996         .vop_ioctl =            pfs_ioctl,
  997         .vop_link =             VOP_EOPNOTSUPP,
  998         .vop_lookup =           vfs_cache_lookup,
  999         .vop_mkdir =            VOP_EOPNOTSUPP,
 1000         .vop_mknod =            VOP_EOPNOTSUPP,
 1001         .vop_open =             pfs_open,
 1002         .vop_read =             pfs_read,
 1003         .vop_readdir =          pfs_readdir,
 1004         .vop_readlink =         pfs_readlink,
 1005         .vop_reclaim =          pfs_reclaim,
 1006         .vop_remove =           VOP_EOPNOTSUPP,
 1007         .vop_rename =           VOP_EOPNOTSUPP,
 1008         .vop_rmdir =            VOP_EOPNOTSUPP,
 1009         .vop_setattr =          pfs_setattr,
 1010         .vop_symlink =          VOP_EOPNOTSUPP,
 1011         .vop_vptocnp =          pfs_vptocnp,
 1012         .vop_write =            pfs_write,
 1013         /* XXX I've probably forgotten a few that need VOP_EOPNOTSUPP */
 1014 };

Cache object: 61512dc9891362ed08314569ba2ed5ef


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