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