xref: /freebsd/sys/kern/vfs_lookup.c (revision 830940567b49bb0c08dfaed40418999e76616909)
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_UPATH1(td, cnp->cn_pnbuf);
168 	if (cnp->cn_flags & AUDITVNODE2)
169 		AUDIT_ARG_UPATH2(td, cnp->cn_pnbuf);
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 			if (cnp->cn_flags & AUDITVNODE1)
208 				AUDIT_ARG_ATFD1(ndp->ni_dirfd);
209 			if (cnp->cn_flags & AUDITVNODE2)
210 				AUDIT_ARG_ATFD2(ndp->ni_dirfd);
211 			error = fgetvp(td, ndp->ni_dirfd, &dp);
212 		}
213 		if (error != 0 || dp != NULL) {
214 			FILEDESC_SUNLOCK(fdp);
215 			if (error == 0 && dp->v_type != VDIR) {
216 				vfslocked = VFS_LOCK_GIANT(dp->v_mount);
217 				vrele(dp);
218 				VFS_UNLOCK_GIANT(vfslocked);
219 				error = ENOTDIR;
220 			}
221 		}
222 		if (error) {
223 			uma_zfree(namei_zone, cnp->cn_pnbuf);
224 #ifdef DIAGNOSTIC
225 			cnp->cn_pnbuf = NULL;
226 			cnp->cn_nameptr = NULL;
227 #endif
228 			return (error);
229 		}
230 	}
231 	if (dp == NULL) {
232 		dp = fdp->fd_cdir;
233 		VREF(dp);
234 		FILEDESC_SUNLOCK(fdp);
235 		if (ndp->ni_startdir != NULL) {
236 			vfslocked = VFS_LOCK_GIANT(ndp->ni_startdir->v_mount);
237 			vrele(ndp->ni_startdir);
238 			VFS_UNLOCK_GIANT(vfslocked);
239 		}
240 	}
241 	SDT_PROBE(vfs, namei, lookup, entry, dp, cnp->cn_pnbuf,
242 	    cnp->cn_flags, 0, 0);
243 	vfslocked = VFS_LOCK_GIANT(dp->v_mount);
244 	for (;;) {
245 		/*
246 		 * Check if root directory should replace current directory.
247 		 * Done at start of translation and after symbolic link.
248 		 */
249 		cnp->cn_nameptr = cnp->cn_pnbuf;
250 		if (*(cnp->cn_nameptr) == '/') {
251 			vrele(dp);
252 			VFS_UNLOCK_GIANT(vfslocked);
253 			while (*(cnp->cn_nameptr) == '/') {
254 				cnp->cn_nameptr++;
255 				ndp->ni_pathlen--;
256 			}
257 			dp = ndp->ni_rootdir;
258 			vfslocked = VFS_LOCK_GIANT(dp->v_mount);
259 			VREF(dp);
260 		}
261 		if (vfslocked)
262 			ndp->ni_cnd.cn_flags |= GIANTHELD;
263 		ndp->ni_startdir = dp;
264 		error = lookup(ndp);
265 		if (error) {
266 			uma_zfree(namei_zone, cnp->cn_pnbuf);
267 #ifdef DIAGNOSTIC
268 			cnp->cn_pnbuf = NULL;
269 			cnp->cn_nameptr = NULL;
270 #endif
271 			SDT_PROBE(vfs, namei, lookup, return, error, NULL, 0,
272 			    0, 0);
273 			return (error);
274 		}
275 		vfslocked = (ndp->ni_cnd.cn_flags & GIANTHELD) != 0;
276 		ndp->ni_cnd.cn_flags &= ~GIANTHELD;
277 		/*
278 		 * If not a symbolic link, we're done.
279 		 */
280 		if ((cnp->cn_flags & ISSYMLINK) == 0) {
281 			if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0) {
282 				uma_zfree(namei_zone, cnp->cn_pnbuf);
283 #ifdef DIAGNOSTIC
284 				cnp->cn_pnbuf = NULL;
285 				cnp->cn_nameptr = NULL;
286 #endif
287 			} else
288 				cnp->cn_flags |= HASBUF;
289 
290 			if ((cnp->cn_flags & MPSAFE) == 0) {
291 				VFS_UNLOCK_GIANT(vfslocked);
292 			} else if (vfslocked)
293 				ndp->ni_cnd.cn_flags |= GIANTHELD;
294 			SDT_PROBE(vfs, namei, lookup, return, 0, ndp->ni_vp,
295 			    0, 0, 0);
296 			return (0);
297 		}
298 		if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
299 			error = ELOOP;
300 			break;
301 		}
302 #ifdef MAC
303 		if ((cnp->cn_flags & NOMACCHECK) == 0) {
304 			error = mac_vnode_check_readlink(td->td_ucred,
305 			    ndp->ni_vp);
306 			if (error)
307 				break;
308 		}
309 #endif
310 		if (ndp->ni_pathlen > 1)
311 			cp = uma_zalloc(namei_zone, M_WAITOK);
312 		else
313 			cp = cnp->cn_pnbuf;
314 		aiov.iov_base = cp;
315 		aiov.iov_len = MAXPATHLEN;
316 		auio.uio_iov = &aiov;
317 		auio.uio_iovcnt = 1;
318 		auio.uio_offset = 0;
319 		auio.uio_rw = UIO_READ;
320 		auio.uio_segflg = UIO_SYSSPACE;
321 		auio.uio_td = (struct thread *)0;
322 		auio.uio_resid = MAXPATHLEN;
323 		error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
324 		if (error) {
325 			if (ndp->ni_pathlen > 1)
326 				uma_zfree(namei_zone, cp);
327 			break;
328 		}
329 		linklen = MAXPATHLEN - auio.uio_resid;
330 		if (linklen == 0) {
331 			if (ndp->ni_pathlen > 1)
332 				uma_zfree(namei_zone, cp);
333 			error = ENOENT;
334 			break;
335 		}
336 		if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
337 			if (ndp->ni_pathlen > 1)
338 				uma_zfree(namei_zone, cp);
339 			error = ENAMETOOLONG;
340 			break;
341 		}
342 		if (ndp->ni_pathlen > 1) {
343 			bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
344 			uma_zfree(namei_zone, cnp->cn_pnbuf);
345 			cnp->cn_pnbuf = cp;
346 		} else
347 			cnp->cn_pnbuf[linklen] = '\0';
348 		ndp->ni_pathlen += linklen;
349 		vput(ndp->ni_vp);
350 		dp = ndp->ni_dvp;
351 	}
352 	uma_zfree(namei_zone, cnp->cn_pnbuf);
353 #ifdef DIAGNOSTIC
354 	cnp->cn_pnbuf = NULL;
355 	cnp->cn_nameptr = NULL;
356 #endif
357 	vput(ndp->ni_vp);
358 	ndp->ni_vp = NULL;
359 	vrele(ndp->ni_dvp);
360 	VFS_UNLOCK_GIANT(vfslocked);
361 	SDT_PROBE(vfs, namei, lookup, return, error, NULL, 0, 0, 0);
362 	return (error);
363 }
364 
365 static int
366 compute_cn_lkflags(struct mount *mp, int lkflags)
367 {
368 
369 	if (mp == NULL ||
370 	    ((lkflags & LK_SHARED) && !(mp->mnt_kern_flag & MNTK_LOOKUP_SHARED))) {
371 		lkflags &= ~LK_SHARED;
372 		lkflags |= LK_EXCLUSIVE;
373 	}
374 	return (lkflags);
375 }
376 
377 static __inline int
378 needs_exclusive_leaf(struct mount *mp, int flags)
379 {
380 
381 	/*
382 	 * Intermediate nodes can use shared locks, we only need to
383 	 * force an exclusive lock for leaf nodes.
384 	 */
385 	if ((flags & (ISLASTCN | LOCKLEAF)) != (ISLASTCN | LOCKLEAF))
386 		return (0);
387 
388 	/* Always use exclusive locks if LOCKSHARED isn't set. */
389 	if (!(flags & LOCKSHARED))
390 		return (1);
391 
392 	/*
393 	 * For lookups during open(), if the mount point supports
394 	 * extended shared operations, then use a shared lock for the
395 	 * leaf node, otherwise use an exclusive lock.
396 	 */
397 	if (flags & ISOPEN) {
398 		if (mp != NULL &&
399 		    (mp->mnt_kern_flag & MNTK_EXTENDED_SHARED))
400 			return (0);
401 		else
402 			return (1);
403 	}
404 
405 	/*
406 	 * Lookup requests outside of open() that specify LOCKSHARED
407 	 * only need a shared lock on the leaf vnode.
408 	 */
409 	return (0);
410 }
411 
412 /*
413  * Search a pathname.
414  * This is a very central and rather complicated routine.
415  *
416  * The pathname is pointed to by ni_ptr and is of length ni_pathlen.
417  * The starting directory is taken from ni_startdir. The pathname is
418  * descended until done, or a symbolic link is encountered. The variable
419  * ni_more is clear if the path is completed; it is set to one if a
420  * symbolic link needing interpretation is encountered.
421  *
422  * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
423  * whether the name is to be looked up, created, renamed, or deleted.
424  * When CREATE, RENAME, or DELETE is specified, information usable in
425  * creating, renaming, or deleting a directory entry may be calculated.
426  * If flag has LOCKPARENT or'ed into it, the parent directory is returned
427  * locked. If flag has WANTPARENT or'ed into it, the parent directory is
428  * returned unlocked. Otherwise the parent directory is not returned. If
429  * the target of the pathname exists and LOCKLEAF is or'ed into the flag
430  * the target is returned locked, otherwise it is returned unlocked.
431  * When creating or renaming and LOCKPARENT is specified, the target may not
432  * be ".".  When deleting and LOCKPARENT is specified, the target may be ".".
433  *
434  * Overall outline of lookup:
435  *
436  * dirloop:
437  *	identify next component of name at ndp->ni_ptr
438  *	handle degenerate case where name is null string
439  *	if .. and crossing mount points and on mounted filesys, find parent
440  *	call VOP_LOOKUP routine for next component name
441  *	    directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set
442  *	    component vnode returned in ni_vp (if it exists), locked.
443  *	if result vnode is mounted on and crossing mount points,
444  *	    find mounted on vnode
445  *	if more components of name, do next level at dirloop
446  *	return the answer in ni_vp, locked if LOCKLEAF set
447  *	    if LOCKPARENT set, return locked parent in ni_dvp
448  *	    if WANTPARENT set, return unlocked parent in ni_dvp
449  */
450 int
451 lookup(struct nameidata *ndp)
452 {
453 	char *cp;		/* pointer into pathname argument */
454 	struct vnode *dp = 0;	/* the directory we are searching */
455 	struct vnode *tdp;		/* saved dp */
456 	struct mount *mp;		/* mount table entry */
457 	struct prison *pr;
458 	int docache;			/* == 0 do not cache last component */
459 	int wantparent;			/* 1 => wantparent or lockparent flag */
460 	int rdonly;			/* lookup read-only flag bit */
461 	int error = 0;
462 	int dpunlocked = 0;		/* dp has already been unlocked */
463 	struct componentname *cnp = &ndp->ni_cnd;
464 	int vfslocked;			/* VFS Giant state for child */
465 	int dvfslocked;			/* VFS Giant state for parent */
466 	int tvfslocked;
467 	int lkflags_save;
468 
469 	/*
470 	 * Setup: break out flag bits into variables.
471 	 */
472 	dvfslocked = (ndp->ni_cnd.cn_flags & GIANTHELD) != 0;
473 	vfslocked = 0;
474 	ndp->ni_cnd.cn_flags &= ~GIANTHELD;
475 	wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT);
476 	KASSERT(cnp->cn_nameiop == LOOKUP || wantparent,
477 	    ("CREATE, DELETE, RENAME require LOCKPARENT or WANTPARENT."));
478 	docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE;
479 	if (cnp->cn_nameiop == DELETE ||
480 	    (wantparent && cnp->cn_nameiop != CREATE &&
481 	     cnp->cn_nameiop != LOOKUP))
482 		docache = 0;
483 	rdonly = cnp->cn_flags & RDONLY;
484 	cnp->cn_flags &= ~ISSYMLINK;
485 	ndp->ni_dvp = NULL;
486 	/*
487 	 * We use shared locks until we hit the parent of the last cn then
488 	 * we adjust based on the requesting flags.
489 	 */
490 	if (lookup_shared)
491 		cnp->cn_lkflags = LK_SHARED;
492 	else
493 		cnp->cn_lkflags = LK_EXCLUSIVE;
494 	dp = ndp->ni_startdir;
495 	ndp->ni_startdir = NULLVP;
496 	vn_lock(dp,
497 	    compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags | LK_RETRY));
498 
499 dirloop:
500 	/*
501 	 * Search a new directory.
502 	 *
503 	 * The last component of the filename is left accessible via
504 	 * cnp->cn_nameptr for callers that need the name. Callers needing
505 	 * the name set the SAVENAME flag. When done, they assume
506 	 * responsibility for freeing the pathname buffer.
507 	 */
508 	cnp->cn_consume = 0;
509 	for (cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++)
510 		continue;
511 	cnp->cn_namelen = cp - cnp->cn_nameptr;
512 	if (cnp->cn_namelen > NAME_MAX) {
513 		error = ENAMETOOLONG;
514 		goto bad;
515 	}
516 #ifdef NAMEI_DIAGNOSTIC
517 	{ char c = *cp;
518 	*cp = '\0';
519 	printf("{%s}: ", cnp->cn_nameptr);
520 	*cp = c; }
521 #endif
522 	ndp->ni_pathlen -= cnp->cn_namelen;
523 	ndp->ni_next = cp;
524 
525 	/*
526 	 * Replace multiple slashes by a single slash and trailing slashes
527 	 * by a null.  This must be done before VOP_LOOKUP() because some
528 	 * fs's don't know about trailing slashes.  Remember if there were
529 	 * trailing slashes to handle symlinks, existing non-directories
530 	 * and non-existing files that won't be directories specially later.
531 	 */
532 	while (*cp == '/' && (cp[1] == '/' || cp[1] == '\0')) {
533 		cp++;
534 		ndp->ni_pathlen--;
535 		if (*cp == '\0') {
536 			*ndp->ni_next = '\0';
537 			cnp->cn_flags |= TRAILINGSLASH;
538 		}
539 	}
540 	ndp->ni_next = cp;
541 
542 	cnp->cn_flags |= MAKEENTRY;
543 	if (*cp == '\0' && docache == 0)
544 		cnp->cn_flags &= ~MAKEENTRY;
545 	if (cnp->cn_namelen == 2 &&
546 	    cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.')
547 		cnp->cn_flags |= ISDOTDOT;
548 	else
549 		cnp->cn_flags &= ~ISDOTDOT;
550 	if (*ndp->ni_next == 0)
551 		cnp->cn_flags |= ISLASTCN;
552 	else
553 		cnp->cn_flags &= ~ISLASTCN;
554 
555 
556 	/*
557 	 * Check for degenerate name (e.g. / or "")
558 	 * which is a way of talking about a directory,
559 	 * e.g. like "/." or ".".
560 	 */
561 	if (cnp->cn_nameptr[0] == '\0') {
562 		if (dp->v_type != VDIR) {
563 			error = ENOTDIR;
564 			goto bad;
565 		}
566 		if (cnp->cn_nameiop != LOOKUP) {
567 			error = EISDIR;
568 			goto bad;
569 		}
570 		if (wantparent) {
571 			ndp->ni_dvp = dp;
572 			VREF(dp);
573 		}
574 		ndp->ni_vp = dp;
575 
576 		if (cnp->cn_flags & AUDITVNODE1)
577 			AUDIT_ARG_VNODE1(dp);
578 		else if (cnp->cn_flags & AUDITVNODE2)
579 			AUDIT_ARG_VNODE2(dp);
580 
581 		if (!(cnp->cn_flags & (LOCKPARENT | LOCKLEAF)))
582 			VOP_UNLOCK(dp, 0);
583 		/* XXX This should probably move to the top of function. */
584 		if (cnp->cn_flags & SAVESTART)
585 			panic("lookup: SAVESTART");
586 		goto success;
587 	}
588 
589 	/*
590 	 * Handle "..": four special cases.
591 	 * 1. Return an error if this is the last component of
592 	 *    the name and the operation is DELETE or RENAME.
593 	 * 2. If at root directory (e.g. after chroot)
594 	 *    or at absolute root directory
595 	 *    then ignore it so can't get out.
596 	 * 3. If this vnode is the root of a mounted
597 	 *    filesystem, then replace it with the
598 	 *    vnode which was mounted on so we take the
599 	 *    .. in the other filesystem.
600 	 * 4. If the vnode is the top directory of
601 	 *    the jail or chroot, don't let them out.
602 	 */
603 	if (cnp->cn_flags & ISDOTDOT) {
604 		if ((cnp->cn_flags & ISLASTCN) != 0 &&
605 		    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
606 			error = EINVAL;
607 			goto bad;
608 		}
609 		for (;;) {
610 			for (pr = cnp->cn_cred->cr_prison; pr != NULL;
611 			     pr = pr->pr_parent)
612 				if (dp == pr->pr_root)
613 					break;
614 			if (dp == ndp->ni_rootdir ||
615 			    dp == ndp->ni_topdir ||
616 			    dp == rootvnode ||
617 			    pr != NULL ||
618 			    ((dp->v_vflag & VV_ROOT) != 0 &&
619 			     (cnp->cn_flags & NOCROSSMOUNT) != 0)) {
620 				ndp->ni_dvp = dp;
621 				ndp->ni_vp = dp;
622 				vfslocked = VFS_LOCK_GIANT(dp->v_mount);
623 				VREF(dp);
624 				goto nextname;
625 			}
626 			if ((dp->v_vflag & VV_ROOT) == 0)
627 				break;
628 			if (dp->v_iflag & VI_DOOMED) {	/* forced unmount */
629 				error = ENOENT;
630 				goto bad;
631 			}
632 			tdp = dp;
633 			dp = dp->v_mount->mnt_vnodecovered;
634 			tvfslocked = dvfslocked;
635 			dvfslocked = VFS_LOCK_GIANT(dp->v_mount);
636 			VREF(dp);
637 			vput(tdp);
638 			VFS_UNLOCK_GIANT(tvfslocked);
639 			vn_lock(dp,
640 			    compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags |
641 			    LK_RETRY));
642 		}
643 	}
644 
645 	/*
646 	 * We now have a segment name to search for, and a directory to search.
647 	 */
648 unionlookup:
649 #ifdef MAC
650 	if ((cnp->cn_flags & NOMACCHECK) == 0) {
651 		error = mac_vnode_check_lookup(cnp->cn_thread->td_ucred, dp,
652 		    cnp);
653 		if (error)
654 			goto bad;
655 	}
656 #endif
657 	ndp->ni_dvp = dp;
658 	ndp->ni_vp = NULL;
659 	ASSERT_VOP_LOCKED(dp, "lookup");
660 	VNASSERT(vfslocked == 0, dp, ("lookup: vfslocked %d", vfslocked));
661 	/*
662 	 * If we have a shared lock we may need to upgrade the lock for the
663 	 * last operation.
664 	 */
665 	if (dp != vp_crossmp &&
666 	    VOP_ISLOCKED(dp) == LK_SHARED &&
667 	    (cnp->cn_flags & ISLASTCN) && (cnp->cn_flags & LOCKPARENT))
668 		vn_lock(dp, LK_UPGRADE|LK_RETRY);
669 	/*
670 	 * If we're looking up the last component and we need an exclusive
671 	 * lock, adjust our lkflags.
672 	 */
673 	if (needs_exclusive_leaf(dp->v_mount, cnp->cn_flags))
674 		cnp->cn_lkflags = LK_EXCLUSIVE;
675 #ifdef NAMEI_DIAGNOSTIC
676 	vprint("lookup in", dp);
677 #endif
678 	lkflags_save = cnp->cn_lkflags;
679 	cnp->cn_lkflags = compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags);
680 	if ((error = VOP_LOOKUP(dp, &ndp->ni_vp, cnp)) != 0) {
681 		cnp->cn_lkflags = lkflags_save;
682 		KASSERT(ndp->ni_vp == NULL, ("leaf should be empty"));
683 #ifdef NAMEI_DIAGNOSTIC
684 		printf("not found\n");
685 #endif
686 		if ((error == ENOENT) &&
687 		    (dp->v_vflag & VV_ROOT) && (dp->v_mount != NULL) &&
688 		    (dp->v_mount->mnt_flag & MNT_UNION)) {
689 			tdp = dp;
690 			dp = dp->v_mount->mnt_vnodecovered;
691 			tvfslocked = dvfslocked;
692 			dvfslocked = VFS_LOCK_GIANT(dp->v_mount);
693 			VREF(dp);
694 			vput(tdp);
695 			VFS_UNLOCK_GIANT(tvfslocked);
696 			vn_lock(dp,
697 			    compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags |
698 			    LK_RETRY));
699 			goto unionlookup;
700 		}
701 
702 		if (error != EJUSTRETURN)
703 			goto bad;
704 		/*
705 		 * At this point, we know we're at the end of the
706 		 * pathname.  If creating / renaming, we can consider
707 		 * allowing the file or directory to be created / renamed,
708 		 * provided we're not on a read-only filesystem.
709 		 */
710 		if (rdonly) {
711 			error = EROFS;
712 			goto bad;
713 		}
714 		/* trailing slash only allowed for directories */
715 		if ((cnp->cn_flags & TRAILINGSLASH) &&
716 		    !(cnp->cn_flags & WILLBEDIR)) {
717 			error = ENOENT;
718 			goto bad;
719 		}
720 		if ((cnp->cn_flags & LOCKPARENT) == 0)
721 			VOP_UNLOCK(dp, 0);
722 		/*
723 		 * We return with ni_vp NULL to indicate that the entry
724 		 * doesn't currently exist, leaving a pointer to the
725 		 * (possibly locked) directory vnode in ndp->ni_dvp.
726 		 */
727 		if (cnp->cn_flags & SAVESTART) {
728 			ndp->ni_startdir = ndp->ni_dvp;
729 			VREF(ndp->ni_startdir);
730 		}
731 		goto success;
732 	} else
733 		cnp->cn_lkflags = lkflags_save;
734 #ifdef NAMEI_DIAGNOSTIC
735 	printf("found\n");
736 #endif
737 	/*
738 	 * Take into account any additional components consumed by
739 	 * the underlying filesystem.
740 	 */
741 	if (cnp->cn_consume > 0) {
742 		cnp->cn_nameptr += cnp->cn_consume;
743 		ndp->ni_next += cnp->cn_consume;
744 		ndp->ni_pathlen -= cnp->cn_consume;
745 		cnp->cn_consume = 0;
746 	}
747 
748 	dp = ndp->ni_vp;
749 	vfslocked = VFS_LOCK_GIANT(dp->v_mount);
750 
751 	/*
752 	 * Check to see if the vnode has been mounted on;
753 	 * if so find the root of the mounted filesystem.
754 	 */
755 	while (dp->v_type == VDIR && (mp = dp->v_mountedhere) &&
756 	       (cnp->cn_flags & NOCROSSMOUNT) == 0) {
757 		if (vfs_busy(mp, 0))
758 			continue;
759 		vput(dp);
760 		VFS_UNLOCK_GIANT(vfslocked);
761 		vfslocked = VFS_LOCK_GIANT(mp);
762 		if (dp != ndp->ni_dvp)
763 			vput(ndp->ni_dvp);
764 		else
765 			vrele(ndp->ni_dvp);
766 		VFS_UNLOCK_GIANT(dvfslocked);
767 		dvfslocked = 0;
768 		vref(vp_crossmp);
769 		ndp->ni_dvp = vp_crossmp;
770 		error = VFS_ROOT(mp, compute_cn_lkflags(mp, cnp->cn_lkflags),
771 		    &tdp);
772 		vfs_unbusy(mp);
773 		if (vn_lock(vp_crossmp, LK_SHARED | LK_NOWAIT))
774 			panic("vp_crossmp exclusively locked or reclaimed");
775 		if (error) {
776 			dpunlocked = 1;
777 			goto bad2;
778 		}
779 		ndp->ni_vp = dp = tdp;
780 	}
781 
782 	/*
783 	 * Check for symbolic link
784 	 */
785 	if ((dp->v_type == VLNK) &&
786 	    ((cnp->cn_flags & FOLLOW) || (cnp->cn_flags & TRAILINGSLASH) ||
787 	     *ndp->ni_next == '/')) {
788 		cnp->cn_flags |= ISSYMLINK;
789 		if (dp->v_iflag & VI_DOOMED) {
790 			/*
791 			 * We can't know whether the directory was mounted with
792 			 * NOSYMFOLLOW, so we can't follow safely.
793 			 */
794 			error = ENOENT;
795 			goto bad2;
796 		}
797 		if (dp->v_mount->mnt_flag & MNT_NOSYMFOLLOW) {
798 			error = EACCES;
799 			goto bad2;
800 		}
801 		/*
802 		 * Symlink code always expects an unlocked dvp.
803 		 */
804 		if (ndp->ni_dvp != ndp->ni_vp)
805 			VOP_UNLOCK(ndp->ni_dvp, 0);
806 		goto success;
807 	}
808 
809 nextname:
810 	/*
811 	 * Not a symbolic link that we will follow.  Continue with the
812 	 * next component if there is any; otherwise, we're done.
813 	 */
814 	KASSERT((cnp->cn_flags & ISLASTCN) || *ndp->ni_next == '/',
815 	    ("lookup: invalid path state."));
816 	if (*ndp->ni_next == '/') {
817 		cnp->cn_nameptr = ndp->ni_next;
818 		while (*cnp->cn_nameptr == '/') {
819 			cnp->cn_nameptr++;
820 			ndp->ni_pathlen--;
821 		}
822 		if (ndp->ni_dvp != dp)
823 			vput(ndp->ni_dvp);
824 		else
825 			vrele(ndp->ni_dvp);
826 		VFS_UNLOCK_GIANT(dvfslocked);
827 		dvfslocked = vfslocked;	/* dp becomes dvp in dirloop */
828 		vfslocked = 0;
829 		goto dirloop;
830 	}
831 	/*
832 	 * If we're processing a path with a trailing slash,
833 	 * check that the end result is a directory.
834 	 */
835 	if ((cnp->cn_flags & TRAILINGSLASH) && dp->v_type != VDIR) {
836 		error = ENOTDIR;
837 		goto bad2;
838 	}
839 	/*
840 	 * Disallow directory write attempts on read-only filesystems.
841 	 */
842 	if (rdonly &&
843 	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
844 		error = EROFS;
845 		goto bad2;
846 	}
847 	if (cnp->cn_flags & SAVESTART) {
848 		ndp->ni_startdir = ndp->ni_dvp;
849 		VREF(ndp->ni_startdir);
850 	}
851 	if (!wantparent) {
852 		if (ndp->ni_dvp != dp)
853 			vput(ndp->ni_dvp);
854 		else
855 			vrele(ndp->ni_dvp);
856 		VFS_UNLOCK_GIANT(dvfslocked);
857 		dvfslocked = 0;
858 	} else if ((cnp->cn_flags & LOCKPARENT) == 0 && ndp->ni_dvp != dp)
859 		VOP_UNLOCK(ndp->ni_dvp, 0);
860 
861 	if (cnp->cn_flags & AUDITVNODE1)
862 		AUDIT_ARG_VNODE1(dp);
863 	else if (cnp->cn_flags & AUDITVNODE2)
864 		AUDIT_ARG_VNODE2(dp);
865 
866 	if ((cnp->cn_flags & LOCKLEAF) == 0)
867 		VOP_UNLOCK(dp, 0);
868 success:
869 	/*
870 	 * Because of lookup_shared we may have the vnode shared locked, but
871 	 * the caller may want it to be exclusively locked.
872 	 */
873 	if (needs_exclusive_leaf(dp->v_mount, cnp->cn_flags) &&
874 	    VOP_ISLOCKED(dp) != LK_EXCLUSIVE) {
875 		vn_lock(dp, LK_UPGRADE | LK_RETRY);
876 		if (dp->v_iflag & VI_DOOMED) {
877 			error = ENOENT;
878 			goto bad2;
879 		}
880 	}
881 	if (vfslocked && dvfslocked)
882 		VFS_UNLOCK_GIANT(dvfslocked);	/* Only need one */
883 	if (vfslocked || dvfslocked)
884 		ndp->ni_cnd.cn_flags |= GIANTHELD;
885 	return (0);
886 
887 bad2:
888 	if (dp != ndp->ni_dvp)
889 		vput(ndp->ni_dvp);
890 	else
891 		vrele(ndp->ni_dvp);
892 bad:
893 	if (!dpunlocked)
894 		vput(dp);
895 	VFS_UNLOCK_GIANT(vfslocked);
896 	VFS_UNLOCK_GIANT(dvfslocked);
897 	ndp->ni_cnd.cn_flags &= ~GIANTHELD;
898 	ndp->ni_vp = NULL;
899 	return (error);
900 }
901 
902 /*
903  * relookup - lookup a path name component
904  *    Used by lookup to re-acquire things.
905  */
906 int
907 relookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp)
908 {
909 	struct vnode *dp = 0;		/* the directory we are searching */
910 	int wantparent;			/* 1 => wantparent or lockparent flag */
911 	int rdonly;			/* lookup read-only flag bit */
912 	int error = 0;
913 
914 	KASSERT(cnp->cn_flags & ISLASTCN,
915 	    ("relookup: Not given last component."));
916 	/*
917 	 * Setup: break out flag bits into variables.
918 	 */
919 	wantparent = cnp->cn_flags & (LOCKPARENT|WANTPARENT);
920 	KASSERT(wantparent, ("relookup: parent not wanted."));
921 	rdonly = cnp->cn_flags & RDONLY;
922 	cnp->cn_flags &= ~ISSYMLINK;
923 	dp = dvp;
924 	cnp->cn_lkflags = LK_EXCLUSIVE;
925 	vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
926 
927 	/*
928 	 * Search a new directory.
929 	 *
930 	 * The last component of the filename is left accessible via
931 	 * cnp->cn_nameptr for callers that need the name. Callers needing
932 	 * the name set the SAVENAME flag. When done, they assume
933 	 * responsibility for freeing the pathname buffer.
934 	 */
935 #ifdef NAMEI_DIAGNOSTIC
936 	printf("{%s}: ", cnp->cn_nameptr);
937 #endif
938 
939 	/*
940 	 * Check for degenerate name (e.g. / or "")
941 	 * which is a way of talking about a directory,
942 	 * e.g. like "/." or ".".
943 	 */
944 	if (cnp->cn_nameptr[0] == '\0') {
945 		if (cnp->cn_nameiop != LOOKUP || wantparent) {
946 			error = EISDIR;
947 			goto bad;
948 		}
949 		if (dp->v_type != VDIR) {
950 			error = ENOTDIR;
951 			goto bad;
952 		}
953 		if (!(cnp->cn_flags & LOCKLEAF))
954 			VOP_UNLOCK(dp, 0);
955 		*vpp = dp;
956 		/* XXX This should probably move to the top of function. */
957 		if (cnp->cn_flags & SAVESTART)
958 			panic("lookup: SAVESTART");
959 		return (0);
960 	}
961 
962 	if (cnp->cn_flags & ISDOTDOT)
963 		panic ("relookup: lookup on dot-dot");
964 
965 	/*
966 	 * We now have a segment name to search for, and a directory to search.
967 	 */
968 #ifdef NAMEI_DIAGNOSTIC
969 	vprint("search in:", dp);
970 #endif
971 	if ((error = VOP_LOOKUP(dp, vpp, cnp)) != 0) {
972 		KASSERT(*vpp == NULL, ("leaf should be empty"));
973 		if (error != EJUSTRETURN)
974 			goto bad;
975 		/*
976 		 * If creating and at end of pathname, then can consider
977 		 * allowing file to be created.
978 		 */
979 		if (rdonly) {
980 			error = EROFS;
981 			goto bad;
982 		}
983 		/* ASSERT(dvp == ndp->ni_startdir) */
984 		if (cnp->cn_flags & SAVESTART)
985 			VREF(dvp);
986 		if ((cnp->cn_flags & LOCKPARENT) == 0)
987 			VOP_UNLOCK(dp, 0);
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