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 38a9320ff3SKATO Takenori * $Id: union_vnops.c,v 1.30 1997/04/27 10:49:37 kato Exp $ 39df8bae1dSRodney W. Grimes */ 40df8bae1dSRodney W. Grimes 41df8bae1dSRodney W. Grimes #include <sys/param.h> 42df8bae1dSRodney W. Grimes #include <sys/systm.h> 43df8bae1dSRodney W. Grimes #include <sys/proc.h> 443ac4d1efSBruce Evans #include <sys/fcntl.h> 45df8bae1dSRodney W. Grimes #include <sys/time.h> 46996c772fSJohn Dyson #include <sys/stat.h> 47df8bae1dSRodney W. Grimes #include <sys/types.h> 489e67ea79SBruce Evans #include <sys/kernel.h> 49df8bae1dSRodney W. Grimes #include <sys/vnode.h> 50df8bae1dSRodney W. Grimes #include <sys/mount.h> 51df8bae1dSRodney W. Grimes #include <sys/namei.h> 52df8bae1dSRodney W. Grimes #include <sys/malloc.h> 53df8bae1dSRodney W. Grimes #include <sys/buf.h> 54df8bae1dSRodney W. Grimes #include <sys/queue.h> 55996c772fSJohn Dyson #include <sys/lock.h> 56df8bae1dSRodney W. Grimes #include <miscfs/union/union.h> 57df8bae1dSRodney W. Grimes 58996c772fSJohn Dyson #define FIXUP(un, p) { \ 59996c772fSJohn Dyson if (((un)->un_flags & UN_ULOCK) == 0) { \ 60996c772fSJohn Dyson union_fixup(un, p); \ 61df8bae1dSRodney W. Grimes } \ 62df8bae1dSRodney W. Grimes } 63df8bae1dSRodney W. Grimes 64a9320ff3SKATO Takenori #define SETKLOCK(un) (un)->un_flags |= UN_KLOCK 65a9320ff3SKATO Takenori #define CLEARKLOCK(un) (un)->un_flags &= ~UN_KLOCK 66a9320ff3SKATO Takenori 679b5e8b3aSBruce Evans extern int union_abortop __P((struct vop_abortop_args *ap)); 689b5e8b3aSBruce Evans extern int union_access __P((struct vop_access_args *ap)); 699b5e8b3aSBruce Evans extern int union_advlock __P((struct vop_advlock_args *ap)); 709b5e8b3aSBruce Evans extern int union_bmap __P((struct vop_bmap_args *ap)); 719b5e8b3aSBruce Evans extern int union_close __P((struct vop_close_args *ap)); 729b5e8b3aSBruce Evans extern int union_create __P((struct vop_create_args *ap)); 73996c772fSJohn Dyson static void union_fixup __P((struct union_node *un, struct proc *p)); 749b5e8b3aSBruce Evans extern int union_fsync __P((struct vop_fsync_args *ap)); 759b5e8b3aSBruce Evans extern int union_getattr __P((struct vop_getattr_args *ap)); 769b5e8b3aSBruce Evans extern int union_inactive __P((struct vop_inactive_args *ap)); 779b5e8b3aSBruce Evans extern int union_ioctl __P((struct vop_ioctl_args *ap)); 789b5e8b3aSBruce Evans extern int union_islocked __P((struct vop_islocked_args *ap)); 7972a5ee14SMike Pritchard extern int union_lease __P((struct vop_lease_args *ap)); 809b5e8b3aSBruce Evans extern int union_link __P((struct vop_link_args *ap)); 819b5e8b3aSBruce Evans extern int union_lock __P((struct vop_lock_args *ap)); 829b5e8b3aSBruce Evans extern int union_lookup __P((struct vop_lookup_args *ap)); 83996c772fSJohn Dyson static int union_lookup1 __P((struct vnode *udvp, struct vnode **dvpp, 849b5e8b3aSBruce Evans struct vnode **vpp, 859b5e8b3aSBruce Evans struct componentname *cnp)); 869b5e8b3aSBruce Evans extern int union_mkdir __P((struct vop_mkdir_args *ap)); 879b5e8b3aSBruce Evans extern int union_mknod __P((struct vop_mknod_args *ap)); 889b5e8b3aSBruce Evans extern int union_mmap __P((struct vop_mmap_args *ap)); 899b5e8b3aSBruce Evans extern int union_open __P((struct vop_open_args *ap)); 909b5e8b3aSBruce Evans extern int union_pathconf __P((struct vop_pathconf_args *ap)); 919b5e8b3aSBruce Evans extern int union_print __P((struct vop_print_args *ap)); 929b5e8b3aSBruce Evans extern int union_read __P((struct vop_read_args *ap)); 939b5e8b3aSBruce Evans extern int union_readdir __P((struct vop_readdir_args *ap)); 949b5e8b3aSBruce Evans extern int union_readlink __P((struct vop_readlink_args *ap)); 959b5e8b3aSBruce Evans extern int union_reclaim __P((struct vop_reclaim_args *ap)); 969b5e8b3aSBruce Evans extern int union_remove __P((struct vop_remove_args *ap)); 979b5e8b3aSBruce Evans extern int union_rename __P((struct vop_rename_args *ap)); 9872a5ee14SMike Pritchard extern int union_revoke __P((struct vop_revoke_args *ap)); 999b5e8b3aSBruce Evans extern int union_rmdir __P((struct vop_rmdir_args *ap)); 1009b5e8b3aSBruce Evans extern int union_seek __P((struct vop_seek_args *ap)); 1019b5e8b3aSBruce Evans extern int union_select __P((struct vop_select_args *ap)); 1029b5e8b3aSBruce Evans extern int union_setattr __P((struct vop_setattr_args *ap)); 1039b5e8b3aSBruce Evans extern int union_strategy __P((struct vop_strategy_args *ap)); 1049b5e8b3aSBruce Evans extern int union_symlink __P((struct vop_symlink_args *ap)); 105996c772fSJohn Dyson extern int union_unlock __P((struct vop_unlock_args *ap)); 10672a5ee14SMike Pritchard extern int union_whiteout __P((struct vop_whiteout_args *ap)); 1079b5e8b3aSBruce Evans extern int union_write __P((struct vop_read_args *ap)); 1089b5e8b3aSBruce Evans 109df8bae1dSRodney W. Grimes static void 110996c772fSJohn Dyson union_fixup(un, p) 111df8bae1dSRodney W. Grimes struct union_node *un; 112996c772fSJohn Dyson struct proc *p; 113df8bae1dSRodney W. Grimes { 114df8bae1dSRodney W. Grimes 115996c772fSJohn Dyson vn_lock(un->un_uppervp, LK_EXCLUSIVE | LK_RETRY, p); 116df8bae1dSRodney W. Grimes un->un_flags |= UN_ULOCK; 117df8bae1dSRodney W. Grimes } 118df8bae1dSRodney W. Grimes 119df8bae1dSRodney W. Grimes static int 120996c772fSJohn Dyson union_lookup1(udvp, dvpp, vpp, cnp) 121df8bae1dSRodney W. Grimes struct vnode *udvp; 122996c772fSJohn Dyson struct vnode **dvpp; 123df8bae1dSRodney W. Grimes struct vnode **vpp; 124df8bae1dSRodney W. Grimes struct componentname *cnp; 125df8bae1dSRodney W. Grimes { 126df8bae1dSRodney W. Grimes int error; 127996c772fSJohn Dyson struct proc *p = cnp->cn_proc; 128df8bae1dSRodney W. Grimes struct vnode *tdvp; 129996c772fSJohn Dyson struct vnode *dvp; 130df8bae1dSRodney W. Grimes struct mount *mp; 131df8bae1dSRodney W. Grimes 132996c772fSJohn Dyson dvp = *dvpp; 133996c772fSJohn Dyson 134df8bae1dSRodney W. Grimes /* 135df8bae1dSRodney W. Grimes * If stepping up the directory tree, check for going 136df8bae1dSRodney W. Grimes * back across the mount point, in which case do what 137df8bae1dSRodney W. Grimes * lookup would do by stepping back down the mount 138df8bae1dSRodney W. Grimes * hierarchy. 139df8bae1dSRodney W. Grimes */ 140df8bae1dSRodney W. Grimes if (cnp->cn_flags & ISDOTDOT) { 141996c772fSJohn Dyson while ((dvp != udvp) && (dvp->v_flag & VROOT)) { 142df8bae1dSRodney W. Grimes /* 143df8bae1dSRodney W. Grimes * Don't do the NOCROSSMOUNT check 144df8bae1dSRodney W. Grimes * at this level. By definition, 145df8bae1dSRodney W. Grimes * union fs deals with namespaces, not 146df8bae1dSRodney W. Grimes * filesystems. 147df8bae1dSRodney W. Grimes */ 148df8bae1dSRodney W. Grimes tdvp = dvp; 149996c772fSJohn Dyson *dvpp = dvp = dvp->v_mount->mnt_vnodecovered; 150df8bae1dSRodney W. Grimes vput(tdvp); 151df8bae1dSRodney W. Grimes VREF(dvp); 152996c772fSJohn Dyson vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, p); 153df8bae1dSRodney W. Grimes } 154df8bae1dSRodney W. Grimes } 155df8bae1dSRodney W. Grimes 156df8bae1dSRodney W. Grimes error = VOP_LOOKUP(dvp, &tdvp, cnp); 157df8bae1dSRodney W. Grimes if (error) 158df8bae1dSRodney W. Grimes return (error); 159df8bae1dSRodney W. Grimes 160df8bae1dSRodney W. Grimes /* 161df8bae1dSRodney W. Grimes * The parent directory will have been unlocked, unless lookup 162df8bae1dSRodney W. Grimes * found the last component. In which case, re-lock the node 163df8bae1dSRodney W. Grimes * here to allow it to be unlocked again (phew) in union_lookup. 164df8bae1dSRodney W. Grimes */ 165df8bae1dSRodney W. Grimes if (dvp != tdvp && !(cnp->cn_flags & ISLASTCN)) 166996c772fSJohn Dyson vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, p); 167df8bae1dSRodney W. Grimes 168df8bae1dSRodney W. Grimes dvp = tdvp; 169df8bae1dSRodney W. Grimes 170df8bae1dSRodney W. Grimes /* 171df8bae1dSRodney W. Grimes * Lastly check if the current node is a mount point in 172df8bae1dSRodney W. Grimes * which case walk up the mount hierarchy making sure not to 173df8bae1dSRodney W. Grimes * bump into the root of the mount tree (ie. dvp != udvp). 174df8bae1dSRodney W. Grimes */ 175df8bae1dSRodney W. Grimes while (dvp != udvp && (dvp->v_type == VDIR) && 176df8bae1dSRodney W. Grimes (mp = dvp->v_mountedhere)) { 177df8bae1dSRodney W. Grimes 178996c772fSJohn Dyson if (vfs_busy(mp, 0, 0, p)) 179df8bae1dSRodney W. Grimes continue; 180df8bae1dSRodney W. Grimes 1813a773ad0SPoul-Henning Kamp error = VFS_ROOT(mp, &tdvp); 182996c772fSJohn Dyson vfs_unbusy(mp, p); 1833a773ad0SPoul-Henning Kamp if (error) { 184df8bae1dSRodney W. Grimes vput(dvp); 185df8bae1dSRodney W. Grimes return (error); 186df8bae1dSRodney W. Grimes } 187df8bae1dSRodney W. Grimes 188df8bae1dSRodney W. Grimes vput(dvp); 189df8bae1dSRodney W. Grimes dvp = tdvp; 190df8bae1dSRodney W. Grimes } 191df8bae1dSRodney W. Grimes 192df8bae1dSRodney W. Grimes *vpp = dvp; 193df8bae1dSRodney W. Grimes return (0); 194df8bae1dSRodney W. Grimes } 195df8bae1dSRodney W. Grimes 196df8bae1dSRodney W. Grimes int 197df8bae1dSRodney W. Grimes union_lookup(ap) 198df8bae1dSRodney W. Grimes struct vop_lookup_args /* { 199df8bae1dSRodney W. Grimes struct vnodeop_desc *a_desc; 200df8bae1dSRodney W. Grimes struct vnode *a_dvp; 201df8bae1dSRodney W. Grimes struct vnode **a_vpp; 202df8bae1dSRodney W. Grimes struct componentname *a_cnp; 203df8bae1dSRodney W. Grimes } */ *ap; 204df8bae1dSRodney W. Grimes { 205df8bae1dSRodney W. Grimes int error; 206df8bae1dSRodney W. Grimes int uerror, lerror; 207df8bae1dSRodney W. Grimes struct vnode *uppervp, *lowervp; 208df8bae1dSRodney W. Grimes struct vnode *upperdvp, *lowerdvp; 209df8bae1dSRodney W. Grimes struct vnode *dvp = ap->a_dvp; 210df8bae1dSRodney W. Grimes struct union_node *dun = VTOUNION(dvp); 211df8bae1dSRodney W. Grimes struct componentname *cnp = ap->a_cnp; 212996c772fSJohn Dyson struct proc *p = cnp->cn_proc; 213df8bae1dSRodney W. Grimes int lockparent = cnp->cn_flags & LOCKPARENT; 214df8bae1dSRodney W. Grimes struct union_mount *um = MOUNTTOUNIONMOUNT(dvp->v_mount); 215996c772fSJohn Dyson struct ucred *saved_cred; 216996c772fSJohn Dyson int iswhiteout; 217996c772fSJohn Dyson struct vattr va; 218996c772fSJohn Dyson 219996c772fSJohn Dyson #ifdef notyet 220996c772fSJohn Dyson if (cnp->cn_namelen == 3 && 221996c772fSJohn Dyson cnp->cn_nameptr[2] == '.' && 222996c772fSJohn Dyson cnp->cn_nameptr[1] == '.' && 223996c772fSJohn Dyson cnp->cn_nameptr[0] == '.') { 224996c772fSJohn Dyson dvp = *ap->a_vpp = LOWERVP(ap->a_dvp); 225996c772fSJohn Dyson if (dvp == NULLVP) 226996c772fSJohn Dyson return (ENOENT); 227996c772fSJohn Dyson VREF(dvp); 228996c772fSJohn Dyson vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, p); 229996c772fSJohn Dyson if (!lockparent || !(cnp->cn_flags & ISLASTCN)) 230996c772fSJohn Dyson VOP_UNLOCK(ap->a_dvp, 0, p); 231996c772fSJohn Dyson return (0); 232996c772fSJohn Dyson } 233996c772fSJohn Dyson #endif 234df8bae1dSRodney W. Grimes 235df8bae1dSRodney W. Grimes cnp->cn_flags |= LOCKPARENT; 236df8bae1dSRodney W. Grimes 237df8bae1dSRodney W. Grimes upperdvp = dun->un_uppervp; 238df8bae1dSRodney W. Grimes lowerdvp = dun->un_lowervp; 239df8bae1dSRodney W. Grimes uppervp = NULLVP; 240df8bae1dSRodney W. Grimes lowervp = NULLVP; 241996c772fSJohn Dyson iswhiteout = 0; 242df8bae1dSRodney W. Grimes 243df8bae1dSRodney W. Grimes /* 244df8bae1dSRodney W. Grimes * do the lookup in the upper level. 245df8bae1dSRodney W. Grimes * if that level comsumes additional pathnames, 246df8bae1dSRodney W. Grimes * then assume that something special is going 247df8bae1dSRodney W. Grimes * on and just return that vnode. 248df8bae1dSRodney W. Grimes */ 249996c772fSJohn Dyson if (upperdvp != NULLVP) { 250996c772fSJohn Dyson FIXUP(dun, p); 251ee582cdfSKATO Takenori /* 252ee582cdfSKATO Takenori * If we're doing `..' in the underlying filesystem, 253ee582cdfSKATO Takenori * we must drop our lock on the union node before 254ee582cdfSKATO Takenori * going up the tree in the lower file system--if we block 255ee582cdfSKATO Takenori * on the lowervp lock, and that's held by someone else 256ee582cdfSKATO Takenori * coming down the tree and who's waiting for our lock, 257ee582cdfSKATO Takenori * we would be hosed. 258ee582cdfSKATO Takenori */ 259ee582cdfSKATO Takenori if (cnp->cn_flags & ISDOTDOT) { 260ee582cdfSKATO Takenori /* retain lock on underlying VP: */ 261a9320ff3SKATO Takenori SETKLOCK(dun); 262ee582cdfSKATO Takenori VOP_UNLOCK(dvp, 0, p); 263a9320ff3SKATO Takenori CLEARKLOCK(dun); 264ee582cdfSKATO Takenori } 265996c772fSJohn Dyson uerror = union_lookup1(um->um_uppervp, &upperdvp, 266df8bae1dSRodney W. Grimes &uppervp, cnp); 267ee582cdfSKATO Takenori if (cnp->cn_flags & ISDOTDOT) { 268ee582cdfSKATO Takenori if (dun->un_uppervp == upperdvp) { 269ee582cdfSKATO Takenori /* 270ee582cdfSKATO Takenori * We got the underlying bugger back locked... 271ee582cdfSKATO Takenori * now take back the union node lock. Since we 272ee582cdfSKATO Takenori * hold the uppervp lock, we can diddle union 273ee582cdfSKATO Takenori * locking flags at will. :) 274ee582cdfSKATO Takenori */ 275ee582cdfSKATO Takenori dun->un_flags |= UN_ULOCK; 276ee582cdfSKATO Takenori } 277ee582cdfSKATO Takenori /* 278ee582cdfSKATO Takenori * If upperdvp got swapped out, it means we did 279ee582cdfSKATO Takenori * some mount point magic, and we do not have 280ee582cdfSKATO Takenori * dun->un_uppervp locked currently--so we get it 281ee582cdfSKATO Takenori * locked here (don't set the UN_ULOCK flag). 282ee582cdfSKATO Takenori */ 283077f31beSKATO Takenori vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, p); 284ee582cdfSKATO Takenori } 285ee582cdfSKATO Takenori 286df8bae1dSRodney W. Grimes /*if (uppervp == upperdvp) 287df8bae1dSRodney W. Grimes dun->un_flags |= UN_KLOCK;*/ 288df8bae1dSRodney W. Grimes 289df8bae1dSRodney W. Grimes if (cnp->cn_consume != 0) { 290df8bae1dSRodney W. Grimes *ap->a_vpp = uppervp; 291df8bae1dSRodney W. Grimes if (!lockparent) 292df8bae1dSRodney W. Grimes cnp->cn_flags &= ~LOCKPARENT; 293df8bae1dSRodney W. Grimes return (uerror); 294df8bae1dSRodney W. Grimes } 295996c772fSJohn Dyson if (uerror == ENOENT || uerror == EJUSTRETURN) { 296996c772fSJohn Dyson if (cnp->cn_flags & ISWHITEOUT) { 297996c772fSJohn Dyson iswhiteout = 1; 298996c772fSJohn Dyson } else if (lowerdvp != NULLVP) { 299996c772fSJohn Dyson lerror = VOP_GETATTR(upperdvp, &va, 300996c772fSJohn Dyson cnp->cn_cred, cnp->cn_proc); 301996c772fSJohn Dyson if (lerror == 0 && (va.va_flags & OPAQUE)) 302996c772fSJohn Dyson iswhiteout = 1; 303996c772fSJohn Dyson } 304996c772fSJohn Dyson } 305df8bae1dSRodney W. Grimes } else { 306df8bae1dSRodney W. Grimes uerror = ENOENT; 307df8bae1dSRodney W. Grimes } 308df8bae1dSRodney W. Grimes 309df8bae1dSRodney W. Grimes /* 310df8bae1dSRodney W. Grimes * in a similar way to the upper layer, do the lookup 311df8bae1dSRodney W. Grimes * in the lower layer. this time, if there is some 312df8bae1dSRodney W. Grimes * component magic going on, then vput whatever we got 313df8bae1dSRodney W. Grimes * back from the upper layer and return the lower vnode 314df8bae1dSRodney W. Grimes * instead. 315df8bae1dSRodney W. Grimes */ 316996c772fSJohn Dyson if (lowerdvp != NULLVP && !iswhiteout) { 317df8bae1dSRodney W. Grimes int nameiop; 318df8bae1dSRodney W. Grimes 319996c772fSJohn Dyson vn_lock(lowerdvp, LK_EXCLUSIVE | LK_RETRY, p); 320df8bae1dSRodney W. Grimes 321df8bae1dSRodney W. Grimes /* 322df8bae1dSRodney W. Grimes * Only do a LOOKUP on the bottom node, since 323df8bae1dSRodney W. Grimes * we won't be making changes to it anyway. 324df8bae1dSRodney W. Grimes */ 325df8bae1dSRodney W. Grimes nameiop = cnp->cn_nameiop; 326df8bae1dSRodney W. Grimes cnp->cn_nameiop = LOOKUP; 327df8bae1dSRodney W. Grimes if (um->um_op == UNMNT_BELOW) { 328df8bae1dSRodney W. Grimes saved_cred = cnp->cn_cred; 329df8bae1dSRodney W. Grimes cnp->cn_cred = um->um_cred; 330df8bae1dSRodney W. Grimes } 331ee582cdfSKATO Takenori /* 332ee582cdfSKATO Takenori * We shouldn't have to worry about locking interactions 333ee582cdfSKATO Takenori * between the lower layer and our union layer (w.r.t. 334ee582cdfSKATO Takenori * `..' processing) because we don't futz with lowervp 335ee582cdfSKATO Takenori * locks in the union-node instantiation code path. 336ee582cdfSKATO Takenori */ 337996c772fSJohn Dyson lerror = union_lookup1(um->um_lowervp, &lowerdvp, 338df8bae1dSRodney W. Grimes &lowervp, cnp); 339df8bae1dSRodney W. Grimes if (um->um_op == UNMNT_BELOW) 340df8bae1dSRodney W. Grimes cnp->cn_cred = saved_cred; 341df8bae1dSRodney W. Grimes cnp->cn_nameiop = nameiop; 342df8bae1dSRodney W. Grimes 343df8bae1dSRodney W. Grimes if (lowervp != lowerdvp) 344996c772fSJohn Dyson VOP_UNLOCK(lowerdvp, 0, p); 345df8bae1dSRodney W. Grimes 346df8bae1dSRodney W. Grimes if (cnp->cn_consume != 0) { 347996c772fSJohn Dyson if (uppervp != NULLVP) { 348df8bae1dSRodney W. Grimes if (uppervp == upperdvp) 349df8bae1dSRodney W. Grimes vrele(uppervp); 350df8bae1dSRodney W. Grimes else 351df8bae1dSRodney W. Grimes vput(uppervp); 352df8bae1dSRodney W. Grimes uppervp = NULLVP; 353df8bae1dSRodney W. Grimes } 354df8bae1dSRodney W. Grimes *ap->a_vpp = lowervp; 355df8bae1dSRodney W. Grimes if (!lockparent) 356df8bae1dSRodney W. Grimes cnp->cn_flags &= ~LOCKPARENT; 357df8bae1dSRodney W. Grimes return (lerror); 358df8bae1dSRodney W. Grimes } 359df8bae1dSRodney W. Grimes } else { 360df8bae1dSRodney W. Grimes lerror = ENOENT; 361996c772fSJohn Dyson if ((cnp->cn_flags & ISDOTDOT) && dun->un_pvp != NULLVP) { 362996c772fSJohn Dyson lowervp = LOWERVP(dun->un_pvp); 363996c772fSJohn Dyson if (lowervp != NULLVP) { 364996c772fSJohn Dyson VREF(lowervp); 365996c772fSJohn Dyson vn_lock(lowervp, LK_EXCLUSIVE | LK_RETRY, p); 366996c772fSJohn Dyson lerror = 0; 367996c772fSJohn Dyson } 368996c772fSJohn Dyson } 369df8bae1dSRodney W. Grimes } 370df8bae1dSRodney W. Grimes 371df8bae1dSRodney W. Grimes if (!lockparent) 372df8bae1dSRodney W. Grimes cnp->cn_flags &= ~LOCKPARENT; 373df8bae1dSRodney W. Grimes 374df8bae1dSRodney W. Grimes /* 375df8bae1dSRodney W. Grimes * at this point, we have uerror and lerror indicating 376df8bae1dSRodney W. Grimes * possible errors with the lookups in the upper and lower 377df8bae1dSRodney W. Grimes * layers. additionally, uppervp and lowervp are (locked) 378df8bae1dSRodney W. Grimes * references to existing vnodes in the upper and lower layers. 379df8bae1dSRodney W. Grimes * 380df8bae1dSRodney W. Grimes * there are now three cases to consider. 381df8bae1dSRodney W. Grimes * 1. if both layers returned an error, then return whatever 382df8bae1dSRodney W. Grimes * error the upper layer generated. 383df8bae1dSRodney W. Grimes * 384df8bae1dSRodney W. Grimes * 2. if the top layer failed and the bottom layer succeeded 385df8bae1dSRodney W. Grimes * then two subcases occur. 386df8bae1dSRodney W. Grimes * a. the bottom vnode is not a directory, in which 387df8bae1dSRodney W. Grimes * case just return a new union vnode referencing 388df8bae1dSRodney W. Grimes * an empty top layer and the existing bottom layer. 389df8bae1dSRodney W. Grimes * b. the bottom vnode is a directory, in which case 390df8bae1dSRodney W. Grimes * create a new directory in the top-level and 391df8bae1dSRodney W. Grimes * continue as in case 3. 392df8bae1dSRodney W. Grimes * 393df8bae1dSRodney W. Grimes * 3. if the top layer succeeded then return a new union 394df8bae1dSRodney W. Grimes * vnode referencing whatever the new top layer and 395df8bae1dSRodney W. Grimes * whatever the bottom layer returned. 396df8bae1dSRodney W. Grimes */ 397df8bae1dSRodney W. Grimes 398df8bae1dSRodney W. Grimes *ap->a_vpp = NULLVP; 399df8bae1dSRodney W. Grimes 400df8bae1dSRodney W. Grimes /* case 1. */ 401df8bae1dSRodney W. Grimes if ((uerror != 0) && (lerror != 0)) { 402df8bae1dSRodney W. Grimes return (uerror); 403df8bae1dSRodney W. Grimes } 404df8bae1dSRodney W. Grimes 405df8bae1dSRodney W. Grimes /* case 2. */ 406df8bae1dSRodney W. Grimes if (uerror != 0 /* && (lerror == 0) */ ) { 407df8bae1dSRodney W. Grimes if (lowervp->v_type == VDIR) { /* case 2b. */ 408df8bae1dSRodney W. Grimes dun->un_flags &= ~UN_ULOCK; 409996c772fSJohn Dyson VOP_UNLOCK(upperdvp, 0, p); 410df8bae1dSRodney W. Grimes uerror = union_mkshadow(um, upperdvp, cnp, &uppervp); 411996c772fSJohn Dyson vn_lock(upperdvp, LK_EXCLUSIVE | LK_RETRY, p); 412df8bae1dSRodney W. Grimes dun->un_flags |= UN_ULOCK; 413df8bae1dSRodney W. Grimes 414df8bae1dSRodney W. Grimes if (uerror) { 415996c772fSJohn Dyson if (lowervp != NULLVP) { 416df8bae1dSRodney W. Grimes vput(lowervp); 417df8bae1dSRodney W. Grimes lowervp = NULLVP; 418df8bae1dSRodney W. Grimes } 419df8bae1dSRodney W. Grimes return (uerror); 420df8bae1dSRodney W. Grimes } 421df8bae1dSRodney W. Grimes } 422df8bae1dSRodney W. Grimes } 423df8bae1dSRodney W. Grimes 424996c772fSJohn Dyson if (lowervp != NULLVP) 425996c772fSJohn Dyson VOP_UNLOCK(lowervp, 0, p); 426df8bae1dSRodney W. Grimes 427df8bae1dSRodney W. Grimes error = union_allocvp(ap->a_vpp, dvp->v_mount, dvp, upperdvp, cnp, 428996c772fSJohn Dyson uppervp, lowervp, 1); 429df8bae1dSRodney W. Grimes 430df8bae1dSRodney W. Grimes if (error) { 431996c772fSJohn Dyson if (uppervp != NULLVP) 432df8bae1dSRodney W. Grimes vput(uppervp); 433996c772fSJohn Dyson if (lowervp != NULLVP) 434df8bae1dSRodney W. Grimes vrele(lowervp); 435df8bae1dSRodney W. Grimes } else { 436df8bae1dSRodney W. Grimes if (*ap->a_vpp != dvp) 437df8bae1dSRodney W. Grimes if (!lockparent || !(cnp->cn_flags & ISLASTCN)) 438996c772fSJohn Dyson VOP_UNLOCK(dvp, 0, p); 439df8bae1dSRodney W. Grimes } 440df8bae1dSRodney W. Grimes 441df8bae1dSRodney W. Grimes return (error); 442df8bae1dSRodney W. Grimes } 443df8bae1dSRodney W. Grimes 444df8bae1dSRodney W. Grimes int 445df8bae1dSRodney W. Grimes union_create(ap) 446df8bae1dSRodney W. Grimes struct vop_create_args /* { 447df8bae1dSRodney W. Grimes struct vnode *a_dvp; 448df8bae1dSRodney W. Grimes struct vnode **a_vpp; 449df8bae1dSRodney W. Grimes struct componentname *a_cnp; 450df8bae1dSRodney W. Grimes struct vattr *a_vap; 451df8bae1dSRodney W. Grimes } */ *ap; 452df8bae1dSRodney W. Grimes { 453df8bae1dSRodney W. Grimes struct union_node *un = VTOUNION(ap->a_dvp); 454df8bae1dSRodney W. Grimes struct vnode *dvp = un->un_uppervp; 455996c772fSJohn Dyson struct componentname *cnp = ap->a_cnp; 456996c772fSJohn Dyson struct proc *p = cnp->cn_proc; 457df8bae1dSRodney W. Grimes 458996c772fSJohn Dyson if (dvp != NULLVP) { 459df8bae1dSRodney W. Grimes int error; 460df8bae1dSRodney W. Grimes struct vnode *vp; 461996c772fSJohn Dyson struct mount *mp; 462df8bae1dSRodney W. Grimes 463996c772fSJohn Dyson FIXUP(un, p); 464df8bae1dSRodney W. Grimes 465df8bae1dSRodney W. Grimes VREF(dvp); 466a9320ff3SKATO Takenori SETKLOCK(un); 467996c772fSJohn Dyson mp = ap->a_dvp->v_mount; 468df8bae1dSRodney W. Grimes vput(ap->a_dvp); 469a9320ff3SKATO Takenori CLEARKLOCK(un); 470996c772fSJohn Dyson error = VOP_CREATE(dvp, &vp, cnp, ap->a_vap); 471df8bae1dSRodney W. Grimes if (error) 472df8bae1dSRodney W. Grimes return (error); 473df8bae1dSRodney W. Grimes 474996c772fSJohn Dyson error = union_allocvp(ap->a_vpp, mp, NULLVP, NULLVP, cnp, vp, 475996c772fSJohn Dyson NULLVP, 1); 476df8bae1dSRodney W. Grimes if (error) 477df8bae1dSRodney W. Grimes vput(vp); 478df8bae1dSRodney W. Grimes return (error); 479df8bae1dSRodney W. Grimes } 480df8bae1dSRodney W. Grimes 481df8bae1dSRodney W. Grimes vput(ap->a_dvp); 482df8bae1dSRodney W. Grimes return (EROFS); 483df8bae1dSRodney W. Grimes } 484df8bae1dSRodney W. Grimes 485df8bae1dSRodney W. Grimes int 486996c772fSJohn Dyson union_whiteout(ap) 487996c772fSJohn Dyson struct vop_whiteout_args /* { 488996c772fSJohn Dyson struct vnode *a_dvp; 489996c772fSJohn Dyson struct componentname *a_cnp; 490996c772fSJohn Dyson int a_flags; 491996c772fSJohn Dyson } */ *ap; 492996c772fSJohn Dyson { 493996c772fSJohn Dyson struct union_node *un = VTOUNION(ap->a_dvp); 494996c772fSJohn Dyson struct componentname *cnp = ap->a_cnp; 495996c772fSJohn Dyson struct proc *p = cnp->cn_proc; 496996c772fSJohn Dyson 497996c772fSJohn Dyson if (un->un_uppervp == NULLVP) 498996c772fSJohn Dyson return (EOPNOTSUPP); 499996c772fSJohn Dyson 500996c772fSJohn Dyson FIXUP(un, p); 501996c772fSJohn Dyson return (VOP_WHITEOUT(un->un_uppervp, cnp, ap->a_flags)); 502996c772fSJohn Dyson } 503996c772fSJohn Dyson 504996c772fSJohn Dyson int 505df8bae1dSRodney W. Grimes union_mknod(ap) 506df8bae1dSRodney W. Grimes struct vop_mknod_args /* { 507df8bae1dSRodney W. Grimes struct vnode *a_dvp; 508df8bae1dSRodney W. Grimes struct vnode **a_vpp; 509df8bae1dSRodney W. Grimes struct componentname *a_cnp; 510df8bae1dSRodney W. Grimes struct vattr *a_vap; 511df8bae1dSRodney W. Grimes } */ *ap; 512df8bae1dSRodney W. Grimes { 513df8bae1dSRodney W. Grimes struct union_node *un = VTOUNION(ap->a_dvp); 514df8bae1dSRodney W. Grimes struct vnode *dvp = un->un_uppervp; 515996c772fSJohn Dyson struct componentname *cnp = ap->a_cnp; 516996c772fSJohn Dyson struct proc *p = cnp->cn_proc; 517df8bae1dSRodney W. Grimes 518996c772fSJohn Dyson if (dvp != NULLVP) { 519df8bae1dSRodney W. Grimes int error; 520df8bae1dSRodney W. Grimes struct vnode *vp; 521996c772fSJohn Dyson struct mount *mp; 522df8bae1dSRodney W. Grimes 523996c772fSJohn Dyson FIXUP(un, p); 524df8bae1dSRodney W. Grimes 525df8bae1dSRodney W. Grimes VREF(dvp); 526a9320ff3SKATO Takenori SETKLOCK(un); 527996c772fSJohn Dyson mp = ap->a_dvp->v_mount; 528df8bae1dSRodney W. Grimes vput(ap->a_dvp); 529a9320ff3SKATO Takenori CLEARKLOCK(un); 530996c772fSJohn Dyson error = VOP_MKNOD(dvp, &vp, cnp, ap->a_vap); 531df8bae1dSRodney W. Grimes if (error) 532df8bae1dSRodney W. Grimes return (error); 533df8bae1dSRodney W. Grimes 534996c772fSJohn Dyson if (vp != NULLVP) { 535996c772fSJohn Dyson error = union_allocvp(ap->a_vpp, mp, NULLVP, NULLVP, 536996c772fSJohn Dyson cnp, vp, NULLVP, 1); 537df8bae1dSRodney W. Grimes if (error) 538df8bae1dSRodney W. Grimes vput(vp); 539df8bae1dSRodney W. Grimes } 540df8bae1dSRodney W. Grimes return (error); 541df8bae1dSRodney W. Grimes } 542df8bae1dSRodney W. Grimes 543df8bae1dSRodney W. Grimes vput(ap->a_dvp); 544df8bae1dSRodney W. Grimes return (EROFS); 545df8bae1dSRodney W. Grimes } 546df8bae1dSRodney W. Grimes 547df8bae1dSRodney W. Grimes int 548df8bae1dSRodney W. Grimes union_open(ap) 549df8bae1dSRodney W. Grimes struct vop_open_args /* { 550df8bae1dSRodney W. Grimes struct vnodeop_desc *a_desc; 551df8bae1dSRodney W. Grimes struct vnode *a_vp; 552df8bae1dSRodney W. Grimes int a_mode; 553df8bae1dSRodney W. Grimes struct ucred *a_cred; 554df8bae1dSRodney W. Grimes struct proc *a_p; 555df8bae1dSRodney W. Grimes } */ *ap; 556df8bae1dSRodney W. Grimes { 557df8bae1dSRodney W. Grimes struct union_node *un = VTOUNION(ap->a_vp); 558df8bae1dSRodney W. Grimes struct vnode *tvp; 559df8bae1dSRodney W. Grimes int mode = ap->a_mode; 560df8bae1dSRodney W. Grimes struct ucred *cred = ap->a_cred; 561df8bae1dSRodney W. Grimes struct proc *p = ap->a_p; 562df8bae1dSRodney W. Grimes int error; 563df8bae1dSRodney W. Grimes 564df8bae1dSRodney W. Grimes /* 565df8bae1dSRodney W. Grimes * If there is an existing upper vp then simply open that. 566df8bae1dSRodney W. Grimes */ 567df8bae1dSRodney W. Grimes tvp = un->un_uppervp; 568df8bae1dSRodney W. Grimes if (tvp == NULLVP) { 569df8bae1dSRodney W. Grimes /* 570df8bae1dSRodney W. Grimes * If the lower vnode is being opened for writing, then 571df8bae1dSRodney W. Grimes * copy the file contents to the upper vnode and open that, 572df8bae1dSRodney W. Grimes * otherwise can simply open the lower vnode. 573df8bae1dSRodney W. Grimes */ 574df8bae1dSRodney W. Grimes tvp = un->un_lowervp; 575df8bae1dSRodney W. Grimes if ((ap->a_mode & FWRITE) && (tvp->v_type == VREG)) { 576996c772fSJohn Dyson error = union_copyup(un, (mode&O_TRUNC) == 0, cred, p); 577df8bae1dSRodney W. Grimes if (error == 0) 578df8bae1dSRodney W. Grimes error = VOP_OPEN(un->un_uppervp, mode, cred, p); 579df8bae1dSRodney W. Grimes return (error); 580df8bae1dSRodney W. Grimes } 581df8bae1dSRodney W. Grimes 582df8bae1dSRodney W. Grimes /* 583df8bae1dSRodney W. Grimes * Just open the lower vnode 584df8bae1dSRodney W. Grimes */ 585df8bae1dSRodney W. Grimes un->un_openl++; 586996c772fSJohn Dyson vn_lock(tvp, LK_EXCLUSIVE | LK_RETRY, p); 587df8bae1dSRodney W. Grimes error = VOP_OPEN(tvp, mode, cred, p); 588996c772fSJohn Dyson VOP_UNLOCK(tvp, 0, p); 589df8bae1dSRodney W. Grimes 590df8bae1dSRodney W. Grimes return (error); 591df8bae1dSRodney W. Grimes } 592df8bae1dSRodney W. Grimes 593996c772fSJohn Dyson FIXUP(un, p); 594df8bae1dSRodney W. Grimes 595df8bae1dSRodney W. Grimes error = VOP_OPEN(tvp, mode, cred, p); 596df8bae1dSRodney W. Grimes 597df8bae1dSRodney W. Grimes return (error); 598df8bae1dSRodney W. Grimes } 599df8bae1dSRodney W. Grimes 600df8bae1dSRodney W. Grimes int 601df8bae1dSRodney W. Grimes union_close(ap) 602df8bae1dSRodney W. Grimes struct vop_close_args /* { 603df8bae1dSRodney W. Grimes struct vnode *a_vp; 604df8bae1dSRodney W. Grimes int a_fflag; 605df8bae1dSRodney W. Grimes struct ucred *a_cred; 606df8bae1dSRodney W. Grimes struct proc *a_p; 607df8bae1dSRodney W. Grimes } */ *ap; 608df8bae1dSRodney W. Grimes { 609df8bae1dSRodney W. Grimes struct union_node *un = VTOUNION(ap->a_vp); 610df8bae1dSRodney W. Grimes struct vnode *vp; 611df8bae1dSRodney W. Grimes 612996c772fSJohn Dyson if ((vp = un->un_uppervp) == NULLVP) { 613df8bae1dSRodney W. Grimes #ifdef UNION_DIAGNOSTIC 614df8bae1dSRodney W. Grimes if (un->un_openl <= 0) 615df8bae1dSRodney W. Grimes panic("union: un_openl cnt"); 616df8bae1dSRodney W. Grimes #endif 617df8bae1dSRodney W. Grimes --un->un_openl; 618df8bae1dSRodney W. Grimes vp = un->un_lowervp; 619df8bae1dSRodney W. Grimes } 620df8bae1dSRodney W. Grimes 621996c772fSJohn Dyson ap->a_vp = vp; 622996c772fSJohn Dyson return (VCALL(vp, VOFFSET(vop_close), ap)); 623df8bae1dSRodney W. Grimes } 624df8bae1dSRodney W. Grimes 625df8bae1dSRodney W. Grimes /* 626df8bae1dSRodney W. Grimes * Check access permission on the union vnode. 627df8bae1dSRodney W. Grimes * The access check being enforced is to check 628df8bae1dSRodney W. Grimes * against both the underlying vnode, and any 629df8bae1dSRodney W. Grimes * copied vnode. This ensures that no additional 630df8bae1dSRodney W. Grimes * file permissions are given away simply because 631df8bae1dSRodney W. Grimes * the user caused an implicit file copy. 632df8bae1dSRodney W. Grimes */ 633df8bae1dSRodney W. Grimes int 634df8bae1dSRodney W. Grimes union_access(ap) 635df8bae1dSRodney W. Grimes struct vop_access_args /* { 636df8bae1dSRodney W. Grimes struct vnodeop_desc *a_desc; 637df8bae1dSRodney W. Grimes struct vnode *a_vp; 638df8bae1dSRodney W. Grimes int a_mode; 639df8bae1dSRodney W. Grimes struct ucred *a_cred; 640df8bae1dSRodney W. Grimes struct proc *a_p; 641df8bae1dSRodney W. Grimes } */ *ap; 642df8bae1dSRodney W. Grimes { 643df8bae1dSRodney W. Grimes struct union_node *un = VTOUNION(ap->a_vp); 644996c772fSJohn Dyson struct proc *p = ap->a_p; 645df8bae1dSRodney W. Grimes int error = EACCES; 646df8bae1dSRodney W. Grimes struct vnode *vp; 647df8bae1dSRodney W. Grimes 648996c772fSJohn Dyson if ((vp = un->un_uppervp) != NULLVP) { 649996c772fSJohn Dyson FIXUP(un, p); 650996c772fSJohn Dyson ap->a_vp = vp; 651996c772fSJohn Dyson return (VCALL(vp, VOFFSET(vop_access), ap)); 652df8bae1dSRodney W. Grimes } 653df8bae1dSRodney W. Grimes 654996c772fSJohn Dyson if ((vp = un->un_lowervp) != NULLVP) { 655996c772fSJohn Dyson vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); 656996c772fSJohn Dyson ap->a_vp = vp; 657996c772fSJohn Dyson error = VCALL(vp, VOFFSET(vop_access), ap); 658df8bae1dSRodney W. Grimes if (error == 0) { 6593a946335SKATO Takenori struct union_mount *um = MOUNTTOUNIONMOUNT(ap->a_vp->v_mount); 660df8bae1dSRodney W. Grimes 661996c772fSJohn Dyson if (um->um_op == UNMNT_BELOW) { 662996c772fSJohn Dyson ap->a_cred = um->um_cred; 663996c772fSJohn Dyson error = VCALL(vp, VOFFSET(vop_access), ap); 664df8bae1dSRodney W. Grimes } 665996c772fSJohn Dyson } 666996c772fSJohn Dyson VOP_UNLOCK(vp, 0, p); 667df8bae1dSRodney W. Grimes if (error) 668df8bae1dSRodney W. Grimes return (error); 669df8bae1dSRodney W. Grimes } 670df8bae1dSRodney W. Grimes 671df8bae1dSRodney W. Grimes return (error); 672df8bae1dSRodney W. Grimes } 673df8bae1dSRodney W. Grimes 674df8bae1dSRodney W. Grimes /* 675996c772fSJohn Dyson * We handle getattr only to change the fsid and 676996c772fSJohn Dyson * track object sizes 677df8bae1dSRodney W. Grimes */ 678df8bae1dSRodney W. Grimes int 679df8bae1dSRodney W. Grimes union_getattr(ap) 680df8bae1dSRodney W. Grimes struct vop_getattr_args /* { 681df8bae1dSRodney W. Grimes struct vnode *a_vp; 682df8bae1dSRodney W. Grimes struct vattr *a_vap; 683df8bae1dSRodney W. Grimes struct ucred *a_cred; 684df8bae1dSRodney W. Grimes struct proc *a_p; 685df8bae1dSRodney W. Grimes } */ *ap; 686df8bae1dSRodney W. Grimes { 687df8bae1dSRodney W. Grimes int error; 688df8bae1dSRodney W. Grimes struct union_node *un = VTOUNION(ap->a_vp); 689df8bae1dSRodney W. Grimes struct vnode *vp = un->un_uppervp; 690996c772fSJohn Dyson struct proc *p = ap->a_p; 691df8bae1dSRodney W. Grimes struct vattr *vap; 692df8bae1dSRodney W. Grimes struct vattr va; 693df8bae1dSRodney W. Grimes 694df8bae1dSRodney W. Grimes 695df8bae1dSRodney W. Grimes /* 696df8bae1dSRodney W. Grimes * Some programs walk the filesystem hierarchy by counting 697df8bae1dSRodney W. Grimes * links to directories to avoid stat'ing all the time. 698df8bae1dSRodney W. Grimes * This means the link count on directories needs to be "correct". 699df8bae1dSRodney W. Grimes * The only way to do that is to call getattr on both layers 700df8bae1dSRodney W. Grimes * and fix up the link count. The link count will not necessarily 701df8bae1dSRodney W. Grimes * be accurate but will be large enough to defeat the tree walkers. 702df8bae1dSRodney W. Grimes */ 703df8bae1dSRodney W. Grimes 704df8bae1dSRodney W. Grimes vap = ap->a_vap; 705df8bae1dSRodney W. Grimes 706df8bae1dSRodney W. Grimes vp = un->un_uppervp; 707df8bae1dSRodney W. Grimes if (vp != NULLVP) { 708996c772fSJohn Dyson /* 709996c772fSJohn Dyson * It's not clear whether VOP_GETATTR is to be 710996c772fSJohn Dyson * called with the vnode locked or not. stat() calls 711996c772fSJohn Dyson * it with (vp) locked, and fstat calls it with 712996c772fSJohn Dyson * (vp) unlocked. 713996c772fSJohn Dyson * In the mean time, compensate here by checking 714996c772fSJohn Dyson * the union_node's lock flag. 715996c772fSJohn Dyson */ 716996c772fSJohn Dyson if (un->un_flags & UN_LOCKED) 717996c772fSJohn Dyson FIXUP(un, p); 718996c772fSJohn Dyson 719df8bae1dSRodney W. Grimes error = VOP_GETATTR(vp, vap, ap->a_cred, ap->a_p); 720df8bae1dSRodney W. Grimes if (error) 721df8bae1dSRodney W. Grimes return (error); 722996c772fSJohn Dyson union_newsize(ap->a_vp, vap->va_size, VNOVAL); 723df8bae1dSRodney W. Grimes } 724df8bae1dSRodney W. Grimes 725df8bae1dSRodney W. Grimes if (vp == NULLVP) { 726df8bae1dSRodney W. Grimes vp = un->un_lowervp; 727df8bae1dSRodney W. Grimes } else if (vp->v_type == VDIR) { 728df8bae1dSRodney W. Grimes vp = un->un_lowervp; 729df8bae1dSRodney W. Grimes vap = &va; 730df8bae1dSRodney W. Grimes } else { 731df8bae1dSRodney W. Grimes vp = NULLVP; 732df8bae1dSRodney W. Grimes } 733df8bae1dSRodney W. Grimes 734df8bae1dSRodney W. Grimes if (vp != NULLVP) { 735df8bae1dSRodney W. Grimes error = VOP_GETATTR(vp, vap, ap->a_cred, ap->a_p); 736df8bae1dSRodney W. Grimes if (error) 737df8bae1dSRodney W. Grimes return (error); 738996c772fSJohn Dyson union_newsize(ap->a_vp, VNOVAL, vap->va_size); 739df8bae1dSRodney W. Grimes } 740df8bae1dSRodney W. Grimes 741df8bae1dSRodney W. Grimes if ((vap != ap->a_vap) && (vap->va_type == VDIR)) 742df8bae1dSRodney W. Grimes ap->a_vap->va_nlink += vap->va_nlink; 743df8bae1dSRodney W. Grimes 744996c772fSJohn Dyson ap->a_vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsid.val[0]; 745df8bae1dSRodney W. Grimes return (0); 746df8bae1dSRodney W. Grimes } 747df8bae1dSRodney W. Grimes 748df8bae1dSRodney W. Grimes int 749df8bae1dSRodney W. Grimes union_setattr(ap) 750df8bae1dSRodney W. Grimes struct vop_setattr_args /* { 751df8bae1dSRodney W. Grimes struct vnode *a_vp; 752df8bae1dSRodney W. Grimes struct vattr *a_vap; 753df8bae1dSRodney W. Grimes struct ucred *a_cred; 754df8bae1dSRodney W. Grimes struct proc *a_p; 755df8bae1dSRodney W. Grimes } */ *ap; 756df8bae1dSRodney W. Grimes { 757df8bae1dSRodney W. Grimes struct union_node *un = VTOUNION(ap->a_vp); 758996c772fSJohn Dyson struct proc *p = ap->a_p; 759df8bae1dSRodney W. Grimes int error; 760df8bae1dSRodney W. Grimes 761df8bae1dSRodney W. Grimes /* 762df8bae1dSRodney W. Grimes * Handle case of truncating lower object to zero size, 763df8bae1dSRodney W. Grimes * by creating a zero length upper object. This is to 764df8bae1dSRodney W. Grimes * handle the case of open with O_TRUNC and O_CREAT. 765df8bae1dSRodney W. Grimes */ 766df8bae1dSRodney W. Grimes if ((un->un_uppervp == NULLVP) && 767df8bae1dSRodney W. Grimes /* assert(un->un_lowervp != NULLVP) */ 768996c772fSJohn Dyson (un->un_lowervp->v_type == VREG)) { 769996c772fSJohn Dyson error = union_copyup(un, (ap->a_vap->va_size != 0), 770996c772fSJohn Dyson ap->a_cred, ap->a_p); 771df8bae1dSRodney W. Grimes if (error) 772df8bae1dSRodney W. Grimes return (error); 773df8bae1dSRodney W. Grimes } 774df8bae1dSRodney W. Grimes 775df8bae1dSRodney W. Grimes /* 776df8bae1dSRodney W. Grimes * Try to set attributes in upper layer, 777df8bae1dSRodney W. Grimes * otherwise return read-only filesystem error. 778df8bae1dSRodney W. Grimes */ 779df8bae1dSRodney W. Grimes if (un->un_uppervp != NULLVP) { 780996c772fSJohn Dyson FIXUP(un, p); 781df8bae1dSRodney W. Grimes error = VOP_SETATTR(un->un_uppervp, ap->a_vap, 782df8bae1dSRodney W. Grimes ap->a_cred, ap->a_p); 783996c772fSJohn Dyson if ((error == 0) && (ap->a_vap->va_size != VNOVAL)) 784996c772fSJohn Dyson union_newsize(ap->a_vp, ap->a_vap->va_size, VNOVAL); 785df8bae1dSRodney W. Grimes } else { 786df8bae1dSRodney W. Grimes error = EROFS; 787df8bae1dSRodney W. Grimes } 788df8bae1dSRodney W. Grimes 789df8bae1dSRodney W. Grimes return (error); 790df8bae1dSRodney W. Grimes } 791df8bae1dSRodney W. Grimes 792df8bae1dSRodney W. Grimes int 793df8bae1dSRodney W. Grimes union_read(ap) 794df8bae1dSRodney W. Grimes struct vop_read_args /* { 795df8bae1dSRodney W. Grimes struct vnode *a_vp; 796df8bae1dSRodney W. Grimes struct uio *a_uio; 797df8bae1dSRodney W. Grimes int a_ioflag; 798df8bae1dSRodney W. Grimes struct ucred *a_cred; 799df8bae1dSRodney W. Grimes } */ *ap; 800df8bae1dSRodney W. Grimes { 801df8bae1dSRodney W. Grimes int error; 802996c772fSJohn Dyson struct proc *p = ap->a_uio->uio_procp; 803df8bae1dSRodney W. Grimes struct vnode *vp = OTHERVP(ap->a_vp); 804df8bae1dSRodney W. Grimes int dolock = (vp == LOWERVP(ap->a_vp)); 805df8bae1dSRodney W. Grimes 806df8bae1dSRodney W. Grimes if (dolock) 807996c772fSJohn Dyson vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); 808df8bae1dSRodney W. Grimes else 809996c772fSJohn Dyson FIXUP(VTOUNION(ap->a_vp), p); 810df8bae1dSRodney W. Grimes error = VOP_READ(vp, ap->a_uio, ap->a_ioflag, ap->a_cred); 811df8bae1dSRodney W. Grimes if (dolock) 812996c772fSJohn Dyson VOP_UNLOCK(vp, 0, p); 813996c772fSJohn Dyson 814996c772fSJohn Dyson /* 815996c772fSJohn Dyson * XXX 816996c772fSJohn Dyson * perhaps the size of the underlying object has changed under 817996c772fSJohn Dyson * our feet. take advantage of the offset information present 818996c772fSJohn Dyson * in the uio structure. 819996c772fSJohn Dyson */ 820996c772fSJohn Dyson if (error == 0) { 821996c772fSJohn Dyson struct union_node *un = VTOUNION(ap->a_vp); 822996c772fSJohn Dyson off_t cur = ap->a_uio->uio_offset; 823996c772fSJohn Dyson 824996c772fSJohn Dyson if (vp == un->un_uppervp) { 825996c772fSJohn Dyson if (cur > un->un_uppersz) 826996c772fSJohn Dyson union_newsize(ap->a_vp, cur, VNOVAL); 827996c772fSJohn Dyson } else { 828996c772fSJohn Dyson if (cur > un->un_lowersz) 829996c772fSJohn Dyson union_newsize(ap->a_vp, VNOVAL, cur); 830996c772fSJohn Dyson } 831996c772fSJohn Dyson } 832df8bae1dSRodney W. Grimes 833df8bae1dSRodney W. Grimes return (error); 834df8bae1dSRodney W. Grimes } 835df8bae1dSRodney W. Grimes 836df8bae1dSRodney W. Grimes int 837df8bae1dSRodney W. Grimes union_write(ap) 838df8bae1dSRodney W. Grimes struct vop_read_args /* { 839df8bae1dSRodney W. Grimes struct vnode *a_vp; 840df8bae1dSRodney W. Grimes struct uio *a_uio; 841df8bae1dSRodney W. Grimes int a_ioflag; 842df8bae1dSRodney W. Grimes struct ucred *a_cred; 843df8bae1dSRodney W. Grimes } */ *ap; 844df8bae1dSRodney W. Grimes { 845df8bae1dSRodney W. Grimes int error; 846996c772fSJohn Dyson struct vnode *vp; 847996c772fSJohn Dyson struct union_node *un = VTOUNION(ap->a_vp); 848996c772fSJohn Dyson struct proc *p = ap->a_uio->uio_procp; 849df8bae1dSRodney W. Grimes 850996c772fSJohn Dyson vp = UPPERVP(ap->a_vp); 851996c772fSJohn Dyson if (vp == NULLVP) 852996c772fSJohn Dyson panic("union: missing upper layer in write"); 853996c772fSJohn Dyson 854996c772fSJohn Dyson FIXUP(un, p); 855df8bae1dSRodney W. Grimes error = VOP_WRITE(vp, ap->a_uio, ap->a_ioflag, ap->a_cred); 856996c772fSJohn Dyson 857996c772fSJohn Dyson /* 858996c772fSJohn Dyson * the size of the underlying object may be changed by the 859996c772fSJohn Dyson * write. 860996c772fSJohn Dyson */ 861996c772fSJohn Dyson if (error == 0) { 862996c772fSJohn Dyson off_t cur = ap->a_uio->uio_offset; 863996c772fSJohn Dyson 864996c772fSJohn Dyson if (cur > un->un_uppersz) 865996c772fSJohn Dyson union_newsize(ap->a_vp, cur, VNOVAL); 866996c772fSJohn Dyson } 867df8bae1dSRodney W. Grimes 868df8bae1dSRodney W. Grimes return (error); 869df8bae1dSRodney W. Grimes } 870df8bae1dSRodney W. Grimes 871df8bae1dSRodney W. Grimes int 872996c772fSJohn Dyson union_lease(ap) 873996c772fSJohn Dyson struct vop_lease_args /* { 874996c772fSJohn Dyson struct vnode *a_vp; 875996c772fSJohn Dyson struct proc *a_p; 876996c772fSJohn Dyson struct ucred *a_cred; 877996c772fSJohn Dyson int a_flag; 878996c772fSJohn Dyson } */ *ap; 879996c772fSJohn Dyson { 880996c772fSJohn Dyson register struct vnode *ovp = OTHERVP(ap->a_vp); 881996c772fSJohn Dyson 882996c772fSJohn Dyson ap->a_vp = ovp; 883996c772fSJohn Dyson return (VCALL(ovp, VOFFSET(vop_lease), ap)); 884996c772fSJohn Dyson } 885996c772fSJohn Dyson 886996c772fSJohn Dyson int 887df8bae1dSRodney W. Grimes union_ioctl(ap) 888df8bae1dSRodney W. Grimes struct vop_ioctl_args /* { 889df8bae1dSRodney W. Grimes struct vnode *a_vp; 890df8bae1dSRodney W. Grimes int a_command; 891df8bae1dSRodney W. Grimes caddr_t a_data; 892df8bae1dSRodney W. Grimes int a_fflag; 893df8bae1dSRodney W. Grimes struct ucred *a_cred; 894df8bae1dSRodney W. Grimes struct proc *a_p; 895df8bae1dSRodney W. Grimes } */ *ap; 896df8bae1dSRodney W. Grimes { 897996c772fSJohn Dyson register struct vnode *ovp = OTHERVP(ap->a_vp); 898df8bae1dSRodney W. Grimes 899996c772fSJohn Dyson ap->a_vp = ovp; 900996c772fSJohn Dyson return (VCALL(ovp, VOFFSET(vop_ioctl), ap)); 901df8bae1dSRodney W. Grimes } 902df8bae1dSRodney W. Grimes 903df8bae1dSRodney W. Grimes int 904df8bae1dSRodney W. Grimes union_select(ap) 905df8bae1dSRodney W. Grimes struct vop_select_args /* { 906df8bae1dSRodney W. Grimes struct vnode *a_vp; 907df8bae1dSRodney W. Grimes int a_which; 908df8bae1dSRodney W. Grimes int a_fflags; 909df8bae1dSRodney W. Grimes struct ucred *a_cred; 910df8bae1dSRodney W. Grimes struct proc *a_p; 911df8bae1dSRodney W. Grimes } */ *ap; 912df8bae1dSRodney W. Grimes { 913996c772fSJohn Dyson register struct vnode *ovp = OTHERVP(ap->a_vp); 914df8bae1dSRodney W. Grimes 915996c772fSJohn Dyson ap->a_vp = ovp; 916996c772fSJohn Dyson return (VCALL(ovp, VOFFSET(vop_select), ap)); 917996c772fSJohn Dyson } 918996c772fSJohn Dyson 919996c772fSJohn Dyson int 920996c772fSJohn Dyson union_revoke(ap) 921996c772fSJohn Dyson struct vop_revoke_args /* { 922996c772fSJohn Dyson struct vnode *a_vp; 923996c772fSJohn Dyson int a_flags; 924996c772fSJohn Dyson struct proc *a_p; 925996c772fSJohn Dyson } */ *ap; 926996c772fSJohn Dyson { 927996c772fSJohn Dyson struct vnode *vp = ap->a_vp; 928996c772fSJohn Dyson 929996c772fSJohn Dyson if (UPPERVP(vp)) 930996c772fSJohn Dyson VOP_REVOKE(UPPERVP(vp), ap->a_flags); 931996c772fSJohn Dyson if (LOWERVP(vp)) 932996c772fSJohn Dyson VOP_REVOKE(LOWERVP(vp), ap->a_flags); 933996c772fSJohn Dyson vgone(vp); 934996c772fSJohn Dyson return (0); 935df8bae1dSRodney W. Grimes } 936df8bae1dSRodney W. Grimes 937df8bae1dSRodney W. Grimes int 938df8bae1dSRodney W. Grimes union_mmap(ap) 939df8bae1dSRodney W. Grimes struct vop_mmap_args /* { 940df8bae1dSRodney W. Grimes struct vnode *a_vp; 941df8bae1dSRodney W. Grimes int a_fflags; 942df8bae1dSRodney W. Grimes struct ucred *a_cred; 943df8bae1dSRodney W. Grimes struct proc *a_p; 944df8bae1dSRodney W. Grimes } */ *ap; 945df8bae1dSRodney W. Grimes { 946996c772fSJohn Dyson register struct vnode *ovp = OTHERVP(ap->a_vp); 947df8bae1dSRodney W. Grimes 948996c772fSJohn Dyson ap->a_vp = ovp; 949996c772fSJohn Dyson return (VCALL(ovp, VOFFSET(vop_mmap), ap)); 950df8bae1dSRodney W. Grimes } 951df8bae1dSRodney W. Grimes 952df8bae1dSRodney W. Grimes int 953df8bae1dSRodney W. Grimes union_fsync(ap) 954df8bae1dSRodney W. Grimes struct vop_fsync_args /* { 955df8bae1dSRodney W. Grimes struct vnode *a_vp; 956df8bae1dSRodney W. Grimes struct ucred *a_cred; 957df8bae1dSRodney W. Grimes int a_waitfor; 958df8bae1dSRodney W. Grimes struct proc *a_p; 959df8bae1dSRodney W. Grimes } */ *ap; 960df8bae1dSRodney W. Grimes { 961df8bae1dSRodney W. Grimes int error = 0; 962996c772fSJohn Dyson struct proc *p = ap->a_p; 963df8bae1dSRodney W. Grimes struct vnode *targetvp = OTHERVP(ap->a_vp); 964363ad700SPoul-Henning Kamp struct union_node *un; 965363ad700SPoul-Henning Kamp int isupperlocked = 0; 966df8bae1dSRodney W. Grimes 967996c772fSJohn Dyson if (targetvp != NULLVP) { 968df8bae1dSRodney W. Grimes int dolock = (targetvp == LOWERVP(ap->a_vp)); 969df8bae1dSRodney W. Grimes 970363ad700SPoul-Henning Kamp un = VTOUNION(ap->a_vp); 971df8bae1dSRodney W. Grimes if (dolock) 972996c772fSJohn Dyson vn_lock(targetvp, LK_EXCLUSIVE | LK_RETRY, p); 973363ad700SPoul-Henning Kamp else if ((un->un_flags & UN_ULOCK) == 0 && 974363ad700SPoul-Henning Kamp VOP_ISLOCKED(targetvp) == 0) { 975363ad700SPoul-Henning Kamp isupperlocked = 1; 976363ad700SPoul-Henning Kamp vn_lock(targetvp, LK_EXCLUSIVE | LK_RETRY, p); 977363ad700SPoul-Henning Kamp un->un_flags |= UN_ULOCK; 978363ad700SPoul-Henning Kamp } 979996c772fSJohn Dyson error = VOP_FSYNC(targetvp, ap->a_cred, ap->a_waitfor, p); 980df8bae1dSRodney W. Grimes if (dolock) 981996c772fSJohn Dyson VOP_UNLOCK(targetvp, 0, p); 982363ad700SPoul-Henning Kamp else if (isupperlocked) { 983363ad700SPoul-Henning Kamp un->un_flags &= ~UN_ULOCK; 984a9320ff3SKATO Takenori VOP_UNLOCK(targetvp, 0, p); 985363ad700SPoul-Henning Kamp } 986df8bae1dSRodney W. Grimes } 987df8bae1dSRodney W. Grimes 988df8bae1dSRodney W. Grimes return (error); 989df8bae1dSRodney W. Grimes } 990df8bae1dSRodney W. Grimes 991df8bae1dSRodney W. Grimes int 992df8bae1dSRodney W. Grimes union_seek(ap) 993df8bae1dSRodney W. Grimes struct vop_seek_args /* { 994df8bae1dSRodney W. Grimes struct vnode *a_vp; 995df8bae1dSRodney W. Grimes off_t a_oldoff; 996df8bae1dSRodney W. Grimes off_t a_newoff; 997df8bae1dSRodney W. Grimes struct ucred *a_cred; 998df8bae1dSRodney W. Grimes } */ *ap; 999df8bae1dSRodney W. Grimes { 1000996c772fSJohn Dyson register struct vnode *ovp = OTHERVP(ap->a_vp); 1001df8bae1dSRodney W. Grimes 1002996c772fSJohn Dyson ap->a_vp = ovp; 1003996c772fSJohn Dyson return (VCALL(ovp, VOFFSET(vop_seek), ap)); 1004df8bae1dSRodney W. Grimes } 1005df8bae1dSRodney W. Grimes 1006df8bae1dSRodney W. Grimes int 1007df8bae1dSRodney W. Grimes union_remove(ap) 1008df8bae1dSRodney W. Grimes struct vop_remove_args /* { 1009df8bae1dSRodney W. Grimes struct vnode *a_dvp; 1010df8bae1dSRodney W. Grimes struct vnode *a_vp; 1011df8bae1dSRodney W. Grimes struct componentname *a_cnp; 1012df8bae1dSRodney W. Grimes } */ *ap; 1013df8bae1dSRodney W. Grimes { 1014df8bae1dSRodney W. Grimes int error; 1015df8bae1dSRodney W. Grimes struct union_node *dun = VTOUNION(ap->a_dvp); 1016df8bae1dSRodney W. Grimes struct union_node *un = VTOUNION(ap->a_vp); 1017996c772fSJohn Dyson struct componentname *cnp = ap->a_cnp; 1018996c772fSJohn Dyson struct proc *p = cnp->cn_proc; 1019df8bae1dSRodney W. Grimes 1020996c772fSJohn Dyson if (dun->un_uppervp == NULLVP) 1021996c772fSJohn Dyson panic("union remove: null upper vnode"); 1022996c772fSJohn Dyson 1023996c772fSJohn Dyson if (un->un_uppervp != NULLVP) { 1024df8bae1dSRodney W. Grimes struct vnode *dvp = dun->un_uppervp; 1025df8bae1dSRodney W. Grimes struct vnode *vp = un->un_uppervp; 1026df8bae1dSRodney W. Grimes 1027996c772fSJohn Dyson FIXUP(dun, p); 1028df8bae1dSRodney W. Grimes VREF(dvp); 1029a9320ff3SKATO Takenori SETKLOCK(dun); 1030df8bae1dSRodney W. Grimes vput(ap->a_dvp); 1031a9320ff3SKATO Takenori CLEARKLOCK(dun); 1032996c772fSJohn Dyson FIXUP(un, p); 1033df8bae1dSRodney W. Grimes VREF(vp); 1034a9320ff3SKATO Takenori SETKLOCK(un); 1035df8bae1dSRodney W. Grimes vput(ap->a_vp); 1036a9320ff3SKATO Takenori CLEARKLOCK(un); 1037df8bae1dSRodney W. Grimes 1038996c772fSJohn Dyson if (union_dowhiteout(un, cnp->cn_cred, cnp->cn_proc)) 1039996c772fSJohn Dyson cnp->cn_flags |= DOWHITEOUT; 1040996c772fSJohn Dyson error = VOP_REMOVE(dvp, vp, cnp); 1041df8bae1dSRodney W. Grimes if (!error) 1042df8bae1dSRodney W. Grimes union_removed_upper(un); 1043df8bae1dSRodney W. Grimes } else { 1044996c772fSJohn Dyson FIXUP(dun, p); 1045996c772fSJohn Dyson error = union_mkwhiteout( 1046996c772fSJohn Dyson MOUNTTOUNIONMOUNT(UNIONTOV(dun)->v_mount), 1047996c772fSJohn Dyson dun->un_uppervp, ap->a_cnp, un->un_path); 1048df8bae1dSRodney W. Grimes vput(ap->a_dvp); 1049df8bae1dSRodney W. Grimes vput(ap->a_vp); 1050df8bae1dSRodney W. Grimes } 1051df8bae1dSRodney W. Grimes 1052df8bae1dSRodney W. Grimes return (error); 1053df8bae1dSRodney W. Grimes } 1054df8bae1dSRodney W. Grimes 1055df8bae1dSRodney W. Grimes int 1056df8bae1dSRodney W. Grimes union_link(ap) 1057df8bae1dSRodney W. Grimes struct vop_link_args /* { 1058df8bae1dSRodney W. Grimes struct vnode *a_tdvp; 105947777413SDavid Greenman struct vnode *a_vp; 1060df8bae1dSRodney W. Grimes struct componentname *a_cnp; 1061df8bae1dSRodney W. Grimes } */ *ap; 1062df8bae1dSRodney W. Grimes { 1063996c772fSJohn Dyson int error = 0; 1064996c772fSJohn Dyson struct componentname *cnp = ap->a_cnp; 1065996c772fSJohn Dyson struct proc *p = cnp->cn_proc; 1066996c772fSJohn Dyson struct union_node *un; 1067996c772fSJohn Dyson struct vnode *vp; 1068996c772fSJohn Dyson struct vnode *tdvp; 1069df8bae1dSRodney W. Grimes 1070996c772fSJohn Dyson un = VTOUNION(ap->a_tdvp); 1071df8bae1dSRodney W. Grimes 1072996c772fSJohn Dyson if (ap->a_tdvp->v_op != ap->a_vp->v_op) { 1073996c772fSJohn Dyson vp = ap->a_vp; 1074df8bae1dSRodney W. Grimes } else { 1075996c772fSJohn Dyson struct union_node *tun = VTOUNION(ap->a_vp); 1076996c772fSJohn Dyson if (tun->un_uppervp == NULLVP) { 1077996c772fSJohn Dyson vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY, p); 1078996c772fSJohn Dyson if (un->un_uppervp == tun->un_dirvp) { 1079996c772fSJohn Dyson un->un_flags &= ~UN_ULOCK; 1080996c772fSJohn Dyson VOP_UNLOCK(un->un_uppervp, 0, p); 1081996c772fSJohn Dyson } 1082996c772fSJohn Dyson error = union_copyup(tun, 1, cnp->cn_cred, p); 1083996c772fSJohn Dyson if (un->un_uppervp == tun->un_dirvp) { 1084996c772fSJohn Dyson vn_lock(un->un_uppervp, 1085996c772fSJohn Dyson LK_EXCLUSIVE | LK_RETRY, p); 1086996c772fSJohn Dyson un->un_flags |= UN_ULOCK; 1087996c772fSJohn Dyson } 1088996c772fSJohn Dyson VOP_UNLOCK(ap->a_vp, 0, p); 1089996c772fSJohn Dyson } 1090996c772fSJohn Dyson vp = tun->un_uppervp; 1091df8bae1dSRodney W. Grimes } 1092df8bae1dSRodney W. Grimes 1093996c772fSJohn Dyson tdvp = un->un_uppervp; 1094996c772fSJohn Dyson if (tdvp == NULLVP) 1095996c772fSJohn Dyson error = EROFS; 1096996c772fSJohn Dyson 1097996c772fSJohn Dyson if (error) { 1098996c772fSJohn Dyson vput(ap->a_tdvp); 1099df8bae1dSRodney W. Grimes return (error); 1100df8bae1dSRodney W. Grimes } 1101df8bae1dSRodney W. Grimes 1102996c772fSJohn Dyson FIXUP(un, p); 1103996c772fSJohn Dyson VREF(tdvp); 1104a9320ff3SKATO Takenori SETKLOCK(un); 1105996c772fSJohn Dyson vput(ap->a_tdvp); 1106a9320ff3SKATO Takenori CLEARKLOCK(un); 1107996c772fSJohn Dyson 110893d71a48SKATO Takenori return (VOP_LINK(tdvp, vp, cnp)); 1109996c772fSJohn Dyson } 1110996c772fSJohn Dyson 1111df8bae1dSRodney W. Grimes int 1112df8bae1dSRodney W. Grimes union_rename(ap) 1113df8bae1dSRodney W. Grimes struct vop_rename_args /* { 1114df8bae1dSRodney W. Grimes struct vnode *a_fdvp; 1115df8bae1dSRodney W. Grimes struct vnode *a_fvp; 1116df8bae1dSRodney W. Grimes struct componentname *a_fcnp; 1117df8bae1dSRodney W. Grimes struct vnode *a_tdvp; 1118df8bae1dSRodney W. Grimes struct vnode *a_tvp; 1119df8bae1dSRodney W. Grimes struct componentname *a_tcnp; 1120df8bae1dSRodney W. Grimes } */ *ap; 1121df8bae1dSRodney W. Grimes { 1122df8bae1dSRodney W. Grimes int error; 1123df8bae1dSRodney W. Grimes 1124df8bae1dSRodney W. Grimes struct vnode *fdvp = ap->a_fdvp; 1125df8bae1dSRodney W. Grimes struct vnode *fvp = ap->a_fvp; 1126df8bae1dSRodney W. Grimes struct vnode *tdvp = ap->a_tdvp; 1127df8bae1dSRodney W. Grimes struct vnode *tvp = ap->a_tvp; 1128a9320ff3SKATO Takenori int isklockset = 0; 1129df8bae1dSRodney W. Grimes 1130df8bae1dSRodney W. Grimes if (fdvp->v_op == union_vnodeop_p) { /* always true */ 1131df8bae1dSRodney W. Grimes struct union_node *un = VTOUNION(fdvp); 1132df8bae1dSRodney W. Grimes if (un->un_uppervp == NULLVP) { 1133996c772fSJohn Dyson /* 1134996c772fSJohn Dyson * this should never happen in normal 1135996c772fSJohn Dyson * operation but might if there was 1136996c772fSJohn Dyson * a problem creating the top-level shadow 1137996c772fSJohn Dyson * directory. 1138996c772fSJohn Dyson */ 1139996c772fSJohn Dyson error = EXDEV; 1140df8bae1dSRodney W. Grimes goto bad; 1141df8bae1dSRodney W. Grimes } 1142df8bae1dSRodney W. Grimes 1143df8bae1dSRodney W. Grimes fdvp = un->un_uppervp; 1144df8bae1dSRodney W. Grimes VREF(fdvp); 1145df8bae1dSRodney W. Grimes vrele(ap->a_fdvp); 1146df8bae1dSRodney W. Grimes } 1147df8bae1dSRodney W. Grimes 1148df8bae1dSRodney W. Grimes if (fvp->v_op == union_vnodeop_p) { /* always true */ 1149df8bae1dSRodney W. Grimes struct union_node *un = VTOUNION(fvp); 1150df8bae1dSRodney W. Grimes if (un->un_uppervp == NULLVP) { 1151996c772fSJohn Dyson /* XXX: should do a copyup */ 1152996c772fSJohn Dyson error = EXDEV; 1153df8bae1dSRodney W. Grimes goto bad; 1154df8bae1dSRodney W. Grimes } 1155df8bae1dSRodney W. Grimes 1156996c772fSJohn Dyson if (un->un_lowervp != NULLVP) 1157996c772fSJohn Dyson ap->a_fcnp->cn_flags |= DOWHITEOUT; 1158996c772fSJohn Dyson 1159df8bae1dSRodney W. Grimes fvp = un->un_uppervp; 1160df8bae1dSRodney W. Grimes VREF(fvp); 1161df8bae1dSRodney W. Grimes vrele(ap->a_fvp); 1162df8bae1dSRodney W. Grimes } 1163df8bae1dSRodney W. Grimes 1164df8bae1dSRodney W. Grimes if (tdvp->v_op == union_vnodeop_p) { 1165df8bae1dSRodney W. Grimes struct union_node *un = VTOUNION(tdvp); 1166df8bae1dSRodney W. Grimes if (un->un_uppervp == NULLVP) { 1167996c772fSJohn Dyson /* 1168996c772fSJohn Dyson * this should never happen in normal 1169996c772fSJohn Dyson * operation but might if there was 1170996c772fSJohn Dyson * a problem creating the top-level shadow 1171996c772fSJohn Dyson * directory. 1172996c772fSJohn Dyson */ 1173996c772fSJohn Dyson error = EXDEV; 1174df8bae1dSRodney W. Grimes goto bad; 1175df8bae1dSRodney W. Grimes } 1176df8bae1dSRodney W. Grimes 1177df8bae1dSRodney W. Grimes tdvp = un->un_uppervp; 1178df8bae1dSRodney W. Grimes VREF(tdvp); 1179a9320ff3SKATO Takenori SETKLOCK(un); 1180df8bae1dSRodney W. Grimes vput(ap->a_tdvp); 1181a9320ff3SKATO Takenori CLEARKLOCK(un); 1182df8bae1dSRodney W. Grimes } 1183df8bae1dSRodney W. Grimes 1184996c772fSJohn Dyson if (tvp != NULLVP && tvp->v_op == union_vnodeop_p) { 1185df8bae1dSRodney W. Grimes struct union_node *un = VTOUNION(tvp); 1186df8bae1dSRodney W. Grimes 1187df8bae1dSRodney W. Grimes tvp = un->un_uppervp; 1188996c772fSJohn Dyson if (tvp != NULLVP) { 1189df8bae1dSRodney W. Grimes VREF(tvp); 1190a9320ff3SKATO Takenori SETKLOCK(un); 1191a9320ff3SKATO Takenori isklockset = 1; 1192996c772fSJohn Dyson } 1193df8bae1dSRodney W. Grimes vput(ap->a_tvp); 1194a9320ff3SKATO Takenori if (isklockset) 1195a9320ff3SKATO Takenori CLEARKLOCK(un); 1196df8bae1dSRodney W. Grimes } 1197df8bae1dSRodney W. Grimes 1198df8bae1dSRodney W. Grimes return (VOP_RENAME(fdvp, fvp, ap->a_fcnp, tdvp, tvp, ap->a_tcnp)); 1199df8bae1dSRodney W. Grimes 1200df8bae1dSRodney W. Grimes bad: 1201df8bae1dSRodney W. Grimes vrele(fdvp); 1202df8bae1dSRodney W. Grimes vrele(fvp); 1203df8bae1dSRodney W. Grimes vput(tdvp); 1204996c772fSJohn Dyson if (tvp != NULLVP) 1205df8bae1dSRodney W. Grimes vput(tvp); 1206df8bae1dSRodney W. Grimes 1207df8bae1dSRodney W. Grimes return (error); 1208df8bae1dSRodney W. Grimes } 1209df8bae1dSRodney W. Grimes 1210df8bae1dSRodney W. Grimes int 1211df8bae1dSRodney W. Grimes union_mkdir(ap) 1212df8bae1dSRodney W. Grimes struct vop_mkdir_args /* { 1213df8bae1dSRodney W. Grimes struct vnode *a_dvp; 1214df8bae1dSRodney W. Grimes struct vnode **a_vpp; 1215df8bae1dSRodney W. Grimes struct componentname *a_cnp; 1216df8bae1dSRodney W. Grimes struct vattr *a_vap; 1217df8bae1dSRodney W. Grimes } */ *ap; 1218df8bae1dSRodney W. Grimes { 1219df8bae1dSRodney W. Grimes struct union_node *un = VTOUNION(ap->a_dvp); 1220df8bae1dSRodney W. Grimes struct vnode *dvp = un->un_uppervp; 1221996c772fSJohn Dyson struct componentname *cnp = ap->a_cnp; 1222996c772fSJohn Dyson struct proc *p = cnp->cn_proc; 1223df8bae1dSRodney W. Grimes 1224996c772fSJohn Dyson if (dvp != NULLVP) { 1225df8bae1dSRodney W. Grimes int error; 1226df8bae1dSRodney W. Grimes struct vnode *vp; 1227df8bae1dSRodney W. Grimes 1228996c772fSJohn Dyson FIXUP(un, p); 1229df8bae1dSRodney W. Grimes VREF(dvp); 1230a9320ff3SKATO Takenori SETKLOCK(un); 1231996c772fSJohn Dyson VOP_UNLOCK(ap->a_dvp, 0, p); 1232a9320ff3SKATO Takenori CLEARKLOCK(un); 1233996c772fSJohn Dyson error = VOP_MKDIR(dvp, &vp, cnp, ap->a_vap); 1234996c772fSJohn Dyson if (error) { 1235996c772fSJohn Dyson vrele(ap->a_dvp); 1236df8bae1dSRodney W. Grimes return (error); 1237996c772fSJohn Dyson } 1238df8bae1dSRodney W. Grimes 1239996c772fSJohn Dyson error = union_allocvp(ap->a_vpp, ap->a_dvp->v_mount, ap->a_dvp, 1240996c772fSJohn Dyson NULLVP, cnp, vp, NULLVP, 1); 1241996c772fSJohn Dyson vrele(ap->a_dvp); 1242df8bae1dSRodney W. Grimes if (error) 1243df8bae1dSRodney W. Grimes vput(vp); 1244df8bae1dSRodney W. Grimes return (error); 1245df8bae1dSRodney W. Grimes } 1246df8bae1dSRodney W. Grimes 1247df8bae1dSRodney W. Grimes vput(ap->a_dvp); 1248df8bae1dSRodney W. Grimes return (EROFS); 1249df8bae1dSRodney W. Grimes } 1250df8bae1dSRodney W. Grimes 1251df8bae1dSRodney W. Grimes int 1252df8bae1dSRodney W. Grimes union_rmdir(ap) 1253df8bae1dSRodney W. Grimes struct vop_rmdir_args /* { 1254df8bae1dSRodney W. Grimes struct vnode *a_dvp; 1255df8bae1dSRodney W. Grimes struct vnode *a_vp; 1256df8bae1dSRodney W. Grimes struct componentname *a_cnp; 1257df8bae1dSRodney W. Grimes } */ *ap; 1258df8bae1dSRodney W. Grimes { 1259df8bae1dSRodney W. Grimes int error; 1260df8bae1dSRodney W. Grimes struct union_node *dun = VTOUNION(ap->a_dvp); 1261df8bae1dSRodney W. Grimes struct union_node *un = VTOUNION(ap->a_vp); 1262996c772fSJohn Dyson struct componentname *cnp = ap->a_cnp; 1263996c772fSJohn Dyson struct proc *p = cnp->cn_proc; 1264df8bae1dSRodney W. Grimes 1265996c772fSJohn Dyson if (dun->un_uppervp == NULLVP) 1266996c772fSJohn Dyson panic("union rmdir: null upper vnode"); 1267996c772fSJohn Dyson 1268996c772fSJohn Dyson if (un->un_uppervp != NULLVP) { 1269df8bae1dSRodney W. Grimes struct vnode *dvp = dun->un_uppervp; 1270df8bae1dSRodney W. Grimes struct vnode *vp = un->un_uppervp; 1271df8bae1dSRodney W. Grimes 1272996c772fSJohn Dyson FIXUP(dun, p); 1273df8bae1dSRodney W. Grimes VREF(dvp); 1274a9320ff3SKATO Takenori SETKLOCK(dun); 1275df8bae1dSRodney W. Grimes vput(ap->a_dvp); 1276a9320ff3SKATO Takenori CLEARKLOCK(dun); 1277996c772fSJohn Dyson FIXUP(un, p); 1278df8bae1dSRodney W. Grimes VREF(vp); 1279a9320ff3SKATO Takenori SETKLOCK(un); 1280df8bae1dSRodney W. Grimes vput(ap->a_vp); 1281a9320ff3SKATO Takenori CLEARKLOCK(un); 1282df8bae1dSRodney W. Grimes 1283996c772fSJohn Dyson if (union_dowhiteout(un, cnp->cn_cred, cnp->cn_proc)) 1284996c772fSJohn Dyson cnp->cn_flags |= DOWHITEOUT; 1285df8bae1dSRodney W. Grimes error = VOP_RMDIR(dvp, vp, ap->a_cnp); 1286df8bae1dSRodney W. Grimes if (!error) 1287df8bae1dSRodney W. Grimes union_removed_upper(un); 1288df8bae1dSRodney W. Grimes } else { 1289996c772fSJohn Dyson FIXUP(dun, p); 1290996c772fSJohn Dyson error = union_mkwhiteout( 1291996c772fSJohn Dyson MOUNTTOUNIONMOUNT(UNIONTOV(dun)->v_mount), 1292996c772fSJohn Dyson dun->un_uppervp, ap->a_cnp, un->un_path); 1293df8bae1dSRodney W. Grimes vput(ap->a_dvp); 1294df8bae1dSRodney W. Grimes vput(ap->a_vp); 1295df8bae1dSRodney W. Grimes } 1296df8bae1dSRodney W. Grimes 1297df8bae1dSRodney W. Grimes return (error); 1298df8bae1dSRodney W. Grimes } 1299df8bae1dSRodney W. Grimes 1300df8bae1dSRodney W. Grimes int 1301df8bae1dSRodney W. Grimes union_symlink(ap) 1302df8bae1dSRodney W. Grimes struct vop_symlink_args /* { 1303df8bae1dSRodney W. Grimes struct vnode *a_dvp; 1304df8bae1dSRodney W. Grimes struct vnode **a_vpp; 1305df8bae1dSRodney W. Grimes struct componentname *a_cnp; 1306df8bae1dSRodney W. Grimes struct vattr *a_vap; 1307df8bae1dSRodney W. Grimes char *a_target; 1308df8bae1dSRodney W. Grimes } */ *ap; 1309df8bae1dSRodney W. Grimes { 1310df8bae1dSRodney W. Grimes struct union_node *un = VTOUNION(ap->a_dvp); 1311df8bae1dSRodney W. Grimes struct vnode *dvp = un->un_uppervp; 1312996c772fSJohn Dyson struct componentname *cnp = ap->a_cnp; 1313996c772fSJohn Dyson struct proc *p = cnp->cn_proc; 1314df8bae1dSRodney W. Grimes 1315996c772fSJohn Dyson if (dvp != NULLVP) { 1316df8bae1dSRodney W. Grimes int error; 1317df8bae1dSRodney W. Grimes struct vnode *vp; 1318df8bae1dSRodney W. Grimes 1319996c772fSJohn Dyson FIXUP(un, p); 1320df8bae1dSRodney W. Grimes VREF(dvp); 1321a9320ff3SKATO Takenori SETKLOCK(un); 1322df8bae1dSRodney W. Grimes vput(ap->a_dvp); 1323a9320ff3SKATO Takenori CLEARKLOCK(un); 1324996c772fSJohn Dyson error = VOP_SYMLINK(dvp, &vp, cnp, ap->a_vap, ap->a_target); 1325df8bae1dSRodney W. Grimes *ap->a_vpp = NULLVP; 1326df8bae1dSRodney W. Grimes return (error); 1327df8bae1dSRodney W. Grimes } 1328df8bae1dSRodney W. Grimes 1329df8bae1dSRodney W. Grimes vput(ap->a_dvp); 1330df8bae1dSRodney W. Grimes return (EROFS); 1331df8bae1dSRodney W. Grimes } 1332df8bae1dSRodney W. Grimes 1333df8bae1dSRodney W. Grimes /* 1334df8bae1dSRodney W. Grimes * union_readdir works in concert with getdirentries and 1335df8bae1dSRodney W. Grimes * readdir(3) to provide a list of entries in the unioned 1336df8bae1dSRodney W. Grimes * directories. getdirentries is responsible for walking 1337df8bae1dSRodney W. Grimes * down the union stack. readdir(3) is responsible for 1338df8bae1dSRodney W. Grimes * eliminating duplicate names from the returned data stream. 1339df8bae1dSRodney W. Grimes */ 1340df8bae1dSRodney W. Grimes int 1341df8bae1dSRodney W. Grimes union_readdir(ap) 1342df8bae1dSRodney W. Grimes struct vop_readdir_args /* { 1343df8bae1dSRodney W. Grimes struct vnode *a_vp; 1344df8bae1dSRodney W. Grimes struct uio *a_uio; 1345df8bae1dSRodney W. Grimes struct ucred *a_cred; 1346996c772fSJohn Dyson int *a_eofflag; 1347996c772fSJohn Dyson u_long *a_cookies; 1348996c772fSJohn Dyson int a_ncookies; 1349df8bae1dSRodney W. Grimes } */ *ap; 1350df8bae1dSRodney W. Grimes { 1351df8bae1dSRodney W. Grimes struct union_node *un = VTOUNION(ap->a_vp); 1352996c772fSJohn Dyson struct vnode *uvp = un->un_uppervp; 1353996c772fSJohn Dyson struct proc *p = ap->a_uio->uio_procp; 1354df8bae1dSRodney W. Grimes 1355996c772fSJohn Dyson if (uvp == NULLVP) 1356996c772fSJohn Dyson return (0); 1357df8bae1dSRodney W. Grimes 1358996c772fSJohn Dyson FIXUP(un, p); 1359996c772fSJohn Dyson ap->a_vp = uvp; 1360996c772fSJohn Dyson return (VCALL(uvp, VOFFSET(vop_readdir), ap)); 1361df8bae1dSRodney W. Grimes } 1362df8bae1dSRodney W. Grimes 1363df8bae1dSRodney W. Grimes int 1364df8bae1dSRodney W. Grimes union_readlink(ap) 1365df8bae1dSRodney W. Grimes struct vop_readlink_args /* { 1366df8bae1dSRodney W. Grimes struct vnode *a_vp; 1367df8bae1dSRodney W. Grimes struct uio *a_uio; 1368df8bae1dSRodney W. Grimes struct ucred *a_cred; 1369df8bae1dSRodney W. Grimes } */ *ap; 1370df8bae1dSRodney W. Grimes { 1371df8bae1dSRodney W. Grimes int error; 1372996c772fSJohn Dyson struct uio *uio = ap->a_uio; 1373996c772fSJohn Dyson struct proc *p = uio->uio_procp; 1374df8bae1dSRodney W. Grimes struct vnode *vp = OTHERVP(ap->a_vp); 1375df8bae1dSRodney W. Grimes int dolock = (vp == LOWERVP(ap->a_vp)); 1376df8bae1dSRodney W. Grimes 1377df8bae1dSRodney W. Grimes if (dolock) 1378996c772fSJohn Dyson vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); 1379df8bae1dSRodney W. Grimes else 1380996c772fSJohn Dyson FIXUP(VTOUNION(ap->a_vp), p); 1381996c772fSJohn Dyson ap->a_vp = vp; 1382996c772fSJohn Dyson error = VCALL(vp, VOFFSET(vop_readlink), ap); 1383df8bae1dSRodney W. Grimes if (dolock) 1384996c772fSJohn Dyson VOP_UNLOCK(vp, 0, p); 1385df8bae1dSRodney W. Grimes 1386df8bae1dSRodney W. Grimes return (error); 1387df8bae1dSRodney W. Grimes } 1388df8bae1dSRodney W. Grimes 1389df8bae1dSRodney W. Grimes int 1390df8bae1dSRodney W. Grimes union_abortop(ap) 1391df8bae1dSRodney W. Grimes struct vop_abortop_args /* { 1392df8bae1dSRodney W. Grimes struct vnode *a_dvp; 1393df8bae1dSRodney W. Grimes struct componentname *a_cnp; 1394df8bae1dSRodney W. Grimes } */ *ap; 1395df8bae1dSRodney W. Grimes { 1396df8bae1dSRodney W. Grimes int error; 1397996c772fSJohn Dyson struct componentname *cnp = ap->a_cnp; 1398996c772fSJohn Dyson struct proc *p = cnp->cn_proc; 1399df8bae1dSRodney W. Grimes struct vnode *vp = OTHERVP(ap->a_dvp); 1400df8bae1dSRodney W. Grimes struct union_node *un = VTOUNION(ap->a_dvp); 1401df8bae1dSRodney W. Grimes int islocked = un->un_flags & UN_LOCKED; 1402df8bae1dSRodney W. Grimes int dolock = (vp == LOWERVP(ap->a_dvp)); 1403df8bae1dSRodney W. Grimes 1404df8bae1dSRodney W. Grimes if (islocked) { 1405df8bae1dSRodney W. Grimes if (dolock) 1406996c772fSJohn Dyson vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); 1407df8bae1dSRodney W. Grimes else 1408996c772fSJohn Dyson FIXUP(VTOUNION(ap->a_dvp), p); 1409df8bae1dSRodney W. Grimes } 1410996c772fSJohn Dyson ap->a_dvp = vp; 1411996c772fSJohn Dyson error = VCALL(vp, VOFFSET(vop_abortop), ap); 1412df8bae1dSRodney W. Grimes if (islocked && dolock) 1413996c772fSJohn Dyson VOP_UNLOCK(vp, 0, p); 1414df8bae1dSRodney W. Grimes 1415df8bae1dSRodney W. Grimes return (error); 1416df8bae1dSRodney W. Grimes } 1417df8bae1dSRodney W. Grimes 1418df8bae1dSRodney W. Grimes int 1419df8bae1dSRodney W. Grimes union_inactive(ap) 1420df8bae1dSRodney W. Grimes struct vop_inactive_args /* { 1421df8bae1dSRodney W. Grimes struct vnode *a_vp; 1422996c772fSJohn Dyson struct proc *a_p; 1423df8bae1dSRodney W. Grimes } */ *ap; 1424df8bae1dSRodney W. Grimes { 1425996c772fSJohn Dyson struct vnode *vp = ap->a_vp; 1426996c772fSJohn Dyson struct proc *p = ap->a_p; 1427996c772fSJohn Dyson struct union_node *un = VTOUNION(vp); 1428996c772fSJohn Dyson struct vnode **vpp; 1429df8bae1dSRodney W. Grimes 1430df8bae1dSRodney W. Grimes /* 1431df8bae1dSRodney W. Grimes * Do nothing (and _don't_ bypass). 1432df8bae1dSRodney W. Grimes * Wait to vrele lowervp until reclaim, 1433df8bae1dSRodney W. Grimes * so that until then our union_node is in the 1434df8bae1dSRodney W. Grimes * cache and reusable. 1435df8bae1dSRodney W. Grimes * 1436df8bae1dSRodney W. Grimes * NEEDSWORK: Someday, consider inactive'ing 1437df8bae1dSRodney W. Grimes * the lowervp and then trying to reactivate it 1438df8bae1dSRodney W. Grimes * with capabilities (v_id) 1439df8bae1dSRodney W. Grimes * like they do in the name lookup cache code. 1440df8bae1dSRodney W. Grimes * That's too much work for now. 1441df8bae1dSRodney W. Grimes */ 1442df8bae1dSRodney W. Grimes 1443996c772fSJohn Dyson if (un->un_dircache != 0) { 1444996c772fSJohn Dyson for (vpp = un->un_dircache; *vpp != NULLVP; vpp++) 1445996c772fSJohn Dyson vrele(*vpp); 1446996c772fSJohn Dyson free(un->un_dircache, M_TEMP); 1447996c772fSJohn Dyson un->un_dircache = 0; 1448996c772fSJohn Dyson } 1449df8bae1dSRodney W. Grimes 1450996c772fSJohn Dyson VOP_UNLOCK(vp, 0, p); 1451996c772fSJohn Dyson 1452996c772fSJohn Dyson if ((un->un_flags & UN_CACHED) == 0) 1453996c772fSJohn Dyson vgone(vp); 1454df8bae1dSRodney W. Grimes 1455df8bae1dSRodney W. Grimes return (0); 1456df8bae1dSRodney W. Grimes } 1457df8bae1dSRodney W. Grimes 1458df8bae1dSRodney W. Grimes int 1459df8bae1dSRodney W. Grimes union_reclaim(ap) 1460df8bae1dSRodney W. Grimes struct vop_reclaim_args /* { 1461df8bae1dSRodney W. Grimes struct vnode *a_vp; 1462df8bae1dSRodney W. Grimes } */ *ap; 1463df8bae1dSRodney W. Grimes { 1464df8bae1dSRodney W. Grimes 1465df8bae1dSRodney W. Grimes union_freevp(ap->a_vp); 1466df8bae1dSRodney W. Grimes 1467df8bae1dSRodney W. Grimes return (0); 1468df8bae1dSRodney W. Grimes } 1469df8bae1dSRodney W. Grimes 1470df8bae1dSRodney W. Grimes int 1471df8bae1dSRodney W. Grimes union_lock(ap) 1472df8bae1dSRodney W. Grimes struct vop_lock_args *ap; 1473df8bae1dSRodney W. Grimes { 1474df8bae1dSRodney W. Grimes struct vnode *vp = ap->a_vp; 1475996c772fSJohn Dyson struct proc *p = ap->a_p; 1476996c772fSJohn Dyson int flags = ap->a_flags; 1477df8bae1dSRodney W. Grimes struct union_node *un; 1478996c772fSJohn Dyson int error; 1479996c772fSJohn Dyson 1480996c772fSJohn Dyson vop_nolock(ap); 1481996c772fSJohn Dyson /* 1482996c772fSJohn Dyson * Need to do real lockmgr-style locking here. 1483996c772fSJohn Dyson * in the mean time, draining won't work quite right, 1484996c772fSJohn Dyson * which could lead to a few race conditions. 1485996c772fSJohn Dyson * the following test was here, but is not quite right, we 1486996c772fSJohn Dyson * still need to take the lock: 1487996c772fSJohn Dyson if ((flags & LK_TYPE_MASK) == LK_DRAIN) 1488996c772fSJohn Dyson return (0); 1489996c772fSJohn Dyson */ 1490996c772fSJohn Dyson flags &= ~LK_INTERLOCK; 1491df8bae1dSRodney W. Grimes 1492df8bae1dSRodney W. Grimes start: 1493df8bae1dSRodney W. Grimes un = VTOUNION(vp); 1494df8bae1dSRodney W. Grimes 1495996c772fSJohn Dyson if (un->un_uppervp != NULLVP) { 1496a9320ff3SKATO Takenori if (((un->un_flags & (UN_ULOCK | UN_KLOCK)) == 0) && 1497996c772fSJohn Dyson (vp->v_usecount != 0)) { 149818b4c8e2SKATO Takenori if (VOP_ISLOCKED(un->un_uppervp)) { 149918b4c8e2SKATO Takenori /* 1500a9320ff3SKATO Takenori * XXX 1501a18ac0ffSKATO Takenori * Erm, we find race! 150218b4c8e2SKATO Takenori */ 150318b4c8e2SKATO Takenori #ifdef DIAGNOSTIC 150418b4c8e2SKATO Takenori panic("union_link: upper vnode is locked, " 150518b4c8e2SKATO Takenori "but UN_UNLOCK is not set."); 150618b4c8e2SKATO Takenori #endif 1507a9320ff3SKATO Takenori un->un_flags |= UN_ULOCK; 150818b4c8e2SKATO Takenori } else { 1509996c772fSJohn Dyson error = vn_lock(un->un_uppervp, flags, p); 1510996c772fSJohn Dyson if (error) 1511996c772fSJohn Dyson return (error); 1512df8bae1dSRodney W. Grimes un->un_flags |= UN_ULOCK; 1513df8bae1dSRodney W. Grimes } 151418b4c8e2SKATO Takenori } 1515df8bae1dSRodney W. Grimes } 1516df8bae1dSRodney W. Grimes 1517df8bae1dSRodney W. Grimes if (un->un_flags & UN_LOCKED) { 1518df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC 1519df8bae1dSRodney W. Grimes if (curproc && un->un_pid == curproc->p_pid && 1520df8bae1dSRodney W. Grimes un->un_pid > -1 && curproc->p_pid > -1) 1521df8bae1dSRodney W. Grimes panic("union: locking against myself"); 1522df8bae1dSRodney W. Grimes #endif 1523df8bae1dSRodney W. Grimes un->un_flags |= UN_WANT; 1524996c772fSJohn Dyson tsleep((caddr_t)&un->un_flags, PINOD, "unionlk2", 0); 1525df8bae1dSRodney W. Grimes goto start; 1526df8bae1dSRodney W. Grimes } 1527df8bae1dSRodney W. Grimes 1528df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC 1529df8bae1dSRodney W. Grimes if (curproc) 1530df8bae1dSRodney W. Grimes un->un_pid = curproc->p_pid; 1531df8bae1dSRodney W. Grimes else 1532df8bae1dSRodney W. Grimes un->un_pid = -1; 1533df8bae1dSRodney W. Grimes #endif 1534df8bae1dSRodney W. Grimes 1535df8bae1dSRodney W. Grimes un->un_flags |= UN_LOCKED; 1536df8bae1dSRodney W. Grimes return (0); 1537df8bae1dSRodney W. Grimes } 1538df8bae1dSRodney W. Grimes 1539996c772fSJohn Dyson /* 1540996c772fSJohn Dyson * When operations want to vput() a union node yet retain a lock on 1541996c772fSJohn Dyson * the upper vnode (say, to do some further operations like link(), 1542996c772fSJohn Dyson * mkdir(), ...), they set UN_KLOCK on the union node, then call 1543996c772fSJohn Dyson * vput() which calls VOP_UNLOCK() and comes here. union_unlock() 1544996c772fSJohn Dyson * unlocks the union node (leaving the upper vnode alone), clears the 1545996c772fSJohn Dyson * KLOCK flag, and then returns to vput(). The caller then does whatever 1546996c772fSJohn Dyson * is left to do with the upper vnode, and ensures that it gets unlocked. 1547996c772fSJohn Dyson * 1548996c772fSJohn Dyson * If UN_KLOCK isn't set, then the upper vnode is unlocked here. 1549996c772fSJohn Dyson */ 1550df8bae1dSRodney W. Grimes int 1551df8bae1dSRodney W. Grimes union_unlock(ap) 1552996c772fSJohn Dyson struct vop_unlock_args /* { 1553996c772fSJohn Dyson struct vnode *a_vp; 1554996c772fSJohn Dyson int a_flags; 1555996c772fSJohn Dyson struct proc *a_p; 1556996c772fSJohn Dyson } */ *ap; 1557df8bae1dSRodney W. Grimes { 1558df8bae1dSRodney W. Grimes struct union_node *un = VTOUNION(ap->a_vp); 1559996c772fSJohn Dyson struct proc *p = ap->a_p; 1560df8bae1dSRodney W. Grimes 1561df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC 1562df8bae1dSRodney W. Grimes if ((un->un_flags & UN_LOCKED) == 0) 1563df8bae1dSRodney W. Grimes panic("union: unlock unlocked node"); 1564df8bae1dSRodney W. Grimes if (curproc && un->un_pid != curproc->p_pid && 1565df8bae1dSRodney W. Grimes curproc->p_pid > -1 && un->un_pid > -1) 1566df8bae1dSRodney W. Grimes panic("union: unlocking other process's union node"); 1567df8bae1dSRodney W. Grimes #endif 1568df8bae1dSRodney W. Grimes 1569df8bae1dSRodney W. Grimes un->un_flags &= ~UN_LOCKED; 1570df8bae1dSRodney W. Grimes 1571df8bae1dSRodney W. Grimes if ((un->un_flags & (UN_ULOCK|UN_KLOCK)) == UN_ULOCK) 1572996c772fSJohn Dyson VOP_UNLOCK(un->un_uppervp, 0, p); 1573df8bae1dSRodney W. Grimes 1574a9320ff3SKATO Takenori un->un_flags &= ~UN_ULOCK; 1575df8bae1dSRodney W. Grimes 1576df8bae1dSRodney W. Grimes if (un->un_flags & UN_WANT) { 1577df8bae1dSRodney W. Grimes un->un_flags &= ~UN_WANT; 1578df8bae1dSRodney W. Grimes wakeup((caddr_t) &un->un_flags); 1579df8bae1dSRodney W. Grimes } 1580df8bae1dSRodney W. Grimes 1581df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC 1582df8bae1dSRodney W. Grimes un->un_pid = 0; 1583df8bae1dSRodney W. Grimes #endif 1584996c772fSJohn Dyson vop_nounlock(ap); 1585df8bae1dSRodney W. Grimes 1586df8bae1dSRodney W. Grimes return (0); 1587df8bae1dSRodney W. Grimes } 1588df8bae1dSRodney W. Grimes 1589df8bae1dSRodney W. Grimes int 1590df8bae1dSRodney W. Grimes union_bmap(ap) 1591df8bae1dSRodney W. Grimes struct vop_bmap_args /* { 1592df8bae1dSRodney W. Grimes struct vnode *a_vp; 1593df8bae1dSRodney W. Grimes daddr_t a_bn; 1594df8bae1dSRodney W. Grimes struct vnode **a_vpp; 1595df8bae1dSRodney W. Grimes daddr_t *a_bnp; 1596df8bae1dSRodney W. Grimes int *a_runp; 1597c83ebe77SJohn Dyson int *a_runb; 1598df8bae1dSRodney W. Grimes } */ *ap; 1599df8bae1dSRodney W. Grimes { 1600df8bae1dSRodney W. Grimes int error; 1601996c772fSJohn Dyson struct proc *p = curproc; /* XXX */ 1602df8bae1dSRodney W. Grimes struct vnode *vp = OTHERVP(ap->a_vp); 1603df8bae1dSRodney W. Grimes int dolock = (vp == LOWERVP(ap->a_vp)); 1604df8bae1dSRodney W. Grimes 1605df8bae1dSRodney W. Grimes if (dolock) 1606996c772fSJohn Dyson vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); 1607df8bae1dSRodney W. Grimes else 1608996c772fSJohn Dyson FIXUP(VTOUNION(ap->a_vp), p); 1609996c772fSJohn Dyson ap->a_vp = vp; 1610996c772fSJohn Dyson error = VCALL(vp, VOFFSET(vop_bmap), ap); 1611df8bae1dSRodney W. Grimes if (dolock) 1612996c772fSJohn Dyson VOP_UNLOCK(vp, 0, p); 1613df8bae1dSRodney W. Grimes 1614df8bae1dSRodney W. Grimes return (error); 1615df8bae1dSRodney W. Grimes } 1616df8bae1dSRodney W. Grimes 1617df8bae1dSRodney W. Grimes int 1618df8bae1dSRodney W. Grimes union_print(ap) 1619df8bae1dSRodney W. Grimes struct vop_print_args /* { 1620df8bae1dSRodney W. Grimes struct vnode *a_vp; 1621df8bae1dSRodney W. Grimes } */ *ap; 1622df8bae1dSRodney W. Grimes { 1623df8bae1dSRodney W. Grimes struct vnode *vp = ap->a_vp; 1624df8bae1dSRodney W. Grimes 16253a773ad0SPoul-Henning Kamp printf("\ttag VT_UNION, vp=%p, uppervp=%p, lowervp=%p\n", 1626df8bae1dSRodney W. Grimes vp, UPPERVP(vp), LOWERVP(vp)); 1627996c772fSJohn Dyson if (UPPERVP(vp) != NULLVP) 1628996c772fSJohn Dyson vprint("union: upper", UPPERVP(vp)); 1629996c772fSJohn Dyson if (LOWERVP(vp) != NULLVP) 1630996c772fSJohn Dyson vprint("union: lower", LOWERVP(vp)); 1631996c772fSJohn Dyson 1632df8bae1dSRodney W. Grimes return (0); 1633df8bae1dSRodney W. Grimes } 1634df8bae1dSRodney W. Grimes 1635df8bae1dSRodney W. Grimes int 1636df8bae1dSRodney W. Grimes union_islocked(ap) 1637df8bae1dSRodney W. Grimes struct vop_islocked_args /* { 1638df8bae1dSRodney W. Grimes struct vnode *a_vp; 1639df8bae1dSRodney W. Grimes } */ *ap; 1640df8bae1dSRodney W. Grimes { 1641df8bae1dSRodney W. Grimes 1642df8bae1dSRodney W. Grimes return ((VTOUNION(ap->a_vp)->un_flags & UN_LOCKED) ? 1 : 0); 1643df8bae1dSRodney W. Grimes } 1644df8bae1dSRodney W. Grimes 1645df8bae1dSRodney W. Grimes int 1646df8bae1dSRodney W. Grimes union_pathconf(ap) 1647df8bae1dSRodney W. Grimes struct vop_pathconf_args /* { 1648df8bae1dSRodney W. Grimes struct vnode *a_vp; 1649df8bae1dSRodney W. Grimes int a_name; 1650df8bae1dSRodney W. Grimes int *a_retval; 1651df8bae1dSRodney W. Grimes } */ *ap; 1652df8bae1dSRodney W. Grimes { 1653df8bae1dSRodney W. Grimes int error; 1654996c772fSJohn Dyson struct proc *p = curproc; /* XXX */ 1655df8bae1dSRodney W. Grimes struct vnode *vp = OTHERVP(ap->a_vp); 1656df8bae1dSRodney W. Grimes int dolock = (vp == LOWERVP(ap->a_vp)); 1657df8bae1dSRodney W. Grimes 1658df8bae1dSRodney W. Grimes if (dolock) 1659996c772fSJohn Dyson vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); 1660df8bae1dSRodney W. Grimes else 1661996c772fSJohn Dyson FIXUP(VTOUNION(ap->a_vp), p); 1662996c772fSJohn Dyson ap->a_vp = vp; 1663996c772fSJohn Dyson error = VCALL(vp, VOFFSET(vop_pathconf), ap); 1664df8bae1dSRodney W. Grimes if (dolock) 1665996c772fSJohn Dyson VOP_UNLOCK(vp, 0, p); 1666df8bae1dSRodney W. Grimes 1667df8bae1dSRodney W. Grimes return (error); 1668df8bae1dSRodney W. Grimes } 1669df8bae1dSRodney W. Grimes 1670df8bae1dSRodney W. Grimes int 1671df8bae1dSRodney W. Grimes union_advlock(ap) 1672df8bae1dSRodney W. Grimes struct vop_advlock_args /* { 1673df8bae1dSRodney W. Grimes struct vnode *a_vp; 1674df8bae1dSRodney W. Grimes caddr_t a_id; 1675df8bae1dSRodney W. Grimes int a_op; 1676df8bae1dSRodney W. Grimes struct flock *a_fl; 1677df8bae1dSRodney W. Grimes int a_flags; 1678df8bae1dSRodney W. Grimes } */ *ap; 1679df8bae1dSRodney W. Grimes { 1680996c772fSJohn Dyson register struct vnode *ovp = OTHERVP(ap->a_vp); 1681df8bae1dSRodney W. Grimes 1682996c772fSJohn Dyson ap->a_vp = ovp; 1683996c772fSJohn Dyson return (VCALL(ovp, VOFFSET(vop_advlock), ap)); 1684df8bae1dSRodney W. Grimes } 1685df8bae1dSRodney W. Grimes 1686df8bae1dSRodney W. Grimes 1687df8bae1dSRodney W. Grimes /* 1688df8bae1dSRodney W. Grimes * XXX - vop_strategy must be hand coded because it has no 1689df8bae1dSRodney W. Grimes * vnode in its arguments. 1690df8bae1dSRodney W. Grimes * This goes away with a merged VM/buffer cache. 1691df8bae1dSRodney W. Grimes */ 1692df8bae1dSRodney W. Grimes int 1693df8bae1dSRodney W. Grimes union_strategy(ap) 1694df8bae1dSRodney W. Grimes struct vop_strategy_args /* { 1695df8bae1dSRodney W. Grimes struct buf *a_bp; 1696df8bae1dSRodney W. Grimes } */ *ap; 1697df8bae1dSRodney W. Grimes { 1698df8bae1dSRodney W. Grimes struct buf *bp = ap->a_bp; 1699df8bae1dSRodney W. Grimes int error; 1700df8bae1dSRodney W. Grimes struct vnode *savedvp; 1701df8bae1dSRodney W. Grimes 1702df8bae1dSRodney W. Grimes savedvp = bp->b_vp; 1703df8bae1dSRodney W. Grimes bp->b_vp = OTHERVP(bp->b_vp); 1704df8bae1dSRodney W. Grimes 1705df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC 1706df8bae1dSRodney W. Grimes if (bp->b_vp == NULLVP) 1707df8bae1dSRodney W. Grimes panic("union_strategy: nil vp"); 1708df8bae1dSRodney W. Grimes if (((bp->b_flags & B_READ) == 0) && 1709df8bae1dSRodney W. Grimes (bp->b_vp == LOWERVP(savedvp))) 1710df8bae1dSRodney W. Grimes panic("union_strategy: writing to lowervp"); 1711df8bae1dSRodney W. Grimes #endif 1712df8bae1dSRodney W. Grimes 1713df8bae1dSRodney W. Grimes error = VOP_STRATEGY(bp); 1714df8bae1dSRodney W. Grimes bp->b_vp = savedvp; 1715df8bae1dSRodney W. Grimes 1716df8bae1dSRodney W. Grimes return (error); 1717df8bae1dSRodney W. Grimes } 1718df8bae1dSRodney W. Grimes 1719df8bae1dSRodney W. Grimes /* 1720df8bae1dSRodney W. Grimes * Global vfs data structures 1721df8bae1dSRodney W. Grimes */ 1722f57e6547SBruce Evans vop_t **union_vnodeop_p; 1723df8bae1dSRodney W. Grimes struct vnodeopv_entry_desc union_vnodeop_entries[] = { 1724f57e6547SBruce Evans { &vop_default_desc, (vop_t *)vn_default_error }, 1725f57e6547SBruce Evans { &vop_lookup_desc, (vop_t *)union_lookup }, /* lookup */ 1726f57e6547SBruce Evans { &vop_create_desc, (vop_t *)union_create }, /* create */ 1727996c772fSJohn Dyson { &vop_whiteout_desc, (vop_t *)union_whiteout }, /* whiteout */ 1728f57e6547SBruce Evans { &vop_mknod_desc, (vop_t *)union_mknod }, /* mknod */ 1729f57e6547SBruce Evans { &vop_open_desc, (vop_t *)union_open }, /* open */ 1730f57e6547SBruce Evans { &vop_close_desc, (vop_t *)union_close }, /* close */ 1731f57e6547SBruce Evans { &vop_access_desc, (vop_t *)union_access }, /* access */ 1732f57e6547SBruce Evans { &vop_getattr_desc, (vop_t *)union_getattr }, /* getattr */ 1733f57e6547SBruce Evans { &vop_setattr_desc, (vop_t *)union_setattr }, /* setattr */ 1734f57e6547SBruce Evans { &vop_read_desc, (vop_t *)union_read }, /* read */ 1735f57e6547SBruce Evans { &vop_write_desc, (vop_t *)union_write }, /* write */ 1736996c772fSJohn Dyson { &vop_lease_desc, (vop_t *)union_lease }, /* lease */ 1737f57e6547SBruce Evans { &vop_ioctl_desc, (vop_t *)union_ioctl }, /* ioctl */ 1738f57e6547SBruce Evans { &vop_select_desc, (vop_t *)union_select }, /* select */ 1739996c772fSJohn Dyson { &vop_revoke_desc, (vop_t *)union_revoke }, /* revoke */ 1740f57e6547SBruce Evans { &vop_mmap_desc, (vop_t *)union_mmap }, /* mmap */ 1741f57e6547SBruce Evans { &vop_fsync_desc, (vop_t *)union_fsync }, /* fsync */ 1742f57e6547SBruce Evans { &vop_seek_desc, (vop_t *)union_seek }, /* seek */ 1743f57e6547SBruce Evans { &vop_remove_desc, (vop_t *)union_remove }, /* remove */ 1744f57e6547SBruce Evans { &vop_link_desc, (vop_t *)union_link }, /* link */ 1745f57e6547SBruce Evans { &vop_rename_desc, (vop_t *)union_rename }, /* rename */ 1746f57e6547SBruce Evans { &vop_mkdir_desc, (vop_t *)union_mkdir }, /* mkdir */ 1747f57e6547SBruce Evans { &vop_rmdir_desc, (vop_t *)union_rmdir }, /* rmdir */ 1748f57e6547SBruce Evans { &vop_symlink_desc, (vop_t *)union_symlink }, /* symlink */ 1749f57e6547SBruce Evans { &vop_readdir_desc, (vop_t *)union_readdir }, /* readdir */ 1750f57e6547SBruce Evans { &vop_readlink_desc, (vop_t *)union_readlink }, /* readlink */ 1751f57e6547SBruce Evans { &vop_abortop_desc, (vop_t *)union_abortop }, /* abortop */ 1752f57e6547SBruce Evans { &vop_inactive_desc, (vop_t *)union_inactive }, /* inactive */ 1753f57e6547SBruce Evans { &vop_reclaim_desc, (vop_t *)union_reclaim }, /* reclaim */ 1754f57e6547SBruce Evans { &vop_lock_desc, (vop_t *)union_lock }, /* lock */ 1755f57e6547SBruce Evans { &vop_unlock_desc, (vop_t *)union_unlock }, /* unlock */ 1756f57e6547SBruce Evans { &vop_bmap_desc, (vop_t *)union_bmap }, /* bmap */ 1757f57e6547SBruce Evans { &vop_strategy_desc, (vop_t *)union_strategy }, /* strategy */ 1758f57e6547SBruce Evans { &vop_print_desc, (vop_t *)union_print }, /* print */ 1759f57e6547SBruce Evans { &vop_islocked_desc, (vop_t *)union_islocked }, /* islocked */ 1760f57e6547SBruce Evans { &vop_pathconf_desc, (vop_t *)union_pathconf }, /* pathconf */ 1761f57e6547SBruce Evans { &vop_advlock_desc, (vop_t *)union_advlock }, /* advlock */ 1762df8bae1dSRodney W. Grimes #ifdef notdef 1763f57e6547SBruce Evans { &vop_blkatoff_desc, (vop_t *)union_blkatoff }, /* blkatoff */ 1764f57e6547SBruce Evans { &vop_valloc_desc, (vop_t *)union_valloc }, /* valloc */ 1765f57e6547SBruce Evans { &vop_vfree_desc, (vop_t *)union_vfree }, /* vfree */ 1766f57e6547SBruce Evans { &vop_truncate_desc, (vop_t *)union_truncate }, /* truncate */ 1767f57e6547SBruce Evans { &vop_update_desc, (vop_t *)union_update }, /* update */ 1768f57e6547SBruce Evans { &vop_bwrite_desc, (vop_t *)union_bwrite }, /* bwrite */ 1769df8bae1dSRodney W. Grimes #endif 1770f57e6547SBruce Evans { NULL, NULL } 1771df8bae1dSRodney W. Grimes }; 1772df8bae1dSRodney W. Grimes struct vnodeopv_desc union_vnodeop_opv_desc = 1773df8bae1dSRodney W. Grimes { &union_vnodeop_p, union_vnodeop_entries }; 1774c901836cSGarrett Wollman 1775c901836cSGarrett Wollman VNODEOP_SET(union_vnodeop_opv_desc); 1776