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

Cache object: 9dc6193620aeda3dcaba538632a3e0b0


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