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