Home | History | Annotate | Line # | Download | only in kern
vfs_lookup.c revision 1.14
      1 /*	$NetBSD: vfs_lookup.c,v 1.14 1994/12/14 19:41:23 mycroft 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.6 (Berkeley) 11/21/94
     41  */
     42 
     43 #include <sys/param.h>
     44 #include <sys/syslimits.h>
     45 #include <sys/time.h>
     46 #include <sys/namei.h>
     47 #include <sys/vnode.h>
     48 #include <sys/mount.h>
     49 #include <sys/errno.h>
     50 #include <sys/malloc.h>
     51 #include <sys/filedesc.h>
     52 #include <sys/proc.h>
     53 
     54 #ifdef KTRACE
     55 #include <sys/ktrace.h>
     56 #endif
     57 
     58 /*
     59  * Convert a pathname into a pointer to a locked inode.
     60  *
     61  * The FOLLOW flag is set when symbolic links are to be followed
     62  * when they occur at the end of the name translation process.
     63  * Symbolic links are always followed for all other pathname
     64  * components other than the last.
     65  *
     66  * The segflg defines whether the name is to be copied from user
     67  * space or kernel space.
     68  *
     69  * Overall outline of namei:
     70  *
     71  *	copy in name
     72  *	get starting directory
     73  *	while (!done && !error) {
     74  *		call lookup to search path.
     75  *		if symbolic link, massage name in buffer and continue
     76  *	}
     77  */
     78 int
     79 namei(ndp)
     80 	register struct nameidata *ndp;
     81 {
     82 	register struct filedesc *fdp;	/* pointer to file descriptor state */
     83 	register char *cp;		/* pointer into pathname argument */
     84 	register struct vnode *dp;	/* the directory we are searching */
     85 	struct iovec aiov;		/* uio for reading symbolic links */
     86 	struct uio auio;
     87 	int error, linklen;
     88 	struct componentname *cnp = &ndp->ni_cnd;
     89 
     90 	ndp->ni_cnd.cn_cred = ndp->ni_cnd.cn_proc->p_ucred;
     91 #ifdef DIAGNOSTIC
     92 	if (!cnp->cn_cred || !cnp->cn_proc)
     93 		panic ("namei: bad cred/proc");
     94 	if (cnp->cn_nameiop & (~OPMASK))
     95 		panic ("namei: nameiop contaminated with flags");
     96 	if (cnp->cn_flags & OPMASK)
     97 		panic ("namei: flags contaminated with nameiops");
     98 #endif
     99 	fdp = cnp->cn_proc->p_fd;
    100 
    101 	/*
    102 	 * Get a buffer for the name to be translated, and copy the
    103 	 * name into the buffer.
    104 	 */
    105 	if ((cnp->cn_flags & HASBUF) == 0)
    106 		MALLOC(cnp->cn_pnbuf, caddr_t, MAXPATHLEN, M_NAMEI, M_WAITOK);
    107 	if (ndp->ni_segflg == UIO_SYSSPACE)
    108 		error = copystr(ndp->ni_dirp, cnp->cn_pnbuf,
    109 			    MAXPATHLEN, &ndp->ni_pathlen);
    110 	else
    111 		error = copyinstr(ndp->ni_dirp, cnp->cn_pnbuf,
    112 			    MAXPATHLEN, &ndp->ni_pathlen);
    113 	if (error) {
    114 		free(cnp->cn_pnbuf, M_NAMEI);
    115 		ndp->ni_vp = NULL;
    116 		return (error);
    117 	}
    118 	ndp->ni_loopcnt = 0;
    119 #ifdef KTRACE
    120 	if (KTRPOINT(cnp->cn_proc, KTR_NAMEI))
    121 		ktrnamei(cnp->cn_proc->p_tracep, cnp->cn_pnbuf);
    122 #endif
    123 
    124 	/*
    125 	 * Get starting point for the translation.
    126 	 */
    127 	if ((ndp->ni_rootdir = fdp->fd_rdir) == NULL)
    128 		ndp->ni_rootdir = rootvnode;
    129 	dp = fdp->fd_cdir;
    130 	VREF(dp);
    131 	for (;;) {
    132 		/*
    133 		 * Check if root directory should replace current directory.
    134 		 * Done at start of translation and after symbolic link.
    135 		 */
    136 		cnp->cn_nameptr = cnp->cn_pnbuf;
    137 		if (*(cnp->cn_nameptr) == '/') {
    138 			vrele(dp);
    139 			while (*(cnp->cn_nameptr) == '/') {
    140 				cnp->cn_nameptr++;
    141 				ndp->ni_pathlen--;
    142 			}
    143 			dp = ndp->ni_rootdir;
    144 			VREF(dp);
    145 		}
    146 		ndp->ni_startdir = dp;
    147 		if (error = lookup(ndp)) {
    148 			FREE(cnp->cn_pnbuf, M_NAMEI);
    149 			return (error);
    150 		}
    151 		/*
    152 		 * Check for symbolic link
    153 		 */
    154 		if ((cnp->cn_flags & ISSYMLINK) == 0) {
    155 			if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0)
    156 				FREE(cnp->cn_pnbuf, M_NAMEI);
    157 			else
    158 				cnp->cn_flags |= HASBUF;
    159 			return (0);
    160 		}
    161 		if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1)
    162 			VOP_UNLOCK(ndp->ni_dvp);
    163 		if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
    164 			error = ELOOP;
    165 			break;
    166 		}
    167 		if (ndp->ni_pathlen > 1)
    168 			MALLOC(cp, char *, MAXPATHLEN, M_NAMEI, M_WAITOK);
    169 		else
    170 			cp = cnp->cn_pnbuf;
    171 		aiov.iov_base = cp;
    172 		aiov.iov_len = MAXPATHLEN;
    173 		auio.uio_iov = &aiov;
    174 		auio.uio_iovcnt = 1;
    175 		auio.uio_offset = 0;
    176 		auio.uio_rw = UIO_READ;
    177 		auio.uio_segflg = UIO_SYSSPACE;
    178 		auio.uio_procp = (struct proc *)0;
    179 		auio.uio_resid = MAXPATHLEN;
    180 		if (error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred)) {
    181 			if (ndp->ni_pathlen > 1)
    182 				free(cp, M_NAMEI);
    183 			break;
    184 		}
    185 		linklen = MAXPATHLEN - auio.uio_resid;
    186 		if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
    187 			if (ndp->ni_pathlen > 1)
    188 				free(cp, M_NAMEI);
    189 			error = ENAMETOOLONG;
    190 			break;
    191 		}
    192 		if (ndp->ni_pathlen > 1) {
    193 			bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
    194 			FREE(cnp->cn_pnbuf, M_NAMEI);
    195 			cnp->cn_pnbuf = cp;
    196 		} else
    197 			cnp->cn_pnbuf[linklen] = '\0';
    198 		ndp->ni_pathlen += linklen;
    199 		vput(ndp->ni_vp);
    200 		dp = ndp->ni_dvp;
    201 	}
    202 	FREE(cnp->cn_pnbuf, M_NAMEI);
    203 	vrele(ndp->ni_dvp);
    204 	vput(ndp->ni_vp);
    205 	ndp->ni_vp = NULL;
    206 	return (error);
    207 }
    208 
    209 /*
    210  * Search a pathname.
    211  * This is a very central and rather complicated routine.
    212  *
    213  * The pathname is pointed to by ni_ptr and is of length ni_pathlen.
    214  * The starting directory is taken from ni_startdir. The pathname is
    215  * descended until done, or a symbolic link is encountered. The variable
    216  * ni_more is clear if the path is completed; it is set to one if a
    217  * symbolic link needing interpretation is encountered.
    218  *
    219  * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
    220  * whether the name is to be looked up, created, renamed, or deleted.
    221  * When CREATE, RENAME, or DELETE is specified, information usable in
    222  * creating, renaming, or deleting a directory entry may be calculated.
    223  * If flag has LOCKPARENT or'ed into it, the parent directory is returned
    224  * locked. If flag has WANTPARENT or'ed into it, the parent directory is
    225  * returned unlocked. Otherwise the parent directory is not returned. If
    226  * the target of the pathname exists and LOCKLEAF is or'ed into the flag
    227  * the target is returned locked, otherwise it is returned unlocked.
    228  * When creating or renaming and LOCKPARENT is specified, the target may not
    229  * be ".".  When deleting and LOCKPARENT is specified, the target may be ".".
    230  *
    231  * Overall outline of lookup:
    232  *
    233  * dirloop:
    234  *	identify next component of name at ndp->ni_ptr
    235  *	handle degenerate case where name is null string
    236  *	if .. and crossing mount points and on mounted filesys, find parent
    237  *	call VOP_LOOKUP routine for next component name
    238  *	    directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set
    239  *	    component vnode returned in ni_vp (if it exists), locked.
    240  *	if result vnode is mounted on and crossing mount points,
    241  *	    find mounted on vnode
    242  *	if more components of name, do next level at dirloop
    243  *	return the answer in ni_vp, locked if LOCKLEAF set
    244  *	    if LOCKPARENT set, return locked parent in ni_dvp
    245  *	    if WANTPARENT set, return unlocked parent in ni_dvp
    246  */
    247 int
    248 lookup(ndp)
    249 	register struct nameidata *ndp;
    250 {
    251 	register char *cp;		/* pointer into pathname argument */
    252 	register struct vnode *dp = 0;	/* the directory we are searching */
    253 	struct vnode *tdp;		/* saved dp */
    254 	struct mount *mp;		/* mount table entry */
    255 	int docache;			/* == 0 do not cache last component */
    256 	int wantparent;			/* 1 => wantparent or lockparent flag */
    257 	int rdonly;			/* lookup read-only flag bit */
    258 	int error = 0;
    259 	struct componentname *cnp = &ndp->ni_cnd;
    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 	VOP_LOCK(dp);
    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);
    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 			VOP_LOCK(dp);
    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 	if (error = VOP_LOOKUP(dp, &ndp->ni_vp, cnp)) {
    378 #ifdef DIAGNOSTIC
    379 		if (ndp->ni_vp != NULL)
    380 			panic("leaf should be empty");
    381 #endif
    382 #ifdef NAMEI_DIAGNOSTIC
    383 		printf("not found\n");
    384 #endif
    385 		if ((error == ENOENT) &&
    386 		    (dp->v_flag & VROOT) &&
    387 		    (dp->v_mount->mnt_flag & MNT_UNION)) {
    388 			tdp = dp;
    389 			dp = dp->v_mount->mnt_vnodecovered;
    390 			vput(tdp);
    391 			VREF(dp);
    392 			VOP_LOCK(dp);
    393 			goto unionlookup;
    394 		}
    395 
    396 		if (error != EJUSTRETURN)
    397 			goto bad;
    398 		/*
    399 		 * If creating and at end of pathname, then can consider
    400 		 * allowing file to be created.
    401 		 */
    402 		if (rdonly || (ndp->ni_dvp->v_mount->mnt_flag & MNT_RDONLY)) {
    403 			error = EROFS;
    404 			goto bad;
    405 		}
    406 		/*
    407 		 * We return with ni_vp NULL to indicate that the entry
    408 		 * doesn't currently exist, leaving a pointer to the
    409 		 * (possibly locked) directory inode in ndp->ni_dvp.
    410 		 */
    411 		if (cnp->cn_flags & SAVESTART) {
    412 			ndp->ni_startdir = ndp->ni_dvp;
    413 			VREF(ndp->ni_startdir);
    414 		}
    415 		return (0);
    416 	}
    417 #ifdef NAMEI_DIAGNOSTIC
    418 	printf("found\n");
    419 #endif
    420 
    421 	/*
    422 	 * Take into account any additional components consumed by
    423 	 * the underlying filesystem.
    424 	 */
    425 	if (cnp->cn_consume > 0) {
    426 		cnp->cn_nameptr += cnp->cn_consume;
    427 		ndp->ni_next += cnp->cn_consume;
    428 		ndp->ni_pathlen -= cnp->cn_consume;
    429 		cnp->cn_consume = 0;
    430 	}
    431 
    432 	dp = ndp->ni_vp;
    433 	/*
    434 	 * Check to see if the vnode has been mounted on;
    435 	 * if so find the root of the mounted file system.
    436 	 */
    437 	while (dp->v_type == VDIR && (mp = dp->v_mountedhere) &&
    438 	       (cnp->cn_flags & NOCROSSMOUNT) == 0) {
    439 		if (mp->mnt_flag & MNT_MLOCK) {
    440 			mp->mnt_flag |= MNT_MWAIT;
    441 			sleep((caddr_t)mp, PVFS);
    442 			continue;
    443 		}
    444 		if (error = VFS_ROOT(dp->v_mountedhere, &tdp))
    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 	 * Check for read-only file systems.
    475 	 */
    476 	if (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME) {
    477 		/*
    478 		 * Disallow directory write attempts on read-only
    479 		 * file systems.
    480 		 */
    481 		if (rdonly || (dp->v_mount->mnt_flag & MNT_RDONLY) ||
    482 		    (wantparent &&
    483 		     (ndp->ni_dvp->v_mount->mnt_flag & MNT_RDONLY))) {
    484 			error = EROFS;
    485 			goto bad2;
    486 		}
    487 	}
    488 	if (cnp->cn_flags & SAVESTART) {
    489 		ndp->ni_startdir = ndp->ni_dvp;
    490 		VREF(ndp->ni_startdir);
    491 	}
    492 	if (!wantparent)
    493 		vrele(ndp->ni_dvp);
    494 	if ((cnp->cn_flags & LOCKLEAF) == 0)
    495 		VOP_UNLOCK(dp);
    496 	return (0);
    497 
    498 bad2:
    499 	if ((cnp->cn_flags & LOCKPARENT) && *ndp->ni_next == '\0')
    500 		VOP_UNLOCK(ndp->ni_dvp);
    501 	vrele(ndp->ni_dvp);
    502 bad:
    503 	vput(dp);
    504 	ndp->ni_vp = NULL;
    505 	return (error);
    506 }
    507 
    508 /*
    509  * Reacquire a path name component.
    510  */
    511 int
    512 relookup(dvp, vpp, cnp)
    513 	struct vnode *dvp, **vpp;
    514 	struct componentname *cnp;
    515 {
    516 	register struct vnode *dp = 0;	/* the directory we are searching */
    517 	int docache;			/* == 0 do not cache last component */
    518 	int wantparent;			/* 1 => wantparent or lockparent flag */
    519 	int rdonly;			/* lookup read-only flag bit */
    520 	int error = 0;
    521 #ifdef NAMEI_DIAGNOSTIC
    522 	int newhash;			/* DEBUG: check name hash */
    523 	char *cp;			/* DEBUG: check name ptr/len */
    524 #endif
    525 
    526 	/*
    527 	 * Setup: break out flag bits into variables.
    528 	 */
    529 	wantparent = cnp->cn_flags & (LOCKPARENT|WANTPARENT);
    530 	docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE;
    531 	if (cnp->cn_nameiop == DELETE ||
    532 	    (wantparent && cnp->cn_nameiop != CREATE))
    533 		docache = 0;
    534 	rdonly = cnp->cn_flags & RDONLY;
    535 	cnp->cn_flags &= ~ISSYMLINK;
    536 	dp = dvp;
    537 	VOP_LOCK(dp);
    538 
    539 /* dirloop: */
    540 	/*
    541 	 * Search a new directory.
    542 	 *
    543 	 * The cn_hash value is for use by vfs_cache.
    544 	 * The last component of the filename is left accessible via
    545 	 * cnp->cn_nameptr for callers that need the name. Callers needing
    546 	 * the name set the SAVENAME flag. When done, they assume
    547 	 * responsibility for freeing the pathname buffer.
    548 	 */
    549 #ifdef NAMEI_DIAGNOSTIC
    550 	for (newhash = 0, cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++)
    551 		newhash += (unsigned char)*cp;
    552 	if (newhash != cnp->cn_hash)
    553 		panic("relookup: bad hash");
    554 	if (cnp->cn_namelen != cp - cnp->cn_nameptr)
    555 		panic ("relookup: bad len");
    556 	if (*cp != 0)
    557 		panic("relookup: not last component");
    558 	printf("{%s}: ", cnp->cn_nameptr);
    559 #endif
    560 
    561 	/*
    562 	 * Check for degenerate name (e.g. / or "")
    563 	 * which is a way of talking about a directory,
    564 	 * e.g. like "/." or ".".
    565 	 */
    566 	if (cnp->cn_nameptr[0] == '\0') {
    567 		if (dp->v_type != VDIR) {
    568 			error = ENOTDIR;
    569 			goto bad;
    570 		}
    571 		if (cnp->cn_nameiop != LOOKUP || wantparent) {
    572 			error = EISDIR;
    573 			goto bad;
    574 		}
    575 		if (!(cnp->cn_flags & LOCKLEAF))
    576 			VOP_UNLOCK(dp);
    577 		*vpp = dp;
    578 		if (cnp->cn_flags & SAVESTART)
    579 			panic("lookup: SAVESTART");
    580 		return (0);
    581 	}
    582 
    583 	if (cnp->cn_flags & ISDOTDOT)
    584 		panic ("relookup: lookup on dot-dot");
    585 
    586 	/*
    587 	 * We now have a segment name to search for, and a directory to search.
    588 	 */
    589 	if (error = VOP_LOOKUP(dp, vpp, cnp)) {
    590 #ifdef DIAGNOSTIC
    591 		if (*vpp != NULL)
    592 			panic("leaf should be empty");
    593 #endif
    594 		if (error != EJUSTRETURN)
    595 			goto bad;
    596 		/*
    597 		 * If creating and at end of pathname, then can consider
    598 		 * allowing file to be created.
    599 		 */
    600 		if (rdonly || (dvp->v_mount->mnt_flag & MNT_RDONLY)) {
    601 			error = EROFS;
    602 			goto bad;
    603 		}
    604 		/* ASSERT(dvp == ndp->ni_startdir) */
    605 		if (cnp->cn_flags & SAVESTART)
    606 			VREF(dvp);
    607 		/*
    608 		 * We return with ni_vp NULL to indicate that the entry
    609 		 * doesn't currently exist, leaving a pointer to the
    610 		 * (possibly locked) directory inode in ndp->ni_dvp.
    611 		 */
    612 		return (0);
    613 	}
    614 	dp = *vpp;
    615 
    616 #ifdef DIAGNOSTIC
    617 	/*
    618 	 * Check for symbolic link
    619 	 */
    620 	if (dp->v_type == VLNK && (cnp->cn_flags & FOLLOW))
    621 		panic ("relookup: symlink found.\n");
    622 #endif
    623 
    624 	/*
    625 	 * Check for read-only file systems.
    626 	 */
    627 	if (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME) {
    628 		/*
    629 		 * Disallow directory write attempts on read-only
    630 		 * file systems.
    631 		 */
    632 		if (rdonly || (dp->v_mount->mnt_flag & MNT_RDONLY) ||
    633 		    (wantparent &&
    634 		     (dvp->v_mount->mnt_flag & MNT_RDONLY))) {
    635 			error = EROFS;
    636 			goto bad2;
    637 		}
    638 	}
    639 	/* ASSERT(dvp == ndp->ni_startdir) */
    640 	if (cnp->cn_flags & SAVESTART)
    641 		VREF(dvp);
    642 
    643 	if (!wantparent)
    644 		vrele(dvp);
    645 	if ((cnp->cn_flags & LOCKLEAF) == 0)
    646 		VOP_UNLOCK(dp);
    647 	return (0);
    648 
    649 bad2:
    650 	if ((cnp->cn_flags & LOCKPARENT) && (cnp->cn_flags & ISLASTCN))
    651 		VOP_UNLOCK(dvp);
    652 	vrele(dvp);
    653 bad:
    654 	vput(dp);
    655 	*vpp = NULL;
    656 	return (error);
    657 }
    658