xref: /freebsd/sys/fs/unionfs/union_vfsops.c (revision 1c4ccf09cd3231080e865aa41696cb726169f88f)
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 
6011caded3SAlfred Perlstein extern int	union_init(struct vfsconf *);
61a9f5c04aSMaxime Henrion static int	union_mount(struct mount *mp, struct nameidata *ndp,
62a9f5c04aSMaxime Henrion 				  struct thread *td);
6311caded3SAlfred Perlstein static int	union_root(struct mount *mp, struct vnode **vpp);
6411caded3SAlfred Perlstein static int	union_statfs(struct mount *mp, struct statfs *sbp,
6511caded3SAlfred Perlstein 				  struct thread *td);
6611caded3SAlfred Perlstein static int	union_unmount(struct mount *mp, int mntflags,
6711caded3SAlfred Perlstein 				   struct thread *td);
68b5e8ce9fSBruce Evans 
69df8bae1dSRodney W. Grimes /*
701c4ccf09SDon Lewis  * Mount union filesystem.
71df8bae1dSRodney W. Grimes  */
7280b301c3SPoul-Henning Kamp static int
73a9f5c04aSMaxime Henrion union_mount(mp, ndp, td)
74df8bae1dSRodney W. Grimes 	struct mount *mp;
75df8bae1dSRodney W. Grimes 	struct nameidata *ndp;
76b40ce416SJulian Elischer 	struct thread *td;
77df8bae1dSRodney W. Grimes {
78df8bae1dSRodney W. Grimes 	int error = 0;
79a9f5c04aSMaxime Henrion 	struct vfsoptlist *opts;
80df8bae1dSRodney W. Grimes 	struct vnode *lowerrootvp = NULLVP;
81df8bae1dSRodney W. Grimes 	struct vnode *upperrootvp = NULLVP;
82996c772fSJohn Dyson 	struct union_mount *um = 0;
83df8bae1dSRodney W. Grimes 	struct ucred *cred = 0;
84a9f5c04aSMaxime Henrion 	char *cp = 0, *target;
854ce86ffdSMaxime Henrion 	int op;
86df8bae1dSRodney W. Grimes 	int len;
87df8bae1dSRodney W. Grimes 	u_int size;
88df8bae1dSRodney W. Grimes 
892a31267eSMatthew Dillon 	UDEBUG(("union_mount(mp = %p)\n", (void *)mp));
90df8bae1dSRodney W. Grimes 
91a9f5c04aSMaxime Henrion 	opts = mp->mnt_optnew;
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 	 */
100a9f5c04aSMaxime Henrion 	if (mp->mnt_flag & MNT_UPDATE)
101df8bae1dSRodney W. Grimes 		/*
1021c4ccf09SDon Lewis 		 * 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 		 */
106a9f5c04aSMaxime Henrion 		return (EOPNOTSUPP);
107df8bae1dSRodney W. Grimes 
108df8bae1dSRodney W. Grimes 	/*
109a9f5c04aSMaxime Henrion 	 * Get arguments.
110df8bae1dSRodney W. Grimes 	 */
111a9f5c04aSMaxime Henrion 	error = vfs_getopt(opts, "target", (void **)&target, &len);
112a9f5c04aSMaxime Henrion 	if (error || target[len - 1] != '\0')
113a9f5c04aSMaxime Henrion 		return (EINVAL);
114a9f5c04aSMaxime Henrion 
1154ce86ffdSMaxime Henrion 	op = 0;
1164ce86ffdSMaxime Henrion 	if (vfs_getopt(opts, "below", NULL, NULL) == 0)
1174ce86ffdSMaxime Henrion 		op = UNMNT_BELOW;
1184ce86ffdSMaxime Henrion 	if (vfs_getopt(opts, "replace", NULL, NULL) == 0) {
1194ce86ffdSMaxime Henrion 		/* These options are mutually exclusive. */
1204ce86ffdSMaxime Henrion 		if (op)
121a9f5c04aSMaxime Henrion 			return (EINVAL);
1224ce86ffdSMaxime Henrion 		op = UNMNT_REPLACE;
1234ce86ffdSMaxime Henrion 	}
1244ce86ffdSMaxime Henrion 	/*
1254ce86ffdSMaxime Henrion 	 * UNMNT_ABOVE is the default.
1264ce86ffdSMaxime Henrion 	 */
1274ce86ffdSMaxime Henrion 	if (op == 0)
1284ce86ffdSMaxime Henrion 		op = UNMNT_ABOVE;
129df8bae1dSRodney W. Grimes 
1302a31267eSMatthew Dillon 	/*
1312a31267eSMatthew Dillon 	 * Obtain lower vnode.  Vnode is stored in mp->mnt_vnodecovered.
1322a31267eSMatthew Dillon 	 * We need to reference it but not lock it.
1332a31267eSMatthew Dillon 	 */
1342a31267eSMatthew Dillon 
135df8bae1dSRodney W. Grimes 	lowerrootvp = mp->mnt_vnodecovered;
136df8bae1dSRodney W. Grimes 	VREF(lowerrootvp);
137df8bae1dSRodney W. Grimes 
1382a31267eSMatthew Dillon #if 0
139df8bae1dSRodney W. Grimes 	/*
1406db918e3SKATO Takenori 	 * Unlock lower node to avoid deadlock.
1416db918e3SKATO Takenori 	 */
142afc2a558SKATO Takenori 	if (lowerrootvp->v_op == union_vnodeop_p)
143b40ce416SJulian Elischer 		VOP_UNLOCK(lowerrootvp, 0, td);
1442a31267eSMatthew Dillon #endif
1456db918e3SKATO Takenori 
1466db918e3SKATO Takenori 	/*
1472a31267eSMatthew Dillon 	 * Obtain upper vnode by calling namei() on the path.  The
1482a31267eSMatthew Dillon 	 * upperrootvp will be turned referenced but not locked.
149df8bae1dSRodney W. Grimes 	 */
150a9f5c04aSMaxime Henrion 	NDINIT(ndp, LOOKUP, FOLLOW|WANTPARENT, UIO_SYSSPACE, target, td);
151df8bae1dSRodney W. Grimes 
1523a773ad0SPoul-Henning Kamp 	error = namei(ndp);
1532a31267eSMatthew Dillon 
1542a31267eSMatthew Dillon #if 0
155afc2a558SKATO Takenori 	if (lowerrootvp->v_op == union_vnodeop_p)
156b40ce416SJulian Elischer 		vn_lock(lowerrootvp, LK_EXCLUSIVE | LK_RETRY, td);
1572a31267eSMatthew Dillon #endif
1583a773ad0SPoul-Henning Kamp 	if (error)
159df8bae1dSRodney W. Grimes 		goto bad;
160df8bae1dSRodney W. Grimes 
161762e6b85SEivind Eklund 	NDFREE(ndp, NDF_ONLY_PNBUF);
162df8bae1dSRodney W. Grimes 	upperrootvp = ndp->ni_vp;
163df8bae1dSRodney W. Grimes 	vrele(ndp->ni_dvp);
164df8bae1dSRodney W. Grimes 	ndp->ni_dvp = NULL;
165df8bae1dSRodney W. Grimes 
1666bdfe06aSEivind Eklund 	UDEBUG(("mount_root UPPERVP %p locked = %d\n", upperrootvp,
1676bdfe06aSEivind Eklund 	    VOP_ISLOCKED(upperrootvp, NULL)));
1682a31267eSMatthew Dillon 
1696db918e3SKATO Takenori 	/*
1706db918e3SKATO Takenori 	 * Check multi union mount to avoid `lock myself again' panic.
1712a31267eSMatthew Dillon 	 * Also require that it be a directory.
1726db918e3SKATO Takenori 	 */
1736db918e3SKATO Takenori 	if (upperrootvp == VTOUNION(lowerrootvp)->un_uppervp) {
174747e9157SKATO Takenori #ifdef DIAGNOSTIC
175747e9157SKATO Takenori 		printf("union_mount: multi union mount?\n");
176747e9157SKATO Takenori #endif
1776db918e3SKATO Takenori 		error = EDEADLK;
1786db918e3SKATO Takenori 		goto bad;
1796db918e3SKATO Takenori 	}
1806db918e3SKATO Takenori 
181df8bae1dSRodney W. Grimes 	if (upperrootvp->v_type != VDIR) {
182df8bae1dSRodney W. Grimes 		error = EINVAL;
183df8bae1dSRodney W. Grimes 		goto bad;
184df8bae1dSRodney W. Grimes 	}
185df8bae1dSRodney W. Grimes 
186df8bae1dSRodney W. Grimes 	/*
1872a31267eSMatthew Dillon 	 * Allocate our union_mount structure and populate the fields.
1882a31267eSMatthew Dillon 	 * The vnode references are stored in the union_mount as held,
1892a31267eSMatthew Dillon 	 * unlocked references.  Depending on the _BELOW flag, the
1902a31267eSMatthew Dillon 	 * filesystems are viewed in a different order.  In effect this
1912a31267eSMatthew Dillon 	 * is the same as providing a mount-under option to the mount
1922a31267eSMatthew Dillon 	 * syscall.
193df8bae1dSRodney W. Grimes 	 */
194df8bae1dSRodney W. Grimes 
1952a31267eSMatthew Dillon 	um = (struct union_mount *) malloc(sizeof(struct union_mount),
1967cc0979fSDavid Malone 				M_UNIONFSMNT, M_WAITOK | M_ZERO);
1972a31267eSMatthew Dillon 
1984ce86ffdSMaxime Henrion 	um->um_op = op;
1992a31267eSMatthew Dillon 
200df8bae1dSRodney W. Grimes 	switch (um->um_op) {
201df8bae1dSRodney W. Grimes 	case UNMNT_ABOVE:
202df8bae1dSRodney W. Grimes 		um->um_lowervp = lowerrootvp;
203df8bae1dSRodney W. Grimes 		um->um_uppervp = upperrootvp;
2042a31267eSMatthew Dillon 		upperrootvp = NULL;
2052a31267eSMatthew Dillon 		lowerrootvp = NULL;
206df8bae1dSRodney W. Grimes 		break;
207df8bae1dSRodney W. Grimes 
208df8bae1dSRodney W. Grimes 	case UNMNT_BELOW:
209df8bae1dSRodney W. Grimes 		um->um_lowervp = upperrootvp;
210df8bae1dSRodney W. Grimes 		um->um_uppervp = lowerrootvp;
2112a31267eSMatthew Dillon 		upperrootvp = NULL;
2122a31267eSMatthew Dillon 		lowerrootvp = NULL;
213df8bae1dSRodney W. Grimes 		break;
214df8bae1dSRodney W. Grimes 
215df8bae1dSRodney W. Grimes 	case UNMNT_REPLACE:
216df8bae1dSRodney W. Grimes 		vrele(lowerrootvp);
2172a31267eSMatthew Dillon 		lowerrootvp = NULL;
218df8bae1dSRodney W. Grimes 		um->um_uppervp = upperrootvp;
219df8bae1dSRodney W. Grimes 		um->um_lowervp = lowerrootvp;
2202a31267eSMatthew Dillon 		upperrootvp = NULL;
221df8bae1dSRodney W. Grimes 		break;
222df8bae1dSRodney W. Grimes 
223df8bae1dSRodney W. Grimes 	default:
224df8bae1dSRodney W. Grimes 		error = EINVAL;
225df8bae1dSRodney W. Grimes 		goto bad;
226df8bae1dSRodney W. Grimes 	}
227df8bae1dSRodney W. Grimes 
228996c772fSJohn Dyson 	/*
229996c772fSJohn Dyson 	 * Unless the mount is readonly, ensure that the top layer
2301c4ccf09SDon Lewis 	 * supports whiteout operations.
231996c772fSJohn Dyson 	 */
232996c772fSJohn Dyson 	if ((mp->mnt_flag & MNT_RDONLY) == 0) {
2332a31267eSMatthew Dillon 		error = VOP_WHITEOUT(um->um_uppervp, NULL, LOOKUP);
234996c772fSJohn Dyson 		if (error)
235996c772fSJohn Dyson 			goto bad;
236996c772fSJohn Dyson 	}
237996c772fSJohn Dyson 
238a854ed98SJohn Baldwin 	um->um_cred = crhold(td->td_ucred);
239426da3bcSAlfred Perlstein 	FILEDESC_LOCK(td->td_proc->p_fd);
240b40ce416SJulian Elischer 	um->um_cmode = UN_DIRMODE &~ td->td_proc->p_fd->fd_cmask;
241426da3bcSAlfred Perlstein 	FILEDESC_UNLOCK(td->td_proc->p_fd);
242df8bae1dSRodney W. Grimes 
243df8bae1dSRodney W. Grimes 	/*
244df8bae1dSRodney W. Grimes 	 * Depending on what you think the MNT_LOCAL flag might mean,
245df8bae1dSRodney W. Grimes 	 * you may want the && to be || on the conditional below.
246df8bae1dSRodney W. Grimes 	 * At the moment it has been defined that the filesystem is
247df8bae1dSRodney W. Grimes 	 * only local if it is all local, ie the MNT_LOCAL flag implies
248df8bae1dSRodney W. Grimes 	 * that the entire namespace is local.  If you think the MNT_LOCAL
249df8bae1dSRodney W. Grimes 	 * flag implies that some of the files might be stored locally
250df8bae1dSRodney W. Grimes 	 * then you will want to change the conditional.
251df8bae1dSRodney W. Grimes 	 */
252df8bae1dSRodney W. Grimes 	if (um->um_op == UNMNT_ABOVE) {
253df8bae1dSRodney W. Grimes 		if (((um->um_lowervp == NULLVP) ||
254df8bae1dSRodney W. Grimes 		     (um->um_lowervp->v_mount->mnt_flag & MNT_LOCAL)) &&
255df8bae1dSRodney W. Grimes 		    (um->um_uppervp->v_mount->mnt_flag & MNT_LOCAL))
256df8bae1dSRodney W. Grimes 			mp->mnt_flag |= MNT_LOCAL;
257df8bae1dSRodney W. Grimes 	}
258df8bae1dSRodney W. Grimes 
259df8bae1dSRodney W. Grimes 	/*
260df8bae1dSRodney W. Grimes 	 * Copy in the upper layer's RDONLY flag.  This is for the benefit
261df8bae1dSRodney W. Grimes 	 * of lookup() which explicitly checks the flag, rather than asking
262dc733423SDag-Erling Smørgrav 	 * the filesystem for its own opinion.  This means, that an update
263df8bae1dSRodney W. Grimes 	 * mount of the underlying filesystem to go from rdonly to rdwr
264df8bae1dSRodney W. Grimes 	 * will leave the unioned view as read-only.
265df8bae1dSRodney W. Grimes 	 */
266df8bae1dSRodney W. Grimes 	mp->mnt_flag |= (um->um_uppervp->v_mount->mnt_flag & MNT_RDONLY);
267df8bae1dSRodney W. Grimes 
268df8bae1dSRodney W. Grimes 	mp->mnt_data = (qaddr_t) um;
269996c772fSJohn Dyson 	vfs_getnewfsid(mp);
270df8bae1dSRodney W. Grimes 
271df8bae1dSRodney W. Grimes 	switch (um->um_op) {
272df8bae1dSRodney W. Grimes 	case UNMNT_ABOVE:
273996c772fSJohn Dyson 		cp = "<above>:";
274df8bae1dSRodney W. Grimes 		break;
275df8bae1dSRodney W. Grimes 	case UNMNT_BELOW:
276996c772fSJohn Dyson 		cp = "<below>:";
277df8bae1dSRodney W. Grimes 		break;
278df8bae1dSRodney W. Grimes 	case UNMNT_REPLACE:
279df8bae1dSRodney W. Grimes 		cp = "";
280df8bae1dSRodney W. Grimes 		break;
281df8bae1dSRodney W. Grimes 	}
282df8bae1dSRodney W. Grimes 	len = strlen(cp);
283df8bae1dSRodney W. Grimes 	bcopy(cp, mp->mnt_stat.f_mntfromname, len);
284df8bae1dSRodney W. Grimes 
285df8bae1dSRodney W. Grimes 	cp = mp->mnt_stat.f_mntfromname + len;
286df8bae1dSRodney W. Grimes 	len = MNAMELEN - len;
287df8bae1dSRodney W. Grimes 
288a9f5c04aSMaxime Henrion 	(void) copystr(target, cp, len - 1, &size);
289df8bae1dSRodney W. Grimes 	bzero(cp + size, len - size);
290df8bae1dSRodney W. Grimes 
291b40ce416SJulian Elischer 	(void)union_statfs(mp, &mp->mnt_stat, td);
292c4a7b7e1SDavid Greenman 
2932a31267eSMatthew Dillon 	UDEBUG(("union_mount: from %s, on %s\n",
2942a31267eSMatthew Dillon 		mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname));
295df8bae1dSRodney W. Grimes 	return (0);
296df8bae1dSRodney W. Grimes 
297df8bae1dSRodney W. Grimes bad:
2982a31267eSMatthew Dillon 	if (um) {
2992a31267eSMatthew Dillon 		if (um->um_uppervp)
3002a31267eSMatthew Dillon 			vrele(um->um_uppervp);
3012a31267eSMatthew Dillon 		if (um->um_lowervp)
3022a31267eSMatthew Dillon 			vrele(um->um_lowervp);
3032a31267eSMatthew Dillon 		/* XXX other fields */
304a1c995b6SPoul-Henning Kamp 		free(um, M_UNIONFSMNT);
3052a31267eSMatthew Dillon 	}
306df8bae1dSRodney W. Grimes 	if (cred)
307df8bae1dSRodney W. Grimes 		crfree(cred);
308df8bae1dSRodney W. Grimes 	if (upperrootvp)
309df8bae1dSRodney W. Grimes 		vrele(upperrootvp);
310df8bae1dSRodney W. Grimes 	if (lowerrootvp)
311df8bae1dSRodney W. Grimes 		vrele(lowerrootvp);
312df8bae1dSRodney W. Grimes 	return (error);
313df8bae1dSRodney W. Grimes }
314df8bae1dSRodney W. Grimes 
315df8bae1dSRodney W. Grimes /*
3161c4ccf09SDon Lewis  * Free reference to union layer.
317df8bae1dSRodney W. Grimes  */
31880b301c3SPoul-Henning Kamp static int
319b40ce416SJulian Elischer union_unmount(mp, mntflags, td)
320df8bae1dSRodney W. Grimes 	struct mount *mp;
321df8bae1dSRodney W. Grimes 	int mntflags;
322b40ce416SJulian Elischer 	struct thread *td;
323df8bae1dSRodney W. Grimes {
324df8bae1dSRodney W. Grimes 	struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
325df8bae1dSRodney W. Grimes 	int error;
326996c772fSJohn Dyson 	int freeing;
327df8bae1dSRodney W. Grimes 	int flags = 0;
328df8bae1dSRodney W. Grimes 
3292a31267eSMatthew Dillon 	UDEBUG(("union_unmount(mp = %p)\n", (void *)mp));
330df8bae1dSRodney W. Grimes 
331996c772fSJohn Dyson 	if (mntflags & MNT_FORCE)
332996c772fSJohn Dyson 		flags |= FORCECLOSE;
333996c772fSJohn Dyson 
334996c772fSJohn Dyson 	/*
335996c772fSJohn Dyson 	 * Keep flushing vnodes from the mount list.
336996c772fSJohn Dyson 	 * This is needed because of the un_pvp held
337996c772fSJohn Dyson 	 * reference to the parent vnode.
338996c772fSJohn Dyson 	 * If more vnodes have been freed on a given pass,
339996c772fSJohn Dyson 	 * the try again.  The loop will iterate at most
340996c772fSJohn Dyson 	 * (d) times, where (d) is the maximum tree depth
341996c772fSJohn Dyson 	 * in the filesystem.
342996c772fSJohn Dyson 	 */
3430864ef1eSIan Dowse 	for (freeing = 0; (error = vflush(mp, 0, flags)) != 0;) {
344996c772fSJohn Dyson 		struct vnode *vp;
345996c772fSJohn Dyson 		int n;
346996c772fSJohn Dyson 
347996c772fSJohn Dyson 		/* count #vnodes held on mount list */
348805d90f7SJohn Baldwin 		mtx_lock(&mntvnode_mtx);
3490864ef1eSIan Dowse 		n = 0;
350c72ccd01SMatthew Dillon 		TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes)
351996c772fSJohn Dyson 			n++;
352805d90f7SJohn Baldwin 		mtx_unlock(&mntvnode_mtx);
353996c772fSJohn Dyson 
354996c772fSJohn Dyson 		/* if this is unchanged then stop */
355996c772fSJohn Dyson 		if (n == freeing)
356996c772fSJohn Dyson 			break;
357996c772fSJohn Dyson 
358996c772fSJohn Dyson 		/* otherwise try once more time */
359996c772fSJohn Dyson 		freeing = n;
360df8bae1dSRodney W. Grimes 	}
361df8bae1dSRodney W. Grimes 
3621c4ccf09SDon Lewis 	/*
3631c4ccf09SDon Lewis 	 * If the most recent vflush failed, the filesystem is still busy.
3641c4ccf09SDon Lewis 	 */
3650864ef1eSIan Dowse 	if (error)
3660864ef1eSIan Dowse 		return (error);
367df8bae1dSRodney W. Grimes 
368df8bae1dSRodney W. Grimes 	/*
369df8bae1dSRodney W. Grimes 	 * Discard references to upper and lower target vnodes.
370df8bae1dSRodney W. Grimes 	 */
371df8bae1dSRodney W. Grimes 	if (um->um_lowervp)
372df8bae1dSRodney W. Grimes 		vrele(um->um_lowervp);
373df8bae1dSRodney W. Grimes 	vrele(um->um_uppervp);
374df8bae1dSRodney W. Grimes 	crfree(um->um_cred);
375df8bae1dSRodney W. Grimes 	/*
3761c4ccf09SDon Lewis 	 * Finally, throw away the union_mount structure.
377df8bae1dSRodney W. Grimes 	 */
378a1c995b6SPoul-Henning Kamp 	free(mp->mnt_data, M_UNIONFSMNT);	/* XXX */
379df8bae1dSRodney W. Grimes 	mp->mnt_data = 0;
380df8bae1dSRodney W. Grimes 	return (0);
381df8bae1dSRodney W. Grimes }
382df8bae1dSRodney W. Grimes 
38380b301c3SPoul-Henning Kamp static int
384df8bae1dSRodney W. Grimes union_root(mp, vpp)
385df8bae1dSRodney W. Grimes 	struct mount *mp;
386df8bae1dSRodney W. Grimes 	struct vnode **vpp;
387df8bae1dSRodney W. Grimes {
388df8bae1dSRodney W. Grimes 	struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
389df8bae1dSRodney W. Grimes 	int error;
390df8bae1dSRodney W. Grimes 
391df8bae1dSRodney W. Grimes 	/*
3922a31267eSMatthew Dillon 	 * Supply an unlocked reference to um_uppervp and to um_lowervp.  It
3932a31267eSMatthew Dillon 	 * is possible for um_uppervp to be locked without the associated
3942a31267eSMatthew Dillon 	 * root union_node being locked.  We let union_allocvp() deal with
3952a31267eSMatthew Dillon 	 * it.
396df8bae1dSRodney W. Grimes 	 */
3976bdfe06aSEivind Eklund 	UDEBUG(("union_root UPPERVP %p locked = %d\n", um->um_uppervp,
3986bdfe06aSEivind Eklund 	    VOP_ISLOCKED(um->um_uppervp, NULL)));
3992a31267eSMatthew Dillon 
400df8bae1dSRodney W. Grimes 	VREF(um->um_uppervp);
401df8bae1dSRodney W. Grimes 	if (um->um_lowervp)
402df8bae1dSRodney W. Grimes 		VREF(um->um_lowervp);
403df8bae1dSRodney W. Grimes 
4042a31267eSMatthew Dillon 	error = union_allocvp(vpp, mp, NULLVP, NULLVP, NULL,
4052a31267eSMatthew Dillon 		    um->um_uppervp, um->um_lowervp, 1);
4062a31267eSMatthew Dillon 	UDEBUG(("error %d\n", error));
4076bdfe06aSEivind Eklund 	UDEBUG(("union_root2 UPPERVP %p locked = %d\n", um->um_uppervp,
4086bdfe06aSEivind Eklund 	    VOP_ISLOCKED(um->um_uppervp, NULL)));
409df8bae1dSRodney W. Grimes 
410df8bae1dSRodney W. Grimes 	return (error);
411df8bae1dSRodney W. Grimes }
412df8bae1dSRodney W. Grimes 
41380b301c3SPoul-Henning Kamp static int
414b40ce416SJulian Elischer union_statfs(mp, sbp, td)
415df8bae1dSRodney W. Grimes 	struct mount *mp;
416df8bae1dSRodney W. Grimes 	struct statfs *sbp;
417b40ce416SJulian Elischer 	struct thread *td;
418df8bae1dSRodney W. Grimes {
419df8bae1dSRodney W. Grimes 	int error;
420df8bae1dSRodney W. Grimes 	struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
421df8bae1dSRodney W. Grimes 	struct statfs mstat;
422df8bae1dSRodney W. Grimes 	int lbsize;
423df8bae1dSRodney W. Grimes 
4242a31267eSMatthew Dillon 	UDEBUG(("union_statfs(mp = %p, lvp = %p, uvp = %p)\n",
4252a31267eSMatthew Dillon 	    (void *)mp, (void *)um->um_lowervp, (void *)um->um_uppervp));
426df8bae1dSRodney W. Grimes 
427df8bae1dSRodney W. Grimes 	bzero(&mstat, sizeof(mstat));
428df8bae1dSRodney W. Grimes 
429df8bae1dSRodney W. Grimes 	if (um->um_lowervp) {
430b40ce416SJulian Elischer 		error = VFS_STATFS(um->um_lowervp->v_mount, &mstat, td);
431df8bae1dSRodney W. Grimes 		if (error)
432df8bae1dSRodney W. Grimes 			return (error);
433df8bae1dSRodney W. Grimes 	}
434df8bae1dSRodney W. Grimes 
4351c4ccf09SDon Lewis 	/*
4361c4ccf09SDon Lewis 	 * Now copy across the "interesting" information and fake the rest.
4371c4ccf09SDon Lewis 	 */
438df8bae1dSRodney W. Grimes #if 0
439df8bae1dSRodney W. Grimes 	sbp->f_type = mstat.f_type;
440df8bae1dSRodney W. Grimes 	sbp->f_flags = mstat.f_flags;
441df8bae1dSRodney W. Grimes 	sbp->f_bsize = mstat.f_bsize;
442df8bae1dSRodney W. Grimes 	sbp->f_iosize = mstat.f_iosize;
443df8bae1dSRodney W. Grimes #endif
444df8bae1dSRodney W. Grimes 	lbsize = mstat.f_bsize;
445df8bae1dSRodney W. Grimes 	sbp->f_blocks = mstat.f_blocks;
446df8bae1dSRodney W. Grimes 	sbp->f_bfree = mstat.f_bfree;
447df8bae1dSRodney W. Grimes 	sbp->f_bavail = mstat.f_bavail;
448df8bae1dSRodney W. Grimes 	sbp->f_files = mstat.f_files;
449df8bae1dSRodney W. Grimes 	sbp->f_ffree = mstat.f_ffree;
450df8bae1dSRodney W. Grimes 
451b40ce416SJulian Elischer 	error = VFS_STATFS(um->um_uppervp->v_mount, &mstat, td);
452df8bae1dSRodney W. Grimes 	if (error)
453df8bae1dSRodney W. Grimes 		return (error);
454df8bae1dSRodney W. Grimes 
455df8bae1dSRodney W. Grimes 	sbp->f_flags = mstat.f_flags;
456df8bae1dSRodney W. Grimes 	sbp->f_bsize = mstat.f_bsize;
457df8bae1dSRodney W. Grimes 	sbp->f_iosize = mstat.f_iosize;
458df8bae1dSRodney W. Grimes 
459df8bae1dSRodney W. Grimes 	/*
4601c4ccf09SDon Lewis 	 * If the lower and upper blocksizes differ, then frig the
461df8bae1dSRodney W. Grimes 	 * block counts so that the sizes reported by df make some
4621c4ccf09SDon Lewis 	 * kind of sense.  None of this makes sense though.
463df8bae1dSRodney W. Grimes 	 */
464df8bae1dSRodney W. Grimes 
465996c772fSJohn Dyson 	if (mstat.f_bsize != lbsize)
466c9bf0111SKATO Takenori 		sbp->f_blocks = ((off_t) sbp->f_blocks * lbsize) / mstat.f_bsize;
467996c772fSJohn Dyson 
468996c772fSJohn Dyson 	/*
469996c772fSJohn Dyson 	 * The "total" fields count total resources in all layers,
470996c772fSJohn Dyson 	 * the "free" fields count only those resources which are
471996c772fSJohn Dyson 	 * free in the upper layer (since only the upper layer
472996c772fSJohn Dyson 	 * is writeable).
473996c772fSJohn Dyson 	 */
474df8bae1dSRodney W. Grimes 	sbp->f_blocks += mstat.f_blocks;
475996c772fSJohn Dyson 	sbp->f_bfree = mstat.f_bfree;
476996c772fSJohn Dyson 	sbp->f_bavail = mstat.f_bavail;
477df8bae1dSRodney W. Grimes 	sbp->f_files += mstat.f_files;
478996c772fSJohn Dyson 	sbp->f_ffree = mstat.f_ffree;
479df8bae1dSRodney W. Grimes 
480df8bae1dSRodney W. Grimes 	if (sbp != &mp->mnt_stat) {
481996c772fSJohn Dyson 		sbp->f_type = mp->mnt_vfc->vfc_typenum;
482df8bae1dSRodney W. Grimes 		bcopy(&mp->mnt_stat.f_fsid, &sbp->f_fsid, sizeof(sbp->f_fsid));
483df8bae1dSRodney W. Grimes 		bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
484df8bae1dSRodney W. Grimes 		bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
485df8bae1dSRodney W. Grimes 	}
486df8bae1dSRodney W. Grimes 	return (0);
487df8bae1dSRodney W. Grimes }
488df8bae1dSRodney W. Grimes 
48980b301c3SPoul-Henning Kamp static struct vfsops union_vfsops = {
490a9f5c04aSMaxime Henrion 	NULL,
491c24fda81SAlfred Perlstein 	vfs_stdstart,	/* underlying start already done */
492df8bae1dSRodney W. Grimes 	union_unmount,
493df8bae1dSRodney W. Grimes 	union_root,
4945a5fccc8SAlfred Perlstein 	vfs_stdquotactl,
495df8bae1dSRodney W. Grimes 	union_statfs,
4965a5fccc8SAlfred Perlstein 	vfs_stdsync,    /* XXX assumes no cached data on union level */
4975a5fccc8SAlfred Perlstein 	vfs_stdvget,
4985a5fccc8SAlfred Perlstein 	vfs_stdfhtovp,
499c24fda81SAlfred Perlstein 	vfs_stdcheckexp,
5005a5fccc8SAlfred Perlstein 	vfs_stdvptofh,
501df8bae1dSRodney W. Grimes 	union_init,
50291f37dcbSRobert Watson 	vfs_stduninit,
50391f37dcbSRobert Watson 	vfs_stdextattrctl,
504a9f5c04aSMaxime Henrion 	union_mount,
505df8bae1dSRodney W. Grimes };
506c901836cSGarrett Wollman 
507c7b23e0fSRuslan Ermilov VFS_SET(union_vfsops, unionfs, VFCF_LOOPBACK);
508