vfs_lookup.c revision 1.127 1 /* $NetBSD: vfs_lookup.c,v 1.127 2010/12/20 00:12:46 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.127 2010/12/20 00:12:46 yamt Exp $");
41
42 #include "opt_magiclinks.h"
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/syslimits.h>
48 #include <sys/time.h>
49 #include <sys/namei.h>
50 #include <sys/vnode.h>
51 #include <sys/mount.h>
52 #include <sys/errno.h>
53 #include <sys/filedesc.h>
54 #include <sys/hash.h>
55 #include <sys/proc.h>
56 #include <sys/syslog.h>
57 #include <sys/kauth.h>
58 #include <sys/ktrace.h>
59
60 #ifndef MAGICLINKS
61 #define MAGICLINKS 0
62 #endif
63
64 int vfs_magiclinks = MAGICLINKS;
65
66 pool_cache_t pnbuf_cache; /* pathname buffer cache */
67
68 /*
69 * Substitute replacement text for 'magic' strings in symlinks.
70 * Returns 0 if successful, and returns non-zero if an error
71 * occurs. (Currently, the only possible error is running out
72 * of temporary pathname space.)
73 *
74 * Looks for "@<string>" and "@<string>/", where <string> is a
75 * recognized 'magic' string. Replaces the "@<string>" with the
76 * appropriate replacement text. (Note that in some cases the
77 * replacement text may have zero length.)
78 *
79 * This would have been table driven, but the variance in
80 * replacement strings (and replacement string lengths) made
81 * that impractical.
82 */
83 #define VNL(x) \
84 (sizeof(x) - 1)
85
86 #define VO '{'
87 #define VC '}'
88
89 #define MATCH(str) \
90 ((termchar == '/' && i + VNL(str) == *len) || \
91 (i + VNL(str) < *len && \
92 cp[i + VNL(str)] == termchar)) && \
93 !strncmp((str), &cp[i], VNL(str))
94
95 #define SUBSTITUTE(m, s, sl) \
96 if ((newlen + (sl)) >= MAXPATHLEN) \
97 return 1; \
98 i += VNL(m); \
99 if (termchar != '/') \
100 i++; \
101 (void)memcpy(&tmp[newlen], (s), (sl)); \
102 newlen += (sl); \
103 change = 1; \
104 termchar = '/';
105
106 static int
107 symlink_magic(struct proc *p, char *cp, size_t *len)
108 {
109 char *tmp;
110 size_t change, i, newlen, slen;
111 char termchar = '/';
112 char idtmp[11]; /* enough for 32 bit *unsigned* integer */
113
114
115 tmp = PNBUF_GET();
116 for (change = i = newlen = 0; i < *len; ) {
117 if (cp[i] != '@') {
118 tmp[newlen++] = cp[i++];
119 continue;
120 }
121
122 i++;
123
124 /* Check for @{var} syntax. */
125 if (cp[i] == VO) {
126 termchar = VC;
127 i++;
128 }
129
130 /*
131 * The following checks should be ordered according
132 * to frequency of use.
133 */
134 if (MATCH("machine_arch")) {
135 slen = VNL(MACHINE_ARCH);
136 SUBSTITUTE("machine_arch", MACHINE_ARCH, slen);
137 } else if (MATCH("machine")) {
138 slen = VNL(MACHINE);
139 SUBSTITUTE("machine", MACHINE, slen);
140 } else if (MATCH("hostname")) {
141 SUBSTITUTE("hostname", hostname, hostnamelen);
142 } else if (MATCH("osrelease")) {
143 slen = strlen(osrelease);
144 SUBSTITUTE("osrelease", osrelease, slen);
145 } else if (MATCH("emul")) {
146 slen = strlen(p->p_emul->e_name);
147 SUBSTITUTE("emul", p->p_emul->e_name, slen);
148 } else if (MATCH("kernel_ident")) {
149 slen = strlen(kernel_ident);
150 SUBSTITUTE("kernel_ident", kernel_ident, slen);
151 } else if (MATCH("domainname")) {
152 SUBSTITUTE("domainname", domainname, domainnamelen);
153 } else if (MATCH("ostype")) {
154 slen = strlen(ostype);
155 SUBSTITUTE("ostype", ostype, slen);
156 } else if (MATCH("uid")) {
157 slen = snprintf(idtmp, sizeof(idtmp), "%u",
158 kauth_cred_geteuid(kauth_cred_get()));
159 SUBSTITUTE("uid", idtmp, slen);
160 } else if (MATCH("ruid")) {
161 slen = snprintf(idtmp, sizeof(idtmp), "%u",
162 kauth_cred_getuid(kauth_cred_get()));
163 SUBSTITUTE("ruid", idtmp, slen);
164 } else if (MATCH("gid")) {
165 slen = snprintf(idtmp, sizeof(idtmp), "%u",
166 kauth_cred_getegid(kauth_cred_get()));
167 SUBSTITUTE("gid", idtmp, slen);
168 } else if (MATCH("rgid")) {
169 slen = snprintf(idtmp, sizeof(idtmp), "%u",
170 kauth_cred_getgid(kauth_cred_get()));
171 SUBSTITUTE("rgid", idtmp, slen);
172 } else {
173 tmp[newlen++] = '@';
174 if (termchar == VC)
175 tmp[newlen++] = VO;
176 }
177 }
178
179 if (change) {
180 (void)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
196 /*
197 * Sealed abstraction for pathnames.
198 *
199 * System-call-layer level code that is going to call namei should
200 * first create a pathbuf and adjust all the bells and whistles on it
201 * as needed by context
202 */
203
204 struct pathbuf {
205 char *pb_path;
206 char *pb_pathcopy;
207 unsigned pb_pathcopyuses;
208 };
209
210 static struct pathbuf *
211 pathbuf_create_raw(void)
212 {
213 struct pathbuf *pb;
214
215 pb = kmem_alloc(sizeof(*pb), KM_SLEEP);
216 if (pb == NULL) {
217 return NULL;
218 }
219 pb->pb_path = PNBUF_GET();
220 if (pb->pb_path == NULL) {
221 kmem_free(pb, sizeof(*pb));
222 return NULL;
223 }
224 pb->pb_pathcopy = NULL;
225 pb->pb_pathcopyuses = 0;
226 return pb;
227 }
228
229 void
230 pathbuf_destroy(struct pathbuf *pb)
231 {
232 KASSERT(pb->pb_pathcopyuses == 0);
233 KASSERT(pb->pb_pathcopy == NULL);
234 PNBUF_PUT(pb->pb_path);
235 kmem_free(pb, sizeof(*pb));
236 }
237
238 struct pathbuf *
239 pathbuf_assimilate(char *pnbuf)
240 {
241 struct pathbuf *pb;
242
243 pb = kmem_alloc(sizeof(*pb), KM_SLEEP);
244 if (pb == NULL) {
245 return NULL;
246 }
247 pb->pb_path = pnbuf;
248 pb->pb_pathcopy = NULL;
249 pb->pb_pathcopyuses = 0;
250 return pb;
251 }
252
253 struct pathbuf *
254 pathbuf_create(const char *path)
255 {
256 struct pathbuf *pb;
257 int error;
258
259 pb = pathbuf_create_raw();
260 if (pb == NULL) {
261 return NULL;
262 }
263 error = copystr(path, pb->pb_path, PATH_MAX, NULL);
264 if (error != 0) {
265 KASSERT(!"kernel path too long in pathbuf_create");
266 /* make sure it's null-terminated, just in case */
267 pb->pb_path[PATH_MAX-1] = '\0';
268 }
269 return pb;
270 }
271
272 int
273 pathbuf_copyin(const char *userpath, struct pathbuf **ret)
274 {
275 struct pathbuf *pb;
276 int error;
277
278 pb = pathbuf_create_raw();
279 if (pb == NULL) {
280 return ENOMEM;
281 }
282 error = copyinstr(userpath, pb->pb_path, PATH_MAX, NULL);
283 if (error) {
284 pathbuf_destroy(pb);
285 return error;
286 }
287 *ret = pb;
288 return 0;
289 }
290
291 /*
292 * XXX should not exist
293 */
294 int
295 pathbuf_maybe_copyin(const char *path, enum uio_seg seg, struct pathbuf **ret)
296 {
297 if (seg == UIO_USERSPACE) {
298 return pathbuf_copyin(path, ret);
299 } else {
300 *ret = pathbuf_create(path);
301 if (*ret == NULL) {
302 return ENOMEM;
303 }
304 return 0;
305 }
306 }
307
308 /*
309 * Get a copy of the path buffer as it currently exists. If this is
310 * called after namei starts the results may be arbitrary.
311 */
312 void
313 pathbuf_copystring(const struct pathbuf *pb, char *buf, size_t maxlen)
314 {
315 strlcpy(buf, pb->pb_path, maxlen);
316 }
317
318 /*
319 * These two functions allow access to a saved copy of the original
320 * path string. The first copy should be gotten before namei is
321 * called. Each copy that is gotten should be put back.
322 */
323
324 const char *
325 pathbuf_stringcopy_get(struct pathbuf *pb)
326 {
327 if (pb->pb_pathcopyuses == 0) {
328 pb->pb_pathcopy = PNBUF_GET();
329 strcpy(pb->pb_pathcopy, pb->pb_path);
330 }
331 pb->pb_pathcopyuses++;
332 return pb->pb_pathcopy;
333 }
334
335 void
336 pathbuf_stringcopy_put(struct pathbuf *pb, const char *str)
337 {
338 KASSERT(str == pb->pb_pathcopy);
339 KASSERT(pb->pb_pathcopyuses > 0);
340 pb->pb_pathcopyuses--;
341 if (pb->pb_pathcopyuses == 0) {
342 PNBUF_PUT(pb->pb_pathcopy);
343 pb->pb_pathcopy = NULL;
344 }
345 }
346
347
348 ////////////////////////////////////////////////////////////
349
350 /*
351 * Convert a pathname into a pointer to a locked vnode.
352 *
353 * The FOLLOW flag is set when symbolic links are to be followed
354 * when they occur at the end of the name translation process.
355 * Symbolic links are always followed for all other pathname
356 * components other than the last.
357 *
358 * The segflg defines whether the name is to be copied from user
359 * space or kernel space.
360 *
361 * Overall outline of namei:
362 *
363 * copy in name
364 * get starting directory
365 * while (!done && !error) {
366 * call lookup to search path.
367 * if symbolic link, massage name in buffer and continue
368 * }
369 */
370
371 /*
372 * Internal state for a namei operation.
373 */
374 struct namei_state {
375 struct nameidata *ndp;
376 struct componentname *cnp;
377
378 /* used by the pieces of namei */
379 struct vnode *namei_startdir; /* The directory namei() starts from. */
380
381 /* used by the pieces of lookup */
382 int lookup_alldone;
383
384 int docache; /* == 0 do not cache last component */
385 int rdonly; /* lookup read-only flag bit */
386 struct vnode *dp; /* the directory we are searching */
387 int slashes;
388 };
389
390 /* XXX reorder things to make this decl unnecessary */
391 static int do_lookup(struct namei_state *state);
392
393
394 /*
395 * Initialize the namei working state.
396 */
397 static void
398 namei_init(struct namei_state *state, struct nameidata *ndp)
399 {
400 state->ndp = ndp;
401 state->cnp = &ndp->ni_cnd;
402
403 state->namei_startdir = NULL;
404
405 state->lookup_alldone = 0;
406
407 state->docache = 0;
408 state->rdonly = 0;
409 state->dp = NULL;
410 state->slashes = 0;
411 }
412
413 /*
414 * Clean up the working namei state, leaving things ready for return
415 * from namei.
416 */
417 static void
418 namei_cleanup(struct namei_state *state)
419 {
420 KASSERT(state->cnp == &state->ndp->ni_cnd);
421
422 //KASSERT(state->namei_startdir == NULL); // not yet
423
424 /* nothing for now */
425 (void)state;
426 }
427
428 //////////////////////////////
429
430 /*
431 * Start up namei. Early portion.
432 *
433 * This is divided from namei_start2 by the emul_retry: point.
434 */
435 static void
436 namei_start1(struct namei_state *state)
437 {
438
439 #ifdef DIAGNOSTIC
440 if (!state->cnp->cn_cred)
441 panic("namei: bad cred/proc");
442 if (state->cnp->cn_nameiop & (~OPMASK))
443 panic("namei: nameiop contaminated with flags");
444 if (state->cnp->cn_flags & OPMASK)
445 panic("namei: flags contaminated with nameiops");
446 #endif
447
448 /*
449 * The buffer for name translation shall be the one inside the
450 * pathbuf.
451 */
452 state->ndp->ni_pnbuf = state->ndp->ni_pathbuf->pb_path;
453 }
454
455 /*
456 * Start up namei. Copy the path, find the root dir and cwd, establish
457 * the starting directory for lookup, and lock it.
458 */
459 static int
460 namei_start2(struct namei_state *state)
461 {
462 struct nameidata *ndp = state->ndp;
463 struct componentname *cnp = state->cnp;
464
465 struct cwdinfo *cwdi; /* pointer to cwd state */
466 struct lwp *self = curlwp; /* thread doing namei() */
467
468 #if 0 /* not any more */
469 /* as both buffers are size PATH_MAX this cannot overflow */
470 strcpy(cnp->cn_pnbuf, ndp->ni_pathbuf->pb_path);
471 #endif
472
473 /* length includes null terminator (was originally from copyinstr) */
474 ndp->ni_pathlen = strlen(ndp->ni_pnbuf) + 1;
475
476 /*
477 * POSIX.1 requirement: "" is not a valid file name.
478 */
479 if (ndp->ni_pathlen == 1) {
480 ndp->ni_vp = NULL;
481 return ENOENT;
482 }
483
484 ndp->ni_loopcnt = 0;
485
486 /*
487 * Get root directory for the translation.
488 */
489 cwdi = self->l_proc->p_cwdi;
490 rw_enter(&cwdi->cwdi_lock, RW_READER);
491 state->namei_startdir = cwdi->cwdi_rdir;
492 if (state->namei_startdir == NULL)
493 state->namei_startdir = rootvnode;
494 ndp->ni_rootdir = state->namei_startdir;
495
496 /*
497 * Check if starting from root directory or current directory.
498 */
499 if (ndp->ni_pnbuf[0] == '/') {
500 if (cnp->cn_flags & TRYEMULROOT) {
501 if (cnp->cn_flags & EMULROOTSET) {
502 /* Called from (eg) emul_find_interp() */
503 state->namei_startdir = ndp->ni_erootdir;
504 } else {
505 if (cwdi->cwdi_edir == NULL
506 || (ndp->ni_pnbuf[1] == '.'
507 && ndp->ni_pnbuf[2] == '.'
508 && ndp->ni_pnbuf[3] == '/')) {
509 ndp->ni_erootdir = NULL;
510 } else {
511 state->namei_startdir = cwdi->cwdi_edir;
512 ndp->ni_erootdir = state->namei_startdir;
513 }
514 }
515 } else {
516 ndp->ni_erootdir = NULL;
517 if (cnp->cn_flags & NOCHROOT)
518 state->namei_startdir = ndp->ni_rootdir = rootvnode;
519 }
520 } else {
521 state->namei_startdir = cwdi->cwdi_cdir;
522 ndp->ni_erootdir = NULL;
523 }
524 vref(state->namei_startdir);
525 rw_exit(&cwdi->cwdi_lock);
526
527 /*
528 * Ktrace it.
529 */
530 if (ktrpoint(KTR_NAMEI)) {
531 if (ndp->ni_erootdir != NULL) {
532 /*
533 * To make any sense, the trace entry need to have the
534 * text of the emulation path prepended.
535 * Usually we can get this from the current process,
536 * but when called from emul_find_interp() it is only
537 * in the exec_package - so we get it passed in ni_next
538 * (this is a hack).
539 */
540 const char *emul_path;
541 if (cnp->cn_flags & EMULROOTSET)
542 emul_path = ndp->ni_next;
543 else
544 emul_path = self->l_proc->p_emul->e_path;
545 ktrnamei2(emul_path, strlen(emul_path),
546 ndp->ni_pnbuf, ndp->ni_pathlen);
547 } else
548 ktrnamei(ndp->ni_pnbuf, ndp->ni_pathlen);
549 }
550
551 vn_lock(state->namei_startdir, LK_EXCLUSIVE | LK_RETRY);
552
553 return 0;
554 }
555
556 /*
557 * Undo namei_start: unlock and release the current lookup directory,
558 * and discard the path buffer.
559 */
560 static void
561 namei_end(struct namei_state *state)
562 {
563 vput(state->namei_startdir);
564 }
565
566 /*
567 * Check for being at a symlink.
568 */
569 static inline int
570 namei_atsymlink(struct namei_state *state)
571 {
572 return (state->cnp->cn_flags & ISSYMLINK) != 0;
573 }
574
575 /*
576 * Follow a symlink.
577 */
578 static inline int
579 namei_follow(struct namei_state *state)
580 {
581 struct nameidata *ndp = state->ndp;
582 struct componentname *cnp = state->cnp;
583
584 struct lwp *self = curlwp; /* thread doing namei() */
585 struct iovec aiov; /* uio for reading symbolic links */
586 struct uio auio;
587 char *cp; /* pointer into pathname argument */
588 size_t linklen;
589 int error;
590
591 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
592 return ELOOP;
593 }
594 if (ndp->ni_vp->v_mount->mnt_flag & MNT_SYMPERM) {
595 error = VOP_ACCESS(ndp->ni_vp, VEXEC, cnp->cn_cred);
596 if (error != 0)
597 return error;
598 }
599
600 /* FUTURE: fix this to not use a second buffer */
601 cp = PNBUF_GET();
602 aiov.iov_base = cp;
603 aiov.iov_len = MAXPATHLEN;
604 auio.uio_iov = &aiov;
605 auio.uio_iovcnt = 1;
606 auio.uio_offset = 0;
607 auio.uio_rw = UIO_READ;
608 auio.uio_resid = MAXPATHLEN;
609 UIO_SETUP_SYSSPACE(&auio);
610 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
611 if (error) {
612 PNBUF_PUT(cp);
613 return error;
614 }
615 linklen = MAXPATHLEN - auio.uio_resid;
616 if (linklen == 0) {
617 PNBUF_PUT(cp);
618 return ENOENT;
619 }
620
621 /*
622 * Do symlink substitution, if appropriate, and
623 * check length for potential overflow.
624 */
625 if ((vfs_magiclinks &&
626 symlink_magic(self->l_proc, cp, &linklen)) ||
627 (linklen + ndp->ni_pathlen >= MAXPATHLEN)) {
628 PNBUF_PUT(cp);
629 return ENAMETOOLONG;
630 }
631 if (ndp->ni_pathlen > 1) {
632 /* includes a null-terminator */
633 memcpy(cp + linklen, ndp->ni_next, ndp->ni_pathlen);
634 } else {
635 cp[linklen] = '\0';
636 }
637 ndp->ni_pathlen += linklen;
638 memcpy(ndp->ni_pnbuf, cp, ndp->ni_pathlen);
639 PNBUF_PUT(cp);
640 vput(ndp->ni_vp);
641 state->namei_startdir = ndp->ni_dvp;
642
643 /*
644 * Check if root directory should replace current directory.
645 */
646 if (ndp->ni_pnbuf[0] == '/') {
647 vput(state->namei_startdir);
648 /* Keep absolute symbolic links inside emulation root */
649 state->namei_startdir = ndp->ni_erootdir;
650 if (state->namei_startdir == NULL ||
651 (ndp->ni_pnbuf[1] == '.'
652 && ndp->ni_pnbuf[2] == '.'
653 && ndp->ni_pnbuf[3] == '/')) {
654 ndp->ni_erootdir = NULL;
655 state->namei_startdir = ndp->ni_rootdir;
656 }
657 vref(state->namei_startdir);
658 vn_lock(state->namei_startdir, LK_EXCLUSIVE | LK_RETRY);
659 }
660
661 return 0;
662 }
663
664 //////////////////////////////
665
666 static int
667 do_namei(struct namei_state *state)
668 {
669 int error;
670
671 struct nameidata *ndp = state->ndp;
672 struct componentname *cnp = state->cnp;
673 const char *savepath = NULL;
674
675 KASSERT(cnp == &ndp->ni_cnd);
676
677 namei_start1(state);
678
679 if (cnp->cn_flags & TRYEMULROOT) {
680 savepath = pathbuf_stringcopy_get(ndp->ni_pathbuf);
681 }
682
683 emul_retry:
684
685 if (savepath != NULL) {
686 /* kinda gross */
687 strcpy(ndp->ni_pathbuf->pb_path, savepath);
688 pathbuf_stringcopy_put(ndp->ni_pathbuf, savepath);
689 savepath = NULL;
690 }
691
692 error = namei_start2(state);
693 if (error) {
694 if (savepath != NULL) {
695 pathbuf_stringcopy_put(ndp->ni_pathbuf, savepath);
696 }
697 return error;
698 }
699
700 /* Loop through symbolic links */
701 for (;;) {
702 if (state->namei_startdir->v_mount == NULL) {
703 /* Give up if the directory is no longer mounted */
704 if (savepath != NULL) {
705 pathbuf_stringcopy_put(ndp->ni_pathbuf, savepath);
706 }
707 namei_end(state);
708 return (ENOENT);
709 }
710 cnp->cn_nameptr = ndp->ni_pnbuf;
711 ndp->ni_startdir = state->namei_startdir;
712 error = do_lookup(state);
713 if (error != 0) {
714 /* XXX this should use namei_end() */
715 if (ndp->ni_dvp) {
716 vput(ndp->ni_dvp);
717 }
718 if (ndp->ni_erootdir != NULL) {
719 /* Retry the whole thing from the normal root */
720 cnp->cn_flags &= ~TRYEMULROOT;
721 goto emul_retry;
722 }
723 KASSERT(savepath == NULL);
724 return (error);
725 }
726
727 /*
728 * Check for symbolic link
729 */
730 if (namei_atsymlink(state)) {
731 error = namei_follow(state);
732 if (error) {
733 KASSERT(ndp->ni_dvp != ndp->ni_vp);
734 vput(ndp->ni_dvp);
735 vput(ndp->ni_vp);
736 ndp->ni_vp = NULL;
737 if (savepath != NULL) {
738 pathbuf_stringcopy_put(ndp->ni_pathbuf, savepath);
739 }
740 return error;
741 }
742 }
743 else {
744 break;
745 }
746 }
747
748 /*
749 * Done
750 */
751
752 if ((cnp->cn_flags & LOCKPARENT) == 0 && ndp->ni_dvp) {
753 if (ndp->ni_dvp == ndp->ni_vp) {
754 vrele(ndp->ni_dvp);
755 } else {
756 vput(ndp->ni_dvp);
757 }
758 }
759
760 if (savepath != NULL) {
761 pathbuf_stringcopy_put(ndp->ni_pathbuf, savepath);
762 }
763
764 return 0;
765 }
766
767 int
768 namei(struct nameidata *ndp)
769 {
770 struct namei_state state;
771 int error;
772
773 namei_init(&state, ndp);
774 error = do_namei(&state);
775 namei_cleanup(&state);
776
777 return error;
778 }
779
780 /*
781 * Determine the namei hash (for cn_hash) for name.
782 * If *ep != NULL, hash from name to ep-1.
783 * If *ep == NULL, hash from name until the first NUL or '/', and
784 * return the location of this termination character in *ep.
785 *
786 * This function returns an equivalent hash to the MI hash32_strn().
787 * The latter isn't used because in the *ep == NULL case, determining
788 * the length of the string to the first NUL or `/' and then calling
789 * hash32_strn() involves unnecessary double-handling of the data.
790 */
791 uint32_t
792 namei_hash(const char *name, const char **ep)
793 {
794 uint32_t hash;
795
796 hash = HASH32_STR_INIT;
797 if (*ep != NULL) {
798 for (; name < *ep; name++)
799 hash = hash * 33 + *(const uint8_t *)name;
800 } else {
801 for (; *name != '\0' && *name != '/'; name++)
802 hash = hash * 33 + *(const uint8_t *)name;
803 *ep = name;
804 }
805 return (hash + (hash >> 5));
806 }
807
808 /*
809 * Search a pathname.
810 * This is a very central and rather complicated routine.
811 *
812 * The pathname is pointed to by ni_ptr and is of length ni_pathlen.
813 * The starting directory is taken from ni_startdir. The pathname is
814 * descended until done, or a symbolic link is encountered. The variable
815 * ni_more is clear if the path is completed; it is set to one if a
816 * symbolic link needing interpretation is encountered.
817 *
818 * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
819 * whether the name is to be looked up, created, renamed, or deleted.
820 * When CREATE, RENAME, or DELETE is specified, information usable in
821 * creating, renaming, or deleting a directory entry may be calculated.
822 * If flag has LOCKPARENT or'ed into it, the parent directory is returned
823 * locked. Otherwise the parent directory is not returned. If the target
824 * of the pathname exists and LOCKLEAF is or'ed into the flag the target
825 * is returned locked, otherwise it is returned unlocked. When creating
826 * or renaming and LOCKPARENT is specified, the target may not be ".".
827 * When deleting and LOCKPARENT is specified, the target may be ".".
828 *
829 * Overall outline of lookup:
830 *
831 * dirloop:
832 * identify next component of name at ndp->ni_ptr
833 * handle degenerate case where name is null string
834 * if .. and crossing mount points and on mounted filesys, find parent
835 * call VOP_LOOKUP routine for next component name
836 * directory vnode returned in ni_dvp, locked.
837 * component vnode returned in ni_vp (if it exists), locked.
838 * if result vnode is mounted on and crossing mount points,
839 * find mounted on vnode
840 * if more components of name, do next level at dirloop
841 * return the answer in ni_vp, locked if LOCKLEAF set
842 * if LOCKPARENT set, return locked parent in ni_dvp
843 */
844
845 /*
846 * Begin lookup().
847 */
848 static int
849 lookup_start(struct namei_state *state)
850 {
851 const char *cp; /* pointer into pathname argument */
852
853 struct componentname *cnp = state->cnp;
854 struct nameidata *ndp = state->ndp;
855
856 KASSERT(cnp == &ndp->ni_cnd);
857
858 state->lookup_alldone = 0;
859 state->dp = NULL;
860
861 /*
862 * Setup: break out flag bits into variables.
863 */
864 state->docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE;
865 if (cnp->cn_nameiop == DELETE)
866 state->docache = 0;
867 state->rdonly = cnp->cn_flags & RDONLY;
868 ndp->ni_dvp = NULL;
869 cnp->cn_flags &= ~ISSYMLINK;
870 state->dp = ndp->ni_startdir;
871 ndp->ni_startdir = NULLVP;
872
873 /*
874 * If we have a leading string of slashes, remove them, and just make
875 * sure the current node is a directory.
876 */
877 cp = cnp->cn_nameptr;
878 if (*cp == '/') {
879 do {
880 cp++;
881 } while (*cp == '/');
882 ndp->ni_pathlen -= cp - cnp->cn_nameptr;
883 cnp->cn_nameptr = cp;
884
885 if (state->dp->v_type != VDIR) {
886 vput(state->dp);
887 return ENOTDIR;
888 }
889
890 /*
891 * If we've exhausted the path name, then just return the
892 * current node.
893 */
894 if (cnp->cn_nameptr[0] == '\0') {
895 ndp->ni_vp = state->dp;
896 cnp->cn_flags |= ISLASTCN;
897
898 /* bleh */
899 state->lookup_alldone = 1;
900 return 0;
901 }
902 }
903
904 return 0;
905 }
906
907 static int
908 lookup_parsepath(struct namei_state *state)
909 {
910 const char *cp; /* pointer into pathname argument */
911
912 struct componentname *cnp = state->cnp;
913 struct nameidata *ndp = state->ndp;
914
915 KASSERT(cnp == &ndp->ni_cnd);
916
917 /*
918 * Search a new directory.
919 *
920 * The cn_hash value is for use by vfs_cache.
921 * The last component of the filename is left accessible via
922 * cnp->cn_nameptr for callers that need the name. Callers needing
923 * the name set the SAVENAME flag. When done, they assume
924 * responsibility for freeing the pathname buffer.
925 *
926 * At this point, our only vnode state is that "dp" is held and locked.
927 */
928 cnp->cn_consume = 0;
929 cp = NULL;
930 cnp->cn_hash = namei_hash(cnp->cn_nameptr, &cp);
931 cnp->cn_namelen = cp - cnp->cn_nameptr;
932 if (cnp->cn_namelen > NAME_MAX) {
933 vput(state->dp);
934 ndp->ni_dvp = NULL;
935 return ENAMETOOLONG;
936 }
937 #ifdef NAMEI_DIAGNOSTIC
938 { char c = *cp;
939 *(char *)cp = '\0';
940 printf("{%s}: ", cnp->cn_nameptr);
941 *(char *)cp = c; }
942 #endif /* NAMEI_DIAGNOSTIC */
943 ndp->ni_pathlen -= cnp->cn_namelen;
944 ndp->ni_next = cp;
945 /*
946 * If this component is followed by a slash, then move the pointer to
947 * the next component forward, and remember that this component must be
948 * a directory.
949 */
950 if (*cp == '/') {
951 do {
952 cp++;
953 } while (*cp == '/');
954 state->slashes = cp - ndp->ni_next;
955 ndp->ni_pathlen -= state->slashes;
956 ndp->ni_next = cp;
957 cnp->cn_flags |= REQUIREDIR;
958 } else {
959 state->slashes = 0;
960 cnp->cn_flags &= ~REQUIREDIR;
961 }
962 /*
963 * We do special processing on the last component, whether or not it's
964 * a directory. Cache all intervening lookups, but not the final one.
965 */
966 if (*cp == '\0') {
967 if (state->docache)
968 cnp->cn_flags |= MAKEENTRY;
969 else
970 cnp->cn_flags &= ~MAKEENTRY;
971 cnp->cn_flags |= ISLASTCN;
972 } else {
973 cnp->cn_flags |= MAKEENTRY;
974 cnp->cn_flags &= ~ISLASTCN;
975 }
976 if (cnp->cn_namelen == 2 &&
977 cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.')
978 cnp->cn_flags |= ISDOTDOT;
979 else
980 cnp->cn_flags &= ~ISDOTDOT;
981
982 return 0;
983 }
984
985 static int
986 lookup_once(struct namei_state *state)
987 {
988 struct vnode *tdp; /* saved dp */
989 struct mount *mp; /* mount table entry */
990 struct lwp *l = curlwp;
991 int error;
992
993 struct componentname *cnp = state->cnp;
994 struct nameidata *ndp = state->ndp;
995
996 KASSERT(cnp == &ndp->ni_cnd);
997
998 /*
999 * Handle "..": two special cases.
1000 * 1. If at root directory (e.g. after chroot)
1001 * or at absolute root directory
1002 * then ignore it so can't get out.
1003 * 1a. If at the root of the emulation filesystem go to the real
1004 * root. So "/../<path>" is always absolute.
1005 * 1b. If we have somehow gotten out of a jail, warn
1006 * and also ignore it so we can't get farther out.
1007 * 2. If this vnode is the root of a mounted
1008 * filesystem, then replace it with the
1009 * vnode which was mounted on so we take the
1010 * .. in the other file system.
1011 */
1012 if (cnp->cn_flags & ISDOTDOT) {
1013 struct proc *p = l->l_proc;
1014
1015 for (;;) {
1016 if (state->dp == ndp->ni_rootdir || state->dp == rootvnode) {
1017 ndp->ni_dvp = state->dp;
1018 ndp->ni_vp = state->dp;
1019 vref(state->dp);
1020 return 0;
1021 }
1022 if (ndp->ni_rootdir != rootvnode) {
1023 int retval;
1024
1025 VOP_UNLOCK(state->dp);
1026 retval = vn_isunder(state->dp, ndp->ni_rootdir, l);
1027 vn_lock(state->dp, LK_EXCLUSIVE | LK_RETRY);
1028 if (!retval) {
1029 /* Oops! We got out of jail! */
1030 log(LOG_WARNING,
1031 "chrooted pid %d uid %d (%s) "
1032 "detected outside of its chroot\n",
1033 p->p_pid, kauth_cred_geteuid(l->l_cred),
1034 p->p_comm);
1035 /* Put us at the jail root. */
1036 vput(state->dp);
1037 state->dp = ndp->ni_rootdir;
1038 ndp->ni_dvp = state->dp;
1039 ndp->ni_vp = state->dp;
1040 vref(state->dp);
1041 vref(state->dp);
1042 vn_lock(state->dp, LK_EXCLUSIVE | LK_RETRY);
1043 return 0;
1044 }
1045 }
1046 if ((state->dp->v_vflag & VV_ROOT) == 0 ||
1047 (cnp->cn_flags & NOCROSSMOUNT))
1048 break;
1049 tdp = state->dp;
1050 state->dp = state->dp->v_mount->mnt_vnodecovered;
1051 vput(tdp);
1052 vref(state->dp);
1053 vn_lock(state->dp, LK_EXCLUSIVE | LK_RETRY);
1054 }
1055 }
1056
1057 /*
1058 * We now have a segment name to search for, and a directory to search.
1059 * Again, our only vnode state is that "dp" is held and locked.
1060 */
1061 unionlookup:
1062 ndp->ni_dvp = state->dp;
1063 ndp->ni_vp = NULL;
1064 error = VOP_LOOKUP(state->dp, &ndp->ni_vp, cnp);
1065 if (error != 0) {
1066 #ifdef DIAGNOSTIC
1067 if (ndp->ni_vp != NULL)
1068 panic("leaf `%s' should be empty", cnp->cn_nameptr);
1069 #endif /* DIAGNOSTIC */
1070 #ifdef NAMEI_DIAGNOSTIC
1071 printf("not found\n");
1072 #endif /* NAMEI_DIAGNOSTIC */
1073 if ((error == ENOENT) &&
1074 (state->dp->v_vflag & VV_ROOT) &&
1075 (state->dp->v_mount->mnt_flag & MNT_UNION)) {
1076 tdp = state->dp;
1077 state->dp = state->dp->v_mount->mnt_vnodecovered;
1078 vput(tdp);
1079 vref(state->dp);
1080 vn_lock(state->dp, LK_EXCLUSIVE | LK_RETRY);
1081 goto unionlookup;
1082 }
1083
1084 if (error != EJUSTRETURN)
1085 return error;
1086
1087 /*
1088 * If this was not the last component, or there were trailing
1089 * slashes, and we are not going to create a directory,
1090 * then the name must exist.
1091 */
1092 if ((cnp->cn_flags & (REQUIREDIR | CREATEDIR)) == REQUIREDIR) {
1093 return ENOENT;
1094 }
1095
1096 /*
1097 * If creating and at end of pathname, then can consider
1098 * allowing file to be created.
1099 */
1100 if (state->rdonly) {
1101 return EROFS;
1102 }
1103
1104 /*
1105 * We return with ni_vp NULL to indicate that the entry
1106 * doesn't currently exist, leaving a pointer to the
1107 * (possibly locked) directory vnode in ndp->ni_dvp.
1108 */
1109 if (cnp->cn_flags & SAVESTART) {
1110 ndp->ni_startdir = ndp->ni_dvp;
1111 vref(ndp->ni_startdir);
1112 }
1113 state->lookup_alldone = 1;
1114 return (0);
1115 }
1116 #ifdef NAMEI_DIAGNOSTIC
1117 printf("found\n");
1118 #endif /* NAMEI_DIAGNOSTIC */
1119
1120 /*
1121 * Take into account any additional components consumed by the
1122 * underlying filesystem. This will include any trailing slashes after
1123 * the last component consumed.
1124 */
1125 if (cnp->cn_consume > 0) {
1126 ndp->ni_pathlen -= cnp->cn_consume - state->slashes;
1127 ndp->ni_next += cnp->cn_consume - state->slashes;
1128 cnp->cn_consume = 0;
1129 if (ndp->ni_next[0] == '\0')
1130 cnp->cn_flags |= ISLASTCN;
1131 }
1132
1133 state->dp = ndp->ni_vp;
1134
1135 /*
1136 * "state->dp" and "ndp->ni_dvp" are both locked and held,
1137 * and may be the same vnode.
1138 */
1139
1140 /*
1141 * Check to see if the vnode has been mounted on;
1142 * if so find the root of the mounted file system.
1143 */
1144 while (state->dp->v_type == VDIR && (mp = state->dp->v_mountedhere) &&
1145 (cnp->cn_flags & NOCROSSMOUNT) == 0) {
1146 error = vfs_busy(mp, NULL);
1147 if (error != 0) {
1148 vput(state->dp);
1149 return error;
1150 }
1151 KASSERT(ndp->ni_dvp != state->dp);
1152 VOP_UNLOCK(ndp->ni_dvp);
1153 vput(state->dp);
1154 error = VFS_ROOT(mp, &tdp);
1155 vfs_unbusy(mp, false, NULL);
1156 if (error) {
1157 vn_lock(ndp->ni_dvp, LK_EXCLUSIVE | LK_RETRY);
1158 return error;
1159 }
1160 VOP_UNLOCK(tdp);
1161 ndp->ni_vp = state->dp = tdp;
1162 vn_lock(ndp->ni_dvp, LK_EXCLUSIVE | LK_RETRY);
1163 vn_lock(ndp->ni_vp, LK_EXCLUSIVE | LK_RETRY);
1164 }
1165
1166 return 0;
1167 }
1168
1169 static int
1170 do_lookup(struct namei_state *state)
1171 {
1172 int error = 0;
1173
1174 struct componentname *cnp = state->cnp;
1175 struct nameidata *ndp = state->ndp;
1176
1177 KASSERT(cnp == &ndp->ni_cnd);
1178
1179 error = lookup_start(state);
1180 if (error) {
1181 goto bad;
1182 }
1183 // XXX: this case should not be necessary given proper handling
1184 // of slashes elsewhere.
1185 if (state->lookup_alldone) {
1186 goto terminal;
1187 }
1188
1189 dirloop:
1190 error = lookup_parsepath(state);
1191 if (error) {
1192 goto bad;
1193 }
1194
1195 error = lookup_once(state);
1196 if (error) {
1197 goto bad;
1198 }
1199 // XXX ought to be able to avoid this case too
1200 if (state->lookup_alldone) {
1201 /* this should NOT be "goto terminal;" */
1202 return 0;
1203 }
1204
1205 /*
1206 * Check for symbolic link. Back up over any slashes that we skipped,
1207 * as we will need them again.
1208 */
1209 if ((state->dp->v_type == VLNK) && (cnp->cn_flags & (FOLLOW|REQUIREDIR))) {
1210 ndp->ni_pathlen += state->slashes;
1211 ndp->ni_next -= state->slashes;
1212 cnp->cn_flags |= ISSYMLINK;
1213 return (0);
1214 }
1215
1216 /*
1217 * Check for directory, if the component was followed by a series of
1218 * slashes.
1219 */
1220 if ((state->dp->v_type != VDIR) && (cnp->cn_flags & REQUIREDIR)) {
1221 error = ENOTDIR;
1222 KASSERT(state->dp != ndp->ni_dvp);
1223 vput(state->dp);
1224 goto bad;
1225 }
1226
1227 /*
1228 * Not a symbolic link. If this was not the last component, then
1229 * continue at the next component, else return.
1230 */
1231 if (!(cnp->cn_flags & ISLASTCN)) {
1232 cnp->cn_nameptr = ndp->ni_next;
1233 if (ndp->ni_dvp == state->dp) {
1234 vrele(ndp->ni_dvp);
1235 } else {
1236 vput(ndp->ni_dvp);
1237 }
1238 goto dirloop;
1239 }
1240
1241 terminal:
1242 if (state->dp == ndp->ni_erootdir) {
1243 /*
1244 * We are about to return the emulation root.
1245 * This isn't a good idea because code might repeatedly
1246 * lookup ".." until the file matches that returned
1247 * for "/" and loop forever.
1248 * So convert it to the real root.
1249 */
1250 if (ndp->ni_dvp == state->dp)
1251 vrele(state->dp);
1252 else
1253 if (ndp->ni_dvp != NULL)
1254 vput(ndp->ni_dvp);
1255 ndp->ni_dvp = NULL;
1256 vput(state->dp);
1257 state->dp = ndp->ni_rootdir;
1258 vref(state->dp);
1259 vn_lock(state->dp, LK_EXCLUSIVE | LK_RETRY);
1260 ndp->ni_vp = state->dp;
1261 }
1262
1263 /*
1264 * If the caller requested the parent node (i.e.
1265 * it's a CREATE, DELETE, or RENAME), and we don't have one
1266 * (because this is the root directory), then we must fail.
1267 */
1268 if (ndp->ni_dvp == NULL && cnp->cn_nameiop != LOOKUP) {
1269 switch (cnp->cn_nameiop) {
1270 case CREATE:
1271 error = EEXIST;
1272 break;
1273 case DELETE:
1274 case RENAME:
1275 error = EBUSY;
1276 break;
1277 default:
1278 KASSERT(0);
1279 }
1280 vput(state->dp);
1281 goto bad;
1282 }
1283
1284 /*
1285 * Disallow directory write attempts on read-only lookups.
1286 * Prefers EEXIST over EROFS for the CREATE case.
1287 */
1288 if (state->rdonly &&
1289 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
1290 error = EROFS;
1291 if (state->dp != ndp->ni_dvp) {
1292 vput(state->dp);
1293 }
1294 goto bad;
1295 }
1296 if (ndp->ni_dvp != NULL) {
1297 if (cnp->cn_flags & SAVESTART) {
1298 ndp->ni_startdir = ndp->ni_dvp;
1299 vref(ndp->ni_startdir);
1300 }
1301 }
1302 if ((cnp->cn_flags & LOCKLEAF) == 0) {
1303 VOP_UNLOCK(state->dp);
1304 }
1305 return (0);
1306
1307 bad:
1308 ndp->ni_vp = NULL;
1309 return (error);
1310 }
1311
1312 /*
1313 * Externally visible interfaces used by nfsd (bletch, yuk, XXX)
1314 *
1315 * The "index" version differs from the "main" version in that it's
1316 * called from a different place in a different context. For now I
1317 * want to be able to shuffle code in from one call site without
1318 * affecting the other.
1319 */
1320
1321 int
1322 lookup_for_nfsd(struct nameidata *ndp, struct vnode *dp, int neverfollow)
1323 {
1324 struct namei_state state;
1325 int error;
1326
1327 struct iovec aiov;
1328 struct uio auio;
1329 int linklen;
1330 char *cp;
1331
1332 ndp->ni_pnbuf = ndp->ni_pathbuf->pb_path;
1333 namei_init(&state, ndp);
1334
1335 /*
1336 * BEGIN wodge of code from nfsd
1337 */
1338
1339 vref(dp);
1340 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
1341
1342 for (;;) {
1343
1344 state.cnp->cn_nameptr = state.ndp->ni_pnbuf;
1345 state.ndp->ni_startdir = dp;
1346
1347 /*
1348 * END wodge of code from nfsd
1349 */
1350
1351 error = do_lookup(&state);
1352 if (error) {
1353 /* BEGIN from nfsd */
1354 if (ndp->ni_dvp) {
1355 vput(ndp->ni_dvp);
1356 }
1357 /* END from nfsd */
1358 namei_cleanup(&state);
1359 return error;
1360 }
1361
1362 /*
1363 * BEGIN wodge of code from nfsd
1364 */
1365
1366 /*
1367 * Check for encountering a symbolic link
1368 */
1369 if ((state.cnp->cn_flags & ISSYMLINK) == 0) {
1370 if ((state.cnp->cn_flags & LOCKPARENT) == 0 && state.ndp->ni_dvp) {
1371 if (state.ndp->ni_dvp == state.ndp->ni_vp) {
1372 vrele(state.ndp->ni_dvp);
1373 } else {
1374 vput(state.ndp->ni_dvp);
1375 }
1376 }
1377 return (0);
1378 } else {
1379 if (neverfollow) {
1380 error = EINVAL;
1381 goto out;
1382 }
1383 if (state.ndp->ni_loopcnt++ >= MAXSYMLINKS) {
1384 error = ELOOP;
1385 goto out;
1386 }
1387 if (state.ndp->ni_vp->v_mount->mnt_flag & MNT_SYMPERM) {
1388 error = VOP_ACCESS(ndp->ni_vp, VEXEC, state.cnp->cn_cred);
1389 if (error != 0)
1390 goto out;
1391 }
1392 cp = PNBUF_GET();
1393 aiov.iov_base = cp;
1394 aiov.iov_len = MAXPATHLEN;
1395 auio.uio_iov = &aiov;
1396 auio.uio_iovcnt = 1;
1397 auio.uio_offset = 0;
1398 auio.uio_rw = UIO_READ;
1399 auio.uio_resid = MAXPATHLEN;
1400 UIO_SETUP_SYSSPACE(&auio);
1401 error = VOP_READLINK(ndp->ni_vp, &auio, state.cnp->cn_cred);
1402 if (error) {
1403 PNBUF_PUT(cp);
1404 goto out;
1405 }
1406 linklen = MAXPATHLEN - auio.uio_resid;
1407 if (linklen == 0) {
1408 PNBUF_PUT(cp);
1409 error = ENOENT;
1410 goto out;
1411 }
1412 if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
1413 PNBUF_PUT(cp);
1414 error = ENAMETOOLONG;
1415 goto out;
1416 }
1417 if (ndp->ni_pathlen > 1) {
1418 /* includes a null-terminator */
1419 memcpy(cp + linklen, ndp->ni_next, ndp->ni_pathlen);
1420 } else {
1421 cp[linklen] = '\0';
1422 }
1423 state.ndp->ni_pathlen += linklen;
1424 memcpy(state.ndp->ni_pnbuf, cp, state.ndp->ni_pathlen);
1425 PNBUF_PUT(cp);
1426 vput(state.ndp->ni_vp);
1427 dp = state.ndp->ni_dvp;
1428
1429 /*
1430 * Check if root directory should replace current directory.
1431 */
1432 if (state.ndp->ni_pnbuf[0] == '/') {
1433 vput(dp);
1434 dp = ndp->ni_rootdir;
1435 vref(dp);
1436 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
1437 }
1438 }
1439
1440 }
1441 out:
1442 vput(state.ndp->ni_vp);
1443 vput(state.ndp->ni_dvp);
1444 state.ndp->ni_vp = NULL;
1445
1446 /*
1447 * END wodge of code from nfsd
1448 */
1449 namei_cleanup(&state);
1450
1451 return error;
1452 }
1453
1454 int
1455 lookup_for_nfsd_index(struct nameidata *ndp)
1456 {
1457 struct namei_state state;
1458 int error;
1459
1460 ndp->ni_pnbuf = ndp->ni_pathbuf->pb_path;
1461 ndp->ni_cnd.cn_nameptr = ndp->ni_pnbuf;
1462
1463 namei_init(&state, ndp);
1464 error = do_lookup(&state);
1465 namei_cleanup(&state);
1466
1467 return error;
1468 }
1469
1470 /*
1471 * Reacquire a path name component.
1472 * dvp is locked on entry and exit.
1473 * *vpp is locked on exit unless it's NULL.
1474 */
1475 int
1476 relookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp)
1477 {
1478 int rdonly; /* lookup read-only flag bit */
1479 int error = 0;
1480 #ifdef DEBUG
1481 uint32_t newhash; /* DEBUG: check name hash */
1482 const char *cp; /* DEBUG: check name ptr/len */
1483 #endif /* DEBUG */
1484
1485 /*
1486 * Setup: break out flag bits into variables.
1487 */
1488 rdonly = cnp->cn_flags & RDONLY;
1489 cnp->cn_flags &= ~ISSYMLINK;
1490
1491 /*
1492 * Search a new directory.
1493 *
1494 * The cn_hash value is for use by vfs_cache.
1495 * The last component of the filename is left accessible via
1496 * cnp->cn_nameptr for callers that need the name. Callers needing
1497 * the name set the SAVENAME flag. When done, they assume
1498 * responsibility for freeing the pathname buffer.
1499 */
1500 #ifdef DEBUG
1501 cp = NULL;
1502 newhash = namei_hash(cnp->cn_nameptr, &cp);
1503 if ((uint32_t)newhash != (uint32_t)cnp->cn_hash)
1504 panic("relookup: bad hash");
1505 if (cnp->cn_namelen != cp - cnp->cn_nameptr)
1506 panic("relookup: bad len");
1507 while (*cp == '/')
1508 cp++;
1509 if (*cp != 0)
1510 panic("relookup: not last component");
1511 #endif /* DEBUG */
1512
1513 /*
1514 * Check for degenerate name (e.g. / or "")
1515 * which is a way of talking about a directory,
1516 * e.g. like "/." or ".".
1517 */
1518 if (cnp->cn_nameptr[0] == '\0')
1519 panic("relookup: null name");
1520
1521 if (cnp->cn_flags & ISDOTDOT)
1522 panic("relookup: lookup on dot-dot");
1523
1524 /*
1525 * We now have a segment name to search for, and a directory to search.
1526 */
1527 if ((error = VOP_LOOKUP(dvp, vpp, cnp)) != 0) {
1528 #ifdef DIAGNOSTIC
1529 if (*vpp != NULL)
1530 panic("leaf `%s' should be empty", cnp->cn_nameptr);
1531 #endif
1532 if (error != EJUSTRETURN)
1533 goto bad;
1534 }
1535
1536 #ifdef DIAGNOSTIC
1537 /*
1538 * Check for symbolic link
1539 */
1540 if (*vpp && (*vpp)->v_type == VLNK && (cnp->cn_flags & FOLLOW))
1541 panic("relookup: symlink found");
1542 #endif
1543
1544 /*
1545 * Check for read-only lookups.
1546 */
1547 if (rdonly && cnp->cn_nameiop != LOOKUP) {
1548 error = EROFS;
1549 if (*vpp) {
1550 vput(*vpp);
1551 }
1552 goto bad;
1553 }
1554 if (cnp->cn_flags & SAVESTART)
1555 vref(dvp);
1556 return (0);
1557
1558 bad:
1559 *vpp = NULL;
1560 return (error);
1561 }
1562
1563 /*
1564 * namei_simple - simple forms of namei.
1565 *
1566 * These are wrappers to allow the simple case callers of namei to be
1567 * left alone while everything else changes under them.
1568 */
1569
1570 /* Flags */
1571 struct namei_simple_flags_type {
1572 int dummy;
1573 };
1574 static const struct namei_simple_flags_type ns_nn, ns_nt, ns_fn, ns_ft;
1575 const namei_simple_flags_t NSM_NOFOLLOW_NOEMULROOT = &ns_nn;
1576 const namei_simple_flags_t NSM_NOFOLLOW_TRYEMULROOT = &ns_nt;
1577 const namei_simple_flags_t NSM_FOLLOW_NOEMULROOT = &ns_fn;
1578 const namei_simple_flags_t NSM_FOLLOW_TRYEMULROOT = &ns_ft;
1579
1580 static
1581 int
1582 namei_simple_convert_flags(namei_simple_flags_t sflags)
1583 {
1584 if (sflags == NSM_NOFOLLOW_NOEMULROOT)
1585 return NOFOLLOW | 0;
1586 if (sflags == NSM_NOFOLLOW_TRYEMULROOT)
1587 return NOFOLLOW | TRYEMULROOT;
1588 if (sflags == NSM_FOLLOW_NOEMULROOT)
1589 return FOLLOW | 0;
1590 if (sflags == NSM_FOLLOW_TRYEMULROOT)
1591 return FOLLOW | TRYEMULROOT;
1592 panic("namei_simple_convert_flags: bogus sflags\n");
1593 return 0;
1594 }
1595
1596 int
1597 namei_simple_kernel(const char *path, namei_simple_flags_t sflags,
1598 struct vnode **vp_ret)
1599 {
1600 struct nameidata nd;
1601 struct pathbuf *pb;
1602 int err;
1603
1604 pb = pathbuf_create(path);
1605 if (pb == NULL) {
1606 return ENOMEM;
1607 }
1608
1609 NDINIT(&nd,
1610 LOOKUP,
1611 namei_simple_convert_flags(sflags),
1612 pb);
1613 err = namei(&nd);
1614 if (err != 0) {
1615 pathbuf_destroy(pb);
1616 return err;
1617 }
1618 *vp_ret = nd.ni_vp;
1619 pathbuf_destroy(pb);
1620 return 0;
1621 }
1622
1623 int
1624 namei_simple_user(const char *path, namei_simple_flags_t sflags,
1625 struct vnode **vp_ret)
1626 {
1627 struct pathbuf *pb;
1628 struct nameidata nd;
1629 int err;
1630
1631 err = pathbuf_copyin(path, &pb);
1632 if (err) {
1633 return err;
1634 }
1635
1636 NDINIT(&nd,
1637 LOOKUP,
1638 namei_simple_convert_flags(sflags),
1639 pb);
1640 err = namei(&nd);
1641 if (err != 0) {
1642 pathbuf_destroy(pb);
1643 return err;
1644 }
1645 *vp_ret = nd.ni_vp;
1646 pathbuf_destroy(pb);
1647 return 0;
1648 }
1649