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_kdtrace.h"
41 #include "opt_ktrace.h"
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/fcntl.h>
49 #include <sys/mutex.h>
50 #include <sys/namei.h>
51 #include <sys/vnode.h>
52 #include <sys/mount.h>
53 #include <sys/filedesc.h>
56 #include <sys/syscallsubr.h>
57 #include <sys/sysctl.h>
59 #include <sys/ktrace.h>
62 #include <security/audit/audit.h>
63 #include <security/mac/mac_framework.h>
67 #define NAMEI_DIAGNOSTIC 1
68 #undef NAMEI_DIAGNOSTIC
70 SDT_PROVIDER_DECLARE(vfs);
71 SDT_PROBE_DEFINE3(vfs, namei, lookup, entry, "struct vnode *", "char *",
73 SDT_PROBE_DEFINE2(vfs, namei, lookup, return, "int", "struct vnode *");
76 * Allocation zone for namei
78 uma_zone_t namei_zone;
80 * Placeholder vnode for mp traversal
82 static struct vnode *vp_crossmp;
85 nameiinit(void *dummy __unused)
89 namei_zone = uma_zcreate("NAMEI", MAXPATHLEN, NULL, NULL, NULL, NULL,
91 error = getnewvnode("crossmp", NULL, &dead_vnodeops, &vp_crossmp);
93 panic("nameiinit: getnewvnode");
94 VN_LOCK_ASHARE(vp_crossmp);
96 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_SECOND, nameiinit, NULL);
98 static int lookup_shared = 1;
99 SYSCTL_INT(_vfs, OID_AUTO, lookup_shared, CTLFLAG_RW, &lookup_shared, 0,
100 "Enables/Disables shared locks for path name translation");
101 TUNABLE_INT("vfs.lookup_shared", &lookup_shared);
104 * Convert a pathname into a pointer to a locked vnode.
106 * The FOLLOW flag is set when symbolic links are to be followed
107 * when they occur at the end of the name translation process.
108 * Symbolic links are always followed for all other pathname
109 * components other than the last.
111 * The segflg defines whether the name is to be copied from user
112 * space or kernel space.
114 * Overall outline of namei:
117 * get starting directory
118 * while (!done && !error) {
119 * call lookup to search path.
120 * if symbolic link, massage name in buffer and continue
124 namei(struct nameidata *ndp)
126 struct filedesc *fdp; /* pointer to file descriptor state */
127 char *cp; /* pointer into pathname argument */
128 struct vnode *dp; /* the directory we are searching */
129 struct iovec aiov; /* uio for reading symbolic links */
132 struct componentname *cnp = &ndp->ni_cnd;
133 struct thread *td = cnp->cn_thread;
134 struct proc *p = td->td_proc;
137 KASSERT((cnp->cn_flags & MPSAFE) != 0 || mtx_owned(&Giant) != 0,
138 ("NOT MPSAFE and Giant not held"));
139 ndp->ni_cnd.cn_cred = ndp->ni_cnd.cn_thread->td_ucred;
140 KASSERT(cnp->cn_cred && p, ("namei: bad cred/proc"));
141 KASSERT((cnp->cn_nameiop & (~OPMASK)) == 0,
142 ("namei: nameiop contaminated with flags"));
143 KASSERT((cnp->cn_flags & OPMASK) == 0,
144 ("namei: flags contaminated with nameiops"));
146 cnp->cn_flags &= ~LOCKSHARED;
149 /* We will set this ourselves if we need it. */
150 cnp->cn_flags &= ~TRAILINGSLASH;
153 * Get a buffer for the name to be translated, and copy the
154 * name into the buffer.
156 if ((cnp->cn_flags & HASBUF) == 0)
157 cnp->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK);
158 if (ndp->ni_segflg == UIO_SYSSPACE)
159 error = copystr(ndp->ni_dirp, cnp->cn_pnbuf,
160 MAXPATHLEN, (size_t *)&ndp->ni_pathlen);
162 error = copyinstr(ndp->ni_dirp, cnp->cn_pnbuf,
163 MAXPATHLEN, (size_t *)&ndp->ni_pathlen);
167 * If we are auditing the kernel pathname, save the user
170 if (cnp->cn_flags & AUDITVNODE1)
171 AUDIT_ARG_UPATH1(td, cnp->cn_pnbuf);
172 if (cnp->cn_flags & AUDITVNODE2)
173 AUDIT_ARG_UPATH2(td, cnp->cn_pnbuf);
177 * Don't allow empty pathnames.
179 if (!error && *cnp->cn_pnbuf == '\0')
183 uma_zfree(namei_zone, cnp->cn_pnbuf);
185 cnp->cn_pnbuf = NULL;
186 cnp->cn_nameptr = NULL;
193 if (KTRPOINT(td, KTR_NAMEI)) {
194 KASSERT(cnp->cn_thread == curthread,
195 ("namei not using curthread"));
196 ktrnamei(cnp->cn_pnbuf);
200 * Get starting point for the translation.
203 ndp->ni_rootdir = fdp->fd_rdir;
204 ndp->ni_topdir = fdp->fd_jdir;
207 if (cnp->cn_pnbuf[0] != '/') {
208 if (ndp->ni_startdir != NULL) {
209 dp = ndp->ni_startdir;
211 } else if (ndp->ni_dirfd != AT_FDCWD) {
212 if (cnp->cn_flags & AUDITVNODE1)
213 AUDIT_ARG_ATFD1(ndp->ni_dirfd);
214 if (cnp->cn_flags & AUDITVNODE2)
215 AUDIT_ARG_ATFD2(ndp->ni_dirfd);
216 error = fgetvp(td, ndp->ni_dirfd, &dp);
218 if (error != 0 || dp != NULL) {
219 FILEDESC_SUNLOCK(fdp);
220 if (error == 0 && dp->v_type != VDIR) {
221 vfslocked = VFS_LOCK_GIANT(dp->v_mount);
223 VFS_UNLOCK_GIANT(vfslocked);
228 uma_zfree(namei_zone, cnp->cn_pnbuf);
230 cnp->cn_pnbuf = NULL;
231 cnp->cn_nameptr = NULL;
239 FILEDESC_SUNLOCK(fdp);
240 if (ndp->ni_startdir != NULL) {
241 vfslocked = VFS_LOCK_GIANT(ndp->ni_startdir->v_mount);
242 vrele(ndp->ni_startdir);
243 VFS_UNLOCK_GIANT(vfslocked);
246 SDT_PROBE(vfs, namei, lookup, entry, dp, cnp->cn_pnbuf,
247 cnp->cn_flags, 0, 0);
248 vfslocked = VFS_LOCK_GIANT(dp->v_mount);
251 * Check if root directory should replace current directory.
252 * Done at start of translation and after symbolic link.
254 cnp->cn_nameptr = cnp->cn_pnbuf;
255 if (*(cnp->cn_nameptr) == '/') {
257 VFS_UNLOCK_GIANT(vfslocked);
258 while (*(cnp->cn_nameptr) == '/') {
262 dp = ndp->ni_rootdir;
263 vfslocked = VFS_LOCK_GIANT(dp->v_mount);
267 ndp->ni_cnd.cn_flags |= GIANTHELD;
268 ndp->ni_startdir = dp;
271 uma_zfree(namei_zone, cnp->cn_pnbuf);
273 cnp->cn_pnbuf = NULL;
274 cnp->cn_nameptr = NULL;
276 SDT_PROBE(vfs, namei, lookup, return, error, NULL, 0,
280 vfslocked = (ndp->ni_cnd.cn_flags & GIANTHELD) != 0;
281 ndp->ni_cnd.cn_flags &= ~GIANTHELD;
283 * If not a symbolic link, we're done.
285 if ((cnp->cn_flags & ISSYMLINK) == 0) {
286 if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0) {
287 uma_zfree(namei_zone, cnp->cn_pnbuf);
289 cnp->cn_pnbuf = NULL;
290 cnp->cn_nameptr = NULL;
293 cnp->cn_flags |= HASBUF;
295 if ((cnp->cn_flags & MPSAFE) == 0) {
296 VFS_UNLOCK_GIANT(vfslocked);
297 } else if (vfslocked)
298 ndp->ni_cnd.cn_flags |= GIANTHELD;
299 SDT_PROBE(vfs, namei, lookup, return, 0, ndp->ni_vp,
303 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
308 if ((cnp->cn_flags & NOMACCHECK) == 0) {
309 error = mac_vnode_check_readlink(td->td_ucred,
315 if (ndp->ni_pathlen > 1)
316 cp = uma_zalloc(namei_zone, M_WAITOK);
320 aiov.iov_len = MAXPATHLEN;
321 auio.uio_iov = &aiov;
324 auio.uio_rw = UIO_READ;
325 auio.uio_segflg = UIO_SYSSPACE;
326 auio.uio_td = (struct thread *)0;
327 auio.uio_resid = MAXPATHLEN;
328 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
330 if (ndp->ni_pathlen > 1)
331 uma_zfree(namei_zone, cp);
334 linklen = MAXPATHLEN - auio.uio_resid;
336 if (ndp->ni_pathlen > 1)
337 uma_zfree(namei_zone, cp);
341 if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
342 if (ndp->ni_pathlen > 1)
343 uma_zfree(namei_zone, cp);
344 error = ENAMETOOLONG;
347 if (ndp->ni_pathlen > 1) {
348 bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
349 uma_zfree(namei_zone, cnp->cn_pnbuf);
352 cnp->cn_pnbuf[linklen] = '\0';
353 ndp->ni_pathlen += linklen;
357 uma_zfree(namei_zone, cnp->cn_pnbuf);
359 cnp->cn_pnbuf = NULL;
360 cnp->cn_nameptr = NULL;
365 VFS_UNLOCK_GIANT(vfslocked);
366 SDT_PROBE(vfs, namei, lookup, return, error, NULL, 0, 0, 0);
371 compute_cn_lkflags(struct mount *mp, int lkflags)
375 ((lkflags & LK_SHARED) && !(mp->mnt_kern_flag & MNTK_LOOKUP_SHARED))) {
376 lkflags &= ~LK_SHARED;
377 lkflags |= LK_EXCLUSIVE;
383 needs_exclusive_leaf(struct mount *mp, int flags)
387 * Intermediate nodes can use shared locks, we only need to
388 * force an exclusive lock for leaf nodes.
390 if ((flags & (ISLASTCN | LOCKLEAF)) != (ISLASTCN | LOCKLEAF))
393 /* Always use exclusive locks if LOCKSHARED isn't set. */
394 if (!(flags & LOCKSHARED))
398 * For lookups during open(), if the mount point supports
399 * extended shared operations, then use a shared lock for the
400 * leaf node, otherwise use an exclusive lock.
402 if (flags & ISOPEN) {
404 (mp->mnt_kern_flag & MNTK_EXTENDED_SHARED))
411 * Lookup requests outside of open() that specify LOCKSHARED
412 * only need a shared lock on the leaf vnode.
419 * This is a very central and rather complicated routine.
421 * The pathname is pointed to by ni_ptr and is of length ni_pathlen.
422 * The starting directory is taken from ni_startdir. The pathname is
423 * descended until done, or a symbolic link is encountered. The variable
424 * ni_more is clear if the path is completed; it is set to one if a
425 * symbolic link needing interpretation is encountered.
427 * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
428 * whether the name is to be looked up, created, renamed, or deleted.
429 * When CREATE, RENAME, or DELETE is specified, information usable in
430 * creating, renaming, or deleting a directory entry may be calculated.
431 * If flag has LOCKPARENT or'ed into it, the parent directory is returned
432 * locked. If flag has WANTPARENT or'ed into it, the parent directory is
433 * returned unlocked. Otherwise the parent directory is not returned. If
434 * the target of the pathname exists and LOCKLEAF is or'ed into the flag
435 * the target is returned locked, otherwise it is returned unlocked.
436 * When creating or renaming and LOCKPARENT is specified, the target may not
437 * be ".". When deleting and LOCKPARENT is specified, the target may be ".".
439 * Overall outline of lookup:
442 * identify next component of name at ndp->ni_ptr
443 * handle degenerate case where name is null string
444 * if .. and crossing mount points and on mounted filesys, find parent
445 * call VOP_LOOKUP routine for next component name
446 * directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set
447 * component vnode returned in ni_vp (if it exists), locked.
448 * if result vnode is mounted on and crossing mount points,
449 * find mounted on vnode
450 * if more components of name, do next level at dirloop
451 * return the answer in ni_vp, locked if LOCKLEAF set
452 * if LOCKPARENT set, return locked parent in ni_dvp
453 * if WANTPARENT set, return unlocked parent in ni_dvp
456 lookup(struct nameidata *ndp)
458 char *cp; /* pointer into pathname argument */
459 struct vnode *dp = 0; /* the directory we are searching */
460 struct vnode *tdp; /* saved dp */
461 struct mount *mp; /* mount table entry */
463 int docache; /* == 0 do not cache last component */
464 int wantparent; /* 1 => wantparent or lockparent flag */
465 int rdonly; /* lookup read-only flag bit */
467 int dpunlocked = 0; /* dp has already been unlocked */
468 struct componentname *cnp = &ndp->ni_cnd;
469 int vfslocked; /* VFS Giant state for child */
470 int dvfslocked; /* VFS Giant state for parent */
475 * Setup: break out flag bits into variables.
477 dvfslocked = (ndp->ni_cnd.cn_flags & GIANTHELD) != 0;
479 ndp->ni_cnd.cn_flags &= ~GIANTHELD;
480 wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT);
481 KASSERT(cnp->cn_nameiop == LOOKUP || wantparent,
482 ("CREATE, DELETE, RENAME require LOCKPARENT or WANTPARENT."));
483 docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE;
484 if (cnp->cn_nameiop == DELETE ||
485 (wantparent && cnp->cn_nameiop != CREATE &&
486 cnp->cn_nameiop != LOOKUP))
488 rdonly = cnp->cn_flags & RDONLY;
489 cnp->cn_flags &= ~ISSYMLINK;
492 * We use shared locks until we hit the parent of the last cn then
493 * we adjust based on the requesting flags.
496 cnp->cn_lkflags = LK_SHARED;
498 cnp->cn_lkflags = LK_EXCLUSIVE;
499 dp = ndp->ni_startdir;
500 ndp->ni_startdir = NULLVP;
502 compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags | LK_RETRY));
506 * Search a new directory.
508 * The last component of the filename is left accessible via
509 * cnp->cn_nameptr for callers that need the name. Callers needing
510 * the name set the SAVENAME flag. When done, they assume
511 * responsibility for freeing the pathname buffer.
514 for (cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++)
516 cnp->cn_namelen = cp - cnp->cn_nameptr;
517 if (cnp->cn_namelen > NAME_MAX) {
518 error = ENAMETOOLONG;
521 #ifdef NAMEI_DIAGNOSTIC
524 printf("{%s}: ", cnp->cn_nameptr);
527 ndp->ni_pathlen -= cnp->cn_namelen;
531 * Replace multiple slashes by a single slash and trailing slashes
532 * by a null. This must be done before VOP_LOOKUP() because some
533 * fs's don't know about trailing slashes. Remember if there were
534 * trailing slashes to handle symlinks, existing non-directories
535 * and non-existing files that won't be directories specially later.
537 while (*cp == '/' && (cp[1] == '/' || cp[1] == '\0')) {
541 *ndp->ni_next = '\0';
542 cnp->cn_flags |= TRAILINGSLASH;
547 cnp->cn_flags |= MAKEENTRY;
548 if (*cp == '\0' && docache == 0)
549 cnp->cn_flags &= ~MAKEENTRY;
550 if (cnp->cn_namelen == 2 &&
551 cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.')
552 cnp->cn_flags |= ISDOTDOT;
554 cnp->cn_flags &= ~ISDOTDOT;
555 if (*ndp->ni_next == 0)
556 cnp->cn_flags |= ISLASTCN;
558 cnp->cn_flags &= ~ISLASTCN;
560 if ((cnp->cn_flags & ISLASTCN) != 0 &&
561 cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.' &&
562 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
568 * Check for degenerate name (e.g. / or "")
569 * which is a way of talking about a directory,
570 * e.g. like "/." or ".".
572 if (cnp->cn_nameptr[0] == '\0') {
573 if (dp->v_type != VDIR) {
577 if (cnp->cn_nameiop != LOOKUP) {
587 if (cnp->cn_flags & AUDITVNODE1)
588 AUDIT_ARG_VNODE1(dp);
589 else if (cnp->cn_flags & AUDITVNODE2)
590 AUDIT_ARG_VNODE2(dp);
592 if (!(cnp->cn_flags & (LOCKPARENT | LOCKLEAF)))
594 /* XXX This should probably move to the top of function. */
595 if (cnp->cn_flags & SAVESTART)
596 panic("lookup: SAVESTART");
601 * Handle "..": four special cases.
602 * 1. Return an error if this is the last component of
603 * the name and the operation is DELETE or RENAME.
604 * 2. If at root directory (e.g. after chroot)
605 * or at absolute root directory
606 * then ignore it so can't get out.
607 * 3. If this vnode is the root of a mounted
608 * filesystem, then replace it with the
609 * vnode which was mounted on so we take the
610 * .. in the other filesystem.
611 * 4. If the vnode is the top directory of
612 * the jail or chroot, don't let them out.
614 if (cnp->cn_flags & ISDOTDOT) {
615 if ((cnp->cn_flags & ISLASTCN) != 0 &&
616 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
621 for (pr = cnp->cn_cred->cr_prison; pr != NULL;
623 if (dp == pr->pr_root)
625 if (dp == ndp->ni_rootdir ||
626 dp == ndp->ni_topdir ||
629 ((dp->v_vflag & VV_ROOT) != 0 &&
630 (cnp->cn_flags & NOCROSSMOUNT) != 0)) {
633 vfslocked = VFS_LOCK_GIANT(dp->v_mount);
637 if ((dp->v_vflag & VV_ROOT) == 0)
639 if (dp->v_iflag & VI_DOOMED) { /* forced unmount */
644 dp = dp->v_mount->mnt_vnodecovered;
645 tvfslocked = dvfslocked;
646 dvfslocked = VFS_LOCK_GIANT(dp->v_mount);
649 VFS_UNLOCK_GIANT(tvfslocked);
651 compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags |
657 * We now have a segment name to search for, and a directory to search.
661 if ((cnp->cn_flags & NOMACCHECK) == 0) {
662 error = mac_vnode_check_lookup(cnp->cn_thread->td_ucred, dp,
670 ASSERT_VOP_LOCKED(dp, "lookup");
671 VNASSERT(vfslocked == 0, dp, ("lookup: vfslocked %d", vfslocked));
673 * If we have a shared lock we may need to upgrade the lock for the
676 if (dp != vp_crossmp &&
677 VOP_ISLOCKED(dp) == LK_SHARED &&
678 (cnp->cn_flags & ISLASTCN) && (cnp->cn_flags & LOCKPARENT))
679 vn_lock(dp, LK_UPGRADE|LK_RETRY);
681 * If we're looking up the last component and we need an exclusive
682 * lock, adjust our lkflags.
684 if (needs_exclusive_leaf(dp->v_mount, cnp->cn_flags))
685 cnp->cn_lkflags = LK_EXCLUSIVE;
686 #ifdef NAMEI_DIAGNOSTIC
687 vprint("lookup in", dp);
689 lkflags_save = cnp->cn_lkflags;
690 cnp->cn_lkflags = compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags);
691 if ((error = VOP_LOOKUP(dp, &ndp->ni_vp, cnp)) != 0) {
692 cnp->cn_lkflags = lkflags_save;
693 KASSERT(ndp->ni_vp == NULL, ("leaf should be empty"));
694 #ifdef NAMEI_DIAGNOSTIC
695 printf("not found\n");
697 if ((error == ENOENT) &&
698 (dp->v_vflag & VV_ROOT) && (dp->v_mount != NULL) &&
699 (dp->v_mount->mnt_flag & MNT_UNION)) {
701 dp = dp->v_mount->mnt_vnodecovered;
702 tvfslocked = dvfslocked;
703 dvfslocked = VFS_LOCK_GIANT(dp->v_mount);
706 VFS_UNLOCK_GIANT(tvfslocked);
708 compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags |
713 if (error != EJUSTRETURN)
716 * At this point, we know we're at the end of the
717 * pathname. If creating / renaming, we can consider
718 * allowing the file or directory to be created / renamed,
719 * provided we're not on a read-only filesystem.
725 /* trailing slash only allowed for directories */
726 if ((cnp->cn_flags & TRAILINGSLASH) &&
727 !(cnp->cn_flags & WILLBEDIR)) {
731 if ((cnp->cn_flags & LOCKPARENT) == 0)
734 * We return with ni_vp NULL to indicate that the entry
735 * doesn't currently exist, leaving a pointer to the
736 * (possibly locked) directory vnode in ndp->ni_dvp.
738 if (cnp->cn_flags & SAVESTART) {
739 ndp->ni_startdir = ndp->ni_dvp;
740 VREF(ndp->ni_startdir);
744 cnp->cn_lkflags = lkflags_save;
745 #ifdef NAMEI_DIAGNOSTIC
749 * Take into account any additional components consumed by
750 * the underlying filesystem.
752 if (cnp->cn_consume > 0) {
753 cnp->cn_nameptr += cnp->cn_consume;
754 ndp->ni_next += cnp->cn_consume;
755 ndp->ni_pathlen -= cnp->cn_consume;
760 vfslocked = VFS_LOCK_GIANT(dp->v_mount);
763 * Check to see if the vnode has been mounted on;
764 * if so find the root of the mounted filesystem.
766 while (dp->v_type == VDIR && (mp = dp->v_mountedhere) &&
767 (cnp->cn_flags & NOCROSSMOUNT) == 0) {
771 VFS_UNLOCK_GIANT(vfslocked);
772 vfslocked = VFS_LOCK_GIANT(mp);
773 if (dp != ndp->ni_dvp)
777 VFS_UNLOCK_GIANT(dvfslocked);
780 ndp->ni_dvp = vp_crossmp;
781 error = VFS_ROOT(mp, compute_cn_lkflags(mp, cnp->cn_lkflags),
784 if (vn_lock(vp_crossmp, LK_SHARED | LK_NOWAIT))
785 panic("vp_crossmp exclusively locked or reclaimed");
790 ndp->ni_vp = dp = tdp;
794 * Check for symbolic link
796 if ((dp->v_type == VLNK) &&
797 ((cnp->cn_flags & FOLLOW) || (cnp->cn_flags & TRAILINGSLASH) ||
798 *ndp->ni_next == '/')) {
799 cnp->cn_flags |= ISSYMLINK;
800 if (dp->v_iflag & VI_DOOMED) {
802 * We can't know whether the directory was mounted with
803 * NOSYMFOLLOW, so we can't follow safely.
808 if (dp->v_mount->mnt_flag & MNT_NOSYMFOLLOW) {
813 * Symlink code always expects an unlocked dvp.
815 if (ndp->ni_dvp != ndp->ni_vp)
816 VOP_UNLOCK(ndp->ni_dvp, 0);
822 * Not a symbolic link that we will follow. Continue with the
823 * next component if there is any; otherwise, we're done.
825 KASSERT((cnp->cn_flags & ISLASTCN) || *ndp->ni_next == '/',
826 ("lookup: invalid path state."));
827 if (*ndp->ni_next == '/') {
828 cnp->cn_nameptr = ndp->ni_next;
829 while (*cnp->cn_nameptr == '/') {
833 if (ndp->ni_dvp != dp)
837 VFS_UNLOCK_GIANT(dvfslocked);
838 dvfslocked = vfslocked; /* dp becomes dvp in dirloop */
843 * If we're processing a path with a trailing slash,
844 * check that the end result is a directory.
846 if ((cnp->cn_flags & TRAILINGSLASH) && dp->v_type != VDIR) {
851 * Disallow directory write attempts on read-only filesystems.
854 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
858 if (cnp->cn_flags & SAVESTART) {
859 ndp->ni_startdir = ndp->ni_dvp;
860 VREF(ndp->ni_startdir);
863 if (ndp->ni_dvp != dp)
867 VFS_UNLOCK_GIANT(dvfslocked);
869 } else if ((cnp->cn_flags & LOCKPARENT) == 0 && ndp->ni_dvp != dp)
870 VOP_UNLOCK(ndp->ni_dvp, 0);
872 if (cnp->cn_flags & AUDITVNODE1)
873 AUDIT_ARG_VNODE1(dp);
874 else if (cnp->cn_flags & AUDITVNODE2)
875 AUDIT_ARG_VNODE2(dp);
877 if ((cnp->cn_flags & LOCKLEAF) == 0)
881 * Because of lookup_shared we may have the vnode shared locked, but
882 * the caller may want it to be exclusively locked.
884 if (needs_exclusive_leaf(dp->v_mount, cnp->cn_flags) &&
885 VOP_ISLOCKED(dp) != LK_EXCLUSIVE) {
886 vn_lock(dp, LK_UPGRADE | LK_RETRY);
887 if (dp->v_iflag & VI_DOOMED) {
892 if (vfslocked && dvfslocked)
893 VFS_UNLOCK_GIANT(dvfslocked); /* Only need one */
894 if (vfslocked || dvfslocked)
895 ndp->ni_cnd.cn_flags |= GIANTHELD;
899 if (dp != ndp->ni_dvp)
906 VFS_UNLOCK_GIANT(vfslocked);
907 VFS_UNLOCK_GIANT(dvfslocked);
908 ndp->ni_cnd.cn_flags &= ~GIANTHELD;
914 * relookup - lookup a path name component
915 * Used by lookup to re-acquire things.
918 relookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp)
920 struct vnode *dp = 0; /* the directory we are searching */
921 int wantparent; /* 1 => wantparent or lockparent flag */
922 int rdonly; /* lookup read-only flag bit */
925 KASSERT(cnp->cn_flags & ISLASTCN,
926 ("relookup: Not given last component."));
928 * Setup: break out flag bits into variables.
930 wantparent = cnp->cn_flags & (LOCKPARENT|WANTPARENT);
931 KASSERT(wantparent, ("relookup: parent not wanted."));
932 rdonly = cnp->cn_flags & RDONLY;
933 cnp->cn_flags &= ~ISSYMLINK;
935 cnp->cn_lkflags = LK_EXCLUSIVE;
936 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
939 * Search a new directory.
941 * The last component of the filename is left accessible via
942 * cnp->cn_nameptr for callers that need the name. Callers needing
943 * the name set the SAVENAME flag. When done, they assume
944 * responsibility for freeing the pathname buffer.
946 #ifdef NAMEI_DIAGNOSTIC
947 printf("{%s}: ", cnp->cn_nameptr);
951 * Check for degenerate name (e.g. / or "")
952 * which is a way of talking about a directory,
953 * e.g. like "/." or ".".
955 if (cnp->cn_nameptr[0] == '\0') {
956 if (cnp->cn_nameiop != LOOKUP || wantparent) {
960 if (dp->v_type != VDIR) {
964 if (!(cnp->cn_flags & LOCKLEAF))
967 /* XXX This should probably move to the top of function. */
968 if (cnp->cn_flags & SAVESTART)
969 panic("lookup: SAVESTART");
973 if (cnp->cn_flags & ISDOTDOT)
974 panic ("relookup: lookup on dot-dot");
977 * We now have a segment name to search for, and a directory to search.
979 #ifdef NAMEI_DIAGNOSTIC
980 vprint("search in:", dp);
982 if ((error = VOP_LOOKUP(dp, vpp, cnp)) != 0) {
983 KASSERT(*vpp == NULL, ("leaf should be empty"));
984 if (error != EJUSTRETURN)
987 * If creating and at end of pathname, then can consider
988 * allowing file to be created.
994 /* ASSERT(dvp == ndp->ni_startdir) */
995 if (cnp->cn_flags & SAVESTART)
997 if ((cnp->cn_flags & LOCKPARENT) == 0)
1000 * We return with ni_vp NULL to indicate that the entry
1001 * doesn't currently exist, leaving a pointer to the
1002 * (possibly locked) directory vnode in ndp->ni_dvp.
1010 * Disallow directory write attempts on read-only filesystems.
1013 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
1022 * Set the parent lock/ref state to the requested state.
1024 if ((cnp->cn_flags & LOCKPARENT) == 0 && dvp != dp) {
1029 } else if (!wantparent)
1032 * Check for symbolic link
1034 KASSERT(dp->v_type != VLNK || !(cnp->cn_flags & FOLLOW),
1035 ("relookup: symlink found.\n"));
1037 /* ASSERT(dvp == ndp->ni_startdir) */
1038 if (cnp->cn_flags & SAVESTART)
1041 if ((cnp->cn_flags & LOCKLEAF) == 0)
1051 * Free data allocated by namei(); see namei(9) for details.
1054 NDFREE(struct nameidata *ndp, const u_int flags)
1062 if (!(flags & NDF_NO_FREE_PNBUF) &&
1063 (ndp->ni_cnd.cn_flags & HASBUF)) {
1064 uma_zfree(namei_zone, ndp->ni_cnd.cn_pnbuf);
1065 ndp->ni_cnd.cn_flags &= ~HASBUF;
1067 if (!(flags & NDF_NO_VP_UNLOCK) &&
1068 (ndp->ni_cnd.cn_flags & LOCKLEAF) && ndp->ni_vp)
1070 if (!(flags & NDF_NO_VP_RELE) && ndp->ni_vp) {
1079 VOP_UNLOCK(ndp->ni_vp, 0);
1080 if (!(flags & NDF_NO_DVP_UNLOCK) &&
1081 (ndp->ni_cnd.cn_flags & LOCKPARENT) &&
1082 ndp->ni_dvp != ndp->ni_vp)
1084 if (!(flags & NDF_NO_DVP_RELE) &&
1085 (ndp->ni_cnd.cn_flags & (LOCKPARENT|WANTPARENT))) {
1094 VOP_UNLOCK(ndp->ni_dvp, 0);
1095 if (!(flags & NDF_NO_STARTDIR_RELE) &&
1096 (ndp->ni_cnd.cn_flags & SAVESTART)) {
1097 vrele(ndp->ni_startdir);
1098 ndp->ni_startdir = NULL;
1103 * Determine if there is a suitable alternate filename under the specified
1104 * prefix for the specified path. If the create flag is set, then the
1105 * alternate prefix will be used so long as the parent directory exists.
1106 * This is used by the various compatiblity ABIs so that Linux binaries prefer
1107 * files under /compat/linux for example. The chosen path (whether under
1108 * the prefix or under /) is returned in a kernel malloc'd buffer pointed
1109 * to by pathbuf. The caller is responsible for free'ing the buffer from
1110 * the M_TEMP bucket if one is returned.
1113 kern_alternate_path(struct thread *td, const char *prefix, const char *path,
1114 enum uio_seg pathseg, char **pathbuf, int create, int dirfd)
1116 struct nameidata nd, ndroot;
1117 char *ptr, *buf, *cp;
1121 buf = (char *) malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1124 /* Copy the prefix into the new pathname as a starting point. */
1125 len = strlcpy(buf, prefix, MAXPATHLEN);
1126 if (len >= MAXPATHLEN) {
1131 sz = MAXPATHLEN - len;
1134 /* Append the filename to the prefix. */
1135 if (pathseg == UIO_SYSSPACE)
1136 error = copystr(path, ptr, sz, &len);
1138 error = copyinstr(path, ptr, sz, &len);
1146 /* Only use a prefix with absolute pathnames. */
1152 if (dirfd != AT_FDCWD) {
1154 * We want the original because the "prefix" is
1155 * included in the already opened dirfd.
1157 bcopy(ptr, buf, len);
1162 * We know that there is a / somewhere in this pathname.
1163 * Search backwards for it, to find the file's parent dir
1164 * to see if it exists in the alternate tree. If it does,
1165 * and we want to create a file (cflag is set). We don't
1166 * need to worry about the root comparison in this case.
1170 for (cp = &ptr[len] - 1; *cp != '/'; cp--);
1173 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, UIO_SYSSPACE, buf, td);
1179 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, UIO_SYSSPACE, buf, td);
1186 * We now compare the vnode of the prefix to the one
1187 * vnode asked. If they resolve to be the same, then we
1188 * ignore the match so that the real root gets used.
1189 * This avoids the problem of traversing "../.." to find the
1190 * root directory and never finding it, because "/" resolves
1191 * to the emulation root directory. This is expensive :-(
1193 NDINIT(&ndroot, LOOKUP, FOLLOW | MPSAFE, UIO_SYSSPACE, prefix,
1196 /* We shouldn't ever get an error from this namei(). */
1197 error = namei(&ndroot);
1199 if (nd.ni_vp == ndroot.ni_vp)
1202 NDFREE(&ndroot, NDF_ONLY_PNBUF);
1203 vrele(ndroot.ni_vp);
1204 VFS_UNLOCK_GIANT(NDHASGIANT(&ndroot));
1208 NDFREE(&nd, NDF_ONLY_PNBUF);
1210 VFS_UNLOCK_GIANT(NDHASGIANT(&nd));
1213 /* If there was an error, use the original path name. */
1215 bcopy(ptr, buf, len);