xref: /titanic_50/usr/src/uts/common/fs/lookup.c (revision bdb005b59455f11dc7f68cad9b1ec5b07de11e5d)
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 (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
23  */
24 
25 /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
26 /*	  All Rights Reserved  	*/
27 
28 /*
29  * University Copyright- Copyright (c) 1982, 1986, 1988
30  * The Regents of the University of California
31  * All Rights Reserved
32  *
33  * University Acknowledgment- Portions of this document are derived from
34  * software developed by the University of California, Berkeley, and its
35  * contributors.
36  */
37 
38 #include <sys/types.h>
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/cpuvar.h>
42 #include <sys/errno.h>
43 #include <sys/cred.h>
44 #include <sys/user.h>
45 #include <sys/uio.h>
46 #include <sys/vfs.h>
47 #include <sys/vnode.h>
48 #include <sys/pathname.h>
49 #include <sys/proc.h>
50 #include <sys/vtrace.h>
51 #include <sys/sysmacros.h>
52 #include <sys/debug.h>
53 #include <sys/dirent.h>
54 #include <c2/audit.h>
55 #include <sys/zone.h>
56 #include <sys/dnlc.h>
57 #include <sys/fs/snode.h>
58 
59 /* Controls whether paths are stored with vnodes. */
60 int vfs_vnode_path = 1;
61 
62 int
63 lookupname(
64 	char *fnamep,
65 	enum uio_seg seg,
66 	enum symfollow followlink,
67 	vnode_t **dirvpp,
68 	vnode_t **compvpp)
69 {
70 	return (lookupnameatcred(fnamep, seg, followlink, dirvpp, compvpp, NULL,
71 	    CRED()));
72 }
73 
74 /*
75  * Lookup the user file name,
76  * Handle allocation and freeing of pathname buffer, return error.
77  */
78 int
79 lookupnameatcred(
80 	char *fnamep,			/* user pathname */
81 	enum uio_seg seg,		/* addr space that name is in */
82 	enum symfollow followlink,	/* follow sym links */
83 	vnode_t **dirvpp,		/* ret for ptr to parent dir vnode */
84 	vnode_t **compvpp,		/* ret for ptr to component vnode */
85 	vnode_t *startvp,		/* start path search from vp */
86 	cred_t *cr)			/* credential */
87 {
88 	char namebuf[TYPICALMAXPATHLEN];
89 	struct pathname lookpn;
90 	int error;
91 
92 	error = pn_get_buf(fnamep, seg, &lookpn, namebuf, sizeof (namebuf));
93 	if (error == 0) {
94 		if (AU_AUDITING())
95 			audit_lookupname();
96 		error = lookuppnatcred(&lookpn, NULL, followlink,
97 		    dirvpp, compvpp, startvp, cr);
98 	}
99 	if (error == ENAMETOOLONG) {
100 		/*
101 		 * This thread used a pathname > TYPICALMAXPATHLEN bytes long.
102 		 */
103 		if (error = pn_get(fnamep, seg, &lookpn))
104 			return (error);
105 		error = lookuppnatcred(&lookpn, NULL, followlink,
106 		    dirvpp, compvpp, startvp, cr);
107 		pn_free(&lookpn);
108 	}
109 
110 	return (error);
111 }
112 
113 int
114 lookupnameat(char *fnamep, enum uio_seg seg, enum symfollow followlink,
115     vnode_t **dirvpp, vnode_t **compvpp, vnode_t *startvp)
116 {
117 	return (lookupnameatcred(fnamep, seg, followlink, dirvpp, compvpp,
118 	    startvp, CRED()));
119 }
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 (lookuppnatcred(pnp, rpnp, followlink, dirvpp, compvpp, NULL,
130 	    CRED()));
131 }
132 
133 /*
134  * Lookup the user file name from a given vp, using a specific credential.
135  */
136 int
137 lookuppnatcred(
138 	struct pathname *pnp,		/* pathname to lookup */
139 	struct pathname *rpnp,		/* if non-NULL, return resolved path */
140 	enum symfollow followlink,	/* (don't) follow sym links */
141 	vnode_t **dirvpp,		/* ptr for parent vnode */
142 	vnode_t **compvpp,		/* ptr for entry vnode */
143 	vnode_t *startvp,		/* start search from this vp */
144 	cred_t *cr)			/* user credential */
145 {
146 	vnode_t *vp;	/* current directory vp */
147 	vnode_t *rootvp;
148 	proc_t *p = curproc;
149 
150 	if (pnp->pn_pathlen == 0)
151 		return (ENOENT);
152 
153 	mutex_enter(&p->p_lock);	/* for u_rdir and u_cdir */
154 	if ((rootvp = PTOU(p)->u_rdir) == NULL)
155 		rootvp = rootdir;
156 	else if (rootvp != rootdir)	/* no need to VN_HOLD rootdir */
157 		VN_HOLD(rootvp);
158 
159 	if (pnp->pn_path[0] == '/') {
160 		vp = rootvp;
161 	} else {
162 		vp = (startvp == NULL) ? PTOU(p)->u_cdir : startvp;
163 	}
164 	VN_HOLD(vp);
165 	mutex_exit(&p->p_lock);
166 
167 	/*
168 	 * Skip over leading slashes
169 	 */
170 	if (pnp->pn_path[0] == '/') {
171 		do {
172 			pnp->pn_path++;
173 			pnp->pn_pathlen--;
174 		} while (pnp->pn_path[0] == '/');
175 	}
176 
177 	return (lookuppnvp(pnp, rpnp, followlink, dirvpp,
178 	    compvpp, rootvp, vp, cr));
179 }
180 
181 int
182 lookuppnat(struct pathname *pnp, struct pathname *rpnp,
183     enum symfollow followlink, vnode_t **dirvpp, vnode_t **compvpp,
184     vnode_t *startvp)
185 {
186 	return (lookuppnatcred(pnp, rpnp, followlink, dirvpp, compvpp, startvp,
187 	    CRED()));
188 }
189 
190 /* Private flag to do our getcwd() dirty work */
191 #define	LOOKUP_CHECKREAD	0x10
192 #define	LOOKUP_MASK		(~LOOKUP_CHECKREAD)
193 
194 /*
195  * Starting at current directory, translate pathname pnp to end.
196  * Leave pathname of final component in pnp, return the vnode
197  * for the final component in *compvpp, and return the vnode
198  * for the parent of the final component in dirvpp.
199  *
200  * This is the central routine in pathname translation and handles
201  * multiple components in pathnames, separating them at /'s.  It also
202  * implements mounted file systems and processes symbolic links.
203  *
204  * vp is the vnode where the directory search should start.
205  *
206  * Reference counts: vp must be held prior to calling this function.  rootvp
207  * should only be held if rootvp != rootdir.
208  */
209 int
210 lookuppnvp(
211 	struct pathname *pnp,		/* pathname to lookup */
212 	struct pathname *rpnp,		/* if non-NULL, return resolved path */
213 	int flags,			/* follow symlinks */
214 	vnode_t **dirvpp,		/* ptr for parent vnode */
215 	vnode_t **compvpp,		/* ptr for entry vnode */
216 	vnode_t *rootvp,		/* rootvp */
217 	vnode_t *vp,			/* directory to start search at */
218 	cred_t *cr)			/* user's credential */
219 {
220 	vnode_t *cvp;	/* current component vp */
221 	vnode_t *tvp;	/* addressable temp ptr */
222 	char component[MAXNAMELEN];	/* buffer for component (incl null) */
223 	int error;
224 	int nlink;
225 	int lookup_flags;
226 	struct pathname presrvd; /* case preserved name */
227 	struct pathname *pp = NULL;
228 	vnode_t *startvp;
229 	vnode_t *zonevp = curproc->p_zone->zone_rootvp;		/* zone root */
230 	int must_be_directory = 0;
231 	boolean_t retry_with_kcred;
232 	uint32_t auditing = AU_AUDITING();
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 (auditing)
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 (auditing)
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 (auditing) {	/* 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 (auditing) {
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 (auditing)
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 (auditing)
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 (auditing)
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 (auditing) {
594 				if (error = audit_pathcomp(pnp, vp, cr))
595 					goto bad;
596 			}
597 			*dirvpp = vp;
598 		} else
599 			VN_RELE(vp);
600 		if (auditing)
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 (auditing) {
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 (auditing)	/* 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  */
1012 static int
1013 dirtopath(vnode_t *vrootp, vnode_t *vp, char *buf, size_t buflen, int flags,
1014     cred_t *cr)
1015 {
1016 	pathname_t pn, rpn, emptypn;
1017 	vnode_t *cmpvp, *pvp = NULL;
1018 	vnode_t *startvp = vp;
1019 	int err = 0, vprivs;
1020 	size_t complen;
1021 	char *dbuf;
1022 	dirent64_t *dp;
1023 	char		*bufloc;
1024 	size_t		dlen = DIRENT64_RECLEN(MAXPATHLEN);
1025 	refstr_t	*mntpt;
1026 
1027 	/* Operation only allowed on directories */
1028 	ASSERT(vp->v_type == VDIR);
1029 
1030 	/* We must have at least enough space for "/" */
1031 	if (buflen < 2)
1032 		return (ENAMETOOLONG);
1033 
1034 	/* Start at end of string with terminating null */
1035 	bufloc = &buf[buflen - 1];
1036 	*bufloc = '\0';
1037 
1038 	pn_alloc(&pn);
1039 	pn_alloc(&rpn);
1040 	dbuf = kmem_alloc(dlen, KM_SLEEP);
1041 	bzero(&emptypn, sizeof (emptypn));
1042 
1043 	/*
1044 	 * Begin with an additional reference on vp.  This will be decremented
1045 	 * during the loop.
1046 	 */
1047 	VN_HOLD(vp);
1048 
1049 	for (;;) {
1050 		/*
1051 		 * Return if we've reached the root.  If the buffer is empty,
1052 		 * return '/'.  We explicitly don't use vn_compare(), since it
1053 		 * compares the real vnodes.  A lofs mount of '/' would produce
1054 		 * incorrect results otherwise.
1055 		 */
1056 		if (VN_CMP(vrootp, vp)) {
1057 			if (*bufloc == '\0')
1058 				*--bufloc = '/';
1059 			break;
1060 		}
1061 
1062 		/*
1063 		 * If we've reached the VFS root, something has gone wrong.  We
1064 		 * should have reached the root in the above check.  The only
1065 		 * explantation is that 'vp' is not contained withing the given
1066 		 * root, in which case we return EPERM.
1067 		 */
1068 		if (VN_CMP(rootdir, vp)) {
1069 			err = EPERM;
1070 			goto out;
1071 		}
1072 
1073 		/*
1074 		 * Shortcut: see if this vnode is a mountpoint.  If so,
1075 		 * grab the path information from the vfs_t.
1076 		 */
1077 		if (vp->v_flag & VROOT) {
1078 
1079 			mntpt = vfs_getmntpoint(vp->v_vfsp);
1080 			if ((err = pn_set(&pn, (char *)refstr_value(mntpt)))
1081 			    == 0) {
1082 				refstr_rele(mntpt);
1083 				rpn.pn_path = rpn.pn_buf;
1084 
1085 				/*
1086 				 * Ensure the mountpoint still exists.
1087 				 */
1088 				VN_HOLD(vrootp);
1089 				if (vrootp != rootdir)
1090 					VN_HOLD(vrootp);
1091 				if (lookuppnvp(&pn, &rpn, flags, NULL,
1092 				    &cmpvp, vrootp, vrootp, cr) == 0) {
1093 
1094 					if (VN_CMP(vp, cmpvp)) {
1095 						VN_RELE(cmpvp);
1096 
1097 						complen = strlen(rpn.pn_path);
1098 						bufloc -= complen;
1099 						if (bufloc < buf) {
1100 							err = ERANGE;
1101 							goto out;
1102 						}
1103 						bcopy(rpn.pn_path, bufloc,
1104 						    complen);
1105 						break;
1106 					} else {
1107 						VN_RELE(cmpvp);
1108 					}
1109 				}
1110 			} else {
1111 				refstr_rele(mntpt);
1112 			}
1113 		}
1114 
1115 		/*
1116 		 * Shortcut: see if this vnode has correct v_path. If so,
1117 		 * we have the work done.
1118 		 */
1119 		mutex_enter(&vp->v_lock);
1120 		if (vp->v_path != NULL) {
1121 
1122 			if ((err = pn_set(&pn, vp->v_path)) == 0) {
1123 				mutex_exit(&vp->v_lock);
1124 				rpn.pn_path = rpn.pn_buf;
1125 
1126 				/*
1127 				 * Ensure the v_path pointing to correct vnode
1128 				 */
1129 				VN_HOLD(vrootp);
1130 				if (vrootp != rootdir)
1131 					VN_HOLD(vrootp);
1132 				if (lookuppnvp(&pn, &rpn, flags, NULL,
1133 				    &cmpvp, vrootp, vrootp, cr) == 0) {
1134 
1135 					if (VN_CMP(vp, cmpvp)) {
1136 						VN_RELE(cmpvp);
1137 
1138 						complen = strlen(rpn.pn_path);
1139 						bufloc -= complen;
1140 						if (bufloc < buf) {
1141 							err = ERANGE;
1142 							goto out;
1143 						}
1144 						bcopy(rpn.pn_path, bufloc,
1145 						    complen);
1146 						break;
1147 					} else {
1148 						VN_RELE(cmpvp);
1149 					}
1150 				}
1151 			} else {
1152 				mutex_exit(&vp->v_lock);
1153 			}
1154 		} else {
1155 			mutex_exit(&vp->v_lock);
1156 		}
1157 
1158 		/*
1159 		 * Shortcuts failed, search for this vnode in its parent.  If
1160 		 * this is a mountpoint, then get the vnode underneath.
1161 		 */
1162 		if (vp->v_flag & VROOT)
1163 			vp = vn_under(vp);
1164 		if ((err = VOP_LOOKUP(vp, "..", &pvp, &emptypn, 0, vrootp, cr,
1165 		    NULL, NULL, NULL)) != 0)
1166 			goto out;
1167 
1168 		/*
1169 		 * With extended attributes, it's possible for a directory to
1170 		 * have a parent that is a regular file.  Check for that here.
1171 		 */
1172 		if (pvp->v_type != VDIR) {
1173 			err = ENOTDIR;
1174 			goto out;
1175 		}
1176 
1177 		/*
1178 		 * If this is true, something strange has happened.  This is
1179 		 * only true if we are the root of a filesystem, which should
1180 		 * have been caught by the check above.
1181 		 */
1182 		if (VN_CMP(pvp, vp)) {
1183 			err = ENOENT;
1184 			goto out;
1185 		}
1186 
1187 		/*
1188 		 * Check if we have read and search privilege so, that
1189 		 * we can lookup the path in the directory
1190 		 */
1191 		vprivs = (flags & LOOKUP_CHECKREAD) ? VREAD | VEXEC : VEXEC;
1192 		if ((err = VOP_ACCESS(pvp, vprivs, 0, cr, NULL)) != 0) {
1193 			goto out;
1194 		}
1195 
1196 		/*
1197 		 * Try to obtain the path component from dnlc cache
1198 		 * before searching through the directory.
1199 		 */
1200 		if ((cmpvp = dnlc_reverse_lookup(vp, dbuf, dlen)) != NULL) {
1201 			/*
1202 			 * If we got parent vnode as a result,
1203 			 * then the answered path is correct.
1204 			 */
1205 			if (VN_CMP(cmpvp, pvp)) {
1206 				VN_RELE(cmpvp);
1207 				complen = strlen(dbuf);
1208 				bufloc -= complen;
1209 				if (bufloc <= buf) {
1210 					err = ENAMETOOLONG;
1211 					goto out;
1212 				}
1213 				bcopy(dbuf, bufloc, complen);
1214 
1215 				/* Prepend a slash to the current path */
1216 				*--bufloc = '/';
1217 
1218 				/* And continue with the next component */
1219 				VN_RELE(vp);
1220 				vp = pvp;
1221 				pvp = NULL;
1222 				continue;
1223 			} else {
1224 				VN_RELE(cmpvp);
1225 			}
1226 		}
1227 
1228 		/*
1229 		 * Search the parent directory for the entry corresponding to
1230 		 * this vnode.
1231 		 */
1232 		if ((err = dirfindvp(vrootp, pvp, vp, cr, dbuf, dlen, &dp))
1233 		    != 0)
1234 			goto out;
1235 		complen = strlen(dp->d_name);
1236 		bufloc -= complen;
1237 		if (bufloc <= buf) {
1238 			err = ENAMETOOLONG;
1239 			goto out;
1240 		}
1241 		bcopy(dp->d_name, bufloc, complen);
1242 
1243 		/* Prepend a slash to the current path.  */
1244 		*--bufloc = '/';
1245 
1246 		/* And continue with the next component */
1247 		VN_RELE(vp);
1248 		vp = pvp;
1249 		pvp = NULL;
1250 	}
1251 
1252 	/*
1253 	 * Place the path at the beginning of the buffer.
1254 	 */
1255 	if (bufloc != buf)
1256 		ovbcopy(bufloc, buf, buflen - (bufloc - buf));
1257 
1258 out:
1259 	/*
1260 	 * If the error was ESTALE and the current directory to look in
1261 	 * was the root for this lookup, the root for a mounted file
1262 	 * system, or the starting directory for lookups, then
1263 	 * return ENOENT instead of ESTALE.  In this case, no recovery
1264 	 * is possible by the higher level.  If ESTALE was returned for
1265 	 * some intermediate directory along the path, then recovery
1266 	 * is potentially possible and retrying from the higher level
1267 	 * will either correct the situation by purging stale cache
1268 	 * entries or eventually get back to the point where no recovery
1269 	 * is possible.
1270 	 */
1271 	if (err == ESTALE &&
1272 	    (VN_CMP(vp, vrootp) || (vp->v_flag & VROOT) || vp == startvp))
1273 		err = ENOENT;
1274 
1275 	kmem_free(dbuf, dlen);
1276 	VN_RELE(vp);
1277 	if (pvp)
1278 		VN_RELE(pvp);
1279 	pn_free(&pn);
1280 	pn_free(&rpn);
1281 
1282 	return (err);
1283 }
1284 
1285 /*
1286  * The additional flag, LOOKUP_CHECKREAD, is used to enforce artificial
1287  * constraints in order to be standards compliant.  For example, if we have
1288  * the cached path of '/foo/bar', and '/foo' has permissions 100 (execute
1289  * only), then we can legitimately look up the path to the current working
1290  * directory without needing read permission.  Existing standards tests,
1291  * however, assume that we are determining the path by repeatedly looking up
1292  * "..".  We need to keep this behavior in order to maintain backwards
1293  * compatibility.
1294  */
1295 static int
1296 vnodetopath_common(vnode_t *vrootp, vnode_t *vp, char *buf, size_t buflen,
1297     cred_t *cr, int flags)
1298 {
1299 	pathname_t pn, rpn;
1300 	int ret, len;
1301 	vnode_t *compvp, *pvp, *realvp;
1302 	proc_t *p = curproc;
1303 	char path[MAXNAMELEN];
1304 	int doclose = 0;
1305 
1306 	/*
1307 	 * If vrootp is NULL, get the root for curproc.  Callers with any other
1308 	 * requirements should pass in a different vrootp.
1309 	 */
1310 	if (vrootp == NULL) {
1311 		mutex_enter(&p->p_lock);
1312 		if ((vrootp = PTOU(p)->u_rdir) == NULL)
1313 			vrootp = rootdir;
1314 		VN_HOLD(vrootp);
1315 		mutex_exit(&p->p_lock);
1316 	} else {
1317 		VN_HOLD(vrootp);
1318 	}
1319 
1320 	/*
1321 	 * This is to get around an annoying artifact of the /proc filesystem,
1322 	 * which is the behavior of {cwd/root}.  Trying to resolve this path
1323 	 * will result in /proc/pid/cwd instead of whatever the real working
1324 	 * directory is.  We can't rely on VOP_REALVP(), since that will break
1325 	 * lofs.  The only difference between procfs and lofs is that opening
1326 	 * the file will return the underling vnode in the case of procfs.
1327 	 */
1328 	if (vp->v_type == VDIR && VOP_REALVP(vp, &realvp, NULL) == 0 &&
1329 	    realvp != vp) {
1330 		VN_HOLD(vp);
1331 		if (VOP_OPEN(&vp, FREAD, cr, NULL) == 0)
1332 			doclose = 1;
1333 		else
1334 			VN_RELE(vp);
1335 	}
1336 
1337 	pn_alloc(&pn);
1338 
1339 	/*
1340 	 * Check to see if we have a cached path in the vnode.
1341 	 */
1342 	mutex_enter(&vp->v_lock);
1343 	if (vp->v_path != NULL) {
1344 		(void) pn_set(&pn, vp->v_path);
1345 		mutex_exit(&vp->v_lock);
1346 
1347 		pn_alloc(&rpn);
1348 
1349 		/* We should only cache absolute paths */
1350 		ASSERT(pn.pn_buf[0] == '/');
1351 
1352 		/*
1353 		 * If we are in a zone or a chroot environment, then we have to
1354 		 * take additional steps, since the path to the root might not
1355 		 * be readable with the current credentials, even though the
1356 		 * process can legitmately access the file.  In this case, we
1357 		 * do the following:
1358 		 *
1359 		 * lookuppnvp() with all privileges to get the resolved path.
1360 		 * call localpath() to get the local portion of the path, and
1361 		 * continue as normal.
1362 		 *
1363 		 * If the the conversion to a local path fails, then we continue
1364 		 * as normal.  This is a heuristic to make process object file
1365 		 * paths available from within a zone.  Because lofs doesn't
1366 		 * support page operations, the vnode stored in the seg_t is
1367 		 * actually the underlying real vnode, not the lofs node itself.
1368 		 * Most of the time, the lofs path is the same as the underlying
1369 		 * vnode (for example, /usr/lib/libc.so.1).
1370 		 */
1371 		if (vrootp != rootdir) {
1372 			char *local = NULL;
1373 			VN_HOLD(rootdir);
1374 			if (lookuppnvp(&pn, &rpn, FOLLOW,
1375 			    NULL, &compvp, rootdir, rootdir, kcred) == 0) {
1376 				local = localpath(rpn.pn_path, vrootp,
1377 				    kcred);
1378 				VN_RELE(compvp);
1379 			}
1380 
1381 			/*
1382 			 * The original pn was changed through lookuppnvp().
1383 			 * Set it to local for next validation attempt.
1384 			 */
1385 			if (local) {
1386 				(void) pn_set(&pn, local);
1387 			} else {
1388 				goto notcached;
1389 			}
1390 		}
1391 
1392 		/*
1393 		 * We should have a local path at this point, so start the
1394 		 * search from the root of the current process.
1395 		 */
1396 		VN_HOLD(vrootp);
1397 		if (vrootp != rootdir)
1398 			VN_HOLD(vrootp);
1399 		ret = lookuppnvp(&pn, &rpn, FOLLOW | flags, NULL,
1400 		    &compvp, vrootp, vrootp, cr);
1401 		if (ret == 0) {
1402 			/*
1403 			 * Check to see if the returned vnode is the same as
1404 			 * the one we expect.  If not, give up.
1405 			 */
1406 			if (!vn_compare(vp, compvp) &&
1407 			    !vnode_match(vp, compvp, cr)) {
1408 				VN_RELE(compvp);
1409 				goto notcached;
1410 			}
1411 
1412 			VN_RELE(compvp);
1413 
1414 			/*
1415 			 * Return the result.
1416 			 */
1417 			if (buflen <= rpn.pn_pathlen)
1418 				goto notcached;
1419 
1420 			bcopy(rpn.pn_path, buf, rpn.pn_pathlen + 1);
1421 			pn_free(&pn);
1422 			pn_free(&rpn);
1423 			VN_RELE(vrootp);
1424 			if (doclose) {
1425 				(void) VOP_CLOSE(vp, FREAD, 1, 0, cr, NULL);
1426 				VN_RELE(vp);
1427 			}
1428 			return (0);
1429 		}
1430 
1431 notcached:
1432 		pn_free(&rpn);
1433 	} else {
1434 		mutex_exit(&vp->v_lock);
1435 	}
1436 
1437 	pn_free(&pn);
1438 
1439 	if (vp->v_type != VDIR) {
1440 		/*
1441 		 * If we don't have a directory, try to find it in the dnlc via
1442 		 * reverse lookup.  Once this is found, we can use the regular
1443 		 * directory search to find the full path.
1444 		 */
1445 		if ((pvp = dnlc_reverse_lookup(vp, path, MAXNAMELEN)) != NULL) {
1446 			/*
1447 			 * Check if we have read privilege so, that
1448 			 * we can lookup the path in the directory
1449 			 */
1450 			ret = 0;
1451 			if ((flags & LOOKUP_CHECKREAD)) {
1452 				ret = VOP_ACCESS(pvp, VREAD, 0, cr, NULL);
1453 			}
1454 			if (ret == 0) {
1455 				ret = dirtopath(vrootp, pvp, buf, buflen,
1456 				    flags, cr);
1457 			}
1458 			if (ret == 0) {
1459 				len = strlen(buf);
1460 				if (len + strlen(path) + 1 >= buflen) {
1461 					ret = ENAMETOOLONG;
1462 				} else {
1463 					if (buf[len - 1] != '/')
1464 						buf[len++] = '/';
1465 					bcopy(path, buf + len,
1466 					    strlen(path) + 1);
1467 				}
1468 			}
1469 
1470 			VN_RELE(pvp);
1471 		} else
1472 			ret = ENOENT;
1473 	} else
1474 		ret = dirtopath(vrootp, vp, buf, buflen, flags, cr);
1475 
1476 	VN_RELE(vrootp);
1477 	if (doclose) {
1478 		(void) VOP_CLOSE(vp, FREAD, 1, 0, cr, NULL);
1479 		VN_RELE(vp);
1480 	}
1481 
1482 	return (ret);
1483 }
1484 
1485 int
1486 vnodetopath(vnode_t *vrootp, vnode_t *vp, char *buf, size_t buflen, cred_t *cr)
1487 {
1488 	return (vnodetopath_common(vrootp, vp, buf, buflen, cr, 0));
1489 }
1490 
1491 int
1492 dogetcwd(char *buf, size_t buflen)
1493 {
1494 	int ret;
1495 	vnode_t *vp;
1496 	vnode_t *compvp;
1497 	refstr_t *cwd, *oldcwd;
1498 	const char *value;
1499 	pathname_t rpnp, pnp;
1500 	proc_t *p = curproc;
1501 
1502 	/*
1503 	 * Check to see if there is a cached version of the cwd.  If so, lookup
1504 	 * the cached value and make sure it is the same vnode.
1505 	 */
1506 	mutex_enter(&p->p_lock);
1507 	if ((cwd = PTOU(p)->u_cwd) != NULL)
1508 		refstr_hold(cwd);
1509 	vp = PTOU(p)->u_cdir;
1510 	VN_HOLD(vp);
1511 	mutex_exit(&p->p_lock);
1512 
1513 	/*
1514 	 * Make sure we have permission to access the current directory.
1515 	 */
1516 	if ((ret = VOP_ACCESS(vp, VEXEC, 0, CRED(), NULL)) != 0) {
1517 		if (cwd != NULL)
1518 			refstr_rele(cwd);
1519 		VN_RELE(vp);
1520 		return (ret);
1521 	}
1522 
1523 	if (cwd) {
1524 		value = refstr_value(cwd);
1525 		if ((ret = pn_get((char *)value, UIO_SYSSPACE, &pnp)) != 0) {
1526 			refstr_rele(cwd);
1527 			VN_RELE(vp);
1528 			return (ret);
1529 		}
1530 
1531 		pn_alloc(&rpnp);
1532 
1533 		if (lookuppn(&pnp, &rpnp, NO_FOLLOW, NULL, &compvp) == 0) {
1534 
1535 			if (VN_CMP(vp, compvp) &&
1536 			    strcmp(value, rpnp.pn_path) == 0) {
1537 				VN_RELE(compvp);
1538 				VN_RELE(vp);
1539 				pn_free(&pnp);
1540 				pn_free(&rpnp);
1541 				if (strlen(value) + 1 > buflen) {
1542 					refstr_rele(cwd);
1543 					return (ENAMETOOLONG);
1544 				}
1545 				bcopy(value, buf, strlen(value) + 1);
1546 				refstr_rele(cwd);
1547 				return (0);
1548 			}
1549 
1550 			VN_RELE(compvp);
1551 		}
1552 
1553 		pn_free(&rpnp);
1554 		pn_free(&pnp);
1555 
1556 		refstr_rele(cwd);
1557 	}
1558 
1559 	ret = vnodetopath_common(NULL, vp, buf, buflen, CRED(),
1560 	    LOOKUP_CHECKREAD);
1561 
1562 	VN_RELE(vp);
1563 
1564 	/*
1565 	 * Store the new cwd and replace the existing cached copy.
1566 	 */
1567 	if (ret == 0)
1568 		cwd = refstr_alloc(buf);
1569 	else
1570 		cwd = NULL;
1571 
1572 	mutex_enter(&p->p_lock);
1573 	oldcwd = PTOU(p)->u_cwd;
1574 	PTOU(p)->u_cwd = cwd;
1575 	mutex_exit(&p->p_lock);
1576 
1577 	if (oldcwd)
1578 		refstr_rele(oldcwd);
1579 
1580 	return (ret);
1581 }
1582