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