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_DEFINE4(vfs, namei, lookup, return, "int", "struct vnode *", "bool",
82 /* Allocation zone for namei. */
83 uma_zone_t namei_zone;
85 /* Placeholder vnode for mp traversal. */
86 static struct vnode *vp_crossmp;
89 crossmp_vop_islocked(struct vop_islocked_args *ap)
96 crossmp_vop_lock1(struct vop_lock1_args *ap)
99 struct lock *lk __unused;
100 const char *file __unused;
101 int flags, line __unused;
109 if ((flags & LK_SHARED) == 0)
110 panic("invalid lock request for crossmp");
112 WITNESS_CHECKORDER(&lk->lock_object, LOP_NEWORDER, file, line,
113 flags & LK_INTERLOCK ? &VI_MTX(vp)->lock_object : NULL);
114 WITNESS_LOCK(&lk->lock_object, 0, file, line);
115 if ((flags & LK_INTERLOCK) != 0)
117 LOCK_LOG_LOCK("SLOCK", &lk->lock_object, 0, 0, ap->a_file, line);
122 crossmp_vop_unlock(struct vop_unlock_args *ap)
125 struct lock *lk __unused;
130 WITNESS_UNLOCK(&lk->lock_object, 0, LOCK_FILE, LOCK_LINE);
131 LOCK_LOG_LOCK("SUNLOCK", &lk->lock_object, 0, 0, LOCK_FILE,
136 static struct vop_vector crossmp_vnodeops = {
137 .vop_default = &default_vnodeops,
138 .vop_islocked = crossmp_vop_islocked,
139 .vop_lock1 = crossmp_vop_lock1,
140 .vop_unlock = crossmp_vop_unlock,
143 * VFS_VOP_VECTOR_REGISTER(crossmp_vnodeops) is not used here since the vnode
144 * gets allocated early. See nameiinit for the direct call below.
147 struct nameicap_tracker {
149 TAILQ_ENTRY(nameicap_tracker) nm_link;
152 /* Zone for cap mode tracker elements used for dotdot capability checks. */
153 MALLOC_DEFINE(M_NAMEITRACKER, "namei_tracker", "namei tracking for dotdot");
156 nameiinit(void *dummy __unused)
159 namei_zone = uma_zcreate("NAMEI", MAXPATHLEN, NULL, NULL, NULL, NULL,
161 vfs_vector_op_register(&crossmp_vnodeops);
162 getnewvnode("crossmp", NULL, &crossmp_vnodeops, &vp_crossmp);
164 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_SECOND, nameiinit, NULL);
166 static int lookup_cap_dotdot = 1;
167 SYSCTL_INT(_vfs, OID_AUTO, lookup_cap_dotdot, CTLFLAG_RWTUN,
168 &lookup_cap_dotdot, 0,
169 "enables \"..\" components in path lookup in capability mode");
170 static int lookup_cap_dotdot_nonlocal = 1;
171 SYSCTL_INT(_vfs, OID_AUTO, lookup_cap_dotdot_nonlocal, CTLFLAG_RWTUN,
172 &lookup_cap_dotdot_nonlocal, 0,
173 "enables \"..\" components in path lookup in capability mode "
174 "on non-local mount");
177 nameicap_tracker_add(struct nameidata *ndp, struct vnode *dp)
179 struct nameicap_tracker *nt;
180 struct componentname *cnp;
182 if ((ndp->ni_lcf & NI_LCF_CAP_DOTDOT) == 0 || dp->v_type != VDIR)
185 nt = malloc(sizeof(*nt), M_NAMEITRACKER, M_WAITOK);
188 TAILQ_INSERT_TAIL(&ndp->ni_cap_tracker, nt, nm_link);
192 nameicap_cleanup(struct nameidata *ndp)
194 struct nameicap_tracker *nt, *nt1;
196 KASSERT(TAILQ_EMPTY(&ndp->ni_cap_tracker) ||
197 (ndp->ni_lcf & NI_LCF_CAP_DOTDOT) != 0, ("not strictrelative"));
198 TAILQ_FOREACH_SAFE(nt, &ndp->ni_cap_tracker, nm_link, nt1) {
199 TAILQ_REMOVE(&ndp->ni_cap_tracker, nt, nm_link);
201 free(nt, M_NAMEITRACKER);
206 * For dotdot lookups in capability mode, only allow the component
207 * lookup to succeed if the resulting directory was already traversed
208 * during the operation. This catches situations where already
209 * traversed directory is moved to different parent, and then we walk
210 * over it with dotdots.
212 * Also allow to force failure of dotdot lookups for non-local
213 * filesystems, where external agents might assist local lookups to
214 * escape the compartment.
217 nameicap_check_dotdot(struct nameidata *ndp, struct vnode *dp)
219 struct nameicap_tracker *nt;
222 if (dp == NULL || dp->v_type != VDIR || (ndp->ni_lcf &
223 NI_LCF_STRICTRELATIVE) == 0)
225 if ((ndp->ni_lcf & NI_LCF_CAP_DOTDOT) == 0)
226 return (ENOTCAPABLE);
228 if (lookup_cap_dotdot_nonlocal == 0 && mp != NULL &&
229 (mp->mnt_flag & MNT_LOCAL) == 0)
230 return (ENOTCAPABLE);
231 TAILQ_FOREACH_REVERSE(nt, &ndp->ni_cap_tracker, nameicap_tracker_head,
236 return (ENOTCAPABLE);
240 namei_cleanup_cnp(struct componentname *cnp)
243 uma_zfree(namei_zone, cnp->cn_pnbuf);
245 cnp->cn_pnbuf = NULL;
246 cnp->cn_nameptr = NULL;
251 namei_handle_root(struct nameidata *ndp, struct vnode **dpp)
253 struct componentname *cnp;
256 if ((ndp->ni_lcf & NI_LCF_STRICTRELATIVE) != 0) {
258 if (KTRPOINT(curthread, KTR_CAPFAIL))
259 ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL);
261 return (ENOTCAPABLE);
263 while (*(cnp->cn_nameptr) == '/') {
267 *dpp = ndp->ni_rootdir;
273 namei_setup(struct nameidata *ndp, struct vnode **dpp, struct pwd **pwdp)
275 struct componentname *cnp;
286 startdir_used = false;
290 #ifdef CAPABILITY_MODE
292 * In capability mode, lookups must be restricted to happen in
293 * the subtree with the root specified by the file descriptor:
294 * - The root must be real file descriptor, not the pseudo-descriptor
296 * - The passed path must be relative and not absolute.
297 * - If lookup_cap_dotdot is disabled, path must not contain the
299 * - If lookup_cap_dotdot is enabled, we verify that all '..'
300 * components lookups result in the directories which were
301 * previously walked by us, which prevents an escape from
304 if (IN_CAPABILITY_MODE(td) && (cnp->cn_flags & NOCAPCHECK) == 0) {
305 ndp->ni_lcf |= NI_LCF_STRICTRELATIVE;
306 ndp->ni_resflags |= NIRES_STRICTREL;
307 if (ndp->ni_dirfd == AT_FDCWD) {
309 if (KTRPOINT(td, KTR_CAPFAIL))
310 ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL);
319 * Get starting point for the translation.
323 * The reference on ni_rootdir is acquired in the block below to avoid
324 * back-to-back atomics for absolute lookups.
326 ndp->ni_rootdir = pwd->pwd_rdir;
327 ndp->ni_topdir = pwd->pwd_jdir;
329 if (cnp->cn_pnbuf[0] == '/') {
330 ndp->ni_resflags |= NIRES_ABS;
331 error = namei_handle_root(ndp, dpp);
333 if (ndp->ni_startdir != NULL) {
334 *dpp = ndp->ni_startdir;
335 startdir_used = true;
336 } else if (ndp->ni_dirfd == AT_FDCWD) {
337 *dpp = pwd->pwd_cdir;
340 rights = *ndp->ni_rightsneeded;
341 cap_rights_set_one(&rights, CAP_LOOKUP);
343 if (cnp->cn_flags & AUDITVNODE1)
344 AUDIT_ARG_ATFD1(ndp->ni_dirfd);
345 if (cnp->cn_flags & AUDITVNODE2)
346 AUDIT_ARG_ATFD2(ndp->ni_dirfd);
348 * Effectively inlined fgetvp_rights, because we need to
349 * inspect the file as well as grabbing the vnode.
351 error = fget_cap(td, ndp->ni_dirfd, &rights,
352 &dfp, &ndp->ni_filecaps);
355 * Preserve the error; it should either be EBADF
356 * or capability-related, both of which can be
357 * safely returned to the caller.
360 if (dfp->f_ops == &badfileops) {
362 } else if (dfp->f_vnode == NULL) {
368 if ((dfp->f_flag & FSEARCH) != 0)
369 cnp->cn_flags |= NOEXECCHECK;
375 * If file descriptor doesn't have all rights,
376 * all lookups relative to it must also be
380 if (!cap_rights_contains(&ndp->ni_filecaps.fc_rights,
382 ndp->ni_filecaps.fc_fcntls != CAP_FCNTL_ALL ||
383 ndp->ni_filecaps.fc_nioctls != -1) {
384 ndp->ni_lcf |= NI_LCF_STRICTRELATIVE;
385 ndp->ni_resflags |= NIRES_STRICTREL;
389 if (error == 0 && (*dpp)->v_type != VDIR)
392 if (error == 0 && (cnp->cn_flags & RBENEATH) != 0) {
393 if (cnp->cn_pnbuf[0] == '/') {
395 } else if ((ndp->ni_lcf & NI_LCF_STRICTRELATIVE) == 0) {
396 ndp->ni_lcf |= NI_LCF_STRICTRELATIVE |
402 * If we are auditing the kernel pathname, save the user pathname.
404 if (cnp->cn_flags & AUDITVNODE1)
405 AUDIT_ARG_UPATH1_VP(td, ndp->ni_rootdir, *dpp, cnp->cn_pnbuf);
406 if (cnp->cn_flags & AUDITVNODE2)
407 AUDIT_ARG_UPATH2_VP(td, ndp->ni_rootdir, *dpp, cnp->cn_pnbuf);
408 if (ndp->ni_startdir != NULL && !startdir_used)
409 vrele(ndp->ni_startdir);
416 if ((ndp->ni_lcf & NI_LCF_STRICTRELATIVE) != 0 &&
417 lookup_cap_dotdot != 0)
418 ndp->ni_lcf |= NI_LCF_CAP_DOTDOT;
419 SDT_PROBE4(vfs, namei, lookup, entry, *dpp, cnp->cn_pnbuf,
420 cnp->cn_flags, false);
426 namei_getpath(struct nameidata *ndp)
428 struct componentname *cnp;
434 * Get a buffer for the name to be translated, and copy the
435 * name into the buffer.
437 cnp->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK);
438 if (ndp->ni_segflg == UIO_SYSSPACE) {
439 error = copystr(ndp->ni_dirp, cnp->cn_pnbuf, MAXPATHLEN,
442 error = copyinstr(ndp->ni_dirp, cnp->cn_pnbuf, MAXPATHLEN,
446 if (__predict_false(error != 0)) {
447 namei_cleanup_cnp(cnp);
452 * Don't allow empty pathnames.
454 if (__predict_false(*cnp->cn_pnbuf == '\0')) {
455 namei_cleanup_cnp(cnp);
459 cnp->cn_nameptr = cnp->cn_pnbuf;
464 * Convert a pathname into a pointer to a locked vnode.
466 * The FOLLOW flag is set when symbolic links are to be followed
467 * when they occur at the end of the name translation process.
468 * Symbolic links are always followed for all other pathname
469 * components other than the last.
471 * The segflg defines whether the name is to be copied from user
472 * space or kernel space.
474 * Overall outline of namei:
477 * get starting directory
478 * while (!done && !error) {
479 * call lookup to search path.
480 * if symbolic link, massage name in buffer and continue
484 namei(struct nameidata *ndp)
486 char *cp; /* pointer into pathname argument */
487 struct vnode *dp; /* the directory we are searching */
488 struct iovec aiov; /* uio for reading symbolic links */
489 struct componentname *cnp;
494 enum cache_fpl_status status;
499 KASSERT((ndp->ni_debugflags & NAMEI_DBG_CALLED) == 0,
500 ("%s: repeated call to namei without NDREINIT", __func__));
501 KASSERT(ndp->ni_debugflags == NAMEI_DBG_INITED,
502 ("%s: bad debugflags %d", __func__, ndp->ni_debugflags));
503 ndp->ni_debugflags |= NAMEI_DBG_CALLED;
504 if (ndp->ni_startdir != NULL)
505 ndp->ni_debugflags |= NAMEI_DBG_HADSTARTDIR;
506 if (cnp->cn_flags & FAILIFEXISTS) {
507 KASSERT(cnp->cn_nameiop == CREATE,
508 ("%s: FAILIFEXISTS passed for op %d", __func__, cnp->cn_nameiop));
510 * The limitation below is to restrict hairy corner cases.
512 KASSERT((cnp->cn_flags & (LOCKPARENT | LOCKLEAF)) == LOCKPARENT,
513 ("%s: FAILIFEXISTS must be passed with LOCKPARENT and without LOCKLEAF",
519 * While NDINIT may seem like a more natural place to do it, there are
520 * callers which directly modify flags past invoking init.
522 cnp->cn_origflags = cnp->cn_flags;
524 ndp->ni_cnd.cn_cred = ndp->ni_cnd.cn_thread->td_ucred;
525 KASSERT(ndp->ni_resflags == 0, ("%s: garbage in ni_resflags: %x\n",
526 __func__, ndp->ni_resflags));
527 KASSERT(cnp->cn_cred && td->td_proc, ("namei: bad cred/proc"));
528 KASSERT((cnp->cn_flags & NAMEI_INTERNAL_FLAGS) == 0,
529 ("namei: unexpected flags: %" PRIx64 "\n",
530 cnp->cn_flags & NAMEI_INTERNAL_FLAGS));
531 if (cnp->cn_flags & NOCACHE)
532 KASSERT(cnp->cn_nameiop != LOOKUP,
533 ("%s: NOCACHE passed with LOOKUP", __func__));
534 MPASS(ndp->ni_startdir == NULL || ndp->ni_startdir->v_type == VDIR ||
535 ndp->ni_startdir->v_type == VBAD);
541 error = namei_getpath(ndp);
542 if (__predict_false(error != 0)) {
547 if (KTRPOINT(td, KTR_NAMEI)) {
548 KASSERT(cnp->cn_thread == curthread,
549 ("namei not using curthread"));
550 ktrnamei(cnp->cn_pnbuf);
555 * First try looking up the target without locking any vnodes.
557 * We may need to start from scratch or pick up where it left off.
559 error = cache_fplookup(ndp, &status, &pwd);
561 case CACHE_FPL_STATUS_UNSET:
562 __assert_unreachable();
564 case CACHE_FPL_STATUS_HANDLED:
568 case CACHE_FPL_STATUS_PARTIAL:
569 TAILQ_INIT(&ndp->ni_cap_tracker);
570 dp = ndp->ni_startdir;
572 case CACHE_FPL_STATUS_DESTROYED:
574 error = namei_getpath(ndp);
575 if (__predict_false(error != 0)) {
579 case CACHE_FPL_STATUS_ABORTED:
580 TAILQ_INIT(&ndp->ni_cap_tracker);
581 MPASS(ndp->ni_lcf == 0);
582 error = namei_setup(ndp, &dp, &pwd);
584 namei_cleanup_cnp(cnp);
594 ndp->ni_startdir = dp;
600 * If not a symbolic link, we're done.
602 if ((cnp->cn_flags & ISSYMLINK) == 0) {
603 SDT_PROBE4(vfs, namei, lookup, return, error,
604 (error == 0 ? ndp->ni_vp : NULL), false, ndp);
605 if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0) {
606 namei_cleanup_cnp(cnp);
608 cnp->cn_flags |= HASBUF;
609 nameicap_cleanup(ndp);
615 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
620 if ((cnp->cn_flags & NOMACCHECK) == 0) {
621 error = mac_vnode_check_readlink(td->td_ucred,
627 if (ndp->ni_pathlen > 1)
628 cp = uma_zalloc(namei_zone, M_WAITOK);
632 aiov.iov_len = MAXPATHLEN;
633 auio.uio_iov = &aiov;
636 auio.uio_rw = UIO_READ;
637 auio.uio_segflg = UIO_SYSSPACE;
639 auio.uio_resid = MAXPATHLEN;
640 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
642 if (ndp->ni_pathlen > 1)
643 uma_zfree(namei_zone, cp);
646 linklen = MAXPATHLEN - auio.uio_resid;
648 if (ndp->ni_pathlen > 1)
649 uma_zfree(namei_zone, cp);
653 if (linklen + ndp->ni_pathlen > MAXPATHLEN) {
654 if (ndp->ni_pathlen > 1)
655 uma_zfree(namei_zone, cp);
656 error = ENAMETOOLONG;
659 if (ndp->ni_pathlen > 1) {
660 bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
661 uma_zfree(namei_zone, cnp->cn_pnbuf);
664 cnp->cn_pnbuf[linklen] = '\0';
665 ndp->ni_pathlen += linklen;
669 * Check if root directory should replace current directory.
671 cnp->cn_nameptr = cnp->cn_pnbuf;
672 if (*(cnp->cn_nameptr) == '/') {
674 error = namei_handle_root(ndp, &dp);
684 SDT_PROBE4(vfs, namei, lookup, return, error, NULL, false, ndp);
685 namei_cleanup_cnp(cnp);
686 nameicap_cleanup(ndp);
692 compute_cn_lkflags(struct mount *mp, int lkflags, int cnflags)
695 if (mp == NULL || ((lkflags & LK_SHARED) &&
696 (!(mp->mnt_kern_flag & MNTK_LOOKUP_SHARED) ||
697 ((cnflags & ISDOTDOT) &&
698 (mp->mnt_kern_flag & MNTK_LOOKUP_EXCL_DOTDOT))))) {
699 lkflags &= ~LK_SHARED;
700 lkflags |= LK_EXCLUSIVE;
702 lkflags |= LK_NODDLKTREAT;
707 needs_exclusive_leaf(struct mount *mp, int flags)
711 * Intermediate nodes can use shared locks, we only need to
712 * force an exclusive lock for leaf nodes.
714 if ((flags & (ISLASTCN | LOCKLEAF)) != (ISLASTCN | LOCKLEAF))
717 /* Always use exclusive locks if LOCKSHARED isn't set. */
718 if (!(flags & LOCKSHARED))
722 * For lookups during open(), if the mount point supports
723 * extended shared operations, then use a shared lock for the
724 * leaf node, otherwise use an exclusive lock.
726 if ((flags & ISOPEN) != 0)
727 return (!MNT_EXTENDED_SHARED(mp));
730 * Lookup requests outside of open() that specify LOCKSHARED
731 * only need a shared lock on the leaf vnode.
738 * This is a very central and rather complicated routine.
740 * The pathname is pointed to by ni_ptr and is of length ni_pathlen.
741 * The starting directory is taken from ni_startdir. The pathname is
742 * descended until done, or a symbolic link is encountered. The variable
743 * ni_more is clear if the path is completed; it is set to one if a
744 * symbolic link needing interpretation is encountered.
746 * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
747 * whether the name is to be looked up, created, renamed, or deleted.
748 * When CREATE, RENAME, or DELETE is specified, information usable in
749 * creating, renaming, or deleting a directory entry may be calculated.
750 * If flag has LOCKPARENT or'ed into it, the parent directory is returned
751 * locked. If flag has WANTPARENT or'ed into it, the parent directory is
752 * returned unlocked. Otherwise the parent directory is not returned. If
753 * the target of the pathname exists and LOCKLEAF is or'ed into the flag
754 * the target is returned locked, otherwise it is returned unlocked.
755 * When creating or renaming and LOCKPARENT is specified, the target may not
756 * be ".". When deleting and LOCKPARENT is specified, the target may be ".".
758 * Overall outline of lookup:
761 * identify next component of name at ndp->ni_ptr
762 * handle degenerate case where name is null string
763 * if .. and crossing mount points and on mounted filesys, find parent
764 * call VOP_LOOKUP routine for next component name
765 * directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set
766 * component vnode returned in ni_vp (if it exists), locked.
767 * if result vnode is mounted on and crossing mount points,
768 * find mounted on vnode
769 * if more components of name, do next level at dirloop
770 * return the answer in ni_vp, locked if LOCKLEAF set
771 * if LOCKPARENT set, return locked parent in ni_dvp
772 * if WANTPARENT set, return unlocked parent in ni_dvp
775 lookup(struct nameidata *ndp)
777 char *cp; /* pointer into pathname argument */
778 char *prev_ni_next; /* saved ndp->ni_next */
779 struct vnode *dp = NULL; /* the directory we are searching */
780 struct vnode *tdp; /* saved dp */
781 struct mount *mp; /* mount table entry */
783 size_t prev_ni_pathlen; /* saved ndp->ni_pathlen */
784 int docache; /* == 0 do not cache last component */
785 int wantparent; /* 1 => wantparent or lockparent flag */
786 int rdonly; /* lookup read-only flag bit */
788 int dpunlocked = 0; /* dp has already been unlocked */
789 int relookup = 0; /* do not consume the path component */
790 struct componentname *cnp = &ndp->ni_cnd;
795 * Setup: break out flag bits into variables.
798 wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT);
799 KASSERT(cnp->cn_nameiop == LOOKUP || wantparent,
800 ("CREATE, DELETE, RENAME require LOCKPARENT or WANTPARENT."));
802 * When set to zero, docache causes the last component of the
803 * pathname to be deleted from the cache and the full lookup
804 * of the name to be done (via VOP_CACHEDLOOKUP()). Often
805 * filesystems need some pre-computed values that are made
806 * during the full lookup, for instance UFS sets dp->i_offset.
808 * The docache variable is set to zero when requested by the
809 * NOCACHE flag and for all modifying operations except CREATE.
811 docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE;
812 if (cnp->cn_nameiop == DELETE ||
813 (wantparent && cnp->cn_nameiop != CREATE &&
814 cnp->cn_nameiop != LOOKUP))
816 rdonly = cnp->cn_flags & RDONLY;
817 cnp->cn_flags &= ~ISSYMLINK;
820 * We use shared locks until we hit the parent of the last cn then
821 * we adjust based on the requesting flags.
823 cnp->cn_lkflags = LK_SHARED;
824 dp = ndp->ni_startdir;
825 ndp->ni_startdir = NULLVP;
827 compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags | LK_RETRY,
832 * Search a new directory.
834 * The last component of the filename is left accessible via
835 * cnp->cn_nameptr for callers that need the name. Callers needing
836 * the name set the SAVENAME flag. When done, they assume
837 * responsibility for freeing the pathname buffer.
839 for (cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++)
841 cnp->cn_namelen = cp - cnp->cn_nameptr;
842 if (cnp->cn_namelen > NAME_MAX) {
843 error = ENAMETOOLONG;
846 #ifdef NAMEI_DIAGNOSTIC
849 printf("{%s}: ", cnp->cn_nameptr);
852 prev_ni_pathlen = ndp->ni_pathlen;
853 ndp->ni_pathlen -= cnp->cn_namelen;
854 KASSERT(ndp->ni_pathlen <= PATH_MAX,
855 ("%s: ni_pathlen underflow to %zd\n", __func__, ndp->ni_pathlen));
856 prev_ni_next = ndp->ni_next;
860 * Replace multiple slashes by a single slash and trailing slashes
861 * by a null. This must be done before VOP_LOOKUP() because some
862 * fs's don't know about trailing slashes. Remember if there were
863 * trailing slashes to handle symlinks, existing non-directories
864 * and non-existing files that won't be directories specially later.
866 while (*cp == '/' && (cp[1] == '/' || cp[1] == '\0')) {
870 *ndp->ni_next = '\0';
871 cnp->cn_flags |= TRAILINGSLASH;
876 cnp->cn_flags |= MAKEENTRY;
877 if (*cp == '\0' && docache == 0)
878 cnp->cn_flags &= ~MAKEENTRY;
879 if (cnp->cn_namelen == 2 &&
880 cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.')
881 cnp->cn_flags |= ISDOTDOT;
883 cnp->cn_flags &= ~ISDOTDOT;
884 if (*ndp->ni_next == 0)
885 cnp->cn_flags |= ISLASTCN;
887 cnp->cn_flags &= ~ISLASTCN;
889 if ((cnp->cn_flags & ISLASTCN) != 0 &&
890 cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.' &&
891 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
896 nameicap_tracker_add(ndp, dp);
899 * Check for degenerate name (e.g. / or "")
900 * which is a way of talking about a directory,
901 * e.g. like "/." or ".".
903 if (cnp->cn_nameptr[0] == '\0') {
904 if (dp->v_type != VDIR) {
908 if (cnp->cn_nameiop != LOOKUP) {
918 if (cnp->cn_flags & AUDITVNODE1)
919 AUDIT_ARG_VNODE1(dp);
920 else if (cnp->cn_flags & AUDITVNODE2)
921 AUDIT_ARG_VNODE2(dp);
923 if (!(cnp->cn_flags & (LOCKPARENT | LOCKLEAF)))
925 /* XXX This should probably move to the top of function. */
926 if (cnp->cn_flags & SAVESTART)
927 panic("lookup: SAVESTART");
932 * Handle "..": five special cases.
933 * 0. If doing a capability lookup and lookup_cap_dotdot is
934 * disabled, return ENOTCAPABLE.
935 * 1. Return an error if this is the last component of
936 * the name and the operation is DELETE or RENAME.
937 * 2. If at root directory (e.g. after chroot)
938 * or at absolute root directory
939 * then ignore it so can't get out.
940 * 3. If this vnode is the root of a mounted
941 * filesystem, then replace it with the
942 * vnode which was mounted on so we take the
943 * .. in the other filesystem.
944 * 4. If the vnode is the top directory of
945 * the jail or chroot, don't let them out.
946 * 5. If doing a capability lookup and lookup_cap_dotdot is
947 * enabled, return ENOTCAPABLE if the lookup would escape
948 * from the initial file descriptor directory. Checks are
949 * done by ensuring that namei() already traversed the
950 * result of dotdot lookup.
952 if (cnp->cn_flags & ISDOTDOT) {
953 if ((ndp->ni_lcf & (NI_LCF_STRICTRELATIVE | NI_LCF_CAP_DOTDOT))
954 == NI_LCF_STRICTRELATIVE) {
956 if (KTRPOINT(curthread, KTR_CAPFAIL))
957 ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL);
962 if ((cnp->cn_flags & ISLASTCN) != 0 &&
963 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
968 for (pr = cnp->cn_cred->cr_prison; pr != NULL;
970 if (dp == pr->pr_root)
972 if (dp == ndp->ni_rootdir ||
973 dp == ndp->ni_topdir ||
976 ((dp->v_vflag & VV_ROOT) != 0 &&
977 (cnp->cn_flags & NOCROSSMOUNT) != 0)) {
983 if ((dp->v_vflag & VV_ROOT) == 0)
985 if (VN_IS_DOOMED(dp)) { /* forced unmount */
990 dp = dp->v_mount->mnt_vnodecovered;
994 compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags |
995 LK_RETRY, ISDOTDOT));
996 error = nameicap_check_dotdot(ndp, dp);
999 if (KTRPOINT(curthread, KTR_CAPFAIL))
1000 ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL);
1008 * We now have a segment name to search for, and a directory to search.
1012 error = mac_vnode_check_lookup(cnp->cn_thread->td_ucred, dp, cnp);
1018 ASSERT_VOP_LOCKED(dp, "lookup");
1020 * If we have a shared lock we may need to upgrade the lock for the
1023 if ((cnp->cn_flags & LOCKPARENT) && (cnp->cn_flags & ISLASTCN) &&
1024 dp != vp_crossmp && VOP_ISLOCKED(dp) == LK_SHARED)
1025 vn_lock(dp, LK_UPGRADE|LK_RETRY);
1026 if (VN_IS_DOOMED(dp)) {
1031 * If we're looking up the last component and we need an exclusive
1032 * lock, adjust our lkflags.
1034 if (needs_exclusive_leaf(dp->v_mount, cnp->cn_flags))
1035 cnp->cn_lkflags = LK_EXCLUSIVE;
1036 #ifdef NAMEI_DIAGNOSTIC
1037 vn_printf(dp, "lookup in ");
1039 lkflags_save = cnp->cn_lkflags;
1040 cnp->cn_lkflags = compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags,
1042 error = VOP_LOOKUP(dp, &ndp->ni_vp, cnp);
1043 cnp->cn_lkflags = lkflags_save;
1045 KASSERT(ndp->ni_vp == NULL, ("leaf should be empty"));
1046 #ifdef NAMEI_DIAGNOSTIC
1047 printf("not found\n");
1049 if ((error == ENOENT) &&
1050 (dp->v_vflag & VV_ROOT) && (dp->v_mount != NULL) &&
1051 (dp->v_mount->mnt_flag & MNT_UNION)) {
1053 dp = dp->v_mount->mnt_vnodecovered;
1057 compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags |
1058 LK_RETRY, cnp->cn_flags));
1059 nameicap_tracker_add(ndp, dp);
1063 if (error == ERELOOKUP) {
1071 if (error != EJUSTRETURN)
1074 * At this point, we know we're at the end of the
1075 * pathname. If creating / renaming, we can consider
1076 * allowing the file or directory to be created / renamed,
1077 * provided we're not on a read-only filesystem.
1083 /* trailing slash only allowed for directories */
1084 if ((cnp->cn_flags & TRAILINGSLASH) &&
1085 !(cnp->cn_flags & WILLBEDIR)) {
1089 if ((cnp->cn_flags & LOCKPARENT) == 0)
1092 * We return with ni_vp NULL to indicate that the entry
1093 * doesn't currently exist, leaving a pointer to the
1094 * (possibly locked) directory vnode in ndp->ni_dvp.
1096 if (cnp->cn_flags & SAVESTART) {
1097 ndp->ni_startdir = ndp->ni_dvp;
1098 VREF(ndp->ni_startdir);
1104 #ifdef NAMEI_DIAGNOSTIC
1110 * Check to see if the vnode has been mounted on;
1111 * if so find the root of the mounted filesystem.
1113 while (dp->v_type == VDIR && (mp = dp->v_mountedhere) &&
1114 (cnp->cn_flags & NOCROSSMOUNT) == 0) {
1115 if (vfs_busy(mp, 0))
1118 if (dp != ndp->ni_dvp)
1122 vrefact(vp_crossmp);
1123 ndp->ni_dvp = vp_crossmp;
1124 error = VFS_ROOT(mp, compute_cn_lkflags(mp, cnp->cn_lkflags,
1125 cnp->cn_flags), &tdp);
1127 if (vn_lock(vp_crossmp, LK_SHARED | LK_NOWAIT))
1128 panic("vp_crossmp exclusively locked or reclaimed");
1133 ndp->ni_vp = dp = tdp;
1137 * Check for symbolic link
1139 if ((dp->v_type == VLNK) &&
1140 ((cnp->cn_flags & FOLLOW) || (cnp->cn_flags & TRAILINGSLASH) ||
1141 *ndp->ni_next == '/')) {
1142 cnp->cn_flags |= ISSYMLINK;
1143 if (VN_IS_DOOMED(dp)) {
1145 * We can't know whether the directory was mounted with
1146 * NOSYMFOLLOW, so we can't follow safely.
1151 if (dp->v_mount->mnt_flag & MNT_NOSYMFOLLOW) {
1156 * Symlink code always expects an unlocked dvp.
1158 if (ndp->ni_dvp != ndp->ni_vp) {
1159 VOP_UNLOCK(ndp->ni_dvp);
1160 ni_dvp_unlocked = 1;
1167 * Not a symbolic link that we will follow. Continue with the
1168 * next component if there is any; otherwise, we're done.
1170 KASSERT((cnp->cn_flags & ISLASTCN) || *ndp->ni_next == '/',
1171 ("lookup: invalid path state."));
1174 ndp->ni_pathlen = prev_ni_pathlen;
1175 ndp->ni_next = prev_ni_next;
1176 if (ndp->ni_dvp != dp)
1182 if (cnp->cn_flags & ISDOTDOT) {
1183 error = nameicap_check_dotdot(ndp, ndp->ni_vp);
1186 if (KTRPOINT(curthread, KTR_CAPFAIL))
1187 ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL);
1192 if (*ndp->ni_next == '/') {
1193 cnp->cn_nameptr = ndp->ni_next;
1194 while (*cnp->cn_nameptr == '/') {
1198 if (ndp->ni_dvp != dp)
1205 * If we're processing a path with a trailing slash,
1206 * check that the end result is a directory.
1208 if ((cnp->cn_flags & TRAILINGSLASH) && dp->v_type != VDIR) {
1213 * Disallow directory write attempts on read-only filesystems.
1216 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
1220 if (cnp->cn_flags & SAVESTART) {
1221 ndp->ni_startdir = ndp->ni_dvp;
1222 VREF(ndp->ni_startdir);
1225 ni_dvp_unlocked = 2;
1226 if (ndp->ni_dvp != dp)
1230 } else if ((cnp->cn_flags & LOCKPARENT) == 0 && ndp->ni_dvp != dp) {
1231 VOP_UNLOCK(ndp->ni_dvp);
1232 ni_dvp_unlocked = 1;
1235 if (cnp->cn_flags & AUDITVNODE1)
1236 AUDIT_ARG_VNODE1(dp);
1237 else if (cnp->cn_flags & AUDITVNODE2)
1238 AUDIT_ARG_VNODE2(dp);
1240 if ((cnp->cn_flags & LOCKLEAF) == 0)
1244 * FIXME: for lookups which only cross a mount point to fetch the
1245 * root vnode, ni_dvp will be set to vp_crossmp. This can be a problem
1246 * if either WANTPARENT or LOCKPARENT is set.
1249 * Because of shared lookup we may have the vnode shared locked, but
1250 * the caller may want it to be exclusively locked.
1252 if (needs_exclusive_leaf(dp->v_mount, cnp->cn_flags) &&
1253 VOP_ISLOCKED(dp) != LK_EXCLUSIVE) {
1254 vn_lock(dp, LK_UPGRADE | LK_RETRY);
1255 if (VN_IS_DOOMED(dp)) {
1260 if (ndp->ni_vp != NULL) {
1261 nameicap_tracker_add(ndp, ndp->ni_vp);
1262 if ((cnp->cn_flags & (FAILIFEXISTS | ISSYMLINK)) == FAILIFEXISTS)
1268 if (ni_dvp_unlocked != 2) {
1269 if (dp != ndp->ni_dvp && !ni_dvp_unlocked)
1281 * FAILIFEXISTS handling.
1283 * XXX namei called with LOCKPARENT but not LOCKLEAF has the strange
1284 * behaviour of leaving the vnode unlocked if the target is the same
1285 * vnode as the parent.
1287 MPASS((cnp->cn_flags & ISSYMLINK) == 0);
1288 if (ndp->ni_vp == ndp->ni_dvp)
1295 NDFREE(ndp, NDF_ONLY_PNBUF);
1300 * relookup - lookup a path name component
1301 * Used by lookup to re-acquire things.
1304 relookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp)
1306 struct vnode *dp = NULL; /* the directory we are searching */
1307 int rdonly; /* lookup read-only flag bit */
1310 KASSERT(cnp->cn_flags & ISLASTCN,
1311 ("relookup: Not given last component."));
1313 * Setup: break out flag bits into variables.
1315 KASSERT((cnp->cn_flags & (LOCKPARENT | WANTPARENT)) != 0,
1316 ("relookup: parent not wanted"));
1317 rdonly = cnp->cn_flags & RDONLY;
1318 cnp->cn_flags &= ~ISSYMLINK;
1320 cnp->cn_lkflags = LK_EXCLUSIVE;
1321 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
1324 * Search a new directory.
1326 * The last component of the filename is left accessible via
1327 * cnp->cn_nameptr for callers that need the name. Callers needing
1328 * the name set the SAVENAME flag. When done, they assume
1329 * responsibility for freeing the pathname buffer.
1331 #ifdef NAMEI_DIAGNOSTIC
1332 printf("{%s}: ", cnp->cn_nameptr);
1336 * Check for "" which represents the root directory after slash
1339 if (cnp->cn_nameptr[0] == '\0') {
1341 * Support only LOOKUP for "/" because lookup()
1342 * can't succeed for CREATE, DELETE and RENAME.
1344 KASSERT(cnp->cn_nameiop == LOOKUP, ("nameiop must be LOOKUP"));
1345 KASSERT(dp->v_type == VDIR, ("dp is not a directory"));
1347 if (!(cnp->cn_flags & LOCKLEAF))
1350 /* XXX This should probably move to the top of function. */
1351 if (cnp->cn_flags & SAVESTART)
1352 panic("lookup: SAVESTART");
1356 if (cnp->cn_flags & ISDOTDOT)
1357 panic ("relookup: lookup on dot-dot");
1360 * We now have a segment name to search for, and a directory to search.
1362 #ifdef NAMEI_DIAGNOSTIC
1363 vn_printf(dp, "search in ");
1365 if ((error = VOP_LOOKUP(dp, vpp, cnp)) != 0) {
1366 KASSERT(*vpp == NULL, ("leaf should be empty"));
1367 if (error != EJUSTRETURN)
1370 * If creating and at end of pathname, then can consider
1371 * allowing file to be created.
1377 /* ASSERT(dvp == ndp->ni_startdir) */
1378 if (cnp->cn_flags & SAVESTART)
1380 if ((cnp->cn_flags & LOCKPARENT) == 0)
1383 * We return with ni_vp NULL to indicate that the entry
1384 * doesn't currently exist, leaving a pointer to the
1385 * (possibly locked) directory vnode in ndp->ni_dvp.
1393 * Disallow directory write attempts on read-only filesystems.
1396 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
1405 * Set the parent lock/ref state to the requested state.
1407 if ((cnp->cn_flags & LOCKPARENT) == 0 && dvp != dp)
1410 * Check for symbolic link
1412 KASSERT(dp->v_type != VLNK || !(cnp->cn_flags & FOLLOW),
1413 ("relookup: symlink found.\n"));
1415 /* ASSERT(dvp == ndp->ni_startdir) */
1416 if (cnp->cn_flags & SAVESTART)
1419 if ((cnp->cn_flags & LOCKLEAF) == 0)
1429 * Free data allocated by namei(); see namei(9) for details.
1432 NDFREE_PNBUF(struct nameidata *ndp)
1435 if ((ndp->ni_cnd.cn_flags & HASBUF) != 0) {
1436 MPASS((ndp->ni_cnd.cn_flags & (SAVENAME | SAVESTART)) != 0);
1437 uma_zfree(namei_zone, ndp->ni_cnd.cn_pnbuf);
1438 ndp->ni_cnd.cn_flags &= ~HASBUF;
1443 * NDFREE_PNBUF replacement for callers that know there is no buffer.
1445 * This is a hack. Preferably the VFS layer would not produce anything more
1446 * than it was asked to do. Unfortunately several non-LOOKUP cases can add the
1447 * HASBUF flag to the result. Even then an interface could be implemented where
1448 * the caller specifies what they expected to see in the result and what they
1449 * are going to take care of.
1451 * In the meantime provide this kludge as a trivial replacement for NDFREE_PNBUF
1452 * calls scattered throughout the kernel where we know for a fact the flag must not
1457 NDFREE_NOTHING(struct nameidata *ndp)
1459 struct componentname *cnp;
1462 KASSERT(cnp->cn_nameiop == LOOKUP, ("%s: got non-LOOKUP op %d\n",
1463 __func__, cnp->cn_nameiop));
1464 KASSERT((cnp->cn_flags & (SAVENAME | HASBUF)) == 0,
1465 ("%s: bad flags \%" PRIx64 "\n", __func__, cnp->cn_flags));
1470 (NDFREE)(struct nameidata *ndp, const u_int flags)
1478 if (!(flags & NDF_NO_FREE_PNBUF)) {
1481 if (!(flags & NDF_NO_VP_UNLOCK) &&
1482 (ndp->ni_cnd.cn_flags & LOCKLEAF) && ndp->ni_vp)
1484 if (!(flags & NDF_NO_DVP_UNLOCK) &&
1485 (ndp->ni_cnd.cn_flags & LOCKPARENT) &&
1486 ndp->ni_dvp != ndp->ni_vp)
1488 if (!(flags & NDF_NO_VP_RELE) && ndp->ni_vp) {
1497 VOP_UNLOCK(ndp->ni_vp);
1498 if (!(flags & NDF_NO_DVP_RELE) &&
1499 (ndp->ni_cnd.cn_flags & (LOCKPARENT|WANTPARENT))) {
1508 VOP_UNLOCK(ndp->ni_dvp);
1509 if (!(flags & NDF_NO_STARTDIR_RELE) &&
1510 (ndp->ni_cnd.cn_flags & SAVESTART)) {
1511 vrele(ndp->ni_startdir);
1512 ndp->ni_startdir = NULL;
1518 * Validate the final state of ndp after the lookup.
1520 * Historically filesystems were allowed to modify cn_flags. Most notably they
1521 * can add SAVENAME to the request, resulting in HASBUF and pushing subsequent
1522 * clean up to the consumer. In practice this seems to only concern != LOOKUP
1525 * As a step towards stricter API contract this routine validates the state to
1526 * clean up. Note validation is a work in progress with the intent of becoming
1527 * stricter over time.
1529 #define NDMODIFYINGFLAGS (LOCKLEAF | LOCKPARENT | WANTPARENT | SAVENAME | SAVESTART | HASBUF)
1531 NDVALIDATE(struct nameidata *ndp)
1533 struct componentname *cnp;
1534 u_int64_t used, orig;
1537 orig = cnp->cn_origflags;
1538 used = cnp->cn_flags;
1539 switch (cnp->cn_nameiop) {
1542 * For plain lookup we require strict conformance -- nothing
1543 * to clean up if it was not requested by the caller.
1545 orig &= NDMODIFYINGFLAGS;
1546 used &= NDMODIFYINGFLAGS;
1547 if ((orig & (SAVENAME | SAVESTART)) != 0)
1557 * Some filesystems set SAVENAME to provoke HASBUF, accomodate
1558 * for it until it gets fixed.
1560 orig &= NDMODIFYINGFLAGS;
1561 orig |= (SAVENAME | HASBUF);
1562 used &= NDMODIFYINGFLAGS;
1563 used |= (SAVENAME | HASBUF);
1571 panic("%s: mismatched flags for op %d: added %" PRIx64 ", "
1572 "removed %" PRIx64" (%" PRIx64" != %" PRIx64"; stored %" PRIx64" != %" PRIx64")",
1573 __func__, cnp->cn_nameiop, used & ~orig, orig &~ used,
1574 orig, used, cnp->cn_origflags, cnp->cn_flags);
1579 * Determine if there is a suitable alternate filename under the specified
1580 * prefix for the specified path. If the create flag is set, then the
1581 * alternate prefix will be used so long as the parent directory exists.
1582 * This is used by the various compatibility ABIs so that Linux binaries prefer
1583 * files under /compat/linux for example. The chosen path (whether under
1584 * the prefix or under /) is returned in a kernel malloc'd buffer pointed
1585 * to by pathbuf. The caller is responsible for free'ing the buffer from
1586 * the M_TEMP bucket if one is returned.
1589 kern_alternate_path(struct thread *td, const char *prefix, const char *path,
1590 enum uio_seg pathseg, char **pathbuf, int create, int dirfd)
1592 struct nameidata nd, ndroot;
1593 char *ptr, *buf, *cp;
1597 buf = (char *) malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1600 /* Copy the prefix into the new pathname as a starting point. */
1601 len = strlcpy(buf, prefix, MAXPATHLEN);
1602 if (len >= MAXPATHLEN) {
1607 sz = MAXPATHLEN - len;
1610 /* Append the filename to the prefix. */
1611 if (pathseg == UIO_SYSSPACE)
1612 error = copystr(path, ptr, sz, &len);
1614 error = copyinstr(path, ptr, sz, &len);
1622 /* Only use a prefix with absolute pathnames. */
1628 if (dirfd != AT_FDCWD) {
1630 * We want the original because the "prefix" is
1631 * included in the already opened dirfd.
1633 bcopy(ptr, buf, len);
1638 * We know that there is a / somewhere in this pathname.
1639 * Search backwards for it, to find the file's parent dir
1640 * to see if it exists in the alternate tree. If it does,
1641 * and we want to create a file (cflag is set). We don't
1642 * need to worry about the root comparison in this case.
1646 for (cp = &ptr[len] - 1; *cp != '/'; cp--);
1649 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, buf, td);
1655 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, buf, td);
1662 * We now compare the vnode of the prefix to the one
1663 * vnode asked. If they resolve to be the same, then we
1664 * ignore the match so that the real root gets used.
1665 * This avoids the problem of traversing "../.." to find the
1666 * root directory and never finding it, because "/" resolves
1667 * to the emulation root directory. This is expensive :-(
1669 NDINIT(&ndroot, LOOKUP, FOLLOW, UIO_SYSSPACE, prefix,
1672 /* We shouldn't ever get an error from this namei(). */
1673 error = namei(&ndroot);
1675 if (nd.ni_vp == ndroot.ni_vp)
1678 NDFREE(&ndroot, NDF_ONLY_PNBUF);
1679 vrele(ndroot.ni_vp);
1683 NDFREE(&nd, NDF_ONLY_PNBUF);
1687 /* If there was an error, use the original path name. */
1689 bcopy(ptr, buf, len);