1 /* $NetBSD: union_subr.c,v 1.55 2011/11/25 11:19:10 hannken 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.55 2011/11/25 11:19:10 hannken 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/dirent.h> 86 #include <sys/file.h> 87 #include <sys/filedesc.h> 88 #include <sys/queue.h> 89 #include <sys/mount.h> 90 #include <sys/stat.h> 91 #include <sys/kauth.h> 92 93 #include <uvm/uvm_extern.h> 94 95 #include <fs/union/union.h> 96 #include <miscfs/genfs/genfs.h> 97 #include <miscfs/specfs/specdev.h> 98 99 static LIST_HEAD(uhashhead, union_node) *uhashtbl; 100 static u_long uhash_mask; /* size of hash table - 1 */ 101 #define UNION_HASH(u, l) \ 102 ((((u_long) (u) + (u_long) (l)) >> 8) & uhash_mask) 103 #define NOHASH ((u_long)-1) 104 105 static kmutex_t uhash_lock; 106 107 void union_updatevp(struct union_node *, struct vnode *, struct vnode *); 108 static int union_do_lookup(struct vnode *, struct componentname *, kauth_cred_t, const char *, u_long); 109 int union_vn_close(struct vnode *, int, kauth_cred_t, struct lwp *); 110 static void union_dircache_r(struct vnode *, struct vnode ***, int *); 111 struct vnode *union_dircache(struct vnode *, struct lwp *); 112 113 void 114 union_init(void) 115 { 116 117 mutex_init(&uhash_lock, MUTEX_DEFAULT, IPL_NONE); 118 uhashtbl = hashinit(desiredvnodes, HASH_LIST, true, &uhash_mask); 119 } 120 121 void 122 union_reinit(void) 123 { 124 struct union_node *un; 125 struct uhashhead *oldhash, *hash; 126 u_long oldmask, mask, val; 127 int i; 128 129 hash = hashinit(desiredvnodes, HASH_LIST, true, &mask); 130 mutex_enter(&uhash_lock); 131 oldhash = uhashtbl; 132 oldmask = uhash_mask; 133 uhashtbl = hash; 134 uhash_mask = mask; 135 for (i = 0; i <= oldmask; i++) { 136 while ((un = LIST_FIRST(&oldhash[i])) != NULL) { 137 LIST_REMOVE(un, un_cache); 138 val = UNION_HASH(un->un_uppervp, un->un_lowervp); 139 LIST_INSERT_HEAD(&hash[val], un, un_cache); 140 } 141 } 142 mutex_exit(&uhash_lock); 143 hashdone(oldhash, HASH_LIST, oldmask); 144 } 145 146 /* 147 * Free global unionfs resources. 148 */ 149 void 150 union_done(void) 151 { 152 153 hashdone(uhashtbl, HASH_LIST, uhash_mask); 154 mutex_destroy(&uhash_lock); 155 156 /* Make sure to unset the readdir hook. */ 157 vn_union_readdir_hook = NULL; 158 } 159 160 void 161 union_updatevp(struct union_node *un, struct vnode *uppervp, 162 struct vnode *lowervp) 163 { 164 int ohash = UNION_HASH(un->un_uppervp, un->un_lowervp); 165 int nhash = UNION_HASH(uppervp, lowervp); 166 int docache = (lowervp != NULLVP || uppervp != NULLVP); 167 bool un_unlock; 168 169 KASSERT(VOP_ISLOCKED(UNIONTOV(un)) == LK_EXCLUSIVE); 170 171 mutex_enter(&uhash_lock); 172 173 if (!docache || ohash != nhash) { 174 if (un->un_cflags & UN_CACHED) { 175 un->un_cflags &= ~UN_CACHED; 176 LIST_REMOVE(un, un_cache); 177 } 178 } 179 180 if (un->un_lowervp != lowervp) { 181 if (un->un_lowervp) { 182 vrele(un->un_lowervp); 183 if (un->un_path) { 184 free(un->un_path, M_TEMP); 185 un->un_path = 0; 186 } 187 if (un->un_dirvp) { 188 vrele(un->un_dirvp); 189 un->un_dirvp = NULLVP; 190 } 191 } 192 un->un_lowervp = lowervp; 193 mutex_enter(&un->un_lock); 194 un->un_lowersz = VNOVAL; 195 mutex_exit(&un->un_lock); 196 } 197 198 if (un->un_uppervp != uppervp) { 199 if (un->un_uppervp) { 200 un_unlock = false; 201 vrele(un->un_uppervp); 202 } else 203 un_unlock = true; 204 205 mutex_enter(&un->un_lock); 206 un->un_uppervp = uppervp; 207 mutex_exit(&un->un_lock); 208 if (un_unlock) { 209 struct vop_unlock_args ap; 210 211 ap.a_vp = UNIONTOV(un); 212 genfs_unlock(&ap); 213 } 214 mutex_enter(&un->un_lock); 215 un->un_uppersz = VNOVAL; 216 mutex_exit(&un->un_lock); 217 /* Update union vnode interlock. */ 218 if (uppervp != NULL) { 219 mutex_obj_hold(uppervp->v_interlock); 220 uvm_obj_setlock(&UNIONTOV(un)->v_uobj, 221 uppervp->v_interlock); 222 } 223 } 224 225 if (docache && (ohash != nhash)) { 226 LIST_INSERT_HEAD(&uhashtbl[nhash], un, un_cache); 227 un->un_cflags |= UN_CACHED; 228 } 229 230 mutex_exit(&uhash_lock); 231 } 232 233 void 234 union_newlower(struct union_node *un, struct vnode *lowervp) 235 { 236 237 union_updatevp(un, un->un_uppervp, lowervp); 238 } 239 240 void 241 union_newupper(struct union_node *un, struct vnode *uppervp) 242 { 243 244 union_updatevp(un, uppervp, un->un_lowervp); 245 } 246 247 /* 248 * Keep track of size changes in the underlying vnodes. 249 * If the size changes, then callback to the vm layer 250 * giving priority to the upper layer size. 251 * 252 * Mutex un_lock hold on entry and released on return. 253 */ 254 void 255 union_newsize(struct vnode *vp, off_t uppersz, off_t lowersz) 256 { 257 struct union_node *un = VTOUNION(vp); 258 off_t sz; 259 260 KASSERT(mutex_owned(&un->un_lock)); 261 /* only interested in regular files */ 262 if (vp->v_type != VREG) { 263 mutex_exit(&un->un_lock); 264 uvm_vnp_setsize(vp, 0); 265 return; 266 } 267 268 sz = VNOVAL; 269 270 if ((uppersz != VNOVAL) && (un->un_uppersz != uppersz)) { 271 un->un_uppersz = uppersz; 272 if (sz == VNOVAL) 273 sz = un->un_uppersz; 274 } 275 276 if ((lowersz != VNOVAL) && (un->un_lowersz != lowersz)) { 277 un->un_lowersz = lowersz; 278 if (sz == VNOVAL) 279 sz = un->un_lowersz; 280 } 281 mutex_exit(&un->un_lock); 282 283 if (sz != VNOVAL) { 284 #ifdef UNION_DIAGNOSTIC 285 printf("union: %s size now %qd\n", 286 uppersz != VNOVAL ? "upper" : "lower", sz); 287 #endif 288 uvm_vnp_setsize(vp, sz); 289 } 290 } 291 292 /* 293 * allocate a union_node/vnode pair. the vnode is 294 * referenced and locked. the new vnode is returned 295 * via (vpp). (mp) is the mountpoint of the union filesystem, 296 * (dvp) is the parent directory where the upper layer object 297 * should exist (but doesn't) and (cnp) is the componentname 298 * information which is partially copied to allow the upper 299 * layer object to be created at a later time. (uppervp) 300 * and (lowervp) reference the upper and lower layer objects 301 * being mapped. either, but not both, can be nil. 302 * if supplied, (uppervp) is locked. 303 * the reference is either maintained in the new union_node 304 * object which is allocated, or they are vrele'd. 305 * 306 * all union_nodes are maintained on a singly-linked 307 * list. new nodes are only allocated when they cannot 308 * be found on this list. entries on the list are 309 * removed when the vfs reclaim entry is called. 310 * 311 * a single lock is kept for the entire list. this is 312 * needed because the getnewvnode() function can block 313 * waiting for a vnode to become free, in which case there 314 * may be more than one process trying to get the same 315 * vnode. this lock is only taken if we are going to 316 * call getnewvnode, since the kernel itself is single-threaded. 317 * 318 * if an entry is found on the list, then call vget() to 319 * take a reference. this is done because there may be 320 * zero references to it and so it needs to removed from 321 * the vnode free list. 322 */ 323 int 324 union_allocvp( 325 struct vnode **vpp, 326 struct mount *mp, 327 struct vnode *undvp, /* parent union vnode */ 328 struct vnode *dvp, /* may be null */ 329 struct componentname *cnp, /* may be null */ 330 struct vnode *uppervp, /* may be null */ 331 struct vnode *lowervp, /* may be null */ 332 int docache) 333 { 334 int error; 335 struct vattr va; 336 struct union_node *un = NULL, *un1; 337 struct vnode *vp, *xlowervp = NULLVP; 338 struct union_mount *um = MOUNTTOUNIONMOUNT(mp); 339 voff_t uppersz, lowersz; 340 dev_t rdev; 341 u_long hash[3]; 342 int vflag, iflag, lflag; 343 int try; 344 345 if (uppervp) 346 KASSERT(VOP_ISLOCKED(uppervp) == LK_EXCLUSIVE); 347 348 if (uppervp == NULLVP && lowervp == NULLVP) 349 panic("union: unidentifiable allocation"); 350 351 if (uppervp && lowervp && (uppervp->v_type != lowervp->v_type)) { 352 xlowervp = lowervp; 353 lowervp = NULLVP; 354 } 355 356 /* detect the root vnode (and aliases) */ 357 iflag = VI_LAYER; 358 vflag = 0; 359 if ((uppervp == um->um_uppervp) && 360 ((lowervp == NULLVP) || lowervp == um->um_lowervp)) { 361 if (lowervp == NULLVP) { 362 lowervp = um->um_lowervp; 363 if (lowervp != NULLVP) 364 vref(lowervp); 365 } 366 iflag = 0; 367 vflag = VV_ROOT; 368 } 369 370 if (!docache) { 371 un = NULL; 372 goto found; 373 } 374 375 /* 376 * If both uppervp and lowervp are not NULL we have to 377 * search union nodes with one vnode as NULL too. 378 */ 379 hash[0] = UNION_HASH(uppervp, lowervp); 380 if (uppervp == NULL || lowervp == NULL) { 381 hash[1] = hash[2] = NOHASH; 382 } else { 383 hash[1] = UNION_HASH(uppervp, NULLVP); 384 hash[2] = UNION_HASH(NULLVP, lowervp); 385 } 386 387 loop: 388 mutex_enter(&uhash_lock); 389 390 for (try = 0; try < 3; try++) { 391 if (hash[try] == NOHASH) 392 continue; 393 LIST_FOREACH(un, &uhashtbl[hash[try]], un_cache) { 394 if ((un->un_lowervp && un->un_lowervp != lowervp) || 395 (un->un_uppervp && un->un_uppervp != uppervp) || 396 UNIONTOV(un)->v_mount != mp) 397 continue; 398 399 if (uppervp != NULL && 400 (uppervp == dvp || uppervp == un->un_uppervp)) 401 /* "." or already locked. */ 402 lflag = 0; 403 else 404 lflag = LK_EXCLUSIVE; 405 vp = UNIONTOV(un); 406 mutex_enter(vp->v_interlock); 407 /* 408 * If this node being cleaned out and our caller 409 * holds a lock, then ignore it and continue. To 410 * allow the cleaning to succeed the current thread 411 * must make progress. For a brief time the cache 412 * may contain more than one vnode referring to 413 * a lower node. 414 */ 415 if ((vp->v_iflag & VI_XLOCK) != 0 && lflag == 0) { 416 mutex_exit(vp->v_interlock); 417 continue; 418 } 419 mutex_exit(&uhash_lock); 420 if (vget(vp, lflag)) 421 goto loop; 422 goto found; 423 } 424 } 425 426 mutex_exit(&uhash_lock); 427 428 found: 429 if (un) { 430 KASSERT(VOP_ISLOCKED(UNIONTOV(un)) == LK_EXCLUSIVE); 431 KASSERT(uppervp == NULL || 432 VOP_ISLOCKED(uppervp) == LK_EXCLUSIVE); 433 /* 434 * Save information about the upper layer. 435 */ 436 if (uppervp != un->un_uppervp) { 437 union_newupper(un, uppervp); 438 } else if (uppervp) { 439 vrele(uppervp); 440 } 441 442 if (un->un_uppervp) 443 un->un_flags &= ~UN_KLOCK; 444 445 /* 446 * Save information about the lower layer. 447 * This needs to keep track of pathname 448 * and directory information which union_vn_create 449 * might need. 450 */ 451 if (lowervp != un->un_lowervp) { 452 union_newlower(un, lowervp); 453 if (cnp && (lowervp != NULLVP)) { 454 un->un_hash = cnp->cn_hash; 455 un->un_path = malloc(cnp->cn_namelen+1, 456 M_TEMP, M_WAITOK); 457 memcpy(un->un_path, cnp->cn_nameptr, 458 cnp->cn_namelen); 459 un->un_path[cnp->cn_namelen] = '\0'; 460 vref(dvp); 461 un->un_dirvp = dvp; 462 } 463 } else if (lowervp) { 464 vrele(lowervp); 465 } 466 *vpp = UNIONTOV(un); 467 return (0); 468 } 469 470 uppersz = lowersz = VNOVAL; 471 if (uppervp != NULLVP) 472 if (VOP_GETATTR(uppervp, &va, FSCRED) == 0) 473 uppersz = va.va_size; 474 if (lowervp != NULLVP) { 475 vn_lock(lowervp, LK_SHARED | LK_RETRY); 476 error = VOP_GETATTR(lowervp, &va, FSCRED); 477 VOP_UNLOCK(lowervp); 478 if (error == 0) 479 lowersz = va.va_size; 480 } 481 482 /* 483 * Get a new vnode and share the lock with upper layer vnode, 484 * unless layers are inverted. 485 */ 486 vnode_t *svp = (uppervp != NULLVP) ? uppervp : lowervp; 487 error = getnewvnode(VT_UNION, mp, union_vnodeop_p, 488 svp->v_interlock, vpp); 489 if (error) { 490 if (uppervp) { 491 if (dvp == uppervp) 492 vrele(uppervp); 493 else 494 vput(uppervp); 495 } 496 if (lowervp) 497 vrele(lowervp); 498 499 return error; 500 } 501 502 if (docache) { 503 mutex_enter(&uhash_lock); 504 LIST_FOREACH(un1, &uhashtbl[hash[0]], un_cache) { 505 if (un1->un_lowervp == lowervp && 506 un1->un_uppervp == uppervp && 507 UNIONTOV(un1)->v_mount == mp) { 508 vp = UNIONTOV(un1); 509 mutex_enter(vp->v_interlock); 510 /* 511 * Ignore nodes being cleaned out. 512 * See the cache lookup above. 513 */ 514 if ((vp->v_iflag & VI_XLOCK) != 0) { 515 mutex_exit(vp->v_interlock); 516 continue; 517 } 518 mutex_exit(vp->v_interlock); 519 /* 520 * Another thread beat us, push back freshly 521 * allocated vnode and retry. 522 */ 523 mutex_exit(&uhash_lock); 524 ungetnewvnode(*vpp); 525 goto loop; 526 } 527 } 528 } 529 530 (*vpp)->v_data = malloc(sizeof(struct union_node), M_TEMP, M_WAITOK); 531 532 (*vpp)->v_vflag |= vflag; 533 (*vpp)->v_iflag |= iflag; 534 rdev = NODEV; 535 if (uppervp) { 536 (*vpp)->v_type = uppervp->v_type; 537 if (uppervp->v_type == VCHR || uppervp->v_type == VBLK) 538 rdev = uppervp->v_rdev; 539 } else { 540 (*vpp)->v_type = lowervp->v_type; 541 if (lowervp->v_type == VCHR || lowervp->v_type == VBLK) 542 rdev = lowervp->v_rdev; 543 } 544 if (rdev != NODEV) 545 spec_node_init(*vpp, rdev); 546 547 un = VTOUNION(*vpp); 548 mutex_init(&un->un_lock, MUTEX_DEFAULT, IPL_NONE); 549 un->un_vnode = *vpp; 550 un->un_uppervp = uppervp; 551 un->un_lowervp = lowervp; 552 un->un_pvp = undvp; 553 if (undvp != NULLVP) 554 vref(undvp); 555 un->un_dircache = 0; 556 un->un_openl = 0; 557 un->un_flags = 0; 558 un->un_cflags = 0; 559 560 if (uppervp == NULL) { 561 struct vop_lock_args ap; 562 563 ap.a_vp = UNIONTOV(un); 564 ap.a_flags = LK_EXCLUSIVE; 565 error = genfs_lock(&ap); 566 KASSERT(error == 0); 567 } 568 569 mutex_enter(&un->un_lock); 570 un->un_uppersz = VNOVAL; 571 un->un_lowersz = VNOVAL; 572 union_newsize(*vpp, uppersz, lowersz); 573 574 if (dvp && cnp && (lowervp != NULLVP)) { 575 un->un_hash = cnp->cn_hash; 576 un->un_path = malloc(cnp->cn_namelen+1, M_TEMP, M_WAITOK); 577 memcpy(un->un_path, cnp->cn_nameptr, cnp->cn_namelen); 578 un->un_path[cnp->cn_namelen] = '\0'; 579 vref(dvp); 580 un->un_dirvp = dvp; 581 } else { 582 un->un_hash = 0; 583 un->un_path = 0; 584 un->un_dirvp = 0; 585 } 586 587 if (docache) { 588 LIST_INSERT_HEAD(&uhashtbl[hash[0]], un, un_cache); 589 un->un_cflags |= UN_CACHED; 590 } 591 592 if (xlowervp) 593 vrele(xlowervp); 594 595 if (docache) 596 mutex_exit(&uhash_lock); 597 598 return (error); 599 } 600 601 int 602 union_freevp(struct vnode *vp) 603 { 604 int hash; 605 struct union_node *un = VTOUNION(vp); 606 607 hash = UNION_HASH(un->un_uppervp, un->un_lowervp); 608 609 mutex_enter(&uhash_lock); 610 if (un->un_cflags & UN_CACHED) { 611 un->un_cflags &= ~UN_CACHED; 612 LIST_REMOVE(un, un_cache); 613 } 614 mutex_exit(&uhash_lock); 615 616 if (un->un_pvp != NULLVP) 617 vrele(un->un_pvp); 618 if (un->un_uppervp != NULLVP) 619 vrele(un->un_uppervp); 620 if (un->un_lowervp != NULLVP) 621 vrele(un->un_lowervp); 622 if (un->un_dirvp != NULLVP) 623 vrele(un->un_dirvp); 624 if (un->un_path) 625 free(un->un_path, M_TEMP); 626 mutex_destroy(&un->un_lock); 627 628 free(vp->v_data, M_TEMP); 629 vp->v_data = NULL; 630 631 return (0); 632 } 633 634 /* 635 * copyfile. copy the vnode (fvp) to the vnode (tvp) 636 * using a sequence of reads and writes. both (fvp) 637 * and (tvp) are locked on entry and exit. 638 */ 639 int 640 union_copyfile(struct vnode *fvp, struct vnode *tvp, kauth_cred_t cred, 641 struct lwp *l) 642 { 643 char *tbuf; 644 struct uio uio; 645 struct iovec iov; 646 int error = 0; 647 648 /* 649 * strategy: 650 * allocate a buffer of size MAXBSIZE. 651 * loop doing reads and writes, keeping track 652 * of the current uio offset. 653 * give up at the first sign of trouble. 654 */ 655 656 uio.uio_offset = 0; 657 UIO_SETUP_SYSSPACE(&uio); 658 659 tbuf = malloc(MAXBSIZE, M_TEMP, M_WAITOK); 660 661 /* ugly loop follows... */ 662 do { 663 off_t offset = uio.uio_offset; 664 665 uio.uio_iov = &iov; 666 uio.uio_iovcnt = 1; 667 iov.iov_base = tbuf; 668 iov.iov_len = MAXBSIZE; 669 uio.uio_resid = iov.iov_len; 670 uio.uio_rw = UIO_READ; 671 error = VOP_READ(fvp, &uio, 0, cred); 672 673 if (error == 0) { 674 uio.uio_iov = &iov; 675 uio.uio_iovcnt = 1; 676 iov.iov_base = tbuf; 677 iov.iov_len = MAXBSIZE - uio.uio_resid; 678 uio.uio_offset = offset; 679 uio.uio_rw = UIO_WRITE; 680 uio.uio_resid = iov.iov_len; 681 682 if (uio.uio_resid == 0) 683 break; 684 685 do { 686 error = VOP_WRITE(tvp, &uio, 0, cred); 687 } while ((uio.uio_resid > 0) && (error == 0)); 688 } 689 690 } while (error == 0); 691 692 free(tbuf, M_TEMP); 693 return (error); 694 } 695 696 /* 697 * (un) is assumed to be locked on entry and remains 698 * locked on exit. 699 */ 700 int 701 union_copyup(struct union_node *un, int docopy, kauth_cred_t cred, 702 struct lwp *l) 703 { 704 int error; 705 struct vnode *lvp, *uvp; 706 struct vattr lvattr, uvattr; 707 708 error = union_vn_create(&uvp, un, l); 709 if (error) 710 return (error); 711 712 KASSERT(VOP_ISLOCKED(uvp) == LK_EXCLUSIVE); 713 union_newupper(un, uvp); 714 715 lvp = un->un_lowervp; 716 717 if (docopy) { 718 /* 719 * XX - should not ignore errors 720 * from VOP_CLOSE 721 */ 722 vn_lock(lvp, LK_EXCLUSIVE | LK_RETRY); 723 724 error = VOP_GETATTR(lvp, &lvattr, cred); 725 if (error == 0) 726 error = VOP_OPEN(lvp, FREAD, cred); 727 if (error == 0) { 728 error = union_copyfile(lvp, uvp, cred, l); 729 (void) VOP_CLOSE(lvp, FREAD, cred); 730 } 731 if (error == 0) { 732 /* Copy permissions up too */ 733 vattr_null(&uvattr); 734 uvattr.va_mode = lvattr.va_mode; 735 uvattr.va_flags = lvattr.va_flags; 736 error = VOP_SETATTR(uvp, &uvattr, cred); 737 } 738 VOP_UNLOCK(lvp); 739 #ifdef UNION_DIAGNOSTIC 740 if (error == 0) 741 uprintf("union: copied up %s\n", un->un_path); 742 #endif 743 744 } 745 union_vn_close(uvp, FWRITE, cred, l); 746 747 /* 748 * Subsequent IOs will go to the top layer, so 749 * call close on the lower vnode and open on the 750 * upper vnode to ensure that the filesystem keeps 751 * its references counts right. This doesn't do 752 * the right thing with (cred) and (FREAD) though. 753 * Ignoring error returns is not right, either. 754 */ 755 if (error == 0) { 756 int i; 757 758 vn_lock(lvp, LK_EXCLUSIVE | LK_RETRY); 759 for (i = 0; i < un->un_openl; i++) { 760 (void) VOP_CLOSE(lvp, FREAD, cred); 761 (void) VOP_OPEN(uvp, FREAD, cred); 762 } 763 un->un_openl = 0; 764 VOP_UNLOCK(lvp); 765 } 766 767 return (error); 768 769 } 770 771 /* 772 * Prepare the creation of a new node in the upper layer. 773 * 774 * (dvp) is the directory in which to create the new node. 775 * it is locked on entry and exit. 776 * (cnp) is the componentname to be created. 777 * (cred, path, hash) are credentials, path and its hash to fill (cnp). 778 */ 779 static int 780 union_do_lookup(struct vnode *dvp, struct componentname *cnp, kauth_cred_t cred, 781 const char *path, u_long hash) 782 { 783 int error; 784 const char *cp; 785 struct vnode *vp; 786 787 cnp->cn_nameiop = CREATE; 788 cnp->cn_flags = LOCKPARENT | ISLASTCN; 789 cnp->cn_cred = cred; 790 cnp->cn_nameptr = path; 791 cnp->cn_namelen = strlen(path); 792 if (hash == 0) { 793 cp = NULL; 794 cnp->cn_hash = namei_hash(cnp->cn_nameptr, &cp); 795 KASSERT(*cp == 0); 796 } else { 797 cnp->cn_hash = hash; 798 } 799 800 error = VOP_LOOKUP(dvp, &vp, cnp); 801 802 if (error == 0) { 803 KASSERT(vp != NULL); 804 VOP_ABORTOP(dvp, cnp); 805 if (dvp != vp) 806 vput(vp); 807 else 808 vrele(vp); 809 error = EEXIST; 810 } else if (error == EJUSTRETURN) { 811 error = 0; 812 } 813 814 return error; 815 } 816 817 /* 818 * Create a shadow directory in the upper layer. 819 * The new vnode is returned locked. 820 * 821 * (um) points to the union mount structure for access to the 822 * the mounting process's credentials. 823 * (dvp) is the directory in which to create the shadow directory. 824 * it is unlocked on entry and exit. 825 * (cnp) is the componentname to be created. 826 * (vpp) is the returned newly created shadow directory, which 827 * is returned locked. 828 * 829 * N.B. We still attempt to create shadow directories even if the union 830 * is mounted read-only, which is a little nonintuitive. 831 */ 832 int 833 union_mkshadow(struct union_mount *um, struct vnode *dvp, 834 struct componentname *cnp, struct vnode **vpp) 835 { 836 int error; 837 struct vattr va; 838 struct componentname cn; 839 char *pnbuf; 840 841 if (cnp->cn_namelen + 1 > MAXPATHLEN) 842 return ENAMETOOLONG; 843 pnbuf = PNBUF_GET(); 844 memcpy(pnbuf, cnp->cn_nameptr, cnp->cn_namelen); 845 pnbuf[cnp->cn_namelen] = '\0'; 846 847 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY); 848 849 error = union_do_lookup(dvp, &cn, 850 (um->um_op == UNMNT_ABOVE ? cnp->cn_cred : um->um_cred), pnbuf, 0); 851 if (error) { 852 VOP_UNLOCK(dvp); 853 PNBUF_PUT(pnbuf); 854 return error; 855 } 856 857 /* 858 * policy: when creating the shadow directory in the 859 * upper layer, create it owned by the user who did 860 * the mount, group from parent directory, and mode 861 * 777 modified by umask (ie mostly identical to the 862 * mkdir syscall). (jsp, kb) 863 */ 864 865 vattr_null(&va); 866 va.va_type = VDIR; 867 va.va_mode = um->um_cmode; 868 869 vref(dvp); 870 error = VOP_MKDIR(dvp, vpp, &cn, &va); 871 PNBUF_PUT(pnbuf); 872 return error; 873 } 874 875 /* 876 * Create a whiteout entry in the upper layer. 877 * 878 * (um) points to the union mount structure for access to the 879 * the mounting process's credentials. 880 * (dvp) is the directory in which to create the whiteout. 881 * it is locked on entry and exit. 882 * (cnp) is the componentname to be created. 883 * (un) holds the path and its hash to be created. 884 */ 885 int 886 union_mkwhiteout(struct union_mount *um, struct vnode *dvp, 887 struct componentname *cnp, struct union_node *un) 888 { 889 int error; 890 struct componentname cn; 891 892 error = union_do_lookup(dvp, &cn, 893 (um->um_op == UNMNT_ABOVE ? cnp->cn_cred : um->um_cred), 894 un->un_path, un->un_hash); 895 if (error) 896 return error; 897 898 error = VOP_WHITEOUT(dvp, &cn, CREATE); 899 return error; 900 } 901 902 /* 903 * union_vn_create: creates and opens a new shadow file 904 * on the upper union layer. this function is similar 905 * in spirit to calling vn_open but it avoids calling namei(). 906 * the problem with calling namei is that a) it locks too many 907 * things, and b) it doesn't start at the "right" directory, 908 * whereas union_do_lookup is told where to start. 909 */ 910 int 911 union_vn_create(struct vnode **vpp, struct union_node *un, struct lwp *l) 912 { 913 struct vnode *vp; 914 kauth_cred_t cred = l->l_cred; 915 struct vattr vat; 916 struct vattr *vap = &vat; 917 int fmode = FFLAGS(O_WRONLY|O_CREAT|O_TRUNC|O_EXCL); 918 int error; 919 int cmode = UN_FILEMODE & ~l->l_proc->p_cwdi->cwdi_cmask; 920 struct componentname cn; 921 922 *vpp = NULLVP; 923 924 vn_lock(un->un_dirvp, LK_EXCLUSIVE | LK_RETRY); 925 926 error = union_do_lookup(un->un_dirvp, &cn, l->l_cred, 927 un->un_path, un->un_hash); 928 if (error) { 929 VOP_UNLOCK(un->un_dirvp); 930 return error; 931 } 932 933 /* 934 * Good - there was no race to create the file 935 * so go ahead and create it. The permissions 936 * on the file will be 0666 modified by the 937 * current user's umask. Access to the file, while 938 * it is unioned, will require access to the top *and* 939 * bottom files. Access when not unioned will simply 940 * require access to the top-level file. 941 * TODO: confirm choice of access permissions. 942 */ 943 vattr_null(vap); 944 vap->va_type = VREG; 945 vap->va_mode = cmode; 946 vref(un->un_dirvp); 947 error = VOP_CREATE(un->un_dirvp, &vp, &cn, vap); 948 if (error) 949 return error; 950 951 error = VOP_OPEN(vp, fmode, cred); 952 if (error) { 953 vput(vp); 954 return error; 955 } 956 957 vp->v_writecount++; 958 *vpp = vp; 959 return 0; 960 } 961 962 int 963 union_vn_close(struct vnode *vp, int fmode, kauth_cred_t cred, struct lwp *l) 964 { 965 966 if (fmode & FWRITE) 967 --vp->v_writecount; 968 return (VOP_CLOSE(vp, fmode, cred)); 969 } 970 971 void 972 union_removed_upper(struct union_node *un) 973 { 974 struct vnode *vp = UNIONTOV(un); 975 int hash; 976 977 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 978 #if 1 979 /* 980 * We do not set the uppervp to NULLVP here, because lowervp 981 * may also be NULLVP, so this routine would end up creating 982 * a bogus union node with no upper or lower VP (that causes 983 * pain in many places that assume at least one VP exists). 984 * Since we've removed this node from the cache hash chains, 985 * it won't be found again. When all current holders 986 * release it, union_inactive() will vgone() it. 987 */ 988 union_diruncache(un); 989 #else 990 union_newupper(un, NULLVP); 991 #endif 992 993 hash = UNION_HASH(un->un_uppervp, un->un_lowervp); 994 VOP_UNLOCK(vp); 995 996 mutex_enter(&uhash_lock); 997 if (un->un_cflags & UN_CACHED) { 998 un->un_cflags &= ~UN_CACHED; 999 LIST_REMOVE(un, un_cache); 1000 } 1001 mutex_exit(&uhash_lock); 1002 } 1003 1004 #if 0 1005 struct vnode * 1006 union_lowervp(struct vnode *vp) 1007 { 1008 struct union_node *un = VTOUNION(vp); 1009 1010 if ((un->un_lowervp != NULLVP) && 1011 (vp->v_type == un->un_lowervp->v_type)) { 1012 if (vget(un->un_lowervp, 0) == 0) 1013 return (un->un_lowervp); 1014 } 1015 1016 return (NULLVP); 1017 } 1018 #endif 1019 1020 /* 1021 * determine whether a whiteout is needed 1022 * during a remove/rmdir operation. 1023 */ 1024 int 1025 union_dowhiteout(struct union_node *un, kauth_cred_t cred) 1026 { 1027 struct vattr va; 1028 1029 if (un->un_lowervp != NULLVP) 1030 return (1); 1031 1032 if (VOP_GETATTR(un->un_uppervp, &va, cred) == 0 && 1033 (va.va_flags & OPAQUE)) 1034 return (1); 1035 1036 return (0); 1037 } 1038 1039 static void 1040 union_dircache_r(struct vnode *vp, struct vnode ***vppp, int *cntp) 1041 { 1042 struct union_node *un; 1043 1044 if (vp->v_op != union_vnodeop_p) { 1045 if (vppp) { 1046 vref(vp); 1047 *(*vppp)++ = vp; 1048 if (--(*cntp) == 0) 1049 panic("union: dircache table too small"); 1050 } else { 1051 (*cntp)++; 1052 } 1053 1054 return; 1055 } 1056 1057 un = VTOUNION(vp); 1058 if (un->un_uppervp != NULLVP) 1059 union_dircache_r(un->un_uppervp, vppp, cntp); 1060 if (un->un_lowervp != NULLVP) 1061 union_dircache_r(un->un_lowervp, vppp, cntp); 1062 } 1063 1064 struct vnode * 1065 union_dircache(struct vnode *vp, struct lwp *l) 1066 { 1067 int cnt; 1068 struct vnode *nvp = NULLVP; 1069 struct vnode **vpp; 1070 struct vnode **dircache; 1071 int error; 1072 1073 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 1074 dircache = VTOUNION(vp)->un_dircache; 1075 1076 nvp = NULLVP; 1077 1078 if (dircache == 0) { 1079 cnt = 0; 1080 union_dircache_r(vp, 0, &cnt); 1081 cnt++; 1082 dircache = (struct vnode **) 1083 malloc(cnt * sizeof(struct vnode *), 1084 M_TEMP, M_WAITOK); 1085 vpp = dircache; 1086 union_dircache_r(vp, &vpp, &cnt); 1087 VTOUNION(vp)->un_dircache = dircache; 1088 *vpp = NULLVP; 1089 vpp = dircache + 1; 1090 } else { 1091 vpp = dircache; 1092 do { 1093 if (*vpp++ == VTOUNION(vp)->un_uppervp) 1094 break; 1095 } while (*vpp != NULLVP); 1096 } 1097 1098 if (*vpp == NULLVP) 1099 goto out; 1100 1101 vn_lock(*vpp, LK_EXCLUSIVE | LK_RETRY); 1102 vref(*vpp); 1103 error = union_allocvp(&nvp, vp->v_mount, NULLVP, NULLVP, 0, *vpp, NULLVP, 0); 1104 if (!error) { 1105 VTOUNION(vp)->un_dircache = 0; 1106 VTOUNION(nvp)->un_dircache = dircache; 1107 } 1108 1109 out: 1110 VOP_UNLOCK(vp); 1111 return (nvp); 1112 } 1113 1114 void 1115 union_diruncache(struct union_node *un) 1116 { 1117 struct vnode **vpp; 1118 1119 KASSERT(VOP_ISLOCKED(UNIONTOV(un)) == LK_EXCLUSIVE); 1120 if (un->un_dircache != 0) { 1121 for (vpp = un->un_dircache; *vpp != NULLVP; vpp++) 1122 vrele(*vpp); 1123 free(un->un_dircache, M_TEMP); 1124 un->un_dircache = 0; 1125 } 1126 } 1127 1128 /* 1129 * Check whether node can rmdir (check empty). 1130 */ 1131 int 1132 union_check_rmdir(struct union_node *un, kauth_cred_t cred) 1133 { 1134 int dirlen, eofflag, error; 1135 char *dirbuf; 1136 struct vattr va; 1137 struct vnode *tvp; 1138 struct dirent *dp, *edp; 1139 struct componentname cn; 1140 struct iovec aiov; 1141 struct uio auio; 1142 1143 KASSERT(un->un_uppervp != NULL); 1144 1145 /* Check upper for being opaque. */ 1146 KASSERT(VOP_ISLOCKED(un->un_uppervp)); 1147 error = VOP_GETATTR(un->un_uppervp, &va, cred); 1148 if (error || (va.va_flags & OPAQUE)) 1149 return error; 1150 1151 if (un->un_lowervp == NULL) 1152 return 0; 1153 1154 /* Check lower for being empty. */ 1155 vn_lock(un->un_lowervp, LK_SHARED | LK_RETRY); 1156 error = VOP_GETATTR(un->un_lowervp, &va, cred); 1157 if (error) { 1158 VOP_UNLOCK(un->un_lowervp); 1159 return error; 1160 } 1161 dirlen = va.va_blocksize; 1162 dirbuf = kmem_alloc(dirlen, KM_SLEEP); 1163 if (dirbuf == NULL) { 1164 VOP_UNLOCK(un->un_lowervp); 1165 return ENOMEM; 1166 } 1167 /* error = 0; */ 1168 eofflag = 0; 1169 auio.uio_offset = 0; 1170 do { 1171 aiov.iov_len = dirlen; 1172 aiov.iov_base = dirbuf; 1173 auio.uio_iov = &aiov; 1174 auio.uio_iovcnt = 1; 1175 auio.uio_resid = aiov.iov_len; 1176 auio.uio_rw = UIO_READ; 1177 UIO_SETUP_SYSSPACE(&auio); 1178 error = VOP_READDIR(un->un_lowervp, &auio, cred, &eofflag, 1179 NULL, NULL); 1180 if (error) 1181 break; 1182 edp = (struct dirent *)&dirbuf[dirlen - auio.uio_resid]; 1183 for (dp = (struct dirent *)dirbuf; 1184 error == 0 && dp < edp; 1185 dp = (struct dirent *)((char *)dp + dp->d_reclen)) { 1186 if (dp->d_reclen == 0) { 1187 error = ENOTEMPTY; 1188 break; 1189 } 1190 if (dp->d_type == DT_WHT || 1191 (dp->d_namlen == 1 && dp->d_name[0] == '.') || 1192 (dp->d_namlen == 2 && !memcmp(dp->d_name, "..", 2))) 1193 continue; 1194 /* Check for presence in the upper layer. */ 1195 cn.cn_nameiop = LOOKUP; 1196 cn.cn_flags = ISLASTCN | RDONLY; 1197 cn.cn_cred = cred; 1198 cn.cn_nameptr = dp->d_name; 1199 cn.cn_namelen = dp->d_namlen; 1200 cn.cn_hash = 0; 1201 error = VOP_LOOKUP(un->un_uppervp, &tvp, &cn); 1202 if (error == ENOENT && (cn.cn_flags & ISWHITEOUT)) { 1203 error = 0; 1204 continue; 1205 } 1206 if (error == 0) 1207 vput(tvp); 1208 error = ENOTEMPTY; 1209 } 1210 } while (error == 0 && !eofflag); 1211 kmem_free(dirbuf, dirlen); 1212 VOP_UNLOCK(un->un_lowervp); 1213 1214 return error; 1215 } 1216 1217 /* 1218 * This hook is called from vn_readdir() to switch to lower directory 1219 * entry after the upper directory is read. 1220 */ 1221 int 1222 union_readdirhook(struct vnode **vpp, struct file *fp, struct lwp *l) 1223 { 1224 struct vnode *vp = *vpp, *lvp; 1225 struct vattr va; 1226 int error; 1227 1228 if (vp->v_op != union_vnodeop_p) 1229 return (0); 1230 1231 /* 1232 * If the directory is opaque, 1233 * then don't show lower entries 1234 */ 1235 vn_lock(vp, LK_SHARED | LK_RETRY); 1236 error = VOP_GETATTR(vp, &va, fp->f_cred); 1237 VOP_UNLOCK(vp); 1238 if (error || (va.va_flags & OPAQUE)) 1239 return error; 1240 1241 if ((lvp = union_dircache(vp, l)) == NULLVP) 1242 return (0); 1243 1244 error = VOP_OPEN(lvp, FREAD, fp->f_cred); 1245 if (error) { 1246 vput(lvp); 1247 return (error); 1248 } 1249 VOP_UNLOCK(lvp); 1250 fp->f_data = lvp; 1251 fp->f_offset = 0; 1252 error = vn_close(vp, FREAD, fp->f_cred); 1253 if (error) 1254 return (error); 1255 *vpp = lvp; 1256 return (0); 1257 } 1258