xref: /freebsd/sys/fs/unionfs/union_vnops.c (revision e3a285c715631db9cd53989d2b7d2b053bed1296)
1df8bae1dSRodney W. Grimes /*
2996c772fSJohn Dyson  * Copyright (c) 1992, 1993, 1994, 1995 Jan-Simon Pendry.
3996c772fSJohn Dyson  * Copyright (c) 1992, 1993, 1994, 1995
4996c772fSJohn Dyson  *	The Regents of the University of California.  All rights reserved.
5df8bae1dSRodney W. Grimes  *
6df8bae1dSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
7df8bae1dSRodney W. Grimes  * Jan-Simon Pendry.
8df8bae1dSRodney W. Grimes  *
9df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
10df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
11df8bae1dSRodney W. Grimes  * are met:
12df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
13df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
14df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
15df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
16df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
17df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
18df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
19df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
20df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
21df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
22df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
23df8bae1dSRodney W. Grimes  *    without specific prior written permission.
24df8bae1dSRodney W. Grimes  *
25df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
36df8bae1dSRodney W. Grimes  *
37996c772fSJohn Dyson  *	@(#)union_vnops.c	8.32 (Berkeley) 6/23/95
38c3aac50fSPeter Wemm  * $FreeBSD$
39df8bae1dSRodney W. Grimes  */
40df8bae1dSRodney W. Grimes 
41df8bae1dSRodney W. Grimes #include <sys/param.h>
42df8bae1dSRodney W. Grimes #include <sys/systm.h>
43df8bae1dSRodney W. Grimes #include <sys/proc.h>
443ac4d1efSBruce Evans #include <sys/fcntl.h>
45996c772fSJohn Dyson #include <sys/stat.h>
469e67ea79SBruce Evans #include <sys/kernel.h>
47df8bae1dSRodney W. Grimes #include <sys/vnode.h>
48df8bae1dSRodney W. Grimes #include <sys/mount.h>
49df8bae1dSRodney W. Grimes #include <sys/namei.h>
50df8bae1dSRodney W. Grimes #include <sys/malloc.h>
51df8bae1dSRodney W. Grimes #include <sys/buf.h>
52996c772fSJohn Dyson #include <sys/lock.h>
532a31267eSMatthew Dillon #include <sys/sysctl.h>
54df8bae1dSRodney W. Grimes #include <miscfs/union/union.h>
55df8bae1dSRodney W. Grimes 
562a31267eSMatthew Dillon #include <vm/vm.h>
572a31267eSMatthew Dillon #include <vm/vnode_pager.h>
582a31267eSMatthew Dillon 
592a31267eSMatthew Dillon #include <vm/vm_prot.h>
602a31267eSMatthew Dillon #include <vm/vm_page.h>
612a31267eSMatthew Dillon #include <vm/vm_object.h>
622a31267eSMatthew Dillon #include <vm/vm_pager.h>
632a31267eSMatthew Dillon #include <vm/vm_extern.h>
642a31267eSMatthew Dillon 
652a31267eSMatthew Dillon int uniondebug = 0;
662a31267eSMatthew Dillon 
672a31267eSMatthew Dillon #if UDEBUG_ENABLED
682a31267eSMatthew Dillon SYSCTL_INT(_vfs, OID_AUTO, uniondebug, CTLFLAG_RW, &uniondebug, 0, "");
692a31267eSMatthew Dillon #else
702a31267eSMatthew Dillon SYSCTL_INT(_vfs, OID_AUTO, uniondebug, CTLFLAG_RD, &uniondebug, 0, "");
712a31267eSMatthew Dillon #endif
72df8bae1dSRodney W. Grimes 
73c9bf0111SKATO Takenori static int	union_abortop __P((struct vop_abortop_args *ap));
74c9bf0111SKATO Takenori static int	union_access __P((struct vop_access_args *ap));
75c9bf0111SKATO Takenori static int	union_advlock __P((struct vop_advlock_args *ap));
76c9bf0111SKATO Takenori static int	union_bmap __P((struct vop_bmap_args *ap));
77c9bf0111SKATO Takenori static int	union_close __P((struct vop_close_args *ap));
78c9bf0111SKATO Takenori static int	union_create __P((struct vop_create_args *ap));
79c9bf0111SKATO Takenori static int	union_fsync __P((struct vop_fsync_args *ap));
80c9bf0111SKATO Takenori static int	union_getattr __P((struct vop_getattr_args *ap));
81c9bf0111SKATO Takenori static int	union_inactive __P((struct vop_inactive_args *ap));
82c9bf0111SKATO Takenori static int	union_ioctl __P((struct vop_ioctl_args *ap));
83c9bf0111SKATO Takenori static int	union_lease __P((struct vop_lease_args *ap));
84c9bf0111SKATO Takenori static int	union_link __P((struct vop_link_args *ap));
85c9bf0111SKATO Takenori static int	union_lock __P((struct vop_lock_args *ap));
86c9bf0111SKATO Takenori static int	union_lookup __P((struct vop_lookup_args *ap));
872a31267eSMatthew Dillon static int	union_lookup1 __P((struct vnode *udvp, struct vnode **dvp,
889b5e8b3aSBruce Evans 				   struct vnode **vpp,
899b5e8b3aSBruce Evans 				   struct componentname *cnp));
90c9bf0111SKATO Takenori static int	union_mkdir __P((struct vop_mkdir_args *ap));
91c9bf0111SKATO Takenori static int	union_mknod __P((struct vop_mknod_args *ap));
92c9bf0111SKATO Takenori static int	union_mmap __P((struct vop_mmap_args *ap));
93c9bf0111SKATO Takenori static int	union_open __P((struct vop_open_args *ap));
94c9bf0111SKATO Takenori static int	union_pathconf __P((struct vop_pathconf_args *ap));
95c9bf0111SKATO Takenori static int	union_print __P((struct vop_print_args *ap));
96c9bf0111SKATO Takenori static int	union_read __P((struct vop_read_args *ap));
97c9bf0111SKATO Takenori static int	union_readdir __P((struct vop_readdir_args *ap));
98c9bf0111SKATO Takenori static int	union_readlink __P((struct vop_readlink_args *ap));
99c9bf0111SKATO Takenori static int	union_reclaim __P((struct vop_reclaim_args *ap));
100c9bf0111SKATO Takenori static int	union_remove __P((struct vop_remove_args *ap));
101c9bf0111SKATO Takenori static int	union_rename __P((struct vop_rename_args *ap));
102c9bf0111SKATO Takenori static int	union_revoke __P((struct vop_revoke_args *ap));
103c9bf0111SKATO Takenori static int	union_rmdir __P((struct vop_rmdir_args *ap));
104a6aeade2SPeter Wemm static int	union_poll __P((struct vop_poll_args *ap));
105c9bf0111SKATO Takenori static int	union_setattr __P((struct vop_setattr_args *ap));
106c9bf0111SKATO Takenori static int	union_strategy __P((struct vop_strategy_args *ap));
1072a31267eSMatthew Dillon static int	union_getpages __P((struct vop_getpages_args *ap));
1082a31267eSMatthew Dillon static int	union_putpages __P((struct vop_putpages_args *ap));
109c9bf0111SKATO Takenori static int	union_symlink __P((struct vop_symlink_args *ap));
110c9bf0111SKATO Takenori static int	union_unlock __P((struct vop_unlock_args *ap));
111c9bf0111SKATO Takenori static int	union_whiteout __P((struct vop_whiteout_args *ap));
112c9bf0111SKATO Takenori static int	union_write __P((struct vop_read_args *ap));
1139b5e8b3aSBruce Evans 
1142a31267eSMatthew Dillon static __inline
1152a31267eSMatthew Dillon struct vnode *
1162a31267eSMatthew Dillon union_lock_upper(struct union_node *un, struct proc *p)
117df8bae1dSRodney W. Grimes {
1182a31267eSMatthew Dillon 	struct vnode *uppervp;
119df8bae1dSRodney W. Grimes 
1202a31267eSMatthew Dillon 	if ((uppervp = un->un_uppervp) != NULL) {
1212a31267eSMatthew Dillon 		VREF(uppervp);
1222a31267eSMatthew Dillon 		vn_lock(uppervp, LK_EXCLUSIVE | LK_CANRECURSE | LK_RETRY, p);
1232a31267eSMatthew Dillon 	}
1242a31267eSMatthew Dillon 	KASSERT((uppervp == NULL || uppervp->v_usecount > 0), ("uppervp usecount is 0"));
1252a31267eSMatthew Dillon 	return(uppervp);
126df8bae1dSRodney W. Grimes }
127df8bae1dSRodney W. Grimes 
1282a31267eSMatthew Dillon static __inline
1292a31267eSMatthew Dillon void
1302a31267eSMatthew Dillon union_unlock_upper(struct vnode *uppervp, struct proc *p)
1312a31267eSMatthew Dillon {
1322a31267eSMatthew Dillon 	vput(uppervp);
1332a31267eSMatthew Dillon }
1342a31267eSMatthew Dillon 
1352a31267eSMatthew Dillon static __inline
1362a31267eSMatthew Dillon struct vnode *
1372a31267eSMatthew Dillon union_lock_other(struct union_node *un, struct proc *p)
1382a31267eSMatthew Dillon {
1392a31267eSMatthew Dillon 	struct vnode *vp;
1402a31267eSMatthew Dillon 
1412a31267eSMatthew Dillon 	if (un->un_uppervp != NULL) {
1422a31267eSMatthew Dillon 		vp = union_lock_upper(un, p);
1432a31267eSMatthew Dillon 	} else if ((vp = un->un_lowervp) != NULL) {
1442a31267eSMatthew Dillon 		VREF(vp);
1452a31267eSMatthew Dillon 		vn_lock(vp, LK_EXCLUSIVE | LK_CANRECURSE | LK_RETRY, p);
1462a31267eSMatthew Dillon 	}
1472a31267eSMatthew Dillon 	return(vp);
1482a31267eSMatthew Dillon }
1492a31267eSMatthew Dillon 
1502a31267eSMatthew Dillon static __inline
1512a31267eSMatthew Dillon void
1522a31267eSMatthew Dillon union_unlock_other(struct vnode *vp, struct proc *p)
1532a31267eSMatthew Dillon {
1542a31267eSMatthew Dillon 	vput(vp);
1552a31267eSMatthew Dillon }
1562a31267eSMatthew Dillon 
1572a31267eSMatthew Dillon /*
1582a31267eSMatthew Dillon  *	union_lookup:
1592a31267eSMatthew Dillon  *
1602a31267eSMatthew Dillon  *	udvp	must be exclusively locked on call and will remain
1612a31267eSMatthew Dillon  *		exclusively locked on return.  This is the mount point
1622a31267eSMatthew Dillon  *		for out filesystem.
1632a31267eSMatthew Dillon  *
1642a31267eSMatthew Dillon  *	dvp	Our base directory, locked and referenced.
1652a31267eSMatthew Dillon  *		The passed dvp will be dereferenced and unlocked on return
1662a31267eSMatthew Dillon  *		and a new dvp will be returned which is locked and
1672a31267eSMatthew Dillon  *		referenced in the same variable.
1682a31267eSMatthew Dillon  *
1692a31267eSMatthew Dillon  *	vpp	is filled in with the result if no error occured,
1702a31267eSMatthew Dillon  *		locked and ref'd.
1712a31267eSMatthew Dillon  *
1722a31267eSMatthew Dillon  *		If an error is returned, *vpp is set to NULLVP.  If no
1732a31267eSMatthew Dillon  *		error occurs, *vpp is returned with a reference and an
1742a31267eSMatthew Dillon  *		exclusive lock.
1752a31267eSMatthew Dillon  */
1762a31267eSMatthew Dillon 
177df8bae1dSRodney W. Grimes static int
1782a31267eSMatthew Dillon union_lookup1(udvp, pdvp, vpp, cnp)
179df8bae1dSRodney W. Grimes 	struct vnode *udvp;
1802a31267eSMatthew Dillon 	struct vnode **pdvp;
181df8bae1dSRodney W. Grimes 	struct vnode **vpp;
182df8bae1dSRodney W. Grimes 	struct componentname *cnp;
183df8bae1dSRodney W. Grimes {
184df8bae1dSRodney W. Grimes 	int error;
185996c772fSJohn Dyson 	struct proc *p = cnp->cn_proc;
1862a31267eSMatthew Dillon 	struct vnode *dvp = *pdvp;
187df8bae1dSRodney W. Grimes 	struct vnode *tdvp;
188df8bae1dSRodney W. Grimes 	struct mount *mp;
189df8bae1dSRodney W. Grimes 
190df8bae1dSRodney W. Grimes 	/*
191df8bae1dSRodney W. Grimes 	 * If stepping up the directory tree, check for going
192df8bae1dSRodney W. Grimes 	 * back across the mount point, in which case do what
193df8bae1dSRodney W. Grimes 	 * lookup would do by stepping back down the mount
194df8bae1dSRodney W. Grimes 	 * hierarchy.
195df8bae1dSRodney W. Grimes 	 */
196df8bae1dSRodney W. Grimes 	if (cnp->cn_flags & ISDOTDOT) {
197996c772fSJohn Dyson 		while ((dvp != udvp) && (dvp->v_flag & VROOT)) {
198df8bae1dSRodney W. Grimes 			/*
199df8bae1dSRodney W. Grimes 			 * Don't do the NOCROSSMOUNT check
200df8bae1dSRodney W. Grimes 			 * at this level.  By definition,
201df8bae1dSRodney W. Grimes 			 * union fs deals with namespaces, not
202df8bae1dSRodney W. Grimes 			 * filesystems.
203df8bae1dSRodney W. Grimes 			 */
204df8bae1dSRodney W. Grimes 			tdvp = dvp;
2052a31267eSMatthew Dillon 			dvp = dvp->v_mount->mnt_vnodecovered;
206df8bae1dSRodney W. Grimes 			VREF(dvp);
2072a31267eSMatthew Dillon 			vput(tdvp);
208996c772fSJohn Dyson 			vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, p);
209df8bae1dSRodney W. Grimes 		}
210df8bae1dSRodney W. Grimes 	}
211df8bae1dSRodney W. Grimes 
2122a31267eSMatthew Dillon 	/*
2132a31267eSMatthew Dillon 	 * Set return dvp to be the upperdvp 'parent directory.
2142a31267eSMatthew Dillon 	 */
2152a31267eSMatthew Dillon 	*pdvp = dvp;
2162a31267eSMatthew Dillon 
2172a31267eSMatthew Dillon 	/*
2182a31267eSMatthew Dillon 	 * If the VOP_LOOKUP call generates an error, tdvp is invalid and no
2192a31267eSMatthew Dillon 	 * changes will have been made to dvp, so we are set to return.
2202a31267eSMatthew Dillon 	 */
2212a31267eSMatthew Dillon 
222df8bae1dSRodney W. Grimes         error = VOP_LOOKUP(dvp, &tdvp, cnp);
2232a31267eSMatthew Dillon 	if (error) {
2242a31267eSMatthew Dillon 		UDEBUG(("dvp %p error %d flags %lx\n", dvp, error, cnp->cn_flags));
2252a31267eSMatthew Dillon 		*vpp = NULL;
226df8bae1dSRodney W. Grimes 		return (error);
2272a31267eSMatthew Dillon 	}
228df8bae1dSRodney W. Grimes 
229df8bae1dSRodney W. Grimes 	/*
230df8bae1dSRodney W. Grimes 	 * The parent directory will have been unlocked, unless lookup
2312a31267eSMatthew Dillon 	 * found the last component or if dvp == tdvp (tdvp must be locked).
2322a31267eSMatthew Dillon 	 *
2332a31267eSMatthew Dillon 	 * We want our dvp to remain locked and ref'd.  We also want tdvp
2342a31267eSMatthew Dillon 	 * to remain locked and ref'd.
235df8bae1dSRodney W. Grimes 	 */
2362a31267eSMatthew Dillon 	UDEBUG(("parentdir %p result %p flag %lx\n", dvp, tdvp, cnp->cn_flags));
237df8bae1dSRodney W. Grimes 
2382a31267eSMatthew Dillon 	if (dvp != tdvp && (cnp->cn_flags & ISLASTCN) == 0)
2392a31267eSMatthew Dillon 		vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, p);
240df8bae1dSRodney W. Grimes 
241df8bae1dSRodney W. Grimes 	/*
242df8bae1dSRodney W. Grimes 	 * Lastly check if the current node is a mount point in
243df8bae1dSRodney W. Grimes 	 * which case walk up the mount hierarchy making sure not to
244df8bae1dSRodney W. Grimes 	 * bump into the root of the mount tree (ie. dvp != udvp).
2452a31267eSMatthew Dillon 	 *
2462a31267eSMatthew Dillon 	 * We use dvp as a temporary variable here, it is no longer related
2472a31267eSMatthew Dillon 	 * to the dvp above.  However, we have to ensure that both *pdvp and
2482a31267eSMatthew Dillon 	 * tdvp are locked on return.
249df8bae1dSRodney W. Grimes 	 */
2502a31267eSMatthew Dillon 
2512a31267eSMatthew Dillon 	dvp = tdvp;
2522a31267eSMatthew Dillon 	while (
2532a31267eSMatthew Dillon 	    dvp != udvp &&
2542a31267eSMatthew Dillon 	    (dvp->v_type == VDIR) &&
2552a31267eSMatthew Dillon 	    (mp = dvp->v_mountedhere)
2562a31267eSMatthew Dillon 	) {
2572a31267eSMatthew Dillon 		int relock_pdvp = 0;
258df8bae1dSRodney W. Grimes 
259996c772fSJohn Dyson 		if (vfs_busy(mp, 0, 0, p))
260df8bae1dSRodney W. Grimes 			continue;
261df8bae1dSRodney W. Grimes 
2622a31267eSMatthew Dillon 		if (dvp == *pdvp)
2632a31267eSMatthew Dillon 			relock_pdvp = 1;
264df8bae1dSRodney W. Grimes 		vput(dvp);
2652a31267eSMatthew Dillon 		dvp = NULL;
2662a31267eSMatthew Dillon 		error = VFS_ROOT(mp, &dvp);
2672a31267eSMatthew Dillon 
2682a31267eSMatthew Dillon 		vfs_unbusy(mp, p);
2692a31267eSMatthew Dillon 
2702a31267eSMatthew Dillon 		if (relock_pdvp)
2712a31267eSMatthew Dillon 			vn_lock(*pdvp, LK_EXCLUSIVE | LK_RETRY, p);
2722a31267eSMatthew Dillon 
2732a31267eSMatthew Dillon 		if (error) {
2742a31267eSMatthew Dillon 			*vpp = NULL;
275df8bae1dSRodney W. Grimes 			return (error);
276df8bae1dSRodney W. Grimes 		}
277df8bae1dSRodney W. Grimes 	}
278df8bae1dSRodney W. Grimes 	*vpp = dvp;
279df8bae1dSRodney W. Grimes 	return (0);
280df8bae1dSRodney W. Grimes }
281df8bae1dSRodney W. Grimes 
282c9bf0111SKATO Takenori static int
283df8bae1dSRodney W. Grimes union_lookup(ap)
284df8bae1dSRodney W. Grimes 	struct vop_lookup_args /* {
285df8bae1dSRodney W. Grimes 		struct vnodeop_desc *a_desc;
286df8bae1dSRodney W. Grimes 		struct vnode *a_dvp;
287df8bae1dSRodney W. Grimes 		struct vnode **a_vpp;
288df8bae1dSRodney W. Grimes 		struct componentname *a_cnp;
289df8bae1dSRodney W. Grimes 	} */ *ap;
290df8bae1dSRodney W. Grimes {
291df8bae1dSRodney W. Grimes 	int error;
292df8bae1dSRodney W. Grimes 	int uerror, lerror;
293df8bae1dSRodney W. Grimes 	struct vnode *uppervp, *lowervp;
294df8bae1dSRodney W. Grimes 	struct vnode *upperdvp, *lowerdvp;
2952a31267eSMatthew Dillon 	struct vnode *dvp = ap->a_dvp;		/* starting dir */
2962a31267eSMatthew Dillon 	struct union_node *dun = VTOUNION(dvp);	/* associated union node */
297df8bae1dSRodney W. Grimes 	struct componentname *cnp = ap->a_cnp;
298996c772fSJohn Dyson 	struct proc *p = cnp->cn_proc;
299df8bae1dSRodney W. Grimes 	int lockparent = cnp->cn_flags & LOCKPARENT;
300df8bae1dSRodney W. Grimes 	struct union_mount *um = MOUNTTOUNIONMOUNT(dvp->v_mount);
30127ed09c2SMatthew Dillon 	struct ucred *saved_cred = NULL;
302996c772fSJohn Dyson 	int iswhiteout;
303996c772fSJohn Dyson 	struct vattr va;
304996c772fSJohn Dyson 
3052a31267eSMatthew Dillon 	*ap->a_vpp = NULLVP;
3066ca02614SKATO Takenori 
3076ca02614SKATO Takenori 	/*
3086ca02614SKATO Takenori 	 * Disallow write attemps to the filesystem mounted read-only.
3096ca02614SKATO Takenori 	 */
3102a31267eSMatthew Dillon 	if ((cnp->cn_flags & ISLASTCN) &&
3112a31267eSMatthew Dillon 	    (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
3122a31267eSMatthew Dillon 	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
3136ca02614SKATO Takenori 		return (EROFS);
314996c772fSJohn Dyson 	}
315df8bae1dSRodney W. Grimes 
3162a31267eSMatthew Dillon 	/*
3172a31267eSMatthew Dillon 	 * For any lookup's we do, always return with the parent locked
3182a31267eSMatthew Dillon 	 */
319df8bae1dSRodney W. Grimes 	cnp->cn_flags |= LOCKPARENT;
320df8bae1dSRodney W. Grimes 
321df8bae1dSRodney W. Grimes 	lowerdvp = dun->un_lowervp;
322df8bae1dSRodney W. Grimes 	uppervp = NULLVP;
323df8bae1dSRodney W. Grimes 	lowervp = NULLVP;
324996c772fSJohn Dyson 	iswhiteout = 0;
325df8bae1dSRodney W. Grimes 
3262a31267eSMatthew Dillon 	uerror = ENOENT;
3272a31267eSMatthew Dillon 	lerror = ENOENT;
3282a31267eSMatthew Dillon 
3292a31267eSMatthew Dillon 	/*
3302a31267eSMatthew Dillon 	 * Get a private lock on uppervp and a reference, effectively
3312a31267eSMatthew Dillon 	 * taking it out of the union_node's control.
3322a31267eSMatthew Dillon 	 *
3332a31267eSMatthew Dillon 	 * We must lock upperdvp while holding our lock on dvp
3342a31267eSMatthew Dillon 	 * to avoid a deadlock.
3352a31267eSMatthew Dillon 	 */
3362a31267eSMatthew Dillon 	upperdvp = union_lock_upper(dun, p);
337afc2a558SKATO Takenori 
338df8bae1dSRodney W. Grimes 	/*
339df8bae1dSRodney W. Grimes 	 * do the lookup in the upper level.
340df8bae1dSRodney W. Grimes 	 * if that level comsumes additional pathnames,
341df8bae1dSRodney W. Grimes 	 * then assume that something special is going
342df8bae1dSRodney W. Grimes 	 * on and just return that vnode.
343df8bae1dSRodney W. Grimes 	 */
344996c772fSJohn Dyson 	if (upperdvp != NULLVP) {
345ee582cdfSKATO Takenori 		/*
3462a31267eSMatthew Dillon 		 * We do not have to worry about the DOTDOT case, we've
3472a31267eSMatthew Dillon 		 * already unlocked dvp.
348ee582cdfSKATO Takenori 		 */
3492a31267eSMatthew Dillon 		UDEBUG(("A %p\n", upperdvp));
3502a31267eSMatthew Dillon 
3512a31267eSMatthew Dillon 		/*
3522a31267eSMatthew Dillon 		 * Do the lookup.   We must supply a locked and referenced
3532a31267eSMatthew Dillon 		 * upperdvp to the function and will get a new locked and
3542a31267eSMatthew Dillon 		 * referenced upperdvp back with the old having been
3552a31267eSMatthew Dillon 		 * dereferenced.
3562a31267eSMatthew Dillon 		 *
3572a31267eSMatthew Dillon 		 * If an error is returned, uppervp will be NULLVP.  If no
3582a31267eSMatthew Dillon 		 * error occurs, uppervp will be the locked and referenced
3592a31267eSMatthew Dillon 		 * return vnode or possibly NULL, depending on what is being
3602a31267eSMatthew Dillon 		 * requested.  It is possible that the returned uppervp
3612a31267eSMatthew Dillon 		 * will be the same as upperdvp.
3622a31267eSMatthew Dillon 		 */
3632a31267eSMatthew Dillon 		uerror = union_lookup1(um->um_uppervp, &upperdvp, &uppervp, cnp);
3642a31267eSMatthew Dillon 		UDEBUG((
3652a31267eSMatthew Dillon 		    "uerror %d upperdvp %p %d/%d, uppervp %p ref=%d/lck=%d\n",
3662a31267eSMatthew Dillon 		    uerror,
3672a31267eSMatthew Dillon 		    upperdvp,
3682a31267eSMatthew Dillon 		    upperdvp->v_usecount,
3692a31267eSMatthew Dillon 		    VOP_ISLOCKED(upperdvp),
3702a31267eSMatthew Dillon 		    uppervp,
3712a31267eSMatthew Dillon 		    (uppervp ? uppervp->v_usecount : -99),
3722a31267eSMatthew Dillon 		    (uppervp ? VOP_ISLOCKED(uppervp) : -99)
3732a31267eSMatthew Dillon 		));
3742a31267eSMatthew Dillon 
3756ca02614SKATO Takenori 		/*
3766ca02614SKATO Takenori 		 * Disallow write attemps to the filesystem mounted read-only.
3776ca02614SKATO Takenori 		 */
3786ca02614SKATO Takenori 		if (uerror == EJUSTRETURN && (cnp->cn_flags & ISLASTCN) &&
3796ca02614SKATO Takenori 		    (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
3806ca02614SKATO Takenori 		    (cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME)) {
3812a31267eSMatthew Dillon 			error = EROFS;
382afc2a558SKATO Takenori 			goto out;
383df8bae1dSRodney W. Grimes 		}
3842a31267eSMatthew Dillon 
3852a31267eSMatthew Dillon 		/*
3862a31267eSMatthew Dillon 		 * Special case.  If cn_consume != 0 skip out.  The result
3872a31267eSMatthew Dillon 		 * of the lookup is transfered to our return variable.  If
3882a31267eSMatthew Dillon 		 * an error occured we have to throw away the results.
3892a31267eSMatthew Dillon 		 */
3902a31267eSMatthew Dillon 
3912a31267eSMatthew Dillon 		if (cnp->cn_consume != 0) {
3922a31267eSMatthew Dillon 			if ((error = uerror) == 0) {
3932a31267eSMatthew Dillon 				*ap->a_vpp = uppervp;
3942a31267eSMatthew Dillon 				uppervp = NULL;
3952a31267eSMatthew Dillon 			}
3962a31267eSMatthew Dillon 			goto out;
3972a31267eSMatthew Dillon 		}
3982a31267eSMatthew Dillon 
3992a31267eSMatthew Dillon 		/*
4002a31267eSMatthew Dillon 		 * Calculate whiteout, fall through
4012a31267eSMatthew Dillon 		 */
4022a31267eSMatthew Dillon 
403996c772fSJohn Dyson 		if (uerror == ENOENT || uerror == EJUSTRETURN) {
404996c772fSJohn Dyson 			if (cnp->cn_flags & ISWHITEOUT) {
405996c772fSJohn Dyson 				iswhiteout = 1;
406996c772fSJohn Dyson 			} else if (lowerdvp != NULLVP) {
407e3a285c7SMatthew Dillon 				int terror;
408e3a285c7SMatthew Dillon 
409e3a285c7SMatthew Dillon 				terror = VOP_GETATTR(upperdvp, &va,
410996c772fSJohn Dyson 					cnp->cn_cred, cnp->cn_proc);
411e3a285c7SMatthew Dillon 				if (terror == 0 && (va.va_flags & OPAQUE))
412996c772fSJohn Dyson 					iswhiteout = 1;
413996c772fSJohn Dyson 			}
414996c772fSJohn Dyson 		}
415df8bae1dSRodney W. Grimes 	}
416df8bae1dSRodney W. Grimes 
417df8bae1dSRodney W. Grimes 	/*
418df8bae1dSRodney W. Grimes 	 * in a similar way to the upper layer, do the lookup
419df8bae1dSRodney W. Grimes 	 * in the lower layer.   this time, if there is some
420df8bae1dSRodney W. Grimes 	 * component magic going on, then vput whatever we got
421df8bae1dSRodney W. Grimes 	 * back from the upper layer and return the lower vnode
422df8bae1dSRodney W. Grimes 	 * instead.
423df8bae1dSRodney W. Grimes 	 */
4242a31267eSMatthew Dillon 
425996c772fSJohn Dyson 	if (lowerdvp != NULLVP && !iswhiteout) {
426df8bae1dSRodney W. Grimes 		int nameiop;
427df8bae1dSRodney W. Grimes 
4282a31267eSMatthew Dillon 		UDEBUG(("B %p\n", lowerdvp));
429df8bae1dSRodney W. Grimes 
430df8bae1dSRodney W. Grimes 		/*
4312a31267eSMatthew Dillon 		 * Force only LOOKUPs on the lower node, since
432df8bae1dSRodney W. Grimes 		 * we won't be making changes to it anyway.
433df8bae1dSRodney W. Grimes 		 */
434df8bae1dSRodney W. Grimes 		nameiop = cnp->cn_nameiop;
435df8bae1dSRodney W. Grimes 		cnp->cn_nameiop = LOOKUP;
436df8bae1dSRodney W. Grimes 		if (um->um_op == UNMNT_BELOW) {
437df8bae1dSRodney W. Grimes 			saved_cred = cnp->cn_cred;
438df8bae1dSRodney W. Grimes 			cnp->cn_cred = um->um_cred;
439df8bae1dSRodney W. Grimes 		}
4402a31267eSMatthew Dillon 
441ee582cdfSKATO Takenori 		/*
442ee582cdfSKATO Takenori 		 * We shouldn't have to worry about locking interactions
443ee582cdfSKATO Takenori 		 * between the lower layer and our union layer (w.r.t.
444ee582cdfSKATO Takenori 		 * `..' processing) because we don't futz with lowervp
445ee582cdfSKATO Takenori 		 * locks in the union-node instantiation code path.
4462a31267eSMatthew Dillon 		 *
4472a31267eSMatthew Dillon 		 * union_lookup1() requires lowervp to be locked on entry,
4482a31267eSMatthew Dillon 		 * and it will be unlocked on return.  The ref count will
4492a31267eSMatthew Dillon 		 * not change.  On return lowervp doesn't represent anything
4502a31267eSMatthew Dillon 		 * to us so we NULL it out.
451ee582cdfSKATO Takenori 		 */
4522a31267eSMatthew Dillon 		VREF(lowerdvp);
4532a31267eSMatthew Dillon 		vn_lock(lowerdvp, LK_EXCLUSIVE | LK_RETRY, p);
4542a31267eSMatthew Dillon 		lerror = union_lookup1(um->um_lowervp, &lowerdvp, &lowervp, cnp);
4552a31267eSMatthew Dillon 		if (lowerdvp == lowervp)
4562a31267eSMatthew Dillon 			vrele(lowerdvp);
4572a31267eSMatthew Dillon 		else
4582a31267eSMatthew Dillon 			vput(lowerdvp);
4592a31267eSMatthew Dillon 		lowerdvp = NULL;	/* lowerdvp invalid after vput */
4602a31267eSMatthew Dillon 
461df8bae1dSRodney W. Grimes 		if (um->um_op == UNMNT_BELOW)
462df8bae1dSRodney W. Grimes 			cnp->cn_cred = saved_cred;
463df8bae1dSRodney W. Grimes 		cnp->cn_nameiop = nameiop;
464df8bae1dSRodney W. Grimes 
465f8fc96b5SKATO Takenori 		if (cnp->cn_consume != 0 || lerror == EACCES) {
4662a31267eSMatthew Dillon 			if ((error = lerror) == 0) {
467df8bae1dSRodney W. Grimes 				*ap->a_vpp = lowervp;
4682a31267eSMatthew Dillon 				lowervp = NULL;
4692a31267eSMatthew Dillon 			}
470afc2a558SKATO Takenori 			goto out;
471df8bae1dSRodney W. Grimes 		}
472df8bae1dSRodney W. Grimes 	} else {
4732a31267eSMatthew Dillon 		UDEBUG(("C %p\n", lowerdvp));
474996c772fSJohn Dyson 		if ((cnp->cn_flags & ISDOTDOT) && dun->un_pvp != NULLVP) {
4752a31267eSMatthew Dillon 			if ((lowervp = LOWERVP(dun->un_pvp)) != NULL) {
476996c772fSJohn Dyson 				VREF(lowervp);
477996c772fSJohn Dyson 				vn_lock(lowervp, LK_EXCLUSIVE | LK_RETRY, p);
478996c772fSJohn Dyson 				lerror = 0;
479996c772fSJohn Dyson 			}
480996c772fSJohn Dyson 		}
481df8bae1dSRodney W. Grimes 	}
482df8bae1dSRodney W. Grimes 
483df8bae1dSRodney W. Grimes 	/*
4842a31267eSMatthew Dillon 	 * Ok.  Now we have uerror, uppervp, upperdvp, lerror, and lowervp.
485df8bae1dSRodney W. Grimes 	 *
4862a31267eSMatthew Dillon 	 * 1. If both layers returned an error, select the upper layer.
487df8bae1dSRodney W. Grimes 	 *
4882a31267eSMatthew Dillon 	 * 2. If the upper layer faile and the bottom layer succeeded,
4892a31267eSMatthew Dillon 	 *    two subcases occur:
490df8bae1dSRodney W. Grimes 	 *
4912a31267eSMatthew Dillon 	 *	a.  The bottom vnode is not a directory, in which case
4922a31267eSMatthew Dillon 	 *	    just return a new union vnode referencing an
4932a31267eSMatthew Dillon 	 *	    empty top layer and the existing bottom layer.
4942a31267eSMatthew Dillon 	 *
4952a31267eSMatthew Dillon 	 *	b.  The button vnode is a directory, in which case
4962a31267eSMatthew Dillon 	 *	    create a new directory in the top layer and
4972a31267eSMatthew Dillon 	 *	    and fall through to case 3.
4982a31267eSMatthew Dillon 	 *
4992a31267eSMatthew Dillon 	 * 3. If the top layer succeeded then return a new union
500df8bae1dSRodney W. Grimes 	 *    vnode referencing whatever the new top layer and
501df8bae1dSRodney W. Grimes 	 *    whatever the bottom layer returned.
502df8bae1dSRodney W. Grimes 	 */
503df8bae1dSRodney W. Grimes 
504df8bae1dSRodney W. Grimes 	/* case 1. */
505df8bae1dSRodney W. Grimes 	if ((uerror != 0) && (lerror != 0)) {
506afc2a558SKATO Takenori 		error = uerror;
507afc2a558SKATO Takenori 		goto out;
508df8bae1dSRodney W. Grimes 	}
509df8bae1dSRodney W. Grimes 
510df8bae1dSRodney W. Grimes 	/* case 2. */
511df8bae1dSRodney W. Grimes 	if (uerror != 0 /* && (lerror == 0) */ ) {
512df8bae1dSRodney W. Grimes 		if (lowervp->v_type == VDIR) { /* case 2b. */
5132a31267eSMatthew Dillon 			KASSERT(uppervp == NULL, ("uppervp unexpectedly non-NULL"));
5142a31267eSMatthew Dillon 			/*
5152a31267eSMatthew Dillon 			 * oops, uppervp has a problem, we may have to shadow.
5162a31267eSMatthew Dillon 			 */
517df8bae1dSRodney W. Grimes 			uerror = union_mkshadow(um, upperdvp, cnp, &uppervp);
518df8bae1dSRodney W. Grimes 			if (uerror) {
519afc2a558SKATO Takenori 				error = uerror;
520afc2a558SKATO Takenori 				goto out;
521df8bae1dSRodney W. Grimes 			}
522df8bae1dSRodney W. Grimes 		}
523df8bae1dSRodney W. Grimes 	}
524df8bae1dSRodney W. Grimes 
5252a31267eSMatthew Dillon 	/*
5262a31267eSMatthew Dillon 	 * Must call union_allocvp with both the upper and lower vnodes
5272a31267eSMatthew Dillon 	 * referenced and the upper vnode locked.   ap->a_vpp is returned
5282a31267eSMatthew Dillon 	 * referenced and locked.  lowervp, uppervp, and upperdvp are
5292a31267eSMatthew Dillon 	 * absorbed by union_allocvp() whether it succeeds or fails.
5302a31267eSMatthew Dillon 	 *
5312a31267eSMatthew Dillon 	 * upperdvp is the parent directory of uppervp which may be
5322a31267eSMatthew Dillon 	 * different, depending on the path, from dvp->un_uppervp.  That's
5332a31267eSMatthew Dillon 	 * why it is a separate argument.  Note that it must be unlocked.
5342a31267eSMatthew Dillon 	 *
5352a31267eSMatthew Dillon 	 * dvp must be locked on entry to the call and will be locked on
5362a31267eSMatthew Dillon 	 * return.
5372a31267eSMatthew Dillon 	 */
5382a31267eSMatthew Dillon 
5392a31267eSMatthew Dillon 	if (uppervp && uppervp != upperdvp)
5402a31267eSMatthew Dillon 		VOP_UNLOCK(uppervp, 0, p);
5412a31267eSMatthew Dillon 	if (lowervp)
542996c772fSJohn Dyson 		VOP_UNLOCK(lowervp, 0, p);
5432a31267eSMatthew Dillon 	if (upperdvp)
5442a31267eSMatthew Dillon 		VOP_UNLOCK(upperdvp, 0, p);
545df8bae1dSRodney W. Grimes 
546df8bae1dSRodney W. Grimes 	error = union_allocvp(ap->a_vpp, dvp->v_mount, dvp, upperdvp, cnp,
547996c772fSJohn Dyson 			      uppervp, lowervp, 1);
548df8bae1dSRodney W. Grimes 
5492a31267eSMatthew Dillon 	UDEBUG(("Create %p = %p %p refs=%d\n", *ap->a_vpp, uppervp, lowervp, (*ap->a_vpp) ? ((*ap->a_vpp)->v_usecount) : -99));
5502a31267eSMatthew Dillon 
5512a31267eSMatthew Dillon 	uppervp = NULL;
5522a31267eSMatthew Dillon 	upperdvp = NULL;
5532a31267eSMatthew Dillon 	lowervp = NULL;
5542a31267eSMatthew Dillon 
5552a31267eSMatthew Dillon 	/*
5562a31267eSMatthew Dillon 	 *	Termination Code
5572a31267eSMatthew Dillon 	 *
5582a31267eSMatthew Dillon 	 *	- put away any extra junk laying around.  Note that lowervp
5592a31267eSMatthew Dillon 	 *	  (if not NULL) will never be the same as *ap->a_vp and
5602a31267eSMatthew Dillon 	 *	  neither will uppervp, because when we set that state we
5612a31267eSMatthew Dillon 	 *	  NULL-out lowervp or uppervp.  On the otherhand, upperdvp
5622a31267eSMatthew Dillon 	 *	  may match uppervp or *ap->a_vpp.
5632a31267eSMatthew Dillon 	 *
5642a31267eSMatthew Dillon 	 *	- relock/unlock dvp if appropriate.
5652a31267eSMatthew Dillon 	 */
5662a31267eSMatthew Dillon 
5672a31267eSMatthew Dillon out:
5682a31267eSMatthew Dillon 	if (upperdvp) {
5692a31267eSMatthew Dillon 		if (upperdvp == uppervp || upperdvp == *ap->a_vpp)
5702a31267eSMatthew Dillon 			vrele(upperdvp);
5712a31267eSMatthew Dillon 		else
5722a31267eSMatthew Dillon 			vput(upperdvp);
5732a31267eSMatthew Dillon 	}
5742a31267eSMatthew Dillon 
5752a31267eSMatthew Dillon 	if (uppervp)
576df8bae1dSRodney W. Grimes 		vput(uppervp);
5772a31267eSMatthew Dillon 
5782a31267eSMatthew Dillon 	if (lowervp)
5792a31267eSMatthew Dillon 		vput(lowervp);
5802a31267eSMatthew Dillon 
5812a31267eSMatthew Dillon 	/*
5822a31267eSMatthew Dillon 	 * Restore LOCKPARENT state
5832a31267eSMatthew Dillon 	 */
5842a31267eSMatthew Dillon 
5852a31267eSMatthew Dillon 	if (!lockparent)
5862a31267eSMatthew Dillon 		cnp->cn_flags &= ~LOCKPARENT;
5872a31267eSMatthew Dillon 
5882a31267eSMatthew Dillon 	UDEBUG(("Out %d vpp %p/%d lower %p upper %p\n", error, *ap->a_vpp,
5892a31267eSMatthew Dillon 		((*ap->a_vpp) ? (*ap->a_vpp)->v_usecount : -99),
5902a31267eSMatthew Dillon 		lowervp, uppervp));
5912a31267eSMatthew Dillon 
5922a31267eSMatthew Dillon 	/*
5932a31267eSMatthew Dillon 	 * dvp lock state, determine whether to relock dvp.  dvp is expected
5942a31267eSMatthew Dillon 	 * to be locked on return if:
5952a31267eSMatthew Dillon 	 *
5962a31267eSMatthew Dillon 	 *	- there was an error (except not EJUSTRETURN), or
5972a31267eSMatthew Dillon 	 *	- we hit the last component and lockparent is true
5982a31267eSMatthew Dillon 	 *
5992a31267eSMatthew Dillon 	 * dvp_is_locked is the current state of the dvp lock, not counting
6002a31267eSMatthew Dillon 	 * the possibility that *ap->a_vpp == dvp (in which case it is locked
6012a31267eSMatthew Dillon 	 * anyway).  Note that *ap->a_vpp == dvp only if no error occured.
6022a31267eSMatthew Dillon 	 */
6032a31267eSMatthew Dillon 
6042a31267eSMatthew Dillon 	if (*ap->a_vpp != dvp) {
6052a31267eSMatthew Dillon 		if ((error == 0 || error == EJUSTRETURN) &&
6062a31267eSMatthew Dillon 		    (!lockparent || (cnp->cn_flags & ISLASTCN) == 0)) {
607996c772fSJohn Dyson 			VOP_UNLOCK(dvp, 0, p);
6082a31267eSMatthew Dillon 		}
6092a31267eSMatthew Dillon 	}
6102a31267eSMatthew Dillon 
6112a31267eSMatthew Dillon 	/*
6122a31267eSMatthew Dillon 	 * Diagnostics
6132a31267eSMatthew Dillon 	 */
6142a31267eSMatthew Dillon 
615f5a5311eSKATO Takenori #ifdef DIAGNOSTIC
616f5a5311eSKATO Takenori 	if (cnp->cn_namelen == 1 &&
617f5a5311eSKATO Takenori 	    cnp->cn_nameptr[0] == '.' &&
618f5a5311eSKATO Takenori 	    *ap->a_vpp != dvp) {
6192a31267eSMatthew Dillon 		panic("union_lookup returning . (%p) not same as startdir (%p)", ap->a_vpp, dvp);
620f5a5311eSKATO Takenori 	}
621f5a5311eSKATO Takenori #endif
622afc2a558SKATO Takenori 
623df8bae1dSRodney W. Grimes 	return (error);
624df8bae1dSRodney W. Grimes }
625df8bae1dSRodney W. Grimes 
6262a31267eSMatthew Dillon /*
6272a31267eSMatthew Dillon  * 	union_create:
6282a31267eSMatthew Dillon  *
6292a31267eSMatthew Dillon  * a_dvp is locked on entry and remains locked on return.  a_vpp is returned
6302a31267eSMatthew Dillon  * locked if no error occurs, otherwise it is garbage.
6312a31267eSMatthew Dillon  */
6322a31267eSMatthew Dillon 
633c9bf0111SKATO Takenori static int
634df8bae1dSRodney W. Grimes union_create(ap)
635df8bae1dSRodney W. Grimes 	struct vop_create_args /* {
636df8bae1dSRodney W. Grimes 		struct vnode *a_dvp;
637df8bae1dSRodney W. Grimes 		struct vnode **a_vpp;
638df8bae1dSRodney W. Grimes 		struct componentname *a_cnp;
639df8bae1dSRodney W. Grimes 		struct vattr *a_vap;
640df8bae1dSRodney W. Grimes 	} */ *ap;
641df8bae1dSRodney W. Grimes {
6427be2d300SMike Smith 	struct union_node *dun = VTOUNION(ap->a_dvp);
643996c772fSJohn Dyson 	struct componentname *cnp = ap->a_cnp;
644996c772fSJohn Dyson 	struct proc *p = cnp->cn_proc;
6452a31267eSMatthew Dillon 	struct vnode *dvp;
6462a31267eSMatthew Dillon 	int error = EROFS;
647df8bae1dSRodney W. Grimes 
6482a31267eSMatthew Dillon 	if ((dvp = union_lock_upper(dun, p)) != NULL) {
649df8bae1dSRodney W. Grimes 		struct vnode *vp;
650996c772fSJohn Dyson 		struct mount *mp;
651df8bae1dSRodney W. Grimes 
652996c772fSJohn Dyson 		error = VOP_CREATE(dvp, &vp, cnp, ap->a_vap);
6532a31267eSMatthew Dillon 		if (error == 0) {
6547be2d300SMike Smith 			mp = ap->a_dvp->v_mount;
6552a31267eSMatthew Dillon 			VOP_UNLOCK(vp, 0, p);
6562a31267eSMatthew Dillon 			UDEBUG(("ALLOCVP-1 FROM %p REFS %d\n", vp, vp->v_usecount));
6572a31267eSMatthew Dillon 			error = union_allocvp(ap->a_vpp, mp, NULLVP, NULLVP,
6582a31267eSMatthew Dillon 				cnp, vp, NULLVP, 1);
6592a31267eSMatthew Dillon 			UDEBUG(("ALLOCVP-2B FROM %p REFS %d\n", *ap->a_vpp, vp->v_usecount));
660df8bae1dSRodney W. Grimes 		}
6612a31267eSMatthew Dillon 		union_unlock_upper(dvp, p);
6622a31267eSMatthew Dillon 	}
6632a31267eSMatthew Dillon 	return (error);
664df8bae1dSRodney W. Grimes }
665df8bae1dSRodney W. Grimes 
666c9bf0111SKATO Takenori static int
667996c772fSJohn Dyson union_whiteout(ap)
668996c772fSJohn Dyson 	struct vop_whiteout_args /* {
669996c772fSJohn Dyson 		struct vnode *a_dvp;
670996c772fSJohn Dyson 		struct componentname *a_cnp;
671996c772fSJohn Dyson 		int a_flags;
672996c772fSJohn Dyson 	} */ *ap;
673996c772fSJohn Dyson {
674996c772fSJohn Dyson 	struct union_node *un = VTOUNION(ap->a_dvp);
675996c772fSJohn Dyson 	struct componentname *cnp = ap->a_cnp;
6762a31267eSMatthew Dillon 	struct vnode *uppervp;
6772a31267eSMatthew Dillon 	int error = EOPNOTSUPP;
678996c772fSJohn Dyson 
6792a31267eSMatthew Dillon 	if ((uppervp = union_lock_upper(un, cnp->cn_proc)) != NULLVP) {
6802a31267eSMatthew Dillon 		error = VOP_WHITEOUT(un->un_uppervp, cnp, ap->a_flags);
6812a31267eSMatthew Dillon 		union_unlock_upper(uppervp, cnp->cn_proc);
682996c772fSJohn Dyson 	}
6832a31267eSMatthew Dillon 	return(error);
6842a31267eSMatthew Dillon }
6852a31267eSMatthew Dillon 
6862a31267eSMatthew Dillon /*
6872a31267eSMatthew Dillon  * 	union_mknod:
6882a31267eSMatthew Dillon  *
6892a31267eSMatthew Dillon  *	a_dvp is locked on entry and should remain locked on return.
6902a31267eSMatthew Dillon  *	a_vpp is garbagre whether an error occurs or not.
6912a31267eSMatthew Dillon  */
692996c772fSJohn Dyson 
693c9bf0111SKATO Takenori static int
694df8bae1dSRodney W. Grimes union_mknod(ap)
695df8bae1dSRodney W. Grimes 	struct vop_mknod_args /* {
696df8bae1dSRodney W. Grimes 		struct vnode *a_dvp;
697df8bae1dSRodney W. Grimes 		struct vnode **a_vpp;
698df8bae1dSRodney W. Grimes 		struct componentname *a_cnp;
699df8bae1dSRodney W. Grimes 		struct vattr *a_vap;
700df8bae1dSRodney W. Grimes 	} */ *ap;
701df8bae1dSRodney W. Grimes {
7027be2d300SMike Smith 	struct union_node *dun = VTOUNION(ap->a_dvp);
703996c772fSJohn Dyson 	struct componentname *cnp = ap->a_cnp;
7042a31267eSMatthew Dillon 	struct vnode *dvp;
7052a31267eSMatthew Dillon 	int error = EROFS;
706df8bae1dSRodney W. Grimes 
7072a31267eSMatthew Dillon 	if ((dvp = union_lock_upper(dun, cnp->cn_proc)) != NULL) {
708df8bae1dSRodney W. Grimes 		struct vnode *vp;
709996c772fSJohn Dyson 		error = VOP_MKNOD(dvp, &vp, cnp, ap->a_vap);
7102a31267eSMatthew Dillon 		/* vp is garbage whether an error occurs or not */
7112a31267eSMatthew Dillon 		union_unlock_upper(dvp, cnp->cn_proc);
712df8bae1dSRodney W. Grimes 	}
713df8bae1dSRodney W. Grimes 	return (error);
714df8bae1dSRodney W. Grimes }
715df8bae1dSRodney W. Grimes 
7162a31267eSMatthew Dillon /*
7172a31267eSMatthew Dillon  *	union_open:
7182a31267eSMatthew Dillon  *
7192a31267eSMatthew Dillon  *	run open VOP.  When opening the underlying vnode we have to mimic
7202a31267eSMatthew Dillon  *	vn_open.  What we *really* need to do to avoid screwups if the
7212a31267eSMatthew Dillon  *	open semantics change is to call vn_open().  For example, ufs blows
7222a31267eSMatthew Dillon  *	up if you open a file but do not vmio it prior to writing.
7232a31267eSMatthew Dillon  */
724df8bae1dSRodney W. Grimes 
725c9bf0111SKATO Takenori static int
726df8bae1dSRodney W. Grimes union_open(ap)
727df8bae1dSRodney W. Grimes 	struct vop_open_args /* {
728df8bae1dSRodney W. Grimes 		struct vnodeop_desc *a_desc;
729df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
730df8bae1dSRodney W. Grimes 		int a_mode;
731df8bae1dSRodney W. Grimes 		struct ucred *a_cred;
732df8bae1dSRodney W. Grimes 		struct proc *a_p;
733df8bae1dSRodney W. Grimes 	} */ *ap;
734df8bae1dSRodney W. Grimes {
735df8bae1dSRodney W. Grimes 	struct union_node *un = VTOUNION(ap->a_vp);
736df8bae1dSRodney W. Grimes 	struct vnode *tvp;
737df8bae1dSRodney W. Grimes 	int mode = ap->a_mode;
738df8bae1dSRodney W. Grimes 	struct ucred *cred = ap->a_cred;
739df8bae1dSRodney W. Grimes 	struct proc *p = ap->a_p;
7402a31267eSMatthew Dillon 	int error = 0;
7412a31267eSMatthew Dillon 	int tvpisupper = 1;
742df8bae1dSRodney W. Grimes 
743df8bae1dSRodney W. Grimes 	/*
744df8bae1dSRodney W. Grimes 	 * If there is an existing upper vp then simply open that.
7452a31267eSMatthew Dillon 	 * The upper vp takes precedence over the lower vp.  When opening
7462a31267eSMatthew Dillon 	 * a lower vp for writing copy it to the uppervp and then open the
7472a31267eSMatthew Dillon 	 * uppervp.
7482a31267eSMatthew Dillon 	 *
7492a31267eSMatthew Dillon 	 * At the end of this section tvp will be left locked.
750df8bae1dSRodney W. Grimes 	 */
7512a31267eSMatthew Dillon 	if ((tvp = union_lock_upper(un, p)) == NULLVP) {
752df8bae1dSRodney W. Grimes 		/*
753df8bae1dSRodney W. Grimes 		 * If the lower vnode is being opened for writing, then
754df8bae1dSRodney W. Grimes 		 * copy the file contents to the upper vnode and open that,
755df8bae1dSRodney W. Grimes 		 * otherwise can simply open the lower vnode.
756df8bae1dSRodney W. Grimes 		 */
757df8bae1dSRodney W. Grimes 		tvp = un->un_lowervp;
758df8bae1dSRodney W. Grimes 		if ((ap->a_mode & FWRITE) && (tvp->v_type == VREG)) {
7592a31267eSMatthew Dillon 			int docopy = !(mode & O_TRUNC);
7602a31267eSMatthew Dillon 			error = union_copyup(un, docopy, cred, p);
7612a31267eSMatthew Dillon 			tvp = union_lock_upper(un, p);
7622a31267eSMatthew Dillon 		} else {
7632a31267eSMatthew Dillon 			un->un_openl++;
7642a31267eSMatthew Dillon 			VREF(tvp);
7652a31267eSMatthew Dillon 			vn_lock(tvp, LK_EXCLUSIVE | LK_RETRY, p);
7662a31267eSMatthew Dillon 			tvpisupper = 0;
7672a31267eSMatthew Dillon 		}
7682a31267eSMatthew Dillon 	}
7692a31267eSMatthew Dillon 
7702a31267eSMatthew Dillon 	/*
7712a31267eSMatthew Dillon 	 * We are holding the correct vnode, open it
7722a31267eSMatthew Dillon 	 */
7732a31267eSMatthew Dillon 
774df8bae1dSRodney W. Grimes 	if (error == 0)
7752a31267eSMatthew Dillon 		error = VOP_OPEN(tvp, mode, cred, p);
7762a31267eSMatthew Dillon 
7772a31267eSMatthew Dillon 	/*
7782a31267eSMatthew Dillon 	 * Absolutely necessary or UFS will blowup
7792a31267eSMatthew Dillon 	 */
7802a31267eSMatthew Dillon         if (error == 0 && vn_canvmio(tvp) == TRUE) {
7812a31267eSMatthew Dillon                 error = vfs_object_create(tvp, p, cred);
7822a31267eSMatthew Dillon         }
7832a31267eSMatthew Dillon 
7842a31267eSMatthew Dillon 	/*
7852a31267eSMatthew Dillon 	 * Release any locks held
7862a31267eSMatthew Dillon 	 */
7872a31267eSMatthew Dillon 	if (tvpisupper) {
7882a31267eSMatthew Dillon 		if (tvp)
7892a31267eSMatthew Dillon 			union_unlock_upper(tvp, p);
7902a31267eSMatthew Dillon 	} else {
7912a31267eSMatthew Dillon 		vput(tvp);
7922a31267eSMatthew Dillon 	}
793df8bae1dSRodney W. Grimes 	return (error);
794df8bae1dSRodney W. Grimes }
795df8bae1dSRodney W. Grimes 
796df8bae1dSRodney W. Grimes /*
7972a31267eSMatthew Dillon  *	union_close:
7982a31267eSMatthew Dillon  *
7992a31267eSMatthew Dillon  *	It is unclear whether a_vp is passed locked or unlocked.  Whatever
8002a31267eSMatthew Dillon  *	the case we do not change it.
801df8bae1dSRodney W. Grimes  */
802df8bae1dSRodney W. Grimes 
803c9bf0111SKATO Takenori static int
804df8bae1dSRodney W. Grimes union_close(ap)
805df8bae1dSRodney W. Grimes 	struct vop_close_args /* {
806df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
807df8bae1dSRodney W. Grimes 		int  a_fflag;
808df8bae1dSRodney W. Grimes 		struct ucred *a_cred;
809df8bae1dSRodney W. Grimes 		struct proc *a_p;
810df8bae1dSRodney W. Grimes 	} */ *ap;
811df8bae1dSRodney W. Grimes {
812df8bae1dSRodney W. Grimes 	struct union_node *un = VTOUNION(ap->a_vp);
813df8bae1dSRodney W. Grimes 	struct vnode *vp;
814df8bae1dSRodney W. Grimes 
815996c772fSJohn Dyson 	if ((vp = un->un_uppervp) == NULLVP) {
816df8bae1dSRodney W. Grimes #ifdef UNION_DIAGNOSTIC
817df8bae1dSRodney W. Grimes 		if (un->un_openl <= 0)
818df8bae1dSRodney W. Grimes 			panic("union: un_openl cnt");
819df8bae1dSRodney W. Grimes #endif
820df8bae1dSRodney W. Grimes 		--un->un_openl;
821df8bae1dSRodney W. Grimes 		vp = un->un_lowervp;
822df8bae1dSRodney W. Grimes 	}
823996c772fSJohn Dyson 	ap->a_vp = vp;
824996c772fSJohn Dyson 	return (VCALL(vp, VOFFSET(vop_close), ap));
825df8bae1dSRodney W. Grimes }
826df8bae1dSRodney W. Grimes 
827df8bae1dSRodney W. Grimes /*
828df8bae1dSRodney W. Grimes  * Check access permission on the union vnode.
829df8bae1dSRodney W. Grimes  * The access check being enforced is to check
830df8bae1dSRodney W. Grimes  * against both the underlying vnode, and any
831df8bae1dSRodney W. Grimes  * copied vnode.  This ensures that no additional
832df8bae1dSRodney W. Grimes  * file permissions are given away simply because
833df8bae1dSRodney W. Grimes  * the user caused an implicit file copy.
834df8bae1dSRodney W. Grimes  */
835c9bf0111SKATO Takenori static int
836df8bae1dSRodney W. Grimes union_access(ap)
837df8bae1dSRodney W. Grimes 	struct vop_access_args /* {
838df8bae1dSRodney W. Grimes 		struct vnodeop_desc *a_desc;
839df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
840df8bae1dSRodney W. Grimes 		int a_mode;
841df8bae1dSRodney W. Grimes 		struct ucred *a_cred;
842df8bae1dSRodney W. Grimes 		struct proc *a_p;
843df8bae1dSRodney W. Grimes 	} */ *ap;
844df8bae1dSRodney W. Grimes {
845df8bae1dSRodney W. Grimes 	struct union_node *un = VTOUNION(ap->a_vp);
846996c772fSJohn Dyson 	struct proc *p = ap->a_p;
847df8bae1dSRodney W. Grimes 	int error = EACCES;
848df8bae1dSRodney W. Grimes 	struct vnode *vp;
849df8bae1dSRodney W. Grimes 
8506ca02614SKATO Takenori 	/*
8516ca02614SKATO Takenori 	 * Disallow write attempts on filesystems mounted read-only.
8526ca02614SKATO Takenori 	 */
8532a31267eSMatthew Dillon 	if ((ap->a_mode & VWRITE) &&
8542a31267eSMatthew Dillon 	    (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)) {
8556ca02614SKATO Takenori 		switch (ap->a_vp->v_type) {
856831a80b0SMatthew Dillon 		case VREG:
857831a80b0SMatthew Dillon 		case VDIR:
858831a80b0SMatthew Dillon 		case VLNK:
8596ca02614SKATO Takenori 			return (EROFS);
860831a80b0SMatthew Dillon 		default:
861831a80b0SMatthew Dillon 			break;
8626ca02614SKATO Takenori 		}
8636ca02614SKATO Takenori 	}
8642a31267eSMatthew Dillon 
8652a31267eSMatthew Dillon 	if ((vp = union_lock_upper(un, p)) != NULLVP) {
866996c772fSJohn Dyson 		ap->a_vp = vp;
8672a31267eSMatthew Dillon 		error = VCALL(vp, VOFFSET(vop_access), ap);
8682a31267eSMatthew Dillon 		union_unlock_upper(vp, p);
8692a31267eSMatthew Dillon 		return(error);
870df8bae1dSRodney W. Grimes 	}
871df8bae1dSRodney W. Grimes 
872996c772fSJohn Dyson 	if ((vp = un->un_lowervp) != NULLVP) {
873996c772fSJohn Dyson 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
874996c772fSJohn Dyson 		ap->a_vp = vp;
8752a31267eSMatthew Dillon 
8762a31267eSMatthew Dillon 		/*
8772a31267eSMatthew Dillon 		 * Remove VWRITE from a_mode if our mount point is RW, because
8782a31267eSMatthew Dillon 		 * we want to allow writes and lowervp may be read-only.
8792a31267eSMatthew Dillon 		 */
8802a31267eSMatthew Dillon 		if ((un->un_vnode->v_mount->mnt_flag & MNT_RDONLY) == 0)
8812a31267eSMatthew Dillon 			ap->a_mode &= ~VWRITE;
8822a31267eSMatthew Dillon 
883996c772fSJohn Dyson 		error = VCALL(vp, VOFFSET(vop_access), ap);
884df8bae1dSRodney W. Grimes 		if (error == 0) {
8852a31267eSMatthew Dillon 			struct union_mount *um;
8862a31267eSMatthew Dillon 
8872a31267eSMatthew Dillon 			um = MOUNTTOUNIONMOUNT(un->un_vnode->v_mount);
888df8bae1dSRodney W. Grimes 
889996c772fSJohn Dyson 			if (um->um_op == UNMNT_BELOW) {
890996c772fSJohn Dyson 				ap->a_cred = um->um_cred;
891996c772fSJohn Dyson 				error = VCALL(vp, VOFFSET(vop_access), ap);
892df8bae1dSRodney W. Grimes 			}
893996c772fSJohn Dyson 		}
894996c772fSJohn Dyson 		VOP_UNLOCK(vp, 0, p);
895df8bae1dSRodney W. Grimes 	}
896df8bae1dSRodney W. Grimes 	return(error);
897df8bae1dSRodney W. Grimes }
898df8bae1dSRodney W. Grimes 
899df8bae1dSRodney W. Grimes /*
900996c772fSJohn Dyson  * We handle getattr only to change the fsid and
901996c772fSJohn Dyson  * track object sizes
9022a31267eSMatthew Dillon  *
9032a31267eSMatthew Dillon  * It's not clear whether VOP_GETATTR is to be
9042a31267eSMatthew Dillon  * called with the vnode locked or not.  stat() calls
9052a31267eSMatthew Dillon  * it with (vp) locked, and fstat calls it with
9062a31267eSMatthew Dillon  * (vp) unlocked.
9072a31267eSMatthew Dillon  *
9082a31267eSMatthew Dillon  * Because of this we cannot use our normal locking functions
9092a31267eSMatthew Dillon  * if we do not intend to lock the main a_vp node.  At the moment
9102a31267eSMatthew Dillon  * we are running without any specific locking at all, but beware
9112a31267eSMatthew Dillon  * to any programmer that care must be taken if locking is added
9122a31267eSMatthew Dillon  * to this function.
913df8bae1dSRodney W. Grimes  */
9142a31267eSMatthew Dillon 
915c9bf0111SKATO Takenori static int
916df8bae1dSRodney W. Grimes union_getattr(ap)
917df8bae1dSRodney W. Grimes 	struct vop_getattr_args /* {
918df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
919df8bae1dSRodney W. Grimes 		struct vattr *a_vap;
920df8bae1dSRodney W. Grimes 		struct ucred *a_cred;
921df8bae1dSRodney W. Grimes 		struct proc *a_p;
922df8bae1dSRodney W. Grimes 	} */ *ap;
923df8bae1dSRodney W. Grimes {
924df8bae1dSRodney W. Grimes 	int error;
925df8bae1dSRodney W. Grimes 	struct union_node *un = VTOUNION(ap->a_vp);
9262a31267eSMatthew Dillon 	struct vnode *vp;
927df8bae1dSRodney W. Grimes 	struct vattr *vap;
928df8bae1dSRodney W. Grimes 	struct vattr va;
929df8bae1dSRodney W. Grimes 
930df8bae1dSRodney W. Grimes 	/*
931df8bae1dSRodney W. Grimes 	 * Some programs walk the filesystem hierarchy by counting
932df8bae1dSRodney W. Grimes 	 * links to directories to avoid stat'ing all the time.
933df8bae1dSRodney W. Grimes 	 * This means the link count on directories needs to be "correct".
934df8bae1dSRodney W. Grimes 	 * The only way to do that is to call getattr on both layers
935df8bae1dSRodney W. Grimes 	 * and fix up the link count.  The link count will not necessarily
936df8bae1dSRodney W. Grimes 	 * be accurate but will be large enough to defeat the tree walkers.
937df8bae1dSRodney W. Grimes 	 */
938df8bae1dSRodney W. Grimes 
939df8bae1dSRodney W. Grimes 	vap = ap->a_vap;
940df8bae1dSRodney W. Grimes 
9412a31267eSMatthew Dillon 	if ((vp = un->un_uppervp) != NULLVP) {
942df8bae1dSRodney W. Grimes 		error = VOP_GETATTR(vp, vap, ap->a_cred, ap->a_p);
943df8bae1dSRodney W. Grimes 		if (error)
944df8bae1dSRodney W. Grimes 			return (error);
9452a31267eSMatthew Dillon 		/* XXX isn't this dangerouso without a lock? */
946996c772fSJohn Dyson 		union_newsize(ap->a_vp, vap->va_size, VNOVAL);
947df8bae1dSRodney W. Grimes 	}
948df8bae1dSRodney W. Grimes 
949df8bae1dSRodney W. Grimes 	if (vp == NULLVP) {
950df8bae1dSRodney W. Grimes 		vp = un->un_lowervp;
951b2bde4cbSKATO Takenori 	} else if (vp->v_type == VDIR && un->un_lowervp != NULLVP) {
952df8bae1dSRodney W. Grimes 		vp = un->un_lowervp;
953df8bae1dSRodney W. Grimes 		vap = &va;
954df8bae1dSRodney W. Grimes 	} else {
955df8bae1dSRodney W. Grimes 		vp = NULLVP;
956df8bae1dSRodney W. Grimes 	}
957df8bae1dSRodney W. Grimes 
958df8bae1dSRodney W. Grimes 	if (vp != NULLVP) {
959df8bae1dSRodney W. Grimes 		error = VOP_GETATTR(vp, vap, ap->a_cred, ap->a_p);
960df8bae1dSRodney W. Grimes 		if (error)
961df8bae1dSRodney W. Grimes 			return (error);
9622a31267eSMatthew Dillon 		/* XXX isn't this dangerous without a lock? */
963996c772fSJohn Dyson 		union_newsize(ap->a_vp, VNOVAL, vap->va_size);
964df8bae1dSRodney W. Grimes 	}
965df8bae1dSRodney W. Grimes 
966df8bae1dSRodney W. Grimes 	if ((vap != ap->a_vap) && (vap->va_type == VDIR))
967df8bae1dSRodney W. Grimes 		ap->a_vap->va_nlink += vap->va_nlink;
968df8bae1dSRodney W. Grimes 	return (0);
969df8bae1dSRodney W. Grimes }
970df8bae1dSRodney W. Grimes 
971c9bf0111SKATO Takenori static int
972df8bae1dSRodney W. Grimes union_setattr(ap)
973df8bae1dSRodney W. Grimes 	struct vop_setattr_args /* {
974df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
975df8bae1dSRodney W. Grimes 		struct vattr *a_vap;
976df8bae1dSRodney W. Grimes 		struct ucred *a_cred;
977df8bae1dSRodney W. Grimes 		struct proc *a_p;
978df8bae1dSRodney W. Grimes 	} */ *ap;
979df8bae1dSRodney W. Grimes {
980df8bae1dSRodney W. Grimes 	struct union_node *un = VTOUNION(ap->a_vp);
981996c772fSJohn Dyson 	struct proc *p = ap->a_p;
9826ca02614SKATO Takenori 	struct vattr *vap = ap->a_vap;
9832a31267eSMatthew Dillon 	struct vnode *uppervp;
984df8bae1dSRodney W. Grimes 	int error;
985df8bae1dSRodney W. Grimes 
986df8bae1dSRodney W. Grimes 	/*
9876ca02614SKATO Takenori 	 * Disallow write attempts on filesystems mounted read-only.
9886ca02614SKATO Takenori 	 */
9896ca02614SKATO Takenori 	if ((ap->a_vp->v_mount->mnt_flag & MNT_RDONLY) &&
9906ca02614SKATO Takenori 	    (vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
9916ca02614SKATO Takenori 	     vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
9922a31267eSMatthew Dillon 	     vap->va_mtime.tv_sec != VNOVAL ||
9932a31267eSMatthew Dillon 	     vap->va_mode != (mode_t)VNOVAL)) {
9946ca02614SKATO Takenori 		return (EROFS);
9952a31267eSMatthew Dillon 	}
9966ca02614SKATO Takenori 
9976ca02614SKATO Takenori 	/*
998df8bae1dSRodney W. Grimes 	 * Handle case of truncating lower object to zero size,
999df8bae1dSRodney W. Grimes 	 * by creating a zero length upper object.  This is to
1000df8bae1dSRodney W. Grimes 	 * handle the case of open with O_TRUNC and O_CREAT.
1001df8bae1dSRodney W. Grimes 	 */
10022a31267eSMatthew Dillon 	if (un->un_uppervp == NULLVP && (un->un_lowervp->v_type == VREG)) {
1003996c772fSJohn Dyson 		error = union_copyup(un, (ap->a_vap->va_size != 0),
1004996c772fSJohn Dyson 			    ap->a_cred, ap->a_p);
1005df8bae1dSRodney W. Grimes 		if (error)
1006df8bae1dSRodney W. Grimes 			return (error);
1007df8bae1dSRodney W. Grimes 	}
1008df8bae1dSRodney W. Grimes 
1009df8bae1dSRodney W. Grimes 	/*
1010df8bae1dSRodney W. Grimes 	 * Try to set attributes in upper layer,
1011df8bae1dSRodney W. Grimes 	 * otherwise return read-only filesystem error.
1012df8bae1dSRodney W. Grimes 	 */
10132a31267eSMatthew Dillon 	error = EROFS;
10142a31267eSMatthew Dillon 	if ((uppervp = union_lock_upper(un, p)) != NULLVP) {
1015df8bae1dSRodney W. Grimes 		error = VOP_SETATTR(un->un_uppervp, ap->a_vap,
1016df8bae1dSRodney W. Grimes 					ap->a_cred, ap->a_p);
1017996c772fSJohn Dyson 		if ((error == 0) && (ap->a_vap->va_size != VNOVAL))
1018996c772fSJohn Dyson 			union_newsize(ap->a_vp, ap->a_vap->va_size, VNOVAL);
10192a31267eSMatthew Dillon 		union_unlock_upper(uppervp, p);
10202a31267eSMatthew Dillon 	}
10212a31267eSMatthew Dillon 	return (error);
1022df8bae1dSRodney W. Grimes }
1023df8bae1dSRodney W. Grimes 
10242a31267eSMatthew Dillon /*
10252a31267eSMatthew Dillon  *	union_getpages:
10262a31267eSMatthew Dillon  */
10272a31267eSMatthew Dillon 
10282a31267eSMatthew Dillon static int
10292a31267eSMatthew Dillon union_getpages(struct vop_getpages_args *ap)
10302a31267eSMatthew Dillon {
10312a31267eSMatthew Dillon 	int r;
10322a31267eSMatthew Dillon 
10332a31267eSMatthew Dillon 	r = vnode_pager_generic_getpages(ap->a_vp, ap->a_m,
10342a31267eSMatthew Dillon 		ap->a_count, ap->a_reqpage);
10352a31267eSMatthew Dillon 	return(r);
10362a31267eSMatthew Dillon }
10372a31267eSMatthew Dillon 
10382a31267eSMatthew Dillon /*
10392a31267eSMatthew Dillon  *	union_putpages:
10402a31267eSMatthew Dillon  */
10412a31267eSMatthew Dillon 
10422a31267eSMatthew Dillon static int
10432a31267eSMatthew Dillon union_putpages(struct vop_putpages_args *ap)
10442a31267eSMatthew Dillon {
10452a31267eSMatthew Dillon 	int r;
10462a31267eSMatthew Dillon 
10472a31267eSMatthew Dillon 	r = vnode_pager_generic_putpages(ap->a_vp, ap->a_m, ap->a_count,
10482a31267eSMatthew Dillon 		ap->a_sync, ap->a_rtvals);
10492a31267eSMatthew Dillon 	return(r);
1050df8bae1dSRodney W. Grimes }
1051df8bae1dSRodney W. Grimes 
1052c9bf0111SKATO Takenori static int
1053df8bae1dSRodney W. Grimes union_read(ap)
1054df8bae1dSRodney W. Grimes 	struct vop_read_args /* {
1055df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
1056df8bae1dSRodney W. Grimes 		struct uio *a_uio;
1057df8bae1dSRodney W. Grimes 		int  a_ioflag;
1058df8bae1dSRodney W. Grimes 		struct ucred *a_cred;
1059df8bae1dSRodney W. Grimes 	} */ *ap;
1060df8bae1dSRodney W. Grimes {
10612a31267eSMatthew Dillon 	struct union_node *un = VTOUNION(ap->a_vp);
1062996c772fSJohn Dyson 	struct proc *p = ap->a_uio->uio_procp;
10632a31267eSMatthew Dillon 	struct vnode *uvp;
10642a31267eSMatthew Dillon 	int error;
1065df8bae1dSRodney W. Grimes 
10662a31267eSMatthew Dillon 	uvp = union_lock_other(un, p);
10672a31267eSMatthew Dillon 	KASSERT(uvp != NULL, ("union_read: backing vnode missing!"));
10682a31267eSMatthew Dillon 
10692a31267eSMatthew Dillon 	if (ap->a_vp->v_flag & VOBJBUF)
10702a31267eSMatthew Dillon 		union_vm_coherency(ap->a_vp, ap->a_uio, 0);
10712a31267eSMatthew Dillon 
10722a31267eSMatthew Dillon 	error = VOP_READ(uvp, ap->a_uio, ap->a_ioflag, ap->a_cred);
10732a31267eSMatthew Dillon 	union_unlock_other(uvp, p);
1074996c772fSJohn Dyson 
1075996c772fSJohn Dyson 	/*
1076996c772fSJohn Dyson 	 * XXX
1077996c772fSJohn Dyson 	 * perhaps the size of the underlying object has changed under
1078996c772fSJohn Dyson 	 * our feet.  take advantage of the offset information present
1079996c772fSJohn Dyson 	 * in the uio structure.
1080996c772fSJohn Dyson 	 */
1081996c772fSJohn Dyson 	if (error == 0) {
1082996c772fSJohn Dyson 		struct union_node *un = VTOUNION(ap->a_vp);
1083996c772fSJohn Dyson 		off_t cur = ap->a_uio->uio_offset;
1084996c772fSJohn Dyson 
10852a31267eSMatthew Dillon 		if (uvp == un->un_uppervp) {
1086996c772fSJohn Dyson 			if (cur > un->un_uppersz)
1087996c772fSJohn Dyson 				union_newsize(ap->a_vp, cur, VNOVAL);
1088996c772fSJohn Dyson 		} else {
1089996c772fSJohn Dyson 			if (cur > un->un_lowersz)
1090996c772fSJohn Dyson 				union_newsize(ap->a_vp, VNOVAL, cur);
1091996c772fSJohn Dyson 		}
1092996c772fSJohn Dyson 	}
1093df8bae1dSRodney W. Grimes 	return (error);
1094df8bae1dSRodney W. Grimes }
1095df8bae1dSRodney W. Grimes 
1096c9bf0111SKATO Takenori static int
1097df8bae1dSRodney W. Grimes union_write(ap)
1098df8bae1dSRodney W. Grimes 	struct vop_read_args /* {
1099df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
1100df8bae1dSRodney W. Grimes 		struct uio *a_uio;
1101df8bae1dSRodney W. Grimes 		int  a_ioflag;
1102df8bae1dSRodney W. Grimes 		struct ucred *a_cred;
1103df8bae1dSRodney W. Grimes 	} */ *ap;
1104df8bae1dSRodney W. Grimes {
1105996c772fSJohn Dyson 	struct union_node *un = VTOUNION(ap->a_vp);
1106996c772fSJohn Dyson 	struct proc *p = ap->a_uio->uio_procp;
11072a31267eSMatthew Dillon 	struct vnode *uppervp;
11082a31267eSMatthew Dillon 	int error;
1109df8bae1dSRodney W. Grimes 
11102a31267eSMatthew Dillon 	if ((uppervp = union_lock_upper(un, p)) == NULLVP)
1111996c772fSJohn Dyson 		panic("union: missing upper layer in write");
1112996c772fSJohn Dyson 
11132a31267eSMatthew Dillon 	/*
11142a31267eSMatthew Dillon 	 * Since our VM pages are associated with our vnode rather then
11152a31267eSMatthew Dillon 	 * the real vnode, and since we do not run our reads and writes
11162a31267eSMatthew Dillon 	 * through our own VM cache, we have a VM/VFS coherency problem.
11172a31267eSMatthew Dillon 	 * We solve them by invalidating or flushing the associated VM
11182a31267eSMatthew Dillon 	 * pages prior to allowing a normal read or write to occur.
11192a31267eSMatthew Dillon 	 *
11202a31267eSMatthew Dillon 	 * VM-backed writes (UIO_NOCOPY) have to be converted to normal
11212a31267eSMatthew Dillon 	 * writes because we are not cache-coherent.  Normal writes need
11222a31267eSMatthew Dillon 	 * to be made coherent with our VM-backing store, which we do by
11232a31267eSMatthew Dillon 	 * first flushing any dirty VM pages associated with the write
11242a31267eSMatthew Dillon 	 * range, and then destroying any clean VM pages associated with
11252a31267eSMatthew Dillon 	 * the write range.
11262a31267eSMatthew Dillon 	 */
11272a31267eSMatthew Dillon 
11282a31267eSMatthew Dillon 	if (ap->a_uio->uio_segflg == UIO_NOCOPY) {
11292a31267eSMatthew Dillon 		ap->a_uio->uio_segflg = UIO_SYSSPACE;
11302a31267eSMatthew Dillon 	} else if (ap->a_vp->v_flag & VOBJBUF) {
11312a31267eSMatthew Dillon 		union_vm_coherency(ap->a_vp, ap->a_uio, 1);
11322a31267eSMatthew Dillon 	}
11332a31267eSMatthew Dillon 
11342a31267eSMatthew Dillon 	error = VOP_WRITE(uppervp, ap->a_uio, ap->a_ioflag, ap->a_cred);
1135996c772fSJohn Dyson 
1136996c772fSJohn Dyson 	/*
1137996c772fSJohn Dyson 	 * the size of the underlying object may be changed by the
1138996c772fSJohn Dyson 	 * write.
1139996c772fSJohn Dyson 	 */
1140996c772fSJohn Dyson 	if (error == 0) {
1141996c772fSJohn Dyson 		off_t cur = ap->a_uio->uio_offset;
1142996c772fSJohn Dyson 
1143996c772fSJohn Dyson 		if (cur > un->un_uppersz)
1144996c772fSJohn Dyson 			union_newsize(ap->a_vp, cur, VNOVAL);
1145996c772fSJohn Dyson 	}
11462a31267eSMatthew Dillon 	union_unlock_upper(uppervp, p);
1147df8bae1dSRodney W. Grimes 	return (error);
1148df8bae1dSRodney W. Grimes }
1149df8bae1dSRodney W. Grimes 
1150c9bf0111SKATO Takenori static int
1151996c772fSJohn Dyson union_lease(ap)
1152996c772fSJohn Dyson 	struct vop_lease_args /* {
1153996c772fSJohn Dyson 		struct vnode *a_vp;
1154996c772fSJohn Dyson 		struct proc *a_p;
1155996c772fSJohn Dyson 		struct ucred *a_cred;
1156996c772fSJohn Dyson 		int a_flag;
1157996c772fSJohn Dyson 	} */ *ap;
1158996c772fSJohn Dyson {
11592a31267eSMatthew Dillon 	struct vnode *ovp = OTHERVP(ap->a_vp);
1160996c772fSJohn Dyson 
1161996c772fSJohn Dyson 	ap->a_vp = ovp;
1162996c772fSJohn Dyson 	return (VCALL(ovp, VOFFSET(vop_lease), ap));
1163996c772fSJohn Dyson }
1164996c772fSJohn Dyson 
1165c9bf0111SKATO Takenori static int
1166df8bae1dSRodney W. Grimes union_ioctl(ap)
1167df8bae1dSRodney W. Grimes 	struct vop_ioctl_args /* {
1168df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
1169df8bae1dSRodney W. Grimes 		int  a_command;
1170df8bae1dSRodney W. Grimes 		caddr_t  a_data;
1171df8bae1dSRodney W. Grimes 		int  a_fflag;
1172df8bae1dSRodney W. Grimes 		struct ucred *a_cred;
1173df8bae1dSRodney W. Grimes 		struct proc *a_p;
1174df8bae1dSRodney W. Grimes 	} */ *ap;
1175df8bae1dSRodney W. Grimes {
11762a31267eSMatthew Dillon 	struct vnode *ovp = OTHERVP(ap->a_vp);
1177df8bae1dSRodney W. Grimes 
1178996c772fSJohn Dyson 	ap->a_vp = ovp;
1179996c772fSJohn Dyson 	return (VCALL(ovp, VOFFSET(vop_ioctl), ap));
1180df8bae1dSRodney W. Grimes }
1181df8bae1dSRodney W. Grimes 
1182c9bf0111SKATO Takenori static int
1183a6aeade2SPeter Wemm union_poll(ap)
1184a6aeade2SPeter Wemm 	struct vop_poll_args /* {
1185df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
1186a6aeade2SPeter Wemm 		int  a_events;
1187df8bae1dSRodney W. Grimes 		struct ucred *a_cred;
1188df8bae1dSRodney W. Grimes 		struct proc *a_p;
1189df8bae1dSRodney W. Grimes 	} */ *ap;
1190df8bae1dSRodney W. Grimes {
11912a31267eSMatthew Dillon 	struct vnode *ovp = OTHERVP(ap->a_vp);
1192df8bae1dSRodney W. Grimes 
1193996c772fSJohn Dyson 	ap->a_vp = ovp;
1194a6aeade2SPeter Wemm 	return (VCALL(ovp, VOFFSET(vop_poll), ap));
1195996c772fSJohn Dyson }
1196996c772fSJohn Dyson 
1197c9bf0111SKATO Takenori static int
1198996c772fSJohn Dyson union_revoke(ap)
1199996c772fSJohn Dyson 	struct vop_revoke_args /* {
1200996c772fSJohn Dyson 		struct vnode *a_vp;
1201996c772fSJohn Dyson 		int a_flags;
1202996c772fSJohn Dyson 		struct proc *a_p;
1203996c772fSJohn Dyson 	} */ *ap;
1204996c772fSJohn Dyson {
1205996c772fSJohn Dyson 	struct vnode *vp = ap->a_vp;
1206996c772fSJohn Dyson 
1207996c772fSJohn Dyson 	if (UPPERVP(vp))
1208996c772fSJohn Dyson 		VOP_REVOKE(UPPERVP(vp), ap->a_flags);
1209996c772fSJohn Dyson 	if (LOWERVP(vp))
1210996c772fSJohn Dyson 		VOP_REVOKE(LOWERVP(vp), ap->a_flags);
1211996c772fSJohn Dyson 	vgone(vp);
1212996c772fSJohn Dyson 	return (0);
1213df8bae1dSRodney W. Grimes }
1214df8bae1dSRodney W. Grimes 
1215c9bf0111SKATO Takenori static int
1216df8bae1dSRodney W. Grimes union_mmap(ap)
1217df8bae1dSRodney W. Grimes 	struct vop_mmap_args /* {
1218df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
1219df8bae1dSRodney W. Grimes 		int  a_fflags;
1220df8bae1dSRodney W. Grimes 		struct ucred *a_cred;
1221df8bae1dSRodney W. Grimes 		struct proc *a_p;
1222df8bae1dSRodney W. Grimes 	} */ *ap;
1223df8bae1dSRodney W. Grimes {
12242a31267eSMatthew Dillon 	struct vnode *ovp = OTHERVP(ap->a_vp);
1225df8bae1dSRodney W. Grimes 
1226996c772fSJohn Dyson 	ap->a_vp = ovp;
1227996c772fSJohn Dyson 	return (VCALL(ovp, VOFFSET(vop_mmap), ap));
1228df8bae1dSRodney W. Grimes }
1229df8bae1dSRodney W. Grimes 
1230c9bf0111SKATO Takenori static int
1231df8bae1dSRodney W. Grimes union_fsync(ap)
1232df8bae1dSRodney W. Grimes 	struct vop_fsync_args /* {
1233df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
1234df8bae1dSRodney W. Grimes 		struct ucred *a_cred;
1235df8bae1dSRodney W. Grimes 		int  a_waitfor;
1236df8bae1dSRodney W. Grimes 		struct proc *a_p;
1237df8bae1dSRodney W. Grimes 	} */ *ap;
1238df8bae1dSRodney W. Grimes {
1239df8bae1dSRodney W. Grimes 	int error = 0;
1240996c772fSJohn Dyson 	struct proc *p = ap->a_p;
12412a31267eSMatthew Dillon 	struct vnode *targetvp;
12422a31267eSMatthew Dillon 	struct union_node *un = VTOUNION(ap->a_vp);
1243df8bae1dSRodney W. Grimes 
12442a31267eSMatthew Dillon 	if ((targetvp = union_lock_other(un, p)) != NULLVP) {
1245996c772fSJohn Dyson 		error = VOP_FSYNC(targetvp, ap->a_cred, ap->a_waitfor, p);
12462a31267eSMatthew Dillon 		union_unlock_other(targetvp, p);
1247df8bae1dSRodney W. Grimes 	}
1248df8bae1dSRodney W. Grimes 
1249df8bae1dSRodney W. Grimes 	return (error);
1250df8bae1dSRodney W. Grimes }
1251df8bae1dSRodney W. Grimes 
12522a31267eSMatthew Dillon /*
12532a31267eSMatthew Dillon  *	union_remove:
12542a31267eSMatthew Dillon  *
12552a31267eSMatthew Dillon  *	Remove the specified cnp.  The dvp and vp are passed to us locked
12562a31267eSMatthew Dillon  *	and must remain locked on return.
12572a31267eSMatthew Dillon  */
12582a31267eSMatthew Dillon 
1259c9bf0111SKATO Takenori static int
1260df8bae1dSRodney W. Grimes union_remove(ap)
1261df8bae1dSRodney W. Grimes 	struct vop_remove_args /* {
1262df8bae1dSRodney W. Grimes 		struct vnode *a_dvp;
1263df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
1264df8bae1dSRodney W. Grimes 		struct componentname *a_cnp;
1265df8bae1dSRodney W. Grimes 	} */ *ap;
1266df8bae1dSRodney W. Grimes {
1267df8bae1dSRodney W. Grimes 	struct union_node *dun = VTOUNION(ap->a_dvp);
1268df8bae1dSRodney W. Grimes 	struct union_node *un = VTOUNION(ap->a_vp);
1269996c772fSJohn Dyson 	struct componentname *cnp = ap->a_cnp;
1270996c772fSJohn Dyson 	struct proc *p = cnp->cn_proc;
12712a31267eSMatthew Dillon 	struct vnode *uppervp;
12722a31267eSMatthew Dillon 	struct vnode *upperdvp;
12737be2d300SMike Smith 	int error;
1274df8bae1dSRodney W. Grimes 
12752a31267eSMatthew Dillon 	if ((upperdvp = union_lock_upper(dun, p)) == NULLVP)
1276996c772fSJohn Dyson 		panic("union remove: null upper vnode");
1277996c772fSJohn Dyson 
12782a31267eSMatthew Dillon 	if ((uppervp = union_lock_upper(un, p)) != NULLVP) {
12797be2d300SMike Smith 		if (union_dowhiteout(un, cnp->cn_cred, p))
1280996c772fSJohn Dyson 			cnp->cn_flags |= DOWHITEOUT;
12812a31267eSMatthew Dillon 		error = VOP_REMOVE(upperdvp, uppervp, cnp);
128287884aadSKATO Takenori #if 0
128387884aadSKATO Takenori 		/* XXX */
1284df8bae1dSRodney W. Grimes 		if (!error)
1285df8bae1dSRodney W. Grimes 			union_removed_upper(un);
128687884aadSKATO Takenori #endif
12872a31267eSMatthew Dillon 		union_unlock_upper(uppervp, p);
1288df8bae1dSRodney W. Grimes 	} else {
1289996c772fSJohn Dyson 		error = union_mkwhiteout(
12902a31267eSMatthew Dillon 			    MOUNTTOUNIONMOUNT(ap->a_dvp->v_mount),
12912a31267eSMatthew Dillon 			    upperdvp, ap->a_cnp, un->un_path);
1292df8bae1dSRodney W. Grimes 	}
12932a31267eSMatthew Dillon 	union_unlock_upper(upperdvp, p);
1294df8bae1dSRodney W. Grimes 	return (error);
1295df8bae1dSRodney W. Grimes }
1296df8bae1dSRodney W. Grimes 
12972a31267eSMatthew Dillon /*
12982a31267eSMatthew Dillon  *	union_link:
12992a31267eSMatthew Dillon  *
13002a31267eSMatthew Dillon  *	tdvp will be locked on entry, vp will not be locked on entry.
13012a31267eSMatthew Dillon  *	tdvp should remain locked on return and vp should remain unlocked
13022a31267eSMatthew Dillon  *	on return.
13032a31267eSMatthew Dillon  */
13042a31267eSMatthew Dillon 
1305c9bf0111SKATO Takenori static int
1306df8bae1dSRodney W. Grimes union_link(ap)
1307df8bae1dSRodney W. Grimes 	struct vop_link_args /* {
1308df8bae1dSRodney W. Grimes 		struct vnode *a_tdvp;
130947777413SDavid Greenman 		struct vnode *a_vp;
1310df8bae1dSRodney W. Grimes 		struct componentname *a_cnp;
1311df8bae1dSRodney W. Grimes 	} */ *ap;
1312df8bae1dSRodney W. Grimes {
1313996c772fSJohn Dyson 	struct componentname *cnp = ap->a_cnp;
1314996c772fSJohn Dyson 	struct proc *p = cnp->cn_proc;
13157be2d300SMike Smith 	struct union_node *dun = VTOUNION(ap->a_tdvp);
1316996c772fSJohn Dyson 	struct vnode *vp;
1317996c772fSJohn Dyson 	struct vnode *tdvp;
13187be2d300SMike Smith 	int error = 0;
1319df8bae1dSRodney W. Grimes 
1320996c772fSJohn Dyson 	if (ap->a_tdvp->v_op != ap->a_vp->v_op) {
1321996c772fSJohn Dyson 		vp = ap->a_vp;
1322df8bae1dSRodney W. Grimes 	} else {
1323996c772fSJohn Dyson 		struct union_node *tun = VTOUNION(ap->a_vp);
13242a31267eSMatthew Dillon 
1325996c772fSJohn Dyson 		if (tun->un_uppervp == NULLVP) {
1326996c772fSJohn Dyson 			vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY, p);
13272a31267eSMatthew Dillon #if 0
13287be2d300SMike Smith 			if (dun->un_uppervp == tun->un_dirvp) {
13292a31267eSMatthew Dillon 				if (dun->un_flags & UN_ULOCK) {
13307be2d300SMike Smith 					dun->un_flags &= ~UN_ULOCK;
13317be2d300SMike Smith 					VOP_UNLOCK(dun->un_uppervp, 0, p);
1332996c772fSJohn Dyson 				}
13332a31267eSMatthew Dillon 			}
13342a31267eSMatthew Dillon #endif
1335996c772fSJohn Dyson 			error = union_copyup(tun, 1, cnp->cn_cred, p);
13362a31267eSMatthew Dillon #if 0
13377be2d300SMike Smith 			if (dun->un_uppervp == tun->un_dirvp) {
13387be2d300SMike Smith 				vn_lock(dun->un_uppervp,
1339996c772fSJohn Dyson 					    LK_EXCLUSIVE | LK_RETRY, p);
13407be2d300SMike Smith 				dun->un_flags |= UN_ULOCK;
1341996c772fSJohn Dyson 			}
13422a31267eSMatthew Dillon #endif
1343996c772fSJohn Dyson 			VOP_UNLOCK(ap->a_vp, 0, p);
1344996c772fSJohn Dyson 		}
1345996c772fSJohn Dyson 		vp = tun->un_uppervp;
1346df8bae1dSRodney W. Grimes 	}
1347df8bae1dSRodney W. Grimes 
13487be2d300SMike Smith 	if (error)
1349df8bae1dSRodney W. Grimes 		return (error);
1350df8bae1dSRodney W. Grimes 
13512a31267eSMatthew Dillon 	/*
13522a31267eSMatthew Dillon 	 * Make sure upper is locked, then unlock the union directory we were
13532a31267eSMatthew Dillon 	 * called with to avoid a deadlock while we are calling VOP_LINK on
13542a31267eSMatthew Dillon 	 * the upper (with tdvp locked and vp not locked).  Our ap->a_tdvp
13552a31267eSMatthew Dillon 	 * is expected to be locked on return.
13562a31267eSMatthew Dillon 	 */
1357996c772fSJohn Dyson 
13582a31267eSMatthew Dillon 	if ((tdvp = union_lock_upper(dun, p)) == NULLVP)
13592a31267eSMatthew Dillon 		return (EROFS);
13607be2d300SMike Smith 
13612a31267eSMatthew Dillon 	VOP_UNLOCK(ap->a_tdvp, 0, p);		/* unlock calling node */
13622a31267eSMatthew Dillon 	error = VOP_LINK(tdvp, vp, cnp);	/* call link on upper */
13637be2d300SMike Smith 
13642a31267eSMatthew Dillon 	/*
13652a31267eSMatthew Dillon 	 * We have to unlock tdvp prior to relocking our calling node in
13662a31267eSMatthew Dillon 	 * order to avoid a deadlock.
13672a31267eSMatthew Dillon 	 */
13682a31267eSMatthew Dillon 	union_unlock_upper(tdvp, p);
13692a31267eSMatthew Dillon 	vn_lock(ap->a_tdvp, LK_EXCLUSIVE | LK_RETRY, p);
13707be2d300SMike Smith 	return (error);
1371996c772fSJohn Dyson }
1372996c772fSJohn Dyson 
1373c9bf0111SKATO Takenori static int
1374df8bae1dSRodney W. Grimes union_rename(ap)
1375df8bae1dSRodney W. Grimes 	struct vop_rename_args  /* {
1376df8bae1dSRodney W. Grimes 		struct vnode *a_fdvp;
1377df8bae1dSRodney W. Grimes 		struct vnode *a_fvp;
1378df8bae1dSRodney W. Grimes 		struct componentname *a_fcnp;
1379df8bae1dSRodney W. Grimes 		struct vnode *a_tdvp;
1380df8bae1dSRodney W. Grimes 		struct vnode *a_tvp;
1381df8bae1dSRodney W. Grimes 		struct componentname *a_tcnp;
1382df8bae1dSRodney W. Grimes 	} */ *ap;
1383df8bae1dSRodney W. Grimes {
1384df8bae1dSRodney W. Grimes 	int error;
1385df8bae1dSRodney W. Grimes 	struct vnode *fdvp = ap->a_fdvp;
1386df8bae1dSRodney W. Grimes 	struct vnode *fvp = ap->a_fvp;
1387df8bae1dSRodney W. Grimes 	struct vnode *tdvp = ap->a_tdvp;
1388df8bae1dSRodney W. Grimes 	struct vnode *tvp = ap->a_tvp;
1389df8bae1dSRodney W. Grimes 
13902a31267eSMatthew Dillon 	/*
13912a31267eSMatthew Dillon 	 * Figure out what fdvp to pass to our upper or lower vnode.  If we
13922a31267eSMatthew Dillon 	 * replace the fdvp, release the original one and ref the new one.
13932a31267eSMatthew Dillon 	 */
13942a31267eSMatthew Dillon 
1395df8bae1dSRodney W. Grimes 	if (fdvp->v_op == union_vnodeop_p) {	/* always true */
1396df8bae1dSRodney W. Grimes 		struct union_node *un = VTOUNION(fdvp);
1397df8bae1dSRodney W. Grimes 		if (un->un_uppervp == NULLVP) {
1398996c772fSJohn Dyson 			/*
1399996c772fSJohn Dyson 			 * this should never happen in normal
1400996c772fSJohn Dyson 			 * operation but might if there was
1401996c772fSJohn Dyson 			 * a problem creating the top-level shadow
1402996c772fSJohn Dyson 			 * directory.
1403996c772fSJohn Dyson 			 */
1404996c772fSJohn Dyson 			error = EXDEV;
1405df8bae1dSRodney W. Grimes 			goto bad;
1406df8bae1dSRodney W. Grimes 		}
1407df8bae1dSRodney W. Grimes 		fdvp = un->un_uppervp;
1408df8bae1dSRodney W. Grimes 		VREF(fdvp);
1409df8bae1dSRodney W. Grimes 		vrele(ap->a_fdvp);
1410df8bae1dSRodney W. Grimes 	}
1411df8bae1dSRodney W. Grimes 
14122a31267eSMatthew Dillon 	/*
14132a31267eSMatthew Dillon 	 * Figure out what fvp to pass to our upper or lower vnode.  If we
14142a31267eSMatthew Dillon 	 * replace the fvp, release the original one and ref the new one.
14152a31267eSMatthew Dillon 	 */
14162a31267eSMatthew Dillon 
1417df8bae1dSRodney W. Grimes 	if (fvp->v_op == union_vnodeop_p) {	/* always true */
1418df8bae1dSRodney W. Grimes 		struct union_node *un = VTOUNION(fvp);
14192a31267eSMatthew Dillon #if 0
14202a31267eSMatthew Dillon 		struct union_mount *um = MOUNTTOUNIONMOUNT(fvp->v_mount);
14212a31267eSMatthew Dillon #endif
14222a31267eSMatthew Dillon 
1423df8bae1dSRodney W. Grimes 		if (un->un_uppervp == NULLVP) {
14242a31267eSMatthew Dillon 			switch(fvp->v_type) {
14252a31267eSMatthew Dillon 			case VREG:
14262a31267eSMatthew Dillon 				vn_lock(un->un_vnode, LK_EXCLUSIVE | LK_RETRY, ap->a_fcnp->cn_proc);
14272a31267eSMatthew Dillon 				error = union_copyup(un, 1, ap->a_fcnp->cn_cred, ap->a_fcnp->cn_proc);
14282a31267eSMatthew Dillon 				VOP_UNLOCK(un->un_vnode, 0, ap->a_fcnp->cn_proc);
14292a31267eSMatthew Dillon 				if (error)
14302a31267eSMatthew Dillon 					goto bad;
14312a31267eSMatthew Dillon 				break;
14322a31267eSMatthew Dillon 			case VDIR:
14332a31267eSMatthew Dillon 				/*
14342a31267eSMatthew Dillon 				 * XXX not yet.
14352a31267eSMatthew Dillon 				 *
14362a31267eSMatthew Dillon 				 * There is only one way to rename a directory
14372a31267eSMatthew Dillon 				 * based in the lowervp, and that is to copy
14382a31267eSMatthew Dillon 				 * the entire directory hierarchy.  Otherwise
14392a31267eSMatthew Dillon 				 * it would not last across a reboot.
14402a31267eSMatthew Dillon 				 */
14412a31267eSMatthew Dillon #if 0
14422a31267eSMatthew Dillon 				vrele(fvp);
14432a31267eSMatthew Dillon 				fvp = NULL;
14442a31267eSMatthew Dillon 				vn_lock(fdvp, LK_EXCLUSIVE | LK_RETRY, ap->a_fcnp->cn_proc);
14452a31267eSMatthew Dillon 				error = union_mkshadow(um, fdvp,
14462a31267eSMatthew Dillon 					    ap->a_fcnp, &un->un_uppervp);
14472a31267eSMatthew Dillon 				VOP_UNLOCK(fdvp, 0, ap->a_fcnp->cn_proc);
14482a31267eSMatthew Dillon 				if (un->un_uppervp)
14492a31267eSMatthew Dillon 					VOP_UNLOCK(un->un_uppervp, 0, ap->a_fcnp->cn_proc);
14502a31267eSMatthew Dillon 				if (error)
14512a31267eSMatthew Dillon 					goto bad;
14522a31267eSMatthew Dillon 				break;
14532a31267eSMatthew Dillon #endif
14542a31267eSMatthew Dillon 			default:
1455996c772fSJohn Dyson 				error = EXDEV;
1456df8bae1dSRodney W. Grimes 				goto bad;
1457df8bae1dSRodney W. Grimes 			}
14582a31267eSMatthew Dillon 		}
1459df8bae1dSRodney W. Grimes 
1460996c772fSJohn Dyson 		if (un->un_lowervp != NULLVP)
1461996c772fSJohn Dyson 			ap->a_fcnp->cn_flags |= DOWHITEOUT;
1462df8bae1dSRodney W. Grimes 		fvp = un->un_uppervp;
1463df8bae1dSRodney W. Grimes 		VREF(fvp);
1464df8bae1dSRodney W. Grimes 		vrele(ap->a_fvp);
1465df8bae1dSRodney W. Grimes 	}
1466df8bae1dSRodney W. Grimes 
14672a31267eSMatthew Dillon 	/*
14682a31267eSMatthew Dillon 	 * Figure out what tdvp (destination directory) to pass to the
14692a31267eSMatthew Dillon 	 * lower level.  If we replace it with uppervp, we need to vput the
14702a31267eSMatthew Dillon 	 * old one.  The exclusive lock is transfered to what we will pass
14712a31267eSMatthew Dillon 	 * down in the VOP_RENAME and we replace uppervp with a simple
14722a31267eSMatthew Dillon 	 * reference.
14732a31267eSMatthew Dillon 	 */
14742a31267eSMatthew Dillon 
1475df8bae1dSRodney W. Grimes 	if (tdvp->v_op == union_vnodeop_p) {
1476df8bae1dSRodney W. Grimes 		struct union_node *un = VTOUNION(tdvp);
14772a31267eSMatthew Dillon 
1478df8bae1dSRodney W. Grimes 		if (un->un_uppervp == NULLVP) {
1479996c772fSJohn Dyson 			/*
1480996c772fSJohn Dyson 			 * this should never happen in normal
1481996c772fSJohn Dyson 			 * operation but might if there was
1482996c772fSJohn Dyson 			 * a problem creating the top-level shadow
1483996c772fSJohn Dyson 			 * directory.
1484996c772fSJohn Dyson 			 */
1485996c772fSJohn Dyson 			error = EXDEV;
1486df8bae1dSRodney W. Grimes 			goto bad;
1487df8bae1dSRodney W. Grimes 		}
1488df8bae1dSRodney W. Grimes 
14892a31267eSMatthew Dillon 		/*
14902a31267eSMatthew Dillon 		 * new tdvp is a lock and reference on uppervp, put away
14912a31267eSMatthew Dillon 		 * the old tdvp.
14922a31267eSMatthew Dillon 		 */
14932a31267eSMatthew Dillon 		tdvp = union_lock_upper(un, ap->a_tcnp->cn_proc);
1494df8bae1dSRodney W. Grimes 		vput(ap->a_tdvp);
1495df8bae1dSRodney W. Grimes 	}
1496df8bae1dSRodney W. Grimes 
14972a31267eSMatthew Dillon 	/*
14982a31267eSMatthew Dillon 	 * Figure out what tvp (destination file) to pass to the
14992a31267eSMatthew Dillon 	 * lower level.
15002a31267eSMatthew Dillon 	 *
15012a31267eSMatthew Dillon 	 * If the uppervp file does not exist put away the (wrong)
15022a31267eSMatthew Dillon 	 * file and change tvp to NULL.
15032a31267eSMatthew Dillon 	 */
15042a31267eSMatthew Dillon 
1505996c772fSJohn Dyson 	if (tvp != NULLVP && tvp->v_op == union_vnodeop_p) {
1506df8bae1dSRodney W. Grimes 		struct union_node *un = VTOUNION(tvp);
1507df8bae1dSRodney W. Grimes 
15082a31267eSMatthew Dillon 		tvp = union_lock_upper(un, ap->a_tcnp->cn_proc);
1509df8bae1dSRodney W. Grimes 		vput(ap->a_tvp);
15102a31267eSMatthew Dillon 		/* note: tvp may be NULL */
1511df8bae1dSRodney W. Grimes 	}
1512df8bae1dSRodney W. Grimes 
15132a31267eSMatthew Dillon 	/*
15142a31267eSMatthew Dillon 	 * VOP_RENAME releases/vputs prior to returning, so we have no
15152a31267eSMatthew Dillon 	 * cleanup to do.
15162a31267eSMatthew Dillon 	 */
15172a31267eSMatthew Dillon 
1518df8bae1dSRodney W. Grimes 	return (VOP_RENAME(fdvp, fvp, ap->a_fcnp, tdvp, tvp, ap->a_tcnp));
1519df8bae1dSRodney W. Grimes 
15202a31267eSMatthew Dillon 	/*
15212a31267eSMatthew Dillon 	 * Error.  We still have to release / vput the various elements.
15222a31267eSMatthew Dillon 	 */
15232a31267eSMatthew Dillon 
1524df8bae1dSRodney W. Grimes bad:
1525df8bae1dSRodney W. Grimes 	vrele(fdvp);
15262a31267eSMatthew Dillon 	if (fvp)
1527df8bae1dSRodney W. Grimes 		vrele(fvp);
1528df8bae1dSRodney W. Grimes 	vput(tdvp);
15292a31267eSMatthew Dillon 	if (tvp != NULLVP) {
15302a31267eSMatthew Dillon 		if (tvp != tdvp)
1531df8bae1dSRodney W. Grimes 			vput(tvp);
15322a31267eSMatthew Dillon 		else
15332a31267eSMatthew Dillon 			vrele(tvp);
15342a31267eSMatthew Dillon 	}
1535df8bae1dSRodney W. Grimes 	return (error);
1536df8bae1dSRodney W. Grimes }
1537df8bae1dSRodney W. Grimes 
1538c9bf0111SKATO Takenori static int
1539df8bae1dSRodney W. Grimes union_mkdir(ap)
1540df8bae1dSRodney W. Grimes 	struct vop_mkdir_args /* {
1541df8bae1dSRodney W. Grimes 		struct vnode *a_dvp;
1542df8bae1dSRodney W. Grimes 		struct vnode **a_vpp;
1543df8bae1dSRodney W. Grimes 		struct componentname *a_cnp;
1544df8bae1dSRodney W. Grimes 		struct vattr *a_vap;
1545df8bae1dSRodney W. Grimes 	} */ *ap;
1546df8bae1dSRodney W. Grimes {
15477be2d300SMike Smith 	struct union_node *dun = VTOUNION(ap->a_dvp);
1548996c772fSJohn Dyson 	struct componentname *cnp = ap->a_cnp;
1549996c772fSJohn Dyson 	struct proc *p = cnp->cn_proc;
15502a31267eSMatthew Dillon 	struct vnode *upperdvp;
15512a31267eSMatthew Dillon 	int error = EROFS;
1552df8bae1dSRodney W. Grimes 
15532a31267eSMatthew Dillon 	if ((upperdvp = union_lock_upper(dun, p)) != NULLVP) {
1554df8bae1dSRodney W. Grimes 		struct vnode *vp;
1555df8bae1dSRodney W. Grimes 
15562a31267eSMatthew Dillon 		error = VOP_MKDIR(upperdvp, &vp, cnp, ap->a_vap);
15572a31267eSMatthew Dillon 		union_unlock_upper(upperdvp, p);
15582a31267eSMatthew Dillon 
15592a31267eSMatthew Dillon 		if (error == 0) {
15602a31267eSMatthew Dillon 			VOP_UNLOCK(vp, 0, p);
15612a31267eSMatthew Dillon 			UDEBUG(("ALLOCVP-2 FROM %p REFS %d\n", vp, vp->v_usecount));
15622a31267eSMatthew Dillon 			error = union_allocvp(ap->a_vpp, ap->a_dvp->v_mount,
15632a31267eSMatthew Dillon 				ap->a_dvp, NULLVP, cnp, vp, NULLVP, 1);
15642a31267eSMatthew Dillon 			UDEBUG(("ALLOCVP-2B FROM %p REFS %d\n", *ap->a_vpp, vp->v_usecount));
1565996c772fSJohn Dyson 		}
1566df8bae1dSRodney W. Grimes 	}
15672a31267eSMatthew Dillon 	return (error);
1568df8bae1dSRodney W. Grimes }
1569df8bae1dSRodney W. Grimes 
1570c9bf0111SKATO Takenori static int
1571df8bae1dSRodney W. Grimes union_rmdir(ap)
1572df8bae1dSRodney W. Grimes 	struct vop_rmdir_args /* {
1573df8bae1dSRodney W. Grimes 		struct vnode *a_dvp;
1574df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
1575df8bae1dSRodney W. Grimes 		struct componentname *a_cnp;
1576df8bae1dSRodney W. Grimes 	} */ *ap;
1577df8bae1dSRodney W. Grimes {
1578df8bae1dSRodney W. Grimes 	struct union_node *dun = VTOUNION(ap->a_dvp);
1579df8bae1dSRodney W. Grimes 	struct union_node *un = VTOUNION(ap->a_vp);
1580996c772fSJohn Dyson 	struct componentname *cnp = ap->a_cnp;
1581996c772fSJohn Dyson 	struct proc *p = cnp->cn_proc;
15822a31267eSMatthew Dillon 	struct vnode *upperdvp;
15832a31267eSMatthew Dillon 	struct vnode *uppervp;
15847be2d300SMike Smith 	int error;
1585df8bae1dSRodney W. Grimes 
15862a31267eSMatthew Dillon 	if ((upperdvp = union_lock_upper(dun, p)) == NULLVP)
1587996c772fSJohn Dyson 		panic("union rmdir: null upper vnode");
1588996c772fSJohn Dyson 
15892a31267eSMatthew Dillon 	if ((uppervp = union_lock_upper(un, p)) != NULLVP) {
15907be2d300SMike Smith 		if (union_dowhiteout(un, cnp->cn_cred, p))
1591996c772fSJohn Dyson 			cnp->cn_flags |= DOWHITEOUT;
15922a31267eSMatthew Dillon 		error = VOP_RMDIR(upperdvp, uppervp, ap->a_cnp);
15932a31267eSMatthew Dillon 		union_unlock_upper(uppervp, p);
1594df8bae1dSRodney W. Grimes 	} else {
1595996c772fSJohn Dyson 		error = union_mkwhiteout(
15962a31267eSMatthew Dillon 			    MOUNTTOUNIONMOUNT(ap->a_dvp->v_mount),
1597996c772fSJohn Dyson 			    dun->un_uppervp, ap->a_cnp, un->un_path);
1598df8bae1dSRodney W. Grimes 	}
15992a31267eSMatthew Dillon 	union_unlock_upper(upperdvp, p);
1600df8bae1dSRodney W. Grimes 	return (error);
1601df8bae1dSRodney W. Grimes }
1602df8bae1dSRodney W. Grimes 
16032a31267eSMatthew Dillon /*
16042a31267eSMatthew Dillon  *	union_symlink:
16052a31267eSMatthew Dillon  *
16062a31267eSMatthew Dillon  *	dvp is locked on entry and remains locked on return.  a_vpp is garbage
16072a31267eSMatthew Dillon  *	(unused).
16082a31267eSMatthew Dillon  */
16092a31267eSMatthew Dillon 
1610c9bf0111SKATO Takenori static int
1611df8bae1dSRodney W. Grimes union_symlink(ap)
1612df8bae1dSRodney W. Grimes 	struct vop_symlink_args /* {
1613df8bae1dSRodney W. Grimes 		struct vnode *a_dvp;
1614df8bae1dSRodney W. Grimes 		struct vnode **a_vpp;
1615df8bae1dSRodney W. Grimes 		struct componentname *a_cnp;
1616df8bae1dSRodney W. Grimes 		struct vattr *a_vap;
1617df8bae1dSRodney W. Grimes 		char *a_target;
1618df8bae1dSRodney W. Grimes 	} */ *ap;
1619df8bae1dSRodney W. Grimes {
16207be2d300SMike Smith 	struct union_node *dun = VTOUNION(ap->a_dvp);
1621996c772fSJohn Dyson 	struct componentname *cnp = ap->a_cnp;
1622996c772fSJohn Dyson 	struct proc *p = cnp->cn_proc;
16232a31267eSMatthew Dillon 	struct vnode *dvp;
16242a31267eSMatthew Dillon 	int error = EROFS;
1625df8bae1dSRodney W. Grimes 
16262a31267eSMatthew Dillon 	if ((dvp = union_lock_upper(dun, p)) != NULLVP) {
1627df8bae1dSRodney W. Grimes 		struct vnode *vp;
1628df8bae1dSRodney W. Grimes 
1629996c772fSJohn Dyson 		error = VOP_SYMLINK(dvp, &vp, cnp, ap->a_vap, ap->a_target);
16302a31267eSMatthew Dillon 		/* vp is garbage whether an error occurs or not */
1631df8bae1dSRodney W. Grimes 		*ap->a_vpp = NULLVP;
16322a31267eSMatthew Dillon 		union_unlock_upper(dvp, p);
1633df8bae1dSRodney W. Grimes 	}
16342a31267eSMatthew Dillon 	return (error);
1635df8bae1dSRodney W. Grimes }
1636df8bae1dSRodney W. Grimes 
1637df8bae1dSRodney W. Grimes /*
1638df8bae1dSRodney W. Grimes  * union_readdir works in concert with getdirentries and
1639df8bae1dSRodney W. Grimes  * readdir(3) to provide a list of entries in the unioned
1640df8bae1dSRodney W. Grimes  * directories.  getdirentries is responsible for walking
1641df8bae1dSRodney W. Grimes  * down the union stack.  readdir(3) is responsible for
1642df8bae1dSRodney W. Grimes  * eliminating duplicate names from the returned data stream.
1643df8bae1dSRodney W. Grimes  */
1644c9bf0111SKATO Takenori static int
1645df8bae1dSRodney W. Grimes union_readdir(ap)
1646df8bae1dSRodney W. Grimes 	struct vop_readdir_args /* {
1647df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
1648df8bae1dSRodney W. Grimes 		struct uio *a_uio;
1649df8bae1dSRodney W. Grimes 		struct ucred *a_cred;
1650996c772fSJohn Dyson 		int *a_eofflag;
1651996c772fSJohn Dyson 		u_long *a_cookies;
1652996c772fSJohn Dyson 		int a_ncookies;
1653df8bae1dSRodney W. Grimes 	} */ *ap;
1654df8bae1dSRodney W. Grimes {
1655df8bae1dSRodney W. Grimes 	struct union_node *un = VTOUNION(ap->a_vp);
1656996c772fSJohn Dyson 	struct proc *p = ap->a_uio->uio_procp;
16572a31267eSMatthew Dillon 	struct vnode *uvp;
16582a31267eSMatthew Dillon 	int error = 0;
1659df8bae1dSRodney W. Grimes 
16602a31267eSMatthew Dillon 	if ((uvp = union_lock_upper(un, p)) != NULLVP) {
1661996c772fSJohn Dyson 		ap->a_vp = uvp;
16622a31267eSMatthew Dillon 		error = VCALL(uvp, VOFFSET(vop_readdir), ap);
16632a31267eSMatthew Dillon 		union_unlock_upper(uvp, p);
16642a31267eSMatthew Dillon 	}
16652a31267eSMatthew Dillon 	return(error);
1666df8bae1dSRodney W. Grimes }
1667df8bae1dSRodney W. Grimes 
1668c9bf0111SKATO Takenori static int
1669df8bae1dSRodney W. Grimes union_readlink(ap)
1670df8bae1dSRodney W. Grimes 	struct vop_readlink_args /* {
1671df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
1672df8bae1dSRodney W. Grimes 		struct uio *a_uio;
1673df8bae1dSRodney W. Grimes 		struct ucred *a_cred;
1674df8bae1dSRodney W. Grimes 	} */ *ap;
1675df8bae1dSRodney W. Grimes {
1676df8bae1dSRodney W. Grimes 	int error;
16772a31267eSMatthew Dillon 	struct union_node *un = VTOUNION(ap->a_vp);
1678996c772fSJohn Dyson 	struct uio *uio = ap->a_uio;
1679996c772fSJohn Dyson 	struct proc *p = uio->uio_procp;
16802a31267eSMatthew Dillon 	struct vnode *vp;
1681df8bae1dSRodney W. Grimes 
16822a31267eSMatthew Dillon 	vp = union_lock_other(un, p);
16832a31267eSMatthew Dillon 	KASSERT(vp != NULL, ("union_readlink: backing vnode missing!"));
16842a31267eSMatthew Dillon 
1685996c772fSJohn Dyson 	ap->a_vp = vp;
1686996c772fSJohn Dyson 	error = VCALL(vp, VOFFSET(vop_readlink), ap);
16872a31267eSMatthew Dillon 	union_unlock_other(vp, p);
1688df8bae1dSRodney W. Grimes 
1689df8bae1dSRodney W. Grimes 	return (error);
1690df8bae1dSRodney W. Grimes }
1691df8bae1dSRodney W. Grimes 
16922a31267eSMatthew Dillon /*
16932a31267eSMatthew Dillon  *	union_abortop:
16942a31267eSMatthew Dillon  *
16952a31267eSMatthew Dillon  *	dvp is locked on entry and left locked on return
16962a31267eSMatthew Dillon  *
16972a31267eSMatthew Dillon  */
16982a31267eSMatthew Dillon 
1699c9bf0111SKATO Takenori static int
1700df8bae1dSRodney W. Grimes union_abortop(ap)
1701df8bae1dSRodney W. Grimes 	struct vop_abortop_args /* {
1702df8bae1dSRodney W. Grimes 		struct vnode *a_dvp;
1703df8bae1dSRodney W. Grimes 		struct componentname *a_cnp;
1704df8bae1dSRodney W. Grimes 	} */ *ap;
1705df8bae1dSRodney W. Grimes {
1706996c772fSJohn Dyson 	struct componentname *cnp = ap->a_cnp;
1707996c772fSJohn Dyson 	struct proc *p = cnp->cn_proc;
1708df8bae1dSRodney W. Grimes 	struct union_node *un = VTOUNION(ap->a_dvp);
17092a31267eSMatthew Dillon 	int islocked = VOP_ISLOCKED(ap->a_dvp);
17102a31267eSMatthew Dillon 	struct vnode *vp;
17112a31267eSMatthew Dillon 	int error;
1712df8bae1dSRodney W. Grimes 
1713df8bae1dSRodney W. Grimes 	if (islocked) {
17142a31267eSMatthew Dillon 		vp = union_lock_other(un, p);
17152a31267eSMatthew Dillon 	} else {
17162a31267eSMatthew Dillon 		vp = OTHERVP(ap->a_dvp);
1717df8bae1dSRodney W. Grimes 	}
17182a31267eSMatthew Dillon 	KASSERT(vp != NULL, ("union_abortop: backing vnode missing!"));
17192a31267eSMatthew Dillon 
1720996c772fSJohn Dyson 	ap->a_dvp = vp;
1721996c772fSJohn Dyson 	error = VCALL(vp, VOFFSET(vop_abortop), ap);
17222a31267eSMatthew Dillon 
17232a31267eSMatthew Dillon 	if (islocked)
17242a31267eSMatthew Dillon 		union_unlock_other(vp, p);
1725df8bae1dSRodney W. Grimes 
1726df8bae1dSRodney W. Grimes 	return (error);
1727df8bae1dSRodney W. Grimes }
1728df8bae1dSRodney W. Grimes 
17292a31267eSMatthew Dillon /*
17302a31267eSMatthew Dillon  *	union_inactive:
17312a31267eSMatthew Dillon  *
17322a31267eSMatthew Dillon  *	Called with the vnode locked.  We are expected to unlock the vnode.
17332a31267eSMatthew Dillon  */
17342a31267eSMatthew Dillon 
1735c9bf0111SKATO Takenori static int
1736df8bae1dSRodney W. Grimes union_inactive(ap)
1737df8bae1dSRodney W. Grimes 	struct vop_inactive_args /* {
1738df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
1739996c772fSJohn Dyson 		struct proc *a_p;
1740df8bae1dSRodney W. Grimes 	} */ *ap;
1741df8bae1dSRodney W. Grimes {
1742996c772fSJohn Dyson 	struct vnode *vp = ap->a_vp;
1743996c772fSJohn Dyson 	struct proc *p = ap->a_p;
1744996c772fSJohn Dyson 	struct union_node *un = VTOUNION(vp);
1745996c772fSJohn Dyson 	struct vnode **vpp;
1746df8bae1dSRodney W. Grimes 
1747df8bae1dSRodney W. Grimes 	/*
1748df8bae1dSRodney W. Grimes 	 * Do nothing (and _don't_ bypass).
1749df8bae1dSRodney W. Grimes 	 * Wait to vrele lowervp until reclaim,
1750df8bae1dSRodney W. Grimes 	 * so that until then our union_node is in the
1751df8bae1dSRodney W. Grimes 	 * cache and reusable.
1752df8bae1dSRodney W. Grimes 	 *
1753df8bae1dSRodney W. Grimes 	 * NEEDSWORK: Someday, consider inactive'ing
1754df8bae1dSRodney W. Grimes 	 * the lowervp and then trying to reactivate it
1755df8bae1dSRodney W. Grimes 	 * with capabilities (v_id)
1756df8bae1dSRodney W. Grimes 	 * like they do in the name lookup cache code.
1757df8bae1dSRodney W. Grimes 	 * That's too much work for now.
1758df8bae1dSRodney W. Grimes 	 */
1759df8bae1dSRodney W. Grimes 
1760996c772fSJohn Dyson 	if (un->un_dircache != 0) {
1761996c772fSJohn Dyson 		for (vpp = un->un_dircache; *vpp != NULLVP; vpp++)
1762996c772fSJohn Dyson 			vrele(*vpp);
1763996c772fSJohn Dyson 		free (un->un_dircache, M_TEMP);
1764996c772fSJohn Dyson 		un->un_dircache = 0;
1765996c772fSJohn Dyson 	}
1766df8bae1dSRodney W. Grimes 
17672a31267eSMatthew Dillon #if 0
17682a31267eSMatthew Dillon 	if ((un->un_flags & UN_ULOCK) && un->un_uppervp) {
17692a31267eSMatthew Dillon 		un->un_flags &= ~UN_ULOCK;
17702a31267eSMatthew Dillon 		VOP_UNLOCK(un->un_uppervp, 0, p);
17712a31267eSMatthew Dillon 	}
17722a31267eSMatthew Dillon #endif
17732a31267eSMatthew Dillon 
1774996c772fSJohn Dyson 	VOP_UNLOCK(vp, 0, p);
1775996c772fSJohn Dyson 
1776996c772fSJohn Dyson 	if ((un->un_flags & UN_CACHED) == 0)
1777996c772fSJohn Dyson 		vgone(vp);
1778df8bae1dSRodney W. Grimes 
1779df8bae1dSRodney W. Grimes 	return (0);
1780df8bae1dSRodney W. Grimes }
1781df8bae1dSRodney W. Grimes 
1782c9bf0111SKATO Takenori static int
1783df8bae1dSRodney W. Grimes union_reclaim(ap)
1784df8bae1dSRodney W. Grimes 	struct vop_reclaim_args /* {
1785df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
1786df8bae1dSRodney W. Grimes 	} */ *ap;
1787df8bae1dSRodney W. Grimes {
1788df8bae1dSRodney W. Grimes 	union_freevp(ap->a_vp);
1789df8bae1dSRodney W. Grimes 
1790df8bae1dSRodney W. Grimes 	return (0);
1791df8bae1dSRodney W. Grimes }
1792df8bae1dSRodney W. Grimes 
1793c9bf0111SKATO Takenori static int
1794df8bae1dSRodney W. Grimes union_lock(ap)
1795df8bae1dSRodney W. Grimes 	struct vop_lock_args *ap;
1796df8bae1dSRodney W. Grimes {
17972a31267eSMatthew Dillon #if 0
1798df8bae1dSRodney W. Grimes 	struct vnode *vp = ap->a_vp;
1799996c772fSJohn Dyson 	struct proc *p = ap->a_p;
1800996c772fSJohn Dyson 	int flags = ap->a_flags;
1801df8bae1dSRodney W. Grimes 	struct union_node *un;
18022a31267eSMatthew Dillon #endif
1803996c772fSJohn Dyson 	int error;
1804996c772fSJohn Dyson 
18052a31267eSMatthew Dillon 	error = vop_stdlock(ap);
18062a31267eSMatthew Dillon #if 0
1807df8bae1dSRodney W. Grimes 	un = VTOUNION(vp);
1808df8bae1dSRodney W. Grimes 
18092a31267eSMatthew Dillon 	if (error == 0) {
18102a31267eSMatthew Dillon 		/*
18112a31267eSMatthew Dillon 		 * Lock the upper if it exists and this is an exclusive lock
18122a31267eSMatthew Dillon 		 * request.
18132a31267eSMatthew Dillon 		 */
18142a31267eSMatthew Dillon 		if (un->un_uppervp != NULLVP &&
18152a31267eSMatthew Dillon 		    (flags & LK_TYPE_MASK) == LK_EXCLUSIVE) {
18162a31267eSMatthew Dillon 			if ((un->un_flags & UN_ULOCK) == 0 && vp->v_usecount) {
1817996c772fSJohn Dyson 				error = vn_lock(un->un_uppervp, flags, p);
18182a31267eSMatthew Dillon 				if (error) {
18192a31267eSMatthew Dillon 					struct vop_unlock_args uap = { 0 };
18202a31267eSMatthew Dillon 					uap.a_vp = ap->a_vp;
18212a31267eSMatthew Dillon 					uap.a_flags = ap->a_flags;
18222a31267eSMatthew Dillon 					uap.a_p = ap->a_p;
18232a31267eSMatthew Dillon 					vop_stdunlock(&uap);
1824996c772fSJohn Dyson 					return (error);
18252a31267eSMatthew Dillon 				}
1826df8bae1dSRodney W. Grimes 				un->un_flags |= UN_ULOCK;
1827df8bae1dSRodney W. Grimes 			}
18282a31267eSMatthew Dillon 		}
1829d6476adaSKATO Takenori 	}
1830d6476adaSKATO Takenori #endif
18312a31267eSMatthew Dillon 	return (error);
1832df8bae1dSRodney W. Grimes }
1833df8bae1dSRodney W. Grimes 
1834996c772fSJohn Dyson /*
18352a31267eSMatthew Dillon  *	union_unlock:
1836996c772fSJohn Dyson  *
18372a31267eSMatthew Dillon  *	Unlock our union node.  This also unlocks uppervp.
1838996c772fSJohn Dyson  */
1839c9bf0111SKATO Takenori static int
1840df8bae1dSRodney W. Grimes union_unlock(ap)
1841996c772fSJohn Dyson 	struct vop_unlock_args /* {
1842996c772fSJohn Dyson 		struct vnode *a_vp;
1843996c772fSJohn Dyson 		int a_flags;
1844996c772fSJohn Dyson 		struct proc *a_p;
1845996c772fSJohn Dyson 	} */ *ap;
1846df8bae1dSRodney W. Grimes {
1847df8bae1dSRodney W. Grimes 	struct union_node *un = VTOUNION(ap->a_vp);
18482a31267eSMatthew Dillon 	int error;
1849df8bae1dSRodney W. Grimes 
18502a31267eSMatthew Dillon 	KASSERT((un->un_uppervp == NULL || un->un_uppervp->v_usecount > 0), ("uppervp usecount is 0"));
18512a31267eSMatthew Dillon 
18522a31267eSMatthew Dillon 	error = vop_stdunlock(ap);
18532a31267eSMatthew Dillon #if 0
18542a31267eSMatthew Dillon 
18552a31267eSMatthew Dillon 	/*
18562a31267eSMatthew Dillon 	 * If no exclusive locks remain and we are holding an uppervp lock,
18572a31267eSMatthew Dillon 	 * remove the uppervp lock.
18582a31267eSMatthew Dillon 	 */
18592a31267eSMatthew Dillon 
18602a31267eSMatthew Dillon 	if ((un->un_flags & UN_ULOCK) &&
18612a31267eSMatthew Dillon 	    lockstatus(&un->un_lock) != LK_EXCLUSIVE) {
18622a31267eSMatthew Dillon 		un->un_flags &= ~UN_ULOCK;
18632a31267eSMatthew Dillon 		VOP_UNLOCK(un->un_uppervp, LK_EXCLUSIVE, p);
18642a31267eSMatthew Dillon 	}
1865df8bae1dSRodney W. Grimes #endif
18662a31267eSMatthew Dillon 	return(error);
1867df8bae1dSRodney W. Grimes }
1868df8bae1dSRodney W. Grimes 
18692a31267eSMatthew Dillon /*
18702a31267eSMatthew Dillon  *	union_bmap:
18712a31267eSMatthew Dillon  *
18722a31267eSMatthew Dillon  *	There isn't much we can do.  We cannot push through to the real vnode
18732a31267eSMatthew Dillon  *	to get to the underlying device because this will bypass data
18742a31267eSMatthew Dillon  *	cached by the real vnode.
18752a31267eSMatthew Dillon  *
18762a31267eSMatthew Dillon  *	For some reason we cannot return the 'real' vnode either, it seems
18772a31267eSMatthew Dillon  *	to blow up memory maps.
18782a31267eSMatthew Dillon  */
1879df8bae1dSRodney W. Grimes 
1880c9bf0111SKATO Takenori static int
1881df8bae1dSRodney W. Grimes union_bmap(ap)
1882df8bae1dSRodney W. Grimes 	struct vop_bmap_args /* {
1883df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
1884df8bae1dSRodney W. Grimes 		daddr_t  a_bn;
1885df8bae1dSRodney W. Grimes 		struct vnode **a_vpp;
1886df8bae1dSRodney W. Grimes 		daddr_t *a_bnp;
1887df8bae1dSRodney W. Grimes 		int *a_runp;
1888c83ebe77SJohn Dyson 		int *a_runb;
1889df8bae1dSRodney W. Grimes 	} */ *ap;
1890df8bae1dSRodney W. Grimes {
18912a31267eSMatthew Dillon 	return(EOPNOTSUPP);
1892df8bae1dSRodney W. Grimes }
1893df8bae1dSRodney W. Grimes 
1894c9bf0111SKATO Takenori static int
1895df8bae1dSRodney W. Grimes union_print(ap)
1896df8bae1dSRodney W. Grimes 	struct vop_print_args /* {
1897df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
1898df8bae1dSRodney W. Grimes 	} */ *ap;
1899df8bae1dSRodney W. Grimes {
1900df8bae1dSRodney W. Grimes 	struct vnode *vp = ap->a_vp;
1901df8bae1dSRodney W. Grimes 
19023a773ad0SPoul-Henning Kamp 	printf("\ttag VT_UNION, vp=%p, uppervp=%p, lowervp=%p\n",
1903df8bae1dSRodney W. Grimes 			vp, UPPERVP(vp), LOWERVP(vp));
1904996c772fSJohn Dyson 	if (UPPERVP(vp) != NULLVP)
1905996c772fSJohn Dyson 		vprint("union: upper", UPPERVP(vp));
1906996c772fSJohn Dyson 	if (LOWERVP(vp) != NULLVP)
1907996c772fSJohn Dyson 		vprint("union: lower", LOWERVP(vp));
1908996c772fSJohn Dyson 
1909df8bae1dSRodney W. Grimes 	return (0);
1910df8bae1dSRodney W. Grimes }
1911df8bae1dSRodney W. Grimes 
1912c9bf0111SKATO Takenori static int
1913df8bae1dSRodney W. Grimes union_pathconf(ap)
1914df8bae1dSRodney W. Grimes 	struct vop_pathconf_args /* {
1915df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
1916df8bae1dSRodney W. Grimes 		int a_name;
1917df8bae1dSRodney W. Grimes 		int *a_retval;
1918df8bae1dSRodney W. Grimes 	} */ *ap;
1919df8bae1dSRodney W. Grimes {
1920df8bae1dSRodney W. Grimes 	int error;
1921996c772fSJohn Dyson 	struct proc *p = curproc;		/* XXX */
19222a31267eSMatthew Dillon 	struct union_node *un = VTOUNION(ap->a_vp);
19232a31267eSMatthew Dillon 	struct vnode *vp;
1924df8bae1dSRodney W. Grimes 
19252a31267eSMatthew Dillon 	vp = union_lock_other(un, p);
19262a31267eSMatthew Dillon 	KASSERT(vp != NULL, ("union_pathconf: backing vnode missing!"));
19272a31267eSMatthew Dillon 
1928996c772fSJohn Dyson 	ap->a_vp = vp;
1929996c772fSJohn Dyson 	error = VCALL(vp, VOFFSET(vop_pathconf), ap);
19302a31267eSMatthew Dillon 	union_unlock_other(vp, p);
1931df8bae1dSRodney W. Grimes 
1932df8bae1dSRodney W. Grimes 	return (error);
1933df8bae1dSRodney W. Grimes }
1934df8bae1dSRodney W. Grimes 
1935c9bf0111SKATO Takenori static int
1936df8bae1dSRodney W. Grimes union_advlock(ap)
1937df8bae1dSRodney W. Grimes 	struct vop_advlock_args /* {
1938df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
1939df8bae1dSRodney W. Grimes 		caddr_t  a_id;
1940df8bae1dSRodney W. Grimes 		int  a_op;
1941df8bae1dSRodney W. Grimes 		struct flock *a_fl;
1942df8bae1dSRodney W. Grimes 		int  a_flags;
1943df8bae1dSRodney W. Grimes 	} */ *ap;
1944df8bae1dSRodney W. Grimes {
1945996c772fSJohn Dyson 	register struct vnode *ovp = OTHERVP(ap->a_vp);
1946df8bae1dSRodney W. Grimes 
1947996c772fSJohn Dyson 	ap->a_vp = ovp;
1948996c772fSJohn Dyson 	return (VCALL(ovp, VOFFSET(vop_advlock), ap));
1949df8bae1dSRodney W. Grimes }
1950df8bae1dSRodney W. Grimes 
1951df8bae1dSRodney W. Grimes 
1952df8bae1dSRodney W. Grimes /*
1953df8bae1dSRodney W. Grimes  * XXX - vop_strategy must be hand coded because it has no
19542a31267eSMatthew Dillon  * YYY - and it is not coherent with anything
19552a31267eSMatthew Dillon  *
1956df8bae1dSRodney W. Grimes  * vnode in its arguments.
1957df8bae1dSRodney W. Grimes  * This goes away with a merged VM/buffer cache.
1958df8bae1dSRodney W. Grimes  */
1959c9bf0111SKATO Takenori static int
1960df8bae1dSRodney W. Grimes union_strategy(ap)
1961df8bae1dSRodney W. Grimes 	struct vop_strategy_args /* {
1962fd5d1124SJulian Elischer 		struct vnode *a_vp;
1963df8bae1dSRodney W. Grimes 		struct buf *a_bp;
1964df8bae1dSRodney W. Grimes 	} */ *ap;
1965df8bae1dSRodney W. Grimes {
1966df8bae1dSRodney W. Grimes 	struct buf *bp = ap->a_bp;
1967f9c8cab5SKirk McKusick 	struct vnode *othervp = OTHERVP(bp->b_vp);
1968df8bae1dSRodney W. Grimes 
1969df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
1970f9c8cab5SKirk McKusick 	if (othervp == NULLVP)
1971df8bae1dSRodney W. Grimes 		panic("union_strategy: nil vp");
1972df8bae1dSRodney W. Grimes 	if (((bp->b_flags & B_READ) == 0) &&
1973f9c8cab5SKirk McKusick 	    (othervp == LOWERVP(bp->b_vp)))
1974df8bae1dSRodney W. Grimes 		panic("union_strategy: writing to lowervp");
1975df8bae1dSRodney W. Grimes #endif
1976f9c8cab5SKirk McKusick 	return (VOP_STRATEGY(othervp, bp));
1977df8bae1dSRodney W. Grimes }
1978df8bae1dSRodney W. Grimes 
1979df8bae1dSRodney W. Grimes /*
1980df8bae1dSRodney W. Grimes  * Global vfs data structures
1981df8bae1dSRodney W. Grimes  */
1982f57e6547SBruce Evans vop_t **union_vnodeop_p;
1983c109c577SBruce Evans static struct vnodeopv_entry_desc union_vnodeop_entries[] = {
1984dba3870cSPoul-Henning Kamp 	{ &vop_default_desc,		(vop_t *) vop_defaultop },
1985539ef70cSPoul-Henning Kamp 	{ &vop_abortop_desc,		(vop_t *) union_abortop },
1986539ef70cSPoul-Henning Kamp 	{ &vop_access_desc,		(vop_t *) union_access },
1987539ef70cSPoul-Henning Kamp 	{ &vop_advlock_desc,		(vop_t *) union_advlock },
1988539ef70cSPoul-Henning Kamp 	{ &vop_bmap_desc,		(vop_t *) union_bmap },
1989539ef70cSPoul-Henning Kamp 	{ &vop_close_desc,		(vop_t *) union_close },
1990539ef70cSPoul-Henning Kamp 	{ &vop_create_desc,		(vop_t *) union_create },
1991539ef70cSPoul-Henning Kamp 	{ &vop_fsync_desc,		(vop_t *) union_fsync },
19922a31267eSMatthew Dillon 	{ &vop_getpages_desc,		(vop_t *) union_getpages },
19932a31267eSMatthew Dillon 	{ &vop_putpages_desc,		(vop_t *) union_putpages },
1994539ef70cSPoul-Henning Kamp 	{ &vop_getattr_desc,		(vop_t *) union_getattr },
1995539ef70cSPoul-Henning Kamp 	{ &vop_inactive_desc,		(vop_t *) union_inactive },
1996539ef70cSPoul-Henning Kamp 	{ &vop_ioctl_desc,		(vop_t *) union_ioctl },
19972a31267eSMatthew Dillon 	{ &vop_islocked_desc,		(vop_t *) vop_stdislocked },
1998539ef70cSPoul-Henning Kamp 	{ &vop_lease_desc,		(vop_t *) union_lease },
1999539ef70cSPoul-Henning Kamp 	{ &vop_link_desc,		(vop_t *) union_link },
2000539ef70cSPoul-Henning Kamp 	{ &vop_lock_desc,		(vop_t *) union_lock },
2001539ef70cSPoul-Henning Kamp 	{ &vop_lookup_desc,		(vop_t *) union_lookup },
2002539ef70cSPoul-Henning Kamp 	{ &vop_mkdir_desc,		(vop_t *) union_mkdir },
2003539ef70cSPoul-Henning Kamp 	{ &vop_mknod_desc,		(vop_t *) union_mknod },
2004539ef70cSPoul-Henning Kamp 	{ &vop_mmap_desc,		(vop_t *) union_mmap },
2005539ef70cSPoul-Henning Kamp 	{ &vop_open_desc,		(vop_t *) union_open },
2006539ef70cSPoul-Henning Kamp 	{ &vop_pathconf_desc,		(vop_t *) union_pathconf },
2007539ef70cSPoul-Henning Kamp 	{ &vop_poll_desc,		(vop_t *) union_poll },
2008539ef70cSPoul-Henning Kamp 	{ &vop_print_desc,		(vop_t *) union_print },
2009539ef70cSPoul-Henning Kamp 	{ &vop_read_desc,		(vop_t *) union_read },
2010539ef70cSPoul-Henning Kamp 	{ &vop_readdir_desc,		(vop_t *) union_readdir },
2011539ef70cSPoul-Henning Kamp 	{ &vop_readlink_desc,		(vop_t *) union_readlink },
2012539ef70cSPoul-Henning Kamp 	{ &vop_reclaim_desc,		(vop_t *) union_reclaim },
2013539ef70cSPoul-Henning Kamp 	{ &vop_remove_desc,		(vop_t *) union_remove },
2014539ef70cSPoul-Henning Kamp 	{ &vop_rename_desc,		(vop_t *) union_rename },
2015539ef70cSPoul-Henning Kamp 	{ &vop_revoke_desc,		(vop_t *) union_revoke },
2016539ef70cSPoul-Henning Kamp 	{ &vop_rmdir_desc,		(vop_t *) union_rmdir },
2017539ef70cSPoul-Henning Kamp 	{ &vop_setattr_desc,		(vop_t *) union_setattr },
2018539ef70cSPoul-Henning Kamp 	{ &vop_strategy_desc,		(vop_t *) union_strategy },
2019539ef70cSPoul-Henning Kamp 	{ &vop_symlink_desc,		(vop_t *) union_symlink },
2020539ef70cSPoul-Henning Kamp 	{ &vop_unlock_desc,		(vop_t *) union_unlock },
2021539ef70cSPoul-Henning Kamp 	{ &vop_whiteout_desc,		(vop_t *) union_whiteout },
2022539ef70cSPoul-Henning Kamp 	{ &vop_write_desc,		(vop_t *) union_write },
2023f57e6547SBruce Evans 	{ NULL, NULL }
2024df8bae1dSRodney W. Grimes };
2025c109c577SBruce Evans static struct vnodeopv_desc union_vnodeop_opv_desc =
2026df8bae1dSRodney W. Grimes 	{ &union_vnodeop_p, union_vnodeop_entries };
2027c901836cSGarrett Wollman 
2028c901836cSGarrett Wollman VNODEOP_SET(union_vnodeop_opv_desc);
2029