vfs_lookup.c revision 260817
1/*-
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.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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.
21 *
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
32 * SUCH DAMAGE.
33 *
34 *	@(#)vfs_lookup.c	8.4 (Berkeley) 2/16/94
35 */
36
37#include <sys/cdefs.h>
38__FBSDID("$FreeBSD: stable/10/sys/kern/vfs_lookup.c 260817 2014-01-17 10:58:59Z avg $");
39
40#include "opt_capsicum.h"
41#include "opt_kdtrace.h"
42#include "opt_ktrace.h"
43
44#include <sys/param.h>
45#include <sys/systm.h>
46#include <sys/kernel.h>
47#include <sys/capability.h>
48#include <sys/fcntl.h>
49#include <sys/jail.h>
50#include <sys/lock.h>
51#include <sys/mutex.h>
52#include <sys/namei.h>
53#include <sys/vnode.h>
54#include <sys/mount.h>
55#include <sys/filedesc.h>
56#include <sys/proc.h>
57#include <sys/sdt.h>
58#include <sys/syscallsubr.h>
59#include <sys/sysctl.h>
60#ifdef KTRACE
61#include <sys/ktrace.h>
62#endif
63
64#include <security/audit/audit.h>
65#include <security/mac/mac_framework.h>
66
67#include <vm/uma.h>
68
69#define	NAMEI_DIAGNOSTIC 1
70#undef NAMEI_DIAGNOSTIC
71
72SDT_PROVIDER_DECLARE(vfs);
73SDT_PROBE_DEFINE3(vfs, namei, lookup, entry, "struct vnode *", "char *",
74    "unsigned long");
75SDT_PROBE_DEFINE2(vfs, namei, lookup, return, "int", "struct vnode *");
76
77/*
78 * Allocation zone for namei
79 */
80uma_zone_t namei_zone;
81/*
82 * Placeholder vnode for mp traversal
83 */
84static struct vnode *vp_crossmp;
85
86static void
87nameiinit(void *dummy __unused)
88{
89
90	namei_zone = uma_zcreate("NAMEI", MAXPATHLEN, NULL, NULL, NULL, NULL,
91	    UMA_ALIGN_PTR, 0);
92	getnewvnode("crossmp", NULL, &dead_vnodeops, &vp_crossmp);
93	vn_lock(vp_crossmp, LK_EXCLUSIVE);
94	VN_LOCK_ASHARE(vp_crossmp);
95	VOP_UNLOCK(vp_crossmp, 0);
96}
97SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_SECOND, nameiinit, NULL);
98
99static int lookup_shared = 1;
100SYSCTL_INT(_vfs, OID_AUTO, lookup_shared, CTLFLAG_RW, &lookup_shared, 0,
101    "Enables/Disables shared locks for path name translation");
102TUNABLE_INT("vfs.lookup_shared", &lookup_shared);
103
104/*
105 * Convert a pathname into a pointer to a locked vnode.
106 *
107 * The FOLLOW flag is set when symbolic links are to be followed
108 * when they occur at the end of the name translation process.
109 * Symbolic links are always followed for all other pathname
110 * components other than the last.
111 *
112 * The segflg defines whether the name is to be copied from user
113 * space or kernel space.
114 *
115 * Overall outline of namei:
116 *
117 *	copy in name
118 *	get starting directory
119 *	while (!done && !error) {
120 *		call lookup to search path.
121 *		if symbolic link, massage name in buffer and continue
122 *	}
123 */
124int
125namei(struct nameidata *ndp)
126{
127	struct filedesc *fdp;	/* pointer to file descriptor state */
128	char *cp;		/* pointer into pathname argument */
129	struct vnode *dp;	/* the directory we are searching */
130	struct iovec aiov;		/* uio for reading symbolic links */
131	struct uio auio;
132	int error, linklen;
133	struct componentname *cnp = &ndp->ni_cnd;
134	struct thread *td = cnp->cn_thread;
135	struct proc *p = td->td_proc;
136
137	ndp->ni_cnd.cn_cred = ndp->ni_cnd.cn_thread->td_ucred;
138	KASSERT(cnp->cn_cred && p, ("namei: bad cred/proc"));
139	KASSERT((cnp->cn_nameiop & (~OPMASK)) == 0,
140	    ("namei: nameiop contaminated with flags"));
141	KASSERT((cnp->cn_flags & OPMASK) == 0,
142	    ("namei: flags contaminated with nameiops"));
143	if (!lookup_shared)
144		cnp->cn_flags &= ~LOCKSHARED;
145	fdp = p->p_fd;
146
147	/* We will set this ourselves if we need it. */
148	cnp->cn_flags &= ~TRAILINGSLASH;
149
150	/*
151	 * Get a buffer for the name to be translated, and copy the
152	 * name into the buffer.
153	 */
154	if ((cnp->cn_flags & HASBUF) == 0)
155		cnp->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK);
156	if (ndp->ni_segflg == UIO_SYSSPACE)
157		error = copystr(ndp->ni_dirp, cnp->cn_pnbuf,
158			    MAXPATHLEN, (size_t *)&ndp->ni_pathlen);
159	else
160		error = copyinstr(ndp->ni_dirp, cnp->cn_pnbuf,
161			    MAXPATHLEN, (size_t *)&ndp->ni_pathlen);
162
163	/*
164	 * Don't allow empty pathnames.
165	 */
166	if (!error && *cnp->cn_pnbuf == '\0')
167		error = ENOENT;
168
169#ifdef CAPABILITY_MODE
170	/*
171	 * In capability mode, lookups must be "strictly relative" (i.e.
172	 * not an absolute path, and not containing '..' components) to
173	 * a real file descriptor, not the pseudo-descriptor AT_FDCWD.
174	 */
175	if (error == 0 && IN_CAPABILITY_MODE(td) &&
176	    (cnp->cn_flags & NOCAPCHECK) == 0) {
177		ndp->ni_strictrelative = 1;
178		if (ndp->ni_dirfd == AT_FDCWD) {
179#ifdef KTRACE
180			if (KTRPOINT(td, KTR_CAPFAIL))
181				ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL);
182#endif
183			error = ECAPMODE;
184		}
185	}
186#endif
187	if (error) {
188		uma_zfree(namei_zone, cnp->cn_pnbuf);
189#ifdef DIAGNOSTIC
190		cnp->cn_pnbuf = NULL;
191		cnp->cn_nameptr = NULL;
192#endif
193		ndp->ni_vp = NULL;
194		return (error);
195	}
196	ndp->ni_loopcnt = 0;
197#ifdef KTRACE
198	if (KTRPOINT(td, KTR_NAMEI)) {
199		KASSERT(cnp->cn_thread == curthread,
200		    ("namei not using curthread"));
201		ktrnamei(cnp->cn_pnbuf);
202	}
203#endif
204	/*
205	 * Get starting point for the translation.
206	 */
207	FILEDESC_SLOCK(fdp);
208	ndp->ni_rootdir = fdp->fd_rdir;
209	ndp->ni_topdir = fdp->fd_jdir;
210
211	/*
212	 * If we are auditing the kernel pathname, save the user pathname.
213	 */
214	if (cnp->cn_flags & AUDITVNODE1)
215		AUDIT_ARG_UPATH1(td, ndp->ni_dirfd, cnp->cn_pnbuf);
216	if (cnp->cn_flags & AUDITVNODE2)
217		AUDIT_ARG_UPATH2(td, ndp->ni_dirfd, cnp->cn_pnbuf);
218
219	dp = NULL;
220	if (cnp->cn_pnbuf[0] != '/') {
221		if (ndp->ni_startdir != NULL) {
222			dp = ndp->ni_startdir;
223			error = 0;
224		} else if (ndp->ni_dirfd != AT_FDCWD) {
225			cap_rights_t rights;
226
227			rights = ndp->ni_rightsneeded;
228			cap_rights_set(&rights, CAP_LOOKUP);
229
230			if (cnp->cn_flags & AUDITVNODE1)
231				AUDIT_ARG_ATFD1(ndp->ni_dirfd);
232			if (cnp->cn_flags & AUDITVNODE2)
233				AUDIT_ARG_ATFD2(ndp->ni_dirfd);
234			error = fgetvp_rights(td, ndp->ni_dirfd,
235			    &rights, &ndp->ni_filecaps, &dp);
236#ifdef CAPABILITIES
237			/*
238			 * If file descriptor doesn't have all rights,
239			 * all lookups relative to it must also be
240			 * strictly relative.
241			 */
242			CAP_ALL(&rights);
243			if (!cap_rights_contains(&ndp->ni_filecaps.fc_rights,
244			    &rights) ||
245			    ndp->ni_filecaps.fc_fcntls != CAP_FCNTL_ALL ||
246			    ndp->ni_filecaps.fc_nioctls != -1) {
247				ndp->ni_strictrelative = 1;
248			}
249#endif
250		}
251		if (error != 0 || dp != NULL) {
252			FILEDESC_SUNLOCK(fdp);
253			if (error == 0 && dp->v_type != VDIR) {
254				vrele(dp);
255				error = ENOTDIR;
256			}
257		}
258		if (error) {
259			uma_zfree(namei_zone, cnp->cn_pnbuf);
260#ifdef DIAGNOSTIC
261			cnp->cn_pnbuf = NULL;
262			cnp->cn_nameptr = NULL;
263#endif
264			return (error);
265		}
266	}
267	if (dp == NULL) {
268		dp = fdp->fd_cdir;
269		VREF(dp);
270		FILEDESC_SUNLOCK(fdp);
271		if (ndp->ni_startdir != NULL)
272			vrele(ndp->ni_startdir);
273	}
274	SDT_PROBE(vfs, namei, lookup, entry, dp, cnp->cn_pnbuf,
275	    cnp->cn_flags, 0, 0);
276	for (;;) {
277		/*
278		 * Check if root directory should replace current directory.
279		 * Done at start of translation and after symbolic link.
280		 */
281		cnp->cn_nameptr = cnp->cn_pnbuf;
282		if (*(cnp->cn_nameptr) == '/') {
283			vrele(dp);
284			if (ndp->ni_strictrelative != 0) {
285#ifdef KTRACE
286				if (KTRPOINT(curthread, KTR_CAPFAIL))
287					ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL);
288#endif
289				return (ENOTCAPABLE);
290			}
291			while (*(cnp->cn_nameptr) == '/') {
292				cnp->cn_nameptr++;
293				ndp->ni_pathlen--;
294			}
295			dp = ndp->ni_rootdir;
296			VREF(dp);
297		}
298		ndp->ni_startdir = dp;
299		error = lookup(ndp);
300		if (error) {
301			uma_zfree(namei_zone, cnp->cn_pnbuf);
302#ifdef DIAGNOSTIC
303			cnp->cn_pnbuf = NULL;
304			cnp->cn_nameptr = NULL;
305#endif
306			SDT_PROBE(vfs, namei, lookup, return, error, NULL, 0,
307			    0, 0);
308			return (error);
309		}
310		/*
311		 * If not a symbolic link, we're done.
312		 */
313		if ((cnp->cn_flags & ISSYMLINK) == 0) {
314			if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0) {
315				uma_zfree(namei_zone, cnp->cn_pnbuf);
316#ifdef DIAGNOSTIC
317				cnp->cn_pnbuf = NULL;
318				cnp->cn_nameptr = NULL;
319#endif
320			} else
321				cnp->cn_flags |= HASBUF;
322
323			SDT_PROBE(vfs, namei, lookup, return, 0, ndp->ni_vp,
324			    0, 0, 0);
325			return (0);
326		}
327		if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
328			error = ELOOP;
329			break;
330		}
331#ifdef MAC
332		if ((cnp->cn_flags & NOMACCHECK) == 0) {
333			error = mac_vnode_check_readlink(td->td_ucred,
334			    ndp->ni_vp);
335			if (error)
336				break;
337		}
338#endif
339		if (ndp->ni_pathlen > 1)
340			cp = uma_zalloc(namei_zone, M_WAITOK);
341		else
342			cp = cnp->cn_pnbuf;
343		aiov.iov_base = cp;
344		aiov.iov_len = MAXPATHLEN;
345		auio.uio_iov = &aiov;
346		auio.uio_iovcnt = 1;
347		auio.uio_offset = 0;
348		auio.uio_rw = UIO_READ;
349		auio.uio_segflg = UIO_SYSSPACE;
350		auio.uio_td = td;
351		auio.uio_resid = MAXPATHLEN;
352		error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
353		if (error) {
354			if (ndp->ni_pathlen > 1)
355				uma_zfree(namei_zone, cp);
356			break;
357		}
358		linklen = MAXPATHLEN - auio.uio_resid;
359		if (linklen == 0) {
360			if (ndp->ni_pathlen > 1)
361				uma_zfree(namei_zone, cp);
362			error = ENOENT;
363			break;
364		}
365		if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
366			if (ndp->ni_pathlen > 1)
367				uma_zfree(namei_zone, cp);
368			error = ENAMETOOLONG;
369			break;
370		}
371		if (ndp->ni_pathlen > 1) {
372			bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
373			uma_zfree(namei_zone, cnp->cn_pnbuf);
374			cnp->cn_pnbuf = cp;
375		} else
376			cnp->cn_pnbuf[linklen] = '\0';
377		ndp->ni_pathlen += linklen;
378		vput(ndp->ni_vp);
379		dp = ndp->ni_dvp;
380	}
381	uma_zfree(namei_zone, cnp->cn_pnbuf);
382#ifdef DIAGNOSTIC
383	cnp->cn_pnbuf = NULL;
384	cnp->cn_nameptr = NULL;
385#endif
386	vput(ndp->ni_vp);
387	ndp->ni_vp = NULL;
388	vrele(ndp->ni_dvp);
389	SDT_PROBE(vfs, namei, lookup, return, error, NULL, 0, 0, 0);
390	return (error);
391}
392
393static int
394compute_cn_lkflags(struct mount *mp, int lkflags, int cnflags)
395{
396
397	if (mp == NULL || ((lkflags & LK_SHARED) &&
398	    (!(mp->mnt_kern_flag & MNTK_LOOKUP_SHARED) ||
399	    ((cnflags & ISDOTDOT) &&
400	    (mp->mnt_kern_flag & MNTK_LOOKUP_EXCL_DOTDOT))))) {
401		lkflags &= ~LK_SHARED;
402		lkflags |= LK_EXCLUSIVE;
403	}
404	return (lkflags);
405}
406
407static __inline int
408needs_exclusive_leaf(struct mount *mp, int flags)
409{
410
411	/*
412	 * Intermediate nodes can use shared locks, we only need to
413	 * force an exclusive lock for leaf nodes.
414	 */
415	if ((flags & (ISLASTCN | LOCKLEAF)) != (ISLASTCN | LOCKLEAF))
416		return (0);
417
418	/* Always use exclusive locks if LOCKSHARED isn't set. */
419	if (!(flags & LOCKSHARED))
420		return (1);
421
422	/*
423	 * For lookups during open(), if the mount point supports
424	 * extended shared operations, then use a shared lock for the
425	 * leaf node, otherwise use an exclusive lock.
426	 */
427	if ((flags & ISOPEN) != 0)
428		return (!MNT_EXTENDED_SHARED(mp));
429
430	/*
431	 * Lookup requests outside of open() that specify LOCKSHARED
432	 * only need a shared lock on the leaf vnode.
433	 */
434	return (0);
435}
436
437/*
438 * Search a pathname.
439 * This is a very central and rather complicated routine.
440 *
441 * The pathname is pointed to by ni_ptr and is of length ni_pathlen.
442 * The starting directory is taken from ni_startdir. The pathname is
443 * descended until done, or a symbolic link is encountered. The variable
444 * ni_more is clear if the path is completed; it is set to one if a
445 * symbolic link needing interpretation is encountered.
446 *
447 * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
448 * whether the name is to be looked up, created, renamed, or deleted.
449 * When CREATE, RENAME, or DELETE is specified, information usable in
450 * creating, renaming, or deleting a directory entry may be calculated.
451 * If flag has LOCKPARENT or'ed into it, the parent directory is returned
452 * locked. If flag has WANTPARENT or'ed into it, the parent directory is
453 * returned unlocked. Otherwise the parent directory is not returned. If
454 * the target of the pathname exists and LOCKLEAF is or'ed into the flag
455 * the target is returned locked, otherwise it is returned unlocked.
456 * When creating or renaming and LOCKPARENT is specified, the target may not
457 * be ".".  When deleting and LOCKPARENT is specified, the target may be ".".
458 *
459 * Overall outline of lookup:
460 *
461 * dirloop:
462 *	identify next component of name at ndp->ni_ptr
463 *	handle degenerate case where name is null string
464 *	if .. and crossing mount points and on mounted filesys, find parent
465 *	call VOP_LOOKUP routine for next component name
466 *	    directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set
467 *	    component vnode returned in ni_vp (if it exists), locked.
468 *	if result vnode is mounted on and crossing mount points,
469 *	    find mounted on vnode
470 *	if more components of name, do next level at dirloop
471 *	return the answer in ni_vp, locked if LOCKLEAF set
472 *	    if LOCKPARENT set, return locked parent in ni_dvp
473 *	    if WANTPARENT set, return unlocked parent in ni_dvp
474 */
475int
476lookup(struct nameidata *ndp)
477{
478	char *cp;		/* pointer into pathname argument */
479	struct vnode *dp = 0;	/* the directory we are searching */
480	struct vnode *tdp;		/* saved dp */
481	struct mount *mp;		/* mount table entry */
482	struct prison *pr;
483	int docache;			/* == 0 do not cache last component */
484	int wantparent;			/* 1 => wantparent or lockparent flag */
485	int rdonly;			/* lookup read-only flag bit */
486	int error = 0;
487	int dpunlocked = 0;		/* dp has already been unlocked */
488	struct componentname *cnp = &ndp->ni_cnd;
489	int lkflags_save;
490	int ni_dvp_unlocked;
491
492	/*
493	 * Setup: break out flag bits into variables.
494	 */
495	ni_dvp_unlocked = 0;
496	wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT);
497	KASSERT(cnp->cn_nameiop == LOOKUP || wantparent,
498	    ("CREATE, DELETE, RENAME require LOCKPARENT or WANTPARENT."));
499	docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE;
500	if (cnp->cn_nameiop == DELETE ||
501	    (wantparent && cnp->cn_nameiop != CREATE &&
502	     cnp->cn_nameiop != LOOKUP))
503		docache = 0;
504	rdonly = cnp->cn_flags & RDONLY;
505	cnp->cn_flags &= ~ISSYMLINK;
506	ndp->ni_dvp = NULL;
507	/*
508	 * We use shared locks until we hit the parent of the last cn then
509	 * we adjust based on the requesting flags.
510	 */
511	if (lookup_shared)
512		cnp->cn_lkflags = LK_SHARED;
513	else
514		cnp->cn_lkflags = LK_EXCLUSIVE;
515	dp = ndp->ni_startdir;
516	ndp->ni_startdir = NULLVP;
517	vn_lock(dp,
518	    compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags | LK_RETRY,
519	    cnp->cn_flags));
520
521dirloop:
522	/*
523	 * Search a new directory.
524	 *
525	 * The last component of the filename is left accessible via
526	 * cnp->cn_nameptr for callers that need the name. Callers needing
527	 * the name set the SAVENAME flag. When done, they assume
528	 * responsibility for freeing the pathname buffer.
529	 */
530	cnp->cn_consume = 0;
531	for (cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++)
532		continue;
533	cnp->cn_namelen = cp - cnp->cn_nameptr;
534	if (cnp->cn_namelen > NAME_MAX) {
535		error = ENAMETOOLONG;
536		goto bad;
537	}
538#ifdef NAMEI_DIAGNOSTIC
539	{ char c = *cp;
540	*cp = '\0';
541	printf("{%s}: ", cnp->cn_nameptr);
542	*cp = c; }
543#endif
544	ndp->ni_pathlen -= cnp->cn_namelen;
545	ndp->ni_next = cp;
546
547	/*
548	 * Replace multiple slashes by a single slash and trailing slashes
549	 * by a null.  This must be done before VOP_LOOKUP() because some
550	 * fs's don't know about trailing slashes.  Remember if there were
551	 * trailing slashes to handle symlinks, existing non-directories
552	 * and non-existing files that won't be directories specially later.
553	 */
554	while (*cp == '/' && (cp[1] == '/' || cp[1] == '\0')) {
555		cp++;
556		ndp->ni_pathlen--;
557		if (*cp == '\0') {
558			*ndp->ni_next = '\0';
559			cnp->cn_flags |= TRAILINGSLASH;
560		}
561	}
562	ndp->ni_next = cp;
563
564	cnp->cn_flags |= MAKEENTRY;
565	if (*cp == '\0' && docache == 0)
566		cnp->cn_flags &= ~MAKEENTRY;
567	if (cnp->cn_namelen == 2 &&
568	    cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.')
569		cnp->cn_flags |= ISDOTDOT;
570	else
571		cnp->cn_flags &= ~ISDOTDOT;
572	if (*ndp->ni_next == 0)
573		cnp->cn_flags |= ISLASTCN;
574	else
575		cnp->cn_flags &= ~ISLASTCN;
576
577	if ((cnp->cn_flags & ISLASTCN) != 0 &&
578	    cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.' &&
579	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
580		error = EINVAL;
581		goto bad;
582	}
583
584	/*
585	 * Check for degenerate name (e.g. / or "")
586	 * which is a way of talking about a directory,
587	 * e.g. like "/." or ".".
588	 */
589	if (cnp->cn_nameptr[0] == '\0') {
590		if (dp->v_type != VDIR) {
591			error = ENOTDIR;
592			goto bad;
593		}
594		if (cnp->cn_nameiop != LOOKUP) {
595			error = EISDIR;
596			goto bad;
597		}
598		if (wantparent) {
599			ndp->ni_dvp = dp;
600			VREF(dp);
601		}
602		ndp->ni_vp = dp;
603
604		if (cnp->cn_flags & AUDITVNODE1)
605			AUDIT_ARG_VNODE1(dp);
606		else if (cnp->cn_flags & AUDITVNODE2)
607			AUDIT_ARG_VNODE2(dp);
608
609		if (!(cnp->cn_flags & (LOCKPARENT | LOCKLEAF)))
610			VOP_UNLOCK(dp, 0);
611		/* XXX This should probably move to the top of function. */
612		if (cnp->cn_flags & SAVESTART)
613			panic("lookup: SAVESTART");
614		goto success;
615	}
616
617	/*
618	 * Handle "..": five special cases.
619	 * 0. If doing a capability lookup, return ENOTCAPABLE (this is a
620	 *    fairly conservative design choice, but it's the only one that we
621	 *    are satisfied guarantees the property we're looking for).
622	 * 1. Return an error if this is the last component of
623	 *    the name and the operation is DELETE or RENAME.
624	 * 2. If at root directory (e.g. after chroot)
625	 *    or at absolute root directory
626	 *    then ignore it so can't get out.
627	 * 3. If this vnode is the root of a mounted
628	 *    filesystem, then replace it with the
629	 *    vnode which was mounted on so we take the
630	 *    .. in the other filesystem.
631	 * 4. If the vnode is the top directory of
632	 *    the jail or chroot, don't let them out.
633	 */
634	if (cnp->cn_flags & ISDOTDOT) {
635		if (ndp->ni_strictrelative != 0) {
636#ifdef KTRACE
637			if (KTRPOINT(curthread, KTR_CAPFAIL))
638				ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL);
639#endif
640			error = ENOTCAPABLE;
641			goto bad;
642		}
643		if ((cnp->cn_flags & ISLASTCN) != 0 &&
644		    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
645			error = EINVAL;
646			goto bad;
647		}
648		for (;;) {
649			for (pr = cnp->cn_cred->cr_prison; pr != NULL;
650			     pr = pr->pr_parent)
651				if (dp == pr->pr_root)
652					break;
653			if (dp == ndp->ni_rootdir ||
654			    dp == ndp->ni_topdir ||
655			    dp == rootvnode ||
656			    pr != NULL ||
657			    ((dp->v_vflag & VV_ROOT) != 0 &&
658			     (cnp->cn_flags & NOCROSSMOUNT) != 0)) {
659				ndp->ni_dvp = dp;
660				ndp->ni_vp = dp;
661				VREF(dp);
662				goto nextname;
663			}
664			if ((dp->v_vflag & VV_ROOT) == 0)
665				break;
666			if (dp->v_iflag & VI_DOOMED) {	/* forced unmount */
667				error = ENOENT;
668				goto bad;
669			}
670			tdp = dp;
671			dp = dp->v_mount->mnt_vnodecovered;
672			VREF(dp);
673			vput(tdp);
674			vn_lock(dp,
675			    compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags |
676			    LK_RETRY, ISDOTDOT));
677		}
678	}
679
680	/*
681	 * We now have a segment name to search for, and a directory to search.
682	 */
683unionlookup:
684#ifdef MAC
685	if ((cnp->cn_flags & NOMACCHECK) == 0) {
686		error = mac_vnode_check_lookup(cnp->cn_thread->td_ucred, dp,
687		    cnp);
688		if (error)
689			goto bad;
690	}
691#endif
692	ndp->ni_dvp = dp;
693	ndp->ni_vp = NULL;
694	ASSERT_VOP_LOCKED(dp, "lookup");
695	/*
696	 * If we have a shared lock we may need to upgrade the lock for the
697	 * last operation.
698	 */
699	if (dp != vp_crossmp &&
700	    VOP_ISLOCKED(dp) == LK_SHARED &&
701	    (cnp->cn_flags & ISLASTCN) && (cnp->cn_flags & LOCKPARENT))
702		vn_lock(dp, LK_UPGRADE|LK_RETRY);
703	if ((dp->v_iflag & VI_DOOMED) != 0) {
704		error = ENOENT;
705		goto bad;
706	}
707	/*
708	 * If we're looking up the last component and we need an exclusive
709	 * lock, adjust our lkflags.
710	 */
711	if (needs_exclusive_leaf(dp->v_mount, cnp->cn_flags))
712		cnp->cn_lkflags = LK_EXCLUSIVE;
713#ifdef NAMEI_DIAGNOSTIC
714	vprint("lookup in", dp);
715#endif
716	lkflags_save = cnp->cn_lkflags;
717	cnp->cn_lkflags = compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags,
718	    cnp->cn_flags);
719	if ((error = VOP_LOOKUP(dp, &ndp->ni_vp, cnp)) != 0) {
720		cnp->cn_lkflags = lkflags_save;
721		KASSERT(ndp->ni_vp == NULL, ("leaf should be empty"));
722#ifdef NAMEI_DIAGNOSTIC
723		printf("not found\n");
724#endif
725		if ((error == ENOENT) &&
726		    (dp->v_vflag & VV_ROOT) && (dp->v_mount != NULL) &&
727		    (dp->v_mount->mnt_flag & MNT_UNION)) {
728			tdp = dp;
729			dp = dp->v_mount->mnt_vnodecovered;
730			VREF(dp);
731			vput(tdp);
732			vn_lock(dp,
733			    compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags |
734			    LK_RETRY, cnp->cn_flags));
735			goto unionlookup;
736		}
737
738		if (error != EJUSTRETURN)
739			goto bad;
740		/*
741		 * At this point, we know we're at the end of the
742		 * pathname.  If creating / renaming, we can consider
743		 * allowing the file or directory to be created / renamed,
744		 * provided we're not on a read-only filesystem.
745		 */
746		if (rdonly) {
747			error = EROFS;
748			goto bad;
749		}
750		/* trailing slash only allowed for directories */
751		if ((cnp->cn_flags & TRAILINGSLASH) &&
752		    !(cnp->cn_flags & WILLBEDIR)) {
753			error = ENOENT;
754			goto bad;
755		}
756		if ((cnp->cn_flags & LOCKPARENT) == 0)
757			VOP_UNLOCK(dp, 0);
758		/*
759		 * We return with ni_vp NULL to indicate that the entry
760		 * doesn't currently exist, leaving a pointer to the
761		 * (possibly locked) directory vnode in ndp->ni_dvp.
762		 */
763		if (cnp->cn_flags & SAVESTART) {
764			ndp->ni_startdir = ndp->ni_dvp;
765			VREF(ndp->ni_startdir);
766		}
767		goto success;
768	} else
769		cnp->cn_lkflags = lkflags_save;
770#ifdef NAMEI_DIAGNOSTIC
771	printf("found\n");
772#endif
773	/*
774	 * Take into account any additional components consumed by
775	 * the underlying filesystem.
776	 */
777	if (cnp->cn_consume > 0) {
778		cnp->cn_nameptr += cnp->cn_consume;
779		ndp->ni_next += cnp->cn_consume;
780		ndp->ni_pathlen -= cnp->cn_consume;
781		cnp->cn_consume = 0;
782	}
783
784	dp = ndp->ni_vp;
785
786	/*
787	 * Check to see if the vnode has been mounted on;
788	 * if so find the root of the mounted filesystem.
789	 */
790	while (dp->v_type == VDIR && (mp = dp->v_mountedhere) &&
791	       (cnp->cn_flags & NOCROSSMOUNT) == 0) {
792		if (vfs_busy(mp, 0))
793			continue;
794		vput(dp);
795		if (dp != ndp->ni_dvp)
796			vput(ndp->ni_dvp);
797		else
798			vrele(ndp->ni_dvp);
799		vref(vp_crossmp);
800		ndp->ni_dvp = vp_crossmp;
801		error = VFS_ROOT(mp, compute_cn_lkflags(mp, cnp->cn_lkflags,
802		    cnp->cn_flags), &tdp);
803		vfs_unbusy(mp);
804		if (vn_lock(vp_crossmp, LK_SHARED | LK_NOWAIT))
805			panic("vp_crossmp exclusively locked or reclaimed");
806		if (error) {
807			dpunlocked = 1;
808			goto bad2;
809		}
810		ndp->ni_vp = dp = tdp;
811	}
812
813	/*
814	 * Check for symbolic link
815	 */
816	if ((dp->v_type == VLNK) &&
817	    ((cnp->cn_flags & FOLLOW) || (cnp->cn_flags & TRAILINGSLASH) ||
818	     *ndp->ni_next == '/')) {
819		cnp->cn_flags |= ISSYMLINK;
820		if (dp->v_iflag & VI_DOOMED) {
821			/*
822			 * We can't know whether the directory was mounted with
823			 * NOSYMFOLLOW, so we can't follow safely.
824			 */
825			error = ENOENT;
826			goto bad2;
827		}
828		if (dp->v_mount->mnt_flag & MNT_NOSYMFOLLOW) {
829			error = EACCES;
830			goto bad2;
831		}
832		/*
833		 * Symlink code always expects an unlocked dvp.
834		 */
835		if (ndp->ni_dvp != ndp->ni_vp) {
836			VOP_UNLOCK(ndp->ni_dvp, 0);
837			ni_dvp_unlocked = 1;
838		}
839		goto success;
840	}
841
842nextname:
843	/*
844	 * Not a symbolic link that we will follow.  Continue with the
845	 * next component if there is any; otherwise, we're done.
846	 */
847	KASSERT((cnp->cn_flags & ISLASTCN) || *ndp->ni_next == '/',
848	    ("lookup: invalid path state."));
849	if (*ndp->ni_next == '/') {
850		cnp->cn_nameptr = ndp->ni_next;
851		while (*cnp->cn_nameptr == '/') {
852			cnp->cn_nameptr++;
853			ndp->ni_pathlen--;
854		}
855		if (ndp->ni_dvp != dp)
856			vput(ndp->ni_dvp);
857		else
858			vrele(ndp->ni_dvp);
859		goto dirloop;
860	}
861	/*
862	 * If we're processing a path with a trailing slash,
863	 * check that the end result is a directory.
864	 */
865	if ((cnp->cn_flags & TRAILINGSLASH) && dp->v_type != VDIR) {
866		error = ENOTDIR;
867		goto bad2;
868	}
869	/*
870	 * Disallow directory write attempts on read-only filesystems.
871	 */
872	if (rdonly &&
873	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
874		error = EROFS;
875		goto bad2;
876	}
877	if (cnp->cn_flags & SAVESTART) {
878		ndp->ni_startdir = ndp->ni_dvp;
879		VREF(ndp->ni_startdir);
880	}
881	if (!wantparent) {
882		ni_dvp_unlocked = 2;
883		if (ndp->ni_dvp != dp)
884			vput(ndp->ni_dvp);
885		else
886			vrele(ndp->ni_dvp);
887	} else if ((cnp->cn_flags & LOCKPARENT) == 0 && ndp->ni_dvp != dp) {
888		VOP_UNLOCK(ndp->ni_dvp, 0);
889		ni_dvp_unlocked = 1;
890	}
891
892	if (cnp->cn_flags & AUDITVNODE1)
893		AUDIT_ARG_VNODE1(dp);
894	else if (cnp->cn_flags & AUDITVNODE2)
895		AUDIT_ARG_VNODE2(dp);
896
897	if ((cnp->cn_flags & LOCKLEAF) == 0)
898		VOP_UNLOCK(dp, 0);
899success:
900	/*
901	 * Because of lookup_shared we may have the vnode shared locked, but
902	 * the caller may want it to be exclusively locked.
903	 */
904	if (needs_exclusive_leaf(dp->v_mount, cnp->cn_flags) &&
905	    VOP_ISLOCKED(dp) != LK_EXCLUSIVE) {
906		vn_lock(dp, LK_UPGRADE | LK_RETRY);
907		if (dp->v_iflag & VI_DOOMED) {
908			error = ENOENT;
909			goto bad2;
910		}
911	}
912	return (0);
913
914bad2:
915	if (ni_dvp_unlocked != 2) {
916		if (dp != ndp->ni_dvp && !ni_dvp_unlocked)
917			vput(ndp->ni_dvp);
918		else
919			vrele(ndp->ni_dvp);
920	}
921bad:
922	if (!dpunlocked)
923		vput(dp);
924	ndp->ni_vp = NULL;
925	return (error);
926}
927
928/*
929 * relookup - lookup a path name component
930 *    Used by lookup to re-acquire things.
931 */
932int
933relookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp)
934{
935	struct vnode *dp = 0;		/* the directory we are searching */
936	int wantparent;			/* 1 => wantparent or lockparent flag */
937	int rdonly;			/* lookup read-only flag bit */
938	int error = 0;
939
940	KASSERT(cnp->cn_flags & ISLASTCN,
941	    ("relookup: Not given last component."));
942	/*
943	 * Setup: break out flag bits into variables.
944	 */
945	wantparent = cnp->cn_flags & (LOCKPARENT|WANTPARENT);
946	KASSERT(wantparent, ("relookup: parent not wanted."));
947	rdonly = cnp->cn_flags & RDONLY;
948	cnp->cn_flags &= ~ISSYMLINK;
949	dp = dvp;
950	cnp->cn_lkflags = LK_EXCLUSIVE;
951	vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
952
953	/*
954	 * Search a new directory.
955	 *
956	 * The last component of the filename is left accessible via
957	 * cnp->cn_nameptr for callers that need the name. Callers needing
958	 * the name set the SAVENAME flag. When done, they assume
959	 * responsibility for freeing the pathname buffer.
960	 */
961#ifdef NAMEI_DIAGNOSTIC
962	printf("{%s}: ", cnp->cn_nameptr);
963#endif
964
965	/*
966	 * Check for "" which represents the root directory after slash
967	 * removal.
968	 */
969	if (cnp->cn_nameptr[0] == '\0') {
970		/*
971		 * Support only LOOKUP for "/" because lookup()
972		 * can't succeed for CREATE, DELETE and RENAME.
973		 */
974		KASSERT(cnp->cn_nameiop == LOOKUP, ("nameiop must be LOOKUP"));
975		KASSERT(dp->v_type == VDIR, ("dp is not a directory"));
976
977		if (!(cnp->cn_flags & LOCKLEAF))
978			VOP_UNLOCK(dp, 0);
979		*vpp = dp;
980		/* XXX This should probably move to the top of function. */
981		if (cnp->cn_flags & SAVESTART)
982			panic("lookup: SAVESTART");
983		return (0);
984	}
985
986	if (cnp->cn_flags & ISDOTDOT)
987		panic ("relookup: lookup on dot-dot");
988
989	/*
990	 * We now have a segment name to search for, and a directory to search.
991	 */
992#ifdef NAMEI_DIAGNOSTIC
993	vprint("search in:", dp);
994#endif
995	if ((error = VOP_LOOKUP(dp, vpp, cnp)) != 0) {
996		KASSERT(*vpp == NULL, ("leaf should be empty"));
997		if (error != EJUSTRETURN)
998			goto bad;
999		/*
1000		 * If creating and at end of pathname, then can consider
1001		 * allowing file to be created.
1002		 */
1003		if (rdonly) {
1004			error = EROFS;
1005			goto bad;
1006		}
1007		/* ASSERT(dvp == ndp->ni_startdir) */
1008		if (cnp->cn_flags & SAVESTART)
1009			VREF(dvp);
1010		if ((cnp->cn_flags & LOCKPARENT) == 0)
1011			VOP_UNLOCK(dp, 0);
1012		/*
1013		 * We return with ni_vp NULL to indicate that the entry
1014		 * doesn't currently exist, leaving a pointer to the
1015		 * (possibly locked) directory vnode in ndp->ni_dvp.
1016		 */
1017		return (0);
1018	}
1019
1020	dp = *vpp;
1021
1022	/*
1023	 * Disallow directory write attempts on read-only filesystems.
1024	 */
1025	if (rdonly &&
1026	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
1027		if (dvp == dp)
1028			vrele(dvp);
1029		else
1030			vput(dvp);
1031		error = EROFS;
1032		goto bad;
1033	}
1034	/*
1035	 * Set the parent lock/ref state to the requested state.
1036	 */
1037	if ((cnp->cn_flags & LOCKPARENT) == 0 && dvp != dp) {
1038		if (wantparent)
1039			VOP_UNLOCK(dvp, 0);
1040		else
1041			vput(dvp);
1042	} else if (!wantparent)
1043		vrele(dvp);
1044	/*
1045	 * Check for symbolic link
1046	 */
1047	KASSERT(dp->v_type != VLNK || !(cnp->cn_flags & FOLLOW),
1048	    ("relookup: symlink found.\n"));
1049
1050	/* ASSERT(dvp == ndp->ni_startdir) */
1051	if (cnp->cn_flags & SAVESTART)
1052		VREF(dvp);
1053
1054	if ((cnp->cn_flags & LOCKLEAF) == 0)
1055		VOP_UNLOCK(dp, 0);
1056	return (0);
1057bad:
1058	vput(dp);
1059	*vpp = NULL;
1060	return (error);
1061}
1062
1063void
1064NDINIT_ALL(struct nameidata *ndp, u_long op, u_long flags, enum uio_seg segflg,
1065    const char *namep, int dirfd, struct vnode *startdir, cap_rights_t *rightsp,
1066    struct thread *td)
1067{
1068
1069	ndp->ni_cnd.cn_nameiop = op;
1070	ndp->ni_cnd.cn_flags = flags;
1071	ndp->ni_segflg = segflg;
1072	ndp->ni_dirp = namep;
1073	ndp->ni_dirfd = dirfd;
1074	ndp->ni_startdir = startdir;
1075	ndp->ni_strictrelative = 0;
1076	if (rightsp != NULL)
1077		ndp->ni_rightsneeded = *rightsp;
1078	else
1079		cap_rights_init(&ndp->ni_rightsneeded);
1080	filecaps_init(&ndp->ni_filecaps);
1081	ndp->ni_cnd.cn_thread = td;
1082}
1083
1084/*
1085 * Free data allocated by namei(); see namei(9) for details.
1086 */
1087void
1088NDFREE(struct nameidata *ndp, const u_int flags)
1089{
1090	int unlock_dvp;
1091	int unlock_vp;
1092
1093	unlock_dvp = 0;
1094	unlock_vp = 0;
1095
1096	if (!(flags & NDF_NO_FREE_PNBUF) &&
1097	    (ndp->ni_cnd.cn_flags & HASBUF)) {
1098		uma_zfree(namei_zone, ndp->ni_cnd.cn_pnbuf);
1099		ndp->ni_cnd.cn_flags &= ~HASBUF;
1100	}
1101	if (!(flags & NDF_NO_VP_UNLOCK) &&
1102	    (ndp->ni_cnd.cn_flags & LOCKLEAF) && ndp->ni_vp)
1103		unlock_vp = 1;
1104	if (!(flags & NDF_NO_VP_RELE) && ndp->ni_vp) {
1105		if (unlock_vp) {
1106			vput(ndp->ni_vp);
1107			unlock_vp = 0;
1108		} else
1109			vrele(ndp->ni_vp);
1110		ndp->ni_vp = NULL;
1111	}
1112	if (unlock_vp)
1113		VOP_UNLOCK(ndp->ni_vp, 0);
1114	if (!(flags & NDF_NO_DVP_UNLOCK) &&
1115	    (ndp->ni_cnd.cn_flags & LOCKPARENT) &&
1116	    ndp->ni_dvp != ndp->ni_vp)
1117		unlock_dvp = 1;
1118	if (!(flags & NDF_NO_DVP_RELE) &&
1119	    (ndp->ni_cnd.cn_flags & (LOCKPARENT|WANTPARENT))) {
1120		if (unlock_dvp) {
1121			vput(ndp->ni_dvp);
1122			unlock_dvp = 0;
1123		} else
1124			vrele(ndp->ni_dvp);
1125		ndp->ni_dvp = NULL;
1126	}
1127	if (unlock_dvp)
1128		VOP_UNLOCK(ndp->ni_dvp, 0);
1129	if (!(flags & NDF_NO_STARTDIR_RELE) &&
1130	    (ndp->ni_cnd.cn_flags & SAVESTART)) {
1131		vrele(ndp->ni_startdir);
1132		ndp->ni_startdir = NULL;
1133	}
1134}
1135
1136/*
1137 * Determine if there is a suitable alternate filename under the specified
1138 * prefix for the specified path.  If the create flag is set, then the
1139 * alternate prefix will be used so long as the parent directory exists.
1140 * This is used by the various compatiblity ABIs so that Linux binaries prefer
1141 * files under /compat/linux for example.  The chosen path (whether under
1142 * the prefix or under /) is returned in a kernel malloc'd buffer pointed
1143 * to by pathbuf.  The caller is responsible for free'ing the buffer from
1144 * the M_TEMP bucket if one is returned.
1145 */
1146int
1147kern_alternate_path(struct thread *td, const char *prefix, const char *path,
1148    enum uio_seg pathseg, char **pathbuf, int create, int dirfd)
1149{
1150	struct nameidata nd, ndroot;
1151	char *ptr, *buf, *cp;
1152	size_t len, sz;
1153	int error;
1154
1155	buf = (char *) malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1156	*pathbuf = buf;
1157
1158	/* Copy the prefix into the new pathname as a starting point. */
1159	len = strlcpy(buf, prefix, MAXPATHLEN);
1160	if (len >= MAXPATHLEN) {
1161		*pathbuf = NULL;
1162		free(buf, M_TEMP);
1163		return (EINVAL);
1164	}
1165	sz = MAXPATHLEN - len;
1166	ptr = buf + len;
1167
1168	/* Append the filename to the prefix. */
1169	if (pathseg == UIO_SYSSPACE)
1170		error = copystr(path, ptr, sz, &len);
1171	else
1172		error = copyinstr(path, ptr, sz, &len);
1173
1174	if (error) {
1175		*pathbuf = NULL;
1176		free(buf, M_TEMP);
1177		return (error);
1178	}
1179
1180	/* Only use a prefix with absolute pathnames. */
1181	if (*ptr != '/') {
1182		error = EINVAL;
1183		goto keeporig;
1184	}
1185
1186	if (dirfd != AT_FDCWD) {
1187		/*
1188		 * We want the original because the "prefix" is
1189		 * included in the already opened dirfd.
1190		 */
1191		bcopy(ptr, buf, len);
1192		return (0);
1193	}
1194
1195	/*
1196	 * We know that there is a / somewhere in this pathname.
1197	 * Search backwards for it, to find the file's parent dir
1198	 * to see if it exists in the alternate tree. If it does,
1199	 * and we want to create a file (cflag is set). We don't
1200	 * need to worry about the root comparison in this case.
1201	 */
1202
1203	if (create) {
1204		for (cp = &ptr[len] - 1; *cp != '/'; cp--);
1205		*cp = '\0';
1206
1207		NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, buf, td);
1208		error = namei(&nd);
1209		*cp = '/';
1210		if (error != 0)
1211			goto keeporig;
1212	} else {
1213		NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, buf, td);
1214
1215		error = namei(&nd);
1216		if (error != 0)
1217			goto keeporig;
1218
1219		/*
1220		 * We now compare the vnode of the prefix to the one
1221		 * vnode asked. If they resolve to be the same, then we
1222		 * ignore the match so that the real root gets used.
1223		 * This avoids the problem of traversing "../.." to find the
1224		 * root directory and never finding it, because "/" resolves
1225		 * to the emulation root directory. This is expensive :-(
1226		 */
1227		NDINIT(&ndroot, LOOKUP, FOLLOW, UIO_SYSSPACE, prefix,
1228		    td);
1229
1230		/* We shouldn't ever get an error from this namei(). */
1231		error = namei(&ndroot);
1232		if (error == 0) {
1233			if (nd.ni_vp == ndroot.ni_vp)
1234				error = ENOENT;
1235
1236			NDFREE(&ndroot, NDF_ONLY_PNBUF);
1237			vrele(ndroot.ni_vp);
1238		}
1239	}
1240
1241	NDFREE(&nd, NDF_ONLY_PNBUF);
1242	vrele(nd.ni_vp);
1243
1244keeporig:
1245	/* If there was an error, use the original path name. */
1246	if (error)
1247		bcopy(ptr, buf, len);
1248	return (error);
1249}
1250