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