1 /*- 2 * Copyright (c) 1992, 1993, 1994, 1995 Jan-Simon Pendry. 3 * Copyright (c) 1992, 1993, 1994, 1995 4 * The Regents of the University of California. All rights reserved. 5 * 6 * This code is derived from software contributed to Berkeley by 7 * Jan-Simon Pendry. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * @(#)union_vnops.c 8.32 (Berkeley) 6/23/95 34 * $FreeBSD$ 35 */ 36 37 #include <sys/param.h> 38 #include <sys/systm.h> 39 #include <sys/fcntl.h> 40 #include <sys/stat.h> 41 #include <sys/kernel.h> 42 #include <sys/vnode.h> 43 #include <sys/mount.h> 44 #include <sys/namei.h> 45 #include <sys/malloc.h> 46 #include <sys/bio.h> 47 #include <sys/buf.h> 48 #include <sys/lock.h> 49 #include <sys/sysctl.h> 50 #include <sys/unistd.h> 51 #include <sys/acl.h> 52 #include <sys/event.h> 53 #include <sys/extattr.h> 54 #include <sys/mac.h> 55 #include <fs/unionfs/union.h> 56 57 #include <vm/vm.h> 58 #include <vm/vnode_pager.h> 59 60 #include <vm/vm_page.h> 61 #include <vm/vm_object.h> 62 63 int uniondebug = 0; 64 65 #if UDEBUG_ENABLED 66 SYSCTL_INT(_vfs, OID_AUTO, uniondebug, CTLFLAG_RW, &uniondebug, 0, ""); 67 #else 68 SYSCTL_INT(_vfs, OID_AUTO, uniondebug, CTLFLAG_RD, &uniondebug, 0, ""); 69 #endif 70 71 static vop_access_t union_access; 72 static vop_aclcheck_t union_aclcheck; 73 static vop_advlock_t union_advlock; 74 static vop_close_t union_close; 75 static vop_closeextattr_t union_closeextattr; 76 static vop_create_t union_create; 77 static vop_deleteextattr_t union_deleteextattr; 78 static vop_fsync_t union_fsync; 79 static vop_getacl_t union_getacl; 80 static vop_getattr_t union_getattr; 81 static vop_getextattr_t union_getextattr; 82 static vop_inactive_t union_inactive; 83 static vop_ioctl_t union_ioctl; 84 static vop_lease_t union_lease; 85 static vop_link_t union_link; 86 static vop_listextattr_t union_listextattr; 87 static vop_lookup_t union_lookup; 88 static int union_lookup1(struct vnode *udvp, struct vnode **dvp, 89 struct vnode **vpp, 90 struct componentname *cnp); 91 static vop_mkdir_t union_mkdir; 92 static vop_mknod_t union_mknod; 93 static vop_open_t union_open; 94 static vop_openextattr_t union_openextattr; 95 static vop_pathconf_t union_pathconf; 96 static vop_print_t union_print; 97 static vop_read_t union_read; 98 static vop_readdir_t union_readdir; 99 static vop_readlink_t union_readlink; 100 static vop_getwritemount_t union_getwritemount; 101 static vop_reclaim_t union_reclaim; 102 static vop_remove_t union_remove; 103 static vop_rename_t union_rename; 104 static vop_rmdir_t union_rmdir; 105 static vop_poll_t union_poll; 106 static vop_setacl_t union_setacl; 107 static vop_setattr_t union_setattr; 108 static vop_setlabel_t union_setlabel; 109 static vop_setextattr_t union_setextattr; 110 static vop_strategy_t union_strategy; 111 static vop_symlink_t union_symlink; 112 static vop_whiteout_t union_whiteout; 113 static vop_write_t union_write; 114 115 static __inline 116 struct vnode * 117 union_lock_upper(struct union_node *un, struct thread *td) 118 { 119 struct vnode *uppervp; 120 121 if ((uppervp = un->un_uppervp) != NULL) { 122 VREF(uppervp); 123 vn_lock(uppervp, LK_EXCLUSIVE | LK_CANRECURSE | LK_RETRY, td); 124 } 125 KASSERT((uppervp == NULL || vrefcnt(uppervp) > 0), ("uppervp usecount is 0")); 126 return(uppervp); 127 } 128 129 static __inline 130 void 131 union_unlock_upper(struct vnode *uppervp, struct thread *td) 132 { 133 vput(uppervp); 134 } 135 136 static __inline 137 struct vnode * 138 union_lock_other(struct union_node *un, struct thread *td) 139 { 140 struct vnode *vp; 141 142 if (un->un_uppervp != NULL) { 143 vp = union_lock_upper(un, td); 144 } else if ((vp = un->un_lowervp) != NULL) { 145 VREF(vp); 146 vn_lock(vp, LK_EXCLUSIVE | LK_CANRECURSE | LK_RETRY, td); 147 } 148 return(vp); 149 } 150 151 static __inline 152 void 153 union_unlock_other(struct vnode *vp, struct thread *td) 154 { 155 vput(vp); 156 } 157 158 /* 159 * union_lookup: 160 * 161 * udvp must be exclusively locked on call and will remain 162 * exclusively locked on return. This is the mount point 163 * for our filesystem. 164 * 165 * dvp Our base directory, locked and referenced. 166 * The passed dvp will be dereferenced and unlocked on return 167 * and a new dvp will be returned which is locked and 168 * referenced in the same variable. 169 * 170 * vpp is filled in with the result if no error occured, 171 * locked and ref'd. 172 * 173 * If an error is returned, *vpp is set to NULLVP. If no 174 * error occurs, *vpp is returned with a reference and an 175 * exclusive lock. 176 */ 177 178 static int 179 union_lookup1(udvp, pdvp, vpp, cnp) 180 struct vnode *udvp; 181 struct vnode **pdvp; 182 struct vnode **vpp; 183 struct componentname *cnp; 184 { 185 int error; 186 struct thread *td = cnp->cn_thread; 187 struct vnode *dvp = *pdvp; 188 struct vnode *tdvp; 189 struct mount *mp; 190 191 /* 192 * If stepping up the directory tree, check for going 193 * back across the mount point, in which case do what 194 * lookup would do by stepping back down the mount 195 * hierarchy. 196 */ 197 if (cnp->cn_flags & ISDOTDOT) { 198 while ((dvp != udvp) && (dvp->v_vflag & VV_ROOT)) { 199 /* 200 * Don't do the NOCROSSMOUNT check 201 * at this level. By definition, 202 * union fs deals with namespaces, not 203 * filesystems. 204 */ 205 tdvp = dvp; 206 dvp = dvp->v_mount->mnt_vnodecovered; 207 VREF(dvp); 208 vput(tdvp); 209 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, td); 210 } 211 } 212 213 /* 214 * Set return dvp to be the upperdvp 'parent directory. 215 */ 216 *pdvp = dvp; 217 218 /* 219 * If the VOP_LOOKUP() call generates an error, tdvp is invalid and 220 * no changes will have been made to dvp, so we are set to return. 221 */ 222 223 error = VOP_LOOKUP(dvp, &tdvp, cnp); 224 if (error) { 225 UDEBUG(("dvp %p error %d flags %lx\n", dvp, error, cnp->cn_flags)); 226 *vpp = NULL; 227 return (error); 228 } 229 UDEBUG(("parentdir %p result %p flag %lx\n", dvp, tdvp, cnp->cn_flags)); 230 231 /* 232 * Lastly check if the current node is a mount point in 233 * which case walk up the mount hierarchy making sure not to 234 * bump into the root of the mount tree (ie. dvp != udvp). 235 * 236 * We use dvp as a temporary variable here, it is no longer related 237 * to the dvp above. However, we have to ensure that both *pdvp and 238 * tdvp are locked on return. 239 */ 240 241 dvp = tdvp; 242 while ( 243 dvp != udvp && 244 (dvp->v_type == VDIR) && 245 (mp = dvp->v_mountedhere) 246 ) { 247 int relock_pdvp = 0; 248 249 if (vfs_busy(mp, 0, 0, td)) 250 continue; 251 252 if (dvp == *pdvp) 253 relock_pdvp = 1; 254 vput(dvp); 255 dvp = NULL; 256 error = VFS_ROOT(mp, LK_EXCLUSIVE, &dvp, td); 257 258 vfs_unbusy(mp, td); 259 260 if (relock_pdvp) 261 vn_lock(*pdvp, LK_EXCLUSIVE | LK_RETRY, td); 262 263 if (error) { 264 *vpp = NULL; 265 return (error); 266 } 267 } 268 *vpp = dvp; 269 return (0); 270 } 271 272 static int 273 union_lookup(ap) 274 struct vop_lookup_args /* { 275 struct vnodeop_desc *a_desc; 276 struct vnode *a_dvp; 277 struct vnode **a_vpp; 278 struct componentname *a_cnp; 279 } */ *ap; 280 { 281 int error; 282 int uerror, lerror; 283 struct vnode *uppervp, *lowervp; 284 struct vnode *upperdvp, *lowerdvp; 285 struct vnode *dvp = ap->a_dvp; /* starting dir */ 286 struct union_node *dun = VTOUNION(dvp); /* associated union node */ 287 struct componentname *cnp = ap->a_cnp; 288 struct thread *td = cnp->cn_thread; 289 struct union_mount *um = MOUNTTOUNIONMOUNT(dvp->v_mount); 290 struct ucred *saved_cred = NULL; 291 int iswhiteout; 292 struct vattr va; 293 294 *ap->a_vpp = NULLVP; 295 296 /* 297 * Disallow write attempts to the filesystem mounted read-only. 298 */ 299 if ((cnp->cn_flags & ISLASTCN) && 300 (dvp->v_mount->mnt_flag & MNT_RDONLY) && 301 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) { 302 return (EROFS); 303 } 304 305 /* 306 * For any lookups we do, always return with the parent locked. 307 */ 308 cnp->cn_flags |= LOCKPARENT; 309 310 lowerdvp = dun->un_lowervp; 311 uppervp = NULLVP; 312 lowervp = NULLVP; 313 iswhiteout = 0; 314 315 uerror = ENOENT; 316 lerror = ENOENT; 317 318 /* 319 * Get a private lock on uppervp and a reference, effectively 320 * taking it out of the union_node's control. 321 * 322 * We must lock upperdvp while holding our lock on dvp 323 * to avoid a deadlock. 324 */ 325 upperdvp = union_lock_upper(dun, td); 326 327 /* 328 * Do the lookup in the upper level. 329 * If that level consumes additional pathnames, 330 * then assume that something special is going 331 * on and just return that vnode. 332 */ 333 if (upperdvp != NULLVP) { 334 /* 335 * We do not have to worry about the DOTDOT case, we've 336 * already unlocked dvp. 337 */ 338 UDEBUG(("A %p\n", upperdvp)); 339 340 /* 341 * Do the lookup. We must supply a locked and referenced 342 * upperdvp to the function and will get a new locked and 343 * referenced upperdvp back, with the old having been 344 * dereferenced. 345 * 346 * If an error is returned, uppervp will be NULLVP. If no 347 * error occurs, uppervp will be the locked and referenced. 348 * Return vnode, or possibly NULL, depending on what is being 349 * requested. It is possible that the returned uppervp 350 * will be the same as upperdvp. 351 */ 352 uerror = union_lookup1(um->um_uppervp, &upperdvp, &uppervp, cnp); 353 UDEBUG(( 354 "uerror %d upperdvp %p %d/%d, uppervp %p ref=%d/lck=%d\n", 355 uerror, 356 upperdvp, 357 vrefcnt(upperdvp), 358 VOP_ISLOCKED(upperdvp, NULL), 359 uppervp, 360 (uppervp ? vrefcnt(uppervp) : -99), 361 (uppervp ? VOP_ISLOCKED(uppervp, NULL) : -99) 362 )); 363 364 /* 365 * Disallow write attempts to the filesystem mounted read-only. 366 */ 367 if (uerror == EJUSTRETURN && (cnp->cn_flags & ISLASTCN) && 368 (dvp->v_mount->mnt_flag & MNT_RDONLY) && 369 (cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME)) { 370 error = EROFS; 371 goto out; 372 } 373 374 /* 375 * Special case: If cn_consume != 0 then skip out. The result 376 * of the lookup is transfered to our return variable. If 377 * an error occured we have to throw away the results. 378 */ 379 380 if (cnp->cn_consume != 0) { 381 if ((error = uerror) == 0) { 382 *ap->a_vpp = uppervp; 383 uppervp = NULL; 384 } 385 goto out; 386 } 387 388 /* 389 * Calculate whiteout, fall through. 390 */ 391 392 if (uerror == ENOENT || uerror == EJUSTRETURN) { 393 if (cnp->cn_flags & ISWHITEOUT) { 394 iswhiteout = 1; 395 } else if (lowerdvp != NULLVP) { 396 int terror; 397 398 terror = VOP_GETATTR(upperdvp, &va, 399 cnp->cn_cred, cnp->cn_thread); 400 if (terror == 0 && (va.va_flags & OPAQUE)) 401 iswhiteout = 1; 402 } 403 } 404 } 405 406 /* 407 * In a similar way to the upper layer, do the lookup 408 * in the lower layer. This time, if there is some 409 * component magic going on, then vput whatever we got 410 * back from the upper layer and return the lower vnode 411 * instead. 412 */ 413 414 if (lowerdvp != NULLVP && !iswhiteout) { 415 int nameiop; 416 417 UDEBUG(("B %p\n", lowerdvp)); 418 419 /* 420 * Force only LOOKUPs on the lower node, since 421 * we won't be making changes to it anyway. 422 */ 423 nameiop = cnp->cn_nameiop; 424 cnp->cn_nameiop = LOOKUP; 425 if (um->um_op == UNMNT_BELOW) { 426 saved_cred = cnp->cn_cred; 427 cnp->cn_cred = um->um_cred; 428 } 429 430 /* 431 * We shouldn't have to worry about locking interactions 432 * between the lower layer and our union layer (w.r.t. 433 * `..' processing) because we don't futz with lowervp 434 * locks in the union-node instantiation code path. 435 * 436 * union_lookup1() requires lowervp to be locked on entry, 437 * and it will be unlocked on return. The ref count will 438 * not change. On return lowervp doesn't represent anything 439 * to us so we NULL it out. 440 */ 441 VREF(lowerdvp); 442 vn_lock(lowerdvp, LK_EXCLUSIVE | LK_RETRY, td); 443 lerror = union_lookup1(um->um_lowervp, &lowerdvp, &lowervp, cnp); 444 if (lowerdvp == lowervp) 445 vrele(lowerdvp); 446 else 447 vput(lowerdvp); 448 lowerdvp = NULL; /* lowerdvp invalid after vput */ 449 450 if (um->um_op == UNMNT_BELOW) 451 cnp->cn_cred = saved_cred; 452 cnp->cn_nameiop = nameiop; 453 454 if (cnp->cn_consume != 0 || lerror == EACCES) { 455 if ((error = lerror) == 0) { 456 *ap->a_vpp = lowervp; 457 lowervp = NULL; 458 } 459 goto out; 460 } 461 } else { 462 UDEBUG(("C %p\n", lowerdvp)); 463 if ((cnp->cn_flags & ISDOTDOT) && dun->un_pvp != NULLVP) { 464 if ((lowervp = LOWERVP(dun->un_pvp)) != NULL) { 465 VREF(lowervp); 466 vn_lock(lowervp, LK_EXCLUSIVE | LK_RETRY, td); 467 lerror = 0; 468 } 469 } 470 } 471 472 /* 473 * Ok. Now we have uerror, uppervp, upperdvp, lerror, and lowervp. 474 * 475 * 1. If both layers returned an error, select the upper layer. 476 * 477 * 2. If the upper layer failed and the bottom layer succeeded, 478 * two subcases occur: 479 * 480 * a. The bottom vnode is not a directory, in which case 481 * just return a new union vnode referencing an 482 * empty top layer and the existing bottom layer. 483 * 484 * b. The bottom vnode is a directory, in which case 485 * create a new directory in the top layer and 486 * and fall through to case 3. 487 * 488 * 3. If the top layer succeeded, then return a new union 489 * vnode referencing whatever the new top layer and 490 * whatever the bottom layer returned. 491 */ 492 493 /* case 1. */ 494 if ((uerror != 0) && (lerror != 0)) { 495 error = uerror; 496 goto out; 497 } 498 499 /* case 2. */ 500 if (uerror != 0 /* && (lerror == 0) */ ) { 501 if (lowervp->v_type == VDIR) { /* case 2b. */ 502 KASSERT(uppervp == NULL, ("uppervp unexpectedly non-NULL")); 503 /* 504 * Oops, uppervp has a problem, we may have to shadow. 505 */ 506 uerror = union_mkshadow(um, upperdvp, cnp, &uppervp); 507 if (uerror) { 508 error = uerror; 509 goto out; 510 } 511 } 512 } 513 514 /* 515 * Must call union_allocvp() with both the upper and lower vnodes 516 * referenced and the upper vnode locked. ap->a_vpp is returned 517 * referenced and locked. lowervp, uppervp, and upperdvp are 518 * absorbed by union_allocvp() whether it succeeds or fails. 519 * 520 * upperdvp is the parent directory of uppervp which may be 521 * different, depending on the path, from dvp->un_uppervp. That's 522 * why it is a separate argument. Note that it must be unlocked. 523 * 524 * dvp must be locked on entry to the call and will be locked on 525 * return. 526 */ 527 528 if (uppervp && uppervp != upperdvp) 529 VOP_UNLOCK(uppervp, 0, td); 530 if (lowervp) 531 VOP_UNLOCK(lowervp, 0, td); 532 if (upperdvp) 533 VOP_UNLOCK(upperdvp, 0, td); 534 535 error = union_allocvp(ap->a_vpp, dvp->v_mount, dvp, upperdvp, cnp, 536 uppervp, lowervp, 1); 537 538 UDEBUG(("Create %p = %p %p refs=%d\n", *ap->a_vpp, uppervp, lowervp, (*ap->a_vpp) ? vrefcnt(*ap->a_vpp) : -99)); 539 540 uppervp = NULL; 541 upperdvp = NULL; 542 lowervp = NULL; 543 544 /* 545 * Termination Code 546 * 547 * - put away any extra junk laying around. Note that lowervp 548 * (if not NULL) will never be the same as *ap->a_vp and 549 * neither will uppervp, because when we set that state we 550 * NULL-out lowervp or uppervp. On the otherhand, upperdvp 551 * may match uppervp or *ap->a_vpp. 552 * 553 * - relock/unlock dvp if appropriate. 554 */ 555 556 out: 557 if (upperdvp) { 558 if (upperdvp == uppervp || upperdvp == *ap->a_vpp) 559 vrele(upperdvp); 560 else 561 vput(upperdvp); 562 } 563 564 if (uppervp) 565 vput(uppervp); 566 567 if (lowervp) 568 vput(lowervp); 569 570 UDEBUG(("Out %d vpp %p/%d lower %p upper %p\n", error, *ap->a_vpp, 571 ((*ap->a_vpp) ? vrefcnt(*ap->a_vpp) : -99), 572 lowervp, uppervp)); 573 574 if (error == 0 || error == EJUSTRETURN) { 575 if (cnp->cn_namelen == 1 && 576 cnp->cn_nameptr[0] == '.' && 577 *ap->a_vpp != dvp) { 578 #ifdef DIAGNOSTIC 579 vprint("union_lookup: vp", *ap->a_vpp); 580 vprint("union_lookup: dvp", dvp); 581 #endif 582 panic("union_lookup returning . (%p) != startdir (%p)", 583 *ap->a_vpp, dvp); 584 } 585 } 586 587 return (error); 588 } 589 590 /* 591 * union_create: 592 * 593 * a_dvp is locked on entry and remains locked on return. a_vpp is returned 594 * locked if no error occurs, otherwise it is garbage. 595 */ 596 597 static int 598 union_create(ap) 599 struct vop_create_args /* { 600 struct vnode *a_dvp; 601 struct vnode **a_vpp; 602 struct componentname *a_cnp; 603 struct vattr *a_vap; 604 } */ *ap; 605 { 606 struct union_node *dun = VTOUNION(ap->a_dvp); 607 struct componentname *cnp = ap->a_cnp; 608 struct thread *td = cnp->cn_thread; 609 struct vnode *dvp; 610 int error = EROFS; 611 612 if ((dvp = union_lock_upper(dun, td)) != NULL) { 613 struct vnode *vp; 614 struct mount *mp; 615 616 error = VOP_CREATE(dvp, &vp, cnp, ap->a_vap); 617 if (error == 0) { 618 mp = ap->a_dvp->v_mount; 619 VOP_UNLOCK(vp, 0, td); 620 UDEBUG(("ALLOCVP-1 FROM %p REFS %d\n", vp, vrefcnt(vp))); 621 error = union_allocvp(ap->a_vpp, mp, NULLVP, NULLVP, 622 cnp, vp, NULLVP, 1); 623 UDEBUG(("ALLOCVP-2B FROM %p REFS %d\n", *ap->a_vpp, vrefcnt(vp))); 624 } 625 union_unlock_upper(dvp, td); 626 } 627 return (error); 628 } 629 630 static int 631 union_whiteout(ap) 632 struct vop_whiteout_args /* { 633 struct vnode *a_dvp; 634 struct componentname *a_cnp; 635 int a_flags; 636 } */ *ap; 637 { 638 struct union_node *un = VTOUNION(ap->a_dvp); 639 struct componentname *cnp = ap->a_cnp; 640 struct vnode *uppervp; 641 int error; 642 643 switch (ap->a_flags) { 644 case CREATE: 645 case DELETE: 646 uppervp = union_lock_upper(un, cnp->cn_thread); 647 if (uppervp != NULLVP) { 648 error = VOP_WHITEOUT(un->un_uppervp, cnp, ap->a_flags); 649 union_unlock_upper(uppervp, cnp->cn_thread); 650 } else 651 error = EOPNOTSUPP; 652 break; 653 case LOOKUP: 654 error = EOPNOTSUPP; 655 break; 656 default: 657 panic("union_whiteout: unknown op"); 658 } 659 return (error); 660 } 661 662 /* 663 * union_mknod: 664 * 665 * a_dvp is locked on entry and should remain locked on return. 666 * a_vpp is garbage whether an error occurs or not. 667 */ 668 669 static int 670 union_mknod(ap) 671 struct vop_mknod_args /* { 672 struct vnode *a_dvp; 673 struct vnode **a_vpp; 674 struct componentname *a_cnp; 675 struct vattr *a_vap; 676 } */ *ap; 677 { 678 struct union_node *dun = VTOUNION(ap->a_dvp); 679 struct componentname *cnp = ap->a_cnp; 680 struct vnode *dvp; 681 int error = EROFS; 682 683 if ((dvp = union_lock_upper(dun, cnp->cn_thread)) != NULL) { 684 error = VOP_MKNOD(dvp, ap->a_vpp, cnp, ap->a_vap); 685 union_unlock_upper(dvp, cnp->cn_thread); 686 } 687 return (error); 688 } 689 690 /* 691 * union_open: 692 * 693 * run open VOP. When opening the underlying vnode we have to mimic 694 * vn_open(). What we *really* need to do to avoid screwups if the 695 * open semantics change is to call vn_open(). For example, ufs blows 696 * up if you open a file but do not vmio it prior to writing. 697 */ 698 699 static int 700 union_open(ap) 701 struct vop_open_args /* { 702 struct vnodeop_desc *a_desc; 703 struct vnode *a_vp; 704 int a_mode; 705 struct ucred *a_cred; 706 struct thread *a_td; 707 } */ *ap; 708 { 709 struct union_node *un = VTOUNION(ap->a_vp); 710 struct vnode *tvp; 711 int mode = ap->a_mode; 712 struct ucred *cred = ap->a_cred; 713 struct thread *td = ap->a_td; 714 int error = 0; 715 int tvpisupper = 1; 716 717 /* 718 * If there is an existing upper vp then simply open that. 719 * The upper vp takes precedence over the lower vp. When opening 720 * a lower vp for writing copy it to the uppervp and then open the 721 * uppervp. 722 * 723 * At the end of this section tvp will be left locked. 724 */ 725 if ((tvp = union_lock_upper(un, td)) == NULLVP) { 726 /* 727 * If the lower vnode is being opened for writing, then 728 * copy the file contents to the upper vnode and open that, 729 * otherwise can simply open the lower vnode. 730 */ 731 tvp = un->un_lowervp; 732 if ((ap->a_mode & FWRITE) && (tvp->v_type == VREG)) { 733 int docopy = !(mode & O_TRUNC); 734 error = union_copyup(un, docopy, cred, td); 735 tvp = union_lock_upper(un, td); 736 } else { 737 un->un_openl++; 738 VREF(tvp); 739 vn_lock(tvp, LK_EXCLUSIVE | LK_RETRY, td); 740 tvpisupper = 0; 741 } 742 } 743 744 /* 745 * We are holding the correct vnode, open it. 746 */ 747 748 if (error == 0) 749 error = VOP_OPEN(tvp, mode, cred, td, -1); 750 if (error == 0) 751 ap->a_vp->v_object = tvp->v_object; 752 /* 753 * Release any locks held. 754 */ 755 if (tvpisupper) { 756 if (tvp) 757 union_unlock_upper(tvp, td); 758 } else { 759 vput(tvp); 760 } 761 return (error); 762 } 763 764 /* 765 * union_close: 766 * 767 * It is unclear whether a_vp is passed locked or unlocked. Whatever 768 * the case we do not change it. 769 */ 770 771 static int 772 union_close(ap) 773 struct vop_close_args /* { 774 struct vnode *a_vp; 775 int a_fflag; 776 struct ucred *a_cred; 777 struct thread *a_td; 778 } */ *ap; 779 { 780 struct union_node *un = VTOUNION(ap->a_vp); 781 struct vnode *vp; 782 783 if ((vp = un->un_uppervp) == NULLVP) { 784 #ifdef UNION_DIAGNOSTIC 785 if (un->un_openl <= 0) 786 panic("union: un_openl cnt"); 787 #endif 788 --un->un_openl; 789 vp = un->un_lowervp; 790 } 791 ap->a_vp = vp; 792 return (VOP_CLOSE_AP(ap)); 793 } 794 795 /* 796 * Check access permission on the union vnode. 797 * The access check being enforced is to check 798 * against both the underlying vnode, and any 799 * copied vnode. This ensures that no additional 800 * file permissions are given away simply because 801 * the user caused an implicit file copy. 802 */ 803 static int 804 union_access(ap) 805 struct vop_access_args /* { 806 struct vnodeop_desc *a_desc; 807 struct vnode *a_vp; 808 int a_mode; 809 struct ucred *a_cred; 810 struct thread *a_td; 811 } */ *ap; 812 { 813 struct union_node *un = VTOUNION(ap->a_vp); 814 struct thread *td = ap->a_td; 815 int error = EACCES; 816 struct vnode *vp; 817 818 /* 819 * Disallow write attempts on filesystems mounted read-only. 820 */ 821 if ((ap->a_mode & VWRITE) && 822 (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)) { 823 switch (ap->a_vp->v_type) { 824 case VREG: 825 case VDIR: 826 case VLNK: 827 return (EROFS); 828 default: 829 break; 830 } 831 } 832 833 if ((vp = union_lock_upper(un, td)) != NULLVP) { 834 ap->a_vp = vp; 835 error = VOP_ACCESS_AP(ap); 836 union_unlock_upper(vp, td); 837 return(error); 838 } 839 840 if ((vp = un->un_lowervp) != NULLVP) { 841 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); 842 ap->a_vp = vp; 843 844 /* 845 * Remove VWRITE from a_mode if our mount point is RW, because 846 * we want to allow writes and lowervp may be read-only. 847 */ 848 if ((un->un_vnode->v_mount->mnt_flag & MNT_RDONLY) == 0) 849 ap->a_mode &= ~VWRITE; 850 851 error = VOP_ACCESS_AP(ap); 852 if (error == 0) { 853 struct union_mount *um; 854 855 um = MOUNTTOUNIONMOUNT(un->un_vnode->v_mount); 856 857 if (um->um_op == UNMNT_BELOW) { 858 ap->a_cred = um->um_cred; 859 error = VOP_ACCESS_AP(ap); 860 } 861 } 862 VOP_UNLOCK(vp, 0, td); 863 } 864 return(error); 865 } 866 867 /* 868 * We handle getattr only to change the fsid and 869 * track object sizes 870 * 871 * It's not clear whether VOP_GETATTR is to be 872 * called with the vnode locked or not. stat() calls 873 * it with (vp) locked, and fstat() calls it with 874 * (vp) unlocked. 875 * 876 * Because of this we cannot use our normal locking functions 877 * if we do not intend to lock the main a_vp node. At the moment 878 * we are running without any specific locking at all, but beware 879 * to any programmer that care must be taken if locking is added 880 * to this function. 881 */ 882 883 static int 884 union_getattr(ap) 885 struct vop_getattr_args /* { 886 struct vnode *a_vp; 887 struct vattr *a_vap; 888 struct ucred *a_cred; 889 struct thread *a_td; 890 } */ *ap; 891 { 892 int error; 893 struct union_node *un = VTOUNION(ap->a_vp); 894 struct union_mount *um = MOUNTTOUNIONMOUNT(ap->a_vp->v_mount); 895 struct vnode *vp; 896 struct vattr *vap; 897 struct vattr va; 898 899 /* 900 * Some programs walk the filesystem hierarchy by counting 901 * links to directories to avoid stat'ing all the time. 902 * This means the link count on directories needs to be "correct". 903 * The only way to do that is to call getattr on both layers 904 * and fix up the link count. The link count will not necessarily 905 * be accurate but will be large enough to defeat the tree walkers. 906 */ 907 908 vap = ap->a_vap; 909 910 if ((vp = un->un_uppervp) != NULLVP) { 911 error = VOP_GETATTR(vp, vap, ap->a_cred, ap->a_td); 912 if (error) 913 return (error); 914 /* XXX isn't this dangerous without a lock? */ 915 union_newsize(ap->a_vp, vap->va_size, VNOVAL); 916 } 917 918 if (vp == NULLVP) { 919 vp = un->un_lowervp; 920 } else if (vp->v_type == VDIR && un->un_lowervp != NULLVP) { 921 vp = un->un_lowervp; 922 vap = &va; 923 } else { 924 vp = NULLVP; 925 } 926 927 if (vp != NULLVP) { 928 error = VOP_GETATTR(vp, vap, ap->a_cred, ap->a_td); 929 if (error) 930 return (error); 931 /* XXX isn't this dangerous without a lock? */ 932 union_newsize(ap->a_vp, VNOVAL, vap->va_size); 933 } 934 935 if (ap->a_vap->va_fsid == um->um_upperdev) 936 ap->a_vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsid.val[0]; 937 938 if ((vap != ap->a_vap) && (vap->va_type == VDIR)) 939 ap->a_vap->va_nlink += vap->va_nlink; 940 return (0); 941 } 942 943 static int 944 union_setattr(ap) 945 struct vop_setattr_args /* { 946 struct vnode *a_vp; 947 struct vattr *a_vap; 948 struct ucred *a_cred; 949 struct thread *a_td; 950 } */ *ap; 951 { 952 struct union_node *un = VTOUNION(ap->a_vp); 953 struct thread *td = ap->a_td; 954 struct vattr *vap = ap->a_vap; 955 struct vnode *uppervp; 956 int error; 957 958 /* 959 * Disallow write attempts on filesystems mounted read-only. 960 */ 961 if ((ap->a_vp->v_mount->mnt_flag & MNT_RDONLY) && 962 (vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL || 963 vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL || 964 vap->va_mtime.tv_sec != VNOVAL || 965 vap->va_mode != (mode_t)VNOVAL)) { 966 return (EROFS); 967 } 968 969 /* 970 * Handle case of truncating lower object to zero size 971 * by creating a zero length upper object. This is to 972 * handle the case of open with O_TRUNC and O_CREAT. 973 */ 974 if (un->un_uppervp == NULLVP && (un->un_lowervp->v_type == VREG)) { 975 error = union_copyup(un, (ap->a_vap->va_size != 0), 976 ap->a_cred, ap->a_td); 977 if (error) 978 return (error); 979 } 980 981 /* 982 * Try to set attributes in upper layer, 983 * otherwise return read-only filesystem error. 984 */ 985 error = EROFS; 986 if ((uppervp = union_lock_upper(un, td)) != NULLVP) { 987 error = VOP_SETATTR(un->un_uppervp, ap->a_vap, 988 ap->a_cred, ap->a_td); 989 if ((error == 0) && (ap->a_vap->va_size != VNOVAL)) 990 union_newsize(ap->a_vp, ap->a_vap->va_size, VNOVAL); 991 union_unlock_upper(uppervp, td); 992 } 993 return (error); 994 } 995 996 static int 997 union_read(ap) 998 struct vop_read_args /* { 999 struct vnode *a_vp; 1000 struct uio *a_uio; 1001 int a_ioflag; 1002 struct ucred *a_cred; 1003 } */ *ap; 1004 { 1005 struct union_node *un = VTOUNION(ap->a_vp); 1006 struct thread *td = ap->a_uio->uio_td; 1007 struct vnode *uvp; 1008 int error; 1009 1010 uvp = union_lock_other(un, td); 1011 KASSERT(uvp != NULL, ("union_read: backing vnode missing!")); 1012 1013 error = VOP_READ(uvp, ap->a_uio, ap->a_ioflag, ap->a_cred); 1014 union_unlock_other(uvp, td); 1015 1016 /* 1017 * XXX 1018 * Perhaps the size of the underlying object has changed under 1019 * our feet. Take advantage of the offset information present 1020 * in the uio structure. 1021 */ 1022 if (error == 0) { 1023 struct union_node *un = VTOUNION(ap->a_vp); 1024 off_t cur = ap->a_uio->uio_offset; 1025 1026 if (uvp == un->un_uppervp) { 1027 if (cur > un->un_uppersz) 1028 union_newsize(ap->a_vp, cur, VNOVAL); 1029 } else { 1030 if (cur > un->un_lowersz) 1031 union_newsize(ap->a_vp, VNOVAL, cur); 1032 } 1033 } 1034 return (error); 1035 } 1036 1037 static int 1038 union_write(ap) 1039 struct vop_write_args /* { 1040 struct vnode *a_vp; 1041 struct uio *a_uio; 1042 int a_ioflag; 1043 struct ucred *a_cred; 1044 } */ *ap; 1045 { 1046 struct union_node *un = VTOUNION(ap->a_vp); 1047 struct thread *td = ap->a_uio->uio_td; 1048 struct vnode *uppervp; 1049 int error; 1050 1051 if ((uppervp = union_lock_upper(un, td)) == NULLVP) 1052 panic("union: missing upper layer in write"); 1053 1054 error = VOP_WRITE(uppervp, ap->a_uio, ap->a_ioflag, ap->a_cred); 1055 1056 /* 1057 * The size of the underlying object may be changed by the 1058 * write. 1059 */ 1060 if (error == 0) { 1061 off_t cur = ap->a_uio->uio_offset; 1062 1063 if (cur > un->un_uppersz) 1064 union_newsize(ap->a_vp, cur, VNOVAL); 1065 } 1066 union_unlock_upper(uppervp, td); 1067 return (error); 1068 } 1069 1070 static int 1071 union_lease(ap) 1072 struct vop_lease_args /* { 1073 struct vnode *a_vp; 1074 struct thread *a_td; 1075 struct ucred *a_cred; 1076 int a_flag; 1077 } */ *ap; 1078 { 1079 struct vnode *ovp = OTHERVP(ap->a_vp); 1080 1081 ap->a_vp = ovp; 1082 return (VOP_LEASE_AP(ap)); 1083 } 1084 1085 static int 1086 union_ioctl(ap) 1087 struct vop_ioctl_args /* { 1088 struct vnode *a_vp; 1089 u_long a_command; 1090 caddr_t a_data; 1091 int a_fflag; 1092 struct ucred *a_cred; 1093 struct thread *a_td; 1094 } */ *ap; 1095 { 1096 struct vnode *ovp = OTHERVP(ap->a_vp); 1097 1098 ap->a_vp = ovp; 1099 return (VOP_IOCTL_AP(ap)); 1100 } 1101 1102 static int 1103 union_poll(ap) 1104 struct vop_poll_args /* { 1105 struct vnode *a_vp; 1106 int a_events; 1107 struct ucred *a_cred; 1108 struct thread *a_td; 1109 } */ *ap; 1110 { 1111 struct vnode *ovp = OTHERVP(ap->a_vp); 1112 1113 ap->a_vp = ovp; 1114 return (VOP_POLL_AP(ap)); 1115 } 1116 1117 static int 1118 union_fsync(ap) 1119 struct vop_fsync_args /* { 1120 struct vnode *a_vp; 1121 struct ucred *a_cred; 1122 int a_waitfor; 1123 struct thread *a_td; 1124 } */ *ap; 1125 { 1126 int error = 0; 1127 struct thread *td = ap->a_td; 1128 struct vnode *targetvp; 1129 struct union_node *un = VTOUNION(ap->a_vp); 1130 1131 if ((targetvp = union_lock_other(un, td)) != NULLVP) { 1132 error = VOP_FSYNC(targetvp, ap->a_waitfor, td); 1133 union_unlock_other(targetvp, td); 1134 } 1135 1136 return (error); 1137 } 1138 1139 /* 1140 * union_remove: 1141 * 1142 * Remove the specified cnp. The dvp and vp are passed to us locked 1143 * and must remain locked on return. 1144 */ 1145 1146 static int 1147 union_remove(ap) 1148 struct vop_remove_args /* { 1149 struct vnode *a_dvp; 1150 struct vnode *a_vp; 1151 struct componentname *a_cnp; 1152 } */ *ap; 1153 { 1154 struct union_node *dun = VTOUNION(ap->a_dvp); 1155 struct union_node *un = VTOUNION(ap->a_vp); 1156 struct componentname *cnp = ap->a_cnp; 1157 struct thread *td = cnp->cn_thread; 1158 struct vnode *uppervp; 1159 struct vnode *upperdvp; 1160 int error; 1161 1162 if ((upperdvp = union_lock_upper(dun, td)) == NULLVP) 1163 panic("union remove: null upper vnode"); 1164 1165 if ((uppervp = union_lock_upper(un, td)) != NULLVP) { 1166 if (union_dowhiteout(un, cnp->cn_cred, td)) 1167 cnp->cn_flags |= DOWHITEOUT; 1168 if (cnp->cn_flags & DOWHITEOUT) /* XXX fs corruption */ 1169 error = EOPNOTSUPP; 1170 else 1171 error = VOP_REMOVE(upperdvp, uppervp, cnp); 1172 if (!error) 1173 union_removed_upper(un); 1174 union_unlock_upper(uppervp, td); 1175 } else { 1176 error = union_mkwhiteout( 1177 MOUNTTOUNIONMOUNT(ap->a_dvp->v_mount), 1178 upperdvp, ap->a_cnp, un->un_path); 1179 } 1180 union_unlock_upper(upperdvp, td); 1181 return (error); 1182 } 1183 1184 /* 1185 * union_link: 1186 * 1187 * tdvp and vp will be locked on entry. 1188 * tdvp and vp should remain locked on return. 1189 */ 1190 1191 static int 1192 union_link(ap) 1193 struct vop_link_args /* { 1194 struct vnode *a_tdvp; 1195 struct vnode *a_vp; 1196 struct componentname *a_cnp; 1197 } */ *ap; 1198 { 1199 struct componentname *cnp = ap->a_cnp; 1200 struct thread *td = cnp->cn_thread; 1201 struct union_node *dun = VTOUNION(ap->a_tdvp); 1202 struct vnode *vp; 1203 struct vnode *tdvp; 1204 int error = 0; 1205 1206 if (ap->a_tdvp->v_op != ap->a_vp->v_op) { 1207 vp = ap->a_vp; 1208 } else { 1209 struct union_node *tun = VTOUNION(ap->a_vp); 1210 1211 if (tun->un_uppervp == NULLVP) { 1212 #if 0 1213 if (dun->un_uppervp == tun->un_dirvp) { 1214 if (dun->un_flags & UN_ULOCK) { 1215 dun->un_flags &= ~UN_ULOCK; 1216 VOP_UNLOCK(dun->un_uppervp, 0, td); 1217 } 1218 } 1219 #endif 1220 error = union_copyup(tun, 1, cnp->cn_cred, td); 1221 #if 0 1222 if (dun->un_uppervp == tun->un_dirvp) { 1223 vn_lock(dun->un_uppervp, 1224 LK_EXCLUSIVE | LK_RETRY, td); 1225 dun->un_flags |= UN_ULOCK; 1226 } 1227 #endif 1228 if (error) 1229 return (error); 1230 } 1231 vp = tun->un_uppervp; 1232 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); 1233 } 1234 1235 /* 1236 * Make sure upper is locked, then unlock the union directory we were 1237 * called with to avoid a deadlock while we are calling VOP_LINK() on 1238 * the upper (with tdvp locked and vp not locked). Our ap->a_tdvp 1239 * is expected to be locked on return. 1240 */ 1241 1242 if ((tdvp = union_lock_upper(dun, td)) == NULLVP) 1243 return (EROFS); 1244 1245 VOP_UNLOCK(ap->a_tdvp, 0, td); /* unlock calling node */ 1246 error = VOP_LINK(tdvp, vp, cnp); /* call link on upper */ 1247 1248 /* 1249 * Unlock tun->un_uppervp if we locked it above. 1250 */ 1251 if (ap->a_tdvp->v_op == ap->a_vp->v_op) 1252 VOP_UNLOCK(vp, 0, td); 1253 /* 1254 * We have to unlock tdvp prior to relocking our calling node in 1255 * order to avoid a deadlock. We also have to unlock ap->a_vp 1256 * before relocking the directory, but then we have to relock 1257 * ap->a_vp as our caller expects. 1258 */ 1259 VOP_UNLOCK(ap->a_vp, 0, td); 1260 union_unlock_upper(tdvp, td); 1261 vn_lock(ap->a_tdvp, LK_EXCLUSIVE | LK_RETRY, td); 1262 vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY, td); 1263 return (error); 1264 } 1265 1266 static int 1267 union_rename(ap) 1268 struct vop_rename_args /* { 1269 struct vnode *a_fdvp; 1270 struct vnode *a_fvp; 1271 struct componentname *a_fcnp; 1272 struct vnode *a_tdvp; 1273 struct vnode *a_tvp; 1274 struct componentname *a_tcnp; 1275 } */ *ap; 1276 { 1277 int error; 1278 struct vnode *fdvp = ap->a_fdvp; 1279 struct vnode *fvp = ap->a_fvp; 1280 struct vnode *tdvp = ap->a_tdvp; 1281 struct vnode *tvp = ap->a_tvp; 1282 1283 /* 1284 * Figure out what fdvp to pass to our upper or lower vnode. If we 1285 * replace the fdvp, release the original one and ref the new one. 1286 */ 1287 1288 if (fdvp->v_op == &union_vnodeops) { /* always true */ 1289 struct union_node *un = VTOUNION(fdvp); 1290 if (un->un_uppervp == NULLVP) { 1291 /* 1292 * this should never happen in normal 1293 * operation but might if there was 1294 * a problem creating the top-level shadow 1295 * directory. 1296 */ 1297 error = EXDEV; 1298 goto bad; 1299 } 1300 fdvp = un->un_uppervp; 1301 VREF(fdvp); 1302 vrele(ap->a_fdvp); 1303 } 1304 1305 /* 1306 * Figure out what fvp to pass to our upper or lower vnode. If we 1307 * replace the fvp, release the original one and ref the new one. 1308 */ 1309 1310 if (fvp->v_op == &union_vnodeops) { /* always true */ 1311 struct union_node *un = VTOUNION(fvp); 1312 #if 0 1313 struct union_mount *um = MOUNTTOUNIONMOUNT(fvp->v_mount); 1314 #endif 1315 1316 if (un->un_uppervp == NULLVP) { 1317 switch(fvp->v_type) { 1318 case VREG: 1319 vn_lock(un->un_vnode, LK_EXCLUSIVE | LK_RETRY, ap->a_fcnp->cn_thread); 1320 error = union_copyup(un, 1, ap->a_fcnp->cn_cred, ap->a_fcnp->cn_thread); 1321 VOP_UNLOCK(un->un_vnode, 0, ap->a_fcnp->cn_thread); 1322 if (error) 1323 goto bad; 1324 break; 1325 case VDIR: 1326 /* 1327 * XXX not yet. 1328 * 1329 * There is only one way to rename a directory 1330 * based in the lowervp, and that is to copy 1331 * the entire directory hierarchy. Otherwise 1332 * it would not last across a reboot. 1333 */ 1334 #if 0 1335 vrele(fvp); 1336 fvp = NULL; 1337 vn_lock(fdvp, LK_EXCLUSIVE | LK_RETRY, ap->a_fcnp->cn_thread); 1338 error = union_mkshadow(um, fdvp, 1339 ap->a_fcnp, &un->un_uppervp); 1340 VOP_UNLOCK(fdvp, 0, ap->a_fcnp->cn_thread); 1341 if (un->un_uppervp) 1342 VOP_UNLOCK(un->un_uppervp, 0, ap->a_fcnp->cn_thread); 1343 if (error) 1344 goto bad; 1345 break; 1346 #endif 1347 default: 1348 error = EXDEV; 1349 goto bad; 1350 } 1351 } 1352 1353 if (un->un_lowervp != NULLVP) 1354 ap->a_fcnp->cn_flags |= DOWHITEOUT; 1355 fvp = un->un_uppervp; 1356 VREF(fvp); 1357 vrele(ap->a_fvp); 1358 } 1359 1360 /* 1361 * Figure out what tdvp (destination directory) to pass to the 1362 * lower level. If we replace it with uppervp, we need to vput the 1363 * old one. The exclusive lock is transfered to what we will pass 1364 * down in the VOP_RENAME() and we replace uppervp with a simple 1365 * reference. 1366 */ 1367 1368 if (tdvp->v_op == &union_vnodeops) { 1369 struct union_node *un = VTOUNION(tdvp); 1370 1371 if (un->un_uppervp == NULLVP) { 1372 /* 1373 * This should never happen in normal 1374 * operation but might if there was 1375 * a problem creating the top-level shadow 1376 * directory. 1377 */ 1378 error = EXDEV; 1379 goto bad; 1380 } 1381 1382 /* 1383 * New tdvp is a lock and reference on uppervp. 1384 * Put away the old tdvp. 1385 */ 1386 tdvp = union_lock_upper(un, ap->a_tcnp->cn_thread); 1387 vput(ap->a_tdvp); 1388 } 1389 1390 /* 1391 * Figure out what tvp (destination file) to pass to the 1392 * lower level. 1393 * 1394 * If the uppervp file does not exist, put away the (wrong) 1395 * file and change tvp to NULL. 1396 */ 1397 1398 if (tvp != NULLVP && tvp->v_op == &union_vnodeops) { 1399 struct union_node *un = VTOUNION(tvp); 1400 1401 tvp = union_lock_upper(un, ap->a_tcnp->cn_thread); 1402 vput(ap->a_tvp); 1403 /* note: tvp may be NULL */ 1404 } 1405 1406 /* 1407 * VOP_RENAME() releases/vputs prior to returning, so we have no 1408 * cleanup to do. 1409 */ 1410 1411 return (VOP_RENAME(fdvp, fvp, ap->a_fcnp, tdvp, tvp, ap->a_tcnp)); 1412 1413 /* 1414 * Error. We still have to release / vput the various elements. 1415 */ 1416 1417 bad: 1418 vrele(fdvp); 1419 if (fvp) 1420 vrele(fvp); 1421 vput(tdvp); 1422 if (tvp != NULLVP) { 1423 if (tvp != tdvp) 1424 vput(tvp); 1425 else 1426 vrele(tvp); 1427 } 1428 return (error); 1429 } 1430 1431 static int 1432 union_mkdir(ap) 1433 struct vop_mkdir_args /* { 1434 struct vnode *a_dvp; 1435 struct vnode **a_vpp; 1436 struct componentname *a_cnp; 1437 struct vattr *a_vap; 1438 } */ *ap; 1439 { 1440 struct union_node *dun = VTOUNION(ap->a_dvp); 1441 struct componentname *cnp = ap->a_cnp; 1442 struct thread *td = cnp->cn_thread; 1443 struct vnode *upperdvp; 1444 int error = EROFS; 1445 1446 if ((upperdvp = union_lock_upper(dun, td)) != NULLVP) { 1447 struct vnode *vp; 1448 1449 error = VOP_MKDIR(upperdvp, &vp, cnp, ap->a_vap); 1450 union_unlock_upper(upperdvp, td); 1451 1452 if (error == 0) { 1453 VOP_UNLOCK(vp, 0, td); 1454 UDEBUG(("ALLOCVP-2 FROM %p REFS %d\n", vp, vrefcnt(vp))); 1455 error = union_allocvp(ap->a_vpp, ap->a_dvp->v_mount, 1456 ap->a_dvp, NULLVP, cnp, vp, NULLVP, 1); 1457 UDEBUG(("ALLOCVP-2B FROM %p REFS %d\n", *ap->a_vpp, vrefcnt(vp))); 1458 } 1459 } 1460 return (error); 1461 } 1462 1463 static int 1464 union_rmdir(ap) 1465 struct vop_rmdir_args /* { 1466 struct vnode *a_dvp; 1467 struct vnode *a_vp; 1468 struct componentname *a_cnp; 1469 } */ *ap; 1470 { 1471 struct union_node *dun = VTOUNION(ap->a_dvp); 1472 struct union_node *un = VTOUNION(ap->a_vp); 1473 struct componentname *cnp = ap->a_cnp; 1474 struct thread *td = cnp->cn_thread; 1475 struct vnode *upperdvp; 1476 struct vnode *uppervp; 1477 int error; 1478 1479 if ((upperdvp = union_lock_upper(dun, td)) == NULLVP) 1480 panic("union rmdir: null upper vnode"); 1481 1482 if ((uppervp = union_lock_upper(un, td)) != NULLVP) { 1483 if (union_dowhiteout(un, cnp->cn_cred, td)) 1484 cnp->cn_flags |= DOWHITEOUT; 1485 if (cnp->cn_flags & DOWHITEOUT) /* XXX fs corruption */ 1486 error = EOPNOTSUPP; 1487 else 1488 error = VOP_RMDIR(upperdvp, uppervp, ap->a_cnp); 1489 if (!error) 1490 union_removed_upper(un); 1491 union_unlock_upper(uppervp, td); 1492 } else { 1493 error = union_mkwhiteout( 1494 MOUNTTOUNIONMOUNT(ap->a_dvp->v_mount), 1495 dun->un_uppervp, ap->a_cnp, un->un_path); 1496 } 1497 union_unlock_upper(upperdvp, td); 1498 return (error); 1499 } 1500 1501 /* 1502 * union_symlink: 1503 * 1504 * dvp is locked on entry and remains locked on return. a_vpp is garbage 1505 * (unused). 1506 */ 1507 1508 static int 1509 union_symlink(ap) 1510 struct vop_symlink_args /* { 1511 struct vnode *a_dvp; 1512 struct vnode **a_vpp; 1513 struct componentname *a_cnp; 1514 struct vattr *a_vap; 1515 char *a_target; 1516 } */ *ap; 1517 { 1518 struct union_node *dun = VTOUNION(ap->a_dvp); 1519 struct componentname *cnp = ap->a_cnp; 1520 struct thread *td = cnp->cn_thread; 1521 struct vnode *dvp; 1522 int error = EROFS; 1523 1524 if ((dvp = union_lock_upper(dun, td)) != NULLVP) { 1525 error = VOP_SYMLINK(dvp, ap->a_vpp, cnp, ap->a_vap, 1526 ap->a_target); 1527 union_unlock_upper(dvp, td); 1528 } 1529 return (error); 1530 } 1531 1532 /* 1533 * union_readdir ()works in concert with getdirentries() and 1534 * readdir(3) to provide a list of entries in the unioned 1535 * directories. getdirentries() is responsible for walking 1536 * down the union stack. readdir(3) is responsible for 1537 * eliminating duplicate names from the returned data stream. 1538 */ 1539 static int 1540 union_readdir(ap) 1541 struct vop_readdir_args /* { 1542 struct vnode *a_vp; 1543 struct uio *a_uio; 1544 struct ucred *a_cred; 1545 int *a_eofflag; 1546 u_long *a_cookies; 1547 int a_ncookies; 1548 } */ *ap; 1549 { 1550 struct union_node *un = VTOUNION(ap->a_vp); 1551 struct thread *td = ap->a_uio->uio_td; 1552 struct vnode *uvp; 1553 int error = 0; 1554 1555 if ((uvp = union_lock_upper(un, td)) != NULLVP) { 1556 ap->a_vp = uvp; 1557 error = VOP_READDIR_AP(ap); 1558 union_unlock_upper(uvp, td); 1559 } 1560 return(error); 1561 } 1562 1563 static int 1564 union_readlink(ap) 1565 struct vop_readlink_args /* { 1566 struct vnode *a_vp; 1567 struct uio *a_uio; 1568 struct ucred *a_cred; 1569 } */ *ap; 1570 { 1571 int error; 1572 struct union_node *un = VTOUNION(ap->a_vp); 1573 struct uio *uio = ap->a_uio; 1574 struct thread *td = uio->uio_td; 1575 struct vnode *vp; 1576 1577 vp = union_lock_other(un, td); 1578 KASSERT(vp != NULL, ("union_readlink: backing vnode missing!")); 1579 1580 ap->a_vp = vp; 1581 error = VOP_READLINK_AP(ap); 1582 union_unlock_other(vp, td); 1583 1584 return (error); 1585 } 1586 1587 static int 1588 union_getwritemount(ap) 1589 struct vop_getwritemount_args /* { 1590 struct vnode *a_vp; 1591 struct mount **a_mpp; 1592 } */ *ap; 1593 { 1594 struct vnode *vp = ap->a_vp; 1595 struct vnode *uvp = UPPERVP(vp); 1596 1597 if (uvp == NULL) { 1598 VI_LOCK(vp); 1599 if (vp->v_iflag & VI_FREE) { 1600 VI_UNLOCK(vp); 1601 return (EOPNOTSUPP); 1602 } 1603 VI_UNLOCK(vp); 1604 return (EACCES); 1605 } 1606 return(VOP_GETWRITEMOUNT(uvp, ap->a_mpp)); 1607 } 1608 1609 /* 1610 * union_inactive: 1611 * 1612 * Called with the vnode locked. We are expected to unlock the vnode. 1613 */ 1614 1615 static int 1616 union_inactive(ap) 1617 struct vop_inactive_args /* { 1618 struct vnode *a_vp; 1619 struct thread *a_td; 1620 } */ *ap; 1621 { 1622 struct vnode *vp = ap->a_vp; 1623 struct union_node *un = VTOUNION(vp); 1624 1625 /* 1626 * Do nothing (and _don't_ bypass). 1627 * Wait to vrele lowervp until reclaim, 1628 * so that until then our union_node is in the 1629 * cache and reusable. 1630 * 1631 */ 1632 1633 if (un->un_dircache != NULL) 1634 union_dircache_free(un); 1635 1636 #if 0 1637 if ((un->un_flags & UN_ULOCK) && un->un_uppervp) { 1638 un->un_flags &= ~UN_ULOCK; 1639 VOP_UNLOCK(un->un_uppervp, 0, td); 1640 } 1641 #endif 1642 1643 if ((un->un_flags & UN_CACHED) == 0) 1644 vgone(vp); 1645 1646 return (0); 1647 } 1648 1649 static int 1650 union_reclaim(ap) 1651 struct vop_reclaim_args /* { 1652 struct vnode *a_vp; 1653 } */ *ap; 1654 { 1655 union_freevp(ap->a_vp); 1656 1657 return (0); 1658 } 1659 1660 static int 1661 union_print(ap) 1662 struct vop_print_args /* { 1663 struct vnode *a_vp; 1664 } */ *ap; 1665 { 1666 struct vnode *vp = ap->a_vp; 1667 1668 printf("\tvp=%p, uppervp=%p, lowervp=%p\n", 1669 vp, UPPERVP(vp), LOWERVP(vp)); 1670 if (UPPERVP(vp) != NULLVP) 1671 vprint("union: upper", UPPERVP(vp)); 1672 if (LOWERVP(vp) != NULLVP) 1673 vprint("union: lower", LOWERVP(vp)); 1674 1675 return (0); 1676 } 1677 1678 static int 1679 union_pathconf(ap) 1680 struct vop_pathconf_args /* { 1681 struct vnode *a_vp; 1682 int a_name; 1683 int *a_retval; 1684 } */ *ap; 1685 { 1686 int error; 1687 struct thread *td = curthread; /* XXX */ 1688 struct union_node *un = VTOUNION(ap->a_vp); 1689 struct vnode *vp; 1690 1691 vp = union_lock_other(un, td); 1692 KASSERT(vp != NULL, ("union_pathconf: backing vnode missing!")); 1693 1694 ap->a_vp = vp; 1695 error = VOP_PATHCONF_AP(ap); 1696 union_unlock_other(vp, td); 1697 1698 return (error); 1699 } 1700 1701 static int 1702 union_advlock(ap) 1703 struct vop_advlock_args /* { 1704 struct vnode *a_vp; 1705 caddr_t a_id; 1706 int a_op; 1707 struct flock *a_fl; 1708 int a_flags; 1709 } */ *ap; 1710 { 1711 register struct vnode *ovp = OTHERVP(ap->a_vp); 1712 1713 ap->a_vp = ovp; 1714 return (VOP_ADVLOCK_AP(ap)); 1715 } 1716 1717 1718 /* 1719 * XXX - vop_strategy must be hand coded because it has no 1720 * YYY - and it is not coherent with anything 1721 * 1722 * vnode in its arguments. 1723 * This goes away with a merged VM/buffer cache. 1724 */ 1725 static int 1726 union_strategy(ap) 1727 struct vop_strategy_args /* { 1728 struct vnode *a_vp; 1729 struct buf *a_bp; 1730 } */ *ap; 1731 { 1732 struct buf *bp = ap->a_bp; 1733 struct vnode *othervp = OTHERVP(ap->a_vp); 1734 1735 #ifdef DIAGNOSTIC 1736 if (othervp == NULLVP) 1737 panic("union_strategy: nil vp"); 1738 if ((bp->b_iocmd == BIO_WRITE) && 1739 (othervp == LOWERVP(ap->a_vp))) 1740 panic("union_strategy: writing to lowervp"); 1741 #endif 1742 return (VOP_STRATEGY(othervp, bp)); 1743 } 1744 1745 static int 1746 union_getacl(ap) 1747 struct vop_getacl_args /* { 1748 struct vnode *a_vp; 1749 acl_type_t a_type; 1750 struct acl *a_aclp; 1751 struct ucred *a_cred; 1752 struct thread *a_td; 1753 } */ *ap; 1754 { 1755 int error; 1756 struct union_node *un = VTOUNION(ap->a_vp); 1757 struct vnode *vp; 1758 1759 vp = union_lock_other(un, ap->a_td); 1760 ap->a_vp = vp; 1761 error = VOP_GETACL_AP(ap); 1762 union_unlock_other(vp, ap->a_td); 1763 1764 return (error); 1765 } 1766 1767 static int 1768 union_setacl(ap) 1769 struct vop_setacl_args /* { 1770 struct vnode *a_vp; 1771 acl_type_t a_type; 1772 struct acl *a_aclp; 1773 struct ucred *a_cred; 1774 struct thread *a_td; 1775 } */ *ap; 1776 { 1777 int error; 1778 struct union_node *un = VTOUNION(ap->a_vp); 1779 struct vnode *vp; 1780 1781 vp = union_lock_other(un, ap->a_td); 1782 ap->a_vp = vp; 1783 error = VOP_SETACL_AP(ap); 1784 union_unlock_other(vp, ap->a_td); 1785 1786 return (error); 1787 } 1788 1789 static int 1790 union_aclcheck(ap) 1791 struct vop_aclcheck_args /* { 1792 struct vnode *a_vp; 1793 acl_type_t a_type; 1794 struct acl *a_aclp; 1795 struct ucred *a_cred; 1796 struct thread *a_td; 1797 } */ *ap; 1798 { 1799 struct vnode *ovp = OTHERVP(ap->a_vp); 1800 1801 ap->a_vp = ovp; 1802 return (VOP_ACLCHECK_AP(ap)); 1803 } 1804 1805 static int 1806 union_closeextattr(ap) 1807 struct vop_closeextattr_args /* { 1808 struct vnode *a_vp; 1809 int a_commit; 1810 struct ucred *a_cred; 1811 struct thread *a_td; 1812 } */ *ap; 1813 { 1814 int error; 1815 struct union_node *un = VTOUNION(ap->a_vp); 1816 struct vnode *vp; 1817 1818 vp = union_lock_other(un, ap->a_td); 1819 ap->a_vp = vp; 1820 error = VOP_CLOSEEXTATTR_AP(ap); 1821 union_unlock_other(vp, ap->a_td); 1822 1823 return (error); 1824 } 1825 1826 static int 1827 union_getextattr(ap) 1828 struct vop_getextattr_args /* { 1829 struct vnode *a_vp; 1830 int a_attrnamespace; 1831 const char *a_name; 1832 struct uio *a_uio; 1833 size_t *a_size; 1834 struct ucred *a_cred; 1835 struct thread *a_td; 1836 } */ *ap; 1837 { 1838 int error; 1839 struct union_node *un = VTOUNION(ap->a_vp); 1840 struct vnode *vp; 1841 1842 vp = union_lock_other(un, ap->a_td); 1843 ap->a_vp = vp; 1844 error = VOP_GETEXTATTR_AP(ap); 1845 union_unlock_other(vp, ap->a_td); 1846 1847 return (error); 1848 } 1849 1850 static int 1851 union_listextattr(ap) 1852 struct vop_listextattr_args /* { 1853 struct vnode *a_vp; 1854 int a_attrnamespace; 1855 struct uio *a_uio; 1856 size_t *a_size; 1857 struct ucred *a_cred; 1858 struct thread *a_td; 1859 } */ *ap; 1860 { 1861 int error; 1862 struct union_node *un = VTOUNION(ap->a_vp); 1863 struct vnode *vp; 1864 1865 vp = union_lock_other(un, ap->a_td); 1866 ap->a_vp = vp; 1867 error = VOP_LISTEXTATTR_AP(ap); 1868 union_unlock_other(vp, ap->a_td); 1869 1870 return (error); 1871 } 1872 1873 static int 1874 union_openextattr(ap) 1875 struct vop_openextattr_args /* { 1876 struct vnode *a_vp; 1877 struct ucred *a_cred; 1878 struct thread *a_td; 1879 } */ *ap; 1880 { 1881 int error; 1882 struct union_node *un = VTOUNION(ap->a_vp); 1883 struct vnode *vp; 1884 1885 vp = union_lock_other(un, ap->a_td); 1886 ap->a_vp = vp; 1887 error = VOP_OPENEXTATTR_AP(ap); 1888 union_unlock_other(vp, ap->a_td); 1889 1890 return (error); 1891 } 1892 1893 static int 1894 union_deleteextattr(ap) 1895 struct vop_deleteextattr_args /* { 1896 struct vnode *a_vp; 1897 int a_attrnamespace; 1898 const char *a_name; 1899 struct ucred *a_cred; 1900 struct thread *a_td; 1901 } */ *ap; 1902 { 1903 int error; 1904 struct union_node *un = VTOUNION(ap->a_vp); 1905 struct vnode *vp; 1906 1907 vp = union_lock_other(un, ap->a_td); 1908 ap->a_vp = vp; 1909 error = VOP_DELETEEXTATTR_AP(ap); 1910 union_unlock_other(vp, ap->a_td); 1911 1912 return (error); 1913 } 1914 1915 static int 1916 union_setextattr(ap) 1917 struct vop_setextattr_args /* { 1918 struct vnode *a_vp; 1919 int a_attrnamespace; 1920 const char *a_name; 1921 struct uio *a_uio; 1922 struct ucred *a_cred; 1923 struct thread *a_td; 1924 } */ *ap; 1925 { 1926 int error; 1927 struct union_node *un = VTOUNION(ap->a_vp); 1928 struct vnode *vp; 1929 1930 vp = union_lock_other(un, ap->a_td); 1931 ap->a_vp = vp; 1932 error = VOP_SETEXTATTR_AP(ap); 1933 union_unlock_other(vp, ap->a_td); 1934 1935 return (error); 1936 } 1937 1938 static int 1939 union_setlabel(ap) 1940 struct vop_setlabel_args /* { 1941 struct vnode *a_vp; 1942 struct label *a_label; 1943 struct ucred *a_cred; 1944 struct thread *a_td; 1945 } */ *ap; 1946 { 1947 int error; 1948 struct union_node *un = VTOUNION(ap->a_vp); 1949 struct vnode *vp; 1950 1951 vp = union_lock_other(un, ap->a_td); 1952 ap->a_vp = vp; 1953 error = VOP_SETLABEL_AP(ap); 1954 union_unlock_other(vp, ap->a_td); 1955 1956 return (error); 1957 } 1958 1959 /* 1960 * Global vfs data structures 1961 */ 1962 struct vop_vector union_vnodeops = { 1963 .vop_default = &default_vnodeops, 1964 1965 .vop_access = union_access, 1966 .vop_aclcheck = union_aclcheck, 1967 .vop_advlock = union_advlock, 1968 .vop_bmap = VOP_EOPNOTSUPP, 1969 .vop_close = union_close, 1970 .vop_closeextattr = union_closeextattr, 1971 .vop_create = union_create, 1972 .vop_deleteextattr = union_deleteextattr, 1973 .vop_fsync = union_fsync, 1974 .vop_getacl = union_getacl, 1975 .vop_getattr = union_getattr, 1976 .vop_getextattr = union_getextattr, 1977 .vop_getwritemount = union_getwritemount, 1978 .vop_inactive = union_inactive, 1979 .vop_ioctl = union_ioctl, 1980 .vop_lease = union_lease, 1981 .vop_link = union_link, 1982 .vop_listextattr = union_listextattr, 1983 .vop_lookup = union_lookup, 1984 .vop_mkdir = union_mkdir, 1985 .vop_mknod = union_mknod, 1986 .vop_open = union_open, 1987 .vop_openextattr = union_openextattr, 1988 .vop_pathconf = union_pathconf, 1989 .vop_poll = union_poll, 1990 .vop_print = union_print, 1991 .vop_read = union_read, 1992 .vop_readdir = union_readdir, 1993 .vop_readlink = union_readlink, 1994 .vop_reclaim = union_reclaim, 1995 .vop_remove = union_remove, 1996 .vop_rename = union_rename, 1997 .vop_rmdir = union_rmdir, 1998 .vop_setacl = union_setacl, 1999 .vop_setattr = union_setattr, 2000 .vop_setextattr = union_setextattr, 2001 .vop_setlabel = union_setlabel, 2002 .vop_strategy = union_strategy, 2003 .vop_symlink = union_symlink, 2004 .vop_whiteout = union_whiteout, 2005 .vop_write = union_write, 2006 }; 2007