vfs_lookup.c revision 139804
1139804Simp/*-
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 * 4. Neither the name of the University nor the names of its contributors
191541Srgrimes *    may be used to endorse or promote products derived from this software
201541Srgrimes *    without specific prior written permission.
211541Srgrimes *
221541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
231541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
241541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
251541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
261541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
271541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
281541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
291541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
301541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
311541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
321541Srgrimes * SUCH DAMAGE.
331541Srgrimes *
341541Srgrimes *	@(#)vfs_lookup.c	8.4 (Berkeley) 2/16/94
351541Srgrimes */
361541Srgrimes
37116182Sobrien#include <sys/cdefs.h>
38116182Sobrien__FBSDID("$FreeBSD: head/sys/kern/vfs_lookup.c 139804 2005-01-06 23:35:40Z imp $");
39116182Sobrien
4013203Swollman#include "opt_ktrace.h"
41101127Srwatson#include "opt_mac.h"
4213203Swollman
431541Srgrimes#include <sys/param.h>
442112Swollman#include <sys/systm.h>
4569664Speter#include <sys/kernel.h>
4676166Smarkm#include <sys/lock.h>
47101127Srwatson#include <sys/mac.h>
4889316Salfred#include <sys/mutex.h>
491541Srgrimes#include <sys/namei.h>
501541Srgrimes#include <sys/vnode.h>
511541Srgrimes#include <sys/mount.h>
521541Srgrimes#include <sys/filedesc.h>
531541Srgrimes#include <sys/proc.h>
541541Srgrimes#ifdef KTRACE
551541Srgrimes#include <sys/ktrace.h>
561541Srgrimes#endif
571541Srgrimes
5892751Sjeff#include <vm/uma.h>
5932011Sbde
60138345Sphk#define NAMEI_DIAGNOSTIC 1
61138345Sphk#undef NAMEI_DIAGNOSTIC
62138345Sphk
631541Srgrimes/*
6469664Speter * Allocation zone for namei
6569664Speter */
6692751Sjeffuma_zone_t namei_zone;
6769664Speter
6869664Speterstatic void
6969664Speternameiinit(void *dummy __unused)
7069664Speter{
7192654Sjeff	namei_zone = uma_zcreate("NAMEI", MAXPATHLEN, NULL, NULL, NULL, NULL,
7292654Sjeff	    UMA_ALIGN_PTR, 0);
7369664Speter
7469664Speter}
7569664SpeterSYSINIT(vfs, SI_SUB_VFS, SI_ORDER_SECOND, nameiinit, NULL)
7669664Speter
7769664Speter/*
781541Srgrimes * Convert a pathname into a pointer to a locked inode.
791541Srgrimes *
801541Srgrimes * The FOLLOW flag is set when symbolic links are to be followed
811541Srgrimes * when they occur at the end of the name translation process.
821541Srgrimes * Symbolic links are always followed for all other pathname
831541Srgrimes * components other than the last.
841541Srgrimes *
851541Srgrimes * The segflg defines whether the name is to be copied from user
861541Srgrimes * space or kernel space.
871541Srgrimes *
881541Srgrimes * Overall outline of namei:
891541Srgrimes *
901541Srgrimes *	copy in name
911541Srgrimes *	get starting directory
921541Srgrimes *	while (!done && !error) {
931541Srgrimes *		call lookup to search path.
941541Srgrimes *		if symbolic link, massage name in buffer and continue
951541Srgrimes *	}
961541Srgrimes */
971541Srgrimesint
981541Srgrimesnamei(ndp)
991541Srgrimes	register struct nameidata *ndp;
1001541Srgrimes{
1011541Srgrimes	register struct filedesc *fdp;	/* pointer to file descriptor state */
1021541Srgrimes	register char *cp;		/* pointer into pathname argument */
1031541Srgrimes	register struct vnode *dp;	/* the directory we are searching */
1041541Srgrimes	struct iovec aiov;		/* uio for reading symbolic links */
1051541Srgrimes	struct uio auio;
1061541Srgrimes	int error, linklen;
1071541Srgrimes	struct componentname *cnp = &ndp->ni_cnd;
10883366Sjulian	struct thread *td = cnp->cn_thread;
10983366Sjulian	struct proc *p = td->td_proc;
1101541Srgrimes
111133131Srwatson	GIANT_REQUIRED;
112133131Srwatson
11391419Sjhb	ndp->ni_cnd.cn_cred = ndp->ni_cnd.cn_thread->td_ucred;
11483366Sjulian	KASSERT(cnp->cn_cred && p, ("namei: bad cred/proc"));
11542408Seivind	KASSERT((cnp->cn_nameiop & (~OPMASK)) == 0,
11642453Seivind	    ("namei: nameiop contaminated with flags"));
11742408Seivind	KASSERT((cnp->cn_flags & OPMASK) == 0,
11842453Seivind	    ("namei: flags contaminated with nameiops"));
11983366Sjulian	fdp = p->p_fd;
1201541Srgrimes
1211541Srgrimes	/*
1221541Srgrimes	 * Get a buffer for the name to be translated, and copy the
1231541Srgrimes	 * name into the buffer.
1241541Srgrimes	 */
1251541Srgrimes	if ((cnp->cn_flags & HASBUF) == 0)
126111119Simp		cnp->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK);
1271541Srgrimes	if (ndp->ni_segflg == UIO_SYSSPACE)
1281541Srgrimes		error = copystr(ndp->ni_dirp, cnp->cn_pnbuf,
12936735Sdfr			    MAXPATHLEN, (size_t *)&ndp->ni_pathlen);
1301541Srgrimes	else
1311541Srgrimes		error = copyinstr(ndp->ni_dirp, cnp->cn_pnbuf,
13236735Sdfr			    MAXPATHLEN, (size_t *)&ndp->ni_pathlen);
13320069Sbde
13420069Sbde	/*
13520069Sbde	 * Don't allow empty pathnames.
13620069Sbde	 */
13720069Sbde	if (!error && *cnp->cn_pnbuf == '\0')
13820069Sbde		error = ENOENT;
13920069Sbde
1401541Srgrimes	if (error) {
14192751Sjeff		uma_zfree(namei_zone, cnp->cn_pnbuf);
142100613Srwatson#ifdef DIAGNOSTIC
143100613Srwatson		cnp->cn_pnbuf = NULL;
144100613Srwatson		cnp->cn_nameptr = NULL;
145100613Srwatson#endif
1461541Srgrimes		ndp->ni_vp = NULL;
1471541Srgrimes		return (error);
1481541Srgrimes	}
1491541Srgrimes	ndp->ni_loopcnt = 0;
1501541Srgrimes#ifdef KTRACE
15197994Sjhb	if (KTRPOINT(td, KTR_NAMEI)) {
15297994Sjhb		KASSERT(cnp->cn_thread == curthread,
15397994Sjhb		    ("namei not using curthread"));
15497994Sjhb		ktrnamei(cnp->cn_pnbuf);
15597994Sjhb	}
1561541Srgrimes#endif
1571541Srgrimes
1581541Srgrimes	/*
1591541Srgrimes	 * Get starting point for the translation.
1601541Srgrimes	 */
16189306Salfred	FILEDESC_LOCK(fdp);
16233360Sdyson	ndp->ni_rootdir = fdp->fd_rdir;
16351649Sphk	ndp->ni_topdir = fdp->fd_jdir;
16433360Sdyson
1651541Srgrimes	dp = fdp->fd_cdir;
1661541Srgrimes	VREF(dp);
16789306Salfred	FILEDESC_UNLOCK(fdp);
1681541Srgrimes	for (;;) {
1691541Srgrimes		/*
1701541Srgrimes		 * Check if root directory should replace current directory.
1711541Srgrimes		 * Done at start of translation and after symbolic link.
1721541Srgrimes		 */
1731541Srgrimes		cnp->cn_nameptr = cnp->cn_pnbuf;
1741541Srgrimes		if (*(cnp->cn_nameptr) == '/') {
1751541Srgrimes			vrele(dp);
1761541Srgrimes			while (*(cnp->cn_nameptr) == '/') {
1771541Srgrimes				cnp->cn_nameptr++;
1781541Srgrimes				ndp->ni_pathlen--;
1791541Srgrimes			}
1801541Srgrimes			dp = ndp->ni_rootdir;
1811541Srgrimes			VREF(dp);
1821541Srgrimes		}
1831541Srgrimes		ndp->ni_startdir = dp;
1843148Sphk		error = lookup(ndp);
1853148Sphk		if (error) {
18692751Sjeff			uma_zfree(namei_zone, cnp->cn_pnbuf);
187100613Srwatson#ifdef DIAGNOSTIC
188100613Srwatson			cnp->cn_pnbuf = NULL;
189100613Srwatson			cnp->cn_nameptr = NULL;
190100613Srwatson#endif
1911541Srgrimes			return (error);
1921541Srgrimes		}
1931541Srgrimes		/*
1941541Srgrimes		 * Check for symbolic link
1951541Srgrimes		 */
1961541Srgrimes		if ((cnp->cn_flags & ISSYMLINK) == 0) {
197100613Srwatson			if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0) {
19892751Sjeff				uma_zfree(namei_zone, cnp->cn_pnbuf);
199100613Srwatson#ifdef DIAGNOSTIC
200100613Srwatson				cnp->cn_pnbuf = NULL;
201100613Srwatson				cnp->cn_nameptr = NULL;
202100613Srwatson#endif
203100613Srwatson			} else
2041541Srgrimes				cnp->cn_flags |= HASBUF;
20532286Sdyson
20649101Salc			if (vn_canvmio(ndp->ni_vp) == TRUE &&
20732286Sdyson				(cnp->cn_nameiop != DELETE) &&
20842315Seivind				((cnp->cn_flags & (NOOBJ|LOCKLEAF)) ==
20942315Seivind				 LOCKLEAF))
21083366Sjulian				vfs_object_create(ndp->ni_vp, td,
21142315Seivind					ndp->ni_cnd.cn_cred);
21232286Sdyson
2131541Srgrimes			return (0);
2141541Srgrimes		}
2151541Srgrimes		if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1)
21683366Sjulian			VOP_UNLOCK(ndp->ni_dvp, 0, td);
2171541Srgrimes		if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
2181541Srgrimes			error = ELOOP;
2191541Srgrimes			break;
2201541Srgrimes		}
221101127Srwatson#ifdef MAC
222105479Srwatson		if ((cnp->cn_flags & NOMACCHECK) == 0) {
223105479Srwatson			error = mac_check_vnode_readlink(td->td_ucred,
224105479Srwatson			    ndp->ni_vp);
225105479Srwatson			if (error)
226105479Srwatson				break;
227105479Srwatson		}
228101127Srwatson#endif
2291541Srgrimes		if (ndp->ni_pathlen > 1)
230111119Simp			cp = uma_zalloc(namei_zone, M_WAITOK);
2311541Srgrimes		else
2321541Srgrimes			cp = cnp->cn_pnbuf;
2331541Srgrimes		aiov.iov_base = cp;
2341541Srgrimes		aiov.iov_len = MAXPATHLEN;
2351541Srgrimes		auio.uio_iov = &aiov;
2361541Srgrimes		auio.uio_iovcnt = 1;
2371541Srgrimes		auio.uio_offset = 0;
2381541Srgrimes		auio.uio_rw = UIO_READ;
2391541Srgrimes		auio.uio_segflg = UIO_SYSSPACE;
24083366Sjulian		auio.uio_td = (struct thread *)0;
2411541Srgrimes		auio.uio_resid = MAXPATHLEN;
2423148Sphk		error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
2433148Sphk		if (error) {
2441541Srgrimes			if (ndp->ni_pathlen > 1)
24592751Sjeff				uma_zfree(namei_zone, cp);
2461541Srgrimes			break;
2471541Srgrimes		}
2481541Srgrimes		linklen = MAXPATHLEN - auio.uio_resid;
24978692Sdillon		if (linklen == 0) {
25078692Sdillon			if (ndp->ni_pathlen > 1)
25192751Sjeff				uma_zfree(namei_zone, cp);
25278692Sdillon			error = ENOENT;
25378692Sdillon			break;
25478692Sdillon		}
2551541Srgrimes		if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
2561541Srgrimes			if (ndp->ni_pathlen > 1)
25792751Sjeff				uma_zfree(namei_zone, cp);
2581541Srgrimes			error = ENAMETOOLONG;
2591541Srgrimes			break;
2601541Srgrimes		}
2611541Srgrimes		if (ndp->ni_pathlen > 1) {
2621541Srgrimes			bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
26392751Sjeff			uma_zfree(namei_zone, cnp->cn_pnbuf);
2641541Srgrimes			cnp->cn_pnbuf = cp;
2651541Srgrimes		} else
2661541Srgrimes			cnp->cn_pnbuf[linklen] = '\0';
2671541Srgrimes		ndp->ni_pathlen += linklen;
2681541Srgrimes		vput(ndp->ni_vp);
2691541Srgrimes		dp = ndp->ni_dvp;
2701541Srgrimes	}
27192751Sjeff	uma_zfree(namei_zone, cnp->cn_pnbuf);
272100613Srwatson#ifdef DIAGNOSTIC
273100613Srwatson	cnp->cn_pnbuf = NULL;
274100613Srwatson	cnp->cn_nameptr = NULL;
275100613Srwatson#endif
2761541Srgrimes	vrele(ndp->ni_dvp);
2771541Srgrimes	vput(ndp->ni_vp);
2781541Srgrimes	ndp->ni_vp = NULL;
2791541Srgrimes	return (error);
2801541Srgrimes}
2811541Srgrimes
2821541Srgrimes/*
2831541Srgrimes * Search a pathname.
2841541Srgrimes * This is a very central and rather complicated routine.
2851541Srgrimes *
2861541Srgrimes * The pathname is pointed to by ni_ptr and is of length ni_pathlen.
2871541Srgrimes * The starting directory is taken from ni_startdir. The pathname is
2881541Srgrimes * descended until done, or a symbolic link is encountered. The variable
2891541Srgrimes * ni_more is clear if the path is completed; it is set to one if a
2901541Srgrimes * symbolic link needing interpretation is encountered.
2911541Srgrimes *
2921541Srgrimes * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
2931541Srgrimes * whether the name is to be looked up, created, renamed, or deleted.
2941541Srgrimes * When CREATE, RENAME, or DELETE is specified, information usable in
2951541Srgrimes * creating, renaming, or deleting a directory entry may be calculated.
2961541Srgrimes * If flag has LOCKPARENT or'ed into it, the parent directory is returned
2971541Srgrimes * locked. If flag has WANTPARENT or'ed into it, the parent directory is
2981541Srgrimes * returned unlocked. Otherwise the parent directory is not returned. If
2991541Srgrimes * the target of the pathname exists and LOCKLEAF is or'ed into the flag
3001541Srgrimes * the target is returned locked, otherwise it is returned unlocked.
3011541Srgrimes * When creating or renaming and LOCKPARENT is specified, the target may not
3021541Srgrimes * be ".".  When deleting and LOCKPARENT is specified, the target may be ".".
3038876Srgrimes *
3041541Srgrimes * Overall outline of lookup:
3051541Srgrimes *
3061541Srgrimes * dirloop:
3071541Srgrimes *	identify next component of name at ndp->ni_ptr
3081541Srgrimes *	handle degenerate case where name is null string
3091541Srgrimes *	if .. and crossing mount points and on mounted filesys, find parent
3101541Srgrimes *	call VOP_LOOKUP routine for next component name
3111541Srgrimes *	    directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set
3121541Srgrimes *	    component vnode returned in ni_vp (if it exists), locked.
3131541Srgrimes *	if result vnode is mounted on and crossing mount points,
3141541Srgrimes *	    find mounted on vnode
3151541Srgrimes *	if more components of name, do next level at dirloop
3161541Srgrimes *	return the answer in ni_vp, locked if LOCKLEAF set
3171541Srgrimes *	    if LOCKPARENT set, return locked parent in ni_dvp
3181541Srgrimes *	    if WANTPARENT set, return unlocked parent in ni_dvp
3191541Srgrimes */
3201541Srgrimesint
3211541Srgrimeslookup(ndp)
3221541Srgrimes	register struct nameidata *ndp;
3231541Srgrimes{
3241541Srgrimes	register char *cp;		/* pointer into pathname argument */
3251541Srgrimes	register struct vnode *dp = 0;	/* the directory we are searching */
3261541Srgrimes	struct vnode *tdp;		/* saved dp */
3271541Srgrimes	struct mount *mp;		/* mount table entry */
3281541Srgrimes	int docache;			/* == 0 do not cache last component */
3291541Srgrimes	int wantparent;			/* 1 => wantparent or lockparent flag */
3301541Srgrimes	int rdonly;			/* lookup read-only flag bit */
3319804Sbde	int trailing_slash;
3321541Srgrimes	int error = 0;
33365805Sbp	int dpunlocked = 0;		/* dp has already been unlocked */
3341541Srgrimes	struct componentname *cnp = &ndp->ni_cnd;
33583366Sjulian	struct thread *td = cnp->cn_thread;
3361541Srgrimes
3371541Srgrimes	/*
3381541Srgrimes	 * Setup: break out flag bits into variables.
3391541Srgrimes	 */
3401541Srgrimes	wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT);
3411541Srgrimes	docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE;
3421541Srgrimes	if (cnp->cn_nameiop == DELETE ||
34322874Sbde	    (wantparent && cnp->cn_nameiop != CREATE &&
34422874Sbde	     cnp->cn_nameiop != LOOKUP))
3451541Srgrimes		docache = 0;
3461541Srgrimes	rdonly = cnp->cn_flags & RDONLY;
3471541Srgrimes	ndp->ni_dvp = NULL;
3481541Srgrimes	cnp->cn_flags &= ~ISSYMLINK;
3491541Srgrimes	dp = ndp->ni_startdir;
3501541Srgrimes	ndp->ni_startdir = NULLVP;
35183366Sjulian	vn_lock(dp, LK_EXCLUSIVE | LK_RETRY, td);
3521541Srgrimes
3531541Srgrimesdirloop:
3541541Srgrimes	/*
3551541Srgrimes	 * Search a new directory.
3561541Srgrimes	 *
3571541Srgrimes	 * The last component of the filename is left accessible via
3581541Srgrimes	 * cnp->cn_nameptr for callers that need the name. Callers needing
3591541Srgrimes	 * the name set the SAVENAME flag. When done, they assume
3601541Srgrimes	 * responsibility for freeing the pathname buffer.
3611541Srgrimes	 */
3621541Srgrimes	cnp->cn_consume = 0;
3631541Srgrimes	for (cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++)
36451906Sphk		continue;
3651541Srgrimes	cnp->cn_namelen = cp - cnp->cn_nameptr;
3661541Srgrimes	if (cnp->cn_namelen > NAME_MAX) {
3671541Srgrimes		error = ENAMETOOLONG;
3681541Srgrimes		goto bad;
3691541Srgrimes	}
3701541Srgrimes#ifdef NAMEI_DIAGNOSTIC
3711541Srgrimes	{ char c = *cp;
3721541Srgrimes	*cp = '\0';
3731541Srgrimes	printf("{%s}: ", cnp->cn_nameptr);
3741541Srgrimes	*cp = c; }
3751541Srgrimes#endif
3761541Srgrimes	ndp->ni_pathlen -= cnp->cn_namelen;
3771541Srgrimes	ndp->ni_next = cp;
3789804Sbde
3799804Sbde	/*
3809804Sbde	 * Replace multiple slashes by a single slash and trailing slashes
3819804Sbde	 * by a null.  This must be done before VOP_LOOKUP() because some
3829804Sbde	 * fs's don't know about trailing slashes.  Remember if there were
3839804Sbde	 * trailing slashes to handle symlinks, existing non-directories
3849804Sbde	 * and non-existing files that won't be directories specially later.
3859804Sbde	 */
3869804Sbde	trailing_slash = 0;
3879804Sbde	while (*cp == '/' && (cp[1] == '/' || cp[1] == '\0')) {
3889804Sbde		cp++;
3899804Sbde		ndp->ni_pathlen--;
3909804Sbde		if (*cp == '\0') {
3919804Sbde			trailing_slash = 1;
3929804Sbde			*ndp->ni_next = '\0';	/* XXX for direnter() ... */
3939804Sbde		}
3949804Sbde	}
3959804Sbde	ndp->ni_next = cp;
3969804Sbde
3971541Srgrimes	cnp->cn_flags |= MAKEENTRY;
3981541Srgrimes	if (*cp == '\0' && docache == 0)
3991541Srgrimes		cnp->cn_flags &= ~MAKEENTRY;
4001541Srgrimes	if (cnp->cn_namelen == 2 &&
4011541Srgrimes	    cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.')
4021541Srgrimes		cnp->cn_flags |= ISDOTDOT;
4031541Srgrimes	else
4041541Srgrimes		cnp->cn_flags &= ~ISDOTDOT;
4051541Srgrimes	if (*ndp->ni_next == 0)
4061541Srgrimes		cnp->cn_flags |= ISLASTCN;
4071541Srgrimes	else
4081541Srgrimes		cnp->cn_flags &= ~ISLASTCN;
4091541Srgrimes
4101541Srgrimes
4111541Srgrimes	/*
4121541Srgrimes	 * Check for degenerate name (e.g. / or "")
4131541Srgrimes	 * which is a way of talking about a directory,
4141541Srgrimes	 * e.g. like "/." or ".".
4151541Srgrimes	 */
4161541Srgrimes	if (cnp->cn_nameptr[0] == '\0') {
41722521Sdyson		if (dp->v_type != VDIR) {
41822521Sdyson			error = ENOTDIR;
41922521Sdyson			goto bad;
42022521Sdyson		}
4211541Srgrimes		if (cnp->cn_nameiop != LOOKUP) {
4221541Srgrimes			error = EISDIR;
4231541Srgrimes			goto bad;
4241541Srgrimes		}
4251541Srgrimes		if (wantparent) {
4261541Srgrimes			ndp->ni_dvp = dp;
4271541Srgrimes			VREF(dp);
4281541Srgrimes		}
4291541Srgrimes		ndp->ni_vp = dp;
4301541Srgrimes		if (!(cnp->cn_flags & (LOCKPARENT | LOCKLEAF)))
43183366Sjulian			VOP_UNLOCK(dp, 0, td);
43254655Seivind		/* XXX This should probably move to the top of function. */
4331541Srgrimes		if (cnp->cn_flags & SAVESTART)
4341541Srgrimes			panic("lookup: SAVESTART");
4351541Srgrimes		return (0);
4361541Srgrimes	}
4371541Srgrimes
4381541Srgrimes	/*
4391541Srgrimes	 * Handle "..": two special cases.
4401541Srgrimes	 * 1. If at root directory (e.g. after chroot)
4411541Srgrimes	 *    or at absolute root directory
4421541Srgrimes	 *    then ignore it so can't get out.
4431541Srgrimes	 * 2. If this vnode is the root of a mounted
4441541Srgrimes	 *    filesystem, then replace it with the
4451541Srgrimes	 *    vnode which was mounted on so we take the
44696755Strhodes	 *    .. in the other filesystem.
44751649Sphk	 * 3. If the vnode is the top directory of
44851649Sphk	 *    the jail or chroot, don't let them out.
4491541Srgrimes	 */
4501541Srgrimes	if (cnp->cn_flags & ISDOTDOT) {
4511541Srgrimes		for (;;) {
45251649Sphk			if (dp == ndp->ni_rootdir ||
45351649Sphk			    dp == ndp->ni_topdir ||
45451649Sphk			    dp == rootvnode) {
4551541Srgrimes				ndp->ni_dvp = dp;
4561541Srgrimes				ndp->ni_vp = dp;
4571541Srgrimes				VREF(dp);
4581541Srgrimes				goto nextname;
4591541Srgrimes			}
460101308Sjeff			if ((dp->v_vflag & VV_ROOT) == 0 ||
4611541Srgrimes			    (cnp->cn_flags & NOCROSSMOUNT))
4621541Srgrimes				break;
46369405Salfred			if (dp->v_mount == NULL) {	/* forced unmount */
46469405Salfred				error = EBADF;
46569405Salfred				goto bad;
46669405Salfred			}
4671541Srgrimes			tdp = dp;
4681541Srgrimes			dp = dp->v_mount->mnt_vnodecovered;
4691541Srgrimes			vput(tdp);
4701541Srgrimes			VREF(dp);
47183366Sjulian			vn_lock(dp, LK_EXCLUSIVE | LK_RETRY, td);
4721541Srgrimes		}
4731541Srgrimes	}
4741541Srgrimes
4751541Srgrimes	/*
4761541Srgrimes	 * We now have a segment name to search for, and a directory to search.
4771541Srgrimes	 */
4781541Srgrimesunionlookup:
479101127Srwatson#ifdef MAC
480105479Srwatson	if ((cnp->cn_flags & NOMACCHECK) == 0) {
481105479Srwatson		error = mac_check_vnode_lookup(td->td_ucred, dp, cnp);
482105479Srwatson		if (error)
483105479Srwatson			goto bad;
484105479Srwatson	}
485101127Srwatson#endif
4861541Srgrimes	ndp->ni_dvp = dp;
48722521Sdyson	ndp->ni_vp = NULL;
48865973Sbp	cnp->cn_flags &= ~PDIRUNLOCK;
48924624Sdfr	ASSERT_VOP_LOCKED(dp, "lookup");
490138345Sphk#ifdef NAMEI_DIAGNOSTIC
491138345Sphk	vprint("lookup in", dp);
492138345Sphk#endif
49343301Sdillon	if ((error = VOP_LOOKUP(dp, &ndp->ni_vp, cnp)) != 0) {
49442408Seivind		KASSERT(ndp->ni_vp == NULL, ("leaf should be empty"));
4951541Srgrimes#ifdef NAMEI_DIAGNOSTIC
4961541Srgrimes		printf("not found\n");
4971541Srgrimes#endif
4981541Srgrimes		if ((error == ENOENT) &&
499101308Sjeff		    (dp->v_vflag & VV_ROOT) && (dp->v_mount != NULL) &&
5001541Srgrimes		    (dp->v_mount->mnt_flag & MNT_UNION)) {
5011541Srgrimes			tdp = dp;
5021541Srgrimes			dp = dp->v_mount->mnt_vnodecovered;
50365973Sbp			if (cnp->cn_flags & PDIRUNLOCK)
50465973Sbp				vrele(tdp);
50565973Sbp			else
50665973Sbp				vput(tdp);
5071541Srgrimes			VREF(dp);
50883366Sjulian			vn_lock(dp, LK_EXCLUSIVE | LK_RETRY, td);
5091541Srgrimes			goto unionlookup;
5101541Srgrimes		}
5111541Srgrimes
5121541Srgrimes		if (error != EJUSTRETURN)
5131541Srgrimes			goto bad;
5141541Srgrimes		/*
5151541Srgrimes		 * If creating and at end of pathname, then can consider
5161541Srgrimes		 * allowing file to be created.
5171541Srgrimes		 */
51811644Sdg		if (rdonly) {
5191541Srgrimes			error = EROFS;
5201541Srgrimes			goto bad;
5211541Srgrimes		}
5229804Sbde		if (*cp == '\0' && trailing_slash &&
5239804Sbde		     !(cnp->cn_flags & WILLBEDIR)) {
5249804Sbde			error = ENOENT;
5259804Sbde			goto bad;
5269804Sbde		}
5271541Srgrimes		/*
5281541Srgrimes		 * We return with ni_vp NULL to indicate that the entry
5291541Srgrimes		 * doesn't currently exist, leaving a pointer to the
5301541Srgrimes		 * (possibly locked) directory inode in ndp->ni_dvp.
5311541Srgrimes		 */
5321541Srgrimes		if (cnp->cn_flags & SAVESTART) {
5331541Srgrimes			ndp->ni_startdir = ndp->ni_dvp;
5341541Srgrimes			VREF(ndp->ni_startdir);
5351541Srgrimes		}
5361541Srgrimes		return (0);
5371541Srgrimes	}
5381541Srgrimes#ifdef NAMEI_DIAGNOSTIC
5391541Srgrimes	printf("found\n");
5401541Srgrimes#endif
5411541Srgrimes
54224624Sdfr	ASSERT_VOP_LOCKED(ndp->ni_vp, "lookup");
54324624Sdfr
5441541Srgrimes	/*
5451541Srgrimes	 * Take into account any additional components consumed by
5461541Srgrimes	 * the underlying filesystem.
5471541Srgrimes	 */
5481541Srgrimes	if (cnp->cn_consume > 0) {
5491541Srgrimes		cnp->cn_nameptr += cnp->cn_consume;
5501541Srgrimes		ndp->ni_next += cnp->cn_consume;
5511541Srgrimes		ndp->ni_pathlen -= cnp->cn_consume;
5521541Srgrimes		cnp->cn_consume = 0;
5531541Srgrimes	}
5541541Srgrimes
5551541Srgrimes	dp = ndp->ni_vp;
5561541Srgrimes
5571541Srgrimes	/*
5581541Srgrimes	 * Check to see if the vnode has been mounted on;
55996755Strhodes	 * if so find the root of the mounted filesystem.
5601541Srgrimes	 */
5611541Srgrimes	while (dp->v_type == VDIR && (mp = dp->v_mountedhere) &&
5621541Srgrimes	       (cnp->cn_flags & NOCROSSMOUNT) == 0) {
56383366Sjulian		if (vfs_busy(mp, 0, 0, td))
5641541Srgrimes			continue;
56583366Sjulian		VOP_UNLOCK(dp, 0, td);
566132023Salfred		error = VFS_ROOT(mp, &tdp, td);
56783366Sjulian		vfs_unbusy(mp, td);
56865805Sbp		if (error) {
56965805Sbp			dpunlocked = 1;
5701541Srgrimes			goto bad2;
57165805Sbp		}
57265805Sbp		vrele(dp);
5731541Srgrimes		ndp->ni_vp = dp = tdp;
5741541Srgrimes	}
5751541Srgrimes
57610219Sdfr	/*
57710219Sdfr	 * Check for symbolic link
57810219Sdfr	 */
57910219Sdfr	if ((dp->v_type == VLNK) &&
58010219Sdfr	    ((cnp->cn_flags & FOLLOW) || trailing_slash ||
58110219Sdfr	     *ndp->ni_next == '/')) {
58210219Sdfr		cnp->cn_flags |= ISSYMLINK;
58369405Salfred		if (dp->v_mount == NULL) {
58469405Salfred			/* We can't know whether the directory was mounted with
58569405Salfred			 * NOSYMFOLLOW, so we can't follow safely. */
58669405Salfred			error = EBADF;
58769405Salfred			goto bad2;
58869405Salfred		}
58935105Swosch		if (dp->v_mount->mnt_flag & MNT_NOSYMFOLLOW) {
59035105Swosch			error = EACCES;
59135105Swosch			goto bad2;
59235105Swosch		}
59310219Sdfr		return (0);
59410219Sdfr	}
59510219Sdfr
59610219Sdfr	/*
59710219Sdfr	 * Check for bogus trailing slashes.
59810219Sdfr	 */
59910219Sdfr	if (trailing_slash && dp->v_type != VDIR) {
60010219Sdfr		error = ENOTDIR;
60110219Sdfr		goto bad2;
60210219Sdfr	}
60310219Sdfr
6041541Srgrimesnextname:
6051541Srgrimes	/*
6061541Srgrimes	 * Not a symbolic link.  If more pathname,
6071541Srgrimes	 * continue at next component, else return.
6081541Srgrimes	 */
6091541Srgrimes	if (*ndp->ni_next == '/') {
6101541Srgrimes		cnp->cn_nameptr = ndp->ni_next;
6111541Srgrimes		while (*cnp->cn_nameptr == '/') {
6121541Srgrimes			cnp->cn_nameptr++;
6131541Srgrimes			ndp->ni_pathlen--;
6141541Srgrimes		}
61554655Seivind		if (ndp->ni_dvp != ndp->ni_vp)
61654655Seivind			ASSERT_VOP_UNLOCKED(ndp->ni_dvp, "lookup");
6171541Srgrimes		vrele(ndp->ni_dvp);
6181541Srgrimes		goto dirloop;
6191541Srgrimes	}
6201541Srgrimes	/*
62196755Strhodes	 * Disallow directory write attempts on read-only filesystems.
6221541Srgrimes	 */
62311644Sdg	if (rdonly &&
62411644Sdg	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
62511644Sdg		error = EROFS;
62611644Sdg		goto bad2;
6271541Srgrimes	}
6281541Srgrimes	if (cnp->cn_flags & SAVESTART) {
6291541Srgrimes		ndp->ni_startdir = ndp->ni_dvp;
6301541Srgrimes		VREF(ndp->ni_startdir);
6311541Srgrimes	}
6321541Srgrimes	if (!wantparent)
6331541Srgrimes		vrele(ndp->ni_dvp);
63432071Sdyson
6351541Srgrimes	if ((cnp->cn_flags & LOCKLEAF) == 0)
63683366Sjulian		VOP_UNLOCK(dp, 0, td);
6371541Srgrimes	return (0);
6381541Srgrimes
6391541Srgrimesbad2:
64065973Sbp	if ((cnp->cn_flags & (LOCKPARENT | PDIRUNLOCK)) == LOCKPARENT &&
64165973Sbp	    *ndp->ni_next == '\0')
64283366Sjulian		VOP_UNLOCK(ndp->ni_dvp, 0, td);
6431541Srgrimes	vrele(ndp->ni_dvp);
6441541Srgrimesbad:
64565805Sbp	if (dpunlocked)
64665805Sbp		vrele(dp);
64765805Sbp	else
64865805Sbp		vput(dp);
6491541Srgrimes	ndp->ni_vp = NULL;
6501541Srgrimes	return (error);
6511541Srgrimes}
6521541Srgrimes
6533148Sphk/*
6543148Sphk * relookup - lookup a path name component
6553148Sphk *    Used by lookup to re-aquire things.
6563148Sphk */
6573148Sphkint
6583148Sphkrelookup(dvp, vpp, cnp)
6593148Sphk	struct vnode *dvp, **vpp;
6603148Sphk	struct componentname *cnp;
6613148Sphk{
66283366Sjulian	struct thread *td = cnp->cn_thread;
66322521Sdyson	struct vnode *dp = 0;		/* the directory we are searching */
6643148Sphk	int docache;			/* == 0 do not cache last component */
6653148Sphk	int wantparent;			/* 1 => wantparent or lockparent flag */
6663148Sphk	int rdonly;			/* lookup read-only flag bit */
6673148Sphk	int error = 0;
6681541Srgrimes
6693148Sphk	/*
6703148Sphk	 * Setup: break out flag bits into variables.
6713148Sphk	 */
6723148Sphk	wantparent = cnp->cn_flags & (LOCKPARENT|WANTPARENT);
6733148Sphk	docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE;
6743148Sphk	if (cnp->cn_nameiop == DELETE ||
6753148Sphk	    (wantparent && cnp->cn_nameiop != CREATE))
6763148Sphk		docache = 0;
6773148Sphk	rdonly = cnp->cn_flags & RDONLY;
6783148Sphk	cnp->cn_flags &= ~ISSYMLINK;
6793148Sphk	dp = dvp;
68083366Sjulian	vn_lock(dp, LK_EXCLUSIVE | LK_RETRY, td);
6813148Sphk
6823148Sphk/* dirloop: */
6833148Sphk	/*
6843148Sphk	 * Search a new directory.
6853148Sphk	 *
6863148Sphk	 * The last component of the filename is left accessible via
6873148Sphk	 * cnp->cn_nameptr for callers that need the name. Callers needing
6883148Sphk	 * the name set the SAVENAME flag. When done, they assume
6893148Sphk	 * responsibility for freeing the pathname buffer.
6903148Sphk	 */
6913148Sphk#ifdef NAMEI_DIAGNOSTIC
6923148Sphk	printf("{%s}: ", cnp->cn_nameptr);
6933148Sphk#endif
6943148Sphk
6953148Sphk	/*
6963148Sphk	 * Check for degenerate name (e.g. / or "")
6973148Sphk	 * which is a way of talking about a directory,
6983148Sphk	 * e.g. like "/." or ".".
6993148Sphk	 */
7003148Sphk	if (cnp->cn_nameptr[0] == '\0') {
7013148Sphk		if (cnp->cn_nameiop != LOOKUP || wantparent) {
7023148Sphk			error = EISDIR;
7033148Sphk			goto bad;
7043148Sphk		}
7053148Sphk		if (dp->v_type != VDIR) {
7063148Sphk			error = ENOTDIR;
7073148Sphk			goto bad;
7083148Sphk		}
7093148Sphk		if (!(cnp->cn_flags & LOCKLEAF))
71083366Sjulian			VOP_UNLOCK(dp, 0, td);
7113148Sphk		*vpp = dp;
71254655Seivind		/* XXX This should probably move to the top of function. */
7133148Sphk		if (cnp->cn_flags & SAVESTART)
7143148Sphk			panic("lookup: SAVESTART");
7153148Sphk		return (0);
7163148Sphk	}
7173148Sphk
7183148Sphk	if (cnp->cn_flags & ISDOTDOT)
7193148Sphk		panic ("relookup: lookup on dot-dot");
7203148Sphk
7213148Sphk	/*
7223148Sphk	 * We now have a segment name to search for, and a directory to search.
7233148Sphk	 */
724138345Sphk#ifdef NAMEI_DIAGNOSTIC
725138345Sphk	vprint("search in:", dp);
726138345Sphk#endif
72743311Sdillon	if ((error = VOP_LOOKUP(dp, vpp, cnp)) != 0) {
72842408Seivind		KASSERT(*vpp == NULL, ("leaf should be empty"));
7293148Sphk		if (error != EJUSTRETURN)
7303148Sphk			goto bad;
7313148Sphk		/*
7323148Sphk		 * If creating and at end of pathname, then can consider
7333148Sphk		 * allowing file to be created.
7343148Sphk		 */
73511644Sdg		if (rdonly) {
7363148Sphk			error = EROFS;
7373148Sphk			goto bad;
7383148Sphk		}
7393148Sphk		/* ASSERT(dvp == ndp->ni_startdir) */
7403148Sphk		if (cnp->cn_flags & SAVESTART)
7413148Sphk			VREF(dvp);
7423148Sphk		/*
7433148Sphk		 * We return with ni_vp NULL to indicate that the entry
7443148Sphk		 * doesn't currently exist, leaving a pointer to the
7453148Sphk		 * (possibly locked) directory inode in ndp->ni_dvp.
7463148Sphk		 */
7473148Sphk		return (0);
7483148Sphk	}
7493148Sphk	dp = *vpp;
7503148Sphk
7513148Sphk	/*
7523148Sphk	 * Check for symbolic link
7533148Sphk	 */
75442408Seivind	KASSERT(dp->v_type != VLNK || !(cnp->cn_flags & FOLLOW),
75542453Seivind	    ("relookup: symlink found.\n"));
7563148Sphk
7573148Sphk	/*
75896755Strhodes	 * Disallow directory write attempts on read-only filesystems.
7593148Sphk	 */
76011644Sdg	if (rdonly &&
76111644Sdg	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
76211644Sdg		error = EROFS;
76311644Sdg		goto bad2;
7643148Sphk	}
7653148Sphk	/* ASSERT(dvp == ndp->ni_startdir) */
7663148Sphk	if (cnp->cn_flags & SAVESTART)
7673148Sphk		VREF(dvp);
76822521Sdyson
7693148Sphk	if (!wantparent)
7703148Sphk		vrele(dvp);
77132071Sdyson
77249101Salc	if (vn_canvmio(dp) == TRUE &&
77332286Sdyson		((cnp->cn_flags & (NOOBJ|LOCKLEAF)) == LOCKLEAF))
77483366Sjulian		vfs_object_create(dp, td, cnp->cn_cred);
77532071Sdyson
7763148Sphk	if ((cnp->cn_flags & LOCKLEAF) == 0)
77783366Sjulian		VOP_UNLOCK(dp, 0, td);
7783148Sphk	return (0);
7793148Sphk
7803148Sphkbad2:
7813148Sphk	if ((cnp->cn_flags & LOCKPARENT) && (cnp->cn_flags & ISLASTCN))
78283366Sjulian		VOP_UNLOCK(dvp, 0, td);
7833148Sphk	vrele(dvp);
7843148Sphkbad:
7853148Sphk	vput(dp);
7863148Sphk	*vpp = NULL;
7873148Sphk	return (error);
7883148Sphk}
789