Home | History | Annotate | Line # | Download | only in kern
vfs_lookup.c revision 1.1.1.3
      1 /*
      2  * Copyright (c) 1982, 1986, 1989, 1993
      3  *	The Regents of the University of California.  All rights reserved.
      4  * (c) UNIX System Laboratories, Inc.
      5  * All or some portions of this file are derived from material licensed
      6  * to the University of California by American Telephone and Telegraph
      7  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
      8  * the permission of UNIX System Laboratories, Inc.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the University of
     21  *	California, Berkeley and its contributors.
     22  * 4. Neither the name of the University nor the names of its contributors
     23  *    may be used to endorse or promote products derived from this software
     24  *    without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  * SUCH DAMAGE.
     37  *
     38  *	@(#)vfs_lookup.c	8.10 (Berkeley) 5/27/95
     39  */
     40 
     41 #include <sys/param.h>
     42 #include <sys/syslimits.h>
     43 #include <sys/time.h>
     44 #include <sys/namei.h>
     45 #include <sys/vnode.h>
     46 #include <sys/mount.h>
     47 #include <sys/errno.h>
     48 #include <sys/malloc.h>
     49 #include <sys/filedesc.h>
     50 #include <sys/proc.h>
     51 
     52 #ifdef KTRACE
     53 #include <sys/ktrace.h>
     54 #endif
     55 
     56 /*
     57  * Convert a pathname into a pointer to a locked inode.
     58  *
     59  * The FOLLOW flag is set when symbolic links are to be followed
     60  * when they occur at the end of the name translation process.
     61  * Symbolic links are always followed for all other pathname
     62  * components other than the last.
     63  *
     64  * The segflg defines whether the name is to be copied from user
     65  * space or kernel space.
     66  *
     67  * Overall outline of namei:
     68  *
     69  *	copy in name
     70  *	get starting directory
     71  *	while (!done && !error) {
     72  *		call lookup to search path.
     73  *		if symbolic link, massage name in buffer and continue
     74  *	}
     75  */
     76 int
     77 namei(ndp)
     78 	register struct nameidata *ndp;
     79 {
     80 	register struct filedesc *fdp;	/* pointer to file descriptor state */
     81 	register char *cp;		/* pointer into pathname argument */
     82 	register struct vnode *dp;	/* the directory we are searching */
     83 	struct iovec aiov;		/* uio for reading symbolic links */
     84 	struct uio auio;
     85 	int error, linklen;
     86 	struct componentname *cnp = &ndp->ni_cnd;
     87 	struct proc *p = cnp->cn_proc;
     88 
     89 	ndp->ni_cnd.cn_cred = ndp->ni_cnd.cn_proc->p_ucred;
     90 #ifdef DIAGNOSTIC
     91 	if (!cnp->cn_cred || !cnp->cn_proc)
     92 		panic ("namei: bad cred/proc");
     93 	if (cnp->cn_nameiop & (~OPMASK))
     94 		panic ("namei: nameiop contaminated with flags");
     95 	if (cnp->cn_flags & OPMASK)
     96 		panic ("namei: flags contaminated with nameiops");
     97 #endif
     98 	fdp = cnp->cn_proc->p_fd;
     99 
    100 	/*
    101 	 * Get a buffer for the name to be translated, and copy the
    102 	 * name into the buffer.
    103 	 */
    104 	if ((cnp->cn_flags & HASBUF) == 0)
    105 		MALLOC(cnp->cn_pnbuf, caddr_t, MAXPATHLEN, M_NAMEI, M_WAITOK);
    106 	if (ndp->ni_segflg == UIO_SYSSPACE)
    107 		error = copystr(ndp->ni_dirp, cnp->cn_pnbuf,
    108 			    MAXPATHLEN, &ndp->ni_pathlen);
    109 	else
    110 		error = copyinstr(ndp->ni_dirp, cnp->cn_pnbuf,
    111 			    MAXPATHLEN, &ndp->ni_pathlen);
    112 	if (error) {
    113 		free(cnp->cn_pnbuf, M_NAMEI);
    114 		ndp->ni_vp = NULL;
    115 		return (error);
    116 	}
    117 	ndp->ni_loopcnt = 0;
    118 #ifdef KTRACE
    119 	if (KTRPOINT(cnp->cn_proc, KTR_NAMEI))
    120 		ktrnamei(cnp->cn_proc->p_tracep, cnp->cn_pnbuf);
    121 #endif
    122 
    123 	/*
    124 	 * Get starting point for the translation.
    125 	 */
    126 	if ((ndp->ni_rootdir = fdp->fd_rdir) == NULL)
    127 		ndp->ni_rootdir = rootvnode;
    128 	dp = fdp->fd_cdir;
    129 	VREF(dp);
    130 	for (;;) {
    131 		/*
    132 		 * Check if root directory should replace current directory.
    133 		 * Done at start of translation and after symbolic link.
    134 		 */
    135 		cnp->cn_nameptr = cnp->cn_pnbuf;
    136 		if (*(cnp->cn_nameptr) == '/') {
    137 			vrele(dp);
    138 			while (*(cnp->cn_nameptr) == '/') {
    139 				cnp->cn_nameptr++;
    140 				ndp->ni_pathlen--;
    141 			}
    142 			dp = ndp->ni_rootdir;
    143 			VREF(dp);
    144 		}
    145 		ndp->ni_startdir = dp;
    146 		if (error = lookup(ndp)) {
    147 			FREE(cnp->cn_pnbuf, M_NAMEI);
    148 			return (error);
    149 		}
    150 		/*
    151 		 * Check for symbolic link
    152 		 */
    153 		if ((cnp->cn_flags & ISSYMLINK) == 0) {
    154 			if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0)
    155 				FREE(cnp->cn_pnbuf, M_NAMEI);
    156 			else
    157 				cnp->cn_flags |= HASBUF;
    158 			return (0);
    159 		}
    160 		if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1)
    161 			VOP_UNLOCK(ndp->ni_dvp, 0, p);
    162 		if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
    163 			error = ELOOP;
    164 			break;
    165 		}
    166 		if (ndp->ni_pathlen > 1)
    167 			MALLOC(cp, char *, MAXPATHLEN, M_NAMEI, M_WAITOK);
    168 		else
    169 			cp = cnp->cn_pnbuf;
    170 		aiov.iov_base = cp;
    171 		aiov.iov_len = MAXPATHLEN;
    172 		auio.uio_iov = &aiov;
    173 		auio.uio_iovcnt = 1;
    174 		auio.uio_offset = 0;
    175 		auio.uio_rw = UIO_READ;
    176 		auio.uio_segflg = UIO_SYSSPACE;
    177 		auio.uio_procp = (struct proc *)0;
    178 		auio.uio_resid = MAXPATHLEN;
    179 		if (error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred)) {
    180 			if (ndp->ni_pathlen > 1)
    181 				free(cp, M_NAMEI);
    182 			break;
    183 		}
    184 		linklen = MAXPATHLEN - auio.uio_resid;
    185 		if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
    186 			if (ndp->ni_pathlen > 1)
    187 				free(cp, M_NAMEI);
    188 			error = ENAMETOOLONG;
    189 			break;
    190 		}
    191 		if (ndp->ni_pathlen > 1) {
    192 			bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
    193 			FREE(cnp->cn_pnbuf, M_NAMEI);
    194 			cnp->cn_pnbuf = cp;
    195 		} else
    196 			cnp->cn_pnbuf[linklen] = '\0';
    197 		ndp->ni_pathlen += linklen;
    198 		vput(ndp->ni_vp);
    199 		dp = ndp->ni_dvp;
    200 	}
    201 	FREE(cnp->cn_pnbuf, M_NAMEI);
    202 	vrele(ndp->ni_dvp);
    203 	vput(ndp->ni_vp);
    204 	ndp->ni_vp = NULL;
    205 	return (error);
    206 }
    207 
    208 /*
    209  * Search a pathname.
    210  * This is a very central and rather complicated routine.
    211  *
    212  * The pathname is pointed to by ni_ptr and is of length ni_pathlen.
    213  * The starting directory is taken from ni_startdir. The pathname is
    214  * descended until done, or a symbolic link is encountered. The variable
    215  * ni_more is clear if the path is completed; it is set to one if a
    216  * symbolic link needing interpretation is encountered.
    217  *
    218  * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
    219  * whether the name is to be looked up, created, renamed, or deleted.
    220  * When CREATE, RENAME, or DELETE is specified, information usable in
    221  * creating, renaming, or deleting a directory entry may be calculated.
    222  * If flag has LOCKPARENT or'ed into it, the parent directory is returned
    223  * locked. If flag has WANTPARENT or'ed into it, the parent directory is
    224  * returned unlocked. Otherwise the parent directory is not returned. If
    225  * the target of the pathname exists and LOCKLEAF is or'ed into the flag
    226  * the target is returned locked, otherwise it is returned unlocked.
    227  * When creating or renaming and LOCKPARENT is specified, the target may not
    228  * be ".".  When deleting and LOCKPARENT is specified, the target may be ".".
    229  *
    230  * Overall outline of lookup:
    231  *
    232  * dirloop:
    233  *	identify next component of name at ndp->ni_ptr
    234  *	handle degenerate case where name is null string
    235  *	if .. and crossing mount points and on mounted filesys, find parent
    236  *	call VOP_LOOKUP routine for next component name
    237  *	    directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set
    238  *	    component vnode returned in ni_vp (if it exists), locked.
    239  *	if result vnode is mounted on and crossing mount points,
    240  *	    find mounted on vnode
    241  *	if more components of name, do next level at dirloop
    242  *	return the answer in ni_vp, locked if LOCKLEAF set
    243  *	    if LOCKPARENT set, return locked parent in ni_dvp
    244  *	    if WANTPARENT set, return unlocked parent in ni_dvp
    245  */
    246 int
    247 lookup(ndp)
    248 	register struct nameidata *ndp;
    249 {
    250 	register char *cp;		/* pointer into pathname argument */
    251 	register struct vnode *dp = 0;	/* the directory we are searching */
    252 	struct vnode *tdp;		/* saved dp */
    253 	struct mount *mp;		/* mount table entry */
    254 	int docache;			/* == 0 do not cache last component */
    255 	int wantparent;			/* 1 => wantparent or lockparent flag */
    256 	int rdonly;			/* lookup read-only flag bit */
    257 	int error = 0;
    258 	struct componentname *cnp = &ndp->ni_cnd;
    259 	struct proc *p = cnp->cn_proc;
    260 
    261 	/*
    262 	 * Setup: break out flag bits into variables.
    263 	 */
    264 	wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT);
    265 	docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE;
    266 	if (cnp->cn_nameiop == DELETE ||
    267 	    (wantparent && cnp->cn_nameiop != CREATE))
    268 		docache = 0;
    269 	rdonly = cnp->cn_flags & RDONLY;
    270 	ndp->ni_dvp = NULL;
    271 	cnp->cn_flags &= ~ISSYMLINK;
    272 	dp = ndp->ni_startdir;
    273 	ndp->ni_startdir = NULLVP;
    274 	vn_lock(dp, LK_EXCLUSIVE | LK_RETRY, p);
    275 
    276 dirloop:
    277 	/*
    278 	 * Search a new directory.
    279 	 *
    280 	 * The cn_hash value is for use by vfs_cache.
    281 	 * The last component of the filename is left accessible via
    282 	 * cnp->cn_nameptr for callers that need the name. Callers needing
    283 	 * the name set the SAVENAME flag. When done, they assume
    284 	 * responsibility for freeing the pathname buffer.
    285 	 */
    286 	cnp->cn_consume = 0;
    287 	cnp->cn_hash = 0;
    288 	for (cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++)
    289 		cnp->cn_hash += (unsigned char)*cp;
    290 	cnp->cn_namelen = cp - cnp->cn_nameptr;
    291 	if (cnp->cn_namelen > NAME_MAX) {
    292 		error = ENAMETOOLONG;
    293 		goto bad;
    294 	}
    295 #ifdef NAMEI_DIAGNOSTIC
    296 	{ char c = *cp;
    297 	*cp = '\0';
    298 	printf("{%s}: ", cnp->cn_nameptr);
    299 	*cp = c; }
    300 #endif
    301 	ndp->ni_pathlen -= cnp->cn_namelen;
    302 	ndp->ni_next = cp;
    303 	cnp->cn_flags |= MAKEENTRY;
    304 	if (*cp == '\0' && docache == 0)
    305 		cnp->cn_flags &= ~MAKEENTRY;
    306 	if (cnp->cn_namelen == 2 &&
    307 	    cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.')
    308 		cnp->cn_flags |= ISDOTDOT;
    309 	else
    310 		cnp->cn_flags &= ~ISDOTDOT;
    311 	if (*ndp->ni_next == 0)
    312 		cnp->cn_flags |= ISLASTCN;
    313 	else
    314 		cnp->cn_flags &= ~ISLASTCN;
    315 
    316 
    317 	/*
    318 	 * Check for degenerate name (e.g. / or "")
    319 	 * which is a way of talking about a directory,
    320 	 * e.g. like "/." or ".".
    321 	 */
    322 	if (cnp->cn_nameptr[0] == '\0') {
    323 		if (dp->v_type != VDIR) {
    324 			error = ENOTDIR;
    325 			goto bad;
    326 		}
    327 		if (cnp->cn_nameiop != LOOKUP) {
    328 			error = EISDIR;
    329 			goto bad;
    330 		}
    331 		if (wantparent) {
    332 			ndp->ni_dvp = dp;
    333 			VREF(dp);
    334 		}
    335 		ndp->ni_vp = dp;
    336 		if (!(cnp->cn_flags & (LOCKPARENT | LOCKLEAF)))
    337 			VOP_UNLOCK(dp, 0, p);
    338 		if (cnp->cn_flags & SAVESTART)
    339 			panic("lookup: SAVESTART");
    340 		return (0);
    341 	}
    342 
    343 	/*
    344 	 * Handle "..": two special cases.
    345 	 * 1. If at root directory (e.g. after chroot)
    346 	 *    or at absolute root directory
    347 	 *    then ignore it so can't get out.
    348 	 * 2. If this vnode is the root of a mounted
    349 	 *    filesystem, then replace it with the
    350 	 *    vnode which was mounted on so we take the
    351 	 *    .. in the other file system.
    352 	 */
    353 	if (cnp->cn_flags & ISDOTDOT) {
    354 		for (;;) {
    355 			if (dp == ndp->ni_rootdir || dp == rootvnode) {
    356 				ndp->ni_dvp = dp;
    357 				ndp->ni_vp = dp;
    358 				VREF(dp);
    359 				goto nextname;
    360 			}
    361 			if ((dp->v_flag & VROOT) == 0 ||
    362 			    (cnp->cn_flags & NOCROSSMOUNT))
    363 				break;
    364 			tdp = dp;
    365 			dp = dp->v_mount->mnt_vnodecovered;
    366 			vput(tdp);
    367 			VREF(dp);
    368 			vn_lock(dp, LK_EXCLUSIVE | LK_RETRY, p);
    369 		}
    370 	}
    371 
    372 	/*
    373 	 * We now have a segment name to search for, and a directory to search.
    374 	 */
    375 unionlookup:
    376 	ndp->ni_dvp = dp;
    377 	ndp->ni_vp = NULL;
    378 	if (error = VOP_LOOKUP(dp, &ndp->ni_vp, cnp)) {
    379 #ifdef DIAGNOSTIC
    380 		if (ndp->ni_vp != NULL)
    381 			panic("leaf should be empty");
    382 #endif
    383 #ifdef NAMEI_DIAGNOSTIC
    384 		printf("not found\n");
    385 #endif
    386 		if ((error == ENOENT) &&
    387 		    (dp->v_flag & VROOT) &&
    388 		    (dp->v_mount->mnt_flag & MNT_UNION)) {
    389 			tdp = dp;
    390 			dp = dp->v_mount->mnt_vnodecovered;
    391 			vput(tdp);
    392 			VREF(dp);
    393 			vn_lock(dp, LK_EXCLUSIVE | LK_RETRY, p);
    394 			goto unionlookup;
    395 		}
    396 
    397 		if (error != EJUSTRETURN)
    398 			goto bad;
    399 		/*
    400 		 * If creating and at end of pathname, then can consider
    401 		 * allowing file to be created.
    402 		 */
    403 		if (rdonly) {
    404 			error = EROFS;
    405 			goto bad;
    406 		}
    407 		/*
    408 		 * We return with ni_vp NULL to indicate that the entry
    409 		 * doesn't currently exist, leaving a pointer to the
    410 		 * (possibly locked) directory inode in ndp->ni_dvp.
    411 		 */
    412 		if (cnp->cn_flags & SAVESTART) {
    413 			ndp->ni_startdir = ndp->ni_dvp;
    414 			VREF(ndp->ni_startdir);
    415 		}
    416 		return (0);
    417 	}
    418 #ifdef NAMEI_DIAGNOSTIC
    419 	printf("found\n");
    420 #endif
    421 
    422 	/*
    423 	 * Take into account any additional components consumed by
    424 	 * the underlying filesystem.
    425 	 */
    426 	if (cnp->cn_consume > 0) {
    427 		cnp->cn_nameptr += cnp->cn_consume;
    428 		ndp->ni_next += cnp->cn_consume;
    429 		ndp->ni_pathlen -= cnp->cn_consume;
    430 		cnp->cn_consume = 0;
    431 	}
    432 
    433 	dp = ndp->ni_vp;
    434 	/*
    435 	 * Check to see if the vnode has been mounted on;
    436 	 * if so find the root of the mounted file system.
    437 	 */
    438 	while (dp->v_type == VDIR && (mp = dp->v_mountedhere) &&
    439 	       (cnp->cn_flags & NOCROSSMOUNT) == 0) {
    440 		if (vfs_busy(mp, 0, 0, p))
    441 			continue;
    442 		error = VFS_ROOT(mp, &tdp);
    443 		vfs_unbusy(mp, p);
    444 		if (error)
    445 			goto bad2;
    446 		vput(dp);
    447 		ndp->ni_vp = dp = tdp;
    448 	}
    449 
    450 	/*
    451 	 * Check for symbolic link
    452 	 */
    453 	if ((dp->v_type == VLNK) &&
    454 	    ((cnp->cn_flags & FOLLOW) || *ndp->ni_next == '/')) {
    455 		cnp->cn_flags |= ISSYMLINK;
    456 		return (0);
    457 	}
    458 
    459 nextname:
    460 	/*
    461 	 * Not a symbolic link.  If more pathname,
    462 	 * continue at next component, else return.
    463 	 */
    464 	if (*ndp->ni_next == '/') {
    465 		cnp->cn_nameptr = ndp->ni_next;
    466 		while (*cnp->cn_nameptr == '/') {
    467 			cnp->cn_nameptr++;
    468 			ndp->ni_pathlen--;
    469 		}
    470 		vrele(ndp->ni_dvp);
    471 		goto dirloop;
    472 	}
    473 	/*
    474 	 * Disallow directory write attempts on read-only file systems.
    475 	 */
    476 	if (rdonly &&
    477 	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
    478 		error = EROFS;
    479 		goto bad2;
    480 	}
    481 	if (cnp->cn_flags & SAVESTART) {
    482 		ndp->ni_startdir = ndp->ni_dvp;
    483 		VREF(ndp->ni_startdir);
    484 	}
    485 	if (!wantparent)
    486 		vrele(ndp->ni_dvp);
    487 	if ((cnp->cn_flags & LOCKLEAF) == 0)
    488 		VOP_UNLOCK(dp, 0, p);
    489 	return (0);
    490 
    491 bad2:
    492 	if ((cnp->cn_flags & LOCKPARENT) && *ndp->ni_next == '\0')
    493 		VOP_UNLOCK(ndp->ni_dvp, 0, p);
    494 	vrele(ndp->ni_dvp);
    495 bad:
    496 	vput(dp);
    497 	ndp->ni_vp = NULL;
    498 	return (error);
    499 }
    500 
    501 /*
    502  * relookup - lookup a path name component
    503  *    Used by lookup to re-aquire things.
    504  */
    505 int
    506 relookup(dvp, vpp, cnp)
    507 	struct vnode *dvp, **vpp;
    508 	struct componentname *cnp;
    509 {
    510 	struct proc *p = cnp->cn_proc;
    511 	struct vnode *dp = 0;		/* the directory we are searching */
    512 	int docache;			/* == 0 do not cache last component */
    513 	int wantparent;			/* 1 => wantparent or lockparent flag */
    514 	int rdonly;			/* lookup read-only flag bit */
    515 	int error = 0;
    516 #ifdef NAMEI_DIAGNOSTIC
    517 	int newhash;			/* DEBUG: check name hash */
    518 	char *cp;			/* DEBUG: check name ptr/len */
    519 #endif
    520 
    521 	/*
    522 	 * Setup: break out flag bits into variables.
    523 	 */
    524 	wantparent = cnp->cn_flags & (LOCKPARENT|WANTPARENT);
    525 	docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE;
    526 	if (cnp->cn_nameiop == DELETE ||
    527 	    (wantparent && cnp->cn_nameiop != CREATE))
    528 		docache = 0;
    529 	rdonly = cnp->cn_flags & RDONLY;
    530 	cnp->cn_flags &= ~ISSYMLINK;
    531 	dp = dvp;
    532 	vn_lock(dp, LK_EXCLUSIVE | LK_RETRY, p);
    533 
    534 /* dirloop: */
    535 	/*
    536 	 * Search a new directory.
    537 	 *
    538 	 * The cn_hash value is for use by vfs_cache.
    539 	 * The last component of the filename is left accessible via
    540 	 * cnp->cn_nameptr for callers that need the name. Callers needing
    541 	 * the name set the SAVENAME flag. When done, they assume
    542 	 * responsibility for freeing the pathname buffer.
    543 	 */
    544 #ifdef NAMEI_DIAGNOSTIC
    545 	for (newhash = 0, cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++)
    546 		newhash += (unsigned char)*cp;
    547 	if (newhash != cnp->cn_hash)
    548 		panic("relookup: bad hash");
    549 	if (cnp->cn_namelen != cp - cnp->cn_nameptr)
    550 		panic ("relookup: bad len");
    551 	if (*cp != 0)
    552 		panic("relookup: not last component");
    553 	printf("{%s}: ", cnp->cn_nameptr);
    554 #endif
    555 
    556 	/*
    557 	 * Check for degenerate name (e.g. / or "")
    558 	 * which is a way of talking about a directory,
    559 	 * e.g. like "/." or ".".
    560 	 */
    561 	if (cnp->cn_nameptr[0] == '\0') {
    562 		if (cnp->cn_nameiop != LOOKUP || wantparent) {
    563 			error = EISDIR;
    564 			goto bad;
    565 		}
    566 		if (dp->v_type != VDIR) {
    567 			error = ENOTDIR;
    568 			goto bad;
    569 		}
    570 		if (!(cnp->cn_flags & LOCKLEAF))
    571 			VOP_UNLOCK(dp, 0, p);
    572 		*vpp = dp;
    573 		if (cnp->cn_flags & SAVESTART)
    574 			panic("lookup: SAVESTART");
    575 		return (0);
    576 	}
    577 
    578 	if (cnp->cn_flags & ISDOTDOT)
    579 		panic ("relookup: lookup on dot-dot");
    580 
    581 	/*
    582 	 * We now have a segment name to search for, and a directory to search.
    583 	 */
    584 	if (error = VOP_LOOKUP(dp, vpp, cnp)) {
    585 #ifdef DIAGNOSTIC
    586 		if (*vpp != NULL)
    587 			panic("leaf should be empty");
    588 #endif
    589 		if (error != EJUSTRETURN)
    590 			goto bad;
    591 		/*
    592 		 * If creating and at end of pathname, then can consider
    593 		 * allowing file to be created.
    594 		 */
    595 		if (rdonly) {
    596 			error = EROFS;
    597 			goto bad;
    598 		}
    599 		/* ASSERT(dvp == ndp->ni_startdir) */
    600 		if (cnp->cn_flags & SAVESTART)
    601 			VREF(dvp);
    602 		/*
    603 		 * We return with ni_vp NULL to indicate that the entry
    604 		 * doesn't currently exist, leaving a pointer to the
    605 		 * (possibly locked) directory inode in ndp->ni_dvp.
    606 		 */
    607 		return (0);
    608 	}
    609 	dp = *vpp;
    610 
    611 #ifdef DIAGNOSTIC
    612 	/*
    613 	 * Check for symbolic link
    614 	 */
    615 	if (dp->v_type == VLNK && (cnp->cn_flags & FOLLOW))
    616 		panic ("relookup: symlink found.\n");
    617 #endif
    618 
    619 	/*
    620 	 * Disallow directory write attempts on read-only file systems.
    621 	 */
    622 	if (rdonly &&
    623 	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
    624 		error = EROFS;
    625 		goto bad2;
    626 	}
    627 	/* ASSERT(dvp == ndp->ni_startdir) */
    628 	if (cnp->cn_flags & SAVESTART)
    629 		VREF(dvp);
    630 
    631 	if (!wantparent)
    632 		vrele(dvp);
    633 	if ((cnp->cn_flags & LOCKLEAF) == 0)
    634 		VOP_UNLOCK(dp, 0, p);
    635 	return (0);
    636 
    637 bad2:
    638 	if ((cnp->cn_flags & LOCKPARENT) && (cnp->cn_flags & ISLASTCN))
    639 		VOP_UNLOCK(dvp, 0, p);
    640 	vrele(dvp);
    641 bad:
    642 	vput(dp);
    643 	*vpp = NULL;
    644 	return (error);
    645 }
    646