Home | History | Annotate | Line # | Download | only in kern
vfs_lookup.c revision 1.12
      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  *	from: @(#)vfs_lookup.c	8.4 (Berkeley) 2/16/94
     39  *	$Id: vfs_lookup.c,v 1.12 1994/06/08 11:28:55 mycroft Exp $
     40  */
     41 
     42 #include <sys/param.h>
     43 #include <sys/syslimits.h>
     44 #include <sys/time.h>
     45 #include <sys/namei.h>
     46 #include <sys/vnode.h>
     47 #include <sys/mount.h>
     48 #include <sys/errno.h>
     49 #include <sys/malloc.h>
     50 #include <sys/filedesc.h>
     51 #include <sys/proc.h>
     52 
     53 #ifdef KTRACE
     54 #include <sys/ktrace.h>
     55 #endif
     56 
     57 /*
     58  * Convert a pathname into a pointer to a locked inode.
     59  *
     60  * The FOLLOW flag is set when symbolic links are to be followed
     61  * when they occur at the end of the name translation process.
     62  * Symbolic links are always followed for all other pathname
     63  * components other than the last.
     64  *
     65  * The segflg defines whether the name is to be copied from user
     66  * space or kernel space.
     67  *
     68  * Overall outline of namei:
     69  *
     70  *	copy in name
     71  *	get starting directory
     72  *	while (!done && !error) {
     73  *		call lookup to search path.
     74  *		if symbolic link, massage name in buffer and continue
     75  *	}
     76  */
     77 int
     78 namei(ndp)
     79 	register struct nameidata *ndp;
     80 {
     81 	register struct filedesc *fdp;	/* pointer to file descriptor state */
     82 	register char *cp;		/* pointer into pathname argument */
     83 	register struct vnode *dp;	/* the directory we are searching */
     84 	struct iovec aiov;		/* uio for reading symbolic links */
     85 	struct uio auio;
     86 	int error, linklen;
     87 	struct componentname *cnp = &ndp->ni_cnd;
     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);
    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 
    260 	/*
    261 	 * Setup: break out flag bits into variables.
    262 	 */
    263 	wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT);
    264 	docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE;
    265 	if (cnp->cn_nameiop == DELETE ||
    266 	    (wantparent && cnp->cn_nameiop != CREATE))
    267 		docache = 0;
    268 	rdonly = cnp->cn_flags & RDONLY;
    269 	ndp->ni_dvp = NULL;
    270 	cnp->cn_flags &= ~ISSYMLINK;
    271 	dp = ndp->ni_startdir;
    272 	ndp->ni_startdir = NULLVP;
    273 	VOP_LOCK(dp);
    274 
    275 dirloop:
    276 	/*
    277 	 * Search a new directory.
    278 	 *
    279 	 * The cn_hash value is for use by vfs_cache.
    280 	 * The last component of the filename is left accessible via
    281 	 * cnp->cn_nameptr for callers that need the name. Callers needing
    282 	 * the name set the SAVENAME flag. When done, they assume
    283 	 * responsibility for freeing the pathname buffer.
    284 	 */
    285 	cnp->cn_consume = 0;
    286 	cnp->cn_hash = 0;
    287 	for (cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++)
    288 		cnp->cn_hash += (unsigned char)*cp;
    289 	cnp->cn_namelen = cp - cnp->cn_nameptr;
    290 	if (cnp->cn_namelen > NAME_MAX) {
    291 		error = ENAMETOOLONG;
    292 		goto bad;
    293 	}
    294 #ifdef NAMEI_DIAGNOSTIC
    295 	{ char c = *cp;
    296 	*cp = '\0';
    297 	printf("{%s}: ", cnp->cn_nameptr);
    298 	*cp = c; }
    299 #endif
    300 	ndp->ni_pathlen -= cnp->cn_namelen;
    301 	ndp->ni_next = cp;
    302 	cnp->cn_flags |= MAKEENTRY;
    303 	if (*cp == '\0' && docache == 0)
    304 		cnp->cn_flags &= ~MAKEENTRY;
    305 	if (cnp->cn_namelen == 2 &&
    306 	    cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.')
    307 		cnp->cn_flags |= ISDOTDOT;
    308 	else
    309 		cnp->cn_flags &= ~ISDOTDOT;
    310 	if (*ndp->ni_next == 0)
    311 		cnp->cn_flags |= ISLASTCN;
    312 	else
    313 		cnp->cn_flags &= ~ISLASTCN;
    314 
    315 
    316 	/*
    317 	 * Check for degenerate name (e.g. / or "")
    318 	 * which is a way of talking about a directory,
    319 	 * e.g. like "/." or ".".
    320 	 */
    321 	if (cnp->cn_nameptr[0] == '\0') {
    322 		if (dp->v_type != VDIR) {
    323 			error = ENOTDIR;
    324 			goto bad;
    325 		}
    326 		if (cnp->cn_nameiop != LOOKUP) {
    327 			error = EISDIR;
    328 			goto bad;
    329 		}
    330 		if (wantparent) {
    331 			ndp->ni_dvp = dp;
    332 			VREF(dp);
    333 		}
    334 		ndp->ni_vp = dp;
    335 		if (!(cnp->cn_flags & (LOCKPARENT | LOCKLEAF)))
    336 			VOP_UNLOCK(dp);
    337 		if (cnp->cn_flags & SAVESTART)
    338 			panic("lookup: SAVESTART");
    339 		return (0);
    340 	}
    341 
    342 	/*
    343 	 * Handle "..": two special cases.
    344 	 * 1. If at root directory (e.g. after chroot)
    345 	 *    or at absolute root directory
    346 	 *    then ignore it so can't get out.
    347 	 * 2. If this vnode is the root of a mounted
    348 	 *    filesystem, then replace it with the
    349 	 *    vnode which was mounted on so we take the
    350 	 *    .. in the other file system.
    351 	 */
    352 	if (cnp->cn_flags & ISDOTDOT) {
    353 		for (;;) {
    354 			if (dp == ndp->ni_rootdir || dp == rootvnode) {
    355 				ndp->ni_dvp = dp;
    356 				ndp->ni_vp = dp;
    357 				VREF(dp);
    358 				goto nextname;
    359 			}
    360 			if ((dp->v_flag & VROOT) == 0 ||
    361 			    (cnp->cn_flags & NOCROSSMOUNT))
    362 				break;
    363 			tdp = dp;
    364 			dp = dp->v_mount->mnt_vnodecovered;
    365 			vput(tdp);
    366 			VREF(dp);
    367 			VOP_LOCK(dp);
    368 		}
    369 	}
    370 
    371 	/*
    372 	 * We now have a segment name to search for, and a directory to search.
    373 	 */
    374 unionlookup:
    375 	ndp->ni_dvp = dp;
    376 	if (error = VOP_LOOKUP(dp, &ndp->ni_vp, cnp)) {
    377 #ifdef DIAGNOSTIC
    378 		if (ndp->ni_vp != NULL)
    379 			panic("leaf should be empty");
    380 #endif
    381 #ifdef NAMEI_DIAGNOSTIC
    382 		printf("not found\n");
    383 #endif
    384 		if ((error == ENOENT) &&
    385 		    (dp->v_flag & VROOT) &&
    386 		    (dp->v_mount->mnt_flag & MNT_UNION)) {
    387 			tdp = dp;
    388 			dp = dp->v_mount->mnt_vnodecovered;
    389 			vput(tdp);
    390 			VREF(dp);
    391 			VOP_LOCK(dp);
    392 			goto unionlookup;
    393 		}
    394 
    395 		if (error != EJUSTRETURN)
    396 			goto bad;
    397 		/*
    398 		 * If creating and at end of pathname, then can consider
    399 		 * allowing file to be created.
    400 		 */
    401 		if (rdonly || (ndp->ni_dvp->v_mount->mnt_flag & MNT_RDONLY)) {
    402 			error = EROFS;
    403 			goto bad;
    404 		}
    405 		/*
    406 		 * We return with ni_vp NULL to indicate that the entry
    407 		 * doesn't currently exist, leaving a pointer to the
    408 		 * (possibly locked) directory inode in ndp->ni_dvp.
    409 		 */
    410 		if (cnp->cn_flags & SAVESTART) {
    411 			ndp->ni_startdir = ndp->ni_dvp;
    412 			VREF(ndp->ni_startdir);
    413 		}
    414 		return (0);
    415 	}
    416 #ifdef NAMEI_DIAGNOSTIC
    417 	printf("found\n");
    418 #endif
    419 
    420 	/*
    421 	 * Take into account any additional components consumed by
    422 	 * the underlying filesystem.
    423 	 */
    424 	if (cnp->cn_consume > 0) {
    425 		cnp->cn_nameptr += cnp->cn_consume;
    426 		ndp->ni_next += cnp->cn_consume;
    427 		ndp->ni_pathlen -= cnp->cn_consume;
    428 		cnp->cn_consume = 0;
    429 	}
    430 
    431 	dp = ndp->ni_vp;
    432 	/*
    433 	 * Check for symbolic link
    434 	 */
    435 	if ((dp->v_type == VLNK) &&
    436 	    ((cnp->cn_flags & FOLLOW) || *ndp->ni_next == '/')) {
    437 		cnp->cn_flags |= ISSYMLINK;
    438 		return (0);
    439 	}
    440 
    441 	/*
    442 	 * Check to see if the vnode has been mounted on;
    443 	 * if so find the root of the mounted file system.
    444 	 */
    445 	while (dp->v_type == VDIR && (mp = dp->v_mountedhere) &&
    446 	       (cnp->cn_flags & NOCROSSMOUNT) == 0) {
    447 		if (mp->mnt_flag & MNT_MLOCK) {
    448 			mp->mnt_flag |= MNT_MWAIT;
    449 			sleep((caddr_t)mp, PVFS);
    450 			continue;
    451 		}
    452 		if (error = VFS_ROOT(dp->v_mountedhere, &tdp))
    453 			goto bad2;
    454 		vput(dp);
    455 		ndp->ni_vp = dp = tdp;
    456 	}
    457 
    458 nextname:
    459 	/*
    460 	 * Not a symbolic link.  If more pathname,
    461 	 * continue at next component, else return.
    462 	 */
    463 	if (*ndp->ni_next == '/') {
    464 		cnp->cn_nameptr = ndp->ni_next;
    465 		while (*cnp->cn_nameptr == '/') {
    466 			cnp->cn_nameptr++;
    467 			ndp->ni_pathlen--;
    468 		}
    469 		vrele(ndp->ni_dvp);
    470 		goto dirloop;
    471 	}
    472 	/*
    473 	 * Check for read-only file systems.
    474 	 */
    475 	if (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME) {
    476 		/*
    477 		 * Disallow directory write attempts on read-only
    478 		 * file systems.
    479 		 */
    480 		if (rdonly || (dp->v_mount->mnt_flag & MNT_RDONLY) ||
    481 		    (wantparent &&
    482 		     (ndp->ni_dvp->v_mount->mnt_flag & MNT_RDONLY))) {
    483 			error = EROFS;
    484 			goto bad2;
    485 		}
    486 	}
    487 	if (cnp->cn_flags & SAVESTART) {
    488 		ndp->ni_startdir = ndp->ni_dvp;
    489 		VREF(ndp->ni_startdir);
    490 	}
    491 	if (!wantparent)
    492 		vrele(ndp->ni_dvp);
    493 	if ((cnp->cn_flags & LOCKLEAF) == 0)
    494 		VOP_UNLOCK(dp);
    495 	return (0);
    496 
    497 bad2:
    498 	if ((cnp->cn_flags & LOCKPARENT) && *ndp->ni_next == '\0')
    499 		VOP_UNLOCK(ndp->ni_dvp);
    500 	vrele(ndp->ni_dvp);
    501 bad:
    502 	vput(dp);
    503 	ndp->ni_vp = NULL;
    504 	return (error);
    505 }
    506 
    507 /*
    508  * Reacquire a path name component.
    509  */
    510 int
    511 relookup(dvp, vpp, cnp)
    512 	struct vnode *dvp, **vpp;
    513 	struct componentname *cnp;
    514 {
    515 	register struct vnode *dp = 0;	/* the directory we are searching */
    516 	int docache;			/* == 0 do not cache last component */
    517 	int wantparent;			/* 1 => wantparent or lockparent flag */
    518 	int rdonly;			/* lookup read-only flag bit */
    519 	int error = 0;
    520 #ifdef NAMEI_DIAGNOSTIC
    521 	int newhash;			/* DEBUG: check name hash */
    522 	char *cp;			/* DEBUG: check name ptr/len */
    523 #endif
    524 
    525 	/*
    526 	 * Setup: break out flag bits into variables.
    527 	 */
    528 	wantparent = cnp->cn_flags & (LOCKPARENT|WANTPARENT);
    529 	docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE;
    530 	if (cnp->cn_nameiop == DELETE ||
    531 	    (wantparent && cnp->cn_nameiop != CREATE))
    532 		docache = 0;
    533 	rdonly = cnp->cn_flags & RDONLY;
    534 	cnp->cn_flags &= ~ISSYMLINK;
    535 	dp = dvp;
    536 	VOP_LOCK(dp);
    537 
    538 /* dirloop: */
    539 	/*
    540 	 * Search a new directory.
    541 	 *
    542 	 * The cn_hash value is for use by vfs_cache.
    543 	 * The last component of the filename is left accessible via
    544 	 * cnp->cn_nameptr for callers that need the name. Callers needing
    545 	 * the name set the SAVENAME flag. When done, they assume
    546 	 * responsibility for freeing the pathname buffer.
    547 	 */
    548 #ifdef NAMEI_DIAGNOSTIC
    549 	for (newhash = 0, cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++)
    550 		newhash += (unsigned char)*cp;
    551 	if (newhash != cnp->cn_hash)
    552 		panic("relookup: bad hash");
    553 	if (cnp->cn_namelen != cp - cnp->cn_nameptr)
    554 		panic ("relookup: bad len");
    555 	if (*cp != 0)
    556 		panic("relookup: not last component");
    557 	printf("{%s}: ", cnp->cn_nameptr);
    558 #endif
    559 
    560 	/*
    561 	 * Check for degenerate name (e.g. / or "")
    562 	 * which is a way of talking about a directory,
    563 	 * e.g. like "/." or ".".
    564 	 */
    565 	if (cnp->cn_nameptr[0] == '\0') {
    566 		if (dp->v_type != VDIR) {
    567 			error = ENOTDIR;
    568 			goto bad;
    569 		}
    570 		if (cnp->cn_nameiop != LOOKUP || wantparent) {
    571 			error = EISDIR;
    572 			goto bad;
    573 		}
    574 		if (!(cnp->cn_flags & LOCKLEAF))
    575 			VOP_UNLOCK(dp);
    576 		*vpp = dp;
    577 		if (cnp->cn_flags & SAVESTART)
    578 			panic("lookup: SAVESTART");
    579 		return (0);
    580 	}
    581 
    582 	if (cnp->cn_flags & ISDOTDOT)
    583 		panic ("relookup: lookup on dot-dot");
    584 
    585 	/*
    586 	 * We now have a segment name to search for, and a directory to search.
    587 	 */
    588 	if (error = VOP_LOOKUP(dp, vpp, cnp)) {
    589 #ifdef DIAGNOSTIC
    590 		if (*vpp != NULL)
    591 			panic("leaf should be empty");
    592 #endif
    593 		if (error != EJUSTRETURN)
    594 			goto bad;
    595 		/*
    596 		 * If creating and at end of pathname, then can consider
    597 		 * allowing file to be created.
    598 		 */
    599 		if (rdonly || (dvp->v_mount->mnt_flag & MNT_RDONLY)) {
    600 			error = EROFS;
    601 			goto bad;
    602 		}
    603 		/* ASSERT(dvp == ndp->ni_startdir) */
    604 		if (cnp->cn_flags & SAVESTART)
    605 			VREF(dvp);
    606 		/*
    607 		 * We return with ni_vp NULL to indicate that the entry
    608 		 * doesn't currently exist, leaving a pointer to the
    609 		 * (possibly locked) directory inode in ndp->ni_dvp.
    610 		 */
    611 		return (0);
    612 	}
    613 	dp = *vpp;
    614 
    615 #ifdef DIAGNOSTIC
    616 	/*
    617 	 * Check for symbolic link
    618 	 */
    619 	if (dp->v_type == VLNK && (cnp->cn_flags & FOLLOW))
    620 		panic ("relookup: symlink found.\n");
    621 #endif
    622 
    623 	/*
    624 	 * Check for read-only file systems.
    625 	 */
    626 	if (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME) {
    627 		/*
    628 		 * Disallow directory write attempts on read-only
    629 		 * file systems.
    630 		 */
    631 		if (rdonly || (dp->v_mount->mnt_flag & MNT_RDONLY) ||
    632 		    (wantparent &&
    633 		     (dvp->v_mount->mnt_flag & MNT_RDONLY))) {
    634 			error = EROFS;
    635 			goto bad2;
    636 		}
    637 	}
    638 	/* ASSERT(dvp == ndp->ni_startdir) */
    639 	if (cnp->cn_flags & SAVESTART)
    640 		VREF(dvp);
    641 
    642 	if (!wantparent)
    643 		vrele(dvp);
    644 	if ((cnp->cn_flags & LOCKLEAF) == 0)
    645 		VOP_UNLOCK(dp);
    646 	return (0);
    647 
    648 bad2:
    649 	if ((cnp->cn_flags & LOCKPARENT) && (cnp->cn_flags & ISLASTCN))
    650 		VOP_UNLOCK(dvp);
    651 	vrele(dvp);
    652 bad:
    653 	vput(dp);
    654 	*vpp = NULL;
    655 	return (error);
    656 }
    657