vfs_lookup.c revision 191991
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 191991 2009-05-11 16:32:58Z attilio $");
39116182Sobrien
40190759Srwatson#include "opt_kdtrace.h"
4113203Swollman#include "opt_ktrace.h"
42101127Srwatson#include "opt_mac.h"
4313203Swollman
441541Srgrimes#include <sys/param.h>
452112Swollman#include <sys/systm.h>
4669664Speter#include <sys/kernel.h>
47177785Skib#include <sys/fcntl.h>
4876166Smarkm#include <sys/lock.h>
4989316Salfred#include <sys/mutex.h>
501541Srgrimes#include <sys/namei.h>
511541Srgrimes#include <sys/vnode.h>
521541Srgrimes#include <sys/mount.h>
531541Srgrimes#include <sys/filedesc.h>
541541Srgrimes#include <sys/proc.h>
55190759Srwatson#include <sys/sdt.h>
56141471Sjhb#include <sys/syscallsubr.h>
57144613Sjeff#include <sys/sysctl.h>
581541Srgrimes#ifdef KTRACE
591541Srgrimes#include <sys/ktrace.h>
601541Srgrimes#endif
611541Srgrimes
62155334Srwatson#include <security/audit/audit.h>
63163606Srwatson#include <security/mac/mac_framework.h>
64155334Srwatson
6592751Sjeff#include <vm/uma.h>
6632011Sbde
67155168Sjeff#define	NAMEI_DIAGNOSTIC 1
68138345Sphk#undef NAMEI_DIAGNOSTIC
69138345Sphk
70190759SrwatsonSDT_PROVIDER_DECLARE(vfs);
71190759SrwatsonSDT_PROBE_DEFINE3(vfs, namei, lookup, entry, "struct vnode *", "char *",
72190759Srwatson    "unsigned long");
73190759SrwatsonSDT_PROBE_DEFINE2(vfs, namei, lookup, return, "int", "struct vnode *");
74190759Srwatson
751541Srgrimes/*
7669664Speter * Allocation zone for namei
7769664Speter */
7892751Sjeffuma_zone_t namei_zone;
79166167Skib/*
80166167Skib * Placeholder vnode for mp traversal
81166167Skib */
82166167Skibstatic struct vnode *vp_crossmp;
8369664Speter
8469664Speterstatic void
8569664Speternameiinit(void *dummy __unused)
8669664Speter{
87168138Srwatson	int error;
88168138Srwatson
8992654Sjeff	namei_zone = uma_zcreate("NAMEI", MAXPATHLEN, NULL, NULL, NULL, NULL,
9092654Sjeff	    UMA_ALIGN_PTR, 0);
91168138Srwatson	error = getnewvnode("crossmp", NULL, &dead_vnodeops, &vp_crossmp);
92168138Srwatson	if (error != 0)
93168138Srwatson		panic("nameiinit: getnewvnode");
94176519Sattilio	VN_LOCK_ASHARE(vp_crossmp);
9569664Speter}
96177253SrwatsonSYSINIT(vfs, SI_SUB_VFS, SI_ORDER_SECOND, nameiinit, NULL);
9769664Speter
98183520Sjhbstatic int lookup_shared = 1;
99144613SjeffSYSCTL_INT(_vfs, OID_AUTO, lookup_shared, CTLFLAG_RW, &lookup_shared, 0,
100144613Sjeff    "Enables/Disables shared locks for path name translation");
101183519SjhbTUNABLE_INT("vfs.lookup_shared", &lookup_shared);
102144613Sjeff
10369664Speter/*
104161010Srwatson * Convert a pathname into a pointer to a locked vnode.
1051541Srgrimes *
1061541Srgrimes * The FOLLOW flag is set when symbolic links are to be followed
1071541Srgrimes * when they occur at the end of the name translation process.
1081541Srgrimes * Symbolic links are always followed for all other pathname
1091541Srgrimes * components other than the last.
1101541Srgrimes *
1111541Srgrimes * The segflg defines whether the name is to be copied from user
1121541Srgrimes * space or kernel space.
1131541Srgrimes *
1141541Srgrimes * Overall outline of namei:
1151541Srgrimes *
1161541Srgrimes *	copy in name
1171541Srgrimes *	get starting directory
1181541Srgrimes *	while (!done && !error) {
1191541Srgrimes *		call lookup to search path.
1201541Srgrimes *		if symbolic link, massage name in buffer and continue
1211541Srgrimes *	}
1221541Srgrimes */
1231541Srgrimesint
124161011Srwatsonnamei(struct nameidata *ndp)
1251541Srgrimes{
126161011Srwatson	struct filedesc *fdp;	/* pointer to file descriptor state */
127161011Srwatson	char *cp;		/* pointer into pathname argument */
128161011Srwatson	struct vnode *dp;	/* the directory we are searching */
1291541Srgrimes	struct iovec aiov;		/* uio for reading symbolic links */
1301541Srgrimes	struct uio auio;
1311541Srgrimes	int error, linklen;
1321541Srgrimes	struct componentname *cnp = &ndp->ni_cnd;
13383366Sjulian	struct thread *td = cnp->cn_thread;
13483366Sjulian	struct proc *p = td->td_proc;
135140714Sjeff	int vfslocked;
1361541Srgrimes
137150164Scsjp	KASSERT((cnp->cn_flags & MPSAFE) != 0 || mtx_owned(&Giant) != 0,
138150164Scsjp	    ("NOT MPSAFE and Giant not held"));
13991419Sjhb	ndp->ni_cnd.cn_cred = ndp->ni_cnd.cn_thread->td_ucred;
14083366Sjulian	KASSERT(cnp->cn_cred && p, ("namei: bad cred/proc"));
14142408Seivind	KASSERT((cnp->cn_nameiop & (~OPMASK)) == 0,
14242453Seivind	    ("namei: nameiop contaminated with flags"));
14342408Seivind	KASSERT((cnp->cn_flags & OPMASK) == 0,
14442453Seivind	    ("namei: flags contaminated with nameiops"));
145144613Sjeff	if (!lookup_shared)
146144613Sjeff		cnp->cn_flags &= ~LOCKSHARED;
14783366Sjulian	fdp = p->p_fd;
1481541Srgrimes
1491541Srgrimes	/*
1501541Srgrimes	 * Get a buffer for the name to be translated, and copy the
1511541Srgrimes	 * name into the buffer.
1521541Srgrimes	 */
1531541Srgrimes	if ((cnp->cn_flags & HASBUF) == 0)
154111119Simp		cnp->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK);
1551541Srgrimes	if (ndp->ni_segflg == UIO_SYSSPACE)
1561541Srgrimes		error = copystr(ndp->ni_dirp, cnp->cn_pnbuf,
15736735Sdfr			    MAXPATHLEN, (size_t *)&ndp->ni_pathlen);
1581541Srgrimes	else
1591541Srgrimes		error = copyinstr(ndp->ni_dirp, cnp->cn_pnbuf,
16036735Sdfr			    MAXPATHLEN, (size_t *)&ndp->ni_pathlen);
16120069Sbde
162155334Srwatson	/* If we are auditing the kernel pathname, save the user pathname. */
163155334Srwatson	if (cnp->cn_flags & AUDITVNODE1)
164155334Srwatson		AUDIT_ARG(upath, td, cnp->cn_pnbuf, ARG_UPATH1);
165155334Srwatson	if (cnp->cn_flags & AUDITVNODE2)
166155334Srwatson		AUDIT_ARG(upath, td, cnp->cn_pnbuf, ARG_UPATH2);
167155334Srwatson
16820069Sbde	/*
16920069Sbde	 * Don't allow empty pathnames.
17020069Sbde	 */
17120069Sbde	if (!error && *cnp->cn_pnbuf == '\0')
17220069Sbde		error = ENOENT;
17320069Sbde
1741541Srgrimes	if (error) {
17592751Sjeff		uma_zfree(namei_zone, cnp->cn_pnbuf);
176100613Srwatson#ifdef DIAGNOSTIC
177100613Srwatson		cnp->cn_pnbuf = NULL;
178100613Srwatson		cnp->cn_nameptr = NULL;
179100613Srwatson#endif
1801541Srgrimes		ndp->ni_vp = NULL;
1811541Srgrimes		return (error);
1821541Srgrimes	}
1831541Srgrimes	ndp->ni_loopcnt = 0;
1841541Srgrimes#ifdef KTRACE
18597994Sjhb	if (KTRPOINT(td, KTR_NAMEI)) {
18697994Sjhb		KASSERT(cnp->cn_thread == curthread,
18797994Sjhb		    ("namei not using curthread"));
18897994Sjhb		ktrnamei(cnp->cn_pnbuf);
18997994Sjhb	}
1901541Srgrimes#endif
1911541Srgrimes	/*
1921541Srgrimes	 * Get starting point for the translation.
1931541Srgrimes	 */
194168355Srwatson	FILEDESC_SLOCK(fdp);
19533360Sdyson	ndp->ni_rootdir = fdp->fd_rdir;
19651649Sphk	ndp->ni_topdir = fdp->fd_jdir;
19733360Sdyson
198185029Spjd	dp = NULL;
199185029Spjd	if (cnp->cn_pnbuf[0] != '/') {
200185029Spjd		if (ndp->ni_startdir != NULL) {
201185029Spjd			dp = ndp->ni_startdir;
202185029Spjd			error = 0;
203185029Spjd		} else if (ndp->ni_dirfd != AT_FDCWD)
204185029Spjd			error = fgetvp(td, ndp->ni_dirfd, &dp);
205185029Spjd		if (error != 0 || dp != NULL) {
206185029Spjd			FILEDESC_SUNLOCK(fdp);
207185029Spjd			if (error == 0 && dp->v_type != VDIR) {
208185029Spjd				vfslocked = VFS_LOCK_GIANT(dp->v_mount);
209185029Spjd				vrele(dp);
210185029Spjd				VFS_UNLOCK_GIANT(vfslocked);
211185029Spjd				error = ENOTDIR;
212185029Spjd			}
213177785Skib		}
214177785Skib		if (error) {
215177785Skib			uma_zfree(namei_zone, cnp->cn_pnbuf);
216177785Skib#ifdef DIAGNOSTIC
217177785Skib			cnp->cn_pnbuf = NULL;
218177785Skib			cnp->cn_nameptr = NULL;
219177785Skib#endif
220177785Skib			return (error);
221177785Skib		}
222185029Spjd	}
223185029Spjd	if (dp == NULL) {
224177785Skib		dp = fdp->fd_cdir;
225177785Skib		VREF(dp);
226177785Skib		FILEDESC_SUNLOCK(fdp);
227185029Spjd		if (ndp->ni_startdir != NULL) {
228185029Spjd			vfslocked = VFS_LOCK_GIANT(ndp->ni_startdir->v_mount);
229185029Spjd			vrele(ndp->ni_startdir);
230185029Spjd			VFS_UNLOCK_GIANT(vfslocked);
231185029Spjd		}
232177785Skib	}
233190759Srwatson	SDT_PROBE(vfs, namei, lookup, entry, dp, cnp->cn_pnbuf,
234190759Srwatson	    cnp->cn_flags, 0, 0);
235140714Sjeff	vfslocked = VFS_LOCK_GIANT(dp->v_mount);
2361541Srgrimes	for (;;) {
2371541Srgrimes		/*
2381541Srgrimes		 * Check if root directory should replace current directory.
2391541Srgrimes		 * Done at start of translation and after symbolic link.
2401541Srgrimes		 */
2411541Srgrimes		cnp->cn_nameptr = cnp->cn_pnbuf;
2421541Srgrimes		if (*(cnp->cn_nameptr) == '/') {
2431541Srgrimes			vrele(dp);
244140714Sjeff			VFS_UNLOCK_GIANT(vfslocked);
2451541Srgrimes			while (*(cnp->cn_nameptr) == '/') {
2461541Srgrimes				cnp->cn_nameptr++;
2471541Srgrimes				ndp->ni_pathlen--;
2481541Srgrimes			}
2491541Srgrimes			dp = ndp->ni_rootdir;
250140714Sjeff			vfslocked = VFS_LOCK_GIANT(dp->v_mount);
2511541Srgrimes			VREF(dp);
2521541Srgrimes		}
253140714Sjeff		if (vfslocked)
254140714Sjeff			ndp->ni_cnd.cn_flags |= GIANTHELD;
2551541Srgrimes		ndp->ni_startdir = dp;
2563148Sphk		error = lookup(ndp);
2573148Sphk		if (error) {
25892751Sjeff			uma_zfree(namei_zone, cnp->cn_pnbuf);
259100613Srwatson#ifdef DIAGNOSTIC
260100613Srwatson			cnp->cn_pnbuf = NULL;
261100613Srwatson			cnp->cn_nameptr = NULL;
262100613Srwatson#endif
263190759Srwatson			SDT_PROBE(vfs, namei, lookup, return, error, NULL, 0,
264190759Srwatson			    0, 0);
2651541Srgrimes			return (error);
2661541Srgrimes		}
267140714Sjeff		vfslocked = (ndp->ni_cnd.cn_flags & GIANTHELD) != 0;
268140714Sjeff		ndp->ni_cnd.cn_flags &= ~GIANTHELD;
2691541Srgrimes		/*
2701541Srgrimes		 * Check for symbolic link
2711541Srgrimes		 */
2721541Srgrimes		if ((cnp->cn_flags & ISSYMLINK) == 0) {
273100613Srwatson			if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0) {
27492751Sjeff				uma_zfree(namei_zone, cnp->cn_pnbuf);
275100613Srwatson#ifdef DIAGNOSTIC
276100613Srwatson				cnp->cn_pnbuf = NULL;
277100613Srwatson				cnp->cn_nameptr = NULL;
278100613Srwatson#endif
279100613Srwatson			} else
2801541Srgrimes				cnp->cn_flags |= HASBUF;
28132286Sdyson
282140714Sjeff			if ((cnp->cn_flags & MPSAFE) == 0) {
283140714Sjeff				VFS_UNLOCK_GIANT(vfslocked);
284140714Sjeff			} else if (vfslocked)
285140714Sjeff				ndp->ni_cnd.cn_flags |= GIANTHELD;
286190759Srwatson			SDT_PROBE(vfs, namei, lookup, return, 0, ndp->ni_vp,
287190759Srwatson			    0, 0, 0);
2881541Srgrimes			return (0);
2891541Srgrimes		}
2901541Srgrimes		if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
2911541Srgrimes			error = ELOOP;
2921541Srgrimes			break;
2931541Srgrimes		}
294101127Srwatson#ifdef MAC
295105479Srwatson		if ((cnp->cn_flags & NOMACCHECK) == 0) {
296172930Srwatson			error = mac_vnode_check_readlink(td->td_ucred,
297105479Srwatson			    ndp->ni_vp);
298105479Srwatson			if (error)
299105479Srwatson				break;
300105479Srwatson		}
301101127Srwatson#endif
3021541Srgrimes		if (ndp->ni_pathlen > 1)
303111119Simp			cp = uma_zalloc(namei_zone, M_WAITOK);
3041541Srgrimes		else
3051541Srgrimes			cp = cnp->cn_pnbuf;
3061541Srgrimes		aiov.iov_base = cp;
3071541Srgrimes		aiov.iov_len = MAXPATHLEN;
3081541Srgrimes		auio.uio_iov = &aiov;
3091541Srgrimes		auio.uio_iovcnt = 1;
3101541Srgrimes		auio.uio_offset = 0;
3111541Srgrimes		auio.uio_rw = UIO_READ;
3121541Srgrimes		auio.uio_segflg = UIO_SYSSPACE;
31383366Sjulian		auio.uio_td = (struct thread *)0;
3141541Srgrimes		auio.uio_resid = MAXPATHLEN;
3153148Sphk		error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
3163148Sphk		if (error) {
3171541Srgrimes			if (ndp->ni_pathlen > 1)
31892751Sjeff				uma_zfree(namei_zone, cp);
3191541Srgrimes			break;
3201541Srgrimes		}
3211541Srgrimes		linklen = MAXPATHLEN - auio.uio_resid;
32278692Sdillon		if (linklen == 0) {
32378692Sdillon			if (ndp->ni_pathlen > 1)
32492751Sjeff				uma_zfree(namei_zone, cp);
32578692Sdillon			error = ENOENT;
32678692Sdillon			break;
32778692Sdillon		}
3281541Srgrimes		if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
3291541Srgrimes			if (ndp->ni_pathlen > 1)
33092751Sjeff				uma_zfree(namei_zone, cp);
3311541Srgrimes			error = ENAMETOOLONG;
3321541Srgrimes			break;
3331541Srgrimes		}
3341541Srgrimes		if (ndp->ni_pathlen > 1) {
3351541Srgrimes			bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
33692751Sjeff			uma_zfree(namei_zone, cnp->cn_pnbuf);
3371541Srgrimes			cnp->cn_pnbuf = cp;
3381541Srgrimes		} else
3391541Srgrimes			cnp->cn_pnbuf[linklen] = '\0';
3401541Srgrimes		ndp->ni_pathlen += linklen;
3411541Srgrimes		vput(ndp->ni_vp);
3421541Srgrimes		dp = ndp->ni_dvp;
3431541Srgrimes	}
34492751Sjeff	uma_zfree(namei_zone, cnp->cn_pnbuf);
345100613Srwatson#ifdef DIAGNOSTIC
346100613Srwatson	cnp->cn_pnbuf = NULL;
347100613Srwatson	cnp->cn_nameptr = NULL;
348100613Srwatson#endif
349144833Sjeff	vput(ndp->ni_vp);
350144833Sjeff	ndp->ni_vp = NULL;
3511541Srgrimes	vrele(ndp->ni_dvp);
352140714Sjeff	VFS_UNLOCK_GIANT(vfslocked);
353190759Srwatson	SDT_PROBE(vfs, namei, lookup, return, error, NULL, 0, 0, 0);
3541541Srgrimes	return (error);
3551541Srgrimes}
3561541Srgrimes
357162288Smohansstatic int
358162288Smohanscompute_cn_lkflags(struct mount *mp, int lkflags)
359162288Smohans{
360184597Sjhb
361162310Smohans	if (mp == NULL ||
362162310Smohans	    ((lkflags & LK_SHARED) && !(mp->mnt_kern_flag & MNTK_LOOKUP_SHARED))) {
363162288Smohans		lkflags &= ~LK_SHARED;
364162288Smohans		lkflags |= LK_EXCLUSIVE;
365162288Smohans	}
366184597Sjhb	return (lkflags);
367162288Smohans}
368162288Smohans
369189696Sjhbstatic __inline int
370189696Sjhbneeds_exclusive_leaf(struct mount *mp, int flags)
371189696Sjhb{
372189696Sjhb
373189696Sjhb	/*
374189696Sjhb	 * Intermediate nodes can use shared locks, we only need to
375189696Sjhb	 * force an exclusive lock for leaf nodes.
376189696Sjhb	 */
377189696Sjhb	if ((flags & (ISLASTCN | LOCKLEAF)) != (ISLASTCN | LOCKLEAF))
378189696Sjhb		return (0);
379189696Sjhb
380189696Sjhb	/* Always use exclusive locks if LOCKSHARED isn't set. */
381189696Sjhb	if (!(flags & LOCKSHARED))
382189696Sjhb		return (1);
383189696Sjhb
384189696Sjhb	/*
385189696Sjhb	 * For lookups during open(), if the mount point supports
386189696Sjhb	 * extended shared operations, then use a shared lock for the
387189696Sjhb	 * leaf node, otherwise use an exclusive lock.
388189696Sjhb	 */
389189696Sjhb	if (flags & ISOPEN) {
390189696Sjhb		if (mp != NULL &&
391189696Sjhb		    (mp->mnt_kern_flag & MNTK_EXTENDED_SHARED))
392189696Sjhb			return (0);
393189696Sjhb		else
394189696Sjhb			return (1);
395189696Sjhb	}
396189696Sjhb
397189696Sjhb	/*
398189696Sjhb	 * Lookup requests outside of open() that specify LOCKSHARED
399189696Sjhb	 * only need a shared lock on the leaf vnode.
400189696Sjhb	 */
401189697Sjhb	return (0);
402189696Sjhb}
403189696Sjhb
4041541Srgrimes/*
4051541Srgrimes * Search a pathname.
4061541Srgrimes * This is a very central and rather complicated routine.
4071541Srgrimes *
4081541Srgrimes * The pathname is pointed to by ni_ptr and is of length ni_pathlen.
4091541Srgrimes * The starting directory is taken from ni_startdir. The pathname is
4101541Srgrimes * descended until done, or a symbolic link is encountered. The variable
4111541Srgrimes * ni_more is clear if the path is completed; it is set to one if a
4121541Srgrimes * symbolic link needing interpretation is encountered.
4131541Srgrimes *
4141541Srgrimes * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
4151541Srgrimes * whether the name is to be looked up, created, renamed, or deleted.
4161541Srgrimes * When CREATE, RENAME, or DELETE is specified, information usable in
4171541Srgrimes * creating, renaming, or deleting a directory entry may be calculated.
4181541Srgrimes * If flag has LOCKPARENT or'ed into it, the parent directory is returned
4191541Srgrimes * locked. If flag has WANTPARENT or'ed into it, the parent directory is
4201541Srgrimes * returned unlocked. Otherwise the parent directory is not returned. If
4211541Srgrimes * the target of the pathname exists and LOCKLEAF is or'ed into the flag
4221541Srgrimes * the target is returned locked, otherwise it is returned unlocked.
4231541Srgrimes * When creating or renaming and LOCKPARENT is specified, the target may not
4241541Srgrimes * be ".".  When deleting and LOCKPARENT is specified, the target may be ".".
4258876Srgrimes *
4261541Srgrimes * Overall outline of lookup:
4271541Srgrimes *
4281541Srgrimes * dirloop:
4291541Srgrimes *	identify next component of name at ndp->ni_ptr
4301541Srgrimes *	handle degenerate case where name is null string
4311541Srgrimes *	if .. and crossing mount points and on mounted filesys, find parent
4321541Srgrimes *	call VOP_LOOKUP routine for next component name
4331541Srgrimes *	    directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set
4341541Srgrimes *	    component vnode returned in ni_vp (if it exists), locked.
4351541Srgrimes *	if result vnode is mounted on and crossing mount points,
4361541Srgrimes *	    find mounted on vnode
4371541Srgrimes *	if more components of name, do next level at dirloop
4381541Srgrimes *	return the answer in ni_vp, locked if LOCKLEAF set
4391541Srgrimes *	    if LOCKPARENT set, return locked parent in ni_dvp
4401541Srgrimes *	    if WANTPARENT set, return unlocked parent in ni_dvp
4411541Srgrimes */
4421541Srgrimesint
443161011Srwatsonlookup(struct nameidata *ndp)
4441541Srgrimes{
445161011Srwatson	char *cp;		/* pointer into pathname argument */
446161011Srwatson	struct vnode *dp = 0;	/* the directory we are searching */
4471541Srgrimes	struct vnode *tdp;		/* saved dp */
4481541Srgrimes	struct mount *mp;		/* mount table entry */
4491541Srgrimes	int docache;			/* == 0 do not cache last component */
4501541Srgrimes	int wantparent;			/* 1 => wantparent or lockparent flag */
4511541Srgrimes	int rdonly;			/* lookup read-only flag bit */
4529804Sbde	int trailing_slash;
4531541Srgrimes	int error = 0;
45465805Sbp	int dpunlocked = 0;		/* dp has already been unlocked */
4551541Srgrimes	struct componentname *cnp = &ndp->ni_cnd;
456158094Sjeff	int vfslocked;			/* VFS Giant state for child */
457158094Sjeff	int dvfslocked;			/* VFS Giant state for parent */
458140714Sjeff	int tvfslocked;
459162288Smohans	int lkflags_save;
460191991Sattilio#ifdef AUDIT
461191991Sattilio	struct thread *td = curthread;
462191991Sattilio#endif
463162288Smohans
4641541Srgrimes	/*
4651541Srgrimes	 * Setup: break out flag bits into variables.
4661541Srgrimes	 */
467158094Sjeff	dvfslocked = (ndp->ni_cnd.cn_flags & GIANTHELD) != 0;
468158094Sjeff	vfslocked = 0;
469140714Sjeff	ndp->ni_cnd.cn_flags &= ~GIANTHELD;
4701541Srgrimes	wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT);
471144229Sjeff	KASSERT(cnp->cn_nameiop == LOOKUP || wantparent,
472144229Sjeff	    ("CREATE, DELETE, RENAME require LOCKPARENT or WANTPARENT."));
4731541Srgrimes	docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE;
4741541Srgrimes	if (cnp->cn_nameiop == DELETE ||
47522874Sbde	    (wantparent && cnp->cn_nameiop != CREATE &&
47622874Sbde	     cnp->cn_nameiop != LOOKUP))
4771541Srgrimes		docache = 0;
4781541Srgrimes	rdonly = cnp->cn_flags & RDONLY;
479144286Sjeff	cnp->cn_flags &= ~ISSYMLINK;
4801541Srgrimes	ndp->ni_dvp = NULL;
481144286Sjeff	/*
482144286Sjeff	 * We use shared locks until we hit the parent of the last cn then
483144286Sjeff	 * we adjust based on the requesting flags.
484144286Sjeff	 */
485144613Sjeff	if (lookup_shared)
486144613Sjeff		cnp->cn_lkflags = LK_SHARED;
487144613Sjeff	else
488144613Sjeff		cnp->cn_lkflags = LK_EXCLUSIVE;
4891541Srgrimes	dp = ndp->ni_startdir;
4901541Srgrimes	ndp->ni_startdir = NULLVP;
491175202Sattilio	vn_lock(dp,
492175202Sattilio	    compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags | LK_RETRY));
4931541Srgrimes
4941541Srgrimesdirloop:
4951541Srgrimes	/*
4961541Srgrimes	 * Search a new directory.
4971541Srgrimes	 *
4981541Srgrimes	 * The last component of the filename is left accessible via
4991541Srgrimes	 * cnp->cn_nameptr for callers that need the name. Callers needing
5001541Srgrimes	 * the name set the SAVENAME flag. When done, they assume
5011541Srgrimes	 * responsibility for freeing the pathname buffer.
5021541Srgrimes	 */
5031541Srgrimes	cnp->cn_consume = 0;
5041541Srgrimes	for (cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++)
50551906Sphk		continue;
5061541Srgrimes	cnp->cn_namelen = cp - cnp->cn_nameptr;
5071541Srgrimes	if (cnp->cn_namelen > NAME_MAX) {
5081541Srgrimes		error = ENAMETOOLONG;
5091541Srgrimes		goto bad;
5101541Srgrimes	}
5111541Srgrimes#ifdef NAMEI_DIAGNOSTIC
5121541Srgrimes	{ char c = *cp;
5131541Srgrimes	*cp = '\0';
5141541Srgrimes	printf("{%s}: ", cnp->cn_nameptr);
5151541Srgrimes	*cp = c; }
5161541Srgrimes#endif
5171541Srgrimes	ndp->ni_pathlen -= cnp->cn_namelen;
5181541Srgrimes	ndp->ni_next = cp;
5199804Sbde
5209804Sbde	/*
5219804Sbde	 * Replace multiple slashes by a single slash and trailing slashes
5229804Sbde	 * by a null.  This must be done before VOP_LOOKUP() because some
5239804Sbde	 * fs's don't know about trailing slashes.  Remember if there were
5249804Sbde	 * trailing slashes to handle symlinks, existing non-directories
5259804Sbde	 * and non-existing files that won't be directories specially later.
5269804Sbde	 */
5279804Sbde	trailing_slash = 0;
5289804Sbde	while (*cp == '/' && (cp[1] == '/' || cp[1] == '\0')) {
5299804Sbde		cp++;
5309804Sbde		ndp->ni_pathlen--;
5319804Sbde		if (*cp == '\0') {
5329804Sbde			trailing_slash = 1;
5339804Sbde			*ndp->ni_next = '\0';	/* XXX for direnter() ... */
5349804Sbde		}
5359804Sbde	}
5369804Sbde	ndp->ni_next = cp;
5379804Sbde
5381541Srgrimes	cnp->cn_flags |= MAKEENTRY;
5391541Srgrimes	if (*cp == '\0' && docache == 0)
5401541Srgrimes		cnp->cn_flags &= ~MAKEENTRY;
5411541Srgrimes	if (cnp->cn_namelen == 2 &&
5421541Srgrimes	    cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.')
5431541Srgrimes		cnp->cn_flags |= ISDOTDOT;
5441541Srgrimes	else
5451541Srgrimes		cnp->cn_flags &= ~ISDOTDOT;
5461541Srgrimes	if (*ndp->ni_next == 0)
5471541Srgrimes		cnp->cn_flags |= ISLASTCN;
5481541Srgrimes	else
5491541Srgrimes		cnp->cn_flags &= ~ISLASTCN;
5501541Srgrimes
5511541Srgrimes
5521541Srgrimes	/*
5531541Srgrimes	 * Check for degenerate name (e.g. / or "")
5541541Srgrimes	 * which is a way of talking about a directory,
5551541Srgrimes	 * e.g. like "/." or ".".
5561541Srgrimes	 */
5571541Srgrimes	if (cnp->cn_nameptr[0] == '\0') {
55822521Sdyson		if (dp->v_type != VDIR) {
55922521Sdyson			error = ENOTDIR;
56022521Sdyson			goto bad;
56122521Sdyson		}
5621541Srgrimes		if (cnp->cn_nameiop != LOOKUP) {
5631541Srgrimes			error = EISDIR;
5641541Srgrimes			goto bad;
5651541Srgrimes		}
5661541Srgrimes		if (wantparent) {
5671541Srgrimes			ndp->ni_dvp = dp;
5681541Srgrimes			VREF(dp);
5691541Srgrimes		}
5701541Srgrimes		ndp->ni_vp = dp;
571155334Srwatson
572155334Srwatson		if (cnp->cn_flags & AUDITVNODE1)
573155334Srwatson			AUDIT_ARG(vnode, dp, ARG_VNODE1);
574155334Srwatson		else if (cnp->cn_flags & AUDITVNODE2)
575155334Srwatson			AUDIT_ARG(vnode, dp, ARG_VNODE2);
576155334Srwatson
5771541Srgrimes		if (!(cnp->cn_flags & (LOCKPARENT | LOCKLEAF)))
578175294Sattilio			VOP_UNLOCK(dp, 0);
57954655Seivind		/* XXX This should probably move to the top of function. */
5801541Srgrimes		if (cnp->cn_flags & SAVESTART)
5811541Srgrimes			panic("lookup: SAVESTART");
582140714Sjeff		goto success;
5831541Srgrimes	}
5841541Srgrimes
5851541Srgrimes	/*
586154649Struckman	 * Handle "..": four special cases.
587154649Struckman	 * 1. Return an error if this is the last component of
588154649Struckman	 *    the name and the operation is DELETE or RENAME.
589154649Struckman	 * 2. If at root directory (e.g. after chroot)
5901541Srgrimes	 *    or at absolute root directory
5911541Srgrimes	 *    then ignore it so can't get out.
592154649Struckman	 * 3. If this vnode is the root of a mounted
5931541Srgrimes	 *    filesystem, then replace it with the
5941541Srgrimes	 *    vnode which was mounted on so we take the
59596755Strhodes	 *    .. in the other filesystem.
596154649Struckman	 * 4. If the vnode is the top directory of
59751649Sphk	 *    the jail or chroot, don't let them out.
5981541Srgrimes	 */
5991541Srgrimes	if (cnp->cn_flags & ISDOTDOT) {
600154649Struckman		if ((cnp->cn_flags & ISLASTCN) != 0 &&
601154649Struckman		    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
602154690Struckman			error = EINVAL;
603154649Struckman			goto bad;
604154649Struckman		}
6051541Srgrimes		for (;;) {
60651649Sphk			if (dp == ndp->ni_rootdir ||
60751649Sphk			    dp == ndp->ni_topdir ||
608166744Skib			    dp == rootvnode ||
609166744Skib			    ((dp->v_vflag & VV_ROOT) != 0 &&
610166744Skib			     (cnp->cn_flags & NOCROSSMOUNT) != 0)) {
6111541Srgrimes				ndp->ni_dvp = dp;
6121541Srgrimes				ndp->ni_vp = dp;
613158142Skris				vfslocked = VFS_LOCK_GIANT(dp->v_mount);
6141541Srgrimes				VREF(dp);
6151541Srgrimes				goto nextname;
6161541Srgrimes			}
617166744Skib			if ((dp->v_vflag & VV_ROOT) == 0)
6181541Srgrimes				break;
619155385Sjeff			if (dp->v_iflag & VI_DOOMED) {	/* forced unmount */
620190387Sjhb				error = ENOENT;
62169405Salfred				goto bad;
62269405Salfred			}
6231541Srgrimes			tdp = dp;
624144833Sjeff			dp = dp->v_mount->mnt_vnodecovered;
625158094Sjeff			tvfslocked = dvfslocked;
626158094Sjeff			dvfslocked = VFS_LOCK_GIANT(dp->v_mount);
627144833Sjeff			VREF(dp);
6281541Srgrimes			vput(tdp);
629140714Sjeff			VFS_UNLOCK_GIANT(tvfslocked);
630175202Sattilio			vn_lock(dp,
631175202Sattilio			    compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags |
632175202Sattilio			    LK_RETRY));
6331541Srgrimes		}
6341541Srgrimes	}
6351541Srgrimes
6361541Srgrimes	/*
6371541Srgrimes	 * We now have a segment name to search for, and a directory to search.
6381541Srgrimes	 */
6391541Srgrimesunionlookup:
640101127Srwatson#ifdef MAC
641105479Srwatson	if ((cnp->cn_flags & NOMACCHECK) == 0) {
642191990Sattilio		error = mac_vnode_check_lookup(cnp->cn_thread->td_ucred, dp,
643191990Sattilio		    cnp);
644105479Srwatson		if (error)
645105479Srwatson			goto bad;
646105479Srwatson	}
647101127Srwatson#endif
6481541Srgrimes	ndp->ni_dvp = dp;
64922521Sdyson	ndp->ni_vp = NULL;
65024624Sdfr	ASSERT_VOP_LOCKED(dp, "lookup");
651158094Sjeff	VNASSERT(vfslocked == 0, dp, ("lookup: vfslocked %d", vfslocked));
652144286Sjeff	/*
653144286Sjeff	 * If we have a shared lock we may need to upgrade the lock for the
654144286Sjeff	 * last operation.
655144286Sjeff	 */
656166167Skib	if (dp != vp_crossmp &&
657176559Sattilio	    VOP_ISLOCKED(dp) == LK_SHARED &&
658144286Sjeff	    (cnp->cn_flags & ISLASTCN) && (cnp->cn_flags & LOCKPARENT))
659175202Sattilio		vn_lock(dp, LK_UPGRADE|LK_RETRY);
660144286Sjeff	/*
661144286Sjeff	 * If we're looking up the last component and we need an exclusive
662144286Sjeff	 * lock, adjust our lkflags.
663144286Sjeff	 */
664189696Sjhb	if (needs_exclusive_leaf(dp->v_mount, cnp->cn_flags))
665144286Sjeff		cnp->cn_lkflags = LK_EXCLUSIVE;
666138345Sphk#ifdef NAMEI_DIAGNOSTIC
667138345Sphk	vprint("lookup in", dp);
668138345Sphk#endif
669162288Smohans	lkflags_save = cnp->cn_lkflags;
670162288Smohans	cnp->cn_lkflags = compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags);
67143301Sdillon	if ((error = VOP_LOOKUP(dp, &ndp->ni_vp, cnp)) != 0) {
672162288Smohans		cnp->cn_lkflags = lkflags_save;
67342408Seivind		KASSERT(ndp->ni_vp == NULL, ("leaf should be empty"));
6741541Srgrimes#ifdef NAMEI_DIAGNOSTIC
6751541Srgrimes		printf("not found\n");
6761541Srgrimes#endif
6771541Srgrimes		if ((error == ENOENT) &&
678101308Sjeff		    (dp->v_vflag & VV_ROOT) && (dp->v_mount != NULL) &&
6791541Srgrimes		    (dp->v_mount->mnt_flag & MNT_UNION)) {
6801541Srgrimes			tdp = dp;
681144833Sjeff			dp = dp->v_mount->mnt_vnodecovered;
682158094Sjeff			tvfslocked = dvfslocked;
683158094Sjeff			dvfslocked = VFS_LOCK_GIANT(dp->v_mount);
684144833Sjeff			VREF(dp);
685144203Sjeff			vput(tdp);
686140714Sjeff			VFS_UNLOCK_GIANT(tvfslocked);
687175202Sattilio			vn_lock(dp,
688175202Sattilio			    compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags |
689175202Sattilio			    LK_RETRY));
6901541Srgrimes			goto unionlookup;
6911541Srgrimes		}
6921541Srgrimes
6931541Srgrimes		if (error != EJUSTRETURN)
6941541Srgrimes			goto bad;
6951541Srgrimes		/*
6961541Srgrimes		 * If creating and at end of pathname, then can consider
6971541Srgrimes		 * allowing file to be created.
6981541Srgrimes		 */
69911644Sdg		if (rdonly) {
7001541Srgrimes			error = EROFS;
7011541Srgrimes			goto bad;
7021541Srgrimes		}
7039804Sbde		if (*cp == '\0' && trailing_slash &&
7049804Sbde		     !(cnp->cn_flags & WILLBEDIR)) {
7059804Sbde			error = ENOENT;
7069804Sbde			goto bad;
7079804Sbde		}
708144203Sjeff		if ((cnp->cn_flags & LOCKPARENT) == 0)
709175294Sattilio			VOP_UNLOCK(dp, 0);
7101541Srgrimes		/*
711144203Sjeff		 * This is a temporary assert to make sure I know what the
712144203Sjeff		 * behavior here was.
713144203Sjeff		 */
714144203Sjeff		KASSERT((cnp->cn_flags & (WANTPARENT|LOCKPARENT)) != 0,
715144203Sjeff		   ("lookup: Unhandled case."));
716144203Sjeff		/*
7171541Srgrimes		 * We return with ni_vp NULL to indicate that the entry
7181541Srgrimes		 * doesn't currently exist, leaving a pointer to the
719161010Srwatson		 * (possibly locked) directory vnode in ndp->ni_dvp.
7201541Srgrimes		 */
7211541Srgrimes		if (cnp->cn_flags & SAVESTART) {
7221541Srgrimes			ndp->ni_startdir = ndp->ni_dvp;
7231541Srgrimes			VREF(ndp->ni_startdir);
7241541Srgrimes		}
725140714Sjeff		goto success;
726162288Smohans	} else
727162288Smohans		cnp->cn_lkflags = lkflags_save;
7281541Srgrimes#ifdef NAMEI_DIAGNOSTIC
7291541Srgrimes	printf("found\n");
7301541Srgrimes#endif
731144203Sjeff	/*
7321541Srgrimes	 * Take into account any additional components consumed by
7331541Srgrimes	 * the underlying filesystem.
7341541Srgrimes	 */
7351541Srgrimes	if (cnp->cn_consume > 0) {
7361541Srgrimes		cnp->cn_nameptr += cnp->cn_consume;
7371541Srgrimes		ndp->ni_next += cnp->cn_consume;
7381541Srgrimes		ndp->ni_pathlen -= cnp->cn_consume;
7391541Srgrimes		cnp->cn_consume = 0;
7401541Srgrimes	}
7411541Srgrimes
7421541Srgrimes	dp = ndp->ni_vp;
743158094Sjeff	vfslocked = VFS_LOCK_GIANT(dp->v_mount);
7441541Srgrimes
7451541Srgrimes	/*
7461541Srgrimes	 * Check to see if the vnode has been mounted on;
74796755Strhodes	 * if so find the root of the mounted filesystem.
7481541Srgrimes	 */
7491541Srgrimes	while (dp->v_type == VDIR && (mp = dp->v_mountedhere) &&
7501541Srgrimes	       (cnp->cn_flags & NOCROSSMOUNT) == 0) {
751184554Sattilio		if (vfs_busy(mp, 0))
7521541Srgrimes			continue;
753144833Sjeff		vput(dp);
754158094Sjeff		VFS_UNLOCK_GIANT(vfslocked);
755155168Sjeff		vfslocked = VFS_LOCK_GIANT(mp);
756158094Sjeff		if (dp != ndp->ni_dvp)
757166167Skib			vput(ndp->ni_dvp);
758166167Skib		else
759166167Skib			vrele(ndp->ni_dvp);
760166167Skib		VFS_UNLOCK_GIANT(dvfslocked);
761166167Skib		dvfslocked = 0;
762166167Skib		vref(vp_crossmp);
763166167Skib		ndp->ni_dvp = vp_crossmp;
764191990Sattilio		error = VFS_ROOT(mp, compute_cn_lkflags(mp, cnp->cn_lkflags),
765191990Sattilio		    &tdp);
766182542Sattilio		vfs_unbusy(mp);
767175202Sattilio		if (vn_lock(vp_crossmp, LK_SHARED | LK_NOWAIT))
768166167Skib			panic("vp_crossmp exclusively locked or reclaimed");
76965805Sbp		if (error) {
77065805Sbp			dpunlocked = 1;
7711541Srgrimes			goto bad2;
77265805Sbp		}
7731541Srgrimes		ndp->ni_vp = dp = tdp;
7741541Srgrimes	}
7751541Srgrimes
77610219Sdfr	/*
77710219Sdfr	 * Check for symbolic link
77810219Sdfr	 */
77910219Sdfr	if ((dp->v_type == VLNK) &&
78010219Sdfr	    ((cnp->cn_flags & FOLLOW) || trailing_slash ||
78110219Sdfr	     *ndp->ni_next == '/')) {
78210219Sdfr		cnp->cn_flags |= ISSYMLINK;
783155385Sjeff		if (dp->v_iflag & VI_DOOMED) {
784190387Sjhb			/*
785190387Sjhb			 * We can't know whether the directory was mounted with
786190387Sjhb			 * NOSYMFOLLOW, so we can't follow safely.
787190387Sjhb			 */
788190387Sjhb			error = ENOENT;
78969405Salfred			goto bad2;
79069405Salfred		}
79135105Swosch		if (dp->v_mount->mnt_flag & MNT_NOSYMFOLLOW) {
79235105Swosch			error = EACCES;
79335105Swosch			goto bad2;
79435105Swosch		}
795144833Sjeff		/*
796144833Sjeff		 * Symlink code always expects an unlocked dvp.
797144833Sjeff		 */
798144833Sjeff		if (ndp->ni_dvp != ndp->ni_vp)
799175294Sattilio			VOP_UNLOCK(ndp->ni_dvp, 0);
800140714Sjeff		goto success;
80110219Sdfr	}
80210219Sdfr
80310219Sdfr	/*
80410219Sdfr	 * Check for bogus trailing slashes.
80510219Sdfr	 */
80610219Sdfr	if (trailing_slash && dp->v_type != VDIR) {
80710219Sdfr		error = ENOTDIR;
80810219Sdfr		goto bad2;
80910219Sdfr	}
81010219Sdfr
8111541Srgrimesnextname:
8121541Srgrimes	/*
8131541Srgrimes	 * Not a symbolic link.  If more pathname,
8141541Srgrimes	 * continue at next component, else return.
8151541Srgrimes	 */
816144203Sjeff	KASSERT((cnp->cn_flags & ISLASTCN) || *ndp->ni_next == '/',
817144203Sjeff	    ("lookup: invalid path state."));
8181541Srgrimes	if (*ndp->ni_next == '/') {
8191541Srgrimes		cnp->cn_nameptr = ndp->ni_next;
8201541Srgrimes		while (*cnp->cn_nameptr == '/') {
8211541Srgrimes			cnp->cn_nameptr++;
8221541Srgrimes			ndp->ni_pathlen--;
8231541Srgrimes		}
824144833Sjeff		if (ndp->ni_dvp != dp)
825144833Sjeff			vput(ndp->ni_dvp);
826144833Sjeff		else
827144833Sjeff			vrele(ndp->ni_dvp);
828155168Sjeff		VFS_UNLOCK_GIANT(dvfslocked);
829158094Sjeff		dvfslocked = vfslocked;	/* dp becomes dvp in dirloop */
830158094Sjeff		vfslocked = 0;
8311541Srgrimes		goto dirloop;
8321541Srgrimes	}
8331541Srgrimes	/*
83496755Strhodes	 * Disallow directory write attempts on read-only filesystems.
8351541Srgrimes	 */
83611644Sdg	if (rdonly &&
83711644Sdg	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
83811644Sdg		error = EROFS;
83911644Sdg		goto bad2;
8401541Srgrimes	}
8411541Srgrimes	if (cnp->cn_flags & SAVESTART) {
8421541Srgrimes		ndp->ni_startdir = ndp->ni_dvp;
8431541Srgrimes		VREF(ndp->ni_startdir);
8441541Srgrimes	}
845144833Sjeff	if (!wantparent) {
846144833Sjeff		if (ndp->ni_dvp != dp)
847144833Sjeff			vput(ndp->ni_dvp);
848144833Sjeff		else
849144833Sjeff			vrele(ndp->ni_dvp);
850155168Sjeff		VFS_UNLOCK_GIANT(dvfslocked);
851155168Sjeff		dvfslocked = 0;
852144833Sjeff	} else if ((cnp->cn_flags & LOCKPARENT) == 0 && ndp->ni_dvp != dp)
853175294Sattilio		VOP_UNLOCK(ndp->ni_dvp, 0);
85432071Sdyson
855155334Srwatson	if (cnp->cn_flags & AUDITVNODE1)
856155334Srwatson		AUDIT_ARG(vnode, dp, ARG_VNODE1);
857155334Srwatson	else if (cnp->cn_flags & AUDITVNODE2)
858155334Srwatson		AUDIT_ARG(vnode, dp, ARG_VNODE2);
859155334Srwatson
8601541Srgrimes	if ((cnp->cn_flags & LOCKLEAF) == 0)
861175294Sattilio		VOP_UNLOCK(dp, 0);
862140714Sjeffsuccess:
863172274Spjd	/*
864172274Spjd	 * Because of lookup_shared we may have the vnode shared locked, but
865172274Spjd	 * the caller may want it to be exclusively locked.
866172274Spjd	 */
867189696Sjhb	if (needs_exclusive_leaf(dp->v_mount, cnp->cn_flags) &&
868189696Sjhb	    VOP_ISLOCKED(dp) != LK_EXCLUSIVE) {
869175202Sattilio		vn_lock(dp, LK_UPGRADE | LK_RETRY);
870186276Skib		if (dp->v_iflag & VI_DOOMED) {
871186276Skib			error = ENOENT;
872186276Skib			goto bad2;
873186276Skib		}
874172274Spjd	}
875155168Sjeff	if (vfslocked && dvfslocked)
876155168Sjeff		VFS_UNLOCK_GIANT(dvfslocked);	/* Only need one */
877155168Sjeff	if (vfslocked || dvfslocked)
878140714Sjeff		ndp->ni_cnd.cn_flags |= GIANTHELD;
8791541Srgrimes	return (0);
8801541Srgrimes
8811541Srgrimesbad2:
882144833Sjeff	if (dp != ndp->ni_dvp)
883144203Sjeff		vput(ndp->ni_dvp);
884144203Sjeff	else
885144203Sjeff		vrele(ndp->ni_dvp);
8861541Srgrimesbad:
887144833Sjeff	if (!dpunlocked)
88865805Sbp		vput(dp);
889140714Sjeff	VFS_UNLOCK_GIANT(vfslocked);
890155168Sjeff	VFS_UNLOCK_GIANT(dvfslocked);
891140714Sjeff	ndp->ni_cnd.cn_flags &= ~GIANTHELD;
8921541Srgrimes	ndp->ni_vp = NULL;
8931541Srgrimes	return (error);
8941541Srgrimes}
8951541Srgrimes
8963148Sphk/*
8973148Sphk * relookup - lookup a path name component
898170035Srwatson *    Used by lookup to re-acquire things.
8993148Sphk */
9003148Sphkint
901161011Srwatsonrelookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp)
9023148Sphk{
90322521Sdyson	struct vnode *dp = 0;		/* the directory we are searching */
9043148Sphk	int wantparent;			/* 1 => wantparent or lockparent flag */
9053148Sphk	int rdonly;			/* lookup read-only flag bit */
9063148Sphk	int error = 0;
9071541Srgrimes
908144203Sjeff	KASSERT(cnp->cn_flags & ISLASTCN,
909144203Sjeff	    ("relookup: Not given last component."));
9103148Sphk	/*
9113148Sphk	 * Setup: break out flag bits into variables.
9123148Sphk	 */
9133148Sphk	wantparent = cnp->cn_flags & (LOCKPARENT|WANTPARENT);
914145004Sjeff	KASSERT(wantparent, ("relookup: parent not wanted."));
9153148Sphk	rdonly = cnp->cn_flags & RDONLY;
9163148Sphk	cnp->cn_flags &= ~ISSYMLINK;
9173148Sphk	dp = dvp;
918144286Sjeff	cnp->cn_lkflags = LK_EXCLUSIVE;
919175202Sattilio	vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
9203148Sphk
9213148Sphk	/*
9223148Sphk	 * Search a new directory.
9233148Sphk	 *
9243148Sphk	 * The last component of the filename is left accessible via
9253148Sphk	 * cnp->cn_nameptr for callers that need the name. Callers needing
9263148Sphk	 * the name set the SAVENAME flag. When done, they assume
9273148Sphk	 * responsibility for freeing the pathname buffer.
9283148Sphk	 */
9293148Sphk#ifdef NAMEI_DIAGNOSTIC
9303148Sphk	printf("{%s}: ", cnp->cn_nameptr);
9313148Sphk#endif
9323148Sphk
9333148Sphk	/*
9343148Sphk	 * Check for degenerate name (e.g. / or "")
9353148Sphk	 * which is a way of talking about a directory,
9363148Sphk	 * e.g. like "/." or ".".
9373148Sphk	 */
9383148Sphk	if (cnp->cn_nameptr[0] == '\0') {
9393148Sphk		if (cnp->cn_nameiop != LOOKUP || wantparent) {
9403148Sphk			error = EISDIR;
9413148Sphk			goto bad;
9423148Sphk		}
9433148Sphk		if (dp->v_type != VDIR) {
9443148Sphk			error = ENOTDIR;
9453148Sphk			goto bad;
9463148Sphk		}
9473148Sphk		if (!(cnp->cn_flags & LOCKLEAF))
948175294Sattilio			VOP_UNLOCK(dp, 0);
9493148Sphk		*vpp = dp;
95054655Seivind		/* XXX This should probably move to the top of function. */
9513148Sphk		if (cnp->cn_flags & SAVESTART)
9523148Sphk			panic("lookup: SAVESTART");
9533148Sphk		return (0);
9543148Sphk	}
9553148Sphk
9563148Sphk	if (cnp->cn_flags & ISDOTDOT)
9573148Sphk		panic ("relookup: lookup on dot-dot");
9583148Sphk
9593148Sphk	/*
9603148Sphk	 * We now have a segment name to search for, and a directory to search.
9613148Sphk	 */
962138345Sphk#ifdef NAMEI_DIAGNOSTIC
963138345Sphk	vprint("search in:", dp);
964138345Sphk#endif
96543311Sdillon	if ((error = VOP_LOOKUP(dp, vpp, cnp)) != 0) {
96642408Seivind		KASSERT(*vpp == NULL, ("leaf should be empty"));
9673148Sphk		if (error != EJUSTRETURN)
9683148Sphk			goto bad;
9693148Sphk		/*
9703148Sphk		 * If creating and at end of pathname, then can consider
9713148Sphk		 * allowing file to be created.
9723148Sphk		 */
97311644Sdg		if (rdonly) {
9743148Sphk			error = EROFS;
9753148Sphk			goto bad;
9763148Sphk		}
9773148Sphk		/* ASSERT(dvp == ndp->ni_startdir) */
9783148Sphk		if (cnp->cn_flags & SAVESTART)
9793148Sphk			VREF(dvp);
980144203Sjeff		if ((cnp->cn_flags & LOCKPARENT) == 0)
981175294Sattilio			VOP_UNLOCK(dp, 0);
9823148Sphk		/*
983144203Sjeff		 * This is a temporary assert to make sure I know what the
984144203Sjeff		 * behavior here was.
985144203Sjeff		 */
986144203Sjeff		KASSERT((cnp->cn_flags & (WANTPARENT|LOCKPARENT)) != 0,
987144203Sjeff		   ("relookup: Unhandled case."));
988144203Sjeff		/*
9893148Sphk		 * We return with ni_vp NULL to indicate that the entry
9903148Sphk		 * doesn't currently exist, leaving a pointer to the
991161010Srwatson		 * (possibly locked) directory vnode in ndp->ni_dvp.
9923148Sphk		 */
9933148Sphk		return (0);
9943148Sphk	}
995162288Smohans
9963148Sphk	dp = *vpp;
9973148Sphk
9983148Sphk	/*
99996755Strhodes	 * Disallow directory write attempts on read-only filesystems.
10003148Sphk	 */
100111644Sdg	if (rdonly &&
100211644Sdg	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
1003145004Sjeff		if (dvp == dp)
1004145004Sjeff			vrele(dvp);
1005145004Sjeff		else
1006145004Sjeff			vput(dvp);
100711644Sdg		error = EROFS;
1008145004Sjeff		goto bad;
10093148Sphk	}
1010145004Sjeff	/*
1011145004Sjeff	 * Set the parent lock/ref state to the requested state.
1012145004Sjeff	 */
1013145004Sjeff	if ((cnp->cn_flags & LOCKPARENT) == 0 && dvp != dp) {
1014145004Sjeff		if (wantparent)
1015175294Sattilio			VOP_UNLOCK(dvp, 0);
1016145004Sjeff		else
1017145004Sjeff			vput(dvp);
1018145004Sjeff	} else if (!wantparent)
1019145004Sjeff		vrele(dvp);
1020145004Sjeff	/*
1021145004Sjeff	 * Check for symbolic link
1022145004Sjeff	 */
1023145004Sjeff	KASSERT(dp->v_type != VLNK || !(cnp->cn_flags & FOLLOW),
1024145004Sjeff	    ("relookup: symlink found.\n"));
1025145004Sjeff
10263148Sphk	/* ASSERT(dvp == ndp->ni_startdir) */
10273148Sphk	if (cnp->cn_flags & SAVESTART)
10283148Sphk		VREF(dvp);
102922521Sdyson
10303148Sphk	if ((cnp->cn_flags & LOCKLEAF) == 0)
1031175294Sattilio		VOP_UNLOCK(dp, 0);
10323148Sphk	return (0);
10333148Sphkbad:
10343148Sphk	vput(dp);
10353148Sphk	*vpp = NULL;
10363148Sphk	return (error);
10373148Sphk}
1038141471Sjhb
1039141471Sjhb/*
1040144661Sjeff * Free data allocated by namei(); see namei(9) for details.
1041144661Sjeff */
1042144661Sjeffvoid
1043161011SrwatsonNDFREE(struct nameidata *ndp, const u_int flags)
1044144661Sjeff{
1045144833Sjeff	int unlock_dvp;
1046144833Sjeff	int unlock_vp;
1047144661Sjeff
1048144833Sjeff	unlock_dvp = 0;
1049144833Sjeff	unlock_vp = 0;
1050144833Sjeff
1051144661Sjeff	if (!(flags & NDF_NO_FREE_PNBUF) &&
1052144661Sjeff	    (ndp->ni_cnd.cn_flags & HASBUF)) {
1053144661Sjeff		uma_zfree(namei_zone, ndp->ni_cnd.cn_pnbuf);
1054144661Sjeff		ndp->ni_cnd.cn_flags &= ~HASBUF;
1055144661Sjeff	}
1056144833Sjeff	if (!(flags & NDF_NO_VP_UNLOCK) &&
1057144833Sjeff	    (ndp->ni_cnd.cn_flags & LOCKLEAF) && ndp->ni_vp)
1058144833Sjeff		unlock_vp = 1;
1059144833Sjeff	if (!(flags & NDF_NO_VP_RELE) && ndp->ni_vp) {
1060144833Sjeff		if (unlock_vp) {
1061144833Sjeff			vput(ndp->ni_vp);
1062144833Sjeff			unlock_vp = 0;
1063144833Sjeff		} else
1064144833Sjeff			vrele(ndp->ni_vp);
1065144833Sjeff		ndp->ni_vp = NULL;
1066144833Sjeff	}
1067144833Sjeff	if (unlock_vp)
1068175294Sattilio		VOP_UNLOCK(ndp->ni_vp, 0);
1069144661Sjeff	if (!(flags & NDF_NO_DVP_UNLOCK) &&
1070144661Sjeff	    (ndp->ni_cnd.cn_flags & LOCKPARENT) &&
1071144661Sjeff	    ndp->ni_dvp != ndp->ni_vp)
1072144833Sjeff		unlock_dvp = 1;
1073144661Sjeff	if (!(flags & NDF_NO_DVP_RELE) &&
1074144661Sjeff	    (ndp->ni_cnd.cn_flags & (LOCKPARENT|WANTPARENT))) {
1075144833Sjeff		if (unlock_dvp) {
1076144833Sjeff			vput(ndp->ni_dvp);
1077144833Sjeff			unlock_dvp = 0;
1078144833Sjeff		} else
1079144833Sjeff			vrele(ndp->ni_dvp);
1080144661Sjeff		ndp->ni_dvp = NULL;
1081144661Sjeff	}
1082144833Sjeff	if (unlock_dvp)
1083175294Sattilio		VOP_UNLOCK(ndp->ni_dvp, 0);
1084144661Sjeff	if (!(flags & NDF_NO_STARTDIR_RELE) &&
1085144661Sjeff	    (ndp->ni_cnd.cn_flags & SAVESTART)) {
1086144661Sjeff		vrele(ndp->ni_startdir);
1087144661Sjeff		ndp->ni_startdir = NULL;
1088144661Sjeff	}
1089144661Sjeff}
1090144661Sjeff
1091144661Sjeff/*
1092141471Sjhb * Determine if there is a suitable alternate filename under the specified
1093141471Sjhb * prefix for the specified path.  If the create flag is set, then the
1094141471Sjhb * alternate prefix will be used so long as the parent directory exists.
1095141471Sjhb * This is used by the various compatiblity ABIs so that Linux binaries prefer
1096141471Sjhb * files under /compat/linux for example.  The chosen path (whether under
1097141471Sjhb * the prefix or under /) is returned in a kernel malloc'd buffer pointed
1098141471Sjhb * to by pathbuf.  The caller is responsible for free'ing the buffer from
1099141471Sjhb * the M_TEMP bucket if one is returned.
1100141471Sjhb */
1101141471Sjhbint
1102177997Skibkern_alternate_path(struct thread *td, const char *prefix, const char *path,
1103177997Skib    enum uio_seg pathseg, char **pathbuf, int create, int dirfd)
1104141471Sjhb{
1105141471Sjhb	struct nameidata nd, ndroot;
1106141471Sjhb	char *ptr, *buf, *cp;
1107141471Sjhb	size_t len, sz;
1108141471Sjhb	int error;
1109141471Sjhb
1110141471Sjhb	buf = (char *) malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1111141471Sjhb	*pathbuf = buf;
1112141471Sjhb
1113141471Sjhb	/* Copy the prefix into the new pathname as a starting point. */
1114141471Sjhb	len = strlcpy(buf, prefix, MAXPATHLEN);
1115141471Sjhb	if (len >= MAXPATHLEN) {
1116141471Sjhb		*pathbuf = NULL;
1117141471Sjhb		free(buf, M_TEMP);
1118141471Sjhb		return (EINVAL);
1119141471Sjhb	}
1120141471Sjhb	sz = MAXPATHLEN - len;
1121141471Sjhb	ptr = buf + len;
1122141471Sjhb
1123141471Sjhb	/* Append the filename to the prefix. */
1124141471Sjhb	if (pathseg == UIO_SYSSPACE)
1125141471Sjhb		error = copystr(path, ptr, sz, &len);
1126141471Sjhb	else
1127141471Sjhb		error = copyinstr(path, ptr, sz, &len);
1128141471Sjhb
1129141471Sjhb	if (error) {
1130141471Sjhb		*pathbuf = NULL;
1131141471Sjhb		free(buf, M_TEMP);
1132141471Sjhb		return (error);
1133141471Sjhb	}
1134141471Sjhb
1135141471Sjhb	/* Only use a prefix with absolute pathnames. */
1136141471Sjhb	if (*ptr != '/') {
1137141471Sjhb		error = EINVAL;
1138141471Sjhb		goto keeporig;
1139141471Sjhb	}
1140141471Sjhb
1141177997Skib	if (dirfd != AT_FDCWD) {
1142177997Skib		/*
1143177997Skib		 * We want the original because the "prefix" is
1144177997Skib		 * included in the already opened dirfd.
1145177997Skib		 */
1146177997Skib		bcopy(ptr, buf, len);
1147177997Skib		return (0);
1148177997Skib	}
1149177997Skib
1150141471Sjhb	/*
1151141471Sjhb	 * We know that there is a / somewhere in this pathname.
1152141471Sjhb	 * Search backwards for it, to find the file's parent dir
1153141471Sjhb	 * to see if it exists in the alternate tree. If it does,
1154141471Sjhb	 * and we want to create a file (cflag is set). We don't
1155141471Sjhb	 * need to worry about the root comparison in this case.
1156141471Sjhb	 */
1157141471Sjhb
1158141471Sjhb	if (create) {
1159141471Sjhb		for (cp = &ptr[len] - 1; *cp != '/'; cp--);
1160141471Sjhb		*cp = '\0';
1161141471Sjhb
1162150431Sjhb		NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, UIO_SYSSPACE, buf, td);
1163141471Sjhb		error = namei(&nd);
1164141471Sjhb		*cp = '/';
1165141471Sjhb		if (error != 0)
1166150431Sjhb			goto keeporig;
1167141471Sjhb	} else {
1168150431Sjhb		NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, UIO_SYSSPACE, buf, td);
1169141471Sjhb
1170141471Sjhb		error = namei(&nd);
1171141471Sjhb		if (error != 0)
1172150431Sjhb			goto keeporig;
1173141471Sjhb
1174141471Sjhb		/*
1175141471Sjhb		 * We now compare the vnode of the prefix to the one
1176141471Sjhb		 * vnode asked. If they resolve to be the same, then we
1177141471Sjhb		 * ignore the match so that the real root gets used.
1178141471Sjhb		 * This avoids the problem of traversing "../.." to find the
1179141471Sjhb		 * root directory and never finding it, because "/" resolves
1180141471Sjhb		 * to the emulation root directory. This is expensive :-(
1181141471Sjhb		 */
1182150431Sjhb		NDINIT(&ndroot, LOOKUP, FOLLOW | MPSAFE, UIO_SYSSPACE, prefix,
1183150431Sjhb		    td);
1184141471Sjhb
1185141471Sjhb		/* We shouldn't ever get an error from this namei(). */
1186141471Sjhb		error = namei(&ndroot);
1187141471Sjhb		if (error == 0) {
1188141471Sjhb			if (nd.ni_vp == ndroot.ni_vp)
1189141471Sjhb				error = ENOENT;
1190141471Sjhb
1191141471Sjhb			NDFREE(&ndroot, NDF_ONLY_PNBUF);
1192141471Sjhb			vrele(ndroot.ni_vp);
1193150431Sjhb			VFS_UNLOCK_GIANT(NDHASGIANT(&ndroot));
1194141471Sjhb		}
1195141471Sjhb	}
1196141471Sjhb
1197141471Sjhb	NDFREE(&nd, NDF_ONLY_PNBUF);
1198141471Sjhb	vrele(nd.ni_vp);
1199150431Sjhb	VFS_UNLOCK_GIANT(NDHASGIANT(&nd));
1200141471Sjhb
1201141471Sjhbkeeporig:
1202141471Sjhb	/* If there was an error, use the original path name. */
1203141471Sjhb	if (error)
1204141471Sjhb		bcopy(ptr, buf, len);
1205141471Sjhb	return (error);
1206141471Sjhb}
1207