vfs_lookup.c revision 177785
1317019Sdim/*-
2317019Sdim * Copyright (c) 1982, 1986, 1989, 1993
3317019Sdim *	The Regents of the University of California.  All rights reserved.
4317019Sdim * (c) UNIX System Laboratories, Inc.
5317019Sdim * All or some portions of this file are derived from material licensed
6317019Sdim * to the University of California by American Telephone and Telegraph
7317019Sdim * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8317019Sdim * the permission of UNIX System Laboratories, Inc.
9317019Sdim *
10317019Sdim * Redistribution and use in source and binary forms, with or without
11317019Sdim * modification, are permitted provided that the following conditions
12317019Sdim * are met:
13317019Sdim * 1. Redistributions of source code must retain the above copyright
14317019Sdim *    notice, this list of conditions and the following disclaimer.
15317019Sdim * 2. Redistributions in binary form must reproduce the above copyright
16317019Sdim *    notice, this list of conditions and the following disclaimer in the
17317019Sdim *    documentation and/or other materials provided with the distribution.
18317019Sdim * 4. Neither the name of the University nor the names of its contributors
19317019Sdim *    may be used to endorse or promote products derived from this software
20317019Sdim *    without specific prior written permission.
21317019Sdim *
22317019Sdim * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23317019Sdim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24317019Sdim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25317019Sdim * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26317019Sdim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27317019Sdim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28317019Sdim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29317019Sdim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30317019Sdim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31317019Sdim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32317019Sdim * SUCH DAMAGE.
33317019Sdim *
34317019Sdim *	@(#)vfs_lookup.c	8.4 (Berkeley) 2/16/94
35317019Sdim */
36317019Sdim
37317019Sdim#include <sys/cdefs.h>
38317019Sdim__FBSDID("$FreeBSD: head/sys/kern/vfs_lookup.c 177785 2008-03-31 12:01:21Z kib $");
39317019Sdim
40317019Sdim#include "opt_ktrace.h"
41317019Sdim#include "opt_mac.h"
42317019Sdim#include "opt_vfs.h"
43317019Sdim
44317019Sdim#include <sys/param.h>
45317019Sdim#include <sys/systm.h>
46317019Sdim#include <sys/kernel.h>
47317019Sdim#include <sys/fcntl.h>
48317019Sdim#include <sys/lock.h>
49317019Sdim#include <sys/mutex.h>
50317019Sdim#include <sys/namei.h>
51317019Sdim#include <sys/vnode.h>
52317019Sdim#include <sys/mount.h>
53317019Sdim#include <sys/filedesc.h>
54317019Sdim#include <sys/proc.h>
55317019Sdim#include <sys/syscallsubr.h>
56317019Sdim#include <sys/sysctl.h>
57317019Sdim#ifdef KTRACE
58317019Sdim#include <sys/ktrace.h>
59317019Sdim#endif
60317019Sdim
61317019Sdim#include <security/audit/audit.h>
62317019Sdim#include <security/mac/mac_framework.h>
63317019Sdim
64317019Sdim#include <vm/uma.h>
65317019Sdim
66317019Sdim#define	NAMEI_DIAGNOSTIC 1
67317019Sdim#undef NAMEI_DIAGNOSTIC
68317019Sdim
69317019Sdim/*
70317019Sdim * Allocation zone for namei
71317019Sdim */
72317019Sdimuma_zone_t namei_zone;
73317019Sdim/*
74317019Sdim * Placeholder vnode for mp traversal
75317019Sdim */
76317019Sdimstatic struct vnode *vp_crossmp;
77317019Sdim
78317019Sdimstatic void
79317019Sdimnameiinit(void *dummy __unused)
80317019Sdim{
81317019Sdim	int error;
82317019Sdim
83317019Sdim	namei_zone = uma_zcreate("NAMEI", MAXPATHLEN, NULL, NULL, NULL, NULL,
84317019Sdim	    UMA_ALIGN_PTR, 0);
85317019Sdim	error = getnewvnode("crossmp", NULL, &dead_vnodeops, &vp_crossmp);
86317019Sdim	if (error != 0)
87317019Sdim		panic("nameiinit: getnewvnode");
88317019Sdim	VN_LOCK_ASHARE(vp_crossmp);
89317019Sdim}
90317019SdimSYSINIT(vfs, SI_SUB_VFS, SI_ORDER_SECOND, nameiinit, NULL);
91317019Sdim
92317019Sdim#ifdef LOOKUP_SHARED
93317019Sdimstatic int lookup_shared = 1;
94317019Sdim#else
95317019Sdimstatic int lookup_shared = 0;
96317019Sdim#endif
97317019SdimSYSCTL_INT(_vfs, OID_AUTO, lookup_shared, CTLFLAG_RW, &lookup_shared, 0,
98317019Sdim    "Enables/Disables shared locks for path name translation");
99317019Sdim
100317019Sdim/*
101317019Sdim * Convert a pathname into a pointer to a locked vnode.
102317019Sdim *
103317019Sdim * The FOLLOW flag is set when symbolic links are to be followed
104317019Sdim * when they occur at the end of the name translation process.
105317019Sdim * Symbolic links are always followed for all other pathname
106317019Sdim * components other than the last.
107317019Sdim *
108317019Sdim * The segflg defines whether the name is to be copied from user
109317019Sdim * space or kernel space.
110317019Sdim *
111317019Sdim * Overall outline of namei:
112317019Sdim *
113317019Sdim *	copy in name
114317019Sdim *	get starting directory
115317019Sdim *	while (!done && !error) {
116317019Sdim *		call lookup to search path.
117317019Sdim *		if symbolic link, massage name in buffer and continue
118317019Sdim *	}
119317019Sdim */
120317019Sdimint
121317019Sdimnamei(struct nameidata *ndp)
122317019Sdim{
123317019Sdim	struct filedesc *fdp;	/* pointer to file descriptor state */
124317019Sdim	char *cp;		/* pointer into pathname argument */
125317019Sdim	struct vnode *dp;	/* the directory we are searching */
126317019Sdim	struct iovec aiov;		/* uio for reading symbolic links */
127317019Sdim	struct uio auio;
128317019Sdim	int error, linklen;
129317019Sdim	struct componentname *cnp = &ndp->ni_cnd;
130317019Sdim	struct thread *td = cnp->cn_thread;
131317019Sdim	struct proc *p = td->td_proc;
132317019Sdim	int vfslocked;
133317019Sdim
134317019Sdim	KASSERT((cnp->cn_flags & MPSAFE) != 0 || mtx_owned(&Giant) != 0,
135317019Sdim	    ("NOT MPSAFE and Giant not held"));
136317019Sdim	ndp->ni_cnd.cn_cred = ndp->ni_cnd.cn_thread->td_ucred;
137317019Sdim	KASSERT(cnp->cn_cred && p, ("namei: bad cred/proc"));
138317019Sdim	KASSERT((cnp->cn_nameiop & (~OPMASK)) == 0,
139317019Sdim	    ("namei: nameiop contaminated with flags"));
140317019Sdim	KASSERT((cnp->cn_flags & OPMASK) == 0,
141317019Sdim	    ("namei: flags contaminated with nameiops"));
142317019Sdim	if (!lookup_shared)
143317019Sdim		cnp->cn_flags &= ~LOCKSHARED;
144317019Sdim	fdp = p->p_fd;
145317019Sdim
146317019Sdim	/*
147317019Sdim	 * Get a buffer for the name to be translated, and copy the
148317019Sdim	 * name into the buffer.
149317019Sdim	 */
150317019Sdim	if ((cnp->cn_flags & HASBUF) == 0)
151317019Sdim		cnp->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK);
152317019Sdim	if (ndp->ni_segflg == UIO_SYSSPACE)
153317019Sdim		error = copystr(ndp->ni_dirp, cnp->cn_pnbuf,
154317019Sdim			    MAXPATHLEN, (size_t *)&ndp->ni_pathlen);
155317019Sdim	else
156317019Sdim		error = copyinstr(ndp->ni_dirp, cnp->cn_pnbuf,
157317019Sdim			    MAXPATHLEN, (size_t *)&ndp->ni_pathlen);
158317019Sdim
159317019Sdim	/* If we are auditing the kernel pathname, save the user pathname. */
160317019Sdim	if (cnp->cn_flags & AUDITVNODE1)
161317019Sdim		AUDIT_ARG(upath, td, cnp->cn_pnbuf, ARG_UPATH1);
162317019Sdim	if (cnp->cn_flags & AUDITVNODE2)
163317019Sdim		AUDIT_ARG(upath, td, cnp->cn_pnbuf, ARG_UPATH2);
164317019Sdim
165317019Sdim	/*
166317019Sdim	 * Don't allow empty pathnames.
167317019Sdim	 */
168317019Sdim	if (!error && *cnp->cn_pnbuf == '\0')
169317019Sdim		error = ENOENT;
170317019Sdim
171317019Sdim	if (error) {
172317019Sdim		uma_zfree(namei_zone, cnp->cn_pnbuf);
173317019Sdim#ifdef DIAGNOSTIC
174317019Sdim		cnp->cn_pnbuf = NULL;
175317019Sdim		cnp->cn_nameptr = NULL;
176317019Sdim#endif
177317019Sdim		ndp->ni_vp = NULL;
178317019Sdim		return (error);
179317019Sdim	}
180317019Sdim	ndp->ni_loopcnt = 0;
181317019Sdim#ifdef KTRACE
182317019Sdim	if (KTRPOINT(td, KTR_NAMEI)) {
183317019Sdim		KASSERT(cnp->cn_thread == curthread,
184317019Sdim		    ("namei not using curthread"));
185317019Sdim		ktrnamei(cnp->cn_pnbuf);
186317019Sdim	}
187317019Sdim#endif
188317019Sdim
189317019Sdim	/*
190317019Sdim	 * Get starting point for the translation.
191317019Sdim	 */
192317019Sdim	FILEDESC_SLOCK(fdp);
193317019Sdim	ndp->ni_rootdir = fdp->fd_rdir;
194317019Sdim	ndp->ni_topdir = fdp->fd_jdir;
195317019Sdim
196317019Sdim	if (cnp->cn_pnbuf[0] != '/' && ndp->ni_dirfd != AT_FDCWD) {
197317019Sdim		error = fgetvp(td, ndp->ni_dirfd, &dp);
198317019Sdim		FILEDESC_SUNLOCK(fdp);
199317019Sdim		if (error == 0 && dp->v_type != VDIR) {
200317019Sdim			vfslocked = VFS_LOCK_GIANT(dp->v_mount);
201317019Sdim			vrele(dp);
202317019Sdim			VFS_UNLOCK_GIANT(vfslocked);
203317019Sdim			error = ENOTDIR;
204317019Sdim		}
205317019Sdim		if (error) {
206317019Sdim			uma_zfree(namei_zone, cnp->cn_pnbuf);
207317019Sdim#ifdef DIAGNOSTIC
208317019Sdim			cnp->cn_pnbuf = NULL;
209317019Sdim			cnp->cn_nameptr = NULL;
210317019Sdim#endif
211317019Sdim			return (error);
212317019Sdim		}
213317019Sdim	} else {
214317019Sdim		dp = fdp->fd_cdir;
215317019Sdim		VREF(dp);
216317019Sdim		FILEDESC_SUNLOCK(fdp);
217317019Sdim	}
218317019Sdim	vfslocked = VFS_LOCK_GIANT(dp->v_mount);
219317019Sdim	for (;;) {
220317019Sdim		/*
221317019Sdim		 * Check if root directory should replace current directory.
222317019Sdim		 * Done at start of translation and after symbolic link.
223317019Sdim		 */
224317019Sdim		cnp->cn_nameptr = cnp->cn_pnbuf;
225317019Sdim		if (*(cnp->cn_nameptr) == '/') {
226317019Sdim			vrele(dp);
227317019Sdim			VFS_UNLOCK_GIANT(vfslocked);
228317019Sdim			while (*(cnp->cn_nameptr) == '/') {
229317019Sdim				cnp->cn_nameptr++;
230317019Sdim				ndp->ni_pathlen--;
231317019Sdim			}
232317019Sdim			dp = ndp->ni_rootdir;
233317019Sdim			vfslocked = VFS_LOCK_GIANT(dp->v_mount);
234317019Sdim			VREF(dp);
235317019Sdim		}
236317019Sdim		if (vfslocked)
237317019Sdim			ndp->ni_cnd.cn_flags |= GIANTHELD;
238317019Sdim		ndp->ni_startdir = dp;
239317019Sdim		error = lookup(ndp);
240317019Sdim		if (error) {
241317019Sdim			uma_zfree(namei_zone, cnp->cn_pnbuf);
242317019Sdim#ifdef DIAGNOSTIC
243317019Sdim			cnp->cn_pnbuf = NULL;
244317019Sdim			cnp->cn_nameptr = NULL;
245317019Sdim#endif
246317019Sdim			return (error);
247317019Sdim		}
248317019Sdim		vfslocked = (ndp->ni_cnd.cn_flags & GIANTHELD) != 0;
249317019Sdim		ndp->ni_cnd.cn_flags &= ~GIANTHELD;
250317019Sdim		/*
251317019Sdim		 * Check for symbolic link
252317019Sdim		 */
253317019Sdim		if ((cnp->cn_flags & ISSYMLINK) == 0) {
254317019Sdim			if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0) {
255317019Sdim				uma_zfree(namei_zone, cnp->cn_pnbuf);
256317019Sdim#ifdef DIAGNOSTIC
257317019Sdim				cnp->cn_pnbuf = NULL;
258317019Sdim				cnp->cn_nameptr = NULL;
259317019Sdim#endif
260317019Sdim			} else
261317019Sdim				cnp->cn_flags |= HASBUF;
262317019Sdim
263317019Sdim			if ((cnp->cn_flags & MPSAFE) == 0) {
264317019Sdim				VFS_UNLOCK_GIANT(vfslocked);
265317019Sdim			} else if (vfslocked)
266317019Sdim				ndp->ni_cnd.cn_flags |= GIANTHELD;
267317019Sdim			return (0);
268317019Sdim		}
269317019Sdim		if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
270317019Sdim			error = ELOOP;
271317019Sdim			break;
272317019Sdim		}
273317019Sdim#ifdef MAC
274317019Sdim		if ((cnp->cn_flags & NOMACCHECK) == 0) {
275317019Sdim			error = mac_vnode_check_readlink(td->td_ucred,
276317019Sdim			    ndp->ni_vp);
277317019Sdim			if (error)
278317019Sdim				break;
279317019Sdim		}
280317019Sdim#endif
281317019Sdim		if (ndp->ni_pathlen > 1)
282317019Sdim			cp = uma_zalloc(namei_zone, M_WAITOK);
283317019Sdim		else
284317019Sdim			cp = cnp->cn_pnbuf;
285317019Sdim		aiov.iov_base = cp;
286317019Sdim		aiov.iov_len = MAXPATHLEN;
287317019Sdim		auio.uio_iov = &aiov;
288317019Sdim		auio.uio_iovcnt = 1;
289317019Sdim		auio.uio_offset = 0;
290317019Sdim		auio.uio_rw = UIO_READ;
291317019Sdim		auio.uio_segflg = UIO_SYSSPACE;
292317019Sdim		auio.uio_td = (struct thread *)0;
293317019Sdim		auio.uio_resid = MAXPATHLEN;
294317019Sdim		error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
295317019Sdim		if (error) {
296317019Sdim			if (ndp->ni_pathlen > 1)
297317019Sdim				uma_zfree(namei_zone, cp);
298317019Sdim			break;
299317019Sdim		}
300317019Sdim		linklen = MAXPATHLEN - auio.uio_resid;
301317019Sdim		if (linklen == 0) {
302317019Sdim			if (ndp->ni_pathlen > 1)
303317019Sdim				uma_zfree(namei_zone, cp);
304317019Sdim			error = ENOENT;
305317019Sdim			break;
306317019Sdim		}
307317019Sdim		if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
308317019Sdim			if (ndp->ni_pathlen > 1)
309317019Sdim				uma_zfree(namei_zone, cp);
310317019Sdim			error = ENAMETOOLONG;
311317019Sdim			break;
312317019Sdim		}
313317019Sdim		if (ndp->ni_pathlen > 1) {
314317019Sdim			bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
315317019Sdim			uma_zfree(namei_zone, cnp->cn_pnbuf);
316317019Sdim			cnp->cn_pnbuf = cp;
317317019Sdim		} else
318317019Sdim			cnp->cn_pnbuf[linklen] = '\0';
319317019Sdim		ndp->ni_pathlen += linklen;
320317019Sdim		vput(ndp->ni_vp);
321317019Sdim		dp = ndp->ni_dvp;
322317019Sdim	}
323317019Sdim	uma_zfree(namei_zone, cnp->cn_pnbuf);
324317019Sdim#ifdef DIAGNOSTIC
325317019Sdim	cnp->cn_pnbuf = NULL;
326317019Sdim	cnp->cn_nameptr = NULL;
327317019Sdim#endif
328317019Sdim	vput(ndp->ni_vp);
329317019Sdim	ndp->ni_vp = NULL;
330317019Sdim	vrele(ndp->ni_dvp);
331317019Sdim	VFS_UNLOCK_GIANT(vfslocked);
332317019Sdim	return (error);
333317019Sdim}
334317019Sdim
335317019Sdimstatic int
336317019Sdimcompute_cn_lkflags(struct mount *mp, int lkflags)
337317019Sdim{
338317019Sdim	if (mp == NULL ||
339317019Sdim	    ((lkflags & LK_SHARED) && !(mp->mnt_kern_flag & MNTK_LOOKUP_SHARED))) {
340317019Sdim		lkflags &= ~LK_SHARED;
341317019Sdim		lkflags |= LK_EXCLUSIVE;
342317019Sdim	}
343317019Sdim	return lkflags;
344317019Sdim}
345317019Sdim
346317019Sdim/*
347317019Sdim * Search a pathname.
348317019Sdim * This is a very central and rather complicated routine.
349317019Sdim *
350317019Sdim * The pathname is pointed to by ni_ptr and is of length ni_pathlen.
351317019Sdim * The starting directory is taken from ni_startdir. The pathname is
352 * descended until done, or a symbolic link is encountered. The variable
353 * ni_more is clear if the path is completed; it is set to one if a
354 * symbolic link needing interpretation is encountered.
355 *
356 * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
357 * whether the name is to be looked up, created, renamed, or deleted.
358 * When CREATE, RENAME, or DELETE is specified, information usable in
359 * creating, renaming, or deleting a directory entry may be calculated.
360 * If flag has LOCKPARENT or'ed into it, the parent directory is returned
361 * locked. If flag has WANTPARENT or'ed into it, the parent directory is
362 * returned unlocked. Otherwise the parent directory is not returned. If
363 * the target of the pathname exists and LOCKLEAF is or'ed into the flag
364 * the target is returned locked, otherwise it is returned unlocked.
365 * When creating or renaming and LOCKPARENT is specified, the target may not
366 * be ".".  When deleting and LOCKPARENT is specified, the target may be ".".
367 *
368 * Overall outline of lookup:
369 *
370 * dirloop:
371 *	identify next component of name at ndp->ni_ptr
372 *	handle degenerate case where name is null string
373 *	if .. and crossing mount points and on mounted filesys, find parent
374 *	call VOP_LOOKUP routine for next component name
375 *	    directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set
376 *	    component vnode returned in ni_vp (if it exists), locked.
377 *	if result vnode is mounted on and crossing mount points,
378 *	    find mounted on vnode
379 *	if more components of name, do next level at dirloop
380 *	return the answer in ni_vp, locked if LOCKLEAF set
381 *	    if LOCKPARENT set, return locked parent in ni_dvp
382 *	    if WANTPARENT set, return unlocked parent in ni_dvp
383 */
384int
385lookup(struct nameidata *ndp)
386{
387	char *cp;		/* pointer into pathname argument */
388	struct vnode *dp = 0;	/* the directory we are searching */
389	struct vnode *tdp;		/* saved dp */
390	struct mount *mp;		/* mount table entry */
391	int docache;			/* == 0 do not cache last component */
392	int wantparent;			/* 1 => wantparent or lockparent flag */
393	int rdonly;			/* lookup read-only flag bit */
394	int trailing_slash;
395	int error = 0;
396	int dpunlocked = 0;		/* dp has already been unlocked */
397	struct componentname *cnp = &ndp->ni_cnd;
398	struct thread *td = cnp->cn_thread;
399	int vfslocked;			/* VFS Giant state for child */
400	int dvfslocked;			/* VFS Giant state for parent */
401	int tvfslocked;
402	int lkflags_save;
403
404	/*
405	 * Setup: break out flag bits into variables.
406	 */
407	dvfslocked = (ndp->ni_cnd.cn_flags & GIANTHELD) != 0;
408	vfslocked = 0;
409	ndp->ni_cnd.cn_flags &= ~GIANTHELD;
410	wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT);
411	KASSERT(cnp->cn_nameiop == LOOKUP || wantparent,
412	    ("CREATE, DELETE, RENAME require LOCKPARENT or WANTPARENT."));
413	docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE;
414	if (cnp->cn_nameiop == DELETE ||
415	    (wantparent && cnp->cn_nameiop != CREATE &&
416	     cnp->cn_nameiop != LOOKUP))
417		docache = 0;
418	rdonly = cnp->cn_flags & RDONLY;
419	cnp->cn_flags &= ~ISSYMLINK;
420	ndp->ni_dvp = NULL;
421	/*
422	 * We use shared locks until we hit the parent of the last cn then
423	 * we adjust based on the requesting flags.
424	 */
425	if (lookup_shared)
426		cnp->cn_lkflags = LK_SHARED;
427	else
428		cnp->cn_lkflags = LK_EXCLUSIVE;
429	dp = ndp->ni_startdir;
430	ndp->ni_startdir = NULLVP;
431	vn_lock(dp,
432	    compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags | LK_RETRY));
433
434dirloop:
435	/*
436	 * Search a new directory.
437	 *
438	 * The last component of the filename is left accessible via
439	 * cnp->cn_nameptr for callers that need the name. Callers needing
440	 * the name set the SAVENAME flag. When done, they assume
441	 * responsibility for freeing the pathname buffer.
442	 */
443	cnp->cn_consume = 0;
444	for (cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++)
445		continue;
446	cnp->cn_namelen = cp - cnp->cn_nameptr;
447	if (cnp->cn_namelen > NAME_MAX) {
448		error = ENAMETOOLONG;
449		goto bad;
450	}
451#ifdef NAMEI_DIAGNOSTIC
452	{ char c = *cp;
453	*cp = '\0';
454	printf("{%s}: ", cnp->cn_nameptr);
455	*cp = c; }
456#endif
457	ndp->ni_pathlen -= cnp->cn_namelen;
458	ndp->ni_next = cp;
459
460	/*
461	 * Replace multiple slashes by a single slash and trailing slashes
462	 * by a null.  This must be done before VOP_LOOKUP() because some
463	 * fs's don't know about trailing slashes.  Remember if there were
464	 * trailing slashes to handle symlinks, existing non-directories
465	 * and non-existing files that won't be directories specially later.
466	 */
467	trailing_slash = 0;
468	while (*cp == '/' && (cp[1] == '/' || cp[1] == '\0')) {
469		cp++;
470		ndp->ni_pathlen--;
471		if (*cp == '\0') {
472			trailing_slash = 1;
473			*ndp->ni_next = '\0';	/* XXX for direnter() ... */
474		}
475	}
476	ndp->ni_next = cp;
477
478	cnp->cn_flags |= MAKEENTRY;
479	if (*cp == '\0' && docache == 0)
480		cnp->cn_flags &= ~MAKEENTRY;
481	if (cnp->cn_namelen == 2 &&
482	    cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.')
483		cnp->cn_flags |= ISDOTDOT;
484	else
485		cnp->cn_flags &= ~ISDOTDOT;
486	if (*ndp->ni_next == 0)
487		cnp->cn_flags |= ISLASTCN;
488	else
489		cnp->cn_flags &= ~ISLASTCN;
490
491
492	/*
493	 * Check for degenerate name (e.g. / or "")
494	 * which is a way of talking about a directory,
495	 * e.g. like "/." or ".".
496	 */
497	if (cnp->cn_nameptr[0] == '\0') {
498		if (dp->v_type != VDIR) {
499			error = ENOTDIR;
500			goto bad;
501		}
502		if (cnp->cn_nameiop != LOOKUP) {
503			error = EISDIR;
504			goto bad;
505		}
506		if (wantparent) {
507			ndp->ni_dvp = dp;
508			VREF(dp);
509		}
510		ndp->ni_vp = dp;
511
512		if (cnp->cn_flags & AUDITVNODE1)
513			AUDIT_ARG(vnode, dp, ARG_VNODE1);
514		else if (cnp->cn_flags & AUDITVNODE2)
515			AUDIT_ARG(vnode, dp, ARG_VNODE2);
516
517		if (!(cnp->cn_flags & (LOCKPARENT | LOCKLEAF)))
518			VOP_UNLOCK(dp, 0);
519		/* XXX This should probably move to the top of function. */
520		if (cnp->cn_flags & SAVESTART)
521			panic("lookup: SAVESTART");
522		goto success;
523	}
524
525	/*
526	 * Handle "..": four special cases.
527	 * 1. Return an error if this is the last component of
528	 *    the name and the operation is DELETE or RENAME.
529	 * 2. If at root directory (e.g. after chroot)
530	 *    or at absolute root directory
531	 *    then ignore it so can't get out.
532	 * 3. If this vnode is the root of a mounted
533	 *    filesystem, then replace it with the
534	 *    vnode which was mounted on so we take the
535	 *    .. in the other filesystem.
536	 * 4. If the vnode is the top directory of
537	 *    the jail or chroot, don't let them out.
538	 */
539	if (cnp->cn_flags & ISDOTDOT) {
540		if ((cnp->cn_flags & ISLASTCN) != 0 &&
541		    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
542			error = EINVAL;
543			goto bad;
544		}
545		for (;;) {
546			if (dp == ndp->ni_rootdir ||
547			    dp == ndp->ni_topdir ||
548			    dp == rootvnode ||
549			    ((dp->v_vflag & VV_ROOT) != 0 &&
550			     (cnp->cn_flags & NOCROSSMOUNT) != 0)) {
551				ndp->ni_dvp = dp;
552				ndp->ni_vp = dp;
553				vfslocked = VFS_LOCK_GIANT(dp->v_mount);
554				VREF(dp);
555				goto nextname;
556			}
557			if ((dp->v_vflag & VV_ROOT) == 0)
558				break;
559			if (dp->v_iflag & VI_DOOMED) {	/* forced unmount */
560				error = EBADF;
561				goto bad;
562			}
563			tdp = dp;
564			dp = dp->v_mount->mnt_vnodecovered;
565			tvfslocked = dvfslocked;
566			dvfslocked = VFS_LOCK_GIANT(dp->v_mount);
567			VREF(dp);
568			vput(tdp);
569			VFS_UNLOCK_GIANT(tvfslocked);
570			vn_lock(dp,
571			    compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags |
572			    LK_RETRY));
573		}
574	}
575
576	/*
577	 * We now have a segment name to search for, and a directory to search.
578	 */
579unionlookup:
580#ifdef MAC
581	if ((cnp->cn_flags & NOMACCHECK) == 0) {
582		error = mac_vnode_check_lookup(td->td_ucred, dp, cnp);
583		if (error)
584			goto bad;
585	}
586#endif
587	ndp->ni_dvp = dp;
588	ndp->ni_vp = NULL;
589	ASSERT_VOP_LOCKED(dp, "lookup");
590	VNASSERT(vfslocked == 0, dp, ("lookup: vfslocked %d", vfslocked));
591	/*
592	 * If we have a shared lock we may need to upgrade the lock for the
593	 * last operation.
594	 */
595	if (dp != vp_crossmp &&
596	    VOP_ISLOCKED(dp) == LK_SHARED &&
597	    (cnp->cn_flags & ISLASTCN) && (cnp->cn_flags & LOCKPARENT))
598		vn_lock(dp, LK_UPGRADE|LK_RETRY);
599	/*
600	 * If we're looking up the last component and we need an exclusive
601	 * lock, adjust our lkflags.
602	 */
603	if ((cnp->cn_flags & (ISLASTCN|LOCKSHARED|LOCKLEAF)) ==
604	    (ISLASTCN|LOCKLEAF))
605		cnp->cn_lkflags = LK_EXCLUSIVE;
606#ifdef NAMEI_DIAGNOSTIC
607	vprint("lookup in", dp);
608#endif
609	lkflags_save = cnp->cn_lkflags;
610	cnp->cn_lkflags = compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags);
611	if ((error = VOP_LOOKUP(dp, &ndp->ni_vp, cnp)) != 0) {
612		cnp->cn_lkflags = lkflags_save;
613		KASSERT(ndp->ni_vp == NULL, ("leaf should be empty"));
614#ifdef NAMEI_DIAGNOSTIC
615		printf("not found\n");
616#endif
617		if ((error == ENOENT) &&
618		    (dp->v_vflag & VV_ROOT) && (dp->v_mount != NULL) &&
619		    (dp->v_mount->mnt_flag & MNT_UNION)) {
620			tdp = dp;
621			dp = dp->v_mount->mnt_vnodecovered;
622			tvfslocked = dvfslocked;
623			dvfslocked = VFS_LOCK_GIANT(dp->v_mount);
624			VREF(dp);
625			vput(tdp);
626			VFS_UNLOCK_GIANT(tvfslocked);
627			vn_lock(dp,
628			    compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags |
629			    LK_RETRY));
630			goto unionlookup;
631		}
632
633		if (error != EJUSTRETURN)
634			goto bad;
635		/*
636		 * If creating and at end of pathname, then can consider
637		 * allowing file to be created.
638		 */
639		if (rdonly) {
640			error = EROFS;
641			goto bad;
642		}
643		if (*cp == '\0' && trailing_slash &&
644		     !(cnp->cn_flags & WILLBEDIR)) {
645			error = ENOENT;
646			goto bad;
647		}
648		if ((cnp->cn_flags & LOCKPARENT) == 0)
649			VOP_UNLOCK(dp, 0);
650		/*
651		 * This is a temporary assert to make sure I know what the
652		 * behavior here was.
653		 */
654		KASSERT((cnp->cn_flags & (WANTPARENT|LOCKPARENT)) != 0,
655		   ("lookup: Unhandled case."));
656		/*
657		 * We return with ni_vp NULL to indicate that the entry
658		 * doesn't currently exist, leaving a pointer to the
659		 * (possibly locked) directory vnode in ndp->ni_dvp.
660		 */
661		if (cnp->cn_flags & SAVESTART) {
662			ndp->ni_startdir = ndp->ni_dvp;
663			VREF(ndp->ni_startdir);
664		}
665		goto success;
666	} else
667		cnp->cn_lkflags = lkflags_save;
668#ifdef NAMEI_DIAGNOSTIC
669	printf("found\n");
670#endif
671	/*
672	 * Take into account any additional components consumed by
673	 * the underlying filesystem.
674	 */
675	if (cnp->cn_consume > 0) {
676		cnp->cn_nameptr += cnp->cn_consume;
677		ndp->ni_next += cnp->cn_consume;
678		ndp->ni_pathlen -= cnp->cn_consume;
679		cnp->cn_consume = 0;
680	}
681
682	dp = ndp->ni_vp;
683	vfslocked = VFS_LOCK_GIANT(dp->v_mount);
684
685	/*
686	 * Check to see if the vnode has been mounted on;
687	 * if so find the root of the mounted filesystem.
688	 */
689	while (dp->v_type == VDIR && (mp = dp->v_mountedhere) &&
690	       (cnp->cn_flags & NOCROSSMOUNT) == 0) {
691		if (vfs_busy(mp, 0, 0, td))
692			continue;
693		vput(dp);
694		VFS_UNLOCK_GIANT(vfslocked);
695		vfslocked = VFS_LOCK_GIANT(mp);
696		if (dp != ndp->ni_dvp)
697			vput(ndp->ni_dvp);
698		else
699			vrele(ndp->ni_dvp);
700		VFS_UNLOCK_GIANT(dvfslocked);
701		dvfslocked = 0;
702		vref(vp_crossmp);
703		ndp->ni_dvp = vp_crossmp;
704		error = VFS_ROOT(mp, compute_cn_lkflags(mp, cnp->cn_lkflags), &tdp, td);
705		vfs_unbusy(mp, td);
706		if (vn_lock(vp_crossmp, LK_SHARED | LK_NOWAIT))
707			panic("vp_crossmp exclusively locked or reclaimed");
708		if (error) {
709			dpunlocked = 1;
710			goto bad2;
711		}
712		ndp->ni_vp = dp = tdp;
713	}
714
715	/*
716	 * Check for symbolic link
717	 */
718	if ((dp->v_type == VLNK) &&
719	    ((cnp->cn_flags & FOLLOW) || trailing_slash ||
720	     *ndp->ni_next == '/')) {
721		cnp->cn_flags |= ISSYMLINK;
722		if (dp->v_iflag & VI_DOOMED) {
723			/* We can't know whether the directory was mounted with
724			 * NOSYMFOLLOW, so we can't follow safely. */
725			error = EBADF;
726			goto bad2;
727		}
728		if (dp->v_mount->mnt_flag & MNT_NOSYMFOLLOW) {
729			error = EACCES;
730			goto bad2;
731		}
732		/*
733		 * Symlink code always expects an unlocked dvp.
734		 */
735		if (ndp->ni_dvp != ndp->ni_vp)
736			VOP_UNLOCK(ndp->ni_dvp, 0);
737		goto success;
738	}
739
740	/*
741	 * Check for bogus trailing slashes.
742	 */
743	if (trailing_slash && dp->v_type != VDIR) {
744		error = ENOTDIR;
745		goto bad2;
746	}
747
748nextname:
749	/*
750	 * Not a symbolic link.  If more pathname,
751	 * continue at next component, else return.
752	 */
753	KASSERT((cnp->cn_flags & ISLASTCN) || *ndp->ni_next == '/',
754	    ("lookup: invalid path state."));
755	if (*ndp->ni_next == '/') {
756		cnp->cn_nameptr = ndp->ni_next;
757		while (*cnp->cn_nameptr == '/') {
758			cnp->cn_nameptr++;
759			ndp->ni_pathlen--;
760		}
761		if (ndp->ni_dvp != dp)
762			vput(ndp->ni_dvp);
763		else
764			vrele(ndp->ni_dvp);
765		VFS_UNLOCK_GIANT(dvfslocked);
766		dvfslocked = vfslocked;	/* dp becomes dvp in dirloop */
767		vfslocked = 0;
768		goto dirloop;
769	}
770	/*
771	 * Disallow directory write attempts on read-only filesystems.
772	 */
773	if (rdonly &&
774	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
775		error = EROFS;
776		goto bad2;
777	}
778	if (cnp->cn_flags & SAVESTART) {
779		ndp->ni_startdir = ndp->ni_dvp;
780		VREF(ndp->ni_startdir);
781	}
782	if (!wantparent) {
783		if (ndp->ni_dvp != dp)
784			vput(ndp->ni_dvp);
785		else
786			vrele(ndp->ni_dvp);
787		VFS_UNLOCK_GIANT(dvfslocked);
788		dvfslocked = 0;
789	} else if ((cnp->cn_flags & LOCKPARENT) == 0 && ndp->ni_dvp != dp)
790		VOP_UNLOCK(ndp->ni_dvp, 0);
791
792	if (cnp->cn_flags & AUDITVNODE1)
793		AUDIT_ARG(vnode, dp, ARG_VNODE1);
794	else if (cnp->cn_flags & AUDITVNODE2)
795		AUDIT_ARG(vnode, dp, ARG_VNODE2);
796
797	if ((cnp->cn_flags & LOCKLEAF) == 0)
798		VOP_UNLOCK(dp, 0);
799success:
800	/*
801	 * Because of lookup_shared we may have the vnode shared locked, but
802	 * the caller may want it to be exclusively locked.
803	 */
804	if ((cnp->cn_flags & (ISLASTCN | LOCKSHARED | LOCKLEAF)) ==
805	    (ISLASTCN | LOCKLEAF) && VOP_ISLOCKED(dp) != LK_EXCLUSIVE) {
806		vn_lock(dp, LK_UPGRADE | LK_RETRY);
807	}
808	if (vfslocked && dvfslocked)
809		VFS_UNLOCK_GIANT(dvfslocked);	/* Only need one */
810	if (vfslocked || dvfslocked)
811		ndp->ni_cnd.cn_flags |= GIANTHELD;
812	return (0);
813
814bad2:
815	if (dp != ndp->ni_dvp)
816		vput(ndp->ni_dvp);
817	else
818		vrele(ndp->ni_dvp);
819bad:
820	if (!dpunlocked)
821		vput(dp);
822	VFS_UNLOCK_GIANT(vfslocked);
823	VFS_UNLOCK_GIANT(dvfslocked);
824	ndp->ni_cnd.cn_flags &= ~GIANTHELD;
825	ndp->ni_vp = NULL;
826	return (error);
827}
828
829/*
830 * relookup - lookup a path name component
831 *    Used by lookup to re-acquire things.
832 */
833int
834relookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp)
835{
836	struct vnode *dp = 0;		/* the directory we are searching */
837	int wantparent;			/* 1 => wantparent or lockparent flag */
838	int rdonly;			/* lookup read-only flag bit */
839	int error = 0;
840
841	KASSERT(cnp->cn_flags & ISLASTCN,
842	    ("relookup: Not given last component."));
843	/*
844	 * Setup: break out flag bits into variables.
845	 */
846	wantparent = cnp->cn_flags & (LOCKPARENT|WANTPARENT);
847	KASSERT(wantparent, ("relookup: parent not wanted."));
848	rdonly = cnp->cn_flags & RDONLY;
849	cnp->cn_flags &= ~ISSYMLINK;
850	dp = dvp;
851	cnp->cn_lkflags = LK_EXCLUSIVE;
852	vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
853
854	/*
855	 * Search a new directory.
856	 *
857	 * The last component of the filename is left accessible via
858	 * cnp->cn_nameptr for callers that need the name. Callers needing
859	 * the name set the SAVENAME flag. When done, they assume
860	 * responsibility for freeing the pathname buffer.
861	 */
862#ifdef NAMEI_DIAGNOSTIC
863	printf("{%s}: ", cnp->cn_nameptr);
864#endif
865
866	/*
867	 * Check for degenerate name (e.g. / or "")
868	 * which is a way of talking about a directory,
869	 * e.g. like "/." or ".".
870	 */
871	if (cnp->cn_nameptr[0] == '\0') {
872		if (cnp->cn_nameiop != LOOKUP || wantparent) {
873			error = EISDIR;
874			goto bad;
875		}
876		if (dp->v_type != VDIR) {
877			error = ENOTDIR;
878			goto bad;
879		}
880		if (!(cnp->cn_flags & LOCKLEAF))
881			VOP_UNLOCK(dp, 0);
882		*vpp = dp;
883		/* XXX This should probably move to the top of function. */
884		if (cnp->cn_flags & SAVESTART)
885			panic("lookup: SAVESTART");
886		return (0);
887	}
888
889	if (cnp->cn_flags & ISDOTDOT)
890		panic ("relookup: lookup on dot-dot");
891
892	/*
893	 * We now have a segment name to search for, and a directory to search.
894	 */
895#ifdef NAMEI_DIAGNOSTIC
896	vprint("search in:", dp);
897#endif
898	if ((error = VOP_LOOKUP(dp, vpp, cnp)) != 0) {
899		KASSERT(*vpp == NULL, ("leaf should be empty"));
900		if (error != EJUSTRETURN)
901			goto bad;
902		/*
903		 * If creating and at end of pathname, then can consider
904		 * allowing file to be created.
905		 */
906		if (rdonly) {
907			error = EROFS;
908			goto bad;
909		}
910		/* ASSERT(dvp == ndp->ni_startdir) */
911		if (cnp->cn_flags & SAVESTART)
912			VREF(dvp);
913		if ((cnp->cn_flags & LOCKPARENT) == 0)
914			VOP_UNLOCK(dp, 0);
915		/*
916		 * This is a temporary assert to make sure I know what the
917		 * behavior here was.
918		 */
919		KASSERT((cnp->cn_flags & (WANTPARENT|LOCKPARENT)) != 0,
920		   ("relookup: Unhandled case."));
921		/*
922		 * We return with ni_vp NULL to indicate that the entry
923		 * doesn't currently exist, leaving a pointer to the
924		 * (possibly locked) directory vnode in ndp->ni_dvp.
925		 */
926		return (0);
927	}
928
929	dp = *vpp;
930
931	/*
932	 * Disallow directory write attempts on read-only filesystems.
933	 */
934	if (rdonly &&
935	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
936		if (dvp == dp)
937			vrele(dvp);
938		else
939			vput(dvp);
940		error = EROFS;
941		goto bad;
942	}
943	/*
944	 * Set the parent lock/ref state to the requested state.
945	 */
946	if ((cnp->cn_flags & LOCKPARENT) == 0 && dvp != dp) {
947		if (wantparent)
948			VOP_UNLOCK(dvp, 0);
949		else
950			vput(dvp);
951	} else if (!wantparent)
952		vrele(dvp);
953	/*
954	 * Check for symbolic link
955	 */
956	KASSERT(dp->v_type != VLNK || !(cnp->cn_flags & FOLLOW),
957	    ("relookup: symlink found.\n"));
958
959	/* ASSERT(dvp == ndp->ni_startdir) */
960	if (cnp->cn_flags & SAVESTART)
961		VREF(dvp);
962
963	if ((cnp->cn_flags & LOCKLEAF) == 0)
964		VOP_UNLOCK(dp, 0);
965	return (0);
966bad:
967	vput(dp);
968	*vpp = NULL;
969	return (error);
970}
971
972/*
973 * Free data allocated by namei(); see namei(9) for details.
974 */
975void
976NDFREE(struct nameidata *ndp, const u_int flags)
977{
978	int unlock_dvp;
979	int unlock_vp;
980
981	unlock_dvp = 0;
982	unlock_vp = 0;
983
984	if (!(flags & NDF_NO_FREE_PNBUF) &&
985	    (ndp->ni_cnd.cn_flags & HASBUF)) {
986		uma_zfree(namei_zone, ndp->ni_cnd.cn_pnbuf);
987		ndp->ni_cnd.cn_flags &= ~HASBUF;
988	}
989	if (!(flags & NDF_NO_VP_UNLOCK) &&
990	    (ndp->ni_cnd.cn_flags & LOCKLEAF) && ndp->ni_vp)
991		unlock_vp = 1;
992	if (!(flags & NDF_NO_VP_RELE) && ndp->ni_vp) {
993		if (unlock_vp) {
994			vput(ndp->ni_vp);
995			unlock_vp = 0;
996		} else
997			vrele(ndp->ni_vp);
998		ndp->ni_vp = NULL;
999	}
1000	if (unlock_vp)
1001		VOP_UNLOCK(ndp->ni_vp, 0);
1002	if (!(flags & NDF_NO_DVP_UNLOCK) &&
1003	    (ndp->ni_cnd.cn_flags & LOCKPARENT) &&
1004	    ndp->ni_dvp != ndp->ni_vp)
1005		unlock_dvp = 1;
1006	if (!(flags & NDF_NO_DVP_RELE) &&
1007	    (ndp->ni_cnd.cn_flags & (LOCKPARENT|WANTPARENT))) {
1008		if (unlock_dvp) {
1009			vput(ndp->ni_dvp);
1010			unlock_dvp = 0;
1011		} else
1012			vrele(ndp->ni_dvp);
1013		ndp->ni_dvp = NULL;
1014	}
1015	if (unlock_dvp)
1016		VOP_UNLOCK(ndp->ni_dvp, 0);
1017	if (!(flags & NDF_NO_STARTDIR_RELE) &&
1018	    (ndp->ni_cnd.cn_flags & SAVESTART)) {
1019		vrele(ndp->ni_startdir);
1020		ndp->ni_startdir = NULL;
1021	}
1022}
1023
1024/*
1025 * Determine if there is a suitable alternate filename under the specified
1026 * prefix for the specified path.  If the create flag is set, then the
1027 * alternate prefix will be used so long as the parent directory exists.
1028 * This is used by the various compatiblity ABIs so that Linux binaries prefer
1029 * files under /compat/linux for example.  The chosen path (whether under
1030 * the prefix or under /) is returned in a kernel malloc'd buffer pointed
1031 * to by pathbuf.  The caller is responsible for free'ing the buffer from
1032 * the M_TEMP bucket if one is returned.
1033 */
1034int
1035kern_alternate_path(struct thread *td, const char *prefix, char *path,
1036    enum uio_seg pathseg, char **pathbuf, int create)
1037{
1038	struct nameidata nd, ndroot;
1039	char *ptr, *buf, *cp;
1040	size_t len, sz;
1041	int error;
1042
1043	buf = (char *) malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1044	*pathbuf = buf;
1045
1046	/* Copy the prefix into the new pathname as a starting point. */
1047	len = strlcpy(buf, prefix, MAXPATHLEN);
1048	if (len >= MAXPATHLEN) {
1049		*pathbuf = NULL;
1050		free(buf, M_TEMP);
1051		return (EINVAL);
1052	}
1053	sz = MAXPATHLEN - len;
1054	ptr = buf + len;
1055
1056	/* Append the filename to the prefix. */
1057	if (pathseg == UIO_SYSSPACE)
1058		error = copystr(path, ptr, sz, &len);
1059	else
1060		error = copyinstr(path, ptr, sz, &len);
1061
1062	if (error) {
1063		*pathbuf = NULL;
1064		free(buf, M_TEMP);
1065		return (error);
1066	}
1067
1068	/* Only use a prefix with absolute pathnames. */
1069	if (*ptr != '/') {
1070		error = EINVAL;
1071		goto keeporig;
1072	}
1073
1074	/*
1075	 * We know that there is a / somewhere in this pathname.
1076	 * Search backwards for it, to find the file's parent dir
1077	 * to see if it exists in the alternate tree. If it does,
1078	 * and we want to create a file (cflag is set). We don't
1079	 * need to worry about the root comparison in this case.
1080	 */
1081
1082	if (create) {
1083		for (cp = &ptr[len] - 1; *cp != '/'; cp--);
1084		*cp = '\0';
1085
1086		NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, UIO_SYSSPACE, buf, td);
1087		error = namei(&nd);
1088		*cp = '/';
1089		if (error != 0)
1090			goto keeporig;
1091	} else {
1092		NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, UIO_SYSSPACE, buf, td);
1093
1094		error = namei(&nd);
1095		if (error != 0)
1096			goto keeporig;
1097
1098		/*
1099		 * We now compare the vnode of the prefix to the one
1100		 * vnode asked. If they resolve to be the same, then we
1101		 * ignore the match so that the real root gets used.
1102		 * This avoids the problem of traversing "../.." to find the
1103		 * root directory and never finding it, because "/" resolves
1104		 * to the emulation root directory. This is expensive :-(
1105		 */
1106		NDINIT(&ndroot, LOOKUP, FOLLOW | MPSAFE, UIO_SYSSPACE, prefix,
1107		    td);
1108
1109		/* We shouldn't ever get an error from this namei(). */
1110		error = namei(&ndroot);
1111		if (error == 0) {
1112			if (nd.ni_vp == ndroot.ni_vp)
1113				error = ENOENT;
1114
1115			NDFREE(&ndroot, NDF_ONLY_PNBUF);
1116			vrele(ndroot.ni_vp);
1117			VFS_UNLOCK_GIANT(NDHASGIANT(&ndroot));
1118		}
1119	}
1120
1121	NDFREE(&nd, NDF_ONLY_PNBUF);
1122	vrele(nd.ni_vp);
1123	VFS_UNLOCK_GIANT(NDHASGIANT(&nd));
1124
1125keeporig:
1126	/* If there was an error, use the original path name. */
1127	if (error)
1128		bcopy(ptr, buf, len);
1129	return (error);
1130}
1131