xref: /freebsd/sys/fs/unionfs/union_vfsops.c (revision 80b301c38576799e30df48fb9f68dce8378906c7)
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
3880b301c3SPoul-Henning Kamp  * $Id: union_vfsops.c,v 1.21 1997/10/12 20:24:57 phk Exp $
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 *));
599b5e8b3aSBruce Evans 
609b5e8b3aSBruce Evans extern int	union_fhtovp __P((struct mount *mp, struct fid *fidp,
619b5e8b3aSBruce Evans 				  struct mbuf *nam, struct vnode **vpp,
629b5e8b3aSBruce Evans 				  int *exflagsp, struct ucred **credanonp));
6380b301c3SPoul-Henning Kamp static int	union_mount __P((struct mount *mp, char *path, caddr_t data,
649b5e8b3aSBruce Evans 				 struct nameidata *ndp, struct proc *p));
659b5e8b3aSBruce Evans extern int	union_quotactl __P((struct mount *mp, int cmd, uid_t uid,
669b5e8b3aSBruce Evans 				    caddr_t arg, struct proc *p));
6780b301c3SPoul-Henning Kamp static int	union_root __P((struct mount *mp, struct vnode **vpp));
6880b301c3SPoul-Henning Kamp static int	union_start __P((struct mount *mp, int flags, struct proc *p));
6980b301c3SPoul-Henning Kamp static int	union_statfs __P((struct mount *mp, struct statfs *sbp,
709b5e8b3aSBruce Evans 				  struct proc *p));
719b5e8b3aSBruce Evans extern int	union_sync __P((struct mount *mp, int waitfor,
729b5e8b3aSBruce Evans 				struct ucred *cred, struct proc *p));
7380b301c3SPoul-Henning Kamp static int	union_unmount __P((struct mount *mp, int mntflags,
749b5e8b3aSBruce Evans 				   struct proc *p));
759b5e8b3aSBruce Evans extern int	union_vget __P((struct mount *mp, ino_t ino,
769b5e8b3aSBruce Evans 				struct vnode **vpp));
779b5e8b3aSBruce Evans extern int	union_vptofh __P((struct vnode *vp, struct fid *fhp));
78b5e8ce9fSBruce Evans 
79df8bae1dSRodney W. Grimes /*
80df8bae1dSRodney W. Grimes  * Mount union filesystem
81df8bae1dSRodney W. Grimes  */
8280b301c3SPoul-Henning Kamp static int
83df8bae1dSRodney W. Grimes union_mount(mp, path, data, ndp, p)
84df8bae1dSRodney W. Grimes 	struct mount *mp;
85df8bae1dSRodney W. Grimes 	char *path;
86df8bae1dSRodney W. Grimes 	caddr_t data;
87df8bae1dSRodney W. Grimes 	struct nameidata *ndp;
88df8bae1dSRodney W. Grimes 	struct proc *p;
89df8bae1dSRodney W. Grimes {
90df8bae1dSRodney W. Grimes 	int error = 0;
91df8bae1dSRodney W. Grimes 	struct union_args args;
92df8bae1dSRodney W. Grimes 	struct vnode *lowerrootvp = NULLVP;
93df8bae1dSRodney W. Grimes 	struct vnode *upperrootvp = NULLVP;
94996c772fSJohn Dyson 	struct union_mount *um = 0;
95df8bae1dSRodney W. Grimes 	struct ucred *cred = 0;
9626f9a767SRodney W. Grimes 	char *cp = 0;
97df8bae1dSRodney W. Grimes 	int len;
98df8bae1dSRodney W. Grimes 	u_int size;
996db918e3SKATO Takenori 	int islowerunlocked = 0;
100df8bae1dSRodney W. Grimes 
101df8bae1dSRodney W. Grimes #ifdef UNION_DIAGNOSTIC
102df8bae1dSRodney W. Grimes 	printf("union_mount(mp = %x)\n", mp);
103df8bae1dSRodney W. Grimes #endif
104df8bae1dSRodney W. Grimes 
105df8bae1dSRodney W. Grimes 	/*
10681bca6ddSKATO Takenori 	 * Disable clustered write, otherwise system becomes unstable.
10781bca6ddSKATO Takenori 	 */
10881bca6ddSKATO Takenori 	mp->mnt_flag |= MNT_NOCLUSTERW;
10981bca6ddSKATO Takenori 
11081bca6ddSKATO Takenori 	/*
111df8bae1dSRodney W. Grimes 	 * Update is a no-op
112df8bae1dSRodney W. Grimes 	 */
113df8bae1dSRodney W. Grimes 	if (mp->mnt_flag & MNT_UPDATE) {
114df8bae1dSRodney W. Grimes 		/*
115df8bae1dSRodney W. Grimes 		 * Need to provide.
116df8bae1dSRodney W. Grimes 		 * 1. a way to convert between rdonly and rdwr mounts.
117df8bae1dSRodney W. Grimes 		 * 2. support for nfs exports.
118df8bae1dSRodney W. Grimes 		 */
119df8bae1dSRodney W. Grimes 		error = EOPNOTSUPP;
120df8bae1dSRodney W. Grimes 		goto bad;
121df8bae1dSRodney W. Grimes 	}
122df8bae1dSRodney W. Grimes 
123df8bae1dSRodney W. Grimes 	/*
124df8bae1dSRodney W. Grimes 	 * Get argument
125df8bae1dSRodney W. Grimes 	 */
1263a773ad0SPoul-Henning Kamp 	error = copyin(data, (caddr_t)&args, sizeof(struct union_args));
1273a773ad0SPoul-Henning Kamp 	if (error)
128df8bae1dSRodney W. Grimes 		goto bad;
129df8bae1dSRodney W. Grimes 
130df8bae1dSRodney W. Grimes 	lowerrootvp = mp->mnt_vnodecovered;
131df8bae1dSRodney W. Grimes 	VREF(lowerrootvp);
132df8bae1dSRodney W. Grimes 
133df8bae1dSRodney W. Grimes 	/*
1346db918e3SKATO Takenori 	 * Unlock lower node to avoid deadlock.
1356db918e3SKATO Takenori 	 * (XXX) VOP_ISLOCKED is needed?
1366db918e3SKATO Takenori 	 */
1376db918e3SKATO Takenori 	if ((lowerrootvp->v_op == union_vnodeop_p) && VOP_ISLOCKED(lowerrootvp)) {
1386db918e3SKATO Takenori 		VOP_UNLOCK(lowerrootvp, 0, p);
1396db918e3SKATO Takenori 		islowerunlocked = 1;
1406db918e3SKATO Takenori 	}
1416db918e3SKATO Takenori 
1426db918e3SKATO Takenori 	/*
143996c772fSJohn Dyson 	 * Find upper node.
144df8bae1dSRodney W. Grimes 	 */
145df8bae1dSRodney W. Grimes 	NDINIT(ndp, LOOKUP, FOLLOW|WANTPARENT,
146df8bae1dSRodney W. Grimes 	       UIO_USERSPACE, args.target, p);
147df8bae1dSRodney W. Grimes 
1483a773ad0SPoul-Henning Kamp 	error = namei(ndp);
1496db918e3SKATO Takenori 	/*
1506db918e3SKATO Takenori 	 * Re-lock vnode.
1516db918e3SKATO Takenori 	 * (XXX) VOP_ISLOCKED is needed?
1526db918e3SKATO Takenori 	 */
1536db918e3SKATO Takenori 	if (islowerunlocked && !VOP_ISLOCKED(lowerrootvp))
1546db918e3SKATO Takenori 		vn_lock(lowerrootvp, LK_EXCLUSIVE | LK_RETRY, p);
1553a773ad0SPoul-Henning Kamp 	if (error)
156df8bae1dSRodney W. Grimes 		goto bad;
157df8bae1dSRodney W. Grimes 
158df8bae1dSRodney W. Grimes 	upperrootvp = ndp->ni_vp;
159df8bae1dSRodney W. Grimes 	vrele(ndp->ni_dvp);
160df8bae1dSRodney W. Grimes 	ndp->ni_dvp = NULL;
161df8bae1dSRodney W. Grimes 
1626db918e3SKATO Takenori 	/*
1636db918e3SKATO Takenori 	 * Check multi union mount to avoid `lock myself again' panic.
1646db918e3SKATO Takenori 	 */
1656db918e3SKATO Takenori 	if (upperrootvp == VTOUNION(lowerrootvp)->un_uppervp) {
166747e9157SKATO Takenori #ifdef DIAGNOSTIC
167747e9157SKATO Takenori 		printf("union_mount: multi union mount?\n");
168747e9157SKATO Takenori #endif
1696db918e3SKATO Takenori 		error = EDEADLK;
1706db918e3SKATO Takenori 		goto bad;
1716db918e3SKATO Takenori 	}
1726db918e3SKATO Takenori 
173df8bae1dSRodney W. Grimes 	if (upperrootvp->v_type != VDIR) {
174df8bae1dSRodney W. Grimes 		error = EINVAL;
175df8bae1dSRodney W. Grimes 		goto bad;
176df8bae1dSRodney W. Grimes 	}
177df8bae1dSRodney W. Grimes 
178df8bae1dSRodney W. Grimes 	um = (struct union_mount *) malloc(sizeof(struct union_mount),
179a1c995b6SPoul-Henning Kamp 				M_UNIONFSMNT, M_WAITOK);	/* XXX */
180df8bae1dSRodney W. Grimes 
181df8bae1dSRodney W. Grimes 	/*
182df8bae1dSRodney W. Grimes 	 * Keep a held reference to the target vnodes.
183df8bae1dSRodney W. Grimes 	 * They are vrele'd in union_unmount.
184df8bae1dSRodney W. Grimes 	 *
185df8bae1dSRodney W. Grimes 	 * Depending on the _BELOW flag, the filesystems are
186df8bae1dSRodney W. Grimes 	 * viewed in a different order.  In effect, this is the
187df8bae1dSRodney W. Grimes 	 * same as providing a mount under option to the mount syscall.
188df8bae1dSRodney W. Grimes 	 */
189df8bae1dSRodney W. Grimes 
190df8bae1dSRodney W. Grimes 	um->um_op = args.mntflags & UNMNT_OPMASK;
191df8bae1dSRodney W. Grimes 	switch (um->um_op) {
192df8bae1dSRodney W. Grimes 	case UNMNT_ABOVE:
193df8bae1dSRodney W. Grimes 		um->um_lowervp = lowerrootvp;
194df8bae1dSRodney W. Grimes 		um->um_uppervp = upperrootvp;
195df8bae1dSRodney W. Grimes 		break;
196df8bae1dSRodney W. Grimes 
197df8bae1dSRodney W. Grimes 	case UNMNT_BELOW:
198df8bae1dSRodney W. Grimes 		um->um_lowervp = upperrootvp;
199df8bae1dSRodney W. Grimes 		um->um_uppervp = lowerrootvp;
200df8bae1dSRodney W. Grimes 		break;
201df8bae1dSRodney W. Grimes 
202df8bae1dSRodney W. Grimes 	case UNMNT_REPLACE:
203df8bae1dSRodney W. Grimes 		vrele(lowerrootvp);
204df8bae1dSRodney W. Grimes 		lowerrootvp = NULLVP;
205df8bae1dSRodney W. Grimes 		um->um_uppervp = upperrootvp;
206df8bae1dSRodney W. Grimes 		um->um_lowervp = lowerrootvp;
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) {
219996c772fSJohn Dyson 		error = VOP_WHITEOUT(um->um_uppervp, (struct componentname *) 0, 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
247df8bae1dSRodney W. Grimes 	 * the filesystem for it's 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 
281df8bae1dSRodney W. Grimes #ifdef UNION_DIAGNOSTIC
282df8bae1dSRodney W. Grimes 	printf("union_mount: from %s, on %s\n",
283df8bae1dSRodney W. Grimes 		mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname);
284df8bae1dSRodney W. Grimes #endif
285df8bae1dSRodney W. Grimes 	return (0);
286df8bae1dSRodney W. Grimes 
287df8bae1dSRodney W. Grimes bad:
288996c772fSJohn Dyson 	if (um)
289a1c995b6SPoul-Henning Kamp 		free(um, M_UNIONFSMNT);
290df8bae1dSRodney W. Grimes 	if (cred)
291df8bae1dSRodney W. Grimes 		crfree(cred);
292df8bae1dSRodney W. Grimes 	if (upperrootvp)
293df8bae1dSRodney W. Grimes 		vrele(upperrootvp);
294df8bae1dSRodney W. Grimes 	if (lowerrootvp)
295df8bae1dSRodney W. Grimes 		vrele(lowerrootvp);
296df8bae1dSRodney W. Grimes 	return (error);
297df8bae1dSRodney W. Grimes }
298df8bae1dSRodney W. Grimes 
299df8bae1dSRodney W. Grimes /*
300df8bae1dSRodney W. Grimes  * VFS start.  Nothing needed here - the start routine
301df8bae1dSRodney W. Grimes  * on the underlying filesystem(s) will have been called
302df8bae1dSRodney W. Grimes  * when that filesystem was mounted.
303df8bae1dSRodney W. Grimes  */
30480b301c3SPoul-Henning Kamp static int
305df8bae1dSRodney W. Grimes union_start(mp, flags, p)
306df8bae1dSRodney W. Grimes 	struct mount *mp;
307df8bae1dSRodney W. Grimes 	int flags;
308df8bae1dSRodney W. Grimes 	struct proc *p;
309df8bae1dSRodney W. Grimes {
310df8bae1dSRodney W. Grimes 
311df8bae1dSRodney W. Grimes 	return (0);
312df8bae1dSRodney W. Grimes }
313df8bae1dSRodney W. Grimes 
314df8bae1dSRodney W. Grimes /*
315df8bae1dSRodney W. Grimes  * Free reference to union layer
316df8bae1dSRodney W. Grimes  */
31780b301c3SPoul-Henning Kamp static int
318df8bae1dSRodney W. Grimes union_unmount(mp, mntflags, p)
319df8bae1dSRodney W. Grimes 	struct mount *mp;
320df8bae1dSRodney W. Grimes 	int mntflags;
321df8bae1dSRodney W. Grimes 	struct proc *p;
322df8bae1dSRodney W. Grimes {
323df8bae1dSRodney W. Grimes 	struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
324df8bae1dSRodney W. Grimes 	struct vnode *um_rootvp;
325df8bae1dSRodney W. Grimes 	int error;
326996c772fSJohn Dyson 	int freeing;
327df8bae1dSRodney W. Grimes 	int flags = 0;
328df8bae1dSRodney W. Grimes 
329df8bae1dSRodney W. Grimes #ifdef UNION_DIAGNOSTIC
330df8bae1dSRodney W. Grimes 	printf("union_unmount(mp = %x)\n", mp);
331df8bae1dSRodney W. Grimes #endif
332df8bae1dSRodney W. Grimes 
333996c772fSJohn Dyson 	if (mntflags & MNT_FORCE)
334996c772fSJohn Dyson 		flags |= FORCECLOSE;
335996c772fSJohn Dyson 
336996c772fSJohn Dyson 	if (error = union_root(mp, &um_rootvp))
337df8bae1dSRodney W. Grimes 		return (error);
338df8bae1dSRodney W. Grimes 
339996c772fSJohn Dyson 	/*
340996c772fSJohn Dyson 	 * Keep flushing vnodes from the mount list.
341996c772fSJohn Dyson 	 * This is needed because of the un_pvp held
342996c772fSJohn Dyson 	 * reference to the parent vnode.
343996c772fSJohn Dyson 	 * If more vnodes have been freed on a given pass,
344996c772fSJohn Dyson 	 * the try again.  The loop will iterate at most
345996c772fSJohn Dyson 	 * (d) times, where (d) is the maximum tree depth
346996c772fSJohn Dyson 	 * in the filesystem.
347996c772fSJohn Dyson 	 */
348996c772fSJohn Dyson 	for (freeing = 0; vflush(mp, um_rootvp, flags) != 0;) {
349996c772fSJohn Dyson 		struct vnode *vp;
350996c772fSJohn Dyson 		int n;
351996c772fSJohn Dyson 
352996c772fSJohn Dyson 		/* count #vnodes held on mount list */
353996c772fSJohn Dyson 		for (n = 0, vp = mp->mnt_vnodelist.lh_first;
354996c772fSJohn Dyson 				vp != NULLVP;
355996c772fSJohn Dyson 				vp = vp->v_mntvnodes.le_next)
356996c772fSJohn Dyson 			n++;
357996c772fSJohn Dyson 
358996c772fSJohn Dyson 		/* if this is unchanged then stop */
359996c772fSJohn Dyson 		if (n == freeing)
360996c772fSJohn Dyson 			break;
361996c772fSJohn Dyson 
362996c772fSJohn Dyson 		/* otherwise try once more time */
363996c772fSJohn Dyson 		freeing = n;
364df8bae1dSRodney W. Grimes 	}
365df8bae1dSRodney W. Grimes 
366996c772fSJohn Dyson 	/* At this point the root vnode should have a single reference */
367df8bae1dSRodney W. Grimes 	if (um_rootvp->v_usecount > 1) {
368df8bae1dSRodney W. Grimes 		vput(um_rootvp);
369df8bae1dSRodney W. Grimes 		return (EBUSY);
370df8bae1dSRodney W. Grimes 	}
371df8bae1dSRodney W. Grimes 
372df8bae1dSRodney W. Grimes #ifdef UNION_DIAGNOSTIC
373996c772fSJohn Dyson 	vprint("union root", um_rootvp);
374df8bae1dSRodney W. Grimes #endif
375df8bae1dSRodney W. Grimes 	/*
376df8bae1dSRodney W. Grimes 	 * Discard references to upper and lower target vnodes.
377df8bae1dSRodney W. Grimes 	 */
378df8bae1dSRodney W. Grimes 	if (um->um_lowervp)
379df8bae1dSRodney W. Grimes 		vrele(um->um_lowervp);
380df8bae1dSRodney W. Grimes 	vrele(um->um_uppervp);
381df8bae1dSRodney W. Grimes 	crfree(um->um_cred);
382df8bae1dSRodney W. Grimes 	/*
383df8bae1dSRodney W. Grimes 	 * Release reference on underlying root vnode
384df8bae1dSRodney W. Grimes 	 */
385df8bae1dSRodney W. Grimes 	vput(um_rootvp);
386df8bae1dSRodney W. Grimes 	/*
387df8bae1dSRodney W. Grimes 	 * And blow it away for future re-use
388df8bae1dSRodney W. Grimes 	 */
389df8bae1dSRodney W. Grimes 	vgone(um_rootvp);
390df8bae1dSRodney W. Grimes 	/*
391df8bae1dSRodney W. Grimes 	 * Finally, throw away the union_mount structure
392df8bae1dSRodney W. Grimes 	 */
393a1c995b6SPoul-Henning Kamp 	free(mp->mnt_data, M_UNIONFSMNT);	/* XXX */
394df8bae1dSRodney W. Grimes 	mp->mnt_data = 0;
395df8bae1dSRodney W. Grimes 	return (0);
396df8bae1dSRodney W. Grimes }
397df8bae1dSRodney W. Grimes 
39880b301c3SPoul-Henning Kamp static int
399df8bae1dSRodney W. Grimes union_root(mp, vpp)
400df8bae1dSRodney W. Grimes 	struct mount *mp;
401df8bae1dSRodney W. Grimes 	struct vnode **vpp;
402df8bae1dSRodney W. Grimes {
403996c772fSJohn Dyson 	struct proc *p = curproc;	/* XXX */
404df8bae1dSRodney W. Grimes 	struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
405df8bae1dSRodney W. Grimes 	int error;
406df8bae1dSRodney W. Grimes 	int loselock;
407df8bae1dSRodney W. Grimes 
408df8bae1dSRodney W. Grimes 	/*
409df8bae1dSRodney W. Grimes 	 * Return locked reference to root.
410df8bae1dSRodney W. Grimes 	 */
411df8bae1dSRodney W. Grimes 	VREF(um->um_uppervp);
412df8bae1dSRodney W. Grimes 	if ((um->um_op == UNMNT_BELOW) &&
413df8bae1dSRodney W. Grimes 	     VOP_ISLOCKED(um->um_uppervp)) {
414df8bae1dSRodney W. Grimes 		loselock = 1;
415df8bae1dSRodney W. Grimes 	} else {
416747e9157SKATO Takenori 		if (VOP_ISLOCKED(um->um_uppervp)) {
417747e9157SKATO Takenori 			/*
418747e9157SKATO Takenori 			 * XXX
419747e9157SKATO Takenori 			 * Should we check type of node?
420747e9157SKATO Takenori 			 */
421747e9157SKATO Takenori #ifdef DIAGNOSTIC
422747e9157SKATO Takenori 			printf("union_root: multi union mount?");
423747e9157SKATO Takenori #endif
424747e9157SKATO Takenori 			vrele(um->um_uppervp);
425747e9157SKATO Takenori 			return EDEADLK;
426747e9157SKATO Takenori 		} else
427996c772fSJohn Dyson 			vn_lock(um->um_uppervp, LK_EXCLUSIVE | LK_RETRY, p);
428df8bae1dSRodney W. Grimes 		loselock = 0;
429df8bae1dSRodney W. Grimes 	}
430df8bae1dSRodney W. Grimes 	if (um->um_lowervp)
431df8bae1dSRodney W. Grimes 		VREF(um->um_lowervp);
432df8bae1dSRodney W. Grimes 	error = union_allocvp(vpp, mp,
433df8bae1dSRodney W. Grimes 			      (struct vnode *) 0,
434df8bae1dSRodney W. Grimes 			      (struct vnode *) 0,
435df8bae1dSRodney W. Grimes 			      (struct componentname *) 0,
436df8bae1dSRodney W. Grimes 			      um->um_uppervp,
437996c772fSJohn Dyson 			      um->um_lowervp,
438996c772fSJohn Dyson 			      1);
439df8bae1dSRodney W. Grimes 
440df8bae1dSRodney W. Grimes 	if (error) {
441996c772fSJohn Dyson 		if (loselock)
442df8bae1dSRodney W. Grimes 			vrele(um->um_uppervp);
443996c772fSJohn Dyson 		else
444996c772fSJohn Dyson 			vput(um->um_uppervp);
445df8bae1dSRodney W. Grimes 		if (um->um_lowervp)
446df8bae1dSRodney W. Grimes 			vrele(um->um_lowervp);
447df8bae1dSRodney W. Grimes 	} else {
448df8bae1dSRodney W. Grimes 		if (loselock)
449df8bae1dSRodney W. Grimes 			VTOUNION(*vpp)->un_flags &= ~UN_ULOCK;
450df8bae1dSRodney W. Grimes 	}
451df8bae1dSRodney W. Grimes 
452df8bae1dSRodney W. Grimes 	return (error);
453df8bae1dSRodney W. Grimes }
454df8bae1dSRodney W. Grimes 
45580b301c3SPoul-Henning Kamp static int
456df8bae1dSRodney W. Grimes union_statfs(mp, sbp, p)
457df8bae1dSRodney W. Grimes 	struct mount *mp;
458df8bae1dSRodney W. Grimes 	struct statfs *sbp;
459df8bae1dSRodney W. Grimes 	struct proc *p;
460df8bae1dSRodney W. Grimes {
461df8bae1dSRodney W. Grimes 	int error;
462df8bae1dSRodney W. Grimes 	struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
463df8bae1dSRodney W. Grimes 	struct statfs mstat;
464df8bae1dSRodney W. Grimes 	int lbsize;
465df8bae1dSRodney W. Grimes 
466df8bae1dSRodney W. Grimes #ifdef UNION_DIAGNOSTIC
467df8bae1dSRodney W. Grimes 	printf("union_statfs(mp = %x, lvp = %x, uvp = %x)\n", mp,
468df8bae1dSRodney W. Grimes 			um->um_lowervp,
469df8bae1dSRodney W. Grimes 	       		um->um_uppervp);
470df8bae1dSRodney W. Grimes #endif
471df8bae1dSRodney W. Grimes 
472df8bae1dSRodney W. Grimes 	bzero(&mstat, sizeof(mstat));
473df8bae1dSRodney W. Grimes 
474df8bae1dSRodney W. Grimes 	if (um->um_lowervp) {
475df8bae1dSRodney W. Grimes 		error = VFS_STATFS(um->um_lowervp->v_mount, &mstat, p);
476df8bae1dSRodney W. Grimes 		if (error)
477df8bae1dSRodney W. Grimes 			return (error);
478df8bae1dSRodney W. Grimes 	}
479df8bae1dSRodney W. Grimes 
480df8bae1dSRodney W. Grimes 	/* now copy across the "interesting" information and fake the rest */
481df8bae1dSRodney W. Grimes #if 0
482df8bae1dSRodney W. Grimes 	sbp->f_type = mstat.f_type;
483df8bae1dSRodney W. Grimes 	sbp->f_flags = mstat.f_flags;
484df8bae1dSRodney W. Grimes 	sbp->f_bsize = mstat.f_bsize;
485df8bae1dSRodney W. Grimes 	sbp->f_iosize = mstat.f_iosize;
486df8bae1dSRodney W. Grimes #endif
487df8bae1dSRodney W. Grimes 	lbsize = mstat.f_bsize;
488df8bae1dSRodney W. Grimes 	sbp->f_blocks = mstat.f_blocks;
489df8bae1dSRodney W. Grimes 	sbp->f_bfree = mstat.f_bfree;
490df8bae1dSRodney W. Grimes 	sbp->f_bavail = mstat.f_bavail;
491df8bae1dSRodney W. Grimes 	sbp->f_files = mstat.f_files;
492df8bae1dSRodney W. Grimes 	sbp->f_ffree = mstat.f_ffree;
493df8bae1dSRodney W. Grimes 
494df8bae1dSRodney W. Grimes 	error = VFS_STATFS(um->um_uppervp->v_mount, &mstat, p);
495df8bae1dSRodney W. Grimes 	if (error)
496df8bae1dSRodney W. Grimes 		return (error);
497df8bae1dSRodney W. Grimes 
498df8bae1dSRodney W. Grimes 	sbp->f_flags = mstat.f_flags;
499df8bae1dSRodney W. Grimes 	sbp->f_bsize = mstat.f_bsize;
500df8bae1dSRodney W. Grimes 	sbp->f_iosize = mstat.f_iosize;
501df8bae1dSRodney W. Grimes 
502df8bae1dSRodney W. Grimes 	/*
503df8bae1dSRodney W. Grimes 	 * if the lower and upper blocksizes differ, then frig the
504df8bae1dSRodney W. Grimes 	 * block counts so that the sizes reported by df make some
505df8bae1dSRodney W. Grimes 	 * kind of sense.  none of this makes sense though.
506df8bae1dSRodney W. Grimes 	 */
507df8bae1dSRodney W. Grimes 
508996c772fSJohn Dyson 	if (mstat.f_bsize != lbsize)
509c9bf0111SKATO Takenori 		sbp->f_blocks = ((off_t) sbp->f_blocks * lbsize) / mstat.f_bsize;
510996c772fSJohn Dyson 
511996c772fSJohn Dyson 	/*
512996c772fSJohn Dyson 	 * The "total" fields count total resources in all layers,
513996c772fSJohn Dyson 	 * the "free" fields count only those resources which are
514996c772fSJohn Dyson 	 * free in the upper layer (since only the upper layer
515996c772fSJohn Dyson 	 * is writeable).
516996c772fSJohn Dyson 	 */
517df8bae1dSRodney W. Grimes 	sbp->f_blocks += mstat.f_blocks;
518996c772fSJohn Dyson 	sbp->f_bfree = mstat.f_bfree;
519996c772fSJohn Dyson 	sbp->f_bavail = mstat.f_bavail;
520df8bae1dSRodney W. Grimes 	sbp->f_files += mstat.f_files;
521996c772fSJohn Dyson 	sbp->f_ffree = mstat.f_ffree;
522df8bae1dSRodney W. Grimes 
523df8bae1dSRodney W. Grimes 	if (sbp != &mp->mnt_stat) {
524996c772fSJohn Dyson 		sbp->f_type = mp->mnt_vfc->vfc_typenum;
525df8bae1dSRodney W. Grimes 		bcopy(&mp->mnt_stat.f_fsid, &sbp->f_fsid, sizeof(sbp->f_fsid));
526df8bae1dSRodney W. Grimes 		bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
527df8bae1dSRodney W. Grimes 		bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
528df8bae1dSRodney W. Grimes 	}
529df8bae1dSRodney W. Grimes 	return (0);
530df8bae1dSRodney W. Grimes }
531df8bae1dSRodney W. Grimes 
532df8bae1dSRodney W. Grimes /*
533df8bae1dSRodney W. Grimes  * XXX - Assumes no data cached at union layer.
534df8bae1dSRodney W. Grimes  */
535996c772fSJohn Dyson #define union_sync ((int (*) __P((struct mount *, int, struct ucred *, \
536996c772fSJohn Dyson 	    struct proc *)))nullop)
537df8bae1dSRodney W. Grimes 
538996c772fSJohn Dyson #define union_fhtovp ((int (*) __P((struct mount *, struct fid *, \
53957bf258eSGarrett Wollman 	    struct sockaddr *, struct vnode **, int *, struct ucred **)))eopnotsupp)
540996c772fSJohn Dyson #define union_quotactl ((int (*) __P((struct mount *, int, uid_t, caddr_t, \
541996c772fSJohn Dyson 	    struct proc *)))eopnotsupp)
542996c772fSJohn Dyson #define union_sysctl ((int (*) __P((int *, u_int, void *, size_t *, void *, \
543996c772fSJohn Dyson 	    size_t, struct proc *)))eopnotsupp)
544996c772fSJohn Dyson #define union_vget ((int (*) __P((struct mount *, ino_t, struct vnode **))) \
545996c772fSJohn Dyson 	    eopnotsupp)
546996c772fSJohn Dyson #define union_vptofh ((int (*) __P((struct vnode *, struct fid *)))eopnotsupp)
547df8bae1dSRodney W. Grimes 
54880b301c3SPoul-Henning Kamp static struct vfsops union_vfsops = {
549df8bae1dSRodney W. Grimes 	union_mount,
550df8bae1dSRodney W. Grimes 	union_start,
551df8bae1dSRodney W. Grimes 	union_unmount,
552df8bae1dSRodney W. Grimes 	union_root,
553df8bae1dSRodney W. Grimes 	union_quotactl,
554df8bae1dSRodney W. Grimes 	union_statfs,
555df8bae1dSRodney W. Grimes 	union_sync,
556df8bae1dSRodney W. Grimes 	union_vget,
557df8bae1dSRodney W. Grimes 	union_fhtovp,
558df8bae1dSRodney W. Grimes 	union_vptofh,
559df8bae1dSRodney W. Grimes 	union_init,
560df8bae1dSRodney W. Grimes };
561c901836cSGarrett Wollman 
562bbf3a566SGarrett Wollman VFS_SET(union_vfsops, union, MOUNT_UNION, VFCF_LOOPBACK);
563