1 /* 2 * Copyright (c) 1994 Jan-Simon Pendry 3 * Copyright (c) 1994 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 * %sccs.include.redist.c% 10 * 11 * @(#)union_subr.c 8.10 (Berkeley) 06/16/94 12 */ 13 14 #include <sys/param.h> 15 #include <sys/systm.h> 16 #include <sys/time.h> 17 #include <sys/kernel.h> 18 #include <sys/vnode.h> 19 #include <sys/namei.h> 20 #include <sys/malloc.h> 21 #include <sys/file.h> 22 #include <sys/filedesc.h> 23 #include <sys/queue.h> 24 #include <sys/mount.h> 25 #include <vm/vm.h> /* for vnode_pager_setsize */ 26 #include <miscfs/union/union.h> 27 28 #ifdef DIAGNOSTIC 29 #include <sys/proc.h> 30 #endif 31 32 /* must be power of two, otherwise change UNION_HASH() */ 33 #define NHASH 32 34 35 /* unsigned int ... */ 36 #define UNION_HASH(u, l) \ 37 (((((unsigned long) (u)) + ((unsigned long) l)) >> 8) & (NHASH-1)) 38 39 static LIST_HEAD(unhead, union_node) unhead[NHASH]; 40 static int unvplock[NHASH]; 41 42 int 43 union_init() 44 { 45 int i; 46 47 for (i = 0; i < NHASH; i++) 48 LIST_INIT(&unhead[i]); 49 bzero((caddr_t) unvplock, sizeof(unvplock)); 50 } 51 52 static int 53 union_list_lock(ix) 54 int ix; 55 { 56 57 if (unvplock[ix] & UN_LOCKED) { 58 unvplock[ix] |= UN_WANT; 59 sleep((caddr_t) &unvplock[ix], PINOD); 60 return (1); 61 } 62 63 unvplock[ix] |= UN_LOCKED; 64 65 return (0); 66 } 67 68 static void 69 union_list_unlock(ix) 70 int ix; 71 { 72 73 unvplock[ix] &= ~UN_LOCKED; 74 75 if (unvplock[ix] & UN_WANT) { 76 unvplock[ix] &= ~UN_WANT; 77 wakeup((caddr_t) &unvplock[ix]); 78 } 79 } 80 81 void 82 union_updatevp(un, uppervp, lowervp) 83 struct union_node *un; 84 struct vnode *uppervp; 85 struct vnode *lowervp; 86 { 87 int ohash = UNION_HASH(un->un_uppervp, un->un_lowervp); 88 int nhash = UNION_HASH(uppervp, lowervp); 89 int docache = (lowervp != NULLVP || uppervp != NULLVP); 90 91 /* 92 * Ensure locking is ordered from lower to higher 93 * to avoid deadlocks. 94 */ 95 if (nhash < ohash) { 96 int t = ohash; 97 ohash = nhash; 98 nhash = t; 99 } 100 101 if (ohash != nhash) 102 while (union_list_lock(ohash)) 103 continue; 104 105 while (union_list_lock(nhash)) 106 continue; 107 108 if (ohash != nhash || !docache) { 109 if (un->un_flags & UN_CACHED) { 110 un->un_flags &= ~UN_CACHED; 111 LIST_REMOVE(un, un_cache); 112 } 113 } 114 115 if (ohash != nhash) 116 union_list_unlock(ohash); 117 118 if (un->un_lowervp != lowervp) { 119 if (un->un_lowervp) { 120 vrele(un->un_lowervp); 121 if (un->un_path) { 122 free(un->un_path, M_TEMP); 123 un->un_path = 0; 124 } 125 if (un->un_dirvp) { 126 vrele(un->un_dirvp); 127 un->un_dirvp = NULLVP; 128 } 129 } 130 un->un_lowervp = lowervp; 131 un->un_lowersz = VNOVAL; 132 } 133 134 if (un->un_uppervp != uppervp) { 135 if (un->un_uppervp) 136 vrele(un->un_uppervp); 137 138 un->un_uppervp = uppervp; 139 un->un_uppersz = VNOVAL; 140 } 141 142 if (docache && (ohash != nhash)) { 143 LIST_INSERT_HEAD(&unhead[nhash], un, un_cache); 144 un->un_flags |= UN_CACHED; 145 } 146 147 union_list_unlock(nhash); 148 } 149 150 void 151 union_newlower(un, lowervp) 152 struct union_node *un; 153 struct vnode *lowervp; 154 { 155 156 union_updatevp(un, un->un_uppervp, lowervp); 157 } 158 159 void 160 union_newupper(un, uppervp) 161 struct union_node *un; 162 struct vnode *uppervp; 163 { 164 165 union_updatevp(un, uppervp, un->un_lowervp); 166 } 167 168 /* 169 * Keep track of size changes in the underlying vnodes. 170 * If the size changes, then callback to the vm layer 171 * giving priority to the upper layer size. 172 */ 173 void 174 union_newsize(vp, uppersz, lowersz) 175 struct vnode *vp; 176 off_t uppersz, lowersz; 177 { 178 struct union_node *un; 179 off_t sz; 180 181 /* only interested in regular files */ 182 if (vp->v_type != VREG) 183 return; 184 185 un = VTOUNION(vp); 186 sz = VNOVAL; 187 188 if ((uppersz != VNOVAL) && (un->un_uppersz != uppersz)) { 189 un->un_uppersz = uppersz; 190 if (sz == VNOVAL) 191 sz = un->un_uppersz; 192 } 193 194 if ((lowersz != VNOVAL) && (un->un_lowersz != lowersz)) { 195 un->un_lowersz = lowersz; 196 if (sz == VNOVAL) 197 sz = un->un_lowersz; 198 } 199 200 if (sz != VNOVAL) { 201 #ifdef UNION_DIAGNOSTIC 202 printf("union: %s size now %ld\n", 203 uppersz != VNOVAL ? "upper" : "lower", (long) sz); 204 #endif 205 vnode_pager_setsize(vp, sz); 206 } 207 } 208 209 /* 210 * allocate a union_node/vnode pair. the vnode is 211 * referenced and locked. the new vnode is returned 212 * via (vpp). (mp) is the mountpoint of the union filesystem, 213 * (dvp) is the parent directory where the upper layer object 214 * should exist (but doesn't) and (cnp) is the componentname 215 * information which is partially copied to allow the upper 216 * layer object to be created at a later time. (uppervp) 217 * and (lowervp) reference the upper and lower layer objects 218 * being mapped. either, but not both, can be nil. 219 * if supplied, (uppervp) is locked. 220 * the reference is either maintained in the new union_node 221 * object which is allocated, or they are vrele'd. 222 * 223 * all union_nodes are maintained on a singly-linked 224 * list. new nodes are only allocated when they cannot 225 * be found on this list. entries on the list are 226 * removed when the vfs reclaim entry is called. 227 * 228 * a single lock is kept for the entire list. this is 229 * needed because the getnewvnode() function can block 230 * waiting for a vnode to become free, in which case there 231 * may be more than one process trying to get the same 232 * vnode. this lock is only taken if we are going to 233 * call getnewvnode, since the kernel itself is single-threaded. 234 * 235 * if an entry is found on the list, then call vget() to 236 * take a reference. this is done because there may be 237 * zero references to it and so it needs to removed from 238 * the vnode free list. 239 */ 240 int 241 union_allocvp(vpp, mp, undvp, dvp, cnp, uppervp, lowervp) 242 struct vnode **vpp; 243 struct mount *mp; 244 struct vnode *undvp; 245 struct vnode *dvp; /* may be null */ 246 struct componentname *cnp; /* may be null */ 247 struct vnode *uppervp; /* may be null */ 248 struct vnode *lowervp; /* may be null */ 249 { 250 int error; 251 struct union_node *un; 252 struct union_node **pp; 253 struct vnode *xlowervp = NULLVP; 254 struct union_mount *um = MOUNTTOUNIONMOUNT(mp); 255 int hash; 256 int vflag; 257 int try; 258 259 if (uppervp == NULLVP && lowervp == NULLVP) 260 panic("union: unidentifiable allocation"); 261 262 if (uppervp && lowervp && (uppervp->v_type != lowervp->v_type)) { 263 xlowervp = lowervp; 264 lowervp = NULLVP; 265 } 266 267 /* detect the root vnode (and aliases) */ 268 vflag = 0; 269 if ((uppervp == um->um_uppervp) && 270 ((lowervp == NULLVP) || lowervp == um->um_lowervp)) { 271 if (lowervp == NULLVP) { 272 lowervp = um->um_lowervp; 273 if (lowervp != NULLVP) 274 VREF(lowervp); 275 } 276 vflag = VROOT; 277 } 278 279 loop: 280 for (try = 0; try < 3; try++) { 281 switch (try) { 282 case 0: 283 if (lowervp == NULLVP) 284 continue; 285 hash = UNION_HASH(uppervp, lowervp); 286 break; 287 288 case 1: 289 if (uppervp == NULLVP) 290 continue; 291 hash = UNION_HASH(uppervp, NULLVP); 292 break; 293 294 case 2: 295 if (lowervp == NULLVP) 296 continue; 297 hash = UNION_HASH(NULLVP, lowervp); 298 break; 299 } 300 301 while (union_list_lock(hash)) 302 continue; 303 304 for (un = unhead[hash].lh_first; un != 0; 305 un = un->un_cache.le_next) { 306 if ((un->un_lowervp == lowervp || 307 un->un_lowervp == NULLVP) && 308 (un->un_uppervp == uppervp || 309 un->un_uppervp == NULLVP) && 310 (UNIONTOV(un)->v_mount == mp)) { 311 if (vget(UNIONTOV(un), 0)) { 312 union_list_unlock(hash); 313 goto loop; 314 } 315 break; 316 } 317 } 318 319 union_list_unlock(hash); 320 321 if (un) 322 break; 323 } 324 325 if (un) { 326 /* 327 * Obtain a lock on the union_node. 328 * uppervp is locked, though un->un_uppervp 329 * may not be. this doesn't break the locking 330 * hierarchy since in the case that un->un_uppervp 331 * is not yet locked it will be vrele'd and replaced 332 * with uppervp. 333 */ 334 335 if ((dvp != NULLVP) && (uppervp == dvp)) { 336 /* 337 * Access ``.'', so (un) will already 338 * be locked. Since this process has 339 * the lock on (uppervp) no other 340 * process can hold the lock on (un). 341 */ 342 #ifdef DIAGNOSTIC 343 if ((un->un_flags & UN_LOCKED) == 0) 344 panic("union: . not locked"); 345 else if (curproc && un->un_pid != curproc->p_pid && 346 un->un_pid > -1 && curproc->p_pid > -1) 347 panic("union: allocvp not lock owner"); 348 #endif 349 } else { 350 if (un->un_flags & UN_LOCKED) { 351 vrele(UNIONTOV(un)); 352 un->un_flags |= UN_WANT; 353 sleep((caddr_t) &un->un_flags, PINOD); 354 goto loop; 355 } 356 un->un_flags |= UN_LOCKED; 357 358 #ifdef DIAGNOSTIC 359 if (curproc) 360 un->un_pid = curproc->p_pid; 361 else 362 un->un_pid = -1; 363 #endif 364 } 365 366 /* 367 * At this point, the union_node is locked, 368 * un->un_uppervp may not be locked, and uppervp 369 * is locked or nil. 370 */ 371 372 /* 373 * Save information about the upper layer. 374 */ 375 if (uppervp != un->un_uppervp) { 376 union_newupper(un, uppervp); 377 } else if (uppervp) { 378 vrele(uppervp); 379 } 380 381 if (un->un_uppervp) { 382 un->un_flags |= UN_ULOCK; 383 un->un_flags &= ~UN_KLOCK; 384 } 385 386 /* 387 * Save information about the lower layer. 388 * This needs to keep track of pathname 389 * and directory information which union_vn_create 390 * might need. 391 */ 392 if (lowervp != un->un_lowervp) { 393 union_newlower(un, lowervp); 394 if (cnp && (lowervp != NULLVP) && 395 (lowervp->v_type == VREG)) { 396 un->un_hash = cnp->cn_hash; 397 un->un_path = malloc(cnp->cn_namelen+1, 398 M_TEMP, M_WAITOK); 399 bcopy(cnp->cn_nameptr, un->un_path, 400 cnp->cn_namelen); 401 un->un_path[cnp->cn_namelen] = '\0'; 402 VREF(dvp); 403 un->un_dirvp = dvp; 404 } 405 } else if (lowervp) { 406 vrele(lowervp); 407 } 408 *vpp = UNIONTOV(un); 409 return (0); 410 } 411 412 /* 413 * otherwise lock the vp list while we call getnewvnode 414 * since that can block. 415 */ 416 hash = UNION_HASH(uppervp, lowervp); 417 418 if (union_list_lock(hash)) 419 goto loop; 420 421 error = getnewvnode(VT_UNION, mp, union_vnodeop_p, vpp); 422 if (error) { 423 if (uppervp) { 424 if (dvp == uppervp) 425 vrele(uppervp); 426 else 427 vput(uppervp); 428 } 429 if (lowervp) 430 vrele(lowervp); 431 432 goto out; 433 } 434 435 MALLOC((*vpp)->v_data, void *, sizeof(struct union_node), 436 M_TEMP, M_WAITOK); 437 438 (*vpp)->v_flag |= vflag; 439 if (uppervp) 440 (*vpp)->v_type = uppervp->v_type; 441 else 442 (*vpp)->v_type = lowervp->v_type; 443 un = VTOUNION(*vpp); 444 un->un_vnode = *vpp; 445 un->un_uppervp = uppervp; 446 un->un_uppersz = VNOVAL; 447 un->un_lowervp = lowervp; 448 un->un_lowersz = VNOVAL; 449 un->un_openl = 0; 450 un->un_flags = UN_LOCKED; 451 if (un->un_uppervp) 452 un->un_flags |= UN_ULOCK; 453 #ifdef DIAGNOSTIC 454 if (curproc) 455 un->un_pid = curproc->p_pid; 456 else 457 un->un_pid = -1; 458 #endif 459 if (cnp && (lowervp != NULLVP) && (lowervp->v_type == VREG)) { 460 un->un_hash = cnp->cn_hash; 461 un->un_path = malloc(cnp->cn_namelen+1, M_TEMP, M_WAITOK); 462 bcopy(cnp->cn_nameptr, un->un_path, cnp->cn_namelen); 463 un->un_path[cnp->cn_namelen] = '\0'; 464 VREF(dvp); 465 un->un_dirvp = dvp; 466 } else { 467 un->un_hash = 0; 468 un->un_path = 0; 469 un->un_dirvp = 0; 470 } 471 472 LIST_INSERT_HEAD(&unhead[hash], un, un_cache); 473 un->un_flags |= UN_CACHED; 474 475 if (xlowervp) 476 vrele(xlowervp); 477 478 out: 479 union_list_unlock(hash); 480 481 return (error); 482 } 483 484 int 485 union_freevp(vp) 486 struct vnode *vp; 487 { 488 struct union_node *un = VTOUNION(vp); 489 490 if (un->un_flags & UN_CACHED) { 491 un->un_flags &= ~UN_CACHED; 492 LIST_REMOVE(un, un_cache); 493 } 494 495 if (un->un_uppervp != NULLVP) 496 vrele(un->un_uppervp); 497 if (un->un_lowervp != NULLVP) 498 vrele(un->un_lowervp); 499 if (un->un_dirvp != NULLVP) 500 vrele(un->un_dirvp); 501 if (un->un_path) 502 free(un->un_path, M_TEMP); 503 504 FREE(vp->v_data, M_TEMP); 505 vp->v_data = 0; 506 507 return (0); 508 } 509 510 /* 511 * copyfile. copy the vnode (fvp) to the vnode (tvp) 512 * using a sequence of reads and writes. both (fvp) 513 * and (tvp) are locked on entry and exit. 514 */ 515 int 516 union_copyfile(fvp, tvp, cred, p) 517 struct vnode *fvp; 518 struct vnode *tvp; 519 struct ucred *cred; 520 struct proc *p; 521 { 522 char *buf; 523 struct uio uio; 524 struct iovec iov; 525 int error = 0; 526 527 /* 528 * strategy: 529 * allocate a buffer of size MAXBSIZE. 530 * loop doing reads and writes, keeping track 531 * of the current uio offset. 532 * give up at the first sign of trouble. 533 */ 534 535 uio.uio_procp = p; 536 uio.uio_segflg = UIO_SYSSPACE; 537 uio.uio_offset = 0; 538 539 VOP_UNLOCK(fvp); /* XXX */ 540 LEASE_CHECK(fvp, p, cred, LEASE_READ); 541 VOP_LOCK(fvp); /* XXX */ 542 VOP_UNLOCK(tvp); /* XXX */ 543 LEASE_CHECK(tvp, p, cred, LEASE_WRITE); 544 VOP_LOCK(tvp); /* XXX */ 545 546 buf = malloc(MAXBSIZE, M_TEMP, M_WAITOK); 547 548 /* ugly loop follows... */ 549 do { 550 off_t offset = uio.uio_offset; 551 552 uio.uio_iov = &iov; 553 uio.uio_iovcnt = 1; 554 iov.iov_base = buf; 555 iov.iov_len = MAXBSIZE; 556 uio.uio_resid = iov.iov_len; 557 uio.uio_rw = UIO_READ; 558 error = VOP_READ(fvp, &uio, 0, cred); 559 560 if (error == 0) { 561 uio.uio_iov = &iov; 562 uio.uio_iovcnt = 1; 563 iov.iov_base = buf; 564 iov.iov_len = MAXBSIZE - uio.uio_resid; 565 uio.uio_offset = offset; 566 uio.uio_rw = UIO_WRITE; 567 uio.uio_resid = iov.iov_len; 568 569 if (uio.uio_resid == 0) 570 break; 571 572 do { 573 error = VOP_WRITE(tvp, &uio, 0, cred); 574 } while ((uio.uio_resid > 0) && (error == 0)); 575 } 576 577 } while (error == 0); 578 579 free(buf, M_TEMP); 580 return (error); 581 } 582 583 /* 584 * (un) is assumed to be locked on entry and remains 585 * locked on exit. 586 */ 587 int 588 union_copyup(un, docopy, cred, p) 589 struct union_node *un; 590 int docopy; 591 struct ucred *cred; 592 struct proc *p; 593 { 594 int error; 595 struct vnode *lvp, *uvp; 596 597 error = union_vn_create(&uvp, un, p); 598 if (error) 599 return (error); 600 601 /* at this point, uppervp is locked */ 602 union_newupper(un, uvp); 603 un->un_flags |= UN_ULOCK; 604 605 lvp = un->un_lowervp; 606 607 if (docopy) { 608 /* 609 * XX - should not ignore errors 610 * from VOP_CLOSE 611 */ 612 VOP_LOCK(lvp); 613 error = VOP_OPEN(lvp, FREAD, cred, p); 614 if (error == 0) { 615 error = union_copyfile(lvp, uvp, cred, p); 616 VOP_UNLOCK(lvp); 617 (void) VOP_CLOSE(lvp, FREAD); 618 } 619 #ifdef UNION_DIAGNOSTIC 620 if (error == 0) 621 uprintf("union: copied up %s\n", un->un_path); 622 #endif 623 624 } 625 un->un_flags &= ~UN_ULOCK; 626 VOP_UNLOCK(uvp); 627 union_vn_close(uvp, FWRITE, cred, p); 628 VOP_LOCK(uvp); 629 un->un_flags |= UN_ULOCK; 630 631 /* 632 * Subsequent IOs will go to the top layer, so 633 * call close on the lower vnode and open on the 634 * upper vnode to ensure that the filesystem keeps 635 * its references counts right. This doesn't do 636 * the right thing with (cred) and (FREAD) though. 637 * Ignoring error returns is not right, either. 638 */ 639 if (error == 0) { 640 int i; 641 642 for (i = 0; i < un->un_openl; i++) { 643 (void) VOP_CLOSE(lvp, FREAD); 644 (void) VOP_OPEN(uvp, FREAD, cred, p); 645 } 646 un->un_openl = 0; 647 } 648 649 return (error); 650 651 } 652 653 /* 654 * Create a shadow directory in the upper layer. 655 * The new vnode is returned locked. 656 * 657 * (um) points to the union mount structure for access to the 658 * the mounting process's credentials. 659 * (dvp) is the directory in which to create the shadow directory. 660 * it is unlocked on entry and exit. 661 * (cnp) is the componentname to be created. 662 * (vpp) is the returned newly created shadow directory, which 663 * is returned locked. 664 */ 665 int 666 union_mkshadow(um, dvp, cnp, vpp) 667 struct union_mount *um; 668 struct vnode *dvp; 669 struct componentname *cnp; 670 struct vnode **vpp; 671 { 672 int error; 673 struct vattr va; 674 struct proc *p = cnp->cn_proc; 675 struct componentname cn; 676 677 /* 678 * policy: when creating the shadow directory in the 679 * upper layer, create it owned by the user who did 680 * the mount, group from parent directory, and mode 681 * 777 modified by umask (ie mostly identical to the 682 * mkdir syscall). (jsp, kb) 683 */ 684 685 /* 686 * A new componentname structure must be faked up because 687 * there is no way to know where the upper level cnp came 688 * from or what it is being used for. This must duplicate 689 * some of the work done by NDINIT, some of the work done 690 * by namei, some of the work done by lookup and some of 691 * the work done by VOP_LOOKUP when given a CREATE flag. 692 * Conclusion: Horrible. 693 * 694 * The pathname buffer will be FREEed by VOP_MKDIR. 695 */ 696 cn.cn_pnbuf = malloc(cnp->cn_namelen+1, M_NAMEI, M_WAITOK); 697 bcopy(cnp->cn_nameptr, cn.cn_pnbuf, cnp->cn_namelen); 698 cn.cn_pnbuf[cnp->cn_namelen] = '\0'; 699 700 cn.cn_nameiop = CREATE; 701 cn.cn_flags = (LOCKPARENT|HASBUF|SAVENAME|SAVESTART|ISLASTCN); 702 cn.cn_proc = cnp->cn_proc; 703 if (um->um_op == UNMNT_ABOVE) 704 cn.cn_cred = cnp->cn_cred; 705 else 706 cn.cn_cred = um->um_cred; 707 cn.cn_nameptr = cn.cn_pnbuf; 708 cn.cn_namelen = cnp->cn_namelen; 709 cn.cn_hash = cnp->cn_hash; 710 cn.cn_consume = cnp->cn_consume; 711 712 VREF(dvp); 713 if (error = relookup(dvp, vpp, &cn)) 714 return (error); 715 vrele(dvp); 716 717 if (*vpp) { 718 VOP_ABORTOP(dvp, &cn); 719 VOP_UNLOCK(dvp); 720 vrele(*vpp); 721 *vpp = NULLVP; 722 return (EEXIST); 723 } 724 725 VATTR_NULL(&va); 726 va.va_type = VDIR; 727 va.va_mode = um->um_cmode; 728 729 /* LEASE_CHECK: dvp is locked */ 730 LEASE_CHECK(dvp, p, p->p_ucred, LEASE_WRITE); 731 732 error = VOP_MKDIR(dvp, vpp, &cn, &va); 733 return (error); 734 } 735 736 /* 737 * union_vn_create: creates and opens a new shadow file 738 * on the upper union layer. this function is similar 739 * in spirit to calling vn_open but it avoids calling namei(). 740 * the problem with calling namei is that a) it locks too many 741 * things, and b) it doesn't start at the "right" directory, 742 * whereas relookup is told where to start. 743 */ 744 int 745 union_vn_create(vpp, un, p) 746 struct vnode **vpp; 747 struct union_node *un; 748 struct proc *p; 749 { 750 struct vnode *vp; 751 struct ucred *cred = p->p_ucred; 752 struct vattr vat; 753 struct vattr *vap = &vat; 754 int fmode = FFLAGS(O_WRONLY|O_CREAT|O_TRUNC|O_EXCL); 755 int error; 756 int cmode = UN_FILEMODE & ~p->p_fd->fd_cmask; 757 char *cp; 758 struct componentname cn; 759 760 *vpp = NULLVP; 761 762 /* 763 * Build a new componentname structure (for the same 764 * reasons outlines in union_mkshadow). 765 * The difference here is that the file is owned by 766 * the current user, rather than by the person who 767 * did the mount, since the current user needs to be 768 * able to write the file (that's why it is being 769 * copied in the first place). 770 */ 771 cn.cn_namelen = strlen(un->un_path); 772 cn.cn_pnbuf = (caddr_t) malloc(cn.cn_namelen, M_NAMEI, M_WAITOK); 773 bcopy(un->un_path, cn.cn_pnbuf, cn.cn_namelen+1); 774 cn.cn_nameiop = CREATE; 775 cn.cn_flags = (LOCKPARENT|HASBUF|SAVENAME|SAVESTART|ISLASTCN); 776 cn.cn_proc = p; 777 cn.cn_cred = p->p_ucred; 778 cn.cn_nameptr = cn.cn_pnbuf; 779 cn.cn_hash = un->un_hash; 780 cn.cn_consume = 0; 781 782 VREF(un->un_dirvp); 783 if (error = relookup(un->un_dirvp, &vp, &cn)) 784 return (error); 785 vrele(un->un_dirvp); 786 787 if (vp) { 788 VOP_ABORTOP(un->un_dirvp, &cn); 789 if (un->un_dirvp == vp) 790 vrele(un->un_dirvp); 791 else 792 vput(un->un_dirvp); 793 vrele(vp); 794 return (EEXIST); 795 } 796 797 /* 798 * Good - there was no race to create the file 799 * so go ahead and create it. The permissions 800 * on the file will be 0666 modified by the 801 * current user's umask. Access to the file, while 802 * it is unioned, will require access to the top *and* 803 * bottom files. Access when not unioned will simply 804 * require access to the top-level file. 805 * TODO: confirm choice of access permissions. 806 */ 807 VATTR_NULL(vap); 808 vap->va_type = VREG; 809 vap->va_mode = cmode; 810 LEASE_CHECK(un->un_dirvp, p, cred, LEASE_WRITE); 811 if (error = VOP_CREATE(un->un_dirvp, &vp, &cn, vap)) 812 return (error); 813 814 if (error = VOP_OPEN(vp, fmode, cred, p)) { 815 vput(vp); 816 return (error); 817 } 818 819 vp->v_writecount++; 820 *vpp = vp; 821 return (0); 822 } 823 824 int 825 union_vn_close(vp, fmode, cred, p) 826 struct vnode *vp; 827 int fmode; 828 struct ucred *cred; 829 struct proc *p; 830 { 831 if (fmode & FWRITE) 832 --vp->v_writecount; 833 return (VOP_CLOSE(vp, fmode)); 834 } 835 836 void 837 union_removed_upper(un) 838 struct union_node *un; 839 { 840 841 if (un->un_flags & UN_ULOCK) { 842 un->un_flags &= ~UN_ULOCK; 843 VOP_UNLOCK(un->un_uppervp); 844 } 845 846 if (un->un_flags & UN_CACHED) { 847 un->un_flags &= ~UN_CACHED; 848 LIST_REMOVE(un, un_cache); 849 } 850 } 851 852 struct vnode * 853 union_lowervp(vp) 854 struct vnode *vp; 855 { 856 struct union_node *un = VTOUNION(vp); 857 858 if ((un->un_lowervp != NULLVP) && 859 (vp->v_type == un->un_lowervp->v_type)) { 860 if (vget(un->un_lowervp, 0) == 0) 861 return (un->un_lowervp); 862 } 863 864 return (NULLVP); 865 } 866