vfs_lookup.c revision 1.231 1 /* $NetBSD: vfs_lookup.c,v 1.231 2022/02/10 10:59:12 hannken 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.231 2022/02/10 10:59:12 hannken Exp $");
41
42 #ifdef _KERNEL_OPT
43 #include "opt_magiclinks.h"
44 #endif
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/syslimits.h>
50 #include <sys/time.h>
51 #include <sys/namei.h>
52 #include <sys/vnode.h>
53 #include <sys/vnode_impl.h>
54 #include <sys/mount.h>
55 #include <sys/errno.h>
56 #include <sys/filedesc.h>
57 #include <sys/hash.h>
58 #include <sys/proc.h>
59 #include <sys/syslog.h>
60 #include <sys/kauth.h>
61 #include <sys/ktrace.h>
62 #include <sys/dirent.h>
63
64 #ifndef MAGICLINKS
65 #define MAGICLINKS 0
66 #endif
67
68 int vfs_magiclinks = MAGICLINKS;
69
70 __CTASSERT(MAXNAMLEN == NAME_MAX);
71
72 /*
73 * Substitute replacement text for 'magic' strings in symlinks.
74 * Returns 0 if successful, and returns non-zero if an error
75 * occurs. (Currently, the only possible error is running out
76 * of temporary pathname space.)
77 *
78 * Looks for "@<string>" and "@<string>/", where <string> is a
79 * recognized 'magic' string. Replaces the "@<string>" with the
80 * appropriate replacement text. (Note that in some cases the
81 * replacement text may have zero length.)
82 *
83 * This would have been table driven, but the variance in
84 * replacement strings (and replacement string lengths) made
85 * that impractical.
86 */
87 #define VNL(x) \
88 (sizeof(x) - 1)
89
90 #define VO '{'
91 #define VC '}'
92
93 #define MATCH(str) \
94 ((termchar == '/' && i + VNL(str) == *len) || \
95 (i + VNL(str) < *len && \
96 cp[i + VNL(str)] == termchar)) && \
97 !strncmp((str), &cp[i], VNL(str))
98
99 #define SUBSTITUTE(m, s, sl) \
100 if ((newlen + (sl)) >= MAXPATHLEN) \
101 return 1; \
102 i += VNL(m); \
103 if (termchar != '/') \
104 i++; \
105 (void)memcpy(&tmp[newlen], (s), (sl)); \
106 newlen += (sl); \
107 change = 1; \
108 termchar = '/';
109
110 static int
111 symlink_magic(struct proc *p, char *cp, size_t *len)
112 {
113 char *tmp;
114 size_t change, i, newlen, slen;
115 char termchar = '/';
116 char idtmp[11]; /* enough for 32 bit *unsigned* integer */
117
118
119 tmp = PNBUF_GET();
120 for (change = i = newlen = 0; i < *len; ) {
121 if (cp[i] != '@') {
122 tmp[newlen++] = cp[i++];
123 continue;
124 }
125
126 i++;
127
128 /* Check for @{var} syntax. */
129 if (cp[i] == VO) {
130 termchar = VC;
131 i++;
132 }
133
134 /*
135 * The following checks should be ordered according
136 * to frequency of use.
137 */
138 if (MATCH("machine_arch")) {
139 slen = VNL(MACHINE_ARCH);
140 SUBSTITUTE("machine_arch", MACHINE_ARCH, slen);
141 } else if (MATCH("machine")) {
142 slen = VNL(MACHINE);
143 SUBSTITUTE("machine", MACHINE, slen);
144 } else if (MATCH("hostname")) {
145 SUBSTITUTE("hostname", hostname, hostnamelen);
146 } else if (MATCH("osrelease")) {
147 slen = strlen(osrelease);
148 SUBSTITUTE("osrelease", osrelease, slen);
149 } else if (MATCH("emul")) {
150 slen = strlen(p->p_emul->e_name);
151 SUBSTITUTE("emul", p->p_emul->e_name, slen);
152 } else if (MATCH("kernel_ident")) {
153 slen = strlen(kernel_ident);
154 SUBSTITUTE("kernel_ident", kernel_ident, slen);
155 } else if (MATCH("domainname")) {
156 SUBSTITUTE("domainname", domainname, domainnamelen);
157 } else if (MATCH("ostype")) {
158 slen = strlen(ostype);
159 SUBSTITUTE("ostype", ostype, slen);
160 } else if (MATCH("uid")) {
161 slen = snprintf(idtmp, sizeof(idtmp), "%u",
162 kauth_cred_geteuid(kauth_cred_get()));
163 SUBSTITUTE("uid", idtmp, slen);
164 } else if (MATCH("ruid")) {
165 slen = snprintf(idtmp, sizeof(idtmp), "%u",
166 kauth_cred_getuid(kauth_cred_get()));
167 SUBSTITUTE("ruid", idtmp, slen);
168 } else if (MATCH("gid")) {
169 slen = snprintf(idtmp, sizeof(idtmp), "%u",
170 kauth_cred_getegid(kauth_cred_get()));
171 SUBSTITUTE("gid", idtmp, slen);
172 } else if (MATCH("rgid")) {
173 slen = snprintf(idtmp, sizeof(idtmp), "%u",
174 kauth_cred_getgid(kauth_cred_get()));
175 SUBSTITUTE("rgid", idtmp, slen);
176 } else {
177 tmp[newlen++] = '@';
178 if (termchar == VC)
179 tmp[newlen++] = VO;
180 }
181 }
182
183 if (change) {
184 (void)memcpy(cp, tmp, newlen);
185 *len = newlen;
186 }
187 PNBUF_PUT(tmp);
188
189 return 0;
190 }
191
192 #undef VNL
193 #undef VO
194 #undef VC
195 #undef MATCH
196 #undef SUBSTITUTE
197
198 ////////////////////////////////////////////////////////////
199
200 /*
201 * Determine the namei hash (for the namecache) for name.
202 * If *ep != NULL, hash from name to ep-1.
203 * If *ep == NULL, hash from name until the first NUL or '/', and
204 * return the location of this termination character in *ep.
205 *
206 * This function returns an equivalent hash to the MI hash32_strn().
207 * The latter isn't used because in the *ep == NULL case, determining
208 * the length of the string to the first NUL or `/' and then calling
209 * hash32_strn() involves unnecessary double-handling of the data.
210 */
211 uint32_t
212 namei_hash(const char *name, const char **ep)
213 {
214 uint32_t hash;
215
216 hash = HASH32_STR_INIT;
217 if (*ep != NULL) {
218 for (; name < *ep; name++)
219 hash = hash * 33 + *(const uint8_t *)name;
220 } else {
221 for (; *name != '\0' && *name != '/'; name++)
222 hash = hash * 33 + *(const uint8_t *)name;
223 *ep = name;
224 }
225 return (hash + (hash >> 5));
226 }
227
228 ////////////////////////////////////////////////////////////
229
230 /*
231 * Sealed abstraction for pathnames.
232 *
233 * System-call-layer level code that is going to call namei should
234 * first create a pathbuf and adjust all the bells and whistles on it
235 * as needed by context.
236 */
237
238 struct pathbuf {
239 char *pb_path;
240 char *pb_pathcopy;
241 unsigned pb_pathcopyuses;
242 };
243
244 static struct pathbuf *
245 pathbuf_create_raw(void)
246 {
247 struct pathbuf *pb;
248
249 pb = kmem_alloc(sizeof(*pb), KM_SLEEP);
250 pb->pb_path = PNBUF_GET();
251 if (pb->pb_path == NULL) {
252 kmem_free(pb, sizeof(*pb));
253 return NULL;
254 }
255 pb->pb_pathcopy = NULL;
256 pb->pb_pathcopyuses = 0;
257 return pb;
258 }
259
260 void
261 pathbuf_destroy(struct pathbuf *pb)
262 {
263 KASSERT(pb->pb_pathcopyuses == 0);
264 KASSERT(pb->pb_pathcopy == NULL);
265 PNBUF_PUT(pb->pb_path);
266 kmem_free(pb, sizeof(*pb));
267 }
268
269 struct pathbuf *
270 pathbuf_assimilate(char *pnbuf)
271 {
272 struct pathbuf *pb;
273
274 pb = kmem_alloc(sizeof(*pb), KM_SLEEP);
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 * 1. whether a pointer is kernel or user should be statically checkable.
322 * 2. copyin should be handled by the upper part of the syscall layer,
323 * not in here.
324 */
325 int
326 pathbuf_maybe_copyin(const char *path, enum uio_seg seg, struct pathbuf **ret)
327 {
328 if (seg == UIO_USERSPACE) {
329 return pathbuf_copyin(path, ret);
330 } else {
331 *ret = pathbuf_create(path);
332 if (*ret == NULL) {
333 return ENOMEM;
334 }
335 return 0;
336 }
337 }
338
339 /*
340 * Get a copy of the path buffer as it currently exists. If this is
341 * called after namei starts the results may be arbitrary.
342 */
343 void
344 pathbuf_copystring(const struct pathbuf *pb, char *buf, size_t maxlen)
345 {
346 strlcpy(buf, pb->pb_path, maxlen);
347 }
348
349 /*
350 * These two functions allow access to a saved copy of the original
351 * path string. The first copy should be gotten before namei is
352 * called. Each copy that is gotten should be put back.
353 */
354
355 const char *
356 pathbuf_stringcopy_get(struct pathbuf *pb)
357 {
358 if (pb->pb_pathcopyuses == 0) {
359 pb->pb_pathcopy = PNBUF_GET();
360 strcpy(pb->pb_pathcopy, pb->pb_path);
361 }
362 pb->pb_pathcopyuses++;
363 return pb->pb_pathcopy;
364 }
365
366 void
367 pathbuf_stringcopy_put(struct pathbuf *pb, const char *str)
368 {
369 KASSERT(str == pb->pb_pathcopy);
370 KASSERT(pb->pb_pathcopyuses > 0);
371 pb->pb_pathcopyuses--;
372 if (pb->pb_pathcopyuses == 0) {
373 PNBUF_PUT(pb->pb_pathcopy);
374 pb->pb_pathcopy = NULL;
375 }
376 }
377
378
379 ////////////////////////////////////////////////////////////
380
381 /*
382 * namei: convert a pathname into a pointer to a (maybe-locked) vnode,
383 * and maybe also its parent directory vnode, and assorted other guff.
384 * See namei(9) for the interface documentation.
385 *
386 *
387 * The FOLLOW flag is set when symbolic links are to be followed
388 * when they occur at the end of the name translation process.
389 * Symbolic links are always followed for all other pathname
390 * components other than the last.
391 *
392 * The segflg defines whether the name is to be copied from user
393 * space or kernel space.
394 *
395 * Overall outline of namei:
396 *
397 * copy in name
398 * get starting directory
399 * while (!done && !error) {
400 * call lookup to search path.
401 * if symbolic link, massage name in buffer and continue
402 * }
403 */
404
405 /*
406 * Search a pathname.
407 * This is a very central and rather complicated routine.
408 *
409 * The pathname is pointed to by ni_ptr and is of length ni_pathlen.
410 * The starting directory is passed in. The pathname is descended
411 * until done, or a symbolic link is encountered. The variable ni_more
412 * is clear if the path is completed; it is set to one if a symbolic
413 * link needing interpretation is encountered.
414 *
415 * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
416 * whether the name is to be looked up, created, renamed, or deleted.
417 * When CREATE, RENAME, or DELETE is specified, information usable in
418 * creating, renaming, or deleting a directory entry may be calculated.
419 * If flag has LOCKPARENT or'ed into it, the parent directory is returned
420 * locked. Otherwise the parent directory is not returned. If the target
421 * of the pathname exists and LOCKLEAF is or'ed into the flag the target
422 * is returned locked, otherwise it is returned unlocked. When creating
423 * or renaming and LOCKPARENT is specified, the target may not be ".".
424 * When deleting and LOCKPARENT is specified, the target may be ".".
425 *
426 * Overall outline of lookup:
427 *
428 * dirloop:
429 * identify next component of name at ndp->ni_ptr
430 * handle degenerate case where name is null string
431 * if .. and crossing mount points and on mounted filesys, find parent
432 * call VOP_LOOKUP routine for next component name
433 * directory vnode returned in ni_dvp, locked.
434 * component vnode returned in ni_vp (if it exists), locked.
435 * if result vnode is mounted on and crossing mount points,
436 * find mounted on vnode
437 * if more components of name, do next level at dirloop
438 * return the answer in ni_vp, locked if LOCKLEAF set
439 * if LOCKPARENT set, return locked parent in ni_dvp
440 */
441
442
443 /*
444 * Internal state for a namei operation.
445 *
446 * cnp is always equal to &ndp->ni_cnp.
447 */
448 struct namei_state {
449 struct nameidata *ndp;
450 struct componentname *cnp;
451
452 int docache; /* == 0 do not cache last component */
453 int rdonly; /* lookup read-only flag bit */
454 int slashes;
455
456 unsigned attempt_retry:1; /* true if error allows emul retry */
457 unsigned root_referenced:1; /* true if ndp->ni_rootdir and
458 ndp->ni_erootdir were referenced */
459 };
460
461
462 /*
463 * Initialize the namei working state.
464 */
465 static void
466 namei_init(struct namei_state *state, struct nameidata *ndp)
467 {
468
469 state->ndp = ndp;
470 state->cnp = &ndp->ni_cnd;
471
472 state->docache = 0;
473 state->rdonly = 0;
474 state->slashes = 0;
475
476 state->root_referenced = 0;
477
478 KASSERTMSG((state->cnp->cn_cred != NULL), "namei: bad cred/proc");
479 KASSERTMSG(((state->cnp->cn_nameiop & (~OPMASK)) == 0),
480 "namei: nameiop contaminated with flags: %08"PRIx32,
481 state->cnp->cn_nameiop);
482 KASSERTMSG(((state->cnp->cn_flags & OPMASK) == 0),
483 "name: flags contaminated with nameiops: %08"PRIx32,
484 state->cnp->cn_flags);
485
486 /*
487 * The buffer for name translation shall be the one inside the
488 * pathbuf.
489 */
490 state->ndp->ni_pnbuf = state->ndp->ni_pathbuf->pb_path;
491 }
492
493 /*
494 * Clean up the working namei state, leaving things ready for return
495 * from namei.
496 */
497 static void
498 namei_cleanup(struct namei_state *state)
499 {
500 KASSERT(state->cnp == &state->ndp->ni_cnd);
501
502 if (state->root_referenced) {
503 if (state->ndp->ni_rootdir != NULL)
504 vrele(state->ndp->ni_rootdir);
505 if (state->ndp->ni_erootdir != NULL)
506 vrele(state->ndp->ni_erootdir);
507 }
508 }
509
510 //////////////////////////////
511
512 /*
513 * Get the directory context.
514 * Initializes the rootdir and erootdir state and returns a reference
515 * to the starting dir.
516 */
517 static struct vnode *
518 namei_getstartdir(struct namei_state *state)
519 {
520 struct nameidata *ndp = state->ndp;
521 struct componentname *cnp = state->cnp;
522 struct cwdinfo *cwdi; /* pointer to cwd state */
523 struct lwp *self = curlwp; /* thread doing namei() */
524 struct vnode *rootdir, *erootdir, *curdir, *startdir;
525
526 if (state->root_referenced) {
527 if (state->ndp->ni_rootdir != NULL)
528 vrele(state->ndp->ni_rootdir);
529 if (state->ndp->ni_erootdir != NULL)
530 vrele(state->ndp->ni_erootdir);
531 state->root_referenced = 0;
532 }
533
534 cwdi = self->l_proc->p_cwdi;
535 rw_enter(&cwdi->cwdi_lock, RW_READER);
536
537 /* root dir */
538 if (cwdi->cwdi_rdir == NULL || (cnp->cn_flags & NOCHROOT)) {
539 rootdir = rootvnode;
540 } else {
541 rootdir = cwdi->cwdi_rdir;
542 }
543
544 /* emulation root dir, if any */
545 if ((cnp->cn_flags & TRYEMULROOT) == 0) {
546 /* if we don't want it, don't fetch it */
547 erootdir = NULL;
548 } else if (cnp->cn_flags & EMULROOTSET) {
549 /* explicitly set emulroot; "/../" doesn't override this */
550 erootdir = ndp->ni_erootdir;
551 } else if (!strncmp(ndp->ni_pnbuf, "/../", 4)) {
552 /* explicit reference to real rootdir */
553 erootdir = NULL;
554 } else {
555 /* may be null */
556 erootdir = cwdi->cwdi_edir;
557 }
558
559 /* current dir */
560 curdir = cwdi->cwdi_cdir;
561
562 if (ndp->ni_pnbuf[0] != '/') {
563 if (ndp->ni_atdir != NULL) {
564 startdir = ndp->ni_atdir;
565 } else {
566 startdir = curdir;
567 }
568 erootdir = NULL;
569 } else if (cnp->cn_flags & TRYEMULROOT && erootdir != NULL) {
570 startdir = erootdir;
571 } else {
572 startdir = rootdir;
573 erootdir = NULL;
574 }
575
576 state->ndp->ni_rootdir = rootdir;
577 state->ndp->ni_erootdir = erootdir;
578
579 /*
580 * Get a reference to the start dir so we can safely unlock cwdi.
581 *
582 * Must hold references to rootdir and erootdir while we're running.
583 * A multithreaded process may chroot during namei.
584 */
585 if (startdir != NULL)
586 vref(startdir);
587 if (state->ndp->ni_rootdir != NULL)
588 vref(state->ndp->ni_rootdir);
589 if (state->ndp->ni_erootdir != NULL)
590 vref(state->ndp->ni_erootdir);
591 state->root_referenced = 1;
592
593 rw_exit(&cwdi->cwdi_lock);
594 return startdir;
595 }
596
597 /*
598 * Get the directory context for the nfsd case, in parallel to
599 * getstartdir. Initializes the rootdir and erootdir state and
600 * returns a reference to the passed-in starting dir.
601 */
602 static struct vnode *
603 namei_getstartdir_for_nfsd(struct namei_state *state)
604 {
605 KASSERT(state->ndp->ni_atdir != NULL);
606
607 /* always use the real root, and never set an emulation root */
608 if (rootvnode == NULL) {
609 return NULL;
610 }
611 state->ndp->ni_rootdir = rootvnode;
612 state->ndp->ni_erootdir = NULL;
613
614 vref(state->ndp->ni_atdir);
615 KASSERT(! state->root_referenced);
616 vref(state->ndp->ni_rootdir);
617 state->root_referenced = 1;
618 return state->ndp->ni_atdir;
619 }
620
621
622 /*
623 * Ktrace the namei operation.
624 */
625 static void
626 namei_ktrace(struct namei_state *state)
627 {
628 struct nameidata *ndp = state->ndp;
629 struct componentname *cnp = state->cnp;
630 struct lwp *self = curlwp; /* thread doing namei() */
631 const char *emul_path;
632
633 if (ktrpoint(KTR_NAMEI)) {
634 if (ndp->ni_erootdir != NULL) {
635 /*
636 * To make any sense, the trace entry need to have the
637 * text of the emulation path prepended.
638 * Usually we can get this from the current process,
639 * but when called from emul_find_interp() it is only
640 * in the exec_package - so we get it passed in ni_next
641 * (this is a hack).
642 */
643 if (cnp->cn_flags & EMULROOTSET)
644 emul_path = ndp->ni_next;
645 else
646 emul_path = self->l_proc->p_emul->e_path;
647 ktrnamei2(emul_path, strlen(emul_path),
648 ndp->ni_pnbuf, ndp->ni_pathlen);
649 } else
650 ktrnamei(ndp->ni_pnbuf, ndp->ni_pathlen);
651 }
652 }
653
654 /*
655 * Start up namei. Find the root dir and cwd, establish the starting
656 * directory for lookup, and lock it. Also calls ktrace when
657 * appropriate.
658 */
659 static int
660 namei_start(struct namei_state *state, int isnfsd,
661 struct vnode **startdir_ret)
662 {
663 struct nameidata *ndp = state->ndp;
664 struct vnode *startdir;
665
666 /* length includes null terminator (was originally from copyinstr) */
667 ndp->ni_pathlen = strlen(ndp->ni_pnbuf) + 1;
668
669 /*
670 * POSIX.1 requirement: "" is not a valid file name.
671 */
672 if (ndp->ni_pathlen == 1) {
673 ndp->ni_erootdir = NULL;
674 return ENOENT;
675 }
676
677 ndp->ni_loopcnt = 0;
678
679 /* Get starting directory, set up root, and ktrace. */
680 if (isnfsd) {
681 startdir = namei_getstartdir_for_nfsd(state);
682 /* no ktrace */
683 } else {
684 startdir = namei_getstartdir(state);
685 namei_ktrace(state);
686 }
687
688 if (startdir == NULL) {
689 return ENOENT;
690 }
691
692 /* NDAT may feed us with a non directory namei_getstartdir */
693 if (startdir->v_type != VDIR) {
694 vrele(startdir);
695 return ENOTDIR;
696 }
697
698 *startdir_ret = startdir;
699 return 0;
700 }
701
702 /*
703 * Check for being at a symlink that we're going to follow.
704 */
705 static inline int
706 namei_atsymlink(struct namei_state *state, struct vnode *foundobj)
707 {
708 return (foundobj->v_type == VLNK) &&
709 (state->cnp->cn_flags & (FOLLOW|REQUIREDIR));
710 }
711
712 /*
713 * Follow a symlink.
714 *
715 * Updates searchdir. inhibitmagic causes magic symlinks to not be
716 * interpreted; this is used by nfsd.
717 *
718 * Unlocks foundobj on success (ugh)
719 */
720 static inline int
721 namei_follow(struct namei_state *state, int inhibitmagic,
722 struct vnode *searchdir, struct vnode *foundobj,
723 struct vnode **newsearchdir_ret)
724 {
725 struct nameidata *ndp = state->ndp;
726 struct componentname *cnp = state->cnp;
727
728 struct lwp *self = curlwp; /* thread doing namei() */
729 struct iovec aiov; /* uio for reading symbolic links */
730 struct uio auio;
731 char *cp; /* pointer into pathname argument */
732 size_t linklen;
733 int error;
734
735 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
736 return ELOOP;
737 }
738
739 vn_lock(foundobj, LK_EXCLUSIVE | LK_RETRY);
740 if (foundobj->v_mount->mnt_flag & MNT_SYMPERM) {
741 error = VOP_ACCESS(foundobj, VEXEC, cnp->cn_cred);
742 if (error != 0) {
743 VOP_UNLOCK(foundobj);
744 return error;
745 }
746 }
747
748 /* FUTURE: fix this to not use a second buffer */
749 cp = PNBUF_GET();
750 aiov.iov_base = cp;
751 aiov.iov_len = MAXPATHLEN;
752 auio.uio_iov = &aiov;
753 auio.uio_iovcnt = 1;
754 auio.uio_offset = 0;
755 auio.uio_rw = UIO_READ;
756 auio.uio_resid = MAXPATHLEN;
757 UIO_SETUP_SYSSPACE(&auio);
758 error = VOP_READLINK(foundobj, &auio, cnp->cn_cred);
759 VOP_UNLOCK(foundobj);
760 if (error) {
761 PNBUF_PUT(cp);
762 return error;
763 }
764 linklen = MAXPATHLEN - auio.uio_resid;
765 if (linklen == 0) {
766 PNBUF_PUT(cp);
767 return ENOENT;
768 }
769
770 /*
771 * Do symlink substitution, if appropriate, and
772 * check length for potential overflow.
773 *
774 * Inhibit symlink substitution for nfsd.
775 * XXX: This is how it was before; is that a bug or a feature?
776 */
777 if ((!inhibitmagic && vfs_magiclinks &&
778 symlink_magic(self->l_proc, cp, &linklen)) ||
779 (linklen + ndp->ni_pathlen >= MAXPATHLEN)) {
780 PNBUF_PUT(cp);
781 return ENAMETOOLONG;
782 }
783 if (ndp->ni_pathlen > 1) {
784 /* includes a null-terminator */
785 memcpy(cp + linklen, ndp->ni_next, ndp->ni_pathlen);
786 } else {
787 cp[linklen] = '\0';
788 }
789 ndp->ni_pathlen += linklen;
790 memcpy(ndp->ni_pnbuf, cp, ndp->ni_pathlen);
791 PNBUF_PUT(cp);
792
793 /* we're now starting from the beginning of the buffer again */
794 cnp->cn_nameptr = ndp->ni_pnbuf;
795
796 /*
797 * Check if root directory should replace current directory.
798 */
799 if (ndp->ni_pnbuf[0] == '/') {
800 vrele(searchdir);
801 /* Keep absolute symbolic links inside emulation root */
802 searchdir = ndp->ni_erootdir;
803 if (searchdir == NULL ||
804 (ndp->ni_pnbuf[1] == '.'
805 && ndp->ni_pnbuf[2] == '.'
806 && ndp->ni_pnbuf[3] == '/')) {
807 ndp->ni_erootdir = NULL;
808 searchdir = ndp->ni_rootdir;
809 }
810 vref(searchdir);
811 while (cnp->cn_nameptr[0] == '/') {
812 cnp->cn_nameptr++;
813 ndp->ni_pathlen--;
814 }
815 }
816
817 *newsearchdir_ret = searchdir;
818 return 0;
819 }
820
821 //////////////////////////////
822
823 /*
824 * Inspect the leading path component and update the state accordingly.
825 */
826 static int
827 lookup_parsepath(struct namei_state *state, struct vnode *searchdir)
828 {
829 const char *cp; /* pointer into pathname argument */
830 int error;
831
832 struct componentname *cnp = state->cnp;
833 struct nameidata *ndp = state->ndp;
834
835 KASSERT(cnp == &ndp->ni_cnd);
836
837 /*
838 * Search a new directory.
839 *
840 * The last component of the filename is left accessible via
841 * cnp->cn_nameptr for callers that need the name. Callers needing
842 * the name set the SAVENAME flag. When done, they assume
843 * responsibility for freeing the pathname buffer.
844 *
845 * At this point, our only vnode state is that the search dir
846 * is held.
847 */
848 error = VOP_PARSEPATH(searchdir, cnp->cn_nameptr, &cnp->cn_namelen);
849 if (error) {
850 return error;
851 }
852 cp = cnp->cn_nameptr + cnp->cn_namelen;
853 if (cnp->cn_namelen > KERNEL_NAME_MAX) {
854 return ENAMETOOLONG;
855 }
856 #ifdef NAMEI_DIAGNOSTIC
857 { char c = *cp;
858 *(char *)cp = '\0';
859 printf("{%s}: ", cnp->cn_nameptr);
860 *(char *)cp = c; }
861 #endif /* NAMEI_DIAGNOSTIC */
862 ndp->ni_pathlen -= cnp->cn_namelen;
863 ndp->ni_next = cp;
864 /*
865 * If this component is followed by a slash, then move the pointer to
866 * the next component forward, and remember that this component must be
867 * a directory.
868 */
869 if (*cp == '/') {
870 do {
871 cp++;
872 } while (*cp == '/');
873 state->slashes = cp - ndp->ni_next;
874 ndp->ni_pathlen -= state->slashes;
875 ndp->ni_next = cp;
876 cnp->cn_flags |= REQUIREDIR;
877 } else {
878 state->slashes = 0;
879 cnp->cn_flags &= ~REQUIREDIR;
880 }
881 /*
882 * We do special processing on the last component, whether or not it's
883 * a directory. Cache all intervening lookups, but not the final one.
884 */
885 if (*cp == '\0') {
886 if (state->docache)
887 cnp->cn_flags |= MAKEENTRY;
888 else
889 cnp->cn_flags &= ~MAKEENTRY;
890 cnp->cn_flags |= ISLASTCN;
891 } else {
892 cnp->cn_flags |= MAKEENTRY;
893 cnp->cn_flags &= ~ISLASTCN;
894 }
895 if (cnp->cn_namelen == 2 &&
896 cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.')
897 cnp->cn_flags |= ISDOTDOT;
898 else
899 cnp->cn_flags &= ~ISDOTDOT;
900
901 return 0;
902 }
903
904 /*
905 * Take care of crossing a mounted-on vnode. On error, foundobj_ret will be
906 * vrele'd, but searchdir is left alone.
907 */
908 static int
909 lookup_crossmount(struct namei_state *state,
910 struct vnode **searchdir_ret,
911 struct vnode **foundobj_ret,
912 bool *searchdir_locked)
913 {
914 struct componentname *cnp = state->cnp;
915 struct vnode *foundobj, *vp;
916 struct vnode *searchdir;
917 struct mount *mp;
918 int error, lktype;
919
920 searchdir = *searchdir_ret;
921 foundobj = *foundobj_ret;
922 error = 0;
923
924 KASSERT((cnp->cn_flags & NOCROSSMOUNT) == 0);
925
926 /* First, unlock searchdir (oof). */
927 if (*searchdir_locked) {
928 KASSERT(searchdir != NULL);
929 lktype = VOP_ISLOCKED(searchdir);
930 VOP_UNLOCK(searchdir);
931 *searchdir_locked = false;
932 } else {
933 lktype = LK_NONE;
934 }
935
936 /*
937 * Do an unlocked check to see if the vnode has been mounted on; if
938 * so find the root of the mounted file system.
939 */
940 while (foundobj->v_type == VDIR &&
941 (mp = foundobj->v_mountedhere) != NULL &&
942 (cnp->cn_flags & NOCROSSMOUNT) == 0) {
943 /*
944 * Try the namecache first. If that doesn't work, do
945 * it the hard way.
946 */
947 if (cache_lookup_mount(foundobj, &vp)) {
948 vrele(foundobj);
949 foundobj = vp;
950 } else {
951 /* First get the vnode stable. */
952 error = vn_lock(foundobj, LK_SHARED);
953 if (error != 0) {
954 vrele(foundobj);
955 foundobj = NULL;
956 break;
957 }
958
959 /*
960 * Check to see if something is still mounted on it.
961 */
962 if ((mp = foundobj->v_mountedhere) == NULL) {
963 VOP_UNLOCK(foundobj);
964 break;
965 }
966
967 /*
968 * Get a reference to the mountpoint, and unlock
969 * foundobj.
970 */
971 error = vfs_busy(mp);
972 VOP_UNLOCK(foundobj);
973 if (error != 0) {
974 vrele(foundobj);
975 foundobj = NULL;
976 break;
977 }
978
979 /*
980 * Now get a reference on the root vnode.
981 * XXX Future - maybe allow only VDIR here.
982 */
983 error = VFS_ROOT(mp, LK_NONE, &vp);
984
985 /*
986 * If successful, enter it into the cache while
987 * holding the mount busy (competing with unmount).
988 */
989 if (error == 0) {
990 cache_enter_mount(foundobj, vp);
991 }
992
993 /* Finally, drop references to foundobj & mountpoint. */
994 vrele(foundobj);
995 vfs_unbusy(mp);
996 if (error) {
997 foundobj = NULL;
998 break;
999 }
1000 foundobj = vp;
1001 }
1002
1003 /*
1004 * Avoid locking vnodes from two filesystems because
1005 * it's prone to deadlock, e.g. when using puffs.
1006 * Also, it isn't a good idea to propagate slowness of
1007 * a filesystem up to the root directory. For now,
1008 * only handle the common case, where foundobj is
1009 * VDIR.
1010 *
1011 * In this case set searchdir to null to avoid using
1012 * it again. It is not correct to set searchdir ==
1013 * foundobj here as that will confuse the caller.
1014 * (See PR 40740.)
1015 */
1016 if (searchdir == NULL) {
1017 /* already been here once; do nothing further */
1018 } else if (foundobj->v_type == VDIR) {
1019 vrele(searchdir);
1020 *searchdir_ret = searchdir = NULL;
1021 lktype = LK_NONE;
1022 }
1023 }
1024
1025 /* If searchdir is still around, re-lock it. */
1026 if (error == 0 && lktype != LK_NONE) {
1027 vn_lock(searchdir, lktype | LK_RETRY);
1028 *searchdir_locked = true;
1029 }
1030 *foundobj_ret = foundobj;
1031 return error;
1032 }
1033
1034 /*
1035 * Determine the desired locking mode for the directory of a lookup.
1036 */
1037 static int
1038 lookup_lktype(struct vnode *searchdir, struct componentname *cnp)
1039 {
1040
1041 /*
1042 * If the file system supports VOP_LOOKUP() with a shared lock, and
1043 * we are not making any modifications (nameiop LOOKUP) or this is
1044 * not the last component then get a shared lock. Where we can't do
1045 * fast-forwarded lookups (for example with layered file systems)
1046 * then this is the fallback for reducing lock contention.
1047 */
1048 if ((searchdir->v_mount->mnt_iflag & IMNT_SHRLOOKUP) != 0 &&
1049 (cnp->cn_nameiop == LOOKUP || (cnp->cn_flags & ISLASTCN) == 0)) {
1050 return LK_SHARED;
1051 } else {
1052 return LK_EXCLUSIVE;
1053 }
1054 }
1055
1056 /*
1057 * Call VOP_LOOKUP for a single lookup; return a new search directory
1058 * (used when crossing mountpoints up or searching union mounts down) and
1059 * the found object, which for create operations may be NULL on success.
1060 *
1061 * Note that the new search directory may be null, which means the
1062 * searchdir was unlocked and released. This happens in the common case
1063 * when crossing a mount point downwards, in order to avoid coupling
1064 * locks between different file system volumes. Importantly, this can
1065 * happen even if the call fails. (XXX: this is gross and should be
1066 * tidied somehow.)
1067 */
1068 static int
1069 lookup_once(struct namei_state *state,
1070 struct vnode *searchdir,
1071 struct vnode **newsearchdir_ret,
1072 struct vnode **foundobj_ret,
1073 bool *newsearchdir_locked_ret)
1074 {
1075 struct vnode *tmpvn; /* scratch vnode */
1076 struct vnode *foundobj; /* result */
1077 struct lwp *l = curlwp;
1078 bool searchdir_locked = false;
1079 int error, lktype;
1080
1081 struct componentname *cnp = state->cnp;
1082 struct nameidata *ndp = state->ndp;
1083
1084 KASSERT(cnp == &ndp->ni_cnd);
1085 *newsearchdir_ret = searchdir;
1086
1087 /*
1088 * Handle "..": two special cases.
1089 * 1. If at root directory (e.g. after chroot)
1090 * or at absolute root directory
1091 * then ignore it so can't get out.
1092 * 1a. If at the root of the emulation filesystem go to the real
1093 * root. So "/../<path>" is always absolute.
1094 * 1b. If we have somehow gotten out of a jail, warn
1095 * and also ignore it so we can't get farther out.
1096 * 2. If this vnode is the root of a mounted
1097 * filesystem, then replace it with the
1098 * vnode which was mounted on so we take the
1099 * .. in the other file system.
1100 */
1101 if (cnp->cn_flags & ISDOTDOT) {
1102 struct proc *p = l->l_proc;
1103
1104 for (;;) {
1105 if (searchdir == ndp->ni_rootdir ||
1106 searchdir == rootvnode) {
1107 foundobj = searchdir;
1108 vref(foundobj);
1109 *foundobj_ret = foundobj;
1110 if (cnp->cn_flags & LOCKPARENT) {
1111 lktype = lookup_lktype(searchdir, cnp);
1112 vn_lock(searchdir, lktype | LK_RETRY);
1113 searchdir_locked = true;
1114 }
1115 error = 0;
1116 goto done;
1117 }
1118 if (ndp->ni_rootdir != rootvnode) {
1119 int retval;
1120
1121 retval = vn_isunder(searchdir, ndp->ni_rootdir, l);
1122 if (!retval) {
1123 /* Oops! We got out of jail! */
1124 log(LOG_WARNING,
1125 "chrooted pid %d uid %d (%s) "
1126 "detected outside of its chroot\n",
1127 p->p_pid, kauth_cred_geteuid(l->l_cred),
1128 p->p_comm);
1129 /* Put us at the jail root. */
1130 vrele(searchdir);
1131 searchdir = NULL;
1132 foundobj = ndp->ni_rootdir;
1133 vref(foundobj);
1134 vref(foundobj);
1135 *newsearchdir_ret = foundobj;
1136 *foundobj_ret = foundobj;
1137 error = 0;
1138 goto done;
1139 }
1140 }
1141 if ((searchdir->v_vflag & VV_ROOT) == 0 ||
1142 (cnp->cn_flags & NOCROSSMOUNT))
1143 break;
1144 tmpvn = searchdir;
1145 searchdir = searchdir->v_mount->mnt_vnodecovered;
1146 vref(searchdir);
1147 vrele(tmpvn);
1148 *newsearchdir_ret = searchdir;
1149 }
1150 }
1151
1152 lktype = lookup_lktype(searchdir, cnp);
1153
1154 /*
1155 * We now have a segment name to search for, and a directory to search.
1156 * Our vnode state here is that "searchdir" is held.
1157 */
1158 unionlookup:
1159 foundobj = NULL;
1160 if (!searchdir_locked) {
1161 vn_lock(searchdir, lktype | LK_RETRY);
1162 searchdir_locked = true;
1163 }
1164 error = VOP_LOOKUP(searchdir, &foundobj, cnp);
1165
1166 if (error != 0) {
1167 KASSERTMSG((foundobj == NULL),
1168 "leaf `%s' should be empty but is %p",
1169 cnp->cn_nameptr, foundobj);
1170 #ifdef NAMEI_DIAGNOSTIC
1171 printf("not found\n");
1172 #endif /* NAMEI_DIAGNOSTIC */
1173
1174 /*
1175 * If ENOLCK, the file system needs us to retry the lookup
1176 * with an exclusive lock. It's likely nothing was found in
1177 * cache and/or modifications need to be made.
1178 */
1179 if (error == ENOLCK) {
1180 KASSERT(VOP_ISLOCKED(searchdir) == LK_SHARED);
1181 KASSERT(searchdir_locked);
1182 if (vn_lock(searchdir, LK_UPGRADE | LK_NOWAIT)) {
1183 VOP_UNLOCK(searchdir);
1184 searchdir_locked = false;
1185 }
1186 lktype = LK_EXCLUSIVE;
1187 goto unionlookup;
1188 }
1189
1190 if ((error == ENOENT) &&
1191 (searchdir->v_vflag & VV_ROOT) &&
1192 (searchdir->v_mount->mnt_flag & MNT_UNION)) {
1193 tmpvn = searchdir;
1194 searchdir = searchdir->v_mount->mnt_vnodecovered;
1195 vref(searchdir);
1196 vput(tmpvn);
1197 searchdir_locked = false;
1198 *newsearchdir_ret = searchdir;
1199 goto unionlookup;
1200 }
1201
1202 if (error != EJUSTRETURN)
1203 goto done;
1204
1205 /*
1206 * If this was not the last component, or there were trailing
1207 * slashes, and we are not going to create a directory,
1208 * then the name must exist.
1209 */
1210 if ((cnp->cn_flags & (REQUIREDIR | CREATEDIR)) == REQUIREDIR) {
1211 error = ENOENT;
1212 goto done;
1213 }
1214
1215 /*
1216 * If creating and at end of pathname, then can consider
1217 * allowing file to be created.
1218 */
1219 if (state->rdonly) {
1220 error = EROFS;
1221 goto done;
1222 }
1223
1224 /*
1225 * We return success and a NULL foundobj to indicate
1226 * that the entry doesn't currently exist, leaving a
1227 * pointer to the (normally, locked) directory vnode
1228 * as searchdir.
1229 */
1230 *foundobj_ret = NULL;
1231 error = 0;
1232 goto done;
1233 }
1234 #ifdef NAMEI_DIAGNOSTIC
1235 printf("found\n");
1236 #endif /* NAMEI_DIAGNOSTIC */
1237
1238 /* Unlock, unless the caller needs the parent locked. */
1239 if (searchdir != NULL) {
1240 KASSERT(searchdir_locked);
1241 if ((cnp->cn_flags & (ISLASTCN | LOCKPARENT)) !=
1242 (ISLASTCN | LOCKPARENT)) {
1243 VOP_UNLOCK(searchdir);
1244 searchdir_locked = false;
1245 }
1246 } else {
1247 KASSERT(!searchdir_locked);
1248 }
1249
1250 *foundobj_ret = foundobj;
1251 error = 0;
1252 done:
1253 *newsearchdir_locked_ret = searchdir_locked;
1254 return error;
1255 }
1256
1257 /*
1258 * Parse out the first path name component that we need to to consider.
1259 *
1260 * While doing this, attempt to use the name cache to fast-forward through
1261 * as many "easy" to find components of the path as possible.
1262 *
1263 * We use the namecache's node locks to form a chain, and avoid as many
1264 * vnode references and locks as possible. In the ideal case, only the
1265 * final vnode will have its reference count adjusted and lock taken.
1266 */
1267 static int
1268 lookup_fastforward(struct namei_state *state, struct vnode **searchdir_ret,
1269 struct vnode **foundobj_ret)
1270 {
1271 struct componentname *cnp = state->cnp;
1272 struct nameidata *ndp = state->ndp;
1273 krwlock_t *plock;
1274 struct vnode *foundobj, *searchdir;
1275 int error, error2;
1276 size_t oldpathlen;
1277 const char *oldnameptr;
1278 bool terminal;
1279
1280 /*
1281 * Eat as many path name components as possible before giving up and
1282 * letting lookup_once() handle it. Remember the starting point in
1283 * case we can't get vnode references and need to roll back.
1284 */
1285 plock = NULL;
1286 searchdir = *searchdir_ret;
1287 oldnameptr = cnp->cn_nameptr;
1288 oldpathlen = ndp->ni_pathlen;
1289 terminal = false;
1290 for (;;) {
1291 foundobj = NULL;
1292
1293 /*
1294 * Get the next component name. There should be no slashes
1295 * here, and we shouldn't have looped around if we were
1296 * done.
1297 */
1298 KASSERT(cnp->cn_nameptr[0] != '/');
1299 KASSERT(cnp->cn_nameptr[0] != '\0');
1300 if ((error = lookup_parsepath(state, searchdir)) != 0) {
1301 break;
1302 }
1303
1304 /*
1305 * Can't deal with DOTDOT lookups if NOCROSSMOUNT or the
1306 * lookup is chrooted.
1307 */
1308 if ((cnp->cn_flags & ISDOTDOT) != 0) {
1309 if ((searchdir->v_vflag & VV_ROOT) != 0 &&
1310 (cnp->cn_flags & NOCROSSMOUNT)) {
1311 error = EOPNOTSUPP;
1312 break;
1313 }
1314 if (ndp->ni_rootdir != rootvnode) {
1315 error = EOPNOTSUPP;
1316 break;
1317 }
1318 }
1319
1320 /*
1321 * Can't deal with last component when modifying; this needs
1322 * searchdir locked and VOP_LOOKUP() called (which can and
1323 * does modify state, despite the name). NB: this case means
1324 * terminal is never set true when LOCKPARENT.
1325 */
1326 if ((cnp->cn_flags & ISLASTCN) != 0) {
1327 if (cnp->cn_nameiop != LOOKUP ||
1328 (cnp->cn_flags & LOCKPARENT) != 0) {
1329 error = EOPNOTSUPP;
1330 break;
1331 }
1332 }
1333
1334 /*
1335 * Good, now look for it in cache. cache_lookup_linked()
1336 * will fail if there's nothing there, or if there's no
1337 * ownership info for the directory, or if the user doesn't
1338 * have permission to look up files in this directory.
1339 */
1340 if (!cache_lookup_linked(searchdir, cnp->cn_nameptr,
1341 cnp->cn_namelen, &foundobj, &plock, cnp->cn_cred)) {
1342 error = EOPNOTSUPP;
1343 break;
1344 }
1345 KASSERT(plock != NULL && rw_lock_held(plock));
1346
1347 /*
1348 * Scored a hit. Negative is good too (ENOENT). If there's
1349 * a '-o union' mount here, punt and let lookup_once() deal
1350 * with it.
1351 */
1352 if (foundobj == NULL) {
1353 if ((searchdir->v_vflag & VV_ROOT) != 0 &&
1354 (searchdir->v_mount->mnt_flag & MNT_UNION) != 0) {
1355 error = EOPNOTSUPP;
1356 } else {
1357 error = ENOENT;
1358 terminal = ((cnp->cn_flags & ISLASTCN) != 0);
1359 }
1360 break;
1361 }
1362
1363 /*
1364 * Stop and get a hold on the vnode if we've encountered
1365 * something other than a dirctory.
1366 */
1367 if (foundobj->v_type != VDIR) {
1368 error = vcache_tryvget(foundobj);
1369 if (error != 0) {
1370 foundobj = NULL;
1371 error = EOPNOTSUPP;
1372 } else {
1373 terminal = (foundobj->v_type != VLNK &&
1374 (cnp->cn_flags & ISLASTCN) != 0);
1375 }
1376 break;
1377 }
1378
1379 /*
1380 * Try to cross mountpoints, bearing in mind that they can
1381 * be stacked. If at any point we can't go further, stop
1382 * and try to get a reference on the vnode. If we are able
1383 * to get a ref then lookup_crossmount() will take care of
1384 * it, otherwise we'll fall through to lookup_once().
1385 */
1386 if (foundobj->v_mountedhere != NULL) {
1387 while (foundobj->v_mountedhere != NULL &&
1388 (cnp->cn_flags & NOCROSSMOUNT) == 0 &&
1389 cache_cross_mount(&foundobj, &plock)) {
1390 KASSERT(foundobj != NULL);
1391 KASSERT(foundobj->v_type == VDIR);
1392 }
1393 if (foundobj->v_mountedhere != NULL) {
1394 error = vcache_tryvget(foundobj);
1395 if (error != 0) {
1396 foundobj = NULL;
1397 error = EOPNOTSUPP;
1398 }
1399 break;
1400 } else {
1401 searchdir = NULL;
1402 }
1403 }
1404
1405 /*
1406 * Time to stop if we found the last component & traversed
1407 * all mounts.
1408 */
1409 if ((cnp->cn_flags & ISLASTCN) != 0) {
1410 error = vcache_tryvget(foundobj);
1411 if (error != 0) {
1412 foundobj = NULL;
1413 error = EOPNOTSUPP;
1414 } else {
1415 terminal = (foundobj->v_type != VLNK);
1416 }
1417 break;
1418 }
1419
1420 /*
1421 * Otherwise, we're still in business. Set the found VDIR
1422 * vnode as the search dir for the next component and
1423 * continue on to it.
1424 */
1425 cnp->cn_nameptr = ndp->ni_next;
1426 searchdir = foundobj;
1427 }
1428
1429 if (terminal) {
1430 /*
1431 * If we exited the loop above having successfully located
1432 * the last component with a zero error code, and it's not a
1433 * symbolic link, then the parent directory is not needed.
1434 * Release reference to the starting parent and make the
1435 * terminal parent disappear into thin air.
1436 */
1437 KASSERT(plock != NULL);
1438 rw_exit(plock);
1439 vrele(*searchdir_ret);
1440 *searchdir_ret = NULL;
1441 } else if (searchdir != *searchdir_ret) {
1442 /*
1443 * Otherwise we need to return the parent. If we ended up
1444 * with a new search dir, ref it before dropping the
1445 * namecache's lock. The lock prevents both searchdir and
1446 * foundobj from disappearing. If we can't ref the new
1447 * searchdir, we have a bit of a problem. Roll back the
1448 * fastforward to the beginning and let lookup_once() take
1449 * care of it.
1450 */
1451 if (searchdir == NULL) {
1452 /*
1453 * It's possible for searchdir to be NULL in the
1454 * case of a root vnode being reclaimed while
1455 * trying to cross a mount.
1456 */
1457 error2 = EOPNOTSUPP;
1458 } else {
1459 error2 = vcache_tryvget(searchdir);
1460 }
1461 KASSERT(plock != NULL);
1462 rw_exit(plock);
1463 if (__predict_true(error2 == 0)) {
1464 /* Returning new searchdir, and maybe new foundobj. */
1465 vrele(*searchdir_ret);
1466 *searchdir_ret = searchdir;
1467 } else {
1468 /* Returning nothing. */
1469 if (foundobj != NULL) {
1470 vrele(foundobj);
1471 foundobj = NULL;
1472 }
1473 cnp->cn_nameptr = oldnameptr;
1474 ndp->ni_pathlen = oldpathlen;
1475 error = lookup_parsepath(state, *searchdir_ret);
1476 if (error == 0) {
1477 error = EOPNOTSUPP;
1478 }
1479 }
1480 } else if (plock != NULL) {
1481 /* Drop any namecache lock still held. */
1482 rw_exit(plock);
1483 }
1484
1485 KASSERT(error == 0 ? foundobj != NULL : foundobj == NULL);
1486 *foundobj_ret = foundobj;
1487 return error;
1488 }
1489
1490 //////////////////////////////
1491
1492 /*
1493 * Do a complete path search from a single root directory.
1494 * (This is called up to twice if TRYEMULROOT is in effect.)
1495 */
1496 static int
1497 namei_oneroot(struct namei_state *state,
1498 int neverfollow, int inhibitmagic, int isnfsd)
1499 {
1500 struct nameidata *ndp = state->ndp;
1501 struct componentname *cnp = state->cnp;
1502 struct vnode *searchdir, *foundobj;
1503 bool searchdir_locked = false;
1504 int error;
1505
1506 error = namei_start(state, isnfsd, &searchdir);
1507 if (error) {
1508 ndp->ni_dvp = NULL;
1509 ndp->ni_vp = NULL;
1510 return error;
1511 }
1512 KASSERT(searchdir->v_type == VDIR);
1513
1514 /*
1515 * Setup: break out flag bits into variables.
1516 */
1517 state->docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE;
1518 if (cnp->cn_nameiop == DELETE)
1519 state->docache = 0;
1520 state->rdonly = cnp->cn_flags & RDONLY;
1521
1522 /*
1523 * Keep going until we run out of path components.
1524 */
1525 cnp->cn_nameptr = ndp->ni_pnbuf;
1526
1527 /* drop leading slashes (already used them to choose startdir) */
1528 while (cnp->cn_nameptr[0] == '/') {
1529 cnp->cn_nameptr++;
1530 ndp->ni_pathlen--;
1531 }
1532 /* was it just "/"? */
1533 if (cnp->cn_nameptr[0] == '\0') {
1534 foundobj = searchdir;
1535 searchdir = NULL;
1536 cnp->cn_flags |= ISLASTCN;
1537
1538 /* bleh */
1539 goto skiploop;
1540 }
1541
1542 for (;;) {
1543 KASSERT(searchdir != NULL);
1544 KASSERT(!searchdir_locked);
1545
1546 /*
1547 * Parse out the first path name component that we need to
1548 * to consider. While doing this, attempt to use the name
1549 * cache to fast-forward through as many "easy" to find
1550 * components of the path as possible.
1551 */
1552 error = lookup_fastforward(state, &searchdir, &foundobj);
1553
1554 /*
1555 * If we didn't get a good answer from the namecache, then
1556 * go directly to the file system.
1557 */
1558 if (error == EOPNOTSUPP) {
1559 error = lookup_once(state, searchdir, &searchdir,
1560 &foundobj, &searchdir_locked);
1561 }
1562
1563 /*
1564 * If the vnode we found is mounted on, then cross the mount
1565 * and get the root vnode in foundobj. If this encounters
1566 * an error, it will dispose of foundobj, but searchdir is
1567 * untouched.
1568 */
1569 if (error == 0 && foundobj != NULL &&
1570 foundobj->v_type == VDIR &&
1571 foundobj->v_mountedhere != NULL &&
1572 (cnp->cn_flags & NOCROSSMOUNT) == 0) {
1573 error = lookup_crossmount(state, &searchdir,
1574 &foundobj, &searchdir_locked);
1575 }
1576
1577 if (error) {
1578 if (searchdir != NULL) {
1579 if (searchdir_locked) {
1580 searchdir_locked = false;
1581 vput(searchdir);
1582 } else {
1583 vrele(searchdir);
1584 }
1585 }
1586 ndp->ni_dvp = NULL;
1587 ndp->ni_vp = NULL;
1588 /*
1589 * Note that if we're doing TRYEMULROOT we can
1590 * retry with the normal root. Where this is
1591 * currently set matches previous practice,
1592 * but the previous practice didn't make much
1593 * sense and somebody should sit down and
1594 * figure out which cases should cause retry
1595 * and which shouldn't. XXX.
1596 */
1597 state->attempt_retry = 1;
1598 return (error);
1599 }
1600
1601 if (foundobj == NULL) {
1602 /*
1603 * Success with no object returned means we're
1604 * creating something and it isn't already
1605 * there. Break out of the main loop now so
1606 * the code below doesn't have to test for
1607 * foundobj == NULL.
1608 */
1609 /* lookup_once can't have dropped the searchdir */
1610 KASSERT(searchdir != NULL ||
1611 (cnp->cn_flags & ISLASTCN) != 0);
1612 break;
1613 }
1614
1615 /*
1616 * Check for symbolic link. If we've reached one,
1617 * follow it, unless we aren't supposed to. Back up
1618 * over any slashes that we skipped, as we will need
1619 * them again.
1620 */
1621 if (namei_atsymlink(state, foundobj)) {
1622 /* Don't need searchdir locked any more. */
1623 if (searchdir_locked) {
1624 searchdir_locked = false;
1625 VOP_UNLOCK(searchdir);
1626 }
1627 ndp->ni_pathlen += state->slashes;
1628 ndp->ni_next -= state->slashes;
1629 if (neverfollow) {
1630 error = EINVAL;
1631 } else if (searchdir == NULL) {
1632 /*
1633 * dholland 20160410: lookup_once only
1634 * drops searchdir if it crossed a
1635 * mount point. Therefore, if we get
1636 * here it means we crossed a mount
1637 * point to a mounted filesystem whose
1638 * root vnode is a symlink. In theory
1639 * we could continue at this point by
1640 * using the pre-crossing searchdir
1641 * (e.g. just take out an extra
1642 * reference on it before calling
1643 * lookup_once so we still have it),
1644 * but this will make an ugly mess and
1645 * it should never happen in practice
1646 * as only badly broken filesystems
1647 * have non-directory root vnodes. (I
1648 * have seen this sort of thing with
1649 * NFS occasionally but even then it
1650 * means something's badly wrong.)
1651 */
1652 error = ENOTDIR;
1653 } else {
1654 /*
1655 * dholland 20110410: if we're at a
1656 * union mount it might make sense to
1657 * use the top of the union stack here
1658 * rather than the layer we found the
1659 * symlink in. (FUTURE)
1660 */
1661 error = namei_follow(state, inhibitmagic,
1662 searchdir, foundobj,
1663 &searchdir);
1664 }
1665 if (error) {
1666 KASSERT(searchdir != foundobj);
1667 if (searchdir != NULL) {
1668 vrele(searchdir);
1669 }
1670 vrele(foundobj);
1671 ndp->ni_dvp = NULL;
1672 ndp->ni_vp = NULL;
1673 return error;
1674 }
1675 vrele(foundobj);
1676 foundobj = NULL;
1677
1678 /*
1679 * If we followed a symlink to `/' and there
1680 * are no more components after the symlink,
1681 * we're done with the loop and what we found
1682 * is the searchdir.
1683 */
1684 if (cnp->cn_nameptr[0] == '\0') {
1685 KASSERT(searchdir != NULL);
1686 foundobj = searchdir;
1687 searchdir = NULL;
1688 cnp->cn_flags |= ISLASTCN;
1689 break;
1690 }
1691
1692 continue;
1693 }
1694
1695 /*
1696 * Not a symbolic link.
1697 *
1698 * Check for directory, if the component was
1699 * followed by a series of slashes.
1700 */
1701 if ((foundobj->v_type != VDIR) &&
1702 (cnp->cn_flags & REQUIREDIR)) {
1703 KASSERT(foundobj != searchdir);
1704 if (searchdir) {
1705 if (searchdir_locked) {
1706 searchdir_locked = false;
1707 vput(searchdir);
1708 } else {
1709 vrele(searchdir);
1710 }
1711 } else {
1712 KASSERT(!searchdir_locked);
1713 }
1714 vrele(foundobj);
1715 ndp->ni_dvp = NULL;
1716 ndp->ni_vp = NULL;
1717 state->attempt_retry = 1;
1718 return ENOTDIR;
1719 }
1720
1721 /*
1722 * Stop if we've reached the last component.
1723 */
1724 if (cnp->cn_flags & ISLASTCN) {
1725 break;
1726 }
1727
1728 /*
1729 * Continue with the next component.
1730 */
1731 cnp->cn_nameptr = ndp->ni_next;
1732 if (searchdir != NULL) {
1733 if (searchdir_locked) {
1734 searchdir_locked = false;
1735 vput(searchdir);
1736 } else {
1737 vrele(searchdir);
1738 }
1739 }
1740 searchdir = foundobj;
1741 foundobj = NULL;
1742 }
1743
1744 KASSERT((cnp->cn_flags & LOCKPARENT) == 0 || searchdir == NULL ||
1745 VOP_ISLOCKED(searchdir) == LK_EXCLUSIVE);
1746
1747 skiploop:
1748
1749 if (foundobj != NULL) {
1750 if (foundobj == ndp->ni_erootdir) {
1751 /*
1752 * We are about to return the emulation root.
1753 * This isn't a good idea because code might
1754 * repeatedly lookup ".." until the file
1755 * matches that returned for "/" and loop
1756 * forever. So convert it to the real root.
1757 */
1758 if (searchdir != NULL) {
1759 if (searchdir_locked) {
1760 vput(searchdir);
1761 searchdir_locked = false;
1762 } else {
1763 vrele(searchdir);
1764 }
1765 searchdir = NULL;
1766 }
1767 vrele(foundobj);
1768 foundobj = ndp->ni_rootdir;
1769 vref(foundobj);
1770 }
1771
1772 /*
1773 * If the caller requested the parent node (i.e. it's
1774 * a CREATE, DELETE, or RENAME), and we don't have one
1775 * (because this is the root directory, or we crossed
1776 * a mount point), then we must fail.
1777 *
1778 * 20210604 dholland when NONEXCLHACK is set (open
1779 * with O_CREAT but not O_EXCL) skip this logic. Since
1780 * we have a foundobj, open will not be creating, so
1781 * it doesn't actually need or use the searchdir, so
1782 * it's ok to return it even if it's on a different
1783 * volume, and it's also ok to return NULL; by setting
1784 * NONEXCLHACK the open code promises to cope with
1785 * those cases correctly. (That is, it should do what
1786 * it would do anyway, that is, just release the
1787 * searchdir, except not crash if it's null.) This is
1788 * needed because otherwise opening mountpoints with
1789 * O_CREAT but not O_EXCL fails... which is a silly
1790 * thing to do but ought to work. (This whole issue
1791 * came to light because 3rd party code wanted to open
1792 * certain procfs nodes with O_CREAT for some 3rd
1793 * party reason, and it failed.)
1794 *
1795 * Note that NONEXCLHACK is properly a different
1796 * nameiop (it is partway between LOOKUP and CREATE)
1797 * but it was stuffed in as a flag instead to make the
1798 * resulting patch less invasive for pullup. Blah.
1799 */
1800 if (cnp->cn_nameiop != LOOKUP &&
1801 (searchdir == NULL ||
1802 searchdir->v_mount != foundobj->v_mount) &&
1803 (cnp->cn_flags & NONEXCLHACK) == 0) {
1804 if (searchdir) {
1805 if (searchdir_locked) {
1806 vput(searchdir);
1807 searchdir_locked = false;
1808 } else {
1809 vrele(searchdir);
1810 }
1811 searchdir = NULL;
1812 }
1813 vrele(foundobj);
1814 foundobj = NULL;
1815 ndp->ni_dvp = NULL;
1816 ndp->ni_vp = NULL;
1817 state->attempt_retry = 1;
1818
1819 switch (cnp->cn_nameiop) {
1820 case CREATE:
1821 return EEXIST;
1822 case DELETE:
1823 case RENAME:
1824 return EBUSY;
1825 default:
1826 break;
1827 }
1828 panic("Invalid nameiop\n");
1829 }
1830
1831 /*
1832 * Disallow directory write attempts on read-only lookups.
1833 * Prefers EEXIST over EROFS for the CREATE case.
1834 */
1835 if (state->rdonly &&
1836 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
1837 if (searchdir) {
1838 if (searchdir_locked) {
1839 vput(searchdir);
1840 searchdir_locked = false;
1841 } else {
1842 vrele(searchdir);
1843 }
1844 searchdir = NULL;
1845 }
1846 vrele(foundobj);
1847 foundobj = NULL;
1848 ndp->ni_dvp = NULL;
1849 ndp->ni_vp = NULL;
1850 state->attempt_retry = 1;
1851 return EROFS;
1852 }
1853
1854 /* Lock the leaf node if requested. */
1855 if ((cnp->cn_flags & (LOCKLEAF | LOCKPARENT)) == LOCKPARENT &&
1856 searchdir == foundobj) {
1857 /*
1858 * Note: if LOCKPARENT but not LOCKLEAF is
1859 * set, and searchdir == foundobj, this code
1860 * necessarily unlocks the parent as well as
1861 * the leaf. That is, just because you specify
1862 * LOCKPARENT doesn't mean you necessarily get
1863 * a locked parent vnode. The code in
1864 * vfs_syscalls.c, and possibly elsewhere,
1865 * that uses this combination "knows" this, so
1866 * it can't be safely changed. Feh. XXX
1867 */
1868 KASSERT(searchdir_locked);
1869 VOP_UNLOCK(searchdir);
1870 searchdir_locked = false;
1871 } else if ((cnp->cn_flags & LOCKLEAF) != 0 &&
1872 (searchdir != foundobj ||
1873 (cnp->cn_flags & LOCKPARENT) == 0)) {
1874 const int lktype = (cnp->cn_flags & LOCKSHARED) != 0 ?
1875 LK_SHARED : LK_EXCLUSIVE;
1876 vn_lock(foundobj, lktype | LK_RETRY);
1877 }
1878 }
1879
1880 /*
1881 * Done.
1882 */
1883
1884 /*
1885 * If LOCKPARENT is not set, the parent directory isn't returned.
1886 */
1887 if ((cnp->cn_flags & LOCKPARENT) == 0 && searchdir != NULL) {
1888 vrele(searchdir);
1889 searchdir = NULL;
1890 }
1891
1892 ndp->ni_dvp = searchdir;
1893 ndp->ni_vp = foundobj;
1894 return 0;
1895 }
1896
1897 /*
1898 * Do namei; wrapper layer that handles TRYEMULROOT.
1899 */
1900 static int
1901 namei_tryemulroot(struct namei_state *state,
1902 int neverfollow, int inhibitmagic, int isnfsd)
1903 {
1904 int error;
1905
1906 struct nameidata *ndp = state->ndp;
1907 struct componentname *cnp = state->cnp;
1908 const char *savepath = NULL;
1909
1910 KASSERT(cnp == &ndp->ni_cnd);
1911
1912 if (cnp->cn_flags & TRYEMULROOT) {
1913 savepath = pathbuf_stringcopy_get(ndp->ni_pathbuf);
1914 }
1915
1916 emul_retry:
1917 state->attempt_retry = 0;
1918
1919 error = namei_oneroot(state, neverfollow, inhibitmagic, isnfsd);
1920 if (error) {
1921 /*
1922 * Once namei has started up, the existence of ni_erootdir
1923 * tells us whether we're working from an emulation root.
1924 * The TRYEMULROOT flag isn't necessarily authoritative.
1925 */
1926 if (ndp->ni_erootdir != NULL && state->attempt_retry) {
1927 /* Retry the whole thing using the normal root */
1928 cnp->cn_flags &= ~TRYEMULROOT;
1929 state->attempt_retry = 0;
1930
1931 /* kinda gross */
1932 strcpy(ndp->ni_pathbuf->pb_path, savepath);
1933 pathbuf_stringcopy_put(ndp->ni_pathbuf, savepath);
1934 savepath = NULL;
1935
1936 goto emul_retry;
1937 }
1938 }
1939 if (savepath != NULL) {
1940 pathbuf_stringcopy_put(ndp->ni_pathbuf, savepath);
1941 }
1942 return error;
1943 }
1944
1945 /*
1946 * External interface.
1947 */
1948 int
1949 namei(struct nameidata *ndp)
1950 {
1951 struct namei_state state;
1952 int error;
1953
1954 namei_init(&state, ndp);
1955 error = namei_tryemulroot(&state,
1956 0/*!neverfollow*/, 0/*!inhibitmagic*/,
1957 0/*isnfsd*/);
1958 namei_cleanup(&state);
1959
1960 if (error) {
1961 /* make sure no stray refs leak out */
1962 KASSERT(ndp->ni_dvp == NULL);
1963 KASSERT(ndp->ni_vp == NULL);
1964 }
1965
1966 return error;
1967 }
1968
1969 ////////////////////////////////////////////////////////////
1970
1971 /*
1972 * External interface used by nfsd. This is basically different from
1973 * namei only in that it has the ability to pass in the "current
1974 * directory", and uses an extra flag "neverfollow" for which there's
1975 * no physical flag defined in namei.h. (There used to be a cut&paste
1976 * copy of about half of namei in nfsd to allow these minor
1977 * adjustments to exist.)
1978 *
1979 * XXX: the namei interface should be adjusted so nfsd can just use
1980 * ordinary namei().
1981 */
1982 int
1983 lookup_for_nfsd(struct nameidata *ndp, struct vnode *forcecwd, int neverfollow)
1984 {
1985 struct namei_state state;
1986 int error;
1987
1988 KASSERT(ndp->ni_atdir == NULL);
1989 ndp->ni_atdir = forcecwd;
1990
1991 namei_init(&state, ndp);
1992 error = namei_tryemulroot(&state,
1993 neverfollow, 1/*inhibitmagic*/, 1/*isnfsd*/);
1994 namei_cleanup(&state);
1995
1996 if (error) {
1997 /* make sure no stray refs leak out */
1998 KASSERT(ndp->ni_dvp == NULL);
1999 KASSERT(ndp->ni_vp == NULL);
2000 }
2001
2002 return error;
2003 }
2004
2005 /*
2006 * A second external interface used by nfsd. This turns out to be a
2007 * single lookup used by the WebNFS code (ha!) to get "index.html" or
2008 * equivalent when asked for a directory. It should eventually evolve
2009 * into some kind of namei_once() call; for the time being it's kind
2010 * of a mess. XXX.
2011 *
2012 * dholland 20110109: I don't think it works, and I don't think it
2013 * worked before I started hacking and slashing either, and I doubt
2014 * anyone will ever notice.
2015 */
2016
2017 /*
2018 * Internals. This calls lookup_once() after setting up the assorted
2019 * pieces of state the way they ought to be.
2020 */
2021 static int
2022 do_lookup_for_nfsd_index(struct namei_state *state)
2023 {
2024 int error;
2025
2026 struct componentname *cnp = state->cnp;
2027 struct nameidata *ndp = state->ndp;
2028 struct vnode *startdir;
2029 struct vnode *foundobj;
2030 bool startdir_locked;
2031 const char *cp; /* pointer into pathname argument */
2032
2033 KASSERT(cnp == &ndp->ni_cnd);
2034
2035 startdir = state->ndp->ni_atdir;
2036
2037 cnp->cn_nameptr = ndp->ni_pnbuf;
2038 state->docache = 1;
2039 state->rdonly = cnp->cn_flags & RDONLY;
2040 ndp->ni_dvp = NULL;
2041
2042 error = VOP_PARSEPATH(startdir, cnp->cn_nameptr, &cnp->cn_namelen);
2043 if (error) {
2044 return error;
2045 }
2046
2047 cp = cnp->cn_nameptr + cnp->cn_namelen;
2048 KASSERT(cnp->cn_namelen <= KERNEL_NAME_MAX);
2049 ndp->ni_pathlen -= cnp->cn_namelen;
2050 ndp->ni_next = cp;
2051 state->slashes = 0;
2052 cnp->cn_flags &= ~REQUIREDIR;
2053 cnp->cn_flags |= MAKEENTRY|ISLASTCN;
2054
2055 if (cnp->cn_namelen == 2 &&
2056 cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.')
2057 cnp->cn_flags |= ISDOTDOT;
2058 else
2059 cnp->cn_flags &= ~ISDOTDOT;
2060
2061 /*
2062 * Because lookup_once can change the startdir, we need our
2063 * own reference to it to avoid consuming the caller's.
2064 */
2065 vref(startdir);
2066 error = lookup_once(state, startdir, &startdir, &foundobj,
2067 &startdir_locked);
2068
2069 KASSERT((cnp->cn_flags & LOCKPARENT) == 0);
2070 if (startdir_locked) {
2071 VOP_UNLOCK(startdir);
2072 startdir_locked = false;
2073 }
2074
2075 /*
2076 * If the vnode we found is mounted on, then cross the mount and get
2077 * the root vnode in foundobj. If this encounters an error, it will
2078 * dispose of foundobj, but searchdir is untouched.
2079 */
2080 if (error == 0 && foundobj != NULL &&
2081 foundobj->v_type == VDIR &&
2082 foundobj->v_mountedhere != NULL &&
2083 (cnp->cn_flags & NOCROSSMOUNT) == 0) {
2084 error = lookup_crossmount(state, &startdir, &foundobj,
2085 &startdir_locked);
2086 }
2087
2088 /* Now toss startdir and see if we have an error. */
2089 if (startdir != NULL)
2090 vrele(startdir);
2091 if (error)
2092 foundobj = NULL;
2093 else if (foundobj != NULL && (cnp->cn_flags & LOCKLEAF) != 0)
2094 vn_lock(foundobj, LK_EXCLUSIVE | LK_RETRY);
2095
2096 ndp->ni_vp = foundobj;
2097 return (error);
2098 }
2099
2100 /*
2101 * External interface. The partitioning between this function and the
2102 * above isn't very clear - the above function exists mostly so code
2103 * that uses "state->" can be shuffled around without having to change
2104 * it to "state.".
2105 */
2106 int
2107 lookup_for_nfsd_index(struct nameidata *ndp, struct vnode *startdir)
2108 {
2109 struct namei_state state;
2110 int error;
2111
2112 KASSERT(ndp->ni_atdir == NULL);
2113 ndp->ni_atdir = startdir;
2114
2115 /*
2116 * Note: the name sent in here (is not|should not be) allowed
2117 * to contain a slash.
2118 */
2119 if (strlen(ndp->ni_pathbuf->pb_path) > KERNEL_NAME_MAX) {
2120 return ENAMETOOLONG;
2121 }
2122 if (strchr(ndp->ni_pathbuf->pb_path, '/')) {
2123 return EINVAL;
2124 }
2125
2126 ndp->ni_pathlen = strlen(ndp->ni_pathbuf->pb_path) + 1;
2127 ndp->ni_pnbuf = NULL;
2128 ndp->ni_cnd.cn_nameptr = NULL;
2129
2130 namei_init(&state, ndp);
2131 error = do_lookup_for_nfsd_index(&state);
2132 namei_cleanup(&state);
2133
2134 return error;
2135 }
2136
2137 ////////////////////////////////////////////////////////////
2138
2139 /*
2140 * Reacquire a path name component.
2141 * dvp is locked on entry and exit.
2142 * *vpp is locked on exit unless it's NULL.
2143 */
2144 int
2145 relookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp, int dummy)
2146 {
2147 int rdonly; /* lookup read-only flag bit */
2148 int error = 0;
2149 #ifdef DEBUG
2150 size_t newlen; /* DEBUG: check name len */
2151 const char *cp; /* DEBUG: check name ptr */
2152 #endif /* DEBUG */
2153
2154 (void)dummy;
2155
2156 /*
2157 * Setup: break out flag bits into variables.
2158 */
2159 rdonly = cnp->cn_flags & RDONLY;
2160
2161 /*
2162 * Search a new directory.
2163 *
2164 * The cn_hash value is for use by vfs_cache.
2165 * The last component of the filename is left accessible via
2166 * cnp->cn_nameptr for callers that need the name. Callers needing
2167 * the name set the SAVENAME flag. When done, they assume
2168 * responsibility for freeing the pathname buffer.
2169 */
2170 #ifdef DEBUG
2171 #if 0
2172 cp = NULL;
2173 newhash = namei_hash(cnp->cn_nameptr, &cp);
2174 if ((uint32_t)newhash != (uint32_t)cnp->cn_hash)
2175 panic("relookup: bad hash");
2176 #endif
2177 error = VOP_PARSEPATH(dvp, cnp->cn_nameptr, &newlen);
2178 if (error) {
2179 panic("relookup: parsepath failed with error %d", error);
2180 }
2181 if (cnp->cn_namelen != newlen)
2182 panic("relookup: bad len");
2183 cp = cnp->cn_nameptr + cnp->cn_namelen;
2184 while (*cp == '/')
2185 cp++;
2186 if (*cp != 0)
2187 panic("relookup: not last component");
2188 #endif /* DEBUG */
2189
2190 /*
2191 * Check for degenerate name (e.g. / or "")
2192 * which is a way of talking about a directory,
2193 * e.g. like "/." or ".".
2194 */
2195 if (cnp->cn_nameptr[0] == '\0')
2196 panic("relookup: null name");
2197
2198 if (cnp->cn_flags & ISDOTDOT)
2199 panic("relookup: lookup on dot-dot");
2200
2201 /*
2202 * We now have a segment name to search for, and a directory to search.
2203 */
2204 *vpp = NULL;
2205 error = VOP_LOOKUP(dvp, vpp, cnp);
2206 if ((error) != 0) {
2207 KASSERTMSG((*vpp == NULL),
2208 "leaf `%s' should be empty but is %p",
2209 cnp->cn_nameptr, *vpp);
2210 if (error != EJUSTRETURN)
2211 goto bad;
2212 }
2213
2214 /*
2215 * Check for symbolic link
2216 */
2217 KASSERTMSG((*vpp == NULL || (*vpp)->v_type != VLNK ||
2218 (cnp->cn_flags & FOLLOW) == 0),
2219 "relookup: symlink found");
2220
2221 /*
2222 * Check for read-only lookups.
2223 */
2224 if (rdonly && cnp->cn_nameiop != LOOKUP) {
2225 error = EROFS;
2226 if (*vpp) {
2227 vrele(*vpp);
2228 }
2229 goto bad;
2230 }
2231 /*
2232 * Lock result.
2233 */
2234 if (*vpp && *vpp != dvp) {
2235 error = vn_lock(*vpp, LK_EXCLUSIVE);
2236 if (error != 0) {
2237 vrele(*vpp);
2238 goto bad;
2239 }
2240 }
2241 return (0);
2242
2243 bad:
2244 *vpp = NULL;
2245 return (error);
2246 }
2247
2248 /*
2249 * namei_simple - simple forms of namei.
2250 *
2251 * These are wrappers to allow the simple case callers of namei to be
2252 * left alone while everything else changes under them.
2253 */
2254
2255 /* Flags */
2256 struct namei_simple_flags_type {
2257 int dummy;
2258 };
2259 static const struct namei_simple_flags_type ns_nn, ns_nt, ns_fn, ns_ft;
2260 const namei_simple_flags_t NSM_NOFOLLOW_NOEMULROOT = &ns_nn;
2261 const namei_simple_flags_t NSM_NOFOLLOW_TRYEMULROOT = &ns_nt;
2262 const namei_simple_flags_t NSM_FOLLOW_NOEMULROOT = &ns_fn;
2263 const namei_simple_flags_t NSM_FOLLOW_TRYEMULROOT = &ns_ft;
2264
2265 static
2266 int
2267 namei_simple_convert_flags(namei_simple_flags_t sflags)
2268 {
2269 if (sflags == NSM_NOFOLLOW_NOEMULROOT)
2270 return NOFOLLOW | 0;
2271 if (sflags == NSM_NOFOLLOW_TRYEMULROOT)
2272 return NOFOLLOW | TRYEMULROOT;
2273 if (sflags == NSM_FOLLOW_NOEMULROOT)
2274 return FOLLOW | 0;
2275 if (sflags == NSM_FOLLOW_TRYEMULROOT)
2276 return FOLLOW | TRYEMULROOT;
2277 panic("namei_simple_convert_flags: bogus sflags\n");
2278 return 0;
2279 }
2280
2281 int
2282 namei_simple_kernel(const char *path, namei_simple_flags_t sflags,
2283 struct vnode **vp_ret)
2284 {
2285 return nameiat_simple_kernel(NULL, path, sflags, vp_ret);
2286 }
2287
2288 int
2289 nameiat_simple_kernel(struct vnode *dvp, const char *path,
2290 namei_simple_flags_t sflags, struct vnode **vp_ret)
2291 {
2292 struct nameidata nd;
2293 struct pathbuf *pb;
2294 int err;
2295
2296 pb = pathbuf_create(path);
2297 if (pb == NULL) {
2298 return ENOMEM;
2299 }
2300
2301 NDINIT(&nd,
2302 LOOKUP,
2303 namei_simple_convert_flags(sflags),
2304 pb);
2305
2306 if (dvp != NULL)
2307 NDAT(&nd, dvp);
2308
2309 err = namei(&nd);
2310 if (err != 0) {
2311 pathbuf_destroy(pb);
2312 return err;
2313 }
2314 *vp_ret = nd.ni_vp;
2315 pathbuf_destroy(pb);
2316 return 0;
2317 }
2318
2319 int
2320 namei_simple_user(const char *path, namei_simple_flags_t sflags,
2321 struct vnode **vp_ret)
2322 {
2323 return nameiat_simple_user(NULL, path, sflags, vp_ret);
2324 }
2325
2326 int
2327 nameiat_simple_user(struct vnode *dvp, const char *path,
2328 namei_simple_flags_t sflags, struct vnode **vp_ret)
2329 {
2330 struct pathbuf *pb;
2331 struct nameidata nd;
2332 int err;
2333
2334 err = pathbuf_copyin(path, &pb);
2335 if (err) {
2336 return err;
2337 }
2338
2339 NDINIT(&nd,
2340 LOOKUP,
2341 namei_simple_convert_flags(sflags),
2342 pb);
2343
2344 if (dvp != NULL)
2345 NDAT(&nd, dvp);
2346
2347 err = namei(&nd);
2348 if (err != 0) {
2349 pathbuf_destroy(pb);
2350 return err;
2351 }
2352 *vp_ret = nd.ni_vp;
2353 pathbuf_destroy(pb);
2354 return 0;
2355 }
2356