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

Cache object: a8f7aaf7df4c8eaae881dbd102eaef5a


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