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

Cache object: 50ab54d7751a6d1e923ac408054856f1


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