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