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

Cache object: 9e1fc7474794ccf8005617f3307d9c6c


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