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 * $FreeBSD: src/sys/kern/vfs_lookup.c,v 1.38.2.3 2001/08/31 19:36:49 dillon Exp $ 40 * $DragonFly: src/sys/kern/vfs_lookup.c,v 1.7 2003/09/28 03:44:02 dillon Exp $ 41 */ 42 43 #include "opt_ktrace.h" 44 45 #include <sys/param.h> 46 #include <sys/systm.h> 47 #include <sys/vnode.h> 48 #include <sys/mount.h> 49 #include <sys/filedesc.h> 50 #include <sys/proc.h> 51 #include <sys/namei.h> 52 53 #ifdef KTRACE 54 #include <sys/ktrace.h> 55 #endif 56 57 #include <vm/vm_zone.h> 58 59 /* 60 * Convert a pathname into a pointer to a locked inode. 61 * 62 * The CNP_FOLLOW flag is set when symbolic links are to be followed 63 * when they occur at the end of the name translation process. 64 * Symbolic links are always followed for all other pathname 65 * components other than the last. 66 * 67 * The segflg defines whether the name is to be copied from user 68 * space or kernel space. 69 * 70 * Overall outline of namei: 71 * 72 * copy in name 73 * get starting directory 74 * while (!done && !error) { 75 * call lookup to search path. 76 * if symbolic link, massage name in buffer and continue 77 * } 78 */ 79 int 80 namei(struct nameidata *ndp) 81 { 82 struct filedesc *fdp; /* pointer to file descriptor state */ 83 char *cp; /* pointer into pathname argument */ 84 struct vnode *dp; /* the directory we are searching */ 85 struct iovec aiov; /* uio for reading symbolic links */ 86 struct uio auio; 87 int error, linklen; 88 struct componentname *cnp = &ndp->ni_cnd; 89 struct proc *p; 90 91 KKASSERT(ndp->ni_cnd.cn_td != NULL); 92 p = cnp->cn_td->td_proc; 93 KKASSERT(p != NULL); 94 KASSERT(cnp->cn_cred, ("namei: bad cred/proc")); 95 KKASSERT(cnp->cn_cred == p->p_ucred); /* YYY */ 96 KASSERT((cnp->cn_nameiop & (~NAMEI_OPMASK)) == 0, 97 ("namei: nameiop contaminated with flags")); 98 KASSERT((cnp->cn_flags & NAMEI_OPMASK) == 0, 99 ("namei: flags contaminated with nameiops")); 100 fdp = p->p_fd; 101 102 /* 103 * Get a buffer for the name to be translated, and copy the 104 * name into the buffer. 105 */ 106 if ((cnp->cn_flags & CNP_HASBUF) == 0) 107 cnp->cn_pnbuf = zalloc(namei_zone); 108 if (ndp->ni_segflg == UIO_SYSSPACE) 109 error = copystr(ndp->ni_dirp, cnp->cn_pnbuf, 110 MAXPATHLEN, (size_t *)&ndp->ni_pathlen); 111 else 112 error = copyinstr(ndp->ni_dirp, cnp->cn_pnbuf, 113 MAXPATHLEN, (size_t *)&ndp->ni_pathlen); 114 115 /* 116 * Don't allow empty pathnames. 117 */ 118 if (!error && *cnp->cn_pnbuf == '\0') 119 error = ENOENT; 120 121 if (error) { 122 zfree(namei_zone, cnp->cn_pnbuf); 123 ndp->ni_vp = NULL; 124 return (error); 125 } 126 ndp->ni_loopcnt = 0; 127 #ifdef KTRACE 128 if (KTRPOINT(cnp->cn_td, KTR_NAMEI)) 129 ktrnamei(cnp->cn_td->td_proc->p_tracep, cnp->cn_pnbuf); 130 #endif 131 132 /* 133 * Get starting point for the translation. 134 */ 135 ndp->ni_rootdir = fdp->fd_rdir; 136 ndp->ni_topdir = fdp->fd_jdir; 137 138 dp = fdp->fd_cdir; 139 VREF(dp); 140 for (;;) { 141 /* 142 * Check if root directory should replace current directory. 143 * Done at start of translation and after symbolic link. 144 */ 145 cnp->cn_nameptr = cnp->cn_pnbuf; 146 if (*(cnp->cn_nameptr) == '/') { 147 vrele(dp); 148 while (*(cnp->cn_nameptr) == '/') { 149 cnp->cn_nameptr++; 150 ndp->ni_pathlen--; 151 } 152 dp = ndp->ni_rootdir; 153 VREF(dp); 154 } 155 ndp->ni_startdir = dp; 156 error = lookup(ndp); 157 if (error) { 158 zfree(namei_zone, cnp->cn_pnbuf); 159 return (error); 160 } 161 /* 162 * Check for symbolic link 163 */ 164 if ((cnp->cn_flags & CNP_ISSYMLINK) == 0) { 165 if ((cnp->cn_flags & (CNP_SAVENAME | CNP_SAVESTART)) == 0) 166 zfree(namei_zone, cnp->cn_pnbuf); 167 else 168 cnp->cn_flags |= CNP_HASBUF; 169 170 if (vn_canvmio(ndp->ni_vp) == TRUE && 171 (cnp->cn_nameiop != NAMEI_DELETE) && 172 ((cnp->cn_flags & (CNP_NOOBJ|CNP_LOCKLEAF)) == 173 CNP_LOCKLEAF)) 174 vfs_object_create(ndp->ni_vp, ndp->ni_cnd.cn_td); 175 176 return (0); 177 } 178 if ((cnp->cn_flags & CNP_LOCKPARENT) && ndp->ni_pathlen == 1) 179 VOP_UNLOCK(ndp->ni_dvp, 0, cnp->cn_td); 180 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) { 181 error = ELOOP; 182 break; 183 } 184 if (ndp->ni_pathlen > 1) 185 cp = zalloc(namei_zone); 186 else 187 cp = cnp->cn_pnbuf; 188 aiov.iov_base = cp; 189 aiov.iov_len = MAXPATHLEN; 190 auio.uio_iov = &aiov; 191 auio.uio_iovcnt = 1; 192 auio.uio_offset = 0; 193 auio.uio_rw = UIO_READ; 194 auio.uio_segflg = UIO_SYSSPACE; 195 auio.uio_td = NULL; 196 auio.uio_resid = MAXPATHLEN; 197 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred); 198 if (error) { 199 if (ndp->ni_pathlen > 1) 200 zfree(namei_zone, cp); 201 break; 202 } 203 linklen = MAXPATHLEN - auio.uio_resid; 204 if (linklen == 0) { 205 if (ndp->ni_pathlen > 1) 206 zfree(namei_zone, cp); 207 error = ENOENT; 208 break; 209 } 210 if (linklen + ndp->ni_pathlen >= MAXPATHLEN) { 211 if (ndp->ni_pathlen > 1) 212 zfree(namei_zone, cp); 213 error = ENAMETOOLONG; 214 break; 215 } 216 if (ndp->ni_pathlen > 1) { 217 bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen); 218 zfree(namei_zone, cnp->cn_pnbuf); 219 cnp->cn_pnbuf = cp; 220 } else 221 cnp->cn_pnbuf[linklen] = '\0'; 222 ndp->ni_pathlen += linklen; 223 vput(ndp->ni_vp); 224 dp = ndp->ni_dvp; 225 } 226 zfree(namei_zone, cnp->cn_pnbuf); 227 vrele(ndp->ni_dvp); 228 vput(ndp->ni_vp); 229 ndp->ni_vp = NULL; 230 return (error); 231 } 232 233 /* 234 * Search a pathname. 235 * This is a very central and rather complicated routine. 236 * 237 * The pathname is pointed to by ni_ptr and is of length ni_pathlen. 238 * The starting directory is taken from ni_startdir. The pathname is 239 * descended until done, or a symbolic link is encountered. The variable 240 * ni_more is clear if the path is completed; it is set to one if a 241 * symbolic link needing interpretation is encountered. 242 * 243 * The flag argument is NAMEI_LOOKUP, CREATE, RENAME, or DELETE depending on 244 * whether the name is to be looked up, created, renamed, or deleted. 245 * When CREATE, RENAME, or DELETE is specified, information usable in 246 * creating, renaming, or deleting a directory entry may be calculated. 247 * If flag has LOCKPARENT or'ed into it, the parent directory is returned 248 * locked. If flag has WANTPARENT or'ed into it, the parent directory is 249 * returned unlocked. Otherwise the parent directory is not returned. If 250 * the target of the pathname exists and LOCKLEAF is or'ed into the flag 251 * the target is returned locked, otherwise it is returned unlocked. 252 * When creating or renaming and LOCKPARENT is specified, the target may not 253 * be ".". When deleting and LOCKPARENT is specified, the target may be ".". 254 * 255 * Overall outline of lookup: 256 * 257 * dirloop: 258 * identify next component of name at ndp->ni_ptr 259 * handle degenerate case where name is null string 260 * if .. and crossing mount points and on mounted filesys, find parent 261 * call VOP_LOOKUP routine for next component name 262 * directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set 263 * component vnode returned in ni_vp (if it exists), locked. 264 * if result vnode is mounted on and crossing mount points, 265 * find mounted on vnode 266 * if more components of name, do next level at dirloop 267 * return the answer in ni_vp, locked if LOCKLEAF set 268 * if LOCKPARENT set, return locked parent in ni_dvp 269 * if WANTPARENT set, return unlocked parent in ni_dvp 270 */ 271 int 272 lookup(ndp) 273 struct nameidata *ndp; 274 { 275 char *cp; /* pointer into pathname argument */ 276 struct vnode *dp = 0; /* the directory we are searching */ 277 struct vnode *tdp; /* saved dp */ 278 struct mount *mp; /* mount table entry */ 279 int docache; /* == 0 do not cache last component */ 280 int wantparent; /* 1 => wantparent or lockparent flag */ 281 int rdonly; /* lookup read-only flag bit */ 282 int trailing_slash; 283 int error = 0; 284 int dpunlocked = 0; /* dp has already been unlocked */ 285 struct componentname *cnp = &ndp->ni_cnd; 286 struct thread *td = cnp->cn_td; 287 288 /* 289 * Setup: break out flag bits into variables. 290 */ 291 wantparent = cnp->cn_flags & (CNP_LOCKPARENT | CNP_WANTPARENT); 292 docache = (cnp->cn_flags & CNP_NOCACHE) ^ CNP_NOCACHE; 293 if (cnp->cn_nameiop == NAMEI_DELETE || 294 (wantparent && cnp->cn_nameiop != NAMEI_CREATE && 295 cnp->cn_nameiop != NAMEI_LOOKUP)) 296 docache = 0; 297 rdonly = cnp->cn_flags & CNP_RDONLY; 298 ndp->ni_dvp = NULL; 299 cnp->cn_flags &= ~CNP_ISSYMLINK; 300 dp = ndp->ni_startdir; 301 ndp->ni_startdir = NULLVP; 302 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY, td); 303 304 dirloop: 305 /* 306 * Search a new directory. 307 * 308 * The last component of the filename is left accessible via 309 * cnp->cn_nameptr for callers that need the name. Callers needing 310 * the name set the CNP_SAVENAME flag. When done, they assume 311 * responsibility for freeing the pathname buffer. 312 */ 313 cnp->cn_consume = 0; 314 for (cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++) 315 continue; 316 cnp->cn_namelen = cp - cnp->cn_nameptr; 317 if (cnp->cn_namelen > NAME_MAX) { 318 error = ENAMETOOLONG; 319 goto bad; 320 } 321 #ifdef NAMEI_DIAGNOSTIC 322 { char c = *cp; 323 *cp = '\0'; 324 printf("{%s}: ", cnp->cn_nameptr); 325 *cp = c; } 326 #endif 327 ndp->ni_pathlen -= cnp->cn_namelen; 328 ndp->ni_next = cp; 329 330 /* 331 * Replace multiple slashes by a single slash and trailing slashes 332 * by a null. This must be done before VOP_LOOKUP() because some 333 * fs's don't know about trailing slashes. Remember if there were 334 * trailing slashes to handle symlinks, existing non-directories 335 * and non-existing files that won't be directories specially later. 336 */ 337 trailing_slash = 0; 338 while (*cp == '/' && (cp[1] == '/' || cp[1] == '\0')) { 339 cp++; 340 ndp->ni_pathlen--; 341 if (*cp == '\0') { 342 trailing_slash = 1; 343 *ndp->ni_next = '\0'; /* XXX for direnter() ... */ 344 } 345 } 346 ndp->ni_next = cp; 347 348 cnp->cn_flags |= CNP_MAKEENTRY; 349 if (*cp == '\0' && docache == 0) 350 cnp->cn_flags &= ~CNP_MAKEENTRY; 351 if (cnp->cn_namelen == 2 && 352 cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.') 353 cnp->cn_flags |= CNP_ISDOTDOT; 354 else 355 cnp->cn_flags &= ~CNP_ISDOTDOT; 356 if (*ndp->ni_next == 0) 357 cnp->cn_flags |= CNP_ISLASTCN; 358 else 359 cnp->cn_flags &= ~CNP_ISLASTCN; 360 361 362 /* 363 * Check for degenerate name (e.g. / or "") 364 * which is a way of talking about a directory, 365 * e.g. like "/." or ".". 366 */ 367 if (cnp->cn_nameptr[0] == '\0') { 368 if (dp->v_type != VDIR) { 369 error = ENOTDIR; 370 goto bad; 371 } 372 if (cnp->cn_nameiop != NAMEI_LOOKUP) { 373 error = EISDIR; 374 goto bad; 375 } 376 if (wantparent) { 377 ndp->ni_dvp = dp; 378 VREF(dp); 379 } 380 ndp->ni_vp = dp; 381 if (!(cnp->cn_flags & (CNP_LOCKPARENT | CNP_LOCKLEAF))) 382 VOP_UNLOCK(dp, 0, cnp->cn_td); 383 /* XXX This should probably move to the top of function. */ 384 if (cnp->cn_flags & CNP_SAVESTART) 385 panic("lookup: CNP_SAVESTART"); 386 return (0); 387 } 388 389 /* 390 * Handle "..": two special cases. 391 * 1. If at root directory (e.g. after chroot) 392 * or at absolute root directory 393 * then ignore it so can't get out. 394 * 2. If this vnode is the root of a mounted 395 * filesystem, then replace it with the 396 * vnode which was mounted on so we take the 397 * .. in the other file system. 398 * 3. If the vnode is the top directory of 399 * the jail or chroot, don't let them out. 400 */ 401 if (cnp->cn_flags & CNP_ISDOTDOT) { 402 for (;;) { 403 if (dp == ndp->ni_rootdir || 404 dp == ndp->ni_topdir || 405 dp == rootvnode) { 406 ndp->ni_dvp = dp; 407 ndp->ni_vp = dp; 408 VREF(dp); 409 goto nextname; 410 } 411 if ((dp->v_flag & VROOT) == 0 || 412 (cnp->cn_flags & CNP_NOCROSSMOUNT)) 413 break; 414 if (dp->v_mount == NULL) { /* forced unmount */ 415 error = EBADF; 416 goto bad; 417 } 418 tdp = dp; 419 dp = dp->v_mount->mnt_vnodecovered; 420 vput(tdp); 421 VREF(dp); 422 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY, td); 423 } 424 } 425 426 /* 427 * We now have a segment name to search for, and a directory to search. 428 */ 429 unionlookup: 430 ndp->ni_dvp = dp; 431 ndp->ni_vp = NULL; 432 cnp->cn_flags &= ~CNP_PDIRUNLOCK; 433 ASSERT_VOP_LOCKED(dp, "lookup"); 434 if ((error = VOP_LOOKUP(dp, &ndp->ni_vp, cnp)) != 0) { 435 KASSERT(ndp->ni_vp == NULL, ("leaf should be empty")); 436 #ifdef NAMEI_DIAGNOSTIC 437 printf("not found\n"); 438 #endif 439 if ((error == ENOENT) && 440 (dp->v_flag & VROOT) && (dp->v_mount != NULL) && 441 (dp->v_mount->mnt_flag & MNT_UNION)) { 442 tdp = dp; 443 dp = dp->v_mount->mnt_vnodecovered; 444 if (cnp->cn_flags & CNP_PDIRUNLOCK) 445 vrele(tdp); 446 else 447 vput(tdp); 448 VREF(dp); 449 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY, td); 450 goto unionlookup; 451 } 452 453 if (error != EJUSTRETURN) 454 goto bad; 455 /* 456 * If creating and at end of pathname, then can consider 457 * allowing file to be created. 458 */ 459 if (rdonly) { 460 error = EROFS; 461 goto bad; 462 } 463 if (*cp == '\0' && trailing_slash && 464 !(cnp->cn_flags & CNP_WILLBEDIR)) { 465 error = ENOENT; 466 goto bad; 467 } 468 /* 469 * We return with ni_vp NULL to indicate that the entry 470 * doesn't currently exist, leaving a pointer to the 471 * (possibly locked) directory inode in ndp->ni_dvp. 472 */ 473 if (cnp->cn_flags & CNP_SAVESTART) { 474 ndp->ni_startdir = ndp->ni_dvp; 475 VREF(ndp->ni_startdir); 476 } 477 return (0); 478 } 479 #ifdef NAMEI_DIAGNOSTIC 480 printf("found\n"); 481 #endif 482 483 ASSERT_VOP_LOCKED(ndp->ni_vp, "lookup"); 484 485 /* 486 * Take into account any additional components consumed by 487 * the underlying filesystem. 488 */ 489 if (cnp->cn_consume > 0) { 490 cnp->cn_nameptr += cnp->cn_consume; 491 ndp->ni_next += cnp->cn_consume; 492 ndp->ni_pathlen -= cnp->cn_consume; 493 cnp->cn_consume = 0; 494 } 495 496 dp = ndp->ni_vp; 497 498 /* 499 * Check to see if the vnode has been mounted on; 500 * if so find the root of the mounted file system. 501 */ 502 while (dp->v_type == VDIR && (mp = dp->v_mountedhere) && 503 (cnp->cn_flags & CNP_NOCROSSMOUNT) == 0) { 504 if (vfs_busy(mp, 0, 0, td)) 505 continue; 506 VOP_UNLOCK(dp, 0, td); 507 error = VFS_ROOT(mp, &tdp); 508 vfs_unbusy(mp, td); 509 if (error) { 510 dpunlocked = 1; 511 goto bad2; 512 } 513 cache_mount(dp, tdp); 514 vrele(dp); 515 ndp->ni_vp = dp = tdp; 516 } 517 518 /* 519 * Check for symbolic link 520 */ 521 if ((dp->v_type == VLNK) && 522 ((cnp->cn_flags & CNP_FOLLOW) || trailing_slash || 523 *ndp->ni_next == '/')) { 524 cnp->cn_flags |= CNP_ISSYMLINK; 525 if (dp->v_mount == NULL) { 526 /* We can't know whether the directory was mounted with 527 * NOSYMFOLLOW, so we can't follow safely. */ 528 error = EBADF; 529 goto bad2; 530 } 531 if (dp->v_mount->mnt_flag & MNT_NOSYMFOLLOW) { 532 error = EACCES; 533 goto bad2; 534 } 535 return (0); 536 } 537 538 /* 539 * Check for bogus trailing slashes. 540 */ 541 if (trailing_slash && dp->v_type != VDIR) { 542 error = ENOTDIR; 543 goto bad2; 544 } 545 546 nextname: 547 /* 548 * Not a symbolic link. If more pathname, 549 * continue at next component, else return. 550 */ 551 if (*ndp->ni_next == '/') { 552 cnp->cn_nameptr = ndp->ni_next; 553 while (*cnp->cn_nameptr == '/') { 554 cnp->cn_nameptr++; 555 ndp->ni_pathlen--; 556 } 557 if (ndp->ni_dvp != ndp->ni_vp) 558 ASSERT_VOP_UNLOCKED(ndp->ni_dvp, "lookup"); 559 vrele(ndp->ni_dvp); 560 goto dirloop; 561 } 562 /* 563 * Disallow directory write attempts on read-only file systems. 564 */ 565 if (rdonly && 566 (cnp->cn_nameiop == NAMEI_DELETE || cnp->cn_nameiop == NAMEI_RENAME)) { 567 error = EROFS; 568 goto bad2; 569 } 570 if (cnp->cn_flags & CNP_SAVESTART) { 571 ndp->ni_startdir = ndp->ni_dvp; 572 VREF(ndp->ni_startdir); 573 } 574 if (!wantparent) 575 vrele(ndp->ni_dvp); 576 577 if ((cnp->cn_flags & CNP_LOCKLEAF) == 0) 578 VOP_UNLOCK(dp, 0, td); 579 return (0); 580 581 bad2: 582 if ((cnp->cn_flags & (CNP_LOCKPARENT | CNP_PDIRUNLOCK)) == CNP_LOCKPARENT && 583 *ndp->ni_next == '\0') 584 VOP_UNLOCK(ndp->ni_dvp, 0, td); 585 vrele(ndp->ni_dvp); 586 bad: 587 if (dpunlocked) 588 vrele(dp); 589 else 590 vput(dp); 591 ndp->ni_vp = NULL; 592 return (error); 593 } 594 595 /* 596 * relookup - lookup a path name component 597 * Used by lookup to re-aquire things. 598 */ 599 int 600 relookup(dvp, vpp, cnp) 601 struct vnode *dvp, **vpp; 602 struct componentname *cnp; 603 { 604 struct thread *td = cnp->cn_td; 605 struct vnode *dp = 0; /* the directory we are searching */ 606 int docache; /* == 0 do not cache last component */ 607 int wantparent; /* 1 => wantparent or lockparent flag */ 608 int rdonly; /* lookup read-only flag bit */ 609 int error = 0; 610 #ifdef NAMEI_DIAGNOSTIC 611 int newhash; /* DEBUG: check name hash */ 612 char *cp; /* DEBUG: check name ptr/len */ 613 #endif 614 615 /* 616 * Setup: break out flag bits into variables. 617 */ 618 wantparent = cnp->cn_flags & (CNP_LOCKPARENT|CNP_WANTPARENT); 619 docache = (cnp->cn_flags & CNP_NOCACHE) ^ CNP_NOCACHE; 620 if (cnp->cn_nameiop == NAMEI_DELETE || 621 (wantparent && cnp->cn_nameiop != NAMEI_CREATE)) 622 docache = 0; 623 rdonly = cnp->cn_flags & CNP_RDONLY; 624 cnp->cn_flags &= ~CNP_ISSYMLINK; 625 dp = dvp; 626 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY, td); 627 628 /* dirloop: */ 629 /* 630 * Search a new directory. 631 * 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 CNP_SAVENAME flag. When done, they assume 635 * responsibility for freeing the pathname buffer. 636 */ 637 #ifdef NAMEI_DIAGNOSTIC 638 if (cnp->cn_namelen != cp - cnp->cn_nameptr) 639 panic ("relookup: bad len"); 640 if (*cp != 0) 641 panic("relookup: not last component"); 642 printf("{%s}: ", cnp->cn_nameptr); 643 #endif 644 645 /* 646 * Check for degenerate name (e.g. / or "") 647 * which is a way of talking about a directory, 648 * e.g. like "/." or ".". 649 */ 650 if (cnp->cn_nameptr[0] == '\0') { 651 if (cnp->cn_nameiop != NAMEI_LOOKUP || wantparent) { 652 error = EISDIR; 653 goto bad; 654 } 655 if (dp->v_type != VDIR) { 656 error = ENOTDIR; 657 goto bad; 658 } 659 if (!(cnp->cn_flags & CNP_LOCKLEAF)) 660 VOP_UNLOCK(dp, 0, td); 661 *vpp = dp; 662 /* XXX This should probably move to the top of function. */ 663 if (cnp->cn_flags & CNP_SAVESTART) 664 panic("lookup: CNP_SAVESTART"); 665 return (0); 666 } 667 668 if (cnp->cn_flags & CNP_ISDOTDOT) 669 panic ("relookup: lookup on dot-dot"); 670 671 /* 672 * We now have a segment name to search for, and a directory to search. 673 */ 674 if ((error = VOP_LOOKUP(dp, vpp, cnp)) != 0) { 675 KASSERT(*vpp == NULL, ("leaf should be empty")); 676 if (error != EJUSTRETURN) 677 goto bad; 678 /* 679 * If creating and at end of pathname, then can consider 680 * allowing file to be created. 681 */ 682 if (rdonly) { 683 error = EROFS; 684 goto bad; 685 } 686 /* ASSERT(dvp == ndp->ni_startdir) */ 687 if (cnp->cn_flags & CNP_SAVESTART) 688 VREF(dvp); 689 /* 690 * We return with ni_vp NULL to indicate that the entry 691 * doesn't currently exist, leaving a pointer to the 692 * (possibly locked) directory inode in ndp->ni_dvp. 693 */ 694 return (0); 695 } 696 dp = *vpp; 697 698 /* 699 * Check for symbolic link 700 */ 701 KASSERT(dp->v_type != VLNK || !(cnp->cn_flags & CNP_FOLLOW), 702 ("relookup: symlink found.\n")); 703 704 /* 705 * Disallow directory write attempts on read-only file systems. 706 */ 707 if (rdonly && 708 (cnp->cn_nameiop == NAMEI_DELETE || cnp->cn_nameiop == NAMEI_RENAME)) { 709 error = EROFS; 710 goto bad2; 711 } 712 /* ASSERT(dvp == ndp->ni_startdir) */ 713 if (cnp->cn_flags & CNP_SAVESTART) 714 VREF(dvp); 715 716 if (!wantparent) 717 vrele(dvp); 718 719 if (vn_canvmio(dp) == TRUE && 720 ((cnp->cn_flags & (CNP_NOOBJ|CNP_LOCKLEAF)) == CNP_LOCKLEAF)) 721 vfs_object_create(dp, cnp->cn_td); 722 723 if ((cnp->cn_flags & CNP_LOCKLEAF) == 0) 724 VOP_UNLOCK(dp, 0, td); 725 return (0); 726 727 bad2: 728 if ((cnp->cn_flags & CNP_LOCKPARENT) && (cnp->cn_flags & CNP_ISLASTCN)) 729 VOP_UNLOCK(dvp, 0, td); 730 vrele(dvp); 731 bad: 732 vput(dp); 733 *vpp = NULL; 734 return (error); 735 } 736