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