xref: /freebsd/sys/fs/unionfs/union_vfsops.c (revision 805d90f763d7f90af4c1da32dd60f9a61b760caf)
1df8bae1dSRodney W. Grimes /*
2996c772fSJohn Dyson  * Copyright (c) 1994, 1995 The Regents of the University of California.
3996c772fSJohn Dyson  * Copyright (c) 1994, 1995 Jan-Simon Pendry.
4df8bae1dSRodney W. Grimes  * All rights reserved.
5df8bae1dSRodney W. Grimes  *
6df8bae1dSRodney W. Grimes  * This code is derived from software donated 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_vfsops.c	8.20 (Berkeley) 5/20/95
38c3aac50fSPeter Wemm  * $FreeBSD$
39df8bae1dSRodney W. Grimes  */
40df8bae1dSRodney W. Grimes 
41df8bae1dSRodney W. Grimes /*
42df8bae1dSRodney W. Grimes  * Union Layer
43df8bae1dSRodney W. Grimes  */
44df8bae1dSRodney W. Grimes 
45df8bae1dSRodney W. Grimes #include <sys/param.h>
46df8bae1dSRodney W. Grimes #include <sys/systm.h>
47c9b1d604SGarrett Wollman #include <sys/kernel.h>
48805d90f7SJohn Baldwin #include <sys/lock.h>
49805d90f7SJohn Baldwin #include <sys/mutex.h>
50df8bae1dSRodney W. Grimes #include <sys/proc.h>
51df8bae1dSRodney W. Grimes #include <sys/vnode.h>
52df8bae1dSRodney W. Grimes #include <sys/mount.h>
53df8bae1dSRodney W. Grimes #include <sys/namei.h>
54df8bae1dSRodney W. Grimes #include <sys/malloc.h>
55df8bae1dSRodney W. Grimes #include <sys/filedesc.h>
5699d300a1SRuslan Ermilov #include <fs/unionfs/union.h>
57df8bae1dSRodney W. Grimes 
58a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_UNIONFSMNT, "UNION mount", "UNION mount structure");
59a1c995b6SPoul-Henning Kamp 
60996c772fSJohn Dyson extern int	union_init __P((struct vfsconf *));
6180b301c3SPoul-Henning Kamp static int	union_mount __P((struct mount *mp, char *path, caddr_t data,
629b5e8b3aSBruce Evans 				 struct nameidata *ndp, struct proc *p));
6380b301c3SPoul-Henning Kamp static int	union_root __P((struct mount *mp, struct vnode **vpp));
6480b301c3SPoul-Henning Kamp static int	union_statfs __P((struct mount *mp, struct statfs *sbp,
659b5e8b3aSBruce Evans 				  struct proc *p));
6680b301c3SPoul-Henning Kamp static int	union_unmount __P((struct mount *mp, int mntflags,
679b5e8b3aSBruce Evans 				   struct proc *p));
68b5e8ce9fSBruce Evans 
69df8bae1dSRodney W. Grimes /*
70df8bae1dSRodney W. Grimes  * Mount union filesystem
71df8bae1dSRodney W. Grimes  */
7280b301c3SPoul-Henning Kamp static int
73df8bae1dSRodney W. Grimes union_mount(mp, path, data, ndp, p)
74df8bae1dSRodney W. Grimes 	struct mount *mp;
75df8bae1dSRodney W. Grimes 	char *path;
76df8bae1dSRodney W. Grimes 	caddr_t data;
77df8bae1dSRodney W. Grimes 	struct nameidata *ndp;
78df8bae1dSRodney W. Grimes 	struct proc *p;
79df8bae1dSRodney W. Grimes {
80df8bae1dSRodney W. Grimes 	int error = 0;
81df8bae1dSRodney W. Grimes 	struct union_args args;
82df8bae1dSRodney W. Grimes 	struct vnode *lowerrootvp = NULLVP;
83df8bae1dSRodney W. Grimes 	struct vnode *upperrootvp = NULLVP;
84996c772fSJohn Dyson 	struct union_mount *um = 0;
85df8bae1dSRodney W. Grimes 	struct ucred *cred = 0;
8626f9a767SRodney W. Grimes 	char *cp = 0;
87df8bae1dSRodney W. Grimes 	int len;
88df8bae1dSRodney W. Grimes 	u_int size;
89df8bae1dSRodney W. Grimes 
902a31267eSMatthew Dillon 	UDEBUG(("union_mount(mp = %p)\n", (void *)mp));
91df8bae1dSRodney W. Grimes 
92df8bae1dSRodney W. Grimes 	/*
9381bca6ddSKATO Takenori 	 * Disable clustered write, otherwise system becomes unstable.
9481bca6ddSKATO Takenori 	 */
9581bca6ddSKATO Takenori 	mp->mnt_flag |= MNT_NOCLUSTERW;
9681bca6ddSKATO Takenori 
9781bca6ddSKATO Takenori 	/*
98df8bae1dSRodney W. Grimes 	 * Update is a no-op
99df8bae1dSRodney W. Grimes 	 */
100df8bae1dSRodney W. Grimes 	if (mp->mnt_flag & MNT_UPDATE) {
101df8bae1dSRodney W. Grimes 		/*
102df8bae1dSRodney W. Grimes 		 * Need to provide.
103df8bae1dSRodney W. Grimes 		 * 1. a way to convert between rdonly and rdwr mounts.
104df8bae1dSRodney W. Grimes 		 * 2. support for nfs exports.
105df8bae1dSRodney W. Grimes 		 */
106df8bae1dSRodney W. Grimes 		error = EOPNOTSUPP;
107df8bae1dSRodney W. Grimes 		goto bad;
108df8bae1dSRodney W. Grimes 	}
109df8bae1dSRodney W. Grimes 
110df8bae1dSRodney W. Grimes 	/*
111df8bae1dSRodney W. Grimes 	 * Get argument
112df8bae1dSRodney W. Grimes 	 */
1133a773ad0SPoul-Henning Kamp 	error = copyin(data, (caddr_t)&args, sizeof(struct union_args));
1143a773ad0SPoul-Henning Kamp 	if (error)
115df8bae1dSRodney W. Grimes 		goto bad;
116df8bae1dSRodney W. Grimes 
1172a31267eSMatthew Dillon 	/*
1182a31267eSMatthew Dillon 	 * Obtain lower vnode.  Vnode is stored in mp->mnt_vnodecovered.
1192a31267eSMatthew Dillon 	 * We need to reference it but not lock it.
1202a31267eSMatthew Dillon 	 */
1212a31267eSMatthew Dillon 
122df8bae1dSRodney W. Grimes 	lowerrootvp = mp->mnt_vnodecovered;
123df8bae1dSRodney W. Grimes 	VREF(lowerrootvp);
124df8bae1dSRodney W. Grimes 
1252a31267eSMatthew Dillon #if 0
126df8bae1dSRodney W. Grimes 	/*
1276db918e3SKATO Takenori 	 * Unlock lower node to avoid deadlock.
1286db918e3SKATO Takenori 	 */
129afc2a558SKATO Takenori 	if (lowerrootvp->v_op == union_vnodeop_p)
1306db918e3SKATO Takenori 		VOP_UNLOCK(lowerrootvp, 0, p);
1312a31267eSMatthew Dillon #endif
1326db918e3SKATO Takenori 
1336db918e3SKATO Takenori 	/*
1342a31267eSMatthew Dillon 	 * Obtain upper vnode by calling namei() on the path.  The
1352a31267eSMatthew Dillon 	 * upperrootvp will be turned referenced but not locked.
136df8bae1dSRodney W. Grimes 	 */
137df8bae1dSRodney W. Grimes 	NDINIT(ndp, LOOKUP, FOLLOW|WANTPARENT,
138df8bae1dSRodney W. Grimes 	       UIO_USERSPACE, args.target, p);
139df8bae1dSRodney W. Grimes 
1403a773ad0SPoul-Henning Kamp 	error = namei(ndp);
1412a31267eSMatthew Dillon 
1422a31267eSMatthew Dillon #if 0
143afc2a558SKATO Takenori 	if (lowerrootvp->v_op == union_vnodeop_p)
1446db918e3SKATO Takenori 		vn_lock(lowerrootvp, LK_EXCLUSIVE | LK_RETRY, p);
1452a31267eSMatthew Dillon #endif
1463a773ad0SPoul-Henning Kamp 	if (error)
147df8bae1dSRodney W. Grimes 		goto bad;
148df8bae1dSRodney W. Grimes 
149762e6b85SEivind Eklund 	NDFREE(ndp, NDF_ONLY_PNBUF);
150df8bae1dSRodney W. Grimes 	upperrootvp = ndp->ni_vp;
151df8bae1dSRodney W. Grimes 	vrele(ndp->ni_dvp);
152df8bae1dSRodney W. Grimes 	ndp->ni_dvp = NULL;
153df8bae1dSRodney W. Grimes 
1546bdfe06aSEivind Eklund 	UDEBUG(("mount_root UPPERVP %p locked = %d\n", upperrootvp,
1556bdfe06aSEivind Eklund 	    VOP_ISLOCKED(upperrootvp, NULL)));
1562a31267eSMatthew Dillon 
1576db918e3SKATO Takenori 	/*
1586db918e3SKATO Takenori 	 * Check multi union mount to avoid `lock myself again' panic.
1592a31267eSMatthew Dillon 	 * Also require that it be a directory.
1606db918e3SKATO Takenori 	 */
1616db918e3SKATO Takenori 	if (upperrootvp == VTOUNION(lowerrootvp)->un_uppervp) {
162747e9157SKATO Takenori #ifdef DIAGNOSTIC
163747e9157SKATO Takenori 		printf("union_mount: multi union mount?\n");
164747e9157SKATO Takenori #endif
1656db918e3SKATO Takenori 		error = EDEADLK;
1666db918e3SKATO Takenori 		goto bad;
1676db918e3SKATO Takenori 	}
1686db918e3SKATO Takenori 
169df8bae1dSRodney W. Grimes 	if (upperrootvp->v_type != VDIR) {
170df8bae1dSRodney W. Grimes 		error = EINVAL;
171df8bae1dSRodney W. Grimes 		goto bad;
172df8bae1dSRodney W. Grimes 	}
173df8bae1dSRodney W. Grimes 
174df8bae1dSRodney W. Grimes 	/*
1752a31267eSMatthew Dillon 	 * Allocate our union_mount structure and populate the fields.
1762a31267eSMatthew Dillon 	 * The vnode references are stored in the union_mount as held,
1772a31267eSMatthew Dillon 	 * unlocked references.  Depending on the _BELOW flag, the
1782a31267eSMatthew Dillon 	 * filesystems are viewed in a different order.  In effect this
1792a31267eSMatthew Dillon 	 * is the same as providing a mount-under option to the mount
1802a31267eSMatthew Dillon 	 * syscall.
181df8bae1dSRodney W. Grimes 	 */
182df8bae1dSRodney W. Grimes 
1832a31267eSMatthew Dillon 	um = (struct union_mount *) malloc(sizeof(struct union_mount),
1847cc0979fSDavid Malone 				M_UNIONFSMNT, M_WAITOK | M_ZERO);
1852a31267eSMatthew Dillon 
186df8bae1dSRodney W. Grimes 	um->um_op = args.mntflags & UNMNT_OPMASK;
1872a31267eSMatthew Dillon 
188df8bae1dSRodney W. Grimes 	switch (um->um_op) {
189df8bae1dSRodney W. Grimes 	case UNMNT_ABOVE:
190df8bae1dSRodney W. Grimes 		um->um_lowervp = lowerrootvp;
191df8bae1dSRodney W. Grimes 		um->um_uppervp = upperrootvp;
1922a31267eSMatthew Dillon 		upperrootvp = NULL;
1932a31267eSMatthew Dillon 		lowerrootvp = NULL;
194df8bae1dSRodney W. Grimes 		break;
195df8bae1dSRodney W. Grimes 
196df8bae1dSRodney W. Grimes 	case UNMNT_BELOW:
197df8bae1dSRodney W. Grimes 		um->um_lowervp = upperrootvp;
198df8bae1dSRodney W. Grimes 		um->um_uppervp = lowerrootvp;
1992a31267eSMatthew Dillon 		upperrootvp = NULL;
2002a31267eSMatthew Dillon 		lowerrootvp = NULL;
201df8bae1dSRodney W. Grimes 		break;
202df8bae1dSRodney W. Grimes 
203df8bae1dSRodney W. Grimes 	case UNMNT_REPLACE:
204df8bae1dSRodney W. Grimes 		vrele(lowerrootvp);
2052a31267eSMatthew Dillon 		lowerrootvp = NULL;
206df8bae1dSRodney W. Grimes 		um->um_uppervp = upperrootvp;
207df8bae1dSRodney W. Grimes 		um->um_lowervp = lowerrootvp;
2082a31267eSMatthew Dillon 		upperrootvp = NULL;
209df8bae1dSRodney W. Grimes 		break;
210df8bae1dSRodney W. Grimes 
211df8bae1dSRodney W. Grimes 	default:
212df8bae1dSRodney W. Grimes 		error = EINVAL;
213df8bae1dSRodney W. Grimes 		goto bad;
214df8bae1dSRodney W. Grimes 	}
215df8bae1dSRodney W. Grimes 
216996c772fSJohn Dyson 	/*
217996c772fSJohn Dyson 	 * Unless the mount is readonly, ensure that the top layer
218996c772fSJohn Dyson 	 * supports whiteout operations
219996c772fSJohn Dyson 	 */
220996c772fSJohn Dyson 	if ((mp->mnt_flag & MNT_RDONLY) == 0) {
2212a31267eSMatthew Dillon 		error = VOP_WHITEOUT(um->um_uppervp, NULL, LOOKUP);
222996c772fSJohn Dyson 		if (error)
223996c772fSJohn Dyson 			goto bad;
224996c772fSJohn Dyson 	}
225996c772fSJohn Dyson 
226996c772fSJohn Dyson 	um->um_cred = p->p_ucred;
227996c772fSJohn Dyson 	crhold(um->um_cred);
228df8bae1dSRodney W. Grimes 	um->um_cmode = UN_DIRMODE &~ p->p_fd->fd_cmask;
229df8bae1dSRodney W. Grimes 
230df8bae1dSRodney W. Grimes 	/*
231df8bae1dSRodney W. Grimes 	 * Depending on what you think the MNT_LOCAL flag might mean,
232df8bae1dSRodney W. Grimes 	 * you may want the && to be || on the conditional below.
233df8bae1dSRodney W. Grimes 	 * At the moment it has been defined that the filesystem is
234df8bae1dSRodney W. Grimes 	 * only local if it is all local, ie the MNT_LOCAL flag implies
235df8bae1dSRodney W. Grimes 	 * that the entire namespace is local.  If you think the MNT_LOCAL
236df8bae1dSRodney W. Grimes 	 * flag implies that some of the files might be stored locally
237df8bae1dSRodney W. Grimes 	 * then you will want to change the conditional.
238df8bae1dSRodney W. Grimes 	 */
239df8bae1dSRodney W. Grimes 	if (um->um_op == UNMNT_ABOVE) {
240df8bae1dSRodney W. Grimes 		if (((um->um_lowervp == NULLVP) ||
241df8bae1dSRodney W. Grimes 		     (um->um_lowervp->v_mount->mnt_flag & MNT_LOCAL)) &&
242df8bae1dSRodney W. Grimes 		    (um->um_uppervp->v_mount->mnt_flag & MNT_LOCAL))
243df8bae1dSRodney W. Grimes 			mp->mnt_flag |= MNT_LOCAL;
244df8bae1dSRodney W. Grimes 	}
245df8bae1dSRodney W. Grimes 
246df8bae1dSRodney W. Grimes 	/*
247df8bae1dSRodney W. Grimes 	 * Copy in the upper layer's RDONLY flag.  This is for the benefit
248df8bae1dSRodney W. Grimes 	 * of lookup() which explicitly checks the flag, rather than asking
249dc733423SDag-Erling Smørgrav 	 * the filesystem for its own opinion.  This means, that an update
250df8bae1dSRodney W. Grimes 	 * mount of the underlying filesystem to go from rdonly to rdwr
251df8bae1dSRodney W. Grimes 	 * will leave the unioned view as read-only.
252df8bae1dSRodney W. Grimes 	 */
253df8bae1dSRodney W. Grimes 	mp->mnt_flag |= (um->um_uppervp->v_mount->mnt_flag & MNT_RDONLY);
254df8bae1dSRodney W. Grimes 
255df8bae1dSRodney W. Grimes 	mp->mnt_data = (qaddr_t) um;
256996c772fSJohn Dyson 	vfs_getnewfsid(mp);
257df8bae1dSRodney W. Grimes 
258df8bae1dSRodney W. Grimes 	switch (um->um_op) {
259df8bae1dSRodney W. Grimes 	case UNMNT_ABOVE:
260996c772fSJohn Dyson 		cp = "<above>:";
261df8bae1dSRodney W. Grimes 		break;
262df8bae1dSRodney W. Grimes 	case UNMNT_BELOW:
263996c772fSJohn Dyson 		cp = "<below>:";
264df8bae1dSRodney W. Grimes 		break;
265df8bae1dSRodney W. Grimes 	case UNMNT_REPLACE:
266df8bae1dSRodney W. Grimes 		cp = "";
267df8bae1dSRodney W. Grimes 		break;
268df8bae1dSRodney W. Grimes 	}
269df8bae1dSRodney W. Grimes 	len = strlen(cp);
270df8bae1dSRodney W. Grimes 	bcopy(cp, mp->mnt_stat.f_mntfromname, len);
271df8bae1dSRodney W. Grimes 
272df8bae1dSRodney W. Grimes 	cp = mp->mnt_stat.f_mntfromname + len;
273df8bae1dSRodney W. Grimes 	len = MNAMELEN - len;
274df8bae1dSRodney W. Grimes 
275df8bae1dSRodney W. Grimes 	(void) copyinstr(args.target, cp, len - 1, &size);
276df8bae1dSRodney W. Grimes 	bzero(cp + size, len - size);
277df8bae1dSRodney W. Grimes 
278c4a7b7e1SDavid Greenman 	(void)union_statfs(mp, &mp->mnt_stat, p);
279c4a7b7e1SDavid Greenman 
2802a31267eSMatthew Dillon 	UDEBUG(("union_mount: from %s, on %s\n",
2812a31267eSMatthew Dillon 		mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname));
282df8bae1dSRodney W. Grimes 	return (0);
283df8bae1dSRodney W. Grimes 
284df8bae1dSRodney W. Grimes bad:
2852a31267eSMatthew Dillon 	if (um) {
2862a31267eSMatthew Dillon 		if (um->um_uppervp)
2872a31267eSMatthew Dillon 			vrele(um->um_uppervp);
2882a31267eSMatthew Dillon 		if (um->um_lowervp)
2892a31267eSMatthew Dillon 			vrele(um->um_lowervp);
2902a31267eSMatthew Dillon 		/* XXX other fields */
291a1c995b6SPoul-Henning Kamp 		free(um, M_UNIONFSMNT);
2922a31267eSMatthew Dillon 	}
293df8bae1dSRodney W. Grimes 	if (cred)
294df8bae1dSRodney W. Grimes 		crfree(cred);
295df8bae1dSRodney W. Grimes 	if (upperrootvp)
296df8bae1dSRodney W. Grimes 		vrele(upperrootvp);
297df8bae1dSRodney W. Grimes 	if (lowerrootvp)
298df8bae1dSRodney W. Grimes 		vrele(lowerrootvp);
299df8bae1dSRodney W. Grimes 	return (error);
300df8bae1dSRodney W. Grimes }
301df8bae1dSRodney W. Grimes 
302df8bae1dSRodney W. Grimes /*
303df8bae1dSRodney W. Grimes  * Free reference to union layer
304df8bae1dSRodney W. Grimes  */
30580b301c3SPoul-Henning Kamp static int
306df8bae1dSRodney W. Grimes union_unmount(mp, mntflags, p)
307df8bae1dSRodney W. Grimes 	struct mount *mp;
308df8bae1dSRodney W. Grimes 	int mntflags;
309df8bae1dSRodney W. Grimes 	struct proc *p;
310df8bae1dSRodney W. Grimes {
311df8bae1dSRodney W. Grimes 	struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
312df8bae1dSRodney W. Grimes 	int error;
313996c772fSJohn Dyson 	int freeing;
314df8bae1dSRodney W. Grimes 	int flags = 0;
315df8bae1dSRodney W. Grimes 
3162a31267eSMatthew Dillon 	UDEBUG(("union_unmount(mp = %p)\n", (void *)mp));
317df8bae1dSRodney W. Grimes 
318996c772fSJohn Dyson 	if (mntflags & MNT_FORCE)
319996c772fSJohn Dyson 		flags |= FORCECLOSE;
320996c772fSJohn Dyson 
321996c772fSJohn Dyson 	/*
322996c772fSJohn Dyson 	 * Keep flushing vnodes from the mount list.
323996c772fSJohn Dyson 	 * This is needed because of the un_pvp held
324996c772fSJohn Dyson 	 * reference to the parent vnode.
325996c772fSJohn Dyson 	 * If more vnodes have been freed on a given pass,
326996c772fSJohn Dyson 	 * the try again.  The loop will iterate at most
327996c772fSJohn Dyson 	 * (d) times, where (d) is the maximum tree depth
328996c772fSJohn Dyson 	 * in the filesystem.
329996c772fSJohn Dyson 	 */
3300864ef1eSIan Dowse 	for (freeing = 0; (error = vflush(mp, 0, flags)) != 0;) {
331996c772fSJohn Dyson 		struct vnode *vp;
332996c772fSJohn Dyson 		int n;
333996c772fSJohn Dyson 
334996c772fSJohn Dyson 		/* count #vnodes held on mount list */
335805d90f7SJohn Baldwin 		mtx_lock(&mntvnode_mtx);
3360864ef1eSIan Dowse 		n = 0;
3370864ef1eSIan Dowse 		LIST_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes)
338996c772fSJohn Dyson 			n++;
339805d90f7SJohn Baldwin 		mtx_unlock(&mntvnode_mtx);
340996c772fSJohn Dyson 
341996c772fSJohn Dyson 		/* if this is unchanged then stop */
342996c772fSJohn Dyson 		if (n == freeing)
343996c772fSJohn Dyson 			break;
344996c772fSJohn Dyson 
345996c772fSJohn Dyson 		/* otherwise try once more time */
346996c772fSJohn Dyson 		freeing = n;
347df8bae1dSRodney W. Grimes 	}
348df8bae1dSRodney W. Grimes 
3490864ef1eSIan Dowse 	/* If the most recent vflush failed, the filesystem is still busy. */
3500864ef1eSIan Dowse 	if (error)
3510864ef1eSIan Dowse 		return (error);
352df8bae1dSRodney W. Grimes 
353df8bae1dSRodney W. Grimes 	/*
354df8bae1dSRodney W. Grimes 	 * Discard references to upper and lower target vnodes.
355df8bae1dSRodney W. Grimes 	 */
356df8bae1dSRodney W. Grimes 	if (um->um_lowervp)
357df8bae1dSRodney W. Grimes 		vrele(um->um_lowervp);
358df8bae1dSRodney W. Grimes 	vrele(um->um_uppervp);
359df8bae1dSRodney W. Grimes 	crfree(um->um_cred);
360df8bae1dSRodney W. Grimes 	/*
361df8bae1dSRodney W. Grimes 	 * Finally, throw away the union_mount structure
362df8bae1dSRodney W. Grimes 	 */
363a1c995b6SPoul-Henning Kamp 	free(mp->mnt_data, M_UNIONFSMNT);	/* XXX */
364df8bae1dSRodney W. Grimes 	mp->mnt_data = 0;
365df8bae1dSRodney W. Grimes 	return (0);
366df8bae1dSRodney W. Grimes }
367df8bae1dSRodney W. Grimes 
36880b301c3SPoul-Henning Kamp static int
369df8bae1dSRodney W. Grimes union_root(mp, vpp)
370df8bae1dSRodney W. Grimes 	struct mount *mp;
371df8bae1dSRodney W. Grimes 	struct vnode **vpp;
372df8bae1dSRodney W. Grimes {
373df8bae1dSRodney W. Grimes 	struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
374df8bae1dSRodney W. Grimes 	int error;
375df8bae1dSRodney W. Grimes 
376df8bae1dSRodney W. Grimes 	/*
3772a31267eSMatthew Dillon 	 * Supply an unlocked reference to um_uppervp and to um_lowervp.  It
3782a31267eSMatthew Dillon 	 * is possible for um_uppervp to be locked without the associated
3792a31267eSMatthew Dillon 	 * root union_node being locked.  We let union_allocvp() deal with
3802a31267eSMatthew Dillon 	 * it.
381df8bae1dSRodney W. Grimes 	 */
3826bdfe06aSEivind Eklund 	UDEBUG(("union_root UPPERVP %p locked = %d\n", um->um_uppervp,
3836bdfe06aSEivind Eklund 	    VOP_ISLOCKED(um->um_uppervp, NULL)));
3842a31267eSMatthew Dillon 
385df8bae1dSRodney W. Grimes 	VREF(um->um_uppervp);
386df8bae1dSRodney W. Grimes 	if (um->um_lowervp)
387df8bae1dSRodney W. Grimes 		VREF(um->um_lowervp);
388df8bae1dSRodney W. Grimes 
3892a31267eSMatthew Dillon 	error = union_allocvp(vpp, mp, NULLVP, NULLVP, NULL,
3902a31267eSMatthew Dillon 		    um->um_uppervp, um->um_lowervp, 1);
3912a31267eSMatthew Dillon 	UDEBUG(("error %d\n", error));
3926bdfe06aSEivind Eklund 	UDEBUG(("union_root2 UPPERVP %p locked = %d\n", um->um_uppervp,
3936bdfe06aSEivind Eklund 	    VOP_ISLOCKED(um->um_uppervp, NULL)));
394df8bae1dSRodney W. Grimes 
395df8bae1dSRodney W. Grimes 	return (error);
396df8bae1dSRodney W. Grimes }
397df8bae1dSRodney W. Grimes 
39880b301c3SPoul-Henning Kamp static int
399df8bae1dSRodney W. Grimes union_statfs(mp, sbp, p)
400df8bae1dSRodney W. Grimes 	struct mount *mp;
401df8bae1dSRodney W. Grimes 	struct statfs *sbp;
402df8bae1dSRodney W. Grimes 	struct proc *p;
403df8bae1dSRodney W. Grimes {
404df8bae1dSRodney W. Grimes 	int error;
405df8bae1dSRodney W. Grimes 	struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
406df8bae1dSRodney W. Grimes 	struct statfs mstat;
407df8bae1dSRodney W. Grimes 	int lbsize;
408df8bae1dSRodney W. Grimes 
4092a31267eSMatthew Dillon 	UDEBUG(("union_statfs(mp = %p, lvp = %p, uvp = %p)\n",
4102a31267eSMatthew Dillon 	    (void *)mp, (void *)um->um_lowervp, (void *)um->um_uppervp));
411df8bae1dSRodney W. Grimes 
412df8bae1dSRodney W. Grimes 	bzero(&mstat, sizeof(mstat));
413df8bae1dSRodney W. Grimes 
414df8bae1dSRodney W. Grimes 	if (um->um_lowervp) {
415df8bae1dSRodney W. Grimes 		error = VFS_STATFS(um->um_lowervp->v_mount, &mstat, p);
416df8bae1dSRodney W. Grimes 		if (error)
417df8bae1dSRodney W. Grimes 			return (error);
418df8bae1dSRodney W. Grimes 	}
419df8bae1dSRodney W. Grimes 
420df8bae1dSRodney W. Grimes 	/* now copy across the "interesting" information and fake the rest */
421df8bae1dSRodney W. Grimes #if 0
422df8bae1dSRodney W. Grimes 	sbp->f_type = mstat.f_type;
423df8bae1dSRodney W. Grimes 	sbp->f_flags = mstat.f_flags;
424df8bae1dSRodney W. Grimes 	sbp->f_bsize = mstat.f_bsize;
425df8bae1dSRodney W. Grimes 	sbp->f_iosize = mstat.f_iosize;
426df8bae1dSRodney W. Grimes #endif
427df8bae1dSRodney W. Grimes 	lbsize = mstat.f_bsize;
428df8bae1dSRodney W. Grimes 	sbp->f_blocks = mstat.f_blocks;
429df8bae1dSRodney W. Grimes 	sbp->f_bfree = mstat.f_bfree;
430df8bae1dSRodney W. Grimes 	sbp->f_bavail = mstat.f_bavail;
431df8bae1dSRodney W. Grimes 	sbp->f_files = mstat.f_files;
432df8bae1dSRodney W. Grimes 	sbp->f_ffree = mstat.f_ffree;
433df8bae1dSRodney W. Grimes 
434df8bae1dSRodney W. Grimes 	error = VFS_STATFS(um->um_uppervp->v_mount, &mstat, p);
435df8bae1dSRodney W. Grimes 	if (error)
436df8bae1dSRodney W. Grimes 		return (error);
437df8bae1dSRodney W. Grimes 
438df8bae1dSRodney W. Grimes 	sbp->f_flags = mstat.f_flags;
439df8bae1dSRodney W. Grimes 	sbp->f_bsize = mstat.f_bsize;
440df8bae1dSRodney W. Grimes 	sbp->f_iosize = mstat.f_iosize;
441df8bae1dSRodney W. Grimes 
442df8bae1dSRodney W. Grimes 	/*
443df8bae1dSRodney W. Grimes 	 * if the lower and upper blocksizes differ, then frig the
444df8bae1dSRodney W. Grimes 	 * block counts so that the sizes reported by df make some
445df8bae1dSRodney W. Grimes 	 * kind of sense.  none of this makes sense though.
446df8bae1dSRodney W. Grimes 	 */
447df8bae1dSRodney W. Grimes 
448996c772fSJohn Dyson 	if (mstat.f_bsize != lbsize)
449c9bf0111SKATO Takenori 		sbp->f_blocks = ((off_t) sbp->f_blocks * lbsize) / mstat.f_bsize;
450996c772fSJohn Dyson 
451996c772fSJohn Dyson 	/*
452996c772fSJohn Dyson 	 * The "total" fields count total resources in all layers,
453996c772fSJohn Dyson 	 * the "free" fields count only those resources which are
454996c772fSJohn Dyson 	 * free in the upper layer (since only the upper layer
455996c772fSJohn Dyson 	 * is writeable).
456996c772fSJohn Dyson 	 */
457df8bae1dSRodney W. Grimes 	sbp->f_blocks += mstat.f_blocks;
458996c772fSJohn Dyson 	sbp->f_bfree = mstat.f_bfree;
459996c772fSJohn Dyson 	sbp->f_bavail = mstat.f_bavail;
460df8bae1dSRodney W. Grimes 	sbp->f_files += mstat.f_files;
461996c772fSJohn Dyson 	sbp->f_ffree = mstat.f_ffree;
462df8bae1dSRodney W. Grimes 
463df8bae1dSRodney W. Grimes 	if (sbp != &mp->mnt_stat) {
464996c772fSJohn Dyson 		sbp->f_type = mp->mnt_vfc->vfc_typenum;
465df8bae1dSRodney W. Grimes 		bcopy(&mp->mnt_stat.f_fsid, &sbp->f_fsid, sizeof(sbp->f_fsid));
466df8bae1dSRodney W. Grimes 		bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
467df8bae1dSRodney W. Grimes 		bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
468df8bae1dSRodney W. Grimes 	}
469df8bae1dSRodney W. Grimes 	return (0);
470df8bae1dSRodney W. Grimes }
471df8bae1dSRodney W. Grimes 
47280b301c3SPoul-Henning Kamp static struct vfsops union_vfsops = {
473df8bae1dSRodney W. Grimes 	union_mount,
474c24fda81SAlfred Perlstein 	vfs_stdstart,	/* underlying start already done */
475df8bae1dSRodney W. Grimes 	union_unmount,
476df8bae1dSRodney W. Grimes 	union_root,
4775a5fccc8SAlfred Perlstein 	vfs_stdquotactl,
478df8bae1dSRodney W. Grimes 	union_statfs,
4795a5fccc8SAlfred Perlstein 	vfs_stdsync,    /* XXX assumes no cached data on union level */
4805a5fccc8SAlfred Perlstein 	vfs_stdvget,
4815a5fccc8SAlfred Perlstein 	vfs_stdfhtovp,
482c24fda81SAlfred Perlstein 	vfs_stdcheckexp,
4835a5fccc8SAlfred Perlstein 	vfs_stdvptofh,
484df8bae1dSRodney W. Grimes 	union_init,
48591f37dcbSRobert Watson 	vfs_stduninit,
48691f37dcbSRobert Watson 	vfs_stdextattrctl,
487df8bae1dSRodney W. Grimes };
488c901836cSGarrett Wollman 
489c7b23e0fSRuslan Ermilov VFS_SET(union_vfsops, unionfs, VFCF_LOOPBACK);
490