vfs_lookup.c revision 32286
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
3932286Sdyson * $Id: vfs_lookup.c,v 1.22 1997/12/29 00:22:38 dyson Exp $
401541Srgrimes */
411541Srgrimes
4213203Swollman#include "opt_ktrace.h"
4313203Swollman
441541Srgrimes#include <sys/param.h>
452112Swollman#include <sys/systm.h>
461541Srgrimes#include <sys/namei.h>
471541Srgrimes#include <sys/vnode.h>
481541Srgrimes#include <sys/mount.h>
491541Srgrimes#include <sys/filedesc.h>
501541Srgrimes#include <sys/proc.h>
511541Srgrimes
521541Srgrimes#ifdef KTRACE
531541Srgrimes#include <sys/ktrace.h>
541541Srgrimes#endif
551541Srgrimes
5632011Sbde#include <vm/vm_zone.h>
5732011Sbde
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;
8922521Sdyson	struct proc *p = cnp->cn_proc;
901541Srgrimes
911541Srgrimes	ndp->ni_cnd.cn_cred = ndp->ni_cnd.cn_proc->p_ucred;
921541Srgrimes#ifdef DIAGNOSTIC
931541Srgrimes	if (!cnp->cn_cred || !cnp->cn_proc)
941541Srgrimes		panic ("namei: bad cred/proc");
951541Srgrimes	if (cnp->cn_nameiop & (~OPMASK))
961541Srgrimes		panic ("namei: nameiop contaminated with flags");
971541Srgrimes	if (cnp->cn_flags & OPMASK)
981541Srgrimes		panic ("namei: flags contaminated with nameiops");
991541Srgrimes#endif
1001541Srgrimes	fdp = cnp->cn_proc->p_fd;
1011541Srgrimes
1021541Srgrimes	/*
1031541Srgrimes	 * Get a buffer for the name to be translated, and copy the
1041541Srgrimes	 * name into the buffer.
1051541Srgrimes	 */
1061541Srgrimes	if ((cnp->cn_flags & HASBUF) == 0)
10729653Sdyson		cnp->cn_pnbuf = zalloc(namei_zone);
1081541Srgrimes	if (ndp->ni_segflg == UIO_SYSSPACE)
1091541Srgrimes		error = copystr(ndp->ni_dirp, cnp->cn_pnbuf,
1102142Sdg			    MAXPATHLEN, (u_int *)&ndp->ni_pathlen);
1111541Srgrimes	else
1121541Srgrimes		error = copyinstr(ndp->ni_dirp, cnp->cn_pnbuf,
1132142Sdg			    MAXPATHLEN, (u_int *)&ndp->ni_pathlen);
11420069Sbde
11520069Sbde	/*
11620069Sbde	 * Don't allow empty pathnames.
11720069Sbde	 */
11820069Sbde	if (!error && *cnp->cn_pnbuf == '\0')
11920069Sbde		error = ENOENT;
12020069Sbde
1211541Srgrimes	if (error) {
12229653Sdyson		zfree(namei_zone, cnp->cn_pnbuf);
1231541Srgrimes		ndp->ni_vp = NULL;
1241541Srgrimes		return (error);
1251541Srgrimes	}
1261541Srgrimes	ndp->ni_loopcnt = 0;
1271541Srgrimes#ifdef KTRACE
1281541Srgrimes	if (KTRPOINT(cnp->cn_proc, KTR_NAMEI))
1291541Srgrimes		ktrnamei(cnp->cn_proc->p_tracep, cnp->cn_pnbuf);
1301541Srgrimes#endif
1311541Srgrimes
1321541Srgrimes	/*
1331541Srgrimes	 * Get starting point for the translation.
1341541Srgrimes	 */
1351541Srgrimes	if ((ndp->ni_rootdir = fdp->fd_rdir) == NULL)
1361541Srgrimes		ndp->ni_rootdir = rootvnode;
1371541Srgrimes	dp = fdp->fd_cdir;
1381541Srgrimes	VREF(dp);
1391541Srgrimes	for (;;) {
1401541Srgrimes		/*
1411541Srgrimes		 * Check if root directory should replace current directory.
1421541Srgrimes		 * Done at start of translation and after symbolic link.
1431541Srgrimes		 */
1441541Srgrimes		cnp->cn_nameptr = cnp->cn_pnbuf;
1451541Srgrimes		if (*(cnp->cn_nameptr) == '/') {
1461541Srgrimes			vrele(dp);
1471541Srgrimes			while (*(cnp->cn_nameptr) == '/') {
1481541Srgrimes				cnp->cn_nameptr++;
1491541Srgrimes				ndp->ni_pathlen--;
1501541Srgrimes			}
1511541Srgrimes			dp = ndp->ni_rootdir;
1521541Srgrimes			VREF(dp);
1531541Srgrimes		}
1541541Srgrimes		ndp->ni_startdir = dp;
1553148Sphk		error = lookup(ndp);
1563148Sphk		if (error) {
15729653Sdyson			zfree(namei_zone, cnp->cn_pnbuf);
1581541Srgrimes			return (error);
1591541Srgrimes		}
1601541Srgrimes		/*
1611541Srgrimes		 * Check for symbolic link
1621541Srgrimes		 */
1631541Srgrimes		if ((cnp->cn_flags & ISSYMLINK) == 0) {
1641541Srgrimes			if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0)
16529653Sdyson				zfree(namei_zone, cnp->cn_pnbuf);
1661541Srgrimes			else
1671541Srgrimes				cnp->cn_flags |= HASBUF;
16832286Sdyson
16932286Sdyson			if (ndp->ni_vp && ndp->ni_vp->v_type == VREG &&
17032286Sdyson				(cnp->cn_nameiop != DELETE) &&
17132286Sdyson				((cnp->cn_flags & (NOOBJ|LOCKLEAF)) == LOCKLEAF))
17232286Sdyson				vfs_object_create(ndp->ni_vp,
17332286Sdyson					ndp->ni_cnd.cn_proc, ndp->ni_cnd.cn_cred, 1);
17432286Sdyson
1751541Srgrimes			return (0);
1761541Srgrimes		}
1771541Srgrimes		if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1)
17822521Sdyson			VOP_UNLOCK(ndp->ni_dvp, 0, p);
1791541Srgrimes		if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
1801541Srgrimes			error = ELOOP;
1811541Srgrimes			break;
1821541Srgrimes		}
1831541Srgrimes		if (ndp->ni_pathlen > 1)
18429653Sdyson			cp = zalloc(namei_zone);
1851541Srgrimes		else
1861541Srgrimes			cp = cnp->cn_pnbuf;
1871541Srgrimes		aiov.iov_base = cp;
1881541Srgrimes		aiov.iov_len = MAXPATHLEN;
1891541Srgrimes		auio.uio_iov = &aiov;
1901541Srgrimes		auio.uio_iovcnt = 1;
1911541Srgrimes		auio.uio_offset = 0;
1921541Srgrimes		auio.uio_rw = UIO_READ;
1931541Srgrimes		auio.uio_segflg = UIO_SYSSPACE;
1941541Srgrimes		auio.uio_procp = (struct proc *)0;
1951541Srgrimes		auio.uio_resid = MAXPATHLEN;
1963148Sphk		error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
1973148Sphk		if (error) {
1981541Srgrimes			if (ndp->ni_pathlen > 1)
19929653Sdyson				zfree(namei_zone, cp);
2001541Srgrimes			break;
2011541Srgrimes		}
2021541Srgrimes		linklen = MAXPATHLEN - auio.uio_resid;
2031541Srgrimes		if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
2041541Srgrimes			if (ndp->ni_pathlen > 1)
20529653Sdyson				zfree(namei_zone, cp);
2061541Srgrimes			error = ENAMETOOLONG;
2071541Srgrimes			break;
2081541Srgrimes		}
2091541Srgrimes		if (ndp->ni_pathlen > 1) {
2101541Srgrimes			bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
21129653Sdyson			zfree(namei_zone, cnp->cn_pnbuf);
2121541Srgrimes			cnp->cn_pnbuf = cp;
2131541Srgrimes		} else
2141541Srgrimes			cnp->cn_pnbuf[linklen] = '\0';
2151541Srgrimes		ndp->ni_pathlen += linklen;
2161541Srgrimes		vput(ndp->ni_vp);
2171541Srgrimes		dp = ndp->ni_dvp;
2181541Srgrimes	}
21929653Sdyson	zfree(namei_zone, cnp->cn_pnbuf);
2201541Srgrimes	vrele(ndp->ni_dvp);
2211541Srgrimes	vput(ndp->ni_vp);
2221541Srgrimes	ndp->ni_vp = NULL;
2231541Srgrimes	return (error);
2241541Srgrimes}
2251541Srgrimes
2261541Srgrimes/*
2271541Srgrimes * Search a pathname.
2281541Srgrimes * This is a very central and rather complicated routine.
2291541Srgrimes *
2301541Srgrimes * The pathname is pointed to by ni_ptr and is of length ni_pathlen.
2311541Srgrimes * The starting directory is taken from ni_startdir. The pathname is
2321541Srgrimes * descended until done, or a symbolic link is encountered. The variable
2331541Srgrimes * ni_more is clear if the path is completed; it is set to one if a
2341541Srgrimes * symbolic link needing interpretation is encountered.
2351541Srgrimes *
2361541Srgrimes * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
2371541Srgrimes * whether the name is to be looked up, created, renamed, or deleted.
2381541Srgrimes * When CREATE, RENAME, or DELETE is specified, information usable in
2391541Srgrimes * creating, renaming, or deleting a directory entry may be calculated.
2401541Srgrimes * If flag has LOCKPARENT or'ed into it, the parent directory is returned
2411541Srgrimes * locked. If flag has WANTPARENT or'ed into it, the parent directory is
2421541Srgrimes * returned unlocked. Otherwise the parent directory is not returned. If
2431541Srgrimes * the target of the pathname exists and LOCKLEAF is or'ed into the flag
2441541Srgrimes * the target is returned locked, otherwise it is returned unlocked.
2451541Srgrimes * When creating or renaming and LOCKPARENT is specified, the target may not
2461541Srgrimes * be ".".  When deleting and LOCKPARENT is specified, the target may be ".".
2478876Srgrimes *
2481541Srgrimes * Overall outline of lookup:
2491541Srgrimes *
2501541Srgrimes * dirloop:
2511541Srgrimes *	identify next component of name at ndp->ni_ptr
2521541Srgrimes *	handle degenerate case where name is null string
2531541Srgrimes *	if .. and crossing mount points and on mounted filesys, find parent
2541541Srgrimes *	call VOP_LOOKUP routine for next component name
2551541Srgrimes *	    directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set
2561541Srgrimes *	    component vnode returned in ni_vp (if it exists), locked.
2571541Srgrimes *	if result vnode is mounted on and crossing mount points,
2581541Srgrimes *	    find mounted on vnode
2591541Srgrimes *	if more components of name, do next level at dirloop
2601541Srgrimes *	return the answer in ni_vp, locked if LOCKLEAF set
2611541Srgrimes *	    if LOCKPARENT set, return locked parent in ni_dvp
2621541Srgrimes *	    if WANTPARENT set, return unlocked parent in ni_dvp
2631541Srgrimes */
2641541Srgrimesint
2651541Srgrimeslookup(ndp)
2661541Srgrimes	register struct nameidata *ndp;
2671541Srgrimes{
2681541Srgrimes	register char *cp;		/* pointer into pathname argument */
2691541Srgrimes	register struct vnode *dp = 0;	/* the directory we are searching */
2701541Srgrimes	struct vnode *tdp;		/* saved dp */
2711541Srgrimes	struct mount *mp;		/* mount table entry */
2721541Srgrimes	int docache;			/* == 0 do not cache last component */
2731541Srgrimes	int wantparent;			/* 1 => wantparent or lockparent flag */
2741541Srgrimes	int rdonly;			/* lookup read-only flag bit */
2759804Sbde	int trailing_slash;
2761541Srgrimes	int error = 0;
2771541Srgrimes	struct componentname *cnp = &ndp->ni_cnd;
27822521Sdyson	struct proc *p = cnp->cn_proc;
2791541Srgrimes
2801541Srgrimes	/*
2811541Srgrimes	 * Setup: break out flag bits into variables.
2821541Srgrimes	 */
2831541Srgrimes	wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT);
2841541Srgrimes	docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE;
2851541Srgrimes	if (cnp->cn_nameiop == DELETE ||
28622874Sbde	    (wantparent && cnp->cn_nameiop != CREATE &&
28722874Sbde	     cnp->cn_nameiop != LOOKUP))
2881541Srgrimes		docache = 0;
2891541Srgrimes	rdonly = cnp->cn_flags & RDONLY;
2901541Srgrimes	ndp->ni_dvp = NULL;
2911541Srgrimes	cnp->cn_flags &= ~ISSYMLINK;
2921541Srgrimes	dp = ndp->ni_startdir;
2931541Srgrimes	ndp->ni_startdir = NULLVP;
29422521Sdyson	vn_lock(dp, LK_EXCLUSIVE | LK_RETRY, p);
2951541Srgrimes
2961541Srgrimesdirloop:
2971541Srgrimes	/*
2981541Srgrimes	 * Search a new directory.
2991541Srgrimes	 *
3001541Srgrimes	 * The cn_hash value is for use by vfs_cache.
3011541Srgrimes	 * The last component of the filename is left accessible via
3021541Srgrimes	 * cnp->cn_nameptr for callers that need the name. Callers needing
3031541Srgrimes	 * the name set the SAVENAME flag. When done, they assume
3041541Srgrimes	 * responsibility for freeing the pathname buffer.
3051541Srgrimes	 */
3061541Srgrimes	cnp->cn_consume = 0;
3071541Srgrimes	cnp->cn_hash = 0;
3081541Srgrimes	for (cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++)
3091541Srgrimes		cnp->cn_hash += (unsigned char)*cp;
3101541Srgrimes	cnp->cn_namelen = cp - cnp->cn_nameptr;
3111541Srgrimes	if (cnp->cn_namelen > NAME_MAX) {
3121541Srgrimes		error = ENAMETOOLONG;
3131541Srgrimes		goto bad;
3141541Srgrimes	}
3151541Srgrimes#ifdef NAMEI_DIAGNOSTIC
3161541Srgrimes	{ char c = *cp;
3171541Srgrimes	*cp = '\0';
3181541Srgrimes	printf("{%s}: ", cnp->cn_nameptr);
3191541Srgrimes	*cp = c; }
3201541Srgrimes#endif
3211541Srgrimes	ndp->ni_pathlen -= cnp->cn_namelen;
3221541Srgrimes	ndp->ni_next = cp;
3239804Sbde
3249804Sbde	/*
3259804Sbde	 * Replace multiple slashes by a single slash and trailing slashes
3269804Sbde	 * by a null.  This must be done before VOP_LOOKUP() because some
3279804Sbde	 * fs's don't know about trailing slashes.  Remember if there were
3289804Sbde	 * trailing slashes to handle symlinks, existing non-directories
3299804Sbde	 * and non-existing files that won't be directories specially later.
3309804Sbde	 */
3319804Sbde	trailing_slash = 0;
3329804Sbde	while (*cp == '/' && (cp[1] == '/' || cp[1] == '\0')) {
3339804Sbde		cp++;
3349804Sbde		ndp->ni_pathlen--;
3359804Sbde		if (*cp == '\0') {
3369804Sbde			trailing_slash = 1;
3379804Sbde			*ndp->ni_next = '\0';	/* XXX for direnter() ... */
3389804Sbde		}
3399804Sbde	}
3409804Sbde	ndp->ni_next = cp;
3419804Sbde
3421541Srgrimes	cnp->cn_flags |= MAKEENTRY;
3431541Srgrimes	if (*cp == '\0' && docache == 0)
3441541Srgrimes		cnp->cn_flags &= ~MAKEENTRY;
3451541Srgrimes	if (cnp->cn_namelen == 2 &&
3461541Srgrimes	    cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.')
3471541Srgrimes		cnp->cn_flags |= ISDOTDOT;
3481541Srgrimes	else
3491541Srgrimes		cnp->cn_flags &= ~ISDOTDOT;
3501541Srgrimes	if (*ndp->ni_next == 0)
3511541Srgrimes		cnp->cn_flags |= ISLASTCN;
3521541Srgrimes	else
3531541Srgrimes		cnp->cn_flags &= ~ISLASTCN;
3541541Srgrimes
3551541Srgrimes
3561541Srgrimes	/*
3571541Srgrimes	 * Check for degenerate name (e.g. / or "")
3581541Srgrimes	 * which is a way of talking about a directory,
3591541Srgrimes	 * e.g. like "/." or ".".
3601541Srgrimes	 */
3611541Srgrimes	if (cnp->cn_nameptr[0] == '\0') {
36222521Sdyson		if (dp->v_type != VDIR) {
36322521Sdyson			error = ENOTDIR;
36422521Sdyson			goto bad;
36522521Sdyson		}
3661541Srgrimes		if (cnp->cn_nameiop != LOOKUP) {
3671541Srgrimes			error = EISDIR;
3681541Srgrimes			goto bad;
3691541Srgrimes		}
3701541Srgrimes		if (wantparent) {
3711541Srgrimes			ndp->ni_dvp = dp;
3721541Srgrimes			VREF(dp);
3731541Srgrimes		}
3741541Srgrimes		ndp->ni_vp = dp;
3751541Srgrimes		if (!(cnp->cn_flags & (LOCKPARENT | LOCKLEAF)))
37622521Sdyson			VOP_UNLOCK(dp, 0, p);
3771541Srgrimes		if (cnp->cn_flags & SAVESTART)
3781541Srgrimes			panic("lookup: SAVESTART");
3791541Srgrimes		return (0);
3801541Srgrimes	}
3811541Srgrimes
3821541Srgrimes	/*
3831541Srgrimes	 * Handle "..": two special cases.
3841541Srgrimes	 * 1. If at root directory (e.g. after chroot)
3851541Srgrimes	 *    or at absolute root directory
3861541Srgrimes	 *    then ignore it so can't get out.
3871541Srgrimes	 * 2. If this vnode is the root of a mounted
3881541Srgrimes	 *    filesystem, then replace it with the
3891541Srgrimes	 *    vnode which was mounted on so we take the
3901541Srgrimes	 *    .. in the other file system.
3911541Srgrimes	 */
3921541Srgrimes	if (cnp->cn_flags & ISDOTDOT) {
3931541Srgrimes		for (;;) {
3941541Srgrimes			if (dp == ndp->ni_rootdir || dp == rootvnode) {
3951541Srgrimes				ndp->ni_dvp = dp;
3961541Srgrimes				ndp->ni_vp = dp;
3971541Srgrimes				VREF(dp);
3981541Srgrimes				goto nextname;
3991541Srgrimes			}
4001541Srgrimes			if ((dp->v_flag & VROOT) == 0 ||
4011541Srgrimes			    (cnp->cn_flags & NOCROSSMOUNT))
4021541Srgrimes				break;
4031541Srgrimes			tdp = dp;
4041541Srgrimes			dp = dp->v_mount->mnt_vnodecovered;
4051541Srgrimes			vput(tdp);
4061541Srgrimes			VREF(dp);
40722521Sdyson			vn_lock(dp, LK_EXCLUSIVE | LK_RETRY, p);
4081541Srgrimes		}
4091541Srgrimes	}
4101541Srgrimes
4111541Srgrimes	/*
4121541Srgrimes	 * We now have a segment name to search for, and a directory to search.
4131541Srgrimes	 */
4141541Srgrimesunionlookup:
4151541Srgrimes	ndp->ni_dvp = dp;
41622521Sdyson	ndp->ni_vp = NULL;
41724624Sdfr	ASSERT_VOP_LOCKED(dp, "lookup");
41822521Sdyson	if (error = VOP_LOOKUP(dp, &ndp->ni_vp, cnp)) {
4191541Srgrimes#ifdef DIAGNOSTIC
4201541Srgrimes		if (ndp->ni_vp != NULL)
4211541Srgrimes			panic("leaf should be empty");
4221541Srgrimes#endif
4231541Srgrimes#ifdef NAMEI_DIAGNOSTIC
4241541Srgrimes		printf("not found\n");
4251541Srgrimes#endif
4261541Srgrimes		if ((error == ENOENT) &&
4271541Srgrimes		    (dp->v_flag & VROOT) &&
4281541Srgrimes		    (dp->v_mount->mnt_flag & MNT_UNION)) {
4291541Srgrimes			tdp = dp;
4301541Srgrimes			dp = dp->v_mount->mnt_vnodecovered;
4311541Srgrimes			vput(tdp);
4321541Srgrimes			VREF(dp);
43322521Sdyson			vn_lock(dp, LK_EXCLUSIVE | LK_RETRY, p);
4341541Srgrimes			goto unionlookup;
4351541Srgrimes		}
4361541Srgrimes
4371541Srgrimes		if (error != EJUSTRETURN)
4381541Srgrimes			goto bad;
4391541Srgrimes		/*
4401541Srgrimes		 * If creating and at end of pathname, then can consider
4411541Srgrimes		 * allowing file to be created.
4421541Srgrimes		 */
44311644Sdg		if (rdonly) {
4441541Srgrimes			error = EROFS;
4451541Srgrimes			goto bad;
4461541Srgrimes		}
4479804Sbde		if (*cp == '\0' && trailing_slash &&
4489804Sbde		     !(cnp->cn_flags & WILLBEDIR)) {
4499804Sbde			error = ENOENT;
4509804Sbde			goto bad;
4519804Sbde		}
4521541Srgrimes		/*
4531541Srgrimes		 * We return with ni_vp NULL to indicate that the entry
4541541Srgrimes		 * doesn't currently exist, leaving a pointer to the
4551541Srgrimes		 * (possibly locked) directory inode in ndp->ni_dvp.
4561541Srgrimes		 */
4571541Srgrimes		if (cnp->cn_flags & SAVESTART) {
4581541Srgrimes			ndp->ni_startdir = ndp->ni_dvp;
4591541Srgrimes			VREF(ndp->ni_startdir);
4601541Srgrimes		}
4611541Srgrimes		return (0);
4621541Srgrimes	}
4631541Srgrimes#ifdef NAMEI_DIAGNOSTIC
4641541Srgrimes	printf("found\n");
4651541Srgrimes#endif
4661541Srgrimes
46724624Sdfr	ASSERT_VOP_LOCKED(ndp->ni_vp, "lookup");
46824624Sdfr
4691541Srgrimes	/*
4701541Srgrimes	 * Take into account any additional components consumed by
4711541Srgrimes	 * the underlying filesystem.
4721541Srgrimes	 */
4731541Srgrimes	if (cnp->cn_consume > 0) {
4741541Srgrimes		cnp->cn_nameptr += cnp->cn_consume;
4751541Srgrimes		ndp->ni_next += cnp->cn_consume;
4761541Srgrimes		ndp->ni_pathlen -= cnp->cn_consume;
4771541Srgrimes		cnp->cn_consume = 0;
4781541Srgrimes	}
4791541Srgrimes
4801541Srgrimes	dp = ndp->ni_vp;
4811541Srgrimes
4821541Srgrimes	/*
4831541Srgrimes	 * Check to see if the vnode has been mounted on;
4841541Srgrimes	 * if so find the root of the mounted file system.
4851541Srgrimes	 */
4861541Srgrimes	while (dp->v_type == VDIR && (mp = dp->v_mountedhere) &&
4871541Srgrimes	       (cnp->cn_flags & NOCROSSMOUNT) == 0) {
48822521Sdyson		if (vfs_busy(mp, 0, 0, p))
4891541Srgrimes			continue;
49022521Sdyson		error = VFS_ROOT(mp, &tdp);
49122521Sdyson		vfs_unbusy(mp, p);
4923148Sphk		if (error)
4931541Srgrimes			goto bad2;
4941541Srgrimes		vput(dp);
4951541Srgrimes		ndp->ni_vp = dp = tdp;
4961541Srgrimes	}
4971541Srgrimes
49810219Sdfr	/*
49910219Sdfr	 * Check for symbolic link
50010219Sdfr	 */
50110219Sdfr	if ((dp->v_type == VLNK) &&
50210219Sdfr	    ((cnp->cn_flags & FOLLOW) || trailing_slash ||
50310219Sdfr	     *ndp->ni_next == '/')) {
50410219Sdfr		cnp->cn_flags |= ISSYMLINK;
50510219Sdfr		return (0);
50610219Sdfr	}
50710219Sdfr
50810219Sdfr	/*
50910219Sdfr	 * Check for bogus trailing slashes.
51010219Sdfr	 */
51110219Sdfr	if (trailing_slash && dp->v_type != VDIR) {
51210219Sdfr		error = ENOTDIR;
51310219Sdfr		goto bad2;
51410219Sdfr	}
51510219Sdfr
5161541Srgrimesnextname:
5171541Srgrimes	/*
5181541Srgrimes	 * Not a symbolic link.  If more pathname,
5191541Srgrimes	 * continue at next component, else return.
5201541Srgrimes	 */
5211541Srgrimes	if (*ndp->ni_next == '/') {
5221541Srgrimes		cnp->cn_nameptr = ndp->ni_next;
5231541Srgrimes		while (*cnp->cn_nameptr == '/') {
5241541Srgrimes			cnp->cn_nameptr++;
5251541Srgrimes			ndp->ni_pathlen--;
5261541Srgrimes		}
52724624Sdfr		if (ndp->ni_dvp != ndp->ni_vp) {
52824624Sdfr		    ASSERT_VOP_UNLOCKED(ndp->ni_dvp, "lookup");
52924624Sdfr		}
5301541Srgrimes		vrele(ndp->ni_dvp);
5311541Srgrimes		goto dirloop;
5321541Srgrimes	}
5331541Srgrimes	/*
53411644Sdg	 * Disallow directory write attempts on read-only file systems.
5351541Srgrimes	 */
53611644Sdg	if (rdonly &&
53711644Sdg	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
53811644Sdg		error = EROFS;
53911644Sdg		goto bad2;
5401541Srgrimes	}
5411541Srgrimes	if (cnp->cn_flags & SAVESTART) {
5421541Srgrimes		ndp->ni_startdir = ndp->ni_dvp;
5431541Srgrimes		VREF(ndp->ni_startdir);
5441541Srgrimes	}
5451541Srgrimes	if (!wantparent)
5461541Srgrimes		vrele(ndp->ni_dvp);
54732071Sdyson
5481541Srgrimes	if ((cnp->cn_flags & LOCKLEAF) == 0)
54922521Sdyson		VOP_UNLOCK(dp, 0, p);
5501541Srgrimes	return (0);
5511541Srgrimes
5521541Srgrimesbad2:
5531541Srgrimes	if ((cnp->cn_flags & LOCKPARENT) && *ndp->ni_next == '\0')
55422521Sdyson		VOP_UNLOCK(ndp->ni_dvp, 0, p);
5551541Srgrimes	vrele(ndp->ni_dvp);
5561541Srgrimesbad:
5571541Srgrimes	vput(dp);
5581541Srgrimes	ndp->ni_vp = NULL;
5591541Srgrimes	return (error);
5601541Srgrimes}
5611541Srgrimes
5623148Sphk/*
5633148Sphk * relookup - lookup a path name component
5643148Sphk *    Used by lookup to re-aquire things.
5653148Sphk */
5663148Sphkint
5673148Sphkrelookup(dvp, vpp, cnp)
5683148Sphk	struct vnode *dvp, **vpp;
5693148Sphk	struct componentname *cnp;
5703148Sphk{
57122521Sdyson	struct proc *p = cnp->cn_proc;
57222521Sdyson	struct vnode *dp = 0;		/* the directory we are searching */
5733148Sphk	int docache;			/* == 0 do not cache last component */
5743148Sphk	int wantparent;			/* 1 => wantparent or lockparent flag */
5753148Sphk	int rdonly;			/* lookup read-only flag bit */
5763148Sphk	int error = 0;
5773148Sphk#ifdef NAMEI_DIAGNOSTIC
5783148Sphk	int newhash;			/* DEBUG: check name hash */
5793148Sphk	char *cp;			/* DEBUG: check name ptr/len */
5803148Sphk#endif
5811541Srgrimes
5823148Sphk	/*
5833148Sphk	 * Setup: break out flag bits into variables.
5843148Sphk	 */
5853148Sphk	wantparent = cnp->cn_flags & (LOCKPARENT|WANTPARENT);
5863148Sphk	docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE;
5873148Sphk	if (cnp->cn_nameiop == DELETE ||
5883148Sphk	    (wantparent && cnp->cn_nameiop != CREATE))
5893148Sphk		docache = 0;
5903148Sphk	rdonly = cnp->cn_flags & RDONLY;
5913148Sphk	cnp->cn_flags &= ~ISSYMLINK;
5923148Sphk	dp = dvp;
59322521Sdyson	vn_lock(dp, LK_EXCLUSIVE | LK_RETRY, p);
5943148Sphk
5953148Sphk/* dirloop: */
5963148Sphk	/*
5973148Sphk	 * Search a new directory.
5983148Sphk	 *
5993148Sphk	 * The cn_hash value is for use by vfs_cache.
6003148Sphk	 * The last component of the filename is left accessible via
6013148Sphk	 * cnp->cn_nameptr for callers that need the name. Callers needing
6023148Sphk	 * the name set the SAVENAME flag. When done, they assume
6033148Sphk	 * responsibility for freeing the pathname buffer.
6043148Sphk	 */
6053148Sphk#ifdef NAMEI_DIAGNOSTIC
6063148Sphk	for (newhash = 0, cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++)
6073148Sphk		newhash += (unsigned char)*cp;
6083148Sphk	if (newhash != cnp->cn_hash)
6093148Sphk		panic("relookup: bad hash");
6103148Sphk	if (cnp->cn_namelen != cp - cnp->cn_nameptr)
6113148Sphk		panic ("relookup: bad len");
6123148Sphk	if (*cp != 0)
6133148Sphk		panic("relookup: not last component");
6143148Sphk	printf("{%s}: ", cnp->cn_nameptr);
6153148Sphk#endif
6163148Sphk
6173148Sphk	/*
6183148Sphk	 * Check for degenerate name (e.g. / or "")
6193148Sphk	 * which is a way of talking about a directory,
6203148Sphk	 * e.g. like "/." or ".".
6213148Sphk	 */
6223148Sphk	if (cnp->cn_nameptr[0] == '\0') {
6233148Sphk		if (cnp->cn_nameiop != LOOKUP || wantparent) {
6243148Sphk			error = EISDIR;
6253148Sphk			goto bad;
6263148Sphk		}
6273148Sphk		if (dp->v_type != VDIR) {
6283148Sphk			error = ENOTDIR;
6293148Sphk			goto bad;
6303148Sphk		}
6313148Sphk		if (!(cnp->cn_flags & LOCKLEAF))
63222521Sdyson			VOP_UNLOCK(dp, 0, p);
6333148Sphk		*vpp = dp;
6343148Sphk		if (cnp->cn_flags & SAVESTART)
6353148Sphk			panic("lookup: SAVESTART");
6363148Sphk		return (0);
6373148Sphk	}
6383148Sphk
6393148Sphk	if (cnp->cn_flags & ISDOTDOT)
6403148Sphk		panic ("relookup: lookup on dot-dot");
6413148Sphk
6423148Sphk	/*
6433148Sphk	 * We now have a segment name to search for, and a directory to search.
6443148Sphk	 */
64522521Sdyson	if (error = VOP_LOOKUP(dp, vpp, cnp)) {
6463148Sphk#ifdef DIAGNOSTIC
6473148Sphk		if (*vpp != NULL)
6483148Sphk			panic("leaf should be empty");
6493148Sphk#endif
6503148Sphk		if (error != EJUSTRETURN)
6513148Sphk			goto bad;
6523148Sphk		/*
6533148Sphk		 * If creating and at end of pathname, then can consider
6543148Sphk		 * allowing file to be created.
6553148Sphk		 */
65611644Sdg		if (rdonly) {
6573148Sphk			error = EROFS;
6583148Sphk			goto bad;
6593148Sphk		}
6603148Sphk		/* ASSERT(dvp == ndp->ni_startdir) */
6613148Sphk		if (cnp->cn_flags & SAVESTART)
6623148Sphk			VREF(dvp);
6633148Sphk		/*
6643148Sphk		 * We return with ni_vp NULL to indicate that the entry
6653148Sphk		 * doesn't currently exist, leaving a pointer to the
6663148Sphk		 * (possibly locked) directory inode in ndp->ni_dvp.
6673148Sphk		 */
6683148Sphk		return (0);
6693148Sphk	}
6703148Sphk	dp = *vpp;
6713148Sphk
6723148Sphk#ifdef DIAGNOSTIC
6733148Sphk	/*
6743148Sphk	 * Check for symbolic link
6753148Sphk	 */
6763148Sphk	if (dp->v_type == VLNK && (cnp->cn_flags & FOLLOW))
6773148Sphk		panic ("relookup: symlink found.\n");
6783148Sphk#endif
6793148Sphk
6803148Sphk	/*
68111644Sdg	 * Disallow directory write attempts on read-only file systems.
6823148Sphk	 */
68311644Sdg	if (rdonly &&
68411644Sdg	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
68511644Sdg		error = EROFS;
68611644Sdg		goto bad2;
6873148Sphk	}
6883148Sphk	/* ASSERT(dvp == ndp->ni_startdir) */
6893148Sphk	if (cnp->cn_flags & SAVESTART)
6903148Sphk		VREF(dvp);
69122521Sdyson
6923148Sphk	if (!wantparent)
6933148Sphk		vrele(dvp);
69432071Sdyson
69532286Sdyson	if (dp->v_type == VREG &&
69632286Sdyson		((cnp->cn_flags & (NOOBJ|LOCKLEAF)) == LOCKLEAF))
69732286Sdyson		vfs_object_create(dp, cnp->cn_proc, cnp->cn_cred, 1);
69832071Sdyson
6993148Sphk	if ((cnp->cn_flags & LOCKLEAF) == 0)
70022521Sdyson		VOP_UNLOCK(dp, 0, p);
7013148Sphk	return (0);
7023148Sphk
7033148Sphkbad2:
7043148Sphk	if ((cnp->cn_flags & LOCKPARENT) && (cnp->cn_flags & ISLASTCN))
70522521Sdyson		VOP_UNLOCK(dvp, 0, p);
7063148Sphk	vrele(dvp);
7073148Sphkbad:
7083148Sphk	vput(dp);
7093148Sphk	*vpp = NULL;
7103148Sphk	return (error);
7113148Sphk}
712