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