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