Home | History | Annotate | Line # | Download | only in kern
vfs_lookup.c revision 1.92
      1 /*	$NetBSD: vfs_lookup.c,v 1.92 2007/05/19 22:11:22 christos 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.92 2007/05/19 22:11:22 christos Exp $");
     41 
     42 #include "opt_ktrace.h"
     43 #include "opt_systrace.h"
     44 #include "opt_magiclinks.h"
     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/malloc.h>
     58 #include <sys/proc.h>
     59 #include <sys/syslog.h>
     60 #include <sys/kauth.h>
     61 
     62 #ifdef KTRACE
     63 #include <sys/ktrace.h>
     64 #endif
     65 #ifdef SYSTRACE
     66 #include <sys/systrace.h>
     67 #endif
     68 
     69 #ifndef MAGICLINKS
     70 #define MAGICLINKS 0
     71 #endif
     72 
     73 struct pathname_internal {
     74 	char *pathbuf;
     75 	bool needfree;
     76 };
     77 
     78 int vfs_magiclinks = MAGICLINKS;
     79 
     80 struct pool pnbuf_pool;		/* pathname buffer pool */
     81 struct pool_cache pnbuf_cache;	/* pathname buffer cache */
     82 
     83 /*
     84  * Substitute replacement text for 'magic' strings in symlinks.
     85  * Returns 0 if successful, and returns non-zero if an error
     86  * occurs.  (Currently, the only possible error is running out
     87  * of temporary pathname space.)
     88  *
     89  * Looks for "@<string>" and "@<string>/", where <string> is a
     90  * recognized 'magic' string.  Replaces the "@<string>" with the
     91  * appropriate replacement text.  (Note that in some cases the
     92  * replacement text may have zero length.)
     93  *
     94  * This would have been table driven, but the variance in
     95  * replacement strings (and replacement string lengths) made
     96  * that impractical.
     97  */
     98 #define	VNL(x)							\
     99 	(sizeof(x) - 1)
    100 
    101 #define	VO	'{'
    102 #define	VC	'}'
    103 
    104 #define	MATCH(str)						\
    105 	((termchar == '/' && i + VNL(str) == *len) ||		\
    106 	 (i + VNL(str) < *len &&				\
    107 	  cp[i + VNL(str)] == termchar)) &&			\
    108 	!strncmp((str), &cp[i], VNL(str))
    109 
    110 #define	SUBSTITUTE(m, s, sl)					\
    111 	if ((newlen + (sl)) > MAXPATHLEN)			\
    112 		return (1);					\
    113 	i += VNL(m);						\
    114 	if (termchar != '/')					\
    115 		i++;						\
    116 	memcpy(&tmp[newlen], (s), (sl));			\
    117 	newlen += (sl);						\
    118 	change = 1;						\
    119 	termchar = '/';
    120 
    121 static int
    122 symlink_magic(struct proc *p, char *cp, int *len)
    123 {
    124 	char *tmp;
    125 	int change, i, newlen;
    126 	int termchar = '/';
    127 
    128 	tmp = PNBUF_GET();
    129 	for (change = i = newlen = 0; i < *len; ) {
    130 		if (cp[i] != '@') {
    131 			tmp[newlen++] = cp[i++];
    132 			continue;
    133 		}
    134 
    135 		i++;
    136 
    137 		/* Check for @{var} syntax. */
    138 		if (cp[i] == VO) {
    139 			termchar = VC;
    140 			i++;
    141 		}
    142 
    143 		/*
    144 		 * The following checks should be ordered according
    145 		 * to frequency of use.
    146 		 */
    147 		if (MATCH("machine_arch")) {
    148 			SUBSTITUTE("machine_arch", MACHINE_ARCH,
    149 			    sizeof(MACHINE_ARCH) - 1);
    150 		} else if (MATCH("machine")) {
    151 			SUBSTITUTE("machine", MACHINE,
    152 			    sizeof(MACHINE) - 1);
    153 		} else if (MATCH("hostname")) {
    154 			SUBSTITUTE("hostname", hostname,
    155 			    hostnamelen);
    156 		} else if (MATCH("osrelease")) {
    157 			SUBSTITUTE("osrelease", osrelease,
    158 			    strlen(osrelease));
    159 		} else if (MATCH("emul")) {
    160 			SUBSTITUTE("emul", p->p_emul->e_name,
    161 			    strlen(p->p_emul->e_name));
    162 		} else if (MATCH("kernel_ident")) {
    163 			SUBSTITUTE("kernel_ident", kernel_ident,
    164 			    strlen(kernel_ident));
    165 		} else if (MATCH("domainname")) {
    166 			SUBSTITUTE("domainname", domainname,
    167 			    domainnamelen);
    168 		} else if (MATCH("ostype")) {
    169 			SUBSTITUTE("ostype", ostype,
    170 			    strlen(ostype));
    171 		} else if (MATCH("uid")) {
    172 			char uidtmp[11]; /* XXX elad */
    173 
    174 			(void)snprintf(uidtmp, sizeof(uidtmp), "%u",
    175 			    kauth_cred_geteuid(kauth_cred_get()));
    176 			SUBSTITUTE("uid", uidtmp, strlen(uidtmp));
    177 		} else {
    178 			tmp[newlen++] = '@';
    179 			if (termchar == VC)
    180 				tmp[newlen++] = VO;
    181 		}
    182 	}
    183 
    184 	if (change) {
    185 		memcpy(cp, tmp, newlen);
    186 		*len = newlen;
    187 	}
    188 	PNBUF_PUT(tmp);
    189 
    190 	return (0);
    191 }
    192 
    193 #undef VNL
    194 #undef VO
    195 #undef VC
    196 #undef MATCH
    197 #undef SUBSTITUTE
    198 
    199 /*
    200  * Convert a pathname into a pointer to a locked vnode.
    201  *
    202  * The FOLLOW flag is set when symbolic links are to be followed
    203  * when they occur at the end of the name translation process.
    204  * Symbolic links are always followed for all other pathname
    205  * components other than the last.
    206  *
    207  * The segflg defines whether the name is to be copied from user
    208  * space or kernel space.
    209  *
    210  * Overall outline of namei:
    211  *
    212  *	copy in name
    213  *	get starting directory
    214  *	while (!done && !error) {
    215  *		call lookup to search path.
    216  *		if symbolic link, massage name in buffer and continue
    217  *	}
    218  */
    219 int
    220 namei(struct nameidata *ndp)
    221 {
    222 	struct cwdinfo *cwdi;		/* pointer to cwd state */
    223 	char *cp;			/* pointer into pathname argument */
    224 	struct vnode *dp;		/* the directory we are searching */
    225 	struct iovec aiov;		/* uio for reading symbolic links */
    226 	struct uio auio;
    227 	int error, linklen;
    228 	struct componentname *cnp = &ndp->ni_cnd;
    229 
    230 #ifdef DIAGNOSTIC
    231 	if (!cnp->cn_cred || !cnp->cn_lwp)
    232 		panic("namei: bad cred/proc");
    233 	if (cnp->cn_nameiop & (~OPMASK))
    234 		panic("namei: nameiop contaminated with flags");
    235 	if (cnp->cn_flags & OPMASK)
    236 		panic("namei: flags contaminated with nameiops");
    237 #endif
    238 
    239 	/*
    240 	 * Get a buffer for the name to be translated, and copy the
    241 	 * name into the buffer.
    242 	 */
    243 	if ((cnp->cn_flags & HASBUF) == 0)
    244 		cnp->cn_pnbuf = PNBUF_GET();
    245     emul_retry:
    246 	if (ndp->ni_segflg == UIO_SYSSPACE)
    247 		error = copystr(ndp->ni_dirp, cnp->cn_pnbuf,
    248 			    MAXPATHLEN, &ndp->ni_pathlen);
    249 	else
    250 		error = copyinstr(ndp->ni_dirp, cnp->cn_pnbuf,
    251 			    MAXPATHLEN, &ndp->ni_pathlen);
    252 
    253 	/*
    254 	 * POSIX.1 requirement: "" is not a valid file name.
    255 	 */
    256 	if (!error && ndp->ni_pathlen == 1)
    257 		error = ENOENT;
    258 
    259 	if (error) {
    260 		PNBUF_PUT(cnp->cn_pnbuf);
    261 		ndp->ni_vp = NULL;
    262 		return (error);
    263 	}
    264 	ndp->ni_loopcnt = 0;
    265 
    266 	/*
    267 	 * Get root directory for the translation.
    268 	 */
    269 	/* XXX: SMP access to p_cwdi needs locking and vnodes held */
    270 	cwdi = cnp->cn_lwp->l_proc->p_cwdi;
    271 	dp = cwdi->cwdi_rdir;
    272 	if (dp == NULL)
    273 		dp = rootvnode;
    274 	ndp->ni_rootdir = dp;
    275 
    276 	/*
    277 	 * Check if starting from root directory or current directory.
    278 	 */
    279 	if (cnp->cn_pnbuf[0] == '/') {
    280 		if (cnp->cn_flags & TRYEMULROOT) {
    281 			if (cnp->cn_flags & EMULROOTSET) {
    282 				/* Called from (eg) emul_find_interp() */
    283 				dp = ndp->ni_erootdir;
    284 			} else {
    285 				if (cwdi->cwdi_edir == NULL
    286 				    || (cnp->cn_pnbuf[1] == '.'
    287 					   && cnp->cn_pnbuf[2] == '.'
    288 					   && cnp->cn_pnbuf[3] == '/')) {
    289 					ndp->ni_erootdir = NULL;
    290 				} else {
    291 					dp = cwdi->cwdi_edir;
    292 					ndp->ni_erootdir = dp;
    293 				}
    294 			}
    295 		} else
    296 			ndp->ni_erootdir = NULL;
    297 	} else {
    298 		dp = cwdi->cwdi_cdir;
    299 		ndp->ni_erootdir = NULL;
    300 	}
    301 	VREF(dp);
    302 
    303 #ifdef KTRACE
    304 	if (KTRPOINT(cnp->cn_lwp->l_proc, KTR_NAMEI)) {
    305 		if (ndp->ni_erootdir != NULL) {
    306 			/*
    307 			 * To make any sense, the trace entry need to have the
    308 			 * text of the emulation path prepended.
    309 			 * Usually we can get this from the current process,
    310 			 * but when called from emul_find_interp() it is only
    311 			 * in the exec_package - so we get it passed in ni_next
    312 			 * (this is a hack).
    313 			 */
    314 			const char *emul_path;
    315 			if (cnp->cn_flags & EMULROOTSET)
    316 				emul_path = ndp->ni_next;
    317 			else
    318 				emul_path = cnp->cn_lwp->l_proc->p_emul->e_path;
    319 			ktrnamei2(cnp->cn_lwp, emul_path, strlen(emul_path),
    320 			    cnp->cn_pnbuf, ndp->ni_pathlen);
    321 		} else
    322 			ktrnamei(cnp->cn_lwp, cnp->cn_pnbuf, ndp->ni_pathlen);
    323 	}
    324 #endif
    325 #ifdef SYSTRACE
    326 	if (ISSET(cnp->cn_lwp->l_proc->p_flag, PK_SYSTRACE))
    327 		systrace_namei(ndp);
    328 #endif
    329 
    330 	vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
    331 	/* Loop through symbolic links */
    332 	for (;;) {
    333 		if (!dp->v_mount) {
    334 			/* Give up if the directory is no longer mounted */
    335 			vput(dp);
    336 			PNBUF_PUT(cnp->cn_pnbuf);
    337 			return (ENOENT);
    338 		}
    339 		cnp->cn_nameptr = cnp->cn_pnbuf;
    340 		ndp->ni_startdir = dp;
    341 		error = lookup(ndp);
    342 		if (error != 0) {
    343 			if (ndp->ni_dvp) {
    344 				vput(ndp->ni_dvp);
    345 			}
    346 			if (ndp->ni_erootdir != NULL) {
    347 				/* Retry the whole thing from the normal root */
    348 				cnp->cn_flags &= ~TRYEMULROOT;
    349 				goto emul_retry;
    350 			}
    351 			PNBUF_PUT(cnp->cn_pnbuf);
    352 			return (error);
    353 		}
    354 
    355 		/*
    356 		 * Check for symbolic link
    357 		 */
    358 		if ((cnp->cn_flags & ISSYMLINK) == 0) {
    359 			if ((cnp->cn_flags & LOCKPARENT) == 0 && ndp->ni_dvp) {
    360 				if (ndp->ni_dvp == ndp->ni_vp) {
    361 					vrele(ndp->ni_dvp);
    362 				} else {
    363 					vput(ndp->ni_dvp);
    364 				}
    365 			}
    366 			if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0)
    367 				PNBUF_PUT(cnp->cn_pnbuf);
    368 			else
    369 				cnp->cn_flags |= HASBUF;
    370 			return (0);
    371 		}
    372 
    373 		if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
    374 			error = ELOOP;
    375 			break;
    376 		}
    377 		if (ndp->ni_vp->v_mount->mnt_flag & MNT_SYMPERM) {
    378 			error = VOP_ACCESS(ndp->ni_vp, VEXEC, cnp->cn_cred,
    379 			    cnp->cn_lwp);
    380 			if (error != 0)
    381 				break;
    382 		}
    383 		if (ndp->ni_pathlen > 1)
    384 			cp = PNBUF_GET();
    385 		else
    386 			cp = cnp->cn_pnbuf;
    387 		aiov.iov_base = cp;
    388 		aiov.iov_len = MAXPATHLEN;
    389 		auio.uio_iov = &aiov;
    390 		auio.uio_iovcnt = 1;
    391 		auio.uio_offset = 0;
    392 		auio.uio_rw = UIO_READ;
    393 		auio.uio_resid = MAXPATHLEN;
    394 		UIO_SETUP_SYSSPACE(&auio);
    395 		error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
    396 		if (error) {
    397 badlink:
    398 			if (ndp->ni_pathlen > 1)
    399 				PNBUF_PUT(cp);
    400 			break;
    401 		}
    402 		linklen = MAXPATHLEN - auio.uio_resid;
    403 		if (linklen == 0) {
    404 			error = ENOENT;
    405 			goto badlink;
    406 		}
    407 
    408 		/*
    409 		 * Do symlink substitution, if appropriate, and
    410 		 * check length for potential overflow.
    411 		 */
    412 		if ((vfs_magiclinks &&
    413 		     symlink_magic(cnp->cn_lwp->l_proc, cp, &linklen)) ||
    414 		    (linklen + ndp->ni_pathlen >= MAXPATHLEN)) {
    415 			error = ENAMETOOLONG;
    416 			goto badlink;
    417 		}
    418 		if (ndp->ni_pathlen > 1) {
    419 			memcpy(cp + linklen, ndp->ni_next, ndp->ni_pathlen);
    420 			PNBUF_PUT(cnp->cn_pnbuf);
    421 			cnp->cn_pnbuf = cp;
    422 		} else
    423 			cnp->cn_pnbuf[linklen] = '\0';
    424 		ndp->ni_pathlen += linklen;
    425 		vput(ndp->ni_vp);
    426 		dp = ndp->ni_dvp;
    427 
    428 		/*
    429 		 * Check if root directory should replace current directory.
    430 		 */
    431 		if (cnp->cn_pnbuf[0] == '/') {
    432 			vput(dp);
    433 			/* Keep absolute symbolic links inside emulation root */
    434 			dp = ndp->ni_erootdir;
    435 			if (dp == NULL || (cnp->cn_pnbuf[1] == '.'
    436 			    && cnp->cn_pnbuf[2] == '.'
    437 			    && cnp->cn_pnbuf[3] == '/')) {
    438 				ndp->ni_erootdir = NULL;
    439 				dp = ndp->ni_rootdir;
    440 			}
    441 			VREF(dp);
    442 			vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
    443 		}
    444 	}
    445 	/* Failed to process a symbolic link */
    446 	KASSERT(ndp->ni_dvp != ndp->ni_vp);
    447 	vput(ndp->ni_dvp);
    448 	vput(ndp->ni_vp);
    449 	ndp->ni_vp = NULL;
    450 	PNBUF_PUT(cnp->cn_pnbuf);
    451 	return (error);
    452 }
    453 
    454 /*
    455  * Determine the namei hash (for cn_hash) for name.
    456  * If *ep != NULL, hash from name to ep-1.
    457  * If *ep == NULL, hash from name until the first NUL or '/', and
    458  * return the location of this termination character in *ep.
    459  *
    460  * This function returns an equivalent hash to the MI hash32_strn().
    461  * The latter isn't used because in the *ep == NULL case, determining
    462  * the length of the string to the first NUL or `/' and then calling
    463  * hash32_strn() involves unnecessary double-handling of the data.
    464  */
    465 uint32_t
    466 namei_hash(const char *name, const char **ep)
    467 {
    468 	uint32_t	hash;
    469 
    470 	hash = HASH32_STR_INIT;
    471 	if (*ep != NULL) {
    472 		for (; name < *ep; name++)
    473 			hash = hash * 33 + *(const uint8_t *)name;
    474 	} else {
    475 		for (; *name != '\0' && *name != '/'; name++)
    476 			hash = hash * 33 + *(const uint8_t *)name;
    477 		*ep = name;
    478 	}
    479 	return (hash + (hash >> 5));
    480 }
    481 
    482 /*
    483  * Search a pathname.
    484  * This is a very central and rather complicated routine.
    485  *
    486  * The pathname is pointed to by ni_ptr and is of length ni_pathlen.
    487  * The starting directory is taken from ni_startdir. The pathname is
    488  * descended until done, or a symbolic link is encountered. The variable
    489  * ni_more is clear if the path is completed; it is set to one if a
    490  * symbolic link needing interpretation is encountered.
    491  *
    492  * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
    493  * whether the name is to be looked up, created, renamed, or deleted.
    494  * When CREATE, RENAME, or DELETE is specified, information usable in
    495  * creating, renaming, or deleting a directory entry may be calculated.
    496  * If flag has LOCKPARENT or'ed into it, the parent directory is returned
    497  * locked.  Otherwise the parent directory is not returned. If the target
    498  * of the pathname exists and LOCKLEAF is or'ed into the flag the target
    499  * is returned locked, otherwise it is returned unlocked.  When creating
    500  * or renaming and LOCKPARENT is specified, the target may not be ".".
    501  * When deleting and LOCKPARENT is specified, the target may be ".".
    502  *
    503  * Overall outline of lookup:
    504  *
    505  * dirloop:
    506  *	identify next component of name at ndp->ni_ptr
    507  *	handle degenerate case where name is null string
    508  *	if .. and crossing mount points and on mounted filesys, find parent
    509  *	call VOP_LOOKUP routine for next component name
    510  *	    directory vnode returned in ni_dvp, locked.
    511  *	    component vnode returned in ni_vp (if it exists), locked.
    512  *	if result vnode is mounted on and crossing mount points,
    513  *	    find mounted on vnode
    514  *	if more components of name, do next level at dirloop
    515  *	return the answer in ni_vp, locked if LOCKLEAF set
    516  *	    if LOCKPARENT set, return locked parent in ni_dvp
    517  */
    518 int
    519 lookup(struct nameidata *ndp)
    520 {
    521 	const char *cp;			/* pointer into pathname argument */
    522 	struct vnode *dp = 0;		/* the directory we are searching */
    523 	struct vnode *tdp;		/* saved dp */
    524 	struct mount *mp;		/* mount table entry */
    525 	int docache;			/* == 0 do not cache last component */
    526 	int rdonly;			/* lookup read-only flag bit */
    527 	int error = 0;
    528 	int slashes;
    529 	struct componentname *cnp = &ndp->ni_cnd;
    530 	struct lwp *l = cnp->cn_lwp;
    531 
    532 	/*
    533 	 * Setup: break out flag bits into variables.
    534 	 */
    535 	docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE;
    536 	if (cnp->cn_nameiop == DELETE)
    537 		docache = 0;
    538 	rdonly = cnp->cn_flags & RDONLY;
    539 	ndp->ni_dvp = NULL;
    540 	cnp->cn_flags &= ~ISSYMLINK;
    541 	dp = ndp->ni_startdir;
    542 	ndp->ni_startdir = NULLVP;
    543 
    544 	/*
    545 	 * If we have a leading string of slashes, remove them, and just make
    546 	 * sure the current node is a directory.
    547 	 */
    548 	cp = cnp->cn_nameptr;
    549 	if (*cp == '/') {
    550 		do {
    551 			cp++;
    552 		} while (*cp == '/');
    553 		ndp->ni_pathlen -= cp - cnp->cn_nameptr;
    554 		cnp->cn_nameptr = cp;
    555 
    556 		if (dp->v_type != VDIR) {
    557 			error = ENOTDIR;
    558 			vput(dp);
    559 			goto bad;
    560 		}
    561 
    562 		/*
    563 		 * If we've exhausted the path name, then just return the
    564 		 * current node.
    565 		 */
    566 		if (cnp->cn_nameptr[0] == '\0') {
    567 			ndp->ni_vp = dp;
    568 			cnp->cn_flags |= ISLASTCN;
    569 			goto terminal;
    570 		}
    571 	}
    572 
    573 dirloop:
    574 	/*
    575 	 * Search a new directory.
    576 	 *
    577 	 * The cn_hash value is for use by vfs_cache.
    578 	 * The last component of the filename is left accessible via
    579 	 * cnp->cn_nameptr for callers that need the name. Callers needing
    580 	 * the name set the SAVENAME flag. When done, they assume
    581 	 * responsibility for freeing the pathname buffer.
    582 	 *
    583 	 * At this point, our only vnode state is that "dp" is held and locked.
    584 	 */
    585 	cnp->cn_consume = 0;
    586 	cp = NULL;
    587 	cnp->cn_hash = namei_hash(cnp->cn_nameptr, &cp);
    588 	cnp->cn_namelen = cp - cnp->cn_nameptr;
    589 	if (cnp->cn_namelen > NAME_MAX) {
    590 		vput(dp);
    591 		error = ENAMETOOLONG;
    592 		ndp->ni_dvp = NULL;
    593 		goto bad;
    594 	}
    595 #ifdef NAMEI_DIAGNOSTIC
    596 	{ char c = *cp;
    597 	*(char *)cp = '\0';
    598 	printf("{%s}: ", cnp->cn_nameptr);
    599 	*(char *)cp = c; }
    600 #endif /* NAMEI_DIAGNOSTIC */
    601 	ndp->ni_pathlen -= cnp->cn_namelen;
    602 	ndp->ni_next = cp;
    603 	/*
    604 	 * If this component is followed by a slash, then move the pointer to
    605 	 * the next component forward, and remember that this component must be
    606 	 * a directory.
    607 	 */
    608 	if (*cp == '/') {
    609 		do {
    610 			cp++;
    611 		} while (*cp == '/');
    612 		slashes = cp - ndp->ni_next;
    613 		ndp->ni_pathlen -= slashes;
    614 		ndp->ni_next = cp;
    615 		cnp->cn_flags |= REQUIREDIR;
    616 	} else {
    617 		slashes = 0;
    618 		cnp->cn_flags &= ~REQUIREDIR;
    619 	}
    620 	/*
    621 	 * We do special processing on the last component, whether or not it's
    622 	 * a directory.  Cache all intervening lookups, but not the final one.
    623 	 */
    624 	if (*cp == '\0') {
    625 		if (docache)
    626 			cnp->cn_flags |= MAKEENTRY;
    627 		else
    628 			cnp->cn_flags &= ~MAKEENTRY;
    629 		cnp->cn_flags |= ISLASTCN;
    630 	} else {
    631 		cnp->cn_flags |= MAKEENTRY;
    632 		cnp->cn_flags &= ~ISLASTCN;
    633 	}
    634 	if (cnp->cn_namelen == 2 &&
    635 	    cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.')
    636 		cnp->cn_flags |= ISDOTDOT;
    637 	else
    638 		cnp->cn_flags &= ~ISDOTDOT;
    639 
    640 	/*
    641 	 * Handle "..": two special cases.
    642 	 * 1. If at root directory (e.g. after chroot)
    643 	 *    or at absolute root directory
    644 	 *    then ignore it so can't get out.
    645 	 * 1a. If at the root of the emulation filesystem go to the real
    646 	 *    root. So "/../<path>" is always absolute.
    647 	 * 1b. If we have somehow gotten out of a jail, warn
    648 	 *    and also ignore it so we can't get farther out.
    649 	 * 2. If this vnode is the root of a mounted
    650 	 *    filesystem, then replace it with the
    651 	 *    vnode which was mounted on so we take the
    652 	 *    .. in the other file system.
    653 	 */
    654 	if (cnp->cn_flags & ISDOTDOT) {
    655 		struct proc *p = l->l_proc;
    656 
    657 		for (;;) {
    658 			if (dp == ndp->ni_rootdir || dp == rootvnode) {
    659 				ndp->ni_dvp = dp;
    660 				ndp->ni_vp = dp;
    661 				VREF(dp);
    662 				goto nextname;
    663 			}
    664 			if (ndp->ni_rootdir != rootvnode) {
    665 				int retval;
    666 
    667 				VOP_UNLOCK(dp, 0);
    668 				retval = vn_isunder(dp, ndp->ni_rootdir, l);
    669 				vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
    670 				if (!retval) {
    671 				    /* Oops! We got out of jail! */
    672 				    log(LOG_WARNING,
    673 					"chrooted pid %d uid %d (%s) "
    674 					"detected outside of its chroot\n",
    675 					p->p_pid, kauth_cred_geteuid(l->l_cred),
    676 					p->p_comm);
    677 				    /* Put us at the jail root. */
    678 				    vput(dp);
    679 				    dp = ndp->ni_rootdir;
    680 				    ndp->ni_dvp = dp;
    681 				    ndp->ni_vp = dp;
    682 				    VREF(dp);
    683 				    VREF(dp);
    684 				    vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
    685 				    goto nextname;
    686 				}
    687 			}
    688 			if ((dp->v_flag & VROOT) == 0 ||
    689 			    (cnp->cn_flags & NOCROSSMOUNT))
    690 				break;
    691 			tdp = dp;
    692 			dp = dp->v_mount->mnt_vnodecovered;
    693 			vput(tdp);
    694 			VREF(dp);
    695 			vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
    696 		}
    697 	}
    698 
    699 	/*
    700 	 * We now have a segment name to search for, and a directory to search.
    701 	 * Again, our only vnode state is that "dp" is held and locked.
    702 	 */
    703 unionlookup:
    704 	ndp->ni_dvp = dp;
    705 	ndp->ni_vp = NULL;
    706 	error = VOP_LOOKUP(dp, &ndp->ni_vp, cnp);
    707 	if (error != 0) {
    708 #ifdef DIAGNOSTIC
    709 		if (ndp->ni_vp != NULL)
    710 			panic("leaf `%s' should be empty", cnp->cn_nameptr);
    711 #endif /* DIAGNOSTIC */
    712 #ifdef NAMEI_DIAGNOSTIC
    713 		printf("not found\n");
    714 #endif /* NAMEI_DIAGNOSTIC */
    715 		if ((error == ENOENT) &&
    716 		    (dp->v_flag & VROOT) &&
    717 		    (dp->v_mount->mnt_flag & MNT_UNION)) {
    718 			tdp = dp;
    719 			dp = dp->v_mount->mnt_vnodecovered;
    720 			vput(tdp);
    721 			VREF(dp);
    722 			vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
    723 			goto unionlookup;
    724 		}
    725 
    726 		if (error != EJUSTRETURN)
    727 			goto bad;
    728 
    729 		/*
    730 		 * If this was not the last component, or there were trailing
    731 		 * slashes, and we are not going to create a directory,
    732 		 * then the name must exist.
    733 		 */
    734 		if ((cnp->cn_flags & (REQUIREDIR | CREATEDIR)) == REQUIREDIR) {
    735 			error = ENOENT;
    736 			goto bad;
    737 		}
    738 
    739 		/*
    740 		 * If creating and at end of pathname, then can consider
    741 		 * allowing file to be created.
    742 		 */
    743 		if (rdonly) {
    744 			error = EROFS;
    745 			goto bad;
    746 		}
    747 
    748 		/*
    749 		 * We return with ni_vp NULL to indicate that the entry
    750 		 * doesn't currently exist, leaving a pointer to the
    751 		 * (possibly locked) directory vnode in ndp->ni_dvp.
    752 		 */
    753 		if (cnp->cn_flags & SAVESTART) {
    754 			ndp->ni_startdir = ndp->ni_dvp;
    755 			VREF(ndp->ni_startdir);
    756 		}
    757 		return (0);
    758 	}
    759 #ifdef NAMEI_DIAGNOSTIC
    760 	printf("found\n");
    761 #endif /* NAMEI_DIAGNOSTIC */
    762 
    763 	/*
    764 	 * Take into account any additional components consumed by the
    765 	 * underlying filesystem.  This will include any trailing slashes after
    766 	 * the last component consumed.
    767 	 */
    768 	if (cnp->cn_consume > 0) {
    769 		ndp->ni_pathlen -= cnp->cn_consume - slashes;
    770 		ndp->ni_next += cnp->cn_consume - slashes;
    771 		cnp->cn_consume = 0;
    772 		if (ndp->ni_next[0] == '\0')
    773 			cnp->cn_flags |= ISLASTCN;
    774 	}
    775 
    776 	dp = ndp->ni_vp;
    777 
    778 	/*
    779 	 * "dp" and "ndp->ni_dvp" are both locked and held,
    780 	 * and may be the same vnode.
    781 	 */
    782 
    783 	/*
    784 	 * Check to see if the vnode has been mounted on;
    785 	 * if so find the root of the mounted file system.
    786 	 */
    787 	while (dp->v_type == VDIR && (mp = dp->v_mountedhere) &&
    788 	       (cnp->cn_flags & NOCROSSMOUNT) == 0) {
    789 		if (vfs_busy(mp, 0, 0))
    790 			continue;
    791 
    792 		KASSERT(ndp->ni_dvp != dp);
    793 		VOP_UNLOCK(ndp->ni_dvp, 0);
    794 		vput(dp);
    795 		error = VFS_ROOT(mp, &tdp);
    796 		vfs_unbusy(mp);
    797 		if (error) {
    798 			vn_lock(ndp->ni_dvp, LK_EXCLUSIVE | LK_RETRY);
    799 			goto bad;
    800 		}
    801 		VOP_UNLOCK(tdp, 0);
    802 		ndp->ni_vp = dp = tdp;
    803 		vn_lock(ndp->ni_dvp, LK_EXCLUSIVE | LK_RETRY);
    804 		vn_lock(ndp->ni_vp, LK_EXCLUSIVE | LK_RETRY);
    805 	}
    806 
    807 	/*
    808 	 * Check for symbolic link.  Back up over any slashes that we skipped,
    809 	 * as we will need them again.
    810 	 */
    811 	if ((dp->v_type == VLNK) && (cnp->cn_flags & (FOLLOW|REQUIREDIR))) {
    812 		ndp->ni_pathlen += slashes;
    813 		ndp->ni_next -= slashes;
    814 		cnp->cn_flags |= ISSYMLINK;
    815 		return (0);
    816 	}
    817 
    818 	/*
    819 	 * Check for directory, if the component was followed by a series of
    820 	 * slashes.
    821 	 */
    822 	if ((dp->v_type != VDIR) && (cnp->cn_flags & REQUIREDIR)) {
    823 		error = ENOTDIR;
    824 		KASSERT(dp != ndp->ni_dvp);
    825 		vput(dp);
    826 		goto bad;
    827 	}
    828 
    829 nextname:
    830 
    831 	/*
    832 	 * Not a symbolic link.  If this was not the last component, then
    833 	 * continue at the next component, else return.
    834 	 */
    835 	if (!(cnp->cn_flags & ISLASTCN)) {
    836 		cnp->cn_nameptr = ndp->ni_next;
    837 		if (ndp->ni_dvp == dp) {
    838 			vrele(ndp->ni_dvp);
    839 		} else {
    840 			vput(ndp->ni_dvp);
    841 		}
    842 		goto dirloop;
    843 	}
    844 
    845 terminal:
    846 	if (dp == ndp->ni_erootdir) {
    847 		/*
    848 		 * We are about to return the emulation root.
    849 		 * This isn't a good idea because code might repeatedly
    850 		 * lookup ".." until the file matches that returned
    851 		 * for "/" and loop forever.
    852 		 * So convert it to the real root.
    853 		 */
    854 		if (ndp->ni_dvp == dp)
    855 			vrele(dp);
    856 		else
    857 			if (ndp->ni_dvp != NULL)
    858 				vput(ndp->ni_dvp);
    859 		ndp->ni_dvp = NULL;
    860 		vput(dp);
    861 		dp = ndp->ni_rootdir;
    862 		VREF(dp);
    863 		vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
    864 		ndp->ni_vp = dp;
    865 	}
    866 
    867 	/*
    868 	 * If the caller requested the parent node (i.e.
    869 	 * it's a CREATE, DELETE, or RENAME), and we don't have one
    870 	 * (because this is the root directory), then we must fail.
    871 	 */
    872 	if (ndp->ni_dvp == NULL && cnp->cn_nameiop != LOOKUP) {
    873 		switch (cnp->cn_nameiop) {
    874 		case CREATE:
    875 			error = EEXIST;
    876 			break;
    877 		case DELETE:
    878 		case RENAME:
    879 			error = EBUSY;
    880 			break;
    881 		default:
    882 			KASSERT(0);
    883 		}
    884 		vput(dp);
    885 		goto bad;
    886 	}
    887 
    888 	/*
    889 	 * Disallow directory write attempts on read-only file systems.
    890 	 */
    891 	if (rdonly &&
    892 	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
    893 
    894 		/*
    895 		 * Disallow directory write attempts on read-only
    896 		 * file systems.
    897 		 */
    898 		error = EROFS;
    899 		if (dp != ndp->ni_dvp) {
    900 			vput(dp);
    901 		}
    902 		goto bad;
    903 	}
    904 	if (ndp->ni_dvp != NULL) {
    905 		if (cnp->cn_flags & SAVESTART) {
    906 			ndp->ni_startdir = ndp->ni_dvp;
    907 			VREF(ndp->ni_startdir);
    908 		}
    909 	}
    910 	if ((cnp->cn_flags & LOCKLEAF) == 0) {
    911 		VOP_UNLOCK(dp, 0);
    912 	}
    913 	return (0);
    914 
    915 bad:
    916 	ndp->ni_vp = NULL;
    917 	return (error);
    918 }
    919 
    920 /*
    921  * Reacquire a path name component.
    922  * dvp is locked on entry and exit.
    923  * *vpp is locked on exit unless it's NULL.
    924  */
    925 int
    926 relookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp)
    927 {
    928 	int rdonly;			/* lookup read-only flag bit */
    929 	int error = 0;
    930 #ifdef DEBUG
    931 	uint32_t newhash;		/* DEBUG: check name hash */
    932 	const char *cp;			/* DEBUG: check name ptr/len */
    933 #endif /* DEBUG */
    934 
    935 	/*
    936 	 * Setup: break out flag bits into variables.
    937 	 */
    938 	rdonly = cnp->cn_flags & RDONLY;
    939 	cnp->cn_flags &= ~ISSYMLINK;
    940 
    941 	/*
    942 	 * Search a new directory.
    943 	 *
    944 	 * The cn_hash value is for use by vfs_cache.
    945 	 * The last component of the filename is left accessible via
    946 	 * cnp->cn_nameptr for callers that need the name. Callers needing
    947 	 * the name set the SAVENAME flag. When done, they assume
    948 	 * responsibility for freeing the pathname buffer.
    949 	 */
    950 #ifdef DEBUG
    951 	cp = NULL;
    952 	newhash = namei_hash(cnp->cn_nameptr, &cp);
    953 	if ((uint32_t)newhash != (uint32_t)cnp->cn_hash)
    954 		panic("relookup: bad hash");
    955 	if (cnp->cn_namelen != cp - cnp->cn_nameptr)
    956 		panic("relookup: bad len");
    957 	while (*cp == '/')
    958 		cp++;
    959 	if (*cp != 0)
    960 		panic("relookup: not last component");
    961 #endif /* DEBUG */
    962 
    963 	/*
    964 	 * Check for degenerate name (e.g. / or "")
    965 	 * which is a way of talking about a directory,
    966 	 * e.g. like "/." or ".".
    967 	 */
    968 	if (cnp->cn_nameptr[0] == '\0')
    969 		panic("relookup: null name");
    970 
    971 	if (cnp->cn_flags & ISDOTDOT)
    972 		panic("relookup: lookup on dot-dot");
    973 
    974 	/*
    975 	 * We now have a segment name to search for, and a directory to search.
    976 	 */
    977 	if ((error = VOP_LOOKUP(dvp, vpp, cnp)) != 0) {
    978 #ifdef DIAGNOSTIC
    979 		if (*vpp != NULL)
    980 			panic("leaf `%s' should be empty", cnp->cn_nameptr);
    981 #endif
    982 		if (error != EJUSTRETURN)
    983 			goto bad;
    984 	}
    985 
    986 #ifdef DIAGNOSTIC
    987 	/*
    988 	 * Check for symbolic link
    989 	 */
    990 	if (*vpp && (*vpp)->v_type == VLNK && (cnp->cn_flags & FOLLOW))
    991 		panic("relookup: symlink found");
    992 #endif
    993 
    994 	/*
    995 	 * Check for read-only file systems.
    996 	 */
    997 	if (rdonly && cnp->cn_nameiop != LOOKUP) {
    998 		error = EROFS;
    999 		if (*vpp) {
   1000 			vput(*vpp);
   1001 		}
   1002 		goto bad;
   1003 	}
   1004 	if (cnp->cn_flags & SAVESTART)
   1005 		VREF(dvp);
   1006 	return (0);
   1007 
   1008 bad:
   1009 	*vpp = NULL;
   1010 	return (error);
   1011 }
   1012