xref: /freebsd/sys/fs/nullfs/null_subr.c (revision 829f0bcb5fe24bb523c5a9e7bd3bb79412e06906)
1d167cf6fSWarner Losh /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
4df8bae1dSRodney W. Grimes  * Copyright (c) 1992, 1993
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  *
34996c772fSJohn Dyson  *	@(#)null_subr.c	8.7 (Berkeley) 5/14/95
35df8bae1dSRodney W. Grimes  *
36c3aac50fSPeter Wemm  * $FreeBSD$
37df8bae1dSRodney W. Grimes  */
38df8bae1dSRodney W. Grimes 
39df8bae1dSRodney W. Grimes #include <sys/param.h>
40df8bae1dSRodney W. Grimes #include <sys/systm.h>
418da80660SBoris Popov #include <sys/kernel.h>
42fb919e4dSMark Murray #include <sys/lock.h>
43cd29b292SMateusz Guzik #include <sys/rwlock.h>
44df8bae1dSRodney W. Grimes #include <sys/malloc.h>
45fb919e4dSMark Murray #include <sys/mount.h>
46fb919e4dSMark Murray #include <sys/proc.h>
47fb919e4dSMark Murray #include <sys/vnode.h>
48fb919e4dSMark Murray 
4999d300a1SRuslan Ermilov #include <fs/nullfs/null.h>
50df8bae1dSRodney W. Grimes 
51df8bae1dSRodney W. Grimes /*
52df8bae1dSRodney W. Grimes  * Null layer cache:
53df8bae1dSRodney W. Grimes  * Each cache entry holds a reference to the lower vnode
54df8bae1dSRodney W. Grimes  * along with a pointer to the alias vnode.  When an
55df8bae1dSRodney W. Grimes  * entry is added the lower vnode is VREF'd.  When the
56df8bae1dSRodney W. Grimes  * alias is removed the lower vnode is vrele'd.
57df8bae1dSRodney W. Grimes  */
58df8bae1dSRodney W. Grimes 
59603f963eSKonstantin Belousov #define	NULL_NHASH(vp) (&null_node_hashtbl[vfs_hash_index(vp) & null_hash_mask])
608da80660SBoris Popov 
61e3975643SJake Burkholder static LIST_HEAD(null_node_hashhead, null_node) *null_node_hashtbl;
62cd29b292SMateusz Guzik static struct rwlock null_hash_lock;
63603f963eSKonstantin Belousov static u_long null_hash_mask;
648da80660SBoris Popov 
655bb84bc8SRobert Watson static MALLOC_DEFINE(M_NULLFSHASH, "nullfs_hash", "NULLFS hash table");
665bb84bc8SRobert Watson MALLOC_DEFINE(M_NULLFSNODE, "nullfs_node", "NULLFS vnode private part");
67df8bae1dSRodney W. Grimes 
68aeabf8d4SMateusz Guzik static void null_hashins(struct mount *, struct null_node *);
699b5e8b3aSBruce Evans 
70df8bae1dSRodney W. Grimes /*
71df8bae1dSRodney W. Grimes  * Initialise cache headers
72df8bae1dSRodney W. Grimes  */
7326f9a767SRodney W. Grimes int
74d35991d3SMateusz Guzik nullfs_init(struct vfsconf *vfsp)
75df8bae1dSRodney W. Grimes {
76996c772fSJohn Dyson 
77603f963eSKonstantin Belousov 	null_node_hashtbl = hashinit(desiredvnodes, M_NULLFSHASH,
78603f963eSKonstantin Belousov 	    &null_hash_mask);
79cd29b292SMateusz Guzik 	rw_init(&null_hash_lock, "nullhs");
808da80660SBoris Popov 	return (0);
818da80660SBoris Popov }
828da80660SBoris Popov 
838da80660SBoris Popov int
84d35991d3SMateusz Guzik nullfs_uninit(struct vfsconf *vfsp)
858da80660SBoris Popov {
868da80660SBoris Popov 
87cd29b292SMateusz Guzik 	rw_destroy(&null_hash_lock);
88603f963eSKonstantin Belousov 	hashdestroy(null_node_hashtbl, M_NULLFSHASH, null_hash_mask);
8926f9a767SRodney W. Grimes 	return (0);
90df8bae1dSRodney W. Grimes }
91df8bae1dSRodney W. Grimes 
92df8bae1dSRodney W. Grimes /*
93df8bae1dSRodney W. Grimes  * Return a VREF'ed alias for lower vnode if already exists, else 0.
944451405fSBoris Popov  * Lower vnode should be locked on entry and will be left locked on exit.
95df8bae1dSRodney W. Grimes  */
96aeabf8d4SMateusz Guzik static struct vnode *
97aeabf8d4SMateusz Guzik null_hashget_locked(struct mount *mp, struct vnode *lowervp)
98df8bae1dSRodney W. Grimes {
99996c772fSJohn Dyson 	struct null_node_hashhead *hd;
100df8bae1dSRodney W. Grimes 	struct null_node *a;
101df8bae1dSRodney W. Grimes 	struct vnode *vp;
1029c12e631SJeff Roberson 
1039c12e631SJeff Roberson 	ASSERT_VOP_LOCKED(lowervp, "null_hashget");
104aeabf8d4SMateusz Guzik 	rw_assert(&null_hash_lock, RA_LOCKED);
105df8bae1dSRodney W. Grimes 
106df8bae1dSRodney W. Grimes 	/*
107df8bae1dSRodney W. Grimes 	 * Find hash base, and then search the (two-way) linked
108df8bae1dSRodney W. Grimes 	 * list looking for a null_node structure which is referencing
109df8bae1dSRodney W. Grimes 	 * the lower vnode.  If found, the increment the null_node
110df8bae1dSRodney W. Grimes 	 * reference count (but NOT the lower vnode's VREF counter).
111df8bae1dSRodney W. Grimes 	 */
112996c772fSJohn Dyson 	hd = NULL_NHASH(lowervp);
113fc2ffbe6SPoul-Henning Kamp 	LIST_FOREACH(a, hd, null_hash) {
11454939875STim J. Robbins 		if (a->null_lowervp == lowervp && NULLTOV(a)->v_mount == mp) {
1159c12e631SJeff Roberson 			/*
1169c12e631SJeff Roberson 			 * Since we have the lower node locked the nullfs
1179c12e631SJeff Roberson 			 * node can not be in the process of recycling.  If
1189c12e631SJeff Roberson 			 * it had been recycled before we grabed the lower
1199c12e631SJeff Roberson 			 * lock it would not have been found on the hash.
1209c12e631SJeff Roberson 			 */
1214c65d593SJeff Roberson 			vp = NULLTOV(a);
1224c65d593SJeff Roberson 			vref(vp);
123df8bae1dSRodney W. Grimes 			return (vp);
124df8bae1dSRodney W. Grimes 		}
125df8bae1dSRodney W. Grimes 	}
1261cfdefbbSSemen Ustimenko 	return (NULLVP);
127df8bae1dSRodney W. Grimes }
128df8bae1dSRodney W. Grimes 
129aeabf8d4SMateusz Guzik struct vnode *
130aeabf8d4SMateusz Guzik null_hashget(struct mount *mp, struct vnode *lowervp)
131aeabf8d4SMateusz Guzik {
132aeabf8d4SMateusz Guzik 	struct null_node_hashhead *hd;
133aeabf8d4SMateusz Guzik 	struct vnode *vp;
134aeabf8d4SMateusz Guzik 
135aeabf8d4SMateusz Guzik 	hd = NULL_NHASH(lowervp);
136aeabf8d4SMateusz Guzik 	if (LIST_EMPTY(hd))
137aeabf8d4SMateusz Guzik 		return (NULLVP);
138aeabf8d4SMateusz Guzik 
139aeabf8d4SMateusz Guzik 	rw_rlock(&null_hash_lock);
140aeabf8d4SMateusz Guzik 	vp = null_hashget_locked(mp, lowervp);
141aeabf8d4SMateusz Guzik 	rw_runlock(&null_hash_lock);
142aeabf8d4SMateusz Guzik 
143aeabf8d4SMateusz Guzik 	return (vp);
144aeabf8d4SMateusz Guzik }
145aeabf8d4SMateusz Guzik 
146aeabf8d4SMateusz Guzik static void
147d35991d3SMateusz Guzik null_hashins(struct mount *mp, struct null_node *xp)
1481cfdefbbSSemen Ustimenko {
1491cfdefbbSSemen Ustimenko 	struct null_node_hashhead *hd;
150aeabf8d4SMateusz Guzik #ifdef INVARIANTS
1511cfdefbbSSemen Ustimenko 	struct null_node *oxp;
152aeabf8d4SMateusz Guzik #endif
153aeabf8d4SMateusz Guzik 
154aeabf8d4SMateusz Guzik 	rw_assert(&null_hash_lock, RA_WLOCKED);
1551cfdefbbSSemen Ustimenko 
1561cfdefbbSSemen Ustimenko 	hd = NULL_NHASH(xp->null_lowervp);
157aeabf8d4SMateusz Guzik #ifdef INVARIANTS
1581cfdefbbSSemen Ustimenko 	LIST_FOREACH(oxp, hd, null_hash) {
15954939875STim J. Robbins 		if (oxp->null_lowervp == xp->null_lowervp &&
16054939875STim J. Robbins 		    NULLTOV(oxp)->v_mount == mp) {
161aeabf8d4SMateusz Guzik 			VNASSERT(0, NULLTOV(oxp),
162aeabf8d4SMateusz Guzik 			    ("vnode already in hash"));
1631cfdefbbSSemen Ustimenko 		}
1641cfdefbbSSemen Ustimenko 	}
165aeabf8d4SMateusz Guzik #endif
1661cfdefbbSSemen Ustimenko 	LIST_INSERT_HEAD(hd, xp, null_hash);
1671cfdefbbSSemen Ustimenko }
168df8bae1dSRodney W. Grimes 
16961b9d89fSTor Egge static void
17067e3d54fSKonstantin Belousov null_destroy_proto(struct vnode *vp, void *xp)
17167e3d54fSKonstantin Belousov {
17267e3d54fSKonstantin Belousov 
17366f02f4bSKonstantin Belousov 	lockmgr(&vp->v_lock, LK_EXCLUSIVE, NULL);
17467e3d54fSKonstantin Belousov 	VI_LOCK(vp);
17567e3d54fSKonstantin Belousov 	vp->v_data = NULL;
17667e3d54fSKonstantin Belousov 	vp->v_vnlock = &vp->v_lock;
17767e3d54fSKonstantin Belousov 	vp->v_op = &dead_vnodeops;
17867e3d54fSKonstantin Belousov 	VI_UNLOCK(vp);
17967e3d54fSKonstantin Belousov 	vgone(vp);
18067e3d54fSKonstantin Belousov 	vput(vp);
18167e3d54fSKonstantin Belousov 	free(xp, M_NULLFSNODE);
18267e3d54fSKonstantin Belousov }
18367e3d54fSKonstantin Belousov 
184df8bae1dSRodney W. Grimes /*
1851cfdefbbSSemen Ustimenko  * Make a new or get existing nullfs node.
1861cfdefbbSSemen Ustimenko  * Vp is the alias vnode, lowervp is the lower vnode.
1871cfdefbbSSemen Ustimenko  *
1881cfdefbbSSemen Ustimenko  * The lowervp assumed to be locked and having "spare" reference. This routine
1891cfdefbbSSemen Ustimenko  * vrele lowervp if nullfs node was taken from hash. Otherwise it "transfers"
1901cfdefbbSSemen Ustimenko  * the caller's "spare" reference to created nullfs vnode.
191df8bae1dSRodney W. Grimes  */
1921cfdefbbSSemen Ustimenko int
193d35991d3SMateusz Guzik null_nodeget(struct mount *mp, struct vnode *lowervp, struct vnode **vpp)
194df8bae1dSRodney W. Grimes {
195df8bae1dSRodney W. Grimes 	struct null_node *xp;
1961cfdefbbSSemen Ustimenko 	struct vnode *vp;
197df8bae1dSRodney W. Grimes 	int error;
198df8bae1dSRodney W. Grimes 
199d9e9650aSKonstantin Belousov 	ASSERT_VOP_LOCKED(lowervp, "lowervp");
200f1fa1ba3SMateusz Guzik 	VNPASS(lowervp->v_usecount > 0, lowervp);
20148a1e3f6SKonstantin Belousov 
202d9e9650aSKonstantin Belousov 	/* Lookup the hash firstly. */
20354939875STim J. Robbins 	*vpp = null_hashget(mp, lowervp);
2041cfdefbbSSemen Ustimenko 	if (*vpp != NULL) {
2051cfdefbbSSemen Ustimenko 		vrele(lowervp);
2061cfdefbbSSemen Ustimenko 		return (0);
2071cfdefbbSSemen Ustimenko 	}
2081cfdefbbSSemen Ustimenko 
2091cfdefbbSSemen Ustimenko 	/*
2101cfdefbbSSemen Ustimenko 	 * We do not serialize vnode creation, instead we will check for
2111cfdefbbSSemen Ustimenko 	 * duplicates later, when adding new vnode to hash.
2121cfdefbbSSemen Ustimenko 	 * Note that duplicate can only appear in hash if the lowervp is
2131cfdefbbSSemen Ustimenko 	 * locked LK_SHARED.
2142f9bae59SDavid Greenman 	 */
215d9e9650aSKonstantin Belousov 	xp = malloc(sizeof(struct null_node), M_NULLFSNODE, M_WAITOK);
2162f9bae59SDavid Greenman 
217e583d999SEdward Tomasz Napierala 	error = getnewvnode("nullfs", mp, &null_vnodeops, &vp);
2182f9bae59SDavid Greenman 	if (error) {
219dd0f9532SKonstantin Belousov 		vput(lowervp);
2201ede983cSDag-Erling Smørgrav 		free(xp, M_NULLFSNODE);
221df8bae1dSRodney W. Grimes 		return (error);
2222f9bae59SDavid Greenman 	}
223df8bae1dSRodney W. Grimes 
224aeabf8d4SMateusz Guzik 	VNPASS(vp->v_object == NULL, vp);
225aeabf8d4SMateusz Guzik 	VNPASS((vn_irflag_read(vp) & VIRF_PGREAD) == 0, vp);
226aeabf8d4SMateusz Guzik 
227aeabf8d4SMateusz Guzik 	rw_wlock(&null_hash_lock);
228df8bae1dSRodney W. Grimes 	xp->null_vnode = vp;
229df8bae1dSRodney W. Grimes 	xp->null_lowervp = lowervp;
2300fc6daa7SKonstantin Belousov 	xp->null_flags = 0;
23108720e34SSemen Ustimenko 	vp->v_type = lowervp->v_type;
23208720e34SSemen Ustimenko 	vp->v_data = xp;
2334451405fSBoris Popov 	vp->v_vnlock = lowervp->v_vnlock;
234aeabf8d4SMateusz Guzik 	*vpp = null_hashget_locked(mp, lowervp);
235aeabf8d4SMateusz Guzik 	if (*vpp != NULL) {
236aeabf8d4SMateusz Guzik 		rw_wunlock(&null_hash_lock);
237aeabf8d4SMateusz Guzik 		vrele(lowervp);
2384e91a0b9SMateusz Guzik 		null_destroy_proto(vp, xp);
239aeabf8d4SMateusz Guzik 		return (0);
2404e91a0b9SMateusz Guzik 	}
241dc1d2cc6SKonstantin Belousov 
242df8bae1dSRodney W. Grimes 	/*
243685cb01aSKonstantin Belousov 	 * We might miss the case where lower vnode sets VIRF_PGREAD
244685cb01aSKonstantin Belousov 	 * some time after construction, which is typical case.
245685cb01aSKonstantin Belousov 	 * null_open rechecks.
246685cb01aSKonstantin Belousov 	 */
2473e506a67SMateusz Guzik 	if ((vn_irflag_read(lowervp) & VIRF_PGREAD) != 0) {
248685cb01aSKonstantin Belousov 		MPASS(lowervp->v_object != NULL);
249685cb01aSKonstantin Belousov 		vp->v_object = lowervp->v_object;
250aeabf8d4SMateusz Guzik 		vn_irflag_set(vp, VIRF_PGREAD);
251685cb01aSKonstantin Belousov 	}
252aeabf8d4SMateusz Guzik 	if (lowervp == MOUNTTONULLMOUNT(mp)->nullm_lowerrootvp)
253aeabf8d4SMateusz Guzik 		vp->v_vflag |= VV_ROOT;
254aeabf8d4SMateusz Guzik 
255aeabf8d4SMateusz Guzik 	error = insmntque1(vp, mp);
256aeabf8d4SMateusz Guzik 	if (error != 0) {
257aeabf8d4SMateusz Guzik 		rw_wunlock(&null_hash_lock);
258aeabf8d4SMateusz Guzik 		vput(lowervp);
259aeabf8d4SMateusz Guzik 		vp->v_object = NULL;
260aeabf8d4SMateusz Guzik 		null_destroy_proto(vp, xp);
261aeabf8d4SMateusz Guzik 		return (error);
262685cb01aSKonstantin Belousov 	}
263685cb01aSKonstantin Belousov 
264aeabf8d4SMateusz Guzik 	null_hashins(mp, xp);
265*829f0bcbSMateusz Guzik 	vn_set_state(vp, VSTATE_CONSTRUCTED);
266aeabf8d4SMateusz Guzik 	rw_wunlock(&null_hash_lock);
2671cfdefbbSSemen Ustimenko 	*vpp = vp;
2681cfdefbbSSemen Ustimenko 
2691cfdefbbSSemen Ustimenko 	return (0);
2701cfdefbbSSemen Ustimenko }
2711cfdefbbSSemen Ustimenko 
2721cfdefbbSSemen Ustimenko /*
2731cfdefbbSSemen Ustimenko  * Remove node from hash.
2741cfdefbbSSemen Ustimenko  */
27508720e34SSemen Ustimenko void
276d35991d3SMateusz Guzik null_hashrem(struct null_node *xp)
27708720e34SSemen Ustimenko {
27808720e34SSemen Ustimenko 
279cd29b292SMateusz Guzik 	rw_wlock(&null_hash_lock);
28008720e34SSemen Ustimenko 	LIST_REMOVE(xp, null_hash);
281cd29b292SMateusz Guzik 	rw_wunlock(&null_hash_lock);
28208720e34SSemen Ustimenko }
28308720e34SSemen Ustimenko 
284a0f40f54SBruce Evans #ifdef DIAGNOSTIC
2851bf978ceSKATO Takenori 
286df8bae1dSRodney W. Grimes struct vnode *
287d35991d3SMateusz Guzik null_checkvp(struct vnode *vp, char *fil, int lno)
288df8bae1dSRodney W. Grimes {
289df8bae1dSRodney W. Grimes 	struct null_node *a = VTONULL(vp);
290b9131889SKonstantin Belousov 
291df8bae1dSRodney W. Grimes #ifdef notyet
292df8bae1dSRodney W. Grimes 	/*
293df8bae1dSRodney W. Grimes 	 * Can't do this check because vop_reclaim runs
294df8bae1dSRodney W. Grimes 	 * with a funny vop vector.
295df8bae1dSRodney W. Grimes 	 */
296df8bae1dSRodney W. Grimes 	if (vp->v_op != null_vnodeop_p) {
297df8bae1dSRodney W. Grimes 		printf ("null_checkvp: on non-null-node\n");
298df8bae1dSRodney W. Grimes 		panic("null_checkvp");
299b9131889SKonstantin Belousov 	}
300df8bae1dSRodney W. Grimes #endif
301c5e17d9eSKATO Takenori 	if (a->null_lowervp == NULLVP) {
302df8bae1dSRodney W. Grimes 		/* Should never happen */
3034d2310ddSKonstantin Belousov 		panic("null_checkvp %p", vp);
304df8bae1dSRodney W. Grimes 	}
305b9131889SKonstantin Belousov 	VI_LOCK_FLAGS(a->null_lowervp, MTX_DUPOK);
3064d2310ddSKonstantin Belousov 	if (a->null_lowervp->v_usecount < 1)
3074d2310ddSKonstantin Belousov 		panic ("null with unref'ed lowervp, vp %p lvp %p",
3084d2310ddSKonstantin Belousov 		    vp, a->null_lowervp);
309b9131889SKonstantin Belousov 	VI_UNLOCK(a->null_lowervp);
310df8bae1dSRodney W. Grimes #ifdef notyet
311df8bae1dSRodney W. Grimes 	printf("null %x/%d -> %x/%d [%s, %d]\n",
3124d93c0beSJeff Roberson 	        NULLTOV(a), vrefcnt(NULLTOV(a)),
3134d93c0beSJeff Roberson 		a->null_lowervp, vrefcnt(a->null_lowervp),
314df8bae1dSRodney W. Grimes 		fil, lno);
315df8bae1dSRodney W. Grimes #endif
316b9131889SKonstantin Belousov 	return (a->null_lowervp);
317df8bae1dSRodney W. Grimes }
318df8bae1dSRodney W. Grimes #endif
319