xref: /freebsd/sys/fs/unionfs/union_vfsops.c (revision b5e8ce9f12b66453759f254bdf8bfc53fe2b6948)
1df8bae1dSRodney W. Grimes /*
2df8bae1dSRodney W. Grimes  * Copyright (c) 1994 The Regents of the University of California.
3df8bae1dSRodney W. Grimes  * Copyright (c) 1994 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  *
37df8bae1dSRodney W. Grimes  *	@(#)union_vfsops.c	8.7 (Berkeley) 3/5/94
38b5e8ce9fSBruce Evans  * $Id: union_vfsops.c,v 1.7 1994/11/04 14:41:43 davidg 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/time.h>
49df8bae1dSRodney W. Grimes #include <sys/types.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>
56df8bae1dSRodney W. Grimes #include <sys/queue.h>
57df8bae1dSRodney W. Grimes #include <miscfs/union/union.h>
58df8bae1dSRodney W. Grimes 
59b5e8ce9fSBruce Evans int	union_root __P((struct mount *, struct vnode **));
60b5e8ce9fSBruce Evans int	union_statfs __P((struct mount *, struct statfs *, struct proc *));
61b5e8ce9fSBruce Evans 
62df8bae1dSRodney W. Grimes /*
63df8bae1dSRodney W. Grimes  * Mount union filesystem
64df8bae1dSRodney W. Grimes  */
65df8bae1dSRodney W. Grimes int
66df8bae1dSRodney W. Grimes union_mount(mp, path, data, ndp, p)
67df8bae1dSRodney W. Grimes 	struct mount *mp;
68df8bae1dSRodney W. Grimes 	char *path;
69df8bae1dSRodney W. Grimes 	caddr_t data;
70df8bae1dSRodney W. Grimes 	struct nameidata *ndp;
71df8bae1dSRodney W. Grimes 	struct proc *p;
72df8bae1dSRodney W. Grimes {
73df8bae1dSRodney W. Grimes 	int error = 0;
74df8bae1dSRodney W. Grimes 	struct union_args args;
75df8bae1dSRodney W. Grimes 	struct vnode *lowerrootvp = NULLVP;
76df8bae1dSRodney W. Grimes 	struct vnode *upperrootvp = NULLVP;
77df8bae1dSRodney W. Grimes 	struct union_mount *um;
78df8bae1dSRodney W. Grimes 	struct ucred *cred = 0;
79df8bae1dSRodney W. Grimes 	struct ucred *scred;
80df8bae1dSRodney W. Grimes 	struct vattr va;
8126f9a767SRodney W. Grimes 	char *cp = 0;
82df8bae1dSRodney W. Grimes 	int len;
83df8bae1dSRodney W. Grimes 	u_int size;
84df8bae1dSRodney W. Grimes 
85df8bae1dSRodney W. Grimes #ifdef UNION_DIAGNOSTIC
86df8bae1dSRodney W. Grimes 	printf("union_mount(mp = %x)\n", mp);
87df8bae1dSRodney W. Grimes #endif
88df8bae1dSRodney W. Grimes 
89df8bae1dSRodney W. Grimes 	/*
90df8bae1dSRodney W. Grimes 	 * Update is a no-op
91df8bae1dSRodney W. Grimes 	 */
92df8bae1dSRodney W. Grimes 	if (mp->mnt_flag & MNT_UPDATE) {
93df8bae1dSRodney W. Grimes 		/*
94df8bae1dSRodney W. Grimes 		 * Need to provide.
95df8bae1dSRodney W. Grimes 		 * 1. a way to convert between rdonly and rdwr mounts.
96df8bae1dSRodney W. Grimes 		 * 2. support for nfs exports.
97df8bae1dSRodney W. Grimes 		 */
98df8bae1dSRodney W. Grimes 		error = EOPNOTSUPP;
99df8bae1dSRodney W. Grimes 		goto bad;
100df8bae1dSRodney W. Grimes 	}
101df8bae1dSRodney W. Grimes 
102df8bae1dSRodney W. Grimes 	/*
103df8bae1dSRodney W. Grimes 	 * Take a copy of the process's credentials.  This isn't
104df8bae1dSRodney W. Grimes 	 * quite right since the euid will always be zero and we
105df8bae1dSRodney W. Grimes 	 * want to get the "real" users credentials.  So fix up
106df8bae1dSRodney W. Grimes 	 * the uid field after taking the copy.
107df8bae1dSRodney W. Grimes 	 */
108df8bae1dSRodney W. Grimes 	cred = crdup(p->p_ucred);
109df8bae1dSRodney W. Grimes 	cred->cr_uid = p->p_cred->p_ruid;
110df8bae1dSRodney W. Grimes 
111df8bae1dSRodney W. Grimes 	/*
112df8bae1dSRodney W. Grimes 	 * Ensure the *real* user has write permission on the
113df8bae1dSRodney W. Grimes 	 * mounted-on directory.  This allows the mount_union
114df8bae1dSRodney W. Grimes 	 * command to be made setuid root so allowing anyone
115df8bae1dSRodney W. Grimes 	 * to do union mounts onto any directory on which they
116df8bae1dSRodney W. Grimes 	 * have write permission and which they also own.
117df8bae1dSRodney W. Grimes 	 */
118df8bae1dSRodney W. Grimes 	error = VOP_GETATTR(mp->mnt_vnodecovered, &va, cred, p);
119df8bae1dSRodney W. Grimes 	if (error)
120df8bae1dSRodney W. Grimes 		goto bad;
121df8bae1dSRodney W. Grimes 	if ((va.va_uid != cred->cr_uid) &&
122df8bae1dSRodney W. Grimes 	    (cred->cr_uid != 0)) {
123df8bae1dSRodney W. Grimes 		error = EACCES;
124df8bae1dSRodney W. Grimes 		goto bad;
125df8bae1dSRodney W. Grimes 	}
126df8bae1dSRodney W. Grimes 	error = VOP_ACCESS(mp->mnt_vnodecovered, VWRITE, cred, p);
127df8bae1dSRodney W. Grimes 	if (error)
128df8bae1dSRodney W. Grimes 		goto bad;
129df8bae1dSRodney W. Grimes 
130df8bae1dSRodney W. Grimes 	/*
131df8bae1dSRodney W. Grimes 	 * Get argument
132df8bae1dSRodney W. Grimes 	 */
1333a773ad0SPoul-Henning Kamp 	error = copyin(data, (caddr_t)&args, sizeof(struct union_args));
1343a773ad0SPoul-Henning Kamp 	if (error)
135df8bae1dSRodney W. Grimes 		goto bad;
136df8bae1dSRodney W. Grimes 
137df8bae1dSRodney W. Grimes 	lowerrootvp = mp->mnt_vnodecovered;
138df8bae1dSRodney W. Grimes 	VREF(lowerrootvp);
139df8bae1dSRodney W. Grimes 
140df8bae1dSRodney W. Grimes 	/*
141df8bae1dSRodney W. Grimes 	 * Find upper node.  Use the real process credentials,
142df8bae1dSRodney W. Grimes 	 * not the effective ones since this will have come
143df8bae1dSRodney W. Grimes 	 * through a setuid process (mount_union).  All this
144df8bae1dSRodney W. Grimes 	 * messing around with permissions is entirely bogus
145df8bae1dSRodney W. Grimes 	 * and should be removed by allowing any user straight
146df8bae1dSRodney W. Grimes 	 * past the mount system call.
147df8bae1dSRodney W. Grimes 	 */
148df8bae1dSRodney W. Grimes 	scred = p->p_ucred;
149df8bae1dSRodney W. Grimes 	p->p_ucred = cred;
150df8bae1dSRodney W. Grimes 	NDINIT(ndp, LOOKUP, FOLLOW|WANTPARENT,
151df8bae1dSRodney W. Grimes 	       UIO_USERSPACE, args.target, p);
152df8bae1dSRodney W. Grimes 	p->p_ucred = scred;
153df8bae1dSRodney W. Grimes 
1543a773ad0SPoul-Henning Kamp 	error = namei(ndp);
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 
162df8bae1dSRodney W. Grimes 	if (upperrootvp->v_type != VDIR) {
163df8bae1dSRodney W. Grimes 		error = EINVAL;
164df8bae1dSRodney W. Grimes 		goto bad;
165df8bae1dSRodney W. Grimes 	}
166df8bae1dSRodney W. Grimes 
167df8bae1dSRodney W. Grimes 	um = (struct union_mount *) malloc(sizeof(struct union_mount),
168df8bae1dSRodney W. Grimes 				M_UFSMNT, M_WAITOK);	/* XXX */
169df8bae1dSRodney W. Grimes 
170df8bae1dSRodney W. Grimes 	/*
171df8bae1dSRodney W. Grimes 	 * Keep a held reference to the target vnodes.
172df8bae1dSRodney W. Grimes 	 * They are vrele'd in union_unmount.
173df8bae1dSRodney W. Grimes 	 *
174df8bae1dSRodney W. Grimes 	 * Depending on the _BELOW flag, the filesystems are
175df8bae1dSRodney W. Grimes 	 * viewed in a different order.  In effect, this is the
176df8bae1dSRodney W. Grimes 	 * same as providing a mount under option to the mount syscall.
177df8bae1dSRodney W. Grimes 	 */
178df8bae1dSRodney W. Grimes 
179df8bae1dSRodney W. Grimes 	um->um_op = args.mntflags & UNMNT_OPMASK;
180df8bae1dSRodney W. Grimes 	switch (um->um_op) {
181df8bae1dSRodney W. Grimes 	case UNMNT_ABOVE:
182df8bae1dSRodney W. Grimes 		um->um_lowervp = lowerrootvp;
183df8bae1dSRodney W. Grimes 		um->um_uppervp = upperrootvp;
184df8bae1dSRodney W. Grimes 		break;
185df8bae1dSRodney W. Grimes 
186df8bae1dSRodney W. Grimes 	case UNMNT_BELOW:
187df8bae1dSRodney W. Grimes 		um->um_lowervp = upperrootvp;
188df8bae1dSRodney W. Grimes 		um->um_uppervp = lowerrootvp;
189df8bae1dSRodney W. Grimes 		break;
190df8bae1dSRodney W. Grimes 
191df8bae1dSRodney W. Grimes 	case UNMNT_REPLACE:
192df8bae1dSRodney W. Grimes 		vrele(lowerrootvp);
193df8bae1dSRodney W. Grimes 		lowerrootvp = NULLVP;
194df8bae1dSRodney W. Grimes 		um->um_uppervp = upperrootvp;
195df8bae1dSRodney W. Grimes 		um->um_lowervp = lowerrootvp;
196df8bae1dSRodney W. Grimes 		break;
197df8bae1dSRodney W. Grimes 
198df8bae1dSRodney W. Grimes 	default:
199df8bae1dSRodney W. Grimes 		error = EINVAL;
200df8bae1dSRodney W. Grimes 		goto bad;
201df8bae1dSRodney W. Grimes 	}
202df8bae1dSRodney W. Grimes 
203df8bae1dSRodney W. Grimes 	um->um_cred = cred;
204df8bae1dSRodney W. Grimes 	um->um_cmode = UN_DIRMODE &~ p->p_fd->fd_cmask;
205df8bae1dSRodney W. Grimes 
206df8bae1dSRodney W. Grimes 	/*
207df8bae1dSRodney W. Grimes 	 * Depending on what you think the MNT_LOCAL flag might mean,
208df8bae1dSRodney W. Grimes 	 * you may want the && to be || on the conditional below.
209df8bae1dSRodney W. Grimes 	 * At the moment it has been defined that the filesystem is
210df8bae1dSRodney W. Grimes 	 * only local if it is all local, ie the MNT_LOCAL flag implies
211df8bae1dSRodney W. Grimes 	 * that the entire namespace is local.  If you think the MNT_LOCAL
212df8bae1dSRodney W. Grimes 	 * flag implies that some of the files might be stored locally
213df8bae1dSRodney W. Grimes 	 * then you will want to change the conditional.
214df8bae1dSRodney W. Grimes 	 */
215df8bae1dSRodney W. Grimes 	if (um->um_op == UNMNT_ABOVE) {
216df8bae1dSRodney W. Grimes 		if (((um->um_lowervp == NULLVP) ||
217df8bae1dSRodney W. Grimes 		     (um->um_lowervp->v_mount->mnt_flag & MNT_LOCAL)) &&
218df8bae1dSRodney W. Grimes 		    (um->um_uppervp->v_mount->mnt_flag & MNT_LOCAL))
219df8bae1dSRodney W. Grimes 			mp->mnt_flag |= MNT_LOCAL;
220df8bae1dSRodney W. Grimes 	}
221df8bae1dSRodney W. Grimes 
222df8bae1dSRodney W. Grimes 	/*
223df8bae1dSRodney W. Grimes 	 * Copy in the upper layer's RDONLY flag.  This is for the benefit
224df8bae1dSRodney W. Grimes 	 * of lookup() which explicitly checks the flag, rather than asking
225df8bae1dSRodney W. Grimes 	 * the filesystem for it's own opinion.  This means, that an update
226df8bae1dSRodney W. Grimes 	 * mount of the underlying filesystem to go from rdonly to rdwr
227df8bae1dSRodney W. Grimes 	 * will leave the unioned view as read-only.
228df8bae1dSRodney W. Grimes 	 */
229df8bae1dSRodney W. Grimes 	mp->mnt_flag |= (um->um_uppervp->v_mount->mnt_flag & MNT_RDONLY);
230df8bae1dSRodney W. Grimes 
231df8bae1dSRodney W. Grimes 	/*
232df8bae1dSRodney W. Grimes 	 * This is a user mount.  Privilege check for unmount
233df8bae1dSRodney W. Grimes 	 * will be done in union_unmount.
234df8bae1dSRodney W. Grimes 	 */
235df8bae1dSRodney W. Grimes 	mp->mnt_flag |= MNT_USER;
236df8bae1dSRodney W. Grimes 
237df8bae1dSRodney W. Grimes 	mp->mnt_data = (qaddr_t) um;
238df8bae1dSRodney W. Grimes 	getnewfsid(mp, MOUNT_UNION);
239df8bae1dSRodney W. Grimes 
240df8bae1dSRodney W. Grimes 	(void) copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
241df8bae1dSRodney W. Grimes 	bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
242df8bae1dSRodney W. Grimes 
243df8bae1dSRodney W. Grimes 	switch (um->um_op) {
244df8bae1dSRodney W. Grimes 	case UNMNT_ABOVE:
245df8bae1dSRodney W. Grimes 		cp = "<above>";
246df8bae1dSRodney W. Grimes 		break;
247df8bae1dSRodney W. Grimes 	case UNMNT_BELOW:
248df8bae1dSRodney W. Grimes 		cp = "<below>";
249df8bae1dSRodney W. Grimes 		break;
250df8bae1dSRodney W. Grimes 	case UNMNT_REPLACE:
251df8bae1dSRodney W. Grimes 		cp = "";
252df8bae1dSRodney W. Grimes 		break;
253df8bae1dSRodney W. Grimes 	}
254df8bae1dSRodney W. Grimes 	len = strlen(cp);
255df8bae1dSRodney W. Grimes 	bcopy(cp, mp->mnt_stat.f_mntfromname, len);
256df8bae1dSRodney W. Grimes 
257df8bae1dSRodney W. Grimes 	cp = mp->mnt_stat.f_mntfromname + len;
258df8bae1dSRodney W. Grimes 	len = MNAMELEN - len;
259df8bae1dSRodney W. Grimes 
260df8bae1dSRodney W. Grimes 	(void) copyinstr(args.target, cp, len - 1, &size);
261df8bae1dSRodney W. Grimes 	bzero(cp + size, len - size);
262df8bae1dSRodney W. Grimes 
263c4a7b7e1SDavid Greenman 	(void)union_statfs(mp, &mp->mnt_stat, p);
264c4a7b7e1SDavid Greenman 
265df8bae1dSRodney W. Grimes #ifdef UNION_DIAGNOSTIC
266df8bae1dSRodney W. Grimes 	printf("union_mount: from %s, on %s\n",
267df8bae1dSRodney W. Grimes 		mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname);
268df8bae1dSRodney W. Grimes #endif
269df8bae1dSRodney W. Grimes 	return (0);
270df8bae1dSRodney W. Grimes 
271df8bae1dSRodney W. Grimes bad:
272df8bae1dSRodney W. Grimes 	if (cred)
273df8bae1dSRodney W. Grimes 		crfree(cred);
274df8bae1dSRodney W. Grimes 	if (upperrootvp)
275df8bae1dSRodney W. Grimes 		vrele(upperrootvp);
276df8bae1dSRodney W. Grimes 	if (lowerrootvp)
277df8bae1dSRodney W. Grimes 		vrele(lowerrootvp);
278df8bae1dSRodney W. Grimes 	return (error);
279df8bae1dSRodney W. Grimes }
280df8bae1dSRodney W. Grimes 
281df8bae1dSRodney W. Grimes /*
282df8bae1dSRodney W. Grimes  * VFS start.  Nothing needed here - the start routine
283df8bae1dSRodney W. Grimes  * on the underlying filesystem(s) will have been called
284df8bae1dSRodney W. Grimes  * when that filesystem was mounted.
285df8bae1dSRodney W. Grimes  */
286df8bae1dSRodney W. Grimes int
287df8bae1dSRodney W. Grimes union_start(mp, flags, p)
288df8bae1dSRodney W. Grimes 	struct mount *mp;
289df8bae1dSRodney W. Grimes 	int flags;
290df8bae1dSRodney W. Grimes 	struct proc *p;
291df8bae1dSRodney W. Grimes {
292df8bae1dSRodney W. Grimes 
293df8bae1dSRodney W. Grimes 	return (0);
294df8bae1dSRodney W. Grimes }
295df8bae1dSRodney W. Grimes 
296df8bae1dSRodney W. Grimes /*
297df8bae1dSRodney W. Grimes  * Free reference to union layer
298df8bae1dSRodney W. Grimes  */
299df8bae1dSRodney W. Grimes int
300df8bae1dSRodney W. Grimes union_unmount(mp, mntflags, p)
301df8bae1dSRodney W. Grimes 	struct mount *mp;
302df8bae1dSRodney W. Grimes 	int mntflags;
303df8bae1dSRodney W. Grimes 	struct proc *p;
304df8bae1dSRodney W. Grimes {
305df8bae1dSRodney W. Grimes 	struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
306df8bae1dSRodney W. Grimes 	struct vnode *um_rootvp;
307df8bae1dSRodney W. Grimes 	int error;
308df8bae1dSRodney W. Grimes 	int flags = 0;
309df8bae1dSRodney W. Grimes 
310df8bae1dSRodney W. Grimes #ifdef UNION_DIAGNOSTIC
311df8bae1dSRodney W. Grimes 	printf("union_unmount(mp = %x)\n", mp);
312df8bae1dSRodney W. Grimes #endif
313df8bae1dSRodney W. Grimes 
314df8bae1dSRodney W. Grimes 	/* only the mounter, or superuser can unmount */
315df8bae1dSRodney W. Grimes 	if ((p->p_cred->p_ruid != um->um_cred->cr_uid) &&
316df8bae1dSRodney W. Grimes 	    (error = suser(p->p_ucred, &p->p_acflag)))
317df8bae1dSRodney W. Grimes 		return (error);
318df8bae1dSRodney W. Grimes 
319df8bae1dSRodney W. Grimes 	if (mntflags & MNT_FORCE) {
320df8bae1dSRodney W. Grimes 		/* union can never be rootfs so don't check for it */
321df8bae1dSRodney W. Grimes 		if (!doforce)
322df8bae1dSRodney W. Grimes 			return (EINVAL);
323df8bae1dSRodney W. Grimes 		flags |= FORCECLOSE;
324df8bae1dSRodney W. Grimes 	}
325df8bae1dSRodney W. Grimes 
3263a773ad0SPoul-Henning Kamp 	error = union_root(mp, &um_rootvp);
3273a773ad0SPoul-Henning Kamp 	if (error)
328df8bae1dSRodney W. Grimes 		return (error);
329df8bae1dSRodney W. Grimes 	if (um_rootvp->v_usecount > 1) {
330df8bae1dSRodney W. Grimes 		vput(um_rootvp);
331df8bae1dSRodney W. Grimes 		return (EBUSY);
332df8bae1dSRodney W. Grimes 	}
3333a773ad0SPoul-Henning Kamp 	error = vflush(mp, um_rootvp, flags);
3343a773ad0SPoul-Henning Kamp 	if (error) {
335df8bae1dSRodney W. Grimes 		vput(um_rootvp);
336df8bae1dSRodney W. Grimes 		return (error);
337df8bae1dSRodney W. Grimes 	}
338df8bae1dSRodney W. Grimes 
339df8bae1dSRodney W. Grimes #ifdef UNION_DIAGNOSTIC
340df8bae1dSRodney W. Grimes 	vprint("alias root of lower", um_rootvp);
341df8bae1dSRodney W. Grimes #endif
342df8bae1dSRodney W. Grimes 	/*
343df8bae1dSRodney W. Grimes 	 * Discard references to upper and lower target vnodes.
344df8bae1dSRodney W. Grimes 	 */
345df8bae1dSRodney W. Grimes 	if (um->um_lowervp)
346df8bae1dSRodney W. Grimes 		vrele(um->um_lowervp);
347df8bae1dSRodney W. Grimes 	vrele(um->um_uppervp);
348df8bae1dSRodney W. Grimes 	crfree(um->um_cred);
349df8bae1dSRodney W. Grimes 	/*
350df8bae1dSRodney W. Grimes 	 * Release reference on underlying root vnode
351df8bae1dSRodney W. Grimes 	 */
352df8bae1dSRodney W. Grimes 	vput(um_rootvp);
353df8bae1dSRodney W. Grimes 	/*
354df8bae1dSRodney W. Grimes 	 * And blow it away for future re-use
355df8bae1dSRodney W. Grimes 	 */
356df8bae1dSRodney W. Grimes 	vgone(um_rootvp);
357df8bae1dSRodney W. Grimes 	/*
358df8bae1dSRodney W. Grimes 	 * Finally, throw away the union_mount structure
359df8bae1dSRodney W. Grimes 	 */
360df8bae1dSRodney W. Grimes 	free(mp->mnt_data, M_UFSMNT);	/* XXX */
361df8bae1dSRodney W. Grimes 	mp->mnt_data = 0;
362df8bae1dSRodney W. Grimes 	return (0);
363df8bae1dSRodney W. Grimes }
364df8bae1dSRodney W. Grimes 
365df8bae1dSRodney W. Grimes int
366df8bae1dSRodney W. Grimes union_root(mp, vpp)
367df8bae1dSRodney W. Grimes 	struct mount *mp;
368df8bae1dSRodney W. Grimes 	struct vnode **vpp;
369df8bae1dSRodney W. Grimes {
370df8bae1dSRodney W. Grimes 	struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
371df8bae1dSRodney W. Grimes 	int error;
372df8bae1dSRodney W. Grimes 	int loselock;
373df8bae1dSRodney W. Grimes 
374df8bae1dSRodney W. Grimes #ifdef UNION_DIAGNOSTIC
375df8bae1dSRodney W. Grimes 	printf("union_root(mp = %x, lvp = %x, uvp = %x)\n", mp,
376df8bae1dSRodney W. Grimes 			um->um_lowervp,
377df8bae1dSRodney W. Grimes 			um->um_uppervp);
378df8bae1dSRodney W. Grimes #endif
379df8bae1dSRodney W. Grimes 
380df8bae1dSRodney W. Grimes 	/*
381df8bae1dSRodney W. Grimes 	 * Return locked reference to root.
382df8bae1dSRodney W. Grimes 	 */
383df8bae1dSRodney W. Grimes 	VREF(um->um_uppervp);
384df8bae1dSRodney W. Grimes 	if ((um->um_op == UNMNT_BELOW) &&
385df8bae1dSRodney W. Grimes 	     VOP_ISLOCKED(um->um_uppervp)) {
386df8bae1dSRodney W. Grimes 		loselock = 1;
387df8bae1dSRodney W. Grimes 	} else {
388df8bae1dSRodney W. Grimes 		VOP_LOCK(um->um_uppervp);
389df8bae1dSRodney W. Grimes 		loselock = 0;
390df8bae1dSRodney W. Grimes 	}
391df8bae1dSRodney W. Grimes 	if (um->um_lowervp)
392df8bae1dSRodney W. Grimes 		VREF(um->um_lowervp);
393df8bae1dSRodney W. Grimes 	error = union_allocvp(vpp, mp,
394df8bae1dSRodney W. Grimes 			      (struct vnode *) 0,
395df8bae1dSRodney W. Grimes 			      (struct vnode *) 0,
396df8bae1dSRodney W. Grimes 			      (struct componentname *) 0,
397df8bae1dSRodney W. Grimes 			      um->um_uppervp,
398df8bae1dSRodney W. Grimes 			      um->um_lowervp);
399df8bae1dSRodney W. Grimes 
400df8bae1dSRodney W. Grimes 	if (error) {
401df8bae1dSRodney W. Grimes 		if (!loselock)
402df8bae1dSRodney W. Grimes 			VOP_UNLOCK(um->um_uppervp);
403df8bae1dSRodney W. Grimes 		vrele(um->um_uppervp);
404df8bae1dSRodney W. Grimes 		if (um->um_lowervp)
405df8bae1dSRodney W. Grimes 			vrele(um->um_lowervp);
406df8bae1dSRodney W. Grimes 	} else {
407df8bae1dSRodney W. Grimes 		(*vpp)->v_flag |= VROOT;
408df8bae1dSRodney W. Grimes 		if (loselock)
409df8bae1dSRodney W. Grimes 			VTOUNION(*vpp)->un_flags &= ~UN_ULOCK;
410df8bae1dSRodney W. Grimes 	}
411df8bae1dSRodney W. Grimes 
412df8bae1dSRodney W. Grimes 	return (error);
413df8bae1dSRodney W. Grimes }
414df8bae1dSRodney W. Grimes 
415df8bae1dSRodney W. Grimes int
416df8bae1dSRodney W. Grimes union_quotactl(mp, cmd, uid, arg, p)
417df8bae1dSRodney W. Grimes 	struct mount *mp;
418df8bae1dSRodney W. Grimes 	int cmd;
419df8bae1dSRodney W. Grimes 	uid_t uid;
420df8bae1dSRodney W. Grimes 	caddr_t arg;
421df8bae1dSRodney W. Grimes 	struct proc *p;
422df8bae1dSRodney W. Grimes {
423df8bae1dSRodney W. Grimes 
424df8bae1dSRodney W. Grimes 	return (EOPNOTSUPP);
425df8bae1dSRodney W. Grimes }
426df8bae1dSRodney W. Grimes 
427df8bae1dSRodney W. Grimes int
428df8bae1dSRodney W. Grimes union_statfs(mp, sbp, p)
429df8bae1dSRodney W. Grimes 	struct mount *mp;
430df8bae1dSRodney W. Grimes 	struct statfs *sbp;
431df8bae1dSRodney W. Grimes 	struct proc *p;
432df8bae1dSRodney W. Grimes {
433df8bae1dSRodney W. Grimes 	int error;
434df8bae1dSRodney W. Grimes 	struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
435df8bae1dSRodney W. Grimes 	struct statfs mstat;
436df8bae1dSRodney W. Grimes 	int lbsize;
437df8bae1dSRodney W. Grimes 
438df8bae1dSRodney W. Grimes #ifdef UNION_DIAGNOSTIC
439df8bae1dSRodney W. Grimes 	printf("union_statfs(mp = %x, lvp = %x, uvp = %x)\n", mp,
440df8bae1dSRodney W. Grimes 			um->um_lowervp,
441df8bae1dSRodney W. Grimes 	       		um->um_uppervp);
442df8bae1dSRodney W. Grimes #endif
443df8bae1dSRodney W. Grimes 
444df8bae1dSRodney W. Grimes 	bzero(&mstat, sizeof(mstat));
445df8bae1dSRodney W. Grimes 
446df8bae1dSRodney W. Grimes 	if (um->um_lowervp) {
447df8bae1dSRodney W. Grimes 		error = VFS_STATFS(um->um_lowervp->v_mount, &mstat, p);
448df8bae1dSRodney W. Grimes 		if (error)
449df8bae1dSRodney W. Grimes 			return (error);
450df8bae1dSRodney W. Grimes 	}
451df8bae1dSRodney W. Grimes 
452df8bae1dSRodney W. Grimes 	/* now copy across the "interesting" information and fake the rest */
453df8bae1dSRodney W. Grimes #if 0
454df8bae1dSRodney W. Grimes 	sbp->f_type = mstat.f_type;
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 #endif
459df8bae1dSRodney W. Grimes 	lbsize = mstat.f_bsize;
460df8bae1dSRodney W. Grimes 	sbp->f_blocks = mstat.f_blocks;
461df8bae1dSRodney W. Grimes 	sbp->f_bfree = mstat.f_bfree;
462df8bae1dSRodney W. Grimes 	sbp->f_bavail = mstat.f_bavail;
463df8bae1dSRodney W. Grimes 	sbp->f_files = mstat.f_files;
464df8bae1dSRodney W. Grimes 	sbp->f_ffree = mstat.f_ffree;
465df8bae1dSRodney W. Grimes 
466df8bae1dSRodney W. Grimes 	error = VFS_STATFS(um->um_uppervp->v_mount, &mstat, p);
467df8bae1dSRodney W. Grimes 	if (error)
468df8bae1dSRodney W. Grimes 		return (error);
469df8bae1dSRodney W. Grimes 
470df8bae1dSRodney W. Grimes 	sbp->f_type = MOUNT_UNION;
471df8bae1dSRodney W. Grimes 	sbp->f_flags = mstat.f_flags;
472df8bae1dSRodney W. Grimes 	sbp->f_bsize = mstat.f_bsize;
473df8bae1dSRodney W. Grimes 	sbp->f_iosize = mstat.f_iosize;
474df8bae1dSRodney W. Grimes 
475df8bae1dSRodney W. Grimes 	/*
476df8bae1dSRodney W. Grimes 	 * if the lower and upper blocksizes differ, then frig the
477df8bae1dSRodney W. Grimes 	 * block counts so that the sizes reported by df make some
478df8bae1dSRodney W. Grimes 	 * kind of sense.  none of this makes sense though.
479df8bae1dSRodney W. Grimes 	 */
480df8bae1dSRodney W. Grimes 
481df8bae1dSRodney W. Grimes 	if (mstat.f_bsize != lbsize) {
482df8bae1dSRodney W. Grimes 		sbp->f_blocks = sbp->f_blocks * lbsize / mstat.f_bsize;
483df8bae1dSRodney W. Grimes 		sbp->f_bfree = sbp->f_bfree * lbsize / mstat.f_bsize;
484df8bae1dSRodney W. Grimes 		sbp->f_bavail = sbp->f_bavail * lbsize / mstat.f_bsize;
485df8bae1dSRodney W. Grimes 	}
486df8bae1dSRodney W. Grimes 	sbp->f_blocks += mstat.f_blocks;
487df8bae1dSRodney W. Grimes 	sbp->f_bfree += mstat.f_bfree;
488df8bae1dSRodney W. Grimes 	sbp->f_bavail += mstat.f_bavail;
489df8bae1dSRodney W. Grimes 	sbp->f_files += mstat.f_files;
490df8bae1dSRodney W. Grimes 	sbp->f_ffree += mstat.f_ffree;
491df8bae1dSRodney W. Grimes 
492df8bae1dSRodney W. Grimes 	if (sbp != &mp->mnt_stat) {
493df8bae1dSRodney W. Grimes 		bcopy(&mp->mnt_stat.f_fsid, &sbp->f_fsid, sizeof(sbp->f_fsid));
494df8bae1dSRodney W. Grimes 		bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
495df8bae1dSRodney W. Grimes 		bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
496df8bae1dSRodney W. Grimes 	}
497df8bae1dSRodney W. Grimes 	return (0);
498df8bae1dSRodney W. Grimes }
499df8bae1dSRodney W. Grimes 
500df8bae1dSRodney W. Grimes int
501df8bae1dSRodney W. Grimes union_sync(mp, waitfor, cred, p)
502df8bae1dSRodney W. Grimes 	struct mount *mp;
503df8bae1dSRodney W. Grimes 	int waitfor;
504df8bae1dSRodney W. Grimes 	struct ucred *cred;
505df8bae1dSRodney W. Grimes 	struct proc *p;
506df8bae1dSRodney W. Grimes {
507df8bae1dSRodney W. Grimes 
508df8bae1dSRodney W. Grimes 	/*
509df8bae1dSRodney W. Grimes 	 * XXX - Assumes no data cached at union layer.
510df8bae1dSRodney W. Grimes 	 */
511df8bae1dSRodney W. Grimes 	return (0);
512df8bae1dSRodney W. Grimes }
513df8bae1dSRodney W. Grimes 
514df8bae1dSRodney W. Grimes int
515df8bae1dSRodney W. Grimes union_vget(mp, ino, vpp)
516df8bae1dSRodney W. Grimes 	struct mount *mp;
517df8bae1dSRodney W. Grimes 	ino_t ino;
518df8bae1dSRodney W. Grimes 	struct vnode **vpp;
519df8bae1dSRodney W. Grimes {
520df8bae1dSRodney W. Grimes 
521df8bae1dSRodney W. Grimes 	return (EOPNOTSUPP);
522df8bae1dSRodney W. Grimes }
523df8bae1dSRodney W. Grimes 
524df8bae1dSRodney W. Grimes int
525df8bae1dSRodney W. Grimes union_fhtovp(mp, fidp, nam, vpp, exflagsp, credanonp)
526df8bae1dSRodney W. Grimes 	struct mount *mp;
527df8bae1dSRodney W. Grimes 	struct fid *fidp;
528df8bae1dSRodney W. Grimes 	struct mbuf *nam;
529df8bae1dSRodney W. Grimes 	struct vnode **vpp;
530df8bae1dSRodney W. Grimes 	int *exflagsp;
531df8bae1dSRodney W. Grimes 	struct ucred **credanonp;
532df8bae1dSRodney W. Grimes {
533df8bae1dSRodney W. Grimes 
534df8bae1dSRodney W. Grimes 	return (EOPNOTSUPP);
535df8bae1dSRodney W. Grimes }
536df8bae1dSRodney W. Grimes 
537df8bae1dSRodney W. Grimes int
538df8bae1dSRodney W. Grimes union_vptofh(vp, fhp)
539df8bae1dSRodney W. Grimes 	struct vnode *vp;
540df8bae1dSRodney W. Grimes 	struct fid *fhp;
541df8bae1dSRodney W. Grimes {
542df8bae1dSRodney W. Grimes 
543df8bae1dSRodney W. Grimes 	return (EOPNOTSUPP);
544df8bae1dSRodney W. Grimes }
545df8bae1dSRodney W. Grimes 
546df8bae1dSRodney W. Grimes int union_init __P((void));
547df8bae1dSRodney W. Grimes 
548df8bae1dSRodney W. Grimes 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 
562c901836cSGarrett Wollman VFS_SET(union_vfsops, union, MOUNT_UNION, 0);
563