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