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/dirent.h>
48 #include <sys/kernel.h>
49 #include <sys/capsicum.h>
50 #include <sys/fcntl.h>
53 #include <sys/mutex.h>
54 #include <sys/namei.h>
55 #include <sys/vnode.h>
56 #include <sys/mount.h>
57 #include <sys/filedesc.h>
60 #include <sys/syscallsubr.h>
61 #include <sys/sysctl.h>
63 #include <sys/ktrace.h>
66 #include <machine/_inttypes.h>
69 #include <security/audit/audit.h>
70 #include <security/mac/mac_framework.h>
74 #define NAMEI_DIAGNOSTIC 1
75 #undef NAMEI_DIAGNOSTIC
77 SDT_PROVIDER_DEFINE(vfs);
78 SDT_PROBE_DEFINE4(vfs, namei, lookup, entry, "struct vnode *", "char *",
79 "unsigned long", "bool");
80 SDT_PROBE_DEFINE4(vfs, namei, lookup, return, "int", "struct vnode *", "bool",
83 /* Allocation zone for namei. */
84 uma_zone_t namei_zone;
86 /* Placeholder vnode for mp traversal. */
87 static struct vnode *vp_crossmp;
90 crossmp_vop_islocked(struct vop_islocked_args *ap)
97 crossmp_vop_lock1(struct vop_lock1_args *ap)
100 struct lock *lk __unused;
101 const char *file __unused;
102 int flags, line __unused;
110 if ((flags & LK_SHARED) == 0)
111 panic("invalid lock request for crossmp");
113 WITNESS_CHECKORDER(&lk->lock_object, LOP_NEWORDER, file, line,
114 flags & LK_INTERLOCK ? &VI_MTX(vp)->lock_object : NULL);
115 WITNESS_LOCK(&lk->lock_object, 0, file, line);
116 if ((flags & LK_INTERLOCK) != 0)
118 LOCK_LOG_LOCK("SLOCK", &lk->lock_object, 0, 0, ap->a_file, line);
123 crossmp_vop_unlock(struct vop_unlock_args *ap)
126 struct lock *lk __unused;
131 WITNESS_UNLOCK(&lk->lock_object, 0, LOCK_FILE, LOCK_LINE);
132 LOCK_LOG_LOCK("SUNLOCK", &lk->lock_object, 0, 0, LOCK_FILE,
137 static struct vop_vector crossmp_vnodeops = {
138 .vop_default = &default_vnodeops,
139 .vop_islocked = crossmp_vop_islocked,
140 .vop_lock1 = crossmp_vop_lock1,
141 .vop_unlock = crossmp_vop_unlock,
144 * VFS_VOP_VECTOR_REGISTER(crossmp_vnodeops) is not used here since the vnode
145 * gets allocated early. See nameiinit for the direct call below.
148 struct nameicap_tracker {
150 TAILQ_ENTRY(nameicap_tracker) nm_link;
153 /* Zone for cap mode tracker elements used for dotdot capability checks. */
154 MALLOC_DEFINE(M_NAMEITRACKER, "namei_tracker", "namei tracking for dotdot");
157 nameiinit(void *dummy __unused)
160 namei_zone = uma_zcreate("NAMEI", MAXPATHLEN, NULL, NULL, NULL, NULL,
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 nt = TAILQ_LAST(&ndp->ni_cap_tracker, nameicap_tracker_head);
187 if (nt != NULL && nt->dp == dp)
189 nt = malloc(sizeof(*nt), M_NAMEITRACKER, M_WAITOK);
192 TAILQ_INSERT_TAIL(&ndp->ni_cap_tracker, nt, nm_link);
196 nameicap_cleanup_from(struct nameidata *ndp, struct nameicap_tracker *first)
198 struct nameicap_tracker *nt, *nt1;
201 TAILQ_FOREACH_FROM_SAFE(nt, &ndp->ni_cap_tracker, nm_link, nt1) {
202 TAILQ_REMOVE(&ndp->ni_cap_tracker, nt, nm_link);
204 free(nt, M_NAMEITRACKER);
209 nameicap_cleanup(struct nameidata *ndp)
211 KASSERT(TAILQ_EMPTY(&ndp->ni_cap_tracker) ||
212 (ndp->ni_lcf & NI_LCF_CAP_DOTDOT) != 0, ("not strictrelative"));
213 nameicap_cleanup_from(ndp, NULL);
217 * For dotdot lookups in capability mode, only allow the component
218 * lookup to succeed if the resulting directory was already traversed
219 * during the operation. This catches situations where already
220 * traversed directory is moved to different parent, and then we walk
221 * over it with dotdots.
223 * Also allow to force failure of dotdot lookups for non-local
224 * filesystems, where external agents might assist local lookups to
225 * escape the compartment.
228 nameicap_check_dotdot(struct nameidata *ndp, struct vnode *dp)
230 struct nameicap_tracker *nt;
233 if (dp == NULL || dp->v_type != VDIR || (ndp->ni_lcf &
234 NI_LCF_STRICTRELATIVE) == 0)
236 if ((ndp->ni_lcf & NI_LCF_CAP_DOTDOT) == 0)
237 return (ENOTCAPABLE);
239 if (lookup_cap_dotdot_nonlocal == 0 && mp != NULL &&
240 (mp->mnt_flag & MNT_LOCAL) == 0)
241 return (ENOTCAPABLE);
242 TAILQ_FOREACH_REVERSE(nt, &ndp->ni_cap_tracker, nameicap_tracker_head,
245 nt = TAILQ_NEXT(nt, nm_link);
247 nameicap_cleanup_from(ndp, nt);
251 return (ENOTCAPABLE);
255 namei_cleanup_cnp(struct componentname *cnp)
258 uma_zfree(namei_zone, cnp->cn_pnbuf);
260 cnp->cn_pnbuf = NULL;
261 cnp->cn_nameptr = NULL;
266 namei_handle_root(struct nameidata *ndp, struct vnode **dpp)
268 struct componentname *cnp;
271 if ((ndp->ni_lcf & NI_LCF_STRICTRELATIVE) != 0) {
273 if (KTRPOINT(curthread, KTR_CAPFAIL))
274 ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL);
276 return (ENOTCAPABLE);
278 while (*(cnp->cn_nameptr) == '/') {
282 *dpp = ndp->ni_rootdir;
288 namei_setup(struct nameidata *ndp, struct vnode **dpp, struct pwd **pwdp)
290 struct componentname *cnp;
301 startdir_used = false;
305 #ifdef CAPABILITY_MODE
307 * In capability mode, lookups must be restricted to happen in
308 * the subtree with the root specified by the file descriptor:
309 * - The root must be real file descriptor, not the pseudo-descriptor
311 * - The passed path must be relative and not absolute.
312 * - If lookup_cap_dotdot is disabled, path must not contain the
314 * - If lookup_cap_dotdot is enabled, we verify that all '..'
315 * components lookups result in the directories which were
316 * previously walked by us, which prevents an escape from
319 if (IN_CAPABILITY_MODE(td) && (cnp->cn_flags & NOCAPCHECK) == 0) {
320 ndp->ni_lcf |= NI_LCF_STRICTRELATIVE;
321 ndp->ni_resflags |= NIRES_STRICTREL;
322 if (ndp->ni_dirfd == AT_FDCWD) {
324 if (KTRPOINT(td, KTR_CAPFAIL))
325 ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL);
334 * Get starting point for the translation.
338 * The reference on ni_rootdir is acquired in the block below to avoid
339 * back-to-back atomics for absolute lookups.
341 ndp->ni_rootdir = pwd->pwd_rdir;
342 ndp->ni_topdir = pwd->pwd_jdir;
344 if (cnp->cn_pnbuf[0] == '/') {
345 ndp->ni_resflags |= NIRES_ABS;
346 error = namei_handle_root(ndp, dpp);
348 if (ndp->ni_startdir != NULL) {
349 *dpp = ndp->ni_startdir;
350 startdir_used = true;
351 } else if (ndp->ni_dirfd == AT_FDCWD) {
352 *dpp = pwd->pwd_cdir;
355 rights = *ndp->ni_rightsneeded;
356 cap_rights_set_one(&rights, CAP_LOOKUP);
358 if (cnp->cn_flags & AUDITVNODE1)
359 AUDIT_ARG_ATFD1(ndp->ni_dirfd);
360 if (cnp->cn_flags & AUDITVNODE2)
361 AUDIT_ARG_ATFD2(ndp->ni_dirfd);
363 * Effectively inlined fgetvp_rights, because
364 * we need to inspect the file as well as
365 * grabbing the vnode. No check for O_PATH,
366 * files to implement its semantic.
368 error = fget_cap(td, ndp->ni_dirfd, &rights,
369 &dfp, &ndp->ni_filecaps);
372 * Preserve the error; it should either be EBADF
373 * or capability-related, both of which can be
374 * safely returned to the caller.
377 if (dfp->f_ops == &badfileops) {
379 } else if (dfp->f_vnode == NULL) {
385 if ((dfp->f_flag & FSEARCH) != 0)
386 cnp->cn_flags |= NOEXECCHECK;
392 * If file descriptor doesn't have all rights,
393 * all lookups relative to it must also be
397 if (!cap_rights_contains(&ndp->ni_filecaps.fc_rights,
399 ndp->ni_filecaps.fc_fcntls != CAP_FCNTL_ALL ||
400 ndp->ni_filecaps.fc_nioctls != -1) {
401 ndp->ni_lcf |= NI_LCF_STRICTRELATIVE;
402 ndp->ni_resflags |= NIRES_STRICTREL;
406 if (error == 0 && (*dpp)->v_type != VDIR &&
407 (cnp->cn_pnbuf[0] != '\0' ||
408 (cnp->cn_flags & EMPTYPATH) == 0))
411 if (error == 0 && (cnp->cn_flags & RBENEATH) != 0) {
412 if (cnp->cn_pnbuf[0] == '/') {
414 } else if ((ndp->ni_lcf & NI_LCF_STRICTRELATIVE) == 0) {
415 ndp->ni_lcf |= NI_LCF_STRICTRELATIVE |
421 * If we are auditing the kernel pathname, save the user pathname.
423 if (cnp->cn_flags & AUDITVNODE1)
424 AUDIT_ARG_UPATH1_VP(td, ndp->ni_rootdir, *dpp, cnp->cn_pnbuf);
425 if (cnp->cn_flags & AUDITVNODE2)
426 AUDIT_ARG_UPATH2_VP(td, ndp->ni_rootdir, *dpp, cnp->cn_pnbuf);
427 if (ndp->ni_startdir != NULL && !startdir_used)
428 vrele(ndp->ni_startdir);
435 if ((ndp->ni_lcf & NI_LCF_STRICTRELATIVE) != 0 &&
436 lookup_cap_dotdot != 0)
437 ndp->ni_lcf |= NI_LCF_CAP_DOTDOT;
438 SDT_PROBE4(vfs, namei, lookup, entry, *dpp, cnp->cn_pnbuf,
439 cnp->cn_flags, false);
445 namei_getpath(struct nameidata *ndp)
447 struct componentname *cnp;
453 * Get a buffer for the name to be translated, and copy the
454 * name into the buffer.
456 cnp->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK);
457 if (ndp->ni_segflg == UIO_SYSSPACE) {
458 error = copystr(ndp->ni_dirp, cnp->cn_pnbuf, MAXPATHLEN,
461 error = copyinstr(ndp->ni_dirp, cnp->cn_pnbuf, MAXPATHLEN,
465 if (__predict_false(error != 0))
469 * Don't allow empty pathnames unless EMPTYPATH is specified.
470 * Caller checks for ENOENT as an indication for the empty path.
472 if (__predict_false(*cnp->cn_pnbuf == '\0'))
475 cnp->cn_nameptr = cnp->cn_pnbuf;
480 namei_emptypath(struct nameidata *ndp)
482 struct componentname *cnp;
488 MPASS(*cnp->cn_pnbuf == '\0');
489 MPASS((cnp->cn_flags & EMPTYPATH) != 0);
490 MPASS((cnp->cn_flags & (LOCKPARENT | WANTPARENT)) == 0);
492 error = namei_setup(ndp, &dp, &pwd);
494 namei_cleanup_cnp(cnp);
499 * Usecount on dp already provided by namei_setup.
502 namei_cleanup_cnp(cnp);
504 ndp->ni_resflags |= NIRES_EMPTYPATH;
506 if ((cnp->cn_flags & LOCKLEAF) != 0) {
507 VOP_LOCK(dp, (cnp->cn_flags & LOCKSHARED) != 0 ?
508 LK_SHARED : LK_EXCLUSIVE);
509 if (VN_IS_DOOMED(dp)) {
515 SDT_PROBE4(vfs, namei, lookup, return, 0, ndp->ni_vp, false, ndp);
519 SDT_PROBE4(vfs, namei, lookup, return, error, NULL, false, ndp);
524 * Convert a pathname into a pointer to a locked vnode.
526 * The FOLLOW flag is set when symbolic links are to be followed
527 * when they occur at the end of the name translation process.
528 * Symbolic links are always followed for all other pathname
529 * components other than the last.
531 * The segflg defines whether the name is to be copied from user
532 * space or kernel space.
534 * Overall outline of namei:
537 * get starting directory
538 * while (!done && !error) {
539 * call lookup to search path.
540 * if symbolic link, massage name in buffer and continue
544 namei(struct nameidata *ndp)
546 char *cp; /* pointer into pathname argument */
547 struct vnode *dp; /* the directory we are searching */
548 struct iovec aiov; /* uio for reading symbolic links */
549 struct componentname *cnp;
554 enum cache_fpl_status status;
559 KASSERT((ndp->ni_debugflags & NAMEI_DBG_CALLED) == 0,
560 ("%s: repeated call to namei without NDREINIT", __func__));
561 KASSERT(ndp->ni_debugflags == NAMEI_DBG_INITED,
562 ("%s: bad debugflags %d", __func__, ndp->ni_debugflags));
563 ndp->ni_debugflags |= NAMEI_DBG_CALLED;
564 if (ndp->ni_startdir != NULL)
565 ndp->ni_debugflags |= NAMEI_DBG_HADSTARTDIR;
566 if (cnp->cn_flags & FAILIFEXISTS) {
567 KASSERT(cnp->cn_nameiop == CREATE,
568 ("%s: FAILIFEXISTS passed for op %d", __func__, cnp->cn_nameiop));
570 * The limitation below is to restrict hairy corner cases.
572 KASSERT((cnp->cn_flags & (LOCKPARENT | LOCKLEAF)) == LOCKPARENT,
573 ("%s: FAILIFEXISTS must be passed with LOCKPARENT and without LOCKLEAF",
579 * While NDINIT may seem like a more natural place to do it, there are
580 * callers which directly modify flags past invoking init.
582 cnp->cn_origflags = cnp->cn_flags;
584 ndp->ni_cnd.cn_cred = ndp->ni_cnd.cn_thread->td_ucred;
585 KASSERT(ndp->ni_resflags == 0, ("%s: garbage in ni_resflags: %x\n",
586 __func__, ndp->ni_resflags));
587 KASSERT(cnp->cn_cred && td->td_proc, ("namei: bad cred/proc"));
588 KASSERT((cnp->cn_flags & NAMEI_INTERNAL_FLAGS) == 0,
589 ("namei: unexpected flags: %" PRIx64 "\n",
590 cnp->cn_flags & NAMEI_INTERNAL_FLAGS));
591 if (cnp->cn_flags & NOCACHE)
592 KASSERT(cnp->cn_nameiop != LOOKUP,
593 ("%s: NOCACHE passed with LOOKUP", __func__));
594 MPASS(ndp->ni_startdir == NULL || ndp->ni_startdir->v_type == VDIR ||
595 ndp->ni_startdir->v_type == VBAD);
601 error = namei_getpath(ndp);
602 if (__predict_false(error != 0)) {
603 if (error == ENOENT && (cnp->cn_flags & EMPTYPATH) != 0)
604 return (namei_emptypath(ndp));
605 namei_cleanup_cnp(cnp);
606 SDT_PROBE4(vfs, namei, lookup, return, error, NULL,
612 if (KTRPOINT(td, KTR_NAMEI)) {
613 KASSERT(cnp->cn_thread == curthread,
614 ("namei not using curthread"));
615 ktrnamei(cnp->cn_pnbuf);
620 * First try looking up the target without locking any vnodes.
622 * We may need to start from scratch or pick up where it left off.
624 error = cache_fplookup(ndp, &status, &pwd);
626 case CACHE_FPL_STATUS_UNSET:
627 __assert_unreachable();
629 case CACHE_FPL_STATUS_HANDLED:
633 case CACHE_FPL_STATUS_PARTIAL:
634 TAILQ_INIT(&ndp->ni_cap_tracker);
635 dp = ndp->ni_startdir;
637 case CACHE_FPL_STATUS_DESTROYED:
639 error = namei_getpath(ndp);
640 if (__predict_false(error != 0)) {
641 namei_cleanup_cnp(cnp);
645 case CACHE_FPL_STATUS_ABORTED:
646 TAILQ_INIT(&ndp->ni_cap_tracker);
647 MPASS(ndp->ni_lcf == 0);
648 error = namei_setup(ndp, &dp, &pwd);
650 namei_cleanup_cnp(cnp);
660 ndp->ni_startdir = dp;
666 * If not a symbolic link, we're done.
668 if ((cnp->cn_flags & ISSYMLINK) == 0) {
669 SDT_PROBE4(vfs, namei, lookup, return, error,
670 (error == 0 ? ndp->ni_vp : NULL), false, ndp);
671 if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0) {
672 namei_cleanup_cnp(cnp);
674 cnp->cn_flags |= HASBUF;
675 nameicap_cleanup(ndp);
681 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
686 if ((cnp->cn_flags & NOMACCHECK) == 0) {
687 error = mac_vnode_check_readlink(td->td_ucred,
693 if (ndp->ni_pathlen > 1)
694 cp = uma_zalloc(namei_zone, M_WAITOK);
698 aiov.iov_len = MAXPATHLEN;
699 auio.uio_iov = &aiov;
702 auio.uio_rw = UIO_READ;
703 auio.uio_segflg = UIO_SYSSPACE;
705 auio.uio_resid = MAXPATHLEN;
706 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
708 if (ndp->ni_pathlen > 1)
709 uma_zfree(namei_zone, cp);
712 linklen = MAXPATHLEN - auio.uio_resid;
714 if (ndp->ni_pathlen > 1)
715 uma_zfree(namei_zone, cp);
719 if (linklen + ndp->ni_pathlen > MAXPATHLEN) {
720 if (ndp->ni_pathlen > 1)
721 uma_zfree(namei_zone, cp);
722 error = ENAMETOOLONG;
725 if (ndp->ni_pathlen > 1) {
726 bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
727 uma_zfree(namei_zone, cnp->cn_pnbuf);
730 cnp->cn_pnbuf[linklen] = '\0';
731 ndp->ni_pathlen += linklen;
735 * Check if root directory should replace current directory.
737 cnp->cn_nameptr = cnp->cn_pnbuf;
738 if (*(cnp->cn_nameptr) == '/') {
740 error = namei_handle_root(ndp, &dp);
750 SDT_PROBE4(vfs, namei, lookup, return, error, NULL, false, ndp);
751 namei_cleanup_cnp(cnp);
752 nameicap_cleanup(ndp);
758 compute_cn_lkflags(struct mount *mp, int lkflags, int cnflags)
761 if (mp == NULL || ((lkflags & LK_SHARED) &&
762 (!(mp->mnt_kern_flag & MNTK_LOOKUP_SHARED) ||
763 ((cnflags & ISDOTDOT) &&
764 (mp->mnt_kern_flag & MNTK_LOOKUP_EXCL_DOTDOT))))) {
765 lkflags &= ~LK_SHARED;
766 lkflags |= LK_EXCLUSIVE;
768 lkflags |= LK_NODDLKTREAT;
773 needs_exclusive_leaf(struct mount *mp, int flags)
777 * Intermediate nodes can use shared locks, we only need to
778 * force an exclusive lock for leaf nodes.
780 if ((flags & (ISLASTCN | LOCKLEAF)) != (ISLASTCN | LOCKLEAF))
783 /* Always use exclusive locks if LOCKSHARED isn't set. */
784 if (!(flags & LOCKSHARED))
788 * For lookups during open(), if the mount point supports
789 * extended shared operations, then use a shared lock for the
790 * leaf node, otherwise use an exclusive lock.
792 if ((flags & ISOPEN) != 0)
793 return (!MNT_EXTENDED_SHARED(mp));
796 * Lookup requests outside of open() that specify LOCKSHARED
797 * only need a shared lock on the leaf vnode.
803 * Various filesystems expect to be able to copy a name component with length
804 * bounded by NAME_MAX into a directory entry buffer of size MAXNAMLEN. Make
805 * sure that these are the same size.
807 _Static_assert(MAXNAMLEN == NAME_MAX,
808 "MAXNAMLEN and NAME_MAX have different values");
812 * This is a very central and rather complicated routine.
814 * The pathname is pointed to by ni_ptr and is of length ni_pathlen.
815 * The starting directory is taken from ni_startdir. The pathname is
816 * descended until done, or a symbolic link is encountered. The variable
817 * ni_more is clear if the path is completed; it is set to one if a
818 * symbolic link needing interpretation is encountered.
820 * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
821 * whether the name is to be looked up, created, renamed, or deleted.
822 * When CREATE, RENAME, or DELETE is specified, information usable in
823 * creating, renaming, or deleting a directory entry may be calculated.
824 * If flag has LOCKPARENT or'ed into it, the parent directory is returned
825 * locked. If flag has WANTPARENT or'ed into it, the parent directory is
826 * returned unlocked. Otherwise the parent directory is not returned. If
827 * the target of the pathname exists and LOCKLEAF is or'ed into the flag
828 * the target is returned locked, otherwise it is returned unlocked.
829 * When creating or renaming and LOCKPARENT is specified, the target may not
830 * be ".". When deleting and LOCKPARENT is specified, the target may be ".".
832 * Overall outline of lookup:
835 * identify next component of name at ndp->ni_ptr
836 * handle degenerate case where name is null string
837 * if .. and crossing mount points and on mounted filesys, find parent
838 * call VOP_LOOKUP routine for next component name
839 * directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set
840 * component vnode returned in ni_vp (if it exists), locked.
841 * if result vnode is mounted on and crossing mount points,
842 * find mounted on vnode
843 * if more components of name, do next level at dirloop
844 * return the answer in ni_vp, locked if LOCKLEAF set
845 * if LOCKPARENT set, return locked parent in ni_dvp
846 * if WANTPARENT set, return unlocked parent in ni_dvp
849 lookup(struct nameidata *ndp)
851 char *cp; /* pointer into pathname argument */
852 char *prev_ni_next; /* saved ndp->ni_next */
853 struct vnode *dp = NULL; /* the directory we are searching */
854 struct vnode *tdp; /* saved dp */
855 struct mount *mp; /* mount table entry */
857 size_t prev_ni_pathlen; /* saved ndp->ni_pathlen */
858 int docache; /* == 0 do not cache last component */
859 int wantparent; /* 1 => wantparent or lockparent flag */
860 int rdonly; /* lookup read-only flag bit */
862 int dpunlocked = 0; /* dp has already been unlocked */
863 int relookup = 0; /* do not consume the path component */
864 struct componentname *cnp = &ndp->ni_cnd;
869 * Setup: break out flag bits into variables.
872 wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT);
873 KASSERT(cnp->cn_nameiop == LOOKUP || wantparent,
874 ("CREATE, DELETE, RENAME require LOCKPARENT or WANTPARENT."));
876 * When set to zero, docache causes the last component of the
877 * pathname to be deleted from the cache and the full lookup
878 * of the name to be done (via VOP_CACHEDLOOKUP()). Often
879 * filesystems need some pre-computed values that are made
880 * during the full lookup, for instance UFS sets dp->i_offset.
882 * The docache variable is set to zero when requested by the
883 * NOCACHE flag and for all modifying operations except CREATE.
885 docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE;
886 if (cnp->cn_nameiop == DELETE ||
887 (wantparent && cnp->cn_nameiop != CREATE &&
888 cnp->cn_nameiop != LOOKUP))
890 rdonly = cnp->cn_flags & RDONLY;
891 cnp->cn_flags &= ~ISSYMLINK;
894 * We use shared locks until we hit the parent of the last cn then
895 * we adjust based on the requesting flags.
897 cnp->cn_lkflags = LK_SHARED;
898 dp = ndp->ni_startdir;
899 ndp->ni_startdir = NULLVP;
901 compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags | LK_RETRY,
906 * Search a new directory.
908 * The last component of the filename is left accessible via
909 * cnp->cn_nameptr for callers that need the name. Callers needing
910 * the name set the SAVENAME flag. When done, they assume
911 * responsibility for freeing the pathname buffer.
913 for (cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++)
915 cnp->cn_namelen = cp - cnp->cn_nameptr;
916 if (cnp->cn_namelen > NAME_MAX) {
917 error = ENAMETOOLONG;
920 #ifdef NAMEI_DIAGNOSTIC
923 printf("{%s}: ", cnp->cn_nameptr);
926 prev_ni_pathlen = ndp->ni_pathlen;
927 ndp->ni_pathlen -= cnp->cn_namelen;
928 KASSERT(ndp->ni_pathlen <= PATH_MAX,
929 ("%s: ni_pathlen underflow to %zd\n", __func__, ndp->ni_pathlen));
930 prev_ni_next = ndp->ni_next;
934 * Replace multiple slashes by a single slash and trailing slashes
935 * by a null. This must be done before VOP_LOOKUP() because some
936 * fs's don't know about trailing slashes. Remember if there were
937 * trailing slashes to handle symlinks, existing non-directories
938 * and non-existing files that won't be directories specially later.
940 while (*cp == '/' && (cp[1] == '/' || cp[1] == '\0')) {
944 *ndp->ni_next = '\0';
945 cnp->cn_flags |= TRAILINGSLASH;
950 cnp->cn_flags |= MAKEENTRY;
951 if (*cp == '\0' && docache == 0)
952 cnp->cn_flags &= ~MAKEENTRY;
953 if (cnp->cn_namelen == 2 &&
954 cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.')
955 cnp->cn_flags |= ISDOTDOT;
957 cnp->cn_flags &= ~ISDOTDOT;
958 if (*ndp->ni_next == 0)
959 cnp->cn_flags |= ISLASTCN;
961 cnp->cn_flags &= ~ISLASTCN;
963 if ((cnp->cn_flags & ISLASTCN) != 0 &&
964 cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.' &&
965 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
970 nameicap_tracker_add(ndp, dp);
973 * Check for degenerate name (e.g. / or "")
974 * which is a way of talking about a directory,
975 * e.g. like "/." or ".".
977 if (cnp->cn_nameptr[0] == '\0') {
978 if (dp->v_type != VDIR) {
982 if (cnp->cn_nameiop != LOOKUP) {
992 if (cnp->cn_flags & AUDITVNODE1)
993 AUDIT_ARG_VNODE1(dp);
994 else if (cnp->cn_flags & AUDITVNODE2)
995 AUDIT_ARG_VNODE2(dp);
997 if (!(cnp->cn_flags & (LOCKPARENT | LOCKLEAF)))
999 /* XXX This should probably move to the top of function. */
1000 if (cnp->cn_flags & SAVESTART)
1001 panic("lookup: SAVESTART");
1006 * Handle "..": five special cases.
1007 * 0. If doing a capability lookup and lookup_cap_dotdot is
1008 * disabled, return ENOTCAPABLE.
1009 * 1. Return an error if this is the last component of
1010 * the name and the operation is DELETE or RENAME.
1011 * 2. If at root directory (e.g. after chroot)
1012 * or at absolute root directory
1013 * then ignore it so can't get out.
1014 * 3. If this vnode is the root of a mounted
1015 * filesystem, then replace it with the
1016 * vnode which was mounted on so we take the
1017 * .. in the other filesystem.
1018 * 4. If the vnode is the top directory of
1019 * the jail or chroot, don't let them out.
1020 * 5. If doing a capability lookup and lookup_cap_dotdot is
1021 * enabled, return ENOTCAPABLE if the lookup would escape
1022 * from the initial file descriptor directory. Checks are
1023 * done by ensuring that namei() already traversed the
1024 * result of dotdot lookup.
1026 if (cnp->cn_flags & ISDOTDOT) {
1027 if ((ndp->ni_lcf & (NI_LCF_STRICTRELATIVE | NI_LCF_CAP_DOTDOT))
1028 == NI_LCF_STRICTRELATIVE) {
1030 if (KTRPOINT(curthread, KTR_CAPFAIL))
1031 ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL);
1033 error = ENOTCAPABLE;
1036 if ((cnp->cn_flags & ISLASTCN) != 0 &&
1037 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
1042 for (pr = cnp->cn_cred->cr_prison; pr != NULL;
1044 if (dp == pr->pr_root)
1046 if (dp == ndp->ni_rootdir ||
1047 dp == ndp->ni_topdir ||
1050 ((dp->v_vflag & VV_ROOT) != 0 &&
1051 (cnp->cn_flags & NOCROSSMOUNT) != 0)) {
1057 if ((dp->v_vflag & VV_ROOT) == 0)
1059 if (VN_IS_DOOMED(dp)) { /* forced unmount */
1064 dp = dp->v_mount->mnt_vnodecovered;
1068 compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags |
1069 LK_RETRY, ISDOTDOT));
1070 error = nameicap_check_dotdot(ndp, dp);
1073 if (KTRPOINT(curthread, KTR_CAPFAIL))
1074 ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL);
1082 * We now have a segment name to search for, and a directory to search.
1086 error = mac_vnode_check_lookup(cnp->cn_thread->td_ucred, dp, cnp);
1092 ASSERT_VOP_LOCKED(dp, "lookup");
1094 * If we have a shared lock we may need to upgrade the lock for the
1097 if ((cnp->cn_flags & LOCKPARENT) && (cnp->cn_flags & ISLASTCN) &&
1098 dp != vp_crossmp && VOP_ISLOCKED(dp) == LK_SHARED)
1099 vn_lock(dp, LK_UPGRADE|LK_RETRY);
1100 if (VN_IS_DOOMED(dp)) {
1105 * If we're looking up the last component and we need an exclusive
1106 * lock, adjust our lkflags.
1108 if (needs_exclusive_leaf(dp->v_mount, cnp->cn_flags))
1109 cnp->cn_lkflags = LK_EXCLUSIVE;
1110 #ifdef NAMEI_DIAGNOSTIC
1111 vn_printf(dp, "lookup in ");
1113 lkflags_save = cnp->cn_lkflags;
1114 cnp->cn_lkflags = compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags,
1116 error = VOP_LOOKUP(dp, &ndp->ni_vp, cnp);
1117 cnp->cn_lkflags = lkflags_save;
1119 KASSERT(ndp->ni_vp == NULL, ("leaf should be empty"));
1120 #ifdef NAMEI_DIAGNOSTIC
1121 printf("not found\n");
1123 if ((error == ENOENT) &&
1124 (dp->v_vflag & VV_ROOT) && (dp->v_mount != NULL) &&
1125 (dp->v_mount->mnt_flag & MNT_UNION)) {
1127 dp = dp->v_mount->mnt_vnodecovered;
1131 compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags |
1132 LK_RETRY, cnp->cn_flags));
1133 nameicap_tracker_add(ndp, dp);
1137 if (error == ERELOOKUP) {
1145 if (error != EJUSTRETURN)
1148 * At this point, we know we're at the end of the
1149 * pathname. If creating / renaming, we can consider
1150 * allowing the file or directory to be created / renamed,
1151 * provided we're not on a read-only filesystem.
1157 /* trailing slash only allowed for directories */
1158 if ((cnp->cn_flags & TRAILINGSLASH) &&
1159 !(cnp->cn_flags & WILLBEDIR)) {
1163 if ((cnp->cn_flags & LOCKPARENT) == 0)
1166 * We return with ni_vp NULL to indicate that the entry
1167 * doesn't currently exist, leaving a pointer to the
1168 * (possibly locked) directory vnode in ndp->ni_dvp.
1170 if (cnp->cn_flags & SAVESTART) {
1171 ndp->ni_startdir = ndp->ni_dvp;
1172 VREF(ndp->ni_startdir);
1178 #ifdef NAMEI_DIAGNOSTIC
1184 * Check to see if the vnode has been mounted on;
1185 * if so find the root of the mounted filesystem.
1187 while (dp->v_type == VDIR && (mp = dp->v_mountedhere) &&
1188 (cnp->cn_flags & NOCROSSMOUNT) == 0) {
1189 if (vfs_busy(mp, 0))
1192 if (dp != ndp->ni_dvp)
1196 vrefact(vp_crossmp);
1197 ndp->ni_dvp = vp_crossmp;
1198 error = VFS_ROOT(mp, compute_cn_lkflags(mp, cnp->cn_lkflags,
1199 cnp->cn_flags), &tdp);
1201 if (vn_lock(vp_crossmp, LK_SHARED | LK_NOWAIT))
1202 panic("vp_crossmp exclusively locked or reclaimed");
1207 ndp->ni_vp = dp = tdp;
1211 * Check for symbolic link
1213 if ((dp->v_type == VLNK) &&
1214 ((cnp->cn_flags & FOLLOW) || (cnp->cn_flags & TRAILINGSLASH) ||
1215 *ndp->ni_next == '/')) {
1216 cnp->cn_flags |= ISSYMLINK;
1217 if (VN_IS_DOOMED(dp)) {
1219 * We can't know whether the directory was mounted with
1220 * NOSYMFOLLOW, so we can't follow safely.
1225 if (dp->v_mount->mnt_flag & MNT_NOSYMFOLLOW) {
1230 * Symlink code always expects an unlocked dvp.
1232 if (ndp->ni_dvp != ndp->ni_vp) {
1233 VOP_UNLOCK(ndp->ni_dvp);
1234 ni_dvp_unlocked = 1;
1241 * Not a symbolic link that we will follow. Continue with the
1242 * next component if there is any; otherwise, we're done.
1244 KASSERT((cnp->cn_flags & ISLASTCN) || *ndp->ni_next == '/',
1245 ("lookup: invalid path state."));
1248 ndp->ni_pathlen = prev_ni_pathlen;
1249 ndp->ni_next = prev_ni_next;
1250 if (ndp->ni_dvp != dp)
1256 if (cnp->cn_flags & ISDOTDOT) {
1257 error = nameicap_check_dotdot(ndp, ndp->ni_vp);
1260 if (KTRPOINT(curthread, KTR_CAPFAIL))
1261 ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL);
1266 if (*ndp->ni_next == '/') {
1267 cnp->cn_nameptr = ndp->ni_next;
1268 while (*cnp->cn_nameptr == '/') {
1272 if (ndp->ni_dvp != dp)
1279 * If we're processing a path with a trailing slash,
1280 * check that the end result is a directory.
1282 if ((cnp->cn_flags & TRAILINGSLASH) && dp->v_type != VDIR) {
1287 * Disallow directory write attempts on read-only filesystems.
1290 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
1294 if (cnp->cn_flags & SAVESTART) {
1295 ndp->ni_startdir = ndp->ni_dvp;
1296 VREF(ndp->ni_startdir);
1299 ni_dvp_unlocked = 2;
1300 if (ndp->ni_dvp != dp)
1304 } else if ((cnp->cn_flags & LOCKPARENT) == 0 && ndp->ni_dvp != dp) {
1305 VOP_UNLOCK(ndp->ni_dvp);
1306 ni_dvp_unlocked = 1;
1309 if (cnp->cn_flags & AUDITVNODE1)
1310 AUDIT_ARG_VNODE1(dp);
1311 else if (cnp->cn_flags & AUDITVNODE2)
1312 AUDIT_ARG_VNODE2(dp);
1314 if ((cnp->cn_flags & LOCKLEAF) == 0)
1318 * FIXME: for lookups which only cross a mount point to fetch the
1319 * root vnode, ni_dvp will be set to vp_crossmp. This can be a problem
1320 * if either WANTPARENT or LOCKPARENT is set.
1323 * Because of shared lookup we may have the vnode shared locked, but
1324 * the caller may want it to be exclusively locked.
1326 if (needs_exclusive_leaf(dp->v_mount, cnp->cn_flags) &&
1327 VOP_ISLOCKED(dp) != LK_EXCLUSIVE) {
1328 vn_lock(dp, LK_UPGRADE | LK_RETRY);
1329 if (VN_IS_DOOMED(dp)) {
1334 if (ndp->ni_vp != NULL) {
1335 if ((cnp->cn_flags & ISDOTDOT) == 0)
1336 nameicap_tracker_add(ndp, ndp->ni_vp);
1337 if ((cnp->cn_flags & (FAILIFEXISTS | ISSYMLINK)) == FAILIFEXISTS)
1343 if (ni_dvp_unlocked != 2) {
1344 if (dp != ndp->ni_dvp && !ni_dvp_unlocked)
1356 * FAILIFEXISTS handling.
1358 * XXX namei called with LOCKPARENT but not LOCKLEAF has the strange
1359 * behaviour of leaving the vnode unlocked if the target is the same
1360 * vnode as the parent.
1362 MPASS((cnp->cn_flags & ISSYMLINK) == 0);
1363 if (ndp->ni_vp == ndp->ni_dvp)
1370 NDFREE(ndp, NDF_ONLY_PNBUF);
1375 * relookup - lookup a path name component
1376 * Used by lookup to re-acquire things.
1379 relookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp)
1381 struct vnode *dp = NULL; /* the directory we are searching */
1382 int rdonly; /* lookup read-only flag bit */
1385 KASSERT(cnp->cn_flags & ISLASTCN,
1386 ("relookup: Not given last component."));
1388 * Setup: break out flag bits into variables.
1390 KASSERT((cnp->cn_flags & (LOCKPARENT | WANTPARENT)) != 0,
1391 ("relookup: parent not wanted"));
1392 rdonly = cnp->cn_flags & RDONLY;
1393 cnp->cn_flags &= ~ISSYMLINK;
1395 cnp->cn_lkflags = LK_EXCLUSIVE;
1396 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
1399 * Search a new directory.
1401 * The last component of the filename is left accessible via
1402 * cnp->cn_nameptr for callers that need the name. Callers needing
1403 * the name set the SAVENAME flag. When done, they assume
1404 * responsibility for freeing the pathname buffer.
1406 #ifdef NAMEI_DIAGNOSTIC
1407 printf("{%s}: ", cnp->cn_nameptr);
1411 * Check for "" which represents the root directory after slash
1414 if (cnp->cn_nameptr[0] == '\0') {
1416 * Support only LOOKUP for "/" because lookup()
1417 * can't succeed for CREATE, DELETE and RENAME.
1419 KASSERT(cnp->cn_nameiop == LOOKUP, ("nameiop must be LOOKUP"));
1420 KASSERT(dp->v_type == VDIR, ("dp is not a directory"));
1422 if (!(cnp->cn_flags & LOCKLEAF))
1425 /* XXX This should probably move to the top of function. */
1426 if (cnp->cn_flags & SAVESTART)
1427 panic("lookup: SAVESTART");
1431 if (cnp->cn_flags & ISDOTDOT)
1432 panic ("relookup: lookup on dot-dot");
1435 * We now have a segment name to search for, and a directory to search.
1437 #ifdef NAMEI_DIAGNOSTIC
1438 vn_printf(dp, "search in ");
1440 if ((error = VOP_LOOKUP(dp, vpp, cnp)) != 0) {
1441 KASSERT(*vpp == NULL, ("leaf should be empty"));
1442 if (error != EJUSTRETURN)
1445 * If creating and at end of pathname, then can consider
1446 * allowing file to be created.
1452 /* ASSERT(dvp == ndp->ni_startdir) */
1453 if (cnp->cn_flags & SAVESTART)
1455 if ((cnp->cn_flags & LOCKPARENT) == 0)
1458 * We return with ni_vp NULL to indicate that the entry
1459 * doesn't currently exist, leaving a pointer to the
1460 * (possibly locked) directory vnode in ndp->ni_dvp.
1468 * Disallow directory write attempts on read-only filesystems.
1471 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
1480 * Set the parent lock/ref state to the requested state.
1482 if ((cnp->cn_flags & LOCKPARENT) == 0 && dvp != dp)
1485 * Check for symbolic link
1487 KASSERT(dp->v_type != VLNK || !(cnp->cn_flags & FOLLOW),
1488 ("relookup: symlink found.\n"));
1490 /* ASSERT(dvp == ndp->ni_startdir) */
1491 if (cnp->cn_flags & SAVESTART)
1494 if ((cnp->cn_flags & LOCKLEAF) == 0)
1504 * Free data allocated by namei(); see namei(9) for details.
1507 NDFREE_PNBUF(struct nameidata *ndp)
1510 if ((ndp->ni_cnd.cn_flags & HASBUF) != 0) {
1511 MPASS((ndp->ni_cnd.cn_flags & (SAVENAME | SAVESTART)) != 0);
1512 uma_zfree(namei_zone, ndp->ni_cnd.cn_pnbuf);
1513 ndp->ni_cnd.cn_flags &= ~HASBUF;
1518 * NDFREE_PNBUF replacement for callers that know there is no buffer.
1520 * This is a hack. Preferably the VFS layer would not produce anything more
1521 * than it was asked to do. Unfortunately several non-LOOKUP cases can add the
1522 * HASBUF flag to the result. Even then an interface could be implemented where
1523 * the caller specifies what they expected to see in the result and what they
1524 * are going to take care of.
1526 * In the meantime provide this kludge as a trivial replacement for NDFREE_PNBUF
1527 * calls scattered throughout the kernel where we know for a fact the flag must not
1532 NDFREE_NOTHING(struct nameidata *ndp)
1534 struct componentname *cnp;
1537 KASSERT(cnp->cn_nameiop == LOOKUP, ("%s: got non-LOOKUP op %d\n",
1538 __func__, cnp->cn_nameiop));
1539 KASSERT((cnp->cn_flags & (SAVENAME | HASBUF)) == 0,
1540 ("%s: bad flags \%" PRIx64 "\n", __func__, cnp->cn_flags));
1545 (NDFREE)(struct nameidata *ndp, const u_int flags)
1553 if (!(flags & NDF_NO_FREE_PNBUF)) {
1556 if (!(flags & NDF_NO_VP_UNLOCK) &&
1557 (ndp->ni_cnd.cn_flags & LOCKLEAF) && ndp->ni_vp)
1559 if (!(flags & NDF_NO_DVP_UNLOCK) &&
1560 (ndp->ni_cnd.cn_flags & LOCKPARENT) &&
1561 ndp->ni_dvp != ndp->ni_vp)
1563 if (!(flags & NDF_NO_VP_RELE) && ndp->ni_vp) {
1572 VOP_UNLOCK(ndp->ni_vp);
1573 if (!(flags & NDF_NO_DVP_RELE) &&
1574 (ndp->ni_cnd.cn_flags & (LOCKPARENT|WANTPARENT))) {
1583 VOP_UNLOCK(ndp->ni_dvp);
1584 if (!(flags & NDF_NO_STARTDIR_RELE) &&
1585 (ndp->ni_cnd.cn_flags & SAVESTART)) {
1586 vrele(ndp->ni_startdir);
1587 ndp->ni_startdir = NULL;
1593 * Validate the final state of ndp after the lookup.
1595 * Historically filesystems were allowed to modify cn_flags. Most notably they
1596 * can add SAVENAME to the request, resulting in HASBUF and pushing subsequent
1597 * clean up to the consumer. In practice this seems to only concern != LOOKUP
1600 * As a step towards stricter API contract this routine validates the state to
1601 * clean up. Note validation is a work in progress with the intent of becoming
1602 * stricter over time.
1604 #define NDMODIFYINGFLAGS (LOCKLEAF | LOCKPARENT | WANTPARENT | SAVENAME | SAVESTART | HASBUF)
1606 NDVALIDATE(struct nameidata *ndp)
1608 struct componentname *cnp;
1609 u_int64_t used, orig;
1612 orig = cnp->cn_origflags;
1613 used = cnp->cn_flags;
1614 switch (cnp->cn_nameiop) {
1617 * For plain lookup we require strict conformance -- nothing
1618 * to clean up if it was not requested by the caller.
1620 orig &= NDMODIFYINGFLAGS;
1621 used &= NDMODIFYINGFLAGS;
1622 if ((orig & (SAVENAME | SAVESTART)) != 0)
1632 * Some filesystems set SAVENAME to provoke HASBUF, accomodate
1633 * for it until it gets fixed.
1635 orig &= NDMODIFYINGFLAGS;
1636 orig |= (SAVENAME | HASBUF);
1637 used &= NDMODIFYINGFLAGS;
1638 used |= (SAVENAME | HASBUF);
1646 panic("%s: mismatched flags for op %d: added %" PRIx64 ", "
1647 "removed %" PRIx64" (%" PRIx64" != %" PRIx64"; stored %" PRIx64" != %" PRIx64")",
1648 __func__, cnp->cn_nameiop, used & ~orig, orig &~ used,
1649 orig, used, cnp->cn_origflags, cnp->cn_flags);
1654 * Determine if there is a suitable alternate filename under the specified
1655 * prefix for the specified path. If the create flag is set, then the
1656 * alternate prefix will be used so long as the parent directory exists.
1657 * This is used by the various compatibility ABIs so that Linux binaries prefer
1658 * files under /compat/linux for example. The chosen path (whether under
1659 * the prefix or under /) is returned in a kernel malloc'd buffer pointed
1660 * to by pathbuf. The caller is responsible for free'ing the buffer from
1661 * the M_TEMP bucket if one is returned.
1664 kern_alternate_path(struct thread *td, const char *prefix, const char *path,
1665 enum uio_seg pathseg, char **pathbuf, int create, int dirfd)
1667 struct nameidata nd, ndroot;
1668 char *ptr, *buf, *cp;
1672 buf = (char *) malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1675 /* Copy the prefix into the new pathname as a starting point. */
1676 len = strlcpy(buf, prefix, MAXPATHLEN);
1677 if (len >= MAXPATHLEN) {
1682 sz = MAXPATHLEN - len;
1685 /* Append the filename to the prefix. */
1686 if (pathseg == UIO_SYSSPACE)
1687 error = copystr(path, ptr, sz, &len);
1689 error = copyinstr(path, ptr, sz, &len);
1697 /* Only use a prefix with absolute pathnames. */
1703 if (dirfd != AT_FDCWD) {
1705 * We want the original because the "prefix" is
1706 * included in the already opened dirfd.
1708 bcopy(ptr, buf, len);
1713 * We know that there is a / somewhere in this pathname.
1714 * Search backwards for it, to find the file's parent dir
1715 * to see if it exists in the alternate tree. If it does,
1716 * and we want to create a file (cflag is set). We don't
1717 * need to worry about the root comparison in this case.
1721 for (cp = &ptr[len] - 1; *cp != '/'; cp--);
1724 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, buf, td);
1730 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, buf, td);
1737 * We now compare the vnode of the prefix to the one
1738 * vnode asked. If they resolve to be the same, then we
1739 * ignore the match so that the real root gets used.
1740 * This avoids the problem of traversing "../.." to find the
1741 * root directory and never finding it, because "/" resolves
1742 * to the emulation root directory. This is expensive :-(
1744 NDINIT(&ndroot, LOOKUP, FOLLOW, UIO_SYSSPACE, prefix,
1747 /* We shouldn't ever get an error from this namei(). */
1748 error = namei(&ndroot);
1750 if (nd.ni_vp == ndroot.ni_vp)
1753 NDFREE(&ndroot, NDF_ONLY_PNBUF);
1754 vrele(ndroot.ni_vp);
1758 NDFREE(&nd, NDF_ONLY_PNBUF);
1762 /* If there was an error, use the original path name. */
1764 bcopy(ptr, buf, len);