vfs_lookup.c revision 144229
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 144229 2005-03-28 13:56:56Z jeff $");
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>
54141471Sjhb#include <sys/syscallsubr.h>
551541Srgrimes#ifdef KTRACE
561541Srgrimes#include <sys/ktrace.h>
571541Srgrimes#endif
581541Srgrimes
5992751Sjeff#include <vm/uma.h>
6032011Sbde
61138345Sphk#define NAMEI_DIAGNOSTIC 1
62138345Sphk#undef NAMEI_DIAGNOSTIC
63138345Sphk
641541Srgrimes/*
6569664Speter * Allocation zone for namei
6669664Speter */
6792751Sjeffuma_zone_t namei_zone;
6869664Speter
6969664Speterstatic void
7069664Speternameiinit(void *dummy __unused)
7169664Speter{
7292654Sjeff	namei_zone = uma_zcreate("NAMEI", MAXPATHLEN, NULL, NULL, NULL, NULL,
7392654Sjeff	    UMA_ALIGN_PTR, 0);
7469664Speter
7569664Speter}
7669664SpeterSYSINIT(vfs, SI_SUB_VFS, SI_ORDER_SECOND, nameiinit, NULL)
7769664Speter
7869664Speter/*
791541Srgrimes * Convert a pathname into a pointer to a locked inode.
801541Srgrimes *
811541Srgrimes * The FOLLOW flag is set when symbolic links are to be followed
821541Srgrimes * when they occur at the end of the name translation process.
831541Srgrimes * Symbolic links are always followed for all other pathname
841541Srgrimes * components other than the last.
851541Srgrimes *
861541Srgrimes * The segflg defines whether the name is to be copied from user
871541Srgrimes * space or kernel space.
881541Srgrimes *
891541Srgrimes * Overall outline of namei:
901541Srgrimes *
911541Srgrimes *	copy in name
921541Srgrimes *	get starting directory
931541Srgrimes *	while (!done && !error) {
941541Srgrimes *		call lookup to search path.
951541Srgrimes *		if symbolic link, massage name in buffer and continue
961541Srgrimes *	}
971541Srgrimes */
981541Srgrimesint
991541Srgrimesnamei(ndp)
1001541Srgrimes	register struct nameidata *ndp;
1011541Srgrimes{
1021541Srgrimes	register struct filedesc *fdp;	/* pointer to file descriptor state */
1031541Srgrimes	register char *cp;		/* pointer into pathname argument */
1041541Srgrimes	register struct vnode *dp;	/* the directory we are searching */
1051541Srgrimes	struct iovec aiov;		/* uio for reading symbolic links */
1061541Srgrimes	struct uio auio;
1071541Srgrimes	int error, linklen;
1081541Srgrimes	struct componentname *cnp = &ndp->ni_cnd;
10983366Sjulian	struct thread *td = cnp->cn_thread;
11083366Sjulian	struct proc *p = td->td_proc;
111140714Sjeff	struct mount *mp;
112140714Sjeff	int vfslocked;
1131541Srgrimes
11491419Sjhb	ndp->ni_cnd.cn_cred = ndp->ni_cnd.cn_thread->td_ucred;
11583366Sjulian	KASSERT(cnp->cn_cred && p, ("namei: bad cred/proc"));
11642408Seivind	KASSERT((cnp->cn_nameiop & (~OPMASK)) == 0,
11742453Seivind	    ("namei: nameiop contaminated with flags"));
11842408Seivind	KASSERT((cnp->cn_flags & OPMASK) == 0,
11942453Seivind	    ("namei: flags contaminated with nameiops"));
120144048Sjeff#ifndef LOOKUP_SHARED
121144048Sjeff	cnp->cn_flags &= ~LOCKSHARED;
122144048Sjeff#endif
12383366Sjulian	fdp = p->p_fd;
1241541Srgrimes
1251541Srgrimes	/*
1261541Srgrimes	 * Get a buffer for the name to be translated, and copy the
1271541Srgrimes	 * name into the buffer.
1281541Srgrimes	 */
1291541Srgrimes	if ((cnp->cn_flags & HASBUF) == 0)
130111119Simp		cnp->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK);
1311541Srgrimes	if (ndp->ni_segflg == UIO_SYSSPACE)
1321541Srgrimes		error = copystr(ndp->ni_dirp, cnp->cn_pnbuf,
13336735Sdfr			    MAXPATHLEN, (size_t *)&ndp->ni_pathlen);
1341541Srgrimes	else
1351541Srgrimes		error = copyinstr(ndp->ni_dirp, cnp->cn_pnbuf,
13636735Sdfr			    MAXPATHLEN, (size_t *)&ndp->ni_pathlen);
13720069Sbde
13820069Sbde	/*
13920069Sbde	 * Don't allow empty pathnames.
14020069Sbde	 */
14120069Sbde	if (!error && *cnp->cn_pnbuf == '\0')
14220069Sbde		error = ENOENT;
14320069Sbde
1441541Srgrimes	if (error) {
14592751Sjeff		uma_zfree(namei_zone, cnp->cn_pnbuf);
146100613Srwatson#ifdef DIAGNOSTIC
147100613Srwatson		cnp->cn_pnbuf = NULL;
148100613Srwatson		cnp->cn_nameptr = NULL;
149100613Srwatson#endif
1501541Srgrimes		ndp->ni_vp = NULL;
1511541Srgrimes		return (error);
1521541Srgrimes	}
1531541Srgrimes	ndp->ni_loopcnt = 0;
1541541Srgrimes#ifdef KTRACE
15597994Sjhb	if (KTRPOINT(td, KTR_NAMEI)) {
15697994Sjhb		KASSERT(cnp->cn_thread == curthread,
15797994Sjhb		    ("namei not using curthread"));
15897994Sjhb		ktrnamei(cnp->cn_pnbuf);
15997994Sjhb	}
1601541Srgrimes#endif
1611541Srgrimes
1621541Srgrimes	/*
1631541Srgrimes	 * Get starting point for the translation.
1641541Srgrimes	 */
16589306Salfred	FILEDESC_LOCK(fdp);
16633360Sdyson	ndp->ni_rootdir = fdp->fd_rdir;
16751649Sphk	ndp->ni_topdir = fdp->fd_jdir;
16833360Sdyson
1691541Srgrimes	dp = fdp->fd_cdir;
170140714Sjeff	vfslocked = VFS_LOCK_GIANT(dp->v_mount);
1711541Srgrimes	VREF(dp);
17289306Salfred	FILEDESC_UNLOCK(fdp);
1731541Srgrimes	for (;;) {
1741541Srgrimes		/*
1751541Srgrimes		 * Check if root directory should replace current directory.
1761541Srgrimes		 * Done at start of translation and after symbolic link.
1771541Srgrimes		 */
1781541Srgrimes		cnp->cn_nameptr = cnp->cn_pnbuf;
1791541Srgrimes		if (*(cnp->cn_nameptr) == '/') {
1801541Srgrimes			vrele(dp);
181140714Sjeff			VFS_UNLOCK_GIANT(vfslocked);
1821541Srgrimes			while (*(cnp->cn_nameptr) == '/') {
1831541Srgrimes				cnp->cn_nameptr++;
1841541Srgrimes				ndp->ni_pathlen--;
1851541Srgrimes			}
1861541Srgrimes			dp = ndp->ni_rootdir;
187140714Sjeff			vfslocked = VFS_LOCK_GIANT(dp->v_mount);
1881541Srgrimes			VREF(dp);
1891541Srgrimes		}
190140714Sjeff		if (vfslocked)
191140714Sjeff			ndp->ni_cnd.cn_flags |= GIANTHELD;
1921541Srgrimes		ndp->ni_startdir = dp;
1933148Sphk		error = lookup(ndp);
1943148Sphk		if (error) {
19592751Sjeff			uma_zfree(namei_zone, cnp->cn_pnbuf);
196100613Srwatson#ifdef DIAGNOSTIC
197100613Srwatson			cnp->cn_pnbuf = NULL;
198100613Srwatson			cnp->cn_nameptr = NULL;
199100613Srwatson#endif
2001541Srgrimes			return (error);
2011541Srgrimes		}
202140714Sjeff		vfslocked = (ndp->ni_cnd.cn_flags & GIANTHELD) != 0;
203140714Sjeff		ndp->ni_cnd.cn_flags &= ~GIANTHELD;
2041541Srgrimes		/*
2051541Srgrimes		 * Check for symbolic link
2061541Srgrimes		 */
2071541Srgrimes		if ((cnp->cn_flags & ISSYMLINK) == 0) {
208100613Srwatson			if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0) {
20992751Sjeff				uma_zfree(namei_zone, cnp->cn_pnbuf);
210100613Srwatson#ifdef DIAGNOSTIC
211100613Srwatson				cnp->cn_pnbuf = NULL;
212100613Srwatson				cnp->cn_nameptr = NULL;
213100613Srwatson#endif
214100613Srwatson			} else
2151541Srgrimes				cnp->cn_flags |= HASBUF;
21632286Sdyson
217140714Sjeff			if ((cnp->cn_flags & MPSAFE) == 0) {
218140714Sjeff				VFS_UNLOCK_GIANT(vfslocked);
219140714Sjeff			} else if (vfslocked)
220140714Sjeff				ndp->ni_cnd.cn_flags |= GIANTHELD;
2211541Srgrimes			return (0);
2221541Srgrimes		}
2231541Srgrimes		if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1)
22483366Sjulian			VOP_UNLOCK(ndp->ni_dvp, 0, td);
2251541Srgrimes		if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
2261541Srgrimes			error = ELOOP;
2271541Srgrimes			break;
2281541Srgrimes		}
229101127Srwatson#ifdef MAC
230105479Srwatson		if ((cnp->cn_flags & NOMACCHECK) == 0) {
231105479Srwatson			error = mac_check_vnode_readlink(td->td_ucred,
232105479Srwatson			    ndp->ni_vp);
233105479Srwatson			if (error)
234105479Srwatson				break;
235105479Srwatson		}
236101127Srwatson#endif
2371541Srgrimes		if (ndp->ni_pathlen > 1)
238111119Simp			cp = uma_zalloc(namei_zone, M_WAITOK);
2391541Srgrimes		else
2401541Srgrimes			cp = cnp->cn_pnbuf;
2411541Srgrimes		aiov.iov_base = cp;
2421541Srgrimes		aiov.iov_len = MAXPATHLEN;
2431541Srgrimes		auio.uio_iov = &aiov;
2441541Srgrimes		auio.uio_iovcnt = 1;
2451541Srgrimes		auio.uio_offset = 0;
2461541Srgrimes		auio.uio_rw = UIO_READ;
2471541Srgrimes		auio.uio_segflg = UIO_SYSSPACE;
24883366Sjulian		auio.uio_td = (struct thread *)0;
2491541Srgrimes		auio.uio_resid = MAXPATHLEN;
2503148Sphk		error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
2513148Sphk		if (error) {
2521541Srgrimes			if (ndp->ni_pathlen > 1)
25392751Sjeff				uma_zfree(namei_zone, cp);
2541541Srgrimes			break;
2551541Srgrimes		}
2561541Srgrimes		linklen = MAXPATHLEN - auio.uio_resid;
25778692Sdillon		if (linklen == 0) {
25878692Sdillon			if (ndp->ni_pathlen > 1)
25992751Sjeff				uma_zfree(namei_zone, cp);
26078692Sdillon			error = ENOENT;
26178692Sdillon			break;
26278692Sdillon		}
2631541Srgrimes		if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
2641541Srgrimes			if (ndp->ni_pathlen > 1)
26592751Sjeff				uma_zfree(namei_zone, cp);
2661541Srgrimes			error = ENAMETOOLONG;
2671541Srgrimes			break;
2681541Srgrimes		}
2691541Srgrimes		if (ndp->ni_pathlen > 1) {
2701541Srgrimes			bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
27192751Sjeff			uma_zfree(namei_zone, cnp->cn_pnbuf);
2721541Srgrimes			cnp->cn_pnbuf = cp;
2731541Srgrimes		} else
2741541Srgrimes			cnp->cn_pnbuf[linklen] = '\0';
2751541Srgrimes		ndp->ni_pathlen += linklen;
2761541Srgrimes		vput(ndp->ni_vp);
2771541Srgrimes		dp = ndp->ni_dvp;
2781541Srgrimes	}
27992751Sjeff	uma_zfree(namei_zone, cnp->cn_pnbuf);
280100613Srwatson#ifdef DIAGNOSTIC
281100613Srwatson	cnp->cn_pnbuf = NULL;
282100613Srwatson	cnp->cn_nameptr = NULL;
283100613Srwatson#endif
2841541Srgrimes	vrele(ndp->ni_dvp);
285140714Sjeff	mp = ndp->ni_vp->v_mount;
2861541Srgrimes	vput(ndp->ni_vp);
287140714Sjeff	VFS_UNLOCK_GIANT(vfslocked);
2881541Srgrimes	ndp->ni_vp = NULL;
2891541Srgrimes	return (error);
2901541Srgrimes}
2911541Srgrimes
2921541Srgrimes/*
2931541Srgrimes * Search a pathname.
2941541Srgrimes * This is a very central and rather complicated routine.
2951541Srgrimes *
2961541Srgrimes * The pathname is pointed to by ni_ptr and is of length ni_pathlen.
2971541Srgrimes * The starting directory is taken from ni_startdir. The pathname is
2981541Srgrimes * descended until done, or a symbolic link is encountered. The variable
2991541Srgrimes * ni_more is clear if the path is completed; it is set to one if a
3001541Srgrimes * symbolic link needing interpretation is encountered.
3011541Srgrimes *
3021541Srgrimes * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
3031541Srgrimes * whether the name is to be looked up, created, renamed, or deleted.
3041541Srgrimes * When CREATE, RENAME, or DELETE is specified, information usable in
3051541Srgrimes * creating, renaming, or deleting a directory entry may be calculated.
3061541Srgrimes * If flag has LOCKPARENT or'ed into it, the parent directory is returned
3071541Srgrimes * locked. If flag has WANTPARENT or'ed into it, the parent directory is
3081541Srgrimes * returned unlocked. Otherwise the parent directory is not returned. If
3091541Srgrimes * the target of the pathname exists and LOCKLEAF is or'ed into the flag
3101541Srgrimes * the target is returned locked, otherwise it is returned unlocked.
3111541Srgrimes * When creating or renaming and LOCKPARENT is specified, the target may not
3121541Srgrimes * be ".".  When deleting and LOCKPARENT is specified, the target may be ".".
3138876Srgrimes *
3141541Srgrimes * Overall outline of lookup:
3151541Srgrimes *
3161541Srgrimes * dirloop:
3171541Srgrimes *	identify next component of name at ndp->ni_ptr
3181541Srgrimes *	handle degenerate case where name is null string
3191541Srgrimes *	if .. and crossing mount points and on mounted filesys, find parent
3201541Srgrimes *	call VOP_LOOKUP routine for next component name
3211541Srgrimes *	    directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set
3221541Srgrimes *	    component vnode returned in ni_vp (if it exists), locked.
3231541Srgrimes *	if result vnode is mounted on and crossing mount points,
3241541Srgrimes *	    find mounted on vnode
3251541Srgrimes *	if more components of name, do next level at dirloop
3261541Srgrimes *	return the answer in ni_vp, locked if LOCKLEAF set
3271541Srgrimes *	    if LOCKPARENT set, return locked parent in ni_dvp
3281541Srgrimes *	    if WANTPARENT set, return unlocked parent in ni_dvp
3291541Srgrimes */
3301541Srgrimesint
3311541Srgrimeslookup(ndp)
3321541Srgrimes	register struct nameidata *ndp;
3331541Srgrimes{
3341541Srgrimes	register char *cp;		/* pointer into pathname argument */
3351541Srgrimes	register struct vnode *dp = 0;	/* the directory we are searching */
3361541Srgrimes	struct vnode *tdp;		/* saved dp */
3371541Srgrimes	struct mount *mp;		/* mount table entry */
3381541Srgrimes	int docache;			/* == 0 do not cache last component */
3391541Srgrimes	int wantparent;			/* 1 => wantparent or lockparent flag */
3401541Srgrimes	int rdonly;			/* lookup read-only flag bit */
3419804Sbde	int trailing_slash;
3421541Srgrimes	int error = 0;
34365805Sbp	int dpunlocked = 0;		/* dp has already been unlocked */
3441541Srgrimes	struct componentname *cnp = &ndp->ni_cnd;
34583366Sjulian	struct thread *td = cnp->cn_thread;
346140714Sjeff	int vfslocked;
347140714Sjeff	int tvfslocked;
3481541Srgrimes
3491541Srgrimes	/*
3501541Srgrimes	 * Setup: break out flag bits into variables.
3511541Srgrimes	 */
352140714Sjeff	vfslocked = (ndp->ni_cnd.cn_flags & GIANTHELD) != 0;
353140714Sjeff	ndp->ni_cnd.cn_flags &= ~GIANTHELD;
3541541Srgrimes	wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT);
355144229Sjeff	KASSERT(cnp->cn_nameiop == LOOKUP || wantparent,
356144229Sjeff	    ("CREATE, DELETE, RENAME require LOCKPARENT or WANTPARENT."));
3571541Srgrimes	docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE;
3581541Srgrimes	if (cnp->cn_nameiop == DELETE ||
35922874Sbde	    (wantparent && cnp->cn_nameiop != CREATE &&
36022874Sbde	     cnp->cn_nameiop != LOOKUP))
3611541Srgrimes		docache = 0;
3621541Srgrimes	rdonly = cnp->cn_flags & RDONLY;
3631541Srgrimes	ndp->ni_dvp = NULL;
3641541Srgrimes	cnp->cn_flags &= ~ISSYMLINK;
3651541Srgrimes	dp = ndp->ni_startdir;
3661541Srgrimes	ndp->ni_startdir = NULLVP;
36783366Sjulian	vn_lock(dp, LK_EXCLUSIVE | LK_RETRY, td);
3681541Srgrimes
3691541Srgrimesdirloop:
3701541Srgrimes	/*
3711541Srgrimes	 * Search a new directory.
3721541Srgrimes	 *
3731541Srgrimes	 * The last component of the filename is left accessible via
3741541Srgrimes	 * cnp->cn_nameptr for callers that need the name. Callers needing
3751541Srgrimes	 * the name set the SAVENAME flag. When done, they assume
3761541Srgrimes	 * responsibility for freeing the pathname buffer.
3771541Srgrimes	 */
3781541Srgrimes	cnp->cn_consume = 0;
3791541Srgrimes	for (cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++)
38051906Sphk		continue;
3811541Srgrimes	cnp->cn_namelen = cp - cnp->cn_nameptr;
3821541Srgrimes	if (cnp->cn_namelen > NAME_MAX) {
3831541Srgrimes		error = ENAMETOOLONG;
3841541Srgrimes		goto bad;
3851541Srgrimes	}
3861541Srgrimes#ifdef NAMEI_DIAGNOSTIC
3871541Srgrimes	{ char c = *cp;
3881541Srgrimes	*cp = '\0';
3891541Srgrimes	printf("{%s}: ", cnp->cn_nameptr);
3901541Srgrimes	*cp = c; }
3911541Srgrimes#endif
3921541Srgrimes	ndp->ni_pathlen -= cnp->cn_namelen;
3931541Srgrimes	ndp->ni_next = cp;
3949804Sbde
3959804Sbde	/*
3969804Sbde	 * Replace multiple slashes by a single slash and trailing slashes
3979804Sbde	 * by a null.  This must be done before VOP_LOOKUP() because some
3989804Sbde	 * fs's don't know about trailing slashes.  Remember if there were
3999804Sbde	 * trailing slashes to handle symlinks, existing non-directories
4009804Sbde	 * and non-existing files that won't be directories specially later.
4019804Sbde	 */
4029804Sbde	trailing_slash = 0;
4039804Sbde	while (*cp == '/' && (cp[1] == '/' || cp[1] == '\0')) {
4049804Sbde		cp++;
4059804Sbde		ndp->ni_pathlen--;
4069804Sbde		if (*cp == '\0') {
4079804Sbde			trailing_slash = 1;
4089804Sbde			*ndp->ni_next = '\0';	/* XXX for direnter() ... */
4099804Sbde		}
4109804Sbde	}
4119804Sbde	ndp->ni_next = cp;
4129804Sbde
4131541Srgrimes	cnp->cn_flags |= MAKEENTRY;
4141541Srgrimes	if (*cp == '\0' && docache == 0)
4151541Srgrimes		cnp->cn_flags &= ~MAKEENTRY;
4161541Srgrimes	if (cnp->cn_namelen == 2 &&
4171541Srgrimes	    cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.')
4181541Srgrimes		cnp->cn_flags |= ISDOTDOT;
4191541Srgrimes	else
4201541Srgrimes		cnp->cn_flags &= ~ISDOTDOT;
4211541Srgrimes	if (*ndp->ni_next == 0)
4221541Srgrimes		cnp->cn_flags |= ISLASTCN;
4231541Srgrimes	else
4241541Srgrimes		cnp->cn_flags &= ~ISLASTCN;
4251541Srgrimes
4261541Srgrimes
4271541Srgrimes	/*
4281541Srgrimes	 * Check for degenerate name (e.g. / or "")
4291541Srgrimes	 * which is a way of talking about a directory,
4301541Srgrimes	 * e.g. like "/." or ".".
4311541Srgrimes	 */
4321541Srgrimes	if (cnp->cn_nameptr[0] == '\0') {
43322521Sdyson		if (dp->v_type != VDIR) {
43422521Sdyson			error = ENOTDIR;
43522521Sdyson			goto bad;
43622521Sdyson		}
4371541Srgrimes		if (cnp->cn_nameiop != LOOKUP) {
4381541Srgrimes			error = EISDIR;
4391541Srgrimes			goto bad;
4401541Srgrimes		}
4411541Srgrimes		if (wantparent) {
4421541Srgrimes			ndp->ni_dvp = dp;
4431541Srgrimes			VREF(dp);
4441541Srgrimes		}
4451541Srgrimes		ndp->ni_vp = dp;
4461541Srgrimes		if (!(cnp->cn_flags & (LOCKPARENT | LOCKLEAF)))
44783366Sjulian			VOP_UNLOCK(dp, 0, td);
44854655Seivind		/* XXX This should probably move to the top of function. */
4491541Srgrimes		if (cnp->cn_flags & SAVESTART)
4501541Srgrimes			panic("lookup: SAVESTART");
451140714Sjeff		goto success;
4521541Srgrimes	}
4531541Srgrimes
4541541Srgrimes	/*
4551541Srgrimes	 * Handle "..": two special cases.
4561541Srgrimes	 * 1. If at root directory (e.g. after chroot)
4571541Srgrimes	 *    or at absolute root directory
4581541Srgrimes	 *    then ignore it so can't get out.
4591541Srgrimes	 * 2. If this vnode is the root of a mounted
4601541Srgrimes	 *    filesystem, then replace it with the
4611541Srgrimes	 *    vnode which was mounted on so we take the
46296755Strhodes	 *    .. in the other filesystem.
46351649Sphk	 * 3. If the vnode is the top directory of
46451649Sphk	 *    the jail or chroot, don't let them out.
4651541Srgrimes	 */
4661541Srgrimes	if (cnp->cn_flags & ISDOTDOT) {
4671541Srgrimes		for (;;) {
46851649Sphk			if (dp == ndp->ni_rootdir ||
46951649Sphk			    dp == ndp->ni_topdir ||
47051649Sphk			    dp == rootvnode) {
4711541Srgrimes				ndp->ni_dvp = dp;
4721541Srgrimes				ndp->ni_vp = dp;
4731541Srgrimes				VREF(dp);
4741541Srgrimes				goto nextname;
4751541Srgrimes			}
476101308Sjeff			if ((dp->v_vflag & VV_ROOT) == 0 ||
4771541Srgrimes			    (cnp->cn_flags & NOCROSSMOUNT))
4781541Srgrimes				break;
47969405Salfred			if (dp->v_mount == NULL) {	/* forced unmount */
48069405Salfred				error = EBADF;
48169405Salfred				goto bad;
48269405Salfred			}
4831541Srgrimes			tdp = dp;
484140714Sjeff			tvfslocked = vfslocked;
4851541Srgrimes			dp = dp->v_mount->mnt_vnodecovered;
4861541Srgrimes			vput(tdp);
487140714Sjeff			vfslocked = VFS_LOCK_GIANT(dp->v_mount);
488140714Sjeff			VFS_UNLOCK_GIANT(tvfslocked);
4891541Srgrimes			VREF(dp);
49083366Sjulian			vn_lock(dp, LK_EXCLUSIVE | LK_RETRY, td);
4911541Srgrimes		}
4921541Srgrimes	}
4931541Srgrimes
4941541Srgrimes	/*
4951541Srgrimes	 * We now have a segment name to search for, and a directory to search.
4961541Srgrimes	 */
4971541Srgrimesunionlookup:
498101127Srwatson#ifdef MAC
499105479Srwatson	if ((cnp->cn_flags & NOMACCHECK) == 0) {
500105479Srwatson		error = mac_check_vnode_lookup(td->td_ucred, dp, cnp);
501105479Srwatson		if (error)
502105479Srwatson			goto bad;
503105479Srwatson	}
504101127Srwatson#endif
5051541Srgrimes	ndp->ni_dvp = dp;
50622521Sdyson	ndp->ni_vp = NULL;
50724624Sdfr	ASSERT_VOP_LOCKED(dp, "lookup");
508138345Sphk#ifdef NAMEI_DIAGNOSTIC
509138345Sphk	vprint("lookup in", dp);
510138345Sphk#endif
51143301Sdillon	if ((error = VOP_LOOKUP(dp, &ndp->ni_vp, cnp)) != 0) {
51242408Seivind		KASSERT(ndp->ni_vp == NULL, ("leaf should be empty"));
5131541Srgrimes#ifdef NAMEI_DIAGNOSTIC
5141541Srgrimes		printf("not found\n");
5151541Srgrimes#endif
5161541Srgrimes		if ((error == ENOENT) &&
517101308Sjeff		    (dp->v_vflag & VV_ROOT) && (dp->v_mount != NULL) &&
5181541Srgrimes		    (dp->v_mount->mnt_flag & MNT_UNION)) {
5191541Srgrimes			tdp = dp;
520140714Sjeff			tvfslocked = vfslocked;
5211541Srgrimes			dp = dp->v_mount->mnt_vnodecovered;
522144203Sjeff			vput(tdp);
523140714Sjeff			vfslocked = VFS_LOCK_GIANT(dp->v_mount);
524140714Sjeff			VFS_UNLOCK_GIANT(tvfslocked);
5251541Srgrimes			VREF(dp);
52683366Sjulian			vn_lock(dp, LK_EXCLUSIVE | LK_RETRY, td);
5271541Srgrimes			goto unionlookup;
5281541Srgrimes		}
5291541Srgrimes
5301541Srgrimes		if (error != EJUSTRETURN)
5311541Srgrimes			goto bad;
5321541Srgrimes		/*
5331541Srgrimes		 * If creating and at end of pathname, then can consider
5341541Srgrimes		 * allowing file to be created.
5351541Srgrimes		 */
53611644Sdg		if (rdonly) {
5371541Srgrimes			error = EROFS;
5381541Srgrimes			goto bad;
5391541Srgrimes		}
5409804Sbde		if (*cp == '\0' && trailing_slash &&
5419804Sbde		     !(cnp->cn_flags & WILLBEDIR)) {
5429804Sbde			error = ENOENT;
5439804Sbde			goto bad;
5449804Sbde		}
545144203Sjeff		if ((cnp->cn_flags & LOCKPARENT) == 0)
546144203Sjeff			VOP_UNLOCK(dp, 0, td);
5471541Srgrimes		/*
548144203Sjeff		 * This is a temporary assert to make sure I know what the
549144203Sjeff		 * behavior here was.
550144203Sjeff		 */
551144203Sjeff		KASSERT((cnp->cn_flags & (WANTPARENT|LOCKPARENT)) != 0,
552144203Sjeff		   ("lookup: Unhandled case."));
553144203Sjeff		/*
5541541Srgrimes		 * We return with ni_vp NULL to indicate that the entry
5551541Srgrimes		 * doesn't currently exist, leaving a pointer to the
5561541Srgrimes		 * (possibly locked) directory inode in ndp->ni_dvp.
5571541Srgrimes		 */
5581541Srgrimes		if (cnp->cn_flags & SAVESTART) {
5591541Srgrimes			ndp->ni_startdir = ndp->ni_dvp;
5601541Srgrimes			VREF(ndp->ni_startdir);
5611541Srgrimes		}
562140714Sjeff		goto success;
5631541Srgrimes	}
5641541Srgrimes#ifdef NAMEI_DIAGNOSTIC
5651541Srgrimes	printf("found\n");
5661541Srgrimes#endif
567144203Sjeff	/*
568144203Sjeff	 * In the DOTDOT case dp is unlocked, we may have to relock it if
569144203Sjeff	 * this is the parent of the last component.  Otherwise, we have to
570144203Sjeff	 * unlock if this is not the last component, or if it is and
571144203Sjeff	 * LOCKPARENT is set.
572144203Sjeff	 */
573144203Sjeff	if (cnp->cn_flags & ISDOTDOT) {
574144203Sjeff		if ((cnp->cn_flags & (ISLASTCN | LOCKPARENT)) ==
575144203Sjeff		    (ISLASTCN | LOCKPARENT))
576144203Sjeff			vn_lock(dp, LK_EXCLUSIVE | LK_RETRY, td);
577144203Sjeff	} else if (dp != ndp->ni_vp) {
578144203Sjeff		if ((cnp->cn_flags & (ISLASTCN | LOCKPARENT)) == ISLASTCN)
579144203Sjeff			VOP_UNLOCK(dp, 0, td);
580144203Sjeff		else if ((cnp->cn_flags & ISLASTCN) == 0)
581144203Sjeff			VOP_UNLOCK(dp, 0, td);
582144203Sjeff	}
5831541Srgrimes
5841541Srgrimes	/*
5851541Srgrimes	 * Take into account any additional components consumed by
5861541Srgrimes	 * the underlying filesystem.
5871541Srgrimes	 */
5881541Srgrimes	if (cnp->cn_consume > 0) {
5891541Srgrimes		cnp->cn_nameptr += cnp->cn_consume;
5901541Srgrimes		ndp->ni_next += cnp->cn_consume;
5911541Srgrimes		ndp->ni_pathlen -= cnp->cn_consume;
5921541Srgrimes		cnp->cn_consume = 0;
5931541Srgrimes	}
5941541Srgrimes
5951541Srgrimes	dp = ndp->ni_vp;
5961541Srgrimes
5971541Srgrimes	/*
5981541Srgrimes	 * Check to see if the vnode has been mounted on;
59996755Strhodes	 * if so find the root of the mounted filesystem.
6001541Srgrimes	 */
6011541Srgrimes	while (dp->v_type == VDIR && (mp = dp->v_mountedhere) &&
6021541Srgrimes	       (cnp->cn_flags & NOCROSSMOUNT) == 0) {
60383366Sjulian		if (vfs_busy(mp, 0, 0, td))
6041541Srgrimes			continue;
60583366Sjulian		VOP_UNLOCK(dp, 0, td);
606140714Sjeff		tvfslocked = VFS_LOCK_GIANT(mp);
607144055Sjeff		error = VFS_ROOT(mp, LK_EXCLUSIVE, &tdp, td);
60883366Sjulian		vfs_unbusy(mp, td);
60965805Sbp		if (error) {
610140714Sjeff			VFS_UNLOCK_GIANT(tvfslocked);
61165805Sbp			dpunlocked = 1;
6121541Srgrimes			goto bad2;
61365805Sbp		}
61465805Sbp		vrele(dp);
615140714Sjeff		VFS_UNLOCK_GIANT(vfslocked);
6161541Srgrimes		ndp->ni_vp = dp = tdp;
617140714Sjeff		vfslocked = tvfslocked;
6181541Srgrimes	}
6191541Srgrimes
62010219Sdfr	/*
62110219Sdfr	 * Check for symbolic link
62210219Sdfr	 */
62310219Sdfr	if ((dp->v_type == VLNK) &&
62410219Sdfr	    ((cnp->cn_flags & FOLLOW) || trailing_slash ||
62510219Sdfr	     *ndp->ni_next == '/')) {
62610219Sdfr		cnp->cn_flags |= ISSYMLINK;
62769405Salfred		if (dp->v_mount == NULL) {
62869405Salfred			/* We can't know whether the directory was mounted with
62969405Salfred			 * NOSYMFOLLOW, so we can't follow safely. */
63069405Salfred			error = EBADF;
63169405Salfred			goto bad2;
63269405Salfred		}
63335105Swosch		if (dp->v_mount->mnt_flag & MNT_NOSYMFOLLOW) {
63435105Swosch			error = EACCES;
63535105Swosch			goto bad2;
63635105Swosch		}
637140714Sjeff		goto success;
63810219Sdfr	}
63910219Sdfr
64010219Sdfr	/*
64110219Sdfr	 * Check for bogus trailing slashes.
64210219Sdfr	 */
64310219Sdfr	if (trailing_slash && dp->v_type != VDIR) {
64410219Sdfr		error = ENOTDIR;
64510219Sdfr		goto bad2;
64610219Sdfr	}
64710219Sdfr
6481541Srgrimesnextname:
6491541Srgrimes	/*
6501541Srgrimes	 * Not a symbolic link.  If more pathname,
6511541Srgrimes	 * continue at next component, else return.
6521541Srgrimes	 */
653144203Sjeff	KASSERT((cnp->cn_flags & ISLASTCN) || *ndp->ni_next == '/',
654144203Sjeff	    ("lookup: invalid path state."));
6551541Srgrimes	if (*ndp->ni_next == '/') {
6561541Srgrimes		cnp->cn_nameptr = ndp->ni_next;
6571541Srgrimes		while (*cnp->cn_nameptr == '/') {
6581541Srgrimes			cnp->cn_nameptr++;
6591541Srgrimes			ndp->ni_pathlen--;
6601541Srgrimes		}
66154655Seivind		if (ndp->ni_dvp != ndp->ni_vp)
66254655Seivind			ASSERT_VOP_UNLOCKED(ndp->ni_dvp, "lookup");
6631541Srgrimes		vrele(ndp->ni_dvp);
6641541Srgrimes		goto dirloop;
6651541Srgrimes	}
6661541Srgrimes	/*
66796755Strhodes	 * Disallow directory write attempts on read-only filesystems.
6681541Srgrimes	 */
66911644Sdg	if (rdonly &&
67011644Sdg	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
67111644Sdg		error = EROFS;
67211644Sdg		goto bad2;
6731541Srgrimes	}
6741541Srgrimes	if (cnp->cn_flags & SAVESTART) {
6751541Srgrimes		ndp->ni_startdir = ndp->ni_dvp;
6761541Srgrimes		VREF(ndp->ni_startdir);
6771541Srgrimes	}
6781541Srgrimes	if (!wantparent)
6791541Srgrimes		vrele(ndp->ni_dvp);
68032071Sdyson
6811541Srgrimes	if ((cnp->cn_flags & LOCKLEAF) == 0)
68283366Sjulian		VOP_UNLOCK(dp, 0, td);
683140714Sjeffsuccess:
684140714Sjeff	if (vfslocked)
685140714Sjeff		ndp->ni_cnd.cn_flags |= GIANTHELD;
6861541Srgrimes	return (0);
6871541Srgrimes
6881541Srgrimesbad2:
689144203Sjeff	if ((cnp->cn_flags & LOCKPARENT) && *ndp->ni_next == '\0')
690144203Sjeff		vput(ndp->ni_dvp);
691144203Sjeff	else
692144203Sjeff		vrele(ndp->ni_dvp);
6931541Srgrimesbad:
69465805Sbp	if (dpunlocked)
69565805Sbp		vrele(dp);
69665805Sbp	else
69765805Sbp		vput(dp);
698140714Sjeff	VFS_UNLOCK_GIANT(vfslocked);
699140714Sjeff	ndp->ni_cnd.cn_flags &= ~GIANTHELD;
7001541Srgrimes	ndp->ni_vp = NULL;
7011541Srgrimes	return (error);
7021541Srgrimes}
7031541Srgrimes
7043148Sphk/*
7053148Sphk * relookup - lookup a path name component
7063148Sphk *    Used by lookup to re-aquire things.
7073148Sphk */
7083148Sphkint
7093148Sphkrelookup(dvp, vpp, cnp)
7103148Sphk	struct vnode *dvp, **vpp;
7113148Sphk	struct componentname *cnp;
7123148Sphk{
71383366Sjulian	struct thread *td = cnp->cn_thread;
71422521Sdyson	struct vnode *dp = 0;		/* the directory we are searching */
7153148Sphk	int wantparent;			/* 1 => wantparent or lockparent flag */
7163148Sphk	int rdonly;			/* lookup read-only flag bit */
7173148Sphk	int error = 0;
7181541Srgrimes
719144203Sjeff	KASSERT(cnp->cn_flags & ISLASTCN,
720144203Sjeff	    ("relookup: Not given last component."));
7213148Sphk	/*
7223148Sphk	 * Setup: break out flag bits into variables.
7233148Sphk	 */
7243148Sphk	wantparent = cnp->cn_flags & (LOCKPARENT|WANTPARENT);
7253148Sphk	rdonly = cnp->cn_flags & RDONLY;
7263148Sphk	cnp->cn_flags &= ~ISSYMLINK;
7273148Sphk	dp = dvp;
72883366Sjulian	vn_lock(dp, LK_EXCLUSIVE | LK_RETRY, td);
7293148Sphk
7303148Sphk	/*
7313148Sphk	 * Search a new directory.
7323148Sphk	 *
7333148Sphk	 * The last component of the filename is left accessible via
7343148Sphk	 * cnp->cn_nameptr for callers that need the name. Callers needing
7353148Sphk	 * the name set the SAVENAME flag. When done, they assume
7363148Sphk	 * responsibility for freeing the pathname buffer.
7373148Sphk	 */
7383148Sphk#ifdef NAMEI_DIAGNOSTIC
7393148Sphk	printf("{%s}: ", cnp->cn_nameptr);
7403148Sphk#endif
7413148Sphk
7423148Sphk	/*
7433148Sphk	 * Check for degenerate name (e.g. / or "")
7443148Sphk	 * which is a way of talking about a directory,
7453148Sphk	 * e.g. like "/." or ".".
7463148Sphk	 */
7473148Sphk	if (cnp->cn_nameptr[0] == '\0') {
7483148Sphk		if (cnp->cn_nameiop != LOOKUP || wantparent) {
7493148Sphk			error = EISDIR;
7503148Sphk			goto bad;
7513148Sphk		}
7523148Sphk		if (dp->v_type != VDIR) {
7533148Sphk			error = ENOTDIR;
7543148Sphk			goto bad;
7553148Sphk		}
7563148Sphk		if (!(cnp->cn_flags & LOCKLEAF))
75783366Sjulian			VOP_UNLOCK(dp, 0, td);
7583148Sphk		*vpp = dp;
75954655Seivind		/* XXX This should probably move to the top of function. */
7603148Sphk		if (cnp->cn_flags & SAVESTART)
7613148Sphk			panic("lookup: SAVESTART");
7623148Sphk		return (0);
7633148Sphk	}
7643148Sphk
7653148Sphk	if (cnp->cn_flags & ISDOTDOT)
7663148Sphk		panic ("relookup: lookup on dot-dot");
7673148Sphk
7683148Sphk	/*
7693148Sphk	 * We now have a segment name to search for, and a directory to search.
7703148Sphk	 */
771138345Sphk#ifdef NAMEI_DIAGNOSTIC
772138345Sphk	vprint("search in:", dp);
773138345Sphk#endif
77443311Sdillon	if ((error = VOP_LOOKUP(dp, vpp, cnp)) != 0) {
77542408Seivind		KASSERT(*vpp == NULL, ("leaf should be empty"));
7763148Sphk		if (error != EJUSTRETURN)
7773148Sphk			goto bad;
7783148Sphk		/*
7793148Sphk		 * If creating and at end of pathname, then can consider
7803148Sphk		 * allowing file to be created.
7813148Sphk		 */
78211644Sdg		if (rdonly) {
7833148Sphk			error = EROFS;
7843148Sphk			goto bad;
7853148Sphk		}
7863148Sphk		/* ASSERT(dvp == ndp->ni_startdir) */
7873148Sphk		if (cnp->cn_flags & SAVESTART)
7883148Sphk			VREF(dvp);
789144203Sjeff		if ((cnp->cn_flags & LOCKPARENT) == 0)
790144203Sjeff			VOP_UNLOCK(dp, 0, td);
7913148Sphk		/*
792144203Sjeff		 * This is a temporary assert to make sure I know what the
793144203Sjeff		 * behavior here was.
794144203Sjeff		 */
795144203Sjeff		KASSERT((cnp->cn_flags & (WANTPARENT|LOCKPARENT)) != 0,
796144203Sjeff		   ("relookup: Unhandled case."));
797144203Sjeff		/*
7983148Sphk		 * We return with ni_vp NULL to indicate that the entry
7993148Sphk		 * doesn't currently exist, leaving a pointer to the
8003148Sphk		 * (possibly locked) directory inode in ndp->ni_dvp.
8013148Sphk		 */
8023148Sphk		return (0);
8033148Sphk	}
804144203Sjeff	/*
805144203Sjeff	 * In the DOTDOT case dp is unlocked, we may have to relock it if
806144203Sjeff	 * LOCKPARENT is set.  Otherwise, unlock the parent.
807144203Sjeff	 */
808144203Sjeff	if ((cnp->cn_flags & (ISDOTDOT | LOCKPARENT)) ==
809144203Sjeff	    (ISDOTDOT | LOCKPARENT))
810144203Sjeff		vn_lock(dp, LK_EXCLUSIVE | LK_RETRY, td);
811144203Sjeff	else if ((cnp->cn_flags & (ISDOTDOT | LOCKPARENT)) == 0 && dp != *vpp)
812144203Sjeff		VOP_UNLOCK(dp, 0, td);
8133148Sphk	dp = *vpp;
8143148Sphk
8153148Sphk	/*
8163148Sphk	 * Check for symbolic link
8173148Sphk	 */
81842408Seivind	KASSERT(dp->v_type != VLNK || !(cnp->cn_flags & FOLLOW),
81942453Seivind	    ("relookup: symlink found.\n"));
8203148Sphk
8213148Sphk	/*
82296755Strhodes	 * Disallow directory write attempts on read-only filesystems.
8233148Sphk	 */
82411644Sdg	if (rdonly &&
82511644Sdg	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
82611644Sdg		error = EROFS;
82711644Sdg		goto bad2;
8283148Sphk	}
8293148Sphk	/* ASSERT(dvp == ndp->ni_startdir) */
8303148Sphk	if (cnp->cn_flags & SAVESTART)
8313148Sphk		VREF(dvp);
83222521Sdyson
8333148Sphk	if (!wantparent)
8343148Sphk		vrele(dvp);
83532071Sdyson
8363148Sphk	if ((cnp->cn_flags & LOCKLEAF) == 0)
83783366Sjulian		VOP_UNLOCK(dp, 0, td);
8383148Sphk	return (0);
8393148Sphk
8403148Sphkbad2:
841144203Sjeff	if (cnp->cn_flags & LOCKPARENT)
84283366Sjulian		VOP_UNLOCK(dvp, 0, td);
8433148Sphk	vrele(dvp);
8443148Sphkbad:
8453148Sphk	vput(dp);
8463148Sphk	*vpp = NULL;
8473148Sphk	return (error);
8483148Sphk}
849141471Sjhb
850141471Sjhb/*
851141471Sjhb * Determine if there is a suitable alternate filename under the specified
852141471Sjhb * prefix for the specified path.  If the create flag is set, then the
853141471Sjhb * alternate prefix will be used so long as the parent directory exists.
854141471Sjhb * This is used by the various compatiblity ABIs so that Linux binaries prefer
855141471Sjhb * files under /compat/linux for example.  The chosen path (whether under
856141471Sjhb * the prefix or under /) is returned in a kernel malloc'd buffer pointed
857141471Sjhb * to by pathbuf.  The caller is responsible for free'ing the buffer from
858141471Sjhb * the M_TEMP bucket if one is returned.
859141471Sjhb */
860141471Sjhbint
861141471Sjhbkern_alternate_path(struct thread *td, const char *prefix, char *path,
862141471Sjhb    enum uio_seg pathseg, char **pathbuf, int create)
863141471Sjhb{
864141471Sjhb	struct nameidata nd, ndroot;
865141471Sjhb	char *ptr, *buf, *cp;
866141471Sjhb	size_t len, sz;
867141471Sjhb	int error;
868141471Sjhb
869141471Sjhb	buf = (char *) malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
870141471Sjhb	*pathbuf = buf;
871141471Sjhb
872141471Sjhb	/* Copy the prefix into the new pathname as a starting point. */
873141471Sjhb	len = strlcpy(buf, prefix, MAXPATHLEN);
874141471Sjhb	if (len >= MAXPATHLEN) {
875141471Sjhb		*pathbuf = NULL;
876141471Sjhb		free(buf, M_TEMP);
877141471Sjhb		return (EINVAL);
878141471Sjhb	}
879141471Sjhb	sz = MAXPATHLEN - len;
880141471Sjhb	ptr = buf + len;
881141471Sjhb
882141471Sjhb	/* Append the filename to the prefix. */
883141471Sjhb	if (pathseg == UIO_SYSSPACE)
884141471Sjhb		error = copystr(path, ptr, sz, &len);
885141471Sjhb	else
886141471Sjhb		error = copyinstr(path, ptr, sz, &len);
887141471Sjhb
888141471Sjhb	if (error) {
889141471Sjhb		*pathbuf = NULL;
890141471Sjhb		free(buf, M_TEMP);
891141471Sjhb		return (error);
892141471Sjhb	}
893141471Sjhb
894141471Sjhb	/* Only use a prefix with absolute pathnames. */
895141471Sjhb	if (*ptr != '/') {
896141471Sjhb		error = EINVAL;
897141471Sjhb		goto keeporig;
898141471Sjhb	}
899141471Sjhb
900141471Sjhb	/* XXX: VFS_LOCK_GIANT? */
901141471Sjhb	mtx_lock(&Giant);
902141471Sjhb
903141471Sjhb	/*
904141471Sjhb	 * We know that there is a / somewhere in this pathname.
905141471Sjhb	 * Search backwards for it, to find the file's parent dir
906141471Sjhb	 * to see if it exists in the alternate tree. If it does,
907141471Sjhb	 * and we want to create a file (cflag is set). We don't
908141471Sjhb	 * need to worry about the root comparison in this case.
909141471Sjhb	 */
910141471Sjhb
911141471Sjhb	if (create) {
912141471Sjhb		for (cp = &ptr[len] - 1; *cp != '/'; cp--);
913141471Sjhb		*cp = '\0';
914141471Sjhb
915141471Sjhb		NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, buf, td);
916141471Sjhb		error = namei(&nd);
917141471Sjhb		*cp = '/';
918141471Sjhb		if (error != 0)
919141471Sjhb			goto nd_failed;
920141471Sjhb	} else {
921141471Sjhb		NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, buf, td);
922141471Sjhb
923141471Sjhb		error = namei(&nd);
924141471Sjhb		if (error != 0)
925141471Sjhb			goto nd_failed;
926141471Sjhb
927141471Sjhb		/*
928141471Sjhb		 * We now compare the vnode of the prefix to the one
929141471Sjhb		 * vnode asked. If they resolve to be the same, then we
930141471Sjhb		 * ignore the match so that the real root gets used.
931141471Sjhb		 * This avoids the problem of traversing "../.." to find the
932141471Sjhb		 * root directory and never finding it, because "/" resolves
933141471Sjhb		 * to the emulation root directory. This is expensive :-(
934141471Sjhb		 */
935141471Sjhb		NDINIT(&ndroot, LOOKUP, FOLLOW, UIO_SYSSPACE, prefix, td);
936141471Sjhb
937141471Sjhb		/* We shouldn't ever get an error from this namei(). */
938141471Sjhb		error = namei(&ndroot);
939141471Sjhb		if (error == 0) {
940141471Sjhb			if (nd.ni_vp == ndroot.ni_vp)
941141471Sjhb				error = ENOENT;
942141471Sjhb
943141471Sjhb			NDFREE(&ndroot, NDF_ONLY_PNBUF);
944141471Sjhb			vrele(ndroot.ni_vp);
945141471Sjhb		}
946141471Sjhb	}
947141471Sjhb
948141471Sjhb	NDFREE(&nd, NDF_ONLY_PNBUF);
949141471Sjhb	vrele(nd.ni_vp);
950141471Sjhb
951141471Sjhbnd_failed:
952141471Sjhb	/* XXX: VFS_UNLOCK_GIANT? */
953141471Sjhb	mtx_unlock(&Giant);
954141471Sjhb
955141471Sjhbkeeporig:
956141471Sjhb	/* If there was an error, use the original path name. */
957141471Sjhb	if (error)
958141471Sjhb		bcopy(ptr, buf, len);
959141471Sjhb	return (error);
960141471Sjhb}
961