xref: /freebsd/sys/kern/vfs_lookup.c (revision 145f01a3dfcba97c20ad14f2054d4b0600b23350)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1982, 1986, 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  * (c) UNIX System Laboratories, Inc.
7  * All or some portions of this file are derived from material licensed
8  * to the University of California by American Telephone and Telegraph
9  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10  * the permission of UNIX System Laboratories, Inc.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *	@(#)vfs_lookup.c	8.4 (Berkeley) 2/16/94
37  */
38 
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
41 
42 #include "opt_capsicum.h"
43 #include "opt_ktrace.h"
44 
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/capsicum.h>
49 #include <sys/fcntl.h>
50 #include <sys/jail.h>
51 #include <sys/lock.h>
52 #include <sys/mutex.h>
53 #include <sys/namei.h>
54 #include <sys/vnode.h>
55 #include <sys/mount.h>
56 #include <sys/filedesc.h>
57 #include <sys/proc.h>
58 #include <sys/sdt.h>
59 #include <sys/syscallsubr.h>
60 #include <sys/sysctl.h>
61 #ifdef KTRACE
62 #include <sys/ktrace.h>
63 #endif
64 #ifdef INVARIANTS
65 #include <machine/_inttypes.h>
66 #endif
67 
68 #include <security/audit/audit.h>
69 #include <security/mac/mac_framework.h>
70 
71 #include <vm/uma.h>
72 
73 #define	NAMEI_DIAGNOSTIC 1
74 #undef NAMEI_DIAGNOSTIC
75 
76 SDT_PROVIDER_DEFINE(vfs);
77 SDT_PROBE_DEFINE4(vfs, namei, lookup, entry, "struct vnode *", "char *",
78     "unsigned long", "bool");
79 SDT_PROBE_DEFINE4(vfs, namei, lookup, return, "int", "struct vnode *", "bool",
80     "struct nameidata");
81 
82 /* Allocation zone for namei. */
83 uma_zone_t namei_zone;
84 
85 /* Placeholder vnode for mp traversal. */
86 static struct vnode *vp_crossmp;
87 
88 static int
89 crossmp_vop_islocked(struct vop_islocked_args *ap)
90 {
91 
92 	return (LK_SHARED);
93 }
94 
95 static int
96 crossmp_vop_lock1(struct vop_lock1_args *ap)
97 {
98 	struct vnode *vp;
99 	struct lock *lk __unused;
100 	const char *file __unused;
101 	int flags, line __unused;
102 
103 	vp = ap->a_vp;
104 	lk = vp->v_vnlock;
105 	flags = ap->a_flags;
106 	file = ap->a_file;
107 	line = ap->a_line;
108 
109 	if ((flags & LK_SHARED) == 0)
110 		panic("invalid lock request for crossmp");
111 
112 	WITNESS_CHECKORDER(&lk->lock_object, LOP_NEWORDER, file, line,
113 	    flags & LK_INTERLOCK ? &VI_MTX(vp)->lock_object : NULL);
114 	WITNESS_LOCK(&lk->lock_object, 0, file, line);
115 	if ((flags & LK_INTERLOCK) != 0)
116 		VI_UNLOCK(vp);
117 	LOCK_LOG_LOCK("SLOCK", &lk->lock_object, 0, 0, ap->a_file, line);
118 	return (0);
119 }
120 
121 static int
122 crossmp_vop_unlock(struct vop_unlock_args *ap)
123 {
124 	struct vnode *vp;
125 	struct lock *lk __unused;
126 
127 	vp = ap->a_vp;
128 	lk = vp->v_vnlock;
129 
130 	WITNESS_UNLOCK(&lk->lock_object, 0, LOCK_FILE, LOCK_LINE);
131 	LOCK_LOG_LOCK("SUNLOCK", &lk->lock_object, 0, 0, LOCK_FILE,
132 	    LOCK_LINE);
133 	return (0);
134 }
135 
136 static struct vop_vector crossmp_vnodeops = {
137 	.vop_default =		&default_vnodeops,
138 	.vop_islocked =		crossmp_vop_islocked,
139 	.vop_lock1 =		crossmp_vop_lock1,
140 	.vop_unlock =		crossmp_vop_unlock,
141 };
142 /*
143  * VFS_VOP_VECTOR_REGISTER(crossmp_vnodeops) is not used here since the vnode
144  * gets allocated early. See nameiinit for the direct call below.
145  */
146 
147 struct nameicap_tracker {
148 	struct vnode *dp;
149 	TAILQ_ENTRY(nameicap_tracker) nm_link;
150 };
151 
152 /* Zone for cap mode tracker elements used for dotdot capability checks. */
153 MALLOC_DEFINE(M_NAMEITRACKER, "namei_tracker", "namei tracking for dotdot");
154 
155 static void
156 nameiinit(void *dummy __unused)
157 {
158 
159 	namei_zone = uma_zcreate("NAMEI", MAXPATHLEN, NULL, NULL, NULL, NULL,
160 	    UMA_ALIGN_PTR, 0);
161 	vfs_vector_op_register(&crossmp_vnodeops);
162 	getnewvnode("crossmp", NULL, &crossmp_vnodeops, &vp_crossmp);
163 }
164 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_SECOND, nameiinit, NULL);
165 
166 static int lookup_cap_dotdot = 1;
167 SYSCTL_INT(_vfs, OID_AUTO, lookup_cap_dotdot, CTLFLAG_RWTUN,
168     &lookup_cap_dotdot, 0,
169     "enables \"..\" components in path lookup in capability mode");
170 static int lookup_cap_dotdot_nonlocal = 1;
171 SYSCTL_INT(_vfs, OID_AUTO, lookup_cap_dotdot_nonlocal, CTLFLAG_RWTUN,
172     &lookup_cap_dotdot_nonlocal, 0,
173     "enables \"..\" components in path lookup in capability mode "
174     "on non-local mount");
175 
176 static void
177 nameicap_tracker_add(struct nameidata *ndp, struct vnode *dp)
178 {
179 	struct nameicap_tracker *nt;
180 	struct componentname *cnp;
181 
182 	if ((ndp->ni_lcf & NI_LCF_CAP_DOTDOT) == 0 || dp->v_type != VDIR)
183 		return;
184 	cnp = &ndp->ni_cnd;
185 	if ((cnp->cn_flags & BENEATH) != 0 &&
186 	    (ndp->ni_lcf & NI_LCF_BENEATH_LATCHED) == 0) {
187 		MPASS((ndp->ni_lcf & NI_LCF_LATCH) != 0);
188 		if (dp != ndp->ni_beneath_latch)
189 			return;
190 		ndp->ni_lcf |= NI_LCF_BENEATH_LATCHED;
191 	}
192 	nt = malloc(sizeof(*nt), M_NAMEITRACKER, M_WAITOK);
193 	vhold(dp);
194 	nt->dp = dp;
195 	TAILQ_INSERT_TAIL(&ndp->ni_cap_tracker, nt, nm_link);
196 }
197 
198 static void
199 nameicap_cleanup(struct nameidata *ndp, bool clean_latch)
200 {
201 	struct nameicap_tracker *nt, *nt1;
202 
203 	KASSERT(TAILQ_EMPTY(&ndp->ni_cap_tracker) ||
204 	    (ndp->ni_lcf & NI_LCF_CAP_DOTDOT) != 0, ("not strictrelative"));
205 	TAILQ_FOREACH_SAFE(nt, &ndp->ni_cap_tracker, nm_link, nt1) {
206 		TAILQ_REMOVE(&ndp->ni_cap_tracker, nt, nm_link);
207 		vdrop(nt->dp);
208 		free(nt, M_NAMEITRACKER);
209 	}
210 	if (clean_latch && (ndp->ni_lcf & NI_LCF_LATCH) != 0) {
211 		ndp->ni_lcf &= ~NI_LCF_LATCH;
212 		vrele(ndp->ni_beneath_latch);
213 	}
214 }
215 
216 /*
217  * For dotdot lookups in capability mode, only allow the component
218  * lookup to succeed if the resulting directory was already traversed
219  * during the operation.  This catches situations where already
220  * traversed directory is moved to different parent, and then we walk
221  * over it with dotdots.
222  *
223  * Also allow to force failure of dotdot lookups for non-local
224  * filesystems, where external agents might assist local lookups to
225  * escape the compartment.
226  */
227 static int
228 nameicap_check_dotdot(struct nameidata *ndp, struct vnode *dp)
229 {
230 	struct nameicap_tracker *nt;
231 	struct mount *mp;
232 
233 	if ((ndp->ni_lcf & NI_LCF_CAP_DOTDOT) == 0 || dp == NULL ||
234 	    dp->v_type != VDIR)
235 		return (0);
236 	mp = dp->v_mount;
237 	if (lookup_cap_dotdot_nonlocal == 0 && mp != NULL &&
238 	    (mp->mnt_flag & MNT_LOCAL) == 0)
239 		return (ENOTCAPABLE);
240 	TAILQ_FOREACH_REVERSE(nt, &ndp->ni_cap_tracker, nameicap_tracker_head,
241 	    nm_link) {
242 		if ((ndp->ni_lcf & NI_LCF_LATCH) != 0 &&
243 		    ndp->ni_beneath_latch == nt->dp) {
244 			ndp->ni_lcf &= ~NI_LCF_BENEATH_LATCHED;
245 			nameicap_cleanup(ndp, false);
246 			return (0);
247 		}
248 		if (dp == nt->dp)
249 			return (0);
250 	}
251 	return (ENOTCAPABLE);
252 }
253 
254 static void
255 namei_cleanup_cnp(struct componentname *cnp)
256 {
257 
258 	uma_zfree(namei_zone, cnp->cn_pnbuf);
259 #ifdef DIAGNOSTIC
260 	cnp->cn_pnbuf = NULL;
261 	cnp->cn_nameptr = NULL;
262 #endif
263 }
264 
265 static int
266 namei_handle_root(struct nameidata *ndp, struct vnode **dpp)
267 {
268 	struct componentname *cnp;
269 
270 	cnp = &ndp->ni_cnd;
271 	if ((ndp->ni_lcf & NI_LCF_STRICTRELATIVE) != 0) {
272 #ifdef KTRACE
273 		if (KTRPOINT(curthread, KTR_CAPFAIL))
274 			ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL);
275 #endif
276 		return (ENOTCAPABLE);
277 	}
278 	if ((cnp->cn_flags & BENEATH) != 0) {
279 		ndp->ni_lcf |= NI_LCF_BENEATH_ABS;
280 		ndp->ni_lcf &= ~NI_LCF_BENEATH_LATCHED;
281 		nameicap_cleanup(ndp, false);
282 	}
283 	while (*(cnp->cn_nameptr) == '/') {
284 		cnp->cn_nameptr++;
285 		ndp->ni_pathlen--;
286 	}
287 	*dpp = ndp->ni_rootdir;
288 	vrefact(*dpp);
289 	return (0);
290 }
291 
292 static int
293 namei_setup(struct nameidata *ndp, struct vnode **dpp, struct pwd **pwdp)
294 {
295 	struct componentname *cnp;
296 	struct file *dfp;
297 	struct thread *td;
298 	struct pwd *pwd;
299 	cap_rights_t rights;
300 	struct filecaps dirfd_caps;
301 	int error;
302 	bool startdir_used;
303 
304 	cnp = &ndp->ni_cnd;
305 	td = cnp->cn_thread;
306 
307 	startdir_used = false;
308 	*pwdp = NULL;
309 	*dpp = NULL;
310 
311 #ifdef CAPABILITY_MODE
312 	/*
313 	 * In capability mode, lookups must be restricted to happen in
314 	 * the subtree with the root specified by the file descriptor:
315 	 * - The root must be real file descriptor, not the pseudo-descriptor
316 	 *   AT_FDCWD.
317 	 * - The passed path must be relative and not absolute.
318 	 * - If lookup_cap_dotdot is disabled, path must not contain the
319 	 *   '..' components.
320 	 * - If lookup_cap_dotdot is enabled, we verify that all '..'
321 	 *   components lookups result in the directories which were
322 	 *   previously walked by us, which prevents an escape from
323 	 *   the relative root.
324 	 */
325 	if (IN_CAPABILITY_MODE(td) && (cnp->cn_flags & NOCAPCHECK) == 0) {
326 		ndp->ni_lcf |= NI_LCF_STRICTRELATIVE;
327 		ndp->ni_resflags |= NIRES_STRICTREL;
328 		if (ndp->ni_dirfd == AT_FDCWD) {
329 #ifdef KTRACE
330 			if (KTRPOINT(td, KTR_CAPFAIL))
331 				ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL);
332 #endif
333 			return (ECAPMODE);
334 		}
335 	}
336 #endif
337 	error = 0;
338 
339 	/*
340 	 * Get starting point for the translation.
341 	 */
342 	pwd = pwd_hold(td);
343 	/*
344 	 * The reference on ni_rootdir is acquired in the block below to avoid
345 	 * back-to-back atomics for absolute lookups.
346 	 */
347 	ndp->ni_rootdir = pwd->pwd_rdir;
348 	ndp->ni_topdir = pwd->pwd_jdir;
349 
350 	if (cnp->cn_pnbuf[0] == '/') {
351 		ndp->ni_resflags |= NIRES_ABS;
352 		error = namei_handle_root(ndp, dpp);
353 	} else {
354 		if (ndp->ni_startdir != NULL) {
355 			*dpp = ndp->ni_startdir;
356 			startdir_used = true;
357 		} else if (ndp->ni_dirfd == AT_FDCWD) {
358 			*dpp = pwd->pwd_cdir;
359 			vrefact(*dpp);
360 		} else {
361 			rights = *ndp->ni_rightsneeded;
362 			cap_rights_set_one(&rights, CAP_LOOKUP);
363 
364 			if (cnp->cn_flags & AUDITVNODE1)
365 				AUDIT_ARG_ATFD1(ndp->ni_dirfd);
366 			if (cnp->cn_flags & AUDITVNODE2)
367 				AUDIT_ARG_ATFD2(ndp->ni_dirfd);
368 			/*
369 			 * Effectively inlined fgetvp_rights, because we need to
370 			 * inspect the file as well as grabbing the vnode.
371 			 */
372 			error = fget_cap(td, ndp->ni_dirfd, &rights,
373 			    &dfp, &ndp->ni_filecaps);
374 			if (error != 0) {
375 				/*
376 				 * Preserve the error; it should either be EBADF
377 				 * or capability-related, both of which can be
378 				 * safely returned to the caller.
379 				 */
380 			} else {
381 				if (dfp->f_ops == &badfileops) {
382 					error = EBADF;
383 				} else if (dfp->f_vnode == NULL) {
384 					error = ENOTDIR;
385 				} else {
386 					*dpp = dfp->f_vnode;
387 					vrefact(*dpp);
388 
389 					if ((dfp->f_flag & FSEARCH) != 0)
390 						cnp->cn_flags |= NOEXECCHECK;
391 				}
392 				fdrop(dfp, td);
393 			}
394 #ifdef CAPABILITIES
395 			/*
396 			 * If file descriptor doesn't have all rights,
397 			 * all lookups relative to it must also be
398 			 * strictly relative.
399 			 */
400 			CAP_ALL(&rights);
401 			if (!cap_rights_contains(&ndp->ni_filecaps.fc_rights,
402 			    &rights) ||
403 			    ndp->ni_filecaps.fc_fcntls != CAP_FCNTL_ALL ||
404 			    ndp->ni_filecaps.fc_nioctls != -1) {
405 				ndp->ni_lcf |= NI_LCF_STRICTRELATIVE;
406 				ndp->ni_resflags |= NIRES_STRICTREL;
407 			}
408 #endif
409 		}
410 		if (error == 0 && (*dpp)->v_type != VDIR)
411 			error = ENOTDIR;
412 	}
413 	if (error == 0 && (cnp->cn_flags & BENEATH) != 0) {
414 		if (ndp->ni_dirfd == AT_FDCWD) {
415 			ndp->ni_beneath_latch = pwd->pwd_cdir;
416 			vrefact(ndp->ni_beneath_latch);
417 		} else {
418 			rights = *ndp->ni_rightsneeded;
419 			cap_rights_set_one(&rights, CAP_LOOKUP);
420 			error = fgetvp_rights(td, ndp->ni_dirfd, &rights,
421 			    &dirfd_caps, &ndp->ni_beneath_latch);
422 			if (error == 0 && (*dpp)->v_type != VDIR) {
423 				vrele(ndp->ni_beneath_latch);
424 				error = ENOTDIR;
425 			}
426 		}
427 		if (error == 0)
428 			ndp->ni_lcf |= NI_LCF_LATCH;
429 	}
430 	if (error == 0 && (cnp->cn_flags & RBENEATH) != 0) {
431 		if (cnp->cn_pnbuf[0] == '/' ||
432 		    (ndp->ni_lcf & NI_LCF_BENEATH_ABS) != 0) {
433 			error = EINVAL;
434 		} else if ((ndp->ni_lcf & NI_LCF_STRICTRELATIVE) == 0) {
435 			ndp->ni_lcf |= NI_LCF_STRICTRELATIVE |
436 			    NI_LCF_CAP_DOTDOT;
437 		}
438 	}
439 
440 	/*
441 	 * If we are auditing the kernel pathname, save the user pathname.
442 	 */
443 	if (cnp->cn_flags & AUDITVNODE1)
444 		AUDIT_ARG_UPATH1_VP(td, ndp->ni_rootdir, *dpp, cnp->cn_pnbuf);
445 	if (cnp->cn_flags & AUDITVNODE2)
446 		AUDIT_ARG_UPATH2_VP(td, ndp->ni_rootdir, *dpp, cnp->cn_pnbuf);
447 	if (ndp->ni_startdir != NULL && !startdir_used)
448 		vrele(ndp->ni_startdir);
449 	if (error != 0) {
450 		if (*dpp != NULL)
451 			vrele(*dpp);
452 		pwd_drop(pwd);
453 		return (error);
454 	}
455 	MPASS((ndp->ni_lcf & (NI_LCF_BENEATH_ABS | NI_LCF_LATCH)) !=
456 	    NI_LCF_BENEATH_ABS);
457 	if (((ndp->ni_lcf & NI_LCF_STRICTRELATIVE) != 0 &&
458 	    lookup_cap_dotdot != 0) ||
459 	    ((ndp->ni_lcf & NI_LCF_STRICTRELATIVE) == 0 &&
460 	    (cnp->cn_flags & BENEATH) != 0))
461 		ndp->ni_lcf |= NI_LCF_CAP_DOTDOT;
462 	SDT_PROBE4(vfs, namei, lookup, entry, *dpp, cnp->cn_pnbuf,
463 	    cnp->cn_flags, false);
464 	*pwdp = pwd;
465 	return (0);
466 }
467 
468 static int
469 namei_getpath(struct nameidata *ndp)
470 {
471 	struct componentname *cnp;
472 	int error;
473 
474 	cnp = &ndp->ni_cnd;
475 
476 	/*
477 	 * Get a buffer for the name to be translated, and copy the
478 	 * name into the buffer.
479 	 */
480 	cnp->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK);
481 	if (ndp->ni_segflg == UIO_SYSSPACE) {
482 		error = copystr(ndp->ni_dirp, cnp->cn_pnbuf, MAXPATHLEN,
483 		    &ndp->ni_pathlen);
484 	} else {
485 		error = copyinstr(ndp->ni_dirp, cnp->cn_pnbuf, MAXPATHLEN,
486 		    &ndp->ni_pathlen);
487 	}
488 
489 	if (__predict_false(error != 0)) {
490 		namei_cleanup_cnp(cnp);
491 		return (error);
492 	}
493 
494 	/*
495 	 * Don't allow empty pathnames.
496 	 */
497 	if (__predict_false(*cnp->cn_pnbuf == '\0')) {
498 		namei_cleanup_cnp(cnp);
499 		return (ENOENT);
500 	}
501 
502 	cnp->cn_nameptr = cnp->cn_pnbuf;
503 	return (0);
504 }
505 
506 /*
507  * Convert a pathname into a pointer to a locked vnode.
508  *
509  * The FOLLOW flag is set when symbolic links are to be followed
510  * when they occur at the end of the name translation process.
511  * Symbolic links are always followed for all other pathname
512  * components other than the last.
513  *
514  * The segflg defines whether the name is to be copied from user
515  * space or kernel space.
516  *
517  * Overall outline of namei:
518  *
519  *	copy in name
520  *	get starting directory
521  *	while (!done && !error) {
522  *		call lookup to search path.
523  *		if symbolic link, massage name in buffer and continue
524  *	}
525  */
526 int
527 namei(struct nameidata *ndp)
528 {
529 	char *cp;		/* pointer into pathname argument */
530 	struct vnode *dp;	/* the directory we are searching */
531 	struct iovec aiov;		/* uio for reading symbolic links */
532 	struct componentname *cnp;
533 	struct thread *td;
534 	struct pwd *pwd;
535 	struct uio auio;
536 	int error, linklen;
537 	enum cache_fpl_status status;
538 
539 	cnp = &ndp->ni_cnd;
540 	td = cnp->cn_thread;
541 #ifdef INVARIANTS
542 	KASSERT((ndp->ni_debugflags & NAMEI_DBG_CALLED) == 0,
543 	    ("%s: repeated call to namei without NDREINIT", __func__));
544 	KASSERT(ndp->ni_debugflags == NAMEI_DBG_INITED,
545 	    ("%s: bad debugflags %d", __func__, ndp->ni_debugflags));
546 	ndp->ni_debugflags |= NAMEI_DBG_CALLED;
547 	if (ndp->ni_startdir != NULL)
548 		ndp->ni_debugflags |= NAMEI_DBG_HADSTARTDIR;
549 	if (cnp->cn_flags & FAILIFEXISTS) {
550 		KASSERT(cnp->cn_nameiop == CREATE,
551 		    ("%s: FAILIFEXISTS passed for op %d", __func__, cnp->cn_nameiop));
552 		/*
553 		 * The limitation below is to restrict hairy corner cases.
554 		 */
555 		KASSERT((cnp->cn_flags & (LOCKPARENT | LOCKLEAF)) == LOCKPARENT,
556 		    ("%s: FAILIFEXISTS must be passed with LOCKPARENT and without LOCKLEAF",
557 		    __func__));
558 	}
559 	/*
560 	 * For NDVALIDATE.
561 	 *
562 	 * While NDINIT may seem like a more natural place to do it, there are
563 	 * callers which directly modify flags past invoking init.
564 	 */
565 	cnp->cn_origflags = cnp->cn_flags;
566 #endif
567 	ndp->ni_cnd.cn_cred = ndp->ni_cnd.cn_thread->td_ucred;
568 	KASSERT(ndp->ni_resflags == 0, ("%s: garbage in ni_resflags: %x\n",
569 	    __func__, ndp->ni_resflags));
570 	KASSERT(cnp->cn_cred && td->td_proc, ("namei: bad cred/proc"));
571 	KASSERT((cnp->cn_flags & NAMEI_INTERNAL_FLAGS) == 0,
572 	    ("namei: unexpected flags: %" PRIx64 "\n",
573 	    cnp->cn_flags & NAMEI_INTERNAL_FLAGS));
574 	if (cnp->cn_flags & NOCACHE)
575 		KASSERT(cnp->cn_nameiop != LOOKUP,
576 		    ("%s: NOCACHE passed with LOOKUP", __func__));
577 	MPASS(ndp->ni_startdir == NULL || ndp->ni_startdir->v_type == VDIR ||
578 	    ndp->ni_startdir->v_type == VBAD);
579 
580 	ndp->ni_lcf = 0;
581 	ndp->ni_vp = NULL;
582 
583 	error = namei_getpath(ndp);
584 	if (__predict_false(error != 0)) {
585 		return (error);
586 	}
587 
588 #ifdef KTRACE
589 	if (KTRPOINT(td, KTR_NAMEI)) {
590 		KASSERT(cnp->cn_thread == curthread,
591 		    ("namei not using curthread"));
592 		ktrnamei(cnp->cn_pnbuf);
593 	}
594 #endif
595 
596 	/*
597 	 * First try looking up the target without locking any vnodes.
598 	 *
599 	 * We may need to start from scratch or pick up where it left off.
600 	 */
601 	error = cache_fplookup(ndp, &status, &pwd);
602 	switch (status) {
603 	case CACHE_FPL_STATUS_UNSET:
604 		__assert_unreachable();
605 		break;
606 	case CACHE_FPL_STATUS_HANDLED:
607 		if (error == 0)
608 			NDVALIDATE(ndp);
609 		return (error);
610 	case CACHE_FPL_STATUS_PARTIAL:
611 		TAILQ_INIT(&ndp->ni_cap_tracker);
612 		dp = ndp->ni_startdir;
613 		break;
614 	case CACHE_FPL_STATUS_ABORTED:
615 		TAILQ_INIT(&ndp->ni_cap_tracker);
616 		error = namei_setup(ndp, &dp, &pwd);
617 		if (error != 0) {
618 			namei_cleanup_cnp(cnp);
619 			return (error);
620 		}
621 		break;
622 	}
623 
624 	ndp->ni_loopcnt = 0;
625 
626 	/*
627 	 * Locked lookup.
628 	 */
629 	for (;;) {
630 		ndp->ni_startdir = dp;
631 		error = lookup(ndp);
632 		if (error != 0) {
633 			/*
634 			 * Override an error to not allow user to use
635 			 * BENEATH as an oracle.
636 			 */
637 			if ((ndp->ni_lcf & (NI_LCF_LATCH |
638 			    NI_LCF_BENEATH_LATCHED)) == NI_LCF_LATCH)
639 				error = ENOTCAPABLE;
640 			goto out;
641 		}
642 
643 		/*
644 		 * If not a symbolic link, we're done.
645 		 */
646 		if ((cnp->cn_flags & ISSYMLINK) == 0) {
647 			SDT_PROBE4(vfs, namei, lookup, return, error,
648 			    (error == 0 ? ndp->ni_vp : NULL), false, ndp);
649 			if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0) {
650 				namei_cleanup_cnp(cnp);
651 			} else
652 				cnp->cn_flags |= HASBUF;
653 			if ((ndp->ni_lcf & (NI_LCF_LATCH |
654 			    NI_LCF_BENEATH_LATCHED)) == NI_LCF_LATCH) {
655 				NDFREE(ndp, 0);
656 				error = ENOTCAPABLE;
657 			}
658 			nameicap_cleanup(ndp, true);
659 			pwd_drop(pwd);
660 			if (error == 0)
661 				NDVALIDATE(ndp);
662 			return (error);
663 		}
664 		if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
665 			error = ELOOP;
666 			break;
667 		}
668 #ifdef MAC
669 		if ((cnp->cn_flags & NOMACCHECK) == 0) {
670 			error = mac_vnode_check_readlink(td->td_ucred,
671 			    ndp->ni_vp);
672 			if (error != 0)
673 				break;
674 		}
675 #endif
676 		if (ndp->ni_pathlen > 1)
677 			cp = uma_zalloc(namei_zone, M_WAITOK);
678 		else
679 			cp = cnp->cn_pnbuf;
680 		aiov.iov_base = cp;
681 		aiov.iov_len = MAXPATHLEN;
682 		auio.uio_iov = &aiov;
683 		auio.uio_iovcnt = 1;
684 		auio.uio_offset = 0;
685 		auio.uio_rw = UIO_READ;
686 		auio.uio_segflg = UIO_SYSSPACE;
687 		auio.uio_td = td;
688 		auio.uio_resid = MAXPATHLEN;
689 		error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
690 		if (error != 0) {
691 			if (ndp->ni_pathlen > 1)
692 				uma_zfree(namei_zone, cp);
693 			break;
694 		}
695 		linklen = MAXPATHLEN - auio.uio_resid;
696 		if (linklen == 0) {
697 			if (ndp->ni_pathlen > 1)
698 				uma_zfree(namei_zone, cp);
699 			error = ENOENT;
700 			break;
701 		}
702 		if (linklen + ndp->ni_pathlen > MAXPATHLEN) {
703 			if (ndp->ni_pathlen > 1)
704 				uma_zfree(namei_zone, cp);
705 			error = ENAMETOOLONG;
706 			break;
707 		}
708 		if (ndp->ni_pathlen > 1) {
709 			bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
710 			uma_zfree(namei_zone, cnp->cn_pnbuf);
711 			cnp->cn_pnbuf = cp;
712 		} else
713 			cnp->cn_pnbuf[linklen] = '\0';
714 		ndp->ni_pathlen += linklen;
715 		vput(ndp->ni_vp);
716 		dp = ndp->ni_dvp;
717 		/*
718 		 * Check if root directory should replace current directory.
719 		 */
720 		cnp->cn_nameptr = cnp->cn_pnbuf;
721 		if (*(cnp->cn_nameptr) == '/') {
722 			vrele(dp);
723 			error = namei_handle_root(ndp, &dp);
724 			if (error != 0)
725 				goto out;
726 		}
727 	}
728 	vput(ndp->ni_vp);
729 	ndp->ni_vp = NULL;
730 	vrele(ndp->ni_dvp);
731 out:
732 	MPASS(error != 0);
733 	SDT_PROBE4(vfs, namei, lookup, return, error, NULL, false, ndp);
734 	namei_cleanup_cnp(cnp);
735 	nameicap_cleanup(ndp, true);
736 	pwd_drop(pwd);
737 	return (error);
738 }
739 
740 static int
741 compute_cn_lkflags(struct mount *mp, int lkflags, int cnflags)
742 {
743 
744 	if (mp == NULL || ((lkflags & LK_SHARED) &&
745 	    (!(mp->mnt_kern_flag & MNTK_LOOKUP_SHARED) ||
746 	    ((cnflags & ISDOTDOT) &&
747 	    (mp->mnt_kern_flag & MNTK_LOOKUP_EXCL_DOTDOT))))) {
748 		lkflags &= ~LK_SHARED;
749 		lkflags |= LK_EXCLUSIVE;
750 	}
751 	lkflags |= LK_NODDLKTREAT;
752 	return (lkflags);
753 }
754 
755 static __inline int
756 needs_exclusive_leaf(struct mount *mp, int flags)
757 {
758 
759 	/*
760 	 * Intermediate nodes can use shared locks, we only need to
761 	 * force an exclusive lock for leaf nodes.
762 	 */
763 	if ((flags & (ISLASTCN | LOCKLEAF)) != (ISLASTCN | LOCKLEAF))
764 		return (0);
765 
766 	/* Always use exclusive locks if LOCKSHARED isn't set. */
767 	if (!(flags & LOCKSHARED))
768 		return (1);
769 
770 	/*
771 	 * For lookups during open(), if the mount point supports
772 	 * extended shared operations, then use a shared lock for the
773 	 * leaf node, otherwise use an exclusive lock.
774 	 */
775 	if ((flags & ISOPEN) != 0)
776 		return (!MNT_EXTENDED_SHARED(mp));
777 
778 	/*
779 	 * Lookup requests outside of open() that specify LOCKSHARED
780 	 * only need a shared lock on the leaf vnode.
781 	 */
782 	return (0);
783 }
784 
785 /*
786  * Search a pathname.
787  * This is a very central and rather complicated routine.
788  *
789  * The pathname is pointed to by ni_ptr and is of length ni_pathlen.
790  * The starting directory is taken from ni_startdir. The pathname is
791  * descended until done, or a symbolic link is encountered. The variable
792  * ni_more is clear if the path is completed; it is set to one if a
793  * symbolic link needing interpretation is encountered.
794  *
795  * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
796  * whether the name is to be looked up, created, renamed, or deleted.
797  * When CREATE, RENAME, or DELETE is specified, information usable in
798  * creating, renaming, or deleting a directory entry may be calculated.
799  * If flag has LOCKPARENT or'ed into it, the parent directory is returned
800  * locked. If flag has WANTPARENT or'ed into it, the parent directory is
801  * returned unlocked. Otherwise the parent directory is not returned. If
802  * the target of the pathname exists and LOCKLEAF is or'ed into the flag
803  * the target is returned locked, otherwise it is returned unlocked.
804  * When creating or renaming and LOCKPARENT is specified, the target may not
805  * be ".".  When deleting and LOCKPARENT is specified, the target may be ".".
806  *
807  * Overall outline of lookup:
808  *
809  * dirloop:
810  *	identify next component of name at ndp->ni_ptr
811  *	handle degenerate case where name is null string
812  *	if .. and crossing mount points and on mounted filesys, find parent
813  *	call VOP_LOOKUP routine for next component name
814  *	    directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set
815  *	    component vnode returned in ni_vp (if it exists), locked.
816  *	if result vnode is mounted on and crossing mount points,
817  *	    find mounted on vnode
818  *	if more components of name, do next level at dirloop
819  *	return the answer in ni_vp, locked if LOCKLEAF set
820  *	    if LOCKPARENT set, return locked parent in ni_dvp
821  *	    if WANTPARENT set, return unlocked parent in ni_dvp
822  */
823 int
824 lookup(struct nameidata *ndp)
825 {
826 	char *cp;			/* pointer into pathname argument */
827 	char *prev_ni_next;		/* saved ndp->ni_next */
828 	struct vnode *dp = NULL;	/* the directory we are searching */
829 	struct vnode *tdp;		/* saved dp */
830 	struct mount *mp;		/* mount table entry */
831 	struct prison *pr;
832 	size_t prev_ni_pathlen;		/* saved ndp->ni_pathlen */
833 	int docache;			/* == 0 do not cache last component */
834 	int wantparent;			/* 1 => wantparent or lockparent flag */
835 	int rdonly;			/* lookup read-only flag bit */
836 	int error = 0;
837 	int dpunlocked = 0;		/* dp has already been unlocked */
838 	int relookup = 0;		/* do not consume the path component */
839 	struct componentname *cnp = &ndp->ni_cnd;
840 	int lkflags_save;
841 	int ni_dvp_unlocked;
842 
843 	/*
844 	 * Setup: break out flag bits into variables.
845 	 */
846 	ni_dvp_unlocked = 0;
847 	wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT);
848 	KASSERT(cnp->cn_nameiop == LOOKUP || wantparent,
849 	    ("CREATE, DELETE, RENAME require LOCKPARENT or WANTPARENT."));
850 	/*
851 	 * When set to zero, docache causes the last component of the
852 	 * pathname to be deleted from the cache and the full lookup
853 	 * of the name to be done (via VOP_CACHEDLOOKUP()). Often
854 	 * filesystems need some pre-computed values that are made
855 	 * during the full lookup, for instance UFS sets dp->i_offset.
856 	 *
857 	 * The docache variable is set to zero when requested by the
858 	 * NOCACHE flag and for all modifying operations except CREATE.
859 	 */
860 	docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE;
861 	if (cnp->cn_nameiop == DELETE ||
862 	    (wantparent && cnp->cn_nameiop != CREATE &&
863 	     cnp->cn_nameiop != LOOKUP))
864 		docache = 0;
865 	rdonly = cnp->cn_flags & RDONLY;
866 	cnp->cn_flags &= ~ISSYMLINK;
867 	ndp->ni_dvp = NULL;
868 	/*
869 	 * We use shared locks until we hit the parent of the last cn then
870 	 * we adjust based on the requesting flags.
871 	 */
872 	cnp->cn_lkflags = LK_SHARED;
873 	dp = ndp->ni_startdir;
874 	ndp->ni_startdir = NULLVP;
875 	vn_lock(dp,
876 	    compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags | LK_RETRY,
877 	    cnp->cn_flags));
878 
879 dirloop:
880 	/*
881 	 * Search a new directory.
882 	 *
883 	 * The last component of the filename is left accessible via
884 	 * cnp->cn_nameptr for callers that need the name. Callers needing
885 	 * the name set the SAVENAME flag. When done, they assume
886 	 * responsibility for freeing the pathname buffer.
887 	 */
888 	for (cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++)
889 		continue;
890 	cnp->cn_namelen = cp - cnp->cn_nameptr;
891 	if (cnp->cn_namelen > NAME_MAX) {
892 		error = ENAMETOOLONG;
893 		goto bad;
894 	}
895 #ifdef NAMEI_DIAGNOSTIC
896 	{ char c = *cp;
897 	*cp = '\0';
898 	printf("{%s}: ", cnp->cn_nameptr);
899 	*cp = c; }
900 #endif
901 	prev_ni_pathlen = ndp->ni_pathlen;
902 	ndp->ni_pathlen -= cnp->cn_namelen;
903 	KASSERT(ndp->ni_pathlen <= PATH_MAX,
904 	    ("%s: ni_pathlen underflow to %zd\n", __func__, ndp->ni_pathlen));
905 	prev_ni_next = ndp->ni_next;
906 	ndp->ni_next = cp;
907 
908 	/*
909 	 * Replace multiple slashes by a single slash and trailing slashes
910 	 * by a null.  This must be done before VOP_LOOKUP() because some
911 	 * fs's don't know about trailing slashes.  Remember if there were
912 	 * trailing slashes to handle symlinks, existing non-directories
913 	 * and non-existing files that won't be directories specially later.
914 	 */
915 	while (*cp == '/' && (cp[1] == '/' || cp[1] == '\0')) {
916 		cp++;
917 		ndp->ni_pathlen--;
918 		if (*cp == '\0') {
919 			*ndp->ni_next = '\0';
920 			cnp->cn_flags |= TRAILINGSLASH;
921 		}
922 	}
923 	ndp->ni_next = cp;
924 
925 	cnp->cn_flags |= MAKEENTRY;
926 	if (*cp == '\0' && docache == 0)
927 		cnp->cn_flags &= ~MAKEENTRY;
928 	if (cnp->cn_namelen == 2 &&
929 	    cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.')
930 		cnp->cn_flags |= ISDOTDOT;
931 	else
932 		cnp->cn_flags &= ~ISDOTDOT;
933 	if (*ndp->ni_next == 0)
934 		cnp->cn_flags |= ISLASTCN;
935 	else
936 		cnp->cn_flags &= ~ISLASTCN;
937 
938 	if ((cnp->cn_flags & ISLASTCN) != 0 &&
939 	    cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.' &&
940 	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
941 		error = EINVAL;
942 		goto bad;
943 	}
944 
945 	nameicap_tracker_add(ndp, dp);
946 
947 	/*
948 	 * Check for degenerate name (e.g. / or "")
949 	 * which is a way of talking about a directory,
950 	 * e.g. like "/." or ".".
951 	 */
952 	if (cnp->cn_nameptr[0] == '\0') {
953 		if (dp->v_type != VDIR) {
954 			error = ENOTDIR;
955 			goto bad;
956 		}
957 		if (cnp->cn_nameiop != LOOKUP) {
958 			error = EISDIR;
959 			goto bad;
960 		}
961 		if (wantparent) {
962 			ndp->ni_dvp = dp;
963 			VREF(dp);
964 		}
965 		ndp->ni_vp = dp;
966 
967 		if (cnp->cn_flags & AUDITVNODE1)
968 			AUDIT_ARG_VNODE1(dp);
969 		else if (cnp->cn_flags & AUDITVNODE2)
970 			AUDIT_ARG_VNODE2(dp);
971 
972 		if (!(cnp->cn_flags & (LOCKPARENT | LOCKLEAF)))
973 			VOP_UNLOCK(dp);
974 		/* XXX This should probably move to the top of function. */
975 		if (cnp->cn_flags & SAVESTART)
976 			panic("lookup: SAVESTART");
977 		goto success;
978 	}
979 
980 	/*
981 	 * Handle "..": five special cases.
982 	 * 0. If doing a capability lookup and lookup_cap_dotdot is
983 	 *    disabled, return ENOTCAPABLE.
984 	 * 1. Return an error if this is the last component of
985 	 *    the name and the operation is DELETE or RENAME.
986 	 * 2. If at root directory (e.g. after chroot)
987 	 *    or at absolute root directory
988 	 *    then ignore it so can't get out.
989 	 * 3. If this vnode is the root of a mounted
990 	 *    filesystem, then replace it with the
991 	 *    vnode which was mounted on so we take the
992 	 *    .. in the other filesystem.
993 	 * 4. If the vnode is the top directory of
994 	 *    the jail or chroot, don't let them out.
995 	 * 5. If doing a capability lookup and lookup_cap_dotdot is
996 	 *    enabled, return ENOTCAPABLE if the lookup would escape
997 	 *    from the initial file descriptor directory.  Checks are
998 	 *    done by ensuring that namei() already traversed the
999 	 *    result of dotdot lookup.
1000 	 */
1001 	if (cnp->cn_flags & ISDOTDOT) {
1002 		if ((ndp->ni_lcf & (NI_LCF_STRICTRELATIVE | NI_LCF_CAP_DOTDOT))
1003 		    == NI_LCF_STRICTRELATIVE) {
1004 #ifdef KTRACE
1005 			if (KTRPOINT(curthread, KTR_CAPFAIL))
1006 				ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL);
1007 #endif
1008 			error = ENOTCAPABLE;
1009 			goto bad;
1010 		}
1011 		if ((cnp->cn_flags & ISLASTCN) != 0 &&
1012 		    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
1013 			error = EINVAL;
1014 			goto bad;
1015 		}
1016 		for (;;) {
1017 			for (pr = cnp->cn_cred->cr_prison; pr != NULL;
1018 			     pr = pr->pr_parent)
1019 				if (dp == pr->pr_root)
1020 					break;
1021 			if (dp == ndp->ni_rootdir ||
1022 			    dp == ndp->ni_topdir ||
1023 			    dp == rootvnode ||
1024 			    pr != NULL ||
1025 			    ((dp->v_vflag & VV_ROOT) != 0 &&
1026 			     (cnp->cn_flags & NOCROSSMOUNT) != 0)) {
1027 				ndp->ni_dvp = dp;
1028 				ndp->ni_vp = dp;
1029 				VREF(dp);
1030 				goto nextname;
1031 			}
1032 			if ((dp->v_vflag & VV_ROOT) == 0)
1033 				break;
1034 			if (VN_IS_DOOMED(dp)) {	/* forced unmount */
1035 				error = ENOENT;
1036 				goto bad;
1037 			}
1038 			tdp = dp;
1039 			dp = dp->v_mount->mnt_vnodecovered;
1040 			VREF(dp);
1041 			vput(tdp);
1042 			vn_lock(dp,
1043 			    compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags |
1044 			    LK_RETRY, ISDOTDOT));
1045 			error = nameicap_check_dotdot(ndp, dp);
1046 			if (error != 0) {
1047 #ifdef KTRACE
1048 				if (KTRPOINT(curthread, KTR_CAPFAIL))
1049 					ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL);
1050 #endif
1051 				goto bad;
1052 			}
1053 		}
1054 	}
1055 
1056 	/*
1057 	 * We now have a segment name to search for, and a directory to search.
1058 	 */
1059 unionlookup:
1060 #ifdef MAC
1061 	error = mac_vnode_check_lookup(cnp->cn_thread->td_ucred, dp, cnp);
1062 	if (error)
1063 		goto bad;
1064 #endif
1065 	ndp->ni_dvp = dp;
1066 	ndp->ni_vp = NULL;
1067 	ASSERT_VOP_LOCKED(dp, "lookup");
1068 	/*
1069 	 * If we have a shared lock we may need to upgrade the lock for the
1070 	 * last operation.
1071 	 */
1072 	if ((cnp->cn_flags & LOCKPARENT) && (cnp->cn_flags & ISLASTCN) &&
1073 	    dp != vp_crossmp && VOP_ISLOCKED(dp) == LK_SHARED)
1074 		vn_lock(dp, LK_UPGRADE|LK_RETRY);
1075 	if (VN_IS_DOOMED(dp)) {
1076 		error = ENOENT;
1077 		goto bad;
1078 	}
1079 	/*
1080 	 * If we're looking up the last component and we need an exclusive
1081 	 * lock, adjust our lkflags.
1082 	 */
1083 	if (needs_exclusive_leaf(dp->v_mount, cnp->cn_flags))
1084 		cnp->cn_lkflags = LK_EXCLUSIVE;
1085 #ifdef NAMEI_DIAGNOSTIC
1086 	vn_printf(dp, "lookup in ");
1087 #endif
1088 	lkflags_save = cnp->cn_lkflags;
1089 	cnp->cn_lkflags = compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags,
1090 	    cnp->cn_flags);
1091 	error = VOP_LOOKUP(dp, &ndp->ni_vp, cnp);
1092 	cnp->cn_lkflags = lkflags_save;
1093 	if (error != 0) {
1094 		KASSERT(ndp->ni_vp == NULL, ("leaf should be empty"));
1095 #ifdef NAMEI_DIAGNOSTIC
1096 		printf("not found\n");
1097 #endif
1098 		if ((error == ENOENT) &&
1099 		    (dp->v_vflag & VV_ROOT) && (dp->v_mount != NULL) &&
1100 		    (dp->v_mount->mnt_flag & MNT_UNION)) {
1101 			tdp = dp;
1102 			dp = dp->v_mount->mnt_vnodecovered;
1103 			VREF(dp);
1104 			vput(tdp);
1105 			vn_lock(dp,
1106 			    compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags |
1107 			    LK_RETRY, cnp->cn_flags));
1108 			nameicap_tracker_add(ndp, dp);
1109 			goto unionlookup;
1110 		}
1111 
1112 		if (error == ERELOOKUP) {
1113 			vref(dp);
1114 			ndp->ni_vp = dp;
1115 			error = 0;
1116 			relookup = 1;
1117 			goto good;
1118 		}
1119 
1120 		if (error != EJUSTRETURN)
1121 			goto bad;
1122 		/*
1123 		 * At this point, we know we're at the end of the
1124 		 * pathname.  If creating / renaming, we can consider
1125 		 * allowing the file or directory to be created / renamed,
1126 		 * provided we're not on a read-only filesystem.
1127 		 */
1128 		if (rdonly) {
1129 			error = EROFS;
1130 			goto bad;
1131 		}
1132 		/* trailing slash only allowed for directories */
1133 		if ((cnp->cn_flags & TRAILINGSLASH) &&
1134 		    !(cnp->cn_flags & WILLBEDIR)) {
1135 			error = ENOENT;
1136 			goto bad;
1137 		}
1138 		if ((cnp->cn_flags & LOCKPARENT) == 0)
1139 			VOP_UNLOCK(dp);
1140 		/*
1141 		 * We return with ni_vp NULL to indicate that the entry
1142 		 * doesn't currently exist, leaving a pointer to the
1143 		 * (possibly locked) directory vnode in ndp->ni_dvp.
1144 		 */
1145 		if (cnp->cn_flags & SAVESTART) {
1146 			ndp->ni_startdir = ndp->ni_dvp;
1147 			VREF(ndp->ni_startdir);
1148 		}
1149 		goto success;
1150 	}
1151 
1152 good:
1153 #ifdef NAMEI_DIAGNOSTIC
1154 	printf("found\n");
1155 #endif
1156 	dp = ndp->ni_vp;
1157 
1158 	/*
1159 	 * Check to see if the vnode has been mounted on;
1160 	 * if so find the root of the mounted filesystem.
1161 	 */
1162 	while (dp->v_type == VDIR && (mp = dp->v_mountedhere) &&
1163 	       (cnp->cn_flags & NOCROSSMOUNT) == 0) {
1164 		if (vfs_busy(mp, 0))
1165 			continue;
1166 		vput(dp);
1167 		if (dp != ndp->ni_dvp)
1168 			vput(ndp->ni_dvp);
1169 		else
1170 			vrele(ndp->ni_dvp);
1171 		vrefact(vp_crossmp);
1172 		ndp->ni_dvp = vp_crossmp;
1173 		error = VFS_ROOT(mp, compute_cn_lkflags(mp, cnp->cn_lkflags,
1174 		    cnp->cn_flags), &tdp);
1175 		vfs_unbusy(mp);
1176 		if (vn_lock(vp_crossmp, LK_SHARED | LK_NOWAIT))
1177 			panic("vp_crossmp exclusively locked or reclaimed");
1178 		if (error) {
1179 			dpunlocked = 1;
1180 			goto bad2;
1181 		}
1182 		ndp->ni_vp = dp = tdp;
1183 	}
1184 
1185 	/*
1186 	 * Check for symbolic link
1187 	 */
1188 	if ((dp->v_type == VLNK) &&
1189 	    ((cnp->cn_flags & FOLLOW) || (cnp->cn_flags & TRAILINGSLASH) ||
1190 	     *ndp->ni_next == '/')) {
1191 		cnp->cn_flags |= ISSYMLINK;
1192 		if (VN_IS_DOOMED(dp)) {
1193 			/*
1194 			 * We can't know whether the directory was mounted with
1195 			 * NOSYMFOLLOW, so we can't follow safely.
1196 			 */
1197 			error = ENOENT;
1198 			goto bad2;
1199 		}
1200 		if (dp->v_mount->mnt_flag & MNT_NOSYMFOLLOW) {
1201 			error = EACCES;
1202 			goto bad2;
1203 		}
1204 		/*
1205 		 * Symlink code always expects an unlocked dvp.
1206 		 */
1207 		if (ndp->ni_dvp != ndp->ni_vp) {
1208 			VOP_UNLOCK(ndp->ni_dvp);
1209 			ni_dvp_unlocked = 1;
1210 		}
1211 		goto success;
1212 	}
1213 
1214 nextname:
1215 	/*
1216 	 * Not a symbolic link that we will follow.  Continue with the
1217 	 * next component if there is any; otherwise, we're done.
1218 	 */
1219 	KASSERT((cnp->cn_flags & ISLASTCN) || *ndp->ni_next == '/',
1220 	    ("lookup: invalid path state."));
1221 	if (relookup) {
1222 		relookup = 0;
1223 		ndp->ni_pathlen = prev_ni_pathlen;
1224 		ndp->ni_next = prev_ni_next;
1225 		if (ndp->ni_dvp != dp)
1226 			vput(ndp->ni_dvp);
1227 		else
1228 			vrele(ndp->ni_dvp);
1229 		goto dirloop;
1230 	}
1231 	if (cnp->cn_flags & ISDOTDOT) {
1232 		error = nameicap_check_dotdot(ndp, ndp->ni_vp);
1233 		if (error != 0) {
1234 #ifdef KTRACE
1235 			if (KTRPOINT(curthread, KTR_CAPFAIL))
1236 				ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL);
1237 #endif
1238 			goto bad2;
1239 		}
1240 	}
1241 	if (*ndp->ni_next == '/') {
1242 		cnp->cn_nameptr = ndp->ni_next;
1243 		while (*cnp->cn_nameptr == '/') {
1244 			cnp->cn_nameptr++;
1245 			ndp->ni_pathlen--;
1246 		}
1247 		if (ndp->ni_dvp != dp)
1248 			vput(ndp->ni_dvp);
1249 		else
1250 			vrele(ndp->ni_dvp);
1251 		goto dirloop;
1252 	}
1253 	/*
1254 	 * If we're processing a path with a trailing slash,
1255 	 * check that the end result is a directory.
1256 	 */
1257 	if ((cnp->cn_flags & TRAILINGSLASH) && dp->v_type != VDIR) {
1258 		error = ENOTDIR;
1259 		goto bad2;
1260 	}
1261 	/*
1262 	 * Disallow directory write attempts on read-only filesystems.
1263 	 */
1264 	if (rdonly &&
1265 	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
1266 		error = EROFS;
1267 		goto bad2;
1268 	}
1269 	if (cnp->cn_flags & SAVESTART) {
1270 		ndp->ni_startdir = ndp->ni_dvp;
1271 		VREF(ndp->ni_startdir);
1272 	}
1273 	if (!wantparent) {
1274 		ni_dvp_unlocked = 2;
1275 		if (ndp->ni_dvp != dp)
1276 			vput(ndp->ni_dvp);
1277 		else
1278 			vrele(ndp->ni_dvp);
1279 	} else if ((cnp->cn_flags & LOCKPARENT) == 0 && ndp->ni_dvp != dp) {
1280 		VOP_UNLOCK(ndp->ni_dvp);
1281 		ni_dvp_unlocked = 1;
1282 	}
1283 
1284 	if (cnp->cn_flags & AUDITVNODE1)
1285 		AUDIT_ARG_VNODE1(dp);
1286 	else if (cnp->cn_flags & AUDITVNODE2)
1287 		AUDIT_ARG_VNODE2(dp);
1288 
1289 	if ((cnp->cn_flags & LOCKLEAF) == 0)
1290 		VOP_UNLOCK(dp);
1291 success:
1292 	/*
1293 	 * FIXME: for lookups which only cross a mount point to fetch the
1294 	 * root vnode, ni_dvp will be set to vp_crossmp. This can be a problem
1295 	 * if either WANTPARENT or LOCKPARENT is set.
1296 	 */
1297 	/*
1298 	 * Because of shared lookup we may have the vnode shared locked, but
1299 	 * the caller may want it to be exclusively locked.
1300 	 */
1301 	if (needs_exclusive_leaf(dp->v_mount, cnp->cn_flags) &&
1302 	    VOP_ISLOCKED(dp) != LK_EXCLUSIVE) {
1303 		vn_lock(dp, LK_UPGRADE | LK_RETRY);
1304 		if (VN_IS_DOOMED(dp)) {
1305 			error = ENOENT;
1306 			goto bad2;
1307 		}
1308 	}
1309 	if (ndp->ni_vp != NULL) {
1310 		nameicap_tracker_add(ndp, ndp->ni_vp);
1311 		if ((cnp->cn_flags & (FAILIFEXISTS | ISSYMLINK)) == FAILIFEXISTS)
1312 			goto bad_eexist;
1313 	}
1314 	return (0);
1315 
1316 bad2:
1317 	if (ni_dvp_unlocked != 2) {
1318 		if (dp != ndp->ni_dvp && !ni_dvp_unlocked)
1319 			vput(ndp->ni_dvp);
1320 		else
1321 			vrele(ndp->ni_dvp);
1322 	}
1323 bad:
1324 	if (!dpunlocked)
1325 		vput(dp);
1326 	ndp->ni_vp = NULL;
1327 	return (error);
1328 bad_eexist:
1329 	/*
1330 	 * FAILIFEXISTS handling.
1331 	 *
1332 	 * XXX namei called with LOCKPARENT but not LOCKLEAF has the strange
1333 	 * behaviour of leaving the vnode unlocked if the target is the same
1334 	 * vnode as the parent.
1335 	 */
1336 	MPASS((cnp->cn_flags & ISSYMLINK) == 0);
1337 	if (ndp->ni_vp == ndp->ni_dvp)
1338 		vrele(ndp->ni_dvp);
1339 	else
1340 		vput(ndp->ni_dvp);
1341 	vrele(ndp->ni_vp);
1342 	ndp->ni_dvp = NULL;
1343 	ndp->ni_vp = NULL;
1344 	NDFREE(ndp, NDF_ONLY_PNBUF);
1345 	return (EEXIST);
1346 }
1347 
1348 /*
1349  * relookup - lookup a path name component
1350  *    Used by lookup to re-acquire things.
1351  */
1352 int
1353 relookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp)
1354 {
1355 	struct vnode *dp = NULL;		/* the directory we are searching */
1356 	int rdonly;			/* lookup read-only flag bit */
1357 	int error = 0;
1358 
1359 	KASSERT(cnp->cn_flags & ISLASTCN,
1360 	    ("relookup: Not given last component."));
1361 	/*
1362 	 * Setup: break out flag bits into variables.
1363 	 */
1364 	KASSERT((cnp->cn_flags & (LOCKPARENT | WANTPARENT)) != 0,
1365 	    ("relookup: parent not wanted"));
1366 	rdonly = cnp->cn_flags & RDONLY;
1367 	cnp->cn_flags &= ~ISSYMLINK;
1368 	dp = dvp;
1369 	cnp->cn_lkflags = LK_EXCLUSIVE;
1370 	vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
1371 
1372 	/*
1373 	 * Search a new directory.
1374 	 *
1375 	 * The last component of the filename is left accessible via
1376 	 * cnp->cn_nameptr for callers that need the name. Callers needing
1377 	 * the name set the SAVENAME flag. When done, they assume
1378 	 * responsibility for freeing the pathname buffer.
1379 	 */
1380 #ifdef NAMEI_DIAGNOSTIC
1381 	printf("{%s}: ", cnp->cn_nameptr);
1382 #endif
1383 
1384 	/*
1385 	 * Check for "" which represents the root directory after slash
1386 	 * removal.
1387 	 */
1388 	if (cnp->cn_nameptr[0] == '\0') {
1389 		/*
1390 		 * Support only LOOKUP for "/" because lookup()
1391 		 * can't succeed for CREATE, DELETE and RENAME.
1392 		 */
1393 		KASSERT(cnp->cn_nameiop == LOOKUP, ("nameiop must be LOOKUP"));
1394 		KASSERT(dp->v_type == VDIR, ("dp is not a directory"));
1395 
1396 		if (!(cnp->cn_flags & LOCKLEAF))
1397 			VOP_UNLOCK(dp);
1398 		*vpp = dp;
1399 		/* XXX This should probably move to the top of function. */
1400 		if (cnp->cn_flags & SAVESTART)
1401 			panic("lookup: SAVESTART");
1402 		return (0);
1403 	}
1404 
1405 	if (cnp->cn_flags & ISDOTDOT)
1406 		panic ("relookup: lookup on dot-dot");
1407 
1408 	/*
1409 	 * We now have a segment name to search for, and a directory to search.
1410 	 */
1411 #ifdef NAMEI_DIAGNOSTIC
1412 	vn_printf(dp, "search in ");
1413 #endif
1414 	if ((error = VOP_LOOKUP(dp, vpp, cnp)) != 0) {
1415 		KASSERT(*vpp == NULL, ("leaf should be empty"));
1416 		if (error != EJUSTRETURN)
1417 			goto bad;
1418 		/*
1419 		 * If creating and at end of pathname, then can consider
1420 		 * allowing file to be created.
1421 		 */
1422 		if (rdonly) {
1423 			error = EROFS;
1424 			goto bad;
1425 		}
1426 		/* ASSERT(dvp == ndp->ni_startdir) */
1427 		if (cnp->cn_flags & SAVESTART)
1428 			VREF(dvp);
1429 		if ((cnp->cn_flags & LOCKPARENT) == 0)
1430 			VOP_UNLOCK(dp);
1431 		/*
1432 		 * We return with ni_vp NULL to indicate that the entry
1433 		 * doesn't currently exist, leaving a pointer to the
1434 		 * (possibly locked) directory vnode in ndp->ni_dvp.
1435 		 */
1436 		return (0);
1437 	}
1438 
1439 	dp = *vpp;
1440 
1441 	/*
1442 	 * Disallow directory write attempts on read-only filesystems.
1443 	 */
1444 	if (rdonly &&
1445 	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
1446 		if (dvp == dp)
1447 			vrele(dvp);
1448 		else
1449 			vput(dvp);
1450 		error = EROFS;
1451 		goto bad;
1452 	}
1453 	/*
1454 	 * Set the parent lock/ref state to the requested state.
1455 	 */
1456 	if ((cnp->cn_flags & LOCKPARENT) == 0 && dvp != dp)
1457 		VOP_UNLOCK(dvp);
1458 	/*
1459 	 * Check for symbolic link
1460 	 */
1461 	KASSERT(dp->v_type != VLNK || !(cnp->cn_flags & FOLLOW),
1462 	    ("relookup: symlink found.\n"));
1463 
1464 	/* ASSERT(dvp == ndp->ni_startdir) */
1465 	if (cnp->cn_flags & SAVESTART)
1466 		VREF(dvp);
1467 
1468 	if ((cnp->cn_flags & LOCKLEAF) == 0)
1469 		VOP_UNLOCK(dp);
1470 	return (0);
1471 bad:
1472 	vput(dp);
1473 	*vpp = NULL;
1474 	return (error);
1475 }
1476 
1477 /*
1478  * Free data allocated by namei(); see namei(9) for details.
1479  */
1480 void
1481 NDFREE_PNBUF(struct nameidata *ndp)
1482 {
1483 
1484 	if ((ndp->ni_cnd.cn_flags & HASBUF) != 0) {
1485 		MPASS((ndp->ni_cnd.cn_flags & (SAVENAME | SAVESTART)) != 0);
1486 		uma_zfree(namei_zone, ndp->ni_cnd.cn_pnbuf);
1487 		ndp->ni_cnd.cn_flags &= ~HASBUF;
1488 	}
1489 }
1490 
1491 /*
1492  * NDFREE_PNBUF replacement for callers that know there is no buffer.
1493  *
1494  * This is a hack. Preferably the VFS layer would not produce anything more
1495  * than it was asked to do. Unfortunately several non-LOOKUP cases can add the
1496  * HASBUF flag to the result. Even then an interface could be implemented where
1497  * the caller specifies what they expected to see in the result and what they
1498  * are going to take care of.
1499  *
1500  * In the meantime provide this kludge as a trivial replacement for NDFREE_PNBUF
1501  * calls scattered throughout the kernel where we know for a fact the flag must not
1502  * be seen.
1503  */
1504 #ifdef INVARIANTS
1505 void
1506 NDFREE_NOTHING(struct nameidata *ndp)
1507 {
1508 	struct componentname *cnp;
1509 
1510 	cnp = &ndp->ni_cnd;
1511 	KASSERT(cnp->cn_nameiop == LOOKUP, ("%s: got non-LOOKUP op %d\n",
1512 	    __func__, cnp->cn_nameiop));
1513 	KASSERT((cnp->cn_flags & (SAVENAME | HASBUF)) == 0,
1514 	    ("%s: bad flags \%" PRIx64 "\n", __func__, cnp->cn_flags));
1515 }
1516 #endif
1517 
1518 void
1519 (NDFREE)(struct nameidata *ndp, const u_int flags)
1520 {
1521 	int unlock_dvp;
1522 	int unlock_vp;
1523 
1524 	unlock_dvp = 0;
1525 	unlock_vp = 0;
1526 
1527 	if (!(flags & NDF_NO_FREE_PNBUF)) {
1528 		NDFREE_PNBUF(ndp);
1529 	}
1530 	if (!(flags & NDF_NO_VP_UNLOCK) &&
1531 	    (ndp->ni_cnd.cn_flags & LOCKLEAF) && ndp->ni_vp)
1532 		unlock_vp = 1;
1533 	if (!(flags & NDF_NO_DVP_UNLOCK) &&
1534 	    (ndp->ni_cnd.cn_flags & LOCKPARENT) &&
1535 	    ndp->ni_dvp != ndp->ni_vp)
1536 		unlock_dvp = 1;
1537 	if (!(flags & NDF_NO_VP_RELE) && ndp->ni_vp) {
1538 		if (unlock_vp) {
1539 			vput(ndp->ni_vp);
1540 			unlock_vp = 0;
1541 		} else
1542 			vrele(ndp->ni_vp);
1543 		ndp->ni_vp = NULL;
1544 	}
1545 	if (unlock_vp)
1546 		VOP_UNLOCK(ndp->ni_vp);
1547 	if (!(flags & NDF_NO_DVP_RELE) &&
1548 	    (ndp->ni_cnd.cn_flags & (LOCKPARENT|WANTPARENT))) {
1549 		if (unlock_dvp) {
1550 			vput(ndp->ni_dvp);
1551 			unlock_dvp = 0;
1552 		} else
1553 			vrele(ndp->ni_dvp);
1554 		ndp->ni_dvp = NULL;
1555 	}
1556 	if (unlock_dvp)
1557 		VOP_UNLOCK(ndp->ni_dvp);
1558 	if (!(flags & NDF_NO_STARTDIR_RELE) &&
1559 	    (ndp->ni_cnd.cn_flags & SAVESTART)) {
1560 		vrele(ndp->ni_startdir);
1561 		ndp->ni_startdir = NULL;
1562 	}
1563 }
1564 
1565 #ifdef INVARIANTS
1566 /*
1567  * Validate the final state of ndp after the lookup.
1568  *
1569  * Historically filesystems were allowed to modify cn_flags. Most notably they
1570  * can add SAVENAME to the request, resulting in HASBUF and pushing subsequent
1571  * clean up to the consumer. In practice this seems to only concern != LOOKUP
1572  * operations.
1573  *
1574  * As a step towards stricter API contract this routine validates the state to
1575  * clean up. Note validation is a work in progress with the intent of becoming
1576  * stricter over time.
1577  */
1578 #define NDMODIFYINGFLAGS (LOCKLEAF | LOCKPARENT | WANTPARENT | SAVENAME | SAVESTART | HASBUF)
1579 void
1580 NDVALIDATE(struct nameidata *ndp)
1581 {
1582 	struct componentname *cnp;
1583 	u_int64_t used, orig;
1584 
1585 	cnp = &ndp->ni_cnd;
1586 	orig = cnp->cn_origflags;
1587 	used = cnp->cn_flags;
1588 	switch (cnp->cn_nameiop) {
1589 	case LOOKUP:
1590 		/*
1591 		 * For plain lookup we require strict conformance -- nothing
1592 		 * to clean up if it was not requested by the caller.
1593 		 */
1594 		orig &= NDMODIFYINGFLAGS;
1595 		used &= NDMODIFYINGFLAGS;
1596 		if ((orig & (SAVENAME | SAVESTART)) != 0)
1597 			orig |= HASBUF;
1598 		if (orig != used) {
1599 			goto out_mismatch;
1600 		}
1601 		break;
1602 	case CREATE:
1603 	case DELETE:
1604 	case RENAME:
1605 		/*
1606 		 * Some filesystems set SAVENAME to provoke HASBUF, accomodate
1607 		 * for it until it gets fixed.
1608 		 */
1609 		orig &= NDMODIFYINGFLAGS;
1610 		orig |= (SAVENAME | HASBUF);
1611 		used &= NDMODIFYINGFLAGS;
1612 		used |= (SAVENAME | HASBUF);
1613 		if (orig != used) {
1614 			goto out_mismatch;
1615 		}
1616 		break;
1617 	}
1618 	return;
1619 out_mismatch:
1620 	panic("%s: mismatched flags for op %d: added %" PRIx64 ", "
1621 	    "removed %" PRIx64" (%" PRIx64" != %" PRIx64"; stored %" PRIx64" != %" PRIx64")",
1622 	    __func__, cnp->cn_nameiop, used & ~orig, orig &~ used,
1623 	    orig, used, cnp->cn_origflags, cnp->cn_flags);
1624 }
1625 #endif
1626 
1627 /*
1628  * Determine if there is a suitable alternate filename under the specified
1629  * prefix for the specified path.  If the create flag is set, then the
1630  * alternate prefix will be used so long as the parent directory exists.
1631  * This is used by the various compatibility ABIs so that Linux binaries prefer
1632  * files under /compat/linux for example.  The chosen path (whether under
1633  * the prefix or under /) is returned in a kernel malloc'd buffer pointed
1634  * to by pathbuf.  The caller is responsible for free'ing the buffer from
1635  * the M_TEMP bucket if one is returned.
1636  */
1637 int
1638 kern_alternate_path(struct thread *td, const char *prefix, const char *path,
1639     enum uio_seg pathseg, char **pathbuf, int create, int dirfd)
1640 {
1641 	struct nameidata nd, ndroot;
1642 	char *ptr, *buf, *cp;
1643 	size_t len, sz;
1644 	int error;
1645 
1646 	buf = (char *) malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1647 	*pathbuf = buf;
1648 
1649 	/* Copy the prefix into the new pathname as a starting point. */
1650 	len = strlcpy(buf, prefix, MAXPATHLEN);
1651 	if (len >= MAXPATHLEN) {
1652 		*pathbuf = NULL;
1653 		free(buf, M_TEMP);
1654 		return (EINVAL);
1655 	}
1656 	sz = MAXPATHLEN - len;
1657 	ptr = buf + len;
1658 
1659 	/* Append the filename to the prefix. */
1660 	if (pathseg == UIO_SYSSPACE)
1661 		error = copystr(path, ptr, sz, &len);
1662 	else
1663 		error = copyinstr(path, ptr, sz, &len);
1664 
1665 	if (error) {
1666 		*pathbuf = NULL;
1667 		free(buf, M_TEMP);
1668 		return (error);
1669 	}
1670 
1671 	/* Only use a prefix with absolute pathnames. */
1672 	if (*ptr != '/') {
1673 		error = EINVAL;
1674 		goto keeporig;
1675 	}
1676 
1677 	if (dirfd != AT_FDCWD) {
1678 		/*
1679 		 * We want the original because the "prefix" is
1680 		 * included in the already opened dirfd.
1681 		 */
1682 		bcopy(ptr, buf, len);
1683 		return (0);
1684 	}
1685 
1686 	/*
1687 	 * We know that there is a / somewhere in this pathname.
1688 	 * Search backwards for it, to find the file's parent dir
1689 	 * to see if it exists in the alternate tree. If it does,
1690 	 * and we want to create a file (cflag is set). We don't
1691 	 * need to worry about the root comparison in this case.
1692 	 */
1693 
1694 	if (create) {
1695 		for (cp = &ptr[len] - 1; *cp != '/'; cp--);
1696 		*cp = '\0';
1697 
1698 		NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, buf, td);
1699 		error = namei(&nd);
1700 		*cp = '/';
1701 		if (error != 0)
1702 			goto keeporig;
1703 	} else {
1704 		NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, buf, td);
1705 
1706 		error = namei(&nd);
1707 		if (error != 0)
1708 			goto keeporig;
1709 
1710 		/*
1711 		 * We now compare the vnode of the prefix to the one
1712 		 * vnode asked. If they resolve to be the same, then we
1713 		 * ignore the match so that the real root gets used.
1714 		 * This avoids the problem of traversing "../.." to find the
1715 		 * root directory and never finding it, because "/" resolves
1716 		 * to the emulation root directory. This is expensive :-(
1717 		 */
1718 		NDINIT(&ndroot, LOOKUP, FOLLOW, UIO_SYSSPACE, prefix,
1719 		    td);
1720 
1721 		/* We shouldn't ever get an error from this namei(). */
1722 		error = namei(&ndroot);
1723 		if (error == 0) {
1724 			if (nd.ni_vp == ndroot.ni_vp)
1725 				error = ENOENT;
1726 
1727 			NDFREE(&ndroot, NDF_ONLY_PNBUF);
1728 			vrele(ndroot.ni_vp);
1729 		}
1730 	}
1731 
1732 	NDFREE(&nd, NDF_ONLY_PNBUF);
1733 	vrele(nd.ni_vp);
1734 
1735 keeporig:
1736 	/* If there was an error, use the original path name. */
1737 	if (error)
1738 		bcopy(ptr, buf, len);
1739 	return (error);
1740 }
1741