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, cred_t *cr) 1013 { 1014 pathname_t pn, rpn, emptypn; 1015 vnode_t *cmpvp, *pvp = NULL; 1016 vnode_t *startvp = vp; 1017 int err = 0; 1018 size_t complen; 1019 char *dbuf; 1020 dirent64_t *dp; 1021 char *bufloc; 1022 size_t dlen = DIRENT64_RECLEN(MAXPATHLEN); 1023 refstr_t *mntpt; 1024 1025 /* Operation only allowed on directories */ 1026 ASSERT(vp->v_type == VDIR); 1027 1028 /* We must have at least enough space for "/" */ 1029 if (buflen < 2) 1030 return (ENAMETOOLONG); 1031 1032 /* Start at end of string with terminating null */ 1033 bufloc = &buf[buflen - 1]; 1034 *bufloc = '\0'; 1035 1036 pn_alloc(&pn); 1037 pn_alloc(&rpn); 1038 dbuf = kmem_alloc(dlen, KM_SLEEP); 1039 bzero(&emptypn, sizeof (emptypn)); 1040 1041 /* 1042 * Begin with an additional reference on vp. This will be decremented 1043 * during the loop. 1044 */ 1045 VN_HOLD(vp); 1046 1047 for (;;) { 1048 /* 1049 * Return if we've reached the root. If the buffer is empty, 1050 * return '/'. We explicitly don't use vn_compare(), since it 1051 * compares the real vnodes. A lofs mount of '/' would produce 1052 * incorrect results otherwise. 1053 */ 1054 if (VN_CMP(vrootp, vp)) { 1055 if (*bufloc == '\0') 1056 *--bufloc = '/'; 1057 break; 1058 } 1059 1060 /* 1061 * If we've reached the VFS root, something has gone wrong. We 1062 * should have reached the root in the above check. The only 1063 * explantation is that 'vp' is not contained withing the given 1064 * root, in which case we return EPERM. 1065 */ 1066 if (VN_CMP(rootdir, vp)) { 1067 err = EPERM; 1068 goto out; 1069 } 1070 1071 /* 1072 * Shortcut: see if this vnode is a mountpoint. If so, 1073 * grab the path information from the vfs_t. 1074 */ 1075 if (vp->v_flag & VROOT) { 1076 1077 mntpt = vfs_getmntpoint(vp->v_vfsp); 1078 if ((err = pn_set(&pn, (char *)refstr_value(mntpt))) 1079 == 0) { 1080 refstr_rele(mntpt); 1081 rpn.pn_path = rpn.pn_buf; 1082 1083 /* 1084 * Ensure the mountpoint still exists. 1085 */ 1086 VN_HOLD(vrootp); 1087 if (vrootp != rootdir) 1088 VN_HOLD(vrootp); 1089 if (lookuppnvp(&pn, &rpn, 0, NULL, 1090 &cmpvp, vrootp, vrootp, cr) == 0) { 1091 1092 if (VN_CMP(vp, cmpvp)) { 1093 VN_RELE(cmpvp); 1094 1095 complen = strlen(rpn.pn_path); 1096 bufloc -= complen; 1097 if (bufloc < buf) { 1098 err = ERANGE; 1099 goto out; 1100 } 1101 bcopy(rpn.pn_path, bufloc, 1102 complen); 1103 break; 1104 } else { 1105 VN_RELE(cmpvp); 1106 } 1107 } 1108 } else { 1109 refstr_rele(mntpt); 1110 } 1111 } 1112 1113 /* 1114 * Shortcut: see if this vnode has correct v_path. If so, 1115 * we have the work done. 1116 */ 1117 mutex_enter(&vp->v_lock); 1118 if (vp->v_path != NULL) { 1119 1120 if ((err = pn_set(&pn, vp->v_path)) == 0) { 1121 mutex_exit(&vp->v_lock); 1122 rpn.pn_path = rpn.pn_buf; 1123 1124 /* 1125 * Ensure the v_path pointing to correct vnode 1126 */ 1127 VN_HOLD(vrootp); 1128 if (vrootp != rootdir) 1129 VN_HOLD(vrootp); 1130 if (lookuppnvp(&pn, &rpn, 0, NULL, 1131 &cmpvp, vrootp, vrootp, cr) == 0) { 1132 1133 if (VN_CMP(vp, cmpvp)) { 1134 VN_RELE(cmpvp); 1135 1136 complen = strlen(rpn.pn_path); 1137 bufloc -= complen; 1138 if (bufloc < buf) { 1139 err = ERANGE; 1140 goto out; 1141 } 1142 bcopy(rpn.pn_path, bufloc, 1143 complen); 1144 break; 1145 } else { 1146 VN_RELE(cmpvp); 1147 } 1148 } 1149 } else { 1150 mutex_exit(&vp->v_lock); 1151 } 1152 } else { 1153 mutex_exit(&vp->v_lock); 1154 } 1155 1156 /* 1157 * Shortcuts failed, search for this vnode in its parent. If 1158 * this is a mountpoint, then get the vnode underneath. 1159 */ 1160 if (vp->v_flag & VROOT) 1161 vp = vn_under(vp); 1162 if ((err = VOP_LOOKUP(vp, "..", &pvp, &emptypn, 0, vrootp, cr, 1163 NULL, NULL, NULL)) != 0) 1164 goto out; 1165 1166 /* 1167 * With extended attributes, it's possible for a directory to 1168 * have a parent that is a regular file. Check for that here. 1169 */ 1170 if (pvp->v_type != VDIR) { 1171 err = ENOTDIR; 1172 goto out; 1173 } 1174 1175 /* 1176 * If this is true, something strange has happened. This is 1177 * only true if we are the root of a filesystem, which should 1178 * have been caught by the check above. 1179 */ 1180 if (VN_CMP(pvp, vp)) { 1181 err = ENOENT; 1182 goto out; 1183 } 1184 1185 /* 1186 * Try to obtain the path component from dnlc cache 1187 * before searching through the directory. 1188 */ 1189 if ((cmpvp = dnlc_reverse_lookup(vp, dbuf, dlen)) != NULL) { 1190 /* 1191 * If we got parent vnode as a result, 1192 * then the answered path is correct. 1193 */ 1194 if (VN_CMP(cmpvp, pvp)) { 1195 VN_RELE(cmpvp); 1196 complen = strlen(dbuf); 1197 bufloc -= complen; 1198 if (bufloc <= buf) { 1199 err = ENAMETOOLONG; 1200 goto out; 1201 } 1202 bcopy(dbuf, bufloc, complen); 1203 1204 /* Prepend a slash to the current path */ 1205 *--bufloc = '/'; 1206 1207 /* And continue with the next component */ 1208 VN_RELE(vp); 1209 vp = pvp; 1210 pvp = NULL; 1211 continue; 1212 } else { 1213 VN_RELE(cmpvp); 1214 } 1215 } 1216 1217 /* 1218 * Search the parent directory for the entry corresponding to 1219 * this vnode. 1220 */ 1221 if ((err = dirfindvp(vrootp, pvp, vp, cr, dbuf, dlen, &dp)) 1222 != 0) 1223 goto out; 1224 complen = strlen(dp->d_name); 1225 bufloc -= complen; 1226 if (bufloc <= buf) { 1227 err = ENAMETOOLONG; 1228 goto out; 1229 } 1230 bcopy(dp->d_name, bufloc, complen); 1231 1232 /* Prepend a slash to the current path. */ 1233 *--bufloc = '/'; 1234 1235 /* And continue with the next component */ 1236 VN_RELE(vp); 1237 vp = pvp; 1238 pvp = NULL; 1239 } 1240 1241 /* 1242 * Place the path at the beginning of the buffer. 1243 */ 1244 if (bufloc != buf) 1245 ovbcopy(bufloc, buf, buflen - (bufloc - buf)); 1246 1247 /* 1248 * We got here because of invalid v_path in startvp. 1249 * Now, we have all info to fix it. 1250 * Path must not include leading slash to let vn_renamepath 1251 * pre-attach chroot'd root directory path. Also, trailing '\0' 1252 * is not counted to length. 1253 */ 1254 vn_renamepath(vrootp, startvp, &buf[1], buflen - (bufloc - buf) - 2); 1255 1256 out: 1257 /* 1258 * If the error was ESTALE and the current directory to look in 1259 * was the root for this lookup, the root for a mounted file 1260 * system, or the starting directory for lookups, then 1261 * return ENOENT instead of ESTALE. In this case, no recovery 1262 * is possible by the higher level. If ESTALE was returned for 1263 * some intermediate directory along the path, then recovery 1264 * is potentially possible and retrying from the higher level 1265 * will either correct the situation by purging stale cache 1266 * entries or eventually get back to the point where no recovery 1267 * is possible. 1268 */ 1269 if (err == ESTALE && 1270 (VN_CMP(vp, vrootp) || (vp->v_flag & VROOT) || vp == startvp)) 1271 err = ENOENT; 1272 1273 kmem_free(dbuf, dlen); 1274 VN_RELE(vp); 1275 if (pvp) 1276 VN_RELE(pvp); 1277 pn_free(&pn); 1278 pn_free(&rpn); 1279 1280 return (err); 1281 } 1282 1283 /* 1284 * The additional flag, LOOKUP_CHECKREAD, is used to enforce artificial 1285 * constraints in order to be standards compliant. For example, if we have 1286 * the cached path of '/foo/bar', and '/foo' has permissions 100 (execute 1287 * only), then we can legitimately look up the path to the current working 1288 * directory without needing read permission. Existing standards tests, 1289 * however, assume that we are determining the path by repeatedly looking up 1290 * "..". We need to keep this behavior in order to maintain backwards 1291 * compatibility. 1292 */ 1293 static int 1294 vnodetopath_common(vnode_t *vrootp, vnode_t *vp, char *buf, size_t buflen, 1295 cred_t *cr, int flags) 1296 { 1297 pathname_t pn, rpn; 1298 int ret, len; 1299 vnode_t *compvp, *pvp, *realvp; 1300 proc_t *p = curproc; 1301 char path[MAXNAMELEN]; 1302 int doclose = 0; 1303 1304 /* 1305 * If vrootp is NULL, get the root for curproc. Callers with any other 1306 * requirements should pass in a different vrootp. 1307 */ 1308 if (vrootp == NULL) { 1309 mutex_enter(&p->p_lock); 1310 if ((vrootp = PTOU(p)->u_rdir) == NULL) 1311 vrootp = rootdir; 1312 VN_HOLD(vrootp); 1313 mutex_exit(&p->p_lock); 1314 } else { 1315 VN_HOLD(vrootp); 1316 } 1317 1318 /* 1319 * This is to get around an annoying artifact of the /proc filesystem, 1320 * which is the behavior of {cwd/root}. Trying to resolve this path 1321 * will result in /proc/pid/cwd instead of whatever the real working 1322 * directory is. We can't rely on VOP_REALVP(), since that will break 1323 * lofs. The only difference between procfs and lofs is that opening 1324 * the file will return the underling vnode in the case of procfs. 1325 */ 1326 if (vp->v_type == VDIR && VOP_REALVP(vp, &realvp, NULL) == 0 && 1327 realvp != vp) { 1328 VN_HOLD(vp); 1329 if (VOP_OPEN(&vp, FREAD, cr, NULL) == 0) 1330 doclose = 1; 1331 else 1332 VN_RELE(vp); 1333 } 1334 1335 pn_alloc(&pn); 1336 1337 /* 1338 * Check to see if we have a cached path in the vnode. 1339 */ 1340 mutex_enter(&vp->v_lock); 1341 if (vp->v_path != NULL) { 1342 (void) pn_set(&pn, vp->v_path); 1343 mutex_exit(&vp->v_lock); 1344 1345 pn_alloc(&rpn); 1346 1347 /* We should only cache absolute paths */ 1348 ASSERT(pn.pn_buf[0] == '/'); 1349 1350 /* 1351 * If we are in a zone or a chroot environment, then we have to 1352 * take additional steps, since the path to the root might not 1353 * be readable with the current credentials, even though the 1354 * process can legitmately access the file. In this case, we 1355 * do the following: 1356 * 1357 * lookuppnvp() with all privileges to get the resolved path. 1358 * call localpath() to get the local portion of the path, and 1359 * continue as normal. 1360 * 1361 * If the the conversion to a local path fails, then we continue 1362 * as normal. This is a heuristic to make process object file 1363 * paths available from within a zone. Because lofs doesn't 1364 * support page operations, the vnode stored in the seg_t is 1365 * actually the underlying real vnode, not the lofs node itself. 1366 * Most of the time, the lofs path is the same as the underlying 1367 * vnode (for example, /usr/lib/libc.so.1). 1368 */ 1369 if (vrootp != rootdir) { 1370 char *local = NULL; 1371 VN_HOLD(rootdir); 1372 if (lookuppnvp(&pn, &rpn, FOLLOW, 1373 NULL, &compvp, rootdir, rootdir, kcred) == 0) { 1374 local = localpath(rpn.pn_path, vrootp, 1375 kcred); 1376 VN_RELE(compvp); 1377 } 1378 1379 /* 1380 * The original pn was changed through lookuppnvp(). 1381 * Set it to local for next validation attempt. 1382 */ 1383 if (local) { 1384 (void) pn_set(&pn, local); 1385 } else { 1386 goto notcached; 1387 } 1388 } 1389 1390 /* 1391 * We should have a local path at this point, so start the 1392 * search from the root of the current process. 1393 */ 1394 VN_HOLD(vrootp); 1395 if (vrootp != rootdir) 1396 VN_HOLD(vrootp); 1397 ret = lookuppnvp(&pn, &rpn, FOLLOW | flags, NULL, 1398 &compvp, vrootp, vrootp, cr); 1399 if (ret == 0) { 1400 /* 1401 * Check to see if the returned vnode is the same as 1402 * the one we expect. If not, give up. 1403 */ 1404 if (!vn_compare(vp, compvp) && 1405 !vnode_match(vp, compvp, cr)) { 1406 VN_RELE(compvp); 1407 goto notcached; 1408 } 1409 1410 VN_RELE(compvp); 1411 1412 /* 1413 * Return the result. 1414 */ 1415 if (buflen <= rpn.pn_pathlen) 1416 goto notcached; 1417 1418 bcopy(rpn.pn_path, buf, rpn.pn_pathlen + 1); 1419 pn_free(&pn); 1420 pn_free(&rpn); 1421 VN_RELE(vrootp); 1422 if (doclose) { 1423 (void) VOP_CLOSE(vp, FREAD, 1, 0, cr, NULL); 1424 VN_RELE(vp); 1425 } 1426 return (0); 1427 } 1428 1429 notcached: 1430 pn_free(&rpn); 1431 } else { 1432 mutex_exit(&vp->v_lock); 1433 } 1434 1435 pn_free(&pn); 1436 1437 if (vp->v_type != VDIR) { 1438 /* 1439 * If we don't have a directory, try to find it in the dnlc via 1440 * reverse lookup. Once this is found, we can use the regular 1441 * directory search to find the full path. 1442 */ 1443 if ((pvp = dnlc_reverse_lookup(vp, path, MAXNAMELEN)) != NULL) { 1444 ret = dirtopath(vrootp, pvp, buf, buflen, cr); 1445 if (ret == 0) { 1446 len = strlen(buf); 1447 if (len + strlen(path) + 1 >= buflen) { 1448 ret = ENAMETOOLONG; 1449 } else { 1450 if (buf[len - 1] != '/') 1451 buf[len++] = '/'; 1452 bcopy(path, buf + len, 1453 strlen(path) + 1); 1454 } 1455 } 1456 1457 VN_RELE(pvp); 1458 } else 1459 ret = ENOENT; 1460 } else 1461 ret = dirtopath(vrootp, vp, buf, buflen, cr); 1462 1463 VN_RELE(vrootp); 1464 if (doclose) { 1465 (void) VOP_CLOSE(vp, FREAD, 1, 0, cr, NULL); 1466 VN_RELE(vp); 1467 } 1468 1469 return (ret); 1470 } 1471 1472 int 1473 vnodetopath(vnode_t *vrootp, vnode_t *vp, char *buf, size_t buflen, cred_t *cr) 1474 { 1475 return (vnodetopath_common(vrootp, vp, buf, buflen, cr, 0)); 1476 } 1477 1478 int 1479 dogetcwd(char *buf, size_t buflen) 1480 { 1481 int ret; 1482 vnode_t *vp; 1483 vnode_t *compvp; 1484 refstr_t *cwd, *oldcwd; 1485 const char *value; 1486 pathname_t rpnp, pnp; 1487 proc_t *p = curproc; 1488 1489 /* 1490 * Check to see if there is a cached version of the cwd. If so, lookup 1491 * the cached value and make sure it is the same vnode. 1492 */ 1493 mutex_enter(&p->p_lock); 1494 if ((cwd = PTOU(p)->u_cwd) != NULL) 1495 refstr_hold(cwd); 1496 vp = PTOU(p)->u_cdir; 1497 VN_HOLD(vp); 1498 mutex_exit(&p->p_lock); 1499 1500 /* 1501 * Make sure we have permission to access the current directory. 1502 */ 1503 if ((ret = VOP_ACCESS(vp, VEXEC, 0, CRED(), NULL)) != 0) { 1504 if (cwd != NULL) 1505 refstr_rele(cwd); 1506 VN_RELE(vp); 1507 return (ret); 1508 } 1509 1510 if (cwd) { 1511 value = refstr_value(cwd); 1512 if ((ret = pn_get((char *)value, UIO_SYSSPACE, &pnp)) != 0) { 1513 refstr_rele(cwd); 1514 VN_RELE(vp); 1515 return (ret); 1516 } 1517 1518 pn_alloc(&rpnp); 1519 1520 if (lookuppn(&pnp, &rpnp, NO_FOLLOW, NULL, &compvp) == 0) { 1521 1522 if (VN_CMP(vp, compvp) && 1523 strcmp(value, rpnp.pn_path) == 0) { 1524 VN_RELE(compvp); 1525 VN_RELE(vp); 1526 pn_free(&pnp); 1527 pn_free(&rpnp); 1528 if (strlen(value) + 1 > buflen) { 1529 refstr_rele(cwd); 1530 return (ENAMETOOLONG); 1531 } 1532 bcopy(value, buf, strlen(value) + 1); 1533 refstr_rele(cwd); 1534 return (0); 1535 } 1536 1537 VN_RELE(compvp); 1538 } 1539 1540 pn_free(&rpnp); 1541 pn_free(&pnp); 1542 1543 refstr_rele(cwd); 1544 } 1545 1546 ret = vnodetopath_common(NULL, vp, buf, buflen, CRED(), 1547 LOOKUP_CHECKREAD); 1548 1549 VN_RELE(vp); 1550 1551 /* 1552 * Store the new cwd and replace the existing cached copy. 1553 */ 1554 if (ret == 0) 1555 cwd = refstr_alloc(buf); 1556 else 1557 cwd = NULL; 1558 1559 mutex_enter(&p->p_lock); 1560 oldcwd = PTOU(p)->u_cwd; 1561 PTOU(p)->u_cwd = cwd; 1562 mutex_exit(&p->p_lock); 1563 1564 if (oldcwd) 1565 refstr_rele(oldcwd); 1566 1567 return (ret); 1568 } 1569