vfs_lookup.c revision 32011
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.20 1997/09/21 04:23:01 dyson 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/filedesc.h>
50#include <sys/proc.h>
51
52#ifdef KTRACE
53#include <sys/ktrace.h>
54#endif
55
56#include <vm/vm_zone.h>
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 */
78int
79namei(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	struct proc *p = cnp->cn_proc;
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		cnp->cn_pnbuf = zalloc(namei_zone);
108	if (ndp->ni_segflg == UIO_SYSSPACE)
109		error = copystr(ndp->ni_dirp, cnp->cn_pnbuf,
110			    MAXPATHLEN, (u_int *)&ndp->ni_pathlen);
111	else
112		error = copyinstr(ndp->ni_dirp, cnp->cn_pnbuf,
113			    MAXPATHLEN, (u_int *)&ndp->ni_pathlen);
114
115	/*
116	 * Don't allow empty pathnames.
117	 */
118	if (!error && *cnp->cn_pnbuf == '\0')
119		error = ENOENT;
120
121	if (error) {
122		zfree(namei_zone, cnp->cn_pnbuf);
123		ndp->ni_vp = NULL;
124		return (error);
125	}
126	ndp->ni_loopcnt = 0;
127#ifdef KTRACE
128	if (KTRPOINT(cnp->cn_proc, KTR_NAMEI))
129		ktrnamei(cnp->cn_proc->p_tracep, cnp->cn_pnbuf);
130#endif
131
132	/*
133	 * Get starting point for the translation.
134	 */
135	if ((ndp->ni_rootdir = fdp->fd_rdir) == NULL)
136		ndp->ni_rootdir = rootvnode;
137	dp = fdp->fd_cdir;
138	VREF(dp);
139	for (;;) {
140		/*
141		 * Check if root directory should replace current directory.
142		 * Done at start of translation and after symbolic link.
143		 */
144		cnp->cn_nameptr = cnp->cn_pnbuf;
145		if (*(cnp->cn_nameptr) == '/') {
146			vrele(dp);
147			while (*(cnp->cn_nameptr) == '/') {
148				cnp->cn_nameptr++;
149				ndp->ni_pathlen--;
150			}
151			dp = ndp->ni_rootdir;
152			VREF(dp);
153		}
154		ndp->ni_startdir = dp;
155		error = lookup(ndp);
156		if (error) {
157			zfree(namei_zone, cnp->cn_pnbuf);
158			return (error);
159		}
160		/*
161		 * Check for symbolic link
162		 */
163		if ((cnp->cn_flags & ISSYMLINK) == 0) {
164			if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0)
165				zfree(namei_zone, cnp->cn_pnbuf);
166			else
167				cnp->cn_flags |= HASBUF;
168			return (0);
169		}
170		if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1)
171			VOP_UNLOCK(ndp->ni_dvp, 0, p);
172		if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
173			error = ELOOP;
174			break;
175		}
176		if (ndp->ni_pathlen > 1)
177			cp = zalloc(namei_zone);
178		else
179			cp = cnp->cn_pnbuf;
180		aiov.iov_base = cp;
181		aiov.iov_len = MAXPATHLEN;
182		auio.uio_iov = &aiov;
183		auio.uio_iovcnt = 1;
184		auio.uio_offset = 0;
185		auio.uio_rw = UIO_READ;
186		auio.uio_segflg = UIO_SYSSPACE;
187		auio.uio_procp = (struct proc *)0;
188		auio.uio_resid = MAXPATHLEN;
189		error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
190		if (error) {
191			if (ndp->ni_pathlen > 1)
192				zfree(namei_zone, cp);
193			break;
194		}
195		linklen = MAXPATHLEN - auio.uio_resid;
196		if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
197			if (ndp->ni_pathlen > 1)
198				zfree(namei_zone, cp);
199			error = ENAMETOOLONG;
200			break;
201		}
202		if (ndp->ni_pathlen > 1) {
203			bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
204			zfree(namei_zone, cnp->cn_pnbuf);
205			cnp->cn_pnbuf = cp;
206		} else
207			cnp->cn_pnbuf[linklen] = '\0';
208		ndp->ni_pathlen += linklen;
209		vput(ndp->ni_vp);
210		dp = ndp->ni_dvp;
211	}
212	zfree(namei_zone, cnp->cn_pnbuf);
213	vrele(ndp->ni_dvp);
214	vput(ndp->ni_vp);
215	ndp->ni_vp = NULL;
216	return (error);
217}
218
219/*
220 * Search a pathname.
221 * This is a very central and rather complicated routine.
222 *
223 * The pathname is pointed to by ni_ptr and is of length ni_pathlen.
224 * The starting directory is taken from ni_startdir. The pathname is
225 * descended until done, or a symbolic link is encountered. The variable
226 * ni_more is clear if the path is completed; it is set to one if a
227 * symbolic link needing interpretation is encountered.
228 *
229 * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
230 * whether the name is to be looked up, created, renamed, or deleted.
231 * When CREATE, RENAME, or DELETE is specified, information usable in
232 * creating, renaming, or deleting a directory entry may be calculated.
233 * If flag has LOCKPARENT or'ed into it, the parent directory is returned
234 * locked. If flag has WANTPARENT or'ed into it, the parent directory is
235 * returned unlocked. Otherwise the parent directory is not returned. If
236 * the target of the pathname exists and LOCKLEAF is or'ed into the flag
237 * the target is returned locked, otherwise it is returned unlocked.
238 * When creating or renaming and LOCKPARENT is specified, the target may not
239 * be ".".  When deleting and LOCKPARENT is specified, the target may be ".".
240 *
241 * Overall outline of lookup:
242 *
243 * dirloop:
244 *	identify next component of name at ndp->ni_ptr
245 *	handle degenerate case where name is null string
246 *	if .. and crossing mount points and on mounted filesys, find parent
247 *	call VOP_LOOKUP routine for next component name
248 *	    directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set
249 *	    component vnode returned in ni_vp (if it exists), locked.
250 *	if result vnode is mounted on and crossing mount points,
251 *	    find mounted on vnode
252 *	if more components of name, do next level at dirloop
253 *	return the answer in ni_vp, locked if LOCKLEAF set
254 *	    if LOCKPARENT set, return locked parent in ni_dvp
255 *	    if WANTPARENT set, return unlocked parent in ni_dvp
256 */
257int
258lookup(ndp)
259	register struct nameidata *ndp;
260{
261	register char *cp;		/* pointer into pathname argument */
262	register struct vnode *dp = 0;	/* the directory we are searching */
263	struct vnode *tdp;		/* saved dp */
264	struct mount *mp;		/* mount table entry */
265	int docache;			/* == 0 do not cache last component */
266	int wantparent;			/* 1 => wantparent or lockparent flag */
267	int rdonly;			/* lookup read-only flag bit */
268	int trailing_slash;
269	int error = 0;
270	struct componentname *cnp = &ndp->ni_cnd;
271	struct proc *p = cnp->cn_proc;
272
273	/*
274	 * Setup: break out flag bits into variables.
275	 */
276	wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT);
277	docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE;
278	if (cnp->cn_nameiop == DELETE ||
279	    (wantparent && cnp->cn_nameiop != CREATE &&
280	     cnp->cn_nameiop != LOOKUP))
281		docache = 0;
282	rdonly = cnp->cn_flags & RDONLY;
283	ndp->ni_dvp = NULL;
284	cnp->cn_flags &= ~ISSYMLINK;
285	dp = ndp->ni_startdir;
286	ndp->ni_startdir = NULLVP;
287	vn_lock(dp, LK_EXCLUSIVE | LK_RETRY, p);
288
289dirloop:
290	/*
291	 * Search a new directory.
292	 *
293	 * The cn_hash value is for use by vfs_cache.
294	 * The last component of the filename is left accessible via
295	 * cnp->cn_nameptr for callers that need the name. Callers needing
296	 * the name set the SAVENAME flag. When done, they assume
297	 * responsibility for freeing the pathname buffer.
298	 */
299	cnp->cn_consume = 0;
300	cnp->cn_hash = 0;
301	for (cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++)
302		cnp->cn_hash += (unsigned char)*cp;
303	cnp->cn_namelen = cp - cnp->cn_nameptr;
304	if (cnp->cn_namelen > NAME_MAX) {
305		error = ENAMETOOLONG;
306		goto bad;
307	}
308#ifdef NAMEI_DIAGNOSTIC
309	{ char c = *cp;
310	*cp = '\0';
311	printf("{%s}: ", cnp->cn_nameptr);
312	*cp = c; }
313#endif
314	ndp->ni_pathlen -= cnp->cn_namelen;
315	ndp->ni_next = cp;
316
317	/*
318	 * Replace multiple slashes by a single slash and trailing slashes
319	 * by a null.  This must be done before VOP_LOOKUP() because some
320	 * fs's don't know about trailing slashes.  Remember if there were
321	 * trailing slashes to handle symlinks, existing non-directories
322	 * and non-existing files that won't be directories specially later.
323	 */
324	trailing_slash = 0;
325	while (*cp == '/' && (cp[1] == '/' || cp[1] == '\0')) {
326		cp++;
327		ndp->ni_pathlen--;
328		if (*cp == '\0') {
329			trailing_slash = 1;
330			*ndp->ni_next = '\0';	/* XXX for direnter() ... */
331		}
332	}
333	ndp->ni_next = cp;
334
335	cnp->cn_flags |= MAKEENTRY;
336	if (*cp == '\0' && docache == 0)
337		cnp->cn_flags &= ~MAKEENTRY;
338	if (cnp->cn_namelen == 2 &&
339	    cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.')
340		cnp->cn_flags |= ISDOTDOT;
341	else
342		cnp->cn_flags &= ~ISDOTDOT;
343	if (*ndp->ni_next == 0)
344		cnp->cn_flags |= ISLASTCN;
345	else
346		cnp->cn_flags &= ~ISLASTCN;
347
348
349	/*
350	 * Check for degenerate name (e.g. / or "")
351	 * which is a way of talking about a directory,
352	 * e.g. like "/." or ".".
353	 */
354	if (cnp->cn_nameptr[0] == '\0') {
355		if (dp->v_type != VDIR) {
356			error = ENOTDIR;
357			goto bad;
358		}
359		if (cnp->cn_nameiop != LOOKUP) {
360			error = EISDIR;
361			goto bad;
362		}
363		if (wantparent) {
364			ndp->ni_dvp = dp;
365			VREF(dp);
366		}
367		ndp->ni_vp = dp;
368		if (!(cnp->cn_flags & (LOCKPARENT | LOCKLEAF)))
369			VOP_UNLOCK(dp, 0, p);
370		if (cnp->cn_flags & SAVESTART)
371			panic("lookup: SAVESTART");
372		return (0);
373	}
374
375	/*
376	 * Handle "..": two special cases.
377	 * 1. If at root directory (e.g. after chroot)
378	 *    or at absolute root directory
379	 *    then ignore it so can't get out.
380	 * 2. If this vnode is the root of a mounted
381	 *    filesystem, then replace it with the
382	 *    vnode which was mounted on so we take the
383	 *    .. in the other file system.
384	 */
385	if (cnp->cn_flags & ISDOTDOT) {
386		for (;;) {
387			if (dp == ndp->ni_rootdir || dp == rootvnode) {
388				ndp->ni_dvp = dp;
389				ndp->ni_vp = dp;
390				VREF(dp);
391				goto nextname;
392			}
393			if ((dp->v_flag & VROOT) == 0 ||
394			    (cnp->cn_flags & NOCROSSMOUNT))
395				break;
396			tdp = dp;
397			dp = dp->v_mount->mnt_vnodecovered;
398			vput(tdp);
399			VREF(dp);
400			vn_lock(dp, LK_EXCLUSIVE | LK_RETRY, p);
401		}
402	}
403
404	/*
405	 * We now have a segment name to search for, and a directory to search.
406	 */
407unionlookup:
408	ndp->ni_dvp = dp;
409	ndp->ni_vp = NULL;
410	ASSERT_VOP_LOCKED(dp, "lookup");
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	ASSERT_VOP_LOCKED(ndp->ni_vp, "lookup");
461
462	/*
463	 * Take into account any additional components consumed by
464	 * the underlying filesystem.
465	 */
466	if (cnp->cn_consume > 0) {
467		cnp->cn_nameptr += cnp->cn_consume;
468		ndp->ni_next += cnp->cn_consume;
469		ndp->ni_pathlen -= cnp->cn_consume;
470		cnp->cn_consume = 0;
471	}
472
473	dp = ndp->ni_vp;
474
475	/*
476	 * Check to see if the vnode has been mounted on;
477	 * if so find the root of the mounted file system.
478	 */
479	while (dp->v_type == VDIR && (mp = dp->v_mountedhere) &&
480	       (cnp->cn_flags & NOCROSSMOUNT) == 0) {
481		if (vfs_busy(mp, 0, 0, p))
482			continue;
483		error = VFS_ROOT(mp, &tdp);
484		vfs_unbusy(mp, p);
485		if (error)
486			goto bad2;
487		vput(dp);
488		ndp->ni_vp = dp = tdp;
489	}
490
491	/*
492	 * Check for symbolic link
493	 */
494	if ((dp->v_type == VLNK) &&
495	    ((cnp->cn_flags & FOLLOW) || trailing_slash ||
496	     *ndp->ni_next == '/')) {
497		cnp->cn_flags |= ISSYMLINK;
498		return (0);
499	}
500
501	/*
502	 * Check for bogus trailing slashes.
503	 */
504	if (trailing_slash && dp->v_type != VDIR) {
505		error = ENOTDIR;
506		goto bad2;
507	}
508
509nextname:
510	/*
511	 * Not a symbolic link.  If more pathname,
512	 * continue at next component, else return.
513	 */
514	if (*ndp->ni_next == '/') {
515		cnp->cn_nameptr = ndp->ni_next;
516		while (*cnp->cn_nameptr == '/') {
517			cnp->cn_nameptr++;
518			ndp->ni_pathlen--;
519		}
520		if (ndp->ni_dvp != ndp->ni_vp) {
521		    ASSERT_VOP_UNLOCKED(ndp->ni_dvp, "lookup");
522		}
523		vrele(ndp->ni_dvp);
524		goto dirloop;
525	}
526	/*
527	 * Disallow directory write attempts on read-only file systems.
528	 */
529	if (rdonly &&
530	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
531		error = EROFS;
532		goto bad2;
533	}
534	if (cnp->cn_flags & SAVESTART) {
535		ndp->ni_startdir = ndp->ni_dvp;
536		VREF(ndp->ni_startdir);
537	}
538	if (!wantparent)
539		vrele(ndp->ni_dvp);
540	if ((cnp->cn_flags & LOCKLEAF) == 0)
541		VOP_UNLOCK(dp, 0, p);
542	return (0);
543
544bad2:
545	if ((cnp->cn_flags & LOCKPARENT) && *ndp->ni_next == '\0')
546		VOP_UNLOCK(ndp->ni_dvp, 0, p);
547	vrele(ndp->ni_dvp);
548bad:
549	vput(dp);
550	ndp->ni_vp = NULL;
551	return (error);
552}
553
554/*
555 * relookup - lookup a path name component
556 *    Used by lookup to re-aquire things.
557 */
558int
559relookup(dvp, vpp, cnp)
560	struct vnode *dvp, **vpp;
561	struct componentname *cnp;
562{
563	struct proc *p = cnp->cn_proc;
564	struct vnode *dp = 0;		/* the directory we are searching */
565	int docache;			/* == 0 do not cache last component */
566	int wantparent;			/* 1 => wantparent or lockparent flag */
567	int rdonly;			/* lookup read-only flag bit */
568	int error = 0;
569#ifdef NAMEI_DIAGNOSTIC
570	int newhash;			/* DEBUG: check name hash */
571	char *cp;			/* DEBUG: check name ptr/len */
572#endif
573
574	/*
575	 * Setup: break out flag bits into variables.
576	 */
577	wantparent = cnp->cn_flags & (LOCKPARENT|WANTPARENT);
578	docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE;
579	if (cnp->cn_nameiop == DELETE ||
580	    (wantparent && cnp->cn_nameiop != CREATE))
581		docache = 0;
582	rdonly = cnp->cn_flags & RDONLY;
583	cnp->cn_flags &= ~ISSYMLINK;
584	dp = dvp;
585	vn_lock(dp, LK_EXCLUSIVE | LK_RETRY, p);
586
587/* dirloop: */
588	/*
589	 * Search a new directory.
590	 *
591	 * The cn_hash value is for use by vfs_cache.
592	 * The last component of the filename is left accessible via
593	 * cnp->cn_nameptr for callers that need the name. Callers needing
594	 * the name set the SAVENAME flag. When done, they assume
595	 * responsibility for freeing the pathname buffer.
596	 */
597#ifdef NAMEI_DIAGNOSTIC
598	for (newhash = 0, cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++)
599		newhash += (unsigned char)*cp;
600	if (newhash != cnp->cn_hash)
601		panic("relookup: bad hash");
602	if (cnp->cn_namelen != cp - cnp->cn_nameptr)
603		panic ("relookup: bad len");
604	if (*cp != 0)
605		panic("relookup: not last component");
606	printf("{%s}: ", cnp->cn_nameptr);
607#endif
608
609	/*
610	 * Check for degenerate name (e.g. / or "")
611	 * which is a way of talking about a directory,
612	 * e.g. like "/." or ".".
613	 */
614	if (cnp->cn_nameptr[0] == '\0') {
615		if (cnp->cn_nameiop != LOOKUP || wantparent) {
616			error = EISDIR;
617			goto bad;
618		}
619		if (dp->v_type != VDIR) {
620			error = ENOTDIR;
621			goto bad;
622		}
623		if (!(cnp->cn_flags & LOCKLEAF))
624			VOP_UNLOCK(dp, 0, p);
625		*vpp = dp;
626		if (cnp->cn_flags & SAVESTART)
627			panic("lookup: SAVESTART");
628		return (0);
629	}
630
631	if (cnp->cn_flags & ISDOTDOT)
632		panic ("relookup: lookup on dot-dot");
633
634	/*
635	 * We now have a segment name to search for, and a directory to search.
636	 */
637	if (error = VOP_LOOKUP(dp, vpp, cnp)) {
638#ifdef DIAGNOSTIC
639		if (*vpp != NULL)
640			panic("leaf should be empty");
641#endif
642		if (error != EJUSTRETURN)
643			goto bad;
644		/*
645		 * If creating and at end of pathname, then can consider
646		 * allowing file to be created.
647		 */
648		if (rdonly) {
649			error = EROFS;
650			goto bad;
651		}
652		/* ASSERT(dvp == ndp->ni_startdir) */
653		if (cnp->cn_flags & SAVESTART)
654			VREF(dvp);
655		/*
656		 * We return with ni_vp NULL to indicate that the entry
657		 * doesn't currently exist, leaving a pointer to the
658		 * (possibly locked) directory inode in ndp->ni_dvp.
659		 */
660		return (0);
661	}
662	dp = *vpp;
663
664#ifdef DIAGNOSTIC
665	/*
666	 * Check for symbolic link
667	 */
668	if (dp->v_type == VLNK && (cnp->cn_flags & FOLLOW))
669		panic ("relookup: symlink found.\n");
670#endif
671
672	/*
673	 * Disallow directory write attempts on read-only file systems.
674	 */
675	if (rdonly &&
676	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
677		error = EROFS;
678		goto bad2;
679	}
680	/* ASSERT(dvp == ndp->ni_startdir) */
681	if (cnp->cn_flags & SAVESTART)
682		VREF(dvp);
683
684	if (!wantparent)
685		vrele(dvp);
686	if ((cnp->cn_flags & LOCKLEAF) == 0)
687		VOP_UNLOCK(dp, 0, p);
688	return (0);
689
690bad2:
691	if ((cnp->cn_flags & LOCKPARENT) && (cnp->cn_flags & ISLASTCN))
692		VOP_UNLOCK(dvp, 0, p);
693	vrele(dvp);
694bad:
695	vput(dp);
696	*vpp = NULL;
697	return (error);
698}
699