vfs_lookup.c revision 1.24.2.1 1 /* $NetBSD: vfs_lookup.c,v 1.24.2.1 1997/10/31 07:50:45 mellon 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.6 (Berkeley) 11/21/94
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);
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 VOP_LOCK(dp);
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 VOP_LOCK(dp);
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 if ((error = VOP_LOOKUP(dp, &ndp->ni_vp, cnp)) != 0) {
432 #ifdef DIAGNOSTIC
433 if (ndp->ni_vp != NULL)
434 panic("leaf should be empty");
435 #endif
436 #ifdef NAMEI_DIAGNOSTIC
437 printf("not found\n");
438 #endif
439 if ((error == ENOENT) &&
440 (dp->v_flag & VROOT) &&
441 (dp->v_mount->mnt_flag & MNT_UNION)) {
442 tdp = dp;
443 dp = dp->v_mount->mnt_vnodecovered;
444 vput(tdp);
445 VREF(dp);
446 VOP_LOCK(dp);
447 goto unionlookup;
448 }
449
450 if (error != EJUSTRETURN)
451 goto bad;
452 /*
453 * If this was not the last component, or there were trailing
454 * slashes, then the name must exist.
455 */
456 if (cnp->cn_flags & REQUIREDIR) {
457 error = ENOENT;
458 goto bad;
459 }
460 /*
461 * If creating and at end of pathname, then can consider
462 * allowing file to be created.
463 */
464 if (rdonly || (ndp->ni_dvp->v_mount->mnt_flag & MNT_RDONLY)) {
465 error = EROFS;
466 goto bad;
467 }
468 /*
469 * We return with ni_vp NULL to indicate that the entry
470 * doesn't currently exist, leaving a pointer to the
471 * (possibly locked) directory inode in ndp->ni_dvp.
472 */
473 if (cnp->cn_flags & SAVESTART) {
474 ndp->ni_startdir = ndp->ni_dvp;
475 VREF(ndp->ni_startdir);
476 }
477 return (0);
478 }
479 #ifdef NAMEI_DIAGNOSTIC
480 printf("found\n");
481 #endif
482
483 /*
484 * Take into account any additional components consumed by the
485 * underlying filesystem. This will include any trailing slashes after
486 * the last component consumed.
487 */
488 if (cnp->cn_consume > 0) {
489 ndp->ni_pathlen -= cnp->cn_consume - slashes;
490 ndp->ni_next += cnp->cn_consume - slashes;
491 cnp->cn_consume = 0;
492 if (ndp->ni_next[0] == '\0')
493 cnp->cn_flags |= ISLASTCN;
494 }
495
496 dp = ndp->ni_vp;
497 /*
498 * Check to see if the vnode has been mounted on;
499 * if so find the root of the mounted file system.
500 */
501 while (dp->v_type == VDIR && (mp = dp->v_mountedhere) &&
502 (cnp->cn_flags & NOCROSSMOUNT) == 0) {
503 if (mp->mnt_flag & MNT_MLOCK) {
504 mp->mnt_flag |= MNT_MWAIT;
505 sleep((caddr_t)mp, PVFS);
506 continue;
507 }
508 if ((error = VFS_ROOT(dp->v_mountedhere, &tdp)) != 0)
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 * Check for read-only file systems.
548 */
549 if (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME) {
550 /*
551 * Disallow directory write attempts on read-only
552 * file systems.
553 */
554 if (rdonly || (dp->v_mount->mnt_flag & MNT_RDONLY) ||
555 (wantparent &&
556 (ndp->ni_dvp->v_mount->mnt_flag & MNT_RDONLY))) {
557 error = EROFS;
558 goto bad2;
559 }
560 }
561 if (ndp->ni_dvp != NULL) {
562 if (cnp->cn_flags & SAVESTART) {
563 ndp->ni_startdir = ndp->ni_dvp;
564 VREF(ndp->ni_startdir);
565 }
566 if (!wantparent)
567 vrele(ndp->ni_dvp);
568 }
569 if ((cnp->cn_flags & LOCKLEAF) == 0)
570 VOP_UNLOCK(dp);
571 return (0);
572
573 bad2:
574 if ((cnp->cn_flags & LOCKPARENT) && (cnp->cn_flags & ISLASTCN))
575 VOP_UNLOCK(ndp->ni_dvp);
576 vrele(ndp->ni_dvp);
577 bad:
578 vput(dp);
579 ndp->ni_vp = NULL;
580 return (error);
581 }
582
583 /*
584 * Reacquire a path name component.
585 */
586 int
587 relookup(dvp, vpp, cnp)
588 struct vnode *dvp, **vpp;
589 struct componentname *cnp;
590 {
591 register struct vnode *dp = 0; /* the directory we are searching */
592 int docache; /* == 0 do not cache last component */
593 int wantparent; /* 1 => wantparent or lockparent flag */
594 int rdonly; /* lookup read-only flag bit */
595 int error = 0;
596 #ifdef NAMEI_DIAGNOSTIC
597 int newhash; /* DEBUG: check name hash */
598 char *cp; /* DEBUG: check name ptr/len */
599 #endif
600
601 /*
602 * Setup: break out flag bits into variables.
603 */
604 wantparent = cnp->cn_flags & (LOCKPARENT|WANTPARENT);
605 docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE;
606 if (cnp->cn_nameiop == DELETE ||
607 (wantparent && cnp->cn_nameiop != CREATE))
608 docache = 0;
609 rdonly = cnp->cn_flags & RDONLY;
610 cnp->cn_flags &= ~ISSYMLINK;
611 dp = dvp;
612 VOP_LOCK(dp);
613
614 /* dirloop: */
615 /*
616 * Search a new directory.
617 *
618 * The cn_hash value is for use by vfs_cache.
619 * The last component of the filename is left accessible via
620 * cnp->cn_nameptr for callers that need the name. Callers needing
621 * the name set the SAVENAME flag. When done, they assume
622 * responsibility for freeing the pathname buffer.
623 */
624 #ifdef NAMEI_DIAGNOSTIC
625 for (newhash = 0, cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++)
626 newhash += (unsigned char)*cp;
627 if (newhash != cnp->cn_hash)
628 panic("relookup: bad hash");
629 if (cnp->cn_namelen != cp - cnp->cn_nameptr)
630 panic ("relookup: bad len");
631 if (*cp != 0)
632 panic("relookup: not last component");
633 printf("{%s}: ", cnp->cn_nameptr);
634 #endif
635
636 /*
637 * Check for degenerate name (e.g. / or "")
638 * which is a way of talking about a directory,
639 * e.g. like "/." or ".".
640 */
641 if (cnp->cn_nameptr[0] == '\0')
642 panic("relookup: null name");
643
644 if (cnp->cn_flags & ISDOTDOT)
645 panic ("relookup: lookup on dot-dot");
646
647 /*
648 * We now have a segment name to search for, and a directory to search.
649 */
650 if ((error = VOP_LOOKUP(dp, vpp, cnp)) != 0) {
651 #ifdef DIAGNOSTIC
652 if (*vpp != NULL)
653 panic("leaf should be empty");
654 #endif
655 if (error != EJUSTRETURN)
656 goto bad;
657 /*
658 * If creating and at end of pathname, then can consider
659 * allowing file to be created.
660 */
661 if (rdonly || (dvp->v_mount->mnt_flag & MNT_RDONLY)) {
662 error = EROFS;
663 goto bad;
664 }
665 /* ASSERT(dvp == ndp->ni_startdir) */
666 if (cnp->cn_flags & SAVESTART)
667 VREF(dvp);
668 /*
669 * We return with ni_vp NULL to indicate that the entry
670 * doesn't currently exist, leaving a pointer to the
671 * (possibly locked) directory inode in ndp->ni_dvp.
672 */
673 return (0);
674 }
675 dp = *vpp;
676
677 #ifdef DIAGNOSTIC
678 /*
679 * Check for symbolic link
680 */
681 if (dp->v_type == VLNK && (cnp->cn_flags & FOLLOW))
682 panic ("relookup: symlink found.\n");
683 #endif
684
685 /*
686 * Check for read-only file systems.
687 */
688 if (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME) {
689 /*
690 * Disallow directory write attempts on read-only
691 * file systems.
692 */
693 if (rdonly || (dp->v_mount->mnt_flag & MNT_RDONLY) ||
694 (wantparent &&
695 (dvp->v_mount->mnt_flag & MNT_RDONLY))) {
696 error = EROFS;
697 goto bad2;
698 }
699 }
700 /* ASSERT(dvp == ndp->ni_startdir) */
701 if (cnp->cn_flags & SAVESTART)
702 VREF(dvp);
703 if (!wantparent)
704 vrele(dvp);
705 if ((cnp->cn_flags & LOCKLEAF) == 0)
706 VOP_UNLOCK(dp);
707 return (0);
708
709 bad2:
710 if ((cnp->cn_flags & LOCKPARENT) && (cnp->cn_flags & ISLASTCN))
711 VOP_UNLOCK(dvp);
712 vrele(dvp);
713 bad:
714 vput(dp);
715 *vpp = NULL;
716 return (error);
717 }
718