vfs_lookup.c revision 154649
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 * 4. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * @(#)vfs_lookup.c 8.4 (Berkeley) 2/16/94 35 */ 36 37#include <sys/cdefs.h> 38__FBSDID("$FreeBSD: head/sys/kern/vfs_lookup.c 154649 2006-01-21 19:57:56Z truckman $"); 39 40#include "opt_ktrace.h" 41#include "opt_mac.h" 42#include "opt_vfs.h" 43 44#include <sys/param.h> 45#include <sys/systm.h> 46#include <sys/kernel.h> 47#include <sys/lock.h> 48#include <sys/mac.h> 49#include <sys/mutex.h> 50#include <sys/namei.h> 51#include <sys/vnode.h> 52#include <sys/mount.h> 53#include <sys/filedesc.h> 54#include <sys/proc.h> 55#include <sys/syscallsubr.h> 56#include <sys/sysctl.h> 57#ifdef KTRACE 58#include <sys/ktrace.h> 59#endif 60 61#include <vm/uma.h> 62 63#define NAMEI_DIAGNOSTIC 1 64#undef NAMEI_DIAGNOSTIC 65 66/* 67 * Allocation zone for namei 68 */ 69uma_zone_t namei_zone; 70 71static void 72nameiinit(void *dummy __unused) 73{ 74 namei_zone = uma_zcreate("NAMEI", MAXPATHLEN, NULL, NULL, NULL, NULL, 75 UMA_ALIGN_PTR, 0); 76 77} 78SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_SECOND, nameiinit, NULL) 79 80#ifdef LOOKUP_SHARED 81static int lookup_shared = 1; 82#else 83static int lookup_shared = 0; 84#endif 85SYSCTL_INT(_vfs, OID_AUTO, lookup_shared, CTLFLAG_RW, &lookup_shared, 0, 86 "Enables/Disables shared locks for path name translation"); 87 88/* 89 * Convert a pathname into a pointer to a locked inode. 90 * 91 * The FOLLOW flag is set when symbolic links are to be followed 92 * when they occur at the end of the name translation process. 93 * Symbolic links are always followed for all other pathname 94 * components other than the last. 95 * 96 * The segflg defines whether the name is to be copied from user 97 * space or kernel space. 98 * 99 * Overall outline of namei: 100 * 101 * copy in name 102 * get starting directory 103 * while (!done && !error) { 104 * call lookup to search path. 105 * if symbolic link, massage name in buffer and continue 106 * } 107 */ 108int 109namei(ndp) 110 register struct nameidata *ndp; 111{ 112 register struct filedesc *fdp; /* pointer to file descriptor state */ 113 register char *cp; /* pointer into pathname argument */ 114 register struct vnode *dp; /* the directory we are searching */ 115 struct iovec aiov; /* uio for reading symbolic links */ 116 struct uio auio; 117 int error, linklen; 118 struct componentname *cnp = &ndp->ni_cnd; 119 struct thread *td = cnp->cn_thread; 120 struct proc *p = td->td_proc; 121 int vfslocked; 122 123 KASSERT((cnp->cn_flags & MPSAFE) != 0 || mtx_owned(&Giant) != 0, 124 ("NOT MPSAFE and Giant not held")); 125 ndp->ni_cnd.cn_cred = ndp->ni_cnd.cn_thread->td_ucred; 126 KASSERT(cnp->cn_cred && p, ("namei: bad cred/proc")); 127 KASSERT((cnp->cn_nameiop & (~OPMASK)) == 0, 128 ("namei: nameiop contaminated with flags")); 129 KASSERT((cnp->cn_flags & OPMASK) == 0, 130 ("namei: flags contaminated with nameiops")); 131 if (!lookup_shared) 132 cnp->cn_flags &= ~LOCKSHARED; 133 fdp = p->p_fd; 134 135 /* 136 * Get a buffer for the name to be translated, and copy the 137 * name into the buffer. 138 */ 139 if ((cnp->cn_flags & HASBUF) == 0) 140 cnp->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK); 141 if (ndp->ni_segflg == UIO_SYSSPACE) 142 error = copystr(ndp->ni_dirp, cnp->cn_pnbuf, 143 MAXPATHLEN, (size_t *)&ndp->ni_pathlen); 144 else 145 error = copyinstr(ndp->ni_dirp, cnp->cn_pnbuf, 146 MAXPATHLEN, (size_t *)&ndp->ni_pathlen); 147 148 /* 149 * Don't allow empty pathnames. 150 */ 151 if (!error && *cnp->cn_pnbuf == '\0') 152 error = ENOENT; 153 154 if (error) { 155 uma_zfree(namei_zone, cnp->cn_pnbuf); 156#ifdef DIAGNOSTIC 157 cnp->cn_pnbuf = NULL; 158 cnp->cn_nameptr = NULL; 159#endif 160 ndp->ni_vp = NULL; 161 return (error); 162 } 163 ndp->ni_loopcnt = 0; 164#ifdef KTRACE 165 if (KTRPOINT(td, KTR_NAMEI)) { 166 KASSERT(cnp->cn_thread == curthread, 167 ("namei not using curthread")); 168 ktrnamei(cnp->cn_pnbuf); 169 } 170#endif 171 172 /* 173 * Get starting point for the translation. 174 */ 175 FILEDESC_LOCK(fdp); 176 ndp->ni_rootdir = fdp->fd_rdir; 177 ndp->ni_topdir = fdp->fd_jdir; 178 179 dp = fdp->fd_cdir; 180 vfslocked = VFS_LOCK_GIANT(dp->v_mount); 181 VREF(dp); 182 FILEDESC_UNLOCK(fdp); 183 for (;;) { 184 /* 185 * Check if root directory should replace current directory. 186 * Done at start of translation and after symbolic link. 187 */ 188 cnp->cn_nameptr = cnp->cn_pnbuf; 189 if (*(cnp->cn_nameptr) == '/') { 190 vrele(dp); 191 VFS_UNLOCK_GIANT(vfslocked); 192 while (*(cnp->cn_nameptr) == '/') { 193 cnp->cn_nameptr++; 194 ndp->ni_pathlen--; 195 } 196 dp = ndp->ni_rootdir; 197 vfslocked = VFS_LOCK_GIANT(dp->v_mount); 198 VREF(dp); 199 } 200 if (vfslocked) 201 ndp->ni_cnd.cn_flags |= GIANTHELD; 202 ndp->ni_startdir = dp; 203 error = lookup(ndp); 204 if (error) { 205 uma_zfree(namei_zone, cnp->cn_pnbuf); 206#ifdef DIAGNOSTIC 207 cnp->cn_pnbuf = NULL; 208 cnp->cn_nameptr = NULL; 209#endif 210 return (error); 211 } 212 vfslocked = (ndp->ni_cnd.cn_flags & GIANTHELD) != 0; 213 ndp->ni_cnd.cn_flags &= ~GIANTHELD; 214 /* 215 * Check for symbolic link 216 */ 217 if ((cnp->cn_flags & ISSYMLINK) == 0) { 218 if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0) { 219 uma_zfree(namei_zone, cnp->cn_pnbuf); 220#ifdef DIAGNOSTIC 221 cnp->cn_pnbuf = NULL; 222 cnp->cn_nameptr = NULL; 223#endif 224 } else 225 cnp->cn_flags |= HASBUF; 226 227 if ((cnp->cn_flags & MPSAFE) == 0) { 228 VFS_UNLOCK_GIANT(vfslocked); 229 } else if (vfslocked) 230 ndp->ni_cnd.cn_flags |= GIANTHELD; 231 return (0); 232 } 233 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) { 234 error = ELOOP; 235 break; 236 } 237#ifdef MAC 238 if ((cnp->cn_flags & NOMACCHECK) == 0) { 239 error = mac_check_vnode_readlink(td->td_ucred, 240 ndp->ni_vp); 241 if (error) 242 break; 243 } 244#endif 245 if (ndp->ni_pathlen > 1) 246 cp = uma_zalloc(namei_zone, M_WAITOK); 247 else 248 cp = cnp->cn_pnbuf; 249 aiov.iov_base = cp; 250 aiov.iov_len = MAXPATHLEN; 251 auio.uio_iov = &aiov; 252 auio.uio_iovcnt = 1; 253 auio.uio_offset = 0; 254 auio.uio_rw = UIO_READ; 255 auio.uio_segflg = UIO_SYSSPACE; 256 auio.uio_td = (struct thread *)0; 257 auio.uio_resid = MAXPATHLEN; 258 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred); 259 if (error) { 260 if (ndp->ni_pathlen > 1) 261 uma_zfree(namei_zone, cp); 262 break; 263 } 264 linklen = MAXPATHLEN - auio.uio_resid; 265 if (linklen == 0) { 266 if (ndp->ni_pathlen > 1) 267 uma_zfree(namei_zone, cp); 268 error = ENOENT; 269 break; 270 } 271 if (linklen + ndp->ni_pathlen >= MAXPATHLEN) { 272 if (ndp->ni_pathlen > 1) 273 uma_zfree(namei_zone, cp); 274 error = ENAMETOOLONG; 275 break; 276 } 277 if (ndp->ni_pathlen > 1) { 278 bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen); 279 uma_zfree(namei_zone, cnp->cn_pnbuf); 280 cnp->cn_pnbuf = cp; 281 } else 282 cnp->cn_pnbuf[linklen] = '\0'; 283 ndp->ni_pathlen += linklen; 284 vput(ndp->ni_vp); 285 dp = ndp->ni_dvp; 286 } 287 uma_zfree(namei_zone, cnp->cn_pnbuf); 288#ifdef DIAGNOSTIC 289 cnp->cn_pnbuf = NULL; 290 cnp->cn_nameptr = NULL; 291#endif 292 vput(ndp->ni_vp); 293 ndp->ni_vp = NULL; 294 vrele(ndp->ni_dvp); 295 VFS_UNLOCK_GIANT(vfslocked); 296 return (error); 297} 298 299/* 300 * Search a pathname. 301 * This is a very central and rather complicated routine. 302 * 303 * The pathname is pointed to by ni_ptr and is of length ni_pathlen. 304 * The starting directory is taken from ni_startdir. The pathname is 305 * descended until done, or a symbolic link is encountered. The variable 306 * ni_more is clear if the path is completed; it is set to one if a 307 * symbolic link needing interpretation is encountered. 308 * 309 * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on 310 * whether the name is to be looked up, created, renamed, or deleted. 311 * When CREATE, RENAME, or DELETE is specified, information usable in 312 * creating, renaming, or deleting a directory entry may be calculated. 313 * If flag has LOCKPARENT or'ed into it, the parent directory is returned 314 * locked. If flag has WANTPARENT or'ed into it, the parent directory is 315 * returned unlocked. Otherwise the parent directory is not returned. If 316 * the target of the pathname exists and LOCKLEAF is or'ed into the flag 317 * the target is returned locked, otherwise it is returned unlocked. 318 * When creating or renaming and LOCKPARENT is specified, the target may not 319 * be ".". When deleting and LOCKPARENT is specified, the target may be ".". 320 * 321 * Overall outline of lookup: 322 * 323 * dirloop: 324 * identify next component of name at ndp->ni_ptr 325 * handle degenerate case where name is null string 326 * if .. and crossing mount points and on mounted filesys, find parent 327 * call VOP_LOOKUP routine for next component name 328 * directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set 329 * component vnode returned in ni_vp (if it exists), locked. 330 * if result vnode is mounted on and crossing mount points, 331 * find mounted on vnode 332 * if more components of name, do next level at dirloop 333 * return the answer in ni_vp, locked if LOCKLEAF set 334 * if LOCKPARENT set, return locked parent in ni_dvp 335 * if WANTPARENT set, return unlocked parent in ni_dvp 336 */ 337int 338lookup(ndp) 339 register struct nameidata *ndp; 340{ 341 register char *cp; /* pointer into pathname argument */ 342 register struct vnode *dp = 0; /* the directory we are searching */ 343 struct vnode *tdp; /* saved dp */ 344 struct mount *mp; /* mount table entry */ 345 int docache; /* == 0 do not cache last component */ 346 int wantparent; /* 1 => wantparent or lockparent flag */ 347 int rdonly; /* lookup read-only flag bit */ 348 int trailing_slash; 349 int error = 0; 350 int dpunlocked = 0; /* dp has already been unlocked */ 351 struct componentname *cnp = &ndp->ni_cnd; 352 struct thread *td = cnp->cn_thread; 353 int vfslocked; 354 int tvfslocked; 355 356 /* 357 * Setup: break out flag bits into variables. 358 */ 359 vfslocked = (ndp->ni_cnd.cn_flags & GIANTHELD) != 0; 360 ndp->ni_cnd.cn_flags &= ~GIANTHELD; 361 wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT); 362 KASSERT(cnp->cn_nameiop == LOOKUP || wantparent, 363 ("CREATE, DELETE, RENAME require LOCKPARENT or WANTPARENT.")); 364 docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE; 365 if (cnp->cn_nameiop == DELETE || 366 (wantparent && cnp->cn_nameiop != CREATE && 367 cnp->cn_nameiop != LOOKUP)) 368 docache = 0; 369 rdonly = cnp->cn_flags & RDONLY; 370 cnp->cn_flags &= ~ISSYMLINK; 371 ndp->ni_dvp = NULL; 372 /* 373 * We use shared locks until we hit the parent of the last cn then 374 * we adjust based on the requesting flags. 375 */ 376 if (lookup_shared) 377 cnp->cn_lkflags = LK_SHARED; 378 else 379 cnp->cn_lkflags = LK_EXCLUSIVE; 380 dp = ndp->ni_startdir; 381 ndp->ni_startdir = NULLVP; 382 vn_lock(dp, cnp->cn_lkflags | LK_RETRY, td); 383 384dirloop: 385 /* 386 * Search a new directory. 387 * 388 * The last component of the filename is left accessible via 389 * cnp->cn_nameptr for callers that need the name. Callers needing 390 * the name set the SAVENAME flag. When done, they assume 391 * responsibility for freeing the pathname buffer. 392 */ 393 cnp->cn_consume = 0; 394 for (cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++) 395 continue; 396 cnp->cn_namelen = cp - cnp->cn_nameptr; 397 if (cnp->cn_namelen > NAME_MAX) { 398 error = ENAMETOOLONG; 399 goto bad; 400 } 401#ifdef NAMEI_DIAGNOSTIC 402 { char c = *cp; 403 *cp = '\0'; 404 printf("{%s}: ", cnp->cn_nameptr); 405 *cp = c; } 406#endif 407 ndp->ni_pathlen -= cnp->cn_namelen; 408 ndp->ni_next = cp; 409 410 /* 411 * Replace multiple slashes by a single slash and trailing slashes 412 * by a null. This must be done before VOP_LOOKUP() because some 413 * fs's don't know about trailing slashes. Remember if there were 414 * trailing slashes to handle symlinks, existing non-directories 415 * and non-existing files that won't be directories specially later. 416 */ 417 trailing_slash = 0; 418 while (*cp == '/' && (cp[1] == '/' || cp[1] == '\0')) { 419 cp++; 420 ndp->ni_pathlen--; 421 if (*cp == '\0') { 422 trailing_slash = 1; 423 *ndp->ni_next = '\0'; /* XXX for direnter() ... */ 424 } 425 } 426 ndp->ni_next = cp; 427 428 cnp->cn_flags |= MAKEENTRY; 429 if (*cp == '\0' && docache == 0) 430 cnp->cn_flags &= ~MAKEENTRY; 431 if (cnp->cn_namelen == 2 && 432 cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.') 433 cnp->cn_flags |= ISDOTDOT; 434 else 435 cnp->cn_flags &= ~ISDOTDOT; 436 if (*ndp->ni_next == 0) 437 cnp->cn_flags |= ISLASTCN; 438 else 439 cnp->cn_flags &= ~ISLASTCN; 440 441 442 /* 443 * Check for degenerate name (e.g. / or "") 444 * which is a way of talking about a directory, 445 * e.g. like "/." or ".". 446 */ 447 if (cnp->cn_nameptr[0] == '\0') { 448 if (dp->v_type != VDIR) { 449 error = ENOTDIR; 450 goto bad; 451 } 452 if (cnp->cn_nameiop != LOOKUP) { 453 error = EISDIR; 454 goto bad; 455 } 456 if (wantparent) { 457 ndp->ni_dvp = dp; 458 VREF(dp); 459 } 460 ndp->ni_vp = dp; 461 if (!(cnp->cn_flags & (LOCKPARENT | LOCKLEAF))) 462 VOP_UNLOCK(dp, 0, td); 463 /* XXX This should probably move to the top of function. */ 464 if (cnp->cn_flags & SAVESTART) 465 panic("lookup: SAVESTART"); 466 goto success; 467 } 468 469 /* 470 * Handle "..": four special cases. 471 * 1. Return an error if this is the last component of 472 * the name and the operation is DELETE or RENAME. 473 * 2. If at root directory (e.g. after chroot) 474 * or at absolute root directory 475 * then ignore it so can't get out. 476 * 3. If this vnode is the root of a mounted 477 * filesystem, then replace it with the 478 * vnode which was mounted on so we take the 479 * .. in the other filesystem. 480 * 4. If the vnode is the top directory of 481 * the jail or chroot, don't let them out. 482 */ 483 if (cnp->cn_flags & ISDOTDOT) { 484 if ((cnp->cn_flags & ISLASTCN) != 0 && 485 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) { 486 error = EPERM; 487 goto bad; 488 } 489 for (;;) { 490 if (dp == ndp->ni_rootdir || 491 dp == ndp->ni_topdir || 492 dp == rootvnode) { 493 ndp->ni_dvp = dp; 494 ndp->ni_vp = dp; 495 VREF(dp); 496 goto nextname; 497 } 498 if ((dp->v_vflag & VV_ROOT) == 0 || 499 (cnp->cn_flags & NOCROSSMOUNT)) 500 break; 501 if (dp->v_mount == NULL) { /* forced unmount */ 502 error = EBADF; 503 goto bad; 504 } 505 tdp = dp; 506 dp = dp->v_mount->mnt_vnodecovered; 507 tvfslocked = vfslocked; 508 vfslocked = VFS_LOCK_GIANT(dp->v_mount); 509 VREF(dp); 510 vput(tdp); 511 VFS_UNLOCK_GIANT(tvfslocked); 512 vn_lock(dp, cnp->cn_lkflags | LK_RETRY, td); 513 } 514 } 515 516 /* 517 * We now have a segment name to search for, and a directory to search. 518 */ 519unionlookup: 520#ifdef MAC 521 if ((cnp->cn_flags & NOMACCHECK) == 0) { 522 error = mac_check_vnode_lookup(td->td_ucred, dp, cnp); 523 if (error) 524 goto bad; 525 } 526#endif 527 ndp->ni_dvp = dp; 528 ndp->ni_vp = NULL; 529 ASSERT_VOP_LOCKED(dp, "lookup"); 530 /* 531 * If we have a shared lock we may need to upgrade the lock for the 532 * last operation. 533 */ 534 if (VOP_ISLOCKED(dp, td) == LK_SHARED && 535 (cnp->cn_flags & ISLASTCN) && (cnp->cn_flags & LOCKPARENT)) 536 vn_lock(dp, LK_UPGRADE|LK_RETRY, td); 537 /* 538 * If we're looking up the last component and we need an exclusive 539 * lock, adjust our lkflags. 540 */ 541 if ((cnp->cn_flags & (ISLASTCN|LOCKSHARED|LOCKLEAF)) == 542 (ISLASTCN|LOCKLEAF)) 543 cnp->cn_lkflags = LK_EXCLUSIVE; 544#ifdef NAMEI_DIAGNOSTIC 545 vprint("lookup in", dp); 546#endif 547 if ((error = VOP_LOOKUP(dp, &ndp->ni_vp, cnp)) != 0) { 548 KASSERT(ndp->ni_vp == NULL, ("leaf should be empty")); 549#ifdef NAMEI_DIAGNOSTIC 550 printf("not found\n"); 551#endif 552 if ((error == ENOENT) && 553 (dp->v_vflag & VV_ROOT) && (dp->v_mount != NULL) && 554 (dp->v_mount->mnt_flag & MNT_UNION)) { 555 tdp = dp; 556 dp = dp->v_mount->mnt_vnodecovered; 557 tvfslocked = vfslocked; 558 vfslocked = VFS_LOCK_GIANT(dp->v_mount); 559 VREF(dp); 560 vput(tdp); 561 VFS_UNLOCK_GIANT(tvfslocked); 562 vn_lock(dp, cnp->cn_lkflags | LK_RETRY, td); 563 goto unionlookup; 564 } 565 566 if (error != EJUSTRETURN) 567 goto bad; 568 /* 569 * If creating and at end of pathname, then can consider 570 * allowing file to be created. 571 */ 572 if (rdonly) { 573 error = EROFS; 574 goto bad; 575 } 576 if (*cp == '\0' && trailing_slash && 577 !(cnp->cn_flags & WILLBEDIR)) { 578 error = ENOENT; 579 goto bad; 580 } 581 if ((cnp->cn_flags & LOCKPARENT) == 0) 582 VOP_UNLOCK(dp, 0, td); 583 /* 584 * This is a temporary assert to make sure I know what the 585 * behavior here was. 586 */ 587 KASSERT((cnp->cn_flags & (WANTPARENT|LOCKPARENT)) != 0, 588 ("lookup: Unhandled case.")); 589 /* 590 * We return with ni_vp NULL to indicate that the entry 591 * doesn't currently exist, leaving a pointer to the 592 * (possibly locked) directory inode in ndp->ni_dvp. 593 */ 594 if (cnp->cn_flags & SAVESTART) { 595 ndp->ni_startdir = ndp->ni_dvp; 596 VREF(ndp->ni_startdir); 597 } 598 goto success; 599 } 600#ifdef NAMEI_DIAGNOSTIC 601 printf("found\n"); 602#endif 603 /* 604 * Take into account any additional components consumed by 605 * the underlying filesystem. 606 */ 607 if (cnp->cn_consume > 0) { 608 cnp->cn_nameptr += cnp->cn_consume; 609 ndp->ni_next += cnp->cn_consume; 610 ndp->ni_pathlen -= cnp->cn_consume; 611 cnp->cn_consume = 0; 612 } 613 614 dp = ndp->ni_vp; 615 616 /* 617 * Check to see if the vnode has been mounted on; 618 * if so find the root of the mounted filesystem. 619 */ 620 while (dp->v_type == VDIR && (mp = dp->v_mountedhere) && 621 (cnp->cn_flags & NOCROSSMOUNT) == 0) { 622 KASSERT(dp != ndp->ni_dvp, ("XXX")); 623 if (vfs_busy(mp, 0, 0, td)) 624 continue; 625 vput(dp); 626 tvfslocked = VFS_LOCK_GIANT(mp); 627 VFS_UNLOCK_GIANT(vfslocked); 628 vfslocked = tvfslocked; 629 VOP_UNLOCK(ndp->ni_dvp, 0, td); 630 error = VFS_ROOT(mp, cnp->cn_lkflags, &tdp, td); 631 VOP_LOCK(ndp->ni_dvp, cnp->cn_lkflags | LK_RETRY, td); 632 vfs_unbusy(mp, td); 633 if (error) { 634 dpunlocked = 1; 635 goto bad2; 636 } 637 ndp->ni_vp = dp = tdp; 638 } 639 640 /* 641 * Check for symbolic link 642 */ 643 if ((dp->v_type == VLNK) && 644 ((cnp->cn_flags & FOLLOW) || trailing_slash || 645 *ndp->ni_next == '/')) { 646 cnp->cn_flags |= ISSYMLINK; 647 if (dp->v_mount == NULL) { 648 /* We can't know whether the directory was mounted with 649 * NOSYMFOLLOW, so we can't follow safely. */ 650 error = EBADF; 651 goto bad2; 652 } 653 if (dp->v_mount->mnt_flag & MNT_NOSYMFOLLOW) { 654 error = EACCES; 655 goto bad2; 656 } 657 /* 658 * Symlink code always expects an unlocked dvp. 659 */ 660 if (ndp->ni_dvp != ndp->ni_vp) 661 VOP_UNLOCK(ndp->ni_dvp, 0, td); 662 goto success; 663 } 664 665 /* 666 * Check for bogus trailing slashes. 667 */ 668 if (trailing_slash && dp->v_type != VDIR) { 669 error = ENOTDIR; 670 goto bad2; 671 } 672 673nextname: 674 /* 675 * Not a symbolic link. If more pathname, 676 * continue at next component, else return. 677 */ 678 KASSERT((cnp->cn_flags & ISLASTCN) || *ndp->ni_next == '/', 679 ("lookup: invalid path state.")); 680 if (*ndp->ni_next == '/') { 681 cnp->cn_nameptr = ndp->ni_next; 682 while (*cnp->cn_nameptr == '/') { 683 cnp->cn_nameptr++; 684 ndp->ni_pathlen--; 685 } 686 if (ndp->ni_dvp != dp) 687 vput(ndp->ni_dvp); 688 else 689 vrele(ndp->ni_dvp); 690 goto dirloop; 691 } 692 /* 693 * Disallow directory write attempts on read-only filesystems. 694 */ 695 if (rdonly && 696 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) { 697 error = EROFS; 698 goto bad2; 699 } 700 if (cnp->cn_flags & SAVESTART) { 701 ndp->ni_startdir = ndp->ni_dvp; 702 VREF(ndp->ni_startdir); 703 } 704 if (!wantparent) { 705 if (ndp->ni_dvp != dp) 706 vput(ndp->ni_dvp); 707 else 708 vrele(ndp->ni_dvp); 709 } else if ((cnp->cn_flags & LOCKPARENT) == 0 && ndp->ni_dvp != dp) 710 VOP_UNLOCK(ndp->ni_dvp, 0, td); 711 712 if ((cnp->cn_flags & LOCKLEAF) == 0) 713 VOP_UNLOCK(dp, 0, td); 714success: 715 if (vfslocked) 716 ndp->ni_cnd.cn_flags |= GIANTHELD; 717 return (0); 718 719bad2: 720 if (dp != ndp->ni_dvp) 721 vput(ndp->ni_dvp); 722 else 723 vrele(ndp->ni_dvp); 724bad: 725 if (!dpunlocked) 726 vput(dp); 727 VFS_UNLOCK_GIANT(vfslocked); 728 ndp->ni_cnd.cn_flags &= ~GIANTHELD; 729 ndp->ni_vp = NULL; 730 return (error); 731} 732 733/* 734 * relookup - lookup a path name component 735 * Used by lookup to re-aquire things. 736 */ 737int 738relookup(dvp, vpp, cnp) 739 struct vnode *dvp, **vpp; 740 struct componentname *cnp; 741{ 742 struct thread *td = cnp->cn_thread; 743 struct vnode *dp = 0; /* the directory we are searching */ 744 int wantparent; /* 1 => wantparent or lockparent flag */ 745 int rdonly; /* lookup read-only flag bit */ 746 int error = 0; 747 748 KASSERT(cnp->cn_flags & ISLASTCN, 749 ("relookup: Not given last component.")); 750 /* 751 * Setup: break out flag bits into variables. 752 */ 753 wantparent = cnp->cn_flags & (LOCKPARENT|WANTPARENT); 754 KASSERT(wantparent, ("relookup: parent not wanted.")); 755 rdonly = cnp->cn_flags & RDONLY; 756 cnp->cn_flags &= ~ISSYMLINK; 757 dp = dvp; 758 cnp->cn_lkflags = LK_EXCLUSIVE; 759 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY, td); 760 761 /* 762 * Search a new directory. 763 * 764 * The last component of the filename is left accessible via 765 * cnp->cn_nameptr for callers that need the name. Callers needing 766 * the name set the SAVENAME flag. When done, they assume 767 * responsibility for freeing the pathname buffer. 768 */ 769#ifdef NAMEI_DIAGNOSTIC 770 printf("{%s}: ", cnp->cn_nameptr); 771#endif 772 773 /* 774 * Check for degenerate name (e.g. / or "") 775 * which is a way of talking about a directory, 776 * e.g. like "/." or ".". 777 */ 778 if (cnp->cn_nameptr[0] == '\0') { 779 if (cnp->cn_nameiop != LOOKUP || wantparent) { 780 error = EISDIR; 781 goto bad; 782 } 783 if (dp->v_type != VDIR) { 784 error = ENOTDIR; 785 goto bad; 786 } 787 if (!(cnp->cn_flags & LOCKLEAF)) 788 VOP_UNLOCK(dp, 0, td); 789 *vpp = dp; 790 /* XXX This should probably move to the top of function. */ 791 if (cnp->cn_flags & SAVESTART) 792 panic("lookup: SAVESTART"); 793 return (0); 794 } 795 796 if (cnp->cn_flags & ISDOTDOT) 797 panic ("relookup: lookup on dot-dot"); 798 799 /* 800 * We now have a segment name to search for, and a directory to search. 801 */ 802#ifdef NAMEI_DIAGNOSTIC 803 vprint("search in:", dp); 804#endif 805 if ((error = VOP_LOOKUP(dp, vpp, cnp)) != 0) { 806 KASSERT(*vpp == NULL, ("leaf should be empty")); 807 if (error != EJUSTRETURN) 808 goto bad; 809 /* 810 * If creating and at end of pathname, then can consider 811 * allowing file to be created. 812 */ 813 if (rdonly) { 814 error = EROFS; 815 goto bad; 816 } 817 /* ASSERT(dvp == ndp->ni_startdir) */ 818 if (cnp->cn_flags & SAVESTART) 819 VREF(dvp); 820 if ((cnp->cn_flags & LOCKPARENT) == 0) 821 VOP_UNLOCK(dp, 0, td); 822 /* 823 * This is a temporary assert to make sure I know what the 824 * behavior here was. 825 */ 826 KASSERT((cnp->cn_flags & (WANTPARENT|LOCKPARENT)) != 0, 827 ("relookup: Unhandled case.")); 828 /* 829 * We return with ni_vp NULL to indicate that the entry 830 * doesn't currently exist, leaving a pointer to the 831 * (possibly locked) directory inode in ndp->ni_dvp. 832 */ 833 return (0); 834 } 835 dp = *vpp; 836 837 /* 838 * Disallow directory write attempts on read-only filesystems. 839 */ 840 if (rdonly && 841 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) { 842 if (dvp == dp) 843 vrele(dvp); 844 else 845 vput(dvp); 846 error = EROFS; 847 goto bad; 848 } 849 /* 850 * Set the parent lock/ref state to the requested state. 851 */ 852 if ((cnp->cn_flags & LOCKPARENT) == 0 && dvp != dp) { 853 if (wantparent) 854 VOP_UNLOCK(dvp, 0, td); 855 else 856 vput(dvp); 857 } else if (!wantparent) 858 vrele(dvp); 859 /* 860 * Check for symbolic link 861 */ 862 KASSERT(dp->v_type != VLNK || !(cnp->cn_flags & FOLLOW), 863 ("relookup: symlink found.\n")); 864 865 /* ASSERT(dvp == ndp->ni_startdir) */ 866 if (cnp->cn_flags & SAVESTART) 867 VREF(dvp); 868 869 if ((cnp->cn_flags & LOCKLEAF) == 0) 870 VOP_UNLOCK(dp, 0, td); 871 return (0); 872bad: 873 vput(dp); 874 *vpp = NULL; 875 return (error); 876} 877 878/* 879 * Free data allocated by namei(); see namei(9) for details. 880 */ 881void 882NDFREE(ndp, flags) 883 struct nameidata *ndp; 884 const u_int flags; 885{ 886 int unlock_dvp; 887 int unlock_vp; 888 889 unlock_dvp = 0; 890 unlock_vp = 0; 891 892 if (!(flags & NDF_NO_FREE_PNBUF) && 893 (ndp->ni_cnd.cn_flags & HASBUF)) { 894 uma_zfree(namei_zone, ndp->ni_cnd.cn_pnbuf); 895 ndp->ni_cnd.cn_flags &= ~HASBUF; 896 } 897 if (!(flags & NDF_NO_VP_UNLOCK) && 898 (ndp->ni_cnd.cn_flags & LOCKLEAF) && ndp->ni_vp) 899 unlock_vp = 1; 900 if (!(flags & NDF_NO_VP_RELE) && ndp->ni_vp) { 901 if (unlock_vp) { 902 vput(ndp->ni_vp); 903 unlock_vp = 0; 904 } else 905 vrele(ndp->ni_vp); 906 ndp->ni_vp = NULL; 907 } 908 if (unlock_vp) 909 VOP_UNLOCK(ndp->ni_vp, 0, ndp->ni_cnd.cn_thread); 910 if (!(flags & NDF_NO_DVP_UNLOCK) && 911 (ndp->ni_cnd.cn_flags & LOCKPARENT) && 912 ndp->ni_dvp != ndp->ni_vp) 913 unlock_dvp = 1; 914 if (!(flags & NDF_NO_DVP_RELE) && 915 (ndp->ni_cnd.cn_flags & (LOCKPARENT|WANTPARENT))) { 916 if (unlock_dvp) { 917 vput(ndp->ni_dvp); 918 unlock_dvp = 0; 919 } else 920 vrele(ndp->ni_dvp); 921 ndp->ni_dvp = NULL; 922 } 923 if (unlock_dvp) 924 VOP_UNLOCK(ndp->ni_dvp, 0, ndp->ni_cnd.cn_thread); 925 if (!(flags & NDF_NO_STARTDIR_RELE) && 926 (ndp->ni_cnd.cn_flags & SAVESTART)) { 927 vrele(ndp->ni_startdir); 928 ndp->ni_startdir = NULL; 929 } 930} 931 932/* 933 * Determine if there is a suitable alternate filename under the specified 934 * prefix for the specified path. If the create flag is set, then the 935 * alternate prefix will be used so long as the parent directory exists. 936 * This is used by the various compatiblity ABIs so that Linux binaries prefer 937 * files under /compat/linux for example. The chosen path (whether under 938 * the prefix or under /) is returned in a kernel malloc'd buffer pointed 939 * to by pathbuf. The caller is responsible for free'ing the buffer from 940 * the M_TEMP bucket if one is returned. 941 */ 942int 943kern_alternate_path(struct thread *td, const char *prefix, char *path, 944 enum uio_seg pathseg, char **pathbuf, int create) 945{ 946 struct nameidata nd, ndroot; 947 char *ptr, *buf, *cp; 948 size_t len, sz; 949 int error; 950 951 buf = (char *) malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 952 *pathbuf = buf; 953 954 /* Copy the prefix into the new pathname as a starting point. */ 955 len = strlcpy(buf, prefix, MAXPATHLEN); 956 if (len >= MAXPATHLEN) { 957 *pathbuf = NULL; 958 free(buf, M_TEMP); 959 return (EINVAL); 960 } 961 sz = MAXPATHLEN - len; 962 ptr = buf + len; 963 964 /* Append the filename to the prefix. */ 965 if (pathseg == UIO_SYSSPACE) 966 error = copystr(path, ptr, sz, &len); 967 else 968 error = copyinstr(path, ptr, sz, &len); 969 970 if (error) { 971 *pathbuf = NULL; 972 free(buf, M_TEMP); 973 return (error); 974 } 975 976 /* Only use a prefix with absolute pathnames. */ 977 if (*ptr != '/') { 978 error = EINVAL; 979 goto keeporig; 980 } 981 982 /* 983 * We know that there is a / somewhere in this pathname. 984 * Search backwards for it, to find the file's parent dir 985 * to see if it exists in the alternate tree. If it does, 986 * and we want to create a file (cflag is set). We don't 987 * need to worry about the root comparison in this case. 988 */ 989 990 if (create) { 991 for (cp = &ptr[len] - 1; *cp != '/'; cp--); 992 *cp = '\0'; 993 994 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, UIO_SYSSPACE, buf, td); 995 error = namei(&nd); 996 *cp = '/'; 997 if (error != 0) 998 goto keeporig; 999 } else { 1000 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, UIO_SYSSPACE, buf, td); 1001 1002 error = namei(&nd); 1003 if (error != 0) 1004 goto keeporig; 1005 1006 /* 1007 * We now compare the vnode of the prefix to the one 1008 * vnode asked. If they resolve to be the same, then we 1009 * ignore the match so that the real root gets used. 1010 * This avoids the problem of traversing "../.." to find the 1011 * root directory and never finding it, because "/" resolves 1012 * to the emulation root directory. This is expensive :-( 1013 */ 1014 NDINIT(&ndroot, LOOKUP, FOLLOW | MPSAFE, UIO_SYSSPACE, prefix, 1015 td); 1016 1017 /* We shouldn't ever get an error from this namei(). */ 1018 error = namei(&ndroot); 1019 if (error == 0) { 1020 if (nd.ni_vp == ndroot.ni_vp) 1021 error = ENOENT; 1022 1023 NDFREE(&ndroot, NDF_ONLY_PNBUF); 1024 vrele(ndroot.ni_vp); 1025 VFS_UNLOCK_GIANT(NDHASGIANT(&ndroot)); 1026 } 1027 } 1028 1029 NDFREE(&nd, NDF_ONLY_PNBUF); 1030 vrele(nd.ni_vp); 1031 VFS_UNLOCK_GIANT(NDHASGIANT(&nd)); 1032 1033keeporig: 1034 /* If there was an error, use the original path name. */ 1035 if (error) 1036 bcopy(ptr, buf, len); 1037 return (error); 1038} 1039