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/fdescfs/fdesc_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  * SPDX-License-Identifier: BSD-3-Clause
    3  *
    4  * Copyright (c) 1992, 1993
    5  *      The Regents of the University of California.  All rights reserved.
    6  *
    7  * This code is derived from software donated to Berkeley by
    8  * Jan-Simon Pendry.
    9  *
   10  * Redistribution and use in source and binary forms, with or without
   11  * modification, are permitted provided that the following conditions
   12  * are met:
   13  * 1. Redistributions of source code must retain the above copyright
   14  *    notice, this list of conditions and the following disclaimer.
   15  * 2. Redistributions in binary form must reproduce the above copyright
   16  *    notice, this list of conditions and the following disclaimer in the
   17  *    documentation and/or other materials provided with the distribution.
   18  * 3. Neither the name of the University nor the names of its contributors
   19  *    may be used to endorse or promote products derived from this software
   20  *    without specific prior written permission.
   21  *
   22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   32  * SUCH DAMAGE.
   33  *
   34  *      @(#)fdesc_vnops.c       8.9 (Berkeley) 1/21/94
   35  *
   36  * $FreeBSD$
   37  */
   38 
   39 /*
   40  * /dev/fd Filesystem
   41  */
   42 
   43 #include <sys/param.h>
   44 #include <sys/systm.h>
   45 #include <sys/capsicum.h>
   46 #include <sys/conf.h>
   47 #include <sys/dirent.h>
   48 #include <sys/filedesc.h>
   49 #include <sys/kernel.h> /* boottime */
   50 #include <sys/lock.h>
   51 #include <sys/mutex.h>
   52 #include <sys/malloc.h>
   53 #include <sys/file.h>   /* Must come after sys/malloc.h */
   54 #include <sys/mount.h>
   55 #include <sys/namei.h>
   56 #include <sys/proc.h>
   57 #include <sys/stat.h>
   58 #include <sys/syscallsubr.h>
   59 #include <sys/unistd.h>
   60 #include <sys/vnode.h>
   61 
   62 #include <fs/fdescfs/fdesc.h>
   63 
   64 #define NFDCACHE 4
   65 #define FD_NHASH(ix) \
   66         (&fdhashtbl[(ix) & fdhash])
   67 static LIST_HEAD(fdhashhead, fdescnode) *fdhashtbl;
   68 static u_long fdhash;
   69 
   70 struct mtx fdesc_hashmtx;
   71 
   72 static vop_getattr_t    fdesc_getattr;
   73 static vop_lookup_t     fdesc_lookup;
   74 static vop_open_t       fdesc_open;
   75 static vop_pathconf_t   fdesc_pathconf;
   76 static vop_readdir_t    fdesc_readdir;
   77 static vop_readlink_t   fdesc_readlink;
   78 static vop_reclaim_t    fdesc_reclaim;
   79 static vop_setattr_t    fdesc_setattr;
   80 
   81 static struct vop_vector fdesc_vnodeops = {
   82         .vop_default =          &default_vnodeops,
   83 
   84         .vop_access =           VOP_NULL,
   85         .vop_getattr =          fdesc_getattr,
   86         .vop_lookup =           fdesc_lookup,
   87         .vop_open =             fdesc_open,
   88         .vop_pathconf =         fdesc_pathconf,
   89         .vop_readdir =          fdesc_readdir,
   90         .vop_readlink =         fdesc_readlink,
   91         .vop_reclaim =          fdesc_reclaim,
   92         .vop_setattr =          fdesc_setattr,
   93 };
   94 VFS_VOP_VECTOR_REGISTER(fdesc_vnodeops);
   95 
   96 static void fdesc_insmntque_dtr(struct vnode *, void *);
   97 static void fdesc_remove_entry(struct fdescnode *);
   98 
   99 /*
  100  * Initialise cache headers
  101  */
  102 int
  103 fdesc_init(struct vfsconf *vfsp)
  104 {
  105 
  106         mtx_init(&fdesc_hashmtx, "fdescfs_hash", NULL, MTX_DEF);
  107         fdhashtbl = hashinit(NFDCACHE, M_CACHE, &fdhash);
  108         return (0);
  109 }
  110 
  111 /*
  112  * Uninit ready for unload.
  113  */
  114 int
  115 fdesc_uninit(struct vfsconf *vfsp)
  116 {
  117 
  118         hashdestroy(fdhashtbl, M_CACHE, fdhash);
  119         mtx_destroy(&fdesc_hashmtx);
  120         return (0);
  121 }
  122 
  123 /*
  124  * If allocating vnode fails, call this.
  125  */
  126 static void
  127 fdesc_insmntque_dtr(struct vnode *vp, void *arg)
  128 {
  129 
  130         vgone(vp);
  131         vput(vp);
  132 }
  133 
  134 /*
  135  * Remove an entry from the hash if it exists.
  136  */
  137 static void
  138 fdesc_remove_entry(struct fdescnode *fd)
  139 {
  140         struct fdhashhead *fc;
  141         struct fdescnode *fd2;
  142 
  143         fc = FD_NHASH(fd->fd_ix);
  144         mtx_lock(&fdesc_hashmtx);
  145         LIST_FOREACH(fd2, fc, fd_hash) {
  146                 if (fd == fd2) {
  147                         LIST_REMOVE(fd, fd_hash);
  148                         break;
  149                 }
  150         }
  151         mtx_unlock(&fdesc_hashmtx);
  152 }
  153 
  154 int
  155 fdesc_allocvp(fdntype ftype, unsigned fd_fd, int ix, struct mount *mp,
  156     struct vnode **vpp)
  157 {
  158         struct fdescmount *fmp;
  159         struct fdhashhead *fc;
  160         struct fdescnode *fd, *fd2;
  161         struct vnode *vp, *vp2;
  162         struct thread *td;
  163         int error;
  164 
  165         td = curthread;
  166         fc = FD_NHASH(ix);
  167 loop:
  168         mtx_lock(&fdesc_hashmtx);
  169         /*
  170          * If a forced unmount is progressing, we need to drop it. The flags are
  171          * protected by the hashmtx.
  172          */
  173         fmp = mp->mnt_data;
  174         if (fmp == NULL || fmp->flags & FMNT_UNMOUNTF) {
  175                 mtx_unlock(&fdesc_hashmtx);
  176                 return (-1);
  177         }
  178 
  179         LIST_FOREACH(fd, fc, fd_hash) {
  180                 if (fd->fd_ix == ix && fd->fd_vnode->v_mount == mp) {
  181                         /* Get reference to vnode in case it's being free'd */
  182                         vp = fd->fd_vnode;
  183                         VI_LOCK(vp);
  184                         mtx_unlock(&fdesc_hashmtx);
  185                         if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK))
  186                                 goto loop;
  187                         *vpp = vp;
  188                         return (0);
  189                 }
  190         }
  191         mtx_unlock(&fdesc_hashmtx);
  192 
  193         fd = malloc(sizeof(struct fdescnode), M_TEMP, M_WAITOK);
  194 
  195         error = getnewvnode("fdescfs", mp, &fdesc_vnodeops, &vp);
  196         if (error) {
  197                 free(fd, M_TEMP);
  198                 return (error);
  199         }
  200         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
  201         vp->v_data = fd;
  202         fd->fd_vnode = vp;
  203         fd->fd_type = ftype;
  204         fd->fd_fd = fd_fd;
  205         fd->fd_ix = ix;
  206         if (ftype == Fdesc && fmp->flags & FMNT_LINRDLNKF)
  207                 vp->v_vflag |= VV_READLINK;
  208         error = insmntque1(vp, mp, fdesc_insmntque_dtr, NULL);
  209         if (error != 0) {
  210                 *vpp = NULLVP;
  211                 return (error);
  212         }
  213 
  214         /* Make sure that someone didn't beat us when inserting the vnode. */
  215         mtx_lock(&fdesc_hashmtx);
  216         /*
  217          * If a forced unmount is progressing, we need to drop it. The flags are
  218          * protected by the hashmtx.
  219          */
  220         fmp = mp->mnt_data;
  221         if (fmp == NULL || fmp->flags & FMNT_UNMOUNTF) {
  222                 mtx_unlock(&fdesc_hashmtx);
  223                 vgone(vp);
  224                 vput(vp);
  225                 *vpp = NULLVP;
  226                 return (-1);
  227         }
  228 
  229         LIST_FOREACH(fd2, fc, fd_hash) {
  230                 if (fd2->fd_ix == ix && fd2->fd_vnode->v_mount == mp) {
  231                         /* Get reference to vnode in case it's being free'd */
  232                         vp2 = fd2->fd_vnode;
  233                         VI_LOCK(vp2);
  234                         mtx_unlock(&fdesc_hashmtx);
  235                         error = vget(vp2, LK_EXCLUSIVE | LK_INTERLOCK);
  236                         /* Someone beat us, dec use count and wait for reclaim */
  237                         vgone(vp);
  238                         vput(vp);
  239                         /* If we didn't get it, return no vnode. */
  240                         if (error)
  241                                 vp2 = NULLVP;
  242                         *vpp = vp2;
  243                         return (error);
  244                 }
  245         }
  246 
  247         /* If we came here, we can insert it safely. */
  248         LIST_INSERT_HEAD(fc, fd, fd_hash);
  249         mtx_unlock(&fdesc_hashmtx);
  250         *vpp = vp;
  251         return (0);
  252 }
  253 
  254 struct fdesc_get_ino_args {
  255         fdntype ftype;
  256         unsigned fd_fd;
  257         int ix;
  258         struct file *fp;
  259         struct thread *td;
  260 };
  261 
  262 static int
  263 fdesc_get_ino_alloc(struct mount *mp, void *arg, int lkflags,
  264     struct vnode **rvp)
  265 {
  266         struct fdesc_get_ino_args *a;
  267         struct fdescmount *fdm;
  268         struct vnode *vp;
  269         int error;
  270 
  271         a = arg;
  272         fdm = VFSTOFDESC(mp);
  273         if ((fdm->flags & FMNT_NODUP) != 0 && a->fp->f_type == DTYPE_VNODE) {
  274                 vp = a->fp->f_vnode;
  275                 vget(vp, lkflags | LK_RETRY);
  276                 *rvp = vp;
  277                 error = 0;
  278         } else {
  279                 error = fdesc_allocvp(a->ftype, a->fd_fd, a->ix, mp, rvp);
  280         }
  281         fdrop(a->fp, a->td);
  282         return (error);
  283 }
  284 
  285 /*
  286  * vp is the current namei directory
  287  * ndp is the name to locate in that directory...
  288  */
  289 static int
  290 fdesc_lookup(struct vop_lookup_args *ap)
  291 {
  292         struct vnode **vpp = ap->a_vpp;
  293         struct vnode *dvp = ap->a_dvp;
  294         struct componentname *cnp = ap->a_cnp;
  295         char *pname = cnp->cn_nameptr;
  296         struct thread *td = cnp->cn_thread;
  297         struct file *fp;
  298         struct fdesc_get_ino_args arg;
  299         int nlen = cnp->cn_namelen;
  300         u_int fd, fd1;
  301         int error;
  302         struct vnode *fvp;
  303 
  304         if ((cnp->cn_flags & ISLASTCN) &&
  305             (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
  306                 error = EROFS;
  307                 goto bad;
  308         }
  309 
  310         if (cnp->cn_namelen == 1 && *pname == '.') {
  311                 *vpp = dvp;
  312                 VREF(dvp);
  313                 return (0);
  314         }
  315 
  316         if (VTOFDESC(dvp)->fd_type != Froot) {
  317                 error = ENOTDIR;
  318                 goto bad;
  319         }
  320 
  321         fd = 0;
  322         /* the only time a leading 0 is acceptable is if it's "" */
  323         if (*pname == '' && nlen != 1) {
  324                 error = ENOENT;
  325                 goto bad;
  326         }
  327         while (nlen--) {
  328                 if (*pname < '' || *pname > '9') {
  329                         error = ENOENT;
  330                         goto bad;
  331                 }
  332                 fd1 = 10 * fd + *pname++ - '';
  333                 if (fd1 < fd) {
  334                         error = ENOENT;
  335                         goto bad;
  336                 }
  337                 fd = fd1;
  338         }
  339 
  340         /*
  341          * No rights to check since 'fp' isn't actually used.
  342          */
  343         if ((error = fget(td, fd, &cap_no_rights, &fp)) != 0)
  344                 goto bad;
  345 
  346         /* Check if we're looking up ourselves. */
  347         if (VTOFDESC(dvp)->fd_ix == FD_DESC + fd) {
  348                 /*
  349                  * In case we're holding the last reference to the file, the dvp
  350                  * will be re-acquired.
  351                  */
  352                 vhold(dvp);
  353                 VOP_UNLOCK(dvp);
  354                 fdrop(fp, td);
  355 
  356                 /* Re-aquire the lock afterwards. */
  357                 vn_lock(dvp, LK_RETRY | LK_EXCLUSIVE);
  358                 vdrop(dvp);
  359                 fvp = dvp;
  360                 if (VN_IS_DOOMED(dvp))
  361                         error = ENOENT;
  362         } else {
  363                 /*
  364                  * Unlock our root node (dvp) when doing this, since we might
  365                  * deadlock since the vnode might be locked by another thread
  366                  * and the root vnode lock will be obtained afterwards (in case
  367                  * we're looking up the fd of the root vnode), which will be the
  368                  * opposite lock order. Vhold the root vnode first so we don't
  369                  * lose it.
  370                  */
  371                 arg.ftype = Fdesc;
  372                 arg.fd_fd = fd;
  373                 arg.ix = FD_DESC + fd;
  374                 arg.fp = fp;
  375                 arg.td = td;
  376                 error = vn_vget_ino_gen(dvp, fdesc_get_ino_alloc, &arg,
  377                     LK_EXCLUSIVE, &fvp);
  378         }
  379 
  380         if (error)
  381                 goto bad;
  382         *vpp = fvp;
  383         return (0);
  384 
  385 bad:
  386         *vpp = NULL;
  387         return (error);
  388 }
  389 
  390 static int
  391 fdesc_open(struct vop_open_args *ap)
  392 {
  393         struct vnode *vp = ap->a_vp;
  394 
  395         if (VTOFDESC(vp)->fd_type == Froot)
  396                 return (0);
  397 
  398         /*
  399          * XXX Kludge: set td->td_proc->p_dupfd to contain the value of the file
  400          * descriptor being sought for duplication. The error return ensures
  401          * that the vnode for this device will be released by vn_open. Open
  402          * will detect this special error and take the actions in dupfdopen.
  403          * Other callers of vn_open or VOP_OPEN will simply report the
  404          * error.
  405          */
  406         ap->a_td->td_dupfd = VTOFDESC(vp)->fd_fd;       /* XXX */
  407         return (ENODEV);
  408 }
  409 
  410 static int
  411 fdesc_pathconf(struct vop_pathconf_args *ap)
  412 {
  413         struct vnode *vp = ap->a_vp;
  414         int error;
  415 
  416         switch (ap->a_name) {
  417         case _PC_NAME_MAX:
  418                 *ap->a_retval = NAME_MAX;
  419                 return (0);
  420         case _PC_LINK_MAX:
  421                 if (VTOFDESC(vp)->fd_type == Froot)
  422                         *ap->a_retval = 2;
  423                 else
  424                         *ap->a_retval = 1;
  425                 return (0);
  426         default:
  427                 if (VTOFDESC(vp)->fd_type == Froot)
  428                         return (vop_stdpathconf(ap));
  429                 vref(vp);
  430                 VOP_UNLOCK(vp);
  431                 error = kern_fpathconf(curthread, VTOFDESC(vp)->fd_fd,
  432                     ap->a_name, ap->a_retval);
  433                 vn_lock(vp, LK_SHARED | LK_RETRY);
  434                 vunref(vp);
  435                 return (error);
  436         }
  437 }
  438 
  439 static int
  440 fdesc_getattr(struct vop_getattr_args *ap)
  441 {
  442         struct vnode *vp = ap->a_vp;
  443         struct vattr *vap = ap->a_vap;
  444         struct timeval boottime;
  445 
  446         getboottime(&boottime);
  447         vap->va_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
  448         vap->va_fileid = VTOFDESC(vp)->fd_ix;
  449         vap->va_uid = 0;
  450         vap->va_gid = 0;
  451         vap->va_blocksize = DEV_BSIZE;
  452         vap->va_atime.tv_sec = boottime.tv_sec;
  453         vap->va_atime.tv_nsec = 0;
  454         vap->va_mtime = vap->va_atime;
  455         vap->va_ctime = vap->va_mtime;
  456         vap->va_gen = 0;
  457         vap->va_flags = 0;
  458         vap->va_bytes = 0;
  459         vap->va_filerev = 0;
  460 
  461         switch (VTOFDESC(vp)->fd_type) {
  462         case Froot:
  463                 vap->va_type = VDIR;
  464                 vap->va_nlink = 2;
  465                 vap->va_size = DEV_BSIZE;
  466                 vap->va_rdev = NODEV;
  467                 break;
  468 
  469         case Fdesc:
  470                 vap->va_type = (vp->v_vflag & VV_READLINK) == 0 ? VCHR : VLNK;
  471                 vap->va_nlink = 1;
  472                 vap->va_size = 0;
  473                 vap->va_rdev = makedev(0, vap->va_fileid);
  474                 break;
  475 
  476         default:
  477                 panic("fdesc_getattr");
  478                 break;
  479         }
  480 
  481         vp->v_type = vap->va_type;
  482         return (0);
  483 }
  484 
  485 static int
  486 fdesc_setattr(struct vop_setattr_args *ap)
  487 {
  488         struct vattr *vap = ap->a_vap;
  489         struct vnode *vp;
  490         struct mount *mp;
  491         struct file *fp;
  492         struct thread *td = curthread;
  493         cap_rights_t rights;
  494         unsigned fd;
  495         int error;
  496 
  497         /*
  498          * Can't mess with the root vnode
  499          */
  500         if (VTOFDESC(ap->a_vp)->fd_type == Froot)
  501                 return (EACCES);
  502 
  503         fd = VTOFDESC(ap->a_vp)->fd_fd;
  504 
  505         /*
  506          * Allow setattr where there is an underlying vnode.
  507          * For O_PATH descriptors, disallow truncate.
  508          */
  509         if (vap->va_size != VNOVAL) {
  510                 error = getvnode(td, fd,
  511                     cap_rights_init_one(&rights, CAP_EXTATTR_SET), &fp);
  512         } else {
  513                 error = getvnode_path(td, fd,
  514                     cap_rights_init_one(&rights, CAP_EXTATTR_SET), &fp);
  515         }
  516         if (error) {
  517                 /*
  518                  * getvnode() returns EINVAL if the file descriptor is not
  519                  * backed by a vnode.  Silently drop all changes except
  520                  * chflags(2) in this case.
  521                  */
  522                 if (error == EINVAL) {
  523                         if (vap->va_flags != VNOVAL)
  524                                 error = EOPNOTSUPP;
  525                         else
  526                                 error = 0;
  527                 }
  528                 return (error);
  529         }
  530         vp = fp->f_vnode;
  531         if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) == 0) {
  532                 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
  533                 error = VOP_SETATTR(vp, ap->a_vap, ap->a_cred);
  534                 VOP_UNLOCK(vp);
  535                 vn_finished_write(mp);
  536         }
  537         fdrop(fp, td);
  538         return (error);
  539 }
  540 
  541 #define UIO_MX _GENERIC_DIRLEN(10) /* number of symbols in INT_MAX printout */
  542 
  543 static int
  544 fdesc_readdir(struct vop_readdir_args *ap)
  545 {
  546         struct fdescmount *fmp;
  547         struct uio *uio = ap->a_uio;
  548         struct filedesc *fdp;
  549         struct dirent d;
  550         struct dirent *dp = &d;
  551         int error, i, off, fcnt;
  552 
  553         if (VTOFDESC(ap->a_vp)->fd_type != Froot)
  554                 panic("fdesc_readdir: not dir");
  555 
  556         fmp = VFSTOFDESC(ap->a_vp->v_mount);
  557         if (ap->a_ncookies != NULL)
  558                 *ap->a_ncookies = 0;
  559 
  560         off = (int)uio->uio_offset;
  561         if (off != uio->uio_offset || off < 0 || (u_int)off % UIO_MX != 0 ||
  562             uio->uio_resid < UIO_MX)
  563                 return (EINVAL);
  564         i = (u_int)off / UIO_MX;
  565         fdp = uio->uio_td->td_proc->p_fd;
  566         error = 0;
  567 
  568         fcnt = i - 2;           /* The first two nodes are `.' and `..' */
  569 
  570         FILEDESC_SLOCK(fdp);
  571         while (i < fdp->fd_nfiles + 2 && uio->uio_resid >= UIO_MX) {
  572                 bzero((caddr_t)dp, UIO_MX);
  573                 switch (i) {
  574                 case 0: /* `.' */
  575                 case 1: /* `..' */
  576                         dp->d_fileno = i + FD_ROOT;
  577                         dp->d_namlen = i + 1;
  578                         dp->d_reclen = UIO_MX;
  579                         bcopy("..", dp->d_name, dp->d_namlen);
  580                         dp->d_type = DT_DIR;
  581                         dirent_terminate(dp);
  582                         break;
  583                 default:
  584                         if (fdp->fd_ofiles[fcnt].fde_file == NULL)
  585                                 break;
  586                         dp->d_namlen = sprintf(dp->d_name, "%d", fcnt);
  587                         dp->d_reclen = UIO_MX;
  588                         dp->d_type = (fmp->flags & FMNT_LINRDLNKF) == 0 ?
  589                             DT_CHR : DT_LNK;
  590                         dp->d_fileno = i + FD_DESC;
  591                         dirent_terminate(dp);
  592                         break;
  593                 }
  594                 /* NOTE: d_off is the offset of the *next* entry. */
  595                 dp->d_off = UIO_MX * (i + 1);
  596                 if (dp->d_namlen != 0) {
  597                         /*
  598                          * And ship to userland
  599                          */
  600                         FILEDESC_SUNLOCK(fdp);
  601                         error = uiomove(dp, UIO_MX, uio);
  602                         if (error)
  603                                 goto done;
  604                         FILEDESC_SLOCK(fdp);
  605                 }
  606                 i++;
  607                 fcnt++;
  608         }
  609         FILEDESC_SUNLOCK(fdp);
  610 
  611 done:
  612         uio->uio_offset = i * UIO_MX;
  613         return (error);
  614 }
  615 
  616 static int
  617 fdesc_reclaim(struct vop_reclaim_args *ap)
  618 {
  619         struct vnode *vp;
  620         struct fdescnode *fd;
  621 
  622         vp = ap->a_vp;
  623         fd = VTOFDESC(vp);
  624         fdesc_remove_entry(fd);
  625         free(vp->v_data, M_TEMP);
  626         vp->v_data = NULL;
  627         return (0);
  628 }
  629 
  630 static int
  631 fdesc_readlink(struct vop_readlink_args *va)
  632 {
  633         struct vnode *vp, *vn;
  634         struct thread *td;
  635         struct uio *uio;
  636         struct file *fp;
  637         char *freepath, *fullpath;
  638         size_t pathlen;
  639         int lockflags, fd_fd;
  640         int error;
  641 
  642         freepath = NULL;
  643         vn = va->a_vp;
  644         if (VTOFDESC(vn)->fd_type != Fdesc)
  645                 panic("fdesc_readlink: not fdescfs link");
  646         fd_fd = ((struct fdescnode *)vn->v_data)->fd_fd;
  647         lockflags = VOP_ISLOCKED(vn);
  648         VOP_UNLOCK(vn);
  649 
  650         td = curthread;
  651         error = fget_cap(td, fd_fd, &cap_no_rights, &fp, NULL);
  652         if (error != 0)
  653                 goto out;
  654 
  655         switch (fp->f_type) {
  656         case DTYPE_VNODE:
  657                 vp = fp->f_vnode;
  658                 error = vn_fullpath(vp, &fullpath, &freepath);
  659                 break;
  660         default:
  661                 fullpath = "anon_inode:[unknown]";
  662                 break;
  663         }
  664         if (error == 0) {
  665                 uio = va->a_uio;
  666                 pathlen = strlen(fullpath);
  667                 error = uiomove(fullpath, pathlen, uio);
  668         }
  669         if (freepath != NULL)
  670                 free(freepath, M_TEMP);
  671         fdrop(fp, td);
  672 
  673 out:
  674         vn_lock(vn, lockflags | LK_RETRY);
  675         return (error);
  676 }

Cache object: edb1c20d645983db6b1089e670b3c916


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