xref: /freebsd/sys/kern/vfs_lookup.c (revision 7c1b51d6dc2e165ae7333373513b080f17cf79bd)
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  * 3. 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_capsicum.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/capsicum.h>
47 #include <sys/fcntl.h>
48 #include <sys/jail.h>
49 #include <sys/lock.h>
50 #include <sys/mutex.h>
51 #include <sys/namei.h>
52 #include <sys/vnode.h>
53 #include <sys/mount.h>
54 #include <sys/filedesc.h>
55 #include <sys/proc.h>
56 #include <sys/sdt.h>
57 #include <sys/syscallsubr.h>
58 #include <sys/sysctl.h>
59 #ifdef KTRACE
60 #include <sys/ktrace.h>
61 #endif
62 
63 #include <security/audit/audit.h>
64 #include <security/mac/mac_framework.h>
65 
66 #include <vm/uma.h>
67 
68 #define	NAMEI_DIAGNOSTIC 1
69 #undef NAMEI_DIAGNOSTIC
70 
71 SDT_PROVIDER_DECLARE(vfs);
72 SDT_PROBE_DEFINE3(vfs, namei, lookup, entry, "struct vnode *", "char *",
73     "unsigned long");
74 SDT_PROBE_DEFINE2(vfs, namei, lookup, return, "int", "struct vnode *");
75 
76 /* Allocation zone for namei. */
77 uma_zone_t namei_zone;
78 
79 /* Placeholder vnode for mp traversal. */
80 static struct vnode *vp_crossmp;
81 
82 static int
83 crossmp_vop_islocked(struct vop_islocked_args *ap)
84 {
85 
86 	return (LK_SHARED);
87 }
88 
89 static int
90 crossmp_vop_lock1(struct vop_lock1_args *ap)
91 {
92 	struct vnode *vp;
93 	struct lock *lk;
94 	const char *file;
95 	int flags, line;
96 
97 	vp = ap->a_vp;
98 	lk = vp->v_vnlock;
99 	flags = ap->a_flags;
100 	file = ap->a_file;
101 	line = ap->a_line;
102 
103 	if ((flags & LK_SHARED) == 0)
104 		panic("invalid lock request for crossmp");
105 
106 	WITNESS_CHECKORDER(&lk->lock_object, LOP_NEWORDER, file, line,
107 	    flags & LK_INTERLOCK ? &VI_MTX(vp)->lock_object : NULL);
108 	WITNESS_LOCK(&lk->lock_object, 0, file, line);
109 	if ((flags & LK_INTERLOCK) != 0)
110 		VI_UNLOCK(vp);
111 	LOCK_LOG_LOCK("SLOCK", &lk->lock_object, 0, 0, ap->a_file, line);
112 	return (0);
113 }
114 
115 static int
116 crossmp_vop_unlock(struct vop_unlock_args *ap)
117 {
118 	struct vnode *vp;
119 	struct lock *lk;
120 	int flags;
121 
122 	vp = ap->a_vp;
123 	lk = vp->v_vnlock;
124 	flags = ap->a_flags;
125 
126 	if ((flags & LK_INTERLOCK) != 0)
127 		VI_UNLOCK(vp);
128 	WITNESS_UNLOCK(&lk->lock_object, 0, LOCK_FILE, LOCK_LINE);
129 	LOCK_LOG_LOCK("SUNLOCK", &lk->lock_object, 0, 0, LOCK_FILE,
130 	    LOCK_LINE);
131 	return (0);
132 }
133 
134 static struct vop_vector crossmp_vnodeops = {
135 	.vop_default =		&default_vnodeops,
136 	.vop_islocked =		crossmp_vop_islocked,
137 	.vop_lock1 =		crossmp_vop_lock1,
138 	.vop_unlock =		crossmp_vop_unlock,
139 };
140 
141 struct nameicap_tracker {
142 	struct vnode *dp;
143 	TAILQ_ENTRY(nameicap_tracker) nm_link;
144 };
145 
146 /* Zone for cap mode tracker elements used for dotdot capability checks. */
147 static uma_zone_t nt_zone;
148 
149 static void
150 nameiinit(void *dummy __unused)
151 {
152 
153 	namei_zone = uma_zcreate("NAMEI", MAXPATHLEN, NULL, NULL, NULL, NULL,
154 	    UMA_ALIGN_PTR, 0);
155 	nt_zone = uma_zcreate("rentr", sizeof(struct nameicap_tracker),
156 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
157 	getnewvnode("crossmp", NULL, &crossmp_vnodeops, &vp_crossmp);
158 }
159 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_SECOND, nameiinit, NULL);
160 
161 static int lookup_shared = 1;
162 SYSCTL_INT(_vfs, OID_AUTO, lookup_shared, CTLFLAG_RWTUN, &lookup_shared, 0,
163     "enables shared locks for path name translation");
164 
165 static int lookup_cap_dotdot = 1;
166 SYSCTL_INT(_vfs, OID_AUTO, lookup_cap_dotdot, CTLFLAG_RWTUN,
167     &lookup_cap_dotdot, 0,
168     "enables \"..\" components in path lookup in capability mode");
169 static int lookup_cap_dotdot_nonlocal = 1;
170 SYSCTL_INT(_vfs, OID_AUTO, lookup_cap_dotdot_nonlocal, CTLFLAG_RWTUN,
171     &lookup_cap_dotdot_nonlocal, 0,
172     "enables \"..\" components in path lookup in capability mode "
173     "on non-local mount");
174 
175 static void
176 nameicap_tracker_add(struct nameidata *ndp, struct vnode *dp)
177 {
178 	struct nameicap_tracker *nt;
179 
180 	if ((ndp->ni_lcf & NI_LCF_CAP_DOTDOT) == 0 || dp->v_type != VDIR)
181 		return;
182 	nt = uma_zalloc(nt_zone, M_WAITOK);
183 	vhold(dp);
184 	nt->dp = dp;
185 	TAILQ_INSERT_TAIL(&ndp->ni_cap_tracker, nt, nm_link);
186 }
187 
188 static void
189 nameicap_cleanup(struct nameidata *ndp)
190 {
191 	struct nameicap_tracker *nt, *nt1;
192 
193 	KASSERT(TAILQ_EMPTY(&ndp->ni_cap_tracker) ||
194 	    (ndp->ni_lcf & NI_LCF_CAP_DOTDOT) != 0, ("not strictrelative"));
195 	TAILQ_FOREACH_SAFE(nt, &ndp->ni_cap_tracker, nm_link, nt1) {
196 		TAILQ_REMOVE(&ndp->ni_cap_tracker, nt, nm_link);
197 		vdrop(nt->dp);
198 		uma_zfree(nt_zone, nt);
199 	}
200 }
201 
202 /*
203  * For dotdot lookups in capability mode, only allow the component
204  * lookup to succeed if the resulting directory was already traversed
205  * during the operation.  Also fail dotdot lookups for non-local
206  * filesystems, where external agents might assist local lookups to
207  * escape the compartment.
208  */
209 static int
210 nameicap_check_dotdot(struct nameidata *ndp, struct vnode *dp)
211 {
212 	struct nameicap_tracker *nt;
213 	struct mount *mp;
214 
215 	if ((ndp->ni_lcf & NI_LCF_CAP_DOTDOT) == 0 || dp == NULL ||
216 	    dp->v_type != VDIR)
217 		return (0);
218 	mp = dp->v_mount;
219 	if (lookup_cap_dotdot_nonlocal == 0 && mp != NULL &&
220 	    (mp->mnt_flag & MNT_LOCAL) == 0)
221 		return (ENOTCAPABLE);
222 	TAILQ_FOREACH_REVERSE(nt, &ndp->ni_cap_tracker, nameicap_tracker_head,
223 	    nm_link) {
224 		if (dp == nt->dp)
225 			return (0);
226 	}
227 	return (ENOTCAPABLE);
228 }
229 
230 static void
231 namei_cleanup_cnp(struct componentname *cnp)
232 {
233 
234 	uma_zfree(namei_zone, cnp->cn_pnbuf);
235 #ifdef DIAGNOSTIC
236 	cnp->cn_pnbuf = NULL;
237 	cnp->cn_nameptr = NULL;
238 #endif
239 }
240 
241 static int
242 namei_handle_root(struct nameidata *ndp, struct vnode **dpp)
243 {
244 	struct componentname *cnp;
245 
246 	cnp = &ndp->ni_cnd;
247 	if ((ndp->ni_lcf & NI_LCF_STRICTRELATIVE) != 0) {
248 #ifdef KTRACE
249 		if (KTRPOINT(curthread, KTR_CAPFAIL))
250 			ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL);
251 #endif
252 		return (ENOTCAPABLE);
253 	}
254 	while (*(cnp->cn_nameptr) == '/') {
255 		cnp->cn_nameptr++;
256 		ndp->ni_pathlen--;
257 	}
258 	*dpp = ndp->ni_rootdir;
259 	vrefact(*dpp);
260 	return (0);
261 }
262 
263 /*
264  * Convert a pathname into a pointer to a locked vnode.
265  *
266  * The FOLLOW flag is set when symbolic links are to be followed
267  * when they occur at the end of the name translation process.
268  * Symbolic links are always followed for all other pathname
269  * components other than the last.
270  *
271  * The segflg defines whether the name is to be copied from user
272  * space or kernel space.
273  *
274  * Overall outline of namei:
275  *
276  *	copy in name
277  *	get starting directory
278  *	while (!done && !error) {
279  *		call lookup to search path.
280  *		if symbolic link, massage name in buffer and continue
281  *	}
282  */
283 int
284 namei(struct nameidata *ndp)
285 {
286 	struct filedesc *fdp;	/* pointer to file descriptor state */
287 	char *cp;		/* pointer into pathname argument */
288 	struct vnode *dp;	/* the directory we are searching */
289 	struct iovec aiov;		/* uio for reading symbolic links */
290 	struct componentname *cnp;
291 	struct thread *td;
292 	struct proc *p;
293 	cap_rights_t rights;
294 	struct uio auio;
295 	int error, linklen, startdir_used;
296 
297 	cnp = &ndp->ni_cnd;
298 	td = cnp->cn_thread;
299 	p = td->td_proc;
300 	ndp->ni_cnd.cn_cred = ndp->ni_cnd.cn_thread->td_ucred;
301 	KASSERT(cnp->cn_cred && p, ("namei: bad cred/proc"));
302 	KASSERT((cnp->cn_nameiop & (~OPMASK)) == 0,
303 	    ("namei: nameiop contaminated with flags"));
304 	KASSERT((cnp->cn_flags & OPMASK) == 0,
305 	    ("namei: flags contaminated with nameiops"));
306 	MPASS(ndp->ni_startdir == NULL || ndp->ni_startdir->v_type == VDIR ||
307 	    ndp->ni_startdir->v_type == VBAD);
308 	if (!lookup_shared)
309 		cnp->cn_flags &= ~LOCKSHARED;
310 	fdp = p->p_fd;
311 	TAILQ_INIT(&ndp->ni_cap_tracker);
312 	ndp->ni_lcf = 0;
313 
314 	/* We will set this ourselves if we need it. */
315 	cnp->cn_flags &= ~TRAILINGSLASH;
316 
317 	/*
318 	 * Get a buffer for the name to be translated, and copy the
319 	 * name into the buffer.
320 	 */
321 	if ((cnp->cn_flags & HASBUF) == 0)
322 		cnp->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK);
323 	if (ndp->ni_segflg == UIO_SYSSPACE)
324 		error = copystr(ndp->ni_dirp, cnp->cn_pnbuf, MAXPATHLEN,
325 		    &ndp->ni_pathlen);
326 	else
327 		error = copyinstr(ndp->ni_dirp, cnp->cn_pnbuf, MAXPATHLEN,
328 		    &ndp->ni_pathlen);
329 
330 	/*
331 	 * Don't allow empty pathnames.
332 	 */
333 	if (error == 0 && *cnp->cn_pnbuf == '\0')
334 		error = ENOENT;
335 
336 #ifdef CAPABILITY_MODE
337 	/*
338 	 * In capability mode, lookups must be restricted to happen in
339 	 * the subtree with the root specified by the file descriptor:
340 	 * - The root must be real file descriptor, not the pseudo-descriptor
341 	 *   AT_FDCWD.
342 	 * - The passed path must be relative and not absolute.
343 	 * - If lookup_cap_dotdot is disabled, path must not contain the
344 	 *   '..' components.
345 	 * - If lookup_cap_dotdot is enabled, we verify that all '..'
346 	 *   components lookups result in the directories which were
347 	 *   previously walked by us, which prevents an escape from
348 	 *   the relative root.
349 	 */
350 	if (error == 0 && IN_CAPABILITY_MODE(td) &&
351 	    (cnp->cn_flags & NOCAPCHECK) == 0) {
352 		ndp->ni_lcf |= NI_LCF_STRICTRELATIVE;
353 		if (ndp->ni_dirfd == AT_FDCWD) {
354 #ifdef KTRACE
355 			if (KTRPOINT(td, KTR_CAPFAIL))
356 				ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL);
357 #endif
358 			error = ECAPMODE;
359 		}
360 	}
361 #endif
362 	if (error != 0) {
363 		namei_cleanup_cnp(cnp);
364 		ndp->ni_vp = NULL;
365 		return (error);
366 	}
367 	ndp->ni_loopcnt = 0;
368 #ifdef KTRACE
369 	if (KTRPOINT(td, KTR_NAMEI)) {
370 		KASSERT(cnp->cn_thread == curthread,
371 		    ("namei not using curthread"));
372 		ktrnamei(cnp->cn_pnbuf);
373 	}
374 #endif
375 	/*
376 	 * Get starting point for the translation.
377 	 */
378 	FILEDESC_SLOCK(fdp);
379 	ndp->ni_rootdir = fdp->fd_rdir;
380 	vrefact(ndp->ni_rootdir);
381 	ndp->ni_topdir = fdp->fd_jdir;
382 
383 	/*
384 	 * If we are auditing the kernel pathname, save the user pathname.
385 	 */
386 	if (cnp->cn_flags & AUDITVNODE1)
387 		AUDIT_ARG_UPATH1(td, ndp->ni_dirfd, cnp->cn_pnbuf);
388 	if (cnp->cn_flags & AUDITVNODE2)
389 		AUDIT_ARG_UPATH2(td, ndp->ni_dirfd, cnp->cn_pnbuf);
390 
391 	startdir_used = 0;
392 	dp = NULL;
393 	cnp->cn_nameptr = cnp->cn_pnbuf;
394 	if (cnp->cn_pnbuf[0] == '/') {
395 		error = namei_handle_root(ndp, &dp);
396 	} else {
397 		if (ndp->ni_startdir != NULL) {
398 			dp = ndp->ni_startdir;
399 			startdir_used = 1;
400 		} else if (ndp->ni_dirfd == AT_FDCWD) {
401 			dp = fdp->fd_cdir;
402 			vrefact(dp);
403 		} else {
404 			rights = ndp->ni_rightsneeded;
405 			cap_rights_set(&rights, CAP_LOOKUP);
406 
407 			if (cnp->cn_flags & AUDITVNODE1)
408 				AUDIT_ARG_ATFD1(ndp->ni_dirfd);
409 			if (cnp->cn_flags & AUDITVNODE2)
410 				AUDIT_ARG_ATFD2(ndp->ni_dirfd);
411 			error = fgetvp_rights(td, ndp->ni_dirfd,
412 			    &rights, &ndp->ni_filecaps, &dp);
413 			if (error == EINVAL)
414 				error = ENOTDIR;
415 #ifdef CAPABILITIES
416 			/*
417 			 * If file descriptor doesn't have all rights,
418 			 * all lookups relative to it must also be
419 			 * strictly relative.
420 			 */
421 			CAP_ALL(&rights);
422 			if (!cap_rights_contains(&ndp->ni_filecaps.fc_rights,
423 			    &rights) ||
424 			    ndp->ni_filecaps.fc_fcntls != CAP_FCNTL_ALL ||
425 			    ndp->ni_filecaps.fc_nioctls != -1) {
426 				ndp->ni_lcf |= NI_LCF_STRICTRELATIVE;
427 			}
428 #endif
429 		}
430 		if (error == 0 && dp->v_type != VDIR)
431 			error = ENOTDIR;
432 	}
433 	FILEDESC_SUNLOCK(fdp);
434 	if (ndp->ni_startdir != NULL && !startdir_used)
435 		vrele(ndp->ni_startdir);
436 	if (error != 0) {
437 		if (dp != NULL)
438 			vrele(dp);
439 		goto out;
440 	}
441 	if ((ndp->ni_lcf & NI_LCF_STRICTRELATIVE) != 0 &&
442 	    lookup_cap_dotdot != 0)
443 		ndp->ni_lcf |= NI_LCF_CAP_DOTDOT;
444 	SDT_PROBE3(vfs, namei, lookup, entry, dp, cnp->cn_pnbuf,
445 	    cnp->cn_flags);
446 	for (;;) {
447 		ndp->ni_startdir = dp;
448 		error = lookup(ndp);
449 		if (error != 0)
450 			goto out;
451 		/*
452 		 * If not a symbolic link, we're done.
453 		 */
454 		if ((cnp->cn_flags & ISSYMLINK) == 0) {
455 			vrele(ndp->ni_rootdir);
456 			if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0) {
457 				namei_cleanup_cnp(cnp);
458 			} else
459 				cnp->cn_flags |= HASBUF;
460 			nameicap_cleanup(ndp);
461 			SDT_PROBE2(vfs, namei, lookup, return, 0, ndp->ni_vp);
462 			return (0);
463 		}
464 		if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
465 			error = ELOOP;
466 			break;
467 		}
468 #ifdef MAC
469 		if ((cnp->cn_flags & NOMACCHECK) == 0) {
470 			error = mac_vnode_check_readlink(td->td_ucred,
471 			    ndp->ni_vp);
472 			if (error != 0)
473 				break;
474 		}
475 #endif
476 		if (ndp->ni_pathlen > 1)
477 			cp = uma_zalloc(namei_zone, M_WAITOK);
478 		else
479 			cp = cnp->cn_pnbuf;
480 		aiov.iov_base = cp;
481 		aiov.iov_len = MAXPATHLEN;
482 		auio.uio_iov = &aiov;
483 		auio.uio_iovcnt = 1;
484 		auio.uio_offset = 0;
485 		auio.uio_rw = UIO_READ;
486 		auio.uio_segflg = UIO_SYSSPACE;
487 		auio.uio_td = td;
488 		auio.uio_resid = MAXPATHLEN;
489 		error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
490 		if (error != 0) {
491 			if (ndp->ni_pathlen > 1)
492 				uma_zfree(namei_zone, cp);
493 			break;
494 		}
495 		linklen = MAXPATHLEN - auio.uio_resid;
496 		if (linklen == 0) {
497 			if (ndp->ni_pathlen > 1)
498 				uma_zfree(namei_zone, cp);
499 			error = ENOENT;
500 			break;
501 		}
502 		if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
503 			if (ndp->ni_pathlen > 1)
504 				uma_zfree(namei_zone, cp);
505 			error = ENAMETOOLONG;
506 			break;
507 		}
508 		if (ndp->ni_pathlen > 1) {
509 			bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
510 			uma_zfree(namei_zone, cnp->cn_pnbuf);
511 			cnp->cn_pnbuf = cp;
512 		} else
513 			cnp->cn_pnbuf[linklen] = '\0';
514 		ndp->ni_pathlen += linklen;
515 		vput(ndp->ni_vp);
516 		dp = ndp->ni_dvp;
517 		/*
518 		 * Check if root directory should replace current directory.
519 		 */
520 		cnp->cn_nameptr = cnp->cn_pnbuf;
521 		if (*(cnp->cn_nameptr) == '/') {
522 			vrele(dp);
523 			error = namei_handle_root(ndp, &dp);
524 			if (error != 0)
525 				goto out;
526 		}
527 	}
528 	vput(ndp->ni_vp);
529 	ndp->ni_vp = NULL;
530 	vrele(ndp->ni_dvp);
531 out:
532 	vrele(ndp->ni_rootdir);
533 	namei_cleanup_cnp(cnp);
534 	nameicap_cleanup(ndp);
535 	SDT_PROBE2(vfs, namei, lookup, return, error, NULL);
536 	return (error);
537 }
538 
539 static int
540 compute_cn_lkflags(struct mount *mp, int lkflags, int cnflags)
541 {
542 
543 	if (mp == NULL || ((lkflags & LK_SHARED) &&
544 	    (!(mp->mnt_kern_flag & MNTK_LOOKUP_SHARED) ||
545 	    ((cnflags & ISDOTDOT) &&
546 	    (mp->mnt_kern_flag & MNTK_LOOKUP_EXCL_DOTDOT))))) {
547 		lkflags &= ~LK_SHARED;
548 		lkflags |= LK_EXCLUSIVE;
549 	}
550 	lkflags |= LK_NODDLKTREAT;
551 	return (lkflags);
552 }
553 
554 static __inline int
555 needs_exclusive_leaf(struct mount *mp, int flags)
556 {
557 
558 	/*
559 	 * Intermediate nodes can use shared locks, we only need to
560 	 * force an exclusive lock for leaf nodes.
561 	 */
562 	if ((flags & (ISLASTCN | LOCKLEAF)) != (ISLASTCN | LOCKLEAF))
563 		return (0);
564 
565 	/* Always use exclusive locks if LOCKSHARED isn't set. */
566 	if (!(flags & LOCKSHARED))
567 		return (1);
568 
569 	/*
570 	 * For lookups during open(), if the mount point supports
571 	 * extended shared operations, then use a shared lock for the
572 	 * leaf node, otherwise use an exclusive lock.
573 	 */
574 	if ((flags & ISOPEN) != 0)
575 		return (!MNT_EXTENDED_SHARED(mp));
576 
577 	/*
578 	 * Lookup requests outside of open() that specify LOCKSHARED
579 	 * only need a shared lock on the leaf vnode.
580 	 */
581 	return (0);
582 }
583 
584 /*
585  * Search a pathname.
586  * This is a very central and rather complicated routine.
587  *
588  * The pathname is pointed to by ni_ptr and is of length ni_pathlen.
589  * The starting directory is taken from ni_startdir. The pathname is
590  * descended until done, or a symbolic link is encountered. The variable
591  * ni_more is clear if the path is completed; it is set to one if a
592  * symbolic link needing interpretation is encountered.
593  *
594  * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
595  * whether the name is to be looked up, created, renamed, or deleted.
596  * When CREATE, RENAME, or DELETE is specified, information usable in
597  * creating, renaming, or deleting a directory entry may be calculated.
598  * If flag has LOCKPARENT or'ed into it, the parent directory is returned
599  * locked. If flag has WANTPARENT or'ed into it, the parent directory is
600  * returned unlocked. Otherwise the parent directory is not returned. If
601  * the target of the pathname exists and LOCKLEAF is or'ed into the flag
602  * the target is returned locked, otherwise it is returned unlocked.
603  * When creating or renaming and LOCKPARENT is specified, the target may not
604  * be ".".  When deleting and LOCKPARENT is specified, the target may be ".".
605  *
606  * Overall outline of lookup:
607  *
608  * dirloop:
609  *	identify next component of name at ndp->ni_ptr
610  *	handle degenerate case where name is null string
611  *	if .. and crossing mount points and on mounted filesys, find parent
612  *	call VOP_LOOKUP routine for next component name
613  *	    directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set
614  *	    component vnode returned in ni_vp (if it exists), locked.
615  *	if result vnode is mounted on and crossing mount points,
616  *	    find mounted on vnode
617  *	if more components of name, do next level at dirloop
618  *	return the answer in ni_vp, locked if LOCKLEAF set
619  *	    if LOCKPARENT set, return locked parent in ni_dvp
620  *	    if WANTPARENT set, return unlocked parent in ni_dvp
621  */
622 int
623 lookup(struct nameidata *ndp)
624 {
625 	char *cp;			/* pointer into pathname argument */
626 	char *prev_ni_next;		/* saved ndp->ni_next */
627 	struct vnode *dp = NULL;	/* the directory we are searching */
628 	struct vnode *tdp;		/* saved dp */
629 	struct mount *mp;		/* mount table entry */
630 	struct prison *pr;
631 	size_t prev_ni_pathlen;		/* saved ndp->ni_pathlen */
632 	int docache;			/* == 0 do not cache last component */
633 	int wantparent;			/* 1 => wantparent or lockparent flag */
634 	int rdonly;			/* lookup read-only flag bit */
635 	int error = 0;
636 	int dpunlocked = 0;		/* dp has already been unlocked */
637 	int relookup = 0;		/* do not consume the path component */
638 	struct componentname *cnp = &ndp->ni_cnd;
639 	int lkflags_save;
640 	int ni_dvp_unlocked;
641 
642 	/*
643 	 * Setup: break out flag bits into variables.
644 	 */
645 	ni_dvp_unlocked = 0;
646 	wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT);
647 	KASSERT(cnp->cn_nameiop == LOOKUP || wantparent,
648 	    ("CREATE, DELETE, RENAME require LOCKPARENT or WANTPARENT."));
649 	docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE;
650 	if (cnp->cn_nameiop == DELETE ||
651 	    (wantparent && cnp->cn_nameiop != CREATE &&
652 	     cnp->cn_nameiop != LOOKUP))
653 		docache = 0;
654 	rdonly = cnp->cn_flags & RDONLY;
655 	cnp->cn_flags &= ~ISSYMLINK;
656 	ndp->ni_dvp = NULL;
657 	/*
658 	 * We use shared locks until we hit the parent of the last cn then
659 	 * we adjust based on the requesting flags.
660 	 */
661 	if (lookup_shared)
662 		cnp->cn_lkflags = LK_SHARED;
663 	else
664 		cnp->cn_lkflags = LK_EXCLUSIVE;
665 	dp = ndp->ni_startdir;
666 	ndp->ni_startdir = NULLVP;
667 	vn_lock(dp,
668 	    compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags | LK_RETRY,
669 	    cnp->cn_flags));
670 
671 dirloop:
672 	/*
673 	 * Search a new directory.
674 	 *
675 	 * The last component of the filename is left accessible via
676 	 * cnp->cn_nameptr for callers that need the name. Callers needing
677 	 * the name set the SAVENAME flag. When done, they assume
678 	 * responsibility for freeing the pathname buffer.
679 	 */
680 	for (cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++)
681 		continue;
682 	cnp->cn_namelen = cp - cnp->cn_nameptr;
683 	if (cnp->cn_namelen > NAME_MAX) {
684 		error = ENAMETOOLONG;
685 		goto bad;
686 	}
687 #ifdef NAMEI_DIAGNOSTIC
688 	{ char c = *cp;
689 	*cp = '\0';
690 	printf("{%s}: ", cnp->cn_nameptr);
691 	*cp = c; }
692 #endif
693 	prev_ni_pathlen = ndp->ni_pathlen;
694 	ndp->ni_pathlen -= cnp->cn_namelen;
695 	KASSERT(ndp->ni_pathlen <= PATH_MAX,
696 	    ("%s: ni_pathlen underflow to %zd\n", __func__, ndp->ni_pathlen));
697 	prev_ni_next = ndp->ni_next;
698 	ndp->ni_next = cp;
699 
700 	/*
701 	 * Replace multiple slashes by a single slash and trailing slashes
702 	 * by a null.  This must be done before VOP_LOOKUP() because some
703 	 * fs's don't know about trailing slashes.  Remember if there were
704 	 * trailing slashes to handle symlinks, existing non-directories
705 	 * and non-existing files that won't be directories specially later.
706 	 */
707 	while (*cp == '/' && (cp[1] == '/' || cp[1] == '\0')) {
708 		cp++;
709 		ndp->ni_pathlen--;
710 		if (*cp == '\0') {
711 			*ndp->ni_next = '\0';
712 			cnp->cn_flags |= TRAILINGSLASH;
713 		}
714 	}
715 	ndp->ni_next = cp;
716 
717 	cnp->cn_flags |= MAKEENTRY;
718 	if (*cp == '\0' && docache == 0)
719 		cnp->cn_flags &= ~MAKEENTRY;
720 	if (cnp->cn_namelen == 2 &&
721 	    cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.')
722 		cnp->cn_flags |= ISDOTDOT;
723 	else
724 		cnp->cn_flags &= ~ISDOTDOT;
725 	if (*ndp->ni_next == 0)
726 		cnp->cn_flags |= ISLASTCN;
727 	else
728 		cnp->cn_flags &= ~ISLASTCN;
729 
730 	if ((cnp->cn_flags & ISLASTCN) != 0 &&
731 	    cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.' &&
732 	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
733 		error = EINVAL;
734 		goto bad;
735 	}
736 
737 	nameicap_tracker_add(ndp, dp);
738 
739 	/*
740 	 * Check for degenerate name (e.g. / or "")
741 	 * which is a way of talking about a directory,
742 	 * e.g. like "/." or ".".
743 	 */
744 	if (cnp->cn_nameptr[0] == '\0') {
745 		if (dp->v_type != VDIR) {
746 			error = ENOTDIR;
747 			goto bad;
748 		}
749 		if (cnp->cn_nameiop != LOOKUP) {
750 			error = EISDIR;
751 			goto bad;
752 		}
753 		if (wantparent) {
754 			ndp->ni_dvp = dp;
755 			VREF(dp);
756 		}
757 		ndp->ni_vp = dp;
758 
759 		if (cnp->cn_flags & AUDITVNODE1)
760 			AUDIT_ARG_VNODE1(dp);
761 		else if (cnp->cn_flags & AUDITVNODE2)
762 			AUDIT_ARG_VNODE2(dp);
763 
764 		if (!(cnp->cn_flags & (LOCKPARENT | LOCKLEAF)))
765 			VOP_UNLOCK(dp, 0);
766 		/* XXX This should probably move to the top of function. */
767 		if (cnp->cn_flags & SAVESTART)
768 			panic("lookup: SAVESTART");
769 		goto success;
770 	}
771 
772 	/*
773 	 * Handle "..": five special cases.
774 	 * 0. If doing a capability lookup and lookup_cap_dotdot is
775 	 *    disabled, return ENOTCAPABLE.
776 	 * 1. Return an error if this is the last component of
777 	 *    the name and the operation is DELETE or RENAME.
778 	 * 2. If at root directory (e.g. after chroot)
779 	 *    or at absolute root directory
780 	 *    then ignore it so can't get out.
781 	 * 3. If this vnode is the root of a mounted
782 	 *    filesystem, then replace it with the
783 	 *    vnode which was mounted on so we take the
784 	 *    .. in the other filesystem.
785 	 * 4. If the vnode is the top directory of
786 	 *    the jail or chroot, don't let them out.
787 	 * 5. If doing a capability lookup and lookup_cap_dotdot is
788 	 *    enabled, return ENOTCAPABLE if the lookup would escape
789 	 *    from the initial file descriptor directory.  Checks are
790 	 *    done by ensuring that namei() already traversed the
791 	 *    result of dotdot lookup.
792 	 */
793 	if (cnp->cn_flags & ISDOTDOT) {
794 		if ((ndp->ni_lcf & (NI_LCF_STRICTRELATIVE | NI_LCF_CAP_DOTDOT))
795 		    == NI_LCF_STRICTRELATIVE) {
796 #ifdef KTRACE
797 			if (KTRPOINT(curthread, KTR_CAPFAIL))
798 				ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL);
799 #endif
800 			error = ENOTCAPABLE;
801 			goto bad;
802 		}
803 		if ((cnp->cn_flags & ISLASTCN) != 0 &&
804 		    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
805 			error = EINVAL;
806 			goto bad;
807 		}
808 		for (;;) {
809 			for (pr = cnp->cn_cred->cr_prison; pr != NULL;
810 			     pr = pr->pr_parent)
811 				if (dp == pr->pr_root)
812 					break;
813 			if (dp == ndp->ni_rootdir ||
814 			    dp == ndp->ni_topdir ||
815 			    dp == rootvnode ||
816 			    pr != NULL ||
817 			    ((dp->v_vflag & VV_ROOT) != 0 &&
818 			     (cnp->cn_flags & NOCROSSMOUNT) != 0)) {
819 				ndp->ni_dvp = dp;
820 				ndp->ni_vp = dp;
821 				VREF(dp);
822 				goto nextname;
823 			}
824 			if ((dp->v_vflag & VV_ROOT) == 0)
825 				break;
826 			if (dp->v_iflag & VI_DOOMED) {	/* forced unmount */
827 				error = ENOENT;
828 				goto bad;
829 			}
830 			tdp = dp;
831 			dp = dp->v_mount->mnt_vnodecovered;
832 			VREF(dp);
833 			vput(tdp);
834 			vn_lock(dp,
835 			    compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags |
836 			    LK_RETRY, ISDOTDOT));
837 			error = nameicap_check_dotdot(ndp, dp);
838 			if (error != 0) {
839 #ifdef KTRACE
840 				if (KTRPOINT(curthread, KTR_CAPFAIL))
841 					ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL);
842 #endif
843 				goto bad;
844 			}
845 		}
846 	}
847 
848 	/*
849 	 * We now have a segment name to search for, and a directory to search.
850 	 */
851 unionlookup:
852 #ifdef MAC
853 	if ((cnp->cn_flags & NOMACCHECK) == 0) {
854 		error = mac_vnode_check_lookup(cnp->cn_thread->td_ucred, dp,
855 		    cnp);
856 		if (error)
857 			goto bad;
858 	}
859 #endif
860 	ndp->ni_dvp = dp;
861 	ndp->ni_vp = NULL;
862 	ASSERT_VOP_LOCKED(dp, "lookup");
863 	/*
864 	 * If we have a shared lock we may need to upgrade the lock for the
865 	 * last operation.
866 	 */
867 	if ((cnp->cn_flags & LOCKPARENT) && (cnp->cn_flags & ISLASTCN) &&
868 	    dp != vp_crossmp && VOP_ISLOCKED(dp) == LK_SHARED)
869 		vn_lock(dp, LK_UPGRADE|LK_RETRY);
870 	if ((dp->v_iflag & VI_DOOMED) != 0) {
871 		error = ENOENT;
872 		goto bad;
873 	}
874 	/*
875 	 * If we're looking up the last component and we need an exclusive
876 	 * lock, adjust our lkflags.
877 	 */
878 	if (needs_exclusive_leaf(dp->v_mount, cnp->cn_flags))
879 		cnp->cn_lkflags = LK_EXCLUSIVE;
880 #ifdef NAMEI_DIAGNOSTIC
881 	vn_printf(dp, "lookup in ");
882 #endif
883 	lkflags_save = cnp->cn_lkflags;
884 	cnp->cn_lkflags = compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags,
885 	    cnp->cn_flags);
886 	error = VOP_LOOKUP(dp, &ndp->ni_vp, cnp);
887 	cnp->cn_lkflags = lkflags_save;
888 	if (error != 0) {
889 		KASSERT(ndp->ni_vp == NULL, ("leaf should be empty"));
890 #ifdef NAMEI_DIAGNOSTIC
891 		printf("not found\n");
892 #endif
893 		if ((error == ENOENT) &&
894 		    (dp->v_vflag & VV_ROOT) && (dp->v_mount != NULL) &&
895 		    (dp->v_mount->mnt_flag & MNT_UNION)) {
896 			tdp = dp;
897 			dp = dp->v_mount->mnt_vnodecovered;
898 			VREF(dp);
899 			vput(tdp);
900 			vn_lock(dp,
901 			    compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags |
902 			    LK_RETRY, cnp->cn_flags));
903 			nameicap_tracker_add(ndp, dp);
904 			goto unionlookup;
905 		}
906 
907 		if (error == ERELOOKUP) {
908 			vref(dp);
909 			ndp->ni_vp = dp;
910 			error = 0;
911 			relookup = 1;
912 			goto good;
913 		}
914 
915 		if (error != EJUSTRETURN)
916 			goto bad;
917 		/*
918 		 * At this point, we know we're at the end of the
919 		 * pathname.  If creating / renaming, we can consider
920 		 * allowing the file or directory to be created / renamed,
921 		 * provided we're not on a read-only filesystem.
922 		 */
923 		if (rdonly) {
924 			error = EROFS;
925 			goto bad;
926 		}
927 		/* trailing slash only allowed for directories */
928 		if ((cnp->cn_flags & TRAILINGSLASH) &&
929 		    !(cnp->cn_flags & WILLBEDIR)) {
930 			error = ENOENT;
931 			goto bad;
932 		}
933 		if ((cnp->cn_flags & LOCKPARENT) == 0)
934 			VOP_UNLOCK(dp, 0);
935 		/*
936 		 * We return with ni_vp NULL to indicate that the entry
937 		 * doesn't currently exist, leaving a pointer to the
938 		 * (possibly locked) directory vnode in ndp->ni_dvp.
939 		 */
940 		if (cnp->cn_flags & SAVESTART) {
941 			ndp->ni_startdir = ndp->ni_dvp;
942 			VREF(ndp->ni_startdir);
943 		}
944 		goto success;
945 	}
946 
947 good:
948 #ifdef NAMEI_DIAGNOSTIC
949 	printf("found\n");
950 #endif
951 	dp = ndp->ni_vp;
952 
953 	/*
954 	 * Check to see if the vnode has been mounted on;
955 	 * if so find the root of the mounted filesystem.
956 	 */
957 	while (dp->v_type == VDIR && (mp = dp->v_mountedhere) &&
958 	       (cnp->cn_flags & NOCROSSMOUNT) == 0) {
959 		if (vfs_busy(mp, 0))
960 			continue;
961 		vput(dp);
962 		if (dp != ndp->ni_dvp)
963 			vput(ndp->ni_dvp);
964 		else
965 			vrele(ndp->ni_dvp);
966 		vrefact(vp_crossmp);
967 		ndp->ni_dvp = vp_crossmp;
968 		error = VFS_ROOT(mp, compute_cn_lkflags(mp, cnp->cn_lkflags,
969 		    cnp->cn_flags), &tdp);
970 		vfs_unbusy(mp);
971 		if (vn_lock(vp_crossmp, LK_SHARED | LK_NOWAIT))
972 			panic("vp_crossmp exclusively locked or reclaimed");
973 		if (error) {
974 			dpunlocked = 1;
975 			goto bad2;
976 		}
977 		ndp->ni_vp = dp = tdp;
978 	}
979 
980 	/*
981 	 * Check for symbolic link
982 	 */
983 	if ((dp->v_type == VLNK) &&
984 	    ((cnp->cn_flags & FOLLOW) || (cnp->cn_flags & TRAILINGSLASH) ||
985 	     *ndp->ni_next == '/')) {
986 		cnp->cn_flags |= ISSYMLINK;
987 		if (dp->v_iflag & VI_DOOMED) {
988 			/*
989 			 * We can't know whether the directory was mounted with
990 			 * NOSYMFOLLOW, so we can't follow safely.
991 			 */
992 			error = ENOENT;
993 			goto bad2;
994 		}
995 		if (dp->v_mount->mnt_flag & MNT_NOSYMFOLLOW) {
996 			error = EACCES;
997 			goto bad2;
998 		}
999 		/*
1000 		 * Symlink code always expects an unlocked dvp.
1001 		 */
1002 		if (ndp->ni_dvp != ndp->ni_vp) {
1003 			VOP_UNLOCK(ndp->ni_dvp, 0);
1004 			ni_dvp_unlocked = 1;
1005 		}
1006 		goto success;
1007 	}
1008 
1009 nextname:
1010 	/*
1011 	 * Not a symbolic link that we will follow.  Continue with the
1012 	 * next component if there is any; otherwise, we're done.
1013 	 */
1014 	KASSERT((cnp->cn_flags & ISLASTCN) || *ndp->ni_next == '/',
1015 	    ("lookup: invalid path state."));
1016 	if (relookup) {
1017 		relookup = 0;
1018 		ndp->ni_pathlen = prev_ni_pathlen;
1019 		ndp->ni_next = prev_ni_next;
1020 		if (ndp->ni_dvp != dp)
1021 			vput(ndp->ni_dvp);
1022 		else
1023 			vrele(ndp->ni_dvp);
1024 		goto dirloop;
1025 	}
1026 	if (cnp->cn_flags & ISDOTDOT) {
1027 		error = nameicap_check_dotdot(ndp, ndp->ni_vp);
1028 		if (error != 0) {
1029 #ifdef KTRACE
1030 			if (KTRPOINT(curthread, KTR_CAPFAIL))
1031 				ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL);
1032 #endif
1033 			goto bad2;
1034 		}
1035 	}
1036 	if (*ndp->ni_next == '/') {
1037 		cnp->cn_nameptr = ndp->ni_next;
1038 		while (*cnp->cn_nameptr == '/') {
1039 			cnp->cn_nameptr++;
1040 			ndp->ni_pathlen--;
1041 		}
1042 		if (ndp->ni_dvp != dp)
1043 			vput(ndp->ni_dvp);
1044 		else
1045 			vrele(ndp->ni_dvp);
1046 		goto dirloop;
1047 	}
1048 	/*
1049 	 * If we're processing a path with a trailing slash,
1050 	 * check that the end result is a directory.
1051 	 */
1052 	if ((cnp->cn_flags & TRAILINGSLASH) && dp->v_type != VDIR) {
1053 		error = ENOTDIR;
1054 		goto bad2;
1055 	}
1056 	/*
1057 	 * Disallow directory write attempts on read-only filesystems.
1058 	 */
1059 	if (rdonly &&
1060 	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
1061 		error = EROFS;
1062 		goto bad2;
1063 	}
1064 	if (cnp->cn_flags & SAVESTART) {
1065 		ndp->ni_startdir = ndp->ni_dvp;
1066 		VREF(ndp->ni_startdir);
1067 	}
1068 	if (!wantparent) {
1069 		ni_dvp_unlocked = 2;
1070 		if (ndp->ni_dvp != dp)
1071 			vput(ndp->ni_dvp);
1072 		else
1073 			vrele(ndp->ni_dvp);
1074 	} else if ((cnp->cn_flags & LOCKPARENT) == 0 && ndp->ni_dvp != dp) {
1075 		VOP_UNLOCK(ndp->ni_dvp, 0);
1076 		ni_dvp_unlocked = 1;
1077 	}
1078 
1079 	if (cnp->cn_flags & AUDITVNODE1)
1080 		AUDIT_ARG_VNODE1(dp);
1081 	else if (cnp->cn_flags & AUDITVNODE2)
1082 		AUDIT_ARG_VNODE2(dp);
1083 
1084 	if ((cnp->cn_flags & LOCKLEAF) == 0)
1085 		VOP_UNLOCK(dp, 0);
1086 success:
1087 	/*
1088 	 * Because of lookup_shared we may have the vnode shared locked, but
1089 	 * the caller may want it to be exclusively locked.
1090 	 */
1091 	if (needs_exclusive_leaf(dp->v_mount, cnp->cn_flags) &&
1092 	    VOP_ISLOCKED(dp) != LK_EXCLUSIVE) {
1093 		vn_lock(dp, LK_UPGRADE | LK_RETRY);
1094 		if (dp->v_iflag & VI_DOOMED) {
1095 			error = ENOENT;
1096 			goto bad2;
1097 		}
1098 	}
1099 	return (0);
1100 
1101 bad2:
1102 	if (ni_dvp_unlocked != 2) {
1103 		if (dp != ndp->ni_dvp && !ni_dvp_unlocked)
1104 			vput(ndp->ni_dvp);
1105 		else
1106 			vrele(ndp->ni_dvp);
1107 	}
1108 bad:
1109 	if (!dpunlocked)
1110 		vput(dp);
1111 	ndp->ni_vp = NULL;
1112 	return (error);
1113 }
1114 
1115 /*
1116  * relookup - lookup a path name component
1117  *    Used by lookup to re-acquire things.
1118  */
1119 int
1120 relookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp)
1121 {
1122 	struct vnode *dp = NULL;		/* the directory we are searching */
1123 	int wantparent;			/* 1 => wantparent or lockparent flag */
1124 	int rdonly;			/* lookup read-only flag bit */
1125 	int error = 0;
1126 
1127 	KASSERT(cnp->cn_flags & ISLASTCN,
1128 	    ("relookup: Not given last component."));
1129 	/*
1130 	 * Setup: break out flag bits into variables.
1131 	 */
1132 	wantparent = cnp->cn_flags & (LOCKPARENT|WANTPARENT);
1133 	KASSERT(wantparent, ("relookup: parent not wanted."));
1134 	rdonly = cnp->cn_flags & RDONLY;
1135 	cnp->cn_flags &= ~ISSYMLINK;
1136 	dp = dvp;
1137 	cnp->cn_lkflags = LK_EXCLUSIVE;
1138 	vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
1139 
1140 	/*
1141 	 * Search a new directory.
1142 	 *
1143 	 * The last component of the filename is left accessible via
1144 	 * cnp->cn_nameptr for callers that need the name. Callers needing
1145 	 * the name set the SAVENAME flag. When done, they assume
1146 	 * responsibility for freeing the pathname buffer.
1147 	 */
1148 #ifdef NAMEI_DIAGNOSTIC
1149 	printf("{%s}: ", cnp->cn_nameptr);
1150 #endif
1151 
1152 	/*
1153 	 * Check for "" which represents the root directory after slash
1154 	 * removal.
1155 	 */
1156 	if (cnp->cn_nameptr[0] == '\0') {
1157 		/*
1158 		 * Support only LOOKUP for "/" because lookup()
1159 		 * can't succeed for CREATE, DELETE and RENAME.
1160 		 */
1161 		KASSERT(cnp->cn_nameiop == LOOKUP, ("nameiop must be LOOKUP"));
1162 		KASSERT(dp->v_type == VDIR, ("dp is not a directory"));
1163 
1164 		if (!(cnp->cn_flags & LOCKLEAF))
1165 			VOP_UNLOCK(dp, 0);
1166 		*vpp = dp;
1167 		/* XXX This should probably move to the top of function. */
1168 		if (cnp->cn_flags & SAVESTART)
1169 			panic("lookup: SAVESTART");
1170 		return (0);
1171 	}
1172 
1173 	if (cnp->cn_flags & ISDOTDOT)
1174 		panic ("relookup: lookup on dot-dot");
1175 
1176 	/*
1177 	 * We now have a segment name to search for, and a directory to search.
1178 	 */
1179 #ifdef NAMEI_DIAGNOSTIC
1180 	vn_printf(dp, "search in ");
1181 #endif
1182 	if ((error = VOP_LOOKUP(dp, vpp, cnp)) != 0) {
1183 		KASSERT(*vpp == NULL, ("leaf should be empty"));
1184 		if (error != EJUSTRETURN)
1185 			goto bad;
1186 		/*
1187 		 * If creating and at end of pathname, then can consider
1188 		 * allowing file to be created.
1189 		 */
1190 		if (rdonly) {
1191 			error = EROFS;
1192 			goto bad;
1193 		}
1194 		/* ASSERT(dvp == ndp->ni_startdir) */
1195 		if (cnp->cn_flags & SAVESTART)
1196 			VREF(dvp);
1197 		if ((cnp->cn_flags & LOCKPARENT) == 0)
1198 			VOP_UNLOCK(dp, 0);
1199 		/*
1200 		 * We return with ni_vp NULL to indicate that the entry
1201 		 * doesn't currently exist, leaving a pointer to the
1202 		 * (possibly locked) directory vnode in ndp->ni_dvp.
1203 		 */
1204 		return (0);
1205 	}
1206 
1207 	dp = *vpp;
1208 
1209 	/*
1210 	 * Disallow directory write attempts on read-only filesystems.
1211 	 */
1212 	if (rdonly &&
1213 	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
1214 		if (dvp == dp)
1215 			vrele(dvp);
1216 		else
1217 			vput(dvp);
1218 		error = EROFS;
1219 		goto bad;
1220 	}
1221 	/*
1222 	 * Set the parent lock/ref state to the requested state.
1223 	 */
1224 	if ((cnp->cn_flags & LOCKPARENT) == 0 && dvp != dp) {
1225 		if (wantparent)
1226 			VOP_UNLOCK(dvp, 0);
1227 		else
1228 			vput(dvp);
1229 	} else if (!wantparent)
1230 		vrele(dvp);
1231 	/*
1232 	 * Check for symbolic link
1233 	 */
1234 	KASSERT(dp->v_type != VLNK || !(cnp->cn_flags & FOLLOW),
1235 	    ("relookup: symlink found.\n"));
1236 
1237 	/* ASSERT(dvp == ndp->ni_startdir) */
1238 	if (cnp->cn_flags & SAVESTART)
1239 		VREF(dvp);
1240 
1241 	if ((cnp->cn_flags & LOCKLEAF) == 0)
1242 		VOP_UNLOCK(dp, 0);
1243 	return (0);
1244 bad:
1245 	vput(dp);
1246 	*vpp = NULL;
1247 	return (error);
1248 }
1249 
1250 void
1251 NDINIT_ALL(struct nameidata *ndp, u_long op, u_long flags, enum uio_seg segflg,
1252     const char *namep, int dirfd, struct vnode *startdir, cap_rights_t *rightsp,
1253     struct thread *td)
1254 {
1255 
1256 	ndp->ni_cnd.cn_nameiop = op;
1257 	ndp->ni_cnd.cn_flags = flags;
1258 	ndp->ni_segflg = segflg;
1259 	ndp->ni_dirp = namep;
1260 	ndp->ni_dirfd = dirfd;
1261 	ndp->ni_startdir = startdir;
1262 	if (rightsp != NULL)
1263 		ndp->ni_rightsneeded = *rightsp;
1264 	else
1265 		cap_rights_init(&ndp->ni_rightsneeded);
1266 	filecaps_init(&ndp->ni_filecaps);
1267 	ndp->ni_cnd.cn_thread = td;
1268 }
1269 
1270 /*
1271  * Free data allocated by namei(); see namei(9) for details.
1272  */
1273 void
1274 NDFREE(struct nameidata *ndp, const u_int flags)
1275 {
1276 	int unlock_dvp;
1277 	int unlock_vp;
1278 
1279 	unlock_dvp = 0;
1280 	unlock_vp = 0;
1281 
1282 	if (!(flags & NDF_NO_FREE_PNBUF) &&
1283 	    (ndp->ni_cnd.cn_flags & HASBUF)) {
1284 		uma_zfree(namei_zone, ndp->ni_cnd.cn_pnbuf);
1285 		ndp->ni_cnd.cn_flags &= ~HASBUF;
1286 	}
1287 	if (!(flags & NDF_NO_VP_UNLOCK) &&
1288 	    (ndp->ni_cnd.cn_flags & LOCKLEAF) && ndp->ni_vp)
1289 		unlock_vp = 1;
1290 	if (!(flags & NDF_NO_VP_RELE) && ndp->ni_vp) {
1291 		if (unlock_vp) {
1292 			vput(ndp->ni_vp);
1293 			unlock_vp = 0;
1294 		} else
1295 			vrele(ndp->ni_vp);
1296 		ndp->ni_vp = NULL;
1297 	}
1298 	if (unlock_vp)
1299 		VOP_UNLOCK(ndp->ni_vp, 0);
1300 	if (!(flags & NDF_NO_DVP_UNLOCK) &&
1301 	    (ndp->ni_cnd.cn_flags & LOCKPARENT) &&
1302 	    ndp->ni_dvp != ndp->ni_vp)
1303 		unlock_dvp = 1;
1304 	if (!(flags & NDF_NO_DVP_RELE) &&
1305 	    (ndp->ni_cnd.cn_flags & (LOCKPARENT|WANTPARENT))) {
1306 		if (unlock_dvp) {
1307 			vput(ndp->ni_dvp);
1308 			unlock_dvp = 0;
1309 		} else
1310 			vrele(ndp->ni_dvp);
1311 		ndp->ni_dvp = NULL;
1312 	}
1313 	if (unlock_dvp)
1314 		VOP_UNLOCK(ndp->ni_dvp, 0);
1315 	if (!(flags & NDF_NO_STARTDIR_RELE) &&
1316 	    (ndp->ni_cnd.cn_flags & SAVESTART)) {
1317 		vrele(ndp->ni_startdir);
1318 		ndp->ni_startdir = NULL;
1319 	}
1320 }
1321 
1322 /*
1323  * Determine if there is a suitable alternate filename under the specified
1324  * prefix for the specified path.  If the create flag is set, then the
1325  * alternate prefix will be used so long as the parent directory exists.
1326  * This is used by the various compatibility ABIs so that Linux binaries prefer
1327  * files under /compat/linux for example.  The chosen path (whether under
1328  * the prefix or under /) is returned in a kernel malloc'd buffer pointed
1329  * to by pathbuf.  The caller is responsible for free'ing the buffer from
1330  * the M_TEMP bucket if one is returned.
1331  */
1332 int
1333 kern_alternate_path(struct thread *td, const char *prefix, const char *path,
1334     enum uio_seg pathseg, char **pathbuf, int create, int dirfd)
1335 {
1336 	struct nameidata nd, ndroot;
1337 	char *ptr, *buf, *cp;
1338 	size_t len, sz;
1339 	int error;
1340 
1341 	buf = (char *) malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1342 	*pathbuf = buf;
1343 
1344 	/* Copy the prefix into the new pathname as a starting point. */
1345 	len = strlcpy(buf, prefix, MAXPATHLEN);
1346 	if (len >= MAXPATHLEN) {
1347 		*pathbuf = NULL;
1348 		free(buf, M_TEMP);
1349 		return (EINVAL);
1350 	}
1351 	sz = MAXPATHLEN - len;
1352 	ptr = buf + len;
1353 
1354 	/* Append the filename to the prefix. */
1355 	if (pathseg == UIO_SYSSPACE)
1356 		error = copystr(path, ptr, sz, &len);
1357 	else
1358 		error = copyinstr(path, ptr, sz, &len);
1359 
1360 	if (error) {
1361 		*pathbuf = NULL;
1362 		free(buf, M_TEMP);
1363 		return (error);
1364 	}
1365 
1366 	/* Only use a prefix with absolute pathnames. */
1367 	if (*ptr != '/') {
1368 		error = EINVAL;
1369 		goto keeporig;
1370 	}
1371 
1372 	if (dirfd != AT_FDCWD) {
1373 		/*
1374 		 * We want the original because the "prefix" is
1375 		 * included in the already opened dirfd.
1376 		 */
1377 		bcopy(ptr, buf, len);
1378 		return (0);
1379 	}
1380 
1381 	/*
1382 	 * We know that there is a / somewhere in this pathname.
1383 	 * Search backwards for it, to find the file's parent dir
1384 	 * to see if it exists in the alternate tree. If it does,
1385 	 * and we want to create a file (cflag is set). We don't
1386 	 * need to worry about the root comparison in this case.
1387 	 */
1388 
1389 	if (create) {
1390 		for (cp = &ptr[len] - 1; *cp != '/'; cp--);
1391 		*cp = '\0';
1392 
1393 		NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, buf, td);
1394 		error = namei(&nd);
1395 		*cp = '/';
1396 		if (error != 0)
1397 			goto keeporig;
1398 	} else {
1399 		NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, buf, td);
1400 
1401 		error = namei(&nd);
1402 		if (error != 0)
1403 			goto keeporig;
1404 
1405 		/*
1406 		 * We now compare the vnode of the prefix to the one
1407 		 * vnode asked. If they resolve to be the same, then we
1408 		 * ignore the match so that the real root gets used.
1409 		 * This avoids the problem of traversing "../.." to find the
1410 		 * root directory and never finding it, because "/" resolves
1411 		 * to the emulation root directory. This is expensive :-(
1412 		 */
1413 		NDINIT(&ndroot, LOOKUP, FOLLOW, UIO_SYSSPACE, prefix,
1414 		    td);
1415 
1416 		/* We shouldn't ever get an error from this namei(). */
1417 		error = namei(&ndroot);
1418 		if (error == 0) {
1419 			if (nd.ni_vp == ndroot.ni_vp)
1420 				error = ENOENT;
1421 
1422 			NDFREE(&ndroot, NDF_ONLY_PNBUF);
1423 			vrele(ndroot.ni_vp);
1424 		}
1425 	}
1426 
1427 	NDFREE(&nd, NDF_ONLY_PNBUF);
1428 	vrele(nd.ni_vp);
1429 
1430 keeporig:
1431 	/* If there was an error, use the original path name. */
1432 	if (error)
1433 		bcopy(ptr, buf, len);
1434 	return (error);
1435 }
1436