1df8bae1dSRodney W. Grimes /* 2996c772fSJohn Dyson * Copyright (c) 1992, 1993, 1994, 1995 Jan-Simon Pendry. 3996c772fSJohn Dyson * Copyright (c) 1992, 1993, 1994, 1995 4996c772fSJohn Dyson * The Regents of the University of California. All rights reserved. 5df8bae1dSRodney W. Grimes * 6df8bae1dSRodney W. Grimes * This code is derived from software contributed to Berkeley by 7df8bae1dSRodney W. Grimes * Jan-Simon Pendry. 8df8bae1dSRodney W. Grimes * 9df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 10df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 11df8bae1dSRodney W. Grimes * are met: 12df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 13df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 14df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 15df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 16df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 17df8bae1dSRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 18df8bae1dSRodney W. Grimes * must display the following acknowledgement: 19df8bae1dSRodney W. Grimes * This product includes software developed by the University of 20df8bae1dSRodney W. Grimes * California, Berkeley and its contributors. 21df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 22df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 23df8bae1dSRodney W. Grimes * without specific prior written permission. 24df8bae1dSRodney W. Grimes * 25df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35df8bae1dSRodney W. Grimes * SUCH DAMAGE. 36df8bae1dSRodney W. Grimes * 37996c772fSJohn Dyson * @(#)union_vnops.c 8.32 (Berkeley) 6/23/95 38c3aac50fSPeter Wemm * $FreeBSD$ 39df8bae1dSRodney W. Grimes */ 40df8bae1dSRodney W. Grimes 41df8bae1dSRodney W. Grimes #include <sys/param.h> 42df8bae1dSRodney W. Grimes #include <sys/systm.h> 433ac4d1efSBruce Evans #include <sys/fcntl.h> 44996c772fSJohn Dyson #include <sys/stat.h> 459e67ea79SBruce Evans #include <sys/kernel.h> 46df8bae1dSRodney W. Grimes #include <sys/vnode.h> 47df8bae1dSRodney W. Grimes #include <sys/mount.h> 48df8bae1dSRodney W. Grimes #include <sys/namei.h> 49df8bae1dSRodney W. Grimes #include <sys/malloc.h> 509626b608SPoul-Henning Kamp #include <sys/bio.h> 51df8bae1dSRodney W. Grimes #include <sys/buf.h> 52996c772fSJohn Dyson #include <sys/lock.h> 532a31267eSMatthew Dillon #include <sys/sysctl.h> 5400fff2c7STim J. Robbins #include <sys/unistd.h> 5500fff2c7STim J. Robbins #include <sys/acl.h> 5600fff2c7STim J. Robbins #include <sys/event.h> 5700fff2c7STim J. Robbins #include <sys/extattr.h> 5800fff2c7STim J. Robbins #include <sys/mac.h> 5999d300a1SRuslan Ermilov #include <fs/unionfs/union.h> 60df8bae1dSRodney W. Grimes 612a31267eSMatthew Dillon #include <vm/vm.h> 622a31267eSMatthew Dillon #include <vm/vnode_pager.h> 632a31267eSMatthew Dillon 642a31267eSMatthew Dillon #include <vm/vm_page.h> 652a31267eSMatthew Dillon #include <vm/vm_object.h> 662a31267eSMatthew Dillon 672a31267eSMatthew Dillon int uniondebug = 0; 682a31267eSMatthew Dillon 692a31267eSMatthew Dillon #if UDEBUG_ENABLED 702a31267eSMatthew Dillon SYSCTL_INT(_vfs, OID_AUTO, uniondebug, CTLFLAG_RW, &uniondebug, 0, ""); 712a31267eSMatthew Dillon #else 722a31267eSMatthew Dillon SYSCTL_INT(_vfs, OID_AUTO, uniondebug, CTLFLAG_RD, &uniondebug, 0, ""); 732a31267eSMatthew Dillon #endif 74df8bae1dSRodney W. Grimes 7511caded3SAlfred Perlstein static int union_access(struct vop_access_args *ap); 7600fff2c7STim J. Robbins static int union_aclcheck(struct vop_aclcheck_args *ap); 7711caded3SAlfred Perlstein static int union_advlock(struct vop_advlock_args *ap); 7811caded3SAlfred Perlstein static int union_close(struct vop_close_args *ap); 7900fff2c7STim J. Robbins static int union_closeextattr(struct vop_closeextattr_args *ap); 8011caded3SAlfred Perlstein static int union_create(struct vop_create_args *ap); 8111caded3SAlfred Perlstein static int union_createvobject(struct vop_createvobject_args *ap); 8200fff2c7STim J. Robbins static int union_deleteextattr(struct vop_deleteextattr_args *ap); 8311caded3SAlfred Perlstein static int union_destroyvobject(struct vop_destroyvobject_args *ap); 8411caded3SAlfred Perlstein static int union_fsync(struct vop_fsync_args *ap); 8500fff2c7STim J. Robbins static int union_getacl(struct vop_getacl_args *ap); 8611caded3SAlfred Perlstein static int union_getattr(struct vop_getattr_args *ap); 8700fff2c7STim J. Robbins static int union_getextattr(struct vop_getextattr_args *ap); 8811caded3SAlfred Perlstein static int union_getvobject(struct vop_getvobject_args *ap); 8911caded3SAlfred Perlstein static int union_inactive(struct vop_inactive_args *ap); 9011caded3SAlfred Perlstein static int union_ioctl(struct vop_ioctl_args *ap); 9111caded3SAlfred Perlstein static int union_lease(struct vop_lease_args *ap); 9211caded3SAlfred Perlstein static int union_link(struct vop_link_args *ap); 9300fff2c7STim J. Robbins static int union_listextattr(struct vop_listextattr_args *ap); 9411caded3SAlfred Perlstein static int union_lookup(struct vop_lookup_args *ap); 9511caded3SAlfred Perlstein static int union_lookup1(struct vnode *udvp, struct vnode **dvp, 969b5e8b3aSBruce Evans struct vnode **vpp, 9711caded3SAlfred Perlstein struct componentname *cnp); 9811caded3SAlfred Perlstein static int union_mkdir(struct vop_mkdir_args *ap); 9911caded3SAlfred Perlstein static int union_mknod(struct vop_mknod_args *ap); 10011caded3SAlfred Perlstein static int union_open(struct vop_open_args *ap); 10100fff2c7STim J. Robbins static int union_openextattr(struct vop_openextattr_args *ap); 10211caded3SAlfred Perlstein static int union_pathconf(struct vop_pathconf_args *ap); 10311caded3SAlfred Perlstein static int union_print(struct vop_print_args *ap); 10411caded3SAlfred Perlstein static int union_read(struct vop_read_args *ap); 10511caded3SAlfred Perlstein static int union_readdir(struct vop_readdir_args *ap); 10611caded3SAlfred Perlstein static int union_readlink(struct vop_readlink_args *ap); 10711caded3SAlfred Perlstein static int union_getwritemount(struct vop_getwritemount_args *ap); 10811caded3SAlfred Perlstein static int union_reclaim(struct vop_reclaim_args *ap); 10911caded3SAlfred Perlstein static int union_remove(struct vop_remove_args *ap); 11011caded3SAlfred Perlstein static int union_rename(struct vop_rename_args *ap); 11111caded3SAlfred Perlstein static int union_revoke(struct vop_revoke_args *ap); 11211caded3SAlfred Perlstein static int union_rmdir(struct vop_rmdir_args *ap); 11311caded3SAlfred Perlstein static int union_poll(struct vop_poll_args *ap); 11400fff2c7STim J. Robbins static int union_setacl(struct vop_setacl_args *ap); 11511caded3SAlfred Perlstein static int union_setattr(struct vop_setattr_args *ap); 11600fff2c7STim J. Robbins static int union_setlabel(struct vop_setlabel_args *ap); 11700fff2c7STim J. Robbins static int union_setextattr(struct vop_setextattr_args *ap); 11811caded3SAlfred Perlstein static int union_strategy(struct vop_strategy_args *ap); 11911caded3SAlfred Perlstein static int union_symlink(struct vop_symlink_args *ap); 12011caded3SAlfred Perlstein static int union_whiteout(struct vop_whiteout_args *ap); 12111caded3SAlfred Perlstein static int union_write(struct vop_read_args *ap); 1229b5e8b3aSBruce Evans 1232a31267eSMatthew Dillon static __inline 1242a31267eSMatthew Dillon struct vnode * 125b40ce416SJulian Elischer union_lock_upper(struct union_node *un, struct thread *td) 126df8bae1dSRodney W. Grimes { 1272a31267eSMatthew Dillon struct vnode *uppervp; 128df8bae1dSRodney W. Grimes 1292a31267eSMatthew Dillon if ((uppervp = un->un_uppervp) != NULL) { 1302a31267eSMatthew Dillon VREF(uppervp); 131b40ce416SJulian Elischer vn_lock(uppervp, LK_EXCLUSIVE | LK_CANRECURSE | LK_RETRY, td); 1322a31267eSMatthew Dillon } 1334d93c0beSJeff Roberson KASSERT((uppervp == NULL || vrefcnt(uppervp) > 0), ("uppervp usecount is 0")); 1342a31267eSMatthew Dillon return(uppervp); 135df8bae1dSRodney W. Grimes } 136df8bae1dSRodney W. Grimes 1372a31267eSMatthew Dillon static __inline 1382a31267eSMatthew Dillon void 139b40ce416SJulian Elischer union_unlock_upper(struct vnode *uppervp, struct thread *td) 1402a31267eSMatthew Dillon { 1412a31267eSMatthew Dillon vput(uppervp); 1422a31267eSMatthew Dillon } 1432a31267eSMatthew Dillon 1442a31267eSMatthew Dillon static __inline 1452a31267eSMatthew Dillon struct vnode * 146b40ce416SJulian Elischer union_lock_other(struct union_node *un, struct thread *td) 1472a31267eSMatthew Dillon { 1482a31267eSMatthew Dillon struct vnode *vp; 1492a31267eSMatthew Dillon 1502a31267eSMatthew Dillon if (un->un_uppervp != NULL) { 151b40ce416SJulian Elischer vp = union_lock_upper(un, td); 1522a31267eSMatthew Dillon } else if ((vp = un->un_lowervp) != NULL) { 1532a31267eSMatthew Dillon VREF(vp); 154b40ce416SJulian Elischer vn_lock(vp, LK_EXCLUSIVE | LK_CANRECURSE | LK_RETRY, td); 1552a31267eSMatthew Dillon } 1562a31267eSMatthew Dillon return(vp); 1572a31267eSMatthew Dillon } 1582a31267eSMatthew Dillon 1592a31267eSMatthew Dillon static __inline 1602a31267eSMatthew Dillon void 161b40ce416SJulian Elischer union_unlock_other(struct vnode *vp, struct thread *td) 1622a31267eSMatthew Dillon { 1632a31267eSMatthew Dillon vput(vp); 1642a31267eSMatthew Dillon } 1652a31267eSMatthew Dillon 1662a31267eSMatthew Dillon /* 1672a31267eSMatthew Dillon * union_lookup: 1682a31267eSMatthew Dillon * 1692a31267eSMatthew Dillon * udvp must be exclusively locked on call and will remain 1702a31267eSMatthew Dillon * exclusively locked on return. This is the mount point 1711c4ccf09SDon Lewis * for our filesystem. 1722a31267eSMatthew Dillon * 1732a31267eSMatthew Dillon * dvp Our base directory, locked and referenced. 1742a31267eSMatthew Dillon * The passed dvp will be dereferenced and unlocked on return 1752a31267eSMatthew Dillon * and a new dvp will be returned which is locked and 1762a31267eSMatthew Dillon * referenced in the same variable. 1772a31267eSMatthew Dillon * 1782a31267eSMatthew Dillon * vpp is filled in with the result if no error occured, 1792a31267eSMatthew Dillon * locked and ref'd. 1802a31267eSMatthew Dillon * 1812a31267eSMatthew Dillon * If an error is returned, *vpp is set to NULLVP. If no 1822a31267eSMatthew Dillon * error occurs, *vpp is returned with a reference and an 1832a31267eSMatthew Dillon * exclusive lock. 1842a31267eSMatthew Dillon */ 1852a31267eSMatthew Dillon 186df8bae1dSRodney W. Grimes static int 1872a31267eSMatthew Dillon union_lookup1(udvp, pdvp, vpp, cnp) 188df8bae1dSRodney W. Grimes struct vnode *udvp; 1892a31267eSMatthew Dillon struct vnode **pdvp; 190df8bae1dSRodney W. Grimes struct vnode **vpp; 191df8bae1dSRodney W. Grimes struct componentname *cnp; 192df8bae1dSRodney W. Grimes { 193df8bae1dSRodney W. Grimes int error; 194b40ce416SJulian Elischer struct thread *td = cnp->cn_thread; 1952a31267eSMatthew Dillon struct vnode *dvp = *pdvp; 196df8bae1dSRodney W. Grimes struct vnode *tdvp; 197df8bae1dSRodney W. Grimes struct mount *mp; 198df8bae1dSRodney W. Grimes 199df8bae1dSRodney W. Grimes /* 200df8bae1dSRodney W. Grimes * If stepping up the directory tree, check for going 201df8bae1dSRodney W. Grimes * back across the mount point, in which case do what 202df8bae1dSRodney W. Grimes * lookup would do by stepping back down the mount 203df8bae1dSRodney W. Grimes * hierarchy. 204df8bae1dSRodney W. Grimes */ 205df8bae1dSRodney W. Grimes if (cnp->cn_flags & ISDOTDOT) { 206e6e370a7SJeff Roberson while ((dvp != udvp) && (dvp->v_vflag & VV_ROOT)) { 207df8bae1dSRodney W. Grimes /* 208df8bae1dSRodney W. Grimes * Don't do the NOCROSSMOUNT check 209df8bae1dSRodney W. Grimes * at this level. By definition, 210df8bae1dSRodney W. Grimes * union fs deals with namespaces, not 211df8bae1dSRodney W. Grimes * filesystems. 212df8bae1dSRodney W. Grimes */ 213df8bae1dSRodney W. Grimes tdvp = dvp; 2142a31267eSMatthew Dillon dvp = dvp->v_mount->mnt_vnodecovered; 215df8bae1dSRodney W. Grimes VREF(dvp); 2162a31267eSMatthew Dillon vput(tdvp); 217b40ce416SJulian Elischer vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, td); 218df8bae1dSRodney W. Grimes } 219df8bae1dSRodney W. Grimes } 220df8bae1dSRodney W. Grimes 2212a31267eSMatthew Dillon /* 2222a31267eSMatthew Dillon * Set return dvp to be the upperdvp 'parent directory. 2232a31267eSMatthew Dillon */ 2242a31267eSMatthew Dillon *pdvp = dvp; 2252a31267eSMatthew Dillon 2262a31267eSMatthew Dillon /* 2271c4ccf09SDon Lewis * If the VOP_LOOKUP() call generates an error, tdvp is invalid and 2281c4ccf09SDon Lewis * no changes will have been made to dvp, so we are set to return. 2292a31267eSMatthew Dillon */ 2302a31267eSMatthew Dillon 231df8bae1dSRodney W. Grimes error = VOP_LOOKUP(dvp, &tdvp, cnp); 2322a31267eSMatthew Dillon if (error) { 2332a31267eSMatthew Dillon UDEBUG(("dvp %p error %d flags %lx\n", dvp, error, cnp->cn_flags)); 2342a31267eSMatthew Dillon *vpp = NULL; 235df8bae1dSRodney W. Grimes return (error); 2362a31267eSMatthew Dillon } 237df8bae1dSRodney W. Grimes 238df8bae1dSRodney W. Grimes /* 239df8bae1dSRodney W. Grimes * The parent directory will have been unlocked, unless lookup 2402a31267eSMatthew Dillon * found the last component or if dvp == tdvp (tdvp must be locked). 2412a31267eSMatthew Dillon * 2422a31267eSMatthew Dillon * We want our dvp to remain locked and ref'd. We also want tdvp 2432a31267eSMatthew Dillon * to remain locked and ref'd. 244df8bae1dSRodney W. Grimes */ 2452a31267eSMatthew Dillon UDEBUG(("parentdir %p result %p flag %lx\n", dvp, tdvp, cnp->cn_flags)); 246df8bae1dSRodney W. Grimes 2472a31267eSMatthew Dillon if (dvp != tdvp && (cnp->cn_flags & ISLASTCN) == 0) 248b40ce416SJulian Elischer vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, td); 249df8bae1dSRodney W. Grimes 250df8bae1dSRodney W. Grimes /* 251df8bae1dSRodney W. Grimes * Lastly check if the current node is a mount point in 252df8bae1dSRodney W. Grimes * which case walk up the mount hierarchy making sure not to 253df8bae1dSRodney W. Grimes * bump into the root of the mount tree (ie. dvp != udvp). 2542a31267eSMatthew Dillon * 2552a31267eSMatthew Dillon * We use dvp as a temporary variable here, it is no longer related 2562a31267eSMatthew Dillon * to the dvp above. However, we have to ensure that both *pdvp and 2572a31267eSMatthew Dillon * tdvp are locked on return. 258df8bae1dSRodney W. Grimes */ 2592a31267eSMatthew Dillon 2602a31267eSMatthew Dillon dvp = tdvp; 2612a31267eSMatthew Dillon while ( 2622a31267eSMatthew Dillon dvp != udvp && 2632a31267eSMatthew Dillon (dvp->v_type == VDIR) && 2642a31267eSMatthew Dillon (mp = dvp->v_mountedhere) 2652a31267eSMatthew Dillon ) { 2662a31267eSMatthew Dillon int relock_pdvp = 0; 267df8bae1dSRodney W. Grimes 268b40ce416SJulian Elischer if (vfs_busy(mp, 0, 0, td)) 269df8bae1dSRodney W. Grimes continue; 270df8bae1dSRodney W. Grimes 2712a31267eSMatthew Dillon if (dvp == *pdvp) 2722a31267eSMatthew Dillon relock_pdvp = 1; 273df8bae1dSRodney W. Grimes vput(dvp); 2742a31267eSMatthew Dillon dvp = NULL; 2752a31267eSMatthew Dillon error = VFS_ROOT(mp, &dvp); 2762a31267eSMatthew Dillon 277b40ce416SJulian Elischer vfs_unbusy(mp, td); 2782a31267eSMatthew Dillon 2792a31267eSMatthew Dillon if (relock_pdvp) 280b40ce416SJulian Elischer vn_lock(*pdvp, LK_EXCLUSIVE | LK_RETRY, td); 2812a31267eSMatthew Dillon 2822a31267eSMatthew Dillon if (error) { 2832a31267eSMatthew Dillon *vpp = NULL; 284df8bae1dSRodney W. Grimes return (error); 285df8bae1dSRodney W. Grimes } 286df8bae1dSRodney W. Grimes } 287df8bae1dSRodney W. Grimes *vpp = dvp; 288df8bae1dSRodney W. Grimes return (0); 289df8bae1dSRodney W. Grimes } 290df8bae1dSRodney W. Grimes 291c9bf0111SKATO Takenori static int 292df8bae1dSRodney W. Grimes union_lookup(ap) 293df8bae1dSRodney W. Grimes struct vop_lookup_args /* { 294df8bae1dSRodney W. Grimes struct vnodeop_desc *a_desc; 295df8bae1dSRodney W. Grimes struct vnode *a_dvp; 296df8bae1dSRodney W. Grimes struct vnode **a_vpp; 297df8bae1dSRodney W. Grimes struct componentname *a_cnp; 298df8bae1dSRodney W. Grimes } */ *ap; 299df8bae1dSRodney W. Grimes { 300df8bae1dSRodney W. Grimes int error; 301df8bae1dSRodney W. Grimes int uerror, lerror; 302df8bae1dSRodney W. Grimes struct vnode *uppervp, *lowervp; 303df8bae1dSRodney W. Grimes struct vnode *upperdvp, *lowerdvp; 3042a31267eSMatthew Dillon struct vnode *dvp = ap->a_dvp; /* starting dir */ 3052a31267eSMatthew Dillon struct union_node *dun = VTOUNION(dvp); /* associated union node */ 306df8bae1dSRodney W. Grimes struct componentname *cnp = ap->a_cnp; 307b40ce416SJulian Elischer struct thread *td = cnp->cn_thread; 308df8bae1dSRodney W. Grimes int lockparent = cnp->cn_flags & LOCKPARENT; 309df8bae1dSRodney W. Grimes struct union_mount *um = MOUNTTOUNIONMOUNT(dvp->v_mount); 31027ed09c2SMatthew Dillon struct ucred *saved_cred = NULL; 311996c772fSJohn Dyson int iswhiteout; 312996c772fSJohn Dyson struct vattr va; 313996c772fSJohn Dyson 3142a31267eSMatthew Dillon *ap->a_vpp = NULLVP; 3156ca02614SKATO Takenori 3166ca02614SKATO Takenori /* 3171c4ccf09SDon Lewis * Disallow write attempts to the filesystem mounted read-only. 3186ca02614SKATO Takenori */ 3192a31267eSMatthew Dillon if ((cnp->cn_flags & ISLASTCN) && 3202a31267eSMatthew Dillon (dvp->v_mount->mnt_flag & MNT_RDONLY) && 3212a31267eSMatthew Dillon (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) { 3226ca02614SKATO Takenori return (EROFS); 323996c772fSJohn Dyson } 324df8bae1dSRodney W. Grimes 3252a31267eSMatthew Dillon /* 3261c4ccf09SDon Lewis * For any lookups we do, always return with the parent locked. 3272a31267eSMatthew Dillon */ 328df8bae1dSRodney W. Grimes cnp->cn_flags |= LOCKPARENT; 329df8bae1dSRodney W. Grimes 330df8bae1dSRodney W. Grimes lowerdvp = dun->un_lowervp; 331df8bae1dSRodney W. Grimes uppervp = NULLVP; 332df8bae1dSRodney W. Grimes lowervp = NULLVP; 333996c772fSJohn Dyson iswhiteout = 0; 334df8bae1dSRodney W. Grimes 3352a31267eSMatthew Dillon uerror = ENOENT; 3362a31267eSMatthew Dillon lerror = ENOENT; 3372a31267eSMatthew Dillon 3382a31267eSMatthew Dillon /* 3392a31267eSMatthew Dillon * Get a private lock on uppervp and a reference, effectively 3402a31267eSMatthew Dillon * taking it out of the union_node's control. 3412a31267eSMatthew Dillon * 3422a31267eSMatthew Dillon * We must lock upperdvp while holding our lock on dvp 3432a31267eSMatthew Dillon * to avoid a deadlock. 3442a31267eSMatthew Dillon */ 345b40ce416SJulian Elischer upperdvp = union_lock_upper(dun, td); 346afc2a558SKATO Takenori 347df8bae1dSRodney W. Grimes /* 3481c4ccf09SDon Lewis * Do the lookup in the upper level. 3491c4ccf09SDon Lewis * If that level consumes additional pathnames, 350df8bae1dSRodney W. Grimes * then assume that something special is going 351df8bae1dSRodney W. Grimes * on and just return that vnode. 352df8bae1dSRodney W. Grimes */ 353996c772fSJohn Dyson if (upperdvp != NULLVP) { 354ee582cdfSKATO Takenori /* 3552a31267eSMatthew Dillon * We do not have to worry about the DOTDOT case, we've 3562a31267eSMatthew Dillon * already unlocked dvp. 357ee582cdfSKATO Takenori */ 3582a31267eSMatthew Dillon UDEBUG(("A %p\n", upperdvp)); 3592a31267eSMatthew Dillon 3602a31267eSMatthew Dillon /* 3612a31267eSMatthew Dillon * Do the lookup. We must supply a locked and referenced 3622a31267eSMatthew Dillon * upperdvp to the function and will get a new locked and 3631c4ccf09SDon Lewis * referenced upperdvp back, with the old having been 3642a31267eSMatthew Dillon * dereferenced. 3652a31267eSMatthew Dillon * 3662a31267eSMatthew Dillon * If an error is returned, uppervp will be NULLVP. If no 3671c4ccf09SDon Lewis * error occurs, uppervp will be the locked and referenced. 3681c4ccf09SDon Lewis * Return vnode, or possibly NULL, depending on what is being 3692a31267eSMatthew Dillon * requested. It is possible that the returned uppervp 3702a31267eSMatthew Dillon * will be the same as upperdvp. 3712a31267eSMatthew Dillon */ 3722a31267eSMatthew Dillon uerror = union_lookup1(um->um_uppervp, &upperdvp, &uppervp, cnp); 3732a31267eSMatthew Dillon UDEBUG(( 3742a31267eSMatthew Dillon "uerror %d upperdvp %p %d/%d, uppervp %p ref=%d/lck=%d\n", 3752a31267eSMatthew Dillon uerror, 3762a31267eSMatthew Dillon upperdvp, 3774d93c0beSJeff Roberson vrefcnt(upperdvp), 3786bdfe06aSEivind Eklund VOP_ISLOCKED(upperdvp, NULL), 3792a31267eSMatthew Dillon uppervp, 3804d93c0beSJeff Roberson (uppervp ? vrefcnt(uppervp) : -99), 3816bdfe06aSEivind Eklund (uppervp ? VOP_ISLOCKED(uppervp, NULL) : -99) 3822a31267eSMatthew Dillon )); 3832a31267eSMatthew Dillon 3846ca02614SKATO Takenori /* 3851c4ccf09SDon Lewis * Disallow write attempts to the filesystem mounted read-only. 3866ca02614SKATO Takenori */ 3876ca02614SKATO Takenori if (uerror == EJUSTRETURN && (cnp->cn_flags & ISLASTCN) && 3886ca02614SKATO Takenori (dvp->v_mount->mnt_flag & MNT_RDONLY) && 3896ca02614SKATO Takenori (cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME)) { 3902a31267eSMatthew Dillon error = EROFS; 391afc2a558SKATO Takenori goto out; 392df8bae1dSRodney W. Grimes } 3932a31267eSMatthew Dillon 3942a31267eSMatthew Dillon /* 3951c4ccf09SDon Lewis * Special case: If cn_consume != 0 then skip out. The result 3962a31267eSMatthew Dillon * of the lookup is transfered to our return variable. If 3972a31267eSMatthew Dillon * an error occured we have to throw away the results. 3982a31267eSMatthew Dillon */ 3992a31267eSMatthew Dillon 4002a31267eSMatthew Dillon if (cnp->cn_consume != 0) { 4012a31267eSMatthew Dillon if ((error = uerror) == 0) { 4022a31267eSMatthew Dillon *ap->a_vpp = uppervp; 4032a31267eSMatthew Dillon uppervp = NULL; 4042a31267eSMatthew Dillon } 4052a31267eSMatthew Dillon goto out; 4062a31267eSMatthew Dillon } 4072a31267eSMatthew Dillon 4082a31267eSMatthew Dillon /* 4091c4ccf09SDon Lewis * Calculate whiteout, fall through. 4102a31267eSMatthew Dillon */ 4112a31267eSMatthew Dillon 412996c772fSJohn Dyson if (uerror == ENOENT || uerror == EJUSTRETURN) { 413996c772fSJohn Dyson if (cnp->cn_flags & ISWHITEOUT) { 414996c772fSJohn Dyson iswhiteout = 1; 415996c772fSJohn Dyson } else if (lowerdvp != NULLVP) { 416e3a285c7SMatthew Dillon int terror; 417e3a285c7SMatthew Dillon 418e3a285c7SMatthew Dillon terror = VOP_GETATTR(upperdvp, &va, 419b40ce416SJulian Elischer cnp->cn_cred, cnp->cn_thread); 420e3a285c7SMatthew Dillon if (terror == 0 && (va.va_flags & OPAQUE)) 421996c772fSJohn Dyson iswhiteout = 1; 422996c772fSJohn Dyson } 423996c772fSJohn Dyson } 424df8bae1dSRodney W. Grimes } 425df8bae1dSRodney W. Grimes 426df8bae1dSRodney W. Grimes /* 4271c4ccf09SDon Lewis * In a similar way to the upper layer, do the lookup 4281c4ccf09SDon Lewis * in the lower layer. This time, if there is some 429df8bae1dSRodney W. Grimes * component magic going on, then vput whatever we got 430df8bae1dSRodney W. Grimes * back from the upper layer and return the lower vnode 431df8bae1dSRodney W. Grimes * instead. 432df8bae1dSRodney W. Grimes */ 4332a31267eSMatthew Dillon 434996c772fSJohn Dyson if (lowerdvp != NULLVP && !iswhiteout) { 435df8bae1dSRodney W. Grimes int nameiop; 436df8bae1dSRodney W. Grimes 4372a31267eSMatthew Dillon UDEBUG(("B %p\n", lowerdvp)); 438df8bae1dSRodney W. Grimes 439df8bae1dSRodney W. Grimes /* 4402a31267eSMatthew Dillon * Force only LOOKUPs on the lower node, since 441df8bae1dSRodney W. Grimes * we won't be making changes to it anyway. 442df8bae1dSRodney W. Grimes */ 443df8bae1dSRodney W. Grimes nameiop = cnp->cn_nameiop; 444df8bae1dSRodney W. Grimes cnp->cn_nameiop = LOOKUP; 445df8bae1dSRodney W. Grimes if (um->um_op == UNMNT_BELOW) { 446df8bae1dSRodney W. Grimes saved_cred = cnp->cn_cred; 447df8bae1dSRodney W. Grimes cnp->cn_cred = um->um_cred; 448df8bae1dSRodney W. Grimes } 4492a31267eSMatthew Dillon 450ee582cdfSKATO Takenori /* 451ee582cdfSKATO Takenori * We shouldn't have to worry about locking interactions 452ee582cdfSKATO Takenori * between the lower layer and our union layer (w.r.t. 453ee582cdfSKATO Takenori * `..' processing) because we don't futz with lowervp 454ee582cdfSKATO Takenori * locks in the union-node instantiation code path. 4552a31267eSMatthew Dillon * 4562a31267eSMatthew Dillon * union_lookup1() requires lowervp to be locked on entry, 4572a31267eSMatthew Dillon * and it will be unlocked on return. The ref count will 4582a31267eSMatthew Dillon * not change. On return lowervp doesn't represent anything 4592a31267eSMatthew Dillon * to us so we NULL it out. 460ee582cdfSKATO Takenori */ 4612a31267eSMatthew Dillon VREF(lowerdvp); 462b40ce416SJulian Elischer vn_lock(lowerdvp, LK_EXCLUSIVE | LK_RETRY, td); 4632a31267eSMatthew Dillon lerror = union_lookup1(um->um_lowervp, &lowerdvp, &lowervp, cnp); 4642a31267eSMatthew Dillon if (lowerdvp == lowervp) 4652a31267eSMatthew Dillon vrele(lowerdvp); 4662a31267eSMatthew Dillon else 4672a31267eSMatthew Dillon vput(lowerdvp); 4682a31267eSMatthew Dillon lowerdvp = NULL; /* lowerdvp invalid after vput */ 4692a31267eSMatthew Dillon 470df8bae1dSRodney W. Grimes if (um->um_op == UNMNT_BELOW) 471df8bae1dSRodney W. Grimes cnp->cn_cred = saved_cred; 472df8bae1dSRodney W. Grimes cnp->cn_nameiop = nameiop; 473df8bae1dSRodney W. Grimes 474f8fc96b5SKATO Takenori if (cnp->cn_consume != 0 || lerror == EACCES) { 4752a31267eSMatthew Dillon if ((error = lerror) == 0) { 476df8bae1dSRodney W. Grimes *ap->a_vpp = lowervp; 4772a31267eSMatthew Dillon lowervp = NULL; 4782a31267eSMatthew Dillon } 479afc2a558SKATO Takenori goto out; 480df8bae1dSRodney W. Grimes } 481df8bae1dSRodney W. Grimes } else { 4822a31267eSMatthew Dillon UDEBUG(("C %p\n", lowerdvp)); 483996c772fSJohn Dyson if ((cnp->cn_flags & ISDOTDOT) && dun->un_pvp != NULLVP) { 4842a31267eSMatthew Dillon if ((lowervp = LOWERVP(dun->un_pvp)) != NULL) { 485996c772fSJohn Dyson VREF(lowervp); 486b40ce416SJulian Elischer vn_lock(lowervp, LK_EXCLUSIVE | LK_RETRY, td); 487996c772fSJohn Dyson lerror = 0; 488996c772fSJohn Dyson } 489996c772fSJohn Dyson } 490df8bae1dSRodney W. Grimes } 491df8bae1dSRodney W. Grimes 492df8bae1dSRodney W. Grimes /* 4932a31267eSMatthew Dillon * Ok. Now we have uerror, uppervp, upperdvp, lerror, and lowervp. 494df8bae1dSRodney W. Grimes * 4952a31267eSMatthew Dillon * 1. If both layers returned an error, select the upper layer. 496df8bae1dSRodney W. Grimes * 4971c4ccf09SDon Lewis * 2. If the upper layer failed and the bottom layer succeeded, 4982a31267eSMatthew Dillon * two subcases occur: 499df8bae1dSRodney W. Grimes * 5002a31267eSMatthew Dillon * a. The bottom vnode is not a directory, in which case 5012a31267eSMatthew Dillon * just return a new union vnode referencing an 5022a31267eSMatthew Dillon * empty top layer and the existing bottom layer. 5032a31267eSMatthew Dillon * 5041c4ccf09SDon Lewis * b. The bottom vnode is a directory, in which case 5052a31267eSMatthew Dillon * create a new directory in the top layer and 5062a31267eSMatthew Dillon * and fall through to case 3. 5072a31267eSMatthew Dillon * 5081c4ccf09SDon Lewis * 3. If the top layer succeeded, then return a new union 509df8bae1dSRodney W. Grimes * vnode referencing whatever the new top layer and 510df8bae1dSRodney W. Grimes * whatever the bottom layer returned. 511df8bae1dSRodney W. Grimes */ 512df8bae1dSRodney W. Grimes 513df8bae1dSRodney W. Grimes /* case 1. */ 514df8bae1dSRodney W. Grimes if ((uerror != 0) && (lerror != 0)) { 515afc2a558SKATO Takenori error = uerror; 516afc2a558SKATO Takenori goto out; 517df8bae1dSRodney W. Grimes } 518df8bae1dSRodney W. Grimes 519df8bae1dSRodney W. Grimes /* case 2. */ 520df8bae1dSRodney W. Grimes if (uerror != 0 /* && (lerror == 0) */ ) { 521df8bae1dSRodney W. Grimes if (lowervp->v_type == VDIR) { /* case 2b. */ 5222a31267eSMatthew Dillon KASSERT(uppervp == NULL, ("uppervp unexpectedly non-NULL")); 5232a31267eSMatthew Dillon /* 5241c4ccf09SDon Lewis * Oops, uppervp has a problem, we may have to shadow. 5252a31267eSMatthew Dillon */ 526df8bae1dSRodney W. Grimes uerror = union_mkshadow(um, upperdvp, cnp, &uppervp); 527df8bae1dSRodney W. Grimes if (uerror) { 528afc2a558SKATO Takenori error = uerror; 529afc2a558SKATO Takenori goto out; 530df8bae1dSRodney W. Grimes } 531df8bae1dSRodney W. Grimes } 532df8bae1dSRodney W. Grimes } 533df8bae1dSRodney W. Grimes 5342a31267eSMatthew Dillon /* 5351c4ccf09SDon Lewis * Must call union_allocvp() with both the upper and lower vnodes 5362a31267eSMatthew Dillon * referenced and the upper vnode locked. ap->a_vpp is returned 5372a31267eSMatthew Dillon * referenced and locked. lowervp, uppervp, and upperdvp are 5382a31267eSMatthew Dillon * absorbed by union_allocvp() whether it succeeds or fails. 5392a31267eSMatthew Dillon * 5402a31267eSMatthew Dillon * upperdvp is the parent directory of uppervp which may be 5412a31267eSMatthew Dillon * different, depending on the path, from dvp->un_uppervp. That's 5422a31267eSMatthew Dillon * why it is a separate argument. Note that it must be unlocked. 5432a31267eSMatthew Dillon * 5442a31267eSMatthew Dillon * dvp must be locked on entry to the call and will be locked on 5452a31267eSMatthew Dillon * return. 5462a31267eSMatthew Dillon */ 5472a31267eSMatthew Dillon 5482a31267eSMatthew Dillon if (uppervp && uppervp != upperdvp) 549b40ce416SJulian Elischer VOP_UNLOCK(uppervp, 0, td); 5502a31267eSMatthew Dillon if (lowervp) 551b40ce416SJulian Elischer VOP_UNLOCK(lowervp, 0, td); 5522a31267eSMatthew Dillon if (upperdvp) 553b40ce416SJulian Elischer VOP_UNLOCK(upperdvp, 0, td); 554df8bae1dSRodney W. Grimes 555df8bae1dSRodney W. Grimes error = union_allocvp(ap->a_vpp, dvp->v_mount, dvp, upperdvp, cnp, 556996c772fSJohn Dyson uppervp, lowervp, 1); 557df8bae1dSRodney W. Grimes 5584d93c0beSJeff Roberson UDEBUG(("Create %p = %p %p refs=%d\n", *ap->a_vpp, uppervp, lowervp, (*ap->a_vpp) ? vrefcnt(*ap->a_vpp) : -99)); 5592a31267eSMatthew Dillon 5602a31267eSMatthew Dillon uppervp = NULL; 5612a31267eSMatthew Dillon upperdvp = NULL; 5622a31267eSMatthew Dillon lowervp = NULL; 5632a31267eSMatthew Dillon 5642a31267eSMatthew Dillon /* 5652a31267eSMatthew Dillon * Termination Code 5662a31267eSMatthew Dillon * 5672a31267eSMatthew Dillon * - put away any extra junk laying around. Note that lowervp 5682a31267eSMatthew Dillon * (if not NULL) will never be the same as *ap->a_vp and 5692a31267eSMatthew Dillon * neither will uppervp, because when we set that state we 5702a31267eSMatthew Dillon * NULL-out lowervp or uppervp. On the otherhand, upperdvp 5712a31267eSMatthew Dillon * may match uppervp or *ap->a_vpp. 5722a31267eSMatthew Dillon * 5732a31267eSMatthew Dillon * - relock/unlock dvp if appropriate. 5742a31267eSMatthew Dillon */ 5752a31267eSMatthew Dillon 5762a31267eSMatthew Dillon out: 5772a31267eSMatthew Dillon if (upperdvp) { 5782a31267eSMatthew Dillon if (upperdvp == uppervp || upperdvp == *ap->a_vpp) 5792a31267eSMatthew Dillon vrele(upperdvp); 5802a31267eSMatthew Dillon else 5812a31267eSMatthew Dillon vput(upperdvp); 5822a31267eSMatthew Dillon } 5832a31267eSMatthew Dillon 5842a31267eSMatthew Dillon if (uppervp) 585df8bae1dSRodney W. Grimes vput(uppervp); 5862a31267eSMatthew Dillon 5872a31267eSMatthew Dillon if (lowervp) 5882a31267eSMatthew Dillon vput(lowervp); 5892a31267eSMatthew Dillon 5902a31267eSMatthew Dillon /* 5912a31267eSMatthew Dillon * Restore LOCKPARENT state 5922a31267eSMatthew Dillon */ 5932a31267eSMatthew Dillon 5942a31267eSMatthew Dillon if (!lockparent) 5952a31267eSMatthew Dillon cnp->cn_flags &= ~LOCKPARENT; 5962a31267eSMatthew Dillon 5972a31267eSMatthew Dillon UDEBUG(("Out %d vpp %p/%d lower %p upper %p\n", error, *ap->a_vpp, 5984d93c0beSJeff Roberson ((*ap->a_vpp) ? vrefcnt(*ap->a_vpp) : -99), 5992a31267eSMatthew Dillon lowervp, uppervp)); 6002a31267eSMatthew Dillon 60195eac68fSDavid Schultz if (error == 0 || error == EJUSTRETURN) { 6022a31267eSMatthew Dillon /* 60395eac68fSDavid Schultz * dvp lock state, determine whether to relock dvp. 60495eac68fSDavid Schultz * We are expected to unlock dvp unless: 6052a31267eSMatthew Dillon * 60695eac68fSDavid Schultz * - there was an error (other than EJUSTRETURN), or 6072a31267eSMatthew Dillon * - we hit the last component and lockparent is true 6082a31267eSMatthew Dillon */ 6092a31267eSMatthew Dillon if (*ap->a_vpp != dvp) { 61095eac68fSDavid Schultz if (!lockparent || (cnp->cn_flags & ISLASTCN) == 0) 611b40ce416SJulian Elischer VOP_UNLOCK(dvp, 0, td); 6122a31267eSMatthew Dillon } 6132a31267eSMatthew Dillon 614f5a5311eSKATO Takenori if (cnp->cn_namelen == 1 && 615f5a5311eSKATO Takenori cnp->cn_nameptr[0] == '.' && 616f5a5311eSKATO Takenori *ap->a_vpp != dvp) { 61795eac68fSDavid Schultz #ifdef DIAGNOSTIC 61895eac68fSDavid Schultz vprint("union_lookup: vp", *ap->a_vpp); 61995eac68fSDavid Schultz vprint("union_lookup: dvp", dvp); 620f5a5311eSKATO Takenori #endif 62195eac68fSDavid Schultz panic("union_lookup returning . (%p) != startdir (%p)", 62295eac68fSDavid Schultz *ap->a_vpp, dvp); 62395eac68fSDavid Schultz } 62495eac68fSDavid Schultz } 625afc2a558SKATO Takenori 626df8bae1dSRodney W. Grimes return (error); 627df8bae1dSRodney W. Grimes } 628df8bae1dSRodney W. Grimes 6292a31267eSMatthew Dillon /* 6302a31267eSMatthew Dillon * union_create: 6312a31267eSMatthew Dillon * 6322a31267eSMatthew Dillon * a_dvp is locked on entry and remains locked on return. a_vpp is returned 6332a31267eSMatthew Dillon * locked if no error occurs, otherwise it is garbage. 6342a31267eSMatthew Dillon */ 6352a31267eSMatthew Dillon 636c9bf0111SKATO Takenori static int 637df8bae1dSRodney W. Grimes union_create(ap) 638df8bae1dSRodney W. Grimes struct vop_create_args /* { 639df8bae1dSRodney W. Grimes struct vnode *a_dvp; 640df8bae1dSRodney W. Grimes struct vnode **a_vpp; 641df8bae1dSRodney W. Grimes struct componentname *a_cnp; 642df8bae1dSRodney W. Grimes struct vattr *a_vap; 643df8bae1dSRodney W. Grimes } */ *ap; 644df8bae1dSRodney W. Grimes { 6457be2d300SMike Smith struct union_node *dun = VTOUNION(ap->a_dvp); 646996c772fSJohn Dyson struct componentname *cnp = ap->a_cnp; 647b40ce416SJulian Elischer struct thread *td = cnp->cn_thread; 6482a31267eSMatthew Dillon struct vnode *dvp; 6492a31267eSMatthew Dillon int error = EROFS; 650df8bae1dSRodney W. Grimes 651b40ce416SJulian Elischer if ((dvp = union_lock_upper(dun, td)) != NULL) { 652df8bae1dSRodney W. Grimes struct vnode *vp; 653996c772fSJohn Dyson struct mount *mp; 654df8bae1dSRodney W. Grimes 655996c772fSJohn Dyson error = VOP_CREATE(dvp, &vp, cnp, ap->a_vap); 6562a31267eSMatthew Dillon if (error == 0) { 6577be2d300SMike Smith mp = ap->a_dvp->v_mount; 658b40ce416SJulian Elischer VOP_UNLOCK(vp, 0, td); 6594d93c0beSJeff Roberson UDEBUG(("ALLOCVP-1 FROM %p REFS %d\n", vp, vrefcnt(vp))); 6602a31267eSMatthew Dillon error = union_allocvp(ap->a_vpp, mp, NULLVP, NULLVP, 6612a31267eSMatthew Dillon cnp, vp, NULLVP, 1); 6624d93c0beSJeff Roberson UDEBUG(("ALLOCVP-2B FROM %p REFS %d\n", *ap->a_vpp, vrefcnt(vp))); 663df8bae1dSRodney W. Grimes } 664b40ce416SJulian Elischer union_unlock_upper(dvp, td); 6652a31267eSMatthew Dillon } 6662a31267eSMatthew Dillon return (error); 667df8bae1dSRodney W. Grimes } 668df8bae1dSRodney W. Grimes 669c9bf0111SKATO Takenori static int 670996c772fSJohn Dyson union_whiteout(ap) 671996c772fSJohn Dyson struct vop_whiteout_args /* { 672996c772fSJohn Dyson struct vnode *a_dvp; 673996c772fSJohn Dyson struct componentname *a_cnp; 674996c772fSJohn Dyson int a_flags; 675996c772fSJohn Dyson } */ *ap; 676996c772fSJohn Dyson { 677996c772fSJohn Dyson struct union_node *un = VTOUNION(ap->a_dvp); 678996c772fSJohn Dyson struct componentname *cnp = ap->a_cnp; 6792a31267eSMatthew Dillon struct vnode *uppervp; 6803bb3827fSDavid Schultz int error; 681996c772fSJohn Dyson 682ac092fb3SDavid Schultz switch (ap->a_flags) { 683ac092fb3SDavid Schultz case CREATE: 684ac092fb3SDavid Schultz case DELETE: 6853bb3827fSDavid Schultz uppervp = union_lock_upper(un, cnp->cn_thread); 6863bb3827fSDavid Schultz if (uppervp != NULLVP) { 6872a31267eSMatthew Dillon error = VOP_WHITEOUT(un->un_uppervp, cnp, ap->a_flags); 688b40ce416SJulian Elischer union_unlock_upper(uppervp, cnp->cn_thread); 6893bb3827fSDavid Schultz } else 6903bb3827fSDavid Schultz error = EOPNOTSUPP; 6913bb3827fSDavid Schultz break; 6923bb3827fSDavid Schultz case LOOKUP: 6933bb3827fSDavid Schultz error = EOPNOTSUPP; 694ac092fb3SDavid Schultz break; 695ac092fb3SDavid Schultz default: 696ac092fb3SDavid Schultz panic("union_whiteout: unknown op"); 697ac092fb3SDavid Schultz } 6982a31267eSMatthew Dillon return (error); 6992a31267eSMatthew Dillon } 7002a31267eSMatthew Dillon 7012a31267eSMatthew Dillon /* 7022a31267eSMatthew Dillon * union_mknod: 7032a31267eSMatthew Dillon * 7042a31267eSMatthew Dillon * a_dvp is locked on entry and should remain locked on return. 7052a31267eSMatthew Dillon * a_vpp is garbagre whether an error occurs or not. 7062a31267eSMatthew Dillon */ 707996c772fSJohn Dyson 708c9bf0111SKATO Takenori static int 709df8bae1dSRodney W. Grimes union_mknod(ap) 710df8bae1dSRodney W. Grimes struct vop_mknod_args /* { 711df8bae1dSRodney W. Grimes struct vnode *a_dvp; 712df8bae1dSRodney W. Grimes struct vnode **a_vpp; 713df8bae1dSRodney W. Grimes struct componentname *a_cnp; 714df8bae1dSRodney W. Grimes struct vattr *a_vap; 715df8bae1dSRodney W. Grimes } */ *ap; 716df8bae1dSRodney W. Grimes { 7177be2d300SMike Smith struct union_node *dun = VTOUNION(ap->a_dvp); 718996c772fSJohn Dyson struct componentname *cnp = ap->a_cnp; 7192a31267eSMatthew Dillon struct vnode *dvp; 7202a31267eSMatthew Dillon int error = EROFS; 721df8bae1dSRodney W. Grimes 722b40ce416SJulian Elischer if ((dvp = union_lock_upper(dun, cnp->cn_thread)) != NULL) { 723edfe736dSEivind Eklund error = VOP_MKNOD(dvp, ap->a_vpp, cnp, ap->a_vap); 724b40ce416SJulian Elischer union_unlock_upper(dvp, cnp->cn_thread); 725df8bae1dSRodney W. Grimes } 726df8bae1dSRodney W. Grimes return (error); 727df8bae1dSRodney W. Grimes } 728df8bae1dSRodney W. Grimes 7292a31267eSMatthew Dillon /* 7302a31267eSMatthew Dillon * union_open: 7312a31267eSMatthew Dillon * 7322a31267eSMatthew Dillon * run open VOP. When opening the underlying vnode we have to mimic 7331c4ccf09SDon Lewis * vn_open(). What we *really* need to do to avoid screwups if the 7342a31267eSMatthew Dillon * open semantics change is to call vn_open(). For example, ufs blows 7352a31267eSMatthew Dillon * up if you open a file but do not vmio it prior to writing. 7362a31267eSMatthew Dillon */ 737df8bae1dSRodney W. Grimes 738c9bf0111SKATO Takenori static int 739df8bae1dSRodney W. Grimes union_open(ap) 740df8bae1dSRodney W. Grimes struct vop_open_args /* { 741df8bae1dSRodney W. Grimes struct vnodeop_desc *a_desc; 742df8bae1dSRodney W. Grimes struct vnode *a_vp; 743df8bae1dSRodney W. Grimes int a_mode; 744df8bae1dSRodney W. Grimes struct ucred *a_cred; 745b40ce416SJulian Elischer struct thread *a_td; 746df8bae1dSRodney W. Grimes } */ *ap; 747df8bae1dSRodney W. Grimes { 748df8bae1dSRodney W. Grimes struct union_node *un = VTOUNION(ap->a_vp); 749df8bae1dSRodney W. Grimes struct vnode *tvp; 750df8bae1dSRodney W. Grimes int mode = ap->a_mode; 751df8bae1dSRodney W. Grimes struct ucred *cred = ap->a_cred; 752b40ce416SJulian Elischer struct thread *td = ap->a_td; 7532a31267eSMatthew Dillon int error = 0; 7542a31267eSMatthew Dillon int tvpisupper = 1; 755df8bae1dSRodney W. Grimes 756df8bae1dSRodney W. Grimes /* 757df8bae1dSRodney W. Grimes * If there is an existing upper vp then simply open that. 7582a31267eSMatthew Dillon * The upper vp takes precedence over the lower vp. When opening 7592a31267eSMatthew Dillon * a lower vp for writing copy it to the uppervp and then open the 7602a31267eSMatthew Dillon * uppervp. 7612a31267eSMatthew Dillon * 7622a31267eSMatthew Dillon * At the end of this section tvp will be left locked. 763df8bae1dSRodney W. Grimes */ 764b40ce416SJulian Elischer if ((tvp = union_lock_upper(un, td)) == NULLVP) { 765df8bae1dSRodney W. Grimes /* 766df8bae1dSRodney W. Grimes * If the lower vnode is being opened for writing, then 767df8bae1dSRodney W. Grimes * copy the file contents to the upper vnode and open that, 768df8bae1dSRodney W. Grimes * otherwise can simply open the lower vnode. 769df8bae1dSRodney W. Grimes */ 770df8bae1dSRodney W. Grimes tvp = un->un_lowervp; 771df8bae1dSRodney W. Grimes if ((ap->a_mode & FWRITE) && (tvp->v_type == VREG)) { 7722a31267eSMatthew Dillon int docopy = !(mode & O_TRUNC); 773b40ce416SJulian Elischer error = union_copyup(un, docopy, cred, td); 774b40ce416SJulian Elischer tvp = union_lock_upper(un, td); 7752a31267eSMatthew Dillon } else { 7762a31267eSMatthew Dillon un->un_openl++; 7772a31267eSMatthew Dillon VREF(tvp); 778b40ce416SJulian Elischer vn_lock(tvp, LK_EXCLUSIVE | LK_RETRY, td); 7792a31267eSMatthew Dillon tvpisupper = 0; 7802a31267eSMatthew Dillon } 7812a31267eSMatthew Dillon } 7822a31267eSMatthew Dillon 7832a31267eSMatthew Dillon /* 7841c4ccf09SDon Lewis * We are holding the correct vnode, open it. 7852a31267eSMatthew Dillon */ 7862a31267eSMatthew Dillon 787df8bae1dSRodney W. Grimes if (error == 0) 788a8d43c90SPoul-Henning Kamp error = VOP_OPEN(tvp, mode, cred, td, -1); 7892a31267eSMatthew Dillon 7902a31267eSMatthew Dillon /* 7911c4ccf09SDon Lewis * This is absolutely necessary or UFS will blow up. 7922a31267eSMatthew Dillon */ 7932a31267eSMatthew Dillon if (error == 0 && vn_canvmio(tvp) == TRUE) { 794b40ce416SJulian Elischer error = vfs_object_create(tvp, td, cred); 7952a31267eSMatthew Dillon } 7962a31267eSMatthew Dillon 7972a31267eSMatthew Dillon /* 7981c4ccf09SDon Lewis * Release any locks held. 7992a31267eSMatthew Dillon */ 8002a31267eSMatthew Dillon if (tvpisupper) { 8012a31267eSMatthew Dillon if (tvp) 802b40ce416SJulian Elischer union_unlock_upper(tvp, td); 8032a31267eSMatthew Dillon } else { 8042a31267eSMatthew Dillon vput(tvp); 8052a31267eSMatthew Dillon } 806df8bae1dSRodney W. Grimes return (error); 807df8bae1dSRodney W. Grimes } 808df8bae1dSRodney W. Grimes 809df8bae1dSRodney W. Grimes /* 8102a31267eSMatthew Dillon * union_close: 8112a31267eSMatthew Dillon * 8122a31267eSMatthew Dillon * It is unclear whether a_vp is passed locked or unlocked. Whatever 8132a31267eSMatthew Dillon * the case we do not change it. 814df8bae1dSRodney W. Grimes */ 815df8bae1dSRodney W. Grimes 816c9bf0111SKATO Takenori static int 817df8bae1dSRodney W. Grimes union_close(ap) 818df8bae1dSRodney W. Grimes struct vop_close_args /* { 819df8bae1dSRodney W. Grimes struct vnode *a_vp; 820df8bae1dSRodney W. Grimes int a_fflag; 821df8bae1dSRodney W. Grimes struct ucred *a_cred; 822b40ce416SJulian Elischer struct thread *a_td; 823df8bae1dSRodney W. Grimes } */ *ap; 824df8bae1dSRodney W. Grimes { 825df8bae1dSRodney W. Grimes struct union_node *un = VTOUNION(ap->a_vp); 826df8bae1dSRodney W. Grimes struct vnode *vp; 827df8bae1dSRodney W. Grimes 828996c772fSJohn Dyson if ((vp = un->un_uppervp) == NULLVP) { 829df8bae1dSRodney W. Grimes #ifdef UNION_DIAGNOSTIC 830df8bae1dSRodney W. Grimes if (un->un_openl <= 0) 831df8bae1dSRodney W. Grimes panic("union: un_openl cnt"); 832df8bae1dSRodney W. Grimes #endif 833df8bae1dSRodney W. Grimes --un->un_openl; 834df8bae1dSRodney W. Grimes vp = un->un_lowervp; 835df8bae1dSRodney W. Grimes } 836996c772fSJohn Dyson ap->a_vp = vp; 837996c772fSJohn Dyson return (VCALL(vp, VOFFSET(vop_close), ap)); 838df8bae1dSRodney W. Grimes } 839df8bae1dSRodney W. Grimes 840df8bae1dSRodney W. Grimes /* 841df8bae1dSRodney W. Grimes * Check access permission on the union vnode. 842df8bae1dSRodney W. Grimes * The access check being enforced is to check 843df8bae1dSRodney W. Grimes * against both the underlying vnode, and any 844df8bae1dSRodney W. Grimes * copied vnode. This ensures that no additional 845df8bae1dSRodney W. Grimes * file permissions are given away simply because 846df8bae1dSRodney W. Grimes * the user caused an implicit file copy. 847df8bae1dSRodney W. Grimes */ 848c9bf0111SKATO Takenori static int 849df8bae1dSRodney W. Grimes union_access(ap) 850df8bae1dSRodney W. Grimes struct vop_access_args /* { 851df8bae1dSRodney W. Grimes struct vnodeop_desc *a_desc; 852df8bae1dSRodney W. Grimes struct vnode *a_vp; 853df8bae1dSRodney W. Grimes int a_mode; 854df8bae1dSRodney W. Grimes struct ucred *a_cred; 855b40ce416SJulian Elischer struct thread *a_td; 856df8bae1dSRodney W. Grimes } */ *ap; 857df8bae1dSRodney W. Grimes { 858df8bae1dSRodney W. Grimes struct union_node *un = VTOUNION(ap->a_vp); 859b40ce416SJulian Elischer struct thread *td = ap->a_td; 860df8bae1dSRodney W. Grimes int error = EACCES; 861df8bae1dSRodney W. Grimes struct vnode *vp; 862df8bae1dSRodney W. Grimes 8636ca02614SKATO Takenori /* 8646ca02614SKATO Takenori * Disallow write attempts on filesystems mounted read-only. 8656ca02614SKATO Takenori */ 8662a31267eSMatthew Dillon if ((ap->a_mode & VWRITE) && 8672a31267eSMatthew Dillon (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)) { 8686ca02614SKATO Takenori switch (ap->a_vp->v_type) { 869831a80b0SMatthew Dillon case VREG: 870831a80b0SMatthew Dillon case VDIR: 871831a80b0SMatthew Dillon case VLNK: 8726ca02614SKATO Takenori return (EROFS); 873831a80b0SMatthew Dillon default: 874831a80b0SMatthew Dillon break; 8756ca02614SKATO Takenori } 8766ca02614SKATO Takenori } 8772a31267eSMatthew Dillon 878b40ce416SJulian Elischer if ((vp = union_lock_upper(un, td)) != NULLVP) { 879996c772fSJohn Dyson ap->a_vp = vp; 8802a31267eSMatthew Dillon error = VCALL(vp, VOFFSET(vop_access), ap); 881b40ce416SJulian Elischer union_unlock_upper(vp, td); 8822a31267eSMatthew Dillon return(error); 883df8bae1dSRodney W. Grimes } 884df8bae1dSRodney W. Grimes 885996c772fSJohn Dyson if ((vp = un->un_lowervp) != NULLVP) { 886b40ce416SJulian Elischer vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); 887996c772fSJohn Dyson ap->a_vp = vp; 8882a31267eSMatthew Dillon 8892a31267eSMatthew Dillon /* 8902a31267eSMatthew Dillon * Remove VWRITE from a_mode if our mount point is RW, because 8912a31267eSMatthew Dillon * we want to allow writes and lowervp may be read-only. 8922a31267eSMatthew Dillon */ 8932a31267eSMatthew Dillon if ((un->un_vnode->v_mount->mnt_flag & MNT_RDONLY) == 0) 8942a31267eSMatthew Dillon ap->a_mode &= ~VWRITE; 8952a31267eSMatthew Dillon 896996c772fSJohn Dyson error = VCALL(vp, VOFFSET(vop_access), ap); 897df8bae1dSRodney W. Grimes if (error == 0) { 8982a31267eSMatthew Dillon struct union_mount *um; 8992a31267eSMatthew Dillon 9002a31267eSMatthew Dillon um = MOUNTTOUNIONMOUNT(un->un_vnode->v_mount); 901df8bae1dSRodney W. Grimes 902996c772fSJohn Dyson if (um->um_op == UNMNT_BELOW) { 903996c772fSJohn Dyson ap->a_cred = um->um_cred; 904996c772fSJohn Dyson error = VCALL(vp, VOFFSET(vop_access), ap); 905df8bae1dSRodney W. Grimes } 906996c772fSJohn Dyson } 907b40ce416SJulian Elischer VOP_UNLOCK(vp, 0, td); 908df8bae1dSRodney W. Grimes } 909df8bae1dSRodney W. Grimes return(error); 910df8bae1dSRodney W. Grimes } 911df8bae1dSRodney W. Grimes 912df8bae1dSRodney W. Grimes /* 913996c772fSJohn Dyson * We handle getattr only to change the fsid and 914996c772fSJohn Dyson * track object sizes 9152a31267eSMatthew Dillon * 9162a31267eSMatthew Dillon * It's not clear whether VOP_GETATTR is to be 9172a31267eSMatthew Dillon * called with the vnode locked or not. stat() calls 9181c4ccf09SDon Lewis * it with (vp) locked, and fstat() calls it with 9192a31267eSMatthew Dillon * (vp) unlocked. 9202a31267eSMatthew Dillon * 9212a31267eSMatthew Dillon * Because of this we cannot use our normal locking functions 9222a31267eSMatthew Dillon * if we do not intend to lock the main a_vp node. At the moment 9232a31267eSMatthew Dillon * we are running without any specific locking at all, but beware 9242a31267eSMatthew Dillon * to any programmer that care must be taken if locking is added 9252a31267eSMatthew Dillon * to this function. 926df8bae1dSRodney W. Grimes */ 9272a31267eSMatthew Dillon 928c9bf0111SKATO Takenori static int 929df8bae1dSRodney W. Grimes union_getattr(ap) 930df8bae1dSRodney W. Grimes struct vop_getattr_args /* { 931df8bae1dSRodney W. Grimes struct vnode *a_vp; 932df8bae1dSRodney W. Grimes struct vattr *a_vap; 933df8bae1dSRodney W. Grimes struct ucred *a_cred; 934b40ce416SJulian Elischer struct thread *a_td; 935df8bae1dSRodney W. Grimes } */ *ap; 936df8bae1dSRodney W. Grimes { 937df8bae1dSRodney W. Grimes int error; 938df8bae1dSRodney W. Grimes struct union_node *un = VTOUNION(ap->a_vp); 9392a31267eSMatthew Dillon struct vnode *vp; 940df8bae1dSRodney W. Grimes struct vattr *vap; 941df8bae1dSRodney W. Grimes struct vattr va; 942df8bae1dSRodney W. Grimes 943df8bae1dSRodney W. Grimes /* 944df8bae1dSRodney W. Grimes * Some programs walk the filesystem hierarchy by counting 945df8bae1dSRodney W. Grimes * links to directories to avoid stat'ing all the time. 946df8bae1dSRodney W. Grimes * This means the link count on directories needs to be "correct". 947df8bae1dSRodney W. Grimes * The only way to do that is to call getattr on both layers 948df8bae1dSRodney W. Grimes * and fix up the link count. The link count will not necessarily 949df8bae1dSRodney W. Grimes * be accurate but will be large enough to defeat the tree walkers. 950df8bae1dSRodney W. Grimes */ 951df8bae1dSRodney W. Grimes 952df8bae1dSRodney W. Grimes vap = ap->a_vap; 953df8bae1dSRodney W. Grimes 9542a31267eSMatthew Dillon if ((vp = un->un_uppervp) != NULLVP) { 955b40ce416SJulian Elischer error = VOP_GETATTR(vp, vap, ap->a_cred, ap->a_td); 956df8bae1dSRodney W. Grimes if (error) 957df8bae1dSRodney W. Grimes return (error); 9581c4ccf09SDon Lewis /* XXX isn't this dangerous without a lock? */ 959996c772fSJohn Dyson union_newsize(ap->a_vp, vap->va_size, VNOVAL); 960df8bae1dSRodney W. Grimes } 961df8bae1dSRodney W. Grimes 962df8bae1dSRodney W. Grimes if (vp == NULLVP) { 963df8bae1dSRodney W. Grimes vp = un->un_lowervp; 964b2bde4cbSKATO Takenori } else if (vp->v_type == VDIR && un->un_lowervp != NULLVP) { 965df8bae1dSRodney W. Grimes vp = un->un_lowervp; 966df8bae1dSRodney W. Grimes vap = &va; 967df8bae1dSRodney W. Grimes } else { 968df8bae1dSRodney W. Grimes vp = NULLVP; 969df8bae1dSRodney W. Grimes } 970df8bae1dSRodney W. Grimes 971df8bae1dSRodney W. Grimes if (vp != NULLVP) { 972b40ce416SJulian Elischer error = VOP_GETATTR(vp, vap, ap->a_cred, ap->a_td); 973df8bae1dSRodney W. Grimes if (error) 974df8bae1dSRodney W. Grimes return (error); 9752a31267eSMatthew Dillon /* XXX isn't this dangerous without a lock? */ 976996c772fSJohn Dyson union_newsize(ap->a_vp, VNOVAL, vap->va_size); 977df8bae1dSRodney W. Grimes } 978df8bae1dSRodney W. Grimes 979df8bae1dSRodney W. Grimes if ((vap != ap->a_vap) && (vap->va_type == VDIR)) 980df8bae1dSRodney W. Grimes ap->a_vap->va_nlink += vap->va_nlink; 981df8bae1dSRodney W. Grimes return (0); 982df8bae1dSRodney W. Grimes } 983df8bae1dSRodney W. Grimes 984c9bf0111SKATO Takenori static int 985df8bae1dSRodney W. Grimes union_setattr(ap) 986df8bae1dSRodney W. Grimes struct vop_setattr_args /* { 987df8bae1dSRodney W. Grimes struct vnode *a_vp; 988df8bae1dSRodney W. Grimes struct vattr *a_vap; 989df8bae1dSRodney W. Grimes struct ucred *a_cred; 990b40ce416SJulian Elischer struct thread *a_td; 991df8bae1dSRodney W. Grimes } */ *ap; 992df8bae1dSRodney W. Grimes { 993df8bae1dSRodney W. Grimes struct union_node *un = VTOUNION(ap->a_vp); 994b40ce416SJulian Elischer struct thread *td = ap->a_td; 9956ca02614SKATO Takenori struct vattr *vap = ap->a_vap; 9962a31267eSMatthew Dillon struct vnode *uppervp; 997df8bae1dSRodney W. Grimes int error; 998df8bae1dSRodney W. Grimes 999df8bae1dSRodney W. Grimes /* 10006ca02614SKATO Takenori * Disallow write attempts on filesystems mounted read-only. 10016ca02614SKATO Takenori */ 10026ca02614SKATO Takenori if ((ap->a_vp->v_mount->mnt_flag & MNT_RDONLY) && 10036ca02614SKATO Takenori (vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL || 10046ca02614SKATO Takenori vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL || 10052a31267eSMatthew Dillon vap->va_mtime.tv_sec != VNOVAL || 10062a31267eSMatthew Dillon vap->va_mode != (mode_t)VNOVAL)) { 10076ca02614SKATO Takenori return (EROFS); 10082a31267eSMatthew Dillon } 10096ca02614SKATO Takenori 10106ca02614SKATO Takenori /* 10111c4ccf09SDon Lewis * Handle case of truncating lower object to zero size 1012df8bae1dSRodney W. Grimes * by creating a zero length upper object. This is to 1013df8bae1dSRodney W. Grimes * handle the case of open with O_TRUNC and O_CREAT. 1014df8bae1dSRodney W. Grimes */ 10152a31267eSMatthew Dillon if (un->un_uppervp == NULLVP && (un->un_lowervp->v_type == VREG)) { 1016996c772fSJohn Dyson error = union_copyup(un, (ap->a_vap->va_size != 0), 1017b40ce416SJulian Elischer ap->a_cred, ap->a_td); 1018df8bae1dSRodney W. Grimes if (error) 1019df8bae1dSRodney W. Grimes return (error); 1020df8bae1dSRodney W. Grimes } 1021df8bae1dSRodney W. Grimes 1022df8bae1dSRodney W. Grimes /* 1023df8bae1dSRodney W. Grimes * Try to set attributes in upper layer, 1024df8bae1dSRodney W. Grimes * otherwise return read-only filesystem error. 1025df8bae1dSRodney W. Grimes */ 10262a31267eSMatthew Dillon error = EROFS; 1027b40ce416SJulian Elischer if ((uppervp = union_lock_upper(un, td)) != NULLVP) { 1028df8bae1dSRodney W. Grimes error = VOP_SETATTR(un->un_uppervp, ap->a_vap, 1029b40ce416SJulian Elischer ap->a_cred, ap->a_td); 1030996c772fSJohn Dyson if ((error == 0) && (ap->a_vap->va_size != VNOVAL)) 1031996c772fSJohn Dyson union_newsize(ap->a_vp, ap->a_vap->va_size, VNOVAL); 1032b40ce416SJulian Elischer union_unlock_upper(uppervp, td); 10332a31267eSMatthew Dillon } 10342a31267eSMatthew Dillon return (error); 1035df8bae1dSRodney W. Grimes } 1036df8bae1dSRodney W. Grimes 1037c9bf0111SKATO Takenori static int 1038df8bae1dSRodney W. Grimes union_read(ap) 1039df8bae1dSRodney W. Grimes struct vop_read_args /* { 1040df8bae1dSRodney W. Grimes struct vnode *a_vp; 1041df8bae1dSRodney W. Grimes struct uio *a_uio; 1042df8bae1dSRodney W. Grimes int a_ioflag; 1043df8bae1dSRodney W. Grimes struct ucred *a_cred; 1044df8bae1dSRodney W. Grimes } */ *ap; 1045df8bae1dSRodney W. Grimes { 10462a31267eSMatthew Dillon struct union_node *un = VTOUNION(ap->a_vp); 1047b40ce416SJulian Elischer struct thread *td = ap->a_uio->uio_td; 10482a31267eSMatthew Dillon struct vnode *uvp; 10492a31267eSMatthew Dillon int error; 1050df8bae1dSRodney W. Grimes 1051b40ce416SJulian Elischer uvp = union_lock_other(un, td); 10522a31267eSMatthew Dillon KASSERT(uvp != NULL, ("union_read: backing vnode missing!")); 10532a31267eSMatthew Dillon 10542a31267eSMatthew Dillon error = VOP_READ(uvp, ap->a_uio, ap->a_ioflag, ap->a_cred); 1055b40ce416SJulian Elischer union_unlock_other(uvp, td); 1056996c772fSJohn Dyson 1057996c772fSJohn Dyson /* 1058996c772fSJohn Dyson * XXX 10591c4ccf09SDon Lewis * Perhaps the size of the underlying object has changed under 10601c4ccf09SDon Lewis * our feet. Take advantage of the offset information present 1061996c772fSJohn Dyson * in the uio structure. 1062996c772fSJohn Dyson */ 1063996c772fSJohn Dyson if (error == 0) { 1064996c772fSJohn Dyson struct union_node *un = VTOUNION(ap->a_vp); 1065996c772fSJohn Dyson off_t cur = ap->a_uio->uio_offset; 1066996c772fSJohn Dyson 10672a31267eSMatthew Dillon if (uvp == un->un_uppervp) { 1068996c772fSJohn Dyson if (cur > un->un_uppersz) 1069996c772fSJohn Dyson union_newsize(ap->a_vp, cur, VNOVAL); 1070996c772fSJohn Dyson } else { 1071996c772fSJohn Dyson if (cur > un->un_lowersz) 1072996c772fSJohn Dyson union_newsize(ap->a_vp, VNOVAL, cur); 1073996c772fSJohn Dyson } 1074996c772fSJohn Dyson } 1075df8bae1dSRodney W. Grimes return (error); 1076df8bae1dSRodney W. Grimes } 1077df8bae1dSRodney W. Grimes 1078c9bf0111SKATO Takenori static int 1079df8bae1dSRodney W. Grimes union_write(ap) 1080df8bae1dSRodney W. Grimes struct vop_read_args /* { 1081df8bae1dSRodney W. Grimes struct vnode *a_vp; 1082df8bae1dSRodney W. Grimes struct uio *a_uio; 1083df8bae1dSRodney W. Grimes int a_ioflag; 1084df8bae1dSRodney W. Grimes struct ucred *a_cred; 1085df8bae1dSRodney W. Grimes } */ *ap; 1086df8bae1dSRodney W. Grimes { 1087996c772fSJohn Dyson struct union_node *un = VTOUNION(ap->a_vp); 1088b40ce416SJulian Elischer struct thread *td = ap->a_uio->uio_td; 10892a31267eSMatthew Dillon struct vnode *uppervp; 10902a31267eSMatthew Dillon int error; 1091df8bae1dSRodney W. Grimes 1092b40ce416SJulian Elischer if ((uppervp = union_lock_upper(un, td)) == NULLVP) 1093996c772fSJohn Dyson panic("union: missing upper layer in write"); 1094996c772fSJohn Dyson 10952a31267eSMatthew Dillon error = VOP_WRITE(uppervp, ap->a_uio, ap->a_ioflag, ap->a_cred); 1096996c772fSJohn Dyson 1097996c772fSJohn Dyson /* 10981c4ccf09SDon Lewis * The size of the underlying object may be changed by the 1099996c772fSJohn Dyson * write. 1100996c772fSJohn Dyson */ 1101996c772fSJohn Dyson if (error == 0) { 1102996c772fSJohn Dyson off_t cur = ap->a_uio->uio_offset; 1103996c772fSJohn Dyson 1104996c772fSJohn Dyson if (cur > un->un_uppersz) 1105996c772fSJohn Dyson union_newsize(ap->a_vp, cur, VNOVAL); 1106996c772fSJohn Dyson } 1107b40ce416SJulian Elischer union_unlock_upper(uppervp, td); 1108df8bae1dSRodney W. Grimes return (error); 1109df8bae1dSRodney W. Grimes } 1110df8bae1dSRodney W. Grimes 1111c9bf0111SKATO Takenori static int 1112996c772fSJohn Dyson union_lease(ap) 1113996c772fSJohn Dyson struct vop_lease_args /* { 1114996c772fSJohn Dyson struct vnode *a_vp; 1115b40ce416SJulian Elischer struct thread *a_td; 1116996c772fSJohn Dyson struct ucred *a_cred; 1117996c772fSJohn Dyson int a_flag; 1118996c772fSJohn Dyson } */ *ap; 1119996c772fSJohn Dyson { 11202a31267eSMatthew Dillon struct vnode *ovp = OTHERVP(ap->a_vp); 1121996c772fSJohn Dyson 1122996c772fSJohn Dyson ap->a_vp = ovp; 1123996c772fSJohn Dyson return (VCALL(ovp, VOFFSET(vop_lease), ap)); 1124996c772fSJohn Dyson } 1125996c772fSJohn Dyson 1126c9bf0111SKATO Takenori static int 1127df8bae1dSRodney W. Grimes union_ioctl(ap) 1128df8bae1dSRodney W. Grimes struct vop_ioctl_args /* { 1129df8bae1dSRodney W. Grimes struct vnode *a_vp; 1130bc9d8a9aSPoul-Henning Kamp u_long a_command; 1131df8bae1dSRodney W. Grimes caddr_t a_data; 1132df8bae1dSRodney W. Grimes int a_fflag; 1133df8bae1dSRodney W. Grimes struct ucred *a_cred; 1134b40ce416SJulian Elischer struct thread *a_td; 1135df8bae1dSRodney W. Grimes } */ *ap; 1136df8bae1dSRodney W. Grimes { 11372a31267eSMatthew Dillon struct vnode *ovp = OTHERVP(ap->a_vp); 1138df8bae1dSRodney W. Grimes 1139996c772fSJohn Dyson ap->a_vp = ovp; 1140996c772fSJohn Dyson return (VCALL(ovp, VOFFSET(vop_ioctl), ap)); 1141df8bae1dSRodney W. Grimes } 1142df8bae1dSRodney W. Grimes 1143c9bf0111SKATO Takenori static int 1144a6aeade2SPeter Wemm union_poll(ap) 1145a6aeade2SPeter Wemm struct vop_poll_args /* { 1146df8bae1dSRodney W. Grimes struct vnode *a_vp; 1147a6aeade2SPeter Wemm int a_events; 1148df8bae1dSRodney W. Grimes struct ucred *a_cred; 1149b40ce416SJulian Elischer struct thread *a_td; 1150df8bae1dSRodney W. Grimes } */ *ap; 1151df8bae1dSRodney W. Grimes { 11522a31267eSMatthew Dillon struct vnode *ovp = OTHERVP(ap->a_vp); 1153df8bae1dSRodney W. Grimes 1154996c772fSJohn Dyson ap->a_vp = ovp; 1155a6aeade2SPeter Wemm return (VCALL(ovp, VOFFSET(vop_poll), ap)); 1156996c772fSJohn Dyson } 1157996c772fSJohn Dyson 1158c9bf0111SKATO Takenori static int 1159996c772fSJohn Dyson union_revoke(ap) 1160996c772fSJohn Dyson struct vop_revoke_args /* { 1161996c772fSJohn Dyson struct vnode *a_vp; 1162996c772fSJohn Dyson int a_flags; 1163b40ce416SJulian Elischer struct thread *a_td; 1164996c772fSJohn Dyson } */ *ap; 1165996c772fSJohn Dyson { 1166996c772fSJohn Dyson struct vnode *vp = ap->a_vp; 1167996c772fSJohn Dyson 1168996c772fSJohn Dyson if (UPPERVP(vp)) 1169996c772fSJohn Dyson VOP_REVOKE(UPPERVP(vp), ap->a_flags); 1170996c772fSJohn Dyson if (LOWERVP(vp)) 1171996c772fSJohn Dyson VOP_REVOKE(LOWERVP(vp), ap->a_flags); 1172996c772fSJohn Dyson vgone(vp); 1173996c772fSJohn Dyson return (0); 1174df8bae1dSRodney W. Grimes } 1175df8bae1dSRodney W. Grimes 1176c9bf0111SKATO Takenori static int 1177df8bae1dSRodney W. Grimes union_fsync(ap) 1178df8bae1dSRodney W. Grimes struct vop_fsync_args /* { 1179df8bae1dSRodney W. Grimes struct vnode *a_vp; 1180df8bae1dSRodney W. Grimes struct ucred *a_cred; 1181df8bae1dSRodney W. Grimes int a_waitfor; 1182b40ce416SJulian Elischer struct thread *a_td; 1183df8bae1dSRodney W. Grimes } */ *ap; 1184df8bae1dSRodney W. Grimes { 1185df8bae1dSRodney W. Grimes int error = 0; 1186b40ce416SJulian Elischer struct thread *td = ap->a_td; 11872a31267eSMatthew Dillon struct vnode *targetvp; 11882a31267eSMatthew Dillon struct union_node *un = VTOUNION(ap->a_vp); 1189df8bae1dSRodney W. Grimes 1190b40ce416SJulian Elischer if ((targetvp = union_lock_other(un, td)) != NULLVP) { 1191b40ce416SJulian Elischer error = VOP_FSYNC(targetvp, ap->a_cred, ap->a_waitfor, td); 1192b40ce416SJulian Elischer union_unlock_other(targetvp, td); 1193df8bae1dSRodney W. Grimes } 1194df8bae1dSRodney W. Grimes 1195df8bae1dSRodney W. Grimes return (error); 1196df8bae1dSRodney W. Grimes } 1197df8bae1dSRodney W. Grimes 11982a31267eSMatthew Dillon /* 11992a31267eSMatthew Dillon * union_remove: 12002a31267eSMatthew Dillon * 12012a31267eSMatthew Dillon * Remove the specified cnp. The dvp and vp are passed to us locked 12022a31267eSMatthew Dillon * and must remain locked on return. 12032a31267eSMatthew Dillon */ 12042a31267eSMatthew Dillon 1205c9bf0111SKATO Takenori static int 1206df8bae1dSRodney W. Grimes union_remove(ap) 1207df8bae1dSRodney W. Grimes struct vop_remove_args /* { 1208df8bae1dSRodney W. Grimes struct vnode *a_dvp; 1209df8bae1dSRodney W. Grimes struct vnode *a_vp; 1210df8bae1dSRodney W. Grimes struct componentname *a_cnp; 1211df8bae1dSRodney W. Grimes } */ *ap; 1212df8bae1dSRodney W. Grimes { 1213df8bae1dSRodney W. Grimes struct union_node *dun = VTOUNION(ap->a_dvp); 1214df8bae1dSRodney W. Grimes struct union_node *un = VTOUNION(ap->a_vp); 1215996c772fSJohn Dyson struct componentname *cnp = ap->a_cnp; 1216b40ce416SJulian Elischer struct thread *td = cnp->cn_thread; 12172a31267eSMatthew Dillon struct vnode *uppervp; 12182a31267eSMatthew Dillon struct vnode *upperdvp; 12197be2d300SMike Smith int error; 1220df8bae1dSRodney W. Grimes 1221b40ce416SJulian Elischer if ((upperdvp = union_lock_upper(dun, td)) == NULLVP) 1222996c772fSJohn Dyson panic("union remove: null upper vnode"); 1223996c772fSJohn Dyson 1224b40ce416SJulian Elischer if ((uppervp = union_lock_upper(un, td)) != NULLVP) { 1225b40ce416SJulian Elischer if (union_dowhiteout(un, cnp->cn_cred, td)) 1226996c772fSJohn Dyson cnp->cn_flags |= DOWHITEOUT; 12272a31267eSMatthew Dillon error = VOP_REMOVE(upperdvp, uppervp, cnp); 1228df8bae1dSRodney W. Grimes if (!error) 1229df8bae1dSRodney W. Grimes union_removed_upper(un); 1230b40ce416SJulian Elischer union_unlock_upper(uppervp, td); 1231df8bae1dSRodney W. Grimes } else { 1232996c772fSJohn Dyson error = union_mkwhiteout( 12332a31267eSMatthew Dillon MOUNTTOUNIONMOUNT(ap->a_dvp->v_mount), 12342a31267eSMatthew Dillon upperdvp, ap->a_cnp, un->un_path); 1235df8bae1dSRodney W. Grimes } 1236b40ce416SJulian Elischer union_unlock_upper(upperdvp, td); 1237df8bae1dSRodney W. Grimes return (error); 1238df8bae1dSRodney W. Grimes } 1239df8bae1dSRodney W. Grimes 12402a31267eSMatthew Dillon /* 12412a31267eSMatthew Dillon * union_link: 12422a31267eSMatthew Dillon * 1243fa288043SDon Lewis * tdvp and vp will be locked on entry. 1244fa288043SDon Lewis * tdvp and vp should remain locked on return. 12452a31267eSMatthew Dillon */ 12462a31267eSMatthew Dillon 1247c9bf0111SKATO Takenori static int 1248df8bae1dSRodney W. Grimes union_link(ap) 1249df8bae1dSRodney W. Grimes struct vop_link_args /* { 1250df8bae1dSRodney W. Grimes struct vnode *a_tdvp; 125147777413SDavid Greenman struct vnode *a_vp; 1252df8bae1dSRodney W. Grimes struct componentname *a_cnp; 1253df8bae1dSRodney W. Grimes } */ *ap; 1254df8bae1dSRodney W. Grimes { 1255996c772fSJohn Dyson struct componentname *cnp = ap->a_cnp; 1256b40ce416SJulian Elischer struct thread *td = cnp->cn_thread; 12577be2d300SMike Smith struct union_node *dun = VTOUNION(ap->a_tdvp); 1258996c772fSJohn Dyson struct vnode *vp; 1259996c772fSJohn Dyson struct vnode *tdvp; 12607be2d300SMike Smith int error = 0; 1261df8bae1dSRodney W. Grimes 1262996c772fSJohn Dyson if (ap->a_tdvp->v_op != ap->a_vp->v_op) { 1263996c772fSJohn Dyson vp = ap->a_vp; 1264df8bae1dSRodney W. Grimes } else { 1265996c772fSJohn Dyson struct union_node *tun = VTOUNION(ap->a_vp); 12662a31267eSMatthew Dillon 1267996c772fSJohn Dyson if (tun->un_uppervp == NULLVP) { 12682a31267eSMatthew Dillon #if 0 12697be2d300SMike Smith if (dun->un_uppervp == tun->un_dirvp) { 12702a31267eSMatthew Dillon if (dun->un_flags & UN_ULOCK) { 12717be2d300SMike Smith dun->un_flags &= ~UN_ULOCK; 1272b40ce416SJulian Elischer VOP_UNLOCK(dun->un_uppervp, 0, td); 1273996c772fSJohn Dyson } 12742a31267eSMatthew Dillon } 12752a31267eSMatthew Dillon #endif 1276b40ce416SJulian Elischer error = union_copyup(tun, 1, cnp->cn_cred, td); 12772a31267eSMatthew Dillon #if 0 12787be2d300SMike Smith if (dun->un_uppervp == tun->un_dirvp) { 12797be2d300SMike Smith vn_lock(dun->un_uppervp, 1280b40ce416SJulian Elischer LK_EXCLUSIVE | LK_RETRY, td); 12817be2d300SMike Smith dun->un_flags |= UN_ULOCK; 1282996c772fSJohn Dyson } 12832a31267eSMatthew Dillon #endif 12847be2d300SMike Smith if (error) 1285df8bae1dSRodney W. Grimes return (error); 1286fa288043SDon Lewis } 1287fa288043SDon Lewis vp = tun->un_uppervp; 1288fa288043SDon Lewis vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); 1289fa288043SDon Lewis } 1290df8bae1dSRodney W. Grimes 12912a31267eSMatthew Dillon /* 12922a31267eSMatthew Dillon * Make sure upper is locked, then unlock the union directory we were 12931c4ccf09SDon Lewis * called with to avoid a deadlock while we are calling VOP_LINK() on 12942a31267eSMatthew Dillon * the upper (with tdvp locked and vp not locked). Our ap->a_tdvp 12952a31267eSMatthew Dillon * is expected to be locked on return. 12962a31267eSMatthew Dillon */ 1297996c772fSJohn Dyson 1298b40ce416SJulian Elischer if ((tdvp = union_lock_upper(dun, td)) == NULLVP) 12992a31267eSMatthew Dillon return (EROFS); 13007be2d300SMike Smith 1301b40ce416SJulian Elischer VOP_UNLOCK(ap->a_tdvp, 0, td); /* unlock calling node */ 13022a31267eSMatthew Dillon error = VOP_LINK(tdvp, vp, cnp); /* call link on upper */ 13037be2d300SMike Smith 13042a31267eSMatthew Dillon /* 1305fa288043SDon Lewis * Unlock tun->un_uppervp if we locked it above. 13062a31267eSMatthew Dillon */ 1307fa288043SDon Lewis if (ap->a_tdvp->v_op == ap->a_vp->v_op) 1308fa288043SDon Lewis VOP_UNLOCK(vp, 0, td); 1309fa288043SDon Lewis /* 1310fa288043SDon Lewis * We have to unlock tdvp prior to relocking our calling node in 1311fa288043SDon Lewis * order to avoid a deadlock. We also have to unlock ap->a_vp 1312fa288043SDon Lewis * before relocking the directory, but then we have to relock 1313fa288043SDon Lewis * ap->a_vp as our caller expects. 1314fa288043SDon Lewis */ 1315fa288043SDon Lewis VOP_UNLOCK(ap->a_vp, 0, td); 1316b40ce416SJulian Elischer union_unlock_upper(tdvp, td); 1317b40ce416SJulian Elischer vn_lock(ap->a_tdvp, LK_EXCLUSIVE | LK_RETRY, td); 1318fa288043SDon Lewis vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY, td); 13197be2d300SMike Smith return (error); 1320996c772fSJohn Dyson } 1321996c772fSJohn Dyson 1322c9bf0111SKATO Takenori static int 1323df8bae1dSRodney W. Grimes union_rename(ap) 1324df8bae1dSRodney W. Grimes struct vop_rename_args /* { 1325df8bae1dSRodney W. Grimes struct vnode *a_fdvp; 1326df8bae1dSRodney W. Grimes struct vnode *a_fvp; 1327df8bae1dSRodney W. Grimes struct componentname *a_fcnp; 1328df8bae1dSRodney W. Grimes struct vnode *a_tdvp; 1329df8bae1dSRodney W. Grimes struct vnode *a_tvp; 1330df8bae1dSRodney W. Grimes struct componentname *a_tcnp; 1331df8bae1dSRodney W. Grimes } */ *ap; 1332df8bae1dSRodney W. Grimes { 1333df8bae1dSRodney W. Grimes int error; 1334df8bae1dSRodney W. Grimes struct vnode *fdvp = ap->a_fdvp; 1335df8bae1dSRodney W. Grimes struct vnode *fvp = ap->a_fvp; 1336df8bae1dSRodney W. Grimes struct vnode *tdvp = ap->a_tdvp; 1337df8bae1dSRodney W. Grimes struct vnode *tvp = ap->a_tvp; 1338df8bae1dSRodney W. Grimes 13392a31267eSMatthew Dillon /* 13402a31267eSMatthew Dillon * Figure out what fdvp to pass to our upper or lower vnode. If we 13412a31267eSMatthew Dillon * replace the fdvp, release the original one and ref the new one. 13422a31267eSMatthew Dillon */ 13432a31267eSMatthew Dillon 1344df8bae1dSRodney W. Grimes if (fdvp->v_op == union_vnodeop_p) { /* always true */ 1345df8bae1dSRodney W. Grimes struct union_node *un = VTOUNION(fdvp); 1346df8bae1dSRodney W. Grimes if (un->un_uppervp == NULLVP) { 1347996c772fSJohn Dyson /* 1348996c772fSJohn Dyson * this should never happen in normal 1349996c772fSJohn Dyson * operation but might if there was 1350996c772fSJohn Dyson * a problem creating the top-level shadow 1351996c772fSJohn Dyson * directory. 1352996c772fSJohn Dyson */ 1353996c772fSJohn Dyson error = EXDEV; 1354df8bae1dSRodney W. Grimes goto bad; 1355df8bae1dSRodney W. Grimes } 1356df8bae1dSRodney W. Grimes fdvp = un->un_uppervp; 1357df8bae1dSRodney W. Grimes VREF(fdvp); 1358df8bae1dSRodney W. Grimes vrele(ap->a_fdvp); 1359df8bae1dSRodney W. Grimes } 1360df8bae1dSRodney W. Grimes 13612a31267eSMatthew Dillon /* 13622a31267eSMatthew Dillon * Figure out what fvp to pass to our upper or lower vnode. If we 13632a31267eSMatthew Dillon * replace the fvp, release the original one and ref the new one. 13642a31267eSMatthew Dillon */ 13652a31267eSMatthew Dillon 1366df8bae1dSRodney W. Grimes if (fvp->v_op == union_vnodeop_p) { /* always true */ 1367df8bae1dSRodney W. Grimes struct union_node *un = VTOUNION(fvp); 13682a31267eSMatthew Dillon #if 0 13692a31267eSMatthew Dillon struct union_mount *um = MOUNTTOUNIONMOUNT(fvp->v_mount); 13702a31267eSMatthew Dillon #endif 13712a31267eSMatthew Dillon 1372df8bae1dSRodney W. Grimes if (un->un_uppervp == NULLVP) { 13732a31267eSMatthew Dillon switch(fvp->v_type) { 13742a31267eSMatthew Dillon case VREG: 1375b40ce416SJulian Elischer vn_lock(un->un_vnode, LK_EXCLUSIVE | LK_RETRY, ap->a_fcnp->cn_thread); 1376b40ce416SJulian Elischer error = union_copyup(un, 1, ap->a_fcnp->cn_cred, ap->a_fcnp->cn_thread); 1377b40ce416SJulian Elischer VOP_UNLOCK(un->un_vnode, 0, ap->a_fcnp->cn_thread); 13782a31267eSMatthew Dillon if (error) 13792a31267eSMatthew Dillon goto bad; 13802a31267eSMatthew Dillon break; 13812a31267eSMatthew Dillon case VDIR: 13822a31267eSMatthew Dillon /* 13832a31267eSMatthew Dillon * XXX not yet. 13842a31267eSMatthew Dillon * 13852a31267eSMatthew Dillon * There is only one way to rename a directory 13862a31267eSMatthew Dillon * based in the lowervp, and that is to copy 13872a31267eSMatthew Dillon * the entire directory hierarchy. Otherwise 13882a31267eSMatthew Dillon * it would not last across a reboot. 13892a31267eSMatthew Dillon */ 13902a31267eSMatthew Dillon #if 0 13912a31267eSMatthew Dillon vrele(fvp); 13922a31267eSMatthew Dillon fvp = NULL; 1393b40ce416SJulian Elischer vn_lock(fdvp, LK_EXCLUSIVE | LK_RETRY, ap->a_fcnp->cn_thread); 13942a31267eSMatthew Dillon error = union_mkshadow(um, fdvp, 13952a31267eSMatthew Dillon ap->a_fcnp, &un->un_uppervp); 1396b40ce416SJulian Elischer VOP_UNLOCK(fdvp, 0, ap->a_fcnp->cn_thread); 13972a31267eSMatthew Dillon if (un->un_uppervp) 1398b40ce416SJulian Elischer VOP_UNLOCK(un->un_uppervp, 0, ap->a_fcnp->cn_thread); 13992a31267eSMatthew Dillon if (error) 14002a31267eSMatthew Dillon goto bad; 14012a31267eSMatthew Dillon break; 14022a31267eSMatthew Dillon #endif 14032a31267eSMatthew Dillon default: 1404996c772fSJohn Dyson error = EXDEV; 1405df8bae1dSRodney W. Grimes goto bad; 1406df8bae1dSRodney W. Grimes } 14072a31267eSMatthew Dillon } 1408df8bae1dSRodney W. Grimes 1409996c772fSJohn Dyson if (un->un_lowervp != NULLVP) 1410996c772fSJohn Dyson ap->a_fcnp->cn_flags |= DOWHITEOUT; 1411df8bae1dSRodney W. Grimes fvp = un->un_uppervp; 1412df8bae1dSRodney W. Grimes VREF(fvp); 1413df8bae1dSRodney W. Grimes vrele(ap->a_fvp); 1414df8bae1dSRodney W. Grimes } 1415df8bae1dSRodney W. Grimes 14162a31267eSMatthew Dillon /* 14172a31267eSMatthew Dillon * Figure out what tdvp (destination directory) to pass to the 14182a31267eSMatthew Dillon * lower level. If we replace it with uppervp, we need to vput the 14192a31267eSMatthew Dillon * old one. The exclusive lock is transfered to what we will pass 14201c4ccf09SDon Lewis * down in the VOP_RENAME() and we replace uppervp with a simple 14212a31267eSMatthew Dillon * reference. 14222a31267eSMatthew Dillon */ 14232a31267eSMatthew Dillon 1424df8bae1dSRodney W. Grimes if (tdvp->v_op == union_vnodeop_p) { 1425df8bae1dSRodney W. Grimes struct union_node *un = VTOUNION(tdvp); 14262a31267eSMatthew Dillon 1427df8bae1dSRodney W. Grimes if (un->un_uppervp == NULLVP) { 1428996c772fSJohn Dyson /* 14291c4ccf09SDon Lewis * This should never happen in normal 1430996c772fSJohn Dyson * operation but might if there was 1431996c772fSJohn Dyson * a problem creating the top-level shadow 1432996c772fSJohn Dyson * directory. 1433996c772fSJohn Dyson */ 1434996c772fSJohn Dyson error = EXDEV; 1435df8bae1dSRodney W. Grimes goto bad; 1436df8bae1dSRodney W. Grimes } 1437df8bae1dSRodney W. Grimes 14382a31267eSMatthew Dillon /* 14391c4ccf09SDon Lewis * New tdvp is a lock and reference on uppervp. 14401c4ccf09SDon Lewis * Put away the old tdvp. 14412a31267eSMatthew Dillon */ 1442b40ce416SJulian Elischer tdvp = union_lock_upper(un, ap->a_tcnp->cn_thread); 1443df8bae1dSRodney W. Grimes vput(ap->a_tdvp); 1444df8bae1dSRodney W. Grimes } 1445df8bae1dSRodney W. Grimes 14462a31267eSMatthew Dillon /* 14472a31267eSMatthew Dillon * Figure out what tvp (destination file) to pass to the 14482a31267eSMatthew Dillon * lower level. 14492a31267eSMatthew Dillon * 14501c4ccf09SDon Lewis * If the uppervp file does not exist, put away the (wrong) 14512a31267eSMatthew Dillon * file and change tvp to NULL. 14522a31267eSMatthew Dillon */ 14532a31267eSMatthew Dillon 1454996c772fSJohn Dyson if (tvp != NULLVP && tvp->v_op == union_vnodeop_p) { 1455df8bae1dSRodney W. Grimes struct union_node *un = VTOUNION(tvp); 1456df8bae1dSRodney W. Grimes 1457b40ce416SJulian Elischer tvp = union_lock_upper(un, ap->a_tcnp->cn_thread); 1458df8bae1dSRodney W. Grimes vput(ap->a_tvp); 14592a31267eSMatthew Dillon /* note: tvp may be NULL */ 1460df8bae1dSRodney W. Grimes } 1461df8bae1dSRodney W. Grimes 14622a31267eSMatthew Dillon /* 14631c4ccf09SDon Lewis * VOP_RENAME() releases/vputs prior to returning, so we have no 14642a31267eSMatthew Dillon * cleanup to do. 14652a31267eSMatthew Dillon */ 14662a31267eSMatthew Dillon 1467df8bae1dSRodney W. Grimes return (VOP_RENAME(fdvp, fvp, ap->a_fcnp, tdvp, tvp, ap->a_tcnp)); 1468df8bae1dSRodney W. Grimes 14692a31267eSMatthew Dillon /* 14702a31267eSMatthew Dillon * Error. We still have to release / vput the various elements. 14712a31267eSMatthew Dillon */ 14722a31267eSMatthew Dillon 1473df8bae1dSRodney W. Grimes bad: 1474df8bae1dSRodney W. Grimes vrele(fdvp); 14752a31267eSMatthew Dillon if (fvp) 1476df8bae1dSRodney W. Grimes vrele(fvp); 1477df8bae1dSRodney W. Grimes vput(tdvp); 14782a31267eSMatthew Dillon if (tvp != NULLVP) { 14792a31267eSMatthew Dillon if (tvp != tdvp) 1480df8bae1dSRodney W. Grimes vput(tvp); 14812a31267eSMatthew Dillon else 14822a31267eSMatthew Dillon vrele(tvp); 14832a31267eSMatthew Dillon } 1484df8bae1dSRodney W. Grimes return (error); 1485df8bae1dSRodney W. Grimes } 1486df8bae1dSRodney W. Grimes 1487c9bf0111SKATO Takenori static int 1488df8bae1dSRodney W. Grimes union_mkdir(ap) 1489df8bae1dSRodney W. Grimes struct vop_mkdir_args /* { 1490df8bae1dSRodney W. Grimes struct vnode *a_dvp; 1491df8bae1dSRodney W. Grimes struct vnode **a_vpp; 1492df8bae1dSRodney W. Grimes struct componentname *a_cnp; 1493df8bae1dSRodney W. Grimes struct vattr *a_vap; 1494df8bae1dSRodney W. Grimes } */ *ap; 1495df8bae1dSRodney W. Grimes { 14967be2d300SMike Smith struct union_node *dun = VTOUNION(ap->a_dvp); 1497996c772fSJohn Dyson struct componentname *cnp = ap->a_cnp; 1498b40ce416SJulian Elischer struct thread *td = cnp->cn_thread; 14992a31267eSMatthew Dillon struct vnode *upperdvp; 15002a31267eSMatthew Dillon int error = EROFS; 1501df8bae1dSRodney W. Grimes 1502b40ce416SJulian Elischer if ((upperdvp = union_lock_upper(dun, td)) != NULLVP) { 1503df8bae1dSRodney W. Grimes struct vnode *vp; 1504df8bae1dSRodney W. Grimes 15052a31267eSMatthew Dillon error = VOP_MKDIR(upperdvp, &vp, cnp, ap->a_vap); 1506b40ce416SJulian Elischer union_unlock_upper(upperdvp, td); 15072a31267eSMatthew Dillon 15082a31267eSMatthew Dillon if (error == 0) { 1509b40ce416SJulian Elischer VOP_UNLOCK(vp, 0, td); 15104d93c0beSJeff Roberson UDEBUG(("ALLOCVP-2 FROM %p REFS %d\n", vp, vrefcnt(vp))); 15112a31267eSMatthew Dillon error = union_allocvp(ap->a_vpp, ap->a_dvp->v_mount, 15122a31267eSMatthew Dillon ap->a_dvp, NULLVP, cnp, vp, NULLVP, 1); 15134d93c0beSJeff Roberson UDEBUG(("ALLOCVP-2B FROM %p REFS %d\n", *ap->a_vpp, vrefcnt(vp))); 1514996c772fSJohn Dyson } 1515df8bae1dSRodney W. Grimes } 15162a31267eSMatthew Dillon return (error); 1517df8bae1dSRodney W. Grimes } 1518df8bae1dSRodney W. Grimes 1519c9bf0111SKATO Takenori static int 1520df8bae1dSRodney W. Grimes union_rmdir(ap) 1521df8bae1dSRodney W. Grimes struct vop_rmdir_args /* { 1522df8bae1dSRodney W. Grimes struct vnode *a_dvp; 1523df8bae1dSRodney W. Grimes struct vnode *a_vp; 1524df8bae1dSRodney W. Grimes struct componentname *a_cnp; 1525df8bae1dSRodney W. Grimes } */ *ap; 1526df8bae1dSRodney W. Grimes { 1527df8bae1dSRodney W. Grimes struct union_node *dun = VTOUNION(ap->a_dvp); 1528df8bae1dSRodney W. Grimes struct union_node *un = VTOUNION(ap->a_vp); 1529996c772fSJohn Dyson struct componentname *cnp = ap->a_cnp; 1530b40ce416SJulian Elischer struct thread *td = cnp->cn_thread; 15312a31267eSMatthew Dillon struct vnode *upperdvp; 15322a31267eSMatthew Dillon struct vnode *uppervp; 15337be2d300SMike Smith int error; 1534df8bae1dSRodney W. Grimes 1535b40ce416SJulian Elischer if ((upperdvp = union_lock_upper(dun, td)) == NULLVP) 1536996c772fSJohn Dyson panic("union rmdir: null upper vnode"); 1537996c772fSJohn Dyson 1538b40ce416SJulian Elischer if ((uppervp = union_lock_upper(un, td)) != NULLVP) { 1539b40ce416SJulian Elischer if (union_dowhiteout(un, cnp->cn_cred, td)) 1540996c772fSJohn Dyson cnp->cn_flags |= DOWHITEOUT; 15412a31267eSMatthew Dillon error = VOP_RMDIR(upperdvp, uppervp, ap->a_cnp); 154201634480SBrian Feldman if (!error) 154301634480SBrian Feldman union_removed_upper(un); 1544b40ce416SJulian Elischer union_unlock_upper(uppervp, td); 1545df8bae1dSRodney W. Grimes } else { 1546996c772fSJohn Dyson error = union_mkwhiteout( 15472a31267eSMatthew Dillon MOUNTTOUNIONMOUNT(ap->a_dvp->v_mount), 1548996c772fSJohn Dyson dun->un_uppervp, ap->a_cnp, un->un_path); 1549df8bae1dSRodney W. Grimes } 1550b40ce416SJulian Elischer union_unlock_upper(upperdvp, td); 1551df8bae1dSRodney W. Grimes return (error); 1552df8bae1dSRodney W. Grimes } 1553df8bae1dSRodney W. Grimes 15542a31267eSMatthew Dillon /* 15552a31267eSMatthew Dillon * union_symlink: 15562a31267eSMatthew Dillon * 15572a31267eSMatthew Dillon * dvp is locked on entry and remains locked on return. a_vpp is garbage 15582a31267eSMatthew Dillon * (unused). 15592a31267eSMatthew Dillon */ 15602a31267eSMatthew Dillon 1561c9bf0111SKATO Takenori static int 1562df8bae1dSRodney W. Grimes union_symlink(ap) 1563df8bae1dSRodney W. Grimes struct vop_symlink_args /* { 1564df8bae1dSRodney W. Grimes struct vnode *a_dvp; 1565df8bae1dSRodney W. Grimes struct vnode **a_vpp; 1566df8bae1dSRodney W. Grimes struct componentname *a_cnp; 1567df8bae1dSRodney W. Grimes struct vattr *a_vap; 1568df8bae1dSRodney W. Grimes char *a_target; 1569df8bae1dSRodney W. Grimes } */ *ap; 1570df8bae1dSRodney W. Grimes { 15717be2d300SMike Smith struct union_node *dun = VTOUNION(ap->a_dvp); 1572996c772fSJohn Dyson struct componentname *cnp = ap->a_cnp; 1573b40ce416SJulian Elischer struct thread *td = cnp->cn_thread; 15742a31267eSMatthew Dillon struct vnode *dvp; 15752a31267eSMatthew Dillon int error = EROFS; 1576df8bae1dSRodney W. Grimes 1577b40ce416SJulian Elischer if ((dvp = union_lock_upper(dun, td)) != NULLVP) { 1578dd8c04f4SEivind Eklund error = VOP_SYMLINK(dvp, ap->a_vpp, cnp, ap->a_vap, 1579dd8c04f4SEivind Eklund ap->a_target); 1580b40ce416SJulian Elischer union_unlock_upper(dvp, td); 1581df8bae1dSRodney W. Grimes } 15822a31267eSMatthew Dillon return (error); 1583df8bae1dSRodney W. Grimes } 1584df8bae1dSRodney W. Grimes 1585df8bae1dSRodney W. Grimes /* 15861c4ccf09SDon Lewis * union_readdir ()works in concert with getdirentries() and 1587df8bae1dSRodney W. Grimes * readdir(3) to provide a list of entries in the unioned 15881c4ccf09SDon Lewis * directories. getdirentries() is responsible for walking 1589df8bae1dSRodney W. Grimes * down the union stack. readdir(3) is responsible for 1590df8bae1dSRodney W. Grimes * eliminating duplicate names from the returned data stream. 1591df8bae1dSRodney W. Grimes */ 1592c9bf0111SKATO Takenori static int 1593df8bae1dSRodney W. Grimes union_readdir(ap) 1594df8bae1dSRodney W. Grimes struct vop_readdir_args /* { 1595df8bae1dSRodney W. Grimes struct vnode *a_vp; 1596df8bae1dSRodney W. Grimes struct uio *a_uio; 1597df8bae1dSRodney W. Grimes struct ucred *a_cred; 1598996c772fSJohn Dyson int *a_eofflag; 1599996c772fSJohn Dyson u_long *a_cookies; 1600996c772fSJohn Dyson int a_ncookies; 1601df8bae1dSRodney W. Grimes } */ *ap; 1602df8bae1dSRodney W. Grimes { 1603df8bae1dSRodney W. Grimes struct union_node *un = VTOUNION(ap->a_vp); 1604b40ce416SJulian Elischer struct thread *td = ap->a_uio->uio_td; 16052a31267eSMatthew Dillon struct vnode *uvp; 16062a31267eSMatthew Dillon int error = 0; 1607df8bae1dSRodney W. Grimes 1608b40ce416SJulian Elischer if ((uvp = union_lock_upper(un, td)) != NULLVP) { 1609996c772fSJohn Dyson ap->a_vp = uvp; 16102a31267eSMatthew Dillon error = VCALL(uvp, VOFFSET(vop_readdir), ap); 1611b40ce416SJulian Elischer union_unlock_upper(uvp, td); 16122a31267eSMatthew Dillon } 16132a31267eSMatthew Dillon return(error); 1614df8bae1dSRodney W. Grimes } 1615df8bae1dSRodney W. Grimes 1616c9bf0111SKATO Takenori static int 1617df8bae1dSRodney W. Grimes union_readlink(ap) 1618df8bae1dSRodney W. Grimes struct vop_readlink_args /* { 1619df8bae1dSRodney W. Grimes struct vnode *a_vp; 1620df8bae1dSRodney W. Grimes struct uio *a_uio; 1621df8bae1dSRodney W. Grimes struct ucred *a_cred; 1622df8bae1dSRodney W. Grimes } */ *ap; 1623df8bae1dSRodney W. Grimes { 1624df8bae1dSRodney W. Grimes int error; 16252a31267eSMatthew Dillon struct union_node *un = VTOUNION(ap->a_vp); 1626996c772fSJohn Dyson struct uio *uio = ap->a_uio; 1627b40ce416SJulian Elischer struct thread *td = uio->uio_td; 16282a31267eSMatthew Dillon struct vnode *vp; 1629df8bae1dSRodney W. Grimes 1630b40ce416SJulian Elischer vp = union_lock_other(un, td); 16312a31267eSMatthew Dillon KASSERT(vp != NULL, ("union_readlink: backing vnode missing!")); 16322a31267eSMatthew Dillon 1633996c772fSJohn Dyson ap->a_vp = vp; 1634996c772fSJohn Dyson error = VCALL(vp, VOFFSET(vop_readlink), ap); 1635b40ce416SJulian Elischer union_unlock_other(vp, td); 1636df8bae1dSRodney W. Grimes 1637df8bae1dSRodney W. Grimes return (error); 1638df8bae1dSRodney W. Grimes } 1639df8bae1dSRodney W. Grimes 1640f2a2857bSKirk McKusick static int 1641f2a2857bSKirk McKusick union_getwritemount(ap) 1642f2a2857bSKirk McKusick struct vop_getwritemount_args /* { 1643f2a2857bSKirk McKusick struct vnode *a_vp; 1644f2a2857bSKirk McKusick struct mount **a_mpp; 1645f2a2857bSKirk McKusick } */ *ap; 1646f2a2857bSKirk McKusick { 1647f3d1ec67SBoris Popov struct vnode *vp = ap->a_vp; 1648f3d1ec67SBoris Popov struct vnode *uvp = UPPERVP(vp); 1649f2a2857bSKirk McKusick 1650f3d1ec67SBoris Popov if (uvp == NULL) { 1651f3d1ec67SBoris Popov VI_LOCK(vp); 1652e6e370a7SJeff Roberson if (vp->v_iflag & VI_FREE) { 1653f3d1ec67SBoris Popov VI_UNLOCK(vp); 1654f3d1ec67SBoris Popov return (EOPNOTSUPP); 1655f3d1ec67SBoris Popov } 1656f3d1ec67SBoris Popov VI_UNLOCK(vp); 165710fa1684SBoris Popov return (EACCES); 1658f3d1ec67SBoris Popov } 1659f3d1ec67SBoris Popov return(VOP_GETWRITEMOUNT(uvp, ap->a_mpp)); 1660f2a2857bSKirk McKusick } 1661f2a2857bSKirk McKusick 16622a31267eSMatthew Dillon /* 16632a31267eSMatthew Dillon * union_inactive: 16642a31267eSMatthew Dillon * 16652a31267eSMatthew Dillon * Called with the vnode locked. We are expected to unlock the vnode. 16662a31267eSMatthew Dillon */ 16672a31267eSMatthew Dillon 1668c9bf0111SKATO Takenori static int 1669df8bae1dSRodney W. Grimes union_inactive(ap) 1670df8bae1dSRodney W. Grimes struct vop_inactive_args /* { 1671df8bae1dSRodney W. Grimes struct vnode *a_vp; 1672b40ce416SJulian Elischer struct thread *a_td; 1673df8bae1dSRodney W. Grimes } */ *ap; 1674df8bae1dSRodney W. Grimes { 1675996c772fSJohn Dyson struct vnode *vp = ap->a_vp; 1676b40ce416SJulian Elischer struct thread *td = ap->a_td; 1677996c772fSJohn Dyson struct union_node *un = VTOUNION(vp); 1678df8bae1dSRodney W. Grimes 1679df8bae1dSRodney W. Grimes /* 1680df8bae1dSRodney W. Grimes * Do nothing (and _don't_ bypass). 1681df8bae1dSRodney W. Grimes * Wait to vrele lowervp until reclaim, 1682df8bae1dSRodney W. Grimes * so that until then our union_node is in the 1683df8bae1dSRodney W. Grimes * cache and reusable. 1684df8bae1dSRodney W. Grimes * 1685df8bae1dSRodney W. Grimes */ 1686df8bae1dSRodney W. Grimes 1687d8c6e674SDavid Schultz if (un->un_dircache != NULL) 1688d8c6e674SDavid Schultz union_dircache_free(un); 1689df8bae1dSRodney W. Grimes 16902a31267eSMatthew Dillon #if 0 16912a31267eSMatthew Dillon if ((un->un_flags & UN_ULOCK) && un->un_uppervp) { 16922a31267eSMatthew Dillon un->un_flags &= ~UN_ULOCK; 1693b40ce416SJulian Elischer VOP_UNLOCK(un->un_uppervp, 0, td); 16942a31267eSMatthew Dillon } 16952a31267eSMatthew Dillon #endif 16962a31267eSMatthew Dillon 1697b40ce416SJulian Elischer VOP_UNLOCK(vp, 0, td); 1698996c772fSJohn Dyson 1699996c772fSJohn Dyson if ((un->un_flags & UN_CACHED) == 0) 1700996c772fSJohn Dyson vgone(vp); 1701df8bae1dSRodney W. Grimes 1702df8bae1dSRodney W. Grimes return (0); 1703df8bae1dSRodney W. Grimes } 1704df8bae1dSRodney W. Grimes 1705c9bf0111SKATO Takenori static int 1706df8bae1dSRodney W. Grimes union_reclaim(ap) 1707df8bae1dSRodney W. Grimes struct vop_reclaim_args /* { 1708df8bae1dSRodney W. Grimes struct vnode *a_vp; 1709df8bae1dSRodney W. Grimes } */ *ap; 1710df8bae1dSRodney W. Grimes { 1711df8bae1dSRodney W. Grimes union_freevp(ap->a_vp); 1712df8bae1dSRodney W. Grimes 1713df8bae1dSRodney W. Grimes return (0); 1714df8bae1dSRodney W. Grimes } 1715df8bae1dSRodney W. Grimes 17163413421bSBoris Popov /* 17173413421bSBoris Popov * unionvp do not hold a VM object and there is no need to create one for 17183413421bSBoris Popov * upper or lower vp because it is done in the union_open() 17193413421bSBoris Popov */ 17203413421bSBoris Popov static int 17213413421bSBoris Popov union_createvobject(ap) 17223413421bSBoris Popov struct vop_createvobject_args /* { 17233413421bSBoris Popov struct vnode *vp; 17243413421bSBoris Popov struct ucred *cred; 1725b40ce416SJulian Elischer struct thread *td; 17263413421bSBoris Popov } */ *ap; 17273413421bSBoris Popov { 17283413421bSBoris Popov struct vnode *vp = ap->a_vp; 17293413421bSBoris Popov 1730e6e370a7SJeff Roberson vp->v_vflag |= VV_OBJBUF; 17313413421bSBoris Popov return (0); 17323413421bSBoris Popov } 17333413421bSBoris Popov 17343413421bSBoris Popov /* 17353413421bSBoris Popov * We have nothing to destroy and this operation shouldn't be bypassed. 17363413421bSBoris Popov */ 17373413421bSBoris Popov static int 17383413421bSBoris Popov union_destroyvobject(ap) 17393413421bSBoris Popov struct vop_destroyvobject_args /* { 17403413421bSBoris Popov struct vnode *vp; 17413413421bSBoris Popov } */ *ap; 17423413421bSBoris Popov { 17433413421bSBoris Popov struct vnode *vp = ap->a_vp; 17443413421bSBoris Popov 1745e6e370a7SJeff Roberson vp->v_vflag &= ~VV_OBJBUF; 17463413421bSBoris Popov return (0); 17473413421bSBoris Popov } 17483413421bSBoris Popov 17493413421bSBoris Popov /* 17503413421bSBoris Popov * Get VM object from the upper or lower vp 17513413421bSBoris Popov */ 17523413421bSBoris Popov static int 17533413421bSBoris Popov union_getvobject(ap) 17543413421bSBoris Popov struct vop_getvobject_args /* { 17553413421bSBoris Popov struct vnode *vp; 17563413421bSBoris Popov struct vm_object **objpp; 17573413421bSBoris Popov } */ *ap; 17583413421bSBoris Popov { 17593413421bSBoris Popov struct vnode *ovp = OTHERVP(ap->a_vp); 17603413421bSBoris Popov 17613413421bSBoris Popov if (ovp == NULL) 17623413421bSBoris Popov return EINVAL; 17633413421bSBoris Popov return (VOP_GETVOBJECT(ovp, ap->a_objpp)); 17643413421bSBoris Popov } 17653413421bSBoris Popov 1766c9bf0111SKATO Takenori static int 1767df8bae1dSRodney W. Grimes union_print(ap) 1768df8bae1dSRodney W. Grimes struct vop_print_args /* { 1769df8bae1dSRodney W. Grimes struct vnode *a_vp; 1770df8bae1dSRodney W. Grimes } */ *ap; 1771df8bae1dSRodney W. Grimes { 1772df8bae1dSRodney W. Grimes struct vnode *vp = ap->a_vp; 1773df8bae1dSRodney W. Grimes 177499648386SNate Lawson printf("\tvp=%p, uppervp=%p, lowervp=%p\n", 1775df8bae1dSRodney W. Grimes vp, UPPERVP(vp), LOWERVP(vp)); 1776996c772fSJohn Dyson if (UPPERVP(vp) != NULLVP) 1777996c772fSJohn Dyson vprint("union: upper", UPPERVP(vp)); 1778996c772fSJohn Dyson if (LOWERVP(vp) != NULLVP) 1779996c772fSJohn Dyson vprint("union: lower", LOWERVP(vp)); 1780996c772fSJohn Dyson 1781df8bae1dSRodney W. Grimes return (0); 1782df8bae1dSRodney W. Grimes } 1783df8bae1dSRodney W. Grimes 1784c9bf0111SKATO Takenori static int 1785df8bae1dSRodney W. Grimes union_pathconf(ap) 1786df8bae1dSRodney W. Grimes struct vop_pathconf_args /* { 1787df8bae1dSRodney W. Grimes struct vnode *a_vp; 1788df8bae1dSRodney W. Grimes int a_name; 1789df8bae1dSRodney W. Grimes int *a_retval; 1790df8bae1dSRodney W. Grimes } */ *ap; 1791df8bae1dSRodney W. Grimes { 1792df8bae1dSRodney W. Grimes int error; 1793b40ce416SJulian Elischer struct thread *td = curthread; /* XXX */ 17942a31267eSMatthew Dillon struct union_node *un = VTOUNION(ap->a_vp); 17952a31267eSMatthew Dillon struct vnode *vp; 1796df8bae1dSRodney W. Grimes 1797b40ce416SJulian Elischer vp = union_lock_other(un, td); 17982a31267eSMatthew Dillon KASSERT(vp != NULL, ("union_pathconf: backing vnode missing!")); 17992a31267eSMatthew Dillon 1800996c772fSJohn Dyson ap->a_vp = vp; 1801996c772fSJohn Dyson error = VCALL(vp, VOFFSET(vop_pathconf), ap); 1802b40ce416SJulian Elischer union_unlock_other(vp, td); 1803df8bae1dSRodney W. Grimes 1804df8bae1dSRodney W. Grimes return (error); 1805df8bae1dSRodney W. Grimes } 1806df8bae1dSRodney W. Grimes 1807c9bf0111SKATO Takenori static int 1808df8bae1dSRodney W. Grimes union_advlock(ap) 1809df8bae1dSRodney W. Grimes struct vop_advlock_args /* { 1810df8bae1dSRodney W. Grimes struct vnode *a_vp; 1811df8bae1dSRodney W. Grimes caddr_t a_id; 1812df8bae1dSRodney W. Grimes int a_op; 1813df8bae1dSRodney W. Grimes struct flock *a_fl; 1814df8bae1dSRodney W. Grimes int a_flags; 1815df8bae1dSRodney W. Grimes } */ *ap; 1816df8bae1dSRodney W. Grimes { 1817996c772fSJohn Dyson register struct vnode *ovp = OTHERVP(ap->a_vp); 1818df8bae1dSRodney W. Grimes 1819996c772fSJohn Dyson ap->a_vp = ovp; 1820996c772fSJohn Dyson return (VCALL(ovp, VOFFSET(vop_advlock), ap)); 1821df8bae1dSRodney W. Grimes } 1822df8bae1dSRodney W. Grimes 1823df8bae1dSRodney W. Grimes 1824df8bae1dSRodney W. Grimes /* 1825df8bae1dSRodney W. Grimes * XXX - vop_strategy must be hand coded because it has no 18262a31267eSMatthew Dillon * YYY - and it is not coherent with anything 18272a31267eSMatthew Dillon * 1828df8bae1dSRodney W. Grimes * vnode in its arguments. 1829df8bae1dSRodney W. Grimes * This goes away with a merged VM/buffer cache. 1830df8bae1dSRodney W. Grimes */ 1831c9bf0111SKATO Takenori static int 1832df8bae1dSRodney W. Grimes union_strategy(ap) 1833df8bae1dSRodney W. Grimes struct vop_strategy_args /* { 1834fd5d1124SJulian Elischer struct vnode *a_vp; 1835df8bae1dSRodney W. Grimes struct buf *a_bp; 1836df8bae1dSRodney W. Grimes } */ *ap; 1837df8bae1dSRodney W. Grimes { 1838df8bae1dSRodney W. Grimes struct buf *bp = ap->a_bp; 1839f9c8cab5SKirk McKusick struct vnode *othervp = OTHERVP(bp->b_vp); 1840df8bae1dSRodney W. Grimes 1841cefb5754SPoul-Henning Kamp KASSERT(ap->a_vp == ap->a_bp->b_vp, ("%s(%p != %p)", 1842cefb5754SPoul-Henning Kamp __func__, ap->a_vp, ap->a_bp->b_vp)); 1843df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC 1844f9c8cab5SKirk McKusick if (othervp == NULLVP) 1845df8bae1dSRodney W. Grimes panic("union_strategy: nil vp"); 184621144e3bSPoul-Henning Kamp if ((bp->b_iocmd == BIO_WRITE) && 1847f9c8cab5SKirk McKusick (othervp == LOWERVP(bp->b_vp))) 1848df8bae1dSRodney W. Grimes panic("union_strategy: writing to lowervp"); 1849df8bae1dSRodney W. Grimes #endif 1850f9c8cab5SKirk McKusick return (VOP_STRATEGY(othervp, bp)); 1851df8bae1dSRodney W. Grimes } 1852df8bae1dSRodney W. Grimes 185300fff2c7STim J. Robbins static int 185400fff2c7STim J. Robbins union_getacl(ap) 185500fff2c7STim J. Robbins struct vop_getacl_args /* { 185600fff2c7STim J. Robbins struct vnode *a_vp; 185700fff2c7STim J. Robbins acl_type_t a_type; 185800fff2c7STim J. Robbins struct acl *a_aclp; 185900fff2c7STim J. Robbins struct ucred *a_cred; 186000fff2c7STim J. Robbins struct thread *a_td; 186100fff2c7STim J. Robbins } */ *ap; 186200fff2c7STim J. Robbins { 186300fff2c7STim J. Robbins int error; 186400fff2c7STim J. Robbins struct union_node *un = VTOUNION(ap->a_vp); 186500fff2c7STim J. Robbins struct vnode *vp; 186600fff2c7STim J. Robbins 186700fff2c7STim J. Robbins vp = union_lock_other(un, ap->a_td); 186800fff2c7STim J. Robbins ap->a_vp = vp; 186900fff2c7STim J. Robbins error = VCALL(vp, VOFFSET(vop_getacl), ap); 187000fff2c7STim J. Robbins union_unlock_other(vp, ap->a_td); 187100fff2c7STim J. Robbins 187200fff2c7STim J. Robbins return (error); 187300fff2c7STim J. Robbins } 187400fff2c7STim J. Robbins 187500fff2c7STim J. Robbins static int 187600fff2c7STim J. Robbins union_setacl(ap) 187700fff2c7STim J. Robbins struct vop_setacl_args /* { 187800fff2c7STim J. Robbins struct vnode *a_vp; 187900fff2c7STim J. Robbins acl_type_t a_type; 188000fff2c7STim J. Robbins struct acl *a_aclp; 188100fff2c7STim J. Robbins struct ucred *a_cred; 188200fff2c7STim J. Robbins struct thread *a_td; 188300fff2c7STim J. Robbins } */ *ap; 188400fff2c7STim J. Robbins { 188500fff2c7STim J. Robbins int error; 188600fff2c7STim J. Robbins struct union_node *un = VTOUNION(ap->a_vp); 188700fff2c7STim J. Robbins struct vnode *vp; 188800fff2c7STim J. Robbins 188900fff2c7STim J. Robbins vp = union_lock_other(un, ap->a_td); 189000fff2c7STim J. Robbins ap->a_vp = vp; 189100fff2c7STim J. Robbins error = VCALL(vp, VOFFSET(vop_setacl), ap); 189200fff2c7STim J. Robbins union_unlock_other(vp, ap->a_td); 189300fff2c7STim J. Robbins 189400fff2c7STim J. Robbins return (error); 189500fff2c7STim J. Robbins } 189600fff2c7STim J. Robbins 189700fff2c7STim J. Robbins static int 189800fff2c7STim J. Robbins union_aclcheck(ap) 189900fff2c7STim J. Robbins struct vop_aclcheck_args /* { 190000fff2c7STim J. Robbins struct vnode *a_vp; 190100fff2c7STim J. Robbins acl_type_t a_type; 190200fff2c7STim J. Robbins struct acl *a_aclp; 190300fff2c7STim J. Robbins struct ucred *a_cred; 190400fff2c7STim J. Robbins struct thread *a_td; 190500fff2c7STim J. Robbins } */ *ap; 190600fff2c7STim J. Robbins { 190700fff2c7STim J. Robbins struct vnode *ovp = OTHERVP(ap->a_vp); 190800fff2c7STim J. Robbins 190900fff2c7STim J. Robbins ap->a_vp = ovp; 191000fff2c7STim J. Robbins return (VCALL(ovp, VOFFSET(vop_aclcheck), ap)); 191100fff2c7STim J. Robbins } 191200fff2c7STim J. Robbins 191300fff2c7STim J. Robbins static int 191400fff2c7STim J. Robbins union_closeextattr(ap) 191500fff2c7STim J. Robbins struct vop_closeextattr_args /* { 191600fff2c7STim J. Robbins struct vnode *a_vp; 191700fff2c7STim J. Robbins int a_commit; 191800fff2c7STim J. Robbins struct ucred *a_cred; 191900fff2c7STim J. Robbins struct thread *a_td; 192000fff2c7STim J. Robbins } */ *ap; 192100fff2c7STim J. Robbins { 192200fff2c7STim J. Robbins int error; 192300fff2c7STim J. Robbins struct union_node *un = VTOUNION(ap->a_vp); 192400fff2c7STim J. Robbins struct vnode *vp; 192500fff2c7STim J. Robbins 192600fff2c7STim J. Robbins vp = union_lock_other(un, ap->a_td); 192700fff2c7STim J. Robbins ap->a_vp = vp; 192800fff2c7STim J. Robbins error = VCALL(vp, VOFFSET(vop_closeextattr), ap); 192900fff2c7STim J. Robbins union_unlock_other(vp, ap->a_td); 193000fff2c7STim J. Robbins 193100fff2c7STim J. Robbins return (error); 193200fff2c7STim J. Robbins } 193300fff2c7STim J. Robbins 193400fff2c7STim J. Robbins static int 193500fff2c7STim J. Robbins union_getextattr(ap) 193600fff2c7STim J. Robbins struct vop_getextattr_args /* { 193700fff2c7STim J. Robbins struct vnode *a_vp; 193800fff2c7STim J. Robbins int a_attrnamespace; 193900fff2c7STim J. Robbins const char *a_name; 194000fff2c7STim J. Robbins struct uio *a_uio; 194100fff2c7STim J. Robbins size_t *a_size; 194200fff2c7STim J. Robbins struct ucred *a_cred; 194300fff2c7STim J. Robbins struct thread *a_td; 194400fff2c7STim J. Robbins } */ *ap; 194500fff2c7STim J. Robbins { 194600fff2c7STim J. Robbins int error; 194700fff2c7STim J. Robbins struct union_node *un = VTOUNION(ap->a_vp); 194800fff2c7STim J. Robbins struct vnode *vp; 194900fff2c7STim J. Robbins 195000fff2c7STim J. Robbins vp = union_lock_other(un, ap->a_td); 195100fff2c7STim J. Robbins ap->a_vp = vp; 195200fff2c7STim J. Robbins error = VCALL(vp, VOFFSET(vop_getextattr), ap); 195300fff2c7STim J. Robbins union_unlock_other(vp, ap->a_td); 195400fff2c7STim J. Robbins 195500fff2c7STim J. Robbins return (error); 195600fff2c7STim J. Robbins } 195700fff2c7STim J. Robbins 195800fff2c7STim J. Robbins static int 195900fff2c7STim J. Robbins union_listextattr(ap) 196000fff2c7STim J. Robbins struct vop_listextattr_args /* { 196100fff2c7STim J. Robbins struct vnode *a_vp; 196200fff2c7STim J. Robbins int a_attrnamespace; 196300fff2c7STim J. Robbins struct uio *a_uio; 196400fff2c7STim J. Robbins size_t *a_size; 196500fff2c7STim J. Robbins struct ucred *a_cred; 196600fff2c7STim J. Robbins struct thread *a_td; 196700fff2c7STim J. Robbins } */ *ap; 196800fff2c7STim J. Robbins { 196900fff2c7STim J. Robbins int error; 197000fff2c7STim J. Robbins struct union_node *un = VTOUNION(ap->a_vp); 197100fff2c7STim J. Robbins struct vnode *vp; 197200fff2c7STim J. Robbins 197300fff2c7STim J. Robbins vp = union_lock_other(un, ap->a_td); 197400fff2c7STim J. Robbins ap->a_vp = vp; 197500fff2c7STim J. Robbins error = VCALL(vp, VOFFSET(vop_listextattr), ap); 197600fff2c7STim J. Robbins union_unlock_other(vp, ap->a_td); 197700fff2c7STim J. Robbins 197800fff2c7STim J. Robbins return (error); 197900fff2c7STim J. Robbins } 198000fff2c7STim J. Robbins 198100fff2c7STim J. Robbins static int 198200fff2c7STim J. Robbins union_openextattr(ap) 198300fff2c7STim J. Robbins struct vop_openextattr_args /* { 198400fff2c7STim J. Robbins struct vnode *a_vp; 198500fff2c7STim J. Robbins struct ucred *a_cred; 198600fff2c7STim J. Robbins struct thread *a_td; 198700fff2c7STim J. Robbins } */ *ap; 198800fff2c7STim J. Robbins { 198900fff2c7STim J. Robbins int error; 199000fff2c7STim J. Robbins struct union_node *un = VTOUNION(ap->a_vp); 199100fff2c7STim J. Robbins struct vnode *vp; 199200fff2c7STim J. Robbins 199300fff2c7STim J. Robbins vp = union_lock_other(un, ap->a_td); 199400fff2c7STim J. Robbins ap->a_vp = vp; 199500fff2c7STim J. Robbins error = VCALL(vp, VOFFSET(vop_openextattr), ap); 199600fff2c7STim J. Robbins union_unlock_other(vp, ap->a_td); 199700fff2c7STim J. Robbins 199800fff2c7STim J. Robbins return (error); 199900fff2c7STim J. Robbins } 200000fff2c7STim J. Robbins 200100fff2c7STim J. Robbins static int 200200fff2c7STim J. Robbins union_deleteextattr(ap) 200300fff2c7STim J. Robbins struct vop_deleteextattr_args /* { 200400fff2c7STim J. Robbins struct vnode *a_vp; 200500fff2c7STim J. Robbins int a_attrnamespace; 200600fff2c7STim J. Robbins const char *a_name; 200700fff2c7STim J. Robbins struct ucred *a_cred; 200800fff2c7STim J. Robbins struct thread *a_td; 200900fff2c7STim J. Robbins } */ *ap; 201000fff2c7STim J. Robbins { 201100fff2c7STim J. Robbins int error; 201200fff2c7STim J. Robbins struct union_node *un = VTOUNION(ap->a_vp); 201300fff2c7STim J. Robbins struct vnode *vp; 201400fff2c7STim J. Robbins 201500fff2c7STim J. Robbins vp = union_lock_other(un, ap->a_td); 201600fff2c7STim J. Robbins ap->a_vp = vp; 201700fff2c7STim J. Robbins error = VCALL(vp, VOFFSET(vop_deleteextattr), ap); 201800fff2c7STim J. Robbins union_unlock_other(vp, ap->a_td); 201900fff2c7STim J. Robbins 202000fff2c7STim J. Robbins return (error); 202100fff2c7STim J. Robbins } 202200fff2c7STim J. Robbins 202300fff2c7STim J. Robbins static int 202400fff2c7STim J. Robbins union_setextattr(ap) 202500fff2c7STim J. Robbins struct vop_setextattr_args /* { 202600fff2c7STim J. Robbins struct vnode *a_vp; 202700fff2c7STim J. Robbins int a_attrnamespace; 202800fff2c7STim J. Robbins const char *a_name; 202900fff2c7STim J. Robbins struct uio *a_uio; 203000fff2c7STim J. Robbins struct ucred *a_cred; 203100fff2c7STim J. Robbins struct thread *a_td; 203200fff2c7STim J. Robbins } */ *ap; 203300fff2c7STim J. Robbins { 203400fff2c7STim J. Robbins int error; 203500fff2c7STim J. Robbins struct union_node *un = VTOUNION(ap->a_vp); 203600fff2c7STim J. Robbins struct vnode *vp; 203700fff2c7STim J. Robbins 203800fff2c7STim J. Robbins vp = union_lock_other(un, ap->a_td); 203900fff2c7STim J. Robbins ap->a_vp = vp; 204000fff2c7STim J. Robbins error = VCALL(vp, VOFFSET(vop_setextattr), ap); 204100fff2c7STim J. Robbins union_unlock_other(vp, ap->a_td); 204200fff2c7STim J. Robbins 204300fff2c7STim J. Robbins return (error); 204400fff2c7STim J. Robbins } 204500fff2c7STim J. Robbins 204600fff2c7STim J. Robbins static int 204700fff2c7STim J. Robbins union_setlabel(ap) 204800fff2c7STim J. Robbins struct vop_setlabel_args /* { 204900fff2c7STim J. Robbins struct vnode *a_vp; 205000fff2c7STim J. Robbins struct label *a_label; 205100fff2c7STim J. Robbins struct ucred *a_cred; 205200fff2c7STim J. Robbins struct thread *a_td; 205300fff2c7STim J. Robbins } */ *ap; 205400fff2c7STim J. Robbins { 205500fff2c7STim J. Robbins int error; 205600fff2c7STim J. Robbins struct union_node *un = VTOUNION(ap->a_vp); 205700fff2c7STim J. Robbins struct vnode *vp; 205800fff2c7STim J. Robbins 205900fff2c7STim J. Robbins vp = union_lock_other(un, ap->a_td); 206000fff2c7STim J. Robbins ap->a_vp = vp; 206100fff2c7STim J. Robbins error = VCALL(vp, VOFFSET(vop_setlabel), ap); 206200fff2c7STim J. Robbins union_unlock_other(vp, ap->a_td); 206300fff2c7STim J. Robbins 206400fff2c7STim J. Robbins return (error); 206500fff2c7STim J. Robbins } 206600fff2c7STim J. Robbins 2067df8bae1dSRodney W. Grimes /* 2068df8bae1dSRodney W. Grimes * Global vfs data structures 2069df8bae1dSRodney W. Grimes */ 2070f57e6547SBruce Evans vop_t **union_vnodeop_p; 2071c109c577SBruce Evans static struct vnodeopv_entry_desc union_vnodeop_entries[] = { 2072dba3870cSPoul-Henning Kamp { &vop_default_desc, (vop_t *) vop_defaultop }, 2073539ef70cSPoul-Henning Kamp { &vop_access_desc, (vop_t *) union_access }, 207400fff2c7STim J. Robbins { &vop_aclcheck_desc, (vop_t *) union_aclcheck }, 2075539ef70cSPoul-Henning Kamp { &vop_advlock_desc, (vop_t *) union_advlock }, 2076b7ebffbcSPoul-Henning Kamp { &vop_bmap_desc, (vop_t *) vop_eopnotsupp }, 2077539ef70cSPoul-Henning Kamp { &vop_close_desc, (vop_t *) union_close }, 207800fff2c7STim J. Robbins { &vop_closeextattr_desc, (vop_t *) union_closeextattr }, 2079539ef70cSPoul-Henning Kamp { &vop_create_desc, (vop_t *) union_create }, 20803413421bSBoris Popov { &vop_createvobject_desc, (vop_t *) union_createvobject }, 208100fff2c7STim J. Robbins { &vop_deleteextattr_desc, (vop_t *) union_deleteextattr }, 20823413421bSBoris Popov { &vop_destroyvobject_desc, (vop_t *) union_destroyvobject }, 2083539ef70cSPoul-Henning Kamp { &vop_fsync_desc, (vop_t *) union_fsync }, 2084539ef70cSPoul-Henning Kamp { &vop_getattr_desc, (vop_t *) union_getattr }, 208500fff2c7STim J. Robbins { &vop_getacl_desc, (vop_t *) union_getacl }, 208600fff2c7STim J. Robbins { &vop_getextattr_desc, (vop_t *) union_getextattr }, 20873413421bSBoris Popov { &vop_getvobject_desc, (vop_t *) union_getvobject }, 2088539ef70cSPoul-Henning Kamp { &vop_inactive_desc, (vop_t *) union_inactive }, 2089539ef70cSPoul-Henning Kamp { &vop_ioctl_desc, (vop_t *) union_ioctl }, 2090539ef70cSPoul-Henning Kamp { &vop_lease_desc, (vop_t *) union_lease }, 2091539ef70cSPoul-Henning Kamp { &vop_link_desc, (vop_t *) union_link }, 209200fff2c7STim J. Robbins { &vop_listextattr_desc, (vop_t *) union_listextattr }, 2093539ef70cSPoul-Henning Kamp { &vop_lookup_desc, (vop_t *) union_lookup }, 2094539ef70cSPoul-Henning Kamp { &vop_mkdir_desc, (vop_t *) union_mkdir }, 2095539ef70cSPoul-Henning Kamp { &vop_mknod_desc, (vop_t *) union_mknod }, 2096539ef70cSPoul-Henning Kamp { &vop_open_desc, (vop_t *) union_open }, 209700fff2c7STim J. Robbins { &vop_openextattr_desc, (vop_t *) union_openextattr }, 2098539ef70cSPoul-Henning Kamp { &vop_pathconf_desc, (vop_t *) union_pathconf }, 2099539ef70cSPoul-Henning Kamp { &vop_poll_desc, (vop_t *) union_poll }, 2100539ef70cSPoul-Henning Kamp { &vop_print_desc, (vop_t *) union_print }, 2101539ef70cSPoul-Henning Kamp { &vop_read_desc, (vop_t *) union_read }, 2102539ef70cSPoul-Henning Kamp { &vop_readdir_desc, (vop_t *) union_readdir }, 2103539ef70cSPoul-Henning Kamp { &vop_readlink_desc, (vop_t *) union_readlink }, 2104f2a2857bSKirk McKusick { &vop_getwritemount_desc, (vop_t *) union_getwritemount }, 2105539ef70cSPoul-Henning Kamp { &vop_reclaim_desc, (vop_t *) union_reclaim }, 2106539ef70cSPoul-Henning Kamp { &vop_remove_desc, (vop_t *) union_remove }, 2107539ef70cSPoul-Henning Kamp { &vop_rename_desc, (vop_t *) union_rename }, 2108539ef70cSPoul-Henning Kamp { &vop_revoke_desc, (vop_t *) union_revoke }, 2109539ef70cSPoul-Henning Kamp { &vop_rmdir_desc, (vop_t *) union_rmdir }, 211000fff2c7STim J. Robbins { &vop_setacl_desc, (vop_t *) union_setacl }, 2111539ef70cSPoul-Henning Kamp { &vop_setattr_desc, (vop_t *) union_setattr }, 211200fff2c7STim J. Robbins { &vop_setextattr_desc, (vop_t *) union_setextattr }, 211300fff2c7STim J. Robbins { &vop_setlabel_desc, (vop_t *) union_setlabel }, 2114539ef70cSPoul-Henning Kamp { &vop_strategy_desc, (vop_t *) union_strategy }, 2115539ef70cSPoul-Henning Kamp { &vop_symlink_desc, (vop_t *) union_symlink }, 2116539ef70cSPoul-Henning Kamp { &vop_whiteout_desc, (vop_t *) union_whiteout }, 2117539ef70cSPoul-Henning Kamp { &vop_write_desc, (vop_t *) union_write }, 2118f57e6547SBruce Evans { NULL, NULL } 2119df8bae1dSRodney W. Grimes }; 2120c109c577SBruce Evans static struct vnodeopv_desc union_vnodeop_opv_desc = 2121df8bae1dSRodney W. Grimes { &union_vnodeop_p, union_vnodeop_entries }; 2122c901836cSGarrett Wollman 2123c901836cSGarrett Wollman VNODEOP_SET(union_vnodeop_opv_desc); 2124