xref: /freebsd/sys/fs/unionfs/union_vfsops.c (revision 16385727ceeed23c377a13879b54248f72d16542)
1d167cf6fSWarner Losh /*-
2996c772fSJohn Dyson  * Copyright (c) 1994, 1995 The Regents of the University of California.
3996c772fSJohn Dyson  * Copyright (c) 1994, 1995 Jan-Simon Pendry.
4d00947d8SCraig Rodrigues  * Copyright (c) 2005, 2006 Masanori Ozawa <ozawa@ongs.co.jp>, ONGS Inc.
5d00947d8SCraig Rodrigues  * Copyright (c) 2006 Daichi Goto <daichi@freebsd.org>
6df8bae1dSRodney W. Grimes  * All rights reserved.
7df8bae1dSRodney W. Grimes  *
8df8bae1dSRodney W. Grimes  * This code is derived from software donated to Berkeley by
9df8bae1dSRodney W. Grimes  * Jan-Simon Pendry.
10df8bae1dSRodney W. Grimes  *
11df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
12df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
13df8bae1dSRodney W. Grimes  * are met:
14df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
15df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
16df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
17df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
18df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
19df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
20df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
21df8bae1dSRodney W. Grimes  *    without specific prior written permission.
22df8bae1dSRodney W. Grimes  *
23df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
34df8bae1dSRodney W. Grimes  *
35996c772fSJohn Dyson  *	@(#)union_vfsops.c	8.20 (Berkeley) 5/20/95
36c3aac50fSPeter Wemm  * $FreeBSD$
37df8bae1dSRodney W. Grimes  */
38df8bae1dSRodney W. Grimes 
39df8bae1dSRodney W. Grimes #include <sys/param.h>
40df8bae1dSRodney W. Grimes #include <sys/systm.h>
41d00947d8SCraig Rodrigues #include <sys/kdb.h>
4257b4252eSKonstantin Belousov #include <sys/fcntl.h>
43c9b1d604SGarrett Wollman #include <sys/kernel.h>
44805d90f7SJohn Baldwin #include <sys/lock.h>
45d00947d8SCraig Rodrigues #include <sys/malloc.h>
46df8bae1dSRodney W. Grimes #include <sys/mount.h>
47df8bae1dSRodney W. Grimes #include <sys/namei.h>
48d00947d8SCraig Rodrigues #include <sys/proc.h>
49d00947d8SCraig Rodrigues #include <sys/vnode.h>
50d00947d8SCraig Rodrigues #include <sys/stat.h>
51d00947d8SCraig Rodrigues 
5299d300a1SRuslan Ermilov #include <fs/unionfs/union.h>
53df8bae1dSRodney W. Grimes 
54d00947d8SCraig Rodrigues static MALLOC_DEFINE(M_UNIONFSMNT, "UNIONFS mount", "UNIONFS mount structure");
55a1c995b6SPoul-Henning Kamp 
56d00947d8SCraig Rodrigues static vfs_fhtovp_t	unionfs_fhtovp;
57d00947d8SCraig Rodrigues static vfs_checkexp_t	unionfs_checkexp;
58d00947d8SCraig Rodrigues static vfs_mount_t	unionfs_domount;
59d00947d8SCraig Rodrigues static vfs_quotactl_t	unionfs_quotactl;
60d00947d8SCraig Rodrigues static vfs_root_t	unionfs_root;
61d00947d8SCraig Rodrigues static vfs_sync_t	unionfs_sync;
62d00947d8SCraig Rodrigues static vfs_statfs_t	unionfs_statfs;
63d00947d8SCraig Rodrigues static vfs_unmount_t	unionfs_unmount;
64d00947d8SCraig Rodrigues static vfs_vget_t	unionfs_vget;
65d00947d8SCraig Rodrigues static vfs_extattrctl_t	unionfs_extattrctl;
66d00947d8SCraig Rodrigues 
67d00947d8SCraig Rodrigues static struct vfsops unionfs_vfsops;
68b5e8ce9fSBruce Evans 
69df8bae1dSRodney W. Grimes /*
70d00947d8SCraig Rodrigues  * Mount unionfs layer.
71df8bae1dSRodney W. Grimes  */
7280b301c3SPoul-Henning Kamp static int
73d00947d8SCraig Rodrigues unionfs_domount(struct mount *mp, struct thread *td)
74df8bae1dSRodney W. Grimes {
75d00947d8SCraig Rodrigues 	int		error;
76d00947d8SCraig Rodrigues 	struct vnode   *lowerrootvp;
77d00947d8SCraig Rodrigues 	struct vnode   *upperrootvp;
78d00947d8SCraig Rodrigues 	struct unionfs_mount *ump;
79d00947d8SCraig Rodrigues 	char           *target;
80d00947d8SCraig Rodrigues 	char           *tmp;
81d00947d8SCraig Rodrigues 	char           *ep;
82df8bae1dSRodney W. Grimes 	int		len;
83d00947d8SCraig Rodrigues 	size_t		done;
84d00947d8SCraig Rodrigues 	int		below;
85d00947d8SCraig Rodrigues 	uid_t		uid;
86d00947d8SCraig Rodrigues 	gid_t		gid;
87d00947d8SCraig Rodrigues 	u_short		udir;
88d00947d8SCraig Rodrigues 	u_short		ufile;
89d00947d8SCraig Rodrigues 	unionfs_copymode copymode;
9020885defSDaichi GOTO 	unionfs_whitemode whitemode;
91d1841903STim J. Robbins 	struct componentname fakecn;
92d00947d8SCraig Rodrigues 	struct nameidata nd, *ndp;
93d00947d8SCraig Rodrigues 	struct vattr	va;
94df8bae1dSRodney W. Grimes 
95d00947d8SCraig Rodrigues 	UNIONFSDEBUG("unionfs_mount(mp = %p)\n", (void *)mp);
96df8bae1dSRodney W. Grimes 
97d00947d8SCraig Rodrigues 	error = 0;
98d00947d8SCraig Rodrigues 	below = 0;
99d00947d8SCraig Rodrigues 	uid = 0;
100d00947d8SCraig Rodrigues 	gid = 0;
101d00947d8SCraig Rodrigues 	udir = 0;
102d00947d8SCraig Rodrigues 	ufile = 0;
103524f3f28SDaichi GOTO 	copymode = UNIONFS_TRANSPARENT;	/* default */
10420885defSDaichi GOTO 	whitemode = UNIONFS_WHITE_ALWAYS;
105d00947d8SCraig Rodrigues 	ndp = &nd;
10681bca6ddSKATO Takenori 
1071e370dbbSCraig Rodrigues 	if (mp->mnt_flag & MNT_ROOTFS) {
1081e370dbbSCraig Rodrigues 		vfs_mount_error(mp, "Cannot union mount root filesystem");
10964042a76SPoul-Henning Kamp 		return (EOPNOTSUPP);
1101e370dbbSCraig Rodrigues 	}
111d00947d8SCraig Rodrigues 
11281bca6ddSKATO Takenori 	/*
113d00947d8SCraig Rodrigues 	 * Update is a no operation.
114df8bae1dSRodney W. Grimes 	 */
1151e370dbbSCraig Rodrigues 	if (mp->mnt_flag & MNT_UPDATE) {
1161e370dbbSCraig Rodrigues 		vfs_mount_error(mp, "unionfs does not support mount update");
117a9f5c04aSMaxime Henrion 		return (EOPNOTSUPP);
1181e370dbbSCraig Rodrigues 	}
119df8bae1dSRodney W. Grimes 
120df8bae1dSRodney W. Grimes 	/*
121d00947d8SCraig Rodrigues 	 * Get argument
122df8bae1dSRodney W. Grimes 	 */
123d00947d8SCraig Rodrigues 	error = vfs_getopt(mp->mnt_optnew, "target", (void **)&target, &len);
1243a773ad0SPoul-Henning Kamp 	if (error)
125d00947d8SCraig Rodrigues 		error = vfs_getopt(mp->mnt_optnew, "from", (void **)&target,
126d00947d8SCraig Rodrigues 		    &len);
127d00947d8SCraig Rodrigues 	if (error || target[len - 1] != '\0') {
128d00947d8SCraig Rodrigues 		vfs_mount_error(mp, "Invalid target");
129d00947d8SCraig Rodrigues 		return (EINVAL);
130d00947d8SCraig Rodrigues 	}
131d00947d8SCraig Rodrigues 	if (vfs_getopt(mp->mnt_optnew, "below", NULL, NULL) == 0)
132d00947d8SCraig Rodrigues 		below = 1;
133d00947d8SCraig Rodrigues 	if (vfs_getopt(mp->mnt_optnew, "udir", (void **)&tmp, NULL) == 0) {
134d00947d8SCraig Rodrigues 		if (tmp != NULL)
135d00947d8SCraig Rodrigues 			udir = (mode_t)strtol(tmp, &ep, 8);
136d00947d8SCraig Rodrigues 		if (tmp == NULL || *ep) {
137d00947d8SCraig Rodrigues 			vfs_mount_error(mp, "Invalid udir");
138d00947d8SCraig Rodrigues 			return (EINVAL);
139d00947d8SCraig Rodrigues 		}
14016385727SDaichi GOTO 		udir &= S_IRWXU | S_IRWXG | S_IRWXO;
141d00947d8SCraig Rodrigues 	}
142d00947d8SCraig Rodrigues 	if (vfs_getopt(mp->mnt_optnew, "ufile", (void **)&tmp, NULL) == 0) {
143d00947d8SCraig Rodrigues 		if (tmp != NULL)
144d00947d8SCraig Rodrigues 			ufile = (mode_t)strtol(tmp, &ep, 8);
145d00947d8SCraig Rodrigues 		if (tmp == NULL || *ep) {
146d00947d8SCraig Rodrigues 			vfs_mount_error(mp, "Invalid ufile");
147d00947d8SCraig Rodrigues 			return (EINVAL);
148d00947d8SCraig Rodrigues 		}
14916385727SDaichi GOTO 		ufile &= S_IRWXU | S_IRWXG | S_IRWXO;
150d00947d8SCraig Rodrigues 	}
151d00947d8SCraig Rodrigues 	/* check umask, uid and gid */
152d00947d8SCraig Rodrigues 	if (udir == 0 && ufile != 0)
153d00947d8SCraig Rodrigues 		udir = ufile;
154d00947d8SCraig Rodrigues 	if (ufile == 0 && udir != 0)
155d00947d8SCraig Rodrigues 		ufile = udir;
156d00947d8SCraig Rodrigues 
157cb05b60aSAttilio Rao 	vn_lock(mp->mnt_vnodecovered, LK_SHARED | LK_RETRY);
1580359a12eSAttilio Rao 	error = VOP_GETATTR(mp->mnt_vnodecovered, &va, mp->mnt_cred);
159d00947d8SCraig Rodrigues 	if (!error) {
160d00947d8SCraig Rodrigues 		if (udir == 0)
161d00947d8SCraig Rodrigues 			udir = va.va_mode;
162d00947d8SCraig Rodrigues 		if (ufile == 0)
163d00947d8SCraig Rodrigues 			ufile = va.va_mode;
164d00947d8SCraig Rodrigues 		uid = va.va_uid;
165d00947d8SCraig Rodrigues 		gid = va.va_gid;
166d00947d8SCraig Rodrigues 	}
16722db15c0SAttilio Rao 	VOP_UNLOCK(mp->mnt_vnodecovered, 0);
168d00947d8SCraig Rodrigues 	if (error)
169d00947d8SCraig Rodrigues 		return (error);
170d00947d8SCraig Rodrigues 
171d00947d8SCraig Rodrigues 	if (mp->mnt_cred->cr_ruid == 0) {	/* root only */
172d00947d8SCraig Rodrigues 		if (vfs_getopt(mp->mnt_optnew, "uid", (void **)&tmp,
173d00947d8SCraig Rodrigues 		    NULL) == 0) {
174d00947d8SCraig Rodrigues 			if (tmp != NULL)
175d00947d8SCraig Rodrigues 				uid = (uid_t)strtol(tmp, &ep, 10);
176d00947d8SCraig Rodrigues 			if (tmp == NULL || *ep) {
177d00947d8SCraig Rodrigues 				vfs_mount_error(mp, "Invalid uid");
178d00947d8SCraig Rodrigues 				return (EINVAL);
179d00947d8SCraig Rodrigues 			}
180d00947d8SCraig Rodrigues 		}
181d00947d8SCraig Rodrigues 		if (vfs_getopt(mp->mnt_optnew, "gid", (void **)&tmp,
182d00947d8SCraig Rodrigues 		    NULL) == 0) {
183d00947d8SCraig Rodrigues 			if (tmp != NULL)
184d00947d8SCraig Rodrigues 				gid = (gid_t)strtol(tmp, &ep, 10);
185d00947d8SCraig Rodrigues 			if (tmp == NULL || *ep) {
186d00947d8SCraig Rodrigues 				vfs_mount_error(mp, "Invalid gid");
187d00947d8SCraig Rodrigues 				return (EINVAL);
188d00947d8SCraig Rodrigues 			}
189d00947d8SCraig Rodrigues 		}
190d00947d8SCraig Rodrigues 		if (vfs_getopt(mp->mnt_optnew, "copymode", (void **)&tmp,
191d00947d8SCraig Rodrigues 		    NULL) == 0) {
192d00947d8SCraig Rodrigues 			if (tmp == NULL) {
193d00947d8SCraig Rodrigues 				vfs_mount_error(mp, "Invalid copymode");
194d00947d8SCraig Rodrigues 				return (EINVAL);
195d00947d8SCraig Rodrigues 			} else if (strcasecmp(tmp, "traditional") == 0)
196d00947d8SCraig Rodrigues 				copymode = UNIONFS_TRADITIONAL;
197d00947d8SCraig Rodrigues 			else if (strcasecmp(tmp, "transparent") == 0)
198d00947d8SCraig Rodrigues 				copymode = UNIONFS_TRANSPARENT;
199d00947d8SCraig Rodrigues 			else if (strcasecmp(tmp, "masquerade") == 0)
200d00947d8SCraig Rodrigues 				copymode = UNIONFS_MASQUERADE;
201d00947d8SCraig Rodrigues 			else {
202d00947d8SCraig Rodrigues 				vfs_mount_error(mp, "Invalid copymode");
203d00947d8SCraig Rodrigues 				return (EINVAL);
204d00947d8SCraig Rodrigues 			}
205d00947d8SCraig Rodrigues 		}
20620885defSDaichi GOTO 		if (vfs_getopt(mp->mnt_optnew, "whiteout", (void **)&tmp,
20720885defSDaichi GOTO 		    NULL) == 0) {
20820885defSDaichi GOTO 			if (tmp == NULL) {
20920885defSDaichi GOTO 				vfs_mount_error(mp, "Invalid whiteout mode");
21020885defSDaichi GOTO 				return (EINVAL);
21120885defSDaichi GOTO 			} else if (strcasecmp(tmp, "always") == 0)
21220885defSDaichi GOTO 				whitemode = UNIONFS_WHITE_ALWAYS;
21320885defSDaichi GOTO 			else if (strcasecmp(tmp, "whenneeded") == 0)
21420885defSDaichi GOTO 				whitemode = UNIONFS_WHITE_WHENNEEDED;
21520885defSDaichi GOTO 			else {
21620885defSDaichi GOTO 				vfs_mount_error(mp, "Invalid whiteout mode");
21720885defSDaichi GOTO 				return (EINVAL);
21820885defSDaichi GOTO 			}
21920885defSDaichi GOTO 		}
220d00947d8SCraig Rodrigues 	}
221d00947d8SCraig Rodrigues 	/* If copymode is UNIONFS_TRADITIONAL, uid/gid is mounted user. */
222d00947d8SCraig Rodrigues 	if (copymode == UNIONFS_TRADITIONAL) {
223d00947d8SCraig Rodrigues 		uid = mp->mnt_cred->cr_ruid;
224d00947d8SCraig Rodrigues 		gid = mp->mnt_cred->cr_rgid;
225d00947d8SCraig Rodrigues 	}
226d00947d8SCraig Rodrigues 
227d00947d8SCraig Rodrigues 	UNIONFSDEBUG("unionfs_mount: uid=%d, gid=%d\n", uid, gid);
228d00947d8SCraig Rodrigues 	UNIONFSDEBUG("unionfs_mount: udir=0%03o, ufile=0%03o\n", udir, ufile);
229d00947d8SCraig Rodrigues 	UNIONFSDEBUG("unionfs_mount: copymode=%d\n", copymode);
230d00947d8SCraig Rodrigues 
231d00947d8SCraig Rodrigues 	/*
232d00947d8SCraig Rodrigues 	 * Find upper node
233d00947d8SCraig Rodrigues 	 */
2347265164fSJohn Baldwin 	NDINIT(ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, target, td);
235d00947d8SCraig Rodrigues 	if ((error = namei(ndp)))
236d00947d8SCraig Rodrigues 		return (error);
237d00947d8SCraig Rodrigues 
238762e6b85SEivind Eklund 	NDFREE(ndp, NDF_ONLY_PNBUF);
239d00947d8SCraig Rodrigues 
240d00947d8SCraig Rodrigues 	/* get root vnodes */
241d00947d8SCraig Rodrigues 	lowerrootvp = mp->mnt_vnodecovered;
242df8bae1dSRodney W. Grimes 	upperrootvp = ndp->ni_vp;
243df8bae1dSRodney W. Grimes 
244d00947d8SCraig Rodrigues 	/* create unionfs_mount */
245d00947d8SCraig Rodrigues 	ump = (struct unionfs_mount *)malloc(sizeof(struct unionfs_mount),
246a163d034SWarner Losh 	    M_UNIONFSMNT, M_WAITOK | M_ZERO);
2472a31267eSMatthew Dillon 
248d00947d8SCraig Rodrigues 	/*
249d00947d8SCraig Rodrigues 	 * Save reference
250d00947d8SCraig Rodrigues 	 */
251d00947d8SCraig Rodrigues 	if (below) {
25222db15c0SAttilio Rao 		VOP_UNLOCK(upperrootvp, 0);
253cb05b60aSAttilio Rao 		vn_lock(lowerrootvp, LK_EXCLUSIVE | LK_RETRY);
254d00947d8SCraig Rodrigues 		ump->um_lowervp = upperrootvp;
255d00947d8SCraig Rodrigues 		ump->um_uppervp = lowerrootvp;
256d00947d8SCraig Rodrigues 	} else {
257d00947d8SCraig Rodrigues 		ump->um_lowervp = lowerrootvp;
258d00947d8SCraig Rodrigues 		ump->um_uppervp = upperrootvp;
259df8bae1dSRodney W. Grimes 	}
260d00947d8SCraig Rodrigues 	ump->um_rootvp = NULLVP;
261d00947d8SCraig Rodrigues 	ump->um_uid = uid;
262d00947d8SCraig Rodrigues 	ump->um_gid = gid;
263d00947d8SCraig Rodrigues 	ump->um_udir = udir;
264d00947d8SCraig Rodrigues 	ump->um_ufile = ufile;
265d00947d8SCraig Rodrigues 	ump->um_copymode = copymode;
26620885defSDaichi GOTO 	ump->um_whitemode = whitemode;
267d00947d8SCraig Rodrigues 
26857821163SDaichi GOTO 	MNT_ILOCK(mp);
26957821163SDaichi GOTO 	if ((lowerrootvp->v_mount->mnt_kern_flag & MNTK_MPSAFE) &&
27057821163SDaichi GOTO 	    (upperrootvp->v_mount->mnt_kern_flag & MNTK_MPSAFE))
27157821163SDaichi GOTO 		mp->mnt_kern_flag |= MNTK_MPSAFE;
27257821163SDaichi GOTO 	MNT_IUNLOCK(mp);
27377465d93SAlfred Perlstein 	mp->mnt_data = ump;
274df8bae1dSRodney W. Grimes 
275996c772fSJohn Dyson 	/*
276d00947d8SCraig Rodrigues 	 * Copy upper layer's RDONLY flag.
277d00947d8SCraig Rodrigues 	 */
278d00947d8SCraig Rodrigues 	mp->mnt_flag |= ump->um_uppervp->v_mount->mnt_flag & MNT_RDONLY;
279d00947d8SCraig Rodrigues 
280d00947d8SCraig Rodrigues 	/*
281d00947d8SCraig Rodrigues 	 * Check whiteout
282996c772fSJohn Dyson 	 */
283996c772fSJohn Dyson 	if ((mp->mnt_flag & MNT_RDONLY) == 0) {
284d00947d8SCraig Rodrigues 		memset(&fakecn, 0, sizeof(fakecn));
285d1841903STim J. Robbins 		fakecn.cn_nameiop = LOOKUP;
286d1841903STim J. Robbins 		fakecn.cn_thread = td;
287d00947d8SCraig Rodrigues 		error = VOP_WHITEOUT(ump->um_uppervp, &fakecn, LOOKUP);
288d00947d8SCraig Rodrigues 		if (error) {
289d00947d8SCraig Rodrigues 			if (below) {
29022db15c0SAttilio Rao 				VOP_UNLOCK(ump->um_uppervp, 0);
291d00947d8SCraig Rodrigues 				vrele(upperrootvp);
292d00947d8SCraig Rodrigues 			} else
293d00947d8SCraig Rodrigues 				vput(ump->um_uppervp);
294d00947d8SCraig Rodrigues 			free(ump, M_UNIONFSMNT);
295d00947d8SCraig Rodrigues 			mp->mnt_data = NULL;
296d00947d8SCraig Rodrigues 			return (error);
2975da56ddbSTor Egge 		}
298df8bae1dSRodney W. Grimes 	}
299df8bae1dSRodney W. Grimes 
300df8bae1dSRodney W. Grimes 	/*
301d00947d8SCraig Rodrigues 	 * Unlock the node
302df8bae1dSRodney W. Grimes 	 */
30322db15c0SAttilio Rao 	VOP_UNLOCK(ump->um_uppervp, 0);
304df8bae1dSRodney W. Grimes 
305d00947d8SCraig Rodrigues 	/*
306d00947d8SCraig Rodrigues 	 * Get the unionfs root vnode.
307d00947d8SCraig Rodrigues 	 */
308d00947d8SCraig Rodrigues 	error = unionfs_nodeget(mp, ump->um_uppervp, ump->um_lowervp,
309d00947d8SCraig Rodrigues 	    NULLVP, &(ump->um_rootvp), NULL, td);
310d00947d8SCraig Rodrigues 	vrele(upperrootvp);
3117d72c5e6SDaichi GOTO 	if (error) {
312d00947d8SCraig Rodrigues 		free(ump, M_UNIONFSMNT);
313d00947d8SCraig Rodrigues 		mp->mnt_data = NULL;
314df8bae1dSRodney W. Grimes 		return (error);
315df8bae1dSRodney W. Grimes 	}
316df8bae1dSRodney W. Grimes 
317df8bae1dSRodney W. Grimes 	/*
318d00947d8SCraig Rodrigues 	 * Check mnt_flag
319d00947d8SCraig Rodrigues 	 */
320d00947d8SCraig Rodrigues 	if ((ump->um_lowervp->v_mount->mnt_flag & MNT_LOCAL) &&
321d00947d8SCraig Rodrigues 	    (ump->um_uppervp->v_mount->mnt_flag & MNT_LOCAL))
322d00947d8SCraig Rodrigues 		mp->mnt_flag |= MNT_LOCAL;
323d00947d8SCraig Rodrigues 
324d00947d8SCraig Rodrigues 	/*
325d00947d8SCraig Rodrigues 	 * Get new fsid
326d00947d8SCraig Rodrigues 	 */
327d00947d8SCraig Rodrigues 	vfs_getnewfsid(mp);
328d00947d8SCraig Rodrigues 
329d00947d8SCraig Rodrigues 	len = MNAMELEN - 1;
330d00947d8SCraig Rodrigues 	tmp = mp->mnt_stat.f_mntfromname;
331d00947d8SCraig Rodrigues 	copystr((below ? "<below>:" : "<above>:"), tmp, len, &done);
332d00947d8SCraig Rodrigues 	len -= done - 1;
333d00947d8SCraig Rodrigues 	tmp += done - 1;
334d00947d8SCraig Rodrigues 	copystr(target, tmp, len, NULL);
335d00947d8SCraig Rodrigues 
336d00947d8SCraig Rodrigues 	UNIONFSDEBUG("unionfs_mount: from %s, on %s\n",
337d00947d8SCraig Rodrigues 	    mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname);
338d00947d8SCraig Rodrigues 
339d00947d8SCraig Rodrigues 	return (0);
340d00947d8SCraig Rodrigues }
341d00947d8SCraig Rodrigues 
342d00947d8SCraig Rodrigues /*
343d00947d8SCraig Rodrigues  * Free reference to unionfs layer
344df8bae1dSRodney W. Grimes  */
34580b301c3SPoul-Henning Kamp static int
346d00947d8SCraig Rodrigues unionfs_unmount(struct mount *mp, int mntflags, struct thread *td)
347df8bae1dSRodney W. Grimes {
348d00947d8SCraig Rodrigues 	struct unionfs_mount *ump;
349df8bae1dSRodney W. Grimes 	int		error;
350d00947d8SCraig Rodrigues 	int		num;
351996c772fSJohn Dyson 	int		freeing;
352d00947d8SCraig Rodrigues 	int		flags;
353df8bae1dSRodney W. Grimes 
354d00947d8SCraig Rodrigues 	UNIONFSDEBUG("unionfs_unmount: mp = %p\n", (void *)mp);
355d00947d8SCraig Rodrigues 
356d00947d8SCraig Rodrigues 	ump = MOUNTTOUNIONFSMOUNT(mp);
357d00947d8SCraig Rodrigues 	flags = 0;
358df8bae1dSRodney W. Grimes 
359996c772fSJohn Dyson 	if (mntflags & MNT_FORCE)
360996c772fSJohn Dyson 		flags |= FORCECLOSE;
361996c772fSJohn Dyson 
362d00947d8SCraig Rodrigues 	/* vflush (no need to call vrele) */
363d00947d8SCraig Rodrigues 	for (freeing = 0; (error = vflush(mp, 1, flags, td)) != 0;) {
364d00947d8SCraig Rodrigues 		num = mp->mnt_nvnodelistsize;
365d00947d8SCraig Rodrigues 		if (num == freeing)
366996c772fSJohn Dyson 			break;
367d00947d8SCraig Rodrigues 		freeing = num;
368df8bae1dSRodney W. Grimes 	}
369df8bae1dSRodney W. Grimes 
3700864ef1eSIan Dowse 	if (error)
3710864ef1eSIan Dowse 		return (error);
372df8bae1dSRodney W. Grimes 
373d00947d8SCraig Rodrigues 	free(ump, M_UNIONFSMNT);
374df8bae1dSRodney W. Grimes 	mp->mnt_data = 0;
375d00947d8SCraig Rodrigues 
376df8bae1dSRodney W. Grimes 	return (0);
377df8bae1dSRodney W. Grimes }
378df8bae1dSRodney W. Grimes 
37980b301c3SPoul-Henning Kamp static int
380d00947d8SCraig Rodrigues unionfs_root(struct mount *mp, int flags, struct vnode **vpp, struct thread *td)
381df8bae1dSRodney W. Grimes {
382d00947d8SCraig Rodrigues 	struct unionfs_mount *ump;
383d00947d8SCraig Rodrigues 	struct vnode   *vp;
384df8bae1dSRodney W. Grimes 
385d00947d8SCraig Rodrigues 	ump = MOUNTTOUNIONFSMOUNT(mp);
386d00947d8SCraig Rodrigues 	vp = ump->um_rootvp;
3872a31267eSMatthew Dillon 
388d00947d8SCraig Rodrigues 	UNIONFSDEBUG("unionfs_root: rootvp=%p locked=%x\n",
38981c794f9SAttilio Rao 	    vp, VOP_ISLOCKED(vp));
390df8bae1dSRodney W. Grimes 
391d00947d8SCraig Rodrigues 	vref(vp);
392d00947d8SCraig Rodrigues 	if (flags & LK_TYPE_MASK)
393cb05b60aSAttilio Rao 		vn_lock(vp, flags);
394df8bae1dSRodney W. Grimes 
395d00947d8SCraig Rodrigues 	*vpp = vp;
396d00947d8SCraig Rodrigues 
397d00947d8SCraig Rodrigues 	return (0);
398df8bae1dSRodney W. Grimes }
399df8bae1dSRodney W. Grimes 
40080b301c3SPoul-Henning Kamp static int
401d00947d8SCraig Rodrigues unionfs_quotactl(struct mount *mp, int cmd, uid_t uid, void *arg,
402d00947d8SCraig Rodrigues     struct thread *td)
403df8bae1dSRodney W. Grimes {
404d00947d8SCraig Rodrigues 	struct unionfs_mount *ump;
405df8bae1dSRodney W. Grimes 
406d00947d8SCraig Rodrigues 	ump = MOUNTTOUNIONFSMOUNT(mp);
407d00947d8SCraig Rodrigues 
408d00947d8SCraig Rodrigues 	/*
409d00947d8SCraig Rodrigues 	 * Writing is always performed to upper vnode.
410d00947d8SCraig Rodrigues 	 */
411d00947d8SCraig Rodrigues 	return (VFS_QUOTACTL(ump->um_uppervp->v_mount, cmd, uid, arg, td));
412d00947d8SCraig Rodrigues }
413d00947d8SCraig Rodrigues 
414d00947d8SCraig Rodrigues static int
415d00947d8SCraig Rodrigues unionfs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td)
416d00947d8SCraig Rodrigues {
417d00947d8SCraig Rodrigues 	struct unionfs_mount *ump;
418d00947d8SCraig Rodrigues 	int		error;
419d00947d8SCraig Rodrigues 	struct statfs	mstat;
420d00947d8SCraig Rodrigues 	uint64_t	lbsize;
421d00947d8SCraig Rodrigues 
422d00947d8SCraig Rodrigues 	ump = MOUNTTOUNIONFSMOUNT(mp);
423d00947d8SCraig Rodrigues 
424d00947d8SCraig Rodrigues 	UNIONFSDEBUG("unionfs_statfs(mp = %p, lvp = %p, uvp = %p)\n",
425d00947d8SCraig Rodrigues 	    (void *)mp, (void *)ump->um_lowervp, (void *)ump->um_uppervp);
426df8bae1dSRodney W. Grimes 
427df8bae1dSRodney W. Grimes 	bzero(&mstat, sizeof(mstat));
428df8bae1dSRodney W. Grimes 
429d00947d8SCraig Rodrigues 	error = VFS_STATFS(ump->um_lowervp->v_mount, &mstat, td);
430df8bae1dSRodney W. Grimes 	if (error)
431df8bae1dSRodney W. Grimes 		return (error);
432d00947d8SCraig Rodrigues 
433d00947d8SCraig Rodrigues 	/* now copy across the "interesting" information and fake the rest */
434d00947d8SCraig Rodrigues 	sbp->f_blocks = mstat.f_blocks;
435d00947d8SCraig Rodrigues 	sbp->f_files = mstat.f_files;
436d00947d8SCraig Rodrigues 
437d00947d8SCraig Rodrigues 	lbsize = mstat.f_bsize;
438d00947d8SCraig Rodrigues 
439d00947d8SCraig Rodrigues 	error = VFS_STATFS(ump->um_uppervp->v_mount, &mstat, td);
440d00947d8SCraig Rodrigues 	if (error)
441d00947d8SCraig Rodrigues 		return (error);
442df8bae1dSRodney W. Grimes 
4431c4ccf09SDon Lewis 	/*
444d00947d8SCraig Rodrigues 	 * The FS type etc is copy from upper vfs.
445d00947d8SCraig Rodrigues 	 * (write able vfs have priority)
4461c4ccf09SDon Lewis 	 */
447df8bae1dSRodney W. Grimes 	sbp->f_type = mstat.f_type;
448df8bae1dSRodney W. Grimes 	sbp->f_flags = mstat.f_flags;
449df8bae1dSRodney W. Grimes 	sbp->f_bsize = mstat.f_bsize;
450df8bae1dSRodney W. Grimes 	sbp->f_iosize = mstat.f_iosize;
451df8bae1dSRodney W. Grimes 
452996c772fSJohn Dyson 	if (mstat.f_bsize != lbsize)
453c9bf0111SKATO Takenori 		sbp->f_blocks = ((off_t)sbp->f_blocks * lbsize) / mstat.f_bsize;
454996c772fSJohn Dyson 
455df8bae1dSRodney W. Grimes 	sbp->f_blocks += mstat.f_blocks;
456996c772fSJohn Dyson 	sbp->f_bfree = mstat.f_bfree;
457996c772fSJohn Dyson 	sbp->f_bavail = mstat.f_bavail;
458df8bae1dSRodney W. Grimes 	sbp->f_files += mstat.f_files;
459996c772fSJohn Dyson 	sbp->f_ffree = mstat.f_ffree;
460df8bae1dSRodney W. Grimes 	return (0);
461df8bae1dSRodney W. Grimes }
462df8bae1dSRodney W. Grimes 
463d00947d8SCraig Rodrigues static int
464d00947d8SCraig Rodrigues unionfs_sync(struct mount *mp, int waitfor, struct thread *td)
465d00947d8SCraig Rodrigues {
466d00947d8SCraig Rodrigues 	/* nothing to do */
467d00947d8SCraig Rodrigues 	return (0);
468d00947d8SCraig Rodrigues }
469d00947d8SCraig Rodrigues 
470d00947d8SCraig Rodrigues static int
471d00947d8SCraig Rodrigues unionfs_vget(struct mount *mp, ino_t ino, int flags, struct vnode **vpp)
472d00947d8SCraig Rodrigues {
473d00947d8SCraig Rodrigues 	return (EOPNOTSUPP);
474d00947d8SCraig Rodrigues }
475d00947d8SCraig Rodrigues 
476d00947d8SCraig Rodrigues static int
477d00947d8SCraig Rodrigues unionfs_fhtovp(struct mount *mp, struct fid *fidp, struct vnode **vpp)
478d00947d8SCraig Rodrigues {
479d00947d8SCraig Rodrigues 	return (EOPNOTSUPP);
480d00947d8SCraig Rodrigues }
481d00947d8SCraig Rodrigues 
482d00947d8SCraig Rodrigues static int
483d00947d8SCraig Rodrigues unionfs_checkexp(struct mount *mp, struct sockaddr *nam, int *extflagsp,
484a9148abdSDoug Rabson     struct ucred **credanonp, int *numsecflavors, int **secflavors)
485d00947d8SCraig Rodrigues {
486d00947d8SCraig Rodrigues 	return (EOPNOTSUPP);
487d00947d8SCraig Rodrigues }
488d00947d8SCraig Rodrigues 
489d00947d8SCraig Rodrigues static int
490d00947d8SCraig Rodrigues unionfs_extattrctl(struct mount *mp, int cmd, struct vnode *filename_vp,
491d00947d8SCraig Rodrigues     int namespace, const char *attrname, struct thread *td)
492d00947d8SCraig Rodrigues {
493d00947d8SCraig Rodrigues 	struct unionfs_mount *ump;
494d00947d8SCraig Rodrigues 	struct unionfs_node *unp;
495d00947d8SCraig Rodrigues 
496d00947d8SCraig Rodrigues 	ump = MOUNTTOUNIONFSMOUNT(mp);
497d00947d8SCraig Rodrigues 	unp = VTOUNIONFS(filename_vp);
498d00947d8SCraig Rodrigues 
499d00947d8SCraig Rodrigues 	if (unp->un_uppervp != NULLVP) {
500d00947d8SCraig Rodrigues 		return (VFS_EXTATTRCTL(ump->um_uppervp->v_mount, cmd,
501d00947d8SCraig Rodrigues 		    unp->un_uppervp, namespace, attrname, td));
502d00947d8SCraig Rodrigues 	} else {
503d00947d8SCraig Rodrigues 		return (VFS_EXTATTRCTL(ump->um_lowervp->v_mount, cmd,
504d00947d8SCraig Rodrigues 		    unp->un_lowervp, namespace, attrname, td));
505d00947d8SCraig Rodrigues 	}
506d00947d8SCraig Rodrigues }
507d00947d8SCraig Rodrigues 
508d00947d8SCraig Rodrigues static struct vfsops unionfs_vfsops = {
509d00947d8SCraig Rodrigues 	.vfs_checkexp =		unionfs_checkexp,
510d00947d8SCraig Rodrigues 	.vfs_extattrctl =	unionfs_extattrctl,
511d00947d8SCraig Rodrigues 	.vfs_fhtovp =		unionfs_fhtovp,
512d00947d8SCraig Rodrigues 	.vfs_init =		unionfs_init,
513d00947d8SCraig Rodrigues 	.vfs_mount =		unionfs_domount,
514d00947d8SCraig Rodrigues 	.vfs_quotactl =		unionfs_quotactl,
515d00947d8SCraig Rodrigues 	.vfs_root =		unionfs_root,
516d00947d8SCraig Rodrigues 	.vfs_statfs =		unionfs_statfs,
517d00947d8SCraig Rodrigues 	.vfs_sync =		unionfs_sync,
518d00947d8SCraig Rodrigues 	.vfs_uninit =		unionfs_uninit,
519d00947d8SCraig Rodrigues 	.vfs_unmount =		unionfs_unmount,
520d00947d8SCraig Rodrigues 	.vfs_vget =		unionfs_vget,
521df8bae1dSRodney W. Grimes };
522c901836cSGarrett Wollman 
523d00947d8SCraig Rodrigues VFS_SET(unionfs_vfsops, unionfs, VFCF_LOOPBACK);
524