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