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 38f5a5311eSKATO Takenori * $Id: union_vnops.c,v 1.37 1997/08/15 02:35:00 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); 441f5a5311eSKATO Takenori #ifdef DIAGNOSTIC 442f5a5311eSKATO Takenori if (cnp->cn_namelen == 1 && 443f5a5311eSKATO Takenori cnp->cn_nameptr[0] == '.' && 444f5a5311eSKATO Takenori *ap->a_vpp != dvp) { 445f5a5311eSKATO Takenori panic("union_lookup returning . (%p) not same as startdir (%p)", 446f5a5311eSKATO Takenori ap->a_vpp, dvp); 447f5a5311eSKATO Takenori } 448f5a5311eSKATO Takenori #endif 449df8bae1dSRodney W. Grimes } 450df8bae1dSRodney W. Grimes 451df8bae1dSRodney W. Grimes return (error); 452df8bae1dSRodney W. Grimes } 453df8bae1dSRodney W. Grimes 454c9bf0111SKATO Takenori static int 455df8bae1dSRodney W. Grimes union_create(ap) 456df8bae1dSRodney W. Grimes struct vop_create_args /* { 457df8bae1dSRodney W. Grimes struct vnode *a_dvp; 458df8bae1dSRodney W. Grimes struct vnode **a_vpp; 459df8bae1dSRodney W. Grimes struct componentname *a_cnp; 460df8bae1dSRodney W. Grimes struct vattr *a_vap; 461df8bae1dSRodney W. Grimes } */ *ap; 462df8bae1dSRodney W. Grimes { 463df8bae1dSRodney W. Grimes struct union_node *un = VTOUNION(ap->a_dvp); 464df8bae1dSRodney W. Grimes struct vnode *dvp = un->un_uppervp; 465996c772fSJohn Dyson struct componentname *cnp = ap->a_cnp; 466996c772fSJohn Dyson struct proc *p = cnp->cn_proc; 467df8bae1dSRodney W. Grimes 468996c772fSJohn Dyson if (dvp != NULLVP) { 469df8bae1dSRodney W. Grimes int error; 470df8bae1dSRodney W. Grimes struct vnode *vp; 471996c772fSJohn Dyson struct mount *mp; 472df8bae1dSRodney W. Grimes 473996c772fSJohn Dyson FIXUP(un, p); 474df8bae1dSRodney W. Grimes 475df8bae1dSRodney W. Grimes VREF(dvp); 476a9320ff3SKATO Takenori SETKLOCK(un); 477996c772fSJohn Dyson mp = ap->a_dvp->v_mount; 478df8bae1dSRodney W. Grimes vput(ap->a_dvp); 479a9320ff3SKATO Takenori CLEARKLOCK(un); 480996c772fSJohn Dyson error = VOP_CREATE(dvp, &vp, cnp, ap->a_vap); 481df8bae1dSRodney W. Grimes if (error) 482df8bae1dSRodney W. Grimes return (error); 483df8bae1dSRodney W. Grimes 484996c772fSJohn Dyson error = union_allocvp(ap->a_vpp, mp, NULLVP, NULLVP, cnp, vp, 485996c772fSJohn Dyson NULLVP, 1); 486df8bae1dSRodney W. Grimes if (error) 487df8bae1dSRodney W. Grimes vput(vp); 488df8bae1dSRodney W. Grimes return (error); 489df8bae1dSRodney W. Grimes } 490df8bae1dSRodney W. Grimes 491df8bae1dSRodney W. Grimes vput(ap->a_dvp); 492df8bae1dSRodney W. Grimes return (EROFS); 493df8bae1dSRodney W. Grimes } 494df8bae1dSRodney W. Grimes 495c9bf0111SKATO Takenori static int 496996c772fSJohn Dyson union_whiteout(ap) 497996c772fSJohn Dyson struct vop_whiteout_args /* { 498996c772fSJohn Dyson struct vnode *a_dvp; 499996c772fSJohn Dyson struct componentname *a_cnp; 500996c772fSJohn Dyson int a_flags; 501996c772fSJohn Dyson } */ *ap; 502996c772fSJohn Dyson { 503996c772fSJohn Dyson struct union_node *un = VTOUNION(ap->a_dvp); 504996c772fSJohn Dyson struct componentname *cnp = ap->a_cnp; 505996c772fSJohn Dyson struct proc *p = cnp->cn_proc; 506996c772fSJohn Dyson 507996c772fSJohn Dyson if (un->un_uppervp == NULLVP) 508996c772fSJohn Dyson return (EOPNOTSUPP); 509996c772fSJohn Dyson 510996c772fSJohn Dyson FIXUP(un, p); 511996c772fSJohn Dyson return (VOP_WHITEOUT(un->un_uppervp, cnp, ap->a_flags)); 512996c772fSJohn Dyson } 513996c772fSJohn Dyson 514c9bf0111SKATO Takenori static int 515df8bae1dSRodney W. Grimes union_mknod(ap) 516df8bae1dSRodney W. Grimes struct vop_mknod_args /* { 517df8bae1dSRodney W. Grimes struct vnode *a_dvp; 518df8bae1dSRodney W. Grimes struct vnode **a_vpp; 519df8bae1dSRodney W. Grimes struct componentname *a_cnp; 520df8bae1dSRodney W. Grimes struct vattr *a_vap; 521df8bae1dSRodney W. Grimes } */ *ap; 522df8bae1dSRodney W. Grimes { 523df8bae1dSRodney W. Grimes struct union_node *un = VTOUNION(ap->a_dvp); 524df8bae1dSRodney W. Grimes struct vnode *dvp = un->un_uppervp; 525996c772fSJohn Dyson struct componentname *cnp = ap->a_cnp; 526996c772fSJohn Dyson struct proc *p = cnp->cn_proc; 527df8bae1dSRodney W. Grimes 528996c772fSJohn Dyson if (dvp != NULLVP) { 529df8bae1dSRodney W. Grimes int error; 530df8bae1dSRodney W. Grimes struct vnode *vp; 531996c772fSJohn Dyson struct mount *mp; 532df8bae1dSRodney W. Grimes 533996c772fSJohn Dyson FIXUP(un, p); 534df8bae1dSRodney W. Grimes 535df8bae1dSRodney W. Grimes VREF(dvp); 536a9320ff3SKATO Takenori SETKLOCK(un); 537996c772fSJohn Dyson mp = ap->a_dvp->v_mount; 538df8bae1dSRodney W. Grimes vput(ap->a_dvp); 539a9320ff3SKATO Takenori CLEARKLOCK(un); 540996c772fSJohn Dyson error = VOP_MKNOD(dvp, &vp, cnp, ap->a_vap); 541df8bae1dSRodney W. Grimes if (error) 542df8bae1dSRodney W. Grimes return (error); 543df8bae1dSRodney W. Grimes 544996c772fSJohn Dyson if (vp != NULLVP) { 545996c772fSJohn Dyson error = union_allocvp(ap->a_vpp, mp, NULLVP, NULLVP, 546996c772fSJohn Dyson cnp, vp, NULLVP, 1); 547df8bae1dSRodney W. Grimes if (error) 548df8bae1dSRodney W. Grimes vput(vp); 549df8bae1dSRodney W. Grimes } 550df8bae1dSRodney W. Grimes return (error); 551df8bae1dSRodney W. Grimes } 552df8bae1dSRodney W. Grimes 553df8bae1dSRodney W. Grimes vput(ap->a_dvp); 554df8bae1dSRodney W. Grimes return (EROFS); 555df8bae1dSRodney W. Grimes } 556df8bae1dSRodney W. Grimes 557c9bf0111SKATO Takenori static int 558df8bae1dSRodney W. Grimes union_open(ap) 559df8bae1dSRodney W. Grimes struct vop_open_args /* { 560df8bae1dSRodney W. Grimes struct vnodeop_desc *a_desc; 561df8bae1dSRodney W. Grimes struct vnode *a_vp; 562df8bae1dSRodney W. Grimes int a_mode; 563df8bae1dSRodney W. Grimes struct ucred *a_cred; 564df8bae1dSRodney W. Grimes struct proc *a_p; 565df8bae1dSRodney W. Grimes } */ *ap; 566df8bae1dSRodney W. Grimes { 567df8bae1dSRodney W. Grimes struct union_node *un = VTOUNION(ap->a_vp); 568df8bae1dSRodney W. Grimes struct vnode *tvp; 569df8bae1dSRodney W. Grimes int mode = ap->a_mode; 570df8bae1dSRodney W. Grimes struct ucred *cred = ap->a_cred; 571df8bae1dSRodney W. Grimes struct proc *p = ap->a_p; 572df8bae1dSRodney W. Grimes int error; 573df8bae1dSRodney W. Grimes 574df8bae1dSRodney W. Grimes /* 575df8bae1dSRodney W. Grimes * If there is an existing upper vp then simply open that. 576df8bae1dSRodney W. Grimes */ 577df8bae1dSRodney W. Grimes tvp = un->un_uppervp; 578df8bae1dSRodney W. Grimes if (tvp == NULLVP) { 579df8bae1dSRodney W. Grimes /* 580df8bae1dSRodney W. Grimes * If the lower vnode is being opened for writing, then 581df8bae1dSRodney W. Grimes * copy the file contents to the upper vnode and open that, 582df8bae1dSRodney W. Grimes * otherwise can simply open the lower vnode. 583df8bae1dSRodney W. Grimes */ 584df8bae1dSRodney W. Grimes tvp = un->un_lowervp; 585df8bae1dSRodney W. Grimes if ((ap->a_mode & FWRITE) && (tvp->v_type == VREG)) { 586996c772fSJohn Dyson error = union_copyup(un, (mode&O_TRUNC) == 0, cred, p); 587df8bae1dSRodney W. Grimes if (error == 0) 588df8bae1dSRodney W. Grimes error = VOP_OPEN(un->un_uppervp, mode, cred, p); 589df8bae1dSRodney W. Grimes return (error); 590df8bae1dSRodney W. Grimes } 591df8bae1dSRodney W. Grimes 592df8bae1dSRodney W. Grimes /* 593df8bae1dSRodney W. Grimes * Just open the lower vnode 594df8bae1dSRodney W. Grimes */ 595df8bae1dSRodney W. Grimes un->un_openl++; 596996c772fSJohn Dyson vn_lock(tvp, LK_EXCLUSIVE | LK_RETRY, p); 597df8bae1dSRodney W. Grimes error = VOP_OPEN(tvp, mode, cred, p); 598996c772fSJohn Dyson VOP_UNLOCK(tvp, 0, p); 599df8bae1dSRodney W. Grimes 600df8bae1dSRodney W. Grimes return (error); 601df8bae1dSRodney W. Grimes } 602df8bae1dSRodney W. Grimes 603996c772fSJohn Dyson FIXUP(un, p); 604df8bae1dSRodney W. Grimes 605df8bae1dSRodney W. Grimes error = VOP_OPEN(tvp, mode, cred, p); 606df8bae1dSRodney W. Grimes 607df8bae1dSRodney W. Grimes return (error); 608df8bae1dSRodney W. Grimes } 609df8bae1dSRodney W. Grimes 610c9bf0111SKATO Takenori static int 611df8bae1dSRodney W. Grimes union_close(ap) 612df8bae1dSRodney W. Grimes struct vop_close_args /* { 613df8bae1dSRodney W. Grimes struct vnode *a_vp; 614df8bae1dSRodney W. Grimes int a_fflag; 615df8bae1dSRodney W. Grimes struct ucred *a_cred; 616df8bae1dSRodney W. Grimes struct proc *a_p; 617df8bae1dSRodney W. Grimes } */ *ap; 618df8bae1dSRodney W. Grimes { 619df8bae1dSRodney W. Grimes struct union_node *un = VTOUNION(ap->a_vp); 620df8bae1dSRodney W. Grimes struct vnode *vp; 621df8bae1dSRodney W. Grimes 622996c772fSJohn Dyson if ((vp = un->un_uppervp) == NULLVP) { 623df8bae1dSRodney W. Grimes #ifdef UNION_DIAGNOSTIC 624df8bae1dSRodney W. Grimes if (un->un_openl <= 0) 625df8bae1dSRodney W. Grimes panic("union: un_openl cnt"); 626df8bae1dSRodney W. Grimes #endif 627df8bae1dSRodney W. Grimes --un->un_openl; 628df8bae1dSRodney W. Grimes vp = un->un_lowervp; 629df8bae1dSRodney W. Grimes } 630df8bae1dSRodney W. Grimes 631996c772fSJohn Dyson ap->a_vp = vp; 632996c772fSJohn Dyson return (VCALL(vp, VOFFSET(vop_close), ap)); 633df8bae1dSRodney W. Grimes } 634df8bae1dSRodney W. Grimes 635df8bae1dSRodney W. Grimes /* 636df8bae1dSRodney W. Grimes * Check access permission on the union vnode. 637df8bae1dSRodney W. Grimes * The access check being enforced is to check 638df8bae1dSRodney W. Grimes * against both the underlying vnode, and any 639df8bae1dSRodney W. Grimes * copied vnode. This ensures that no additional 640df8bae1dSRodney W. Grimes * file permissions are given away simply because 641df8bae1dSRodney W. Grimes * the user caused an implicit file copy. 642df8bae1dSRodney W. Grimes */ 643c9bf0111SKATO Takenori static int 644df8bae1dSRodney W. Grimes union_access(ap) 645df8bae1dSRodney W. Grimes struct vop_access_args /* { 646df8bae1dSRodney W. Grimes struct vnodeop_desc *a_desc; 647df8bae1dSRodney W. Grimes struct vnode *a_vp; 648df8bae1dSRodney W. Grimes int a_mode; 649df8bae1dSRodney W. Grimes struct ucred *a_cred; 650df8bae1dSRodney W. Grimes struct proc *a_p; 651df8bae1dSRodney W. Grimes } */ *ap; 652df8bae1dSRodney W. Grimes { 653df8bae1dSRodney W. Grimes struct union_node *un = VTOUNION(ap->a_vp); 654996c772fSJohn Dyson struct proc *p = ap->a_p; 655df8bae1dSRodney W. Grimes int error = EACCES; 656df8bae1dSRodney W. Grimes struct vnode *vp; 6575f3f68a8SKATO Takenori struct vnode *savedvp; 658df8bae1dSRodney W. Grimes 659996c772fSJohn Dyson if ((vp = un->un_uppervp) != NULLVP) { 660996c772fSJohn Dyson FIXUP(un, p); 661996c772fSJohn Dyson ap->a_vp = vp; 662996c772fSJohn Dyson return (VCALL(vp, VOFFSET(vop_access), ap)); 663df8bae1dSRodney W. Grimes } 664df8bae1dSRodney W. Grimes 665996c772fSJohn Dyson if ((vp = un->un_lowervp) != NULLVP) { 666996c772fSJohn Dyson vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); 6675f3f68a8SKATO Takenori savedvp = ap->a_vp; 668996c772fSJohn Dyson ap->a_vp = vp; 669996c772fSJohn Dyson error = VCALL(vp, VOFFSET(vop_access), ap); 670df8bae1dSRodney W. Grimes if (error == 0) { 6715f3f68a8SKATO Takenori struct union_mount *um = MOUNTTOUNIONMOUNT(savedvp->v_mount); 672df8bae1dSRodney W. Grimes 673996c772fSJohn Dyson if (um->um_op == UNMNT_BELOW) { 674996c772fSJohn Dyson ap->a_cred = um->um_cred; 675996c772fSJohn Dyson error = VCALL(vp, VOFFSET(vop_access), ap); 676df8bae1dSRodney W. Grimes } 677996c772fSJohn Dyson } 678996c772fSJohn Dyson VOP_UNLOCK(vp, 0, p); 679df8bae1dSRodney W. Grimes if (error) 680df8bae1dSRodney W. Grimes return (error); 681df8bae1dSRodney W. Grimes } 682df8bae1dSRodney W. Grimes 683df8bae1dSRodney W. Grimes return (error); 684df8bae1dSRodney W. Grimes } 685df8bae1dSRodney W. Grimes 686df8bae1dSRodney W. Grimes /* 687996c772fSJohn Dyson * We handle getattr only to change the fsid and 688996c772fSJohn Dyson * track object sizes 689df8bae1dSRodney W. Grimes */ 690c9bf0111SKATO Takenori static int 691df8bae1dSRodney W. Grimes union_getattr(ap) 692df8bae1dSRodney W. Grimes struct vop_getattr_args /* { 693df8bae1dSRodney W. Grimes struct vnode *a_vp; 694df8bae1dSRodney W. Grimes struct vattr *a_vap; 695df8bae1dSRodney W. Grimes struct ucred *a_cred; 696df8bae1dSRodney W. Grimes struct proc *a_p; 697df8bae1dSRodney W. Grimes } */ *ap; 698df8bae1dSRodney W. Grimes { 699df8bae1dSRodney W. Grimes int error; 700df8bae1dSRodney W. Grimes struct union_node *un = VTOUNION(ap->a_vp); 701df8bae1dSRodney W. Grimes struct vnode *vp = un->un_uppervp; 702996c772fSJohn Dyson struct proc *p = ap->a_p; 703df8bae1dSRodney W. Grimes struct vattr *vap; 704df8bae1dSRodney W. Grimes struct vattr va; 705df8bae1dSRodney W. Grimes 706df8bae1dSRodney W. Grimes 707df8bae1dSRodney W. Grimes /* 708df8bae1dSRodney W. Grimes * Some programs walk the filesystem hierarchy by counting 709df8bae1dSRodney W. Grimes * links to directories to avoid stat'ing all the time. 710df8bae1dSRodney W. Grimes * This means the link count on directories needs to be "correct". 711df8bae1dSRodney W. Grimes * The only way to do that is to call getattr on both layers 712df8bae1dSRodney W. Grimes * and fix up the link count. The link count will not necessarily 713df8bae1dSRodney W. Grimes * be accurate but will be large enough to defeat the tree walkers. 714df8bae1dSRodney W. Grimes */ 715df8bae1dSRodney W. Grimes 716df8bae1dSRodney W. Grimes vap = ap->a_vap; 717df8bae1dSRodney W. Grimes 718df8bae1dSRodney W. Grimes vp = un->un_uppervp; 719df8bae1dSRodney W. Grimes if (vp != NULLVP) { 720996c772fSJohn Dyson /* 721996c772fSJohn Dyson * It's not clear whether VOP_GETATTR is to be 722996c772fSJohn Dyson * called with the vnode locked or not. stat() calls 723996c772fSJohn Dyson * it with (vp) locked, and fstat calls it with 724996c772fSJohn Dyson * (vp) unlocked. 725996c772fSJohn Dyson * In the mean time, compensate here by checking 726996c772fSJohn Dyson * the union_node's lock flag. 727996c772fSJohn Dyson */ 728996c772fSJohn Dyson if (un->un_flags & UN_LOCKED) 729996c772fSJohn Dyson FIXUP(un, p); 730996c772fSJohn Dyson 731df8bae1dSRodney W. Grimes error = VOP_GETATTR(vp, vap, ap->a_cred, ap->a_p); 732df8bae1dSRodney W. Grimes if (error) 733df8bae1dSRodney W. Grimes return (error); 734996c772fSJohn Dyson union_newsize(ap->a_vp, vap->va_size, VNOVAL); 735df8bae1dSRodney W. Grimes } 736df8bae1dSRodney W. Grimes 737df8bae1dSRodney W. Grimes if (vp == NULLVP) { 738df8bae1dSRodney W. Grimes vp = un->un_lowervp; 739df8bae1dSRodney W. Grimes } else if (vp->v_type == VDIR) { 740df8bae1dSRodney W. Grimes vp = un->un_lowervp; 741df8bae1dSRodney W. Grimes vap = &va; 742df8bae1dSRodney W. Grimes } else { 743df8bae1dSRodney W. Grimes vp = NULLVP; 744df8bae1dSRodney W. Grimes } 745df8bae1dSRodney W. Grimes 746df8bae1dSRodney W. Grimes if (vp != NULLVP) { 747df8bae1dSRodney W. Grimes error = VOP_GETATTR(vp, vap, ap->a_cred, ap->a_p); 748df8bae1dSRodney W. Grimes if (error) 749df8bae1dSRodney W. Grimes return (error); 750996c772fSJohn Dyson union_newsize(ap->a_vp, VNOVAL, vap->va_size); 751df8bae1dSRodney W. Grimes } 752df8bae1dSRodney W. Grimes 753df8bae1dSRodney W. Grimes if ((vap != ap->a_vap) && (vap->va_type == VDIR)) 754df8bae1dSRodney W. Grimes ap->a_vap->va_nlink += vap->va_nlink; 755df8bae1dSRodney W. Grimes 756996c772fSJohn Dyson ap->a_vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsid.val[0]; 757df8bae1dSRodney W. Grimes return (0); 758df8bae1dSRodney W. Grimes } 759df8bae1dSRodney W. Grimes 760c9bf0111SKATO Takenori static int 761df8bae1dSRodney W. Grimes union_setattr(ap) 762df8bae1dSRodney W. Grimes struct vop_setattr_args /* { 763df8bae1dSRodney W. Grimes struct vnode *a_vp; 764df8bae1dSRodney W. Grimes struct vattr *a_vap; 765df8bae1dSRodney W. Grimes struct ucred *a_cred; 766df8bae1dSRodney W. Grimes struct proc *a_p; 767df8bae1dSRodney W. Grimes } */ *ap; 768df8bae1dSRodney W. Grimes { 769df8bae1dSRodney W. Grimes struct union_node *un = VTOUNION(ap->a_vp); 770996c772fSJohn Dyson struct proc *p = ap->a_p; 771df8bae1dSRodney W. Grimes int error; 772df8bae1dSRodney W. Grimes 773df8bae1dSRodney W. Grimes /* 774df8bae1dSRodney W. Grimes * Handle case of truncating lower object to zero size, 775df8bae1dSRodney W. Grimes * by creating a zero length upper object. This is to 776df8bae1dSRodney W. Grimes * handle the case of open with O_TRUNC and O_CREAT. 777df8bae1dSRodney W. Grimes */ 778df8bae1dSRodney W. Grimes if ((un->un_uppervp == NULLVP) && 779df8bae1dSRodney W. Grimes /* assert(un->un_lowervp != NULLVP) */ 780996c772fSJohn Dyson (un->un_lowervp->v_type == VREG)) { 781996c772fSJohn Dyson error = union_copyup(un, (ap->a_vap->va_size != 0), 782996c772fSJohn Dyson ap->a_cred, ap->a_p); 783df8bae1dSRodney W. Grimes if (error) 784df8bae1dSRodney W. Grimes return (error); 785df8bae1dSRodney W. Grimes } 786df8bae1dSRodney W. Grimes 787df8bae1dSRodney W. Grimes /* 788df8bae1dSRodney W. Grimes * Try to set attributes in upper layer, 789df8bae1dSRodney W. Grimes * otherwise return read-only filesystem error. 790df8bae1dSRodney W. Grimes */ 791df8bae1dSRodney W. Grimes if (un->un_uppervp != NULLVP) { 792996c772fSJohn Dyson FIXUP(un, p); 793df8bae1dSRodney W. Grimes error = VOP_SETATTR(un->un_uppervp, ap->a_vap, 794df8bae1dSRodney W. Grimes ap->a_cred, ap->a_p); 795996c772fSJohn Dyson if ((error == 0) && (ap->a_vap->va_size != VNOVAL)) 796996c772fSJohn Dyson union_newsize(ap->a_vp, ap->a_vap->va_size, VNOVAL); 797df8bae1dSRodney W. Grimes } else { 798df8bae1dSRodney W. Grimes error = EROFS; 799df8bae1dSRodney W. Grimes } 800df8bae1dSRodney W. Grimes 801df8bae1dSRodney W. Grimes return (error); 802df8bae1dSRodney W. Grimes } 803df8bae1dSRodney W. Grimes 804c9bf0111SKATO Takenori static int 805df8bae1dSRodney W. Grimes union_read(ap) 806df8bae1dSRodney W. Grimes struct vop_read_args /* { 807df8bae1dSRodney W. Grimes struct vnode *a_vp; 808df8bae1dSRodney W. Grimes struct uio *a_uio; 809df8bae1dSRodney W. Grimes int a_ioflag; 810df8bae1dSRodney W. Grimes struct ucred *a_cred; 811df8bae1dSRodney W. Grimes } */ *ap; 812df8bae1dSRodney W. Grimes { 813df8bae1dSRodney W. Grimes int error; 814996c772fSJohn Dyson struct proc *p = ap->a_uio->uio_procp; 815df8bae1dSRodney W. Grimes struct vnode *vp = OTHERVP(ap->a_vp); 816df8bae1dSRodney W. Grimes int dolock = (vp == LOWERVP(ap->a_vp)); 817df8bae1dSRodney W. Grimes 818df8bae1dSRodney W. Grimes if (dolock) 819996c772fSJohn Dyson vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); 820df8bae1dSRodney W. Grimes else 821996c772fSJohn Dyson FIXUP(VTOUNION(ap->a_vp), p); 822df8bae1dSRodney W. Grimes error = VOP_READ(vp, ap->a_uio, ap->a_ioflag, ap->a_cred); 823df8bae1dSRodney W. Grimes if (dolock) 824996c772fSJohn Dyson VOP_UNLOCK(vp, 0, p); 825996c772fSJohn Dyson 826996c772fSJohn Dyson /* 827996c772fSJohn Dyson * XXX 828996c772fSJohn Dyson * perhaps the size of the underlying object has changed under 829996c772fSJohn Dyson * our feet. take advantage of the offset information present 830996c772fSJohn Dyson * in the uio structure. 831996c772fSJohn Dyson */ 832996c772fSJohn Dyson if (error == 0) { 833996c772fSJohn Dyson struct union_node *un = VTOUNION(ap->a_vp); 834996c772fSJohn Dyson off_t cur = ap->a_uio->uio_offset; 835996c772fSJohn Dyson 836996c772fSJohn Dyson if (vp == un->un_uppervp) { 837996c772fSJohn Dyson if (cur > un->un_uppersz) 838996c772fSJohn Dyson union_newsize(ap->a_vp, cur, VNOVAL); 839996c772fSJohn Dyson } else { 840996c772fSJohn Dyson if (cur > un->un_lowersz) 841996c772fSJohn Dyson union_newsize(ap->a_vp, VNOVAL, cur); 842996c772fSJohn Dyson } 843996c772fSJohn Dyson } 844df8bae1dSRodney W. Grimes 845df8bae1dSRodney W. Grimes return (error); 846df8bae1dSRodney W. Grimes } 847df8bae1dSRodney W. Grimes 848c9bf0111SKATO Takenori static int 849df8bae1dSRodney W. Grimes union_write(ap) 850df8bae1dSRodney W. Grimes struct vop_read_args /* { 851df8bae1dSRodney W. Grimes struct vnode *a_vp; 852df8bae1dSRodney W. Grimes struct uio *a_uio; 853df8bae1dSRodney W. Grimes int a_ioflag; 854df8bae1dSRodney W. Grimes struct ucred *a_cred; 855df8bae1dSRodney W. Grimes } */ *ap; 856df8bae1dSRodney W. Grimes { 857df8bae1dSRodney W. Grimes int error; 858996c772fSJohn Dyson struct vnode *vp; 859996c772fSJohn Dyson struct union_node *un = VTOUNION(ap->a_vp); 860996c772fSJohn Dyson struct proc *p = ap->a_uio->uio_procp; 861df8bae1dSRodney W. Grimes 862996c772fSJohn Dyson vp = UPPERVP(ap->a_vp); 863996c772fSJohn Dyson if (vp == NULLVP) 864996c772fSJohn Dyson panic("union: missing upper layer in write"); 865996c772fSJohn Dyson 866996c772fSJohn Dyson FIXUP(un, p); 867df8bae1dSRodney W. Grimes error = VOP_WRITE(vp, ap->a_uio, ap->a_ioflag, ap->a_cred); 868996c772fSJohn Dyson 869996c772fSJohn Dyson /* 870996c772fSJohn Dyson * the size of the underlying object may be changed by the 871996c772fSJohn Dyson * write. 872996c772fSJohn Dyson */ 873996c772fSJohn Dyson if (error == 0) { 874996c772fSJohn Dyson off_t cur = ap->a_uio->uio_offset; 875996c772fSJohn Dyson 876996c772fSJohn Dyson if (cur > un->un_uppersz) 877996c772fSJohn Dyson union_newsize(ap->a_vp, cur, VNOVAL); 878996c772fSJohn Dyson } 879df8bae1dSRodney W. Grimes 880df8bae1dSRodney W. Grimes return (error); 881df8bae1dSRodney W. Grimes } 882df8bae1dSRodney W. Grimes 883c9bf0111SKATO Takenori static int 884996c772fSJohn Dyson union_lease(ap) 885996c772fSJohn Dyson struct vop_lease_args /* { 886996c772fSJohn Dyson struct vnode *a_vp; 887996c772fSJohn Dyson struct proc *a_p; 888996c772fSJohn Dyson struct ucred *a_cred; 889996c772fSJohn Dyson int a_flag; 890996c772fSJohn Dyson } */ *ap; 891996c772fSJohn Dyson { 892996c772fSJohn Dyson register struct vnode *ovp = OTHERVP(ap->a_vp); 893996c772fSJohn Dyson 894996c772fSJohn Dyson ap->a_vp = ovp; 895996c772fSJohn Dyson return (VCALL(ovp, VOFFSET(vop_lease), ap)); 896996c772fSJohn Dyson } 897996c772fSJohn Dyson 898c9bf0111SKATO Takenori static int 899df8bae1dSRodney W. Grimes union_ioctl(ap) 900df8bae1dSRodney W. Grimes struct vop_ioctl_args /* { 901df8bae1dSRodney W. Grimes struct vnode *a_vp; 902df8bae1dSRodney W. Grimes int a_command; 903df8bae1dSRodney W. Grimes caddr_t a_data; 904df8bae1dSRodney W. Grimes int a_fflag; 905df8bae1dSRodney W. Grimes struct ucred *a_cred; 906df8bae1dSRodney W. Grimes struct proc *a_p; 907df8bae1dSRodney W. Grimes } */ *ap; 908df8bae1dSRodney W. Grimes { 909996c772fSJohn Dyson register struct vnode *ovp = OTHERVP(ap->a_vp); 910df8bae1dSRodney W. Grimes 911996c772fSJohn Dyson ap->a_vp = ovp; 912996c772fSJohn Dyson return (VCALL(ovp, VOFFSET(vop_ioctl), ap)); 913df8bae1dSRodney W. Grimes } 914df8bae1dSRodney W. Grimes 915c9bf0111SKATO Takenori static int 916df8bae1dSRodney W. Grimes union_select(ap) 917df8bae1dSRodney W. Grimes struct vop_select_args /* { 918df8bae1dSRodney W. Grimes struct vnode *a_vp; 919df8bae1dSRodney W. Grimes int a_which; 920df8bae1dSRodney W. Grimes int a_fflags; 921df8bae1dSRodney W. Grimes struct ucred *a_cred; 922df8bae1dSRodney W. Grimes struct proc *a_p; 923df8bae1dSRodney W. Grimes } */ *ap; 924df8bae1dSRodney W. Grimes { 925996c772fSJohn Dyson register struct vnode *ovp = OTHERVP(ap->a_vp); 926df8bae1dSRodney W. Grimes 927996c772fSJohn Dyson ap->a_vp = ovp; 928996c772fSJohn Dyson return (VCALL(ovp, VOFFSET(vop_select), ap)); 929996c772fSJohn Dyson } 930996c772fSJohn Dyson 931c9bf0111SKATO Takenori static int 932996c772fSJohn Dyson union_revoke(ap) 933996c772fSJohn Dyson struct vop_revoke_args /* { 934996c772fSJohn Dyson struct vnode *a_vp; 935996c772fSJohn Dyson int a_flags; 936996c772fSJohn Dyson struct proc *a_p; 937996c772fSJohn Dyson } */ *ap; 938996c772fSJohn Dyson { 939996c772fSJohn Dyson struct vnode *vp = ap->a_vp; 940996c772fSJohn Dyson 941996c772fSJohn Dyson if (UPPERVP(vp)) 942996c772fSJohn Dyson VOP_REVOKE(UPPERVP(vp), ap->a_flags); 943996c772fSJohn Dyson if (LOWERVP(vp)) 944996c772fSJohn Dyson VOP_REVOKE(LOWERVP(vp), ap->a_flags); 945996c772fSJohn Dyson vgone(vp); 946996c772fSJohn Dyson return (0); 947df8bae1dSRodney W. Grimes } 948df8bae1dSRodney W. Grimes 949c9bf0111SKATO Takenori static int 950df8bae1dSRodney W. Grimes union_mmap(ap) 951df8bae1dSRodney W. Grimes struct vop_mmap_args /* { 952df8bae1dSRodney W. Grimes struct vnode *a_vp; 953df8bae1dSRodney W. Grimes int a_fflags; 954df8bae1dSRodney W. Grimes struct ucred *a_cred; 955df8bae1dSRodney W. Grimes struct proc *a_p; 956df8bae1dSRodney W. Grimes } */ *ap; 957df8bae1dSRodney W. Grimes { 958996c772fSJohn Dyson register struct vnode *ovp = OTHERVP(ap->a_vp); 959df8bae1dSRodney W. Grimes 960996c772fSJohn Dyson ap->a_vp = ovp; 961996c772fSJohn Dyson return (VCALL(ovp, VOFFSET(vop_mmap), ap)); 962df8bae1dSRodney W. Grimes } 963df8bae1dSRodney W. Grimes 964c9bf0111SKATO Takenori static int 965df8bae1dSRodney W. Grimes union_fsync(ap) 966df8bae1dSRodney W. Grimes struct vop_fsync_args /* { 967df8bae1dSRodney W. Grimes struct vnode *a_vp; 968df8bae1dSRodney W. Grimes struct ucred *a_cred; 969df8bae1dSRodney W. Grimes int a_waitfor; 970df8bae1dSRodney W. Grimes struct proc *a_p; 971df8bae1dSRodney W. Grimes } */ *ap; 972df8bae1dSRodney W. Grimes { 973df8bae1dSRodney W. Grimes int error = 0; 974996c772fSJohn Dyson struct proc *p = ap->a_p; 975df8bae1dSRodney W. Grimes struct vnode *targetvp = OTHERVP(ap->a_vp); 976363ad700SPoul-Henning Kamp struct union_node *un; 977363ad700SPoul-Henning Kamp int isupperlocked = 0; 978df8bae1dSRodney W. Grimes 979996c772fSJohn Dyson if (targetvp != NULLVP) { 980df8bae1dSRodney W. Grimes int dolock = (targetvp == LOWERVP(ap->a_vp)); 981df8bae1dSRodney W. Grimes 982363ad700SPoul-Henning Kamp un = VTOUNION(ap->a_vp); 983df8bae1dSRodney W. Grimes if (dolock) 984996c772fSJohn Dyson vn_lock(targetvp, LK_EXCLUSIVE | LK_RETRY, p); 985363ad700SPoul-Henning Kamp else if ((un->un_flags & UN_ULOCK) == 0 && 986363ad700SPoul-Henning Kamp VOP_ISLOCKED(targetvp) == 0) { 987363ad700SPoul-Henning Kamp isupperlocked = 1; 988363ad700SPoul-Henning Kamp vn_lock(targetvp, LK_EXCLUSIVE | LK_RETRY, p); 989363ad700SPoul-Henning Kamp un->un_flags |= UN_ULOCK; 990363ad700SPoul-Henning Kamp } 991996c772fSJohn Dyson error = VOP_FSYNC(targetvp, ap->a_cred, ap->a_waitfor, p); 992df8bae1dSRodney W. Grimes if (dolock) 993996c772fSJohn Dyson VOP_UNLOCK(targetvp, 0, p); 994363ad700SPoul-Henning Kamp else if (isupperlocked) { 995363ad700SPoul-Henning Kamp un->un_flags &= ~UN_ULOCK; 996a9320ff3SKATO Takenori VOP_UNLOCK(targetvp, 0, p); 997363ad700SPoul-Henning Kamp } 998df8bae1dSRodney W. Grimes } 999df8bae1dSRodney W. Grimes 1000df8bae1dSRodney W. Grimes return (error); 1001df8bae1dSRodney W. Grimes } 1002df8bae1dSRodney W. Grimes 1003c9bf0111SKATO Takenori static int 1004df8bae1dSRodney W. Grimes union_seek(ap) 1005df8bae1dSRodney W. Grimes struct vop_seek_args /* { 1006df8bae1dSRodney W. Grimes struct vnode *a_vp; 1007df8bae1dSRodney W. Grimes off_t a_oldoff; 1008df8bae1dSRodney W. Grimes off_t a_newoff; 1009df8bae1dSRodney W. Grimes struct ucred *a_cred; 1010df8bae1dSRodney W. Grimes } */ *ap; 1011df8bae1dSRodney W. Grimes { 1012996c772fSJohn Dyson register struct vnode *ovp = OTHERVP(ap->a_vp); 1013df8bae1dSRodney W. Grimes 1014996c772fSJohn Dyson ap->a_vp = ovp; 1015996c772fSJohn Dyson return (VCALL(ovp, VOFFSET(vop_seek), ap)); 1016df8bae1dSRodney W. Grimes } 1017df8bae1dSRodney W. Grimes 1018c9bf0111SKATO Takenori static int 1019df8bae1dSRodney W. Grimes union_remove(ap) 1020df8bae1dSRodney W. Grimes struct vop_remove_args /* { 1021df8bae1dSRodney W. Grimes struct vnode *a_dvp; 1022df8bae1dSRodney W. Grimes struct vnode *a_vp; 1023df8bae1dSRodney W. Grimes struct componentname *a_cnp; 1024df8bae1dSRodney W. Grimes } */ *ap; 1025df8bae1dSRodney W. Grimes { 1026df8bae1dSRodney W. Grimes int error; 1027df8bae1dSRodney W. Grimes struct union_node *dun = VTOUNION(ap->a_dvp); 1028df8bae1dSRodney W. Grimes struct union_node *un = VTOUNION(ap->a_vp); 1029996c772fSJohn Dyson struct componentname *cnp = ap->a_cnp; 1030996c772fSJohn Dyson struct proc *p = cnp->cn_proc; 1031df8bae1dSRodney W. Grimes 1032996c772fSJohn Dyson if (dun->un_uppervp == NULLVP) 1033996c772fSJohn Dyson panic("union remove: null upper vnode"); 1034996c772fSJohn Dyson 1035996c772fSJohn Dyson if (un->un_uppervp != NULLVP) { 1036df8bae1dSRodney W. Grimes struct vnode *dvp = dun->un_uppervp; 1037df8bae1dSRodney W. Grimes struct vnode *vp = un->un_uppervp; 1038df8bae1dSRodney W. Grimes 1039996c772fSJohn Dyson FIXUP(dun, p); 1040df8bae1dSRodney W. Grimes VREF(dvp); 1041a9320ff3SKATO Takenori SETKLOCK(dun); 1042df8bae1dSRodney W. Grimes vput(ap->a_dvp); 1043a9320ff3SKATO Takenori CLEARKLOCK(dun); 1044996c772fSJohn Dyson FIXUP(un, p); 1045df8bae1dSRodney W. Grimes VREF(vp); 1046a9320ff3SKATO Takenori SETKLOCK(un); 1047df8bae1dSRodney W. Grimes vput(ap->a_vp); 1048a9320ff3SKATO Takenori CLEARKLOCK(un); 1049df8bae1dSRodney W. Grimes 1050996c772fSJohn Dyson if (union_dowhiteout(un, cnp->cn_cred, cnp->cn_proc)) 1051996c772fSJohn Dyson cnp->cn_flags |= DOWHITEOUT; 1052996c772fSJohn Dyson error = VOP_REMOVE(dvp, vp, cnp); 1053df8bae1dSRodney W. Grimes if (!error) 1054df8bae1dSRodney W. Grimes union_removed_upper(un); 1055df8bae1dSRodney W. Grimes } else { 1056996c772fSJohn Dyson FIXUP(dun, p); 1057996c772fSJohn Dyson error = union_mkwhiteout( 1058996c772fSJohn Dyson MOUNTTOUNIONMOUNT(UNIONTOV(dun)->v_mount), 1059996c772fSJohn Dyson dun->un_uppervp, ap->a_cnp, un->un_path); 1060df8bae1dSRodney W. Grimes vput(ap->a_dvp); 1061df8bae1dSRodney W. Grimes vput(ap->a_vp); 1062df8bae1dSRodney W. Grimes } 1063df8bae1dSRodney W. Grimes 1064df8bae1dSRodney W. Grimes return (error); 1065df8bae1dSRodney W. Grimes } 1066df8bae1dSRodney W. Grimes 1067c9bf0111SKATO Takenori static int 1068df8bae1dSRodney W. Grimes union_link(ap) 1069df8bae1dSRodney W. Grimes struct vop_link_args /* { 1070df8bae1dSRodney W. Grimes struct vnode *a_tdvp; 107147777413SDavid Greenman struct vnode *a_vp; 1072df8bae1dSRodney W. Grimes struct componentname *a_cnp; 1073df8bae1dSRodney W. Grimes } */ *ap; 1074df8bae1dSRodney W. Grimes { 1075996c772fSJohn Dyson int error = 0; 1076996c772fSJohn Dyson struct componentname *cnp = ap->a_cnp; 1077996c772fSJohn Dyson struct proc *p = cnp->cn_proc; 1078996c772fSJohn Dyson struct union_node *un; 1079996c772fSJohn Dyson struct vnode *vp; 1080996c772fSJohn Dyson struct vnode *tdvp; 1081df8bae1dSRodney W. Grimes 1082996c772fSJohn Dyson un = VTOUNION(ap->a_tdvp); 1083df8bae1dSRodney W. Grimes 1084996c772fSJohn Dyson if (ap->a_tdvp->v_op != ap->a_vp->v_op) { 1085996c772fSJohn Dyson vp = ap->a_vp; 1086df8bae1dSRodney W. Grimes } else { 1087996c772fSJohn Dyson struct union_node *tun = VTOUNION(ap->a_vp); 1088996c772fSJohn Dyson if (tun->un_uppervp == NULLVP) { 1089996c772fSJohn Dyson vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY, p); 1090996c772fSJohn Dyson if (un->un_uppervp == tun->un_dirvp) { 1091996c772fSJohn Dyson un->un_flags &= ~UN_ULOCK; 1092996c772fSJohn Dyson VOP_UNLOCK(un->un_uppervp, 0, p); 1093996c772fSJohn Dyson } 1094996c772fSJohn Dyson error = union_copyup(tun, 1, cnp->cn_cred, p); 1095996c772fSJohn Dyson if (un->un_uppervp == tun->un_dirvp) { 1096996c772fSJohn Dyson vn_lock(un->un_uppervp, 1097996c772fSJohn Dyson LK_EXCLUSIVE | LK_RETRY, p); 1098996c772fSJohn Dyson un->un_flags |= UN_ULOCK; 1099996c772fSJohn Dyson } 1100996c772fSJohn Dyson VOP_UNLOCK(ap->a_vp, 0, p); 1101996c772fSJohn Dyson } 1102996c772fSJohn Dyson vp = tun->un_uppervp; 1103df8bae1dSRodney W. Grimes } 1104df8bae1dSRodney W. Grimes 1105996c772fSJohn Dyson tdvp = un->un_uppervp; 1106996c772fSJohn Dyson if (tdvp == NULLVP) 1107996c772fSJohn Dyson error = EROFS; 1108996c772fSJohn Dyson 1109996c772fSJohn Dyson if (error) { 1110996c772fSJohn Dyson vput(ap->a_tdvp); 1111df8bae1dSRodney W. Grimes return (error); 1112df8bae1dSRodney W. Grimes } 1113df8bae1dSRodney W. Grimes 1114996c772fSJohn Dyson FIXUP(un, p); 1115996c772fSJohn Dyson VREF(tdvp); 1116a9320ff3SKATO Takenori SETKLOCK(un); 1117996c772fSJohn Dyson vput(ap->a_tdvp); 1118a9320ff3SKATO Takenori CLEARKLOCK(un); 1119996c772fSJohn Dyson 112093d71a48SKATO Takenori return (VOP_LINK(tdvp, vp, cnp)); 1121996c772fSJohn Dyson } 1122996c772fSJohn Dyson 1123c9bf0111SKATO Takenori static int 1124df8bae1dSRodney W. Grimes union_rename(ap) 1125df8bae1dSRodney W. Grimes struct vop_rename_args /* { 1126df8bae1dSRodney W. Grimes struct vnode *a_fdvp; 1127df8bae1dSRodney W. Grimes struct vnode *a_fvp; 1128df8bae1dSRodney W. Grimes struct componentname *a_fcnp; 1129df8bae1dSRodney W. Grimes struct vnode *a_tdvp; 1130df8bae1dSRodney W. Grimes struct vnode *a_tvp; 1131df8bae1dSRodney W. Grimes struct componentname *a_tcnp; 1132df8bae1dSRodney W. Grimes } */ *ap; 1133df8bae1dSRodney W. Grimes { 1134df8bae1dSRodney W. Grimes int error; 1135df8bae1dSRodney W. Grimes 1136df8bae1dSRodney W. Grimes struct vnode *fdvp = ap->a_fdvp; 1137df8bae1dSRodney W. Grimes struct vnode *fvp = ap->a_fvp; 1138df8bae1dSRodney W. Grimes struct vnode *tdvp = ap->a_tdvp; 1139df8bae1dSRodney W. Grimes struct vnode *tvp = ap->a_tvp; 1140a9320ff3SKATO Takenori int isklockset = 0; 1141df8bae1dSRodney W. Grimes 1142df8bae1dSRodney W. Grimes if (fdvp->v_op == union_vnodeop_p) { /* always true */ 1143df8bae1dSRodney W. Grimes struct union_node *un = VTOUNION(fdvp); 1144df8bae1dSRodney W. Grimes if (un->un_uppervp == NULLVP) { 1145996c772fSJohn Dyson /* 1146996c772fSJohn Dyson * this should never happen in normal 1147996c772fSJohn Dyson * operation but might if there was 1148996c772fSJohn Dyson * a problem creating the top-level shadow 1149996c772fSJohn Dyson * directory. 1150996c772fSJohn Dyson */ 1151996c772fSJohn Dyson error = EXDEV; 1152df8bae1dSRodney W. Grimes goto bad; 1153df8bae1dSRodney W. Grimes } 1154df8bae1dSRodney W. Grimes 1155df8bae1dSRodney W. Grimes fdvp = un->un_uppervp; 1156df8bae1dSRodney W. Grimes VREF(fdvp); 1157df8bae1dSRodney W. Grimes vrele(ap->a_fdvp); 1158df8bae1dSRodney W. Grimes } 1159df8bae1dSRodney W. Grimes 1160df8bae1dSRodney W. Grimes if (fvp->v_op == union_vnodeop_p) { /* always true */ 1161df8bae1dSRodney W. Grimes struct union_node *un = VTOUNION(fvp); 1162df8bae1dSRodney W. Grimes if (un->un_uppervp == NULLVP) { 1163996c772fSJohn Dyson /* XXX: should do a copyup */ 1164996c772fSJohn Dyson error = EXDEV; 1165df8bae1dSRodney W. Grimes goto bad; 1166df8bae1dSRodney W. Grimes } 1167df8bae1dSRodney W. Grimes 1168996c772fSJohn Dyson if (un->un_lowervp != NULLVP) 1169996c772fSJohn Dyson ap->a_fcnp->cn_flags |= DOWHITEOUT; 1170996c772fSJohn Dyson 1171df8bae1dSRodney W. Grimes fvp = un->un_uppervp; 1172df8bae1dSRodney W. Grimes VREF(fvp); 1173df8bae1dSRodney W. Grimes vrele(ap->a_fvp); 1174df8bae1dSRodney W. Grimes } 1175df8bae1dSRodney W. Grimes 1176df8bae1dSRodney W. Grimes if (tdvp->v_op == union_vnodeop_p) { 1177df8bae1dSRodney W. Grimes struct union_node *un = VTOUNION(tdvp); 1178df8bae1dSRodney W. Grimes if (un->un_uppervp == NULLVP) { 1179996c772fSJohn Dyson /* 1180996c772fSJohn Dyson * this should never happen in normal 1181996c772fSJohn Dyson * operation but might if there was 1182996c772fSJohn Dyson * a problem creating the top-level shadow 1183996c772fSJohn Dyson * directory. 1184996c772fSJohn Dyson */ 1185996c772fSJohn Dyson error = EXDEV; 1186df8bae1dSRodney W. Grimes goto bad; 1187df8bae1dSRodney W. Grimes } 1188df8bae1dSRodney W. Grimes 1189df8bae1dSRodney W. Grimes tdvp = un->un_uppervp; 1190df8bae1dSRodney W. Grimes VREF(tdvp); 1191a9320ff3SKATO Takenori SETKLOCK(un); 1192df8bae1dSRodney W. Grimes vput(ap->a_tdvp); 1193a9320ff3SKATO Takenori CLEARKLOCK(un); 1194df8bae1dSRodney W. Grimes } 1195df8bae1dSRodney W. Grimes 1196996c772fSJohn Dyson if (tvp != NULLVP && tvp->v_op == union_vnodeop_p) { 1197df8bae1dSRodney W. Grimes struct union_node *un = VTOUNION(tvp); 1198df8bae1dSRodney W. Grimes 1199df8bae1dSRodney W. Grimes tvp = un->un_uppervp; 1200996c772fSJohn Dyson if (tvp != NULLVP) { 1201df8bae1dSRodney W. Grimes VREF(tvp); 1202a9320ff3SKATO Takenori SETKLOCK(un); 1203a9320ff3SKATO Takenori isklockset = 1; 1204996c772fSJohn Dyson } 1205df8bae1dSRodney W. Grimes vput(ap->a_tvp); 1206a9320ff3SKATO Takenori if (isklockset) 1207a9320ff3SKATO Takenori CLEARKLOCK(un); 1208df8bae1dSRodney W. Grimes } 1209df8bae1dSRodney W. Grimes 1210df8bae1dSRodney W. Grimes return (VOP_RENAME(fdvp, fvp, ap->a_fcnp, tdvp, tvp, ap->a_tcnp)); 1211df8bae1dSRodney W. Grimes 1212df8bae1dSRodney W. Grimes bad: 1213df8bae1dSRodney W. Grimes vrele(fdvp); 1214df8bae1dSRodney W. Grimes vrele(fvp); 1215df8bae1dSRodney W. Grimes vput(tdvp); 1216996c772fSJohn Dyson if (tvp != NULLVP) 1217df8bae1dSRodney W. Grimes vput(tvp); 1218df8bae1dSRodney W. Grimes 1219df8bae1dSRodney W. Grimes return (error); 1220df8bae1dSRodney W. Grimes } 1221df8bae1dSRodney W. Grimes 1222c9bf0111SKATO Takenori static int 1223df8bae1dSRodney W. Grimes union_mkdir(ap) 1224df8bae1dSRodney W. Grimes struct vop_mkdir_args /* { 1225df8bae1dSRodney W. Grimes struct vnode *a_dvp; 1226df8bae1dSRodney W. Grimes struct vnode **a_vpp; 1227df8bae1dSRodney W. Grimes struct componentname *a_cnp; 1228df8bae1dSRodney W. Grimes struct vattr *a_vap; 1229df8bae1dSRodney W. Grimes } */ *ap; 1230df8bae1dSRodney W. Grimes { 1231df8bae1dSRodney W. Grimes struct union_node *un = VTOUNION(ap->a_dvp); 1232df8bae1dSRodney W. Grimes struct vnode *dvp = un->un_uppervp; 1233996c772fSJohn Dyson struct componentname *cnp = ap->a_cnp; 1234996c772fSJohn Dyson struct proc *p = cnp->cn_proc; 1235df8bae1dSRodney W. Grimes 1236996c772fSJohn Dyson if (dvp != NULLVP) { 1237df8bae1dSRodney W. Grimes int error; 1238df8bae1dSRodney W. Grimes struct vnode *vp; 1239df8bae1dSRodney W. Grimes 1240996c772fSJohn Dyson FIXUP(un, p); 1241df8bae1dSRodney W. Grimes VREF(dvp); 1242a9320ff3SKATO Takenori SETKLOCK(un); 1243996c772fSJohn Dyson VOP_UNLOCK(ap->a_dvp, 0, p); 1244a9320ff3SKATO Takenori CLEARKLOCK(un); 1245996c772fSJohn Dyson error = VOP_MKDIR(dvp, &vp, cnp, ap->a_vap); 1246996c772fSJohn Dyson if (error) { 1247996c772fSJohn Dyson vrele(ap->a_dvp); 1248df8bae1dSRodney W. Grimes return (error); 1249996c772fSJohn Dyson } 1250df8bae1dSRodney W. Grimes 1251996c772fSJohn Dyson error = union_allocvp(ap->a_vpp, ap->a_dvp->v_mount, ap->a_dvp, 1252996c772fSJohn Dyson NULLVP, cnp, vp, NULLVP, 1); 1253996c772fSJohn Dyson vrele(ap->a_dvp); 1254df8bae1dSRodney W. Grimes if (error) 1255df8bae1dSRodney W. Grimes vput(vp); 1256df8bae1dSRodney W. Grimes return (error); 1257df8bae1dSRodney W. Grimes } 1258df8bae1dSRodney W. Grimes 1259df8bae1dSRodney W. Grimes vput(ap->a_dvp); 1260df8bae1dSRodney W. Grimes return (EROFS); 1261df8bae1dSRodney W. Grimes } 1262df8bae1dSRodney W. Grimes 1263c9bf0111SKATO Takenori static int 1264df8bae1dSRodney W. Grimes union_rmdir(ap) 1265df8bae1dSRodney W. Grimes struct vop_rmdir_args /* { 1266df8bae1dSRodney W. Grimes struct vnode *a_dvp; 1267df8bae1dSRodney W. Grimes struct vnode *a_vp; 1268df8bae1dSRodney W. Grimes struct componentname *a_cnp; 1269df8bae1dSRodney W. Grimes } */ *ap; 1270df8bae1dSRodney W. Grimes { 1271df8bae1dSRodney W. Grimes int error; 1272df8bae1dSRodney W. Grimes struct union_node *dun = VTOUNION(ap->a_dvp); 1273df8bae1dSRodney W. Grimes struct union_node *un = VTOUNION(ap->a_vp); 1274996c772fSJohn Dyson struct componentname *cnp = ap->a_cnp; 1275996c772fSJohn Dyson struct proc *p = cnp->cn_proc; 1276df8bae1dSRodney W. Grimes 1277996c772fSJohn Dyson if (dun->un_uppervp == NULLVP) 1278996c772fSJohn Dyson panic("union rmdir: null upper vnode"); 1279996c772fSJohn Dyson 1280996c772fSJohn Dyson if (un->un_uppervp != NULLVP) { 1281df8bae1dSRodney W. Grimes struct vnode *dvp = dun->un_uppervp; 1282df8bae1dSRodney W. Grimes struct vnode *vp = un->un_uppervp; 1283df8bae1dSRodney W. Grimes 1284996c772fSJohn Dyson FIXUP(dun, p); 1285df8bae1dSRodney W. Grimes VREF(dvp); 1286a9320ff3SKATO Takenori SETKLOCK(dun); 1287df8bae1dSRodney W. Grimes vput(ap->a_dvp); 1288a9320ff3SKATO Takenori CLEARKLOCK(dun); 1289996c772fSJohn Dyson FIXUP(un, p); 1290df8bae1dSRodney W. Grimes VREF(vp); 1291a9320ff3SKATO Takenori SETKLOCK(un); 1292df8bae1dSRodney W. Grimes vput(ap->a_vp); 1293a9320ff3SKATO Takenori CLEARKLOCK(un); 1294df8bae1dSRodney W. Grimes 1295996c772fSJohn Dyson if (union_dowhiteout(un, cnp->cn_cred, cnp->cn_proc)) 1296996c772fSJohn Dyson cnp->cn_flags |= DOWHITEOUT; 1297df8bae1dSRodney W. Grimes error = VOP_RMDIR(dvp, vp, ap->a_cnp); 1298df8bae1dSRodney W. Grimes if (!error) 1299df8bae1dSRodney W. Grimes union_removed_upper(un); 1300df8bae1dSRodney W. Grimes } else { 1301996c772fSJohn Dyson FIXUP(dun, p); 1302996c772fSJohn Dyson error = union_mkwhiteout( 1303996c772fSJohn Dyson MOUNTTOUNIONMOUNT(UNIONTOV(dun)->v_mount), 1304996c772fSJohn Dyson dun->un_uppervp, ap->a_cnp, un->un_path); 1305df8bae1dSRodney W. Grimes vput(ap->a_dvp); 1306df8bae1dSRodney W. Grimes vput(ap->a_vp); 1307df8bae1dSRodney W. Grimes } 1308df8bae1dSRodney W. Grimes 1309df8bae1dSRodney W. Grimes return (error); 1310df8bae1dSRodney W. Grimes } 1311df8bae1dSRodney W. Grimes 1312c9bf0111SKATO Takenori static int 1313df8bae1dSRodney W. Grimes union_symlink(ap) 1314df8bae1dSRodney W. Grimes struct vop_symlink_args /* { 1315df8bae1dSRodney W. Grimes struct vnode *a_dvp; 1316df8bae1dSRodney W. Grimes struct vnode **a_vpp; 1317df8bae1dSRodney W. Grimes struct componentname *a_cnp; 1318df8bae1dSRodney W. Grimes struct vattr *a_vap; 1319df8bae1dSRodney W. Grimes char *a_target; 1320df8bae1dSRodney W. Grimes } */ *ap; 1321df8bae1dSRodney W. Grimes { 1322df8bae1dSRodney W. Grimes struct union_node *un = VTOUNION(ap->a_dvp); 1323df8bae1dSRodney W. Grimes struct vnode *dvp = un->un_uppervp; 1324996c772fSJohn Dyson struct componentname *cnp = ap->a_cnp; 1325996c772fSJohn Dyson struct proc *p = cnp->cn_proc; 1326df8bae1dSRodney W. Grimes 1327996c772fSJohn Dyson if (dvp != NULLVP) { 1328df8bae1dSRodney W. Grimes int error; 1329df8bae1dSRodney W. Grimes struct vnode *vp; 1330df8bae1dSRodney W. Grimes 1331996c772fSJohn Dyson FIXUP(un, p); 1332df8bae1dSRodney W. Grimes VREF(dvp); 1333a9320ff3SKATO Takenori SETKLOCK(un); 1334df8bae1dSRodney W. Grimes vput(ap->a_dvp); 1335a9320ff3SKATO Takenori CLEARKLOCK(un); 1336996c772fSJohn Dyson error = VOP_SYMLINK(dvp, &vp, cnp, ap->a_vap, ap->a_target); 1337df8bae1dSRodney W. Grimes *ap->a_vpp = NULLVP; 1338df8bae1dSRodney W. Grimes return (error); 1339df8bae1dSRodney W. Grimes } 1340df8bae1dSRodney W. Grimes 1341df8bae1dSRodney W. Grimes vput(ap->a_dvp); 1342df8bae1dSRodney W. Grimes return (EROFS); 1343df8bae1dSRodney W. Grimes } 1344df8bae1dSRodney W. Grimes 1345df8bae1dSRodney W. Grimes /* 1346df8bae1dSRodney W. Grimes * union_readdir works in concert with getdirentries and 1347df8bae1dSRodney W. Grimes * readdir(3) to provide a list of entries in the unioned 1348df8bae1dSRodney W. Grimes * directories. getdirentries is responsible for walking 1349df8bae1dSRodney W. Grimes * down the union stack. readdir(3) is responsible for 1350df8bae1dSRodney W. Grimes * eliminating duplicate names from the returned data stream. 1351df8bae1dSRodney W. Grimes */ 1352c9bf0111SKATO Takenori static int 1353df8bae1dSRodney W. Grimes union_readdir(ap) 1354df8bae1dSRodney W. Grimes struct vop_readdir_args /* { 1355df8bae1dSRodney W. Grimes struct vnode *a_vp; 1356df8bae1dSRodney W. Grimes struct uio *a_uio; 1357df8bae1dSRodney W. Grimes struct ucred *a_cred; 1358996c772fSJohn Dyson int *a_eofflag; 1359996c772fSJohn Dyson u_long *a_cookies; 1360996c772fSJohn Dyson int a_ncookies; 1361df8bae1dSRodney W. Grimes } */ *ap; 1362df8bae1dSRodney W. Grimes { 1363df8bae1dSRodney W. Grimes struct union_node *un = VTOUNION(ap->a_vp); 1364996c772fSJohn Dyson struct vnode *uvp = un->un_uppervp; 1365996c772fSJohn Dyson struct proc *p = ap->a_uio->uio_procp; 1366df8bae1dSRodney W. Grimes 1367996c772fSJohn Dyson if (uvp == NULLVP) 1368996c772fSJohn Dyson return (0); 1369df8bae1dSRodney W. Grimes 1370996c772fSJohn Dyson FIXUP(un, p); 1371996c772fSJohn Dyson ap->a_vp = uvp; 1372996c772fSJohn Dyson return (VCALL(uvp, VOFFSET(vop_readdir), ap)); 1373df8bae1dSRodney W. Grimes } 1374df8bae1dSRodney W. Grimes 1375c9bf0111SKATO Takenori static int 1376df8bae1dSRodney W. Grimes union_readlink(ap) 1377df8bae1dSRodney W. Grimes struct vop_readlink_args /* { 1378df8bae1dSRodney W. Grimes struct vnode *a_vp; 1379df8bae1dSRodney W. Grimes struct uio *a_uio; 1380df8bae1dSRodney W. Grimes struct ucred *a_cred; 1381df8bae1dSRodney W. Grimes } */ *ap; 1382df8bae1dSRodney W. Grimes { 1383df8bae1dSRodney W. Grimes int error; 1384996c772fSJohn Dyson struct uio *uio = ap->a_uio; 1385996c772fSJohn Dyson struct proc *p = uio->uio_procp; 1386df8bae1dSRodney W. Grimes struct vnode *vp = OTHERVP(ap->a_vp); 1387df8bae1dSRodney W. Grimes int dolock = (vp == LOWERVP(ap->a_vp)); 1388df8bae1dSRodney W. Grimes 1389df8bae1dSRodney W. Grimes if (dolock) 1390996c772fSJohn Dyson vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); 1391df8bae1dSRodney W. Grimes else 1392996c772fSJohn Dyson FIXUP(VTOUNION(ap->a_vp), p); 1393996c772fSJohn Dyson ap->a_vp = vp; 1394996c772fSJohn Dyson error = VCALL(vp, VOFFSET(vop_readlink), ap); 1395df8bae1dSRodney W. Grimes if (dolock) 1396996c772fSJohn Dyson VOP_UNLOCK(vp, 0, p); 1397df8bae1dSRodney W. Grimes 1398df8bae1dSRodney W. Grimes return (error); 1399df8bae1dSRodney W. Grimes } 1400df8bae1dSRodney W. Grimes 1401c9bf0111SKATO Takenori static int 1402df8bae1dSRodney W. Grimes union_abortop(ap) 1403df8bae1dSRodney W. Grimes struct vop_abortop_args /* { 1404df8bae1dSRodney W. Grimes struct vnode *a_dvp; 1405df8bae1dSRodney W. Grimes struct componentname *a_cnp; 1406df8bae1dSRodney W. Grimes } */ *ap; 1407df8bae1dSRodney W. Grimes { 1408df8bae1dSRodney W. Grimes int error; 1409996c772fSJohn Dyson struct componentname *cnp = ap->a_cnp; 1410996c772fSJohn Dyson struct proc *p = cnp->cn_proc; 1411df8bae1dSRodney W. Grimes struct vnode *vp = OTHERVP(ap->a_dvp); 1412df8bae1dSRodney W. Grimes struct union_node *un = VTOUNION(ap->a_dvp); 1413df8bae1dSRodney W. Grimes int islocked = un->un_flags & UN_LOCKED; 1414df8bae1dSRodney W. Grimes int dolock = (vp == LOWERVP(ap->a_dvp)); 1415df8bae1dSRodney W. Grimes 1416df8bae1dSRodney W. Grimes if (islocked) { 1417df8bae1dSRodney W. Grimes if (dolock) 1418996c772fSJohn Dyson vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); 1419df8bae1dSRodney W. Grimes else 1420996c772fSJohn Dyson FIXUP(VTOUNION(ap->a_dvp), p); 1421df8bae1dSRodney W. Grimes } 1422996c772fSJohn Dyson ap->a_dvp = vp; 1423996c772fSJohn Dyson error = VCALL(vp, VOFFSET(vop_abortop), ap); 1424df8bae1dSRodney W. Grimes if (islocked && dolock) 1425996c772fSJohn Dyson VOP_UNLOCK(vp, 0, p); 1426df8bae1dSRodney W. Grimes 1427df8bae1dSRodney W. Grimes return (error); 1428df8bae1dSRodney W. Grimes } 1429df8bae1dSRodney W. Grimes 1430c9bf0111SKATO Takenori static int 1431df8bae1dSRodney W. Grimes union_inactive(ap) 1432df8bae1dSRodney W. Grimes struct vop_inactive_args /* { 1433df8bae1dSRodney W. Grimes struct vnode *a_vp; 1434996c772fSJohn Dyson struct proc *a_p; 1435df8bae1dSRodney W. Grimes } */ *ap; 1436df8bae1dSRodney W. Grimes { 1437996c772fSJohn Dyson struct vnode *vp = ap->a_vp; 1438996c772fSJohn Dyson struct proc *p = ap->a_p; 1439996c772fSJohn Dyson struct union_node *un = VTOUNION(vp); 1440996c772fSJohn Dyson struct vnode **vpp; 1441df8bae1dSRodney W. Grimes 1442df8bae1dSRodney W. Grimes /* 1443df8bae1dSRodney W. Grimes * Do nothing (and _don't_ bypass). 1444df8bae1dSRodney W. Grimes * Wait to vrele lowervp until reclaim, 1445df8bae1dSRodney W. Grimes * so that until then our union_node is in the 1446df8bae1dSRodney W. Grimes * cache and reusable. 1447df8bae1dSRodney W. Grimes * 1448df8bae1dSRodney W. Grimes * NEEDSWORK: Someday, consider inactive'ing 1449df8bae1dSRodney W. Grimes * the lowervp and then trying to reactivate it 1450df8bae1dSRodney W. Grimes * with capabilities (v_id) 1451df8bae1dSRodney W. Grimes * like they do in the name lookup cache code. 1452df8bae1dSRodney W. Grimes * That's too much work for now. 1453df8bae1dSRodney W. Grimes */ 1454df8bae1dSRodney W. Grimes 1455996c772fSJohn Dyson if (un->un_dircache != 0) { 1456996c772fSJohn Dyson for (vpp = un->un_dircache; *vpp != NULLVP; vpp++) 1457996c772fSJohn Dyson vrele(*vpp); 1458996c772fSJohn Dyson free(un->un_dircache, M_TEMP); 1459996c772fSJohn Dyson un->un_dircache = 0; 1460996c772fSJohn Dyson } 1461df8bae1dSRodney W. Grimes 1462996c772fSJohn Dyson VOP_UNLOCK(vp, 0, p); 1463996c772fSJohn Dyson 1464996c772fSJohn Dyson if ((un->un_flags & UN_CACHED) == 0) 1465996c772fSJohn Dyson vgone(vp); 1466df8bae1dSRodney W. Grimes 1467df8bae1dSRodney W. Grimes return (0); 1468df8bae1dSRodney W. Grimes } 1469df8bae1dSRodney W. Grimes 1470c9bf0111SKATO Takenori static int 1471df8bae1dSRodney W. Grimes union_reclaim(ap) 1472df8bae1dSRodney W. Grimes struct vop_reclaim_args /* { 1473df8bae1dSRodney W. Grimes struct vnode *a_vp; 1474df8bae1dSRodney W. Grimes } */ *ap; 1475df8bae1dSRodney W. Grimes { 1476df8bae1dSRodney W. Grimes 1477df8bae1dSRodney W. Grimes union_freevp(ap->a_vp); 1478df8bae1dSRodney W. Grimes 1479df8bae1dSRodney W. Grimes return (0); 1480df8bae1dSRodney W. Grimes } 1481df8bae1dSRodney W. Grimes 1482c9bf0111SKATO Takenori static int 1483df8bae1dSRodney W. Grimes union_lock(ap) 1484df8bae1dSRodney W. Grimes struct vop_lock_args *ap; 1485df8bae1dSRodney W. Grimes { 1486df8bae1dSRodney W. Grimes struct vnode *vp = ap->a_vp; 1487996c772fSJohn Dyson struct proc *p = ap->a_p; 1488996c772fSJohn Dyson int flags = ap->a_flags; 1489df8bae1dSRodney W. Grimes struct union_node *un; 1490996c772fSJohn Dyson int error; 1491996c772fSJohn Dyson 1492996c772fSJohn Dyson vop_nolock(ap); 1493996c772fSJohn Dyson /* 1494996c772fSJohn Dyson * Need to do real lockmgr-style locking here. 1495996c772fSJohn Dyson * in the mean time, draining won't work quite right, 1496996c772fSJohn Dyson * which could lead to a few race conditions. 1497996c772fSJohn Dyson * the following test was here, but is not quite right, we 1498996c772fSJohn Dyson * still need to take the lock: 1499996c772fSJohn Dyson if ((flags & LK_TYPE_MASK) == LK_DRAIN) 1500996c772fSJohn Dyson return (0); 1501996c772fSJohn Dyson */ 1502996c772fSJohn Dyson flags &= ~LK_INTERLOCK; 1503df8bae1dSRodney W. Grimes 1504df8bae1dSRodney W. Grimes start: 1505df8bae1dSRodney W. Grimes un = VTOUNION(vp); 1506df8bae1dSRodney W. Grimes 1507996c772fSJohn Dyson if (un->un_uppervp != NULLVP) { 1508a9320ff3SKATO Takenori if (((un->un_flags & (UN_ULOCK | UN_KLOCK)) == 0) && 1509996c772fSJohn Dyson (vp->v_usecount != 0)) { 1510996c772fSJohn Dyson error = vn_lock(un->un_uppervp, flags, p); 1511996c772fSJohn Dyson if (error) 1512996c772fSJohn Dyson return (error); 1513df8bae1dSRodney W. Grimes un->un_flags |= UN_ULOCK; 1514df8bae1dSRodney W. Grimes } 151518b4c8e2SKATO Takenori } 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 */ 1550c9bf0111SKATO Takenori static 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 1589c9bf0111SKATO Takenori static 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 1617c9bf0111SKATO Takenori static 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 1635c9bf0111SKATO Takenori static 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 1645c9bf0111SKATO Takenori static 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 1670c9bf0111SKATO Takenori static 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 */ 1692c9bf0111SKATO Takenori static 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