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

Cache object: 6e74920a661c5f3b81bd7185a068f746


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