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