xref: /freebsd/sys/fs/unionfs/union_vfsops.c (revision 2a31267e436da0fa9ba9a869356e4cbb52fe703f)
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>
48df8bae1dSRodney W. Grimes #include <sys/proc.h>
49df8bae1dSRodney W. Grimes #include <sys/vnode.h>
50df8bae1dSRodney W. Grimes #include <sys/mount.h>
51df8bae1dSRodney W. Grimes #include <sys/namei.h>
52df8bae1dSRodney W. Grimes #include <sys/malloc.h>
53df8bae1dSRodney W. Grimes #include <sys/filedesc.h>
54df8bae1dSRodney W. Grimes #include <miscfs/union/union.h>
55df8bae1dSRodney W. Grimes 
56a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_UNIONFSMNT, "UNION mount", "UNION mount structure");
57a1c995b6SPoul-Henning Kamp 
58996c772fSJohn Dyson extern int	union_init __P((struct vfsconf *));
5980b301c3SPoul-Henning Kamp static int	union_mount __P((struct mount *mp, char *path, caddr_t data,
609b5e8b3aSBruce Evans 				 struct nameidata *ndp, struct proc *p));
6180b301c3SPoul-Henning Kamp static int	union_root __P((struct mount *mp, struct vnode **vpp));
6280b301c3SPoul-Henning Kamp static int	union_statfs __P((struct mount *mp, struct statfs *sbp,
639b5e8b3aSBruce Evans 				  struct proc *p));
6480b301c3SPoul-Henning Kamp static int	union_unmount __P((struct mount *mp, int mntflags,
659b5e8b3aSBruce Evans 				   struct proc *p));
66b5e8ce9fSBruce Evans 
67df8bae1dSRodney W. Grimes /*
68df8bae1dSRodney W. Grimes  * Mount union filesystem
69df8bae1dSRodney W. Grimes  */
7080b301c3SPoul-Henning Kamp static int
71df8bae1dSRodney W. Grimes union_mount(mp, path, data, ndp, p)
72df8bae1dSRodney W. Grimes 	struct mount *mp;
73df8bae1dSRodney W. Grimes 	char *path;
74df8bae1dSRodney W. Grimes 	caddr_t data;
75df8bae1dSRodney W. Grimes 	struct nameidata *ndp;
76df8bae1dSRodney W. Grimes 	struct proc *p;
77df8bae1dSRodney W. Grimes {
78df8bae1dSRodney W. Grimes 	int error = 0;
79df8bae1dSRodney W. Grimes 	struct union_args args;
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;
8426f9a767SRodney W. Grimes 	char *cp = 0;
85df8bae1dSRodney W. Grimes 	int len;
86df8bae1dSRodney W. Grimes 	u_int size;
87df8bae1dSRodney W. Grimes 
882a31267eSMatthew Dillon 	UDEBUG(("union_mount(mp = %p)\n", (void *)mp));
89df8bae1dSRodney W. Grimes 
90df8bae1dSRodney W. Grimes 	/*
9181bca6ddSKATO Takenori 	 * Disable clustered write, otherwise system becomes unstable.
9281bca6ddSKATO Takenori 	 */
9381bca6ddSKATO Takenori 	mp->mnt_flag |= MNT_NOCLUSTERW;
9481bca6ddSKATO Takenori 
9581bca6ddSKATO Takenori 	/*
96df8bae1dSRodney W. Grimes 	 * Update is a no-op
97df8bae1dSRodney W. Grimes 	 */
98df8bae1dSRodney W. Grimes 	if (mp->mnt_flag & MNT_UPDATE) {
99df8bae1dSRodney W. Grimes 		/*
100df8bae1dSRodney W. Grimes 		 * Need to provide.
101df8bae1dSRodney W. Grimes 		 * 1. a way to convert between rdonly and rdwr mounts.
102df8bae1dSRodney W. Grimes 		 * 2. support for nfs exports.
103df8bae1dSRodney W. Grimes 		 */
104df8bae1dSRodney W. Grimes 		error = EOPNOTSUPP;
105df8bae1dSRodney W. Grimes 		goto bad;
106df8bae1dSRodney W. Grimes 	}
107df8bae1dSRodney W. Grimes 
108df8bae1dSRodney W. Grimes 	/*
109df8bae1dSRodney W. Grimes 	 * Get argument
110df8bae1dSRodney W. Grimes 	 */
1113a773ad0SPoul-Henning Kamp 	error = copyin(data, (caddr_t)&args, sizeof(struct union_args));
1123a773ad0SPoul-Henning Kamp 	if (error)
113df8bae1dSRodney W. Grimes 		goto bad;
114df8bae1dSRodney W. Grimes 
1152a31267eSMatthew Dillon 	/*
1162a31267eSMatthew Dillon 	 * Obtain lower vnode.  Vnode is stored in mp->mnt_vnodecovered.
1172a31267eSMatthew Dillon 	 * We need to reference it but not lock it.
1182a31267eSMatthew Dillon 	 */
1192a31267eSMatthew Dillon 
120df8bae1dSRodney W. Grimes 	lowerrootvp = mp->mnt_vnodecovered;
121df8bae1dSRodney W. Grimes 	VREF(lowerrootvp);
122df8bae1dSRodney W. Grimes 
1232a31267eSMatthew Dillon #if 0
124df8bae1dSRodney W. Grimes 	/*
1256db918e3SKATO Takenori 	 * Unlock lower node to avoid deadlock.
1266db918e3SKATO Takenori 	 */
127afc2a558SKATO Takenori 	if (lowerrootvp->v_op == union_vnodeop_p)
1286db918e3SKATO Takenori 		VOP_UNLOCK(lowerrootvp, 0, p);
1292a31267eSMatthew Dillon #endif
1306db918e3SKATO Takenori 
1316db918e3SKATO Takenori 	/*
1322a31267eSMatthew Dillon 	 * Obtain upper vnode by calling namei() on the path.  The
1332a31267eSMatthew Dillon 	 * upperrootvp will be turned referenced but not locked.
134df8bae1dSRodney W. Grimes 	 */
135df8bae1dSRodney W. Grimes 	NDINIT(ndp, LOOKUP, FOLLOW|WANTPARENT,
136df8bae1dSRodney W. Grimes 	       UIO_USERSPACE, args.target, p);
137df8bae1dSRodney W. Grimes 
1383a773ad0SPoul-Henning Kamp 	error = namei(ndp);
1392a31267eSMatthew Dillon 
1402a31267eSMatthew Dillon #if 0
141afc2a558SKATO Takenori 	if (lowerrootvp->v_op == union_vnodeop_p)
1426db918e3SKATO Takenori 		vn_lock(lowerrootvp, LK_EXCLUSIVE | LK_RETRY, p);
1432a31267eSMatthew Dillon #endif
1443a773ad0SPoul-Henning Kamp 	if (error)
145df8bae1dSRodney W. Grimes 		goto bad;
146df8bae1dSRodney W. Grimes 
147df8bae1dSRodney W. Grimes 	upperrootvp = ndp->ni_vp;
148df8bae1dSRodney W. Grimes 	vrele(ndp->ni_dvp);
149df8bae1dSRodney W. Grimes 	ndp->ni_dvp = NULL;
150df8bae1dSRodney W. Grimes 
1512a31267eSMatthew Dillon 	UDEBUG(("mount_root UPPERVP %p locked = %d\n", upperrootvp, VOP_ISLOCKED(upperrootvp)));
1522a31267eSMatthew Dillon 
1536db918e3SKATO Takenori 	/*
1546db918e3SKATO Takenori 	 * Check multi union mount to avoid `lock myself again' panic.
1552a31267eSMatthew Dillon 	 * Also require that it be a directory.
1566db918e3SKATO Takenori 	 */
1576db918e3SKATO Takenori 	if (upperrootvp == VTOUNION(lowerrootvp)->un_uppervp) {
158747e9157SKATO Takenori #ifdef DIAGNOSTIC
159747e9157SKATO Takenori 		printf("union_mount: multi union mount?\n");
160747e9157SKATO Takenori #endif
1616db918e3SKATO Takenori 		error = EDEADLK;
1626db918e3SKATO Takenori 		goto bad;
1636db918e3SKATO Takenori 	}
1646db918e3SKATO Takenori 
165df8bae1dSRodney W. Grimes 	if (upperrootvp->v_type != VDIR) {
166df8bae1dSRodney W. Grimes 		error = EINVAL;
167df8bae1dSRodney W. Grimes 		goto bad;
168df8bae1dSRodney W. Grimes 	}
169df8bae1dSRodney W. Grimes 
170df8bae1dSRodney W. Grimes 	/*
1712a31267eSMatthew Dillon 	 * Allocate our union_mount structure and populate the fields.
1722a31267eSMatthew Dillon 	 * The vnode references are stored in the union_mount as held,
1732a31267eSMatthew Dillon 	 * unlocked references.  Depending on the _BELOW flag, the
1742a31267eSMatthew Dillon 	 * filesystems are viewed in a different order.  In effect this
1752a31267eSMatthew Dillon 	 * is the same as providing a mount-under option to the mount
1762a31267eSMatthew Dillon 	 * syscall.
177df8bae1dSRodney W. Grimes 	 */
178df8bae1dSRodney W. Grimes 
1792a31267eSMatthew Dillon 	um = (struct union_mount *) malloc(sizeof(struct union_mount),
1802a31267eSMatthew Dillon 				M_UNIONFSMNT, M_WAITOK);
1812a31267eSMatthew Dillon 
1822a31267eSMatthew Dillon 	bzero(um, sizeof(struct union_mount));
1832a31267eSMatthew Dillon 
184df8bae1dSRodney W. Grimes 	um->um_op = args.mntflags & UNMNT_OPMASK;
1852a31267eSMatthew Dillon 
186df8bae1dSRodney W. Grimes 	switch (um->um_op) {
187df8bae1dSRodney W. Grimes 	case UNMNT_ABOVE:
188df8bae1dSRodney W. Grimes 		um->um_lowervp = lowerrootvp;
189df8bae1dSRodney W. Grimes 		um->um_uppervp = upperrootvp;
1902a31267eSMatthew Dillon 		upperrootvp = NULL;
1912a31267eSMatthew Dillon 		lowerrootvp = NULL;
192df8bae1dSRodney W. Grimes 		break;
193df8bae1dSRodney W. Grimes 
194df8bae1dSRodney W. Grimes 	case UNMNT_BELOW:
195df8bae1dSRodney W. Grimes 		um->um_lowervp = upperrootvp;
196df8bae1dSRodney W. Grimes 		um->um_uppervp = lowerrootvp;
1972a31267eSMatthew Dillon 		upperrootvp = NULL;
1982a31267eSMatthew Dillon 		lowerrootvp = NULL;
199df8bae1dSRodney W. Grimes 		break;
200df8bae1dSRodney W. Grimes 
201df8bae1dSRodney W. Grimes 	case UNMNT_REPLACE:
202df8bae1dSRodney W. Grimes 		vrele(lowerrootvp);
2032a31267eSMatthew Dillon 		lowerrootvp = NULL;
204df8bae1dSRodney W. Grimes 		um->um_uppervp = upperrootvp;
205df8bae1dSRodney W. Grimes 		um->um_lowervp = lowerrootvp;
2062a31267eSMatthew Dillon 		upperrootvp = NULL;
207df8bae1dSRodney W. Grimes 		break;
208df8bae1dSRodney W. Grimes 
209df8bae1dSRodney W. Grimes 	default:
210df8bae1dSRodney W. Grimes 		error = EINVAL;
211df8bae1dSRodney W. Grimes 		goto bad;
212df8bae1dSRodney W. Grimes 	}
213df8bae1dSRodney W. Grimes 
214996c772fSJohn Dyson 	/*
215996c772fSJohn Dyson 	 * Unless the mount is readonly, ensure that the top layer
216996c772fSJohn Dyson 	 * supports whiteout operations
217996c772fSJohn Dyson 	 */
218996c772fSJohn Dyson 	if ((mp->mnt_flag & MNT_RDONLY) == 0) {
2192a31267eSMatthew Dillon 		error = VOP_WHITEOUT(um->um_uppervp, NULL, LOOKUP);
220996c772fSJohn Dyson 		if (error)
221996c772fSJohn Dyson 			goto bad;
222996c772fSJohn Dyson 	}
223996c772fSJohn Dyson 
224996c772fSJohn Dyson 	um->um_cred = p->p_ucred;
225996c772fSJohn Dyson 	crhold(um->um_cred);
226df8bae1dSRodney W. Grimes 	um->um_cmode = UN_DIRMODE &~ p->p_fd->fd_cmask;
227df8bae1dSRodney W. Grimes 
228df8bae1dSRodney W. Grimes 	/*
229df8bae1dSRodney W. Grimes 	 * Depending on what you think the MNT_LOCAL flag might mean,
230df8bae1dSRodney W. Grimes 	 * you may want the && to be || on the conditional below.
231df8bae1dSRodney W. Grimes 	 * At the moment it has been defined that the filesystem is
232df8bae1dSRodney W. Grimes 	 * only local if it is all local, ie the MNT_LOCAL flag implies
233df8bae1dSRodney W. Grimes 	 * that the entire namespace is local.  If you think the MNT_LOCAL
234df8bae1dSRodney W. Grimes 	 * flag implies that some of the files might be stored locally
235df8bae1dSRodney W. Grimes 	 * then you will want to change the conditional.
236df8bae1dSRodney W. Grimes 	 */
237df8bae1dSRodney W. Grimes 	if (um->um_op == UNMNT_ABOVE) {
238df8bae1dSRodney W. Grimes 		if (((um->um_lowervp == NULLVP) ||
239df8bae1dSRodney W. Grimes 		     (um->um_lowervp->v_mount->mnt_flag & MNT_LOCAL)) &&
240df8bae1dSRodney W. Grimes 		    (um->um_uppervp->v_mount->mnt_flag & MNT_LOCAL))
241df8bae1dSRodney W. Grimes 			mp->mnt_flag |= MNT_LOCAL;
242df8bae1dSRodney W. Grimes 	}
243df8bae1dSRodney W. Grimes 
244df8bae1dSRodney W. Grimes 	/*
245df8bae1dSRodney W. Grimes 	 * Copy in the upper layer's RDONLY flag.  This is for the benefit
246df8bae1dSRodney W. Grimes 	 * of lookup() which explicitly checks the flag, rather than asking
247dc733423SDag-Erling Smørgrav 	 * the filesystem for its own opinion.  This means, that an update
248df8bae1dSRodney W. Grimes 	 * mount of the underlying filesystem to go from rdonly to rdwr
249df8bae1dSRodney W. Grimes 	 * will leave the unioned view as read-only.
250df8bae1dSRodney W. Grimes 	 */
251df8bae1dSRodney W. Grimes 	mp->mnt_flag |= (um->um_uppervp->v_mount->mnt_flag & MNT_RDONLY);
252df8bae1dSRodney W. Grimes 
253df8bae1dSRodney W. Grimes 	mp->mnt_data = (qaddr_t) um;
254996c772fSJohn Dyson 	vfs_getnewfsid(mp);
255df8bae1dSRodney W. Grimes 
256df8bae1dSRodney W. Grimes 	(void) copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
257df8bae1dSRodney W. Grimes 	bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
258df8bae1dSRodney W. Grimes 
259df8bae1dSRodney W. Grimes 	switch (um->um_op) {
260df8bae1dSRodney W. Grimes 	case UNMNT_ABOVE:
261996c772fSJohn Dyson 		cp = "<above>:";
262df8bae1dSRodney W. Grimes 		break;
263df8bae1dSRodney W. Grimes 	case UNMNT_BELOW:
264996c772fSJohn Dyson 		cp = "<below>:";
265df8bae1dSRodney W. Grimes 		break;
266df8bae1dSRodney W. Grimes 	case UNMNT_REPLACE:
267df8bae1dSRodney W. Grimes 		cp = "";
268df8bae1dSRodney W. Grimes 		break;
269df8bae1dSRodney W. Grimes 	}
270df8bae1dSRodney W. Grimes 	len = strlen(cp);
271df8bae1dSRodney W. Grimes 	bcopy(cp, mp->mnt_stat.f_mntfromname, len);
272df8bae1dSRodney W. Grimes 
273df8bae1dSRodney W. Grimes 	cp = mp->mnt_stat.f_mntfromname + len;
274df8bae1dSRodney W. Grimes 	len = MNAMELEN - len;
275df8bae1dSRodney W. Grimes 
276df8bae1dSRodney W. Grimes 	(void) copyinstr(args.target, cp, len - 1, &size);
277df8bae1dSRodney W. Grimes 	bzero(cp + size, len - size);
278df8bae1dSRodney W. Grimes 
279c4a7b7e1SDavid Greenman 	(void)union_statfs(mp, &mp->mnt_stat, p);
280c4a7b7e1SDavid Greenman 
2812a31267eSMatthew Dillon 	UDEBUG(("union_mount: from %s, on %s\n",
2822a31267eSMatthew Dillon 		mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname));
283df8bae1dSRodney W. Grimes 	return (0);
284df8bae1dSRodney W. Grimes 
285df8bae1dSRodney W. Grimes bad:
2862a31267eSMatthew Dillon 	if (um) {
2872a31267eSMatthew Dillon 		if (um->um_uppervp)
2882a31267eSMatthew Dillon 			vrele(um->um_uppervp);
2892a31267eSMatthew Dillon 		if (um->um_lowervp)
2902a31267eSMatthew Dillon 			vrele(um->um_lowervp);
2912a31267eSMatthew Dillon 		/* XXX other fields */
292a1c995b6SPoul-Henning Kamp 		free(um, M_UNIONFSMNT);
2932a31267eSMatthew Dillon 	}
294df8bae1dSRodney W. Grimes 	if (cred)
295df8bae1dSRodney W. Grimes 		crfree(cred);
296df8bae1dSRodney W. Grimes 	if (upperrootvp)
297df8bae1dSRodney W. Grimes 		vrele(upperrootvp);
298df8bae1dSRodney W. Grimes 	if (lowerrootvp)
299df8bae1dSRodney W. Grimes 		vrele(lowerrootvp);
300df8bae1dSRodney W. Grimes 	return (error);
301df8bae1dSRodney W. Grimes }
302df8bae1dSRodney W. Grimes 
303df8bae1dSRodney W. Grimes /*
304df8bae1dSRodney W. Grimes  * Free reference to union layer
305df8bae1dSRodney W. Grimes  */
30680b301c3SPoul-Henning Kamp static int
307df8bae1dSRodney W. Grimes union_unmount(mp, mntflags, p)
308df8bae1dSRodney W. Grimes 	struct mount *mp;
309df8bae1dSRodney W. Grimes 	int mntflags;
310df8bae1dSRodney W. Grimes 	struct proc *p;
311df8bae1dSRodney W. Grimes {
312df8bae1dSRodney W. Grimes 	struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
313df8bae1dSRodney W. Grimes 	struct vnode *um_rootvp;
314df8bae1dSRodney W. Grimes 	int error;
315996c772fSJohn Dyson 	int freeing;
316df8bae1dSRodney W. Grimes 	int flags = 0;
317df8bae1dSRodney W. Grimes 
3182a31267eSMatthew Dillon 	UDEBUG(("union_unmount(mp = %p)\n", (void *)mp));
319df8bae1dSRodney W. Grimes 
320996c772fSJohn Dyson 	if (mntflags & MNT_FORCE)
321996c772fSJohn Dyson 		flags |= FORCECLOSE;
322996c772fSJohn Dyson 
323831a80b0SMatthew Dillon 	if ((error = union_root(mp, &um_rootvp)) != 0)
324df8bae1dSRodney W. Grimes 		return (error);
325df8bae1dSRodney W. Grimes 
326996c772fSJohn Dyson 	/*
327996c772fSJohn Dyson 	 * Keep flushing vnodes from the mount list.
328996c772fSJohn Dyson 	 * This is needed because of the un_pvp held
329996c772fSJohn Dyson 	 * reference to the parent vnode.
330996c772fSJohn Dyson 	 * If more vnodes have been freed on a given pass,
331996c772fSJohn Dyson 	 * the try again.  The loop will iterate at most
332996c772fSJohn Dyson 	 * (d) times, where (d) is the maximum tree depth
333996c772fSJohn Dyson 	 * in the filesystem.
334996c772fSJohn Dyson 	 */
335996c772fSJohn Dyson 	for (freeing = 0; vflush(mp, um_rootvp, flags) != 0;) {
336996c772fSJohn Dyson 		struct vnode *vp;
337996c772fSJohn Dyson 		int n;
338996c772fSJohn Dyson 
339996c772fSJohn Dyson 		/* count #vnodes held on mount list */
340996c772fSJohn Dyson 		for (n = 0, vp = mp->mnt_vnodelist.lh_first;
341996c772fSJohn Dyson 				vp != NULLVP;
342996c772fSJohn Dyson 				vp = vp->v_mntvnodes.le_next)
343996c772fSJohn Dyson 			n++;
344996c772fSJohn Dyson 
345996c772fSJohn Dyson 		/* if this is unchanged then stop */
346996c772fSJohn Dyson 		if (n == freeing)
347996c772fSJohn Dyson 			break;
348996c772fSJohn Dyson 
349996c772fSJohn Dyson 		/* otherwise try once more time */
350996c772fSJohn Dyson 		freeing = n;
351df8bae1dSRodney W. Grimes 	}
352df8bae1dSRodney W. Grimes 
353996c772fSJohn Dyson 	/* At this point the root vnode should have a single reference */
354df8bae1dSRodney W. Grimes 	if (um_rootvp->v_usecount > 1) {
355df8bae1dSRodney W. Grimes 		vput(um_rootvp);
356df8bae1dSRodney W. Grimes 		return (EBUSY);
357df8bae1dSRodney W. Grimes 	}
358df8bae1dSRodney W. Grimes 
3598021ba87SBruce Evans #ifdef DEBUG
360996c772fSJohn Dyson 	vprint("union root", um_rootvp);
361df8bae1dSRodney W. Grimes #endif
362df8bae1dSRodney W. Grimes 	/*
363df8bae1dSRodney W. Grimes 	 * Discard references to upper and lower target vnodes.
364df8bae1dSRodney W. Grimes 	 */
365df8bae1dSRodney W. Grimes 	if (um->um_lowervp)
366df8bae1dSRodney W. Grimes 		vrele(um->um_lowervp);
367df8bae1dSRodney W. Grimes 	vrele(um->um_uppervp);
368df8bae1dSRodney W. Grimes 	crfree(um->um_cred);
369df8bae1dSRodney W. Grimes 	/*
370df8bae1dSRodney W. Grimes 	 * Release reference on underlying root vnode
371df8bae1dSRodney W. Grimes 	 */
372df8bae1dSRodney W. Grimes 	vput(um_rootvp);
373df8bae1dSRodney W. Grimes 	/*
374df8bae1dSRodney W. Grimes 	 * And blow it away for future re-use
375df8bae1dSRodney W. Grimes 	 */
376df8bae1dSRodney W. Grimes 	vgone(um_rootvp);
377df8bae1dSRodney W. Grimes 	/*
378df8bae1dSRodney W. Grimes 	 * Finally, throw away the union_mount structure
379df8bae1dSRodney W. Grimes 	 */
380a1c995b6SPoul-Henning Kamp 	free(mp->mnt_data, M_UNIONFSMNT);	/* XXX */
381df8bae1dSRodney W. Grimes 	mp->mnt_data = 0;
382df8bae1dSRodney W. Grimes 	return (0);
383df8bae1dSRodney W. Grimes }
384df8bae1dSRodney W. Grimes 
38580b301c3SPoul-Henning Kamp static int
386df8bae1dSRodney W. Grimes union_root(mp, vpp)
387df8bae1dSRodney W. Grimes 	struct mount *mp;
388df8bae1dSRodney W. Grimes 	struct vnode **vpp;
389df8bae1dSRodney W. Grimes {
390df8bae1dSRodney W. Grimes 	struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
391df8bae1dSRodney W. Grimes 	int error;
392df8bae1dSRodney W. Grimes 
393df8bae1dSRodney W. Grimes 	/*
3942a31267eSMatthew Dillon 	 * Supply an unlocked reference to um_uppervp and to um_lowervp.  It
3952a31267eSMatthew Dillon 	 * is possible for um_uppervp to be locked without the associated
3962a31267eSMatthew Dillon 	 * root union_node being locked.  We let union_allocvp() deal with
3972a31267eSMatthew Dillon 	 * it.
398df8bae1dSRodney W. Grimes 	 */
3992a31267eSMatthew Dillon 	UDEBUG(("union_root UPPERVP %p locked = %d\n", um->um_uppervp, VOP_ISLOCKED(um->um_uppervp)));
4002a31267eSMatthew Dillon 
401df8bae1dSRodney W. Grimes 	VREF(um->um_uppervp);
402df8bae1dSRodney W. Grimes 	if (um->um_lowervp)
403df8bae1dSRodney W. Grimes 		VREF(um->um_lowervp);
404df8bae1dSRodney W. Grimes 
4052a31267eSMatthew Dillon 	error = union_allocvp(vpp, mp, NULLVP, NULLVP, NULL,
4062a31267eSMatthew Dillon 		    um->um_uppervp, um->um_lowervp, 1);
4072a31267eSMatthew Dillon 	UDEBUG(("error %d\n", error));
4082a31267eSMatthew Dillon 	UDEBUG(("union_root2 UPPERVP %p locked = %d\n", um->um_uppervp, VOP_ISLOCKED(um->um_uppervp)));
409df8bae1dSRodney W. Grimes 
410df8bae1dSRodney W. Grimes 	return (error);
411df8bae1dSRodney W. Grimes }
412df8bae1dSRodney W. Grimes 
41380b301c3SPoul-Henning Kamp static int
414df8bae1dSRodney W. Grimes union_statfs(mp, sbp, p)
415df8bae1dSRodney W. Grimes 	struct mount *mp;
416df8bae1dSRodney W. Grimes 	struct statfs *sbp;
417df8bae1dSRodney W. Grimes 	struct proc *p;
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) {
430df8bae1dSRodney W. Grimes 		error = VFS_STATFS(um->um_lowervp->v_mount, &mstat, p);
431df8bae1dSRodney W. Grimes 		if (error)
432df8bae1dSRodney W. Grimes 			return (error);
433df8bae1dSRodney W. Grimes 	}
434df8bae1dSRodney W. Grimes 
435df8bae1dSRodney W. Grimes 	/* now copy across the "interesting" information and fake the rest */
436df8bae1dSRodney W. Grimes #if 0
437df8bae1dSRodney W. Grimes 	sbp->f_type = mstat.f_type;
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 #endif
442df8bae1dSRodney W. Grimes 	lbsize = mstat.f_bsize;
443df8bae1dSRodney W. Grimes 	sbp->f_blocks = mstat.f_blocks;
444df8bae1dSRodney W. Grimes 	sbp->f_bfree = mstat.f_bfree;
445df8bae1dSRodney W. Grimes 	sbp->f_bavail = mstat.f_bavail;
446df8bae1dSRodney W. Grimes 	sbp->f_files = mstat.f_files;
447df8bae1dSRodney W. Grimes 	sbp->f_ffree = mstat.f_ffree;
448df8bae1dSRodney W. Grimes 
449df8bae1dSRodney W. Grimes 	error = VFS_STATFS(um->um_uppervp->v_mount, &mstat, p);
450df8bae1dSRodney W. Grimes 	if (error)
451df8bae1dSRodney W. Grimes 		return (error);
452df8bae1dSRodney W. Grimes 
453df8bae1dSRodney W. Grimes 	sbp->f_flags = mstat.f_flags;
454df8bae1dSRodney W. Grimes 	sbp->f_bsize = mstat.f_bsize;
455df8bae1dSRodney W. Grimes 	sbp->f_iosize = mstat.f_iosize;
456df8bae1dSRodney W. Grimes 
457df8bae1dSRodney W. Grimes 	/*
458df8bae1dSRodney W. Grimes 	 * if the lower and upper blocksizes differ, then frig the
459df8bae1dSRodney W. Grimes 	 * block counts so that the sizes reported by df make some
460df8bae1dSRodney W. Grimes 	 * kind of sense.  none of this makes sense though.
461df8bae1dSRodney W. Grimes 	 */
462df8bae1dSRodney W. Grimes 
463996c772fSJohn Dyson 	if (mstat.f_bsize != lbsize)
464c9bf0111SKATO Takenori 		sbp->f_blocks = ((off_t) sbp->f_blocks * lbsize) / mstat.f_bsize;
465996c772fSJohn Dyson 
466996c772fSJohn Dyson 	/*
467996c772fSJohn Dyson 	 * The "total" fields count total resources in all layers,
468996c772fSJohn Dyson 	 * the "free" fields count only those resources which are
469996c772fSJohn Dyson 	 * free in the upper layer (since only the upper layer
470996c772fSJohn Dyson 	 * is writeable).
471996c772fSJohn Dyson 	 */
472df8bae1dSRodney W. Grimes 	sbp->f_blocks += mstat.f_blocks;
473996c772fSJohn Dyson 	sbp->f_bfree = mstat.f_bfree;
474996c772fSJohn Dyson 	sbp->f_bavail = mstat.f_bavail;
475df8bae1dSRodney W. Grimes 	sbp->f_files += mstat.f_files;
476996c772fSJohn Dyson 	sbp->f_ffree = mstat.f_ffree;
477df8bae1dSRodney W. Grimes 
478df8bae1dSRodney W. Grimes 	if (sbp != &mp->mnt_stat) {
479996c772fSJohn Dyson 		sbp->f_type = mp->mnt_vfc->vfc_typenum;
480df8bae1dSRodney W. Grimes 		bcopy(&mp->mnt_stat.f_fsid, &sbp->f_fsid, sizeof(sbp->f_fsid));
481df8bae1dSRodney W. Grimes 		bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
482df8bae1dSRodney W. Grimes 		bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
483df8bae1dSRodney W. Grimes 	}
484df8bae1dSRodney W. Grimes 	return (0);
485df8bae1dSRodney W. Grimes }
486df8bae1dSRodney W. Grimes 
48780b301c3SPoul-Henning Kamp static struct vfsops union_vfsops = {
488df8bae1dSRodney W. Grimes 	union_mount,
489c24fda81SAlfred Perlstein 	vfs_stdstart,	/* underlying start already done */
490df8bae1dSRodney W. Grimes 	union_unmount,
491df8bae1dSRodney W. Grimes 	union_root,
4925a5fccc8SAlfred Perlstein 	vfs_stdquotactl,
493df8bae1dSRodney W. Grimes 	union_statfs,
4945a5fccc8SAlfred Perlstein 	vfs_stdsync,    /* XXX assumes no cached data on union level */
4955a5fccc8SAlfred Perlstein 	vfs_stdvget,
4965a5fccc8SAlfred Perlstein 	vfs_stdfhtovp,
497c24fda81SAlfred Perlstein 	vfs_stdcheckexp,
4985a5fccc8SAlfred Perlstein 	vfs_stdvptofh,
499df8bae1dSRodney W. Grimes 	union_init,
500df8bae1dSRodney W. Grimes };
501c901836cSGarrett Wollman 
5028994ca3cSBruce Evans VFS_SET(union_vfsops, union, VFCF_LOOPBACK);
503