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