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