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