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