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