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