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


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

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

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

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

Cache object: 8e5c01921d9d223c5f3ec6102c35a95f


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