vfs_lookup.c revision 1.74 1 /* $NetBSD: vfs_lookup.c,v 1.74 2006/12/13 06:36:35 chs 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.74 2006/12/13 06:36:35 chs 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 goto bad;
502 }
503
504 /*
505 * If we've exhausted the path name, then just return the
506 * current node. If the caller requested the parent node (i.e.
507 * it's a CREATE, DELETE, or RENAME), and we don't have one
508 * (because this is the root directory), then we must fail.
509 */
510 if (cnp->cn_nameptr[0] == '\0') {
511 if (ndp->ni_dvp == NULL && cnp->cn_nameiop != LOOKUP) {
512 switch (cnp->cn_nameiop) {
513 case CREATE:
514 error = EEXIST;
515 break;
516 case DELETE:
517 case RENAME:
518 error = EBUSY;
519 break;
520 default:
521 KASSERT(0);
522 }
523 vput(dp);
524 goto bad;
525 }
526 ndp->ni_vp = dp;
527 cnp->cn_flags |= ISLASTCN;
528 goto terminal;
529 }
530 }
531
532 dirloop:
533 /*
534 * Search a new directory.
535 *
536 * The cn_hash value is for use by vfs_cache.
537 * The last component of the filename is left accessible via
538 * cnp->cn_nameptr for callers that need the name. Callers needing
539 * the name set the SAVENAME flag. When done, they assume
540 * responsibility for freeing the pathname buffer.
541 *
542 * At this point, our only vnode state is that "dp" is held and locked.
543 */
544 cnp->cn_consume = 0;
545 cp = NULL;
546 cnp->cn_hash = namei_hash(cnp->cn_nameptr, &cp);
547 cnp->cn_namelen = cp - cnp->cn_nameptr;
548 if (cnp->cn_namelen > NAME_MAX) {
549 error = ENAMETOOLONG;
550 goto bad;
551 }
552 #ifdef NAMEI_DIAGNOSTIC
553 { char c = *cp;
554 *(char *)cp = '\0';
555 printf("{%s}: ", cnp->cn_nameptr);
556 *(char *)cp = c; }
557 #endif /* NAMEI_DIAGNOSTIC */
558 ndp->ni_pathlen -= cnp->cn_namelen;
559 ndp->ni_next = cp;
560 /*
561 * If this component is followed by a slash, then move the pointer to
562 * the next component forward, and remember that this component must be
563 * a directory.
564 */
565 if (*cp == '/') {
566 do {
567 cp++;
568 } while (*cp == '/');
569 slashes = cp - ndp->ni_next;
570 ndp->ni_pathlen -= slashes;
571 ndp->ni_next = cp;
572 cnp->cn_flags |= REQUIREDIR;
573 } else {
574 slashes = 0;
575 cnp->cn_flags &= ~REQUIREDIR;
576 }
577 /*
578 * We do special processing on the last component, whether or not it's
579 * a directory. Cache all intervening lookups, but not the final one.
580 */
581 if (*cp == '\0') {
582 if (docache)
583 cnp->cn_flags |= MAKEENTRY;
584 else
585 cnp->cn_flags &= ~MAKEENTRY;
586 cnp->cn_flags |= ISLASTCN;
587 } else {
588 cnp->cn_flags |= MAKEENTRY;
589 cnp->cn_flags &= ~ISLASTCN;
590 }
591 if (cnp->cn_namelen == 2 &&
592 cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.')
593 cnp->cn_flags |= ISDOTDOT;
594 else
595 cnp->cn_flags &= ~ISDOTDOT;
596
597 /*
598 * Handle "..": two special cases.
599 * 1. If at root directory (e.g. after chroot)
600 * or at absolute root directory
601 * then ignore it so can't get out.
602 * 1a. If we have somehow gotten out of a jail, warn
603 * and also ignore it so we can't get farther out.
604 * 2. If this vnode is the root of a mounted
605 * filesystem, then replace it with the
606 * vnode which was mounted on so we take the
607 * .. in the other file system.
608 */
609 if (cnp->cn_flags & ISDOTDOT) {
610 struct proc *p = l->l_proc;
611
612 for (;;) {
613 if (dp == ndp->ni_rootdir || dp == rootvnode) {
614 ndp->ni_dvp = dp;
615 ndp->ni_vp = dp;
616 VREF(dp);
617 goto nextname;
618 }
619 if (ndp->ni_rootdir != rootvnode) {
620 int retval;
621
622 VOP_UNLOCK(dp, 0);
623 retval = vn_isunder(dp, ndp->ni_rootdir, l);
624 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
625 if (!retval) {
626 /* Oops! We got out of jail! */
627 log(LOG_WARNING,
628 "chrooted pid %d uid %d (%s) "
629 "detected outside of its chroot\n",
630 p->p_pid, kauth_cred_geteuid(l->l_cred),
631 p->p_comm);
632 /* Put us at the jail root. */
633 vput(dp);
634 dp = ndp->ni_rootdir;
635 ndp->ni_dvp = dp;
636 ndp->ni_vp = dp;
637 VREF(dp);
638 VREF(dp);
639 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
640 goto nextname;
641 }
642 }
643 if ((dp->v_flag & VROOT) == 0 ||
644 (cnp->cn_flags & NOCROSSMOUNT))
645 break;
646 tdp = dp;
647 dp = dp->v_mount->mnt_vnodecovered;
648 vput(tdp);
649 VREF(dp);
650 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
651 }
652 }
653
654 /*
655 * We now have a segment name to search for, and a directory to search.
656 * Again, our only vnode state is that "dp" is held and locked.
657 */
658 unionlookup:
659 ndp->ni_dvp = dp;
660 ndp->ni_vp = NULL;
661 error = VOP_LOOKUP(dp, &ndp->ni_vp, cnp);
662 if (error != 0) {
663 #ifdef DIAGNOSTIC
664 if (ndp->ni_vp != NULL)
665 panic("leaf `%s' should be empty", cnp->cn_nameptr);
666 #endif /* DIAGNOSTIC */
667 #ifdef NAMEI_DIAGNOSTIC
668 printf("not found\n");
669 #endif /* NAMEI_DIAGNOSTIC */
670 if ((error == ENOENT) &&
671 (dp->v_flag & VROOT) &&
672 (dp->v_mount->mnt_flag & MNT_UNION)) {
673 tdp = dp;
674 dp = dp->v_mount->mnt_vnodecovered;
675 vput(tdp);
676 VREF(dp);
677 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
678 goto unionlookup;
679 }
680
681 if (error != EJUSTRETURN)
682 goto bad;
683
684 /*
685 * If this was not the last component, or there were trailing
686 * slashes, and we are not going to create a directory,
687 * then the name must exist.
688 */
689 if ((cnp->cn_flags & (REQUIREDIR | CREATEDIR)) == REQUIREDIR) {
690 error = ENOENT;
691 goto bad;
692 }
693
694 /*
695 * If creating and at end of pathname, then can consider
696 * allowing file to be created.
697 */
698 if (rdonly) {
699 error = EROFS;
700 goto bad;
701 }
702
703 /*
704 * We return with ni_vp NULL to indicate that the entry
705 * doesn't currently exist, leaving a pointer to the
706 * (possibly locked) directory vnode in ndp->ni_dvp.
707 */
708 if (cnp->cn_flags & SAVESTART) {
709 ndp->ni_startdir = ndp->ni_dvp;
710 VREF(ndp->ni_startdir);
711 }
712 return (0);
713 }
714 #ifdef NAMEI_DIAGNOSTIC
715 printf("found\n");
716 #endif /* NAMEI_DIAGNOSTIC */
717
718 /*
719 * Take into account any additional components consumed by the
720 * underlying filesystem. This will include any trailing slashes after
721 * the last component consumed.
722 */
723 if (cnp->cn_consume > 0) {
724 ndp->ni_pathlen -= cnp->cn_consume - slashes;
725 ndp->ni_next += cnp->cn_consume - slashes;
726 cnp->cn_consume = 0;
727 if (ndp->ni_next[0] == '\0')
728 cnp->cn_flags |= ISLASTCN;
729 }
730
731 dp = ndp->ni_vp;
732
733 /*
734 * "dp" and "ndp->ni_dvp" are both locked and held,
735 * and may be the same vnode.
736 */
737
738 /*
739 * Check to see if the vnode has been mounted on;
740 * if so find the root of the mounted file system.
741 */
742 while (dp->v_type == VDIR && (mp = dp->v_mountedhere) &&
743 (cnp->cn_flags & NOCROSSMOUNT) == 0) {
744 if (vfs_busy(mp, 0, 0))
745 continue;
746
747 KASSERT(ndp->ni_dvp != dp);
748 vput(dp);
749 error = VFS_ROOT(mp, &tdp);
750 vfs_unbusy(mp);
751 if (error) {
752 goto bad;
753 }
754 ndp->ni_vp = dp = tdp;
755 }
756
757 /*
758 * Check for symbolic link. Back up over any slashes that we skipped,
759 * as we will need them again.
760 */
761 if ((dp->v_type == VLNK) && (cnp->cn_flags & (FOLLOW|REQUIREDIR))) {
762 ndp->ni_pathlen += slashes;
763 ndp->ni_next -= slashes;
764 cnp->cn_flags |= ISSYMLINK;
765 return (0);
766 }
767
768 /*
769 * Check for directory, if the component was followed by a series of
770 * slashes.
771 */
772 if ((dp->v_type != VDIR) && (cnp->cn_flags & REQUIREDIR)) {
773 error = ENOTDIR;
774 KASSERT(dp != ndp->ni_dvp);
775 vput(dp);
776 goto bad;
777 }
778
779 nextname:
780
781 /*
782 * Not a symbolic link. If this was not the last component, then
783 * continue at the next component, else return.
784 */
785 if (!(cnp->cn_flags & ISLASTCN)) {
786 cnp->cn_nameptr = ndp->ni_next;
787 if (ndp->ni_dvp == dp) {
788 vrele(ndp->ni_dvp);
789 } else {
790 vput(ndp->ni_dvp);
791 }
792 goto dirloop;
793 }
794
795 terminal:
796
797 /*
798 * Disallow directory write attempts on read-only file systems.
799 */
800 if (rdonly &&
801 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
802
803 /*
804 * Disallow directory write attempts on read-only
805 * file systems.
806 */
807 error = EROFS;
808 if (dp != ndp->ni_dvp) {
809 vput(dp);
810 }
811 goto bad;
812 }
813 if (ndp->ni_dvp != NULL) {
814 if (cnp->cn_flags & SAVESTART) {
815 ndp->ni_startdir = ndp->ni_dvp;
816 VREF(ndp->ni_startdir);
817 }
818 }
819 if ((cnp->cn_flags & LOCKLEAF) == 0) {
820 VOP_UNLOCK(dp, 0);
821 }
822 return (0);
823
824 bad:
825 ndp->ni_vp = NULL;
826 return (error);
827 }
828
829 /*
830 * Reacquire a path name component.
831 * dvp is locked on entry and exit.
832 * *vpp is locked on exit unless it's NULL.
833 */
834 int
835 relookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp)
836 {
837 struct vnode *dp = 0; /* the directory we are searching */
838 int rdonly; /* lookup read-only flag bit */
839 int error = 0;
840 #ifdef DEBUG
841 u_long newhash; /* DEBUG: check name hash */
842 const char *cp; /* DEBUG: check name ptr/len */
843 #endif /* DEBUG */
844
845 /*
846 * Setup: break out flag bits into variables.
847 */
848 rdonly = cnp->cn_flags & RDONLY;
849 cnp->cn_flags &= ~ISSYMLINK;
850 dp = dvp;
851
852 /* dirloop: */
853 /*
854 * Search a new directory.
855 *
856 * The cn_hash value is for use by vfs_cache.
857 * The last component of the filename is left accessible via
858 * cnp->cn_nameptr for callers that need the name. Callers needing
859 * the name set the SAVENAME flag. When done, they assume
860 * responsibility for freeing the pathname buffer.
861 */
862 #ifdef DEBUG
863 cp = NULL;
864 newhash = namei_hash(cnp->cn_nameptr, &cp);
865 if (newhash != cnp->cn_hash)
866 panic("relookup: bad hash");
867 if (cnp->cn_namelen != cp - cnp->cn_nameptr)
868 panic("relookup: bad len");
869 while (*cp == '/')
870 cp++;
871 if (*cp != 0)
872 panic("relookup: not last component");
873 #endif /* DEBUG */
874 #ifdef NAMEI_DIAGNOSTIC
875 printf("{%s}: ", cnp->cn_nameptr);
876 #endif /* NAMEI_DIAGNOSTIC */
877
878 /*
879 * Check for degenerate name (e.g. / or "")
880 * which is a way of talking about a directory,
881 * e.g. like "/." or ".".
882 */
883 if (cnp->cn_nameptr[0] == '\0')
884 panic("relookup: null name");
885
886 if (cnp->cn_flags & ISDOTDOT)
887 panic("relookup: lookup on dot-dot");
888
889 /*
890 * We now have a segment name to search for, and a directory to search.
891 */
892 if ((error = VOP_LOOKUP(dp, vpp, cnp)) != 0) {
893 #ifdef DIAGNOSTIC
894 if (*vpp != NULL)
895 panic("leaf `%s' should be empty", cnp->cn_nameptr);
896 #endif
897 if (error != EJUSTRETURN)
898 goto bad;
899
900 /*
901 * If creating and at end of pathname, then can consider
902 * allowing file to be created.
903 */
904 if (rdonly) {
905 error = EROFS;
906 goto bad;
907 }
908
909 /* ASSERT(dvp == ndp->ni_startdir) */
910 if (cnp->cn_flags & SAVESTART)
911 VREF(dvp);
912
913 /*
914 * We return with ni_vp NULL to indicate that the entry
915 * doesn't currently exist, leaving a pointer to the
916 * locked directory vnode in ndp->ni_dvp.
917 */
918 return (0);
919 }
920 dp = *vpp;
921
922 #ifdef DIAGNOSTIC
923 /*
924 * Check for symbolic link
925 */
926 if (dp->v_type == VLNK && (cnp->cn_flags & FOLLOW))
927 panic("relookup: symlink found");
928 #endif
929
930 /*
931 * Check for read-only file systems.
932 */
933 if (rdonly &&
934 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
935 error = EROFS;
936 goto bad;
937 }
938 /* ASSERT(dvp == ndp->ni_startdir) */
939 if (cnp->cn_flags & SAVESTART)
940 VREF(dvp);
941 if ((cnp->cn_flags & LOCKLEAF) == 0)
942 VOP_UNLOCK(dp, 0);
943 return (0);
944
945 bad:
946 vput(dp);
947 *vpp = NULL;
948 return (error);
949 }
950