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