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