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/capability.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, entry, "struct vnode *", "char *",
75 SDT_PROBE_DEFINE2(vfs, namei, lookup, return, 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(struct nameidata *ndp)
127 struct filedesc *fdp; /* pointer to file descriptor state */
128 char *cp; /* pointer into pathname argument */
129 struct vnode *dp; /* the directory we are searching */
130 struct iovec aiov; /* uio for reading symbolic links */
133 struct componentname *cnp = &ndp->ni_cnd;
134 struct thread *td = cnp->cn_thread;
135 struct proc *p = td->td_proc;
138 KASSERT((cnp->cn_flags & MPSAFE) != 0 || mtx_owned(&Giant) != 0,
139 ("NOT MPSAFE and Giant not held"));
140 ndp->ni_cnd.cn_cred = ndp->ni_cnd.cn_thread->td_ucred;
141 KASSERT(cnp->cn_cred && p, ("namei: bad cred/proc"));
142 KASSERT((cnp->cn_nameiop & (~OPMASK)) == 0,
143 ("namei: nameiop contaminated with flags"));
144 KASSERT((cnp->cn_flags & OPMASK) == 0,
145 ("namei: flags contaminated with nameiops"));
147 cnp->cn_flags &= ~LOCKSHARED;
150 /* We will set this ourselves if we need it. */
151 cnp->cn_flags &= ~TRAILINGSLASH;
154 * Get a buffer for the name to be translated, and copy the
155 * name into the buffer.
157 if ((cnp->cn_flags & HASBUF) == 0)
158 cnp->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK);
159 if (ndp->ni_segflg == UIO_SYSSPACE)
160 error = copystr(ndp->ni_dirp, cnp->cn_pnbuf,
161 MAXPATHLEN, (size_t *)&ndp->ni_pathlen);
163 error = copyinstr(ndp->ni_dirp, cnp->cn_pnbuf,
164 MAXPATHLEN, (size_t *)&ndp->ni_pathlen);
168 * If we are auditing the kernel pathname, save the user
171 if (cnp->cn_flags & AUDITVNODE1)
172 AUDIT_ARG_UPATH1(td, cnp->cn_pnbuf);
173 if (cnp->cn_flags & AUDITVNODE2)
174 AUDIT_ARG_UPATH2(td, cnp->cn_pnbuf);
178 * Don't allow empty pathnames.
180 if (!error && *cnp->cn_pnbuf == '\0')
183 #ifdef CAPABILITY_MODE
185 * In capability mode, lookups must be "strictly relative" (i.e.
186 * not an absolute path, and not containing '..' components) to
187 * a real file descriptor, not the pseudo-descriptor AT_FDCWD.
189 if (IN_CAPABILITY_MODE(td)) {
190 ndp->ni_strictrelative = 1;
191 if (ndp->ni_dirfd == AT_FDCWD)
196 uma_zfree(namei_zone, cnp->cn_pnbuf);
198 cnp->cn_pnbuf = NULL;
199 cnp->cn_nameptr = NULL;
206 if (KTRPOINT(td, KTR_NAMEI)) {
207 KASSERT(cnp->cn_thread == curthread,
208 ("namei not using curthread"));
209 ktrnamei(cnp->cn_pnbuf);
213 * Get starting point for the translation.
216 ndp->ni_rootdir = fdp->fd_rdir;
217 ndp->ni_topdir = fdp->fd_jdir;
220 if (cnp->cn_pnbuf[0] != '/') {
221 if (ndp->ni_startdir != NULL) {
222 dp = ndp->ni_startdir;
224 } else if (ndp->ni_dirfd != AT_FDCWD) {
225 if (cnp->cn_flags & AUDITVNODE1)
226 AUDIT_ARG_ATFD1(ndp->ni_dirfd);
227 if (cnp->cn_flags & AUDITVNODE2)
228 AUDIT_ARG_ATFD2(ndp->ni_dirfd);
229 error = fgetvp_rights(td, ndp->ni_dirfd,
230 ndp->ni_rightsneeded | CAP_LOOKUP,
231 &(ndp->ni_baserights), &dp);
234 * Lookups relative to a capability must also be
237 * Note that a capability with rights CAP_MASK_VALID
238 * is treated exactly like a regular file descriptor.
240 if (ndp->ni_baserights != CAP_MASK_VALID)
241 ndp->ni_strictrelative = 1;
244 if (error != 0 || dp != NULL) {
245 FILEDESC_SUNLOCK(fdp);
246 if (error == 0 && dp->v_type != VDIR) {
247 vfslocked = VFS_LOCK_GIANT(dp->v_mount);
249 VFS_UNLOCK_GIANT(vfslocked);
254 uma_zfree(namei_zone, cnp->cn_pnbuf);
256 cnp->cn_pnbuf = NULL;
257 cnp->cn_nameptr = NULL;
265 FILEDESC_SUNLOCK(fdp);
266 if (ndp->ni_startdir != NULL) {
267 vfslocked = VFS_LOCK_GIANT(ndp->ni_startdir->v_mount);
268 vrele(ndp->ni_startdir);
269 VFS_UNLOCK_GIANT(vfslocked);
272 SDT_PROBE(vfs, namei, lookup, entry, dp, cnp->cn_pnbuf,
273 cnp->cn_flags, 0, 0);
274 vfslocked = VFS_LOCK_GIANT(dp->v_mount);
277 * Check if root directory should replace current directory.
278 * Done at start of translation and after symbolic link.
280 cnp->cn_nameptr = cnp->cn_pnbuf;
281 if (*(cnp->cn_nameptr) == '/') {
283 VFS_UNLOCK_GIANT(vfslocked);
284 if (ndp->ni_strictrelative != 0)
285 return (ENOTCAPABLE);
286 while (*(cnp->cn_nameptr) == '/') {
290 dp = ndp->ni_rootdir;
291 vfslocked = VFS_LOCK_GIANT(dp->v_mount);
295 ndp->ni_cnd.cn_flags |= GIANTHELD;
296 ndp->ni_startdir = dp;
299 uma_zfree(namei_zone, cnp->cn_pnbuf);
301 cnp->cn_pnbuf = NULL;
302 cnp->cn_nameptr = NULL;
304 SDT_PROBE(vfs, namei, lookup, return, error, NULL, 0,
308 vfslocked = (ndp->ni_cnd.cn_flags & GIANTHELD) != 0;
309 ndp->ni_cnd.cn_flags &= ~GIANTHELD;
311 * If not a symbolic link, we're done.
313 if ((cnp->cn_flags & ISSYMLINK) == 0) {
314 if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0) {
315 uma_zfree(namei_zone, cnp->cn_pnbuf);
317 cnp->cn_pnbuf = NULL;
318 cnp->cn_nameptr = NULL;
321 cnp->cn_flags |= HASBUF;
323 if ((cnp->cn_flags & MPSAFE) == 0) {
324 VFS_UNLOCK_GIANT(vfslocked);
325 } else if (vfslocked)
326 ndp->ni_cnd.cn_flags |= GIANTHELD;
327 SDT_PROBE(vfs, namei, lookup, return, 0, ndp->ni_vp,
331 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
336 if ((cnp->cn_flags & NOMACCHECK) == 0) {
337 error = mac_vnode_check_readlink(td->td_ucred,
343 if (ndp->ni_pathlen > 1)
344 cp = uma_zalloc(namei_zone, M_WAITOK);
348 aiov.iov_len = MAXPATHLEN;
349 auio.uio_iov = &aiov;
352 auio.uio_rw = UIO_READ;
353 auio.uio_segflg = UIO_SYSSPACE;
354 auio.uio_td = (struct thread *)0;
355 auio.uio_resid = MAXPATHLEN;
356 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
358 if (ndp->ni_pathlen > 1)
359 uma_zfree(namei_zone, cp);
362 linklen = MAXPATHLEN - auio.uio_resid;
364 if (ndp->ni_pathlen > 1)
365 uma_zfree(namei_zone, cp);
369 if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
370 if (ndp->ni_pathlen > 1)
371 uma_zfree(namei_zone, cp);
372 error = ENAMETOOLONG;
375 if (ndp->ni_pathlen > 1) {
376 bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
377 uma_zfree(namei_zone, cnp->cn_pnbuf);
380 cnp->cn_pnbuf[linklen] = '\0';
381 ndp->ni_pathlen += linklen;
385 uma_zfree(namei_zone, cnp->cn_pnbuf);
387 cnp->cn_pnbuf = NULL;
388 cnp->cn_nameptr = NULL;
393 VFS_UNLOCK_GIANT(vfslocked);
394 SDT_PROBE(vfs, namei, lookup, return, error, NULL, 0, 0, 0);
399 compute_cn_lkflags(struct mount *mp, int lkflags)
403 ((lkflags & LK_SHARED) && !(mp->mnt_kern_flag & MNTK_LOOKUP_SHARED))) {
404 lkflags &= ~LK_SHARED;
405 lkflags |= LK_EXCLUSIVE;
411 needs_exclusive_leaf(struct mount *mp, int flags)
415 * Intermediate nodes can use shared locks, we only need to
416 * force an exclusive lock for leaf nodes.
418 if ((flags & (ISLASTCN | LOCKLEAF)) != (ISLASTCN | LOCKLEAF))
421 /* Always use exclusive locks if LOCKSHARED isn't set. */
422 if (!(flags & LOCKSHARED))
426 * For lookups during open(), if the mount point supports
427 * extended shared operations, then use a shared lock for the
428 * leaf node, otherwise use an exclusive lock.
430 if (flags & ISOPEN) {
432 (mp->mnt_kern_flag & MNTK_EXTENDED_SHARED))
439 * Lookup requests outside of open() that specify LOCKSHARED
440 * only need a shared lock on the leaf vnode.
447 * This is a very central and rather complicated routine.
449 * The pathname is pointed to by ni_ptr and is of length ni_pathlen.
450 * The starting directory is taken from ni_startdir. The pathname is
451 * descended until done, or a symbolic link is encountered. The variable
452 * ni_more is clear if the path is completed; it is set to one if a
453 * symbolic link needing interpretation is encountered.
455 * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
456 * whether the name is to be looked up, created, renamed, or deleted.
457 * When CREATE, RENAME, or DELETE is specified, information usable in
458 * creating, renaming, or deleting a directory entry may be calculated.
459 * If flag has LOCKPARENT or'ed into it, the parent directory is returned
460 * locked. If flag has WANTPARENT or'ed into it, the parent directory is
461 * returned unlocked. Otherwise the parent directory is not returned. If
462 * the target of the pathname exists and LOCKLEAF is or'ed into the flag
463 * the target is returned locked, otherwise it is returned unlocked.
464 * When creating or renaming and LOCKPARENT is specified, the target may not
465 * be ".". When deleting and LOCKPARENT is specified, the target may be ".".
467 * Overall outline of lookup:
470 * identify next component of name at ndp->ni_ptr
471 * handle degenerate case where name is null string
472 * if .. and crossing mount points and on mounted filesys, find parent
473 * call VOP_LOOKUP routine for next component name
474 * directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set
475 * component vnode returned in ni_vp (if it exists), locked.
476 * if result vnode is mounted on and crossing mount points,
477 * find mounted on vnode
478 * if more components of name, do next level at dirloop
479 * return the answer in ni_vp, locked if LOCKLEAF set
480 * if LOCKPARENT set, return locked parent in ni_dvp
481 * if WANTPARENT set, return unlocked parent in ni_dvp
484 lookup(struct nameidata *ndp)
486 char *cp; /* pointer into pathname argument */
487 struct vnode *dp = 0; /* the directory we are searching */
488 struct vnode *tdp; /* saved dp */
489 struct mount *mp; /* mount table entry */
491 int docache; /* == 0 do not cache last component */
492 int wantparent; /* 1 => wantparent or lockparent flag */
493 int rdonly; /* lookup read-only flag bit */
495 int dpunlocked = 0; /* dp has already been unlocked */
496 struct componentname *cnp = &ndp->ni_cnd;
497 int vfslocked; /* VFS Giant state for child */
498 int dvfslocked; /* VFS Giant state for parent */
503 * Setup: break out flag bits into variables.
505 dvfslocked = (ndp->ni_cnd.cn_flags & GIANTHELD) != 0;
507 ndp->ni_cnd.cn_flags &= ~GIANTHELD;
508 wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT);
509 KASSERT(cnp->cn_nameiop == LOOKUP || wantparent,
510 ("CREATE, DELETE, RENAME require LOCKPARENT or WANTPARENT."));
511 docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE;
512 if (cnp->cn_nameiop == DELETE ||
513 (wantparent && cnp->cn_nameiop != CREATE &&
514 cnp->cn_nameiop != LOOKUP))
516 rdonly = cnp->cn_flags & RDONLY;
517 cnp->cn_flags &= ~ISSYMLINK;
520 * We use shared locks until we hit the parent of the last cn then
521 * we adjust based on the requesting flags.
524 cnp->cn_lkflags = LK_SHARED;
526 cnp->cn_lkflags = LK_EXCLUSIVE;
527 dp = ndp->ni_startdir;
528 ndp->ni_startdir = NULLVP;
530 compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags | LK_RETRY));
534 * Search a new directory.
536 * The last component of the filename is left accessible via
537 * cnp->cn_nameptr for callers that need the name. Callers needing
538 * the name set the SAVENAME flag. When done, they assume
539 * responsibility for freeing the pathname buffer.
542 for (cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++)
544 cnp->cn_namelen = cp - cnp->cn_nameptr;
545 if (cnp->cn_namelen > NAME_MAX) {
546 error = ENAMETOOLONG;
549 #ifdef NAMEI_DIAGNOSTIC
552 printf("{%s}: ", cnp->cn_nameptr);
555 ndp->ni_pathlen -= cnp->cn_namelen;
559 * Replace multiple slashes by a single slash and trailing slashes
560 * by a null. This must be done before VOP_LOOKUP() because some
561 * fs's don't know about trailing slashes. Remember if there were
562 * trailing slashes to handle symlinks, existing non-directories
563 * and non-existing files that won't be directories specially later.
565 while (*cp == '/' && (cp[1] == '/' || cp[1] == '\0')) {
569 *ndp->ni_next = '\0';
570 cnp->cn_flags |= TRAILINGSLASH;
575 cnp->cn_flags |= MAKEENTRY;
576 if (*cp == '\0' && docache == 0)
577 cnp->cn_flags &= ~MAKEENTRY;
578 if (cnp->cn_namelen == 2 &&
579 cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.')
580 cnp->cn_flags |= ISDOTDOT;
582 cnp->cn_flags &= ~ISDOTDOT;
583 if (*ndp->ni_next == 0)
584 cnp->cn_flags |= ISLASTCN;
586 cnp->cn_flags &= ~ISLASTCN;
588 if ((cnp->cn_flags & ISLASTCN) != 0 &&
589 cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.' &&
590 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
596 * Check for degenerate name (e.g. / or "")
597 * which is a way of talking about a directory,
598 * e.g. like "/." or ".".
600 if (cnp->cn_nameptr[0] == '\0') {
601 if (dp->v_type != VDIR) {
605 if (cnp->cn_nameiop != LOOKUP) {
615 if (cnp->cn_flags & AUDITVNODE1)
616 AUDIT_ARG_VNODE1(dp);
617 else if (cnp->cn_flags & AUDITVNODE2)
618 AUDIT_ARG_VNODE2(dp);
620 if (!(cnp->cn_flags & (LOCKPARENT | LOCKLEAF)))
622 /* XXX This should probably move to the top of function. */
623 if (cnp->cn_flags & SAVESTART)
624 panic("lookup: SAVESTART");
629 * Handle "..": five special cases.
630 * 0. If doing a capability lookup, return ENOTCAPABLE (this is a
631 * fairly conservative design choice, but it's the only one that we
632 * are satisfied guarantees the property we're looking for).
633 * 1. Return an error if this is the last component of
634 * the name and the operation is DELETE or RENAME.
635 * 2. If at root directory (e.g. after chroot)
636 * or at absolute root directory
637 * then ignore it so can't get out.
638 * 3. If this vnode is the root of a mounted
639 * filesystem, then replace it with the
640 * vnode which was mounted on so we take the
641 * .. in the other filesystem.
642 * 4. If the vnode is the top directory of
643 * the jail or chroot, don't let them out.
645 if (cnp->cn_flags & ISDOTDOT) {
646 if (ndp->ni_strictrelative != 0) {
650 if ((cnp->cn_flags & ISLASTCN) != 0 &&
651 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
656 for (pr = cnp->cn_cred->cr_prison; pr != NULL;
658 if (dp == pr->pr_root)
660 if (dp == ndp->ni_rootdir ||
661 dp == ndp->ni_topdir ||
664 ((dp->v_vflag & VV_ROOT) != 0 &&
665 (cnp->cn_flags & NOCROSSMOUNT) != 0)) {
668 vfslocked = VFS_LOCK_GIANT(dp->v_mount);
672 if ((dp->v_vflag & VV_ROOT) == 0)
674 if (dp->v_iflag & VI_DOOMED) { /* forced unmount */
679 dp = dp->v_mount->mnt_vnodecovered;
680 tvfslocked = dvfslocked;
681 dvfslocked = VFS_LOCK_GIANT(dp->v_mount);
684 VFS_UNLOCK_GIANT(tvfslocked);
686 compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags |
692 * We now have a segment name to search for, and a directory to search.
696 if ((cnp->cn_flags & NOMACCHECK) == 0) {
697 error = mac_vnode_check_lookup(cnp->cn_thread->td_ucred, dp,
705 ASSERT_VOP_LOCKED(dp, "lookup");
706 VNASSERT(vfslocked == 0, dp, ("lookup: vfslocked %d", vfslocked));
708 * If we have a shared lock we may need to upgrade the lock for the
711 if (dp != vp_crossmp &&
712 VOP_ISLOCKED(dp) == LK_SHARED &&
713 (cnp->cn_flags & ISLASTCN) && (cnp->cn_flags & LOCKPARENT))
714 vn_lock(dp, LK_UPGRADE|LK_RETRY);
716 * If we're looking up the last component and we need an exclusive
717 * lock, adjust our lkflags.
719 if (needs_exclusive_leaf(dp->v_mount, cnp->cn_flags))
720 cnp->cn_lkflags = LK_EXCLUSIVE;
721 #ifdef NAMEI_DIAGNOSTIC
722 vprint("lookup in", dp);
724 lkflags_save = cnp->cn_lkflags;
725 cnp->cn_lkflags = compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags);
726 if ((error = VOP_LOOKUP(dp, &ndp->ni_vp, cnp)) != 0) {
727 cnp->cn_lkflags = lkflags_save;
728 KASSERT(ndp->ni_vp == NULL, ("leaf should be empty"));
729 #ifdef NAMEI_DIAGNOSTIC
730 printf("not found\n");
732 if ((error == ENOENT) &&
733 (dp->v_vflag & VV_ROOT) && (dp->v_mount != NULL) &&
734 (dp->v_mount->mnt_flag & MNT_UNION)) {
736 dp = dp->v_mount->mnt_vnodecovered;
737 tvfslocked = dvfslocked;
738 dvfslocked = VFS_LOCK_GIANT(dp->v_mount);
741 VFS_UNLOCK_GIANT(tvfslocked);
743 compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags |
748 if (error != EJUSTRETURN)
751 * At this point, we know we're at the end of the
752 * pathname. If creating / renaming, we can consider
753 * allowing the file or directory to be created / renamed,
754 * provided we're not on a read-only filesystem.
760 /* trailing slash only allowed for directories */
761 if ((cnp->cn_flags & TRAILINGSLASH) &&
762 !(cnp->cn_flags & WILLBEDIR)) {
766 if ((cnp->cn_flags & LOCKPARENT) == 0)
769 * We return with ni_vp NULL to indicate that the entry
770 * doesn't currently exist, leaving a pointer to the
771 * (possibly locked) directory vnode in ndp->ni_dvp.
773 if (cnp->cn_flags & SAVESTART) {
774 ndp->ni_startdir = ndp->ni_dvp;
775 VREF(ndp->ni_startdir);
779 cnp->cn_lkflags = lkflags_save;
780 #ifdef NAMEI_DIAGNOSTIC
784 * Take into account any additional components consumed by
785 * the underlying filesystem.
787 if (cnp->cn_consume > 0) {
788 cnp->cn_nameptr += cnp->cn_consume;
789 ndp->ni_next += cnp->cn_consume;
790 ndp->ni_pathlen -= cnp->cn_consume;
795 vfslocked = VFS_LOCK_GIANT(dp->v_mount);
798 * Check to see if the vnode has been mounted on;
799 * if so find the root of the mounted filesystem.
801 while (dp->v_type == VDIR && (mp = dp->v_mountedhere) &&
802 (cnp->cn_flags & NOCROSSMOUNT) == 0) {
806 VFS_UNLOCK_GIANT(vfslocked);
807 vfslocked = VFS_LOCK_GIANT(mp);
808 if (dp != ndp->ni_dvp)
812 VFS_UNLOCK_GIANT(dvfslocked);
815 ndp->ni_dvp = vp_crossmp;
816 error = VFS_ROOT(mp, compute_cn_lkflags(mp, cnp->cn_lkflags),
819 if (vn_lock(vp_crossmp, LK_SHARED | LK_NOWAIT))
820 panic("vp_crossmp exclusively locked or reclaimed");
825 ndp->ni_vp = dp = tdp;
829 * Check for symbolic link
831 if ((dp->v_type == VLNK) &&
832 ((cnp->cn_flags & FOLLOW) || (cnp->cn_flags & TRAILINGSLASH) ||
833 *ndp->ni_next == '/')) {
834 cnp->cn_flags |= ISSYMLINK;
835 if (dp->v_iflag & VI_DOOMED) {
837 * We can't know whether the directory was mounted with
838 * NOSYMFOLLOW, so we can't follow safely.
843 if (dp->v_mount->mnt_flag & MNT_NOSYMFOLLOW) {
848 * Symlink code always expects an unlocked dvp.
850 if (ndp->ni_dvp != ndp->ni_vp)
851 VOP_UNLOCK(ndp->ni_dvp, 0);
857 * Not a symbolic link that we will follow. Continue with the
858 * next component if there is any; otherwise, we're done.
860 KASSERT((cnp->cn_flags & ISLASTCN) || *ndp->ni_next == '/',
861 ("lookup: invalid path state."));
862 if (*ndp->ni_next == '/') {
863 cnp->cn_nameptr = ndp->ni_next;
864 while (*cnp->cn_nameptr == '/') {
868 if (ndp->ni_dvp != dp)
872 VFS_UNLOCK_GIANT(dvfslocked);
873 dvfslocked = vfslocked; /* dp becomes dvp in dirloop */
878 * If we're processing a path with a trailing slash,
879 * check that the end result is a directory.
881 if ((cnp->cn_flags & TRAILINGSLASH) && dp->v_type != VDIR) {
886 * Disallow directory write attempts on read-only filesystems.
889 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
893 if (cnp->cn_flags & SAVESTART) {
894 ndp->ni_startdir = ndp->ni_dvp;
895 VREF(ndp->ni_startdir);
898 if (ndp->ni_dvp != dp)
902 VFS_UNLOCK_GIANT(dvfslocked);
904 } else if ((cnp->cn_flags & LOCKPARENT) == 0 && ndp->ni_dvp != dp)
905 VOP_UNLOCK(ndp->ni_dvp, 0);
907 if (cnp->cn_flags & AUDITVNODE1)
908 AUDIT_ARG_VNODE1(dp);
909 else if (cnp->cn_flags & AUDITVNODE2)
910 AUDIT_ARG_VNODE2(dp);
912 if ((cnp->cn_flags & LOCKLEAF) == 0)
916 * Because of lookup_shared we may have the vnode shared locked, but
917 * the caller may want it to be exclusively locked.
919 if (needs_exclusive_leaf(dp->v_mount, cnp->cn_flags) &&
920 VOP_ISLOCKED(dp) != LK_EXCLUSIVE) {
921 vn_lock(dp, LK_UPGRADE | LK_RETRY);
922 if (dp->v_iflag & VI_DOOMED) {
927 if (vfslocked && dvfslocked)
928 VFS_UNLOCK_GIANT(dvfslocked); /* Only need one */
929 if (vfslocked || dvfslocked)
930 ndp->ni_cnd.cn_flags |= GIANTHELD;
934 if (dp != ndp->ni_dvp)
941 VFS_UNLOCK_GIANT(vfslocked);
942 VFS_UNLOCK_GIANT(dvfslocked);
943 ndp->ni_cnd.cn_flags &= ~GIANTHELD;
949 * relookup - lookup a path name component
950 * Used by lookup to re-acquire things.
953 relookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp)
955 struct vnode *dp = 0; /* the directory we are searching */
956 int wantparent; /* 1 => wantparent or lockparent flag */
957 int rdonly; /* lookup read-only flag bit */
960 KASSERT(cnp->cn_flags & ISLASTCN,
961 ("relookup: Not given last component."));
963 * Setup: break out flag bits into variables.
965 wantparent = cnp->cn_flags & (LOCKPARENT|WANTPARENT);
966 KASSERT(wantparent, ("relookup: parent not wanted."));
967 rdonly = cnp->cn_flags & RDONLY;
968 cnp->cn_flags &= ~ISSYMLINK;
970 cnp->cn_lkflags = LK_EXCLUSIVE;
971 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
974 * Search a new directory.
976 * The last component of the filename is left accessible via
977 * cnp->cn_nameptr for callers that need the name. Callers needing
978 * the name set the SAVENAME flag. When done, they assume
979 * responsibility for freeing the pathname buffer.
981 #ifdef NAMEI_DIAGNOSTIC
982 printf("{%s}: ", cnp->cn_nameptr);
986 * Check for "" which represents the root directory after slash
989 if (cnp->cn_nameptr[0] == '\0') {
991 * Support only LOOKUP for "/" because lookup()
992 * can't succeed for CREATE, DELETE and RENAME.
994 KASSERT(cnp->cn_nameiop == LOOKUP, ("nameiop must be LOOKUP"));
995 KASSERT(dp->v_type == VDIR, ("dp is not a directory"));
997 if (!(cnp->cn_flags & LOCKLEAF))
1000 /* XXX This should probably move to the top of function. */
1001 if (cnp->cn_flags & SAVESTART)
1002 panic("lookup: SAVESTART");
1006 if (cnp->cn_flags & ISDOTDOT)
1007 panic ("relookup: lookup on dot-dot");
1010 * We now have a segment name to search for, and a directory to search.
1012 #ifdef NAMEI_DIAGNOSTIC
1013 vprint("search in:", dp);
1015 if ((error = VOP_LOOKUP(dp, vpp, cnp)) != 0) {
1016 KASSERT(*vpp == NULL, ("leaf should be empty"));
1017 if (error != EJUSTRETURN)
1020 * If creating and at end of pathname, then can consider
1021 * allowing file to be created.
1027 /* ASSERT(dvp == ndp->ni_startdir) */
1028 if (cnp->cn_flags & SAVESTART)
1030 if ((cnp->cn_flags & LOCKPARENT) == 0)
1033 * We return with ni_vp NULL to indicate that the entry
1034 * doesn't currently exist, leaving a pointer to the
1035 * (possibly locked) directory vnode in ndp->ni_dvp.
1043 * Disallow directory write attempts on read-only filesystems.
1046 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
1055 * Set the parent lock/ref state to the requested state.
1057 if ((cnp->cn_flags & LOCKPARENT) == 0 && dvp != dp) {
1062 } else if (!wantparent)
1065 * Check for symbolic link
1067 KASSERT(dp->v_type != VLNK || !(cnp->cn_flags & FOLLOW),
1068 ("relookup: symlink found.\n"));
1070 /* ASSERT(dvp == ndp->ni_startdir) */
1071 if (cnp->cn_flags & SAVESTART)
1074 if ((cnp->cn_flags & LOCKLEAF) == 0)
1084 * Free data allocated by namei(); see namei(9) for details.
1087 NDFREE(struct nameidata *ndp, const u_int flags)
1095 if (!(flags & NDF_NO_FREE_PNBUF) &&
1096 (ndp->ni_cnd.cn_flags & HASBUF)) {
1097 uma_zfree(namei_zone, ndp->ni_cnd.cn_pnbuf);
1098 ndp->ni_cnd.cn_flags &= ~HASBUF;
1100 if (!(flags & NDF_NO_VP_UNLOCK) &&
1101 (ndp->ni_cnd.cn_flags & LOCKLEAF) && ndp->ni_vp)
1103 if (!(flags & NDF_NO_VP_RELE) && ndp->ni_vp) {
1112 VOP_UNLOCK(ndp->ni_vp, 0);
1113 if (!(flags & NDF_NO_DVP_UNLOCK) &&
1114 (ndp->ni_cnd.cn_flags & LOCKPARENT) &&
1115 ndp->ni_dvp != ndp->ni_vp)
1117 if (!(flags & NDF_NO_DVP_RELE) &&
1118 (ndp->ni_cnd.cn_flags & (LOCKPARENT|WANTPARENT))) {
1127 VOP_UNLOCK(ndp->ni_dvp, 0);
1128 if (!(flags & NDF_NO_STARTDIR_RELE) &&
1129 (ndp->ni_cnd.cn_flags & SAVESTART)) {
1130 vrele(ndp->ni_startdir);
1131 ndp->ni_startdir = NULL;
1136 * Determine if there is a suitable alternate filename under the specified
1137 * prefix for the specified path. If the create flag is set, then the
1138 * alternate prefix will be used so long as the parent directory exists.
1139 * This is used by the various compatiblity ABIs so that Linux binaries prefer
1140 * files under /compat/linux for example. The chosen path (whether under
1141 * the prefix or under /) is returned in a kernel malloc'd buffer pointed
1142 * to by pathbuf. The caller is responsible for free'ing the buffer from
1143 * the M_TEMP bucket if one is returned.
1146 kern_alternate_path(struct thread *td, const char *prefix, const char *path,
1147 enum uio_seg pathseg, char **pathbuf, int create, int dirfd)
1149 struct nameidata nd, ndroot;
1150 char *ptr, *buf, *cp;
1154 buf = (char *) malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1157 /* Copy the prefix into the new pathname as a starting point. */
1158 len = strlcpy(buf, prefix, MAXPATHLEN);
1159 if (len >= MAXPATHLEN) {
1164 sz = MAXPATHLEN - len;
1167 /* Append the filename to the prefix. */
1168 if (pathseg == UIO_SYSSPACE)
1169 error = copystr(path, ptr, sz, &len);
1171 error = copyinstr(path, ptr, sz, &len);
1179 /* Only use a prefix with absolute pathnames. */
1185 if (dirfd != AT_FDCWD) {
1187 * We want the original because the "prefix" is
1188 * included in the already opened dirfd.
1190 bcopy(ptr, buf, len);
1195 * We know that there is a / somewhere in this pathname.
1196 * Search backwards for it, to find the file's parent dir
1197 * to see if it exists in the alternate tree. If it does,
1198 * and we want to create a file (cflag is set). We don't
1199 * need to worry about the root comparison in this case.
1203 for (cp = &ptr[len] - 1; *cp != '/'; cp--);
1206 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, UIO_SYSSPACE, buf, td);
1212 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, UIO_SYSSPACE, buf, td);
1219 * We now compare the vnode of the prefix to the one
1220 * vnode asked. If they resolve to be the same, then we
1221 * ignore the match so that the real root gets used.
1222 * This avoids the problem of traversing "../.." to find the
1223 * root directory and never finding it, because "/" resolves
1224 * to the emulation root directory. This is expensive :-(
1226 NDINIT(&ndroot, LOOKUP, FOLLOW | MPSAFE, UIO_SYSSPACE, prefix,
1229 /* We shouldn't ever get an error from this namei(). */
1230 error = namei(&ndroot);
1232 if (nd.ni_vp == ndroot.ni_vp)
1235 NDFREE(&ndroot, NDF_ONLY_PNBUF);
1236 vrele(ndroot.ni_vp);
1237 VFS_UNLOCK_GIANT(NDHASGIANT(&ndroot));
1241 NDFREE(&nd, NDF_ONLY_PNBUF);
1243 VFS_UNLOCK_GIANT(NDHASGIANT(&nd));
1246 /* If there was an error, use the original path name. */
1248 bcopy(ptr, buf, len);