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/kern/vfs_lookup.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) 1982, 1986, 1989, 1993
    3  *      The Regents of the University of California.  All rights reserved.
    4  * (c) UNIX System Laboratories, Inc.
    5  * All or some portions of this file are derived from material licensed
    6  * to the University of California by American Telephone and Telegraph
    7  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
    8  * the permission of UNIX System Laboratories, Inc.
    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  * 4. 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  *      @(#)vfs_lookup.c        8.4 (Berkeley) 2/16/94
   35  */
   36 
   37 #include <sys/cdefs.h>
   38 __FBSDID("$FreeBSD$");
   39 
   40 #include "opt_ktrace.h"
   41 #include "opt_mac.h"
   42 
   43 #include <sys/param.h>
   44 #include <sys/systm.h>
   45 #include <sys/kernel.h>
   46 #include <sys/lock.h>
   47 #include <sys/mac.h>
   48 #include <sys/mutex.h>
   49 #include <sys/namei.h>
   50 #include <sys/vnode.h>
   51 #include <sys/mount.h>
   52 #include <sys/filedesc.h>
   53 #include <sys/proc.h>
   54 #include <sys/syscallsubr.h>
   55 #ifdef KTRACE
   56 #include <sys/ktrace.h>
   57 #endif
   58 
   59 #include <vm/uma.h>
   60 
   61 /*
   62  * Allocation zone for namei
   63  */
   64 uma_zone_t namei_zone;
   65 
   66 static void
   67 nameiinit(void *dummy __unused)
   68 {
   69         namei_zone = uma_zcreate("NAMEI", MAXPATHLEN, NULL, NULL, NULL, NULL,
   70             UMA_ALIGN_PTR, 0);
   71 
   72 }
   73 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_SECOND, nameiinit, NULL)
   74 
   75 /*
   76  * Convert a pathname into a pointer to a locked inode.
   77  *
   78  * The FOLLOW flag is set when symbolic links are to be followed
   79  * when they occur at the end of the name translation process.
   80  * Symbolic links are always followed for all other pathname
   81  * components other than the last.
   82  *
   83  * The segflg defines whether the name is to be copied from user
   84  * space or kernel space.
   85  *
   86  * Overall outline of namei:
   87  *
   88  *      copy in name
   89  *      get starting directory
   90  *      while (!done && !error) {
   91  *              call lookup to search path.
   92  *              if symbolic link, massage name in buffer and continue
   93  *      }
   94  */
   95 int
   96 namei(ndp)
   97         register struct nameidata *ndp;
   98 {
   99         register struct filedesc *fdp;  /* pointer to file descriptor state */
  100         register char *cp;              /* pointer into pathname argument */
  101         register struct vnode *dp;      /* the directory we are searching */
  102         struct iovec aiov;              /* uio for reading symbolic links */
  103         struct uio auio;
  104         int error, linklen;
  105         struct componentname *cnp = &ndp->ni_cnd;
  106         struct thread *td = cnp->cn_thread;
  107         struct proc *p = td->td_proc;
  108 
  109         GIANT_REQUIRED;
  110 
  111         ndp->ni_cnd.cn_cred = ndp->ni_cnd.cn_thread->td_ucred;
  112         KASSERT(cnp->cn_cred && p, ("namei: bad cred/proc"));
  113         KASSERT((cnp->cn_nameiop & (~OPMASK)) == 0,
  114             ("namei: nameiop contaminated with flags"));
  115         KASSERT((cnp->cn_flags & OPMASK) == 0,
  116             ("namei: flags contaminated with nameiops"));
  117         fdp = p->p_fd;
  118 
  119         /*
  120          * Get a buffer for the name to be translated, and copy the
  121          * name into the buffer.
  122          */
  123         if ((cnp->cn_flags & HASBUF) == 0)
  124                 cnp->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK);
  125         if (ndp->ni_segflg == UIO_SYSSPACE)
  126                 error = copystr(ndp->ni_dirp, cnp->cn_pnbuf,
  127                             MAXPATHLEN, (size_t *)&ndp->ni_pathlen);
  128         else
  129                 error = copyinstr(ndp->ni_dirp, cnp->cn_pnbuf,
  130                             MAXPATHLEN, (size_t *)&ndp->ni_pathlen);
  131 
  132         /*
  133          * Don't allow empty pathnames.
  134          */
  135         if (!error && *cnp->cn_pnbuf == '\0')
  136                 error = ENOENT;
  137 
  138         if (error) {
  139                 uma_zfree(namei_zone, cnp->cn_pnbuf);
  140 #ifdef DIAGNOSTIC
  141                 cnp->cn_pnbuf = NULL;
  142                 cnp->cn_nameptr = NULL;
  143 #endif
  144                 ndp->ni_vp = NULL;
  145                 return (error);
  146         }
  147         ndp->ni_loopcnt = 0;
  148 #ifdef KTRACE
  149         if (KTRPOINT(td, KTR_NAMEI)) {
  150                 KASSERT(cnp->cn_thread == curthread,
  151                     ("namei not using curthread"));
  152                 ktrnamei(cnp->cn_pnbuf);
  153         }
  154 #endif
  155 
  156         /*
  157          * Get starting point for the translation.
  158          */
  159         FILEDESC_LOCK(fdp);
  160         ndp->ni_rootdir = fdp->fd_rdir;
  161         ndp->ni_topdir = fdp->fd_jdir;
  162 
  163         dp = fdp->fd_cdir;
  164         VREF(dp);
  165         FILEDESC_UNLOCK(fdp);
  166         for (;;) {
  167                 /*
  168                  * Check if root directory should replace current directory.
  169                  * Done at start of translation and after symbolic link.
  170                  */
  171                 cnp->cn_nameptr = cnp->cn_pnbuf;
  172                 if (*(cnp->cn_nameptr) == '/') {
  173                         vrele(dp);
  174                         while (*(cnp->cn_nameptr) == '/') {
  175                                 cnp->cn_nameptr++;
  176                                 ndp->ni_pathlen--;
  177                         }
  178                         dp = ndp->ni_rootdir;
  179                         VREF(dp);
  180                 }
  181                 ndp->ni_startdir = dp;
  182                 error = lookup(ndp);
  183                 if (error) {
  184                         uma_zfree(namei_zone, cnp->cn_pnbuf);
  185 #ifdef DIAGNOSTIC
  186                         cnp->cn_pnbuf = NULL;
  187                         cnp->cn_nameptr = NULL;
  188 #endif
  189                         return (error);
  190                 }
  191                 /*
  192                  * Check for symbolic link
  193                  */
  194                 if ((cnp->cn_flags & ISSYMLINK) == 0) {
  195                         if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0) {
  196                                 uma_zfree(namei_zone, cnp->cn_pnbuf);
  197 #ifdef DIAGNOSTIC
  198                                 cnp->cn_pnbuf = NULL;
  199                                 cnp->cn_nameptr = NULL;
  200 #endif
  201                         } else
  202                                 cnp->cn_flags |= HASBUF;
  203 
  204                         if (vn_canvmio(ndp->ni_vp) == TRUE &&
  205                                 (cnp->cn_nameiop != DELETE) &&
  206                                 ((cnp->cn_flags & (NOOBJ|LOCKLEAF)) ==
  207                                  LOCKLEAF))
  208                                 vfs_object_create(ndp->ni_vp, td,
  209                                         ndp->ni_cnd.cn_cred);
  210 
  211                         return (0);
  212                 }
  213                 if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1)
  214                         VOP_UNLOCK(ndp->ni_dvp, 0, td);
  215                 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
  216                         error = ELOOP;
  217                         break;
  218                 }
  219 #ifdef MAC
  220                 if ((cnp->cn_flags & NOMACCHECK) == 0) {
  221                         error = mac_check_vnode_readlink(td->td_ucred,
  222                             ndp->ni_vp);
  223                         if (error)
  224                                 break;
  225                 }
  226 #endif
  227                 if (ndp->ni_pathlen > 1)
  228                         cp = uma_zalloc(namei_zone, M_WAITOK);
  229                 else
  230                         cp = cnp->cn_pnbuf;
  231                 aiov.iov_base = cp;
  232                 aiov.iov_len = MAXPATHLEN;
  233                 auio.uio_iov = &aiov;
  234                 auio.uio_iovcnt = 1;
  235                 auio.uio_offset = 0;
  236                 auio.uio_rw = UIO_READ;
  237                 auio.uio_segflg = UIO_SYSSPACE;
  238                 auio.uio_td = (struct thread *)0;
  239                 auio.uio_resid = MAXPATHLEN;
  240                 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
  241                 if (error) {
  242                         if (ndp->ni_pathlen > 1)
  243                                 uma_zfree(namei_zone, cp);
  244                         break;
  245                 }
  246                 linklen = MAXPATHLEN - auio.uio_resid;
  247                 if (linklen == 0) {
  248                         if (ndp->ni_pathlen > 1)
  249                                 uma_zfree(namei_zone, cp);
  250                         error = ENOENT;
  251                         break;
  252                 }
  253                 if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
  254                         if (ndp->ni_pathlen > 1)
  255                                 uma_zfree(namei_zone, cp);
  256                         error = ENAMETOOLONG;
  257                         break;
  258                 }
  259                 if (ndp->ni_pathlen > 1) {
  260                         bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
  261                         uma_zfree(namei_zone, cnp->cn_pnbuf);
  262                         cnp->cn_pnbuf = cp;
  263                 } else
  264                         cnp->cn_pnbuf[linklen] = '\0';
  265                 ndp->ni_pathlen += linklen;
  266                 vput(ndp->ni_vp);
  267                 dp = ndp->ni_dvp;
  268         }
  269         uma_zfree(namei_zone, cnp->cn_pnbuf);
  270 #ifdef DIAGNOSTIC
  271         cnp->cn_pnbuf = NULL;
  272         cnp->cn_nameptr = NULL;
  273 #endif
  274         vrele(ndp->ni_dvp);
  275         vput(ndp->ni_vp);
  276         ndp->ni_vp = NULL;
  277         return (error);
  278 }
  279 
  280 /*
  281  * Search a pathname.
  282  * This is a very central and rather complicated routine.
  283  *
  284  * The pathname is pointed to by ni_ptr and is of length ni_pathlen.
  285  * The starting directory is taken from ni_startdir. The pathname is
  286  * descended until done, or a symbolic link is encountered. The variable
  287  * ni_more is clear if the path is completed; it is set to one if a
  288  * symbolic link needing interpretation is encountered.
  289  *
  290  * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
  291  * whether the name is to be looked up, created, renamed, or deleted.
  292  * When CREATE, RENAME, or DELETE is specified, information usable in
  293  * creating, renaming, or deleting a directory entry may be calculated.
  294  * If flag has LOCKPARENT or'ed into it, the parent directory is returned
  295  * locked. If flag has WANTPARENT or'ed into it, the parent directory is
  296  * returned unlocked. Otherwise the parent directory is not returned. If
  297  * the target of the pathname exists and LOCKLEAF is or'ed into the flag
  298  * the target is returned locked, otherwise it is returned unlocked.
  299  * When creating or renaming and LOCKPARENT is specified, the target may not
  300  * be ".".  When deleting and LOCKPARENT is specified, the target may be ".".
  301  *
  302  * Overall outline of lookup:
  303  *
  304  * dirloop:
  305  *      identify next component of name at ndp->ni_ptr
  306  *      handle degenerate case where name is null string
  307  *      if .. and crossing mount points and on mounted filesys, find parent
  308  *      call VOP_LOOKUP routine for next component name
  309  *          directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set
  310  *          component vnode returned in ni_vp (if it exists), locked.
  311  *      if result vnode is mounted on and crossing mount points,
  312  *          find mounted on vnode
  313  *      if more components of name, do next level at dirloop
  314  *      return the answer in ni_vp, locked if LOCKLEAF set
  315  *          if LOCKPARENT set, return locked parent in ni_dvp
  316  *          if WANTPARENT set, return unlocked parent in ni_dvp
  317  */
  318 int
  319 lookup(ndp)
  320         register struct nameidata *ndp;
  321 {
  322         register char *cp;              /* pointer into pathname argument */
  323         register struct vnode *dp = 0;  /* the directory we are searching */
  324         struct vnode *tdp;              /* saved dp */
  325         struct mount *mp;               /* mount table entry */
  326         int docache;                    /* == 0 do not cache last component */
  327         int wantparent;                 /* 1 => wantparent or lockparent flag */
  328         int rdonly;                     /* lookup read-only flag bit */
  329         int trailing_slash;
  330         int error = 0;
  331         int dpunlocked = 0;             /* dp has already been unlocked */
  332         struct componentname *cnp = &ndp->ni_cnd;
  333         struct thread *td = cnp->cn_thread;
  334 
  335         /*
  336          * Setup: break out flag bits into variables.
  337          */
  338         wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT);
  339         docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE;
  340         if (cnp->cn_nameiop == DELETE ||
  341             (wantparent && cnp->cn_nameiop != CREATE &&
  342              cnp->cn_nameiop != LOOKUP))
  343                 docache = 0;
  344         rdonly = cnp->cn_flags & RDONLY;
  345         ndp->ni_dvp = NULL;
  346         cnp->cn_flags &= ~ISSYMLINK;
  347         dp = ndp->ni_startdir;
  348         ndp->ni_startdir = NULLVP;
  349         vn_lock(dp, LK_EXCLUSIVE | LK_RETRY, td);
  350 
  351 dirloop:
  352         /*
  353          * Search a new directory.
  354          *
  355          * The last component of the filename is left accessible via
  356          * cnp->cn_nameptr for callers that need the name. Callers needing
  357          * the name set the SAVENAME flag. When done, they assume
  358          * responsibility for freeing the pathname buffer.
  359          */
  360         cnp->cn_consume = 0;
  361         for (cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++)
  362                 continue;
  363         cnp->cn_namelen = cp - cnp->cn_nameptr;
  364         if (cnp->cn_namelen > NAME_MAX) {
  365                 error = ENAMETOOLONG;
  366                 goto bad;
  367         }
  368 #ifdef NAMEI_DIAGNOSTIC
  369         { char c = *cp;
  370         *cp = '\0';
  371         printf("{%s}: ", cnp->cn_nameptr);
  372         *cp = c; }
  373 #endif
  374         ndp->ni_pathlen -= cnp->cn_namelen;
  375         ndp->ni_next = cp;
  376 
  377         /*
  378          * Replace multiple slashes by a single slash and trailing slashes
  379          * by a null.  This must be done before VOP_LOOKUP() because some
  380          * fs's don't know about trailing slashes.  Remember if there were
  381          * trailing slashes to handle symlinks, existing non-directories
  382          * and non-existing files that won't be directories specially later.
  383          */
  384         trailing_slash = 0;
  385         while (*cp == '/' && (cp[1] == '/' || cp[1] == '\0')) {
  386                 cp++;
  387                 ndp->ni_pathlen--;
  388                 if (*cp == '\0') {
  389                         trailing_slash = 1;
  390                         *ndp->ni_next = '\0';   /* XXX for direnter() ... */
  391                 }
  392         }
  393         ndp->ni_next = cp;
  394 
  395         cnp->cn_flags |= MAKEENTRY;
  396         if (*cp == '\0' && docache == 0)
  397                 cnp->cn_flags &= ~MAKEENTRY;
  398         if (cnp->cn_namelen == 2 &&
  399             cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.')
  400                 cnp->cn_flags |= ISDOTDOT;
  401         else
  402                 cnp->cn_flags &= ~ISDOTDOT;
  403         if (*ndp->ni_next == 0)
  404                 cnp->cn_flags |= ISLASTCN;
  405         else
  406                 cnp->cn_flags &= ~ISLASTCN;
  407 
  408 
  409         /*
  410          * Check for degenerate name (e.g. / or "")
  411          * which is a way of talking about a directory,
  412          * e.g. like "/." or ".".
  413          */
  414         if (cnp->cn_nameptr[0] == '\0') {
  415                 if (dp->v_type != VDIR) {
  416                         error = ENOTDIR;
  417                         goto bad;
  418                 }
  419                 if (cnp->cn_nameiop != LOOKUP) {
  420                         error = EISDIR;
  421                         goto bad;
  422                 }
  423                 if (wantparent) {
  424                         ndp->ni_dvp = dp;
  425                         VREF(dp);
  426                 }
  427                 ndp->ni_vp = dp;
  428                 if (!(cnp->cn_flags & (LOCKPARENT | LOCKLEAF)))
  429                         VOP_UNLOCK(dp, 0, td);
  430                 /* XXX This should probably move to the top of function. */
  431                 if (cnp->cn_flags & SAVESTART)
  432                         panic("lookup: SAVESTART");
  433                 return (0);
  434         }
  435 
  436         /*
  437          * Handle "..": four special cases.
  438          * 1. Return an error if this is the last component of
  439          *    the name and the operation is DELETE or RENAME.
  440          * 2. If at root directory (e.g. after chroot)
  441          *    or at absolute root directory
  442          *    then ignore it so can't get out.
  443          * 3. If this vnode is the root of a mounted
  444          *    filesystem, then replace it with the
  445          *    vnode which was mounted on so we take the
  446          *    .. in the other filesystem.
  447          * 4. If the vnode is the top directory of
  448          *    the jail or chroot, don't let them out.
  449          */
  450         if (cnp->cn_flags & ISDOTDOT) {
  451                 if ((cnp->cn_flags & ISLASTCN) != 0 &&
  452                     (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
  453                         error = EINVAL;
  454                         goto bad;
  455                 }
  456                 for (;;) {
  457                         if (dp == ndp->ni_rootdir || 
  458                             dp == ndp->ni_topdir || 
  459                             dp == rootvnode) {
  460                                 ndp->ni_dvp = dp;
  461                                 ndp->ni_vp = dp;
  462                                 VREF(dp);
  463                                 goto nextname;
  464                         }
  465                         if ((dp->v_vflag & VV_ROOT) == 0 ||
  466                             (cnp->cn_flags & NOCROSSMOUNT))
  467                                 break;
  468                         if (dp->v_mount == NULL) {      /* forced unmount */
  469                                 error = EBADF;
  470                                 goto bad;
  471                         }
  472                         tdp = dp;
  473                         dp = dp->v_mount->mnt_vnodecovered;
  474                         vput(tdp);
  475                         VREF(dp);
  476                         vn_lock(dp, LK_EXCLUSIVE | LK_RETRY, td);
  477                 }
  478         }
  479 
  480         /*
  481          * We now have a segment name to search for, and a directory to search.
  482          */
  483 unionlookup:
  484 #ifdef MAC
  485         if ((cnp->cn_flags & NOMACCHECK) == 0) {
  486                 error = mac_check_vnode_lookup(td->td_ucred, dp, cnp);
  487                 if (error)
  488                         goto bad;
  489         }
  490 #endif
  491         ndp->ni_dvp = dp;
  492         ndp->ni_vp = NULL;
  493         cnp->cn_flags &= ~PDIRUNLOCK;
  494         ASSERT_VOP_LOCKED(dp, "lookup");
  495         if ((error = VOP_LOOKUP(dp, &ndp->ni_vp, cnp)) != 0) {
  496                 KASSERT(ndp->ni_vp == NULL, ("leaf should be empty"));
  497 #ifdef NAMEI_DIAGNOSTIC
  498                 printf("not found\n");
  499 #endif
  500                 if ((error == ENOENT) &&
  501                     (dp->v_vflag & VV_ROOT) && (dp->v_mount != NULL) &&
  502                     (dp->v_mount->mnt_flag & MNT_UNION)) {
  503                         tdp = dp;
  504                         dp = dp->v_mount->mnt_vnodecovered;
  505                         if (cnp->cn_flags & PDIRUNLOCK)
  506                                 vrele(tdp);
  507                         else
  508                                 vput(tdp);
  509                         VREF(dp);
  510                         vn_lock(dp, LK_EXCLUSIVE | LK_RETRY, td);
  511                         goto unionlookup;
  512                 }
  513 
  514                 if (error != EJUSTRETURN)
  515                         goto bad;
  516                 /*
  517                  * If creating and at end of pathname, then can consider
  518                  * allowing file to be created.
  519                  */
  520                 if (rdonly) {
  521                         error = EROFS;
  522                         goto bad;
  523                 }
  524                 if (*cp == '\0' && trailing_slash &&
  525                      !(cnp->cn_flags & WILLBEDIR)) {
  526                         error = ENOENT;
  527                         goto bad;
  528                 }
  529                 /*
  530                  * We return with ni_vp NULL to indicate that the entry
  531                  * doesn't currently exist, leaving a pointer to the
  532                  * (possibly locked) directory inode in ndp->ni_dvp.
  533                  */
  534                 if (cnp->cn_flags & SAVESTART) {
  535                         ndp->ni_startdir = ndp->ni_dvp;
  536                         VREF(ndp->ni_startdir);
  537                 }
  538                 return (0);
  539         }
  540 #ifdef NAMEI_DIAGNOSTIC
  541         printf("found\n");
  542 #endif
  543 
  544         ASSERT_VOP_LOCKED(ndp->ni_vp, "lookup");
  545 
  546         /*
  547          * Take into account any additional components consumed by
  548          * the underlying filesystem.
  549          */
  550         if (cnp->cn_consume > 0) {
  551                 cnp->cn_nameptr += cnp->cn_consume;
  552                 ndp->ni_next += cnp->cn_consume;
  553                 ndp->ni_pathlen -= cnp->cn_consume;
  554                 cnp->cn_consume = 0;
  555         }
  556 
  557         dp = ndp->ni_vp;
  558 
  559         /*
  560          * Check to see if the vnode has been mounted on;
  561          * if so find the root of the mounted filesystem.
  562          */
  563         while (dp->v_type == VDIR && (mp = dp->v_mountedhere) &&
  564                (cnp->cn_flags & NOCROSSMOUNT) == 0) {
  565                 if (vfs_busy(mp, 0, 0, td))
  566                         continue;
  567                 VOP_UNLOCK(dp, 0, td);
  568                 error = VFS_ROOT(mp, &tdp, td);
  569                 vfs_unbusy(mp, td);
  570                 if (error) {
  571                         dpunlocked = 1;
  572                         goto bad2;
  573                 }
  574                 vrele(dp);
  575                 ndp->ni_vp = dp = tdp;
  576         }
  577 
  578         /*
  579          * Check for symbolic link
  580          */
  581         if ((dp->v_type == VLNK) &&
  582             ((cnp->cn_flags & FOLLOW) || trailing_slash ||
  583              *ndp->ni_next == '/')) {
  584                 cnp->cn_flags |= ISSYMLINK;
  585                 if (dp->v_mount == NULL) {
  586                         /* We can't know whether the directory was mounted with
  587                          * NOSYMFOLLOW, so we can't follow safely. */
  588                         error = EBADF;
  589                         goto bad2;
  590                 }
  591                 if (dp->v_mount->mnt_flag & MNT_NOSYMFOLLOW) {
  592                         error = EACCES;
  593                         goto bad2;
  594                 }
  595                 return (0);
  596         }
  597 
  598         /*
  599          * Check for bogus trailing slashes.
  600          */
  601         if (trailing_slash && dp->v_type != VDIR) {
  602                 error = ENOTDIR;
  603                 goto bad2;
  604         }
  605 
  606 nextname:
  607         /*
  608          * Not a symbolic link.  If more pathname,
  609          * continue at next component, else return.
  610          */
  611         if (*ndp->ni_next == '/') {
  612                 cnp->cn_nameptr = ndp->ni_next;
  613                 while (*cnp->cn_nameptr == '/') {
  614                         cnp->cn_nameptr++;
  615                         ndp->ni_pathlen--;
  616                 }
  617                 if (ndp->ni_dvp != ndp->ni_vp)
  618                         ASSERT_VOP_UNLOCKED(ndp->ni_dvp, "lookup");
  619                 vrele(ndp->ni_dvp);
  620                 goto dirloop;
  621         }
  622         /*
  623          * Disallow directory write attempts on read-only filesystems.
  624          */
  625         if (rdonly &&
  626             (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
  627                 error = EROFS;
  628                 goto bad2;
  629         }
  630         if (cnp->cn_flags & SAVESTART) {
  631                 ndp->ni_startdir = ndp->ni_dvp;
  632                 VREF(ndp->ni_startdir);
  633         }
  634         if (!wantparent)
  635                 vrele(ndp->ni_dvp);
  636 
  637         if ((cnp->cn_flags & LOCKLEAF) == 0)
  638                 VOP_UNLOCK(dp, 0, td);
  639         return (0);
  640 
  641 bad2:
  642         if ((cnp->cn_flags & (LOCKPARENT | PDIRUNLOCK)) == LOCKPARENT &&
  643             *ndp->ni_next == '\0')
  644                 VOP_UNLOCK(ndp->ni_dvp, 0, td);
  645         vrele(ndp->ni_dvp);
  646 bad:
  647         if (dpunlocked)
  648                 vrele(dp);
  649         else
  650                 vput(dp);
  651         ndp->ni_vp = NULL;
  652         return (error);
  653 }
  654 
  655 /*
  656  * relookup - lookup a path name component
  657  *    Used by lookup to re-aquire things.
  658  */
  659 int
  660 relookup(dvp, vpp, cnp)
  661         struct vnode *dvp, **vpp;
  662         struct componentname *cnp;
  663 {
  664         struct thread *td = cnp->cn_thread;
  665         struct vnode *dp = 0;           /* the directory we are searching */
  666         int docache;                    /* == 0 do not cache last component */
  667         int wantparent;                 /* 1 => wantparent or lockparent flag */
  668         int rdonly;                     /* lookup read-only flag bit */
  669         int error = 0;
  670 #ifdef NAMEI_DIAGNOSTIC
  671         int newhash;                    /* DEBUG: check name hash */
  672         char *cp;                       /* DEBUG: check name ptr/len */
  673 #endif
  674 
  675         /*
  676          * Setup: break out flag bits into variables.
  677          */
  678         wantparent = cnp->cn_flags & (LOCKPARENT|WANTPARENT);
  679         docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE;
  680         if (cnp->cn_nameiop == DELETE ||
  681             (wantparent && cnp->cn_nameiop != CREATE))
  682                 docache = 0;
  683         rdonly = cnp->cn_flags & RDONLY;
  684         cnp->cn_flags &= ~ISSYMLINK;
  685         dp = dvp;
  686         vn_lock(dp, LK_EXCLUSIVE | LK_RETRY, td);
  687 
  688 /* dirloop: */
  689         /*
  690          * Search a new directory.
  691          *
  692          * The last component of the filename is left accessible via
  693          * cnp->cn_nameptr for callers that need the name. Callers needing
  694          * the name set the SAVENAME flag. When done, they assume
  695          * responsibility for freeing the pathname buffer.
  696          */
  697 #ifdef NAMEI_DIAGNOSTIC
  698         if (cnp->cn_namelen != cp - cnp->cn_nameptr)
  699                 panic ("relookup: bad len");
  700         if (*cp != 0)
  701                 panic("relookup: not last component");
  702         printf("{%s}: ", cnp->cn_nameptr);
  703 #endif
  704 
  705         /*
  706          * Check for degenerate name (e.g. / or "")
  707          * which is a way of talking about a directory,
  708          * e.g. like "/." or ".".
  709          */
  710         if (cnp->cn_nameptr[0] == '\0') {
  711                 if (cnp->cn_nameiop != LOOKUP || wantparent) {
  712                         error = EISDIR;
  713                         goto bad;
  714                 }
  715                 if (dp->v_type != VDIR) {
  716                         error = ENOTDIR;
  717                         goto bad;
  718                 }
  719                 if (!(cnp->cn_flags & LOCKLEAF))
  720                         VOP_UNLOCK(dp, 0, td);
  721                 *vpp = dp;
  722                 /* XXX This should probably move to the top of function. */
  723                 if (cnp->cn_flags & SAVESTART)
  724                         panic("lookup: SAVESTART");
  725                 return (0);
  726         }
  727 
  728         if (cnp->cn_flags & ISDOTDOT)
  729                 panic ("relookup: lookup on dot-dot");
  730 
  731         /*
  732          * We now have a segment name to search for, and a directory to search.
  733          */
  734         if ((error = VOP_LOOKUP(dp, vpp, cnp)) != 0) {
  735                 KASSERT(*vpp == NULL, ("leaf should be empty"));
  736                 if (error != EJUSTRETURN)
  737                         goto bad;
  738                 /*
  739                  * If creating and at end of pathname, then can consider
  740                  * allowing file to be created.
  741                  */
  742                 if (rdonly) {
  743                         error = EROFS;
  744                         goto bad;
  745                 }
  746                 /* ASSERT(dvp == ndp->ni_startdir) */
  747                 if (cnp->cn_flags & SAVESTART)
  748                         VREF(dvp);
  749                 /*
  750                  * We return with ni_vp NULL to indicate that the entry
  751                  * doesn't currently exist, leaving a pointer to the
  752                  * (possibly locked) directory inode in ndp->ni_dvp.
  753                  */
  754                 return (0);
  755         }
  756         dp = *vpp;
  757 
  758         /*
  759          * Check for symbolic link
  760          */
  761         KASSERT(dp->v_type != VLNK || !(cnp->cn_flags & FOLLOW),
  762             ("relookup: symlink found.\n"));
  763 
  764         /*
  765          * Disallow directory write attempts on read-only filesystems.
  766          */
  767         if (rdonly &&
  768             (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
  769                 error = EROFS;
  770                 goto bad2;
  771         }
  772         /* ASSERT(dvp == ndp->ni_startdir) */
  773         if (cnp->cn_flags & SAVESTART)
  774                 VREF(dvp);
  775         
  776         if (!wantparent)
  777                 vrele(dvp);
  778 
  779         if (vn_canvmio(dp) == TRUE &&
  780                 ((cnp->cn_flags & (NOOBJ|LOCKLEAF)) == LOCKLEAF))
  781                 vfs_object_create(dp, td, cnp->cn_cred);
  782 
  783         if ((cnp->cn_flags & LOCKLEAF) == 0)
  784                 VOP_UNLOCK(dp, 0, td);
  785         return (0);
  786 
  787 bad2:
  788         if ((cnp->cn_flags & LOCKPARENT) && (cnp->cn_flags & ISLASTCN))
  789                 VOP_UNLOCK(dvp, 0, td);
  790         vrele(dvp);
  791 bad:
  792         vput(dp);
  793         *vpp = NULL;
  794         return (error);
  795 }
  796 
  797 /*
  798  * Determine if there is a suitable alternate filename under the specified
  799  * prefix for the specified path.  If the create flag is set, then the
  800  * alternate prefix will be used so long as the parent directory exists.
  801  * This is used by the various compatiblity ABIs so that Linux binaries prefer
  802  * files under /compat/linux for example.  The chosen path (whether under
  803  * the prefix or under /) is returned in a kernel malloc'd buffer pointed
  804  * to by pathbuf.  The caller is responsible for free'ing the buffer from
  805  * the M_TEMP bucket if one is returned.
  806  */
  807 int
  808 kern_alternate_path(struct thread *td, const char *prefix, char *path,
  809     enum uio_seg pathseg, char **pathbuf, int create)
  810 {
  811         struct nameidata nd, ndroot;
  812         char *ptr, *buf, *cp;
  813         size_t len, sz;
  814         int error;
  815 
  816         buf = (char *) malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
  817         *pathbuf = buf;
  818 
  819         /* Copy the prefix into the new pathname as a starting point. */
  820         len = strlcpy(buf, prefix, MAXPATHLEN);
  821         if (len >= MAXPATHLEN) {
  822                 *pathbuf = NULL;
  823                 free(buf, M_TEMP);
  824                 return (EINVAL);
  825         }
  826         sz = MAXPATHLEN - len;
  827         ptr = buf + len;
  828 
  829         /* Append the filename to the prefix. */
  830         if (pathseg == UIO_SYSSPACE)
  831                 error = copystr(path, ptr, sz, &len);
  832         else
  833                 error = copyinstr(path, ptr, sz, &len);
  834 
  835         if (error) {
  836                 *pathbuf = NULL;
  837                 free(buf, M_TEMP);
  838                 return (error);
  839         }
  840 
  841         /* Only use a prefix with absolute pathnames. */
  842         if (*ptr != '/') {
  843                 error = EINVAL;
  844                 goto keeporig;
  845         }
  846 
  847         /* XXX: VFS_LOCK_GIANT? */
  848         mtx_lock(&Giant);
  849 
  850         /*
  851          * We know that there is a / somewhere in this pathname.
  852          * Search backwards for it, to find the file's parent dir
  853          * to see if it exists in the alternate tree. If it does,
  854          * and we want to create a file (cflag is set). We don't
  855          * need to worry about the root comparison in this case.
  856          */
  857 
  858         if (create) {
  859                 for (cp = &ptr[len] - 1; *cp != '/'; cp--);
  860                 *cp = '\0';
  861 
  862                 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, buf, td);
  863                 error = namei(&nd);
  864                 *cp = '/';
  865                 if (error != 0)
  866                         goto nd_failed;
  867         } else {
  868                 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, buf, td);
  869 
  870                 error = namei(&nd);
  871                 if (error != 0)
  872                         goto nd_failed;
  873 
  874                 /*
  875                  * We now compare the vnode of the prefix to the one
  876                  * vnode asked. If they resolve to be the same, then we
  877                  * ignore the match so that the real root gets used.
  878                  * This avoids the problem of traversing "../.." to find the
  879                  * root directory and never finding it, because "/" resolves
  880                  * to the emulation root directory. This is expensive :-(
  881                  */
  882                 NDINIT(&ndroot, LOOKUP, FOLLOW, UIO_SYSSPACE, prefix, td);
  883 
  884                 /* We shouldn't ever get an error from this namei(). */
  885                 error = namei(&ndroot);
  886                 if (error == 0) {
  887                         if (nd.ni_vp == ndroot.ni_vp)
  888                                 error = ENOENT;
  889 
  890                         NDFREE(&ndroot, NDF_ONLY_PNBUF);
  891                         vrele(ndroot.ni_vp);
  892                 }
  893         }
  894 
  895         NDFREE(&nd, NDF_ONLY_PNBUF);
  896         vrele(nd.ni_vp);
  897 
  898 nd_failed:
  899         /* XXX: VFS_UNLOCK_GIANT? */
  900         mtx_unlock(&Giant);
  901 
  902 keeporig:
  903         /* If there was an error, use the original path name. */
  904         if (error)
  905                 bcopy(ptr, buf, len);
  906         return (error);
  907 }

Cache object: 44af4e6f3186b5a792846ba840f67113


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