xref: /freebsd/sys/fs/unionfs/union_vnops.c (revision 87884aad6ee7a01f0d7e9d79c90e6376b34fd820)
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
3887884aadSKATO Takenori  * $Id: union_vnops.c,v 1.51 1998/02/04 22:32:55 eivind Exp $
39df8bae1dSRodney W. Grimes  */
40df8bae1dSRodney W. Grimes 
4147cfdb16SEivind Eklund #include "opt_diagnostic.h"
4247cfdb16SEivind Eklund 
43df8bae1dSRodney W. Grimes #include <sys/param.h>
44df8bae1dSRodney W. Grimes #include <sys/systm.h>
45df8bae1dSRodney W. Grimes #include <sys/proc.h>
463ac4d1efSBruce Evans #include <sys/fcntl.h>
47996c772fSJohn Dyson #include <sys/stat.h>
489e67ea79SBruce Evans #include <sys/kernel.h>
49df8bae1dSRodney W. Grimes #include <sys/vnode.h>
50df8bae1dSRodney W. Grimes #include <sys/mount.h>
51df8bae1dSRodney W. Grimes #include <sys/namei.h>
52df8bae1dSRodney W. Grimes #include <sys/malloc.h>
53df8bae1dSRodney W. Grimes #include <sys/buf.h>
54996c772fSJohn Dyson #include <sys/lock.h>
55df8bae1dSRodney W. Grimes #include <miscfs/union/union.h>
56df8bae1dSRodney W. Grimes 
57996c772fSJohn Dyson #define FIXUP(un, p) { \
58996c772fSJohn Dyson 	if (((un)->un_flags & UN_ULOCK) == 0) { \
59996c772fSJohn Dyson 		union_fixup(un, p); \
60df8bae1dSRodney W. Grimes 	} \
61df8bae1dSRodney W. Grimes }
62df8bae1dSRodney W. Grimes 
63c9bf0111SKATO Takenori static int	union_abortop __P((struct vop_abortop_args *ap));
64c9bf0111SKATO Takenori static int	union_access __P((struct vop_access_args *ap));
65c9bf0111SKATO Takenori static int	union_advlock __P((struct vop_advlock_args *ap));
66c9bf0111SKATO Takenori static int	union_bmap __P((struct vop_bmap_args *ap));
67c9bf0111SKATO Takenori static int	union_close __P((struct vop_close_args *ap));
68c9bf0111SKATO Takenori static int	union_create __P((struct vop_create_args *ap));
69996c772fSJohn Dyson static void	union_fixup __P((struct union_node *un, struct proc *p));
70c9bf0111SKATO Takenori static int	union_fsync __P((struct vop_fsync_args *ap));
71c9bf0111SKATO Takenori static int	union_getattr __P((struct vop_getattr_args *ap));
72c9bf0111SKATO Takenori static int	union_inactive __P((struct vop_inactive_args *ap));
73c9bf0111SKATO Takenori static int	union_ioctl __P((struct vop_ioctl_args *ap));
74c9bf0111SKATO Takenori static int	union_islocked __P((struct vop_islocked_args *ap));
75c9bf0111SKATO Takenori static int	union_lease __P((struct vop_lease_args *ap));
76c9bf0111SKATO Takenori static int	union_link __P((struct vop_link_args *ap));
77c9bf0111SKATO Takenori static int	union_lock __P((struct vop_lock_args *ap));
78c9bf0111SKATO Takenori static int	union_lookup __P((struct vop_lookup_args *ap));
79996c772fSJohn Dyson static int	union_lookup1 __P((struct vnode *udvp, struct vnode **dvpp,
809b5e8b3aSBruce Evans 				   struct vnode **vpp,
819b5e8b3aSBruce Evans 				   struct componentname *cnp));
82c9bf0111SKATO Takenori static int	union_mkdir __P((struct vop_mkdir_args *ap));
83c9bf0111SKATO Takenori static int	union_mknod __P((struct vop_mknod_args *ap));
84c9bf0111SKATO Takenori static int	union_mmap __P((struct vop_mmap_args *ap));
85c9bf0111SKATO Takenori static int	union_open __P((struct vop_open_args *ap));
86c9bf0111SKATO Takenori static int	union_pathconf __P((struct vop_pathconf_args *ap));
87c9bf0111SKATO Takenori static int	union_print __P((struct vop_print_args *ap));
88c9bf0111SKATO Takenori static int	union_read __P((struct vop_read_args *ap));
89c9bf0111SKATO Takenori static int	union_readdir __P((struct vop_readdir_args *ap));
90c9bf0111SKATO Takenori static int	union_readlink __P((struct vop_readlink_args *ap));
91c9bf0111SKATO Takenori static int	union_reclaim __P((struct vop_reclaim_args *ap));
92c9bf0111SKATO Takenori static int	union_remove __P((struct vop_remove_args *ap));
93c9bf0111SKATO Takenori static int	union_rename __P((struct vop_rename_args *ap));
94c9bf0111SKATO Takenori static int	union_revoke __P((struct vop_revoke_args *ap));
95c9bf0111SKATO Takenori static int	union_rmdir __P((struct vop_rmdir_args *ap));
96a6aeade2SPeter Wemm static int	union_poll __P((struct vop_poll_args *ap));
97c9bf0111SKATO Takenori static int	union_setattr __P((struct vop_setattr_args *ap));
98c9bf0111SKATO Takenori static int	union_strategy __P((struct vop_strategy_args *ap));
99c9bf0111SKATO Takenori static int	union_symlink __P((struct vop_symlink_args *ap));
100c9bf0111SKATO Takenori static int	union_unlock __P((struct vop_unlock_args *ap));
101c9bf0111SKATO Takenori static int	union_whiteout __P((struct vop_whiteout_args *ap));
102c9bf0111SKATO Takenori static int	union_write __P((struct vop_read_args *ap));
1039b5e8b3aSBruce Evans 
104df8bae1dSRodney W. Grimes static void
105996c772fSJohn Dyson union_fixup(un, p)
106df8bae1dSRodney W. Grimes 	struct union_node *un;
107996c772fSJohn Dyson 	struct proc *p;
108df8bae1dSRodney W. Grimes {
109df8bae1dSRodney W. Grimes 
110996c772fSJohn Dyson 	vn_lock(un->un_uppervp, LK_EXCLUSIVE | LK_RETRY, p);
111df8bae1dSRodney W. Grimes 	un->un_flags |= UN_ULOCK;
112df8bae1dSRodney W. Grimes }
113df8bae1dSRodney W. Grimes 
114df8bae1dSRodney W. Grimes static int
115996c772fSJohn Dyson union_lookup1(udvp, dvpp, vpp, cnp)
116df8bae1dSRodney W. Grimes 	struct vnode *udvp;
117996c772fSJohn Dyson 	struct vnode **dvpp;
118df8bae1dSRodney W. Grimes 	struct vnode **vpp;
119df8bae1dSRodney W. Grimes 	struct componentname *cnp;
120df8bae1dSRodney W. Grimes {
121df8bae1dSRodney W. Grimes 	int error;
122996c772fSJohn Dyson 	struct proc *p = cnp->cn_proc;
123df8bae1dSRodney W. Grimes 	struct vnode *tdvp;
124996c772fSJohn Dyson 	struct vnode *dvp;
125df8bae1dSRodney W. Grimes 	struct mount *mp;
126df8bae1dSRodney W. Grimes 
127996c772fSJohn Dyson 	dvp = *dvpp;
128996c772fSJohn Dyson 
129df8bae1dSRodney W. Grimes 	/*
130df8bae1dSRodney W. Grimes 	 * If stepping up the directory tree, check for going
131df8bae1dSRodney W. Grimes 	 * back across the mount point, in which case do what
132df8bae1dSRodney W. Grimes 	 * lookup would do by stepping back down the mount
133df8bae1dSRodney W. Grimes 	 * hierarchy.
134df8bae1dSRodney W. Grimes 	 */
135df8bae1dSRodney W. Grimes 	if (cnp->cn_flags & ISDOTDOT) {
136996c772fSJohn Dyson 		while ((dvp != udvp) && (dvp->v_flag & VROOT)) {
137df8bae1dSRodney W. Grimes 			/*
138df8bae1dSRodney W. Grimes 			 * Don't do the NOCROSSMOUNT check
139df8bae1dSRodney W. Grimes 			 * at this level.  By definition,
140df8bae1dSRodney W. Grimes 			 * union fs deals with namespaces, not
141df8bae1dSRodney W. Grimes 			 * filesystems.
142df8bae1dSRodney W. Grimes 			 */
143df8bae1dSRodney W. Grimes 			tdvp = dvp;
144996c772fSJohn Dyson 			*dvpp = dvp = dvp->v_mount->mnt_vnodecovered;
145df8bae1dSRodney W. Grimes 			vput(tdvp);
146df8bae1dSRodney W. Grimes 			VREF(dvp);
147996c772fSJohn Dyson 			vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, p);
148df8bae1dSRodney W. Grimes 		}
149df8bae1dSRodney W. Grimes 	}
150df8bae1dSRodney W. Grimes 
151df8bae1dSRodney W. Grimes         error = VOP_LOOKUP(dvp, &tdvp, cnp);
152df8bae1dSRodney W. Grimes 	if (error)
153df8bae1dSRodney W. Grimes 		return (error);
154df8bae1dSRodney W. Grimes 
155df8bae1dSRodney W. Grimes 	/*
156df8bae1dSRodney W. Grimes 	 * The parent directory will have been unlocked, unless lookup
157df8bae1dSRodney W. Grimes 	 * found the last component.  In which case, re-lock the node
158df8bae1dSRodney W. Grimes 	 * here to allow it to be unlocked again (phew) in union_lookup.
159df8bae1dSRodney W. Grimes 	 */
160df8bae1dSRodney W. Grimes 	if (dvp != tdvp && !(cnp->cn_flags & ISLASTCN))
161996c772fSJohn Dyson 		vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, p);
162df8bae1dSRodney W. Grimes 
163df8bae1dSRodney W. Grimes 	dvp = tdvp;
164df8bae1dSRodney W. Grimes 
165df8bae1dSRodney W. Grimes 	/*
166df8bae1dSRodney W. Grimes 	 * Lastly check if the current node is a mount point in
167df8bae1dSRodney W. Grimes 	 * which case walk up the mount hierarchy making sure not to
168df8bae1dSRodney W. Grimes 	 * bump into the root of the mount tree (ie. dvp != udvp).
169df8bae1dSRodney W. Grimes 	 */
170df8bae1dSRodney W. Grimes 	while (dvp != udvp && (dvp->v_type == VDIR) &&
171df8bae1dSRodney W. Grimes 	       (mp = dvp->v_mountedhere)) {
172df8bae1dSRodney W. Grimes 
173996c772fSJohn Dyson 		if (vfs_busy(mp, 0, 0, p))
174df8bae1dSRodney W. Grimes 			continue;
175df8bae1dSRodney W. Grimes 
1763a773ad0SPoul-Henning Kamp 		error = VFS_ROOT(mp, &tdvp);
177996c772fSJohn Dyson 		vfs_unbusy(mp, p);
1783a773ad0SPoul-Henning Kamp 		if (error) {
179df8bae1dSRodney W. Grimes 			vput(dvp);
180df8bae1dSRodney W. Grimes 			return (error);
181df8bae1dSRodney W. Grimes 		}
182df8bae1dSRodney W. Grimes 
183df8bae1dSRodney W. Grimes 		vput(dvp);
184df8bae1dSRodney W. Grimes 		dvp = tdvp;
185df8bae1dSRodney W. Grimes 	}
186df8bae1dSRodney W. Grimes 
187df8bae1dSRodney W. Grimes 	*vpp = dvp;
188df8bae1dSRodney W. Grimes 	return (0);
189df8bae1dSRodney W. Grimes }
190df8bae1dSRodney W. Grimes 
191c9bf0111SKATO Takenori static int
192df8bae1dSRodney W. Grimes union_lookup(ap)
193df8bae1dSRodney W. Grimes 	struct vop_lookup_args /* {
194df8bae1dSRodney W. Grimes 		struct vnodeop_desc *a_desc;
195df8bae1dSRodney W. Grimes 		struct vnode *a_dvp;
196df8bae1dSRodney W. Grimes 		struct vnode **a_vpp;
197df8bae1dSRodney W. Grimes 		struct componentname *a_cnp;
198df8bae1dSRodney W. Grimes 	} */ *ap;
199df8bae1dSRodney W. Grimes {
200df8bae1dSRodney W. Grimes 	int error;
201df8bae1dSRodney W. Grimes 	int uerror, lerror;
202df8bae1dSRodney W. Grimes 	struct vnode *uppervp, *lowervp;
203df8bae1dSRodney W. Grimes 	struct vnode *upperdvp, *lowerdvp;
204df8bae1dSRodney W. Grimes 	struct vnode *dvp = ap->a_dvp;
205df8bae1dSRodney W. Grimes 	struct union_node *dun = VTOUNION(dvp);
206df8bae1dSRodney W. Grimes 	struct componentname *cnp = ap->a_cnp;
207996c772fSJohn Dyson 	struct proc *p = cnp->cn_proc;
208df8bae1dSRodney W. Grimes 	int lockparent = cnp->cn_flags & LOCKPARENT;
209df8bae1dSRodney W. Grimes 	struct union_mount *um = MOUNTTOUNIONMOUNT(dvp->v_mount);
210996c772fSJohn Dyson 	struct ucred *saved_cred;
211996c772fSJohn Dyson 	int iswhiteout;
212996c772fSJohn Dyson 	struct vattr va;
213996c772fSJohn Dyson 
2146ca02614SKATO Takenori 
2156ca02614SKATO Takenori 	/*
2166ca02614SKATO Takenori 	 * Disallow write attemps to the filesystem mounted read-only.
2176ca02614SKATO Takenori 	 */
2186ca02614SKATO Takenori 	if ((cnp->cn_flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
2196ca02614SKATO Takenori 		(cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
2206ca02614SKATO Takenori 		return (EROFS);
2216ca02614SKATO Takenori 
222996c772fSJohn Dyson #ifdef notyet
223996c772fSJohn Dyson 	if (cnp->cn_namelen == 3 &&
224996c772fSJohn Dyson 			cnp->cn_nameptr[2] == '.' &&
225996c772fSJohn Dyson 			cnp->cn_nameptr[1] == '.' &&
226996c772fSJohn Dyson 			cnp->cn_nameptr[0] == '.') {
227996c772fSJohn Dyson 		dvp = *ap->a_vpp = LOWERVP(ap->a_dvp);
228996c772fSJohn Dyson 		if (dvp == NULLVP)
229996c772fSJohn Dyson 			return (ENOENT);
230996c772fSJohn Dyson 		VREF(dvp);
231996c772fSJohn Dyson 		vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, p);
232996c772fSJohn Dyson 		if (!lockparent || !(cnp->cn_flags & ISLASTCN))
233996c772fSJohn Dyson 			VOP_UNLOCK(ap->a_dvp, 0, p);
234996c772fSJohn Dyson 		return (0);
235996c772fSJohn Dyson 	}
236996c772fSJohn Dyson #endif
237df8bae1dSRodney W. Grimes 
238df8bae1dSRodney W. Grimes 	cnp->cn_flags |= LOCKPARENT;
239df8bae1dSRodney W. Grimes 
240df8bae1dSRodney W. Grimes 	upperdvp = dun->un_uppervp;
241df8bae1dSRodney W. Grimes 	lowerdvp = dun->un_lowervp;
242df8bae1dSRodney W. Grimes 	uppervp = NULLVP;
243df8bae1dSRodney W. Grimes 	lowervp = NULLVP;
244996c772fSJohn Dyson 	iswhiteout = 0;
245df8bae1dSRodney W. Grimes 
246afc2a558SKATO Takenori 	if (cnp->cn_flags & ISDOTDOT) {
247afc2a558SKATO Takenori 		if (upperdvp != NULL)
248afc2a558SKATO Takenori 			VREF(upperdvp);
249afc2a558SKATO Takenori 		if (lowerdvp != NULL)
250afc2a558SKATO Takenori 			VREF(lowerdvp);
251afc2a558SKATO Takenori 	}
252afc2a558SKATO Takenori 
253df8bae1dSRodney W. Grimes 	/*
254df8bae1dSRodney W. Grimes 	 * do the lookup in the upper level.
255df8bae1dSRodney W. Grimes 	 * if that level comsumes additional pathnames,
256df8bae1dSRodney W. Grimes 	 * then assume that something special is going
257df8bae1dSRodney W. Grimes 	 * on and just return that vnode.
258df8bae1dSRodney W. Grimes 	 */
259996c772fSJohn Dyson 	if (upperdvp != NULLVP) {
260996c772fSJohn Dyson 		FIXUP(dun, p);
261ee582cdfSKATO Takenori 		/*
262ee582cdfSKATO Takenori 		 * If we're doing `..' in the underlying filesystem,
263ee582cdfSKATO Takenori 		 * we must drop our lock on the union node before
264ee582cdfSKATO Takenori 		 * going up the tree in the lower file system--if we block
265ee582cdfSKATO Takenori 		 * on the lowervp lock, and that's held by someone else
266ee582cdfSKATO Takenori 		 * coming down the tree and who's waiting for our lock,
267ee582cdfSKATO Takenori 		 * we would be hosed.
268ee582cdfSKATO Takenori 		 */
269ee582cdfSKATO Takenori 		if (cnp->cn_flags & ISDOTDOT) {
270ee582cdfSKATO Takenori 			/* retain lock on underlying VP: */
271a9320ff3SKATO Takenori 			SETKLOCK(dun);
272ee582cdfSKATO Takenori 			VOP_UNLOCK(dvp, 0, p);
273a9320ff3SKATO Takenori 			CLEARKLOCK(dun);
274ee582cdfSKATO Takenori 		}
275996c772fSJohn Dyson 		uerror = union_lookup1(um->um_uppervp, &upperdvp,
276df8bae1dSRodney W. Grimes 					&uppervp, cnp);
2776ca02614SKATO Takenori 		/*
2786ca02614SKATO Takenori 		 * Disallow write attemps to the filesystem mounted read-only.
2796ca02614SKATO Takenori 		 */
2806ca02614SKATO Takenori 		if (uerror == EJUSTRETURN && (cnp->cn_flags & ISLASTCN) &&
2816ca02614SKATO Takenori 			(dvp->v_mount->mnt_flag & MNT_RDONLY) &&
2826ca02614SKATO Takenori 			(cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME)) {
2836ca02614SKATO Takenori 			if (!lockparent)
2846ca02614SKATO Takenori 				cnp->cn_flags &= ~LOCKPARENT;
2856ca02614SKATO Takenori 			return (EROFS);
2866ca02614SKATO Takenori 		}
2876ca02614SKATO Takenori 
288ee582cdfSKATO Takenori 		if (cnp->cn_flags & ISDOTDOT) {
289ee582cdfSKATO Takenori 			if (dun->un_uppervp == upperdvp) {
290ee582cdfSKATO Takenori 				/*
291ee582cdfSKATO Takenori 				 * We got the underlying bugger back locked...
292ee582cdfSKATO Takenori 				 * now take back the union node lock.  Since we
293ee582cdfSKATO Takenori 				 * hold the uppervp lock, we can diddle union
294ee582cdfSKATO Takenori 				 * locking flags at will. :)
295ee582cdfSKATO Takenori 				 */
296ee582cdfSKATO Takenori 				dun->un_flags |= UN_ULOCK;
297ee582cdfSKATO Takenori 			}
298ee582cdfSKATO Takenori 			/*
299ee582cdfSKATO Takenori 			 * If upperdvp got swapped out, it means we did
300ee582cdfSKATO Takenori 			 * some mount point magic, and we do not have
301ee582cdfSKATO Takenori 			 * dun->un_uppervp locked currently--so we get it
302ee582cdfSKATO Takenori 			 * locked here (don't set the UN_ULOCK flag).
303ee582cdfSKATO Takenori 			 */
304077f31beSKATO Takenori 			vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, p);
305ee582cdfSKATO Takenori 		}
306ee582cdfSKATO Takenori 
307df8bae1dSRodney W. Grimes 		/*if (uppervp == upperdvp)
308df8bae1dSRodney W. Grimes 			dun->un_flags |= UN_KLOCK;*/
309df8bae1dSRodney W. Grimes 
3103dc942bbSKATO Takenori 		if (cnp->cn_consume != 0) {
311df8bae1dSRodney W. Grimes 			*ap->a_vpp = uppervp;
312df8bae1dSRodney W. Grimes 			if (!lockparent)
313df8bae1dSRodney W. Grimes 				cnp->cn_flags &= ~LOCKPARENT;
314afc2a558SKATO Takenori 			error = uerror;
315afc2a558SKATO Takenori 			goto out;
316df8bae1dSRodney W. Grimes 		}
317996c772fSJohn Dyson 		if (uerror == ENOENT || uerror == EJUSTRETURN) {
318996c772fSJohn Dyson 			if (cnp->cn_flags & ISWHITEOUT) {
319996c772fSJohn Dyson 				iswhiteout = 1;
320996c772fSJohn Dyson 			} else if (lowerdvp != NULLVP) {
321996c772fSJohn Dyson 				lerror = VOP_GETATTR(upperdvp, &va,
322996c772fSJohn Dyson 					cnp->cn_cred, cnp->cn_proc);
323996c772fSJohn Dyson 				if (lerror == 0 && (va.va_flags & OPAQUE))
324996c772fSJohn Dyson 					iswhiteout = 1;
325996c772fSJohn Dyson 			}
326996c772fSJohn Dyson 		}
327df8bae1dSRodney W. Grimes 	} else {
328df8bae1dSRodney W. Grimes 		uerror = ENOENT;
329df8bae1dSRodney W. Grimes 	}
330df8bae1dSRodney W. Grimes 
331df8bae1dSRodney W. Grimes 	/*
332df8bae1dSRodney W. Grimes 	 * in a similar way to the upper layer, do the lookup
333df8bae1dSRodney W. Grimes 	 * in the lower layer.   this time, if there is some
334df8bae1dSRodney W. Grimes 	 * component magic going on, then vput whatever we got
335df8bae1dSRodney W. Grimes 	 * back from the upper layer and return the lower vnode
336df8bae1dSRodney W. Grimes 	 * instead.
337df8bae1dSRodney W. Grimes 	 */
338996c772fSJohn Dyson 	if (lowerdvp != NULLVP && !iswhiteout) {
339df8bae1dSRodney W. Grimes 		int nameiop;
340df8bae1dSRodney W. Grimes 
341996c772fSJohn Dyson 		vn_lock(lowerdvp, LK_EXCLUSIVE | LK_RETRY, p);
342df8bae1dSRodney W. Grimes 
343df8bae1dSRodney W. Grimes 		/*
344df8bae1dSRodney W. Grimes 		 * Only do a LOOKUP on the bottom node, since
345df8bae1dSRodney W. Grimes 		 * we won't be making changes to it anyway.
346df8bae1dSRodney W. Grimes 		 */
347df8bae1dSRodney W. Grimes 		nameiop = cnp->cn_nameiop;
348df8bae1dSRodney W. Grimes 		cnp->cn_nameiop = LOOKUP;
349df8bae1dSRodney W. Grimes 		if (um->um_op == UNMNT_BELOW) {
350df8bae1dSRodney W. Grimes 			saved_cred = cnp->cn_cred;
351df8bae1dSRodney W. Grimes 			cnp->cn_cred = um->um_cred;
352df8bae1dSRodney W. Grimes 		}
353ee582cdfSKATO Takenori 		/*
354ee582cdfSKATO Takenori 		 * We shouldn't have to worry about locking interactions
355ee582cdfSKATO Takenori 		 * between the lower layer and our union layer (w.r.t.
356ee582cdfSKATO Takenori 		 * `..' processing) because we don't futz with lowervp
357ee582cdfSKATO Takenori 		 * locks in the union-node instantiation code path.
358ee582cdfSKATO Takenori 		 */
359996c772fSJohn Dyson 		lerror = union_lookup1(um->um_lowervp, &lowerdvp,
360df8bae1dSRodney W. Grimes 				&lowervp, cnp);
361df8bae1dSRodney W. Grimes 		if (um->um_op == UNMNT_BELOW)
362df8bae1dSRodney W. Grimes 			cnp->cn_cred = saved_cred;
363df8bae1dSRodney W. Grimes 		cnp->cn_nameiop = nameiop;
364df8bae1dSRodney W. Grimes 
365df8bae1dSRodney W. Grimes 		if (lowervp != lowerdvp)
366996c772fSJohn Dyson 			VOP_UNLOCK(lowerdvp, 0, p);
367df8bae1dSRodney W. Grimes 
368f8fc96b5SKATO Takenori 		if (cnp->cn_consume != 0 || lerror == EACCES) {
369f8fc96b5SKATO Takenori 			if (lerror == EACCES)
370f8fc96b5SKATO Takenori 				lowervp = NULLVP;
371996c772fSJohn Dyson 			if (uppervp != NULLVP) {
372df8bae1dSRodney W. Grimes 				if (uppervp == upperdvp)
373df8bae1dSRodney W. Grimes 					vrele(uppervp);
374df8bae1dSRodney W. Grimes 				else
375df8bae1dSRodney W. Grimes 					vput(uppervp);
376df8bae1dSRodney W. Grimes 				uppervp = NULLVP;
377df8bae1dSRodney W. Grimes 			}
378df8bae1dSRodney W. Grimes 			*ap->a_vpp = lowervp;
379df8bae1dSRodney W. Grimes 			if (!lockparent)
380df8bae1dSRodney W. Grimes 				cnp->cn_flags &= ~LOCKPARENT;
381afc2a558SKATO Takenori 			error = lerror;
382afc2a558SKATO Takenori 			goto out;
383df8bae1dSRodney W. Grimes 		}
384df8bae1dSRodney W. Grimes 	} else {
385df8bae1dSRodney W. Grimes 		lerror = ENOENT;
386996c772fSJohn Dyson 		if ((cnp->cn_flags & ISDOTDOT) && dun->un_pvp != NULLVP) {
387996c772fSJohn Dyson 			lowervp = LOWERVP(dun->un_pvp);
388996c772fSJohn Dyson 			if (lowervp != NULLVP) {
389996c772fSJohn Dyson 				VREF(lowervp);
390996c772fSJohn Dyson 				vn_lock(lowervp, LK_EXCLUSIVE | LK_RETRY, p);
391996c772fSJohn Dyson 				lerror = 0;
392996c772fSJohn Dyson 			}
393996c772fSJohn Dyson 		}
394df8bae1dSRodney W. Grimes 	}
395df8bae1dSRodney W. Grimes 
396df8bae1dSRodney W. Grimes 	if (!lockparent)
397df8bae1dSRodney W. Grimes 		cnp->cn_flags &= ~LOCKPARENT;
398df8bae1dSRodney W. Grimes 
399df8bae1dSRodney W. Grimes 	/*
400df8bae1dSRodney W. Grimes 	 * at this point, we have uerror and lerror indicating
401df8bae1dSRodney W. Grimes 	 * possible errors with the lookups in the upper and lower
402df8bae1dSRodney W. Grimes 	 * layers.  additionally, uppervp and lowervp are (locked)
403df8bae1dSRodney W. Grimes 	 * references to existing vnodes in the upper and lower layers.
404df8bae1dSRodney W. Grimes 	 *
405df8bae1dSRodney W. Grimes 	 * there are now three cases to consider.
406df8bae1dSRodney W. Grimes 	 * 1. if both layers returned an error, then return whatever
407df8bae1dSRodney W. Grimes 	 *    error the upper layer generated.
408df8bae1dSRodney W. Grimes 	 *
409df8bae1dSRodney W. Grimes 	 * 2. if the top layer failed and the bottom layer succeeded
410df8bae1dSRodney W. Grimes 	 *    then two subcases occur.
411df8bae1dSRodney W. Grimes 	 *    a.  the bottom vnode is not a directory, in which
412df8bae1dSRodney W. Grimes 	 *	  case just return a new union vnode referencing
413df8bae1dSRodney W. Grimes 	 *	  an empty top layer and the existing bottom layer.
414df8bae1dSRodney W. Grimes 	 *    b.  the bottom vnode is a directory, in which case
415df8bae1dSRodney W. Grimes 	 *	  create a new directory in the top-level and
416df8bae1dSRodney W. Grimes 	 *	  continue as in case 3.
417df8bae1dSRodney W. Grimes 	 *
418df8bae1dSRodney W. Grimes 	 * 3. if the top layer succeeded then return a new union
419df8bae1dSRodney W. Grimes 	 *    vnode referencing whatever the new top layer and
420df8bae1dSRodney W. Grimes 	 *    whatever the bottom layer returned.
421df8bae1dSRodney W. Grimes 	 */
422df8bae1dSRodney W. Grimes 
423df8bae1dSRodney W. Grimes 	*ap->a_vpp = NULLVP;
424df8bae1dSRodney W. Grimes 
425df8bae1dSRodney W. Grimes 	/* case 1. */
426df8bae1dSRodney W. Grimes 	if ((uerror != 0) && (lerror != 0)) {
427afc2a558SKATO Takenori 		error = uerror;
428afc2a558SKATO Takenori 		goto out;
429df8bae1dSRodney W. Grimes 	}
430df8bae1dSRodney W. Grimes 
431df8bae1dSRodney W. Grimes 	/* case 2. */
432df8bae1dSRodney W. Grimes 	if (uerror != 0 /* && (lerror == 0) */ ) {
433df8bae1dSRodney W. Grimes 		if (lowervp->v_type == VDIR) { /* case 2b. */
434df8bae1dSRodney W. Grimes 			dun->un_flags &= ~UN_ULOCK;
435996c772fSJohn Dyson 			VOP_UNLOCK(upperdvp, 0, p);
436df8bae1dSRodney W. Grimes 			uerror = union_mkshadow(um, upperdvp, cnp, &uppervp);
437996c772fSJohn Dyson 			vn_lock(upperdvp, LK_EXCLUSIVE | LK_RETRY, p);
438df8bae1dSRodney W. Grimes 			dun->un_flags |= UN_ULOCK;
439df8bae1dSRodney W. Grimes 
440df8bae1dSRodney W. Grimes 			if (uerror) {
441996c772fSJohn Dyson 				if (lowervp != NULLVP) {
442df8bae1dSRodney W. Grimes 					vput(lowervp);
443df8bae1dSRodney W. Grimes 					lowervp = NULLVP;
444df8bae1dSRodney W. Grimes 				}
445afc2a558SKATO Takenori 				error = uerror;
446afc2a558SKATO Takenori 				goto out;
447df8bae1dSRodney W. Grimes 			}
448df8bae1dSRodney W. Grimes 		}
449df8bae1dSRodney W. Grimes 	}
450df8bae1dSRodney W. Grimes 
451996c772fSJohn Dyson 	if (lowervp != NULLVP)
452996c772fSJohn Dyson 		VOP_UNLOCK(lowervp, 0, p);
453df8bae1dSRodney W. Grimes 
454df8bae1dSRodney W. Grimes 	error = union_allocvp(ap->a_vpp, dvp->v_mount, dvp, upperdvp, cnp,
455996c772fSJohn Dyson 			      uppervp, lowervp, 1);
456df8bae1dSRodney W. Grimes 
457df8bae1dSRodney W. Grimes 	if (error) {
458996c772fSJohn Dyson 		if (uppervp != NULLVP)
459df8bae1dSRodney W. Grimes 			vput(uppervp);
460996c772fSJohn Dyson 		if (lowervp != NULLVP)
461df8bae1dSRodney W. Grimes 			vrele(lowervp);
462df8bae1dSRodney W. Grimes 	} else {
463df8bae1dSRodney W. Grimes 		if (*ap->a_vpp != dvp)
464df8bae1dSRodney W. Grimes 			if (!lockparent || !(cnp->cn_flags & ISLASTCN))
465996c772fSJohn Dyson 				VOP_UNLOCK(dvp, 0, p);
466f5a5311eSKATO Takenori #ifdef DIAGNOSTIC
467f5a5311eSKATO Takenori         if (cnp->cn_namelen == 1 &&
468f5a5311eSKATO Takenori             cnp->cn_nameptr[0] == '.' &&
469f5a5311eSKATO Takenori             *ap->a_vpp != dvp) {
470f5a5311eSKATO Takenori             panic("union_lookup returning . (%p) not same as startdir (%p)",
471f5a5311eSKATO Takenori 				  ap->a_vpp, dvp);
472f5a5311eSKATO Takenori 		}
473f5a5311eSKATO Takenori #endif
474df8bae1dSRodney W. Grimes 	}
475df8bae1dSRodney W. Grimes 
476afc2a558SKATO Takenori out:
477afc2a558SKATO Takenori     if (cnp->cn_flags & ISDOTDOT) {
478afc2a558SKATO Takenori         if (upperdvp != NULL)
479afc2a558SKATO Takenori             vrele(upperdvp);
480afc2a558SKATO Takenori         if (lowerdvp != NULL)
481afc2a558SKATO Takenori             vrele(lowerdvp);
482afc2a558SKATO Takenori     }
483afc2a558SKATO Takenori 
484df8bae1dSRodney W. Grimes 	return (error);
485df8bae1dSRodney W. Grimes }
486df8bae1dSRodney W. Grimes 
487c9bf0111SKATO Takenori static int
488df8bae1dSRodney W. Grimes union_create(ap)
489df8bae1dSRodney W. Grimes 	struct vop_create_args /* {
490df8bae1dSRodney W. Grimes 		struct vnode *a_dvp;
491df8bae1dSRodney W. Grimes 		struct vnode **a_vpp;
492df8bae1dSRodney W. Grimes 		struct componentname *a_cnp;
493df8bae1dSRodney W. Grimes 		struct vattr *a_vap;
494df8bae1dSRodney W. Grimes 	} */ *ap;
495df8bae1dSRodney W. Grimes {
496df8bae1dSRodney W. Grimes 	struct union_node *un = VTOUNION(ap->a_dvp);
497df8bae1dSRodney W. Grimes 	struct vnode *dvp = un->un_uppervp;
498996c772fSJohn Dyson 	struct componentname *cnp = ap->a_cnp;
499996c772fSJohn Dyson 	struct proc *p = cnp->cn_proc;
500df8bae1dSRodney W. Grimes 
501996c772fSJohn Dyson 	if (dvp != NULLVP) {
502df8bae1dSRodney W. Grimes 		int error;
503df8bae1dSRodney W. Grimes 		struct vnode *vp;
504996c772fSJohn Dyson 		struct mount *mp;
505df8bae1dSRodney W. Grimes 
506996c772fSJohn Dyson 		FIXUP(un, p);
507df8bae1dSRodney W. Grimes 
508df8bae1dSRodney W. Grimes 		VREF(dvp);
509a9320ff3SKATO Takenori 		SETKLOCK(un);
510996c772fSJohn Dyson 		mp = ap->a_dvp->v_mount;
511df8bae1dSRodney W. Grimes 		vput(ap->a_dvp);
512a9320ff3SKATO Takenori 		CLEARKLOCK(un);
513996c772fSJohn Dyson 		error = VOP_CREATE(dvp, &vp, cnp, ap->a_vap);
514df8bae1dSRodney W. Grimes 		if (error)
515df8bae1dSRodney W. Grimes 			return (error);
516df8bae1dSRodney W. Grimes 
517996c772fSJohn Dyson 		error = union_allocvp(ap->a_vpp, mp, NULLVP, NULLVP, cnp, vp,
518996c772fSJohn Dyson 				NULLVP, 1);
519df8bae1dSRodney W. Grimes 		if (error)
520df8bae1dSRodney W. Grimes 			vput(vp);
521df8bae1dSRodney W. Grimes 		return (error);
522df8bae1dSRodney W. Grimes 	}
523df8bae1dSRodney W. Grimes 
524df8bae1dSRodney W. Grimes 	vput(ap->a_dvp);
525df8bae1dSRodney W. Grimes 	return (EROFS);
526df8bae1dSRodney W. Grimes }
527df8bae1dSRodney W. Grimes 
528c9bf0111SKATO Takenori static int
529996c772fSJohn Dyson union_whiteout(ap)
530996c772fSJohn Dyson 	struct vop_whiteout_args /* {
531996c772fSJohn Dyson 		struct vnode *a_dvp;
532996c772fSJohn Dyson 		struct componentname *a_cnp;
533996c772fSJohn Dyson 		int a_flags;
534996c772fSJohn Dyson 	} */ *ap;
535996c772fSJohn Dyson {
536996c772fSJohn Dyson 	struct union_node *un = VTOUNION(ap->a_dvp);
537996c772fSJohn Dyson 	struct componentname *cnp = ap->a_cnp;
538996c772fSJohn Dyson 	struct proc *p = cnp->cn_proc;
539996c772fSJohn Dyson 
540996c772fSJohn Dyson 	if (un->un_uppervp == NULLVP)
541996c772fSJohn Dyson 		return (EOPNOTSUPP);
542996c772fSJohn Dyson 
543996c772fSJohn Dyson 	FIXUP(un, p);
544996c772fSJohn Dyson 	return (VOP_WHITEOUT(un->un_uppervp, cnp, ap->a_flags));
545996c772fSJohn Dyson }
546996c772fSJohn Dyson 
547c9bf0111SKATO Takenori static int
548df8bae1dSRodney W. Grimes union_mknod(ap)
549df8bae1dSRodney W. Grimes 	struct vop_mknod_args /* {
550df8bae1dSRodney W. Grimes 		struct vnode *a_dvp;
551df8bae1dSRodney W. Grimes 		struct vnode **a_vpp;
552df8bae1dSRodney W. Grimes 		struct componentname *a_cnp;
553df8bae1dSRodney W. Grimes 		struct vattr *a_vap;
554df8bae1dSRodney W. Grimes 	} */ *ap;
555df8bae1dSRodney W. Grimes {
556df8bae1dSRodney W. Grimes 	struct union_node *un = VTOUNION(ap->a_dvp);
557df8bae1dSRodney W. Grimes 	struct vnode *dvp = un->un_uppervp;
558996c772fSJohn Dyson 	struct componentname *cnp = ap->a_cnp;
559996c772fSJohn Dyson 	struct proc *p = cnp->cn_proc;
560df8bae1dSRodney W. Grimes 
561996c772fSJohn Dyson 	if (dvp != NULLVP) {
562df8bae1dSRodney W. Grimes 		int error;
563df8bae1dSRodney W. Grimes 		struct vnode *vp;
564996c772fSJohn Dyson 		struct mount *mp;
565df8bae1dSRodney W. Grimes 
566996c772fSJohn Dyson 		FIXUP(un, p);
567df8bae1dSRodney W. Grimes 
568df8bae1dSRodney W. Grimes 		VREF(dvp);
569a9320ff3SKATO Takenori 		SETKLOCK(un);
570996c772fSJohn Dyson 		mp = ap->a_dvp->v_mount;
571df8bae1dSRodney W. Grimes 		vput(ap->a_dvp);
572a9320ff3SKATO Takenori 		CLEARKLOCK(un);
573996c772fSJohn Dyson 		error = VOP_MKNOD(dvp, &vp, cnp, ap->a_vap);
574df8bae1dSRodney W. Grimes 		if (error)
575df8bae1dSRodney W. Grimes 			return (error);
576df8bae1dSRodney W. Grimes 
577996c772fSJohn Dyson 		if (vp != NULLVP) {
578996c772fSJohn Dyson 			error = union_allocvp(ap->a_vpp, mp, NULLVP, NULLVP,
579996c772fSJohn Dyson 					cnp, vp, NULLVP, 1);
580df8bae1dSRodney W. Grimes 			if (error)
581df8bae1dSRodney W. Grimes 				vput(vp);
582df8bae1dSRodney W. Grimes 		}
583df8bae1dSRodney W. Grimes 		return (error);
584df8bae1dSRodney W. Grimes 	}
585df8bae1dSRodney W. Grimes 
586df8bae1dSRodney W. Grimes 	vput(ap->a_dvp);
587df8bae1dSRodney W. Grimes 	return (EROFS);
588df8bae1dSRodney W. Grimes }
589df8bae1dSRodney W. Grimes 
590c9bf0111SKATO Takenori static int
591df8bae1dSRodney W. Grimes union_open(ap)
592df8bae1dSRodney W. Grimes 	struct vop_open_args /* {
593df8bae1dSRodney W. Grimes 		struct vnodeop_desc *a_desc;
594df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
595df8bae1dSRodney W. Grimes 		int a_mode;
596df8bae1dSRodney W. Grimes 		struct ucred *a_cred;
597df8bae1dSRodney W. Grimes 		struct proc *a_p;
598df8bae1dSRodney W. Grimes 	} */ *ap;
599df8bae1dSRodney W. Grimes {
600df8bae1dSRodney W. Grimes 	struct union_node *un = VTOUNION(ap->a_vp);
601df8bae1dSRodney W. Grimes 	struct vnode *tvp;
602df8bae1dSRodney W. Grimes 	int mode = ap->a_mode;
603df8bae1dSRodney W. Grimes 	struct ucred *cred = ap->a_cred;
604df8bae1dSRodney W. Grimes 	struct proc *p = ap->a_p;
605df8bae1dSRodney W. Grimes 	int error;
606df8bae1dSRodney W. Grimes 
607df8bae1dSRodney W. Grimes 	/*
608df8bae1dSRodney W. Grimes 	 * If there is an existing upper vp then simply open that.
609df8bae1dSRodney W. Grimes 	 */
610df8bae1dSRodney W. Grimes 	tvp = un->un_uppervp;
611df8bae1dSRodney W. Grimes 	if (tvp == NULLVP) {
612df8bae1dSRodney W. Grimes 		/*
613df8bae1dSRodney W. Grimes 		 * If the lower vnode is being opened for writing, then
614df8bae1dSRodney W. Grimes 		 * copy the file contents to the upper vnode and open that,
615df8bae1dSRodney W. Grimes 		 * otherwise can simply open the lower vnode.
616df8bae1dSRodney W. Grimes 		 */
617df8bae1dSRodney W. Grimes 		tvp = un->un_lowervp;
618df8bae1dSRodney W. Grimes 		if ((ap->a_mode & FWRITE) && (tvp->v_type == VREG)) {
619996c772fSJohn Dyson 			error = union_copyup(un, (mode&O_TRUNC) == 0, cred, p);
620df8bae1dSRodney W. Grimes 			if (error == 0)
621df8bae1dSRodney W. Grimes 				error = VOP_OPEN(un->un_uppervp, mode, cred, p);
622df8bae1dSRodney W. Grimes 			return (error);
623df8bae1dSRodney W. Grimes 		}
624df8bae1dSRodney W. Grimes 
625df8bae1dSRodney W. Grimes 		/*
626df8bae1dSRodney W. Grimes 		 * Just open the lower vnode
627df8bae1dSRodney W. Grimes 		 */
628df8bae1dSRodney W. Grimes 		un->un_openl++;
629996c772fSJohn Dyson 		vn_lock(tvp, LK_EXCLUSIVE | LK_RETRY, p);
630df8bae1dSRodney W. Grimes 		error = VOP_OPEN(tvp, mode, cred, p);
631996c772fSJohn Dyson 		VOP_UNLOCK(tvp, 0, p);
632df8bae1dSRodney W. Grimes 
633df8bae1dSRodney W. Grimes 		return (error);
634df8bae1dSRodney W. Grimes 	}
635df8bae1dSRodney W. Grimes 
636996c772fSJohn Dyson 	FIXUP(un, p);
637df8bae1dSRodney W. Grimes 
638df8bae1dSRodney W. Grimes 	error = VOP_OPEN(tvp, mode, cred, p);
639df8bae1dSRodney W. Grimes 
640df8bae1dSRodney W. Grimes 	return (error);
641df8bae1dSRodney W. Grimes }
642df8bae1dSRodney W. Grimes 
643c9bf0111SKATO Takenori static int
644df8bae1dSRodney W. Grimes union_close(ap)
645df8bae1dSRodney W. Grimes 	struct vop_close_args /* {
646df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
647df8bae1dSRodney W. Grimes 		int  a_fflag;
648df8bae1dSRodney W. Grimes 		struct ucred *a_cred;
649df8bae1dSRodney W. Grimes 		struct proc *a_p;
650df8bae1dSRodney W. Grimes 	} */ *ap;
651df8bae1dSRodney W. Grimes {
652df8bae1dSRodney W. Grimes 	struct union_node *un = VTOUNION(ap->a_vp);
653df8bae1dSRodney W. Grimes 	struct vnode *vp;
654df8bae1dSRodney W. Grimes 
655996c772fSJohn Dyson 	if ((vp = un->un_uppervp) == NULLVP) {
656df8bae1dSRodney W. Grimes #ifdef UNION_DIAGNOSTIC
657df8bae1dSRodney W. Grimes 		if (un->un_openl <= 0)
658df8bae1dSRodney W. Grimes 			panic("union: un_openl cnt");
659df8bae1dSRodney W. Grimes #endif
660df8bae1dSRodney W. Grimes 		--un->un_openl;
661df8bae1dSRodney W. Grimes 		vp = un->un_lowervp;
662df8bae1dSRodney W. Grimes 	}
663df8bae1dSRodney W. Grimes 
664996c772fSJohn Dyson 	ap->a_vp = vp;
665996c772fSJohn Dyson 	return (VCALL(vp, VOFFSET(vop_close), ap));
666df8bae1dSRodney W. Grimes }
667df8bae1dSRodney W. Grimes 
668df8bae1dSRodney W. Grimes /*
669df8bae1dSRodney W. Grimes  * Check access permission on the union vnode.
670df8bae1dSRodney W. Grimes  * The access check being enforced is to check
671df8bae1dSRodney W. Grimes  * against both the underlying vnode, and any
672df8bae1dSRodney W. Grimes  * copied vnode.  This ensures that no additional
673df8bae1dSRodney W. Grimes  * file permissions are given away simply because
674df8bae1dSRodney W. Grimes  * the user caused an implicit file copy.
675df8bae1dSRodney W. Grimes  */
676c9bf0111SKATO Takenori static int
677df8bae1dSRodney W. Grimes union_access(ap)
678df8bae1dSRodney W. Grimes 	struct vop_access_args /* {
679df8bae1dSRodney W. Grimes 		struct vnodeop_desc *a_desc;
680df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
681df8bae1dSRodney W. Grimes 		int a_mode;
682df8bae1dSRodney W. Grimes 		struct ucred *a_cred;
683df8bae1dSRodney W. Grimes 		struct proc *a_p;
684df8bae1dSRodney W. Grimes 	} */ *ap;
685df8bae1dSRodney W. Grimes {
686df8bae1dSRodney W. Grimes 	struct union_node *un = VTOUNION(ap->a_vp);
687996c772fSJohn Dyson 	struct proc *p = ap->a_p;
688df8bae1dSRodney W. Grimes 	int error = EACCES;
689df8bae1dSRodney W. Grimes 	struct vnode *vp;
6905f3f68a8SKATO Takenori 	struct vnode *savedvp;
691df8bae1dSRodney W. Grimes 
6926ca02614SKATO Takenori 	/*
6936ca02614SKATO Takenori 	 * Disallow write attempts on filesystems mounted read-only.
6946ca02614SKATO Takenori 	 */
6956ca02614SKATO Takenori 	if (ap->a_mode & VWRITE && (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)) {
6966ca02614SKATO Takenori 		switch (ap->a_vp->v_type) {
6976ca02614SKATO Takenori 		case VREG: case VDIR: case VLNK:
6986ca02614SKATO Takenori 			return (EROFS);
6996ca02614SKATO Takenori 		}
7006ca02614SKATO Takenori 	}
701996c772fSJohn Dyson 	if ((vp = un->un_uppervp) != NULLVP) {
702996c772fSJohn Dyson 		FIXUP(un, p);
703996c772fSJohn Dyson 		ap->a_vp = vp;
704996c772fSJohn Dyson 		return (VCALL(vp, VOFFSET(vop_access), ap));
705df8bae1dSRodney W. Grimes 	}
706df8bae1dSRodney W. Grimes 
707996c772fSJohn Dyson 	if ((vp = un->un_lowervp) != NULLVP) {
708996c772fSJohn Dyson 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
7095f3f68a8SKATO Takenori 		savedvp = ap->a_vp;
710996c772fSJohn Dyson 		ap->a_vp = vp;
711996c772fSJohn Dyson 		error = VCALL(vp, VOFFSET(vop_access), ap);
712df8bae1dSRodney W. Grimes 		if (error == 0) {
7135f3f68a8SKATO Takenori 			struct union_mount *um = MOUNTTOUNIONMOUNT(savedvp->v_mount);
714df8bae1dSRodney W. Grimes 
715996c772fSJohn Dyson 			if (um->um_op == UNMNT_BELOW) {
716996c772fSJohn Dyson 				ap->a_cred = um->um_cred;
717996c772fSJohn Dyson 				error = VCALL(vp, VOFFSET(vop_access), ap);
718df8bae1dSRodney W. Grimes 			}
719996c772fSJohn Dyson 		}
720996c772fSJohn Dyson 		VOP_UNLOCK(vp, 0, p);
721df8bae1dSRodney W. Grimes 		if (error)
722df8bae1dSRodney W. Grimes 			return (error);
723df8bae1dSRodney W. Grimes 	}
724df8bae1dSRodney W. Grimes 
725df8bae1dSRodney W. Grimes 	return (error);
726df8bae1dSRodney W. Grimes }
727df8bae1dSRodney W. Grimes 
728df8bae1dSRodney W. Grimes /*
729996c772fSJohn Dyson  * We handle getattr only to change the fsid and
730996c772fSJohn Dyson  * track object sizes
731df8bae1dSRodney W. Grimes  */
732c9bf0111SKATO Takenori static int
733df8bae1dSRodney W. Grimes union_getattr(ap)
734df8bae1dSRodney W. Grimes 	struct vop_getattr_args /* {
735df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
736df8bae1dSRodney W. Grimes 		struct vattr *a_vap;
737df8bae1dSRodney W. Grimes 		struct ucred *a_cred;
738df8bae1dSRodney W. Grimes 		struct proc *a_p;
739df8bae1dSRodney W. Grimes 	} */ *ap;
740df8bae1dSRodney W. Grimes {
741df8bae1dSRodney W. Grimes 	int error;
742df8bae1dSRodney W. Grimes 	struct union_node *un = VTOUNION(ap->a_vp);
743df8bae1dSRodney W. Grimes 	struct vnode *vp = un->un_uppervp;
744996c772fSJohn Dyson 	struct proc *p = ap->a_p;
745df8bae1dSRodney W. Grimes 	struct vattr *vap;
746df8bae1dSRodney W. Grimes 	struct vattr va;
747df8bae1dSRodney W. Grimes 
748df8bae1dSRodney W. Grimes 
749df8bae1dSRodney W. Grimes 	/*
750df8bae1dSRodney W. Grimes 	 * Some programs walk the filesystem hierarchy by counting
751df8bae1dSRodney W. Grimes 	 * links to directories to avoid stat'ing all the time.
752df8bae1dSRodney W. Grimes 	 * This means the link count on directories needs to be "correct".
753df8bae1dSRodney W. Grimes 	 * The only way to do that is to call getattr on both layers
754df8bae1dSRodney W. Grimes 	 * and fix up the link count.  The link count will not necessarily
755df8bae1dSRodney W. Grimes 	 * be accurate but will be large enough to defeat the tree walkers.
756df8bae1dSRodney W. Grimes 	 */
757df8bae1dSRodney W. Grimes 
758df8bae1dSRodney W. Grimes 	vap = ap->a_vap;
759df8bae1dSRodney W. Grimes 
760df8bae1dSRodney W. Grimes 	vp = un->un_uppervp;
761df8bae1dSRodney W. Grimes 	if (vp != NULLVP) {
762996c772fSJohn Dyson 		/*
763996c772fSJohn Dyson 		 * It's not clear whether VOP_GETATTR is to be
764996c772fSJohn Dyson 		 * called with the vnode locked or not.  stat() calls
765996c772fSJohn Dyson 		 * it with (vp) locked, and fstat calls it with
766996c772fSJohn Dyson 		 * (vp) unlocked.
767996c772fSJohn Dyson 		 * In the mean time, compensate here by checking
768996c772fSJohn Dyson 		 * the union_node's lock flag.
769996c772fSJohn Dyson 		 */
770996c772fSJohn Dyson 		if (un->un_flags & UN_LOCKED)
771996c772fSJohn Dyson 			FIXUP(un, p);
772996c772fSJohn Dyson 
773df8bae1dSRodney W. Grimes 		error = VOP_GETATTR(vp, vap, ap->a_cred, ap->a_p);
774df8bae1dSRodney W. Grimes 		if (error)
775df8bae1dSRodney W. Grimes 			return (error);
776996c772fSJohn Dyson 		union_newsize(ap->a_vp, vap->va_size, VNOVAL);
777df8bae1dSRodney W. Grimes 	}
778df8bae1dSRodney W. Grimes 
779df8bae1dSRodney W. Grimes 	if (vp == NULLVP) {
780df8bae1dSRodney W. Grimes 		vp = un->un_lowervp;
781df8bae1dSRodney W. Grimes 	} else if (vp->v_type == VDIR) {
782df8bae1dSRodney W. Grimes 		vp = un->un_lowervp;
783df8bae1dSRodney W. Grimes 		vap = &va;
784df8bae1dSRodney W. Grimes 	} else {
785df8bae1dSRodney W. Grimes 		vp = NULLVP;
786df8bae1dSRodney W. Grimes 	}
787df8bae1dSRodney W. Grimes 
788df8bae1dSRodney W. Grimes 	if (vp != NULLVP) {
789df8bae1dSRodney W. Grimes 		error = VOP_GETATTR(vp, vap, ap->a_cred, ap->a_p);
790df8bae1dSRodney W. Grimes 		if (error)
791df8bae1dSRodney W. Grimes 			return (error);
792996c772fSJohn Dyson 		union_newsize(ap->a_vp, VNOVAL, vap->va_size);
793df8bae1dSRodney W. Grimes 	}
794df8bae1dSRodney W. Grimes 
795df8bae1dSRodney W. Grimes 	if ((vap != ap->a_vap) && (vap->va_type == VDIR))
796df8bae1dSRodney W. Grimes 		ap->a_vap->va_nlink += vap->va_nlink;
797df8bae1dSRodney W. Grimes 
798996c772fSJohn Dyson 	ap->a_vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsid.val[0];
799df8bae1dSRodney W. Grimes 	return (0);
800df8bae1dSRodney W. Grimes }
801df8bae1dSRodney W. Grimes 
802c9bf0111SKATO Takenori static int
803df8bae1dSRodney W. Grimes union_setattr(ap)
804df8bae1dSRodney W. Grimes 	struct vop_setattr_args /* {
805df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
806df8bae1dSRodney W. Grimes 		struct vattr *a_vap;
807df8bae1dSRodney W. Grimes 		struct ucred *a_cred;
808df8bae1dSRodney W. Grimes 		struct proc *a_p;
809df8bae1dSRodney W. Grimes 	} */ *ap;
810df8bae1dSRodney W. Grimes {
811df8bae1dSRodney W. Grimes 	struct union_node *un = VTOUNION(ap->a_vp);
812996c772fSJohn Dyson 	struct proc *p = ap->a_p;
8136ca02614SKATO Takenori 	struct vattr *vap = ap->a_vap;
814df8bae1dSRodney W. Grimes 	int error;
815df8bae1dSRodney W. Grimes 
816df8bae1dSRodney W. Grimes 	/*
8176ca02614SKATO Takenori 	 * Disallow write attempts on filesystems mounted read-only.
8186ca02614SKATO Takenori 	 */
8196ca02614SKATO Takenori 	if ((ap->a_vp->v_mount->mnt_flag & MNT_RDONLY) &&
8206ca02614SKATO Takenori 		(vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
8216ca02614SKATO Takenori 		 vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
8226ca02614SKATO Takenori 		 vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL))
8236ca02614SKATO Takenori 		return (EROFS);
8246ca02614SKATO Takenori 
8256ca02614SKATO Takenori 	/*
826df8bae1dSRodney W. Grimes 	 * Handle case of truncating lower object to zero size,
827df8bae1dSRodney W. Grimes 	 * by creating a zero length upper object.  This is to
828df8bae1dSRodney W. Grimes 	 * handle the case of open with O_TRUNC and O_CREAT.
829df8bae1dSRodney W. Grimes 	 */
830df8bae1dSRodney W. Grimes 	if ((un->un_uppervp == NULLVP) &&
831df8bae1dSRodney W. Grimes 	    /* assert(un->un_lowervp != NULLVP) */
832996c772fSJohn Dyson 	    (un->un_lowervp->v_type == VREG)) {
833996c772fSJohn Dyson 		error = union_copyup(un, (ap->a_vap->va_size != 0),
834996c772fSJohn Dyson 						ap->a_cred, ap->a_p);
835df8bae1dSRodney W. Grimes 		if (error)
836df8bae1dSRodney W. Grimes 			return (error);
837df8bae1dSRodney W. Grimes 	}
838df8bae1dSRodney W. Grimes 
839df8bae1dSRodney W. Grimes 	/*
840df8bae1dSRodney W. Grimes 	 * Try to set attributes in upper layer,
841df8bae1dSRodney W. Grimes 	 * otherwise return read-only filesystem error.
842df8bae1dSRodney W. Grimes 	 */
843df8bae1dSRodney W. Grimes 	if (un->un_uppervp != NULLVP) {
844996c772fSJohn Dyson 		FIXUP(un, p);
845df8bae1dSRodney W. Grimes 		error = VOP_SETATTR(un->un_uppervp, ap->a_vap,
846df8bae1dSRodney W. Grimes 					ap->a_cred, ap->a_p);
847996c772fSJohn Dyson 		if ((error == 0) && (ap->a_vap->va_size != VNOVAL))
848996c772fSJohn Dyson 			union_newsize(ap->a_vp, ap->a_vap->va_size, VNOVAL);
849df8bae1dSRodney W. Grimes 	} else {
850df8bae1dSRodney W. Grimes 		error = EROFS;
851df8bae1dSRodney W. Grimes 	}
852df8bae1dSRodney W. Grimes 
853df8bae1dSRodney W. Grimes 	return (error);
854df8bae1dSRodney W. Grimes }
855df8bae1dSRodney W. Grimes 
856c9bf0111SKATO Takenori static int
857df8bae1dSRodney W. Grimes union_read(ap)
858df8bae1dSRodney W. Grimes 	struct vop_read_args /* {
859df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
860df8bae1dSRodney W. Grimes 		struct uio *a_uio;
861df8bae1dSRodney W. Grimes 		int  a_ioflag;
862df8bae1dSRodney W. Grimes 		struct ucred *a_cred;
863df8bae1dSRodney W. Grimes 	} */ *ap;
864df8bae1dSRodney W. Grimes {
865df8bae1dSRodney W. Grimes 	int error;
866996c772fSJohn Dyson 	struct proc *p = ap->a_uio->uio_procp;
867df8bae1dSRodney W. Grimes 	struct vnode *vp = OTHERVP(ap->a_vp);
868df8bae1dSRodney W. Grimes 	int dolock = (vp == LOWERVP(ap->a_vp));
869df8bae1dSRodney W. Grimes 
870df8bae1dSRodney W. Grimes 	if (dolock)
871996c772fSJohn Dyson 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
872df8bae1dSRodney W. Grimes 	else
873996c772fSJohn Dyson 		FIXUP(VTOUNION(ap->a_vp), p);
874df8bae1dSRodney W. Grimes 	error = VOP_READ(vp, ap->a_uio, ap->a_ioflag, ap->a_cred);
875df8bae1dSRodney W. Grimes 	if (dolock)
876996c772fSJohn Dyson 		VOP_UNLOCK(vp, 0, p);
877996c772fSJohn Dyson 
878996c772fSJohn Dyson 	/*
879996c772fSJohn Dyson 	 * XXX
880996c772fSJohn Dyson 	 * perhaps the size of the underlying object has changed under
881996c772fSJohn Dyson 	 * our feet.  take advantage of the offset information present
882996c772fSJohn Dyson 	 * in the uio structure.
883996c772fSJohn Dyson 	 */
884996c772fSJohn Dyson 	if (error == 0) {
885996c772fSJohn Dyson 		struct union_node *un = VTOUNION(ap->a_vp);
886996c772fSJohn Dyson 		off_t cur = ap->a_uio->uio_offset;
887996c772fSJohn Dyson 
888996c772fSJohn Dyson 		if (vp == un->un_uppervp) {
889996c772fSJohn Dyson 			if (cur > un->un_uppersz)
890996c772fSJohn Dyson 				union_newsize(ap->a_vp, cur, VNOVAL);
891996c772fSJohn Dyson 		} else {
892996c772fSJohn Dyson 			if (cur > un->un_lowersz)
893996c772fSJohn Dyson 				union_newsize(ap->a_vp, VNOVAL, cur);
894996c772fSJohn Dyson 		}
895996c772fSJohn Dyson 	}
896df8bae1dSRodney W. Grimes 
897df8bae1dSRodney W. Grimes 	return (error);
898df8bae1dSRodney W. Grimes }
899df8bae1dSRodney W. Grimes 
900c9bf0111SKATO Takenori static int
901df8bae1dSRodney W. Grimes union_write(ap)
902df8bae1dSRodney W. Grimes 	struct vop_read_args /* {
903df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
904df8bae1dSRodney W. Grimes 		struct uio *a_uio;
905df8bae1dSRodney W. Grimes 		int  a_ioflag;
906df8bae1dSRodney W. Grimes 		struct ucred *a_cred;
907df8bae1dSRodney W. Grimes 	} */ *ap;
908df8bae1dSRodney W. Grimes {
909df8bae1dSRodney W. Grimes 	int error;
910996c772fSJohn Dyson 	struct vnode *vp;
911996c772fSJohn Dyson 	struct union_node *un = VTOUNION(ap->a_vp);
912996c772fSJohn Dyson 	struct proc *p = ap->a_uio->uio_procp;
913df8bae1dSRodney W. Grimes 
914996c772fSJohn Dyson 	vp = UPPERVP(ap->a_vp);
915996c772fSJohn Dyson 	if (vp == NULLVP)
916996c772fSJohn Dyson 		panic("union: missing upper layer in write");
917996c772fSJohn Dyson 
918996c772fSJohn Dyson 	FIXUP(un, p);
919df8bae1dSRodney W. Grimes 	error = VOP_WRITE(vp, ap->a_uio, ap->a_ioflag, ap->a_cred);
920996c772fSJohn Dyson 
921996c772fSJohn Dyson 	/*
922996c772fSJohn Dyson 	 * the size of the underlying object may be changed by the
923996c772fSJohn Dyson 	 * write.
924996c772fSJohn Dyson 	 */
925996c772fSJohn Dyson 	if (error == 0) {
926996c772fSJohn Dyson 		off_t cur = ap->a_uio->uio_offset;
927996c772fSJohn Dyson 
928996c772fSJohn Dyson 		if (cur > un->un_uppersz)
929996c772fSJohn Dyson 			union_newsize(ap->a_vp, cur, VNOVAL);
930996c772fSJohn Dyson 	}
931df8bae1dSRodney W. Grimes 
932df8bae1dSRodney W. Grimes 	return (error);
933df8bae1dSRodney W. Grimes }
934df8bae1dSRodney W. Grimes 
935c9bf0111SKATO Takenori static int
936996c772fSJohn Dyson union_lease(ap)
937996c772fSJohn Dyson 	struct vop_lease_args /* {
938996c772fSJohn Dyson 		struct vnode *a_vp;
939996c772fSJohn Dyson 		struct proc *a_p;
940996c772fSJohn Dyson 		struct ucred *a_cred;
941996c772fSJohn Dyson 		int a_flag;
942996c772fSJohn Dyson 	} */ *ap;
943996c772fSJohn Dyson {
944996c772fSJohn Dyson 	register struct vnode *ovp = OTHERVP(ap->a_vp);
945996c772fSJohn Dyson 
946996c772fSJohn Dyson 	ap->a_vp = ovp;
947996c772fSJohn Dyson 	return (VCALL(ovp, VOFFSET(vop_lease), ap));
948996c772fSJohn Dyson }
949996c772fSJohn Dyson 
950c9bf0111SKATO Takenori static int
951df8bae1dSRodney W. Grimes union_ioctl(ap)
952df8bae1dSRodney W. Grimes 	struct vop_ioctl_args /* {
953df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
954df8bae1dSRodney W. Grimes 		int  a_command;
955df8bae1dSRodney W. Grimes 		caddr_t  a_data;
956df8bae1dSRodney W. Grimes 		int  a_fflag;
957df8bae1dSRodney W. Grimes 		struct ucred *a_cred;
958df8bae1dSRodney W. Grimes 		struct proc *a_p;
959df8bae1dSRodney W. Grimes 	} */ *ap;
960df8bae1dSRodney W. Grimes {
961996c772fSJohn Dyson 	register struct vnode *ovp = OTHERVP(ap->a_vp);
962df8bae1dSRodney W. Grimes 
963996c772fSJohn Dyson 	ap->a_vp = ovp;
964996c772fSJohn Dyson 	return (VCALL(ovp, VOFFSET(vop_ioctl), ap));
965df8bae1dSRodney W. Grimes }
966df8bae1dSRodney W. Grimes 
967c9bf0111SKATO Takenori static int
968a6aeade2SPeter Wemm union_poll(ap)
969a6aeade2SPeter Wemm 	struct vop_poll_args /* {
970df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
971a6aeade2SPeter Wemm 		int  a_events;
972df8bae1dSRodney W. Grimes 		struct ucred *a_cred;
973df8bae1dSRodney W. Grimes 		struct proc *a_p;
974df8bae1dSRodney W. Grimes 	} */ *ap;
975df8bae1dSRodney W. Grimes {
976996c772fSJohn Dyson 	register struct vnode *ovp = OTHERVP(ap->a_vp);
977df8bae1dSRodney W. Grimes 
978996c772fSJohn Dyson 	ap->a_vp = ovp;
979a6aeade2SPeter Wemm 	return (VCALL(ovp, VOFFSET(vop_poll), ap));
980996c772fSJohn Dyson }
981996c772fSJohn Dyson 
982c9bf0111SKATO Takenori static int
983996c772fSJohn Dyson union_revoke(ap)
984996c772fSJohn Dyson 	struct vop_revoke_args /* {
985996c772fSJohn Dyson 		struct vnode *a_vp;
986996c772fSJohn Dyson 		int a_flags;
987996c772fSJohn Dyson 		struct proc *a_p;
988996c772fSJohn Dyson 	} */ *ap;
989996c772fSJohn Dyson {
990996c772fSJohn Dyson 	struct vnode *vp = ap->a_vp;
991996c772fSJohn Dyson 
992996c772fSJohn Dyson 	if (UPPERVP(vp))
993996c772fSJohn Dyson 		VOP_REVOKE(UPPERVP(vp), ap->a_flags);
994996c772fSJohn Dyson 	if (LOWERVP(vp))
995996c772fSJohn Dyson 		VOP_REVOKE(LOWERVP(vp), ap->a_flags);
996996c772fSJohn Dyson 	vgone(vp);
997996c772fSJohn Dyson 	return (0);
998df8bae1dSRodney W. Grimes }
999df8bae1dSRodney W. Grimes 
1000c9bf0111SKATO Takenori static int
1001df8bae1dSRodney W. Grimes union_mmap(ap)
1002df8bae1dSRodney W. Grimes 	struct vop_mmap_args /* {
1003df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
1004df8bae1dSRodney W. Grimes 		int  a_fflags;
1005df8bae1dSRodney W. Grimes 		struct ucred *a_cred;
1006df8bae1dSRodney W. Grimes 		struct proc *a_p;
1007df8bae1dSRodney W. Grimes 	} */ *ap;
1008df8bae1dSRodney W. Grimes {
1009996c772fSJohn Dyson 	register struct vnode *ovp = OTHERVP(ap->a_vp);
1010df8bae1dSRodney W. Grimes 
1011996c772fSJohn Dyson 	ap->a_vp = ovp;
1012996c772fSJohn Dyson 	return (VCALL(ovp, VOFFSET(vop_mmap), ap));
1013df8bae1dSRodney W. Grimes }
1014df8bae1dSRodney W. Grimes 
1015c9bf0111SKATO Takenori static int
1016df8bae1dSRodney W. Grimes union_fsync(ap)
1017df8bae1dSRodney W. Grimes 	struct vop_fsync_args /* {
1018df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
1019df8bae1dSRodney W. Grimes 		struct ucred *a_cred;
1020df8bae1dSRodney W. Grimes 		int  a_waitfor;
1021df8bae1dSRodney W. Grimes 		struct proc *a_p;
1022df8bae1dSRodney W. Grimes 	} */ *ap;
1023df8bae1dSRodney W. Grimes {
1024df8bae1dSRodney W. Grimes 	int error = 0;
1025996c772fSJohn Dyson 	struct proc *p = ap->a_p;
1026df8bae1dSRodney W. Grimes 	struct vnode *targetvp = OTHERVP(ap->a_vp);
1027363ad700SPoul-Henning Kamp 	struct union_node *un;
1028df8bae1dSRodney W. Grimes 
1029996c772fSJohn Dyson 	if (targetvp != NULLVP) {
1030df8bae1dSRodney W. Grimes 		int dolock = (targetvp == LOWERVP(ap->a_vp));
1031df8bae1dSRodney W. Grimes 
1032363ad700SPoul-Henning Kamp 		un = VTOUNION(ap->a_vp);
1033df8bae1dSRodney W. Grimes 		if (dolock)
1034996c772fSJohn Dyson 			vn_lock(targetvp, LK_EXCLUSIVE | LK_RETRY, p);
1035afc2a558SKATO Takenori 		else  {
1036afc2a558SKATO Takenori 			un = VTOUNION(ap->a_vp);
1037afc2a558SKATO Takenori 			if ((un->un_flags & UN_ULOCK) == 0 &&
1038afc2a558SKATO Takenori 			    targetvp->v_data != NULL &&
1039afc2a558SKATO Takenori 			    ((struct lock *)targetvp->v_data)->lk_lockholder
1040afc2a558SKATO Takenori 			        == curproc->p_pid &&
1041afc2a558SKATO Takenori 			    VOP_ISLOCKED(targetvp) != 0)
1042afc2a558SKATO Takenori 				return 0;   /* XXX */
1043afc2a558SKATO Takenori 
1044afc2a558SKATO Takenori 			FIXUP(un, p);
1045363ad700SPoul-Henning Kamp 		}
1046afc2a558SKATO Takenori 
1047996c772fSJohn Dyson 		error = VOP_FSYNC(targetvp, ap->a_cred, ap->a_waitfor, p);
1048df8bae1dSRodney W. Grimes 		if (dolock)
1049996c772fSJohn Dyson 			VOP_UNLOCK(targetvp, 0, p);
1050df8bae1dSRodney W. Grimes 	}
1051df8bae1dSRodney W. Grimes 
1052df8bae1dSRodney W. Grimes 	return (error);
1053df8bae1dSRodney W. Grimes }
1054df8bae1dSRodney W. Grimes 
1055c9bf0111SKATO Takenori static int
1056df8bae1dSRodney W. Grimes union_remove(ap)
1057df8bae1dSRodney W. Grimes 	struct vop_remove_args /* {
1058df8bae1dSRodney W. Grimes 		struct vnode *a_dvp;
1059df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
1060df8bae1dSRodney W. Grimes 		struct componentname *a_cnp;
1061df8bae1dSRodney W. Grimes 	} */ *ap;
1062df8bae1dSRodney W. Grimes {
1063df8bae1dSRodney W. Grimes 	int error;
1064df8bae1dSRodney W. Grimes 	struct union_node *dun = VTOUNION(ap->a_dvp);
1065df8bae1dSRodney W. Grimes 	struct union_node *un = VTOUNION(ap->a_vp);
1066996c772fSJohn Dyson 	struct componentname *cnp = ap->a_cnp;
1067996c772fSJohn Dyson 	struct proc *p = cnp->cn_proc;
1068df8bae1dSRodney W. Grimes 
1069996c772fSJohn Dyson 	if (dun->un_uppervp == NULLVP)
1070996c772fSJohn Dyson 		panic("union remove: null upper vnode");
1071996c772fSJohn Dyson 
1072996c772fSJohn Dyson 	if (un->un_uppervp != NULLVP) {
1073df8bae1dSRodney W. Grimes 		struct vnode *dvp = dun->un_uppervp;
1074df8bae1dSRodney W. Grimes 		struct vnode *vp = un->un_uppervp;
1075df8bae1dSRodney W. Grimes 
1076996c772fSJohn Dyson 		FIXUP(dun, p);
1077df8bae1dSRodney W. Grimes 		VREF(dvp);
1078a9320ff3SKATO Takenori 		SETKLOCK(dun);
1079df8bae1dSRodney W. Grimes 		vput(ap->a_dvp);
1080a9320ff3SKATO Takenori 		CLEARKLOCK(dun);
1081996c772fSJohn Dyson 		FIXUP(un, p);
1082df8bae1dSRodney W. Grimes 		VREF(vp);
1083a9320ff3SKATO Takenori 		SETKLOCK(un);
1084df8bae1dSRodney W. Grimes 		vput(ap->a_vp);
1085a9320ff3SKATO Takenori 		CLEARKLOCK(un);
1086df8bae1dSRodney W. Grimes 
1087996c772fSJohn Dyson 		if (union_dowhiteout(un, cnp->cn_cred, cnp->cn_proc))
1088996c772fSJohn Dyson 			cnp->cn_flags |= DOWHITEOUT;
1089996c772fSJohn Dyson 		error = VOP_REMOVE(dvp, vp, cnp);
109087884aadSKATO Takenori #if 0
109187884aadSKATO Takenori 		/* XXX */
1092df8bae1dSRodney W. Grimes 		if (!error)
1093df8bae1dSRodney W. Grimes 			union_removed_upper(un);
109487884aadSKATO Takenori #endif
1095df8bae1dSRodney W. Grimes 	} else {
1096996c772fSJohn Dyson 		FIXUP(dun, p);
1097996c772fSJohn Dyson 		error = union_mkwhiteout(
1098996c772fSJohn Dyson 			MOUNTTOUNIONMOUNT(UNIONTOV(dun)->v_mount),
1099996c772fSJohn Dyson 			dun->un_uppervp, ap->a_cnp, un->un_path);
1100df8bae1dSRodney W. Grimes 		vput(ap->a_dvp);
1101df8bae1dSRodney W. Grimes 		vput(ap->a_vp);
1102df8bae1dSRodney W. Grimes 	}
1103df8bae1dSRodney W. Grimes 
1104df8bae1dSRodney W. Grimes 	return (error);
1105df8bae1dSRodney W. Grimes }
1106df8bae1dSRodney W. Grimes 
1107c9bf0111SKATO Takenori static int
1108df8bae1dSRodney W. Grimes union_link(ap)
1109df8bae1dSRodney W. Grimes 	struct vop_link_args /* {
1110df8bae1dSRodney W. Grimes 		struct vnode *a_tdvp;
111147777413SDavid Greenman 		struct vnode *a_vp;
1112df8bae1dSRodney W. Grimes 		struct componentname *a_cnp;
1113df8bae1dSRodney W. Grimes 	} */ *ap;
1114df8bae1dSRodney W. Grimes {
1115996c772fSJohn Dyson 	int error = 0;
1116996c772fSJohn Dyson 	struct componentname *cnp = ap->a_cnp;
1117996c772fSJohn Dyson 	struct proc *p = cnp->cn_proc;
1118996c772fSJohn Dyson 	struct union_node *un;
1119996c772fSJohn Dyson 	struct vnode *vp;
1120996c772fSJohn Dyson 	struct vnode *tdvp;
1121df8bae1dSRodney W. Grimes 
1122996c772fSJohn Dyson 	un = VTOUNION(ap->a_tdvp);
1123df8bae1dSRodney W. Grimes 
1124996c772fSJohn Dyson 	if (ap->a_tdvp->v_op != ap->a_vp->v_op) {
1125996c772fSJohn Dyson 		vp = ap->a_vp;
1126df8bae1dSRodney W. Grimes 	} else {
1127996c772fSJohn Dyson 		struct union_node *tun = VTOUNION(ap->a_vp);
1128996c772fSJohn Dyson 		if (tun->un_uppervp == NULLVP) {
1129996c772fSJohn Dyson 			vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY, p);
1130996c772fSJohn Dyson 			if (un->un_uppervp == tun->un_dirvp) {
1131996c772fSJohn Dyson 				un->un_flags &= ~UN_ULOCK;
1132996c772fSJohn Dyson 				VOP_UNLOCK(un->un_uppervp, 0, p);
1133996c772fSJohn Dyson 			}
1134996c772fSJohn Dyson 			error = union_copyup(tun, 1, cnp->cn_cred, p);
1135996c772fSJohn Dyson 			if (un->un_uppervp == tun->un_dirvp) {
1136996c772fSJohn Dyson 				vn_lock(un->un_uppervp,
1137996c772fSJohn Dyson 						LK_EXCLUSIVE | LK_RETRY, p);
1138996c772fSJohn Dyson 				un->un_flags |= UN_ULOCK;
1139996c772fSJohn Dyson 			}
1140996c772fSJohn Dyson 			VOP_UNLOCK(ap->a_vp, 0, p);
1141996c772fSJohn Dyson 		}
1142996c772fSJohn Dyson 		vp = tun->un_uppervp;
1143df8bae1dSRodney W. Grimes 	}
1144df8bae1dSRodney W. Grimes 
1145996c772fSJohn Dyson 	tdvp = un->un_uppervp;
1146996c772fSJohn Dyson 	if (tdvp == NULLVP)
1147996c772fSJohn Dyson 		error = EROFS;
1148996c772fSJohn Dyson 
1149996c772fSJohn Dyson 	if (error) {
1150996c772fSJohn Dyson 		vput(ap->a_tdvp);
1151df8bae1dSRodney W. Grimes 		return (error);
1152df8bae1dSRodney W. Grimes 	}
1153df8bae1dSRodney W. Grimes 
1154996c772fSJohn Dyson 	FIXUP(un, p);
1155996c772fSJohn Dyson 	VREF(tdvp);
1156a9320ff3SKATO Takenori 	SETKLOCK(un);
1157996c772fSJohn Dyson 	vput(ap->a_tdvp);
1158a9320ff3SKATO Takenori 	CLEARKLOCK(un);
1159996c772fSJohn Dyson 
116093d71a48SKATO Takenori 	return (VOP_LINK(tdvp, vp, cnp));
1161996c772fSJohn Dyson }
1162996c772fSJohn Dyson 
1163c9bf0111SKATO Takenori static int
1164df8bae1dSRodney W. Grimes union_rename(ap)
1165df8bae1dSRodney W. Grimes 	struct vop_rename_args  /* {
1166df8bae1dSRodney W. Grimes 		struct vnode *a_fdvp;
1167df8bae1dSRodney W. Grimes 		struct vnode *a_fvp;
1168df8bae1dSRodney W. Grimes 		struct componentname *a_fcnp;
1169df8bae1dSRodney W. Grimes 		struct vnode *a_tdvp;
1170df8bae1dSRodney W. Grimes 		struct vnode *a_tvp;
1171df8bae1dSRodney W. Grimes 		struct componentname *a_tcnp;
1172df8bae1dSRodney W. Grimes 	} */ *ap;
1173df8bae1dSRodney W. Grimes {
1174df8bae1dSRodney W. Grimes 	int error;
1175df8bae1dSRodney W. Grimes 
1176df8bae1dSRodney W. Grimes 	struct vnode *fdvp = ap->a_fdvp;
1177df8bae1dSRodney W. Grimes 	struct vnode *fvp = ap->a_fvp;
1178df8bae1dSRodney W. Grimes 	struct vnode *tdvp = ap->a_tdvp;
1179df8bae1dSRodney W. Grimes 	struct vnode *tvp = ap->a_tvp;
1180a9320ff3SKATO Takenori 	int isklockset = 0;
1181df8bae1dSRodney W. Grimes 
1182df8bae1dSRodney W. Grimes 	if (fdvp->v_op == union_vnodeop_p) {	/* always true */
1183df8bae1dSRodney W. Grimes 		struct union_node *un = VTOUNION(fdvp);
1184df8bae1dSRodney W. Grimes 		if (un->un_uppervp == NULLVP) {
1185996c772fSJohn Dyson 			/*
1186996c772fSJohn Dyson 			 * this should never happen in normal
1187996c772fSJohn Dyson 			 * operation but might if there was
1188996c772fSJohn Dyson 			 * a problem creating the top-level shadow
1189996c772fSJohn Dyson 			 * directory.
1190996c772fSJohn Dyson 			 */
1191996c772fSJohn Dyson 			error = EXDEV;
1192df8bae1dSRodney W. Grimes 			goto bad;
1193df8bae1dSRodney W. Grimes 		}
1194df8bae1dSRodney W. Grimes 
1195df8bae1dSRodney W. Grimes 		fdvp = un->un_uppervp;
1196df8bae1dSRodney W. Grimes 		VREF(fdvp);
1197df8bae1dSRodney W. Grimes 		vrele(ap->a_fdvp);
1198df8bae1dSRodney W. Grimes 	}
1199df8bae1dSRodney W. Grimes 
1200df8bae1dSRodney W. Grimes 	if (fvp->v_op == union_vnodeop_p) {	/* always true */
1201df8bae1dSRodney W. Grimes 		struct union_node *un = VTOUNION(fvp);
1202df8bae1dSRodney W. Grimes 		if (un->un_uppervp == NULLVP) {
1203996c772fSJohn Dyson 			/* XXX: should do a copyup */
1204996c772fSJohn Dyson 			error = EXDEV;
1205df8bae1dSRodney W. Grimes 			goto bad;
1206df8bae1dSRodney W. Grimes 		}
1207df8bae1dSRodney W. Grimes 
1208996c772fSJohn Dyson 		if (un->un_lowervp != NULLVP)
1209996c772fSJohn Dyson 			ap->a_fcnp->cn_flags |= DOWHITEOUT;
1210996c772fSJohn Dyson 
1211df8bae1dSRodney W. Grimes 		fvp = un->un_uppervp;
1212df8bae1dSRodney W. Grimes 		VREF(fvp);
1213df8bae1dSRodney W. Grimes 		vrele(ap->a_fvp);
1214df8bae1dSRodney W. Grimes 	}
1215df8bae1dSRodney W. Grimes 
1216df8bae1dSRodney W. Grimes 	if (tdvp->v_op == union_vnodeop_p) {
1217df8bae1dSRodney W. Grimes 		struct union_node *un = VTOUNION(tdvp);
1218df8bae1dSRodney W. Grimes 		if (un->un_uppervp == NULLVP) {
1219996c772fSJohn Dyson 			/*
1220996c772fSJohn Dyson 			 * this should never happen in normal
1221996c772fSJohn Dyson 			 * operation but might if there was
1222996c772fSJohn Dyson 			 * a problem creating the top-level shadow
1223996c772fSJohn Dyson 			 * directory.
1224996c772fSJohn Dyson 			 */
1225996c772fSJohn Dyson 			error = EXDEV;
1226df8bae1dSRodney W. Grimes 			goto bad;
1227df8bae1dSRodney W. Grimes 		}
1228df8bae1dSRodney W. Grimes 
1229df8bae1dSRodney W. Grimes 		tdvp = un->un_uppervp;
1230df8bae1dSRodney W. Grimes 		VREF(tdvp);
1231a9320ff3SKATO Takenori 		SETKLOCK(un);
1232df8bae1dSRodney W. Grimes 		vput(ap->a_tdvp);
1233a9320ff3SKATO Takenori 		CLEARKLOCK(un);
1234df8bae1dSRodney W. Grimes 	}
1235df8bae1dSRodney W. Grimes 
1236996c772fSJohn Dyson 	if (tvp != NULLVP && tvp->v_op == union_vnodeop_p) {
1237df8bae1dSRodney W. Grimes 		struct union_node *un = VTOUNION(tvp);
1238df8bae1dSRodney W. Grimes 
1239df8bae1dSRodney W. Grimes 		tvp = un->un_uppervp;
1240996c772fSJohn Dyson 		if (tvp != NULLVP) {
1241df8bae1dSRodney W. Grimes 			VREF(tvp);
1242a9320ff3SKATO Takenori 			SETKLOCK(un);
1243a9320ff3SKATO Takenori 			isklockset = 1;
1244996c772fSJohn Dyson 		}
1245df8bae1dSRodney W. Grimes 		vput(ap->a_tvp);
1246a9320ff3SKATO Takenori 		if (isklockset)
1247a9320ff3SKATO Takenori 			CLEARKLOCK(un);
1248df8bae1dSRodney W. Grimes 	}
1249df8bae1dSRodney W. Grimes 
1250df8bae1dSRodney W. Grimes 	return (VOP_RENAME(fdvp, fvp, ap->a_fcnp, tdvp, tvp, ap->a_tcnp));
1251df8bae1dSRodney W. Grimes 
1252df8bae1dSRodney W. Grimes bad:
1253df8bae1dSRodney W. Grimes 	vrele(fdvp);
1254df8bae1dSRodney W. Grimes 	vrele(fvp);
1255df8bae1dSRodney W. Grimes 	vput(tdvp);
1256996c772fSJohn Dyson 	if (tvp != NULLVP)
1257df8bae1dSRodney W. Grimes 		vput(tvp);
1258df8bae1dSRodney W. Grimes 
1259df8bae1dSRodney W. Grimes 	return (error);
1260df8bae1dSRodney W. Grimes }
1261df8bae1dSRodney W. Grimes 
1262c9bf0111SKATO Takenori static int
1263df8bae1dSRodney W. Grimes union_mkdir(ap)
1264df8bae1dSRodney W. Grimes 	struct vop_mkdir_args /* {
1265df8bae1dSRodney W. Grimes 		struct vnode *a_dvp;
1266df8bae1dSRodney W. Grimes 		struct vnode **a_vpp;
1267df8bae1dSRodney W. Grimes 		struct componentname *a_cnp;
1268df8bae1dSRodney W. Grimes 		struct vattr *a_vap;
1269df8bae1dSRodney W. Grimes 	} */ *ap;
1270df8bae1dSRodney W. Grimes {
1271df8bae1dSRodney W. Grimes 	struct union_node *un = VTOUNION(ap->a_dvp);
1272df8bae1dSRodney W. Grimes 	struct vnode *dvp = un->un_uppervp;
1273996c772fSJohn Dyson 	struct componentname *cnp = ap->a_cnp;
1274996c772fSJohn Dyson 	struct proc *p = cnp->cn_proc;
1275df8bae1dSRodney W. Grimes 
1276996c772fSJohn Dyson 	if (dvp != NULLVP) {
1277df8bae1dSRodney W. Grimes 		int error;
1278df8bae1dSRodney W. Grimes 		struct vnode *vp;
1279df8bae1dSRodney W. Grimes 
1280996c772fSJohn Dyson 		FIXUP(un, p);
1281df8bae1dSRodney W. Grimes 		VREF(dvp);
1282a9320ff3SKATO Takenori 		SETKLOCK(un);
1283996c772fSJohn Dyson 		VOP_UNLOCK(ap->a_dvp, 0, p);
1284a9320ff3SKATO Takenori 		CLEARKLOCK(un);
1285996c772fSJohn Dyson 		error = VOP_MKDIR(dvp, &vp, cnp, ap->a_vap);
1286996c772fSJohn Dyson 		if (error) {
1287996c772fSJohn Dyson 			vrele(ap->a_dvp);
1288df8bae1dSRodney W. Grimes 			return (error);
1289996c772fSJohn Dyson 		}
1290df8bae1dSRodney W. Grimes 
1291996c772fSJohn Dyson 		error = union_allocvp(ap->a_vpp, ap->a_dvp->v_mount, ap->a_dvp,
1292996c772fSJohn Dyson 				NULLVP, cnp, vp, NULLVP, 1);
1293996c772fSJohn Dyson 		vrele(ap->a_dvp);
1294df8bae1dSRodney W. Grimes 		if (error)
1295df8bae1dSRodney W. Grimes 			vput(vp);
1296df8bae1dSRodney W. Grimes 		return (error);
1297df8bae1dSRodney W. Grimes 	}
1298df8bae1dSRodney W. Grimes 
1299df8bae1dSRodney W. Grimes 	vput(ap->a_dvp);
1300df8bae1dSRodney W. Grimes 	return (EROFS);
1301df8bae1dSRodney W. Grimes }
1302df8bae1dSRodney W. Grimes 
1303c9bf0111SKATO Takenori static int
1304df8bae1dSRodney W. Grimes union_rmdir(ap)
1305df8bae1dSRodney W. Grimes 	struct vop_rmdir_args /* {
1306df8bae1dSRodney W. Grimes 		struct vnode *a_dvp;
1307df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
1308df8bae1dSRodney W. Grimes 		struct componentname *a_cnp;
1309df8bae1dSRodney W. Grimes 	} */ *ap;
1310df8bae1dSRodney W. Grimes {
1311df8bae1dSRodney W. Grimes 	int error;
1312df8bae1dSRodney W. Grimes 	struct union_node *dun = VTOUNION(ap->a_dvp);
1313df8bae1dSRodney W. Grimes 	struct union_node *un = VTOUNION(ap->a_vp);
1314996c772fSJohn Dyson 	struct componentname *cnp = ap->a_cnp;
1315996c772fSJohn Dyson 	struct proc *p = cnp->cn_proc;
1316df8bae1dSRodney W. Grimes 
1317996c772fSJohn Dyson 	if (dun->un_uppervp == NULLVP)
1318996c772fSJohn Dyson 		panic("union rmdir: null upper vnode");
1319996c772fSJohn Dyson 
1320996c772fSJohn Dyson 	if (un->un_uppervp != NULLVP) {
1321df8bae1dSRodney W. Grimes 		struct vnode *dvp = dun->un_uppervp;
1322df8bae1dSRodney W. Grimes 		struct vnode *vp = un->un_uppervp;
1323df8bae1dSRodney W. Grimes 
1324996c772fSJohn Dyson 		FIXUP(dun, p);
1325df8bae1dSRodney W. Grimes 		VREF(dvp);
1326a9320ff3SKATO Takenori 		SETKLOCK(dun);
1327df8bae1dSRodney W. Grimes 		vput(ap->a_dvp);
1328a9320ff3SKATO Takenori 		CLEARKLOCK(dun);
1329996c772fSJohn Dyson 		FIXUP(un, p);
1330df8bae1dSRodney W. Grimes 		VREF(vp);
1331a9320ff3SKATO Takenori 		SETKLOCK(un);
1332df8bae1dSRodney W. Grimes 		vput(ap->a_vp);
1333a9320ff3SKATO Takenori 		CLEARKLOCK(un);
1334df8bae1dSRodney W. Grimes 
1335996c772fSJohn Dyson 		if (union_dowhiteout(un, cnp->cn_cred, cnp->cn_proc))
1336996c772fSJohn Dyson 			cnp->cn_flags |= DOWHITEOUT;
1337df8bae1dSRodney W. Grimes 		error = VOP_RMDIR(dvp, vp, ap->a_cnp);
133887884aadSKATO Takenori #if 0
133987884aadSKATO Takenori 		/* XXX */
1340df8bae1dSRodney W. Grimes 		if (!error)
1341df8bae1dSRodney W. Grimes 			union_removed_upper(un);
134287884aadSKATO Takenori #endif
1343df8bae1dSRodney W. Grimes 	} else {
1344996c772fSJohn Dyson 		FIXUP(dun, p);
1345996c772fSJohn Dyson 		error = union_mkwhiteout(
1346996c772fSJohn Dyson 			MOUNTTOUNIONMOUNT(UNIONTOV(dun)->v_mount),
1347996c772fSJohn Dyson 			dun->un_uppervp, ap->a_cnp, un->un_path);
1348df8bae1dSRodney W. Grimes 		vput(ap->a_dvp);
1349df8bae1dSRodney W. Grimes 		vput(ap->a_vp);
1350df8bae1dSRodney W. Grimes 	}
1351df8bae1dSRodney W. Grimes 
1352df8bae1dSRodney W. Grimes 	return (error);
1353df8bae1dSRodney W. Grimes }
1354df8bae1dSRodney W. Grimes 
1355c9bf0111SKATO Takenori static int
1356df8bae1dSRodney W. Grimes union_symlink(ap)
1357df8bae1dSRodney W. Grimes 	struct vop_symlink_args /* {
1358df8bae1dSRodney W. Grimes 		struct vnode *a_dvp;
1359df8bae1dSRodney W. Grimes 		struct vnode **a_vpp;
1360df8bae1dSRodney W. Grimes 		struct componentname *a_cnp;
1361df8bae1dSRodney W. Grimes 		struct vattr *a_vap;
1362df8bae1dSRodney W. Grimes 		char *a_target;
1363df8bae1dSRodney W. Grimes 	} */ *ap;
1364df8bae1dSRodney W. Grimes {
1365df8bae1dSRodney W. Grimes 	struct union_node *un = VTOUNION(ap->a_dvp);
1366df8bae1dSRodney W. Grimes 	struct vnode *dvp = un->un_uppervp;
1367996c772fSJohn Dyson 	struct componentname *cnp = ap->a_cnp;
1368996c772fSJohn Dyson 	struct proc *p = cnp->cn_proc;
1369df8bae1dSRodney W. Grimes 
1370996c772fSJohn Dyson 	if (dvp != NULLVP) {
1371df8bae1dSRodney W. Grimes 		int error;
1372df8bae1dSRodney W. Grimes 		struct vnode *vp;
1373df8bae1dSRodney W. Grimes 
1374996c772fSJohn Dyson 		FIXUP(un, p);
1375df8bae1dSRodney W. Grimes 		VREF(dvp);
1376a9320ff3SKATO Takenori 		SETKLOCK(un);
1377df8bae1dSRodney W. Grimes 		vput(ap->a_dvp);
1378a9320ff3SKATO Takenori 		CLEARKLOCK(un);
1379996c772fSJohn Dyson 		error = VOP_SYMLINK(dvp, &vp, cnp, ap->a_vap, ap->a_target);
1380df8bae1dSRodney W. Grimes 		*ap->a_vpp = NULLVP;
1381df8bae1dSRodney W. Grimes 		return (error);
1382df8bae1dSRodney W. Grimes 	}
1383df8bae1dSRodney W. Grimes 
1384df8bae1dSRodney W. Grimes 	vput(ap->a_dvp);
1385df8bae1dSRodney W. Grimes 	return (EROFS);
1386df8bae1dSRodney W. Grimes }
1387df8bae1dSRodney W. Grimes 
1388df8bae1dSRodney W. Grimes /*
1389df8bae1dSRodney W. Grimes  * union_readdir works in concert with getdirentries and
1390df8bae1dSRodney W. Grimes  * readdir(3) to provide a list of entries in the unioned
1391df8bae1dSRodney W. Grimes  * directories.  getdirentries is responsible for walking
1392df8bae1dSRodney W. Grimes  * down the union stack.  readdir(3) is responsible for
1393df8bae1dSRodney W. Grimes  * eliminating duplicate names from the returned data stream.
1394df8bae1dSRodney W. Grimes  */
1395c9bf0111SKATO Takenori static int
1396df8bae1dSRodney W. Grimes union_readdir(ap)
1397df8bae1dSRodney W. Grimes 	struct vop_readdir_args /* {
1398df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
1399df8bae1dSRodney W. Grimes 		struct uio *a_uio;
1400df8bae1dSRodney W. Grimes 		struct ucred *a_cred;
1401996c772fSJohn Dyson 		int *a_eofflag;
1402996c772fSJohn Dyson 		u_long *a_cookies;
1403996c772fSJohn Dyson 		int a_ncookies;
1404df8bae1dSRodney W. Grimes 	} */ *ap;
1405df8bae1dSRodney W. Grimes {
1406df8bae1dSRodney W. Grimes 	struct union_node *un = VTOUNION(ap->a_vp);
1407996c772fSJohn Dyson 	struct vnode *uvp = un->un_uppervp;
1408996c772fSJohn Dyson 	struct proc *p = ap->a_uio->uio_procp;
1409df8bae1dSRodney W. Grimes 
1410996c772fSJohn Dyson 	if (uvp == NULLVP)
1411996c772fSJohn Dyson 		return (0);
1412df8bae1dSRodney W. Grimes 
1413996c772fSJohn Dyson 	FIXUP(un, p);
1414996c772fSJohn Dyson 	ap->a_vp = uvp;
1415996c772fSJohn Dyson 	return (VCALL(uvp, VOFFSET(vop_readdir), ap));
1416df8bae1dSRodney W. Grimes }
1417df8bae1dSRodney W. Grimes 
1418c9bf0111SKATO Takenori static int
1419df8bae1dSRodney W. Grimes union_readlink(ap)
1420df8bae1dSRodney W. Grimes 	struct vop_readlink_args /* {
1421df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
1422df8bae1dSRodney W. Grimes 		struct uio *a_uio;
1423df8bae1dSRodney W. Grimes 		struct ucred *a_cred;
1424df8bae1dSRodney W. Grimes 	} */ *ap;
1425df8bae1dSRodney W. Grimes {
1426df8bae1dSRodney W. Grimes 	int error;
1427996c772fSJohn Dyson 	struct uio *uio = ap->a_uio;
1428996c772fSJohn Dyson 	struct proc *p = uio->uio_procp;
1429df8bae1dSRodney W. Grimes 	struct vnode *vp = OTHERVP(ap->a_vp);
1430df8bae1dSRodney W. Grimes 	int dolock = (vp == LOWERVP(ap->a_vp));
1431df8bae1dSRodney W. Grimes 
1432df8bae1dSRodney W. Grimes 	if (dolock)
1433996c772fSJohn Dyson 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
1434df8bae1dSRodney W. Grimes 	else
1435996c772fSJohn Dyson 		FIXUP(VTOUNION(ap->a_vp), p);
1436996c772fSJohn Dyson 	ap->a_vp = vp;
1437996c772fSJohn Dyson 	error = VCALL(vp, VOFFSET(vop_readlink), ap);
1438df8bae1dSRodney W. Grimes 	if (dolock)
1439996c772fSJohn Dyson 		VOP_UNLOCK(vp, 0, p);
1440df8bae1dSRodney W. Grimes 
1441df8bae1dSRodney W. Grimes 	return (error);
1442df8bae1dSRodney W. Grimes }
1443df8bae1dSRodney W. Grimes 
1444c9bf0111SKATO Takenori static int
1445df8bae1dSRodney W. Grimes union_abortop(ap)
1446df8bae1dSRodney W. Grimes 	struct vop_abortop_args /* {
1447df8bae1dSRodney W. Grimes 		struct vnode *a_dvp;
1448df8bae1dSRodney W. Grimes 		struct componentname *a_cnp;
1449df8bae1dSRodney W. Grimes 	} */ *ap;
1450df8bae1dSRodney W. Grimes {
1451df8bae1dSRodney W. Grimes 	int error;
1452996c772fSJohn Dyson 	struct componentname *cnp = ap->a_cnp;
1453996c772fSJohn Dyson 	struct proc *p = cnp->cn_proc;
1454df8bae1dSRodney W. Grimes 	struct vnode *vp = OTHERVP(ap->a_dvp);
1455df8bae1dSRodney W. Grimes 	struct union_node *un = VTOUNION(ap->a_dvp);
1456df8bae1dSRodney W. Grimes 	int islocked = un->un_flags & UN_LOCKED;
1457df8bae1dSRodney W. Grimes 	int dolock = (vp == LOWERVP(ap->a_dvp));
1458df8bae1dSRodney W. Grimes 
1459df8bae1dSRodney W. Grimes 	if (islocked) {
1460df8bae1dSRodney W. Grimes 		if (dolock)
1461996c772fSJohn Dyson 			vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
1462df8bae1dSRodney W. Grimes 		else
1463996c772fSJohn Dyson 			FIXUP(VTOUNION(ap->a_dvp), p);
1464df8bae1dSRodney W. Grimes 	}
1465996c772fSJohn Dyson 	ap->a_dvp = vp;
1466996c772fSJohn Dyson 	error = VCALL(vp, VOFFSET(vop_abortop), ap);
1467df8bae1dSRodney W. Grimes 	if (islocked && dolock)
1468996c772fSJohn Dyson 		VOP_UNLOCK(vp, 0, p);
1469df8bae1dSRodney W. Grimes 
1470df8bae1dSRodney W. Grimes 	return (error);
1471df8bae1dSRodney W. Grimes }
1472df8bae1dSRodney W. Grimes 
1473c9bf0111SKATO Takenori static int
1474df8bae1dSRodney W. Grimes union_inactive(ap)
1475df8bae1dSRodney W. Grimes 	struct vop_inactive_args /* {
1476df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
1477996c772fSJohn Dyson 		struct proc *a_p;
1478df8bae1dSRodney W. Grimes 	} */ *ap;
1479df8bae1dSRodney W. Grimes {
1480996c772fSJohn Dyson 	struct vnode *vp = ap->a_vp;
1481996c772fSJohn Dyson 	struct proc *p = ap->a_p;
1482996c772fSJohn Dyson 	struct union_node *un = VTOUNION(vp);
1483996c772fSJohn Dyson 	struct vnode **vpp;
1484df8bae1dSRodney W. Grimes 
1485df8bae1dSRodney W. Grimes 	/*
1486df8bae1dSRodney W. Grimes 	 * Do nothing (and _don't_ bypass).
1487df8bae1dSRodney W. Grimes 	 * Wait to vrele lowervp until reclaim,
1488df8bae1dSRodney W. Grimes 	 * so that until then our union_node is in the
1489df8bae1dSRodney W. Grimes 	 * cache and reusable.
1490df8bae1dSRodney W. Grimes 	 *
1491df8bae1dSRodney W. Grimes 	 * NEEDSWORK: Someday, consider inactive'ing
1492df8bae1dSRodney W. Grimes 	 * the lowervp and then trying to reactivate it
1493df8bae1dSRodney W. Grimes 	 * with capabilities (v_id)
1494df8bae1dSRodney W. Grimes 	 * like they do in the name lookup cache code.
1495df8bae1dSRodney W. Grimes 	 * That's too much work for now.
1496df8bae1dSRodney W. Grimes 	 */
1497df8bae1dSRodney W. Grimes 
1498996c772fSJohn Dyson 	if (un->un_dircache != 0) {
1499996c772fSJohn Dyson 		for (vpp = un->un_dircache; *vpp != NULLVP; vpp++)
1500996c772fSJohn Dyson 			vrele(*vpp);
1501996c772fSJohn Dyson 		free(un->un_dircache, M_TEMP);
1502996c772fSJohn Dyson 		un->un_dircache = 0;
1503996c772fSJohn Dyson 	}
1504df8bae1dSRodney W. Grimes 
1505996c772fSJohn Dyson 	VOP_UNLOCK(vp, 0, p);
1506996c772fSJohn Dyson 
1507996c772fSJohn Dyson 	if ((un->un_flags & UN_CACHED) == 0)
1508996c772fSJohn Dyson 		vgone(vp);
1509df8bae1dSRodney W. Grimes 
1510df8bae1dSRodney W. Grimes 	return (0);
1511df8bae1dSRodney W. Grimes }
1512df8bae1dSRodney W. Grimes 
1513c9bf0111SKATO Takenori static int
1514df8bae1dSRodney W. Grimes union_reclaim(ap)
1515df8bae1dSRodney W. Grimes 	struct vop_reclaim_args /* {
1516df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
1517df8bae1dSRodney W. Grimes 	} */ *ap;
1518df8bae1dSRodney W. Grimes {
1519df8bae1dSRodney W. Grimes 
1520df8bae1dSRodney W. Grimes 	union_freevp(ap->a_vp);
1521df8bae1dSRodney W. Grimes 
1522df8bae1dSRodney W. Grimes 	return (0);
1523df8bae1dSRodney W. Grimes }
1524df8bae1dSRodney W. Grimes 
1525c9bf0111SKATO Takenori static int
1526df8bae1dSRodney W. Grimes union_lock(ap)
1527df8bae1dSRodney W. Grimes 	struct vop_lock_args *ap;
1528df8bae1dSRodney W. Grimes {
1529df8bae1dSRodney W. Grimes 	struct vnode *vp = ap->a_vp;
1530996c772fSJohn Dyson 	struct proc *p = ap->a_p;
1531996c772fSJohn Dyson 	int flags = ap->a_flags;
1532df8bae1dSRodney W. Grimes 	struct union_node *un;
1533996c772fSJohn Dyson 	int error;
1534996c772fSJohn Dyson 
1535996c772fSJohn Dyson 	vop_nolock(ap);
1536996c772fSJohn Dyson 	/*
1537996c772fSJohn Dyson 	 * Need to do real lockmgr-style locking here.
1538996c772fSJohn Dyson 	 * in the mean time, draining won't work quite right,
1539996c772fSJohn Dyson 	 * which could lead to a few race conditions.
1540996c772fSJohn Dyson 	 * the following test was here, but is not quite right, we
1541996c772fSJohn Dyson 	 * still need to take the lock:
1542996c772fSJohn Dyson 	if ((flags & LK_TYPE_MASK) == LK_DRAIN)
1543996c772fSJohn Dyson 		return (0);
1544996c772fSJohn Dyson 	 */
1545996c772fSJohn Dyson 	flags &= ~LK_INTERLOCK;
1546df8bae1dSRodney W. Grimes 
1547df8bae1dSRodney W. Grimes start:
1548df8bae1dSRodney W. Grimes 	un = VTOUNION(vp);
1549df8bae1dSRodney W. Grimes 
1550996c772fSJohn Dyson 	if (un->un_uppervp != NULLVP) {
15513ae60298SKATO Takenori 		if (((un->un_flags & UN_ULOCK) == 0) &&
1552996c772fSJohn Dyson 		    (vp->v_usecount != 0)) {
15533ae60298SKATO Takenori 			if ((un->un_flags & UN_KLOCK) == 0) {
1554996c772fSJohn Dyson 				error = vn_lock(un->un_uppervp, flags, p);
1555996c772fSJohn Dyson 				if (error)
1556996c772fSJohn Dyson 					return (error);
15573ae60298SKATO Takenori 			}
1558df8bae1dSRodney W. Grimes 			un->un_flags |= UN_ULOCK;
1559df8bae1dSRodney W. Grimes 		}
156018b4c8e2SKATO Takenori 	}
1561df8bae1dSRodney W. Grimes 
1562df8bae1dSRodney W. Grimes 	if (un->un_flags & UN_LOCKED) {
1563df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
1564df8bae1dSRodney W. Grimes 		if (curproc && un->un_pid == curproc->p_pid &&
1565df8bae1dSRodney W. Grimes 			    un->un_pid > -1 && curproc->p_pid > -1)
1566df8bae1dSRodney W. Grimes 			panic("union: locking against myself");
1567df8bae1dSRodney W. Grimes #endif
1568df8bae1dSRodney W. Grimes 		un->un_flags |= UN_WANT;
1569996c772fSJohn Dyson 		tsleep((caddr_t)&un->un_flags, PINOD, "unionlk2", 0);
1570df8bae1dSRodney W. Grimes 		goto start;
1571df8bae1dSRodney W. Grimes 	}
1572df8bae1dSRodney W. Grimes 
1573df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
1574df8bae1dSRodney W. Grimes 	if (curproc)
1575df8bae1dSRodney W. Grimes 		un->un_pid = curproc->p_pid;
1576df8bae1dSRodney W. Grimes 	else
1577df8bae1dSRodney W. Grimes 		un->un_pid = -1;
1578df8bae1dSRodney W. Grimes #endif
1579df8bae1dSRodney W. Grimes 
1580df8bae1dSRodney W. Grimes 	un->un_flags |= UN_LOCKED;
1581df8bae1dSRodney W. Grimes 	return (0);
1582df8bae1dSRodney W. Grimes }
1583df8bae1dSRodney W. Grimes 
1584996c772fSJohn Dyson /*
1585996c772fSJohn Dyson  * When operations want to vput() a union node yet retain a lock on
1586996c772fSJohn Dyson  * the upper vnode (say, to do some further operations like link(),
1587996c772fSJohn Dyson  * mkdir(), ...), they set UN_KLOCK on the union node, then call
1588996c772fSJohn Dyson  * vput() which calls VOP_UNLOCK() and comes here.  union_unlock()
1589996c772fSJohn Dyson  * unlocks the union node (leaving the upper vnode alone), clears the
1590996c772fSJohn Dyson  * KLOCK flag, and then returns to vput().  The caller then does whatever
1591996c772fSJohn Dyson  * is left to do with the upper vnode, and ensures that it gets unlocked.
1592996c772fSJohn Dyson  *
1593996c772fSJohn Dyson  * If UN_KLOCK isn't set, then the upper vnode is unlocked here.
1594996c772fSJohn Dyson  */
15953ae60298SKATO Takenori /*
15963ae60298SKATO Takenori  * FreeBSD:  Do not cleark UN_KLOCK flag.  UN_KLOCK flag is tested
15973ae60298SKATO Takenori  * in union_lock().
15983ae60298SKATO Takenori  */
1599c9bf0111SKATO Takenori static int
1600df8bae1dSRodney W. Grimes union_unlock(ap)
1601996c772fSJohn Dyson 	struct vop_unlock_args /* {
1602996c772fSJohn Dyson 		struct vnode *a_vp;
1603996c772fSJohn Dyson 		int a_flags;
1604996c772fSJohn Dyson 		struct proc *a_p;
1605996c772fSJohn Dyson 	} */ *ap;
1606df8bae1dSRodney W. Grimes {
1607df8bae1dSRodney W. Grimes 	struct union_node *un = VTOUNION(ap->a_vp);
1608996c772fSJohn Dyson 	struct proc *p = ap->a_p;
1609df8bae1dSRodney W. Grimes 
1610df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
1611df8bae1dSRodney W. Grimes 	if ((un->un_flags & UN_LOCKED) == 0)
1612df8bae1dSRodney W. Grimes 		panic("union: unlock unlocked node");
1613df8bae1dSRodney W. Grimes 	if (curproc && un->un_pid != curproc->p_pid &&
1614df8bae1dSRodney W. Grimes 			curproc->p_pid > -1 && un->un_pid > -1)
1615df8bae1dSRodney W. Grimes 		panic("union: unlocking other process's union node");
1616df8bae1dSRodney W. Grimes #endif
1617df8bae1dSRodney W. Grimes 
1618df8bae1dSRodney W. Grimes 	un->un_flags &= ~UN_LOCKED;
1619df8bae1dSRodney W. Grimes 
1620471f8060SKATO Takenori 	if ((un->un_flags & (UN_ULOCK|UN_KLOCK)) == UN_ULOCK) {
1621471f8060SKATO Takenori 		/*
1622471f8060SKATO Takenori 		 * XXX
1623471f8060SKATO Takenori 		 * Workarround for lockmgr violation.  Need to implement
1624471f8060SKATO Takenori 		 * real lockmgr-style lock/unlock.
1625471f8060SKATO Takenori 		 */
1626471f8060SKATO Takenori 		if ((ap->a_vp->v_flag & VDOOMED) &&
1627471f8060SKATO Takenori 		    ((struct lock *)un->un_uppervp->v_data)->lk_lockholder
1628471f8060SKATO Takenori 		    == LK_KERNPROC)
1629471f8060SKATO Takenori 			VOP_UNLOCK(un->un_uppervp, 0, 0);
1630471f8060SKATO Takenori 		else
1631996c772fSJohn Dyson 			VOP_UNLOCK(un->un_uppervp, 0, p);
1632471f8060SKATO Takenori 	}
1633a9320ff3SKATO Takenori 	un->un_flags &= ~UN_ULOCK;
1634df8bae1dSRodney W. Grimes 
1635df8bae1dSRodney W. Grimes 	if (un->un_flags & UN_WANT) {
1636df8bae1dSRodney W. Grimes 		un->un_flags &= ~UN_WANT;
1637df8bae1dSRodney W. Grimes 		wakeup((caddr_t) &un->un_flags);
1638df8bae1dSRodney W. Grimes 	}
1639df8bae1dSRodney W. Grimes 
1640df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
1641df8bae1dSRodney W. Grimes 	un->un_pid = 0;
1642df8bae1dSRodney W. Grimes #endif
1643996c772fSJohn Dyson 	vop_nounlock(ap);
1644df8bae1dSRodney W. Grimes 
1645df8bae1dSRodney W. Grimes 	return (0);
1646df8bae1dSRodney W. Grimes }
1647df8bae1dSRodney W. Grimes 
1648c9bf0111SKATO Takenori static int
1649df8bae1dSRodney W. Grimes union_bmap(ap)
1650df8bae1dSRodney W. Grimes 	struct vop_bmap_args /* {
1651df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
1652df8bae1dSRodney W. Grimes 		daddr_t  a_bn;
1653df8bae1dSRodney W. Grimes 		struct vnode **a_vpp;
1654df8bae1dSRodney W. Grimes 		daddr_t *a_bnp;
1655df8bae1dSRodney W. Grimes 		int *a_runp;
1656c83ebe77SJohn Dyson 		int *a_runb;
1657df8bae1dSRodney W. Grimes 	} */ *ap;
1658df8bae1dSRodney W. Grimes {
1659df8bae1dSRodney W. Grimes 	int error;
1660996c772fSJohn Dyson 	struct proc *p = curproc;		/* XXX */
1661df8bae1dSRodney W. Grimes 	struct vnode *vp = OTHERVP(ap->a_vp);
1662df8bae1dSRodney W. Grimes 	int dolock = (vp == LOWERVP(ap->a_vp));
1663df8bae1dSRodney W. Grimes 
1664df8bae1dSRodney W. Grimes 	if (dolock)
1665996c772fSJohn Dyson 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
1666df8bae1dSRodney W. Grimes 	else
1667996c772fSJohn Dyson 		FIXUP(VTOUNION(ap->a_vp), p);
1668996c772fSJohn Dyson 	ap->a_vp = vp;
1669996c772fSJohn Dyson 	error = VCALL(vp, VOFFSET(vop_bmap), ap);
1670df8bae1dSRodney W. Grimes 	if (dolock)
1671996c772fSJohn Dyson 		VOP_UNLOCK(vp, 0, p);
1672df8bae1dSRodney W. Grimes 
1673df8bae1dSRodney W. Grimes 	return (error);
1674df8bae1dSRodney W. Grimes }
1675df8bae1dSRodney W. Grimes 
1676c9bf0111SKATO Takenori static int
1677df8bae1dSRodney W. Grimes union_print(ap)
1678df8bae1dSRodney W. Grimes 	struct vop_print_args /* {
1679df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
1680df8bae1dSRodney W. Grimes 	} */ *ap;
1681df8bae1dSRodney W. Grimes {
1682df8bae1dSRodney W. Grimes 	struct vnode *vp = ap->a_vp;
1683df8bae1dSRodney W. Grimes 
16843a773ad0SPoul-Henning Kamp 	printf("\ttag VT_UNION, vp=%p, uppervp=%p, lowervp=%p\n",
1685df8bae1dSRodney W. Grimes 			vp, UPPERVP(vp), LOWERVP(vp));
1686996c772fSJohn Dyson 	if (UPPERVP(vp) != NULLVP)
1687996c772fSJohn Dyson 		vprint("union: upper", UPPERVP(vp));
1688996c772fSJohn Dyson 	if (LOWERVP(vp) != NULLVP)
1689996c772fSJohn Dyson 		vprint("union: lower", LOWERVP(vp));
1690996c772fSJohn Dyson 
1691df8bae1dSRodney W. Grimes 	return (0);
1692df8bae1dSRodney W. Grimes }
1693df8bae1dSRodney W. Grimes 
1694c9bf0111SKATO Takenori static int
1695df8bae1dSRodney W. Grimes union_islocked(ap)
1696df8bae1dSRodney W. Grimes 	struct vop_islocked_args /* {
1697df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
1698df8bae1dSRodney W. Grimes 	} */ *ap;
1699df8bae1dSRodney W. Grimes {
1700df8bae1dSRodney W. Grimes 
1701df8bae1dSRodney W. Grimes 	return ((VTOUNION(ap->a_vp)->un_flags & UN_LOCKED) ? 1 : 0);
1702df8bae1dSRodney W. Grimes }
1703df8bae1dSRodney W. Grimes 
1704c9bf0111SKATO Takenori static int
1705df8bae1dSRodney W. Grimes union_pathconf(ap)
1706df8bae1dSRodney W. Grimes 	struct vop_pathconf_args /* {
1707df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
1708df8bae1dSRodney W. Grimes 		int a_name;
1709df8bae1dSRodney W. Grimes 		int *a_retval;
1710df8bae1dSRodney W. Grimes 	} */ *ap;
1711df8bae1dSRodney W. Grimes {
1712df8bae1dSRodney W. Grimes 	int error;
1713996c772fSJohn Dyson 	struct proc *p = curproc;		/* XXX */
1714df8bae1dSRodney W. Grimes 	struct vnode *vp = OTHERVP(ap->a_vp);
1715df8bae1dSRodney W. Grimes 	int dolock = (vp == LOWERVP(ap->a_vp));
1716df8bae1dSRodney W. Grimes 
1717df8bae1dSRodney W. Grimes 	if (dolock)
1718996c772fSJohn Dyson 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
1719df8bae1dSRodney W. Grimes 	else
1720996c772fSJohn Dyson 		FIXUP(VTOUNION(ap->a_vp), p);
1721996c772fSJohn Dyson 	ap->a_vp = vp;
1722996c772fSJohn Dyson 	error = VCALL(vp, VOFFSET(vop_pathconf), ap);
1723df8bae1dSRodney W. Grimes 	if (dolock)
1724996c772fSJohn Dyson 		VOP_UNLOCK(vp, 0, p);
1725df8bae1dSRodney W. Grimes 
1726df8bae1dSRodney W. Grimes 	return (error);
1727df8bae1dSRodney W. Grimes }
1728df8bae1dSRodney W. Grimes 
1729c9bf0111SKATO Takenori static int
1730df8bae1dSRodney W. Grimes union_advlock(ap)
1731df8bae1dSRodney W. Grimes 	struct vop_advlock_args /* {
1732df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
1733df8bae1dSRodney W. Grimes 		caddr_t  a_id;
1734df8bae1dSRodney W. Grimes 		int  a_op;
1735df8bae1dSRodney W. Grimes 		struct flock *a_fl;
1736df8bae1dSRodney W. Grimes 		int  a_flags;
1737df8bae1dSRodney W. Grimes 	} */ *ap;
1738df8bae1dSRodney W. Grimes {
1739996c772fSJohn Dyson 	register struct vnode *ovp = OTHERVP(ap->a_vp);
1740df8bae1dSRodney W. Grimes 
1741996c772fSJohn Dyson 	ap->a_vp = ovp;
1742996c772fSJohn Dyson 	return (VCALL(ovp, VOFFSET(vop_advlock), ap));
1743df8bae1dSRodney W. Grimes }
1744df8bae1dSRodney W. Grimes 
1745df8bae1dSRodney W. Grimes 
1746df8bae1dSRodney W. Grimes /*
1747df8bae1dSRodney W. Grimes  * XXX - vop_strategy must be hand coded because it has no
1748df8bae1dSRodney W. Grimes  * vnode in its arguments.
1749df8bae1dSRodney W. Grimes  * This goes away with a merged VM/buffer cache.
1750df8bae1dSRodney W. Grimes  */
1751c9bf0111SKATO Takenori static int
1752df8bae1dSRodney W. Grimes union_strategy(ap)
1753df8bae1dSRodney W. Grimes 	struct vop_strategy_args /* {
1754df8bae1dSRodney W. Grimes 		struct buf *a_bp;
1755df8bae1dSRodney W. Grimes 	} */ *ap;
1756df8bae1dSRodney W. Grimes {
1757df8bae1dSRodney W. Grimes 	struct buf *bp = ap->a_bp;
1758df8bae1dSRodney W. Grimes 	int error;
1759df8bae1dSRodney W. Grimes 	struct vnode *savedvp;
1760df8bae1dSRodney W. Grimes 
1761df8bae1dSRodney W. Grimes 	savedvp = bp->b_vp;
1762df8bae1dSRodney W. Grimes 	bp->b_vp = OTHERVP(bp->b_vp);
1763df8bae1dSRodney W. Grimes 
1764df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
1765df8bae1dSRodney W. Grimes 	if (bp->b_vp == NULLVP)
1766df8bae1dSRodney W. Grimes 		panic("union_strategy: nil vp");
1767df8bae1dSRodney W. Grimes 	if (((bp->b_flags & B_READ) == 0) &&
1768df8bae1dSRodney W. Grimes 	    (bp->b_vp == LOWERVP(savedvp)))
1769df8bae1dSRodney W. Grimes 		panic("union_strategy: writing to lowervp");
1770df8bae1dSRodney W. Grimes #endif
1771df8bae1dSRodney W. Grimes 
1772df8bae1dSRodney W. Grimes 	error = VOP_STRATEGY(bp);
1773df8bae1dSRodney W. Grimes 	bp->b_vp = savedvp;
1774df8bae1dSRodney W. Grimes 
1775df8bae1dSRodney W. Grimes 	return (error);
1776df8bae1dSRodney W. Grimes }
1777df8bae1dSRodney W. Grimes 
1778df8bae1dSRodney W. Grimes /*
1779df8bae1dSRodney W. Grimes  * Global vfs data structures
1780df8bae1dSRodney W. Grimes  */
1781f57e6547SBruce Evans vop_t **union_vnodeop_p;
1782c109c577SBruce Evans static struct vnodeopv_entry_desc union_vnodeop_entries[] = {
1783dba3870cSPoul-Henning Kamp 	{ &vop_default_desc,		(vop_t *) vop_defaultop },
1784539ef70cSPoul-Henning Kamp 	{ &vop_abortop_desc,		(vop_t *) union_abortop },
1785539ef70cSPoul-Henning Kamp 	{ &vop_access_desc,		(vop_t *) union_access },
1786539ef70cSPoul-Henning Kamp 	{ &vop_advlock_desc,		(vop_t *) union_advlock },
1787539ef70cSPoul-Henning Kamp 	{ &vop_bmap_desc,		(vop_t *) union_bmap },
1788539ef70cSPoul-Henning Kamp 	{ &vop_close_desc,		(vop_t *) union_close },
1789539ef70cSPoul-Henning Kamp 	{ &vop_create_desc,		(vop_t *) union_create },
1790539ef70cSPoul-Henning Kamp 	{ &vop_fsync_desc,		(vop_t *) union_fsync },
1791539ef70cSPoul-Henning Kamp 	{ &vop_getattr_desc,		(vop_t *) union_getattr },
1792539ef70cSPoul-Henning Kamp 	{ &vop_inactive_desc,		(vop_t *) union_inactive },
1793539ef70cSPoul-Henning Kamp 	{ &vop_ioctl_desc,		(vop_t *) union_ioctl },
1794539ef70cSPoul-Henning Kamp 	{ &vop_islocked_desc,		(vop_t *) union_islocked },
1795539ef70cSPoul-Henning Kamp 	{ &vop_lease_desc,		(vop_t *) union_lease },
1796539ef70cSPoul-Henning Kamp 	{ &vop_link_desc,		(vop_t *) union_link },
1797539ef70cSPoul-Henning Kamp 	{ &vop_lock_desc,		(vop_t *) union_lock },
1798539ef70cSPoul-Henning Kamp 	{ &vop_lookup_desc,		(vop_t *) union_lookup },
1799539ef70cSPoul-Henning Kamp 	{ &vop_mkdir_desc,		(vop_t *) union_mkdir },
1800539ef70cSPoul-Henning Kamp 	{ &vop_mknod_desc,		(vop_t *) union_mknod },
1801539ef70cSPoul-Henning Kamp 	{ &vop_mmap_desc,		(vop_t *) union_mmap },
1802539ef70cSPoul-Henning Kamp 	{ &vop_open_desc,		(vop_t *) union_open },
1803539ef70cSPoul-Henning Kamp 	{ &vop_pathconf_desc,		(vop_t *) union_pathconf },
1804539ef70cSPoul-Henning Kamp 	{ &vop_poll_desc,		(vop_t *) union_poll },
1805539ef70cSPoul-Henning Kamp 	{ &vop_print_desc,		(vop_t *) union_print },
1806539ef70cSPoul-Henning Kamp 	{ &vop_read_desc,		(vop_t *) union_read },
1807539ef70cSPoul-Henning Kamp 	{ &vop_readdir_desc,		(vop_t *) union_readdir },
1808539ef70cSPoul-Henning Kamp 	{ &vop_readlink_desc,		(vop_t *) union_readlink },
1809539ef70cSPoul-Henning Kamp 	{ &vop_reclaim_desc,		(vop_t *) union_reclaim },
1810539ef70cSPoul-Henning Kamp 	{ &vop_remove_desc,		(vop_t *) union_remove },
1811539ef70cSPoul-Henning Kamp 	{ &vop_rename_desc,		(vop_t *) union_rename },
1812539ef70cSPoul-Henning Kamp 	{ &vop_revoke_desc,		(vop_t *) union_revoke },
1813539ef70cSPoul-Henning Kamp 	{ &vop_rmdir_desc,		(vop_t *) union_rmdir },
1814539ef70cSPoul-Henning Kamp 	{ &vop_setattr_desc,		(vop_t *) union_setattr },
1815539ef70cSPoul-Henning Kamp 	{ &vop_strategy_desc,		(vop_t *) union_strategy },
1816539ef70cSPoul-Henning Kamp 	{ &vop_symlink_desc,		(vop_t *) union_symlink },
1817539ef70cSPoul-Henning Kamp 	{ &vop_unlock_desc,		(vop_t *) union_unlock },
1818539ef70cSPoul-Henning Kamp 	{ &vop_whiteout_desc,		(vop_t *) union_whiteout },
1819539ef70cSPoul-Henning Kamp 	{ &vop_write_desc,		(vop_t *) union_write },
1820f57e6547SBruce Evans 	{ NULL, NULL }
1821df8bae1dSRodney W. Grimes };
1822c109c577SBruce Evans static struct vnodeopv_desc union_vnodeop_opv_desc =
1823df8bae1dSRodney W. Grimes 	{ &union_vnodeop_p, union_vnodeop_entries };
1824c901836cSGarrett Wollman 
1825c901836cSGarrett Wollman VNODEOP_SET(union_vnodeop_opv_desc);
1826