2 * Copyright (c) 1982, 1986, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)vfs_lookup.c 8.4 (Berkeley) 2/16/94
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include "opt_capsicum.h"
41 #include "opt_kdtrace.h"
42 #include "opt_ktrace.h"
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/capsicum.h>
48 #include <sys/fcntl.h>
51 #include <sys/mutex.h>
52 #include <sys/namei.h>
53 #include <sys/vnode.h>
54 #include <sys/mount.h>
55 #include <sys/filedesc.h>
58 #include <sys/syscallsubr.h>
59 #include <sys/sysctl.h>
61 #include <sys/ktrace.h>
64 #include <security/audit/audit.h>
65 #include <security/mac/mac_framework.h>
69 #define NAMEI_DIAGNOSTIC 1
70 #undef NAMEI_DIAGNOSTIC
72 SDT_PROVIDER_DECLARE(vfs);
73 SDT_PROBE_DEFINE3(vfs, namei, lookup, entry, "struct vnode *", "char *",
75 SDT_PROBE_DEFINE2(vfs, namei, lookup, return, "int", "struct vnode *");
78 * Allocation zone for namei
80 uma_zone_t namei_zone;
82 * Placeholder vnode for mp traversal
84 static struct vnode *vp_crossmp;
87 nameiinit(void *dummy __unused)
90 namei_zone = uma_zcreate("NAMEI", MAXPATHLEN, NULL, NULL, NULL, NULL,
92 getnewvnode("crossmp", NULL, &dead_vnodeops, &vp_crossmp);
93 vn_lock(vp_crossmp, LK_EXCLUSIVE);
94 VN_LOCK_ASHARE(vp_crossmp);
95 VOP_UNLOCK(vp_crossmp, 0);
97 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_SECOND, nameiinit, NULL);
99 static int lookup_shared = 1;
100 SYSCTL_INT(_vfs, OID_AUTO, lookup_shared, CTLFLAG_RW, &lookup_shared, 0,
101 "Enables/Disables shared locks for path name translation");
102 TUNABLE_INT("vfs.lookup_shared", &lookup_shared);
105 * Convert a pathname into a pointer to a locked vnode.
107 * The FOLLOW flag is set when symbolic links are to be followed
108 * when they occur at the end of the name translation process.
109 * Symbolic links are always followed for all other pathname
110 * components other than the last.
112 * The segflg defines whether the name is to be copied from user
113 * space or kernel space.
115 * Overall outline of namei:
118 * get starting directory
119 * while (!done && !error) {
120 * call lookup to search path.
121 * if symbolic link, massage name in buffer and continue
125 namei_cleanup_cnp(struct componentname *cnp)
127 uma_zfree(namei_zone, cnp->cn_pnbuf);
129 cnp->cn_pnbuf = NULL;
130 cnp->cn_nameptr = NULL;
135 namei(struct nameidata *ndp)
137 struct filedesc *fdp; /* pointer to file descriptor state */
138 char *cp; /* pointer into pathname argument */
139 struct vnode *dp; /* the directory we are searching */
140 struct iovec aiov; /* uio for reading symbolic links */
143 struct componentname *cnp = &ndp->ni_cnd;
144 struct thread *td = cnp->cn_thread;
145 struct proc *p = td->td_proc;
147 ndp->ni_cnd.cn_cred = ndp->ni_cnd.cn_thread->td_ucred;
148 KASSERT(cnp->cn_cred && p, ("namei: bad cred/proc"));
149 KASSERT((cnp->cn_nameiop & (~OPMASK)) == 0,
150 ("namei: nameiop contaminated with flags"));
151 KASSERT((cnp->cn_flags & OPMASK) == 0,
152 ("namei: flags contaminated with nameiops"));
154 cnp->cn_flags &= ~LOCKSHARED;
157 /* We will set this ourselves if we need it. */
158 cnp->cn_flags &= ~TRAILINGSLASH;
161 * Get a buffer for the name to be translated, and copy the
162 * name into the buffer.
164 if ((cnp->cn_flags & HASBUF) == 0)
165 cnp->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK);
166 if (ndp->ni_segflg == UIO_SYSSPACE)
167 error = copystr(ndp->ni_dirp, cnp->cn_pnbuf, MAXPATHLEN,
170 error = copyinstr(ndp->ni_dirp, cnp->cn_pnbuf, MAXPATHLEN,
174 * Don't allow empty pathnames.
176 if (!error && *cnp->cn_pnbuf == '\0')
179 #ifdef CAPABILITY_MODE
181 * In capability mode, lookups must be "strictly relative" (i.e.
182 * not an absolute path, and not containing '..' components) to
183 * a real file descriptor, not the pseudo-descriptor AT_FDCWD.
185 if (error == 0 && IN_CAPABILITY_MODE(td) &&
186 (cnp->cn_flags & NOCAPCHECK) == 0) {
187 ndp->ni_strictrelative = 1;
188 if (ndp->ni_dirfd == AT_FDCWD) {
190 if (KTRPOINT(td, KTR_CAPFAIL))
191 ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL);
198 namei_cleanup_cnp(cnp);
204 if (KTRPOINT(td, KTR_NAMEI)) {
205 KASSERT(cnp->cn_thread == curthread,
206 ("namei not using curthread"));
207 ktrnamei(cnp->cn_pnbuf);
211 * Get starting point for the translation.
214 ndp->ni_rootdir = fdp->fd_rdir;
215 ndp->ni_topdir = fdp->fd_jdir;
218 * If we are auditing the kernel pathname, save the user pathname.
220 if (cnp->cn_flags & AUDITVNODE1)
221 AUDIT_ARG_UPATH1(td, ndp->ni_dirfd, cnp->cn_pnbuf);
222 if (cnp->cn_flags & AUDITVNODE2)
223 AUDIT_ARG_UPATH2(td, ndp->ni_dirfd, cnp->cn_pnbuf);
226 if (cnp->cn_pnbuf[0] != '/') {
227 if (ndp->ni_startdir != NULL) {
228 dp = ndp->ni_startdir;
230 } else if (ndp->ni_dirfd != AT_FDCWD) {
233 rights = ndp->ni_rightsneeded;
234 cap_rights_set(&rights, CAP_LOOKUP);
236 if (cnp->cn_flags & AUDITVNODE1)
237 AUDIT_ARG_ATFD1(ndp->ni_dirfd);
238 if (cnp->cn_flags & AUDITVNODE2)
239 AUDIT_ARG_ATFD2(ndp->ni_dirfd);
240 error = fgetvp_rights(td, ndp->ni_dirfd,
241 &rights, &ndp->ni_filecaps, &dp);
244 * If file descriptor doesn't have all rights,
245 * all lookups relative to it must also be
249 if (!cap_rights_contains(&ndp->ni_filecaps.fc_rights,
251 ndp->ni_filecaps.fc_fcntls != CAP_FCNTL_ALL ||
252 ndp->ni_filecaps.fc_nioctls != -1) {
253 ndp->ni_strictrelative = 1;
257 if (error != 0 || dp != NULL) {
258 FILEDESC_SUNLOCK(fdp);
259 if (error == 0 && dp->v_type != VDIR) {
265 namei_cleanup_cnp(cnp);
272 FILEDESC_SUNLOCK(fdp);
273 if (ndp->ni_startdir != NULL)
274 vrele(ndp->ni_startdir);
276 SDT_PROBE3(vfs, namei, lookup, entry, dp, cnp->cn_pnbuf,
280 * Check if root directory should replace current directory.
281 * Done at start of translation and after symbolic link.
283 cnp->cn_nameptr = cnp->cn_pnbuf;
284 if (*(cnp->cn_nameptr) == '/') {
286 if (ndp->ni_strictrelative != 0) {
288 if (KTRPOINT(curthread, KTR_CAPFAIL))
289 ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL);
291 namei_cleanup_cnp(cnp);
292 return (ENOTCAPABLE);
294 while (*(cnp->cn_nameptr) == '/') {
298 dp = ndp->ni_rootdir;
301 ndp->ni_startdir = dp;
304 namei_cleanup_cnp(cnp);
305 SDT_PROBE2(vfs, namei, lookup, return, error, NULL);
309 * If not a symbolic link, we're done.
311 if ((cnp->cn_flags & ISSYMLINK) == 0) {
312 if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0) {
313 namei_cleanup_cnp(cnp);
315 cnp->cn_flags |= HASBUF;
317 SDT_PROBE2(vfs, namei, lookup, return, 0, ndp->ni_vp);
320 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
325 if ((cnp->cn_flags & NOMACCHECK) == 0) {
326 error = mac_vnode_check_readlink(td->td_ucred,
332 if (ndp->ni_pathlen > 1)
333 cp = uma_zalloc(namei_zone, M_WAITOK);
337 aiov.iov_len = MAXPATHLEN;
338 auio.uio_iov = &aiov;
341 auio.uio_rw = UIO_READ;
342 auio.uio_segflg = UIO_SYSSPACE;
344 auio.uio_resid = MAXPATHLEN;
345 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
347 if (ndp->ni_pathlen > 1)
348 uma_zfree(namei_zone, cp);
351 linklen = MAXPATHLEN - auio.uio_resid;
353 if (ndp->ni_pathlen > 1)
354 uma_zfree(namei_zone, cp);
358 if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
359 if (ndp->ni_pathlen > 1)
360 uma_zfree(namei_zone, cp);
361 error = ENAMETOOLONG;
364 if (ndp->ni_pathlen > 1) {
365 bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
366 uma_zfree(namei_zone, cnp->cn_pnbuf);
369 cnp->cn_pnbuf[linklen] = '\0';
370 ndp->ni_pathlen += linklen;
374 namei_cleanup_cnp(cnp);
378 SDT_PROBE2(vfs, namei, lookup, return, error, NULL);
383 compute_cn_lkflags(struct mount *mp, int lkflags, int cnflags)
386 if (mp == NULL || ((lkflags & LK_SHARED) &&
387 (!(mp->mnt_kern_flag & MNTK_LOOKUP_SHARED) ||
388 ((cnflags & ISDOTDOT) &&
389 (mp->mnt_kern_flag & MNTK_LOOKUP_EXCL_DOTDOT))))) {
390 lkflags &= ~LK_SHARED;
391 lkflags |= LK_EXCLUSIVE;
393 lkflags |= LK_NODDLKTREAT;
398 needs_exclusive_leaf(struct mount *mp, int flags)
402 * Intermediate nodes can use shared locks, we only need to
403 * force an exclusive lock for leaf nodes.
405 if ((flags & (ISLASTCN | LOCKLEAF)) != (ISLASTCN | LOCKLEAF))
408 /* Always use exclusive locks if LOCKSHARED isn't set. */
409 if (!(flags & LOCKSHARED))
413 * For lookups during open(), if the mount point supports
414 * extended shared operations, then use a shared lock for the
415 * leaf node, otherwise use an exclusive lock.
417 if ((flags & ISOPEN) != 0)
418 return (!MNT_EXTENDED_SHARED(mp));
421 * Lookup requests outside of open() that specify LOCKSHARED
422 * only need a shared lock on the leaf vnode.
429 * This is a very central and rather complicated routine.
431 * The pathname is pointed to by ni_ptr and is of length ni_pathlen.
432 * The starting directory is taken from ni_startdir. The pathname is
433 * descended until done, or a symbolic link is encountered. The variable
434 * ni_more is clear if the path is completed; it is set to one if a
435 * symbolic link needing interpretation is encountered.
437 * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
438 * whether the name is to be looked up, created, renamed, or deleted.
439 * When CREATE, RENAME, or DELETE is specified, information usable in
440 * creating, renaming, or deleting a directory entry may be calculated.
441 * If flag has LOCKPARENT or'ed into it, the parent directory is returned
442 * locked. If flag has WANTPARENT or'ed into it, the parent directory is
443 * returned unlocked. Otherwise the parent directory is not returned. If
444 * the target of the pathname exists and LOCKLEAF is or'ed into the flag
445 * the target is returned locked, otherwise it is returned unlocked.
446 * When creating or renaming and LOCKPARENT is specified, the target may not
447 * be ".". When deleting and LOCKPARENT is specified, the target may be ".".
449 * Overall outline of lookup:
452 * identify next component of name at ndp->ni_ptr
453 * handle degenerate case where name is null string
454 * if .. and crossing mount points and on mounted filesys, find parent
455 * call VOP_LOOKUP routine for next component name
456 * directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set
457 * component vnode returned in ni_vp (if it exists), locked.
458 * if result vnode is mounted on and crossing mount points,
459 * find mounted on vnode
460 * if more components of name, do next level at dirloop
461 * return the answer in ni_vp, locked if LOCKLEAF set
462 * if LOCKPARENT set, return locked parent in ni_dvp
463 * if WANTPARENT set, return unlocked parent in ni_dvp
466 lookup(struct nameidata *ndp)
468 char *cp; /* pointer into pathname argument */
469 struct vnode *dp = 0; /* the directory we are searching */
470 struct vnode *tdp; /* saved dp */
471 struct mount *mp; /* mount table entry */
473 int docache; /* == 0 do not cache last component */
474 int wantparent; /* 1 => wantparent or lockparent flag */
475 int rdonly; /* lookup read-only flag bit */
477 int dpunlocked = 0; /* dp has already been unlocked */
478 struct componentname *cnp = &ndp->ni_cnd;
483 * Setup: break out flag bits into variables.
486 wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT);
487 KASSERT(cnp->cn_nameiop == LOOKUP || wantparent,
488 ("CREATE, DELETE, RENAME require LOCKPARENT or WANTPARENT."));
489 docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE;
490 if (cnp->cn_nameiop == DELETE ||
491 (wantparent && cnp->cn_nameiop != CREATE &&
492 cnp->cn_nameiop != LOOKUP))
494 rdonly = cnp->cn_flags & RDONLY;
495 cnp->cn_flags &= ~ISSYMLINK;
498 * We use shared locks until we hit the parent of the last cn then
499 * we adjust based on the requesting flags.
502 cnp->cn_lkflags = LK_SHARED;
504 cnp->cn_lkflags = LK_EXCLUSIVE;
505 dp = ndp->ni_startdir;
506 ndp->ni_startdir = NULLVP;
508 compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags | LK_RETRY,
513 * Search a new directory.
515 * The last component of the filename is left accessible via
516 * cnp->cn_nameptr for callers that need the name. Callers needing
517 * the name set the SAVENAME flag. When done, they assume
518 * responsibility for freeing the pathname buffer.
521 for (cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++)
523 cnp->cn_namelen = cp - cnp->cn_nameptr;
524 if (cnp->cn_namelen > NAME_MAX) {
525 error = ENAMETOOLONG;
528 #ifdef NAMEI_DIAGNOSTIC
531 printf("{%s}: ", cnp->cn_nameptr);
534 ndp->ni_pathlen -= cnp->cn_namelen;
538 * Replace multiple slashes by a single slash and trailing slashes
539 * by a null. This must be done before VOP_LOOKUP() because some
540 * fs's don't know about trailing slashes. Remember if there were
541 * trailing slashes to handle symlinks, existing non-directories
542 * and non-existing files that won't be directories specially later.
544 while (*cp == '/' && (cp[1] == '/' || cp[1] == '\0')) {
548 *ndp->ni_next = '\0';
549 cnp->cn_flags |= TRAILINGSLASH;
554 cnp->cn_flags |= MAKEENTRY;
555 if (*cp == '\0' && docache == 0)
556 cnp->cn_flags &= ~MAKEENTRY;
557 if (cnp->cn_namelen == 2 &&
558 cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.')
559 cnp->cn_flags |= ISDOTDOT;
561 cnp->cn_flags &= ~ISDOTDOT;
562 if (*ndp->ni_next == 0)
563 cnp->cn_flags |= ISLASTCN;
565 cnp->cn_flags &= ~ISLASTCN;
567 if ((cnp->cn_flags & ISLASTCN) != 0 &&
568 cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.' &&
569 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
575 * Check for degenerate name (e.g. / or "")
576 * which is a way of talking about a directory,
577 * e.g. like "/." or ".".
579 if (cnp->cn_nameptr[0] == '\0') {
580 if (dp->v_type != VDIR) {
584 if (cnp->cn_nameiop != LOOKUP) {
594 if (cnp->cn_flags & AUDITVNODE1)
595 AUDIT_ARG_VNODE1(dp);
596 else if (cnp->cn_flags & AUDITVNODE2)
597 AUDIT_ARG_VNODE2(dp);
599 if (!(cnp->cn_flags & (LOCKPARENT | LOCKLEAF)))
601 /* XXX This should probably move to the top of function. */
602 if (cnp->cn_flags & SAVESTART)
603 panic("lookup: SAVESTART");
608 * Handle "..": five special cases.
609 * 0. If doing a capability lookup, return ENOTCAPABLE (this is a
610 * fairly conservative design choice, but it's the only one that we
611 * are satisfied guarantees the property we're looking for).
612 * 1. Return an error if this is the last component of
613 * the name and the operation is DELETE or RENAME.
614 * 2. If at root directory (e.g. after chroot)
615 * or at absolute root directory
616 * then ignore it so can't get out.
617 * 3. If this vnode is the root of a mounted
618 * filesystem, then replace it with the
619 * vnode which was mounted on so we take the
620 * .. in the other filesystem.
621 * 4. If the vnode is the top directory of
622 * the jail or chroot, don't let them out.
624 if (cnp->cn_flags & ISDOTDOT) {
625 if (ndp->ni_strictrelative != 0) {
627 if (KTRPOINT(curthread, KTR_CAPFAIL))
628 ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL);
633 if ((cnp->cn_flags & ISLASTCN) != 0 &&
634 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
639 for (pr = cnp->cn_cred->cr_prison; pr != NULL;
641 if (dp == pr->pr_root)
643 if (dp == ndp->ni_rootdir ||
644 dp == ndp->ni_topdir ||
647 ((dp->v_vflag & VV_ROOT) != 0 &&
648 (cnp->cn_flags & NOCROSSMOUNT) != 0)) {
654 if ((dp->v_vflag & VV_ROOT) == 0)
656 if (dp->v_iflag & VI_DOOMED) { /* forced unmount */
661 dp = dp->v_mount->mnt_vnodecovered;
665 compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags |
666 LK_RETRY, ISDOTDOT));
671 * We now have a segment name to search for, and a directory to search.
675 if ((cnp->cn_flags & NOMACCHECK) == 0) {
676 error = mac_vnode_check_lookup(cnp->cn_thread->td_ucred, dp,
684 ASSERT_VOP_LOCKED(dp, "lookup");
686 * If we have a shared lock we may need to upgrade the lock for the
689 if (dp != vp_crossmp &&
690 VOP_ISLOCKED(dp) == LK_SHARED &&
691 (cnp->cn_flags & ISLASTCN) && (cnp->cn_flags & LOCKPARENT))
692 vn_lock(dp, LK_UPGRADE|LK_RETRY);
693 if ((dp->v_iflag & VI_DOOMED) != 0) {
698 * If we're looking up the last component and we need an exclusive
699 * lock, adjust our lkflags.
701 if (needs_exclusive_leaf(dp->v_mount, cnp->cn_flags))
702 cnp->cn_lkflags = LK_EXCLUSIVE;
703 #ifdef NAMEI_DIAGNOSTIC
704 vprint("lookup in", dp);
706 lkflags_save = cnp->cn_lkflags;
707 cnp->cn_lkflags = compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags,
709 if ((error = VOP_LOOKUP(dp, &ndp->ni_vp, cnp)) != 0) {
710 cnp->cn_lkflags = lkflags_save;
711 KASSERT(ndp->ni_vp == NULL, ("leaf should be empty"));
712 #ifdef NAMEI_DIAGNOSTIC
713 printf("not found\n");
715 if ((error == ENOENT) &&
716 (dp->v_vflag & VV_ROOT) && (dp->v_mount != NULL) &&
717 (dp->v_mount->mnt_flag & MNT_UNION)) {
719 dp = dp->v_mount->mnt_vnodecovered;
723 compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags |
724 LK_RETRY, cnp->cn_flags));
728 if (error != EJUSTRETURN)
731 * At this point, we know we're at the end of the
732 * pathname. If creating / renaming, we can consider
733 * allowing the file or directory to be created / renamed,
734 * provided we're not on a read-only filesystem.
740 /* trailing slash only allowed for directories */
741 if ((cnp->cn_flags & TRAILINGSLASH) &&
742 !(cnp->cn_flags & WILLBEDIR)) {
746 if ((cnp->cn_flags & LOCKPARENT) == 0)
749 * We return with ni_vp NULL to indicate that the entry
750 * doesn't currently exist, leaving a pointer to the
751 * (possibly locked) directory vnode in ndp->ni_dvp.
753 if (cnp->cn_flags & SAVESTART) {
754 ndp->ni_startdir = ndp->ni_dvp;
755 VREF(ndp->ni_startdir);
759 cnp->cn_lkflags = lkflags_save;
760 #ifdef NAMEI_DIAGNOSTIC
764 * Take into account any additional components consumed by
765 * the underlying filesystem.
767 if (cnp->cn_consume > 0) {
768 cnp->cn_nameptr += cnp->cn_consume;
769 ndp->ni_next += cnp->cn_consume;
770 ndp->ni_pathlen -= cnp->cn_consume;
777 * Check to see if the vnode has been mounted on;
778 * if so find the root of the mounted filesystem.
780 while (dp->v_type == VDIR && (mp = dp->v_mountedhere) &&
781 (cnp->cn_flags & NOCROSSMOUNT) == 0) {
785 if (dp != ndp->ni_dvp)
790 ndp->ni_dvp = vp_crossmp;
791 error = VFS_ROOT(mp, compute_cn_lkflags(mp, cnp->cn_lkflags,
792 cnp->cn_flags), &tdp);
794 if (vn_lock(vp_crossmp, LK_SHARED | LK_NOWAIT))
795 panic("vp_crossmp exclusively locked or reclaimed");
800 ndp->ni_vp = dp = tdp;
804 * Check for symbolic link
806 if ((dp->v_type == VLNK) &&
807 ((cnp->cn_flags & FOLLOW) || (cnp->cn_flags & TRAILINGSLASH) ||
808 *ndp->ni_next == '/')) {
809 cnp->cn_flags |= ISSYMLINK;
810 if (dp->v_iflag & VI_DOOMED) {
812 * We can't know whether the directory was mounted with
813 * NOSYMFOLLOW, so we can't follow safely.
818 if (dp->v_mount->mnt_flag & MNT_NOSYMFOLLOW) {
823 * Symlink code always expects an unlocked dvp.
825 if (ndp->ni_dvp != ndp->ni_vp) {
826 VOP_UNLOCK(ndp->ni_dvp, 0);
834 * Not a symbolic link that we will follow. Continue with the
835 * next component if there is any; otherwise, we're done.
837 KASSERT((cnp->cn_flags & ISLASTCN) || *ndp->ni_next == '/',
838 ("lookup: invalid path state."));
839 if (*ndp->ni_next == '/') {
840 cnp->cn_nameptr = ndp->ni_next;
841 while (*cnp->cn_nameptr == '/') {
845 if (ndp->ni_dvp != dp)
852 * If we're processing a path with a trailing slash,
853 * check that the end result is a directory.
855 if ((cnp->cn_flags & TRAILINGSLASH) && dp->v_type != VDIR) {
860 * Disallow directory write attempts on read-only filesystems.
863 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
867 if (cnp->cn_flags & SAVESTART) {
868 ndp->ni_startdir = ndp->ni_dvp;
869 VREF(ndp->ni_startdir);
873 if (ndp->ni_dvp != dp)
877 } else if ((cnp->cn_flags & LOCKPARENT) == 0 && ndp->ni_dvp != dp) {
878 VOP_UNLOCK(ndp->ni_dvp, 0);
882 if (cnp->cn_flags & AUDITVNODE1)
883 AUDIT_ARG_VNODE1(dp);
884 else if (cnp->cn_flags & AUDITVNODE2)
885 AUDIT_ARG_VNODE2(dp);
887 if ((cnp->cn_flags & LOCKLEAF) == 0)
891 * Because of lookup_shared we may have the vnode shared locked, but
892 * the caller may want it to be exclusively locked.
894 if (needs_exclusive_leaf(dp->v_mount, cnp->cn_flags) &&
895 VOP_ISLOCKED(dp) != LK_EXCLUSIVE) {
896 vn_lock(dp, LK_UPGRADE | LK_RETRY);
897 if (dp->v_iflag & VI_DOOMED) {
905 if (ni_dvp_unlocked != 2) {
906 if (dp != ndp->ni_dvp && !ni_dvp_unlocked)
919 * relookup - lookup a path name component
920 * Used by lookup to re-acquire things.
923 relookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp)
925 struct vnode *dp = 0; /* the directory we are searching */
926 int wantparent; /* 1 => wantparent or lockparent flag */
927 int rdonly; /* lookup read-only flag bit */
930 KASSERT(cnp->cn_flags & ISLASTCN,
931 ("relookup: Not given last component."));
933 * Setup: break out flag bits into variables.
935 wantparent = cnp->cn_flags & (LOCKPARENT|WANTPARENT);
936 KASSERT(wantparent, ("relookup: parent not wanted."));
937 rdonly = cnp->cn_flags & RDONLY;
938 cnp->cn_flags &= ~ISSYMLINK;
940 cnp->cn_lkflags = LK_EXCLUSIVE;
941 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
944 * Search a new directory.
946 * The last component of the filename is left accessible via
947 * cnp->cn_nameptr for callers that need the name. Callers needing
948 * the name set the SAVENAME flag. When done, they assume
949 * responsibility for freeing the pathname buffer.
951 #ifdef NAMEI_DIAGNOSTIC
952 printf("{%s}: ", cnp->cn_nameptr);
956 * Check for "" which represents the root directory after slash
959 if (cnp->cn_nameptr[0] == '\0') {
961 * Support only LOOKUP for "/" because lookup()
962 * can't succeed for CREATE, DELETE and RENAME.
964 KASSERT(cnp->cn_nameiop == LOOKUP, ("nameiop must be LOOKUP"));
965 KASSERT(dp->v_type == VDIR, ("dp is not a directory"));
967 if (!(cnp->cn_flags & LOCKLEAF))
970 /* XXX This should probably move to the top of function. */
971 if (cnp->cn_flags & SAVESTART)
972 panic("lookup: SAVESTART");
976 if (cnp->cn_flags & ISDOTDOT)
977 panic ("relookup: lookup on dot-dot");
980 * We now have a segment name to search for, and a directory to search.
982 #ifdef NAMEI_DIAGNOSTIC
983 vprint("search in:", dp);
985 if ((error = VOP_LOOKUP(dp, vpp, cnp)) != 0) {
986 KASSERT(*vpp == NULL, ("leaf should be empty"));
987 if (error != EJUSTRETURN)
990 * If creating and at end of pathname, then can consider
991 * allowing file to be created.
997 /* ASSERT(dvp == ndp->ni_startdir) */
998 if (cnp->cn_flags & SAVESTART)
1000 if ((cnp->cn_flags & LOCKPARENT) == 0)
1003 * We return with ni_vp NULL to indicate that the entry
1004 * doesn't currently exist, leaving a pointer to the
1005 * (possibly locked) directory vnode in ndp->ni_dvp.
1013 * Disallow directory write attempts on read-only filesystems.
1016 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
1025 * Set the parent lock/ref state to the requested state.
1027 if ((cnp->cn_flags & LOCKPARENT) == 0 && dvp != dp) {
1032 } else if (!wantparent)
1035 * Check for symbolic link
1037 KASSERT(dp->v_type != VLNK || !(cnp->cn_flags & FOLLOW),
1038 ("relookup: symlink found.\n"));
1040 /* ASSERT(dvp == ndp->ni_startdir) */
1041 if (cnp->cn_flags & SAVESTART)
1044 if ((cnp->cn_flags & LOCKLEAF) == 0)
1054 NDINIT_ALL(struct nameidata *ndp, u_long op, u_long flags, enum uio_seg segflg,
1055 const char *namep, int dirfd, struct vnode *startdir, cap_rights_t *rightsp,
1059 ndp->ni_cnd.cn_nameiop = op;
1060 ndp->ni_cnd.cn_flags = flags;
1061 ndp->ni_segflg = segflg;
1062 ndp->ni_dirp = namep;
1063 ndp->ni_dirfd = dirfd;
1064 ndp->ni_startdir = startdir;
1065 ndp->ni_strictrelative = 0;
1066 if (rightsp != NULL)
1067 ndp->ni_rightsneeded = *rightsp;
1069 cap_rights_init(&ndp->ni_rightsneeded);
1070 filecaps_init(&ndp->ni_filecaps);
1071 ndp->ni_cnd.cn_thread = td;
1075 * Free data allocated by namei(); see namei(9) for details.
1078 NDFREE(struct nameidata *ndp, const u_int flags)
1086 if (!(flags & NDF_NO_FREE_PNBUF) &&
1087 (ndp->ni_cnd.cn_flags & HASBUF)) {
1088 uma_zfree(namei_zone, ndp->ni_cnd.cn_pnbuf);
1089 ndp->ni_cnd.cn_flags &= ~HASBUF;
1091 if (!(flags & NDF_NO_VP_UNLOCK) &&
1092 (ndp->ni_cnd.cn_flags & LOCKLEAF) && ndp->ni_vp)
1094 if (!(flags & NDF_NO_VP_RELE) && ndp->ni_vp) {
1103 VOP_UNLOCK(ndp->ni_vp, 0);
1104 if (!(flags & NDF_NO_DVP_UNLOCK) &&
1105 (ndp->ni_cnd.cn_flags & LOCKPARENT) &&
1106 ndp->ni_dvp != ndp->ni_vp)
1108 if (!(flags & NDF_NO_DVP_RELE) &&
1109 (ndp->ni_cnd.cn_flags & (LOCKPARENT|WANTPARENT))) {
1118 VOP_UNLOCK(ndp->ni_dvp, 0);
1119 if (!(flags & NDF_NO_STARTDIR_RELE) &&
1120 (ndp->ni_cnd.cn_flags & SAVESTART)) {
1121 vrele(ndp->ni_startdir);
1122 ndp->ni_startdir = NULL;
1127 * Determine if there is a suitable alternate filename under the specified
1128 * prefix for the specified path. If the create flag is set, then the
1129 * alternate prefix will be used so long as the parent directory exists.
1130 * This is used by the various compatibility ABIs so that Linux binaries prefer
1131 * files under /compat/linux for example. The chosen path (whether under
1132 * the prefix or under /) is returned in a kernel malloc'd buffer pointed
1133 * to by pathbuf. The caller is responsible for free'ing the buffer from
1134 * the M_TEMP bucket if one is returned.
1137 kern_alternate_path(struct thread *td, const char *prefix, const char *path,
1138 enum uio_seg pathseg, char **pathbuf, int create, int dirfd)
1140 struct nameidata nd, ndroot;
1141 char *ptr, *buf, *cp;
1145 buf = (char *) malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1148 /* Copy the prefix into the new pathname as a starting point. */
1149 len = strlcpy(buf, prefix, MAXPATHLEN);
1150 if (len >= MAXPATHLEN) {
1155 sz = MAXPATHLEN - len;
1158 /* Append the filename to the prefix. */
1159 if (pathseg == UIO_SYSSPACE)
1160 error = copystr(path, ptr, sz, &len);
1162 error = copyinstr(path, ptr, sz, &len);
1170 /* Only use a prefix with absolute pathnames. */
1176 if (dirfd != AT_FDCWD) {
1178 * We want the original because the "prefix" is
1179 * included in the already opened dirfd.
1181 bcopy(ptr, buf, len);
1186 * We know that there is a / somewhere in this pathname.
1187 * Search backwards for it, to find the file's parent dir
1188 * to see if it exists in the alternate tree. If it does,
1189 * and we want to create a file (cflag is set). We don't
1190 * need to worry about the root comparison in this case.
1194 for (cp = &ptr[len] - 1; *cp != '/'; cp--);
1197 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, buf, td);
1203 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, buf, td);
1210 * We now compare the vnode of the prefix to the one
1211 * vnode asked. If they resolve to be the same, then we
1212 * ignore the match so that the real root gets used.
1213 * This avoids the problem of traversing "../.." to find the
1214 * root directory and never finding it, because "/" resolves
1215 * to the emulation root directory. This is expensive :-(
1217 NDINIT(&ndroot, LOOKUP, FOLLOW, UIO_SYSSPACE, prefix,
1220 /* We shouldn't ever get an error from this namei(). */
1221 error = namei(&ndroot);
1223 if (nd.ni_vp == ndroot.ni_vp)
1226 NDFREE(&ndroot, NDF_ONLY_PNBUF);
1227 vrele(ndroot.ni_vp);
1231 NDFREE(&nd, NDF_ONLY_PNBUF);
1235 /* If there was an error, use the original path name. */
1237 bcopy(ptr, buf, len);