xref: /freebsd/sys/fs/nullfs/null_vfsops.c (revision 51369649b03ece2aed3eb61b0c8214b9aa5b2fa2)
1d167cf6fSWarner Losh /*-
2*51369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*51369649SPedro F. Giffuni  *
4996c772fSJohn Dyson  * Copyright (c) 1992, 1993, 1995
5df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
6df8bae1dSRodney W. Grimes  *
7df8bae1dSRodney W. Grimes  * This code is derived from software donated to Berkeley by
8df8bae1dSRodney W. Grimes  * Jan-Simon Pendry.
9df8bae1dSRodney W. Grimes  *
10df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
11df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
12df8bae1dSRodney W. Grimes  * are met:
13df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
14df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
15df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
16df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
17df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
18fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
19df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
20df8bae1dSRodney W. Grimes  *    without specific prior written permission.
21df8bae1dSRodney W. Grimes  *
22df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
33df8bae1dSRodney W. Grimes  *
34df8bae1dSRodney W. Grimes  *	@(#)null_vfsops.c	8.2 (Berkeley) 1/21/94
35df8bae1dSRodney W. Grimes  *
36df8bae1dSRodney W. Grimes  * @(#)lofs_vfsops.c	1.2 (Berkeley) 6/18/92
37c3aac50fSPeter Wemm  * $FreeBSD$
38df8bae1dSRodney W. Grimes  */
39df8bae1dSRodney W. Grimes 
40df8bae1dSRodney W. Grimes /*
41df8bae1dSRodney W. Grimes  * Null Layer
42df8bae1dSRodney W. Grimes  * (See null_vnops.c for a description of what this does.)
43df8bae1dSRodney W. Grimes  */
44df8bae1dSRodney W. Grimes 
45df8bae1dSRodney W. Grimes #include <sys/param.h>
46df8bae1dSRodney W. Grimes #include <sys/systm.h>
4757b4252eSKonstantin Belousov #include <sys/fcntl.h>
48fdc0430eSMike Pritchard #include <sys/kernel.h>
49fb919e4dSMark Murray #include <sys/lock.h>
50a1c995b6SPoul-Henning Kamp #include <sys/malloc.h>
51df8bae1dSRodney W. Grimes #include <sys/mount.h>
52df8bae1dSRodney W. Grimes #include <sys/namei.h>
53fb919e4dSMark Murray #include <sys/proc.h>
54fb919e4dSMark Murray #include <sys/vnode.h>
55bf3db8aaSMartin Matuska #include <sys/jail.h>
56fb919e4dSMark Murray 
5799d300a1SRuslan Ermilov #include <fs/nullfs/null.h>
58df8bae1dSRodney W. Grimes 
595bb84bc8SRobert Watson static MALLOC_DEFINE(M_NULLFSMNT, "nullfs_mount", "NULLFS mount structure");
60a1c995b6SPoul-Henning Kamp 
617652131bSPoul-Henning Kamp static vfs_fhtovp_t	nullfs_fhtovp;
625e8c582aSPoul-Henning Kamp static vfs_mount_t	nullfs_mount;
637652131bSPoul-Henning Kamp static vfs_quotactl_t	nullfs_quotactl;
647652131bSPoul-Henning Kamp static vfs_root_t	nullfs_root;
657652131bSPoul-Henning Kamp static vfs_sync_t	nullfs_sync;
667652131bSPoul-Henning Kamp static vfs_statfs_t	nullfs_statfs;
677652131bSPoul-Henning Kamp static vfs_unmount_t	nullfs_unmount;
687652131bSPoul-Henning Kamp static vfs_vget_t	nullfs_vget;
697652131bSPoul-Henning Kamp static vfs_extattrctl_t	nullfs_extattrctl;
709b5e8b3aSBruce Evans 
71df8bae1dSRodney W. Grimes /*
72df8bae1dSRodney W. Grimes  * Mount null layer
73df8bae1dSRodney W. Grimes  */
74d4b7a369SPoul-Henning Kamp static int
75dfd233edSAttilio Rao nullfs_mount(struct mount *mp)
76df8bae1dSRodney W. Grimes {
77df8bae1dSRodney W. Grimes 	int error = 0;
78df8bae1dSRodney W. Grimes 	struct vnode *lowerrootvp, *vp;
79df8bae1dSRodney W. Grimes 	struct vnode *nullm_rootvp;
80df8bae1dSRodney W. Grimes 	struct null_mount *xmp;
81bf3db8aaSMartin Matuska 	struct thread *td = curthread;
829fcc512cSMaxime Henrion 	char *target;
839fcc512cSMaxime Henrion 	int isvnunlocked = 0, len;
845e8c582aSPoul-Henning Kamp 	struct nameidata nd, *ndp = &nd;
85df8bae1dSRodney W. Grimes 
868da80660SBoris Popov 	NULLFSDEBUG("nullfs_mount(mp = %p)\n", (void *)mp);
87df8bae1dSRodney W. Grimes 
88bf3db8aaSMartin Matuska 	if (!prison_allow(td->td_ucred, PR_ALLOW_MOUNT_NULLFS))
89bf3db8aaSMartin Matuska 		return (EPERM);
9064042a76SPoul-Henning Kamp 	if (mp->mnt_flag & MNT_ROOTFS)
9164042a76SPoul-Henning Kamp 		return (EOPNOTSUPP);
929cf4c952SKonstantin Belousov 
93df8bae1dSRodney W. Grimes 	/*
94df8bae1dSRodney W. Grimes 	 * Update is a no-op
95df8bae1dSRodney W. Grimes 	 */
96df8bae1dSRodney W. Grimes 	if (mp->mnt_flag & MNT_UPDATE) {
97ebbf93fdSCraig Rodrigues 		/*
98ebbf93fdSCraig Rodrigues 		 * Only support update mounts for NFS export.
99ebbf93fdSCraig Rodrigues 		 */
100ebbf93fdSCraig Rodrigues 		if (vfs_flagopt(mp->mnt_optnew, "export", NULL, 0))
101ebbf93fdSCraig Rodrigues 			return (0);
102ebbf93fdSCraig Rodrigues 		else
103df8bae1dSRodney W. Grimes 			return (EOPNOTSUPP);
104df8bae1dSRodney W. Grimes 	}
105df8bae1dSRodney W. Grimes 
106df8bae1dSRodney W. Grimes 	/*
107df8bae1dSRodney W. Grimes 	 * Get argument
108df8bae1dSRodney W. Grimes 	 */
1099fcc512cSMaxime Henrion 	error = vfs_getopt(mp->mnt_optnew, "target", (void **)&target, &len);
1109fcc512cSMaxime Henrion 	if (error || target[len - 1] != '\0')
1119fcc512cSMaxime Henrion 		return (EINVAL);
112df8bae1dSRodney W. Grimes 
113df8bae1dSRodney W. Grimes 	/*
1149ce73797SPeter Holm 	 * Unlock lower node to avoid possible deadlock.
115f85e8fc5SKATO Takenori 	 */
116aec0fb7bSPoul-Henning Kamp 	if ((mp->mnt_vnodecovered->v_op == &null_vnodeops) &&
1179ce73797SPeter Holm 	    VOP_ISLOCKED(mp->mnt_vnodecovered) == LK_EXCLUSIVE) {
11822db15c0SAttilio Rao 		VOP_UNLOCK(mp->mnt_vnodecovered, 0);
119f85e8fc5SKATO Takenori 		isvnunlocked = 1;
120f85e8fc5SKATO Takenori 	}
121f85e8fc5SKATO Takenori 	/*
122df8bae1dSRodney W. Grimes 	 * Find lower node
123df8bae1dSRodney W. Grimes 	 */
124dfd233edSAttilio Rao 	NDINIT(ndp, LOOKUP, FOLLOW|LOCKLEAF, UIO_SYSSPACE, target, curthread);
1253a773ad0SPoul-Henning Kamp 	error = namei(ndp);
126d9e9650aSKonstantin Belousov 
127f85e8fc5SKATO Takenori 	/*
128f85e8fc5SKATO Takenori 	 * Re-lock vnode.
129d9e9650aSKonstantin Belousov 	 * XXXKIB This is deadlock-prone as well.
130f85e8fc5SKATO Takenori 	 */
131ffa43617SKonstantin Belousov 	if (isvnunlocked)
132cb05b60aSAttilio Rao 		vn_lock(mp->mnt_vnodecovered, LK_EXCLUSIVE | LK_RETRY);
133f85e8fc5SKATO Takenori 
1343a773ad0SPoul-Henning Kamp 	if (error)
135df8bae1dSRodney W. Grimes 		return (error);
136762e6b85SEivind Eklund 	NDFREE(ndp, NDF_ONLY_PNBUF);
137df8bae1dSRodney W. Grimes 
138df8bae1dSRodney W. Grimes 	/*
139df8bae1dSRodney W. Grimes 	 * Sanity check on lower vnode
140df8bae1dSRodney W. Grimes 	 */
141df8bae1dSRodney W. Grimes 	lowerrootvp = ndp->ni_vp;
142df8bae1dSRodney W. Grimes 
143f85e8fc5SKATO Takenori 	/*
144f85e8fc5SKATO Takenori 	 * Check multi null mount to avoid `lock against myself' panic.
145f85e8fc5SKATO Takenori 	 */
146f85e8fc5SKATO Takenori 	if (lowerrootvp == VTONULL(mp->mnt_vnodecovered)->null_lowervp) {
1478da80660SBoris Popov 		NULLFSDEBUG("nullfs_mount: multi null mount?\n");
1486716c905SBoris Popov 		vput(lowerrootvp);
149747e9157SKATO Takenori 		return (EDEADLK);
150f85e8fc5SKATO Takenori 	}
151f85e8fc5SKATO Takenori 
152df8bae1dSRodney W. Grimes 	xmp = (struct null_mount *) malloc(sizeof(struct null_mount),
1539cf4c952SKonstantin Belousov 	    M_NULLFSMNT, M_WAITOK | M_ZERO);
154df8bae1dSRodney W. Grimes 
155df8bae1dSRodney W. Grimes 	/*
156df8bae1dSRodney W. Grimes 	 * Save reference to underlying FS
157df8bae1dSRodney W. Grimes 	 */
158df8bae1dSRodney W. Grimes 	xmp->nullm_vfs = lowerrootvp->v_mount;
159df8bae1dSRodney W. Grimes 
160df8bae1dSRodney W. Grimes 	/*
161df8bae1dSRodney W. Grimes 	 * Save reference.  Each mount also holds
162df8bae1dSRodney W. Grimes 	 * a reference on the root vnode.
163df8bae1dSRodney W. Grimes 	 */
1641cfdefbbSSemen Ustimenko 	error = null_nodeget(mp, lowerrootvp, &vp);
165df8bae1dSRodney W. Grimes 	/*
166df8bae1dSRodney W. Grimes 	 * Make sure the node alias worked
167df8bae1dSRodney W. Grimes 	 */
168df8bae1dSRodney W. Grimes 	if (error) {
169dd0f9532SKonstantin Belousov 		free(xmp, M_NULLFSMNT);
170df8bae1dSRodney W. Grimes 		return (error);
171df8bae1dSRodney W. Grimes 	}
172df8bae1dSRodney W. Grimes 
173df8bae1dSRodney W. Grimes 	/*
174df8bae1dSRodney W. Grimes 	 * Keep a held reference to the root vnode.
175df8bae1dSRodney W. Grimes 	 * It is vrele'd in nullfs_unmount.
176df8bae1dSRodney W. Grimes 	 */
177df8bae1dSRodney W. Grimes 	nullm_rootvp = vp;
178e6e370a7SJeff Roberson 	nullm_rootvp->v_vflag |= VV_ROOT;
179df8bae1dSRodney W. Grimes 	xmp->nullm_rootvp = nullm_rootvp;
18075cabb63SJeff Roberson 
18175cabb63SJeff Roberson 	/*
18275cabb63SJeff Roberson 	 * Unlock the node (either the lower or the alias)
18375cabb63SJeff Roberson 	 */
18422db15c0SAttilio Rao 	VOP_UNLOCK(vp, 0);
18575cabb63SJeff Roberson 
1865da56ddbSTor Egge 	if (NULLVPTOLOWERVP(nullm_rootvp)->v_mount->mnt_flag & MNT_LOCAL) {
1875da56ddbSTor Egge 		MNT_ILOCK(mp);
188df8bae1dSRodney W. Grimes 		mp->mnt_flag |= MNT_LOCAL;
1895da56ddbSTor Egge 		MNT_IUNLOCK(mp);
1905da56ddbSTor Egge 	}
1919cf4c952SKonstantin Belousov 
1929cf4c952SKonstantin Belousov 	xmp->nullm_flags |= NULLM_CACHE;
193abc15156SKonstantin Belousov 	if (vfs_getopt(mp->mnt_optnew, "nocache", NULL, NULL) == 0 ||
194abc15156SKonstantin Belousov 	    (xmp->nullm_vfs->mnt_kern_flag & MNTK_NULL_NOCACHE) != 0)
1959cf4c952SKonstantin Belousov 		xmp->nullm_flags &= ~NULLM_CACHE;
1969cf4c952SKonstantin Belousov 
1975da56ddbSTor Egge 	MNT_ILOCK(mp);
1989cf4c952SKonstantin Belousov 	if ((xmp->nullm_flags & NULLM_CACHE) != 0) {
19937a1046eSKonstantin Belousov 		mp->mnt_kern_flag |= lowerrootvp->v_mount->mnt_kern_flag &
2009cf4c952SKonstantin Belousov 		    (MNTK_SHARED_WRITES | MNTK_LOOKUP_SHARED |
2019cf4c952SKonstantin Belousov 		    MNTK_EXTENDED_SHARED);
2029cf4c952SKonstantin Belousov 	}
203d9e9650aSKonstantin Belousov 	mp->mnt_kern_flag |= MNTK_LOOKUP_EXCL_DOTDOT;
2044fce16e4SMateusz Guzik 	mp->mnt_kern_flag |= lowerrootvp->v_mount->mnt_kern_flag &
205f36aa2b7SKonstantin Belousov 	    (MNTK_USES_BCACHE | MNTK_NO_IOPF | MNTK_UNMAPPED_BUFS);
2065da56ddbSTor Egge 	MNT_IUNLOCK(mp);
20777465d93SAlfred Perlstein 	mp->mnt_data = xmp;
208996c772fSJohn Dyson 	vfs_getnewfsid(mp);
2099cf4c952SKonstantin Belousov 	if ((xmp->nullm_flags & NULLM_CACHE) != 0) {
210d9e9650aSKonstantin Belousov 		MNT_ILOCK(xmp->nullm_vfs);
2119cf4c952SKonstantin Belousov 		TAILQ_INSERT_TAIL(&xmp->nullm_vfs->mnt_uppers, mp,
2129cf4c952SKonstantin Belousov 		    mnt_upper_link);
213d9e9650aSKonstantin Belousov 		MNT_IUNLOCK(xmp->nullm_vfs);
2149cf4c952SKonstantin Belousov 	}
215df8bae1dSRodney W. Grimes 
2167ab8c8c0SPoul-Henning Kamp 	vfs_mountedfrom(mp, target);
2177ab8c8c0SPoul-Henning Kamp 
2188da80660SBoris Popov 	NULLFSDEBUG("nullfs_mount: lower %s, alias at %s\n",
219df8bae1dSRodney W. Grimes 		mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname);
220df8bae1dSRodney W. Grimes 	return (0);
221df8bae1dSRodney W. Grimes }
222df8bae1dSRodney W. Grimes 
223df8bae1dSRodney W. Grimes /*
224df8bae1dSRodney W. Grimes  * Free reference to null layer
225df8bae1dSRodney W. Grimes  */
226d4b7a369SPoul-Henning Kamp static int
227dfd233edSAttilio Rao nullfs_unmount(mp, mntflags)
228df8bae1dSRodney W. Grimes 	struct mount *mp;
229df8bae1dSRodney W. Grimes 	int mntflags;
230df8bae1dSRodney W. Grimes {
231d9e9650aSKonstantin Belousov 	struct null_mount *mntdata;
232d9e9650aSKonstantin Belousov 	struct mount *ump;
233d9e9650aSKonstantin Belousov 	int error, flags;
234df8bae1dSRodney W. Grimes 
2358da80660SBoris Popov 	NULLFSDEBUG("nullfs_unmount: mp = %p\n", (void *)mp);
236df8bae1dSRodney W. Grimes 
237996c772fSJohn Dyson 	if (mntflags & MNT_FORCE)
238d9e9650aSKonstantin Belousov 		flags = FORCECLOSE;
239d9e9650aSKonstantin Belousov 	else
240d9e9650aSKonstantin Belousov 		flags = 0;
241df8bae1dSRodney W. Grimes 
2420864ef1eSIan Dowse 	/* There is 1 extra root vnode reference (nullm_rootvp). */
243dfd233edSAttilio Rao 	error = vflush(mp, 1, flags, curthread);
2443a773ad0SPoul-Henning Kamp 	if (error)
245df8bae1dSRodney W. Grimes 		return (error);
246df8bae1dSRodney W. Grimes 
247df8bae1dSRodney W. Grimes 	/*
248df8bae1dSRodney W. Grimes 	 * Finally, throw away the null_mount structure
249df8bae1dSRodney W. Grimes 	 */
2508da80660SBoris Popov 	mntdata = mp->mnt_data;
251d9e9650aSKonstantin Belousov 	ump = mntdata->nullm_vfs;
2529cf4c952SKonstantin Belousov 	if ((mntdata->nullm_flags & NULLM_CACHE) != 0) {
253d9e9650aSKonstantin Belousov 		MNT_ILOCK(ump);
254d9e9650aSKonstantin Belousov 		while ((ump->mnt_kern_flag & MNTK_VGONE_UPPER) != 0) {
255d9e9650aSKonstantin Belousov 			ump->mnt_kern_flag |= MNTK_VGONE_WAITER;
256d9e9650aSKonstantin Belousov 			msleep(&ump->mnt_uppers, &ump->mnt_mtx, 0, "vgnupw", 0);
257d9e9650aSKonstantin Belousov 		}
258d9e9650aSKonstantin Belousov 		TAILQ_REMOVE(&ump->mnt_uppers, mp, mnt_upper_link);
259d9e9650aSKonstantin Belousov 		MNT_IUNLOCK(ump);
2609cf4c952SKonstantin Belousov 	}
26111753bd0SKevin Lo 	mp->mnt_data = NULL;
2624451405fSBoris Popov 	free(mntdata, M_NULLFSMNT);
263d9e9650aSKonstantin Belousov 	return (0);
264df8bae1dSRodney W. Grimes }
265df8bae1dSRodney W. Grimes 
266d4b7a369SPoul-Henning Kamp static int
267dfd233edSAttilio Rao nullfs_root(mp, flags, vpp)
268df8bae1dSRodney W. Grimes 	struct mount *mp;
269d9b2d9f7SJeff Roberson 	int flags;
270df8bae1dSRodney W. Grimes 	struct vnode **vpp;
271df8bae1dSRodney W. Grimes {
272df8bae1dSRodney W. Grimes 	struct vnode *vp;
273df8bae1dSRodney W. Grimes 
2748da80660SBoris Popov 	NULLFSDEBUG("nullfs_root(mp = %p, vp = %p->%p)\n", (void *)mp,
27589785a16SBruce Evans 	    (void *)MOUNTTONULLMOUNT(mp)->nullm_rootvp,
27689785a16SBruce Evans 	    (void *)NULLVPTOLOWERVP(MOUNTTONULLMOUNT(mp)->nullm_rootvp));
277df8bae1dSRodney W. Grimes 
278df8bae1dSRodney W. Grimes 	/*
279df8bae1dSRodney W. Grimes 	 * Return locked reference to root.
280df8bae1dSRodney W. Grimes 	 */
281df8bae1dSRodney W. Grimes 	vp = MOUNTTONULLMOUNT(mp)->nullm_rootvp;
282df8bae1dSRodney W. Grimes 	VREF(vp);
2834451405fSBoris Popov 
28417edcd76SKonstantin Belousov 	ASSERT_VOP_UNLOCKED(vp, "root vnode is locked");
285cb05b60aSAttilio Rao 	vn_lock(vp, flags | LK_RETRY);
286df8bae1dSRodney W. Grimes 	*vpp = vp;
287df8bae1dSRodney W. Grimes 	return 0;
288df8bae1dSRodney W. Grimes }
289df8bae1dSRodney W. Grimes 
290d4b7a369SPoul-Henning Kamp static int
291dfd233edSAttilio Rao nullfs_quotactl(mp, cmd, uid, arg)
292df8bae1dSRodney W. Grimes 	struct mount *mp;
293df8bae1dSRodney W. Grimes 	int cmd;
294df8bae1dSRodney W. Grimes 	uid_t uid;
2950430a5e2SDag-Erling Smørgrav 	void *arg;
296df8bae1dSRodney W. Grimes {
297dfd233edSAttilio Rao 	return VFS_QUOTACTL(MOUNTTONULLMOUNT(mp)->nullm_vfs, cmd, uid, arg);
298df8bae1dSRodney W. Grimes }
299df8bae1dSRodney W. Grimes 
300d4b7a369SPoul-Henning Kamp static int
301dfd233edSAttilio Rao nullfs_statfs(mp, sbp)
302df8bae1dSRodney W. Grimes 	struct mount *mp;
303df8bae1dSRodney W. Grimes 	struct statfs *sbp;
304df8bae1dSRodney W. Grimes {
305df8bae1dSRodney W. Grimes 	int error;
3062f304845SKonstantin Belousov 	struct statfs *mstat;
307df8bae1dSRodney W. Grimes 
3088da80660SBoris Popov 	NULLFSDEBUG("nullfs_statfs(mp = %p, vp = %p->%p)\n", (void *)mp,
30989785a16SBruce Evans 	    (void *)MOUNTTONULLMOUNT(mp)->nullm_rootvp,
31089785a16SBruce Evans 	    (void *)NULLVPTOLOWERVP(MOUNTTONULLMOUNT(mp)->nullm_rootvp));
311df8bae1dSRodney W. Grimes 
3122f304845SKonstantin Belousov 	mstat = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK | M_ZERO);
313df8bae1dSRodney W. Grimes 
3142f304845SKonstantin Belousov 	error = VFS_STATFS(MOUNTTONULLMOUNT(mp)->nullm_vfs, mstat);
3152f304845SKonstantin Belousov 	if (error) {
3162f304845SKonstantin Belousov 		free(mstat, M_STATFS);
317df8bae1dSRodney W. Grimes 		return (error);
3182f304845SKonstantin Belousov 	}
319df8bae1dSRodney W. Grimes 
320df8bae1dSRodney W. Grimes 	/* now copy across the "interesting" information and fake the rest */
3212f304845SKonstantin Belousov 	sbp->f_type = mstat->f_type;
3226d6a91c5SJilles Tjoelker 	sbp->f_flags = (sbp->f_flags & (MNT_RDONLY | MNT_NOEXEC | MNT_NOSUID |
3232f304845SKonstantin Belousov 	    MNT_UNION | MNT_NOSYMFOLLOW)) | (mstat->f_flags & ~MNT_ROOTFS);
3242f304845SKonstantin Belousov 	sbp->f_bsize = mstat->f_bsize;
3252f304845SKonstantin Belousov 	sbp->f_iosize = mstat->f_iosize;
3262f304845SKonstantin Belousov 	sbp->f_blocks = mstat->f_blocks;
3272f304845SKonstantin Belousov 	sbp->f_bfree = mstat->f_bfree;
3282f304845SKonstantin Belousov 	sbp->f_bavail = mstat->f_bavail;
3292f304845SKonstantin Belousov 	sbp->f_files = mstat->f_files;
3302f304845SKonstantin Belousov 	sbp->f_ffree = mstat->f_ffree;
3312f304845SKonstantin Belousov 
3322f304845SKonstantin Belousov 	free(mstat, M_STATFS);
333df8bae1dSRodney W. Grimes 	return (0);
334df8bae1dSRodney W. Grimes }
335df8bae1dSRodney W. Grimes 
336d4b7a369SPoul-Henning Kamp static int
337dfd233edSAttilio Rao nullfs_sync(mp, waitfor)
338df8bae1dSRodney W. Grimes 	struct mount *mp;
339df8bae1dSRodney W. Grimes 	int waitfor;
340df8bae1dSRodney W. Grimes {
341df8bae1dSRodney W. Grimes 	/*
342df8bae1dSRodney W. Grimes 	 * XXX - Assumes no data cached at null layer.
343df8bae1dSRodney W. Grimes 	 */
344df8bae1dSRodney W. Grimes 	return (0);
345df8bae1dSRodney W. Grimes }
346df8bae1dSRodney W. Grimes 
347d4b7a369SPoul-Henning Kamp static int
348a0595d02SKirk McKusick nullfs_vget(mp, ino, flags, vpp)
349df8bae1dSRodney W. Grimes 	struct mount *mp;
350df8bae1dSRodney W. Grimes 	ino_t ino;
351a0595d02SKirk McKusick 	int flags;
352df8bae1dSRodney W. Grimes 	struct vnode **vpp;
353df8bae1dSRodney W. Grimes {
3544451405fSBoris Popov 	int error;
355e4e1d9f3SKonstantin Belousov 
356e4e1d9f3SKonstantin Belousov 	KASSERT((flags & LK_TYPE_MASK) != 0,
357e4e1d9f3SKonstantin Belousov 	    ("nullfs_vget: no lock requested"));
358e4e1d9f3SKonstantin Belousov 
359a0595d02SKirk McKusick 	error = VFS_VGET(MOUNTTONULLMOUNT(mp)->nullm_vfs, ino, flags, vpp);
360d9e9650aSKonstantin Belousov 	if (error != 0)
3614451405fSBoris Popov 		return (error);
3621cfdefbbSSemen Ustimenko 	return (null_nodeget(mp, *vpp, vpp));
363df8bae1dSRodney W. Grimes }
364df8bae1dSRodney W. Grimes 
365d4b7a369SPoul-Henning Kamp static int
366694a586aSRick Macklem nullfs_fhtovp(mp, fidp, flags, vpp)
367df8bae1dSRodney W. Grimes 	struct mount *mp;
368df8bae1dSRodney W. Grimes 	struct fid *fidp;
369694a586aSRick Macklem 	int flags;
370df8bae1dSRodney W. Grimes 	struct vnode **vpp;
371c24fda81SAlfred Perlstein {
3724451405fSBoris Popov 	int error;
373c24fda81SAlfred Perlstein 
374d9e9650aSKonstantin Belousov 	error = VFS_FHTOVP(MOUNTTONULLMOUNT(mp)->nullm_vfs, fidp, flags,
375d9e9650aSKonstantin Belousov 	    vpp);
376d9e9650aSKonstantin Belousov 	if (error != 0)
377d9e9650aSKonstantin Belousov 		return (error);
3781cfdefbbSSemen Ustimenko 	return (null_nodeget(mp, *vpp, vpp));
379c24fda81SAlfred Perlstein }
380c24fda81SAlfred Perlstein 
381c24fda81SAlfred Perlstein static int
382dfd233edSAttilio Rao nullfs_extattrctl(mp, cmd, filename_vp, namespace, attrname)
38391f37dcbSRobert Watson 	struct mount *mp;
38491f37dcbSRobert Watson 	int cmd;
38570f36851SRobert Watson 	struct vnode *filename_vp;
38670f36851SRobert Watson 	int namespace;
3878f073875SRobert Watson 	const char *attrname;
38891f37dcbSRobert Watson {
389d9e9650aSKonstantin Belousov 
390d9e9650aSKonstantin Belousov 	return (VFS_EXTATTRCTL(MOUNTTONULLMOUNT(mp)->nullm_vfs, cmd,
391d9e9650aSKonstantin Belousov 	    filename_vp, namespace, attrname));
39291f37dcbSRobert Watson }
39391f37dcbSRobert Watson 
394d9e9650aSKonstantin Belousov static void
395d9e9650aSKonstantin Belousov nullfs_reclaim_lowervp(struct mount *mp, struct vnode *lowervp)
396d9e9650aSKonstantin Belousov {
397d9e9650aSKonstantin Belousov 	struct vnode *vp;
398d9e9650aSKonstantin Belousov 
399d9e9650aSKonstantin Belousov 	vp = null_hashget(mp, lowervp);
400d9e9650aSKonstantin Belousov 	if (vp == NULL)
401d9e9650aSKonstantin Belousov 		return;
4020fc6daa7SKonstantin Belousov 	VTONULL(vp)->null_flags |= NULLV_NOUNLOCK;
403d9e9650aSKonstantin Belousov 	vgone(vp);
4040fc6daa7SKonstantin Belousov 	vput(vp);
4050fc6daa7SKonstantin Belousov }
4060fc6daa7SKonstantin Belousov 
4070fc6daa7SKonstantin Belousov static void
4080fc6daa7SKonstantin Belousov nullfs_unlink_lowervp(struct mount *mp, struct vnode *lowervp)
4090fc6daa7SKonstantin Belousov {
4100fc6daa7SKonstantin Belousov 	struct vnode *vp;
4110fc6daa7SKonstantin Belousov 	struct null_node *xp;
4120fc6daa7SKonstantin Belousov 
4130fc6daa7SKonstantin Belousov 	vp = null_hashget(mp, lowervp);
4140fc6daa7SKonstantin Belousov 	if (vp == NULL)
4150fc6daa7SKonstantin Belousov 		return;
4160fc6daa7SKonstantin Belousov 	xp = VTONULL(vp);
4170fc6daa7SKonstantin Belousov 	xp->null_flags |= NULLV_DROP | NULLV_NOUNLOCK;
4180fc6daa7SKonstantin Belousov 	vhold(vp);
4190fc6daa7SKonstantin Belousov 	vunref(vp);
4200fc6daa7SKonstantin Belousov 
4210fc6daa7SKonstantin Belousov 	if (vp->v_usecount == 0) {
42274c7ff1aSKonstantin Belousov 		/*
42374c7ff1aSKonstantin Belousov 		 * If vunref() dropped the last use reference on the
42474c7ff1aSKonstantin Belousov 		 * nullfs vnode, it must be reclaimed, and its lock
42574c7ff1aSKonstantin Belousov 		 * was split from the lower vnode lock.  Need to do
42674c7ff1aSKonstantin Belousov 		 * extra unlock before allowing the final vdrop() to
42774c7ff1aSKonstantin Belousov 		 * free the vnode.
42874c7ff1aSKonstantin Belousov 		 */
4290fc6daa7SKonstantin Belousov 		KASSERT((vp->v_iflag & VI_DOOMED) != 0,
43074c7ff1aSKonstantin Belousov 		    ("not reclaimed nullfs vnode %p", vp));
4310fc6daa7SKonstantin Belousov 		VOP_UNLOCK(vp, 0);
43274c7ff1aSKonstantin Belousov 	} else {
43374c7ff1aSKonstantin Belousov 		/*
43474c7ff1aSKonstantin Belousov 		 * Otherwise, the nullfs vnode still shares the lock
43574c7ff1aSKonstantin Belousov 		 * with the lower vnode, and must not be unlocked.
43674c7ff1aSKonstantin Belousov 		 * Also clear the NULLV_NOUNLOCK, the flag is not
43774c7ff1aSKonstantin Belousov 		 * relevant for future reclamations.
43874c7ff1aSKonstantin Belousov 		 */
43974c7ff1aSKonstantin Belousov 		ASSERT_VOP_ELOCKED(vp, "unlink_lowervp");
44074c7ff1aSKonstantin Belousov 		KASSERT((vp->v_iflag & VI_DOOMED) == 0,
44174c7ff1aSKonstantin Belousov 		    ("reclaimed nullfs vnode %p", vp));
44274c7ff1aSKonstantin Belousov 		xp->null_flags &= ~NULLV_NOUNLOCK;
4430fc6daa7SKonstantin Belousov 	}
4440fc6daa7SKonstantin Belousov 	vdrop(vp);
445d9e9650aSKonstantin Belousov }
44691f37dcbSRobert Watson 
447d4b7a369SPoul-Henning Kamp static struct vfsops null_vfsops = {
4487652131bSPoul-Henning Kamp 	.vfs_extattrctl =	nullfs_extattrctl,
4497652131bSPoul-Henning Kamp 	.vfs_fhtovp =		nullfs_fhtovp,
4507652131bSPoul-Henning Kamp 	.vfs_init =		nullfs_init,
4515e8c582aSPoul-Henning Kamp 	.vfs_mount =		nullfs_mount,
4527652131bSPoul-Henning Kamp 	.vfs_quotactl =		nullfs_quotactl,
4537652131bSPoul-Henning Kamp 	.vfs_root =		nullfs_root,
4547652131bSPoul-Henning Kamp 	.vfs_statfs =		nullfs_statfs,
4557652131bSPoul-Henning Kamp 	.vfs_sync =		nullfs_sync,
4567652131bSPoul-Henning Kamp 	.vfs_uninit =		nullfs_uninit,
4577652131bSPoul-Henning Kamp 	.vfs_unmount =		nullfs_unmount,
4587652131bSPoul-Henning Kamp 	.vfs_vget =		nullfs_vget,
459d9e9650aSKonstantin Belousov 	.vfs_reclaim_lowervp =	nullfs_reclaim_lowervp,
4600fc6daa7SKonstantin Belousov 	.vfs_unlink_lowervp =	nullfs_unlink_lowervp,
461df8bae1dSRodney W. Grimes };
462c901836cSGarrett Wollman 
46361f0e25aSMartin Matuska VFS_SET(null_vfsops, nullfs, VFCF_LOOPBACK | VFCF_JAIL);
464