Home | History | Annotate | Line # | Download | only in kern
vfs_lookup.c revision 1.117
      1  1.117  dholland /*	$NetBSD: vfs_lookup.c,v 1.117 2009/08/09 03:28:35 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.117  dholland __KERNEL_RCSID(0, "$NetBSD: vfs_lookup.c,v 1.117 2009/08/09 03:28:35 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.76      elad struct pathname_internal {
     65   1.76      elad 	char *pathbuf;
     66   1.83   thorpej 	bool needfree;
     67   1.76      elad };
     68   1.76      elad 
     69   1.67       chs int vfs_magiclinks = MAGICLINKS;
     70   1.67       chs 
     71   1.99        ad pool_cache_t pnbuf_cache;	/* pathname buffer cache */
     72   1.44   thorpej 
     73   1.10       cgd /*
     74   1.61   thorpej  * Substitute replacement text for 'magic' strings in symlinks.
     75   1.61   thorpej  * Returns 0 if successful, and returns non-zero if an error
     76   1.61   thorpej  * occurs.  (Currently, the only possible error is running out
     77   1.61   thorpej  * of temporary pathname space.)
     78   1.61   thorpej  *
     79   1.61   thorpej  * Looks for "@<string>" and "@<string>/", where <string> is a
     80   1.61   thorpej  * recognized 'magic' string.  Replaces the "@<string>" with the
     81   1.61   thorpej  * appropriate replacement text.  (Note that in some cases the
     82   1.61   thorpej  * replacement text may have zero length.)
     83   1.61   thorpej  *
     84   1.61   thorpej  * This would have been table driven, but the variance in
     85   1.61   thorpej  * replacement strings (and replacement string lengths) made
     86   1.61   thorpej  * that impractical.
     87   1.61   thorpej  */
     88   1.63   thorpej #define	VNL(x)							\
     89   1.63   thorpej 	(sizeof(x) - 1)
     90   1.63   thorpej 
     91   1.63   thorpej #define	VO	'{'
     92   1.63   thorpej #define	VC	'}'
     93   1.63   thorpej 
     94   1.61   thorpej #define	MATCH(str)						\
     95   1.63   thorpej 	((termchar == '/' && i + VNL(str) == *len) ||		\
     96   1.63   thorpej 	 (i + VNL(str) < *len &&				\
     97   1.63   thorpej 	  cp[i + VNL(str)] == termchar)) &&			\
     98   1.63   thorpej 	!strncmp((str), &cp[i], VNL(str))
     99   1.61   thorpej 
    100   1.61   thorpej #define	SUBSTITUTE(m, s, sl)					\
    101  1.115  christos 	if ((newlen + (sl)) >= MAXPATHLEN)			\
    102  1.115  christos 		return 1;					\
    103   1.63   thorpej 	i += VNL(m);						\
    104   1.63   thorpej 	if (termchar != '/')					\
    105   1.63   thorpej 		i++;						\
    106  1.115  christos 	(void)memcpy(&tmp[newlen], (s), (sl));			\
    107   1.63   thorpej 	newlen += (sl);						\
    108   1.63   thorpej 	change = 1;						\
    109   1.63   thorpej 	termchar = '/';
    110   1.61   thorpej 
    111   1.61   thorpej static int
    112  1.115  christos symlink_magic(struct proc *p, char *cp, size_t *len)
    113   1.61   thorpej {
    114   1.66      yamt 	char *tmp;
    115  1.115  christos 	size_t change, i, newlen, slen;
    116  1.115  christos 	char termchar = '/';
    117  1.115  christos 	char idtmp[11]; /* enough for 32 bit *unsigned* integer */
    118  1.101       mjf 
    119   1.61   thorpej 
    120   1.66      yamt 	tmp = PNBUF_GET();
    121   1.61   thorpej 	for (change = i = newlen = 0; i < *len; ) {
    122   1.63   thorpej 		if (cp[i] != '@') {
    123   1.61   thorpej 			tmp[newlen++] = cp[i++];
    124   1.63   thorpej 			continue;
    125   1.63   thorpej 		}
    126   1.63   thorpej 
    127   1.63   thorpej 		i++;
    128   1.63   thorpej 
    129   1.63   thorpej 		/* Check for @{var} syntax. */
    130   1.63   thorpej 		if (cp[i] == VO) {
    131   1.63   thorpej 			termchar = VC;
    132   1.61   thorpej 			i++;
    133   1.63   thorpej 		}
    134   1.63   thorpej 
    135   1.63   thorpej 		/*
    136   1.63   thorpej 		 * The following checks should be ordered according
    137   1.63   thorpej 		 * to frequency of use.
    138   1.63   thorpej 		 */
    139   1.63   thorpej 		if (MATCH("machine_arch")) {
    140  1.115  christos 			slen = VNL(MACHINE_ARCH);
    141  1.115  christos 			SUBSTITUTE("machine_arch", MACHINE_ARCH, slen);
    142   1.63   thorpej 		} else if (MATCH("machine")) {
    143  1.115  christos 			slen = VNL(MACHINE);
    144  1.115  christos 			SUBSTITUTE("machine", MACHINE, slen);
    145   1.63   thorpej 		} else if (MATCH("hostname")) {
    146  1.115  christos 			SUBSTITUTE("hostname", hostname, hostnamelen);
    147   1.63   thorpej 		} else if (MATCH("osrelease")) {
    148  1.115  christos 			slen = strlen(osrelease);
    149  1.115  christos 			SUBSTITUTE("osrelease", osrelease, slen);
    150   1.63   thorpej 		} else if (MATCH("emul")) {
    151  1.115  christos 			slen = strlen(p->p_emul->e_name);
    152  1.115  christos 			SUBSTITUTE("emul", p->p_emul->e_name, slen);
    153   1.63   thorpej 		} else if (MATCH("kernel_ident")) {
    154  1.115  christos 			slen = strlen(kernel_ident);
    155  1.115  christos 			SUBSTITUTE("kernel_ident", kernel_ident, slen);
    156   1.63   thorpej 		} else if (MATCH("domainname")) {
    157  1.115  christos 			SUBSTITUTE("domainname", domainname, domainnamelen);
    158   1.63   thorpej 		} else if (MATCH("ostype")) {
    159  1.115  christos 			slen = strlen(ostype);
    160  1.115  christos 			SUBSTITUTE("ostype", ostype, slen);
    161   1.72      elad 		} else if (MATCH("uid")) {
    162  1.115  christos 			slen = snprintf(idtmp, sizeof(idtmp), "%u",
    163   1.72      elad 			    kauth_cred_geteuid(kauth_cred_get()));
    164  1.115  christos 			SUBSTITUTE("uid", idtmp, slen);
    165  1.101       mjf 		} else if (MATCH("ruid")) {
    166  1.115  christos 			slen = snprintf(idtmp, sizeof(idtmp), "%u",
    167  1.101       mjf 			    kauth_cred_getuid(kauth_cred_get()));
    168  1.115  christos 			SUBSTITUTE("ruid", idtmp, slen);
    169  1.115  christos 		} else if (MATCH("gid")) {
    170  1.115  christos 			slen = snprintf(idtmp, sizeof(idtmp), "%u",
    171  1.115  christos 			    kauth_cred_getegid(kauth_cred_get()));
    172  1.115  christos 			SUBSTITUTE("gid", idtmp, slen);
    173  1.115  christos 		} else if (MATCH("rgid")) {
    174  1.115  christos 			slen = snprintf(idtmp, sizeof(idtmp), "%u",
    175  1.115  christos 			    kauth_cred_getgid(kauth_cred_get()));
    176  1.115  christos 			SUBSTITUTE("rgid", idtmp, slen);
    177   1.63   thorpej 		} else {
    178   1.63   thorpej 			tmp[newlen++] = '@';
    179   1.63   thorpej 			if (termchar == VC)
    180   1.63   thorpej 				tmp[newlen++] = VO;
    181   1.61   thorpej 		}
    182   1.61   thorpej 	}
    183   1.61   thorpej 
    184   1.66      yamt 	if (change) {
    185  1.115  christos 		(void)memcpy(cp, tmp, newlen);
    186   1.66      yamt 		*len = newlen;
    187   1.66      yamt 	}
    188   1.66      yamt 	PNBUF_PUT(tmp);
    189   1.61   thorpej 
    190  1.115  christos 	return 0;
    191   1.61   thorpej }
    192   1.61   thorpej 
    193   1.63   thorpej #undef VNL
    194   1.63   thorpej #undef VO
    195   1.63   thorpej #undef VC
    196   1.63   thorpej #undef MATCH
    197   1.63   thorpej #undef SUBSTITUTE
    198   1.63   thorpej 
    199   1.61   thorpej /*
    200   1.69    rumble  * Convert a pathname into a pointer to a locked vnode.
    201   1.10       cgd  *
    202   1.10       cgd  * The FOLLOW flag is set when symbolic links are to be followed
    203   1.10       cgd  * when they occur at the end of the name translation process.
    204   1.10       cgd  * Symbolic links are always followed for all other pathname
    205   1.10       cgd  * components other than the last.
    206   1.10       cgd  *
    207   1.10       cgd  * The segflg defines whether the name is to be copied from user
    208   1.10       cgd  * space or kernel space.
    209   1.10       cgd  *
    210   1.10       cgd  * Overall outline of namei:
    211   1.10       cgd  *
    212   1.10       cgd  *	copy in name
    213   1.10       cgd  *	get starting directory
    214   1.10       cgd  *	while (!done && !error) {
    215   1.10       cgd  *		call lookup to search path.
    216   1.10       cgd  *		if symbolic link, massage name in buffer and continue
    217   1.10       cgd  *	}
    218   1.10       cgd  */
    219  1.117  dholland 
    220  1.117  dholland /*
    221  1.117  dholland  * Internal state for a namei operation.
    222  1.117  dholland  */
    223  1.117  dholland struct namei_state {
    224  1.117  dholland 	struct nameidata *ndp;
    225  1.117  dholland 	struct componentname *cnp;
    226  1.117  dholland 
    227  1.117  dholland 	struct vnode *dp;		/* the directory we are searching */
    228  1.117  dholland };
    229  1.117  dholland 
    230  1.117  dholland /*
    231  1.117  dholland  * Initialize the namei working state.
    232  1.117  dholland  */
    233  1.117  dholland static void
    234  1.117  dholland namei_init(struct namei_state *state, struct nameidata *ndp)
    235  1.117  dholland {
    236  1.117  dholland 	state->ndp = ndp;
    237  1.117  dholland 	state->cnp = &ndp->ni_cnd;
    238  1.117  dholland 
    239  1.117  dholland 	state->dp = NULL;
    240  1.117  dholland }
    241  1.117  dholland 
    242  1.117  dholland /*
    243  1.117  dholland  * Clean up the working namei state, leaving things ready for return
    244  1.117  dholland  * from namei.
    245  1.117  dholland  */
    246  1.117  dholland static void
    247  1.117  dholland namei_cleanup(struct namei_state *state)
    248  1.117  dholland {
    249  1.117  dholland 	KASSERT(state->cnp == &state->ndp->ni_cnd);
    250  1.117  dholland 
    251  1.117  dholland 	//KASSERT(state->dp == NULL); 	// not yet
    252  1.117  dholland 
    253  1.117  dholland 	/* nothing for now */
    254  1.117  dholland 	(void)state;
    255  1.117  dholland }
    256  1.117  dholland 
    257  1.117  dholland //////////////////////////////
    258  1.117  dholland 
    259  1.117  dholland /*
    260  1.117  dholland  * Start up namei. Early portion.
    261  1.117  dholland  *
    262  1.117  dholland  * This is divided from namei_start2 by the emul_retry: point.
    263  1.117  dholland  */
    264  1.117  dholland static void
    265  1.117  dholland namei_start1(struct namei_state *state)
    266   1.10       cgd {
    267   1.10       cgd 
    268   1.12   mycroft #ifdef DIAGNOSTIC
    269  1.117  dholland 	if (!state->cnp->cn_cred)
    270   1.58  christos 		panic("namei: bad cred/proc");
    271  1.117  dholland 	if (state->cnp->cn_nameiop & (~OPMASK))
    272   1.58  christos 		panic("namei: nameiop contaminated with flags");
    273  1.117  dholland 	if (state->cnp->cn_flags & OPMASK)
    274   1.58  christos 		panic("namei: flags contaminated with nameiops");
    275   1.12   mycroft #endif
    276   1.10       cgd 
    277   1.10       cgd 	/*
    278   1.10       cgd 	 * Get a buffer for the name to be translated, and copy the
    279   1.10       cgd 	 * name into the buffer.
    280   1.10       cgd 	 */
    281  1.117  dholland 	if ((state->cnp->cn_flags & HASBUF) == 0)
    282  1.117  dholland 		state->cnp->cn_pnbuf = PNBUF_GET();
    283  1.117  dholland }
    284  1.117  dholland 
    285  1.117  dholland /*
    286  1.117  dholland  * Start up namei. Copy the path, find the root dir and cwd, establish
    287  1.117  dholland  * the starting directory for lookup, and lock it.
    288  1.117  dholland  */
    289  1.117  dholland static int
    290  1.117  dholland namei_start2(struct namei_state *state)
    291  1.117  dholland {
    292  1.117  dholland 	struct nameidata *ndp = state->ndp;
    293  1.117  dholland 	struct componentname *cnp = state->cnp;
    294  1.117  dholland 
    295  1.117  dholland 	struct cwdinfo *cwdi;		/* pointer to cwd state */
    296  1.117  dholland 	struct lwp *self = curlwp;	/* thread doing namei() */
    297  1.117  dholland 	int error;
    298  1.117  dholland 
    299   1.10       cgd 	if (ndp->ni_segflg == UIO_SYSSPACE)
    300   1.12   mycroft 		error = copystr(ndp->ni_dirp, cnp->cn_pnbuf,
    301   1.10       cgd 			    MAXPATHLEN, &ndp->ni_pathlen);
    302   1.10       cgd 	else
    303   1.12   mycroft 		error = copyinstr(ndp->ni_dirp, cnp->cn_pnbuf,
    304   1.10       cgd 			    MAXPATHLEN, &ndp->ni_pathlen);
    305   1.21    kleink 
    306   1.21    kleink 	/*
    307   1.21    kleink 	 * POSIX.1 requirement: "" is not a valid file name.
    308   1.56     perry 	 */
    309   1.21    kleink 	if (!error && ndp->ni_pathlen == 1)
    310   1.21    kleink 		error = ENOENT;
    311   1.21    kleink 
    312   1.10       cgd 	if (error) {
    313   1.35   thorpej 		PNBUF_PUT(cnp->cn_pnbuf);
    314   1.10       cgd 		ndp->ni_vp = NULL;
    315   1.10       cgd 		return (error);
    316   1.10       cgd 	}
    317   1.10       cgd 	ndp->ni_loopcnt = 0;
    318   1.21    kleink 
    319   1.10       cgd 	/*
    320   1.85       dsl 	 * Get root directory for the translation.
    321   1.10       cgd 	 */
    322  1.117  dholland 	cwdi = self->l_proc->p_cwdi;
    323   1.93        ad 	rw_enter(&cwdi->cwdi_lock, RW_READER);
    324  1.117  dholland 	state->dp = cwdi->cwdi_rdir;
    325  1.117  dholland 	if (state->dp == NULL)
    326  1.117  dholland 		state->dp = rootvnode;
    327  1.117  dholland 	ndp->ni_rootdir = state->dp;
    328   1.85       dsl 
    329   1.23   mycroft 	/*
    330   1.23   mycroft 	 * Check if starting from root directory or current directory.
    331   1.23   mycroft 	 */
    332   1.23   mycroft 	if (cnp->cn_pnbuf[0] == '/') {
    333   1.85       dsl 		if (cnp->cn_flags & TRYEMULROOT) {
    334   1.85       dsl 			if (cnp->cn_flags & EMULROOTSET) {
    335   1.89       dsl 				/* Called from (eg) emul_find_interp() */
    336  1.117  dholland 				state->dp = ndp->ni_erootdir;
    337   1.85       dsl 			} else {
    338   1.90       dsl 				if (cwdi->cwdi_edir == NULL
    339   1.90       dsl 				    || (cnp->cn_pnbuf[1] == '.'
    340   1.90       dsl 					   && cnp->cn_pnbuf[2] == '.'
    341   1.90       dsl 					   && cnp->cn_pnbuf[3] == '/')) {
    342   1.90       dsl 					ndp->ni_erootdir = NULL;
    343   1.90       dsl 				} else {
    344  1.117  dholland 					state->dp = cwdi->cwdi_edir;
    345  1.117  dholland 					ndp->ni_erootdir = state->dp;
    346   1.85       dsl 				}
    347   1.85       dsl 			}
    348  1.111        ad 		} else {
    349   1.85       dsl 			ndp->ni_erootdir = NULL;
    350  1.113     enami 			if (cnp->cn_flags & NOCHROOT)
    351  1.117  dholland 				state->dp = ndp->ni_rootdir = rootvnode;
    352  1.111        ad 		}
    353   1.23   mycroft 	} else {
    354  1.117  dholland 		state->dp = cwdi->cwdi_cdir;
    355   1.85       dsl 		ndp->ni_erootdir = NULL;
    356   1.23   mycroft 	}
    357  1.117  dholland 	VREF(state->dp);
    358   1.93        ad 	rw_exit(&cwdi->cwdi_lock);
    359  1.117  dholland 
    360  1.117  dholland 	/*
    361  1.117  dholland 	 * Ktrace it.
    362  1.117  dholland 	 */
    363   1.97        ad 	if (ktrpoint(KTR_NAMEI)) {
    364   1.90       dsl 		if (ndp->ni_erootdir != NULL) {
    365   1.89       dsl 			/*
    366   1.89       dsl 			 * To make any sense, the trace entry need to have the
    367   1.89       dsl 			 * text of the emulation path prepended.
    368   1.89       dsl 			 * Usually we can get this from the current process,
    369   1.89       dsl 			 * but when called from emul_find_interp() it is only
    370   1.89       dsl 			 * in the exec_package - so we get it passed in ni_next
    371   1.89       dsl 			 * (this is a hack).
    372   1.89       dsl 			 */
    373   1.89       dsl 			const char *emul_path;
    374   1.88       dsl 			if (cnp->cn_flags & EMULROOTSET)
    375   1.89       dsl 				emul_path = ndp->ni_next;
    376   1.88       dsl 			else
    377  1.117  dholland 				emul_path = self->l_proc->p_emul->e_path;
    378   1.97        ad 			ktrnamei2(emul_path, strlen(emul_path),
    379   1.89       dsl 			    cnp->cn_pnbuf, ndp->ni_pathlen);
    380   1.88       dsl 		} else
    381   1.97        ad 			ktrnamei(cnp->cn_pnbuf, ndp->ni_pathlen);
    382   1.88       dsl 	}
    383   1.97        ad 
    384  1.117  dholland 	vn_lock(state->dp, LK_EXCLUSIVE | LK_RETRY);
    385  1.117  dholland 
    386  1.117  dholland 	return 0;
    387  1.117  dholland }
    388  1.117  dholland 
    389  1.117  dholland /*
    390  1.117  dholland  * Undo namei_start: unlock and release the current lookup directory,
    391  1.117  dholland  * and discard the path buffer.
    392  1.117  dholland  */
    393  1.117  dholland static void
    394  1.117  dholland namei_end(struct namei_state *state)
    395  1.117  dholland {
    396  1.117  dholland 	vput(state->dp);
    397  1.117  dholland 	PNBUF_PUT(state->cnp->cn_pnbuf);
    398  1.117  dholland 	//state->cnp->cn_pnbuf = NULL; // not yet (just in case) (XXX)
    399  1.117  dholland }
    400  1.117  dholland 
    401  1.117  dholland /*
    402  1.117  dholland  * Check for being at a symlink.
    403  1.117  dholland  */
    404  1.117  dholland static inline int
    405  1.117  dholland namei_atsymlink(struct namei_state *state)
    406  1.117  dholland {
    407  1.117  dholland 	return (state->cnp->cn_flags & ISSYMLINK) != 0;
    408  1.117  dholland }
    409  1.117  dholland 
    410  1.117  dholland /*
    411  1.117  dholland  * Follow a symlink.
    412  1.117  dholland  */
    413  1.117  dholland static inline int
    414  1.117  dholland namei_follow(struct namei_state *state)
    415  1.117  dholland {
    416  1.117  dholland 	struct nameidata *ndp = state->ndp;
    417  1.117  dholland 	struct componentname *cnp = state->cnp;
    418  1.117  dholland 
    419  1.117  dholland 	struct lwp *self = curlwp;	/* thread doing namei() */
    420  1.117  dholland 	struct iovec aiov;		/* uio for reading symbolic links */
    421  1.117  dholland 	struct uio auio;
    422  1.117  dholland 	char *cp;			/* pointer into pathname argument */
    423  1.117  dholland 	size_t linklen;
    424  1.117  dholland 	int error;
    425  1.117  dholland 
    426  1.117  dholland 	if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
    427  1.117  dholland 		return ELOOP;
    428  1.117  dholland 	}
    429  1.117  dholland 	if (ndp->ni_vp->v_mount->mnt_flag & MNT_SYMPERM) {
    430  1.117  dholland 		error = VOP_ACCESS(ndp->ni_vp, VEXEC, cnp->cn_cred);
    431  1.117  dholland 		if (error != 0)
    432  1.117  dholland 			return error;
    433  1.117  dholland 	}
    434  1.117  dholland 	if (ndp->ni_pathlen > 1)
    435  1.117  dholland 		cp = PNBUF_GET();
    436  1.117  dholland 	else
    437  1.117  dholland 		cp = cnp->cn_pnbuf;
    438  1.117  dholland 	aiov.iov_base = cp;
    439  1.117  dholland 	aiov.iov_len = MAXPATHLEN;
    440  1.117  dholland 	auio.uio_iov = &aiov;
    441  1.117  dholland 	auio.uio_iovcnt = 1;
    442  1.117  dholland 	auio.uio_offset = 0;
    443  1.117  dholland 	auio.uio_rw = UIO_READ;
    444  1.117  dholland 	auio.uio_resid = MAXPATHLEN;
    445  1.117  dholland 	UIO_SETUP_SYSSPACE(&auio);
    446  1.117  dholland 	error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
    447  1.117  dholland 	if (error) {
    448  1.117  dholland badlink:
    449  1.117  dholland 		if (ndp->ni_pathlen > 1)
    450  1.117  dholland 			PNBUF_PUT(cp);
    451  1.117  dholland 		return error;
    452  1.117  dholland 	}
    453  1.117  dholland 	linklen = MAXPATHLEN - auio.uio_resid;
    454  1.117  dholland 	if (linklen == 0) {
    455  1.117  dholland 		error = ENOENT;
    456  1.117  dholland 		goto badlink;
    457  1.117  dholland 	}
    458  1.117  dholland 
    459  1.117  dholland 	/*
    460  1.117  dholland 	 * Do symlink substitution, if appropriate, and
    461  1.117  dholland 	 * check length for potential overflow.
    462  1.117  dholland 	 */
    463  1.117  dholland 	if ((vfs_magiclinks &&
    464  1.117  dholland 	     symlink_magic(self->l_proc, cp, &linklen)) ||
    465  1.117  dholland 	    (linklen + ndp->ni_pathlen >= MAXPATHLEN)) {
    466  1.117  dholland 		error = ENAMETOOLONG;
    467  1.117  dholland 		goto badlink;
    468  1.117  dholland 	}
    469  1.117  dholland 	if (ndp->ni_pathlen > 1) {
    470  1.117  dholland 		memcpy(cp + linklen, ndp->ni_next, ndp->ni_pathlen);
    471  1.117  dholland 		PNBUF_PUT(cnp->cn_pnbuf);
    472  1.117  dholland 		cnp->cn_pnbuf = cp;
    473  1.117  dholland 	} else
    474  1.117  dholland 		cnp->cn_pnbuf[linklen] = '\0';
    475  1.117  dholland 	ndp->ni_pathlen += linklen;
    476  1.117  dholland 	vput(ndp->ni_vp);
    477  1.117  dholland 	state->dp = ndp->ni_dvp;
    478  1.117  dholland 
    479  1.117  dholland 	/*
    480  1.117  dholland 	 * Check if root directory should replace current directory.
    481  1.117  dholland 	 */
    482  1.117  dholland 	if (cnp->cn_pnbuf[0] == '/') {
    483  1.117  dholland 		vput(state->dp);
    484  1.117  dholland 		/* Keep absolute symbolic links inside emulation root */
    485  1.117  dholland 		state->dp = ndp->ni_erootdir;
    486  1.117  dholland 		if (state->dp == NULL || (cnp->cn_pnbuf[1] == '.'
    487  1.117  dholland 					  && cnp->cn_pnbuf[2] == '.'
    488  1.117  dholland 					  && cnp->cn_pnbuf[3] == '/')) {
    489  1.117  dholland 			ndp->ni_erootdir = NULL;
    490  1.117  dholland 			state->dp = ndp->ni_rootdir;
    491  1.117  dholland 		}
    492  1.117  dholland 		VREF(state->dp);
    493  1.117  dholland 		vn_lock(state->dp, LK_EXCLUSIVE | LK_RETRY);
    494  1.117  dholland 	}
    495  1.117  dholland 
    496  1.117  dholland 	return 0;
    497  1.117  dholland }
    498  1.117  dholland 
    499  1.117  dholland //////////////////////////////
    500  1.117  dholland 
    501  1.117  dholland static int
    502  1.117  dholland do_namei(struct namei_state *state)
    503  1.117  dholland {
    504  1.117  dholland 	int error;
    505  1.117  dholland 
    506  1.117  dholland 	struct nameidata *ndp = state->ndp;
    507  1.117  dholland 	struct componentname *cnp = state->cnp;
    508  1.117  dholland 
    509  1.117  dholland 	KASSERT(cnp == &ndp->ni_cnd);
    510  1.117  dholland 
    511  1.117  dholland 	namei_start1(state);
    512  1.117  dholland 
    513  1.117  dholland     emul_retry:
    514  1.117  dholland 
    515  1.117  dholland 	error = namei_start2(state);
    516  1.117  dholland 	if (error) {
    517  1.117  dholland 		return error;
    518  1.117  dholland 	}
    519  1.117  dholland 
    520   1.85       dsl 	/* Loop through symbolic links */
    521   1.10       cgd 	for (;;) {
    522  1.117  dholland 		if (!state->dp->v_mount) {
    523   1.45       erh 			/* Give up if the directory is no longer mounted */
    524  1.117  dholland 			namei_end(state);
    525   1.45       erh 			return (ENOENT);
    526   1.45       erh 		}
    527   1.12   mycroft 		cnp->cn_nameptr = cnp->cn_pnbuf;
    528  1.117  dholland 		ndp->ni_startdir = state->dp;
    529   1.73       chs 		error = lookup(ndp);
    530   1.73       chs 		if (error != 0) {
    531  1.117  dholland 			/* XXX this should use namei_end() */
    532   1.73       chs 			if (ndp->ni_dvp) {
    533   1.73       chs 				vput(ndp->ni_dvp);
    534   1.73       chs 			}
    535   1.90       dsl 			if (ndp->ni_erootdir != NULL) {
    536   1.85       dsl 				/* Retry the whole thing from the normal root */
    537   1.85       dsl 				cnp->cn_flags &= ~TRYEMULROOT;
    538   1.85       dsl 				goto emul_retry;
    539   1.85       dsl 			}
    540   1.35   thorpej 			PNBUF_PUT(cnp->cn_pnbuf);
    541   1.10       cgd 			return (error);
    542   1.10       cgd 		}
    543   1.73       chs 
    544   1.10       cgd 		/*
    545   1.10       cgd 		 * Check for symbolic link
    546   1.10       cgd 		 */
    547  1.117  dholland 		if (namei_atsymlink(state)) {
    548  1.117  dholland 			error = namei_follow(state);
    549  1.117  dholland 			if (error) {
    550  1.117  dholland 				KASSERT(ndp->ni_dvp != ndp->ni_vp);
    551  1.117  dholland 				vput(ndp->ni_dvp);
    552  1.117  dholland 				vput(ndp->ni_vp);
    553  1.117  dholland 				ndp->ni_vp = NULL;
    554   1.35   thorpej 				PNBUF_PUT(cnp->cn_pnbuf);
    555  1.117  dholland 				return error;
    556  1.114      yamt 			}
    557   1.10       cgd 		}
    558  1.117  dholland 		else {
    559   1.10       cgd 			break;
    560   1.10       cgd 		}
    561  1.117  dholland 	}
    562   1.73       chs 
    563  1.117  dholland 	/*
    564  1.117  dholland 	 * Done
    565  1.117  dholland 	 */
    566   1.73       chs 
    567  1.117  dholland 	if ((cnp->cn_flags & LOCKPARENT) == 0 && ndp->ni_dvp) {
    568  1.117  dholland 		if (ndp->ni_dvp == ndp->ni_vp) {
    569  1.117  dholland 			vrele(ndp->ni_dvp);
    570  1.117  dholland 		} else {
    571  1.117  dholland 			vput(ndp->ni_dvp);
    572   1.23   mycroft 		}
    573   1.10       cgd 	}
    574  1.117  dholland 	if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0) {
    575  1.117  dholland 		PNBUF_PUT(cnp->cn_pnbuf);
    576  1.117  dholland #if defined(DIAGNOSTIC)
    577  1.117  dholland 		cnp->cn_pnbuf = NULL;
    578  1.117  dholland #endif /* defined(DIAGNOSTIC) */
    579  1.117  dholland 	} else {
    580  1.117  dholland 		cnp->cn_flags |= HASBUF;
    581  1.117  dholland 	}
    582  1.117  dholland 
    583  1.117  dholland 	return 0;
    584  1.117  dholland }
    585  1.117  dholland 
    586  1.117  dholland int
    587  1.117  dholland namei(struct nameidata *ndp)
    588  1.117  dholland {
    589  1.117  dholland 	struct namei_state state;
    590  1.117  dholland 	int error;
    591  1.117  dholland 
    592  1.117  dholland 	namei_init(&state, ndp);
    593  1.117  dholland 	error = do_namei(&state);
    594  1.117  dholland 	namei_cleanup(&state);
    595  1.117  dholland 
    596  1.117  dholland 	return error;
    597   1.10       cgd }
    598   1.10       cgd 
    599   1.10       cgd /*
    600   1.39     lukem  * Determine the namei hash (for cn_hash) for name.
    601   1.39     lukem  * If *ep != NULL, hash from name to ep-1.
    602   1.39     lukem  * If *ep == NULL, hash from name until the first NUL or '/', and
    603   1.39     lukem  * return the location of this termination character in *ep.
    604   1.39     lukem  *
    605   1.39     lukem  * This function returns an equivalent hash to the MI hash32_strn().
    606   1.39     lukem  * The latter isn't used because in the *ep == NULL case, determining
    607   1.39     lukem  * the length of the string to the first NUL or `/' and then calling
    608   1.39     lukem  * hash32_strn() involves unnecessary double-handling of the data.
    609   1.39     lukem  */
    610   1.39     lukem uint32_t
    611   1.39     lukem namei_hash(const char *name, const char **ep)
    612   1.39     lukem {
    613   1.39     lukem 	uint32_t	hash;
    614   1.39     lukem 
    615   1.39     lukem 	hash = HASH32_STR_INIT;
    616   1.39     lukem 	if (*ep != NULL) {
    617   1.39     lukem 		for (; name < *ep; name++)
    618   1.59  christos 			hash = hash * 33 + *(const uint8_t *)name;
    619   1.39     lukem 	} else {
    620   1.39     lukem 		for (; *name != '\0' && *name != '/'; name++)
    621   1.59  christos 			hash = hash * 33 + *(const uint8_t *)name;
    622   1.39     lukem 		*ep = name;
    623   1.39     lukem 	}
    624   1.39     lukem 	return (hash + (hash >> 5));
    625   1.39     lukem }
    626   1.39     lukem 
    627   1.39     lukem /*
    628   1.10       cgd  * Search a pathname.
    629   1.10       cgd  * This is a very central and rather complicated routine.
    630   1.10       cgd  *
    631   1.10       cgd  * The pathname is pointed to by ni_ptr and is of length ni_pathlen.
    632   1.10       cgd  * The starting directory is taken from ni_startdir. The pathname is
    633   1.10       cgd  * descended until done, or a symbolic link is encountered. The variable
    634   1.10       cgd  * ni_more is clear if the path is completed; it is set to one if a
    635   1.10       cgd  * symbolic link needing interpretation is encountered.
    636   1.10       cgd  *
    637   1.10       cgd  * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
    638   1.10       cgd  * whether the name is to be looked up, created, renamed, or deleted.
    639   1.10       cgd  * When CREATE, RENAME, or DELETE is specified, information usable in
    640   1.10       cgd  * creating, renaming, or deleting a directory entry may be calculated.
    641   1.10       cgd  * If flag has LOCKPARENT or'ed into it, the parent directory is returned
    642   1.79     pooka  * locked.  Otherwise the parent directory is not returned. If the target
    643   1.79     pooka  * of the pathname exists and LOCKLEAF is or'ed into the flag the target
    644   1.79     pooka  * is returned locked, otherwise it is returned unlocked.  When creating
    645   1.79     pooka  * or renaming and LOCKPARENT is specified, the target may not be ".".
    646   1.79     pooka  * When deleting and LOCKPARENT is specified, the target may be ".".
    647   1.56     perry  *
    648   1.10       cgd  * Overall outline of lookup:
    649   1.10       cgd  *
    650   1.10       cgd  * dirloop:
    651   1.10       cgd  *	identify next component of name at ndp->ni_ptr
    652   1.10       cgd  *	handle degenerate case where name is null string
    653   1.10       cgd  *	if .. and crossing mount points and on mounted filesys, find parent
    654   1.10       cgd  *	call VOP_LOOKUP routine for next component name
    655   1.79     pooka  *	    directory vnode returned in ni_dvp, locked.
    656   1.10       cgd  *	    component vnode returned in ni_vp (if it exists), locked.
    657   1.10       cgd  *	if result vnode is mounted on and crossing mount points,
    658   1.10       cgd  *	    find mounted on vnode
    659   1.10       cgd  *	if more components of name, do next level at dirloop
    660   1.10       cgd  *	return the answer in ni_vp, locked if LOCKLEAF set
    661   1.10       cgd  *	    if LOCKPARENT set, return locked parent in ni_dvp
    662   1.10       cgd  */
    663   1.12   mycroft int
    664   1.60   thorpej lookup(struct nameidata *ndp)
    665   1.10       cgd {
    666   1.33  augustss 	const char *cp;			/* pointer into pathname argument */
    667   1.33  augustss 	struct vnode *dp = 0;		/* the directory we are searching */
    668   1.10       cgd 	struct vnode *tdp;		/* saved dp */
    669   1.10       cgd 	struct mount *mp;		/* mount table entry */
    670   1.10       cgd 	int docache;			/* == 0 do not cache last component */
    671   1.12   mycroft 	int rdonly;			/* lookup read-only flag bit */
    672   1.10       cgd 	int error = 0;
    673   1.23   mycroft 	int slashes;
    674   1.12   mycroft 	struct componentname *cnp = &ndp->ni_cnd;
    675  1.102     pooka 	struct lwp *l = curlwp;
    676   1.10       cgd 
    677   1.10       cgd 	/*
    678   1.10       cgd 	 * Setup: break out flag bits into variables.
    679   1.10       cgd 	 */
    680   1.12   mycroft 	docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE;
    681   1.78     pooka 	if (cnp->cn_nameiop == DELETE)
    682   1.10       cgd 		docache = 0;
    683   1.12   mycroft 	rdonly = cnp->cn_flags & RDONLY;
    684   1.10       cgd 	ndp->ni_dvp = NULL;
    685   1.12   mycroft 	cnp->cn_flags &= ~ISSYMLINK;
    686   1.10       cgd 	dp = ndp->ni_startdir;
    687   1.10       cgd 	ndp->ni_startdir = NULLVP;
    688   1.10       cgd 
    689   1.23   mycroft 	/*
    690   1.23   mycroft 	 * If we have a leading string of slashes, remove them, and just make
    691   1.23   mycroft 	 * sure the current node is a directory.
    692   1.23   mycroft 	 */
    693   1.23   mycroft 	cp = cnp->cn_nameptr;
    694   1.23   mycroft 	if (*cp == '/') {
    695   1.23   mycroft 		do {
    696   1.23   mycroft 			cp++;
    697   1.23   mycroft 		} while (*cp == '/');
    698   1.23   mycroft 		ndp->ni_pathlen -= cp - cnp->cn_nameptr;
    699   1.23   mycroft 		cnp->cn_nameptr = cp;
    700   1.23   mycroft 
    701   1.23   mycroft 		if (dp->v_type != VDIR) {
    702   1.23   mycroft 			error = ENOTDIR;
    703   1.75      yamt 			vput(dp);
    704   1.23   mycroft 			goto bad;
    705   1.23   mycroft 		}
    706   1.23   mycroft 
    707   1.23   mycroft 		/*
    708   1.23   mycroft 		 * If we've exhausted the path name, then just return the
    709   1.87       dsl 		 * current node.
    710   1.23   mycroft 		 */
    711   1.23   mycroft 		if (cnp->cn_nameptr[0] == '\0') {
    712   1.23   mycroft 			ndp->ni_vp = dp;
    713   1.23   mycroft 			cnp->cn_flags |= ISLASTCN;
    714   1.23   mycroft 			goto terminal;
    715   1.23   mycroft 		}
    716   1.23   mycroft 	}
    717   1.23   mycroft 
    718   1.10       cgd dirloop:
    719   1.10       cgd 	/*
    720   1.10       cgd 	 * Search a new directory.
    721   1.10       cgd 	 *
    722   1.12   mycroft 	 * The cn_hash value is for use by vfs_cache.
    723   1.10       cgd 	 * The last component of the filename is left accessible via
    724   1.12   mycroft 	 * cnp->cn_nameptr for callers that need the name. Callers needing
    725   1.10       cgd 	 * the name set the SAVENAME flag. When done, they assume
    726   1.10       cgd 	 * responsibility for freeing the pathname buffer.
    727   1.73       chs 	 *
    728   1.73       chs 	 * At this point, our only vnode state is that "dp" is held and locked.
    729   1.10       cgd 	 */
    730   1.12   mycroft 	cnp->cn_consume = 0;
    731   1.39     lukem 	cp = NULL;
    732   1.39     lukem 	cnp->cn_hash = namei_hash(cnp->cn_nameptr, &cp);
    733   1.12   mycroft 	cnp->cn_namelen = cp - cnp->cn_nameptr;
    734   1.12   mycroft 	if (cnp->cn_namelen > NAME_MAX) {
    735   1.75      yamt 		vput(dp);
    736   1.10       cgd 		error = ENAMETOOLONG;
    737   1.77       chs 		ndp->ni_dvp = NULL;
    738   1.10       cgd 		goto bad;
    739   1.10       cgd 	}
    740   1.10       cgd #ifdef NAMEI_DIAGNOSTIC
    741   1.10       cgd 	{ char c = *cp;
    742   1.41     soren 	*(char *)cp = '\0';
    743   1.19  christos 	printf("{%s}: ", cnp->cn_nameptr);
    744   1.41     soren 	*(char *)cp = c; }
    745   1.52      yamt #endif /* NAMEI_DIAGNOSTIC */
    746   1.12   mycroft 	ndp->ni_pathlen -= cnp->cn_namelen;
    747   1.10       cgd 	ndp->ni_next = cp;
    748   1.23   mycroft 	/*
    749   1.23   mycroft 	 * If this component is followed by a slash, then move the pointer to
    750   1.23   mycroft 	 * the next component forward, and remember that this component must be
    751   1.23   mycroft 	 * a directory.
    752   1.23   mycroft 	 */
    753   1.23   mycroft 	if (*cp == '/') {
    754   1.23   mycroft 		do {
    755   1.23   mycroft 			cp++;
    756   1.23   mycroft 		} while (*cp == '/');
    757   1.23   mycroft 		slashes = cp - ndp->ni_next;
    758   1.23   mycroft 		ndp->ni_pathlen -= slashes;
    759   1.23   mycroft 		ndp->ni_next = cp;
    760   1.23   mycroft 		cnp->cn_flags |= REQUIREDIR;
    761   1.23   mycroft 	} else {
    762   1.23   mycroft 		slashes = 0;
    763   1.23   mycroft 		cnp->cn_flags &= ~REQUIREDIR;
    764   1.23   mycroft 	}
    765   1.23   mycroft 	/*
    766   1.23   mycroft 	 * We do special processing on the last component, whether or not it's
    767   1.23   mycroft 	 * a directory.  Cache all intervening lookups, but not the final one.
    768   1.23   mycroft 	 */
    769   1.23   mycroft 	if (*cp == '\0') {
    770   1.23   mycroft 		if (docache)
    771   1.23   mycroft 			cnp->cn_flags |= MAKEENTRY;
    772   1.23   mycroft 		else
    773   1.23   mycroft 			cnp->cn_flags &= ~MAKEENTRY;
    774   1.23   mycroft 		cnp->cn_flags |= ISLASTCN;
    775   1.23   mycroft 	} else {
    776   1.23   mycroft 		cnp->cn_flags |= MAKEENTRY;
    777   1.23   mycroft 		cnp->cn_flags &= ~ISLASTCN;
    778   1.23   mycroft 	}
    779   1.12   mycroft 	if (cnp->cn_namelen == 2 &&
    780   1.12   mycroft 	    cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.')
    781   1.12   mycroft 		cnp->cn_flags |= ISDOTDOT;
    782   1.12   mycroft 	else
    783   1.12   mycroft 		cnp->cn_flags &= ~ISDOTDOT;
    784   1.10       cgd 
    785   1.10       cgd 	/*
    786   1.10       cgd 	 * Handle "..": two special cases.
    787   1.10       cgd 	 * 1. If at root directory (e.g. after chroot)
    788   1.12   mycroft 	 *    or at absolute root directory
    789   1.10       cgd 	 *    then ignore it so can't get out.
    790   1.85       dsl 	 * 1a. If at the root of the emulation filesystem go to the real
    791   1.85       dsl 	 *    root. So "/../<path>" is always absolute.
    792   1.85       dsl 	 * 1b. If we have somehow gotten out of a jail, warn
    793   1.40  wrstuden 	 *    and also ignore it so we can't get farther out.
    794   1.10       cgd 	 * 2. If this vnode is the root of a mounted
    795   1.10       cgd 	 *    filesystem, then replace it with the
    796   1.10       cgd 	 *    vnode which was mounted on so we take the
    797   1.10       cgd 	 *    .. in the other file system.
    798   1.10       cgd 	 */
    799   1.12   mycroft 	if (cnp->cn_flags & ISDOTDOT) {
    800   1.64  christos 		struct proc *p = l->l_proc;
    801   1.64  christos 
    802   1.10       cgd 		for (;;) {
    803   1.90       dsl 			if (dp == ndp->ni_rootdir || dp == rootvnode) {
    804   1.10       cgd 				ndp->ni_dvp = dp;
    805   1.10       cgd 				ndp->ni_vp = dp;
    806   1.10       cgd 				VREF(dp);
    807   1.10       cgd 				goto nextname;
    808   1.40  wrstuden 			}
    809   1.40  wrstuden 			if (ndp->ni_rootdir != rootvnode) {
    810   1.40  wrstuden 				int retval;
    811   1.73       chs 
    812   1.40  wrstuden 				VOP_UNLOCK(dp, 0);
    813   1.64  christos 				retval = vn_isunder(dp, ndp->ni_rootdir, l);
    814   1.40  wrstuden 				vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
    815   1.40  wrstuden 				if (!retval) {
    816   1.40  wrstuden 				    /* Oops! We got out of jail! */
    817   1.40  wrstuden 				    log(LOG_WARNING,
    818   1.40  wrstuden 					"chrooted pid %d uid %d (%s) "
    819   1.40  wrstuden 					"detected outside of its chroot\n",
    820   1.71        ad 					p->p_pid, kauth_cred_geteuid(l->l_cred),
    821   1.64  christos 					p->p_comm);
    822   1.40  wrstuden 				    /* Put us at the jail root. */
    823   1.40  wrstuden 				    vput(dp);
    824   1.40  wrstuden 				    dp = ndp->ni_rootdir;
    825   1.40  wrstuden 				    ndp->ni_dvp = dp;
    826   1.40  wrstuden 				    ndp->ni_vp = dp;
    827   1.40  wrstuden 				    VREF(dp);
    828   1.40  wrstuden 				    VREF(dp);
    829   1.40  wrstuden 				    vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
    830   1.40  wrstuden 				    goto nextname;
    831   1.40  wrstuden 				}
    832   1.10       cgd 			}
    833   1.98        ad 			if ((dp->v_vflag & VV_ROOT) == 0 ||
    834   1.12   mycroft 			    (cnp->cn_flags & NOCROSSMOUNT))
    835   1.10       cgd 				break;
    836   1.10       cgd 			tdp = dp;
    837   1.10       cgd 			dp = dp->v_mount->mnt_vnodecovered;
    838   1.10       cgd 			vput(tdp);
    839   1.10       cgd 			VREF(dp);
    840   1.26      fvdl 			vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
    841   1.10       cgd 		}
    842   1.10       cgd 	}
    843   1.10       cgd 
    844   1.10       cgd 	/*
    845   1.10       cgd 	 * We now have a segment name to search for, and a directory to search.
    846   1.73       chs 	 * Again, our only vnode state is that "dp" is held and locked.
    847   1.10       cgd 	 */
    848   1.12   mycroft unionlookup:
    849   1.12   mycroft 	ndp->ni_dvp = dp;
    850   1.26      fvdl 	ndp->ni_vp = NULL;
    851   1.73       chs 	error = VOP_LOOKUP(dp, &ndp->ni_vp, cnp);
    852   1.73       chs 	if (error != 0) {
    853   1.10       cgd #ifdef DIAGNOSTIC
    854   1.10       cgd 		if (ndp->ni_vp != NULL)
    855   1.43  christos 			panic("leaf `%s' should be empty", cnp->cn_nameptr);
    856   1.52      yamt #endif /* DIAGNOSTIC */
    857   1.10       cgd #ifdef NAMEI_DIAGNOSTIC
    858   1.19  christos 		printf("not found\n");
    859   1.52      yamt #endif /* NAMEI_DIAGNOSTIC */
    860   1.12   mycroft 		if ((error == ENOENT) &&
    861   1.98        ad 		    (dp->v_vflag & VV_ROOT) &&
    862   1.10       cgd 		    (dp->v_mount->mnt_flag & MNT_UNION)) {
    863   1.10       cgd 			tdp = dp;
    864   1.10       cgd 			dp = dp->v_mount->mnt_vnodecovered;
    865   1.73       chs 			vput(tdp);
    866   1.10       cgd 			VREF(dp);
    867   1.26      fvdl 			vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
    868   1.12   mycroft 			goto unionlookup;
    869   1.10       cgd 		}
    870   1.12   mycroft 
    871   1.10       cgd 		if (error != EJUSTRETURN)
    872   1.10       cgd 			goto bad;
    873   1.73       chs 
    874   1.10       cgd 		/*
    875   1.23   mycroft 		 * If this was not the last component, or there were trailing
    876   1.51  christos 		 * slashes, and we are not going to create a directory,
    877   1.51  christos 		 * then the name must exist.
    878   1.23   mycroft 		 */
    879   1.51  christos 		if ((cnp->cn_flags & (REQUIREDIR | CREATEDIR)) == REQUIREDIR) {
    880   1.23   mycroft 			error = ENOENT;
    881   1.23   mycroft 			goto bad;
    882   1.23   mycroft 		}
    883   1.73       chs 
    884   1.23   mycroft 		/*
    885   1.10       cgd 		 * If creating and at end of pathname, then can consider
    886   1.10       cgd 		 * allowing file to be created.
    887   1.10       cgd 		 */
    888   1.26      fvdl 		if (rdonly) {
    889   1.10       cgd 			error = EROFS;
    890   1.10       cgd 			goto bad;
    891   1.10       cgd 		}
    892   1.73       chs 
    893   1.10       cgd 		/*
    894   1.10       cgd 		 * We return with ni_vp NULL to indicate that the entry
    895   1.10       cgd 		 * doesn't currently exist, leaving a pointer to the
    896   1.69    rumble 		 * (possibly locked) directory vnode in ndp->ni_dvp.
    897   1.10       cgd 		 */
    898   1.12   mycroft 		if (cnp->cn_flags & SAVESTART) {
    899   1.10       cgd 			ndp->ni_startdir = ndp->ni_dvp;
    900   1.10       cgd 			VREF(ndp->ni_startdir);
    901   1.10       cgd 		}
    902   1.10       cgd 		return (0);
    903   1.10       cgd 	}
    904   1.10       cgd #ifdef NAMEI_DIAGNOSTIC
    905   1.19  christos 	printf("found\n");
    906   1.52      yamt #endif /* NAMEI_DIAGNOSTIC */
    907   1.10       cgd 
    908   1.12   mycroft 	/*
    909   1.23   mycroft 	 * Take into account any additional components consumed by the
    910   1.23   mycroft 	 * underlying filesystem.  This will include any trailing slashes after
    911   1.23   mycroft 	 * the last component consumed.
    912   1.12   mycroft 	 */
    913   1.12   mycroft 	if (cnp->cn_consume > 0) {
    914   1.23   mycroft 		ndp->ni_pathlen -= cnp->cn_consume - slashes;
    915   1.23   mycroft 		ndp->ni_next += cnp->cn_consume - slashes;
    916   1.12   mycroft 		cnp->cn_consume = 0;
    917   1.23   mycroft 		if (ndp->ni_next[0] == '\0')
    918   1.23   mycroft 			cnp->cn_flags |= ISLASTCN;
    919   1.12   mycroft 	}
    920   1.12   mycroft 
    921   1.10       cgd 	dp = ndp->ni_vp;
    922   1.73       chs 
    923   1.73       chs 	/*
    924   1.73       chs 	 * "dp" and "ndp->ni_dvp" are both locked and held,
    925   1.73       chs 	 * and may be the same vnode.
    926   1.73       chs 	 */
    927   1.73       chs 
    928   1.10       cgd 	/*
    929   1.10       cgd 	 * Check to see if the vnode has been mounted on;
    930   1.10       cgd 	 * if so find the root of the mounted file system.
    931   1.10       cgd 	 */
    932   1.10       cgd 	while (dp->v_type == VDIR && (mp = dp->v_mountedhere) &&
    933   1.12   mycroft 	       (cnp->cn_flags & NOCROSSMOUNT) == 0) {
    934  1.108        ad 		error = vfs_busy(mp, NULL);
    935  1.107        ad 		if (error != 0) {
    936  1.107        ad 			vput(dp);
    937  1.107        ad 			goto bad;
    938  1.107        ad 		}
    939   1.73       chs 		KASSERT(ndp->ni_dvp != dp);
    940   1.77       chs 		VOP_UNLOCK(ndp->ni_dvp, 0);
    941   1.73       chs 		vput(dp);
    942   1.47   thorpej 		error = VFS_ROOT(mp, &tdp);
    943  1.106        ad 		vfs_unbusy(mp, false, NULL);
    944   1.32  wrstuden 		if (error) {
    945   1.77       chs 			vn_lock(ndp->ni_dvp, LK_EXCLUSIVE | LK_RETRY);
    946   1.73       chs 			goto bad;
    947   1.32  wrstuden 		}
    948   1.77       chs 		VOP_UNLOCK(tdp, 0);
    949   1.10       cgd 		ndp->ni_vp = dp = tdp;
    950   1.77       chs 		vn_lock(ndp->ni_dvp, LK_EXCLUSIVE | LK_RETRY);
    951   1.77       chs 		vn_lock(ndp->ni_vp, LK_EXCLUSIVE | LK_RETRY);
    952   1.14   mycroft 	}
    953   1.14   mycroft 
    954   1.14   mycroft 	/*
    955   1.23   mycroft 	 * Check for symbolic link.  Back up over any slashes that we skipped,
    956   1.23   mycroft 	 * as we will need them again.
    957   1.14   mycroft 	 */
    958   1.23   mycroft 	if ((dp->v_type == VLNK) && (cnp->cn_flags & (FOLLOW|REQUIREDIR))) {
    959   1.23   mycroft 		ndp->ni_pathlen += slashes;
    960   1.23   mycroft 		ndp->ni_next -= slashes;
    961   1.14   mycroft 		cnp->cn_flags |= ISSYMLINK;
    962   1.14   mycroft 		return (0);
    963   1.10       cgd 	}
    964   1.10       cgd 
    965   1.23   mycroft 	/*
    966   1.23   mycroft 	 * Check for directory, if the component was followed by a series of
    967   1.23   mycroft 	 * slashes.
    968   1.23   mycroft 	 */
    969   1.23   mycroft 	if ((dp->v_type != VDIR) && (cnp->cn_flags & REQUIREDIR)) {
    970   1.23   mycroft 		error = ENOTDIR;
    971   1.73       chs 		KASSERT(dp != ndp->ni_dvp);
    972   1.73       chs 		vput(dp);
    973   1.73       chs 		goto bad;
    974   1.23   mycroft 	}
    975   1.23   mycroft 
    976   1.10       cgd nextname:
    977   1.73       chs 
    978   1.10       cgd 	/*
    979   1.23   mycroft 	 * Not a symbolic link.  If this was not the last component, then
    980   1.23   mycroft 	 * continue at the next component, else return.
    981   1.10       cgd 	 */
    982   1.23   mycroft 	if (!(cnp->cn_flags & ISLASTCN)) {
    983   1.12   mycroft 		cnp->cn_nameptr = ndp->ni_next;
    984   1.73       chs 		if (ndp->ni_dvp == dp) {
    985   1.73       chs 			vrele(ndp->ni_dvp);
    986   1.73       chs 		} else {
    987   1.73       chs 			vput(ndp->ni_dvp);
    988   1.73       chs 		}
    989   1.10       cgd 		goto dirloop;
    990   1.10       cgd 	}
    991   1.23   mycroft 
    992   1.23   mycroft terminal:
    993   1.87       dsl 	if (dp == ndp->ni_erootdir) {
    994   1.87       dsl 		/*
    995   1.87       dsl 		 * We are about to return the emulation root.
    996   1.87       dsl 		 * This isn't a good idea because code might repeatedly
    997   1.87       dsl 		 * lookup ".." until the file matches that returned
    998   1.87       dsl 		 * for "/" and loop forever.
    999   1.87       dsl 		 * So convert it to the real root.
   1000   1.87       dsl 		 */
   1001   1.87       dsl 		if (ndp->ni_dvp == dp)
   1002   1.87       dsl 			vrele(dp);
   1003   1.87       dsl 		else
   1004   1.87       dsl 			if (ndp->ni_dvp != NULL)
   1005   1.87       dsl 				vput(ndp->ni_dvp);
   1006   1.87       dsl 		ndp->ni_dvp = NULL;
   1007   1.87       dsl 		vput(dp);
   1008   1.87       dsl 		dp = ndp->ni_rootdir;
   1009   1.87       dsl 		VREF(dp);
   1010   1.87       dsl 		vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
   1011   1.87       dsl 		ndp->ni_vp = dp;
   1012   1.87       dsl 	}
   1013   1.87       dsl 
   1014   1.87       dsl 	/*
   1015   1.87       dsl 	 * If the caller requested the parent node (i.e.
   1016   1.87       dsl 	 * it's a CREATE, DELETE, or RENAME), and we don't have one
   1017   1.87       dsl 	 * (because this is the root directory), then we must fail.
   1018   1.87       dsl 	 */
   1019   1.87       dsl 	if (ndp->ni_dvp == NULL && cnp->cn_nameiop != LOOKUP) {
   1020   1.87       dsl 		switch (cnp->cn_nameiop) {
   1021   1.87       dsl 		case CREATE:
   1022   1.87       dsl 			error = EEXIST;
   1023   1.87       dsl 			break;
   1024   1.87       dsl 		case DELETE:
   1025   1.87       dsl 		case RENAME:
   1026   1.87       dsl 			error = EBUSY;
   1027   1.87       dsl 			break;
   1028   1.87       dsl 		default:
   1029   1.87       dsl 			KASSERT(0);
   1030   1.87       dsl 		}
   1031   1.87       dsl 		vput(dp);
   1032   1.87       dsl 		goto bad;
   1033   1.87       dsl 	}
   1034   1.73       chs 
   1035   1.10       cgd 	/*
   1036   1.94     pooka 	 * Disallow directory write attempts on read-only lookups.
   1037   1.96     pooka 	 * Prefers EEXIST over EROFS for the CREATE case.
   1038   1.10       cgd 	 */
   1039   1.26      fvdl 	if (rdonly &&
   1040   1.96     pooka 	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
   1041   1.26      fvdl 		error = EROFS;
   1042   1.73       chs 		if (dp != ndp->ni_dvp) {
   1043   1.73       chs 			vput(dp);
   1044   1.73       chs 		}
   1045   1.73       chs 		goto bad;
   1046   1.10       cgd 	}
   1047   1.23   mycroft 	if (ndp->ni_dvp != NULL) {
   1048   1.23   mycroft 		if (cnp->cn_flags & SAVESTART) {
   1049   1.23   mycroft 			ndp->ni_startdir = ndp->ni_dvp;
   1050   1.23   mycroft 			VREF(ndp->ni_startdir);
   1051   1.23   mycroft 		}
   1052   1.10       cgd 	}
   1053   1.73       chs 	if ((cnp->cn_flags & LOCKLEAF) == 0) {
   1054   1.26      fvdl 		VOP_UNLOCK(dp, 0);
   1055   1.73       chs 	}
   1056   1.10       cgd 	return (0);
   1057   1.10       cgd 
   1058   1.10       cgd bad:
   1059   1.10       cgd 	ndp->ni_vp = NULL;
   1060   1.12   mycroft 	return (error);
   1061   1.12   mycroft }
   1062   1.12   mycroft 
   1063   1.12   mycroft /*
   1064   1.12   mycroft  * Reacquire a path name component.
   1065   1.73       chs  * dvp is locked on entry and exit.
   1066   1.73       chs  * *vpp is locked on exit unless it's NULL.
   1067   1.12   mycroft  */
   1068   1.12   mycroft int
   1069   1.60   thorpej relookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp)
   1070   1.12   mycroft {
   1071   1.12   mycroft 	int rdonly;			/* lookup read-only flag bit */
   1072   1.12   mycroft 	int error = 0;
   1073   1.52      yamt #ifdef DEBUG
   1074   1.81       chs 	uint32_t newhash;		/* DEBUG: check name hash */
   1075   1.41     soren 	const char *cp;			/* DEBUG: check name ptr/len */
   1076   1.52      yamt #endif /* DEBUG */
   1077   1.12   mycroft 
   1078   1.12   mycroft 	/*
   1079   1.12   mycroft 	 * Setup: break out flag bits into variables.
   1080   1.12   mycroft 	 */
   1081   1.12   mycroft 	rdonly = cnp->cn_flags & RDONLY;
   1082   1.12   mycroft 	cnp->cn_flags &= ~ISSYMLINK;
   1083   1.12   mycroft 
   1084   1.12   mycroft 	/*
   1085   1.12   mycroft 	 * Search a new directory.
   1086   1.12   mycroft 	 *
   1087   1.12   mycroft 	 * The cn_hash value is for use by vfs_cache.
   1088   1.12   mycroft 	 * The last component of the filename is left accessible via
   1089   1.12   mycroft 	 * cnp->cn_nameptr for callers that need the name. Callers needing
   1090   1.12   mycroft 	 * the name set the SAVENAME flag. When done, they assume
   1091   1.12   mycroft 	 * responsibility for freeing the pathname buffer.
   1092   1.12   mycroft 	 */
   1093   1.52      yamt #ifdef DEBUG
   1094   1.39     lukem 	cp = NULL;
   1095   1.39     lukem 	newhash = namei_hash(cnp->cn_nameptr, &cp);
   1096   1.81       chs 	if ((uint32_t)newhash != (uint32_t)cnp->cn_hash)
   1097   1.12   mycroft 		panic("relookup: bad hash");
   1098   1.12   mycroft 	if (cnp->cn_namelen != cp - cnp->cn_nameptr)
   1099   1.58  christos 		panic("relookup: bad len");
   1100   1.53      yamt 	while (*cp == '/')
   1101   1.53      yamt 		cp++;
   1102   1.12   mycroft 	if (*cp != 0)
   1103   1.12   mycroft 		panic("relookup: not last component");
   1104   1.52      yamt #endif /* DEBUG */
   1105   1.12   mycroft 
   1106   1.12   mycroft 	/*
   1107   1.12   mycroft 	 * Check for degenerate name (e.g. / or "")
   1108   1.12   mycroft 	 * which is a way of talking about a directory,
   1109   1.12   mycroft 	 * e.g. like "/." or ".".
   1110   1.12   mycroft 	 */
   1111   1.23   mycroft 	if (cnp->cn_nameptr[0] == '\0')
   1112   1.23   mycroft 		panic("relookup: null name");
   1113   1.12   mycroft 
   1114   1.12   mycroft 	if (cnp->cn_flags & ISDOTDOT)
   1115   1.58  christos 		panic("relookup: lookup on dot-dot");
   1116   1.12   mycroft 
   1117   1.12   mycroft 	/*
   1118   1.12   mycroft 	 * We now have a segment name to search for, and a directory to search.
   1119   1.12   mycroft 	 */
   1120   1.81       chs 	if ((error = VOP_LOOKUP(dvp, vpp, cnp)) != 0) {
   1121   1.12   mycroft #ifdef DIAGNOSTIC
   1122   1.12   mycroft 		if (*vpp != NULL)
   1123   1.43  christos 			panic("leaf `%s' should be empty", cnp->cn_nameptr);
   1124   1.12   mycroft #endif
   1125   1.12   mycroft 		if (error != EJUSTRETURN)
   1126   1.12   mycroft 			goto bad;
   1127   1.12   mycroft 	}
   1128   1.12   mycroft 
   1129   1.12   mycroft #ifdef DIAGNOSTIC
   1130   1.12   mycroft 	/*
   1131   1.12   mycroft 	 * Check for symbolic link
   1132   1.12   mycroft 	 */
   1133   1.81       chs 	if (*vpp && (*vpp)->v_type == VLNK && (cnp->cn_flags & FOLLOW))
   1134   1.58  christos 		panic("relookup: symlink found");
   1135   1.12   mycroft #endif
   1136   1.12   mycroft 
   1137   1.12   mycroft 	/*
   1138   1.94     pooka 	 * Check for read-only lookups.
   1139   1.12   mycroft 	 */
   1140   1.81       chs 	if (rdonly && cnp->cn_nameiop != LOOKUP) {
   1141   1.26      fvdl 		error = EROFS;
   1142   1.81       chs 		if (*vpp) {
   1143   1.81       chs 			vput(*vpp);
   1144   1.81       chs 		}
   1145   1.73       chs 		goto bad;
   1146   1.12   mycroft 	}
   1147   1.12   mycroft 	if (cnp->cn_flags & SAVESTART)
   1148   1.12   mycroft 		VREF(dvp);
   1149   1.12   mycroft 	return (0);
   1150   1.12   mycroft 
   1151   1.12   mycroft bad:
   1152   1.12   mycroft 	*vpp = NULL;
   1153   1.10       cgd 	return (error);
   1154   1.10       cgd }
   1155  1.116  dholland 
   1156  1.116  dholland /*
   1157  1.116  dholland  * namei_simple - simple forms of namei.
   1158  1.116  dholland  *
   1159  1.116  dholland  * These are wrappers to allow the simple case callers of namei to be
   1160  1.116  dholland  * left alone while everything else changes under them.
   1161  1.116  dholland  */
   1162  1.116  dholland 
   1163  1.116  dholland /* Flags */
   1164  1.116  dholland struct namei_simple_flags_type {
   1165  1.116  dholland 	int dummy;
   1166  1.116  dholland };
   1167  1.116  dholland static const struct namei_simple_flags_type ns_nn, ns_nt, ns_fn, ns_ft;
   1168  1.116  dholland const namei_simple_flags_t NSM_NOFOLLOW_NOEMULROOT = &ns_nn;
   1169  1.116  dholland const namei_simple_flags_t NSM_NOFOLLOW_TRYEMULROOT = &ns_nt;
   1170  1.116  dholland const namei_simple_flags_t NSM_FOLLOW_NOEMULROOT = &ns_fn;
   1171  1.116  dholland const namei_simple_flags_t NSM_FOLLOW_TRYEMULROOT = &ns_ft;
   1172  1.116  dholland 
   1173  1.116  dholland static
   1174  1.116  dholland int
   1175  1.116  dholland namei_simple_convert_flags(namei_simple_flags_t sflags)
   1176  1.116  dholland {
   1177  1.116  dholland 	if (sflags == NSM_NOFOLLOW_NOEMULROOT)
   1178  1.116  dholland 		return NOFOLLOW | 0;
   1179  1.116  dholland 	if (sflags == NSM_NOFOLLOW_TRYEMULROOT)
   1180  1.116  dholland 		return NOFOLLOW | TRYEMULROOT;
   1181  1.116  dholland 	if (sflags == NSM_FOLLOW_NOEMULROOT)
   1182  1.116  dholland 		return FOLLOW | 0;
   1183  1.116  dholland 	if (sflags == NSM_FOLLOW_TRYEMULROOT)
   1184  1.116  dholland 		return FOLLOW | TRYEMULROOT;
   1185  1.116  dholland 	panic("namei_simple_convert_flags: bogus sflags\n");
   1186  1.116  dholland 	return 0;
   1187  1.116  dholland }
   1188  1.116  dholland 
   1189  1.116  dholland int
   1190  1.116  dholland namei_simple_kernel(const char *path, namei_simple_flags_t sflags,
   1191  1.116  dholland 			struct vnode **vp_ret)
   1192  1.116  dholland {
   1193  1.116  dholland 	struct nameidata nd;
   1194  1.116  dholland 	int err;
   1195  1.116  dholland 
   1196  1.116  dholland 	NDINIT(&nd,
   1197  1.116  dholland 		LOOKUP,
   1198  1.116  dholland 		namei_simple_convert_flags(sflags),
   1199  1.116  dholland 		UIO_SYSSPACE,
   1200  1.116  dholland 		path);
   1201  1.116  dholland 	err = namei(&nd);
   1202  1.116  dholland 	if (err != 0) {
   1203  1.116  dholland 		return err;
   1204  1.116  dholland 	}
   1205  1.116  dholland 	*vp_ret = nd.ni_vp;
   1206  1.116  dholland 	return 0;
   1207  1.116  dholland }
   1208  1.116  dholland 
   1209  1.116  dholland int
   1210  1.116  dholland namei_simple_user(const char *path, namei_simple_flags_t sflags,
   1211  1.116  dholland 			struct vnode **vp_ret)
   1212  1.116  dholland {
   1213  1.116  dholland 	struct nameidata nd;
   1214  1.116  dholland 	int err;
   1215  1.116  dholland 
   1216  1.116  dholland 	NDINIT(&nd,
   1217  1.116  dholland 		LOOKUP,
   1218  1.116  dholland 		namei_simple_convert_flags(sflags),
   1219  1.116  dholland 		UIO_USERSPACE,
   1220  1.116  dholland 		path);
   1221  1.116  dholland 	err = namei(&nd);
   1222  1.116  dholland 	if (err != 0) {
   1223  1.116  dholland 		return err;
   1224  1.116  dholland 	}
   1225  1.116  dholland 	*vp_ret = nd.ni_vp;
   1226  1.116  dholland 	return 0;
   1227  1.116  dholland }
   1228