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