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