1 /* 2 * Copyright (c) 1992, 1993, 1994 The Regents of the University of California. 3 * Copyright (c) 1992, 1993, 1994 Jan-Simon Pendry. 4 * 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 * 3. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed by the University of 20 * California, Berkeley and its contributors. 21 * 4. Neither the name of the University nor the names of its contributors 22 * may be used to endorse or promote products derived from this software 23 * without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 * SUCH DAMAGE. 36 * 37 * @(#)union_vnops.c 8.6 (Berkeley) 2/17/94 38 * $Id$ 39 */ 40 41 #include <sys/param.h> 42 #include <sys/systm.h> 43 #include <sys/proc.h> 44 #include <sys/file.h> 45 #include <sys/time.h> 46 #include <sys/types.h> 47 #include <sys/vnode.h> 48 #include <sys/mount.h> 49 #include <sys/namei.h> 50 #include <sys/malloc.h> 51 #include <sys/buf.h> 52 #include <sys/queue.h> 53 #include <miscfs/union/union.h> 54 55 #define FIXUP(un) { \ 56 if (((un)->un_flags & UN_ULOCK) == 0) { \ 57 union_fixup(un); \ 58 } \ 59 } 60 61 static void 62 union_fixup(un) 63 struct union_node *un; 64 { 65 66 VOP_LOCK(un->un_uppervp); 67 un->un_flags |= UN_ULOCK; 68 } 69 70 static int 71 union_lookup1(udvp, dvp, vpp, cnp) 72 struct vnode *udvp; 73 struct vnode *dvp; 74 struct vnode **vpp; 75 struct componentname *cnp; 76 { 77 int error; 78 struct vnode *tdvp; 79 struct mount *mp; 80 81 /* 82 * If stepping up the directory tree, check for going 83 * back across the mount point, in which case do what 84 * lookup would do by stepping back down the mount 85 * hierarchy. 86 */ 87 if (cnp->cn_flags & ISDOTDOT) { 88 for (;;) { 89 /* 90 * Don't do the NOCROSSMOUNT check 91 * at this level. By definition, 92 * union fs deals with namespaces, not 93 * filesystems. 94 */ 95 if ((dvp->v_flag & VROOT) == 0) 96 break; 97 98 tdvp = dvp; 99 dvp = dvp->v_mount->mnt_vnodecovered; 100 vput(tdvp); 101 VREF(dvp); 102 VOP_LOCK(dvp); 103 } 104 } 105 106 error = VOP_LOOKUP(dvp, &tdvp, cnp); 107 if (error) 108 return (error); 109 110 /* 111 * The parent directory will have been unlocked, unless lookup 112 * found the last component. In which case, re-lock the node 113 * here to allow it to be unlocked again (phew) in union_lookup. 114 */ 115 if (dvp != tdvp && !(cnp->cn_flags & ISLASTCN)) 116 VOP_LOCK(dvp); 117 118 dvp = tdvp; 119 120 /* 121 * Lastly check if the current node is a mount point in 122 * which case walk up the mount hierarchy making sure not to 123 * bump into the root of the mount tree (ie. dvp != udvp). 124 */ 125 while (dvp != udvp && (dvp->v_type == VDIR) && 126 (mp = dvp->v_mountedhere)) { 127 128 if (mp->mnt_flag & MNT_MLOCK) { 129 mp->mnt_flag |= MNT_MWAIT; 130 sleep((caddr_t) mp, PVFS); 131 continue; 132 } 133 134 if (error = VFS_ROOT(mp, &tdvp)) { 135 vput(dvp); 136 return (error); 137 } 138 139 vput(dvp); 140 dvp = tdvp; 141 } 142 143 *vpp = dvp; 144 return (0); 145 } 146 147 int 148 union_lookup(ap) 149 struct vop_lookup_args /* { 150 struct vnodeop_desc *a_desc; 151 struct vnode *a_dvp; 152 struct vnode **a_vpp; 153 struct componentname *a_cnp; 154 } */ *ap; 155 { 156 int error; 157 int uerror, lerror; 158 struct vnode *uppervp, *lowervp; 159 struct vnode *upperdvp, *lowerdvp; 160 struct vnode *dvp = ap->a_dvp; 161 struct union_node *dun = VTOUNION(dvp); 162 struct componentname *cnp = ap->a_cnp; 163 int lockparent = cnp->cn_flags & LOCKPARENT; 164 int rdonly = cnp->cn_flags & RDONLY; 165 struct union_mount *um = MOUNTTOUNIONMOUNT(dvp->v_mount); 166 struct ucred *saved_cred = 0; 167 168 cnp->cn_flags |= LOCKPARENT; 169 170 upperdvp = dun->un_uppervp; 171 lowerdvp = dun->un_lowervp; 172 uppervp = NULLVP; 173 lowervp = NULLVP; 174 175 /* 176 * do the lookup in the upper level. 177 * if that level comsumes additional pathnames, 178 * then assume that something special is going 179 * on and just return that vnode. 180 */ 181 if (upperdvp) { 182 FIXUP(dun); 183 uerror = union_lookup1(um->um_uppervp, upperdvp, 184 &uppervp, cnp); 185 /*if (uppervp == upperdvp) 186 dun->un_flags |= UN_KLOCK;*/ 187 188 if (cnp->cn_consume != 0) { 189 *ap->a_vpp = uppervp; 190 if (!lockparent) 191 cnp->cn_flags &= ~LOCKPARENT; 192 return (uerror); 193 } 194 } else { 195 uerror = ENOENT; 196 } 197 198 /* 199 * in a similar way to the upper layer, do the lookup 200 * in the lower layer. this time, if there is some 201 * component magic going on, then vput whatever we got 202 * back from the upper layer and return the lower vnode 203 * instead. 204 */ 205 if (lowerdvp) { 206 int nameiop; 207 208 VOP_LOCK(lowerdvp); 209 210 /* 211 * Only do a LOOKUP on the bottom node, since 212 * we won't be making changes to it anyway. 213 */ 214 nameiop = cnp->cn_nameiop; 215 cnp->cn_nameiop = LOOKUP; 216 if (um->um_op == UNMNT_BELOW) { 217 saved_cred = cnp->cn_cred; 218 cnp->cn_cred = um->um_cred; 219 } 220 lerror = union_lookup1(um->um_lowervp, lowerdvp, 221 &lowervp, cnp); 222 if (um->um_op == UNMNT_BELOW) 223 cnp->cn_cred = saved_cred; 224 cnp->cn_nameiop = nameiop; 225 226 if (lowervp != lowerdvp) 227 VOP_UNLOCK(lowerdvp); 228 229 if (cnp->cn_consume != 0) { 230 if (uppervp) { 231 if (uppervp == upperdvp) 232 vrele(uppervp); 233 else 234 vput(uppervp); 235 uppervp = NULLVP; 236 } 237 *ap->a_vpp = lowervp; 238 if (!lockparent) 239 cnp->cn_flags &= ~LOCKPARENT; 240 return (lerror); 241 } 242 } else { 243 lerror = ENOENT; 244 } 245 246 if (!lockparent) 247 cnp->cn_flags &= ~LOCKPARENT; 248 249 /* 250 * at this point, we have uerror and lerror indicating 251 * possible errors with the lookups in the upper and lower 252 * layers. additionally, uppervp and lowervp are (locked) 253 * references to existing vnodes in the upper and lower layers. 254 * 255 * there are now three cases to consider. 256 * 1. if both layers returned an error, then return whatever 257 * error the upper layer generated. 258 * 259 * 2. if the top layer failed and the bottom layer succeeded 260 * then two subcases occur. 261 * a. the bottom vnode is not a directory, in which 262 * case just return a new union vnode referencing 263 * an empty top layer and the existing bottom layer. 264 * b. the bottom vnode is a directory, in which case 265 * create a new directory in the top-level and 266 * continue as in case 3. 267 * 268 * 3. if the top layer succeeded then return a new union 269 * vnode referencing whatever the new top layer and 270 * whatever the bottom layer returned. 271 */ 272 273 *ap->a_vpp = NULLVP; 274 275 /* case 1. */ 276 if ((uerror != 0) && (lerror != 0)) { 277 return (uerror); 278 } 279 280 /* case 2. */ 281 if (uerror != 0 /* && (lerror == 0) */ ) { 282 if (lowervp->v_type == VDIR) { /* case 2b. */ 283 dun->un_flags &= ~UN_ULOCK; 284 VOP_UNLOCK(upperdvp); 285 uerror = union_mkshadow(um, upperdvp, cnp, &uppervp); 286 VOP_LOCK(upperdvp); 287 dun->un_flags |= UN_ULOCK; 288 289 if (uerror) { 290 if (lowervp) { 291 vput(lowervp); 292 lowervp = NULLVP; 293 } 294 return (uerror); 295 } 296 } 297 } 298 299 if (lowervp) 300 VOP_UNLOCK(lowervp); 301 302 error = union_allocvp(ap->a_vpp, dvp->v_mount, dvp, upperdvp, cnp, 303 uppervp, lowervp); 304 305 if (error) { 306 if (uppervp) 307 vput(uppervp); 308 if (lowervp) 309 vrele(lowervp); 310 } else { 311 if (*ap->a_vpp != dvp) 312 if (!lockparent || !(cnp->cn_flags & ISLASTCN)) 313 VOP_UNLOCK(dvp); 314 } 315 316 return (error); 317 } 318 319 int 320 union_create(ap) 321 struct vop_create_args /* { 322 struct vnode *a_dvp; 323 struct vnode **a_vpp; 324 struct componentname *a_cnp; 325 struct vattr *a_vap; 326 } */ *ap; 327 { 328 struct union_node *un = VTOUNION(ap->a_dvp); 329 struct vnode *dvp = un->un_uppervp; 330 331 if (dvp) { 332 int error; 333 struct vnode *vp; 334 335 FIXUP(un); 336 337 VREF(dvp); 338 un->un_flags |= UN_KLOCK; 339 vput(ap->a_dvp); 340 error = VOP_CREATE(dvp, &vp, ap->a_cnp, ap->a_vap); 341 if (error) 342 return (error); 343 344 error = union_allocvp( 345 ap->a_vpp, 346 ap->a_dvp->v_mount, 347 ap->a_dvp, 348 NULLVP, 349 ap->a_cnp, 350 vp, 351 NULLVP); 352 if (error) 353 vput(vp); 354 return (error); 355 } 356 357 vput(ap->a_dvp); 358 return (EROFS); 359 } 360 361 int 362 union_mknod(ap) 363 struct vop_mknod_args /* { 364 struct vnode *a_dvp; 365 struct vnode **a_vpp; 366 struct componentname *a_cnp; 367 struct vattr *a_vap; 368 } */ *ap; 369 { 370 struct union_node *un = VTOUNION(ap->a_dvp); 371 struct vnode *dvp = un->un_uppervp; 372 373 if (dvp) { 374 int error; 375 struct vnode *vp; 376 377 FIXUP(un); 378 379 VREF(dvp); 380 un->un_flags |= UN_KLOCK; 381 vput(ap->a_dvp); 382 error = VOP_MKNOD(dvp, &vp, ap->a_cnp, ap->a_vap); 383 if (error) 384 return (error); 385 386 if (vp) { 387 error = union_allocvp( 388 ap->a_vpp, 389 ap->a_dvp->v_mount, 390 ap->a_dvp, 391 NULLVP, 392 ap->a_cnp, 393 vp, 394 NULLVP); 395 if (error) 396 vput(vp); 397 } 398 return (error); 399 } 400 401 vput(ap->a_dvp); 402 return (EROFS); 403 } 404 405 int 406 union_open(ap) 407 struct vop_open_args /* { 408 struct vnodeop_desc *a_desc; 409 struct vnode *a_vp; 410 int a_mode; 411 struct ucred *a_cred; 412 struct proc *a_p; 413 } */ *ap; 414 { 415 struct union_node *un = VTOUNION(ap->a_vp); 416 struct vnode *tvp; 417 int mode = ap->a_mode; 418 struct ucred *cred = ap->a_cred; 419 struct proc *p = ap->a_p; 420 int error; 421 422 /* 423 * If there is an existing upper vp then simply open that. 424 */ 425 tvp = un->un_uppervp; 426 if (tvp == NULLVP) { 427 /* 428 * If the lower vnode is being opened for writing, then 429 * copy the file contents to the upper vnode and open that, 430 * otherwise can simply open the lower vnode. 431 */ 432 tvp = un->un_lowervp; 433 if ((ap->a_mode & FWRITE) && (tvp->v_type == VREG)) { 434 struct vnode *vp; 435 int i; 436 437 /* 438 * Open the named file in the upper layer. Note that 439 * the file may have come into existence *since* the 440 * lookup was done, since the upper layer may really 441 * be a loopback mount of some other filesystem... 442 * so open the file with exclusive create and barf if 443 * it already exists. 444 * XXX - perhaps should re-lookup the node (once more 445 * with feeling) and simply open that. Who knows. 446 */ 447 error = union_vn_create(&vp, un, p); 448 if (error) 449 return (error); 450 451 /* at this point, uppervp is locked */ 452 union_newupper(un, vp); 453 un->un_flags |= UN_ULOCK; 454 455 /* 456 * Now, if the file is being opened with truncation, 457 * then the (new) upper vnode is ready to fly, 458 * otherwise the data from the lower vnode must be 459 * copied to the upper layer first. This only works 460 * for regular files (check is made above). 461 */ 462 if ((mode & O_TRUNC) == 0) { 463 /* 464 * XXX - should not ignore errors 465 * from VOP_CLOSE 466 */ 467 VOP_LOCK(tvp); 468 error = VOP_OPEN(tvp, FREAD, cred, p); 469 if (error == 0) { 470 error = union_copyfile(p, cred, 471 tvp, un->un_uppervp); 472 VOP_UNLOCK(tvp); 473 (void) VOP_CLOSE(tvp, FREAD); 474 } else { 475 VOP_UNLOCK(tvp); 476 } 477 478 #ifdef UNION_DIAGNOSTIC 479 if (!error) 480 uprintf("union: copied up %s\n", 481 un->un_path); 482 #endif 483 } 484 485 un->un_flags &= ~UN_ULOCK; 486 VOP_UNLOCK(un->un_uppervp); 487 union_vn_close(un->un_uppervp, FWRITE, cred, p); 488 VOP_LOCK(un->un_uppervp); 489 un->un_flags |= UN_ULOCK; 490 491 /* 492 * Subsequent IOs will go to the top layer, so 493 * call close on the lower vnode and open on the 494 * upper vnode to ensure that the filesystem keeps 495 * its references counts right. This doesn't do 496 * the right thing with (cred) and (FREAD) though. 497 * Ignoring error returns is not righ, either. 498 */ 499 for (i = 0; i < un->un_openl; i++) { 500 (void) VOP_CLOSE(tvp, FREAD); 501 (void) VOP_OPEN(un->un_uppervp, FREAD, cred, p); 502 } 503 un->un_openl = 0; 504 505 if (error == 0) 506 error = VOP_OPEN(un->un_uppervp, mode, cred, p); 507 return (error); 508 } 509 510 /* 511 * Just open the lower vnode 512 */ 513 un->un_openl++; 514 VOP_LOCK(tvp); 515 error = VOP_OPEN(tvp, mode, cred, p); 516 VOP_UNLOCK(tvp); 517 518 return (error); 519 } 520 521 FIXUP(un); 522 523 error = VOP_OPEN(tvp, mode, cred, p); 524 525 return (error); 526 } 527 528 int 529 union_close(ap) 530 struct vop_close_args /* { 531 struct vnode *a_vp; 532 int a_fflag; 533 struct ucred *a_cred; 534 struct proc *a_p; 535 } */ *ap; 536 { 537 struct union_node *un = VTOUNION(ap->a_vp); 538 struct vnode *vp; 539 540 if (un->un_uppervp) { 541 vp = un->un_uppervp; 542 } else { 543 #ifdef UNION_DIAGNOSTIC 544 if (un->un_openl <= 0) 545 panic("union: un_openl cnt"); 546 #endif 547 --un->un_openl; 548 vp = un->un_lowervp; 549 } 550 551 return (VOP_CLOSE(vp, ap->a_fflag, ap->a_cred, ap->a_p)); 552 } 553 554 /* 555 * Check access permission on the union vnode. 556 * The access check being enforced is to check 557 * against both the underlying vnode, and any 558 * copied vnode. This ensures that no additional 559 * file permissions are given away simply because 560 * the user caused an implicit file copy. 561 */ 562 int 563 union_access(ap) 564 struct vop_access_args /* { 565 struct vnodeop_desc *a_desc; 566 struct vnode *a_vp; 567 int a_mode; 568 struct ucred *a_cred; 569 struct proc *a_p; 570 } */ *ap; 571 { 572 struct union_node *un = VTOUNION(ap->a_vp); 573 int error = EACCES; 574 struct vnode *vp; 575 576 if (vp = un->un_uppervp) { 577 FIXUP(un); 578 return (VOP_ACCESS(vp, ap->a_mode, ap->a_cred, ap->a_p)); 579 } 580 581 if (vp = un->un_lowervp) { 582 VOP_LOCK(vp); 583 error = VOP_ACCESS(vp, ap->a_mode, ap->a_cred, ap->a_p); 584 if (error == 0) { 585 struct union_mount *um = MOUNTTOUNIONMOUNT(vp->v_mount); 586 587 if (um->um_op == UNMNT_BELOW) 588 error = VOP_ACCESS(vp, ap->a_mode, 589 um->um_cred, ap->a_p); 590 } 591 VOP_UNLOCK(vp); 592 if (error) 593 return (error); 594 } 595 596 return (error); 597 } 598 599 /* 600 * We handle getattr only to change the fsid. 601 */ 602 int 603 union_getattr(ap) 604 struct vop_getattr_args /* { 605 struct vnode *a_vp; 606 struct vattr *a_vap; 607 struct ucred *a_cred; 608 struct proc *a_p; 609 } */ *ap; 610 { 611 int error; 612 struct union_node *un = VTOUNION(ap->a_vp); 613 struct vnode *vp = un->un_uppervp; 614 struct vattr *vap; 615 struct vattr va; 616 617 618 /* 619 * Some programs walk the filesystem hierarchy by counting 620 * links to directories to avoid stat'ing all the time. 621 * This means the link count on directories needs to be "correct". 622 * The only way to do that is to call getattr on both layers 623 * and fix up the link count. The link count will not necessarily 624 * be accurate but will be large enough to defeat the tree walkers. 625 */ 626 627 vap = ap->a_vap; 628 629 vp = un->un_uppervp; 630 if (vp != NULLVP) { 631 FIXUP(un); 632 error = VOP_GETATTR(vp, vap, ap->a_cred, ap->a_p); 633 if (error) 634 return (error); 635 } 636 637 if (vp == NULLVP) { 638 vp = un->un_lowervp; 639 } else if (vp->v_type == VDIR) { 640 vp = un->un_lowervp; 641 vap = &va; 642 } else { 643 vp = NULLVP; 644 } 645 646 if (vp != NULLVP) { 647 VOP_LOCK(vp); 648 error = VOP_GETATTR(vp, vap, ap->a_cred, ap->a_p); 649 VOP_UNLOCK(vp); 650 if (error) 651 return (error); 652 } 653 654 if ((vap != ap->a_vap) && (vap->va_type == VDIR)) 655 ap->a_vap->va_nlink += vap->va_nlink; 656 657 vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsid.val[0]; 658 return (0); 659 } 660 661 int 662 union_setattr(ap) 663 struct vop_setattr_args /* { 664 struct vnode *a_vp; 665 struct vattr *a_vap; 666 struct ucred *a_cred; 667 struct proc *a_p; 668 } */ *ap; 669 { 670 struct union_node *un = VTOUNION(ap->a_vp); 671 int error; 672 673 /* 674 * Handle case of truncating lower object to zero size, 675 * by creating a zero length upper object. This is to 676 * handle the case of open with O_TRUNC and O_CREAT. 677 */ 678 if ((un->un_uppervp == NULLVP) && 679 /* assert(un->un_lowervp != NULLVP) */ 680 (un->un_lowervp->v_type == VREG) && 681 (ap->a_vap->va_size == 0)) { 682 struct vnode *vp; 683 684 error = union_vn_create(&vp, un, ap->a_p); 685 if (error) 686 return (error); 687 688 /* at this point, uppervp is locked */ 689 union_newupper(un, vp); 690 691 VOP_UNLOCK(vp); 692 union_vn_close(un->un_uppervp, FWRITE, ap->a_cred, ap->a_p); 693 VOP_LOCK(vp); 694 un->un_flags |= UN_ULOCK; 695 } 696 697 /* 698 * Try to set attributes in upper layer, 699 * otherwise return read-only filesystem error. 700 */ 701 if (un->un_uppervp != NULLVP) { 702 FIXUP(un); 703 error = VOP_SETATTR(un->un_uppervp, ap->a_vap, 704 ap->a_cred, ap->a_p); 705 } else { 706 error = EROFS; 707 } 708 709 return (error); 710 } 711 712 int 713 union_read(ap) 714 struct vop_read_args /* { 715 struct vnode *a_vp; 716 struct uio *a_uio; 717 int a_ioflag; 718 struct ucred *a_cred; 719 } */ *ap; 720 { 721 int error; 722 struct vnode *vp = OTHERVP(ap->a_vp); 723 int dolock = (vp == LOWERVP(ap->a_vp)); 724 725 if (dolock) 726 VOP_LOCK(vp); 727 else 728 FIXUP(VTOUNION(ap->a_vp)); 729 error = VOP_READ(vp, ap->a_uio, ap->a_ioflag, ap->a_cred); 730 if (dolock) 731 VOP_UNLOCK(vp); 732 733 return (error); 734 } 735 736 int 737 union_write(ap) 738 struct vop_read_args /* { 739 struct vnode *a_vp; 740 struct uio *a_uio; 741 int a_ioflag; 742 struct ucred *a_cred; 743 } */ *ap; 744 { 745 int error; 746 struct vnode *vp = OTHERVP(ap->a_vp); 747 int dolock = (vp == LOWERVP(ap->a_vp)); 748 749 if (dolock) 750 VOP_LOCK(vp); 751 else 752 FIXUP(VTOUNION(ap->a_vp)); 753 error = VOP_WRITE(vp, ap->a_uio, ap->a_ioflag, ap->a_cred); 754 if (dolock) 755 VOP_UNLOCK(vp); 756 757 return (error); 758 } 759 760 int 761 union_ioctl(ap) 762 struct vop_ioctl_args /* { 763 struct vnode *a_vp; 764 int a_command; 765 caddr_t a_data; 766 int a_fflag; 767 struct ucred *a_cred; 768 struct proc *a_p; 769 } */ *ap; 770 { 771 772 return (VOP_IOCTL(OTHERVP(ap->a_vp), ap->a_command, ap->a_data, 773 ap->a_fflag, ap->a_cred, ap->a_p)); 774 } 775 776 int 777 union_select(ap) 778 struct vop_select_args /* { 779 struct vnode *a_vp; 780 int a_which; 781 int a_fflags; 782 struct ucred *a_cred; 783 struct proc *a_p; 784 } */ *ap; 785 { 786 787 return (VOP_SELECT(OTHERVP(ap->a_vp), ap->a_which, ap->a_fflags, 788 ap->a_cred, ap->a_p)); 789 } 790 791 int 792 union_mmap(ap) 793 struct vop_mmap_args /* { 794 struct vnode *a_vp; 795 int a_fflags; 796 struct ucred *a_cred; 797 struct proc *a_p; 798 } */ *ap; 799 { 800 801 return (VOP_MMAP(OTHERVP(ap->a_vp), ap->a_fflags, 802 ap->a_cred, ap->a_p)); 803 } 804 805 int 806 union_fsync(ap) 807 struct vop_fsync_args /* { 808 struct vnode *a_vp; 809 struct ucred *a_cred; 810 int a_waitfor; 811 struct proc *a_p; 812 } */ *ap; 813 { 814 int error = 0; 815 struct vnode *targetvp = OTHERVP(ap->a_vp); 816 817 if (targetvp) { 818 int dolock = (targetvp == LOWERVP(ap->a_vp)); 819 820 if (dolock) 821 VOP_LOCK(targetvp); 822 else 823 FIXUP(VTOUNION(ap->a_vp)); 824 error = VOP_FSYNC(targetvp, ap->a_cred, 825 ap->a_waitfor, ap->a_p); 826 if (dolock) 827 VOP_UNLOCK(targetvp); 828 } 829 830 return (error); 831 } 832 833 int 834 union_seek(ap) 835 struct vop_seek_args /* { 836 struct vnode *a_vp; 837 off_t a_oldoff; 838 off_t a_newoff; 839 struct ucred *a_cred; 840 } */ *ap; 841 { 842 843 return (VOP_SEEK(OTHERVP(ap->a_vp), ap->a_oldoff, ap->a_newoff, ap->a_cred)); 844 } 845 846 int 847 union_remove(ap) 848 struct vop_remove_args /* { 849 struct vnode *a_dvp; 850 struct vnode *a_vp; 851 struct componentname *a_cnp; 852 } */ *ap; 853 { 854 int error; 855 struct union_node *dun = VTOUNION(ap->a_dvp); 856 struct union_node *un = VTOUNION(ap->a_vp); 857 858 if (dun->un_uppervp && un->un_uppervp) { 859 struct vnode *dvp = dun->un_uppervp; 860 struct vnode *vp = un->un_uppervp; 861 862 FIXUP(dun); 863 VREF(dvp); 864 dun->un_flags |= UN_KLOCK; 865 vput(ap->a_dvp); 866 FIXUP(un); 867 VREF(vp); 868 un->un_flags |= UN_KLOCK; 869 vput(ap->a_vp); 870 871 error = VOP_REMOVE(dvp, vp, ap->a_cnp); 872 if (!error) 873 union_removed_upper(un); 874 875 /* 876 * XXX: should create a whiteout here 877 */ 878 } else { 879 /* 880 * XXX: should create a whiteout here 881 */ 882 vput(ap->a_dvp); 883 vput(ap->a_vp); 884 error = EROFS; 885 } 886 887 return (error); 888 } 889 890 int 891 union_link(ap) 892 struct vop_link_args /* { 893 struct vnode *a_vp; 894 struct vnode *a_tdvp; 895 struct componentname *a_cnp; 896 } */ *ap; 897 { 898 int error; 899 struct union_node *dun = VTOUNION(ap->a_vp); 900 struct union_node *un = VTOUNION(ap->a_tdvp); 901 902 if (dun->un_uppervp && un->un_uppervp) { 903 struct vnode *dvp = dun->un_uppervp; 904 struct vnode *vp = un->un_uppervp; 905 906 FIXUP(dun); 907 VREF(dvp); 908 dun->un_flags |= UN_KLOCK; 909 vput(ap->a_vp); 910 FIXUP(un); 911 VREF(vp); 912 vrele(ap->a_tdvp); 913 914 error = VOP_LINK(dvp, vp, ap->a_cnp); 915 } else { 916 /* 917 * XXX: need to copy to upper layer 918 * and do the link there. 919 */ 920 vput(ap->a_vp); 921 vrele(ap->a_tdvp); 922 error = EROFS; 923 } 924 925 return (error); 926 } 927 928 int 929 union_rename(ap) 930 struct vop_rename_args /* { 931 struct vnode *a_fdvp; 932 struct vnode *a_fvp; 933 struct componentname *a_fcnp; 934 struct vnode *a_tdvp; 935 struct vnode *a_tvp; 936 struct componentname *a_tcnp; 937 } */ *ap; 938 { 939 int error; 940 941 struct vnode *fdvp = ap->a_fdvp; 942 struct vnode *fvp = ap->a_fvp; 943 struct vnode *tdvp = ap->a_tdvp; 944 struct vnode *tvp = ap->a_tvp; 945 946 if (fdvp->v_op == union_vnodeop_p) { /* always true */ 947 struct union_node *un = VTOUNION(fdvp); 948 if (un->un_uppervp == NULLVP) { 949 error = EROFS; 950 goto bad; 951 } 952 953 FIXUP(un); 954 fdvp = un->un_uppervp; 955 VREF(fdvp); 956 vrele(ap->a_fdvp); 957 } 958 959 if (fvp->v_op == union_vnodeop_p) { /* always true */ 960 struct union_node *un = VTOUNION(fvp); 961 if (un->un_uppervp == NULLVP) { 962 error = EROFS; 963 goto bad; 964 } 965 966 FIXUP(un); 967 fvp = un->un_uppervp; 968 VREF(fvp); 969 vrele(ap->a_fvp); 970 } 971 972 if (tdvp->v_op == union_vnodeop_p) { 973 struct union_node *un = VTOUNION(tdvp); 974 if (un->un_uppervp == NULLVP) { 975 error = EROFS; 976 goto bad; 977 } 978 979 tdvp = un->un_uppervp; 980 VREF(tdvp); 981 un->un_flags |= UN_KLOCK; 982 vput(ap->a_tdvp); 983 } 984 985 if (tvp && tvp->v_op == union_vnodeop_p) { 986 struct union_node *un = VTOUNION(tvp); 987 if (un->un_uppervp == NULLVP) { 988 error = EROFS; 989 goto bad; 990 } 991 992 tvp = un->un_uppervp; 993 VREF(tvp); 994 un->un_flags |= UN_KLOCK; 995 vput(ap->a_tvp); 996 } 997 998 return (VOP_RENAME(fdvp, fvp, ap->a_fcnp, tdvp, tvp, ap->a_tcnp)); 999 1000 bad: 1001 vrele(fdvp); 1002 vrele(fvp); 1003 vput(tdvp); 1004 if (tvp) 1005 vput(tvp); 1006 1007 return (error); 1008 } 1009 1010 int 1011 union_mkdir(ap) 1012 struct vop_mkdir_args /* { 1013 struct vnode *a_dvp; 1014 struct vnode **a_vpp; 1015 struct componentname *a_cnp; 1016 struct vattr *a_vap; 1017 } */ *ap; 1018 { 1019 struct union_node *un = VTOUNION(ap->a_dvp); 1020 struct vnode *dvp = un->un_uppervp; 1021 1022 if (dvp) { 1023 int error; 1024 struct vnode *vp; 1025 1026 FIXUP(un); 1027 VREF(dvp); 1028 un->un_flags |= UN_KLOCK; 1029 vput(ap->a_dvp); 1030 error = VOP_MKDIR(dvp, &vp, ap->a_cnp, ap->a_vap); 1031 if (error) 1032 return (error); 1033 1034 error = union_allocvp( 1035 ap->a_vpp, 1036 ap->a_dvp->v_mount, 1037 ap->a_dvp, 1038 NULLVP, 1039 ap->a_cnp, 1040 vp, 1041 NULLVP); 1042 if (error) 1043 vput(vp); 1044 return (error); 1045 } 1046 1047 vput(ap->a_dvp); 1048 return (EROFS); 1049 } 1050 1051 int 1052 union_rmdir(ap) 1053 struct vop_rmdir_args /* { 1054 struct vnode *a_dvp; 1055 struct vnode *a_vp; 1056 struct componentname *a_cnp; 1057 } */ *ap; 1058 { 1059 int error; 1060 struct union_node *dun = VTOUNION(ap->a_dvp); 1061 struct union_node *un = VTOUNION(ap->a_vp); 1062 1063 if (dun->un_uppervp && un->un_uppervp) { 1064 struct vnode *dvp = dun->un_uppervp; 1065 struct vnode *vp = un->un_uppervp; 1066 1067 FIXUP(dun); 1068 VREF(dvp); 1069 dun->un_flags |= UN_KLOCK; 1070 vput(ap->a_dvp); 1071 FIXUP(un); 1072 VREF(vp); 1073 un->un_flags |= UN_KLOCK; 1074 vput(ap->a_vp); 1075 1076 error = VOP_RMDIR(dvp, vp, ap->a_cnp); 1077 if (!error) 1078 union_removed_upper(un); 1079 1080 /* 1081 * XXX: should create a whiteout here 1082 */ 1083 } else { 1084 /* 1085 * XXX: should create a whiteout here 1086 */ 1087 vput(ap->a_dvp); 1088 vput(ap->a_vp); 1089 error = EROFS; 1090 } 1091 1092 return (error); 1093 } 1094 1095 int 1096 union_symlink(ap) 1097 struct vop_symlink_args /* { 1098 struct vnode *a_dvp; 1099 struct vnode **a_vpp; 1100 struct componentname *a_cnp; 1101 struct vattr *a_vap; 1102 char *a_target; 1103 } */ *ap; 1104 { 1105 struct union_node *un = VTOUNION(ap->a_dvp); 1106 struct vnode *dvp = un->un_uppervp; 1107 1108 if (dvp) { 1109 int error; 1110 struct vnode *vp; 1111 struct mount *mp = ap->a_dvp->v_mount; 1112 1113 FIXUP(un); 1114 VREF(dvp); 1115 un->un_flags |= UN_KLOCK; 1116 vput(ap->a_dvp); 1117 error = VOP_SYMLINK(dvp, &vp, ap->a_cnp, 1118 ap->a_vap, ap->a_target); 1119 *ap->a_vpp = NULLVP; 1120 return (error); 1121 } 1122 1123 vput(ap->a_dvp); 1124 return (EROFS); 1125 } 1126 1127 /* 1128 * union_readdir works in concert with getdirentries and 1129 * readdir(3) to provide a list of entries in the unioned 1130 * directories. getdirentries is responsible for walking 1131 * down the union stack. readdir(3) is responsible for 1132 * eliminating duplicate names from the returned data stream. 1133 */ 1134 int 1135 union_readdir(ap) 1136 struct vop_readdir_args /* { 1137 struct vnodeop_desc *a_desc; 1138 struct vnode *a_vp; 1139 struct uio *a_uio; 1140 struct ucred *a_cred; 1141 } */ *ap; 1142 { 1143 int error = 0; 1144 struct union_node *un = VTOUNION(ap->a_vp); 1145 1146 if (un->un_uppervp) { 1147 FIXUP(un); 1148 error = VOP_READDIR(un->un_uppervp, ap->a_uio, ap->a_cred); 1149 } 1150 1151 return (error); 1152 } 1153 1154 int 1155 union_readlink(ap) 1156 struct vop_readlink_args /* { 1157 struct vnode *a_vp; 1158 struct uio *a_uio; 1159 struct ucred *a_cred; 1160 } */ *ap; 1161 { 1162 int error; 1163 struct vnode *vp = OTHERVP(ap->a_vp); 1164 int dolock = (vp == LOWERVP(ap->a_vp)); 1165 1166 if (dolock) 1167 VOP_LOCK(vp); 1168 else 1169 FIXUP(VTOUNION(ap->a_vp)); 1170 error = VOP_READLINK(vp, ap->a_uio, ap->a_cred); 1171 if (dolock) 1172 VOP_UNLOCK(vp); 1173 1174 return (error); 1175 } 1176 1177 int 1178 union_abortop(ap) 1179 struct vop_abortop_args /* { 1180 struct vnode *a_dvp; 1181 struct componentname *a_cnp; 1182 } */ *ap; 1183 { 1184 int error; 1185 struct vnode *vp = OTHERVP(ap->a_dvp); 1186 struct union_node *un = VTOUNION(ap->a_dvp); 1187 int islocked = un->un_flags & UN_LOCKED; 1188 int dolock = (vp == LOWERVP(ap->a_dvp)); 1189 1190 if (islocked) { 1191 if (dolock) 1192 VOP_LOCK(vp); 1193 else 1194 FIXUP(VTOUNION(ap->a_dvp)); 1195 } 1196 error = VOP_ABORTOP(vp, ap->a_cnp); 1197 if (islocked && dolock) 1198 VOP_UNLOCK(vp); 1199 1200 return (error); 1201 } 1202 1203 int 1204 union_inactive(ap) 1205 struct vop_inactive_args /* { 1206 struct vnode *a_vp; 1207 } */ *ap; 1208 { 1209 1210 /* 1211 * Do nothing (and _don't_ bypass). 1212 * Wait to vrele lowervp until reclaim, 1213 * so that until then our union_node is in the 1214 * cache and reusable. 1215 * 1216 * NEEDSWORK: Someday, consider inactive'ing 1217 * the lowervp and then trying to reactivate it 1218 * with capabilities (v_id) 1219 * like they do in the name lookup cache code. 1220 * That's too much work for now. 1221 */ 1222 1223 #ifdef UNION_DIAGNOSTIC 1224 struct union_node *un = VTOUNION(ap->a_vp); 1225 1226 if (un->un_flags & UN_LOCKED) 1227 panic("union: inactivating locked node"); 1228 #endif 1229 1230 return (0); 1231 } 1232 1233 int 1234 union_reclaim(ap) 1235 struct vop_reclaim_args /* { 1236 struct vnode *a_vp; 1237 } */ *ap; 1238 { 1239 1240 union_freevp(ap->a_vp); 1241 1242 return (0); 1243 } 1244 1245 int 1246 union_lock(ap) 1247 struct vop_lock_args *ap; 1248 { 1249 struct vnode *vp = ap->a_vp; 1250 struct union_node *un; 1251 1252 start: 1253 while (vp->v_flag & VXLOCK) { 1254 vp->v_flag |= VXWANT; 1255 sleep((caddr_t)vp, PINOD); 1256 } 1257 1258 un = VTOUNION(vp); 1259 1260 if (un->un_uppervp) { 1261 if ((un->un_flags & UN_ULOCK) == 0) { 1262 un->un_flags |= UN_ULOCK; 1263 VOP_LOCK(un->un_uppervp); 1264 } 1265 #ifdef DIAGNOSTIC 1266 if (un->un_flags & UN_KLOCK) 1267 panic("union: dangling upper lock"); 1268 #endif 1269 } 1270 1271 if (un->un_flags & UN_LOCKED) { 1272 #ifdef DIAGNOSTIC 1273 if (curproc && un->un_pid == curproc->p_pid && 1274 un->un_pid > -1 && curproc->p_pid > -1) 1275 panic("union: locking against myself"); 1276 #endif 1277 un->un_flags |= UN_WANT; 1278 sleep((caddr_t) &un->un_flags, PINOD); 1279 goto start; 1280 } 1281 1282 #ifdef DIAGNOSTIC 1283 if (curproc) 1284 un->un_pid = curproc->p_pid; 1285 else 1286 un->un_pid = -1; 1287 #endif 1288 1289 un->un_flags |= UN_LOCKED; 1290 return (0); 1291 } 1292 1293 int 1294 union_unlock(ap) 1295 struct vop_lock_args *ap; 1296 { 1297 struct union_node *un = VTOUNION(ap->a_vp); 1298 1299 #ifdef DIAGNOSTIC 1300 if ((un->un_flags & UN_LOCKED) == 0) 1301 panic("union: unlock unlocked node"); 1302 if (curproc && un->un_pid != curproc->p_pid && 1303 curproc->p_pid > -1 && un->un_pid > -1) 1304 panic("union: unlocking other process's union node"); 1305 #endif 1306 1307 un->un_flags &= ~UN_LOCKED; 1308 1309 if ((un->un_flags & (UN_ULOCK|UN_KLOCK)) == UN_ULOCK) 1310 VOP_UNLOCK(un->un_uppervp); 1311 1312 un->un_flags &= ~(UN_ULOCK|UN_KLOCK); 1313 1314 if (un->un_flags & UN_WANT) { 1315 un->un_flags &= ~UN_WANT; 1316 wakeup((caddr_t) &un->un_flags); 1317 } 1318 1319 #ifdef DIAGNOSTIC 1320 un->un_pid = 0; 1321 #endif 1322 1323 return (0); 1324 } 1325 1326 int 1327 union_bmap(ap) 1328 struct vop_bmap_args /* { 1329 struct vnode *a_vp; 1330 daddr_t a_bn; 1331 struct vnode **a_vpp; 1332 daddr_t *a_bnp; 1333 int *a_runp; 1334 } */ *ap; 1335 { 1336 int error; 1337 struct vnode *vp = OTHERVP(ap->a_vp); 1338 int dolock = (vp == LOWERVP(ap->a_vp)); 1339 1340 if (dolock) 1341 VOP_LOCK(vp); 1342 else 1343 FIXUP(VTOUNION(ap->a_vp)); 1344 error = VOP_BMAP(vp, ap->a_bn, ap->a_vpp, ap->a_bnp, ap->a_runp); 1345 if (dolock) 1346 VOP_UNLOCK(vp); 1347 1348 return (error); 1349 } 1350 1351 int 1352 union_print(ap) 1353 struct vop_print_args /* { 1354 struct vnode *a_vp; 1355 } */ *ap; 1356 { 1357 struct vnode *vp = ap->a_vp; 1358 1359 printf("\ttag VT_UNION, vp=%x, uppervp=%x, lowervp=%x\n", 1360 vp, UPPERVP(vp), LOWERVP(vp)); 1361 return (0); 1362 } 1363 1364 int 1365 union_islocked(ap) 1366 struct vop_islocked_args /* { 1367 struct vnode *a_vp; 1368 } */ *ap; 1369 { 1370 1371 return ((VTOUNION(ap->a_vp)->un_flags & UN_LOCKED) ? 1 : 0); 1372 } 1373 1374 int 1375 union_pathconf(ap) 1376 struct vop_pathconf_args /* { 1377 struct vnode *a_vp; 1378 int a_name; 1379 int *a_retval; 1380 } */ *ap; 1381 { 1382 int error; 1383 struct vnode *vp = OTHERVP(ap->a_vp); 1384 int dolock = (vp == LOWERVP(ap->a_vp)); 1385 1386 if (dolock) 1387 VOP_LOCK(vp); 1388 else 1389 FIXUP(VTOUNION(ap->a_vp)); 1390 error = VOP_PATHCONF(vp, ap->a_name, ap->a_retval); 1391 if (dolock) 1392 VOP_UNLOCK(vp); 1393 1394 return (error); 1395 } 1396 1397 int 1398 union_advlock(ap) 1399 struct vop_advlock_args /* { 1400 struct vnode *a_vp; 1401 caddr_t a_id; 1402 int a_op; 1403 struct flock *a_fl; 1404 int a_flags; 1405 } */ *ap; 1406 { 1407 1408 return (VOP_ADVLOCK(OTHERVP(ap->a_vp), ap->a_id, ap->a_op, 1409 ap->a_fl, ap->a_flags)); 1410 } 1411 1412 1413 /* 1414 * XXX - vop_strategy must be hand coded because it has no 1415 * vnode in its arguments. 1416 * This goes away with a merged VM/buffer cache. 1417 */ 1418 int 1419 union_strategy(ap) 1420 struct vop_strategy_args /* { 1421 struct buf *a_bp; 1422 } */ *ap; 1423 { 1424 struct buf *bp = ap->a_bp; 1425 int error; 1426 struct vnode *savedvp; 1427 1428 savedvp = bp->b_vp; 1429 bp->b_vp = OTHERVP(bp->b_vp); 1430 1431 #ifdef DIAGNOSTIC 1432 if (bp->b_vp == NULLVP) 1433 panic("union_strategy: nil vp"); 1434 if (((bp->b_flags & B_READ) == 0) && 1435 (bp->b_vp == LOWERVP(savedvp))) 1436 panic("union_strategy: writing to lowervp"); 1437 #endif 1438 1439 error = VOP_STRATEGY(bp); 1440 bp->b_vp = savedvp; 1441 1442 return (error); 1443 } 1444 1445 /* 1446 * Global vfs data structures 1447 */ 1448 int (**union_vnodeop_p)(); 1449 struct vnodeopv_entry_desc union_vnodeop_entries[] = { 1450 { &vop_default_desc, vn_default_error }, 1451 { &vop_lookup_desc, union_lookup }, /* lookup */ 1452 { &vop_create_desc, union_create }, /* create */ 1453 { &vop_mknod_desc, union_mknod }, /* mknod */ 1454 { &vop_open_desc, union_open }, /* open */ 1455 { &vop_close_desc, union_close }, /* close */ 1456 { &vop_access_desc, union_access }, /* access */ 1457 { &vop_getattr_desc, union_getattr }, /* getattr */ 1458 { &vop_setattr_desc, union_setattr }, /* setattr */ 1459 { &vop_read_desc, union_read }, /* read */ 1460 { &vop_write_desc, union_write }, /* write */ 1461 { &vop_ioctl_desc, union_ioctl }, /* ioctl */ 1462 { &vop_select_desc, union_select }, /* select */ 1463 { &vop_mmap_desc, union_mmap }, /* mmap */ 1464 { &vop_fsync_desc, union_fsync }, /* fsync */ 1465 { &vop_seek_desc, union_seek }, /* seek */ 1466 { &vop_remove_desc, union_remove }, /* remove */ 1467 { &vop_link_desc, union_link }, /* link */ 1468 { &vop_rename_desc, union_rename }, /* rename */ 1469 { &vop_mkdir_desc, union_mkdir }, /* mkdir */ 1470 { &vop_rmdir_desc, union_rmdir }, /* rmdir */ 1471 { &vop_symlink_desc, union_symlink }, /* symlink */ 1472 { &vop_readdir_desc, union_readdir }, /* readdir */ 1473 { &vop_readlink_desc, union_readlink }, /* readlink */ 1474 { &vop_abortop_desc, union_abortop }, /* abortop */ 1475 { &vop_inactive_desc, union_inactive }, /* inactive */ 1476 { &vop_reclaim_desc, union_reclaim }, /* reclaim */ 1477 { &vop_lock_desc, union_lock }, /* lock */ 1478 { &vop_unlock_desc, union_unlock }, /* unlock */ 1479 { &vop_bmap_desc, union_bmap }, /* bmap */ 1480 { &vop_strategy_desc, union_strategy }, /* strategy */ 1481 { &vop_print_desc, union_print }, /* print */ 1482 { &vop_islocked_desc, union_islocked }, /* islocked */ 1483 { &vop_pathconf_desc, union_pathconf }, /* pathconf */ 1484 { &vop_advlock_desc, union_advlock }, /* advlock */ 1485 #ifdef notdef 1486 { &vop_blkatoff_desc, union_blkatoff }, /* blkatoff */ 1487 { &vop_valloc_desc, union_valloc }, /* valloc */ 1488 { &vop_vfree_desc, union_vfree }, /* vfree */ 1489 { &vop_truncate_desc, union_truncate }, /* truncate */ 1490 { &vop_update_desc, union_update }, /* update */ 1491 { &vop_bwrite_desc, union_bwrite }, /* bwrite */ 1492 #endif 1493 { (struct vnodeop_desc*)NULL, (int(*)())NULL } 1494 }; 1495 struct vnodeopv_desc union_vnodeop_opv_desc = 1496 { &union_vnodeop_p, union_vnodeop_entries }; 1497