xref: /freebsd/sys/fs/unionfs/union_vfsops.c (revision 852c303b61d12d13b56ed7affe31df193aadf9ae)
1d167cf6fSWarner Losh /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
4996c772fSJohn Dyson  * Copyright (c) 1994, 1995 The Regents of the University of California.
5996c772fSJohn Dyson  * Copyright (c) 1994, 1995 Jan-Simon Pendry.
6cb5736b7SDaichi GOTO  * Copyright (c) 2005, 2006, 2012 Masanori Ozawa <ozawa@ongs.co.jp>, ONGS Inc.
7cb5736b7SDaichi GOTO  * Copyright (c) 2006, 2012 Daichi Goto <daichi@freebsd.org>
8df8bae1dSRodney W. Grimes  * All rights reserved.
9df8bae1dSRodney W. Grimes  *
10df8bae1dSRodney W. Grimes  * This code is derived from software donated to Berkeley by
11df8bae1dSRodney W. Grimes  * Jan-Simon Pendry.
12df8bae1dSRodney W. Grimes  *
13df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
14df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
15df8bae1dSRodney W. Grimes  * are met:
16df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
17df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
18df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
19df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
20df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
21fbbd9655SWarner Losh  * 3. 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 #include <sys/param.h>
42df8bae1dSRodney W. Grimes #include <sys/systm.h>
43d00947d8SCraig Rodrigues #include <sys/kdb.h>
4457b4252eSKonstantin Belousov #include <sys/fcntl.h>
45c9b1d604SGarrett Wollman #include <sys/kernel.h>
46805d90f7SJohn Baldwin #include <sys/lock.h>
47d00947d8SCraig Rodrigues #include <sys/malloc.h>
48df8bae1dSRodney W. Grimes #include <sys/mount.h>
49df8bae1dSRodney W. Grimes #include <sys/namei.h>
50d00947d8SCraig Rodrigues #include <sys/proc.h>
51d00947d8SCraig Rodrigues #include <sys/vnode.h>
52d00947d8SCraig Rodrigues #include <sys/stat.h>
53d00947d8SCraig Rodrigues 
5499d300a1SRuslan Ermilov #include <fs/unionfs/union.h>
55df8bae1dSRodney W. Grimes 
56d00947d8SCraig Rodrigues static MALLOC_DEFINE(M_UNIONFSMNT, "UNIONFS mount", "UNIONFS mount structure");
57a1c995b6SPoul-Henning Kamp 
58d00947d8SCraig Rodrigues static vfs_fhtovp_t	unionfs_fhtovp;
59d00947d8SCraig Rodrigues static vfs_checkexp_t	unionfs_checkexp;
60d00947d8SCraig Rodrigues static vfs_mount_t	unionfs_domount;
61d00947d8SCraig Rodrigues static vfs_quotactl_t	unionfs_quotactl;
62d00947d8SCraig Rodrigues static vfs_root_t	unionfs_root;
63d00947d8SCraig Rodrigues static vfs_sync_t	unionfs_sync;
64d00947d8SCraig Rodrigues static vfs_statfs_t	unionfs_statfs;
65d00947d8SCraig Rodrigues static vfs_unmount_t	unionfs_unmount;
66d00947d8SCraig Rodrigues static vfs_vget_t	unionfs_vget;
67d00947d8SCraig Rodrigues static vfs_extattrctl_t	unionfs_extattrctl;
68d00947d8SCraig Rodrigues 
69d00947d8SCraig Rodrigues static struct vfsops unionfs_vfsops;
70b5e8ce9fSBruce Evans 
71df8bae1dSRodney W. Grimes /*
72d00947d8SCraig Rodrigues  * Mount unionfs layer.
73df8bae1dSRodney W. Grimes  */
7480b301c3SPoul-Henning Kamp static int
75dfd233edSAttilio Rao unionfs_domount(struct mount *mp)
76df8bae1dSRodney W. Grimes {
77d00947d8SCraig Rodrigues 	int		error;
78d00947d8SCraig Rodrigues 	struct vnode   *lowerrootvp;
79d00947d8SCraig Rodrigues 	struct vnode   *upperrootvp;
80d00947d8SCraig Rodrigues 	struct unionfs_mount *ump;
81dfd233edSAttilio Rao 	struct thread *td;
82d00947d8SCraig Rodrigues 	char           *target;
83d00947d8SCraig Rodrigues 	char           *tmp;
84d00947d8SCraig Rodrigues 	char           *ep;
85df8bae1dSRodney W. Grimes 	int		len;
86d00947d8SCraig Rodrigues 	int		below;
87d00947d8SCraig Rodrigues 	uid_t		uid;
88d00947d8SCraig Rodrigues 	gid_t		gid;
89d00947d8SCraig Rodrigues 	u_short		udir;
90d00947d8SCraig Rodrigues 	u_short		ufile;
91d00947d8SCraig Rodrigues 	unionfs_copymode copymode;
9220885defSDaichi GOTO 	unionfs_whitemode whitemode;
93d00947d8SCraig Rodrigues 	struct nameidata nd, *ndp;
94d00947d8SCraig Rodrigues 	struct vattr	va;
95df8bae1dSRodney W. Grimes 
96d00947d8SCraig Rodrigues 	UNIONFSDEBUG("unionfs_mount(mp = %p)\n", (void *)mp);
97df8bae1dSRodney W. Grimes 
98d00947d8SCraig Rodrigues 	error = 0;
99d00947d8SCraig Rodrigues 	below = 0;
100d00947d8SCraig Rodrigues 	uid = 0;
101d00947d8SCraig Rodrigues 	gid = 0;
102d00947d8SCraig Rodrigues 	udir = 0;
103d00947d8SCraig Rodrigues 	ufile = 0;
104524f3f28SDaichi GOTO 	copymode = UNIONFS_TRANSPARENT;	/* default */
10520885defSDaichi GOTO 	whitemode = UNIONFS_WHITE_ALWAYS;
106d00947d8SCraig Rodrigues 	ndp = &nd;
107dfd233edSAttilio Rao 	td = curthread;
10881bca6ddSKATO Takenori 
1091e370dbbSCraig Rodrigues 	if (mp->mnt_flag & MNT_ROOTFS) {
1101e370dbbSCraig Rodrigues 		vfs_mount_error(mp, "Cannot union mount root filesystem");
11164042a76SPoul-Henning Kamp 		return (EOPNOTSUPP);
1121e370dbbSCraig Rodrigues 	}
113d00947d8SCraig Rodrigues 
11481bca6ddSKATO Takenori 	/*
115d00947d8SCraig Rodrigues 	 * Update is a no operation.
116df8bae1dSRodney W. Grimes 	 */
1171e370dbbSCraig Rodrigues 	if (mp->mnt_flag & MNT_UPDATE) {
1181e370dbbSCraig Rodrigues 		vfs_mount_error(mp, "unionfs does not support mount update");
119a9f5c04aSMaxime Henrion 		return (EOPNOTSUPP);
1201e370dbbSCraig Rodrigues 	}
121df8bae1dSRodney W. Grimes 
122df8bae1dSRodney W. Grimes 	/*
123d00947d8SCraig Rodrigues 	 * Get argument
124df8bae1dSRodney W. Grimes 	 */
125d00947d8SCraig Rodrigues 	error = vfs_getopt(mp->mnt_optnew, "target", (void **)&target, &len);
1263a773ad0SPoul-Henning Kamp 	if (error)
127d00947d8SCraig Rodrigues 		error = vfs_getopt(mp->mnt_optnew, "from", (void **)&target,
128d00947d8SCraig Rodrigues 		    &len);
129d00947d8SCraig Rodrigues 	if (error || target[len - 1] != '\0') {
130d00947d8SCraig Rodrigues 		vfs_mount_error(mp, "Invalid target");
131d00947d8SCraig Rodrigues 		return (EINVAL);
132d00947d8SCraig Rodrigues 	}
133d00947d8SCraig Rodrigues 	if (vfs_getopt(mp->mnt_optnew, "below", NULL, NULL) == 0)
134d00947d8SCraig Rodrigues 		below = 1;
135d00947d8SCraig Rodrigues 	if (vfs_getopt(mp->mnt_optnew, "udir", (void **)&tmp, NULL) == 0) {
136d00947d8SCraig Rodrigues 		if (tmp != NULL)
137d00947d8SCraig Rodrigues 			udir = (mode_t)strtol(tmp, &ep, 8);
138d00947d8SCraig Rodrigues 		if (tmp == NULL || *ep) {
139d00947d8SCraig Rodrigues 			vfs_mount_error(mp, "Invalid udir");
140d00947d8SCraig Rodrigues 			return (EINVAL);
141d00947d8SCraig Rodrigues 		}
14216385727SDaichi GOTO 		udir &= S_IRWXU | S_IRWXG | S_IRWXO;
143d00947d8SCraig Rodrigues 	}
144d00947d8SCraig Rodrigues 	if (vfs_getopt(mp->mnt_optnew, "ufile", (void **)&tmp, NULL) == 0) {
145d00947d8SCraig Rodrigues 		if (tmp != NULL)
146d00947d8SCraig Rodrigues 			ufile = (mode_t)strtol(tmp, &ep, 8);
147d00947d8SCraig Rodrigues 		if (tmp == NULL || *ep) {
148d00947d8SCraig Rodrigues 			vfs_mount_error(mp, "Invalid ufile");
149d00947d8SCraig Rodrigues 			return (EINVAL);
150d00947d8SCraig Rodrigues 		}
15116385727SDaichi GOTO 		ufile &= S_IRWXU | S_IRWXG | S_IRWXO;
152d00947d8SCraig Rodrigues 	}
153d00947d8SCraig Rodrigues 	/* check umask, uid and gid */
154d00947d8SCraig Rodrigues 	if (udir == 0 && ufile != 0)
155d00947d8SCraig Rodrigues 		udir = ufile;
156d00947d8SCraig Rodrigues 	if (ufile == 0 && udir != 0)
157d00947d8SCraig Rodrigues 		ufile = udir;
158d00947d8SCraig Rodrigues 
159cb05b60aSAttilio Rao 	vn_lock(mp->mnt_vnodecovered, LK_SHARED | LK_RETRY);
1600359a12eSAttilio Rao 	error = VOP_GETATTR(mp->mnt_vnodecovered, &va, mp->mnt_cred);
161d00947d8SCraig Rodrigues 	if (!error) {
162d00947d8SCraig Rodrigues 		if (udir == 0)
163d00947d8SCraig Rodrigues 			udir = va.va_mode;
164d00947d8SCraig Rodrigues 		if (ufile == 0)
165d00947d8SCraig Rodrigues 			ufile = va.va_mode;
166d00947d8SCraig Rodrigues 		uid = va.va_uid;
167d00947d8SCraig Rodrigues 		gid = va.va_gid;
168d00947d8SCraig Rodrigues 	}
169b249ce48SMateusz Guzik 	VOP_UNLOCK(mp->mnt_vnodecovered);
170d00947d8SCraig Rodrigues 	if (error)
171d00947d8SCraig Rodrigues 		return (error);
172d00947d8SCraig Rodrigues 
173d00947d8SCraig Rodrigues 	if (mp->mnt_cred->cr_ruid == 0) {	/* root only */
174d00947d8SCraig Rodrigues 		if (vfs_getopt(mp->mnt_optnew, "uid", (void **)&tmp,
175d00947d8SCraig Rodrigues 		    NULL) == 0) {
176d00947d8SCraig Rodrigues 			if (tmp != NULL)
177d00947d8SCraig Rodrigues 				uid = (uid_t)strtol(tmp, &ep, 10);
178d00947d8SCraig Rodrigues 			if (tmp == NULL || *ep) {
179d00947d8SCraig Rodrigues 				vfs_mount_error(mp, "Invalid uid");
180d00947d8SCraig Rodrigues 				return (EINVAL);
181d00947d8SCraig Rodrigues 			}
182d00947d8SCraig Rodrigues 		}
183d00947d8SCraig Rodrigues 		if (vfs_getopt(mp->mnt_optnew, "gid", (void **)&tmp,
184d00947d8SCraig Rodrigues 		    NULL) == 0) {
185d00947d8SCraig Rodrigues 			if (tmp != NULL)
186d00947d8SCraig Rodrigues 				gid = (gid_t)strtol(tmp, &ep, 10);
187d00947d8SCraig Rodrigues 			if (tmp == NULL || *ep) {
188d00947d8SCraig Rodrigues 				vfs_mount_error(mp, "Invalid gid");
189d00947d8SCraig Rodrigues 				return (EINVAL);
190d00947d8SCraig Rodrigues 			}
191d00947d8SCraig Rodrigues 		}
192d00947d8SCraig Rodrigues 		if (vfs_getopt(mp->mnt_optnew, "copymode", (void **)&tmp,
193d00947d8SCraig Rodrigues 		    NULL) == 0) {
194d00947d8SCraig Rodrigues 			if (tmp == NULL) {
195d00947d8SCraig Rodrigues 				vfs_mount_error(mp, "Invalid copymode");
196d00947d8SCraig Rodrigues 				return (EINVAL);
197d00947d8SCraig Rodrigues 			} else if (strcasecmp(tmp, "traditional") == 0)
198d00947d8SCraig Rodrigues 				copymode = UNIONFS_TRADITIONAL;
199d00947d8SCraig Rodrigues 			else if (strcasecmp(tmp, "transparent") == 0)
200d00947d8SCraig Rodrigues 				copymode = UNIONFS_TRANSPARENT;
201d00947d8SCraig Rodrigues 			else if (strcasecmp(tmp, "masquerade") == 0)
202d00947d8SCraig Rodrigues 				copymode = UNIONFS_MASQUERADE;
203d00947d8SCraig Rodrigues 			else {
204d00947d8SCraig Rodrigues 				vfs_mount_error(mp, "Invalid copymode");
205d00947d8SCraig Rodrigues 				return (EINVAL);
206d00947d8SCraig Rodrigues 			}
207d00947d8SCraig Rodrigues 		}
20820885defSDaichi GOTO 		if (vfs_getopt(mp->mnt_optnew, "whiteout", (void **)&tmp,
20920885defSDaichi GOTO 		    NULL) == 0) {
21020885defSDaichi GOTO 			if (tmp == NULL) {
21120885defSDaichi GOTO 				vfs_mount_error(mp, "Invalid whiteout mode");
21220885defSDaichi GOTO 				return (EINVAL);
21320885defSDaichi GOTO 			} else if (strcasecmp(tmp, "always") == 0)
21420885defSDaichi GOTO 				whitemode = UNIONFS_WHITE_ALWAYS;
21520885defSDaichi GOTO 			else if (strcasecmp(tmp, "whenneeded") == 0)
21620885defSDaichi GOTO 				whitemode = UNIONFS_WHITE_WHENNEEDED;
21720885defSDaichi GOTO 			else {
21820885defSDaichi GOTO 				vfs_mount_error(mp, "Invalid whiteout mode");
21920885defSDaichi GOTO 				return (EINVAL);
22020885defSDaichi GOTO 			}
22120885defSDaichi GOTO 		}
222d00947d8SCraig Rodrigues 	}
223d00947d8SCraig Rodrigues 	/* If copymode is UNIONFS_TRADITIONAL, uid/gid is mounted user. */
224d00947d8SCraig Rodrigues 	if (copymode == UNIONFS_TRADITIONAL) {
225d00947d8SCraig Rodrigues 		uid = mp->mnt_cred->cr_ruid;
226d00947d8SCraig Rodrigues 		gid = mp->mnt_cred->cr_rgid;
227d00947d8SCraig Rodrigues 	}
228d00947d8SCraig Rodrigues 
229d00947d8SCraig Rodrigues 	UNIONFSDEBUG("unionfs_mount: uid=%d, gid=%d\n", uid, gid);
230d00947d8SCraig Rodrigues 	UNIONFSDEBUG("unionfs_mount: udir=0%03o, ufile=0%03o\n", udir, ufile);
231d00947d8SCraig Rodrigues 	UNIONFSDEBUG("unionfs_mount: copymode=%d\n", copymode);
232d00947d8SCraig Rodrigues 
233d00947d8SCraig Rodrigues 	/*
234d00947d8SCraig Rodrigues 	 * Find upper node
235d00947d8SCraig Rodrigues 	 */
2367265164fSJohn Baldwin 	NDINIT(ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, target, td);
237d00947d8SCraig Rodrigues 	if ((error = namei(ndp)))
238d00947d8SCraig Rodrigues 		return (error);
239d00947d8SCraig Rodrigues 
240762e6b85SEivind Eklund 	NDFREE(ndp, NDF_ONLY_PNBUF);
241d00947d8SCraig Rodrigues 
242d00947d8SCraig Rodrigues 	/* get root vnodes */
243d00947d8SCraig Rodrigues 	lowerrootvp = mp->mnt_vnodecovered;
244df8bae1dSRodney W. Grimes 	upperrootvp = ndp->ni_vp;
245df8bae1dSRodney W. Grimes 
246d00947d8SCraig Rodrigues 	/* create unionfs_mount */
247d00947d8SCraig Rodrigues 	ump = (struct unionfs_mount *)malloc(sizeof(struct unionfs_mount),
248a163d034SWarner Losh 	    M_UNIONFSMNT, M_WAITOK | M_ZERO);
2492a31267eSMatthew Dillon 
250d00947d8SCraig Rodrigues 	/*
251d00947d8SCraig Rodrigues 	 * Save reference
252d00947d8SCraig Rodrigues 	 */
253d00947d8SCraig Rodrigues 	if (below) {
254b249ce48SMateusz Guzik 		VOP_UNLOCK(upperrootvp);
255cb05b60aSAttilio Rao 		vn_lock(lowerrootvp, LK_EXCLUSIVE | LK_RETRY);
256d00947d8SCraig Rodrigues 		ump->um_lowervp = upperrootvp;
257d00947d8SCraig Rodrigues 		ump->um_uppervp = lowerrootvp;
258d00947d8SCraig Rodrigues 	} else {
259d00947d8SCraig Rodrigues 		ump->um_lowervp = lowerrootvp;
260d00947d8SCraig Rodrigues 		ump->um_uppervp = upperrootvp;
261df8bae1dSRodney W. Grimes 	}
262d00947d8SCraig Rodrigues 	ump->um_rootvp = NULLVP;
263d00947d8SCraig Rodrigues 	ump->um_uid = uid;
264d00947d8SCraig Rodrigues 	ump->um_gid = gid;
265d00947d8SCraig Rodrigues 	ump->um_udir = udir;
266d00947d8SCraig Rodrigues 	ump->um_ufile = ufile;
267d00947d8SCraig Rodrigues 	ump->um_copymode = copymode;
26820885defSDaichi GOTO 	ump->um_whitemode = whitemode;
269d00947d8SCraig Rodrigues 
27077465d93SAlfred Perlstein 	mp->mnt_data = ump;
271df8bae1dSRodney W. Grimes 
272996c772fSJohn Dyson 	/*
273d00947d8SCraig Rodrigues 	 * Copy upper layer's RDONLY flag.
274d00947d8SCraig Rodrigues 	 */
275d00947d8SCraig Rodrigues 	mp->mnt_flag |= ump->um_uppervp->v_mount->mnt_flag & MNT_RDONLY;
276d00947d8SCraig Rodrigues 
277d00947d8SCraig Rodrigues 	/*
278d00947d8SCraig Rodrigues 	 * Unlock the node
279df8bae1dSRodney W. Grimes 	 */
280b249ce48SMateusz Guzik 	VOP_UNLOCK(ump->um_uppervp);
281df8bae1dSRodney W. Grimes 
282d00947d8SCraig Rodrigues 	/*
283d00947d8SCraig Rodrigues 	 * Get the unionfs root vnode.
284d00947d8SCraig Rodrigues 	 */
285d00947d8SCraig Rodrigues 	error = unionfs_nodeget(mp, ump->um_uppervp, ump->um_lowervp,
286d00947d8SCraig Rodrigues 	    NULLVP, &(ump->um_rootvp), NULL, td);
287d00947d8SCraig Rodrigues 	vrele(upperrootvp);
2887d72c5e6SDaichi GOTO 	if (error) {
289d00947d8SCraig Rodrigues 		free(ump, M_UNIONFSMNT);
290d00947d8SCraig Rodrigues 		mp->mnt_data = NULL;
291df8bae1dSRodney W. Grimes 		return (error);
292df8bae1dSRodney W. Grimes 	}
293df8bae1dSRodney W. Grimes 
294a8a07fd6SMateusz Guzik 	MNT_ILOCK(mp);
295d00947d8SCraig Rodrigues 	if ((ump->um_lowervp->v_mount->mnt_flag & MNT_LOCAL) &&
296d00947d8SCraig Rodrigues 	    (ump->um_uppervp->v_mount->mnt_flag & MNT_LOCAL))
297d00947d8SCraig Rodrigues 		mp->mnt_flag |= MNT_LOCAL;
298d3cc5354SMateusz Guzik 	mp->mnt_kern_flag |= MNTK_NOMSYNC | MNTK_UNIONFS;
299a8a07fd6SMateusz Guzik 	MNT_IUNLOCK(mp);
300d00947d8SCraig Rodrigues 
301d00947d8SCraig Rodrigues 	/*
302d00947d8SCraig Rodrigues 	 * Get new fsid
303d00947d8SCraig Rodrigues 	 */
304d00947d8SCraig Rodrigues 	vfs_getnewfsid(mp);
305d00947d8SCraig Rodrigues 
306*852c303bSConrad Meyer 	snprintf(mp->mnt_stat.f_mntfromname, MNAMELEN, "<%s>:%s",
307*852c303bSConrad Meyer 	    below ? "below" : "above", target);
308d00947d8SCraig Rodrigues 
309d00947d8SCraig Rodrigues 	UNIONFSDEBUG("unionfs_mount: from %s, on %s\n",
310d00947d8SCraig Rodrigues 	    mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname);
311d00947d8SCraig Rodrigues 
312d00947d8SCraig Rodrigues 	return (0);
313d00947d8SCraig Rodrigues }
314d00947d8SCraig Rodrigues 
315d00947d8SCraig Rodrigues /*
316d00947d8SCraig Rodrigues  * Free reference to unionfs layer
317df8bae1dSRodney W. Grimes  */
31880b301c3SPoul-Henning Kamp static int
319dfd233edSAttilio Rao unionfs_unmount(struct mount *mp, int mntflags)
320df8bae1dSRodney W. Grimes {
321d00947d8SCraig Rodrigues 	struct unionfs_mount *ump;
322df8bae1dSRodney W. Grimes 	int		error;
323d00947d8SCraig Rodrigues 	int		num;
324996c772fSJohn Dyson 	int		freeing;
325d00947d8SCraig Rodrigues 	int		flags;
326df8bae1dSRodney W. Grimes 
327d00947d8SCraig Rodrigues 	UNIONFSDEBUG("unionfs_unmount: mp = %p\n", (void *)mp);
328d00947d8SCraig Rodrigues 
329d00947d8SCraig Rodrigues 	ump = MOUNTTOUNIONFSMOUNT(mp);
330d00947d8SCraig Rodrigues 	flags = 0;
331df8bae1dSRodney W. Grimes 
332996c772fSJohn Dyson 	if (mntflags & MNT_FORCE)
333996c772fSJohn Dyson 		flags |= FORCECLOSE;
334996c772fSJohn Dyson 
335d00947d8SCraig Rodrigues 	/* vflush (no need to call vrele) */
336dfd233edSAttilio Rao 	for (freeing = 0; (error = vflush(mp, 1, flags, curthread)) != 0;) {
337d00947d8SCraig Rodrigues 		num = mp->mnt_nvnodelistsize;
338d00947d8SCraig Rodrigues 		if (num == freeing)
339996c772fSJohn Dyson 			break;
340d00947d8SCraig Rodrigues 		freeing = num;
341df8bae1dSRodney W. Grimes 	}
342df8bae1dSRodney W. Grimes 
3430864ef1eSIan Dowse 	if (error)
3440864ef1eSIan Dowse 		return (error);
345df8bae1dSRodney W. Grimes 
346d00947d8SCraig Rodrigues 	free(ump, M_UNIONFSMNT);
34711753bd0SKevin Lo 	mp->mnt_data = NULL;
348d00947d8SCraig Rodrigues 
349df8bae1dSRodney W. Grimes 	return (0);
350df8bae1dSRodney W. Grimes }
351df8bae1dSRodney W. Grimes 
35280b301c3SPoul-Henning Kamp static int
353dfd233edSAttilio Rao unionfs_root(struct mount *mp, int flags, struct vnode **vpp)
354df8bae1dSRodney W. Grimes {
355d00947d8SCraig Rodrigues 	struct unionfs_mount *ump;
356d00947d8SCraig Rodrigues 	struct vnode   *vp;
357df8bae1dSRodney W. Grimes 
358d00947d8SCraig Rodrigues 	ump = MOUNTTOUNIONFSMOUNT(mp);
359d00947d8SCraig Rodrigues 	vp = ump->um_rootvp;
3602a31267eSMatthew Dillon 
361d00947d8SCraig Rodrigues 	UNIONFSDEBUG("unionfs_root: rootvp=%p locked=%x\n",
36281c794f9SAttilio Rao 	    vp, VOP_ISLOCKED(vp));
363df8bae1dSRodney W. Grimes 
364d00947d8SCraig Rodrigues 	vref(vp);
365d00947d8SCraig Rodrigues 	if (flags & LK_TYPE_MASK)
366cb05b60aSAttilio Rao 		vn_lock(vp, flags);
367df8bae1dSRodney W. Grimes 
368d00947d8SCraig Rodrigues 	*vpp = vp;
369d00947d8SCraig Rodrigues 
370d00947d8SCraig Rodrigues 	return (0);
371df8bae1dSRodney W. Grimes }
372df8bae1dSRodney W. Grimes 
37380b301c3SPoul-Henning Kamp static int
374dfd233edSAttilio Rao unionfs_quotactl(struct mount *mp, int cmd, uid_t uid, void *arg)
375df8bae1dSRodney W. Grimes {
376d00947d8SCraig Rodrigues 	struct unionfs_mount *ump;
377df8bae1dSRodney W. Grimes 
378d00947d8SCraig Rodrigues 	ump = MOUNTTOUNIONFSMOUNT(mp);
379d00947d8SCraig Rodrigues 
380d00947d8SCraig Rodrigues 	/*
381d00947d8SCraig Rodrigues 	 * Writing is always performed to upper vnode.
382d00947d8SCraig Rodrigues 	 */
383dfd233edSAttilio Rao 	return (VFS_QUOTACTL(ump->um_uppervp->v_mount, cmd, uid, arg));
384d00947d8SCraig Rodrigues }
385d00947d8SCraig Rodrigues 
386d00947d8SCraig Rodrigues static int
387dfd233edSAttilio Rao unionfs_statfs(struct mount *mp, struct statfs *sbp)
388d00947d8SCraig Rodrigues {
389d00947d8SCraig Rodrigues 	struct unionfs_mount *ump;
390d00947d8SCraig Rodrigues 	int		error;
3912f304845SKonstantin Belousov 	struct statfs	*mstat;
392d00947d8SCraig Rodrigues 	uint64_t	lbsize;
393d00947d8SCraig Rodrigues 
394d00947d8SCraig Rodrigues 	ump = MOUNTTOUNIONFSMOUNT(mp);
395d00947d8SCraig Rodrigues 
396d00947d8SCraig Rodrigues 	UNIONFSDEBUG("unionfs_statfs(mp = %p, lvp = %p, uvp = %p)\n",
397d00947d8SCraig Rodrigues 	    (void *)mp, (void *)ump->um_lowervp, (void *)ump->um_uppervp);
398df8bae1dSRodney W. Grimes 
3992f304845SKonstantin Belousov 	mstat = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK | M_ZERO);
400df8bae1dSRodney W. Grimes 
4012f304845SKonstantin Belousov 	error = VFS_STATFS(ump->um_lowervp->v_mount, mstat);
4022f304845SKonstantin Belousov 	if (error) {
4032f304845SKonstantin Belousov 		free(mstat, M_STATFS);
404df8bae1dSRodney W. Grimes 		return (error);
4052f304845SKonstantin Belousov 	}
406d00947d8SCraig Rodrigues 
407d00947d8SCraig Rodrigues 	/* now copy across the "interesting" information and fake the rest */
4082f304845SKonstantin Belousov 	sbp->f_blocks = mstat->f_blocks;
4092f304845SKonstantin Belousov 	sbp->f_files = mstat->f_files;
410d00947d8SCraig Rodrigues 
4112f304845SKonstantin Belousov 	lbsize = mstat->f_bsize;
412d00947d8SCraig Rodrigues 
4132f304845SKonstantin Belousov 	error = VFS_STATFS(ump->um_uppervp->v_mount, mstat);
4142f304845SKonstantin Belousov 	if (error) {
4152f304845SKonstantin Belousov 		free(mstat, M_STATFS);
416d00947d8SCraig Rodrigues 		return (error);
4172f304845SKonstantin Belousov 	}
4182f304845SKonstantin Belousov 
419df8bae1dSRodney W. Grimes 
4201c4ccf09SDon Lewis 	/*
421d00947d8SCraig Rodrigues 	 * The FS type etc is copy from upper vfs.
422d00947d8SCraig Rodrigues 	 * (write able vfs have priority)
4231c4ccf09SDon Lewis 	 */
4242f304845SKonstantin Belousov 	sbp->f_type = mstat->f_type;
4252f304845SKonstantin Belousov 	sbp->f_flags = mstat->f_flags;
4262f304845SKonstantin Belousov 	sbp->f_bsize = mstat->f_bsize;
4272f304845SKonstantin Belousov 	sbp->f_iosize = mstat->f_iosize;
428df8bae1dSRodney W. Grimes 
4292f304845SKonstantin Belousov 	if (mstat->f_bsize != lbsize)
4302f304845SKonstantin Belousov 		sbp->f_blocks = ((off_t)sbp->f_blocks * lbsize) /
4312f304845SKonstantin Belousov 		    mstat->f_bsize;
432996c772fSJohn Dyson 
4332f304845SKonstantin Belousov 	sbp->f_blocks += mstat->f_blocks;
4342f304845SKonstantin Belousov 	sbp->f_bfree = mstat->f_bfree;
4352f304845SKonstantin Belousov 	sbp->f_bavail = mstat->f_bavail;
4362f304845SKonstantin Belousov 	sbp->f_files += mstat->f_files;
4372f304845SKonstantin Belousov 	sbp->f_ffree = mstat->f_ffree;
4382f304845SKonstantin Belousov 
4392f304845SKonstantin Belousov 	free(mstat, M_STATFS);
440df8bae1dSRodney W. Grimes 	return (0);
441df8bae1dSRodney W. Grimes }
442df8bae1dSRodney W. Grimes 
443d00947d8SCraig Rodrigues static int
444dfd233edSAttilio Rao unionfs_sync(struct mount *mp, int waitfor)
445d00947d8SCraig Rodrigues {
446d00947d8SCraig Rodrigues 	/* nothing to do */
447d00947d8SCraig Rodrigues 	return (0);
448d00947d8SCraig Rodrigues }
449d00947d8SCraig Rodrigues 
450d00947d8SCraig Rodrigues static int
451d00947d8SCraig Rodrigues unionfs_vget(struct mount *mp, ino_t ino, int flags, struct vnode **vpp)
452d00947d8SCraig Rodrigues {
453d00947d8SCraig Rodrigues 	return (EOPNOTSUPP);
454d00947d8SCraig Rodrigues }
455d00947d8SCraig Rodrigues 
456d00947d8SCraig Rodrigues static int
457694a586aSRick Macklem unionfs_fhtovp(struct mount *mp, struct fid *fidp, int flags,
458694a586aSRick Macklem     struct vnode **vpp)
459d00947d8SCraig Rodrigues {
460d00947d8SCraig Rodrigues 	return (EOPNOTSUPP);
461d00947d8SCraig Rodrigues }
462d00947d8SCraig Rodrigues 
463d00947d8SCraig Rodrigues static int
464d00947d8SCraig Rodrigues unionfs_checkexp(struct mount *mp, struct sockaddr *nam, int *extflagsp,
465a9148abdSDoug Rabson     struct ucred **credanonp, int *numsecflavors, int **secflavors)
466d00947d8SCraig Rodrigues {
467d00947d8SCraig Rodrigues 	return (EOPNOTSUPP);
468d00947d8SCraig Rodrigues }
469d00947d8SCraig Rodrigues 
470d00947d8SCraig Rodrigues static int
471d00947d8SCraig Rodrigues unionfs_extattrctl(struct mount *mp, int cmd, struct vnode *filename_vp,
472dfd233edSAttilio Rao     int namespace, const char *attrname)
473d00947d8SCraig Rodrigues {
474d00947d8SCraig Rodrigues 	struct unionfs_mount *ump;
475d00947d8SCraig Rodrigues 	struct unionfs_node *unp;
476d00947d8SCraig Rodrigues 
477d00947d8SCraig Rodrigues 	ump = MOUNTTOUNIONFSMOUNT(mp);
478d00947d8SCraig Rodrigues 	unp = VTOUNIONFS(filename_vp);
479d00947d8SCraig Rodrigues 
480d00947d8SCraig Rodrigues 	if (unp->un_uppervp != NULLVP) {
481d00947d8SCraig Rodrigues 		return (VFS_EXTATTRCTL(ump->um_uppervp->v_mount, cmd,
482dfd233edSAttilio Rao 		    unp->un_uppervp, namespace, attrname));
483d00947d8SCraig Rodrigues 	} else {
484d00947d8SCraig Rodrigues 		return (VFS_EXTATTRCTL(ump->um_lowervp->v_mount, cmd,
485dfd233edSAttilio Rao 		    unp->un_lowervp, namespace, attrname));
486d00947d8SCraig Rodrigues 	}
487d00947d8SCraig Rodrigues }
488d00947d8SCraig Rodrigues 
489d00947d8SCraig Rodrigues static struct vfsops unionfs_vfsops = {
490d00947d8SCraig Rodrigues 	.vfs_checkexp =		unionfs_checkexp,
491d00947d8SCraig Rodrigues 	.vfs_extattrctl =	unionfs_extattrctl,
492d00947d8SCraig Rodrigues 	.vfs_fhtovp =		unionfs_fhtovp,
493d00947d8SCraig Rodrigues 	.vfs_init =		unionfs_init,
494d00947d8SCraig Rodrigues 	.vfs_mount =		unionfs_domount,
495d00947d8SCraig Rodrigues 	.vfs_quotactl =		unionfs_quotactl,
496d00947d8SCraig Rodrigues 	.vfs_root =		unionfs_root,
497d00947d8SCraig Rodrigues 	.vfs_statfs =		unionfs_statfs,
498d00947d8SCraig Rodrigues 	.vfs_sync =		unionfs_sync,
499d00947d8SCraig Rodrigues 	.vfs_uninit =		unionfs_uninit,
500d00947d8SCraig Rodrigues 	.vfs_unmount =		unionfs_unmount,
501d00947d8SCraig Rodrigues 	.vfs_vget =		unionfs_vget,
502df8bae1dSRodney W. Grimes };
503c901836cSGarrett Wollman 
504d00947d8SCraig Rodrigues VFS_SET(unionfs_vfsops, unionfs, VFCF_LOOPBACK);
505