vfs_lookup.c revision 1.75 1 /* $NetBSD: vfs_lookup.c,v 1.75 2006/12/13 13:36:19 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.75 2006/12/13 13:36:19 yamt 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 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
286 for (;;) {
287 if (!dp->v_mount) {
288 /* Give up if the directory is no longer mounted */
289 vput(dp);
290 PNBUF_PUT(cnp->cn_pnbuf);
291 return (ENOENT);
292 }
293 cnp->cn_nameptr = cnp->cn_pnbuf;
294 ndp->ni_startdir = dp;
295 error = lookup(ndp);
296 if (error != 0) {
297 if (ndp->ni_dvp) {
298 vput(ndp->ni_dvp);
299 }
300 PNBUF_PUT(cnp->cn_pnbuf);
301 return (error);
302 }
303
304 /*
305 * Check for symbolic link
306 */
307 if ((cnp->cn_flags & ISSYMLINK) == 0) {
308 if ((cnp->cn_flags & LOCKPARENT) == 0 && ndp->ni_dvp) {
309 if (ndp->ni_dvp == ndp->ni_vp) {
310 vrele(ndp->ni_dvp);
311 } else {
312 vput(ndp->ni_dvp);
313 }
314 }
315 if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0)
316 PNBUF_PUT(cnp->cn_pnbuf);
317 else
318 cnp->cn_flags |= HASBUF;
319 return (0);
320 }
321
322 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
323 error = ELOOP;
324 break;
325 }
326 if (ndp->ni_vp->v_mount->mnt_flag & MNT_SYMPERM) {
327 error = VOP_ACCESS(ndp->ni_vp, VEXEC, cnp->cn_cred,
328 cnp->cn_lwp);
329 if (error != 0)
330 break;
331 }
332 if (ndp->ni_pathlen > 1)
333 cp = PNBUF_GET();
334 else
335 cp = cnp->cn_pnbuf;
336 aiov.iov_base = cp;
337 aiov.iov_len = MAXPATHLEN;
338 auio.uio_iov = &aiov;
339 auio.uio_iovcnt = 1;
340 auio.uio_offset = 0;
341 auio.uio_rw = UIO_READ;
342 auio.uio_resid = MAXPATHLEN;
343 UIO_SETUP_SYSSPACE(&auio);
344 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
345 if (error) {
346 badlink:
347 if (ndp->ni_pathlen > 1)
348 PNBUF_PUT(cp);
349 break;
350 }
351 linklen = MAXPATHLEN - auio.uio_resid;
352 if (linklen == 0) {
353 error = ENOENT;
354 goto badlink;
355 }
356
357 /*
358 * Do symlink substitution, if appropriate, and
359 * check length for potential overflow.
360 */
361 if ((vfs_magiclinks &&
362 symlink_magic(cnp->cn_lwp->l_proc, cp, &linklen)) ||
363 (linklen + ndp->ni_pathlen >= MAXPATHLEN)) {
364 error = ENAMETOOLONG;
365 goto badlink;
366 }
367 if (ndp->ni_pathlen > 1) {
368 memcpy(cp + linklen, ndp->ni_next, ndp->ni_pathlen);
369 PNBUF_PUT(cnp->cn_pnbuf);
370 cnp->cn_pnbuf = cp;
371 } else
372 cnp->cn_pnbuf[linklen] = '\0';
373 ndp->ni_pathlen += linklen;
374 vput(ndp->ni_vp);
375 dp = ndp->ni_dvp;
376
377 /*
378 * Check if root directory should replace current directory.
379 */
380 if (cnp->cn_pnbuf[0] == '/') {
381 vput(dp);
382 dp = ndp->ni_rootdir;
383 VREF(dp);
384 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
385 }
386 }
387 KASSERT(ndp->ni_dvp != ndp->ni_vp);
388 vput(ndp->ni_dvp);
389 vput(ndp->ni_vp);
390 ndp->ni_vp = NULL;
391 PNBUF_PUT(cnp->cn_pnbuf);
392 return (error);
393 }
394
395 /*
396 * Determine the namei hash (for cn_hash) for name.
397 * If *ep != NULL, hash from name to ep-1.
398 * If *ep == NULL, hash from name until the first NUL or '/', and
399 * return the location of this termination character in *ep.
400 *
401 * This function returns an equivalent hash to the MI hash32_strn().
402 * The latter isn't used because in the *ep == NULL case, determining
403 * the length of the string to the first NUL or `/' and then calling
404 * hash32_strn() involves unnecessary double-handling of the data.
405 */
406 uint32_t
407 namei_hash(const char *name, const char **ep)
408 {
409 uint32_t hash;
410
411 hash = HASH32_STR_INIT;
412 if (*ep != NULL) {
413 for (; name < *ep; name++)
414 hash = hash * 33 + *(const uint8_t *)name;
415 } else {
416 for (; *name != '\0' && *name != '/'; name++)
417 hash = hash * 33 + *(const uint8_t *)name;
418 *ep = name;
419 }
420 return (hash + (hash >> 5));
421 }
422
423 /*
424 * Search a pathname.
425 * This is a very central and rather complicated routine.
426 *
427 * The pathname is pointed to by ni_ptr and is of length ni_pathlen.
428 * The starting directory is taken from ni_startdir. The pathname is
429 * descended until done, or a symbolic link is encountered. The variable
430 * ni_more is clear if the path is completed; it is set to one if a
431 * symbolic link needing interpretation is encountered.
432 *
433 * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
434 * whether the name is to be looked up, created, renamed, or deleted.
435 * When CREATE, RENAME, or DELETE is specified, information usable in
436 * creating, renaming, or deleting a directory entry may be calculated.
437 * If flag has LOCKPARENT or'ed into it, the parent directory is returned
438 * locked. If flag has WANTPARENT or'ed into it, the parent directory is
439 * returned unlocked. Otherwise the parent directory is not returned. If
440 * the target of the pathname exists and LOCKLEAF is or'ed into the flag
441 * the target is returned locked, otherwise it is returned unlocked.
442 * When creating or renaming and LOCKPARENT is specified, the target may not
443 * be ".". When deleting and LOCKPARENT is specified, the target may be ".".
444 *
445 * Overall outline of lookup:
446 *
447 * dirloop:
448 * identify next component of name at ndp->ni_ptr
449 * handle degenerate case where name is null string
450 * if .. and crossing mount points and on mounted filesys, find parent
451 * call VOP_LOOKUP routine for next component name
452 * directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set
453 * component vnode returned in ni_vp (if it exists), locked.
454 * if result vnode is mounted on and crossing mount points,
455 * find mounted on vnode
456 * if more components of name, do next level at dirloop
457 * return the answer in ni_vp, locked if LOCKLEAF set
458 * if LOCKPARENT set, return locked parent in ni_dvp
459 * if WANTPARENT set, return unlocked parent in ni_dvp
460 */
461 int
462 lookup(struct nameidata *ndp)
463 {
464 const char *cp; /* pointer into pathname argument */
465 struct vnode *dp = 0; /* the directory we are searching */
466 struct vnode *tdp; /* saved dp */
467 struct mount *mp; /* mount table entry */
468 int docache; /* == 0 do not cache last component */
469 int rdonly; /* lookup read-only flag bit */
470 int error = 0;
471 int slashes;
472 struct componentname *cnp = &ndp->ni_cnd;
473 struct lwp *l = cnp->cn_lwp;
474
475 /*
476 * Setup: break out flag bits into variables.
477 */
478 docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE;
479 if (cnp->cn_nameiop != CREATE)
480 docache = 0;
481 rdonly = cnp->cn_flags & RDONLY;
482 ndp->ni_dvp = NULL;
483 cnp->cn_flags &= ~ISSYMLINK;
484 dp = ndp->ni_startdir;
485 ndp->ni_startdir = NULLVP;
486
487 /*
488 * If we have a leading string of slashes, remove them, and just make
489 * sure the current node is a directory.
490 */
491 cp = cnp->cn_nameptr;
492 if (*cp == '/') {
493 do {
494 cp++;
495 } while (*cp == '/');
496 ndp->ni_pathlen -= cp - cnp->cn_nameptr;
497 cnp->cn_nameptr = cp;
498
499 if (dp->v_type != VDIR) {
500 error = ENOTDIR;
501 vput(dp);
502 goto bad;
503 }
504
505 /*
506 * If we've exhausted the path name, then just return the
507 * current node. If the caller requested the parent node (i.e.
508 * it's a CREATE, DELETE, or RENAME), and we don't have one
509 * (because this is the root directory), then we must fail.
510 */
511 if (cnp->cn_nameptr[0] == '\0') {
512 if (ndp->ni_dvp == NULL && cnp->cn_nameiop != LOOKUP) {
513 switch (cnp->cn_nameiop) {
514 case CREATE:
515 error = EEXIST;
516 break;
517 case DELETE:
518 case RENAME:
519 error = EBUSY;
520 break;
521 default:
522 KASSERT(0);
523 }
524 vput(dp);
525 goto bad;
526 }
527 ndp->ni_vp = dp;
528 cnp->cn_flags |= ISLASTCN;
529 goto terminal;
530 }
531 }
532
533 dirloop:
534 /*
535 * Search a new directory.
536 *
537 * The cn_hash value is for use by vfs_cache.
538 * The last component of the filename is left accessible via
539 * cnp->cn_nameptr for callers that need the name. Callers needing
540 * the name set the SAVENAME flag. When done, they assume
541 * responsibility for freeing the pathname buffer.
542 *
543 * At this point, our only vnode state is that "dp" is held and locked.
544 */
545 cnp->cn_consume = 0;
546 cp = NULL;
547 cnp->cn_hash = namei_hash(cnp->cn_nameptr, &cp);
548 cnp->cn_namelen = cp - cnp->cn_nameptr;
549 if (cnp->cn_namelen > NAME_MAX) {
550 vput(dp);
551 error = ENAMETOOLONG;
552 goto bad;
553 }
554 #ifdef NAMEI_DIAGNOSTIC
555 { char c = *cp;
556 *(char *)cp = '\0';
557 printf("{%s}: ", cnp->cn_nameptr);
558 *(char *)cp = c; }
559 #endif /* NAMEI_DIAGNOSTIC */
560 ndp->ni_pathlen -= cnp->cn_namelen;
561 ndp->ni_next = cp;
562 /*
563 * If this component is followed by a slash, then move the pointer to
564 * the next component forward, and remember that this component must be
565 * a directory.
566 */
567 if (*cp == '/') {
568 do {
569 cp++;
570 } while (*cp == '/');
571 slashes = cp - ndp->ni_next;
572 ndp->ni_pathlen -= slashes;
573 ndp->ni_next = cp;
574 cnp->cn_flags |= REQUIREDIR;
575 } else {
576 slashes = 0;
577 cnp->cn_flags &= ~REQUIREDIR;
578 }
579 /*
580 * We do special processing on the last component, whether or not it's
581 * a directory. Cache all intervening lookups, but not the final one.
582 */
583 if (*cp == '\0') {
584 if (docache)
585 cnp->cn_flags |= MAKEENTRY;
586 else
587 cnp->cn_flags &= ~MAKEENTRY;
588 cnp->cn_flags |= ISLASTCN;
589 } else {
590 cnp->cn_flags |= MAKEENTRY;
591 cnp->cn_flags &= ~ISLASTCN;
592 }
593 if (cnp->cn_namelen == 2 &&
594 cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.')
595 cnp->cn_flags |= ISDOTDOT;
596 else
597 cnp->cn_flags &= ~ISDOTDOT;
598
599 /*
600 * Handle "..": two special cases.
601 * 1. If at root directory (e.g. after chroot)
602 * or at absolute root directory
603 * then ignore it so can't get out.
604 * 1a. If we have somehow gotten out of a jail, warn
605 * and also ignore it so we can't get farther out.
606 * 2. If this vnode is the root of a mounted
607 * filesystem, then replace it with the
608 * vnode which was mounted on so we take the
609 * .. in the other file system.
610 */
611 if (cnp->cn_flags & ISDOTDOT) {
612 struct proc *p = l->l_proc;
613
614 for (;;) {
615 if (dp == ndp->ni_rootdir || dp == rootvnode) {
616 ndp->ni_dvp = dp;
617 ndp->ni_vp = dp;
618 VREF(dp);
619 goto nextname;
620 }
621 if (ndp->ni_rootdir != rootvnode) {
622 int retval;
623
624 VOP_UNLOCK(dp, 0);
625 retval = vn_isunder(dp, ndp->ni_rootdir, l);
626 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
627 if (!retval) {
628 /* Oops! We got out of jail! */
629 log(LOG_WARNING,
630 "chrooted pid %d uid %d (%s) "
631 "detected outside of its chroot\n",
632 p->p_pid, kauth_cred_geteuid(l->l_cred),
633 p->p_comm);
634 /* Put us at the jail root. */
635 vput(dp);
636 dp = ndp->ni_rootdir;
637 ndp->ni_dvp = dp;
638 ndp->ni_vp = dp;
639 VREF(dp);
640 VREF(dp);
641 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
642 goto nextname;
643 }
644 }
645 if ((dp->v_flag & VROOT) == 0 ||
646 (cnp->cn_flags & NOCROSSMOUNT))
647 break;
648 tdp = dp;
649 dp = dp->v_mount->mnt_vnodecovered;
650 vput(tdp);
651 VREF(dp);
652 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
653 }
654 }
655
656 /*
657 * We now have a segment name to search for, and a directory to search.
658 * Again, our only vnode state is that "dp" is held and locked.
659 */
660 unionlookup:
661 ndp->ni_dvp = dp;
662 ndp->ni_vp = NULL;
663 error = VOP_LOOKUP(dp, &ndp->ni_vp, cnp);
664 if (error != 0) {
665 #ifdef DIAGNOSTIC
666 if (ndp->ni_vp != NULL)
667 panic("leaf `%s' should be empty", cnp->cn_nameptr);
668 #endif /* DIAGNOSTIC */
669 #ifdef NAMEI_DIAGNOSTIC
670 printf("not found\n");
671 #endif /* NAMEI_DIAGNOSTIC */
672 if ((error == ENOENT) &&
673 (dp->v_flag & VROOT) &&
674 (dp->v_mount->mnt_flag & MNT_UNION)) {
675 tdp = dp;
676 dp = dp->v_mount->mnt_vnodecovered;
677 vput(tdp);
678 VREF(dp);
679 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
680 goto unionlookup;
681 }
682
683 if (error != EJUSTRETURN)
684 goto bad;
685
686 /*
687 * If this was not the last component, or there were trailing
688 * slashes, and we are not going to create a directory,
689 * then the name must exist.
690 */
691 if ((cnp->cn_flags & (REQUIREDIR | CREATEDIR)) == REQUIREDIR) {
692 error = ENOENT;
693 goto bad;
694 }
695
696 /*
697 * If creating and at end of pathname, then can consider
698 * allowing file to be created.
699 */
700 if (rdonly) {
701 error = EROFS;
702 goto bad;
703 }
704
705 /*
706 * We return with ni_vp NULL to indicate that the entry
707 * doesn't currently exist, leaving a pointer to the
708 * (possibly locked) directory vnode in ndp->ni_dvp.
709 */
710 if (cnp->cn_flags & SAVESTART) {
711 ndp->ni_startdir = ndp->ni_dvp;
712 VREF(ndp->ni_startdir);
713 }
714 return (0);
715 }
716 #ifdef NAMEI_DIAGNOSTIC
717 printf("found\n");
718 #endif /* NAMEI_DIAGNOSTIC */
719
720 /*
721 * Take into account any additional components consumed by the
722 * underlying filesystem. This will include any trailing slashes after
723 * the last component consumed.
724 */
725 if (cnp->cn_consume > 0) {
726 ndp->ni_pathlen -= cnp->cn_consume - slashes;
727 ndp->ni_next += cnp->cn_consume - slashes;
728 cnp->cn_consume = 0;
729 if (ndp->ni_next[0] == '\0')
730 cnp->cn_flags |= ISLASTCN;
731 }
732
733 dp = ndp->ni_vp;
734
735 /*
736 * "dp" and "ndp->ni_dvp" are both locked and held,
737 * and may be the same vnode.
738 */
739
740 /*
741 * Check to see if the vnode has been mounted on;
742 * if so find the root of the mounted file system.
743 */
744 while (dp->v_type == VDIR && (mp = dp->v_mountedhere) &&
745 (cnp->cn_flags & NOCROSSMOUNT) == 0) {
746 if (vfs_busy(mp, 0, 0))
747 continue;
748
749 KASSERT(ndp->ni_dvp != dp);
750 vput(dp);
751 error = VFS_ROOT(mp, &tdp);
752 vfs_unbusy(mp);
753 if (error) {
754 goto bad;
755 }
756 ndp->ni_vp = dp = tdp;
757 }
758
759 /*
760 * Check for symbolic link. Back up over any slashes that we skipped,
761 * as we will need them again.
762 */
763 if ((dp->v_type == VLNK) && (cnp->cn_flags & (FOLLOW|REQUIREDIR))) {
764 ndp->ni_pathlen += slashes;
765 ndp->ni_next -= slashes;
766 cnp->cn_flags |= ISSYMLINK;
767 return (0);
768 }
769
770 /*
771 * Check for directory, if the component was followed by a series of
772 * slashes.
773 */
774 if ((dp->v_type != VDIR) && (cnp->cn_flags & REQUIREDIR)) {
775 error = ENOTDIR;
776 KASSERT(dp != ndp->ni_dvp);
777 vput(dp);
778 goto bad;
779 }
780
781 nextname:
782
783 /*
784 * Not a symbolic link. If this was not the last component, then
785 * continue at the next component, else return.
786 */
787 if (!(cnp->cn_flags & ISLASTCN)) {
788 cnp->cn_nameptr = ndp->ni_next;
789 if (ndp->ni_dvp == dp) {
790 vrele(ndp->ni_dvp);
791 } else {
792 vput(ndp->ni_dvp);
793 }
794 goto dirloop;
795 }
796
797 terminal:
798
799 /*
800 * Disallow directory write attempts on read-only file systems.
801 */
802 if (rdonly &&
803 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
804
805 /*
806 * Disallow directory write attempts on read-only
807 * file systems.
808 */
809 error = EROFS;
810 if (dp != ndp->ni_dvp) {
811 vput(dp);
812 }
813 goto bad;
814 }
815 if (ndp->ni_dvp != NULL) {
816 if (cnp->cn_flags & SAVESTART) {
817 ndp->ni_startdir = ndp->ni_dvp;
818 VREF(ndp->ni_startdir);
819 }
820 }
821 if ((cnp->cn_flags & LOCKLEAF) == 0) {
822 VOP_UNLOCK(dp, 0);
823 }
824 return (0);
825
826 bad:
827 ndp->ni_vp = NULL;
828 return (error);
829 }
830
831 /*
832 * Reacquire a path name component.
833 * dvp is locked on entry and exit.
834 * *vpp is locked on exit unless it's NULL.
835 */
836 int
837 relookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp)
838 {
839 struct vnode *dp = 0; /* the directory we are searching */
840 int rdonly; /* lookup read-only flag bit */
841 int error = 0;
842 #ifdef DEBUG
843 u_long newhash; /* DEBUG: check name hash */
844 const char *cp; /* DEBUG: check name ptr/len */
845 #endif /* DEBUG */
846
847 /*
848 * Setup: break out flag bits into variables.
849 */
850 rdonly = cnp->cn_flags & RDONLY;
851 cnp->cn_flags &= ~ISSYMLINK;
852 dp = dvp;
853
854 /* dirloop: */
855 /*
856 * Search a new directory.
857 *
858 * The cn_hash value is for use by vfs_cache.
859 * The last component of the filename is left accessible via
860 * cnp->cn_nameptr for callers that need the name. Callers needing
861 * the name set the SAVENAME flag. When done, they assume
862 * responsibility for freeing the pathname buffer.
863 */
864 #ifdef DEBUG
865 cp = NULL;
866 newhash = namei_hash(cnp->cn_nameptr, &cp);
867 if (newhash != cnp->cn_hash)
868 panic("relookup: bad hash");
869 if (cnp->cn_namelen != cp - cnp->cn_nameptr)
870 panic("relookup: bad len");
871 while (*cp == '/')
872 cp++;
873 if (*cp != 0)
874 panic("relookup: not last component");
875 #endif /* DEBUG */
876 #ifdef NAMEI_DIAGNOSTIC
877 printf("{%s}: ", cnp->cn_nameptr);
878 #endif /* NAMEI_DIAGNOSTIC */
879
880 /*
881 * Check for degenerate name (e.g. / or "")
882 * which is a way of talking about a directory,
883 * e.g. like "/." or ".".
884 */
885 if (cnp->cn_nameptr[0] == '\0')
886 panic("relookup: null name");
887
888 if (cnp->cn_flags & ISDOTDOT)
889 panic("relookup: lookup on dot-dot");
890
891 /*
892 * We now have a segment name to search for, and a directory to search.
893 */
894 if ((error = VOP_LOOKUP(dp, vpp, cnp)) != 0) {
895 #ifdef DIAGNOSTIC
896 if (*vpp != NULL)
897 panic("leaf `%s' should be empty", cnp->cn_nameptr);
898 #endif
899 if (error != EJUSTRETURN)
900 goto bad;
901
902 /*
903 * If creating and at end of pathname, then can consider
904 * allowing file to be created.
905 */
906 if (rdonly) {
907 error = EROFS;
908 goto bad;
909 }
910
911 /* ASSERT(dvp == ndp->ni_startdir) */
912 if (cnp->cn_flags & SAVESTART)
913 VREF(dvp);
914
915 /*
916 * We return with ni_vp NULL to indicate that the entry
917 * doesn't currently exist, leaving a pointer to the
918 * locked directory vnode in ndp->ni_dvp.
919 */
920 return (0);
921 }
922 dp = *vpp;
923
924 #ifdef DIAGNOSTIC
925 /*
926 * Check for symbolic link
927 */
928 if (dp->v_type == VLNK && (cnp->cn_flags & FOLLOW))
929 panic("relookup: symlink found");
930 #endif
931
932 /*
933 * Check for read-only file systems.
934 */
935 if (rdonly &&
936 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
937 error = EROFS;
938 goto bad;
939 }
940 /* ASSERT(dvp == ndp->ni_startdir) */
941 if (cnp->cn_flags & SAVESTART)
942 VREF(dvp);
943 if ((cnp->cn_flags & LOCKLEAF) == 0)
944 VOP_UNLOCK(dp, 0);
945 return (0);
946
947 bad:
948 vput(dp);
949 *vpp = NULL;
950 return (error);
951 }
952