vfs_lookup.c revision 2112
11541Srgrimes/*
21541Srgrimes * Copyright (c) 1982, 1986, 1989, 1993
31541Srgrimes *	The Regents of the University of California.  All rights reserved.
41541Srgrimes * (c) UNIX System Laboratories, Inc.
51541Srgrimes * All or some portions of this file are derived from material licensed
61541Srgrimes * to the University of California by American Telephone and Telegraph
71541Srgrimes * Co. or Unix System Laboratories, Inc. and are reproduced herein with
81541Srgrimes * the permission of UNIX System Laboratories, Inc.
91541Srgrimes *
101541Srgrimes * Redistribution and use in source and binary forms, with or without
111541Srgrimes * modification, are permitted provided that the following conditions
121541Srgrimes * are met:
131541Srgrimes * 1. Redistributions of source code must retain the above copyright
141541Srgrimes *    notice, this list of conditions and the following disclaimer.
151541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
161541Srgrimes *    notice, this list of conditions and the following disclaimer in the
171541Srgrimes *    documentation and/or other materials provided with the distribution.
181541Srgrimes * 3. All advertising materials mentioning features or use of this software
191541Srgrimes *    must display the following acknowledgement:
201541Srgrimes *	This product includes software developed by the University of
211541Srgrimes *	California, Berkeley and its contributors.
221541Srgrimes * 4. Neither the name of the University nor the names of its contributors
231541Srgrimes *    may be used to endorse or promote products derived from this software
241541Srgrimes *    without specific prior written permission.
251541Srgrimes *
261541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
271541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
281541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
291541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
301541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
311541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
321541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
331541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
341541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
351541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
361541Srgrimes * SUCH DAMAGE.
371541Srgrimes *
381541Srgrimes *	@(#)vfs_lookup.c	8.4 (Berkeley) 2/16/94
392112Swollman * $Id: vfs_lookup.c,v 1.2 1994/08/02 07:43:25 davidg Exp $
401541Srgrimes */
411541Srgrimes
421541Srgrimes#include <sys/param.h>
432112Swollman#include <sys/systm.h>
441541Srgrimes#include <sys/syslimits.h>
451541Srgrimes#include <sys/time.h>
461541Srgrimes#include <sys/namei.h>
471541Srgrimes#include <sys/vnode.h>
481541Srgrimes#include <sys/mount.h>
491541Srgrimes#include <sys/errno.h>
501541Srgrimes#include <sys/malloc.h>
511541Srgrimes#include <sys/filedesc.h>
521541Srgrimes#include <sys/proc.h>
531541Srgrimes
541541Srgrimes#ifdef KTRACE
551541Srgrimes#include <sys/ktrace.h>
561541Srgrimes#endif
571541Srgrimes
581541Srgrimes/*
591541Srgrimes * Convert a pathname into a pointer to a locked inode.
601541Srgrimes *
611541Srgrimes * The FOLLOW flag is set when symbolic links are to be followed
621541Srgrimes * when they occur at the end of the name translation process.
631541Srgrimes * Symbolic links are always followed for all other pathname
641541Srgrimes * components other than the last.
651541Srgrimes *
661541Srgrimes * The segflg defines whether the name is to be copied from user
671541Srgrimes * space or kernel space.
681541Srgrimes *
691541Srgrimes * Overall outline of namei:
701541Srgrimes *
711541Srgrimes *	copy in name
721541Srgrimes *	get starting directory
731541Srgrimes *	while (!done && !error) {
741541Srgrimes *		call lookup to search path.
751541Srgrimes *		if symbolic link, massage name in buffer and continue
761541Srgrimes *	}
771541Srgrimes */
781541Srgrimesint
791541Srgrimesnamei(ndp)
801541Srgrimes	register struct nameidata *ndp;
811541Srgrimes{
821541Srgrimes	register struct filedesc *fdp;	/* pointer to file descriptor state */
831541Srgrimes	register char *cp;		/* pointer into pathname argument */
841541Srgrimes	register struct vnode *dp;	/* the directory we are searching */
851541Srgrimes	struct iovec aiov;		/* uio for reading symbolic links */
861541Srgrimes	struct uio auio;
871541Srgrimes	int error, linklen;
881541Srgrimes	struct componentname *cnp = &ndp->ni_cnd;
891541Srgrimes
901541Srgrimes	ndp->ni_cnd.cn_cred = ndp->ni_cnd.cn_proc->p_ucred;
911541Srgrimes#ifdef DIAGNOSTIC
921541Srgrimes	if (!cnp->cn_cred || !cnp->cn_proc)
931541Srgrimes		panic ("namei: bad cred/proc");
941541Srgrimes	if (cnp->cn_nameiop & (~OPMASK))
951541Srgrimes		panic ("namei: nameiop contaminated with flags");
961541Srgrimes	if (cnp->cn_flags & OPMASK)
971541Srgrimes		panic ("namei: flags contaminated with nameiops");
981541Srgrimes#endif
991541Srgrimes	fdp = cnp->cn_proc->p_fd;
1001541Srgrimes
1011541Srgrimes	/*
1021541Srgrimes	 * Get a buffer for the name to be translated, and copy the
1031541Srgrimes	 * name into the buffer.
1041541Srgrimes	 */
1051541Srgrimes	if ((cnp->cn_flags & HASBUF) == 0)
1061541Srgrimes		MALLOC(cnp->cn_pnbuf, caddr_t, MAXPATHLEN, M_NAMEI, M_WAITOK);
1071541Srgrimes	if (ndp->ni_segflg == UIO_SYSSPACE)
1081541Srgrimes		error = copystr(ndp->ni_dirp, cnp->cn_pnbuf,
1091541Srgrimes			    MAXPATHLEN, &ndp->ni_pathlen);
1101541Srgrimes	else
1111541Srgrimes		error = copyinstr(ndp->ni_dirp, cnp->cn_pnbuf,
1121541Srgrimes			    MAXPATHLEN, &ndp->ni_pathlen);
1131541Srgrimes	if (error) {
1141541Srgrimes		free(cnp->cn_pnbuf, M_NAMEI);
1151541Srgrimes		ndp->ni_vp = NULL;
1161541Srgrimes		return (error);
1171541Srgrimes	}
1181541Srgrimes	ndp->ni_loopcnt = 0;
1191541Srgrimes#ifdef KTRACE
1201541Srgrimes	if (KTRPOINT(cnp->cn_proc, KTR_NAMEI))
1211541Srgrimes		ktrnamei(cnp->cn_proc->p_tracep, cnp->cn_pnbuf);
1221541Srgrimes#endif
1231541Srgrimes
1241541Srgrimes	/*
1251541Srgrimes	 * Get starting point for the translation.
1261541Srgrimes	 */
1271541Srgrimes	if ((ndp->ni_rootdir = fdp->fd_rdir) == NULL)
1281541Srgrimes		ndp->ni_rootdir = rootvnode;
1291541Srgrimes	dp = fdp->fd_cdir;
1301541Srgrimes	VREF(dp);
1311541Srgrimes	for (;;) {
1321541Srgrimes		/*
1331541Srgrimes		 * Check if root directory should replace current directory.
1341541Srgrimes		 * Done at start of translation and after symbolic link.
1351541Srgrimes		 */
1361541Srgrimes		cnp->cn_nameptr = cnp->cn_pnbuf;
1371541Srgrimes		if (*(cnp->cn_nameptr) == '/') {
1381541Srgrimes			vrele(dp);
1391541Srgrimes			while (*(cnp->cn_nameptr) == '/') {
1401541Srgrimes				cnp->cn_nameptr++;
1411541Srgrimes				ndp->ni_pathlen--;
1421541Srgrimes			}
1431541Srgrimes			dp = ndp->ni_rootdir;
1441541Srgrimes			VREF(dp);
1451541Srgrimes		}
1461541Srgrimes		ndp->ni_startdir = dp;
1471541Srgrimes		if (error = lookup(ndp)) {
1481541Srgrimes			FREE(cnp->cn_pnbuf, M_NAMEI);
1491541Srgrimes			return (error);
1501541Srgrimes		}
1511541Srgrimes		/*
1521541Srgrimes		 * Check for symbolic link
1531541Srgrimes		 */
1541541Srgrimes		if ((cnp->cn_flags & ISSYMLINK) == 0) {
1551541Srgrimes			if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0)
1561541Srgrimes				FREE(cnp->cn_pnbuf, M_NAMEI);
1571541Srgrimes			else
1581541Srgrimes				cnp->cn_flags |= HASBUF;
1591541Srgrimes			return (0);
1601541Srgrimes		}
1611541Srgrimes		if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1)
1621541Srgrimes			VOP_UNLOCK(ndp->ni_dvp);
1631541Srgrimes		if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
1641541Srgrimes			error = ELOOP;
1651541Srgrimes			break;
1661541Srgrimes		}
1671541Srgrimes		if (ndp->ni_pathlen > 1)
1681541Srgrimes			MALLOC(cp, char *, MAXPATHLEN, M_NAMEI, M_WAITOK);
1691541Srgrimes		else
1701541Srgrimes			cp = cnp->cn_pnbuf;
1711541Srgrimes		aiov.iov_base = cp;
1721541Srgrimes		aiov.iov_len = MAXPATHLEN;
1731541Srgrimes		auio.uio_iov = &aiov;
1741541Srgrimes		auio.uio_iovcnt = 1;
1751541Srgrimes		auio.uio_offset = 0;
1761541Srgrimes		auio.uio_rw = UIO_READ;
1771541Srgrimes		auio.uio_segflg = UIO_SYSSPACE;
1781541Srgrimes		auio.uio_procp = (struct proc *)0;
1791541Srgrimes		auio.uio_resid = MAXPATHLEN;
1801541Srgrimes		if (error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred)) {
1811541Srgrimes			if (ndp->ni_pathlen > 1)
1821541Srgrimes				free(cp, M_NAMEI);
1831541Srgrimes			break;
1841541Srgrimes		}
1851541Srgrimes		linklen = MAXPATHLEN - auio.uio_resid;
1861541Srgrimes		if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
1871541Srgrimes			if (ndp->ni_pathlen > 1)
1881541Srgrimes				free(cp, M_NAMEI);
1891541Srgrimes			error = ENAMETOOLONG;
1901541Srgrimes			break;
1911541Srgrimes		}
1921541Srgrimes		if (ndp->ni_pathlen > 1) {
1931541Srgrimes			bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
1941541Srgrimes			FREE(cnp->cn_pnbuf, M_NAMEI);
1951541Srgrimes			cnp->cn_pnbuf = cp;
1961541Srgrimes		} else
1971541Srgrimes			cnp->cn_pnbuf[linklen] = '\0';
1981541Srgrimes		ndp->ni_pathlen += linklen;
1991541Srgrimes		vput(ndp->ni_vp);
2001541Srgrimes		dp = ndp->ni_dvp;
2011541Srgrimes	}
2021541Srgrimes	FREE(cnp->cn_pnbuf, M_NAMEI);
2031541Srgrimes	vrele(ndp->ni_dvp);
2041541Srgrimes	vput(ndp->ni_vp);
2051541Srgrimes	ndp->ni_vp = NULL;
2061541Srgrimes	return (error);
2071541Srgrimes}
2081541Srgrimes
2091541Srgrimes/*
2101541Srgrimes * Search a pathname.
2111541Srgrimes * This is a very central and rather complicated routine.
2121541Srgrimes *
2131541Srgrimes * The pathname is pointed to by ni_ptr and is of length ni_pathlen.
2141541Srgrimes * The starting directory is taken from ni_startdir. The pathname is
2151541Srgrimes * descended until done, or a symbolic link is encountered. The variable
2161541Srgrimes * ni_more is clear if the path is completed; it is set to one if a
2171541Srgrimes * symbolic link needing interpretation is encountered.
2181541Srgrimes *
2191541Srgrimes * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
2201541Srgrimes * whether the name is to be looked up, created, renamed, or deleted.
2211541Srgrimes * When CREATE, RENAME, or DELETE is specified, information usable in
2221541Srgrimes * creating, renaming, or deleting a directory entry may be calculated.
2231541Srgrimes * If flag has LOCKPARENT or'ed into it, the parent directory is returned
2241541Srgrimes * locked. If flag has WANTPARENT or'ed into it, the parent directory is
2251541Srgrimes * returned unlocked. Otherwise the parent directory is not returned. If
2261541Srgrimes * the target of the pathname exists and LOCKLEAF is or'ed into the flag
2271541Srgrimes * the target is returned locked, otherwise it is returned unlocked.
2281541Srgrimes * When creating or renaming and LOCKPARENT is specified, the target may not
2291541Srgrimes * be ".".  When deleting and LOCKPARENT is specified, the target may be ".".
2301541Srgrimes *
2311541Srgrimes * Overall outline of lookup:
2321541Srgrimes *
2331541Srgrimes * dirloop:
2341541Srgrimes *	identify next component of name at ndp->ni_ptr
2351541Srgrimes *	handle degenerate case where name is null string
2361541Srgrimes *	if .. and crossing mount points and on mounted filesys, find parent
2371541Srgrimes *	call VOP_LOOKUP routine for next component name
2381541Srgrimes *	    directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set
2391541Srgrimes *	    component vnode returned in ni_vp (if it exists), locked.
2401541Srgrimes *	if result vnode is mounted on and crossing mount points,
2411541Srgrimes *	    find mounted on vnode
2421541Srgrimes *	if more components of name, do next level at dirloop
2431541Srgrimes *	return the answer in ni_vp, locked if LOCKLEAF set
2441541Srgrimes *	    if LOCKPARENT set, return locked parent in ni_dvp
2451541Srgrimes *	    if WANTPARENT set, return unlocked parent in ni_dvp
2461541Srgrimes */
2471541Srgrimesint
2481541Srgrimeslookup(ndp)
2491541Srgrimes	register struct nameidata *ndp;
2501541Srgrimes{
2511541Srgrimes	register char *cp;		/* pointer into pathname argument */
2521541Srgrimes	register struct vnode *dp = 0;	/* the directory we are searching */
2531541Srgrimes	struct vnode *tdp;		/* saved dp */
2541541Srgrimes	struct mount *mp;		/* mount table entry */
2551541Srgrimes	int docache;			/* == 0 do not cache last component */
2561541Srgrimes	int wantparent;			/* 1 => wantparent or lockparent flag */
2571541Srgrimes	int rdonly;			/* lookup read-only flag bit */
2581541Srgrimes	int error = 0;
2591541Srgrimes	struct componentname *cnp = &ndp->ni_cnd;
2601541Srgrimes
2611541Srgrimes	/*
2621541Srgrimes	 * Setup: break out flag bits into variables.
2631541Srgrimes	 */
2641541Srgrimes	wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT);
2651541Srgrimes	docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE;
2661541Srgrimes	if (cnp->cn_nameiop == DELETE ||
2671541Srgrimes	    (wantparent && cnp->cn_nameiop != CREATE))
2681541Srgrimes		docache = 0;
2691541Srgrimes	rdonly = cnp->cn_flags & RDONLY;
2701541Srgrimes	ndp->ni_dvp = NULL;
2711541Srgrimes	cnp->cn_flags &= ~ISSYMLINK;
2721541Srgrimes	dp = ndp->ni_startdir;
2731541Srgrimes	ndp->ni_startdir = NULLVP;
2741541Srgrimes	VOP_LOCK(dp);
2751541Srgrimes
2761541Srgrimesdirloop:
2771541Srgrimes	/*
2781541Srgrimes	 * Search a new directory.
2791541Srgrimes	 *
2801541Srgrimes	 * The cn_hash value is for use by vfs_cache.
2811541Srgrimes	 * The last component of the filename is left accessible via
2821541Srgrimes	 * cnp->cn_nameptr for callers that need the name. Callers needing
2831541Srgrimes	 * the name set the SAVENAME flag. When done, they assume
2841541Srgrimes	 * responsibility for freeing the pathname buffer.
2851541Srgrimes	 */
2861541Srgrimes	cnp->cn_consume = 0;
2871541Srgrimes	cnp->cn_hash = 0;
2881541Srgrimes	for (cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++)
2891541Srgrimes		cnp->cn_hash += (unsigned char)*cp;
2901541Srgrimes	cnp->cn_namelen = cp - cnp->cn_nameptr;
2911541Srgrimes	if (cnp->cn_namelen > NAME_MAX) {
2921541Srgrimes		error = ENAMETOOLONG;
2931541Srgrimes		goto bad;
2941541Srgrimes	}
2951541Srgrimes#ifdef NAMEI_DIAGNOSTIC
2961541Srgrimes	{ char c = *cp;
2971541Srgrimes	*cp = '\0';
2981541Srgrimes	printf("{%s}: ", cnp->cn_nameptr);
2991541Srgrimes	*cp = c; }
3001541Srgrimes#endif
3011541Srgrimes	ndp->ni_pathlen -= cnp->cn_namelen;
3021541Srgrimes	ndp->ni_next = cp;
3031541Srgrimes	cnp->cn_flags |= MAKEENTRY;
3041541Srgrimes	if (*cp == '\0' && docache == 0)
3051541Srgrimes		cnp->cn_flags &= ~MAKEENTRY;
3061541Srgrimes	if (cnp->cn_namelen == 2 &&
3071541Srgrimes	    cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.')
3081541Srgrimes		cnp->cn_flags |= ISDOTDOT;
3091541Srgrimes	else
3101541Srgrimes		cnp->cn_flags &= ~ISDOTDOT;
3111541Srgrimes	if (*ndp->ni_next == 0)
3121541Srgrimes		cnp->cn_flags |= ISLASTCN;
3131541Srgrimes	else
3141541Srgrimes		cnp->cn_flags &= ~ISLASTCN;
3151541Srgrimes
3161541Srgrimes
3171541Srgrimes	/*
3181541Srgrimes	 * Check for degenerate name (e.g. / or "")
3191541Srgrimes	 * which is a way of talking about a directory,
3201541Srgrimes	 * e.g. like "/." or ".".
3211541Srgrimes	 */
3221541Srgrimes	if (cnp->cn_nameptr[0] == '\0') {
3231541Srgrimes		if (cnp->cn_nameiop != LOOKUP) {
3241541Srgrimes			error = EISDIR;
3251541Srgrimes			goto bad;
3261541Srgrimes		}
3271541Srgrimes		if (dp->v_type != VDIR) {
3281541Srgrimes			error = ENOTDIR;
3291541Srgrimes			goto bad;
3301541Srgrimes		}
3311541Srgrimes		if (wantparent) {
3321541Srgrimes			ndp->ni_dvp = dp;
3331541Srgrimes			VREF(dp);
3341541Srgrimes		}
3351541Srgrimes		ndp->ni_vp = dp;
3361541Srgrimes		if (!(cnp->cn_flags & (LOCKPARENT | LOCKLEAF)))
3371541Srgrimes			VOP_UNLOCK(dp);
3381541Srgrimes		if (cnp->cn_flags & SAVESTART)
3391541Srgrimes			panic("lookup: SAVESTART");
3401541Srgrimes		return (0);
3411541Srgrimes	}
3421541Srgrimes
3431541Srgrimes	/*
3441541Srgrimes	 * Handle "..": two special cases.
3451541Srgrimes	 * 1. If at root directory (e.g. after chroot)
3461541Srgrimes	 *    or at absolute root directory
3471541Srgrimes	 *    then ignore it so can't get out.
3481541Srgrimes	 * 2. If this vnode is the root of a mounted
3491541Srgrimes	 *    filesystem, then replace it with the
3501541Srgrimes	 *    vnode which was mounted on so we take the
3511541Srgrimes	 *    .. in the other file system.
3521541Srgrimes	 */
3531541Srgrimes	if (cnp->cn_flags & ISDOTDOT) {
3541541Srgrimes		for (;;) {
3551541Srgrimes			if (dp == ndp->ni_rootdir || dp == rootvnode) {
3561541Srgrimes				ndp->ni_dvp = dp;
3571541Srgrimes				ndp->ni_vp = dp;
3581541Srgrimes				VREF(dp);
3591541Srgrimes				goto nextname;
3601541Srgrimes			}
3611541Srgrimes			if ((dp->v_flag & VROOT) == 0 ||
3621541Srgrimes			    (cnp->cn_flags & NOCROSSMOUNT))
3631541Srgrimes				break;
3641541Srgrimes			tdp = dp;
3651541Srgrimes			dp = dp->v_mount->mnt_vnodecovered;
3661541Srgrimes			vput(tdp);
3671541Srgrimes			VREF(dp);
3681541Srgrimes			VOP_LOCK(dp);
3691541Srgrimes		}
3701541Srgrimes	}
3711541Srgrimes
3721541Srgrimes	/*
3731541Srgrimes	 * We now have a segment name to search for, and a directory to search.
3741541Srgrimes	 */
3751541Srgrimesunionlookup:
3761541Srgrimes	ndp->ni_dvp = dp;
3771541Srgrimes	if (error = VOP_LOOKUP(dp, &ndp->ni_vp, cnp)) {
3781541Srgrimes#ifdef DIAGNOSTIC
3791541Srgrimes		if (ndp->ni_vp != NULL)
3801541Srgrimes			panic("leaf should be empty");
3811541Srgrimes#endif
3821541Srgrimes#ifdef NAMEI_DIAGNOSTIC
3831541Srgrimes		printf("not found\n");
3841541Srgrimes#endif
3851541Srgrimes		if ((error == ENOENT) &&
3861541Srgrimes		    (dp->v_flag & VROOT) &&
3871541Srgrimes		    (dp->v_mount->mnt_flag & MNT_UNION)) {
3881541Srgrimes			tdp = dp;
3891541Srgrimes			dp = dp->v_mount->mnt_vnodecovered;
3901541Srgrimes			vput(tdp);
3911541Srgrimes			VREF(dp);
3921541Srgrimes			VOP_LOCK(dp);
3931541Srgrimes			goto unionlookup;
3941541Srgrimes		}
3951541Srgrimes
3961541Srgrimes		if (error != EJUSTRETURN)
3971541Srgrimes			goto bad;
3981541Srgrimes		/*
3991541Srgrimes		 * If creating and at end of pathname, then can consider
4001541Srgrimes		 * allowing file to be created.
4011541Srgrimes		 */
4021541Srgrimes		if (rdonly || (ndp->ni_dvp->v_mount->mnt_flag & MNT_RDONLY)) {
4031541Srgrimes			error = EROFS;
4041541Srgrimes			goto bad;
4051541Srgrimes		}
4061541Srgrimes		/*
4071541Srgrimes		 * We return with ni_vp NULL to indicate that the entry
4081541Srgrimes		 * doesn't currently exist, leaving a pointer to the
4091541Srgrimes		 * (possibly locked) directory inode in ndp->ni_dvp.
4101541Srgrimes		 */
4111541Srgrimes		if (cnp->cn_flags & SAVESTART) {
4121541Srgrimes			ndp->ni_startdir = ndp->ni_dvp;
4131541Srgrimes			VREF(ndp->ni_startdir);
4141541Srgrimes		}
4151541Srgrimes		return (0);
4161541Srgrimes	}
4171541Srgrimes#ifdef NAMEI_DIAGNOSTIC
4181541Srgrimes	printf("found\n");
4191541Srgrimes#endif
4201541Srgrimes
4211541Srgrimes	/*
4221541Srgrimes	 * Take into account any additional components consumed by
4231541Srgrimes	 * the underlying filesystem.
4241541Srgrimes	 */
4251541Srgrimes	if (cnp->cn_consume > 0) {
4261541Srgrimes		cnp->cn_nameptr += cnp->cn_consume;
4271541Srgrimes		ndp->ni_next += cnp->cn_consume;
4281541Srgrimes		ndp->ni_pathlen -= cnp->cn_consume;
4291541Srgrimes		cnp->cn_consume = 0;
4301541Srgrimes	}
4311541Srgrimes
4321541Srgrimes	dp = ndp->ni_vp;
4331541Srgrimes	/*
4341541Srgrimes	 * Check for symbolic link
4351541Srgrimes	 */
4361541Srgrimes	if ((dp->v_type == VLNK) &&
4371541Srgrimes	    ((cnp->cn_flags & FOLLOW) || *ndp->ni_next == '/')) {
4381541Srgrimes		cnp->cn_flags |= ISSYMLINK;
4391541Srgrimes		return (0);
4401541Srgrimes	}
4411541Srgrimes
4421541Srgrimes	/*
4431541Srgrimes	 * Check to see if the vnode has been mounted on;
4441541Srgrimes	 * if so find the root of the mounted file system.
4451541Srgrimes	 */
4461541Srgrimes	while (dp->v_type == VDIR && (mp = dp->v_mountedhere) &&
4471541Srgrimes	       (cnp->cn_flags & NOCROSSMOUNT) == 0) {
4481541Srgrimes		if (mp->mnt_flag & MNT_MLOCK) {
4491541Srgrimes			mp->mnt_flag |= MNT_MWAIT;
4501541Srgrimes			sleep((caddr_t)mp, PVFS);
4511541Srgrimes			continue;
4521541Srgrimes		}
4531541Srgrimes		if (error = VFS_ROOT(dp->v_mountedhere, &tdp))
4541541Srgrimes			goto bad2;
4551541Srgrimes		vput(dp);
4561541Srgrimes		ndp->ni_vp = dp = tdp;
4571541Srgrimes	}
4581541Srgrimes
4591541Srgrimesnextname:
4601541Srgrimes	/*
4611541Srgrimes	 * Not a symbolic link.  If more pathname,
4621541Srgrimes	 * continue at next component, else return.
4631541Srgrimes	 */
4641541Srgrimes	if (*ndp->ni_next == '/') {
4651541Srgrimes		cnp->cn_nameptr = ndp->ni_next;
4661541Srgrimes		while (*cnp->cn_nameptr == '/') {
4671541Srgrimes			cnp->cn_nameptr++;
4681541Srgrimes			ndp->ni_pathlen--;
4691541Srgrimes		}
4701541Srgrimes		vrele(ndp->ni_dvp);
4711541Srgrimes		goto dirloop;
4721541Srgrimes	}
4731541Srgrimes	/*
4741541Srgrimes	 * Check for read-only file systems.
4751541Srgrimes	 */
4761541Srgrimes	if (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME) {
4771541Srgrimes		/*
4781541Srgrimes		 * Disallow directory write attempts on read-only
4791541Srgrimes		 * file systems.
4801541Srgrimes		 */
4811541Srgrimes		if (rdonly || (dp->v_mount->mnt_flag & MNT_RDONLY) ||
4821541Srgrimes		    (wantparent &&
4831541Srgrimes		     (ndp->ni_dvp->v_mount->mnt_flag & MNT_RDONLY))) {
4841541Srgrimes			error = EROFS;
4851541Srgrimes			goto bad2;
4861541Srgrimes		}
4871541Srgrimes	}
4881541Srgrimes	if (cnp->cn_flags & SAVESTART) {
4891541Srgrimes		ndp->ni_startdir = ndp->ni_dvp;
4901541Srgrimes		VREF(ndp->ni_startdir);
4911541Srgrimes	}
4921541Srgrimes	if (!wantparent)
4931541Srgrimes		vrele(ndp->ni_dvp);
4941541Srgrimes	if ((cnp->cn_flags & LOCKLEAF) == 0)
4951541Srgrimes		VOP_UNLOCK(dp);
4961541Srgrimes	return (0);
4971541Srgrimes
4981541Srgrimesbad2:
4991541Srgrimes	if ((cnp->cn_flags & LOCKPARENT) && *ndp->ni_next == '\0')
5001541Srgrimes		VOP_UNLOCK(ndp->ni_dvp);
5011541Srgrimes	vrele(ndp->ni_dvp);
5021541Srgrimesbad:
5031541Srgrimes	vput(dp);
5041541Srgrimes	ndp->ni_vp = NULL;
5051541Srgrimes	return (error);
5061541Srgrimes}
5071541Srgrimes
5081541Srgrimes
509