vfs_lookup.c revision 155334
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 155334 2006-02-05 15:42:01Z rwatson $"); 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 */ 71uma_zone_t namei_zone; 72 73static void 74nameiinit(void *dummy __unused) 75{ 76 namei_zone = uma_zcreate("NAMEI", MAXPATHLEN, NULL, NULL, NULL, NULL, 77 UMA_ALIGN_PTR, 0); 78 79} 80SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_SECOND, nameiinit, NULL) 81 82#ifdef LOOKUP_SHARED 83static int lookup_shared = 1; 84#else 85static int lookup_shared = 0; 86#endif 87SYSCTL_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 */ 110int 111namei(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 */ 345int 346lookup(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 394dirloop: 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_mount == NULL) { /* 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 */ 535unionlookup: 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 dvfslocked = vfslocked; 643 vfslocked = VFS_LOCK_GIANT(mp); 644 VOP_UNLOCK(ndp->ni_dvp, 0, td); 645 error = VFS_ROOT(mp, cnp->cn_lkflags, &tdp, td); 646 VOP_LOCK(ndp->ni_dvp, cnp->cn_lkflags | LK_RETRY, td); 647 vfs_unbusy(mp, td); 648 if (error) { 649 dpunlocked = 1; 650 goto bad2; 651 } 652 ndp->ni_vp = dp = tdp; 653 } 654 655 /* 656 * Check for symbolic link 657 */ 658 if ((dp->v_type == VLNK) && 659 ((cnp->cn_flags & FOLLOW) || trailing_slash || 660 *ndp->ni_next == '/')) { 661 cnp->cn_flags |= ISSYMLINK; 662 if (dp->v_mount == NULL) { 663 /* We can't know whether the directory was mounted with 664 * NOSYMFOLLOW, so we can't follow safely. */ 665 error = EBADF; 666 goto bad2; 667 } 668 if (dp->v_mount->mnt_flag & MNT_NOSYMFOLLOW) { 669 error = EACCES; 670 goto bad2; 671 } 672 /* 673 * Symlink code always expects an unlocked dvp. 674 */ 675 if (ndp->ni_dvp != ndp->ni_vp) 676 VOP_UNLOCK(ndp->ni_dvp, 0, td); 677 goto success; 678 } 679 680 /* 681 * Check for bogus trailing slashes. 682 */ 683 if (trailing_slash && dp->v_type != VDIR) { 684 error = ENOTDIR; 685 goto bad2; 686 } 687 688nextname: 689 /* 690 * Not a symbolic link. If more pathname, 691 * continue at next component, else return. 692 */ 693 KASSERT((cnp->cn_flags & ISLASTCN) || *ndp->ni_next == '/', 694 ("lookup: invalid path state.")); 695 if (*ndp->ni_next == '/') { 696 cnp->cn_nameptr = ndp->ni_next; 697 while (*cnp->cn_nameptr == '/') { 698 cnp->cn_nameptr++; 699 ndp->ni_pathlen--; 700 } 701 if (ndp->ni_dvp != dp) 702 vput(ndp->ni_dvp); 703 else 704 vrele(ndp->ni_dvp); 705 VFS_UNLOCK_GIANT(dvfslocked); 706 dvfslocked = 0; 707 goto dirloop; 708 } 709 /* 710 * Disallow directory write attempts on read-only filesystems. 711 */ 712 if (rdonly && 713 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) { 714 error = EROFS; 715 goto bad2; 716 } 717 if (cnp->cn_flags & SAVESTART) { 718 ndp->ni_startdir = ndp->ni_dvp; 719 VREF(ndp->ni_startdir); 720 } 721 if (!wantparent) { 722 if (ndp->ni_dvp != dp) 723 vput(ndp->ni_dvp); 724 else 725 vrele(ndp->ni_dvp); 726 VFS_UNLOCK_GIANT(dvfslocked); 727 dvfslocked = 0; 728 } else if ((cnp->cn_flags & LOCKPARENT) == 0 && ndp->ni_dvp != dp) 729 VOP_UNLOCK(ndp->ni_dvp, 0, td); 730 731 if (cnp->cn_flags & AUDITVNODE1) 732 AUDIT_ARG(vnode, dp, ARG_VNODE1); 733 else if (cnp->cn_flags & AUDITVNODE2) 734 AUDIT_ARG(vnode, dp, ARG_VNODE2); 735 736 if ((cnp->cn_flags & LOCKLEAF) == 0) 737 VOP_UNLOCK(dp, 0, td); 738success: 739 if (vfslocked && dvfslocked) 740 VFS_UNLOCK_GIANT(dvfslocked); /* Only need one */ 741 if (vfslocked || dvfslocked) 742 ndp->ni_cnd.cn_flags |= GIANTHELD; 743 return (0); 744 745bad2: 746 if (dp != ndp->ni_dvp) 747 vput(ndp->ni_dvp); 748 else 749 vrele(ndp->ni_dvp); 750bad: 751 if (!dpunlocked) 752 vput(dp); 753 VFS_UNLOCK_GIANT(vfslocked); 754 VFS_UNLOCK_GIANT(dvfslocked); 755 ndp->ni_cnd.cn_flags &= ~GIANTHELD; 756 ndp->ni_vp = NULL; 757 return (error); 758} 759 760/* 761 * relookup - lookup a path name component 762 * Used by lookup to re-aquire things. 763 */ 764int 765relookup(dvp, vpp, cnp) 766 struct vnode *dvp, **vpp; 767 struct componentname *cnp; 768{ 769 struct thread *td = cnp->cn_thread; 770 struct vnode *dp = 0; /* the directory we are searching */ 771 int wantparent; /* 1 => wantparent or lockparent flag */ 772 int rdonly; /* lookup read-only flag bit */ 773 int error = 0; 774 775 KASSERT(cnp->cn_flags & ISLASTCN, 776 ("relookup: Not given last component.")); 777 /* 778 * Setup: break out flag bits into variables. 779 */ 780 wantparent = cnp->cn_flags & (LOCKPARENT|WANTPARENT); 781 KASSERT(wantparent, ("relookup: parent not wanted.")); 782 rdonly = cnp->cn_flags & RDONLY; 783 cnp->cn_flags &= ~ISSYMLINK; 784 dp = dvp; 785 cnp->cn_lkflags = LK_EXCLUSIVE; 786 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY, td); 787 788 /* 789 * Search a new directory. 790 * 791 * The last component of the filename is left accessible via 792 * cnp->cn_nameptr for callers that need the name. Callers needing 793 * the name set the SAVENAME flag. When done, they assume 794 * responsibility for freeing the pathname buffer. 795 */ 796#ifdef NAMEI_DIAGNOSTIC 797 printf("{%s}: ", cnp->cn_nameptr); 798#endif 799 800 /* 801 * Check for degenerate name (e.g. / or "") 802 * which is a way of talking about a directory, 803 * e.g. like "/." or ".". 804 */ 805 if (cnp->cn_nameptr[0] == '\0') { 806 if (cnp->cn_nameiop != LOOKUP || wantparent) { 807 error = EISDIR; 808 goto bad; 809 } 810 if (dp->v_type != VDIR) { 811 error = ENOTDIR; 812 goto bad; 813 } 814 if (!(cnp->cn_flags & LOCKLEAF)) 815 VOP_UNLOCK(dp, 0, td); 816 *vpp = dp; 817 /* XXX This should probably move to the top of function. */ 818 if (cnp->cn_flags & SAVESTART) 819 panic("lookup: SAVESTART"); 820 return (0); 821 } 822 823 if (cnp->cn_flags & ISDOTDOT) 824 panic ("relookup: lookup on dot-dot"); 825 826 /* 827 * We now have a segment name to search for, and a directory to search. 828 */ 829#ifdef NAMEI_DIAGNOSTIC 830 vprint("search in:", dp); 831#endif 832 if ((error = VOP_LOOKUP(dp, vpp, cnp)) != 0) { 833 KASSERT(*vpp == NULL, ("leaf should be empty")); 834 if (error != EJUSTRETURN) 835 goto bad; 836 /* 837 * If creating and at end of pathname, then can consider 838 * allowing file to be created. 839 */ 840 if (rdonly) { 841 error = EROFS; 842 goto bad; 843 } 844 /* ASSERT(dvp == ndp->ni_startdir) */ 845 if (cnp->cn_flags & SAVESTART) 846 VREF(dvp); 847 if ((cnp->cn_flags & LOCKPARENT) == 0) 848 VOP_UNLOCK(dp, 0, td); 849 /* 850 * This is a temporary assert to make sure I know what the 851 * behavior here was. 852 */ 853 KASSERT((cnp->cn_flags & (WANTPARENT|LOCKPARENT)) != 0, 854 ("relookup: Unhandled case.")); 855 /* 856 * We return with ni_vp NULL to indicate that the entry 857 * doesn't currently exist, leaving a pointer to the 858 * (possibly locked) directory inode in ndp->ni_dvp. 859 */ 860 return (0); 861 } 862 dp = *vpp; 863 864 /* 865 * Disallow directory write attempts on read-only filesystems. 866 */ 867 if (rdonly && 868 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) { 869 if (dvp == dp) 870 vrele(dvp); 871 else 872 vput(dvp); 873 error = EROFS; 874 goto bad; 875 } 876 /* 877 * Set the parent lock/ref state to the requested state. 878 */ 879 if ((cnp->cn_flags & LOCKPARENT) == 0 && dvp != dp) { 880 if (wantparent) 881 VOP_UNLOCK(dvp, 0, td); 882 else 883 vput(dvp); 884 } else if (!wantparent) 885 vrele(dvp); 886 /* 887 * Check for symbolic link 888 */ 889 KASSERT(dp->v_type != VLNK || !(cnp->cn_flags & FOLLOW), 890 ("relookup: symlink found.\n")); 891 892 /* ASSERT(dvp == ndp->ni_startdir) */ 893 if (cnp->cn_flags & SAVESTART) 894 VREF(dvp); 895 896 if ((cnp->cn_flags & LOCKLEAF) == 0) 897 VOP_UNLOCK(dp, 0, td); 898 return (0); 899bad: 900 vput(dp); 901 *vpp = NULL; 902 return (error); 903} 904 905/* 906 * Free data allocated by namei(); see namei(9) for details. 907 */ 908void 909NDFREE(ndp, flags) 910 struct nameidata *ndp; 911 const u_int flags; 912{ 913 int unlock_dvp; 914 int unlock_vp; 915 916 unlock_dvp = 0; 917 unlock_vp = 0; 918 919 if (!(flags & NDF_NO_FREE_PNBUF) && 920 (ndp->ni_cnd.cn_flags & HASBUF)) { 921 uma_zfree(namei_zone, ndp->ni_cnd.cn_pnbuf); 922 ndp->ni_cnd.cn_flags &= ~HASBUF; 923 } 924 if (!(flags & NDF_NO_VP_UNLOCK) && 925 (ndp->ni_cnd.cn_flags & LOCKLEAF) && ndp->ni_vp) 926 unlock_vp = 1; 927 if (!(flags & NDF_NO_VP_RELE) && ndp->ni_vp) { 928 if (unlock_vp) { 929 vput(ndp->ni_vp); 930 unlock_vp = 0; 931 } else 932 vrele(ndp->ni_vp); 933 ndp->ni_vp = NULL; 934 } 935 if (unlock_vp) 936 VOP_UNLOCK(ndp->ni_vp, 0, ndp->ni_cnd.cn_thread); 937 if (!(flags & NDF_NO_DVP_UNLOCK) && 938 (ndp->ni_cnd.cn_flags & LOCKPARENT) && 939 ndp->ni_dvp != ndp->ni_vp) 940 unlock_dvp = 1; 941 if (!(flags & NDF_NO_DVP_RELE) && 942 (ndp->ni_cnd.cn_flags & (LOCKPARENT|WANTPARENT))) { 943 if (unlock_dvp) { 944 vput(ndp->ni_dvp); 945 unlock_dvp = 0; 946 } else 947 vrele(ndp->ni_dvp); 948 ndp->ni_dvp = NULL; 949 } 950 if (unlock_dvp) 951 VOP_UNLOCK(ndp->ni_dvp, 0, ndp->ni_cnd.cn_thread); 952 if (!(flags & NDF_NO_STARTDIR_RELE) && 953 (ndp->ni_cnd.cn_flags & SAVESTART)) { 954 vrele(ndp->ni_startdir); 955 ndp->ni_startdir = NULL; 956 } 957} 958 959/* 960 * Determine if there is a suitable alternate filename under the specified 961 * prefix for the specified path. If the create flag is set, then the 962 * alternate prefix will be used so long as the parent directory exists. 963 * This is used by the various compatiblity ABIs so that Linux binaries prefer 964 * files under /compat/linux for example. The chosen path (whether under 965 * the prefix or under /) is returned in a kernel malloc'd buffer pointed 966 * to by pathbuf. The caller is responsible for free'ing the buffer from 967 * the M_TEMP bucket if one is returned. 968 */ 969int 970kern_alternate_path(struct thread *td, const char *prefix, char *path, 971 enum uio_seg pathseg, char **pathbuf, int create) 972{ 973 struct nameidata nd, ndroot; 974 char *ptr, *buf, *cp; 975 size_t len, sz; 976 int error; 977 978 buf = (char *) malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 979 *pathbuf = buf; 980 981 /* Copy the prefix into the new pathname as a starting point. */ 982 len = strlcpy(buf, prefix, MAXPATHLEN); 983 if (len >= MAXPATHLEN) { 984 *pathbuf = NULL; 985 free(buf, M_TEMP); 986 return (EINVAL); 987 } 988 sz = MAXPATHLEN - len; 989 ptr = buf + len; 990 991 /* Append the filename to the prefix. */ 992 if (pathseg == UIO_SYSSPACE) 993 error = copystr(path, ptr, sz, &len); 994 else 995 error = copyinstr(path, ptr, sz, &len); 996 997 if (error) { 998 *pathbuf = NULL; 999 free(buf, M_TEMP); 1000 return (error); 1001 } 1002 1003 /* Only use a prefix with absolute pathnames. */ 1004 if (*ptr != '/') { 1005 error = EINVAL; 1006 goto keeporig; 1007 } 1008 1009 /* 1010 * We know that there is a / somewhere in this pathname. 1011 * Search backwards for it, to find the file's parent dir 1012 * to see if it exists in the alternate tree. If it does, 1013 * and we want to create a file (cflag is set). We don't 1014 * need to worry about the root comparison in this case. 1015 */ 1016 1017 if (create) { 1018 for (cp = &ptr[len] - 1; *cp != '/'; cp--); 1019 *cp = '\0'; 1020 1021 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, UIO_SYSSPACE, buf, td); 1022 error = namei(&nd); 1023 *cp = '/'; 1024 if (error != 0) 1025 goto keeporig; 1026 } else { 1027 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, UIO_SYSSPACE, buf, td); 1028 1029 error = namei(&nd); 1030 if (error != 0) 1031 goto keeporig; 1032 1033 /* 1034 * We now compare the vnode of the prefix to the one 1035 * vnode asked. If they resolve to be the same, then we 1036 * ignore the match so that the real root gets used. 1037 * This avoids the problem of traversing "../.." to find the 1038 * root directory and never finding it, because "/" resolves 1039 * to the emulation root directory. This is expensive :-( 1040 */ 1041 NDINIT(&ndroot, LOOKUP, FOLLOW | MPSAFE, UIO_SYSSPACE, prefix, 1042 td); 1043 1044 /* We shouldn't ever get an error from this namei(). */ 1045 error = namei(&ndroot); 1046 if (error == 0) { 1047 if (nd.ni_vp == ndroot.ni_vp) 1048 error = ENOENT; 1049 1050 NDFREE(&ndroot, NDF_ONLY_PNBUF); 1051 vrele(ndroot.ni_vp); 1052 VFS_UNLOCK_GIANT(NDHASGIANT(&ndroot)); 1053 } 1054 } 1055 1056 NDFREE(&nd, NDF_ONLY_PNBUF); 1057 vrele(nd.ni_vp); 1058 VFS_UNLOCK_GIANT(NDHASGIANT(&nd)); 1059 1060keeporig: 1061 /* If there was an error, use the original path name. */ 1062 if (error) 1063 bcopy(ptr, buf, len); 1064 return (error); 1065} 1066