2 * SPDX-License-Identifier: BSD-3-Clause
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.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
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.
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
36 * @(#)vfs_lookup.c 8.4 (Berkeley) 2/16/94
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
42 #include "opt_capsicum.h"
43 #include "opt_ktrace.h"
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>
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>
59 #include <sys/syscallsubr.h>
60 #include <sys/sysctl.h>
62 #include <sys/ktrace.h>
65 #include <machine/_inttypes.h>
68 #include <security/audit/audit.h>
69 #include <security/mac/mac_framework.h>
73 #define NAMEI_DIAGNOSTIC 1
74 #undef NAMEI_DIAGNOSTIC
76 SDT_PROVIDER_DEFINE(vfs);
77 SDT_PROBE_DEFINE4(vfs, namei, lookup, entry, "struct vnode *", "char *",
78 "unsigned long", "bool");
79 SDT_PROBE_DEFINE3(vfs, namei, lookup, return, "int", "struct vnode *", "bool");
81 /* Allocation zone for namei. */
82 uma_zone_t namei_zone;
84 /* Placeholder vnode for mp traversal. */
85 static struct vnode *vp_crossmp;
88 crossmp_vop_islocked(struct vop_islocked_args *ap)
95 crossmp_vop_lock1(struct vop_lock1_args *ap)
98 struct lock *lk __unused;
99 const char *file __unused;
100 int flags, line __unused;
108 if ((flags & LK_SHARED) == 0)
109 panic("invalid lock request for crossmp");
111 WITNESS_CHECKORDER(&lk->lock_object, LOP_NEWORDER, file, line,
112 flags & LK_INTERLOCK ? &VI_MTX(vp)->lock_object : NULL);
113 WITNESS_LOCK(&lk->lock_object, 0, file, line);
114 if ((flags & LK_INTERLOCK) != 0)
116 LOCK_LOG_LOCK("SLOCK", &lk->lock_object, 0, 0, ap->a_file, line);
121 crossmp_vop_unlock(struct vop_unlock_args *ap)
124 struct lock *lk __unused;
129 WITNESS_UNLOCK(&lk->lock_object, 0, LOCK_FILE, LOCK_LINE);
130 LOCK_LOG_LOCK("SUNLOCK", &lk->lock_object, 0, 0, LOCK_FILE,
135 static struct vop_vector crossmp_vnodeops = {
136 .vop_default = &default_vnodeops,
137 .vop_islocked = crossmp_vop_islocked,
138 .vop_lock1 = crossmp_vop_lock1,
139 .vop_unlock = crossmp_vop_unlock,
142 * VFS_VOP_VECTOR_REGISTER(crossmp_vnodeops) is not used here since the vnode
143 * gets allocated early. See nameiinit for the direct call below.
146 struct nameicap_tracker {
148 TAILQ_ENTRY(nameicap_tracker) nm_link;
151 /* Zone for cap mode tracker elements used for dotdot capability checks. */
152 MALLOC_DEFINE(M_NAMEITRACKER, "namei_tracker", "namei tracking for dotdot");
155 nameiinit(void *dummy __unused)
158 namei_zone = uma_zcreate("NAMEI", MAXPATHLEN, NULL, NULL, NULL, NULL,
160 vfs_vector_op_register(&crossmp_vnodeops);
161 getnewvnode("crossmp", NULL, &crossmp_vnodeops, &vp_crossmp);
163 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_SECOND, nameiinit, NULL);
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");
176 nameicap_tracker_add(struct nameidata *ndp, struct vnode *dp)
178 struct nameicap_tracker *nt;
179 struct componentname *cnp;
181 if ((ndp->ni_lcf & NI_LCF_CAP_DOTDOT) == 0 || dp->v_type != VDIR)
184 if ((cnp->cn_flags & BENEATH) != 0 &&
185 (ndp->ni_lcf & NI_LCF_BENEATH_LATCHED) == 0) {
186 MPASS((ndp->ni_lcf & NI_LCF_LATCH) != 0);
187 if (dp != ndp->ni_beneath_latch)
189 ndp->ni_lcf |= NI_LCF_BENEATH_LATCHED;
191 nt = malloc(sizeof(*nt), M_NAMEITRACKER, M_WAITOK);
194 TAILQ_INSERT_TAIL(&ndp->ni_cap_tracker, nt, nm_link);
198 nameicap_cleanup(struct nameidata *ndp, bool clean_latch)
200 struct nameicap_tracker *nt, *nt1;
202 KASSERT(TAILQ_EMPTY(&ndp->ni_cap_tracker) ||
203 (ndp->ni_lcf & NI_LCF_CAP_DOTDOT) != 0, ("not strictrelative"));
204 TAILQ_FOREACH_SAFE(nt, &ndp->ni_cap_tracker, nm_link, nt1) {
205 TAILQ_REMOVE(&ndp->ni_cap_tracker, nt, nm_link);
207 free(nt, M_NAMEITRACKER);
209 if (clean_latch && (ndp->ni_lcf & NI_LCF_LATCH) != 0) {
210 ndp->ni_lcf &= ~NI_LCF_LATCH;
211 vrele(ndp->ni_beneath_latch);
216 * For dotdot lookups in capability mode, only allow the component
217 * lookup to succeed if the resulting directory was already traversed
218 * during the operation. This catches situations where already
219 * traversed directory is moved to different parent, and then we walk
220 * over it with dotdots.
222 * Also allow to force failure of dotdot lookups for non-local
223 * filesystems, where external agents might assist local lookups to
224 * escape the compartment.
227 nameicap_check_dotdot(struct nameidata *ndp, struct vnode *dp)
229 struct nameicap_tracker *nt;
232 if ((ndp->ni_lcf & NI_LCF_CAP_DOTDOT) == 0 || dp == NULL ||
236 if (lookup_cap_dotdot_nonlocal == 0 && mp != NULL &&
237 (mp->mnt_flag & MNT_LOCAL) == 0)
238 return (ENOTCAPABLE);
239 TAILQ_FOREACH_REVERSE(nt, &ndp->ni_cap_tracker, nameicap_tracker_head,
241 if ((ndp->ni_lcf & NI_LCF_LATCH) != 0 &&
242 ndp->ni_beneath_latch == nt->dp) {
243 ndp->ni_lcf &= ~NI_LCF_BENEATH_LATCHED;
244 nameicap_cleanup(ndp, false);
250 return (ENOTCAPABLE);
254 namei_cleanup_cnp(struct componentname *cnp)
257 uma_zfree(namei_zone, cnp->cn_pnbuf);
259 cnp->cn_pnbuf = NULL;
260 cnp->cn_nameptr = NULL;
265 namei_handle_root(struct nameidata *ndp, struct vnode **dpp)
267 struct componentname *cnp;
270 if ((ndp->ni_lcf & NI_LCF_STRICTRELATIVE) != 0) {
272 if (KTRPOINT(curthread, KTR_CAPFAIL))
273 ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL);
275 return (ENOTCAPABLE);
277 if ((cnp->cn_flags & BENEATH) != 0) {
278 ndp->ni_lcf |= NI_LCF_BENEATH_ABS;
279 ndp->ni_lcf &= ~NI_LCF_BENEATH_LATCHED;
280 nameicap_cleanup(ndp, false);
282 while (*(cnp->cn_nameptr) == '/') {
286 *dpp = ndp->ni_rootdir;
292 namei_setup(struct nameidata *ndp, struct vnode **dpp, struct pwd **pwdp)
294 struct componentname *cnp;
299 struct filecaps dirfd_caps;
306 startdir_used = false;
310 #ifdef CAPABILITY_MODE
312 * In capability mode, lookups must be restricted to happen in
313 * the subtree with the root specified by the file descriptor:
314 * - The root must be real file descriptor, not the pseudo-descriptor
316 * - The passed path must be relative and not absolute.
317 * - If lookup_cap_dotdot is disabled, path must not contain the
319 * - If lookup_cap_dotdot is enabled, we verify that all '..'
320 * components lookups result in the directories which were
321 * previously walked by us, which prevents an escape from
324 if (IN_CAPABILITY_MODE(td) && (cnp->cn_flags & NOCAPCHECK) == 0) {
325 ndp->ni_lcf |= NI_LCF_STRICTRELATIVE;
326 ndp->ni_resflags |= NIRES_STRICTREL;
327 if (ndp->ni_dirfd == AT_FDCWD) {
329 if (KTRPOINT(td, KTR_CAPFAIL))
330 ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL);
339 * Get starting point for the translation.
343 * The reference on ni_rootdir is acquired in the block below to avoid
344 * back-to-back atomics for absolute lookups.
346 ndp->ni_rootdir = pwd->pwd_rdir;
347 ndp->ni_topdir = pwd->pwd_jdir;
349 if (cnp->cn_pnbuf[0] == '/') {
350 ndp->ni_resflags |= NIRES_ABS;
351 error = namei_handle_root(ndp, dpp);
353 if (ndp->ni_startdir != NULL) {
354 *dpp = ndp->ni_startdir;
355 startdir_used = true;
356 } else if (ndp->ni_dirfd == AT_FDCWD) {
357 *dpp = pwd->pwd_cdir;
360 rights = *ndp->ni_rightsneeded;
361 cap_rights_set_one(&rights, CAP_LOOKUP);
363 if (cnp->cn_flags & AUDITVNODE1)
364 AUDIT_ARG_ATFD1(ndp->ni_dirfd);
365 if (cnp->cn_flags & AUDITVNODE2)
366 AUDIT_ARG_ATFD2(ndp->ni_dirfd);
368 * Effectively inlined fgetvp_rights, because we need to
369 * inspect the file as well as grabbing the vnode.
371 error = fget_cap(td, ndp->ni_dirfd, &rights,
372 &dfp, &ndp->ni_filecaps);
375 * Preserve the error; it should either be EBADF
376 * or capability-related, both of which can be
377 * safely returned to the caller.
380 if (dfp->f_ops == &badfileops) {
382 } else if (dfp->f_vnode == NULL) {
388 if ((dfp->f_flag & FSEARCH) != 0)
389 cnp->cn_flags |= NOEXECCHECK;
395 * If file descriptor doesn't have all rights,
396 * all lookups relative to it must also be
400 if (!cap_rights_contains(&ndp->ni_filecaps.fc_rights,
402 ndp->ni_filecaps.fc_fcntls != CAP_FCNTL_ALL ||
403 ndp->ni_filecaps.fc_nioctls != -1) {
404 ndp->ni_lcf |= NI_LCF_STRICTRELATIVE;
405 ndp->ni_resflags |= NIRES_STRICTREL;
409 if (error == 0 && (*dpp)->v_type != VDIR)
412 if (error == 0 && (cnp->cn_flags & BENEATH) != 0) {
413 if (ndp->ni_dirfd == AT_FDCWD) {
414 ndp->ni_beneath_latch = pwd->pwd_cdir;
415 vrefact(ndp->ni_beneath_latch);
417 rights = *ndp->ni_rightsneeded;
418 cap_rights_set_one(&rights, CAP_LOOKUP);
419 error = fgetvp_rights(td, ndp->ni_dirfd, &rights,
420 &dirfd_caps, &ndp->ni_beneath_latch);
421 if (error == 0 && (*dpp)->v_type != VDIR) {
422 vrele(ndp->ni_beneath_latch);
427 ndp->ni_lcf |= NI_LCF_LATCH;
429 if (error == 0 && (cnp->cn_flags & RBENEATH) != 0) {
430 if (cnp->cn_pnbuf[0] == '/' ||
431 (ndp->ni_lcf & NI_LCF_BENEATH_ABS) != 0) {
433 } else if ((ndp->ni_lcf & NI_LCF_STRICTRELATIVE) == 0) {
434 ndp->ni_lcf |= NI_LCF_STRICTRELATIVE |
440 * If we are auditing the kernel pathname, save the user pathname.
442 if (cnp->cn_flags & AUDITVNODE1)
443 AUDIT_ARG_UPATH1_VP(td, ndp->ni_rootdir, *dpp, cnp->cn_pnbuf);
444 if (cnp->cn_flags & AUDITVNODE2)
445 AUDIT_ARG_UPATH2_VP(td, ndp->ni_rootdir, *dpp, cnp->cn_pnbuf);
446 if (ndp->ni_startdir != NULL && !startdir_used)
447 vrele(ndp->ni_startdir);
454 MPASS((ndp->ni_lcf & (NI_LCF_BENEATH_ABS | NI_LCF_LATCH)) !=
456 if (((ndp->ni_lcf & NI_LCF_STRICTRELATIVE) != 0 &&
457 lookup_cap_dotdot != 0) ||
458 ((ndp->ni_lcf & NI_LCF_STRICTRELATIVE) == 0 &&
459 (cnp->cn_flags & BENEATH) != 0))
460 ndp->ni_lcf |= NI_LCF_CAP_DOTDOT;
461 SDT_PROBE4(vfs, namei, lookup, entry, *dpp, cnp->cn_pnbuf,
462 cnp->cn_flags, false);
468 namei_getpath(struct nameidata *ndp)
470 struct componentname *cnp;
476 * Get a buffer for the name to be translated, and copy the
477 * name into the buffer.
479 cnp->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK);
480 if (ndp->ni_segflg == UIO_SYSSPACE) {
481 error = copystr(ndp->ni_dirp, cnp->cn_pnbuf, MAXPATHLEN,
484 error = copyinstr(ndp->ni_dirp, cnp->cn_pnbuf, MAXPATHLEN,
488 if (__predict_false(error != 0)) {
489 namei_cleanup_cnp(cnp);
494 * Don't allow empty pathnames.
496 if (__predict_false(*cnp->cn_pnbuf == '\0')) {
497 namei_cleanup_cnp(cnp);
501 cnp->cn_nameptr = cnp->cn_pnbuf;
506 * Convert a pathname into a pointer to a locked vnode.
508 * The FOLLOW flag is set when symbolic links are to be followed
509 * when they occur at the end of the name translation process.
510 * Symbolic links are always followed for all other pathname
511 * components other than the last.
513 * The segflg defines whether the name is to be copied from user
514 * space or kernel space.
516 * Overall outline of namei:
519 * get starting directory
520 * while (!done && !error) {
521 * call lookup to search path.
522 * if symbolic link, massage name in buffer and continue
526 namei(struct nameidata *ndp)
528 char *cp; /* pointer into pathname argument */
529 struct vnode *dp; /* the directory we are searching */
530 struct iovec aiov; /* uio for reading symbolic links */
531 struct componentname *cnp;
536 enum cache_fpl_status status;
541 KASSERT((ndp->ni_debugflags & NAMEI_DBG_CALLED) == 0,
542 ("%s: repeated call to namei without NDREINIT", __func__));
543 KASSERT(ndp->ni_debugflags == NAMEI_DBG_INITED,
544 ("%s: bad debugflags %d", __func__, ndp->ni_debugflags));
545 ndp->ni_debugflags |= NAMEI_DBG_CALLED;
546 if (ndp->ni_startdir != NULL)
547 ndp->ni_debugflags |= NAMEI_DBG_HADSTARTDIR;
551 * While NDINIT may seem like a more natural place to do it, there are
552 * callers which directly modify flags past invoking init.
554 cnp->cn_origflags = cnp->cn_flags;
556 ndp->ni_cnd.cn_cred = ndp->ni_cnd.cn_thread->td_ucred;
557 KASSERT(ndp->ni_resflags == 0, ("%s: garbage in ni_resflags: %x\n",
558 __func__, ndp->ni_resflags));
559 KASSERT(cnp->cn_cred && td->td_proc, ("namei: bad cred/proc"));
560 KASSERT((cnp->cn_flags & NAMEI_INTERNAL_FLAGS) == 0,
561 ("namei: unexpected flags: %" PRIx64 "\n",
562 cnp->cn_flags & NAMEI_INTERNAL_FLAGS));
563 if (cnp->cn_flags & NOCACHE)
564 KASSERT(cnp->cn_nameiop != LOOKUP,
565 ("%s: NOCACHE passed with LOOKUP", __func__));
566 MPASS(ndp->ni_startdir == NULL || ndp->ni_startdir->v_type == VDIR ||
567 ndp->ni_startdir->v_type == VBAD);
572 error = namei_getpath(ndp);
573 if (__predict_false(error != 0)) {
578 if (KTRPOINT(td, KTR_NAMEI)) {
579 KASSERT(cnp->cn_thread == curthread,
580 ("namei not using curthread"));
581 ktrnamei(cnp->cn_pnbuf);
586 * First try looking up the target without locking any vnodes.
588 * We may need to start from scratch or pick up where it left off.
590 error = cache_fplookup(ndp, &status, &pwd);
592 case CACHE_FPL_STATUS_UNSET:
593 __assert_unreachable();
595 case CACHE_FPL_STATUS_HANDLED:
599 case CACHE_FPL_STATUS_PARTIAL:
600 TAILQ_INIT(&ndp->ni_cap_tracker);
601 dp = ndp->ni_startdir;
603 case CACHE_FPL_STATUS_ABORTED:
604 TAILQ_INIT(&ndp->ni_cap_tracker);
605 error = namei_setup(ndp, &dp, &pwd);
607 namei_cleanup_cnp(cnp);
619 ndp->ni_startdir = dp;
623 * Override an error to not allow user to use
624 * BENEATH as an oracle.
626 if ((ndp->ni_lcf & (NI_LCF_LATCH |
627 NI_LCF_BENEATH_LATCHED)) == NI_LCF_LATCH)
633 * If not a symbolic link, we're done.
635 if ((cnp->cn_flags & ISSYMLINK) == 0) {
636 if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0) {
637 namei_cleanup_cnp(cnp);
639 cnp->cn_flags |= HASBUF;
640 if ((ndp->ni_lcf & (NI_LCF_LATCH |
641 NI_LCF_BENEATH_LATCHED)) == NI_LCF_LATCH) {
645 nameicap_cleanup(ndp, true);
646 SDT_PROBE3(vfs, namei, lookup, return, error,
647 (error == 0 ? ndp->ni_vp : NULL), false);
653 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
658 if ((cnp->cn_flags & NOMACCHECK) == 0) {
659 error = mac_vnode_check_readlink(td->td_ucred,
665 if (ndp->ni_pathlen > 1)
666 cp = uma_zalloc(namei_zone, M_WAITOK);
670 aiov.iov_len = MAXPATHLEN;
671 auio.uio_iov = &aiov;
674 auio.uio_rw = UIO_READ;
675 auio.uio_segflg = UIO_SYSSPACE;
677 auio.uio_resid = MAXPATHLEN;
678 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
680 if (ndp->ni_pathlen > 1)
681 uma_zfree(namei_zone, cp);
684 linklen = MAXPATHLEN - auio.uio_resid;
686 if (ndp->ni_pathlen > 1)
687 uma_zfree(namei_zone, cp);
691 if (linklen + ndp->ni_pathlen > MAXPATHLEN) {
692 if (ndp->ni_pathlen > 1)
693 uma_zfree(namei_zone, cp);
694 error = ENAMETOOLONG;
697 if (ndp->ni_pathlen > 1) {
698 bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
699 uma_zfree(namei_zone, cnp->cn_pnbuf);
702 cnp->cn_pnbuf[linklen] = '\0';
703 ndp->ni_pathlen += linklen;
707 * Check if root directory should replace current directory.
709 cnp->cn_nameptr = cnp->cn_pnbuf;
710 if (*(cnp->cn_nameptr) == '/') {
712 error = namei_handle_root(ndp, &dp);
722 namei_cleanup_cnp(cnp);
723 nameicap_cleanup(ndp, true);
724 SDT_PROBE3(vfs, namei, lookup, return, error, NULL, false);
730 compute_cn_lkflags(struct mount *mp, int lkflags, int cnflags)
733 if (mp == NULL || ((lkflags & LK_SHARED) &&
734 (!(mp->mnt_kern_flag & MNTK_LOOKUP_SHARED) ||
735 ((cnflags & ISDOTDOT) &&
736 (mp->mnt_kern_flag & MNTK_LOOKUP_EXCL_DOTDOT))))) {
737 lkflags &= ~LK_SHARED;
738 lkflags |= LK_EXCLUSIVE;
740 lkflags |= LK_NODDLKTREAT;
745 needs_exclusive_leaf(struct mount *mp, int flags)
749 * Intermediate nodes can use shared locks, we only need to
750 * force an exclusive lock for leaf nodes.
752 if ((flags & (ISLASTCN | LOCKLEAF)) != (ISLASTCN | LOCKLEAF))
755 /* Always use exclusive locks if LOCKSHARED isn't set. */
756 if (!(flags & LOCKSHARED))
760 * For lookups during open(), if the mount point supports
761 * extended shared operations, then use a shared lock for the
762 * leaf node, otherwise use an exclusive lock.
764 if ((flags & ISOPEN) != 0)
765 return (!MNT_EXTENDED_SHARED(mp));
768 * Lookup requests outside of open() that specify LOCKSHARED
769 * only need a shared lock on the leaf vnode.
776 * This is a very central and rather complicated routine.
778 * The pathname is pointed to by ni_ptr and is of length ni_pathlen.
779 * The starting directory is taken from ni_startdir. The pathname is
780 * descended until done, or a symbolic link is encountered. The variable
781 * ni_more is clear if the path is completed; it is set to one if a
782 * symbolic link needing interpretation is encountered.
784 * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
785 * whether the name is to be looked up, created, renamed, or deleted.
786 * When CREATE, RENAME, or DELETE is specified, information usable in
787 * creating, renaming, or deleting a directory entry may be calculated.
788 * If flag has LOCKPARENT or'ed into it, the parent directory is returned
789 * locked. If flag has WANTPARENT or'ed into it, the parent directory is
790 * returned unlocked. Otherwise the parent directory is not returned. If
791 * the target of the pathname exists and LOCKLEAF is or'ed into the flag
792 * the target is returned locked, otherwise it is returned unlocked.
793 * When creating or renaming and LOCKPARENT is specified, the target may not
794 * be ".". When deleting and LOCKPARENT is specified, the target may be ".".
796 * Overall outline of lookup:
799 * identify next component of name at ndp->ni_ptr
800 * handle degenerate case where name is null string
801 * if .. and crossing mount points and on mounted filesys, find parent
802 * call VOP_LOOKUP routine for next component name
803 * directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set
804 * component vnode returned in ni_vp (if it exists), locked.
805 * if result vnode is mounted on and crossing mount points,
806 * find mounted on vnode
807 * if more components of name, do next level at dirloop
808 * return the answer in ni_vp, locked if LOCKLEAF set
809 * if LOCKPARENT set, return locked parent in ni_dvp
810 * if WANTPARENT set, return unlocked parent in ni_dvp
813 lookup(struct nameidata *ndp)
815 char *cp; /* pointer into pathname argument */
816 char *prev_ni_next; /* saved ndp->ni_next */
817 struct vnode *dp = NULL; /* the directory we are searching */
818 struct vnode *tdp; /* saved dp */
819 struct mount *mp; /* mount table entry */
821 size_t prev_ni_pathlen; /* saved ndp->ni_pathlen */
822 int docache; /* == 0 do not cache last component */
823 int wantparent; /* 1 => wantparent or lockparent flag */
824 int rdonly; /* lookup read-only flag bit */
826 int dpunlocked = 0; /* dp has already been unlocked */
827 int relookup = 0; /* do not consume the path component */
828 struct componentname *cnp = &ndp->ni_cnd;
833 * Setup: break out flag bits into variables.
836 wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT);
837 KASSERT(cnp->cn_nameiop == LOOKUP || wantparent,
838 ("CREATE, DELETE, RENAME require LOCKPARENT or WANTPARENT."));
840 * When set to zero, docache causes the last component of the
841 * pathname to be deleted from the cache and the full lookup
842 * of the name to be done (via VOP_CACHEDLOOKUP()). Often
843 * filesystems need some pre-computed values that are made
844 * during the full lookup, for instance UFS sets dp->i_offset.
846 * The docache variable is set to zero when requested by the
847 * NOCACHE flag and for all modifying operations except CREATE.
849 docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE;
850 if (cnp->cn_nameiop == DELETE ||
851 (wantparent && cnp->cn_nameiop != CREATE &&
852 cnp->cn_nameiop != LOOKUP))
854 rdonly = cnp->cn_flags & RDONLY;
855 cnp->cn_flags &= ~ISSYMLINK;
858 * We use shared locks until we hit the parent of the last cn then
859 * we adjust based on the requesting flags.
861 cnp->cn_lkflags = LK_SHARED;
862 dp = ndp->ni_startdir;
863 ndp->ni_startdir = NULLVP;
865 compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags | LK_RETRY,
870 * Search a new directory.
872 * The last component of the filename is left accessible via
873 * cnp->cn_nameptr for callers that need the name. Callers needing
874 * the name set the SAVENAME flag. When done, they assume
875 * responsibility for freeing the pathname buffer.
877 for (cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++)
879 cnp->cn_namelen = cp - cnp->cn_nameptr;
880 if (cnp->cn_namelen > NAME_MAX) {
881 error = ENAMETOOLONG;
884 #ifdef NAMEI_DIAGNOSTIC
887 printf("{%s}: ", cnp->cn_nameptr);
890 prev_ni_pathlen = ndp->ni_pathlen;
891 ndp->ni_pathlen -= cnp->cn_namelen;
892 KASSERT(ndp->ni_pathlen <= PATH_MAX,
893 ("%s: ni_pathlen underflow to %zd\n", __func__, ndp->ni_pathlen));
894 prev_ni_next = ndp->ni_next;
898 * Replace multiple slashes by a single slash and trailing slashes
899 * by a null. This must be done before VOP_LOOKUP() because some
900 * fs's don't know about trailing slashes. Remember if there were
901 * trailing slashes to handle symlinks, existing non-directories
902 * and non-existing files that won't be directories specially later.
904 while (*cp == '/' && (cp[1] == '/' || cp[1] == '\0')) {
908 *ndp->ni_next = '\0';
909 cnp->cn_flags |= TRAILINGSLASH;
914 cnp->cn_flags |= MAKEENTRY;
915 if (*cp == '\0' && docache == 0)
916 cnp->cn_flags &= ~MAKEENTRY;
917 if (cnp->cn_namelen == 2 &&
918 cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.')
919 cnp->cn_flags |= ISDOTDOT;
921 cnp->cn_flags &= ~ISDOTDOT;
922 if (*ndp->ni_next == 0)
923 cnp->cn_flags |= ISLASTCN;
925 cnp->cn_flags &= ~ISLASTCN;
927 if ((cnp->cn_flags & ISLASTCN) != 0 &&
928 cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.' &&
929 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
934 nameicap_tracker_add(ndp, dp);
937 * Check for degenerate name (e.g. / or "")
938 * which is a way of talking about a directory,
939 * e.g. like "/." or ".".
941 if (cnp->cn_nameptr[0] == '\0') {
942 if (dp->v_type != VDIR) {
946 if (cnp->cn_nameiop != LOOKUP) {
956 if (cnp->cn_flags & AUDITVNODE1)
957 AUDIT_ARG_VNODE1(dp);
958 else if (cnp->cn_flags & AUDITVNODE2)
959 AUDIT_ARG_VNODE2(dp);
961 if (!(cnp->cn_flags & (LOCKPARENT | LOCKLEAF)))
963 /* XXX This should probably move to the top of function. */
964 if (cnp->cn_flags & SAVESTART)
965 panic("lookup: SAVESTART");
970 * Handle "..": five special cases.
971 * 0. If doing a capability lookup and lookup_cap_dotdot is
972 * disabled, return ENOTCAPABLE.
973 * 1. Return an error if this is the last component of
974 * the name and the operation is DELETE or RENAME.
975 * 2. If at root directory (e.g. after chroot)
976 * or at absolute root directory
977 * then ignore it so can't get out.
978 * 3. If this vnode is the root of a mounted
979 * filesystem, then replace it with the
980 * vnode which was mounted on so we take the
981 * .. in the other filesystem.
982 * 4. If the vnode is the top directory of
983 * the jail or chroot, don't let them out.
984 * 5. If doing a capability lookup and lookup_cap_dotdot is
985 * enabled, return ENOTCAPABLE if the lookup would escape
986 * from the initial file descriptor directory. Checks are
987 * done by ensuring that namei() already traversed the
988 * result of dotdot lookup.
990 if (cnp->cn_flags & ISDOTDOT) {
991 if ((ndp->ni_lcf & (NI_LCF_STRICTRELATIVE | NI_LCF_CAP_DOTDOT))
992 == NI_LCF_STRICTRELATIVE) {
994 if (KTRPOINT(curthread, KTR_CAPFAIL))
995 ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL);
1000 if ((cnp->cn_flags & ISLASTCN) != 0 &&
1001 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
1006 for (pr = cnp->cn_cred->cr_prison; pr != NULL;
1008 if (dp == pr->pr_root)
1010 if (dp == ndp->ni_rootdir ||
1011 dp == ndp->ni_topdir ||
1014 ((dp->v_vflag & VV_ROOT) != 0 &&
1015 (cnp->cn_flags & NOCROSSMOUNT) != 0)) {
1021 if ((dp->v_vflag & VV_ROOT) == 0)
1023 if (VN_IS_DOOMED(dp)) { /* forced unmount */
1028 dp = dp->v_mount->mnt_vnodecovered;
1032 compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags |
1033 LK_RETRY, ISDOTDOT));
1034 error = nameicap_check_dotdot(ndp, dp);
1037 if (KTRPOINT(curthread, KTR_CAPFAIL))
1038 ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL);
1046 * We now have a segment name to search for, and a directory to search.
1050 error = mac_vnode_check_lookup(cnp->cn_thread->td_ucred, dp, cnp);
1056 ASSERT_VOP_LOCKED(dp, "lookup");
1058 * If we have a shared lock we may need to upgrade the lock for the
1061 if ((cnp->cn_flags & LOCKPARENT) && (cnp->cn_flags & ISLASTCN) &&
1062 dp != vp_crossmp && VOP_ISLOCKED(dp) == LK_SHARED)
1063 vn_lock(dp, LK_UPGRADE|LK_RETRY);
1064 if (VN_IS_DOOMED(dp)) {
1069 * If we're looking up the last component and we need an exclusive
1070 * lock, adjust our lkflags.
1072 if (needs_exclusive_leaf(dp->v_mount, cnp->cn_flags))
1073 cnp->cn_lkflags = LK_EXCLUSIVE;
1074 #ifdef NAMEI_DIAGNOSTIC
1075 vn_printf(dp, "lookup in ");
1077 lkflags_save = cnp->cn_lkflags;
1078 cnp->cn_lkflags = compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags,
1080 error = VOP_LOOKUP(dp, &ndp->ni_vp, cnp);
1081 cnp->cn_lkflags = lkflags_save;
1083 KASSERT(ndp->ni_vp == NULL, ("leaf should be empty"));
1084 #ifdef NAMEI_DIAGNOSTIC
1085 printf("not found\n");
1087 if ((error == ENOENT) &&
1088 (dp->v_vflag & VV_ROOT) && (dp->v_mount != NULL) &&
1089 (dp->v_mount->mnt_flag & MNT_UNION)) {
1091 dp = dp->v_mount->mnt_vnodecovered;
1095 compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags |
1096 LK_RETRY, cnp->cn_flags));
1097 nameicap_tracker_add(ndp, dp);
1101 if (error == ERELOOKUP) {
1109 if (error != EJUSTRETURN)
1112 * At this point, we know we're at the end of the
1113 * pathname. If creating / renaming, we can consider
1114 * allowing the file or directory to be created / renamed,
1115 * provided we're not on a read-only filesystem.
1121 /* trailing slash only allowed for directories */
1122 if ((cnp->cn_flags & TRAILINGSLASH) &&
1123 !(cnp->cn_flags & WILLBEDIR)) {
1127 if ((cnp->cn_flags & LOCKPARENT) == 0)
1130 * We return with ni_vp NULL to indicate that the entry
1131 * doesn't currently exist, leaving a pointer to the
1132 * (possibly locked) directory vnode in ndp->ni_dvp.
1134 if (cnp->cn_flags & SAVESTART) {
1135 ndp->ni_startdir = ndp->ni_dvp;
1136 VREF(ndp->ni_startdir);
1142 #ifdef NAMEI_DIAGNOSTIC
1148 * Check to see if the vnode has been mounted on;
1149 * if so find the root of the mounted filesystem.
1151 while (dp->v_type == VDIR && (mp = dp->v_mountedhere) &&
1152 (cnp->cn_flags & NOCROSSMOUNT) == 0) {
1153 if (vfs_busy(mp, 0))
1156 if (dp != ndp->ni_dvp)
1160 vrefact(vp_crossmp);
1161 ndp->ni_dvp = vp_crossmp;
1162 error = VFS_ROOT(mp, compute_cn_lkflags(mp, cnp->cn_lkflags,
1163 cnp->cn_flags), &tdp);
1165 if (vn_lock(vp_crossmp, LK_SHARED | LK_NOWAIT))
1166 panic("vp_crossmp exclusively locked or reclaimed");
1171 ndp->ni_vp = dp = tdp;
1175 * Check for symbolic link
1177 if ((dp->v_type == VLNK) &&
1178 ((cnp->cn_flags & FOLLOW) || (cnp->cn_flags & TRAILINGSLASH) ||
1179 *ndp->ni_next == '/')) {
1180 cnp->cn_flags |= ISSYMLINK;
1181 if (VN_IS_DOOMED(dp)) {
1183 * We can't know whether the directory was mounted with
1184 * NOSYMFOLLOW, so we can't follow safely.
1189 if (dp->v_mount->mnt_flag & MNT_NOSYMFOLLOW) {
1194 * Symlink code always expects an unlocked dvp.
1196 if (ndp->ni_dvp != ndp->ni_vp) {
1197 VOP_UNLOCK(ndp->ni_dvp);
1198 ni_dvp_unlocked = 1;
1205 * Not a symbolic link that we will follow. Continue with the
1206 * next component if there is any; otherwise, we're done.
1208 KASSERT((cnp->cn_flags & ISLASTCN) || *ndp->ni_next == '/',
1209 ("lookup: invalid path state."));
1212 ndp->ni_pathlen = prev_ni_pathlen;
1213 ndp->ni_next = prev_ni_next;
1214 if (ndp->ni_dvp != dp)
1220 if (cnp->cn_flags & ISDOTDOT) {
1221 error = nameicap_check_dotdot(ndp, ndp->ni_vp);
1224 if (KTRPOINT(curthread, KTR_CAPFAIL))
1225 ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL);
1230 if (*ndp->ni_next == '/') {
1231 cnp->cn_nameptr = ndp->ni_next;
1232 while (*cnp->cn_nameptr == '/') {
1236 if (ndp->ni_dvp != dp)
1243 * If we're processing a path with a trailing slash,
1244 * check that the end result is a directory.
1246 if ((cnp->cn_flags & TRAILINGSLASH) && dp->v_type != VDIR) {
1251 * Disallow directory write attempts on read-only filesystems.
1254 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
1258 if (cnp->cn_flags & SAVESTART) {
1259 ndp->ni_startdir = ndp->ni_dvp;
1260 VREF(ndp->ni_startdir);
1263 ni_dvp_unlocked = 2;
1264 if (ndp->ni_dvp != dp)
1268 } else if ((cnp->cn_flags & LOCKPARENT) == 0 && ndp->ni_dvp != dp) {
1269 VOP_UNLOCK(ndp->ni_dvp);
1270 ni_dvp_unlocked = 1;
1273 if (cnp->cn_flags & AUDITVNODE1)
1274 AUDIT_ARG_VNODE1(dp);
1275 else if (cnp->cn_flags & AUDITVNODE2)
1276 AUDIT_ARG_VNODE2(dp);
1278 if ((cnp->cn_flags & LOCKLEAF) == 0)
1282 * FIXME: for lookups which only cross a mount point to fetch the
1283 * root vnode, ni_dvp will be set to vp_crossmp. This can be a problem
1284 * if either WANTPARENT or LOCKPARENT is set.
1287 * Because of shared lookup we may have the vnode shared locked, but
1288 * the caller may want it to be exclusively locked.
1290 if (needs_exclusive_leaf(dp->v_mount, cnp->cn_flags) &&
1291 VOP_ISLOCKED(dp) != LK_EXCLUSIVE) {
1292 vn_lock(dp, LK_UPGRADE | LK_RETRY);
1293 if (VN_IS_DOOMED(dp)) {
1298 if (ndp->ni_vp != NULL && ndp->ni_vp->v_type == VDIR)
1299 nameicap_tracker_add(ndp, ndp->ni_vp);
1303 if (ni_dvp_unlocked != 2) {
1304 if (dp != ndp->ni_dvp && !ni_dvp_unlocked)
1317 * relookup - lookup a path name component
1318 * Used by lookup to re-acquire things.
1321 relookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp)
1323 struct vnode *dp = NULL; /* the directory we are searching */
1324 int rdonly; /* lookup read-only flag bit */
1327 KASSERT(cnp->cn_flags & ISLASTCN,
1328 ("relookup: Not given last component."));
1330 * Setup: break out flag bits into variables.
1332 KASSERT((cnp->cn_flags & (LOCKPARENT | WANTPARENT)) != 0,
1333 ("relookup: parent not wanted"));
1334 rdonly = cnp->cn_flags & RDONLY;
1335 cnp->cn_flags &= ~ISSYMLINK;
1337 cnp->cn_lkflags = LK_EXCLUSIVE;
1338 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
1341 * Search a new directory.
1343 * The last component of the filename is left accessible via
1344 * cnp->cn_nameptr for callers that need the name. Callers needing
1345 * the name set the SAVENAME flag. When done, they assume
1346 * responsibility for freeing the pathname buffer.
1348 #ifdef NAMEI_DIAGNOSTIC
1349 printf("{%s}: ", cnp->cn_nameptr);
1353 * Check for "" which represents the root directory after slash
1356 if (cnp->cn_nameptr[0] == '\0') {
1358 * Support only LOOKUP for "/" because lookup()
1359 * can't succeed for CREATE, DELETE and RENAME.
1361 KASSERT(cnp->cn_nameiop == LOOKUP, ("nameiop must be LOOKUP"));
1362 KASSERT(dp->v_type == VDIR, ("dp is not a directory"));
1364 if (!(cnp->cn_flags & LOCKLEAF))
1367 /* XXX This should probably move to the top of function. */
1368 if (cnp->cn_flags & SAVESTART)
1369 panic("lookup: SAVESTART");
1373 if (cnp->cn_flags & ISDOTDOT)
1374 panic ("relookup: lookup on dot-dot");
1377 * We now have a segment name to search for, and a directory to search.
1379 #ifdef NAMEI_DIAGNOSTIC
1380 vn_printf(dp, "search in ");
1382 if ((error = VOP_LOOKUP(dp, vpp, cnp)) != 0) {
1383 KASSERT(*vpp == NULL, ("leaf should be empty"));
1384 if (error != EJUSTRETURN)
1387 * If creating and at end of pathname, then can consider
1388 * allowing file to be created.
1394 /* ASSERT(dvp == ndp->ni_startdir) */
1395 if (cnp->cn_flags & SAVESTART)
1397 if ((cnp->cn_flags & LOCKPARENT) == 0)
1400 * We return with ni_vp NULL to indicate that the entry
1401 * doesn't currently exist, leaving a pointer to the
1402 * (possibly locked) directory vnode in ndp->ni_dvp.
1410 * Disallow directory write attempts on read-only filesystems.
1413 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
1422 * Set the parent lock/ref state to the requested state.
1424 if ((cnp->cn_flags & LOCKPARENT) == 0 && dvp != dp)
1427 * Check for symbolic link
1429 KASSERT(dp->v_type != VLNK || !(cnp->cn_flags & FOLLOW),
1430 ("relookup: symlink found.\n"));
1432 /* ASSERT(dvp == ndp->ni_startdir) */
1433 if (cnp->cn_flags & SAVESTART)
1436 if ((cnp->cn_flags & LOCKLEAF) == 0)
1446 * Free data allocated by namei(); see namei(9) for details.
1449 NDFREE_PNBUF(struct nameidata *ndp)
1452 if ((ndp->ni_cnd.cn_flags & HASBUF) != 0) {
1453 MPASS((ndp->ni_cnd.cn_flags & (SAVENAME | SAVESTART)) != 0);
1454 uma_zfree(namei_zone, ndp->ni_cnd.cn_pnbuf);
1455 ndp->ni_cnd.cn_flags &= ~HASBUF;
1460 (NDFREE)(struct nameidata *ndp, const u_int flags)
1468 if (!(flags & NDF_NO_FREE_PNBUF)) {
1471 if (!(flags & NDF_NO_VP_UNLOCK) &&
1472 (ndp->ni_cnd.cn_flags & LOCKLEAF) && ndp->ni_vp)
1474 if (!(flags & NDF_NO_DVP_UNLOCK) &&
1475 (ndp->ni_cnd.cn_flags & LOCKPARENT) &&
1476 ndp->ni_dvp != ndp->ni_vp)
1478 if (!(flags & NDF_NO_VP_RELE) && ndp->ni_vp) {
1487 VOP_UNLOCK(ndp->ni_vp);
1488 if (!(flags & NDF_NO_DVP_RELE) &&
1489 (ndp->ni_cnd.cn_flags & (LOCKPARENT|WANTPARENT))) {
1498 VOP_UNLOCK(ndp->ni_dvp);
1499 if (!(flags & NDF_NO_STARTDIR_RELE) &&
1500 (ndp->ni_cnd.cn_flags & SAVESTART)) {
1501 vrele(ndp->ni_startdir);
1502 ndp->ni_startdir = NULL;
1508 * Validate the final state of ndp after the lookup.
1510 * Historically filesystems were allowed to modify cn_flags. Most notably they
1511 * can add SAVENAME to the request, resulting in HASBUF and pushing subsequent
1512 * clean up to the consumer. In practice this seems to only concern != LOOKUP
1515 * As a step towards stricter API contract this routine validates the state to
1516 * clean up. Note validation is a work in progress with the intent of becoming
1517 * stricter over time.
1519 #define NDMODIFYINGFLAGS (LOCKLEAF | LOCKPARENT | WANTPARENT | SAVENAME | SAVESTART | HASBUF)
1521 NDVALIDATE(struct nameidata *ndp)
1523 struct componentname *cnp;
1524 u_int64_t used, orig;
1527 orig = cnp->cn_origflags;
1528 used = cnp->cn_flags;
1529 switch (cnp->cn_nameiop) {
1532 * For plain lookup we require strict conformance -- nothing
1533 * to clean up if it was not requested by the caller.
1535 orig &= NDMODIFYINGFLAGS;
1536 used &= NDMODIFYINGFLAGS;
1537 if ((orig & (SAVENAME | SAVESTART)) != 0)
1547 * Some filesystems set SAVENAME to provoke HASBUF, accomodate
1548 * for it until it gets fixed.
1550 orig &= NDMODIFYINGFLAGS;
1551 orig |= (SAVENAME | HASBUF);
1552 used &= NDMODIFYINGFLAGS;
1553 used |= (SAVENAME | HASBUF);
1561 panic("%s: mismatched flags for op %d: added %" PRIx64 ", "
1562 "removed %" PRIx64" (%" PRIx64" != %" PRIx64"; stored %" PRIx64" != %" PRIx64")",
1563 __func__, cnp->cn_nameiop, used & ~orig, orig &~ used,
1564 orig, used, cnp->cn_origflags, cnp->cn_flags);
1569 * Determine if there is a suitable alternate filename under the specified
1570 * prefix for the specified path. If the create flag is set, then the
1571 * alternate prefix will be used so long as the parent directory exists.
1572 * This is used by the various compatibility ABIs so that Linux binaries prefer
1573 * files under /compat/linux for example. The chosen path (whether under
1574 * the prefix or under /) is returned in a kernel malloc'd buffer pointed
1575 * to by pathbuf. The caller is responsible for free'ing the buffer from
1576 * the M_TEMP bucket if one is returned.
1579 kern_alternate_path(struct thread *td, const char *prefix, const char *path,
1580 enum uio_seg pathseg, char **pathbuf, int create, int dirfd)
1582 struct nameidata nd, ndroot;
1583 char *ptr, *buf, *cp;
1587 buf = (char *) malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1590 /* Copy the prefix into the new pathname as a starting point. */
1591 len = strlcpy(buf, prefix, MAXPATHLEN);
1592 if (len >= MAXPATHLEN) {
1597 sz = MAXPATHLEN - len;
1600 /* Append the filename to the prefix. */
1601 if (pathseg == UIO_SYSSPACE)
1602 error = copystr(path, ptr, sz, &len);
1604 error = copyinstr(path, ptr, sz, &len);
1612 /* Only use a prefix with absolute pathnames. */
1618 if (dirfd != AT_FDCWD) {
1620 * We want the original because the "prefix" is
1621 * included in the already opened dirfd.
1623 bcopy(ptr, buf, len);
1628 * We know that there is a / somewhere in this pathname.
1629 * Search backwards for it, to find the file's parent dir
1630 * to see if it exists in the alternate tree. If it does,
1631 * and we want to create a file (cflag is set). We don't
1632 * need to worry about the root comparison in this case.
1636 for (cp = &ptr[len] - 1; *cp != '/'; cp--);
1639 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, buf, td);
1645 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, buf, td);
1652 * We now compare the vnode of the prefix to the one
1653 * vnode asked. If they resolve to be the same, then we
1654 * ignore the match so that the real root gets used.
1655 * This avoids the problem of traversing "../.." to find the
1656 * root directory and never finding it, because "/" resolves
1657 * to the emulation root directory. This is expensive :-(
1659 NDINIT(&ndroot, LOOKUP, FOLLOW, UIO_SYSSPACE, prefix,
1662 /* We shouldn't ever get an error from this namei(). */
1663 error = namei(&ndroot);
1665 if (nd.ni_vp == ndroot.ni_vp)
1668 NDFREE(&ndroot, NDF_ONLY_PNBUF);
1669 vrele(ndroot.ni_vp);
1673 NDFREE(&nd, NDF_ONLY_PNBUF);
1677 /* If there was an error, use the original path name. */
1679 bcopy(ptr, buf, len);