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

Cache object: 06025d54177b3c124289a014a83df4be


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