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