xref: /freebsd/sys/fs/unionfs/union_subr.c (revision ef9e85abba5ce45c8b3fc840db320edb1abe0160)
1df8bae1dSRodney W. Grimes /*
2df8bae1dSRodney W. Grimes  * Copyright (c) 1994 Jan-Simon Pendry
3df8bae1dSRodney W. Grimes  * Copyright (c) 1994
4df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
5df8bae1dSRodney W. Grimes  *
6df8bae1dSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
7df8bae1dSRodney W. Grimes  * Jan-Simon Pendry.
8df8bae1dSRodney W. Grimes  *
9df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
10df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
11df8bae1dSRodney W. Grimes  * are met:
12df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
13df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
14df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
15df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
16df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
17df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
18df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
19df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
20df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
21df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
22df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
23df8bae1dSRodney W. Grimes  *    without specific prior written permission.
24df8bae1dSRodney W. Grimes  *
25df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
36df8bae1dSRodney W. Grimes  *
37996c772fSJohn Dyson  *	@(#)union_subr.c	8.20 (Berkeley) 5/20/95
38c3aac50fSPeter Wemm  * $FreeBSD$
39df8bae1dSRodney W. Grimes  */
40df8bae1dSRodney W. Grimes 
41df8bae1dSRodney W. Grimes #include <sys/param.h>
42df8bae1dSRodney W. Grimes #include <sys/systm.h>
438c14bf40SPeter Wemm #include <sys/kernel.h>
44df8bae1dSRodney W. Grimes #include <sys/vnode.h>
45df8bae1dSRodney W. Grimes #include <sys/namei.h>
46df8bae1dSRodney W. Grimes #include <sys/malloc.h>
473ac4d1efSBruce Evans #include <sys/fcntl.h>
488c14bf40SPeter Wemm #include <sys/file.h>
49df8bae1dSRodney W. Grimes #include <sys/filedesc.h>
508c14bf40SPeter Wemm #include <sys/module.h>
51996c772fSJohn Dyson #include <sys/mount.h>
52996c772fSJohn Dyson #include <sys/stat.h>
53724ab195SMike Pritchard #include <vm/vm.h>
54724ab195SMike Pritchard #include <vm/vm_extern.h>	/* for vnode_pager_setsize */
55675ea6f0SBruce Evans #include <vm/vm_zone.h>
562a31267eSMatthew Dillon #include <vm/vm_object.h>	/* for vm cache coherency */
57df8bae1dSRodney W. Grimes #include <miscfs/union/union.h>
58df8bae1dSRodney W. Grimes 
59df8bae1dSRodney W. Grimes #include <sys/proc.h>
60df8bae1dSRodney W. Grimes 
619b5e8b3aSBruce Evans extern int	union_init __P((void));
629b5e8b3aSBruce Evans 
63df8bae1dSRodney W. Grimes /* must be power of two, otherwise change UNION_HASH() */
64df8bae1dSRodney W. Grimes #define NHASH 32
65df8bae1dSRodney W. Grimes 
66df8bae1dSRodney W. Grimes /* unsigned int ... */
67df8bae1dSRodney W. Grimes #define UNION_HASH(u, l) \
6815c73825SBruce Evans 	(((((uintptr_t) (u)) + ((uintptr_t) l)) >> 8) & (NHASH-1))
69df8bae1dSRodney W. Grimes 
70e3975643SJake Burkholder static LIST_HEAD(unhead, union_node) unhead[NHASH];
71df8bae1dSRodney W. Grimes static int unvplock[NHASH];
72df8bae1dSRodney W. Grimes 
73938958b9SBruce Evans static void	union_dircache_r __P((struct vnode *vp, struct vnode ***vppp,
74938958b9SBruce Evans 				      int *cntp));
759b5e8b3aSBruce Evans static int	union_list_lock __P((int ix));
769b5e8b3aSBruce Evans static void	union_list_unlock __P((int ix));
77938958b9SBruce Evans static int	union_relookup __P((struct union_mount *um, struct vnode *dvp,
78938958b9SBruce Evans 				    struct vnode **vpp,
79938958b9SBruce Evans 				    struct componentname *cnp,
80938958b9SBruce Evans 				    struct componentname *cn, char *path,
81938958b9SBruce Evans 				    int pathlen));
8280b301c3SPoul-Henning Kamp static void	union_updatevp __P((struct union_node *un,
839b5e8b3aSBruce Evans 				    struct vnode *uppervp,
849b5e8b3aSBruce Evans 				    struct vnode *lowervp));
8580b301c3SPoul-Henning Kamp static void union_newlower __P((struct union_node *, struct vnode *));
8680b301c3SPoul-Henning Kamp static void union_newupper __P((struct union_node *, struct vnode *));
8780b301c3SPoul-Henning Kamp static int union_copyfile __P((struct vnode *, struct vnode *,
8880b301c3SPoul-Henning Kamp 					struct ucred *, struct proc *));
8980b301c3SPoul-Henning Kamp static int union_vn_create __P((struct vnode **, struct union_node *,
9080b301c3SPoul-Henning Kamp 				struct proc *));
9180b301c3SPoul-Henning Kamp static int union_vn_close __P((struct vnode *, int, struct ucred *,
9280b301c3SPoul-Henning Kamp 				struct proc *));
939b5e8b3aSBruce Evans 
94df8bae1dSRodney W. Grimes int
95df8bae1dSRodney W. Grimes union_init()
96df8bae1dSRodney W. Grimes {
97df8bae1dSRodney W. Grimes 	int i;
98df8bae1dSRodney W. Grimes 
99df8bae1dSRodney W. Grimes 	for (i = 0; i < NHASH; i++)
100df8bae1dSRodney W. Grimes 		LIST_INIT(&unhead[i]);
101df8bae1dSRodney W. Grimes 	bzero((caddr_t)unvplock, sizeof(unvplock));
10226f9a767SRodney W. Grimes 	return (0);
103df8bae1dSRodney W. Grimes }
104df8bae1dSRodney W. Grimes 
105df8bae1dSRodney W. Grimes static int
106df8bae1dSRodney W. Grimes union_list_lock(ix)
107df8bae1dSRodney W. Grimes 	int ix;
108df8bae1dSRodney W. Grimes {
1092a31267eSMatthew Dillon 	if (unvplock[ix] & UNVP_LOCKED) {
1102a31267eSMatthew Dillon 		unvplock[ix] |= UNVP_WANT;
11182478919SDavid Greenman 		(void) tsleep((caddr_t) &unvplock[ix], PINOD, "unllck", 0);
112df8bae1dSRodney W. Grimes 		return (1);
113df8bae1dSRodney W. Grimes 	}
1142a31267eSMatthew Dillon 	unvplock[ix] |= UNVP_LOCKED;
115df8bae1dSRodney W. Grimes 	return (0);
116df8bae1dSRodney W. Grimes }
117df8bae1dSRodney W. Grimes 
118df8bae1dSRodney W. Grimes static void
119df8bae1dSRodney W. Grimes union_list_unlock(ix)
120df8bae1dSRodney W. Grimes 	int ix;
121df8bae1dSRodney W. Grimes {
1222a31267eSMatthew Dillon 	unvplock[ix] &= ~UNVP_LOCKED;
123df8bae1dSRodney W. Grimes 
1242a31267eSMatthew Dillon 	if (unvplock[ix] & UNVP_WANT) {
1252a31267eSMatthew Dillon 		unvplock[ix] &= ~UNVP_WANT;
126df8bae1dSRodney W. Grimes 		wakeup((caddr_t) &unvplock[ix]);
127df8bae1dSRodney W. Grimes 	}
128df8bae1dSRodney W. Grimes }
129df8bae1dSRodney W. Grimes 
1302a31267eSMatthew Dillon /*
1312a31267eSMatthew Dillon  *	union_updatevp:
1322a31267eSMatthew Dillon  *
1332a31267eSMatthew Dillon  *	The uppervp, if not NULL, must be referenced and not locked by us
1342a31267eSMatthew Dillon  *	The lowervp, if not NULL, must be referenced.
1352a31267eSMatthew Dillon  *
1362a31267eSMatthew Dillon  *	if uppervp and lowervp match pointers already installed, nothing
1372a31267eSMatthew Dillon  *	happens. The passed vp's (when matching) are not adjusted.  This
1382a31267eSMatthew Dillon  *	routine may only be called by union_newupper() and union_newlower().
1392a31267eSMatthew Dillon  */
1402a31267eSMatthew Dillon 
14180b301c3SPoul-Henning Kamp static void
142df8bae1dSRodney W. Grimes union_updatevp(un, uppervp, lowervp)
143df8bae1dSRodney W. Grimes 	struct union_node *un;
144df8bae1dSRodney W. Grimes 	struct vnode *uppervp;
145df8bae1dSRodney W. Grimes 	struct vnode *lowervp;
146df8bae1dSRodney W. Grimes {
147df8bae1dSRodney W. Grimes 	int ohash = UNION_HASH(un->un_uppervp, un->un_lowervp);
148df8bae1dSRodney W. Grimes 	int nhash = UNION_HASH(uppervp, lowervp);
149996c772fSJohn Dyson 	int docache = (lowervp != NULLVP || uppervp != NULLVP);
15080b301c3SPoul-Henning Kamp 	int lhash, uhash;
151df8bae1dSRodney W. Grimes 
152df8bae1dSRodney W. Grimes 	/*
153df8bae1dSRodney W. Grimes 	 * Ensure locking is ordered from lower to higher
154df8bae1dSRodney W. Grimes 	 * to avoid deadlocks.
155df8bae1dSRodney W. Grimes 	 */
156df8bae1dSRodney W. Grimes 	if (nhash < ohash) {
157996c772fSJohn Dyson 		lhash = nhash;
158996c772fSJohn Dyson 		uhash = ohash;
159df8bae1dSRodney W. Grimes 	} else {
160996c772fSJohn Dyson 		lhash = ohash;
161996c772fSJohn Dyson 		uhash = nhash;
162df8bae1dSRodney W. Grimes 	}
163df8bae1dSRodney W. Grimes 
1642a31267eSMatthew Dillon 	if (lhash != uhash) {
165996c772fSJohn Dyson 		while (union_list_lock(lhash))
166996c772fSJohn Dyson 			continue;
1672a31267eSMatthew Dillon 	}
168996c772fSJohn Dyson 
169996c772fSJohn Dyson 	while (union_list_lock(uhash))
170996c772fSJohn Dyson 		continue;
171996c772fSJohn Dyson 
172996c772fSJohn Dyson 	if (ohash != nhash || !docache) {
173996c772fSJohn Dyson 		if (un->un_flags & UN_CACHED) {
174996c772fSJohn Dyson 			un->un_flags &= ~UN_CACHED;
175996c772fSJohn Dyson 			LIST_REMOVE(un, un_cache);
176996c772fSJohn Dyson 		}
177996c772fSJohn Dyson 	}
178996c772fSJohn Dyson 
179996c772fSJohn Dyson 	if (ohash != nhash)
180996c772fSJohn Dyson 		union_list_unlock(ohash);
181996c772fSJohn Dyson 
182df8bae1dSRodney W. Grimes 	if (un->un_lowervp != lowervp) {
183df8bae1dSRodney W. Grimes 		if (un->un_lowervp) {
184df8bae1dSRodney W. Grimes 			vrele(un->un_lowervp);
185df8bae1dSRodney W. Grimes 			if (un->un_path) {
186df8bae1dSRodney W. Grimes 				free(un->un_path, M_TEMP);
187df8bae1dSRodney W. Grimes 				un->un_path = 0;
188df8bae1dSRodney W. Grimes 			}
189df8bae1dSRodney W. Grimes 		}
190df8bae1dSRodney W. Grimes 		un->un_lowervp = lowervp;
191996c772fSJohn Dyson 		un->un_lowersz = VNOVAL;
192df8bae1dSRodney W. Grimes 	}
193df8bae1dSRodney W. Grimes 
194df8bae1dSRodney W. Grimes 	if (un->un_uppervp != uppervp) {
195df8bae1dSRodney W. Grimes 		if (un->un_uppervp)
196df8bae1dSRodney W. Grimes 			vrele(un->un_uppervp);
197df8bae1dSRodney W. Grimes 		un->un_uppervp = uppervp;
198996c772fSJohn Dyson 		un->un_uppersz = VNOVAL;
199df8bae1dSRodney W. Grimes 	}
200df8bae1dSRodney W. Grimes 
201996c772fSJohn Dyson 	if (docache && (ohash != nhash)) {
202df8bae1dSRodney W. Grimes 		LIST_INSERT_HEAD(&unhead[nhash], un, un_cache);
203996c772fSJohn Dyson 		un->un_flags |= UN_CACHED;
204996c772fSJohn Dyson 	}
205df8bae1dSRodney W. Grimes 
206df8bae1dSRodney W. Grimes 	union_list_unlock(nhash);
207df8bae1dSRodney W. Grimes }
208df8bae1dSRodney W. Grimes 
2092a31267eSMatthew Dillon /*
2102a31267eSMatthew Dillon  * Set a new lowervp.  The passed lowervp must be referenced and will be
2112a31267eSMatthew Dillon  * stored in the vp in a referenced state.
2122a31267eSMatthew Dillon  */
2132a31267eSMatthew Dillon 
21480b301c3SPoul-Henning Kamp static void
215df8bae1dSRodney W. Grimes union_newlower(un, lowervp)
216df8bae1dSRodney W. Grimes 	struct union_node *un;
217df8bae1dSRodney W. Grimes 	struct vnode *lowervp;
218df8bae1dSRodney W. Grimes {
219df8bae1dSRodney W. Grimes 	union_updatevp(un, un->un_uppervp, lowervp);
220df8bae1dSRodney W. Grimes }
221df8bae1dSRodney W. Grimes 
2222a31267eSMatthew Dillon /*
2232a31267eSMatthew Dillon  * Set a new uppervp.  The passed uppervp must be locked and will be
2242a31267eSMatthew Dillon  * stored in the vp in a locked state.  The caller should not unlock
2252a31267eSMatthew Dillon  * uppervp.
2262a31267eSMatthew Dillon  */
2272a31267eSMatthew Dillon 
22880b301c3SPoul-Henning Kamp static void
229df8bae1dSRodney W. Grimes union_newupper(un, uppervp)
230df8bae1dSRodney W. Grimes 	struct union_node *un;
231df8bae1dSRodney W. Grimes 	struct vnode *uppervp;
232df8bae1dSRodney W. Grimes {
233df8bae1dSRodney W. Grimes 	union_updatevp(un, uppervp, un->un_lowervp);
234df8bae1dSRodney W. Grimes }
235df8bae1dSRodney W. Grimes 
236df8bae1dSRodney W. Grimes /*
237996c772fSJohn Dyson  * Keep track of size changes in the underlying vnodes.
238996c772fSJohn Dyson  * If the size changes, then callback to the vm layer
239996c772fSJohn Dyson  * giving priority to the upper layer size.
240996c772fSJohn Dyson  */
241996c772fSJohn Dyson void
242996c772fSJohn Dyson union_newsize(vp, uppersz, lowersz)
243996c772fSJohn Dyson 	struct vnode *vp;
244996c772fSJohn Dyson 	off_t uppersz, lowersz;
245996c772fSJohn Dyson {
246996c772fSJohn Dyson 	struct union_node *un;
247996c772fSJohn Dyson 	off_t sz;
248996c772fSJohn Dyson 
249996c772fSJohn Dyson 	/* only interested in regular files */
250996c772fSJohn Dyson 	if (vp->v_type != VREG)
251996c772fSJohn Dyson 		return;
252996c772fSJohn Dyson 
253996c772fSJohn Dyson 	un = VTOUNION(vp);
254996c772fSJohn Dyson 	sz = VNOVAL;
255996c772fSJohn Dyson 
256996c772fSJohn Dyson 	if ((uppersz != VNOVAL) && (un->un_uppersz != uppersz)) {
257996c772fSJohn Dyson 		un->un_uppersz = uppersz;
258996c772fSJohn Dyson 		if (sz == VNOVAL)
259996c772fSJohn Dyson 			sz = un->un_uppersz;
260996c772fSJohn Dyson 	}
261996c772fSJohn Dyson 
262996c772fSJohn Dyson 	if ((lowersz != VNOVAL) && (un->un_lowersz != lowersz)) {
263996c772fSJohn Dyson 		un->un_lowersz = lowersz;
264996c772fSJohn Dyson 		if (sz == VNOVAL)
265996c772fSJohn Dyson 			sz = un->un_lowersz;
266996c772fSJohn Dyson 	}
267996c772fSJohn Dyson 
268996c772fSJohn Dyson 	if (sz != VNOVAL) {
2692a31267eSMatthew Dillon 		UDEBUG(("union: %s size now %ld\n",
2702a31267eSMatthew Dillon 			(uppersz != VNOVAL ? "upper" : "lower"), (long)sz));
271996c772fSJohn Dyson 		vnode_pager_setsize(vp, sz);
272996c772fSJohn Dyson 	}
273996c772fSJohn Dyson }
274996c772fSJohn Dyson 
275996c772fSJohn Dyson /*
2762a31267eSMatthew Dillon  *	union_allocvp:	allocate a union_node and associate it with a
2772a31267eSMatthew Dillon  *			parent union_node and one or two vnodes.
2782a31267eSMatthew Dillon  *
2792a31267eSMatthew Dillon  *	vpp	Holds the returned vnode locked and referenced if no
2802a31267eSMatthew Dillon  *		error occurs.
2812a31267eSMatthew Dillon  *
2822a31267eSMatthew Dillon  *	mp	Holds the mount point.  mp may or may not be busied.
2832a31267eSMatthew Dillon  *		allocvp makes no changes to mp.
2842a31267eSMatthew Dillon  *
2852a31267eSMatthew Dillon  *	dvp	Holds the parent union_node to the one we wish to create.
2862a31267eSMatthew Dillon  *		XXX may only be used to traverse an uncopied lowervp-based
2872a31267eSMatthew Dillon  *		tree?  XXX
2882a31267eSMatthew Dillon  *
2892a31267eSMatthew Dillon  *		dvp may or may not be locked.  allocvp makes no changes
2902a31267eSMatthew Dillon  *		to dvp.
2912a31267eSMatthew Dillon  *
2922a31267eSMatthew Dillon  *	upperdvp Holds the parent vnode to uppervp, generally used along
2932a31267eSMatthew Dillon  *		with path component information to create a shadow of
2942a31267eSMatthew Dillon  *		lowervp when uppervp does not exist.
2952a31267eSMatthew Dillon  *
2962a31267eSMatthew Dillon  *		upperdvp is referenced but unlocked on entry, and will be
2972a31267eSMatthew Dillon  *		dereferenced on return.
2982a31267eSMatthew Dillon  *
2992a31267eSMatthew Dillon  *	uppervp	Holds the new uppervp vnode to be stored in the
3002a31267eSMatthew Dillon  *		union_node we are allocating.  uppervp is referenced but
3012a31267eSMatthew Dillon  *		not locked, and will be dereferenced on return.
3022a31267eSMatthew Dillon  *
3032a31267eSMatthew Dillon  *	lowervp	Holds the new lowervp vnode to be stored in the
3042a31267eSMatthew Dillon  *		union_node we are allocating.  uppervp is referenced but
3052a31267eSMatthew Dillon  *		not locked, and will be dereferenced on return.
3062a31267eSMatthew Dillon  *
3072a31267eSMatthew Dillon  *	cnp	Holds path component information to be coupled with
3082a31267eSMatthew Dillon  *		lowervp and upperdvp to allow unionfs to create an uppervp
3092a31267eSMatthew Dillon  *		later on.  Only used if lowervp is valid.  The conents
3102a31267eSMatthew Dillon  *		of cnp is only valid for the duration of the call.
3112a31267eSMatthew Dillon  *
3122a31267eSMatthew Dillon  *	docache	Determine whether this node should be entered in the
3132a31267eSMatthew Dillon  *		cache or whether it should be destroyed as soon as possible.
314df8bae1dSRodney W. Grimes  *
315df8bae1dSRodney W. Grimes  * all union_nodes are maintained on a singly-linked
316df8bae1dSRodney W. Grimes  * list.  new nodes are only allocated when they cannot
317df8bae1dSRodney W. Grimes  * be found on this list.  entries on the list are
318df8bae1dSRodney W. Grimes  * removed when the vfs reclaim entry is called.
319df8bae1dSRodney W. Grimes  *
320df8bae1dSRodney W. Grimes  * a single lock is kept for the entire list.  this is
321df8bae1dSRodney W. Grimes  * needed because the getnewvnode() function can block
322df8bae1dSRodney W. Grimes  * waiting for a vnode to become free, in which case there
323df8bae1dSRodney W. Grimes  * may be more than one process trying to get the same
324df8bae1dSRodney W. Grimes  * vnode.  this lock is only taken if we are going to
325df8bae1dSRodney W. Grimes  * call getnewvnode, since the kernel itself is single-threaded.
326df8bae1dSRodney W. Grimes  *
327df8bae1dSRodney W. Grimes  * if an entry is found on the list, then call vget() to
328df8bae1dSRodney W. Grimes  * take a reference.  this is done because there may be
329df8bae1dSRodney W. Grimes  * zero references to it and so it needs to removed from
330df8bae1dSRodney W. Grimes  * the vnode free list.
331df8bae1dSRodney W. Grimes  */
3322a31267eSMatthew Dillon 
333df8bae1dSRodney W. Grimes int
3342a31267eSMatthew Dillon union_allocvp(vpp, mp, dvp, upperdvp, cnp, uppervp, lowervp, docache)
335df8bae1dSRodney W. Grimes 	struct vnode **vpp;
336df8bae1dSRodney W. Grimes 	struct mount *mp;
3372a31267eSMatthew Dillon 	struct vnode *dvp;		/* parent union vnode */
3382a31267eSMatthew Dillon 	struct vnode *upperdvp;		/* parent vnode of uppervp */
339df8bae1dSRodney W. Grimes 	struct componentname *cnp;	/* may be null */
340df8bae1dSRodney W. Grimes 	struct vnode *uppervp;		/* may be null */
341df8bae1dSRodney W. Grimes 	struct vnode *lowervp;		/* may be null */
342996c772fSJohn Dyson 	int docache;
343df8bae1dSRodney W. Grimes {
344df8bae1dSRodney W. Grimes 	int error;
34526f9a767SRodney W. Grimes 	struct union_node *un = 0;
346df8bae1dSRodney W. Grimes 	struct vnode *xlowervp = NULLVP;
347996c772fSJohn Dyson 	struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
3482a31267eSMatthew Dillon 	struct proc *p = (cnp) ? cnp->cn_proc : curproc;
34927ed09c2SMatthew Dillon 	int hash = 0;
350996c772fSJohn Dyson 	int vflag;
351df8bae1dSRodney W. Grimes 	int try;
352df8bae1dSRodney W. Grimes 
353df8bae1dSRodney W. Grimes 	if (uppervp == NULLVP && lowervp == NULLVP)
354df8bae1dSRodney W. Grimes 		panic("union: unidentifiable allocation");
355df8bae1dSRodney W. Grimes 
356df8bae1dSRodney W. Grimes 	if (uppervp && lowervp && (uppervp->v_type != lowervp->v_type)) {
357df8bae1dSRodney W. Grimes 		xlowervp = lowervp;
358df8bae1dSRodney W. Grimes 		lowervp = NULLVP;
359df8bae1dSRodney W. Grimes 	}
360df8bae1dSRodney W. Grimes 
361996c772fSJohn Dyson 	/* detect the root vnode (and aliases) */
362996c772fSJohn Dyson 	vflag = 0;
363996c772fSJohn Dyson 	if ((uppervp == um->um_uppervp) &&
364996c772fSJohn Dyson 	    ((lowervp == NULLVP) || lowervp == um->um_lowervp)) {
365996c772fSJohn Dyson 		if (lowervp == NULLVP) {
366996c772fSJohn Dyson 			lowervp = um->um_lowervp;
367996c772fSJohn Dyson 			if (lowervp != NULLVP)
368996c772fSJohn Dyson 				VREF(lowervp);
369996c772fSJohn Dyson 		}
370996c772fSJohn Dyson 		vflag = VROOT;
371996c772fSJohn Dyson 	}
372996c772fSJohn Dyson 
373df8bae1dSRodney W. Grimes loop:
374996c772fSJohn Dyson 	if (!docache) {
375996c772fSJohn Dyson 		un = 0;
376996c772fSJohn Dyson 	} else for (try = 0; try < 3; try++) {
377df8bae1dSRodney W. Grimes 		switch (try) {
378df8bae1dSRodney W. Grimes 		case 0:
379df8bae1dSRodney W. Grimes 			if (lowervp == NULLVP)
380df8bae1dSRodney W. Grimes 				continue;
381df8bae1dSRodney W. Grimes 			hash = UNION_HASH(uppervp, lowervp);
382df8bae1dSRodney W. Grimes 			break;
383df8bae1dSRodney W. Grimes 
384df8bae1dSRodney W. Grimes 		case 1:
385df8bae1dSRodney W. Grimes 			if (uppervp == NULLVP)
386df8bae1dSRodney W. Grimes 				continue;
387df8bae1dSRodney W. Grimes 			hash = UNION_HASH(uppervp, NULLVP);
388df8bae1dSRodney W. Grimes 			break;
389df8bae1dSRodney W. Grimes 
390df8bae1dSRodney W. Grimes 		case 2:
391df8bae1dSRodney W. Grimes 			if (lowervp == NULLVP)
392df8bae1dSRodney W. Grimes 				continue;
393df8bae1dSRodney W. Grimes 			hash = UNION_HASH(NULLVP, lowervp);
394df8bae1dSRodney W. Grimes 			break;
395df8bae1dSRodney W. Grimes 		}
396df8bae1dSRodney W. Grimes 
397df8bae1dSRodney W. Grimes 		while (union_list_lock(hash))
398df8bae1dSRodney W. Grimes 			continue;
399df8bae1dSRodney W. Grimes 
400ef9e85abSPoul-Henning Kamp 		LIST_FOREACH(un, &unhead[hash], un_cache) {
401df8bae1dSRodney W. Grimes 			if ((un->un_lowervp == lowervp ||
402df8bae1dSRodney W. Grimes 			     un->un_lowervp == NULLVP) &&
403df8bae1dSRodney W. Grimes 			    (un->un_uppervp == uppervp ||
404df8bae1dSRodney W. Grimes 			     un->un_uppervp == NULLVP) &&
405df8bae1dSRodney W. Grimes 			    (UNIONTOV(un)->v_mount == mp)) {
406996c772fSJohn Dyson 				if (vget(UNIONTOV(un), 0,
407996c772fSJohn Dyson 				    cnp ? cnp->cn_proc : NULL)) {
408df8bae1dSRodney W. Grimes 					union_list_unlock(hash);
409df8bae1dSRodney W. Grimes 					goto loop;
410df8bae1dSRodney W. Grimes 				}
411df8bae1dSRodney W. Grimes 				break;
412df8bae1dSRodney W. Grimes 			}
413df8bae1dSRodney W. Grimes 		}
414df8bae1dSRodney W. Grimes 
415df8bae1dSRodney W. Grimes 		union_list_unlock(hash);
416df8bae1dSRodney W. Grimes 
417df8bae1dSRodney W. Grimes 		if (un)
418df8bae1dSRodney W. Grimes 			break;
419df8bae1dSRodney W. Grimes 	}
420df8bae1dSRodney W. Grimes 
421df8bae1dSRodney W. Grimes 	if (un) {
422df8bae1dSRodney W. Grimes 		/*
4232a31267eSMatthew Dillon 		 * Obtain a lock on the union_node.  Everything is unlocked
4242a31267eSMatthew Dillon 		 * except for dvp, so check that case.  If they match, our
4252a31267eSMatthew Dillon 		 * new un is already locked.  Otherwise we have to lock our
4262a31267eSMatthew Dillon 		 * new un.
4272a31267eSMatthew Dillon 		 *
4282a31267eSMatthew Dillon 		 * A potential deadlock situation occurs when we are holding
4292a31267eSMatthew Dillon 		 * one lock while trying to get another.  We must follow
4302a31267eSMatthew Dillon 		 * strict ordering rules to avoid it.  We try to locate dvp
4312a31267eSMatthew Dillon 		 * by scanning up from un_vnode, since the most likely
4322a31267eSMatthew Dillon 		 * scenario is un being under dvp.
433df8bae1dSRodney W. Grimes 		 */
434df8bae1dSRodney W. Grimes 
4352a31267eSMatthew Dillon 		if (dvp && un->un_vnode != dvp) {
4362a31267eSMatthew Dillon 			struct vnode *scan = un->un_vnode;
4372a31267eSMatthew Dillon 
4382a31267eSMatthew Dillon 			do {
4392a31267eSMatthew Dillon 				scan = VTOUNION(scan)->un_pvp;
4402a31267eSMatthew Dillon 			} while (scan && scan->v_tag == VT_UNION && scan != dvp);
4412a31267eSMatthew Dillon 			if (scan != dvp) {
442df8bae1dSRodney W. Grimes 				/*
4432a31267eSMatthew Dillon 				 * our new un is above dvp (we never saw dvp
4442a31267eSMatthew Dillon 				 * while moving up the tree).
445df8bae1dSRodney W. Grimes 				 */
4462a31267eSMatthew Dillon 				VREF(dvp);
4472a31267eSMatthew Dillon 				VOP_UNLOCK(dvp, 0, p);
4482a31267eSMatthew Dillon 				error = vn_lock(un->un_vnode, LK_EXCLUSIVE, p);
4492a31267eSMatthew Dillon 				vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, p);
4502a31267eSMatthew Dillon 				vrele(dvp);
451df8bae1dSRodney W. Grimes 			} else {
4522a31267eSMatthew Dillon 				/*
4532a31267eSMatthew Dillon 				 * our new un is under dvp
4542a31267eSMatthew Dillon 				 */
4552a31267eSMatthew Dillon 				error = vn_lock(un->un_vnode, LK_EXCLUSIVE, p);
4562a31267eSMatthew Dillon 			}
4572a31267eSMatthew Dillon 		} else if (dvp == NULLVP) {
4582a31267eSMatthew Dillon 			/*
4592a31267eSMatthew Dillon 			 * dvp is NULL, we need to lock un.
4602a31267eSMatthew Dillon 			 */
4612a31267eSMatthew Dillon 			error = vn_lock(un->un_vnode, LK_EXCLUSIVE, p);
4622a31267eSMatthew Dillon 		} else {
4632a31267eSMatthew Dillon 			/*
4642a31267eSMatthew Dillon 			 * dvp == un->un_vnode, we are already locked.
4652a31267eSMatthew Dillon 			 */
4662a31267eSMatthew Dillon 			error = 0;
4672a31267eSMatthew Dillon 		}
4682a31267eSMatthew Dillon 
4692a31267eSMatthew Dillon 		if (error)
470df8bae1dSRodney W. Grimes 			goto loop;
471df8bae1dSRodney W. Grimes 
472df8bae1dSRodney W. Grimes 		/*
4732a31267eSMatthew Dillon 		 * At this point, the union_node is locked and referenced.
4742a31267eSMatthew Dillon 		 *
4752a31267eSMatthew Dillon 		 * uppervp is locked and referenced or NULL, lowervp is
4762a31267eSMatthew Dillon 		 * referenced or NULL.
477df8bae1dSRodney W. Grimes 		 */
4782a31267eSMatthew Dillon 		UDEBUG(("Modify existing un %p vn %p upper %p(refs %d) -> %p(refs %d)\n",
4792a31267eSMatthew Dillon 			un, un->un_vnode, un->un_uppervp,
4802a31267eSMatthew Dillon 			(un->un_uppervp ? un->un_uppervp->v_usecount : -99),
4812a31267eSMatthew Dillon 			uppervp,
4822a31267eSMatthew Dillon 			(uppervp ? uppervp->v_usecount : -99)
4832a31267eSMatthew Dillon 		));
484df8bae1dSRodney W. Grimes 
485df8bae1dSRodney W. Grimes 		if (uppervp != un->un_uppervp) {
4862a31267eSMatthew Dillon 			KASSERT(uppervp == NULL || uppervp->v_usecount > 0, ("union_allocvp: too few refs %d (at least 1 required) on uppervp", uppervp->v_usecount));
487df8bae1dSRodney W. Grimes 			union_newupper(un, uppervp);
488df8bae1dSRodney W. Grimes 		} else if (uppervp) {
4892a31267eSMatthew Dillon 			KASSERT(uppervp->v_usecount > 1, ("union_allocvp: too few refs %d (at least 2 required) on uppervp", uppervp->v_usecount));
490df8bae1dSRodney W. Grimes 			vrele(uppervp);
491df8bae1dSRodney W. Grimes 		}
492df8bae1dSRodney W. Grimes 
493df8bae1dSRodney W. Grimes 		/*
494df8bae1dSRodney W. Grimes 		 * Save information about the lower layer.
495df8bae1dSRodney W. Grimes 		 * This needs to keep track of pathname
496df8bae1dSRodney W. Grimes 		 * and directory information which union_vn_create
497df8bae1dSRodney W. Grimes 		 * might need.
498df8bae1dSRodney W. Grimes 		 */
499df8bae1dSRodney W. Grimes 		if (lowervp != un->un_lowervp) {
500df8bae1dSRodney W. Grimes 			union_newlower(un, lowervp);
501996c772fSJohn Dyson 			if (cnp && (lowervp != NULLVP)) {
502df8bae1dSRodney W. Grimes 				un->un_path = malloc(cnp->cn_namelen+1,
503df8bae1dSRodney W. Grimes 						M_TEMP, M_WAITOK);
504df8bae1dSRodney W. Grimes 				bcopy(cnp->cn_nameptr, un->un_path,
505df8bae1dSRodney W. Grimes 						cnp->cn_namelen);
506df8bae1dSRodney W. Grimes 				un->un_path[cnp->cn_namelen] = '\0';
507df8bae1dSRodney W. Grimes 			}
508df8bae1dSRodney W. Grimes 		} else if (lowervp) {
509df8bae1dSRodney W. Grimes 			vrele(lowervp);
510df8bae1dSRodney W. Grimes 		}
5112a31267eSMatthew Dillon 
5122a31267eSMatthew Dillon 		/*
5132a31267eSMatthew Dillon 		 * and upperdvp
5142a31267eSMatthew Dillon 		 */
5152a31267eSMatthew Dillon 		if (upperdvp != un->un_dirvp) {
5162a31267eSMatthew Dillon 			if (un->un_dirvp)
5172a31267eSMatthew Dillon 				vrele(un->un_dirvp);
5182a31267eSMatthew Dillon 			un->un_dirvp = upperdvp;
5192a31267eSMatthew Dillon 		} else if (upperdvp) {
5202a31267eSMatthew Dillon 			vrele(upperdvp);
5212a31267eSMatthew Dillon 		}
5222a31267eSMatthew Dillon 
523df8bae1dSRodney W. Grimes 		*vpp = UNIONTOV(un);
524df8bae1dSRodney W. Grimes 		return (0);
525df8bae1dSRodney W. Grimes 	}
526df8bae1dSRodney W. Grimes 
527996c772fSJohn Dyson 	if (docache) {
528df8bae1dSRodney W. Grimes 		/*
529df8bae1dSRodney W. Grimes 		 * otherwise lock the vp list while we call getnewvnode
530df8bae1dSRodney W. Grimes 		 * since that can block.
531df8bae1dSRodney W. Grimes 		 */
532df8bae1dSRodney W. Grimes 		hash = UNION_HASH(uppervp, lowervp);
533df8bae1dSRodney W. Grimes 
534df8bae1dSRodney W. Grimes 		if (union_list_lock(hash))
535df8bae1dSRodney W. Grimes 			goto loop;
536996c772fSJohn Dyson 	}
537df8bae1dSRodney W. Grimes 
5382a31267eSMatthew Dillon 	/*
5392a31267eSMatthew Dillon 	 * Create new node rather then replace old node
5402a31267eSMatthew Dillon 	 */
5412a31267eSMatthew Dillon 
542df8bae1dSRodney W. Grimes 	error = getnewvnode(VT_UNION, mp, union_vnodeop_p, vpp);
543df8bae1dSRodney W. Grimes 	if (error) {
5442a31267eSMatthew Dillon 		/*
5452a31267eSMatthew Dillon 		 * If an error occurs clear out vnodes.
5462a31267eSMatthew Dillon 		 */
547df8bae1dSRodney W. Grimes 		if (lowervp)
548df8bae1dSRodney W. Grimes 			vrele(lowervp);
5492a31267eSMatthew Dillon 		if (uppervp)
5502a31267eSMatthew Dillon 			vrele(uppervp);
5512a31267eSMatthew Dillon 		if (upperdvp)
5522a31267eSMatthew Dillon 			vrele(upperdvp);
5532a31267eSMatthew Dillon 		*vpp = NULL;
554df8bae1dSRodney W. Grimes 		goto out;
555df8bae1dSRodney W. Grimes 	}
556df8bae1dSRodney W. Grimes 
557df8bae1dSRodney W. Grimes 	MALLOC((*vpp)->v_data, void *, sizeof(struct union_node),
558df8bae1dSRodney W. Grimes 		M_TEMP, M_WAITOK);
559df8bae1dSRodney W. Grimes 
560996c772fSJohn Dyson 	(*vpp)->v_flag |= vflag;
561df8bae1dSRodney W. Grimes 	if (uppervp)
562df8bae1dSRodney W. Grimes 		(*vpp)->v_type = uppervp->v_type;
563df8bae1dSRodney W. Grimes 	else
564df8bae1dSRodney W. Grimes 		(*vpp)->v_type = lowervp->v_type;
5652a31267eSMatthew Dillon 
566df8bae1dSRodney W. Grimes 	un = VTOUNION(*vpp);
5672a31267eSMatthew Dillon 	bzero(un, sizeof(*un));
5682a31267eSMatthew Dillon 
5692a31267eSMatthew Dillon 	lockinit(&un->un_lock, PVFS, "unlock", 0, 0);
5702a31267eSMatthew Dillon 	vn_lock(*vpp, LK_EXCLUSIVE | LK_RETRY, p);
5712a31267eSMatthew Dillon 
572df8bae1dSRodney W. Grimes 	un->un_vnode = *vpp;
573df8bae1dSRodney W. Grimes 	un->un_uppervp = uppervp;
574996c772fSJohn Dyson 	un->un_uppersz = VNOVAL;
575df8bae1dSRodney W. Grimes 	un->un_lowervp = lowervp;
576996c772fSJohn Dyson 	un->un_lowersz = VNOVAL;
5772a31267eSMatthew Dillon 	un->un_dirvp = upperdvp;
5782a31267eSMatthew Dillon 	un->un_pvp = dvp;		/* only parent dir in new allocation */
5792a31267eSMatthew Dillon 	if (dvp != NULLVP)
5802a31267eSMatthew Dillon 		VREF(dvp);
581996c772fSJohn Dyson 	un->un_dircache = 0;
582df8bae1dSRodney W. Grimes 	un->un_openl = 0;
5832a31267eSMatthew Dillon 
584996c772fSJohn Dyson 	if (cnp && (lowervp != NULLVP)) {
585df8bae1dSRodney W. Grimes 		un->un_path = malloc(cnp->cn_namelen+1, M_TEMP, M_WAITOK);
586df8bae1dSRodney W. Grimes 		bcopy(cnp->cn_nameptr, un->un_path, cnp->cn_namelen);
587df8bae1dSRodney W. Grimes 		un->un_path[cnp->cn_namelen] = '\0';
588df8bae1dSRodney W. Grimes 	} else {
589df8bae1dSRodney W. Grimes 		un->un_path = 0;
5902a31267eSMatthew Dillon 		un->un_dirvp = NULL;
591df8bae1dSRodney W. Grimes 	}
592df8bae1dSRodney W. Grimes 
593996c772fSJohn Dyson 	if (docache) {
594df8bae1dSRodney W. Grimes 		LIST_INSERT_HEAD(&unhead[hash], un, un_cache);
595996c772fSJohn Dyson 		un->un_flags |= UN_CACHED;
596996c772fSJohn Dyson 	}
597df8bae1dSRodney W. Grimes 
5982a31267eSMatthew Dillon out:
599df8bae1dSRodney W. Grimes 	if (xlowervp)
600df8bae1dSRodney W. Grimes 		vrele(xlowervp);
601df8bae1dSRodney W. Grimes 
602996c772fSJohn Dyson 	if (docache)
603df8bae1dSRodney W. Grimes 		union_list_unlock(hash);
604df8bae1dSRodney W. Grimes 
605df8bae1dSRodney W. Grimes 	return (error);
606df8bae1dSRodney W. Grimes }
607df8bae1dSRodney W. Grimes 
608df8bae1dSRodney W. Grimes int
609df8bae1dSRodney W. Grimes union_freevp(vp)
610df8bae1dSRodney W. Grimes 	struct vnode *vp;
611df8bae1dSRodney W. Grimes {
612df8bae1dSRodney W. Grimes 	struct union_node *un = VTOUNION(vp);
613df8bae1dSRodney W. Grimes 
614996c772fSJohn Dyson 	if (un->un_flags & UN_CACHED) {
615996c772fSJohn Dyson 		un->un_flags &= ~UN_CACHED;
616df8bae1dSRodney W. Grimes 		LIST_REMOVE(un, un_cache);
617996c772fSJohn Dyson 	}
618df8bae1dSRodney W. Grimes 
6192a31267eSMatthew Dillon 	if (un->un_pvp != NULLVP) {
620996c772fSJohn Dyson 		vrele(un->un_pvp);
6212a31267eSMatthew Dillon 		un->un_pvp = NULL;
6222a31267eSMatthew Dillon 	}
6232a31267eSMatthew Dillon 	if (un->un_uppervp != NULLVP) {
624df8bae1dSRodney W. Grimes 		vrele(un->un_uppervp);
6252a31267eSMatthew Dillon 		un->un_uppervp = NULL;
6262a31267eSMatthew Dillon 	}
6272a31267eSMatthew Dillon 	if (un->un_lowervp != NULLVP) {
628df8bae1dSRodney W. Grimes 		vrele(un->un_lowervp);
6292a31267eSMatthew Dillon 		un->un_lowervp = NULL;
6302a31267eSMatthew Dillon 	}
6312a31267eSMatthew Dillon 	if (un->un_dirvp != NULLVP) {
632df8bae1dSRodney W. Grimes 		vrele(un->un_dirvp);
6332a31267eSMatthew Dillon 		un->un_dirvp = NULL;
6342a31267eSMatthew Dillon 	}
6352a31267eSMatthew Dillon 	if (un->un_path) {
636df8bae1dSRodney W. Grimes 		free(un->un_path, M_TEMP);
6372a31267eSMatthew Dillon 		un->un_path = NULL;
6382a31267eSMatthew Dillon 	}
639a18b1f1dSJason Evans 	lockdestroy(&un->un_lock);
640df8bae1dSRodney W. Grimes 
641df8bae1dSRodney W. Grimes 	FREE(vp->v_data, M_TEMP);
642df8bae1dSRodney W. Grimes 	vp->v_data = 0;
643df8bae1dSRodney W. Grimes 
644df8bae1dSRodney W. Grimes 	return (0);
645df8bae1dSRodney W. Grimes }
646df8bae1dSRodney W. Grimes 
647df8bae1dSRodney W. Grimes /*
648df8bae1dSRodney W. Grimes  * copyfile.  copy the vnode (fvp) to the vnode (tvp)
649df8bae1dSRodney W. Grimes  * using a sequence of reads and writes.  both (fvp)
650df8bae1dSRodney W. Grimes  * and (tvp) are locked on entry and exit.
6512a31267eSMatthew Dillon  *
6522a31267eSMatthew Dillon  * fvp and tvp are both exclusive locked on call, but their refcount's
6532a31267eSMatthew Dillon  * haven't been bumped at all.
654df8bae1dSRodney W. Grimes  */
65580b301c3SPoul-Henning Kamp static int
656996c772fSJohn Dyson union_copyfile(fvp, tvp, cred, p)
657df8bae1dSRodney W. Grimes 	struct vnode *fvp;
658df8bae1dSRodney W. Grimes 	struct vnode *tvp;
659996c772fSJohn Dyson 	struct ucred *cred;
660996c772fSJohn Dyson 	struct proc *p;
661df8bae1dSRodney W. Grimes {
662df8bae1dSRodney W. Grimes 	char *buf;
663df8bae1dSRodney W. Grimes 	struct uio uio;
664df8bae1dSRodney W. Grimes 	struct iovec iov;
665df8bae1dSRodney W. Grimes 	int error = 0;
666df8bae1dSRodney W. Grimes 
667df8bae1dSRodney W. Grimes 	/*
668df8bae1dSRodney W. Grimes 	 * strategy:
669df8bae1dSRodney W. Grimes 	 * allocate a buffer of size MAXBSIZE.
670df8bae1dSRodney W. Grimes 	 * loop doing reads and writes, keeping track
671df8bae1dSRodney W. Grimes 	 * of the current uio offset.
672df8bae1dSRodney W. Grimes 	 * give up at the first sign of trouble.
673df8bae1dSRodney W. Grimes 	 */
674df8bae1dSRodney W. Grimes 
6752a31267eSMatthew Dillon 	bzero(&uio, sizeof(uio));
6762a31267eSMatthew Dillon 
677df8bae1dSRodney W. Grimes 	uio.uio_procp = p;
678df8bae1dSRodney W. Grimes 	uio.uio_segflg = UIO_SYSSPACE;
679df8bae1dSRodney W. Grimes 	uio.uio_offset = 0;
680df8bae1dSRodney W. Grimes 
681996c772fSJohn Dyson 	VOP_LEASE(fvp, p, cred, LEASE_READ);
682996c772fSJohn Dyson 	VOP_LEASE(tvp, p, cred, LEASE_WRITE);
683df8bae1dSRodney W. Grimes 
684df8bae1dSRodney W. Grimes 	buf = malloc(MAXBSIZE, M_TEMP, M_WAITOK);
685df8bae1dSRodney W. Grimes 
686df8bae1dSRodney W. Grimes 	/* ugly loop follows... */
687df8bae1dSRodney W. Grimes 	do {
688df8bae1dSRodney W. Grimes 		off_t offset = uio.uio_offset;
6892a31267eSMatthew Dillon 		int count;
6902a31267eSMatthew Dillon 		int bufoffset;
691df8bae1dSRodney W. Grimes 
6922a31267eSMatthew Dillon 		/*
6932a31267eSMatthew Dillon 		 * Setup for big read
6942a31267eSMatthew Dillon 		 */
695df8bae1dSRodney W. Grimes 		uio.uio_iov = &iov;
696df8bae1dSRodney W. Grimes 		uio.uio_iovcnt = 1;
697df8bae1dSRodney W. Grimes 		iov.iov_base = buf;
698df8bae1dSRodney W. Grimes 		iov.iov_len = MAXBSIZE;
699df8bae1dSRodney W. Grimes 		uio.uio_resid = iov.iov_len;
700df8bae1dSRodney W. Grimes 		uio.uio_rw = UIO_READ;
701df8bae1dSRodney W. Grimes 
7022a31267eSMatthew Dillon 		if ((error = VOP_READ(fvp, &uio, 0, cred)) != 0)
7032a31267eSMatthew Dillon 			break;
7042a31267eSMatthew Dillon 
7052a31267eSMatthew Dillon 		/*
7062a31267eSMatthew Dillon 		 * Get bytes read, handle read eof case and setup for
7072a31267eSMatthew Dillon 		 * write loop
7082a31267eSMatthew Dillon 		 */
7092a31267eSMatthew Dillon 		if ((count = MAXBSIZE - uio.uio_resid) == 0)
7102a31267eSMatthew Dillon 			break;
7112a31267eSMatthew Dillon 		bufoffset = 0;
7122a31267eSMatthew Dillon 
7132a31267eSMatthew Dillon 		/*
7142a31267eSMatthew Dillon 		 * Write until an error occurs or our buffer has been
7152a31267eSMatthew Dillon 		 * exhausted, then update the offset for the next read.
7162a31267eSMatthew Dillon 		 */
7172a31267eSMatthew Dillon 		while (bufoffset < count) {
718df8bae1dSRodney W. Grimes 			uio.uio_iov = &iov;
719df8bae1dSRodney W. Grimes 			uio.uio_iovcnt = 1;
7202a31267eSMatthew Dillon 			iov.iov_base = buf + bufoffset;
7212a31267eSMatthew Dillon 			iov.iov_len = count - bufoffset;
7222a31267eSMatthew Dillon 			uio.uio_offset = offset + bufoffset;
723df8bae1dSRodney W. Grimes 			uio.uio_rw = UIO_WRITE;
724df8bae1dSRodney W. Grimes 			uio.uio_resid = iov.iov_len;
725df8bae1dSRodney W. Grimes 
7262a31267eSMatthew Dillon 			if ((error = VOP_WRITE(tvp, &uio, 0, cred)) != 0)
727df8bae1dSRodney W. Grimes 				break;
7282a31267eSMatthew Dillon 			bufoffset += (count - bufoffset) - uio.uio_resid;
729df8bae1dSRodney W. Grimes 		}
7302a31267eSMatthew Dillon 		uio.uio_offset = offset + bufoffset;
731df8bae1dSRodney W. Grimes 	} while (error == 0);
732df8bae1dSRodney W. Grimes 
733df8bae1dSRodney W. Grimes 	free(buf, M_TEMP);
734df8bae1dSRodney W. Grimes 	return (error);
735df8bae1dSRodney W. Grimes }
736df8bae1dSRodney W. Grimes 
737df8bae1dSRodney W. Grimes /*
7382a31267eSMatthew Dillon  *
7392a31267eSMatthew Dillon  * un's vnode is assumed to be locked on entry and remains locked on exit.
740996c772fSJohn Dyson  */
7412a31267eSMatthew Dillon 
742996c772fSJohn Dyson int
743996c772fSJohn Dyson union_copyup(un, docopy, cred, p)
744996c772fSJohn Dyson 	struct union_node *un;
745996c772fSJohn Dyson 	int docopy;
746996c772fSJohn Dyson 	struct ucred *cred;
747996c772fSJohn Dyson 	struct proc *p;
748996c772fSJohn Dyson {
749996c772fSJohn Dyson 	int error;
750f2a2857bSKirk McKusick 	struct mount *mp;
751996c772fSJohn Dyson 	struct vnode *lvp, *uvp;
752996c772fSJohn Dyson 
7535842d4e5SKATO Takenori 	/*
7545842d4e5SKATO Takenori 	 * If the user does not have read permission, the vnode should not
7555842d4e5SKATO Takenori 	 * be copied to upper layer.
7565842d4e5SKATO Takenori 	 */
7575842d4e5SKATO Takenori 	vn_lock(un->un_lowervp, LK_EXCLUSIVE | LK_RETRY, p);
7585842d4e5SKATO Takenori 	error = VOP_ACCESS(un->un_lowervp, VREAD, cred, p);
7595842d4e5SKATO Takenori 	VOP_UNLOCK(un->un_lowervp, 0, p);
7605842d4e5SKATO Takenori 	if (error)
7615842d4e5SKATO Takenori 		return (error);
7625842d4e5SKATO Takenori 
763f2a2857bSKirk McKusick 	if ((error = vn_start_write(un->un_dirvp, &mp, V_WAIT | PCATCH)) != 0)
764996c772fSJohn Dyson 		return (error);
765f2a2857bSKirk McKusick 	if ((error = union_vn_create(&uvp, un, p)) != 0) {
766f2a2857bSKirk McKusick 		vn_finished_write(mp);
767f2a2857bSKirk McKusick 		return (error);
768f2a2857bSKirk McKusick 	}
769996c772fSJohn Dyson 
770996c772fSJohn Dyson 	lvp = un->un_lowervp;
771996c772fSJohn Dyson 
7722a31267eSMatthew Dillon 	KASSERT(uvp->v_usecount > 0, ("copy: uvp refcount 0: %d", uvp->v_usecount));
773996c772fSJohn Dyson 	if (docopy) {
774996c772fSJohn Dyson 		/*
775996c772fSJohn Dyson 		 * XX - should not ignore errors
776996c772fSJohn Dyson 		 * from VOP_CLOSE
777996c772fSJohn Dyson 		 */
778996c772fSJohn Dyson 		vn_lock(lvp, LK_EXCLUSIVE | LK_RETRY, p);
779996c772fSJohn Dyson 		error = VOP_OPEN(lvp, FREAD, cred, p);
7802a31267eSMatthew Dillon 		if (error == 0 && vn_canvmio(lvp) == TRUE)
7812a31267eSMatthew Dillon 			error = vfs_object_create(lvp, p, cred);
782996c772fSJohn Dyson 		if (error == 0) {
783996c772fSJohn Dyson 			error = union_copyfile(lvp, uvp, cred, p);
784996c772fSJohn Dyson 			VOP_UNLOCK(lvp, 0, p);
785996c772fSJohn Dyson 			(void) VOP_CLOSE(lvp, FREAD, cred, p);
786996c772fSJohn Dyson 		}
787996c772fSJohn Dyson 		if (error == 0)
7882a31267eSMatthew Dillon 			UDEBUG(("union: copied up %s\n", un->un_path));
789996c772fSJohn Dyson 
790996c772fSJohn Dyson 	}
791996c772fSJohn Dyson 	VOP_UNLOCK(uvp, 0, p);
792f2a2857bSKirk McKusick 	vn_finished_write(mp);
7932a31267eSMatthew Dillon 	union_newupper(un, uvp);
7942a31267eSMatthew Dillon 	KASSERT(uvp->v_usecount > 0, ("copy: uvp refcount 0: %d", uvp->v_usecount));
795996c772fSJohn Dyson 	union_vn_close(uvp, FWRITE, cred, p);
7962a31267eSMatthew Dillon 	KASSERT(uvp->v_usecount > 0, ("copy: uvp refcount 0: %d", uvp->v_usecount));
797996c772fSJohn Dyson 	/*
798996c772fSJohn Dyson 	 * Subsequent IOs will go to the top layer, so
799996c772fSJohn Dyson 	 * call close on the lower vnode and open on the
800996c772fSJohn Dyson 	 * upper vnode to ensure that the filesystem keeps
801996c772fSJohn Dyson 	 * its references counts right.  This doesn't do
802996c772fSJohn Dyson 	 * the right thing with (cred) and (FREAD) though.
803996c772fSJohn Dyson 	 * Ignoring error returns is not right, either.
804996c772fSJohn Dyson 	 */
805996c772fSJohn Dyson 	if (error == 0) {
806996c772fSJohn Dyson 		int i;
807996c772fSJohn Dyson 
808996c772fSJohn Dyson 		for (i = 0; i < un->un_openl; i++) {
809996c772fSJohn Dyson 			(void) VOP_CLOSE(lvp, FREAD, cred, p);
810996c772fSJohn Dyson 			(void) VOP_OPEN(uvp, FREAD, cred, p);
811996c772fSJohn Dyson 		}
812e3a285c7SMatthew Dillon 		if (un->un_openl) {
8132a31267eSMatthew Dillon 			if (vn_canvmio(uvp) == TRUE)
8142a31267eSMatthew Dillon 				error = vfs_object_create(uvp, p, cred);
815e3a285c7SMatthew Dillon 		}
816996c772fSJohn Dyson 		un->un_openl = 0;
817996c772fSJohn Dyson 	}
818996c772fSJohn Dyson 
819996c772fSJohn Dyson 	return (error);
820996c772fSJohn Dyson 
821996c772fSJohn Dyson }
822996c772fSJohn Dyson 
8232a31267eSMatthew Dillon /*
8242a31267eSMatthew Dillon  *	union_relookup:
8252a31267eSMatthew Dillon  *
8262a31267eSMatthew Dillon  *	dvp should be locked on entry and will be locked on return.  No
8272a31267eSMatthew Dillon  *	net change in the ref count will occur.
8282a31267eSMatthew Dillon  *
8292a31267eSMatthew Dillon  *	If an error is returned, *vpp will be invalid, otherwise it
8302a31267eSMatthew Dillon  *	will hold a locked, referenced vnode.  If *vpp == dvp then
8312a31267eSMatthew Dillon  *	remember that only one exclusive lock is held.
8322a31267eSMatthew Dillon  */
8332a31267eSMatthew Dillon 
834996c772fSJohn Dyson static int
835996c772fSJohn Dyson union_relookup(um, dvp, vpp, cnp, cn, path, pathlen)
836996c772fSJohn Dyson 	struct union_mount *um;
837996c772fSJohn Dyson 	struct vnode *dvp;
838996c772fSJohn Dyson 	struct vnode **vpp;
839996c772fSJohn Dyson 	struct componentname *cnp;
840996c772fSJohn Dyson 	struct componentname *cn;
841996c772fSJohn Dyson 	char *path;
842996c772fSJohn Dyson 	int pathlen;
843996c772fSJohn Dyson {
844996c772fSJohn Dyson 	int error;
845996c772fSJohn Dyson 
846996c772fSJohn Dyson 	/*
847996c772fSJohn Dyson 	 * A new componentname structure must be faked up because
848996c772fSJohn Dyson 	 * there is no way to know where the upper level cnp came
849996c772fSJohn Dyson 	 * from or what it is being used for.  This must duplicate
850996c772fSJohn Dyson 	 * some of the work done by NDINIT, some of the work done
851996c772fSJohn Dyson 	 * by namei, some of the work done by lookup and some of
852996c772fSJohn Dyson 	 * the work done by VOP_LOOKUP when given a CREATE flag.
853996c772fSJohn Dyson 	 * Conclusion: Horrible.
854996c772fSJohn Dyson 	 */
855996c772fSJohn Dyson 	cn->cn_namelen = pathlen;
85699448ed1SJohn Dyson 	cn->cn_pnbuf = zalloc(namei_zone);
857996c772fSJohn Dyson 	bcopy(path, cn->cn_pnbuf, cn->cn_namelen);
858996c772fSJohn Dyson 	cn->cn_pnbuf[cn->cn_namelen] = '\0';
859996c772fSJohn Dyson 
860996c772fSJohn Dyson 	cn->cn_nameiop = CREATE;
8612a31267eSMatthew Dillon 	cn->cn_flags = (LOCKPARENT|LOCKLEAF|HASBUF|SAVENAME|ISLASTCN);
862996c772fSJohn Dyson 	cn->cn_proc = cnp->cn_proc;
863996c772fSJohn Dyson 	if (um->um_op == UNMNT_ABOVE)
864996c772fSJohn Dyson 		cn->cn_cred = cnp->cn_cred;
865996c772fSJohn Dyson 	else
866996c772fSJohn Dyson 		cn->cn_cred = um->um_cred;
867996c772fSJohn Dyson 	cn->cn_nameptr = cn->cn_pnbuf;
868996c772fSJohn Dyson 	cn->cn_consume = cnp->cn_consume;
869996c772fSJohn Dyson 
870996c772fSJohn Dyson 	VREF(dvp);
8712a31267eSMatthew Dillon 	VOP_UNLOCK(dvp, 0, cnp->cn_proc);
8722a31267eSMatthew Dillon 
8732a31267eSMatthew Dillon 	/*
8742a31267eSMatthew Dillon 	 * Pass dvp unlocked and referenced on call to relookup().
8752a31267eSMatthew Dillon 	 *
8762a31267eSMatthew Dillon 	 * If an error occurs, dvp will be returned unlocked and dereferenced.
8772a31267eSMatthew Dillon 	 */
8782a31267eSMatthew Dillon 
8792a31267eSMatthew Dillon 	if ((error = relookup(dvp, vpp, cn)) != 0) {
8802a31267eSMatthew Dillon 		vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, cnp->cn_proc);
8812a31267eSMatthew Dillon 		return(error);
882b422956cSPoul-Henning Kamp 	}
883996c772fSJohn Dyson 
8842a31267eSMatthew Dillon 	/*
8852a31267eSMatthew Dillon 	 * If no error occurs, dvp will be returned locked with the reference
8862a31267eSMatthew Dillon 	 * left as before, and vpp will be returned referenced and locked.
8872a31267eSMatthew Dillon 	 *
8882a31267eSMatthew Dillon 	 * We want to return with dvp as it was passed to us, so we get
8892a31267eSMatthew Dillon 	 * rid of our reference.
8902a31267eSMatthew Dillon 	 */
8912a31267eSMatthew Dillon 	vrele(dvp);
8922a31267eSMatthew Dillon 	return (0);
893996c772fSJohn Dyson }
894996c772fSJohn Dyson 
895996c772fSJohn Dyson /*
896df8bae1dSRodney W. Grimes  * Create a shadow directory in the upper layer.
897df8bae1dSRodney W. Grimes  * The new vnode is returned locked.
898df8bae1dSRodney W. Grimes  *
899df8bae1dSRodney W. Grimes  * (um) points to the union mount structure for access to the
900df8bae1dSRodney W. Grimes  * the mounting process's credentials.
9012a31267eSMatthew Dillon  * (dvp) is the directory in which to create the shadow directory,
9022a31267eSMatthew Dillon  * it is locked (but not ref'd) on entry and return.
903df8bae1dSRodney W. Grimes  * (cnp) is the componentname to be created.
904df8bae1dSRodney W. Grimes  * (vpp) is the returned newly created shadow directory, which
9052a31267eSMatthew Dillon  * is returned locked and ref'd
906df8bae1dSRodney W. Grimes  */
907df8bae1dSRodney W. Grimes int
908df8bae1dSRodney W. Grimes union_mkshadow(um, dvp, cnp, vpp)
909df8bae1dSRodney W. Grimes 	struct union_mount *um;
910df8bae1dSRodney W. Grimes 	struct vnode *dvp;
911df8bae1dSRodney W. Grimes 	struct componentname *cnp;
912df8bae1dSRodney W. Grimes 	struct vnode **vpp;
913df8bae1dSRodney W. Grimes {
914df8bae1dSRodney W. Grimes 	int error;
915df8bae1dSRodney W. Grimes 	struct vattr va;
916df8bae1dSRodney W. Grimes 	struct proc *p = cnp->cn_proc;
917df8bae1dSRodney W. Grimes 	struct componentname cn;
918f2a2857bSKirk McKusick 	struct mount *mp;
919df8bae1dSRodney W. Grimes 
920f2a2857bSKirk McKusick 	if ((error = vn_start_write(dvp, &mp, V_WAIT | PCATCH)) != 0)
921996c772fSJohn Dyson 		return (error);
922f2a2857bSKirk McKusick 	if ((error = union_relookup(um, dvp, vpp, cnp, &cn,
923f2a2857bSKirk McKusick 			cnp->cn_nameptr, cnp->cn_namelen)) != 0) {
924f2a2857bSKirk McKusick 		vn_finished_write(mp);
925f2a2857bSKirk McKusick 		return (error);
926f2a2857bSKirk McKusick 	}
927996c772fSJohn Dyson 
928996c772fSJohn Dyson 	if (*vpp) {
929762e6b85SEivind Eklund 		if (cn.cn_flags & HASBUF) {
930762e6b85SEivind Eklund 			zfree(namei_zone, cn.cn_pnbuf);
931762e6b85SEivind Eklund 			cn.cn_flags &= ~HASBUF;
932762e6b85SEivind Eklund 		}
9332a31267eSMatthew Dillon 		if (dvp == *vpp)
934996c772fSJohn Dyson 			vrele(*vpp);
9352a31267eSMatthew Dillon 		else
9362a31267eSMatthew Dillon 			vput(*vpp);
937f2a2857bSKirk McKusick 		vn_finished_write(mp);
938996c772fSJohn Dyson 		*vpp = NULLVP;
939996c772fSJohn Dyson 		return (EEXIST);
940996c772fSJohn Dyson 	}
941996c772fSJohn Dyson 
942df8bae1dSRodney W. Grimes 	/*
943df8bae1dSRodney W. Grimes 	 * policy: when creating the shadow directory in the
944df8bae1dSRodney W. Grimes 	 * upper layer, create it owned by the user who did
945df8bae1dSRodney W. Grimes 	 * the mount, group from parent directory, and mode
946df8bae1dSRodney W. Grimes 	 * 777 modified by umask (ie mostly identical to the
947df8bae1dSRodney W. Grimes 	 * mkdir syscall).  (jsp, kb)
948df8bae1dSRodney W. Grimes 	 */
949df8bae1dSRodney W. Grimes 
950df8bae1dSRodney W. Grimes 	VATTR_NULL(&va);
951df8bae1dSRodney W. Grimes 	va.va_type = VDIR;
952df8bae1dSRodney W. Grimes 	va.va_mode = um->um_cmode;
953df8bae1dSRodney W. Grimes 
954996c772fSJohn Dyson 	/* VOP_LEASE: dvp is locked */
955996c772fSJohn Dyson 	VOP_LEASE(dvp, p, cn.cn_cred, LEASE_WRITE);
956df8bae1dSRodney W. Grimes 
957df8bae1dSRodney W. Grimes 	error = VOP_MKDIR(dvp, vpp, &cn, &va);
958762e6b85SEivind Eklund 	if (cn.cn_flags & HASBUF) {
959762e6b85SEivind Eklund 		zfree(namei_zone, cn.cn_pnbuf);
960762e6b85SEivind Eklund 		cn.cn_flags &= ~HASBUF;
961762e6b85SEivind Eklund 	}
9622a31267eSMatthew Dillon 	/*vput(dvp);*/
963f2a2857bSKirk McKusick 	vn_finished_write(mp);
964df8bae1dSRodney W. Grimes 	return (error);
965df8bae1dSRodney W. Grimes }
966df8bae1dSRodney W. Grimes 
967df8bae1dSRodney W. Grimes /*
968996c772fSJohn Dyson  * Create a whiteout entry in the upper layer.
969996c772fSJohn Dyson  *
970996c772fSJohn Dyson  * (um) points to the union mount structure for access to the
971996c772fSJohn Dyson  * the mounting process's credentials.
972996c772fSJohn Dyson  * (dvp) is the directory in which to create the whiteout.
9732a31267eSMatthew Dillon  * it is locked on entry and return.
974996c772fSJohn Dyson  * (cnp) is the componentname to be created.
975996c772fSJohn Dyson  */
976996c772fSJohn Dyson int
977996c772fSJohn Dyson union_mkwhiteout(um, dvp, cnp, path)
978996c772fSJohn Dyson 	struct union_mount *um;
979996c772fSJohn Dyson 	struct vnode *dvp;
980996c772fSJohn Dyson 	struct componentname *cnp;
981996c772fSJohn Dyson 	char *path;
982996c772fSJohn Dyson {
983996c772fSJohn Dyson 	int error;
984996c772fSJohn Dyson 	struct proc *p = cnp->cn_proc;
985996c772fSJohn Dyson 	struct vnode *wvp;
986996c772fSJohn Dyson 	struct componentname cn;
987f2a2857bSKirk McKusick 	struct mount *mp;
988996c772fSJohn Dyson 
989f2a2857bSKirk McKusick 	if ((error = vn_start_write(dvp, &mp, V_WAIT | PCATCH)) != 0)
990996c772fSJohn Dyson 		return (error);
991f2a2857bSKirk McKusick 	error = union_relookup(um, dvp, &wvp, cnp, &cn, path, strlen(path));
992f2a2857bSKirk McKusick 	if (error) {
993f2a2857bSKirk McKusick 		vn_finished_write(mp);
994f2a2857bSKirk McKusick 		return (error);
995f2a2857bSKirk McKusick 	}
996996c772fSJohn Dyson 
997996c772fSJohn Dyson 	if (wvp) {
998762e6b85SEivind Eklund 		if (cn.cn_flags & HASBUF) {
999762e6b85SEivind Eklund 			zfree(namei_zone, cn.cn_pnbuf);
1000762e6b85SEivind Eklund 			cn.cn_flags &= ~HASBUF;
1001762e6b85SEivind Eklund 		}
10022a31267eSMatthew Dillon 		if (wvp == dvp)
1003996c772fSJohn Dyson 			vrele(wvp);
10042a31267eSMatthew Dillon 		else
10052a31267eSMatthew Dillon 			vput(wvp);
1006f2a2857bSKirk McKusick 		vn_finished_write(mp);
1007996c772fSJohn Dyson 		return (EEXIST);
1008996c772fSJohn Dyson 	}
1009996c772fSJohn Dyson 
1010996c772fSJohn Dyson 	/* VOP_LEASE: dvp is locked */
1011996c772fSJohn Dyson 	VOP_LEASE(dvp, p, p->p_ucred, LEASE_WRITE);
1012996c772fSJohn Dyson 
1013996c772fSJohn Dyson 	error = VOP_WHITEOUT(dvp, &cn, CREATE);
1014762e6b85SEivind Eklund 	if (cn.cn_flags & HASBUF) {
1015762e6b85SEivind Eklund 		zfree(namei_zone, cn.cn_pnbuf);
1016762e6b85SEivind Eklund 		cn.cn_flags &= ~HASBUF;
1017762e6b85SEivind Eklund 	}
1018f2a2857bSKirk McKusick 	vn_finished_write(mp);
1019996c772fSJohn Dyson 	return (error);
1020996c772fSJohn Dyson }
1021996c772fSJohn Dyson 
1022996c772fSJohn Dyson /*
1023df8bae1dSRodney W. Grimes  * union_vn_create: creates and opens a new shadow file
1024df8bae1dSRodney W. Grimes  * on the upper union layer.  this function is similar
1025df8bae1dSRodney W. Grimes  * in spirit to calling vn_open but it avoids calling namei().
1026df8bae1dSRodney W. Grimes  * the problem with calling namei is that a) it locks too many
1027df8bae1dSRodney W. Grimes  * things, and b) it doesn't start at the "right" directory,
1028df8bae1dSRodney W. Grimes  * whereas relookup is told where to start.
10292a31267eSMatthew Dillon  *
10302a31267eSMatthew Dillon  * On entry, the vnode associated with un is locked.  It remains locked
10312a31267eSMatthew Dillon  * on return.
10322a31267eSMatthew Dillon  *
10332a31267eSMatthew Dillon  * If no error occurs, *vpp contains a locked referenced vnode for your
10342a31267eSMatthew Dillon  * use.  If an error occurs *vpp iis undefined.
1035df8bae1dSRodney W. Grimes  */
103680b301c3SPoul-Henning Kamp static int
1037df8bae1dSRodney W. Grimes union_vn_create(vpp, un, p)
1038df8bae1dSRodney W. Grimes 	struct vnode **vpp;
1039df8bae1dSRodney W. Grimes 	struct union_node *un;
1040df8bae1dSRodney W. Grimes 	struct proc *p;
1041df8bae1dSRodney W. Grimes {
1042df8bae1dSRodney W. Grimes 	struct vnode *vp;
1043df8bae1dSRodney W. Grimes 	struct ucred *cred = p->p_ucred;
1044df8bae1dSRodney W. Grimes 	struct vattr vat;
1045df8bae1dSRodney W. Grimes 	struct vattr *vap = &vat;
1046df8bae1dSRodney W. Grimes 	int fmode = FFLAGS(O_WRONLY|O_CREAT|O_TRUNC|O_EXCL);
1047df8bae1dSRodney W. Grimes 	int error;
1048df8bae1dSRodney W. Grimes 	int cmode = UN_FILEMODE & ~p->p_fd->fd_cmask;
1049df8bae1dSRodney W. Grimes 	struct componentname cn;
1050df8bae1dSRodney W. Grimes 
1051df8bae1dSRodney W. Grimes 	*vpp = NULLVP;
1052df8bae1dSRodney W. Grimes 
1053df8bae1dSRodney W. Grimes 	/*
1054df8bae1dSRodney W. Grimes 	 * Build a new componentname structure (for the same
1055df8bae1dSRodney W. Grimes 	 * reasons outlines in union_mkshadow).
1056df8bae1dSRodney W. Grimes 	 * The difference here is that the file is owned by
1057df8bae1dSRodney W. Grimes 	 * the current user, rather than by the person who
1058df8bae1dSRodney W. Grimes 	 * did the mount, since the current user needs to be
1059df8bae1dSRodney W. Grimes 	 * able to write the file (that's why it is being
1060df8bae1dSRodney W. Grimes 	 * copied in the first place).
1061df8bae1dSRodney W. Grimes 	 */
1062df8bae1dSRodney W. Grimes 	cn.cn_namelen = strlen(un->un_path);
106399448ed1SJohn Dyson 	cn.cn_pnbuf = zalloc(namei_zone);
1064df8bae1dSRodney W. Grimes 	bcopy(un->un_path, cn.cn_pnbuf, cn.cn_namelen+1);
1065df8bae1dSRodney W. Grimes 	cn.cn_nameiop = CREATE;
10662a31267eSMatthew Dillon 	cn.cn_flags = (LOCKPARENT|LOCKLEAF|HASBUF|SAVENAME|ISLASTCN);
1067df8bae1dSRodney W. Grimes 	cn.cn_proc = p;
1068df8bae1dSRodney W. Grimes 	cn.cn_cred = p->p_ucred;
1069df8bae1dSRodney W. Grimes 	cn.cn_nameptr = cn.cn_pnbuf;
1070df8bae1dSRodney W. Grimes 	cn.cn_consume = 0;
1071df8bae1dSRodney W. Grimes 
10722a31267eSMatthew Dillon 	/*
10732a31267eSMatthew Dillon 	 * Pass dvp unlocked and referenced on call to relookup().
10742a31267eSMatthew Dillon 	 *
10752a31267eSMatthew Dillon 	 * If an error occurs, dvp will be returned unlocked and dereferenced.
10762a31267eSMatthew Dillon 	 */
1077df8bae1dSRodney W. Grimes 	VREF(un->un_dirvp);
10783a773ad0SPoul-Henning Kamp 	error = relookup(un->un_dirvp, &vp, &cn);
10793a773ad0SPoul-Henning Kamp 	if (error)
1080df8bae1dSRodney W. Grimes 		return (error);
1081df8bae1dSRodney W. Grimes 
10822a31267eSMatthew Dillon 	/*
10832a31267eSMatthew Dillon 	 * If no error occurs, dvp will be returned locked with the reference
10842a31267eSMatthew Dillon 	 * left as before, and vpp will be returned referenced and locked.
10852a31267eSMatthew Dillon 	 */
1086df8bae1dSRodney W. Grimes 	if (vp) {
1087df8bae1dSRodney W. Grimes 		vput(un->un_dirvp);
1088762e6b85SEivind Eklund 		if (cn.cn_flags & HASBUF) {
1089762e6b85SEivind Eklund 			zfree(namei_zone, cn.cn_pnbuf);
1090762e6b85SEivind Eklund 			cn.cn_flags &= ~HASBUF;
1091762e6b85SEivind Eklund 		}
10922a31267eSMatthew Dillon 		if (vp == un->un_dirvp)
1093df8bae1dSRodney W. Grimes 			vrele(vp);
10942a31267eSMatthew Dillon 		else
10952a31267eSMatthew Dillon 			vput(vp);
1096df8bae1dSRodney W. Grimes 		return (EEXIST);
1097df8bae1dSRodney W. Grimes 	}
1098df8bae1dSRodney W. Grimes 
1099df8bae1dSRodney W. Grimes 	/*
1100df8bae1dSRodney W. Grimes 	 * Good - there was no race to create the file
1101df8bae1dSRodney W. Grimes 	 * so go ahead and create it.  The permissions
1102df8bae1dSRodney W. Grimes 	 * on the file will be 0666 modified by the
1103df8bae1dSRodney W. Grimes 	 * current user's umask.  Access to the file, while
1104df8bae1dSRodney W. Grimes 	 * it is unioned, will require access to the top *and*
1105df8bae1dSRodney W. Grimes 	 * bottom files.  Access when not unioned will simply
1106df8bae1dSRodney W. Grimes 	 * require access to the top-level file.
1107df8bae1dSRodney W. Grimes 	 * TODO: confirm choice of access permissions.
1108df8bae1dSRodney W. Grimes 	 */
1109df8bae1dSRodney W. Grimes 	VATTR_NULL(vap);
1110df8bae1dSRodney W. Grimes 	vap->va_type = VREG;
1111df8bae1dSRodney W. Grimes 	vap->va_mode = cmode;
1112996c772fSJohn Dyson 	VOP_LEASE(un->un_dirvp, p, cred, LEASE_WRITE);
11137be2d300SMike Smith 	error = VOP_CREATE(un->un_dirvp, &vp, &cn, vap);
1114762e6b85SEivind Eklund 	if (cn.cn_flags & HASBUF) {
1115762e6b85SEivind Eklund 		zfree(namei_zone, cn.cn_pnbuf);
1116762e6b85SEivind Eklund 		cn.cn_flags &= ~HASBUF;
1117762e6b85SEivind Eklund 	}
11187be2d300SMike Smith 	vput(un->un_dirvp);
11197be2d300SMike Smith 	if (error)
1120df8bae1dSRodney W. Grimes 		return (error);
1121df8bae1dSRodney W. Grimes 
11223a773ad0SPoul-Henning Kamp 	error = VOP_OPEN(vp, fmode, cred, p);
11232a31267eSMatthew Dillon 	if (error == 0 && vn_canvmio(vp) == TRUE)
11242a31267eSMatthew Dillon 		error = vfs_object_create(vp, p, cred);
11253a773ad0SPoul-Henning Kamp 	if (error) {
1126df8bae1dSRodney W. Grimes 		vput(vp);
1127df8bae1dSRodney W. Grimes 		return (error);
1128df8bae1dSRodney W. Grimes 	}
1129df8bae1dSRodney W. Grimes 	vp->v_writecount++;
1130df8bae1dSRodney W. Grimes 	*vpp = vp;
1131df8bae1dSRodney W. Grimes 	return (0);
1132df8bae1dSRodney W. Grimes }
1133df8bae1dSRodney W. Grimes 
113480b301c3SPoul-Henning Kamp static int
1135df8bae1dSRodney W. Grimes union_vn_close(vp, fmode, cred, p)
1136df8bae1dSRodney W. Grimes 	struct vnode *vp;
1137df8bae1dSRodney W. Grimes 	int fmode;
1138df8bae1dSRodney W. Grimes 	struct ucred *cred;
1139df8bae1dSRodney W. Grimes 	struct proc *p;
1140df8bae1dSRodney W. Grimes {
1141996c772fSJohn Dyson 
1142df8bae1dSRodney W. Grimes 	if (fmode & FWRITE)
1143df8bae1dSRodney W. Grimes 		--vp->v_writecount;
1144cf2455a3SBruce Evans 	return (VOP_CLOSE(vp, fmode, cred, p));
1145df8bae1dSRodney W. Grimes }
1146df8bae1dSRodney W. Grimes 
11472a31267eSMatthew Dillon #if 0
11482a31267eSMatthew Dillon 
11492a31267eSMatthew Dillon /*
11502a31267eSMatthew Dillon  *	union_removed_upper:
11512a31267eSMatthew Dillon  *
11522a31267eSMatthew Dillon  *	called with union_node unlocked. XXX
11532a31267eSMatthew Dillon  */
11542a31267eSMatthew Dillon 
1155df8bae1dSRodney W. Grimes void
1156df8bae1dSRodney W. Grimes union_removed_upper(un)
1157df8bae1dSRodney W. Grimes 	struct union_node *un;
1158df8bae1dSRodney W. Grimes {
1159996c772fSJohn Dyson 	struct proc *p = curproc;	/* XXX */
1160b69aa7f1SKATO Takenori 	struct vnode **vpp;
1161df8bae1dSRodney W. Grimes 
1162b69aa7f1SKATO Takenori 	/*
1163b69aa7f1SKATO Takenori 	 * Do not set the uppervp to NULLVP.  If lowervp is NULLVP,
11649758931dSKATO Takenori 	 * union node will have neither uppervp nor lowervp.  We remove
1165b69aa7f1SKATO Takenori 	 * the union node from cache, so that it will not be referrenced.
1166b69aa7f1SKATO Takenori 	 */
1167df8bae1dSRodney W. Grimes 	union_newupper(un, NULLVP);
1168b69aa7f1SKATO Takenori 	if (un->un_dircache != 0) {
1169b69aa7f1SKATO Takenori 		for (vpp = un->un_dircache; *vpp != NULLVP; vpp++)
1170b69aa7f1SKATO Takenori 			vrele(*vpp);
1171b69aa7f1SKATO Takenori 		free(un->un_dircache, M_TEMP);
1172b69aa7f1SKATO Takenori 		un->un_dircache = 0;
1173b69aa7f1SKATO Takenori 	}
1174b69aa7f1SKATO Takenori 
1175996c772fSJohn Dyson 	if (un->un_flags & UN_CACHED) {
1176996c772fSJohn Dyson 		un->un_flags &= ~UN_CACHED;
1177996c772fSJohn Dyson 		LIST_REMOVE(un, un_cache);
1178df8bae1dSRodney W. Grimes 	}
1179996c772fSJohn Dyson }
1180996c772fSJohn Dyson 
1181996c772fSJohn Dyson #endif
1182996c772fSJohn Dyson 
1183996c772fSJohn Dyson /*
1184996c772fSJohn Dyson  * determine whether a whiteout is needed
1185996c772fSJohn Dyson  * during a remove/rmdir operation.
1186996c772fSJohn Dyson  */
1187996c772fSJohn Dyson int
1188996c772fSJohn Dyson union_dowhiteout(un, cred, p)
1189996c772fSJohn Dyson 	struct union_node *un;
1190996c772fSJohn Dyson 	struct ucred *cred;
1191996c772fSJohn Dyson 	struct proc *p;
1192996c772fSJohn Dyson {
1193996c772fSJohn Dyson 	struct vattr va;
1194996c772fSJohn Dyson 
1195996c772fSJohn Dyson 	if (un->un_lowervp != NULLVP)
1196996c772fSJohn Dyson 		return (1);
1197996c772fSJohn Dyson 
1198996c772fSJohn Dyson 	if (VOP_GETATTR(un->un_uppervp, &va, cred, p) == 0 &&
1199996c772fSJohn Dyson 	    (va.va_flags & OPAQUE))
1200996c772fSJohn Dyson 		return (1);
1201996c772fSJohn Dyson 
1202996c772fSJohn Dyson 	return (0);
1203996c772fSJohn Dyson }
1204996c772fSJohn Dyson 
1205996c772fSJohn Dyson static void
1206996c772fSJohn Dyson union_dircache_r(vp, vppp, cntp)
1207996c772fSJohn Dyson 	struct vnode *vp;
1208996c772fSJohn Dyson 	struct vnode ***vppp;
1209996c772fSJohn Dyson 	int *cntp;
1210996c772fSJohn Dyson {
1211996c772fSJohn Dyson 	struct union_node *un;
1212996c772fSJohn Dyson 
1213996c772fSJohn Dyson 	if (vp->v_op != union_vnodeop_p) {
1214996c772fSJohn Dyson 		if (vppp) {
1215996c772fSJohn Dyson 			VREF(vp);
1216996c772fSJohn Dyson 			*(*vppp)++ = vp;
1217996c772fSJohn Dyson 			if (--(*cntp) == 0)
1218996c772fSJohn Dyson 				panic("union: dircache table too small");
1219996c772fSJohn Dyson 		} else {
1220996c772fSJohn Dyson 			(*cntp)++;
1221996c772fSJohn Dyson 		}
1222996c772fSJohn Dyson 
1223996c772fSJohn Dyson 		return;
1224996c772fSJohn Dyson 	}
1225996c772fSJohn Dyson 
1226996c772fSJohn Dyson 	un = VTOUNION(vp);
1227996c772fSJohn Dyson 	if (un->un_uppervp != NULLVP)
1228996c772fSJohn Dyson 		union_dircache_r(un->un_uppervp, vppp, cntp);
1229996c772fSJohn Dyson 	if (un->un_lowervp != NULLVP)
1230996c772fSJohn Dyson 		union_dircache_r(un->un_lowervp, vppp, cntp);
1231996c772fSJohn Dyson }
1232996c772fSJohn Dyson 
1233996c772fSJohn Dyson struct vnode *
1234996c772fSJohn Dyson union_dircache(vp, p)
1235996c772fSJohn Dyson 	struct vnode *vp;
1236996c772fSJohn Dyson 	struct proc *p;
1237996c772fSJohn Dyson {
1238996c772fSJohn Dyson 	int cnt;
1239996c772fSJohn Dyson 	struct vnode *nvp;
1240996c772fSJohn Dyson 	struct vnode **vpp;
1241996c772fSJohn Dyson 	struct vnode **dircache;
1242996c772fSJohn Dyson 	struct union_node *un;
1243996c772fSJohn Dyson 	int error;
1244996c772fSJohn Dyson 
1245996c772fSJohn Dyson 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
1246996c772fSJohn Dyson 	dircache = VTOUNION(vp)->un_dircache;
1247996c772fSJohn Dyson 
1248996c772fSJohn Dyson 	nvp = NULLVP;
1249996c772fSJohn Dyson 
12502a31267eSMatthew Dillon 	if (dircache == NULL) {
1251996c772fSJohn Dyson 		cnt = 0;
1252996c772fSJohn Dyson 		union_dircache_r(vp, 0, &cnt);
1253996c772fSJohn Dyson 		cnt++;
12542a31267eSMatthew Dillon 		dircache = malloc(cnt * sizeof(struct vnode *),
1255996c772fSJohn Dyson 				M_TEMP, M_WAITOK);
1256996c772fSJohn Dyson 		vpp = dircache;
1257996c772fSJohn Dyson 		union_dircache_r(vp, &vpp, &cnt);
1258996c772fSJohn Dyson 		*vpp = NULLVP;
1259996c772fSJohn Dyson 		vpp = dircache + 1;
1260996c772fSJohn Dyson 	} else {
1261996c772fSJohn Dyson 		vpp = dircache;
1262996c772fSJohn Dyson 		do {
1263996c772fSJohn Dyson 			if (*vpp++ == VTOUNION(vp)->un_uppervp)
1264996c772fSJohn Dyson 				break;
1265996c772fSJohn Dyson 		} while (*vpp != NULLVP);
1266996c772fSJohn Dyson 	}
1267996c772fSJohn Dyson 
1268996c772fSJohn Dyson 	if (*vpp == NULLVP)
1269996c772fSJohn Dyson 		goto out;
1270996c772fSJohn Dyson 
12712a31267eSMatthew Dillon 	/*vn_lock(*vpp, LK_EXCLUSIVE | LK_RETRY, p);*/
12722a31267eSMatthew Dillon 	UDEBUG(("ALLOCVP-3 %p ref %d\n", *vpp, (*vpp ? (*vpp)->v_usecount : -99)));
1273996c772fSJohn Dyson 	VREF(*vpp);
12742a31267eSMatthew Dillon 	error = union_allocvp(&nvp, vp->v_mount, NULLVP, NULLVP, NULL, *vpp, NULLVP, 0);
12752a31267eSMatthew Dillon 	UDEBUG(("ALLOCVP-3B %p ref %d\n", nvp, (*vpp ? (*vpp)->v_usecount : -99)));
1276996c772fSJohn Dyson 	if (error)
1277996c772fSJohn Dyson 		goto out;
1278996c772fSJohn Dyson 
1279996c772fSJohn Dyson 	VTOUNION(vp)->un_dircache = 0;
1280996c772fSJohn Dyson 	un = VTOUNION(nvp);
1281996c772fSJohn Dyson 	un->un_dircache = dircache;
1282996c772fSJohn Dyson 
1283996c772fSJohn Dyson out:
1284996c772fSJohn Dyson 	VOP_UNLOCK(vp, 0, p);
1285996c772fSJohn Dyson 	return (nvp);
1286df8bae1dSRodney W. Grimes }
12878c14bf40SPeter Wemm 
12888c14bf40SPeter Wemm /*
12892a31267eSMatthew Dillon  * Guarentee coherency with the VM cache by invalidating any clean VM pages
12902a31267eSMatthew Dillon  * associated with this write and updating any dirty VM pages.  Since our
12912a31267eSMatthew Dillon  * vnode is locked, other processes will not be able to read the pages in
12922a31267eSMatthew Dillon  * again until after our write completes.
12932a31267eSMatthew Dillon  *
12942a31267eSMatthew Dillon  * We also have to be coherent with reads, by flushing any pending dirty
12952a31267eSMatthew Dillon  * pages prior to issuing the read.
12962a31267eSMatthew Dillon  *
12972a31267eSMatthew Dillon  * XXX this is somewhat of a hack at the moment.  To support this properly
12982a31267eSMatthew Dillon  * we would have to be able to run VOP_READ and VOP_WRITE through the VM
12992a31267eSMatthew Dillon  * cache.  Then we wouldn't need to worry about coherency.
13002a31267eSMatthew Dillon  */
13012a31267eSMatthew Dillon 
13022a31267eSMatthew Dillon void
13032a31267eSMatthew Dillon union_vm_coherency(struct vnode *vp, struct uio *uio, int cleanfls)
13042a31267eSMatthew Dillon {
13052a31267eSMatthew Dillon 	vm_object_t object;
13062a31267eSMatthew Dillon 	vm_pindex_t pstart;
13072a31267eSMatthew Dillon 	vm_pindex_t pend;
13082a31267eSMatthew Dillon 	int pgoff;
13092a31267eSMatthew Dillon 
13102a31267eSMatthew Dillon 	if ((object = vp->v_object) == NULL)
13112a31267eSMatthew Dillon 	    return;
13122a31267eSMatthew Dillon 
13132a31267eSMatthew Dillon 	pgoff = uio->uio_offset & PAGE_MASK;
13142a31267eSMatthew Dillon 	pstart = uio->uio_offset / PAGE_SIZE;
13152a31267eSMatthew Dillon 	pend = pstart + (uio->uio_resid + pgoff + PAGE_MASK) / PAGE_SIZE;
13162a31267eSMatthew Dillon 
13172a31267eSMatthew Dillon 	vm_object_page_clean(object, pstart, pend, OBJPC_SYNC);
13182a31267eSMatthew Dillon 	if (cleanfls)
13192a31267eSMatthew Dillon 		vm_object_page_remove(object, pstart, pend, TRUE);
13202a31267eSMatthew Dillon }
13212a31267eSMatthew Dillon 
13222a31267eSMatthew Dillon /*
13238c14bf40SPeter Wemm  * Module glue to remove #ifdef UNION from vfs_syscalls.c
13248c14bf40SPeter Wemm  */
13258c14bf40SPeter Wemm static int
13268c14bf40SPeter Wemm union_dircheck(struct proc *p, struct vnode **vp, struct file *fp)
13278c14bf40SPeter Wemm {
13288c14bf40SPeter Wemm 	int error = 0;
13298c14bf40SPeter Wemm 
13308c14bf40SPeter Wemm 	if ((*vp)->v_op == union_vnodeop_p) {
13318c14bf40SPeter Wemm 		struct vnode *lvp;
13328c14bf40SPeter Wemm 
13338c14bf40SPeter Wemm 		lvp = union_dircache(*vp, p);
13348c14bf40SPeter Wemm 		if (lvp != NULLVP) {
13358c14bf40SPeter Wemm 			struct vattr va;
13368c14bf40SPeter Wemm 
13378c14bf40SPeter Wemm 			/*
13388c14bf40SPeter Wemm 			 * If the directory is opaque,
13398c14bf40SPeter Wemm 			 * then don't show lower entries
13408c14bf40SPeter Wemm 			 */
13418c14bf40SPeter Wemm 			error = VOP_GETATTR(*vp, &va, fp->f_cred, p);
13428c14bf40SPeter Wemm 			if (va.va_flags & OPAQUE) {
13438c14bf40SPeter Wemm 				vput(lvp);
13448c14bf40SPeter Wemm 				lvp = NULL;
13458c14bf40SPeter Wemm 			}
13468c14bf40SPeter Wemm 		}
13478c14bf40SPeter Wemm 
13488c14bf40SPeter Wemm 		if (lvp != NULLVP) {
13498c14bf40SPeter Wemm 			error = VOP_OPEN(lvp, FREAD, fp->f_cred, p);
13502a31267eSMatthew Dillon 			if (error == 0 && vn_canvmio(lvp) == TRUE)
13512a31267eSMatthew Dillon 				error = vfs_object_create(lvp, p, fp->f_cred);
13528c14bf40SPeter Wemm 			if (error) {
13538c14bf40SPeter Wemm 				vput(lvp);
13548c14bf40SPeter Wemm 				return (error);
13558c14bf40SPeter Wemm 			}
13568c14bf40SPeter Wemm 			VOP_UNLOCK(lvp, 0, p);
13578c14bf40SPeter Wemm 			fp->f_data = (caddr_t) lvp;
13588c14bf40SPeter Wemm 			fp->f_offset = 0;
13598c14bf40SPeter Wemm 			error = vn_close(*vp, FREAD, fp->f_cred, p);
13608c14bf40SPeter Wemm 			if (error)
13618c14bf40SPeter Wemm 				return (error);
13628c14bf40SPeter Wemm 			*vp = lvp;
13638c14bf40SPeter Wemm 			return -1;	/* goto unionread */
13648c14bf40SPeter Wemm 		}
13658c14bf40SPeter Wemm 	}
13668c14bf40SPeter Wemm 	return error;
13678c14bf40SPeter Wemm }
13688c14bf40SPeter Wemm 
1369c25ded31SBruce Evans static int
1370c25ded31SBruce Evans union_modevent(module_t mod, int type, void *data)
13718c14bf40SPeter Wemm {
13728c14bf40SPeter Wemm 	switch (type) {
13738c14bf40SPeter Wemm 	case MOD_LOAD:
13748c14bf40SPeter Wemm 		union_dircheckp = union_dircheck;
13758c14bf40SPeter Wemm 		break;
13768c14bf40SPeter Wemm 	case MOD_UNLOAD:
13778c14bf40SPeter Wemm 		union_dircheckp = NULL;
13788c14bf40SPeter Wemm 		break;
13798c14bf40SPeter Wemm 	default:
13808c14bf40SPeter Wemm 		break;
13818c14bf40SPeter Wemm 	}
13828c14bf40SPeter Wemm 	return 0;
13838c14bf40SPeter Wemm }
13842a31267eSMatthew Dillon 
13858c14bf40SPeter Wemm static moduledata_t union_mod = {
13868c14bf40SPeter Wemm 	"union_dircheck",
13878c14bf40SPeter Wemm 	union_modevent,
13888c14bf40SPeter Wemm 	NULL
13898c14bf40SPeter Wemm };
13902a31267eSMatthew Dillon 
13918c14bf40SPeter Wemm DECLARE_MODULE(union_dircheck, union_mod, SI_SUB_VFS, SI_ORDER_ANY);
1392