xref: /freebsd/sys/fs/unionfs/union_vnops.c (revision 9b5e8b3aa54f6b5a613ad640be26529d4a45247e)
1df8bae1dSRodney W. Grimes /*
2df8bae1dSRodney W. Grimes  * Copyright (c) 1992, 1993, 1994 The Regents of the University of California.
3df8bae1dSRodney W. Grimes  * Copyright (c) 1992, 1993, 1994 Jan-Simon Pendry.
4df8bae1dSRodney W. Grimes  * 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  *
37df8bae1dSRodney W. Grimes  *	@(#)union_vnops.c	8.6 (Berkeley) 2/17/94
389b5e8b3aSBruce Evans  * $Id: union_vnops.c,v 1.13 1995/11/09 08:16:38 bde Exp $
39df8bae1dSRodney W. Grimes  */
40df8bae1dSRodney W. Grimes 
41df8bae1dSRodney W. Grimes #include <sys/param.h>
42df8bae1dSRodney W. Grimes #include <sys/systm.h>
43df8bae1dSRodney W. Grimes #include <sys/proc.h>
44df8bae1dSRodney W. Grimes #include <sys/file.h>
45df8bae1dSRodney W. Grimes #include <sys/time.h>
4667bfdf83SGarrett Wollman #include <sys/kernel.h>
47df8bae1dSRodney W. Grimes #include <sys/types.h>
48df8bae1dSRodney W. Grimes #include <sys/vnode.h>
49df8bae1dSRodney W. Grimes #include <sys/mount.h>
50df8bae1dSRodney W. Grimes #include <sys/namei.h>
51df8bae1dSRodney W. Grimes #include <sys/malloc.h>
52df8bae1dSRodney W. Grimes #include <sys/buf.h>
53df8bae1dSRodney W. Grimes #include <sys/queue.h>
54df8bae1dSRodney W. Grimes #include <miscfs/union/union.h>
55df8bae1dSRodney W. Grimes 
56c4a7b7e1SDavid Greenman /* FIXUP throws the lock on the uppervp vnode if the union_node is already
57c4a7b7e1SDavid Greenman  * locked and the uppervp vnode is not.  Before, this was thrown regardless
58c4a7b7e1SDavid Greenman  * of the state of the union_node which resulted in locked vnodes which
59c4a7b7e1SDavid Greenman  * were never unlocked (since the union would never be unlocked).
60c4a7b7e1SDavid Greenman  */
61df8bae1dSRodney W. Grimes #define FIXUP(un) { \
62c4a7b7e1SDavid Greenman 	if (((un)->un_flags & (UN_LOCKED|UN_ULOCK)) == UN_LOCKED) { \
63df8bae1dSRodney W. Grimes 		union_fixup(un); \
64df8bae1dSRodney W. Grimes 	} \
65df8bae1dSRodney W. Grimes }
66df8bae1dSRodney W. Grimes 
679b5e8b3aSBruce Evans extern int	union_abortop __P((struct vop_abortop_args *ap));
689b5e8b3aSBruce Evans extern int	union_access __P((struct vop_access_args *ap));
699b5e8b3aSBruce Evans extern int	union_advlock __P((struct vop_advlock_args *ap));
709b5e8b3aSBruce Evans extern int	union_bmap __P((struct vop_bmap_args *ap));
719b5e8b3aSBruce Evans extern int	union_close __P((struct vop_close_args *ap));
729b5e8b3aSBruce Evans extern int	union_create __P((struct vop_create_args *ap));
739b5e8b3aSBruce Evans static void	union_fixup __P((struct union_node *un));
749b5e8b3aSBruce Evans extern int	union_fsync __P((struct vop_fsync_args *ap));
759b5e8b3aSBruce Evans extern int	union_getattr __P((struct vop_getattr_args *ap));
769b5e8b3aSBruce Evans extern int	union_inactive __P((struct vop_inactive_args *ap));
779b5e8b3aSBruce Evans extern int	union_ioctl __P((struct vop_ioctl_args *ap));
789b5e8b3aSBruce Evans extern int	union_islocked __P((struct vop_islocked_args *ap));
799b5e8b3aSBruce Evans extern int	union_link __P((struct vop_link_args *ap));
809b5e8b3aSBruce Evans extern int	union_lock __P((struct vop_lock_args *ap));
819b5e8b3aSBruce Evans extern int	union_lookup __P((struct vop_lookup_args *ap));
829b5e8b3aSBruce Evans static int	union_lookup1 __P((struct vnode *udvp, struct vnode *dvp,
839b5e8b3aSBruce Evans 				   struct vnode **vpp,
849b5e8b3aSBruce Evans 				   struct componentname *cnp));
859b5e8b3aSBruce Evans extern int	union_mkdir __P((struct vop_mkdir_args *ap));
869b5e8b3aSBruce Evans extern int	union_mknod __P((struct vop_mknod_args *ap));
879b5e8b3aSBruce Evans extern int	union_mmap __P((struct vop_mmap_args *ap));
889b5e8b3aSBruce Evans extern int	union_open __P((struct vop_open_args *ap));
899b5e8b3aSBruce Evans extern int	union_pathconf __P((struct vop_pathconf_args *ap));
909b5e8b3aSBruce Evans extern int	union_print __P((struct vop_print_args *ap));
919b5e8b3aSBruce Evans extern int	union_read __P((struct vop_read_args *ap));
929b5e8b3aSBruce Evans extern int	union_readdir __P((struct vop_readdir_args *ap));
939b5e8b3aSBruce Evans extern int	union_readlink __P((struct vop_readlink_args *ap));
949b5e8b3aSBruce Evans extern int	union_reclaim __P((struct vop_reclaim_args *ap));
959b5e8b3aSBruce Evans extern int	union_remove __P((struct vop_remove_args *ap));
969b5e8b3aSBruce Evans extern int	union_rename __P((struct vop_rename_args *ap));
979b5e8b3aSBruce Evans extern int	union_rmdir __P((struct vop_rmdir_args *ap));
989b5e8b3aSBruce Evans extern int	union_seek __P((struct vop_seek_args *ap));
999b5e8b3aSBruce Evans extern int	union_select __P((struct vop_select_args *ap));
1009b5e8b3aSBruce Evans extern int	union_setattr __P((struct vop_setattr_args *ap));
1019b5e8b3aSBruce Evans extern int	union_strategy __P((struct vop_strategy_args *ap));
1029b5e8b3aSBruce Evans extern int	union_symlink __P((struct vop_symlink_args *ap));
1039b5e8b3aSBruce Evans extern int	union_unlock __P((struct vop_lock_args *ap));
1049b5e8b3aSBruce Evans extern int	union_write __P((struct vop_read_args *ap));
1059b5e8b3aSBruce Evans 
106df8bae1dSRodney W. Grimes static void
107df8bae1dSRodney W. Grimes union_fixup(un)
108df8bae1dSRodney W. Grimes 	struct union_node *un;
109df8bae1dSRodney W. Grimes {
110df8bae1dSRodney W. Grimes 
111df8bae1dSRodney W. Grimes 	VOP_LOCK(un->un_uppervp);
112df8bae1dSRodney W. Grimes 	un->un_flags |= UN_ULOCK;
113df8bae1dSRodney W. Grimes }
114df8bae1dSRodney W. Grimes 
115df8bae1dSRodney W. Grimes static int
116df8bae1dSRodney W. Grimes union_lookup1(udvp, dvp, vpp, cnp)
117df8bae1dSRodney W. Grimes 	struct vnode *udvp;
118df8bae1dSRodney W. Grimes 	struct vnode *dvp;
119df8bae1dSRodney W. Grimes 	struct vnode **vpp;
120df8bae1dSRodney W. Grimes 	struct componentname *cnp;
121df8bae1dSRodney W. Grimes {
122df8bae1dSRodney W. Grimes 	int error;
123df8bae1dSRodney W. Grimes 	struct vnode *tdvp;
124df8bae1dSRodney W. Grimes 	struct mount *mp;
125df8bae1dSRodney W. Grimes 
126df8bae1dSRodney W. Grimes 	/*
127df8bae1dSRodney W. Grimes 	 * If stepping up the directory tree, check for going
128df8bae1dSRodney W. Grimes 	 * back across the mount point, in which case do what
129df8bae1dSRodney W. Grimes 	 * lookup would do by stepping back down the mount
130df8bae1dSRodney W. Grimes 	 * hierarchy.
131df8bae1dSRodney W. Grimes 	 */
132df8bae1dSRodney W. Grimes 	if (cnp->cn_flags & ISDOTDOT) {
133df8bae1dSRodney W. Grimes 		for (;;) {
134df8bae1dSRodney W. Grimes 			/*
135df8bae1dSRodney W. Grimes 			 * Don't do the NOCROSSMOUNT check
136df8bae1dSRodney W. Grimes 			 * at this level.  By definition,
137df8bae1dSRodney W. Grimes 			 * union fs deals with namespaces, not
138df8bae1dSRodney W. Grimes 			 * filesystems.
139df8bae1dSRodney W. Grimes 			 */
140df8bae1dSRodney W. Grimes 			if ((dvp->v_flag & VROOT) == 0)
141df8bae1dSRodney W. Grimes 				break;
142df8bae1dSRodney W. Grimes 
143df8bae1dSRodney W. Grimes 			tdvp = dvp;
144df8bae1dSRodney W. Grimes 			dvp = dvp->v_mount->mnt_vnodecovered;
145df8bae1dSRodney W. Grimes 			vput(tdvp);
146df8bae1dSRodney W. Grimes 			VREF(dvp);
147df8bae1dSRodney W. Grimes 			VOP_LOCK(dvp);
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))
161df8bae1dSRodney W. Grimes 		VOP_LOCK(dvp);
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 
173df8bae1dSRodney W. Grimes 		if (mp->mnt_flag & MNT_MLOCK) {
174df8bae1dSRodney W. Grimes 			mp->mnt_flag |= MNT_MWAIT;
17582478919SDavid Greenman 			(void) tsleep((caddr_t) mp, PVFS, "unlkup", 0);
176df8bae1dSRodney W. Grimes 			continue;
177df8bae1dSRodney W. Grimes 		}
178df8bae1dSRodney W. Grimes 
1793a773ad0SPoul-Henning Kamp 		error = VFS_ROOT(mp, &tdvp);
1803a773ad0SPoul-Henning Kamp 		if (error) {
181df8bae1dSRodney W. Grimes 			vput(dvp);
182df8bae1dSRodney W. Grimes 			return (error);
183df8bae1dSRodney W. Grimes 		}
184df8bae1dSRodney W. Grimes 
185df8bae1dSRodney W. Grimes 		vput(dvp);
186df8bae1dSRodney W. Grimes 		dvp = tdvp;
187df8bae1dSRodney W. Grimes 	}
188df8bae1dSRodney W. Grimes 
189df8bae1dSRodney W. Grimes 	*vpp = dvp;
190df8bae1dSRodney W. Grimes 	return (0);
191df8bae1dSRodney W. Grimes }
192df8bae1dSRodney W. Grimes 
193df8bae1dSRodney W. Grimes int
194df8bae1dSRodney W. Grimes union_lookup(ap)
195df8bae1dSRodney W. Grimes 	struct vop_lookup_args /* {
196df8bae1dSRodney W. Grimes 		struct vnodeop_desc *a_desc;
197df8bae1dSRodney W. Grimes 		struct vnode *a_dvp;
198df8bae1dSRodney W. Grimes 		struct vnode **a_vpp;
199df8bae1dSRodney W. Grimes 		struct componentname *a_cnp;
200df8bae1dSRodney W. Grimes 	} */ *ap;
201df8bae1dSRodney W. Grimes {
202df8bae1dSRodney W. Grimes 	int error;
203df8bae1dSRodney W. Grimes 	int uerror, lerror;
204df8bae1dSRodney W. Grimes 	struct vnode *uppervp, *lowervp;
205df8bae1dSRodney W. Grimes 	struct vnode *upperdvp, *lowerdvp;
206df8bae1dSRodney W. Grimes 	struct vnode *dvp = ap->a_dvp;
207df8bae1dSRodney W. Grimes 	struct union_node *dun = VTOUNION(dvp);
208df8bae1dSRodney W. Grimes 	struct componentname *cnp = ap->a_cnp;
209df8bae1dSRodney W. Grimes 	int lockparent = cnp->cn_flags & LOCKPARENT;
210df8bae1dSRodney W. Grimes 	struct union_mount *um = MOUNTTOUNIONMOUNT(dvp->v_mount);
21126f9a767SRodney W. Grimes 	struct ucred *saved_cred = 0;
212df8bae1dSRodney W. Grimes 
213df8bae1dSRodney W. Grimes 	cnp->cn_flags |= LOCKPARENT;
214df8bae1dSRodney W. Grimes 
215df8bae1dSRodney W. Grimes 	upperdvp = dun->un_uppervp;
216df8bae1dSRodney W. Grimes 	lowerdvp = dun->un_lowervp;
217df8bae1dSRodney W. Grimes 	uppervp = NULLVP;
218df8bae1dSRodney W. Grimes 	lowervp = NULLVP;
219df8bae1dSRodney W. Grimes 
220df8bae1dSRodney W. Grimes 	/*
221df8bae1dSRodney W. Grimes 	 * do the lookup in the upper level.
222df8bae1dSRodney W. Grimes 	 * if that level comsumes additional pathnames,
223df8bae1dSRodney W. Grimes 	 * then assume that something special is going
224df8bae1dSRodney W. Grimes 	 * on and just return that vnode.
225df8bae1dSRodney W. Grimes 	 */
226df8bae1dSRodney W. Grimes 	if (upperdvp) {
227df8bae1dSRodney W. Grimes 		FIXUP(dun);
228df8bae1dSRodney W. Grimes 		uerror = union_lookup1(um->um_uppervp, upperdvp,
229df8bae1dSRodney W. Grimes 					&uppervp, cnp);
230df8bae1dSRodney W. Grimes 		/*if (uppervp == upperdvp)
231df8bae1dSRodney W. Grimes 			dun->un_flags |= UN_KLOCK;*/
232df8bae1dSRodney W. Grimes 
233df8bae1dSRodney W. Grimes 		if (cnp->cn_consume != 0) {
234df8bae1dSRodney W. Grimes 			*ap->a_vpp = uppervp;
235df8bae1dSRodney W. Grimes 			if (!lockparent)
236df8bae1dSRodney W. Grimes 				cnp->cn_flags &= ~LOCKPARENT;
237df8bae1dSRodney W. Grimes 			return (uerror);
238df8bae1dSRodney W. Grimes 		}
239df8bae1dSRodney W. Grimes 	} else {
240df8bae1dSRodney W. Grimes 		uerror = ENOENT;
241df8bae1dSRodney W. Grimes 	}
242df8bae1dSRodney W. Grimes 
243df8bae1dSRodney W. Grimes 	/*
244df8bae1dSRodney W. Grimes 	 * in a similar way to the upper layer, do the lookup
245df8bae1dSRodney W. Grimes 	 * in the lower layer.   this time, if there is some
246df8bae1dSRodney W. Grimes 	 * component magic going on, then vput whatever we got
247df8bae1dSRodney W. Grimes 	 * back from the upper layer and return the lower vnode
248df8bae1dSRodney W. Grimes 	 * instead.
249df8bae1dSRodney W. Grimes 	 */
250df8bae1dSRodney W. Grimes 	if (lowerdvp) {
251df8bae1dSRodney W. Grimes 		int nameiop;
252df8bae1dSRodney W. Grimes 
253df8bae1dSRodney W. Grimes 		VOP_LOCK(lowerdvp);
254df8bae1dSRodney W. Grimes 
255df8bae1dSRodney W. Grimes 		/*
256df8bae1dSRodney W. Grimes 		 * Only do a LOOKUP on the bottom node, since
257df8bae1dSRodney W. Grimes 		 * we won't be making changes to it anyway.
258df8bae1dSRodney W. Grimes 		 */
259df8bae1dSRodney W. Grimes 		nameiop = cnp->cn_nameiop;
260df8bae1dSRodney W. Grimes 		cnp->cn_nameiop = LOOKUP;
261df8bae1dSRodney W. Grimes 		if (um->um_op == UNMNT_BELOW) {
262df8bae1dSRodney W. Grimes 			saved_cred = cnp->cn_cred;
263df8bae1dSRodney W. Grimes 			cnp->cn_cred = um->um_cred;
264df8bae1dSRodney W. Grimes 		}
265df8bae1dSRodney W. Grimes 		lerror = union_lookup1(um->um_lowervp, lowerdvp,
266df8bae1dSRodney W. Grimes 				&lowervp, cnp);
267df8bae1dSRodney W. Grimes 		if (um->um_op == UNMNT_BELOW)
268df8bae1dSRodney W. Grimes 			cnp->cn_cred = saved_cred;
269df8bae1dSRodney W. Grimes 		cnp->cn_nameiop = nameiop;
270df8bae1dSRodney W. Grimes 
271df8bae1dSRodney W. Grimes 		if (lowervp != lowerdvp)
272df8bae1dSRodney W. Grimes 			VOP_UNLOCK(lowerdvp);
273df8bae1dSRodney W. Grimes 
274df8bae1dSRodney W. Grimes 		if (cnp->cn_consume != 0) {
275df8bae1dSRodney W. Grimes 			if (uppervp) {
276df8bae1dSRodney W. Grimes 				if (uppervp == upperdvp)
277df8bae1dSRodney W. Grimes 					vrele(uppervp);
278df8bae1dSRodney W. Grimes 				else
279df8bae1dSRodney W. Grimes 					vput(uppervp);
280df8bae1dSRodney W. Grimes 				uppervp = NULLVP;
281df8bae1dSRodney W. Grimes 			}
282df8bae1dSRodney W. Grimes 			*ap->a_vpp = lowervp;
283df8bae1dSRodney W. Grimes 			if (!lockparent)
284df8bae1dSRodney W. Grimes 				cnp->cn_flags &= ~LOCKPARENT;
285df8bae1dSRodney W. Grimes 			return (lerror);
286df8bae1dSRodney W. Grimes 		}
287df8bae1dSRodney W. Grimes 	} else {
288df8bae1dSRodney W. Grimes 		lerror = ENOENT;
289df8bae1dSRodney W. Grimes 	}
290df8bae1dSRodney W. Grimes 
291df8bae1dSRodney W. Grimes 	if (!lockparent)
292df8bae1dSRodney W. Grimes 		cnp->cn_flags &= ~LOCKPARENT;
293df8bae1dSRodney W. Grimes 
294df8bae1dSRodney W. Grimes 	/*
295df8bae1dSRodney W. Grimes 	 * at this point, we have uerror and lerror indicating
296df8bae1dSRodney W. Grimes 	 * possible errors with the lookups in the upper and lower
297df8bae1dSRodney W. Grimes 	 * layers.  additionally, uppervp and lowervp are (locked)
298df8bae1dSRodney W. Grimes 	 * references to existing vnodes in the upper and lower layers.
299df8bae1dSRodney W. Grimes 	 *
300df8bae1dSRodney W. Grimes 	 * there are now three cases to consider.
301df8bae1dSRodney W. Grimes 	 * 1. if both layers returned an error, then return whatever
302df8bae1dSRodney W. Grimes 	 *    error the upper layer generated.
303df8bae1dSRodney W. Grimes 	 *
304df8bae1dSRodney W. Grimes 	 * 2. if the top layer failed and the bottom layer succeeded
305df8bae1dSRodney W. Grimes 	 *    then two subcases occur.
306df8bae1dSRodney W. Grimes 	 *    a.  the bottom vnode is not a directory, in which
307df8bae1dSRodney W. Grimes 	 *	  case just return a new union vnode referencing
308df8bae1dSRodney W. Grimes 	 *	  an empty top layer and the existing bottom layer.
309df8bae1dSRodney W. Grimes 	 *    b.  the bottom vnode is a directory, in which case
310df8bae1dSRodney W. Grimes 	 *	  create a new directory in the top-level and
311df8bae1dSRodney W. Grimes 	 *	  continue as in case 3.
312df8bae1dSRodney W. Grimes 	 *
313df8bae1dSRodney W. Grimes 	 * 3. if the top layer succeeded then return a new union
314df8bae1dSRodney W. Grimes 	 *    vnode referencing whatever the new top layer and
315df8bae1dSRodney W. Grimes 	 *    whatever the bottom layer returned.
316df8bae1dSRodney W. Grimes 	 */
317df8bae1dSRodney W. Grimes 
318df8bae1dSRodney W. Grimes 	*ap->a_vpp = NULLVP;
319df8bae1dSRodney W. Grimes 
320df8bae1dSRodney W. Grimes 	/* case 1. */
321df8bae1dSRodney W. Grimes 	if ((uerror != 0) && (lerror != 0)) {
322df8bae1dSRodney W. Grimes 		return (uerror);
323df8bae1dSRodney W. Grimes 	}
324df8bae1dSRodney W. Grimes 
325df8bae1dSRodney W. Grimes 	/* case 2. */
326df8bae1dSRodney W. Grimes 	if (uerror != 0 /* && (lerror == 0) */ ) {
327df8bae1dSRodney W. Grimes 		if (lowervp->v_type == VDIR) { /* case 2b. */
328df8bae1dSRodney W. Grimes 			dun->un_flags &= ~UN_ULOCK;
329df8bae1dSRodney W. Grimes 			VOP_UNLOCK(upperdvp);
330df8bae1dSRodney W. Grimes 			uerror = union_mkshadow(um, upperdvp, cnp, &uppervp);
331df8bae1dSRodney W. Grimes 			VOP_LOCK(upperdvp);
332df8bae1dSRodney W. Grimes 			dun->un_flags |= UN_ULOCK;
333df8bae1dSRodney W. Grimes 
334df8bae1dSRodney W. Grimes 			if (uerror) {
335df8bae1dSRodney W. Grimes 				if (lowervp) {
336df8bae1dSRodney W. Grimes 					vput(lowervp);
337df8bae1dSRodney W. Grimes 					lowervp = NULLVP;
338df8bae1dSRodney W. Grimes 				}
339df8bae1dSRodney W. Grimes 				return (uerror);
340df8bae1dSRodney W. Grimes 			}
341df8bae1dSRodney W. Grimes 		}
342df8bae1dSRodney W. Grimes 	}
343df8bae1dSRodney W. Grimes 
344df8bae1dSRodney W. Grimes 	if (lowervp)
345df8bae1dSRodney W. Grimes 		VOP_UNLOCK(lowervp);
346df8bae1dSRodney W. Grimes 
347df8bae1dSRodney W. Grimes 	error = union_allocvp(ap->a_vpp, dvp->v_mount, dvp, upperdvp, cnp,
348df8bae1dSRodney W. Grimes 			      uppervp, lowervp);
349df8bae1dSRodney W. Grimes 
350df8bae1dSRodney W. Grimes 	if (error) {
351df8bae1dSRodney W. Grimes 		if (uppervp)
352df8bae1dSRodney W. Grimes 			vput(uppervp);
353df8bae1dSRodney W. Grimes 		if (lowervp)
354df8bae1dSRodney W. Grimes 			vrele(lowervp);
355df8bae1dSRodney W. Grimes 	} else {
356df8bae1dSRodney W. Grimes 		if (*ap->a_vpp != dvp)
357df8bae1dSRodney W. Grimes 			if (!lockparent || !(cnp->cn_flags & ISLASTCN))
358df8bae1dSRodney W. Grimes 				VOP_UNLOCK(dvp);
359df8bae1dSRodney W. Grimes 	}
360df8bae1dSRodney W. Grimes 
361df8bae1dSRodney W. Grimes 	return (error);
362df8bae1dSRodney W. Grimes }
363df8bae1dSRodney W. Grimes 
364df8bae1dSRodney W. Grimes int
365df8bae1dSRodney W. Grimes union_create(ap)
366df8bae1dSRodney W. Grimes 	struct vop_create_args /* {
367df8bae1dSRodney W. Grimes 		struct vnode *a_dvp;
368df8bae1dSRodney W. Grimes 		struct vnode **a_vpp;
369df8bae1dSRodney W. Grimes 		struct componentname *a_cnp;
370df8bae1dSRodney W. Grimes 		struct vattr *a_vap;
371df8bae1dSRodney W. Grimes 	} */ *ap;
372df8bae1dSRodney W. Grimes {
373df8bae1dSRodney W. Grimes 	struct union_node *un = VTOUNION(ap->a_dvp);
374df8bae1dSRodney W. Grimes 	struct vnode *dvp = un->un_uppervp;
375df8bae1dSRodney W. Grimes 
376df8bae1dSRodney W. Grimes 	if (dvp) {
377df8bae1dSRodney W. Grimes 		int error;
378df8bae1dSRodney W. Grimes 		struct vnode *vp;
379df8bae1dSRodney W. Grimes 
380df8bae1dSRodney W. Grimes 		FIXUP(un);
381df8bae1dSRodney W. Grimes 
382df8bae1dSRodney W. Grimes 		VREF(dvp);
383df8bae1dSRodney W. Grimes 		un->un_flags |= UN_KLOCK;
384df8bae1dSRodney W. Grimes 		vput(ap->a_dvp);
385df8bae1dSRodney W. Grimes 		error = VOP_CREATE(dvp, &vp, ap->a_cnp, ap->a_vap);
386df8bae1dSRodney W. Grimes 		if (error)
387df8bae1dSRodney W. Grimes 			return (error);
388df8bae1dSRodney W. Grimes 
389df8bae1dSRodney W. Grimes 		error = union_allocvp(
390df8bae1dSRodney W. Grimes 				ap->a_vpp,
391df8bae1dSRodney W. Grimes 				ap->a_dvp->v_mount,
392df8bae1dSRodney W. Grimes 				ap->a_dvp,
393df8bae1dSRodney W. Grimes 				NULLVP,
394df8bae1dSRodney W. Grimes 				ap->a_cnp,
395df8bae1dSRodney W. Grimes 				vp,
396df8bae1dSRodney W. Grimes 				NULLVP);
397df8bae1dSRodney W. Grimes 		if (error)
398df8bae1dSRodney W. Grimes 			vput(vp);
399df8bae1dSRodney W. Grimes 		return (error);
400df8bae1dSRodney W. Grimes 	}
401df8bae1dSRodney W. Grimes 
402df8bae1dSRodney W. Grimes 	vput(ap->a_dvp);
403df8bae1dSRodney W. Grimes 	return (EROFS);
404df8bae1dSRodney W. Grimes }
405df8bae1dSRodney W. Grimes 
406df8bae1dSRodney W. Grimes int
407df8bae1dSRodney W. Grimes union_mknod(ap)
408df8bae1dSRodney W. Grimes 	struct vop_mknod_args /* {
409df8bae1dSRodney W. Grimes 		struct vnode *a_dvp;
410df8bae1dSRodney W. Grimes 		struct vnode **a_vpp;
411df8bae1dSRodney W. Grimes 		struct componentname *a_cnp;
412df8bae1dSRodney W. Grimes 		struct vattr *a_vap;
413df8bae1dSRodney W. Grimes 	} */ *ap;
414df8bae1dSRodney W. Grimes {
415df8bae1dSRodney W. Grimes 	struct union_node *un = VTOUNION(ap->a_dvp);
416df8bae1dSRodney W. Grimes 	struct vnode *dvp = un->un_uppervp;
417df8bae1dSRodney W. Grimes 
418df8bae1dSRodney W. Grimes 	if (dvp) {
419df8bae1dSRodney W. Grimes 		int error;
420df8bae1dSRodney W. Grimes 		struct vnode *vp;
421df8bae1dSRodney W. Grimes 
422df8bae1dSRodney W. Grimes 		FIXUP(un);
423df8bae1dSRodney W. Grimes 
424df8bae1dSRodney W. Grimes 		VREF(dvp);
425df8bae1dSRodney W. Grimes 		un->un_flags |= UN_KLOCK;
426df8bae1dSRodney W. Grimes 		vput(ap->a_dvp);
427df8bae1dSRodney W. Grimes 		error = VOP_MKNOD(dvp, &vp, ap->a_cnp, ap->a_vap);
428df8bae1dSRodney W. Grimes 		if (error)
429df8bae1dSRodney W. Grimes 			return (error);
430df8bae1dSRodney W. Grimes 
431df8bae1dSRodney W. Grimes 		if (vp) {
432df8bae1dSRodney W. Grimes 			error = union_allocvp(
433df8bae1dSRodney W. Grimes 					ap->a_vpp,
434df8bae1dSRodney W. Grimes 					ap->a_dvp->v_mount,
435df8bae1dSRodney W. Grimes 					ap->a_dvp,
436df8bae1dSRodney W. Grimes 					NULLVP,
437df8bae1dSRodney W. Grimes 					ap->a_cnp,
438df8bae1dSRodney W. Grimes 					vp,
439df8bae1dSRodney W. Grimes 					NULLVP);
440df8bae1dSRodney W. Grimes 			if (error)
441df8bae1dSRodney W. Grimes 				vput(vp);
442df8bae1dSRodney W. Grimes 		}
443df8bae1dSRodney W. Grimes 		return (error);
444df8bae1dSRodney W. Grimes 	}
445df8bae1dSRodney W. Grimes 
446df8bae1dSRodney W. Grimes 	vput(ap->a_dvp);
447df8bae1dSRodney W. Grimes 	return (EROFS);
448df8bae1dSRodney W. Grimes }
449df8bae1dSRodney W. Grimes 
450df8bae1dSRodney W. Grimes int
451df8bae1dSRodney W. Grimes union_open(ap)
452df8bae1dSRodney W. Grimes 	struct vop_open_args /* {
453df8bae1dSRodney W. Grimes 		struct vnodeop_desc *a_desc;
454df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
455df8bae1dSRodney W. Grimes 		int a_mode;
456df8bae1dSRodney W. Grimes 		struct ucred *a_cred;
457df8bae1dSRodney W. Grimes 		struct proc *a_p;
458df8bae1dSRodney W. Grimes 	} */ *ap;
459df8bae1dSRodney W. Grimes {
460df8bae1dSRodney W. Grimes 	struct union_node *un = VTOUNION(ap->a_vp);
461df8bae1dSRodney W. Grimes 	struct vnode *tvp;
462df8bae1dSRodney W. Grimes 	int mode = ap->a_mode;
463df8bae1dSRodney W. Grimes 	struct ucred *cred = ap->a_cred;
464df8bae1dSRodney W. Grimes 	struct proc *p = ap->a_p;
465df8bae1dSRodney W. Grimes 	int error;
466df8bae1dSRodney W. Grimes 
467df8bae1dSRodney W. Grimes 	/*
468df8bae1dSRodney W. Grimes 	 * If there is an existing upper vp then simply open that.
469df8bae1dSRodney W. Grimes 	 */
470df8bae1dSRodney W. Grimes 	tvp = un->un_uppervp;
471df8bae1dSRodney W. Grimes 	if (tvp == NULLVP) {
472df8bae1dSRodney W. Grimes 		/*
473df8bae1dSRodney W. Grimes 		 * If the lower vnode is being opened for writing, then
474df8bae1dSRodney W. Grimes 		 * copy the file contents to the upper vnode and open that,
475df8bae1dSRodney W. Grimes 		 * otherwise can simply open the lower vnode.
476df8bae1dSRodney W. Grimes 		 */
477df8bae1dSRodney W. Grimes 		tvp = un->un_lowervp;
478df8bae1dSRodney W. Grimes 		if ((ap->a_mode & FWRITE) && (tvp->v_type == VREG)) {
479df8bae1dSRodney W. Grimes 			struct vnode *vp;
480df8bae1dSRodney W. Grimes 			int i;
481df8bae1dSRodney W. Grimes 
482df8bae1dSRodney W. Grimes 			/*
483df8bae1dSRodney W. Grimes 			 * Open the named file in the upper layer.  Note that
484df8bae1dSRodney W. Grimes 			 * the file may have come into existence *since* the
485df8bae1dSRodney W. Grimes 			 * lookup was done, since the upper layer may really
486df8bae1dSRodney W. Grimes 			 * be a loopback mount of some other filesystem...
487df8bae1dSRodney W. Grimes 			 * so open the file with exclusive create and barf if
488df8bae1dSRodney W. Grimes 			 * it already exists.
489df8bae1dSRodney W. Grimes 			 * XXX - perhaps should re-lookup the node (once more
490df8bae1dSRodney W. Grimes 			 * with feeling) and simply open that.  Who knows.
491df8bae1dSRodney W. Grimes 			 */
492df8bae1dSRodney W. Grimes 			error = union_vn_create(&vp, un, p);
493df8bae1dSRodney W. Grimes 			if (error)
494df8bae1dSRodney W. Grimes 				return (error);
495df8bae1dSRodney W. Grimes 
496df8bae1dSRodney W. Grimes 			/* at this point, uppervp is locked */
497df8bae1dSRodney W. Grimes 			union_newupper(un, vp);
498df8bae1dSRodney W. Grimes 			un->un_flags |= UN_ULOCK;
499df8bae1dSRodney W. Grimes 
500df8bae1dSRodney W. Grimes 			/*
501df8bae1dSRodney W. Grimes 			 * Now, if the file is being opened with truncation,
502df8bae1dSRodney W. Grimes 			 * then the (new) upper vnode is ready to fly,
503df8bae1dSRodney W. Grimes 			 * otherwise the data from the lower vnode must be
504df8bae1dSRodney W. Grimes 			 * copied to the upper layer first.  This only works
505df8bae1dSRodney W. Grimes 			 * for regular files (check is made above).
506df8bae1dSRodney W. Grimes 			 */
507df8bae1dSRodney W. Grimes 			if ((mode & O_TRUNC) == 0) {
508df8bae1dSRodney W. Grimes 				/*
509df8bae1dSRodney W. Grimes 				 * XXX - should not ignore errors
510df8bae1dSRodney W. Grimes 				 * from VOP_CLOSE
511df8bae1dSRodney W. Grimes 				 */
512df8bae1dSRodney W. Grimes 				VOP_LOCK(tvp);
513df8bae1dSRodney W. Grimes 				error = VOP_OPEN(tvp, FREAD, cred, p);
514df8bae1dSRodney W. Grimes 				if (error == 0) {
515df8bae1dSRodney W. Grimes 					error = union_copyfile(p, cred,
516df8bae1dSRodney W. Grimes 						       tvp, un->un_uppervp);
517df8bae1dSRodney W. Grimes 					VOP_UNLOCK(tvp);
518cf2455a3SBruce Evans 					(void) VOP_CLOSE(tvp, FREAD, cred, p);
519df8bae1dSRodney W. Grimes 				} else {
520df8bae1dSRodney W. Grimes 					VOP_UNLOCK(tvp);
521df8bae1dSRodney W. Grimes 				}
522df8bae1dSRodney W. Grimes 
523df8bae1dSRodney W. Grimes #ifdef UNION_DIAGNOSTIC
524df8bae1dSRodney W. Grimes 				if (!error)
525df8bae1dSRodney W. Grimes 					uprintf("union: copied up %s\n",
526df8bae1dSRodney W. Grimes 								un->un_path);
527df8bae1dSRodney W. Grimes #endif
528df8bae1dSRodney W. Grimes 			}
529df8bae1dSRodney W. Grimes 
530df8bae1dSRodney W. Grimes 			un->un_flags &= ~UN_ULOCK;
531df8bae1dSRodney W. Grimes 			VOP_UNLOCK(un->un_uppervp);
532df8bae1dSRodney W. Grimes 			union_vn_close(un->un_uppervp, FWRITE, cred, p);
533df8bae1dSRodney W. Grimes 			VOP_LOCK(un->un_uppervp);
534df8bae1dSRodney W. Grimes 			un->un_flags |= UN_ULOCK;
535df8bae1dSRodney W. Grimes 
536df8bae1dSRodney W. Grimes 			/*
537df8bae1dSRodney W. Grimes 			 * Subsequent IOs will go to the top layer, so
538df8bae1dSRodney W. Grimes 			 * call close on the lower vnode and open on the
539df8bae1dSRodney W. Grimes 			 * upper vnode to ensure that the filesystem keeps
540df8bae1dSRodney W. Grimes 			 * its references counts right.  This doesn't do
541df8bae1dSRodney W. Grimes 			 * the right thing with (cred) and (FREAD) though.
542df8bae1dSRodney W. Grimes 			 * Ignoring error returns is not righ, either.
543df8bae1dSRodney W. Grimes 			 */
544df8bae1dSRodney W. Grimes 			for (i = 0; i < un->un_openl; i++) {
545cf2455a3SBruce Evans 				(void) VOP_CLOSE(tvp, FREAD, cred, p);
546df8bae1dSRodney W. Grimes 				(void) VOP_OPEN(un->un_uppervp, FREAD, cred, p);
547df8bae1dSRodney W. Grimes 			}
548df8bae1dSRodney W. Grimes 			un->un_openl = 0;
549df8bae1dSRodney W. Grimes 
550df8bae1dSRodney W. Grimes 			if (error == 0)
551df8bae1dSRodney W. Grimes 				error = VOP_OPEN(un->un_uppervp, mode, cred, p);
552df8bae1dSRodney W. Grimes 			return (error);
553df8bae1dSRodney W. Grimes 		}
554df8bae1dSRodney W. Grimes 
555df8bae1dSRodney W. Grimes 		/*
556df8bae1dSRodney W. Grimes 		 * Just open the lower vnode
557df8bae1dSRodney W. Grimes 		 */
558df8bae1dSRodney W. Grimes 		un->un_openl++;
559df8bae1dSRodney W. Grimes 		VOP_LOCK(tvp);
560df8bae1dSRodney W. Grimes 		error = VOP_OPEN(tvp, mode, cred, p);
561df8bae1dSRodney W. Grimes 		VOP_UNLOCK(tvp);
562df8bae1dSRodney W. Grimes 
563df8bae1dSRodney W. Grimes 		return (error);
564df8bae1dSRodney W. Grimes 	}
565df8bae1dSRodney W. Grimes 
566df8bae1dSRodney W. Grimes 	FIXUP(un);
567df8bae1dSRodney W. Grimes 
568df8bae1dSRodney W. Grimes 	error = VOP_OPEN(tvp, mode, cred, p);
569df8bae1dSRodney W. Grimes 
570df8bae1dSRodney W. Grimes 	return (error);
571df8bae1dSRodney W. Grimes }
572df8bae1dSRodney W. Grimes 
573df8bae1dSRodney W. Grimes int
574df8bae1dSRodney W. Grimes union_close(ap)
575df8bae1dSRodney W. Grimes 	struct vop_close_args /* {
576df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
577df8bae1dSRodney W. Grimes 		int  a_fflag;
578df8bae1dSRodney W. Grimes 		struct ucred *a_cred;
579df8bae1dSRodney W. Grimes 		struct proc *a_p;
580df8bae1dSRodney W. Grimes 	} */ *ap;
581df8bae1dSRodney W. Grimes {
582df8bae1dSRodney W. Grimes 	struct union_node *un = VTOUNION(ap->a_vp);
583df8bae1dSRodney W. Grimes 	struct vnode *vp;
584df8bae1dSRodney W. Grimes 
585df8bae1dSRodney W. Grimes 	if (un->un_uppervp) {
586df8bae1dSRodney W. Grimes 		vp = un->un_uppervp;
587df8bae1dSRodney W. Grimes 	} else {
588df8bae1dSRodney W. Grimes #ifdef UNION_DIAGNOSTIC
589df8bae1dSRodney W. Grimes 		if (un->un_openl <= 0)
590df8bae1dSRodney W. Grimes 			panic("union: un_openl cnt");
591df8bae1dSRodney W. Grimes #endif
592df8bae1dSRodney W. Grimes 		--un->un_openl;
593df8bae1dSRodney W. Grimes 		vp = un->un_lowervp;
594df8bae1dSRodney W. Grimes 	}
595df8bae1dSRodney W. Grimes 
596df8bae1dSRodney W. Grimes 	return (VOP_CLOSE(vp, ap->a_fflag, ap->a_cred, ap->a_p));
597df8bae1dSRodney W. Grimes }
598df8bae1dSRodney W. Grimes 
599df8bae1dSRodney W. Grimes /*
600df8bae1dSRodney W. Grimes  * Check access permission on the union vnode.
601df8bae1dSRodney W. Grimes  * The access check being enforced is to check
602df8bae1dSRodney W. Grimes  * against both the underlying vnode, and any
603df8bae1dSRodney W. Grimes  * copied vnode.  This ensures that no additional
604df8bae1dSRodney W. Grimes  * file permissions are given away simply because
605df8bae1dSRodney W. Grimes  * the user caused an implicit file copy.
606df8bae1dSRodney W. Grimes  */
607df8bae1dSRodney W. Grimes int
608df8bae1dSRodney W. Grimes union_access(ap)
609df8bae1dSRodney W. Grimes 	struct vop_access_args /* {
610df8bae1dSRodney W. Grimes 		struct vnodeop_desc *a_desc;
611df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
612df8bae1dSRodney W. Grimes 		int a_mode;
613df8bae1dSRodney W. Grimes 		struct ucred *a_cred;
614df8bae1dSRodney W. Grimes 		struct proc *a_p;
615df8bae1dSRodney W. Grimes 	} */ *ap;
616df8bae1dSRodney W. Grimes {
617df8bae1dSRodney W. Grimes 	struct union_node *un = VTOUNION(ap->a_vp);
618df8bae1dSRodney W. Grimes 	int error = EACCES;
619df8bae1dSRodney W. Grimes 	struct vnode *vp;
620df8bae1dSRodney W. Grimes 
6213a773ad0SPoul-Henning Kamp 	vp = un->un_uppervp;
6223a773ad0SPoul-Henning Kamp 	if (vp) {
623df8bae1dSRodney W. Grimes 		FIXUP(un);
624df8bae1dSRodney W. Grimes 		return (VOP_ACCESS(vp, ap->a_mode, ap->a_cred, ap->a_p));
625df8bae1dSRodney W. Grimes 	}
626df8bae1dSRodney W. Grimes 
6273a773ad0SPoul-Henning Kamp 	vp = un->un_lowervp;
6283a773ad0SPoul-Henning Kamp 	if (vp) {
629df8bae1dSRodney W. Grimes 		VOP_LOCK(vp);
630df8bae1dSRodney W. Grimes 		error = VOP_ACCESS(vp, ap->a_mode, ap->a_cred, ap->a_p);
631df8bae1dSRodney W. Grimes 		if (error == 0) {
632df8bae1dSRodney W. Grimes 			struct union_mount *um = MOUNTTOUNIONMOUNT(vp->v_mount);
633df8bae1dSRodney W. Grimes 
634df8bae1dSRodney W. Grimes 			if (um->um_op == UNMNT_BELOW)
635df8bae1dSRodney W. Grimes 				error = VOP_ACCESS(vp, ap->a_mode,
636df8bae1dSRodney W. Grimes 						um->um_cred, ap->a_p);
637df8bae1dSRodney W. Grimes 		}
638df8bae1dSRodney W. Grimes 		VOP_UNLOCK(vp);
639df8bae1dSRodney W. Grimes 		if (error)
640df8bae1dSRodney W. Grimes 			return (error);
641df8bae1dSRodney W. Grimes 	}
642df8bae1dSRodney W. Grimes 
643df8bae1dSRodney W. Grimes 	return (error);
644df8bae1dSRodney W. Grimes }
645df8bae1dSRodney W. Grimes 
646df8bae1dSRodney W. Grimes /*
647df8bae1dSRodney W. Grimes  *  We handle getattr only to change the fsid.
648df8bae1dSRodney W. Grimes  */
649df8bae1dSRodney W. Grimes int
650df8bae1dSRodney W. Grimes union_getattr(ap)
651df8bae1dSRodney W. Grimes 	struct vop_getattr_args /* {
652df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
653df8bae1dSRodney W. Grimes 		struct vattr *a_vap;
654df8bae1dSRodney W. Grimes 		struct ucred *a_cred;
655df8bae1dSRodney W. Grimes 		struct proc *a_p;
656df8bae1dSRodney W. Grimes 	} */ *ap;
657df8bae1dSRodney W. Grimes {
658df8bae1dSRodney W. Grimes 	int error;
659df8bae1dSRodney W. Grimes 	struct union_node *un = VTOUNION(ap->a_vp);
660df8bae1dSRodney W. Grimes 	struct vnode *vp = un->un_uppervp;
661df8bae1dSRodney W. Grimes 	struct vattr *vap;
662df8bae1dSRodney W. Grimes 	struct vattr va;
663df8bae1dSRodney W. Grimes 
664df8bae1dSRodney W. Grimes 
665df8bae1dSRodney W. Grimes 	/*
666df8bae1dSRodney W. Grimes 	 * Some programs walk the filesystem hierarchy by counting
667df8bae1dSRodney W. Grimes 	 * links to directories to avoid stat'ing all the time.
668df8bae1dSRodney W. Grimes 	 * This means the link count on directories needs to be "correct".
669df8bae1dSRodney W. Grimes 	 * The only way to do that is to call getattr on both layers
670df8bae1dSRodney W. Grimes 	 * and fix up the link count.  The link count will not necessarily
671df8bae1dSRodney W. Grimes 	 * be accurate but will be large enough to defeat the tree walkers.
672df8bae1dSRodney W. Grimes 	 */
673df8bae1dSRodney W. Grimes 
674df8bae1dSRodney W. Grimes 	vap = ap->a_vap;
675df8bae1dSRodney W. Grimes 
676df8bae1dSRodney W. Grimes 	vp = un->un_uppervp;
677df8bae1dSRodney W. Grimes 	if (vp != NULLVP) {
678df8bae1dSRodney W. Grimes 		FIXUP(un);
679df8bae1dSRodney W. Grimes 		error = VOP_GETATTR(vp, vap, ap->a_cred, ap->a_p);
680df8bae1dSRodney W. Grimes 		if (error)
681df8bae1dSRodney W. Grimes 			return (error);
682df8bae1dSRodney W. Grimes 	}
683df8bae1dSRodney W. Grimes 
684df8bae1dSRodney W. Grimes 	if (vp == NULLVP) {
685df8bae1dSRodney W. Grimes 		vp = un->un_lowervp;
686df8bae1dSRodney W. Grimes 	} else if (vp->v_type == VDIR) {
687df8bae1dSRodney W. Grimes 		vp = un->un_lowervp;
688df8bae1dSRodney W. Grimes 		vap = &va;
689df8bae1dSRodney W. Grimes 	} else {
690df8bae1dSRodney W. Grimes 		vp = NULLVP;
691df8bae1dSRodney W. Grimes 	}
692df8bae1dSRodney W. Grimes 
693df8bae1dSRodney W. Grimes 	if (vp != NULLVP) {
694df8bae1dSRodney W. Grimes 		VOP_LOCK(vp);
695df8bae1dSRodney W. Grimes 		error = VOP_GETATTR(vp, vap, ap->a_cred, ap->a_p);
696df8bae1dSRodney W. Grimes 		VOP_UNLOCK(vp);
697df8bae1dSRodney W. Grimes 		if (error)
698df8bae1dSRodney W. Grimes 			return (error);
699df8bae1dSRodney W. Grimes 	}
700df8bae1dSRodney W. Grimes 
701df8bae1dSRodney W. Grimes 	if ((vap != ap->a_vap) && (vap->va_type == VDIR))
702df8bae1dSRodney W. Grimes 		ap->a_vap->va_nlink += vap->va_nlink;
703df8bae1dSRodney W. Grimes 
704df8bae1dSRodney W. Grimes 	vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsid.val[0];
705df8bae1dSRodney W. Grimes 	return (0);
706df8bae1dSRodney W. Grimes }
707df8bae1dSRodney W. Grimes 
708df8bae1dSRodney W. Grimes int
709df8bae1dSRodney W. Grimes union_setattr(ap)
710df8bae1dSRodney W. Grimes 	struct vop_setattr_args /* {
711df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
712df8bae1dSRodney W. Grimes 		struct vattr *a_vap;
713df8bae1dSRodney W. Grimes 		struct ucred *a_cred;
714df8bae1dSRodney W. Grimes 		struct proc *a_p;
715df8bae1dSRodney W. Grimes 	} */ *ap;
716df8bae1dSRodney W. Grimes {
717df8bae1dSRodney W. Grimes 	struct union_node *un = VTOUNION(ap->a_vp);
718df8bae1dSRodney W. Grimes 	int error;
719df8bae1dSRodney W. Grimes 
720df8bae1dSRodney W. Grimes 	/*
721df8bae1dSRodney W. Grimes 	 * Handle case of truncating lower object to zero size,
722df8bae1dSRodney W. Grimes 	 * by creating a zero length upper object.  This is to
723df8bae1dSRodney W. Grimes 	 * handle the case of open with O_TRUNC and O_CREAT.
724df8bae1dSRodney W. Grimes 	 */
725df8bae1dSRodney W. Grimes 	if ((un->un_uppervp == NULLVP) &&
726df8bae1dSRodney W. Grimes 	    /* assert(un->un_lowervp != NULLVP) */
727df8bae1dSRodney W. Grimes 	    (un->un_lowervp->v_type == VREG) &&
728df8bae1dSRodney W. Grimes 	    (ap->a_vap->va_size == 0)) {
729df8bae1dSRodney W. Grimes 		struct vnode *vp;
730df8bae1dSRodney W. Grimes 
731df8bae1dSRodney W. Grimes 		error = union_vn_create(&vp, un, ap->a_p);
732df8bae1dSRodney W. Grimes 		if (error)
733df8bae1dSRodney W. Grimes 			return (error);
734df8bae1dSRodney W. Grimes 
735df8bae1dSRodney W. Grimes 		/* at this point, uppervp is locked */
736df8bae1dSRodney W. Grimes 		union_newupper(un, vp);
737df8bae1dSRodney W. Grimes 
738df8bae1dSRodney W. Grimes 		VOP_UNLOCK(vp);
739df8bae1dSRodney W. Grimes 		union_vn_close(un->un_uppervp, FWRITE, ap->a_cred, ap->a_p);
740df8bae1dSRodney W. Grimes 		VOP_LOCK(vp);
741df8bae1dSRodney W. Grimes 		un->un_flags |= UN_ULOCK;
742df8bae1dSRodney W. Grimes 	}
743df8bae1dSRodney W. Grimes 
744df8bae1dSRodney W. Grimes 	/*
745df8bae1dSRodney W. Grimes 	 * Try to set attributes in upper layer,
746df8bae1dSRodney W. Grimes 	 * otherwise return read-only filesystem error.
747df8bae1dSRodney W. Grimes 	 */
748df8bae1dSRodney W. Grimes 	if (un->un_uppervp != NULLVP) {
749df8bae1dSRodney W. Grimes 		FIXUP(un);
750df8bae1dSRodney W. Grimes 		error = VOP_SETATTR(un->un_uppervp, ap->a_vap,
751df8bae1dSRodney W. Grimes 					ap->a_cred, ap->a_p);
752df8bae1dSRodney W. Grimes 	} else {
753df8bae1dSRodney W. Grimes 		error = EROFS;
754df8bae1dSRodney W. Grimes 	}
755df8bae1dSRodney W. Grimes 
756df8bae1dSRodney W. Grimes 	return (error);
757df8bae1dSRodney W. Grimes }
758df8bae1dSRodney W. Grimes 
759df8bae1dSRodney W. Grimes int
760df8bae1dSRodney W. Grimes union_read(ap)
761df8bae1dSRodney W. Grimes 	struct vop_read_args /* {
762df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
763df8bae1dSRodney W. Grimes 		struct uio *a_uio;
764df8bae1dSRodney W. Grimes 		int  a_ioflag;
765df8bae1dSRodney W. Grimes 		struct ucred *a_cred;
766df8bae1dSRodney W. Grimes 	} */ *ap;
767df8bae1dSRodney W. Grimes {
768df8bae1dSRodney W. Grimes 	int error;
769df8bae1dSRodney W. Grimes 	struct vnode *vp = OTHERVP(ap->a_vp);
770df8bae1dSRodney W. Grimes 	int dolock = (vp == LOWERVP(ap->a_vp));
771df8bae1dSRodney W. Grimes 
772df8bae1dSRodney W. Grimes 	if (dolock)
773df8bae1dSRodney W. Grimes 		VOP_LOCK(vp);
774df8bae1dSRodney W. Grimes 	else
775df8bae1dSRodney W. Grimes 		FIXUP(VTOUNION(ap->a_vp));
776df8bae1dSRodney W. Grimes 	error = VOP_READ(vp, ap->a_uio, ap->a_ioflag, ap->a_cred);
777df8bae1dSRodney W. Grimes 	if (dolock)
778df8bae1dSRodney W. Grimes 		VOP_UNLOCK(vp);
779df8bae1dSRodney W. Grimes 
780df8bae1dSRodney W. Grimes 	return (error);
781df8bae1dSRodney W. Grimes }
782df8bae1dSRodney W. Grimes 
783df8bae1dSRodney W. Grimes int
784df8bae1dSRodney W. Grimes union_write(ap)
785df8bae1dSRodney W. Grimes 	struct vop_read_args /* {
786df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
787df8bae1dSRodney W. Grimes 		struct uio *a_uio;
788df8bae1dSRodney W. Grimes 		int  a_ioflag;
789df8bae1dSRodney W. Grimes 		struct ucred *a_cred;
790df8bae1dSRodney W. Grimes 	} */ *ap;
791df8bae1dSRodney W. Grimes {
792df8bae1dSRodney W. Grimes 	int error;
793df8bae1dSRodney W. Grimes 	struct vnode *vp = OTHERVP(ap->a_vp);
794df8bae1dSRodney W. Grimes 	int dolock = (vp == LOWERVP(ap->a_vp));
795df8bae1dSRodney W. Grimes 
796df8bae1dSRodney W. Grimes 	if (dolock)
797df8bae1dSRodney W. Grimes 		VOP_LOCK(vp);
798df8bae1dSRodney W. Grimes 	else
799df8bae1dSRodney W. Grimes 		FIXUP(VTOUNION(ap->a_vp));
800df8bae1dSRodney W. Grimes 	error = VOP_WRITE(vp, ap->a_uio, ap->a_ioflag, ap->a_cred);
801df8bae1dSRodney W. Grimes 	if (dolock)
802df8bae1dSRodney W. Grimes 		VOP_UNLOCK(vp);
803df8bae1dSRodney W. Grimes 
804df8bae1dSRodney W. Grimes 	return (error);
805df8bae1dSRodney W. Grimes }
806df8bae1dSRodney W. Grimes 
807df8bae1dSRodney W. Grimes int
808df8bae1dSRodney W. Grimes union_ioctl(ap)
809df8bae1dSRodney W. Grimes 	struct vop_ioctl_args /* {
810df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
811df8bae1dSRodney W. Grimes 		int  a_command;
812df8bae1dSRodney W. Grimes 		caddr_t  a_data;
813df8bae1dSRodney W. Grimes 		int  a_fflag;
814df8bae1dSRodney W. Grimes 		struct ucred *a_cred;
815df8bae1dSRodney W. Grimes 		struct proc *a_p;
816df8bae1dSRodney W. Grimes 	} */ *ap;
817df8bae1dSRodney W. Grimes {
818df8bae1dSRodney W. Grimes 
819df8bae1dSRodney W. Grimes 	return (VOP_IOCTL(OTHERVP(ap->a_vp), ap->a_command, ap->a_data,
820df8bae1dSRodney W. Grimes 				ap->a_fflag, ap->a_cred, ap->a_p));
821df8bae1dSRodney W. Grimes }
822df8bae1dSRodney W. Grimes 
823df8bae1dSRodney W. Grimes int
824df8bae1dSRodney W. Grimes union_select(ap)
825df8bae1dSRodney W. Grimes 	struct vop_select_args /* {
826df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
827df8bae1dSRodney W. Grimes 		int  a_which;
828df8bae1dSRodney W. Grimes 		int  a_fflags;
829df8bae1dSRodney W. Grimes 		struct ucred *a_cred;
830df8bae1dSRodney W. Grimes 		struct proc *a_p;
831df8bae1dSRodney W. Grimes 	} */ *ap;
832df8bae1dSRodney W. Grimes {
833df8bae1dSRodney W. Grimes 
834df8bae1dSRodney W. Grimes 	return (VOP_SELECT(OTHERVP(ap->a_vp), ap->a_which, ap->a_fflags,
835df8bae1dSRodney W. Grimes 				ap->a_cred, ap->a_p));
836df8bae1dSRodney W. Grimes }
837df8bae1dSRodney W. Grimes 
838df8bae1dSRodney W. Grimes int
839df8bae1dSRodney W. Grimes union_mmap(ap)
840df8bae1dSRodney W. Grimes 	struct vop_mmap_args /* {
841df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
842df8bae1dSRodney W. Grimes 		int  a_fflags;
843df8bae1dSRodney W. Grimes 		struct ucred *a_cred;
844df8bae1dSRodney W. Grimes 		struct proc *a_p;
845df8bae1dSRodney W. Grimes 	} */ *ap;
846df8bae1dSRodney W. Grimes {
847df8bae1dSRodney W. Grimes 
848df8bae1dSRodney W. Grimes 	return (VOP_MMAP(OTHERVP(ap->a_vp), ap->a_fflags,
849df8bae1dSRodney W. Grimes 				ap->a_cred, ap->a_p));
850df8bae1dSRodney W. Grimes }
851df8bae1dSRodney W. Grimes 
852df8bae1dSRodney W. Grimes int
853df8bae1dSRodney W. Grimes union_fsync(ap)
854df8bae1dSRodney W. Grimes 	struct vop_fsync_args /* {
855df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
856df8bae1dSRodney W. Grimes 		struct ucred *a_cred;
857df8bae1dSRodney W. Grimes 		int  a_waitfor;
858df8bae1dSRodney W. Grimes 		struct proc *a_p;
859df8bae1dSRodney W. Grimes 	} */ *ap;
860df8bae1dSRodney W. Grimes {
861df8bae1dSRodney W. Grimes 	int error = 0;
862df8bae1dSRodney W. Grimes 	struct vnode *targetvp = OTHERVP(ap->a_vp);
863df8bae1dSRodney W. Grimes 
864df8bae1dSRodney W. Grimes 	if (targetvp) {
865df8bae1dSRodney W. Grimes 		int dolock = (targetvp == LOWERVP(ap->a_vp));
866df8bae1dSRodney W. Grimes 
867df8bae1dSRodney W. Grimes 		if (dolock)
868df8bae1dSRodney W. Grimes 			VOP_LOCK(targetvp);
869df8bae1dSRodney W. Grimes 		else
870df8bae1dSRodney W. Grimes 			FIXUP(VTOUNION(ap->a_vp));
871df8bae1dSRodney W. Grimes 		error = VOP_FSYNC(targetvp, ap->a_cred,
872df8bae1dSRodney W. Grimes 					ap->a_waitfor, ap->a_p);
873df8bae1dSRodney W. Grimes 		if (dolock)
874df8bae1dSRodney W. Grimes 			VOP_UNLOCK(targetvp);
875df8bae1dSRodney W. Grimes 	}
876df8bae1dSRodney W. Grimes 
877df8bae1dSRodney W. Grimes 	return (error);
878df8bae1dSRodney W. Grimes }
879df8bae1dSRodney W. Grimes 
880df8bae1dSRodney W. Grimes int
881df8bae1dSRodney W. Grimes union_seek(ap)
882df8bae1dSRodney W. Grimes 	struct vop_seek_args /* {
883df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
884df8bae1dSRodney W. Grimes 		off_t  a_oldoff;
885df8bae1dSRodney W. Grimes 		off_t  a_newoff;
886df8bae1dSRodney W. Grimes 		struct ucred *a_cred;
887df8bae1dSRodney W. Grimes 	} */ *ap;
888df8bae1dSRodney W. Grimes {
889df8bae1dSRodney W. Grimes 
890df8bae1dSRodney W. Grimes 	return (VOP_SEEK(OTHERVP(ap->a_vp), ap->a_oldoff, ap->a_newoff, ap->a_cred));
891df8bae1dSRodney W. Grimes }
892df8bae1dSRodney W. Grimes 
893df8bae1dSRodney W. Grimes int
894df8bae1dSRodney W. Grimes union_remove(ap)
895df8bae1dSRodney W. Grimes 	struct vop_remove_args /* {
896df8bae1dSRodney W. Grimes 		struct vnode *a_dvp;
897df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
898df8bae1dSRodney W. Grimes 		struct componentname *a_cnp;
899df8bae1dSRodney W. Grimes 	} */ *ap;
900df8bae1dSRodney W. Grimes {
901df8bae1dSRodney W. Grimes 	int error;
902df8bae1dSRodney W. Grimes 	struct union_node *dun = VTOUNION(ap->a_dvp);
903df8bae1dSRodney W. Grimes 	struct union_node *un = VTOUNION(ap->a_vp);
904df8bae1dSRodney W. Grimes 
905df8bae1dSRodney W. Grimes 	if (dun->un_uppervp && un->un_uppervp) {
906df8bae1dSRodney W. Grimes 		struct vnode *dvp = dun->un_uppervp;
907df8bae1dSRodney W. Grimes 		struct vnode *vp = un->un_uppervp;
908df8bae1dSRodney W. Grimes 
909df8bae1dSRodney W. Grimes 		FIXUP(dun);
910df8bae1dSRodney W. Grimes 		VREF(dvp);
911df8bae1dSRodney W. Grimes 		dun->un_flags |= UN_KLOCK;
912df8bae1dSRodney W. Grimes 		vput(ap->a_dvp);
913df8bae1dSRodney W. Grimes 		FIXUP(un);
914df8bae1dSRodney W. Grimes 		VREF(vp);
915df8bae1dSRodney W. Grimes 		un->un_flags |= UN_KLOCK;
916df8bae1dSRodney W. Grimes 		vput(ap->a_vp);
917df8bae1dSRodney W. Grimes 
918df8bae1dSRodney W. Grimes 		error = VOP_REMOVE(dvp, vp, ap->a_cnp);
919df8bae1dSRodney W. Grimes 		if (!error)
920df8bae1dSRodney W. Grimes 			union_removed_upper(un);
921df8bae1dSRodney W. Grimes 
922df8bae1dSRodney W. Grimes 		/*
923df8bae1dSRodney W. Grimes 		 * XXX: should create a whiteout here
924df8bae1dSRodney W. Grimes 		 */
925df8bae1dSRodney W. Grimes 	} else {
926df8bae1dSRodney W. Grimes 		/*
927df8bae1dSRodney W. Grimes 		 * XXX: should create a whiteout here
928df8bae1dSRodney W. Grimes 		 */
929df8bae1dSRodney W. Grimes 		vput(ap->a_dvp);
930df8bae1dSRodney W. Grimes 		vput(ap->a_vp);
931df8bae1dSRodney W. Grimes 		error = EROFS;
932df8bae1dSRodney W. Grimes 	}
933df8bae1dSRodney W. Grimes 
934df8bae1dSRodney W. Grimes 	return (error);
935df8bae1dSRodney W. Grimes }
936df8bae1dSRodney W. Grimes 
937df8bae1dSRodney W. Grimes int
938df8bae1dSRodney W. Grimes union_link(ap)
939df8bae1dSRodney W. Grimes 	struct vop_link_args /* {
940df8bae1dSRodney W. Grimes 		struct vnode *a_tdvp;
94147777413SDavid Greenman 		struct vnode *a_vp;
942df8bae1dSRodney W. Grimes 		struct componentname *a_cnp;
943df8bae1dSRodney W. Grimes 	} */ *ap;
944df8bae1dSRodney W. Grimes {
945df8bae1dSRodney W. Grimes 	int error;
946df8bae1dSRodney W. Grimes 	struct union_node *dun = VTOUNION(ap->a_vp);
947df8bae1dSRodney W. Grimes 	struct union_node *un = VTOUNION(ap->a_tdvp);
948df8bae1dSRodney W. Grimes 
949df8bae1dSRodney W. Grimes 	if (dun->un_uppervp && un->un_uppervp) {
950df8bae1dSRodney W. Grimes 		struct vnode *dvp = dun->un_uppervp;
951df8bae1dSRodney W. Grimes 		struct vnode *vp = un->un_uppervp;
952df8bae1dSRodney W. Grimes 
953df8bae1dSRodney W. Grimes 		FIXUP(dun);
954df8bae1dSRodney W. Grimes 		VREF(dvp);
955df8bae1dSRodney W. Grimes 		dun->un_flags |= UN_KLOCK;
956df8bae1dSRodney W. Grimes 		vput(ap->a_vp);
957df8bae1dSRodney W. Grimes 		FIXUP(un);
958df8bae1dSRodney W. Grimes 		VREF(vp);
959df8bae1dSRodney W. Grimes 		vrele(ap->a_tdvp);
960df8bae1dSRodney W. Grimes 
96147777413SDavid Greenman 		error = VOP_LINK(dvp, vp, ap->a_cnp);
962df8bae1dSRodney W. Grimes 	} else {
963df8bae1dSRodney W. Grimes 		/*
964df8bae1dSRodney W. Grimes 		 * XXX: need to copy to upper layer
965df8bae1dSRodney W. Grimes 		 * and do the link there.
966df8bae1dSRodney W. Grimes 		 */
967df8bae1dSRodney W. Grimes 		vput(ap->a_vp);
968df8bae1dSRodney W. Grimes 		vrele(ap->a_tdvp);
969df8bae1dSRodney W. Grimes 		error = EROFS;
970df8bae1dSRodney W. Grimes 	}
971df8bae1dSRodney W. Grimes 
972df8bae1dSRodney W. Grimes 	return (error);
973df8bae1dSRodney W. Grimes }
974df8bae1dSRodney W. Grimes 
975df8bae1dSRodney W. Grimes int
976df8bae1dSRodney W. Grimes union_rename(ap)
977df8bae1dSRodney W. Grimes 	struct vop_rename_args  /* {
978df8bae1dSRodney W. Grimes 		struct vnode *a_fdvp;
979df8bae1dSRodney W. Grimes 		struct vnode *a_fvp;
980df8bae1dSRodney W. Grimes 		struct componentname *a_fcnp;
981df8bae1dSRodney W. Grimes 		struct vnode *a_tdvp;
982df8bae1dSRodney W. Grimes 		struct vnode *a_tvp;
983df8bae1dSRodney W. Grimes 		struct componentname *a_tcnp;
984df8bae1dSRodney W. Grimes 	} */ *ap;
985df8bae1dSRodney W. Grimes {
986df8bae1dSRodney W. Grimes 	int error;
987df8bae1dSRodney W. Grimes 
988df8bae1dSRodney W. Grimes 	struct vnode *fdvp = ap->a_fdvp;
989df8bae1dSRodney W. Grimes 	struct vnode *fvp = ap->a_fvp;
990df8bae1dSRodney W. Grimes 	struct vnode *tdvp = ap->a_tdvp;
991df8bae1dSRodney W. Grimes 	struct vnode *tvp = ap->a_tvp;
992df8bae1dSRodney W. Grimes 
993df8bae1dSRodney W. Grimes 	if (fdvp->v_op == union_vnodeop_p) {	/* always true */
994df8bae1dSRodney W. Grimes 		struct union_node *un = VTOUNION(fdvp);
995df8bae1dSRodney W. Grimes 		if (un->un_uppervp == NULLVP) {
996df8bae1dSRodney W. Grimes 			error = EROFS;
997df8bae1dSRodney W. Grimes 			goto bad;
998df8bae1dSRodney W. Grimes 		}
999df8bae1dSRodney W. Grimes 
1000df8bae1dSRodney W. Grimes 		FIXUP(un);
1001df8bae1dSRodney W. Grimes 		fdvp = un->un_uppervp;
1002df8bae1dSRodney W. Grimes 		VREF(fdvp);
1003df8bae1dSRodney W. Grimes 		vrele(ap->a_fdvp);
1004df8bae1dSRodney W. Grimes 	}
1005df8bae1dSRodney W. Grimes 
1006df8bae1dSRodney W. Grimes 	if (fvp->v_op == union_vnodeop_p) {	/* always true */
1007df8bae1dSRodney W. Grimes 		struct union_node *un = VTOUNION(fvp);
1008df8bae1dSRodney W. Grimes 		if (un->un_uppervp == NULLVP) {
1009df8bae1dSRodney W. Grimes 			error = EROFS;
1010df8bae1dSRodney W. Grimes 			goto bad;
1011df8bae1dSRodney W. Grimes 		}
1012df8bae1dSRodney W. Grimes 
1013df8bae1dSRodney W. Grimes 		FIXUP(un);
1014df8bae1dSRodney W. Grimes 		fvp = un->un_uppervp;
1015df8bae1dSRodney W. Grimes 		VREF(fvp);
1016df8bae1dSRodney W. Grimes 		vrele(ap->a_fvp);
1017df8bae1dSRodney W. Grimes 	}
1018df8bae1dSRodney W. Grimes 
1019df8bae1dSRodney W. Grimes 	if (tdvp->v_op == union_vnodeop_p) {
1020df8bae1dSRodney W. Grimes 		struct union_node *un = VTOUNION(tdvp);
1021df8bae1dSRodney W. Grimes 		if (un->un_uppervp == NULLVP) {
1022df8bae1dSRodney W. Grimes 			error = EROFS;
1023df8bae1dSRodney W. Grimes 			goto bad;
1024df8bae1dSRodney W. Grimes 		}
1025df8bae1dSRodney W. Grimes 
1026df8bae1dSRodney W. Grimes 		tdvp = un->un_uppervp;
1027df8bae1dSRodney W. Grimes 		VREF(tdvp);
1028df8bae1dSRodney W. Grimes 		un->un_flags |= UN_KLOCK;
1029df8bae1dSRodney W. Grimes 		vput(ap->a_tdvp);
1030df8bae1dSRodney W. Grimes 	}
1031df8bae1dSRodney W. Grimes 
1032df8bae1dSRodney W. Grimes 	if (tvp && tvp->v_op == union_vnodeop_p) {
1033df8bae1dSRodney W. Grimes 		struct union_node *un = VTOUNION(tvp);
1034df8bae1dSRodney W. Grimes 		if (un->un_uppervp == NULLVP) {
1035df8bae1dSRodney W. Grimes 			error = EROFS;
1036df8bae1dSRodney W. Grimes 			goto bad;
1037df8bae1dSRodney W. Grimes 		}
1038df8bae1dSRodney W. Grimes 
1039df8bae1dSRodney W. Grimes 		tvp = un->un_uppervp;
1040df8bae1dSRodney W. Grimes 		VREF(tvp);
1041df8bae1dSRodney W. Grimes 		un->un_flags |= UN_KLOCK;
1042df8bae1dSRodney W. Grimes 		vput(ap->a_tvp);
1043df8bae1dSRodney W. Grimes 	}
1044df8bae1dSRodney W. Grimes 
1045df8bae1dSRodney W. Grimes 	return (VOP_RENAME(fdvp, fvp, ap->a_fcnp, tdvp, tvp, ap->a_tcnp));
1046df8bae1dSRodney W. Grimes 
1047df8bae1dSRodney W. Grimes bad:
1048df8bae1dSRodney W. Grimes 	vrele(fdvp);
1049df8bae1dSRodney W. Grimes 	vrele(fvp);
1050df8bae1dSRodney W. Grimes 	vput(tdvp);
1051df8bae1dSRodney W. Grimes 	if (tvp)
1052df8bae1dSRodney W. Grimes 		vput(tvp);
1053df8bae1dSRodney W. Grimes 
1054df8bae1dSRodney W. Grimes 	return (error);
1055df8bae1dSRodney W. Grimes }
1056df8bae1dSRodney W. Grimes 
1057df8bae1dSRodney W. Grimes int
1058df8bae1dSRodney W. Grimes union_mkdir(ap)
1059df8bae1dSRodney W. Grimes 	struct vop_mkdir_args /* {
1060df8bae1dSRodney W. Grimes 		struct vnode *a_dvp;
1061df8bae1dSRodney W. Grimes 		struct vnode **a_vpp;
1062df8bae1dSRodney W. Grimes 		struct componentname *a_cnp;
1063df8bae1dSRodney W. Grimes 		struct vattr *a_vap;
1064df8bae1dSRodney W. Grimes 	} */ *ap;
1065df8bae1dSRodney W. Grimes {
1066df8bae1dSRodney W. Grimes 	struct union_node *un = VTOUNION(ap->a_dvp);
1067df8bae1dSRodney W. Grimes 	struct vnode *dvp = un->un_uppervp;
1068df8bae1dSRodney W. Grimes 
1069df8bae1dSRodney W. Grimes 	if (dvp) {
1070df8bae1dSRodney W. Grimes 		int error;
1071df8bae1dSRodney W. Grimes 		struct vnode *vp;
1072df8bae1dSRodney W. Grimes 
1073df8bae1dSRodney W. Grimes 		FIXUP(un);
1074df8bae1dSRodney W. Grimes 		VREF(dvp);
1075df8bae1dSRodney W. Grimes 		un->un_flags |= UN_KLOCK;
1076df8bae1dSRodney W. Grimes 		vput(ap->a_dvp);
1077df8bae1dSRodney W. Grimes 		error = VOP_MKDIR(dvp, &vp, ap->a_cnp, ap->a_vap);
1078df8bae1dSRodney W. Grimes 		if (error)
1079df8bae1dSRodney W. Grimes 			return (error);
1080df8bae1dSRodney W. Grimes 
1081df8bae1dSRodney W. Grimes 		error = union_allocvp(
1082df8bae1dSRodney W. Grimes 				ap->a_vpp,
1083df8bae1dSRodney W. Grimes 				ap->a_dvp->v_mount,
1084df8bae1dSRodney W. Grimes 				ap->a_dvp,
1085df8bae1dSRodney W. Grimes 				NULLVP,
1086df8bae1dSRodney W. Grimes 				ap->a_cnp,
1087df8bae1dSRodney W. Grimes 				vp,
1088df8bae1dSRodney W. Grimes 				NULLVP);
1089df8bae1dSRodney W. Grimes 		if (error)
1090df8bae1dSRodney W. Grimes 			vput(vp);
1091df8bae1dSRodney W. Grimes 		return (error);
1092df8bae1dSRodney W. Grimes 	}
1093df8bae1dSRodney W. Grimes 
1094df8bae1dSRodney W. Grimes 	vput(ap->a_dvp);
1095df8bae1dSRodney W. Grimes 	return (EROFS);
1096df8bae1dSRodney W. Grimes }
1097df8bae1dSRodney W. Grimes 
1098df8bae1dSRodney W. Grimes int
1099df8bae1dSRodney W. Grimes union_rmdir(ap)
1100df8bae1dSRodney W. Grimes 	struct vop_rmdir_args /* {
1101df8bae1dSRodney W. Grimes 		struct vnode *a_dvp;
1102df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
1103df8bae1dSRodney W. Grimes 		struct componentname *a_cnp;
1104df8bae1dSRodney W. Grimes 	} */ *ap;
1105df8bae1dSRodney W. Grimes {
1106df8bae1dSRodney W. Grimes 	int error;
1107df8bae1dSRodney W. Grimes 	struct union_node *dun = VTOUNION(ap->a_dvp);
1108df8bae1dSRodney W. Grimes 	struct union_node *un = VTOUNION(ap->a_vp);
1109df8bae1dSRodney W. Grimes 
1110df8bae1dSRodney W. Grimes 	if (dun->un_uppervp && un->un_uppervp) {
1111df8bae1dSRodney W. Grimes 		struct vnode *dvp = dun->un_uppervp;
1112df8bae1dSRodney W. Grimes 		struct vnode *vp = un->un_uppervp;
1113df8bae1dSRodney W. Grimes 
1114df8bae1dSRodney W. Grimes 		FIXUP(dun);
1115df8bae1dSRodney W. Grimes 		VREF(dvp);
1116df8bae1dSRodney W. Grimes 		dun->un_flags |= UN_KLOCK;
1117df8bae1dSRodney W. Grimes 		vput(ap->a_dvp);
1118df8bae1dSRodney W. Grimes 		FIXUP(un);
1119df8bae1dSRodney W. Grimes 		VREF(vp);
1120df8bae1dSRodney W. Grimes 		un->un_flags |= UN_KLOCK;
1121df8bae1dSRodney W. Grimes 		vput(ap->a_vp);
1122df8bae1dSRodney W. Grimes 
1123df8bae1dSRodney W. Grimes 		error = VOP_RMDIR(dvp, vp, ap->a_cnp);
1124df8bae1dSRodney W. Grimes 		if (!error)
1125df8bae1dSRodney W. Grimes 			union_removed_upper(un);
1126df8bae1dSRodney W. Grimes 
1127df8bae1dSRodney W. Grimes 		/*
1128df8bae1dSRodney W. Grimes 		 * XXX: should create a whiteout here
1129df8bae1dSRodney W. Grimes 		 */
1130df8bae1dSRodney W. Grimes 	} else {
1131df8bae1dSRodney W. Grimes 		/*
1132df8bae1dSRodney W. Grimes 		 * XXX: should create a whiteout here
1133df8bae1dSRodney W. Grimes 		 */
1134df8bae1dSRodney W. Grimes 		vput(ap->a_dvp);
1135df8bae1dSRodney W. Grimes 		vput(ap->a_vp);
1136df8bae1dSRodney W. Grimes 		error = EROFS;
1137df8bae1dSRodney W. Grimes 	}
1138df8bae1dSRodney W. Grimes 
1139df8bae1dSRodney W. Grimes 	return (error);
1140df8bae1dSRodney W. Grimes }
1141df8bae1dSRodney W. Grimes 
1142df8bae1dSRodney W. Grimes int
1143df8bae1dSRodney W. Grimes union_symlink(ap)
1144df8bae1dSRodney W. Grimes 	struct vop_symlink_args /* {
1145df8bae1dSRodney W. Grimes 		struct vnode *a_dvp;
1146df8bae1dSRodney W. Grimes 		struct vnode **a_vpp;
1147df8bae1dSRodney W. Grimes 		struct componentname *a_cnp;
1148df8bae1dSRodney W. Grimes 		struct vattr *a_vap;
1149df8bae1dSRodney W. Grimes 		char *a_target;
1150df8bae1dSRodney W. Grimes 	} */ *ap;
1151df8bae1dSRodney W. Grimes {
1152df8bae1dSRodney W. Grimes 	struct union_node *un = VTOUNION(ap->a_dvp);
1153df8bae1dSRodney W. Grimes 	struct vnode *dvp = un->un_uppervp;
1154df8bae1dSRodney W. Grimes 
1155df8bae1dSRodney W. Grimes 	if (dvp) {
1156df8bae1dSRodney W. Grimes 		int error;
1157df8bae1dSRodney W. Grimes 		struct vnode *vp;
1158df8bae1dSRodney W. Grimes 
1159df8bae1dSRodney W. Grimes 		FIXUP(un);
1160df8bae1dSRodney W. Grimes 		VREF(dvp);
1161df8bae1dSRodney W. Grimes 		un->un_flags |= UN_KLOCK;
1162df8bae1dSRodney W. Grimes 		vput(ap->a_dvp);
1163df8bae1dSRodney W. Grimes 		error = VOP_SYMLINK(dvp, &vp, ap->a_cnp,
1164df8bae1dSRodney W. Grimes 					ap->a_vap, ap->a_target);
1165df8bae1dSRodney W. Grimes 		*ap->a_vpp = NULLVP;
1166df8bae1dSRodney W. Grimes 		return (error);
1167df8bae1dSRodney W. Grimes 	}
1168df8bae1dSRodney W. Grimes 
1169df8bae1dSRodney W. Grimes 	vput(ap->a_dvp);
1170df8bae1dSRodney W. Grimes 	return (EROFS);
1171df8bae1dSRodney W. Grimes }
1172df8bae1dSRodney W. Grimes 
1173df8bae1dSRodney W. Grimes /*
1174df8bae1dSRodney W. Grimes  * union_readdir works in concert with getdirentries and
1175df8bae1dSRodney W. Grimes  * readdir(3) to provide a list of entries in the unioned
1176df8bae1dSRodney W. Grimes  * directories.  getdirentries is responsible for walking
1177df8bae1dSRodney W. Grimes  * down the union stack.  readdir(3) is responsible for
1178df8bae1dSRodney W. Grimes  * eliminating duplicate names from the returned data stream.
1179df8bae1dSRodney W. Grimes  */
1180df8bae1dSRodney W. Grimes int
1181df8bae1dSRodney W. Grimes union_readdir(ap)
1182df8bae1dSRodney W. Grimes 	struct vop_readdir_args /* {
1183df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
1184df8bae1dSRodney W. Grimes 		struct uio *a_uio;
1185df8bae1dSRodney W. Grimes 		struct ucred *a_cred;
1186df8bae1dSRodney W. Grimes 	} */ *ap;
1187df8bae1dSRodney W. Grimes {
1188df8bae1dSRodney W. Grimes 	int error = 0;
1189df8bae1dSRodney W. Grimes 	struct union_node *un = VTOUNION(ap->a_vp);
1190df8bae1dSRodney W. Grimes 
1191df8bae1dSRodney W. Grimes 	if (un->un_uppervp) {
1192df8bae1dSRodney W. Grimes 		FIXUP(un);
1193c4a7b7e1SDavid Greenman 		error = VOP_READDIR(un->un_uppervp, ap->a_uio, ap->a_cred, NULL, NULL, NULL);
1194df8bae1dSRodney W. Grimes 	}
1195df8bae1dSRodney W. Grimes 
1196df8bae1dSRodney W. Grimes 	return (error);
1197df8bae1dSRodney W. Grimes }
1198df8bae1dSRodney W. Grimes 
1199df8bae1dSRodney W. Grimes int
1200df8bae1dSRodney W. Grimes union_readlink(ap)
1201df8bae1dSRodney W. Grimes 	struct vop_readlink_args /* {
1202df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
1203df8bae1dSRodney W. Grimes 		struct uio *a_uio;
1204df8bae1dSRodney W. Grimes 		struct ucred *a_cred;
1205df8bae1dSRodney W. Grimes 	} */ *ap;
1206df8bae1dSRodney W. Grimes {
1207df8bae1dSRodney W. Grimes 	int error;
1208df8bae1dSRodney W. Grimes 	struct vnode *vp = OTHERVP(ap->a_vp);
1209df8bae1dSRodney W. Grimes 	int dolock = (vp == LOWERVP(ap->a_vp));
1210df8bae1dSRodney W. Grimes 
1211df8bae1dSRodney W. Grimes 	if (dolock)
1212df8bae1dSRodney W. Grimes 		VOP_LOCK(vp);
1213df8bae1dSRodney W. Grimes 	else
1214df8bae1dSRodney W. Grimes 		FIXUP(VTOUNION(ap->a_vp));
1215df8bae1dSRodney W. Grimes 	error = VOP_READLINK(vp, ap->a_uio, ap->a_cred);
1216df8bae1dSRodney W. Grimes 	if (dolock)
1217df8bae1dSRodney W. Grimes 		VOP_UNLOCK(vp);
1218df8bae1dSRodney W. Grimes 
1219df8bae1dSRodney W. Grimes 	return (error);
1220df8bae1dSRodney W. Grimes }
1221df8bae1dSRodney W. Grimes 
1222df8bae1dSRodney W. Grimes int
1223df8bae1dSRodney W. Grimes union_abortop(ap)
1224df8bae1dSRodney W. Grimes 	struct vop_abortop_args /* {
1225df8bae1dSRodney W. Grimes 		struct vnode *a_dvp;
1226df8bae1dSRodney W. Grimes 		struct componentname *a_cnp;
1227df8bae1dSRodney W. Grimes 	} */ *ap;
1228df8bae1dSRodney W. Grimes {
1229df8bae1dSRodney W. Grimes 	int error;
1230df8bae1dSRodney W. Grimes 	struct vnode *vp = OTHERVP(ap->a_dvp);
1231df8bae1dSRodney W. Grimes 	struct union_node *un = VTOUNION(ap->a_dvp);
1232df8bae1dSRodney W. Grimes 	int islocked = un->un_flags & UN_LOCKED;
1233df8bae1dSRodney W. Grimes 	int dolock = (vp == LOWERVP(ap->a_dvp));
1234df8bae1dSRodney W. Grimes 
1235df8bae1dSRodney W. Grimes 	if (islocked) {
1236df8bae1dSRodney W. Grimes 		if (dolock)
1237df8bae1dSRodney W. Grimes 			VOP_LOCK(vp);
1238df8bae1dSRodney W. Grimes 		else
1239df8bae1dSRodney W. Grimes 			FIXUP(VTOUNION(ap->a_dvp));
1240df8bae1dSRodney W. Grimes 	}
1241df8bae1dSRodney W. Grimes 	error = VOP_ABORTOP(vp, ap->a_cnp);
1242df8bae1dSRodney W. Grimes 	if (islocked && dolock)
1243df8bae1dSRodney W. Grimes 		VOP_UNLOCK(vp);
1244df8bae1dSRodney W. Grimes 
1245df8bae1dSRodney W. Grimes 	return (error);
1246df8bae1dSRodney W. Grimes }
1247df8bae1dSRodney W. Grimes 
1248df8bae1dSRodney W. Grimes int
1249df8bae1dSRodney W. Grimes union_inactive(ap)
1250df8bae1dSRodney W. Grimes 	struct vop_inactive_args /* {
1251df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
1252df8bae1dSRodney W. Grimes 	} */ *ap;
1253df8bae1dSRodney W. Grimes {
1254df8bae1dSRodney W. Grimes 
1255df8bae1dSRodney W. Grimes 	/*
1256df8bae1dSRodney W. Grimes 	 * Do nothing (and _don't_ bypass).
1257df8bae1dSRodney W. Grimes 	 * Wait to vrele lowervp until reclaim,
1258df8bae1dSRodney W. Grimes 	 * so that until then our union_node is in the
1259df8bae1dSRodney W. Grimes 	 * cache and reusable.
1260df8bae1dSRodney W. Grimes 	 *
1261df8bae1dSRodney W. Grimes 	 * NEEDSWORK: Someday, consider inactive'ing
1262df8bae1dSRodney W. Grimes 	 * the lowervp and then trying to reactivate it
1263df8bae1dSRodney W. Grimes 	 * with capabilities (v_id)
1264df8bae1dSRodney W. Grimes 	 * like they do in the name lookup cache code.
1265df8bae1dSRodney W. Grimes 	 * That's too much work for now.
1266df8bae1dSRodney W. Grimes 	 */
1267df8bae1dSRodney W. Grimes 
1268df8bae1dSRodney W. Grimes #ifdef UNION_DIAGNOSTIC
1269df8bae1dSRodney W. Grimes 	struct union_node *un = VTOUNION(ap->a_vp);
1270df8bae1dSRodney W. Grimes 
1271df8bae1dSRodney W. Grimes 	if (un->un_flags & UN_LOCKED)
1272df8bae1dSRodney W. Grimes 		panic("union: inactivating locked node");
1273df8bae1dSRodney W. Grimes #endif
1274df8bae1dSRodney W. Grimes 
1275df8bae1dSRodney W. Grimes 	return (0);
1276df8bae1dSRodney W. Grimes }
1277df8bae1dSRodney W. Grimes 
1278df8bae1dSRodney W. Grimes int
1279df8bae1dSRodney W. Grimes union_reclaim(ap)
1280df8bae1dSRodney W. Grimes 	struct vop_reclaim_args /* {
1281df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
1282df8bae1dSRodney W. Grimes 	} */ *ap;
1283df8bae1dSRodney W. Grimes {
1284df8bae1dSRodney W. Grimes 
1285df8bae1dSRodney W. Grimes 	union_freevp(ap->a_vp);
1286df8bae1dSRodney W. Grimes 
1287df8bae1dSRodney W. Grimes 	return (0);
1288df8bae1dSRodney W. Grimes }
1289df8bae1dSRodney W. Grimes 
1290df8bae1dSRodney W. Grimes int
1291df8bae1dSRodney W. Grimes union_lock(ap)
1292df8bae1dSRodney W. Grimes 	struct vop_lock_args *ap;
1293df8bae1dSRodney W. Grimes {
1294df8bae1dSRodney W. Grimes 	struct vnode *vp = ap->a_vp;
1295df8bae1dSRodney W. Grimes 	struct union_node *un;
1296df8bae1dSRodney W. Grimes 
1297df8bae1dSRodney W. Grimes start:
1298df8bae1dSRodney W. Grimes 	while (vp->v_flag & VXLOCK) {
1299df8bae1dSRodney W. Grimes 		vp->v_flag |= VXWANT;
130082478919SDavid Greenman 		(void) tsleep((caddr_t)vp, PINOD, "unnlk1", 0);
1301df8bae1dSRodney W. Grimes 	}
1302df8bae1dSRodney W. Grimes 
1303df8bae1dSRodney W. Grimes 	un = VTOUNION(vp);
1304df8bae1dSRodney W. Grimes 
1305df8bae1dSRodney W. Grimes 	if (un->un_uppervp) {
1306df8bae1dSRodney W. Grimes 		if ((un->un_flags & UN_ULOCK) == 0) {
1307df8bae1dSRodney W. Grimes 			un->un_flags |= UN_ULOCK;
1308df8bae1dSRodney W. Grimes 			VOP_LOCK(un->un_uppervp);
1309df8bae1dSRodney W. Grimes 		}
1310df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
1311df8bae1dSRodney W. Grimes 		if (un->un_flags & UN_KLOCK)
1312df8bae1dSRodney W. Grimes 			panic("union: dangling upper lock");
1313df8bae1dSRodney W. Grimes #endif
1314df8bae1dSRodney W. Grimes 	}
1315df8bae1dSRodney W. Grimes 
1316df8bae1dSRodney W. Grimes 	if (un->un_flags & UN_LOCKED) {
1317df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
1318df8bae1dSRodney W. Grimes 		if (curproc && un->un_pid == curproc->p_pid &&
1319df8bae1dSRodney W. Grimes 			    un->un_pid > -1 && curproc->p_pid > -1)
1320df8bae1dSRodney W. Grimes 			panic("union: locking against myself");
1321df8bae1dSRodney W. Grimes #endif
1322df8bae1dSRodney W. Grimes 		un->un_flags |= UN_WANT;
132382478919SDavid Greenman 		(void) tsleep((caddr_t) &un->un_flags, PINOD, "unnlk2", 0);
1324df8bae1dSRodney W. Grimes 		goto start;
1325df8bae1dSRodney W. Grimes 	}
1326df8bae1dSRodney W. Grimes 
1327df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
1328df8bae1dSRodney W. Grimes 	if (curproc)
1329df8bae1dSRodney W. Grimes 		un->un_pid = curproc->p_pid;
1330df8bae1dSRodney W. Grimes 	else
1331df8bae1dSRodney W. Grimes 		un->un_pid = -1;
1332df8bae1dSRodney W. Grimes #endif
1333df8bae1dSRodney W. Grimes 
1334df8bae1dSRodney W. Grimes 	un->un_flags |= UN_LOCKED;
1335df8bae1dSRodney W. Grimes 	return (0);
1336df8bae1dSRodney W. Grimes }
1337df8bae1dSRodney W. Grimes 
1338df8bae1dSRodney W. Grimes int
1339df8bae1dSRodney W. Grimes union_unlock(ap)
1340df8bae1dSRodney W. Grimes 	struct vop_lock_args *ap;
1341df8bae1dSRodney W. Grimes {
1342df8bae1dSRodney W. Grimes 	struct union_node *un = VTOUNION(ap->a_vp);
1343df8bae1dSRodney W. Grimes 
1344df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
1345df8bae1dSRodney W. Grimes 	if ((un->un_flags & UN_LOCKED) == 0)
1346df8bae1dSRodney W. Grimes 		panic("union: unlock unlocked node");
1347df8bae1dSRodney W. Grimes 	if (curproc && un->un_pid != curproc->p_pid &&
1348df8bae1dSRodney W. Grimes 			curproc->p_pid > -1 && un->un_pid > -1)
1349df8bae1dSRodney W. Grimes 		panic("union: unlocking other process's union node");
1350df8bae1dSRodney W. Grimes #endif
1351df8bae1dSRodney W. Grimes 
1352df8bae1dSRodney W. Grimes 	un->un_flags &= ~UN_LOCKED;
1353df8bae1dSRodney W. Grimes 
1354df8bae1dSRodney W. Grimes 	if ((un->un_flags & (UN_ULOCK|UN_KLOCK)) == UN_ULOCK)
1355df8bae1dSRodney W. Grimes 		VOP_UNLOCK(un->un_uppervp);
1356df8bae1dSRodney W. Grimes 
1357df8bae1dSRodney W. Grimes 	un->un_flags &= ~(UN_ULOCK|UN_KLOCK);
1358df8bae1dSRodney W. Grimes 
1359df8bae1dSRodney W. Grimes 	if (un->un_flags & UN_WANT) {
1360df8bae1dSRodney W. Grimes 		un->un_flags &= ~UN_WANT;
1361df8bae1dSRodney W. Grimes 		wakeup((caddr_t) &un->un_flags);
1362df8bae1dSRodney W. Grimes 	}
1363df8bae1dSRodney W. Grimes 
1364df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
1365df8bae1dSRodney W. Grimes 	un->un_pid = 0;
1366df8bae1dSRodney W. Grimes #endif
1367df8bae1dSRodney W. Grimes 
1368df8bae1dSRodney W. Grimes 	return (0);
1369df8bae1dSRodney W. Grimes }
1370df8bae1dSRodney W. Grimes 
1371df8bae1dSRodney W. Grimes int
1372df8bae1dSRodney W. Grimes union_bmap(ap)
1373df8bae1dSRodney W. Grimes 	struct vop_bmap_args /* {
1374df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
1375df8bae1dSRodney W. Grimes 		daddr_t  a_bn;
1376df8bae1dSRodney W. Grimes 		struct vnode **a_vpp;
1377df8bae1dSRodney W. Grimes 		daddr_t *a_bnp;
1378df8bae1dSRodney W. Grimes 		int *a_runp;
1379c83ebe77SJohn Dyson 		int *a_runb;
1380df8bae1dSRodney W. Grimes 	} */ *ap;
1381df8bae1dSRodney W. Grimes {
1382df8bae1dSRodney W. Grimes 	int error;
1383df8bae1dSRodney W. Grimes 	struct vnode *vp = OTHERVP(ap->a_vp);
1384df8bae1dSRodney W. Grimes 	int dolock = (vp == LOWERVP(ap->a_vp));
1385df8bae1dSRodney W. Grimes 
1386df8bae1dSRodney W. Grimes 	if (dolock)
1387df8bae1dSRodney W. Grimes 		VOP_LOCK(vp);
1388df8bae1dSRodney W. Grimes 	else
1389df8bae1dSRodney W. Grimes 		FIXUP(VTOUNION(ap->a_vp));
1390c83ebe77SJohn Dyson 	error = VOP_BMAP(vp, ap->a_bn, ap->a_vpp, ap->a_bnp, ap->a_runp, ap->a_runb);
1391df8bae1dSRodney W. Grimes 	if (dolock)
1392df8bae1dSRodney W. Grimes 		VOP_UNLOCK(vp);
1393df8bae1dSRodney W. Grimes 
1394df8bae1dSRodney W. Grimes 	return (error);
1395df8bae1dSRodney W. Grimes }
1396df8bae1dSRodney W. Grimes 
1397df8bae1dSRodney W. Grimes int
1398df8bae1dSRodney W. Grimes union_print(ap)
1399df8bae1dSRodney W. Grimes 	struct vop_print_args /* {
1400df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
1401df8bae1dSRodney W. Grimes 	} */ *ap;
1402df8bae1dSRodney W. Grimes {
1403df8bae1dSRodney W. Grimes 	struct vnode *vp = ap->a_vp;
1404df8bae1dSRodney W. Grimes 
14053a773ad0SPoul-Henning Kamp 	printf("\ttag VT_UNION, vp=%p, uppervp=%p, lowervp=%p\n",
1406df8bae1dSRodney W. Grimes 			vp, UPPERVP(vp), LOWERVP(vp));
1407df8bae1dSRodney W. Grimes 	return (0);
1408df8bae1dSRodney W. Grimes }
1409df8bae1dSRodney W. Grimes 
1410df8bae1dSRodney W. Grimes int
1411df8bae1dSRodney W. Grimes union_islocked(ap)
1412df8bae1dSRodney W. Grimes 	struct vop_islocked_args /* {
1413df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
1414df8bae1dSRodney W. Grimes 	} */ *ap;
1415df8bae1dSRodney W. Grimes {
1416df8bae1dSRodney W. Grimes 
1417df8bae1dSRodney W. Grimes 	return ((VTOUNION(ap->a_vp)->un_flags & UN_LOCKED) ? 1 : 0);
1418df8bae1dSRodney W. Grimes }
1419df8bae1dSRodney W. Grimes 
1420df8bae1dSRodney W. Grimes int
1421df8bae1dSRodney W. Grimes union_pathconf(ap)
1422df8bae1dSRodney W. Grimes 	struct vop_pathconf_args /* {
1423df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
1424df8bae1dSRodney W. Grimes 		int a_name;
1425df8bae1dSRodney W. Grimes 		int *a_retval;
1426df8bae1dSRodney W. Grimes 	} */ *ap;
1427df8bae1dSRodney W. Grimes {
1428df8bae1dSRodney W. Grimes 	int error;
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)
1433df8bae1dSRodney W. Grimes 		VOP_LOCK(vp);
1434df8bae1dSRodney W. Grimes 	else
1435df8bae1dSRodney W. Grimes 		FIXUP(VTOUNION(ap->a_vp));
1436df8bae1dSRodney W. Grimes 	error = VOP_PATHCONF(vp, ap->a_name, ap->a_retval);
1437df8bae1dSRodney W. Grimes 	if (dolock)
1438df8bae1dSRodney W. Grimes 		VOP_UNLOCK(vp);
1439df8bae1dSRodney W. Grimes 
1440df8bae1dSRodney W. Grimes 	return (error);
1441df8bae1dSRodney W. Grimes }
1442df8bae1dSRodney W. Grimes 
1443df8bae1dSRodney W. Grimes int
1444df8bae1dSRodney W. Grimes union_advlock(ap)
1445df8bae1dSRodney W. Grimes 	struct vop_advlock_args /* {
1446df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
1447df8bae1dSRodney W. Grimes 		caddr_t  a_id;
1448df8bae1dSRodney W. Grimes 		int  a_op;
1449df8bae1dSRodney W. Grimes 		struct flock *a_fl;
1450df8bae1dSRodney W. Grimes 		int  a_flags;
1451df8bae1dSRodney W. Grimes 	} */ *ap;
1452df8bae1dSRodney W. Grimes {
1453df8bae1dSRodney W. Grimes 
1454df8bae1dSRodney W. Grimes 	return (VOP_ADVLOCK(OTHERVP(ap->a_vp), ap->a_id, ap->a_op,
1455df8bae1dSRodney W. Grimes 				ap->a_fl, ap->a_flags));
1456df8bae1dSRodney W. Grimes }
1457df8bae1dSRodney W. Grimes 
1458df8bae1dSRodney W. Grimes 
1459df8bae1dSRodney W. Grimes /*
1460df8bae1dSRodney W. Grimes  * XXX - vop_strategy must be hand coded because it has no
1461df8bae1dSRodney W. Grimes  * vnode in its arguments.
1462df8bae1dSRodney W. Grimes  * This goes away with a merged VM/buffer cache.
1463df8bae1dSRodney W. Grimes  */
1464df8bae1dSRodney W. Grimes int
1465df8bae1dSRodney W. Grimes union_strategy(ap)
1466df8bae1dSRodney W. Grimes 	struct vop_strategy_args /* {
1467df8bae1dSRodney W. Grimes 		struct buf *a_bp;
1468df8bae1dSRodney W. Grimes 	} */ *ap;
1469df8bae1dSRodney W. Grimes {
1470df8bae1dSRodney W. Grimes 	struct buf *bp = ap->a_bp;
1471df8bae1dSRodney W. Grimes 	int error;
1472df8bae1dSRodney W. Grimes 	struct vnode *savedvp;
1473df8bae1dSRodney W. Grimes 
1474df8bae1dSRodney W. Grimes 	savedvp = bp->b_vp;
1475df8bae1dSRodney W. Grimes 	bp->b_vp = OTHERVP(bp->b_vp);
1476df8bae1dSRodney W. Grimes 
1477df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
1478df8bae1dSRodney W. Grimes 	if (bp->b_vp == NULLVP)
1479df8bae1dSRodney W. Grimes 		panic("union_strategy: nil vp");
1480df8bae1dSRodney W. Grimes 	if (((bp->b_flags & B_READ) == 0) &&
1481df8bae1dSRodney W. Grimes 	    (bp->b_vp == LOWERVP(savedvp)))
1482df8bae1dSRodney W. Grimes 		panic("union_strategy: writing to lowervp");
1483df8bae1dSRodney W. Grimes #endif
1484df8bae1dSRodney W. Grimes 
1485df8bae1dSRodney W. Grimes 	error = VOP_STRATEGY(bp);
1486df8bae1dSRodney W. Grimes 	bp->b_vp = savedvp;
1487df8bae1dSRodney W. Grimes 
1488df8bae1dSRodney W. Grimes 	return (error);
1489df8bae1dSRodney W. Grimes }
1490df8bae1dSRodney W. Grimes 
1491df8bae1dSRodney W. Grimes /*
1492df8bae1dSRodney W. Grimes  * Global vfs data structures
1493df8bae1dSRodney W. Grimes  */
1494f57e6547SBruce Evans vop_t **union_vnodeop_p;
1495df8bae1dSRodney W. Grimes struct vnodeopv_entry_desc union_vnodeop_entries[] = {
1496f57e6547SBruce Evans 	{ &vop_default_desc, (vop_t *)vn_default_error },
1497f57e6547SBruce Evans 	{ &vop_lookup_desc, (vop_t *)union_lookup },		/* lookup */
1498f57e6547SBruce Evans 	{ &vop_create_desc, (vop_t *)union_create },		/* create */
1499f57e6547SBruce Evans 	{ &vop_mknod_desc, (vop_t *)union_mknod },		/* mknod */
1500f57e6547SBruce Evans 	{ &vop_open_desc, (vop_t *)union_open },		/* open */
1501f57e6547SBruce Evans 	{ &vop_close_desc, (vop_t *)union_close },		/* close */
1502f57e6547SBruce Evans 	{ &vop_access_desc, (vop_t *)union_access },		/* access */
1503f57e6547SBruce Evans 	{ &vop_getattr_desc, (vop_t *)union_getattr },		/* getattr */
1504f57e6547SBruce Evans 	{ &vop_setattr_desc, (vop_t *)union_setattr },		/* setattr */
1505f57e6547SBruce Evans 	{ &vop_read_desc, (vop_t *)union_read },		/* read */
1506f57e6547SBruce Evans 	{ &vop_write_desc, (vop_t *)union_write },		/* write */
1507f57e6547SBruce Evans 	{ &vop_ioctl_desc, (vop_t *)union_ioctl },		/* ioctl */
1508f57e6547SBruce Evans 	{ &vop_select_desc, (vop_t *)union_select },		/* select */
1509f57e6547SBruce Evans 	{ &vop_mmap_desc, (vop_t *)union_mmap },		/* mmap */
1510f57e6547SBruce Evans 	{ &vop_fsync_desc, (vop_t *)union_fsync },		/* fsync */
1511f57e6547SBruce Evans 	{ &vop_seek_desc, (vop_t *)union_seek },		/* seek */
1512f57e6547SBruce Evans 	{ &vop_remove_desc, (vop_t *)union_remove },		/* remove */
1513f57e6547SBruce Evans 	{ &vop_link_desc, (vop_t *)union_link },		/* link */
1514f57e6547SBruce Evans 	{ &vop_rename_desc, (vop_t *)union_rename },		/* rename */
1515f57e6547SBruce Evans 	{ &vop_mkdir_desc, (vop_t *)union_mkdir },		/* mkdir */
1516f57e6547SBruce Evans 	{ &vop_rmdir_desc, (vop_t *)union_rmdir },		/* rmdir */
1517f57e6547SBruce Evans 	{ &vop_symlink_desc, (vop_t *)union_symlink },		/* symlink */
1518f57e6547SBruce Evans 	{ &vop_readdir_desc, (vop_t *)union_readdir },		/* readdir */
1519f57e6547SBruce Evans 	{ &vop_readlink_desc, (vop_t *)union_readlink },	/* readlink */
1520f57e6547SBruce Evans 	{ &vop_abortop_desc, (vop_t *)union_abortop },		/* abortop */
1521f57e6547SBruce Evans 	{ &vop_inactive_desc, (vop_t *)union_inactive },	/* inactive */
1522f57e6547SBruce Evans 	{ &vop_reclaim_desc, (vop_t *)union_reclaim },		/* reclaim */
1523f57e6547SBruce Evans 	{ &vop_lock_desc, (vop_t *)union_lock },		/* lock */
1524f57e6547SBruce Evans 	{ &vop_unlock_desc, (vop_t *)union_unlock },		/* unlock */
1525f57e6547SBruce Evans 	{ &vop_bmap_desc, (vop_t *)union_bmap },		/* bmap */
1526f57e6547SBruce Evans 	{ &vop_strategy_desc, (vop_t *)union_strategy },	/* strategy */
1527f57e6547SBruce Evans 	{ &vop_print_desc, (vop_t *)union_print },		/* print */
1528f57e6547SBruce Evans 	{ &vop_islocked_desc, (vop_t *)union_islocked },	/* islocked */
1529f57e6547SBruce Evans 	{ &vop_pathconf_desc, (vop_t *)union_pathconf },	/* pathconf */
1530f57e6547SBruce Evans 	{ &vop_advlock_desc, (vop_t *)union_advlock },		/* advlock */
1531df8bae1dSRodney W. Grimes #ifdef notdef
1532f57e6547SBruce Evans 	{ &vop_blkatoff_desc, (vop_t *)union_blkatoff },	/* blkatoff */
1533f57e6547SBruce Evans 	{ &vop_valloc_desc, (vop_t *)union_valloc },		/* valloc */
1534f57e6547SBruce Evans 	{ &vop_vfree_desc, (vop_t *)union_vfree },		/* vfree */
1535f57e6547SBruce Evans 	{ &vop_truncate_desc, (vop_t *)union_truncate },	/* truncate */
1536f57e6547SBruce Evans 	{ &vop_update_desc, (vop_t *)union_update },		/* update */
1537f57e6547SBruce Evans 	{ &vop_bwrite_desc, (vop_t *)union_bwrite },		/* bwrite */
1538df8bae1dSRodney W. Grimes #endif
1539f57e6547SBruce Evans 	{ NULL, NULL }
1540df8bae1dSRodney W. Grimes };
1541df8bae1dSRodney W. Grimes struct vnodeopv_desc union_vnodeop_opv_desc =
1542df8bae1dSRodney W. Grimes 	{ &union_vnodeop_p, union_vnodeop_entries };
1543c901836cSGarrett Wollman 
1544c901836cSGarrett Wollman VNODEOP_SET(union_vnodeop_opv_desc);
1545