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