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