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 383dc942bbSKATO Takenori * $Id: union_vnops.c,v 1.35 1997/08/12 07:18:59 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 67c9bf0111SKATO Takenori static int union_abortop __P((struct vop_abortop_args *ap)); 68c9bf0111SKATO Takenori static int union_access __P((struct vop_access_args *ap)); 69c9bf0111SKATO Takenori static int union_advlock __P((struct vop_advlock_args *ap)); 70c9bf0111SKATO Takenori static int union_bmap __P((struct vop_bmap_args *ap)); 71c9bf0111SKATO Takenori static int union_close __P((struct vop_close_args *ap)); 72c9bf0111SKATO Takenori static int union_create __P((struct vop_create_args *ap)); 73996c772fSJohn Dyson static void union_fixup __P((struct union_node *un, struct proc *p)); 74c9bf0111SKATO Takenori static int union_fsync __P((struct vop_fsync_args *ap)); 75c9bf0111SKATO Takenori static int union_getattr __P((struct vop_getattr_args *ap)); 76c9bf0111SKATO Takenori static int union_inactive __P((struct vop_inactive_args *ap)); 77c9bf0111SKATO Takenori static int union_ioctl __P((struct vop_ioctl_args *ap)); 78c9bf0111SKATO Takenori static int union_islocked __P((struct vop_islocked_args *ap)); 79c9bf0111SKATO Takenori static int union_lease __P((struct vop_lease_args *ap)); 80c9bf0111SKATO Takenori static int union_link __P((struct vop_link_args *ap)); 81c9bf0111SKATO Takenori static int union_lock __P((struct vop_lock_args *ap)); 82c9bf0111SKATO Takenori static 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)); 86c9bf0111SKATO Takenori static int union_mkdir __P((struct vop_mkdir_args *ap)); 87c9bf0111SKATO Takenori static int union_mknod __P((struct vop_mknod_args *ap)); 88c9bf0111SKATO Takenori static int union_mmap __P((struct vop_mmap_args *ap)); 89c9bf0111SKATO Takenori static int union_open __P((struct vop_open_args *ap)); 90c9bf0111SKATO Takenori static int union_pathconf __P((struct vop_pathconf_args *ap)); 91c9bf0111SKATO Takenori static int union_print __P((struct vop_print_args *ap)); 92c9bf0111SKATO Takenori static int union_read __P((struct vop_read_args *ap)); 93c9bf0111SKATO Takenori static int union_readdir __P((struct vop_readdir_args *ap)); 94c9bf0111SKATO Takenori static int union_readlink __P((struct vop_readlink_args *ap)); 95c9bf0111SKATO Takenori static int union_reclaim __P((struct vop_reclaim_args *ap)); 96c9bf0111SKATO Takenori static int union_remove __P((struct vop_remove_args *ap)); 97c9bf0111SKATO Takenori static int union_rename __P((struct vop_rename_args *ap)); 98c9bf0111SKATO Takenori static int union_revoke __P((struct vop_revoke_args *ap)); 99c9bf0111SKATO Takenori static int union_rmdir __P((struct vop_rmdir_args *ap)); 100c9bf0111SKATO Takenori static int union_seek __P((struct vop_seek_args *ap)); 101c9bf0111SKATO Takenori static int union_select __P((struct vop_select_args *ap)); 102c9bf0111SKATO Takenori static int union_setattr __P((struct vop_setattr_args *ap)); 103c9bf0111SKATO Takenori static int union_strategy __P((struct vop_strategy_args *ap)); 104c9bf0111SKATO Takenori static int union_symlink __P((struct vop_symlink_args *ap)); 105c9bf0111SKATO Takenori static int union_unlock __P((struct vop_unlock_args *ap)); 106c9bf0111SKATO Takenori static int union_whiteout __P((struct vop_whiteout_args *ap)); 107c9bf0111SKATO Takenori static 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 196c9bf0111SKATO Takenori static 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 2893dc942bbSKATO Takenori 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 346f8fc96b5SKATO Takenori if (cnp->cn_consume != 0 || lerror == EACCES) { 347f8fc96b5SKATO Takenori if (lerror == EACCES) 348f8fc96b5SKATO Takenori lowervp = NULLVP; 349996c772fSJohn Dyson if (uppervp != NULLVP) { 350df8bae1dSRodney W. Grimes if (uppervp == upperdvp) 351df8bae1dSRodney W. Grimes vrele(uppervp); 352df8bae1dSRodney W. Grimes else 353df8bae1dSRodney W. Grimes vput(uppervp); 354df8bae1dSRodney W. Grimes uppervp = NULLVP; 355df8bae1dSRodney W. Grimes } 356df8bae1dSRodney W. Grimes *ap->a_vpp = lowervp; 357df8bae1dSRodney W. Grimes if (!lockparent) 358df8bae1dSRodney W. Grimes cnp->cn_flags &= ~LOCKPARENT; 359df8bae1dSRodney W. Grimes return (lerror); 360df8bae1dSRodney W. Grimes } 361df8bae1dSRodney W. Grimes } else { 362df8bae1dSRodney W. Grimes lerror = ENOENT; 363996c772fSJohn Dyson if ((cnp->cn_flags & ISDOTDOT) && dun->un_pvp != NULLVP) { 364996c772fSJohn Dyson lowervp = LOWERVP(dun->un_pvp); 365996c772fSJohn Dyson if (lowervp != NULLVP) { 366996c772fSJohn Dyson VREF(lowervp); 367996c772fSJohn Dyson vn_lock(lowervp, LK_EXCLUSIVE | LK_RETRY, p); 368996c772fSJohn Dyson lerror = 0; 369996c772fSJohn Dyson } 370996c772fSJohn Dyson } 371df8bae1dSRodney W. Grimes } 372df8bae1dSRodney W. Grimes 373df8bae1dSRodney W. Grimes if (!lockparent) 374df8bae1dSRodney W. Grimes cnp->cn_flags &= ~LOCKPARENT; 375df8bae1dSRodney W. Grimes 376df8bae1dSRodney W. Grimes /* 377df8bae1dSRodney W. Grimes * at this point, we have uerror and lerror indicating 378df8bae1dSRodney W. Grimes * possible errors with the lookups in the upper and lower 379df8bae1dSRodney W. Grimes * layers. additionally, uppervp and lowervp are (locked) 380df8bae1dSRodney W. Grimes * references to existing vnodes in the upper and lower layers. 381df8bae1dSRodney W. Grimes * 382df8bae1dSRodney W. Grimes * there are now three cases to consider. 383df8bae1dSRodney W. Grimes * 1. if both layers returned an error, then return whatever 384df8bae1dSRodney W. Grimes * error the upper layer generated. 385df8bae1dSRodney W. Grimes * 386df8bae1dSRodney W. Grimes * 2. if the top layer failed and the bottom layer succeeded 387df8bae1dSRodney W. Grimes * then two subcases occur. 388df8bae1dSRodney W. Grimes * a. the bottom vnode is not a directory, in which 389df8bae1dSRodney W. Grimes * case just return a new union vnode referencing 390df8bae1dSRodney W. Grimes * an empty top layer and the existing bottom layer. 391df8bae1dSRodney W. Grimes * b. the bottom vnode is a directory, in which case 392df8bae1dSRodney W. Grimes * create a new directory in the top-level and 393df8bae1dSRodney W. Grimes * continue as in case 3. 394df8bae1dSRodney W. Grimes * 395df8bae1dSRodney W. Grimes * 3. if the top layer succeeded then return a new union 396df8bae1dSRodney W. Grimes * vnode referencing whatever the new top layer and 397df8bae1dSRodney W. Grimes * whatever the bottom layer returned. 398df8bae1dSRodney W. Grimes */ 399df8bae1dSRodney W. Grimes 400df8bae1dSRodney W. Grimes *ap->a_vpp = NULLVP; 401df8bae1dSRodney W. Grimes 402df8bae1dSRodney W. Grimes /* case 1. */ 403df8bae1dSRodney W. Grimes if ((uerror != 0) && (lerror != 0)) { 404df8bae1dSRodney W. Grimes return (uerror); 405df8bae1dSRodney W. Grimes } 406df8bae1dSRodney W. Grimes 407df8bae1dSRodney W. Grimes /* case 2. */ 408df8bae1dSRodney W. Grimes if (uerror != 0 /* && (lerror == 0) */ ) { 409df8bae1dSRodney W. Grimes if (lowervp->v_type == VDIR) { /* case 2b. */ 410df8bae1dSRodney W. Grimes dun->un_flags &= ~UN_ULOCK; 411996c772fSJohn Dyson VOP_UNLOCK(upperdvp, 0, p); 412df8bae1dSRodney W. Grimes uerror = union_mkshadow(um, upperdvp, cnp, &uppervp); 413996c772fSJohn Dyson vn_lock(upperdvp, LK_EXCLUSIVE | LK_RETRY, p); 414df8bae1dSRodney W. Grimes dun->un_flags |= UN_ULOCK; 415df8bae1dSRodney W. Grimes 416df8bae1dSRodney W. Grimes if (uerror) { 417996c772fSJohn Dyson if (lowervp != NULLVP) { 418df8bae1dSRodney W. Grimes vput(lowervp); 419df8bae1dSRodney W. Grimes lowervp = NULLVP; 420df8bae1dSRodney W. Grimes } 421df8bae1dSRodney W. Grimes return (uerror); 422df8bae1dSRodney W. Grimes } 423df8bae1dSRodney W. Grimes } 424df8bae1dSRodney W. Grimes } 425df8bae1dSRodney W. Grimes 426996c772fSJohn Dyson if (lowervp != NULLVP) 427996c772fSJohn Dyson VOP_UNLOCK(lowervp, 0, p); 428df8bae1dSRodney W. Grimes 429df8bae1dSRodney W. Grimes error = union_allocvp(ap->a_vpp, dvp->v_mount, dvp, upperdvp, cnp, 430996c772fSJohn Dyson uppervp, lowervp, 1); 431df8bae1dSRodney W. Grimes 432df8bae1dSRodney W. Grimes if (error) { 433996c772fSJohn Dyson if (uppervp != NULLVP) 434df8bae1dSRodney W. Grimes vput(uppervp); 435996c772fSJohn Dyson if (lowervp != NULLVP) 436df8bae1dSRodney W. Grimes vrele(lowervp); 437df8bae1dSRodney W. Grimes } else { 438df8bae1dSRodney W. Grimes if (*ap->a_vpp != dvp) 439df8bae1dSRodney W. Grimes if (!lockparent || !(cnp->cn_flags & ISLASTCN)) 440996c772fSJohn Dyson VOP_UNLOCK(dvp, 0, p); 441df8bae1dSRodney W. Grimes } 442df8bae1dSRodney W. Grimes 443df8bae1dSRodney W. Grimes return (error); 444df8bae1dSRodney W. Grimes } 445df8bae1dSRodney W. Grimes 446c9bf0111SKATO Takenori static int 447df8bae1dSRodney W. Grimes union_create(ap) 448df8bae1dSRodney W. Grimes struct vop_create_args /* { 449df8bae1dSRodney W. Grimes struct vnode *a_dvp; 450df8bae1dSRodney W. Grimes struct vnode **a_vpp; 451df8bae1dSRodney W. Grimes struct componentname *a_cnp; 452df8bae1dSRodney W. Grimes struct vattr *a_vap; 453df8bae1dSRodney W. Grimes } */ *ap; 454df8bae1dSRodney W. Grimes { 455df8bae1dSRodney W. Grimes struct union_node *un = VTOUNION(ap->a_dvp); 456df8bae1dSRodney W. Grimes struct vnode *dvp = un->un_uppervp; 457996c772fSJohn Dyson struct componentname *cnp = ap->a_cnp; 458996c772fSJohn Dyson struct proc *p = cnp->cn_proc; 459df8bae1dSRodney W. Grimes 460996c772fSJohn Dyson if (dvp != NULLVP) { 461df8bae1dSRodney W. Grimes int error; 462df8bae1dSRodney W. Grimes struct vnode *vp; 463996c772fSJohn Dyson struct mount *mp; 464df8bae1dSRodney W. Grimes 465996c772fSJohn Dyson FIXUP(un, p); 466df8bae1dSRodney W. Grimes 467df8bae1dSRodney W. Grimes VREF(dvp); 468a9320ff3SKATO Takenori SETKLOCK(un); 469996c772fSJohn Dyson mp = ap->a_dvp->v_mount; 470df8bae1dSRodney W. Grimes vput(ap->a_dvp); 471a9320ff3SKATO Takenori CLEARKLOCK(un); 472996c772fSJohn Dyson error = VOP_CREATE(dvp, &vp, cnp, ap->a_vap); 473df8bae1dSRodney W. Grimes if (error) 474df8bae1dSRodney W. Grimes return (error); 475df8bae1dSRodney W. Grimes 476996c772fSJohn Dyson error = union_allocvp(ap->a_vpp, mp, NULLVP, NULLVP, cnp, vp, 477996c772fSJohn Dyson NULLVP, 1); 478df8bae1dSRodney W. Grimes if (error) 479df8bae1dSRodney W. Grimes vput(vp); 480df8bae1dSRodney W. Grimes return (error); 481df8bae1dSRodney W. Grimes } 482df8bae1dSRodney W. Grimes 483df8bae1dSRodney W. Grimes vput(ap->a_dvp); 484df8bae1dSRodney W. Grimes return (EROFS); 485df8bae1dSRodney W. Grimes } 486df8bae1dSRodney W. Grimes 487c9bf0111SKATO Takenori static int 488996c772fSJohn Dyson union_whiteout(ap) 489996c772fSJohn Dyson struct vop_whiteout_args /* { 490996c772fSJohn Dyson struct vnode *a_dvp; 491996c772fSJohn Dyson struct componentname *a_cnp; 492996c772fSJohn Dyson int a_flags; 493996c772fSJohn Dyson } */ *ap; 494996c772fSJohn Dyson { 495996c772fSJohn Dyson struct union_node *un = VTOUNION(ap->a_dvp); 496996c772fSJohn Dyson struct componentname *cnp = ap->a_cnp; 497996c772fSJohn Dyson struct proc *p = cnp->cn_proc; 498996c772fSJohn Dyson 499996c772fSJohn Dyson if (un->un_uppervp == NULLVP) 500996c772fSJohn Dyson return (EOPNOTSUPP); 501996c772fSJohn Dyson 502996c772fSJohn Dyson FIXUP(un, p); 503996c772fSJohn Dyson return (VOP_WHITEOUT(un->un_uppervp, cnp, ap->a_flags)); 504996c772fSJohn Dyson } 505996c772fSJohn Dyson 506c9bf0111SKATO Takenori static int 507df8bae1dSRodney W. Grimes union_mknod(ap) 508df8bae1dSRodney W. Grimes struct vop_mknod_args /* { 509df8bae1dSRodney W. Grimes struct vnode *a_dvp; 510df8bae1dSRodney W. Grimes struct vnode **a_vpp; 511df8bae1dSRodney W. Grimes struct componentname *a_cnp; 512df8bae1dSRodney W. Grimes struct vattr *a_vap; 513df8bae1dSRodney W. Grimes } */ *ap; 514df8bae1dSRodney W. Grimes { 515df8bae1dSRodney W. Grimes struct union_node *un = VTOUNION(ap->a_dvp); 516df8bae1dSRodney W. Grimes struct vnode *dvp = un->un_uppervp; 517996c772fSJohn Dyson struct componentname *cnp = ap->a_cnp; 518996c772fSJohn Dyson struct proc *p = cnp->cn_proc; 519df8bae1dSRodney W. Grimes 520996c772fSJohn Dyson if (dvp != NULLVP) { 521df8bae1dSRodney W. Grimes int error; 522df8bae1dSRodney W. Grimes struct vnode *vp; 523996c772fSJohn Dyson struct mount *mp; 524df8bae1dSRodney W. Grimes 525996c772fSJohn Dyson FIXUP(un, p); 526df8bae1dSRodney W. Grimes 527df8bae1dSRodney W. Grimes VREF(dvp); 528a9320ff3SKATO Takenori SETKLOCK(un); 529996c772fSJohn Dyson mp = ap->a_dvp->v_mount; 530df8bae1dSRodney W. Grimes vput(ap->a_dvp); 531a9320ff3SKATO Takenori CLEARKLOCK(un); 532996c772fSJohn Dyson error = VOP_MKNOD(dvp, &vp, cnp, ap->a_vap); 533df8bae1dSRodney W. Grimes if (error) 534df8bae1dSRodney W. Grimes return (error); 535df8bae1dSRodney W. Grimes 536996c772fSJohn Dyson if (vp != NULLVP) { 537996c772fSJohn Dyson error = union_allocvp(ap->a_vpp, mp, NULLVP, NULLVP, 538996c772fSJohn Dyson cnp, vp, NULLVP, 1); 539df8bae1dSRodney W. Grimes if (error) 540df8bae1dSRodney W. Grimes vput(vp); 541df8bae1dSRodney W. Grimes } 542df8bae1dSRodney W. Grimes return (error); 543df8bae1dSRodney W. Grimes } 544df8bae1dSRodney W. Grimes 545df8bae1dSRodney W. Grimes vput(ap->a_dvp); 546df8bae1dSRodney W. Grimes return (EROFS); 547df8bae1dSRodney W. Grimes } 548df8bae1dSRodney W. Grimes 549c9bf0111SKATO Takenori static int 550df8bae1dSRodney W. Grimes union_open(ap) 551df8bae1dSRodney W. Grimes struct vop_open_args /* { 552df8bae1dSRodney W. Grimes struct vnodeop_desc *a_desc; 553df8bae1dSRodney W. Grimes struct vnode *a_vp; 554df8bae1dSRodney W. Grimes int a_mode; 555df8bae1dSRodney W. Grimes struct ucred *a_cred; 556df8bae1dSRodney W. Grimes struct proc *a_p; 557df8bae1dSRodney W. Grimes } */ *ap; 558df8bae1dSRodney W. Grimes { 559df8bae1dSRodney W. Grimes struct union_node *un = VTOUNION(ap->a_vp); 560df8bae1dSRodney W. Grimes struct vnode *tvp; 561df8bae1dSRodney W. Grimes int mode = ap->a_mode; 562df8bae1dSRodney W. Grimes struct ucred *cred = ap->a_cred; 563df8bae1dSRodney W. Grimes struct proc *p = ap->a_p; 564df8bae1dSRodney W. Grimes int error; 565df8bae1dSRodney W. Grimes 566df8bae1dSRodney W. Grimes /* 567df8bae1dSRodney W. Grimes * If there is an existing upper vp then simply open that. 568df8bae1dSRodney W. Grimes */ 569df8bae1dSRodney W. Grimes tvp = un->un_uppervp; 570df8bae1dSRodney W. Grimes if (tvp == NULLVP) { 571df8bae1dSRodney W. Grimes /* 572df8bae1dSRodney W. Grimes * If the lower vnode is being opened for writing, then 573df8bae1dSRodney W. Grimes * copy the file contents to the upper vnode and open that, 574df8bae1dSRodney W. Grimes * otherwise can simply open the lower vnode. 575df8bae1dSRodney W. Grimes */ 576df8bae1dSRodney W. Grimes tvp = un->un_lowervp; 577df8bae1dSRodney W. Grimes if ((ap->a_mode & FWRITE) && (tvp->v_type == VREG)) { 578996c772fSJohn Dyson error = union_copyup(un, (mode&O_TRUNC) == 0, cred, p); 579df8bae1dSRodney W. Grimes if (error == 0) 580df8bae1dSRodney W. Grimes error = VOP_OPEN(un->un_uppervp, mode, cred, p); 581df8bae1dSRodney W. Grimes return (error); 582df8bae1dSRodney W. Grimes } 583df8bae1dSRodney W. Grimes 584df8bae1dSRodney W. Grimes /* 585df8bae1dSRodney W. Grimes * Just open the lower vnode 586df8bae1dSRodney W. Grimes */ 587df8bae1dSRodney W. Grimes un->un_openl++; 588996c772fSJohn Dyson vn_lock(tvp, LK_EXCLUSIVE | LK_RETRY, p); 589df8bae1dSRodney W. Grimes error = VOP_OPEN(tvp, mode, cred, p); 590996c772fSJohn Dyson VOP_UNLOCK(tvp, 0, p); 591df8bae1dSRodney W. Grimes 592df8bae1dSRodney W. Grimes return (error); 593df8bae1dSRodney W. Grimes } 594df8bae1dSRodney W. Grimes 595996c772fSJohn Dyson FIXUP(un, p); 596df8bae1dSRodney W. Grimes 597df8bae1dSRodney W. Grimes error = VOP_OPEN(tvp, mode, cred, p); 598df8bae1dSRodney W. Grimes 599df8bae1dSRodney W. Grimes return (error); 600df8bae1dSRodney W. Grimes } 601df8bae1dSRodney W. Grimes 602c9bf0111SKATO Takenori static int 603df8bae1dSRodney W. Grimes union_close(ap) 604df8bae1dSRodney W. Grimes struct vop_close_args /* { 605df8bae1dSRodney W. Grimes struct vnode *a_vp; 606df8bae1dSRodney W. Grimes int a_fflag; 607df8bae1dSRodney W. Grimes struct ucred *a_cred; 608df8bae1dSRodney W. Grimes struct proc *a_p; 609df8bae1dSRodney W. Grimes } */ *ap; 610df8bae1dSRodney W. Grimes { 611df8bae1dSRodney W. Grimes struct union_node *un = VTOUNION(ap->a_vp); 612df8bae1dSRodney W. Grimes struct vnode *vp; 613df8bae1dSRodney W. Grimes 614996c772fSJohn Dyson if ((vp = un->un_uppervp) == NULLVP) { 615df8bae1dSRodney W. Grimes #ifdef UNION_DIAGNOSTIC 616df8bae1dSRodney W. Grimes if (un->un_openl <= 0) 617df8bae1dSRodney W. Grimes panic("union: un_openl cnt"); 618df8bae1dSRodney W. Grimes #endif 619df8bae1dSRodney W. Grimes --un->un_openl; 620df8bae1dSRodney W. Grimes vp = un->un_lowervp; 621df8bae1dSRodney W. Grimes } 622df8bae1dSRodney W. Grimes 623996c772fSJohn Dyson ap->a_vp = vp; 624996c772fSJohn Dyson return (VCALL(vp, VOFFSET(vop_close), ap)); 625df8bae1dSRodney W. Grimes } 626df8bae1dSRodney W. Grimes 627df8bae1dSRodney W. Grimes /* 628df8bae1dSRodney W. Grimes * Check access permission on the union vnode. 629df8bae1dSRodney W. Grimes * The access check being enforced is to check 630df8bae1dSRodney W. Grimes * against both the underlying vnode, and any 631df8bae1dSRodney W. Grimes * copied vnode. This ensures that no additional 632df8bae1dSRodney W. Grimes * file permissions are given away simply because 633df8bae1dSRodney W. Grimes * the user caused an implicit file copy. 634df8bae1dSRodney W. Grimes */ 635c9bf0111SKATO Takenori static int 636df8bae1dSRodney W. Grimes union_access(ap) 637df8bae1dSRodney W. Grimes struct vop_access_args /* { 638df8bae1dSRodney W. Grimes struct vnodeop_desc *a_desc; 639df8bae1dSRodney W. Grimes struct vnode *a_vp; 640df8bae1dSRodney W. Grimes int a_mode; 641df8bae1dSRodney W. Grimes struct ucred *a_cred; 642df8bae1dSRodney W. Grimes struct proc *a_p; 643df8bae1dSRodney W. Grimes } */ *ap; 644df8bae1dSRodney W. Grimes { 645df8bae1dSRodney W. Grimes struct union_node *un = VTOUNION(ap->a_vp); 646996c772fSJohn Dyson struct proc *p = ap->a_p; 647df8bae1dSRodney W. Grimes int error = EACCES; 648df8bae1dSRodney W. Grimes struct vnode *vp; 6495f3f68a8SKATO Takenori struct vnode *savedvp; 650df8bae1dSRodney W. Grimes 651996c772fSJohn Dyson if ((vp = un->un_uppervp) != NULLVP) { 652996c772fSJohn Dyson FIXUP(un, p); 653996c772fSJohn Dyson ap->a_vp = vp; 654996c772fSJohn Dyson return (VCALL(vp, VOFFSET(vop_access), ap)); 655df8bae1dSRodney W. Grimes } 656df8bae1dSRodney W. Grimes 657996c772fSJohn Dyson if ((vp = un->un_lowervp) != NULLVP) { 658996c772fSJohn Dyson vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); 6595f3f68a8SKATO Takenori savedvp = ap->a_vp; 660996c772fSJohn Dyson ap->a_vp = vp; 661996c772fSJohn Dyson error = VCALL(vp, VOFFSET(vop_access), ap); 662df8bae1dSRodney W. Grimes if (error == 0) { 6635f3f68a8SKATO Takenori struct union_mount *um = MOUNTTOUNIONMOUNT(savedvp->v_mount); 664df8bae1dSRodney W. Grimes 665996c772fSJohn Dyson if (um->um_op == UNMNT_BELOW) { 666996c772fSJohn Dyson ap->a_cred = um->um_cred; 667996c772fSJohn Dyson error = VCALL(vp, VOFFSET(vop_access), ap); 668df8bae1dSRodney W. Grimes } 669996c772fSJohn Dyson } 670996c772fSJohn Dyson VOP_UNLOCK(vp, 0, p); 671df8bae1dSRodney W. Grimes if (error) 672df8bae1dSRodney W. Grimes return (error); 673df8bae1dSRodney W. Grimes } 674df8bae1dSRodney W. Grimes 675df8bae1dSRodney W. Grimes return (error); 676df8bae1dSRodney W. Grimes } 677df8bae1dSRodney W. Grimes 678df8bae1dSRodney W. Grimes /* 679996c772fSJohn Dyson * We handle getattr only to change the fsid and 680996c772fSJohn Dyson * track object sizes 681df8bae1dSRodney W. Grimes */ 682c9bf0111SKATO Takenori static int 683df8bae1dSRodney W. Grimes union_getattr(ap) 684df8bae1dSRodney W. Grimes struct vop_getattr_args /* { 685df8bae1dSRodney W. Grimes struct vnode *a_vp; 686df8bae1dSRodney W. Grimes struct vattr *a_vap; 687df8bae1dSRodney W. Grimes struct ucred *a_cred; 688df8bae1dSRodney W. Grimes struct proc *a_p; 689df8bae1dSRodney W. Grimes } */ *ap; 690df8bae1dSRodney W. Grimes { 691df8bae1dSRodney W. Grimes int error; 692df8bae1dSRodney W. Grimes struct union_node *un = VTOUNION(ap->a_vp); 693df8bae1dSRodney W. Grimes struct vnode *vp = un->un_uppervp; 694996c772fSJohn Dyson struct proc *p = ap->a_p; 695df8bae1dSRodney W. Grimes struct vattr *vap; 696df8bae1dSRodney W. Grimes struct vattr va; 697df8bae1dSRodney W. Grimes 698df8bae1dSRodney W. Grimes 699df8bae1dSRodney W. Grimes /* 700df8bae1dSRodney W. Grimes * Some programs walk the filesystem hierarchy by counting 701df8bae1dSRodney W. Grimes * links to directories to avoid stat'ing all the time. 702df8bae1dSRodney W. Grimes * This means the link count on directories needs to be "correct". 703df8bae1dSRodney W. Grimes * The only way to do that is to call getattr on both layers 704df8bae1dSRodney W. Grimes * and fix up the link count. The link count will not necessarily 705df8bae1dSRodney W. Grimes * be accurate but will be large enough to defeat the tree walkers. 706df8bae1dSRodney W. Grimes */ 707df8bae1dSRodney W. Grimes 708df8bae1dSRodney W. Grimes vap = ap->a_vap; 709df8bae1dSRodney W. Grimes 710df8bae1dSRodney W. Grimes vp = un->un_uppervp; 711df8bae1dSRodney W. Grimes if (vp != NULLVP) { 712996c772fSJohn Dyson /* 713996c772fSJohn Dyson * It's not clear whether VOP_GETATTR is to be 714996c772fSJohn Dyson * called with the vnode locked or not. stat() calls 715996c772fSJohn Dyson * it with (vp) locked, and fstat calls it with 716996c772fSJohn Dyson * (vp) unlocked. 717996c772fSJohn Dyson * In the mean time, compensate here by checking 718996c772fSJohn Dyson * the union_node's lock flag. 719996c772fSJohn Dyson */ 720996c772fSJohn Dyson if (un->un_flags & UN_LOCKED) 721996c772fSJohn Dyson FIXUP(un, p); 722996c772fSJohn Dyson 723df8bae1dSRodney W. Grimes error = VOP_GETATTR(vp, vap, ap->a_cred, ap->a_p); 724df8bae1dSRodney W. Grimes if (error) 725df8bae1dSRodney W. Grimes return (error); 726996c772fSJohn Dyson union_newsize(ap->a_vp, vap->va_size, VNOVAL); 727df8bae1dSRodney W. Grimes } 728df8bae1dSRodney W. Grimes 729df8bae1dSRodney W. Grimes if (vp == NULLVP) { 730df8bae1dSRodney W. Grimes vp = un->un_lowervp; 731df8bae1dSRodney W. Grimes } else if (vp->v_type == VDIR) { 732df8bae1dSRodney W. Grimes vp = un->un_lowervp; 733df8bae1dSRodney W. Grimes vap = &va; 734df8bae1dSRodney W. Grimes } else { 735df8bae1dSRodney W. Grimes vp = NULLVP; 736df8bae1dSRodney W. Grimes } 737df8bae1dSRodney W. Grimes 738df8bae1dSRodney W. Grimes if (vp != NULLVP) { 739df8bae1dSRodney W. Grimes error = VOP_GETATTR(vp, vap, ap->a_cred, ap->a_p); 740df8bae1dSRodney W. Grimes if (error) 741df8bae1dSRodney W. Grimes return (error); 742996c772fSJohn Dyson union_newsize(ap->a_vp, VNOVAL, vap->va_size); 743df8bae1dSRodney W. Grimes } 744df8bae1dSRodney W. Grimes 745df8bae1dSRodney W. Grimes if ((vap != ap->a_vap) && (vap->va_type == VDIR)) 746df8bae1dSRodney W. Grimes ap->a_vap->va_nlink += vap->va_nlink; 747df8bae1dSRodney W. Grimes 748996c772fSJohn Dyson ap->a_vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsid.val[0]; 749df8bae1dSRodney W. Grimes return (0); 750df8bae1dSRodney W. Grimes } 751df8bae1dSRodney W. Grimes 752c9bf0111SKATO Takenori static int 753df8bae1dSRodney W. Grimes union_setattr(ap) 754df8bae1dSRodney W. Grimes struct vop_setattr_args /* { 755df8bae1dSRodney W. Grimes struct vnode *a_vp; 756df8bae1dSRodney W. Grimes struct vattr *a_vap; 757df8bae1dSRodney W. Grimes struct ucred *a_cred; 758df8bae1dSRodney W. Grimes struct proc *a_p; 759df8bae1dSRodney W. Grimes } */ *ap; 760df8bae1dSRodney W. Grimes { 761df8bae1dSRodney W. Grimes struct union_node *un = VTOUNION(ap->a_vp); 762996c772fSJohn Dyson struct proc *p = ap->a_p; 763df8bae1dSRodney W. Grimes int error; 764df8bae1dSRodney W. Grimes 765df8bae1dSRodney W. Grimes /* 766df8bae1dSRodney W. Grimes * Handle case of truncating lower object to zero size, 767df8bae1dSRodney W. Grimes * by creating a zero length upper object. This is to 768df8bae1dSRodney W. Grimes * handle the case of open with O_TRUNC and O_CREAT. 769df8bae1dSRodney W. Grimes */ 770df8bae1dSRodney W. Grimes if ((un->un_uppervp == NULLVP) && 771df8bae1dSRodney W. Grimes /* assert(un->un_lowervp != NULLVP) */ 772996c772fSJohn Dyson (un->un_lowervp->v_type == VREG)) { 773996c772fSJohn Dyson error = union_copyup(un, (ap->a_vap->va_size != 0), 774996c772fSJohn Dyson ap->a_cred, ap->a_p); 775df8bae1dSRodney W. Grimes if (error) 776df8bae1dSRodney W. Grimes return (error); 777df8bae1dSRodney W. Grimes } 778df8bae1dSRodney W. Grimes 779df8bae1dSRodney W. Grimes /* 780df8bae1dSRodney W. Grimes * Try to set attributes in upper layer, 781df8bae1dSRodney W. Grimes * otherwise return read-only filesystem error. 782df8bae1dSRodney W. Grimes */ 783df8bae1dSRodney W. Grimes if (un->un_uppervp != NULLVP) { 784996c772fSJohn Dyson FIXUP(un, p); 785df8bae1dSRodney W. Grimes error = VOP_SETATTR(un->un_uppervp, ap->a_vap, 786df8bae1dSRodney W. Grimes ap->a_cred, ap->a_p); 787996c772fSJohn Dyson if ((error == 0) && (ap->a_vap->va_size != VNOVAL)) 788996c772fSJohn Dyson union_newsize(ap->a_vp, ap->a_vap->va_size, VNOVAL); 789df8bae1dSRodney W. Grimes } else { 790df8bae1dSRodney W. Grimes error = EROFS; 791df8bae1dSRodney W. Grimes } 792df8bae1dSRodney W. Grimes 793df8bae1dSRodney W. Grimes return (error); 794df8bae1dSRodney W. Grimes } 795df8bae1dSRodney W. Grimes 796c9bf0111SKATO Takenori static int 797df8bae1dSRodney W. Grimes union_read(ap) 798df8bae1dSRodney W. Grimes struct vop_read_args /* { 799df8bae1dSRodney W. Grimes struct vnode *a_vp; 800df8bae1dSRodney W. Grimes struct uio *a_uio; 801df8bae1dSRodney W. Grimes int a_ioflag; 802df8bae1dSRodney W. Grimes struct ucred *a_cred; 803df8bae1dSRodney W. Grimes } */ *ap; 804df8bae1dSRodney W. Grimes { 805df8bae1dSRodney W. Grimes int error; 806996c772fSJohn Dyson struct proc *p = ap->a_uio->uio_procp; 807df8bae1dSRodney W. Grimes struct vnode *vp = OTHERVP(ap->a_vp); 808df8bae1dSRodney W. Grimes int dolock = (vp == LOWERVP(ap->a_vp)); 809df8bae1dSRodney W. Grimes 810df8bae1dSRodney W. Grimes if (dolock) 811996c772fSJohn Dyson vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); 812df8bae1dSRodney W. Grimes else 813996c772fSJohn Dyson FIXUP(VTOUNION(ap->a_vp), p); 814df8bae1dSRodney W. Grimes error = VOP_READ(vp, ap->a_uio, ap->a_ioflag, ap->a_cred); 815df8bae1dSRodney W. Grimes if (dolock) 816996c772fSJohn Dyson VOP_UNLOCK(vp, 0, p); 817996c772fSJohn Dyson 818996c772fSJohn Dyson /* 819996c772fSJohn Dyson * XXX 820996c772fSJohn Dyson * perhaps the size of the underlying object has changed under 821996c772fSJohn Dyson * our feet. take advantage of the offset information present 822996c772fSJohn Dyson * in the uio structure. 823996c772fSJohn Dyson */ 824996c772fSJohn Dyson if (error == 0) { 825996c772fSJohn Dyson struct union_node *un = VTOUNION(ap->a_vp); 826996c772fSJohn Dyson off_t cur = ap->a_uio->uio_offset; 827996c772fSJohn Dyson 828996c772fSJohn Dyson if (vp == un->un_uppervp) { 829996c772fSJohn Dyson if (cur > un->un_uppersz) 830996c772fSJohn Dyson union_newsize(ap->a_vp, cur, VNOVAL); 831996c772fSJohn Dyson } else { 832996c772fSJohn Dyson if (cur > un->un_lowersz) 833996c772fSJohn Dyson union_newsize(ap->a_vp, VNOVAL, cur); 834996c772fSJohn Dyson } 835996c772fSJohn Dyson } 836df8bae1dSRodney W. Grimes 837df8bae1dSRodney W. Grimes return (error); 838df8bae1dSRodney W. Grimes } 839df8bae1dSRodney W. Grimes 840c9bf0111SKATO Takenori static int 841df8bae1dSRodney W. Grimes union_write(ap) 842df8bae1dSRodney W. Grimes struct vop_read_args /* { 843df8bae1dSRodney W. Grimes struct vnode *a_vp; 844df8bae1dSRodney W. Grimes struct uio *a_uio; 845df8bae1dSRodney W. Grimes int a_ioflag; 846df8bae1dSRodney W. Grimes struct ucred *a_cred; 847df8bae1dSRodney W. Grimes } */ *ap; 848df8bae1dSRodney W. Grimes { 849df8bae1dSRodney W. Grimes int error; 850996c772fSJohn Dyson struct vnode *vp; 851996c772fSJohn Dyson struct union_node *un = VTOUNION(ap->a_vp); 852996c772fSJohn Dyson struct proc *p = ap->a_uio->uio_procp; 853df8bae1dSRodney W. Grimes 854996c772fSJohn Dyson vp = UPPERVP(ap->a_vp); 855996c772fSJohn Dyson if (vp == NULLVP) 856996c772fSJohn Dyson panic("union: missing upper layer in write"); 857996c772fSJohn Dyson 858996c772fSJohn Dyson FIXUP(un, p); 859df8bae1dSRodney W. Grimes error = VOP_WRITE(vp, ap->a_uio, ap->a_ioflag, ap->a_cred); 860996c772fSJohn Dyson 861996c772fSJohn Dyson /* 862996c772fSJohn Dyson * the size of the underlying object may be changed by the 863996c772fSJohn Dyson * write. 864996c772fSJohn Dyson */ 865996c772fSJohn Dyson if (error == 0) { 866996c772fSJohn Dyson off_t cur = ap->a_uio->uio_offset; 867996c772fSJohn Dyson 868996c772fSJohn Dyson if (cur > un->un_uppersz) 869996c772fSJohn Dyson union_newsize(ap->a_vp, cur, VNOVAL); 870996c772fSJohn Dyson } 871df8bae1dSRodney W. Grimes 872df8bae1dSRodney W. Grimes return (error); 873df8bae1dSRodney W. Grimes } 874df8bae1dSRodney W. Grimes 875c9bf0111SKATO Takenori static int 876996c772fSJohn Dyson union_lease(ap) 877996c772fSJohn Dyson struct vop_lease_args /* { 878996c772fSJohn Dyson struct vnode *a_vp; 879996c772fSJohn Dyson struct proc *a_p; 880996c772fSJohn Dyson struct ucred *a_cred; 881996c772fSJohn Dyson int a_flag; 882996c772fSJohn Dyson } */ *ap; 883996c772fSJohn Dyson { 884996c772fSJohn Dyson register struct vnode *ovp = OTHERVP(ap->a_vp); 885996c772fSJohn Dyson 886996c772fSJohn Dyson ap->a_vp = ovp; 887996c772fSJohn Dyson return (VCALL(ovp, VOFFSET(vop_lease), ap)); 888996c772fSJohn Dyson } 889996c772fSJohn Dyson 890c9bf0111SKATO Takenori static int 891df8bae1dSRodney W. Grimes union_ioctl(ap) 892df8bae1dSRodney W. Grimes struct vop_ioctl_args /* { 893df8bae1dSRodney W. Grimes struct vnode *a_vp; 894df8bae1dSRodney W. Grimes int a_command; 895df8bae1dSRodney W. Grimes caddr_t a_data; 896df8bae1dSRodney W. Grimes int a_fflag; 897df8bae1dSRodney W. Grimes struct ucred *a_cred; 898df8bae1dSRodney W. Grimes struct proc *a_p; 899df8bae1dSRodney W. Grimes } */ *ap; 900df8bae1dSRodney W. Grimes { 901996c772fSJohn Dyson register struct vnode *ovp = OTHERVP(ap->a_vp); 902df8bae1dSRodney W. Grimes 903996c772fSJohn Dyson ap->a_vp = ovp; 904996c772fSJohn Dyson return (VCALL(ovp, VOFFSET(vop_ioctl), ap)); 905df8bae1dSRodney W. Grimes } 906df8bae1dSRodney W. Grimes 907c9bf0111SKATO Takenori static int 908df8bae1dSRodney W. Grimes union_select(ap) 909df8bae1dSRodney W. Grimes struct vop_select_args /* { 910df8bae1dSRodney W. Grimes struct vnode *a_vp; 911df8bae1dSRodney W. Grimes int a_which; 912df8bae1dSRodney W. Grimes int a_fflags; 913df8bae1dSRodney W. Grimes struct ucred *a_cred; 914df8bae1dSRodney W. Grimes struct proc *a_p; 915df8bae1dSRodney W. Grimes } */ *ap; 916df8bae1dSRodney W. Grimes { 917996c772fSJohn Dyson register struct vnode *ovp = OTHERVP(ap->a_vp); 918df8bae1dSRodney W. Grimes 919996c772fSJohn Dyson ap->a_vp = ovp; 920996c772fSJohn Dyson return (VCALL(ovp, VOFFSET(vop_select), ap)); 921996c772fSJohn Dyson } 922996c772fSJohn Dyson 923c9bf0111SKATO Takenori static int 924996c772fSJohn Dyson union_revoke(ap) 925996c772fSJohn Dyson struct vop_revoke_args /* { 926996c772fSJohn Dyson struct vnode *a_vp; 927996c772fSJohn Dyson int a_flags; 928996c772fSJohn Dyson struct proc *a_p; 929996c772fSJohn Dyson } */ *ap; 930996c772fSJohn Dyson { 931996c772fSJohn Dyson struct vnode *vp = ap->a_vp; 932996c772fSJohn Dyson 933996c772fSJohn Dyson if (UPPERVP(vp)) 934996c772fSJohn Dyson VOP_REVOKE(UPPERVP(vp), ap->a_flags); 935996c772fSJohn Dyson if (LOWERVP(vp)) 936996c772fSJohn Dyson VOP_REVOKE(LOWERVP(vp), ap->a_flags); 937996c772fSJohn Dyson vgone(vp); 938996c772fSJohn Dyson return (0); 939df8bae1dSRodney W. Grimes } 940df8bae1dSRodney W. Grimes 941c9bf0111SKATO Takenori static int 942df8bae1dSRodney W. Grimes union_mmap(ap) 943df8bae1dSRodney W. Grimes struct vop_mmap_args /* { 944df8bae1dSRodney W. Grimes struct vnode *a_vp; 945df8bae1dSRodney W. Grimes int a_fflags; 946df8bae1dSRodney W. Grimes struct ucred *a_cred; 947df8bae1dSRodney W. Grimes struct proc *a_p; 948df8bae1dSRodney W. Grimes } */ *ap; 949df8bae1dSRodney W. Grimes { 950996c772fSJohn Dyson register struct vnode *ovp = OTHERVP(ap->a_vp); 951df8bae1dSRodney W. Grimes 952996c772fSJohn Dyson ap->a_vp = ovp; 953996c772fSJohn Dyson return (VCALL(ovp, VOFFSET(vop_mmap), ap)); 954df8bae1dSRodney W. Grimes } 955df8bae1dSRodney W. Grimes 956c9bf0111SKATO Takenori static int 957df8bae1dSRodney W. Grimes union_fsync(ap) 958df8bae1dSRodney W. Grimes struct vop_fsync_args /* { 959df8bae1dSRodney W. Grimes struct vnode *a_vp; 960df8bae1dSRodney W. Grimes struct ucred *a_cred; 961df8bae1dSRodney W. Grimes int a_waitfor; 962df8bae1dSRodney W. Grimes struct proc *a_p; 963df8bae1dSRodney W. Grimes } */ *ap; 964df8bae1dSRodney W. Grimes { 965df8bae1dSRodney W. Grimes int error = 0; 966996c772fSJohn Dyson struct proc *p = ap->a_p; 967df8bae1dSRodney W. Grimes struct vnode *targetvp = OTHERVP(ap->a_vp); 968363ad700SPoul-Henning Kamp struct union_node *un; 969363ad700SPoul-Henning Kamp int isupperlocked = 0; 970df8bae1dSRodney W. Grimes 971996c772fSJohn Dyson if (targetvp != NULLVP) { 972df8bae1dSRodney W. Grimes int dolock = (targetvp == LOWERVP(ap->a_vp)); 973df8bae1dSRodney W. Grimes 974363ad700SPoul-Henning Kamp un = VTOUNION(ap->a_vp); 975df8bae1dSRodney W. Grimes if (dolock) 976996c772fSJohn Dyson vn_lock(targetvp, LK_EXCLUSIVE | LK_RETRY, p); 977363ad700SPoul-Henning Kamp else if ((un->un_flags & UN_ULOCK) == 0 && 978363ad700SPoul-Henning Kamp VOP_ISLOCKED(targetvp) == 0) { 979363ad700SPoul-Henning Kamp isupperlocked = 1; 980363ad700SPoul-Henning Kamp vn_lock(targetvp, LK_EXCLUSIVE | LK_RETRY, p); 981363ad700SPoul-Henning Kamp un->un_flags |= UN_ULOCK; 982363ad700SPoul-Henning Kamp } 983996c772fSJohn Dyson error = VOP_FSYNC(targetvp, ap->a_cred, ap->a_waitfor, p); 984df8bae1dSRodney W. Grimes if (dolock) 985996c772fSJohn Dyson VOP_UNLOCK(targetvp, 0, p); 986363ad700SPoul-Henning Kamp else if (isupperlocked) { 987363ad700SPoul-Henning Kamp un->un_flags &= ~UN_ULOCK; 988a9320ff3SKATO Takenori VOP_UNLOCK(targetvp, 0, p); 989363ad700SPoul-Henning Kamp } 990df8bae1dSRodney W. Grimes } 991df8bae1dSRodney W. Grimes 992df8bae1dSRodney W. Grimes return (error); 993df8bae1dSRodney W. Grimes } 994df8bae1dSRodney W. Grimes 995c9bf0111SKATO Takenori static int 996df8bae1dSRodney W. Grimes union_seek(ap) 997df8bae1dSRodney W. Grimes struct vop_seek_args /* { 998df8bae1dSRodney W. Grimes struct vnode *a_vp; 999df8bae1dSRodney W. Grimes off_t a_oldoff; 1000df8bae1dSRodney W. Grimes off_t a_newoff; 1001df8bae1dSRodney W. Grimes struct ucred *a_cred; 1002df8bae1dSRodney W. Grimes } */ *ap; 1003df8bae1dSRodney W. Grimes { 1004996c772fSJohn Dyson register struct vnode *ovp = OTHERVP(ap->a_vp); 1005df8bae1dSRodney W. Grimes 1006996c772fSJohn Dyson ap->a_vp = ovp; 1007996c772fSJohn Dyson return (VCALL(ovp, VOFFSET(vop_seek), ap)); 1008df8bae1dSRodney W. Grimes } 1009df8bae1dSRodney W. Grimes 1010c9bf0111SKATO Takenori static int 1011df8bae1dSRodney W. Grimes union_remove(ap) 1012df8bae1dSRodney W. Grimes struct vop_remove_args /* { 1013df8bae1dSRodney W. Grimes struct vnode *a_dvp; 1014df8bae1dSRodney W. Grimes struct vnode *a_vp; 1015df8bae1dSRodney W. Grimes struct componentname *a_cnp; 1016df8bae1dSRodney W. Grimes } */ *ap; 1017df8bae1dSRodney W. Grimes { 1018df8bae1dSRodney W. Grimes int error; 1019df8bae1dSRodney W. Grimes struct union_node *dun = VTOUNION(ap->a_dvp); 1020df8bae1dSRodney W. Grimes struct union_node *un = VTOUNION(ap->a_vp); 1021996c772fSJohn Dyson struct componentname *cnp = ap->a_cnp; 1022996c772fSJohn Dyson struct proc *p = cnp->cn_proc; 1023df8bae1dSRodney W. Grimes 1024996c772fSJohn Dyson if (dun->un_uppervp == NULLVP) 1025996c772fSJohn Dyson panic("union remove: null upper vnode"); 1026996c772fSJohn Dyson 1027996c772fSJohn Dyson if (un->un_uppervp != NULLVP) { 1028df8bae1dSRodney W. Grimes struct vnode *dvp = dun->un_uppervp; 1029df8bae1dSRodney W. Grimes struct vnode *vp = un->un_uppervp; 1030df8bae1dSRodney W. Grimes 1031996c772fSJohn Dyson FIXUP(dun, p); 1032df8bae1dSRodney W. Grimes VREF(dvp); 1033a9320ff3SKATO Takenori SETKLOCK(dun); 1034df8bae1dSRodney W. Grimes vput(ap->a_dvp); 1035a9320ff3SKATO Takenori CLEARKLOCK(dun); 1036996c772fSJohn Dyson FIXUP(un, p); 1037df8bae1dSRodney W. Grimes VREF(vp); 1038a9320ff3SKATO Takenori SETKLOCK(un); 1039df8bae1dSRodney W. Grimes vput(ap->a_vp); 1040a9320ff3SKATO Takenori CLEARKLOCK(un); 1041df8bae1dSRodney W. Grimes 1042996c772fSJohn Dyson if (union_dowhiteout(un, cnp->cn_cred, cnp->cn_proc)) 1043996c772fSJohn Dyson cnp->cn_flags |= DOWHITEOUT; 1044996c772fSJohn Dyson error = VOP_REMOVE(dvp, vp, cnp); 1045df8bae1dSRodney W. Grimes if (!error) 1046df8bae1dSRodney W. Grimes union_removed_upper(un); 1047df8bae1dSRodney W. Grimes } else { 1048996c772fSJohn Dyson FIXUP(dun, p); 1049996c772fSJohn Dyson error = union_mkwhiteout( 1050996c772fSJohn Dyson MOUNTTOUNIONMOUNT(UNIONTOV(dun)->v_mount), 1051996c772fSJohn Dyson dun->un_uppervp, ap->a_cnp, un->un_path); 1052df8bae1dSRodney W. Grimes vput(ap->a_dvp); 1053df8bae1dSRodney W. Grimes vput(ap->a_vp); 1054df8bae1dSRodney W. Grimes } 1055df8bae1dSRodney W. Grimes 1056df8bae1dSRodney W. Grimes return (error); 1057df8bae1dSRodney W. Grimes } 1058df8bae1dSRodney W. Grimes 1059c9bf0111SKATO Takenori static int 1060df8bae1dSRodney W. Grimes union_link(ap) 1061df8bae1dSRodney W. Grimes struct vop_link_args /* { 1062df8bae1dSRodney W. Grimes struct vnode *a_tdvp; 106347777413SDavid Greenman struct vnode *a_vp; 1064df8bae1dSRodney W. Grimes struct componentname *a_cnp; 1065df8bae1dSRodney W. Grimes } */ *ap; 1066df8bae1dSRodney W. Grimes { 1067996c772fSJohn Dyson int error = 0; 1068996c772fSJohn Dyson struct componentname *cnp = ap->a_cnp; 1069996c772fSJohn Dyson struct proc *p = cnp->cn_proc; 1070996c772fSJohn Dyson struct union_node *un; 1071996c772fSJohn Dyson struct vnode *vp; 1072996c772fSJohn Dyson struct vnode *tdvp; 1073df8bae1dSRodney W. Grimes 1074996c772fSJohn Dyson un = VTOUNION(ap->a_tdvp); 1075df8bae1dSRodney W. Grimes 1076996c772fSJohn Dyson if (ap->a_tdvp->v_op != ap->a_vp->v_op) { 1077996c772fSJohn Dyson vp = ap->a_vp; 1078df8bae1dSRodney W. Grimes } else { 1079996c772fSJohn Dyson struct union_node *tun = VTOUNION(ap->a_vp); 1080996c772fSJohn Dyson if (tun->un_uppervp == NULLVP) { 1081996c772fSJohn Dyson vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY, p); 1082996c772fSJohn Dyson if (un->un_uppervp == tun->un_dirvp) { 1083996c772fSJohn Dyson un->un_flags &= ~UN_ULOCK; 1084996c772fSJohn Dyson VOP_UNLOCK(un->un_uppervp, 0, p); 1085996c772fSJohn Dyson } 1086996c772fSJohn Dyson error = union_copyup(tun, 1, cnp->cn_cred, p); 1087996c772fSJohn Dyson if (un->un_uppervp == tun->un_dirvp) { 1088996c772fSJohn Dyson vn_lock(un->un_uppervp, 1089996c772fSJohn Dyson LK_EXCLUSIVE | LK_RETRY, p); 1090996c772fSJohn Dyson un->un_flags |= UN_ULOCK; 1091996c772fSJohn Dyson } 1092996c772fSJohn Dyson VOP_UNLOCK(ap->a_vp, 0, p); 1093996c772fSJohn Dyson } 1094996c772fSJohn Dyson vp = tun->un_uppervp; 1095df8bae1dSRodney W. Grimes } 1096df8bae1dSRodney W. Grimes 1097996c772fSJohn Dyson tdvp = un->un_uppervp; 1098996c772fSJohn Dyson if (tdvp == NULLVP) 1099996c772fSJohn Dyson error = EROFS; 1100996c772fSJohn Dyson 1101996c772fSJohn Dyson if (error) { 1102996c772fSJohn Dyson vput(ap->a_tdvp); 1103df8bae1dSRodney W. Grimes return (error); 1104df8bae1dSRodney W. Grimes } 1105df8bae1dSRodney W. Grimes 1106996c772fSJohn Dyson FIXUP(un, p); 1107996c772fSJohn Dyson VREF(tdvp); 1108a9320ff3SKATO Takenori SETKLOCK(un); 1109996c772fSJohn Dyson vput(ap->a_tdvp); 1110a9320ff3SKATO Takenori CLEARKLOCK(un); 1111996c772fSJohn Dyson 111293d71a48SKATO Takenori return (VOP_LINK(tdvp, vp, cnp)); 1113996c772fSJohn Dyson } 1114996c772fSJohn Dyson 1115c9bf0111SKATO Takenori static int 1116df8bae1dSRodney W. Grimes union_rename(ap) 1117df8bae1dSRodney W. Grimes struct vop_rename_args /* { 1118df8bae1dSRodney W. Grimes struct vnode *a_fdvp; 1119df8bae1dSRodney W. Grimes struct vnode *a_fvp; 1120df8bae1dSRodney W. Grimes struct componentname *a_fcnp; 1121df8bae1dSRodney W. Grimes struct vnode *a_tdvp; 1122df8bae1dSRodney W. Grimes struct vnode *a_tvp; 1123df8bae1dSRodney W. Grimes struct componentname *a_tcnp; 1124df8bae1dSRodney W. Grimes } */ *ap; 1125df8bae1dSRodney W. Grimes { 1126df8bae1dSRodney W. Grimes int error; 1127df8bae1dSRodney W. Grimes 1128df8bae1dSRodney W. Grimes struct vnode *fdvp = ap->a_fdvp; 1129df8bae1dSRodney W. Grimes struct vnode *fvp = ap->a_fvp; 1130df8bae1dSRodney W. Grimes struct vnode *tdvp = ap->a_tdvp; 1131df8bae1dSRodney W. Grimes struct vnode *tvp = ap->a_tvp; 1132a9320ff3SKATO Takenori int isklockset = 0; 1133df8bae1dSRodney W. Grimes 1134df8bae1dSRodney W. Grimes if (fdvp->v_op == union_vnodeop_p) { /* always true */ 1135df8bae1dSRodney W. Grimes struct union_node *un = VTOUNION(fdvp); 1136df8bae1dSRodney W. Grimes if (un->un_uppervp == NULLVP) { 1137996c772fSJohn Dyson /* 1138996c772fSJohn Dyson * this should never happen in normal 1139996c772fSJohn Dyson * operation but might if there was 1140996c772fSJohn Dyson * a problem creating the top-level shadow 1141996c772fSJohn Dyson * directory. 1142996c772fSJohn Dyson */ 1143996c772fSJohn Dyson error = EXDEV; 1144df8bae1dSRodney W. Grimes goto bad; 1145df8bae1dSRodney W. Grimes } 1146df8bae1dSRodney W. Grimes 1147df8bae1dSRodney W. Grimes fdvp = un->un_uppervp; 1148df8bae1dSRodney W. Grimes VREF(fdvp); 1149df8bae1dSRodney W. Grimes vrele(ap->a_fdvp); 1150df8bae1dSRodney W. Grimes } 1151df8bae1dSRodney W. Grimes 1152df8bae1dSRodney W. Grimes if (fvp->v_op == union_vnodeop_p) { /* always true */ 1153df8bae1dSRodney W. Grimes struct union_node *un = VTOUNION(fvp); 1154df8bae1dSRodney W. Grimes if (un->un_uppervp == NULLVP) { 1155996c772fSJohn Dyson /* XXX: should do a copyup */ 1156996c772fSJohn Dyson error = EXDEV; 1157df8bae1dSRodney W. Grimes goto bad; 1158df8bae1dSRodney W. Grimes } 1159df8bae1dSRodney W. Grimes 1160996c772fSJohn Dyson if (un->un_lowervp != NULLVP) 1161996c772fSJohn Dyson ap->a_fcnp->cn_flags |= DOWHITEOUT; 1162996c772fSJohn Dyson 1163df8bae1dSRodney W. Grimes fvp = un->un_uppervp; 1164df8bae1dSRodney W. Grimes VREF(fvp); 1165df8bae1dSRodney W. Grimes vrele(ap->a_fvp); 1166df8bae1dSRodney W. Grimes } 1167df8bae1dSRodney W. Grimes 1168df8bae1dSRodney W. Grimes if (tdvp->v_op == union_vnodeop_p) { 1169df8bae1dSRodney W. Grimes struct union_node *un = VTOUNION(tdvp); 1170df8bae1dSRodney W. Grimes if (un->un_uppervp == NULLVP) { 1171996c772fSJohn Dyson /* 1172996c772fSJohn Dyson * this should never happen in normal 1173996c772fSJohn Dyson * operation but might if there was 1174996c772fSJohn Dyson * a problem creating the top-level shadow 1175996c772fSJohn Dyson * directory. 1176996c772fSJohn Dyson */ 1177996c772fSJohn Dyson error = EXDEV; 1178df8bae1dSRodney W. Grimes goto bad; 1179df8bae1dSRodney W. Grimes } 1180df8bae1dSRodney W. Grimes 1181df8bae1dSRodney W. Grimes tdvp = un->un_uppervp; 1182df8bae1dSRodney W. Grimes VREF(tdvp); 1183a9320ff3SKATO Takenori SETKLOCK(un); 1184df8bae1dSRodney W. Grimes vput(ap->a_tdvp); 1185a9320ff3SKATO Takenori CLEARKLOCK(un); 1186df8bae1dSRodney W. Grimes } 1187df8bae1dSRodney W. Grimes 1188996c772fSJohn Dyson if (tvp != NULLVP && tvp->v_op == union_vnodeop_p) { 1189df8bae1dSRodney W. Grimes struct union_node *un = VTOUNION(tvp); 1190df8bae1dSRodney W. Grimes 1191df8bae1dSRodney W. Grimes tvp = un->un_uppervp; 1192996c772fSJohn Dyson if (tvp != NULLVP) { 1193df8bae1dSRodney W. Grimes VREF(tvp); 1194a9320ff3SKATO Takenori SETKLOCK(un); 1195a9320ff3SKATO Takenori isklockset = 1; 1196996c772fSJohn Dyson } 1197df8bae1dSRodney W. Grimes vput(ap->a_tvp); 1198a9320ff3SKATO Takenori if (isklockset) 1199a9320ff3SKATO Takenori CLEARKLOCK(un); 1200df8bae1dSRodney W. Grimes } 1201df8bae1dSRodney W. Grimes 1202df8bae1dSRodney W. Grimes return (VOP_RENAME(fdvp, fvp, ap->a_fcnp, tdvp, tvp, ap->a_tcnp)); 1203df8bae1dSRodney W. Grimes 1204df8bae1dSRodney W. Grimes bad: 1205df8bae1dSRodney W. Grimes vrele(fdvp); 1206df8bae1dSRodney W. Grimes vrele(fvp); 1207df8bae1dSRodney W. Grimes vput(tdvp); 1208996c772fSJohn Dyson if (tvp != NULLVP) 1209df8bae1dSRodney W. Grimes vput(tvp); 1210df8bae1dSRodney W. Grimes 1211df8bae1dSRodney W. Grimes return (error); 1212df8bae1dSRodney W. Grimes } 1213df8bae1dSRodney W. Grimes 1214c9bf0111SKATO Takenori static int 1215df8bae1dSRodney W. Grimes union_mkdir(ap) 1216df8bae1dSRodney W. Grimes struct vop_mkdir_args /* { 1217df8bae1dSRodney W. Grimes struct vnode *a_dvp; 1218df8bae1dSRodney W. Grimes struct vnode **a_vpp; 1219df8bae1dSRodney W. Grimes struct componentname *a_cnp; 1220df8bae1dSRodney W. Grimes struct vattr *a_vap; 1221df8bae1dSRodney W. Grimes } */ *ap; 1222df8bae1dSRodney W. Grimes { 1223df8bae1dSRodney W. Grimes struct union_node *un = VTOUNION(ap->a_dvp); 1224df8bae1dSRodney W. Grimes struct vnode *dvp = un->un_uppervp; 1225996c772fSJohn Dyson struct componentname *cnp = ap->a_cnp; 1226996c772fSJohn Dyson struct proc *p = cnp->cn_proc; 1227df8bae1dSRodney W. Grimes 1228996c772fSJohn Dyson if (dvp != NULLVP) { 1229df8bae1dSRodney W. Grimes int error; 1230df8bae1dSRodney W. Grimes struct vnode *vp; 1231df8bae1dSRodney W. Grimes 1232996c772fSJohn Dyson FIXUP(un, p); 1233df8bae1dSRodney W. Grimes VREF(dvp); 1234a9320ff3SKATO Takenori SETKLOCK(un); 1235996c772fSJohn Dyson VOP_UNLOCK(ap->a_dvp, 0, p); 1236a9320ff3SKATO Takenori CLEARKLOCK(un); 1237996c772fSJohn Dyson error = VOP_MKDIR(dvp, &vp, cnp, ap->a_vap); 1238996c772fSJohn Dyson if (error) { 1239996c772fSJohn Dyson vrele(ap->a_dvp); 1240df8bae1dSRodney W. Grimes return (error); 1241996c772fSJohn Dyson } 1242df8bae1dSRodney W. Grimes 1243996c772fSJohn Dyson error = union_allocvp(ap->a_vpp, ap->a_dvp->v_mount, ap->a_dvp, 1244996c772fSJohn Dyson NULLVP, cnp, vp, NULLVP, 1); 1245996c772fSJohn Dyson vrele(ap->a_dvp); 1246df8bae1dSRodney W. Grimes if (error) 1247df8bae1dSRodney W. Grimes vput(vp); 1248df8bae1dSRodney W. Grimes return (error); 1249df8bae1dSRodney W. Grimes } 1250df8bae1dSRodney W. Grimes 1251df8bae1dSRodney W. Grimes vput(ap->a_dvp); 1252df8bae1dSRodney W. Grimes return (EROFS); 1253df8bae1dSRodney W. Grimes } 1254df8bae1dSRodney W. Grimes 1255c9bf0111SKATO Takenori static int 1256df8bae1dSRodney W. Grimes union_rmdir(ap) 1257df8bae1dSRodney W. Grimes struct vop_rmdir_args /* { 1258df8bae1dSRodney W. Grimes struct vnode *a_dvp; 1259df8bae1dSRodney W. Grimes struct vnode *a_vp; 1260df8bae1dSRodney W. Grimes struct componentname *a_cnp; 1261df8bae1dSRodney W. Grimes } */ *ap; 1262df8bae1dSRodney W. Grimes { 1263df8bae1dSRodney W. Grimes int error; 1264df8bae1dSRodney W. Grimes struct union_node *dun = VTOUNION(ap->a_dvp); 1265df8bae1dSRodney W. Grimes struct union_node *un = VTOUNION(ap->a_vp); 1266996c772fSJohn Dyson struct componentname *cnp = ap->a_cnp; 1267996c772fSJohn Dyson struct proc *p = cnp->cn_proc; 1268df8bae1dSRodney W. Grimes 1269996c772fSJohn Dyson if (dun->un_uppervp == NULLVP) 1270996c772fSJohn Dyson panic("union rmdir: null upper vnode"); 1271996c772fSJohn Dyson 1272996c772fSJohn Dyson if (un->un_uppervp != NULLVP) { 1273df8bae1dSRodney W. Grimes struct vnode *dvp = dun->un_uppervp; 1274df8bae1dSRodney W. Grimes struct vnode *vp = un->un_uppervp; 1275df8bae1dSRodney W. Grimes 1276996c772fSJohn Dyson FIXUP(dun, p); 1277df8bae1dSRodney W. Grimes VREF(dvp); 1278a9320ff3SKATO Takenori SETKLOCK(dun); 1279df8bae1dSRodney W. Grimes vput(ap->a_dvp); 1280a9320ff3SKATO Takenori CLEARKLOCK(dun); 1281996c772fSJohn Dyson FIXUP(un, p); 1282df8bae1dSRodney W. Grimes VREF(vp); 1283a9320ff3SKATO Takenori SETKLOCK(un); 1284df8bae1dSRodney W. Grimes vput(ap->a_vp); 1285a9320ff3SKATO Takenori CLEARKLOCK(un); 1286df8bae1dSRodney W. Grimes 1287996c772fSJohn Dyson if (union_dowhiteout(un, cnp->cn_cred, cnp->cn_proc)) 1288996c772fSJohn Dyson cnp->cn_flags |= DOWHITEOUT; 1289df8bae1dSRodney W. Grimes error = VOP_RMDIR(dvp, vp, ap->a_cnp); 1290df8bae1dSRodney W. Grimes if (!error) 1291df8bae1dSRodney W. Grimes union_removed_upper(un); 1292df8bae1dSRodney W. Grimes } else { 1293996c772fSJohn Dyson FIXUP(dun, p); 1294996c772fSJohn Dyson error = union_mkwhiteout( 1295996c772fSJohn Dyson MOUNTTOUNIONMOUNT(UNIONTOV(dun)->v_mount), 1296996c772fSJohn Dyson dun->un_uppervp, ap->a_cnp, un->un_path); 1297df8bae1dSRodney W. Grimes vput(ap->a_dvp); 1298df8bae1dSRodney W. Grimes vput(ap->a_vp); 1299df8bae1dSRodney W. Grimes } 1300df8bae1dSRodney W. Grimes 1301df8bae1dSRodney W. Grimes return (error); 1302df8bae1dSRodney W. Grimes } 1303df8bae1dSRodney W. Grimes 1304c9bf0111SKATO Takenori static int 1305df8bae1dSRodney W. Grimes union_symlink(ap) 1306df8bae1dSRodney W. Grimes struct vop_symlink_args /* { 1307df8bae1dSRodney W. Grimes struct vnode *a_dvp; 1308df8bae1dSRodney W. Grimes struct vnode **a_vpp; 1309df8bae1dSRodney W. Grimes struct componentname *a_cnp; 1310df8bae1dSRodney W. Grimes struct vattr *a_vap; 1311df8bae1dSRodney W. Grimes char *a_target; 1312df8bae1dSRodney W. Grimes } */ *ap; 1313df8bae1dSRodney W. Grimes { 1314df8bae1dSRodney W. Grimes struct union_node *un = VTOUNION(ap->a_dvp); 1315df8bae1dSRodney W. Grimes struct vnode *dvp = un->un_uppervp; 1316996c772fSJohn Dyson struct componentname *cnp = ap->a_cnp; 1317996c772fSJohn Dyson struct proc *p = cnp->cn_proc; 1318df8bae1dSRodney W. Grimes 1319996c772fSJohn Dyson if (dvp != NULLVP) { 1320df8bae1dSRodney W. Grimes int error; 1321df8bae1dSRodney W. Grimes struct vnode *vp; 1322df8bae1dSRodney W. Grimes 1323996c772fSJohn Dyson FIXUP(un, p); 1324df8bae1dSRodney W. Grimes VREF(dvp); 1325a9320ff3SKATO Takenori SETKLOCK(un); 1326df8bae1dSRodney W. Grimes vput(ap->a_dvp); 1327a9320ff3SKATO Takenori CLEARKLOCK(un); 1328996c772fSJohn Dyson error = VOP_SYMLINK(dvp, &vp, cnp, ap->a_vap, ap->a_target); 1329df8bae1dSRodney W. Grimes *ap->a_vpp = NULLVP; 1330df8bae1dSRodney W. Grimes return (error); 1331df8bae1dSRodney W. Grimes } 1332df8bae1dSRodney W. Grimes 1333df8bae1dSRodney W. Grimes vput(ap->a_dvp); 1334df8bae1dSRodney W. Grimes return (EROFS); 1335df8bae1dSRodney W. Grimes } 1336df8bae1dSRodney W. Grimes 1337df8bae1dSRodney W. Grimes /* 1338df8bae1dSRodney W. Grimes * union_readdir works in concert with getdirentries and 1339df8bae1dSRodney W. Grimes * readdir(3) to provide a list of entries in the unioned 1340df8bae1dSRodney W. Grimes * directories. getdirentries is responsible for walking 1341df8bae1dSRodney W. Grimes * down the union stack. readdir(3) is responsible for 1342df8bae1dSRodney W. Grimes * eliminating duplicate names from the returned data stream. 1343df8bae1dSRodney W. Grimes */ 1344c9bf0111SKATO Takenori static int 1345df8bae1dSRodney W. Grimes union_readdir(ap) 1346df8bae1dSRodney W. Grimes struct vop_readdir_args /* { 1347df8bae1dSRodney W. Grimes struct vnode *a_vp; 1348df8bae1dSRodney W. Grimes struct uio *a_uio; 1349df8bae1dSRodney W. Grimes struct ucred *a_cred; 1350996c772fSJohn Dyson int *a_eofflag; 1351996c772fSJohn Dyson u_long *a_cookies; 1352996c772fSJohn Dyson int a_ncookies; 1353df8bae1dSRodney W. Grimes } */ *ap; 1354df8bae1dSRodney W. Grimes { 1355df8bae1dSRodney W. Grimes struct union_node *un = VTOUNION(ap->a_vp); 1356996c772fSJohn Dyson struct vnode *uvp = un->un_uppervp; 1357996c772fSJohn Dyson struct proc *p = ap->a_uio->uio_procp; 1358df8bae1dSRodney W. Grimes 1359996c772fSJohn Dyson if (uvp == NULLVP) 1360996c772fSJohn Dyson return (0); 1361df8bae1dSRodney W. Grimes 1362996c772fSJohn Dyson FIXUP(un, p); 1363996c772fSJohn Dyson ap->a_vp = uvp; 1364996c772fSJohn Dyson return (VCALL(uvp, VOFFSET(vop_readdir), ap)); 1365df8bae1dSRodney W. Grimes } 1366df8bae1dSRodney W. Grimes 1367c9bf0111SKATO Takenori static int 1368df8bae1dSRodney W. Grimes union_readlink(ap) 1369df8bae1dSRodney W. Grimes struct vop_readlink_args /* { 1370df8bae1dSRodney W. Grimes struct vnode *a_vp; 1371df8bae1dSRodney W. Grimes struct uio *a_uio; 1372df8bae1dSRodney W. Grimes struct ucred *a_cred; 1373df8bae1dSRodney W. Grimes } */ *ap; 1374df8bae1dSRodney W. Grimes { 1375df8bae1dSRodney W. Grimes int error; 1376996c772fSJohn Dyson struct uio *uio = ap->a_uio; 1377996c772fSJohn Dyson struct proc *p = uio->uio_procp; 1378df8bae1dSRodney W. Grimes struct vnode *vp = OTHERVP(ap->a_vp); 1379df8bae1dSRodney W. Grimes int dolock = (vp == LOWERVP(ap->a_vp)); 1380df8bae1dSRodney W. Grimes 1381df8bae1dSRodney W. Grimes if (dolock) 1382996c772fSJohn Dyson vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); 1383df8bae1dSRodney W. Grimes else 1384996c772fSJohn Dyson FIXUP(VTOUNION(ap->a_vp), p); 1385996c772fSJohn Dyson ap->a_vp = vp; 1386996c772fSJohn Dyson error = VCALL(vp, VOFFSET(vop_readlink), ap); 1387df8bae1dSRodney W. Grimes if (dolock) 1388996c772fSJohn Dyson VOP_UNLOCK(vp, 0, p); 1389df8bae1dSRodney W. Grimes 1390df8bae1dSRodney W. Grimes return (error); 1391df8bae1dSRodney W. Grimes } 1392df8bae1dSRodney W. Grimes 1393c9bf0111SKATO Takenori static int 1394df8bae1dSRodney W. Grimes union_abortop(ap) 1395df8bae1dSRodney W. Grimes struct vop_abortop_args /* { 1396df8bae1dSRodney W. Grimes struct vnode *a_dvp; 1397df8bae1dSRodney W. Grimes struct componentname *a_cnp; 1398df8bae1dSRodney W. Grimes } */ *ap; 1399df8bae1dSRodney W. Grimes { 1400df8bae1dSRodney W. Grimes int error; 1401996c772fSJohn Dyson struct componentname *cnp = ap->a_cnp; 1402996c772fSJohn Dyson struct proc *p = cnp->cn_proc; 1403df8bae1dSRodney W. Grimes struct vnode *vp = OTHERVP(ap->a_dvp); 1404df8bae1dSRodney W. Grimes struct union_node *un = VTOUNION(ap->a_dvp); 1405df8bae1dSRodney W. Grimes int islocked = un->un_flags & UN_LOCKED; 1406df8bae1dSRodney W. Grimes int dolock = (vp == LOWERVP(ap->a_dvp)); 1407df8bae1dSRodney W. Grimes 1408df8bae1dSRodney W. Grimes if (islocked) { 1409df8bae1dSRodney W. Grimes if (dolock) 1410996c772fSJohn Dyson vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); 1411df8bae1dSRodney W. Grimes else 1412996c772fSJohn Dyson FIXUP(VTOUNION(ap->a_dvp), p); 1413df8bae1dSRodney W. Grimes } 1414996c772fSJohn Dyson ap->a_dvp = vp; 1415996c772fSJohn Dyson error = VCALL(vp, VOFFSET(vop_abortop), ap); 1416df8bae1dSRodney W. Grimes if (islocked && dolock) 1417996c772fSJohn Dyson VOP_UNLOCK(vp, 0, p); 1418df8bae1dSRodney W. Grimes 1419df8bae1dSRodney W. Grimes return (error); 1420df8bae1dSRodney W. Grimes } 1421df8bae1dSRodney W. Grimes 1422c9bf0111SKATO Takenori static int 1423df8bae1dSRodney W. Grimes union_inactive(ap) 1424df8bae1dSRodney W. Grimes struct vop_inactive_args /* { 1425df8bae1dSRodney W. Grimes struct vnode *a_vp; 1426996c772fSJohn Dyson struct proc *a_p; 1427df8bae1dSRodney W. Grimes } */ *ap; 1428df8bae1dSRodney W. Grimes { 1429996c772fSJohn Dyson struct vnode *vp = ap->a_vp; 1430996c772fSJohn Dyson struct proc *p = ap->a_p; 1431996c772fSJohn Dyson struct union_node *un = VTOUNION(vp); 1432996c772fSJohn Dyson struct vnode **vpp; 1433df8bae1dSRodney W. Grimes 1434df8bae1dSRodney W. Grimes /* 1435df8bae1dSRodney W. Grimes * Do nothing (and _don't_ bypass). 1436df8bae1dSRodney W. Grimes * Wait to vrele lowervp until reclaim, 1437df8bae1dSRodney W. Grimes * so that until then our union_node is in the 1438df8bae1dSRodney W. Grimes * cache and reusable. 1439df8bae1dSRodney W. Grimes * 1440df8bae1dSRodney W. Grimes * NEEDSWORK: Someday, consider inactive'ing 1441df8bae1dSRodney W. Grimes * the lowervp and then trying to reactivate it 1442df8bae1dSRodney W. Grimes * with capabilities (v_id) 1443df8bae1dSRodney W. Grimes * like they do in the name lookup cache code. 1444df8bae1dSRodney W. Grimes * That's too much work for now. 1445df8bae1dSRodney W. Grimes */ 1446df8bae1dSRodney W. Grimes 1447996c772fSJohn Dyson if (un->un_dircache != 0) { 1448996c772fSJohn Dyson for (vpp = un->un_dircache; *vpp != NULLVP; vpp++) 1449996c772fSJohn Dyson vrele(*vpp); 1450996c772fSJohn Dyson free(un->un_dircache, M_TEMP); 1451996c772fSJohn Dyson un->un_dircache = 0; 1452996c772fSJohn Dyson } 1453df8bae1dSRodney W. Grimes 1454996c772fSJohn Dyson VOP_UNLOCK(vp, 0, p); 1455996c772fSJohn Dyson 1456996c772fSJohn Dyson if ((un->un_flags & UN_CACHED) == 0) 1457996c772fSJohn Dyson vgone(vp); 1458df8bae1dSRodney W. Grimes 1459df8bae1dSRodney W. Grimes return (0); 1460df8bae1dSRodney W. Grimes } 1461df8bae1dSRodney W. Grimes 1462c9bf0111SKATO Takenori static int 1463df8bae1dSRodney W. Grimes union_reclaim(ap) 1464df8bae1dSRodney W. Grimes struct vop_reclaim_args /* { 1465df8bae1dSRodney W. Grimes struct vnode *a_vp; 1466df8bae1dSRodney W. Grimes } */ *ap; 1467df8bae1dSRodney W. Grimes { 1468df8bae1dSRodney W. Grimes 1469df8bae1dSRodney W. Grimes union_freevp(ap->a_vp); 1470df8bae1dSRodney W. Grimes 1471df8bae1dSRodney W. Grimes return (0); 1472df8bae1dSRodney W. Grimes } 1473df8bae1dSRodney W. Grimes 1474c9bf0111SKATO Takenori static int 1475df8bae1dSRodney W. Grimes union_lock(ap) 1476df8bae1dSRodney W. Grimes struct vop_lock_args *ap; 1477df8bae1dSRodney W. Grimes { 1478df8bae1dSRodney W. Grimes struct vnode *vp = ap->a_vp; 1479996c772fSJohn Dyson struct proc *p = ap->a_p; 1480996c772fSJohn Dyson int flags = ap->a_flags; 1481df8bae1dSRodney W. Grimes struct union_node *un; 1482996c772fSJohn Dyson int error; 1483996c772fSJohn Dyson 1484996c772fSJohn Dyson vop_nolock(ap); 1485996c772fSJohn Dyson /* 1486996c772fSJohn Dyson * Need to do real lockmgr-style locking here. 1487996c772fSJohn Dyson * in the mean time, draining won't work quite right, 1488996c772fSJohn Dyson * which could lead to a few race conditions. 1489996c772fSJohn Dyson * the following test was here, but is not quite right, we 1490996c772fSJohn Dyson * still need to take the lock: 1491996c772fSJohn Dyson if ((flags & LK_TYPE_MASK) == LK_DRAIN) 1492996c772fSJohn Dyson return (0); 1493996c772fSJohn Dyson */ 1494996c772fSJohn Dyson flags &= ~LK_INTERLOCK; 1495df8bae1dSRodney W. Grimes 1496df8bae1dSRodney W. Grimes start: 1497df8bae1dSRodney W. Grimes un = VTOUNION(vp); 1498df8bae1dSRodney W. Grimes 1499996c772fSJohn Dyson if (un->un_uppervp != NULLVP) { 1500a9320ff3SKATO Takenori if (((un->un_flags & (UN_ULOCK | UN_KLOCK)) == 0) && 1501996c772fSJohn Dyson (vp->v_usecount != 0)) { 1502c9bf0111SKATO Takenori if (0 && VOP_ISLOCKED(un->un_uppervp)) { 150318b4c8e2SKATO Takenori /* 1504a9320ff3SKATO Takenori * XXX 1505a18ac0ffSKATO Takenori * Erm, we find race! 150618b4c8e2SKATO Takenori */ 150718b4c8e2SKATO Takenori #ifdef DIAGNOSTIC 150832cfb899SKATO Takenori panic("union_lock: upper vnode is locked, " 150918b4c8e2SKATO Takenori "but UN_UNLOCK is not set."); 151018b4c8e2SKATO Takenori #endif 1511a9320ff3SKATO Takenori un->un_flags |= UN_ULOCK; 151218b4c8e2SKATO Takenori } else { 1513996c772fSJohn Dyson error = vn_lock(un->un_uppervp, flags, p); 1514996c772fSJohn Dyson if (error) 1515996c772fSJohn Dyson return (error); 1516df8bae1dSRodney W. Grimes un->un_flags |= UN_ULOCK; 1517df8bae1dSRodney W. Grimes } 151818b4c8e2SKATO Takenori } 1519df8bae1dSRodney W. Grimes } 1520df8bae1dSRodney W. Grimes 1521df8bae1dSRodney W. Grimes if (un->un_flags & UN_LOCKED) { 1522df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC 1523df8bae1dSRodney W. Grimes if (curproc && un->un_pid == curproc->p_pid && 1524df8bae1dSRodney W. Grimes un->un_pid > -1 && curproc->p_pid > -1) 1525df8bae1dSRodney W. Grimes panic("union: locking against myself"); 1526df8bae1dSRodney W. Grimes #endif 1527df8bae1dSRodney W. Grimes un->un_flags |= UN_WANT; 1528996c772fSJohn Dyson tsleep((caddr_t)&un->un_flags, PINOD, "unionlk2", 0); 1529df8bae1dSRodney W. Grimes goto start; 1530df8bae1dSRodney W. Grimes } 1531df8bae1dSRodney W. Grimes 1532df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC 1533df8bae1dSRodney W. Grimes if (curproc) 1534df8bae1dSRodney W. Grimes un->un_pid = curproc->p_pid; 1535df8bae1dSRodney W. Grimes else 1536df8bae1dSRodney W. Grimes un->un_pid = -1; 1537df8bae1dSRodney W. Grimes #endif 1538df8bae1dSRodney W. Grimes 1539df8bae1dSRodney W. Grimes un->un_flags |= UN_LOCKED; 1540df8bae1dSRodney W. Grimes return (0); 1541df8bae1dSRodney W. Grimes } 1542df8bae1dSRodney W. Grimes 1543996c772fSJohn Dyson /* 1544996c772fSJohn Dyson * When operations want to vput() a union node yet retain a lock on 1545996c772fSJohn Dyson * the upper vnode (say, to do some further operations like link(), 1546996c772fSJohn Dyson * mkdir(), ...), they set UN_KLOCK on the union node, then call 1547996c772fSJohn Dyson * vput() which calls VOP_UNLOCK() and comes here. union_unlock() 1548996c772fSJohn Dyson * unlocks the union node (leaving the upper vnode alone), clears the 1549996c772fSJohn Dyson * KLOCK flag, and then returns to vput(). The caller then does whatever 1550996c772fSJohn Dyson * is left to do with the upper vnode, and ensures that it gets unlocked. 1551996c772fSJohn Dyson * 1552996c772fSJohn Dyson * If UN_KLOCK isn't set, then the upper vnode is unlocked here. 1553996c772fSJohn Dyson */ 1554c9bf0111SKATO Takenori static int 1555df8bae1dSRodney W. Grimes union_unlock(ap) 1556996c772fSJohn Dyson struct vop_unlock_args /* { 1557996c772fSJohn Dyson struct vnode *a_vp; 1558996c772fSJohn Dyson int a_flags; 1559996c772fSJohn Dyson struct proc *a_p; 1560996c772fSJohn Dyson } */ *ap; 1561df8bae1dSRodney W. Grimes { 1562df8bae1dSRodney W. Grimes struct union_node *un = VTOUNION(ap->a_vp); 1563996c772fSJohn Dyson struct proc *p = ap->a_p; 1564df8bae1dSRodney W. Grimes 1565df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC 1566df8bae1dSRodney W. Grimes if ((un->un_flags & UN_LOCKED) == 0) 1567df8bae1dSRodney W. Grimes panic("union: unlock unlocked node"); 1568df8bae1dSRodney W. Grimes if (curproc && un->un_pid != curproc->p_pid && 1569df8bae1dSRodney W. Grimes curproc->p_pid > -1 && un->un_pid > -1) 1570df8bae1dSRodney W. Grimes panic("union: unlocking other process's union node"); 1571df8bae1dSRodney W. Grimes #endif 1572df8bae1dSRodney W. Grimes 1573df8bae1dSRodney W. Grimes un->un_flags &= ~UN_LOCKED; 1574df8bae1dSRodney W. Grimes 1575df8bae1dSRodney W. Grimes if ((un->un_flags & (UN_ULOCK|UN_KLOCK)) == UN_ULOCK) 1576996c772fSJohn Dyson VOP_UNLOCK(un->un_uppervp, 0, p); 1577df8bae1dSRodney W. Grimes 1578a9320ff3SKATO Takenori un->un_flags &= ~UN_ULOCK; 1579df8bae1dSRodney W. Grimes 1580df8bae1dSRodney W. Grimes if (un->un_flags & UN_WANT) { 1581df8bae1dSRodney W. Grimes un->un_flags &= ~UN_WANT; 1582df8bae1dSRodney W. Grimes wakeup((caddr_t) &un->un_flags); 1583df8bae1dSRodney W. Grimes } 1584df8bae1dSRodney W. Grimes 1585df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC 1586df8bae1dSRodney W. Grimes un->un_pid = 0; 1587df8bae1dSRodney W. Grimes #endif 1588996c772fSJohn Dyson vop_nounlock(ap); 1589df8bae1dSRodney W. Grimes 1590df8bae1dSRodney W. Grimes return (0); 1591df8bae1dSRodney W. Grimes } 1592df8bae1dSRodney W. Grimes 1593c9bf0111SKATO Takenori static int 1594df8bae1dSRodney W. Grimes union_bmap(ap) 1595df8bae1dSRodney W. Grimes struct vop_bmap_args /* { 1596df8bae1dSRodney W. Grimes struct vnode *a_vp; 1597df8bae1dSRodney W. Grimes daddr_t a_bn; 1598df8bae1dSRodney W. Grimes struct vnode **a_vpp; 1599df8bae1dSRodney W. Grimes daddr_t *a_bnp; 1600df8bae1dSRodney W. Grimes int *a_runp; 1601c83ebe77SJohn Dyson int *a_runb; 1602df8bae1dSRodney W. Grimes } */ *ap; 1603df8bae1dSRodney W. Grimes { 1604df8bae1dSRodney W. Grimes int error; 1605996c772fSJohn Dyson struct proc *p = curproc; /* XXX */ 1606df8bae1dSRodney W. Grimes struct vnode *vp = OTHERVP(ap->a_vp); 1607df8bae1dSRodney W. Grimes int dolock = (vp == LOWERVP(ap->a_vp)); 1608df8bae1dSRodney W. Grimes 1609df8bae1dSRodney W. Grimes if (dolock) 1610996c772fSJohn Dyson vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); 1611df8bae1dSRodney W. Grimes else 1612996c772fSJohn Dyson FIXUP(VTOUNION(ap->a_vp), p); 1613996c772fSJohn Dyson ap->a_vp = vp; 1614996c772fSJohn Dyson error = VCALL(vp, VOFFSET(vop_bmap), ap); 1615df8bae1dSRodney W. Grimes if (dolock) 1616996c772fSJohn Dyson VOP_UNLOCK(vp, 0, p); 1617df8bae1dSRodney W. Grimes 1618df8bae1dSRodney W. Grimes return (error); 1619df8bae1dSRodney W. Grimes } 1620df8bae1dSRodney W. Grimes 1621c9bf0111SKATO Takenori static int 1622df8bae1dSRodney W. Grimes union_print(ap) 1623df8bae1dSRodney W. Grimes struct vop_print_args /* { 1624df8bae1dSRodney W. Grimes struct vnode *a_vp; 1625df8bae1dSRodney W. Grimes } */ *ap; 1626df8bae1dSRodney W. Grimes { 1627df8bae1dSRodney W. Grimes struct vnode *vp = ap->a_vp; 1628df8bae1dSRodney W. Grimes 16293a773ad0SPoul-Henning Kamp printf("\ttag VT_UNION, vp=%p, uppervp=%p, lowervp=%p\n", 1630df8bae1dSRodney W. Grimes vp, UPPERVP(vp), LOWERVP(vp)); 1631996c772fSJohn Dyson if (UPPERVP(vp) != NULLVP) 1632996c772fSJohn Dyson vprint("union: upper", UPPERVP(vp)); 1633996c772fSJohn Dyson if (LOWERVP(vp) != NULLVP) 1634996c772fSJohn Dyson vprint("union: lower", LOWERVP(vp)); 1635996c772fSJohn Dyson 1636df8bae1dSRodney W. Grimes return (0); 1637df8bae1dSRodney W. Grimes } 1638df8bae1dSRodney W. Grimes 1639c9bf0111SKATO Takenori static int 1640df8bae1dSRodney W. Grimes union_islocked(ap) 1641df8bae1dSRodney W. Grimes struct vop_islocked_args /* { 1642df8bae1dSRodney W. Grimes struct vnode *a_vp; 1643df8bae1dSRodney W. Grimes } */ *ap; 1644df8bae1dSRodney W. Grimes { 1645df8bae1dSRodney W. Grimes 1646df8bae1dSRodney W. Grimes return ((VTOUNION(ap->a_vp)->un_flags & UN_LOCKED) ? 1 : 0); 1647df8bae1dSRodney W. Grimes } 1648df8bae1dSRodney W. Grimes 1649c9bf0111SKATO Takenori static int 1650df8bae1dSRodney W. Grimes union_pathconf(ap) 1651df8bae1dSRodney W. Grimes struct vop_pathconf_args /* { 1652df8bae1dSRodney W. Grimes struct vnode *a_vp; 1653df8bae1dSRodney W. Grimes int a_name; 1654df8bae1dSRodney W. Grimes int *a_retval; 1655df8bae1dSRodney W. Grimes } */ *ap; 1656df8bae1dSRodney W. Grimes { 1657df8bae1dSRodney W. Grimes int error; 1658996c772fSJohn Dyson struct proc *p = curproc; /* XXX */ 1659df8bae1dSRodney W. Grimes struct vnode *vp = OTHERVP(ap->a_vp); 1660df8bae1dSRodney W. Grimes int dolock = (vp == LOWERVP(ap->a_vp)); 1661df8bae1dSRodney W. Grimes 1662df8bae1dSRodney W. Grimes if (dolock) 1663996c772fSJohn Dyson vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); 1664df8bae1dSRodney W. Grimes else 1665996c772fSJohn Dyson FIXUP(VTOUNION(ap->a_vp), p); 1666996c772fSJohn Dyson ap->a_vp = vp; 1667996c772fSJohn Dyson error = VCALL(vp, VOFFSET(vop_pathconf), ap); 1668df8bae1dSRodney W. Grimes if (dolock) 1669996c772fSJohn Dyson VOP_UNLOCK(vp, 0, p); 1670df8bae1dSRodney W. Grimes 1671df8bae1dSRodney W. Grimes return (error); 1672df8bae1dSRodney W. Grimes } 1673df8bae1dSRodney W. Grimes 1674c9bf0111SKATO Takenori static int 1675df8bae1dSRodney W. Grimes union_advlock(ap) 1676df8bae1dSRodney W. Grimes struct vop_advlock_args /* { 1677df8bae1dSRodney W. Grimes struct vnode *a_vp; 1678df8bae1dSRodney W. Grimes caddr_t a_id; 1679df8bae1dSRodney W. Grimes int a_op; 1680df8bae1dSRodney W. Grimes struct flock *a_fl; 1681df8bae1dSRodney W. Grimes int a_flags; 1682df8bae1dSRodney W. Grimes } */ *ap; 1683df8bae1dSRodney W. Grimes { 1684996c772fSJohn Dyson register struct vnode *ovp = OTHERVP(ap->a_vp); 1685df8bae1dSRodney W. Grimes 1686996c772fSJohn Dyson ap->a_vp = ovp; 1687996c772fSJohn Dyson return (VCALL(ovp, VOFFSET(vop_advlock), ap)); 1688df8bae1dSRodney W. Grimes } 1689df8bae1dSRodney W. Grimes 1690df8bae1dSRodney W. Grimes 1691df8bae1dSRodney W. Grimes /* 1692df8bae1dSRodney W. Grimes * XXX - vop_strategy must be hand coded because it has no 1693df8bae1dSRodney W. Grimes * vnode in its arguments. 1694df8bae1dSRodney W. Grimes * This goes away with a merged VM/buffer cache. 1695df8bae1dSRodney W. Grimes */ 1696c9bf0111SKATO Takenori static int 1697df8bae1dSRodney W. Grimes union_strategy(ap) 1698df8bae1dSRodney W. Grimes struct vop_strategy_args /* { 1699df8bae1dSRodney W. Grimes struct buf *a_bp; 1700df8bae1dSRodney W. Grimes } */ *ap; 1701df8bae1dSRodney W. Grimes { 1702df8bae1dSRodney W. Grimes struct buf *bp = ap->a_bp; 1703df8bae1dSRodney W. Grimes int error; 1704df8bae1dSRodney W. Grimes struct vnode *savedvp; 1705df8bae1dSRodney W. Grimes 1706df8bae1dSRodney W. Grimes savedvp = bp->b_vp; 1707df8bae1dSRodney W. Grimes bp->b_vp = OTHERVP(bp->b_vp); 1708df8bae1dSRodney W. Grimes 1709df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC 1710df8bae1dSRodney W. Grimes if (bp->b_vp == NULLVP) 1711df8bae1dSRodney W. Grimes panic("union_strategy: nil vp"); 1712df8bae1dSRodney W. Grimes if (((bp->b_flags & B_READ) == 0) && 1713df8bae1dSRodney W. Grimes (bp->b_vp == LOWERVP(savedvp))) 1714df8bae1dSRodney W. Grimes panic("union_strategy: writing to lowervp"); 1715df8bae1dSRodney W. Grimes #endif 1716df8bae1dSRodney W. Grimes 1717df8bae1dSRodney W. Grimes error = VOP_STRATEGY(bp); 1718df8bae1dSRodney W. Grimes bp->b_vp = savedvp; 1719df8bae1dSRodney W. Grimes 1720df8bae1dSRodney W. Grimes return (error); 1721df8bae1dSRodney W. Grimes } 1722df8bae1dSRodney W. Grimes 1723df8bae1dSRodney W. Grimes /* 1724df8bae1dSRodney W. Grimes * Global vfs data structures 1725df8bae1dSRodney W. Grimes */ 1726f57e6547SBruce Evans vop_t **union_vnodeop_p; 1727df8bae1dSRodney W. Grimes struct vnodeopv_entry_desc union_vnodeop_entries[] = { 1728f57e6547SBruce Evans { &vop_default_desc, (vop_t *)vn_default_error }, 1729f57e6547SBruce Evans { &vop_lookup_desc, (vop_t *)union_lookup }, /* lookup */ 1730f57e6547SBruce Evans { &vop_create_desc, (vop_t *)union_create }, /* create */ 1731996c772fSJohn Dyson { &vop_whiteout_desc, (vop_t *)union_whiteout }, /* whiteout */ 1732f57e6547SBruce Evans { &vop_mknod_desc, (vop_t *)union_mknod }, /* mknod */ 1733f57e6547SBruce Evans { &vop_open_desc, (vop_t *)union_open }, /* open */ 1734f57e6547SBruce Evans { &vop_close_desc, (vop_t *)union_close }, /* close */ 1735f57e6547SBruce Evans { &vop_access_desc, (vop_t *)union_access }, /* access */ 1736f57e6547SBruce Evans { &vop_getattr_desc, (vop_t *)union_getattr }, /* getattr */ 1737f57e6547SBruce Evans { &vop_setattr_desc, (vop_t *)union_setattr }, /* setattr */ 1738f57e6547SBruce Evans { &vop_read_desc, (vop_t *)union_read }, /* read */ 1739f57e6547SBruce Evans { &vop_write_desc, (vop_t *)union_write }, /* write */ 1740996c772fSJohn Dyson { &vop_lease_desc, (vop_t *)union_lease }, /* lease */ 1741f57e6547SBruce Evans { &vop_ioctl_desc, (vop_t *)union_ioctl }, /* ioctl */ 1742f57e6547SBruce Evans { &vop_select_desc, (vop_t *)union_select }, /* select */ 1743996c772fSJohn Dyson { &vop_revoke_desc, (vop_t *)union_revoke }, /* revoke */ 1744f57e6547SBruce Evans { &vop_mmap_desc, (vop_t *)union_mmap }, /* mmap */ 1745f57e6547SBruce Evans { &vop_fsync_desc, (vop_t *)union_fsync }, /* fsync */ 1746f57e6547SBruce Evans { &vop_seek_desc, (vop_t *)union_seek }, /* seek */ 1747f57e6547SBruce Evans { &vop_remove_desc, (vop_t *)union_remove }, /* remove */ 1748f57e6547SBruce Evans { &vop_link_desc, (vop_t *)union_link }, /* link */ 1749f57e6547SBruce Evans { &vop_rename_desc, (vop_t *)union_rename }, /* rename */ 1750f57e6547SBruce Evans { &vop_mkdir_desc, (vop_t *)union_mkdir }, /* mkdir */ 1751f57e6547SBruce Evans { &vop_rmdir_desc, (vop_t *)union_rmdir }, /* rmdir */ 1752f57e6547SBruce Evans { &vop_symlink_desc, (vop_t *)union_symlink }, /* symlink */ 1753f57e6547SBruce Evans { &vop_readdir_desc, (vop_t *)union_readdir }, /* readdir */ 1754f57e6547SBruce Evans { &vop_readlink_desc, (vop_t *)union_readlink }, /* readlink */ 1755f57e6547SBruce Evans { &vop_abortop_desc, (vop_t *)union_abortop }, /* abortop */ 1756f57e6547SBruce Evans { &vop_inactive_desc, (vop_t *)union_inactive }, /* inactive */ 1757f57e6547SBruce Evans { &vop_reclaim_desc, (vop_t *)union_reclaim }, /* reclaim */ 1758f57e6547SBruce Evans { &vop_lock_desc, (vop_t *)union_lock }, /* lock */ 1759f57e6547SBruce Evans { &vop_unlock_desc, (vop_t *)union_unlock }, /* unlock */ 1760f57e6547SBruce Evans { &vop_bmap_desc, (vop_t *)union_bmap }, /* bmap */ 1761f57e6547SBruce Evans { &vop_strategy_desc, (vop_t *)union_strategy }, /* strategy */ 1762f57e6547SBruce Evans { &vop_print_desc, (vop_t *)union_print }, /* print */ 1763f57e6547SBruce Evans { &vop_islocked_desc, (vop_t *)union_islocked }, /* islocked */ 1764f57e6547SBruce Evans { &vop_pathconf_desc, (vop_t *)union_pathconf }, /* pathconf */ 1765f57e6547SBruce Evans { &vop_advlock_desc, (vop_t *)union_advlock }, /* advlock */ 1766df8bae1dSRodney W. Grimes #ifdef notdef 1767f57e6547SBruce Evans { &vop_blkatoff_desc, (vop_t *)union_blkatoff }, /* blkatoff */ 1768f57e6547SBruce Evans { &vop_valloc_desc, (vop_t *)union_valloc }, /* valloc */ 1769f57e6547SBruce Evans { &vop_vfree_desc, (vop_t *)union_vfree }, /* vfree */ 1770f57e6547SBruce Evans { &vop_truncate_desc, (vop_t *)union_truncate }, /* truncate */ 1771f57e6547SBruce Evans { &vop_update_desc, (vop_t *)union_update }, /* update */ 1772f57e6547SBruce Evans { &vop_bwrite_desc, (vop_t *)union_bwrite }, /* bwrite */ 1773df8bae1dSRodney W. Grimes #endif 1774f57e6547SBruce Evans { NULL, NULL } 1775df8bae1dSRodney W. Grimes }; 1776df8bae1dSRodney W. Grimes struct vnodeopv_desc union_vnodeop_opv_desc = 1777df8bae1dSRodney W. Grimes { &union_vnodeop_p, union_vnodeop_entries }; 1778c901836cSGarrett Wollman 1779c901836cSGarrett Wollman VNODEOP_SET(union_vnodeop_opv_desc); 1780