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>
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>
55 #include <sys/syscallsubr.h>
56 #include <sys/sysctl.h>
58 #include <sys/ktrace.h>
61 #include <security/audit/audit.h>
65 #define NAMEI_DIAGNOSTIC 1
66 #undef NAMEI_DIAGNOSTIC
69 * Allocation zone for namei
71 uma_zone_t namei_zone;
74 nameiinit(void *dummy __unused)
76 namei_zone = uma_zcreate("NAMEI", MAXPATHLEN, NULL, NULL, NULL, NULL,
80 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_SECOND, nameiinit, NULL)
83 static int lookup_shared = 1;
85 static int lookup_shared = 0;
87 SYSCTL_INT(_vfs, OID_AUTO, lookup_shared, CTLFLAG_RW, &lookup_shared, 0,
88 "Enables/Disables shared locks for path name translation");
91 * Convert a pathname into a pointer to a locked inode.
93 * The FOLLOW flag is set when symbolic links are to be followed
94 * when they occur at the end of the name translation process.
95 * Symbolic links are always followed for all other pathname
96 * components other than the last.
98 * The segflg defines whether the name is to be copied from user
99 * space or kernel space.
101 * Overall outline of namei:
104 * get starting directory
105 * while (!done && !error) {
106 * call lookup to search path.
107 * if symbolic link, massage name in buffer and continue
112 register struct nameidata *ndp;
114 register struct filedesc *fdp; /* pointer to file descriptor state */
115 register char *cp; /* pointer into pathname argument */
116 register struct vnode *dp; /* the directory we are searching */
117 struct iovec aiov; /* uio for reading symbolic links */
120 struct componentname *cnp = &ndp->ni_cnd;
121 struct thread *td = cnp->cn_thread;
122 struct proc *p = td->td_proc;
125 KASSERT((cnp->cn_flags & MPSAFE) != 0 || mtx_owned(&Giant) != 0,
126 ("NOT MPSAFE and Giant not held"));
127 ndp->ni_cnd.cn_cred = ndp->ni_cnd.cn_thread->td_ucred;
128 KASSERT(cnp->cn_cred && p, ("namei: bad cred/proc"));
129 KASSERT((cnp->cn_nameiop & (~OPMASK)) == 0,
130 ("namei: nameiop contaminated with flags"));
131 KASSERT((cnp->cn_flags & OPMASK) == 0,
132 ("namei: flags contaminated with nameiops"));
134 cnp->cn_flags &= ~LOCKSHARED;
138 * Get a buffer for the name to be translated, and copy the
139 * name into the buffer.
141 if ((cnp->cn_flags & HASBUF) == 0)
142 cnp->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK);
143 if (ndp->ni_segflg == UIO_SYSSPACE)
144 error = copystr(ndp->ni_dirp, cnp->cn_pnbuf,
145 MAXPATHLEN, (size_t *)&ndp->ni_pathlen);
147 error = copyinstr(ndp->ni_dirp, cnp->cn_pnbuf,
148 MAXPATHLEN, (size_t *)&ndp->ni_pathlen);
150 /* If we are auditing the kernel pathname, save the user pathname. */
151 if (cnp->cn_flags & AUDITVNODE1)
152 AUDIT_ARG(upath, td, cnp->cn_pnbuf, ARG_UPATH1);
153 if (cnp->cn_flags & AUDITVNODE2)
154 AUDIT_ARG(upath, td, cnp->cn_pnbuf, ARG_UPATH2);
157 * Don't allow empty pathnames.
159 if (!error && *cnp->cn_pnbuf == '\0')
163 uma_zfree(namei_zone, cnp->cn_pnbuf);
165 cnp->cn_pnbuf = NULL;
166 cnp->cn_nameptr = NULL;
173 if (KTRPOINT(td, KTR_NAMEI)) {
174 KASSERT(cnp->cn_thread == curthread,
175 ("namei not using curthread"));
176 ktrnamei(cnp->cn_pnbuf);
181 * Get starting point for the translation.
184 ndp->ni_rootdir = fdp->fd_rdir;
185 ndp->ni_topdir = fdp->fd_jdir;
188 vfslocked = VFS_LOCK_GIANT(dp->v_mount);
190 FILEDESC_UNLOCK(fdp);
193 * Check if root directory should replace current directory.
194 * Done at start of translation and after symbolic link.
196 cnp->cn_nameptr = cnp->cn_pnbuf;
197 if (*(cnp->cn_nameptr) == '/') {
199 VFS_UNLOCK_GIANT(vfslocked);
200 while (*(cnp->cn_nameptr) == '/') {
204 dp = ndp->ni_rootdir;
205 vfslocked = VFS_LOCK_GIANT(dp->v_mount);
209 ndp->ni_cnd.cn_flags |= GIANTHELD;
210 ndp->ni_startdir = dp;
213 uma_zfree(namei_zone, cnp->cn_pnbuf);
215 cnp->cn_pnbuf = NULL;
216 cnp->cn_nameptr = NULL;
220 vfslocked = (ndp->ni_cnd.cn_flags & GIANTHELD) != 0;
221 ndp->ni_cnd.cn_flags &= ~GIANTHELD;
223 * Check for symbolic link
225 if ((cnp->cn_flags & ISSYMLINK) == 0) {
226 if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0) {
227 uma_zfree(namei_zone, cnp->cn_pnbuf);
229 cnp->cn_pnbuf = NULL;
230 cnp->cn_nameptr = NULL;
233 cnp->cn_flags |= HASBUF;
235 if ((cnp->cn_flags & MPSAFE) == 0) {
236 VFS_UNLOCK_GIANT(vfslocked);
237 } else if (vfslocked)
238 ndp->ni_cnd.cn_flags |= GIANTHELD;
241 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
246 if ((cnp->cn_flags & NOMACCHECK) == 0) {
247 error = mac_check_vnode_readlink(td->td_ucred,
253 if (ndp->ni_pathlen > 1)
254 cp = uma_zalloc(namei_zone, M_WAITOK);
258 aiov.iov_len = MAXPATHLEN;
259 auio.uio_iov = &aiov;
262 auio.uio_rw = UIO_READ;
263 auio.uio_segflg = UIO_SYSSPACE;
264 auio.uio_td = (struct thread *)0;
265 auio.uio_resid = MAXPATHLEN;
266 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
268 if (ndp->ni_pathlen > 1)
269 uma_zfree(namei_zone, cp);
272 linklen = MAXPATHLEN - auio.uio_resid;
274 if (ndp->ni_pathlen > 1)
275 uma_zfree(namei_zone, cp);
279 if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
280 if (ndp->ni_pathlen > 1)
281 uma_zfree(namei_zone, cp);
282 error = ENAMETOOLONG;
285 if (ndp->ni_pathlen > 1) {
286 bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
287 uma_zfree(namei_zone, cnp->cn_pnbuf);
290 cnp->cn_pnbuf[linklen] = '\0';
291 ndp->ni_pathlen += linklen;
295 uma_zfree(namei_zone, cnp->cn_pnbuf);
297 cnp->cn_pnbuf = NULL;
298 cnp->cn_nameptr = NULL;
303 VFS_UNLOCK_GIANT(vfslocked);
309 * This is a very central and rather complicated routine.
311 * The pathname is pointed to by ni_ptr and is of length ni_pathlen.
312 * The starting directory is taken from ni_startdir. The pathname is
313 * descended until done, or a symbolic link is encountered. The variable
314 * ni_more is clear if the path is completed; it is set to one if a
315 * symbolic link needing interpretation is encountered.
317 * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
318 * whether the name is to be looked up, created, renamed, or deleted.
319 * When CREATE, RENAME, or DELETE is specified, information usable in
320 * creating, renaming, or deleting a directory entry may be calculated.
321 * If flag has LOCKPARENT or'ed into it, the parent directory is returned
322 * locked. If flag has WANTPARENT or'ed into it, the parent directory is
323 * returned unlocked. Otherwise the parent directory is not returned. If
324 * the target of the pathname exists and LOCKLEAF is or'ed into the flag
325 * the target is returned locked, otherwise it is returned unlocked.
326 * When creating or renaming and LOCKPARENT is specified, the target may not
327 * be ".". When deleting and LOCKPARENT is specified, the target may be ".".
329 * Overall outline of lookup:
332 * identify next component of name at ndp->ni_ptr
333 * handle degenerate case where name is null string
334 * if .. and crossing mount points and on mounted filesys, find parent
335 * call VOP_LOOKUP routine for next component name
336 * directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set
337 * component vnode returned in ni_vp (if it exists), locked.
338 * if result vnode is mounted on and crossing mount points,
339 * find mounted on vnode
340 * if more components of name, do next level at dirloop
341 * return the answer in ni_vp, locked if LOCKLEAF set
342 * if LOCKPARENT set, return locked parent in ni_dvp
343 * if WANTPARENT set, return unlocked parent in ni_dvp
347 register struct nameidata *ndp;
349 register char *cp; /* pointer into pathname argument */
350 register struct vnode *dp = 0; /* the directory we are searching */
351 struct vnode *tdp; /* saved dp */
352 struct mount *mp; /* mount table entry */
353 int docache; /* == 0 do not cache last component */
354 int wantparent; /* 1 => wantparent or lockparent flag */
355 int rdonly; /* lookup read-only flag bit */
358 int dpunlocked = 0; /* dp has already been unlocked */
359 struct componentname *cnp = &ndp->ni_cnd;
360 struct thread *td = cnp->cn_thread;
366 * Setup: break out flag bits into variables.
368 vfslocked = (ndp->ni_cnd.cn_flags & GIANTHELD) != 0;
370 ndp->ni_cnd.cn_flags &= ~GIANTHELD;
371 wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT);
372 KASSERT(cnp->cn_nameiop == LOOKUP || wantparent,
373 ("CREATE, DELETE, RENAME require LOCKPARENT or WANTPARENT."));
374 docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE;
375 if (cnp->cn_nameiop == DELETE ||
376 (wantparent && cnp->cn_nameiop != CREATE &&
377 cnp->cn_nameiop != LOOKUP))
379 rdonly = cnp->cn_flags & RDONLY;
380 cnp->cn_flags &= ~ISSYMLINK;
383 * We use shared locks until we hit the parent of the last cn then
384 * we adjust based on the requesting flags.
387 cnp->cn_lkflags = LK_SHARED;
389 cnp->cn_lkflags = LK_EXCLUSIVE;
390 dp = ndp->ni_startdir;
391 ndp->ni_startdir = NULLVP;
392 vn_lock(dp, cnp->cn_lkflags | LK_RETRY, td);
396 * Search a new directory.
398 * The last component of the filename is left accessible via
399 * cnp->cn_nameptr for callers that need the name. Callers needing
400 * the name set the SAVENAME flag. When done, they assume
401 * responsibility for freeing the pathname buffer.
404 for (cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++)
406 cnp->cn_namelen = cp - cnp->cn_nameptr;
407 if (cnp->cn_namelen > NAME_MAX) {
408 error = ENAMETOOLONG;
411 #ifdef NAMEI_DIAGNOSTIC
414 printf("{%s}: ", cnp->cn_nameptr);
417 ndp->ni_pathlen -= cnp->cn_namelen;
421 * Replace multiple slashes by a single slash and trailing slashes
422 * by a null. This must be done before VOP_LOOKUP() because some
423 * fs's don't know about trailing slashes. Remember if there were
424 * trailing slashes to handle symlinks, existing non-directories
425 * and non-existing files that won't be directories specially later.
428 while (*cp == '/' && (cp[1] == '/' || cp[1] == '\0')) {
433 *ndp->ni_next = '\0'; /* XXX for direnter() ... */
438 cnp->cn_flags |= MAKEENTRY;
439 if (*cp == '\0' && docache == 0)
440 cnp->cn_flags &= ~MAKEENTRY;
441 if (cnp->cn_namelen == 2 &&
442 cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.')
443 cnp->cn_flags |= ISDOTDOT;
445 cnp->cn_flags &= ~ISDOTDOT;
446 if (*ndp->ni_next == 0)
447 cnp->cn_flags |= ISLASTCN;
449 cnp->cn_flags &= ~ISLASTCN;
453 * Check for degenerate name (e.g. / or "")
454 * which is a way of talking about a directory,
455 * e.g. like "/." or ".".
457 if (cnp->cn_nameptr[0] == '\0') {
458 if (dp->v_type != VDIR) {
462 if (cnp->cn_nameiop != LOOKUP) {
472 if (cnp->cn_flags & AUDITVNODE1)
473 AUDIT_ARG(vnode, dp, ARG_VNODE1);
474 else if (cnp->cn_flags & AUDITVNODE2)
475 AUDIT_ARG(vnode, dp, ARG_VNODE2);
477 if (!(cnp->cn_flags & (LOCKPARENT | LOCKLEAF)))
478 VOP_UNLOCK(dp, 0, td);
479 /* XXX This should probably move to the top of function. */
480 if (cnp->cn_flags & SAVESTART)
481 panic("lookup: SAVESTART");
486 * Handle "..": four special cases.
487 * 1. Return an error if this is the last component of
488 * the name and the operation is DELETE or RENAME.
489 * 2. If at root directory (e.g. after chroot)
490 * or at absolute root directory
491 * then ignore it so can't get out.
492 * 3. If this vnode is the root of a mounted
493 * filesystem, then replace it with the
494 * vnode which was mounted on so we take the
495 * .. in the other filesystem.
496 * 4. If the vnode is the top directory of
497 * the jail or chroot, don't let them out.
499 if (cnp->cn_flags & ISDOTDOT) {
500 if ((cnp->cn_flags & ISLASTCN) != 0 &&
501 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
506 if (dp == ndp->ni_rootdir ||
507 dp == ndp->ni_topdir ||
514 if ((dp->v_vflag & VV_ROOT) == 0 ||
515 (cnp->cn_flags & NOCROSSMOUNT))
517 if (dp->v_iflag & VI_DOOMED) { /* forced unmount */
522 dp = dp->v_mount->mnt_vnodecovered;
523 tvfslocked = vfslocked;
524 vfslocked = VFS_LOCK_GIANT(dp->v_mount);
527 VFS_UNLOCK_GIANT(tvfslocked);
528 vn_lock(dp, cnp->cn_lkflags | LK_RETRY, td);
533 * We now have a segment name to search for, and a directory to search.
537 if ((cnp->cn_flags & NOMACCHECK) == 0) {
538 error = mac_check_vnode_lookup(td->td_ucred, dp, cnp);
545 ASSERT_VOP_LOCKED(dp, "lookup");
547 * If we have a shared lock we may need to upgrade the lock for the
550 if (VOP_ISLOCKED(dp, td) == LK_SHARED &&
551 (cnp->cn_flags & ISLASTCN) && (cnp->cn_flags & LOCKPARENT))
552 vn_lock(dp, LK_UPGRADE|LK_RETRY, td);
554 * If we're looking up the last component and we need an exclusive
555 * lock, adjust our lkflags.
557 if ((cnp->cn_flags & (ISLASTCN|LOCKSHARED|LOCKLEAF)) ==
559 cnp->cn_lkflags = LK_EXCLUSIVE;
560 #ifdef NAMEI_DIAGNOSTIC
561 vprint("lookup in", dp);
563 if ((error = VOP_LOOKUP(dp, &ndp->ni_vp, cnp)) != 0) {
564 KASSERT(ndp->ni_vp == NULL, ("leaf should be empty"));
565 #ifdef NAMEI_DIAGNOSTIC
566 printf("not found\n");
568 if ((error == ENOENT) &&
569 (dp->v_vflag & VV_ROOT) && (dp->v_mount != NULL) &&
570 (dp->v_mount->mnt_flag & MNT_UNION)) {
572 dp = dp->v_mount->mnt_vnodecovered;
573 tvfslocked = vfslocked;
574 vfslocked = VFS_LOCK_GIANT(dp->v_mount);
577 VFS_UNLOCK_GIANT(tvfslocked);
578 vn_lock(dp, cnp->cn_lkflags | LK_RETRY, td);
582 if (error != EJUSTRETURN)
585 * If creating and at end of pathname, then can consider
586 * allowing file to be created.
592 if (*cp == '\0' && trailing_slash &&
593 !(cnp->cn_flags & WILLBEDIR)) {
597 if ((cnp->cn_flags & LOCKPARENT) == 0)
598 VOP_UNLOCK(dp, 0, td);
600 * This is a temporary assert to make sure I know what the
603 KASSERT((cnp->cn_flags & (WANTPARENT|LOCKPARENT)) != 0,
604 ("lookup: Unhandled case."));
606 * We return with ni_vp NULL to indicate that the entry
607 * doesn't currently exist, leaving a pointer to the
608 * (possibly locked) directory inode in ndp->ni_dvp.
610 if (cnp->cn_flags & SAVESTART) {
611 ndp->ni_startdir = ndp->ni_dvp;
612 VREF(ndp->ni_startdir);
616 #ifdef NAMEI_DIAGNOSTIC
620 * Take into account any additional components consumed by
621 * the underlying filesystem.
623 if (cnp->cn_consume > 0) {
624 cnp->cn_nameptr += cnp->cn_consume;
625 ndp->ni_next += cnp->cn_consume;
626 ndp->ni_pathlen -= cnp->cn_consume;
633 * Check to see if the vnode has been mounted on;
634 * if so find the root of the mounted filesystem.
636 while (dp->v_type == VDIR && (mp = dp->v_mountedhere) &&
637 (cnp->cn_flags & NOCROSSMOUNT) == 0) {
638 KASSERT(dp != ndp->ni_dvp, ("XXX"));
639 if (vfs_busy(mp, 0, 0, td))
642 dvfslocked = vfslocked;
643 vfslocked = VFS_LOCK_GIANT(mp);
644 VOP_UNLOCK(ndp->ni_dvp, 0, td);
645 error = VFS_ROOT(mp, cnp->cn_lkflags, &tdp, td);
646 VOP_LOCK(ndp->ni_dvp, cnp->cn_lkflags | LK_RETRY, td);
652 ndp->ni_vp = dp = tdp;
656 * Check for symbolic link
658 if ((dp->v_type == VLNK) &&
659 ((cnp->cn_flags & FOLLOW) || trailing_slash ||
660 *ndp->ni_next == '/')) {
661 cnp->cn_flags |= ISSYMLINK;
662 if (dp->v_iflag & VI_DOOMED) {
663 /* We can't know whether the directory was mounted with
664 * NOSYMFOLLOW, so we can't follow safely. */
668 if (dp->v_mount->mnt_flag & MNT_NOSYMFOLLOW) {
673 * Symlink code always expects an unlocked dvp.
675 if (ndp->ni_dvp != ndp->ni_vp)
676 VOP_UNLOCK(ndp->ni_dvp, 0, td);
681 * Check for bogus trailing slashes.
683 if (trailing_slash && dp->v_type != VDIR) {
690 * Not a symbolic link. If more pathname,
691 * continue at next component, else return.
693 KASSERT((cnp->cn_flags & ISLASTCN) || *ndp->ni_next == '/',
694 ("lookup: invalid path state."));
695 if (*ndp->ni_next == '/') {
696 cnp->cn_nameptr = ndp->ni_next;
697 while (*cnp->cn_nameptr == '/') {
701 if (ndp->ni_dvp != dp)
705 VFS_UNLOCK_GIANT(dvfslocked);
710 * Disallow directory write attempts on read-only filesystems.
713 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
717 if (cnp->cn_flags & SAVESTART) {
718 ndp->ni_startdir = ndp->ni_dvp;
719 VREF(ndp->ni_startdir);
722 if (ndp->ni_dvp != dp)
726 VFS_UNLOCK_GIANT(dvfslocked);
728 } else if ((cnp->cn_flags & LOCKPARENT) == 0 && ndp->ni_dvp != dp)
729 VOP_UNLOCK(ndp->ni_dvp, 0, td);
731 if (cnp->cn_flags & AUDITVNODE1)
732 AUDIT_ARG(vnode, dp, ARG_VNODE1);
733 else if (cnp->cn_flags & AUDITVNODE2)
734 AUDIT_ARG(vnode, dp, ARG_VNODE2);
736 if ((cnp->cn_flags & LOCKLEAF) == 0)
737 VOP_UNLOCK(dp, 0, td);
739 if (vfslocked && dvfslocked)
740 VFS_UNLOCK_GIANT(dvfslocked); /* Only need one */
741 if (vfslocked || dvfslocked)
742 ndp->ni_cnd.cn_flags |= GIANTHELD;
746 if (dp != ndp->ni_dvp)
753 VFS_UNLOCK_GIANT(vfslocked);
754 VFS_UNLOCK_GIANT(dvfslocked);
755 ndp->ni_cnd.cn_flags &= ~GIANTHELD;
761 * relookup - lookup a path name component
762 * Used by lookup to re-aquire things.
765 relookup(dvp, vpp, cnp)
766 struct vnode *dvp, **vpp;
767 struct componentname *cnp;
769 struct thread *td = cnp->cn_thread;
770 struct vnode *dp = 0; /* the directory we are searching */
771 int wantparent; /* 1 => wantparent or lockparent flag */
772 int rdonly; /* lookup read-only flag bit */
775 KASSERT(cnp->cn_flags & ISLASTCN,
776 ("relookup: Not given last component."));
778 * Setup: break out flag bits into variables.
780 wantparent = cnp->cn_flags & (LOCKPARENT|WANTPARENT);
781 KASSERT(wantparent, ("relookup: parent not wanted."));
782 rdonly = cnp->cn_flags & RDONLY;
783 cnp->cn_flags &= ~ISSYMLINK;
785 cnp->cn_lkflags = LK_EXCLUSIVE;
786 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY, td);
789 * Search a new directory.
791 * The last component of the filename is left accessible via
792 * cnp->cn_nameptr for callers that need the name. Callers needing
793 * the name set the SAVENAME flag. When done, they assume
794 * responsibility for freeing the pathname buffer.
796 #ifdef NAMEI_DIAGNOSTIC
797 printf("{%s}: ", cnp->cn_nameptr);
801 * Check for degenerate name (e.g. / or "")
802 * which is a way of talking about a directory,
803 * e.g. like "/." or ".".
805 if (cnp->cn_nameptr[0] == '\0') {
806 if (cnp->cn_nameiop != LOOKUP || wantparent) {
810 if (dp->v_type != VDIR) {
814 if (!(cnp->cn_flags & LOCKLEAF))
815 VOP_UNLOCK(dp, 0, td);
817 /* XXX This should probably move to the top of function. */
818 if (cnp->cn_flags & SAVESTART)
819 panic("lookup: SAVESTART");
823 if (cnp->cn_flags & ISDOTDOT)
824 panic ("relookup: lookup on dot-dot");
827 * We now have a segment name to search for, and a directory to search.
829 #ifdef NAMEI_DIAGNOSTIC
830 vprint("search in:", dp);
832 if ((error = VOP_LOOKUP(dp, vpp, cnp)) != 0) {
833 KASSERT(*vpp == NULL, ("leaf should be empty"));
834 if (error != EJUSTRETURN)
837 * If creating and at end of pathname, then can consider
838 * allowing file to be created.
844 /* ASSERT(dvp == ndp->ni_startdir) */
845 if (cnp->cn_flags & SAVESTART)
847 if ((cnp->cn_flags & LOCKPARENT) == 0)
848 VOP_UNLOCK(dp, 0, td);
850 * This is a temporary assert to make sure I know what the
853 KASSERT((cnp->cn_flags & (WANTPARENT|LOCKPARENT)) != 0,
854 ("relookup: Unhandled case."));
856 * We return with ni_vp NULL to indicate that the entry
857 * doesn't currently exist, leaving a pointer to the
858 * (possibly locked) directory inode in ndp->ni_dvp.
865 * Disallow directory write attempts on read-only filesystems.
868 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
877 * Set the parent lock/ref state to the requested state.
879 if ((cnp->cn_flags & LOCKPARENT) == 0 && dvp != dp) {
881 VOP_UNLOCK(dvp, 0, td);
884 } else if (!wantparent)
887 * Check for symbolic link
889 KASSERT(dp->v_type != VLNK || !(cnp->cn_flags & FOLLOW),
890 ("relookup: symlink found.\n"));
892 /* ASSERT(dvp == ndp->ni_startdir) */
893 if (cnp->cn_flags & SAVESTART)
896 if ((cnp->cn_flags & LOCKLEAF) == 0)
897 VOP_UNLOCK(dp, 0, td);
906 * Free data allocated by namei(); see namei(9) for details.
910 struct nameidata *ndp;
919 if (!(flags & NDF_NO_FREE_PNBUF) &&
920 (ndp->ni_cnd.cn_flags & HASBUF)) {
921 uma_zfree(namei_zone, ndp->ni_cnd.cn_pnbuf);
922 ndp->ni_cnd.cn_flags &= ~HASBUF;
924 if (!(flags & NDF_NO_VP_UNLOCK) &&
925 (ndp->ni_cnd.cn_flags & LOCKLEAF) && ndp->ni_vp)
927 if (!(flags & NDF_NO_VP_RELE) && ndp->ni_vp) {
936 VOP_UNLOCK(ndp->ni_vp, 0, ndp->ni_cnd.cn_thread);
937 if (!(flags & NDF_NO_DVP_UNLOCK) &&
938 (ndp->ni_cnd.cn_flags & LOCKPARENT) &&
939 ndp->ni_dvp != ndp->ni_vp)
941 if (!(flags & NDF_NO_DVP_RELE) &&
942 (ndp->ni_cnd.cn_flags & (LOCKPARENT|WANTPARENT))) {
951 VOP_UNLOCK(ndp->ni_dvp, 0, ndp->ni_cnd.cn_thread);
952 if (!(flags & NDF_NO_STARTDIR_RELE) &&
953 (ndp->ni_cnd.cn_flags & SAVESTART)) {
954 vrele(ndp->ni_startdir);
955 ndp->ni_startdir = NULL;
960 * Determine if there is a suitable alternate filename under the specified
961 * prefix for the specified path. If the create flag is set, then the
962 * alternate prefix will be used so long as the parent directory exists.
963 * This is used by the various compatiblity ABIs so that Linux binaries prefer
964 * files under /compat/linux for example. The chosen path (whether under
965 * the prefix or under /) is returned in a kernel malloc'd buffer pointed
966 * to by pathbuf. The caller is responsible for free'ing the buffer from
967 * the M_TEMP bucket if one is returned.
970 kern_alternate_path(struct thread *td, const char *prefix, char *path,
971 enum uio_seg pathseg, char **pathbuf, int create)
973 struct nameidata nd, ndroot;
974 char *ptr, *buf, *cp;
978 buf = (char *) malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
981 /* Copy the prefix into the new pathname as a starting point. */
982 len = strlcpy(buf, prefix, MAXPATHLEN);
983 if (len >= MAXPATHLEN) {
988 sz = MAXPATHLEN - len;
991 /* Append the filename to the prefix. */
992 if (pathseg == UIO_SYSSPACE)
993 error = copystr(path, ptr, sz, &len);
995 error = copyinstr(path, ptr, sz, &len);
1003 /* Only use a prefix with absolute pathnames. */
1010 * We know that there is a / somewhere in this pathname.
1011 * Search backwards for it, to find the file's parent dir
1012 * to see if it exists in the alternate tree. If it does,
1013 * and we want to create a file (cflag is set). We don't
1014 * need to worry about the root comparison in this case.
1018 for (cp = &ptr[len] - 1; *cp != '/'; cp--);
1021 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, UIO_SYSSPACE, buf, td);
1027 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, UIO_SYSSPACE, buf, td);
1034 * We now compare the vnode of the prefix to the one
1035 * vnode asked. If they resolve to be the same, then we
1036 * ignore the match so that the real root gets used.
1037 * This avoids the problem of traversing "../.." to find the
1038 * root directory and never finding it, because "/" resolves
1039 * to the emulation root directory. This is expensive :-(
1041 NDINIT(&ndroot, LOOKUP, FOLLOW | MPSAFE, UIO_SYSSPACE, prefix,
1044 /* We shouldn't ever get an error from this namei(). */
1045 error = namei(&ndroot);
1047 if (nd.ni_vp == ndroot.ni_vp)
1050 NDFREE(&ndroot, NDF_ONLY_PNBUF);
1051 vrele(ndroot.ni_vp);
1052 VFS_UNLOCK_GIANT(NDHASGIANT(&ndroot));
1056 NDFREE(&nd, NDF_ONLY_PNBUF);
1058 VFS_UNLOCK_GIANT(NDHASGIANT(&nd));
1061 /* If there was an error, use the original path name. */
1063 bcopy(ptr, buf, len);