vfs_lookup.c revision 1.201.4.1 1 /* $NetBSD: vfs_lookup.c,v 1.201.4.1 2017/07/10 13:12:04 martin 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.201.4.1 2017/07/10 13:12:04 martin Exp $");
41
42 #include "opt_magiclinks.h"
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/syslimits.h>
48 #include <sys/time.h>
49 #include <sys/namei.h>
50 #include <sys/vnode.h>
51 #include <sys/mount.h>
52 #include <sys/errno.h>
53 #include <sys/filedesc.h>
54 #include <sys/hash.h>
55 #include <sys/proc.h>
56 #include <sys/syslog.h>
57 #include <sys/kauth.h>
58 #include <sys/ktrace.h>
59 #include <sys/dirent.h>
60
61 #ifndef MAGICLINKS
62 #define MAGICLINKS 0
63 #endif
64
65 int vfs_magiclinks = MAGICLINKS;
66
67 __CTASSERT(MAXNAMLEN == NAME_MAX);
68
69 /*
70 * Substitute replacement text for 'magic' strings in symlinks.
71 * Returns 0 if successful, and returns non-zero if an error
72 * occurs. (Currently, the only possible error is running out
73 * of temporary pathname space.)
74 *
75 * Looks for "@<string>" and "@<string>/", where <string> is a
76 * recognized 'magic' string. Replaces the "@<string>" with the
77 * appropriate replacement text. (Note that in some cases the
78 * replacement text may have zero length.)
79 *
80 * This would have been table driven, but the variance in
81 * replacement strings (and replacement string lengths) made
82 * that impractical.
83 */
84 #define VNL(x) \
85 (sizeof(x) - 1)
86
87 #define VO '{'
88 #define VC '}'
89
90 #define MATCH(str) \
91 ((termchar == '/' && i + VNL(str) == *len) || \
92 (i + VNL(str) < *len && \
93 cp[i + VNL(str)] == termchar)) && \
94 !strncmp((str), &cp[i], VNL(str))
95
96 #define SUBSTITUTE(m, s, sl) \
97 if ((newlen + (sl)) >= MAXPATHLEN) \
98 return 1; \
99 i += VNL(m); \
100 if (termchar != '/') \
101 i++; \
102 (void)memcpy(&tmp[newlen], (s), (sl)); \
103 newlen += (sl); \
104 change = 1; \
105 termchar = '/';
106
107 static int
108 symlink_magic(struct proc *p, char *cp, size_t *len)
109 {
110 char *tmp;
111 size_t change, i, newlen, slen;
112 char termchar = '/';
113 char idtmp[11]; /* enough for 32 bit *unsigned* integer */
114
115
116 tmp = PNBUF_GET();
117 for (change = i = newlen = 0; i < *len; ) {
118 if (cp[i] != '@') {
119 tmp[newlen++] = cp[i++];
120 continue;
121 }
122
123 i++;
124
125 /* Check for @{var} syntax. */
126 if (cp[i] == VO) {
127 termchar = VC;
128 i++;
129 }
130
131 /*
132 * The following checks should be ordered according
133 * to frequency of use.
134 */
135 if (MATCH("machine_arch")) {
136 slen = VNL(MACHINE_ARCH);
137 SUBSTITUTE("machine_arch", MACHINE_ARCH, slen);
138 } else if (MATCH("machine")) {
139 slen = VNL(MACHINE);
140 SUBSTITUTE("machine", MACHINE, slen);
141 } else if (MATCH("hostname")) {
142 SUBSTITUTE("hostname", hostname, hostnamelen);
143 } else if (MATCH("osrelease")) {
144 slen = strlen(osrelease);
145 SUBSTITUTE("osrelease", osrelease, slen);
146 } else if (MATCH("emul")) {
147 slen = strlen(p->p_emul->e_name);
148 SUBSTITUTE("emul", p->p_emul->e_name, slen);
149 } else if (MATCH("kernel_ident")) {
150 slen = strlen(kernel_ident);
151 SUBSTITUTE("kernel_ident", kernel_ident, slen);
152 } else if (MATCH("domainname")) {
153 SUBSTITUTE("domainname", domainname, domainnamelen);
154 } else if (MATCH("ostype")) {
155 slen = strlen(ostype);
156 SUBSTITUTE("ostype", ostype, slen);
157 } else if (MATCH("uid")) {
158 slen = snprintf(idtmp, sizeof(idtmp), "%u",
159 kauth_cred_geteuid(kauth_cred_get()));
160 SUBSTITUTE("uid", idtmp, slen);
161 } else if (MATCH("ruid")) {
162 slen = snprintf(idtmp, sizeof(idtmp), "%u",
163 kauth_cred_getuid(kauth_cred_get()));
164 SUBSTITUTE("ruid", idtmp, slen);
165 } else if (MATCH("gid")) {
166 slen = snprintf(idtmp, sizeof(idtmp), "%u",
167 kauth_cred_getegid(kauth_cred_get()));
168 SUBSTITUTE("gid", idtmp, slen);
169 } else if (MATCH("rgid")) {
170 slen = snprintf(idtmp, sizeof(idtmp), "%u",
171 kauth_cred_getgid(kauth_cred_get()));
172 SUBSTITUTE("rgid", idtmp, slen);
173 } else {
174 tmp[newlen++] = '@';
175 if (termchar == VC)
176 tmp[newlen++] = VO;
177 }
178 }
179
180 if (change) {
181 (void)memcpy(cp, tmp, newlen);
182 *len = newlen;
183 }
184 PNBUF_PUT(tmp);
185
186 return 0;
187 }
188
189 #undef VNL
190 #undef VO
191 #undef VC
192 #undef MATCH
193 #undef SUBSTITUTE
194
195 ////////////////////////////////////////////////////////////
196
197 /*
198 * Determine the namei hash (for the namecache) for name.
199 * If *ep != NULL, hash from name to ep-1.
200 * If *ep == NULL, hash from name until the first NUL or '/', and
201 * return the location of this termination character in *ep.
202 *
203 * This function returns an equivalent hash to the MI hash32_strn().
204 * The latter isn't used because in the *ep == NULL case, determining
205 * the length of the string to the first NUL or `/' and then calling
206 * hash32_strn() involves unnecessary double-handling of the data.
207 */
208 uint32_t
209 namei_hash(const char *name, const char **ep)
210 {
211 uint32_t hash;
212
213 hash = HASH32_STR_INIT;
214 if (*ep != NULL) {
215 for (; name < *ep; name++)
216 hash = hash * 33 + *(const uint8_t *)name;
217 } else {
218 for (; *name != '\0' && *name != '/'; name++)
219 hash = hash * 33 + *(const uint8_t *)name;
220 *ep = name;
221 }
222 return (hash + (hash >> 5));
223 }
224
225 /*
226 * Find the end of the first path component in NAME and return its
227 * length.
228 */
229 static size_t
230 namei_getcomponent(const char *name)
231 {
232 size_t pos;
233
234 pos = 0;
235 while (name[pos] != '\0' && name[pos] != '/') {
236 pos++;
237 }
238 return pos;
239 }
240
241 ////////////////////////////////////////////////////////////
242
243 /*
244 * Sealed abstraction for pathnames.
245 *
246 * System-call-layer level code that is going to call namei should
247 * first create a pathbuf and adjust all the bells and whistles on it
248 * as needed by context.
249 */
250
251 struct pathbuf {
252 char *pb_path;
253 char *pb_pathcopy;
254 unsigned pb_pathcopyuses;
255 };
256
257 static struct pathbuf *
258 pathbuf_create_raw(void)
259 {
260 struct pathbuf *pb;
261
262 pb = kmem_alloc(sizeof(*pb), KM_SLEEP);
263 if (pb == NULL) {
264 return NULL;
265 }
266 pb->pb_path = PNBUF_GET();
267 if (pb->pb_path == NULL) {
268 kmem_free(pb, sizeof(*pb));
269 return NULL;
270 }
271 pb->pb_pathcopy = NULL;
272 pb->pb_pathcopyuses = 0;
273 return pb;
274 }
275
276 void
277 pathbuf_destroy(struct pathbuf *pb)
278 {
279 KASSERT(pb->pb_pathcopyuses == 0);
280 KASSERT(pb->pb_pathcopy == NULL);
281 PNBUF_PUT(pb->pb_path);
282 kmem_free(pb, sizeof(*pb));
283 }
284
285 struct pathbuf *
286 pathbuf_assimilate(char *pnbuf)
287 {
288 struct pathbuf *pb;
289
290 pb = kmem_alloc(sizeof(*pb), KM_SLEEP);
291 if (pb == NULL) {
292 return NULL;
293 }
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 };
477
478
479 /*
480 * Initialize the namei working state.
481 */
482 static void
483 namei_init(struct namei_state *state, struct nameidata *ndp)
484 {
485 state->ndp = ndp;
486 state->cnp = &ndp->ni_cnd;
487 KASSERT((state->cnp->cn_flags & INRELOOKUP) == 0);
488
489 state->docache = 0;
490 state->rdonly = 0;
491 state->slashes = 0;
492
493 #ifdef DIAGNOSTIC
494 if (!state->cnp->cn_cred)
495 panic("namei: bad cred/proc");
496 if (state->cnp->cn_nameiop & (~OPMASK))
497 panic("namei: nameiop contaminated with flags");
498 if (state->cnp->cn_flags & OPMASK)
499 panic("namei: flags contaminated with nameiops");
500 #endif
501
502 /*
503 * The buffer for name translation shall be the one inside the
504 * pathbuf.
505 */
506 state->ndp->ni_pnbuf = state->ndp->ni_pathbuf->pb_path;
507 }
508
509 /*
510 * Clean up the working namei state, leaving things ready for return
511 * from namei.
512 */
513 static void
514 namei_cleanup(struct namei_state *state)
515 {
516 KASSERT(state->cnp == &state->ndp->ni_cnd);
517
518 /* nothing for now */
519 (void)state;
520 }
521
522 //////////////////////////////
523
524 /*
525 * Get the directory context.
526 * Initializes the rootdir and erootdir state and returns a reference
527 * to the starting dir.
528 */
529 static struct vnode *
530 namei_getstartdir(struct namei_state *state)
531 {
532 struct nameidata *ndp = state->ndp;
533 struct componentname *cnp = state->cnp;
534 struct cwdinfo *cwdi; /* pointer to cwd state */
535 struct lwp *self = curlwp; /* thread doing namei() */
536 struct vnode *rootdir, *erootdir, *curdir, *startdir;
537
538 cwdi = self->l_proc->p_cwdi;
539 rw_enter(&cwdi->cwdi_lock, RW_READER);
540
541 /* root dir */
542 if (cwdi->cwdi_rdir == NULL || (cnp->cn_flags & NOCHROOT)) {
543 rootdir = rootvnode;
544 } else {
545 rootdir = cwdi->cwdi_rdir;
546 }
547
548 /* emulation root dir, if any */
549 if ((cnp->cn_flags & TRYEMULROOT) == 0) {
550 /* if we don't want it, don't fetch it */
551 erootdir = NULL;
552 } else if (cnp->cn_flags & EMULROOTSET) {
553 /* explicitly set emulroot; "/../" doesn't override this */
554 erootdir = ndp->ni_erootdir;
555 } else if (!strncmp(ndp->ni_pnbuf, "/../", 4)) {
556 /* explicit reference to real rootdir */
557 erootdir = NULL;
558 } else {
559 /* may be null */
560 erootdir = cwdi->cwdi_edir;
561 }
562
563 /* current dir */
564 curdir = cwdi->cwdi_cdir;
565
566 if (ndp->ni_pnbuf[0] != '/') {
567 if (ndp->ni_atdir != NULL) {
568 startdir = ndp->ni_atdir;
569 } else {
570 startdir = curdir;
571 }
572 erootdir = NULL;
573 } else if (cnp->cn_flags & TRYEMULROOT && erootdir != NULL) {
574 startdir = erootdir;
575 } else {
576 startdir = rootdir;
577 erootdir = NULL;
578 }
579
580 state->ndp->ni_rootdir = rootdir;
581 state->ndp->ni_erootdir = erootdir;
582
583 /*
584 * Get a reference to the start dir so we can safely unlock cwdi.
585 *
586 * XXX: should we hold references to rootdir and erootdir while
587 * we're running? What happens if a multithreaded process chroots
588 * during namei?
589 */
590 vref(startdir);
591
592 rw_exit(&cwdi->cwdi_lock);
593 return startdir;
594 }
595
596 /*
597 * Get the directory context for the nfsd case, in parallel to
598 * getstartdir. Initializes the rootdir and erootdir state and
599 * returns a reference to the passed-in starting dir.
600 */
601 static struct vnode *
602 namei_getstartdir_for_nfsd(struct namei_state *state)
603 {
604 KASSERT(state->ndp->ni_atdir != NULL);
605
606 /* always use the real root, and never set an emulation root */
607 state->ndp->ni_rootdir = rootvnode;
608 state->ndp->ni_erootdir = NULL;
609
610 vref(state->ndp->ni_atdir);
611 return state->ndp->ni_atdir;
612 }
613
614
615 /*
616 * Ktrace the namei operation.
617 */
618 static void
619 namei_ktrace(struct namei_state *state)
620 {
621 struct nameidata *ndp = state->ndp;
622 struct componentname *cnp = state->cnp;
623 struct lwp *self = curlwp; /* thread doing namei() */
624 const char *emul_path;
625
626 if (ktrpoint(KTR_NAMEI)) {
627 if (ndp->ni_erootdir != NULL) {
628 /*
629 * To make any sense, the trace entry need to have the
630 * text of the emulation path prepended.
631 * Usually we can get this from the current process,
632 * but when called from emul_find_interp() it is only
633 * in the exec_package - so we get it passed in ni_next
634 * (this is a hack).
635 */
636 if (cnp->cn_flags & EMULROOTSET)
637 emul_path = ndp->ni_next;
638 else
639 emul_path = self->l_proc->p_emul->e_path;
640 ktrnamei2(emul_path, strlen(emul_path),
641 ndp->ni_pnbuf, ndp->ni_pathlen);
642 } else
643 ktrnamei(ndp->ni_pnbuf, ndp->ni_pathlen);
644 }
645 }
646
647 /*
648 * Start up namei. Find the root dir and cwd, establish the starting
649 * directory for lookup, and lock it. Also calls ktrace when
650 * appropriate.
651 */
652 static int
653 namei_start(struct namei_state *state, int isnfsd,
654 struct vnode **startdir_ret)
655 {
656 struct nameidata *ndp = state->ndp;
657 struct vnode *startdir;
658
659 /* length includes null terminator (was originally from copyinstr) */
660 ndp->ni_pathlen = strlen(ndp->ni_pnbuf) + 1;
661
662 /*
663 * POSIX.1 requirement: "" is not a valid file name.
664 */
665 if (ndp->ni_pathlen == 1) {
666 return ENOENT;
667 }
668
669 ndp->ni_loopcnt = 0;
670
671 /* Get starting directory, set up root, and ktrace. */
672 if (isnfsd) {
673 startdir = namei_getstartdir_for_nfsd(state);
674 /* no ktrace */
675 } else {
676 startdir = namei_getstartdir(state);
677 namei_ktrace(state);
678 }
679
680 /* NDAT may feed us with a non directory namei_getstartdir */
681 if (startdir->v_type != VDIR) {
682 vrele(startdir);
683 return ENOTDIR;
684 }
685
686 vn_lock(startdir, LK_EXCLUSIVE | LK_RETRY);
687
688 *startdir_ret = startdir;
689 return 0;
690 }
691
692 /*
693 * Check for being at a symlink that we're going to follow.
694 */
695 static inline int
696 namei_atsymlink(struct namei_state *state, struct vnode *foundobj)
697 {
698 return (foundobj->v_type == VLNK) &&
699 (state->cnp->cn_flags & (FOLLOW|REQUIREDIR));
700 }
701
702 /*
703 * Follow a symlink.
704 *
705 * Updates searchdir. inhibitmagic causes magic symlinks to not be
706 * interpreted; this is used by nfsd.
707 *
708 * Unlocks foundobj on success (ugh)
709 */
710 static inline int
711 namei_follow(struct namei_state *state, int inhibitmagic,
712 struct vnode *searchdir, struct vnode *foundobj,
713 struct vnode **newsearchdir_ret)
714 {
715 struct nameidata *ndp = state->ndp;
716 struct componentname *cnp = state->cnp;
717
718 struct lwp *self = curlwp; /* thread doing namei() */
719 struct iovec aiov; /* uio for reading symbolic links */
720 struct uio auio;
721 char *cp; /* pointer into pathname argument */
722 size_t linklen;
723 int error;
724
725 KASSERT(VOP_ISLOCKED(searchdir) == LK_EXCLUSIVE);
726 KASSERT(VOP_ISLOCKED(foundobj) == LK_EXCLUSIVE);
727 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
728 return ELOOP;
729 }
730 if (foundobj->v_mount->mnt_flag & MNT_SYMPERM) {
731 error = VOP_ACCESS(foundobj, VEXEC, cnp->cn_cred);
732 if (error != 0)
733 return error;
734 }
735
736 /* FUTURE: fix this to not use a second buffer */
737 cp = PNBUF_GET();
738 aiov.iov_base = cp;
739 aiov.iov_len = MAXPATHLEN;
740 auio.uio_iov = &aiov;
741 auio.uio_iovcnt = 1;
742 auio.uio_offset = 0;
743 auio.uio_rw = UIO_READ;
744 auio.uio_resid = MAXPATHLEN;
745 UIO_SETUP_SYSSPACE(&auio);
746 error = VOP_READLINK(foundobj, &auio, cnp->cn_cred);
747 if (error) {
748 PNBUF_PUT(cp);
749 return error;
750 }
751 linklen = MAXPATHLEN - auio.uio_resid;
752 if (linklen == 0) {
753 PNBUF_PUT(cp);
754 return ENOENT;
755 }
756
757 /*
758 * Do symlink substitution, if appropriate, and
759 * check length for potential overflow.
760 *
761 * Inhibit symlink substitution for nfsd.
762 * XXX: This is how it was before; is that a bug or a feature?
763 */
764 if ((!inhibitmagic && vfs_magiclinks &&
765 symlink_magic(self->l_proc, cp, &linklen)) ||
766 (linklen + ndp->ni_pathlen >= MAXPATHLEN)) {
767 PNBUF_PUT(cp);
768 return ENAMETOOLONG;
769 }
770 if (ndp->ni_pathlen > 1) {
771 /* includes a null-terminator */
772 memcpy(cp + linklen, ndp->ni_next, ndp->ni_pathlen);
773 } else {
774 cp[linklen] = '\0';
775 }
776 ndp->ni_pathlen += linklen;
777 memcpy(ndp->ni_pnbuf, cp, ndp->ni_pathlen);
778 PNBUF_PUT(cp);
779
780 /* we're now starting from the beginning of the buffer again */
781 cnp->cn_nameptr = ndp->ni_pnbuf;
782
783 /* must unlock this before relocking searchdir */
784 VOP_UNLOCK(foundobj);
785
786 /*
787 * Check if root directory should replace current directory.
788 */
789 if (ndp->ni_pnbuf[0] == '/') {
790 vput(searchdir);
791 /* Keep absolute symbolic links inside emulation root */
792 searchdir = ndp->ni_erootdir;
793 if (searchdir == NULL ||
794 (ndp->ni_pnbuf[1] == '.'
795 && ndp->ni_pnbuf[2] == '.'
796 && ndp->ni_pnbuf[3] == '/')) {
797 ndp->ni_erootdir = NULL;
798 searchdir = ndp->ni_rootdir;
799 }
800 vref(searchdir);
801 vn_lock(searchdir, LK_EXCLUSIVE | LK_RETRY);
802 while (cnp->cn_nameptr[0] == '/') {
803 cnp->cn_nameptr++;
804 ndp->ni_pathlen--;
805 }
806 }
807
808 *newsearchdir_ret = searchdir;
809 KASSERT(VOP_ISLOCKED(searchdir) == LK_EXCLUSIVE);
810 return 0;
811 }
812
813 //////////////////////////////
814
815 /*
816 * Inspect the leading path component and update the state accordingly.
817 */
818 static int
819 lookup_parsepath(struct namei_state *state)
820 {
821 const char *cp; /* pointer into pathname argument */
822
823 struct componentname *cnp = state->cnp;
824 struct nameidata *ndp = state->ndp;
825
826 KASSERT(cnp == &ndp->ni_cnd);
827
828 /*
829 * Search a new directory.
830 *
831 * The last component of the filename is left accessible via
832 * cnp->cn_nameptr for callers that need the name. Callers needing
833 * the name set the SAVENAME flag. When done, they assume
834 * responsibility for freeing the pathname buffer.
835 *
836 * At this point, our only vnode state is that the search dir
837 * is held and locked.
838 */
839 cnp->cn_consume = 0;
840 cnp->cn_namelen = namei_getcomponent(cnp->cn_nameptr);
841 cp = cnp->cn_nameptr + cnp->cn_namelen;
842 if (cnp->cn_namelen > KERNEL_NAME_MAX) {
843 return ENAMETOOLONG;
844 }
845 #ifdef NAMEI_DIAGNOSTIC
846 { char c = *cp;
847 *(char *)cp = '\0';
848 printf("{%s}: ", cnp->cn_nameptr);
849 *(char *)cp = c; }
850 #endif /* NAMEI_DIAGNOSTIC */
851 ndp->ni_pathlen -= cnp->cn_namelen;
852 ndp->ni_next = cp;
853 /*
854 * If this component is followed by a slash, then move the pointer to
855 * the next component forward, and remember that this component must be
856 * a directory.
857 */
858 if (*cp == '/') {
859 do {
860 cp++;
861 } while (*cp == '/');
862 state->slashes = cp - ndp->ni_next;
863 ndp->ni_pathlen -= state->slashes;
864 ndp->ni_next = cp;
865 cnp->cn_flags |= REQUIREDIR;
866 } else {
867 state->slashes = 0;
868 cnp->cn_flags &= ~REQUIREDIR;
869 }
870 /*
871 * We do special processing on the last component, whether or not it's
872 * a directory. Cache all intervening lookups, but not the final one.
873 */
874 if (*cp == '\0') {
875 if (state->docache)
876 cnp->cn_flags |= MAKEENTRY;
877 else
878 cnp->cn_flags &= ~MAKEENTRY;
879 cnp->cn_flags |= ISLASTCN;
880 } else {
881 cnp->cn_flags |= MAKEENTRY;
882 cnp->cn_flags &= ~ISLASTCN;
883 }
884 if (cnp->cn_namelen == 2 &&
885 cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.')
886 cnp->cn_flags |= ISDOTDOT;
887 else
888 cnp->cn_flags &= ~ISDOTDOT;
889
890 return 0;
891 }
892
893 /*
894 * Call VOP_LOOKUP for a single lookup; return a new search directory
895 * (used when crossing mountpoints up or searching union mounts down) and
896 * the found object, which for create operations may be NULL on success.
897 */
898 static int
899 lookup_once(struct namei_state *state,
900 struct vnode *searchdir,
901 struct vnode **newsearchdir_ret,
902 struct vnode **foundobj_ret)
903 {
904 struct vnode *tmpvn; /* scratch vnode */
905 struct vnode *foundobj; /* result */
906 struct mount *mp; /* mount table entry */
907 struct lwp *l = curlwp;
908 int error;
909
910 struct componentname *cnp = state->cnp;
911 struct nameidata *ndp = state->ndp;
912
913 KASSERT(cnp == &ndp->ni_cnd);
914 KASSERT(VOP_ISLOCKED(searchdir) == LK_EXCLUSIVE);
915 *newsearchdir_ret = searchdir;
916
917 /*
918 * Handle "..": two special cases.
919 * 1. If at root directory (e.g. after chroot)
920 * or at absolute root directory
921 * then ignore it so can't get out.
922 * 1a. If at the root of the emulation filesystem go to the real
923 * root. So "/../<path>" is always absolute.
924 * 1b. If we have somehow gotten out of a jail, warn
925 * and also ignore it so we can't get farther out.
926 * 2. If this vnode is the root of a mounted
927 * filesystem, then replace it with the
928 * vnode which was mounted on so we take the
929 * .. in the other file system.
930 */
931 if (cnp->cn_flags & ISDOTDOT) {
932 struct proc *p = l->l_proc;
933
934 for (;;) {
935 if (searchdir == ndp->ni_rootdir ||
936 searchdir == rootvnode) {
937 foundobj = searchdir;
938 vref(foundobj);
939 *foundobj_ret = foundobj;
940 error = 0;
941 goto done;
942 }
943 if (ndp->ni_rootdir != rootvnode) {
944 int retval;
945
946 VOP_UNLOCK(searchdir);
947 retval = vn_isunder(searchdir, ndp->ni_rootdir, l);
948 vn_lock(searchdir, LK_EXCLUSIVE | LK_RETRY);
949 if (!retval) {
950 /* Oops! We got out of jail! */
951 log(LOG_WARNING,
952 "chrooted pid %d uid %d (%s) "
953 "detected outside of its chroot\n",
954 p->p_pid, kauth_cred_geteuid(l->l_cred),
955 p->p_comm);
956 /* Put us at the jail root. */
957 vput(searchdir);
958 searchdir = NULL;
959 foundobj = ndp->ni_rootdir;
960 vref(foundobj);
961 vref(foundobj);
962 vn_lock(foundobj, LK_EXCLUSIVE | LK_RETRY);
963 *newsearchdir_ret = foundobj;
964 *foundobj_ret = foundobj;
965 error = 0;
966 goto done;
967 }
968 }
969 if ((searchdir->v_vflag & VV_ROOT) == 0 ||
970 (cnp->cn_flags & NOCROSSMOUNT))
971 break;
972 tmpvn = searchdir;
973 searchdir = searchdir->v_mount->mnt_vnodecovered;
974 vref(searchdir);
975 vput(tmpvn);
976 vn_lock(searchdir, LK_EXCLUSIVE | LK_RETRY);
977 *newsearchdir_ret = searchdir;
978 }
979 }
980
981 /*
982 * We now have a segment name to search for, and a directory to search.
983 * Our vnode state here is that "searchdir" is held and locked.
984 */
985 unionlookup:
986 foundobj = NULL;
987 error = VOP_LOOKUP(searchdir, &foundobj, cnp);
988
989 if (error != 0) {
990 #ifdef DIAGNOSTIC
991 if (foundobj != NULL)
992 panic("leaf `%s' should be empty", cnp->cn_nameptr);
993 #endif /* DIAGNOSTIC */
994 #ifdef NAMEI_DIAGNOSTIC
995 printf("not found\n");
996 #endif /* NAMEI_DIAGNOSTIC */
997 if ((error == ENOENT) &&
998 (searchdir->v_vflag & VV_ROOT) &&
999 (searchdir->v_mount->mnt_flag & MNT_UNION)) {
1000 tmpvn = searchdir;
1001 searchdir = searchdir->v_mount->mnt_vnodecovered;
1002 vref(searchdir);
1003 vput(tmpvn);
1004 vn_lock(searchdir, LK_EXCLUSIVE | LK_RETRY);
1005 *newsearchdir_ret = searchdir;
1006 goto unionlookup;
1007 }
1008
1009 if (error != EJUSTRETURN)
1010 goto done;
1011
1012 /*
1013 * If this was not the last component, or there were trailing
1014 * slashes, and we are not going to create a directory,
1015 * then the name must exist.
1016 */
1017 if ((cnp->cn_flags & (REQUIREDIR | CREATEDIR)) == REQUIREDIR) {
1018 error = ENOENT;
1019 goto done;
1020 }
1021
1022 /*
1023 * If creating and at end of pathname, then can consider
1024 * allowing file to be created.
1025 */
1026 if (state->rdonly) {
1027 error = EROFS;
1028 goto done;
1029 }
1030
1031 /*
1032 * We return success and a NULL foundobj to indicate
1033 * that the entry doesn't currently exist, leaving a
1034 * pointer to the (normally, locked) directory vnode
1035 * as searchdir.
1036 */
1037 *foundobj_ret = NULL;
1038 error = 0;
1039 goto done;
1040 }
1041 #ifdef NAMEI_DIAGNOSTIC
1042 printf("found\n");
1043 #endif /* NAMEI_DIAGNOSTIC */
1044
1045 /*
1046 * Take into account any additional components consumed by the
1047 * underlying filesystem. This will include any trailing slashes after
1048 * the last component consumed.
1049 */
1050 if (cnp->cn_consume > 0) {
1051 ndp->ni_pathlen -= cnp->cn_consume - state->slashes;
1052 ndp->ni_next += cnp->cn_consume - state->slashes;
1053 cnp->cn_consume = 0;
1054 if (ndp->ni_next[0] == '\0')
1055 cnp->cn_flags |= ISLASTCN;
1056 }
1057
1058 /*
1059 * "searchdir" is locked and held, "foundobj" is held,
1060 * they may be the same vnode.
1061 */
1062 if (searchdir != foundobj) {
1063 if (cnp->cn_flags & ISDOTDOT)
1064 VOP_UNLOCK(searchdir);
1065 error = vn_lock(foundobj, LK_EXCLUSIVE);
1066 if (cnp->cn_flags & ISDOTDOT)
1067 vn_lock(searchdir, LK_EXCLUSIVE | LK_RETRY);
1068 if (error != 0) {
1069 vrele(foundobj);
1070 goto done;
1071 }
1072 }
1073
1074 /*
1075 * Check to see if the vnode has been mounted on;
1076 * if so find the root of the mounted file system.
1077 */
1078 while (foundobj->v_type == VDIR &&
1079 (mp = foundobj->v_mountedhere) != NULL &&
1080 (cnp->cn_flags & NOCROSSMOUNT) == 0) {
1081 error = vfs_busy(mp, NULL);
1082 if (error != 0) {
1083 if (searchdir != foundobj) {
1084 vput(foundobj);
1085 } else {
1086 vrele(foundobj);
1087 }
1088 goto done;
1089 }
1090 if (searchdir != foundobj) {
1091 VOP_UNLOCK(searchdir);
1092 }
1093 vput(foundobj);
1094 error = VFS_ROOT(mp, &foundobj);
1095 vfs_unbusy(mp, false, NULL);
1096 if (error) {
1097 vn_lock(searchdir, LK_EXCLUSIVE | LK_RETRY);
1098 goto done;
1099 }
1100 /*
1101 * avoid locking vnodes from two filesystems because it's
1102 * prune to deadlock. eg. when using puffs.
1103 * also, it isn't a good idea to propagate slowness of a
1104 * filesystem up to the root directory.
1105 * for now, only handle the common case. (ie. foundobj is VDIR)
1106 */
1107 if (foundobj->v_type == VDIR) {
1108 vrele(searchdir);
1109 *newsearchdir_ret = searchdir = foundobj;
1110 vref(searchdir);
1111 } else {
1112 VOP_UNLOCK(foundobj);
1113 vn_lock(searchdir, LK_EXCLUSIVE | LK_RETRY);
1114 vn_lock(foundobj, LK_EXCLUSIVE | LK_RETRY);
1115 }
1116 }
1117
1118 *foundobj_ret = foundobj;
1119 error = 0;
1120 done:
1121 KASSERT(VOP_ISLOCKED(*newsearchdir_ret) == LK_EXCLUSIVE);
1122 /*
1123 * *foundobj_ret is valid only if error == 0.
1124 */
1125 KASSERT(error != 0 || *foundobj_ret == NULL ||
1126 VOP_ISLOCKED(*foundobj_ret) == LK_EXCLUSIVE);
1127 return error;
1128 }
1129
1130 //////////////////////////////
1131
1132 /*
1133 * Do a complete path search from a single root directory.
1134 * (This is called up to twice if TRYEMULROOT is in effect.)
1135 */
1136 static int
1137 namei_oneroot(struct namei_state *state,
1138 int neverfollow, int inhibitmagic, int isnfsd)
1139 {
1140 struct nameidata *ndp = state->ndp;
1141 struct componentname *cnp = state->cnp;
1142 struct vnode *searchdir, *foundobj;
1143 int error;
1144
1145 error = namei_start(state, isnfsd, &searchdir);
1146 if (error) {
1147 ndp->ni_dvp = NULL;
1148 ndp->ni_vp = NULL;
1149 return error;
1150 }
1151 KASSERT(searchdir->v_type == VDIR);
1152
1153 /*
1154 * Setup: break out flag bits into variables.
1155 */
1156 state->docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE;
1157 if (cnp->cn_nameiop == DELETE)
1158 state->docache = 0;
1159 state->rdonly = cnp->cn_flags & RDONLY;
1160
1161 /*
1162 * Keep going until we run out of path components.
1163 */
1164 cnp->cn_nameptr = ndp->ni_pnbuf;
1165
1166 /* drop leading slashes (already used them to choose startdir) */
1167 while (cnp->cn_nameptr[0] == '/') {
1168 cnp->cn_nameptr++;
1169 ndp->ni_pathlen--;
1170 }
1171 /* was it just "/"? */
1172 if (cnp->cn_nameptr[0] == '\0') {
1173 foundobj = searchdir;
1174 searchdir = NULL;
1175 cnp->cn_flags |= ISLASTCN;
1176
1177 /* bleh */
1178 goto skiploop;
1179 }
1180
1181 for (;;) {
1182
1183 /*
1184 * If the directory we're on is unmounted, bail out.
1185 * XXX: should this also check if it's unlinked?
1186 * XXX: yes it should... but how?
1187 */
1188 if (searchdir->v_mount == NULL) {
1189 vput(searchdir);
1190 ndp->ni_dvp = NULL;
1191 ndp->ni_vp = NULL;
1192 return (ENOENT);
1193 }
1194
1195 /*
1196 * Look up the next path component.
1197 * (currently, this may consume more than one)
1198 */
1199
1200 /* There should be no slashes here. */
1201 KASSERT(cnp->cn_nameptr[0] != '/');
1202
1203 /* and we shouldn't have looped around if we were done */
1204 KASSERT(cnp->cn_nameptr[0] != '\0');
1205
1206 error = lookup_parsepath(state);
1207 if (error) {
1208 vput(searchdir);
1209 ndp->ni_dvp = NULL;
1210 ndp->ni_vp = NULL;
1211 state->attempt_retry = 1;
1212 return (error);
1213 }
1214
1215 error = lookup_once(state, searchdir, &searchdir, &foundobj);
1216 if (error) {
1217 vput(searchdir);
1218 ndp->ni_dvp = NULL;
1219 ndp->ni_vp = NULL;
1220 /*
1221 * Note that if we're doing TRYEMULROOT we can
1222 * retry with the normal root. Where this is
1223 * currently set matches previous practice,
1224 * but the previous practice didn't make much
1225 * sense and somebody should sit down and
1226 * figure out which cases should cause retry
1227 * and which shouldn't. XXX.
1228 */
1229 state->attempt_retry = 1;
1230 return (error);
1231 }
1232
1233 if (foundobj == NULL) {
1234 /*
1235 * Success with no object returned means we're
1236 * creating something and it isn't already
1237 * there. Break out of the main loop now so
1238 * the code below doesn't have to test for
1239 * foundobj == NULL.
1240 */
1241 break;
1242 }
1243
1244 /*
1245 * Check for symbolic link. If we've reached one,
1246 * follow it, unless we aren't supposed to. Back up
1247 * over any slashes that we skipped, as we will need
1248 * them again.
1249 */
1250 if (namei_atsymlink(state, foundobj)) {
1251 ndp->ni_pathlen += state->slashes;
1252 ndp->ni_next -= state->slashes;
1253 if (neverfollow) {
1254 error = EINVAL;
1255 } else {
1256 /*
1257 * dholland 20110410: if we're at a
1258 * union mount it might make sense to
1259 * use the top of the union stack here
1260 * rather than the layer we found the
1261 * symlink in. (FUTURE)
1262 */
1263 error = namei_follow(state, inhibitmagic,
1264 searchdir, foundobj,
1265 &searchdir);
1266 }
1267 if (error) {
1268 KASSERT(searchdir != foundobj);
1269 vput(searchdir);
1270 vput(foundobj);
1271 ndp->ni_dvp = NULL;
1272 ndp->ni_vp = NULL;
1273 return error;
1274 }
1275 /* namei_follow unlocks it (ugh) so rele, not put */
1276 vrele(foundobj);
1277 foundobj = NULL;
1278
1279 /*
1280 * If we followed a symlink to `/' and there
1281 * are no more components after the symlink,
1282 * we're done with the loop and what we found
1283 * is the searchdir.
1284 */
1285 if (cnp->cn_nameptr[0] == '\0') {
1286 foundobj = searchdir;
1287 searchdir = NULL;
1288 cnp->cn_flags |= ISLASTCN;
1289 break;
1290 }
1291
1292 continue;
1293 }
1294
1295 /*
1296 * Not a symbolic link.
1297 *
1298 * Check for directory, if the component was
1299 * followed by a series of slashes.
1300 */
1301 if ((foundobj->v_type != VDIR) &&
1302 (cnp->cn_flags & REQUIREDIR)) {
1303 if (searchdir == foundobj) {
1304 vrele(searchdir);
1305 } else {
1306 vput(searchdir);
1307 }
1308 vput(foundobj);
1309 ndp->ni_dvp = NULL;
1310 ndp->ni_vp = NULL;
1311 state->attempt_retry = 1;
1312 return ENOTDIR;
1313 }
1314
1315 /*
1316 * Stop if we've reached the last component.
1317 */
1318 if (cnp->cn_flags & ISLASTCN) {
1319 break;
1320 }
1321
1322 /*
1323 * Continue with the next component.
1324 */
1325 cnp->cn_nameptr = ndp->ni_next;
1326 if (searchdir == foundobj) {
1327 vrele(searchdir);
1328 } else {
1329 vput(searchdir);
1330 }
1331 searchdir = foundobj;
1332 foundobj = NULL;
1333 }
1334
1335 skiploop:
1336
1337 if (foundobj != NULL) {
1338 if (foundobj == ndp->ni_erootdir) {
1339 /*
1340 * We are about to return the emulation root.
1341 * This isn't a good idea because code might
1342 * repeatedly lookup ".." until the file
1343 * matches that returned for "/" and loop
1344 * forever. So convert it to the real root.
1345 */
1346 if (searchdir != NULL) {
1347 if (searchdir == foundobj)
1348 vrele(searchdir);
1349 else
1350 vput(searchdir);
1351 searchdir = NULL;
1352 }
1353 vput(foundobj);
1354 foundobj = ndp->ni_rootdir;
1355 vref(foundobj);
1356 vn_lock(foundobj, LK_EXCLUSIVE | LK_RETRY);
1357 }
1358
1359 /*
1360 * If the caller requested the parent node (i.e. it's
1361 * a CREATE, DELETE, or RENAME), and we don't have one
1362 * (because this is the root directory, or we crossed
1363 * a mount point), then we must fail.
1364 */
1365 if (cnp->cn_nameiop != LOOKUP &&
1366 (searchdir == NULL ||
1367 searchdir->v_mount != foundobj->v_mount)) {
1368 if (searchdir) {
1369 vput(searchdir);
1370 }
1371 vput(foundobj);
1372 foundobj = NULL;
1373 ndp->ni_dvp = NULL;
1374 ndp->ni_vp = NULL;
1375 state->attempt_retry = 1;
1376
1377 switch (cnp->cn_nameiop) {
1378 case CREATE:
1379 return EEXIST;
1380 case DELETE:
1381 case RENAME:
1382 return EBUSY;
1383 default:
1384 break;
1385 }
1386 panic("Invalid nameiop\n");
1387 }
1388
1389 /*
1390 * Disallow directory write attempts on read-only lookups.
1391 * Prefers EEXIST over EROFS for the CREATE case.
1392 */
1393 if (state->rdonly &&
1394 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
1395 if (searchdir) {
1396 if (foundobj != searchdir) {
1397 vput(searchdir);
1398 } else {
1399 vrele(searchdir);
1400 }
1401 searchdir = NULL;
1402 }
1403 vput(foundobj);
1404 foundobj = NULL;
1405 ndp->ni_dvp = NULL;
1406 ndp->ni_vp = NULL;
1407 state->attempt_retry = 1;
1408 return EROFS;
1409 }
1410 if ((cnp->cn_flags & LOCKLEAF) == 0) {
1411 /*
1412 * Note: if LOCKPARENT but not LOCKLEAF is
1413 * set, and searchdir == foundobj, this code
1414 * necessarily unlocks the parent as well as
1415 * the leaf. That is, just because you specify
1416 * LOCKPARENT doesn't mean you necessarily get
1417 * a locked parent vnode. The code in
1418 * vfs_syscalls.c, and possibly elsewhere,
1419 * that uses this combination "knows" this, so
1420 * it can't be safely changed. Feh. XXX
1421 */
1422 VOP_UNLOCK(foundobj);
1423 }
1424 }
1425
1426 /*
1427 * Done.
1428 */
1429
1430 /*
1431 * If LOCKPARENT is not set, the parent directory isn't returned.
1432 */
1433 if ((cnp->cn_flags & LOCKPARENT) == 0 && searchdir != NULL) {
1434 if (searchdir == foundobj) {
1435 vrele(searchdir);
1436 } else {
1437 vput(searchdir);
1438 }
1439 searchdir = NULL;
1440 }
1441
1442 ndp->ni_dvp = searchdir;
1443 ndp->ni_vp = foundobj;
1444 return 0;
1445 }
1446
1447 /*
1448 * Do namei; wrapper layer that handles TRYEMULROOT.
1449 */
1450 static int
1451 namei_tryemulroot(struct namei_state *state,
1452 int neverfollow, int inhibitmagic, int isnfsd)
1453 {
1454 int error;
1455
1456 struct nameidata *ndp = state->ndp;
1457 struct componentname *cnp = state->cnp;
1458 const char *savepath = NULL;
1459
1460 KASSERT(cnp == &ndp->ni_cnd);
1461
1462 if (cnp->cn_flags & TRYEMULROOT) {
1463 savepath = pathbuf_stringcopy_get(ndp->ni_pathbuf);
1464 }
1465
1466 emul_retry:
1467 state->attempt_retry = 0;
1468
1469 error = namei_oneroot(state, neverfollow, inhibitmagic, isnfsd);
1470 if (error) {
1471 /*
1472 * Once namei has started up, the existence of ni_erootdir
1473 * tells us whether we're working from an emulation root.
1474 * The TRYEMULROOT flag isn't necessarily authoritative.
1475 */
1476 if (ndp->ni_erootdir != NULL && state->attempt_retry) {
1477 /* Retry the whole thing using the normal root */
1478 cnp->cn_flags &= ~TRYEMULROOT;
1479 state->attempt_retry = 0;
1480
1481 /* kinda gross */
1482 strcpy(ndp->ni_pathbuf->pb_path, savepath);
1483 pathbuf_stringcopy_put(ndp->ni_pathbuf, savepath);
1484 savepath = NULL;
1485
1486 goto emul_retry;
1487 }
1488 }
1489 if (savepath != NULL) {
1490 pathbuf_stringcopy_put(ndp->ni_pathbuf, savepath);
1491 }
1492 return error;
1493 }
1494
1495 /*
1496 * External interface.
1497 */
1498 int
1499 namei(struct nameidata *ndp)
1500 {
1501 struct namei_state state;
1502 int error;
1503
1504 namei_init(&state, ndp);
1505 error = namei_tryemulroot(&state,
1506 0/*!neverfollow*/, 0/*!inhibitmagic*/,
1507 0/*isnfsd*/);
1508 namei_cleanup(&state);
1509
1510 if (error) {
1511 /* make sure no stray refs leak out */
1512 KASSERT(ndp->ni_dvp == NULL);
1513 KASSERT(ndp->ni_vp == NULL);
1514 }
1515
1516 return error;
1517 }
1518
1519 ////////////////////////////////////////////////////////////
1520
1521 /*
1522 * External interface used by nfsd. This is basically different from
1523 * namei only in that it has the ability to pass in the "current
1524 * directory", and uses an extra flag "neverfollow" for which there's
1525 * no physical flag defined in namei.h. (There used to be a cut&paste
1526 * copy of about half of namei in nfsd to allow these minor
1527 * adjustments to exist.)
1528 *
1529 * XXX: the namei interface should be adjusted so nfsd can just use
1530 * ordinary namei().
1531 */
1532 int
1533 lookup_for_nfsd(struct nameidata *ndp, struct vnode *forcecwd, int neverfollow)
1534 {
1535 struct namei_state state;
1536 int error;
1537
1538 KASSERT(ndp->ni_atdir == NULL);
1539 ndp->ni_atdir = forcecwd;
1540
1541 namei_init(&state, ndp);
1542 error = namei_tryemulroot(&state,
1543 neverfollow, 1/*inhibitmagic*/, 1/*isnfsd*/);
1544 namei_cleanup(&state);
1545
1546 if (error) {
1547 /* make sure no stray refs leak out */
1548 KASSERT(ndp->ni_dvp == NULL);
1549 KASSERT(ndp->ni_vp == NULL);
1550 }
1551
1552 return error;
1553 }
1554
1555 /*
1556 * A second external interface used by nfsd. This turns out to be a
1557 * single lookup used by the WebNFS code (ha!) to get "index.html" or
1558 * equivalent when asked for a directory. It should eventually evolve
1559 * into some kind of namei_once() call; for the time being it's kind
1560 * of a mess. XXX.
1561 *
1562 * dholland 20110109: I don't think it works, and I don't think it
1563 * worked before I started hacking and slashing either, and I doubt
1564 * anyone will ever notice.
1565 */
1566
1567 /*
1568 * Internals. This calls lookup_once() after setting up the assorted
1569 * pieces of state the way they ought to be.
1570 */
1571 static int
1572 do_lookup_for_nfsd_index(struct namei_state *state)
1573 {
1574 int error = 0;
1575
1576 struct componentname *cnp = state->cnp;
1577 struct nameidata *ndp = state->ndp;
1578 struct vnode *startdir;
1579 struct vnode *foundobj;
1580 const char *cp; /* pointer into pathname argument */
1581
1582 KASSERT(cnp == &ndp->ni_cnd);
1583
1584 startdir = state->ndp->ni_atdir;
1585
1586 cnp->cn_nameptr = ndp->ni_pnbuf;
1587 state->docache = 1;
1588 state->rdonly = cnp->cn_flags & RDONLY;
1589 ndp->ni_dvp = NULL;
1590
1591 cnp->cn_consume = 0;
1592 cnp->cn_namelen = namei_getcomponent(cnp->cn_nameptr);
1593 cp = cnp->cn_nameptr + cnp->cn_namelen;
1594 KASSERT(cnp->cn_namelen <= KERNEL_NAME_MAX);
1595 ndp->ni_pathlen -= cnp->cn_namelen;
1596 ndp->ni_next = cp;
1597 state->slashes = 0;
1598 cnp->cn_flags &= ~REQUIREDIR;
1599 cnp->cn_flags |= MAKEENTRY|ISLASTCN;
1600
1601 if (cnp->cn_namelen == 2 &&
1602 cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.')
1603 cnp->cn_flags |= ISDOTDOT;
1604 else
1605 cnp->cn_flags &= ~ISDOTDOT;
1606
1607 /*
1608 * Because lookup_once can change the startdir, we need our
1609 * own reference to it to avoid consuming the caller's.
1610 */
1611 vref(startdir);
1612 vn_lock(startdir, LK_EXCLUSIVE | LK_RETRY);
1613 error = lookup_once(state, startdir, &startdir, &foundobj);
1614 if (error == 0 && startdir == foundobj) {
1615 vrele(startdir);
1616 } else {
1617 vput(startdir);
1618 }
1619 if (error) {
1620 goto bad;
1621 }
1622 ndp->ni_vp = foundobj;
1623
1624 if (foundobj == NULL) {
1625 return 0;
1626 }
1627
1628 KASSERT((cnp->cn_flags & LOCKPARENT) == 0);
1629 if ((cnp->cn_flags & LOCKLEAF) == 0) {
1630 VOP_UNLOCK(foundobj);
1631 }
1632 return (0);
1633
1634 bad:
1635 ndp->ni_vp = NULL;
1636 return (error);
1637 }
1638
1639 /*
1640 * External interface. The partitioning between this function and the
1641 * above isn't very clear - the above function exists mostly so code
1642 * that uses "state->" can be shuffled around without having to change
1643 * it to "state.".
1644 */
1645 int
1646 lookup_for_nfsd_index(struct nameidata *ndp, struct vnode *startdir)
1647 {
1648 struct namei_state state;
1649 int error;
1650
1651 KASSERT(ndp->ni_atdir == NULL);
1652 ndp->ni_atdir = startdir;
1653
1654 /*
1655 * Note: the name sent in here (is not|should not be) allowed
1656 * to contain a slash.
1657 */
1658 if (strlen(ndp->ni_pathbuf->pb_path) > KERNEL_NAME_MAX) {
1659 return ENAMETOOLONG;
1660 }
1661 if (strchr(ndp->ni_pathbuf->pb_path, '/')) {
1662 return EINVAL;
1663 }
1664
1665 ndp->ni_pathlen = strlen(ndp->ni_pathbuf->pb_path) + 1;
1666 ndp->ni_pnbuf = NULL;
1667 ndp->ni_cnd.cn_nameptr = NULL;
1668
1669 namei_init(&state, ndp);
1670 error = do_lookup_for_nfsd_index(&state);
1671 namei_cleanup(&state);
1672
1673 return error;
1674 }
1675
1676 ////////////////////////////////////////////////////////////
1677
1678 /*
1679 * Reacquire a path name component.
1680 * dvp is locked on entry and exit.
1681 * *vpp is locked on exit unless it's NULL.
1682 */
1683 int
1684 relookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp, int dummy)
1685 {
1686 int rdonly; /* lookup read-only flag bit */
1687 int error = 0;
1688 #ifdef DEBUG
1689 size_t newlen; /* DEBUG: check name len */
1690 const char *cp; /* DEBUG: check name ptr */
1691 #endif /* DEBUG */
1692
1693 (void)dummy;
1694
1695 /*
1696 * Setup: break out flag bits into variables.
1697 */
1698 rdonly = cnp->cn_flags & RDONLY;
1699
1700 /*
1701 * Search a new directory.
1702 *
1703 * The cn_hash value is for use by vfs_cache.
1704 * The last component of the filename is left accessible via
1705 * cnp->cn_nameptr for callers that need the name. Callers needing
1706 * the name set the SAVENAME flag. When done, they assume
1707 * responsibility for freeing the pathname buffer.
1708 */
1709 #ifdef DEBUG
1710 #if 0
1711 cp = NULL;
1712 newhash = namei_hash(cnp->cn_nameptr, &cp);
1713 if ((uint32_t)newhash != (uint32_t)cnp->cn_hash)
1714 panic("relookup: bad hash");
1715 #endif
1716 newlen = namei_getcomponent(cnp->cn_nameptr);
1717 if (cnp->cn_namelen != newlen)
1718 panic("relookup: bad len");
1719 cp = cnp->cn_nameptr + cnp->cn_namelen;
1720 while (*cp == '/')
1721 cp++;
1722 if (*cp != 0)
1723 panic("relookup: not last component");
1724 #endif /* DEBUG */
1725
1726 /*
1727 * Check for degenerate name (e.g. / or "")
1728 * which is a way of talking about a directory,
1729 * e.g. like "/." or ".".
1730 */
1731 if (cnp->cn_nameptr[0] == '\0')
1732 panic("relookup: null name");
1733
1734 if (cnp->cn_flags & ISDOTDOT)
1735 panic("relookup: lookup on dot-dot");
1736
1737 /*
1738 * We now have a segment name to search for, and a directory to search.
1739 */
1740 *vpp = NULL;
1741 cnp->cn_flags |= INRELOOKUP;
1742 error = VOP_LOOKUP(dvp, vpp, cnp);
1743 cnp->cn_flags &= ~INRELOOKUP;
1744 if ((error) != 0) {
1745 #ifdef DIAGNOSTIC
1746 if (*vpp != NULL)
1747 panic("leaf `%s' should be empty", cnp->cn_nameptr);
1748 #endif
1749 if (error != EJUSTRETURN)
1750 goto bad;
1751 }
1752
1753 #ifdef DIAGNOSTIC
1754 /*
1755 * Check for symbolic link
1756 */
1757 if (*vpp && (*vpp)->v_type == VLNK && (cnp->cn_flags & FOLLOW))
1758 panic("relookup: symlink found");
1759 #endif
1760
1761 /*
1762 * Check for read-only lookups.
1763 */
1764 if (rdonly && cnp->cn_nameiop != LOOKUP) {
1765 error = EROFS;
1766 if (*vpp) {
1767 vrele(*vpp);
1768 }
1769 goto bad;
1770 }
1771 /*
1772 * Lock result.
1773 */
1774 if (*vpp && *vpp != dvp) {
1775 error = vn_lock(*vpp, LK_EXCLUSIVE);
1776 if (error != 0) {
1777 vrele(*vpp);
1778 goto bad;
1779 }
1780 }
1781 return (0);
1782
1783 bad:
1784 *vpp = NULL;
1785 return (error);
1786 }
1787
1788 /*
1789 * namei_simple - simple forms of namei.
1790 *
1791 * These are wrappers to allow the simple case callers of namei to be
1792 * left alone while everything else changes under them.
1793 */
1794
1795 /* Flags */
1796 struct namei_simple_flags_type {
1797 int dummy;
1798 };
1799 static const struct namei_simple_flags_type ns_nn, ns_nt, ns_fn, ns_ft;
1800 const namei_simple_flags_t NSM_NOFOLLOW_NOEMULROOT = &ns_nn;
1801 const namei_simple_flags_t NSM_NOFOLLOW_TRYEMULROOT = &ns_nt;
1802 const namei_simple_flags_t NSM_FOLLOW_NOEMULROOT = &ns_fn;
1803 const namei_simple_flags_t NSM_FOLLOW_TRYEMULROOT = &ns_ft;
1804
1805 static
1806 int
1807 namei_simple_convert_flags(namei_simple_flags_t sflags)
1808 {
1809 if (sflags == NSM_NOFOLLOW_NOEMULROOT)
1810 return NOFOLLOW | 0;
1811 if (sflags == NSM_NOFOLLOW_TRYEMULROOT)
1812 return NOFOLLOW | TRYEMULROOT;
1813 if (sflags == NSM_FOLLOW_NOEMULROOT)
1814 return FOLLOW | 0;
1815 if (sflags == NSM_FOLLOW_TRYEMULROOT)
1816 return FOLLOW | TRYEMULROOT;
1817 panic("namei_simple_convert_flags: bogus sflags\n");
1818 return 0;
1819 }
1820
1821 int
1822 namei_simple_kernel(const char *path, namei_simple_flags_t sflags,
1823 struct vnode **vp_ret)
1824 {
1825 return nameiat_simple_kernel(NULL, path, sflags, vp_ret);
1826 }
1827
1828 int
1829 nameiat_simple_kernel(struct vnode *dvp, const char *path,
1830 namei_simple_flags_t sflags, struct vnode **vp_ret)
1831 {
1832 struct nameidata nd;
1833 struct pathbuf *pb;
1834 int err;
1835
1836 pb = pathbuf_create(path);
1837 if (pb == NULL) {
1838 return ENOMEM;
1839 }
1840
1841 NDINIT(&nd,
1842 LOOKUP,
1843 namei_simple_convert_flags(sflags),
1844 pb);
1845
1846 if (dvp != NULL)
1847 NDAT(&nd, dvp);
1848
1849 err = namei(&nd);
1850 if (err != 0) {
1851 pathbuf_destroy(pb);
1852 return err;
1853 }
1854 *vp_ret = nd.ni_vp;
1855 pathbuf_destroy(pb);
1856 return 0;
1857 }
1858
1859 int
1860 namei_simple_user(const char *path, namei_simple_flags_t sflags,
1861 struct vnode **vp_ret)
1862 {
1863 return nameiat_simple_user(NULL, path, sflags, vp_ret);
1864 }
1865
1866 int
1867 nameiat_simple_user(struct vnode *dvp, const char *path,
1868 namei_simple_flags_t sflags, struct vnode **vp_ret)
1869 {
1870 struct pathbuf *pb;
1871 struct nameidata nd;
1872 int err;
1873
1874 err = pathbuf_copyin(path, &pb);
1875 if (err) {
1876 return err;
1877 }
1878
1879 NDINIT(&nd,
1880 LOOKUP,
1881 namei_simple_convert_flags(sflags),
1882 pb);
1883
1884 if (dvp != NULL)
1885 NDAT(&nd, dvp);
1886
1887 err = namei(&nd);
1888 if (err != 0) {
1889 pathbuf_destroy(pb);
1890 return err;
1891 }
1892 *vp_ret = nd.ni_vp;
1893 pathbuf_destroy(pb);
1894 return 0;
1895 }
1896