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