xref: /freebsd/sys/fs/nullfs/null_subr.c (revision fbbd9655e5107c68e4e0146ff22b73d7350475bc)
1d167cf6fSWarner Losh /*-
2df8bae1dSRodney W. Grimes  * Copyright (c) 1992, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * This code is derived from software donated to Berkeley by
6df8bae1dSRodney W. Grimes  * Jan-Simon Pendry.
7df8bae1dSRodney W. Grimes  *
8df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
9df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
10df8bae1dSRodney W. Grimes  * are met:
11df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
12df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
13df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
14df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
15df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
16*fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
17df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
18df8bae1dSRodney W. Grimes  *    without specific prior written permission.
19df8bae1dSRodney W. Grimes  *
20df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
31df8bae1dSRodney W. Grimes  *
32996c772fSJohn Dyson  *	@(#)null_subr.c	8.7 (Berkeley) 5/14/95
33df8bae1dSRodney W. Grimes  *
34c3aac50fSPeter Wemm  * $FreeBSD$
35df8bae1dSRodney W. Grimes  */
36df8bae1dSRodney W. Grimes 
37df8bae1dSRodney W. Grimes #include <sys/param.h>
38df8bae1dSRodney W. Grimes #include <sys/systm.h>
398da80660SBoris Popov #include <sys/kernel.h>
40fb919e4dSMark Murray #include <sys/lock.h>
41cd29b292SMateusz Guzik #include <sys/rwlock.h>
42df8bae1dSRodney W. Grimes #include <sys/malloc.h>
43fb919e4dSMark Murray #include <sys/mount.h>
44fb919e4dSMark Murray #include <sys/proc.h>
45fb919e4dSMark Murray #include <sys/vnode.h>
46fb919e4dSMark Murray 
4799d300a1SRuslan Ermilov #include <fs/nullfs/null.h>
48df8bae1dSRodney W. Grimes 
49df8bae1dSRodney W. Grimes /*
50df8bae1dSRodney W. Grimes  * Null layer cache:
51df8bae1dSRodney W. Grimes  * Each cache entry holds a reference to the lower vnode
52df8bae1dSRodney W. Grimes  * along with a pointer to the alias vnode.  When an
53df8bae1dSRodney W. Grimes  * entry is added the lower vnode is VREF'd.  When the
54df8bae1dSRodney W. Grimes  * alias is removed the lower vnode is vrele'd.
55df8bae1dSRodney W. Grimes  */
56df8bae1dSRodney W. Grimes 
57603f963eSKonstantin Belousov #define	NULL_NHASH(vp) (&null_node_hashtbl[vfs_hash_index(vp) & null_hash_mask])
588da80660SBoris Popov 
59e3975643SJake Burkholder static LIST_HEAD(null_node_hashhead, null_node) *null_node_hashtbl;
60cd29b292SMateusz Guzik static struct rwlock null_hash_lock;
61603f963eSKonstantin Belousov static u_long null_hash_mask;
628da80660SBoris Popov 
635bb84bc8SRobert Watson static MALLOC_DEFINE(M_NULLFSHASH, "nullfs_hash", "NULLFS hash table");
645bb84bc8SRobert Watson MALLOC_DEFINE(M_NULLFSNODE, "nullfs_node", "NULLFS vnode private part");
65df8bae1dSRodney W. Grimes 
6654939875STim J. Robbins static struct vnode * null_hashins(struct mount *, struct null_node *);
679b5e8b3aSBruce Evans 
68df8bae1dSRodney W. Grimes /*
69df8bae1dSRodney W. Grimes  * Initialise cache headers
70df8bae1dSRodney W. Grimes  */
7126f9a767SRodney W. Grimes int
72996c772fSJohn Dyson nullfs_init(vfsp)
73996c772fSJohn Dyson 	struct vfsconf *vfsp;
74df8bae1dSRodney W. Grimes {
75996c772fSJohn Dyson 
76603f963eSKonstantin Belousov 	null_node_hashtbl = hashinit(desiredvnodes, M_NULLFSHASH,
77603f963eSKonstantin Belousov 	    &null_hash_mask);
78cd29b292SMateusz Guzik 	rw_init(&null_hash_lock, "nullhs");
798da80660SBoris Popov 	return (0);
808da80660SBoris Popov }
818da80660SBoris Popov 
828da80660SBoris Popov int
838da80660SBoris Popov nullfs_uninit(vfsp)
848da80660SBoris Popov 	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  */
96d9e9650aSKonstantin Belousov struct vnode *
9754939875STim J. Robbins null_hashget(mp, lowervp)
9854939875STim J. Robbins 	struct mount *mp;
99df8bae1dSRodney W. Grimes 	struct vnode *lowervp;
100df8bae1dSRodney W. Grimes {
101996c772fSJohn Dyson 	struct null_node_hashhead *hd;
102df8bae1dSRodney W. Grimes 	struct null_node *a;
103df8bae1dSRodney W. Grimes 	struct vnode *vp;
1049c12e631SJeff Roberson 
1059c12e631SJeff Roberson 	ASSERT_VOP_LOCKED(lowervp, "null_hashget");
106df8bae1dSRodney W. Grimes 
107df8bae1dSRodney W. Grimes 	/*
108df8bae1dSRodney W. Grimes 	 * Find hash base, and then search the (two-way) linked
109df8bae1dSRodney W. Grimes 	 * list looking for a null_node structure which is referencing
110df8bae1dSRodney W. Grimes 	 * the lower vnode.  If found, the increment the null_node
111df8bae1dSRodney W. Grimes 	 * reference count (but NOT the lower vnode's VREF counter).
112df8bae1dSRodney W. Grimes 	 */
113996c772fSJohn Dyson 	hd = NULL_NHASH(lowervp);
114cd29b292SMateusz Guzik 	rw_rlock(&null_hash_lock);
115fc2ffbe6SPoul-Henning Kamp 	LIST_FOREACH(a, hd, null_hash) {
11654939875STim J. Robbins 		if (a->null_lowervp == lowervp && NULLTOV(a)->v_mount == mp) {
1179c12e631SJeff Roberson 			/*
1189c12e631SJeff Roberson 			 * Since we have the lower node locked the nullfs
1199c12e631SJeff Roberson 			 * node can not be in the process of recycling.  If
1209c12e631SJeff Roberson 			 * it had been recycled before we grabed the lower
1219c12e631SJeff Roberson 			 * lock it would not have been found on the hash.
1229c12e631SJeff Roberson 			 */
1234c65d593SJeff Roberson 			vp = NULLTOV(a);
1244c65d593SJeff Roberson 			vref(vp);
125cd29b292SMateusz Guzik 			rw_runlock(&null_hash_lock);
126df8bae1dSRodney W. Grimes 			return (vp);
127df8bae1dSRodney W. Grimes 		}
128df8bae1dSRodney W. Grimes 	}
129cd29b292SMateusz Guzik 	rw_runlock(&null_hash_lock);
1301cfdefbbSSemen Ustimenko 	return (NULLVP);
131df8bae1dSRodney W. Grimes }
132df8bae1dSRodney W. Grimes 
1331cfdefbbSSemen Ustimenko /*
1341cfdefbbSSemen Ustimenko  * Act like null_hashget, but add passed null_node to hash if no existing
1351cfdefbbSSemen Ustimenko  * node found.
1361cfdefbbSSemen Ustimenko  */
1371cfdefbbSSemen Ustimenko static struct vnode *
13854939875STim J. Robbins null_hashins(mp, xp)
13954939875STim J. Robbins 	struct mount *mp;
1401cfdefbbSSemen Ustimenko 	struct null_node *xp;
1411cfdefbbSSemen Ustimenko {
1421cfdefbbSSemen Ustimenko 	struct null_node_hashhead *hd;
1431cfdefbbSSemen Ustimenko 	struct null_node *oxp;
1441cfdefbbSSemen Ustimenko 	struct vnode *ovp;
1451cfdefbbSSemen Ustimenko 
1461cfdefbbSSemen Ustimenko 	hd = NULL_NHASH(xp->null_lowervp);
147cd29b292SMateusz Guzik 	rw_wlock(&null_hash_lock);
1481cfdefbbSSemen Ustimenko 	LIST_FOREACH(oxp, hd, null_hash) {
14954939875STim J. Robbins 		if (oxp->null_lowervp == xp->null_lowervp &&
15054939875STim J. Robbins 		    NULLTOV(oxp)->v_mount == mp) {
1519c12e631SJeff Roberson 			/*
1529c12e631SJeff Roberson 			 * See null_hashget for a description of this
1539c12e631SJeff Roberson 			 * operation.
1549c12e631SJeff Roberson 			 */
1551cfdefbbSSemen Ustimenko 			ovp = NULLTOV(oxp);
1564c65d593SJeff Roberson 			vref(ovp);
157cd29b292SMateusz Guzik 			rw_wunlock(&null_hash_lock);
1581cfdefbbSSemen Ustimenko 			return (ovp);
1591cfdefbbSSemen Ustimenko 		}
1601cfdefbbSSemen Ustimenko 	}
1611cfdefbbSSemen Ustimenko 	LIST_INSERT_HEAD(hd, xp, null_hash);
162cd29b292SMateusz Guzik 	rw_wunlock(&null_hash_lock);
1631cfdefbbSSemen Ustimenko 	return (NULLVP);
1641cfdefbbSSemen Ustimenko }
165df8bae1dSRodney W. Grimes 
16661b9d89fSTor Egge static void
16767e3d54fSKonstantin Belousov null_destroy_proto(struct vnode *vp, void *xp)
16867e3d54fSKonstantin Belousov {
16967e3d54fSKonstantin Belousov 
17066f02f4bSKonstantin Belousov 	lockmgr(&vp->v_lock, LK_EXCLUSIVE, NULL);
17167e3d54fSKonstantin Belousov 	VI_LOCK(vp);
17267e3d54fSKonstantin Belousov 	vp->v_data = NULL;
17367e3d54fSKonstantin Belousov 	vp->v_vnlock = &vp->v_lock;
17467e3d54fSKonstantin Belousov 	vp->v_op = &dead_vnodeops;
17567e3d54fSKonstantin Belousov 	VI_UNLOCK(vp);
17667e3d54fSKonstantin Belousov 	vgone(vp);
17767e3d54fSKonstantin Belousov 	vput(vp);
17867e3d54fSKonstantin Belousov 	free(xp, M_NULLFSNODE);
17967e3d54fSKonstantin Belousov }
18067e3d54fSKonstantin Belousov 
18167e3d54fSKonstantin Belousov static void
18261b9d89fSTor Egge null_insmntque_dtr(struct vnode *vp, void *xp)
18361b9d89fSTor Egge {
184dd0f9532SKonstantin Belousov 
185dd0f9532SKonstantin Belousov 	vput(((struct null_node *)xp)->null_lowervp);
18667e3d54fSKonstantin Belousov 	null_destroy_proto(vp, xp);
18761b9d89fSTor Egge }
18861b9d89fSTor Egge 
189df8bae1dSRodney W. Grimes /*
1901cfdefbbSSemen Ustimenko  * Make a new or get existing nullfs node.
1911cfdefbbSSemen Ustimenko  * Vp is the alias vnode, lowervp is the lower vnode.
1921cfdefbbSSemen Ustimenko  *
1931cfdefbbSSemen Ustimenko  * The lowervp assumed to be locked and having "spare" reference. This routine
1941cfdefbbSSemen Ustimenko  * vrele lowervp if nullfs node was taken from hash. Otherwise it "transfers"
1951cfdefbbSSemen Ustimenko  * the caller's "spare" reference to created nullfs vnode.
196df8bae1dSRodney W. Grimes  */
1971cfdefbbSSemen Ustimenko int
1981cfdefbbSSemen Ustimenko null_nodeget(mp, lowervp, vpp)
199df8bae1dSRodney W. Grimes 	struct mount *mp;
200df8bae1dSRodney W. Grimes 	struct vnode *lowervp;
201df8bae1dSRodney W. Grimes 	struct vnode **vpp;
202df8bae1dSRodney W. Grimes {
203df8bae1dSRodney W. Grimes 	struct null_node *xp;
2041cfdefbbSSemen Ustimenko 	struct vnode *vp;
205df8bae1dSRodney W. Grimes 	int error;
206df8bae1dSRodney W. Grimes 
207d9e9650aSKonstantin Belousov 	ASSERT_VOP_LOCKED(lowervp, "lowervp");
208d9e9650aSKonstantin Belousov 	KASSERT(lowervp->v_usecount >= 1, ("Unreferenced vnode %p", lowervp));
20948a1e3f6SKonstantin Belousov 
210d9e9650aSKonstantin Belousov 	/* Lookup the hash firstly. */
21154939875STim J. Robbins 	*vpp = null_hashget(mp, lowervp);
2121cfdefbbSSemen Ustimenko 	if (*vpp != NULL) {
2131cfdefbbSSemen Ustimenko 		vrele(lowervp);
2141cfdefbbSSemen Ustimenko 		return (0);
2151cfdefbbSSemen Ustimenko 	}
2161cfdefbbSSemen Ustimenko 
2171cfdefbbSSemen Ustimenko 	/*
218d9e9650aSKonstantin Belousov 	 * The insmntque1() call below requires the exclusive lock on
219d9e9650aSKonstantin Belousov 	 * the nullfs vnode.  Upgrade the lock now if hash failed to
220d9e9650aSKonstantin Belousov 	 * provide ready to use vnode.
221d9e9650aSKonstantin Belousov 	 */
222d9e9650aSKonstantin Belousov 	if (VOP_ISLOCKED(lowervp) != LK_EXCLUSIVE) {
223268dd286SKonstantin Belousov 		KASSERT((MOUNTTONULLMOUNT(mp)->nullm_flags & NULLM_CACHE) != 0,
2249cf4c952SKonstantin Belousov 		    ("lowervp %p is not excl locked and cache is disabled",
2259cf4c952SKonstantin Belousov 		    lowervp));
226d9e9650aSKonstantin Belousov 		vn_lock(lowervp, LK_UPGRADE | LK_RETRY);
227d9e9650aSKonstantin Belousov 		if ((lowervp->v_iflag & VI_DOOMED) != 0) {
228d9e9650aSKonstantin Belousov 			vput(lowervp);
229d9e9650aSKonstantin Belousov 			return (ENOENT);
230d9e9650aSKonstantin Belousov 		}
231d9e9650aSKonstantin Belousov 	}
232d9e9650aSKonstantin Belousov 
233d9e9650aSKonstantin Belousov 	/*
2341cfdefbbSSemen Ustimenko 	 * We do not serialize vnode creation, instead we will check for
2351cfdefbbSSemen Ustimenko 	 * duplicates later, when adding new vnode to hash.
2361cfdefbbSSemen Ustimenko 	 * Note that duplicate can only appear in hash if the lowervp is
2371cfdefbbSSemen Ustimenko 	 * locked LK_SHARED.
2382f9bae59SDavid Greenman 	 */
239d9e9650aSKonstantin Belousov 	xp = malloc(sizeof(struct null_node), M_NULLFSNODE, M_WAITOK);
2402f9bae59SDavid Greenman 
241e583d999SEdward Tomasz Napierala 	error = getnewvnode("nullfs", mp, &null_vnodeops, &vp);
2422f9bae59SDavid Greenman 	if (error) {
243dd0f9532SKonstantin Belousov 		vput(lowervp);
2441ede983cSDag-Erling Smørgrav 		free(xp, M_NULLFSNODE);
245df8bae1dSRodney W. Grimes 		return (error);
2462f9bae59SDavid Greenman 	}
247df8bae1dSRodney W. Grimes 
248df8bae1dSRodney W. Grimes 	xp->null_vnode = vp;
249df8bae1dSRodney W. Grimes 	xp->null_lowervp = lowervp;
2500fc6daa7SKonstantin Belousov 	xp->null_flags = 0;
25108720e34SSemen Ustimenko 	vp->v_type = lowervp->v_type;
25208720e34SSemen Ustimenko 	vp->v_data = xp;
2534451405fSBoris Popov 	vp->v_vnlock = lowervp->v_vnlock;
25461b9d89fSTor Egge 	error = insmntque1(vp, mp, null_insmntque_dtr, xp);
25561b9d89fSTor Egge 	if (error != 0)
25661b9d89fSTor Egge 		return (error);
257df8bae1dSRodney W. Grimes 	/*
2581cfdefbbSSemen Ustimenko 	 * Atomically insert our new node into the hash or vget existing
2591cfdefbbSSemen Ustimenko 	 * if someone else has beaten us to it.
260df8bae1dSRodney W. Grimes 	 */
26154939875STim J. Robbins 	*vpp = null_hashins(mp, xp);
2621cfdefbbSSemen Ustimenko 	if (*vpp != NULL) {
2634451405fSBoris Popov 		vrele(lowervp);
26467e3d54fSKonstantin Belousov 		null_destroy_proto(vp, xp);
265df8bae1dSRodney W. Grimes 		return (0);
266df8bae1dSRodney W. Grimes 	}
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
27608720e34SSemen Ustimenko null_hashrem(xp)
27708720e34SSemen Ustimenko 	struct null_node *xp;
27808720e34SSemen Ustimenko {
27908720e34SSemen Ustimenko 
280cd29b292SMateusz Guzik 	rw_wlock(&null_hash_lock);
28108720e34SSemen Ustimenko 	LIST_REMOVE(xp, null_hash);
282cd29b292SMateusz Guzik 	rw_wunlock(&null_hash_lock);
28308720e34SSemen Ustimenko }
28408720e34SSemen Ustimenko 
285a0f40f54SBruce Evans #ifdef DIAGNOSTIC
2861bf978ceSKATO Takenori 
287df8bae1dSRodney W. Grimes struct vnode *
288df8bae1dSRodney W. Grimes null_checkvp(vp, fil, lno)
289df8bae1dSRodney W. Grimes 	struct vnode *vp;
290df8bae1dSRodney W. Grimes 	char *fil;
291df8bae1dSRodney W. Grimes 	int lno;
292df8bae1dSRodney W. Grimes {
293df8bae1dSRodney W. Grimes 	struct null_node *a = VTONULL(vp);
294b9131889SKonstantin Belousov 
295df8bae1dSRodney W. Grimes #ifdef notyet
296df8bae1dSRodney W. Grimes 	/*
297df8bae1dSRodney W. Grimes 	 * Can't do this check because vop_reclaim runs
298df8bae1dSRodney W. Grimes 	 * with a funny vop vector.
299df8bae1dSRodney W. Grimes 	 */
300df8bae1dSRodney W. Grimes 	if (vp->v_op != null_vnodeop_p) {
301df8bae1dSRodney W. Grimes 		printf ("null_checkvp: on non-null-node\n");
302df8bae1dSRodney W. Grimes 		panic("null_checkvp");
303b9131889SKonstantin Belousov 	}
304df8bae1dSRodney W. Grimes #endif
305c5e17d9eSKATO Takenori 	if (a->null_lowervp == NULLVP) {
306df8bae1dSRodney W. Grimes 		/* Should never happen */
3074d2310ddSKonstantin Belousov 		panic("null_checkvp %p", vp);
308df8bae1dSRodney W. Grimes 	}
309b9131889SKonstantin Belousov 	VI_LOCK_FLAGS(a->null_lowervp, MTX_DUPOK);
3104d2310ddSKonstantin Belousov 	if (a->null_lowervp->v_usecount < 1)
3114d2310ddSKonstantin Belousov 		panic ("null with unref'ed lowervp, vp %p lvp %p",
3124d2310ddSKonstantin Belousov 		    vp, a->null_lowervp);
313b9131889SKonstantin Belousov 	VI_UNLOCK(a->null_lowervp);
314df8bae1dSRodney W. Grimes #ifdef notyet
315df8bae1dSRodney W. Grimes 	printf("null %x/%d -> %x/%d [%s, %d]\n",
3164d93c0beSJeff Roberson 	        NULLTOV(a), vrefcnt(NULLTOV(a)),
3174d93c0beSJeff Roberson 		a->null_lowervp, vrefcnt(a->null_lowervp),
318df8bae1dSRodney W. Grimes 		fil, lno);
319df8bae1dSRodney W. Grimes #endif
320b9131889SKonstantin Belousov 	return (a->null_lowervp);
321df8bae1dSRodney W. Grimes }
322df8bae1dSRodney W. Grimes #endif
323