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