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