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