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