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