xref: /netbsd-src/sys/kern/vfs_lookup.c (revision 95d875fb90b1458e4f1de6950286ddcd6644bc61)
1 /*	$NetBSD: vfs_lookup.c,v 1.32 1999/08/03 18:17:24 wrstuden Exp $	*/
2 
3 /*
4  * Copyright (c) 1982, 1986, 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  * (c) UNIX System Laboratories, Inc.
7  * All or some portions of this file are derived from material licensed
8  * to the University of California by American Telephone and Telegraph
9  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10  * the permission of UNIX System Laboratories, Inc.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *	This product includes software developed by the University of
23  *	California, Berkeley and its contributors.
24  * 4. Neither the name of the University nor the names of its contributors
25  *    may be used to endorse or promote products derived from this software
26  *    without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38  * SUCH DAMAGE.
39  *
40  *	@(#)vfs_lookup.c	8.10 (Berkeley) 5/27/95
41  */
42 
43 #include "opt_ktrace.h"
44 
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/syslimits.h>
48 #include <sys/time.h>
49 #include <sys/namei.h>
50 #include <sys/vnode.h>
51 #include <sys/mount.h>
52 #include <sys/errno.h>
53 #include <sys/malloc.h>
54 #include <sys/filedesc.h>
55 #include <sys/proc.h>
56 
57 #ifdef KTRACE
58 #include <sys/ktrace.h>
59 #endif
60 
61 /*
62  * Convert a pathname into a pointer to a locked inode.
63  *
64  * The FOLLOW flag is set when symbolic links are to be followed
65  * when they occur at the end of the name translation process.
66  * Symbolic links are always followed for all other pathname
67  * components other than the last.
68  *
69  * The segflg defines whether the name is to be copied from user
70  * space or kernel space.
71  *
72  * Overall outline of namei:
73  *
74  *	copy in name
75  *	get starting directory
76  *	while (!done && !error) {
77  *		call lookup to search path.
78  *		if symbolic link, massage name in buffer and continue
79  *	}
80  */
81 int
82 namei(ndp)
83 	register struct nameidata *ndp;
84 {
85 	struct cwdinfo *cwdi;		/* pointer to cwd state */
86 	register char *cp;		/* pointer into pathname argument */
87 	register struct vnode *dp;	/* the directory we are searching */
88 	struct iovec aiov;		/* uio for reading symbolic links */
89 	struct uio auio;
90 	int error, linklen;
91 	struct componentname *cnp = &ndp->ni_cnd;
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 	cwdi = cnp->cn_proc->p_cwdi;
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, &ndp->ni_pathlen);
113 	else
114 		error = copyinstr(ndp->ni_dirp, cnp->cn_pnbuf,
115 			    MAXPATHLEN, &ndp->ni_pathlen);
116 
117 	/*
118 	 * POSIX.1 requirement: "" is not a valid file name.
119 	 */
120 	if (!error && ndp->ni_pathlen == 1)
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 
130 #ifdef KTRACE
131 	if (KTRPOINT(cnp->cn_proc, KTR_NAMEI))
132 		ktrnamei(cnp->cn_proc->p_tracep, cnp->cn_pnbuf);
133 #endif
134 
135 	/*
136 	 * Get starting point for the translation.
137 	 */
138 	if ((ndp->ni_rootdir = cwdi->cwdi_rdir) == NULL)
139 		ndp->ni_rootdir = rootvnode;
140 	/*
141 	 * Check if starting from root directory or current directory.
142 	 */
143 	if (cnp->cn_pnbuf[0] == '/') {
144 		dp = ndp->ni_rootdir;
145 		VREF(dp);
146 	} else {
147 		dp = cwdi->cwdi_cdir;
148 		VREF(dp);
149 	}
150 	for (;;) {
151 		cnp->cn_nameptr = cnp->cn_pnbuf;
152 		ndp->ni_startdir = dp;
153 		if ((error = lookup(ndp)) != 0) {
154 			FREE(cnp->cn_pnbuf, M_NAMEI);
155 			return (error);
156 		}
157 		/*
158 		 * Check for symbolic link
159 		 */
160 		if ((cnp->cn_flags & ISSYMLINK) == 0) {
161 			if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0)
162 				FREE(cnp->cn_pnbuf, M_NAMEI);
163 			else
164 				cnp->cn_flags |= HASBUF;
165 			return (0);
166 		}
167 		if ((cnp->cn_flags & LOCKPARENT) && (cnp->cn_flags & ISLASTCN))
168 			VOP_UNLOCK(ndp->ni_dvp, 0);
169 		if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
170 			error = ELOOP;
171 			break;
172 		}
173 		if (ndp->ni_vp->v_mount->mnt_flag & MNT_SYMPERM) {
174 			error = VOP_ACCESS(ndp->ni_vp, VEXEC, cnp->cn_cred,
175 			    cnp->cn_proc);
176 			if (error != 0)
177 				break;
178 		}
179 		if (ndp->ni_pathlen > 1)
180 			MALLOC(cp, char *, MAXPATHLEN, M_NAMEI, M_WAITOK);
181 		else
182 			cp = cnp->cn_pnbuf;
183 		aiov.iov_base = cp;
184 		aiov.iov_len = MAXPATHLEN;
185 		auio.uio_iov = &aiov;
186 		auio.uio_iovcnt = 1;
187 		auio.uio_offset = 0;
188 		auio.uio_rw = UIO_READ;
189 		auio.uio_segflg = UIO_SYSSPACE;
190 		auio.uio_procp = (struct proc *)0;
191 		auio.uio_resid = MAXPATHLEN;
192 		error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
193 		if (error) {
194 		badlink:
195 			if (ndp->ni_pathlen > 1)
196 				FREE(cp, M_NAMEI);
197 			break;
198 		}
199 		linklen = MAXPATHLEN - auio.uio_resid;
200 		if (linklen == 0) {
201 			error = ENOENT;
202 			goto badlink;
203 		}
204 		if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
205 			error = ENAMETOOLONG;
206 			goto badlink;
207 		}
208 		if (ndp->ni_pathlen > 1) {
209 			memcpy(cp + linklen, ndp->ni_next, ndp->ni_pathlen);
210 			FREE(cnp->cn_pnbuf, M_NAMEI);
211 			cnp->cn_pnbuf = cp;
212 		} else
213 			cnp->cn_pnbuf[linklen] = '\0';
214 		ndp->ni_pathlen += linklen;
215 		vput(ndp->ni_vp);
216 		dp = ndp->ni_dvp;
217 		/*
218 		 * Check if root directory should replace current directory.
219 		 */
220 		if (cnp->cn_pnbuf[0] == '/') {
221 			vrele(dp);
222 			dp = ndp->ni_rootdir;
223 			VREF(dp);
224 		}
225 	}
226 	FREE(cnp->cn_pnbuf, M_NAMEI);
227 	vrele(ndp->ni_dvp);
228 	vput(ndp->ni_vp);
229 	ndp->ni_vp = NULL;
230 	return (error);
231 }
232 
233 /*
234  * Search a pathname.
235  * This is a very central and rather complicated routine.
236  *
237  * The pathname is pointed to by ni_ptr and is of length ni_pathlen.
238  * The starting directory is taken from ni_startdir. The pathname is
239  * descended until done, or a symbolic link is encountered. The variable
240  * ni_more is clear if the path is completed; it is set to one if a
241  * symbolic link needing interpretation is encountered.
242  *
243  * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
244  * whether the name is to be looked up, created, renamed, or deleted.
245  * When CREATE, RENAME, or DELETE is specified, information usable in
246  * creating, renaming, or deleting a directory entry may be calculated.
247  * If flag has LOCKPARENT or'ed into it, the parent directory is returned
248  * locked. If flag has WANTPARENT or'ed into it, the parent directory is
249  * returned unlocked. Otherwise the parent directory is not returned. If
250  * the target of the pathname exists and LOCKLEAF is or'ed into the flag
251  * the target is returned locked, otherwise it is returned unlocked.
252  * When creating or renaming and LOCKPARENT is specified, the target may not
253  * be ".".  When deleting and LOCKPARENT is specified, the target may be ".".
254  *
255  * Overall outline of lookup:
256  *
257  * dirloop:
258  *	identify next component of name at ndp->ni_ptr
259  *	handle degenerate case where name is null string
260  *	if .. and crossing mount points and on mounted filesys, find parent
261  *	call VOP_LOOKUP routine for next component name
262  *	    directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set
263  *	    component vnode returned in ni_vp (if it exists), locked.
264  *	if result vnode is mounted on and crossing mount points,
265  *	    find mounted on vnode
266  *	if more components of name, do next level at dirloop
267  *	return the answer in ni_vp, locked if LOCKLEAF set
268  *	    if LOCKPARENT set, return locked parent in ni_dvp
269  *	    if WANTPARENT set, return unlocked parent in ni_dvp
270  */
271 int
272 lookup(ndp)
273 	register struct nameidata *ndp;
274 {
275 	register const char *cp;	/* pointer into pathname argument */
276 	register struct vnode *dp = 0;	/* the directory we are searching */
277 	struct vnode *tdp;		/* saved dp */
278 	struct mount *mp;		/* mount table entry */
279 	int docache;			/* == 0 do not cache last component */
280 	int wantparent;			/* 1 => wantparent or lockparent flag */
281 	int rdonly;			/* lookup read-only flag bit */
282 	int error = 0;
283 	int slashes;
284 	int dpunlocked = 0;		/* dp has already been unlocked */
285 	struct componentname *cnp = &ndp->ni_cnd;
286 
287 	/*
288 	 * Setup: break out flag bits into variables.
289 	 */
290 	wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT);
291 	docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE;
292 	if (cnp->cn_nameiop == DELETE ||
293 	    (wantparent && cnp->cn_nameiop != CREATE))
294 		docache = 0;
295 	rdonly = cnp->cn_flags & RDONLY;
296 	ndp->ni_dvp = NULL;
297 	cnp->cn_flags &= ~ISSYMLINK;
298 	dp = ndp->ni_startdir;
299 	ndp->ni_startdir = NULLVP;
300 	vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
301 
302 	/*
303 	 * If we have a leading string of slashes, remove them, and just make
304 	 * sure the current node is a directory.
305 	 */
306 	cp = cnp->cn_nameptr;
307 	if (*cp == '/') {
308 		do {
309 			cp++;
310 		} while (*cp == '/');
311 		ndp->ni_pathlen -= cp - cnp->cn_nameptr;
312 		cnp->cn_nameptr = cp;
313 
314 		if (dp->v_type != VDIR) {
315 			error = ENOTDIR;
316 			goto bad;
317 		}
318 
319 		/*
320 		 * If we've exhausted the path name, then just return the
321 		 * current node.  If the caller requested the parent node (i.e.
322 		 * it's a CREATE, DELETE, or RENAME), and we don't have one
323 		 * (because this is the root directory), then we must fail.
324 		 */
325 		if (cnp->cn_nameptr[0] == '\0') {
326 			if (ndp->ni_dvp == NULL && wantparent) {
327 				error = EISDIR;
328 				goto bad;
329 			}
330 			ndp->ni_vp = dp;
331 			cnp->cn_flags |= ISLASTCN;
332 			goto terminal;
333 		}
334 	}
335 
336 dirloop:
337 	/*
338 	 * Search a new directory.
339 	 *
340 	 * The cn_hash value is for use by vfs_cache.
341 	 * The last component of the filename is left accessible via
342 	 * cnp->cn_nameptr for callers that need the name. Callers needing
343 	 * the name set the SAVENAME flag. When done, they assume
344 	 * responsibility for freeing the pathname buffer.
345 	 */
346 	cnp->cn_consume = 0;
347 	cnp->cn_hash = 0;
348 	for (cp = cnp->cn_nameptr; *cp != '\0' && *cp != '/'; cp++)
349 		cnp->cn_hash += (unsigned char)*cp;
350 	cnp->cn_namelen = cp - cnp->cn_nameptr;
351 	if (cnp->cn_namelen > NAME_MAX) {
352 		error = ENAMETOOLONG;
353 		goto bad;
354 	}
355 #ifdef NAMEI_DIAGNOSTIC
356 	{ char c = *cp;
357 	*cp = '\0';
358 	printf("{%s}: ", cnp->cn_nameptr);
359 	*cp = c; }
360 #endif
361 	ndp->ni_pathlen -= cnp->cn_namelen;
362 	ndp->ni_next = cp;
363 	/*
364 	 * If this component is followed by a slash, then move the pointer to
365 	 * the next component forward, and remember that this component must be
366 	 * a directory.
367 	 */
368 	if (*cp == '/') {
369 		do {
370 			cp++;
371 		} while (*cp == '/');
372 		slashes = cp - ndp->ni_next;
373 		ndp->ni_pathlen -= slashes;
374 		ndp->ni_next = cp;
375 		cnp->cn_flags |= REQUIREDIR;
376 	} else {
377 		slashes = 0;
378 		cnp->cn_flags &= ~REQUIREDIR;
379 	}
380 	/*
381 	 * We do special processing on the last component, whether or not it's
382 	 * a directory.  Cache all intervening lookups, but not the final one.
383 	 */
384 	if (*cp == '\0') {
385 		if (docache)
386 			cnp->cn_flags |= MAKEENTRY;
387 		else
388 			cnp->cn_flags &= ~MAKEENTRY;
389 		cnp->cn_flags |= ISLASTCN;
390 	} else {
391 		cnp->cn_flags |= MAKEENTRY;
392 		cnp->cn_flags &= ~ISLASTCN;
393 	}
394 	if (cnp->cn_namelen == 2 &&
395 	    cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.')
396 		cnp->cn_flags |= ISDOTDOT;
397 	else
398 		cnp->cn_flags &= ~ISDOTDOT;
399 
400 	/*
401 	 * Handle "..": two special cases.
402 	 * 1. If at root directory (e.g. after chroot)
403 	 *    or at absolute root directory
404 	 *    then ignore it so can't get out.
405 	 * 2. If this vnode is the root of a mounted
406 	 *    filesystem, then replace it with the
407 	 *    vnode which was mounted on so we take the
408 	 *    .. in the other file system.
409 	 */
410 	if (cnp->cn_flags & ISDOTDOT) {
411 		for (;;) {
412 			if (dp == ndp->ni_rootdir || 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 			tdp = dp;
422 			dp = dp->v_mount->mnt_vnodecovered;
423 			vput(tdp);
424 			VREF(dp);
425 			vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
426 		}
427 	}
428 
429 	/*
430 	 * We now have a segment name to search for, and a directory to search.
431 	 */
432 unionlookup:
433 	ndp->ni_dvp = dp;
434 	ndp->ni_vp = NULL;
435 	cnp->cn_flags &= ~PDIRUNLOCK;
436 	if ((error = VOP_LOOKUP(dp, &ndp->ni_vp, cnp)) != 0) {
437 #ifdef DIAGNOSTIC
438 		if (ndp->ni_vp != NULL)
439 			panic("leaf should be empty");
440 #endif
441 #ifdef NAMEI_DIAGNOSTIC
442 		printf("not found\n");
443 #endif
444 		if ((error == ENOENT) &&
445 		    (dp->v_flag & VROOT) &&
446 		    (dp->v_mount->mnt_flag & MNT_UNION)) {
447 			tdp = dp;
448 			dp = dp->v_mount->mnt_vnodecovered;
449 			if (cnp->cn_flags & PDIRUNLOCK)
450 				vrele(tdp);
451 			else
452 				vput(tdp);
453 			VREF(dp);
454 			vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
455 			goto unionlookup;
456 		}
457 
458 		if (error != EJUSTRETURN)
459 			goto bad;
460 		/*
461 		 * If this was not the last component, or there were trailing
462 		 * slashes, then the name must exist.
463 		 */
464 		if (cnp->cn_flags & REQUIREDIR) {
465 			error = ENOENT;
466 			goto bad;
467 		}
468 		/*
469 		 * If creating and at end of pathname, then can consider
470 		 * allowing file to be created.
471 		 */
472 		if (rdonly) {
473 			error = EROFS;
474 			goto bad;
475 		}
476 		/*
477 		 * We return with ni_vp NULL to indicate that the entry
478 		 * doesn't currently exist, leaving a pointer to the
479 		 * (possibly locked) directory inode in ndp->ni_dvp.
480 		 */
481 		if (cnp->cn_flags & SAVESTART) {
482 			ndp->ni_startdir = ndp->ni_dvp;
483 			VREF(ndp->ni_startdir);
484 		}
485 		return (0);
486 	}
487 #ifdef NAMEI_DIAGNOSTIC
488 	printf("found\n");
489 #endif
490 
491 	/*
492 	 * Take into account any additional components consumed by the
493 	 * underlying filesystem.  This will include any trailing slashes after
494 	 * the last component consumed.
495 	 */
496 	if (cnp->cn_consume > 0) {
497 		ndp->ni_pathlen -= cnp->cn_consume - slashes;
498 		ndp->ni_next += cnp->cn_consume - slashes;
499 		cnp->cn_consume = 0;
500 		if (ndp->ni_next[0] == '\0')
501 			cnp->cn_flags |= ISLASTCN;
502 	}
503 
504 	dp = ndp->ni_vp;
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))
512 			continue;
513 		VOP_UNLOCK(dp, 0);
514 		error = VFS_ROOT(mp, &tdp);
515 		vfs_unbusy(mp);
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.  Back up over any slashes that we skipped,
526 	 * as we will need them again.
527 	 */
528 	if ((dp->v_type == VLNK) && (cnp->cn_flags & (FOLLOW|REQUIREDIR))) {
529 		ndp->ni_pathlen += slashes;
530 		ndp->ni_next -= slashes;
531 		cnp->cn_flags |= ISSYMLINK;
532 		return (0);
533 	}
534 
535 	/*
536 	 * Check for directory, if the component was followed by a series of
537 	 * slashes.
538 	 */
539 	if ((dp->v_type != VDIR) && (cnp->cn_flags & REQUIREDIR)) {
540 		error = ENOTDIR;
541 		goto bad2;
542 	}
543 
544 nextname:
545 	/*
546 	 * Not a symbolic link.  If this was not the last component, then
547 	 * continue at the next component, else return.
548 	 */
549 	if (!(cnp->cn_flags & ISLASTCN)) {
550 		cnp->cn_nameptr = ndp->ni_next;
551 		vrele(ndp->ni_dvp);
552 		goto dirloop;
553 	}
554 
555 terminal:
556 	/*
557 	 * Disallow directory write attempts on read-only file systems.
558 	 */
559 	if (rdonly &&
560 	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
561 		/*
562 		 * Disallow directory write attempts on read-only
563 		 * file systems.
564 		 */
565 		error = EROFS;
566 		goto bad2;
567 	}
568 	if (ndp->ni_dvp != NULL) {
569 		if (cnp->cn_flags & SAVESTART) {
570 			ndp->ni_startdir = ndp->ni_dvp;
571 			VREF(ndp->ni_startdir);
572 		}
573 		if (!wantparent)
574 			vrele(ndp->ni_dvp);
575 	}
576 	if ((cnp->cn_flags & LOCKLEAF) == 0)
577 		VOP_UNLOCK(dp, 0);
578 	return (0);
579 
580 bad2:
581 	if ((cnp->cn_flags & LOCKPARENT) && (cnp->cn_flags & ISLASTCN) &&
582 			((cnp->cn_flags & PDIRUNLOCK) == 0))
583 		VOP_UNLOCK(ndp->ni_dvp, 0);
584 	vrele(ndp->ni_dvp);
585 bad:
586 	if (dpunlocked)
587 		vrele(dp);
588 	else
589 		vput(dp);
590 	ndp->ni_vp = NULL;
591 	return (error);
592 }
593 
594 /*
595  * Reacquire a path name component.
596  */
597 int
598 relookup(dvp, vpp, cnp)
599 	struct vnode *dvp, **vpp;
600 	struct componentname *cnp;
601 {
602 	struct vnode *dp = 0;		/* the directory we are searching */
603 	int docache;			/* == 0 do not cache last component */
604 	int wantparent;			/* 1 => wantparent or lockparent flag */
605 	int rdonly;			/* lookup read-only flag bit */
606 	int error = 0;
607 #ifdef NAMEI_DIAGNOSTIC
608 	int newhash;			/* DEBUG: check name hash */
609 	char *cp;			/* DEBUG: check name ptr/len */
610 #endif
611 
612 	/*
613 	 * Setup: break out flag bits into variables.
614 	 */
615 	wantparent = cnp->cn_flags & (LOCKPARENT|WANTPARENT);
616 	docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE;
617 	if (cnp->cn_nameiop == DELETE ||
618 	    (wantparent && cnp->cn_nameiop != CREATE))
619 		docache = 0;
620 	rdonly = cnp->cn_flags & RDONLY;
621 	cnp->cn_flags &= ~ISSYMLINK;
622 	dp = dvp;
623 	vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
624 
625 /* dirloop: */
626 	/*
627 	 * Search a new directory.
628 	 *
629 	 * The cn_hash value is for use by vfs_cache.
630 	 * The last component of the filename is left accessible via
631 	 * cnp->cn_nameptr for callers that need the name. Callers needing
632 	 * the name set the SAVENAME flag. When done, they assume
633 	 * responsibility for freeing the pathname buffer.
634 	 */
635 #ifdef NAMEI_DIAGNOSTIC
636 	for (newhash = 0, cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++)
637 		newhash += (unsigned char)*cp;
638 	if (newhash != cnp->cn_hash)
639 		panic("relookup: bad hash");
640 	if (cnp->cn_namelen != cp - cnp->cn_nameptr)
641 		panic ("relookup: bad len");
642 	if (*cp != 0)
643 		panic("relookup: not last component");
644 	printf("{%s}: ", cnp->cn_nameptr);
645 #endif
646 
647 	/*
648 	 * Check for degenerate name (e.g. / or "")
649 	 * which is a way of talking about a directory,
650 	 * e.g. like "/." or ".".
651 	 */
652 	if (cnp->cn_nameptr[0] == '\0')
653 		panic("relookup: null name");
654 
655 	if (cnp->cn_flags & ISDOTDOT)
656 		panic ("relookup: lookup on dot-dot");
657 
658 	/*
659 	 * We now have a segment name to search for, and a directory to search.
660 	 */
661 	if ((error = VOP_LOOKUP(dp, vpp, cnp)) != 0) {
662 #ifdef DIAGNOSTIC
663 		if (*vpp != NULL)
664 			panic("leaf should be empty");
665 #endif
666 		if (error != EJUSTRETURN)
667 			goto bad;
668 		/*
669 		 * If creating and at end of pathname, then can consider
670 		 * allowing file to be created.
671 		 */
672 		if (rdonly) {
673 			error = EROFS;
674 			goto bad;
675 		}
676 		/* ASSERT(dvp == ndp->ni_startdir) */
677 		if (cnp->cn_flags & SAVESTART)
678 			VREF(dvp);
679 		/*
680 		 * We return with ni_vp NULL to indicate that the entry
681 		 * doesn't currently exist, leaving a pointer to the
682 		 * (possibly locked) directory inode in ndp->ni_dvp.
683 		 */
684 		return (0);
685 	}
686 	dp = *vpp;
687 
688 #ifdef DIAGNOSTIC
689 	/*
690 	 * Check for symbolic link
691 	 */
692 	if (dp->v_type == VLNK && (cnp->cn_flags & FOLLOW))
693 		panic ("relookup: symlink found.\n");
694 #endif
695 
696 	/*
697 	 * Check for read-only file systems.
698 	 */
699 	if (rdonly &&
700 	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
701 		error = EROFS;
702 		goto bad2;
703 	}
704 	/* ASSERT(dvp == ndp->ni_startdir) */
705 	if (cnp->cn_flags & SAVESTART)
706 		VREF(dvp);
707 	if (!wantparent)
708 		vrele(dvp);
709 	if ((cnp->cn_flags & LOCKLEAF) == 0)
710 		VOP_UNLOCK(dp, 0);
711 	return (0);
712 
713 bad2:
714 	if ((cnp->cn_flags & LOCKPARENT) && (cnp->cn_flags & ISLASTCN))
715 		VOP_UNLOCK(dvp, 0);
716 	vrele(dvp);
717 bad:
718 	vput(dp);
719 	*vpp = NULL;
720 	return (error);
721 }
722