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