vfs_lookup.c revision 1.124 1 /* $NetBSD: vfs_lookup.c,v 1.124 2010/11/30 10:30:02 dholland 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.124 2010/11/30 10:30:02 dholland 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 KASSERT((state->cnp->cn_flags & HASBUF) == 0);
449 /*
450 * The buffer for name translation shall be the one inside the
451 * pathbuf.
452 */
453 state->ndp->ni_pnbuf = state->ndp->ni_pathbuf->pb_path;
454 }
455
456 /*
457 * Start up namei. Copy the path, find the root dir and cwd, establish
458 * the starting directory for lookup, and lock it.
459 */
460 static int
461 namei_start2(struct namei_state *state)
462 {
463 struct nameidata *ndp = state->ndp;
464 struct componentname *cnp = state->cnp;
465
466 struct cwdinfo *cwdi; /* pointer to cwd state */
467 struct lwp *self = curlwp; /* thread doing namei() */
468
469 #if 0 /* not any more */
470 /* as both buffers are size PATH_MAX this cannot overflow */
471 strcpy(cnp->cn_pnbuf, ndp->ni_pathbuf->pb_path);
472 #endif
473
474 /* length includes null terminator (was originally from copyinstr) */
475 ndp->ni_pathlen = strlen(ndp->ni_pnbuf) + 1;
476
477 /*
478 * POSIX.1 requirement: "" is not a valid file name.
479 */
480 if (ndp->ni_pathlen == 1) {
481 ndp->ni_vp = NULL;
482 return ENOENT;
483 }
484
485 ndp->ni_loopcnt = 0;
486
487 /*
488 * Get root directory for the translation.
489 */
490 cwdi = self->l_proc->p_cwdi;
491 rw_enter(&cwdi->cwdi_lock, RW_READER);
492 state->namei_startdir = cwdi->cwdi_rdir;
493 if (state->namei_startdir == NULL)
494 state->namei_startdir = rootvnode;
495 ndp->ni_rootdir = state->namei_startdir;
496
497 /*
498 * Check if starting from root directory or current directory.
499 */
500 if (ndp->ni_pnbuf[0] == '/') {
501 if (cnp->cn_flags & TRYEMULROOT) {
502 if (cnp->cn_flags & EMULROOTSET) {
503 /* Called from (eg) emul_find_interp() */
504 state->namei_startdir = ndp->ni_erootdir;
505 } else {
506 if (cwdi->cwdi_edir == NULL
507 || (ndp->ni_pnbuf[1] == '.'
508 && ndp->ni_pnbuf[2] == '.'
509 && ndp->ni_pnbuf[3] == '/')) {
510 ndp->ni_erootdir = NULL;
511 } else {
512 state->namei_startdir = cwdi->cwdi_edir;
513 ndp->ni_erootdir = state->namei_startdir;
514 }
515 }
516 } else {
517 ndp->ni_erootdir = NULL;
518 if (cnp->cn_flags & NOCHROOT)
519 state->namei_startdir = ndp->ni_rootdir = rootvnode;
520 }
521 } else {
522 state->namei_startdir = cwdi->cwdi_cdir;
523 ndp->ni_erootdir = NULL;
524 }
525 vref(state->namei_startdir);
526 rw_exit(&cwdi->cwdi_lock);
527
528 /*
529 * Ktrace it.
530 */
531 if (ktrpoint(KTR_NAMEI)) {
532 if (ndp->ni_erootdir != NULL) {
533 /*
534 * To make any sense, the trace entry need to have the
535 * text of the emulation path prepended.
536 * Usually we can get this from the current process,
537 * but when called from emul_find_interp() it is only
538 * in the exec_package - so we get it passed in ni_next
539 * (this is a hack).
540 */
541 const char *emul_path;
542 if (cnp->cn_flags & EMULROOTSET)
543 emul_path = ndp->ni_next;
544 else
545 emul_path = self->l_proc->p_emul->e_path;
546 ktrnamei2(emul_path, strlen(emul_path),
547 ndp->ni_pnbuf, ndp->ni_pathlen);
548 } else
549 ktrnamei(ndp->ni_pnbuf, ndp->ni_pathlen);
550 }
551
552 vn_lock(state->namei_startdir, LK_EXCLUSIVE | LK_RETRY);
553
554 return 0;
555 }
556
557 /*
558 * Undo namei_start: unlock and release the current lookup directory,
559 * and discard the path buffer.
560 */
561 static void
562 namei_end(struct namei_state *state)
563 {
564 vput(state->namei_startdir);
565 }
566
567 /*
568 * Check for being at a symlink.
569 */
570 static inline int
571 namei_atsymlink(struct namei_state *state)
572 {
573 return (state->cnp->cn_flags & ISSYMLINK) != 0;
574 }
575
576 /*
577 * Follow a symlink.
578 */
579 static inline int
580 namei_follow(struct namei_state *state)
581 {
582 struct nameidata *ndp = state->ndp;
583 struct componentname *cnp = state->cnp;
584
585 struct lwp *self = curlwp; /* thread doing namei() */
586 struct iovec aiov; /* uio for reading symbolic links */
587 struct uio auio;
588 char *cp; /* pointer into pathname argument */
589 size_t linklen;
590 int error;
591
592 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
593 return ELOOP;
594 }
595 if (ndp->ni_vp->v_mount->mnt_flag & MNT_SYMPERM) {
596 error = VOP_ACCESS(ndp->ni_vp, VEXEC, cnp->cn_cred);
597 if (error != 0)
598 return error;
599 }
600
601 /* FUTURE: fix this to not use a second buffer */
602 cp = PNBUF_GET();
603 aiov.iov_base = cp;
604 aiov.iov_len = MAXPATHLEN;
605 auio.uio_iov = &aiov;
606 auio.uio_iovcnt = 1;
607 auio.uio_offset = 0;
608 auio.uio_rw = UIO_READ;
609 auio.uio_resid = MAXPATHLEN;
610 UIO_SETUP_SYSSPACE(&auio);
611 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
612 if (error) {
613 PNBUF_PUT(cp);
614 return error;
615 }
616 linklen = MAXPATHLEN - auio.uio_resid;
617 if (linklen == 0) {
618 PNBUF_PUT(cp);
619 return ENOENT;
620 }
621
622 /*
623 * Do symlink substitution, if appropriate, and
624 * check length for potential overflow.
625 */
626 if ((vfs_magiclinks &&
627 symlink_magic(self->l_proc, cp, &linklen)) ||
628 (linklen + ndp->ni_pathlen >= MAXPATHLEN)) {
629 PNBUF_PUT(cp);
630 return ENAMETOOLONG;
631 }
632 if (ndp->ni_pathlen > 1) {
633 /* includes a null-terminator */
634 memcpy(cp + linklen, ndp->ni_next, ndp->ni_pathlen);
635 } else {
636 cp[linklen] = '\0';
637 }
638 ndp->ni_pathlen += linklen;
639 memcpy(ndp->ni_pnbuf, cp, ndp->ni_pathlen);
640 PNBUF_PUT(cp);
641 vput(ndp->ni_vp);
642 state->namei_startdir = ndp->ni_dvp;
643
644 /*
645 * Check if root directory should replace current directory.
646 */
647 if (ndp->ni_pnbuf[0] == '/') {
648 vput(state->namei_startdir);
649 /* Keep absolute symbolic links inside emulation root */
650 state->namei_startdir = ndp->ni_erootdir;
651 if (state->namei_startdir == NULL ||
652 (ndp->ni_pnbuf[1] == '.'
653 && ndp->ni_pnbuf[2] == '.'
654 && ndp->ni_pnbuf[3] == '/')) {
655 ndp->ni_erootdir = NULL;
656 state->namei_startdir = ndp->ni_rootdir;
657 }
658 vref(state->namei_startdir);
659 vn_lock(state->namei_startdir, LK_EXCLUSIVE | LK_RETRY);
660 }
661
662 return 0;
663 }
664
665 //////////////////////////////
666
667 static int
668 do_namei(struct namei_state *state)
669 {
670 int error;
671
672 struct nameidata *ndp = state->ndp;
673 struct componentname *cnp = state->cnp;
674 const char *savepath = NULL;
675
676 KASSERT(cnp == &ndp->ni_cnd);
677
678 namei_start1(state);
679
680 if (cnp->cn_flags & TRYEMULROOT) {
681 savepath = pathbuf_stringcopy_get(ndp->ni_pathbuf);
682 }
683
684 emul_retry:
685
686 if (savepath != NULL) {
687 /* kinda gross */
688 strcpy(ndp->ni_pathbuf->pb_path, savepath);
689 pathbuf_stringcopy_put(ndp->ni_pathbuf, savepath);
690 savepath = NULL;
691 }
692
693 error = namei_start2(state);
694 if (error) {
695 if (savepath != NULL) {
696 pathbuf_stringcopy_put(ndp->ni_pathbuf, savepath);
697 }
698 return error;
699 }
700
701 /* Loop through symbolic links */
702 for (;;) {
703 if (state->namei_startdir->v_mount == NULL) {
704 /* Give up if the directory is no longer mounted */
705 if (savepath != NULL) {
706 pathbuf_stringcopy_put(ndp->ni_pathbuf, savepath);
707 }
708 namei_end(state);
709 return (ENOENT);
710 }
711 cnp->cn_nameptr = ndp->ni_pnbuf;
712 ndp->ni_startdir = state->namei_startdir;
713 error = do_lookup(state);
714 if (error != 0) {
715 /* XXX this should use namei_end() */
716 if (ndp->ni_dvp) {
717 vput(ndp->ni_dvp);
718 }
719 if (ndp->ni_erootdir != NULL) {
720 /* Retry the whole thing from the normal root */
721 cnp->cn_flags &= ~TRYEMULROOT;
722 goto emul_retry;
723 }
724 KASSERT(savepath == NULL);
725 return (error);
726 }
727
728 /*
729 * Check for symbolic link
730 */
731 if (namei_atsymlink(state)) {
732 error = namei_follow(state);
733 if (error) {
734 KASSERT(ndp->ni_dvp != ndp->ni_vp);
735 vput(ndp->ni_dvp);
736 vput(ndp->ni_vp);
737 ndp->ni_vp = NULL;
738 if (savepath != NULL) {
739 pathbuf_stringcopy_put(ndp->ni_pathbuf, savepath);
740 }
741 return error;
742 }
743 }
744 else {
745 break;
746 }
747 }
748
749 /*
750 * Done
751 */
752
753 if ((cnp->cn_flags & LOCKPARENT) == 0 && ndp->ni_dvp) {
754 if (ndp->ni_dvp == ndp->ni_vp) {
755 vrele(ndp->ni_dvp);
756 } else {
757 vput(ndp->ni_dvp);
758 }
759 }
760
761 if (savepath != NULL) {
762 pathbuf_stringcopy_put(ndp->ni_pathbuf, savepath);
763 }
764
765 return 0;
766 }
767
768 int
769 namei(struct nameidata *ndp)
770 {
771 struct namei_state state;
772 int error;
773
774 namei_init(&state, ndp);
775 error = do_namei(&state);
776 namei_cleanup(&state);
777
778 return error;
779 }
780
781 /*
782 * Determine the namei hash (for cn_hash) for name.
783 * If *ep != NULL, hash from name to ep-1.
784 * If *ep == NULL, hash from name until the first NUL or '/', and
785 * return the location of this termination character in *ep.
786 *
787 * This function returns an equivalent hash to the MI hash32_strn().
788 * The latter isn't used because in the *ep == NULL case, determining
789 * the length of the string to the first NUL or `/' and then calling
790 * hash32_strn() involves unnecessary double-handling of the data.
791 */
792 uint32_t
793 namei_hash(const char *name, const char **ep)
794 {
795 uint32_t hash;
796
797 hash = HASH32_STR_INIT;
798 if (*ep != NULL) {
799 for (; name < *ep; name++)
800 hash = hash * 33 + *(const uint8_t *)name;
801 } else {
802 for (; *name != '\0' && *name != '/'; name++)
803 hash = hash * 33 + *(const uint8_t *)name;
804 *ep = name;
805 }
806 return (hash + (hash >> 5));
807 }
808
809 /*
810 * Search a pathname.
811 * This is a very central and rather complicated routine.
812 *
813 * The pathname is pointed to by ni_ptr and is of length ni_pathlen.
814 * The starting directory is taken from ni_startdir. The pathname is
815 * descended until done, or a symbolic link is encountered. The variable
816 * ni_more is clear if the path is completed; it is set to one if a
817 * symbolic link needing interpretation is encountered.
818 *
819 * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
820 * whether the name is to be looked up, created, renamed, or deleted.
821 * When CREATE, RENAME, or DELETE is specified, information usable in
822 * creating, renaming, or deleting a directory entry may be calculated.
823 * If flag has LOCKPARENT or'ed into it, the parent directory is returned
824 * locked. Otherwise the parent directory is not returned. If the target
825 * of the pathname exists and LOCKLEAF is or'ed into the flag the target
826 * is returned locked, otherwise it is returned unlocked. When creating
827 * or renaming and LOCKPARENT is specified, the target may not be ".".
828 * When deleting and LOCKPARENT is specified, the target may be ".".
829 *
830 * Overall outline of lookup:
831 *
832 * dirloop:
833 * identify next component of name at ndp->ni_ptr
834 * handle degenerate case where name is null string
835 * if .. and crossing mount points and on mounted filesys, find parent
836 * call VOP_LOOKUP routine for next component name
837 * directory vnode returned in ni_dvp, locked.
838 * component vnode returned in ni_vp (if it exists), locked.
839 * if result vnode is mounted on and crossing mount points,
840 * find mounted on vnode
841 * if more components of name, do next level at dirloop
842 * return the answer in ni_vp, locked if LOCKLEAF set
843 * if LOCKPARENT set, return locked parent in ni_dvp
844 */
845
846 /*
847 * Begin lookup().
848 */
849 static int
850 lookup_start(struct namei_state *state)
851 {
852 const char *cp; /* pointer into pathname argument */
853
854 struct componentname *cnp = state->cnp;
855 struct nameidata *ndp = state->ndp;
856
857 KASSERT(cnp == &ndp->ni_cnd);
858
859 state->lookup_alldone = 0;
860 state->dp = NULL;
861
862 /*
863 * Setup: break out flag bits into variables.
864 */
865 state->docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE;
866 if (cnp->cn_nameiop == DELETE)
867 state->docache = 0;
868 state->rdonly = cnp->cn_flags & RDONLY;
869 ndp->ni_dvp = NULL;
870 cnp->cn_flags &= ~ISSYMLINK;
871 state->dp = ndp->ni_startdir;
872 ndp->ni_startdir = NULLVP;
873
874 /*
875 * If we have a leading string of slashes, remove them, and just make
876 * sure the current node is a directory.
877 */
878 cp = cnp->cn_nameptr;
879 if (*cp == '/') {
880 do {
881 cp++;
882 } while (*cp == '/');
883 ndp->ni_pathlen -= cp - cnp->cn_nameptr;
884 cnp->cn_nameptr = cp;
885
886 if (state->dp->v_type != VDIR) {
887 vput(state->dp);
888 return ENOTDIR;
889 }
890
891 /*
892 * If we've exhausted the path name, then just return the
893 * current node.
894 */
895 if (cnp->cn_nameptr[0] == '\0') {
896 ndp->ni_vp = state->dp;
897 cnp->cn_flags |= ISLASTCN;
898
899 /* bleh */
900 state->lookup_alldone = 1;
901 return 0;
902 }
903 }
904
905 return 0;
906 }
907
908 static int
909 lookup_parsepath(struct namei_state *state)
910 {
911 const char *cp; /* pointer into pathname argument */
912
913 struct componentname *cnp = state->cnp;
914 struct nameidata *ndp = state->ndp;
915
916 KASSERT(cnp == &ndp->ni_cnd);
917
918 /*
919 * Search a new directory.
920 *
921 * The cn_hash value is for use by vfs_cache.
922 * The last component of the filename is left accessible via
923 * cnp->cn_nameptr for callers that need the name. Callers needing
924 * the name set the SAVENAME flag. When done, they assume
925 * responsibility for freeing the pathname buffer.
926 *
927 * At this point, our only vnode state is that "dp" is held and locked.
928 */
929 cnp->cn_consume = 0;
930 cp = NULL;
931 cnp->cn_hash = namei_hash(cnp->cn_nameptr, &cp);
932 cnp->cn_namelen = cp - cnp->cn_nameptr;
933 if (cnp->cn_namelen > NAME_MAX) {
934 vput(state->dp);
935 ndp->ni_dvp = NULL;
936 return ENAMETOOLONG;
937 }
938 #ifdef NAMEI_DIAGNOSTIC
939 { char c = *cp;
940 *(char *)cp = '\0';
941 printf("{%s}: ", cnp->cn_nameptr);
942 *(char *)cp = c; }
943 #endif /* NAMEI_DIAGNOSTIC */
944 ndp->ni_pathlen -= cnp->cn_namelen;
945 ndp->ni_next = cp;
946 /*
947 * If this component is followed by a slash, then move the pointer to
948 * the next component forward, and remember that this component must be
949 * a directory.
950 */
951 if (*cp == '/') {
952 do {
953 cp++;
954 } while (*cp == '/');
955 state->slashes = cp - ndp->ni_next;
956 ndp->ni_pathlen -= state->slashes;
957 ndp->ni_next = cp;
958 cnp->cn_flags |= REQUIREDIR;
959 } else {
960 state->slashes = 0;
961 cnp->cn_flags &= ~REQUIREDIR;
962 }
963 /*
964 * We do special processing on the last component, whether or not it's
965 * a directory. Cache all intervening lookups, but not the final one.
966 */
967 if (*cp == '\0') {
968 if (state->docache)
969 cnp->cn_flags |= MAKEENTRY;
970 else
971 cnp->cn_flags &= ~MAKEENTRY;
972 cnp->cn_flags |= ISLASTCN;
973 } else {
974 cnp->cn_flags |= MAKEENTRY;
975 cnp->cn_flags &= ~ISLASTCN;
976 }
977 if (cnp->cn_namelen == 2 &&
978 cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.')
979 cnp->cn_flags |= ISDOTDOT;
980 else
981 cnp->cn_flags &= ~ISDOTDOT;
982
983 return 0;
984 }
985
986 static int
987 lookup_once(struct namei_state *state)
988 {
989 struct vnode *tdp; /* saved dp */
990 struct mount *mp; /* mount table entry */
991 struct lwp *l = curlwp;
992 int error;
993
994 struct componentname *cnp = state->cnp;
995 struct nameidata *ndp = state->ndp;
996
997 KASSERT(cnp == &ndp->ni_cnd);
998
999 /*
1000 * Handle "..": two special cases.
1001 * 1. If at root directory (e.g. after chroot)
1002 * or at absolute root directory
1003 * then ignore it so can't get out.
1004 * 1a. If at the root of the emulation filesystem go to the real
1005 * root. So "/../<path>" is always absolute.
1006 * 1b. If we have somehow gotten out of a jail, warn
1007 * and also ignore it so we can't get farther out.
1008 * 2. If this vnode is the root of a mounted
1009 * filesystem, then replace it with the
1010 * vnode which was mounted on so we take the
1011 * .. in the other file system.
1012 */
1013 if (cnp->cn_flags & ISDOTDOT) {
1014 struct proc *p = l->l_proc;
1015
1016 for (;;) {
1017 if (state->dp == ndp->ni_rootdir || state->dp == rootvnode) {
1018 ndp->ni_dvp = state->dp;
1019 ndp->ni_vp = state->dp;
1020 vref(state->dp);
1021 return 0;
1022 }
1023 if (ndp->ni_rootdir != rootvnode) {
1024 int retval;
1025
1026 VOP_UNLOCK(state->dp);
1027 retval = vn_isunder(state->dp, ndp->ni_rootdir, l);
1028 vn_lock(state->dp, LK_EXCLUSIVE | LK_RETRY);
1029 if (!retval) {
1030 /* Oops! We got out of jail! */
1031 log(LOG_WARNING,
1032 "chrooted pid %d uid %d (%s) "
1033 "detected outside of its chroot\n",
1034 p->p_pid, kauth_cred_geteuid(l->l_cred),
1035 p->p_comm);
1036 /* Put us at the jail root. */
1037 vput(state->dp);
1038 state->dp = ndp->ni_rootdir;
1039 ndp->ni_dvp = state->dp;
1040 ndp->ni_vp = state->dp;
1041 vref(state->dp);
1042 vref(state->dp);
1043 vn_lock(state->dp, LK_EXCLUSIVE | LK_RETRY);
1044 return 0;
1045 }
1046 }
1047 if ((state->dp->v_vflag & VV_ROOT) == 0 ||
1048 (cnp->cn_flags & NOCROSSMOUNT))
1049 break;
1050 tdp = state->dp;
1051 state->dp = state->dp->v_mount->mnt_vnodecovered;
1052 vput(tdp);
1053 vref(state->dp);
1054 vn_lock(state->dp, LK_EXCLUSIVE | LK_RETRY);
1055 }
1056 }
1057
1058 /*
1059 * We now have a segment name to search for, and a directory to search.
1060 * Again, our only vnode state is that "dp" is held and locked.
1061 */
1062 unionlookup:
1063 ndp->ni_dvp = state->dp;
1064 ndp->ni_vp = NULL;
1065 error = VOP_LOOKUP(state->dp, &ndp->ni_vp, cnp);
1066 if (error != 0) {
1067 #ifdef DIAGNOSTIC
1068 if (ndp->ni_vp != NULL)
1069 panic("leaf `%s' should be empty", cnp->cn_nameptr);
1070 #endif /* DIAGNOSTIC */
1071 #ifdef NAMEI_DIAGNOSTIC
1072 printf("not found\n");
1073 #endif /* NAMEI_DIAGNOSTIC */
1074 if ((error == ENOENT) &&
1075 (state->dp->v_vflag & VV_ROOT) &&
1076 (state->dp->v_mount->mnt_flag & MNT_UNION)) {
1077 tdp = state->dp;
1078 state->dp = state->dp->v_mount->mnt_vnodecovered;
1079 vput(tdp);
1080 vref(state->dp);
1081 vn_lock(state->dp, LK_EXCLUSIVE | LK_RETRY);
1082 goto unionlookup;
1083 }
1084
1085 if (error != EJUSTRETURN)
1086 return error;
1087
1088 /*
1089 * If this was not the last component, or there were trailing
1090 * slashes, and we are not going to create a directory,
1091 * then the name must exist.
1092 */
1093 if ((cnp->cn_flags & (REQUIREDIR | CREATEDIR)) == REQUIREDIR) {
1094 return ENOENT;
1095 }
1096
1097 /*
1098 * If creating and at end of pathname, then can consider
1099 * allowing file to be created.
1100 */
1101 if (state->rdonly) {
1102 return EROFS;
1103 }
1104
1105 /*
1106 * We return with ni_vp NULL to indicate that the entry
1107 * doesn't currently exist, leaving a pointer to the
1108 * (possibly locked) directory vnode in ndp->ni_dvp.
1109 */
1110 if (cnp->cn_flags & SAVESTART) {
1111 ndp->ni_startdir = ndp->ni_dvp;
1112 vref(ndp->ni_startdir);
1113 }
1114 state->lookup_alldone = 1;
1115 return (0);
1116 }
1117 #ifdef NAMEI_DIAGNOSTIC
1118 printf("found\n");
1119 #endif /* NAMEI_DIAGNOSTIC */
1120
1121 /*
1122 * Take into account any additional components consumed by the
1123 * underlying filesystem. This will include any trailing slashes after
1124 * the last component consumed.
1125 */
1126 if (cnp->cn_consume > 0) {
1127 ndp->ni_pathlen -= cnp->cn_consume - state->slashes;
1128 ndp->ni_next += cnp->cn_consume - state->slashes;
1129 cnp->cn_consume = 0;
1130 if (ndp->ni_next[0] == '\0')
1131 cnp->cn_flags |= ISLASTCN;
1132 }
1133
1134 state->dp = ndp->ni_vp;
1135
1136 /*
1137 * "state->dp" and "ndp->ni_dvp" are both locked and held,
1138 * and may be the same vnode.
1139 */
1140
1141 /*
1142 * Check to see if the vnode has been mounted on;
1143 * if so find the root of the mounted file system.
1144 */
1145 while (state->dp->v_type == VDIR && (mp = state->dp->v_mountedhere) &&
1146 (cnp->cn_flags & NOCROSSMOUNT) == 0) {
1147 error = vfs_busy(mp, NULL);
1148 if (error != 0) {
1149 vput(state->dp);
1150 return error;
1151 }
1152 KASSERT(ndp->ni_dvp != state->dp);
1153 VOP_UNLOCK(ndp->ni_dvp);
1154 vput(state->dp);
1155 error = VFS_ROOT(mp, &tdp);
1156 vfs_unbusy(mp, false, NULL);
1157 if (error) {
1158 vn_lock(ndp->ni_dvp, LK_EXCLUSIVE | LK_RETRY);
1159 return error;
1160 }
1161 VOP_UNLOCK(tdp);
1162 ndp->ni_vp = state->dp = tdp;
1163 vn_lock(ndp->ni_dvp, LK_EXCLUSIVE | LK_RETRY);
1164 vn_lock(ndp->ni_vp, LK_EXCLUSIVE | LK_RETRY);
1165 }
1166
1167 return 0;
1168 }
1169
1170 static int
1171 do_lookup(struct namei_state *state)
1172 {
1173 int error = 0;
1174
1175 struct componentname *cnp = state->cnp;
1176 struct nameidata *ndp = state->ndp;
1177
1178 KASSERT(cnp == &ndp->ni_cnd);
1179
1180 error = lookup_start(state);
1181 if (error) {
1182 goto bad;
1183 }
1184 // XXX: this case should not be necessary given proper handling
1185 // of slashes elsewhere.
1186 if (state->lookup_alldone) {
1187 goto terminal;
1188 }
1189
1190 dirloop:
1191 error = lookup_parsepath(state);
1192 if (error) {
1193 goto bad;
1194 }
1195
1196 error = lookup_once(state);
1197 if (error) {
1198 goto bad;
1199 }
1200 // XXX ought to be able to avoid this case too
1201 if (state->lookup_alldone) {
1202 /* this should NOT be "goto terminal;" */
1203 return 0;
1204 }
1205
1206 /*
1207 * Check for symbolic link. Back up over any slashes that we skipped,
1208 * as we will need them again.
1209 */
1210 if ((state->dp->v_type == VLNK) && (cnp->cn_flags & (FOLLOW|REQUIREDIR))) {
1211 ndp->ni_pathlen += state->slashes;
1212 ndp->ni_next -= state->slashes;
1213 cnp->cn_flags |= ISSYMLINK;
1214 return (0);
1215 }
1216
1217 /*
1218 * Check for directory, if the component was followed by a series of
1219 * slashes.
1220 */
1221 if ((state->dp->v_type != VDIR) && (cnp->cn_flags & REQUIREDIR)) {
1222 error = ENOTDIR;
1223 KASSERT(state->dp != ndp->ni_dvp);
1224 vput(state->dp);
1225 goto bad;
1226 }
1227
1228 /*
1229 * Not a symbolic link. If this was not the last component, then
1230 * continue at the next component, else return.
1231 */
1232 if (!(cnp->cn_flags & ISLASTCN)) {
1233 cnp->cn_nameptr = ndp->ni_next;
1234 if (ndp->ni_dvp == state->dp) {
1235 vrele(ndp->ni_dvp);
1236 } else {
1237 vput(ndp->ni_dvp);
1238 }
1239 goto dirloop;
1240 }
1241
1242 terminal:
1243 if (state->dp == ndp->ni_erootdir) {
1244 /*
1245 * We are about to return the emulation root.
1246 * This isn't a good idea because code might repeatedly
1247 * lookup ".." until the file matches that returned
1248 * for "/" and loop forever.
1249 * So convert it to the real root.
1250 */
1251 if (ndp->ni_dvp == state->dp)
1252 vrele(state->dp);
1253 else
1254 if (ndp->ni_dvp != NULL)
1255 vput(ndp->ni_dvp);
1256 ndp->ni_dvp = NULL;
1257 vput(state->dp);
1258 state->dp = ndp->ni_rootdir;
1259 vref(state->dp);
1260 vn_lock(state->dp, LK_EXCLUSIVE | LK_RETRY);
1261 ndp->ni_vp = state->dp;
1262 }
1263
1264 /*
1265 * If the caller requested the parent node (i.e.
1266 * it's a CREATE, DELETE, or RENAME), and we don't have one
1267 * (because this is the root directory), then we must fail.
1268 */
1269 if (ndp->ni_dvp == NULL && cnp->cn_nameiop != LOOKUP) {
1270 switch (cnp->cn_nameiop) {
1271 case CREATE:
1272 error = EEXIST;
1273 break;
1274 case DELETE:
1275 case RENAME:
1276 error = EBUSY;
1277 break;
1278 default:
1279 KASSERT(0);
1280 }
1281 vput(state->dp);
1282 goto bad;
1283 }
1284
1285 /*
1286 * Disallow directory write attempts on read-only lookups.
1287 * Prefers EEXIST over EROFS for the CREATE case.
1288 */
1289 if (state->rdonly &&
1290 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
1291 error = EROFS;
1292 if (state->dp != ndp->ni_dvp) {
1293 vput(state->dp);
1294 }
1295 goto bad;
1296 }
1297 if (ndp->ni_dvp != NULL) {
1298 if (cnp->cn_flags & SAVESTART) {
1299 ndp->ni_startdir = ndp->ni_dvp;
1300 vref(ndp->ni_startdir);
1301 }
1302 }
1303 if ((cnp->cn_flags & LOCKLEAF) == 0) {
1304 VOP_UNLOCK(state->dp);
1305 }
1306 return (0);
1307
1308 bad:
1309 ndp->ni_vp = NULL;
1310 return (error);
1311 }
1312
1313 /*
1314 * Externally visible interfaces used by nfsd (bletch, yuk, XXX)
1315 *
1316 * The "index" version differs from the "main" version in that it's
1317 * called from a different place in a different context. For now I
1318 * want to be able to shuffle code in from one call site without
1319 * affecting the other.
1320 */
1321
1322 int
1323 lookup_for_nfsd(struct nameidata *ndp, struct vnode *dp, int neverfollow)
1324 {
1325 struct namei_state state;
1326 int error;
1327
1328 struct iovec aiov;
1329 struct uio auio;
1330 int linklen;
1331 char *cp;
1332
1333 ndp->ni_pnbuf = ndp->ni_pathbuf->pb_path;
1334 namei_init(&state, ndp);
1335
1336 /*
1337 * BEGIN wodge of code from nfsd
1338 */
1339
1340 vref(dp);
1341 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
1342
1343 for (;;) {
1344
1345 state.cnp->cn_nameptr = state.ndp->ni_pnbuf;
1346 state.ndp->ni_startdir = dp;
1347
1348 /*
1349 * END wodge of code from nfsd
1350 */
1351
1352 error = do_lookup(&state);
1353 if (error) {
1354 /* BEGIN from nfsd */
1355 if (ndp->ni_dvp) {
1356 vput(ndp->ni_dvp);
1357 }
1358 /* END from nfsd */
1359 namei_cleanup(&state);
1360 return error;
1361 }
1362
1363 /*
1364 * BEGIN wodge of code from nfsd
1365 */
1366
1367 /*
1368 * Check for encountering a symbolic link
1369 */
1370 if ((state.cnp->cn_flags & ISSYMLINK) == 0) {
1371 if ((state.cnp->cn_flags & LOCKPARENT) == 0 && state.ndp->ni_dvp) {
1372 if (state.ndp->ni_dvp == state.ndp->ni_vp) {
1373 vrele(state.ndp->ni_dvp);
1374 } else {
1375 vput(state.ndp->ni_dvp);
1376 }
1377 }
1378 return (0);
1379 } else {
1380 if (neverfollow) {
1381 error = EINVAL;
1382 goto out;
1383 }
1384 if (state.ndp->ni_loopcnt++ >= MAXSYMLINKS) {
1385 error = ELOOP;
1386 goto out;
1387 }
1388 if (state.ndp->ni_vp->v_mount->mnt_flag & MNT_SYMPERM) {
1389 error = VOP_ACCESS(ndp->ni_vp, VEXEC, state.cnp->cn_cred);
1390 if (error != 0)
1391 goto out;
1392 }
1393 cp = PNBUF_GET();
1394 aiov.iov_base = cp;
1395 aiov.iov_len = MAXPATHLEN;
1396 auio.uio_iov = &aiov;
1397 auio.uio_iovcnt = 1;
1398 auio.uio_offset = 0;
1399 auio.uio_rw = UIO_READ;
1400 auio.uio_resid = MAXPATHLEN;
1401 UIO_SETUP_SYSSPACE(&auio);
1402 error = VOP_READLINK(ndp->ni_vp, &auio, state.cnp->cn_cred);
1403 if (error) {
1404 PNBUF_PUT(cp);
1405 goto out;
1406 }
1407 linklen = MAXPATHLEN - auio.uio_resid;
1408 if (linklen == 0) {
1409 PNBUF_PUT(cp);
1410 error = ENOENT;
1411 goto out;
1412 }
1413 if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
1414 PNBUF_PUT(cp);
1415 error = ENAMETOOLONG;
1416 goto out;
1417 }
1418 if (ndp->ni_pathlen > 1) {
1419 /* includes a null-terminator */
1420 memcpy(cp + linklen, ndp->ni_next, ndp->ni_pathlen);
1421 } else {
1422 cp[linklen] = '\0';
1423 }
1424 state.ndp->ni_pathlen += linklen;
1425 memcpy(state.ndp->ni_pnbuf, cp, state.ndp->ni_pathlen);
1426 PNBUF_PUT(cp);
1427 vput(state.ndp->ni_vp);
1428 dp = state.ndp->ni_dvp;
1429
1430 /*
1431 * Check if root directory should replace current directory.
1432 */
1433 if (state.ndp->ni_pnbuf[0] == '/') {
1434 vput(dp);
1435 dp = ndp->ni_rootdir;
1436 vref(dp);
1437 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
1438 }
1439 }
1440
1441 }
1442 out:
1443 vput(state.ndp->ni_vp);
1444 vput(state.ndp->ni_dvp);
1445 state.ndp->ni_vp = NULL;
1446
1447 /*
1448 * END wodge of code from nfsd
1449 */
1450 namei_cleanup(&state);
1451
1452 return error;
1453 }
1454
1455 int
1456 lookup_for_nfsd_index(struct nameidata *ndp)
1457 {
1458 struct namei_state state;
1459 int error;
1460
1461 ndp->ni_pnbuf = ndp->ni_pathbuf->pb_path;
1462 ndp->ni_cnd.cn_nameptr = ndp->ni_pnbuf;
1463
1464 namei_init(&state, ndp);
1465 error = do_lookup(&state);
1466 namei_cleanup(&state);
1467
1468 return error;
1469 }
1470
1471 /*
1472 * Reacquire a path name component.
1473 * dvp is locked on entry and exit.
1474 * *vpp is locked on exit unless it's NULL.
1475 */
1476 int
1477 relookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp)
1478 {
1479 int rdonly; /* lookup read-only flag bit */
1480 int error = 0;
1481 #ifdef DEBUG
1482 uint32_t newhash; /* DEBUG: check name hash */
1483 const char *cp; /* DEBUG: check name ptr/len */
1484 #endif /* DEBUG */
1485
1486 /*
1487 * Setup: break out flag bits into variables.
1488 */
1489 rdonly = cnp->cn_flags & RDONLY;
1490 cnp->cn_flags &= ~ISSYMLINK;
1491
1492 /*
1493 * Search a new directory.
1494 *
1495 * The cn_hash value is for use by vfs_cache.
1496 * The last component of the filename is left accessible via
1497 * cnp->cn_nameptr for callers that need the name. Callers needing
1498 * the name set the SAVENAME flag. When done, they assume
1499 * responsibility for freeing the pathname buffer.
1500 */
1501 #ifdef DEBUG
1502 cp = NULL;
1503 newhash = namei_hash(cnp->cn_nameptr, &cp);
1504 if ((uint32_t)newhash != (uint32_t)cnp->cn_hash)
1505 panic("relookup: bad hash");
1506 if (cnp->cn_namelen != cp - cnp->cn_nameptr)
1507 panic("relookup: bad len");
1508 while (*cp == '/')
1509 cp++;
1510 if (*cp != 0)
1511 panic("relookup: not last component");
1512 #endif /* DEBUG */
1513
1514 /*
1515 * Check for degenerate name (e.g. / or "")
1516 * which is a way of talking about a directory,
1517 * e.g. like "/." or ".".
1518 */
1519 if (cnp->cn_nameptr[0] == '\0')
1520 panic("relookup: null name");
1521
1522 if (cnp->cn_flags & ISDOTDOT)
1523 panic("relookup: lookup on dot-dot");
1524
1525 /*
1526 * We now have a segment name to search for, and a directory to search.
1527 */
1528 if ((error = VOP_LOOKUP(dvp, vpp, cnp)) != 0) {
1529 #ifdef DIAGNOSTIC
1530 if (*vpp != NULL)
1531 panic("leaf `%s' should be empty", cnp->cn_nameptr);
1532 #endif
1533 if (error != EJUSTRETURN)
1534 goto bad;
1535 }
1536
1537 #ifdef DIAGNOSTIC
1538 /*
1539 * Check for symbolic link
1540 */
1541 if (*vpp && (*vpp)->v_type == VLNK && (cnp->cn_flags & FOLLOW))
1542 panic("relookup: symlink found");
1543 #endif
1544
1545 /*
1546 * Check for read-only lookups.
1547 */
1548 if (rdonly && cnp->cn_nameiop != LOOKUP) {
1549 error = EROFS;
1550 if (*vpp) {
1551 vput(*vpp);
1552 }
1553 goto bad;
1554 }
1555 if (cnp->cn_flags & SAVESTART)
1556 vref(dvp);
1557 return (0);
1558
1559 bad:
1560 *vpp = NULL;
1561 return (error);
1562 }
1563
1564 /*
1565 * namei_simple - simple forms of namei.
1566 *
1567 * These are wrappers to allow the simple case callers of namei to be
1568 * left alone while everything else changes under them.
1569 */
1570
1571 /* Flags */
1572 struct namei_simple_flags_type {
1573 int dummy;
1574 };
1575 static const struct namei_simple_flags_type ns_nn, ns_nt, ns_fn, ns_ft;
1576 const namei_simple_flags_t NSM_NOFOLLOW_NOEMULROOT = &ns_nn;
1577 const namei_simple_flags_t NSM_NOFOLLOW_TRYEMULROOT = &ns_nt;
1578 const namei_simple_flags_t NSM_FOLLOW_NOEMULROOT = &ns_fn;
1579 const namei_simple_flags_t NSM_FOLLOW_TRYEMULROOT = &ns_ft;
1580
1581 static
1582 int
1583 namei_simple_convert_flags(namei_simple_flags_t sflags)
1584 {
1585 if (sflags == NSM_NOFOLLOW_NOEMULROOT)
1586 return NOFOLLOW | 0;
1587 if (sflags == NSM_NOFOLLOW_TRYEMULROOT)
1588 return NOFOLLOW | TRYEMULROOT;
1589 if (sflags == NSM_FOLLOW_NOEMULROOT)
1590 return FOLLOW | 0;
1591 if (sflags == NSM_FOLLOW_TRYEMULROOT)
1592 return FOLLOW | TRYEMULROOT;
1593 panic("namei_simple_convert_flags: bogus sflags\n");
1594 return 0;
1595 }
1596
1597 int
1598 namei_simple_kernel(const char *path, namei_simple_flags_t sflags,
1599 struct vnode **vp_ret)
1600 {
1601 struct nameidata nd;
1602 struct pathbuf *pb;
1603 int err;
1604
1605 pb = pathbuf_create(path);
1606 if (pb == NULL) {
1607 return ENOMEM;
1608 }
1609
1610 NDINIT(&nd,
1611 LOOKUP,
1612 namei_simple_convert_flags(sflags),
1613 pb);
1614 err = namei(&nd);
1615 if (err != 0) {
1616 pathbuf_destroy(pb);
1617 return err;
1618 }
1619 *vp_ret = nd.ni_vp;
1620 pathbuf_destroy(pb);
1621 return 0;
1622 }
1623
1624 int
1625 namei_simple_user(const char *path, namei_simple_flags_t sflags,
1626 struct vnode **vp_ret)
1627 {
1628 struct pathbuf *pb;
1629 struct nameidata nd;
1630 int err;
1631
1632 err = pathbuf_copyin(path, &pb);
1633 if (err) {
1634 return err;
1635 }
1636
1637 NDINIT(&nd,
1638 LOOKUP,
1639 namei_simple_convert_flags(sflags),
1640 pb);
1641 err = namei(&nd);
1642 if (err != 0) {
1643 pathbuf_destroy(pb);
1644 return err;
1645 }
1646 *vp_ret = nd.ni_vp;
1647 pathbuf_destroy(pb);
1648 return 0;
1649 }
1650