xref: /titanic_51/usr/src/uts/common/fs/lookup.c (revision 2f5706a12e66ecfd6addbd3136587542d162304c)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52e538c1cSvv149972  * Common Development and Distribution License (the "License").
62e538c1cSvv149972  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*2f5706a1SWilliam.Johnston  * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
237c478bd9Sstevel@tonic-gate  */
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
267c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
307c478bd9Sstevel@tonic-gate  * The Regents of the University of California
317c478bd9Sstevel@tonic-gate  * All Rights Reserved
327c478bd9Sstevel@tonic-gate  *
337c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
347c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
357c478bd9Sstevel@tonic-gate  * contributors.
367c478bd9Sstevel@tonic-gate  */
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #include <sys/types.h>
397c478bd9Sstevel@tonic-gate #include <sys/param.h>
407c478bd9Sstevel@tonic-gate #include <sys/systm.h>
417c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
427c478bd9Sstevel@tonic-gate #include <sys/errno.h>
437c478bd9Sstevel@tonic-gate #include <sys/cred.h>
447c478bd9Sstevel@tonic-gate #include <sys/user.h>
457c478bd9Sstevel@tonic-gate #include <sys/uio.h>
467c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
477c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
487c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
497c478bd9Sstevel@tonic-gate #include <sys/proc.h>
507c478bd9Sstevel@tonic-gate #include <sys/vtrace.h>
517c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
527c478bd9Sstevel@tonic-gate #include <sys/debug.h>
537c478bd9Sstevel@tonic-gate #include <sys/dirent.h>
547c478bd9Sstevel@tonic-gate #include <c2/audit.h>
557c478bd9Sstevel@tonic-gate #include <sys/zone.h>
567c478bd9Sstevel@tonic-gate #include <sys/dnlc.h>
577c478bd9Sstevel@tonic-gate #include <sys/fs/snode.h>
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate /* Controls whether paths are stored with vnodes. */
607c478bd9Sstevel@tonic-gate int vfs_vnode_path = 1;
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate int
637c478bd9Sstevel@tonic-gate lookupname(
647c478bd9Sstevel@tonic-gate 	char *fnamep,
657c478bd9Sstevel@tonic-gate 	enum uio_seg seg,
667c478bd9Sstevel@tonic-gate 	enum symfollow followlink,
677c478bd9Sstevel@tonic-gate 	vnode_t **dirvpp,
687c478bd9Sstevel@tonic-gate 	vnode_t **compvpp)
697c478bd9Sstevel@tonic-gate {
707a142be9SCasper H.S. Dik 	return (lookupnameatcred(fnamep, seg, followlink, dirvpp, compvpp, NULL,
717a142be9SCasper H.S. Dik 	    CRED()));
727c478bd9Sstevel@tonic-gate }
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate /*
757c478bd9Sstevel@tonic-gate  * Lookup the user file name,
767c478bd9Sstevel@tonic-gate  * Handle allocation and freeing of pathname buffer, return error.
777c478bd9Sstevel@tonic-gate  */
787c478bd9Sstevel@tonic-gate int
797a142be9SCasper H.S. Dik lookupnameatcred(
807c478bd9Sstevel@tonic-gate 	char *fnamep,			/* user pathname */
817c478bd9Sstevel@tonic-gate 	enum uio_seg seg,		/* addr space that name is in */
827c478bd9Sstevel@tonic-gate 	enum symfollow followlink,	/* follow sym links */
837c478bd9Sstevel@tonic-gate 	vnode_t **dirvpp,		/* ret for ptr to parent dir vnode */
847c478bd9Sstevel@tonic-gate 	vnode_t **compvpp,		/* ret for ptr to component vnode */
857a142be9SCasper H.S. Dik 	vnode_t *startvp,		/* start path search from vp */
867a142be9SCasper H.S. Dik 	cred_t *cr)			/* credential */
877c478bd9Sstevel@tonic-gate {
887c478bd9Sstevel@tonic-gate 	char namebuf[TYPICALMAXPATHLEN];
897c478bd9Sstevel@tonic-gate 	struct pathname lookpn;
907c478bd9Sstevel@tonic-gate 	int error;
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 	error = pn_get_buf(fnamep, seg, &lookpn, namebuf, sizeof (namebuf));
937c478bd9Sstevel@tonic-gate 	if (error == 0) {
94005d3febSMarek Pospisil 		if (AU_AUDITING())
957c478bd9Sstevel@tonic-gate 			audit_lookupname();
967a142be9SCasper H.S. Dik 		error = lookuppnatcred(&lookpn, NULL, followlink,
977a142be9SCasper H.S. Dik 		    dirvpp, compvpp, startvp, cr);
987c478bd9Sstevel@tonic-gate 	}
997c478bd9Sstevel@tonic-gate 	if (error == ENAMETOOLONG) {
1007c478bd9Sstevel@tonic-gate 		/*
1017c478bd9Sstevel@tonic-gate 		 * This thread used a pathname > TYPICALMAXPATHLEN bytes long.
1027c478bd9Sstevel@tonic-gate 		 */
1037c478bd9Sstevel@tonic-gate 		if (error = pn_get(fnamep, seg, &lookpn))
1047c478bd9Sstevel@tonic-gate 			return (error);
1057a142be9SCasper H.S. Dik 		error = lookuppnatcred(&lookpn, NULL, followlink,
1067a142be9SCasper H.S. Dik 		    dirvpp, compvpp, startvp, cr);
1077c478bd9Sstevel@tonic-gate 		pn_free(&lookpn);
1087c478bd9Sstevel@tonic-gate 	}
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	return (error);
1117c478bd9Sstevel@tonic-gate }
1127c478bd9Sstevel@tonic-gate 
1137a142be9SCasper H.S. Dik int
1147a142be9SCasper H.S. Dik lookupnameat(char *fnamep, enum uio_seg seg, enum symfollow followlink,
1157a142be9SCasper H.S. Dik     vnode_t **dirvpp, vnode_t **compvpp, vnode_t *startvp)
1167a142be9SCasper H.S. Dik {
1177a142be9SCasper H.S. Dik 	return (lookupnameatcred(fnamep, seg, followlink, dirvpp, compvpp,
1187a142be9SCasper H.S. Dik 	    startvp, CRED()));
1197a142be9SCasper H.S. Dik }
1207a142be9SCasper H.S. Dik 
1217c478bd9Sstevel@tonic-gate int
1227c478bd9Sstevel@tonic-gate lookuppn(
1237c478bd9Sstevel@tonic-gate 	struct pathname *pnp,
1247c478bd9Sstevel@tonic-gate 	struct pathname *rpnp,
1257c478bd9Sstevel@tonic-gate 	enum symfollow followlink,
1267c478bd9Sstevel@tonic-gate 	vnode_t **dirvpp,
1277c478bd9Sstevel@tonic-gate 	vnode_t **compvpp)
1287c478bd9Sstevel@tonic-gate {
1297a142be9SCasper H.S. Dik 	return (lookuppnatcred(pnp, rpnp, followlink, dirvpp, compvpp, NULL,
1307a142be9SCasper H.S. Dik 	    CRED()));
1317c478bd9Sstevel@tonic-gate }
1327c478bd9Sstevel@tonic-gate 
1337a142be9SCasper H.S. Dik /*
1347a142be9SCasper H.S. Dik  * Lookup the user file name from a given vp, using a specific credential.
1357a142be9SCasper H.S. Dik  */
1367c478bd9Sstevel@tonic-gate int
1377a142be9SCasper H.S. Dik lookuppnatcred(
1387c478bd9Sstevel@tonic-gate 	struct pathname *pnp,		/* pathname to lookup */
1397c478bd9Sstevel@tonic-gate 	struct pathname *rpnp,		/* if non-NULL, return resolved path */
1407c478bd9Sstevel@tonic-gate 	enum symfollow followlink,	/* (don't) follow sym links */
1417c478bd9Sstevel@tonic-gate 	vnode_t **dirvpp,		/* ptr for parent vnode */
1427c478bd9Sstevel@tonic-gate 	vnode_t **compvpp,		/* ptr for entry vnode */
1437a142be9SCasper H.S. Dik 	vnode_t *startvp,		/* start search from this vp */
1447a142be9SCasper H.S. Dik 	cred_t *cr)			/* user credential */
1457c478bd9Sstevel@tonic-gate {
1467c478bd9Sstevel@tonic-gate 	vnode_t *vp;	/* current directory vp */
1477c478bd9Sstevel@tonic-gate 	vnode_t *rootvp;
1487c478bd9Sstevel@tonic-gate 	proc_t *p = curproc;
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 	if (pnp->pn_pathlen == 0)
1517c478bd9Sstevel@tonic-gate 		return (ENOENT);
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);	/* for u_rdir and u_cdir */
1547c478bd9Sstevel@tonic-gate 	if ((rootvp = PTOU(p)->u_rdir) == NULL)
1557c478bd9Sstevel@tonic-gate 		rootvp = rootdir;
1567c478bd9Sstevel@tonic-gate 	else if (rootvp != rootdir)	/* no need to VN_HOLD rootdir */
1577c478bd9Sstevel@tonic-gate 		VN_HOLD(rootvp);
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	if (pnp->pn_path[0] == '/') {
1607c478bd9Sstevel@tonic-gate 		vp = rootvp;
1617c478bd9Sstevel@tonic-gate 	} else {
1627c478bd9Sstevel@tonic-gate 		vp = (startvp == NULL) ? PTOU(p)->u_cdir : startvp;
1637c478bd9Sstevel@tonic-gate 	}
1647c478bd9Sstevel@tonic-gate 	VN_HOLD(vp);
1657c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	/*
1687c478bd9Sstevel@tonic-gate 	 * Skip over leading slashes
1697c478bd9Sstevel@tonic-gate 	 */
1707c478bd9Sstevel@tonic-gate 	if (pnp->pn_path[0] == '/') {
1717c478bd9Sstevel@tonic-gate 		do {
1727c478bd9Sstevel@tonic-gate 			pnp->pn_path++;
1737c478bd9Sstevel@tonic-gate 			pnp->pn_pathlen--;
1747c478bd9Sstevel@tonic-gate 		} while (pnp->pn_path[0] == '/');
1757c478bd9Sstevel@tonic-gate 	}
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	return (lookuppnvp(pnp, rpnp, followlink, dirvpp,
1787a142be9SCasper H.S. Dik 	    compvpp, rootvp, vp, cr));
1797a142be9SCasper H.S. Dik }
1807a142be9SCasper H.S. Dik 
1817a142be9SCasper H.S. Dik int
1827a142be9SCasper H.S. Dik lookuppnat(struct pathname *pnp, struct pathname *rpnp,
1837a142be9SCasper H.S. Dik     enum symfollow followlink, vnode_t **dirvpp, vnode_t **compvpp,
1847a142be9SCasper H.S. Dik     vnode_t *startvp)
1857a142be9SCasper H.S. Dik {
1867a142be9SCasper H.S. Dik 	return (lookuppnatcred(pnp, rpnp, followlink, dirvpp, compvpp, startvp,
1877a142be9SCasper H.S. Dik 	    CRED()));
1887c478bd9Sstevel@tonic-gate }
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate /* Private flag to do our getcwd() dirty work */
1917c478bd9Sstevel@tonic-gate #define	LOOKUP_CHECKREAD	0x10
1927c478bd9Sstevel@tonic-gate #define	LOOKUP_MASK		(~LOOKUP_CHECKREAD)
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate /*
1957c478bd9Sstevel@tonic-gate  * Starting at current directory, translate pathname pnp to end.
1967c478bd9Sstevel@tonic-gate  * Leave pathname of final component in pnp, return the vnode
1977c478bd9Sstevel@tonic-gate  * for the final component in *compvpp, and return the vnode
1987c478bd9Sstevel@tonic-gate  * for the parent of the final component in dirvpp.
1997c478bd9Sstevel@tonic-gate  *
2007c478bd9Sstevel@tonic-gate  * This is the central routine in pathname translation and handles
2017c478bd9Sstevel@tonic-gate  * multiple components in pathnames, separating them at /'s.  It also
2027c478bd9Sstevel@tonic-gate  * implements mounted file systems and processes symbolic links.
2037c478bd9Sstevel@tonic-gate  *
2047c478bd9Sstevel@tonic-gate  * vp is the vnode where the directory search should start.
2057c478bd9Sstevel@tonic-gate  *
2067c478bd9Sstevel@tonic-gate  * Reference counts: vp must be held prior to calling this function.  rootvp
2077c478bd9Sstevel@tonic-gate  * should only be held if rootvp != rootdir.
2087c478bd9Sstevel@tonic-gate  */
2097c478bd9Sstevel@tonic-gate int
2107c478bd9Sstevel@tonic-gate lookuppnvp(
2117c478bd9Sstevel@tonic-gate 	struct pathname *pnp,		/* pathname to lookup */
2127c478bd9Sstevel@tonic-gate 	struct pathname *rpnp,		/* if non-NULL, return resolved path */
2137c478bd9Sstevel@tonic-gate 	int flags,			/* follow symlinks */
2147c478bd9Sstevel@tonic-gate 	vnode_t **dirvpp,		/* ptr for parent vnode */
2157c478bd9Sstevel@tonic-gate 	vnode_t **compvpp,		/* ptr for entry vnode */
2167c478bd9Sstevel@tonic-gate 	vnode_t *rootvp,		/* rootvp */
2177c478bd9Sstevel@tonic-gate 	vnode_t *vp,			/* directory to start search at */
2187c478bd9Sstevel@tonic-gate 	cred_t *cr)			/* user's credential */
2197c478bd9Sstevel@tonic-gate {
2207c478bd9Sstevel@tonic-gate 	vnode_t *cvp;	/* current component vp */
2217c478bd9Sstevel@tonic-gate 	vnode_t *tvp;	/* addressable temp ptr */
2227c478bd9Sstevel@tonic-gate 	char component[MAXNAMELEN];	/* buffer for component (incl null) */
2237c478bd9Sstevel@tonic-gate 	int error;
2247c478bd9Sstevel@tonic-gate 	int nlink;
2257c478bd9Sstevel@tonic-gate 	int lookup_flags;
226da6c28aaSamw 	struct pathname presrvd; /* case preserved name */
227da6c28aaSamw 	struct pathname *pp = NULL;
2287c478bd9Sstevel@tonic-gate 	vnode_t *startvp;
2297c478bd9Sstevel@tonic-gate 	vnode_t *zonevp = curproc->p_zone->zone_rootvp;		/* zone root */
2307c478bd9Sstevel@tonic-gate 	int must_be_directory = 0;
23153ebc32aSVallish Vaidyeshwara 	boolean_t retry_with_kcred;
232005d3febSMarek Pospisil 	uint32_t auditing = AU_AUDITING();
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 	CPU_STATS_ADDQ(CPU, sys, namei, 1);
2357c478bd9Sstevel@tonic-gate 	nlink = 0;
2367c478bd9Sstevel@tonic-gate 	cvp = NULL;
2377c478bd9Sstevel@tonic-gate 	if (rpnp)
2387c478bd9Sstevel@tonic-gate 		rpnp->pn_pathlen = 0;
239da6c28aaSamw 
2407c478bd9Sstevel@tonic-gate 	lookup_flags = dirvpp ? LOOKUP_DIR : 0;
241da6c28aaSamw 	if (flags & FIGNORECASE) {
242da6c28aaSamw 		lookup_flags |= FIGNORECASE;
243da6c28aaSamw 		pn_alloc(&presrvd);
244da6c28aaSamw 		pp = &presrvd;
245da6c28aaSamw 	}
246da6c28aaSamw 
247005d3febSMarek Pospisil 	if (auditing)
2487c478bd9Sstevel@tonic-gate 		audit_anchorpath(pnp, vp == rootvp);
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 	/*
2517c478bd9Sstevel@tonic-gate 	 * Eliminate any trailing slashes in the pathname.
2527c478bd9Sstevel@tonic-gate 	 * If there are any, we must follow all symlinks.
2537c478bd9Sstevel@tonic-gate 	 * Also, we must guarantee that the last component is a directory.
2547c478bd9Sstevel@tonic-gate 	 */
2557c478bd9Sstevel@tonic-gate 	if (pn_fixslash(pnp)) {
2567c478bd9Sstevel@tonic-gate 		flags |= FOLLOW;
2577c478bd9Sstevel@tonic-gate 		must_be_directory = 1;
2587c478bd9Sstevel@tonic-gate 	}
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 	startvp = vp;
2617c478bd9Sstevel@tonic-gate next:
26253ebc32aSVallish Vaidyeshwara 	retry_with_kcred = B_FALSE;
26353ebc32aSVallish Vaidyeshwara 
2647c478bd9Sstevel@tonic-gate 	/*
2657c478bd9Sstevel@tonic-gate 	 * Make sure we have a directory.
2667c478bd9Sstevel@tonic-gate 	 */
2677c478bd9Sstevel@tonic-gate 	if (vp->v_type != VDIR) {
2687c478bd9Sstevel@tonic-gate 		error = ENOTDIR;
2697c478bd9Sstevel@tonic-gate 		goto bad;
2707c478bd9Sstevel@tonic-gate 	}
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 	if (rpnp && VN_CMP(vp, rootvp))
2737c478bd9Sstevel@tonic-gate 		(void) pn_set(rpnp, "/");
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 	/*
2767c478bd9Sstevel@tonic-gate 	 * Process the next component of the pathname.
2777c478bd9Sstevel@tonic-gate 	 */
2787c478bd9Sstevel@tonic-gate 	if (error = pn_getcomponent(pnp, component)) {
279005d3febSMarek Pospisil 		if (auditing)
2807c478bd9Sstevel@tonic-gate 			audit_addcomponent(pnp);
2817c478bd9Sstevel@tonic-gate 		goto bad;
2827c478bd9Sstevel@tonic-gate 	}
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 	/*
2857c478bd9Sstevel@tonic-gate 	 * Handle "..": two special cases.
2867c478bd9Sstevel@tonic-gate 	 * 1. If we're at the root directory (e.g. after chroot or
2877c478bd9Sstevel@tonic-gate 	 *    zone_enter) then change ".." to "." so we can't get
2887c478bd9Sstevel@tonic-gate 	 *    out of this subtree.
2897c478bd9Sstevel@tonic-gate 	 * 2. If this vnode is the root of a mounted file system,
2907c478bd9Sstevel@tonic-gate 	 *    then replace it with the vnode that was mounted on
2917c478bd9Sstevel@tonic-gate 	 *    so that we take the ".." in the other file system.
2927c478bd9Sstevel@tonic-gate 	 */
2937c478bd9Sstevel@tonic-gate 	if (component[0] == '.' && component[1] == '.' && component[2] == 0) {
2947c478bd9Sstevel@tonic-gate checkforroot:
2957c478bd9Sstevel@tonic-gate 		if (VN_CMP(vp, rootvp) || VN_CMP(vp, zonevp)) {
2967c478bd9Sstevel@tonic-gate 			component[1] = '\0';
2977c478bd9Sstevel@tonic-gate 		} else if (vp->v_flag & VROOT) {
2987c478bd9Sstevel@tonic-gate 			vfs_t *vfsp;
2997c478bd9Sstevel@tonic-gate 			cvp = vp;
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 			/*
3027c478bd9Sstevel@tonic-gate 			 * While we deal with the vfs pointer from the vnode
3037c478bd9Sstevel@tonic-gate 			 * the filesystem could have been forcefully unmounted
3047c478bd9Sstevel@tonic-gate 			 * and the vnode's v_vfsp could have been invalidated
3057c478bd9Sstevel@tonic-gate 			 * by VFS_UNMOUNT. Hence, we cache v_vfsp and use it
3067c478bd9Sstevel@tonic-gate 			 * with vfs_rlock_wait/vfs_unlock.
3077c478bd9Sstevel@tonic-gate 			 * It is safe to use the v_vfsp even it is freed by
3087c478bd9Sstevel@tonic-gate 			 * VFS_UNMOUNT because vfs_rlock_wait/vfs_unlock
3097c478bd9Sstevel@tonic-gate 			 * do not dereference v_vfsp. It is just used as a
3107c478bd9Sstevel@tonic-gate 			 * magic cookie.
3117c478bd9Sstevel@tonic-gate 			 * One more corner case here is the memory getting
3127c478bd9Sstevel@tonic-gate 			 * reused for another vfs structure. In this case
3137c478bd9Sstevel@tonic-gate 			 * lookuppnvp's vfs_rlock_wait will succeed, domount's
3147c478bd9Sstevel@tonic-gate 			 * vfs_lock will fail and domount will bail out with an
3157c478bd9Sstevel@tonic-gate 			 * error (EBUSY).
3167c478bd9Sstevel@tonic-gate 			 */
3177c478bd9Sstevel@tonic-gate 			vfsp = cvp->v_vfsp;
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 			/*
3207c478bd9Sstevel@tonic-gate 			 * This lock is used to synchronize
3217c478bd9Sstevel@tonic-gate 			 * mounts/unmounts and lookups.
3227c478bd9Sstevel@tonic-gate 			 * Threads doing mounts/unmounts hold the
3237c478bd9Sstevel@tonic-gate 			 * writers version vfs_lock_wait().
3247c478bd9Sstevel@tonic-gate 			 */
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 			vfs_rlock_wait(vfsp);
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 			/*
3297c478bd9Sstevel@tonic-gate 			 * If this vnode is on a file system that
3307c478bd9Sstevel@tonic-gate 			 * has been forcibly unmounted,
3317c478bd9Sstevel@tonic-gate 			 * we can't proceed. Cancel this operation
3327c478bd9Sstevel@tonic-gate 			 * and return EIO.
3337c478bd9Sstevel@tonic-gate 			 *
3347c478bd9Sstevel@tonic-gate 			 * vfs_vnodecovered is NULL if unmounted.
3357c478bd9Sstevel@tonic-gate 			 * Currently, nfs uses VFS_UNMOUNTED to
3367c478bd9Sstevel@tonic-gate 			 * check if it's a forced-umount. Keep the
3377c478bd9Sstevel@tonic-gate 			 * same checking here as well even though it
3387c478bd9Sstevel@tonic-gate 			 * may not be needed.
3397c478bd9Sstevel@tonic-gate 			 */
3407c478bd9Sstevel@tonic-gate 			if (((vp = cvp->v_vfsp->vfs_vnodecovered) == NULL) ||
3417c478bd9Sstevel@tonic-gate 			    (cvp->v_vfsp->vfs_flag & VFS_UNMOUNTED)) {
3427c478bd9Sstevel@tonic-gate 				vfs_unlock(vfsp);
3437c478bd9Sstevel@tonic-gate 				VN_RELE(cvp);
344da6c28aaSamw 				if (pp)
345da6c28aaSamw 					pn_free(pp);
3467c478bd9Sstevel@tonic-gate 				return (EIO);
3477c478bd9Sstevel@tonic-gate 			}
3487c478bd9Sstevel@tonic-gate 			VN_HOLD(vp);
3497c478bd9Sstevel@tonic-gate 			vfs_unlock(vfsp);
3507c478bd9Sstevel@tonic-gate 			VN_RELE(cvp);
3517c478bd9Sstevel@tonic-gate 			cvp = NULL;
3522e538c1cSvv149972 			/*
3532e538c1cSvv149972 			 * Crossing mount points. For eg: We are doing
3542e538c1cSvv149972 			 * a lookup of ".." for file systems root vnode
3552e538c1cSvv149972 			 * mounted here, and VOP_LOOKUP() (with covered vnode)
3562e538c1cSvv149972 			 * will be on underlying file systems mount point
3572e538c1cSvv149972 			 * vnode. Set retry_with_kcred flag as we might end
3582e538c1cSvv149972 			 * up doing VOP_LOOKUP() with kcred if required.
3592e538c1cSvv149972 			 */
3602e538c1cSvv149972 			retry_with_kcred = B_TRUE;
3617c478bd9Sstevel@tonic-gate 			goto checkforroot;
3627c478bd9Sstevel@tonic-gate 		}
3637c478bd9Sstevel@tonic-gate 	}
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 	/*
3667c478bd9Sstevel@tonic-gate 	 * LOOKUP_CHECKREAD is a private flag used by vnodetopath() to indicate
3677c478bd9Sstevel@tonic-gate 	 * that we need to have read permission on every directory in the entire
3687c478bd9Sstevel@tonic-gate 	 * path.  This is used to ensure that a forward-lookup of a cached value
3697c478bd9Sstevel@tonic-gate 	 * has the same effect as a reverse-lookup when the cached value cannot
3707c478bd9Sstevel@tonic-gate 	 * be found.
3717c478bd9Sstevel@tonic-gate 	 */
3727c478bd9Sstevel@tonic-gate 	if ((flags & LOOKUP_CHECKREAD) &&
373da6c28aaSamw 	    (error = VOP_ACCESS(vp, VREAD, 0, cr, NULL)) != 0)
3747c478bd9Sstevel@tonic-gate 		goto bad;
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 	/*
3777c478bd9Sstevel@tonic-gate 	 * Perform a lookup in the current directory.
3787c478bd9Sstevel@tonic-gate 	 */
3797c478bd9Sstevel@tonic-gate 	error = VOP_LOOKUP(vp, component, &tvp, pnp, lookup_flags,
380da6c28aaSamw 	    rootvp, cr, NULL, NULL, pp);
3812e538c1cSvv149972 
3822e538c1cSvv149972 	/*
3832e538c1cSvv149972 	 * Retry with kcred - If crossing mount points & error is EACCES.
3842e538c1cSvv149972 	 *
3852e538c1cSvv149972 	 * If we are crossing mount points here and doing ".." lookup,
3862e538c1cSvv149972 	 * VOP_LOOKUP() might fail if the underlying file systems
3872e538c1cSvv149972 	 * mount point has no execute permission. In cases like these,
3882e538c1cSvv149972 	 * we retry VOP_LOOKUP() by giving as much privilage as possible
3892e538c1cSvv149972 	 * by passing kcred credentials.
3902e538c1cSvv149972 	 *
3912e538c1cSvv149972 	 * In case of hierarchical file systems, passing kcred still may
3922e538c1cSvv149972 	 * or may not work.
3932e538c1cSvv149972 	 * For eg: UFS FS --> Mount NFS FS --> Again mount UFS on some
3942e538c1cSvv149972 	 *			directory inside NFS FS.
3952e538c1cSvv149972 	 */
3962e538c1cSvv149972 	if ((error == EACCES) && retry_with_kcred)
3972e538c1cSvv149972 		error = VOP_LOOKUP(vp, component, &tvp, pnp, lookup_flags,
398da6c28aaSamw 		    rootvp, zone_kcred(), NULL, NULL, pp);
3992e538c1cSvv149972 
4007c478bd9Sstevel@tonic-gate 	cvp = tvp;
4017c478bd9Sstevel@tonic-gate 	if (error) {
4027c478bd9Sstevel@tonic-gate 		cvp = NULL;
4037c478bd9Sstevel@tonic-gate 		/*
4047c478bd9Sstevel@tonic-gate 		 * On error, return hard error if
4057c478bd9Sstevel@tonic-gate 		 * (a) we're not at the end of the pathname yet, or
4067c478bd9Sstevel@tonic-gate 		 * (b) the caller didn't want the parent directory, or
4077c478bd9Sstevel@tonic-gate 		 * (c) we failed for some reason other than a missing entry.
4087c478bd9Sstevel@tonic-gate 		 */
4097c478bd9Sstevel@tonic-gate 		if (pn_pathleft(pnp) || dirvpp == NULL || error != ENOENT)
4107c478bd9Sstevel@tonic-gate 			goto bad;
411005d3febSMarek Pospisil 		if (auditing) {	/* directory access */
4127c478bd9Sstevel@tonic-gate 			if (error = audit_savepath(pnp, vp, error, cr))
4137c478bd9Sstevel@tonic-gate 				goto bad_noaudit;
4147c478bd9Sstevel@tonic-gate 		}
4157c478bd9Sstevel@tonic-gate 		pn_setlast(pnp);
4167c478bd9Sstevel@tonic-gate 		/*
4177c478bd9Sstevel@tonic-gate 		 * We inform the caller that the desired entry must be
4187c478bd9Sstevel@tonic-gate 		 * a directory by adding a '/' to the component name.
4197c478bd9Sstevel@tonic-gate 		 */
4207c478bd9Sstevel@tonic-gate 		if (must_be_directory && (error = pn_addslash(pnp)) != 0)
4217c478bd9Sstevel@tonic-gate 			goto bad;
4227c478bd9Sstevel@tonic-gate 		*dirvpp = vp;
4237c478bd9Sstevel@tonic-gate 		if (compvpp != NULL)
4247c478bd9Sstevel@tonic-gate 			*compvpp = NULL;
4257c478bd9Sstevel@tonic-gate 		if (rootvp != rootdir)
4267c478bd9Sstevel@tonic-gate 			VN_RELE(rootvp);
427da6c28aaSamw 		if (pp)
428da6c28aaSamw 			pn_free(pp);
4297c478bd9Sstevel@tonic-gate 		return (0);
4307c478bd9Sstevel@tonic-gate 	}
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate 	/*
4337c478bd9Sstevel@tonic-gate 	 * Traverse mount points.
4347c478bd9Sstevel@tonic-gate 	 * XXX why don't we need to hold a read lock here (call vn_vfsrlock)?
4357c478bd9Sstevel@tonic-gate 	 * What prevents a concurrent update to v_vfsmountedhere?
4367c478bd9Sstevel@tonic-gate 	 * 	Possible answer: if mounting, we might not see the mount
4377c478bd9Sstevel@tonic-gate 	 *	if it is concurrently coming into existence, but that's
4387c478bd9Sstevel@tonic-gate 	 *	really not much different from the thread running a bit slower.
4397c478bd9Sstevel@tonic-gate 	 *	If unmounting, we may get into traverse() when we shouldn't,
4407c478bd9Sstevel@tonic-gate 	 *	but traverse() will catch this case for us.
4417c478bd9Sstevel@tonic-gate 	 *	(For this to work, fetching v_vfsmountedhere had better
4427c478bd9Sstevel@tonic-gate 	 *	be atomic!)
4437c478bd9Sstevel@tonic-gate 	 */
4447c478bd9Sstevel@tonic-gate 	if (vn_mountedvfs(cvp) != NULL) {
4457c478bd9Sstevel@tonic-gate 		tvp = cvp;
4467c478bd9Sstevel@tonic-gate 		if ((error = traverse(&tvp)) != 0) {
4477c478bd9Sstevel@tonic-gate 			/*
4487c478bd9Sstevel@tonic-gate 			 * It is required to assign cvp here, because
4497c478bd9Sstevel@tonic-gate 			 * traverse() will return a held vnode which
4507c478bd9Sstevel@tonic-gate 			 * may different than the vnode that was passed
4517c478bd9Sstevel@tonic-gate 			 * in (even in the error case).  If traverse()
4527c478bd9Sstevel@tonic-gate 			 * changes the vnode it releases the original,
4537c478bd9Sstevel@tonic-gate 			 * and holds the new one.
4547c478bd9Sstevel@tonic-gate 			 */
4557c478bd9Sstevel@tonic-gate 			cvp = tvp;
4567c478bd9Sstevel@tonic-gate 			goto bad;
4577c478bd9Sstevel@tonic-gate 		}
4587c478bd9Sstevel@tonic-gate 		cvp = tvp;
4597c478bd9Sstevel@tonic-gate 	}
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 	/*
4627c478bd9Sstevel@tonic-gate 	 * If we hit a symbolic link and there is more path to be
4637c478bd9Sstevel@tonic-gate 	 * translated or this operation does not wish to apply
4647c478bd9Sstevel@tonic-gate 	 * to a link, then place the contents of the link at the
4657c478bd9Sstevel@tonic-gate 	 * front of the remaining pathname.
4667c478bd9Sstevel@tonic-gate 	 */
4677c478bd9Sstevel@tonic-gate 	if (cvp->v_type == VLNK && ((flags & FOLLOW) || pn_pathleft(pnp))) {
4687c478bd9Sstevel@tonic-gate 		struct pathname linkpath;
469005d3febSMarek Pospisil 		if (auditing) {
4707c478bd9Sstevel@tonic-gate 			if (error = audit_pathcomp(pnp, cvp, cr))
4717c478bd9Sstevel@tonic-gate 				goto bad;
4727c478bd9Sstevel@tonic-gate 		}
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate 		if (++nlink > MAXSYMLINKS) {
4757c478bd9Sstevel@tonic-gate 			error = ELOOP;
4767c478bd9Sstevel@tonic-gate 			goto bad;
4777c478bd9Sstevel@tonic-gate 		}
4787c478bd9Sstevel@tonic-gate 		pn_alloc(&linkpath);
4797c478bd9Sstevel@tonic-gate 		if (error = pn_getsymlink(cvp, &linkpath, cr)) {
4807c478bd9Sstevel@tonic-gate 			pn_free(&linkpath);
4817c478bd9Sstevel@tonic-gate 			goto bad;
4827c478bd9Sstevel@tonic-gate 		}
4837c478bd9Sstevel@tonic-gate 
484005d3febSMarek Pospisil 		if (auditing)
4857c478bd9Sstevel@tonic-gate 			audit_symlink(pnp, &linkpath);
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate 		if (pn_pathleft(&linkpath) == 0)
4887c478bd9Sstevel@tonic-gate 			(void) pn_set(&linkpath, ".");
4897c478bd9Sstevel@tonic-gate 		error = pn_insert(pnp, &linkpath, strlen(component));
4907c478bd9Sstevel@tonic-gate 		pn_free(&linkpath);
4917c478bd9Sstevel@tonic-gate 		if (error)
4927c478bd9Sstevel@tonic-gate 			goto bad;
4937c478bd9Sstevel@tonic-gate 		VN_RELE(cvp);
4947c478bd9Sstevel@tonic-gate 		cvp = NULL;
4957c478bd9Sstevel@tonic-gate 		if (pnp->pn_pathlen == 0) {
4967c478bd9Sstevel@tonic-gate 			error = ENOENT;
4977c478bd9Sstevel@tonic-gate 			goto bad;
4987c478bd9Sstevel@tonic-gate 		}
4997c478bd9Sstevel@tonic-gate 		if (pnp->pn_path[0] == '/') {
5007c478bd9Sstevel@tonic-gate 			do {
5017c478bd9Sstevel@tonic-gate 				pnp->pn_path++;
5027c478bd9Sstevel@tonic-gate 				pnp->pn_pathlen--;
5037c478bd9Sstevel@tonic-gate 			} while (pnp->pn_path[0] == '/');
5047c478bd9Sstevel@tonic-gate 			VN_RELE(vp);
5057c478bd9Sstevel@tonic-gate 			vp = rootvp;
5067c478bd9Sstevel@tonic-gate 			VN_HOLD(vp);
5077c478bd9Sstevel@tonic-gate 		}
508005d3febSMarek Pospisil 		if (auditing)
5097c478bd9Sstevel@tonic-gate 			audit_anchorpath(pnp, vp == rootvp);
5107c478bd9Sstevel@tonic-gate 		if (pn_fixslash(pnp)) {
5117c478bd9Sstevel@tonic-gate 			flags |= FOLLOW;
5127c478bd9Sstevel@tonic-gate 			must_be_directory = 1;
5137c478bd9Sstevel@tonic-gate 		}
5147c478bd9Sstevel@tonic-gate 		goto next;
5157c478bd9Sstevel@tonic-gate 	}
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate 	/*
5187c478bd9Sstevel@tonic-gate 	 * If rpnp is non-NULL, remember the resolved path name therein.
5197c478bd9Sstevel@tonic-gate 	 * Do not include "." components.  Collapse occurrences of
5207c478bd9Sstevel@tonic-gate 	 * "previous/..", so long as "previous" is not itself "..".
5217c478bd9Sstevel@tonic-gate 	 * Exhausting rpnp results in error ENAMETOOLONG.
5227c478bd9Sstevel@tonic-gate 	 */
5237c478bd9Sstevel@tonic-gate 	if (rpnp && strcmp(component, ".") != 0) {
5247c478bd9Sstevel@tonic-gate 		size_t len;
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate 		if (strcmp(component, "..") == 0 &&
5277c478bd9Sstevel@tonic-gate 		    rpnp->pn_pathlen != 0 &&
5287c478bd9Sstevel@tonic-gate 		    !((rpnp->pn_pathlen > 2 &&
5297c478bd9Sstevel@tonic-gate 		    strncmp(rpnp->pn_path+rpnp->pn_pathlen-3, "/..", 3) == 0) ||
5307c478bd9Sstevel@tonic-gate 		    (rpnp->pn_pathlen == 2 &&
5317c478bd9Sstevel@tonic-gate 		    strncmp(rpnp->pn_path, "..", 2) == 0))) {
5327c478bd9Sstevel@tonic-gate 			while (rpnp->pn_pathlen &&
5337c478bd9Sstevel@tonic-gate 			    rpnp->pn_path[rpnp->pn_pathlen-1] != '/')
5347c478bd9Sstevel@tonic-gate 				rpnp->pn_pathlen--;
5357c478bd9Sstevel@tonic-gate 			if (rpnp->pn_pathlen > 1)
5367c478bd9Sstevel@tonic-gate 				rpnp->pn_pathlen--;
5377c478bd9Sstevel@tonic-gate 			rpnp->pn_path[rpnp->pn_pathlen] = '\0';
5387c478bd9Sstevel@tonic-gate 		} else {
5397c478bd9Sstevel@tonic-gate 			if (rpnp->pn_pathlen != 0 &&
5407c478bd9Sstevel@tonic-gate 			    rpnp->pn_path[rpnp->pn_pathlen-1] != '/')
5417c478bd9Sstevel@tonic-gate 				rpnp->pn_path[rpnp->pn_pathlen++] = '/';
542da6c28aaSamw 			if (flags & FIGNORECASE) {
543da6c28aaSamw 				/*
544da6c28aaSamw 				 * Return the case-preserved name
545da6c28aaSamw 				 * within the resolved path.
546da6c28aaSamw 				 */
547ab04eb8eStimh 				error = copystr(pp->pn_buf,
548da6c28aaSamw 				    rpnp->pn_path + rpnp->pn_pathlen,
549da6c28aaSamw 				    rpnp->pn_bufsize - rpnp->pn_pathlen, &len);
550da6c28aaSamw 			} else {
5517c478bd9Sstevel@tonic-gate 				error = copystr(component,
5527c478bd9Sstevel@tonic-gate 				    rpnp->pn_path + rpnp->pn_pathlen,
5537c478bd9Sstevel@tonic-gate 				    rpnp->pn_bufsize - rpnp->pn_pathlen, &len);
554da6c28aaSamw 			}
5557c478bd9Sstevel@tonic-gate 			if (error)	/* copystr() returns ENAMETOOLONG */
5567c478bd9Sstevel@tonic-gate 				goto bad;
5577c478bd9Sstevel@tonic-gate 			rpnp->pn_pathlen += (len - 1);
5587c478bd9Sstevel@tonic-gate 			ASSERT(rpnp->pn_bufsize > rpnp->pn_pathlen);
5597c478bd9Sstevel@tonic-gate 		}
5607c478bd9Sstevel@tonic-gate 	}
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate 	/*
5637c478bd9Sstevel@tonic-gate 	 * If no more components, return last directory (if wanted) and
5647c478bd9Sstevel@tonic-gate 	 * last component (if wanted).
5657c478bd9Sstevel@tonic-gate 	 */
5667c478bd9Sstevel@tonic-gate 	if (pn_pathleft(pnp) == 0) {
5677c478bd9Sstevel@tonic-gate 		/*
5687c478bd9Sstevel@tonic-gate 		 * If there was a trailing slash in the pathname,
5697c478bd9Sstevel@tonic-gate 		 * make sure the last component is a directory.
5707c478bd9Sstevel@tonic-gate 		 */
5717c478bd9Sstevel@tonic-gate 		if (must_be_directory && cvp->v_type != VDIR) {
5727c478bd9Sstevel@tonic-gate 			error = ENOTDIR;
5737c478bd9Sstevel@tonic-gate 			goto bad;
5747c478bd9Sstevel@tonic-gate 		}
5757c478bd9Sstevel@tonic-gate 		if (dirvpp != NULL) {
5767c478bd9Sstevel@tonic-gate 			/*
5777c478bd9Sstevel@tonic-gate 			 * Check that we have the real parent and not
5787c478bd9Sstevel@tonic-gate 			 * an alias of the last component.
5797c478bd9Sstevel@tonic-gate 			 */
5807c478bd9Sstevel@tonic-gate 			if (vn_compare(vp, cvp)) {
581005d3febSMarek Pospisil 				if (auditing)
5827c478bd9Sstevel@tonic-gate 					(void) audit_savepath(pnp, cvp,
5837c478bd9Sstevel@tonic-gate 					    EINVAL, cr);
5847c478bd9Sstevel@tonic-gate 				pn_setlast(pnp);
5857c478bd9Sstevel@tonic-gate 				VN_RELE(vp);
5867c478bd9Sstevel@tonic-gate 				VN_RELE(cvp);
5877c478bd9Sstevel@tonic-gate 				if (rootvp != rootdir)
5887c478bd9Sstevel@tonic-gate 					VN_RELE(rootvp);
589da6c28aaSamw 				if (pp)
590da6c28aaSamw 					pn_free(pp);
5917c478bd9Sstevel@tonic-gate 				return (EINVAL);
5927c478bd9Sstevel@tonic-gate 			}
593005d3febSMarek Pospisil 			if (auditing) {
5947c478bd9Sstevel@tonic-gate 				if (error = audit_pathcomp(pnp, vp, cr))
5957c478bd9Sstevel@tonic-gate 					goto bad;
5967c478bd9Sstevel@tonic-gate 			}
5977c478bd9Sstevel@tonic-gate 			*dirvpp = vp;
5987c478bd9Sstevel@tonic-gate 		} else
5997c478bd9Sstevel@tonic-gate 			VN_RELE(vp);
600005d3febSMarek Pospisil 		if (auditing)
6017c478bd9Sstevel@tonic-gate 			(void) audit_savepath(pnp, cvp, 0, cr);
6027c478bd9Sstevel@tonic-gate 		if (pnp->pn_path == pnp->pn_buf)
6037c478bd9Sstevel@tonic-gate 			(void) pn_set(pnp, ".");
6047c478bd9Sstevel@tonic-gate 		else
6057c478bd9Sstevel@tonic-gate 			pn_setlast(pnp);
6067c478bd9Sstevel@tonic-gate 		if (rpnp) {
6077c478bd9Sstevel@tonic-gate 			if (VN_CMP(cvp, rootvp))
6087c478bd9Sstevel@tonic-gate 				(void) pn_set(rpnp, "/");
6097c478bd9Sstevel@tonic-gate 			else if (rpnp->pn_pathlen == 0)
6107c478bd9Sstevel@tonic-gate 				(void) pn_set(rpnp, ".");
6117c478bd9Sstevel@tonic-gate 		}
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate 		if (compvpp != NULL)
6147c478bd9Sstevel@tonic-gate 			*compvpp = cvp;
6157c478bd9Sstevel@tonic-gate 		else
6167c478bd9Sstevel@tonic-gate 			VN_RELE(cvp);
6177c478bd9Sstevel@tonic-gate 		if (rootvp != rootdir)
6187c478bd9Sstevel@tonic-gate 			VN_RELE(rootvp);
619da6c28aaSamw 		if (pp)
620da6c28aaSamw 			pn_free(pp);
6217c478bd9Sstevel@tonic-gate 		return (0);
6227c478bd9Sstevel@tonic-gate 	}
6237c478bd9Sstevel@tonic-gate 
624005d3febSMarek Pospisil 	if (auditing) {
6257c478bd9Sstevel@tonic-gate 		if (error = audit_pathcomp(pnp, cvp, cr))
6267c478bd9Sstevel@tonic-gate 			goto bad;
6277c478bd9Sstevel@tonic-gate 	}
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate 	/*
6307c478bd9Sstevel@tonic-gate 	 * Skip over slashes from end of last component.
6317c478bd9Sstevel@tonic-gate 	 */
6327c478bd9Sstevel@tonic-gate 	while (pnp->pn_path[0] == '/') {
6337c478bd9Sstevel@tonic-gate 		pnp->pn_path++;
6347c478bd9Sstevel@tonic-gate 		pnp->pn_pathlen--;
6357c478bd9Sstevel@tonic-gate 	}
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate 	/*
6387c478bd9Sstevel@tonic-gate 	 * Searched through another level of directory:
6397c478bd9Sstevel@tonic-gate 	 * release previous directory handle and save new (result
6407c478bd9Sstevel@tonic-gate 	 * of lookup) as current directory.
6417c478bd9Sstevel@tonic-gate 	 */
6427c478bd9Sstevel@tonic-gate 	VN_RELE(vp);
6437c478bd9Sstevel@tonic-gate 	vp = cvp;
6447c478bd9Sstevel@tonic-gate 	cvp = NULL;
6457c478bd9Sstevel@tonic-gate 	goto next;
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate bad:
648005d3febSMarek Pospisil 	if (auditing)	/* reached end of path */
6497c478bd9Sstevel@tonic-gate 		(void) audit_savepath(pnp, cvp, error, cr);
6507c478bd9Sstevel@tonic-gate bad_noaudit:
6517c478bd9Sstevel@tonic-gate 	/*
6527c478bd9Sstevel@tonic-gate 	 * Error.  Release vnodes and return.
6537c478bd9Sstevel@tonic-gate 	 */
6547c478bd9Sstevel@tonic-gate 	if (cvp)
6557c478bd9Sstevel@tonic-gate 		VN_RELE(cvp);
6567c478bd9Sstevel@tonic-gate 	/*
6577c478bd9Sstevel@tonic-gate 	 * If the error was ESTALE and the current directory to look in
6587c478bd9Sstevel@tonic-gate 	 * was the root for this lookup, the root for a mounted file
6597c478bd9Sstevel@tonic-gate 	 * system, or the starting directory for lookups, then
6607c478bd9Sstevel@tonic-gate 	 * return ENOENT instead of ESTALE.  In this case, no recovery
6617c478bd9Sstevel@tonic-gate 	 * is possible by the higher level.  If ESTALE was returned for
6627c478bd9Sstevel@tonic-gate 	 * some intermediate directory along the path, then recovery
6637c478bd9Sstevel@tonic-gate 	 * is potentially possible and retrying from the higher level
6647c478bd9Sstevel@tonic-gate 	 * will either correct the situation by purging stale cache
6657c478bd9Sstevel@tonic-gate 	 * entries or eventually get back to the point where no recovery
6667c478bd9Sstevel@tonic-gate 	 * is possible.
6677c478bd9Sstevel@tonic-gate 	 */
6687c478bd9Sstevel@tonic-gate 	if (error == ESTALE &&
6697c478bd9Sstevel@tonic-gate 	    (VN_CMP(vp, rootvp) || (vp->v_flag & VROOT) || vp == startvp))
6707c478bd9Sstevel@tonic-gate 		error = ENOENT;
6717c478bd9Sstevel@tonic-gate 	VN_RELE(vp);
6727c478bd9Sstevel@tonic-gate 	if (rootvp != rootdir)
6737c478bd9Sstevel@tonic-gate 		VN_RELE(rootvp);
674da6c28aaSamw 	if (pp)
675da6c28aaSamw 		pn_free(pp);
6767c478bd9Sstevel@tonic-gate 	return (error);
6777c478bd9Sstevel@tonic-gate }
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate /*
6807c478bd9Sstevel@tonic-gate  * Traverse a mount point.  Routine accepts a vnode pointer as a reference
6817c478bd9Sstevel@tonic-gate  * parameter and performs the indirection, releasing the original vnode.
6827c478bd9Sstevel@tonic-gate  */
6837c478bd9Sstevel@tonic-gate int
6847c478bd9Sstevel@tonic-gate traverse(vnode_t **cvpp)
6857c478bd9Sstevel@tonic-gate {
6867c478bd9Sstevel@tonic-gate 	int error = 0;
6877c478bd9Sstevel@tonic-gate 	vnode_t *cvp;
6887c478bd9Sstevel@tonic-gate 	vnode_t *tvp;
6897c478bd9Sstevel@tonic-gate 	vfs_t *vfsp;
6907c478bd9Sstevel@tonic-gate 
6917c478bd9Sstevel@tonic-gate 	cvp = *cvpp;
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate 	/*
6947c478bd9Sstevel@tonic-gate 	 * If this vnode is mounted on, then we transparently indirect
6957c478bd9Sstevel@tonic-gate 	 * to the vnode which is the root of the mounted file system.
6967c478bd9Sstevel@tonic-gate 	 * Before we do this we must check that an unmount is not in
6977c478bd9Sstevel@tonic-gate 	 * progress on this vnode.
6987c478bd9Sstevel@tonic-gate 	 */
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate 	for (;;) {
7017c478bd9Sstevel@tonic-gate 		/*
7027c478bd9Sstevel@tonic-gate 		 * Try to read lock the vnode.  If this fails because
7037c478bd9Sstevel@tonic-gate 		 * the vnode is already write locked, then check to
7047c478bd9Sstevel@tonic-gate 		 * see whether it is the current thread which locked
7057c478bd9Sstevel@tonic-gate 		 * the vnode.  If it is not, then read lock the vnode
7067c478bd9Sstevel@tonic-gate 		 * by waiting to acquire the lock.
7077c478bd9Sstevel@tonic-gate 		 *
7087c478bd9Sstevel@tonic-gate 		 * The code path in domount() is an example of support
7097c478bd9Sstevel@tonic-gate 		 * which needs to look up two pathnames and locks one
7107c478bd9Sstevel@tonic-gate 		 * of them in between the two lookups.
7117c478bd9Sstevel@tonic-gate 		 */
7127c478bd9Sstevel@tonic-gate 		error = vn_vfsrlock(cvp);
7137c478bd9Sstevel@tonic-gate 		if (error) {
7147c478bd9Sstevel@tonic-gate 			if (!vn_vfswlock_held(cvp))
7157c478bd9Sstevel@tonic-gate 				error = vn_vfsrlock_wait(cvp);
7167c478bd9Sstevel@tonic-gate 			if (error != 0) {
7177c478bd9Sstevel@tonic-gate 				/*
7187c478bd9Sstevel@tonic-gate 				 * lookuppn() expects a held vnode to be
7197c478bd9Sstevel@tonic-gate 				 * returned because it promptly calls
7207c478bd9Sstevel@tonic-gate 				 * VN_RELE after the error return
7217c478bd9Sstevel@tonic-gate 				 */
7227c478bd9Sstevel@tonic-gate 				*cvpp = cvp;
7237c478bd9Sstevel@tonic-gate 				return (error);
7247c478bd9Sstevel@tonic-gate 			}
7257c478bd9Sstevel@tonic-gate 		}
7267c478bd9Sstevel@tonic-gate 
7277c478bd9Sstevel@tonic-gate 		/*
7287c478bd9Sstevel@tonic-gate 		 * Reached the end of the mount chain?
7297c478bd9Sstevel@tonic-gate 		 */
7307c478bd9Sstevel@tonic-gate 		vfsp = vn_mountedvfs(cvp);
7317c478bd9Sstevel@tonic-gate 		if (vfsp == NULL) {
7327c478bd9Sstevel@tonic-gate 			vn_vfsunlock(cvp);
7337c478bd9Sstevel@tonic-gate 			break;
7347c478bd9Sstevel@tonic-gate 		}
7357c478bd9Sstevel@tonic-gate 
7367c478bd9Sstevel@tonic-gate 		/*
7377c478bd9Sstevel@tonic-gate 		 * The read lock must be held across the call to VFS_ROOT() to
7387c478bd9Sstevel@tonic-gate 		 * prevent a concurrent unmount from destroying the vfs.
7397c478bd9Sstevel@tonic-gate 		 */
7407c478bd9Sstevel@tonic-gate 		error = VFS_ROOT(vfsp, &tvp);
7417c478bd9Sstevel@tonic-gate 		vn_vfsunlock(cvp);
7427c478bd9Sstevel@tonic-gate 
7437c478bd9Sstevel@tonic-gate 		if (error)
7447c478bd9Sstevel@tonic-gate 			break;
7457c478bd9Sstevel@tonic-gate 
7467c478bd9Sstevel@tonic-gate 		VN_RELE(cvp);
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate 		cvp = tvp;
7497c478bd9Sstevel@tonic-gate 	}
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate 	*cvpp = cvp;
7527c478bd9Sstevel@tonic-gate 	return (error);
7537c478bd9Sstevel@tonic-gate }
7547c478bd9Sstevel@tonic-gate 
7557c478bd9Sstevel@tonic-gate /*
7567c478bd9Sstevel@tonic-gate  * Return the lowermost vnode if this is a mountpoint.
7577c478bd9Sstevel@tonic-gate  */
7587c478bd9Sstevel@tonic-gate static vnode_t *
7597c478bd9Sstevel@tonic-gate vn_under(vnode_t *vp)
7607c478bd9Sstevel@tonic-gate {
7617c478bd9Sstevel@tonic-gate 	vnode_t *uvp;
7627c478bd9Sstevel@tonic-gate 	vfs_t *vfsp;
7637c478bd9Sstevel@tonic-gate 
7647c478bd9Sstevel@tonic-gate 	while (vp->v_flag & VROOT) {
7657c478bd9Sstevel@tonic-gate 
7667c478bd9Sstevel@tonic-gate 		vfsp = vp->v_vfsp;
7677c478bd9Sstevel@tonic-gate 		vfs_rlock_wait(vfsp);
7687c478bd9Sstevel@tonic-gate 		if ((uvp = vfsp->vfs_vnodecovered) == NULL ||
7697c478bd9Sstevel@tonic-gate 		    (vfsp->vfs_flag & VFS_UNMOUNTED)) {
7707c478bd9Sstevel@tonic-gate 			vfs_unlock(vfsp);
7717c478bd9Sstevel@tonic-gate 			break;
7727c478bd9Sstevel@tonic-gate 		}
7737c478bd9Sstevel@tonic-gate 		VN_HOLD(uvp);
7747c478bd9Sstevel@tonic-gate 		vfs_unlock(vfsp);
7757c478bd9Sstevel@tonic-gate 		VN_RELE(vp);
7767c478bd9Sstevel@tonic-gate 		vp = uvp;
7777c478bd9Sstevel@tonic-gate 	}
7787c478bd9Sstevel@tonic-gate 
7797c478bd9Sstevel@tonic-gate 	return (vp);
7807c478bd9Sstevel@tonic-gate }
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate static int
7837c478bd9Sstevel@tonic-gate vnode_match(vnode_t *v1, vnode_t *v2, cred_t *cr)
7847c478bd9Sstevel@tonic-gate {
7857c478bd9Sstevel@tonic-gate 	vattr_t	v1attr, v2attr;
7867c478bd9Sstevel@tonic-gate 
7877c478bd9Sstevel@tonic-gate 	/*
7887c478bd9Sstevel@tonic-gate 	 * If we have a device file, check to see if is a cloned open of the
7897c478bd9Sstevel@tonic-gate 	 * same device.  For self-cloning devices, the major numbers will match.
7907c478bd9Sstevel@tonic-gate 	 * For devices cloned through the 'clone' driver, the minor number of
7917c478bd9Sstevel@tonic-gate 	 * the source device will be the same as the major number of the cloned
7927c478bd9Sstevel@tonic-gate 	 * device.
7937c478bd9Sstevel@tonic-gate 	 */
7947c478bd9Sstevel@tonic-gate 	if ((v1->v_type == VCHR || v1->v_type == VBLK) &&
7957c478bd9Sstevel@tonic-gate 	    v1->v_type == v2->v_type) {
7967c478bd9Sstevel@tonic-gate 		if ((spec_is_selfclone(v1) || spec_is_selfclone(v2)) &&
7977c478bd9Sstevel@tonic-gate 		    getmajor(v1->v_rdev) == getmajor(v2->v_rdev))
7987c478bd9Sstevel@tonic-gate 			return (1);
7997c478bd9Sstevel@tonic-gate 
8007c478bd9Sstevel@tonic-gate 		if (spec_is_clone(v1) &&
8017c478bd9Sstevel@tonic-gate 		    getmajor(v1->v_rdev) == getminor(v2->v_rdev))
8027c478bd9Sstevel@tonic-gate 			return (1);
8037c478bd9Sstevel@tonic-gate 
8047c478bd9Sstevel@tonic-gate 		if (spec_is_clone(v2) &&
8057c478bd9Sstevel@tonic-gate 		    getmajor(v2->v_rdev) == getminor(v1->v_rdev))
8067c478bd9Sstevel@tonic-gate 			return (1);
8077c478bd9Sstevel@tonic-gate 	}
8087c478bd9Sstevel@tonic-gate 
8097c478bd9Sstevel@tonic-gate 	v1attr.va_mask = v2attr.va_mask = AT_TYPE;
8107c478bd9Sstevel@tonic-gate 
8117c478bd9Sstevel@tonic-gate 	/*
8127c478bd9Sstevel@tonic-gate 	 * This check for symbolic links handles the pseudo-symlinks in procfs.
8137c478bd9Sstevel@tonic-gate 	 * These particular links have v_type of VDIR, but the attributes have a
8147c478bd9Sstevel@tonic-gate 	 * type of VLNK.  We need to avoid these links because otherwise if we
8157c478bd9Sstevel@tonic-gate 	 * are currently in '/proc/self/fd', then '/proc/self/cwd' will compare
8167c478bd9Sstevel@tonic-gate 	 * as the same vnode.
8177c478bd9Sstevel@tonic-gate 	 */
818da6c28aaSamw 	if (VOP_GETATTR(v1, &v1attr, 0, cr, NULL) != 0 ||
819da6c28aaSamw 	    VOP_GETATTR(v2, &v2attr, 0, cr, NULL) != 0 ||
8207c478bd9Sstevel@tonic-gate 	    v1attr.va_type == VLNK || v2attr.va_type == VLNK)
8217c478bd9Sstevel@tonic-gate 		return (0);
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate 	v1attr.va_mask = v2attr.va_mask = AT_TYPE | AT_FSID | AT_NODEID;
8247c478bd9Sstevel@tonic-gate 
825da6c28aaSamw 	if (VOP_GETATTR(v1, &v1attr, ATTR_REAL, cr, NULL) != 0 ||
826da6c28aaSamw 	    VOP_GETATTR(v2, &v2attr, ATTR_REAL, cr, NULL) != 0)
8277c478bd9Sstevel@tonic-gate 		return (0);
8287c478bd9Sstevel@tonic-gate 
8297c478bd9Sstevel@tonic-gate 	return (v1attr.va_fsid == v2attr.va_fsid &&
8307c478bd9Sstevel@tonic-gate 	    v1attr.va_nodeid == v2attr.va_nodeid);
8317c478bd9Sstevel@tonic-gate }
8327c478bd9Sstevel@tonic-gate 
8337c478bd9Sstevel@tonic-gate 
8347c478bd9Sstevel@tonic-gate /*
8357c478bd9Sstevel@tonic-gate  * Find the entry in the directory corresponding to the target vnode.
8367c478bd9Sstevel@tonic-gate  */
8377c478bd9Sstevel@tonic-gate int
8387c478bd9Sstevel@tonic-gate dirfindvp(vnode_t *vrootp, vnode_t *dvp, vnode_t *tvp, cred_t *cr, char *dbuf,
8397c478bd9Sstevel@tonic-gate     size_t dlen, dirent64_t **rdp)
8407c478bd9Sstevel@tonic-gate {
8417c478bd9Sstevel@tonic-gate 	size_t dbuflen;
8427c478bd9Sstevel@tonic-gate 	struct iovec iov;
8437c478bd9Sstevel@tonic-gate 	struct uio uio;
844fa9e4066Sahrens 	int error;
8457c478bd9Sstevel@tonic-gate 	int eof;
8467c478bd9Sstevel@tonic-gate 	vnode_t *cmpvp;
8477c478bd9Sstevel@tonic-gate 	struct dirent64 *dp;
8487c478bd9Sstevel@tonic-gate 	pathname_t pnp;
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate 	ASSERT(dvp->v_type == VDIR);
8517c478bd9Sstevel@tonic-gate 
8527c478bd9Sstevel@tonic-gate 	/*
8537c478bd9Sstevel@tonic-gate 	 * This is necessary because of the strange semantics of VOP_LOOKUP().
8547c478bd9Sstevel@tonic-gate 	 */
8557c478bd9Sstevel@tonic-gate 	bzero(&pnp, sizeof (pnp));
8567c478bd9Sstevel@tonic-gate 
8577c478bd9Sstevel@tonic-gate 	eof = 0;
8587c478bd9Sstevel@tonic-gate 
8597c478bd9Sstevel@tonic-gate 	uio.uio_iov = &iov;
8607c478bd9Sstevel@tonic-gate 	uio.uio_iovcnt = 1;
8617c478bd9Sstevel@tonic-gate 	uio.uio_segflg = UIO_SYSSPACE;
8627c478bd9Sstevel@tonic-gate 	uio.uio_fmode = 0;
8637c478bd9Sstevel@tonic-gate 	uio.uio_extflg = UIO_COPY_CACHED;
8647c478bd9Sstevel@tonic-gate 	uio.uio_loffset = 0;
8657c478bd9Sstevel@tonic-gate 
866da6c28aaSamw 	if ((error = VOP_ACCESS(dvp, VREAD, 0, cr, NULL)) != 0)
867fa9e4066Sahrens 		return (error);
8687c478bd9Sstevel@tonic-gate 
8697c478bd9Sstevel@tonic-gate 	while (!eof) {
8707c478bd9Sstevel@tonic-gate 		uio.uio_resid = dlen;
8717c478bd9Sstevel@tonic-gate 		iov.iov_base = dbuf;
8727c478bd9Sstevel@tonic-gate 		iov.iov_len = dlen;
8737c478bd9Sstevel@tonic-gate 
8747c478bd9Sstevel@tonic-gate 		(void) VOP_RWLOCK(dvp, V_WRITELOCK_FALSE, NULL);
875da6c28aaSamw 		error = VOP_READDIR(dvp, &uio, cr, &eof, NULL, 0);
8767c478bd9Sstevel@tonic-gate 		VOP_RWUNLOCK(dvp, V_WRITELOCK_FALSE, NULL);
8777c478bd9Sstevel@tonic-gate 
8787c478bd9Sstevel@tonic-gate 		dbuflen = dlen - uio.uio_resid;
8797c478bd9Sstevel@tonic-gate 
880fa9e4066Sahrens 		if (error || dbuflen == 0)
8817c478bd9Sstevel@tonic-gate 			break;
8827c478bd9Sstevel@tonic-gate 
8837c478bd9Sstevel@tonic-gate 		dp = (dirent64_t *)dbuf;
8847c478bd9Sstevel@tonic-gate 		while ((intptr_t)dp < (intptr_t)dbuf + dbuflen) {
8857c478bd9Sstevel@tonic-gate 			/*
8867c478bd9Sstevel@tonic-gate 			 * Ignore '.' and '..' entries
8877c478bd9Sstevel@tonic-gate 			 */
8887c478bd9Sstevel@tonic-gate 			if (strcmp(dp->d_name, ".") == 0 ||
8897c478bd9Sstevel@tonic-gate 			    strcmp(dp->d_name, "..") == 0) {
8907c478bd9Sstevel@tonic-gate 				dp = (dirent64_t *)((intptr_t)dp +
8917c478bd9Sstevel@tonic-gate 				    dp->d_reclen);
8927c478bd9Sstevel@tonic-gate 				continue;
8937c478bd9Sstevel@tonic-gate 			}
8947c478bd9Sstevel@tonic-gate 
895fa9e4066Sahrens 			error = VOP_LOOKUP(dvp, dp->d_name, &cmpvp, &pnp, 0,
896da6c28aaSamw 			    vrootp, cr, NULL, NULL, NULL);
8977c478bd9Sstevel@tonic-gate 
8987c478bd9Sstevel@tonic-gate 			/*
8997c478bd9Sstevel@tonic-gate 			 * We only want to bail out if there was an error other
9007c478bd9Sstevel@tonic-gate 			 * than ENOENT.  Otherwise, it could be that someone
9017c478bd9Sstevel@tonic-gate 			 * just removed an entry since the readdir() call, and
9027c478bd9Sstevel@tonic-gate 			 * the entry we want is further on in the directory.
9037c478bd9Sstevel@tonic-gate 			 */
904fa9e4066Sahrens 			if (error == 0) {
9057c478bd9Sstevel@tonic-gate 				if (vnode_match(tvp, cmpvp, cr)) {
9067c478bd9Sstevel@tonic-gate 					VN_RELE(cmpvp);
9077c478bd9Sstevel@tonic-gate 					*rdp = dp;
9087c478bd9Sstevel@tonic-gate 					return (0);
9097c478bd9Sstevel@tonic-gate 				}
9107c478bd9Sstevel@tonic-gate 
9117c478bd9Sstevel@tonic-gate 				VN_RELE(cmpvp);
912fa9e4066Sahrens 			} else if (error != ENOENT) {
913fa9e4066Sahrens 				return (error);
9147c478bd9Sstevel@tonic-gate 			}
9157c478bd9Sstevel@tonic-gate 
9167c478bd9Sstevel@tonic-gate 			dp = (dirent64_t *)((intptr_t)dp + dp->d_reclen);
9177c478bd9Sstevel@tonic-gate 		}
9187c478bd9Sstevel@tonic-gate 	}
9197c478bd9Sstevel@tonic-gate 
9207c478bd9Sstevel@tonic-gate 	/*
9217c478bd9Sstevel@tonic-gate 	 * Something strange has happened, this directory does not contain the
9227c478bd9Sstevel@tonic-gate 	 * specified vnode.  This should never happen in the normal case, since
923fa9e4066Sahrens 	 * we ensured that dvp is the parent of vp.  This is possible in some
924fa9e4066Sahrens 	 * rare conditions (races and the special .zfs directory).
9257c478bd9Sstevel@tonic-gate 	 */
926fa9e4066Sahrens 	if (error == 0) {
927da6c28aaSamw 		error = VOP_LOOKUP(dvp, ".zfs", &cmpvp, &pnp, 0, vrootp, cr,
928da6c28aaSamw 		    NULL, NULL, NULL);
929fa9e4066Sahrens 		if (error == 0) {
930fa9e4066Sahrens 			if (vnode_match(tvp, cmpvp, cr)) {
931fa9e4066Sahrens 				(void) strcpy(dp->d_name, ".zfs");
932fa9e4066Sahrens 				dp->d_reclen = strlen(".zfs");
933fa9e4066Sahrens 				dp->d_off = 2;
934fa9e4066Sahrens 				dp->d_ino = 1;
935fa9e4066Sahrens 				*rdp = dp;
936fa9e4066Sahrens 			} else {
937fa9e4066Sahrens 				error = ENOENT;
938fa9e4066Sahrens 			}
939fa9e4066Sahrens 			VN_RELE(cmpvp);
940fa9e4066Sahrens 		}
941fa9e4066Sahrens 	}
9427c478bd9Sstevel@tonic-gate 
943fa9e4066Sahrens 	return (error);
9447c478bd9Sstevel@tonic-gate }
9457c478bd9Sstevel@tonic-gate 
9467c478bd9Sstevel@tonic-gate /*
9477c478bd9Sstevel@tonic-gate  * Given a global path (from rootdir), and a vnode that is the current root,
9487c478bd9Sstevel@tonic-gate  * return the portion of the path that is beneath the current root or NULL on
9497c478bd9Sstevel@tonic-gate  * failure.  The path MUST be a resolved path (no '..' entries or symlinks),
9507c478bd9Sstevel@tonic-gate  * otherwise this function will fail.
9517c478bd9Sstevel@tonic-gate  */
9527c478bd9Sstevel@tonic-gate static char *
9537c478bd9Sstevel@tonic-gate localpath(char *path, struct vnode *vrootp, cred_t *cr)
9547c478bd9Sstevel@tonic-gate {
9557c478bd9Sstevel@tonic-gate 	vnode_t *vp;
9567c478bd9Sstevel@tonic-gate 	vnode_t *cvp;
9577c478bd9Sstevel@tonic-gate 	char component[MAXNAMELEN];
9587c478bd9Sstevel@tonic-gate 	char *ret = NULL;
9597c478bd9Sstevel@tonic-gate 	pathname_t pn;
9607c478bd9Sstevel@tonic-gate 
9617c478bd9Sstevel@tonic-gate 	/*
9627c478bd9Sstevel@tonic-gate 	 * We use vn_compare() instead of VN_CMP() in order to detect lofs
9637c478bd9Sstevel@tonic-gate 	 * mounts and stacked vnodes.
9647c478bd9Sstevel@tonic-gate 	 */
9657c478bd9Sstevel@tonic-gate 	if (vn_compare(vrootp, rootdir))
9667c478bd9Sstevel@tonic-gate 		return (path);
9677c478bd9Sstevel@tonic-gate 
9687c478bd9Sstevel@tonic-gate 	if (pn_get(path, UIO_SYSSPACE, &pn) != 0)
9697c478bd9Sstevel@tonic-gate 		return (NULL);
9707c478bd9Sstevel@tonic-gate 
9717c478bd9Sstevel@tonic-gate 	vp = rootdir;
9727c478bd9Sstevel@tonic-gate 	VN_HOLD(vp);
9737c478bd9Sstevel@tonic-gate 
9745fd6860bSMilan Cermak 	if (vn_ismntpt(vp) && traverse(&vp) != 0) {
9755fd6860bSMilan Cermak 		VN_RELE(vp);
9765fd6860bSMilan Cermak 		pn_free(&pn);
9775fd6860bSMilan Cermak 		return (NULL);
9785fd6860bSMilan Cermak 	}
9795fd6860bSMilan Cermak 
9807c478bd9Sstevel@tonic-gate 	while (pn_pathleft(&pn)) {
9817c478bd9Sstevel@tonic-gate 		pn_skipslash(&pn);
9827c478bd9Sstevel@tonic-gate 
9837c478bd9Sstevel@tonic-gate 		if (pn_getcomponent(&pn, component) != 0)
9847c478bd9Sstevel@tonic-gate 			break;
9857c478bd9Sstevel@tonic-gate 
986da6c28aaSamw 		if (VOP_LOOKUP(vp, component, &cvp, &pn, 0, rootdir, cr,
987da6c28aaSamw 		    NULL, NULL, NULL) != 0)
9887c478bd9Sstevel@tonic-gate 			break;
9897c478bd9Sstevel@tonic-gate 		VN_RELE(vp);
9907c478bd9Sstevel@tonic-gate 		vp = cvp;
9917c478bd9Sstevel@tonic-gate 
9925fd6860bSMilan Cermak 		if (vn_ismntpt(vp) && traverse(&vp) != 0)
9935fd6860bSMilan Cermak 			break;
9945fd6860bSMilan Cermak 
9957c478bd9Sstevel@tonic-gate 		if (vn_compare(vp, vrootp)) {
9967c478bd9Sstevel@tonic-gate 			ret = path + (pn.pn_path - pn.pn_buf);
9977c478bd9Sstevel@tonic-gate 			break;
9987c478bd9Sstevel@tonic-gate 		}
9997c478bd9Sstevel@tonic-gate 	}
10007c478bd9Sstevel@tonic-gate 
10017c478bd9Sstevel@tonic-gate 	VN_RELE(vp);
10027c478bd9Sstevel@tonic-gate 	pn_free(&pn);
10037c478bd9Sstevel@tonic-gate 
10047c478bd9Sstevel@tonic-gate 	return (ret);
10057c478bd9Sstevel@tonic-gate }
10067c478bd9Sstevel@tonic-gate 
10077c478bd9Sstevel@tonic-gate /*
10087c478bd9Sstevel@tonic-gate  * Given a directory, return the full, resolved path.  This looks up "..",
10097c478bd9Sstevel@tonic-gate  * searches for the given vnode in the parent, appends the component, etc.  It
1010*2f5706a1SWilliam.Johnston  * is used to implement vnodetopath() and getcwd() when the cached path fails.
10117c478bd9Sstevel@tonic-gate  */
10127c478bd9Sstevel@tonic-gate static int
10137b2df5ebSMilan Cermak dirtopath(vnode_t *vrootp, vnode_t *vp, char *buf, size_t buflen, int flags,
10147b2df5ebSMilan Cermak     cred_t *cr)
10157c478bd9Sstevel@tonic-gate {
10167c478bd9Sstevel@tonic-gate 	pathname_t pn, rpn, emptypn;
10177c478bd9Sstevel@tonic-gate 	vnode_t *cmpvp, *pvp = NULL;
10187c478bd9Sstevel@tonic-gate 	vnode_t *startvp = vp;
10197b2df5ebSMilan Cermak 	int err = 0, vprivs;
10207c478bd9Sstevel@tonic-gate 	size_t complen;
10217c478bd9Sstevel@tonic-gate 	char *dbuf;
10227c478bd9Sstevel@tonic-gate 	dirent64_t *dp;
10237c478bd9Sstevel@tonic-gate 	char		*bufloc;
10247c478bd9Sstevel@tonic-gate 	size_t		dlen = DIRENT64_RECLEN(MAXPATHLEN);
10257c478bd9Sstevel@tonic-gate 	refstr_t	*mntpt;
10267c478bd9Sstevel@tonic-gate 
10277c478bd9Sstevel@tonic-gate 	/* Operation only allowed on directories */
10287c478bd9Sstevel@tonic-gate 	ASSERT(vp->v_type == VDIR);
10297c478bd9Sstevel@tonic-gate 
10307c478bd9Sstevel@tonic-gate 	/* We must have at least enough space for "/" */
10317c478bd9Sstevel@tonic-gate 	if (buflen < 2)
10327c478bd9Sstevel@tonic-gate 		return (ENAMETOOLONG);
10337c478bd9Sstevel@tonic-gate 
10347c478bd9Sstevel@tonic-gate 	/* Start at end of string with terminating null */
10357c478bd9Sstevel@tonic-gate 	bufloc = &buf[buflen - 1];
10367c478bd9Sstevel@tonic-gate 	*bufloc = '\0';
10377c478bd9Sstevel@tonic-gate 
10387c478bd9Sstevel@tonic-gate 	pn_alloc(&pn);
10397c478bd9Sstevel@tonic-gate 	pn_alloc(&rpn);
10407c478bd9Sstevel@tonic-gate 	dbuf = kmem_alloc(dlen, KM_SLEEP);
10417c478bd9Sstevel@tonic-gate 	bzero(&emptypn, sizeof (emptypn));
10427c478bd9Sstevel@tonic-gate 
10437c478bd9Sstevel@tonic-gate 	/*
10447c478bd9Sstevel@tonic-gate 	 * Begin with an additional reference on vp.  This will be decremented
10457c478bd9Sstevel@tonic-gate 	 * during the loop.
10467c478bd9Sstevel@tonic-gate 	 */
10477c478bd9Sstevel@tonic-gate 	VN_HOLD(vp);
10487c478bd9Sstevel@tonic-gate 
10497c478bd9Sstevel@tonic-gate 	for (;;) {
10507c478bd9Sstevel@tonic-gate 		/*
10517c478bd9Sstevel@tonic-gate 		 * Return if we've reached the root.  If the buffer is empty,
10527c478bd9Sstevel@tonic-gate 		 * return '/'.  We explicitly don't use vn_compare(), since it
10537c478bd9Sstevel@tonic-gate 		 * compares the real vnodes.  A lofs mount of '/' would produce
10547c478bd9Sstevel@tonic-gate 		 * incorrect results otherwise.
10557c478bd9Sstevel@tonic-gate 		 */
10567c478bd9Sstevel@tonic-gate 		if (VN_CMP(vrootp, vp)) {
10577c478bd9Sstevel@tonic-gate 			if (*bufloc == '\0')
10587c478bd9Sstevel@tonic-gate 				*--bufloc = '/';
10597c478bd9Sstevel@tonic-gate 			break;
10607c478bd9Sstevel@tonic-gate 		}
10617c478bd9Sstevel@tonic-gate 
10627c478bd9Sstevel@tonic-gate 		/*
10637c478bd9Sstevel@tonic-gate 		 * If we've reached the VFS root, something has gone wrong.  We
10647c478bd9Sstevel@tonic-gate 		 * should have reached the root in the above check.  The only
10657c478bd9Sstevel@tonic-gate 		 * explantation is that 'vp' is not contained withing the given
10667c478bd9Sstevel@tonic-gate 		 * root, in which case we return EPERM.
10677c478bd9Sstevel@tonic-gate 		 */
10687c478bd9Sstevel@tonic-gate 		if (VN_CMP(rootdir, vp)) {
10697c478bd9Sstevel@tonic-gate 			err = EPERM;
10707c478bd9Sstevel@tonic-gate 			goto out;
10717c478bd9Sstevel@tonic-gate 		}
10727c478bd9Sstevel@tonic-gate 
10737c478bd9Sstevel@tonic-gate 		/*
10747c478bd9Sstevel@tonic-gate 		 * Shortcut: see if this vnode is a mountpoint.  If so,
10757c478bd9Sstevel@tonic-gate 		 * grab the path information from the vfs_t.
10767c478bd9Sstevel@tonic-gate 		 */
10777c478bd9Sstevel@tonic-gate 		if (vp->v_flag & VROOT) {
10787c478bd9Sstevel@tonic-gate 
10797c478bd9Sstevel@tonic-gate 			mntpt = vfs_getmntpoint(vp->v_vfsp);
10807c478bd9Sstevel@tonic-gate 			if ((err = pn_set(&pn, (char *)refstr_value(mntpt)))
10817c478bd9Sstevel@tonic-gate 			    == 0) {
10827c478bd9Sstevel@tonic-gate 				refstr_rele(mntpt);
10837c478bd9Sstevel@tonic-gate 				rpn.pn_path = rpn.pn_buf;
10847c478bd9Sstevel@tonic-gate 
10857c478bd9Sstevel@tonic-gate 				/*
1086207983b5SMilan Cermak 				 * Ensure the mountpoint still exists.
10877c478bd9Sstevel@tonic-gate 				 */
10887c478bd9Sstevel@tonic-gate 				VN_HOLD(vrootp);
10897c478bd9Sstevel@tonic-gate 				if (vrootp != rootdir)
10907c478bd9Sstevel@tonic-gate 					VN_HOLD(vrootp);
10917b2df5ebSMilan Cermak 				if (lookuppnvp(&pn, &rpn, flags, NULL,
10927c478bd9Sstevel@tonic-gate 				    &cmpvp, vrootp, vrootp, cr) == 0) {
10937c478bd9Sstevel@tonic-gate 
10947c478bd9Sstevel@tonic-gate 					if (VN_CMP(vp, cmpvp)) {
10957c478bd9Sstevel@tonic-gate 						VN_RELE(cmpvp);
10967c478bd9Sstevel@tonic-gate 
10977c478bd9Sstevel@tonic-gate 						complen = strlen(rpn.pn_path);
10987c478bd9Sstevel@tonic-gate 						bufloc -= complen;
10997c478bd9Sstevel@tonic-gate 						if (bufloc < buf) {
11007c478bd9Sstevel@tonic-gate 							err = ERANGE;
11017c478bd9Sstevel@tonic-gate 							goto out;
11027c478bd9Sstevel@tonic-gate 						}
11037c478bd9Sstevel@tonic-gate 						bcopy(rpn.pn_path, bufloc,
11047c478bd9Sstevel@tonic-gate 						    complen);
11057c478bd9Sstevel@tonic-gate 						break;
11067c478bd9Sstevel@tonic-gate 					} else {
11077c478bd9Sstevel@tonic-gate 						VN_RELE(cmpvp);
11087c478bd9Sstevel@tonic-gate 					}
11097c478bd9Sstevel@tonic-gate 				}
11107c478bd9Sstevel@tonic-gate 			} else {
11117c478bd9Sstevel@tonic-gate 				refstr_rele(mntpt);
11127c478bd9Sstevel@tonic-gate 			}
11137c478bd9Sstevel@tonic-gate 		}
11147c478bd9Sstevel@tonic-gate 
11157c478bd9Sstevel@tonic-gate 		/*
1116207983b5SMilan Cermak 		 * Shortcut: see if this vnode has correct v_path. If so,
1117207983b5SMilan Cermak 		 * we have the work done.
1118207983b5SMilan Cermak 		 */
1119207983b5SMilan Cermak 		mutex_enter(&vp->v_lock);
1120207983b5SMilan Cermak 		if (vp->v_path != NULL) {
1121207983b5SMilan Cermak 
1122207983b5SMilan Cermak 			if ((err = pn_set(&pn, vp->v_path)) == 0) {
1123207983b5SMilan Cermak 				mutex_exit(&vp->v_lock);
1124207983b5SMilan Cermak 				rpn.pn_path = rpn.pn_buf;
1125207983b5SMilan Cermak 
1126207983b5SMilan Cermak 				/*
1127207983b5SMilan Cermak 				 * Ensure the v_path pointing to correct vnode
1128207983b5SMilan Cermak 				 */
1129207983b5SMilan Cermak 				VN_HOLD(vrootp);
1130207983b5SMilan Cermak 				if (vrootp != rootdir)
1131207983b5SMilan Cermak 					VN_HOLD(vrootp);
11327b2df5ebSMilan Cermak 				if (lookuppnvp(&pn, &rpn, flags, NULL,
1133207983b5SMilan Cermak 				    &cmpvp, vrootp, vrootp, cr) == 0) {
1134207983b5SMilan Cermak 
1135207983b5SMilan Cermak 					if (VN_CMP(vp, cmpvp)) {
1136207983b5SMilan Cermak 						VN_RELE(cmpvp);
1137207983b5SMilan Cermak 
1138207983b5SMilan Cermak 						complen = strlen(rpn.pn_path);
1139207983b5SMilan Cermak 						bufloc -= complen;
1140207983b5SMilan Cermak 						if (bufloc < buf) {
1141207983b5SMilan Cermak 							err = ERANGE;
1142207983b5SMilan Cermak 							goto out;
1143207983b5SMilan Cermak 						}
1144207983b5SMilan Cermak 						bcopy(rpn.pn_path, bufloc,
1145207983b5SMilan Cermak 						    complen);
1146207983b5SMilan Cermak 						break;
1147207983b5SMilan Cermak 					} else {
1148207983b5SMilan Cermak 						VN_RELE(cmpvp);
1149207983b5SMilan Cermak 					}
1150207983b5SMilan Cermak 				}
1151207983b5SMilan Cermak 			} else {
1152207983b5SMilan Cermak 				mutex_exit(&vp->v_lock);
1153207983b5SMilan Cermak 			}
1154207983b5SMilan Cermak 		} else {
1155207983b5SMilan Cermak 			mutex_exit(&vp->v_lock);
1156207983b5SMilan Cermak 		}
1157207983b5SMilan Cermak 
1158207983b5SMilan Cermak 		/*
11597c478bd9Sstevel@tonic-gate 		 * Shortcuts failed, search for this vnode in its parent.  If
11607c478bd9Sstevel@tonic-gate 		 * this is a mountpoint, then get the vnode underneath.
11617c478bd9Sstevel@tonic-gate 		 */
11627c478bd9Sstevel@tonic-gate 		if (vp->v_flag & VROOT)
11637c478bd9Sstevel@tonic-gate 			vp = vn_under(vp);
1164da6c28aaSamw 		if ((err = VOP_LOOKUP(vp, "..", &pvp, &emptypn, 0, vrootp, cr,
1165da6c28aaSamw 		    NULL, NULL, NULL)) != 0)
11667c478bd9Sstevel@tonic-gate 			goto out;
11677c478bd9Sstevel@tonic-gate 
11687c478bd9Sstevel@tonic-gate 		/*
11697c478bd9Sstevel@tonic-gate 		 * With extended attributes, it's possible for a directory to
11707c478bd9Sstevel@tonic-gate 		 * have a parent that is a regular file.  Check for that here.
11717c478bd9Sstevel@tonic-gate 		 */
11727c478bd9Sstevel@tonic-gate 		if (pvp->v_type != VDIR) {
11737c478bd9Sstevel@tonic-gate 			err = ENOTDIR;
11747c478bd9Sstevel@tonic-gate 			goto out;
11757c478bd9Sstevel@tonic-gate 		}
11767c478bd9Sstevel@tonic-gate 
11777c478bd9Sstevel@tonic-gate 		/*
11787c478bd9Sstevel@tonic-gate 		 * If this is true, something strange has happened.  This is
11797c478bd9Sstevel@tonic-gate 		 * only true if we are the root of a filesystem, which should
11807c478bd9Sstevel@tonic-gate 		 * have been caught by the check above.
11817c478bd9Sstevel@tonic-gate 		 */
11827c478bd9Sstevel@tonic-gate 		if (VN_CMP(pvp, vp)) {
11837c478bd9Sstevel@tonic-gate 			err = ENOENT;
11847c478bd9Sstevel@tonic-gate 			goto out;
11857c478bd9Sstevel@tonic-gate 		}
11867c478bd9Sstevel@tonic-gate 
11877c478bd9Sstevel@tonic-gate 		/*
11887b2df5ebSMilan Cermak 		 * Check if we have read and search privilege so, that
11897b2df5ebSMilan Cermak 		 * we can lookup the path in the directory
11907b2df5ebSMilan Cermak 		 */
11917b2df5ebSMilan Cermak 		vprivs = (flags & LOOKUP_CHECKREAD) ? VREAD | VEXEC : VEXEC;
11927b2df5ebSMilan Cermak 		if ((err = VOP_ACCESS(pvp, vprivs, 0, cr, NULL)) != 0) {
11937b2df5ebSMilan Cermak 			goto out;
11947b2df5ebSMilan Cermak 		}
11957b2df5ebSMilan Cermak 
11967b2df5ebSMilan Cermak 		/*
1197207983b5SMilan Cermak 		 * Try to obtain the path component from dnlc cache
1198207983b5SMilan Cermak 		 * before searching through the directory.
1199207983b5SMilan Cermak 		 */
1200207983b5SMilan Cermak 		if ((cmpvp = dnlc_reverse_lookup(vp, dbuf, dlen)) != NULL) {
1201207983b5SMilan Cermak 			/*
1202207983b5SMilan Cermak 			 * If we got parent vnode as a result,
1203207983b5SMilan Cermak 			 * then the answered path is correct.
1204207983b5SMilan Cermak 			 */
1205207983b5SMilan Cermak 			if (VN_CMP(cmpvp, pvp)) {
1206207983b5SMilan Cermak 				VN_RELE(cmpvp);
1207207983b5SMilan Cermak 				complen = strlen(dbuf);
1208207983b5SMilan Cermak 				bufloc -= complen;
1209207983b5SMilan Cermak 				if (bufloc <= buf) {
1210207983b5SMilan Cermak 					err = ENAMETOOLONG;
1211207983b5SMilan Cermak 					goto out;
1212207983b5SMilan Cermak 				}
1213207983b5SMilan Cermak 				bcopy(dbuf, bufloc, complen);
1214207983b5SMilan Cermak 
1215207983b5SMilan Cermak 				/* Prepend a slash to the current path */
1216207983b5SMilan Cermak 				*--bufloc = '/';
1217207983b5SMilan Cermak 
1218207983b5SMilan Cermak 				/* And continue with the next component */
1219207983b5SMilan Cermak 				VN_RELE(vp);
1220207983b5SMilan Cermak 				vp = pvp;
1221207983b5SMilan Cermak 				pvp = NULL;
1222207983b5SMilan Cermak 				continue;
1223207983b5SMilan Cermak 			} else {
1224207983b5SMilan Cermak 				VN_RELE(cmpvp);
1225207983b5SMilan Cermak 			}
1226207983b5SMilan Cermak 		}
1227207983b5SMilan Cermak 
1228207983b5SMilan Cermak 		/*
12297c478bd9Sstevel@tonic-gate 		 * Search the parent directory for the entry corresponding to
12307c478bd9Sstevel@tonic-gate 		 * this vnode.
12317c478bd9Sstevel@tonic-gate 		 */
12327c478bd9Sstevel@tonic-gate 		if ((err = dirfindvp(vrootp, pvp, vp, cr, dbuf, dlen, &dp))
12337c478bd9Sstevel@tonic-gate 		    != 0)
12347c478bd9Sstevel@tonic-gate 			goto out;
12357c478bd9Sstevel@tonic-gate 		complen = strlen(dp->d_name);
12367c478bd9Sstevel@tonic-gate 		bufloc -= complen;
12377c478bd9Sstevel@tonic-gate 		if (bufloc <= buf) {
12387c478bd9Sstevel@tonic-gate 			err = ENAMETOOLONG;
12397c478bd9Sstevel@tonic-gate 			goto out;
12407c478bd9Sstevel@tonic-gate 		}
12417c478bd9Sstevel@tonic-gate 		bcopy(dp->d_name, bufloc, complen);
12427c478bd9Sstevel@tonic-gate 
12437c478bd9Sstevel@tonic-gate 		/* Prepend a slash to the current path.  */
12447c478bd9Sstevel@tonic-gate 		*--bufloc = '/';
12457c478bd9Sstevel@tonic-gate 
12467c478bd9Sstevel@tonic-gate 		/* And continue with the next component */
12477c478bd9Sstevel@tonic-gate 		VN_RELE(vp);
12487c478bd9Sstevel@tonic-gate 		vp = pvp;
12497c478bd9Sstevel@tonic-gate 		pvp = NULL;
12507c478bd9Sstevel@tonic-gate 	}
12517c478bd9Sstevel@tonic-gate 
12527c478bd9Sstevel@tonic-gate 	/*
12537c478bd9Sstevel@tonic-gate 	 * Place the path at the beginning of the buffer.
12547c478bd9Sstevel@tonic-gate 	 */
12557c478bd9Sstevel@tonic-gate 	if (bufloc != buf)
12567c478bd9Sstevel@tonic-gate 		ovbcopy(bufloc, buf, buflen - (bufloc - buf));
12577c478bd9Sstevel@tonic-gate 
12587c478bd9Sstevel@tonic-gate out:
12597c478bd9Sstevel@tonic-gate 	/*
12607c478bd9Sstevel@tonic-gate 	 * If the error was ESTALE and the current directory to look in
12617c478bd9Sstevel@tonic-gate 	 * was the root for this lookup, the root for a mounted file
12627c478bd9Sstevel@tonic-gate 	 * system, or the starting directory for lookups, then
12637c478bd9Sstevel@tonic-gate 	 * return ENOENT instead of ESTALE.  In this case, no recovery
12647c478bd9Sstevel@tonic-gate 	 * is possible by the higher level.  If ESTALE was returned for
12657c478bd9Sstevel@tonic-gate 	 * some intermediate directory along the path, then recovery
12667c478bd9Sstevel@tonic-gate 	 * is potentially possible and retrying from the higher level
12677c478bd9Sstevel@tonic-gate 	 * will either correct the situation by purging stale cache
12687c478bd9Sstevel@tonic-gate 	 * entries or eventually get back to the point where no recovery
12697c478bd9Sstevel@tonic-gate 	 * is possible.
12707c478bd9Sstevel@tonic-gate 	 */
12717c478bd9Sstevel@tonic-gate 	if (err == ESTALE &&
12727c478bd9Sstevel@tonic-gate 	    (VN_CMP(vp, vrootp) || (vp->v_flag & VROOT) || vp == startvp))
12737c478bd9Sstevel@tonic-gate 		err = ENOENT;
12747c478bd9Sstevel@tonic-gate 
12757c478bd9Sstevel@tonic-gate 	kmem_free(dbuf, dlen);
12767c478bd9Sstevel@tonic-gate 	VN_RELE(vp);
12777c478bd9Sstevel@tonic-gate 	if (pvp)
12787c478bd9Sstevel@tonic-gate 		VN_RELE(pvp);
12797c478bd9Sstevel@tonic-gate 	pn_free(&pn);
12807c478bd9Sstevel@tonic-gate 	pn_free(&rpn);
12817c478bd9Sstevel@tonic-gate 
12827c478bd9Sstevel@tonic-gate 	return (err);
12837c478bd9Sstevel@tonic-gate }
12847c478bd9Sstevel@tonic-gate 
12857c478bd9Sstevel@tonic-gate /*
1286207983b5SMilan Cermak  * The additional flag, LOOKUP_CHECKREAD, is used to enforce artificial
12877c478bd9Sstevel@tonic-gate  * constraints in order to be standards compliant.  For example, if we have
12887c478bd9Sstevel@tonic-gate  * the cached path of '/foo/bar', and '/foo' has permissions 100 (execute
12897c478bd9Sstevel@tonic-gate  * only), then we can legitimately look up the path to the current working
12907c478bd9Sstevel@tonic-gate  * directory without needing read permission.  Existing standards tests,
12917c478bd9Sstevel@tonic-gate  * however, assume that we are determining the path by repeatedly looking up
12927c478bd9Sstevel@tonic-gate  * "..".  We need to keep this behavior in order to maintain backwards
12937c478bd9Sstevel@tonic-gate  * compatibility.
12947c478bd9Sstevel@tonic-gate  */
12957c478bd9Sstevel@tonic-gate static int
12967c478bd9Sstevel@tonic-gate vnodetopath_common(vnode_t *vrootp, vnode_t *vp, char *buf, size_t buflen,
12977c478bd9Sstevel@tonic-gate     cred_t *cr, int flags)
12987c478bd9Sstevel@tonic-gate {
12997c478bd9Sstevel@tonic-gate 	pathname_t pn, rpn;
13007c478bd9Sstevel@tonic-gate 	int ret, len;
13017c478bd9Sstevel@tonic-gate 	vnode_t *compvp, *pvp, *realvp;
13027c478bd9Sstevel@tonic-gate 	proc_t *p = curproc;
13037c478bd9Sstevel@tonic-gate 	char path[MAXNAMELEN];
13047c478bd9Sstevel@tonic-gate 	int doclose = 0;
13057c478bd9Sstevel@tonic-gate 
13067c478bd9Sstevel@tonic-gate 	/*
13077c478bd9Sstevel@tonic-gate 	 * If vrootp is NULL, get the root for curproc.  Callers with any other
13087c478bd9Sstevel@tonic-gate 	 * requirements should pass in a different vrootp.
13097c478bd9Sstevel@tonic-gate 	 */
13107c478bd9Sstevel@tonic-gate 	if (vrootp == NULL) {
13117c478bd9Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
13127c478bd9Sstevel@tonic-gate 		if ((vrootp = PTOU(p)->u_rdir) == NULL)
13137c478bd9Sstevel@tonic-gate 			vrootp = rootdir;
13147c478bd9Sstevel@tonic-gate 		VN_HOLD(vrootp);
13157c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
13167c478bd9Sstevel@tonic-gate 	} else {
13177c478bd9Sstevel@tonic-gate 		VN_HOLD(vrootp);
13187c478bd9Sstevel@tonic-gate 	}
13197c478bd9Sstevel@tonic-gate 
13207c478bd9Sstevel@tonic-gate 	/*
13217c478bd9Sstevel@tonic-gate 	 * This is to get around an annoying artifact of the /proc filesystem,
13227c478bd9Sstevel@tonic-gate 	 * which is the behavior of {cwd/root}.  Trying to resolve this path
13237c478bd9Sstevel@tonic-gate 	 * will result in /proc/pid/cwd instead of whatever the real working
13247c478bd9Sstevel@tonic-gate 	 * directory is.  We can't rely on VOP_REALVP(), since that will break
13257c478bd9Sstevel@tonic-gate 	 * lofs.  The only difference between procfs and lofs is that opening
13267c478bd9Sstevel@tonic-gate 	 * the file will return the underling vnode in the case of procfs.
13277c478bd9Sstevel@tonic-gate 	 */
1328da6c28aaSamw 	if (vp->v_type == VDIR && VOP_REALVP(vp, &realvp, NULL) == 0 &&
13297c478bd9Sstevel@tonic-gate 	    realvp != vp) {
13307c478bd9Sstevel@tonic-gate 		VN_HOLD(vp);
1331da6c28aaSamw 		if (VOP_OPEN(&vp, FREAD, cr, NULL) == 0)
13327c478bd9Sstevel@tonic-gate 			doclose = 1;
13337c478bd9Sstevel@tonic-gate 		else
13347c478bd9Sstevel@tonic-gate 			VN_RELE(vp);
13357c478bd9Sstevel@tonic-gate 	}
13367c478bd9Sstevel@tonic-gate 
13377c478bd9Sstevel@tonic-gate 	pn_alloc(&pn);
13387c478bd9Sstevel@tonic-gate 
13397c478bd9Sstevel@tonic-gate 	/*
13407c478bd9Sstevel@tonic-gate 	 * Check to see if we have a cached path in the vnode.
13417c478bd9Sstevel@tonic-gate 	 */
13427c478bd9Sstevel@tonic-gate 	mutex_enter(&vp->v_lock);
1343ca2c3138Seschrock 	if (vp->v_path != NULL) {
1344ca2c3138Seschrock 		(void) pn_set(&pn, vp->v_path);
13457c478bd9Sstevel@tonic-gate 		mutex_exit(&vp->v_lock);
13467c478bd9Sstevel@tonic-gate 
13477c478bd9Sstevel@tonic-gate 		pn_alloc(&rpn);
13487c478bd9Sstevel@tonic-gate 
13497c478bd9Sstevel@tonic-gate 		/* We should only cache absolute paths */
13507c478bd9Sstevel@tonic-gate 		ASSERT(pn.pn_buf[0] == '/');
13517c478bd9Sstevel@tonic-gate 
13527c478bd9Sstevel@tonic-gate 		/*
13537c478bd9Sstevel@tonic-gate 		 * If we are in a zone or a chroot environment, then we have to
13547c478bd9Sstevel@tonic-gate 		 * take additional steps, since the path to the root might not
13557c478bd9Sstevel@tonic-gate 		 * be readable with the current credentials, even though the
13567c478bd9Sstevel@tonic-gate 		 * process can legitmately access the file.  In this case, we
13577c478bd9Sstevel@tonic-gate 		 * do the following:
13587c478bd9Sstevel@tonic-gate 		 *
13597c478bd9Sstevel@tonic-gate 		 * lookuppnvp() with all privileges to get the resolved path.
13607c478bd9Sstevel@tonic-gate 		 * call localpath() to get the local portion of the path, and
13617c478bd9Sstevel@tonic-gate 		 * continue as normal.
13627c478bd9Sstevel@tonic-gate 		 *
13637c478bd9Sstevel@tonic-gate 		 * If the the conversion to a local path fails, then we continue
13647c478bd9Sstevel@tonic-gate 		 * as normal.  This is a heuristic to make process object file
13657c478bd9Sstevel@tonic-gate 		 * paths available from within a zone.  Because lofs doesn't
13667c478bd9Sstevel@tonic-gate 		 * support page operations, the vnode stored in the seg_t is
13677c478bd9Sstevel@tonic-gate 		 * actually the underlying real vnode, not the lofs node itself.
13687c478bd9Sstevel@tonic-gate 		 * Most of the time, the lofs path is the same as the underlying
13697c478bd9Sstevel@tonic-gate 		 * vnode (for example, /usr/lib/libc.so.1).
13707c478bd9Sstevel@tonic-gate 		 */
13717c478bd9Sstevel@tonic-gate 		if (vrootp != rootdir) {
13727c478bd9Sstevel@tonic-gate 			char *local = NULL;
13737c478bd9Sstevel@tonic-gate 			VN_HOLD(rootdir);
13747c478bd9Sstevel@tonic-gate 			if (lookuppnvp(&pn, &rpn, FOLLOW,
13757c478bd9Sstevel@tonic-gate 			    NULL, &compvp, rootdir, rootdir, kcred) == 0) {
13767c478bd9Sstevel@tonic-gate 				local = localpath(rpn.pn_path, vrootp,
13777c478bd9Sstevel@tonic-gate 				    kcred);
13787c478bd9Sstevel@tonic-gate 				VN_RELE(compvp);
13797c478bd9Sstevel@tonic-gate 			}
13807c478bd9Sstevel@tonic-gate 
13817c478bd9Sstevel@tonic-gate 			/*
1382207983b5SMilan Cermak 			 * The original pn was changed through lookuppnvp().
1383207983b5SMilan Cermak 			 * Set it to local for next validation attempt.
13847c478bd9Sstevel@tonic-gate 			 */
13857c478bd9Sstevel@tonic-gate 			if (local) {
13867c478bd9Sstevel@tonic-gate 				(void) pn_set(&pn, local);
13877c478bd9Sstevel@tonic-gate 			} else {
13887c478bd9Sstevel@tonic-gate 				goto notcached;
13897c478bd9Sstevel@tonic-gate 			}
13907c478bd9Sstevel@tonic-gate 		}
13917c478bd9Sstevel@tonic-gate 
13927c478bd9Sstevel@tonic-gate 		/*
13937c478bd9Sstevel@tonic-gate 		 * We should have a local path at this point, so start the
13947c478bd9Sstevel@tonic-gate 		 * search from the root of the current process.
13957c478bd9Sstevel@tonic-gate 		 */
13967c478bd9Sstevel@tonic-gate 		VN_HOLD(vrootp);
13977c478bd9Sstevel@tonic-gate 		if (vrootp != rootdir)
13987c478bd9Sstevel@tonic-gate 			VN_HOLD(vrootp);
13997c478bd9Sstevel@tonic-gate 		ret = lookuppnvp(&pn, &rpn, FOLLOW | flags, NULL,
14007c478bd9Sstevel@tonic-gate 		    &compvp, vrootp, vrootp, cr);
14017c478bd9Sstevel@tonic-gate 		if (ret == 0) {
14027c478bd9Sstevel@tonic-gate 			/*
14037c478bd9Sstevel@tonic-gate 			 * Check to see if the returned vnode is the same as
14047c478bd9Sstevel@tonic-gate 			 * the one we expect.  If not, give up.
14057c478bd9Sstevel@tonic-gate 			 */
14067c478bd9Sstevel@tonic-gate 			if (!vn_compare(vp, compvp) &&
14077c478bd9Sstevel@tonic-gate 			    !vnode_match(vp, compvp, cr)) {
14087c478bd9Sstevel@tonic-gate 				VN_RELE(compvp);
14097c478bd9Sstevel@tonic-gate 				goto notcached;
14107c478bd9Sstevel@tonic-gate 			}
14117c478bd9Sstevel@tonic-gate 
14127c478bd9Sstevel@tonic-gate 			VN_RELE(compvp);
14137c478bd9Sstevel@tonic-gate 
14147c478bd9Sstevel@tonic-gate 			/*
14157c478bd9Sstevel@tonic-gate 			 * Return the result.
14167c478bd9Sstevel@tonic-gate 			 */
14177c478bd9Sstevel@tonic-gate 			if (buflen <= rpn.pn_pathlen)
14187c478bd9Sstevel@tonic-gate 				goto notcached;
14197c478bd9Sstevel@tonic-gate 
14207c478bd9Sstevel@tonic-gate 			bcopy(rpn.pn_path, buf, rpn.pn_pathlen + 1);
14217c478bd9Sstevel@tonic-gate 			pn_free(&pn);
14227c478bd9Sstevel@tonic-gate 			pn_free(&rpn);
14237c478bd9Sstevel@tonic-gate 			VN_RELE(vrootp);
14247c478bd9Sstevel@tonic-gate 			if (doclose) {
1425da6c28aaSamw 				(void) VOP_CLOSE(vp, FREAD, 1, 0, cr, NULL);
14267c478bd9Sstevel@tonic-gate 				VN_RELE(vp);
14277c478bd9Sstevel@tonic-gate 			}
14287c478bd9Sstevel@tonic-gate 			return (0);
14297c478bd9Sstevel@tonic-gate 		}
14307c478bd9Sstevel@tonic-gate 
14317c478bd9Sstevel@tonic-gate notcached:
14327c478bd9Sstevel@tonic-gate 		pn_free(&rpn);
14337c478bd9Sstevel@tonic-gate 	} else {
14347c478bd9Sstevel@tonic-gate 		mutex_exit(&vp->v_lock);
14357c478bd9Sstevel@tonic-gate 	}
14367c478bd9Sstevel@tonic-gate 
14377c478bd9Sstevel@tonic-gate 	pn_free(&pn);
14387c478bd9Sstevel@tonic-gate 
14397c478bd9Sstevel@tonic-gate 	if (vp->v_type != VDIR) {
14407c478bd9Sstevel@tonic-gate 		/*
14417c478bd9Sstevel@tonic-gate 		 * If we don't have a directory, try to find it in the dnlc via
14427c478bd9Sstevel@tonic-gate 		 * reverse lookup.  Once this is found, we can use the regular
14437c478bd9Sstevel@tonic-gate 		 * directory search to find the full path.
14447c478bd9Sstevel@tonic-gate 		 */
14457c478bd9Sstevel@tonic-gate 		if ((pvp = dnlc_reverse_lookup(vp, path, MAXNAMELEN)) != NULL) {
14467b2df5ebSMilan Cermak 			/*
14477b2df5ebSMilan Cermak 			 * Check if we have read privilege so, that
14487b2df5ebSMilan Cermak 			 * we can lookup the path in the directory
14497b2df5ebSMilan Cermak 			 */
14507b2df5ebSMilan Cermak 			ret = 0;
14517b2df5ebSMilan Cermak 			if ((flags & LOOKUP_CHECKREAD)) {
14527b2df5ebSMilan Cermak 				ret = VOP_ACCESS(pvp, VREAD, 0, cr, NULL);
14537b2df5ebSMilan Cermak 			}
14547b2df5ebSMilan Cermak 			if (ret == 0) {
14557b2df5ebSMilan Cermak 				ret = dirtopath(vrootp, pvp, buf, buflen,
14567b2df5ebSMilan Cermak 				    flags, cr);
14577b2df5ebSMilan Cermak 			}
14587c478bd9Sstevel@tonic-gate 			if (ret == 0) {
14597c478bd9Sstevel@tonic-gate 				len = strlen(buf);
14607c478bd9Sstevel@tonic-gate 				if (len + strlen(path) + 1 >= buflen) {
14617c478bd9Sstevel@tonic-gate 					ret = ENAMETOOLONG;
14627c478bd9Sstevel@tonic-gate 				} else {
14637c478bd9Sstevel@tonic-gate 					if (buf[len - 1] != '/')
14647c478bd9Sstevel@tonic-gate 						buf[len++] = '/';
14657c478bd9Sstevel@tonic-gate 					bcopy(path, buf + len,
14667c478bd9Sstevel@tonic-gate 					    strlen(path) + 1);
14677c478bd9Sstevel@tonic-gate 				}
14687c478bd9Sstevel@tonic-gate 			}
14697c478bd9Sstevel@tonic-gate 
14707c478bd9Sstevel@tonic-gate 			VN_RELE(pvp);
14717c478bd9Sstevel@tonic-gate 		} else
14727c478bd9Sstevel@tonic-gate 			ret = ENOENT;
14737c478bd9Sstevel@tonic-gate 	} else
14747b2df5ebSMilan Cermak 		ret = dirtopath(vrootp, vp, buf, buflen, flags, cr);
14757c478bd9Sstevel@tonic-gate 
14767c478bd9Sstevel@tonic-gate 	VN_RELE(vrootp);
14777c478bd9Sstevel@tonic-gate 	if (doclose) {
1478da6c28aaSamw 		(void) VOP_CLOSE(vp, FREAD, 1, 0, cr, NULL);
14797c478bd9Sstevel@tonic-gate 		VN_RELE(vp);
14807c478bd9Sstevel@tonic-gate 	}
14817c478bd9Sstevel@tonic-gate 
14827c478bd9Sstevel@tonic-gate 	return (ret);
14837c478bd9Sstevel@tonic-gate }
14847c478bd9Sstevel@tonic-gate 
14857c478bd9Sstevel@tonic-gate int
14867c478bd9Sstevel@tonic-gate vnodetopath(vnode_t *vrootp, vnode_t *vp, char *buf, size_t buflen, cred_t *cr)
14877c478bd9Sstevel@tonic-gate {
14887c478bd9Sstevel@tonic-gate 	return (vnodetopath_common(vrootp, vp, buf, buflen, cr, 0));
14897c478bd9Sstevel@tonic-gate }
14907c478bd9Sstevel@tonic-gate 
14917c478bd9Sstevel@tonic-gate int
14927c478bd9Sstevel@tonic-gate dogetcwd(char *buf, size_t buflen)
14937c478bd9Sstevel@tonic-gate {
14947c478bd9Sstevel@tonic-gate 	int ret;
14957c478bd9Sstevel@tonic-gate 	vnode_t *vp;
14967c478bd9Sstevel@tonic-gate 	vnode_t *compvp;
14977c478bd9Sstevel@tonic-gate 	refstr_t *cwd, *oldcwd;
14987c478bd9Sstevel@tonic-gate 	const char *value;
14997c478bd9Sstevel@tonic-gate 	pathname_t rpnp, pnp;
15007c478bd9Sstevel@tonic-gate 	proc_t *p = curproc;
15017c478bd9Sstevel@tonic-gate 
15027c478bd9Sstevel@tonic-gate 	/*
15037c478bd9Sstevel@tonic-gate 	 * Check to see if there is a cached version of the cwd.  If so, lookup
15047c478bd9Sstevel@tonic-gate 	 * the cached value and make sure it is the same vnode.
15057c478bd9Sstevel@tonic-gate 	 */
15067c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
15077c478bd9Sstevel@tonic-gate 	if ((cwd = PTOU(p)->u_cwd) != NULL)
15087c478bd9Sstevel@tonic-gate 		refstr_hold(cwd);
15097c478bd9Sstevel@tonic-gate 	vp = PTOU(p)->u_cdir;
15107c478bd9Sstevel@tonic-gate 	VN_HOLD(vp);
15117c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
15127c478bd9Sstevel@tonic-gate 
15137c478bd9Sstevel@tonic-gate 	/*
15147c478bd9Sstevel@tonic-gate 	 * Make sure we have permission to access the current directory.
15157c478bd9Sstevel@tonic-gate 	 */
1516da6c28aaSamw 	if ((ret = VOP_ACCESS(vp, VEXEC, 0, CRED(), NULL)) != 0) {
15177c478bd9Sstevel@tonic-gate 		if (cwd != NULL)
15187c478bd9Sstevel@tonic-gate 			refstr_rele(cwd);
15197c478bd9Sstevel@tonic-gate 		VN_RELE(vp);
15207c478bd9Sstevel@tonic-gate 		return (ret);
15217c478bd9Sstevel@tonic-gate 	}
15227c478bd9Sstevel@tonic-gate 
15237c478bd9Sstevel@tonic-gate 	if (cwd) {
15247c478bd9Sstevel@tonic-gate 		value = refstr_value(cwd);
15257c478bd9Sstevel@tonic-gate 		if ((ret = pn_get((char *)value, UIO_SYSSPACE, &pnp)) != 0) {
15267c478bd9Sstevel@tonic-gate 			refstr_rele(cwd);
15277c478bd9Sstevel@tonic-gate 			VN_RELE(vp);
15287c478bd9Sstevel@tonic-gate 			return (ret);
15297c478bd9Sstevel@tonic-gate 		}
15307c478bd9Sstevel@tonic-gate 
15317c478bd9Sstevel@tonic-gate 		pn_alloc(&rpnp);
15327c478bd9Sstevel@tonic-gate 
15337c478bd9Sstevel@tonic-gate 		if (lookuppn(&pnp, &rpnp, NO_FOLLOW, NULL, &compvp) == 0) {
15347c478bd9Sstevel@tonic-gate 
15357c478bd9Sstevel@tonic-gate 			if (VN_CMP(vp, compvp) &&
15367c478bd9Sstevel@tonic-gate 			    strcmp(value, rpnp.pn_path) == 0) {
15377c478bd9Sstevel@tonic-gate 				VN_RELE(compvp);
15387c478bd9Sstevel@tonic-gate 				VN_RELE(vp);
15397c478bd9Sstevel@tonic-gate 				pn_free(&pnp);
15407c478bd9Sstevel@tonic-gate 				pn_free(&rpnp);
15417c478bd9Sstevel@tonic-gate 				if (strlen(value) + 1 > buflen) {
15427c478bd9Sstevel@tonic-gate 					refstr_rele(cwd);
15437c478bd9Sstevel@tonic-gate 					return (ENAMETOOLONG);
15447c478bd9Sstevel@tonic-gate 				}
15457c478bd9Sstevel@tonic-gate 				bcopy(value, buf, strlen(value) + 1);
15467c478bd9Sstevel@tonic-gate 				refstr_rele(cwd);
15477c478bd9Sstevel@tonic-gate 				return (0);
15487c478bd9Sstevel@tonic-gate 			}
15497c478bd9Sstevel@tonic-gate 
15507c478bd9Sstevel@tonic-gate 			VN_RELE(compvp);
15517c478bd9Sstevel@tonic-gate 		}
15527c478bd9Sstevel@tonic-gate 
15537c478bd9Sstevel@tonic-gate 		pn_free(&rpnp);
15547c478bd9Sstevel@tonic-gate 		pn_free(&pnp);
15557c478bd9Sstevel@tonic-gate 
15567c478bd9Sstevel@tonic-gate 		refstr_rele(cwd);
15577c478bd9Sstevel@tonic-gate 	}
15587c478bd9Sstevel@tonic-gate 
15597c478bd9Sstevel@tonic-gate 	ret = vnodetopath_common(NULL, vp, buf, buflen, CRED(),
15607c478bd9Sstevel@tonic-gate 	    LOOKUP_CHECKREAD);
15617c478bd9Sstevel@tonic-gate 
15627c478bd9Sstevel@tonic-gate 	VN_RELE(vp);
15637c478bd9Sstevel@tonic-gate 
15647c478bd9Sstevel@tonic-gate 	/*
15657c478bd9Sstevel@tonic-gate 	 * Store the new cwd and replace the existing cached copy.
15667c478bd9Sstevel@tonic-gate 	 */
15677c478bd9Sstevel@tonic-gate 	if (ret == 0)
15687c478bd9Sstevel@tonic-gate 		cwd = refstr_alloc(buf);
15697c478bd9Sstevel@tonic-gate 	else
15707c478bd9Sstevel@tonic-gate 		cwd = NULL;
15717c478bd9Sstevel@tonic-gate 
15727c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
15737c478bd9Sstevel@tonic-gate 	oldcwd = PTOU(p)->u_cwd;
15747c478bd9Sstevel@tonic-gate 	PTOU(p)->u_cwd = cwd;
15757c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
15767c478bd9Sstevel@tonic-gate 
15777c478bd9Sstevel@tonic-gate 	if (oldcwd)
15787c478bd9Sstevel@tonic-gate 		refstr_rele(oldcwd);
15797c478bd9Sstevel@tonic-gate 
15807c478bd9Sstevel@tonic-gate 	return (ret);
15817c478bd9Sstevel@tonic-gate }
1582