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