Home | History | Annotate | Download | only in fs
      1      0    stevel /*
      2      0    stevel  * CDDL HEADER START
      3      0    stevel  *
      4      0    stevel  * The contents of this file are subject to the terms of the
      5   4530  vv149972  * Common Development and Distribution License (the "License").
      6   4530  vv149972  * You may not use this file except in compliance with the License.
      7      0    stevel  *
      8      0    stevel  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
      9      0    stevel  * or http://www.opensolaris.org/os/licensing.
     10      0    stevel  * See the License for the specific language governing permissions
     11      0    stevel  * and limitations under the License.
     12      0    stevel  *
     13      0    stevel  * When distributing Covered Code, include this CDDL HEADER in each
     14      0    stevel  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
     15      0    stevel  * If applicable, add the following below this CDDL HEADER, with the
     16      0    stevel  * fields enclosed by brackets "[]" replaced with your own identifying
     17      0    stevel  * information: Portions Copyright [yyyy] [name of copyright owner]
     18      0    stevel  *
     19      0    stevel  * CDDL HEADER END
     20      0    stevel  */
     21      0    stevel /*
     22   9668    Casper  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
     23      0    stevel  * Use is subject to license terms.
     24      0    stevel  */
     25      0    stevel 
     26      0    stevel /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
     27      0    stevel /*	  All Rights Reserved  	*/
     28      0    stevel 
     29      0    stevel /*
     30      0    stevel  * University Copyright- Copyright (c) 1982, 1986, 1988
     31      0    stevel  * The Regents of the University of California
     32      0    stevel  * All Rights Reserved
     33      0    stevel  *
     34      0    stevel  * University Acknowledgment- Portions of this document are derived from
     35      0    stevel  * software developed by the University of California, Berkeley, and its
     36      0    stevel  * contributors.
     37      0    stevel  */
     38      0    stevel 
     39      0    stevel #include <sys/types.h>
     40      0    stevel #include <sys/param.h>
     41      0    stevel #include <sys/systm.h>
     42      0    stevel #include <sys/cpuvar.h>
     43      0    stevel #include <sys/errno.h>
     44      0    stevel #include <sys/cred.h>
     45      0    stevel #include <sys/user.h>
     46      0    stevel #include <sys/uio.h>
     47      0    stevel #include <sys/vfs.h>
     48      0    stevel #include <sys/vnode.h>
     49      0    stevel #include <sys/pathname.h>
     50      0    stevel #include <sys/proc.h>
     51      0    stevel #include <sys/vtrace.h>
     52      0    stevel #include <sys/sysmacros.h>
     53      0    stevel #include <sys/debug.h>
     54      0    stevel #include <sys/dirent.h>
     55      0    stevel #include <c2/audit.h>
     56      0    stevel #include <sys/zone.h>
     57      0    stevel #include <sys/dnlc.h>
     58      0    stevel #include <sys/fs/snode.h>
     59      0    stevel 
     60      0    stevel /* Controls whether paths are stored with vnodes. */
     61      0    stevel int vfs_vnode_path = 1;
     62      0    stevel 
     63      0    stevel int
     64      0    stevel lookupname(
     65      0    stevel 	char *fnamep,
     66      0    stevel 	enum uio_seg seg,
     67      0    stevel 	enum symfollow followlink,
     68      0    stevel 	vnode_t **dirvpp,
     69      0    stevel 	vnode_t **compvpp)
     70      0    stevel {
     71   9668    Casper 	return (lookupnameatcred(fnamep, seg, followlink, dirvpp, compvpp, NULL,
     72   9668    Casper 	    CRED()));
     73      0    stevel }
     74      0    stevel 
     75      0    stevel /*
     76      0    stevel  * Lookup the user file name,
     77      0    stevel  * Handle allocation and freeing of pathname buffer, return error.
     78      0    stevel  */
     79      0    stevel int
     80   9668    Casper lookupnameatcred(
     81      0    stevel 	char *fnamep,			/* user pathname */
     82      0    stevel 	enum uio_seg seg,		/* addr space that name is in */
     83      0    stevel 	enum symfollow followlink,	/* follow sym links */
     84      0    stevel 	vnode_t **dirvpp,		/* ret for ptr to parent dir vnode */
     85      0    stevel 	vnode_t **compvpp,		/* ret for ptr to component vnode */
     86   9668    Casper 	vnode_t *startvp,		/* start path search from vp */
     87   9668    Casper 	cred_t *cr)			/* credential */
     88      0    stevel {
     89      0    stevel 	char namebuf[TYPICALMAXPATHLEN];
     90      0    stevel 	struct pathname lookpn;
     91      0    stevel 	int error;
     92      0    stevel 
     93      0    stevel 	error = pn_get_buf(fnamep, seg, &lookpn, namebuf, sizeof (namebuf));
     94      0    stevel 	if (error == 0) {
     95      0    stevel 		if (audit_active)
     96      0    stevel 			audit_lookupname();
     97   9668    Casper 		error = lookuppnatcred(&lookpn, NULL, followlink,
     98   9668    Casper 		    dirvpp, compvpp, startvp, cr);
     99      0    stevel 	}
    100      0    stevel 	if (error == ENAMETOOLONG) {
    101      0    stevel 		/*
    102      0    stevel 		 * This thread used a pathname > TYPICALMAXPATHLEN bytes long.
    103      0    stevel 		 */
    104      0    stevel 		if (error = pn_get(fnamep, seg, &lookpn))
    105      0    stevel 			return (error);
    106   9668    Casper 		error = lookuppnatcred(&lookpn, NULL, followlink,
    107   9668    Casper 		    dirvpp, compvpp, startvp, cr);
    108      0    stevel 		pn_free(&lookpn);
    109      0    stevel 	}
    110      0    stevel 
    111      0    stevel 	return (error);
    112      0    stevel }
    113      0    stevel 
    114   9668    Casper int
    115   9668    Casper lookupnameat(char *fnamep, enum uio_seg seg, enum symfollow followlink,
    116   9668    Casper     vnode_t **dirvpp, vnode_t **compvpp, vnode_t *startvp)
    117   9668    Casper {
    118   9668    Casper 	return (lookupnameatcred(fnamep, seg, followlink, dirvpp, compvpp,
    119   9668    Casper 	    startvp, CRED()));
    120   9668    Casper }
    121   9668    Casper 
    122      0    stevel int
    123      0    stevel lookuppn(
    124      0    stevel 	struct pathname *pnp,
    125      0    stevel 	struct pathname *rpnp,
    126      0    stevel 	enum symfollow followlink,
    127      0    stevel 	vnode_t **dirvpp,
    128      0    stevel 	vnode_t **compvpp)
    129      0    stevel {
    130   9668    Casper 	return (lookuppnatcred(pnp, rpnp, followlink, dirvpp, compvpp, NULL,
    131   9668    Casper 	    CRED()));
    132      0    stevel }
    133      0    stevel 
    134   9668    Casper /*
    135   9668    Casper  * Lookup the user file name from a given vp, using a specific credential.
    136   9668    Casper  */
    137      0    stevel int
    138   9668    Casper lookuppnatcred(
    139      0    stevel 	struct pathname *pnp,		/* pathname to lookup */
    140      0    stevel 	struct pathname *rpnp,		/* if non-NULL, return resolved path */
    141      0    stevel 	enum symfollow followlink,	/* (don't) follow sym links */
    142      0    stevel 	vnode_t **dirvpp,		/* ptr for parent vnode */
    143      0    stevel 	vnode_t **compvpp,		/* ptr for entry vnode */
    144   9668    Casper 	vnode_t *startvp,		/* start search from this vp */
    145   9668    Casper 	cred_t *cr)			/* user credential */
    146      0    stevel {
    147      0    stevel 	vnode_t *vp;	/* current directory vp */
    148      0    stevel 	vnode_t *rootvp;
    149      0    stevel 	proc_t *p = curproc;
    150      0    stevel 
    151      0    stevel 	if (pnp->pn_pathlen == 0)
    152      0    stevel 		return (ENOENT);
    153      0    stevel 
    154      0    stevel 	mutex_enter(&p->p_lock);	/* for u_rdir and u_cdir */
    155      0    stevel 	if ((rootvp = PTOU(p)->u_rdir) == NULL)
    156      0    stevel 		rootvp = rootdir;
    157      0    stevel 	else if (rootvp != rootdir)	/* no need to VN_HOLD rootdir */
    158      0    stevel 		VN_HOLD(rootvp);
    159      0    stevel 
    160      0    stevel 	if (pnp->pn_path[0] == '/') {
    161      0    stevel 		vp = rootvp;
    162      0    stevel 	} else {
    163      0    stevel 		vp = (startvp == NULL) ? PTOU(p)->u_cdir : startvp;
    164      0    stevel 	}
    165      0    stevel 	VN_HOLD(vp);
    166      0    stevel 	mutex_exit(&p->p_lock);
    167      0    stevel 
    168      0    stevel 	/*
    169      0    stevel 	 * Skip over leading slashes
    170      0    stevel 	 */
    171      0    stevel 	if (pnp->pn_path[0] == '/') {
    172      0    stevel 		do {
    173      0    stevel 			pnp->pn_path++;
    174      0    stevel 			pnp->pn_pathlen--;
    175      0    stevel 		} while (pnp->pn_path[0] == '/');
    176      0    stevel 	}
    177      0    stevel 
    178      0    stevel 	return (lookuppnvp(pnp, rpnp, followlink, dirvpp,
    179   9668    Casper 	    compvpp, rootvp, vp, cr));
    180   9668    Casper }
    181   9668    Casper 
    182   9668    Casper int
    183   9668    Casper lookuppnat(struct pathname *pnp, struct pathname *rpnp,
    184   9668    Casper     enum symfollow followlink, vnode_t **dirvpp, vnode_t **compvpp,
    185   9668    Casper     vnode_t *startvp)
    186   9668    Casper {
    187   9668    Casper 	return (lookuppnatcred(pnp, rpnp, followlink, dirvpp, compvpp, startvp,
    188   9668    Casper 	    CRED()));
    189      0    stevel }
    190      0    stevel 
    191      0    stevel /* Private flag to do our getcwd() dirty work */
    192      0    stevel #define	LOOKUP_CHECKREAD	0x10
    193      0    stevel #define	LOOKUP_MASK		(~LOOKUP_CHECKREAD)
    194      0    stevel 
    195      0    stevel /*
    196      0    stevel  * Starting at current directory, translate pathname pnp to end.
    197      0    stevel  * Leave pathname of final component in pnp, return the vnode
    198      0    stevel  * for the final component in *compvpp, and return the vnode
    199      0    stevel  * for the parent of the final component in dirvpp.
    200      0    stevel  *
    201      0    stevel  * This is the central routine in pathname translation and handles
    202      0    stevel  * multiple components in pathnames, separating them at /'s.  It also
    203      0    stevel  * implements mounted file systems and processes symbolic links.
    204      0    stevel  *
    205      0    stevel  * vp is the vnode where the directory search should start.
    206      0    stevel  *
    207      0    stevel  * Reference counts: vp must be held prior to calling this function.  rootvp
    208      0    stevel  * should only be held if rootvp != rootdir.
    209      0    stevel  */
    210      0    stevel int
    211      0    stevel lookuppnvp(
    212      0    stevel 	struct pathname *pnp,		/* pathname to lookup */
    213      0    stevel 	struct pathname *rpnp,		/* if non-NULL, return resolved path */
    214      0    stevel 	int flags,			/* follow symlinks */
    215      0    stevel 	vnode_t **dirvpp,		/* ptr for parent vnode */
    216      0    stevel 	vnode_t **compvpp,		/* ptr for entry vnode */
    217      0    stevel 	vnode_t *rootvp,		/* rootvp */
    218      0    stevel 	vnode_t *vp,			/* directory to start search at */
    219      0    stevel 	cred_t *cr)			/* user's credential */
    220      0    stevel {
    221      0    stevel 	vnode_t *cvp;	/* current component vp */
    222      0    stevel 	vnode_t *tvp;	/* addressable temp ptr */
    223      0    stevel 	char component[MAXNAMELEN];	/* buffer for component (incl null) */
    224      0    stevel 	int error;
    225      0    stevel 	int nlink;
    226      0    stevel 	int lookup_flags;
    227   5331       amw 	struct pathname presrvd; /* case preserved name */
    228   5331       amw 	struct pathname *pp = NULL;
    229      0    stevel 	vnode_t *startvp;
    230      0    stevel 	vnode_t *zonevp = curproc->p_zone->zone_rootvp;		/* zone root */
    231      0    stevel 	int must_be_directory = 0;
    232  11089   Vallish 	boolean_t retry_with_kcred;
    233      0    stevel 
    234      0    stevel 	CPU_STATS_ADDQ(CPU, sys, namei, 1);
    235      0    stevel 	nlink = 0;
    236      0    stevel 	cvp = NULL;
    237      0    stevel 	if (rpnp)
    238      0    stevel 		rpnp->pn_pathlen = 0;
    239   5331       amw 
    240      0    stevel 	lookup_flags = dirvpp ? LOOKUP_DIR : 0;
    241   5331       amw 	if (flags & FIGNORECASE) {
    242   5331       amw 		lookup_flags |= FIGNORECASE;
    243   5331       amw 		pn_alloc(&presrvd);
    244   5331       amw 		pp = &presrvd;
    245   5331       amw 	}
    246   5331       amw 
    247      0    stevel 	if (audit_active)
    248      0    stevel 		audit_anchorpath(pnp, vp == rootvp);
    249      0    stevel 
    250      0    stevel 	/*
    251      0    stevel 	 * Eliminate any trailing slashes in the pathname.
    252      0    stevel 	 * If there are any, we must follow all symlinks.
    253      0    stevel 	 * Also, we must guarantee that the last component is a directory.
    254      0    stevel 	 */
    255      0    stevel 	if (pn_fixslash(pnp)) {
    256      0    stevel 		flags |= FOLLOW;
    257      0    stevel 		must_be_directory = 1;
    258      0    stevel 	}
    259      0    stevel 
    260      0    stevel 	startvp = vp;
    261      0    stevel next:
    262  11089   Vallish 	retry_with_kcred = B_FALSE;
    263  11089   Vallish 
    264      0    stevel 	/*
    265      0    stevel 	 * Make sure we have a directory.
    266      0    stevel 	 */
    267      0    stevel 	if (vp->v_type != VDIR) {
    268      0    stevel 		error = ENOTDIR;
    269      0    stevel 		goto bad;
    270      0    stevel 	}
    271      0    stevel 
    272      0    stevel 	if (rpnp && VN_CMP(vp, rootvp))
    273      0    stevel 		(void) pn_set(rpnp, "/");
    274      0    stevel 
    275      0    stevel 	/*
    276      0    stevel 	 * Process the next component of the pathname.
    277      0    stevel 	 */
    278      0    stevel 	if (error = pn_getcomponent(pnp, component)) {
    279      0    stevel 		if (audit_active)
    280      0    stevel 			audit_addcomponent(pnp);
    281      0    stevel 		goto bad;
    282      0    stevel 	}
    283      0    stevel 
    284      0    stevel 	/*
    285      0    stevel 	 * Handle "..": two special cases.
    286      0    stevel 	 * 1. If we're at the root directory (e.g. after chroot or
    287      0    stevel 	 *    zone_enter) then change ".." to "." so we can't get
    288      0    stevel 	 *    out of this subtree.
    289      0    stevel 	 * 2. If this vnode is the root of a mounted file system,
    290      0    stevel 	 *    then replace it with the vnode that was mounted on
    291      0    stevel 	 *    so that we take the ".." in the other file system.
    292      0    stevel 	 */
    293      0    stevel 	if (component[0] == '.' && component[1] == '.' && component[2] == 0) {
    294      0    stevel checkforroot:
    295      0    stevel 		if (VN_CMP(vp, rootvp) || VN_CMP(vp, zonevp)) {
    296      0    stevel 			component[1] = '\0';
    297      0    stevel 		} else if (vp->v_flag & VROOT) {
    298      0    stevel 			vfs_t *vfsp;
    299      0    stevel 			cvp = vp;
    300      0    stevel 
    301      0    stevel 			/*
    302      0    stevel 			 * While we deal with the vfs pointer from the vnode
    303      0    stevel 			 * the filesystem could have been forcefully unmounted
    304      0    stevel 			 * and the vnode's v_vfsp could have been invalidated
    305      0    stevel 			 * by VFS_UNMOUNT. Hence, we cache v_vfsp and use it
    306      0    stevel 			 * with vfs_rlock_wait/vfs_unlock.
    307      0    stevel 			 * It is safe to use the v_vfsp even it is freed by
    308      0    stevel 			 * VFS_UNMOUNT because vfs_rlock_wait/vfs_unlock
    309      0    stevel 			 * do not dereference v_vfsp. It is just used as a
    310      0    stevel 			 * magic cookie.
    311      0    stevel 			 * One more corner case here is the memory getting
    312      0    stevel 			 * reused for another vfs structure. In this case
    313      0    stevel 			 * lookuppnvp's vfs_rlock_wait will succeed, domount's
    314      0    stevel 			 * vfs_lock will fail and domount will bail out with an
    315      0    stevel 			 * error (EBUSY).
    316      0    stevel 			 */
    317      0    stevel 			vfsp = cvp->v_vfsp;
    318      0    stevel 
    319      0    stevel 			/*
    320      0    stevel 			 * This lock is used to synchronize
    321      0    stevel 			 * mounts/unmounts and lookups.
    322      0    stevel 			 * Threads doing mounts/unmounts hold the
    323      0    stevel 			 * writers version vfs_lock_wait().
    324      0    stevel 			 */
    325      0    stevel 
    326      0    stevel 			vfs_rlock_wait(vfsp);
    327      0    stevel 
    328      0    stevel 			/*
    329      0    stevel 			 * If this vnode is on a file system that
    330      0    stevel 			 * has been forcibly unmounted,
    331      0    stevel 			 * we can't proceed. Cancel this operation
    332      0    stevel 			 * and return EIO.
    333      0    stevel 			 *
    334      0    stevel 			 * vfs_vnodecovered is NULL if unmounted.
    335      0    stevel 			 * Currently, nfs uses VFS_UNMOUNTED to
    336      0    stevel 			 * check if it's a forced-umount. Keep the
    337      0    stevel 			 * same checking here as well even though it
    338      0    stevel 			 * may not be needed.
    339      0    stevel 			 */
    340      0    stevel 			if (((vp = cvp->v_vfsp->vfs_vnodecovered) == NULL) ||
    341      0    stevel 			    (cvp->v_vfsp->vfs_flag & VFS_UNMOUNTED)) {
    342      0    stevel 				vfs_unlock(vfsp);
    343      0    stevel 				VN_RELE(cvp);
    344   5331       amw 				if (pp)
    345   5331       amw 					pn_free(pp);
    346      0    stevel 				return (EIO);
    347      0    stevel 			}
    348      0    stevel 			VN_HOLD(vp);
    349      0    stevel 			vfs_unlock(vfsp);
    350      0    stevel 			VN_RELE(cvp);
    351      0    stevel 			cvp = NULL;
    352   4530  vv149972 			/*
    353   4530  vv149972 			 * Crossing mount points. For eg: We are doing
    354   4530  vv149972 			 * a lookup of ".." for file systems root vnode
    355   4530  vv149972 			 * mounted here, and VOP_LOOKUP() (with covered vnode)
    356   4530  vv149972 			 * will be on underlying file systems mount point
    357   4530  vv149972 			 * vnode. Set retry_with_kcred flag as we might end
    358   4530  vv149972 			 * up doing VOP_LOOKUP() with kcred if required.
    359   4530  vv149972 			 */
    360   4530  vv149972 			retry_with_kcred = B_TRUE;
    361      0    stevel 			goto checkforroot;
    362      0    stevel 		}
    363      0    stevel 	}
    364      0    stevel 
    365      0    stevel 	/*
    366      0    stevel 	 * LOOKUP_CHECKREAD is a private flag used by vnodetopath() to indicate
    367      0    stevel 	 * that we need to have read permission on every directory in the entire
    368      0    stevel 	 * path.  This is used to ensure that a forward-lookup of a cached value
    369      0    stevel 	 * has the same effect as a reverse-lookup when the cached value cannot
    370      0    stevel 	 * be found.
    371      0    stevel 	 */
    372      0    stevel 	if ((flags & LOOKUP_CHECKREAD) &&
    373   5331       amw 	    (error = VOP_ACCESS(vp, VREAD, 0, cr, NULL)) != 0)
    374      0    stevel 		goto bad;
    375      0    stevel 
    376      0    stevel 	/*
    377      0    stevel 	 * Perform a lookup in the current directory.
    378      0    stevel 	 */
    379      0    stevel 	error = VOP_LOOKUP(vp, component, &tvp, pnp, lookup_flags,
    380   5331       amw 	    rootvp, cr, NULL, NULL, pp);
    381   4530  vv149972 
    382   4530  vv149972 	/*
    383   4530  vv149972 	 * Retry with kcred - If crossing mount points & error is EACCES.
    384   4530  vv149972 	 *
    385   4530  vv149972 	 * If we are crossing mount points here and doing ".." lookup,
    386   4530  vv149972 	 * VOP_LOOKUP() might fail if the underlying file systems
    387   4530  vv149972 	 * mount point has no execute permission. In cases like these,
    388   4530  vv149972 	 * we retry VOP_LOOKUP() by giving as much privilage as possible
    389   4530  vv149972 	 * by passing kcred credentials.
    390   4530  vv149972 	 *
    391   4530  vv149972 	 * In case of hierarchical file systems, passing kcred still may
    392   4530  vv149972 	 * or may not work.
    393   4530  vv149972 	 * For eg: UFS FS --> Mount NFS FS --> Again mount UFS on some
    394   4530  vv149972 	 *			directory inside NFS FS.
    395   4530  vv149972 	 */
    396   4530  vv149972 	if ((error == EACCES) && retry_with_kcred)
    397   4530  vv149972 		error = VOP_LOOKUP(vp, component, &tvp, pnp, lookup_flags,
    398   5331       amw 		    rootvp, zone_kcred(), NULL, NULL, pp);
    399   4530  vv149972 
    400      0    stevel 	cvp = tvp;
    401      0    stevel 	if (error) {
    402      0    stevel 		cvp = NULL;
    403      0    stevel 		/*
    404      0    stevel 		 * On error, return hard error if
    405      0    stevel 		 * (a) we're not at the end of the pathname yet, or
    406      0    stevel 		 * (b) the caller didn't want the parent directory, or
    407      0    stevel 		 * (c) we failed for some reason other than a missing entry.
    408      0    stevel 		 */
    409      0    stevel 		if (pn_pathleft(pnp) || dirvpp == NULL || error != ENOENT)
    410      0    stevel 			goto bad;
    411      0    stevel 		if (audit_active) {	/* directory access */
    412      0    stevel 			if (error = audit_savepath(pnp, vp, error, cr))
    413      0    stevel 				goto bad_noaudit;
    414      0    stevel 		}
    415      0    stevel 		pn_setlast(pnp);
    416      0    stevel 		/*
    417      0    stevel 		 * We inform the caller that the desired entry must be
    418      0    stevel 		 * a directory by adding a '/' to the component name.
    419      0    stevel 		 */
    420      0    stevel 		if (must_be_directory && (error = pn_addslash(pnp)) != 0)
    421      0    stevel 			goto bad;
    422      0    stevel 		*dirvpp = vp;
    423      0    stevel 		if (compvpp != NULL)
    424      0    stevel 			*compvpp = NULL;
    425      0    stevel 		if (rootvp != rootdir)
    426      0    stevel 			VN_RELE(rootvp);
    427   5331       amw 		if (pp)
    428   5331       amw 			pn_free(pp);
    429      0    stevel 		return (0);
    430      0    stevel 	}
    431      0    stevel 
    432      0    stevel 	/*
    433      0    stevel 	 * Traverse mount points.
    434      0    stevel 	 * XXX why don't we need to hold a read lock here (call vn_vfsrlock)?
    435      0    stevel 	 * What prevents a concurrent update to v_vfsmountedhere?
    436      0    stevel 	 * 	Possible answer: if mounting, we might not see the mount
    437      0    stevel 	 *	if it is concurrently coming into existence, but that's
    438      0    stevel 	 *	really not much different from the thread running a bit slower.
    439      0    stevel 	 *	If unmounting, we may get into traverse() when we shouldn't,
    440      0    stevel 	 *	but traverse() will catch this case for us.
    441      0    stevel 	 *	(For this to work, fetching v_vfsmountedhere had better
    442      0    stevel 	 *	be atomic!)
    443      0    stevel 	 */
    444      0    stevel 	if (vn_mountedvfs(cvp) != NULL) {
    445      0    stevel 		tvp = cvp;
    446      0    stevel 		if ((error = traverse(&tvp)) != 0) {
    447      0    stevel 			/*
    448      0    stevel 			 * It is required to assign cvp here, because
    449      0    stevel 			 * traverse() will return a held vnode which
    450      0    stevel 			 * may different than the vnode that was passed
    451      0    stevel 			 * in (even in the error case).  If traverse()
    452      0    stevel 			 * changes the vnode it releases the original,
    453      0    stevel 			 * and holds the new one.
    454      0    stevel 			 */
    455      0    stevel 			cvp = tvp;
    456      0    stevel 			goto bad;
    457      0    stevel 		}
    458      0    stevel 		cvp = tvp;
    459      0    stevel 	}
    460      0    stevel 
    461      0    stevel 	/*
    462      0    stevel 	 * If we hit a symbolic link and there is more path to be
    463      0    stevel 	 * translated or this operation does not wish to apply
    464      0    stevel 	 * to a link, then place the contents of the link at the
    465      0    stevel 	 * front of the remaining pathname.
    466      0    stevel 	 */
    467      0    stevel 	if (cvp->v_type == VLNK && ((flags & FOLLOW) || pn_pathleft(pnp))) {
    468      0    stevel 		struct pathname linkpath;
    469      0    stevel 		if (audit_active) {
    470      0    stevel 			if (error = audit_pathcomp(pnp, cvp, cr))
    471      0    stevel 				goto bad;
    472      0    stevel 		}
    473      0    stevel 
    474      0    stevel 		if (++nlink > MAXSYMLINKS) {
    475      0    stevel 			error = ELOOP;
    476      0    stevel 			goto bad;
    477      0    stevel 		}
    478      0    stevel 		pn_alloc(&linkpath);
    479      0    stevel 		if (error = pn_getsymlink(cvp, &linkpath, cr)) {
    480      0    stevel 			pn_free(&linkpath);
    481      0    stevel 			goto bad;
    482      0    stevel 		}
    483      0    stevel 
    484      0    stevel 		if (audit_active)
    485      0    stevel 			audit_symlink(pnp, &linkpath);
    486      0    stevel 
    487      0    stevel 		if (pn_pathleft(&linkpath) == 0)
    488      0    stevel 			(void) pn_set(&linkpath, ".");
    489      0    stevel 		error = pn_insert(pnp, &linkpath, strlen(component));
    490      0    stevel 		pn_free(&linkpath);
    491      0    stevel 		if (error)
    492      0    stevel 			goto bad;
    493      0    stevel 		VN_RELE(cvp);
    494      0    stevel 		cvp = NULL;
    495      0    stevel 		if (pnp->pn_pathlen == 0) {
    496      0    stevel 			error = ENOENT;
    497      0    stevel 			goto bad;
    498      0    stevel 		}
    499      0    stevel 		if (pnp->pn_path[0] == '/') {
    500      0    stevel 			do {
    501      0    stevel 				pnp->pn_path++;
    502      0    stevel 				pnp->pn_pathlen--;
    503      0    stevel 			} while (pnp->pn_path[0] == '/');
    504      0    stevel 			VN_RELE(vp);
    505      0    stevel 			vp = rootvp;
    506      0    stevel 			VN_HOLD(vp);
    507      0    stevel 		}
    508      0    stevel 		if (audit_active)
    509      0    stevel 			audit_anchorpath(pnp, vp == rootvp);
    510      0    stevel 		if (pn_fixslash(pnp)) {
    511      0    stevel 			flags |= FOLLOW;
    512      0    stevel 			must_be_directory = 1;
    513      0    stevel 		}
    514      0    stevel 		goto next;
    515      0    stevel 	}
    516      0    stevel 
    517      0    stevel 	/*
    518      0    stevel 	 * If rpnp is non-NULL, remember the resolved path name therein.
    519      0    stevel 	 * Do not include "." components.  Collapse occurrences of
    520      0    stevel 	 * "previous/..", so long as "previous" is not itself "..".
    521      0    stevel 	 * Exhausting rpnp results in error ENAMETOOLONG.
    522      0    stevel 	 */
    523      0    stevel 	if (rpnp && strcmp(component, ".") != 0) {
    524      0    stevel 		size_t len;
    525      0    stevel 
    526      0    stevel 		if (strcmp(component, "..") == 0 &&
    527      0    stevel 		    rpnp->pn_pathlen != 0 &&
    528      0    stevel 		    !((rpnp->pn_pathlen > 2 &&
    529      0    stevel 		    strncmp(rpnp->pn_path+rpnp->pn_pathlen-3, "/..", 3) == 0) ||
    530      0    stevel 		    (rpnp->pn_pathlen == 2 &&
    531      0    stevel 		    strncmp(rpnp->pn_path, "..", 2) == 0))) {
    532      0    stevel 			while (rpnp->pn_pathlen &&
    533      0    stevel 			    rpnp->pn_path[rpnp->pn_pathlen-1] != '/')
    534      0    stevel 				rpnp->pn_pathlen--;
    535      0    stevel 			if (rpnp->pn_pathlen > 1)
    536      0    stevel 				rpnp->pn_pathlen--;
    537      0    stevel 			rpnp->pn_path[rpnp->pn_pathlen] = '\0';
    538      0    stevel 		} else {
    539      0    stevel 			if (rpnp->pn_pathlen != 0 &&
    540      0    stevel 			    rpnp->pn_path[rpnp->pn_pathlen-1] != '/')
    541      0    stevel 				rpnp->pn_path[rpnp->pn_pathlen++] = '/';
    542   5331       amw 			if (flags & FIGNORECASE) {
    543   5331       amw 				/*
    544   5331       amw 				 * Return the case-preserved name
    545   5331       amw 				 * within the resolved path.
    546   5331       amw 				 */
    547   6492      timh 				error = copystr(pp->pn_buf,
    548   5331       amw 				    rpnp->pn_path + rpnp->pn_pathlen,
    549   5331       amw 				    rpnp->pn_bufsize - rpnp->pn_pathlen, &len);
    550   5331       amw 			} else {
    551   5331       amw 				error = copystr(component,
    552   5331       amw 				    rpnp->pn_path + rpnp->pn_pathlen,
    553   5331       amw 				    rpnp->pn_bufsize - rpnp->pn_pathlen, &len);
    554   5331       amw 			}
    555      0    stevel 			if (error)	/* copystr() returns ENAMETOOLONG */
    556      0    stevel 				goto bad;
    557      0    stevel 			rpnp->pn_pathlen += (len - 1);
    558      0    stevel 			ASSERT(rpnp->pn_bufsize > rpnp->pn_pathlen);
    559      0    stevel 		}
    560      0    stevel 	}
    561      0    stevel 
    562      0    stevel 	/*
    563      0    stevel 	 * If no more components, return last directory (if wanted) and
    564      0    stevel 	 * last component (if wanted).
    565      0    stevel 	 */
    566      0    stevel 	if (pn_pathleft(pnp) == 0) {
    567      0    stevel 		/*
    568      0    stevel 		 * If there was a trailing slash in the pathname,
    569      0    stevel 		 * make sure the last component is a directory.
    570      0    stevel 		 */
    571      0    stevel 		if (must_be_directory && cvp->v_type != VDIR) {
    572      0    stevel 			error = ENOTDIR;
    573      0    stevel 			goto bad;
    574      0    stevel 		}
    575      0    stevel 		if (dirvpp != NULL) {
    576      0    stevel 			/*
    577      0    stevel 			 * Check that we have the real parent and not
    578      0    stevel 			 * an alias of the last component.
    579      0    stevel 			 */
    580      0    stevel 			if (vn_compare(vp, cvp)) {
    581      0    stevel 				if (audit_active)
    582      0    stevel 					(void) audit_savepath(pnp, cvp,
    583   4530  vv149972 					    EINVAL, cr);
    584      0    stevel 				pn_setlast(pnp);
    585      0    stevel 				VN_RELE(vp);
    586      0    stevel 				VN_RELE(cvp);
    587      0    stevel 				if (rootvp != rootdir)
    588      0    stevel 					VN_RELE(rootvp);
    589   5331       amw 				if (pp)
    590   5331       amw 					pn_free(pp);
    591      0    stevel 				return (EINVAL);
    592      0    stevel 			}
    593      0    stevel 			if (audit_active) {
    594      0    stevel 				if (error = audit_pathcomp(pnp, vp, cr))
    595      0    stevel 					goto bad;
    596      0    stevel 			}
    597      0    stevel 			*dirvpp = vp;
    598      0    stevel 		} else
    599      0    stevel 			VN_RELE(vp);
    600      0    stevel 		if (audit_active)
    601      0    stevel 			(void) audit_savepath(pnp, cvp, 0, cr);
    602      0    stevel 		if (pnp->pn_path == pnp->pn_buf)
    603      0    stevel 			(void) pn_set(pnp, ".");
    604      0    stevel 		else
    605      0    stevel 			pn_setlast(pnp);
    606      0    stevel 		if (rpnp) {
    607      0    stevel 			if (VN_CMP(cvp, rootvp))
    608      0    stevel 				(void) pn_set(rpnp, "/");
    609      0    stevel 			else if (rpnp->pn_pathlen == 0)
    610      0    stevel 				(void) pn_set(rpnp, ".");
    611      0    stevel 		}
    612      0    stevel 
    613      0    stevel 		if (compvpp != NULL)
    614      0    stevel 			*compvpp = cvp;
    615      0    stevel 		else
    616      0    stevel 			VN_RELE(cvp);
    617      0    stevel 		if (rootvp != rootdir)
    618      0    stevel 			VN_RELE(rootvp);
    619   5331       amw 		if (pp)
    620   5331       amw 			pn_free(pp);
    621      0    stevel 		return (0);
    622      0    stevel 	}
    623      0    stevel 
    624      0    stevel 	if (audit_active) {
    625      0    stevel 		if (error = audit_pathcomp(pnp, cvp, cr))
    626      0    stevel 			goto bad;
    627      0    stevel 	}
    628      0    stevel 
    629      0    stevel 	/*
    630      0    stevel 	 * Skip over slashes from end of last component.
    631      0    stevel 	 */
    632      0    stevel 	while (pnp->pn_path[0] == '/') {
    633      0    stevel 		pnp->pn_path++;
    634      0    stevel 		pnp->pn_pathlen--;
    635      0    stevel 	}
    636      0    stevel 
    637      0    stevel 	/*
    638      0    stevel 	 * Searched through another level of directory:
    639      0    stevel 	 * release previous directory handle and save new (result
    640      0    stevel 	 * of lookup) as current directory.
    641      0    stevel 	 */
    642      0    stevel 	VN_RELE(vp);
    643      0    stevel 	vp = cvp;
    644      0    stevel 	cvp = NULL;
    645      0    stevel 	goto next;
    646      0    stevel 
    647      0    stevel bad:
    648      0    stevel 	if (audit_active)	/* reached end of path */
    649      0    stevel 		(void) audit_savepath(pnp, cvp, error, cr);
    650      0    stevel bad_noaudit:
    651      0    stevel 	/*
    652      0    stevel 	 * Error.  Release vnodes and return.
    653      0    stevel 	 */
    654      0    stevel 	if (cvp)
    655      0    stevel 		VN_RELE(cvp);
    656      0    stevel 	/*
    657      0    stevel 	 * If the error was ESTALE and the current directory to look in
    658      0    stevel 	 * was the root for this lookup, the root for a mounted file
    659      0    stevel 	 * system, or the starting directory for lookups, then
    660      0    stevel 	 * return ENOENT instead of ESTALE.  In this case, no recovery
    661      0    stevel 	 * is possible by the higher level.  If ESTALE was returned for
    662      0    stevel 	 * some intermediate directory along the path, then recovery
    663      0    stevel 	 * is potentially possible and retrying from the higher level
    664      0    stevel 	 * will either correct the situation by purging stale cache
    665      0    stevel 	 * entries or eventually get back to the point where no recovery
    666      0    stevel 	 * is possible.
    667      0    stevel 	 */
    668      0    stevel 	if (error == ESTALE &&
    669      0    stevel 	    (VN_CMP(vp, rootvp) || (vp->v_flag & VROOT) || vp == startvp))
    670      0    stevel 		error = ENOENT;
    671      0    stevel 	VN_RELE(vp);
    672      0    stevel 	if (rootvp != rootdir)
    673      0    stevel 		VN_RELE(rootvp);
    674   5331       amw 	if (pp)
    675   5331       amw 		pn_free(pp);
    676      0    stevel 	return (error);
    677      0    stevel }
    678      0    stevel 
    679      0    stevel /*
    680      0    stevel  * Traverse a mount point.  Routine accepts a vnode pointer as a reference
    681      0    stevel  * parameter and performs the indirection, releasing the original vnode.
    682      0    stevel  */
    683      0    stevel int
    684      0    stevel traverse(vnode_t **cvpp)
    685      0    stevel {
    686      0    stevel 	int error = 0;
    687      0    stevel 	vnode_t *cvp;
    688      0    stevel 	vnode_t *tvp;
    689      0    stevel 	vfs_t *vfsp;
    690      0    stevel 
    691      0    stevel 	cvp = *cvpp;
    692      0    stevel 
    693      0    stevel 	/*
    694      0    stevel 	 * If this vnode is mounted on, then we transparently indirect
    695      0    stevel 	 * to the vnode which is the root of the mounted file system.
    696      0    stevel 	 * Before we do this we must check that an unmount is not in
    697      0    stevel 	 * progress on this vnode.
    698      0    stevel 	 */
    699      0    stevel 
    700      0    stevel 	for (;;) {
    701      0    stevel 		/*
    702      0    stevel 		 * Try to read lock the vnode.  If this fails because
    703      0    stevel 		 * the vnode is already write locked, then check to
    704      0    stevel 		 * see whether it is the current thread which locked
    705      0    stevel 		 * the vnode.  If it is not, then read lock the vnode
    706      0    stevel 		 * by waiting to acquire the lock.
    707      0    stevel 		 *
    708      0    stevel 		 * The code path in domount() is an example of support
    709      0    stevel 		 * which needs to look up two pathnames and locks one
    710      0    stevel 		 * of them in between the two lookups.
    711      0    stevel 		 */
    712      0    stevel 		error = vn_vfsrlock(cvp);
    713      0    stevel 		if (error) {
    714      0    stevel 			if (!vn_vfswlock_held(cvp))
    715      0    stevel 				error = vn_vfsrlock_wait(cvp);
    716      0    stevel 			if (error != 0) {
    717      0    stevel 				/*
    718      0    stevel 				 * lookuppn() expects a held vnode to be
    719      0    stevel 				 * returned because it promptly calls
    720      0    stevel 				 * VN_RELE after the error return
    721      0    stevel 				 */
    722      0    stevel 				*cvpp = cvp;
    723      0    stevel 				return (error);
    724      0    stevel 			}
    725      0    stevel 		}
    726      0    stevel 
    727      0    stevel 		/*
    728      0    stevel 		 * Reached the end of the mount chain?
    729      0    stevel 		 */
    730      0    stevel 		vfsp = vn_mountedvfs(cvp);
    731      0    stevel 		if (vfsp == NULL) {
    732      0    stevel 			vn_vfsunlock(cvp);
    733      0    stevel 			break;
    734      0    stevel 		}
    735      0    stevel 
    736      0    stevel 		/*
    737      0    stevel 		 * The read lock must be held across the call to VFS_ROOT() to
    738      0    stevel 		 * prevent a concurrent unmount from destroying the vfs.
    739      0    stevel 		 */
    740      0    stevel 		error = VFS_ROOT(vfsp, &tvp);
    741      0    stevel 		vn_vfsunlock(cvp);
    742      0    stevel 
    743      0    stevel 		if (error)
    744      0    stevel 			break;
    745      0    stevel 
    746      0    stevel 		VN_RELE(cvp);
    747      0    stevel 
    748      0    stevel 		cvp = tvp;
    749      0    stevel 	}
    750      0    stevel 
    751      0    stevel 	*cvpp = cvp;
    752      0    stevel 	return (error);
    753      0    stevel }
    754      0    stevel 
    755      0    stevel /*
    756      0    stevel  * Return the lowermost vnode if this is a mountpoint.
    757      0    stevel  */
    758      0    stevel static vnode_t *
    759      0    stevel vn_under(vnode_t *vp)
    760      0    stevel {
    761      0    stevel 	vnode_t *uvp;
    762      0    stevel 	vfs_t *vfsp;
    763      0    stevel 
    764      0    stevel 	while (vp->v_flag & VROOT) {
    765      0    stevel 
    766      0    stevel 		vfsp = vp->v_vfsp;
    767      0    stevel 		vfs_rlock_wait(vfsp);
    768      0    stevel 		if ((uvp = vfsp->vfs_vnodecovered) == NULL ||
    769      0    stevel 		    (vfsp->vfs_flag & VFS_UNMOUNTED)) {
    770      0    stevel 			vfs_unlock(vfsp);
    771      0    stevel 			break;
    772      0    stevel 		}
    773      0    stevel 		VN_HOLD(uvp);
    774      0    stevel 		vfs_unlock(vfsp);
    775      0    stevel 		VN_RELE(vp);
    776      0    stevel 		vp = uvp;
    777      0    stevel 	}
    778      0    stevel 
    779      0    stevel 	return (vp);
    780      0    stevel }
    781      0    stevel 
    782      0    stevel static int
    783      0    stevel vnode_match(vnode_t *v1, vnode_t *v2, cred_t *cr)
    784      0    stevel {
    785      0    stevel 	vattr_t	v1attr, v2attr;
    786      0    stevel 
    787      0    stevel 	/*
    788      0    stevel 	 * If we have a device file, check to see if is a cloned open of the
    789      0    stevel 	 * same device.  For self-cloning devices, the major numbers will match.
    790      0    stevel 	 * For devices cloned through the 'clone' driver, the minor number of
    791      0    stevel 	 * the source device will be the same as the major number of the cloned
    792      0    stevel 	 * device.
    793      0    stevel 	 */
    794      0    stevel 	if ((v1->v_type == VCHR || v1->v_type == VBLK) &&
    795      0    stevel 	    v1->v_type == v2->v_type) {
    796      0    stevel 		if ((spec_is_selfclone(v1) || spec_is_selfclone(v2)) &&
    797      0    stevel 		    getmajor(v1->v_rdev) == getmajor(v2->v_rdev))
    798      0    stevel 			return (1);
    799      0    stevel 
    800      0    stevel 		if (spec_is_clone(v1) &&
    801      0    stevel 		    getmajor(v1->v_rdev) == getminor(v2->v_rdev))
    802      0    stevel 			return (1);
    803      0    stevel 
    804      0    stevel 		if (spec_is_clone(v2) &&
    805      0    stevel 		    getmajor(v2->v_rdev) == getminor(v1->v_rdev))
    806      0    stevel 			return (1);
    807      0    stevel 	}
    808      0    stevel 
    809      0    stevel 	v1attr.va_mask = v2attr.va_mask = AT_TYPE;
    810      0    stevel 
    811      0    stevel 	/*
    812      0    stevel 	 * This check for symbolic links handles the pseudo-symlinks in procfs.
    813      0    stevel 	 * These particular links have v_type of VDIR, but the attributes have a
    814      0    stevel 	 * type of VLNK.  We need to avoid these links because otherwise if we
    815      0    stevel 	 * are currently in '/proc/self/fd', then '/proc/self/cwd' will compare
    816      0    stevel 	 * as the same vnode.
    817      0    stevel 	 */
    818   5331       amw 	if (VOP_GETATTR(v1, &v1attr, 0, cr, NULL) != 0 ||
    819   5331       amw 	    VOP_GETATTR(v2, &v2attr, 0, cr, NULL) != 0 ||
    820      0    stevel 	    v1attr.va_type == VLNK || v2attr.va_type == VLNK)
    821      0    stevel 		return (0);
    822      0    stevel 
    823      0    stevel 	v1attr.va_mask = v2attr.va_mask = AT_TYPE | AT_FSID | AT_NODEID;
    824      0    stevel 
    825   5331       amw 	if (VOP_GETATTR(v1, &v1attr, ATTR_REAL, cr, NULL) != 0 ||
    826   5331       amw 	    VOP_GETATTR(v2, &v2attr, ATTR_REAL, cr, NULL) != 0)
    827      0    stevel 		return (0);
    828      0    stevel 
    829      0    stevel 	return (v1attr.va_fsid == v2attr.va_fsid &&
    830      0    stevel 	    v1attr.va_nodeid == v2attr.va_nodeid);
    831      0    stevel }
    832      0    stevel 
    833      0    stevel 
    834      0    stevel /*
    835      0    stevel  * Find the entry in the directory corresponding to the target vnode.
    836      0    stevel  */
    837      0    stevel int
    838      0    stevel dirfindvp(vnode_t *vrootp, vnode_t *dvp, vnode_t *tvp, cred_t *cr, char *dbuf,
    839      0    stevel     size_t dlen, dirent64_t **rdp)
    840      0    stevel {
    841      0    stevel 	size_t dbuflen;
    842      0    stevel 	struct iovec iov;
    843      0    stevel 	struct uio uio;
    844    789    ahrens 	int error;
    845      0    stevel 	int eof;
    846      0    stevel 	vnode_t *cmpvp;
    847      0    stevel 	struct dirent64 *dp;
    848      0    stevel 	pathname_t pnp;
    849      0    stevel 
    850      0    stevel 	ASSERT(dvp->v_type == VDIR);
    851      0    stevel 
    852      0    stevel 	/*
    853      0    stevel 	 * This is necessary because of the strange semantics of VOP_LOOKUP().
    854      0    stevel 	 */
    855      0    stevel 	bzero(&pnp, sizeof (pnp));
    856      0    stevel 
    857      0    stevel 	eof = 0;
    858      0    stevel 
    859      0    stevel 	uio.uio_iov = &iov;
    860      0    stevel 	uio.uio_iovcnt = 1;
    861      0    stevel 	uio.uio_segflg = UIO_SYSSPACE;
    862      0    stevel 	uio.uio_fmode = 0;
    863      0    stevel 	uio.uio_extflg = UIO_COPY_CACHED;
    864      0    stevel 	uio.uio_loffset = 0;
    865      0    stevel 
    866   5331       amw 	if ((error = VOP_ACCESS(dvp, VREAD, 0, cr, NULL)) != 0)
    867    789    ahrens 		return (error);
    868      0    stevel 
    869      0    stevel 	while (!eof) {
    870      0    stevel 		uio.uio_resid = dlen;
    871      0    stevel 		iov.iov_base = dbuf;
    872      0    stevel 		iov.iov_len = dlen;
    873      0    stevel 
    874      0    stevel 		(void) VOP_RWLOCK(dvp, V_WRITELOCK_FALSE, NULL);
    875   5331       amw 		error = VOP_READDIR(dvp, &uio, cr, &eof, NULL, 0);
    876      0    stevel 		VOP_RWUNLOCK(dvp, V_WRITELOCK_FALSE, NULL);
    877      0    stevel 
    878      0    stevel 		dbuflen = dlen - uio.uio_resid;
    879      0    stevel 
    880    789    ahrens 		if (error || dbuflen == 0)
    881      0    stevel 			break;
    882      0    stevel 
    883      0    stevel 		dp = (dirent64_t *)dbuf;
    884      0    stevel 		while ((intptr_t)dp < (intptr_t)dbuf + dbuflen) {
    885      0    stevel 			/*
    886      0    stevel 			 * Ignore '.' and '..' entries
    887      0    stevel 			 */
    888      0    stevel 			if (strcmp(dp->d_name, ".") == 0 ||
    889      0    stevel 			    strcmp(dp->d_name, "..") == 0) {
    890      0    stevel 				dp = (dirent64_t *)((intptr_t)dp +
    891      0    stevel 				    dp->d_reclen);
    892      0    stevel 				continue;
    893      0    stevel 			}
    894      0    stevel 
    895    789    ahrens 			error = VOP_LOOKUP(dvp, dp->d_name, &cmpvp, &pnp, 0,
    896   5331       amw 			    vrootp, cr, NULL, NULL, NULL);
    897      0    stevel 
    898      0    stevel 			/*
    899      0    stevel 			 * We only want to bail out if there was an error other
    900      0    stevel 			 * than ENOENT.  Otherwise, it could be that someone
    901      0    stevel 			 * just removed an entry since the readdir() call, and
    902      0    stevel 			 * the entry we want is further on in the directory.
    903      0    stevel 			 */
    904    789    ahrens 			if (error == 0) {
    905      0    stevel 				if (vnode_match(tvp, cmpvp, cr)) {
    906      0    stevel 					VN_RELE(cmpvp);
    907      0    stevel 					*rdp = dp;
    908      0    stevel 					return (0);
    909      0    stevel 				}
    910      0    stevel 
    911      0    stevel 				VN_RELE(cmpvp);
    912    789    ahrens 			} else if (error != ENOENT) {
    913    789    ahrens 				return (error);
    914      0    stevel 			}
    915      0    stevel 
    916      0    stevel 			dp = (dirent64_t *)((intptr_t)dp + dp->d_reclen);
    917      0    stevel 		}
    918      0    stevel 	}
    919      0    stevel 
    920      0    stevel 	/*
    921      0    stevel 	 * Something strange has happened, this directory does not contain the
    922      0    stevel 	 * specified vnode.  This should never happen in the normal case, since
    923    789    ahrens 	 * we ensured that dvp is the parent of vp.  This is possible in some
    924    789    ahrens 	 * rare conditions (races and the special .zfs directory).
    925      0    stevel 	 */
    926    789    ahrens 	if (error == 0) {
    927   5331       amw 		error = VOP_LOOKUP(dvp, ".zfs", &cmpvp, &pnp, 0, vrootp, cr,
    928   5331       amw 		    NULL, NULL, NULL);
    929    789    ahrens 		if (error == 0) {
    930    789    ahrens 			if (vnode_match(tvp, cmpvp, cr)) {
    931    789    ahrens 				(void) strcpy(dp->d_name, ".zfs");
    932    789    ahrens 				dp->d_reclen = strlen(".zfs");
    933    789    ahrens 				dp->d_off = 2;
    934    789    ahrens 				dp->d_ino = 1;
    935    789    ahrens 				*rdp = dp;
    936    789    ahrens 			} else {
    937    789    ahrens 				error = ENOENT;
    938    789    ahrens 			}
    939    789    ahrens 			VN_RELE(cmpvp);
    940    789    ahrens 		}
    941    789    ahrens 	}
    942      0    stevel 
    943    789    ahrens 	return (error);
    944      0    stevel }
    945      0    stevel 
    946      0    stevel /*
    947      0    stevel  * Given a global path (from rootdir), and a vnode that is the current root,
    948      0    stevel  * return the portion of the path that is beneath the current root or NULL on
    949      0    stevel  * failure.  The path MUST be a resolved path (no '..' entries or symlinks),
    950      0    stevel  * otherwise this function will fail.
    951      0    stevel  */
    952      0    stevel static char *
    953      0    stevel localpath(char *path, struct vnode *vrootp, cred_t *cr)
    954      0    stevel {
    955      0    stevel 	vnode_t *vp;
    956      0    stevel 	vnode_t *cvp;
    957      0    stevel 	char component[MAXNAMELEN];
    958      0    stevel 	char *ret = NULL;
    959      0    stevel 	pathname_t pn;
    960      0    stevel 
    961      0    stevel 	/*
    962      0    stevel 	 * We use vn_compare() instead of VN_CMP() in order to detect lofs
    963      0    stevel 	 * mounts and stacked vnodes.
    964      0    stevel 	 */
    965      0    stevel 	if (vn_compare(vrootp, rootdir))
    966      0    stevel 		return (path);
    967      0    stevel 
    968      0    stevel 	if (pn_get(path, UIO_SYSSPACE, &pn) != 0)
    969      0    stevel 		return (NULL);
    970      0    stevel 
    971      0    stevel 	vp = rootdir;
    972      0    stevel 	VN_HOLD(vp);
    973      0    stevel 
    974   7795     Milan 	if (vn_ismntpt(vp) && traverse(&vp) != 0) {
    975   7795     Milan 		VN_RELE(vp);
    976   7795     Milan 		pn_free(&pn);
    977   7795     Milan 		return (NULL);
    978   7795     Milan 	}
    979   7795     Milan 
    980      0    stevel 	while (pn_pathleft(&pn)) {
    981      0    stevel 		pn_skipslash(&pn);
    982      0    stevel 
    983      0    stevel 		if (pn_getcomponent(&pn, component) != 0)
    984      0    stevel 			break;
    985      0    stevel 
    986   5331       amw 		if (VOP_LOOKUP(vp, component, &cvp, &pn, 0, rootdir, cr,
    987   5331       amw 		    NULL, NULL, NULL) != 0)
    988      0    stevel 			break;
    989      0    stevel 		VN_RELE(vp);
    990      0    stevel 		vp = cvp;
    991   7795     Milan 
    992   7795     Milan 		if (vn_ismntpt(vp) && traverse(&vp) != 0)
    993   7795     Milan 			break;
    994      0    stevel 
    995      0    stevel 		if (vn_compare(vp, vrootp)) {
    996      0    stevel 			ret = path + (pn.pn_path - pn.pn_buf);
    997      0    stevel 			break;
    998      0    stevel 		}
    999      0    stevel 	}
   1000      0    stevel 
   1001      0    stevel 	VN_RELE(vp);
   1002      0    stevel 	pn_free(&pn);
   1003      0    stevel 
   1004      0    stevel 	return (ret);
   1005      0    stevel }
   1006      0    stevel 
   1007      0    stevel /*
   1008      0    stevel  * Given a directory, return the full, resolved path.  This looks up "..",
   1009      0    stevel  * searches for the given vnode in the parent, appends the component, etc.  It
   1010      0    stevel  * is used to implement vnodetopath() and getcwd() when the cached path fails
   1011      0    stevel  * (or vfs_vnode_path is not set).
   1012      0    stevel  */
   1013      0    stevel static int
   1014  10808     Milan dirtopath(vnode_t *vrootp, vnode_t *vp, char *buf, size_t buflen, int flags,
   1015  10808     Milan     cred_t *cr)
   1016      0    stevel {
   1017      0    stevel 	pathname_t pn, rpn, emptypn;
   1018      0    stevel 	vnode_t *cmpvp, *pvp = NULL;
   1019      0    stevel 	vnode_t *startvp = vp;
   1020  10808     Milan 	int err = 0, vprivs;
   1021      0    stevel 	size_t complen;
   1022      0    stevel 	char *dbuf;
   1023      0    stevel 	dirent64_t *dp;
   1024      0    stevel 	char		*bufloc;
   1025      0    stevel 	size_t		dlen = DIRENT64_RECLEN(MAXPATHLEN);
   1026      0    stevel 	refstr_t	*mntpt;
   1027      0    stevel 
   1028      0    stevel 	/* Operation only allowed on directories */
   1029      0    stevel 	ASSERT(vp->v_type == VDIR);
   1030      0    stevel 
   1031      0    stevel 	/* We must have at least enough space for "/" */
   1032      0    stevel 	if (buflen < 2)
   1033      0    stevel 		return (ENAMETOOLONG);
   1034      0    stevel 
   1035      0    stevel 	/* Start at end of string with terminating null */
   1036      0    stevel 	bufloc = &buf[buflen - 1];
   1037      0    stevel 	*bufloc = '\0';
   1038      0    stevel 
   1039      0    stevel 	pn_alloc(&pn);
   1040      0    stevel 	pn_alloc(&rpn);
   1041      0    stevel 	dbuf = kmem_alloc(dlen, KM_SLEEP);
   1042      0    stevel 	bzero(&emptypn, sizeof (emptypn));
   1043      0    stevel 
   1044      0    stevel 	/*
   1045      0    stevel 	 * Begin with an additional reference on vp.  This will be decremented
   1046      0    stevel 	 * during the loop.
   1047      0    stevel 	 */
   1048      0    stevel 	VN_HOLD(vp);
   1049      0    stevel 
   1050      0    stevel 	for (;;) {
   1051      0    stevel 		/*
   1052      0    stevel 		 * Return if we've reached the root.  If the buffer is empty,
   1053      0    stevel 		 * return '/'.  We explicitly don't use vn_compare(), since it
   1054      0    stevel 		 * compares the real vnodes.  A lofs mount of '/' would produce
   1055      0    stevel 		 * incorrect results otherwise.
   1056      0    stevel 		 */
   1057      0    stevel 		if (VN_CMP(vrootp, vp)) {
   1058      0    stevel 			if (*bufloc == '\0')
   1059      0    stevel 				*--bufloc = '/';
   1060      0    stevel 			break;
   1061      0    stevel 		}
   1062      0    stevel 
   1063      0    stevel 		/*
   1064      0    stevel 		 * If we've reached the VFS root, something has gone wrong.  We
   1065      0    stevel 		 * should have reached the root in the above check.  The only
   1066      0    stevel 		 * explantation is that 'vp' is not contained withing the given
   1067      0    stevel 		 * root, in which case we return EPERM.
   1068      0    stevel 		 */
   1069      0    stevel 		if (VN_CMP(rootdir, vp)) {
   1070      0    stevel 			err = EPERM;
   1071      0    stevel 			goto out;
   1072      0    stevel 		}
   1073      0    stevel 
   1074      0    stevel 		/*
   1075      0    stevel 		 * Shortcut: see if this vnode is a mountpoint.  If so,
   1076      0    stevel 		 * grab the path information from the vfs_t.
   1077      0    stevel 		 */
   1078      0    stevel 		if (vp->v_flag & VROOT) {
   1079      0    stevel 
   1080      0    stevel 			mntpt = vfs_getmntpoint(vp->v_vfsp);
   1081      0    stevel 			if ((err = pn_set(&pn, (char *)refstr_value(mntpt)))
   1082      0    stevel 			    == 0) {
   1083      0    stevel 				refstr_rele(mntpt);
   1084      0    stevel 				rpn.pn_path = rpn.pn_buf;
   1085      0    stevel 
   1086      0    stevel 				/*
   1087   8741     Milan 				 * Ensure the mountpoint still exists.
   1088      0    stevel 				 */
   1089      0    stevel 				VN_HOLD(vrootp);
   1090      0    stevel 				if (vrootp != rootdir)
   1091      0    stevel 					VN_HOLD(vrootp);
   1092  10808     Milan 				if (lookuppnvp(&pn, &rpn, flags, NULL,
   1093      0    stevel 				    &cmpvp, vrootp, vrootp, cr) == 0) {
   1094      0    stevel 
   1095      0    stevel 					if (VN_CMP(vp, cmpvp)) {
   1096      0    stevel 						VN_RELE(cmpvp);
   1097      0    stevel 
   1098      0    stevel 						complen = strlen(rpn.pn_path);
   1099      0    stevel 						bufloc -= complen;
   1100      0    stevel 						if (bufloc < buf) {
   1101      0    stevel 							err = ERANGE;
   1102      0    stevel 							goto out;
   1103      0    stevel 						}
   1104      0    stevel 						bcopy(rpn.pn_path, bufloc,
   1105      0    stevel 						    complen);
   1106      0    stevel 						break;
   1107      0    stevel 					} else {
   1108      0    stevel 						VN_RELE(cmpvp);
   1109      0    stevel 					}
   1110      0    stevel 				}
   1111      0    stevel 			} else {
   1112      0    stevel 				refstr_rele(mntpt);
   1113      0    stevel 			}
   1114   8741     Milan 		}
   1115   8741     Milan 
   1116   8741     Milan 		/*
   1117   8741     Milan 		 * Shortcut: see if this vnode has correct v_path. If so,
   1118   8741     Milan 		 * we have the work done.
   1119   8741     Milan 		 */
   1120   8741     Milan 		mutex_enter(&vp->v_lock);
   1121   8741     Milan 		if (vp->v_path != NULL) {
   1122   8741     Milan 
   1123   8741     Milan 			if ((err = pn_set(&pn, vp->v_path)) == 0) {
   1124   8741     Milan 				mutex_exit(&vp->v_lock);
   1125   8741     Milan 				rpn.pn_path = rpn.pn_buf;
   1126   8741     Milan 
   1127   8741     Milan 				/*
   1128   8741     Milan 				 * Ensure the v_path pointing to correct vnode
   1129   8741     Milan 				 */
   1130   8741     Milan 				VN_HOLD(vrootp);
   1131   8741     Milan 				if (vrootp != rootdir)
   1132   8741     Milan 					VN_HOLD(vrootp);
   1133  10808     Milan 				if (lookuppnvp(&pn, &rpn, flags, NULL,
   1134   8741     Milan 				    &cmpvp, vrootp, vrootp, cr) == 0) {
   1135   8741     Milan 
   1136   8741     Milan 					if (VN_CMP(vp, cmpvp)) {
   1137   8741     Milan 						VN_RELE(cmpvp);
   1138   8741     Milan 
   1139   8741     Milan 						complen = strlen(rpn.pn_path);
   1140   8741     Milan 						bufloc -= complen;
   1141   8741     Milan 						if (bufloc < buf) {
   1142   8741     Milan 							err = ERANGE;
   1143   8741     Milan 							goto out;
   1144   8741     Milan 						}
   1145   8741     Milan 						bcopy(rpn.pn_path, bufloc,
   1146   8741     Milan 						    complen);
   1147   8741     Milan 						break;
   1148   8741     Milan 					} else {
   1149   8741     Milan 						VN_RELE(cmpvp);
   1150   8741     Milan 					}
   1151   8741     Milan 				}
   1152   8741     Milan 			} else {
   1153   8741     Milan 				mutex_exit(&vp->v_lock);
   1154   8741     Milan 			}
   1155   8741     Milan 		} else {
   1156   8741     Milan 			mutex_exit(&vp->v_lock);
   1157      0    stevel 		}
   1158      0    stevel 
   1159      0    stevel 		/*
   1160      0    stevel 		 * Shortcuts failed, search for this vnode in its parent.  If
   1161      0    stevel 		 * this is a mountpoint, then get the vnode underneath.
   1162      0    stevel 		 */
   1163      0    stevel 		if (vp->v_flag & VROOT)
   1164      0    stevel 			vp = vn_under(vp);
   1165   5331       amw 		if ((err = VOP_LOOKUP(vp, "..", &pvp, &emptypn, 0, vrootp, cr,
   1166   5331       amw 		    NULL, NULL, NULL)) != 0)
   1167      0    stevel 			goto out;
   1168      0    stevel 
   1169      0    stevel 		/*
   1170      0    stevel 		 * With extended attributes, it's possible for a directory to
   1171      0    stevel 		 * have a parent that is a regular file.  Check for that here.
   1172      0    stevel 		 */
   1173      0    stevel 		if (pvp->v_type != VDIR) {
   1174      0    stevel 			err = ENOTDIR;
   1175      0    stevel 			goto out;
   1176      0    stevel 		}
   1177      0    stevel 
   1178      0    stevel 		/*
   1179      0    stevel 		 * If this is true, something strange has happened.  This is
   1180      0    stevel 		 * only true if we are the root of a filesystem, which should
   1181      0    stevel 		 * have been caught by the check above.
   1182      0    stevel 		 */
   1183      0    stevel 		if (VN_CMP(pvp, vp)) {
   1184      0    stevel 			err = ENOENT;
   1185  10808     Milan 			goto out;
   1186  10808     Milan 		}
   1187  10808     Milan 
   1188  10808     Milan 		/*
   1189  10808     Milan 		 * Check if we have read and search privilege so, that
   1190  10808     Milan 		 * we can lookup the path in the directory
   1191  10808     Milan 		 */
   1192  10808     Milan 		vprivs = (flags & LOOKUP_CHECKREAD) ? VREAD | VEXEC : VEXEC;
   1193  10808     Milan 		if ((err = VOP_ACCESS(pvp, vprivs, 0, cr, NULL)) != 0) {
   1194      0    stevel 			goto out;
   1195      0    stevel 		}
   1196      0    stevel 
   1197      0    stevel 		/*
   1198   8741     Milan 		 * Try to obtain the path component from dnlc cache
   1199   8741     Milan 		 * before searching through the directory.
   1200   8741     Milan 		 */
   1201   8741     Milan 		if ((cmpvp = dnlc_reverse_lookup(vp, dbuf, dlen)) != NULL) {
   1202   8741     Milan 			/*
   1203   8741     Milan 			 * If we got parent vnode as a result,
   1204   8741     Milan 			 * then the answered path is correct.
   1205   8741     Milan 			 */
   1206   8741     Milan 			if (VN_CMP(cmpvp, pvp)) {
   1207   8741     Milan 				VN_RELE(cmpvp);
   1208   8741     Milan 				complen = strlen(dbuf);
   1209   8741     Milan 				bufloc -= complen;
   1210   8741     Milan 				if (bufloc <= buf) {
   1211   8741     Milan 					err = ENAMETOOLONG;
   1212   8741     Milan 					goto out;
   1213   8741     Milan 				}
   1214   8741     Milan 				bcopy(dbuf, bufloc, complen);
   1215   8741     Milan 
   1216   8741     Milan 				/* Prepend a slash to the current path */
   1217   8741     Milan 				*--bufloc = '/';
   1218   8741     Milan 
   1219   8741     Milan 				/* And continue with the next component */
   1220   8741     Milan 				VN_RELE(vp);
   1221   8741     Milan 				vp = pvp;
   1222   8741     Milan 				pvp = NULL;
   1223   8741     Milan 				continue;
   1224   8741     Milan 			} else {
   1225   8741     Milan 				VN_RELE(cmpvp);
   1226   8741     Milan 			}
   1227   8741     Milan 		}
   1228   8741     Milan 
   1229   8741     Milan 		/*
   1230      0    stevel 		 * Search the parent directory for the entry corresponding to
   1231      0    stevel 		 * this vnode.
   1232      0    stevel 		 */
   1233      0    stevel 		if ((err = dirfindvp(vrootp, pvp, vp, cr, dbuf, dlen, &dp))
   1234      0    stevel 		    != 0)
   1235      0    stevel 			goto out;
   1236      0    stevel 		complen = strlen(dp->d_name);
   1237      0    stevel 		bufloc -= complen;
   1238      0    stevel 		if (bufloc <= buf) {
   1239      0    stevel 			err = ENAMETOOLONG;
   1240      0    stevel 			goto out;
   1241      0    stevel 		}
   1242      0    stevel 		bcopy(dp->d_name, bufloc, complen);
   1243      0    stevel 
   1244      0    stevel 		/* Prepend a slash to the current path.  */
   1245      0    stevel 		*--bufloc = '/';
   1246      0    stevel 
   1247      0    stevel 		/* And continue with the next component */
   1248      0    stevel 		VN_RELE(vp);
   1249      0    stevel 		vp = pvp;
   1250      0    stevel 		pvp = NULL;
   1251      0    stevel 	}
   1252      0    stevel 
   1253      0    stevel 	/*
   1254      0    stevel 	 * Place the path at the beginning of the buffer.
   1255      0    stevel 	 */
   1256      0    stevel 	if (bufloc != buf)
   1257      0    stevel 		ovbcopy(bufloc, buf, buflen - (bufloc - buf));
   1258      0    stevel 
   1259   8741     Milan 	/*
   1260   8741     Milan 	 * We got here because of invalid v_path in startvp.
   1261   8741     Milan 	 * Now, we have all info to fix it.
   1262   8741     Milan 	 * Path must not include leading slash to let vn_renamepath
   1263   8741     Milan 	 * pre-attach chroot'd root directory path. Also, trailing '\0'
   1264   8741     Milan 	 * is not counted to length.
   1265   8741     Milan 	 */
   1266   8741     Milan 	vn_renamepath(vrootp, startvp, &buf[1], buflen - (bufloc - buf) - 2);
   1267   8741     Milan 
   1268      0    stevel out:
   1269      0    stevel 	/*
   1270      0    stevel 	 * If the error was ESTALE and the current directory to look in
   1271      0    stevel 	 * was the root for this lookup, the root for a mounted file
   1272      0    stevel 	 * system, or the starting directory for lookups, then
   1273      0    stevel 	 * return ENOENT instead of ESTALE.  In this case, no recovery
   1274      0    stevel 	 * is possible by the higher level.  If ESTALE was returned for
   1275      0    stevel 	 * some intermediate directory along the path, then recovery
   1276      0    stevel 	 * is potentially possible and retrying from the higher level
   1277      0    stevel 	 * will either correct the situation by purging stale cache
   1278      0    stevel 	 * entries or eventually get back to the point where no recovery
   1279      0    stevel 	 * is possible.
   1280      0    stevel 	 */
   1281      0    stevel 	if (err == ESTALE &&
   1282      0    stevel 	    (VN_CMP(vp, vrootp) || (vp->v_flag & VROOT) || vp == startvp))
   1283      0    stevel 		err = ENOENT;
   1284      0    stevel 
   1285      0    stevel 	kmem_free(dbuf, dlen);
   1286      0    stevel 	VN_RELE(vp);
   1287      0    stevel 	if (pvp)
   1288      0    stevel 		VN_RELE(pvp);
   1289      0    stevel 	pn_free(&pn);
   1290      0    stevel 	pn_free(&rpn);
   1291      0    stevel 
   1292      0    stevel 	return (err);
   1293      0    stevel }
   1294      0    stevel 
   1295      0    stevel /*
   1296   8741     Milan  * The additional flag, LOOKUP_CHECKREAD, is used to enforce artificial
   1297      0    stevel  * constraints in order to be standards compliant.  For example, if we have
   1298      0    stevel  * the cached path of '/foo/bar', and '/foo' has permissions 100 (execute
   1299      0    stevel  * only), then we can legitimately look up the path to the current working
   1300      0    stevel  * directory without needing read permission.  Existing standards tests,
   1301      0    stevel  * however, assume that we are determining the path by repeatedly looking up
   1302      0    stevel  * "..".  We need to keep this behavior in order to maintain backwards
   1303      0    stevel  * compatibility.
   1304      0    stevel  */
   1305      0    stevel static int
   1306      0    stevel vnodetopath_common(vnode_t *vrootp, vnode_t *vp, char *buf, size_t buflen,
   1307      0    stevel     cred_t *cr, int flags)
   1308      0    stevel {
   1309      0    stevel 	pathname_t pn, rpn;
   1310      0    stevel 	int ret, len;
   1311      0    stevel 	vnode_t *compvp, *pvp, *realvp;
   1312      0    stevel 	proc_t *p = curproc;
   1313      0    stevel 	char path[MAXNAMELEN];
   1314      0    stevel 	int doclose = 0;
   1315      0    stevel 
   1316      0    stevel 	/*
   1317      0    stevel 	 * If vrootp is NULL, get the root for curproc.  Callers with any other
   1318      0    stevel 	 * requirements should pass in a different vrootp.
   1319      0    stevel 	 */
   1320      0    stevel 	if (vrootp == NULL) {
   1321      0    stevel 		mutex_enter(&p->p_lock);
   1322      0    stevel 		if ((vrootp = PTOU(p)->u_rdir) == NULL)
   1323      0    stevel 			vrootp = rootdir;
   1324      0    stevel 		VN_HOLD(vrootp);
   1325      0    stevel 		mutex_exit(&p->p_lock);
   1326      0    stevel 	} else {
   1327      0    stevel 		VN_HOLD(vrootp);
   1328      0    stevel 	}
   1329      0    stevel 
   1330      0    stevel 	/*
   1331      0    stevel 	 * This is to get around an annoying artifact of the /proc filesystem,
   1332      0    stevel 	 * which is the behavior of {cwd/root}.  Trying to resolve this path
   1333      0    stevel 	 * will result in /proc/pid/cwd instead of whatever the real working
   1334      0    stevel 	 * directory is.  We can't rely on VOP_REALVP(), since that will break
   1335      0    stevel 	 * lofs.  The only difference between procfs and lofs is that opening
   1336      0    stevel 	 * the file will return the underling vnode in the case of procfs.
   1337      0    stevel 	 */
   1338   5331       amw 	if (vp->v_type == VDIR && VOP_REALVP(vp, &realvp, NULL) == 0 &&
   1339      0    stevel 	    realvp != vp) {
   1340      0    stevel 		VN_HOLD(vp);
   1341   5331       amw 		if (VOP_OPEN(&vp, FREAD, cr, NULL) == 0)
   1342      0    stevel 			doclose = 1;
   1343      0    stevel 		else
   1344      0    stevel 			VN_RELE(vp);
   1345      0    stevel 	}
   1346      0    stevel 
   1347      0    stevel 	pn_alloc(&pn);
   1348      0    stevel 
   1349      0    stevel 	/*
   1350      0    stevel 	 * Check to see if we have a cached path in the vnode.
   1351      0    stevel 	 */
   1352      0    stevel 	mutex_enter(&vp->v_lock);
   1353    254  eschrock 	if (vp->v_path != NULL) {
   1354    254  eschrock 		(void) pn_set(&pn, vp->v_path);
   1355      0    stevel 		mutex_exit(&vp->v_lock);
   1356      0    stevel 
   1357      0    stevel 		pn_alloc(&rpn);
   1358      0    stevel 
   1359      0    stevel 		/* We should only cache absolute paths */
   1360      0    stevel 		ASSERT(pn.pn_buf[0] == '/');
   1361      0    stevel 
   1362      0    stevel 		/*
   1363      0    stevel 		 * If we are in a zone or a chroot environment, then we have to
   1364      0    stevel 		 * take additional steps, since the path to the root might not
   1365      0    stevel 		 * be readable with the current credentials, even though the
   1366      0    stevel 		 * process can legitmately access the file.  In this case, we
   1367      0    stevel 		 * do the following:
   1368      0    stevel 		 *
   1369      0    stevel 		 * lookuppnvp() with all privileges to get the resolved path.
   1370      0    stevel 		 * call localpath() to get the local portion of the path, and
   1371      0    stevel 		 * continue as normal.
   1372      0    stevel 		 *
   1373      0    stevel 		 * If the the conversion to a local path fails, then we continue
   1374      0    stevel 		 * as normal.  This is a heuristic to make process object file
   1375      0    stevel 		 * paths available from within a zone.  Because lofs doesn't
   1376      0    stevel 		 * support page operations, the vnode stored in the seg_t is
   1377      0    stevel 		 * actually the underlying real vnode, not the lofs node itself.
   1378      0    stevel 		 * Most of the time, the lofs path is the same as the underlying
   1379      0    stevel 		 * vnode (for example, /usr/lib/libc.so.1).
   1380      0    stevel 		 */
   1381      0    stevel 		if (vrootp != rootdir) {
   1382      0    stevel 			char *local = NULL;
   1383      0    stevel 			VN_HOLD(rootdir);
   1384      0    stevel 			if (lookuppnvp(&pn, &rpn, FOLLOW,
   1385      0    stevel 			    NULL, &compvp, rootdir, rootdir, kcred) == 0) {
   1386      0    stevel 				local = localpath(rpn.pn_path, vrootp,
   1387      0    stevel 				    kcred);
   1388      0    stevel 				VN_RELE(compvp);
   1389      0    stevel 			}
   1390      0    stevel 
   1391      0    stevel 			/*
   1392   8741     Milan 			 * The original pn was changed through lookuppnvp().
   1393   8741     Milan 			 * Set it to local for next validation attempt.
   1394      0    stevel 			 */
   1395      0    stevel 			if (local) {
   1396      0    stevel 				(void) pn_set(&pn, local);
   1397      0    stevel 			} else {
   1398   8741     Milan 				goto notcached;
   1399      0    stevel 			}
   1400      0    stevel 		}
   1401      0    stevel 
   1402      0    stevel 		/*
   1403      0    stevel 		 * We should have a local path at this point, so start the
   1404      0    stevel 		 * search from the root of the current process.
   1405      0    stevel 		 */
   1406      0    stevel 		VN_HOLD(vrootp);
   1407      0    stevel 		if (vrootp != rootdir)
   1408      0    stevel 			VN_HOLD(vrootp);
   1409      0    stevel 		ret = lookuppnvp(&pn, &rpn, FOLLOW | flags, NULL,
   1410      0    stevel 		    &compvp, vrootp, vrootp, cr);
   1411      0    stevel 		if (ret == 0) {
   1412      0    stevel 			/*
   1413      0    stevel 			 * Check to see if the returned vnode is the same as
   1414      0    stevel 			 * the one we expect.  If not, give up.
   1415      0    stevel 			 */
   1416      0    stevel 			if (!vn_compare(vp, compvp) &&
   1417      0    stevel 			    !vnode_match(vp, compvp, cr)) {
   1418      0    stevel 				VN_RELE(compvp);
   1419      0    stevel 				goto notcached;
   1420      0    stevel 			}
   1421      0    stevel 
   1422      0    stevel 			VN_RELE(compvp);
   1423      0    stevel 
   1424      0    stevel 			/*
   1425      0    stevel 			 * Return the result.
   1426      0    stevel 			 */
   1427      0    stevel 			if (buflen <= rpn.pn_pathlen)
   1428      0    stevel 				goto notcached;
   1429      0    stevel 
   1430      0    stevel 			bcopy(rpn.pn_path, buf, rpn.pn_pathlen + 1);
   1431      0    stevel 			pn_free(&pn);
   1432      0    stevel 			pn_free(&rpn);
   1433      0    stevel 			VN_RELE(vrootp);
   1434      0    stevel 			if (doclose) {
   1435   5331       amw 				(void) VOP_CLOSE(vp, FREAD, 1, 0, cr, NULL);
   1436      0    stevel 				VN_RELE(vp);
   1437      0    stevel 			}
   1438      0    stevel 			return (0);
   1439      0    stevel 		}
   1440      0    stevel 
   1441      0    stevel notcached:
   1442      0    stevel 		pn_free(&rpn);
   1443      0    stevel 	} else {
   1444      0    stevel 		mutex_exit(&vp->v_lock);
   1445      0    stevel 	}
   1446      0    stevel 
   1447      0    stevel 	pn_free(&pn);
   1448      0    stevel 
   1449      0    stevel 	if (vp->v_type != VDIR) {
   1450      0    stevel 		/*
   1451      0    stevel 		 * If we don't have a directory, try to find it in the dnlc via
   1452      0    stevel 		 * reverse lookup.  Once this is found, we can use the regular
   1453      0    stevel 		 * directory search to find the full path.
   1454      0    stevel 		 */
   1455      0    stevel 		if ((pvp = dnlc_reverse_lookup(vp, path, MAXNAMELEN)) != NULL) {
   1456  10808     Milan 			/*
   1457  10808     Milan 			 * Check if we have read privilege so, that
   1458  10808     Milan 			 * we can lookup the path in the directory
   1459  10808     Milan 			 */
   1460  10808     Milan 			ret = 0;
   1461  10808     Milan 			if ((flags & LOOKUP_CHECKREAD)) {
   1462  10808     Milan 				ret = VOP_ACCESS(pvp, VREAD, 0, cr, NULL);
   1463  10808     Milan 			}
   1464  10808     Milan 			if (ret == 0) {
   1465  10808     Milan 				ret = dirtopath(vrootp, pvp, buf, buflen,
   1466  10808     Milan 				    flags, cr);
   1467  10808     Milan 			}
   1468      0    stevel 			if (ret == 0) {
   1469      0    stevel 				len = strlen(buf);
   1470      0    stevel 				if (len + strlen(path) + 1 >= buflen) {
   1471      0    stevel 					ret = ENAMETOOLONG;
   1472      0    stevel 				} else {
   1473      0    stevel 					if (buf[len - 1] != '/')
   1474      0    stevel 						buf[len++] = '/';
   1475      0    stevel 					bcopy(path, buf + len,
   1476      0    stevel 					    strlen(path) + 1);
   1477      0    stevel 				}
   1478      0    stevel 			}
   1479      0    stevel 
   1480      0    stevel 			VN_RELE(pvp);
   1481      0    stevel 		} else
   1482      0    stevel 			ret = ENOENT;
   1483      0    stevel 	} else
   1484  10808     Milan 		ret = dirtopath(vrootp, vp, buf, buflen, flags, cr);
   1485      0    stevel 
   1486      0    stevel 	VN_RELE(vrootp);
   1487      0    stevel 	if (doclose) {
   1488   5331       amw 		(void) VOP_CLOSE(vp, FREAD, 1, 0, cr, NULL);
   1489      0    stevel 		VN_RELE(vp);
   1490      0    stevel 	}
   1491      0    stevel 
   1492      0    stevel 	return (ret);
   1493      0    stevel }
   1494      0    stevel 
   1495      0    stevel int
   1496      0    stevel vnodetopath(vnode_t *vrootp, vnode_t *vp, char *buf, size_t buflen, cred_t *cr)
   1497      0    stevel {
   1498      0    stevel 	return (vnodetopath_common(vrootp, vp, buf, buflen, cr, 0));
   1499      0    stevel }
   1500      0    stevel 
   1501      0    stevel int
   1502      0    stevel dogetcwd(char *buf, size_t buflen)
   1503      0    stevel {
   1504      0    stevel 	int ret;
   1505      0    stevel 	vnode_t *vp;
   1506      0    stevel 	vnode_t *compvp;
   1507      0    stevel 	refstr_t *cwd, *oldcwd;
   1508      0    stevel 	const char *value;
   1509      0    stevel 	pathname_t rpnp, pnp;
   1510      0    stevel 	proc_t *p = curproc;
   1511      0    stevel 
   1512      0    stevel 	/*
   1513      0    stevel 	 * Check to see if there is a cached version of the cwd.  If so, lookup
   1514      0    stevel 	 * the cached value and make sure it is the same vnode.
   1515      0    stevel 	 */
   1516      0    stevel 	mutex_enter(&p->p_lock);
   1517      0    stevel 	if ((cwd = PTOU(p)->u_cwd) != NULL)
   1518      0    stevel 		refstr_hold(cwd);
   1519      0    stevel 	vp = PTOU(p)->u_cdir;
   1520      0    stevel 	VN_HOLD(vp);
   1521      0    stevel 	mutex_exit(&p->p_lock);
   1522      0    stevel 
   1523      0    stevel 	/*
   1524      0    stevel 	 * Make sure we have permission to access the current directory.
   1525      0    stevel 	 */
   1526   5331       amw 	if ((ret = VOP_ACCESS(vp, VEXEC, 0, CRED(), NULL)) != 0) {
   1527      0    stevel 		if (cwd != NULL)
   1528      0    stevel 			refstr_rele(cwd);
   1529      0    stevel 		VN_RELE(vp);
   1530      0    stevel 		return (ret);
   1531      0    stevel 	}
   1532      0    stevel 
   1533      0    stevel 	if (cwd) {
   1534      0    stevel 		value = refstr_value(cwd);
   1535      0    stevel 		if ((ret = pn_get((char *)value, UIO_SYSSPACE, &pnp)) != 0) {
   1536      0    stevel 			refstr_rele(cwd);
   1537      0    stevel 			VN_RELE(vp);
   1538      0    stevel 			return (ret);
   1539      0    stevel 		}
   1540      0    stevel 
   1541      0    stevel 		pn_alloc(&rpnp);
   1542      0    stevel 
   1543      0    stevel 		if (lookuppn(&pnp, &rpnp, NO_FOLLOW, NULL, &compvp) == 0) {
   1544      0    stevel 
   1545      0    stevel 			if (VN_CMP(vp, compvp) &&
   1546      0    stevel 			    strcmp(value, rpnp.pn_path) == 0) {
   1547      0    stevel 				VN_RELE(compvp);
   1548      0    stevel 				VN_RELE(vp);
   1549      0    stevel 				pn_free(&pnp);
   1550      0    stevel 				pn_free(&rpnp);
   1551      0    stevel 				if (strlen(value) + 1 > buflen) {
   1552      0    stevel 					refstr_rele(cwd);
   1553      0    stevel 					return (ENAMETOOLONG);
   1554      0    stevel 				}
   1555      0    stevel 				bcopy(value, buf, strlen(value) + 1);
   1556      0    stevel 				refstr_rele(cwd);
   1557      0    stevel 				return (0);
   1558      0    stevel 			}
   1559      0    stevel 
   1560      0    stevel 			VN_RELE(compvp);
   1561      0    stevel 		}
   1562      0    stevel 
   1563      0    stevel 		pn_free(&rpnp);
   1564      0    stevel 		pn_free(&pnp);
   1565      0    stevel 
   1566      0    stevel 		refstr_rele(cwd);
   1567      0    stevel 	}
   1568      0    stevel 
   1569      0    stevel 	ret = vnodetopath_common(NULL, vp, buf, buflen, CRED(),
   1570      0    stevel 	    LOOKUP_CHECKREAD);
   1571      0    stevel 
   1572      0    stevel 	VN_RELE(vp);
   1573      0    stevel 
   1574      0    stevel 	/*
   1575      0    stevel 	 * Store the new cwd and replace the existing cached copy.
   1576      0    stevel 	 */
   1577      0    stevel 	if (ret == 0)
   1578      0    stevel 		cwd = refstr_alloc(buf);
   1579      0    stevel 	else
   1580      0    stevel 		cwd = NULL;
   1581      0    stevel 
   1582      0    stevel 	mutex_enter(&p->p_lock);
   1583      0    stevel 	oldcwd = PTOU(p)->u_cwd;
   1584      0    stevel 	PTOU(p)->u_cwd = cwd;
   1585      0    stevel 	mutex_exit(&p->p_lock);
   1586      0    stevel 
   1587      0    stevel 	if (oldcwd)
   1588      0    stevel 		refstr_rele(oldcwd);
   1589      0    stevel 
   1590      0    stevel 	return (ret);
   1591      0    stevel }
   1592