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 static uma_zone_t nt_zone;
155 nameiinit(void *dummy __unused)
158 namei_zone = uma_zcreate("NAMEI", MAXPATHLEN, NULL, NULL, NULL, NULL,
160 nt_zone = uma_zcreate("rentr", sizeof(struct nameicap_tracker),
161 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
162 vfs_vector_op_register(&crossmp_vnodeops);
163 getnewvnode("crossmp", NULL, &crossmp_vnodeops, &vp_crossmp);
165 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_SECOND, nameiinit, NULL);
167 static int lookup_cap_dotdot = 1;
168 SYSCTL_INT(_vfs, OID_AUTO, lookup_cap_dotdot, CTLFLAG_RWTUN,
169 &lookup_cap_dotdot, 0,
170 "enables \"..\" components in path lookup in capability mode");
171 static int lookup_cap_dotdot_nonlocal = 1;
172 SYSCTL_INT(_vfs, OID_AUTO, lookup_cap_dotdot_nonlocal, CTLFLAG_RWTUN,
173 &lookup_cap_dotdot_nonlocal, 0,
174 "enables \"..\" components in path lookup in capability mode "
175 "on non-local mount");
178 nameicap_tracker_add(struct nameidata *ndp, struct vnode *dp)
180 struct nameicap_tracker *nt;
181 struct componentname *cnp;
183 if ((ndp->ni_lcf & NI_LCF_CAP_DOTDOT) == 0 || dp->v_type != VDIR)
186 if ((cnp->cn_flags & BENEATH) != 0 &&
187 (ndp->ni_lcf & NI_LCF_BENEATH_LATCHED) == 0) {
188 MPASS((ndp->ni_lcf & NI_LCF_LATCH) != 0);
189 if (dp != ndp->ni_beneath_latch)
191 ndp->ni_lcf |= NI_LCF_BENEATH_LATCHED;
193 nt = uma_zalloc(nt_zone, M_WAITOK);
196 TAILQ_INSERT_TAIL(&ndp->ni_cap_tracker, nt, nm_link);
200 nameicap_cleanup(struct nameidata *ndp, bool clean_latch)
202 struct nameicap_tracker *nt, *nt1;
204 KASSERT(TAILQ_EMPTY(&ndp->ni_cap_tracker) ||
205 (ndp->ni_lcf & NI_LCF_CAP_DOTDOT) != 0, ("not strictrelative"));
206 TAILQ_FOREACH_SAFE(nt, &ndp->ni_cap_tracker, nm_link, nt1) {
207 TAILQ_REMOVE(&ndp->ni_cap_tracker, nt, nm_link);
209 uma_zfree(nt_zone, nt);
211 if (clean_latch && (ndp->ni_lcf & NI_LCF_LATCH) != 0) {
212 ndp->ni_lcf &= ~NI_LCF_LATCH;
213 vrele(ndp->ni_beneath_latch);
218 * For dotdot lookups in capability mode, only allow the component
219 * lookup to succeed if the resulting directory was already traversed
220 * during the operation. This catches situations where already
221 * traversed directory is moved to different parent, and then we walk
222 * over it with dotdots.
224 * Also allow to force failure of dotdot lookups for non-local
225 * filesystems, where external agents might assist local lookups to
226 * escape the compartment.
229 nameicap_check_dotdot(struct nameidata *ndp, struct vnode *dp)
231 struct nameicap_tracker *nt;
234 if ((ndp->ni_lcf & NI_LCF_CAP_DOTDOT) == 0 || dp == NULL ||
238 if (lookup_cap_dotdot_nonlocal == 0 && mp != NULL &&
239 (mp->mnt_flag & MNT_LOCAL) == 0)
240 return (ENOTCAPABLE);
241 TAILQ_FOREACH_REVERSE(nt, &ndp->ni_cap_tracker, nameicap_tracker_head,
243 if ((ndp->ni_lcf & NI_LCF_LATCH) != 0 &&
244 ndp->ni_beneath_latch == nt->dp) {
245 ndp->ni_lcf &= ~NI_LCF_BENEATH_LATCHED;
246 nameicap_cleanup(ndp, false);
252 return (ENOTCAPABLE);
256 namei_cleanup_cnp(struct componentname *cnp)
259 uma_zfree(namei_zone, cnp->cn_pnbuf);
261 cnp->cn_pnbuf = NULL;
262 cnp->cn_nameptr = NULL;
267 namei_handle_root(struct nameidata *ndp, struct vnode **dpp)
269 struct componentname *cnp;
272 if ((ndp->ni_lcf & NI_LCF_STRICTRELATIVE) != 0) {
274 if (KTRPOINT(curthread, KTR_CAPFAIL))
275 ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL);
277 return (ENOTCAPABLE);
279 if ((cnp->cn_flags & BENEATH) != 0) {
280 ndp->ni_lcf |= NI_LCF_BENEATH_ABS;
281 ndp->ni_lcf &= ~NI_LCF_BENEATH_LATCHED;
282 nameicap_cleanup(ndp, false);
284 while (*(cnp->cn_nameptr) == '/') {
288 *dpp = ndp->ni_rootdir;
294 namei_setup(struct nameidata *ndp, struct vnode **dpp, struct pwd **pwdp)
296 struct componentname *cnp;
301 struct filecaps dirfd_caps;
308 startdir_used = false;
312 #ifdef CAPABILITY_MODE
314 * In capability mode, lookups must be restricted to happen in
315 * the subtree with the root specified by the file descriptor:
316 * - The root must be real file descriptor, not the pseudo-descriptor
318 * - The passed path must be relative and not absolute.
319 * - If lookup_cap_dotdot is disabled, path must not contain the
321 * - If lookup_cap_dotdot is enabled, we verify that all '..'
322 * components lookups result in the directories which were
323 * previously walked by us, which prevents an escape from
326 if (IN_CAPABILITY_MODE(td) && (cnp->cn_flags & NOCAPCHECK) == 0) {
327 ndp->ni_lcf |= NI_LCF_STRICTRELATIVE;
328 ndp->ni_resflags |= NIRES_STRICTREL;
329 if (ndp->ni_dirfd == AT_FDCWD) {
331 if (KTRPOINT(td, KTR_CAPFAIL))
332 ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL);
341 * Get starting point for the translation.
345 * The reference on ni_rootdir is acquired in the block below to avoid
346 * back-to-back atomics for absolute lookups.
348 ndp->ni_rootdir = pwd->pwd_rdir;
349 ndp->ni_topdir = pwd->pwd_jdir;
351 if (cnp->cn_pnbuf[0] == '/') {
352 ndp->ni_resflags |= NIRES_ABS;
353 error = namei_handle_root(ndp, dpp);
355 if (ndp->ni_startdir != NULL) {
356 *dpp = ndp->ni_startdir;
357 startdir_used = true;
358 } else if (ndp->ni_dirfd == AT_FDCWD) {
359 *dpp = pwd->pwd_cdir;
362 rights = *ndp->ni_rightsneeded;
363 cap_rights_set_one(&rights, CAP_LOOKUP);
365 if (cnp->cn_flags & AUDITVNODE1)
366 AUDIT_ARG_ATFD1(ndp->ni_dirfd);
367 if (cnp->cn_flags & AUDITVNODE2)
368 AUDIT_ARG_ATFD2(ndp->ni_dirfd);
370 * Effectively inlined fgetvp_rights, because we need to
371 * inspect the file as well as grabbing the vnode.
373 error = fget_cap(td, ndp->ni_dirfd, &rights,
374 &dfp, &ndp->ni_filecaps);
377 * Preserve the error; it should either be EBADF
378 * or capability-related, both of which can be
379 * safely returned to the caller.
382 if (dfp->f_ops == &badfileops) {
384 } else if (dfp->f_vnode == NULL) {
390 if ((dfp->f_flag & FSEARCH) != 0)
391 cnp->cn_flags |= NOEXECCHECK;
397 * If file descriptor doesn't have all rights,
398 * all lookups relative to it must also be
402 if (!cap_rights_contains(&ndp->ni_filecaps.fc_rights,
404 ndp->ni_filecaps.fc_fcntls != CAP_FCNTL_ALL ||
405 ndp->ni_filecaps.fc_nioctls != -1) {
406 ndp->ni_lcf |= NI_LCF_STRICTRELATIVE;
407 ndp->ni_resflags |= NIRES_STRICTREL;
411 if (error == 0 && (*dpp)->v_type != VDIR)
414 if (error == 0 && (cnp->cn_flags & BENEATH) != 0) {
415 if (ndp->ni_dirfd == AT_FDCWD) {
416 ndp->ni_beneath_latch = pwd->pwd_cdir;
417 vrefact(ndp->ni_beneath_latch);
419 rights = *ndp->ni_rightsneeded;
420 cap_rights_set_one(&rights, CAP_LOOKUP);
421 error = fgetvp_rights(td, ndp->ni_dirfd, &rights,
422 &dirfd_caps, &ndp->ni_beneath_latch);
423 if (error == 0 && (*dpp)->v_type != VDIR) {
424 vrele(ndp->ni_beneath_latch);
429 ndp->ni_lcf |= NI_LCF_LATCH;
431 if (error == 0 && (cnp->cn_flags & RBENEATH) != 0) {
432 if (cnp->cn_pnbuf[0] == '/' ||
433 (ndp->ni_lcf & NI_LCF_BENEATH_ABS) != 0) {
435 } else if ((ndp->ni_lcf & NI_LCF_STRICTRELATIVE) == 0) {
436 ndp->ni_lcf |= NI_LCF_STRICTRELATIVE |
442 * If we are auditing the kernel pathname, save the user pathname.
444 if (cnp->cn_flags & AUDITVNODE1)
445 AUDIT_ARG_UPATH1_VP(td, ndp->ni_rootdir, *dpp, cnp->cn_pnbuf);
446 if (cnp->cn_flags & AUDITVNODE2)
447 AUDIT_ARG_UPATH2_VP(td, ndp->ni_rootdir, *dpp, cnp->cn_pnbuf);
448 if (ndp->ni_startdir != NULL && !startdir_used)
449 vrele(ndp->ni_startdir);
456 MPASS((ndp->ni_lcf & (NI_LCF_BENEATH_ABS | NI_LCF_LATCH)) !=
458 if (((ndp->ni_lcf & NI_LCF_STRICTRELATIVE) != 0 &&
459 lookup_cap_dotdot != 0) ||
460 ((ndp->ni_lcf & NI_LCF_STRICTRELATIVE) == 0 &&
461 (cnp->cn_flags & BENEATH) != 0))
462 ndp->ni_lcf |= NI_LCF_CAP_DOTDOT;
463 SDT_PROBE4(vfs, namei, lookup, entry, *dpp, cnp->cn_pnbuf,
464 cnp->cn_flags, false);
470 * Convert a pathname into a pointer to a locked vnode.
472 * The FOLLOW flag is set when symbolic links are to be followed
473 * when they occur at the end of the name translation process.
474 * Symbolic links are always followed for all other pathname
475 * components other than the last.
477 * The segflg defines whether the name is to be copied from user
478 * space or kernel space.
480 * Overall outline of namei:
483 * get starting directory
484 * while (!done && !error) {
485 * call lookup to search path.
486 * if symbolic link, massage name in buffer and continue
490 namei(struct nameidata *ndp)
492 char *cp; /* pointer into pathname argument */
493 struct vnode *dp; /* the directory we are searching */
494 struct iovec aiov; /* uio for reading symbolic links */
495 struct componentname *cnp;
500 enum cache_fpl_status status;
508 * While NDINIT may seem like a more natural place to do it, there are
509 * callers which directly modify flags past invoking init.
511 cnp->cn_origflags = cnp->cn_flags;
513 ndp->ni_cnd.cn_cred = ndp->ni_cnd.cn_thread->td_ucred;
514 KASSERT(cnp->cn_cred && td->td_proc, ("namei: bad cred/proc"));
515 KASSERT((cnp->cn_flags & NAMEI_INTERNAL_FLAGS) == 0,
516 ("namei: unexpected flags: %" PRIx64 "\n",
517 cnp->cn_flags & NAMEI_INTERNAL_FLAGS));
518 if (cnp->cn_flags & NOCACHE)
519 KASSERT(cnp->cn_nameiop != LOOKUP,
520 ("%s: NOCACHE passed with LOOKUP", __func__));
521 MPASS(ndp->ni_startdir == NULL || ndp->ni_startdir->v_type == VDIR ||
522 ndp->ni_startdir->v_type == VBAD);
528 * Get a buffer for the name to be translated, and copy the
529 * name into the buffer.
531 cnp->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK);
532 if (ndp->ni_segflg == UIO_SYSSPACE)
533 error = copystr(ndp->ni_dirp, cnp->cn_pnbuf, MAXPATHLEN,
536 error = copyinstr(ndp->ni_dirp, cnp->cn_pnbuf, MAXPATHLEN,
539 if (__predict_false(error != 0)) {
540 namei_cleanup_cnp(cnp);
545 * Don't allow empty pathnames.
547 if (__predict_false(*cnp->cn_pnbuf == '\0')) {
548 namei_cleanup_cnp(cnp);
553 if (KTRPOINT(td, KTR_NAMEI)) {
554 KASSERT(cnp->cn_thread == curthread,
555 ("namei not using curthread"));
556 ktrnamei(cnp->cn_pnbuf);
560 cnp->cn_nameptr = cnp->cn_pnbuf;
563 * First try looking up the target without locking any vnodes.
565 * We may need to start from scratch or pick up where it left off.
567 error = cache_fplookup(ndp, &status, &pwd);
569 case CACHE_FPL_STATUS_UNSET:
570 __assert_unreachable();
572 case CACHE_FPL_STATUS_HANDLED:
576 case CACHE_FPL_STATUS_PARTIAL:
577 TAILQ_INIT(&ndp->ni_cap_tracker);
578 dp = ndp->ni_startdir;
580 case CACHE_FPL_STATUS_ABORTED:
581 TAILQ_INIT(&ndp->ni_cap_tracker);
582 error = namei_setup(ndp, &dp, &pwd);
584 namei_cleanup_cnp(cnp);
596 ndp->ni_startdir = dp;
601 * If not a symbolic link, we're done.
603 if ((cnp->cn_flags & ISSYMLINK) == 0) {
604 if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0) {
605 namei_cleanup_cnp(cnp);
607 cnp->cn_flags |= HASBUF;
608 if ((ndp->ni_lcf & (NI_LCF_LATCH |
609 NI_LCF_BENEATH_LATCHED)) == NI_LCF_LATCH) {
613 nameicap_cleanup(ndp, true);
614 SDT_PROBE3(vfs, namei, lookup, return, error,
615 (error == 0 ? ndp->ni_vp : NULL), false);
621 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
626 if ((cnp->cn_flags & NOMACCHECK) == 0) {
627 error = mac_vnode_check_readlink(td->td_ucred,
633 if (ndp->ni_pathlen > 1)
634 cp = uma_zalloc(namei_zone, M_WAITOK);
638 aiov.iov_len = MAXPATHLEN;
639 auio.uio_iov = &aiov;
642 auio.uio_rw = UIO_READ;
643 auio.uio_segflg = UIO_SYSSPACE;
645 auio.uio_resid = MAXPATHLEN;
646 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
648 if (ndp->ni_pathlen > 1)
649 uma_zfree(namei_zone, cp);
652 linklen = MAXPATHLEN - auio.uio_resid;
654 if (ndp->ni_pathlen > 1)
655 uma_zfree(namei_zone, cp);
659 if (linklen + ndp->ni_pathlen > MAXPATHLEN) {
660 if (ndp->ni_pathlen > 1)
661 uma_zfree(namei_zone, cp);
662 error = ENAMETOOLONG;
665 if (ndp->ni_pathlen > 1) {
666 bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
667 uma_zfree(namei_zone, cnp->cn_pnbuf);
670 cnp->cn_pnbuf[linklen] = '\0';
671 ndp->ni_pathlen += linklen;
675 * Check if root directory should replace current directory.
677 cnp->cn_nameptr = cnp->cn_pnbuf;
678 if (*(cnp->cn_nameptr) == '/') {
680 error = namei_handle_root(ndp, &dp);
690 namei_cleanup_cnp(cnp);
691 nameicap_cleanup(ndp, true);
692 SDT_PROBE3(vfs, namei, lookup, return, error, NULL, false);
698 compute_cn_lkflags(struct mount *mp, int lkflags, int cnflags)
701 if (mp == NULL || ((lkflags & LK_SHARED) &&
702 (!(mp->mnt_kern_flag & MNTK_LOOKUP_SHARED) ||
703 ((cnflags & ISDOTDOT) &&
704 (mp->mnt_kern_flag & MNTK_LOOKUP_EXCL_DOTDOT))))) {
705 lkflags &= ~LK_SHARED;
706 lkflags |= LK_EXCLUSIVE;
708 lkflags |= LK_NODDLKTREAT;
713 needs_exclusive_leaf(struct mount *mp, int flags)
717 * Intermediate nodes can use shared locks, we only need to
718 * force an exclusive lock for leaf nodes.
720 if ((flags & (ISLASTCN | LOCKLEAF)) != (ISLASTCN | LOCKLEAF))
723 /* Always use exclusive locks if LOCKSHARED isn't set. */
724 if (!(flags & LOCKSHARED))
728 * For lookups during open(), if the mount point supports
729 * extended shared operations, then use a shared lock for the
730 * leaf node, otherwise use an exclusive lock.
732 if ((flags & ISOPEN) != 0)
733 return (!MNT_EXTENDED_SHARED(mp));
736 * Lookup requests outside of open() that specify LOCKSHARED
737 * only need a shared lock on the leaf vnode.
744 * This is a very central and rather complicated routine.
746 * The pathname is pointed to by ni_ptr and is of length ni_pathlen.
747 * The starting directory is taken from ni_startdir. The pathname is
748 * descended until done, or a symbolic link is encountered. The variable
749 * ni_more is clear if the path is completed; it is set to one if a
750 * symbolic link needing interpretation is encountered.
752 * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
753 * whether the name is to be looked up, created, renamed, or deleted.
754 * When CREATE, RENAME, or DELETE is specified, information usable in
755 * creating, renaming, or deleting a directory entry may be calculated.
756 * If flag has LOCKPARENT or'ed into it, the parent directory is returned
757 * locked. If flag has WANTPARENT or'ed into it, the parent directory is
758 * returned unlocked. Otherwise the parent directory is not returned. If
759 * the target of the pathname exists and LOCKLEAF is or'ed into the flag
760 * the target is returned locked, otherwise it is returned unlocked.
761 * When creating or renaming and LOCKPARENT is specified, the target may not
762 * be ".". When deleting and LOCKPARENT is specified, the target may be ".".
764 * Overall outline of lookup:
767 * identify next component of name at ndp->ni_ptr
768 * handle degenerate case where name is null string
769 * if .. and crossing mount points and on mounted filesys, find parent
770 * call VOP_LOOKUP routine for next component name
771 * directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set
772 * component vnode returned in ni_vp (if it exists), locked.
773 * if result vnode is mounted on and crossing mount points,
774 * find mounted on vnode
775 * if more components of name, do next level at dirloop
776 * return the answer in ni_vp, locked if LOCKLEAF set
777 * if LOCKPARENT set, return locked parent in ni_dvp
778 * if WANTPARENT set, return unlocked parent in ni_dvp
781 lookup(struct nameidata *ndp)
783 char *cp; /* pointer into pathname argument */
784 char *prev_ni_next; /* saved ndp->ni_next */
785 struct vnode *dp = NULL; /* the directory we are searching */
786 struct vnode *tdp; /* saved dp */
787 struct mount *mp; /* mount table entry */
789 size_t prev_ni_pathlen; /* saved ndp->ni_pathlen */
790 int docache; /* == 0 do not cache last component */
791 int wantparent; /* 1 => wantparent or lockparent flag */
792 int rdonly; /* lookup read-only flag bit */
794 int dpunlocked = 0; /* dp has already been unlocked */
795 int relookup = 0; /* do not consume the path component */
796 struct componentname *cnp = &ndp->ni_cnd;
801 * Setup: break out flag bits into variables.
804 wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT);
805 KASSERT(cnp->cn_nameiop == LOOKUP || wantparent,
806 ("CREATE, DELETE, RENAME require LOCKPARENT or WANTPARENT."));
808 * When set to zero, docache causes the last component of the
809 * pathname to be deleted from the cache and the full lookup
810 * of the name to be done (via VOP_CACHEDLOOKUP()). Often
811 * filesystems need some pre-computed values that are made
812 * during the full lookup, for instance UFS sets dp->i_offset.
814 * The docache variable is set to zero when requested by the
815 * NOCACHE flag and for all modifying operations except CREATE.
817 docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE;
818 if (cnp->cn_nameiop == DELETE ||
819 (wantparent && cnp->cn_nameiop != CREATE &&
820 cnp->cn_nameiop != LOOKUP))
822 rdonly = cnp->cn_flags & RDONLY;
823 cnp->cn_flags &= ~ISSYMLINK;
826 * We use shared locks until we hit the parent of the last cn then
827 * we adjust based on the requesting flags.
829 cnp->cn_lkflags = LK_SHARED;
830 dp = ndp->ni_startdir;
831 ndp->ni_startdir = NULLVP;
833 compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags | LK_RETRY,
838 * Search a new directory.
840 * The last component of the filename is left accessible via
841 * cnp->cn_nameptr for callers that need the name. Callers needing
842 * the name set the SAVENAME flag. When done, they assume
843 * responsibility for freeing the pathname buffer.
845 for (cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++)
847 cnp->cn_namelen = cp - cnp->cn_nameptr;
848 if (cnp->cn_namelen > NAME_MAX) {
849 error = ENAMETOOLONG;
852 #ifdef NAMEI_DIAGNOSTIC
855 printf("{%s}: ", cnp->cn_nameptr);
858 prev_ni_pathlen = ndp->ni_pathlen;
859 ndp->ni_pathlen -= cnp->cn_namelen;
860 KASSERT(ndp->ni_pathlen <= PATH_MAX,
861 ("%s: ni_pathlen underflow to %zd\n", __func__, ndp->ni_pathlen));
862 prev_ni_next = ndp->ni_next;
866 * Replace multiple slashes by a single slash and trailing slashes
867 * by a null. This must be done before VOP_LOOKUP() because some
868 * fs's don't know about trailing slashes. Remember if there were
869 * trailing slashes to handle symlinks, existing non-directories
870 * and non-existing files that won't be directories specially later.
872 while (*cp == '/' && (cp[1] == '/' || cp[1] == '\0')) {
876 *ndp->ni_next = '\0';
877 cnp->cn_flags |= TRAILINGSLASH;
882 cnp->cn_flags |= MAKEENTRY;
883 if (*cp == '\0' && docache == 0)
884 cnp->cn_flags &= ~MAKEENTRY;
885 if (cnp->cn_namelen == 2 &&
886 cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.')
887 cnp->cn_flags |= ISDOTDOT;
889 cnp->cn_flags &= ~ISDOTDOT;
890 if (*ndp->ni_next == 0)
891 cnp->cn_flags |= ISLASTCN;
893 cnp->cn_flags &= ~ISLASTCN;
895 if ((cnp->cn_flags & ISLASTCN) != 0 &&
896 cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.' &&
897 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
902 nameicap_tracker_add(ndp, dp);
905 * Check for degenerate name (e.g. / or "")
906 * which is a way of talking about a directory,
907 * e.g. like "/." or ".".
909 if (cnp->cn_nameptr[0] == '\0') {
910 if (dp->v_type != VDIR) {
914 if (cnp->cn_nameiop != LOOKUP) {
924 if (cnp->cn_flags & AUDITVNODE1)
925 AUDIT_ARG_VNODE1(dp);
926 else if (cnp->cn_flags & AUDITVNODE2)
927 AUDIT_ARG_VNODE2(dp);
929 if (!(cnp->cn_flags & (LOCKPARENT | LOCKLEAF)))
931 /* XXX This should probably move to the top of function. */
932 if (cnp->cn_flags & SAVESTART)
933 panic("lookup: SAVESTART");
938 * Handle "..": five special cases.
939 * 0. If doing a capability lookup and lookup_cap_dotdot is
940 * disabled, return ENOTCAPABLE.
941 * 1. Return an error if this is the last component of
942 * the name and the operation is DELETE or RENAME.
943 * 2. If at root directory (e.g. after chroot)
944 * or at absolute root directory
945 * then ignore it so can't get out.
946 * 3. If this vnode is the root of a mounted
947 * filesystem, then replace it with the
948 * vnode which was mounted on so we take the
949 * .. in the other filesystem.
950 * 4. If the vnode is the top directory of
951 * the jail or chroot, don't let them out.
952 * 5. If doing a capability lookup and lookup_cap_dotdot is
953 * enabled, return ENOTCAPABLE if the lookup would escape
954 * from the initial file descriptor directory. Checks are
955 * done by ensuring that namei() already traversed the
956 * result of dotdot lookup.
958 if (cnp->cn_flags & ISDOTDOT) {
959 if ((ndp->ni_lcf & (NI_LCF_STRICTRELATIVE | NI_LCF_CAP_DOTDOT))
960 == NI_LCF_STRICTRELATIVE) {
962 if (KTRPOINT(curthread, KTR_CAPFAIL))
963 ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL);
968 if ((cnp->cn_flags & ISLASTCN) != 0 &&
969 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
974 for (pr = cnp->cn_cred->cr_prison; pr != NULL;
976 if (dp == pr->pr_root)
978 if (dp == ndp->ni_rootdir ||
979 dp == ndp->ni_topdir ||
982 ((dp->v_vflag & VV_ROOT) != 0 &&
983 (cnp->cn_flags & NOCROSSMOUNT) != 0)) {
989 if ((dp->v_vflag & VV_ROOT) == 0)
991 if (VN_IS_DOOMED(dp)) { /* forced unmount */
996 dp = dp->v_mount->mnt_vnodecovered;
1000 compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags |
1001 LK_RETRY, ISDOTDOT));
1002 error = nameicap_check_dotdot(ndp, dp);
1005 if (KTRPOINT(curthread, KTR_CAPFAIL))
1006 ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL);
1014 * We now have a segment name to search for, and a directory to search.
1018 error = mac_vnode_check_lookup(cnp->cn_thread->td_ucred, dp, cnp);
1024 ASSERT_VOP_LOCKED(dp, "lookup");
1026 * If we have a shared lock we may need to upgrade the lock for the
1029 if ((cnp->cn_flags & LOCKPARENT) && (cnp->cn_flags & ISLASTCN) &&
1030 dp != vp_crossmp && VOP_ISLOCKED(dp) == LK_SHARED)
1031 vn_lock(dp, LK_UPGRADE|LK_RETRY);
1032 if (VN_IS_DOOMED(dp)) {
1037 * If we're looking up the last component and we need an exclusive
1038 * lock, adjust our lkflags.
1040 if (needs_exclusive_leaf(dp->v_mount, cnp->cn_flags))
1041 cnp->cn_lkflags = LK_EXCLUSIVE;
1042 #ifdef NAMEI_DIAGNOSTIC
1043 vn_printf(dp, "lookup in ");
1045 lkflags_save = cnp->cn_lkflags;
1046 cnp->cn_lkflags = compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags,
1048 error = VOP_LOOKUP(dp, &ndp->ni_vp, cnp);
1049 cnp->cn_lkflags = lkflags_save;
1051 KASSERT(ndp->ni_vp == NULL, ("leaf should be empty"));
1052 #ifdef NAMEI_DIAGNOSTIC
1053 printf("not found\n");
1055 if ((error == ENOENT) &&
1056 (dp->v_vflag & VV_ROOT) && (dp->v_mount != NULL) &&
1057 (dp->v_mount->mnt_flag & MNT_UNION)) {
1059 dp = dp->v_mount->mnt_vnodecovered;
1063 compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags |
1064 LK_RETRY, cnp->cn_flags));
1065 nameicap_tracker_add(ndp, dp);
1069 if (error == ERELOOKUP) {
1077 if (error != EJUSTRETURN)
1080 * At this point, we know we're at the end of the
1081 * pathname. If creating / renaming, we can consider
1082 * allowing the file or directory to be created / renamed,
1083 * provided we're not on a read-only filesystem.
1089 /* trailing slash only allowed for directories */
1090 if ((cnp->cn_flags & TRAILINGSLASH) &&
1091 !(cnp->cn_flags & WILLBEDIR)) {
1095 if ((cnp->cn_flags & LOCKPARENT) == 0)
1098 * We return with ni_vp NULL to indicate that the entry
1099 * doesn't currently exist, leaving a pointer to the
1100 * (possibly locked) directory vnode in ndp->ni_dvp.
1102 if (cnp->cn_flags & SAVESTART) {
1103 ndp->ni_startdir = ndp->ni_dvp;
1104 VREF(ndp->ni_startdir);
1110 #ifdef NAMEI_DIAGNOSTIC
1116 * Check to see if the vnode has been mounted on;
1117 * if so find the root of the mounted filesystem.
1119 while (dp->v_type == VDIR && (mp = dp->v_mountedhere) &&
1120 (cnp->cn_flags & NOCROSSMOUNT) == 0) {
1121 if (vfs_busy(mp, 0))
1124 if (dp != ndp->ni_dvp)
1128 vrefact(vp_crossmp);
1129 ndp->ni_dvp = vp_crossmp;
1130 error = VFS_ROOT(mp, compute_cn_lkflags(mp, cnp->cn_lkflags,
1131 cnp->cn_flags), &tdp);
1133 if (vn_lock(vp_crossmp, LK_SHARED | LK_NOWAIT))
1134 panic("vp_crossmp exclusively locked or reclaimed");
1139 ndp->ni_vp = dp = tdp;
1143 * Check for symbolic link
1145 if ((dp->v_type == VLNK) &&
1146 ((cnp->cn_flags & FOLLOW) || (cnp->cn_flags & TRAILINGSLASH) ||
1147 *ndp->ni_next == '/')) {
1148 cnp->cn_flags |= ISSYMLINK;
1149 if (VN_IS_DOOMED(dp)) {
1151 * We can't know whether the directory was mounted with
1152 * NOSYMFOLLOW, so we can't follow safely.
1157 if (dp->v_mount->mnt_flag & MNT_NOSYMFOLLOW) {
1162 * Symlink code always expects an unlocked dvp.
1164 if (ndp->ni_dvp != ndp->ni_vp) {
1165 VOP_UNLOCK(ndp->ni_dvp);
1166 ni_dvp_unlocked = 1;
1173 * Not a symbolic link that we will follow. Continue with the
1174 * next component if there is any; otherwise, we're done.
1176 KASSERT((cnp->cn_flags & ISLASTCN) || *ndp->ni_next == '/',
1177 ("lookup: invalid path state."));
1180 ndp->ni_pathlen = prev_ni_pathlen;
1181 ndp->ni_next = prev_ni_next;
1182 if (ndp->ni_dvp != dp)
1188 if (cnp->cn_flags & ISDOTDOT) {
1189 error = nameicap_check_dotdot(ndp, ndp->ni_vp);
1192 if (KTRPOINT(curthread, KTR_CAPFAIL))
1193 ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL);
1198 if (*ndp->ni_next == '/') {
1199 cnp->cn_nameptr = ndp->ni_next;
1200 while (*cnp->cn_nameptr == '/') {
1204 if (ndp->ni_dvp != dp)
1211 * If we're processing a path with a trailing slash,
1212 * check that the end result is a directory.
1214 if ((cnp->cn_flags & TRAILINGSLASH) && dp->v_type != VDIR) {
1219 * Disallow directory write attempts on read-only filesystems.
1222 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
1226 if (cnp->cn_flags & SAVESTART) {
1227 ndp->ni_startdir = ndp->ni_dvp;
1228 VREF(ndp->ni_startdir);
1231 ni_dvp_unlocked = 2;
1232 if (ndp->ni_dvp != dp)
1236 } else if ((cnp->cn_flags & LOCKPARENT) == 0 && ndp->ni_dvp != dp) {
1237 VOP_UNLOCK(ndp->ni_dvp);
1238 ni_dvp_unlocked = 1;
1241 if (cnp->cn_flags & AUDITVNODE1)
1242 AUDIT_ARG_VNODE1(dp);
1243 else if (cnp->cn_flags & AUDITVNODE2)
1244 AUDIT_ARG_VNODE2(dp);
1246 if ((cnp->cn_flags & LOCKLEAF) == 0)
1250 * FIXME: for lookups which only cross a mount point to fetch the
1251 * root vnode, ni_dvp will be set to vp_crossmp. This can be a problem
1252 * if either WANTPARENT or LOCKPARENT is set.
1255 * Because of shared lookup we may have the vnode shared locked, but
1256 * the caller may want it to be exclusively locked.
1258 if (needs_exclusive_leaf(dp->v_mount, cnp->cn_flags) &&
1259 VOP_ISLOCKED(dp) != LK_EXCLUSIVE) {
1260 vn_lock(dp, LK_UPGRADE | LK_RETRY);
1261 if (VN_IS_DOOMED(dp)) {
1266 if (ndp->ni_vp != NULL && ndp->ni_vp->v_type == VDIR)
1267 nameicap_tracker_add(ndp, ndp->ni_vp);
1271 if (ni_dvp_unlocked != 2) {
1272 if (dp != ndp->ni_dvp && !ni_dvp_unlocked)
1285 * relookup - lookup a path name component
1286 * Used by lookup to re-acquire things.
1289 relookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp)
1291 struct vnode *dp = NULL; /* the directory we are searching */
1292 int wantparent; /* 1 => wantparent or lockparent flag */
1293 int rdonly; /* lookup read-only flag bit */
1296 KASSERT(cnp->cn_flags & ISLASTCN,
1297 ("relookup: Not given last component."));
1299 * Setup: break out flag bits into variables.
1301 wantparent = cnp->cn_flags & (LOCKPARENT|WANTPARENT);
1302 KASSERT(wantparent, ("relookup: parent not wanted."));
1303 rdonly = cnp->cn_flags & RDONLY;
1304 cnp->cn_flags &= ~ISSYMLINK;
1306 cnp->cn_lkflags = LK_EXCLUSIVE;
1307 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
1310 * Search a new directory.
1312 * The last component of the filename is left accessible via
1313 * cnp->cn_nameptr for callers that need the name. Callers needing
1314 * the name set the SAVENAME flag. When done, they assume
1315 * responsibility for freeing the pathname buffer.
1317 #ifdef NAMEI_DIAGNOSTIC
1318 printf("{%s}: ", cnp->cn_nameptr);
1322 * Check for "" which represents the root directory after slash
1325 if (cnp->cn_nameptr[0] == '\0') {
1327 * Support only LOOKUP for "/" because lookup()
1328 * can't succeed for CREATE, DELETE and RENAME.
1330 KASSERT(cnp->cn_nameiop == LOOKUP, ("nameiop must be LOOKUP"));
1331 KASSERT(dp->v_type == VDIR, ("dp is not a directory"));
1333 if (!(cnp->cn_flags & LOCKLEAF))
1336 /* XXX This should probably move to the top of function. */
1337 if (cnp->cn_flags & SAVESTART)
1338 panic("lookup: SAVESTART");
1342 if (cnp->cn_flags & ISDOTDOT)
1343 panic ("relookup: lookup on dot-dot");
1346 * We now have a segment name to search for, and a directory to search.
1348 #ifdef NAMEI_DIAGNOSTIC
1349 vn_printf(dp, "search in ");
1351 if ((error = VOP_LOOKUP(dp, vpp, cnp)) != 0) {
1352 KASSERT(*vpp == NULL, ("leaf should be empty"));
1353 if (error != EJUSTRETURN)
1356 * If creating and at end of pathname, then can consider
1357 * allowing file to be created.
1363 /* ASSERT(dvp == ndp->ni_startdir) */
1364 if (cnp->cn_flags & SAVESTART)
1366 if ((cnp->cn_flags & LOCKPARENT) == 0)
1369 * We return with ni_vp NULL to indicate that the entry
1370 * doesn't currently exist, leaving a pointer to the
1371 * (possibly locked) directory vnode in ndp->ni_dvp.
1379 * Disallow directory write attempts on read-only filesystems.
1382 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
1391 * Set the parent lock/ref state to the requested state.
1393 if ((cnp->cn_flags & LOCKPARENT) == 0 && dvp != dp) {
1398 } else if (!wantparent)
1401 * Check for symbolic link
1403 KASSERT(dp->v_type != VLNK || !(cnp->cn_flags & FOLLOW),
1404 ("relookup: symlink found.\n"));
1406 /* ASSERT(dvp == ndp->ni_startdir) */
1407 if (cnp->cn_flags & SAVESTART)
1410 if ((cnp->cn_flags & LOCKLEAF) == 0)
1420 * Free data allocated by namei(); see namei(9) for details.
1423 NDFREE_PNBUF(struct nameidata *ndp)
1426 if ((ndp->ni_cnd.cn_flags & HASBUF) != 0) {
1427 MPASS((ndp->ni_cnd.cn_flags & (SAVENAME | SAVESTART)) != 0);
1428 uma_zfree(namei_zone, ndp->ni_cnd.cn_pnbuf);
1429 ndp->ni_cnd.cn_flags &= ~HASBUF;
1434 (NDFREE)(struct nameidata *ndp, const u_int flags)
1442 if (!(flags & NDF_NO_FREE_PNBUF)) {
1445 if (!(flags & NDF_NO_VP_UNLOCK) &&
1446 (ndp->ni_cnd.cn_flags & LOCKLEAF) && ndp->ni_vp)
1448 if (!(flags & NDF_NO_DVP_UNLOCK) &&
1449 (ndp->ni_cnd.cn_flags & LOCKPARENT) &&
1450 ndp->ni_dvp != ndp->ni_vp)
1452 if (!(flags & NDF_NO_VP_RELE) && ndp->ni_vp) {
1461 VOP_UNLOCK(ndp->ni_vp);
1462 if (!(flags & NDF_NO_DVP_RELE) &&
1463 (ndp->ni_cnd.cn_flags & (LOCKPARENT|WANTPARENT))) {
1472 VOP_UNLOCK(ndp->ni_dvp);
1473 if (!(flags & NDF_NO_STARTDIR_RELE) &&
1474 (ndp->ni_cnd.cn_flags & SAVESTART)) {
1475 vrele(ndp->ni_startdir);
1476 ndp->ni_startdir = NULL;
1482 * Validate the final state of ndp after the lookup.
1484 * Historically filesystems were allowed to modify cn_flags. Most notably they
1485 * can add SAVENAME to the request, resulting in HASBUF and pushing subsequent
1486 * clean up to the consumer. In practice this seems to only concern != LOOKUP
1489 * As a step towards stricter API contract this routine validates the state to
1490 * clean up. Note validation is a work in progress with the intent of becoming
1491 * stricter over time.
1493 #define NDMODIFYINGFLAGS (LOCKLEAF | LOCKPARENT | WANTPARENT | SAVENAME | SAVESTART | HASBUF)
1495 NDVALIDATE(struct nameidata *ndp)
1497 struct componentname *cnp;
1498 u_int64_t used, orig;
1501 orig = cnp->cn_origflags;
1502 used = cnp->cn_flags;
1503 switch (cnp->cn_nameiop) {
1506 * For plain lookup we require strict conformance -- nothing
1507 * to clean up if it was not requested by the caller.
1509 orig &= NDMODIFYINGFLAGS;
1510 used &= NDMODIFYINGFLAGS;
1511 if ((orig & (SAVENAME | SAVESTART)) != 0)
1521 * Some filesystems set SAVENAME to provoke HASBUF, accomodate
1522 * for it until it gets fixed.
1524 orig &= NDMODIFYINGFLAGS;
1525 orig |= (SAVENAME | HASBUF);
1526 used &= NDMODIFYINGFLAGS;
1527 used |= (SAVENAME | HASBUF);
1535 panic("%s: mismatched flags for op %d: added %" PRIx64 ", "
1536 "removed %" PRIx64" (%" PRIx64" != %" PRIx64"; stored %" PRIx64" != %" PRIx64")",
1537 __func__, cnp->cn_nameiop, used & ~orig, orig &~ used,
1538 orig, used, cnp->cn_origflags, cnp->cn_flags);
1543 * Determine if there is a suitable alternate filename under the specified
1544 * prefix for the specified path. If the create flag is set, then the
1545 * alternate prefix will be used so long as the parent directory exists.
1546 * This is used by the various compatibility ABIs so that Linux binaries prefer
1547 * files under /compat/linux for example. The chosen path (whether under
1548 * the prefix or under /) is returned in a kernel malloc'd buffer pointed
1549 * to by pathbuf. The caller is responsible for free'ing the buffer from
1550 * the M_TEMP bucket if one is returned.
1553 kern_alternate_path(struct thread *td, const char *prefix, const char *path,
1554 enum uio_seg pathseg, char **pathbuf, int create, int dirfd)
1556 struct nameidata nd, ndroot;
1557 char *ptr, *buf, *cp;
1561 buf = (char *) malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1564 /* Copy the prefix into the new pathname as a starting point. */
1565 len = strlcpy(buf, prefix, MAXPATHLEN);
1566 if (len >= MAXPATHLEN) {
1571 sz = MAXPATHLEN - len;
1574 /* Append the filename to the prefix. */
1575 if (pathseg == UIO_SYSSPACE)
1576 error = copystr(path, ptr, sz, &len);
1578 error = copyinstr(path, ptr, sz, &len);
1586 /* Only use a prefix with absolute pathnames. */
1592 if (dirfd != AT_FDCWD) {
1594 * We want the original because the "prefix" is
1595 * included in the already opened dirfd.
1597 bcopy(ptr, buf, len);
1602 * We know that there is a / somewhere in this pathname.
1603 * Search backwards for it, to find the file's parent dir
1604 * to see if it exists in the alternate tree. If it does,
1605 * and we want to create a file (cflag is set). We don't
1606 * need to worry about the root comparison in this case.
1610 for (cp = &ptr[len] - 1; *cp != '/'; cp--);
1613 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, buf, td);
1619 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, buf, td);
1626 * We now compare the vnode of the prefix to the one
1627 * vnode asked. If they resolve to be the same, then we
1628 * ignore the match so that the real root gets used.
1629 * This avoids the problem of traversing "../.." to find the
1630 * root directory and never finding it, because "/" resolves
1631 * to the emulation root directory. This is expensive :-(
1633 NDINIT(&ndroot, LOOKUP, FOLLOW, UIO_SYSSPACE, prefix,
1636 /* We shouldn't ever get an error from this namei(). */
1637 error = namei(&ndroot);
1639 if (nd.ni_vp == ndroot.ni_vp)
1642 NDFREE(&ndroot, NDF_ONLY_PNBUF);
1643 vrele(ndroot.ni_vp);
1647 NDFREE(&nd, NDF_ONLY_PNBUF);
1651 /* If there was an error, use the original path name. */
1653 bcopy(ptr, buf, len);