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