xref: /netbsd-src/sys/fs/union/union_vfsops.c (revision 0ad7109896ab30655ef90ac44584554ddcfaf33a)
1*0ad71098Sthorpej /*	$NetBSD: union_vfsops.c,v 1.88 2024/05/18 00:04:46 thorpej Exp $	*/
2a0deb3e9Sjdolecek 
3a0deb3e9Sjdolecek /*
4a0deb3e9Sjdolecek  * Copyright (c) 1994 The Regents of the University of California.
5aad01611Sagc  * All rights reserved.
6aad01611Sagc  *
7aad01611Sagc  * This code is derived from software donated to Berkeley by
8aad01611Sagc  * Jan-Simon Pendry.
9aad01611Sagc  *
10aad01611Sagc  * Redistribution and use in source and binary forms, with or without
11aad01611Sagc  * modification, are permitted provided that the following conditions
12aad01611Sagc  * are met:
13aad01611Sagc  * 1. Redistributions of source code must retain the above copyright
14aad01611Sagc  *    notice, this list of conditions and the following disclaimer.
15aad01611Sagc  * 2. Redistributions in binary form must reproduce the above copyright
16aad01611Sagc  *    notice, this list of conditions and the following disclaimer in the
17aad01611Sagc  *    documentation and/or other materials provided with the distribution.
18aad01611Sagc  * 3. Neither the name of the University nor the names of its contributors
19aad01611Sagc  *    may be used to endorse or promote products derived from this software
20aad01611Sagc  *    without specific prior written permission.
21aad01611Sagc  *
22aad01611Sagc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23aad01611Sagc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24aad01611Sagc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25aad01611Sagc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26aad01611Sagc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27aad01611Sagc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28aad01611Sagc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29aad01611Sagc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30aad01611Sagc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31aad01611Sagc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32aad01611Sagc  * SUCH DAMAGE.
33aad01611Sagc  *
34aad01611Sagc  *	@(#)union_vfsops.c	8.20 (Berkeley) 5/20/95
35aad01611Sagc  */
36aad01611Sagc 
37aad01611Sagc /*
38a0deb3e9Sjdolecek  * Copyright (c) 1994 Jan-Simon Pendry.
39a0deb3e9Sjdolecek  * All rights reserved.
40a0deb3e9Sjdolecek  *
41a0deb3e9Sjdolecek  * This code is derived from software donated to Berkeley by
42a0deb3e9Sjdolecek  * Jan-Simon Pendry.
43a0deb3e9Sjdolecek  *
44a0deb3e9Sjdolecek  * Redistribution and use in source and binary forms, with or without
45a0deb3e9Sjdolecek  * modification, are permitted provided that the following conditions
46a0deb3e9Sjdolecek  * are met:
47a0deb3e9Sjdolecek  * 1. Redistributions of source code must retain the above copyright
48a0deb3e9Sjdolecek  *    notice, this list of conditions and the following disclaimer.
49a0deb3e9Sjdolecek  * 2. Redistributions in binary form must reproduce the above copyright
50a0deb3e9Sjdolecek  *    notice, this list of conditions and the following disclaimer in the
51a0deb3e9Sjdolecek  *    documentation and/or other materials provided with the distribution.
52a0deb3e9Sjdolecek  * 3. All advertising materials mentioning features or use of this software
53a0deb3e9Sjdolecek  *    must display the following acknowledgement:
54a0deb3e9Sjdolecek  *	This product includes software developed by the University of
55a0deb3e9Sjdolecek  *	California, Berkeley and its contributors.
56a0deb3e9Sjdolecek  * 4. Neither the name of the University nor the names of its contributors
57a0deb3e9Sjdolecek  *    may be used to endorse or promote products derived from this software
58a0deb3e9Sjdolecek  *    without specific prior written permission.
59a0deb3e9Sjdolecek  *
60a0deb3e9Sjdolecek  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
61a0deb3e9Sjdolecek  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62a0deb3e9Sjdolecek  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
63a0deb3e9Sjdolecek  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
64a0deb3e9Sjdolecek  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
65a0deb3e9Sjdolecek  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
66a0deb3e9Sjdolecek  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67a0deb3e9Sjdolecek  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68a0deb3e9Sjdolecek  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69a0deb3e9Sjdolecek  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70a0deb3e9Sjdolecek  * SUCH DAMAGE.
71a0deb3e9Sjdolecek  *
72a0deb3e9Sjdolecek  *	@(#)union_vfsops.c	8.20 (Berkeley) 5/20/95
73a0deb3e9Sjdolecek  */
74a0deb3e9Sjdolecek 
75a0deb3e9Sjdolecek /*
76a0deb3e9Sjdolecek  * Union Layer
77a0deb3e9Sjdolecek  */
78a0deb3e9Sjdolecek 
79a0deb3e9Sjdolecek #include <sys/cdefs.h>
80*0ad71098Sthorpej __KERNEL_RCSID(0, "$NetBSD: union_vfsops.c,v 1.88 2024/05/18 00:04:46 thorpej Exp $");
81a0deb3e9Sjdolecek 
82a0deb3e9Sjdolecek #include <sys/param.h>
83a0deb3e9Sjdolecek #include <sys/systm.h>
8413f8d2ceSatatat #include <sys/sysctl.h>
85a0deb3e9Sjdolecek #include <sys/time.h>
86a0deb3e9Sjdolecek #include <sys/proc.h>
87a0deb3e9Sjdolecek #include <sys/vnode.h>
88a0deb3e9Sjdolecek #include <sys/mount.h>
89a0deb3e9Sjdolecek #include <sys/namei.h>
90a0deb3e9Sjdolecek #include <sys/filedesc.h>
91a0deb3e9Sjdolecek #include <sys/queue.h>
92a0deb3e9Sjdolecek #include <sys/stat.h>
93fc9422c9Selad #include <sys/kauth.h>
94a1221b6dSrumble #include <sys/module.h>
95a0deb3e9Sjdolecek 
96326db3aaShannken #include <miscfs/genfs/genfs.h>
97a0deb3e9Sjdolecek #include <fs/union/union.h>
98a0deb3e9Sjdolecek 
99e73db952Spooka MODULE(MODULE_CLASS_VFS, union, NULL);
100a1221b6dSrumble 
101a0deb3e9Sjdolecek /*
102a0deb3e9Sjdolecek  * Mount union filesystem
103a0deb3e9Sjdolecek  */
104a0deb3e9Sjdolecek int
union_mount(struct mount * mp,const char * path,void * data,size_t * data_len)1059a1bbaabSmatt union_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
106a0deb3e9Sjdolecek {
10761e8303eSpooka 	struct lwp *l = curlwp;
108a0deb3e9Sjdolecek 	int error = 0;
1092721ab6cSdsl 	struct union_args *args = data;
110a0deb3e9Sjdolecek 	struct vnode *lowerrootvp = NULLVP;
111a0deb3e9Sjdolecek 	struct vnode *upperrootvp = NULLVP;
112a0deb3e9Sjdolecek 	struct union_mount *um = 0;
113a29d4b25Schristos 	const char *cp;
114a29d4b25Schristos 	char *xp;
115a0deb3e9Sjdolecek 	int len;
116a0deb3e9Sjdolecek 	size_t size;
117a0deb3e9Sjdolecek 
11823f76b6dSmaxv 	if (args == NULL)
11923f76b6dSmaxv 		return EINVAL;
1202721ab6cSdsl 	if (*data_len < sizeof *args)
1212721ab6cSdsl 		return EINVAL;
1222721ab6cSdsl 
123a0deb3e9Sjdolecek #ifdef UNION_DIAGNOSTIC
124ae87d18fSchristos 	printf("%s(mp = %p)\n", __func__, mp);
125a0deb3e9Sjdolecek #endif
126a0deb3e9Sjdolecek 
127a0deb3e9Sjdolecek 	if (mp->mnt_flag & MNT_GETARGS) {
128a0deb3e9Sjdolecek 		um = MOUNTTOUNIONMOUNT(mp);
129a0deb3e9Sjdolecek 		if (um == NULL)
130a0deb3e9Sjdolecek 			return EIO;
1312721ab6cSdsl 		args->target = NULL;
1322721ab6cSdsl 		args->mntflags = um->um_op;
1332721ab6cSdsl 		*data_len = sizeof *args;
1342721ab6cSdsl 		return 0;
135a0deb3e9Sjdolecek 	}
136a0deb3e9Sjdolecek 	/*
137a0deb3e9Sjdolecek 	 * Update is a no-op
138a0deb3e9Sjdolecek 	 */
139a0deb3e9Sjdolecek 	if (mp->mnt_flag & MNT_UPDATE) {
140a0deb3e9Sjdolecek 		/*
141a0deb3e9Sjdolecek 		 * Need to provide.
142a0deb3e9Sjdolecek 		 * 1. a way to convert between rdonly and rdwr mounts.
143a0deb3e9Sjdolecek 		 * 2. support for nfs exports.
144a0deb3e9Sjdolecek 		 */
145a0deb3e9Sjdolecek 		error = EOPNOTSUPP;
146a0deb3e9Sjdolecek 		goto bad;
147a0deb3e9Sjdolecek 	}
148a0deb3e9Sjdolecek 
149a0deb3e9Sjdolecek 	lowerrootvp = mp->mnt_vnodecovered;
150c3183f32Spooka 	vref(lowerrootvp);
151a0deb3e9Sjdolecek 
152a0deb3e9Sjdolecek 	/*
153a0deb3e9Sjdolecek 	 * Find upper node.
154a0deb3e9Sjdolecek 	 */
155effcf1afSdholland 	error = namei_simple_user(args->target,
156effcf1afSdholland 	    NSM_FOLLOW_NOEMULROOT, &upperrootvp);
157effcf1afSdholland 	if (error != 0)
158a0deb3e9Sjdolecek 		goto bad;
159a0deb3e9Sjdolecek 
160a0deb3e9Sjdolecek 	if (upperrootvp->v_type != VDIR) {
161a0deb3e9Sjdolecek 		error = EINVAL;
162a0deb3e9Sjdolecek 		goto bad;
163a0deb3e9Sjdolecek 	}
164a0deb3e9Sjdolecek 
165ae87d18fSchristos 	um = kmem_zalloc(sizeof(*um), KM_SLEEP);
166a0deb3e9Sjdolecek 
167a0deb3e9Sjdolecek 	/*
168a0deb3e9Sjdolecek 	 * Keep a held reference to the target vnodes.
169a0deb3e9Sjdolecek 	 * They are vrele'd in union_unmount.
170a0deb3e9Sjdolecek 	 *
171a0deb3e9Sjdolecek 	 * Depending on the _BELOW flag, the filesystems are
172a0deb3e9Sjdolecek 	 * viewed in a different order.  In effect, this is the
173a0deb3e9Sjdolecek 	 * same as providing a mount under option to the mount syscall.
174a0deb3e9Sjdolecek 	 */
175a0deb3e9Sjdolecek 
1762721ab6cSdsl 	um->um_op = args->mntflags & UNMNT_OPMASK;
177a0deb3e9Sjdolecek 	switch (um->um_op) {
178a0deb3e9Sjdolecek 	case UNMNT_ABOVE:
179a0deb3e9Sjdolecek 		um->um_lowervp = lowerrootvp;
180a0deb3e9Sjdolecek 		um->um_uppervp = upperrootvp;
181a0deb3e9Sjdolecek 		break;
182a0deb3e9Sjdolecek 
183a0deb3e9Sjdolecek 	case UNMNT_BELOW:
184a0deb3e9Sjdolecek 		um->um_lowervp = upperrootvp;
185a0deb3e9Sjdolecek 		um->um_uppervp = lowerrootvp;
186a0deb3e9Sjdolecek 		break;
187a0deb3e9Sjdolecek 
188a0deb3e9Sjdolecek 	case UNMNT_REPLACE:
189a0deb3e9Sjdolecek 		vrele(lowerrootvp);
190a0deb3e9Sjdolecek 		lowerrootvp = NULLVP;
191a0deb3e9Sjdolecek 		um->um_uppervp = upperrootvp;
192a0deb3e9Sjdolecek 		um->um_lowervp = lowerrootvp;
193a0deb3e9Sjdolecek 		break;
194a0deb3e9Sjdolecek 
195a0deb3e9Sjdolecek 	default:
196a0deb3e9Sjdolecek 		error = EINVAL;
197a0deb3e9Sjdolecek 		goto bad;
198a0deb3e9Sjdolecek 	}
199a0deb3e9Sjdolecek 
200daf1598eShannken 	/*
201daf1598eShannken 	 * This mount is mp-safe if both lower mounts are mp-safe.
202daf1598eShannken 	 */
203daf1598eShannken 
204daf1598eShannken 	if (((um->um_lowervp == NULLVP) ||
205daf1598eShannken 	    (um->um_lowervp->v_mount->mnt_iflag & IMNT_MPSAFE)) &&
206daf1598eShannken 	    (um->um_uppervp->v_mount->mnt_iflag & IMNT_MPSAFE))
2074e825063Shannken 		mp->mnt_iflag |= IMNT_MPSAFE;
2084e825063Shannken 
209a0deb3e9Sjdolecek 	/*
210a0deb3e9Sjdolecek 	 * Unless the mount is readonly, ensure that the top layer
211a0deb3e9Sjdolecek 	 * supports whiteout operations
212a0deb3e9Sjdolecek 	 */
213a0deb3e9Sjdolecek 	if ((mp->mnt_flag & MNT_RDONLY) == 0) {
214db9cd5ddShannken 		static struct componentname nullcn = {
215db9cd5ddShannken 			.cn_nameiop = LOOKUP,
216db9cd5ddShannken 			.cn_cred = NOCRED
217db9cd5ddShannken 		};
218db9cd5ddShannken 
219e65a3283Shannken 		vn_lock(um->um_uppervp, LK_EXCLUSIVE | LK_RETRY);
220db9cd5ddShannken 		error = VOP_WHITEOUT(um->um_uppervp, &nullcn, LOOKUP);
221e65a3283Shannken 		VOP_UNLOCK(um->um_uppervp);
222a0deb3e9Sjdolecek 		if (error)
223a0deb3e9Sjdolecek 			goto bad;
224a0deb3e9Sjdolecek 	}
225a0deb3e9Sjdolecek 
226f474dcebSad 	um->um_cred = l->l_cred;
227fc9422c9Selad 	kauth_cred_hold(um->um_cred);
22895e1ffb1Schristos 	um->um_cmode = UN_DIRMODE &~ l->l_proc->p_cwdi->cwdi_cmask;
229a0deb3e9Sjdolecek 
230a0deb3e9Sjdolecek 	/*
231a0deb3e9Sjdolecek 	 * Depending on what you think the MNT_LOCAL flag might mean,
232a0deb3e9Sjdolecek 	 * you may want the && to be || on the conditional below.
233a0deb3e9Sjdolecek 	 * At the moment it has been defined that the filesystem is
234a0deb3e9Sjdolecek 	 * only local if it is all local, ie the MNT_LOCAL flag implies
235a0deb3e9Sjdolecek 	 * that the entire namespace is local.  If you think the MNT_LOCAL
236a0deb3e9Sjdolecek 	 * flag implies that some of the files might be stored locally
237a0deb3e9Sjdolecek 	 * then you will want to change the conditional.
238a0deb3e9Sjdolecek 	 */
239a0deb3e9Sjdolecek 	if (um->um_op == UNMNT_ABOVE) {
240a0deb3e9Sjdolecek 		if (((um->um_lowervp == NULLVP) ||
241a0deb3e9Sjdolecek 		     (um->um_lowervp->v_mount->mnt_flag & MNT_LOCAL)) &&
242a0deb3e9Sjdolecek 		    (um->um_uppervp->v_mount->mnt_flag & MNT_LOCAL))
243a0deb3e9Sjdolecek 			mp->mnt_flag |= MNT_LOCAL;
244a0deb3e9Sjdolecek 	}
245a0deb3e9Sjdolecek 
246a0deb3e9Sjdolecek 	/*
247a0deb3e9Sjdolecek 	 * Copy in the upper layer's RDONLY flag.  This is for the benefit
248a0deb3e9Sjdolecek 	 * of lookup() which explicitly checks the flag, rather than asking
249f0a7346dSsnj 	 * the filesystem for its own opinion.  This means, that an update
250a0deb3e9Sjdolecek 	 * mount of the underlying filesystem to go from rdonly to rdwr
251a0deb3e9Sjdolecek 	 * will leave the unioned view as read-only.
252a0deb3e9Sjdolecek 	 */
253a0deb3e9Sjdolecek 	mp->mnt_flag |= (um->um_uppervp->v_mount->mnt_flag & MNT_RDONLY);
254a0deb3e9Sjdolecek 
255a0deb3e9Sjdolecek 	mp->mnt_data = um;
256a0deb3e9Sjdolecek 	vfs_getnewfsid(mp);
257a0deb3e9Sjdolecek 
2586bd1d6d4Schristos 	error = set_statvfs_info(path, UIO_USERSPACE, NULL, UIO_USERSPACE,
259e24b0872Spooka 	    mp->mnt_op->vfs_name, mp, l);
26080ecd573Schristos 	if (error)
26180ecd573Schristos 		goto bad;
262a0deb3e9Sjdolecek 
263af759f02Shannken 	error = vfs_set_lowermount(mp, um->um_uppervp->v_mount);
264af759f02Shannken 	if (error)
265af759f02Shannken 		goto bad;
266af759f02Shannken 
267a0deb3e9Sjdolecek 	switch (um->um_op) {
268a0deb3e9Sjdolecek 	case UNMNT_ABOVE:
269a0deb3e9Sjdolecek 		cp = "<above>:";
270a0deb3e9Sjdolecek 		break;
271a0deb3e9Sjdolecek 	case UNMNT_BELOW:
272a0deb3e9Sjdolecek 		cp = "<below>:";
273a0deb3e9Sjdolecek 		break;
274a0deb3e9Sjdolecek 	case UNMNT_REPLACE:
275a0deb3e9Sjdolecek 		cp = "";
276a0deb3e9Sjdolecek 		break;
277a0deb3e9Sjdolecek 	default:
278a0deb3e9Sjdolecek 		cp = "<invalid>:";
279a0deb3e9Sjdolecek #ifdef DIAGNOSTIC
280ae87d18fSchristos 		panic("%s: bad um_op", __func__);
281a0deb3e9Sjdolecek #endif
282a0deb3e9Sjdolecek 		break;
283a0deb3e9Sjdolecek 	}
284a0deb3e9Sjdolecek 	len = strlen(cp);
285a0deb3e9Sjdolecek 	memcpy(mp->mnt_stat.f_mntfromname, cp, len);
286a0deb3e9Sjdolecek 
287a29d4b25Schristos 	xp = mp->mnt_stat.f_mntfromname + len;
288a0deb3e9Sjdolecek 	len = MNAMELEN - len;
289a0deb3e9Sjdolecek 
2902721ab6cSdsl 	(void) copyinstr(args->target, xp, len - 1, &size);
291a29d4b25Schristos 	memset(xp + size, 0, len - size);
292a0deb3e9Sjdolecek 
293a0deb3e9Sjdolecek #ifdef UNION_DIAGNOSTIC
294ae87d18fSchristos 	printf("%s: from %s, on %s\n", __func__,
295a0deb3e9Sjdolecek 	    mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname);
296a0deb3e9Sjdolecek #endif
29746eb0d5bSjdolecek 
29846eb0d5bSjdolecek 	/* Setup the readdir hook if it's not set already */
29946eb0d5bSjdolecek 	if (!vn_union_readdir_hook)
30046eb0d5bSjdolecek 		vn_union_readdir_hook = union_readdirhook;
30146eb0d5bSjdolecek 
302ae87d18fSchristos 	return 0;
303a0deb3e9Sjdolecek 
304a0deb3e9Sjdolecek bad:
305ae87d18fSchristos 	if (um) {
306ae87d18fSchristos 		if (um->um_cred)
307ae87d18fSchristos 			kauth_cred_free(um->um_cred);
308ae87d18fSchristos 		kmem_free(um, sizeof(*um));
309ae87d18fSchristos 	}
310a0deb3e9Sjdolecek 	if (upperrootvp)
311a0deb3e9Sjdolecek 		vrele(upperrootvp);
312a0deb3e9Sjdolecek 	if (lowerrootvp)
313a0deb3e9Sjdolecek 		vrele(lowerrootvp);
314ae87d18fSchristos 	return error;
315a0deb3e9Sjdolecek }
316a0deb3e9Sjdolecek 
317a0deb3e9Sjdolecek /*
318a0deb3e9Sjdolecek  * VFS start.  Nothing needed here - the start routine
319a0deb3e9Sjdolecek  * on the underlying filesystem(s) will have been called
320a0deb3e9Sjdolecek  * when that filesystem was mounted.
321a0deb3e9Sjdolecek  */
322a0deb3e9Sjdolecek  /*ARGSUSED*/
323a0deb3e9Sjdolecek int
union_start(struct mount * mp,int flags)32461e8303eSpooka union_start(struct mount *mp, int flags)
325a0deb3e9Sjdolecek {
326a0deb3e9Sjdolecek 
327ae87d18fSchristos 	return 0;
328a0deb3e9Sjdolecek }
329a0deb3e9Sjdolecek 
330a0deb3e9Sjdolecek /*
331a0deb3e9Sjdolecek  * Free reference to union layer
332a0deb3e9Sjdolecek  */
33356b07d27Shannken static bool
union_unmount_selector(void * cl,struct vnode * vp)33456b07d27Shannken union_unmount_selector(void *cl, struct vnode *vp)
33556b07d27Shannken {
33656b07d27Shannken 	int *count = cl;
33756b07d27Shannken 
33830509f80Sriastradh 	KASSERT(mutex_owned(vp->v_interlock));
33930509f80Sriastradh 
34056b07d27Shannken 	*count += 1;
34156b07d27Shannken 	return false;
34256b07d27Shannken }
34356b07d27Shannken 
344a0deb3e9Sjdolecek int
union_unmount(struct mount * mp,int mntflags)34561e8303eSpooka union_unmount(struct mount *mp, int mntflags)
346a0deb3e9Sjdolecek {
347a0deb3e9Sjdolecek 	struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
348a0deb3e9Sjdolecek 	int freeing;
349f8912e44Schristos 	int error;
350a0deb3e9Sjdolecek 
351a0deb3e9Sjdolecek #ifdef UNION_DIAGNOSTIC
352ae87d18fSchristos 	printf("%s(mp = %p)\n", __func__, mp);
353a0deb3e9Sjdolecek #endif
354a0deb3e9Sjdolecek 
355a0deb3e9Sjdolecek 	/*
356a0deb3e9Sjdolecek 	 * Keep flushing vnodes from the mount list.
357a0deb3e9Sjdolecek 	 * This is needed because of the un_pvp held
358a0deb3e9Sjdolecek 	 * reference to the parent vnode.
359a0deb3e9Sjdolecek 	 * If more vnodes have been freed on a given pass,
360a0deb3e9Sjdolecek 	 * the try again.  The loop will iterate at most
361a0deb3e9Sjdolecek 	 * (d) times, where (d) is the maximum tree depth
362a0deb3e9Sjdolecek 	 * in the filesystem.
363a0deb3e9Sjdolecek 	 */
364f8912e44Schristos 	for (freeing = 0; (error = vflush(mp, NULL, 0)) != 0;) {
36556b07d27Shannken 		struct vnode_iterator *marker;
366a0deb3e9Sjdolecek 		int n;
367a0deb3e9Sjdolecek 
368a0deb3e9Sjdolecek 		/* count #vnodes held on mount list */
3690ce80909Sreinoud 		n = 0;
37056b07d27Shannken 		vfs_vnode_iterator_init(mp, &marker);
37156b07d27Shannken 		vfs_vnode_iterator_next(marker, union_unmount_selector, &n);
37256b07d27Shannken 		vfs_vnode_iterator_destroy(marker);
373a0deb3e9Sjdolecek 
374a0deb3e9Sjdolecek 		/* if this is unchanged then stop */
375a0deb3e9Sjdolecek 		if (n == freeing)
376a0deb3e9Sjdolecek 			break;
377a0deb3e9Sjdolecek 
378a0deb3e9Sjdolecek 		/* otherwise try once more time */
379a0deb3e9Sjdolecek 		freeing = n;
380a0deb3e9Sjdolecek 	}
381a0deb3e9Sjdolecek 
382a0deb3e9Sjdolecek 	/*
383a0deb3e9Sjdolecek 	 * Ok, now that we've tried doing it gently, get out the hammer.
384a0deb3e9Sjdolecek 	 */
385a0deb3e9Sjdolecek 
386a0deb3e9Sjdolecek 	if (mntflags & MNT_FORCE)
387f8912e44Schristos 		error = vflush(mp, NULL, FORCECLOSE);
388a0deb3e9Sjdolecek 
389f8912e44Schristos 	if (error)
390f8912e44Schristos 		return error;
391a0deb3e9Sjdolecek 
392a0deb3e9Sjdolecek 	/*
393a0deb3e9Sjdolecek 	 * Discard references to upper and lower target vnodes.
394a0deb3e9Sjdolecek 	 */
395a0deb3e9Sjdolecek 	if (um->um_lowervp)
396a0deb3e9Sjdolecek 		vrele(um->um_lowervp);
397a0deb3e9Sjdolecek 	vrele(um->um_uppervp);
398fc9422c9Selad 	kauth_cred_free(um->um_cred);
399a0deb3e9Sjdolecek 	/*
400a0deb3e9Sjdolecek 	 * Finally, throw away the union_mount structure
401a0deb3e9Sjdolecek 	 */
402ae87d18fSchristos 	kmem_free(um, sizeof(*um));
4032fd51303Ssimonb 	mp->mnt_data = NULL;
404f1d428afSrmind 	return 0;
405a0deb3e9Sjdolecek }
406a0deb3e9Sjdolecek 
407a0deb3e9Sjdolecek int
union_root(struct mount * mp,int lktype,struct vnode ** vpp)408c2e9cb94Sad union_root(struct mount *mp, int lktype, struct vnode **vpp)
409a0deb3e9Sjdolecek {
410a0deb3e9Sjdolecek 	struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
411a0deb3e9Sjdolecek 	int error;
412a0deb3e9Sjdolecek 
413a0deb3e9Sjdolecek 	/*
414a0deb3e9Sjdolecek 	 * Return locked reference to root.
415a0deb3e9Sjdolecek 	 */
416c3183f32Spooka 	vref(um->um_uppervp);
417a0deb3e9Sjdolecek 	if (um->um_lowervp)
418c3183f32Spooka 		vref(um->um_lowervp);
41905077472Schs 	error = union_allocvp(vpp, mp, NULL, NULL, NULL,
42005077472Schs 	    um->um_uppervp, um->um_lowervp, 1);
421a0deb3e9Sjdolecek 
422a0deb3e9Sjdolecek 	if (error) {
423c36eeb72Shannken 		vrele(um->um_uppervp);
424a0deb3e9Sjdolecek 		if (um->um_lowervp)
425a0deb3e9Sjdolecek 			vrele(um->um_lowervp);
426c36eeb72Shannken 		return error;
427a0deb3e9Sjdolecek 	}
428a0deb3e9Sjdolecek 
429c2e9cb94Sad 	vn_lock(*vpp, lktype | LK_RETRY);
430c36eeb72Shannken 
431c36eeb72Shannken 	return 0;
432a0deb3e9Sjdolecek }
433a0deb3e9Sjdolecek 
434a0deb3e9Sjdolecek int
union_statvfs(struct mount * mp,struct statvfs * sbp)4359a1bbaabSmatt union_statvfs(struct mount *mp, struct statvfs *sbp)
436a0deb3e9Sjdolecek {
437a0deb3e9Sjdolecek 	int error;
438a0deb3e9Sjdolecek 	struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
439d3a31968Schristos 	struct statvfs *sbuf = kmem_zalloc(sizeof(*sbuf), KM_SLEEP);
440e944b1cdSchristos 	unsigned long lbsize;
441a0deb3e9Sjdolecek 
442a0deb3e9Sjdolecek #ifdef UNION_DIAGNOSTIC
443ae87d18fSchristos 	printf("%s(mp = %p, lvp = %p, uvp = %p)\n", __func__, mp,
444a0deb3e9Sjdolecek 	    um->um_lowervp, um->um_uppervp);
445a0deb3e9Sjdolecek #endif
446a0deb3e9Sjdolecek 
447a0deb3e9Sjdolecek 	if (um->um_lowervp) {
44861e8303eSpooka 		error = VFS_STATVFS(um->um_lowervp->v_mount, sbuf);
449a0deb3e9Sjdolecek 		if (error)
450e944b1cdSchristos 			goto done;
451a0deb3e9Sjdolecek 	}
452a0deb3e9Sjdolecek 
453e944b1cdSchristos 	/* now copy across the "interesting" information and fake the rest */
454e944b1cdSchristos 	lbsize = sbuf->f_bsize;
455e944b1cdSchristos 	sbp->f_blocks = sbuf->f_blocks - sbuf->f_bfree;
456e944b1cdSchristos 	sbp->f_files = sbuf->f_files - sbuf->f_ffree;
457e944b1cdSchristos 
45861e8303eSpooka 	error = VFS_STATVFS(um->um_uppervp->v_mount, sbuf);
459a0deb3e9Sjdolecek 	if (error)
460e944b1cdSchristos 		goto done;
461e944b1cdSchristos 
462e944b1cdSchristos 	sbp->f_flag = sbuf->f_flag;
463e944b1cdSchristos 	sbp->f_bsize = sbuf->f_bsize;
464e944b1cdSchristos 	sbp->f_frsize = sbuf->f_frsize;
465e944b1cdSchristos 	sbp->f_iosize = sbuf->f_iosize;
466e944b1cdSchristos 
467a0deb3e9Sjdolecek 	/*
468a0deb3e9Sjdolecek 	 * The "total" fields count total resources in all layers,
469a0deb3e9Sjdolecek 	 * the "free" fields count only those resources which are
470a0deb3e9Sjdolecek 	 * free in the upper layer (since only the upper layer
471a0deb3e9Sjdolecek 	 * is writable).
472a0deb3e9Sjdolecek 	 */
473e944b1cdSchristos 
474e944b1cdSchristos 	if (sbuf->f_bsize != lbsize)
475e944b1cdSchristos 		sbp->f_blocks = sbp->f_blocks * lbsize / sbuf->f_bsize;
476e944b1cdSchristos 	sbp->f_blocks += sbuf->f_blocks;
477e944b1cdSchristos 	sbp->f_bfree = sbuf->f_bfree;
478e944b1cdSchristos 	sbp->f_bavail = sbuf->f_bavail;
479e944b1cdSchristos 	sbp->f_bresvd = sbuf->f_bresvd;
480e944b1cdSchristos 	sbp->f_files += sbuf->f_files;
481e944b1cdSchristos 	sbp->f_ffree = sbuf->f_ffree;
482e944b1cdSchristos 	sbp->f_favail = sbuf->f_favail;
483e944b1cdSchristos 	sbp->f_fresvd = sbuf->f_fresvd;
484a0deb3e9Sjdolecek 
4856bd1d6d4Schristos 	copy_statvfs_info(sbp, mp);
486e944b1cdSchristos done:
487ae87d18fSchristos 	kmem_free(sbuf, sizeof(*sbuf));
488e944b1cdSchristos 	return error;
489a0deb3e9Sjdolecek }
490a0deb3e9Sjdolecek 
491a0deb3e9Sjdolecek /*ARGSUSED*/
492a0deb3e9Sjdolecek int
union_sync(struct mount * mp,int waitfor,kauth_cred_t cred)493ae87d18fSchristos union_sync(struct mount *mp, int waitfor, kauth_cred_t cred)
494a0deb3e9Sjdolecek {
495a0deb3e9Sjdolecek 
496a0deb3e9Sjdolecek 	/*
497a0deb3e9Sjdolecek 	 * XXX - Assumes no data cached at union layer.
498a0deb3e9Sjdolecek 	 */
499ae87d18fSchristos 	return 0;
500a0deb3e9Sjdolecek }
501a0deb3e9Sjdolecek 
502a0deb3e9Sjdolecek /*ARGSUSED*/
503a0deb3e9Sjdolecek int
union_vget(struct mount * mp,ino_t ino,int lktype,struct vnode ** vpp)504ae87d18fSchristos union_vget(struct mount *mp, ino_t ino, int lktype, struct vnode **vpp)
505a0deb3e9Sjdolecek {
506a0deb3e9Sjdolecek 
507ae87d18fSchristos 	return EOPNOTSUPP;
508a0deb3e9Sjdolecek }
509a0deb3e9Sjdolecek 
510717e1785Sdholland static int
union_renamelock_enter(struct mount * mp)511717e1785Sdholland union_renamelock_enter(struct mount *mp)
512717e1785Sdholland {
513717e1785Sdholland 	struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
514717e1785Sdholland 
515717e1785Sdholland 	/* Lock just the upper fs, where the action happens. */
516717e1785Sdholland 	return VFS_RENAMELOCK_ENTER(um->um_uppervp->v_mount);
517717e1785Sdholland }
518717e1785Sdholland 
519717e1785Sdholland static void
union_renamelock_exit(struct mount * mp)520717e1785Sdholland union_renamelock_exit(struct mount *mp)
521717e1785Sdholland {
522717e1785Sdholland 	struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
523717e1785Sdholland 
524717e1785Sdholland 	VFS_RENAMELOCK_EXIT(um->um_uppervp->v_mount);
525717e1785Sdholland }
526717e1785Sdholland 
527a0deb3e9Sjdolecek extern const struct vnodeopv_desc union_vnodeop_opv_desc;
528a0deb3e9Sjdolecek 
529a0deb3e9Sjdolecek const struct vnodeopv_desc * const union_vnodeopv_descs[] = {
530a0deb3e9Sjdolecek 	&union_vnodeop_opv_desc,
531a0deb3e9Sjdolecek 	NULL,
532a0deb3e9Sjdolecek };
533a0deb3e9Sjdolecek 
534a0deb3e9Sjdolecek struct vfsops union_vfsops = {
5356d285189Shannken 	.vfs_name = MOUNT_UNION,
5366d285189Shannken 	.vfs_min_mount_data = sizeof (struct union_args),
5376d285189Shannken 	.vfs_mount = union_mount,
5386d285189Shannken 	.vfs_start = union_start,
5396d285189Shannken 	.vfs_unmount = union_unmount,
5406d285189Shannken 	.vfs_root = union_root,
5416d285189Shannken 	.vfs_quotactl = (void *)eopnotsupp,
5426d285189Shannken 	.vfs_statvfs = union_statvfs,
5436d285189Shannken 	.vfs_sync = union_sync,
5446d285189Shannken 	.vfs_vget = union_vget,
5458596fa40Shannken 	.vfs_loadvnode = union_loadvnode,
5466d285189Shannken 	.vfs_fhtovp = (void *)eopnotsupp,
5476d285189Shannken 	.vfs_vptofh = (void *)eopnotsupp,
5486d285189Shannken 	.vfs_init = union_init,
5496d285189Shannken 	.vfs_reinit = union_reinit,
5506d285189Shannken 	.vfs_done = union_done,
5516d285189Shannken 	.vfs_snapshot = (void *)eopnotsupp,
5526d285189Shannken 	.vfs_extattrctl = vfs_stdextattrctl,
553326db3aaShannken 	.vfs_suspendctl = genfs_suspendctl,
5546d285189Shannken 	.vfs_renamelock_enter = union_renamelock_enter,
5556d285189Shannken 	.vfs_renamelock_exit = union_renamelock_exit,
5566d285189Shannken 	.vfs_fsync = (void *)eopnotsupp,
5576d285189Shannken 	.vfs_opv_descs = union_vnodeopv_descs
558a0deb3e9Sjdolecek };
559a1221b6dSrumble 
5609120d451Spgoyette SYSCTL_SETUP(unionfs_sysctl_setup, "unionfs sysctl")
561a1221b6dSrumble {
562a1221b6dSrumble 
5639120d451Spgoyette 	sysctl_createv(clog, 0, NULL, NULL,
56428f5ebd8Srumble 	    CTLFLAG_PERMANENT,
56528f5ebd8Srumble 	    CTLTYPE_NODE, "union",
56628f5ebd8Srumble 	    SYSCTL_DESCR("Union file system"),
56728f5ebd8Srumble 	    NULL, 0, NULL, 0,
56828f5ebd8Srumble 	    CTL_VFS, 15, CTL_EOL);
56928f5ebd8Srumble 	/*
57028f5ebd8Srumble 	 * XXX the "15" above could be dynamic, thereby eliminating
57128f5ebd8Srumble 	 * one more instance of the "number to vfs" mapping problem,
57228f5ebd8Srumble 	 * but "15" is the order as taken from sys/mount.h
57328f5ebd8Srumble 	 */
5749120d451Spgoyette }
5759120d451Spgoyette 
5769120d451Spgoyette static int
union_modcmd(modcmd_t cmd,void * arg)5779120d451Spgoyette union_modcmd(modcmd_t cmd, void *arg)
5789120d451Spgoyette {
5799120d451Spgoyette 
5809120d451Spgoyette 	switch (cmd) {
5819120d451Spgoyette 	case MODULE_CMD_INIT:
582ae87d18fSchristos 		return vfs_attach(&union_vfsops);
583a1221b6dSrumble 	case MODULE_CMD_FINI:
584ae87d18fSchristos 		return vfs_detach(&union_vfsops);
585a1221b6dSrumble 	default:
586ae87d18fSchristos 		return ENOTTY;
587a1221b6dSrumble 	}
588a1221b6dSrumble }
589