xref: /freebsd/sys/kern/vfs_lookup.c (revision 00a5db46de56179184c0f000eaacad695e2b0859)
1 /*-
2  * Copyright (c) 1982, 1986, 1989, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  * (c) UNIX System Laboratories, Inc.
5  * All or some portions of this file are derived from material licensed
6  * to the University of California by American Telephone and Telegraph
7  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8  * the permission of UNIX System Laboratories, Inc.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 4. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *	@(#)vfs_lookup.c	8.4 (Berkeley) 2/16/94
35  */
36 
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39 
40 #include "opt_kdtrace.h"
41 #include "opt_ktrace.h"
42 #include "opt_mac.h"
43 
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/fcntl.h>
48 #include <sys/lock.h>
49 #include <sys/mutex.h>
50 #include <sys/namei.h>
51 #include <sys/vnode.h>
52 #include <sys/mount.h>
53 #include <sys/filedesc.h>
54 #include <sys/proc.h>
55 #include <sys/sdt.h>
56 #include <sys/syscallsubr.h>
57 #include <sys/sysctl.h>
58 #ifdef KTRACE
59 #include <sys/ktrace.h>
60 #endif
61 
62 #include <security/audit/audit.h>
63 #include <security/mac/mac_framework.h>
64 
65 #include <vm/uma.h>
66 
67 #define	NAMEI_DIAGNOSTIC 1
68 #undef NAMEI_DIAGNOSTIC
69 
70 SDT_PROVIDER_DECLARE(vfs);
71 SDT_PROBE_DEFINE3(vfs, namei, lookup, entry, "struct vnode *", "char *",
72     "unsigned long");
73 SDT_PROBE_DEFINE2(vfs, namei, lookup, return, "int", "struct vnode *");
74 
75 /*
76  * Allocation zone for namei
77  */
78 uma_zone_t namei_zone;
79 /*
80  * Placeholder vnode for mp traversal
81  */
82 static struct vnode *vp_crossmp;
83 
84 static void
85 nameiinit(void *dummy __unused)
86 {
87 	int error;
88 
89 	namei_zone = uma_zcreate("NAMEI", MAXPATHLEN, NULL, NULL, NULL, NULL,
90 	    UMA_ALIGN_PTR, 0);
91 	error = getnewvnode("crossmp", NULL, &dead_vnodeops, &vp_crossmp);
92 	if (error != 0)
93 		panic("nameiinit: getnewvnode");
94 	VN_LOCK_ASHARE(vp_crossmp);
95 }
96 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_SECOND, nameiinit, NULL);
97 
98 static int lookup_shared = 1;
99 SYSCTL_INT(_vfs, OID_AUTO, lookup_shared, CTLFLAG_RW, &lookup_shared, 0,
100     "Enables/Disables shared locks for path name translation");
101 TUNABLE_INT("vfs.lookup_shared", &lookup_shared);
102 
103 /*
104  * Convert a pathname into a pointer to a locked vnode.
105  *
106  * The FOLLOW flag is set when symbolic links are to be followed
107  * when they occur at the end of the name translation process.
108  * Symbolic links are always followed for all other pathname
109  * components other than the last.
110  *
111  * The segflg defines whether the name is to be copied from user
112  * space or kernel space.
113  *
114  * Overall outline of namei:
115  *
116  *	copy in name
117  *	get starting directory
118  *	while (!done && !error) {
119  *		call lookup to search path.
120  *		if symbolic link, massage name in buffer and continue
121  *	}
122  */
123 int
124 namei(struct nameidata *ndp)
125 {
126 	struct filedesc *fdp;	/* pointer to file descriptor state */
127 	char *cp;		/* pointer into pathname argument */
128 	struct vnode *dp;	/* the directory we are searching */
129 	struct iovec aiov;		/* uio for reading symbolic links */
130 	struct uio auio;
131 	int error, linklen;
132 	struct componentname *cnp = &ndp->ni_cnd;
133 	struct thread *td = cnp->cn_thread;
134 	struct proc *p = td->td_proc;
135 	int vfslocked;
136 
137 	KASSERT((cnp->cn_flags & MPSAFE) != 0 || mtx_owned(&Giant) != 0,
138 	    ("NOT MPSAFE and Giant not held"));
139 	ndp->ni_cnd.cn_cred = ndp->ni_cnd.cn_thread->td_ucred;
140 	KASSERT(cnp->cn_cred && p, ("namei: bad cred/proc"));
141 	KASSERT((cnp->cn_nameiop & (~OPMASK)) == 0,
142 	    ("namei: nameiop contaminated with flags"));
143 	KASSERT((cnp->cn_flags & OPMASK) == 0,
144 	    ("namei: flags contaminated with nameiops"));
145 	if (!lookup_shared)
146 		cnp->cn_flags &= ~LOCKSHARED;
147 	fdp = p->p_fd;
148 
149 	/*
150 	 * Get a buffer for the name to be translated, and copy the
151 	 * name into the buffer.
152 	 */
153 	if ((cnp->cn_flags & HASBUF) == 0)
154 		cnp->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK);
155 	if (ndp->ni_segflg == UIO_SYSSPACE)
156 		error = copystr(ndp->ni_dirp, cnp->cn_pnbuf,
157 			    MAXPATHLEN, (size_t *)&ndp->ni_pathlen);
158 	else
159 		error = copyinstr(ndp->ni_dirp, cnp->cn_pnbuf,
160 			    MAXPATHLEN, (size_t *)&ndp->ni_pathlen);
161 
162 	/* If we are auditing the kernel pathname, save the user pathname. */
163 	if (cnp->cn_flags & AUDITVNODE1)
164 		AUDIT_ARG(upath, td, cnp->cn_pnbuf, ARG_UPATH1);
165 	if (cnp->cn_flags & AUDITVNODE2)
166 		AUDIT_ARG(upath, td, cnp->cn_pnbuf, ARG_UPATH2);
167 
168 	/*
169 	 * Don't allow empty pathnames.
170 	 */
171 	if (!error && *cnp->cn_pnbuf == '\0')
172 		error = ENOENT;
173 
174 	if (error) {
175 		uma_zfree(namei_zone, cnp->cn_pnbuf);
176 #ifdef DIAGNOSTIC
177 		cnp->cn_pnbuf = NULL;
178 		cnp->cn_nameptr = NULL;
179 #endif
180 		ndp->ni_vp = NULL;
181 		return (error);
182 	}
183 	ndp->ni_loopcnt = 0;
184 #ifdef KTRACE
185 	if (KTRPOINT(td, KTR_NAMEI)) {
186 		KASSERT(cnp->cn_thread == curthread,
187 		    ("namei not using curthread"));
188 		ktrnamei(cnp->cn_pnbuf);
189 	}
190 #endif
191 	/*
192 	 * Get starting point for the translation.
193 	 */
194 	FILEDESC_SLOCK(fdp);
195 	ndp->ni_rootdir = fdp->fd_rdir;
196 	ndp->ni_topdir = fdp->fd_jdir;
197 
198 	dp = NULL;
199 	if (cnp->cn_pnbuf[0] != '/') {
200 		if (ndp->ni_startdir != NULL) {
201 			dp = ndp->ni_startdir;
202 			error = 0;
203 		} else if (ndp->ni_dirfd != AT_FDCWD)
204 			error = fgetvp(td, ndp->ni_dirfd, &dp);
205 		if (error != 0 || dp != NULL) {
206 			FILEDESC_SUNLOCK(fdp);
207 			if (error == 0 && dp->v_type != VDIR) {
208 				vfslocked = VFS_LOCK_GIANT(dp->v_mount);
209 				vrele(dp);
210 				VFS_UNLOCK_GIANT(vfslocked);
211 				error = ENOTDIR;
212 			}
213 		}
214 		if (error) {
215 			uma_zfree(namei_zone, cnp->cn_pnbuf);
216 #ifdef DIAGNOSTIC
217 			cnp->cn_pnbuf = NULL;
218 			cnp->cn_nameptr = NULL;
219 #endif
220 			return (error);
221 		}
222 	}
223 	if (dp == NULL) {
224 		dp = fdp->fd_cdir;
225 		VREF(dp);
226 		FILEDESC_SUNLOCK(fdp);
227 		if (ndp->ni_startdir != NULL) {
228 			vfslocked = VFS_LOCK_GIANT(ndp->ni_startdir->v_mount);
229 			vrele(ndp->ni_startdir);
230 			VFS_UNLOCK_GIANT(vfslocked);
231 		}
232 	}
233 	SDT_PROBE(vfs, namei, lookup, entry, dp, cnp->cn_pnbuf,
234 	    cnp->cn_flags, 0, 0);
235 	vfslocked = VFS_LOCK_GIANT(dp->v_mount);
236 	for (;;) {
237 		/*
238 		 * Check if root directory should replace current directory.
239 		 * Done at start of translation and after symbolic link.
240 		 */
241 		cnp->cn_nameptr = cnp->cn_pnbuf;
242 		if (*(cnp->cn_nameptr) == '/') {
243 			vrele(dp);
244 			VFS_UNLOCK_GIANT(vfslocked);
245 			while (*(cnp->cn_nameptr) == '/') {
246 				cnp->cn_nameptr++;
247 				ndp->ni_pathlen--;
248 			}
249 			dp = ndp->ni_rootdir;
250 			vfslocked = VFS_LOCK_GIANT(dp->v_mount);
251 			VREF(dp);
252 		}
253 		if (vfslocked)
254 			ndp->ni_cnd.cn_flags |= GIANTHELD;
255 		ndp->ni_startdir = dp;
256 		error = lookup(ndp);
257 		if (error) {
258 			uma_zfree(namei_zone, cnp->cn_pnbuf);
259 #ifdef DIAGNOSTIC
260 			cnp->cn_pnbuf = NULL;
261 			cnp->cn_nameptr = NULL;
262 #endif
263 			SDT_PROBE(vfs, namei, lookup, return, error, NULL, 0,
264 			    0, 0);
265 			return (error);
266 		}
267 		vfslocked = (ndp->ni_cnd.cn_flags & GIANTHELD) != 0;
268 		ndp->ni_cnd.cn_flags &= ~GIANTHELD;
269 		/*
270 		 * Check for symbolic link
271 		 */
272 		if ((cnp->cn_flags & ISSYMLINK) == 0) {
273 			if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0) {
274 				uma_zfree(namei_zone, cnp->cn_pnbuf);
275 #ifdef DIAGNOSTIC
276 				cnp->cn_pnbuf = NULL;
277 				cnp->cn_nameptr = NULL;
278 #endif
279 			} else
280 				cnp->cn_flags |= HASBUF;
281 
282 			if ((cnp->cn_flags & MPSAFE) == 0) {
283 				VFS_UNLOCK_GIANT(vfslocked);
284 			} else if (vfslocked)
285 				ndp->ni_cnd.cn_flags |= GIANTHELD;
286 			SDT_PROBE(vfs, namei, lookup, return, 0, ndp->ni_vp,
287 			    0, 0, 0);
288 			return (0);
289 		}
290 		if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
291 			error = ELOOP;
292 			break;
293 		}
294 #ifdef MAC
295 		if ((cnp->cn_flags & NOMACCHECK) == 0) {
296 			error = mac_vnode_check_readlink(td->td_ucred,
297 			    ndp->ni_vp);
298 			if (error)
299 				break;
300 		}
301 #endif
302 		if (ndp->ni_pathlen > 1)
303 			cp = uma_zalloc(namei_zone, M_WAITOK);
304 		else
305 			cp = cnp->cn_pnbuf;
306 		aiov.iov_base = cp;
307 		aiov.iov_len = MAXPATHLEN;
308 		auio.uio_iov = &aiov;
309 		auio.uio_iovcnt = 1;
310 		auio.uio_offset = 0;
311 		auio.uio_rw = UIO_READ;
312 		auio.uio_segflg = UIO_SYSSPACE;
313 		auio.uio_td = (struct thread *)0;
314 		auio.uio_resid = MAXPATHLEN;
315 		error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
316 		if (error) {
317 			if (ndp->ni_pathlen > 1)
318 				uma_zfree(namei_zone, cp);
319 			break;
320 		}
321 		linklen = MAXPATHLEN - auio.uio_resid;
322 		if (linklen == 0) {
323 			if (ndp->ni_pathlen > 1)
324 				uma_zfree(namei_zone, cp);
325 			error = ENOENT;
326 			break;
327 		}
328 		if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
329 			if (ndp->ni_pathlen > 1)
330 				uma_zfree(namei_zone, cp);
331 			error = ENAMETOOLONG;
332 			break;
333 		}
334 		if (ndp->ni_pathlen > 1) {
335 			bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
336 			uma_zfree(namei_zone, cnp->cn_pnbuf);
337 			cnp->cn_pnbuf = cp;
338 		} else
339 			cnp->cn_pnbuf[linklen] = '\0';
340 		ndp->ni_pathlen += linklen;
341 		vput(ndp->ni_vp);
342 		dp = ndp->ni_dvp;
343 	}
344 	uma_zfree(namei_zone, cnp->cn_pnbuf);
345 #ifdef DIAGNOSTIC
346 	cnp->cn_pnbuf = NULL;
347 	cnp->cn_nameptr = NULL;
348 #endif
349 	vput(ndp->ni_vp);
350 	ndp->ni_vp = NULL;
351 	vrele(ndp->ni_dvp);
352 	VFS_UNLOCK_GIANT(vfslocked);
353 	SDT_PROBE(vfs, namei, lookup, return, error, NULL, 0, 0, 0);
354 	return (error);
355 }
356 
357 static int
358 compute_cn_lkflags(struct mount *mp, int lkflags)
359 {
360 
361 	if (mp == NULL ||
362 	    ((lkflags & LK_SHARED) && !(mp->mnt_kern_flag & MNTK_LOOKUP_SHARED))) {
363 		lkflags &= ~LK_SHARED;
364 		lkflags |= LK_EXCLUSIVE;
365 	}
366 	return (lkflags);
367 }
368 
369 static __inline int
370 needs_exclusive_leaf(struct mount *mp, int flags)
371 {
372 
373 	/*
374 	 * Intermediate nodes can use shared locks, we only need to
375 	 * force an exclusive lock for leaf nodes.
376 	 */
377 	if ((flags & (ISLASTCN | LOCKLEAF)) != (ISLASTCN | LOCKLEAF))
378 		return (0);
379 
380 	/* Always use exclusive locks if LOCKSHARED isn't set. */
381 	if (!(flags & LOCKSHARED))
382 		return (1);
383 
384 	/*
385 	 * For lookups during open(), if the mount point supports
386 	 * extended shared operations, then use a shared lock for the
387 	 * leaf node, otherwise use an exclusive lock.
388 	 */
389 	if (flags & ISOPEN) {
390 		if (mp != NULL &&
391 		    (mp->mnt_kern_flag & MNTK_EXTENDED_SHARED))
392 			return (0);
393 		else
394 			return (1);
395 	}
396 
397 	/*
398 	 * Lookup requests outside of open() that specify LOCKSHARED
399 	 * only need a shared lock on the leaf vnode.
400 	 */
401 	return (0);
402 }
403 
404 /*
405  * Search a pathname.
406  * This is a very central and rather complicated routine.
407  *
408  * The pathname is pointed to by ni_ptr and is of length ni_pathlen.
409  * The starting directory is taken from ni_startdir. The pathname is
410  * descended until done, or a symbolic link is encountered. The variable
411  * ni_more is clear if the path is completed; it is set to one if a
412  * symbolic link needing interpretation is encountered.
413  *
414  * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
415  * whether the name is to be looked up, created, renamed, or deleted.
416  * When CREATE, RENAME, or DELETE is specified, information usable in
417  * creating, renaming, or deleting a directory entry may be calculated.
418  * If flag has LOCKPARENT or'ed into it, the parent directory is returned
419  * locked. If flag has WANTPARENT or'ed into it, the parent directory is
420  * returned unlocked. Otherwise the parent directory is not returned. If
421  * the target of the pathname exists and LOCKLEAF is or'ed into the flag
422  * the target is returned locked, otherwise it is returned unlocked.
423  * When creating or renaming and LOCKPARENT is specified, the target may not
424  * be ".".  When deleting and LOCKPARENT is specified, the target may be ".".
425  *
426  * Overall outline of lookup:
427  *
428  * dirloop:
429  *	identify next component of name at ndp->ni_ptr
430  *	handle degenerate case where name is null string
431  *	if .. and crossing mount points and on mounted filesys, find parent
432  *	call VOP_LOOKUP routine for next component name
433  *	    directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set
434  *	    component vnode returned in ni_vp (if it exists), locked.
435  *	if result vnode is mounted on and crossing mount points,
436  *	    find mounted on vnode
437  *	if more components of name, do next level at dirloop
438  *	return the answer in ni_vp, locked if LOCKLEAF set
439  *	    if LOCKPARENT set, return locked parent in ni_dvp
440  *	    if WANTPARENT set, return unlocked parent in ni_dvp
441  */
442 int
443 lookup(struct nameidata *ndp)
444 {
445 	char *cp;		/* pointer into pathname argument */
446 	struct vnode *dp = 0;	/* the directory we are searching */
447 	struct vnode *tdp;		/* saved dp */
448 	struct mount *mp;		/* mount table entry */
449 	int docache;			/* == 0 do not cache last component */
450 	int wantparent;			/* 1 => wantparent or lockparent flag */
451 	int rdonly;			/* lookup read-only flag bit */
452 	int trailing_slash;
453 	int error = 0;
454 	int dpunlocked = 0;		/* dp has already been unlocked */
455 	struct componentname *cnp = &ndp->ni_cnd;
456 	int vfslocked;			/* VFS Giant state for child */
457 	int dvfslocked;			/* VFS Giant state for parent */
458 	int tvfslocked;
459 	int lkflags_save;
460 #ifdef AUDIT
461 	struct thread *td = curthread;
462 #endif
463 
464 	/*
465 	 * Setup: break out flag bits into variables.
466 	 */
467 	dvfslocked = (ndp->ni_cnd.cn_flags & GIANTHELD) != 0;
468 	vfslocked = 0;
469 	ndp->ni_cnd.cn_flags &= ~GIANTHELD;
470 	wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT);
471 	KASSERT(cnp->cn_nameiop == LOOKUP || wantparent,
472 	    ("CREATE, DELETE, RENAME require LOCKPARENT or WANTPARENT."));
473 	docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE;
474 	if (cnp->cn_nameiop == DELETE ||
475 	    (wantparent && cnp->cn_nameiop != CREATE &&
476 	     cnp->cn_nameiop != LOOKUP))
477 		docache = 0;
478 	rdonly = cnp->cn_flags & RDONLY;
479 	cnp->cn_flags &= ~ISSYMLINK;
480 	ndp->ni_dvp = NULL;
481 	/*
482 	 * We use shared locks until we hit the parent of the last cn then
483 	 * we adjust based on the requesting flags.
484 	 */
485 	if (lookup_shared)
486 		cnp->cn_lkflags = LK_SHARED;
487 	else
488 		cnp->cn_lkflags = LK_EXCLUSIVE;
489 	dp = ndp->ni_startdir;
490 	ndp->ni_startdir = NULLVP;
491 	vn_lock(dp,
492 	    compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags | LK_RETRY));
493 
494 dirloop:
495 	/*
496 	 * Search a new directory.
497 	 *
498 	 * The last component of the filename is left accessible via
499 	 * cnp->cn_nameptr for callers that need the name. Callers needing
500 	 * the name set the SAVENAME flag. When done, they assume
501 	 * responsibility for freeing the pathname buffer.
502 	 */
503 	cnp->cn_consume = 0;
504 	for (cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++)
505 		continue;
506 	cnp->cn_namelen = cp - cnp->cn_nameptr;
507 	if (cnp->cn_namelen > NAME_MAX) {
508 		error = ENAMETOOLONG;
509 		goto bad;
510 	}
511 #ifdef NAMEI_DIAGNOSTIC
512 	{ char c = *cp;
513 	*cp = '\0';
514 	printf("{%s}: ", cnp->cn_nameptr);
515 	*cp = c; }
516 #endif
517 	ndp->ni_pathlen -= cnp->cn_namelen;
518 	ndp->ni_next = cp;
519 
520 	/*
521 	 * Replace multiple slashes by a single slash and trailing slashes
522 	 * by a null.  This must be done before VOP_LOOKUP() because some
523 	 * fs's don't know about trailing slashes.  Remember if there were
524 	 * trailing slashes to handle symlinks, existing non-directories
525 	 * and non-existing files that won't be directories specially later.
526 	 */
527 	trailing_slash = 0;
528 	while (*cp == '/' && (cp[1] == '/' || cp[1] == '\0')) {
529 		cp++;
530 		ndp->ni_pathlen--;
531 		if (*cp == '\0') {
532 			trailing_slash = 1;
533 			*ndp->ni_next = '\0';	/* XXX for direnter() ... */
534 		}
535 	}
536 	ndp->ni_next = cp;
537 
538 	cnp->cn_flags |= MAKEENTRY;
539 	if (*cp == '\0' && docache == 0)
540 		cnp->cn_flags &= ~MAKEENTRY;
541 	if (cnp->cn_namelen == 2 &&
542 	    cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.')
543 		cnp->cn_flags |= ISDOTDOT;
544 	else
545 		cnp->cn_flags &= ~ISDOTDOT;
546 	if (*ndp->ni_next == 0)
547 		cnp->cn_flags |= ISLASTCN;
548 	else
549 		cnp->cn_flags &= ~ISLASTCN;
550 
551 
552 	/*
553 	 * Check for degenerate name (e.g. / or "")
554 	 * which is a way of talking about a directory,
555 	 * e.g. like "/." or ".".
556 	 */
557 	if (cnp->cn_nameptr[0] == '\0') {
558 		if (dp->v_type != VDIR) {
559 			error = ENOTDIR;
560 			goto bad;
561 		}
562 		if (cnp->cn_nameiop != LOOKUP) {
563 			error = EISDIR;
564 			goto bad;
565 		}
566 		if (wantparent) {
567 			ndp->ni_dvp = dp;
568 			VREF(dp);
569 		}
570 		ndp->ni_vp = dp;
571 
572 		if (cnp->cn_flags & AUDITVNODE1)
573 			AUDIT_ARG(vnode, dp, ARG_VNODE1);
574 		else if (cnp->cn_flags & AUDITVNODE2)
575 			AUDIT_ARG(vnode, dp, ARG_VNODE2);
576 
577 		if (!(cnp->cn_flags & (LOCKPARENT | LOCKLEAF)))
578 			VOP_UNLOCK(dp, 0);
579 		/* XXX This should probably move to the top of function. */
580 		if (cnp->cn_flags & SAVESTART)
581 			panic("lookup: SAVESTART");
582 		goto success;
583 	}
584 
585 	/*
586 	 * Handle "..": four special cases.
587 	 * 1. Return an error if this is the last component of
588 	 *    the name and the operation is DELETE or RENAME.
589 	 * 2. If at root directory (e.g. after chroot)
590 	 *    or at absolute root directory
591 	 *    then ignore it so can't get out.
592 	 * 3. If this vnode is the root of a mounted
593 	 *    filesystem, then replace it with the
594 	 *    vnode which was mounted on so we take the
595 	 *    .. in the other filesystem.
596 	 * 4. If the vnode is the top directory of
597 	 *    the jail or chroot, don't let them out.
598 	 */
599 	if (cnp->cn_flags & ISDOTDOT) {
600 		if ((cnp->cn_flags & ISLASTCN) != 0 &&
601 		    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
602 			error = EINVAL;
603 			goto bad;
604 		}
605 		for (;;) {
606 			if (dp == ndp->ni_rootdir ||
607 			    dp == ndp->ni_topdir ||
608 			    dp == rootvnode ||
609 			    ((dp->v_vflag & VV_ROOT) != 0 &&
610 			     (cnp->cn_flags & NOCROSSMOUNT) != 0)) {
611 				ndp->ni_dvp = dp;
612 				ndp->ni_vp = dp;
613 				vfslocked = VFS_LOCK_GIANT(dp->v_mount);
614 				VREF(dp);
615 				goto nextname;
616 			}
617 			if ((dp->v_vflag & VV_ROOT) == 0)
618 				break;
619 			if (dp->v_iflag & VI_DOOMED) {	/* forced unmount */
620 				error = ENOENT;
621 				goto bad;
622 			}
623 			tdp = dp;
624 			dp = dp->v_mount->mnt_vnodecovered;
625 			tvfslocked = dvfslocked;
626 			dvfslocked = VFS_LOCK_GIANT(dp->v_mount);
627 			VREF(dp);
628 			vput(tdp);
629 			VFS_UNLOCK_GIANT(tvfslocked);
630 			vn_lock(dp,
631 			    compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags |
632 			    LK_RETRY));
633 		}
634 	}
635 
636 	/*
637 	 * We now have a segment name to search for, and a directory to search.
638 	 */
639 unionlookup:
640 #ifdef MAC
641 	if ((cnp->cn_flags & NOMACCHECK) == 0) {
642 		error = mac_vnode_check_lookup(cnp->cn_thread->td_ucred, dp,
643 		    cnp);
644 		if (error)
645 			goto bad;
646 	}
647 #endif
648 	ndp->ni_dvp = dp;
649 	ndp->ni_vp = NULL;
650 	ASSERT_VOP_LOCKED(dp, "lookup");
651 	VNASSERT(vfslocked == 0, dp, ("lookup: vfslocked %d", vfslocked));
652 	/*
653 	 * If we have a shared lock we may need to upgrade the lock for the
654 	 * last operation.
655 	 */
656 	if (dp != vp_crossmp &&
657 	    VOP_ISLOCKED(dp) == LK_SHARED &&
658 	    (cnp->cn_flags & ISLASTCN) && (cnp->cn_flags & LOCKPARENT))
659 		vn_lock(dp, LK_UPGRADE|LK_RETRY);
660 	/*
661 	 * If we're looking up the last component and we need an exclusive
662 	 * lock, adjust our lkflags.
663 	 */
664 	if (needs_exclusive_leaf(dp->v_mount, cnp->cn_flags))
665 		cnp->cn_lkflags = LK_EXCLUSIVE;
666 #ifdef NAMEI_DIAGNOSTIC
667 	vprint("lookup in", dp);
668 #endif
669 	lkflags_save = cnp->cn_lkflags;
670 	cnp->cn_lkflags = compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags);
671 	if ((error = VOP_LOOKUP(dp, &ndp->ni_vp, cnp)) != 0) {
672 		cnp->cn_lkflags = lkflags_save;
673 		KASSERT(ndp->ni_vp == NULL, ("leaf should be empty"));
674 #ifdef NAMEI_DIAGNOSTIC
675 		printf("not found\n");
676 #endif
677 		if ((error == ENOENT) &&
678 		    (dp->v_vflag & VV_ROOT) && (dp->v_mount != NULL) &&
679 		    (dp->v_mount->mnt_flag & MNT_UNION)) {
680 			tdp = dp;
681 			dp = dp->v_mount->mnt_vnodecovered;
682 			tvfslocked = dvfslocked;
683 			dvfslocked = VFS_LOCK_GIANT(dp->v_mount);
684 			VREF(dp);
685 			vput(tdp);
686 			VFS_UNLOCK_GIANT(tvfslocked);
687 			vn_lock(dp,
688 			    compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags |
689 			    LK_RETRY));
690 			goto unionlookup;
691 		}
692 
693 		if (error != EJUSTRETURN)
694 			goto bad;
695 		/*
696 		 * If creating and at end of pathname, then can consider
697 		 * allowing file to be created.
698 		 */
699 		if (rdonly) {
700 			error = EROFS;
701 			goto bad;
702 		}
703 		if (*cp == '\0' && trailing_slash &&
704 		     !(cnp->cn_flags & WILLBEDIR)) {
705 			error = ENOENT;
706 			goto bad;
707 		}
708 		if ((cnp->cn_flags & LOCKPARENT) == 0)
709 			VOP_UNLOCK(dp, 0);
710 		/*
711 		 * This is a temporary assert to make sure I know what the
712 		 * behavior here was.
713 		 */
714 		KASSERT((cnp->cn_flags & (WANTPARENT|LOCKPARENT)) != 0,
715 		   ("lookup: Unhandled case."));
716 		/*
717 		 * We return with ni_vp NULL to indicate that the entry
718 		 * doesn't currently exist, leaving a pointer to the
719 		 * (possibly locked) directory vnode in ndp->ni_dvp.
720 		 */
721 		if (cnp->cn_flags & SAVESTART) {
722 			ndp->ni_startdir = ndp->ni_dvp;
723 			VREF(ndp->ni_startdir);
724 		}
725 		goto success;
726 	} else
727 		cnp->cn_lkflags = lkflags_save;
728 #ifdef NAMEI_DIAGNOSTIC
729 	printf("found\n");
730 #endif
731 	/*
732 	 * Take into account any additional components consumed by
733 	 * the underlying filesystem.
734 	 */
735 	if (cnp->cn_consume > 0) {
736 		cnp->cn_nameptr += cnp->cn_consume;
737 		ndp->ni_next += cnp->cn_consume;
738 		ndp->ni_pathlen -= cnp->cn_consume;
739 		cnp->cn_consume = 0;
740 	}
741 
742 	dp = ndp->ni_vp;
743 	vfslocked = VFS_LOCK_GIANT(dp->v_mount);
744 
745 	/*
746 	 * Check to see if the vnode has been mounted on;
747 	 * if so find the root of the mounted filesystem.
748 	 */
749 	while (dp->v_type == VDIR && (mp = dp->v_mountedhere) &&
750 	       (cnp->cn_flags & NOCROSSMOUNT) == 0) {
751 		if (vfs_busy(mp, 0))
752 			continue;
753 		vput(dp);
754 		VFS_UNLOCK_GIANT(vfslocked);
755 		vfslocked = VFS_LOCK_GIANT(mp);
756 		if (dp != ndp->ni_dvp)
757 			vput(ndp->ni_dvp);
758 		else
759 			vrele(ndp->ni_dvp);
760 		VFS_UNLOCK_GIANT(dvfslocked);
761 		dvfslocked = 0;
762 		vref(vp_crossmp);
763 		ndp->ni_dvp = vp_crossmp;
764 		error = VFS_ROOT(mp, compute_cn_lkflags(mp, cnp->cn_lkflags),
765 		    &tdp);
766 		vfs_unbusy(mp);
767 		if (vn_lock(vp_crossmp, LK_SHARED | LK_NOWAIT))
768 			panic("vp_crossmp exclusively locked or reclaimed");
769 		if (error) {
770 			dpunlocked = 1;
771 			goto bad2;
772 		}
773 		ndp->ni_vp = dp = tdp;
774 	}
775 
776 	/*
777 	 * Check for symbolic link
778 	 */
779 	if ((dp->v_type == VLNK) &&
780 	    ((cnp->cn_flags & FOLLOW) || trailing_slash ||
781 	     *ndp->ni_next == '/')) {
782 		cnp->cn_flags |= ISSYMLINK;
783 		if (dp->v_iflag & VI_DOOMED) {
784 			/*
785 			 * We can't know whether the directory was mounted with
786 			 * NOSYMFOLLOW, so we can't follow safely.
787 			 */
788 			error = ENOENT;
789 			goto bad2;
790 		}
791 		if (dp->v_mount->mnt_flag & MNT_NOSYMFOLLOW) {
792 			error = EACCES;
793 			goto bad2;
794 		}
795 		/*
796 		 * Symlink code always expects an unlocked dvp.
797 		 */
798 		if (ndp->ni_dvp != ndp->ni_vp)
799 			VOP_UNLOCK(ndp->ni_dvp, 0);
800 		goto success;
801 	}
802 
803 	/*
804 	 * Check for bogus trailing slashes.
805 	 */
806 	if (trailing_slash && dp->v_type != VDIR) {
807 		error = ENOTDIR;
808 		goto bad2;
809 	}
810 
811 nextname:
812 	/*
813 	 * Not a symbolic link.  If more pathname,
814 	 * continue at next component, else return.
815 	 */
816 	KASSERT((cnp->cn_flags & ISLASTCN) || *ndp->ni_next == '/',
817 	    ("lookup: invalid path state."));
818 	if (*ndp->ni_next == '/') {
819 		cnp->cn_nameptr = ndp->ni_next;
820 		while (*cnp->cn_nameptr == '/') {
821 			cnp->cn_nameptr++;
822 			ndp->ni_pathlen--;
823 		}
824 		if (ndp->ni_dvp != dp)
825 			vput(ndp->ni_dvp);
826 		else
827 			vrele(ndp->ni_dvp);
828 		VFS_UNLOCK_GIANT(dvfslocked);
829 		dvfslocked = vfslocked;	/* dp becomes dvp in dirloop */
830 		vfslocked = 0;
831 		goto dirloop;
832 	}
833 	/*
834 	 * Disallow directory write attempts on read-only filesystems.
835 	 */
836 	if (rdonly &&
837 	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
838 		error = EROFS;
839 		goto bad2;
840 	}
841 	if (cnp->cn_flags & SAVESTART) {
842 		ndp->ni_startdir = ndp->ni_dvp;
843 		VREF(ndp->ni_startdir);
844 	}
845 	if (!wantparent) {
846 		if (ndp->ni_dvp != dp)
847 			vput(ndp->ni_dvp);
848 		else
849 			vrele(ndp->ni_dvp);
850 		VFS_UNLOCK_GIANT(dvfslocked);
851 		dvfslocked = 0;
852 	} else if ((cnp->cn_flags & LOCKPARENT) == 0 && ndp->ni_dvp != dp)
853 		VOP_UNLOCK(ndp->ni_dvp, 0);
854 
855 	if (cnp->cn_flags & AUDITVNODE1)
856 		AUDIT_ARG(vnode, dp, ARG_VNODE1);
857 	else if (cnp->cn_flags & AUDITVNODE2)
858 		AUDIT_ARG(vnode, dp, ARG_VNODE2);
859 
860 	if ((cnp->cn_flags & LOCKLEAF) == 0)
861 		VOP_UNLOCK(dp, 0);
862 success:
863 	/*
864 	 * Because of lookup_shared we may have the vnode shared locked, but
865 	 * the caller may want it to be exclusively locked.
866 	 */
867 	if (needs_exclusive_leaf(dp->v_mount, cnp->cn_flags) &&
868 	    VOP_ISLOCKED(dp) != LK_EXCLUSIVE) {
869 		vn_lock(dp, LK_UPGRADE | LK_RETRY);
870 		if (dp->v_iflag & VI_DOOMED) {
871 			error = ENOENT;
872 			goto bad2;
873 		}
874 	}
875 	if (vfslocked && dvfslocked)
876 		VFS_UNLOCK_GIANT(dvfslocked);	/* Only need one */
877 	if (vfslocked || dvfslocked)
878 		ndp->ni_cnd.cn_flags |= GIANTHELD;
879 	return (0);
880 
881 bad2:
882 	if (dp != ndp->ni_dvp)
883 		vput(ndp->ni_dvp);
884 	else
885 		vrele(ndp->ni_dvp);
886 bad:
887 	if (!dpunlocked)
888 		vput(dp);
889 	VFS_UNLOCK_GIANT(vfslocked);
890 	VFS_UNLOCK_GIANT(dvfslocked);
891 	ndp->ni_cnd.cn_flags &= ~GIANTHELD;
892 	ndp->ni_vp = NULL;
893 	return (error);
894 }
895 
896 /*
897  * relookup - lookup a path name component
898  *    Used by lookup to re-acquire things.
899  */
900 int
901 relookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp)
902 {
903 	struct vnode *dp = 0;		/* the directory we are searching */
904 	int wantparent;			/* 1 => wantparent or lockparent flag */
905 	int rdonly;			/* lookup read-only flag bit */
906 	int error = 0;
907 
908 	KASSERT(cnp->cn_flags & ISLASTCN,
909 	    ("relookup: Not given last component."));
910 	/*
911 	 * Setup: break out flag bits into variables.
912 	 */
913 	wantparent = cnp->cn_flags & (LOCKPARENT|WANTPARENT);
914 	KASSERT(wantparent, ("relookup: parent not wanted."));
915 	rdonly = cnp->cn_flags & RDONLY;
916 	cnp->cn_flags &= ~ISSYMLINK;
917 	dp = dvp;
918 	cnp->cn_lkflags = LK_EXCLUSIVE;
919 	vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
920 
921 	/*
922 	 * Search a new directory.
923 	 *
924 	 * The last component of the filename is left accessible via
925 	 * cnp->cn_nameptr for callers that need the name. Callers needing
926 	 * the name set the SAVENAME flag. When done, they assume
927 	 * responsibility for freeing the pathname buffer.
928 	 */
929 #ifdef NAMEI_DIAGNOSTIC
930 	printf("{%s}: ", cnp->cn_nameptr);
931 #endif
932 
933 	/*
934 	 * Check for degenerate name (e.g. / or "")
935 	 * which is a way of talking about a directory,
936 	 * e.g. like "/." or ".".
937 	 */
938 	if (cnp->cn_nameptr[0] == '\0') {
939 		if (cnp->cn_nameiop != LOOKUP || wantparent) {
940 			error = EISDIR;
941 			goto bad;
942 		}
943 		if (dp->v_type != VDIR) {
944 			error = ENOTDIR;
945 			goto bad;
946 		}
947 		if (!(cnp->cn_flags & LOCKLEAF))
948 			VOP_UNLOCK(dp, 0);
949 		*vpp = dp;
950 		/* XXX This should probably move to the top of function. */
951 		if (cnp->cn_flags & SAVESTART)
952 			panic("lookup: SAVESTART");
953 		return (0);
954 	}
955 
956 	if (cnp->cn_flags & ISDOTDOT)
957 		panic ("relookup: lookup on dot-dot");
958 
959 	/*
960 	 * We now have a segment name to search for, and a directory to search.
961 	 */
962 #ifdef NAMEI_DIAGNOSTIC
963 	vprint("search in:", dp);
964 #endif
965 	if ((error = VOP_LOOKUP(dp, vpp, cnp)) != 0) {
966 		KASSERT(*vpp == NULL, ("leaf should be empty"));
967 		if (error != EJUSTRETURN)
968 			goto bad;
969 		/*
970 		 * If creating and at end of pathname, then can consider
971 		 * allowing file to be created.
972 		 */
973 		if (rdonly) {
974 			error = EROFS;
975 			goto bad;
976 		}
977 		/* ASSERT(dvp == ndp->ni_startdir) */
978 		if (cnp->cn_flags & SAVESTART)
979 			VREF(dvp);
980 		if ((cnp->cn_flags & LOCKPARENT) == 0)
981 			VOP_UNLOCK(dp, 0);
982 		/*
983 		 * This is a temporary assert to make sure I know what the
984 		 * behavior here was.
985 		 */
986 		KASSERT((cnp->cn_flags & (WANTPARENT|LOCKPARENT)) != 0,
987 		   ("relookup: Unhandled case."));
988 		/*
989 		 * We return with ni_vp NULL to indicate that the entry
990 		 * doesn't currently exist, leaving a pointer to the
991 		 * (possibly locked) directory vnode in ndp->ni_dvp.
992 		 */
993 		return (0);
994 	}
995 
996 	dp = *vpp;
997 
998 	/*
999 	 * Disallow directory write attempts on read-only filesystems.
1000 	 */
1001 	if (rdonly &&
1002 	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
1003 		if (dvp == dp)
1004 			vrele(dvp);
1005 		else
1006 			vput(dvp);
1007 		error = EROFS;
1008 		goto bad;
1009 	}
1010 	/*
1011 	 * Set the parent lock/ref state to the requested state.
1012 	 */
1013 	if ((cnp->cn_flags & LOCKPARENT) == 0 && dvp != dp) {
1014 		if (wantparent)
1015 			VOP_UNLOCK(dvp, 0);
1016 		else
1017 			vput(dvp);
1018 	} else if (!wantparent)
1019 		vrele(dvp);
1020 	/*
1021 	 * Check for symbolic link
1022 	 */
1023 	KASSERT(dp->v_type != VLNK || !(cnp->cn_flags & FOLLOW),
1024 	    ("relookup: symlink found.\n"));
1025 
1026 	/* ASSERT(dvp == ndp->ni_startdir) */
1027 	if (cnp->cn_flags & SAVESTART)
1028 		VREF(dvp);
1029 
1030 	if ((cnp->cn_flags & LOCKLEAF) == 0)
1031 		VOP_UNLOCK(dp, 0);
1032 	return (0);
1033 bad:
1034 	vput(dp);
1035 	*vpp = NULL;
1036 	return (error);
1037 }
1038 
1039 /*
1040  * Free data allocated by namei(); see namei(9) for details.
1041  */
1042 void
1043 NDFREE(struct nameidata *ndp, const u_int flags)
1044 {
1045 	int unlock_dvp;
1046 	int unlock_vp;
1047 
1048 	unlock_dvp = 0;
1049 	unlock_vp = 0;
1050 
1051 	if (!(flags & NDF_NO_FREE_PNBUF) &&
1052 	    (ndp->ni_cnd.cn_flags & HASBUF)) {
1053 		uma_zfree(namei_zone, ndp->ni_cnd.cn_pnbuf);
1054 		ndp->ni_cnd.cn_flags &= ~HASBUF;
1055 	}
1056 	if (!(flags & NDF_NO_VP_UNLOCK) &&
1057 	    (ndp->ni_cnd.cn_flags & LOCKLEAF) && ndp->ni_vp)
1058 		unlock_vp = 1;
1059 	if (!(flags & NDF_NO_VP_RELE) && ndp->ni_vp) {
1060 		if (unlock_vp) {
1061 			vput(ndp->ni_vp);
1062 			unlock_vp = 0;
1063 		} else
1064 			vrele(ndp->ni_vp);
1065 		ndp->ni_vp = NULL;
1066 	}
1067 	if (unlock_vp)
1068 		VOP_UNLOCK(ndp->ni_vp, 0);
1069 	if (!(flags & NDF_NO_DVP_UNLOCK) &&
1070 	    (ndp->ni_cnd.cn_flags & LOCKPARENT) &&
1071 	    ndp->ni_dvp != ndp->ni_vp)
1072 		unlock_dvp = 1;
1073 	if (!(flags & NDF_NO_DVP_RELE) &&
1074 	    (ndp->ni_cnd.cn_flags & (LOCKPARENT|WANTPARENT))) {
1075 		if (unlock_dvp) {
1076 			vput(ndp->ni_dvp);
1077 			unlock_dvp = 0;
1078 		} else
1079 			vrele(ndp->ni_dvp);
1080 		ndp->ni_dvp = NULL;
1081 	}
1082 	if (unlock_dvp)
1083 		VOP_UNLOCK(ndp->ni_dvp, 0);
1084 	if (!(flags & NDF_NO_STARTDIR_RELE) &&
1085 	    (ndp->ni_cnd.cn_flags & SAVESTART)) {
1086 		vrele(ndp->ni_startdir);
1087 		ndp->ni_startdir = NULL;
1088 	}
1089 }
1090 
1091 /*
1092  * Determine if there is a suitable alternate filename under the specified
1093  * prefix for the specified path.  If the create flag is set, then the
1094  * alternate prefix will be used so long as the parent directory exists.
1095  * This is used by the various compatiblity ABIs so that Linux binaries prefer
1096  * files under /compat/linux for example.  The chosen path (whether under
1097  * the prefix or under /) is returned in a kernel malloc'd buffer pointed
1098  * to by pathbuf.  The caller is responsible for free'ing the buffer from
1099  * the M_TEMP bucket if one is returned.
1100  */
1101 int
1102 kern_alternate_path(struct thread *td, const char *prefix, const char *path,
1103     enum uio_seg pathseg, char **pathbuf, int create, int dirfd)
1104 {
1105 	struct nameidata nd, ndroot;
1106 	char *ptr, *buf, *cp;
1107 	size_t len, sz;
1108 	int error;
1109 
1110 	buf = (char *) malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1111 	*pathbuf = buf;
1112 
1113 	/* Copy the prefix into the new pathname as a starting point. */
1114 	len = strlcpy(buf, prefix, MAXPATHLEN);
1115 	if (len >= MAXPATHLEN) {
1116 		*pathbuf = NULL;
1117 		free(buf, M_TEMP);
1118 		return (EINVAL);
1119 	}
1120 	sz = MAXPATHLEN - len;
1121 	ptr = buf + len;
1122 
1123 	/* Append the filename to the prefix. */
1124 	if (pathseg == UIO_SYSSPACE)
1125 		error = copystr(path, ptr, sz, &len);
1126 	else
1127 		error = copyinstr(path, ptr, sz, &len);
1128 
1129 	if (error) {
1130 		*pathbuf = NULL;
1131 		free(buf, M_TEMP);
1132 		return (error);
1133 	}
1134 
1135 	/* Only use a prefix with absolute pathnames. */
1136 	if (*ptr != '/') {
1137 		error = EINVAL;
1138 		goto keeporig;
1139 	}
1140 
1141 	if (dirfd != AT_FDCWD) {
1142 		/*
1143 		 * We want the original because the "prefix" is
1144 		 * included in the already opened dirfd.
1145 		 */
1146 		bcopy(ptr, buf, len);
1147 		return (0);
1148 	}
1149 
1150 	/*
1151 	 * We know that there is a / somewhere in this pathname.
1152 	 * Search backwards for it, to find the file's parent dir
1153 	 * to see if it exists in the alternate tree. If it does,
1154 	 * and we want to create a file (cflag is set). We don't
1155 	 * need to worry about the root comparison in this case.
1156 	 */
1157 
1158 	if (create) {
1159 		for (cp = &ptr[len] - 1; *cp != '/'; cp--);
1160 		*cp = '\0';
1161 
1162 		NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, UIO_SYSSPACE, buf, td);
1163 		error = namei(&nd);
1164 		*cp = '/';
1165 		if (error != 0)
1166 			goto keeporig;
1167 	} else {
1168 		NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, UIO_SYSSPACE, buf, td);
1169 
1170 		error = namei(&nd);
1171 		if (error != 0)
1172 			goto keeporig;
1173 
1174 		/*
1175 		 * We now compare the vnode of the prefix to the one
1176 		 * vnode asked. If they resolve to be the same, then we
1177 		 * ignore the match so that the real root gets used.
1178 		 * This avoids the problem of traversing "../.." to find the
1179 		 * root directory and never finding it, because "/" resolves
1180 		 * to the emulation root directory. This is expensive :-(
1181 		 */
1182 		NDINIT(&ndroot, LOOKUP, FOLLOW | MPSAFE, UIO_SYSSPACE, prefix,
1183 		    td);
1184 
1185 		/* We shouldn't ever get an error from this namei(). */
1186 		error = namei(&ndroot);
1187 		if (error == 0) {
1188 			if (nd.ni_vp == ndroot.ni_vp)
1189 				error = ENOENT;
1190 
1191 			NDFREE(&ndroot, NDF_ONLY_PNBUF);
1192 			vrele(ndroot.ni_vp);
1193 			VFS_UNLOCK_GIANT(NDHASGIANT(&ndroot));
1194 		}
1195 	}
1196 
1197 	NDFREE(&nd, NDF_ONLY_PNBUF);
1198 	vrele(nd.ni_vp);
1199 	VFS_UNLOCK_GIANT(NDHASGIANT(&nd));
1200 
1201 keeporig:
1202 	/* If there was an error, use the original path name. */
1203 	if (error)
1204 		bcopy(ptr, buf, len);
1205 	return (error);
1206 }
1207