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