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