xref: /freebsd/sys/fs/unionfs/union_vnops.c (revision d6476ada99dfd8db77be244d6f4c5f465beabfcb)
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
38d6476adaSKATO Takenori  * $Id: union_vnops.c,v 1.53 1998/02/06 12:13:44 eivind Exp $
39df8bae1dSRodney W. Grimes  */
40df8bae1dSRodney W. Grimes 
41df8bae1dSRodney W. Grimes #include <sys/param.h>
42df8bae1dSRodney W. Grimes #include <sys/systm.h>
43df8bae1dSRodney W. Grimes #include <sys/proc.h>
443ac4d1efSBruce Evans #include <sys/fcntl.h>
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>
53df8bae1dSRodney W. Grimes #include <miscfs/union/union.h>
54df8bae1dSRodney W. Grimes 
55996c772fSJohn Dyson #define FIXUP(un, p) { \
56996c772fSJohn Dyson 	if (((un)->un_flags & UN_ULOCK) == 0) { \
57996c772fSJohn Dyson 		union_fixup(un, p); \
58df8bae1dSRodney W. Grimes 	} \
59df8bae1dSRodney W. Grimes }
60df8bae1dSRodney W. Grimes 
61c9bf0111SKATO Takenori static int	union_abortop __P((struct vop_abortop_args *ap));
62c9bf0111SKATO Takenori static int	union_access __P((struct vop_access_args *ap));
63c9bf0111SKATO Takenori static int	union_advlock __P((struct vop_advlock_args *ap));
64c9bf0111SKATO Takenori static int	union_bmap __P((struct vop_bmap_args *ap));
65c9bf0111SKATO Takenori static int	union_close __P((struct vop_close_args *ap));
66c9bf0111SKATO Takenori static int	union_create __P((struct vop_create_args *ap));
67996c772fSJohn Dyson static void	union_fixup __P((struct union_node *un, struct proc *p));
68c9bf0111SKATO Takenori static int	union_fsync __P((struct vop_fsync_args *ap));
69c9bf0111SKATO Takenori static int	union_getattr __P((struct vop_getattr_args *ap));
70c9bf0111SKATO Takenori static int	union_inactive __P((struct vop_inactive_args *ap));
71c9bf0111SKATO Takenori static int	union_ioctl __P((struct vop_ioctl_args *ap));
72c9bf0111SKATO Takenori static int	union_islocked __P((struct vop_islocked_args *ap));
73c9bf0111SKATO Takenori static int	union_lease __P((struct vop_lease_args *ap));
74c9bf0111SKATO Takenori static int	union_link __P((struct vop_link_args *ap));
75c9bf0111SKATO Takenori static int	union_lock __P((struct vop_lock_args *ap));
76c9bf0111SKATO Takenori static int	union_lookup __P((struct vop_lookup_args *ap));
77996c772fSJohn Dyson static int	union_lookup1 __P((struct vnode *udvp, struct vnode **dvpp,
789b5e8b3aSBruce Evans 				   struct vnode **vpp,
799b5e8b3aSBruce Evans 				   struct componentname *cnp));
80c9bf0111SKATO Takenori static int	union_mkdir __P((struct vop_mkdir_args *ap));
81c9bf0111SKATO Takenori static int	union_mknod __P((struct vop_mknod_args *ap));
82c9bf0111SKATO Takenori static int	union_mmap __P((struct vop_mmap_args *ap));
83c9bf0111SKATO Takenori static int	union_open __P((struct vop_open_args *ap));
84c9bf0111SKATO Takenori static int	union_pathconf __P((struct vop_pathconf_args *ap));
85c9bf0111SKATO Takenori static int	union_print __P((struct vop_print_args *ap));
86c9bf0111SKATO Takenori static int	union_read __P((struct vop_read_args *ap));
87c9bf0111SKATO Takenori static int	union_readdir __P((struct vop_readdir_args *ap));
88c9bf0111SKATO Takenori static int	union_readlink __P((struct vop_readlink_args *ap));
89c9bf0111SKATO Takenori static int	union_reclaim __P((struct vop_reclaim_args *ap));
90c9bf0111SKATO Takenori static int	union_remove __P((struct vop_remove_args *ap));
91c9bf0111SKATO Takenori static int	union_rename __P((struct vop_rename_args *ap));
92c9bf0111SKATO Takenori static int	union_revoke __P((struct vop_revoke_args *ap));
93c9bf0111SKATO Takenori static int	union_rmdir __P((struct vop_rmdir_args *ap));
94a6aeade2SPeter Wemm static int	union_poll __P((struct vop_poll_args *ap));
95c9bf0111SKATO Takenori static int	union_setattr __P((struct vop_setattr_args *ap));
96c9bf0111SKATO Takenori static int	union_strategy __P((struct vop_strategy_args *ap));
97c9bf0111SKATO Takenori static int	union_symlink __P((struct vop_symlink_args *ap));
98c9bf0111SKATO Takenori static int	union_unlock __P((struct vop_unlock_args *ap));
99c9bf0111SKATO Takenori static int	union_whiteout __P((struct vop_whiteout_args *ap));
100c9bf0111SKATO Takenori static int	union_write __P((struct vop_read_args *ap));
1019b5e8b3aSBruce Evans 
102df8bae1dSRodney W. Grimes static void
103996c772fSJohn Dyson union_fixup(un, p)
104df8bae1dSRodney W. Grimes 	struct union_node *un;
105996c772fSJohn Dyson 	struct proc *p;
106df8bae1dSRodney W. Grimes {
107df8bae1dSRodney W. Grimes 
108996c772fSJohn Dyson 	vn_lock(un->un_uppervp, LK_EXCLUSIVE | LK_RETRY, p);
109df8bae1dSRodney W. Grimes 	un->un_flags |= UN_ULOCK;
110df8bae1dSRodney W. Grimes }
111df8bae1dSRodney W. Grimes 
112df8bae1dSRodney W. Grimes static int
113996c772fSJohn Dyson union_lookup1(udvp, dvpp, vpp, cnp)
114df8bae1dSRodney W. Grimes 	struct vnode *udvp;
115996c772fSJohn Dyson 	struct vnode **dvpp;
116df8bae1dSRodney W. Grimes 	struct vnode **vpp;
117df8bae1dSRodney W. Grimes 	struct componentname *cnp;
118df8bae1dSRodney W. Grimes {
119df8bae1dSRodney W. Grimes 	int error;
120996c772fSJohn Dyson 	struct proc *p = cnp->cn_proc;
121df8bae1dSRodney W. Grimes 	struct vnode *tdvp;
122996c772fSJohn Dyson 	struct vnode *dvp;
123df8bae1dSRodney W. Grimes 	struct mount *mp;
124df8bae1dSRodney W. Grimes 
125996c772fSJohn Dyson 	dvp = *dvpp;
126996c772fSJohn Dyson 
127df8bae1dSRodney W. Grimes 	/*
128df8bae1dSRodney W. Grimes 	 * If stepping up the directory tree, check for going
129df8bae1dSRodney W. Grimes 	 * back across the mount point, in which case do what
130df8bae1dSRodney W. Grimes 	 * lookup would do by stepping back down the mount
131df8bae1dSRodney W. Grimes 	 * hierarchy.
132df8bae1dSRodney W. Grimes 	 */
133df8bae1dSRodney W. Grimes 	if (cnp->cn_flags & ISDOTDOT) {
134996c772fSJohn Dyson 		while ((dvp != udvp) && (dvp->v_flag & VROOT)) {
135df8bae1dSRodney W. Grimes 			/*
136df8bae1dSRodney W. Grimes 			 * Don't do the NOCROSSMOUNT check
137df8bae1dSRodney W. Grimes 			 * at this level.  By definition,
138df8bae1dSRodney W. Grimes 			 * union fs deals with namespaces, not
139df8bae1dSRodney W. Grimes 			 * filesystems.
140df8bae1dSRodney W. Grimes 			 */
141df8bae1dSRodney W. Grimes 			tdvp = dvp;
142996c772fSJohn Dyson 			*dvpp = dvp = dvp->v_mount->mnt_vnodecovered;
143df8bae1dSRodney W. Grimes 			vput(tdvp);
144df8bae1dSRodney W. Grimes 			VREF(dvp);
145996c772fSJohn Dyson 			vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, p);
146df8bae1dSRodney W. Grimes 		}
147df8bae1dSRodney W. Grimes 	}
148df8bae1dSRodney W. Grimes 
149df8bae1dSRodney W. Grimes         error = VOP_LOOKUP(dvp, &tdvp, cnp);
150df8bae1dSRodney W. Grimes 	if (error)
151df8bae1dSRodney W. Grimes 		return (error);
152df8bae1dSRodney W. Grimes 
153df8bae1dSRodney W. Grimes 	/*
154df8bae1dSRodney W. Grimes 	 * The parent directory will have been unlocked, unless lookup
155df8bae1dSRodney W. Grimes 	 * found the last component.  In which case, re-lock the node
156df8bae1dSRodney W. Grimes 	 * here to allow it to be unlocked again (phew) in union_lookup.
157df8bae1dSRodney W. Grimes 	 */
158df8bae1dSRodney W. Grimes 	if (dvp != tdvp && !(cnp->cn_flags & ISLASTCN))
159996c772fSJohn Dyson 		vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, p);
160df8bae1dSRodney W. Grimes 
161df8bae1dSRodney W. Grimes 	dvp = tdvp;
162df8bae1dSRodney W. Grimes 
163df8bae1dSRodney W. Grimes 	/*
164df8bae1dSRodney W. Grimes 	 * Lastly check if the current node is a mount point in
165df8bae1dSRodney W. Grimes 	 * which case walk up the mount hierarchy making sure not to
166df8bae1dSRodney W. Grimes 	 * bump into the root of the mount tree (ie. dvp != udvp).
167df8bae1dSRodney W. Grimes 	 */
168df8bae1dSRodney W. Grimes 	while (dvp != udvp && (dvp->v_type == VDIR) &&
169df8bae1dSRodney W. Grimes 	       (mp = dvp->v_mountedhere)) {
170df8bae1dSRodney W. Grimes 
171996c772fSJohn Dyson 		if (vfs_busy(mp, 0, 0, p))
172df8bae1dSRodney W. Grimes 			continue;
173df8bae1dSRodney W. Grimes 
1743a773ad0SPoul-Henning Kamp 		error = VFS_ROOT(mp, &tdvp);
175996c772fSJohn Dyson 		vfs_unbusy(mp, p);
1763a773ad0SPoul-Henning Kamp 		if (error) {
177df8bae1dSRodney W. Grimes 			vput(dvp);
178df8bae1dSRodney W. Grimes 			return (error);
179df8bae1dSRodney W. Grimes 		}
180df8bae1dSRodney W. Grimes 
181df8bae1dSRodney W. Grimes 		vput(dvp);
182df8bae1dSRodney W. Grimes 		dvp = tdvp;
183df8bae1dSRodney W. Grimes 	}
184df8bae1dSRodney W. Grimes 
185df8bae1dSRodney W. Grimes 	*vpp = dvp;
186df8bae1dSRodney W. Grimes 	return (0);
187df8bae1dSRodney W. Grimes }
188df8bae1dSRodney W. Grimes 
189c9bf0111SKATO Takenori static int
190df8bae1dSRodney W. Grimes union_lookup(ap)
191df8bae1dSRodney W. Grimes 	struct vop_lookup_args /* {
192df8bae1dSRodney W. Grimes 		struct vnodeop_desc *a_desc;
193df8bae1dSRodney W. Grimes 		struct vnode *a_dvp;
194df8bae1dSRodney W. Grimes 		struct vnode **a_vpp;
195df8bae1dSRodney W. Grimes 		struct componentname *a_cnp;
196df8bae1dSRodney W. Grimes 	} */ *ap;
197df8bae1dSRodney W. Grimes {
198df8bae1dSRodney W. Grimes 	int error;
199df8bae1dSRodney W. Grimes 	int uerror, lerror;
200df8bae1dSRodney W. Grimes 	struct vnode *uppervp, *lowervp;
201df8bae1dSRodney W. Grimes 	struct vnode *upperdvp, *lowerdvp;
202df8bae1dSRodney W. Grimes 	struct vnode *dvp = ap->a_dvp;
203df8bae1dSRodney W. Grimes 	struct union_node *dun = VTOUNION(dvp);
204df8bae1dSRodney W. Grimes 	struct componentname *cnp = ap->a_cnp;
205996c772fSJohn Dyson 	struct proc *p = cnp->cn_proc;
206df8bae1dSRodney W. Grimes 	int lockparent = cnp->cn_flags & LOCKPARENT;
207df8bae1dSRodney W. Grimes 	struct union_mount *um = MOUNTTOUNIONMOUNT(dvp->v_mount);
208996c772fSJohn Dyson 	struct ucred *saved_cred;
209996c772fSJohn Dyson 	int iswhiteout;
210996c772fSJohn Dyson 	struct vattr va;
211996c772fSJohn Dyson 
2126ca02614SKATO Takenori 
2136ca02614SKATO Takenori 	/*
2146ca02614SKATO Takenori 	 * Disallow write attemps to the filesystem mounted read-only.
2156ca02614SKATO Takenori 	 */
2166ca02614SKATO Takenori 	if ((cnp->cn_flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
2176ca02614SKATO Takenori 		(cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
2186ca02614SKATO Takenori 		return (EROFS);
2196ca02614SKATO Takenori 
220996c772fSJohn Dyson #ifdef notyet
221996c772fSJohn Dyson 	if (cnp->cn_namelen == 3 &&
222996c772fSJohn Dyson 			cnp->cn_nameptr[2] == '.' &&
223996c772fSJohn Dyson 			cnp->cn_nameptr[1] == '.' &&
224996c772fSJohn Dyson 			cnp->cn_nameptr[0] == '.') {
225996c772fSJohn Dyson 		dvp = *ap->a_vpp = LOWERVP(ap->a_dvp);
226996c772fSJohn Dyson 		if (dvp == NULLVP)
227996c772fSJohn Dyson 			return (ENOENT);
228996c772fSJohn Dyson 		VREF(dvp);
229996c772fSJohn Dyson 		vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, p);
230996c772fSJohn Dyson 		if (!lockparent || !(cnp->cn_flags & ISLASTCN))
231996c772fSJohn Dyson 			VOP_UNLOCK(ap->a_dvp, 0, p);
232996c772fSJohn Dyson 		return (0);
233996c772fSJohn Dyson 	}
234996c772fSJohn Dyson #endif
235df8bae1dSRodney W. Grimes 
236df8bae1dSRodney W. Grimes 	cnp->cn_flags |= LOCKPARENT;
237df8bae1dSRodney W. Grimes 
238df8bae1dSRodney W. Grimes 	upperdvp = dun->un_uppervp;
239df8bae1dSRodney W. Grimes 	lowerdvp = dun->un_lowervp;
240df8bae1dSRodney W. Grimes 	uppervp = NULLVP;
241df8bae1dSRodney W. Grimes 	lowervp = NULLVP;
242996c772fSJohn Dyson 	iswhiteout = 0;
243df8bae1dSRodney W. Grimes 
244afc2a558SKATO Takenori 	if (cnp->cn_flags & ISDOTDOT) {
245afc2a558SKATO Takenori 		if (upperdvp != NULL)
246afc2a558SKATO Takenori 			VREF(upperdvp);
247afc2a558SKATO Takenori 		if (lowerdvp != NULL)
248afc2a558SKATO Takenori 			VREF(lowerdvp);
249afc2a558SKATO Takenori 	}
250afc2a558SKATO Takenori 
251df8bae1dSRodney W. Grimes 	/*
252df8bae1dSRodney W. Grimes 	 * do the lookup in the upper level.
253df8bae1dSRodney W. Grimes 	 * if that level comsumes additional pathnames,
254df8bae1dSRodney W. Grimes 	 * then assume that something special is going
255df8bae1dSRodney W. Grimes 	 * on and just return that vnode.
256df8bae1dSRodney W. Grimes 	 */
257996c772fSJohn Dyson 	if (upperdvp != NULLVP) {
258996c772fSJohn Dyson 		FIXUP(dun, p);
259ee582cdfSKATO Takenori 		/*
260ee582cdfSKATO Takenori 		 * If we're doing `..' in the underlying filesystem,
261ee582cdfSKATO Takenori 		 * we must drop our lock on the union node before
262ee582cdfSKATO Takenori 		 * going up the tree in the lower file system--if we block
263ee582cdfSKATO Takenori 		 * on the lowervp lock, and that's held by someone else
264ee582cdfSKATO Takenori 		 * coming down the tree and who's waiting for our lock,
265ee582cdfSKATO Takenori 		 * we would be hosed.
266ee582cdfSKATO Takenori 		 */
267ee582cdfSKATO Takenori 		if (cnp->cn_flags & ISDOTDOT) {
268ee582cdfSKATO Takenori 			/* retain lock on underlying VP: */
269d6476adaSKATO Takenori 			dun->un_flags |= UN_KLOCK;
270ee582cdfSKATO Takenori 			VOP_UNLOCK(dvp, 0, p);
271ee582cdfSKATO Takenori 		}
272996c772fSJohn Dyson 		uerror = union_lookup1(um->um_uppervp, &upperdvp,
273df8bae1dSRodney W. Grimes 					&uppervp, cnp);
2746ca02614SKATO Takenori 		/*
2756ca02614SKATO Takenori 		 * Disallow write attemps to the filesystem mounted read-only.
2766ca02614SKATO Takenori 		 */
2776ca02614SKATO Takenori 		if (uerror == EJUSTRETURN && (cnp->cn_flags & ISLASTCN) &&
2786ca02614SKATO Takenori 			(dvp->v_mount->mnt_flag & MNT_RDONLY) &&
2796ca02614SKATO Takenori 			(cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME)) {
2806ca02614SKATO Takenori 			if (!lockparent)
2816ca02614SKATO Takenori 				cnp->cn_flags &= ~LOCKPARENT;
2826ca02614SKATO Takenori 			return (EROFS);
2836ca02614SKATO Takenori 		}
2846ca02614SKATO Takenori 
285ee582cdfSKATO Takenori 		if (cnp->cn_flags & ISDOTDOT) {
286ee582cdfSKATO Takenori 			if (dun->un_uppervp == upperdvp) {
287ee582cdfSKATO Takenori 				/*
288ee582cdfSKATO Takenori 				 * We got the underlying bugger back locked...
289ee582cdfSKATO Takenori 				 * now take back the union node lock.  Since we
290ee582cdfSKATO Takenori 				 * hold the uppervp lock, we can diddle union
291ee582cdfSKATO Takenori 				 * locking flags at will. :)
292ee582cdfSKATO Takenori 				 */
293ee582cdfSKATO Takenori 				dun->un_flags |= UN_ULOCK;
294ee582cdfSKATO Takenori 			}
295ee582cdfSKATO Takenori 			/*
296ee582cdfSKATO Takenori 			 * If upperdvp got swapped out, it means we did
297ee582cdfSKATO Takenori 			 * some mount point magic, and we do not have
298ee582cdfSKATO Takenori 			 * dun->un_uppervp locked currently--so we get it
299ee582cdfSKATO Takenori 			 * locked here (don't set the UN_ULOCK flag).
300ee582cdfSKATO Takenori 			 */
301077f31beSKATO Takenori 			vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, p);
302ee582cdfSKATO Takenori 		}
303ee582cdfSKATO Takenori 
304df8bae1dSRodney W. Grimes 		/*if (uppervp == upperdvp)
305df8bae1dSRodney W. Grimes 			dun->un_flags |= UN_KLOCK;*/
306df8bae1dSRodney W. Grimes 
3073dc942bbSKATO Takenori 		if (cnp->cn_consume != 0) {
308df8bae1dSRodney W. Grimes 			*ap->a_vpp = uppervp;
309df8bae1dSRodney W. Grimes 			if (!lockparent)
310df8bae1dSRodney W. Grimes 				cnp->cn_flags &= ~LOCKPARENT;
311afc2a558SKATO Takenori 			error = uerror;
312afc2a558SKATO Takenori 			goto out;
313df8bae1dSRodney W. Grimes 		}
314996c772fSJohn Dyson 		if (uerror == ENOENT || uerror == EJUSTRETURN) {
315996c772fSJohn Dyson 			if (cnp->cn_flags & ISWHITEOUT) {
316996c772fSJohn Dyson 				iswhiteout = 1;
317996c772fSJohn Dyson 			} else if (lowerdvp != NULLVP) {
318996c772fSJohn Dyson 				lerror = VOP_GETATTR(upperdvp, &va,
319996c772fSJohn Dyson 					cnp->cn_cred, cnp->cn_proc);
320996c772fSJohn Dyson 				if (lerror == 0 && (va.va_flags & OPAQUE))
321996c772fSJohn Dyson 					iswhiteout = 1;
322996c772fSJohn Dyson 			}
323996c772fSJohn Dyson 		}
324df8bae1dSRodney W. Grimes 	} else {
325df8bae1dSRodney W. Grimes 		uerror = ENOENT;
326df8bae1dSRodney W. Grimes 	}
327df8bae1dSRodney W. Grimes 
328df8bae1dSRodney W. Grimes 	/*
329df8bae1dSRodney W. Grimes 	 * in a similar way to the upper layer, do the lookup
330df8bae1dSRodney W. Grimes 	 * in the lower layer.   this time, if there is some
331df8bae1dSRodney W. Grimes 	 * component magic going on, then vput whatever we got
332df8bae1dSRodney W. Grimes 	 * back from the upper layer and return the lower vnode
333df8bae1dSRodney W. Grimes 	 * instead.
334df8bae1dSRodney W. Grimes 	 */
335996c772fSJohn Dyson 	if (lowerdvp != NULLVP && !iswhiteout) {
336df8bae1dSRodney W. Grimes 		int nameiop;
337df8bae1dSRodney W. Grimes 
338996c772fSJohn Dyson 		vn_lock(lowerdvp, LK_EXCLUSIVE | LK_RETRY, p);
339df8bae1dSRodney W. Grimes 
340df8bae1dSRodney W. Grimes 		/*
341df8bae1dSRodney W. Grimes 		 * Only do a LOOKUP on the bottom node, since
342df8bae1dSRodney W. Grimes 		 * we won't be making changes to it anyway.
343df8bae1dSRodney W. Grimes 		 */
344df8bae1dSRodney W. Grimes 		nameiop = cnp->cn_nameiop;
345df8bae1dSRodney W. Grimes 		cnp->cn_nameiop = LOOKUP;
346df8bae1dSRodney W. Grimes 		if (um->um_op == UNMNT_BELOW) {
347df8bae1dSRodney W. Grimes 			saved_cred = cnp->cn_cred;
348df8bae1dSRodney W. Grimes 			cnp->cn_cred = um->um_cred;
349df8bae1dSRodney W. Grimes 		}
350ee582cdfSKATO Takenori 		/*
351ee582cdfSKATO Takenori 		 * We shouldn't have to worry about locking interactions
352ee582cdfSKATO Takenori 		 * between the lower layer and our union layer (w.r.t.
353ee582cdfSKATO Takenori 		 * `..' processing) because we don't futz with lowervp
354ee582cdfSKATO Takenori 		 * locks in the union-node instantiation code path.
355ee582cdfSKATO Takenori 		 */
356996c772fSJohn Dyson 		lerror = union_lookup1(um->um_lowervp, &lowerdvp,
357df8bae1dSRodney W. Grimes 				&lowervp, cnp);
358df8bae1dSRodney W. Grimes 		if (um->um_op == UNMNT_BELOW)
359df8bae1dSRodney W. Grimes 			cnp->cn_cred = saved_cred;
360df8bae1dSRodney W. Grimes 		cnp->cn_nameiop = nameiop;
361df8bae1dSRodney W. Grimes 
362df8bae1dSRodney W. Grimes 		if (lowervp != lowerdvp)
363996c772fSJohn Dyson 			VOP_UNLOCK(lowerdvp, 0, p);
364df8bae1dSRodney W. Grimes 
365f8fc96b5SKATO Takenori 		if (cnp->cn_consume != 0 || lerror == EACCES) {
366f8fc96b5SKATO Takenori 			if (lerror == EACCES)
367f8fc96b5SKATO Takenori 				lowervp = NULLVP;
368996c772fSJohn Dyson 			if (uppervp != NULLVP) {
369df8bae1dSRodney W. Grimes 				if (uppervp == upperdvp)
370df8bae1dSRodney W. Grimes 					vrele(uppervp);
371df8bae1dSRodney W. Grimes 				else
372df8bae1dSRodney W. Grimes 					vput(uppervp);
373df8bae1dSRodney W. Grimes 				uppervp = NULLVP;
374df8bae1dSRodney W. Grimes 			}
375df8bae1dSRodney W. Grimes 			*ap->a_vpp = lowervp;
376df8bae1dSRodney W. Grimes 			if (!lockparent)
377df8bae1dSRodney W. Grimes 				cnp->cn_flags &= ~LOCKPARENT;
378afc2a558SKATO Takenori 			error = lerror;
379afc2a558SKATO Takenori 			goto out;
380df8bae1dSRodney W. Grimes 		}
381df8bae1dSRodney W. Grimes 	} else {
382df8bae1dSRodney W. Grimes 		lerror = ENOENT;
383996c772fSJohn Dyson 		if ((cnp->cn_flags & ISDOTDOT) && dun->un_pvp != NULLVP) {
384996c772fSJohn Dyson 			lowervp = LOWERVP(dun->un_pvp);
385996c772fSJohn Dyson 			if (lowervp != NULLVP) {
386996c772fSJohn Dyson 				VREF(lowervp);
387996c772fSJohn Dyson 				vn_lock(lowervp, LK_EXCLUSIVE | LK_RETRY, p);
388996c772fSJohn Dyson 				lerror = 0;
389996c772fSJohn Dyson 			}
390996c772fSJohn Dyson 		}
391df8bae1dSRodney W. Grimes 	}
392df8bae1dSRodney W. Grimes 
393df8bae1dSRodney W. Grimes 	if (!lockparent)
394df8bae1dSRodney W. Grimes 		cnp->cn_flags &= ~LOCKPARENT;
395df8bae1dSRodney W. Grimes 
396df8bae1dSRodney W. Grimes 	/*
397df8bae1dSRodney W. Grimes 	 * at this point, we have uerror and lerror indicating
398df8bae1dSRodney W. Grimes 	 * possible errors with the lookups in the upper and lower
399df8bae1dSRodney W. Grimes 	 * layers.  additionally, uppervp and lowervp are (locked)
400df8bae1dSRodney W. Grimes 	 * references to existing vnodes in the upper and lower layers.
401df8bae1dSRodney W. Grimes 	 *
402df8bae1dSRodney W. Grimes 	 * there are now three cases to consider.
403df8bae1dSRodney W. Grimes 	 * 1. if both layers returned an error, then return whatever
404df8bae1dSRodney W. Grimes 	 *    error the upper layer generated.
405df8bae1dSRodney W. Grimes 	 *
406df8bae1dSRodney W. Grimes 	 * 2. if the top layer failed and the bottom layer succeeded
407df8bae1dSRodney W. Grimes 	 *    then two subcases occur.
408df8bae1dSRodney W. Grimes 	 *    a.  the bottom vnode is not a directory, in which
409df8bae1dSRodney W. Grimes 	 *	  case just return a new union vnode referencing
410df8bae1dSRodney W. Grimes 	 *	  an empty top layer and the existing bottom layer.
411df8bae1dSRodney W. Grimes 	 *    b.  the bottom vnode is a directory, in which case
412df8bae1dSRodney W. Grimes 	 *	  create a new directory in the top-level and
413df8bae1dSRodney W. Grimes 	 *	  continue as in case 3.
414df8bae1dSRodney W. Grimes 	 *
415df8bae1dSRodney W. Grimes 	 * 3. if the top layer succeeded then return a new union
416df8bae1dSRodney W. Grimes 	 *    vnode referencing whatever the new top layer and
417df8bae1dSRodney W. Grimes 	 *    whatever the bottom layer returned.
418df8bae1dSRodney W. Grimes 	 */
419df8bae1dSRodney W. Grimes 
420df8bae1dSRodney W. Grimes 	*ap->a_vpp = NULLVP;
421df8bae1dSRodney W. Grimes 
422df8bae1dSRodney W. Grimes 	/* case 1. */
423df8bae1dSRodney W. Grimes 	if ((uerror != 0) && (lerror != 0)) {
424afc2a558SKATO Takenori 		error = uerror;
425afc2a558SKATO Takenori 		goto out;
426df8bae1dSRodney W. Grimes 	}
427df8bae1dSRodney W. Grimes 
428df8bae1dSRodney W. Grimes 	/* case 2. */
429df8bae1dSRodney W. Grimes 	if (uerror != 0 /* && (lerror == 0) */ ) {
430df8bae1dSRodney W. Grimes 		if (lowervp->v_type == VDIR) { /* case 2b. */
431df8bae1dSRodney W. Grimes 			dun->un_flags &= ~UN_ULOCK;
432996c772fSJohn Dyson 			VOP_UNLOCK(upperdvp, 0, p);
433df8bae1dSRodney W. Grimes 			uerror = union_mkshadow(um, upperdvp, cnp, &uppervp);
434996c772fSJohn Dyson 			vn_lock(upperdvp, LK_EXCLUSIVE | LK_RETRY, p);
435df8bae1dSRodney W. Grimes 			dun->un_flags |= UN_ULOCK;
436df8bae1dSRodney W. Grimes 
437df8bae1dSRodney W. Grimes 			if (uerror) {
438996c772fSJohn Dyson 				if (lowervp != NULLVP) {
439df8bae1dSRodney W. Grimes 					vput(lowervp);
440df8bae1dSRodney W. Grimes 					lowervp = NULLVP;
441df8bae1dSRodney W. Grimes 				}
442afc2a558SKATO Takenori 				error = uerror;
443afc2a558SKATO Takenori 				goto out;
444df8bae1dSRodney W. Grimes 			}
445df8bae1dSRodney W. Grimes 		}
446df8bae1dSRodney W. Grimes 	}
447df8bae1dSRodney W. Grimes 
448996c772fSJohn Dyson 	if (lowervp != NULLVP)
449996c772fSJohn Dyson 		VOP_UNLOCK(lowervp, 0, p);
450df8bae1dSRodney W. Grimes 
451df8bae1dSRodney W. Grimes 	error = union_allocvp(ap->a_vpp, dvp->v_mount, dvp, upperdvp, cnp,
452996c772fSJohn Dyson 			      uppervp, lowervp, 1);
453df8bae1dSRodney W. Grimes 
454df8bae1dSRodney W. Grimes 	if (error) {
455996c772fSJohn Dyson 		if (uppervp != NULLVP)
456df8bae1dSRodney W. Grimes 			vput(uppervp);
457996c772fSJohn Dyson 		if (lowervp != NULLVP)
458df8bae1dSRodney W. Grimes 			vrele(lowervp);
459df8bae1dSRodney W. Grimes 	} else {
460df8bae1dSRodney W. Grimes 		if (*ap->a_vpp != dvp)
461df8bae1dSRodney W. Grimes 			if (!lockparent || !(cnp->cn_flags & ISLASTCN))
462996c772fSJohn Dyson 				VOP_UNLOCK(dvp, 0, p);
463f5a5311eSKATO Takenori #ifdef DIAGNOSTIC
464f5a5311eSKATO Takenori         if (cnp->cn_namelen == 1 &&
465f5a5311eSKATO Takenori             cnp->cn_nameptr[0] == '.' &&
466f5a5311eSKATO Takenori             *ap->a_vpp != dvp) {
467f5a5311eSKATO Takenori             panic("union_lookup returning . (%p) not same as startdir (%p)",
468f5a5311eSKATO Takenori 				  ap->a_vpp, dvp);
469f5a5311eSKATO Takenori 		}
470f5a5311eSKATO Takenori #endif
471df8bae1dSRodney W. Grimes 	}
472df8bae1dSRodney W. Grimes 
473afc2a558SKATO Takenori out:
474afc2a558SKATO Takenori     if (cnp->cn_flags & ISDOTDOT) {
475afc2a558SKATO Takenori         if (upperdvp != NULL)
476afc2a558SKATO Takenori             vrele(upperdvp);
477afc2a558SKATO Takenori         if (lowerdvp != NULL)
478afc2a558SKATO Takenori             vrele(lowerdvp);
479afc2a558SKATO Takenori     }
480afc2a558SKATO Takenori 
481df8bae1dSRodney W. Grimes 	return (error);
482df8bae1dSRodney W. Grimes }
483df8bae1dSRodney W. Grimes 
484c9bf0111SKATO Takenori static int
485df8bae1dSRodney W. Grimes union_create(ap)
486df8bae1dSRodney W. Grimes 	struct vop_create_args /* {
487df8bae1dSRodney W. Grimes 		struct vnode *a_dvp;
488df8bae1dSRodney W. Grimes 		struct vnode **a_vpp;
489df8bae1dSRodney W. Grimes 		struct componentname *a_cnp;
490df8bae1dSRodney W. Grimes 		struct vattr *a_vap;
491df8bae1dSRodney W. Grimes 	} */ *ap;
492df8bae1dSRodney W. Grimes {
493df8bae1dSRodney W. Grimes 	struct union_node *un = VTOUNION(ap->a_dvp);
494df8bae1dSRodney W. Grimes 	struct vnode *dvp = un->un_uppervp;
495996c772fSJohn Dyson 	struct componentname *cnp = ap->a_cnp;
496996c772fSJohn Dyson 	struct proc *p = cnp->cn_proc;
497df8bae1dSRodney W. Grimes 
498996c772fSJohn Dyson 	if (dvp != NULLVP) {
499df8bae1dSRodney W. Grimes 		int error;
500df8bae1dSRodney W. Grimes 		struct vnode *vp;
501996c772fSJohn Dyson 		struct mount *mp;
502df8bae1dSRodney W. Grimes 
503996c772fSJohn Dyson 		FIXUP(un, p);
504df8bae1dSRodney W. Grimes 
505df8bae1dSRodney W. Grimes 		VREF(dvp);
506d6476adaSKATO Takenori 		un->un_flags |= UN_KLOCK;
507996c772fSJohn Dyson 		mp = ap->a_dvp->v_mount;
508df8bae1dSRodney W. Grimes 		vput(ap->a_dvp);
509996c772fSJohn Dyson 		error = VOP_CREATE(dvp, &vp, cnp, ap->a_vap);
510df8bae1dSRodney W. Grimes 		if (error)
511df8bae1dSRodney W. Grimes 			return (error);
512df8bae1dSRodney W. Grimes 
513996c772fSJohn Dyson 		error = union_allocvp(ap->a_vpp, mp, NULLVP, NULLVP, cnp, vp,
514996c772fSJohn Dyson 				NULLVP, 1);
515df8bae1dSRodney W. Grimes 		if (error)
516df8bae1dSRodney W. Grimes 			vput(vp);
517df8bae1dSRodney W. Grimes 		return (error);
518df8bae1dSRodney W. Grimes 	}
519df8bae1dSRodney W. Grimes 
520df8bae1dSRodney W. Grimes 	vput(ap->a_dvp);
521df8bae1dSRodney W. Grimes 	return (EROFS);
522df8bae1dSRodney W. Grimes }
523df8bae1dSRodney W. Grimes 
524c9bf0111SKATO Takenori static int
525996c772fSJohn Dyson union_whiteout(ap)
526996c772fSJohn Dyson 	struct vop_whiteout_args /* {
527996c772fSJohn Dyson 		struct vnode *a_dvp;
528996c772fSJohn Dyson 		struct componentname *a_cnp;
529996c772fSJohn Dyson 		int a_flags;
530996c772fSJohn Dyson 	} */ *ap;
531996c772fSJohn Dyson {
532996c772fSJohn Dyson 	struct union_node *un = VTOUNION(ap->a_dvp);
533996c772fSJohn Dyson 	struct componentname *cnp = ap->a_cnp;
534996c772fSJohn Dyson 	struct proc *p = cnp->cn_proc;
535996c772fSJohn Dyson 
536996c772fSJohn Dyson 	if (un->un_uppervp == NULLVP)
537996c772fSJohn Dyson 		return (EOPNOTSUPP);
538996c772fSJohn Dyson 
539996c772fSJohn Dyson 	FIXUP(un, p);
540996c772fSJohn Dyson 	return (VOP_WHITEOUT(un->un_uppervp, cnp, ap->a_flags));
541996c772fSJohn Dyson }
542996c772fSJohn Dyson 
543c9bf0111SKATO Takenori static int
544df8bae1dSRodney W. Grimes union_mknod(ap)
545df8bae1dSRodney W. Grimes 	struct vop_mknod_args /* {
546df8bae1dSRodney W. Grimes 		struct vnode *a_dvp;
547df8bae1dSRodney W. Grimes 		struct vnode **a_vpp;
548df8bae1dSRodney W. Grimes 		struct componentname *a_cnp;
549df8bae1dSRodney W. Grimes 		struct vattr *a_vap;
550df8bae1dSRodney W. Grimes 	} */ *ap;
551df8bae1dSRodney W. Grimes {
552df8bae1dSRodney W. Grimes 	struct union_node *un = VTOUNION(ap->a_dvp);
553df8bae1dSRodney W. Grimes 	struct vnode *dvp = un->un_uppervp;
554996c772fSJohn Dyson 	struct componentname *cnp = ap->a_cnp;
555996c772fSJohn Dyson 	struct proc *p = cnp->cn_proc;
556df8bae1dSRodney W. Grimes 
557996c772fSJohn Dyson 	if (dvp != NULLVP) {
558df8bae1dSRodney W. Grimes 		int error;
559df8bae1dSRodney W. Grimes 		struct vnode *vp;
560996c772fSJohn Dyson 		struct mount *mp;
561df8bae1dSRodney W. Grimes 
562996c772fSJohn Dyson 		FIXUP(un, p);
563df8bae1dSRodney W. Grimes 
564df8bae1dSRodney W. Grimes 		VREF(dvp);
565d6476adaSKATO Takenori 		un->un_flags |= UN_KLOCK;
566996c772fSJohn Dyson 		mp = ap->a_dvp->v_mount;
567df8bae1dSRodney W. Grimes 		vput(ap->a_dvp);
568996c772fSJohn Dyson 		error = VOP_MKNOD(dvp, &vp, cnp, ap->a_vap);
569df8bae1dSRodney W. Grimes 		if (error)
570df8bae1dSRodney W. Grimes 			return (error);
571df8bae1dSRodney W. Grimes 
572996c772fSJohn Dyson 		if (vp != NULLVP) {
573996c772fSJohn Dyson 			error = union_allocvp(ap->a_vpp, mp, NULLVP, NULLVP,
574996c772fSJohn Dyson 					cnp, vp, NULLVP, 1);
575df8bae1dSRodney W. Grimes 			if (error)
576df8bae1dSRodney W. Grimes 				vput(vp);
577df8bae1dSRodney W. Grimes 		}
578df8bae1dSRodney W. Grimes 		return (error);
579df8bae1dSRodney W. Grimes 	}
580df8bae1dSRodney W. Grimes 
581df8bae1dSRodney W. Grimes 	vput(ap->a_dvp);
582df8bae1dSRodney W. Grimes 	return (EROFS);
583df8bae1dSRodney W. Grimes }
584df8bae1dSRodney W. Grimes 
585c9bf0111SKATO Takenori static int
586df8bae1dSRodney W. Grimes union_open(ap)
587df8bae1dSRodney W. Grimes 	struct vop_open_args /* {
588df8bae1dSRodney W. Grimes 		struct vnodeop_desc *a_desc;
589df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
590df8bae1dSRodney W. Grimes 		int a_mode;
591df8bae1dSRodney W. Grimes 		struct ucred *a_cred;
592df8bae1dSRodney W. Grimes 		struct proc *a_p;
593df8bae1dSRodney W. Grimes 	} */ *ap;
594df8bae1dSRodney W. Grimes {
595df8bae1dSRodney W. Grimes 	struct union_node *un = VTOUNION(ap->a_vp);
596df8bae1dSRodney W. Grimes 	struct vnode *tvp;
597df8bae1dSRodney W. Grimes 	int mode = ap->a_mode;
598df8bae1dSRodney W. Grimes 	struct ucred *cred = ap->a_cred;
599df8bae1dSRodney W. Grimes 	struct proc *p = ap->a_p;
600df8bae1dSRodney W. Grimes 	int error;
601df8bae1dSRodney W. Grimes 
602df8bae1dSRodney W. Grimes 	/*
603df8bae1dSRodney W. Grimes 	 * If there is an existing upper vp then simply open that.
604df8bae1dSRodney W. Grimes 	 */
605df8bae1dSRodney W. Grimes 	tvp = un->un_uppervp;
606df8bae1dSRodney W. Grimes 	if (tvp == NULLVP) {
607df8bae1dSRodney W. Grimes 		/*
608df8bae1dSRodney W. Grimes 		 * If the lower vnode is being opened for writing, then
609df8bae1dSRodney W. Grimes 		 * copy the file contents to the upper vnode and open that,
610df8bae1dSRodney W. Grimes 		 * otherwise can simply open the lower vnode.
611df8bae1dSRodney W. Grimes 		 */
612df8bae1dSRodney W. Grimes 		tvp = un->un_lowervp;
613df8bae1dSRodney W. Grimes 		if ((ap->a_mode & FWRITE) && (tvp->v_type == VREG)) {
614996c772fSJohn Dyson 			error = union_copyup(un, (mode&O_TRUNC) == 0, cred, p);
615df8bae1dSRodney W. Grimes 			if (error == 0)
616df8bae1dSRodney W. Grimes 				error = VOP_OPEN(un->un_uppervp, mode, cred, p);
617df8bae1dSRodney W. Grimes 			return (error);
618df8bae1dSRodney W. Grimes 		}
619df8bae1dSRodney W. Grimes 
620df8bae1dSRodney W. Grimes 		/*
621df8bae1dSRodney W. Grimes 		 * Just open the lower vnode
622df8bae1dSRodney W. Grimes 		 */
623df8bae1dSRodney W. Grimes 		un->un_openl++;
624996c772fSJohn Dyson 		vn_lock(tvp, LK_EXCLUSIVE | LK_RETRY, p);
625df8bae1dSRodney W. Grimes 		error = VOP_OPEN(tvp, mode, cred, p);
626996c772fSJohn Dyson 		VOP_UNLOCK(tvp, 0, p);
627df8bae1dSRodney W. Grimes 
628df8bae1dSRodney W. Grimes 		return (error);
629df8bae1dSRodney W. Grimes 	}
630df8bae1dSRodney W. Grimes 
631996c772fSJohn Dyson 	FIXUP(un, p);
632df8bae1dSRodney W. Grimes 
633df8bae1dSRodney W. Grimes 	error = VOP_OPEN(tvp, mode, cred, p);
634df8bae1dSRodney W. Grimes 
635df8bae1dSRodney W. Grimes 	return (error);
636df8bae1dSRodney W. Grimes }
637df8bae1dSRodney W. Grimes 
638c9bf0111SKATO Takenori static int
639df8bae1dSRodney W. Grimes union_close(ap)
640df8bae1dSRodney W. Grimes 	struct vop_close_args /* {
641df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
642df8bae1dSRodney W. Grimes 		int  a_fflag;
643df8bae1dSRodney W. Grimes 		struct ucred *a_cred;
644df8bae1dSRodney W. Grimes 		struct proc *a_p;
645df8bae1dSRodney W. Grimes 	} */ *ap;
646df8bae1dSRodney W. Grimes {
647df8bae1dSRodney W. Grimes 	struct union_node *un = VTOUNION(ap->a_vp);
648df8bae1dSRodney W. Grimes 	struct vnode *vp;
649df8bae1dSRodney W. Grimes 
650996c772fSJohn Dyson 	if ((vp = un->un_uppervp) == NULLVP) {
651df8bae1dSRodney W. Grimes #ifdef UNION_DIAGNOSTIC
652df8bae1dSRodney W. Grimes 		if (un->un_openl <= 0)
653df8bae1dSRodney W. Grimes 			panic("union: un_openl cnt");
654df8bae1dSRodney W. Grimes #endif
655df8bae1dSRodney W. Grimes 		--un->un_openl;
656df8bae1dSRodney W. Grimes 		vp = un->un_lowervp;
657df8bae1dSRodney W. Grimes 	}
658df8bae1dSRodney W. Grimes 
659996c772fSJohn Dyson 	ap->a_vp = vp;
660996c772fSJohn Dyson 	return (VCALL(vp, VOFFSET(vop_close), ap));
661df8bae1dSRodney W. Grimes }
662df8bae1dSRodney W. Grimes 
663df8bae1dSRodney W. Grimes /*
664df8bae1dSRodney W. Grimes  * Check access permission on the union vnode.
665df8bae1dSRodney W. Grimes  * The access check being enforced is to check
666df8bae1dSRodney W. Grimes  * against both the underlying vnode, and any
667df8bae1dSRodney W. Grimes  * copied vnode.  This ensures that no additional
668df8bae1dSRodney W. Grimes  * file permissions are given away simply because
669df8bae1dSRodney W. Grimes  * the user caused an implicit file copy.
670df8bae1dSRodney W. Grimes  */
671c9bf0111SKATO Takenori static int
672df8bae1dSRodney W. Grimes union_access(ap)
673df8bae1dSRodney W. Grimes 	struct vop_access_args /* {
674df8bae1dSRodney W. Grimes 		struct vnodeop_desc *a_desc;
675df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
676df8bae1dSRodney W. Grimes 		int a_mode;
677df8bae1dSRodney W. Grimes 		struct ucred *a_cred;
678df8bae1dSRodney W. Grimes 		struct proc *a_p;
679df8bae1dSRodney W. Grimes 	} */ *ap;
680df8bae1dSRodney W. Grimes {
681df8bae1dSRodney W. Grimes 	struct union_node *un = VTOUNION(ap->a_vp);
682996c772fSJohn Dyson 	struct proc *p = ap->a_p;
683df8bae1dSRodney W. Grimes 	int error = EACCES;
684df8bae1dSRodney W. Grimes 	struct vnode *vp;
6855f3f68a8SKATO Takenori 	struct vnode *savedvp;
686df8bae1dSRodney W. Grimes 
6876ca02614SKATO Takenori 	/*
6886ca02614SKATO Takenori 	 * Disallow write attempts on filesystems mounted read-only.
6896ca02614SKATO Takenori 	 */
6906ca02614SKATO Takenori 	if (ap->a_mode & VWRITE && (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)) {
6916ca02614SKATO Takenori 		switch (ap->a_vp->v_type) {
6926ca02614SKATO Takenori 		case VREG: case VDIR: case VLNK:
6936ca02614SKATO Takenori 			return (EROFS);
6946ca02614SKATO Takenori 		}
6956ca02614SKATO Takenori 	}
696996c772fSJohn Dyson 	if ((vp = un->un_uppervp) != NULLVP) {
697996c772fSJohn Dyson 		FIXUP(un, p);
698996c772fSJohn Dyson 		ap->a_vp = vp;
699996c772fSJohn Dyson 		return (VCALL(vp, VOFFSET(vop_access), ap));
700df8bae1dSRodney W. Grimes 	}
701df8bae1dSRodney W. Grimes 
702996c772fSJohn Dyson 	if ((vp = un->un_lowervp) != NULLVP) {
703996c772fSJohn Dyson 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
7045f3f68a8SKATO Takenori 		savedvp = ap->a_vp;
705996c772fSJohn Dyson 		ap->a_vp = vp;
706996c772fSJohn Dyson 		error = VCALL(vp, VOFFSET(vop_access), ap);
707df8bae1dSRodney W. Grimes 		if (error == 0) {
7085f3f68a8SKATO Takenori 			struct union_mount *um = MOUNTTOUNIONMOUNT(savedvp->v_mount);
709df8bae1dSRodney W. Grimes 
710996c772fSJohn Dyson 			if (um->um_op == UNMNT_BELOW) {
711996c772fSJohn Dyson 				ap->a_cred = um->um_cred;
712996c772fSJohn Dyson 				error = VCALL(vp, VOFFSET(vop_access), ap);
713df8bae1dSRodney W. Grimes 			}
714996c772fSJohn Dyson 		}
715996c772fSJohn Dyson 		VOP_UNLOCK(vp, 0, p);
716df8bae1dSRodney W. Grimes 		if (error)
717df8bae1dSRodney W. Grimes 			return (error);
718df8bae1dSRodney W. Grimes 	}
719df8bae1dSRodney W. Grimes 
720df8bae1dSRodney W. Grimes 	return (error);
721df8bae1dSRodney W. Grimes }
722df8bae1dSRodney W. Grimes 
723df8bae1dSRodney W. Grimes /*
724996c772fSJohn Dyson  * We handle getattr only to change the fsid and
725996c772fSJohn Dyson  * track object sizes
726df8bae1dSRodney W. Grimes  */
727c9bf0111SKATO Takenori static int
728df8bae1dSRodney W. Grimes union_getattr(ap)
729df8bae1dSRodney W. Grimes 	struct vop_getattr_args /* {
730df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
731df8bae1dSRodney W. Grimes 		struct vattr *a_vap;
732df8bae1dSRodney W. Grimes 		struct ucred *a_cred;
733df8bae1dSRodney W. Grimes 		struct proc *a_p;
734df8bae1dSRodney W. Grimes 	} */ *ap;
735df8bae1dSRodney W. Grimes {
736df8bae1dSRodney W. Grimes 	int error;
737df8bae1dSRodney W. Grimes 	struct union_node *un = VTOUNION(ap->a_vp);
738df8bae1dSRodney W. Grimes 	struct vnode *vp = un->un_uppervp;
739996c772fSJohn Dyson 	struct proc *p = ap->a_p;
740df8bae1dSRodney W. Grimes 	struct vattr *vap;
741df8bae1dSRodney W. Grimes 	struct vattr va;
742df8bae1dSRodney W. Grimes 
743df8bae1dSRodney W. Grimes 
744df8bae1dSRodney W. Grimes 	/*
745df8bae1dSRodney W. Grimes 	 * Some programs walk the filesystem hierarchy by counting
746df8bae1dSRodney W. Grimes 	 * links to directories to avoid stat'ing all the time.
747df8bae1dSRodney W. Grimes 	 * This means the link count on directories needs to be "correct".
748df8bae1dSRodney W. Grimes 	 * The only way to do that is to call getattr on both layers
749df8bae1dSRodney W. Grimes 	 * and fix up the link count.  The link count will not necessarily
750df8bae1dSRodney W. Grimes 	 * be accurate but will be large enough to defeat the tree walkers.
751df8bae1dSRodney W. Grimes 	 */
752df8bae1dSRodney W. Grimes 
753df8bae1dSRodney W. Grimes 	vap = ap->a_vap;
754df8bae1dSRodney W. Grimes 
755df8bae1dSRodney W. Grimes 	vp = un->un_uppervp;
756df8bae1dSRodney W. Grimes 	if (vp != NULLVP) {
757996c772fSJohn Dyson 		/*
758996c772fSJohn Dyson 		 * It's not clear whether VOP_GETATTR is to be
759996c772fSJohn Dyson 		 * called with the vnode locked or not.  stat() calls
760996c772fSJohn Dyson 		 * it with (vp) locked, and fstat calls it with
761996c772fSJohn Dyson 		 * (vp) unlocked.
762996c772fSJohn Dyson 		 * In the mean time, compensate here by checking
763996c772fSJohn Dyson 		 * the union_node's lock flag.
764996c772fSJohn Dyson 		 */
765996c772fSJohn Dyson 		if (un->un_flags & UN_LOCKED)
766996c772fSJohn Dyson 			FIXUP(un, p);
767996c772fSJohn Dyson 
768df8bae1dSRodney W. Grimes 		error = VOP_GETATTR(vp, vap, ap->a_cred, ap->a_p);
769df8bae1dSRodney W. Grimes 		if (error)
770df8bae1dSRodney W. Grimes 			return (error);
771996c772fSJohn Dyson 		union_newsize(ap->a_vp, vap->va_size, VNOVAL);
772df8bae1dSRodney W. Grimes 	}
773df8bae1dSRodney W. Grimes 
774df8bae1dSRodney W. Grimes 	if (vp == NULLVP) {
775df8bae1dSRodney W. Grimes 		vp = un->un_lowervp;
776df8bae1dSRodney W. Grimes 	} else if (vp->v_type == VDIR) {
777df8bae1dSRodney W. Grimes 		vp = un->un_lowervp;
778df8bae1dSRodney W. Grimes 		vap = &va;
779df8bae1dSRodney W. Grimes 	} else {
780df8bae1dSRodney W. Grimes 		vp = NULLVP;
781df8bae1dSRodney W. Grimes 	}
782df8bae1dSRodney W. Grimes 
783df8bae1dSRodney W. Grimes 	if (vp != NULLVP) {
784df8bae1dSRodney W. Grimes 		error = VOP_GETATTR(vp, vap, ap->a_cred, ap->a_p);
785df8bae1dSRodney W. Grimes 		if (error)
786df8bae1dSRodney W. Grimes 			return (error);
787996c772fSJohn Dyson 		union_newsize(ap->a_vp, VNOVAL, vap->va_size);
788df8bae1dSRodney W. Grimes 	}
789df8bae1dSRodney W. Grimes 
790df8bae1dSRodney W. Grimes 	if ((vap != ap->a_vap) && (vap->va_type == VDIR))
791df8bae1dSRodney W. Grimes 		ap->a_vap->va_nlink += vap->va_nlink;
792df8bae1dSRodney W. Grimes 
793996c772fSJohn Dyson 	ap->a_vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsid.val[0];
794df8bae1dSRodney W. Grimes 	return (0);
795df8bae1dSRodney W. Grimes }
796df8bae1dSRodney W. Grimes 
797c9bf0111SKATO Takenori static int
798df8bae1dSRodney W. Grimes union_setattr(ap)
799df8bae1dSRodney W. Grimes 	struct vop_setattr_args /* {
800df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
801df8bae1dSRodney W. Grimes 		struct vattr *a_vap;
802df8bae1dSRodney W. Grimes 		struct ucred *a_cred;
803df8bae1dSRodney W. Grimes 		struct proc *a_p;
804df8bae1dSRodney W. Grimes 	} */ *ap;
805df8bae1dSRodney W. Grimes {
806df8bae1dSRodney W. Grimes 	struct union_node *un = VTOUNION(ap->a_vp);
807996c772fSJohn Dyson 	struct proc *p = ap->a_p;
8086ca02614SKATO Takenori 	struct vattr *vap = ap->a_vap;
809df8bae1dSRodney W. Grimes 	int error;
810df8bae1dSRodney W. Grimes 
811df8bae1dSRodney W. Grimes 	/*
8126ca02614SKATO Takenori 	 * Disallow write attempts on filesystems mounted read-only.
8136ca02614SKATO Takenori 	 */
8146ca02614SKATO Takenori 	if ((ap->a_vp->v_mount->mnt_flag & MNT_RDONLY) &&
8156ca02614SKATO Takenori 		(vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
8166ca02614SKATO Takenori 		 vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
8176ca02614SKATO Takenori 		 vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL))
8186ca02614SKATO Takenori 		return (EROFS);
8196ca02614SKATO Takenori 
8206ca02614SKATO Takenori 	/*
821df8bae1dSRodney W. Grimes 	 * Handle case of truncating lower object to zero size,
822df8bae1dSRodney W. Grimes 	 * by creating a zero length upper object.  This is to
823df8bae1dSRodney W. Grimes 	 * handle the case of open with O_TRUNC and O_CREAT.
824df8bae1dSRodney W. Grimes 	 */
825df8bae1dSRodney W. Grimes 	if ((un->un_uppervp == NULLVP) &&
826df8bae1dSRodney W. Grimes 	    /* assert(un->un_lowervp != NULLVP) */
827996c772fSJohn Dyson 	    (un->un_lowervp->v_type == VREG)) {
828996c772fSJohn Dyson 		error = union_copyup(un, (ap->a_vap->va_size != 0),
829996c772fSJohn Dyson 						ap->a_cred, ap->a_p);
830df8bae1dSRodney W. Grimes 		if (error)
831df8bae1dSRodney W. Grimes 			return (error);
832df8bae1dSRodney W. Grimes 	}
833df8bae1dSRodney W. Grimes 
834df8bae1dSRodney W. Grimes 	/*
835df8bae1dSRodney W. Grimes 	 * Try to set attributes in upper layer,
836df8bae1dSRodney W. Grimes 	 * otherwise return read-only filesystem error.
837df8bae1dSRodney W. Grimes 	 */
838df8bae1dSRodney W. Grimes 	if (un->un_uppervp != NULLVP) {
839996c772fSJohn Dyson 		FIXUP(un, p);
840df8bae1dSRodney W. Grimes 		error = VOP_SETATTR(un->un_uppervp, ap->a_vap,
841df8bae1dSRodney W. Grimes 					ap->a_cred, ap->a_p);
842996c772fSJohn Dyson 		if ((error == 0) && (ap->a_vap->va_size != VNOVAL))
843996c772fSJohn Dyson 			union_newsize(ap->a_vp, ap->a_vap->va_size, VNOVAL);
844df8bae1dSRodney W. Grimes 	} else {
845df8bae1dSRodney W. Grimes 		error = EROFS;
846df8bae1dSRodney W. Grimes 	}
847df8bae1dSRodney W. Grimes 
848df8bae1dSRodney W. Grimes 	return (error);
849df8bae1dSRodney W. Grimes }
850df8bae1dSRodney W. Grimes 
851c9bf0111SKATO Takenori static int
852df8bae1dSRodney W. Grimes union_read(ap)
853df8bae1dSRodney W. Grimes 	struct vop_read_args /* {
854df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
855df8bae1dSRodney W. Grimes 		struct uio *a_uio;
856df8bae1dSRodney W. Grimes 		int  a_ioflag;
857df8bae1dSRodney W. Grimes 		struct ucred *a_cred;
858df8bae1dSRodney W. Grimes 	} */ *ap;
859df8bae1dSRodney W. Grimes {
860df8bae1dSRodney W. Grimes 	int error;
861996c772fSJohn Dyson 	struct proc *p = ap->a_uio->uio_procp;
862df8bae1dSRodney W. Grimes 	struct vnode *vp = OTHERVP(ap->a_vp);
863df8bae1dSRodney W. Grimes 	int dolock = (vp == LOWERVP(ap->a_vp));
864df8bae1dSRodney W. Grimes 
865df8bae1dSRodney W. Grimes 	if (dolock)
866996c772fSJohn Dyson 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
867df8bae1dSRodney W. Grimes 	else
868996c772fSJohn Dyson 		FIXUP(VTOUNION(ap->a_vp), p);
869df8bae1dSRodney W. Grimes 	error = VOP_READ(vp, ap->a_uio, ap->a_ioflag, ap->a_cred);
870df8bae1dSRodney W. Grimes 	if (dolock)
871996c772fSJohn Dyson 		VOP_UNLOCK(vp, 0, p);
872996c772fSJohn Dyson 
873996c772fSJohn Dyson 	/*
874996c772fSJohn Dyson 	 * XXX
875996c772fSJohn Dyson 	 * perhaps the size of the underlying object has changed under
876996c772fSJohn Dyson 	 * our feet.  take advantage of the offset information present
877996c772fSJohn Dyson 	 * in the uio structure.
878996c772fSJohn Dyson 	 */
879996c772fSJohn Dyson 	if (error == 0) {
880996c772fSJohn Dyson 		struct union_node *un = VTOUNION(ap->a_vp);
881996c772fSJohn Dyson 		off_t cur = ap->a_uio->uio_offset;
882996c772fSJohn Dyson 
883996c772fSJohn Dyson 		if (vp == un->un_uppervp) {
884996c772fSJohn Dyson 			if (cur > un->un_uppersz)
885996c772fSJohn Dyson 				union_newsize(ap->a_vp, cur, VNOVAL);
886996c772fSJohn Dyson 		} else {
887996c772fSJohn Dyson 			if (cur > un->un_lowersz)
888996c772fSJohn Dyson 				union_newsize(ap->a_vp, VNOVAL, cur);
889996c772fSJohn Dyson 		}
890996c772fSJohn Dyson 	}
891df8bae1dSRodney W. Grimes 
892df8bae1dSRodney W. Grimes 	return (error);
893df8bae1dSRodney W. Grimes }
894df8bae1dSRodney W. Grimes 
895c9bf0111SKATO Takenori static int
896df8bae1dSRodney W. Grimes union_write(ap)
897df8bae1dSRodney W. Grimes 	struct vop_read_args /* {
898df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
899df8bae1dSRodney W. Grimes 		struct uio *a_uio;
900df8bae1dSRodney W. Grimes 		int  a_ioflag;
901df8bae1dSRodney W. Grimes 		struct ucred *a_cred;
902df8bae1dSRodney W. Grimes 	} */ *ap;
903df8bae1dSRodney W. Grimes {
904df8bae1dSRodney W. Grimes 	int error;
905996c772fSJohn Dyson 	struct vnode *vp;
906996c772fSJohn Dyson 	struct union_node *un = VTOUNION(ap->a_vp);
907996c772fSJohn Dyson 	struct proc *p = ap->a_uio->uio_procp;
908df8bae1dSRodney W. Grimes 
909996c772fSJohn Dyson 	vp = UPPERVP(ap->a_vp);
910996c772fSJohn Dyson 	if (vp == NULLVP)
911996c772fSJohn Dyson 		panic("union: missing upper layer in write");
912996c772fSJohn Dyson 
913996c772fSJohn Dyson 	FIXUP(un, p);
914df8bae1dSRodney W. Grimes 	error = VOP_WRITE(vp, ap->a_uio, ap->a_ioflag, ap->a_cred);
915996c772fSJohn Dyson 
916996c772fSJohn Dyson 	/*
917996c772fSJohn Dyson 	 * the size of the underlying object may be changed by the
918996c772fSJohn Dyson 	 * write.
919996c772fSJohn Dyson 	 */
920996c772fSJohn Dyson 	if (error == 0) {
921996c772fSJohn Dyson 		off_t cur = ap->a_uio->uio_offset;
922996c772fSJohn Dyson 
923996c772fSJohn Dyson 		if (cur > un->un_uppersz)
924996c772fSJohn Dyson 			union_newsize(ap->a_vp, cur, VNOVAL);
925996c772fSJohn Dyson 	}
926df8bae1dSRodney W. Grimes 
927df8bae1dSRodney W. Grimes 	return (error);
928df8bae1dSRodney W. Grimes }
929df8bae1dSRodney W. Grimes 
930c9bf0111SKATO Takenori static int
931996c772fSJohn Dyson union_lease(ap)
932996c772fSJohn Dyson 	struct vop_lease_args /* {
933996c772fSJohn Dyson 		struct vnode *a_vp;
934996c772fSJohn Dyson 		struct proc *a_p;
935996c772fSJohn Dyson 		struct ucred *a_cred;
936996c772fSJohn Dyson 		int a_flag;
937996c772fSJohn Dyson 	} */ *ap;
938996c772fSJohn Dyson {
939996c772fSJohn Dyson 	register struct vnode *ovp = OTHERVP(ap->a_vp);
940996c772fSJohn Dyson 
941996c772fSJohn Dyson 	ap->a_vp = ovp;
942996c772fSJohn Dyson 	return (VCALL(ovp, VOFFSET(vop_lease), ap));
943996c772fSJohn Dyson }
944996c772fSJohn Dyson 
945c9bf0111SKATO Takenori static int
946df8bae1dSRodney W. Grimes union_ioctl(ap)
947df8bae1dSRodney W. Grimes 	struct vop_ioctl_args /* {
948df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
949df8bae1dSRodney W. Grimes 		int  a_command;
950df8bae1dSRodney W. Grimes 		caddr_t  a_data;
951df8bae1dSRodney W. Grimes 		int  a_fflag;
952df8bae1dSRodney W. Grimes 		struct ucred *a_cred;
953df8bae1dSRodney W. Grimes 		struct proc *a_p;
954df8bae1dSRodney W. Grimes 	} */ *ap;
955df8bae1dSRodney W. Grimes {
956996c772fSJohn Dyson 	register struct vnode *ovp = OTHERVP(ap->a_vp);
957df8bae1dSRodney W. Grimes 
958996c772fSJohn Dyson 	ap->a_vp = ovp;
959996c772fSJohn Dyson 	return (VCALL(ovp, VOFFSET(vop_ioctl), ap));
960df8bae1dSRodney W. Grimes }
961df8bae1dSRodney W. Grimes 
962c9bf0111SKATO Takenori static int
963a6aeade2SPeter Wemm union_poll(ap)
964a6aeade2SPeter Wemm 	struct vop_poll_args /* {
965df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
966a6aeade2SPeter Wemm 		int  a_events;
967df8bae1dSRodney W. Grimes 		struct ucred *a_cred;
968df8bae1dSRodney W. Grimes 		struct proc *a_p;
969df8bae1dSRodney W. Grimes 	} */ *ap;
970df8bae1dSRodney W. Grimes {
971996c772fSJohn Dyson 	register struct vnode *ovp = OTHERVP(ap->a_vp);
972df8bae1dSRodney W. Grimes 
973996c772fSJohn Dyson 	ap->a_vp = ovp;
974a6aeade2SPeter Wemm 	return (VCALL(ovp, VOFFSET(vop_poll), ap));
975996c772fSJohn Dyson }
976996c772fSJohn Dyson 
977c9bf0111SKATO Takenori static int
978996c772fSJohn Dyson union_revoke(ap)
979996c772fSJohn Dyson 	struct vop_revoke_args /* {
980996c772fSJohn Dyson 		struct vnode *a_vp;
981996c772fSJohn Dyson 		int a_flags;
982996c772fSJohn Dyson 		struct proc *a_p;
983996c772fSJohn Dyson 	} */ *ap;
984996c772fSJohn Dyson {
985996c772fSJohn Dyson 	struct vnode *vp = ap->a_vp;
986996c772fSJohn Dyson 
987996c772fSJohn Dyson 	if (UPPERVP(vp))
988996c772fSJohn Dyson 		VOP_REVOKE(UPPERVP(vp), ap->a_flags);
989996c772fSJohn Dyson 	if (LOWERVP(vp))
990996c772fSJohn Dyson 		VOP_REVOKE(LOWERVP(vp), ap->a_flags);
991996c772fSJohn Dyson 	vgone(vp);
992996c772fSJohn Dyson 	return (0);
993df8bae1dSRodney W. Grimes }
994df8bae1dSRodney W. Grimes 
995c9bf0111SKATO Takenori static int
996df8bae1dSRodney W. Grimes union_mmap(ap)
997df8bae1dSRodney W. Grimes 	struct vop_mmap_args /* {
998df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
999df8bae1dSRodney W. Grimes 		int  a_fflags;
1000df8bae1dSRodney W. Grimes 		struct ucred *a_cred;
1001df8bae1dSRodney W. Grimes 		struct proc *a_p;
1002df8bae1dSRodney W. Grimes 	} */ *ap;
1003df8bae1dSRodney W. Grimes {
1004996c772fSJohn Dyson 	register struct vnode *ovp = OTHERVP(ap->a_vp);
1005df8bae1dSRodney W. Grimes 
1006996c772fSJohn Dyson 	ap->a_vp = ovp;
1007996c772fSJohn Dyson 	return (VCALL(ovp, VOFFSET(vop_mmap), ap));
1008df8bae1dSRodney W. Grimes }
1009df8bae1dSRodney W. Grimes 
1010c9bf0111SKATO Takenori static int
1011df8bae1dSRodney W. Grimes union_fsync(ap)
1012df8bae1dSRodney W. Grimes 	struct vop_fsync_args /* {
1013df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
1014df8bae1dSRodney W. Grimes 		struct ucred *a_cred;
1015df8bae1dSRodney W. Grimes 		int  a_waitfor;
1016df8bae1dSRodney W. Grimes 		struct proc *a_p;
1017df8bae1dSRodney W. Grimes 	} */ *ap;
1018df8bae1dSRodney W. Grimes {
1019df8bae1dSRodney W. Grimes 	int error = 0;
1020996c772fSJohn Dyson 	struct proc *p = ap->a_p;
1021df8bae1dSRodney W. Grimes 	struct vnode *targetvp = OTHERVP(ap->a_vp);
1022363ad700SPoul-Henning Kamp 	struct union_node *un;
1023df8bae1dSRodney W. Grimes 
1024996c772fSJohn Dyson 	if (targetvp != NULLVP) {
1025df8bae1dSRodney W. Grimes 		int dolock = (targetvp == LOWERVP(ap->a_vp));
1026df8bae1dSRodney W. Grimes 
1027363ad700SPoul-Henning Kamp 		un = VTOUNION(ap->a_vp);
1028df8bae1dSRodney W. Grimes 		if (dolock)
1029996c772fSJohn Dyson 			vn_lock(targetvp, LK_EXCLUSIVE | LK_RETRY, p);
1030afc2a558SKATO Takenori 		else  {
1031afc2a558SKATO Takenori 			un = VTOUNION(ap->a_vp);
1032afc2a558SKATO Takenori 			if ((un->un_flags & UN_ULOCK) == 0 &&
1033afc2a558SKATO Takenori 			    targetvp->v_data != NULL &&
1034afc2a558SKATO Takenori 			    ((struct lock *)targetvp->v_data)->lk_lockholder
1035afc2a558SKATO Takenori 			        == curproc->p_pid &&
1036afc2a558SKATO Takenori 			    VOP_ISLOCKED(targetvp) != 0)
1037afc2a558SKATO Takenori 				return 0;   /* XXX */
1038afc2a558SKATO Takenori 
1039afc2a558SKATO Takenori 			FIXUP(un, p);
1040363ad700SPoul-Henning Kamp 		}
1041afc2a558SKATO Takenori 
1042996c772fSJohn Dyson 		error = VOP_FSYNC(targetvp, ap->a_cred, ap->a_waitfor, p);
1043df8bae1dSRodney W. Grimes 		if (dolock)
1044996c772fSJohn Dyson 			VOP_UNLOCK(targetvp, 0, p);
1045df8bae1dSRodney W. Grimes 	}
1046df8bae1dSRodney W. Grimes 
1047df8bae1dSRodney W. Grimes 	return (error);
1048df8bae1dSRodney W. Grimes }
1049df8bae1dSRodney W. Grimes 
1050c9bf0111SKATO Takenori static int
1051df8bae1dSRodney W. Grimes union_remove(ap)
1052df8bae1dSRodney W. Grimes 	struct vop_remove_args /* {
1053df8bae1dSRodney W. Grimes 		struct vnode *a_dvp;
1054df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
1055df8bae1dSRodney W. Grimes 		struct componentname *a_cnp;
1056df8bae1dSRodney W. Grimes 	} */ *ap;
1057df8bae1dSRodney W. Grimes {
1058df8bae1dSRodney W. Grimes 	int error;
1059df8bae1dSRodney W. Grimes 	struct union_node *dun = VTOUNION(ap->a_dvp);
1060df8bae1dSRodney W. Grimes 	struct union_node *un = VTOUNION(ap->a_vp);
1061996c772fSJohn Dyson 	struct componentname *cnp = ap->a_cnp;
1062996c772fSJohn Dyson 	struct proc *p = cnp->cn_proc;
1063df8bae1dSRodney W. Grimes 
1064996c772fSJohn Dyson 	if (dun->un_uppervp == NULLVP)
1065996c772fSJohn Dyson 		panic("union remove: null upper vnode");
1066996c772fSJohn Dyson 
1067996c772fSJohn Dyson 	if (un->un_uppervp != NULLVP) {
1068df8bae1dSRodney W. Grimes 		struct vnode *dvp = dun->un_uppervp;
1069df8bae1dSRodney W. Grimes 		struct vnode *vp = un->un_uppervp;
1070df8bae1dSRodney W. Grimes 
1071996c772fSJohn Dyson 		FIXUP(dun, p);
1072df8bae1dSRodney W. Grimes 		VREF(dvp);
1073d6476adaSKATO Takenori 		dun->un_flags |= UN_KLOCK;
1074df8bae1dSRodney W. Grimes 		vput(ap->a_dvp);
1075996c772fSJohn Dyson 		FIXUP(un, p);
1076df8bae1dSRodney W. Grimes 		VREF(vp);
1077d6476adaSKATO Takenori 		un->un_flags |= UN_KLOCK;
1078df8bae1dSRodney W. Grimes 		vput(ap->a_vp);
1079df8bae1dSRodney W. Grimes 
1080996c772fSJohn Dyson 		if (union_dowhiteout(un, cnp->cn_cred, cnp->cn_proc))
1081996c772fSJohn Dyson 			cnp->cn_flags |= DOWHITEOUT;
1082996c772fSJohn Dyson 		error = VOP_REMOVE(dvp, vp, cnp);
108387884aadSKATO Takenori #if 0
108487884aadSKATO Takenori 		/* XXX */
1085df8bae1dSRodney W. Grimes 		if (!error)
1086df8bae1dSRodney W. Grimes 			union_removed_upper(un);
108787884aadSKATO Takenori #endif
1088df8bae1dSRodney W. Grimes 	} else {
1089996c772fSJohn Dyson 		FIXUP(dun, p);
1090996c772fSJohn Dyson 		error = union_mkwhiteout(
1091996c772fSJohn Dyson 			MOUNTTOUNIONMOUNT(UNIONTOV(dun)->v_mount),
1092996c772fSJohn Dyson 			dun->un_uppervp, ap->a_cnp, un->un_path);
1093df8bae1dSRodney W. Grimes 		vput(ap->a_dvp);
1094df8bae1dSRodney W. Grimes 		vput(ap->a_vp);
1095df8bae1dSRodney W. Grimes 	}
1096df8bae1dSRodney W. Grimes 
1097df8bae1dSRodney W. Grimes 	return (error);
1098df8bae1dSRodney W. Grimes }
1099df8bae1dSRodney W. Grimes 
1100c9bf0111SKATO Takenori static int
1101df8bae1dSRodney W. Grimes union_link(ap)
1102df8bae1dSRodney W. Grimes 	struct vop_link_args /* {
1103df8bae1dSRodney W. Grimes 		struct vnode *a_tdvp;
110447777413SDavid Greenman 		struct vnode *a_vp;
1105df8bae1dSRodney W. Grimes 		struct componentname *a_cnp;
1106df8bae1dSRodney W. Grimes 	} */ *ap;
1107df8bae1dSRodney W. Grimes {
1108996c772fSJohn Dyson 	int error = 0;
1109996c772fSJohn Dyson 	struct componentname *cnp = ap->a_cnp;
1110996c772fSJohn Dyson 	struct proc *p = cnp->cn_proc;
1111996c772fSJohn Dyson 	struct union_node *un;
1112996c772fSJohn Dyson 	struct vnode *vp;
1113996c772fSJohn Dyson 	struct vnode *tdvp;
1114df8bae1dSRodney W. Grimes 
1115996c772fSJohn Dyson 	un = VTOUNION(ap->a_tdvp);
1116df8bae1dSRodney W. Grimes 
1117996c772fSJohn Dyson 	if (ap->a_tdvp->v_op != ap->a_vp->v_op) {
1118996c772fSJohn Dyson 		vp = ap->a_vp;
1119df8bae1dSRodney W. Grimes 	} else {
1120996c772fSJohn Dyson 		struct union_node *tun = VTOUNION(ap->a_vp);
1121996c772fSJohn Dyson 		if (tun->un_uppervp == NULLVP) {
1122996c772fSJohn Dyson 			vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY, p);
1123996c772fSJohn Dyson 			if (un->un_uppervp == tun->un_dirvp) {
1124996c772fSJohn Dyson 				un->un_flags &= ~UN_ULOCK;
1125996c772fSJohn Dyson 				VOP_UNLOCK(un->un_uppervp, 0, p);
1126996c772fSJohn Dyson 			}
1127996c772fSJohn Dyson 			error = union_copyup(tun, 1, cnp->cn_cred, p);
1128996c772fSJohn Dyson 			if (un->un_uppervp == tun->un_dirvp) {
1129996c772fSJohn Dyson 				vn_lock(un->un_uppervp,
1130996c772fSJohn Dyson 						LK_EXCLUSIVE | LK_RETRY, p);
1131996c772fSJohn Dyson 				un->un_flags |= UN_ULOCK;
1132996c772fSJohn Dyson 			}
1133996c772fSJohn Dyson 			VOP_UNLOCK(ap->a_vp, 0, p);
1134996c772fSJohn Dyson 		}
1135996c772fSJohn Dyson 		vp = tun->un_uppervp;
1136df8bae1dSRodney W. Grimes 	}
1137df8bae1dSRodney W. Grimes 
1138996c772fSJohn Dyson 	tdvp = un->un_uppervp;
1139996c772fSJohn Dyson 	if (tdvp == NULLVP)
1140996c772fSJohn Dyson 		error = EROFS;
1141996c772fSJohn Dyson 
1142996c772fSJohn Dyson 	if (error) {
1143996c772fSJohn Dyson 		vput(ap->a_tdvp);
1144df8bae1dSRodney W. Grimes 		return (error);
1145df8bae1dSRodney W. Grimes 	}
1146df8bae1dSRodney W. Grimes 
1147996c772fSJohn Dyson 	FIXUP(un, p);
1148996c772fSJohn Dyson 	VREF(tdvp);
1149d6476adaSKATO Takenori 	un->un_flags |= UN_KLOCK;
1150996c772fSJohn Dyson 	vput(ap->a_tdvp);
1151996c772fSJohn Dyson 
115293d71a48SKATO Takenori 	return (VOP_LINK(tdvp, vp, cnp));
1153996c772fSJohn Dyson }
1154996c772fSJohn Dyson 
1155c9bf0111SKATO Takenori static int
1156df8bae1dSRodney W. Grimes union_rename(ap)
1157df8bae1dSRodney W. Grimes 	struct vop_rename_args  /* {
1158df8bae1dSRodney W. Grimes 		struct vnode *a_fdvp;
1159df8bae1dSRodney W. Grimes 		struct vnode *a_fvp;
1160df8bae1dSRodney W. Grimes 		struct componentname *a_fcnp;
1161df8bae1dSRodney W. Grimes 		struct vnode *a_tdvp;
1162df8bae1dSRodney W. Grimes 		struct vnode *a_tvp;
1163df8bae1dSRodney W. Grimes 		struct componentname *a_tcnp;
1164df8bae1dSRodney W. Grimes 	} */ *ap;
1165df8bae1dSRodney W. Grimes {
1166df8bae1dSRodney W. Grimes 	int error;
1167df8bae1dSRodney W. Grimes 
1168df8bae1dSRodney W. Grimes 	struct vnode *fdvp = ap->a_fdvp;
1169df8bae1dSRodney W. Grimes 	struct vnode *fvp = ap->a_fvp;
1170df8bae1dSRodney W. Grimes 	struct vnode *tdvp = ap->a_tdvp;
1171df8bae1dSRodney W. Grimes 	struct vnode *tvp = ap->a_tvp;
1172df8bae1dSRodney W. Grimes 
1173df8bae1dSRodney W. Grimes 	if (fdvp->v_op == union_vnodeop_p) {	/* always true */
1174df8bae1dSRodney W. Grimes 		struct union_node *un = VTOUNION(fdvp);
1175df8bae1dSRodney W. Grimes 		if (un->un_uppervp == NULLVP) {
1176996c772fSJohn Dyson 			/*
1177996c772fSJohn Dyson 			 * this should never happen in normal
1178996c772fSJohn Dyson 			 * operation but might if there was
1179996c772fSJohn Dyson 			 * a problem creating the top-level shadow
1180996c772fSJohn Dyson 			 * directory.
1181996c772fSJohn Dyson 			 */
1182996c772fSJohn Dyson 			error = EXDEV;
1183df8bae1dSRodney W. Grimes 			goto bad;
1184df8bae1dSRodney W. Grimes 		}
1185df8bae1dSRodney W. Grimes 
1186df8bae1dSRodney W. Grimes 		fdvp = un->un_uppervp;
1187df8bae1dSRodney W. Grimes 		VREF(fdvp);
1188df8bae1dSRodney W. Grimes 		vrele(ap->a_fdvp);
1189df8bae1dSRodney W. Grimes 	}
1190df8bae1dSRodney W. Grimes 
1191df8bae1dSRodney W. Grimes 	if (fvp->v_op == union_vnodeop_p) {	/* always true */
1192df8bae1dSRodney W. Grimes 		struct union_node *un = VTOUNION(fvp);
1193df8bae1dSRodney W. Grimes 		if (un->un_uppervp == NULLVP) {
1194996c772fSJohn Dyson 			/* XXX: should do a copyup */
1195996c772fSJohn Dyson 			error = EXDEV;
1196df8bae1dSRodney W. Grimes 			goto bad;
1197df8bae1dSRodney W. Grimes 		}
1198df8bae1dSRodney W. Grimes 
1199996c772fSJohn Dyson 		if (un->un_lowervp != NULLVP)
1200996c772fSJohn Dyson 			ap->a_fcnp->cn_flags |= DOWHITEOUT;
1201996c772fSJohn Dyson 
1202df8bae1dSRodney W. Grimes 		fvp = un->un_uppervp;
1203df8bae1dSRodney W. Grimes 		VREF(fvp);
1204df8bae1dSRodney W. Grimes 		vrele(ap->a_fvp);
1205df8bae1dSRodney W. Grimes 	}
1206df8bae1dSRodney W. Grimes 
1207df8bae1dSRodney W. Grimes 	if (tdvp->v_op == union_vnodeop_p) {
1208df8bae1dSRodney W. Grimes 		struct union_node *un = VTOUNION(tdvp);
1209df8bae1dSRodney W. Grimes 		if (un->un_uppervp == NULLVP) {
1210996c772fSJohn Dyson 			/*
1211996c772fSJohn Dyson 			 * this should never happen in normal
1212996c772fSJohn Dyson 			 * operation but might if there was
1213996c772fSJohn Dyson 			 * a problem creating the top-level shadow
1214996c772fSJohn Dyson 			 * directory.
1215996c772fSJohn Dyson 			 */
1216996c772fSJohn Dyson 			error = EXDEV;
1217df8bae1dSRodney W. Grimes 			goto bad;
1218df8bae1dSRodney W. Grimes 		}
1219df8bae1dSRodney W. Grimes 
1220df8bae1dSRodney W. Grimes 		tdvp = un->un_uppervp;
1221df8bae1dSRodney W. Grimes 		VREF(tdvp);
1222d6476adaSKATO Takenori 		un->un_flags |= UN_KLOCK;
1223df8bae1dSRodney W. Grimes 		vput(ap->a_tdvp);
1224df8bae1dSRodney W. Grimes 	}
1225df8bae1dSRodney W. Grimes 
1226996c772fSJohn Dyson 	if (tvp != NULLVP && tvp->v_op == union_vnodeop_p) {
1227df8bae1dSRodney W. Grimes 		struct union_node *un = VTOUNION(tvp);
1228df8bae1dSRodney W. Grimes 
1229df8bae1dSRodney W. Grimes 		tvp = un->un_uppervp;
1230996c772fSJohn Dyson 		if (tvp != NULLVP) {
1231df8bae1dSRodney W. Grimes 			VREF(tvp);
1232d6476adaSKATO Takenori 			un->un_flags |= UN_KLOCK;
1233996c772fSJohn Dyson 		}
1234df8bae1dSRodney W. Grimes 		vput(ap->a_tvp);
1235df8bae1dSRodney W. Grimes 	}
1236df8bae1dSRodney W. Grimes 
1237df8bae1dSRodney W. Grimes 	return (VOP_RENAME(fdvp, fvp, ap->a_fcnp, tdvp, tvp, ap->a_tcnp));
1238df8bae1dSRodney W. Grimes 
1239df8bae1dSRodney W. Grimes bad:
1240df8bae1dSRodney W. Grimes 	vrele(fdvp);
1241df8bae1dSRodney W. Grimes 	vrele(fvp);
1242df8bae1dSRodney W. Grimes 	vput(tdvp);
1243996c772fSJohn Dyson 	if (tvp != NULLVP)
1244df8bae1dSRodney W. Grimes 		vput(tvp);
1245df8bae1dSRodney W. Grimes 
1246df8bae1dSRodney W. Grimes 	return (error);
1247df8bae1dSRodney W. Grimes }
1248df8bae1dSRodney W. Grimes 
1249c9bf0111SKATO Takenori static int
1250df8bae1dSRodney W. Grimes union_mkdir(ap)
1251df8bae1dSRodney W. Grimes 	struct vop_mkdir_args /* {
1252df8bae1dSRodney W. Grimes 		struct vnode *a_dvp;
1253df8bae1dSRodney W. Grimes 		struct vnode **a_vpp;
1254df8bae1dSRodney W. Grimes 		struct componentname *a_cnp;
1255df8bae1dSRodney W. Grimes 		struct vattr *a_vap;
1256df8bae1dSRodney W. Grimes 	} */ *ap;
1257df8bae1dSRodney W. Grimes {
1258df8bae1dSRodney W. Grimes 	struct union_node *un = VTOUNION(ap->a_dvp);
1259df8bae1dSRodney W. Grimes 	struct vnode *dvp = un->un_uppervp;
1260996c772fSJohn Dyson 	struct componentname *cnp = ap->a_cnp;
1261996c772fSJohn Dyson 	struct proc *p = cnp->cn_proc;
1262df8bae1dSRodney W. Grimes 
1263996c772fSJohn Dyson 	if (dvp != NULLVP) {
1264df8bae1dSRodney W. Grimes 		int error;
1265df8bae1dSRodney W. Grimes 		struct vnode *vp;
1266df8bae1dSRodney W. Grimes 
1267996c772fSJohn Dyson 		FIXUP(un, p);
1268df8bae1dSRodney W. Grimes 		VREF(dvp);
1269d6476adaSKATO Takenori 		un->un_flags |= UN_KLOCK;
1270996c772fSJohn Dyson 		VOP_UNLOCK(ap->a_dvp, 0, p);
1271996c772fSJohn Dyson 		error = VOP_MKDIR(dvp, &vp, cnp, ap->a_vap);
1272996c772fSJohn Dyson 		if (error) {
1273996c772fSJohn Dyson 			vrele(ap->a_dvp);
1274df8bae1dSRodney W. Grimes 			return (error);
1275996c772fSJohn Dyson 		}
1276df8bae1dSRodney W. Grimes 
1277996c772fSJohn Dyson 		error = union_allocvp(ap->a_vpp, ap->a_dvp->v_mount, ap->a_dvp,
1278996c772fSJohn Dyson 				NULLVP, cnp, vp, NULLVP, 1);
1279996c772fSJohn Dyson 		vrele(ap->a_dvp);
1280df8bae1dSRodney W. Grimes 		if (error)
1281df8bae1dSRodney W. Grimes 			vput(vp);
1282df8bae1dSRodney W. Grimes 		return (error);
1283df8bae1dSRodney W. Grimes 	}
1284df8bae1dSRodney W. Grimes 
1285df8bae1dSRodney W. Grimes 	vput(ap->a_dvp);
1286df8bae1dSRodney W. Grimes 	return (EROFS);
1287df8bae1dSRodney W. Grimes }
1288df8bae1dSRodney W. Grimes 
1289c9bf0111SKATO Takenori static int
1290df8bae1dSRodney W. Grimes union_rmdir(ap)
1291df8bae1dSRodney W. Grimes 	struct vop_rmdir_args /* {
1292df8bae1dSRodney W. Grimes 		struct vnode *a_dvp;
1293df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
1294df8bae1dSRodney W. Grimes 		struct componentname *a_cnp;
1295df8bae1dSRodney W. Grimes 	} */ *ap;
1296df8bae1dSRodney W. Grimes {
1297df8bae1dSRodney W. Grimes 	int error;
1298df8bae1dSRodney W. Grimes 	struct union_node *dun = VTOUNION(ap->a_dvp);
1299df8bae1dSRodney W. Grimes 	struct union_node *un = VTOUNION(ap->a_vp);
1300996c772fSJohn Dyson 	struct componentname *cnp = ap->a_cnp;
1301996c772fSJohn Dyson 	struct proc *p = cnp->cn_proc;
1302df8bae1dSRodney W. Grimes 
1303996c772fSJohn Dyson 	if (dun->un_uppervp == NULLVP)
1304996c772fSJohn Dyson 		panic("union rmdir: null upper vnode");
1305996c772fSJohn Dyson 
1306996c772fSJohn Dyson 	if (un->un_uppervp != NULLVP) {
1307df8bae1dSRodney W. Grimes 		struct vnode *dvp = dun->un_uppervp;
1308df8bae1dSRodney W. Grimes 		struct vnode *vp = un->un_uppervp;
1309df8bae1dSRodney W. Grimes 
1310996c772fSJohn Dyson 		FIXUP(dun, p);
1311df8bae1dSRodney W. Grimes 		VREF(dvp);
1312d6476adaSKATO Takenori 		dun->un_flags |= UN_KLOCK;
1313df8bae1dSRodney W. Grimes 		vput(ap->a_dvp);
1314996c772fSJohn Dyson 		FIXUP(un, p);
1315df8bae1dSRodney W. Grimes 		VREF(vp);
1316d6476adaSKATO Takenori 		un->un_flags |= UN_KLOCK;
1317df8bae1dSRodney W. Grimes 		vput(ap->a_vp);
1318df8bae1dSRodney W. Grimes 
1319996c772fSJohn Dyson 		if (union_dowhiteout(un, cnp->cn_cred, cnp->cn_proc))
1320996c772fSJohn Dyson 			cnp->cn_flags |= DOWHITEOUT;
1321df8bae1dSRodney W. Grimes 		error = VOP_RMDIR(dvp, vp, ap->a_cnp);
132287884aadSKATO Takenori #if 0
132387884aadSKATO Takenori 		/* XXX */
1324df8bae1dSRodney W. Grimes 		if (!error)
1325df8bae1dSRodney W. Grimes 			union_removed_upper(un);
132687884aadSKATO Takenori #endif
1327df8bae1dSRodney W. Grimes 	} else {
1328996c772fSJohn Dyson 		FIXUP(dun, p);
1329996c772fSJohn Dyson 		error = union_mkwhiteout(
1330996c772fSJohn Dyson 			MOUNTTOUNIONMOUNT(UNIONTOV(dun)->v_mount),
1331996c772fSJohn Dyson 			dun->un_uppervp, ap->a_cnp, un->un_path);
1332df8bae1dSRodney W. Grimes 		vput(ap->a_dvp);
1333df8bae1dSRodney W. Grimes 		vput(ap->a_vp);
1334df8bae1dSRodney W. Grimes 	}
1335df8bae1dSRodney W. Grimes 
1336df8bae1dSRodney W. Grimes 	return (error);
1337df8bae1dSRodney W. Grimes }
1338df8bae1dSRodney W. Grimes 
1339c9bf0111SKATO Takenori static int
1340df8bae1dSRodney W. Grimes union_symlink(ap)
1341df8bae1dSRodney W. Grimes 	struct vop_symlink_args /* {
1342df8bae1dSRodney W. Grimes 		struct vnode *a_dvp;
1343df8bae1dSRodney W. Grimes 		struct vnode **a_vpp;
1344df8bae1dSRodney W. Grimes 		struct componentname *a_cnp;
1345df8bae1dSRodney W. Grimes 		struct vattr *a_vap;
1346df8bae1dSRodney W. Grimes 		char *a_target;
1347df8bae1dSRodney W. Grimes 	} */ *ap;
1348df8bae1dSRodney W. Grimes {
1349df8bae1dSRodney W. Grimes 	struct union_node *un = VTOUNION(ap->a_dvp);
1350df8bae1dSRodney W. Grimes 	struct vnode *dvp = un->un_uppervp;
1351996c772fSJohn Dyson 	struct componentname *cnp = ap->a_cnp;
1352996c772fSJohn Dyson 	struct proc *p = cnp->cn_proc;
1353df8bae1dSRodney W. Grimes 
1354996c772fSJohn Dyson 	if (dvp != NULLVP) {
1355df8bae1dSRodney W. Grimes 		int error;
1356df8bae1dSRodney W. Grimes 		struct vnode *vp;
1357df8bae1dSRodney W. Grimes 
1358996c772fSJohn Dyson 		FIXUP(un, p);
1359df8bae1dSRodney W. Grimes 		VREF(dvp);
1360d6476adaSKATO Takenori 		un->un_flags |= UN_KLOCK;
1361df8bae1dSRodney W. Grimes 		vput(ap->a_dvp);
1362996c772fSJohn Dyson 		error = VOP_SYMLINK(dvp, &vp, cnp, ap->a_vap, ap->a_target);
1363df8bae1dSRodney W. Grimes 		*ap->a_vpp = NULLVP;
1364df8bae1dSRodney W. Grimes 		return (error);
1365df8bae1dSRodney W. Grimes 	}
1366df8bae1dSRodney W. Grimes 
1367df8bae1dSRodney W. Grimes 	vput(ap->a_dvp);
1368df8bae1dSRodney W. Grimes 	return (EROFS);
1369df8bae1dSRodney W. Grimes }
1370df8bae1dSRodney W. Grimes 
1371df8bae1dSRodney W. Grimes /*
1372df8bae1dSRodney W. Grimes  * union_readdir works in concert with getdirentries and
1373df8bae1dSRodney W. Grimes  * readdir(3) to provide a list of entries in the unioned
1374df8bae1dSRodney W. Grimes  * directories.  getdirentries is responsible for walking
1375df8bae1dSRodney W. Grimes  * down the union stack.  readdir(3) is responsible for
1376df8bae1dSRodney W. Grimes  * eliminating duplicate names from the returned data stream.
1377df8bae1dSRodney W. Grimes  */
1378c9bf0111SKATO Takenori static int
1379df8bae1dSRodney W. Grimes union_readdir(ap)
1380df8bae1dSRodney W. Grimes 	struct vop_readdir_args /* {
1381df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
1382df8bae1dSRodney W. Grimes 		struct uio *a_uio;
1383df8bae1dSRodney W. Grimes 		struct ucred *a_cred;
1384996c772fSJohn Dyson 		int *a_eofflag;
1385996c772fSJohn Dyson 		u_long *a_cookies;
1386996c772fSJohn Dyson 		int a_ncookies;
1387df8bae1dSRodney W. Grimes 	} */ *ap;
1388df8bae1dSRodney W. Grimes {
1389df8bae1dSRodney W. Grimes 	struct union_node *un = VTOUNION(ap->a_vp);
1390996c772fSJohn Dyson 	struct vnode *uvp = un->un_uppervp;
1391996c772fSJohn Dyson 	struct proc *p = ap->a_uio->uio_procp;
1392df8bae1dSRodney W. Grimes 
1393996c772fSJohn Dyson 	if (uvp == NULLVP)
1394996c772fSJohn Dyson 		return (0);
1395df8bae1dSRodney W. Grimes 
1396996c772fSJohn Dyson 	FIXUP(un, p);
1397996c772fSJohn Dyson 	ap->a_vp = uvp;
1398996c772fSJohn Dyson 	return (VCALL(uvp, VOFFSET(vop_readdir), ap));
1399df8bae1dSRodney W. Grimes }
1400df8bae1dSRodney W. Grimes 
1401c9bf0111SKATO Takenori static int
1402df8bae1dSRodney W. Grimes union_readlink(ap)
1403df8bae1dSRodney W. Grimes 	struct vop_readlink_args /* {
1404df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
1405df8bae1dSRodney W. Grimes 		struct uio *a_uio;
1406df8bae1dSRodney W. Grimes 		struct ucred *a_cred;
1407df8bae1dSRodney W. Grimes 	} */ *ap;
1408df8bae1dSRodney W. Grimes {
1409df8bae1dSRodney W. Grimes 	int error;
1410996c772fSJohn Dyson 	struct uio *uio = ap->a_uio;
1411996c772fSJohn Dyson 	struct proc *p = uio->uio_procp;
1412df8bae1dSRodney W. Grimes 	struct vnode *vp = OTHERVP(ap->a_vp);
1413df8bae1dSRodney W. Grimes 	int dolock = (vp == LOWERVP(ap->a_vp));
1414df8bae1dSRodney W. Grimes 
1415df8bae1dSRodney W. Grimes 	if (dolock)
1416996c772fSJohn Dyson 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
1417df8bae1dSRodney W. Grimes 	else
1418996c772fSJohn Dyson 		FIXUP(VTOUNION(ap->a_vp), p);
1419996c772fSJohn Dyson 	ap->a_vp = vp;
1420996c772fSJohn Dyson 	error = VCALL(vp, VOFFSET(vop_readlink), ap);
1421df8bae1dSRodney W. Grimes 	if (dolock)
1422996c772fSJohn Dyson 		VOP_UNLOCK(vp, 0, p);
1423df8bae1dSRodney W. Grimes 
1424df8bae1dSRodney W. Grimes 	return (error);
1425df8bae1dSRodney W. Grimes }
1426df8bae1dSRodney W. Grimes 
1427c9bf0111SKATO Takenori static int
1428df8bae1dSRodney W. Grimes union_abortop(ap)
1429df8bae1dSRodney W. Grimes 	struct vop_abortop_args /* {
1430df8bae1dSRodney W. Grimes 		struct vnode *a_dvp;
1431df8bae1dSRodney W. Grimes 		struct componentname *a_cnp;
1432df8bae1dSRodney W. Grimes 	} */ *ap;
1433df8bae1dSRodney W. Grimes {
1434df8bae1dSRodney W. Grimes 	int error;
1435996c772fSJohn Dyson 	struct componentname *cnp = ap->a_cnp;
1436996c772fSJohn Dyson 	struct proc *p = cnp->cn_proc;
1437df8bae1dSRodney W. Grimes 	struct vnode *vp = OTHERVP(ap->a_dvp);
1438df8bae1dSRodney W. Grimes 	struct union_node *un = VTOUNION(ap->a_dvp);
1439df8bae1dSRodney W. Grimes 	int islocked = un->un_flags & UN_LOCKED;
1440df8bae1dSRodney W. Grimes 	int dolock = (vp == LOWERVP(ap->a_dvp));
1441df8bae1dSRodney W. Grimes 
1442df8bae1dSRodney W. Grimes 	if (islocked) {
1443df8bae1dSRodney W. Grimes 		if (dolock)
1444996c772fSJohn Dyson 			vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
1445df8bae1dSRodney W. Grimes 		else
1446996c772fSJohn Dyson 			FIXUP(VTOUNION(ap->a_dvp), p);
1447df8bae1dSRodney W. Grimes 	}
1448996c772fSJohn Dyson 	ap->a_dvp = vp;
1449996c772fSJohn Dyson 	error = VCALL(vp, VOFFSET(vop_abortop), ap);
1450df8bae1dSRodney W. Grimes 	if (islocked && dolock)
1451996c772fSJohn Dyson 		VOP_UNLOCK(vp, 0, p);
1452df8bae1dSRodney W. Grimes 
1453df8bae1dSRodney W. Grimes 	return (error);
1454df8bae1dSRodney W. Grimes }
1455df8bae1dSRodney W. Grimes 
1456c9bf0111SKATO Takenori static int
1457df8bae1dSRodney W. Grimes union_inactive(ap)
1458df8bae1dSRodney W. Grimes 	struct vop_inactive_args /* {
1459df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
1460996c772fSJohn Dyson 		struct proc *a_p;
1461df8bae1dSRodney W. Grimes 	} */ *ap;
1462df8bae1dSRodney W. Grimes {
1463996c772fSJohn Dyson 	struct vnode *vp = ap->a_vp;
1464996c772fSJohn Dyson 	struct proc *p = ap->a_p;
1465996c772fSJohn Dyson 	struct union_node *un = VTOUNION(vp);
1466996c772fSJohn Dyson 	struct vnode **vpp;
1467df8bae1dSRodney W. Grimes 
1468df8bae1dSRodney W. Grimes 	/*
1469df8bae1dSRodney W. Grimes 	 * Do nothing (and _don't_ bypass).
1470df8bae1dSRodney W. Grimes 	 * Wait to vrele lowervp until reclaim,
1471df8bae1dSRodney W. Grimes 	 * so that until then our union_node is in the
1472df8bae1dSRodney W. Grimes 	 * cache and reusable.
1473df8bae1dSRodney W. Grimes 	 *
1474df8bae1dSRodney W. Grimes 	 * NEEDSWORK: Someday, consider inactive'ing
1475df8bae1dSRodney W. Grimes 	 * the lowervp and then trying to reactivate it
1476df8bae1dSRodney W. Grimes 	 * with capabilities (v_id)
1477df8bae1dSRodney W. Grimes 	 * like they do in the name lookup cache code.
1478df8bae1dSRodney W. Grimes 	 * That's too much work for now.
1479df8bae1dSRodney W. Grimes 	 */
1480df8bae1dSRodney W. Grimes 
1481996c772fSJohn Dyson 	if (un->un_dircache != 0) {
1482996c772fSJohn Dyson 		for (vpp = un->un_dircache; *vpp != NULLVP; vpp++)
1483996c772fSJohn Dyson 			vrele(*vpp);
1484996c772fSJohn Dyson 		free(un->un_dircache, M_TEMP);
1485996c772fSJohn Dyson 		un->un_dircache = 0;
1486996c772fSJohn Dyson 	}
1487df8bae1dSRodney W. Grimes 
1488996c772fSJohn Dyson 	VOP_UNLOCK(vp, 0, p);
1489996c772fSJohn Dyson 
1490996c772fSJohn Dyson 	if ((un->un_flags & UN_CACHED) == 0)
1491996c772fSJohn Dyson 		vgone(vp);
1492df8bae1dSRodney W. Grimes 
1493df8bae1dSRodney W. Grimes 	return (0);
1494df8bae1dSRodney W. Grimes }
1495df8bae1dSRodney W. Grimes 
1496c9bf0111SKATO Takenori static int
1497df8bae1dSRodney W. Grimes union_reclaim(ap)
1498df8bae1dSRodney W. Grimes 	struct vop_reclaim_args /* {
1499df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
1500df8bae1dSRodney W. Grimes 	} */ *ap;
1501df8bae1dSRodney W. Grimes {
1502df8bae1dSRodney W. Grimes 
1503df8bae1dSRodney W. Grimes 	union_freevp(ap->a_vp);
1504df8bae1dSRodney W. Grimes 
1505df8bae1dSRodney W. Grimes 	return (0);
1506df8bae1dSRodney W. Grimes }
1507df8bae1dSRodney W. Grimes 
1508c9bf0111SKATO Takenori static int
1509df8bae1dSRodney W. Grimes union_lock(ap)
1510df8bae1dSRodney W. Grimes 	struct vop_lock_args *ap;
1511df8bae1dSRodney W. Grimes {
1512df8bae1dSRodney W. Grimes 	struct vnode *vp = ap->a_vp;
1513996c772fSJohn Dyson 	struct proc *p = ap->a_p;
1514996c772fSJohn Dyson 	int flags = ap->a_flags;
1515df8bae1dSRodney W. Grimes 	struct union_node *un;
1516996c772fSJohn Dyson 	int error;
1517996c772fSJohn Dyson 
1518996c772fSJohn Dyson 	vop_nolock(ap);
1519996c772fSJohn Dyson 	/*
1520996c772fSJohn Dyson 	 * Need to do real lockmgr-style locking here.
1521996c772fSJohn Dyson 	 * in the mean time, draining won't work quite right,
1522996c772fSJohn Dyson 	 * which could lead to a few race conditions.
1523996c772fSJohn Dyson 	 * the following test was here, but is not quite right, we
1524996c772fSJohn Dyson 	 * still need to take the lock:
1525996c772fSJohn Dyson 	if ((flags & LK_TYPE_MASK) == LK_DRAIN)
1526996c772fSJohn Dyson 		return (0);
1527996c772fSJohn Dyson 	 */
1528996c772fSJohn Dyson 	flags &= ~LK_INTERLOCK;
1529df8bae1dSRodney W. Grimes 
1530df8bae1dSRodney W. Grimes start:
1531df8bae1dSRodney W. Grimes 	un = VTOUNION(vp);
1532df8bae1dSRodney W. Grimes 
1533996c772fSJohn Dyson 	if (un->un_uppervp != NULLVP) {
15343ae60298SKATO Takenori 		if (((un->un_flags & UN_ULOCK) == 0) &&
1535996c772fSJohn Dyson 		    (vp->v_usecount != 0)) {
1536d6476adaSKATO Takenori 			if ((un->un_flags & UN_GLOCK) == 0) {
1537996c772fSJohn Dyson 				error = vn_lock(un->un_uppervp, flags, p);
1538996c772fSJohn Dyson 				if (error)
1539996c772fSJohn Dyson 					return (error);
15403ae60298SKATO Takenori 			}
1541df8bae1dSRodney W. Grimes 			un->un_flags |= UN_ULOCK;
1542df8bae1dSRodney W. Grimes 		}
1543d6476adaSKATO Takenori #ifdef DIAGNOSTIC
1544d6476adaSKATO Takenori 		if (un->un_flags & UN_KLOCK) {
1545d6476adaSKATO Takenori 			vprint("dangling upper lock", vp);
1546d6476adaSKATO Takenori 			panic("union: dangling upper lock");
1547d6476adaSKATO Takenori 		}
1548d6476adaSKATO Takenori #endif
154918b4c8e2SKATO Takenori 	}
1550df8bae1dSRodney W. Grimes 
1551df8bae1dSRodney W. Grimes 	if (un->un_flags & UN_LOCKED) {
1552df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
1553df8bae1dSRodney W. Grimes 		if (curproc && un->un_pid == curproc->p_pid &&
1554df8bae1dSRodney W. Grimes 			    un->un_pid > -1 && curproc->p_pid > -1)
1555df8bae1dSRodney W. Grimes 			panic("union: locking against myself");
1556df8bae1dSRodney W. Grimes #endif
1557df8bae1dSRodney W. Grimes 		un->un_flags |= UN_WANT;
1558996c772fSJohn Dyson 		tsleep((caddr_t)&un->un_flags, PINOD, "unionlk2", 0);
1559df8bae1dSRodney W. Grimes 		goto start;
1560df8bae1dSRodney W. Grimes 	}
1561df8bae1dSRodney W. Grimes 
1562df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
1563df8bae1dSRodney W. Grimes 	if (curproc)
1564df8bae1dSRodney W. Grimes 		un->un_pid = curproc->p_pid;
1565df8bae1dSRodney W. Grimes 	else
1566df8bae1dSRodney W. Grimes 		un->un_pid = -1;
1567df8bae1dSRodney W. Grimes #endif
1568df8bae1dSRodney W. Grimes 
1569df8bae1dSRodney W. Grimes 	un->un_flags |= UN_LOCKED;
1570df8bae1dSRodney W. Grimes 	return (0);
1571df8bae1dSRodney W. Grimes }
1572df8bae1dSRodney W. Grimes 
1573996c772fSJohn Dyson /*
1574996c772fSJohn Dyson  * When operations want to vput() a union node yet retain a lock on
1575996c772fSJohn Dyson  * the upper vnode (say, to do some further operations like link(),
1576996c772fSJohn Dyson  * mkdir(), ...), they set UN_KLOCK on the union node, then call
1577996c772fSJohn Dyson  * vput() which calls VOP_UNLOCK() and comes here.  union_unlock()
1578996c772fSJohn Dyson  * unlocks the union node (leaving the upper vnode alone), clears the
1579996c772fSJohn Dyson  * KLOCK flag, and then returns to vput().  The caller then does whatever
1580996c772fSJohn Dyson  * is left to do with the upper vnode, and ensures that it gets unlocked.
1581996c772fSJohn Dyson  *
1582996c772fSJohn Dyson  * If UN_KLOCK isn't set, then the upper vnode is unlocked here.
1583996c772fSJohn Dyson  */
1584c9bf0111SKATO Takenori static int
1585df8bae1dSRodney W. Grimes union_unlock(ap)
1586996c772fSJohn Dyson 	struct vop_unlock_args /* {
1587996c772fSJohn Dyson 		struct vnode *a_vp;
1588996c772fSJohn Dyson 		int a_flags;
1589996c772fSJohn Dyson 		struct proc *a_p;
1590996c772fSJohn Dyson 	} */ *ap;
1591df8bae1dSRodney W. Grimes {
1592df8bae1dSRodney W. Grimes 	struct union_node *un = VTOUNION(ap->a_vp);
1593996c772fSJohn Dyson 	struct proc *p = ap->a_p;
1594df8bae1dSRodney W. Grimes 
1595df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
1596df8bae1dSRodney W. Grimes 	if ((un->un_flags & UN_LOCKED) == 0)
1597df8bae1dSRodney W. Grimes 		panic("union: unlock unlocked node");
1598df8bae1dSRodney W. Grimes 	if (curproc && un->un_pid != curproc->p_pid &&
1599df8bae1dSRodney W. Grimes 			curproc->p_pid > -1 && un->un_pid > -1)
1600df8bae1dSRodney W. Grimes 		panic("union: unlocking other process's union node");
1601df8bae1dSRodney W. Grimes #endif
1602df8bae1dSRodney W. Grimes 
1603df8bae1dSRodney W. Grimes 	un->un_flags &= ~UN_LOCKED;
1604df8bae1dSRodney W. Grimes 
1605d6476adaSKATO Takenori 	if ((un->un_flags & (UN_ULOCK|UN_KLOCK|UN_GLOCK)) == UN_ULOCK)
1606996c772fSJohn Dyson 		VOP_UNLOCK(un->un_uppervp, 0, p);
1607d6476adaSKATO Takenori 
1608d6476adaSKATO Takenori 	un->un_flags &= ~(UN_ULOCK|UN_KLOCK);
1609df8bae1dSRodney W. Grimes 
1610df8bae1dSRodney W. Grimes 	if (un->un_flags & UN_WANT) {
1611df8bae1dSRodney W. Grimes 		un->un_flags &= ~UN_WANT;
1612df8bae1dSRodney W. Grimes 		wakeup((caddr_t) &un->un_flags);
1613df8bae1dSRodney W. Grimes 	}
1614df8bae1dSRodney W. Grimes 
1615df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
1616df8bae1dSRodney W. Grimes 	un->un_pid = 0;
1617df8bae1dSRodney W. Grimes #endif
1618996c772fSJohn Dyson 	vop_nounlock(ap);
1619df8bae1dSRodney W. Grimes 
1620df8bae1dSRodney W. Grimes 	return (0);
1621df8bae1dSRodney W. Grimes }
1622df8bae1dSRodney W. Grimes 
1623c9bf0111SKATO Takenori static int
1624df8bae1dSRodney W. Grimes union_bmap(ap)
1625df8bae1dSRodney W. Grimes 	struct vop_bmap_args /* {
1626df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
1627df8bae1dSRodney W. Grimes 		daddr_t  a_bn;
1628df8bae1dSRodney W. Grimes 		struct vnode **a_vpp;
1629df8bae1dSRodney W. Grimes 		daddr_t *a_bnp;
1630df8bae1dSRodney W. Grimes 		int *a_runp;
1631c83ebe77SJohn Dyson 		int *a_runb;
1632df8bae1dSRodney W. Grimes 	} */ *ap;
1633df8bae1dSRodney W. Grimes {
1634df8bae1dSRodney W. Grimes 	int error;
1635996c772fSJohn Dyson 	struct proc *p = curproc;		/* XXX */
1636df8bae1dSRodney W. Grimes 	struct vnode *vp = OTHERVP(ap->a_vp);
1637df8bae1dSRodney W. Grimes 	int dolock = (vp == LOWERVP(ap->a_vp));
1638df8bae1dSRodney W. Grimes 
1639df8bae1dSRodney W. Grimes 	if (dolock)
1640996c772fSJohn Dyson 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
1641df8bae1dSRodney W. Grimes 	else
1642996c772fSJohn Dyson 		FIXUP(VTOUNION(ap->a_vp), p);
1643996c772fSJohn Dyson 	ap->a_vp = vp;
1644996c772fSJohn Dyson 	error = VCALL(vp, VOFFSET(vop_bmap), ap);
1645df8bae1dSRodney W. Grimes 	if (dolock)
1646996c772fSJohn Dyson 		VOP_UNLOCK(vp, 0, p);
1647df8bae1dSRodney W. Grimes 
1648df8bae1dSRodney W. Grimes 	return (error);
1649df8bae1dSRodney W. Grimes }
1650df8bae1dSRodney W. Grimes 
1651c9bf0111SKATO Takenori static int
1652df8bae1dSRodney W. Grimes union_print(ap)
1653df8bae1dSRodney W. Grimes 	struct vop_print_args /* {
1654df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
1655df8bae1dSRodney W. Grimes 	} */ *ap;
1656df8bae1dSRodney W. Grimes {
1657df8bae1dSRodney W. Grimes 	struct vnode *vp = ap->a_vp;
1658df8bae1dSRodney W. Grimes 
16593a773ad0SPoul-Henning Kamp 	printf("\ttag VT_UNION, vp=%p, uppervp=%p, lowervp=%p\n",
1660df8bae1dSRodney W. Grimes 			vp, UPPERVP(vp), LOWERVP(vp));
1661996c772fSJohn Dyson 	if (UPPERVP(vp) != NULLVP)
1662996c772fSJohn Dyson 		vprint("union: upper", UPPERVP(vp));
1663996c772fSJohn Dyson 	if (LOWERVP(vp) != NULLVP)
1664996c772fSJohn Dyson 		vprint("union: lower", LOWERVP(vp));
1665996c772fSJohn Dyson 
1666df8bae1dSRodney W. Grimes 	return (0);
1667df8bae1dSRodney W. Grimes }
1668df8bae1dSRodney W. Grimes 
1669c9bf0111SKATO Takenori static int
1670df8bae1dSRodney W. Grimes union_islocked(ap)
1671df8bae1dSRodney W. Grimes 	struct vop_islocked_args /* {
1672df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
1673df8bae1dSRodney W. Grimes 	} */ *ap;
1674df8bae1dSRodney W. Grimes {
1675df8bae1dSRodney W. Grimes 
1676df8bae1dSRodney W. Grimes 	return ((VTOUNION(ap->a_vp)->un_flags & UN_LOCKED) ? 1 : 0);
1677df8bae1dSRodney W. Grimes }
1678df8bae1dSRodney W. Grimes 
1679c9bf0111SKATO Takenori static int
1680df8bae1dSRodney W. Grimes union_pathconf(ap)
1681df8bae1dSRodney W. Grimes 	struct vop_pathconf_args /* {
1682df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
1683df8bae1dSRodney W. Grimes 		int a_name;
1684df8bae1dSRodney W. Grimes 		int *a_retval;
1685df8bae1dSRodney W. Grimes 	} */ *ap;
1686df8bae1dSRodney W. Grimes {
1687df8bae1dSRodney W. Grimes 	int error;
1688996c772fSJohn Dyson 	struct proc *p = curproc;		/* XXX */
1689df8bae1dSRodney W. Grimes 	struct vnode *vp = OTHERVP(ap->a_vp);
1690df8bae1dSRodney W. Grimes 	int dolock = (vp == LOWERVP(ap->a_vp));
1691df8bae1dSRodney W. Grimes 
1692df8bae1dSRodney W. Grimes 	if (dolock)
1693996c772fSJohn Dyson 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
1694df8bae1dSRodney W. Grimes 	else
1695996c772fSJohn Dyson 		FIXUP(VTOUNION(ap->a_vp), p);
1696996c772fSJohn Dyson 	ap->a_vp = vp;
1697996c772fSJohn Dyson 	error = VCALL(vp, VOFFSET(vop_pathconf), ap);
1698df8bae1dSRodney W. Grimes 	if (dolock)
1699996c772fSJohn Dyson 		VOP_UNLOCK(vp, 0, p);
1700df8bae1dSRodney W. Grimes 
1701df8bae1dSRodney W. Grimes 	return (error);
1702df8bae1dSRodney W. Grimes }
1703df8bae1dSRodney W. Grimes 
1704c9bf0111SKATO Takenori static int
1705df8bae1dSRodney W. Grimes union_advlock(ap)
1706df8bae1dSRodney W. Grimes 	struct vop_advlock_args /* {
1707df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
1708df8bae1dSRodney W. Grimes 		caddr_t  a_id;
1709df8bae1dSRodney W. Grimes 		int  a_op;
1710df8bae1dSRodney W. Grimes 		struct flock *a_fl;
1711df8bae1dSRodney W. Grimes 		int  a_flags;
1712df8bae1dSRodney W. Grimes 	} */ *ap;
1713df8bae1dSRodney W. Grimes {
1714996c772fSJohn Dyson 	register struct vnode *ovp = OTHERVP(ap->a_vp);
1715df8bae1dSRodney W. Grimes 
1716996c772fSJohn Dyson 	ap->a_vp = ovp;
1717996c772fSJohn Dyson 	return (VCALL(ovp, VOFFSET(vop_advlock), ap));
1718df8bae1dSRodney W. Grimes }
1719df8bae1dSRodney W. Grimes 
1720df8bae1dSRodney W. Grimes 
1721df8bae1dSRodney W. Grimes /*
1722df8bae1dSRodney W. Grimes  * XXX - vop_strategy must be hand coded because it has no
1723df8bae1dSRodney W. Grimes  * vnode in its arguments.
1724df8bae1dSRodney W. Grimes  * This goes away with a merged VM/buffer cache.
1725df8bae1dSRodney W. Grimes  */
1726c9bf0111SKATO Takenori static int
1727df8bae1dSRodney W. Grimes union_strategy(ap)
1728df8bae1dSRodney W. Grimes 	struct vop_strategy_args /* {
1729df8bae1dSRodney W. Grimes 		struct buf *a_bp;
1730df8bae1dSRodney W. Grimes 	} */ *ap;
1731df8bae1dSRodney W. Grimes {
1732df8bae1dSRodney W. Grimes 	struct buf *bp = ap->a_bp;
1733df8bae1dSRodney W. Grimes 	int error;
1734df8bae1dSRodney W. Grimes 	struct vnode *savedvp;
1735df8bae1dSRodney W. Grimes 
1736df8bae1dSRodney W. Grimes 	savedvp = bp->b_vp;
1737df8bae1dSRodney W. Grimes 	bp->b_vp = OTHERVP(bp->b_vp);
1738df8bae1dSRodney W. Grimes 
1739df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
1740df8bae1dSRodney W. Grimes 	if (bp->b_vp == NULLVP)
1741df8bae1dSRodney W. Grimes 		panic("union_strategy: nil vp");
1742df8bae1dSRodney W. Grimes 	if (((bp->b_flags & B_READ) == 0) &&
1743df8bae1dSRodney W. Grimes 	    (bp->b_vp == LOWERVP(savedvp)))
1744df8bae1dSRodney W. Grimes 		panic("union_strategy: writing to lowervp");
1745df8bae1dSRodney W. Grimes #endif
1746df8bae1dSRodney W. Grimes 
1747df8bae1dSRodney W. Grimes 	error = VOP_STRATEGY(bp);
1748df8bae1dSRodney W. Grimes 	bp->b_vp = savedvp;
1749df8bae1dSRodney W. Grimes 
1750df8bae1dSRodney W. Grimes 	return (error);
1751df8bae1dSRodney W. Grimes }
1752df8bae1dSRodney W. Grimes 
1753df8bae1dSRodney W. Grimes /*
1754df8bae1dSRodney W. Grimes  * Global vfs data structures
1755df8bae1dSRodney W. Grimes  */
1756f57e6547SBruce Evans vop_t **union_vnodeop_p;
1757c109c577SBruce Evans static struct vnodeopv_entry_desc union_vnodeop_entries[] = {
1758dba3870cSPoul-Henning Kamp 	{ &vop_default_desc,		(vop_t *) vop_defaultop },
1759539ef70cSPoul-Henning Kamp 	{ &vop_abortop_desc,		(vop_t *) union_abortop },
1760539ef70cSPoul-Henning Kamp 	{ &vop_access_desc,		(vop_t *) union_access },
1761539ef70cSPoul-Henning Kamp 	{ &vop_advlock_desc,		(vop_t *) union_advlock },
1762539ef70cSPoul-Henning Kamp 	{ &vop_bmap_desc,		(vop_t *) union_bmap },
1763539ef70cSPoul-Henning Kamp 	{ &vop_close_desc,		(vop_t *) union_close },
1764539ef70cSPoul-Henning Kamp 	{ &vop_create_desc,		(vop_t *) union_create },
1765539ef70cSPoul-Henning Kamp 	{ &vop_fsync_desc,		(vop_t *) union_fsync },
1766539ef70cSPoul-Henning Kamp 	{ &vop_getattr_desc,		(vop_t *) union_getattr },
1767539ef70cSPoul-Henning Kamp 	{ &vop_inactive_desc,		(vop_t *) union_inactive },
1768539ef70cSPoul-Henning Kamp 	{ &vop_ioctl_desc,		(vop_t *) union_ioctl },
1769539ef70cSPoul-Henning Kamp 	{ &vop_islocked_desc,		(vop_t *) union_islocked },
1770539ef70cSPoul-Henning Kamp 	{ &vop_lease_desc,		(vop_t *) union_lease },
1771539ef70cSPoul-Henning Kamp 	{ &vop_link_desc,		(vop_t *) union_link },
1772539ef70cSPoul-Henning Kamp 	{ &vop_lock_desc,		(vop_t *) union_lock },
1773539ef70cSPoul-Henning Kamp 	{ &vop_lookup_desc,		(vop_t *) union_lookup },
1774539ef70cSPoul-Henning Kamp 	{ &vop_mkdir_desc,		(vop_t *) union_mkdir },
1775539ef70cSPoul-Henning Kamp 	{ &vop_mknod_desc,		(vop_t *) union_mknod },
1776539ef70cSPoul-Henning Kamp 	{ &vop_mmap_desc,		(vop_t *) union_mmap },
1777539ef70cSPoul-Henning Kamp 	{ &vop_open_desc,		(vop_t *) union_open },
1778539ef70cSPoul-Henning Kamp 	{ &vop_pathconf_desc,		(vop_t *) union_pathconf },
1779539ef70cSPoul-Henning Kamp 	{ &vop_poll_desc,		(vop_t *) union_poll },
1780539ef70cSPoul-Henning Kamp 	{ &vop_print_desc,		(vop_t *) union_print },
1781539ef70cSPoul-Henning Kamp 	{ &vop_read_desc,		(vop_t *) union_read },
1782539ef70cSPoul-Henning Kamp 	{ &vop_readdir_desc,		(vop_t *) union_readdir },
1783539ef70cSPoul-Henning Kamp 	{ &vop_readlink_desc,		(vop_t *) union_readlink },
1784539ef70cSPoul-Henning Kamp 	{ &vop_reclaim_desc,		(vop_t *) union_reclaim },
1785539ef70cSPoul-Henning Kamp 	{ &vop_remove_desc,		(vop_t *) union_remove },
1786539ef70cSPoul-Henning Kamp 	{ &vop_rename_desc,		(vop_t *) union_rename },
1787539ef70cSPoul-Henning Kamp 	{ &vop_revoke_desc,		(vop_t *) union_revoke },
1788539ef70cSPoul-Henning Kamp 	{ &vop_rmdir_desc,		(vop_t *) union_rmdir },
1789539ef70cSPoul-Henning Kamp 	{ &vop_setattr_desc,		(vop_t *) union_setattr },
1790539ef70cSPoul-Henning Kamp 	{ &vop_strategy_desc,		(vop_t *) union_strategy },
1791539ef70cSPoul-Henning Kamp 	{ &vop_symlink_desc,		(vop_t *) union_symlink },
1792539ef70cSPoul-Henning Kamp 	{ &vop_unlock_desc,		(vop_t *) union_unlock },
1793539ef70cSPoul-Henning Kamp 	{ &vop_whiteout_desc,		(vop_t *) union_whiteout },
1794539ef70cSPoul-Henning Kamp 	{ &vop_write_desc,		(vop_t *) union_write },
1795f57e6547SBruce Evans 	{ NULL, NULL }
1796df8bae1dSRodney W. Grimes };
1797c109c577SBruce Evans static struct vnodeopv_desc union_vnodeop_opv_desc =
1798df8bae1dSRodney W. Grimes 	{ &union_vnodeop_p, union_vnodeop_entries };
1799c901836cSGarrett Wollman 
1800c901836cSGarrett Wollman VNODEOP_SET(union_vnodeop_opv_desc);
1801