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