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_ktrace.h"
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
48 #include <sys/mutex.h>
49 #include <sys/namei.h>
50 #include <sys/vnode.h>
51 #include <sys/mount.h>
52 #include <sys/filedesc.h>
54 #include <sys/syscallsubr.h>
55 #include <sys/sysctl.h>
57 #include <sys/ktrace.h>
60 #include <security/audit/audit.h>
61 #include <security/mac/mac_framework.h>
65 #define NAMEI_DIAGNOSTIC 1
66 #undef NAMEI_DIAGNOSTIC
69 * Allocation zone for namei
71 uma_zone_t namei_zone;
73 * Placeholder vnode for mp traversal
75 static struct vnode *vp_crossmp;
78 nameiinit(void *dummy __unused)
82 namei_zone = uma_zcreate("NAMEI", MAXPATHLEN, NULL, NULL, NULL, NULL,
84 error = getnewvnode("crossmp", NULL, &dead_vnodeops, &vp_crossmp);
86 panic("nameiinit: getnewvnode");
87 VN_LOCK_ASHARE(vp_crossmp);
89 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_SECOND, nameiinit, NULL)
92 static int lookup_shared = 1;
94 static int lookup_shared = 0;
96 SYSCTL_INT(_vfs, OID_AUTO, lookup_shared, CTLFLAG_RW, &lookup_shared, 0,
97 "Enables/Disables shared locks for path name translation");
100 * Convert a pathname into a pointer to a locked vnode.
102 * The FOLLOW flag is set when symbolic links are to be followed
103 * when they occur at the end of the name translation process.
104 * Symbolic links are always followed for all other pathname
105 * components other than the last.
107 * The segflg defines whether the name is to be copied from user
108 * space or kernel space.
110 * Overall outline of namei:
113 * get starting directory
114 * while (!done && !error) {
115 * call lookup to search path.
116 * if symbolic link, massage name in buffer and continue
120 namei(struct nameidata *ndp)
122 struct filedesc *fdp; /* pointer to file descriptor state */
123 char *cp; /* pointer into pathname argument */
124 struct vnode *dp; /* the directory we are searching */
125 struct iovec aiov; /* uio for reading symbolic links */
128 struct componentname *cnp = &ndp->ni_cnd;
129 struct thread *td = cnp->cn_thread;
130 struct proc *p = td->td_proc;
133 KASSERT((cnp->cn_flags & MPSAFE) != 0 || mtx_owned(&Giant) != 0,
134 ("NOT MPSAFE and Giant not held"));
135 ndp->ni_cnd.cn_cred = ndp->ni_cnd.cn_thread->td_ucred;
136 KASSERT(cnp->cn_cred && p, ("namei: bad cred/proc"));
137 KASSERT((cnp->cn_nameiop & (~OPMASK)) == 0,
138 ("namei: nameiop contaminated with flags"));
139 KASSERT((cnp->cn_flags & OPMASK) == 0,
140 ("namei: flags contaminated with nameiops"));
142 cnp->cn_flags &= ~LOCKSHARED;
146 * Get a buffer for the name to be translated, and copy the
147 * name into the buffer.
149 if ((cnp->cn_flags & HASBUF) == 0)
150 cnp->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK);
151 if (ndp->ni_segflg == UIO_SYSSPACE)
152 error = copystr(ndp->ni_dirp, cnp->cn_pnbuf,
153 MAXPATHLEN, (size_t *)&ndp->ni_pathlen);
155 error = copyinstr(ndp->ni_dirp, cnp->cn_pnbuf,
156 MAXPATHLEN, (size_t *)&ndp->ni_pathlen);
158 /* If we are auditing the kernel pathname, save the user pathname. */
159 if (cnp->cn_flags & AUDITVNODE1)
160 AUDIT_ARG(upath, td, cnp->cn_pnbuf, ARG_UPATH1);
161 if (cnp->cn_flags & AUDITVNODE2)
162 AUDIT_ARG(upath, td, cnp->cn_pnbuf, ARG_UPATH2);
165 * Don't allow empty pathnames.
167 if (!error && *cnp->cn_pnbuf == '\0')
171 uma_zfree(namei_zone, cnp->cn_pnbuf);
173 cnp->cn_pnbuf = NULL;
174 cnp->cn_nameptr = NULL;
181 if (KTRPOINT(td, KTR_NAMEI)) {
182 KASSERT(cnp->cn_thread == curthread,
183 ("namei not using curthread"));
184 ktrnamei(cnp->cn_pnbuf);
189 * Get starting point for the translation.
192 ndp->ni_rootdir = fdp->fd_rdir;
193 ndp->ni_topdir = fdp->fd_jdir;
196 vfslocked = VFS_LOCK_GIANT(dp->v_mount);
198 FILEDESC_SUNLOCK(fdp);
201 * Check if root directory should replace current directory.
202 * Done at start of translation and after symbolic link.
204 cnp->cn_nameptr = cnp->cn_pnbuf;
205 if (*(cnp->cn_nameptr) == '/') {
207 VFS_UNLOCK_GIANT(vfslocked);
208 while (*(cnp->cn_nameptr) == '/') {
212 dp = ndp->ni_rootdir;
213 vfslocked = VFS_LOCK_GIANT(dp->v_mount);
217 ndp->ni_cnd.cn_flags |= GIANTHELD;
218 ndp->ni_startdir = dp;
221 uma_zfree(namei_zone, cnp->cn_pnbuf);
223 cnp->cn_pnbuf = NULL;
224 cnp->cn_nameptr = NULL;
228 vfslocked = (ndp->ni_cnd.cn_flags & GIANTHELD) != 0;
229 ndp->ni_cnd.cn_flags &= ~GIANTHELD;
231 * Check for symbolic link
233 if ((cnp->cn_flags & ISSYMLINK) == 0) {
234 if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0) {
235 uma_zfree(namei_zone, cnp->cn_pnbuf);
237 cnp->cn_pnbuf = NULL;
238 cnp->cn_nameptr = NULL;
241 cnp->cn_flags |= HASBUF;
243 if ((cnp->cn_flags & MPSAFE) == 0) {
244 VFS_UNLOCK_GIANT(vfslocked);
245 } else if (vfslocked)
246 ndp->ni_cnd.cn_flags |= GIANTHELD;
249 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
254 if ((cnp->cn_flags & NOMACCHECK) == 0) {
255 error = mac_vnode_check_readlink(td->td_ucred,
261 if (ndp->ni_pathlen > 1)
262 cp = uma_zalloc(namei_zone, M_WAITOK);
266 aiov.iov_len = MAXPATHLEN;
267 auio.uio_iov = &aiov;
270 auio.uio_rw = UIO_READ;
271 auio.uio_segflg = UIO_SYSSPACE;
272 auio.uio_td = (struct thread *)0;
273 auio.uio_resid = MAXPATHLEN;
274 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
276 if (ndp->ni_pathlen > 1)
277 uma_zfree(namei_zone, cp);
280 linklen = MAXPATHLEN - auio.uio_resid;
282 if (ndp->ni_pathlen > 1)
283 uma_zfree(namei_zone, cp);
287 if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
288 if (ndp->ni_pathlen > 1)
289 uma_zfree(namei_zone, cp);
290 error = ENAMETOOLONG;
293 if (ndp->ni_pathlen > 1) {
294 bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
295 uma_zfree(namei_zone, cnp->cn_pnbuf);
298 cnp->cn_pnbuf[linklen] = '\0';
299 ndp->ni_pathlen += linklen;
303 uma_zfree(namei_zone, cnp->cn_pnbuf);
305 cnp->cn_pnbuf = NULL;
306 cnp->cn_nameptr = NULL;
311 VFS_UNLOCK_GIANT(vfslocked);
316 compute_cn_lkflags(struct mount *mp, int lkflags)
319 ((lkflags & LK_SHARED) && !(mp->mnt_kern_flag & MNTK_LOOKUP_SHARED))) {
320 lkflags &= ~LK_SHARED;
321 lkflags |= LK_EXCLUSIVE;
328 * This is a very central and rather complicated routine.
330 * The pathname is pointed to by ni_ptr and is of length ni_pathlen.
331 * The starting directory is taken from ni_startdir. The pathname is
332 * descended until done, or a symbolic link is encountered. The variable
333 * ni_more is clear if the path is completed; it is set to one if a
334 * symbolic link needing interpretation is encountered.
336 * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
337 * whether the name is to be looked up, created, renamed, or deleted.
338 * When CREATE, RENAME, or DELETE is specified, information usable in
339 * creating, renaming, or deleting a directory entry may be calculated.
340 * If flag has LOCKPARENT or'ed into it, the parent directory is returned
341 * locked. If flag has WANTPARENT or'ed into it, the parent directory is
342 * returned unlocked. Otherwise the parent directory is not returned. If
343 * the target of the pathname exists and LOCKLEAF is or'ed into the flag
344 * the target is returned locked, otherwise it is returned unlocked.
345 * When creating or renaming and LOCKPARENT is specified, the target may not
346 * be ".". When deleting and LOCKPARENT is specified, the target may be ".".
348 * Overall outline of lookup:
351 * identify next component of name at ndp->ni_ptr
352 * handle degenerate case where name is null string
353 * if .. and crossing mount points and on mounted filesys, find parent
354 * call VOP_LOOKUP routine for next component name
355 * directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set
356 * component vnode returned in ni_vp (if it exists), locked.
357 * if result vnode is mounted on and crossing mount points,
358 * find mounted on vnode
359 * if more components of name, do next level at dirloop
360 * return the answer in ni_vp, locked if LOCKLEAF set
361 * if LOCKPARENT set, return locked parent in ni_dvp
362 * if WANTPARENT set, return unlocked parent in ni_dvp
365 lookup(struct nameidata *ndp)
367 char *cp; /* pointer into pathname argument */
368 struct vnode *dp = 0; /* the directory we are searching */
369 struct vnode *tdp; /* saved dp */
370 struct mount *mp; /* mount table entry */
371 int docache; /* == 0 do not cache last component */
372 int wantparent; /* 1 => wantparent or lockparent flag */
373 int rdonly; /* lookup read-only flag bit */
376 int dpunlocked = 0; /* dp has already been unlocked */
377 struct componentname *cnp = &ndp->ni_cnd;
378 struct thread *td = cnp->cn_thread;
379 int vfslocked; /* VFS Giant state for child */
380 int dvfslocked; /* VFS Giant state for parent */
385 * Setup: break out flag bits into variables.
387 dvfslocked = (ndp->ni_cnd.cn_flags & GIANTHELD) != 0;
389 ndp->ni_cnd.cn_flags &= ~GIANTHELD;
390 wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT);
391 KASSERT(cnp->cn_nameiop == LOOKUP || wantparent,
392 ("CREATE, DELETE, RENAME require LOCKPARENT or WANTPARENT."));
393 docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE;
394 if (cnp->cn_nameiop == DELETE ||
395 (wantparent && cnp->cn_nameiop != CREATE &&
396 cnp->cn_nameiop != LOOKUP))
398 rdonly = cnp->cn_flags & RDONLY;
399 cnp->cn_flags &= ~ISSYMLINK;
402 * We use shared locks until we hit the parent of the last cn then
403 * we adjust based on the requesting flags.
406 cnp->cn_lkflags = LK_SHARED;
408 cnp->cn_lkflags = LK_EXCLUSIVE;
409 dp = ndp->ni_startdir;
410 ndp->ni_startdir = NULLVP;
412 compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags | LK_RETRY));
416 * Search a new directory.
418 * The last component of the filename is left accessible via
419 * cnp->cn_nameptr for callers that need the name. Callers needing
420 * the name set the SAVENAME flag. When done, they assume
421 * responsibility for freeing the pathname buffer.
424 for (cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++)
426 cnp->cn_namelen = cp - cnp->cn_nameptr;
427 if (cnp->cn_namelen > NAME_MAX) {
428 error = ENAMETOOLONG;
431 #ifdef NAMEI_DIAGNOSTIC
434 printf("{%s}: ", cnp->cn_nameptr);
437 ndp->ni_pathlen -= cnp->cn_namelen;
441 * Replace multiple slashes by a single slash and trailing slashes
442 * by a null. This must be done before VOP_LOOKUP() because some
443 * fs's don't know about trailing slashes. Remember if there were
444 * trailing slashes to handle symlinks, existing non-directories
445 * and non-existing files that won't be directories specially later.
448 while (*cp == '/' && (cp[1] == '/' || cp[1] == '\0')) {
453 *ndp->ni_next = '\0'; /* XXX for direnter() ... */
458 cnp->cn_flags |= MAKEENTRY;
459 if (*cp == '\0' && docache == 0)
460 cnp->cn_flags &= ~MAKEENTRY;
461 if (cnp->cn_namelen == 2 &&
462 cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.')
463 cnp->cn_flags |= ISDOTDOT;
465 cnp->cn_flags &= ~ISDOTDOT;
466 if (*ndp->ni_next == 0)
467 cnp->cn_flags |= ISLASTCN;
469 cnp->cn_flags &= ~ISLASTCN;
473 * Check for degenerate name (e.g. / or "")
474 * which is a way of talking about a directory,
475 * e.g. like "/." or ".".
477 if (cnp->cn_nameptr[0] == '\0') {
478 if (dp->v_type != VDIR) {
482 if (cnp->cn_nameiop != LOOKUP) {
492 if (cnp->cn_flags & AUDITVNODE1)
493 AUDIT_ARG(vnode, dp, ARG_VNODE1);
494 else if (cnp->cn_flags & AUDITVNODE2)
495 AUDIT_ARG(vnode, dp, ARG_VNODE2);
497 if (!(cnp->cn_flags & (LOCKPARENT | LOCKLEAF)))
499 /* XXX This should probably move to the top of function. */
500 if (cnp->cn_flags & SAVESTART)
501 panic("lookup: SAVESTART");
506 * Handle "..": four special cases.
507 * 1. Return an error if this is the last component of
508 * the name and the operation is DELETE or RENAME.
509 * 2. If at root directory (e.g. after chroot)
510 * or at absolute root directory
511 * then ignore it so can't get out.
512 * 3. If this vnode is the root of a mounted
513 * filesystem, then replace it with the
514 * vnode which was mounted on so we take the
515 * .. in the other filesystem.
516 * 4. If the vnode is the top directory of
517 * the jail or chroot, don't let them out.
519 if (cnp->cn_flags & ISDOTDOT) {
520 if ((cnp->cn_flags & ISLASTCN) != 0 &&
521 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
526 if (dp == ndp->ni_rootdir ||
527 dp == ndp->ni_topdir ||
529 ((dp->v_vflag & VV_ROOT) != 0 &&
530 (cnp->cn_flags & NOCROSSMOUNT) != 0)) {
533 vfslocked = VFS_LOCK_GIANT(dp->v_mount);
537 if ((dp->v_vflag & VV_ROOT) == 0)
539 if (dp->v_iflag & VI_DOOMED) { /* forced unmount */
544 dp = dp->v_mount->mnt_vnodecovered;
545 tvfslocked = dvfslocked;
546 dvfslocked = VFS_LOCK_GIANT(dp->v_mount);
549 VFS_UNLOCK_GIANT(tvfslocked);
551 compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags |
557 * We now have a segment name to search for, and a directory to search.
561 if ((cnp->cn_flags & NOMACCHECK) == 0) {
562 error = mac_vnode_check_lookup(td->td_ucred, dp, cnp);
569 ASSERT_VOP_LOCKED(dp, "lookup");
570 VNASSERT(vfslocked == 0, dp, ("lookup: vfslocked %d", vfslocked));
572 * If we have a shared lock we may need to upgrade the lock for the
575 if (dp != vp_crossmp &&
576 VOP_ISLOCKED(dp) == LK_SHARED &&
577 (cnp->cn_flags & ISLASTCN) && (cnp->cn_flags & LOCKPARENT))
578 vn_lock(dp, LK_UPGRADE|LK_RETRY);
580 * If we're looking up the last component and we need an exclusive
581 * lock, adjust our lkflags.
583 if ((cnp->cn_flags & (ISLASTCN|LOCKSHARED|LOCKLEAF)) ==
585 cnp->cn_lkflags = LK_EXCLUSIVE;
586 #ifdef NAMEI_DIAGNOSTIC
587 vprint("lookup in", dp);
589 lkflags_save = cnp->cn_lkflags;
590 cnp->cn_lkflags = compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags);
591 if ((error = VOP_LOOKUP(dp, &ndp->ni_vp, cnp)) != 0) {
592 cnp->cn_lkflags = lkflags_save;
593 KASSERT(ndp->ni_vp == NULL, ("leaf should be empty"));
594 #ifdef NAMEI_DIAGNOSTIC
595 printf("not found\n");
597 if ((error == ENOENT) &&
598 (dp->v_vflag & VV_ROOT) && (dp->v_mount != NULL) &&
599 (dp->v_mount->mnt_flag & MNT_UNION)) {
601 dp = dp->v_mount->mnt_vnodecovered;
602 tvfslocked = dvfslocked;
603 dvfslocked = VFS_LOCK_GIANT(dp->v_mount);
606 VFS_UNLOCK_GIANT(tvfslocked);
608 compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags |
613 if (error != EJUSTRETURN)
616 * If creating and at end of pathname, then can consider
617 * allowing file to be created.
623 if (*cp == '\0' && trailing_slash &&
624 !(cnp->cn_flags & WILLBEDIR)) {
628 if ((cnp->cn_flags & LOCKPARENT) == 0)
631 * This is a temporary assert to make sure I know what the
634 KASSERT((cnp->cn_flags & (WANTPARENT|LOCKPARENT)) != 0,
635 ("lookup: Unhandled case."));
637 * We return with ni_vp NULL to indicate that the entry
638 * doesn't currently exist, leaving a pointer to the
639 * (possibly locked) directory vnode in ndp->ni_dvp.
641 if (cnp->cn_flags & SAVESTART) {
642 ndp->ni_startdir = ndp->ni_dvp;
643 VREF(ndp->ni_startdir);
647 cnp->cn_lkflags = lkflags_save;
648 #ifdef NAMEI_DIAGNOSTIC
652 * Take into account any additional components consumed by
653 * the underlying filesystem.
655 if (cnp->cn_consume > 0) {
656 cnp->cn_nameptr += cnp->cn_consume;
657 ndp->ni_next += cnp->cn_consume;
658 ndp->ni_pathlen -= cnp->cn_consume;
663 vfslocked = VFS_LOCK_GIANT(dp->v_mount);
666 * Check to see if the vnode has been mounted on;
667 * if so find the root of the mounted filesystem.
669 while (dp->v_type == VDIR && (mp = dp->v_mountedhere) &&
670 (cnp->cn_flags & NOCROSSMOUNT) == 0) {
671 if (vfs_busy(mp, 0, 0, td))
674 VFS_UNLOCK_GIANT(vfslocked);
675 vfslocked = VFS_LOCK_GIANT(mp);
676 if (dp != ndp->ni_dvp)
680 VFS_UNLOCK_GIANT(dvfslocked);
683 ndp->ni_dvp = vp_crossmp;
684 error = VFS_ROOT(mp, compute_cn_lkflags(mp, cnp->cn_lkflags), &tdp, td);
686 if (vn_lock(vp_crossmp, LK_SHARED | LK_NOWAIT))
687 panic("vp_crossmp exclusively locked or reclaimed");
692 ndp->ni_vp = dp = tdp;
696 * Check for symbolic link
698 if ((dp->v_type == VLNK) &&
699 ((cnp->cn_flags & FOLLOW) || trailing_slash ||
700 *ndp->ni_next == '/')) {
701 cnp->cn_flags |= ISSYMLINK;
702 if (dp->v_iflag & VI_DOOMED) {
703 /* We can't know whether the directory was mounted with
704 * NOSYMFOLLOW, so we can't follow safely. */
708 if (dp->v_mount->mnt_flag & MNT_NOSYMFOLLOW) {
713 * Symlink code always expects an unlocked dvp.
715 if (ndp->ni_dvp != ndp->ni_vp)
716 VOP_UNLOCK(ndp->ni_dvp, 0);
721 * Check for bogus trailing slashes.
723 if (trailing_slash && dp->v_type != VDIR) {
730 * Not a symbolic link. If more pathname,
731 * continue at next component, else return.
733 KASSERT((cnp->cn_flags & ISLASTCN) || *ndp->ni_next == '/',
734 ("lookup: invalid path state."));
735 if (*ndp->ni_next == '/') {
736 cnp->cn_nameptr = ndp->ni_next;
737 while (*cnp->cn_nameptr == '/') {
741 if (ndp->ni_dvp != dp)
745 VFS_UNLOCK_GIANT(dvfslocked);
746 dvfslocked = vfslocked; /* dp becomes dvp in dirloop */
751 * Disallow directory write attempts on read-only filesystems.
754 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
758 if (cnp->cn_flags & SAVESTART) {
759 ndp->ni_startdir = ndp->ni_dvp;
760 VREF(ndp->ni_startdir);
763 if (ndp->ni_dvp != dp)
767 VFS_UNLOCK_GIANT(dvfslocked);
769 } else if ((cnp->cn_flags & LOCKPARENT) == 0 && ndp->ni_dvp != dp)
770 VOP_UNLOCK(ndp->ni_dvp, 0);
772 if (cnp->cn_flags & AUDITVNODE1)
773 AUDIT_ARG(vnode, dp, ARG_VNODE1);
774 else if (cnp->cn_flags & AUDITVNODE2)
775 AUDIT_ARG(vnode, dp, ARG_VNODE2);
777 if ((cnp->cn_flags & LOCKLEAF) == 0)
781 * Because of lookup_shared we may have the vnode shared locked, but
782 * the caller may want it to be exclusively locked.
784 if ((cnp->cn_flags & (ISLASTCN | LOCKSHARED | LOCKLEAF)) ==
785 (ISLASTCN | LOCKLEAF) && VOP_ISLOCKED(dp) != LK_EXCLUSIVE) {
786 vn_lock(dp, LK_UPGRADE | LK_RETRY);
788 if (vfslocked && dvfslocked)
789 VFS_UNLOCK_GIANT(dvfslocked); /* Only need one */
790 if (vfslocked || dvfslocked)
791 ndp->ni_cnd.cn_flags |= GIANTHELD;
795 if (dp != ndp->ni_dvp)
802 VFS_UNLOCK_GIANT(vfslocked);
803 VFS_UNLOCK_GIANT(dvfslocked);
804 ndp->ni_cnd.cn_flags &= ~GIANTHELD;
810 * relookup - lookup a path name component
811 * Used by lookup to re-acquire things.
814 relookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp)
816 struct vnode *dp = 0; /* the directory we are searching */
817 int wantparent; /* 1 => wantparent or lockparent flag */
818 int rdonly; /* lookup read-only flag bit */
821 KASSERT(cnp->cn_flags & ISLASTCN,
822 ("relookup: Not given last component."));
824 * Setup: break out flag bits into variables.
826 wantparent = cnp->cn_flags & (LOCKPARENT|WANTPARENT);
827 KASSERT(wantparent, ("relookup: parent not wanted."));
828 rdonly = cnp->cn_flags & RDONLY;
829 cnp->cn_flags &= ~ISSYMLINK;
831 cnp->cn_lkflags = LK_EXCLUSIVE;
832 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
835 * Search a new directory.
837 * The last component of the filename is left accessible via
838 * cnp->cn_nameptr for callers that need the name. Callers needing
839 * the name set the SAVENAME flag. When done, they assume
840 * responsibility for freeing the pathname buffer.
842 #ifdef NAMEI_DIAGNOSTIC
843 printf("{%s}: ", cnp->cn_nameptr);
847 * Check for degenerate name (e.g. / or "")
848 * which is a way of talking about a directory,
849 * e.g. like "/." or ".".
851 if (cnp->cn_nameptr[0] == '\0') {
852 if (cnp->cn_nameiop != LOOKUP || wantparent) {
856 if (dp->v_type != VDIR) {
860 if (!(cnp->cn_flags & LOCKLEAF))
863 /* XXX This should probably move to the top of function. */
864 if (cnp->cn_flags & SAVESTART)
865 panic("lookup: SAVESTART");
869 if (cnp->cn_flags & ISDOTDOT)
870 panic ("relookup: lookup on dot-dot");
873 * We now have a segment name to search for, and a directory to search.
875 #ifdef NAMEI_DIAGNOSTIC
876 vprint("search in:", dp);
878 if ((error = VOP_LOOKUP(dp, vpp, cnp)) != 0) {
879 KASSERT(*vpp == NULL, ("leaf should be empty"));
880 if (error != EJUSTRETURN)
883 * If creating and at end of pathname, then can consider
884 * allowing file to be created.
890 /* ASSERT(dvp == ndp->ni_startdir) */
891 if (cnp->cn_flags & SAVESTART)
893 if ((cnp->cn_flags & LOCKPARENT) == 0)
896 * This is a temporary assert to make sure I know what the
899 KASSERT((cnp->cn_flags & (WANTPARENT|LOCKPARENT)) != 0,
900 ("relookup: Unhandled case."));
902 * We return with ni_vp NULL to indicate that the entry
903 * doesn't currently exist, leaving a pointer to the
904 * (possibly locked) directory vnode in ndp->ni_dvp.
912 * Disallow directory write attempts on read-only filesystems.
915 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
924 * Set the parent lock/ref state to the requested state.
926 if ((cnp->cn_flags & LOCKPARENT) == 0 && dvp != dp) {
931 } else if (!wantparent)
934 * Check for symbolic link
936 KASSERT(dp->v_type != VLNK || !(cnp->cn_flags & FOLLOW),
937 ("relookup: symlink found.\n"));
939 /* ASSERT(dvp == ndp->ni_startdir) */
940 if (cnp->cn_flags & SAVESTART)
943 if ((cnp->cn_flags & LOCKLEAF) == 0)
953 * Free data allocated by namei(); see namei(9) for details.
956 NDFREE(struct nameidata *ndp, const u_int flags)
964 if (!(flags & NDF_NO_FREE_PNBUF) &&
965 (ndp->ni_cnd.cn_flags & HASBUF)) {
966 uma_zfree(namei_zone, ndp->ni_cnd.cn_pnbuf);
967 ndp->ni_cnd.cn_flags &= ~HASBUF;
969 if (!(flags & NDF_NO_VP_UNLOCK) &&
970 (ndp->ni_cnd.cn_flags & LOCKLEAF) && ndp->ni_vp)
972 if (!(flags & NDF_NO_VP_RELE) && ndp->ni_vp) {
981 VOP_UNLOCK(ndp->ni_vp, 0);
982 if (!(flags & NDF_NO_DVP_UNLOCK) &&
983 (ndp->ni_cnd.cn_flags & LOCKPARENT) &&
984 ndp->ni_dvp != ndp->ni_vp)
986 if (!(flags & NDF_NO_DVP_RELE) &&
987 (ndp->ni_cnd.cn_flags & (LOCKPARENT|WANTPARENT))) {
996 VOP_UNLOCK(ndp->ni_dvp, 0);
997 if (!(flags & NDF_NO_STARTDIR_RELE) &&
998 (ndp->ni_cnd.cn_flags & SAVESTART)) {
999 vrele(ndp->ni_startdir);
1000 ndp->ni_startdir = NULL;
1005 * Determine if there is a suitable alternate filename under the specified
1006 * prefix for the specified path. If the create flag is set, then the
1007 * alternate prefix will be used so long as the parent directory exists.
1008 * This is used by the various compatiblity ABIs so that Linux binaries prefer
1009 * files under /compat/linux for example. The chosen path (whether under
1010 * the prefix or under /) is returned in a kernel malloc'd buffer pointed
1011 * to by pathbuf. The caller is responsible for free'ing the buffer from
1012 * the M_TEMP bucket if one is returned.
1015 kern_alternate_path(struct thread *td, const char *prefix, char *path,
1016 enum uio_seg pathseg, char **pathbuf, int create)
1018 struct nameidata nd, ndroot;
1019 char *ptr, *buf, *cp;
1023 buf = (char *) malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1026 /* Copy the prefix into the new pathname as a starting point. */
1027 len = strlcpy(buf, prefix, MAXPATHLEN);
1028 if (len >= MAXPATHLEN) {
1033 sz = MAXPATHLEN - len;
1036 /* Append the filename to the prefix. */
1037 if (pathseg == UIO_SYSSPACE)
1038 error = copystr(path, ptr, sz, &len);
1040 error = copyinstr(path, ptr, sz, &len);
1048 /* Only use a prefix with absolute pathnames. */
1055 * We know that there is a / somewhere in this pathname.
1056 * Search backwards for it, to find the file's parent dir
1057 * to see if it exists in the alternate tree. If it does,
1058 * and we want to create a file (cflag is set). We don't
1059 * need to worry about the root comparison in this case.
1063 for (cp = &ptr[len] - 1; *cp != '/'; cp--);
1066 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, UIO_SYSSPACE, buf, td);
1072 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, UIO_SYSSPACE, buf, td);
1079 * We now compare the vnode of the prefix to the one
1080 * vnode asked. If they resolve to be the same, then we
1081 * ignore the match so that the real root gets used.
1082 * This avoids the problem of traversing "../.." to find the
1083 * root directory and never finding it, because "/" resolves
1084 * to the emulation root directory. This is expensive :-(
1086 NDINIT(&ndroot, LOOKUP, FOLLOW | MPSAFE, UIO_SYSSPACE, prefix,
1089 /* We shouldn't ever get an error from this namei(). */
1090 error = namei(&ndroot);
1092 if (nd.ni_vp == ndroot.ni_vp)
1095 NDFREE(&ndroot, NDF_ONLY_PNBUF);
1096 vrele(ndroot.ni_vp);
1097 VFS_UNLOCK_GIANT(NDHASGIANT(&ndroot));
1101 NDFREE(&nd, NDF_ONLY_PNBUF);
1103 VFS_UNLOCK_GIANT(NDHASGIANT(&nd));
1106 /* If there was an error, use the original path name. */
1108 bcopy(ptr, buf, len);