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