1 /*- 2 * Copyright (c) 1992, 1993, 1994, 1995 Jan-Simon Pendry. 3 * Copyright (c) 1992, 1993, 1994, 1995 4 * The Regents of the University of California. 5 * Copyright (c) 2005, 2006 Masanori Ozawa <ozawa@ongs.co.jp>, ONGS Inc. 6 * Copyright (c) 2006 Daichi Goto <daichi@freebsd.org> 7 * All rights reserved. 8 * 9 * This code is derived from software contributed to Berkeley by 10 * Jan-Simon Pendry. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * @(#)union_vnops.c 8.32 (Berkeley) 6/23/95 37 * $FreeBSD: src/sys/fs/unionfs/union_vnops.c,v 1.152 2008/01/13 14:44:06 attilio Exp $ 38 * 39 */ 40 41 #include <sys/param.h> 42 #include <sys/systm.h> 43 #include <sys/conf.h> 44 #include <sys/kernel.h> 45 #include <sys/lock.h> 46 #include <sys/malloc.h> 47 #include <sys/mount.h> 48 #include <sys/mutex.h> 49 #include <sys/namei.h> 50 #include <sys/sysctl.h> 51 #include <sys/vnode.h> 52 #include <sys/buf.h> 53 #include <sys/fcntl.h> 54 #include <sys/stat.h> 55 #include <sys/dirent.h> 56 #include <sys/proc.h> 57 58 #include <fs/unionfs/unionfs.h> 59 60 #if 0 61 #define UNIONFS_INTERNAL_DEBUG(msg, args...) printf(msg, ## args) 62 #define UNIONFS_IDBG_RENAME 63 #else 64 #define UNIONFS_INTERNAL_DEBUG(msg, args...) 65 #endif 66 67 static int 68 unionfs_lookup(void *v) 69 { 70 struct vop_lookup_args *ap = v; 71 int iswhiteout; 72 int lockflag; 73 int error , uerror, lerror; 74 u_long nameiop; 75 u_long cnflags, cnflagsbk; 76 struct unionfs_node *dunp; 77 struct vnode *dvp, *udvp, *ldvp, *vp, *uvp, *lvp, *dtmpvp; 78 struct vattr va; 79 struct componentname *cnp; 80 81 iswhiteout = 0; 82 lockflag = 0; 83 error = uerror = lerror = ENOENT; 84 cnp = ap->a_cnp; 85 nameiop = cnp->cn_nameiop; 86 cnflags = cnp->cn_flags; 87 dvp = ap->a_dvp; 88 dunp = VTOUNIONFS(dvp); 89 udvp = dunp->un_uppervp; 90 ldvp = dunp->un_lowervp; 91 vp = uvp = lvp = NULLVP; 92 *(ap->a_vpp) = NULLVP; 93 94 UNIONFS_INTERNAL_DEBUG("unionfs_lookup: enter: nameiop=%ld, flags=%lx, path=%s\n", nameiop, cnflags, cnp->cn_nameptr); 95 96 if (dvp->v_type != VDIR) 97 return (ENOTDIR); 98 99 /* 100 * If read-only and op is not LOOKUP, will return EROFS. 101 */ 102 if ((cnflags & ISLASTCN) && 103 (dvp->v_mount->mnt_flag & MNT_RDONLY) && 104 LOOKUP != nameiop) 105 return (EROFS); 106 107 /* 108 * lookup dotdot 109 */ 110 if (cnflags & ISDOTDOT) { 111 if (LOOKUP != nameiop && udvp == NULLVP) 112 return (EROFS); 113 114 if (udvp != NULLVP) { 115 dtmpvp = udvp; 116 if (ldvp != NULLVP) 117 VOP_UNLOCK(ldvp); 118 } 119 else 120 dtmpvp = ldvp; 121 122 error = VOP_LOOKUP(dtmpvp, &vp, cnp); 123 124 if (dtmpvp == udvp && ldvp != NULLVP) { 125 VOP_UNLOCK(udvp); 126 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY); 127 } 128 129 if (error == 0) { 130 /* 131 * Exchange lock and reference from vp to 132 * dunp->un_dvp. vp is upper/lower vnode, but it 133 * will need to return the unionfs vnode. 134 */ 135 if (nameiop == DELETE || nameiop == RENAME) 136 VOP_UNLOCK(vp); 137 vrele(vp); 138 139 VOP_UNLOCK(dvp); 140 *(ap->a_vpp) = dunp->un_dvp; 141 vref(dunp->un_dvp); 142 143 vn_lock(dunp->un_dvp, LK_EXCLUSIVE | LK_RETRY); 144 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY); 145 } else if (error == ENOENT && (cnflags & MAKEENTRY) && 146 nameiop != CREATE) 147 cache_enter(dvp, NULLVP, cnp); 148 149 UNIONFS_INTERNAL_DEBUG("unionfs_lookup: leave (%d)\n", error); 150 151 return (error); 152 } 153 154 /* 155 * lookup upper layer 156 */ 157 if (udvp != NULLVP) { 158 uerror = VOP_LOOKUP(udvp, &uvp, cnp); 159 160 if (uerror == 0) { 161 if (udvp == uvp) { /* is dot */ 162 vrele(uvp); 163 *(ap->a_vpp) = dvp; 164 vref(dvp); 165 166 UNIONFS_INTERNAL_DEBUG("unionfs_lookup: leave (%d)\n", uerror); 167 168 return (uerror); 169 } 170 } 171 172 /* check whiteout */ 173 if (uerror == ENOENT || uerror == EJUSTRETURN) 174 if (cnp->cn_flags & ISWHITEOUT) 175 iswhiteout = 1; /* don't lookup lower */ 176 if (iswhiteout == 0 && ldvp != NULLVP) 177 if (VOP_GETATTR(udvp, &va, cnp->cn_cred) == 0 && 178 (va.va_flags & OPAQUE)) 179 iswhiteout = 1; /* don't lookup lower */ 180 #if 0 181 UNIONFS_INTERNAL_DEBUG("unionfs_lookup: debug: whiteout=%d, path=%s\n", iswhiteout, cnp->cn_nameptr); 182 #endif 183 } 184 185 /* 186 * lookup lower layer 187 */ 188 if (ldvp != NULLVP && !(cnflags & DOWHITEOUT) && iswhiteout == 0) { 189 /* always op is LOOKUP */ 190 cnp->cn_nameiop = LOOKUP; 191 cnflagsbk = cnp->cn_flags; 192 cnp->cn_flags = cnflags; 193 194 lerror = VOP_LOOKUP(ldvp, &lvp, cnp); 195 196 cnp->cn_nameiop = nameiop; 197 if (udvp != NULLVP && (uerror == 0 || uerror == EJUSTRETURN)) 198 cnp->cn_flags = cnflagsbk; 199 200 if (lerror == 0) { 201 if (ldvp == lvp) { /* is dot */ 202 if (uvp != NULLVP) 203 vrele(uvp); /* no need? */ 204 vrele(lvp); 205 *(ap->a_vpp) = dvp; 206 vref(dvp); 207 208 UNIONFS_INTERNAL_DEBUG("unionfs_lookup: leave (%d)\n", lerror); 209 if (uvp != NULL) 210 VOP_UNLOCK(uvp); 211 return (lerror); 212 } 213 } 214 } 215 216 /* 217 * check lookup result 218 */ 219 if (uvp == NULLVP && lvp == NULLVP) { 220 UNIONFS_INTERNAL_DEBUG("unionfs_lookup: leave (%d)\n", 221 (udvp != NULLVP ? uerror : lerror)); 222 return (udvp != NULLVP ? uerror : lerror); 223 } 224 225 /* 226 * check vnode type 227 */ 228 if (uvp != NULLVP && lvp != NULLVP && uvp->v_type != lvp->v_type) { 229 vput(lvp); 230 lvp = NULLVP; 231 } 232 233 /* 234 * check shadow dir 235 */ 236 if (uerror != 0 && uerror != EJUSTRETURN && udvp != NULLVP && 237 lerror == 0 && lvp != NULLVP && lvp->v_type == VDIR && 238 !(dvp->v_mount->mnt_flag & MNT_RDONLY) && 239 (1 < cnp->cn_namelen || '.' != *(cnp->cn_nameptr))) { 240 /* get unionfs vnode in order to create a new shadow dir. */ 241 error = unionfs_nodeget(dvp->v_mount, NULLVP, lvp, dvp, &vp, 242 cnp); 243 if (error != 0) 244 goto unionfs_lookup_out; 245 error = unionfs_mkshadowdir(MOUNTTOUNIONFSMOUNT(dvp->v_mount), 246 udvp, VTOUNIONFS(vp), cnp); 247 if (error != 0) { 248 UNIONFSDEBUG("unionfs_lookup: Unable to create shadow dir."); 249 vput(vp); 250 goto unionfs_lookup_out; 251 } 252 } 253 /* 254 * get unionfs vnode. 255 */ 256 else { 257 if (uvp != NULLVP) 258 error = uerror; 259 else 260 error = lerror; 261 if (error != 0) 262 goto unionfs_lookup_out; 263 error = unionfs_nodeget(dvp->v_mount, uvp, lvp, dvp, &vp, cnp); 264 if (error != 0) { 265 UNIONFSDEBUG("unionfs_lookup: Unable to create unionfs vnode."); 266 goto unionfs_lookup_out; 267 } 268 } 269 270 *(ap->a_vpp) = vp; 271 272 if (cnflags & MAKEENTRY) 273 cache_enter(dvp, vp, cnp); 274 275 /* XXXAD lock status on error */ 276 unionfs_lookup_out: 277 if (uvp != NULLVP) 278 vrele(uvp); 279 if (lvp != NULLVP) 280 vrele(lvp); 281 282 if (error == ENOENT && (cnflags & MAKEENTRY) && nameiop != CREATE) 283 cache_enter(dvp, NULLVP, cnp); 284 285 UNIONFS_INTERNAL_DEBUG("unionfs_lookup: leave (%d)\n", error); 286 287 return (error); 288 } 289 290 static int 291 unionfs_create(void *v) 292 { 293 struct vop_create_args *ap = v; 294 struct unionfs_node *dunp; 295 struct componentname *cnp; 296 struct vnode *udvp; 297 struct vnode *vp; 298 int error; 299 300 UNIONFS_INTERNAL_DEBUG("unionfs_create: enter\n"); 301 302 dunp = VTOUNIONFS(ap->a_dvp); 303 cnp = ap->a_cnp; 304 udvp = dunp->un_uppervp; 305 error = EROFS; 306 307 if (udvp != NULLVP) { 308 if ((error = VOP_CREATE(udvp, &vp, cnp, ap->a_vap)) == 0) { 309 error = unionfs_nodeget(ap->a_dvp->v_mount, vp, NULLVP, 310 ap->a_dvp, ap->a_vpp, cnp); 311 if (error) { 312 vput(vp); 313 } else { 314 vrele(vp); 315 } 316 } 317 } 318 319 UNIONFS_INTERNAL_DEBUG("unionfs_create: leave (%d)\n", error); 320 321 return (error); 322 } 323 324 static int 325 unionfs_whiteout(void *v) 326 { 327 struct vop_whiteout_args *ap = v; 328 struct unionfs_node *dunp; 329 struct componentname *cnp; 330 struct vnode *udvp; 331 int error; 332 333 UNIONFS_INTERNAL_DEBUG("unionfs_whiteout: enter\n"); 334 335 dunp = VTOUNIONFS(ap->a_dvp); 336 cnp = ap->a_cnp; 337 udvp = dunp->un_uppervp; 338 error = EOPNOTSUPP; 339 340 if (udvp != NULLVP) { 341 switch (ap->a_flags) { 342 case CREATE: 343 case DELETE: 344 case LOOKUP: 345 error = VOP_WHITEOUT(udvp, cnp, ap->a_flags); 346 break; 347 default: 348 error = EINVAL; 349 break; 350 } 351 } 352 353 UNIONFS_INTERNAL_DEBUG("unionfs_whiteout: leave (%d)\n", error); 354 355 return (error); 356 } 357 358 static int 359 unionfs_mknod(void *v) 360 { 361 struct vop_mknod_args *ap = v; 362 struct unionfs_node *dunp; 363 struct componentname *cnp; 364 struct vnode *udvp; 365 struct vnode *vp; 366 int error; 367 368 UNIONFS_INTERNAL_DEBUG("unionfs_mknod: enter\n"); 369 370 dunp = VTOUNIONFS(ap->a_dvp); 371 cnp = ap->a_cnp; 372 udvp = dunp->un_uppervp; 373 error = EROFS; 374 375 if (udvp != NULLVP) { 376 if ((error = VOP_MKNOD(udvp, &vp, cnp, ap->a_vap)) == 0) { 377 error = unionfs_nodeget(ap->a_dvp->v_mount, vp, NULLVP, 378 ap->a_dvp, ap->a_vpp, cnp); 379 if (error) { 380 vput(vp); 381 } else { 382 vrele(vp); 383 } 384 } 385 } 386 387 UNIONFS_INTERNAL_DEBUG("unionfs_mknod: leave (%d)\n", error); 388 389 return (error); 390 } 391 392 static int 393 unionfs_open(void *v) 394 { 395 struct vop_open_args *ap = v; 396 int error; 397 struct unionfs_node *unp; 398 struct unionfs_node_status *unsp; 399 struct vnode *uvp; 400 struct vnode *lvp; 401 struct vnode *targetvp; 402 kauth_cred_t cred; 403 404 UNIONFS_INTERNAL_DEBUG("unionfs_open: enter\n"); 405 406 error = 0; 407 unp = VTOUNIONFS(ap->a_vp); 408 uvp = unp->un_uppervp; 409 lvp = unp->un_lowervp; 410 targetvp = NULLVP; 411 cred = ap->a_cred; 412 413 unionfs_get_node_status(unp, &unsp); 414 415 if (unsp->uns_lower_opencnt > 0 || unsp->uns_upper_opencnt > 0) { 416 /* vnode is already opend. */ 417 if (unsp->uns_upper_opencnt > 0) 418 targetvp = uvp; 419 else 420 targetvp = lvp; 421 422 if (targetvp == lvp && 423 (ap->a_mode & FWRITE) && lvp->v_type == VREG) 424 targetvp = NULLVP; 425 } 426 if (targetvp == NULLVP) { 427 if (uvp == NULLVP) { 428 if ((ap->a_mode & FWRITE) && lvp->v_type == VREG) { 429 error = unionfs_copyfile(unp, 430 !(ap->a_mode & O_TRUNC), cred); 431 if (error != 0) 432 goto unionfs_open_abort; 433 targetvp = uvp = unp->un_uppervp; 434 } else 435 targetvp = lvp; 436 } else 437 targetvp = uvp; 438 } 439 440 error = VOP_OPEN(targetvp, ap->a_mode, cred); 441 if (error == 0) { 442 if (targetvp == uvp) { 443 if (uvp->v_type == VDIR && lvp != NULLVP && 444 unsp->uns_lower_opencnt <= 0) { 445 /* open lower for readdir */ 446 error = VOP_OPEN(lvp, FREAD, cred); 447 if (error != 0) { 448 VOP_CLOSE(uvp, ap->a_mode, cred); 449 goto unionfs_open_abort; 450 } 451 unsp->uns_node_flag |= UNS_OPENL_4_READDIR; 452 unsp->uns_lower_opencnt++; 453 } 454 unsp->uns_upper_opencnt++; 455 } else { 456 unsp->uns_lower_opencnt++; 457 unsp->uns_lower_openmode = ap->a_mode; 458 } 459 } 460 461 unionfs_open_abort: 462 if (error != 0) 463 unionfs_tryrem_node_status(unp, unsp); 464 465 UNIONFS_INTERNAL_DEBUG("unionfs_open: leave (%d)\n", error); 466 467 return (error); 468 } 469 470 static int 471 unionfs_close(void *v) 472 { 473 struct vop_close_args *ap = v; 474 int error; 475 struct unionfs_node *unp; 476 struct unionfs_node_status *unsp; 477 kauth_cred_t cred; 478 struct vnode *ovp; 479 480 UNIONFS_INTERNAL_DEBUG("unionfs_close: enter\n"); 481 482 KASSERT(VOP_ISLOCKED(ap->a_vp) == LK_EXCLUSIVE); 483 unp = VTOUNIONFS(ap->a_vp); 484 cred = ap->a_cred; 485 486 unionfs_get_node_status(unp, &unsp); 487 488 if (unsp->uns_lower_opencnt <= 0 && unsp->uns_upper_opencnt <= 0) { 489 #ifdef DIAGNOSTIC 490 printf("unionfs_close: warning: open count is 0\n"); 491 #endif 492 if (unp->un_uppervp != NULLVP) 493 ovp = unp->un_uppervp; 494 else 495 ovp = unp->un_lowervp; 496 } else if (unsp->uns_upper_opencnt > 0) 497 ovp = unp->un_uppervp; 498 else 499 ovp = unp->un_lowervp; 500 501 error = VOP_CLOSE(ovp, ap->a_fflag, cred); 502 503 if (error != 0) 504 goto unionfs_close_abort; 505 506 if (ovp == unp->un_uppervp) { 507 unsp->uns_upper_opencnt--; 508 if (unsp->uns_upper_opencnt == 0) { 509 if (unsp->uns_node_flag & UNS_OPENL_4_READDIR) { 510 VOP_CLOSE(unp->un_lowervp, FREAD, cred); 511 unsp->uns_node_flag &= ~UNS_OPENL_4_READDIR; 512 unsp->uns_lower_opencnt--; 513 } 514 } 515 } else 516 unsp->uns_lower_opencnt--; 517 518 unionfs_close_abort: 519 unionfs_tryrem_node_status(unp, unsp); 520 521 UNIONFS_INTERNAL_DEBUG("unionfs_close: leave (%d)\n", error); 522 523 return (error); 524 } 525 526 /* 527 * Check the access mode toward shadow file/dir. 528 */ 529 static int 530 unionfs_check_corrected_access(u_short mode, struct vattr *va, kauth_cred_t cred) 531 { 532 int result; 533 int error; 534 uid_t uid; /* upper side vnode's uid */ 535 gid_t gid; /* upper side vnode's gid */ 536 u_short vmode; /* upper side vnode's mode */ 537 u_short mask; 538 539 mask = 0; 540 uid = va->va_uid; 541 gid = va->va_gid; 542 vmode = va->va_mode; 543 544 /* check owner */ 545 if (kauth_cred_getuid(cred) == uid) { 546 if (mode & VEXEC) 547 mask |= S_IXUSR; 548 if (mode & VREAD) 549 mask |= S_IRUSR; 550 if (mode & VWRITE) 551 mask |= S_IWUSR; 552 return ((vmode & mask) == mask ? 0 : EACCES); 553 } 554 555 /* check group */ 556 error = kauth_cred_ismember_gid(cred, gid, &result); 557 if (error != 0) 558 return error; 559 if (result) { 560 if (mode & VEXEC) 561 mask |= S_IXGRP; 562 if (mode & VREAD) 563 mask |= S_IRGRP; 564 if (mode & VWRITE) 565 mask |= S_IWGRP; 566 return ((vmode & mask) == mask ? 0 : EACCES); 567 } 568 569 /* check other */ 570 if (mode & VEXEC) 571 mask |= S_IXOTH; 572 if (mode & VREAD) 573 mask |= S_IROTH; 574 if (mode & VWRITE) 575 mask |= S_IWOTH; 576 577 return ((vmode & mask) == mask ? 0 : EACCES); 578 } 579 580 static int 581 unionfs_access(void *v) 582 { 583 struct vop_access_args *ap = v; 584 struct unionfs_mount *ump; 585 struct unionfs_node *unp; 586 struct vnode *uvp; 587 struct vnode *lvp; 588 struct vattr va; 589 int mode; 590 int error; 591 592 UNIONFS_INTERNAL_DEBUG("unionfs_access: enter\n"); 593 594 ump = MOUNTTOUNIONFSMOUNT(ap->a_vp->v_mount); 595 unp = VTOUNIONFS(ap->a_vp); 596 uvp = unp->un_uppervp; 597 lvp = unp->un_lowervp; 598 mode = ap->a_mode; 599 error = EACCES; 600 601 if ((mode & VWRITE) && 602 (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)) { 603 switch (ap->a_vp->v_type) { 604 case VREG: 605 case VDIR: 606 case VLNK: 607 return (EROFS); 608 default: 609 break; 610 } 611 } 612 613 if (uvp != NULLVP) { 614 error = VOP_ACCESS(uvp, mode, ap->a_cred); 615 616 UNIONFS_INTERNAL_DEBUG("unionfs_access: leave (%d)\n", error); 617 618 return (error); 619 } 620 621 if (lvp != NULLVP) { 622 if (mode & VWRITE) { 623 if (ump->um_uppervp->v_mount->mnt_flag & MNT_RDONLY) { 624 switch (ap->a_vp->v_type) { 625 case VREG: 626 case VDIR: 627 case VLNK: 628 return (EROFS); 629 default: 630 break; 631 } 632 } else if (ap->a_vp->v_type == VREG || ap->a_vp->v_type == VDIR) { 633 /* check shadow file/dir */ 634 if (ump->um_copymode != UNIONFS_TRANSPARENT) { 635 error = unionfs_create_uppervattr(ump, 636 lvp, &va, ap->a_cred); 637 if (error != 0) 638 return (error); 639 640 error = unionfs_check_corrected_access( 641 mode, &va, ap->a_cred); 642 if (error != 0) 643 return (error); 644 } 645 } 646 mode &= ~VWRITE; 647 mode |= VREAD; /* will copy to upper */ 648 } 649 error = VOP_ACCESS(lvp, mode, ap->a_cred); 650 } 651 652 UNIONFS_INTERNAL_DEBUG("unionfs_access: leave (%d)\n", error); 653 654 return (error); 655 } 656 657 static int 658 unionfs_getattr(void *v) 659 { 660 struct vop_getattr_args *ap = v; 661 int error; 662 struct unionfs_node *unp; 663 struct unionfs_mount *ump; 664 struct vnode *uvp; 665 struct vnode *lvp; 666 struct vattr va; 667 668 UNIONFS_INTERNAL_DEBUG("unionfs_getattr: enter\n"); 669 670 unp = VTOUNIONFS(ap->a_vp); 671 ump = MOUNTTOUNIONFSMOUNT(ap->a_vp->v_mount); 672 uvp = unp->un_uppervp; 673 lvp = unp->un_lowervp; 674 675 if (uvp != NULLVP) { 676 if ((error = VOP_GETATTR(uvp, ap->a_vap, ap->a_cred)) == 0) 677 ap->a_vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsid; 678 679 UNIONFS_INTERNAL_DEBUG("unionfs_getattr: leave mode=%o, uid=%d, gid=%d (%d)\n", 680 ap->a_vap->va_mode, ap->a_vap->va_uid, 681 ap->a_vap->va_gid, error); 682 683 return (error); 684 } 685 686 error = VOP_GETATTR(lvp, ap->a_vap, ap->a_cred); 687 688 if (error == 0 && !(ump->um_uppervp->v_mount->mnt_flag & MNT_RDONLY)) { 689 /* correct the attr toward shadow file/dir. */ 690 if (ap->a_vp->v_type == VREG || ap->a_vp->v_type == VDIR) { 691 unionfs_create_uppervattr_core(ump, ap->a_vap, &va); 692 ap->a_vap->va_mode = va.va_mode; 693 ap->a_vap->va_uid = va.va_uid; 694 ap->a_vap->va_gid = va.va_gid; 695 } 696 } 697 698 if (error == 0) 699 ap->a_vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsid; 700 701 UNIONFS_INTERNAL_DEBUG("unionfs_getattr: leave mode=%o, uid=%d, gid=%d (%d)\n", 702 ap->a_vap->va_mode, ap->a_vap->va_uid, ap->a_vap->va_gid, error); 703 704 return (error); 705 } 706 707 static int 708 unionfs_setattr(void *v) 709 { 710 struct vop_setattr_args *ap = v; 711 int error; 712 struct unionfs_node *unp; 713 struct vnode *uvp; 714 struct vnode *lvp; 715 struct vattr *vap; 716 717 UNIONFS_INTERNAL_DEBUG("unionfs_setattr: enter\n"); 718 719 error = EROFS; 720 unp = VTOUNIONFS(ap->a_vp); 721 uvp = unp->un_uppervp; 722 lvp = unp->un_lowervp; 723 vap = ap->a_vap; 724 725 if ((ap->a_vp->v_mount->mnt_flag & MNT_RDONLY) && 726 (vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL || 727 vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL || 728 vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL)) 729 return (EROFS); 730 731 if (uvp == NULLVP && lvp->v_type == VREG) { 732 error = unionfs_copyfile(unp, (vap->va_size != 0), 733 ap->a_cred); 734 if (error != 0) 735 return (error); 736 uvp = unp->un_uppervp; 737 } 738 739 if (uvp != NULLVP) 740 error = VOP_SETATTR(uvp, vap, ap->a_cred); 741 742 UNIONFS_INTERNAL_DEBUG("unionfs_setattr: leave (%d)\n", error); 743 744 return (error); 745 } 746 747 static int 748 unionfs_read(void *v) 749 { 750 struct vop_read_args *ap = v; 751 int error; 752 struct unionfs_node *unp; 753 struct vnode *tvp; 754 755 /* UNIONFS_INTERNAL_DEBUG("unionfs_read: enter\n"); */ 756 757 unp = VTOUNIONFS(ap->a_vp); 758 tvp = (unp->un_uppervp != NULLVP ? unp->un_uppervp : unp->un_lowervp); 759 760 error = VOP_READ(tvp, ap->a_uio, ap->a_ioflag, ap->a_cred); 761 762 /* UNIONFS_INTERNAL_DEBUG("unionfs_read: leave (%d)\n", error); */ 763 764 return (error); 765 } 766 767 static int 768 unionfs_write(void *v) 769 { 770 struct vop_write_args *ap = v; 771 int error; 772 struct unionfs_node *unp; 773 struct vnode *tvp; 774 775 /* UNIONFS_INTERNAL_DEBUG("unionfs_write: enter\n"); */ 776 777 unp = VTOUNIONFS(ap->a_vp); 778 tvp = (unp->un_uppervp != NULLVP ? unp->un_uppervp : unp->un_lowervp); 779 780 error = VOP_WRITE(tvp, ap->a_uio, ap->a_ioflag, ap->a_cred); 781 782 /* UNIONFS_INTERNAL_DEBUG("unionfs_write: leave (%d)\n", error); */ 783 784 return (error); 785 } 786 787 static int 788 unionfs_ioctl(void *v) 789 { 790 struct vop_ioctl_args *ap = v; 791 int error; 792 struct unionfs_node *unp; 793 struct unionfs_node_status *unsp; 794 struct vnode *ovp; 795 796 UNIONFS_INTERNAL_DEBUG("unionfs_ioctl: enter\n"); 797 798 vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY); 799 unp = VTOUNIONFS(ap->a_vp); 800 unionfs_get_node_status(unp, &unsp); 801 ovp = (unsp->uns_upper_opencnt ? unp->un_uppervp : unp->un_lowervp); 802 unionfs_tryrem_node_status(unp, unsp); 803 VOP_UNLOCK(ap->a_vp); 804 805 if (ovp == NULLVP) 806 return (EBADF); 807 808 error = VOP_IOCTL(ovp, ap->a_command, ap->a_data, ap->a_fflag, 809 ap->a_cred); 810 811 UNIONFS_INTERNAL_DEBUG("unionfs_ioctl: lease (%d)\n", error); 812 813 return (error); 814 } 815 816 static int 817 unionfs_poll(void *v) 818 { 819 struct vop_poll_args *ap = v; 820 struct unionfs_node *unp; 821 struct unionfs_node_status *unsp; 822 struct vnode *ovp; 823 824 vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY); 825 unp = VTOUNIONFS(ap->a_vp); 826 unionfs_get_node_status(unp, &unsp); 827 ovp = (unsp->uns_upper_opencnt ? unp->un_uppervp : unp->un_lowervp); 828 unionfs_tryrem_node_status(unp, unsp); 829 VOP_UNLOCK(ap->a_vp); 830 831 if (ovp == NULLVP) 832 return (EBADF); 833 834 return (VOP_POLL(ovp, ap->a_events)); 835 } 836 837 static int 838 unionfs_fsync(void *v) 839 { 840 struct vop_fsync_args *ap = v; 841 struct unionfs_node *unp; 842 struct unionfs_node_status *unsp; 843 struct vnode *ovp; 844 845 unp = VTOUNIONFS(ap->a_vp); 846 unionfs_get_node_status(unp, &unsp); 847 ovp = (unsp->uns_upper_opencnt ? unp->un_uppervp : unp->un_lowervp); 848 unionfs_tryrem_node_status(unp, unsp); 849 850 if (ovp == NULLVP) 851 return (EBADF); 852 853 return (VOP_FSYNC(ovp, ap->a_cred, ap->a_flags, ap->a_offlo, ap->a_offhi)); 854 } 855 856 static int 857 unionfs_remove(void *v) 858 { 859 struct vop_remove_args *ap = v; 860 int error; 861 struct unionfs_node *dunp; 862 struct unionfs_node *unp; 863 struct unionfs_mount *ump; 864 struct vnode *udvp; 865 struct vnode *uvp; 866 struct vnode *lvp; 867 struct componentname *cnp; 868 869 UNIONFS_INTERNAL_DEBUG("unionfs_remove: enter\n"); 870 871 error = 0; 872 dunp = VTOUNIONFS(ap->a_dvp); 873 unp = VTOUNIONFS(ap->a_vp); 874 udvp = dunp->un_uppervp; 875 uvp = unp->un_uppervp; 876 lvp = unp->un_lowervp; 877 cnp = ap->a_cnp; 878 879 if (udvp == NULLVP) 880 return (EROFS); 881 882 if (uvp != NULLVP) { 883 ump = MOUNTTOUNIONFSMOUNT(ap->a_vp->v_mount); 884 if (ump->um_whitemode == UNIONFS_WHITE_ALWAYS || lvp != NULLVP) 885 cnp->cn_flags |= DOWHITEOUT; 886 error = VOP_REMOVE(udvp, uvp, cnp); 887 } else if (lvp != NULLVP) 888 error = unionfs_mkwhiteout(udvp, cnp, unp->un_path); 889 890 UNIONFS_INTERNAL_DEBUG("unionfs_remove: leave (%d)\n", error); 891 892 return (error); 893 } 894 895 static int 896 unionfs_link(void *v) 897 { 898 #if 0 899 struct vop_link_args *ap = v; 900 int error; 901 int needrelookup; 902 struct unionfs_node *dunp; 903 struct unionfs_node *unp; 904 struct vnode *udvp; 905 struct vnode *uvp; 906 struct componentname *cnp; 907 908 UNIONFS_INTERNAL_DEBUG("unionfs_link: enter\n"); 909 910 error = 0; 911 needrelookup = 0; 912 dunp = VTOUNIONFS(ap->a_tdvp); 913 unp = NULL; 914 udvp = dunp->un_uppervp; 915 uvp = NULLVP; 916 cnp = ap->a_cnp; 917 918 if (udvp == NULLVP) 919 return (EROFS); 920 921 if (ap->a_vp->v_op != unionfs_vnodeop_p) 922 uvp = ap->a_vp; 923 else { 924 unp = VTOUNIONFS(ap->a_vp); 925 926 if (unp->un_uppervp == NULLVP) { 927 if (ap->a_vp->v_type != VREG) 928 return (EOPNOTSUPP); 929 930 error = unionfs_copyfile(unp, 1, cnp->cn_cred); 931 if (error != 0) 932 return (error); 933 needrelookup = 1; 934 } 935 uvp = unp->un_uppervp; 936 } 937 938 if (needrelookup != 0) 939 error = unionfs_relookup_for_create(ap->a_tdvp, cnp); 940 941 if (error == 0) 942 error = VOP_LINK(udvp, uvp, cnp); 943 944 UNIONFS_INTERNAL_DEBUG("unionfs_link: leave (%d)\n", error); 945 946 return (error); 947 #else 948 panic("XXXAD"); 949 return 0; 950 #endif 951 } 952 953 static int 954 unionfs_rename(void *v) 955 { 956 struct vop_rename_args *ap = v; 957 int error; 958 struct vnode *fdvp; 959 struct vnode *fvp; 960 struct componentname *fcnp; 961 struct vnode *tdvp; 962 struct vnode *tvp; 963 struct componentname *tcnp; 964 struct vnode *ltdvp; 965 struct vnode *ltvp; 966 967 /* rename target vnodes */ 968 struct vnode *rfdvp; 969 struct vnode *rfvp; 970 struct vnode *rtdvp; 971 struct vnode *rtvp; 972 973 int needrelookup; 974 struct unionfs_mount *ump; 975 struct unionfs_node *unp; 976 977 UNIONFS_INTERNAL_DEBUG("unionfs_rename: enter\n"); 978 979 error = 0; 980 fdvp = ap->a_fdvp; 981 fvp = ap->a_fvp; 982 fcnp = ap->a_fcnp; 983 tdvp = ap->a_tdvp; 984 tvp = ap->a_tvp; 985 tcnp = ap->a_tcnp; 986 ltdvp = NULLVP; 987 ltvp = NULLVP; 988 rfdvp = fdvp; 989 rfvp = fvp; 990 rtdvp = tdvp; 991 rtvp = tvp; 992 needrelookup = 0; 993 994 /* check for cross device rename */ 995 if (fvp->v_mount != tdvp->v_mount || 996 (tvp != NULLVP && fvp->v_mount != tvp->v_mount)) { 997 error = EXDEV; 998 goto unionfs_rename_abort; 999 } 1000 1001 /* Renaming a file to itself has no effect. */ 1002 if (fvp == tvp) 1003 goto unionfs_rename_abort; 1004 1005 /* 1006 * from/to vnode is unionfs node. 1007 */ 1008 1009 unp = VTOUNIONFS(fdvp); 1010 #ifdef UNIONFS_IDBG_RENAME 1011 UNIONFS_INTERNAL_DEBUG("fdvp=%p, ufdvp=%p, lfdvp=%p\n", fdvp, unp->un_uppervp, unp->un_lowervp); 1012 #endif 1013 if (unp->un_uppervp == NULLVP) { 1014 error = ENODEV; 1015 goto unionfs_rename_abort; 1016 } 1017 rfdvp = unp->un_uppervp; 1018 vref(rfdvp); 1019 1020 unp = VTOUNIONFS(fvp); 1021 #ifdef UNIONFS_IDBG_RENAME 1022 UNIONFS_INTERNAL_DEBUG("fvp=%p, ufvp=%p, lfvp=%p\n", fvp, unp->un_uppervp, unp->un_lowervp); 1023 #endif 1024 ump = MOUNTTOUNIONFSMOUNT(fvp->v_mount); 1025 if (unp->un_uppervp == NULLVP) { 1026 switch (fvp->v_type) { 1027 case VREG: 1028 if ((error = vn_lock(fvp, LK_EXCLUSIVE)) != 0) 1029 goto unionfs_rename_abort; 1030 error = unionfs_copyfile(unp, 1, fcnp->cn_cred); 1031 VOP_UNLOCK(fvp); 1032 if (error != 0) 1033 goto unionfs_rename_abort; 1034 break; 1035 case VDIR: 1036 if ((error = vn_lock(fvp, LK_EXCLUSIVE)) != 0) 1037 goto unionfs_rename_abort; 1038 error = unionfs_mkshadowdir(ump, rfdvp, unp, fcnp); 1039 VOP_UNLOCK(fvp); 1040 if (error != 0) 1041 goto unionfs_rename_abort; 1042 break; 1043 default: 1044 error = ENODEV; 1045 goto unionfs_rename_abort; 1046 } 1047 1048 needrelookup = 1; 1049 } 1050 1051 if (unp->un_lowervp != NULLVP) 1052 fcnp->cn_flags |= DOWHITEOUT; 1053 rfvp = unp->un_uppervp; 1054 vref(rfvp); 1055 1056 unp = VTOUNIONFS(tdvp); 1057 #ifdef UNIONFS_IDBG_RENAME 1058 UNIONFS_INTERNAL_DEBUG("tdvp=%p, utdvp=%p, ltdvp=%p\n", tdvp, unp->un_uppervp, unp->un_lowervp); 1059 #endif 1060 if (unp->un_uppervp == NULLVP) { 1061 error = ENODEV; 1062 goto unionfs_rename_abort; 1063 } 1064 rtdvp = unp->un_uppervp; 1065 ltdvp = unp->un_lowervp; 1066 vref(rtdvp); 1067 1068 if (tdvp == tvp) { 1069 rtvp = rtdvp; 1070 vref(rtvp); 1071 } else if (tvp != NULLVP) { 1072 unp = VTOUNIONFS(tvp); 1073 #ifdef UNIONFS_IDBG_RENAME 1074 UNIONFS_INTERNAL_DEBUG("tvp=%p, utvp=%p, ltvp=%p\n", tvp, unp->un_uppervp, unp->un_lowervp); 1075 #endif 1076 if (unp->un_uppervp == NULLVP) 1077 rtvp = NULLVP; 1078 else { 1079 if (tvp->v_type == VDIR) { 1080 error = EINVAL; 1081 goto unionfs_rename_abort; 1082 } 1083 rtvp = unp->un_uppervp; 1084 ltvp = unp->un_lowervp; 1085 vref(rtvp); 1086 } 1087 } 1088 1089 if (needrelookup != 0) { 1090 if ((error = vn_lock(fdvp, LK_EXCLUSIVE)) != 0) 1091 goto unionfs_rename_abort; 1092 error = unionfs_relookup_for_delete(fdvp, fcnp); 1093 VOP_UNLOCK(fdvp); 1094 if (error != 0) 1095 goto unionfs_rename_abort; 1096 1097 /* Locke of tvp is canceled in order to avoid recursive lock. */ 1098 if (tvp != NULLVP && tvp != tdvp) 1099 VOP_UNLOCK(tvp); 1100 error = unionfs_relookup_for_rename(tdvp, tcnp); 1101 if (tvp != NULLVP && tvp != tdvp) 1102 vn_lock(tvp, LK_EXCLUSIVE | LK_RETRY); 1103 if (error != 0) 1104 goto unionfs_rename_abort; 1105 } 1106 1107 error = VOP_RENAME(rfdvp, rfvp, fcnp, rtdvp, rtvp, tcnp); 1108 1109 if (error == 0) { 1110 if (rtvp != NULLVP && rtvp->v_type == VDIR) 1111 cache_purge(tdvp); 1112 if (fvp->v_type == VDIR && fdvp != tdvp) 1113 cache_purge(fdvp); 1114 } 1115 1116 if (fdvp != rfdvp) 1117 vrele(fdvp); 1118 if (fvp != rfvp) 1119 vrele(fvp); 1120 if (ltdvp != NULLVP) 1121 VOP_UNLOCK(ltdvp); 1122 if (tdvp != rtdvp) 1123 vrele(tdvp); 1124 if (ltvp != NULLVP) 1125 VOP_UNLOCK(ltvp); 1126 if (tvp != rtvp && tvp != NULLVP) { 1127 if (rtvp == NULLVP) 1128 vput(tvp); 1129 else 1130 vrele(tvp); 1131 } 1132 1133 UNIONFS_INTERNAL_DEBUG("unionfs_rename: leave (%d)\n", error); 1134 1135 return (error); 1136 1137 unionfs_rename_abort: 1138 if (fdvp != rfdvp) 1139 vrele(rfdvp); 1140 if (fvp != rfvp) 1141 vrele(rfvp); 1142 if (tdvp != rtdvp) 1143 vrele(rtdvp); 1144 vput(tdvp); 1145 if (tvp != rtvp && rtvp != NULLVP) 1146 vrele(rtvp); 1147 if (tvp != NULLVP) { 1148 if (tdvp != tvp) 1149 vput(tvp); 1150 else 1151 vrele(tvp); 1152 } 1153 vrele(fdvp); 1154 vrele(fvp); 1155 1156 UNIONFS_INTERNAL_DEBUG("unionfs_rename: leave (%d)\n", error); 1157 1158 return (error); 1159 } 1160 1161 static int 1162 unionfs_mkdir(void *v) 1163 { 1164 struct vop_mkdir_args *ap = v; 1165 int error; 1166 struct unionfs_node *dunp; 1167 struct componentname *cnp; 1168 struct vnode *udvp; 1169 struct vnode *uvp; 1170 struct vattr va; 1171 1172 UNIONFS_INTERNAL_DEBUG("unionfs_mkdir: enter\n"); 1173 1174 error = EROFS; 1175 dunp = VTOUNIONFS(ap->a_dvp); 1176 cnp = ap->a_cnp; 1177 udvp = dunp->un_uppervp; 1178 1179 if (udvp != NULLVP) { 1180 /* check opaque */ 1181 if (!(cnp->cn_flags & ISWHITEOUT)) { 1182 error = VOP_GETATTR(udvp, &va, cnp->cn_cred); 1183 if (error != 0) 1184 return (error); 1185 if (va.va_flags & OPAQUE) 1186 cnp->cn_flags |= ISWHITEOUT; 1187 } 1188 1189 if ((error = VOP_MKDIR(udvp, &uvp, cnp, ap->a_vap)) == 0) { 1190 error = unionfs_nodeget(ap->a_dvp->v_mount, uvp, NULLVP, 1191 ap->a_dvp, ap->a_vpp, cnp); 1192 if (error) { 1193 vput(uvp); 1194 } else { 1195 vrele(uvp); 1196 } 1197 } 1198 } 1199 1200 UNIONFS_INTERNAL_DEBUG("unionfs_mkdir: leave (%d)\n", error); 1201 1202 return (error); 1203 } 1204 1205 static int 1206 unionfs_rmdir(void *v) 1207 { 1208 struct vop_rmdir_args *ap = v; 1209 int error; 1210 struct unionfs_node *dunp; 1211 struct unionfs_node *unp; 1212 struct unionfs_mount *ump; 1213 struct componentname *cnp; 1214 struct vnode *udvp; 1215 struct vnode *uvp; 1216 struct vnode *lvp; 1217 1218 UNIONFS_INTERNAL_DEBUG("unionfs_rmdir: enter\n"); 1219 1220 error = 0; 1221 dunp = VTOUNIONFS(ap->a_dvp); 1222 unp = VTOUNIONFS(ap->a_vp); 1223 cnp = ap->a_cnp; 1224 udvp = dunp->un_uppervp; 1225 uvp = unp->un_uppervp; 1226 lvp = unp->un_lowervp; 1227 1228 if (udvp == NULLVP) 1229 return (EROFS); 1230 1231 if (udvp == uvp) 1232 return (EOPNOTSUPP); 1233 1234 if (uvp != NULLVP) { 1235 if (lvp != NULLVP) { 1236 error = unionfs_check_rmdir(ap->a_vp, cnp->cn_cred); 1237 if (error != 0) 1238 return (error); 1239 } 1240 ump = MOUNTTOUNIONFSMOUNT(ap->a_vp->v_mount); 1241 if (ump->um_whitemode == UNIONFS_WHITE_ALWAYS || lvp != NULLVP) 1242 cnp->cn_flags |= DOWHITEOUT; 1243 error = VOP_RMDIR(udvp, uvp, cnp); 1244 } 1245 else if (lvp != NULLVP) 1246 error = unionfs_mkwhiteout(udvp, cnp, unp->un_path); 1247 1248 if (error == 0) { 1249 cache_purge(ap->a_dvp); 1250 cache_purge(ap->a_vp); 1251 } 1252 1253 UNIONFS_INTERNAL_DEBUG("unionfs_rmdir: leave (%d)\n", error); 1254 1255 return (error); 1256 } 1257 1258 static int 1259 unionfs_symlink(void *v) 1260 { 1261 struct vop_symlink_args *ap = v; 1262 int error; 1263 struct unionfs_node *dunp; 1264 struct componentname *cnp; 1265 struct vnode *udvp; 1266 struct vnode *uvp; 1267 1268 UNIONFS_INTERNAL_DEBUG("unionfs_symlink: enter\n"); 1269 1270 error = EROFS; 1271 dunp = VTOUNIONFS(ap->a_dvp); 1272 cnp = ap->a_cnp; 1273 udvp = dunp->un_uppervp; 1274 1275 if (udvp != NULLVP) { 1276 error = VOP_SYMLINK(udvp, &uvp, cnp, ap->a_vap, ap->a_target); 1277 if (error == 0) { 1278 error = unionfs_nodeget(ap->a_dvp->v_mount, uvp, NULLVP, 1279 ap->a_dvp, ap->a_vpp, cnp); 1280 if (error) { 1281 vput(uvp); 1282 } else { 1283 vrele(uvp); 1284 } 1285 } 1286 } 1287 1288 UNIONFS_INTERNAL_DEBUG("unionfs_symlink: leave (%d)\n", error); 1289 1290 return (error); 1291 } 1292 1293 static int 1294 unionfs_readdir(void *v) 1295 { 1296 struct vop_readdir_args *ap = v; 1297 int error; 1298 int eofflag; 1299 int locked; 1300 struct unionfs_node *unp; 1301 struct unionfs_node_status *unsp; 1302 struct uio *uio; 1303 struct vnode *uvp; 1304 struct vnode *lvp; 1305 struct vattr va; 1306 1307 int ncookies_bk; 1308 off_t *cookies_bk; 1309 1310 UNIONFS_INTERNAL_DEBUG("unionfs_readdir: enter\n"); 1311 1312 error = 0; 1313 eofflag = 0; 1314 locked = 0; 1315 unp = VTOUNIONFS(ap->a_vp); 1316 uio = ap->a_uio; 1317 uvp = unp->un_uppervp; 1318 lvp = unp->un_lowervp; 1319 ncookies_bk = 0; 1320 cookies_bk = NULL; 1321 1322 if (ap->a_vp->v_type != VDIR) 1323 return (ENOTDIR); 1324 1325 /* check opaque */ 1326 if (uvp != NULLVP && lvp != NULLVP) { 1327 if ((error = VOP_GETATTR(uvp, &va, ap->a_cred)) != 0) 1328 goto unionfs_readdir_exit; 1329 if (va.va_flags & OPAQUE) 1330 lvp = NULLVP; 1331 } 1332 1333 /* check the open count. unionfs needs to open before readdir. */ 1334 VOP_UNLOCK(ap->a_vp); 1335 vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY); 1336 unionfs_get_node_status(unp, &unsp); 1337 if ((uvp != NULLVP && unsp->uns_upper_opencnt <= 0) || 1338 (lvp != NULLVP && unsp->uns_lower_opencnt <= 0)) { 1339 unionfs_tryrem_node_status(unp, unsp); 1340 error = EBADF; 1341 } 1342 if (error != 0) 1343 goto unionfs_readdir_exit; 1344 1345 /* upper only */ 1346 if (uvp != NULLVP && lvp == NULLVP) { 1347 error = VOP_READDIR(uvp, uio, ap->a_cred, ap->a_eofflag, 1348 ap->a_cookies, ap->a_ncookies); 1349 unsp->uns_readdir_status = 0; 1350 1351 goto unionfs_readdir_exit; 1352 } 1353 1354 /* lower only */ 1355 if (uvp == NULLVP && lvp != NULLVP) { 1356 error = VOP_READDIR(lvp, uio, ap->a_cred, ap->a_eofflag, 1357 ap->a_cookies, ap->a_ncookies); 1358 unsp->uns_readdir_status = 2; 1359 1360 goto unionfs_readdir_exit; 1361 } 1362 1363 /* 1364 * readdir upper and lower 1365 */ 1366 KASSERT(uvp != NULLVP); 1367 KASSERT(lvp != NULLVP); 1368 if (uio->uio_offset == 0) 1369 unsp->uns_readdir_status = 0; 1370 1371 if (unsp->uns_readdir_status == 0) { 1372 /* read upper */ 1373 error = VOP_READDIR(uvp, uio, ap->a_cred, &eofflag, 1374 ap->a_cookies, ap->a_ncookies); 1375 1376 if (error != 0 || eofflag == 0) 1377 goto unionfs_readdir_exit; 1378 unsp->uns_readdir_status = 1; 1379 1380 /* 1381 * ufs(and other fs) needs size of uio_resid larger than 1382 * DIRBLKSIZ. 1383 * size of DIRBLKSIZ equals DEV_BSIZE. 1384 * (see: ufs/ufs/ufs_vnops.c ufs_readdir func , ufs/ufs/dir.h) 1385 */ 1386 if (uio->uio_resid <= (uio->uio_resid & (DEV_BSIZE -1))) 1387 goto unionfs_readdir_exit; 1388 1389 /* 1390 * backup cookies 1391 * It prepares to readdir in lower. 1392 */ 1393 if (ap->a_ncookies != NULL) { 1394 ncookies_bk = *(ap->a_ncookies); 1395 *(ap->a_ncookies) = 0; 1396 } 1397 if (ap->a_cookies != NULL) { 1398 cookies_bk = *(ap->a_cookies); 1399 *(ap->a_cookies) = NULL; 1400 } 1401 } 1402 1403 /* initialize for readdir in lower */ 1404 if (unsp->uns_readdir_status == 1) { 1405 unsp->uns_readdir_status = 2; 1406 uio->uio_offset = 0; 1407 } 1408 1409 if (lvp == NULLVP) { 1410 error = EBADF; 1411 goto unionfs_readdir_exit; 1412 } 1413 /* read lower */ 1414 error = VOP_READDIR(lvp, uio, ap->a_cred, ap->a_eofflag, 1415 ap->a_cookies, ap->a_ncookies); 1416 1417 if (cookies_bk != NULL) { 1418 /* merge cookies */ 1419 int size; 1420 off_t *newcookies, *pos; 1421 1422 size = *(ap->a_ncookies) + ncookies_bk; 1423 newcookies = (off_t *) malloc(size * sizeof(off_t), 1424 M_TEMP, M_WAITOK); 1425 pos = newcookies; 1426 1427 memcpy(pos, cookies_bk, ncookies_bk * sizeof(off_t)); 1428 pos += ncookies_bk * sizeof(off_t); 1429 memcpy(pos, *(ap->a_cookies), *(ap->a_ncookies) * sizeof(off_t)); 1430 free(cookies_bk, M_TEMP); 1431 free(*(ap->a_cookies), M_TEMP); 1432 *(ap->a_ncookies) = size; 1433 *(ap->a_cookies) = newcookies; 1434 } 1435 1436 unionfs_readdir_exit: 1437 if (error != 0 && ap->a_eofflag != NULL) 1438 *(ap->a_eofflag) = 1; 1439 1440 UNIONFS_INTERNAL_DEBUG("unionfs_readdir: leave (%d)\n", error); 1441 1442 return (error); 1443 } 1444 1445 static int 1446 unionfs_readlink(void *v) 1447 { 1448 struct vop_readlink_args *ap = v; 1449 int error; 1450 struct unionfs_node *unp; 1451 struct vnode *vp; 1452 1453 UNIONFS_INTERNAL_DEBUG("unionfs_readlink: enter\n"); 1454 1455 unp = VTOUNIONFS(ap->a_vp); 1456 vp = (unp->un_uppervp != NULLVP ? unp->un_uppervp : unp->un_lowervp); 1457 1458 error = VOP_READLINK(vp, ap->a_uio, ap->a_cred); 1459 1460 UNIONFS_INTERNAL_DEBUG("unionfs_readlink: leave (%d)\n", error); 1461 1462 return (error); 1463 } 1464 1465 static int 1466 unionfs_inactive(void *v) 1467 { 1468 struct vop_inactive_args *ap = v; 1469 *ap->a_recycle = true; 1470 VOP_UNLOCK(ap->a_vp); 1471 return (0); 1472 } 1473 1474 static int 1475 unionfs_reclaim(void *v) 1476 { 1477 struct vop_reclaim_args *ap = v; 1478 1479 /* UNIONFS_INTERNAL_DEBUG("unionfs_reclaim: enter\n"); */ 1480 1481 unionfs_noderem(ap->a_vp); 1482 1483 /* UNIONFS_INTERNAL_DEBUG("unionfs_reclaim: leave\n"); */ 1484 1485 return (0); 1486 } 1487 1488 static int 1489 unionfs_print(void *v) 1490 { 1491 struct vop_print_args *ap = v; 1492 struct unionfs_node *unp; 1493 /* struct unionfs_node_status *unsp; */ 1494 1495 unp = VTOUNIONFS(ap->a_vp); 1496 /* unionfs_get_node_status(unp, &unsp); */ 1497 1498 printf("unionfs_vp=%p, uppervp=%p, lowervp=%p\n", 1499 ap->a_vp, unp->un_uppervp, unp->un_lowervp); 1500 /* 1501 printf("unionfs opencnt: uppervp=%d, lowervp=%d\n", 1502 unsp->uns_upper_opencnt, unsp->uns_lower_opencnt); 1503 */ 1504 1505 if (unp->un_uppervp != NULLVP) 1506 vprint("unionfs: upper", unp->un_uppervp); 1507 if (unp->un_lowervp != NULLVP) 1508 vprint("unionfs: lower", unp->un_lowervp); 1509 1510 return (0); 1511 } 1512 1513 static int 1514 unionfs_lock(void *v) 1515 { 1516 struct vop_lock_args *ap = v; 1517 int error; 1518 int flags; 1519 struct vnode *lvp; 1520 struct vnode *uvp; 1521 struct unionfs_node *unp; 1522 1523 unp = VTOUNIONFS(ap->a_vp); 1524 lvp = unp->un_lowervp; 1525 uvp = unp->un_uppervp; 1526 flags = ap->a_flags; 1527 error = 0; 1528 1529 if (lvp != NULLVP) { 1530 error = VOP_LOCK(lvp, flags); 1531 } 1532 if (error == 0 && uvp != NULLVP) { 1533 error = VOP_LOCK(uvp, flags); 1534 if (error != 0) { 1535 VOP_UNLOCK(lvp); 1536 } 1537 } 1538 1539 return error; 1540 } 1541 1542 static int 1543 unionfs_unlock(void *v) 1544 { 1545 struct vop_unlock_args *ap = v; 1546 int error; 1547 struct vnode *lvp; 1548 struct vnode *uvp; 1549 struct unionfs_node *unp; 1550 1551 unp = VTOUNIONFS(ap->a_vp); 1552 lvp = unp->un_lowervp; 1553 uvp = unp->un_uppervp; 1554 error = 0; 1555 1556 if (lvp != NULLVP) { 1557 error = VOP_UNLOCK(lvp); 1558 } 1559 if (error == 0 && uvp != NULLVP) { 1560 error = VOP_UNLOCK(uvp); 1561 } 1562 1563 return error; 1564 } 1565 1566 static int 1567 unionfs_pathconf(void *v) 1568 { 1569 struct vop_pathconf_args *ap = v; 1570 struct unionfs_node *unp; 1571 struct vnode *vp; 1572 1573 unp = VTOUNIONFS(ap->a_vp); 1574 vp = (unp->un_uppervp != NULLVP ? unp->un_uppervp : unp->un_lowervp); 1575 1576 return (VOP_PATHCONF(vp, ap->a_name, ap->a_retval)); 1577 } 1578 1579 static int 1580 unionfs_advlock(void *v) 1581 { 1582 struct vop_advlock_args *ap = v; 1583 int error; 1584 struct unionfs_node *unp; 1585 struct unionfs_node_status *unsp; 1586 struct vnode *vp; 1587 struct vnode *uvp; 1588 kauth_cred_t cred; 1589 1590 UNIONFS_INTERNAL_DEBUG("unionfs_advlock: enter\n"); 1591 1592 vp = ap->a_vp; 1593 cred = kauth_cred_get(); 1594 1595 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 1596 1597 unp = VTOUNIONFS(ap->a_vp); 1598 uvp = unp->un_uppervp; 1599 1600 if (uvp == NULLVP) { 1601 error = unionfs_copyfile(unp, 1, cred); 1602 if (error != 0) 1603 goto unionfs_advlock_abort; 1604 uvp = unp->un_uppervp; 1605 1606 unionfs_get_node_status(unp, &unsp); 1607 if (unsp->uns_lower_opencnt > 0) { 1608 /* try reopen the vnode */ 1609 error = VOP_OPEN(uvp, unsp->uns_lower_openmode, cred); 1610 if (error) 1611 goto unionfs_advlock_abort; 1612 unsp->uns_upper_opencnt++; 1613 VOP_CLOSE(unp->un_lowervp, unsp->uns_lower_openmode, cred); 1614 unsp->uns_lower_opencnt--; 1615 } else 1616 unionfs_tryrem_node_status(unp, unsp); 1617 } 1618 1619 VOP_UNLOCK(vp); 1620 1621 error = VOP_ADVLOCK(uvp, ap->a_id, ap->a_op, ap->a_fl, ap->a_flags); 1622 1623 UNIONFS_INTERNAL_DEBUG("unionfs_advlock: leave (%d)\n", error); 1624 1625 return error; 1626 1627 unionfs_advlock_abort: 1628 VOP_UNLOCK(vp); 1629 1630 UNIONFS_INTERNAL_DEBUG("unionfs_advlock: leave (%d)\n", error); 1631 1632 return error; 1633 } 1634 1635 static int 1636 unionfs_strategy(void *v) 1637 { 1638 struct vop_strategy_args *ap = v; 1639 struct unionfs_node *unp; 1640 struct vnode *vp; 1641 1642 unp = VTOUNIONFS(ap->a_vp); 1643 vp = (unp->un_uppervp != NULLVP ? unp->un_uppervp : unp->un_lowervp); 1644 1645 #ifdef DIAGNOSTIC 1646 if (vp == NULLVP) 1647 panic("unionfs_strategy: nullvp"); 1648 if ((ap->a_bp->b_flags & B_READ) == 0 && vp == unp->un_lowervp) 1649 panic("unionfs_strategy: writing to lowervp"); 1650 #endif 1651 1652 return (VOP_STRATEGY(vp, ap->a_bp)); 1653 } 1654 1655 static int 1656 unionfs_kqfilter(void *v) 1657 { 1658 struct vop_kqfilter_args *ap = v; 1659 struct unionfs_node *unp; 1660 struct vnode *tvp; 1661 1662 unp = VTOUNIONFS(ap->a_vp); 1663 tvp = (unp->un_uppervp != NULLVP ? unp->un_uppervp : unp->un_lowervp); 1664 1665 return VOP_KQFILTER(tvp, ap->a_kn); 1666 } 1667 1668 static int 1669 unionfs_bmap(void *v) 1670 { 1671 struct vop_bmap_args *ap = v; 1672 struct unionfs_node *unp; 1673 struct vnode *tvp; 1674 1675 unp = VTOUNIONFS(ap->a_vp); 1676 tvp = (unp->un_uppervp != NULLVP ? unp->un_uppervp : unp->un_lowervp); 1677 1678 return VOP_BMAP(tvp, ap->a_bn, ap->a_vpp, ap->a_bnp, ap->a_runp); 1679 } 1680 1681 static int 1682 unionfs_mmap(void *v) 1683 { 1684 struct vop_mmap_args *ap = v; 1685 struct unionfs_node *unp; 1686 struct vnode *tvp; 1687 1688 unp = VTOUNIONFS(ap->a_vp); 1689 tvp = (unp->un_uppervp != NULLVP ? unp->un_uppervp : unp->un_lowervp); 1690 1691 return VOP_MMAP(tvp, ap->a_prot, ap->a_cred); 1692 } 1693 1694 static int 1695 unionfs_abortop(void *v) 1696 { 1697 struct vop_abortop_args *ap = v; 1698 struct unionfs_node *unp; 1699 struct vnode *tvp; 1700 1701 unp = VTOUNIONFS(ap->a_dvp); 1702 tvp = (unp->un_uppervp != NULLVP ? unp->un_uppervp : unp->un_lowervp); 1703 1704 return VOP_ABORTOP(tvp, ap->a_cnp); 1705 } 1706 1707 static int 1708 unionfs_islocked(void *v) 1709 { 1710 struct vop_islocked_args *ap = v; 1711 struct unionfs_node *unp; 1712 struct vnode *tvp; 1713 1714 unp = VTOUNIONFS(ap->a_vp); 1715 tvp = (unp->un_uppervp != NULLVP ? unp->un_uppervp : unp->un_lowervp); 1716 1717 return VOP_ISLOCKED(tvp); 1718 } 1719 1720 static int 1721 unionfs_seek(void *v) 1722 { 1723 struct vop_seek_args *ap = v; 1724 struct unionfs_node *unp; 1725 struct vnode *tvp; 1726 1727 unp = VTOUNIONFS(ap->a_vp); 1728 tvp = (unp->un_uppervp != NULLVP ? unp->un_uppervp : unp->un_lowervp); 1729 1730 return VOP_SEEK(tvp, ap->a_oldoff, ap->a_newoff, ap->a_cred); 1731 } 1732 1733 static int 1734 unionfs_putpages(void *v) 1735 { 1736 struct vop_putpages_args *ap = v; 1737 struct unionfs_node *unp; 1738 struct vnode *tvp; 1739 1740 unp = VTOUNIONFS(ap->a_vp); 1741 tvp = (unp->un_uppervp != NULLVP ? unp->un_uppervp : unp->un_lowervp); 1742 1743 mutex_exit(&ap->a_vp->v_interlock); 1744 if (ap->a_flags & PGO_RECLAIM) { 1745 return 0; 1746 } 1747 mutex_enter(&tvp->v_interlock); 1748 1749 return VOP_PUTPAGES(tvp, ap->a_offlo, ap->a_offhi, ap->a_flags); 1750 } 1751 1752 static int 1753 unionfs_getpages(void *v) 1754 { 1755 struct vop_getpages_args *ap = v; 1756 struct unionfs_node *unp; 1757 struct vnode *tvp; 1758 1759 unp = VTOUNIONFS(ap->a_vp); 1760 tvp = (unp->un_uppervp != NULLVP ? unp->un_uppervp : unp->un_lowervp); 1761 1762 if (ap->a_flags & PGO_LOCKED) { 1763 return EBUSY; 1764 } 1765 mutex_exit(&ap->a_vp->v_interlock); 1766 mutex_enter(&tvp->v_interlock); 1767 1768 return VOP_GETPAGES(tvp, ap->a_offset, ap->a_m, ap->a_count, 1769 ap->a_centeridx, ap->a_access_type, ap->a_advice, ap->a_flags); 1770 } 1771 1772 static int 1773 unionfs_revoke(void *v) 1774 { 1775 struct vop_revoke_args *ap = v; 1776 struct unionfs_node *unp; 1777 struct vnode *tvp; 1778 int error; 1779 1780 unp = VTOUNIONFS(ap->a_vp); 1781 tvp = (unp->un_uppervp != NULLVP ? unp->un_uppervp : unp->un_lowervp); 1782 1783 error = VOP_REVOKE(tvp, ap->a_flags); 1784 if (error == 0) { 1785 vgone(ap->a_vp); /* ??? */ 1786 } 1787 return error; 1788 } 1789 1790 /* 1791 * Global vfs data structures 1792 */ 1793 int (**unionfs_vnodeop_p)(void *); 1794 const struct vnodeopv_entry_desc unionfs_vnodeop_entries[] = { 1795 { &vop_default_desc, vn_default_error }, 1796 { &vop_lookup_desc, unionfs_lookup }, /* lookup */ 1797 { &vop_create_desc, unionfs_create }, /* create */ 1798 { &vop_whiteout_desc, unionfs_whiteout }, /* whiteout */ 1799 { &vop_mknod_desc, unionfs_mknod }, /* mknod */ 1800 { &vop_open_desc, unionfs_open }, /* open */ 1801 { &vop_close_desc, unionfs_close }, /* close */ 1802 { &vop_access_desc, unionfs_access }, /* access */ 1803 { &vop_getattr_desc, unionfs_getattr }, /* getattr */ 1804 { &vop_setattr_desc, unionfs_setattr }, /* setattr */ 1805 { &vop_read_desc, unionfs_read }, /* read */ 1806 { &vop_write_desc, unionfs_write }, /* write */ 1807 { &vop_ioctl_desc, unionfs_ioctl }, /* ioctl */ 1808 { &vop_poll_desc, unionfs_poll }, /* select */ 1809 { &vop_revoke_desc, unionfs_revoke }, /* revoke */ 1810 { &vop_mmap_desc, unionfs_mmap }, /* mmap */ 1811 { &vop_fsync_desc, unionfs_fsync }, /* fsync */ 1812 { &vop_seek_desc, unionfs_seek }, /* seek */ 1813 { &vop_remove_desc, unionfs_remove }, /* remove */ 1814 { &vop_link_desc, unionfs_link }, /* link */ 1815 { &vop_rename_desc, unionfs_rename }, /* rename */ 1816 { &vop_mkdir_desc, unionfs_mkdir }, /* mkdir */ 1817 { &vop_rmdir_desc, unionfs_rmdir }, /* rmdir */ 1818 { &vop_symlink_desc, unionfs_symlink }, /* symlink */ 1819 { &vop_readdir_desc, unionfs_readdir }, /* readdir */ 1820 { &vop_readlink_desc, unionfs_readlink }, /* readlink */ 1821 { &vop_abortop_desc, unionfs_abortop }, /* abortop */ 1822 { &vop_inactive_desc, unionfs_inactive }, /* inactive */ 1823 { &vop_reclaim_desc, unionfs_reclaim }, /* reclaim */ 1824 { &vop_lock_desc, unionfs_lock }, /* lock */ 1825 { &vop_unlock_desc, unionfs_unlock }, /* unlock */ 1826 { &vop_bmap_desc, unionfs_bmap }, /* bmap */ 1827 { &vop_strategy_desc, unionfs_strategy }, /* strategy */ 1828 { &vop_print_desc, unionfs_print }, /* print */ 1829 { &vop_islocked_desc, unionfs_islocked }, /* islocked */ 1830 { &vop_pathconf_desc, unionfs_pathconf }, /* pathconf */ 1831 { &vop_advlock_desc, unionfs_advlock }, /* advlock */ 1832 { &vop_getpages_desc, unionfs_getpages }, /* getpages */ 1833 { &vop_putpages_desc, unionfs_putpages }, /* putpages */ 1834 { &vop_kqfilter_desc, unionfs_kqfilter }, /* kqfilter */ 1835 #ifdef notdef 1836 { &vop_bwrite_desc, unionfs_bwrite }, /* bwrite */ 1837 #endif 1838 { NULL, NULL } 1839 }; 1840 const struct vnodeopv_desc unionfs_vnodeop_opv_desc = 1841 { &unionfs_vnodeop_p, unionfs_vnodeop_entries }; 1842