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