xref: /illumos-gate/usr/src/uts/common/fs/lookup.c (revision 680069d3)
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
52e538c1cSvv  * Common Development and Distribution License (the "License").
62e538c1cSvv  * 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  */
21794f0adbSRoger A. Faulkner 
227c478bd9Sstevel@tonic-gate /*
23fad76a8aSMatt Barden  * Copyright 2019 Nexenta by DDN, Inc. All rights reserved.
242f5706a1SWilliam.Johnston  * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
25e2fc3408SPatrick Mooney  * Copyright 2016 Joyent, Inc.
26*680069d3SMatt Barden  * Copyright 2023 RackTop Systems, Inc.
277c478bd9Sstevel@tonic-gate  */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
305f61829aSRobert Mustacchi /*	  All Rights Reserved   */
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate /*
337c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
347c478bd9Sstevel@tonic-gate  * The Regents of the University of California
357c478bd9Sstevel@tonic-gate  * All Rights Reserved
367c478bd9Sstevel@tonic-gate  *
377c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
387c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
397c478bd9Sstevel@tonic-gate  * contributors.
407c478bd9Sstevel@tonic-gate  */
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #include <sys/types.h>
437c478bd9Sstevel@tonic-gate #include <sys/param.h>
447c478bd9Sstevel@tonic-gate #include <sys/systm.h>
457c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
467c478bd9Sstevel@tonic-gate #include <sys/errno.h>
477c478bd9Sstevel@tonic-gate #include <sys/cred.h>
487c478bd9Sstevel@tonic-gate #include <sys/user.h>
497c478bd9Sstevel@tonic-gate #include <sys/uio.h>
507c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
517c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
527c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
537c478bd9Sstevel@tonic-gate #include <sys/proc.h>
547c478bd9Sstevel@tonic-gate #include <sys/vtrace.h>
557c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
567c478bd9Sstevel@tonic-gate #include <sys/debug.h>
577c478bd9Sstevel@tonic-gate #include <sys/dirent.h>
587c478bd9Sstevel@tonic-gate #include <c2/audit.h>
597c478bd9Sstevel@tonic-gate #include <sys/zone.h>
607c478bd9Sstevel@tonic-gate #include <sys/dnlc.h>
617c478bd9Sstevel@tonic-gate #include <sys/fs/snode.h>
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate /* Controls whether paths are stored with vnodes. */
647c478bd9Sstevel@tonic-gate int vfs_vnode_path = 1;
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate int
lookupname(const char * fnamep,enum uio_seg seg,int followlink,vnode_t ** dirvpp,vnode_t ** compvpp)677c478bd9Sstevel@tonic-gate lookupname(
685f61829aSRobert Mustacchi 	const char *fnamep,
697c478bd9Sstevel@tonic-gate 	enum uio_seg seg,
70794f0adbSRoger A. Faulkner 	int followlink,
717c478bd9Sstevel@tonic-gate 	vnode_t **dirvpp,
727c478bd9Sstevel@tonic-gate 	vnode_t **compvpp)
737c478bd9Sstevel@tonic-gate {
747a142be9SCasper H.S. Dik 	return (lookupnameatcred(fnamep, seg, followlink, dirvpp, compvpp, NULL,
757a142be9SCasper H.S. Dik 	    CRED()));
767c478bd9Sstevel@tonic-gate }
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate /*
797c478bd9Sstevel@tonic-gate  * Lookup the user file name,
807c478bd9Sstevel@tonic-gate  * Handle allocation and freeing of pathname buffer, return error.
817c478bd9Sstevel@tonic-gate  */
827c478bd9Sstevel@tonic-gate int
lookupnameatcred(const char * fnamep,enum uio_seg seg,int followlink,vnode_t ** dirvpp,vnode_t ** compvpp,vnode_t * startvp,cred_t * cr)837a142be9SCasper H.S. Dik lookupnameatcred(
845f61829aSRobert Mustacchi 	const char *fnamep,		/* user pathname */
857c478bd9Sstevel@tonic-gate 	enum uio_seg seg,		/* addr space that name is in */
86794f0adbSRoger A. Faulkner 	int followlink,			/* follow sym links */
877c478bd9Sstevel@tonic-gate 	vnode_t **dirvpp,		/* ret for ptr to parent dir vnode */
887c478bd9Sstevel@tonic-gate 	vnode_t **compvpp,		/* ret for ptr to component vnode */
897a142be9SCasper H.S. Dik 	vnode_t *startvp,		/* start path search from vp */
907a142be9SCasper H.S. Dik 	cred_t *cr)			/* credential */
917c478bd9Sstevel@tonic-gate {
927c478bd9Sstevel@tonic-gate 	char namebuf[TYPICALMAXPATHLEN];
937c478bd9Sstevel@tonic-gate 	struct pathname lookpn;
947c478bd9Sstevel@tonic-gate 	int error;
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 	error = pn_get_buf(fnamep, seg, &lookpn, namebuf, sizeof (namebuf));
977c478bd9Sstevel@tonic-gate 	if (error == 0) {
987a142be9SCasper H.S. Dik 		error = lookuppnatcred(&lookpn, NULL, followlink,
997a142be9SCasper H.S. Dik 		    dirvpp, compvpp, startvp, cr);
1007c478bd9Sstevel@tonic-gate 	}
1017c478bd9Sstevel@tonic-gate 	if (error == ENAMETOOLONG) {
1027c478bd9Sstevel@tonic-gate 		/*
1037c478bd9Sstevel@tonic-gate 		 * This thread used a pathname > TYPICALMAXPATHLEN bytes long.
1047c478bd9Sstevel@tonic-gate 		 */
1057c478bd9Sstevel@tonic-gate 		if (error = pn_get(fnamep, seg, &lookpn))
1067c478bd9Sstevel@tonic-gate 			return (error);
1077a142be9SCasper H.S. Dik 		error = lookuppnatcred(&lookpn, NULL, followlink,
1087a142be9SCasper H.S. Dik 		    dirvpp, compvpp, startvp, cr);
1097c478bd9Sstevel@tonic-gate 		pn_free(&lookpn);
1107c478bd9Sstevel@tonic-gate 	}
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 	return (error);
1137c478bd9Sstevel@tonic-gate }
1147c478bd9Sstevel@tonic-gate 
1157a142be9SCasper H.S. Dik int
lookupnameat(const char * fnamep,enum uio_seg seg,int followlink,vnode_t ** dirvpp,vnode_t ** compvpp,vnode_t * startvp)1165f61829aSRobert Mustacchi lookupnameat(const char *fnamep, enum uio_seg seg, int followlink,
1177a142be9SCasper H.S. Dik     vnode_t **dirvpp, vnode_t **compvpp, vnode_t *startvp)
1187a142be9SCasper H.S. Dik {
1197a142be9SCasper H.S. Dik 	return (lookupnameatcred(fnamep, seg, followlink, dirvpp, compvpp,
1207a142be9SCasper H.S. Dik 	    startvp, CRED()));
1217a142be9SCasper H.S. Dik }
1227a142be9SCasper H.S. Dik 
1237c478bd9Sstevel@tonic-gate int
lookuppn(struct pathname * pnp,struct pathname * rpnp,int followlink,vnode_t ** dirvpp,vnode_t ** compvpp)1247c478bd9Sstevel@tonic-gate lookuppn(
1257c478bd9Sstevel@tonic-gate 	struct pathname *pnp,
1267c478bd9Sstevel@tonic-gate 	struct pathname *rpnp,
127794f0adbSRoger A. Faulkner 	int followlink,
1287c478bd9Sstevel@tonic-gate 	vnode_t **dirvpp,
1297c478bd9Sstevel@tonic-gate 	vnode_t **compvpp)
1307c478bd9Sstevel@tonic-gate {
1317a142be9SCasper H.S. Dik 	return (lookuppnatcred(pnp, rpnp, followlink, dirvpp, compvpp, NULL,
1327a142be9SCasper H.S. Dik 	    CRED()));
1337c478bd9Sstevel@tonic-gate }
1347c478bd9Sstevel@tonic-gate 
1357a142be9SCasper H.S. Dik /*
1367a142be9SCasper H.S. Dik  * Lookup the user file name from a given vp, using a specific credential.
1377a142be9SCasper H.S. Dik  */
1387c478bd9Sstevel@tonic-gate int
lookuppnatcred(struct pathname * pnp,struct pathname * rpnp,int followlink,vnode_t ** dirvpp,vnode_t ** compvpp,vnode_t * startvp,cred_t * cr)1397a142be9SCasper H.S. Dik lookuppnatcred(
1407c478bd9Sstevel@tonic-gate 	struct pathname *pnp,		/* pathname to lookup */
1417c478bd9Sstevel@tonic-gate 	struct pathname *rpnp,		/* if non-NULL, return resolved path */
142794f0adbSRoger A. Faulkner 	int followlink,			/* (don't) follow sym links */
1437c478bd9Sstevel@tonic-gate 	vnode_t **dirvpp,		/* ptr for parent vnode */
1447c478bd9Sstevel@tonic-gate 	vnode_t **compvpp,		/* ptr for entry vnode */
1457a142be9SCasper H.S. Dik 	vnode_t *startvp,		/* start search from this vp */
1467a142be9SCasper H.S. Dik 	cred_t *cr)			/* user credential */
1477c478bd9Sstevel@tonic-gate {
1487c478bd9Sstevel@tonic-gate 	vnode_t *vp;	/* current directory vp */
1497c478bd9Sstevel@tonic-gate 	vnode_t *rootvp;
1507c478bd9Sstevel@tonic-gate 	proc_t *p = curproc;
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	if (pnp->pn_pathlen == 0)
1537c478bd9Sstevel@tonic-gate 		return (ENOENT);
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);	/* for u_rdir and u_cdir */
1567c478bd9Sstevel@tonic-gate 	if ((rootvp = PTOU(p)->u_rdir) == NULL)
1577c478bd9Sstevel@tonic-gate 		rootvp = rootdir;
1587c478bd9Sstevel@tonic-gate 	else if (rootvp != rootdir)	/* no need to VN_HOLD rootdir */
1597c478bd9Sstevel@tonic-gate 		VN_HOLD(rootvp);
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 	if (pnp->pn_path[0] == '/') {
1627c478bd9Sstevel@tonic-gate 		vp = rootvp;
1637c478bd9Sstevel@tonic-gate 	} else {
1647c478bd9Sstevel@tonic-gate 		vp = (startvp == NULL) ? PTOU(p)->u_cdir : startvp;
1657c478bd9Sstevel@tonic-gate 	}
1667c478bd9Sstevel@tonic-gate 	VN_HOLD(vp);
1677c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	/*
1707c478bd9Sstevel@tonic-gate 	 * Skip over leading slashes
1717c478bd9Sstevel@tonic-gate 	 */
1727c478bd9Sstevel@tonic-gate 	if (pnp->pn_path[0] == '/') {
1737c478bd9Sstevel@tonic-gate 		do {
1747c478bd9Sstevel@tonic-gate 			pnp->pn_path++;
1757c478bd9Sstevel@tonic-gate 			pnp->pn_pathlen--;
1767c478bd9Sstevel@tonic-gate 		} while (pnp->pn_path[0] == '/');
1777c478bd9Sstevel@tonic-gate 	}
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 	return (lookuppnvp(pnp, rpnp, followlink, dirvpp,
1807a142be9SCasper H.S. Dik 	    compvpp, rootvp, vp, cr));
1817a142be9SCasper H.S. Dik }
1827a142be9SCasper H.S. Dik 
1837a142be9SCasper H.S. Dik int
lookuppnat(struct pathname * pnp,struct pathname * rpnp,int followlink,vnode_t ** dirvpp,vnode_t ** compvpp,vnode_t * startvp)1847a142be9SCasper H.S. Dik lookuppnat(struct pathname *pnp, struct pathname *rpnp,
185794f0adbSRoger A. Faulkner     int followlink, vnode_t **dirvpp, vnode_t **compvpp,
1867a142be9SCasper H.S. Dik     vnode_t *startvp)
1877a142be9SCasper H.S. Dik {
1887a142be9SCasper H.S. Dik 	return (lookuppnatcred(pnp, rpnp, followlink, dirvpp, compvpp, startvp,
1897a142be9SCasper H.S. Dik 	    CRED()));
1907c478bd9Sstevel@tonic-gate }
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate /* Private flag to do our getcwd() dirty work */
1937c478bd9Sstevel@tonic-gate #define	LOOKUP_CHECKREAD	0x10
1947c478bd9Sstevel@tonic-gate #define	LOOKUP_MASK		(~LOOKUP_CHECKREAD)
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate /*
1977c478bd9Sstevel@tonic-gate  * Starting at current directory, translate pathname pnp to end.
1987c478bd9Sstevel@tonic-gate  * Leave pathname of final component in pnp, return the vnode
1997c478bd9Sstevel@tonic-gate  * for the final component in *compvpp, and return the vnode
2007c478bd9Sstevel@tonic-gate  * for the parent of the final component in dirvpp.
2017c478bd9Sstevel@tonic-gate  *
2027c478bd9Sstevel@tonic-gate  * This is the central routine in pathname translation and handles
2037c478bd9Sstevel@tonic-gate  * multiple components in pathnames, separating them at /'s.  It also
2047c478bd9Sstevel@tonic-gate  * implements mounted file systems and processes symbolic links.
2057c478bd9Sstevel@tonic-gate  *
2067c478bd9Sstevel@tonic-gate  * vp is the vnode where the directory search should start.
2077c478bd9Sstevel@tonic-gate  *
2087c478bd9Sstevel@tonic-gate  * Reference counts: vp must be held prior to calling this function.  rootvp
2097c478bd9Sstevel@tonic-gate  * should only be held if rootvp != rootdir.
2107c478bd9Sstevel@tonic-gate  */
2117c478bd9Sstevel@tonic-gate int
lookuppnvp(struct pathname * pnp,struct pathname * rpnp,int flags,vnode_t ** dirvpp,vnode_t ** compvpp,vnode_t * rootvp,vnode_t * vp,cred_t * cr)2127c478bd9Sstevel@tonic-gate lookuppnvp(
2137c478bd9Sstevel@tonic-gate 	struct pathname *pnp,		/* pathname to lookup */
2147c478bd9Sstevel@tonic-gate 	struct pathname *rpnp,		/* if non-NULL, return resolved path */
2157c478bd9Sstevel@tonic-gate 	int flags,			/* follow symlinks */
2167c478bd9Sstevel@tonic-gate 	vnode_t **dirvpp,		/* ptr for parent vnode */
2177c478bd9Sstevel@tonic-gate 	vnode_t **compvpp,		/* ptr for entry vnode */
2187c478bd9Sstevel@tonic-gate 	vnode_t *rootvp,		/* rootvp */
2197c478bd9Sstevel@tonic-gate 	vnode_t *vp,			/* directory to start search at */
2207c478bd9Sstevel@tonic-gate 	cred_t *cr)			/* user's credential */
2217c478bd9Sstevel@tonic-gate {
2227c478bd9Sstevel@tonic-gate 	vnode_t *cvp;	/* current component vp */
2237c478bd9Sstevel@tonic-gate 	char component[MAXNAMELEN];	/* buffer for component (incl null) */
2247c478bd9Sstevel@tonic-gate 	int error;
2257c478bd9Sstevel@tonic-gate 	int nlink;
2267c478bd9Sstevel@tonic-gate 	int lookup_flags;
227da6c28aaSamw 	struct pathname presrvd; /* case preserved name */
228da6c28aaSamw 	struct pathname *pp = NULL;
2297c478bd9Sstevel@tonic-gate 	vnode_t *startvp;
2307c478bd9Sstevel@tonic-gate 	vnode_t *zonevp = curproc->p_zone->zone_rootvp;		/* zone root */
2317c478bd9Sstevel@tonic-gate 	int must_be_directory = 0;
23253ebc32aSVallish Vaidyeshwara 	boolean_t retry_with_kcred;
233005d3febSMarek Pospisil 	uint32_t auditing = AU_AUDITING();
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	CPU_STATS_ADDQ(CPU, sys, namei, 1);
2367c478bd9Sstevel@tonic-gate 	nlink = 0;
2377c478bd9Sstevel@tonic-gate 	cvp = NULL;
2387c478bd9Sstevel@tonic-gate 	if (rpnp)
2397c478bd9Sstevel@tonic-gate 		rpnp->pn_pathlen = 0;
240da6c28aaSamw 
2417c478bd9Sstevel@tonic-gate 	lookup_flags = dirvpp ? LOOKUP_DIR : 0;
242da6c28aaSamw 	if (flags & FIGNORECASE) {
243da6c28aaSamw 		lookup_flags |= FIGNORECASE;
244da6c28aaSamw 		pn_alloc(&presrvd);
245da6c28aaSamw 		pp = &presrvd;
246da6c28aaSamw 	}
247*680069d3SMatt Barden 	if ((flags & LOOKUP_NOACLCHECK) != 0) {
248*680069d3SMatt Barden 		lookup_flags |= LOOKUP_NOACLCHECK;
249*680069d3SMatt Barden 		flags &= ~LOOKUP_NOACLCHECK;
250*680069d3SMatt Barden 	}
251da6c28aaSamw 
252005d3febSMarek Pospisil 	if (auditing)
2537c478bd9Sstevel@tonic-gate 		audit_anchorpath(pnp, vp == rootvp);
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 	/*
2567c478bd9Sstevel@tonic-gate 	 * Eliminate any trailing slashes in the pathname.
2577c478bd9Sstevel@tonic-gate 	 * If there are any, we must follow all symlinks.
2587c478bd9Sstevel@tonic-gate 	 * Also, we must guarantee that the last component is a directory.
2597c478bd9Sstevel@tonic-gate 	 */
2607c478bd9Sstevel@tonic-gate 	if (pn_fixslash(pnp)) {
2617c478bd9Sstevel@tonic-gate 		flags |= FOLLOW;
2627c478bd9Sstevel@tonic-gate 		must_be_directory = 1;
2637c478bd9Sstevel@tonic-gate 	}
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 	startvp = vp;
2667c478bd9Sstevel@tonic-gate next:
26753ebc32aSVallish Vaidyeshwara 	retry_with_kcred = B_FALSE;
26853ebc32aSVallish Vaidyeshwara 
2697c478bd9Sstevel@tonic-gate 	/*
2707c478bd9Sstevel@tonic-gate 	 * Make sure we have a directory.
2717c478bd9Sstevel@tonic-gate 	 */
2727c478bd9Sstevel@tonic-gate 	if (vp->v_type != VDIR) {
2737c478bd9Sstevel@tonic-gate 		error = ENOTDIR;
2747c478bd9Sstevel@tonic-gate 		goto bad;
2757c478bd9Sstevel@tonic-gate 	}
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 	if (rpnp && VN_CMP(vp, rootvp))
2787c478bd9Sstevel@tonic-gate 		(void) pn_set(rpnp, "/");
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 	/*
2817c478bd9Sstevel@tonic-gate 	 * Process the next component of the pathname.
2827c478bd9Sstevel@tonic-gate 	 */
2837c478bd9Sstevel@tonic-gate 	if (error = pn_getcomponent(pnp, component)) {
2847c478bd9Sstevel@tonic-gate 		goto bad;
2857c478bd9Sstevel@tonic-gate 	}
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 	/*
2887c478bd9Sstevel@tonic-gate 	 * Handle "..": two special cases.
2897c478bd9Sstevel@tonic-gate 	 * 1. If we're at the root directory (e.g. after chroot or
2907c478bd9Sstevel@tonic-gate 	 *    zone_enter) then change ".." to "." so we can't get
2917c478bd9Sstevel@tonic-gate 	 *    out of this subtree.
2927c478bd9Sstevel@tonic-gate 	 * 2. If this vnode is the root of a mounted file system,
2937c478bd9Sstevel@tonic-gate 	 *    then replace it with the vnode that was mounted on
2947c478bd9Sstevel@tonic-gate 	 *    so that we take the ".." in the other file system.
2957c478bd9Sstevel@tonic-gate 	 */
2967c478bd9Sstevel@tonic-gate 	if (component[0] == '.' && component[1] == '.' && component[2] == 0) {
2977c478bd9Sstevel@tonic-gate checkforroot:
2987c478bd9Sstevel@tonic-gate 		if (VN_CMP(vp, rootvp) || VN_CMP(vp, zonevp)) {
2997c478bd9Sstevel@tonic-gate 			component[1] = '\0';
3007c478bd9Sstevel@tonic-gate 		} else if (vp->v_flag & VROOT) {
3017c478bd9Sstevel@tonic-gate 			vfs_t *vfsp;
3027c478bd9Sstevel@tonic-gate 			cvp = vp;
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 			/*
3057c478bd9Sstevel@tonic-gate 			 * While we deal with the vfs pointer from the vnode
3067c478bd9Sstevel@tonic-gate 			 * the filesystem could have been forcefully unmounted
3077c478bd9Sstevel@tonic-gate 			 * and the vnode's v_vfsp could have been invalidated
3087c478bd9Sstevel@tonic-gate 			 * by VFS_UNMOUNT. Hence, we cache v_vfsp and use it
3097c478bd9Sstevel@tonic-gate 			 * with vfs_rlock_wait/vfs_unlock.
3107c478bd9Sstevel@tonic-gate 			 * It is safe to use the v_vfsp even it is freed by
3117c478bd9Sstevel@tonic-gate 			 * VFS_UNMOUNT because vfs_rlock_wait/vfs_unlock
3127c478bd9Sstevel@tonic-gate 			 * do not dereference v_vfsp. It is just used as a
3137c478bd9Sstevel@tonic-gate 			 * magic cookie.
3147c478bd9Sstevel@tonic-gate 			 * One more corner case here is the memory getting
3157c478bd9Sstevel@tonic-gate 			 * reused for another vfs structure. In this case
3167c478bd9Sstevel@tonic-gate 			 * lookuppnvp's vfs_rlock_wait will succeed, domount's
3177c478bd9Sstevel@tonic-gate 			 * vfs_lock will fail and domount will bail out with an
3187c478bd9Sstevel@tonic-gate 			 * error (EBUSY).
3197c478bd9Sstevel@tonic-gate 			 */
3207c478bd9Sstevel@tonic-gate 			vfsp = cvp->v_vfsp;
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 			/*
3237c478bd9Sstevel@tonic-gate 			 * This lock is used to synchronize
3247c478bd9Sstevel@tonic-gate 			 * mounts/unmounts and lookups.
3257c478bd9Sstevel@tonic-gate 			 * Threads doing mounts/unmounts hold the
3267c478bd9Sstevel@tonic-gate 			 * writers version vfs_lock_wait().
3277c478bd9Sstevel@tonic-gate 			 */
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 			vfs_rlock_wait(vfsp);
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 			/*
3327c478bd9Sstevel@tonic-gate 			 * If this vnode is on a file system that
3337c478bd9Sstevel@tonic-gate 			 * has been forcibly unmounted,
3347c478bd9Sstevel@tonic-gate 			 * we can't proceed. Cancel this operation
3357c478bd9Sstevel@tonic-gate 			 * and return EIO.
3367c478bd9Sstevel@tonic-gate 			 *
3377c478bd9Sstevel@tonic-gate 			 * vfs_vnodecovered is NULL if unmounted.
3387c478bd9Sstevel@tonic-gate 			 * Currently, nfs uses VFS_UNMOUNTED to
3397c478bd9Sstevel@tonic-gate 			 * check if it's a forced-umount. Keep the
3407c478bd9Sstevel@tonic-gate 			 * same checking here as well even though it
3417c478bd9Sstevel@tonic-gate 			 * may not be needed.
3427c478bd9Sstevel@tonic-gate 			 */
3437c478bd9Sstevel@tonic-gate 			if (((vp = cvp->v_vfsp->vfs_vnodecovered) == NULL) ||
3447c478bd9Sstevel@tonic-gate 			    (cvp->v_vfsp->vfs_flag & VFS_UNMOUNTED)) {
3457c478bd9Sstevel@tonic-gate 				vfs_unlock(vfsp);
3467c478bd9Sstevel@tonic-gate 				VN_RELE(cvp);
347da6c28aaSamw 				if (pp)
348da6c28aaSamw 					pn_free(pp);
3497c478bd9Sstevel@tonic-gate 				return (EIO);
3507c478bd9Sstevel@tonic-gate 			}
3517c478bd9Sstevel@tonic-gate 			VN_HOLD(vp);
3527c478bd9Sstevel@tonic-gate 			vfs_unlock(vfsp);
3537c478bd9Sstevel@tonic-gate 			VN_RELE(cvp);
3547c478bd9Sstevel@tonic-gate 			cvp = NULL;
3552e538c1cSvv 			/*
3562e538c1cSvv 			 * Crossing mount points. For eg: We are doing
3572e538c1cSvv 			 * a lookup of ".." for file systems root vnode
3582e538c1cSvv 			 * mounted here, and VOP_LOOKUP() (with covered vnode)
3592e538c1cSvv 			 * will be on underlying file systems mount point
3602e538c1cSvv 			 * vnode. Set retry_with_kcred flag as we might end
3612e538c1cSvv 			 * up doing VOP_LOOKUP() with kcred if required.
3622e538c1cSvv 			 */
3632e538c1cSvv 			retry_with_kcred = B_TRUE;
3647c478bd9Sstevel@tonic-gate 			goto checkforroot;
3657c478bd9Sstevel@tonic-gate 		}
3667c478bd9Sstevel@tonic-gate 	}
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 	/*
3697c478bd9Sstevel@tonic-gate 	 * LOOKUP_CHECKREAD is a private flag used by vnodetopath() to indicate
3707c478bd9Sstevel@tonic-gate 	 * that we need to have read permission on every directory in the entire
3717c478bd9Sstevel@tonic-gate 	 * path.  This is used to ensure that a forward-lookup of a cached value
3727c478bd9Sstevel@tonic-gate 	 * has the same effect as a reverse-lookup when the cached value cannot
3737c478bd9Sstevel@tonic-gate 	 * be found.
3747c478bd9Sstevel@tonic-gate 	 */
3757c478bd9Sstevel@tonic-gate 	if ((flags & LOOKUP_CHECKREAD) &&
376da6c28aaSamw 	    (error = VOP_ACCESS(vp, VREAD, 0, cr, NULL)) != 0)
3777c478bd9Sstevel@tonic-gate 		goto bad;
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 	/*
3807c478bd9Sstevel@tonic-gate 	 * Perform a lookup in the current directory.
3817c478bd9Sstevel@tonic-gate 	 */
3826e062f4aSMarcel Telka 	error = VOP_LOOKUP(vp, component, &cvp, pnp, lookup_flags,
383da6c28aaSamw 	    rootvp, cr, NULL, NULL, pp);
3842e538c1cSvv 
3852e538c1cSvv 	/*
3862e538c1cSvv 	 * Retry with kcred - If crossing mount points & error is EACCES.
3872e538c1cSvv 	 *
3882e538c1cSvv 	 * If we are crossing mount points here and doing ".." lookup,
3892e538c1cSvv 	 * VOP_LOOKUP() might fail if the underlying file systems
3902e538c1cSvv 	 * mount point has no execute permission. In cases like these,
3912e538c1cSvv 	 * we retry VOP_LOOKUP() by giving as much privilage as possible
3922e538c1cSvv 	 * by passing kcred credentials.
3932e538c1cSvv 	 *
3942e538c1cSvv 	 * In case of hierarchical file systems, passing kcred still may
3952e538c1cSvv 	 * or may not work.
3962e538c1cSvv 	 * For eg: UFS FS --> Mount NFS FS --> Again mount UFS on some
3972e538c1cSvv 	 *			directory inside NFS FS.
3982e538c1cSvv 	 */
3992e538c1cSvv 	if ((error == EACCES) && retry_with_kcred)
4006e062f4aSMarcel Telka 		error = VOP_LOOKUP(vp, component, &cvp, pnp, lookup_flags,
401da6c28aaSamw 		    rootvp, zone_kcred(), NULL, NULL, pp);
4022e538c1cSvv 
4037c478bd9Sstevel@tonic-gate 	if (error) {
4047c478bd9Sstevel@tonic-gate 		cvp = NULL;
4057c478bd9Sstevel@tonic-gate 		/*
4067c478bd9Sstevel@tonic-gate 		 * On error, return hard error if
4077c478bd9Sstevel@tonic-gate 		 * (a) we're not at the end of the pathname yet, or
4087c478bd9Sstevel@tonic-gate 		 * (b) the caller didn't want the parent directory, or
4097c478bd9Sstevel@tonic-gate 		 * (c) we failed for some reason other than a missing entry.
4107c478bd9Sstevel@tonic-gate 		 */
4117c478bd9Sstevel@tonic-gate 		if (pn_pathleft(pnp) || dirvpp == NULL || error != ENOENT)
4127c478bd9Sstevel@tonic-gate 			goto bad;
413005d3febSMarek Pospisil 		if (auditing) {	/* directory access */
4144a0fa546SMarek Pospisil 			if (error = audit_savepath(pnp, vp, vp, error, cr))
4157c478bd9Sstevel@tonic-gate 				goto bad_noaudit;
4167c478bd9Sstevel@tonic-gate 		}
4174a0fa546SMarek Pospisil 
4187c478bd9Sstevel@tonic-gate 		pn_setlast(pnp);
4197c478bd9Sstevel@tonic-gate 		/*
4207c478bd9Sstevel@tonic-gate 		 * We inform the caller that the desired entry must be
4217c478bd9Sstevel@tonic-gate 		 * a directory by adding a '/' to the component name.
4227c478bd9Sstevel@tonic-gate 		 */
4237c478bd9Sstevel@tonic-gate 		if (must_be_directory && (error = pn_addslash(pnp)) != 0)
4247c478bd9Sstevel@tonic-gate 			goto bad;
4257c478bd9Sstevel@tonic-gate 		*dirvpp = vp;
4267c478bd9Sstevel@tonic-gate 		if (compvpp != NULL)
4277c478bd9Sstevel@tonic-gate 			*compvpp = NULL;
4287c478bd9Sstevel@tonic-gate 		if (rootvp != rootdir)
4297c478bd9Sstevel@tonic-gate 			VN_RELE(rootvp);
430da6c28aaSamw 		if (pp)
431da6c28aaSamw 			pn_free(pp);
4327c478bd9Sstevel@tonic-gate 		return (0);
4337c478bd9Sstevel@tonic-gate 	}
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate 	/*
4367c478bd9Sstevel@tonic-gate 	 * Traverse mount points.
4377c478bd9Sstevel@tonic-gate 	 * XXX why don't we need to hold a read lock here (call vn_vfsrlock)?
4387c478bd9Sstevel@tonic-gate 	 * What prevents a concurrent update to v_vfsmountedhere?
439fad76a8aSMatt Barden 	 *	Possible answer: if mounting, we might not see the mount
4407c478bd9Sstevel@tonic-gate 	 *	if it is concurrently coming into existence, but that's
4417c478bd9Sstevel@tonic-gate 	 *	really not much different from the thread running a bit slower.
4427c478bd9Sstevel@tonic-gate 	 *	If unmounting, we may get into traverse() when we shouldn't,
4437c478bd9Sstevel@tonic-gate 	 *	but traverse() will catch this case for us.
4447c478bd9Sstevel@tonic-gate 	 *	(For this to work, fetching v_vfsmountedhere had better
4457c478bd9Sstevel@tonic-gate 	 *	be atomic!)
4467c478bd9Sstevel@tonic-gate 	 */
4477c478bd9Sstevel@tonic-gate 	if (vn_mountedvfs(cvp) != NULL) {
4486e062f4aSMarcel Telka 		if ((error = traverse(&cvp)) != 0)
4497c478bd9Sstevel@tonic-gate 			goto bad;
4507c478bd9Sstevel@tonic-gate 	}
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate 	/*
4537c478bd9Sstevel@tonic-gate 	 * If we hit a symbolic link and there is more path to be
4547c478bd9Sstevel@tonic-gate 	 * translated or this operation does not wish to apply
4557c478bd9Sstevel@tonic-gate 	 * to a link, then place the contents of the link at the
4567c478bd9Sstevel@tonic-gate 	 * front of the remaining pathname.
4577c478bd9Sstevel@tonic-gate 	 */
4587c478bd9Sstevel@tonic-gate 	if (cvp->v_type == VLNK && ((flags & FOLLOW) || pn_pathleft(pnp))) {
4597c478bd9Sstevel@tonic-gate 		struct pathname linkpath;
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 		if (++nlink > MAXSYMLINKS) {
4627c478bd9Sstevel@tonic-gate 			error = ELOOP;
4637c478bd9Sstevel@tonic-gate 			goto bad;
4647c478bd9Sstevel@tonic-gate 		}
4657c478bd9Sstevel@tonic-gate 		pn_alloc(&linkpath);
4667c478bd9Sstevel@tonic-gate 		if (error = pn_getsymlink(cvp, &linkpath, cr)) {
4677c478bd9Sstevel@tonic-gate 			pn_free(&linkpath);
4687c478bd9Sstevel@tonic-gate 			goto bad;
4697c478bd9Sstevel@tonic-gate 		}
4707c478bd9Sstevel@tonic-gate 
471005d3febSMarek Pospisil 		if (auditing)
4727c478bd9Sstevel@tonic-gate 			audit_symlink(pnp, &linkpath);
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate 		if (pn_pathleft(&linkpath) == 0)
4757c478bd9Sstevel@tonic-gate 			(void) pn_set(&linkpath, ".");
4767c478bd9Sstevel@tonic-gate 		error = pn_insert(pnp, &linkpath, strlen(component));
4777c478bd9Sstevel@tonic-gate 		pn_free(&linkpath);
4787c478bd9Sstevel@tonic-gate 		if (error)
4797c478bd9Sstevel@tonic-gate 			goto bad;
4807c478bd9Sstevel@tonic-gate 		VN_RELE(cvp);
4817c478bd9Sstevel@tonic-gate 		cvp = NULL;
4827c478bd9Sstevel@tonic-gate 		if (pnp->pn_pathlen == 0) {
4837c478bd9Sstevel@tonic-gate 			error = ENOENT;
4847c478bd9Sstevel@tonic-gate 			goto bad;
4857c478bd9Sstevel@tonic-gate 		}
4867c478bd9Sstevel@tonic-gate 		if (pnp->pn_path[0] == '/') {
4877c478bd9Sstevel@tonic-gate 			do {
4887c478bd9Sstevel@tonic-gate 				pnp->pn_path++;
4897c478bd9Sstevel@tonic-gate 				pnp->pn_pathlen--;
4907c478bd9Sstevel@tonic-gate 			} while (pnp->pn_path[0] == '/');
4917c478bd9Sstevel@tonic-gate 			VN_RELE(vp);
4927c478bd9Sstevel@tonic-gate 			vp = rootvp;
4937c478bd9Sstevel@tonic-gate 			VN_HOLD(vp);
4947c478bd9Sstevel@tonic-gate 		}
495005d3febSMarek Pospisil 		if (auditing)
4967c478bd9Sstevel@tonic-gate 			audit_anchorpath(pnp, vp == rootvp);
4977c478bd9Sstevel@tonic-gate 		if (pn_fixslash(pnp)) {
4987c478bd9Sstevel@tonic-gate 			flags |= FOLLOW;
4997c478bd9Sstevel@tonic-gate 			must_be_directory = 1;
5007c478bd9Sstevel@tonic-gate 		}
5017c478bd9Sstevel@tonic-gate 		goto next;
5027c478bd9Sstevel@tonic-gate 	}
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate 	/*
5057c478bd9Sstevel@tonic-gate 	 * If rpnp is non-NULL, remember the resolved path name therein.
5067c478bd9Sstevel@tonic-gate 	 * Do not include "." components.  Collapse occurrences of
5077c478bd9Sstevel@tonic-gate 	 * "previous/..", so long as "previous" is not itself "..".
5087c478bd9Sstevel@tonic-gate 	 * Exhausting rpnp results in error ENAMETOOLONG.
5097c478bd9Sstevel@tonic-gate 	 */
5107c478bd9Sstevel@tonic-gate 	if (rpnp && strcmp(component, ".") != 0) {
5117c478bd9Sstevel@tonic-gate 		size_t len;
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate 		if (strcmp(component, "..") == 0 &&
5147c478bd9Sstevel@tonic-gate 		    rpnp->pn_pathlen != 0 &&
5157c478bd9Sstevel@tonic-gate 		    !((rpnp->pn_pathlen > 2 &&
5167c478bd9Sstevel@tonic-gate 		    strncmp(rpnp->pn_path+rpnp->pn_pathlen-3, "/..", 3) == 0) ||
5177c478bd9Sstevel@tonic-gate 		    (rpnp->pn_pathlen == 2 &&
5187c478bd9Sstevel@tonic-gate 		    strncmp(rpnp->pn_path, "..", 2) == 0))) {
5197c478bd9Sstevel@tonic-gate 			while (rpnp->pn_pathlen &&
5207c478bd9Sstevel@tonic-gate 			    rpnp->pn_path[rpnp->pn_pathlen-1] != '/')
5217c478bd9Sstevel@tonic-gate 				rpnp->pn_pathlen--;
5227c478bd9Sstevel@tonic-gate 			if (rpnp->pn_pathlen > 1)
5237c478bd9Sstevel@tonic-gate 				rpnp->pn_pathlen--;
5247c478bd9Sstevel@tonic-gate 			rpnp->pn_path[rpnp->pn_pathlen] = '\0';
5257c478bd9Sstevel@tonic-gate 		} else {
5267c478bd9Sstevel@tonic-gate 			if (rpnp->pn_pathlen != 0 &&
5277c478bd9Sstevel@tonic-gate 			    rpnp->pn_path[rpnp->pn_pathlen-1] != '/')
5287c478bd9Sstevel@tonic-gate 				rpnp->pn_path[rpnp->pn_pathlen++] = '/';
529da6c28aaSamw 			if (flags & FIGNORECASE) {
530da6c28aaSamw 				/*
531da6c28aaSamw 				 * Return the case-preserved name
532da6c28aaSamw 				 * within the resolved path.
533da6c28aaSamw 				 */
534ab04eb8eStimh 				error = copystr(pp->pn_buf,
535da6c28aaSamw 				    rpnp->pn_path + rpnp->pn_pathlen,
536da6c28aaSamw 				    rpnp->pn_bufsize - rpnp->pn_pathlen, &len);
537da6c28aaSamw 			} else {
538da6c28aaSamw 				error = copystr(component,
539da6c28aaSamw 				    rpnp->pn_path + rpnp->pn_pathlen,
540da6c28aaSamw 				    rpnp->pn_bufsize - rpnp->pn_pathlen, &len);
541da6c28aaSamw 			}
5427c478bd9Sstevel@tonic-gate 			if (error)	/* copystr() returns ENAMETOOLONG */
5437c478bd9Sstevel@tonic-gate 				goto bad;
5447c478bd9Sstevel@tonic-gate 			rpnp->pn_pathlen += (len - 1);
5457c478bd9Sstevel@tonic-gate 			ASSERT(rpnp->pn_bufsize > rpnp->pn_pathlen);
5467c478bd9Sstevel@tonic-gate 		}
5477c478bd9Sstevel@tonic-gate 	}
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate 	/*
5507c478bd9Sstevel@tonic-gate 	 * If no more components, return last directory (if wanted) and
5517c478bd9Sstevel@tonic-gate 	 * last component (if wanted).
5527c478bd9Sstevel@tonic-gate 	 */
5537c478bd9Sstevel@tonic-gate 	if (pn_pathleft(pnp) == 0) {
5547c478bd9Sstevel@tonic-gate 		/*
5557c478bd9Sstevel@tonic-gate 		 * If there was a trailing slash in the pathname,
5567c478bd9Sstevel@tonic-gate 		 * make sure the last component is a directory.
5577c478bd9Sstevel@tonic-gate 		 */
5587c478bd9Sstevel@tonic-gate 		if (must_be_directory && cvp->v_type != VDIR) {
5597c478bd9Sstevel@tonic-gate 			error = ENOTDIR;
5607c478bd9Sstevel@tonic-gate 			goto bad;
5617c478bd9Sstevel@tonic-gate 		}
5627c478bd9Sstevel@tonic-gate 		if (dirvpp != NULL) {
5637c478bd9Sstevel@tonic-gate 			/*
5647c478bd9Sstevel@tonic-gate 			 * Check that we have the real parent and not
5657c478bd9Sstevel@tonic-gate 			 * an alias of the last component.
5667c478bd9Sstevel@tonic-gate 			 */
5677c478bd9Sstevel@tonic-gate 			if (vn_compare(vp, cvp)) {
568005d3febSMarek Pospisil 				if (auditing)
5694a0fa546SMarek Pospisil 					(void) audit_savepath(pnp, cvp, vp,
5702e538c1cSvv 					    EINVAL, cr);
5717c478bd9Sstevel@tonic-gate 				pn_setlast(pnp);
5727c478bd9Sstevel@tonic-gate 				VN_RELE(vp);
5737c478bd9Sstevel@tonic-gate 				VN_RELE(cvp);
5747c478bd9Sstevel@tonic-gate 				if (rootvp != rootdir)
5757c478bd9Sstevel@tonic-gate 					VN_RELE(rootvp);
576da6c28aaSamw 				if (pp)
577da6c28aaSamw 					pn_free(pp);
5787c478bd9Sstevel@tonic-gate 				return (EINVAL);
5797c478bd9Sstevel@tonic-gate 			}
5807c478bd9Sstevel@tonic-gate 			*dirvpp = vp;
5817c478bd9Sstevel@tonic-gate 		} else
5827c478bd9Sstevel@tonic-gate 			VN_RELE(vp);
583005d3febSMarek Pospisil 		if (auditing)
5844a0fa546SMarek Pospisil 			(void) audit_savepath(pnp, cvp, vp, 0, cr);
5857c478bd9Sstevel@tonic-gate 		if (pnp->pn_path == pnp->pn_buf)
5867c478bd9Sstevel@tonic-gate 			(void) pn_set(pnp, ".");
5877c478bd9Sstevel@tonic-gate 		else
5887c478bd9Sstevel@tonic-gate 			pn_setlast(pnp);
5897c478bd9Sstevel@tonic-gate 		if (rpnp) {
5907c478bd9Sstevel@tonic-gate 			if (VN_CMP(cvp, rootvp))
5917c478bd9Sstevel@tonic-gate 				(void) pn_set(rpnp, "/");
5927c478bd9Sstevel@tonic-gate 			else if (rpnp->pn_pathlen == 0)
5937c478bd9Sstevel@tonic-gate 				(void) pn_set(rpnp, ".");
5947c478bd9Sstevel@tonic-gate 		}
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate 		if (compvpp != NULL)
5977c478bd9Sstevel@tonic-gate 			*compvpp = cvp;
5987c478bd9Sstevel@tonic-gate 		else
5997c478bd9Sstevel@tonic-gate 			VN_RELE(cvp);
6007c478bd9Sstevel@tonic-gate 		if (rootvp != rootdir)
6017c478bd9Sstevel@tonic-gate 			VN_RELE(rootvp);
602da6c28aaSamw 		if (pp)
603da6c28aaSamw 			pn_free(pp);
6047c478bd9Sstevel@tonic-gate 		return (0);
6057c478bd9Sstevel@tonic-gate 	}
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate 	/*
6087c478bd9Sstevel@tonic-gate 	 * Skip over slashes from end of last component.
6097c478bd9Sstevel@tonic-gate 	 */
6107c478bd9Sstevel@tonic-gate 	while (pnp->pn_path[0] == '/') {
6117c478bd9Sstevel@tonic-gate 		pnp->pn_path++;
6127c478bd9Sstevel@tonic-gate 		pnp->pn_pathlen--;
6137c478bd9Sstevel@tonic-gate 	}
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate 	/*
6167c478bd9Sstevel@tonic-gate 	 * Searched through another level of directory:
6177c478bd9Sstevel@tonic-gate 	 * release previous directory handle and save new (result
6187c478bd9Sstevel@tonic-gate 	 * of lookup) as current directory.
6197c478bd9Sstevel@tonic-gate 	 */
6207c478bd9Sstevel@tonic-gate 	VN_RELE(vp);
6217c478bd9Sstevel@tonic-gate 	vp = cvp;
6227c478bd9Sstevel@tonic-gate 	cvp = NULL;
6237c478bd9Sstevel@tonic-gate 	goto next;
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate bad:
626005d3febSMarek Pospisil 	if (auditing)	/* reached end of path */
6274a0fa546SMarek Pospisil 		(void) audit_savepath(pnp, cvp, vp, error, cr);
6287c478bd9Sstevel@tonic-gate bad_noaudit:
6297c478bd9Sstevel@tonic-gate 	/*
6307c478bd9Sstevel@tonic-gate 	 * Error.  Release vnodes and return.
6317c478bd9Sstevel@tonic-gate 	 */
6327c478bd9Sstevel@tonic-gate 	if (cvp)
6337c478bd9Sstevel@tonic-gate 		VN_RELE(cvp);
6347c478bd9Sstevel@tonic-gate 	/*
6357c478bd9Sstevel@tonic-gate 	 * If the error was ESTALE and the current directory to look in
6367c478bd9Sstevel@tonic-gate 	 * was the root for this lookup, the root for a mounted file
6377c478bd9Sstevel@tonic-gate 	 * system, or the starting directory for lookups, then
6387c478bd9Sstevel@tonic-gate 	 * return ENOENT instead of ESTALE.  In this case, no recovery
6397c478bd9Sstevel@tonic-gate 	 * is possible by the higher level.  If ESTALE was returned for
6407c478bd9Sstevel@tonic-gate 	 * some intermediate directory along the path, then recovery
6417c478bd9Sstevel@tonic-gate 	 * is potentially possible and retrying from the higher level
6427c478bd9Sstevel@tonic-gate 	 * will either correct the situation by purging stale cache
6437c478bd9Sstevel@tonic-gate 	 * entries or eventually get back to the point where no recovery
6447c478bd9Sstevel@tonic-gate 	 * is possible.
6457c478bd9Sstevel@tonic-gate 	 */
6467c478bd9Sstevel@tonic-gate 	if (error == ESTALE &&
6477c478bd9Sstevel@tonic-gate 	    (VN_CMP(vp, rootvp) || (vp->v_flag & VROOT) || vp == startvp))
6487c478bd9Sstevel@tonic-gate 		error = ENOENT;
6497c478bd9Sstevel@tonic-gate 	VN_RELE(vp);
6507c478bd9Sstevel@tonic-gate 	if (rootvp != rootdir)
6517c478bd9Sstevel@tonic-gate 		VN_RELE(rootvp);
652da6c28aaSamw 	if (pp)
653da6c28aaSamw 		pn_free(pp);
6547c478bd9Sstevel@tonic-gate 	return (error);
6557c478bd9Sstevel@tonic-gate }
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate /*
6587c478bd9Sstevel@tonic-gate  * Traverse a mount point.  Routine accepts a vnode pointer as a reference
6597c478bd9Sstevel@tonic-gate  * parameter and performs the indirection, releasing the original vnode.
6607c478bd9Sstevel@tonic-gate  */
6617c478bd9Sstevel@tonic-gate int
traverse(vnode_t ** cvpp)6627c478bd9Sstevel@tonic-gate traverse(vnode_t **cvpp)
6637c478bd9Sstevel@tonic-gate {
6647c478bd9Sstevel@tonic-gate 	int error = 0;
6657c478bd9Sstevel@tonic-gate 	vnode_t *cvp;
6667c478bd9Sstevel@tonic-gate 	vnode_t *tvp;
6677c478bd9Sstevel@tonic-gate 	vfs_t *vfsp;
6687c478bd9Sstevel@tonic-gate 
6697c478bd9Sstevel@tonic-gate 	cvp = *cvpp;
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate 	/*
6727c478bd9Sstevel@tonic-gate 	 * If this vnode is mounted on, then we transparently indirect
6737c478bd9Sstevel@tonic-gate 	 * to the vnode which is the root of the mounted file system.
6747c478bd9Sstevel@tonic-gate 	 * Before we do this we must check that an unmount is not in
6757c478bd9Sstevel@tonic-gate 	 * progress on this vnode.
6767c478bd9Sstevel@tonic-gate 	 */
6777c478bd9Sstevel@tonic-gate 
6787c478bd9Sstevel@tonic-gate 	for (;;) {
6797c478bd9Sstevel@tonic-gate 		/*
6807c478bd9Sstevel@tonic-gate 		 * Try to read lock the vnode.  If this fails because
6817c478bd9Sstevel@tonic-gate 		 * the vnode is already write locked, then check to
6827c478bd9Sstevel@tonic-gate 		 * see whether it is the current thread which locked
6837c478bd9Sstevel@tonic-gate 		 * the vnode.  If it is not, then read lock the vnode
6847c478bd9Sstevel@tonic-gate 		 * by waiting to acquire the lock.
6857c478bd9Sstevel@tonic-gate 		 *
6867c478bd9Sstevel@tonic-gate 		 * The code path in domount() is an example of support
6877c478bd9Sstevel@tonic-gate 		 * which needs to look up two pathnames and locks one
6887c478bd9Sstevel@tonic-gate 		 * of them in between the two lookups.
6897c478bd9Sstevel@tonic-gate 		 */
6907c478bd9Sstevel@tonic-gate 		error = vn_vfsrlock(cvp);
6917c478bd9Sstevel@tonic-gate 		if (error) {
6927c478bd9Sstevel@tonic-gate 			if (!vn_vfswlock_held(cvp))
6937c478bd9Sstevel@tonic-gate 				error = vn_vfsrlock_wait(cvp);
6947c478bd9Sstevel@tonic-gate 			if (error != 0) {
6957c478bd9Sstevel@tonic-gate 				/*
6967c478bd9Sstevel@tonic-gate 				 * lookuppn() expects a held vnode to be
6977c478bd9Sstevel@tonic-gate 				 * returned because it promptly calls
6987c478bd9Sstevel@tonic-gate 				 * VN_RELE after the error return
6997c478bd9Sstevel@tonic-gate 				 */
7007c478bd9Sstevel@tonic-gate 				*cvpp = cvp;
7017c478bd9Sstevel@tonic-gate 				return (error);
7027c478bd9Sstevel@tonic-gate 			}
7037c478bd9Sstevel@tonic-gate 		}
7047c478bd9Sstevel@tonic-gate 
7057c478bd9Sstevel@tonic-gate 		/*
7067c478bd9Sstevel@tonic-gate 		 * Reached the end of the mount chain?
7077c478bd9Sstevel@tonic-gate 		 */
7087c478bd9Sstevel@tonic-gate 		vfsp = vn_mountedvfs(cvp);
7097c478bd9Sstevel@tonic-gate 		if (vfsp == NULL) {
7107c478bd9Sstevel@tonic-gate 			vn_vfsunlock(cvp);
7117c478bd9Sstevel@tonic-gate 			break;
7127c478bd9Sstevel@tonic-gate 		}
7137c478bd9Sstevel@tonic-gate 
7147c478bd9Sstevel@tonic-gate 		/*
7157c478bd9Sstevel@tonic-gate 		 * The read lock must be held across the call to VFS_ROOT() to
7167c478bd9Sstevel@tonic-gate 		 * prevent a concurrent unmount from destroying the vfs.
7177c478bd9Sstevel@tonic-gate 		 */
7187c478bd9Sstevel@tonic-gate 		error = VFS_ROOT(vfsp, &tvp);
7197c478bd9Sstevel@tonic-gate 		vn_vfsunlock(cvp);
7207c478bd9Sstevel@tonic-gate 
7217c478bd9Sstevel@tonic-gate 		if (error)
7227c478bd9Sstevel@tonic-gate 			break;
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate 		VN_RELE(cvp);
7257c478bd9Sstevel@tonic-gate 
7267c478bd9Sstevel@tonic-gate 		cvp = tvp;
7277c478bd9Sstevel@tonic-gate 	}
7287c478bd9Sstevel@tonic-gate 
7297c478bd9Sstevel@tonic-gate 	*cvpp = cvp;
7307c478bd9Sstevel@tonic-gate 	return (error);
7317c478bd9Sstevel@tonic-gate }
7327c478bd9Sstevel@tonic-gate 
7337c478bd9Sstevel@tonic-gate /*
7347c478bd9Sstevel@tonic-gate  * Return the lowermost vnode if this is a mountpoint.
7357c478bd9Sstevel@tonic-gate  */
7367c478bd9Sstevel@tonic-gate static vnode_t *
vn_under(vnode_t * vp)7377c478bd9Sstevel@tonic-gate vn_under(vnode_t *vp)
7387c478bd9Sstevel@tonic-gate {
7397c478bd9Sstevel@tonic-gate 	vnode_t *uvp;
7407c478bd9Sstevel@tonic-gate 	vfs_t *vfsp;
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate 	while (vp->v_flag & VROOT) {
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate 		vfsp = vp->v_vfsp;
7457c478bd9Sstevel@tonic-gate 		vfs_rlock_wait(vfsp);
7467c478bd9Sstevel@tonic-gate 		if ((uvp = vfsp->vfs_vnodecovered) == NULL ||
7477c478bd9Sstevel@tonic-gate 		    (vfsp->vfs_flag & VFS_UNMOUNTED)) {
7487c478bd9Sstevel@tonic-gate 			vfs_unlock(vfsp);
7497c478bd9Sstevel@tonic-gate 			break;
7507c478bd9Sstevel@tonic-gate 		}
7517c478bd9Sstevel@tonic-gate 		VN_HOLD(uvp);
7527c478bd9Sstevel@tonic-gate 		vfs_unlock(vfsp);
7537c478bd9Sstevel@tonic-gate 		VN_RELE(vp);
7547c478bd9Sstevel@tonic-gate 		vp = uvp;
7557c478bd9Sstevel@tonic-gate 	}
7567c478bd9Sstevel@tonic-gate 
7577c478bd9Sstevel@tonic-gate 	return (vp);
7587c478bd9Sstevel@tonic-gate }
7597c478bd9Sstevel@tonic-gate 
7607c478bd9Sstevel@tonic-gate static int
vnode_match(vnode_t * v1,vnode_t * v2,cred_t * cr)7617c478bd9Sstevel@tonic-gate vnode_match(vnode_t *v1, vnode_t *v2, cred_t *cr)
7627c478bd9Sstevel@tonic-gate {
7637c478bd9Sstevel@tonic-gate 	vattr_t	v1attr, v2attr;
7647c478bd9Sstevel@tonic-gate 
7657c478bd9Sstevel@tonic-gate 	/*
7667c478bd9Sstevel@tonic-gate 	 * If we have a device file, check to see if is a cloned open of the
7677c478bd9Sstevel@tonic-gate 	 * same device.  For self-cloning devices, the major numbers will match.
7687c478bd9Sstevel@tonic-gate 	 * For devices cloned through the 'clone' driver, the minor number of
7697c478bd9Sstevel@tonic-gate 	 * the source device will be the same as the major number of the cloned
7707c478bd9Sstevel@tonic-gate 	 * device.
7717c478bd9Sstevel@tonic-gate 	 */
7727c478bd9Sstevel@tonic-gate 	if ((v1->v_type == VCHR || v1->v_type == VBLK) &&
7737c478bd9Sstevel@tonic-gate 	    v1->v_type == v2->v_type) {
7747c478bd9Sstevel@tonic-gate 		if ((spec_is_selfclone(v1) || spec_is_selfclone(v2)) &&
7757c478bd9Sstevel@tonic-gate 		    getmajor(v1->v_rdev) == getmajor(v2->v_rdev))
7767c478bd9Sstevel@tonic-gate 			return (1);
7777c478bd9Sstevel@tonic-gate 
7787c478bd9Sstevel@tonic-gate 		if (spec_is_clone(v1) &&
7797c478bd9Sstevel@tonic-gate 		    getmajor(v1->v_rdev) == getminor(v2->v_rdev))
7807c478bd9Sstevel@tonic-gate 			return (1);
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate 		if (spec_is_clone(v2) &&
7837c478bd9Sstevel@tonic-gate 		    getmajor(v2->v_rdev) == getminor(v1->v_rdev))
7847c478bd9Sstevel@tonic-gate 			return (1);
7857c478bd9Sstevel@tonic-gate 	}
7867c478bd9Sstevel@tonic-gate 
7877c478bd9Sstevel@tonic-gate 	v1attr.va_mask = v2attr.va_mask = AT_TYPE;
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate 	/*
7907c478bd9Sstevel@tonic-gate 	 * This check for symbolic links handles the pseudo-symlinks in procfs.
7917c478bd9Sstevel@tonic-gate 	 * These particular links have v_type of VDIR, but the attributes have a
7927c478bd9Sstevel@tonic-gate 	 * type of VLNK.  We need to avoid these links because otherwise if we
7937c478bd9Sstevel@tonic-gate 	 * are currently in '/proc/self/fd', then '/proc/self/cwd' will compare
7947c478bd9Sstevel@tonic-gate 	 * as the same vnode.
7957c478bd9Sstevel@tonic-gate 	 */
796da6c28aaSamw 	if (VOP_GETATTR(v1, &v1attr, 0, cr, NULL) != 0 ||
797da6c28aaSamw 	    VOP_GETATTR(v2, &v2attr, 0, cr, NULL) != 0 ||
7987c478bd9Sstevel@tonic-gate 	    v1attr.va_type == VLNK || v2attr.va_type == VLNK)
7997c478bd9Sstevel@tonic-gate 		return (0);
8007c478bd9Sstevel@tonic-gate 
8017c478bd9Sstevel@tonic-gate 	v1attr.va_mask = v2attr.va_mask = AT_TYPE | AT_FSID | AT_NODEID;
8027c478bd9Sstevel@tonic-gate 
803da6c28aaSamw 	if (VOP_GETATTR(v1, &v1attr, ATTR_REAL, cr, NULL) != 0 ||
804da6c28aaSamw 	    VOP_GETATTR(v2, &v2attr, ATTR_REAL, cr, NULL) != 0)
8057c478bd9Sstevel@tonic-gate 		return (0);
8067c478bd9Sstevel@tonic-gate 
8077c478bd9Sstevel@tonic-gate 	return (v1attr.va_fsid == v2attr.va_fsid &&
8087c478bd9Sstevel@tonic-gate 	    v1attr.va_nodeid == v2attr.va_nodeid);
8097c478bd9Sstevel@tonic-gate }
8107c478bd9Sstevel@tonic-gate 
8117c478bd9Sstevel@tonic-gate 
8127c478bd9Sstevel@tonic-gate /*
8137c478bd9Sstevel@tonic-gate  * Find the entry in the directory corresponding to the target vnode.
8147c478bd9Sstevel@tonic-gate  */
8157c478bd9Sstevel@tonic-gate int
dirfindvp(vnode_t * vrootp,vnode_t * dvp,vnode_t * tvp,cred_t * cr,char * dbuf,size_t dlen,dirent64_t ** rdp)8167c478bd9Sstevel@tonic-gate dirfindvp(vnode_t *vrootp, vnode_t *dvp, vnode_t *tvp, cred_t *cr, char *dbuf,
8177c478bd9Sstevel@tonic-gate     size_t dlen, dirent64_t **rdp)
8187c478bd9Sstevel@tonic-gate {
8197c478bd9Sstevel@tonic-gate 	size_t dbuflen;
8207c478bd9Sstevel@tonic-gate 	struct iovec iov;
8217c478bd9Sstevel@tonic-gate 	struct uio uio;
822fa9e4066Sahrens 	int error;
8237c478bd9Sstevel@tonic-gate 	int eof;
8247c478bd9Sstevel@tonic-gate 	vnode_t *cmpvp;
8257c478bd9Sstevel@tonic-gate 	struct dirent64 *dp;
8267c478bd9Sstevel@tonic-gate 	pathname_t pnp;
8277c478bd9Sstevel@tonic-gate 
8287c478bd9Sstevel@tonic-gate 	ASSERT(dvp->v_type == VDIR);
8297c478bd9Sstevel@tonic-gate 
8307c478bd9Sstevel@tonic-gate 	/*
8317c478bd9Sstevel@tonic-gate 	 * This is necessary because of the strange semantics of VOP_LOOKUP().
8327c478bd9Sstevel@tonic-gate 	 */
8337c478bd9Sstevel@tonic-gate 	bzero(&pnp, sizeof (pnp));
8347c478bd9Sstevel@tonic-gate 
8357c478bd9Sstevel@tonic-gate 	uio.uio_iov = &iov;
8367c478bd9Sstevel@tonic-gate 	uio.uio_iovcnt = 1;
8377c478bd9Sstevel@tonic-gate 	uio.uio_segflg = UIO_SYSSPACE;
8387c478bd9Sstevel@tonic-gate 	uio.uio_fmode = 0;
8397c478bd9Sstevel@tonic-gate 	uio.uio_extflg = UIO_COPY_CACHED;
8407c478bd9Sstevel@tonic-gate 	uio.uio_loffset = 0;
8417c478bd9Sstevel@tonic-gate 
842da6c28aaSamw 	if ((error = VOP_ACCESS(dvp, VREAD, 0, cr, NULL)) != 0)
843fa9e4066Sahrens 		return (error);
8447c478bd9Sstevel@tonic-gate 
845c6f039c7SToomas Soome 	dp = NULL;
846c6f039c7SToomas Soome 	eof = 0;
847c6f039c7SToomas Soome 
8487c478bd9Sstevel@tonic-gate 	while (!eof) {
8497c478bd9Sstevel@tonic-gate 		uio.uio_resid = dlen;
8507c478bd9Sstevel@tonic-gate 		iov.iov_base = dbuf;
8517c478bd9Sstevel@tonic-gate 		iov.iov_len = dlen;
8527c478bd9Sstevel@tonic-gate 
8537c478bd9Sstevel@tonic-gate 		(void) VOP_RWLOCK(dvp, V_WRITELOCK_FALSE, NULL);
854da6c28aaSamw 		error = VOP_READDIR(dvp, &uio, cr, &eof, NULL, 0);
8557c478bd9Sstevel@tonic-gate 		VOP_RWUNLOCK(dvp, V_WRITELOCK_FALSE, NULL);
8567c478bd9Sstevel@tonic-gate 
8577c478bd9Sstevel@tonic-gate 		dbuflen = dlen - uio.uio_resid;
8587c478bd9Sstevel@tonic-gate 
859fa9e4066Sahrens 		if (error || dbuflen == 0)
8607c478bd9Sstevel@tonic-gate 			break;
8617c478bd9Sstevel@tonic-gate 
8627c478bd9Sstevel@tonic-gate 		dp = (dirent64_t *)dbuf;
8637c478bd9Sstevel@tonic-gate 		while ((intptr_t)dp < (intptr_t)dbuf + dbuflen) {
8647c478bd9Sstevel@tonic-gate 			/*
8657c478bd9Sstevel@tonic-gate 			 * Ignore '.' and '..' entries
8667c478bd9Sstevel@tonic-gate 			 */
8677c478bd9Sstevel@tonic-gate 			if (strcmp(dp->d_name, ".") == 0 ||
8687c478bd9Sstevel@tonic-gate 			    strcmp(dp->d_name, "..") == 0) {
8697c478bd9Sstevel@tonic-gate 				dp = (dirent64_t *)((intptr_t)dp +
8707c478bd9Sstevel@tonic-gate 				    dp->d_reclen);
8717c478bd9Sstevel@tonic-gate 				continue;
8727c478bd9Sstevel@tonic-gate 			}
8737c478bd9Sstevel@tonic-gate 
874fa9e4066Sahrens 			error = VOP_LOOKUP(dvp, dp->d_name, &cmpvp, &pnp, 0,
875da6c28aaSamw 			    vrootp, cr, NULL, NULL, NULL);
8767c478bd9Sstevel@tonic-gate 
8777c478bd9Sstevel@tonic-gate 			/*
8787c478bd9Sstevel@tonic-gate 			 * We only want to bail out if there was an error other
8797c478bd9Sstevel@tonic-gate 			 * than ENOENT.  Otherwise, it could be that someone
8807c478bd9Sstevel@tonic-gate 			 * just removed an entry since the readdir() call, and
8817c478bd9Sstevel@tonic-gate 			 * the entry we want is further on in the directory.
8827c478bd9Sstevel@tonic-gate 			 */
883fa9e4066Sahrens 			if (error == 0) {
8847c478bd9Sstevel@tonic-gate 				if (vnode_match(tvp, cmpvp, cr)) {
8857c478bd9Sstevel@tonic-gate 					VN_RELE(cmpvp);
8867c478bd9Sstevel@tonic-gate 					*rdp = dp;
8877c478bd9Sstevel@tonic-gate 					return (0);
8887c478bd9Sstevel@tonic-gate 				}
8897c478bd9Sstevel@tonic-gate 
8907c478bd9Sstevel@tonic-gate 				VN_RELE(cmpvp);
891fa9e4066Sahrens 			} else if (error != ENOENT) {
892fa9e4066Sahrens 				return (error);
8937c478bd9Sstevel@tonic-gate 			}
8947c478bd9Sstevel@tonic-gate 
8957c478bd9Sstevel@tonic-gate 			dp = (dirent64_t *)((intptr_t)dp + dp->d_reclen);
8967c478bd9Sstevel@tonic-gate 		}
8977c478bd9Sstevel@tonic-gate 	}
8987c478bd9Sstevel@tonic-gate 
8997c478bd9Sstevel@tonic-gate 	/*
9007c478bd9Sstevel@tonic-gate 	 * Something strange has happened, this directory does not contain the
9017c478bd9Sstevel@tonic-gate 	 * specified vnode.  This should never happen in the normal case, since
902fa9e4066Sahrens 	 * we ensured that dvp is the parent of vp.  This is possible in some
903fa9e4066Sahrens 	 * rare conditions (races and the special .zfs directory).
9047c478bd9Sstevel@tonic-gate 	 */
905fa9e4066Sahrens 	if (error == 0) {
906da6c28aaSamw 		error = VOP_LOOKUP(dvp, ".zfs", &cmpvp, &pnp, 0, vrootp, cr,
907da6c28aaSamw 		    NULL, NULL, NULL);
908fa9e4066Sahrens 		if (error == 0) {
909fa9e4066Sahrens 			if (vnode_match(tvp, cmpvp, cr)) {
910fa9e4066Sahrens 				(void) strcpy(dp->d_name, ".zfs");
911fa9e4066Sahrens 				dp->d_reclen = strlen(".zfs");
912fa9e4066Sahrens 				dp->d_off = 2;
913fa9e4066Sahrens 				dp->d_ino = 1;
914fa9e4066Sahrens 				*rdp = dp;
915fa9e4066Sahrens 			} else {
916fa9e4066Sahrens 				error = ENOENT;
917fa9e4066Sahrens 			}
918fa9e4066Sahrens 			VN_RELE(cmpvp);
919fa9e4066Sahrens 		}
920fa9e4066Sahrens 	}
9217c478bd9Sstevel@tonic-gate 
922fa9e4066Sahrens 	return (error);
9237c478bd9Sstevel@tonic-gate }
9247c478bd9Sstevel@tonic-gate 
9257c478bd9Sstevel@tonic-gate /*
9267c478bd9Sstevel@tonic-gate  * Given a global path (from rootdir), and a vnode that is the current root,
9277c478bd9Sstevel@tonic-gate  * return the portion of the path that is beneath the current root or NULL on
9287c478bd9Sstevel@tonic-gate  * failure.  The path MUST be a resolved path (no '..' entries or symlinks),
9297c478bd9Sstevel@tonic-gate  * otherwise this function will fail.
9307c478bd9Sstevel@tonic-gate  */
9317c478bd9Sstevel@tonic-gate static char *
localpath(char * path,struct vnode * vrootp,cred_t * cr)9327c478bd9Sstevel@tonic-gate localpath(char *path, struct vnode *vrootp, cred_t *cr)
9337c478bd9Sstevel@tonic-gate {
9347c478bd9Sstevel@tonic-gate 	vnode_t *vp;
9357c478bd9Sstevel@tonic-gate 	vnode_t *cvp;
9367c478bd9Sstevel@tonic-gate 	char component[MAXNAMELEN];
9377c478bd9Sstevel@tonic-gate 	char *ret = NULL;
9387c478bd9Sstevel@tonic-gate 	pathname_t pn;
9397c478bd9Sstevel@tonic-gate 
9407c478bd9Sstevel@tonic-gate 	/*
9417c478bd9Sstevel@tonic-gate 	 * We use vn_compare() instead of VN_CMP() in order to detect lofs
9427c478bd9Sstevel@tonic-gate 	 * mounts and stacked vnodes.
9437c478bd9Sstevel@tonic-gate 	 */
9447c478bd9Sstevel@tonic-gate 	if (vn_compare(vrootp, rootdir))
9457c478bd9Sstevel@tonic-gate 		return (path);
9467c478bd9Sstevel@tonic-gate 
9477c478bd9Sstevel@tonic-gate 	if (pn_get(path, UIO_SYSSPACE, &pn) != 0)
9487c478bd9Sstevel@tonic-gate 		return (NULL);
9497c478bd9Sstevel@tonic-gate 
9507c478bd9Sstevel@tonic-gate 	vp = rootdir;
9517c478bd9Sstevel@tonic-gate 	VN_HOLD(vp);
9527c478bd9Sstevel@tonic-gate 
9535fd6860bSMilan Cermak 	if (vn_ismntpt(vp) && traverse(&vp) != 0) {
9545fd6860bSMilan Cermak 		VN_RELE(vp);
9555fd6860bSMilan Cermak 		pn_free(&pn);
9565fd6860bSMilan Cermak 		return (NULL);
9575fd6860bSMilan Cermak 	}
9585fd6860bSMilan Cermak 
9597c478bd9Sstevel@tonic-gate 	while (pn_pathleft(&pn)) {
9607c478bd9Sstevel@tonic-gate 		pn_skipslash(&pn);
9617c478bd9Sstevel@tonic-gate 
9627c478bd9Sstevel@tonic-gate 		if (pn_getcomponent(&pn, component) != 0)
9637c478bd9Sstevel@tonic-gate 			break;
9647c478bd9Sstevel@tonic-gate 
965da6c28aaSamw 		if (VOP_LOOKUP(vp, component, &cvp, &pn, 0, rootdir, cr,
966da6c28aaSamw 		    NULL, NULL, NULL) != 0)
9677c478bd9Sstevel@tonic-gate 			break;
9687c478bd9Sstevel@tonic-gate 		VN_RELE(vp);
9697c478bd9Sstevel@tonic-gate 		vp = cvp;
9707c478bd9Sstevel@tonic-gate 
9715fd6860bSMilan Cermak 		if (vn_ismntpt(vp) && traverse(&vp) != 0)
9725fd6860bSMilan Cermak 			break;
9735fd6860bSMilan Cermak 
9747c478bd9Sstevel@tonic-gate 		if (vn_compare(vp, vrootp)) {
9757c478bd9Sstevel@tonic-gate 			ret = path + (pn.pn_path - pn.pn_buf);
9767c478bd9Sstevel@tonic-gate 			break;
9777c478bd9Sstevel@tonic-gate 		}
9787c478bd9Sstevel@tonic-gate 	}
9797c478bd9Sstevel@tonic-gate 
9807c478bd9Sstevel@tonic-gate 	VN_RELE(vp);
9817c478bd9Sstevel@tonic-gate 	pn_free(&pn);
9827c478bd9Sstevel@tonic-gate 
9837c478bd9Sstevel@tonic-gate 	return (ret);
9847c478bd9Sstevel@tonic-gate }
9857c478bd9Sstevel@tonic-gate 
986e2fc3408SPatrick Mooney /*
987e2fc3408SPatrick Mooney  * Clean a stale v_path from a vnode.  This is only performed if the v_path has
988e2fc3408SPatrick Mooney  * not been altered since it was found to be stale
989e2fc3408SPatrick Mooney  */
990e2fc3408SPatrick Mooney static void
vnode_clear_vpath(vnode_t * vp,char * vpath_old)991e2fc3408SPatrick Mooney vnode_clear_vpath(vnode_t *vp, char *vpath_old)
992e2fc3408SPatrick Mooney {
993e2fc3408SPatrick Mooney 	mutex_enter(&vp->v_lock);
994e2fc3408SPatrick Mooney 	if (vp->v_path != vn_vpath_empty && vp->v_path == vpath_old) {
995e2fc3408SPatrick Mooney 		vp->v_path = vn_vpath_empty;
996e2fc3408SPatrick Mooney 		mutex_exit(&vp->v_lock);
997e2fc3408SPatrick Mooney 		kmem_free(vpath_old, strlen(vpath_old) + 1);
998e2fc3408SPatrick Mooney 	} else {
999e2fc3408SPatrick Mooney 		mutex_exit(&vp->v_lock);
1000e2fc3408SPatrick Mooney 	}
1001e2fc3408SPatrick Mooney }
1002e2fc3408SPatrick Mooney 
1003e2fc3408SPatrick Mooney /*
1004e2fc3408SPatrick Mooney  * Validate that a pathname refers to a given vnode.
1005e2fc3408SPatrick Mooney  */
1006e2fc3408SPatrick Mooney static int
vnode_valid_pn(vnode_t * vp,vnode_t * vrootp,pathname_t * pn,pathname_t * rpn,int flags,cred_t * cr)1007e2fc3408SPatrick Mooney vnode_valid_pn(vnode_t *vp, vnode_t *vrootp, pathname_t *pn, pathname_t *rpn,
1008e2fc3408SPatrick Mooney     int flags, cred_t *cr)
1009e2fc3408SPatrick Mooney {
1010e2fc3408SPatrick Mooney 	vnode_t *compvp;
1011e2fc3408SPatrick Mooney 	/*
1012e2fc3408SPatrick Mooney 	 * If we are in a zone or a chroot environment, then we have to
1013e2fc3408SPatrick Mooney 	 * take additional steps, since the path to the root might not
1014e2fc3408SPatrick Mooney 	 * be readable with the current credentials, even though the
1015e2fc3408SPatrick Mooney 	 * process can legitmately access the file.  In this case, we
1016e2fc3408SPatrick Mooney 	 * do the following:
1017e2fc3408SPatrick Mooney 	 *
1018e2fc3408SPatrick Mooney 	 * lookuppnvp() with all privileges to get the resolved path.
1019e2fc3408SPatrick Mooney 	 * call localpath() to get the local portion of the path, and
1020e2fc3408SPatrick Mooney 	 * continue as normal.
1021e2fc3408SPatrick Mooney 	 *
1022e2fc3408SPatrick Mooney 	 * If the the conversion to a local path fails, then we continue
1023e2fc3408SPatrick Mooney 	 * as normal.  This is a heuristic to make process object file
1024e2fc3408SPatrick Mooney 	 * paths available from within a zone.  Because lofs doesn't
1025e2fc3408SPatrick Mooney 	 * support page operations, the vnode stored in the seg_t is
1026e2fc3408SPatrick Mooney 	 * actually the underlying real vnode, not the lofs node itself.
1027e2fc3408SPatrick Mooney 	 * Most of the time, the lofs path is the same as the underlying
1028e2fc3408SPatrick Mooney 	 * vnode (for example, /usr/lib/libc.so.1).
1029e2fc3408SPatrick Mooney 	 */
1030e2fc3408SPatrick Mooney 	if (vrootp != rootdir) {
1031e2fc3408SPatrick Mooney 		char *local = NULL;
1032e2fc3408SPatrick Mooney 
1033e2fc3408SPatrick Mooney 		VN_HOLD(rootdir);
1034e2fc3408SPatrick Mooney 		if (lookuppnvp(pn, rpn, FOLLOW, NULL, &compvp, rootdir,
1035e2fc3408SPatrick Mooney 		    rootdir, kcred) == 0) {
1036e2fc3408SPatrick Mooney 			local = localpath(rpn->pn_path, vrootp, kcred);
1037e2fc3408SPatrick Mooney 			VN_RELE(compvp);
1038e2fc3408SPatrick Mooney 		}
1039e2fc3408SPatrick Mooney 
1040e2fc3408SPatrick Mooney 		/*
1041e2fc3408SPatrick Mooney 		 * The original pn was changed through lookuppnvp().
1042e2fc3408SPatrick Mooney 		 * Set it to local for next validation attempt.
1043e2fc3408SPatrick Mooney 		 */
1044e2fc3408SPatrick Mooney 		if (local) {
1045e2fc3408SPatrick Mooney 			(void) pn_set(pn, local);
1046e2fc3408SPatrick Mooney 		} else {
1047e2fc3408SPatrick Mooney 			return (1);
1048e2fc3408SPatrick Mooney 		}
1049e2fc3408SPatrick Mooney 	}
1050e2fc3408SPatrick Mooney 
1051e2fc3408SPatrick Mooney 	/*
1052e2fc3408SPatrick Mooney 	 * We should have a local path at this point, so start the search from
1053e2fc3408SPatrick Mooney 	 * the root of the current process.
1054e2fc3408SPatrick Mooney 	 */
1055e2fc3408SPatrick Mooney 	VN_HOLD(vrootp);
1056e2fc3408SPatrick Mooney 	if (vrootp != rootdir)
1057e2fc3408SPatrick Mooney 		VN_HOLD(vrootp);
1058fad76a8aSMatt Barden 
1059fad76a8aSMatt Barden 	/*
1060fad76a8aSMatt Barden 	 * The FOLLOW flag only determines, if the final path component
1061fad76a8aSMatt Barden 	 * is a symlink, whether lookuppnvp will return the symlink, or its
1062fad76a8aSMatt Barden 	 * target.
1063fad76a8aSMatt Barden 	 *
1064fad76a8aSMatt Barden 	 * If the vp is a VLNK, then passing the FOLLOW flag will cause
1065fad76a8aSMatt Barden 	 * lookuppnvp to return the vnode of its target, instead of itself, and
1066fad76a8aSMatt Barden 	 * so vn_compare will fail. Therefore, we do not pass FOLLOW when our vp
1067fad76a8aSMatt Barden 	 * is a symlink.
1068fad76a8aSMatt Barden 	 *
1069fad76a8aSMatt Barden 	 * If the vp is not a VLNK, then we pass FOLLOW on the off-chance that
1070fad76a8aSMatt Barden 	 * the stored v_path ends at a symlink, instead of the symlink's target.
1071fad76a8aSMatt Barden 	 */
1072fad76a8aSMatt Barden 	if (vp->v_type != VLNK)
1073fad76a8aSMatt Barden 		flags |= FOLLOW;
1074fad76a8aSMatt Barden 	else
1075fad76a8aSMatt Barden 		flags &= ~FOLLOW;
1076fad76a8aSMatt Barden 
1077fad76a8aSMatt Barden 	if (lookuppnvp(pn, rpn, flags, NULL, &compvp, vrootp, vrootp,
1078e2fc3408SPatrick Mooney 	    cr) == 0) {
1079e2fc3408SPatrick Mooney 		/*
1080e2fc3408SPatrick Mooney 		 * Check to see if the returned vnode is the same as the one we
1081e2fc3408SPatrick Mooney 		 * expect.
1082e2fc3408SPatrick Mooney 		 */
1083e2fc3408SPatrick Mooney 		if (vn_compare(vp, compvp) ||
1084e2fc3408SPatrick Mooney 		    vnode_match(vp, compvp, cr)) {
1085e2fc3408SPatrick Mooney 			VN_RELE(compvp);
1086e2fc3408SPatrick Mooney 			return (0);
10878902f61aSPatrick Mooney 		} else {
10888902f61aSPatrick Mooney 			VN_RELE(compvp);
1089e2fc3408SPatrick Mooney 		}
1090e2fc3408SPatrick Mooney 	}
1091e2fc3408SPatrick Mooney 
1092e2fc3408SPatrick Mooney 	return (1);
1093e2fc3408SPatrick Mooney }
1094e2fc3408SPatrick Mooney 
1095e2fc3408SPatrick Mooney /*
1096e2fc3408SPatrick Mooney  * Struct for tracking vnodes with invalidated v_path entries during a
1097e2fc3408SPatrick Mooney  * dirtopath reverse lookup.  By keeping adequate state, those vnodes can be
1098e2fc3408SPatrick Mooney  * revisted to populate v_path.
1099e2fc3408SPatrick Mooney  */
1100e2fc3408SPatrick Mooney struct dirpath_walk {
1101e2fc3408SPatrick Mooney 	struct dirpath_walk	*dw_next;
1102e2fc3408SPatrick Mooney 	vnode_t			*dw_vnode;
1103e2fc3408SPatrick Mooney 	vnode_t			*dw_pvnode;
1104e2fc3408SPatrick Mooney 	size_t			dw_len;
1105e2fc3408SPatrick Mooney 	char			*dw_name;
1106e2fc3408SPatrick Mooney };
1107e2fc3408SPatrick Mooney 
11087c478bd9Sstevel@tonic-gate /*
11097c478bd9Sstevel@tonic-gate  * Given a directory, return the full, resolved path.  This looks up "..",
11107c478bd9Sstevel@tonic-gate  * searches for the given vnode in the parent, appends the component, etc.  It
11112f5706a1SWilliam.Johnston  * is used to implement vnodetopath() and getcwd() when the cached path fails.
11127c478bd9Sstevel@tonic-gate  */
11137c478bd9Sstevel@tonic-gate static int
dirtopath(vnode_t * vrootp,vnode_t * vp,char * buf,size_t buflen,int flags,cred_t * cr)11147b2df5ebSMilan Cermak dirtopath(vnode_t *vrootp, vnode_t *vp, char *buf, size_t buflen, int flags,
11157b2df5ebSMilan Cermak     cred_t *cr)
11167c478bd9Sstevel@tonic-gate {
1117e2fc3408SPatrick Mooney 	pathname_t	pn, rpn, emptypn;
1118e2fc3408SPatrick Mooney 	vnode_t		*pvp = NULL, *startvp = vp;
1119e2fc3408SPatrick Mooney 	int		err = 0;
1120e2fc3408SPatrick Mooney 	size_t		complen;
1121e2fc3408SPatrick Mooney 	dirent64_t	*dp;
1122e2fc3408SPatrick Mooney 	char		*bufloc, *dbuf;
1123e2fc3408SPatrick Mooney 	const size_t	dlen = DIRENT64_RECLEN(MAXPATHLEN);
1124e2fc3408SPatrick Mooney 	struct dirpath_walk *dw_chain = NULL, *dw_entry;
11257c478bd9Sstevel@tonic-gate 
11267c478bd9Sstevel@tonic-gate 	/* Operation only allowed on directories */
11277c478bd9Sstevel@tonic-gate 	ASSERT(vp->v_type == VDIR);
11287c478bd9Sstevel@tonic-gate 
11297c478bd9Sstevel@tonic-gate 	/* We must have at least enough space for "/" */
11307c478bd9Sstevel@tonic-gate 	if (buflen < 2)
11317c478bd9Sstevel@tonic-gate 		return (ENAMETOOLONG);
11327c478bd9Sstevel@tonic-gate 
11337c478bd9Sstevel@tonic-gate 	/* Start at end of string with terminating null */
11347c478bd9Sstevel@tonic-gate 	bufloc = &buf[buflen - 1];
11357c478bd9Sstevel@tonic-gate 	*bufloc = '\0';
11367c478bd9Sstevel@tonic-gate 
11377c478bd9Sstevel@tonic-gate 	pn_alloc(&pn);
11387c478bd9Sstevel@tonic-gate 	pn_alloc(&rpn);
11397c478bd9Sstevel@tonic-gate 	dbuf = kmem_alloc(dlen, KM_SLEEP);
11407c478bd9Sstevel@tonic-gate 	bzero(&emptypn, sizeof (emptypn));
11417c478bd9Sstevel@tonic-gate 
11427c478bd9Sstevel@tonic-gate 	/*
11437c478bd9Sstevel@tonic-gate 	 * Begin with an additional reference on vp.  This will be decremented
11447c478bd9Sstevel@tonic-gate 	 * during the loop.
11457c478bd9Sstevel@tonic-gate 	 */
11467c478bd9Sstevel@tonic-gate 	VN_HOLD(vp);
11477c478bd9Sstevel@tonic-gate 
11487c478bd9Sstevel@tonic-gate 	for (;;) {
1149e2fc3408SPatrick Mooney 		int vprivs;
1150e2fc3408SPatrick Mooney 		hrtime_t cached_stamp;
1151e2fc3408SPatrick Mooney 
11527c478bd9Sstevel@tonic-gate 		/*
11537c478bd9Sstevel@tonic-gate 		 * Return if we've reached the root.  If the buffer is empty,
11547c478bd9Sstevel@tonic-gate 		 * return '/'.  We explicitly don't use vn_compare(), since it
11557c478bd9Sstevel@tonic-gate 		 * compares the real vnodes.  A lofs mount of '/' would produce
11567c478bd9Sstevel@tonic-gate 		 * incorrect results otherwise.
11577c478bd9Sstevel@tonic-gate 		 */
11587c478bd9Sstevel@tonic-gate 		if (VN_CMP(vrootp, vp)) {
11597c478bd9Sstevel@tonic-gate 			if (*bufloc == '\0')
11607c478bd9Sstevel@tonic-gate 				*--bufloc = '/';
11617c478bd9Sstevel@tonic-gate 			break;
11627c478bd9Sstevel@tonic-gate 		}
11637c478bd9Sstevel@tonic-gate 
11647c478bd9Sstevel@tonic-gate 		/*
11657c478bd9Sstevel@tonic-gate 		 * If we've reached the VFS root, something has gone wrong.  We
11667c478bd9Sstevel@tonic-gate 		 * should have reached the root in the above check.  The only
11677c478bd9Sstevel@tonic-gate 		 * explantation is that 'vp' is not contained withing the given
11687c478bd9Sstevel@tonic-gate 		 * root, in which case we return EPERM.
11697c478bd9Sstevel@tonic-gate 		 */
11707c478bd9Sstevel@tonic-gate 		if (VN_CMP(rootdir, vp)) {
11717c478bd9Sstevel@tonic-gate 			err = EPERM;
11727c478bd9Sstevel@tonic-gate 			goto out;
11737c478bd9Sstevel@tonic-gate 		}
11747c478bd9Sstevel@tonic-gate 
1175207983b5SMilan Cermak 		/*
1176207983b5SMilan Cermak 		 * Shortcut: see if this vnode has correct v_path. If so,
1177207983b5SMilan Cermak 		 * we have the work done.
1178207983b5SMilan Cermak 		 */
1179207983b5SMilan Cermak 		mutex_enter(&vp->v_lock);
1180e2fc3408SPatrick Mooney 		if (vp->v_path != vn_vpath_empty &&
1181e2fc3408SPatrick Mooney 		    pn_set(&pn, vp->v_path) == 0) {
1182e2fc3408SPatrick Mooney 			cached_stamp = vp->v_path_stamp;
1183e2fc3408SPatrick Mooney 			mutex_exit(&vp->v_lock);
1184e2fc3408SPatrick Mooney 			rpn.pn_path = rpn.pn_buf;
1185e2fc3408SPatrick Mooney 
1186e2fc3408SPatrick Mooney 			/* Ensure the v_path pointing to correct vnode */
1187e2fc3408SPatrick Mooney 			if (vnode_valid_pn(vp, vrootp, &pn, &rpn, flags,
1188e2fc3408SPatrick Mooney 			    cr) == 0) {
1189e2fc3408SPatrick Mooney 				complen = strlen(rpn.pn_path);
1190e2fc3408SPatrick Mooney 				bufloc -= complen;
1191e2fc3408SPatrick Mooney 				if (bufloc < buf) {
1192e2fc3408SPatrick Mooney 					err = ERANGE;
1193e2fc3408SPatrick Mooney 					goto out;
1194207983b5SMilan Cermak 				}
1195e2fc3408SPatrick Mooney 				bcopy(rpn.pn_path, bufloc, complen);
1196e2fc3408SPatrick Mooney 				break;
1197207983b5SMilan Cermak 			} else {
1198e2fc3408SPatrick Mooney 				/*
1199e2fc3408SPatrick Mooney 				 * Immediately nuke cached v_path entries known
1200e2fc3408SPatrick Mooney 				 * to be invalid.
1201e2fc3408SPatrick Mooney 				 */
1202e2fc3408SPatrick Mooney 				vn_clearpath(vp, cached_stamp);
1203207983b5SMilan Cermak 			}
1204207983b5SMilan Cermak 		} else {
1205207983b5SMilan Cermak 			mutex_exit(&vp->v_lock);
1206207983b5SMilan Cermak 		}
1207207983b5SMilan Cermak 
12087c478bd9Sstevel@tonic-gate 		/*
12097c478bd9Sstevel@tonic-gate 		 * Shortcuts failed, search for this vnode in its parent.  If
12107c478bd9Sstevel@tonic-gate 		 * this is a mountpoint, then get the vnode underneath.
12117c478bd9Sstevel@tonic-gate 		 */
12127c478bd9Sstevel@tonic-gate 		if (vp->v_flag & VROOT)
12137c478bd9Sstevel@tonic-gate 			vp = vn_under(vp);
1214da6c28aaSamw 		if ((err = VOP_LOOKUP(vp, "..", &pvp, &emptypn, 0, vrootp, cr,
1215da6c28aaSamw 		    NULL, NULL, NULL)) != 0)
12167c478bd9Sstevel@tonic-gate 			goto out;
12177c478bd9Sstevel@tonic-gate 
12187c478bd9Sstevel@tonic-gate 		/*
12197c478bd9Sstevel@tonic-gate 		 * With extended attributes, it's possible for a directory to
12207c478bd9Sstevel@tonic-gate 		 * have a parent that is a regular file.  Check for that here.
12217c478bd9Sstevel@tonic-gate 		 */
12227c478bd9Sstevel@tonic-gate 		if (pvp->v_type != VDIR) {
12237c478bd9Sstevel@tonic-gate 			err = ENOTDIR;
12247c478bd9Sstevel@tonic-gate 			goto out;
12257c478bd9Sstevel@tonic-gate 		}
12267c478bd9Sstevel@tonic-gate 
12277c478bd9Sstevel@tonic-gate 		/*
12287c478bd9Sstevel@tonic-gate 		 * If this is true, something strange has happened.  This is
12297c478bd9Sstevel@tonic-gate 		 * only true if we are the root of a filesystem, which should
12307c478bd9Sstevel@tonic-gate 		 * have been caught by the check above.
12317c478bd9Sstevel@tonic-gate 		 */
12327c478bd9Sstevel@tonic-gate 		if (VN_CMP(pvp, vp)) {
12337c478bd9Sstevel@tonic-gate 			err = ENOENT;
12347c478bd9Sstevel@tonic-gate 			goto out;
12357c478bd9Sstevel@tonic-gate 		}
12367c478bd9Sstevel@tonic-gate 
12377b2df5ebSMilan Cermak 		/*
12387b2df5ebSMilan Cermak 		 * Check if we have read and search privilege so, that
12397b2df5ebSMilan Cermak 		 * we can lookup the path in the directory
12407b2df5ebSMilan Cermak 		 */
12417b2df5ebSMilan Cermak 		vprivs = (flags & LOOKUP_CHECKREAD) ? VREAD | VEXEC : VEXEC;
12427b2df5ebSMilan Cermak 		if ((err = VOP_ACCESS(pvp, vprivs, 0, cr, NULL)) != 0) {
12437b2df5ebSMilan Cermak 			goto out;
12447b2df5ebSMilan Cermak 		}
12457b2df5ebSMilan Cermak 
12467c478bd9Sstevel@tonic-gate 		/*
12477c478bd9Sstevel@tonic-gate 		 * Search the parent directory for the entry corresponding to
12487c478bd9Sstevel@tonic-gate 		 * this vnode.
12497c478bd9Sstevel@tonic-gate 		 */
12507c478bd9Sstevel@tonic-gate 		if ((err = dirfindvp(vrootp, pvp, vp, cr, dbuf, dlen, &dp))
12517c478bd9Sstevel@tonic-gate 		    != 0)
12527c478bd9Sstevel@tonic-gate 			goto out;
12537c478bd9Sstevel@tonic-gate 		complen = strlen(dp->d_name);
12547c478bd9Sstevel@tonic-gate 		bufloc -= complen;
12557c478bd9Sstevel@tonic-gate 		if (bufloc <= buf) {
12567c478bd9Sstevel@tonic-gate 			err = ENAMETOOLONG;
12577c478bd9Sstevel@tonic-gate 			goto out;
12587c478bd9Sstevel@tonic-gate 		}
12597c478bd9Sstevel@tonic-gate 		bcopy(dp->d_name, bufloc, complen);
12607c478bd9Sstevel@tonic-gate 
12617c478bd9Sstevel@tonic-gate 		/* Prepend a slash to the current path.  */
12627c478bd9Sstevel@tonic-gate 		*--bufloc = '/';
12637c478bd9Sstevel@tonic-gate 
1264e2fc3408SPatrick Mooney 		/*
1265e2fc3408SPatrick Mooney 		 * Record the name and directory for later reconstruction and
1266e2fc3408SPatrick Mooney 		 * link it up with the others.
1267e2fc3408SPatrick Mooney 		 */
1268e2fc3408SPatrick Mooney 		dw_entry = kmem_alloc(sizeof (*dw_entry), KM_SLEEP);
1269e2fc3408SPatrick Mooney 		dw_entry->dw_name = kmem_alloc(complen + 1, KM_SLEEP);
1270e2fc3408SPatrick Mooney 		VN_HOLD(dw_entry->dw_vnode = vp);
1271e2fc3408SPatrick Mooney 		VN_HOLD(dw_entry->dw_pvnode = pvp);
1272e2fc3408SPatrick Mooney 		bcopy(dp->d_name, dw_entry->dw_name, complen + 1);
1273e2fc3408SPatrick Mooney 		dw_entry->dw_len = complen;
1274e2fc3408SPatrick Mooney 		dw_entry->dw_next = dw_chain;
1275e2fc3408SPatrick Mooney 		dw_chain = dw_entry;
1276e2fc3408SPatrick Mooney 
12777c478bd9Sstevel@tonic-gate 		/* And continue with the next component */
12787c478bd9Sstevel@tonic-gate 		VN_RELE(vp);
12797c478bd9Sstevel@tonic-gate 		vp = pvp;
12807c478bd9Sstevel@tonic-gate 		pvp = NULL;
12817c478bd9Sstevel@tonic-gate 	}
12827c478bd9Sstevel@tonic-gate 
12837c478bd9Sstevel@tonic-gate 	/*
12847c478bd9Sstevel@tonic-gate 	 * Place the path at the beginning of the buffer.
12857c478bd9Sstevel@tonic-gate 	 */
12867c478bd9Sstevel@tonic-gate 	if (bufloc != buf)
12877c478bd9Sstevel@tonic-gate 		ovbcopy(bufloc, buf, buflen - (bufloc - buf));
12887c478bd9Sstevel@tonic-gate 
12897c478bd9Sstevel@tonic-gate out:
1290e2fc3408SPatrick Mooney 	/*
1291e2fc3408SPatrick Mooney 	 * Walk over encountered directory entries which were afflicted with a
1292e2fc3408SPatrick Mooney 	 * stale or absent v_path.  If the dirtopath was successful, we should
1293e2fc3408SPatrick Mooney 	 * possess the necessary information to populate all of them with a
1294e2fc3408SPatrick Mooney 	 * valid v_path.
1295e2fc3408SPatrick Mooney 	 *
1296e2fc3408SPatrick Mooney 	 * While processing this list, it is safe to call vn_setpath despite
1297e2fc3408SPatrick Mooney 	 * the fact that racing vnode actions may have altered v_path entries
1298e2fc3408SPatrick Mooney 	 * while the above loopwas still executing.  Any updated entries will
1299e2fc3408SPatrick Mooney 	 * have a newer v_path_stamp value which prevents an invalid overwrite.
1300e2fc3408SPatrick Mooney 	 *
1301e2fc3408SPatrick Mooney 	 * If an error was encountered during the search, freeing the chain is
1302e2fc3408SPatrick Mooney 	 * still required.
1303e2fc3408SPatrick Mooney 	 */
1304e2fc3408SPatrick Mooney 	dw_entry = dw_chain;
1305e2fc3408SPatrick Mooney 	while (dw_entry != NULL) {
1306e2fc3408SPatrick Mooney 		struct dirpath_walk *next = dw_entry->dw_next;
1307e2fc3408SPatrick Mooney 
1308e2fc3408SPatrick Mooney 		if (err == 0) {
1309e2fc3408SPatrick Mooney 			vn_setpath(NULL, dw_entry->dw_pvnode,
1310e2fc3408SPatrick Mooney 			    dw_entry->dw_vnode, dw_entry->dw_name,
1311e2fc3408SPatrick Mooney 			    dw_entry->dw_len);
1312e2fc3408SPatrick Mooney 		}
1313e2fc3408SPatrick Mooney 
1314e2fc3408SPatrick Mooney 		VN_RELE(dw_entry->dw_vnode);
1315e2fc3408SPatrick Mooney 		VN_RELE(dw_entry->dw_pvnode);
1316e2fc3408SPatrick Mooney 		kmem_free(dw_entry->dw_name, dw_entry->dw_len + 1);
1317e2fc3408SPatrick Mooney 		kmem_free(dw_entry, sizeof (*dw_entry));
1318e2fc3408SPatrick Mooney 		dw_entry = next;
1319e2fc3408SPatrick Mooney 	}
1320e2fc3408SPatrick Mooney 
13217c478bd9Sstevel@tonic-gate 	/*
13227c478bd9Sstevel@tonic-gate 	 * If the error was ESTALE and the current directory to look in
13237c478bd9Sstevel@tonic-gate 	 * was the root for this lookup, the root for a mounted file
13247c478bd9Sstevel@tonic-gate 	 * system, or the starting directory for lookups, then
13257c478bd9Sstevel@tonic-gate 	 * return ENOENT instead of ESTALE.  In this case, no recovery
13267c478bd9Sstevel@tonic-gate 	 * is possible by the higher level.  If ESTALE was returned for
13277c478bd9Sstevel@tonic-gate 	 * some intermediate directory along the path, then recovery
13287c478bd9Sstevel@tonic-gate 	 * is potentially possible and retrying from the higher level
13297c478bd9Sstevel@tonic-gate 	 * will either correct the situation by purging stale cache
13307c478bd9Sstevel@tonic-gate 	 * entries or eventually get back to the point where no recovery
13317c478bd9Sstevel@tonic-gate 	 * is possible.
13327c478bd9Sstevel@tonic-gate 	 */
13337c478bd9Sstevel@tonic-gate 	if (err == ESTALE &&
13347c478bd9Sstevel@tonic-gate 	    (VN_CMP(vp, vrootp) || (vp->v_flag & VROOT) || vp == startvp))
13357c478bd9Sstevel@tonic-gate 		err = ENOENT;
13367c478bd9Sstevel@tonic-gate 
13377c478bd9Sstevel@tonic-gate 	kmem_free(dbuf, dlen);
13387c478bd9Sstevel@tonic-gate 	VN_RELE(vp);
13397c478bd9Sstevel@tonic-gate 	if (pvp)
13407c478bd9Sstevel@tonic-gate 		VN_RELE(pvp);
13417c478bd9Sstevel@tonic-gate 	pn_free(&pn);
13427c478bd9Sstevel@tonic-gate 	pn_free(&rpn);
13437c478bd9Sstevel@tonic-gate 
13447c478bd9Sstevel@tonic-gate 	return (err);
13457c478bd9Sstevel@tonic-gate }
13467c478bd9Sstevel@tonic-gate 
13477c478bd9Sstevel@tonic-gate /*
1348207983b5SMilan Cermak  * The additional flag, LOOKUP_CHECKREAD, is used to enforce artificial
13497c478bd9Sstevel@tonic-gate  * constraints in order to be standards compliant.  For example, if we have
13507c478bd9Sstevel@tonic-gate  * the cached path of '/foo/bar', and '/foo' has permissions 100 (execute
13517c478bd9Sstevel@tonic-gate  * only), then we can legitimately look up the path to the current working
13527c478bd9Sstevel@tonic-gate  * directory without needing read permission.  Existing standards tests,
13537c478bd9Sstevel@tonic-gate  * however, assume that we are determining the path by repeatedly looking up
13547c478bd9Sstevel@tonic-gate  * "..".  We need to keep this behavior in order to maintain backwards
13557c478bd9Sstevel@tonic-gate  * compatibility.
13567c478bd9Sstevel@tonic-gate  */
13577c478bd9Sstevel@tonic-gate static int
vnodetopath_common(vnode_t * vrootp,vnode_t * vp,char * buf,size_t buflen,cred_t * cr,int flags)13587c478bd9Sstevel@tonic-gate vnodetopath_common(vnode_t *vrootp, vnode_t *vp, char *buf, size_t buflen,
13597c478bd9Sstevel@tonic-gate     cred_t *cr, int flags)
13607c478bd9Sstevel@tonic-gate {
1361e2fc3408SPatrick Mooney 	pathname_t pn;
1362e2fc3408SPatrick Mooney 	int ret = 0;
1363e2fc3408SPatrick Mooney 	vnode_t *realvp;
1364e2fc3408SPatrick Mooney 	boolean_t doclose = B_FALSE;
13657c478bd9Sstevel@tonic-gate 
13667c478bd9Sstevel@tonic-gate 	/*
13677c478bd9Sstevel@tonic-gate 	 * If vrootp is NULL, get the root for curproc.  Callers with any other
13687c478bd9Sstevel@tonic-gate 	 * requirements should pass in a different vrootp.
13697c478bd9Sstevel@tonic-gate 	 */
13707c478bd9Sstevel@tonic-gate 	if (vrootp == NULL) {
1371e2fc3408SPatrick Mooney 		proc_t *p = curproc;
1372e2fc3408SPatrick Mooney 
13737c478bd9Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
13747c478bd9Sstevel@tonic-gate 		if ((vrootp = PTOU(p)->u_rdir) == NULL)
13757c478bd9Sstevel@tonic-gate 			vrootp = rootdir;
13767c478bd9Sstevel@tonic-gate 		VN_HOLD(vrootp);
13777c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
13787c478bd9Sstevel@tonic-gate 	} else {
13797c478bd9Sstevel@tonic-gate 		VN_HOLD(vrootp);
13807c478bd9Sstevel@tonic-gate 	}
13817c478bd9Sstevel@tonic-gate 
13827c478bd9Sstevel@tonic-gate 	/*
13837c478bd9Sstevel@tonic-gate 	 * This is to get around an annoying artifact of the /proc filesystem,
13847c478bd9Sstevel@tonic-gate 	 * which is the behavior of {cwd/root}.  Trying to resolve this path
13857c478bd9Sstevel@tonic-gate 	 * will result in /proc/pid/cwd instead of whatever the real working
13867c478bd9Sstevel@tonic-gate 	 * directory is.  We can't rely on VOP_REALVP(), since that will break
13877c478bd9Sstevel@tonic-gate 	 * lofs.  The only difference between procfs and lofs is that opening
13887c478bd9Sstevel@tonic-gate 	 * the file will return the underling vnode in the case of procfs.
13897c478bd9Sstevel@tonic-gate 	 */
1390da6c28aaSamw 	if (vp->v_type == VDIR && VOP_REALVP(vp, &realvp, NULL) == 0 &&
13917c478bd9Sstevel@tonic-gate 	    realvp != vp) {
13927c478bd9Sstevel@tonic-gate 		VN_HOLD(vp);
1393da6c28aaSamw 		if (VOP_OPEN(&vp, FREAD, cr, NULL) == 0)
1394e2fc3408SPatrick Mooney 			doclose = B_TRUE;
13957c478bd9Sstevel@tonic-gate 		else
13967c478bd9Sstevel@tonic-gate 			VN_RELE(vp);
13977c478bd9Sstevel@tonic-gate 	}
13987c478bd9Sstevel@tonic-gate 
13997c478bd9Sstevel@tonic-gate 	/*
1400e2fc3408SPatrick Mooney 	 * Check to see if we have a valid cached path in the vnode.
14017c478bd9Sstevel@tonic-gate 	 */
1402e2fc3408SPatrick Mooney 	pn_alloc(&pn);
14037c478bd9Sstevel@tonic-gate 	mutex_enter(&vp->v_lock);
1404e2fc3408SPatrick Mooney 	if (vp->v_path != vn_vpath_empty) {
1405e2fc3408SPatrick Mooney 		hrtime_t cached_stamp;
1406e2fc3408SPatrick Mooney 		pathname_t rpn;
1407e2fc3408SPatrick Mooney 
1408e2fc3408SPatrick Mooney 		cached_stamp = vp->v_path_stamp;
1409ca2c3138Seschrock 		(void) pn_set(&pn, vp->v_path);
14107c478bd9Sstevel@tonic-gate 		mutex_exit(&vp->v_lock);
14117c478bd9Sstevel@tonic-gate 
14127c478bd9Sstevel@tonic-gate 		/* We should only cache absolute paths */
14137c478bd9Sstevel@tonic-gate 		ASSERT(pn.pn_buf[0] == '/');
14147c478bd9Sstevel@tonic-gate 
1415e2fc3408SPatrick Mooney 		pn_alloc(&rpn);
1416e2fc3408SPatrick Mooney 		if (vnode_valid_pn(vp, vrootp, &pn, &rpn, flags, cr) == 0) {
1417e2fc3408SPatrick Mooney 			/* Return the result, if we're able. */
1418e2fc3408SPatrick Mooney 			if (buflen > rpn.pn_pathlen) {
1419e2fc3408SPatrick Mooney 				bcopy(rpn.pn_path, buf, rpn.pn_pathlen + 1);
14207c478bd9Sstevel@tonic-gate 			} else {
1421e2fc3408SPatrick Mooney 				ret = ENAMETOOLONG;
14227c478bd9Sstevel@tonic-gate 			}
14237c478bd9Sstevel@tonic-gate 			pn_free(&pn);
14247c478bd9Sstevel@tonic-gate 			pn_free(&rpn);
1425e2fc3408SPatrick Mooney 			goto out;
14267c478bd9Sstevel@tonic-gate 		}
14277c478bd9Sstevel@tonic-gate 		pn_free(&rpn);
1428e2fc3408SPatrick Mooney 		vn_clearpath(vp, cached_stamp);
14297c478bd9Sstevel@tonic-gate 	} else {
14307c478bd9Sstevel@tonic-gate 		mutex_exit(&vp->v_lock);
14317c478bd9Sstevel@tonic-gate 	}
14327c478bd9Sstevel@tonic-gate 	pn_free(&pn);
14337c478bd9Sstevel@tonic-gate 
14347c478bd9Sstevel@tonic-gate 	if (vp->v_type != VDIR) {
1435e2fc3408SPatrick Mooney 		/*
1436e2fc3408SPatrick Mooney 		 * The reverse lookup tricks used by dirtopath aren't possible
1437e2fc3408SPatrick Mooney 		 * for non-directory entries.  The best which can be done is
1438e2fc3408SPatrick Mooney 		 * clearing any stale v_path so later lookups can potentially
1439e2fc3408SPatrick Mooney 		 * repopulate it with a valid path.
1440e2fc3408SPatrick Mooney 		 */
1441f176a0a4SBryan Cantrill 		ret = ENOENT;
1442f176a0a4SBryan Cantrill 	} else {
14437b2df5ebSMilan Cermak 		ret = dirtopath(vrootp, vp, buf, buflen, flags, cr);
1444f176a0a4SBryan Cantrill 	}
14457c478bd9Sstevel@tonic-gate 
1446e2fc3408SPatrick Mooney out:
14477c478bd9Sstevel@tonic-gate 	VN_RELE(vrootp);
14487c478bd9Sstevel@tonic-gate 	if (doclose) {
1449da6c28aaSamw 		(void) VOP_CLOSE(vp, FREAD, 1, 0, cr, NULL);
14507c478bd9Sstevel@tonic-gate 		VN_RELE(vp);
14517c478bd9Sstevel@tonic-gate 	}
14527c478bd9Sstevel@tonic-gate 
14537c478bd9Sstevel@tonic-gate 	return (ret);
14547c478bd9Sstevel@tonic-gate }
14557c478bd9Sstevel@tonic-gate 
14567c478bd9Sstevel@tonic-gate int
vnodetopath(vnode_t * vrootp,vnode_t * vp,char * buf,size_t buflen,cred_t * cr)14577c478bd9Sstevel@tonic-gate vnodetopath(vnode_t *vrootp, vnode_t *vp, char *buf, size_t buflen, cred_t *cr)
14587c478bd9Sstevel@tonic-gate {
14597c478bd9Sstevel@tonic-gate 	return (vnodetopath_common(vrootp, vp, buf, buflen, cr, 0));
14607c478bd9Sstevel@tonic-gate }
14617c478bd9Sstevel@tonic-gate 
14627c478bd9Sstevel@tonic-gate int
dogetcwd(char * buf,size_t buflen)14637c478bd9Sstevel@tonic-gate dogetcwd(char *buf, size_t buflen)
14647c478bd9Sstevel@tonic-gate {
14657c478bd9Sstevel@tonic-gate 	int ret;
14667c478bd9Sstevel@tonic-gate 	vnode_t *vp;
14677c478bd9Sstevel@tonic-gate 	vnode_t *compvp;
14687c478bd9Sstevel@tonic-gate 	refstr_t *cwd, *oldcwd;
14697c478bd9Sstevel@tonic-gate 	const char *value;
14707c478bd9Sstevel@tonic-gate 	pathname_t rpnp, pnp;
14717c478bd9Sstevel@tonic-gate 	proc_t *p = curproc;
14727c478bd9Sstevel@tonic-gate 
14737c478bd9Sstevel@tonic-gate 	/*
14747c478bd9Sstevel@tonic-gate 	 * Check to see if there is a cached version of the cwd.  If so, lookup
14757c478bd9Sstevel@tonic-gate 	 * the cached value and make sure it is the same vnode.
14767c478bd9Sstevel@tonic-gate 	 */
14777c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
14787c478bd9Sstevel@tonic-gate 	if ((cwd = PTOU(p)->u_cwd) != NULL)
14797c478bd9Sstevel@tonic-gate 		refstr_hold(cwd);
14807c478bd9Sstevel@tonic-gate 	vp = PTOU(p)->u_cdir;
14817c478bd9Sstevel@tonic-gate 	VN_HOLD(vp);
14827c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
14837c478bd9Sstevel@tonic-gate 
14847c478bd9Sstevel@tonic-gate 	/*
14857c478bd9Sstevel@tonic-gate 	 * Make sure we have permission to access the current directory.
14867c478bd9Sstevel@tonic-gate 	 */
1487da6c28aaSamw 	if ((ret = VOP_ACCESS(vp, VEXEC, 0, CRED(), NULL)) != 0) {
14887c478bd9Sstevel@tonic-gate 		if (cwd != NULL)
14897c478bd9Sstevel@tonic-gate 			refstr_rele(cwd);
14907c478bd9Sstevel@tonic-gate 		VN_RELE(vp);
14917c478bd9Sstevel@tonic-gate 		return (ret);
14927c478bd9Sstevel@tonic-gate 	}
14937c478bd9Sstevel@tonic-gate 
14947c478bd9Sstevel@tonic-gate 	if (cwd) {
14957c478bd9Sstevel@tonic-gate 		value = refstr_value(cwd);
14967c478bd9Sstevel@tonic-gate 		if ((ret = pn_get((char *)value, UIO_SYSSPACE, &pnp)) != 0) {
14977c478bd9Sstevel@tonic-gate 			refstr_rele(cwd);
14987c478bd9Sstevel@tonic-gate 			VN_RELE(vp);
14997c478bd9Sstevel@tonic-gate 			return (ret);
15007c478bd9Sstevel@tonic-gate 		}
15017c478bd9Sstevel@tonic-gate 
15027c478bd9Sstevel@tonic-gate 		pn_alloc(&rpnp);
15037c478bd9Sstevel@tonic-gate 
15047c478bd9Sstevel@tonic-gate 		if (lookuppn(&pnp, &rpnp, NO_FOLLOW, NULL, &compvp) == 0) {
15057c478bd9Sstevel@tonic-gate 
15067c478bd9Sstevel@tonic-gate 			if (VN_CMP(vp, compvp) &&
15077c478bd9Sstevel@tonic-gate 			    strcmp(value, rpnp.pn_path) == 0) {
15087c478bd9Sstevel@tonic-gate 				VN_RELE(compvp);
15097c478bd9Sstevel@tonic-gate 				VN_RELE(vp);
15107c478bd9Sstevel@tonic-gate 				pn_free(&pnp);
15117c478bd9Sstevel@tonic-gate 				pn_free(&rpnp);
15127c478bd9Sstevel@tonic-gate 				if (strlen(value) + 1 > buflen) {
15137c478bd9Sstevel@tonic-gate 					refstr_rele(cwd);
15147c478bd9Sstevel@tonic-gate 					return (ENAMETOOLONG);
15157c478bd9Sstevel@tonic-gate 				}
15167c478bd9Sstevel@tonic-gate 				bcopy(value, buf, strlen(value) + 1);
15177c478bd9Sstevel@tonic-gate 				refstr_rele(cwd);
15187c478bd9Sstevel@tonic-gate 				return (0);
15197c478bd9Sstevel@tonic-gate 			}
15207c478bd9Sstevel@tonic-gate 
15217c478bd9Sstevel@tonic-gate 			VN_RELE(compvp);
15227c478bd9Sstevel@tonic-gate 		}
15237c478bd9Sstevel@tonic-gate 
15247c478bd9Sstevel@tonic-gate 		pn_free(&rpnp);
15257c478bd9Sstevel@tonic-gate 		pn_free(&pnp);
15267c478bd9Sstevel@tonic-gate 
15277c478bd9Sstevel@tonic-gate 		refstr_rele(cwd);
15287c478bd9Sstevel@tonic-gate 	}
15297c478bd9Sstevel@tonic-gate 
15307c478bd9Sstevel@tonic-gate 	ret = vnodetopath_common(NULL, vp, buf, buflen, CRED(),
15317c478bd9Sstevel@tonic-gate 	    LOOKUP_CHECKREAD);
15327c478bd9Sstevel@tonic-gate 
15337c478bd9Sstevel@tonic-gate 	VN_RELE(vp);
15347c478bd9Sstevel@tonic-gate 
15357c478bd9Sstevel@tonic-gate 	/*
15367c478bd9Sstevel@tonic-gate 	 * Store the new cwd and replace the existing cached copy.
15377c478bd9Sstevel@tonic-gate 	 */
15387c478bd9Sstevel@tonic-gate 	if (ret == 0)
15397c478bd9Sstevel@tonic-gate 		cwd = refstr_alloc(buf);
15407c478bd9Sstevel@tonic-gate 	else
15417c478bd9Sstevel@tonic-gate 		cwd = NULL;
15427c478bd9Sstevel@tonic-gate 
15437c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
15447c478bd9Sstevel@tonic-gate 	oldcwd = PTOU(p)->u_cwd;
15457c478bd9Sstevel@tonic-gate 	PTOU(p)->u_cwd = cwd;
15467c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
15477c478bd9Sstevel@tonic-gate 
15487c478bd9Sstevel@tonic-gate 	if (oldcwd)
15497c478bd9Sstevel@tonic-gate 		refstr_rele(oldcwd);
15507c478bd9Sstevel@tonic-gate 
15517c478bd9Sstevel@tonic-gate 	return (ret);
15527c478bd9Sstevel@tonic-gate }
1553