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>
63 #define NAMEI_DIAGNOSTIC 1
64 #undef NAMEI_DIAGNOSTIC
67 * Allocation zone for namei
69 uma_zone_t namei_zone;
72 nameiinit(void *dummy __unused)
74 namei_zone = uma_zcreate("NAMEI", MAXPATHLEN, NULL, NULL, NULL, NULL,
78 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_SECOND, nameiinit, NULL)
81 static int lookup_shared = 1;
83 static int lookup_shared = 0;
85 SYSCTL_INT(_vfs, OID_AUTO, lookup_shared, CTLFLAG_RW, &lookup_shared, 0,
86 "Enables/Disables shared locks for path name translation");
89 * Convert a pathname into a pointer to a locked inode.
91 * The FOLLOW flag is set when symbolic links are to be followed
92 * when they occur at the end of the name translation process.
93 * Symbolic links are always followed for all other pathname
94 * components other than the last.
96 * The segflg defines whether the name is to be copied from user
97 * space or kernel space.
99 * Overall outline of namei:
102 * get starting directory
103 * while (!done && !error) {
104 * call lookup to search path.
105 * if symbolic link, massage name in buffer and continue
110 register struct nameidata *ndp;
112 register struct filedesc *fdp; /* pointer to file descriptor state */
113 register char *cp; /* pointer into pathname argument */
114 register struct vnode *dp; /* the directory we are searching */
115 struct iovec aiov; /* uio for reading symbolic links */
118 struct componentname *cnp = &ndp->ni_cnd;
119 struct thread *td = cnp->cn_thread;
120 struct proc *p = td->td_proc;
123 ndp->ni_cnd.cn_cred = ndp->ni_cnd.cn_thread->td_ucred;
124 KASSERT(cnp->cn_cred && p, ("namei: bad cred/proc"));
125 KASSERT((cnp->cn_nameiop & (~OPMASK)) == 0,
126 ("namei: nameiop contaminated with flags"));
127 KASSERT((cnp->cn_flags & OPMASK) == 0,
128 ("namei: flags contaminated with nameiops"));
130 cnp->cn_flags &= ~LOCKSHARED;
134 * Get a buffer for the name to be translated, and copy the
135 * name into the buffer.
137 if ((cnp->cn_flags & HASBUF) == 0)
138 cnp->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK);
139 if (ndp->ni_segflg == UIO_SYSSPACE)
140 error = copystr(ndp->ni_dirp, cnp->cn_pnbuf,
141 MAXPATHLEN, (size_t *)&ndp->ni_pathlen);
143 error = copyinstr(ndp->ni_dirp, cnp->cn_pnbuf,
144 MAXPATHLEN, (size_t *)&ndp->ni_pathlen);
147 * Don't allow empty pathnames.
149 if (!error && *cnp->cn_pnbuf == '\0')
153 uma_zfree(namei_zone, cnp->cn_pnbuf);
155 cnp->cn_pnbuf = NULL;
156 cnp->cn_nameptr = NULL;
163 if (KTRPOINT(td, KTR_NAMEI)) {
164 KASSERT(cnp->cn_thread == curthread,
165 ("namei not using curthread"));
166 ktrnamei(cnp->cn_pnbuf);
171 * Get starting point for the translation.
174 ndp->ni_rootdir = fdp->fd_rdir;
175 ndp->ni_topdir = fdp->fd_jdir;
178 vfslocked = VFS_LOCK_GIANT(dp->v_mount);
180 FILEDESC_UNLOCK(fdp);
183 * Check if root directory should replace current directory.
184 * Done at start of translation and after symbolic link.
186 cnp->cn_nameptr = cnp->cn_pnbuf;
187 if (*(cnp->cn_nameptr) == '/') {
189 VFS_UNLOCK_GIANT(vfslocked);
190 while (*(cnp->cn_nameptr) == '/') {
194 dp = ndp->ni_rootdir;
195 vfslocked = VFS_LOCK_GIANT(dp->v_mount);
199 ndp->ni_cnd.cn_flags |= GIANTHELD;
200 ndp->ni_startdir = dp;
203 uma_zfree(namei_zone, cnp->cn_pnbuf);
205 cnp->cn_pnbuf = NULL;
206 cnp->cn_nameptr = NULL;
210 vfslocked = (ndp->ni_cnd.cn_flags & GIANTHELD) != 0;
211 ndp->ni_cnd.cn_flags &= ~GIANTHELD;
213 * Check for symbolic link
215 if ((cnp->cn_flags & ISSYMLINK) == 0) {
216 if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0) {
217 uma_zfree(namei_zone, cnp->cn_pnbuf);
219 cnp->cn_pnbuf = NULL;
220 cnp->cn_nameptr = NULL;
223 cnp->cn_flags |= HASBUF;
225 if ((cnp->cn_flags & MPSAFE) == 0) {
226 VFS_UNLOCK_GIANT(vfslocked);
227 } else if (vfslocked)
228 ndp->ni_cnd.cn_flags |= GIANTHELD;
231 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
236 if ((cnp->cn_flags & NOMACCHECK) == 0) {
237 error = mac_check_vnode_readlink(td->td_ucred,
243 if (ndp->ni_pathlen > 1)
244 cp = uma_zalloc(namei_zone, M_WAITOK);
248 aiov.iov_len = MAXPATHLEN;
249 auio.uio_iov = &aiov;
252 auio.uio_rw = UIO_READ;
253 auio.uio_segflg = UIO_SYSSPACE;
254 auio.uio_td = (struct thread *)0;
255 auio.uio_resid = MAXPATHLEN;
256 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
258 if (ndp->ni_pathlen > 1)
259 uma_zfree(namei_zone, cp);
262 linklen = MAXPATHLEN - auio.uio_resid;
264 if (ndp->ni_pathlen > 1)
265 uma_zfree(namei_zone, cp);
269 if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
270 if (ndp->ni_pathlen > 1)
271 uma_zfree(namei_zone, cp);
272 error = ENAMETOOLONG;
275 if (ndp->ni_pathlen > 1) {
276 bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
277 uma_zfree(namei_zone, cnp->cn_pnbuf);
280 cnp->cn_pnbuf[linklen] = '\0';
281 ndp->ni_pathlen += linklen;
285 uma_zfree(namei_zone, cnp->cn_pnbuf);
287 cnp->cn_pnbuf = NULL;
288 cnp->cn_nameptr = NULL;
293 VFS_UNLOCK_GIANT(vfslocked);
299 * This is a very central and rather complicated routine.
301 * The pathname is pointed to by ni_ptr and is of length ni_pathlen.
302 * The starting directory is taken from ni_startdir. The pathname is
303 * descended until done, or a symbolic link is encountered. The variable
304 * ni_more is clear if the path is completed; it is set to one if a
305 * symbolic link needing interpretation is encountered.
307 * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
308 * whether the name is to be looked up, created, renamed, or deleted.
309 * When CREATE, RENAME, or DELETE is specified, information usable in
310 * creating, renaming, or deleting a directory entry may be calculated.
311 * If flag has LOCKPARENT or'ed into it, the parent directory is returned
312 * locked. If flag has WANTPARENT or'ed into it, the parent directory is
313 * returned unlocked. Otherwise the parent directory is not returned. If
314 * the target of the pathname exists and LOCKLEAF is or'ed into the flag
315 * the target is returned locked, otherwise it is returned unlocked.
316 * When creating or renaming and LOCKPARENT is specified, the target may not
317 * be ".". When deleting and LOCKPARENT is specified, the target may be ".".
319 * Overall outline of lookup:
322 * identify next component of name at ndp->ni_ptr
323 * handle degenerate case where name is null string
324 * if .. and crossing mount points and on mounted filesys, find parent
325 * call VOP_LOOKUP routine for next component name
326 * directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set
327 * component vnode returned in ni_vp (if it exists), locked.
328 * if result vnode is mounted on and crossing mount points,
329 * find mounted on vnode
330 * if more components of name, do next level at dirloop
331 * return the answer in ni_vp, locked if LOCKLEAF set
332 * if LOCKPARENT set, return locked parent in ni_dvp
333 * if WANTPARENT set, return unlocked parent in ni_dvp
337 register struct nameidata *ndp;
339 register char *cp; /* pointer into pathname argument */
340 register struct vnode *dp = 0; /* the directory we are searching */
341 struct vnode *tdp; /* saved dp */
342 struct mount *mp; /* mount table entry */
343 int docache; /* == 0 do not cache last component */
344 int wantparent; /* 1 => wantparent or lockparent flag */
345 int rdonly; /* lookup read-only flag bit */
348 int dpunlocked = 0; /* dp has already been unlocked */
349 struct componentname *cnp = &ndp->ni_cnd;
350 struct thread *td = cnp->cn_thread;
355 * Setup: break out flag bits into variables.
357 vfslocked = (ndp->ni_cnd.cn_flags & GIANTHELD) != 0;
358 ndp->ni_cnd.cn_flags &= ~GIANTHELD;
359 wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT);
360 KASSERT(cnp->cn_nameiop == LOOKUP || wantparent,
361 ("CREATE, DELETE, RENAME require LOCKPARENT or WANTPARENT."));
362 docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE;
363 if (cnp->cn_nameiop == DELETE ||
364 (wantparent && cnp->cn_nameiop != CREATE &&
365 cnp->cn_nameiop != LOOKUP))
367 rdonly = cnp->cn_flags & RDONLY;
368 cnp->cn_flags &= ~ISSYMLINK;
371 * We use shared locks until we hit the parent of the last cn then
372 * we adjust based on the requesting flags.
375 cnp->cn_lkflags = LK_SHARED;
377 cnp->cn_lkflags = LK_EXCLUSIVE;
378 dp = ndp->ni_startdir;
379 ndp->ni_startdir = NULLVP;
380 vn_lock(dp, cnp->cn_lkflags | LK_RETRY, td);
384 * Search a new directory.
386 * The last component of the filename is left accessible via
387 * cnp->cn_nameptr for callers that need the name. Callers needing
388 * the name set the SAVENAME flag. When done, they assume
389 * responsibility for freeing the pathname buffer.
392 for (cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++)
394 cnp->cn_namelen = cp - cnp->cn_nameptr;
395 if (cnp->cn_namelen > NAME_MAX) {
396 error = ENAMETOOLONG;
399 #ifdef NAMEI_DIAGNOSTIC
402 printf("{%s}: ", cnp->cn_nameptr);
405 ndp->ni_pathlen -= cnp->cn_namelen;
409 * Replace multiple slashes by a single slash and trailing slashes
410 * by a null. This must be done before VOP_LOOKUP() because some
411 * fs's don't know about trailing slashes. Remember if there were
412 * trailing slashes to handle symlinks, existing non-directories
413 * and non-existing files that won't be directories specially later.
416 while (*cp == '/' && (cp[1] == '/' || cp[1] == '\0')) {
421 *ndp->ni_next = '\0'; /* XXX for direnter() ... */
426 cnp->cn_flags |= MAKEENTRY;
427 if (*cp == '\0' && docache == 0)
428 cnp->cn_flags &= ~MAKEENTRY;
429 if (cnp->cn_namelen == 2 &&
430 cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.')
431 cnp->cn_flags |= ISDOTDOT;
433 cnp->cn_flags &= ~ISDOTDOT;
434 if (*ndp->ni_next == 0)
435 cnp->cn_flags |= ISLASTCN;
437 cnp->cn_flags &= ~ISLASTCN;
441 * Check for degenerate name (e.g. / or "")
442 * which is a way of talking about a directory,
443 * e.g. like "/." or ".".
445 if (cnp->cn_nameptr[0] == '\0') {
446 if (dp->v_type != VDIR) {
450 if (cnp->cn_nameiop != LOOKUP) {
459 if (!(cnp->cn_flags & (LOCKPARENT | LOCKLEAF)))
460 VOP_UNLOCK(dp, 0, td);
461 /* XXX This should probably move to the top of function. */
462 if (cnp->cn_flags & SAVESTART)
463 panic("lookup: SAVESTART");
468 * Handle "..": two special cases.
469 * 1. If at root directory (e.g. after chroot)
470 * or at absolute root directory
471 * then ignore it so can't get out.
472 * 2. If this vnode is the root of a mounted
473 * filesystem, then replace it with the
474 * vnode which was mounted on so we take the
475 * .. in the other filesystem.
476 * 3. If the vnode is the top directory of
477 * the jail or chroot, don't let them out.
479 if (cnp->cn_flags & ISDOTDOT) {
481 if (dp == ndp->ni_rootdir ||
482 dp == ndp->ni_topdir ||
489 if ((dp->v_vflag & VV_ROOT) == 0 ||
490 (cnp->cn_flags & NOCROSSMOUNT))
492 if (dp->v_mount == NULL) { /* forced unmount */
497 dp = dp->v_mount->mnt_vnodecovered;
498 tvfslocked = vfslocked;
499 vfslocked = VFS_LOCK_GIANT(dp->v_mount);
502 VFS_UNLOCK_GIANT(tvfslocked);
503 vn_lock(dp, cnp->cn_lkflags | LK_RETRY, td);
508 * We now have a segment name to search for, and a directory to search.
512 if ((cnp->cn_flags & NOMACCHECK) == 0) {
513 error = mac_check_vnode_lookup(td->td_ucred, dp, cnp);
520 ASSERT_VOP_LOCKED(dp, "lookup");
522 * If we have a shared lock we may need to upgrade the lock for the
525 if (VOP_ISLOCKED(dp, td) == LK_SHARED &&
526 (cnp->cn_flags & ISLASTCN) && (cnp->cn_flags & LOCKPARENT))
527 vn_lock(dp, LK_UPGRADE|LK_RETRY, td);
529 * If we're looking up the last component and we need an exclusive
530 * lock, adjust our lkflags.
532 if ((cnp->cn_flags & (ISLASTCN|LOCKSHARED|LOCKLEAF)) ==
534 cnp->cn_lkflags = LK_EXCLUSIVE;
535 #ifdef NAMEI_DIAGNOSTIC
536 vprint("lookup in", dp);
538 if ((error = VOP_LOOKUP(dp, &ndp->ni_vp, cnp)) != 0) {
539 KASSERT(ndp->ni_vp == NULL, ("leaf should be empty"));
540 #ifdef NAMEI_DIAGNOSTIC
541 printf("not found\n");
543 if ((error == ENOENT) &&
544 (dp->v_vflag & VV_ROOT) && (dp->v_mount != NULL) &&
545 (dp->v_mount->mnt_flag & MNT_UNION)) {
547 dp = dp->v_mount->mnt_vnodecovered;
548 tvfslocked = vfslocked;
549 vfslocked = VFS_LOCK_GIANT(dp->v_mount);
552 VFS_UNLOCK_GIANT(tvfslocked);
553 vn_lock(dp, cnp->cn_lkflags | LK_RETRY, td);
557 if (error != EJUSTRETURN)
560 * If creating and at end of pathname, then can consider
561 * allowing file to be created.
567 if (*cp == '\0' && trailing_slash &&
568 !(cnp->cn_flags & WILLBEDIR)) {
572 if ((cnp->cn_flags & LOCKPARENT) == 0)
573 VOP_UNLOCK(dp, 0, td);
575 * This is a temporary assert to make sure I know what the
578 KASSERT((cnp->cn_flags & (WANTPARENT|LOCKPARENT)) != 0,
579 ("lookup: Unhandled case."));
581 * We return with ni_vp NULL to indicate that the entry
582 * doesn't currently exist, leaving a pointer to the
583 * (possibly locked) directory inode in ndp->ni_dvp.
585 if (cnp->cn_flags & SAVESTART) {
586 ndp->ni_startdir = ndp->ni_dvp;
587 VREF(ndp->ni_startdir);
591 #ifdef NAMEI_DIAGNOSTIC
595 * Take into account any additional components consumed by
596 * the underlying filesystem.
598 if (cnp->cn_consume > 0) {
599 cnp->cn_nameptr += cnp->cn_consume;
600 ndp->ni_next += cnp->cn_consume;
601 ndp->ni_pathlen -= cnp->cn_consume;
608 * Check to see if the vnode has been mounted on;
609 * if so find the root of the mounted filesystem.
611 while (dp->v_type == VDIR && (mp = dp->v_mountedhere) &&
612 (cnp->cn_flags & NOCROSSMOUNT) == 0) {
613 KASSERT(dp != ndp->ni_dvp, ("XXX"));
614 if (vfs_busy(mp, 0, 0, td))
617 tvfslocked = VFS_LOCK_GIANT(mp);
618 VFS_UNLOCK_GIANT(vfslocked);
619 vfslocked = tvfslocked;
620 error = VFS_ROOT(mp, cnp->cn_lkflags, &tdp, td);
626 ndp->ni_vp = dp = tdp;
630 * Check for symbolic link
632 if ((dp->v_type == VLNK) &&
633 ((cnp->cn_flags & FOLLOW) || trailing_slash ||
634 *ndp->ni_next == '/')) {
635 cnp->cn_flags |= ISSYMLINK;
636 if (dp->v_mount == NULL) {
637 /* We can't know whether the directory was mounted with
638 * NOSYMFOLLOW, so we can't follow safely. */
642 if (dp->v_mount->mnt_flag & MNT_NOSYMFOLLOW) {
647 * Symlink code always expects an unlocked dvp.
649 if (ndp->ni_dvp != ndp->ni_vp)
650 VOP_UNLOCK(ndp->ni_dvp, 0, td);
655 * Check for bogus trailing slashes.
657 if (trailing_slash && dp->v_type != VDIR) {
664 * Not a symbolic link. If more pathname,
665 * continue at next component, else return.
667 KASSERT((cnp->cn_flags & ISLASTCN) || *ndp->ni_next == '/',
668 ("lookup: invalid path state."));
669 if (*ndp->ni_next == '/') {
670 cnp->cn_nameptr = ndp->ni_next;
671 while (*cnp->cn_nameptr == '/') {
675 if (ndp->ni_dvp != dp)
682 * Disallow directory write attempts on read-only filesystems.
685 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
689 if (cnp->cn_flags & SAVESTART) {
690 ndp->ni_startdir = ndp->ni_dvp;
691 VREF(ndp->ni_startdir);
694 if (ndp->ni_dvp != dp)
698 } else if ((cnp->cn_flags & LOCKPARENT) == 0 && ndp->ni_dvp != dp)
699 VOP_UNLOCK(ndp->ni_dvp, 0, td);
701 if ((cnp->cn_flags & LOCKLEAF) == 0)
702 VOP_UNLOCK(dp, 0, td);
705 ndp->ni_cnd.cn_flags |= GIANTHELD;
709 if (dp != ndp->ni_dvp)
716 VFS_UNLOCK_GIANT(vfslocked);
717 ndp->ni_cnd.cn_flags &= ~GIANTHELD;
723 * relookup - lookup a path name component
724 * Used by lookup to re-aquire things.
727 relookup(dvp, vpp, cnp)
728 struct vnode *dvp, **vpp;
729 struct componentname *cnp;
731 struct thread *td = cnp->cn_thread;
732 struct vnode *dp = 0; /* the directory we are searching */
733 int wantparent; /* 1 => wantparent or lockparent flag */
734 int rdonly; /* lookup read-only flag bit */
737 KASSERT(cnp->cn_flags & ISLASTCN,
738 ("relookup: Not given last component."));
740 * Setup: break out flag bits into variables.
742 wantparent = cnp->cn_flags & (LOCKPARENT|WANTPARENT);
743 KASSERT(wantparent, ("relookup: parent not wanted."));
744 rdonly = cnp->cn_flags & RDONLY;
745 cnp->cn_flags &= ~ISSYMLINK;
747 cnp->cn_lkflags = LK_EXCLUSIVE;
748 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY, td);
751 * Search a new directory.
753 * The last component of the filename is left accessible via
754 * cnp->cn_nameptr for callers that need the name. Callers needing
755 * the name set the SAVENAME flag. When done, they assume
756 * responsibility for freeing the pathname buffer.
758 #ifdef NAMEI_DIAGNOSTIC
759 printf("{%s}: ", cnp->cn_nameptr);
763 * Check for degenerate name (e.g. / or "")
764 * which is a way of talking about a directory,
765 * e.g. like "/." or ".".
767 if (cnp->cn_nameptr[0] == '\0') {
768 if (cnp->cn_nameiop != LOOKUP || wantparent) {
772 if (dp->v_type != VDIR) {
776 if (!(cnp->cn_flags & LOCKLEAF))
777 VOP_UNLOCK(dp, 0, td);
779 /* XXX This should probably move to the top of function. */
780 if (cnp->cn_flags & SAVESTART)
781 panic("lookup: SAVESTART");
785 if (cnp->cn_flags & ISDOTDOT)
786 panic ("relookup: lookup on dot-dot");
789 * We now have a segment name to search for, and a directory to search.
791 #ifdef NAMEI_DIAGNOSTIC
792 vprint("search in:", dp);
794 if ((error = VOP_LOOKUP(dp, vpp, cnp)) != 0) {
795 KASSERT(*vpp == NULL, ("leaf should be empty"));
796 if (error != EJUSTRETURN)
799 * If creating and at end of pathname, then can consider
800 * allowing file to be created.
806 /* ASSERT(dvp == ndp->ni_startdir) */
807 if (cnp->cn_flags & SAVESTART)
809 if ((cnp->cn_flags & LOCKPARENT) == 0)
810 VOP_UNLOCK(dp, 0, td);
812 * This is a temporary assert to make sure I know what the
815 KASSERT((cnp->cn_flags & (WANTPARENT|LOCKPARENT)) != 0,
816 ("relookup: Unhandled case."));
818 * We return with ni_vp NULL to indicate that the entry
819 * doesn't currently exist, leaving a pointer to the
820 * (possibly locked) directory inode in ndp->ni_dvp.
827 * Disallow directory write attempts on read-only filesystems.
830 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
839 * Set the parent lock/ref state to the requested state.
841 if ((cnp->cn_flags & LOCKPARENT) == 0 && dvp != dp) {
843 VOP_UNLOCK(dvp, 0, td);
846 } else if (!wantparent)
849 * Check for symbolic link
851 KASSERT(dp->v_type != VLNK || !(cnp->cn_flags & FOLLOW),
852 ("relookup: symlink found.\n"));
854 /* ASSERT(dvp == ndp->ni_startdir) */
855 if (cnp->cn_flags & SAVESTART)
858 if ((cnp->cn_flags & LOCKLEAF) == 0)
859 VOP_UNLOCK(dp, 0, td);
868 * Free data allocated by namei(); see namei(9) for details.
872 struct nameidata *ndp;
881 if (!(flags & NDF_NO_FREE_PNBUF) &&
882 (ndp->ni_cnd.cn_flags & HASBUF)) {
883 uma_zfree(namei_zone, ndp->ni_cnd.cn_pnbuf);
884 ndp->ni_cnd.cn_flags &= ~HASBUF;
886 if (!(flags & NDF_NO_VP_UNLOCK) &&
887 (ndp->ni_cnd.cn_flags & LOCKLEAF) && ndp->ni_vp)
889 if (!(flags & NDF_NO_VP_RELE) && ndp->ni_vp) {
898 VOP_UNLOCK(ndp->ni_vp, 0, ndp->ni_cnd.cn_thread);
899 if (!(flags & NDF_NO_DVP_UNLOCK) &&
900 (ndp->ni_cnd.cn_flags & LOCKPARENT) &&
901 ndp->ni_dvp != ndp->ni_vp)
903 if (!(flags & NDF_NO_DVP_RELE) &&
904 (ndp->ni_cnd.cn_flags & (LOCKPARENT|WANTPARENT))) {
913 VOP_UNLOCK(ndp->ni_dvp, 0, ndp->ni_cnd.cn_thread);
914 if (!(flags & NDF_NO_STARTDIR_RELE) &&
915 (ndp->ni_cnd.cn_flags & SAVESTART)) {
916 vrele(ndp->ni_startdir);
917 ndp->ni_startdir = NULL;
922 * Determine if there is a suitable alternate filename under the specified
923 * prefix for the specified path. If the create flag is set, then the
924 * alternate prefix will be used so long as the parent directory exists.
925 * This is used by the various compatiblity ABIs so that Linux binaries prefer
926 * files under /compat/linux for example. The chosen path (whether under
927 * the prefix or under /) is returned in a kernel malloc'd buffer pointed
928 * to by pathbuf. The caller is responsible for free'ing the buffer from
929 * the M_TEMP bucket if one is returned.
932 kern_alternate_path(struct thread *td, const char *prefix, char *path,
933 enum uio_seg pathseg, char **pathbuf, int create)
935 struct nameidata nd, ndroot;
936 char *ptr, *buf, *cp;
940 buf = (char *) malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
943 /* Copy the prefix into the new pathname as a starting point. */
944 len = strlcpy(buf, prefix, MAXPATHLEN);
945 if (len >= MAXPATHLEN) {
950 sz = MAXPATHLEN - len;
953 /* Append the filename to the prefix. */
954 if (pathseg == UIO_SYSSPACE)
955 error = copystr(path, ptr, sz, &len);
957 error = copyinstr(path, ptr, sz, &len);
965 /* Only use a prefix with absolute pathnames. */
971 /* XXX: VFS_LOCK_GIANT? */
975 * We know that there is a / somewhere in this pathname.
976 * Search backwards for it, to find the file's parent dir
977 * to see if it exists in the alternate tree. If it does,
978 * and we want to create a file (cflag is set). We don't
979 * need to worry about the root comparison in this case.
983 for (cp = &ptr[len] - 1; *cp != '/'; cp--);
986 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, buf, td);
992 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, buf, td);
999 * We now compare the vnode of the prefix to the one
1000 * vnode asked. If they resolve to be the same, then we
1001 * ignore the match so that the real root gets used.
1002 * This avoids the problem of traversing "../.." to find the
1003 * root directory and never finding it, because "/" resolves
1004 * to the emulation root directory. This is expensive :-(
1006 NDINIT(&ndroot, LOOKUP, FOLLOW, UIO_SYSSPACE, prefix, td);
1008 /* We shouldn't ever get an error from this namei(). */
1009 error = namei(&ndroot);
1011 if (nd.ni_vp == ndroot.ni_vp)
1014 NDFREE(&ndroot, NDF_ONLY_PNBUF);
1015 vrele(ndroot.ni_vp);
1019 NDFREE(&nd, NDF_ONLY_PNBUF);
1023 /* XXX: VFS_UNLOCK_GIANT? */
1027 /* If there was an error, use the original path name. */
1029 bcopy(ptr, buf, len);