Home | History | Annotate | Line # | Download | only in kern
vfs_lookup.c revision 1.73
      1 /*	$NetBSD: vfs_lookup.c,v 1.73 2006/12/09 16:11:51 chs Exp $	*/
      2 
      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.10 (Berkeley) 5/27/95
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: vfs_lookup.c,v 1.73 2006/12/09 16:11:51 chs Exp $");
     41 
     42 #include "opt_ktrace.h"
     43 #include "opt_systrace.h"
     44 #include "opt_magiclinks.h"
     45 
     46 #include <sys/param.h>
     47 #include <sys/systm.h>
     48 #include <sys/kernel.h>
     49 #include <sys/syslimits.h>
     50 #include <sys/time.h>
     51 #include <sys/namei.h>
     52 #include <sys/vnode.h>
     53 #include <sys/mount.h>
     54 #include <sys/errno.h>
     55 #include <sys/filedesc.h>
     56 #include <sys/hash.h>
     57 #include <sys/malloc.h>
     58 #include <sys/proc.h>
     59 #include <sys/syslog.h>
     60 #include <sys/kauth.h>
     61 
     62 #ifdef KTRACE
     63 #include <sys/ktrace.h>
     64 #endif
     65 #ifdef SYSTRACE
     66 #include <sys/systrace.h>
     67 #endif
     68 
     69 #ifndef MAGICLINKS
     70 #define MAGICLINKS 0
     71 #endif
     72 
     73 int vfs_magiclinks = MAGICLINKS;
     74 
     75 struct pool pnbuf_pool;		/* pathname buffer pool */
     76 struct pool_cache pnbuf_cache;	/* pathname buffer cache */
     77 
     78 /*
     79  * Substitute replacement text for 'magic' strings in symlinks.
     80  * Returns 0 if successful, and returns non-zero if an error
     81  * occurs.  (Currently, the only possible error is running out
     82  * of temporary pathname space.)
     83  *
     84  * Looks for "@<string>" and "@<string>/", where <string> is a
     85  * recognized 'magic' string.  Replaces the "@<string>" with the
     86  * appropriate replacement text.  (Note that in some cases the
     87  * replacement text may have zero length.)
     88  *
     89  * This would have been table driven, but the variance in
     90  * replacement strings (and replacement string lengths) made
     91  * that impractical.
     92  */
     93 #define	VNL(x)							\
     94 	(sizeof(x) - 1)
     95 
     96 #define	VO	'{'
     97 #define	VC	'}'
     98 
     99 #define	MATCH(str)						\
    100 	((termchar == '/' && i + VNL(str) == *len) ||		\
    101 	 (i + VNL(str) < *len &&				\
    102 	  cp[i + VNL(str)] == termchar)) &&			\
    103 	!strncmp((str), &cp[i], VNL(str))
    104 
    105 #define	SUBSTITUTE(m, s, sl)					\
    106 	if ((newlen + (sl)) > MAXPATHLEN)			\
    107 		return (1);					\
    108 	i += VNL(m);						\
    109 	if (termchar != '/')					\
    110 		i++;						\
    111 	memcpy(&tmp[newlen], (s), (sl));			\
    112 	newlen += (sl);						\
    113 	change = 1;						\
    114 	termchar = '/';
    115 
    116 static int
    117 symlink_magic(struct proc *p, char *cp, int *len)
    118 {
    119 	char *tmp;
    120 	int change, i, newlen;
    121 	int termchar = '/';
    122 
    123 	tmp = PNBUF_GET();
    124 	for (change = i = newlen = 0; i < *len; ) {
    125 		if (cp[i] != '@') {
    126 			tmp[newlen++] = cp[i++];
    127 			continue;
    128 		}
    129 
    130 		i++;
    131 
    132 		/* Check for @{var} syntax. */
    133 		if (cp[i] == VO) {
    134 			termchar = VC;
    135 			i++;
    136 		}
    137 
    138 		/*
    139 		 * The following checks should be ordered according
    140 		 * to frequency of use.
    141 		 */
    142 		if (MATCH("machine_arch")) {
    143 			SUBSTITUTE("machine_arch", MACHINE_ARCH,
    144 			    sizeof(MACHINE_ARCH) - 1);
    145 		} else if (MATCH("machine")) {
    146 			SUBSTITUTE("machine", MACHINE,
    147 			    sizeof(MACHINE) - 1);
    148 		} else if (MATCH("hostname")) {
    149 			SUBSTITUTE("hostname", hostname,
    150 			    hostnamelen);
    151 		} else if (MATCH("osrelease")) {
    152 			SUBSTITUTE("osrelease", osrelease,
    153 			    strlen(osrelease));
    154 		} else if (MATCH("emul")) {
    155 			SUBSTITUTE("emul", p->p_emul->e_name,
    156 			    strlen(p->p_emul->e_name));
    157 		} else if (MATCH("kernel_ident")) {
    158 			SUBSTITUTE("kernel_ident", kernel_ident,
    159 			    strlen(kernel_ident));
    160 		} else if (MATCH("domainname")) {
    161 			SUBSTITUTE("domainname", domainname,
    162 			    domainnamelen);
    163 		} else if (MATCH("ostype")) {
    164 			SUBSTITUTE("ostype", ostype,
    165 			    strlen(ostype));
    166 		} else if (MATCH("uid")) {
    167 			char uidtmp[11]; /* XXX elad */
    168 
    169 			(void)snprintf(uidtmp, sizeof(uidtmp), "%u",
    170 			    kauth_cred_geteuid(kauth_cred_get()));
    171 			SUBSTITUTE("uid", uidtmp, strlen(uidtmp));
    172 		} else {
    173 			tmp[newlen++] = '@';
    174 			if (termchar == VC)
    175 				tmp[newlen++] = VO;
    176 		}
    177 	}
    178 
    179 	if (change) {
    180 		memcpy(cp, tmp, newlen);
    181 		*len = newlen;
    182 	}
    183 	PNBUF_PUT(tmp);
    184 
    185 	return (0);
    186 }
    187 
    188 #undef VNL
    189 #undef VO
    190 #undef VC
    191 #undef MATCH
    192 #undef SUBSTITUTE
    193 
    194 /*
    195  * Convert a pathname into a pointer to a locked vnode.
    196  *
    197  * The FOLLOW flag is set when symbolic links are to be followed
    198  * when they occur at the end of the name translation process.
    199  * Symbolic links are always followed for all other pathname
    200  * components other than the last.
    201  *
    202  * The segflg defines whether the name is to be copied from user
    203  * space or kernel space.
    204  *
    205  * Overall outline of namei:
    206  *
    207  *	copy in name
    208  *	get starting directory
    209  *	while (!done && !error) {
    210  *		call lookup to search path.
    211  *		if symbolic link, massage name in buffer and continue
    212  *	}
    213  */
    214 int
    215 namei(struct nameidata *ndp)
    216 {
    217 	struct cwdinfo *cwdi;		/* pointer to cwd state */
    218 	char *cp;			/* pointer into pathname argument */
    219 	struct vnode *dp;		/* the directory we are searching */
    220 	struct iovec aiov;		/* uio for reading symbolic links */
    221 	struct uio auio;
    222 	int error, linklen;
    223 	struct componentname *cnp = &ndp->ni_cnd;
    224 
    225 #ifdef DIAGNOSTIC
    226 	if (!cnp->cn_cred || !cnp->cn_lwp)
    227 		panic("namei: bad cred/proc");
    228 	if (cnp->cn_nameiop & (~OPMASK))
    229 		panic("namei: nameiop contaminated with flags");
    230 	if (cnp->cn_flags & OPMASK)
    231 		panic("namei: flags contaminated with nameiops");
    232 #endif
    233 	cwdi = cnp->cn_lwp->l_proc->p_cwdi;
    234 
    235 	/*
    236 	 * Get a buffer for the name to be translated, and copy the
    237 	 * name into the buffer.
    238 	 */
    239 	if ((cnp->cn_flags & HASBUF) == 0)
    240 		cnp->cn_pnbuf = PNBUF_GET();
    241 	if (ndp->ni_segflg == UIO_SYSSPACE)
    242 		error = copystr(ndp->ni_dirp, cnp->cn_pnbuf,
    243 			    MAXPATHLEN, &ndp->ni_pathlen);
    244 	else
    245 		error = copyinstr(ndp->ni_dirp, cnp->cn_pnbuf,
    246 			    MAXPATHLEN, &ndp->ni_pathlen);
    247 
    248 	/*
    249 	 * POSIX.1 requirement: "" is not a valid file name.
    250 	 */
    251 	if (!error && ndp->ni_pathlen == 1)
    252 		error = ENOENT;
    253 
    254 	if (error) {
    255 		PNBUF_PUT(cnp->cn_pnbuf);
    256 		ndp->ni_vp = NULL;
    257 		return (error);
    258 	}
    259 	ndp->ni_loopcnt = 0;
    260 
    261 #ifdef KTRACE
    262 	if (KTRPOINT(cnp->cn_lwp->l_proc, KTR_NAMEI))
    263 		ktrnamei(cnp->cn_lwp, cnp->cn_pnbuf);
    264 #endif
    265 #ifdef SYSTRACE
    266 	if (ISSET(cnp->cn_lwp->l_proc->p_flag, P_SYSTRACE))
    267 		systrace_namei(ndp);
    268 #endif
    269 
    270 	/*
    271 	 * Get starting point for the translation.
    272 	 */
    273 	if ((ndp->ni_rootdir = cwdi->cwdi_rdir) == NULL)
    274 		ndp->ni_rootdir = rootvnode;
    275 	/*
    276 	 * Check if starting from root directory or current directory.
    277 	 */
    278 	if (cnp->cn_pnbuf[0] == '/') {
    279 		dp = ndp->ni_rootdir;
    280 		VREF(dp);
    281 	} else {
    282 		dp = cwdi->cwdi_cdir;
    283 		VREF(dp);
    284 	}
    285 	vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
    286 	for (;;) {
    287 		if (!dp->v_mount) {
    288 			/* Give up if the directory is no longer mounted */
    289 			vput(dp);
    290 			PNBUF_PUT(cnp->cn_pnbuf);
    291 			return (ENOENT);
    292 		}
    293 		cnp->cn_nameptr = cnp->cn_pnbuf;
    294 		ndp->ni_startdir = dp;
    295 		error = lookup(ndp);
    296 		if (error != 0) {
    297 			if (ndp->ni_dvp) {
    298 				vput(ndp->ni_dvp);
    299 			}
    300 			PNBUF_PUT(cnp->cn_pnbuf);
    301 			return (error);
    302 		}
    303 
    304 		/*
    305 		 * Check for symbolic link
    306 		 */
    307 		if ((cnp->cn_flags & ISSYMLINK) == 0) {
    308 			if ((cnp->cn_flags & LOCKPARENT) == 0 && ndp->ni_dvp) {
    309 				if (ndp->ni_dvp == ndp->ni_vp) {
    310 					vrele(ndp->ni_dvp);
    311 				} else {
    312 					vput(ndp->ni_dvp);
    313 				}
    314 			}
    315 			if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0)
    316 				PNBUF_PUT(cnp->cn_pnbuf);
    317 			else
    318 				cnp->cn_flags |= HASBUF;
    319 			return (0);
    320 		}
    321 
    322 		if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
    323 			error = ELOOP;
    324 			break;
    325 		}
    326 		if (ndp->ni_vp->v_mount->mnt_flag & MNT_SYMPERM) {
    327 			error = VOP_ACCESS(ndp->ni_vp, VEXEC, cnp->cn_cred,
    328 			    cnp->cn_lwp);
    329 			if (error != 0)
    330 				break;
    331 		}
    332 		if (ndp->ni_pathlen > 1)
    333 			cp = PNBUF_GET();
    334 		else
    335 			cp = cnp->cn_pnbuf;
    336 		aiov.iov_base = cp;
    337 		aiov.iov_len = MAXPATHLEN;
    338 		auio.uio_iov = &aiov;
    339 		auio.uio_iovcnt = 1;
    340 		auio.uio_offset = 0;
    341 		auio.uio_rw = UIO_READ;
    342 		auio.uio_resid = MAXPATHLEN;
    343 		UIO_SETUP_SYSSPACE(&auio);
    344 		error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
    345 		if (error) {
    346 badlink:
    347 			if (ndp->ni_pathlen > 1)
    348 				PNBUF_PUT(cp);
    349 			break;
    350 		}
    351 		linklen = MAXPATHLEN - auio.uio_resid;
    352 		if (linklen == 0) {
    353 			error = ENOENT;
    354 			goto badlink;
    355 		}
    356 
    357 		/*
    358 		 * Do symlink substitution, if appropriate, and
    359 		 * check length for potential overflow.
    360 		 */
    361 		if ((vfs_magiclinks &&
    362 		     symlink_magic(cnp->cn_lwp->l_proc, cp, &linklen)) ||
    363 		    (linklen + ndp->ni_pathlen >= MAXPATHLEN)) {
    364 			error = ENAMETOOLONG;
    365 			goto badlink;
    366 		}
    367 		if (ndp->ni_pathlen > 1) {
    368 			memcpy(cp + linklen, ndp->ni_next, ndp->ni_pathlen);
    369 			PNBUF_PUT(cnp->cn_pnbuf);
    370 			cnp->cn_pnbuf = cp;
    371 		} else
    372 			cnp->cn_pnbuf[linklen] = '\0';
    373 		ndp->ni_pathlen += linklen;
    374 		vput(ndp->ni_vp);
    375 		dp = ndp->ni_dvp;
    376 
    377 		/*
    378 		 * Check if root directory should replace current directory.
    379 		 */
    380 		if (cnp->cn_pnbuf[0] == '/') {
    381 			vput(dp);
    382 			dp = ndp->ni_rootdir;
    383 			VREF(dp);
    384 			vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
    385 		}
    386 	}
    387 	KASSERT(ndp->ni_dvp != ndp->ni_vp);
    388 	vput(ndp->ni_dvp);
    389 	vput(ndp->ni_vp);
    390 	ndp->ni_vp = NULL;
    391 	PNBUF_PUT(cnp->cn_pnbuf);
    392 	return (error);
    393 }
    394 
    395 /*
    396  * Determine the namei hash (for cn_hash) for name.
    397  * If *ep != NULL, hash from name to ep-1.
    398  * If *ep == NULL, hash from name until the first NUL or '/', and
    399  * return the location of this termination character in *ep.
    400  *
    401  * This function returns an equivalent hash to the MI hash32_strn().
    402  * The latter isn't used because in the *ep == NULL case, determining
    403  * the length of the string to the first NUL or `/' and then calling
    404  * hash32_strn() involves unnecessary double-handling of the data.
    405  */
    406 uint32_t
    407 namei_hash(const char *name, const char **ep)
    408 {
    409 	uint32_t	hash;
    410 
    411 	hash = HASH32_STR_INIT;
    412 	if (*ep != NULL) {
    413 		for (; name < *ep; name++)
    414 			hash = hash * 33 + *(const uint8_t *)name;
    415 	} else {
    416 		for (; *name != '\0' && *name != '/'; name++)
    417 			hash = hash * 33 + *(const uint8_t *)name;
    418 		*ep = name;
    419 	}
    420 	return (hash + (hash >> 5));
    421 }
    422 
    423 /*
    424  * Search a pathname.
    425  * This is a very central and rather complicated routine.
    426  *
    427  * The pathname is pointed to by ni_ptr and is of length ni_pathlen.
    428  * The starting directory is taken from ni_startdir. The pathname is
    429  * descended until done, or a symbolic link is encountered. The variable
    430  * ni_more is clear if the path is completed; it is set to one if a
    431  * symbolic link needing interpretation is encountered.
    432  *
    433  * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
    434  * whether the name is to be looked up, created, renamed, or deleted.
    435  * When CREATE, RENAME, or DELETE is specified, information usable in
    436  * creating, renaming, or deleting a directory entry may be calculated.
    437  * If flag has LOCKPARENT or'ed into it, the parent directory is returned
    438  * locked. If flag has WANTPARENT or'ed into it, the parent directory is
    439  * returned unlocked. Otherwise the parent directory is not returned. If
    440  * the target of the pathname exists and LOCKLEAF is or'ed into the flag
    441  * the target is returned locked, otherwise it is returned unlocked.
    442  * When creating or renaming and LOCKPARENT is specified, the target may not
    443  * be ".".  When deleting and LOCKPARENT is specified, the target may be ".".
    444  *
    445  * Overall outline of lookup:
    446  *
    447  * dirloop:
    448  *	identify next component of name at ndp->ni_ptr
    449  *	handle degenerate case where name is null string
    450  *	if .. and crossing mount points and on mounted filesys, find parent
    451  *	call VOP_LOOKUP routine for next component name
    452  *	    directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set
    453  *	    component vnode returned in ni_vp (if it exists), locked.
    454  *	if result vnode is mounted on and crossing mount points,
    455  *	    find mounted on vnode
    456  *	if more components of name, do next level at dirloop
    457  *	return the answer in ni_vp, locked if LOCKLEAF set
    458  *	    if LOCKPARENT set, return locked parent in ni_dvp
    459  *	    if WANTPARENT set, return unlocked parent in ni_dvp
    460  */
    461 int
    462 lookup(struct nameidata *ndp)
    463 {
    464 	const char *cp;			/* pointer into pathname argument */
    465 	struct vnode *dp = 0;		/* the directory we are searching */
    466 	struct vnode *tdp;		/* saved dp */
    467 	struct mount *mp;		/* mount table entry */
    468 	int docache;			/* == 0 do not cache last component */
    469 	int rdonly;			/* lookup read-only flag bit */
    470 	int error = 0;
    471 	int slashes;
    472 	struct componentname *cnp = &ndp->ni_cnd;
    473 	struct lwp *l = cnp->cn_lwp;
    474 
    475 	/*
    476 	 * Setup: break out flag bits into variables.
    477 	 */
    478 	docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE;
    479 	if (cnp->cn_nameiop != CREATE)
    480 		docache = 0;
    481 	rdonly = cnp->cn_flags & RDONLY;
    482 	ndp->ni_dvp = NULL;
    483 	cnp->cn_flags &= ~ISSYMLINK;
    484 	dp = ndp->ni_startdir;
    485 	ndp->ni_startdir = NULLVP;
    486 
    487 	/*
    488 	 * If we have a leading string of slashes, remove them, and just make
    489 	 * sure the current node is a directory.
    490 	 */
    491 	cp = cnp->cn_nameptr;
    492 	if (*cp == '/') {
    493 		do {
    494 			cp++;
    495 		} while (*cp == '/');
    496 		ndp->ni_pathlen -= cp - cnp->cn_nameptr;
    497 		cnp->cn_nameptr = cp;
    498 
    499 		if (dp->v_type != VDIR) {
    500 			error = ENOTDIR;
    501 			goto bad;
    502 		}
    503 
    504 		/*
    505 		 * If we've exhausted the path name, then just return the
    506 		 * current node.  If the caller requested the parent node (i.e.
    507 		 * it's a CREATE, DELETE, or RENAME), and we don't have one
    508 		 * (because this is the root directory), then we must fail.
    509 		 */
    510 		if (cnp->cn_nameptr[0] == '\0') {
    511 			if (ndp->ni_dvp == NULL && cnp->cn_nameiop != LOOKUP) {
    512 				switch (cnp->cn_nameiop) {
    513 				case CREATE:
    514 					error = EEXIST;
    515 					break;
    516 				case DELETE:
    517 				case RENAME:
    518 					error = EBUSY;
    519 					break;
    520 				default:
    521 					KASSERT(0);
    522 				}
    523 				goto bad;
    524 			}
    525 			ndp->ni_vp = dp;
    526 			cnp->cn_flags |= ISLASTCN;
    527 			goto terminal;
    528 		}
    529 	}
    530 
    531 dirloop:
    532 	/*
    533 	 * Search a new directory.
    534 	 *
    535 	 * The cn_hash value is for use by vfs_cache.
    536 	 * The last component of the filename is left accessible via
    537 	 * cnp->cn_nameptr for callers that need the name. Callers needing
    538 	 * the name set the SAVENAME flag. When done, they assume
    539 	 * responsibility for freeing the pathname buffer.
    540 	 *
    541 	 * At this point, our only vnode state is that "dp" is held and locked.
    542 	 */
    543 	cnp->cn_consume = 0;
    544 	cp = NULL;
    545 	cnp->cn_hash = namei_hash(cnp->cn_nameptr, &cp);
    546 	cnp->cn_namelen = cp - cnp->cn_nameptr;
    547 	if (cnp->cn_namelen > NAME_MAX) {
    548 		error = ENAMETOOLONG;
    549 		goto bad;
    550 	}
    551 #ifdef NAMEI_DIAGNOSTIC
    552 	{ char c = *cp;
    553 	*(char *)cp = '\0';
    554 	printf("{%s}: ", cnp->cn_nameptr);
    555 	*(char *)cp = c; }
    556 #endif /* NAMEI_DIAGNOSTIC */
    557 	ndp->ni_pathlen -= cnp->cn_namelen;
    558 	ndp->ni_next = cp;
    559 	/*
    560 	 * If this component is followed by a slash, then move the pointer to
    561 	 * the next component forward, and remember that this component must be
    562 	 * a directory.
    563 	 */
    564 	if (*cp == '/') {
    565 		do {
    566 			cp++;
    567 		} while (*cp == '/');
    568 		slashes = cp - ndp->ni_next;
    569 		ndp->ni_pathlen -= slashes;
    570 		ndp->ni_next = cp;
    571 		cnp->cn_flags |= REQUIREDIR;
    572 	} else {
    573 		slashes = 0;
    574 		cnp->cn_flags &= ~REQUIREDIR;
    575 	}
    576 	/*
    577 	 * We do special processing on the last component, whether or not it's
    578 	 * a directory.  Cache all intervening lookups, but not the final one.
    579 	 */
    580 	if (*cp == '\0') {
    581 		if (docache)
    582 			cnp->cn_flags |= MAKEENTRY;
    583 		else
    584 			cnp->cn_flags &= ~MAKEENTRY;
    585 		cnp->cn_flags |= ISLASTCN;
    586 	} else {
    587 		cnp->cn_flags |= MAKEENTRY;
    588 		cnp->cn_flags &= ~ISLASTCN;
    589 	}
    590 	if (cnp->cn_namelen == 2 &&
    591 	    cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.')
    592 		cnp->cn_flags |= ISDOTDOT;
    593 	else
    594 		cnp->cn_flags &= ~ISDOTDOT;
    595 
    596 	/*
    597 	 * Handle "..": two special cases.
    598 	 * 1. If at root directory (e.g. after chroot)
    599 	 *    or at absolute root directory
    600 	 *    then ignore it so can't get out.
    601 	 * 1a. If we have somehow gotten out of a jail, warn
    602 	 *    and also ignore it so we can't get farther out.
    603 	 * 2. If this vnode is the root of a mounted
    604 	 *    filesystem, then replace it with the
    605 	 *    vnode which was mounted on so we take the
    606 	 *    .. in the other file system.
    607 	 */
    608 	if (cnp->cn_flags & ISDOTDOT) {
    609 		struct proc *p = l->l_proc;
    610 
    611 		for (;;) {
    612 			if (dp == ndp->ni_rootdir || dp == rootvnode) {
    613 				ndp->ni_dvp = dp;
    614 				ndp->ni_vp = dp;
    615 				VREF(dp);
    616 				goto nextname;
    617 			}
    618 			if (ndp->ni_rootdir != rootvnode) {
    619 				int retval;
    620 
    621 				VOP_UNLOCK(dp, 0);
    622 				retval = vn_isunder(dp, ndp->ni_rootdir, l);
    623 				vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
    624 				if (!retval) {
    625 				    /* Oops! We got out of jail! */
    626 				    log(LOG_WARNING,
    627 					"chrooted pid %d uid %d (%s) "
    628 					"detected outside of its chroot\n",
    629 					p->p_pid, kauth_cred_geteuid(l->l_cred),
    630 					p->p_comm);
    631 				    /* Put us at the jail root. */
    632 				    vput(dp);
    633 				    dp = ndp->ni_rootdir;
    634 				    ndp->ni_dvp = dp;
    635 				    ndp->ni_vp = dp;
    636 				    VREF(dp);
    637 				    VREF(dp);
    638 				    vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
    639 				    goto nextname;
    640 				}
    641 			}
    642 			if ((dp->v_flag & VROOT) == 0 ||
    643 			    (cnp->cn_flags & NOCROSSMOUNT))
    644 				break;
    645 			tdp = dp;
    646 			dp = dp->v_mount->mnt_vnodecovered;
    647 			vput(tdp);
    648 			VREF(dp);
    649 			vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
    650 		}
    651 	}
    652 
    653 	/*
    654 	 * We now have a segment name to search for, and a directory to search.
    655 	 * Again, our only vnode state is that "dp" is held and locked.
    656 	 */
    657 unionlookup:
    658 	ndp->ni_dvp = dp;
    659 	ndp->ni_vp = NULL;
    660 	error = VOP_LOOKUP(dp, &ndp->ni_vp, cnp);
    661 	if (error != 0) {
    662 #ifdef DIAGNOSTIC
    663 		if (ndp->ni_vp != NULL)
    664 			panic("leaf `%s' should be empty", cnp->cn_nameptr);
    665 #endif /* DIAGNOSTIC */
    666 #ifdef NAMEI_DIAGNOSTIC
    667 		printf("not found\n");
    668 #endif /* NAMEI_DIAGNOSTIC */
    669 		if ((error == ENOENT) &&
    670 		    (dp->v_flag & VROOT) &&
    671 		    (dp->v_mount->mnt_flag & MNT_UNION)) {
    672 			tdp = dp;
    673 			dp = dp->v_mount->mnt_vnodecovered;
    674 			vput(tdp);
    675 			VREF(dp);
    676 			vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
    677 			goto unionlookup;
    678 		}
    679 
    680 		if (error != EJUSTRETURN)
    681 			goto bad;
    682 
    683 		/*
    684 		 * If this was not the last component, or there were trailing
    685 		 * slashes, and we are not going to create a directory,
    686 		 * then the name must exist.
    687 		 */
    688 		if ((cnp->cn_flags & (REQUIREDIR | CREATEDIR)) == REQUIREDIR) {
    689 			error = ENOENT;
    690 			goto bad;
    691 		}
    692 
    693 		/*
    694 		 * If creating and at end of pathname, then can consider
    695 		 * allowing file to be created.
    696 		 */
    697 		if (rdonly) {
    698 			error = EROFS;
    699 			goto bad;
    700 		}
    701 
    702 		/*
    703 		 * We return with ni_vp NULL to indicate that the entry
    704 		 * doesn't currently exist, leaving a pointer to the
    705 		 * (possibly locked) directory vnode in ndp->ni_dvp.
    706 		 */
    707 		if (cnp->cn_flags & SAVESTART) {
    708 			ndp->ni_startdir = ndp->ni_dvp;
    709 			VREF(ndp->ni_startdir);
    710 		}
    711 		return (0);
    712 	}
    713 #ifdef NAMEI_DIAGNOSTIC
    714 	printf("found\n");
    715 #endif /* NAMEI_DIAGNOSTIC */
    716 
    717 	/*
    718 	 * Take into account any additional components consumed by the
    719 	 * underlying filesystem.  This will include any trailing slashes after
    720 	 * the last component consumed.
    721 	 */
    722 	if (cnp->cn_consume > 0) {
    723 		ndp->ni_pathlen -= cnp->cn_consume - slashes;
    724 		ndp->ni_next += cnp->cn_consume - slashes;
    725 		cnp->cn_consume = 0;
    726 		if (ndp->ni_next[0] == '\0')
    727 			cnp->cn_flags |= ISLASTCN;
    728 	}
    729 
    730 	dp = ndp->ni_vp;
    731 
    732 	/*
    733 	 * "dp" and "ndp->ni_dvp" are both locked and held,
    734 	 * and may be the same vnode.
    735 	 */
    736 
    737 	/*
    738 	 * Check to see if the vnode has been mounted on;
    739 	 * if so find the root of the mounted file system.
    740 	 */
    741 	while (dp->v_type == VDIR && (mp = dp->v_mountedhere) &&
    742 	       (cnp->cn_flags & NOCROSSMOUNT) == 0) {
    743 		if (vfs_busy(mp, 0, 0))
    744 			continue;
    745 
    746 		KASSERT(ndp->ni_dvp != dp);
    747 		vput(dp);
    748 		error = VFS_ROOT(mp, &tdp);
    749 		vfs_unbusy(mp);
    750 		if (error) {
    751 			goto bad;
    752 		}
    753 		ndp->ni_vp = dp = tdp;
    754 	}
    755 
    756 	/*
    757 	 * Check for symbolic link.  Back up over any slashes that we skipped,
    758 	 * as we will need them again.
    759 	 */
    760 	if ((dp->v_type == VLNK) && (cnp->cn_flags & (FOLLOW|REQUIREDIR))) {
    761 		ndp->ni_pathlen += slashes;
    762 		ndp->ni_next -= slashes;
    763 		cnp->cn_flags |= ISSYMLINK;
    764 		return (0);
    765 	}
    766 
    767 	/*
    768 	 * Check for directory, if the component was followed by a series of
    769 	 * slashes.
    770 	 */
    771 	if ((dp->v_type != VDIR) && (cnp->cn_flags & REQUIREDIR)) {
    772 		error = ENOTDIR;
    773 		KASSERT(dp != ndp->ni_dvp);
    774 		vput(dp);
    775 		goto bad;
    776 	}
    777 
    778 nextname:
    779 
    780 	/*
    781 	 * Not a symbolic link.  If this was not the last component, then
    782 	 * continue at the next component, else return.
    783 	 */
    784 	if (!(cnp->cn_flags & ISLASTCN)) {
    785 		cnp->cn_nameptr = ndp->ni_next;
    786 		if (ndp->ni_dvp == dp) {
    787 			vrele(ndp->ni_dvp);
    788 		} else {
    789 			vput(ndp->ni_dvp);
    790 		}
    791 		goto dirloop;
    792 	}
    793 
    794 terminal:
    795 
    796 	/*
    797 	 * Disallow directory write attempts on read-only file systems.
    798 	 */
    799 	if (rdonly &&
    800 	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
    801 
    802 		/*
    803 		 * Disallow directory write attempts on read-only
    804 		 * file systems.
    805 		 */
    806 		error = EROFS;
    807 		if (dp != ndp->ni_dvp) {
    808 			vput(dp);
    809 		}
    810 		goto bad;
    811 	}
    812 	if (ndp->ni_dvp != NULL) {
    813 		if (cnp->cn_flags & SAVESTART) {
    814 			ndp->ni_startdir = ndp->ni_dvp;
    815 			VREF(ndp->ni_startdir);
    816 		}
    817 	}
    818 	if ((cnp->cn_flags & LOCKLEAF) == 0) {
    819 		VOP_UNLOCK(dp, 0);
    820 	}
    821 	return (0);
    822 
    823 bad:
    824 	ndp->ni_vp = NULL;
    825 	return (error);
    826 }
    827 
    828 /*
    829  * Reacquire a path name component.
    830  * dvp is locked on entry and exit.
    831  * *vpp is locked on exit unless it's NULL.
    832  */
    833 int
    834 relookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp)
    835 {
    836 	struct vnode *dp = 0;		/* the directory we are searching */
    837 	int rdonly;			/* lookup read-only flag bit */
    838 	int error = 0;
    839 #ifdef DEBUG
    840 	u_long newhash;			/* DEBUG: check name hash */
    841 	const char *cp;			/* DEBUG: check name ptr/len */
    842 #endif /* DEBUG */
    843 
    844 	/*
    845 	 * Setup: break out flag bits into variables.
    846 	 */
    847 	rdonly = cnp->cn_flags & RDONLY;
    848 	cnp->cn_flags &= ~ISSYMLINK;
    849 	dp = dvp;
    850 
    851 /* dirloop: */
    852 	/*
    853 	 * Search a new directory.
    854 	 *
    855 	 * The cn_hash value is for use by vfs_cache.
    856 	 * The last component of the filename is left accessible via
    857 	 * cnp->cn_nameptr for callers that need the name. Callers needing
    858 	 * the name set the SAVENAME flag. When done, they assume
    859 	 * responsibility for freeing the pathname buffer.
    860 	 */
    861 #ifdef DEBUG
    862 	cp = NULL;
    863 	newhash = namei_hash(cnp->cn_nameptr, &cp);
    864 	if (newhash != cnp->cn_hash)
    865 		panic("relookup: bad hash");
    866 	if (cnp->cn_namelen != cp - cnp->cn_nameptr)
    867 		panic("relookup: bad len");
    868 	while (*cp == '/')
    869 		cp++;
    870 	if (*cp != 0)
    871 		panic("relookup: not last component");
    872 #endif /* DEBUG */
    873 #ifdef NAMEI_DIAGNOSTIC
    874 	printf("{%s}: ", cnp->cn_nameptr);
    875 #endif /* NAMEI_DIAGNOSTIC */
    876 
    877 	/*
    878 	 * Check for degenerate name (e.g. / or "")
    879 	 * which is a way of talking about a directory,
    880 	 * e.g. like "/." or ".".
    881 	 */
    882 	if (cnp->cn_nameptr[0] == '\0')
    883 		panic("relookup: null name");
    884 
    885 	if (cnp->cn_flags & ISDOTDOT)
    886 		panic("relookup: lookup on dot-dot");
    887 
    888 	/*
    889 	 * We now have a segment name to search for, and a directory to search.
    890 	 */
    891 	if ((error = VOP_LOOKUP(dp, vpp, cnp)) != 0) {
    892 #ifdef DIAGNOSTIC
    893 		if (*vpp != NULL)
    894 			panic("leaf `%s' should be empty", cnp->cn_nameptr);
    895 #endif
    896 		if (error != EJUSTRETURN)
    897 			goto bad;
    898 
    899 		/*
    900 		 * If creating and at end of pathname, then can consider
    901 		 * allowing file to be created.
    902 		 */
    903 		if (rdonly) {
    904 			error = EROFS;
    905 			goto bad;
    906 		}
    907 
    908 		/* ASSERT(dvp == ndp->ni_startdir) */
    909 		if (cnp->cn_flags & SAVESTART)
    910 			VREF(dvp);
    911 
    912 		/*
    913 		 * We return with ni_vp NULL to indicate that the entry
    914 		 * doesn't currently exist, leaving a pointer to the
    915 		 * locked directory vnode in ndp->ni_dvp.
    916 		 */
    917 		return (0);
    918 	}
    919 	dp = *vpp;
    920 
    921 #ifdef DIAGNOSTIC
    922 	/*
    923 	 * Check for symbolic link
    924 	 */
    925 	if (dp->v_type == VLNK && (cnp->cn_flags & FOLLOW))
    926 		panic("relookup: symlink found");
    927 #endif
    928 
    929 	/*
    930 	 * Check for read-only file systems.
    931 	 */
    932 	if (rdonly &&
    933 	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
    934 		error = EROFS;
    935 		goto bad;
    936 	}
    937 	/* ASSERT(dvp == ndp->ni_startdir) */
    938 	if (cnp->cn_flags & SAVESTART)
    939 		VREF(dvp);
    940 	if ((cnp->cn_flags & LOCKLEAF) == 0)
    941 		VOP_UNLOCK(dp, 0);
    942 	return (0);
    943 
    944 bad:
    945 	vput(dp);
    946 	*vpp = NULL;
    947 	return (error);
    948 }
    949