1 /* 2 * Copyright (c) 1982, 1986, 1989, 1993 3 * The Regents of the University of California. All rights reserved. 4 * (c) UNIX System Laboratories, Inc. 5 * All or some portions of this file are derived from material licensed 6 * to the University of California by American Telephone and Telegraph 7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8 * the permission of UNIX System Laboratories, Inc. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 * 4. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 * 38 * @(#)vfs_lookup.c 8.4 (Berkeley) 2/16/94 39 * $Id: vfs_lookup.c,v 1.17 1997/04/04 17:46:13 dfr Exp $ 40 */ 41 42 #include "opt_ktrace.h" 43 44 #include <sys/param.h> 45 #include <sys/systm.h> 46 #include <sys/syslimits.h> 47 #include <sys/time.h> 48 #include <sys/namei.h> 49 #include <sys/vnode.h> 50 #include <sys/mount.h> 51 #include <sys/errno.h> 52 #include <sys/malloc.h> 53 #include <sys/filedesc.h> 54 #include <sys/proc.h> 55 56 #ifdef KTRACE 57 #include <sys/ktrace.h> 58 #endif 59 60 /* 61 * Convert a pathname into a pointer to a locked inode. 62 * 63 * The FOLLOW flag is set when symbolic links are to be followed 64 * when they occur at the end of the name translation process. 65 * Symbolic links are always followed for all other pathname 66 * components other than the last. 67 * 68 * The segflg defines whether the name is to be copied from user 69 * space or kernel space. 70 * 71 * Overall outline of namei: 72 * 73 * copy in name 74 * get starting directory 75 * while (!done && !error) { 76 * call lookup to search path. 77 * if symbolic link, massage name in buffer and continue 78 * } 79 */ 80 int 81 namei(ndp) 82 register struct nameidata *ndp; 83 { 84 register struct filedesc *fdp; /* pointer to file descriptor state */ 85 register char *cp; /* pointer into pathname argument */ 86 register struct vnode *dp; /* the directory we are searching */ 87 struct iovec aiov; /* uio for reading symbolic links */ 88 struct uio auio; 89 int error, linklen; 90 struct componentname *cnp = &ndp->ni_cnd; 91 struct proc *p = cnp->cn_proc; 92 93 ndp->ni_cnd.cn_cred = ndp->ni_cnd.cn_proc->p_ucred; 94 #ifdef DIAGNOSTIC 95 if (!cnp->cn_cred || !cnp->cn_proc) 96 panic ("namei: bad cred/proc"); 97 if (cnp->cn_nameiop & (~OPMASK)) 98 panic ("namei: nameiop contaminated with flags"); 99 if (cnp->cn_flags & OPMASK) 100 panic ("namei: flags contaminated with nameiops"); 101 #endif 102 fdp = cnp->cn_proc->p_fd; 103 104 /* 105 * Get a buffer for the name to be translated, and copy the 106 * name into the buffer. 107 */ 108 if ((cnp->cn_flags & HASBUF) == 0) 109 MALLOC(cnp->cn_pnbuf, caddr_t, MAXPATHLEN, M_NAMEI, M_WAITOK); 110 if (ndp->ni_segflg == UIO_SYSSPACE) 111 error = copystr(ndp->ni_dirp, cnp->cn_pnbuf, 112 MAXPATHLEN, (u_int *)&ndp->ni_pathlen); 113 else 114 error = copyinstr(ndp->ni_dirp, cnp->cn_pnbuf, 115 MAXPATHLEN, (u_int *)&ndp->ni_pathlen); 116 117 /* 118 * Don't allow empty pathnames. 119 */ 120 if (!error && *cnp->cn_pnbuf == '\0') 121 error = ENOENT; 122 123 if (error) { 124 free(cnp->cn_pnbuf, M_NAMEI); 125 ndp->ni_vp = NULL; 126 return (error); 127 } 128 ndp->ni_loopcnt = 0; 129 #ifdef KTRACE 130 if (KTRPOINT(cnp->cn_proc, KTR_NAMEI)) 131 ktrnamei(cnp->cn_proc->p_tracep, cnp->cn_pnbuf); 132 #endif 133 134 /* 135 * Get starting point for the translation. 136 */ 137 if ((ndp->ni_rootdir = fdp->fd_rdir) == NULL) 138 ndp->ni_rootdir = rootvnode; 139 dp = fdp->fd_cdir; 140 VREF(dp); 141 for (;;) { 142 /* 143 * Check if root directory should replace current directory. 144 * Done at start of translation and after symbolic link. 145 */ 146 cnp->cn_nameptr = cnp->cn_pnbuf; 147 if (*(cnp->cn_nameptr) == '/') { 148 vrele(dp); 149 while (*(cnp->cn_nameptr) == '/') { 150 cnp->cn_nameptr++; 151 ndp->ni_pathlen--; 152 } 153 dp = ndp->ni_rootdir; 154 VREF(dp); 155 } 156 ndp->ni_startdir = dp; 157 error = lookup(ndp); 158 if (error) { 159 FREE(cnp->cn_pnbuf, M_NAMEI); 160 return (error); 161 } 162 /* 163 * Check for symbolic link 164 */ 165 if ((cnp->cn_flags & ISSYMLINK) == 0) { 166 if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0) 167 FREE(cnp->cn_pnbuf, M_NAMEI); 168 else 169 cnp->cn_flags |= HASBUF; 170 return (0); 171 } 172 if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1) 173 VOP_UNLOCK(ndp->ni_dvp, 0, p); 174 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) { 175 error = ELOOP; 176 break; 177 } 178 if (ndp->ni_pathlen > 1) 179 MALLOC(cp, char *, MAXPATHLEN, M_NAMEI, M_WAITOK); 180 else 181 cp = cnp->cn_pnbuf; 182 aiov.iov_base = cp; 183 aiov.iov_len = MAXPATHLEN; 184 auio.uio_iov = &aiov; 185 auio.uio_iovcnt = 1; 186 auio.uio_offset = 0; 187 auio.uio_rw = UIO_READ; 188 auio.uio_segflg = UIO_SYSSPACE; 189 auio.uio_procp = (struct proc *)0; 190 auio.uio_resid = MAXPATHLEN; 191 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred); 192 if (error) { 193 if (ndp->ni_pathlen > 1) 194 free(cp, M_NAMEI); 195 break; 196 } 197 linklen = MAXPATHLEN - auio.uio_resid; 198 if (linklen + ndp->ni_pathlen >= MAXPATHLEN) { 199 if (ndp->ni_pathlen > 1) 200 free(cp, M_NAMEI); 201 error = ENAMETOOLONG; 202 break; 203 } 204 if (ndp->ni_pathlen > 1) { 205 bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen); 206 FREE(cnp->cn_pnbuf, M_NAMEI); 207 cnp->cn_pnbuf = cp; 208 } else 209 cnp->cn_pnbuf[linklen] = '\0'; 210 ndp->ni_pathlen += linklen; 211 vput(ndp->ni_vp); 212 dp = ndp->ni_dvp; 213 } 214 FREE(cnp->cn_pnbuf, M_NAMEI); 215 vrele(ndp->ni_dvp); 216 vput(ndp->ni_vp); 217 ndp->ni_vp = NULL; 218 return (error); 219 } 220 221 /* 222 * Search a pathname. 223 * This is a very central and rather complicated routine. 224 * 225 * The pathname is pointed to by ni_ptr and is of length ni_pathlen. 226 * The starting directory is taken from ni_startdir. The pathname is 227 * descended until done, or a symbolic link is encountered. The variable 228 * ni_more is clear if the path is completed; it is set to one if a 229 * symbolic link needing interpretation is encountered. 230 * 231 * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on 232 * whether the name is to be looked up, created, renamed, or deleted. 233 * When CREATE, RENAME, or DELETE is specified, information usable in 234 * creating, renaming, or deleting a directory entry may be calculated. 235 * If flag has LOCKPARENT or'ed into it, the parent directory is returned 236 * locked. If flag has WANTPARENT or'ed into it, the parent directory is 237 * returned unlocked. Otherwise the parent directory is not returned. If 238 * the target of the pathname exists and LOCKLEAF is or'ed into the flag 239 * the target is returned locked, otherwise it is returned unlocked. 240 * When creating or renaming and LOCKPARENT is specified, the target may not 241 * be ".". When deleting and LOCKPARENT is specified, the target may be ".". 242 * 243 * Overall outline of lookup: 244 * 245 * dirloop: 246 * identify next component of name at ndp->ni_ptr 247 * handle degenerate case where name is null string 248 * if .. and crossing mount points and on mounted filesys, find parent 249 * call VOP_LOOKUP routine for next component name 250 * directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set 251 * component vnode returned in ni_vp (if it exists), locked. 252 * if result vnode is mounted on and crossing mount points, 253 * find mounted on vnode 254 * if more components of name, do next level at dirloop 255 * return the answer in ni_vp, locked if LOCKLEAF set 256 * if LOCKPARENT set, return locked parent in ni_dvp 257 * if WANTPARENT set, return unlocked parent in ni_dvp 258 */ 259 int 260 lookup(ndp) 261 register struct nameidata *ndp; 262 { 263 register char *cp; /* pointer into pathname argument */ 264 register struct vnode *dp = 0; /* the directory we are searching */ 265 struct vnode *tdp; /* saved dp */ 266 struct mount *mp; /* mount table entry */ 267 int docache; /* == 0 do not cache last component */ 268 int wantparent; /* 1 => wantparent or lockparent flag */ 269 int rdonly; /* lookup read-only flag bit */ 270 int trailing_slash; 271 int error = 0; 272 struct componentname *cnp = &ndp->ni_cnd; 273 struct proc *p = cnp->cn_proc; 274 275 /* 276 * Setup: break out flag bits into variables. 277 */ 278 wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT); 279 docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE; 280 if (cnp->cn_nameiop == DELETE || 281 (wantparent && cnp->cn_nameiop != CREATE && 282 cnp->cn_nameiop != LOOKUP)) 283 docache = 0; 284 rdonly = cnp->cn_flags & RDONLY; 285 ndp->ni_dvp = NULL; 286 cnp->cn_flags &= ~ISSYMLINK; 287 dp = ndp->ni_startdir; 288 ndp->ni_startdir = NULLVP; 289 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY, p); 290 291 dirloop: 292 /* 293 * Search a new directory. 294 * 295 * The cn_hash value is for use by vfs_cache. 296 * The last component of the filename is left accessible via 297 * cnp->cn_nameptr for callers that need the name. Callers needing 298 * the name set the SAVENAME flag. When done, they assume 299 * responsibility for freeing the pathname buffer. 300 */ 301 cnp->cn_consume = 0; 302 cnp->cn_hash = 0; 303 for (cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++) 304 cnp->cn_hash += (unsigned char)*cp; 305 cnp->cn_namelen = cp - cnp->cn_nameptr; 306 if (cnp->cn_namelen > NAME_MAX) { 307 error = ENAMETOOLONG; 308 goto bad; 309 } 310 #ifdef NAMEI_DIAGNOSTIC 311 { char c = *cp; 312 *cp = '\0'; 313 printf("{%s}: ", cnp->cn_nameptr); 314 *cp = c; } 315 #endif 316 ndp->ni_pathlen -= cnp->cn_namelen; 317 ndp->ni_next = cp; 318 319 /* 320 * Replace multiple slashes by a single slash and trailing slashes 321 * by a null. This must be done before VOP_LOOKUP() because some 322 * fs's don't know about trailing slashes. Remember if there were 323 * trailing slashes to handle symlinks, existing non-directories 324 * and non-existing files that won't be directories specially later. 325 */ 326 trailing_slash = 0; 327 while (*cp == '/' && (cp[1] == '/' || cp[1] == '\0')) { 328 cp++; 329 ndp->ni_pathlen--; 330 if (*cp == '\0') { 331 trailing_slash = 1; 332 *ndp->ni_next = '\0'; /* XXX for direnter() ... */ 333 } 334 } 335 ndp->ni_next = cp; 336 337 cnp->cn_flags |= MAKEENTRY; 338 if (*cp == '\0' && docache == 0) 339 cnp->cn_flags &= ~MAKEENTRY; 340 if (cnp->cn_namelen == 2 && 341 cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.') 342 cnp->cn_flags |= ISDOTDOT; 343 else 344 cnp->cn_flags &= ~ISDOTDOT; 345 if (*ndp->ni_next == 0) 346 cnp->cn_flags |= ISLASTCN; 347 else 348 cnp->cn_flags &= ~ISLASTCN; 349 350 351 /* 352 * Check for degenerate name (e.g. / or "") 353 * which is a way of talking about a directory, 354 * e.g. like "/." or ".". 355 */ 356 if (cnp->cn_nameptr[0] == '\0') { 357 if (dp->v_type != VDIR) { 358 error = ENOTDIR; 359 goto bad; 360 } 361 if (cnp->cn_nameiop != LOOKUP) { 362 error = EISDIR; 363 goto bad; 364 } 365 if (wantparent) { 366 ndp->ni_dvp = dp; 367 VREF(dp); 368 } 369 ndp->ni_vp = dp; 370 if (!(cnp->cn_flags & (LOCKPARENT | LOCKLEAF))) 371 VOP_UNLOCK(dp, 0, p); 372 if (cnp->cn_flags & SAVESTART) 373 panic("lookup: SAVESTART"); 374 return (0); 375 } 376 377 /* 378 * Handle "..": two special cases. 379 * 1. If at root directory (e.g. after chroot) 380 * or at absolute root directory 381 * then ignore it so can't get out. 382 * 2. If this vnode is the root of a mounted 383 * filesystem, then replace it with the 384 * vnode which was mounted on so we take the 385 * .. in the other file system. 386 */ 387 if (cnp->cn_flags & ISDOTDOT) { 388 for (;;) { 389 if (dp == ndp->ni_rootdir || dp == rootvnode) { 390 ndp->ni_dvp = dp; 391 ndp->ni_vp = dp; 392 VREF(dp); 393 goto nextname; 394 } 395 if ((dp->v_flag & VROOT) == 0 || 396 (cnp->cn_flags & NOCROSSMOUNT)) 397 break; 398 tdp = dp; 399 dp = dp->v_mount->mnt_vnodecovered; 400 vput(tdp); 401 VREF(dp); 402 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY, p); 403 } 404 } 405 406 /* 407 * We now have a segment name to search for, and a directory to search. 408 */ 409 unionlookup: 410 ndp->ni_dvp = dp; 411 ndp->ni_vp = NULL; 412 ASSERT_VOP_LOCKED(dp, "lookup"); 413 if (error = VOP_LOOKUP(dp, &ndp->ni_vp, cnp)) { 414 #ifdef DIAGNOSTIC 415 if (ndp->ni_vp != NULL) 416 panic("leaf should be empty"); 417 #endif 418 #ifdef NAMEI_DIAGNOSTIC 419 printf("not found\n"); 420 #endif 421 if ((error == ENOENT) && 422 (dp->v_flag & VROOT) && 423 (dp->v_mount->mnt_flag & MNT_UNION)) { 424 tdp = dp; 425 dp = dp->v_mount->mnt_vnodecovered; 426 vput(tdp); 427 VREF(dp); 428 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY, p); 429 goto unionlookup; 430 } 431 432 if (error != EJUSTRETURN) 433 goto bad; 434 /* 435 * If creating and at end of pathname, then can consider 436 * allowing file to be created. 437 */ 438 if (rdonly) { 439 error = EROFS; 440 goto bad; 441 } 442 if (*cp == '\0' && trailing_slash && 443 !(cnp->cn_flags & WILLBEDIR)) { 444 error = ENOENT; 445 goto bad; 446 } 447 /* 448 * We return with ni_vp NULL to indicate that the entry 449 * doesn't currently exist, leaving a pointer to the 450 * (possibly locked) directory inode in ndp->ni_dvp. 451 */ 452 if (cnp->cn_flags & SAVESTART) { 453 ndp->ni_startdir = ndp->ni_dvp; 454 VREF(ndp->ni_startdir); 455 } 456 return (0); 457 } 458 #ifdef NAMEI_DIAGNOSTIC 459 printf("found\n"); 460 #endif 461 462 ASSERT_VOP_LOCKED(ndp->ni_vp, "lookup"); 463 464 /* 465 * Take into account any additional components consumed by 466 * the underlying filesystem. 467 */ 468 if (cnp->cn_consume > 0) { 469 cnp->cn_nameptr += cnp->cn_consume; 470 ndp->ni_next += cnp->cn_consume; 471 ndp->ni_pathlen -= cnp->cn_consume; 472 cnp->cn_consume = 0; 473 } 474 475 dp = ndp->ni_vp; 476 477 /* 478 * Check to see if the vnode has been mounted on; 479 * if so find the root of the mounted file system. 480 */ 481 while (dp->v_type == VDIR && (mp = dp->v_mountedhere) && 482 (cnp->cn_flags & NOCROSSMOUNT) == 0) { 483 if (vfs_busy(mp, 0, 0, p)) 484 continue; 485 error = VFS_ROOT(mp, &tdp); 486 vfs_unbusy(mp, p); 487 if (error) 488 goto bad2; 489 vput(dp); 490 ndp->ni_vp = dp = tdp; 491 } 492 493 /* 494 * Check for symbolic link 495 */ 496 if ((dp->v_type == VLNK) && 497 ((cnp->cn_flags & FOLLOW) || trailing_slash || 498 *ndp->ni_next == '/')) { 499 cnp->cn_flags |= ISSYMLINK; 500 return (0); 501 } 502 503 /* 504 * Check for bogus trailing slashes. 505 */ 506 if (trailing_slash && dp->v_type != VDIR) { 507 error = ENOTDIR; 508 goto bad2; 509 } 510 511 nextname: 512 /* 513 * Not a symbolic link. If more pathname, 514 * continue at next component, else return. 515 */ 516 if (*ndp->ni_next == '/') { 517 cnp->cn_nameptr = ndp->ni_next; 518 while (*cnp->cn_nameptr == '/') { 519 cnp->cn_nameptr++; 520 ndp->ni_pathlen--; 521 } 522 if (ndp->ni_dvp != ndp->ni_vp) { 523 ASSERT_VOP_UNLOCKED(ndp->ni_dvp, "lookup"); 524 } 525 vrele(ndp->ni_dvp); 526 goto dirloop; 527 } 528 /* 529 * Disallow directory write attempts on read-only file systems. 530 */ 531 if (rdonly && 532 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) { 533 error = EROFS; 534 goto bad2; 535 } 536 if (cnp->cn_flags & SAVESTART) { 537 ndp->ni_startdir = ndp->ni_dvp; 538 VREF(ndp->ni_startdir); 539 } 540 if (!wantparent) 541 vrele(ndp->ni_dvp); 542 if ((cnp->cn_flags & LOCKLEAF) == 0) 543 VOP_UNLOCK(dp, 0, p); 544 return (0); 545 546 bad2: 547 if ((cnp->cn_flags & LOCKPARENT) && *ndp->ni_next == '\0') 548 VOP_UNLOCK(ndp->ni_dvp, 0, p); 549 vrele(ndp->ni_dvp); 550 bad: 551 vput(dp); 552 ndp->ni_vp = NULL; 553 return (error); 554 } 555 556 /* 557 * relookup - lookup a path name component 558 * Used by lookup to re-aquire things. 559 */ 560 int 561 relookup(dvp, vpp, cnp) 562 struct vnode *dvp, **vpp; 563 struct componentname *cnp; 564 { 565 struct proc *p = cnp->cn_proc; 566 struct vnode *dp = 0; /* the directory we are searching */ 567 int docache; /* == 0 do not cache last component */ 568 int wantparent; /* 1 => wantparent or lockparent flag */ 569 int rdonly; /* lookup read-only flag bit */ 570 int error = 0; 571 #ifdef NAMEI_DIAGNOSTIC 572 int newhash; /* DEBUG: check name hash */ 573 char *cp; /* DEBUG: check name ptr/len */ 574 #endif 575 576 /* 577 * Setup: break out flag bits into variables. 578 */ 579 wantparent = cnp->cn_flags & (LOCKPARENT|WANTPARENT); 580 docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE; 581 if (cnp->cn_nameiop == DELETE || 582 (wantparent && cnp->cn_nameiop != CREATE)) 583 docache = 0; 584 rdonly = cnp->cn_flags & RDONLY; 585 cnp->cn_flags &= ~ISSYMLINK; 586 dp = dvp; 587 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY, p); 588 589 /* dirloop: */ 590 /* 591 * Search a new directory. 592 * 593 * The cn_hash value is for use by vfs_cache. 594 * The last component of the filename is left accessible via 595 * cnp->cn_nameptr for callers that need the name. Callers needing 596 * the name set the SAVENAME flag. When done, they assume 597 * responsibility for freeing the pathname buffer. 598 */ 599 #ifdef NAMEI_DIAGNOSTIC 600 for (newhash = 0, cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++) 601 newhash += (unsigned char)*cp; 602 if (newhash != cnp->cn_hash) 603 panic("relookup: bad hash"); 604 if (cnp->cn_namelen != cp - cnp->cn_nameptr) 605 panic ("relookup: bad len"); 606 if (*cp != 0) 607 panic("relookup: not last component"); 608 printf("{%s}: ", cnp->cn_nameptr); 609 #endif 610 611 /* 612 * Check for degenerate name (e.g. / or "") 613 * which is a way of talking about a directory, 614 * e.g. like "/." or ".". 615 */ 616 if (cnp->cn_nameptr[0] == '\0') { 617 if (cnp->cn_nameiop != LOOKUP || wantparent) { 618 error = EISDIR; 619 goto bad; 620 } 621 if (dp->v_type != VDIR) { 622 error = ENOTDIR; 623 goto bad; 624 } 625 if (!(cnp->cn_flags & LOCKLEAF)) 626 VOP_UNLOCK(dp, 0, p); 627 *vpp = dp; 628 if (cnp->cn_flags & SAVESTART) 629 panic("lookup: SAVESTART"); 630 return (0); 631 } 632 633 if (cnp->cn_flags & ISDOTDOT) 634 panic ("relookup: lookup on dot-dot"); 635 636 /* 637 * We now have a segment name to search for, and a directory to search. 638 */ 639 if (error = VOP_LOOKUP(dp, vpp, cnp)) { 640 #ifdef DIAGNOSTIC 641 if (*vpp != NULL) 642 panic("leaf should be empty"); 643 #endif 644 if (error != EJUSTRETURN) 645 goto bad; 646 /* 647 * If creating and at end of pathname, then can consider 648 * allowing file to be created. 649 */ 650 if (rdonly) { 651 error = EROFS; 652 goto bad; 653 } 654 /* ASSERT(dvp == ndp->ni_startdir) */ 655 if (cnp->cn_flags & SAVESTART) 656 VREF(dvp); 657 /* 658 * We return with ni_vp NULL to indicate that the entry 659 * doesn't currently exist, leaving a pointer to the 660 * (possibly locked) directory inode in ndp->ni_dvp. 661 */ 662 return (0); 663 } 664 dp = *vpp; 665 666 #ifdef DIAGNOSTIC 667 /* 668 * Check for symbolic link 669 */ 670 if (dp->v_type == VLNK && (cnp->cn_flags & FOLLOW)) 671 panic ("relookup: symlink found.\n"); 672 #endif 673 674 /* 675 * Disallow directory write attempts on read-only file systems. 676 */ 677 if (rdonly && 678 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) { 679 error = EROFS; 680 goto bad2; 681 } 682 /* ASSERT(dvp == ndp->ni_startdir) */ 683 if (cnp->cn_flags & SAVESTART) 684 VREF(dvp); 685 686 if (!wantparent) 687 vrele(dvp); 688 if ((cnp->cn_flags & LOCKLEAF) == 0) 689 VOP_UNLOCK(dp, 0, p); 690 return (0); 691 692 bad2: 693 if ((cnp->cn_flags & LOCKPARENT) && (cnp->cn_flags & ISLASTCN)) 694 VOP_UNLOCK(dvp, 0, p); 695 vrele(dvp); 696 bad: 697 vput(dp); 698 *vpp = NULL; 699 return (error); 700 } 701