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