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