xref: /freebsd/sys/fs/unionfs/union_vfsops.c (revision dfd233edd5040ed82a16a0074e220188e2c67ad4)
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
73dfd233edSAttilio Rao unionfs_domount(struct mount *mp)
74df8bae1dSRodney W. Grimes {
75d00947d8SCraig Rodrigues 	int		error;
76d00947d8SCraig Rodrigues 	struct vnode   *lowerrootvp;
77d00947d8SCraig Rodrigues 	struct vnode   *upperrootvp;
78d00947d8SCraig Rodrigues 	struct unionfs_mount *ump;
79dfd233edSAttilio Rao 	struct thread *td;
80d00947d8SCraig Rodrigues 	char           *target;
81d00947d8SCraig Rodrigues 	char           *tmp;
82d00947d8SCraig Rodrigues 	char           *ep;
83df8bae1dSRodney W. Grimes 	int		len;
84d00947d8SCraig Rodrigues 	size_t		done;
85d00947d8SCraig Rodrigues 	int		below;
86d00947d8SCraig Rodrigues 	uid_t		uid;
87d00947d8SCraig Rodrigues 	gid_t		gid;
88d00947d8SCraig Rodrigues 	u_short		udir;
89d00947d8SCraig Rodrigues 	u_short		ufile;
90d00947d8SCraig Rodrigues 	unionfs_copymode copymode;
9120885defSDaichi GOTO 	unionfs_whitemode whitemode;
92d1841903STim J. Robbins 	struct componentname fakecn;
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 	}
16922db15c0SAttilio Rao 	VOP_UNLOCK(mp->mnt_vnodecovered, 0);
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) {
25422db15c0SAttilio Rao 		VOP_UNLOCK(upperrootvp, 0);
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 
27057821163SDaichi GOTO 	MNT_ILOCK(mp);
27157821163SDaichi GOTO 	if ((lowerrootvp->v_mount->mnt_kern_flag & MNTK_MPSAFE) &&
27257821163SDaichi GOTO 	    (upperrootvp->v_mount->mnt_kern_flag & MNTK_MPSAFE))
27357821163SDaichi GOTO 		mp->mnt_kern_flag |= MNTK_MPSAFE;
27457821163SDaichi GOTO 	MNT_IUNLOCK(mp);
27577465d93SAlfred Perlstein 	mp->mnt_data = ump;
276df8bae1dSRodney W. Grimes 
277996c772fSJohn Dyson 	/*
278d00947d8SCraig Rodrigues 	 * Copy upper layer's RDONLY flag.
279d00947d8SCraig Rodrigues 	 */
280d00947d8SCraig Rodrigues 	mp->mnt_flag |= ump->um_uppervp->v_mount->mnt_flag & MNT_RDONLY;
281d00947d8SCraig Rodrigues 
282d00947d8SCraig Rodrigues 	/*
283d00947d8SCraig Rodrigues 	 * Check whiteout
284996c772fSJohn Dyson 	 */
285996c772fSJohn Dyson 	if ((mp->mnt_flag & MNT_RDONLY) == 0) {
286d00947d8SCraig Rodrigues 		memset(&fakecn, 0, sizeof(fakecn));
287d1841903STim J. Robbins 		fakecn.cn_nameiop = LOOKUP;
288d1841903STim J. Robbins 		fakecn.cn_thread = td;
289d00947d8SCraig Rodrigues 		error = VOP_WHITEOUT(ump->um_uppervp, &fakecn, LOOKUP);
290d00947d8SCraig Rodrigues 		if (error) {
291d00947d8SCraig Rodrigues 			if (below) {
29222db15c0SAttilio Rao 				VOP_UNLOCK(ump->um_uppervp, 0);
293d00947d8SCraig Rodrigues 				vrele(upperrootvp);
294d00947d8SCraig Rodrigues 			} else
295d00947d8SCraig Rodrigues 				vput(ump->um_uppervp);
296d00947d8SCraig Rodrigues 			free(ump, M_UNIONFSMNT);
297d00947d8SCraig Rodrigues 			mp->mnt_data = NULL;
298d00947d8SCraig Rodrigues 			return (error);
2995da56ddbSTor Egge 		}
300df8bae1dSRodney W. Grimes 	}
301df8bae1dSRodney W. Grimes 
302df8bae1dSRodney W. Grimes 	/*
303d00947d8SCraig Rodrigues 	 * Unlock the node
304df8bae1dSRodney W. Grimes 	 */
30522db15c0SAttilio Rao 	VOP_UNLOCK(ump->um_uppervp, 0);
306df8bae1dSRodney W. Grimes 
307d00947d8SCraig Rodrigues 	/*
308d00947d8SCraig Rodrigues 	 * Get the unionfs root vnode.
309d00947d8SCraig Rodrigues 	 */
310d00947d8SCraig Rodrigues 	error = unionfs_nodeget(mp, ump->um_uppervp, ump->um_lowervp,
311d00947d8SCraig Rodrigues 	    NULLVP, &(ump->um_rootvp), NULL, td);
312d00947d8SCraig Rodrigues 	vrele(upperrootvp);
3137d72c5e6SDaichi GOTO 	if (error) {
314d00947d8SCraig Rodrigues 		free(ump, M_UNIONFSMNT);
315d00947d8SCraig Rodrigues 		mp->mnt_data = NULL;
316df8bae1dSRodney W. Grimes 		return (error);
317df8bae1dSRodney W. Grimes 	}
318df8bae1dSRodney W. Grimes 
319df8bae1dSRodney W. Grimes 	/*
320d00947d8SCraig Rodrigues 	 * Check mnt_flag
321d00947d8SCraig Rodrigues 	 */
322d00947d8SCraig Rodrigues 	if ((ump->um_lowervp->v_mount->mnt_flag & MNT_LOCAL) &&
323d00947d8SCraig Rodrigues 	    (ump->um_uppervp->v_mount->mnt_flag & MNT_LOCAL))
324d00947d8SCraig Rodrigues 		mp->mnt_flag |= MNT_LOCAL;
325d00947d8SCraig Rodrigues 
326d00947d8SCraig Rodrigues 	/*
327d00947d8SCraig Rodrigues 	 * Get new fsid
328d00947d8SCraig Rodrigues 	 */
329d00947d8SCraig Rodrigues 	vfs_getnewfsid(mp);
330d00947d8SCraig Rodrigues 
331d00947d8SCraig Rodrigues 	len = MNAMELEN - 1;
332d00947d8SCraig Rodrigues 	tmp = mp->mnt_stat.f_mntfromname;
333d00947d8SCraig Rodrigues 	copystr((below ? "<below>:" : "<above>:"), tmp, len, &done);
334d00947d8SCraig Rodrigues 	len -= done - 1;
335d00947d8SCraig Rodrigues 	tmp += done - 1;
336d00947d8SCraig Rodrigues 	copystr(target, tmp, len, NULL);
337d00947d8SCraig Rodrigues 
338d00947d8SCraig Rodrigues 	UNIONFSDEBUG("unionfs_mount: from %s, on %s\n",
339d00947d8SCraig Rodrigues 	    mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname);
340d00947d8SCraig Rodrigues 
341d00947d8SCraig Rodrigues 	return (0);
342d00947d8SCraig Rodrigues }
343d00947d8SCraig Rodrigues 
344d00947d8SCraig Rodrigues /*
345d00947d8SCraig Rodrigues  * Free reference to unionfs layer
346df8bae1dSRodney W. Grimes  */
34780b301c3SPoul-Henning Kamp static int
348dfd233edSAttilio Rao unionfs_unmount(struct mount *mp, int mntflags)
349df8bae1dSRodney W. Grimes {
350d00947d8SCraig Rodrigues 	struct unionfs_mount *ump;
351df8bae1dSRodney W. Grimes 	int		error;
352d00947d8SCraig Rodrigues 	int		num;
353996c772fSJohn Dyson 	int		freeing;
354d00947d8SCraig Rodrigues 	int		flags;
355df8bae1dSRodney W. Grimes 
356d00947d8SCraig Rodrigues 	UNIONFSDEBUG("unionfs_unmount: mp = %p\n", (void *)mp);
357d00947d8SCraig Rodrigues 
358d00947d8SCraig Rodrigues 	ump = MOUNTTOUNIONFSMOUNT(mp);
359d00947d8SCraig Rodrigues 	flags = 0;
360df8bae1dSRodney W. Grimes 
361996c772fSJohn Dyson 	if (mntflags & MNT_FORCE)
362996c772fSJohn Dyson 		flags |= FORCECLOSE;
363996c772fSJohn Dyson 
364d00947d8SCraig Rodrigues 	/* vflush (no need to call vrele) */
365dfd233edSAttilio Rao 	for (freeing = 0; (error = vflush(mp, 1, flags, curthread)) != 0;) {
366d00947d8SCraig Rodrigues 		num = mp->mnt_nvnodelistsize;
367d00947d8SCraig Rodrigues 		if (num == freeing)
368996c772fSJohn Dyson 			break;
369d00947d8SCraig Rodrigues 		freeing = num;
370df8bae1dSRodney W. Grimes 	}
371df8bae1dSRodney W. Grimes 
3720864ef1eSIan Dowse 	if (error)
3730864ef1eSIan Dowse 		return (error);
374df8bae1dSRodney W. Grimes 
375d00947d8SCraig Rodrigues 	free(ump, M_UNIONFSMNT);
376df8bae1dSRodney W. Grimes 	mp->mnt_data = 0;
377d00947d8SCraig Rodrigues 
378df8bae1dSRodney W. Grimes 	return (0);
379df8bae1dSRodney W. Grimes }
380df8bae1dSRodney W. Grimes 
38180b301c3SPoul-Henning Kamp static int
382dfd233edSAttilio Rao unionfs_root(struct mount *mp, int flags, struct vnode **vpp)
383df8bae1dSRodney W. Grimes {
384d00947d8SCraig Rodrigues 	struct unionfs_mount *ump;
385d00947d8SCraig Rodrigues 	struct vnode   *vp;
386df8bae1dSRodney W. Grimes 
387d00947d8SCraig Rodrigues 	ump = MOUNTTOUNIONFSMOUNT(mp);
388d00947d8SCraig Rodrigues 	vp = ump->um_rootvp;
3892a31267eSMatthew Dillon 
390d00947d8SCraig Rodrigues 	UNIONFSDEBUG("unionfs_root: rootvp=%p locked=%x\n",
39181c794f9SAttilio Rao 	    vp, VOP_ISLOCKED(vp));
392df8bae1dSRodney W. Grimes 
393d00947d8SCraig Rodrigues 	vref(vp);
394d00947d8SCraig Rodrigues 	if (flags & LK_TYPE_MASK)
395cb05b60aSAttilio Rao 		vn_lock(vp, flags);
396df8bae1dSRodney W. Grimes 
397d00947d8SCraig Rodrigues 	*vpp = vp;
398d00947d8SCraig Rodrigues 
399d00947d8SCraig Rodrigues 	return (0);
400df8bae1dSRodney W. Grimes }
401df8bae1dSRodney W. Grimes 
40280b301c3SPoul-Henning Kamp static int
403dfd233edSAttilio Rao unionfs_quotactl(struct mount *mp, int cmd, uid_t uid, void *arg)
404df8bae1dSRodney W. Grimes {
405d00947d8SCraig Rodrigues 	struct unionfs_mount *ump;
406df8bae1dSRodney W. Grimes 
407d00947d8SCraig Rodrigues 	ump = MOUNTTOUNIONFSMOUNT(mp);
408d00947d8SCraig Rodrigues 
409d00947d8SCraig Rodrigues 	/*
410d00947d8SCraig Rodrigues 	 * Writing is always performed to upper vnode.
411d00947d8SCraig Rodrigues 	 */
412dfd233edSAttilio Rao 	return (VFS_QUOTACTL(ump->um_uppervp->v_mount, cmd, uid, arg));
413d00947d8SCraig Rodrigues }
414d00947d8SCraig Rodrigues 
415d00947d8SCraig Rodrigues static int
416dfd233edSAttilio Rao unionfs_statfs(struct mount *mp, struct statfs *sbp)
417d00947d8SCraig Rodrigues {
418d00947d8SCraig Rodrigues 	struct unionfs_mount *ump;
419d00947d8SCraig Rodrigues 	int		error;
420d00947d8SCraig Rodrigues 	struct statfs	mstat;
421d00947d8SCraig Rodrigues 	uint64_t	lbsize;
422d00947d8SCraig Rodrigues 
423d00947d8SCraig Rodrigues 	ump = MOUNTTOUNIONFSMOUNT(mp);
424d00947d8SCraig Rodrigues 
425d00947d8SCraig Rodrigues 	UNIONFSDEBUG("unionfs_statfs(mp = %p, lvp = %p, uvp = %p)\n",
426d00947d8SCraig Rodrigues 	    (void *)mp, (void *)ump->um_lowervp, (void *)ump->um_uppervp);
427df8bae1dSRodney W. Grimes 
428df8bae1dSRodney W. Grimes 	bzero(&mstat, sizeof(mstat));
429df8bae1dSRodney W. Grimes 
430dfd233edSAttilio Rao 	error = VFS_STATFS(ump->um_lowervp->v_mount, &mstat);
431df8bae1dSRodney W. Grimes 	if (error)
432df8bae1dSRodney W. Grimes 		return (error);
433d00947d8SCraig Rodrigues 
434d00947d8SCraig Rodrigues 	/* now copy across the "interesting" information and fake the rest */
435d00947d8SCraig Rodrigues 	sbp->f_blocks = mstat.f_blocks;
436d00947d8SCraig Rodrigues 	sbp->f_files = mstat.f_files;
437d00947d8SCraig Rodrigues 
438d00947d8SCraig Rodrigues 	lbsize = mstat.f_bsize;
439d00947d8SCraig Rodrigues 
440dfd233edSAttilio Rao 	error = VFS_STATFS(ump->um_uppervp->v_mount, &mstat);
441d00947d8SCraig Rodrigues 	if (error)
442d00947d8SCraig Rodrigues 		return (error);
443df8bae1dSRodney W. Grimes 
4441c4ccf09SDon Lewis 	/*
445d00947d8SCraig Rodrigues 	 * The FS type etc is copy from upper vfs.
446d00947d8SCraig Rodrigues 	 * (write able vfs have priority)
4471c4ccf09SDon Lewis 	 */
448df8bae1dSRodney W. Grimes 	sbp->f_type = mstat.f_type;
449df8bae1dSRodney W. Grimes 	sbp->f_flags = mstat.f_flags;
450df8bae1dSRodney W. Grimes 	sbp->f_bsize = mstat.f_bsize;
451df8bae1dSRodney W. Grimes 	sbp->f_iosize = mstat.f_iosize;
452df8bae1dSRodney W. Grimes 
453996c772fSJohn Dyson 	if (mstat.f_bsize != lbsize)
454c9bf0111SKATO Takenori 		sbp->f_blocks = ((off_t)sbp->f_blocks * lbsize) / mstat.f_bsize;
455996c772fSJohn Dyson 
456df8bae1dSRodney W. Grimes 	sbp->f_blocks += mstat.f_blocks;
457996c772fSJohn Dyson 	sbp->f_bfree = mstat.f_bfree;
458996c772fSJohn Dyson 	sbp->f_bavail = mstat.f_bavail;
459df8bae1dSRodney W. Grimes 	sbp->f_files += mstat.f_files;
460996c772fSJohn Dyson 	sbp->f_ffree = mstat.f_ffree;
461df8bae1dSRodney W. Grimes 	return (0);
462df8bae1dSRodney W. Grimes }
463df8bae1dSRodney W. Grimes 
464d00947d8SCraig Rodrigues static int
465dfd233edSAttilio Rao unionfs_sync(struct mount *mp, int waitfor)
466d00947d8SCraig Rodrigues {
467d00947d8SCraig Rodrigues 	/* nothing to do */
468d00947d8SCraig Rodrigues 	return (0);
469d00947d8SCraig Rodrigues }
470d00947d8SCraig Rodrigues 
471d00947d8SCraig Rodrigues static int
472d00947d8SCraig Rodrigues unionfs_vget(struct mount *mp, ino_t ino, int flags, struct vnode **vpp)
473d00947d8SCraig Rodrigues {
474d00947d8SCraig Rodrigues 	return (EOPNOTSUPP);
475d00947d8SCraig Rodrigues }
476d00947d8SCraig Rodrigues 
477d00947d8SCraig Rodrigues static int
478d00947d8SCraig Rodrigues unionfs_fhtovp(struct mount *mp, struct fid *fidp, struct vnode **vpp)
479d00947d8SCraig Rodrigues {
480d00947d8SCraig Rodrigues 	return (EOPNOTSUPP);
481d00947d8SCraig Rodrigues }
482d00947d8SCraig Rodrigues 
483d00947d8SCraig Rodrigues static int
484d00947d8SCraig Rodrigues unionfs_checkexp(struct mount *mp, struct sockaddr *nam, int *extflagsp,
485a9148abdSDoug Rabson     struct ucred **credanonp, int *numsecflavors, int **secflavors)
486d00947d8SCraig Rodrigues {
487d00947d8SCraig Rodrigues 	return (EOPNOTSUPP);
488d00947d8SCraig Rodrigues }
489d00947d8SCraig Rodrigues 
490d00947d8SCraig Rodrigues static int
491d00947d8SCraig Rodrigues unionfs_extattrctl(struct mount *mp, int cmd, struct vnode *filename_vp,
492dfd233edSAttilio Rao     int namespace, const char *attrname)
493d00947d8SCraig Rodrigues {
494d00947d8SCraig Rodrigues 	struct unionfs_mount *ump;
495d00947d8SCraig Rodrigues 	struct unionfs_node *unp;
496d00947d8SCraig Rodrigues 
497d00947d8SCraig Rodrigues 	ump = MOUNTTOUNIONFSMOUNT(mp);
498d00947d8SCraig Rodrigues 	unp = VTOUNIONFS(filename_vp);
499d00947d8SCraig Rodrigues 
500d00947d8SCraig Rodrigues 	if (unp->un_uppervp != NULLVP) {
501d00947d8SCraig Rodrigues 		return (VFS_EXTATTRCTL(ump->um_uppervp->v_mount, cmd,
502dfd233edSAttilio Rao 		    unp->un_uppervp, namespace, attrname));
503d00947d8SCraig Rodrigues 	} else {
504d00947d8SCraig Rodrigues 		return (VFS_EXTATTRCTL(ump->um_lowervp->v_mount, cmd,
505dfd233edSAttilio Rao 		    unp->un_lowervp, namespace, attrname));
506d00947d8SCraig Rodrigues 	}
507d00947d8SCraig Rodrigues }
508d00947d8SCraig Rodrigues 
509d00947d8SCraig Rodrigues static struct vfsops unionfs_vfsops = {
510d00947d8SCraig Rodrigues 	.vfs_checkexp =		unionfs_checkexp,
511d00947d8SCraig Rodrigues 	.vfs_extattrctl =	unionfs_extattrctl,
512d00947d8SCraig Rodrigues 	.vfs_fhtovp =		unionfs_fhtovp,
513d00947d8SCraig Rodrigues 	.vfs_init =		unionfs_init,
514d00947d8SCraig Rodrigues 	.vfs_mount =		unionfs_domount,
515d00947d8SCraig Rodrigues 	.vfs_quotactl =		unionfs_quotactl,
516d00947d8SCraig Rodrigues 	.vfs_root =		unionfs_root,
517d00947d8SCraig Rodrigues 	.vfs_statfs =		unionfs_statfs,
518d00947d8SCraig Rodrigues 	.vfs_sync =		unionfs_sync,
519d00947d8SCraig Rodrigues 	.vfs_uninit =		unionfs_uninit,
520d00947d8SCraig Rodrigues 	.vfs_unmount =		unionfs_unmount,
521d00947d8SCraig Rodrigues 	.vfs_vget =		unionfs_vget,
522df8bae1dSRodney W. Grimes };
523c901836cSGarrett Wollman 
524d00947d8SCraig Rodrigues VFS_SET(unionfs_vfsops, unionfs, VFCF_LOOPBACK);
525