vfs_lookup.c revision 1.57.2.2 1 /* $NetBSD: vfs_lookup.c,v 1.57.2.2 2005/12/29 01:03:56 riz 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. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * @(#)vfs_lookup.c 8.10 (Berkeley) 5/27/95
37 */
38
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: vfs_lookup.c,v 1.57.2.2 2005/12/29 01:03:56 riz Exp $");
41
42 #include "opt_ktrace.h"
43 #include "opt_systrace.h"
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/syslimits.h>
49 #include <sys/time.h>
50 #include <sys/namei.h>
51 #include <sys/vnode.h>
52 #include <sys/mount.h>
53 #include <sys/errno.h>
54 #include <sys/filedesc.h>
55 #include <sys/hash.h>
56 #include <sys/malloc.h>
57 #include <sys/proc.h>
58 #include <sys/syslog.h>
59
60 #ifdef KTRACE
61 #include <sys/ktrace.h>
62 #endif
63 #ifdef SYSTRACE
64 #include <sys/systrace.h>
65 #endif
66
67 struct pool pnbuf_pool; /* pathname buffer pool */
68 struct pool_cache pnbuf_cache; /* pathname buffer cache */
69
70 MALLOC_DEFINE(M_NAMEI, "namei", "namei path buffer");
71
72 /*
73 * Substitute replacement text for 'magic' strings in symlinks.
74 * Returns 0 if successful, and returns non-zero if an error
75 * occurs. (Currently, the only possible error is running out
76 * of temporary pathname space.)
77 *
78 * Looks for "@<string>" and "@<string>/", where <string> is a
79 * recognized 'magic' string. Replaces the "@<string>" with the
80 * appropriate replacement text. (Note that in some cases the
81 * replacement text may have zero length.)
82 *
83 * This would have been table driven, but the variance in
84 * replacement strings (and replacement string lengths) made
85 * that impractical.
86 */
87 #define VNL(x) \
88 (sizeof(x) - 1)
89
90 #define VO '{'
91 #define VC '}'
92
93 #define MATCH(str) \
94 ((termchar == '/' && i + VNL(str) == *len) || \
95 (i + VNL(str) < *len && \
96 cp[i + VNL(str)] == termchar)) && \
97 !strncmp((str), &cp[i], VNL(str))
98
99 #define SUBSTITUTE(m, s, sl) \
100 if ((newlen + (sl)) > MAXPATHLEN) \
101 return (1); \
102 i += VNL(m); \
103 if (termchar != '/') \
104 i++; \
105 memcpy(&tmp[newlen], (s), (sl)); \
106 newlen += (sl); \
107 change = 1; \
108 termchar = '/';
109
110 static int
111 symlink_magic(struct proc *p, char *cp, int *len)
112 {
113 char tmp[MAXPATHLEN];
114 int change, i, newlen;
115 int termchar = '/';
116
117 for (change = i = newlen = 0; i < *len; ) {
118 if (cp[i] != '@') {
119 tmp[newlen++] = cp[i++];
120 continue;
121 }
122
123 i++;
124
125 /* Check for @{var} syntax. */
126 if (cp[i] == VO) {
127 termchar = VC;
128 i++;
129 }
130
131 /*
132 * The following checks should be ordered according
133 * to frequency of use.
134 */
135 if (MATCH("machine_arch")) {
136 SUBSTITUTE("machine_arch", MACHINE_ARCH,
137 sizeof(MACHINE_ARCH) - 1);
138 } else if (MATCH("machine")) {
139 SUBSTITUTE("machine", MACHINE,
140 sizeof(MACHINE) - 1);
141 } else if (MATCH("hostname")) {
142 SUBSTITUTE("hostname", hostname,
143 hostnamelen);
144 } else if (MATCH("osrelease")) {
145 SUBSTITUTE("osrelease", osrelease,
146 strlen(osrelease));
147 } else if (MATCH("emul")) {
148 SUBSTITUTE("emul", p->p_emul->e_name,
149 strlen(p->p_emul->e_name));
150 } else if (MATCH("kernel_ident")) {
151 SUBSTITUTE("kernel_ident", kernel_ident,
152 strlen(kernel_ident));
153 } else if (MATCH("domainname")) {
154 SUBSTITUTE("domainname", domainname,
155 domainnamelen);
156 } else if (MATCH("ostype")) {
157 SUBSTITUTE("ostype", ostype,
158 strlen(ostype));
159 } else {
160 tmp[newlen++] = '@';
161 if (termchar == VC)
162 tmp[newlen++] = VO;
163 }
164 }
165
166 if (! change)
167 return (0);
168
169 memcpy(cp, tmp, newlen);
170 *len = newlen;
171
172 return (0);
173 }
174
175 #undef VNL
176 #undef VO
177 #undef VC
178 #undef MATCH
179 #undef SUBSTITUTE
180
181 /*
182 * Convert a pathname into a pointer to a locked inode.
183 *
184 * The FOLLOW flag is set when symbolic links are to be followed
185 * when they occur at the end of the name translation process.
186 * Symbolic links are always followed for all other pathname
187 * components other than the last.
188 *
189 * The segflg defines whether the name is to be copied from user
190 * space or kernel space.
191 *
192 * Overall outline of namei:
193 *
194 * copy in name
195 * get starting directory
196 * while (!done && !error) {
197 * call lookup to search path.
198 * if symbolic link, massage name in buffer and continue
199 * }
200 */
201 int
202 namei(ndp)
203 struct nameidata *ndp;
204 {
205 struct cwdinfo *cwdi; /* pointer to cwd state */
206 char *cp; /* pointer into pathname argument */
207 struct vnode *dp; /* the directory we are searching */
208 struct iovec aiov; /* uio for reading symbolic links */
209 struct uio auio;
210 int error, linklen;
211 struct componentname *cnp = &ndp->ni_cnd;
212
213 #ifdef DIAGNOSTIC
214 if (!cnp->cn_cred || !cnp->cn_proc)
215 panic ("namei: bad cred/proc");
216 if (cnp->cn_nameiop & (~OPMASK))
217 panic ("namei: nameiop contaminated with flags");
218 if (cnp->cn_flags & OPMASK)
219 panic ("namei: flags contaminated with nameiops");
220 #endif
221 cwdi = cnp->cn_proc->p_cwdi;
222
223 /*
224 * Get a buffer for the name to be translated, and copy the
225 * name into the buffer.
226 */
227 if ((cnp->cn_flags & HASBUF) == 0)
228 cnp->cn_pnbuf = PNBUF_GET();
229 if (ndp->ni_segflg == UIO_SYSSPACE)
230 error = copystr(ndp->ni_dirp, cnp->cn_pnbuf,
231 MAXPATHLEN, &ndp->ni_pathlen);
232 else
233 error = copyinstr(ndp->ni_dirp, cnp->cn_pnbuf,
234 MAXPATHLEN, &ndp->ni_pathlen);
235
236 /*
237 * POSIX.1 requirement: "" is not a valid file name.
238 */
239 if (!error && ndp->ni_pathlen == 1)
240 error = ENOENT;
241
242 if (error) {
243 PNBUF_PUT(cnp->cn_pnbuf);
244 ndp->ni_vp = NULL;
245 return (error);
246 }
247 ndp->ni_loopcnt = 0;
248
249 #ifdef KTRACE
250 if (KTRPOINT(cnp->cn_proc, KTR_NAMEI))
251 ktrnamei(cnp->cn_proc, cnp->cn_pnbuf);
252 #endif
253 #ifdef SYSTRACE
254 if (ISSET(cnp->cn_proc->p_flag, P_SYSTRACE))
255 systrace_namei(ndp);
256 #endif
257
258 /*
259 * Get starting point for the translation.
260 */
261 if ((ndp->ni_rootdir = cwdi->cwdi_rdir) == NULL)
262 ndp->ni_rootdir = rootvnode;
263 /*
264 * Check if starting from root directory or current directory.
265 */
266 if (cnp->cn_pnbuf[0] == '/') {
267 dp = ndp->ni_rootdir;
268 VREF(dp);
269 } else {
270 dp = cwdi->cwdi_cdir;
271 VREF(dp);
272 }
273 for (;;) {
274 if (!dp->v_mount)
275 {
276 /* Give up if the directory is no longer mounted */
277 PNBUF_PUT(cnp->cn_pnbuf);
278 return (ENOENT);
279 }
280 cnp->cn_nameptr = cnp->cn_pnbuf;
281 ndp->ni_startdir = dp;
282 if ((error = lookup(ndp)) != 0) {
283 PNBUF_PUT(cnp->cn_pnbuf);
284 return (error);
285 }
286 /*
287 * Check for symbolic link
288 */
289 if ((cnp->cn_flags & ISSYMLINK) == 0) {
290 if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0)
291 PNBUF_PUT(cnp->cn_pnbuf);
292 else
293 cnp->cn_flags |= HASBUF;
294 return (0);
295 }
296 if ((cnp->cn_flags & LOCKPARENT) && (cnp->cn_flags & ISLASTCN))
297 VOP_UNLOCK(ndp->ni_dvp, 0);
298 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
299 error = ELOOP;
300 break;
301 }
302 if (ndp->ni_vp->v_mount->mnt_flag & MNT_SYMPERM) {
303 error = VOP_ACCESS(ndp->ni_vp, VEXEC, cnp->cn_cred,
304 cnp->cn_proc);
305 if (error != 0)
306 break;
307 }
308 if (ndp->ni_pathlen > 1)
309 cp = PNBUF_GET();
310 else
311 cp = cnp->cn_pnbuf;
312 aiov.iov_base = cp;
313 aiov.iov_len = MAXPATHLEN;
314 auio.uio_iov = &aiov;
315 auio.uio_iovcnt = 1;
316 auio.uio_offset = 0;
317 auio.uio_rw = UIO_READ;
318 auio.uio_segflg = UIO_SYSSPACE;
319 auio.uio_procp = NULL;
320 auio.uio_resid = MAXPATHLEN;
321 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
322 if (error) {
323 badlink:
324 if (ndp->ni_pathlen > 1)
325 PNBUF_PUT(cp);
326 break;
327 }
328 linklen = MAXPATHLEN - auio.uio_resid;
329 if (linklen == 0) {
330 error = ENOENT;
331 goto badlink;
332 }
333 /*
334 * Do symlink substitution, if appropriate, and
335 * check length for potential overflow.
336 */
337 if (((ndp->ni_vp->v_mount->mnt_flag & MNT_MAGICLINKS) &&
338 symlink_magic(cnp->cn_proc, cp, &linklen)) ||
339 (linklen + ndp->ni_pathlen >= MAXPATHLEN)) {
340 error = ENAMETOOLONG;
341 goto badlink;
342 }
343 if (ndp->ni_pathlen > 1) {
344 memcpy(cp + linklen, ndp->ni_next, ndp->ni_pathlen);
345 PNBUF_PUT(cnp->cn_pnbuf);
346 cnp->cn_pnbuf = cp;
347 } else
348 cnp->cn_pnbuf[linklen] = '\0';
349 ndp->ni_pathlen += linklen;
350 vput(ndp->ni_vp);
351 dp = ndp->ni_dvp;
352 /*
353 * Check if root directory should replace current directory.
354 */
355 if (cnp->cn_pnbuf[0] == '/') {
356 vrele(dp);
357 dp = ndp->ni_rootdir;
358 VREF(dp);
359 }
360 }
361 PNBUF_PUT(cnp->cn_pnbuf);
362 vrele(ndp->ni_dvp);
363 vput(ndp->ni_vp);
364 ndp->ni_vp = NULL;
365 return (error);
366 }
367
368 /*
369 * Determine the namei hash (for cn_hash) for name.
370 * If *ep != NULL, hash from name to ep-1.
371 * If *ep == NULL, hash from name until the first NUL or '/', and
372 * return the location of this termination character in *ep.
373 *
374 * This function returns an equivalent hash to the MI hash32_strn().
375 * The latter isn't used because in the *ep == NULL case, determining
376 * the length of the string to the first NUL or `/' and then calling
377 * hash32_strn() involves unnecessary double-handling of the data.
378 */
379 uint32_t
380 namei_hash(const char *name, const char **ep)
381 {
382 uint32_t hash;
383
384 hash = HASH32_STR_INIT;
385 if (*ep != NULL) {
386 for (; name < *ep; name++)
387 hash = hash * 33 + *(uint8_t *)name;
388 } else {
389 for (; *name != '\0' && *name != '/'; name++)
390 hash = hash * 33 + *(uint8_t *)name;
391 *ep = name;
392 }
393 return (hash + (hash >> 5));
394 }
395
396 /*
397 * Search a pathname.
398 * This is a very central and rather complicated routine.
399 *
400 * The pathname is pointed to by ni_ptr and is of length ni_pathlen.
401 * The starting directory is taken from ni_startdir. The pathname is
402 * descended until done, or a symbolic link is encountered. The variable
403 * ni_more is clear if the path is completed; it is set to one if a
404 * symbolic link needing interpretation is encountered.
405 *
406 * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
407 * whether the name is to be looked up, created, renamed, or deleted.
408 * When CREATE, RENAME, or DELETE is specified, information usable in
409 * creating, renaming, or deleting a directory entry may be calculated.
410 * If flag has LOCKPARENT or'ed into it, the parent directory is returned
411 * locked. If flag has WANTPARENT or'ed into it, the parent directory is
412 * returned unlocked. Otherwise the parent directory is not returned. If
413 * the target of the pathname exists and LOCKLEAF is or'ed into the flag
414 * the target is returned locked, otherwise it is returned unlocked.
415 * When creating or renaming and LOCKPARENT is specified, the target may not
416 * be ".". When deleting and LOCKPARENT is specified, the target may be ".".
417 *
418 * Overall outline of lookup:
419 *
420 * dirloop:
421 * identify next component of name at ndp->ni_ptr
422 * handle degenerate case where name is null string
423 * if .. and crossing mount points and on mounted filesys, find parent
424 * call VOP_LOOKUP routine for next component name
425 * directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set
426 * component vnode returned in ni_vp (if it exists), locked.
427 * if result vnode is mounted on and crossing mount points,
428 * find mounted on vnode
429 * if more components of name, do next level at dirloop
430 * return the answer in ni_vp, locked if LOCKLEAF set
431 * if LOCKPARENT set, return locked parent in ni_dvp
432 * if WANTPARENT set, return unlocked parent in ni_dvp
433 */
434 int
435 lookup(ndp)
436 struct nameidata *ndp;
437 {
438 const char *cp; /* pointer into pathname argument */
439 struct vnode *dp = 0; /* the directory we are searching */
440 struct vnode *tdp; /* saved dp */
441 struct mount *mp; /* mount table entry */
442 int docache; /* == 0 do not cache last component */
443 int wantparent; /* 1 => wantparent or lockparent flag */
444 int rdonly; /* lookup read-only flag bit */
445 int error = 0;
446 int slashes;
447 int dpunlocked = 0; /* dp has already been unlocked */
448 struct componentname *cnp = &ndp->ni_cnd;
449
450 /*
451 * Setup: break out flag bits into variables.
452 */
453 wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT);
454 docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE;
455 if (cnp->cn_nameiop == DELETE ||
456 (wantparent && cnp->cn_nameiop != CREATE))
457 docache = 0;
458 rdonly = cnp->cn_flags & RDONLY;
459 ndp->ni_dvp = NULL;
460 cnp->cn_flags &= ~ISSYMLINK;
461 dp = ndp->ni_startdir;
462 ndp->ni_startdir = NULLVP;
463 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
464
465 /*
466 * If we have a leading string of slashes, remove them, and just make
467 * sure the current node is a directory.
468 */
469 cp = cnp->cn_nameptr;
470 if (*cp == '/') {
471 do {
472 cp++;
473 } while (*cp == '/');
474 ndp->ni_pathlen -= cp - cnp->cn_nameptr;
475 cnp->cn_nameptr = cp;
476
477 if (dp->v_type != VDIR) {
478 error = ENOTDIR;
479 goto bad;
480 }
481
482 /*
483 * If we've exhausted the path name, then just return the
484 * current node. If the caller requested the parent node (i.e.
485 * it's a CREATE, DELETE, or RENAME), and we don't have one
486 * (because this is the root directory), then we must fail.
487 */
488 if (cnp->cn_nameptr[0] == '\0') {
489 if (ndp->ni_dvp == NULL && wantparent) {
490 error = EISDIR;
491 goto bad;
492 }
493 ndp->ni_vp = dp;
494 cnp->cn_flags |= ISLASTCN;
495 goto terminal;
496 }
497 }
498
499 dirloop:
500 /*
501 * Search a new directory.
502 *
503 * The cn_hash value is for use by vfs_cache.
504 * The last component of the filename is left accessible via
505 * cnp->cn_nameptr for callers that need the name. Callers needing
506 * the name set the SAVENAME flag. When done, they assume
507 * responsibility for freeing the pathname buffer.
508 */
509 cnp->cn_consume = 0;
510 cp = NULL;
511 cnp->cn_hash = namei_hash(cnp->cn_nameptr, &cp);
512 cnp->cn_namelen = cp - cnp->cn_nameptr;
513 if (cnp->cn_namelen > NAME_MAX) {
514 error = ENAMETOOLONG;
515 goto bad;
516 }
517 #ifdef NAMEI_DIAGNOSTIC
518 { char c = *cp;
519 *(char *)cp = '\0';
520 printf("{%s}: ", cnp->cn_nameptr);
521 *(char *)cp = c; }
522 #endif /* NAMEI_DIAGNOSTIC */
523 ndp->ni_pathlen -= cnp->cn_namelen;
524 ndp->ni_next = cp;
525 /*
526 * If this component is followed by a slash, then move the pointer to
527 * the next component forward, and remember that this component must be
528 * a directory.
529 */
530 if (*cp == '/') {
531 do {
532 cp++;
533 } while (*cp == '/');
534 slashes = cp - ndp->ni_next;
535 ndp->ni_pathlen -= slashes;
536 ndp->ni_next = cp;
537 cnp->cn_flags |= REQUIREDIR;
538 } else {
539 slashes = 0;
540 cnp->cn_flags &= ~REQUIREDIR;
541 }
542 /*
543 * We do special processing on the last component, whether or not it's
544 * a directory. Cache all intervening lookups, but not the final one.
545 */
546 if (*cp == '\0') {
547 if (docache)
548 cnp->cn_flags |= MAKEENTRY;
549 else
550 cnp->cn_flags &= ~MAKEENTRY;
551 cnp->cn_flags |= ISLASTCN;
552 } else {
553 cnp->cn_flags |= MAKEENTRY;
554 cnp->cn_flags &= ~ISLASTCN;
555 }
556 if (cnp->cn_namelen == 2 &&
557 cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.')
558 cnp->cn_flags |= ISDOTDOT;
559 else
560 cnp->cn_flags &= ~ISDOTDOT;
561
562 /*
563 * Handle "..": two special cases.
564 * 1. If at root directory (e.g. after chroot)
565 * or at absolute root directory
566 * then ignore it so can't get out.
567 * 1a. If we have somehow gotten out of a jail, warn
568 * and also ignore it so we can't get farther out.
569 * 2. If this vnode is the root of a mounted
570 * filesystem, then replace it with the
571 * vnode which was mounted on so we take the
572 * .. in the other file system.
573 */
574 if (cnp->cn_flags & ISDOTDOT) {
575 for (;;) {
576 if (dp == ndp->ni_rootdir || dp == rootvnode) {
577 ndp->ni_dvp = dp;
578 ndp->ni_vp = dp;
579 VREF(dp);
580 goto nextname;
581 }
582 if (ndp->ni_rootdir != rootvnode) {
583 int retval;
584 VOP_UNLOCK(dp, 0);
585 retval = vn_isunder(dp, ndp->ni_rootdir,
586 cnp->cn_proc);
587 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
588 if (!retval) {
589 /* Oops! We got out of jail! */
590 log(LOG_WARNING,
591 "chrooted pid %d uid %d (%s) "
592 "detected outside of its chroot\n",
593 cnp->cn_proc->p_pid,
594 cnp->cn_proc->p_ucred->cr_uid,
595 cnp->cn_proc->p_comm);
596 /* Put us at the jail root. */
597 vput(dp);
598 dp = ndp->ni_rootdir;
599 ndp->ni_dvp = dp;
600 ndp->ni_vp = dp;
601 VREF(dp);
602 VREF(dp);
603 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
604 goto nextname;
605 }
606 }
607 if ((dp->v_flag & VROOT) == 0 ||
608 (cnp->cn_flags & NOCROSSMOUNT))
609 break;
610 tdp = dp;
611 dp = dp->v_mount->mnt_vnodecovered;
612 vput(tdp);
613 VREF(dp);
614 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
615 }
616 }
617
618 /*
619 * We now have a segment name to search for, and a directory to search.
620 */
621 unionlookup:
622 ndp->ni_dvp = dp;
623 ndp->ni_vp = NULL;
624 cnp->cn_flags &= ~PDIRUNLOCK;
625 if ((error = VOP_LOOKUP(dp, &ndp->ni_vp, cnp)) != 0) {
626 #ifdef DIAGNOSTIC
627 if (ndp->ni_vp != NULL)
628 panic("leaf `%s' should be empty", cnp->cn_nameptr);
629 #endif /* DIAGNOSTIC */
630 #ifdef NAMEI_DIAGNOSTIC
631 printf("not found\n");
632 #endif /* NAMEI_DIAGNOSTIC */
633 if ((error == ENOENT) &&
634 (dp->v_flag & VROOT) &&
635 (dp->v_mount->mnt_flag & MNT_UNION)) {
636 tdp = dp;
637 dp = dp->v_mount->mnt_vnodecovered;
638 if (cnp->cn_flags & PDIRUNLOCK)
639 vrele(tdp);
640 else
641 vput(tdp);
642 VREF(dp);
643 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
644 goto unionlookup;
645 }
646
647 if (cnp->cn_flags & PDIRUNLOCK)
648 dpunlocked = 1;
649
650 if (error != EJUSTRETURN)
651 goto bad;
652 /*
653 * If this was not the last component, or there were trailing
654 * slashes, and we are not going to create a directory,
655 * then the name must exist.
656 */
657 if ((cnp->cn_flags & (REQUIREDIR | CREATEDIR)) == REQUIREDIR) {
658 error = ENOENT;
659 goto bad;
660 }
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 /*
670 * We return with ni_vp NULL to indicate that the entry
671 * doesn't currently exist, leaving a pointer to the
672 * (possibly locked) directory inode in ndp->ni_dvp.
673 */
674 if (cnp->cn_flags & SAVESTART) {
675 ndp->ni_startdir = ndp->ni_dvp;
676 VREF(ndp->ni_startdir);
677 }
678 return (0);
679 }
680 #ifdef NAMEI_DIAGNOSTIC
681 printf("found\n");
682 #endif /* NAMEI_DIAGNOSTIC */
683
684 /*
685 * Take into account any additional components consumed by the
686 * underlying filesystem. This will include any trailing slashes after
687 * the last component consumed.
688 */
689 if (cnp->cn_consume > 0) {
690 ndp->ni_pathlen -= cnp->cn_consume - slashes;
691 ndp->ni_next += cnp->cn_consume - slashes;
692 cnp->cn_consume = 0;
693 if (ndp->ni_next[0] == '\0')
694 cnp->cn_flags |= ISLASTCN;
695 }
696
697 dp = ndp->ni_vp;
698 /*
699 * Check to see if the vnode has been mounted on;
700 * if so find the root of the mounted file system.
701 */
702 while (dp->v_type == VDIR && (mp = dp->v_mountedhere) &&
703 (cnp->cn_flags & NOCROSSMOUNT) == 0) {
704 if (vfs_busy(mp, 0, 0))
705 continue;
706 VOP_UNLOCK(dp, 0);
707 error = VFS_ROOT(mp, &tdp);
708 vfs_unbusy(mp);
709 if (error) {
710 dpunlocked = 1;
711 goto bad2;
712 }
713 vrele(dp);
714 ndp->ni_vp = dp = tdp;
715 }
716
717 /*
718 * Check for symbolic link. Back up over any slashes that we skipped,
719 * as we will need them again.
720 */
721 if ((dp->v_type == VLNK) && (cnp->cn_flags & (FOLLOW|REQUIREDIR))) {
722 ndp->ni_pathlen += slashes;
723 ndp->ni_next -= slashes;
724 cnp->cn_flags |= ISSYMLINK;
725 return (0);
726 }
727
728 /*
729 * Check for directory, if the component was followed by a series of
730 * slashes.
731 */
732 if ((dp->v_type != VDIR) && (cnp->cn_flags & REQUIREDIR)) {
733 error = ENOTDIR;
734 goto bad2;
735 }
736
737 nextname:
738 /*
739 * Not a symbolic link. If this was not the last component, then
740 * continue at the next component, else return.
741 */
742 if (!(cnp->cn_flags & ISLASTCN)) {
743 cnp->cn_nameptr = ndp->ni_next;
744 vrele(ndp->ni_dvp);
745 goto dirloop;
746 }
747
748 terminal:
749 /*
750 * Disallow directory write attempts on read-only file systems.
751 */
752 if (rdonly &&
753 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
754 /*
755 * Disallow directory write attempts on read-only
756 * file systems.
757 */
758 error = EROFS;
759 goto bad2;
760 }
761 if (ndp->ni_dvp != NULL) {
762 if (cnp->cn_flags & SAVESTART) {
763 ndp->ni_startdir = ndp->ni_dvp;
764 VREF(ndp->ni_startdir);
765 }
766 if (!wantparent)
767 vrele(ndp->ni_dvp);
768 }
769 if ((cnp->cn_flags & LOCKLEAF) == 0)
770 VOP_UNLOCK(dp, 0);
771 return (0);
772
773 bad2:
774 if ((cnp->cn_flags & LOCKPARENT) && (cnp->cn_flags & ISLASTCN) &&
775 ((cnp->cn_flags & PDIRUNLOCK) == 0))
776 VOP_UNLOCK(ndp->ni_dvp, 0);
777 vrele(ndp->ni_dvp);
778 bad:
779 if (dpunlocked)
780 vrele(dp);
781 else
782 vput(dp);
783 ndp->ni_vp = NULL;
784 return (error);
785 }
786
787 /*
788 * Reacquire a path name component.
789 */
790 int
791 relookup(dvp, vpp, cnp)
792 struct vnode *dvp, **vpp;
793 struct componentname *cnp;
794 {
795 struct vnode *dp = 0; /* the directory we are searching */
796 int wantparent; /* 1 => wantparent or lockparent flag */
797 int rdonly; /* lookup read-only flag bit */
798 int error = 0;
799 #ifdef DEBUG
800 u_long newhash; /* DEBUG: check name hash */
801 const char *cp; /* DEBUG: check name ptr/len */
802 #endif /* DEBUG */
803
804 /*
805 * Setup: break out flag bits into variables.
806 */
807 wantparent = cnp->cn_flags & (LOCKPARENT|WANTPARENT);
808 rdonly = cnp->cn_flags & RDONLY;
809 cnp->cn_flags &= ~ISSYMLINK;
810 dp = dvp;
811 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
812
813 /* dirloop: */
814 /*
815 * Search a new directory.
816 *
817 * The cn_hash value is for use by vfs_cache.
818 * The last component of the filename is left accessible via
819 * cnp->cn_nameptr for callers that need the name. Callers needing
820 * the name set the SAVENAME flag. When done, they assume
821 * responsibility for freeing the pathname buffer.
822 */
823 #ifdef DEBUG
824 cp = NULL;
825 newhash = namei_hash(cnp->cn_nameptr, &cp);
826 if (newhash != cnp->cn_hash)
827 panic("relookup: bad hash");
828 if (cnp->cn_namelen != cp - cnp->cn_nameptr)
829 panic ("relookup: bad len");
830 while (*cp == '/')
831 cp++;
832 if (*cp != 0)
833 panic("relookup: not last component");
834 #endif /* DEBUG */
835 #ifdef NAMEI_DIAGNOSTIC
836 printf("{%s}: ", cnp->cn_nameptr);
837 #endif /* NAMEI_DIAGNOSTIC */
838
839 /*
840 * Check for degenerate name (e.g. / or "")
841 * which is a way of talking about a directory,
842 * e.g. like "/." or ".".
843 */
844 if (cnp->cn_nameptr[0] == '\0')
845 panic("relookup: null name");
846
847 if (cnp->cn_flags & ISDOTDOT)
848 panic ("relookup: lookup on dot-dot");
849
850 /*
851 * We now have a segment name to search for, and a directory to search.
852 */
853 if ((error = VOP_LOOKUP(dp, vpp, cnp)) != 0) {
854 #ifdef DIAGNOSTIC
855 if (*vpp != NULL)
856 panic("leaf `%s' should be empty", cnp->cn_nameptr);
857 #endif
858 if (error != EJUSTRETURN)
859 goto bad;
860 /*
861 * If creating and at end of pathname, then can consider
862 * allowing file to be created.
863 */
864 if (rdonly) {
865 error = EROFS;
866 goto bad;
867 }
868 /* ASSERT(dvp == ndp->ni_startdir) */
869 if (cnp->cn_flags & SAVESTART)
870 VREF(dvp);
871 /*
872 * We return with ni_vp NULL to indicate that the entry
873 * doesn't currently exist, leaving a pointer to the
874 * (possibly locked) directory inode in ndp->ni_dvp.
875 */
876 return (0);
877 }
878 dp = *vpp;
879
880 #ifdef DIAGNOSTIC
881 /*
882 * Check for symbolic link
883 */
884 if (dp->v_type == VLNK && (cnp->cn_flags & FOLLOW))
885 panic ("relookup: symlink found.\n");
886 #endif
887
888 /*
889 * Check for read-only file systems.
890 */
891 if (rdonly &&
892 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
893 error = EROFS;
894 goto bad2;
895 }
896 /* ASSERT(dvp == ndp->ni_startdir) */
897 if (cnp->cn_flags & SAVESTART)
898 VREF(dvp);
899 if (!wantparent)
900 vrele(dvp);
901 if ((cnp->cn_flags & LOCKLEAF) == 0)
902 VOP_UNLOCK(dp, 0);
903 return (0);
904
905 bad2:
906 if ((cnp->cn_flags & LOCKPARENT) && (cnp->cn_flags & ISLASTCN))
907 VOP_UNLOCK(dvp, 0);
908 vrele(dvp);
909 bad:
910 vput(dp);
911 *vpp = NULL;
912 return (error);
913 }
914