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