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