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