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