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