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