vfs_lookup.c revision 22521
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 * $FreeBSD: head/sys/kern/vfs_lookup.c 22521 1997-02-10 02:22:35Z dyson $
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 */
80int
81namei(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 */
259int
260lookup(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		docache = 0;
283	rdonly = cnp->cn_flags & RDONLY;
284	ndp->ni_dvp = NULL;
285	cnp->cn_flags &= ~ISSYMLINK;
286	dp = ndp->ni_startdir;
287	ndp->ni_startdir = NULLVP;
288	vn_lock(dp, LK_EXCLUSIVE | LK_RETRY, p);
289
290dirloop:
291	/*
292	 * Search a new directory.
293	 *
294	 * The cn_hash value is for use by vfs_cache.
295	 * The last component of the filename is left accessible via
296	 * cnp->cn_nameptr for callers that need the name. Callers needing
297	 * the name set the SAVENAME flag. When done, they assume
298	 * responsibility for freeing the pathname buffer.
299	 */
300	cnp->cn_consume = 0;
301	cnp->cn_hash = 0;
302	for (cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++)
303		cnp->cn_hash += (unsigned char)*cp;
304	cnp->cn_namelen = cp - cnp->cn_nameptr;
305	if (cnp->cn_namelen > NAME_MAX) {
306		error = ENAMETOOLONG;
307		goto bad;
308	}
309#ifdef NAMEI_DIAGNOSTIC
310	{ char c = *cp;
311	*cp = '\0';
312	printf("{%s}: ", cnp->cn_nameptr);
313	*cp = c; }
314#endif
315	ndp->ni_pathlen -= cnp->cn_namelen;
316	ndp->ni_next = cp;
317
318	/*
319	 * Replace multiple slashes by a single slash and trailing slashes
320	 * by a null.  This must be done before VOP_LOOKUP() because some
321	 * fs's don't know about trailing slashes.  Remember if there were
322	 * trailing slashes to handle symlinks, existing non-directories
323	 * and non-existing files that won't be directories specially later.
324	 */
325	trailing_slash = 0;
326	while (*cp == '/' && (cp[1] == '/' || cp[1] == '\0')) {
327		cp++;
328		ndp->ni_pathlen--;
329		if (*cp == '\0') {
330			trailing_slash = 1;
331			*ndp->ni_next = '\0';	/* XXX for direnter() ... */
332		}
333	}
334	ndp->ni_next = cp;
335
336	cnp->cn_flags |= MAKEENTRY;
337	if (*cp == '\0' && docache == 0)
338		cnp->cn_flags &= ~MAKEENTRY;
339	if (cnp->cn_namelen == 2 &&
340	    cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.')
341		cnp->cn_flags |= ISDOTDOT;
342	else
343		cnp->cn_flags &= ~ISDOTDOT;
344	if (*ndp->ni_next == 0)
345		cnp->cn_flags |= ISLASTCN;
346	else
347		cnp->cn_flags &= ~ISLASTCN;
348
349
350	/*
351	 * Check for degenerate name (e.g. / or "")
352	 * which is a way of talking about a directory,
353	 * e.g. like "/." or ".".
354	 */
355	if (cnp->cn_nameptr[0] == '\0') {
356		if (dp->v_type != VDIR) {
357			error = ENOTDIR;
358			goto bad;
359		}
360		if (cnp->cn_nameiop != LOOKUP) {
361			error = EISDIR;
362			goto bad;
363		}
364		if (wantparent) {
365			ndp->ni_dvp = dp;
366			VREF(dp);
367		}
368		ndp->ni_vp = dp;
369		if (!(cnp->cn_flags & (LOCKPARENT | LOCKLEAF)))
370			VOP_UNLOCK(dp, 0, p);
371		if (cnp->cn_flags & SAVESTART)
372			panic("lookup: SAVESTART");
373		return (0);
374	}
375
376	/*
377	 * Handle "..": two special cases.
378	 * 1. If at root directory (e.g. after chroot)
379	 *    or at absolute root directory
380	 *    then ignore it so can't get out.
381	 * 2. If this vnode is the root of a mounted
382	 *    filesystem, then replace it with the
383	 *    vnode which was mounted on so we take the
384	 *    .. in the other file system.
385	 */
386	if (cnp->cn_flags & ISDOTDOT) {
387		for (;;) {
388			if (dp == ndp->ni_rootdir || dp == rootvnode) {
389				ndp->ni_dvp = dp;
390				ndp->ni_vp = dp;
391				VREF(dp);
392				goto nextname;
393			}
394			if ((dp->v_flag & VROOT) == 0 ||
395			    (cnp->cn_flags & NOCROSSMOUNT))
396				break;
397			tdp = dp;
398			dp = dp->v_mount->mnt_vnodecovered;
399			vput(tdp);
400			VREF(dp);
401			vn_lock(dp, LK_EXCLUSIVE | LK_RETRY, p);
402		}
403	}
404
405	/*
406	 * We now have a segment name to search for, and a directory to search.
407	 */
408unionlookup:
409	ndp->ni_dvp = dp;
410	ndp->ni_vp = NULL;
411	if (error = VOP_LOOKUP(dp, &ndp->ni_vp, cnp)) {
412#ifdef DIAGNOSTIC
413		if (ndp->ni_vp != NULL)
414			panic("leaf should be empty");
415#endif
416#ifdef NAMEI_DIAGNOSTIC
417		printf("not found\n");
418#endif
419		if ((error == ENOENT) &&
420		    (dp->v_flag & VROOT) &&
421		    (dp->v_mount->mnt_flag & MNT_UNION)) {
422			tdp = dp;
423			dp = dp->v_mount->mnt_vnodecovered;
424			vput(tdp);
425			VREF(dp);
426			vn_lock(dp, LK_EXCLUSIVE | LK_RETRY, p);
427			goto unionlookup;
428		}
429
430		if (error != EJUSTRETURN)
431			goto bad;
432		/*
433		 * If creating and at end of pathname, then can consider
434		 * allowing file to be created.
435		 */
436		if (rdonly) {
437			error = EROFS;
438			goto bad;
439		}
440		if (*cp == '\0' && trailing_slash &&
441		     !(cnp->cn_flags & WILLBEDIR)) {
442			error = ENOENT;
443			goto bad;
444		}
445		/*
446		 * We return with ni_vp NULL to indicate that the entry
447		 * doesn't currently exist, leaving a pointer to the
448		 * (possibly locked) directory inode in ndp->ni_dvp.
449		 */
450		if (cnp->cn_flags & SAVESTART) {
451			ndp->ni_startdir = ndp->ni_dvp;
452			VREF(ndp->ni_startdir);
453		}
454		return (0);
455	}
456#ifdef NAMEI_DIAGNOSTIC
457	printf("found\n");
458#endif
459
460	/*
461	 * Take into account any additional components consumed by
462	 * the underlying filesystem.
463	 */
464	if (cnp->cn_consume > 0) {
465		cnp->cn_nameptr += cnp->cn_consume;
466		ndp->ni_next += cnp->cn_consume;
467		ndp->ni_pathlen -= cnp->cn_consume;
468		cnp->cn_consume = 0;
469	}
470
471	dp = ndp->ni_vp;
472
473	/*
474	 * Check to see if the vnode has been mounted on;
475	 * if so find the root of the mounted file system.
476	 */
477	while (dp->v_type == VDIR && (mp = dp->v_mountedhere) &&
478	       (cnp->cn_flags & NOCROSSMOUNT) == 0) {
479		if (vfs_busy(mp, 0, 0, p))
480			continue;
481		error = VFS_ROOT(mp, &tdp);
482		vfs_unbusy(mp, p);
483		if (error)
484			goto bad2;
485		vput(dp);
486		ndp->ni_vp = dp = tdp;
487	}
488
489	/*
490	 * Check for symbolic link
491	 */
492	if ((dp->v_type == VLNK) &&
493	    ((cnp->cn_flags & FOLLOW) || trailing_slash ||
494	     *ndp->ni_next == '/')) {
495		cnp->cn_flags |= ISSYMLINK;
496		return (0);
497	}
498
499	/*
500	 * Check for bogus trailing slashes.
501	 */
502	if (trailing_slash && dp->v_type != VDIR) {
503		error = ENOTDIR;
504		goto bad2;
505	}
506
507nextname:
508	/*
509	 * Not a symbolic link.  If more pathname,
510	 * continue at next component, else return.
511	 */
512	if (*ndp->ni_next == '/') {
513		cnp->cn_nameptr = ndp->ni_next;
514		while (*cnp->cn_nameptr == '/') {
515			cnp->cn_nameptr++;
516			ndp->ni_pathlen--;
517		}
518		vrele(ndp->ni_dvp);
519		goto dirloop;
520	}
521	/*
522	 * Disallow directory write attempts on read-only file systems.
523	 */
524	if (rdonly &&
525	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
526		error = EROFS;
527		goto bad2;
528	}
529	if (cnp->cn_flags & SAVESTART) {
530		ndp->ni_startdir = ndp->ni_dvp;
531		VREF(ndp->ni_startdir);
532	}
533	if (!wantparent)
534		vrele(ndp->ni_dvp);
535	if ((cnp->cn_flags & LOCKLEAF) == 0)
536		VOP_UNLOCK(dp, 0, p);
537	return (0);
538
539bad2:
540	if ((cnp->cn_flags & LOCKPARENT) && *ndp->ni_next == '\0')
541		VOP_UNLOCK(ndp->ni_dvp, 0, p);
542	vrele(ndp->ni_dvp);
543bad:
544	vput(dp);
545	ndp->ni_vp = NULL;
546	return (error);
547}
548
549/*
550 * relookup - lookup a path name component
551 *    Used by lookup to re-aquire things.
552 */
553int
554relookup(dvp, vpp, cnp)
555	struct vnode *dvp, **vpp;
556	struct componentname *cnp;
557{
558	struct proc *p = cnp->cn_proc;
559	struct vnode *dp = 0;		/* the directory we are searching */
560	int docache;			/* == 0 do not cache last component */
561	int wantparent;			/* 1 => wantparent or lockparent flag */
562	int rdonly;			/* lookup read-only flag bit */
563	int error = 0;
564#ifdef NAMEI_DIAGNOSTIC
565	int newhash;			/* DEBUG: check name hash */
566	char *cp;			/* DEBUG: check name ptr/len */
567#endif
568
569	/*
570	 * Setup: break out flag bits into variables.
571	 */
572	wantparent = cnp->cn_flags & (LOCKPARENT|WANTPARENT);
573	docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE;
574	if (cnp->cn_nameiop == DELETE ||
575	    (wantparent && cnp->cn_nameiop != CREATE))
576		docache = 0;
577	rdonly = cnp->cn_flags & RDONLY;
578	cnp->cn_flags &= ~ISSYMLINK;
579	dp = dvp;
580	vn_lock(dp, LK_EXCLUSIVE | LK_RETRY, p);
581
582/* dirloop: */
583	/*
584	 * Search a new directory.
585	 *
586	 * The cn_hash value is for use by vfs_cache.
587	 * The last component of the filename is left accessible via
588	 * cnp->cn_nameptr for callers that need the name. Callers needing
589	 * the name set the SAVENAME flag. When done, they assume
590	 * responsibility for freeing the pathname buffer.
591	 */
592#ifdef NAMEI_DIAGNOSTIC
593	for (newhash = 0, cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++)
594		newhash += (unsigned char)*cp;
595	if (newhash != cnp->cn_hash)
596		panic("relookup: bad hash");
597	if (cnp->cn_namelen != cp - cnp->cn_nameptr)
598		panic ("relookup: bad len");
599	if (*cp != 0)
600		panic("relookup: not last component");
601	printf("{%s}: ", cnp->cn_nameptr);
602#endif
603
604	/*
605	 * Check for degenerate name (e.g. / or "")
606	 * which is a way of talking about a directory,
607	 * e.g. like "/." or ".".
608	 */
609	if (cnp->cn_nameptr[0] == '\0') {
610		if (cnp->cn_nameiop != LOOKUP || wantparent) {
611			error = EISDIR;
612			goto bad;
613		}
614		if (dp->v_type != VDIR) {
615			error = ENOTDIR;
616			goto bad;
617		}
618		if (!(cnp->cn_flags & LOCKLEAF))
619			VOP_UNLOCK(dp, 0, p);
620		*vpp = dp;
621		if (cnp->cn_flags & SAVESTART)
622			panic("lookup: SAVESTART");
623		return (0);
624	}
625
626	if (cnp->cn_flags & ISDOTDOT)
627		panic ("relookup: lookup on dot-dot");
628
629	/*
630	 * We now have a segment name to search for, and a directory to search.
631	 */
632	if (error = VOP_LOOKUP(dp, vpp, cnp)) {
633#ifdef DIAGNOSTIC
634		if (*vpp != NULL)
635			panic("leaf should be empty");
636#endif
637		if (error != EJUSTRETURN)
638			goto bad;
639		/*
640		 * If creating and at end of pathname, then can consider
641		 * allowing file to be created.
642		 */
643		if (rdonly) {
644			error = EROFS;
645			goto bad;
646		}
647		/* ASSERT(dvp == ndp->ni_startdir) */
648		if (cnp->cn_flags & SAVESTART)
649			VREF(dvp);
650		/*
651		 * We return with ni_vp NULL to indicate that the entry
652		 * doesn't currently exist, leaving a pointer to the
653		 * (possibly locked) directory inode in ndp->ni_dvp.
654		 */
655		return (0);
656	}
657	dp = *vpp;
658
659#ifdef DIAGNOSTIC
660	/*
661	 * Check for symbolic link
662	 */
663	if (dp->v_type == VLNK && (cnp->cn_flags & FOLLOW))
664		panic ("relookup: symlink found.\n");
665#endif
666
667	/*
668	 * Disallow directory write attempts on read-only file systems.
669	 */
670	if (rdonly &&
671	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
672		error = EROFS;
673		goto bad2;
674	}
675	/* ASSERT(dvp == ndp->ni_startdir) */
676	if (cnp->cn_flags & SAVESTART)
677		VREF(dvp);
678
679	if (!wantparent)
680		vrele(dvp);
681	if ((cnp->cn_flags & LOCKLEAF) == 0)
682		VOP_UNLOCK(dp, 0, p);
683	return (0);
684
685bad2:
686	if ((cnp->cn_flags & LOCKPARENT) && (cnp->cn_flags & ISLASTCN))
687		VOP_UNLOCK(dvp, 0, p);
688	vrele(dvp);
689bad:
690	vput(dp);
691	*vpp = NULL;
692	return (error);
693}
694