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