xref: /freebsd/sys/kern/vfs_lookup.c (revision f257b7a54b4fe77840cf694314bdc401e00c31a1)
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/lock.h>
47 #include <sys/mac.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 #ifdef KTRACE
55 #include <sys/ktrace.h>
56 #endif
57 
58 #include <vm/uma.h>
59 
60 /*
61  * Allocation zone for namei
62  */
63 uma_zone_t namei_zone;
64 
65 static void
66 nameiinit(void *dummy __unused)
67 {
68 	namei_zone = uma_zcreate("NAMEI", MAXPATHLEN, NULL, NULL, NULL, NULL,
69 	    UMA_ALIGN_PTR, 0);
70 
71 }
72 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_SECOND, nameiinit, NULL)
73 
74 /*
75  * Convert a pathname into a pointer to a locked inode.
76  *
77  * The FOLLOW flag is set when symbolic links are to be followed
78  * when they occur at the end of the name translation process.
79  * Symbolic links are always followed for all other pathname
80  * components other than the last.
81  *
82  * The segflg defines whether the name is to be copied from user
83  * space or kernel space.
84  *
85  * Overall outline of namei:
86  *
87  *	copy in name
88  *	get starting directory
89  *	while (!done && !error) {
90  *		call lookup to search path.
91  *		if symbolic link, massage name in buffer and continue
92  *	}
93  */
94 int
95 namei(ndp)
96 	register struct nameidata *ndp;
97 {
98 	register struct filedesc *fdp;	/* pointer to file descriptor state */
99 	register char *cp;		/* pointer into pathname argument */
100 	register struct vnode *dp;	/* the directory we are searching */
101 	struct iovec aiov;		/* uio for reading symbolic links */
102 	struct uio auio;
103 	int error, linklen;
104 	struct componentname *cnp = &ndp->ni_cnd;
105 	struct thread *td = cnp->cn_thread;
106 	struct proc *p = td->td_proc;
107 
108 	ndp->ni_cnd.cn_cred = ndp->ni_cnd.cn_thread->td_ucred;
109 	KASSERT(cnp->cn_cred && p, ("namei: bad cred/proc"));
110 	KASSERT((cnp->cn_nameiop & (~OPMASK)) == 0,
111 	    ("namei: nameiop contaminated with flags"));
112 	KASSERT((cnp->cn_flags & OPMASK) == 0,
113 	    ("namei: flags contaminated with nameiops"));
114 	fdp = p->p_fd;
115 
116 	/*
117 	 * Get a buffer for the name to be translated, and copy the
118 	 * name into the buffer.
119 	 */
120 	if ((cnp->cn_flags & HASBUF) == 0)
121 		cnp->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK);
122 	if (ndp->ni_segflg == UIO_SYSSPACE)
123 		error = copystr(ndp->ni_dirp, cnp->cn_pnbuf,
124 			    MAXPATHLEN, (size_t *)&ndp->ni_pathlen);
125 	else
126 		error = copyinstr(ndp->ni_dirp, cnp->cn_pnbuf,
127 			    MAXPATHLEN, (size_t *)&ndp->ni_pathlen);
128 
129 	/*
130 	 * Don't allow empty pathnames.
131 	 */
132 	if (!error && *cnp->cn_pnbuf == '\0')
133 		error = ENOENT;
134 
135 	if (error) {
136 		uma_zfree(namei_zone, cnp->cn_pnbuf);
137 #ifdef DIAGNOSTIC
138 		cnp->cn_pnbuf = NULL;
139 		cnp->cn_nameptr = NULL;
140 #endif
141 		ndp->ni_vp = NULL;
142 		return (error);
143 	}
144 	ndp->ni_loopcnt = 0;
145 #ifdef KTRACE
146 	if (KTRPOINT(td, KTR_NAMEI)) {
147 		KASSERT(cnp->cn_thread == curthread,
148 		    ("namei not using curthread"));
149 		ktrnamei(cnp->cn_pnbuf);
150 	}
151 #endif
152 
153 	/*
154 	 * Get starting point for the translation.
155 	 */
156 	FILEDESC_LOCK(fdp);
157 	ndp->ni_rootdir = fdp->fd_rdir;
158 	ndp->ni_topdir = fdp->fd_jdir;
159 
160 	dp = fdp->fd_cdir;
161 	VREF(dp);
162 	FILEDESC_UNLOCK(fdp);
163 	for (;;) {
164 		/*
165 		 * Check if root directory should replace current directory.
166 		 * Done at start of translation and after symbolic link.
167 		 */
168 		cnp->cn_nameptr = cnp->cn_pnbuf;
169 		if (*(cnp->cn_nameptr) == '/') {
170 			vrele(dp);
171 			while (*(cnp->cn_nameptr) == '/') {
172 				cnp->cn_nameptr++;
173 				ndp->ni_pathlen--;
174 			}
175 			dp = ndp->ni_rootdir;
176 			VREF(dp);
177 		}
178 		ndp->ni_startdir = dp;
179 		error = lookup(ndp);
180 		if (error) {
181 			uma_zfree(namei_zone, cnp->cn_pnbuf);
182 #ifdef DIAGNOSTIC
183 			cnp->cn_pnbuf = NULL;
184 			cnp->cn_nameptr = NULL;
185 #endif
186 			return (error);
187 		}
188 		/*
189 		 * Check for symbolic link
190 		 */
191 		if ((cnp->cn_flags & ISSYMLINK) == 0) {
192 			if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0) {
193 				uma_zfree(namei_zone, cnp->cn_pnbuf);
194 #ifdef DIAGNOSTIC
195 				cnp->cn_pnbuf = NULL;
196 				cnp->cn_nameptr = NULL;
197 #endif
198 			} else
199 				cnp->cn_flags |= HASBUF;
200 
201 			if (vn_canvmio(ndp->ni_vp) == TRUE &&
202 				(cnp->cn_nameiop != DELETE) &&
203 				((cnp->cn_flags & (NOOBJ|LOCKLEAF)) ==
204 				 LOCKLEAF))
205 				vfs_object_create(ndp->ni_vp, td,
206 					ndp->ni_cnd.cn_cred);
207 
208 			return (0);
209 		}
210 		if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1)
211 			VOP_UNLOCK(ndp->ni_dvp, 0, td);
212 		if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
213 			error = ELOOP;
214 			break;
215 		}
216 #ifdef MAC
217 		if ((cnp->cn_flags & NOMACCHECK) == 0) {
218 			error = mac_check_vnode_readlink(td->td_ucred,
219 			    ndp->ni_vp);
220 			if (error)
221 				break;
222 		}
223 #endif
224 		if (ndp->ni_pathlen > 1)
225 			cp = uma_zalloc(namei_zone, M_WAITOK);
226 		else
227 			cp = cnp->cn_pnbuf;
228 		aiov.iov_base = cp;
229 		aiov.iov_len = MAXPATHLEN;
230 		auio.uio_iov = &aiov;
231 		auio.uio_iovcnt = 1;
232 		auio.uio_offset = 0;
233 		auio.uio_rw = UIO_READ;
234 		auio.uio_segflg = UIO_SYSSPACE;
235 		auio.uio_td = (struct thread *)0;
236 		auio.uio_resid = MAXPATHLEN;
237 		error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
238 		if (error) {
239 			if (ndp->ni_pathlen > 1)
240 				uma_zfree(namei_zone, cp);
241 			break;
242 		}
243 		linklen = MAXPATHLEN - auio.uio_resid;
244 		if (linklen == 0) {
245 			if (ndp->ni_pathlen > 1)
246 				uma_zfree(namei_zone, cp);
247 			error = ENOENT;
248 			break;
249 		}
250 		if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
251 			if (ndp->ni_pathlen > 1)
252 				uma_zfree(namei_zone, cp);
253 			error = ENAMETOOLONG;
254 			break;
255 		}
256 		if (ndp->ni_pathlen > 1) {
257 			bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
258 			uma_zfree(namei_zone, cnp->cn_pnbuf);
259 			cnp->cn_pnbuf = cp;
260 		} else
261 			cnp->cn_pnbuf[linklen] = '\0';
262 		ndp->ni_pathlen += linklen;
263 		vput(ndp->ni_vp);
264 		dp = ndp->ni_dvp;
265 	}
266 	uma_zfree(namei_zone, cnp->cn_pnbuf);
267 #ifdef DIAGNOSTIC
268 	cnp->cn_pnbuf = NULL;
269 	cnp->cn_nameptr = NULL;
270 #endif
271 	vrele(ndp->ni_dvp);
272 	vput(ndp->ni_vp);
273 	ndp->ni_vp = NULL;
274 	return (error);
275 }
276 
277 /*
278  * Search a pathname.
279  * This is a very central and rather complicated routine.
280  *
281  * The pathname is pointed to by ni_ptr and is of length ni_pathlen.
282  * The starting directory is taken from ni_startdir. The pathname is
283  * descended until done, or a symbolic link is encountered. The variable
284  * ni_more is clear if the path is completed; it is set to one if a
285  * symbolic link needing interpretation is encountered.
286  *
287  * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
288  * whether the name is to be looked up, created, renamed, or deleted.
289  * When CREATE, RENAME, or DELETE is specified, information usable in
290  * creating, renaming, or deleting a directory entry may be calculated.
291  * If flag has LOCKPARENT or'ed into it, the parent directory is returned
292  * locked. If flag has WANTPARENT or'ed into it, the parent directory is
293  * returned unlocked. Otherwise the parent directory is not returned. If
294  * the target of the pathname exists and LOCKLEAF is or'ed into the flag
295  * the target is returned locked, otherwise it is returned unlocked.
296  * When creating or renaming and LOCKPARENT is specified, the target may not
297  * be ".".  When deleting and LOCKPARENT is specified, the target may be ".".
298  *
299  * Overall outline of lookup:
300  *
301  * dirloop:
302  *	identify next component of name at ndp->ni_ptr
303  *	handle degenerate case where name is null string
304  *	if .. and crossing mount points and on mounted filesys, find parent
305  *	call VOP_LOOKUP routine for next component name
306  *	    directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set
307  *	    component vnode returned in ni_vp (if it exists), locked.
308  *	if result vnode is mounted on and crossing mount points,
309  *	    find mounted on vnode
310  *	if more components of name, do next level at dirloop
311  *	return the answer in ni_vp, locked if LOCKLEAF set
312  *	    if LOCKPARENT set, return locked parent in ni_dvp
313  *	    if WANTPARENT set, return unlocked parent in ni_dvp
314  */
315 int
316 lookup(ndp)
317 	register struct nameidata *ndp;
318 {
319 	register char *cp;		/* pointer into pathname argument */
320 	register struct vnode *dp = 0;	/* the directory we are searching */
321 	struct vnode *tdp;		/* saved dp */
322 	struct mount *mp;		/* mount table entry */
323 	int docache;			/* == 0 do not cache last component */
324 	int wantparent;			/* 1 => wantparent or lockparent flag */
325 	int rdonly;			/* lookup read-only flag bit */
326 	int trailing_slash;
327 	int error = 0;
328 	int dpunlocked = 0;		/* dp has already been unlocked */
329 	struct componentname *cnp = &ndp->ni_cnd;
330 	struct thread *td = cnp->cn_thread;
331 
332 	/*
333 	 * Setup: break out flag bits into variables.
334 	 */
335 	wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT);
336 	docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE;
337 	if (cnp->cn_nameiop == DELETE ||
338 	    (wantparent && cnp->cn_nameiop != CREATE &&
339 	     cnp->cn_nameiop != LOOKUP))
340 		docache = 0;
341 	rdonly = cnp->cn_flags & RDONLY;
342 	ndp->ni_dvp = NULL;
343 	cnp->cn_flags &= ~ISSYMLINK;
344 	dp = ndp->ni_startdir;
345 	ndp->ni_startdir = NULLVP;
346 	vn_lock(dp, LK_EXCLUSIVE | LK_RETRY, td);
347 
348 dirloop:
349 	/*
350 	 * Search a new directory.
351 	 *
352 	 * The last component of the filename is left accessible via
353 	 * cnp->cn_nameptr for callers that need the name. Callers needing
354 	 * the name set the SAVENAME flag. When done, they assume
355 	 * responsibility for freeing the pathname buffer.
356 	 */
357 	cnp->cn_consume = 0;
358 	for (cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++)
359 		continue;
360 	cnp->cn_namelen = cp - cnp->cn_nameptr;
361 	if (cnp->cn_namelen > NAME_MAX) {
362 		error = ENAMETOOLONG;
363 		goto bad;
364 	}
365 #ifdef NAMEI_DIAGNOSTIC
366 	{ char c = *cp;
367 	*cp = '\0';
368 	printf("{%s}: ", cnp->cn_nameptr);
369 	*cp = c; }
370 #endif
371 	ndp->ni_pathlen -= cnp->cn_namelen;
372 	ndp->ni_next = cp;
373 
374 	/*
375 	 * Replace multiple slashes by a single slash and trailing slashes
376 	 * by a null.  This must be done before VOP_LOOKUP() because some
377 	 * fs's don't know about trailing slashes.  Remember if there were
378 	 * trailing slashes to handle symlinks, existing non-directories
379 	 * and non-existing files that won't be directories specially later.
380 	 */
381 	trailing_slash = 0;
382 	while (*cp == '/' && (cp[1] == '/' || cp[1] == '\0')) {
383 		cp++;
384 		ndp->ni_pathlen--;
385 		if (*cp == '\0') {
386 			trailing_slash = 1;
387 			*ndp->ni_next = '\0';	/* XXX for direnter() ... */
388 		}
389 	}
390 	ndp->ni_next = cp;
391 
392 	cnp->cn_flags |= MAKEENTRY;
393 	if (*cp == '\0' && docache == 0)
394 		cnp->cn_flags &= ~MAKEENTRY;
395 	if (cnp->cn_namelen == 2 &&
396 	    cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.')
397 		cnp->cn_flags |= ISDOTDOT;
398 	else
399 		cnp->cn_flags &= ~ISDOTDOT;
400 	if (*ndp->ni_next == 0)
401 		cnp->cn_flags |= ISLASTCN;
402 	else
403 		cnp->cn_flags &= ~ISLASTCN;
404 
405 
406 	/*
407 	 * Check for degenerate name (e.g. / or "")
408 	 * which is a way of talking about a directory,
409 	 * e.g. like "/." or ".".
410 	 */
411 	if (cnp->cn_nameptr[0] == '\0') {
412 		if (dp->v_type != VDIR) {
413 			error = ENOTDIR;
414 			goto bad;
415 		}
416 		if (cnp->cn_nameiop != LOOKUP) {
417 			error = EISDIR;
418 			goto bad;
419 		}
420 		if (wantparent) {
421 			ndp->ni_dvp = dp;
422 			VREF(dp);
423 		}
424 		ndp->ni_vp = dp;
425 		if (!(cnp->cn_flags & (LOCKPARENT | LOCKLEAF)))
426 			VOP_UNLOCK(dp, 0, td);
427 		/* XXX This should probably move to the top of function. */
428 		if (cnp->cn_flags & SAVESTART)
429 			panic("lookup: SAVESTART");
430 		return (0);
431 	}
432 
433 	/*
434 	 * Handle "..": two special cases.
435 	 * 1. If at root directory (e.g. after chroot)
436 	 *    or at absolute root directory
437 	 *    then ignore it so can't get out.
438 	 * 2. If this vnode is the root of a mounted
439 	 *    filesystem, then replace it with the
440 	 *    vnode which was mounted on so we take the
441 	 *    .. in the other filesystem.
442 	 * 3. If the vnode is the top directory of
443 	 *    the jail or chroot, don't let them out.
444 	 */
445 	if (cnp->cn_flags & ISDOTDOT) {
446 		for (;;) {
447 			if (dp == ndp->ni_rootdir ||
448 			    dp == ndp->ni_topdir ||
449 			    dp == rootvnode) {
450 				ndp->ni_dvp = dp;
451 				ndp->ni_vp = dp;
452 				VREF(dp);
453 				goto nextname;
454 			}
455 			if ((dp->v_vflag & VV_ROOT) == 0 ||
456 			    (cnp->cn_flags & NOCROSSMOUNT))
457 				break;
458 			if (dp->v_mount == NULL) {	/* forced unmount */
459 				error = EBADF;
460 				goto bad;
461 			}
462 			tdp = dp;
463 			dp = dp->v_mount->mnt_vnodecovered;
464 			vput(tdp);
465 			VREF(dp);
466 			vn_lock(dp, LK_EXCLUSIVE | LK_RETRY, td);
467 		}
468 	}
469 
470 	/*
471 	 * We now have a segment name to search for, and a directory to search.
472 	 */
473 unionlookup:
474 #ifdef MAC
475 	if ((cnp->cn_flags & NOMACCHECK) == 0) {
476 		error = mac_check_vnode_lookup(td->td_ucred, dp, cnp);
477 		if (error)
478 			goto bad;
479 	}
480 #endif
481 	ndp->ni_dvp = dp;
482 	ndp->ni_vp = NULL;
483 	cnp->cn_flags &= ~PDIRUNLOCK;
484 	ASSERT_VOP_LOCKED(dp, "lookup");
485 	if ((error = VOP_LOOKUP(dp, &ndp->ni_vp, cnp)) != 0) {
486 		KASSERT(ndp->ni_vp == NULL, ("leaf should be empty"));
487 #ifdef NAMEI_DIAGNOSTIC
488 		printf("not found\n");
489 #endif
490 		if ((error == ENOENT) &&
491 		    (dp->v_vflag & VV_ROOT) && (dp->v_mount != NULL) &&
492 		    (dp->v_mount->mnt_flag & MNT_UNION)) {
493 			tdp = dp;
494 			dp = dp->v_mount->mnt_vnodecovered;
495 			if (cnp->cn_flags & PDIRUNLOCK)
496 				vrele(tdp);
497 			else
498 				vput(tdp);
499 			VREF(dp);
500 			vn_lock(dp, LK_EXCLUSIVE | LK_RETRY, td);
501 			goto unionlookup;
502 		}
503 
504 		if (error != EJUSTRETURN)
505 			goto bad;
506 		/*
507 		 * If creating and at end of pathname, then can consider
508 		 * allowing file to be created.
509 		 */
510 		if (rdonly) {
511 			error = EROFS;
512 			goto bad;
513 		}
514 		if (*cp == '\0' && trailing_slash &&
515 		     !(cnp->cn_flags & WILLBEDIR)) {
516 			error = ENOENT;
517 			goto bad;
518 		}
519 		/*
520 		 * We return with ni_vp NULL to indicate that the entry
521 		 * doesn't currently exist, leaving a pointer to the
522 		 * (possibly locked) directory inode in ndp->ni_dvp.
523 		 */
524 		if (cnp->cn_flags & SAVESTART) {
525 			ndp->ni_startdir = ndp->ni_dvp;
526 			VREF(ndp->ni_startdir);
527 		}
528 		return (0);
529 	}
530 #ifdef NAMEI_DIAGNOSTIC
531 	printf("found\n");
532 #endif
533 
534 	ASSERT_VOP_LOCKED(ndp->ni_vp, "lookup");
535 
536 	/*
537 	 * Take into account any additional components consumed by
538 	 * the underlying filesystem.
539 	 */
540 	if (cnp->cn_consume > 0) {
541 		cnp->cn_nameptr += cnp->cn_consume;
542 		ndp->ni_next += cnp->cn_consume;
543 		ndp->ni_pathlen -= cnp->cn_consume;
544 		cnp->cn_consume = 0;
545 	}
546 
547 	dp = ndp->ni_vp;
548 
549 	/*
550 	 * Check to see if the vnode has been mounted on;
551 	 * if so find the root of the mounted filesystem.
552 	 */
553 	while (dp->v_type == VDIR && (mp = dp->v_mountedhere) &&
554 	       (cnp->cn_flags & NOCROSSMOUNT) == 0) {
555 		if (vfs_busy(mp, 0, 0, td))
556 			continue;
557 		VOP_UNLOCK(dp, 0, td);
558 		error = VFS_ROOT(mp, &tdp, td);
559 		vfs_unbusy(mp, td);
560 		if (error) {
561 			dpunlocked = 1;
562 			goto bad2;
563 		}
564 		vrele(dp);
565 		ndp->ni_vp = dp = tdp;
566 	}
567 
568 	/*
569 	 * Check for symbolic link
570 	 */
571 	if ((dp->v_type == VLNK) &&
572 	    ((cnp->cn_flags & FOLLOW) || trailing_slash ||
573 	     *ndp->ni_next == '/')) {
574 		cnp->cn_flags |= ISSYMLINK;
575 		if (dp->v_mount == NULL) {
576 			/* We can't know whether the directory was mounted with
577 			 * NOSYMFOLLOW, so we can't follow safely. */
578 			error = EBADF;
579 			goto bad2;
580 		}
581 		if (dp->v_mount->mnt_flag & MNT_NOSYMFOLLOW) {
582 			error = EACCES;
583 			goto bad2;
584 		}
585 		return (0);
586 	}
587 
588 	/*
589 	 * Check for bogus trailing slashes.
590 	 */
591 	if (trailing_slash && dp->v_type != VDIR) {
592 		error = ENOTDIR;
593 		goto bad2;
594 	}
595 
596 nextname:
597 	/*
598 	 * Not a symbolic link.  If more pathname,
599 	 * continue at next component, else return.
600 	 */
601 	if (*ndp->ni_next == '/') {
602 		cnp->cn_nameptr = ndp->ni_next;
603 		while (*cnp->cn_nameptr == '/') {
604 			cnp->cn_nameptr++;
605 			ndp->ni_pathlen--;
606 		}
607 		if (ndp->ni_dvp != ndp->ni_vp)
608 			ASSERT_VOP_UNLOCKED(ndp->ni_dvp, "lookup");
609 		vrele(ndp->ni_dvp);
610 		goto dirloop;
611 	}
612 	/*
613 	 * Disallow directory write attempts on read-only filesystems.
614 	 */
615 	if (rdonly &&
616 	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
617 		error = EROFS;
618 		goto bad2;
619 	}
620 	if (cnp->cn_flags & SAVESTART) {
621 		ndp->ni_startdir = ndp->ni_dvp;
622 		VREF(ndp->ni_startdir);
623 	}
624 	if (!wantparent)
625 		vrele(ndp->ni_dvp);
626 
627 	if ((cnp->cn_flags & LOCKLEAF) == 0)
628 		VOP_UNLOCK(dp, 0, td);
629 	return (0);
630 
631 bad2:
632 	if ((cnp->cn_flags & (LOCKPARENT | PDIRUNLOCK)) == LOCKPARENT &&
633 	    *ndp->ni_next == '\0')
634 		VOP_UNLOCK(ndp->ni_dvp, 0, td);
635 	vrele(ndp->ni_dvp);
636 bad:
637 	if (dpunlocked)
638 		vrele(dp);
639 	else
640 		vput(dp);
641 	ndp->ni_vp = NULL;
642 	return (error);
643 }
644 
645 /*
646  * relookup - lookup a path name component
647  *    Used by lookup to re-aquire things.
648  */
649 int
650 relookup(dvp, vpp, cnp)
651 	struct vnode *dvp, **vpp;
652 	struct componentname *cnp;
653 {
654 	struct thread *td = cnp->cn_thread;
655 	struct vnode *dp = 0;		/* the directory we are searching */
656 	int docache;			/* == 0 do not cache last component */
657 	int wantparent;			/* 1 => wantparent or lockparent flag */
658 	int rdonly;			/* lookup read-only flag bit */
659 	int error = 0;
660 #ifdef NAMEI_DIAGNOSTIC
661 	int newhash;			/* DEBUG: check name hash */
662 	char *cp;			/* DEBUG: check name ptr/len */
663 #endif
664 
665 	/*
666 	 * Setup: break out flag bits into variables.
667 	 */
668 	wantparent = cnp->cn_flags & (LOCKPARENT|WANTPARENT);
669 	docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE;
670 	if (cnp->cn_nameiop == DELETE ||
671 	    (wantparent && cnp->cn_nameiop != CREATE))
672 		docache = 0;
673 	rdonly = cnp->cn_flags & RDONLY;
674 	cnp->cn_flags &= ~ISSYMLINK;
675 	dp = dvp;
676 	vn_lock(dp, LK_EXCLUSIVE | LK_RETRY, td);
677 
678 /* dirloop: */
679 	/*
680 	 * Search a new directory.
681 	 *
682 	 * The last component of the filename is left accessible via
683 	 * cnp->cn_nameptr for callers that need the name. Callers needing
684 	 * the name set the SAVENAME flag. When done, they assume
685 	 * responsibility for freeing the pathname buffer.
686 	 */
687 #ifdef NAMEI_DIAGNOSTIC
688 	if (cnp->cn_namelen != cp - cnp->cn_nameptr)
689 		panic ("relookup: bad len");
690 	if (*cp != 0)
691 		panic("relookup: not last component");
692 	printf("{%s}: ", cnp->cn_nameptr);
693 #endif
694 
695 	/*
696 	 * Check for degenerate name (e.g. / or "")
697 	 * which is a way of talking about a directory,
698 	 * e.g. like "/." or ".".
699 	 */
700 	if (cnp->cn_nameptr[0] == '\0') {
701 		if (cnp->cn_nameiop != LOOKUP || wantparent) {
702 			error = EISDIR;
703 			goto bad;
704 		}
705 		if (dp->v_type != VDIR) {
706 			error = ENOTDIR;
707 			goto bad;
708 		}
709 		if (!(cnp->cn_flags & LOCKLEAF))
710 			VOP_UNLOCK(dp, 0, td);
711 		*vpp = dp;
712 		/* XXX This should probably move to the top of function. */
713 		if (cnp->cn_flags & SAVESTART)
714 			panic("lookup: SAVESTART");
715 		return (0);
716 	}
717 
718 	if (cnp->cn_flags & ISDOTDOT)
719 		panic ("relookup: lookup on dot-dot");
720 
721 	/*
722 	 * We now have a segment name to search for, and a directory to search.
723 	 */
724 	if ((error = VOP_LOOKUP(dp, vpp, cnp)) != 0) {
725 		KASSERT(*vpp == NULL, ("leaf should be empty"));
726 		if (error != EJUSTRETURN)
727 			goto bad;
728 		/*
729 		 * If creating and at end of pathname, then can consider
730 		 * allowing file to be created.
731 		 */
732 		if (rdonly) {
733 			error = EROFS;
734 			goto bad;
735 		}
736 		/* ASSERT(dvp == ndp->ni_startdir) */
737 		if (cnp->cn_flags & SAVESTART)
738 			VREF(dvp);
739 		/*
740 		 * We return with ni_vp NULL to indicate that the entry
741 		 * doesn't currently exist, leaving a pointer to the
742 		 * (possibly locked) directory inode in ndp->ni_dvp.
743 		 */
744 		return (0);
745 	}
746 	dp = *vpp;
747 
748 	/*
749 	 * Check for symbolic link
750 	 */
751 	KASSERT(dp->v_type != VLNK || !(cnp->cn_flags & FOLLOW),
752 	    ("relookup: symlink found.\n"));
753 
754 	/*
755 	 * Disallow directory write attempts on read-only filesystems.
756 	 */
757 	if (rdonly &&
758 	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
759 		error = EROFS;
760 		goto bad2;
761 	}
762 	/* ASSERT(dvp == ndp->ni_startdir) */
763 	if (cnp->cn_flags & SAVESTART)
764 		VREF(dvp);
765 
766 	if (!wantparent)
767 		vrele(dvp);
768 
769 	if (vn_canvmio(dp) == TRUE &&
770 		((cnp->cn_flags & (NOOBJ|LOCKLEAF)) == LOCKLEAF))
771 		vfs_object_create(dp, td, cnp->cn_cred);
772 
773 	if ((cnp->cn_flags & LOCKLEAF) == 0)
774 		VOP_UNLOCK(dp, 0, td);
775 	return (0);
776 
777 bad2:
778 	if ((cnp->cn_flags & LOCKPARENT) && (cnp->cn_flags & ISLASTCN))
779 		VOP_UNLOCK(dvp, 0, td);
780 	vrele(dvp);
781 bad:
782 	vput(dp);
783 	*vpp = NULL;
784 	return (error);
785 }
786