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