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