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