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