1 /* $NetBSD: vfs_lookup.c,v 1.64 2005/12/11 12:24:30 christos 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. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * @(#)vfs_lookup.c 8.10 (Berkeley) 5/27/95 37 */ 38 39 #include <sys/cdefs.h> 40 __KERNEL_RCSID(0, "$NetBSD: vfs_lookup.c,v 1.64 2005/12/11 12:24:30 christos Exp $"); 41 42 #include "opt_ktrace.h" 43 #include "opt_systrace.h" 44 45 #include <sys/param.h> 46 #include <sys/systm.h> 47 #include <sys/kernel.h> 48 #include <sys/syslimits.h> 49 #include <sys/time.h> 50 #include <sys/namei.h> 51 #include <sys/vnode.h> 52 #include <sys/mount.h> 53 #include <sys/errno.h> 54 #include <sys/filedesc.h> 55 #include <sys/hash.h> 56 #include <sys/malloc.h> 57 #include <sys/proc.h> 58 #include <sys/syslog.h> 59 60 #ifdef KTRACE 61 #include <sys/ktrace.h> 62 #endif 63 #ifdef SYSTRACE 64 #include <sys/systrace.h> 65 #endif 66 67 struct pool pnbuf_pool; /* pathname buffer pool */ 68 struct pool_cache pnbuf_cache; /* pathname buffer cache */ 69 70 /* 71 * Substitute replacement text for 'magic' strings in symlinks. 72 * Returns 0 if successful, and returns non-zero if an error 73 * occurs. (Currently, the only possible error is running out 74 * of temporary pathname space.) 75 * 76 * Looks for "@<string>" and "@<string>/", where <string> is a 77 * recognized 'magic' string. Replaces the "@<string>" with the 78 * appropriate replacement text. (Note that in some cases the 79 * replacement text may have zero length.) 80 * 81 * This would have been table driven, but the variance in 82 * replacement strings (and replacement string lengths) made 83 * that impractical. 84 */ 85 #define VNL(x) \ 86 (sizeof(x) - 1) 87 88 #define VO '{' 89 #define VC '}' 90 91 #define MATCH(str) \ 92 ((termchar == '/' && i + VNL(str) == *len) || \ 93 (i + VNL(str) < *len && \ 94 cp[i + VNL(str)] == termchar)) && \ 95 !strncmp((str), &cp[i], VNL(str)) 96 97 #define SUBSTITUTE(m, s, sl) \ 98 if ((newlen + (sl)) > MAXPATHLEN) \ 99 return (1); \ 100 i += VNL(m); \ 101 if (termchar != '/') \ 102 i++; \ 103 memcpy(&tmp[newlen], (s), (sl)); \ 104 newlen += (sl); \ 105 change = 1; \ 106 termchar = '/'; 107 108 static int 109 symlink_magic(struct proc *p, char *cp, int *len) 110 { 111 char tmp[MAXPATHLEN]; 112 int change, i, newlen; 113 int termchar = '/'; 114 115 for (change = i = newlen = 0; i < *len; ) { 116 if (cp[i] != '@') { 117 tmp[newlen++] = cp[i++]; 118 continue; 119 } 120 121 i++; 122 123 /* Check for @{var} syntax. */ 124 if (cp[i] == VO) { 125 termchar = VC; 126 i++; 127 } 128 129 /* 130 * The following checks should be ordered according 131 * to frequency of use. 132 */ 133 if (MATCH("machine_arch")) { 134 SUBSTITUTE("machine_arch", MACHINE_ARCH, 135 sizeof(MACHINE_ARCH) - 1); 136 } else if (MATCH("machine")) { 137 SUBSTITUTE("machine", MACHINE, 138 sizeof(MACHINE) - 1); 139 } else if (MATCH("hostname")) { 140 SUBSTITUTE("hostname", hostname, 141 hostnamelen); 142 } else if (MATCH("osrelease")) { 143 SUBSTITUTE("osrelease", osrelease, 144 strlen(osrelease)); 145 } else if (MATCH("emul")) { 146 SUBSTITUTE("emul", p->p_emul->e_name, 147 strlen(p->p_emul->e_name)); 148 } else if (MATCH("kernel_ident")) { 149 SUBSTITUTE("kernel_ident", kernel_ident, 150 strlen(kernel_ident)); 151 } else if (MATCH("domainname")) { 152 SUBSTITUTE("domainname", domainname, 153 domainnamelen); 154 } else if (MATCH("ostype")) { 155 SUBSTITUTE("ostype", ostype, 156 strlen(ostype)); 157 } else { 158 tmp[newlen++] = '@'; 159 if (termchar == VC) 160 tmp[newlen++] = VO; 161 } 162 } 163 164 if (! change) 165 return (0); 166 167 memcpy(cp, tmp, newlen); 168 *len = newlen; 169 170 return (0); 171 } 172 173 #undef VNL 174 #undef VO 175 #undef VC 176 #undef MATCH 177 #undef SUBSTITUTE 178 179 /* 180 * Convert a pathname into a pointer to a locked inode. 181 * 182 * The FOLLOW flag is set when symbolic links are to be followed 183 * when they occur at the end of the name translation process. 184 * Symbolic links are always followed for all other pathname 185 * components other than the last. 186 * 187 * The segflg defines whether the name is to be copied from user 188 * space or kernel space. 189 * 190 * Overall outline of namei: 191 * 192 * copy in name 193 * get starting directory 194 * while (!done && !error) { 195 * call lookup to search path. 196 * if symbolic link, massage name in buffer and continue 197 * } 198 */ 199 int 200 namei(struct nameidata *ndp) 201 { 202 struct cwdinfo *cwdi; /* pointer to cwd state */ 203 char *cp; /* pointer into pathname argument */ 204 struct vnode *dp; /* the directory we are searching */ 205 struct iovec aiov; /* uio for reading symbolic links */ 206 struct uio auio; 207 int error, linklen; 208 struct componentname *cnp = &ndp->ni_cnd; 209 210 #ifdef DIAGNOSTIC 211 if (!cnp->cn_cred || !cnp->cn_lwp) 212 panic("namei: bad cred/proc"); 213 if (cnp->cn_nameiop & (~OPMASK)) 214 panic("namei: nameiop contaminated with flags"); 215 if (cnp->cn_flags & OPMASK) 216 panic("namei: flags contaminated with nameiops"); 217 #endif 218 cwdi = cnp->cn_lwp->l_proc->p_cwdi; 219 220 /* 221 * Get a buffer for the name to be translated, and copy the 222 * name into the buffer. 223 */ 224 if ((cnp->cn_flags & HASBUF) == 0) 225 cnp->cn_pnbuf = PNBUF_GET(); 226 if (ndp->ni_segflg == UIO_SYSSPACE) 227 error = copystr(ndp->ni_dirp, cnp->cn_pnbuf, 228 MAXPATHLEN, &ndp->ni_pathlen); 229 else 230 error = copyinstr(ndp->ni_dirp, cnp->cn_pnbuf, 231 MAXPATHLEN, &ndp->ni_pathlen); 232 233 /* 234 * POSIX.1 requirement: "" is not a valid file name. 235 */ 236 if (!error && ndp->ni_pathlen == 1) 237 error = ENOENT; 238 239 if (error) { 240 PNBUF_PUT(cnp->cn_pnbuf); 241 ndp->ni_vp = NULL; 242 return (error); 243 } 244 ndp->ni_loopcnt = 0; 245 246 #ifdef KTRACE 247 if (KTRPOINT(cnp->cn_lwp->l_proc, KTR_NAMEI)) 248 ktrnamei(cnp->cn_lwp, cnp->cn_pnbuf); 249 #endif 250 #ifdef SYSTRACE 251 if (ISSET(cnp->cn_lwp->l_proc->p_flag, P_SYSTRACE)) 252 systrace_namei(ndp); 253 #endif 254 255 /* 256 * Get starting point for the translation. 257 */ 258 if ((ndp->ni_rootdir = cwdi->cwdi_rdir) == NULL) 259 ndp->ni_rootdir = rootvnode; 260 /* 261 * Check if starting from root directory or current directory. 262 */ 263 if (cnp->cn_pnbuf[0] == '/') { 264 dp = ndp->ni_rootdir; 265 VREF(dp); 266 } else { 267 dp = cwdi->cwdi_cdir; 268 VREF(dp); 269 } 270 for (;;) { 271 if (!dp->v_mount) 272 { 273 /* Give up if the directory is no longer mounted */ 274 PNBUF_PUT(cnp->cn_pnbuf); 275 return (ENOENT); 276 } 277 cnp->cn_nameptr = cnp->cn_pnbuf; 278 ndp->ni_startdir = dp; 279 if ((error = lookup(ndp)) != 0) { 280 PNBUF_PUT(cnp->cn_pnbuf); 281 return (error); 282 } 283 /* 284 * Check for symbolic link 285 */ 286 if ((cnp->cn_flags & ISSYMLINK) == 0) { 287 if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0) 288 PNBUF_PUT(cnp->cn_pnbuf); 289 else 290 cnp->cn_flags |= HASBUF; 291 return (0); 292 } 293 if ((cnp->cn_flags & LOCKPARENT) && (cnp->cn_flags & ISLASTCN)) 294 VOP_UNLOCK(ndp->ni_dvp, 0); 295 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) { 296 error = ELOOP; 297 break; 298 } 299 if (ndp->ni_vp->v_mount->mnt_flag & MNT_SYMPERM) { 300 error = VOP_ACCESS(ndp->ni_vp, VEXEC, cnp->cn_cred, 301 cnp->cn_lwp); 302 if (error != 0) 303 break; 304 } 305 if (ndp->ni_pathlen > 1) 306 cp = PNBUF_GET(); 307 else 308 cp = cnp->cn_pnbuf; 309 aiov.iov_base = cp; 310 aiov.iov_len = MAXPATHLEN; 311 auio.uio_iov = &aiov; 312 auio.uio_iovcnt = 1; 313 auio.uio_offset = 0; 314 auio.uio_rw = UIO_READ; 315 auio.uio_segflg = UIO_SYSSPACE; 316 auio.uio_lwp = NULL; 317 auio.uio_resid = MAXPATHLEN; 318 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred); 319 if (error) { 320 badlink: 321 if (ndp->ni_pathlen > 1) 322 PNBUF_PUT(cp); 323 break; 324 } 325 linklen = MAXPATHLEN - auio.uio_resid; 326 if (linklen == 0) { 327 error = ENOENT; 328 goto badlink; 329 } 330 /* 331 * Do symlink substitution, if appropriate, and 332 * check length for potential overflow. 333 */ 334 if (((ndp->ni_vp->v_mount->mnt_flag & MNT_MAGICLINKS) && 335 symlink_magic(cnp->cn_lwp->l_proc, cp, &linklen)) || 336 (linklen + ndp->ni_pathlen >= MAXPATHLEN)) { 337 error = ENAMETOOLONG; 338 goto badlink; 339 } 340 if (ndp->ni_pathlen > 1) { 341 memcpy(cp + linklen, ndp->ni_next, ndp->ni_pathlen); 342 PNBUF_PUT(cnp->cn_pnbuf); 343 cnp->cn_pnbuf = cp; 344 } else 345 cnp->cn_pnbuf[linklen] = '\0'; 346 ndp->ni_pathlen += linklen; 347 vput(ndp->ni_vp); 348 dp = ndp->ni_dvp; 349 /* 350 * Check if root directory should replace current directory. 351 */ 352 if (cnp->cn_pnbuf[0] == '/') { 353 vrele(dp); 354 dp = ndp->ni_rootdir; 355 VREF(dp); 356 } 357 } 358 PNBUF_PUT(cnp->cn_pnbuf); 359 vrele(ndp->ni_dvp); 360 vput(ndp->ni_vp); 361 ndp->ni_vp = NULL; 362 return (error); 363 } 364 365 /* 366 * Determine the namei hash (for cn_hash) for name. 367 * If *ep != NULL, hash from name to ep-1. 368 * If *ep == NULL, hash from name until the first NUL or '/', and 369 * return the location of this termination character in *ep. 370 * 371 * This function returns an equivalent hash to the MI hash32_strn(). 372 * The latter isn't used because in the *ep == NULL case, determining 373 * the length of the string to the first NUL or `/' and then calling 374 * hash32_strn() involves unnecessary double-handling of the data. 375 */ 376 uint32_t 377 namei_hash(const char *name, const char **ep) 378 { 379 uint32_t hash; 380 381 hash = HASH32_STR_INIT; 382 if (*ep != NULL) { 383 for (; name < *ep; name++) 384 hash = hash * 33 + *(const uint8_t *)name; 385 } else { 386 for (; *name != '\0' && *name != '/'; name++) 387 hash = hash * 33 + *(const uint8_t *)name; 388 *ep = name; 389 } 390 return (hash + (hash >> 5)); 391 } 392 393 /* 394 * Search a pathname. 395 * This is a very central and rather complicated routine. 396 * 397 * The pathname is pointed to by ni_ptr and is of length ni_pathlen. 398 * The starting directory is taken from ni_startdir. The pathname is 399 * descended until done, or a symbolic link is encountered. The variable 400 * ni_more is clear if the path is completed; it is set to one if a 401 * symbolic link needing interpretation is encountered. 402 * 403 * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on 404 * whether the name is to be looked up, created, renamed, or deleted. 405 * When CREATE, RENAME, or DELETE is specified, information usable in 406 * creating, renaming, or deleting a directory entry may be calculated. 407 * If flag has LOCKPARENT or'ed into it, the parent directory is returned 408 * locked. If flag has WANTPARENT or'ed into it, the parent directory is 409 * returned unlocked. Otherwise the parent directory is not returned. If 410 * the target of the pathname exists and LOCKLEAF is or'ed into the flag 411 * the target is returned locked, otherwise it is returned unlocked. 412 * When creating or renaming and LOCKPARENT is specified, the target may not 413 * be ".". When deleting and LOCKPARENT is specified, the target may be ".". 414 * 415 * Overall outline of lookup: 416 * 417 * dirloop: 418 * identify next component of name at ndp->ni_ptr 419 * handle degenerate case where name is null string 420 * if .. and crossing mount points and on mounted filesys, find parent 421 * call VOP_LOOKUP routine for next component name 422 * directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set 423 * component vnode returned in ni_vp (if it exists), locked. 424 * if result vnode is mounted on and crossing mount points, 425 * find mounted on vnode 426 * if more components of name, do next level at dirloop 427 * return the answer in ni_vp, locked if LOCKLEAF set 428 * if LOCKPARENT set, return locked parent in ni_dvp 429 * if WANTPARENT set, return unlocked parent in ni_dvp 430 */ 431 int 432 lookup(struct nameidata *ndp) 433 { 434 const char *cp; /* pointer into pathname argument */ 435 struct vnode *dp = 0; /* the directory we are searching */ 436 struct vnode *tdp; /* saved dp */ 437 struct mount *mp; /* mount table entry */ 438 int docache; /* == 0 do not cache last component */ 439 int wantparent; /* 1 => wantparent or lockparent flag */ 440 int rdonly; /* lookup read-only flag bit */ 441 int error = 0; 442 int slashes; 443 int dpunlocked = 0; /* dp has already been unlocked */ 444 struct componentname *cnp = &ndp->ni_cnd; 445 struct lwp *l = cnp->cn_lwp; 446 447 /* 448 * Setup: break out flag bits into variables. 449 */ 450 wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT); 451 docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE; 452 if (cnp->cn_nameiop == DELETE || 453 (wantparent && cnp->cn_nameiop != CREATE)) 454 docache = 0; 455 rdonly = cnp->cn_flags & RDONLY; 456 ndp->ni_dvp = NULL; 457 cnp->cn_flags &= ~ISSYMLINK; 458 dp = ndp->ni_startdir; 459 ndp->ni_startdir = NULLVP; 460 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY); 461 462 /* 463 * If we have a leading string of slashes, remove them, and just make 464 * sure the current node is a directory. 465 */ 466 cp = cnp->cn_nameptr; 467 if (*cp == '/') { 468 do { 469 cp++; 470 } while (*cp == '/'); 471 ndp->ni_pathlen -= cp - cnp->cn_nameptr; 472 cnp->cn_nameptr = cp; 473 474 if (dp->v_type != VDIR) { 475 error = ENOTDIR; 476 goto bad; 477 } 478 479 /* 480 * If we've exhausted the path name, then just return the 481 * current node. If the caller requested the parent node (i.e. 482 * it's a CREATE, DELETE, or RENAME), and we don't have one 483 * (because this is the root directory), then we must fail. 484 */ 485 if (cnp->cn_nameptr[0] == '\0') { 486 if (ndp->ni_dvp == NULL && wantparent) { 487 error = EISDIR; 488 goto bad; 489 } 490 ndp->ni_vp = dp; 491 cnp->cn_flags |= ISLASTCN; 492 goto terminal; 493 } 494 } 495 496 dirloop: 497 /* 498 * Search a new directory. 499 * 500 * The cn_hash value is for use by vfs_cache. 501 * The last component of the filename is left accessible via 502 * cnp->cn_nameptr for callers that need the name. Callers needing 503 * the name set the SAVENAME flag. When done, they assume 504 * responsibility for freeing the pathname buffer. 505 */ 506 cnp->cn_consume = 0; 507 cp = NULL; 508 cnp->cn_hash = namei_hash(cnp->cn_nameptr, &cp); 509 cnp->cn_namelen = cp - cnp->cn_nameptr; 510 if (cnp->cn_namelen > NAME_MAX) { 511 error = ENAMETOOLONG; 512 goto bad; 513 } 514 #ifdef NAMEI_DIAGNOSTIC 515 { char c = *cp; 516 *(char *)cp = '\0'; 517 printf("{%s}: ", cnp->cn_nameptr); 518 *(char *)cp = c; } 519 #endif /* NAMEI_DIAGNOSTIC */ 520 ndp->ni_pathlen -= cnp->cn_namelen; 521 ndp->ni_next = cp; 522 /* 523 * If this component is followed by a slash, then move the pointer to 524 * the next component forward, and remember that this component must be 525 * a directory. 526 */ 527 if (*cp == '/') { 528 do { 529 cp++; 530 } while (*cp == '/'); 531 slashes = cp - ndp->ni_next; 532 ndp->ni_pathlen -= slashes; 533 ndp->ni_next = cp; 534 cnp->cn_flags |= REQUIREDIR; 535 } else { 536 slashes = 0; 537 cnp->cn_flags &= ~REQUIREDIR; 538 } 539 /* 540 * We do special processing on the last component, whether or not it's 541 * a directory. Cache all intervening lookups, but not the final one. 542 */ 543 if (*cp == '\0') { 544 if (docache) 545 cnp->cn_flags |= MAKEENTRY; 546 else 547 cnp->cn_flags &= ~MAKEENTRY; 548 cnp->cn_flags |= ISLASTCN; 549 } else { 550 cnp->cn_flags |= MAKEENTRY; 551 cnp->cn_flags &= ~ISLASTCN; 552 } 553 if (cnp->cn_namelen == 2 && 554 cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.') 555 cnp->cn_flags |= ISDOTDOT; 556 else 557 cnp->cn_flags &= ~ISDOTDOT; 558 559 /* 560 * Handle "..": two special cases. 561 * 1. If at root directory (e.g. after chroot) 562 * or at absolute root directory 563 * then ignore it so can't get out. 564 * 1a. If we have somehow gotten out of a jail, warn 565 * and also ignore it so we can't get farther out. 566 * 2. If this vnode is the root of a mounted 567 * filesystem, then replace it with the 568 * vnode which was mounted on so we take the 569 * .. in the other file system. 570 */ 571 if (cnp->cn_flags & ISDOTDOT) { 572 struct proc *p = l->l_proc; 573 574 for (;;) { 575 if (dp == ndp->ni_rootdir || dp == rootvnode) { 576 ndp->ni_dvp = dp; 577 ndp->ni_vp = dp; 578 VREF(dp); 579 goto nextname; 580 } 581 if (ndp->ni_rootdir != rootvnode) { 582 int retval; 583 VOP_UNLOCK(dp, 0); 584 retval = vn_isunder(dp, ndp->ni_rootdir, l); 585 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY); 586 if (!retval) { 587 /* Oops! We got out of jail! */ 588 log(LOG_WARNING, 589 "chrooted pid %d uid %d (%s) " 590 "detected outside of its chroot\n", 591 p->p_pid, p->p_ucred->cr_uid, 592 p->p_comm); 593 /* Put us at the jail root. */ 594 vput(dp); 595 dp = ndp->ni_rootdir; 596 ndp->ni_dvp = dp; 597 ndp->ni_vp = dp; 598 VREF(dp); 599 VREF(dp); 600 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY); 601 goto nextname; 602 } 603 } 604 if ((dp->v_flag & VROOT) == 0 || 605 (cnp->cn_flags & NOCROSSMOUNT)) 606 break; 607 tdp = dp; 608 dp = dp->v_mount->mnt_vnodecovered; 609 vput(tdp); 610 VREF(dp); 611 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY); 612 } 613 } 614 615 /* 616 * We now have a segment name to search for, and a directory to search. 617 */ 618 unionlookup: 619 ndp->ni_dvp = dp; 620 ndp->ni_vp = NULL; 621 cnp->cn_flags &= ~PDIRUNLOCK; 622 if ((error = VOP_LOOKUP(dp, &ndp->ni_vp, cnp)) != 0) { 623 #ifdef DIAGNOSTIC 624 if (ndp->ni_vp != NULL) 625 panic("leaf `%s' should be empty", cnp->cn_nameptr); 626 #endif /* DIAGNOSTIC */ 627 #ifdef NAMEI_DIAGNOSTIC 628 printf("not found\n"); 629 #endif /* NAMEI_DIAGNOSTIC */ 630 if ((error == ENOENT) && 631 (dp->v_flag & VROOT) && 632 (dp->v_mount->mnt_flag & MNT_UNION)) { 633 tdp = dp; 634 dp = dp->v_mount->mnt_vnodecovered; 635 if (cnp->cn_flags & PDIRUNLOCK) 636 vrele(tdp); 637 else 638 vput(tdp); 639 VREF(dp); 640 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY); 641 goto unionlookup; 642 } 643 644 if (cnp->cn_flags & PDIRUNLOCK) 645 dpunlocked = 1; 646 647 if (error != EJUSTRETURN) 648 goto bad; 649 /* 650 * If this was not the last component, or there were trailing 651 * slashes, and we are not going to create a directory, 652 * then the name must exist. 653 */ 654 if ((cnp->cn_flags & (REQUIREDIR | CREATEDIR)) == REQUIREDIR) { 655 error = ENOENT; 656 goto bad; 657 } 658 /* 659 * If creating and at end of pathname, then can consider 660 * allowing file to be created. 661 */ 662 if (rdonly) { 663 error = EROFS; 664 goto bad; 665 } 666 /* 667 * We return with ni_vp NULL to indicate that the entry 668 * doesn't currently exist, leaving a pointer to the 669 * (possibly locked) directory inode in ndp->ni_dvp. 670 */ 671 if (cnp->cn_flags & SAVESTART) { 672 ndp->ni_startdir = ndp->ni_dvp; 673 VREF(ndp->ni_startdir); 674 } 675 return (0); 676 } 677 #ifdef NAMEI_DIAGNOSTIC 678 printf("found\n"); 679 #endif /* NAMEI_DIAGNOSTIC */ 680 681 /* 682 * Take into account any additional components consumed by the 683 * underlying filesystem. This will include any trailing slashes after 684 * the last component consumed. 685 */ 686 if (cnp->cn_consume > 0) { 687 ndp->ni_pathlen -= cnp->cn_consume - slashes; 688 ndp->ni_next += cnp->cn_consume - slashes; 689 cnp->cn_consume = 0; 690 if (ndp->ni_next[0] == '\0') 691 cnp->cn_flags |= ISLASTCN; 692 } 693 694 dp = ndp->ni_vp; 695 /* 696 * Check to see if the vnode has been mounted on; 697 * if so find the root of the mounted file system. 698 */ 699 while (dp->v_type == VDIR && (mp = dp->v_mountedhere) && 700 (cnp->cn_flags & NOCROSSMOUNT) == 0) { 701 if (vfs_busy(mp, 0, 0)) 702 continue; 703 VOP_UNLOCK(dp, 0); 704 error = VFS_ROOT(mp, &tdp); 705 vfs_unbusy(mp); 706 if (error) { 707 dpunlocked = 1; 708 goto bad2; 709 } 710 vrele(dp); 711 ndp->ni_vp = dp = tdp; 712 } 713 714 /* 715 * Check for symbolic link. Back up over any slashes that we skipped, 716 * as we will need them again. 717 */ 718 if ((dp->v_type == VLNK) && (cnp->cn_flags & (FOLLOW|REQUIREDIR))) { 719 ndp->ni_pathlen += slashes; 720 ndp->ni_next -= slashes; 721 cnp->cn_flags |= ISSYMLINK; 722 return (0); 723 } 724 725 /* 726 * Check for directory, if the component was followed by a series of 727 * slashes. 728 */ 729 if ((dp->v_type != VDIR) && (cnp->cn_flags & REQUIREDIR)) { 730 error = ENOTDIR; 731 goto bad2; 732 } 733 734 nextname: 735 /* 736 * Not a symbolic link. If this was not the last component, then 737 * continue at the next component, else return. 738 */ 739 if (!(cnp->cn_flags & ISLASTCN)) { 740 cnp->cn_nameptr = ndp->ni_next; 741 vrele(ndp->ni_dvp); 742 goto dirloop; 743 } 744 745 terminal: 746 /* 747 * Disallow directory write attempts on read-only file systems. 748 */ 749 if (rdonly && 750 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) { 751 /* 752 * Disallow directory write attempts on read-only 753 * file systems. 754 */ 755 error = EROFS; 756 goto bad2; 757 } 758 if (ndp->ni_dvp != NULL) { 759 if (cnp->cn_flags & SAVESTART) { 760 ndp->ni_startdir = ndp->ni_dvp; 761 VREF(ndp->ni_startdir); 762 } 763 if (!wantparent) 764 vrele(ndp->ni_dvp); 765 } 766 if ((cnp->cn_flags & LOCKLEAF) == 0) 767 VOP_UNLOCK(dp, 0); 768 return (0); 769 770 bad2: 771 if ((cnp->cn_flags & LOCKPARENT) && (cnp->cn_flags & ISLASTCN) && 772 ((cnp->cn_flags & PDIRUNLOCK) == 0)) 773 VOP_UNLOCK(ndp->ni_dvp, 0); 774 vrele(ndp->ni_dvp); 775 bad: 776 if (dpunlocked) 777 vrele(dp); 778 else 779 vput(dp); 780 ndp->ni_vp = NULL; 781 return (error); 782 } 783 784 /* 785 * Reacquire a path name component. 786 */ 787 int 788 relookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp) 789 { 790 struct vnode *dp = 0; /* the directory we are searching */ 791 int wantparent; /* 1 => wantparent or lockparent flag */ 792 int rdonly; /* lookup read-only flag bit */ 793 int error = 0; 794 #ifdef DEBUG 795 u_long newhash; /* DEBUG: check name hash */ 796 const char *cp; /* DEBUG: check name ptr/len */ 797 #endif /* DEBUG */ 798 799 /* 800 * Setup: break out flag bits into variables. 801 */ 802 wantparent = cnp->cn_flags & (LOCKPARENT|WANTPARENT); 803 rdonly = cnp->cn_flags & RDONLY; 804 cnp->cn_flags &= ~ISSYMLINK; 805 dp = dvp; 806 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY); 807 808 /* dirloop: */ 809 /* 810 * Search a new directory. 811 * 812 * The cn_hash value is for use by vfs_cache. 813 * The last component of the filename is left accessible via 814 * cnp->cn_nameptr for callers that need the name. Callers needing 815 * the name set the SAVENAME flag. When done, they assume 816 * responsibility for freeing the pathname buffer. 817 */ 818 #ifdef DEBUG 819 cp = NULL; 820 newhash = namei_hash(cnp->cn_nameptr, &cp); 821 if (newhash != cnp->cn_hash) 822 panic("relookup: bad hash"); 823 if (cnp->cn_namelen != cp - cnp->cn_nameptr) 824 panic("relookup: bad len"); 825 while (*cp == '/') 826 cp++; 827 if (*cp != 0) 828 panic("relookup: not last component"); 829 #endif /* DEBUG */ 830 #ifdef NAMEI_DIAGNOSTIC 831 printf("{%s}: ", cnp->cn_nameptr); 832 #endif /* NAMEI_DIAGNOSTIC */ 833 834 /* 835 * Check for degenerate name (e.g. / or "") 836 * which is a way of talking about a directory, 837 * e.g. like "/." or ".". 838 */ 839 if (cnp->cn_nameptr[0] == '\0') 840 panic("relookup: null name"); 841 842 if (cnp->cn_flags & ISDOTDOT) 843 panic("relookup: lookup on dot-dot"); 844 845 /* 846 * We now have a segment name to search for, and a directory to search. 847 */ 848 if ((error = VOP_LOOKUP(dp, vpp, cnp)) != 0) { 849 #ifdef DIAGNOSTIC 850 if (*vpp != NULL) 851 panic("leaf `%s' should be empty", cnp->cn_nameptr); 852 #endif 853 if (error != EJUSTRETURN) 854 goto bad; 855 /* 856 * If creating and at end of pathname, then can consider 857 * allowing file to be created. 858 */ 859 if (rdonly) { 860 error = EROFS; 861 goto bad; 862 } 863 /* ASSERT(dvp == ndp->ni_startdir) */ 864 if (cnp->cn_flags & SAVESTART) 865 VREF(dvp); 866 /* 867 * We return with ni_vp NULL to indicate that the entry 868 * doesn't currently exist, leaving a pointer to the 869 * (possibly locked) directory inode in ndp->ni_dvp. 870 */ 871 return (0); 872 } 873 dp = *vpp; 874 875 #ifdef DIAGNOSTIC 876 /* 877 * Check for symbolic link 878 */ 879 if (dp->v_type == VLNK && (cnp->cn_flags & FOLLOW)) 880 panic("relookup: symlink found"); 881 #endif 882 883 /* 884 * Check for read-only file systems. 885 */ 886 if (rdonly && 887 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) { 888 error = EROFS; 889 goto bad2; 890 } 891 /* ASSERT(dvp == ndp->ni_startdir) */ 892 if (cnp->cn_flags & SAVESTART) 893 VREF(dvp); 894 if (!wantparent) 895 vrele(dvp); 896 if ((cnp->cn_flags & LOCKLEAF) == 0) 897 VOP_UNLOCK(dp, 0); 898 return (0); 899 900 bad2: 901 if ((cnp->cn_flags & LOCKPARENT) && (cnp->cn_flags & ISLASTCN)) 902 VOP_UNLOCK(dvp, 0); 903 vrele(dvp); 904 bad: 905 vput(dp); 906 *vpp = NULL; 907 return (error); 908 } 909