The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/kern/vfs_lookup.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: 67d500585f73807385ee60475fc275cb


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