xref: /freebsd/sys/fs/nullfs/null_subr.c (revision 2e3f49888ec8851bafb22011533217487764fdb0)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1992, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software donated to Berkeley by
8  * Jan-Simon Pendry.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/lock.h>
39 #include <sys/rwlock.h>
40 #include <sys/malloc.h>
41 #include <sys/mount.h>
42 #include <sys/proc.h>
43 #include <sys/vnode.h>
44 
45 #include <fs/nullfs/null.h>
46 
47 /*
48  * Null layer cache:
49  * Each cache entry holds a reference to the lower vnode
50  * along with a pointer to the alias vnode.  When an
51  * entry is added the lower vnode is VREF'd.  When the
52  * alias is removed the lower vnode is vrele'd.
53  */
54 
55 #define	NULL_NHASH(vp) (&null_node_hashtbl[vfs_hash_index(vp) & null_hash_mask])
56 
57 static LIST_HEAD(null_node_hashhead, null_node) *null_node_hashtbl;
58 static struct rwlock null_hash_lock;
59 static u_long null_hash_mask;
60 
61 static MALLOC_DEFINE(M_NULLFSHASH, "nullfs_hash", "NULLFS hash table");
62 MALLOC_DEFINE(M_NULLFSNODE, "nullfs_node", "NULLFS vnode private part");
63 
64 static void null_hashins(struct mount *, struct null_node *);
65 
66 /*
67  * Initialise cache headers
68  */
69 int
70 nullfs_init(struct vfsconf *vfsp)
71 {
72 
73 	null_node_hashtbl = hashinit(desiredvnodes, M_NULLFSHASH,
74 	    &null_hash_mask);
75 	rw_init(&null_hash_lock, "nullhs");
76 	return (0);
77 }
78 
79 int
80 nullfs_uninit(struct vfsconf *vfsp)
81 {
82 
83 	rw_destroy(&null_hash_lock);
84 	hashdestroy(null_node_hashtbl, M_NULLFSHASH, null_hash_mask);
85 	return (0);
86 }
87 
88 /*
89  * Return a VREF'ed alias for lower vnode if already exists, else 0.
90  * Lower vnode should be locked on entry and will be left locked on exit.
91  */
92 static struct vnode *
93 null_hashget_locked(struct mount *mp, struct vnode *lowervp)
94 {
95 	struct null_node_hashhead *hd;
96 	struct null_node *a;
97 	struct vnode *vp;
98 
99 	ASSERT_VOP_LOCKED(lowervp, "null_hashget");
100 	rw_assert(&null_hash_lock, RA_LOCKED);
101 
102 	/*
103 	 * Find hash base, and then search the (two-way) linked
104 	 * list looking for a null_node structure which is referencing
105 	 * the lower vnode.  If found, the increment the null_node
106 	 * reference count (but NOT the lower vnode's VREF counter).
107 	 */
108 	hd = NULL_NHASH(lowervp);
109 	LIST_FOREACH(a, hd, null_hash) {
110 		if (a->null_lowervp == lowervp && NULLTOV(a)->v_mount == mp) {
111 			/*
112 			 * Since we have the lower node locked the nullfs
113 			 * node can not be in the process of recycling.  If
114 			 * it had been recycled before we grabed the lower
115 			 * lock it would not have been found on the hash.
116 			 */
117 			vp = NULLTOV(a);
118 			vref(vp);
119 			return (vp);
120 		}
121 	}
122 	return (NULLVP);
123 }
124 
125 struct vnode *
126 null_hashget(struct mount *mp, struct vnode *lowervp)
127 {
128 	struct null_node_hashhead *hd;
129 	struct vnode *vp;
130 
131 	hd = NULL_NHASH(lowervp);
132 	if (LIST_EMPTY(hd))
133 		return (NULLVP);
134 
135 	rw_rlock(&null_hash_lock);
136 	vp = null_hashget_locked(mp, lowervp);
137 	rw_runlock(&null_hash_lock);
138 
139 	return (vp);
140 }
141 
142 static void
143 null_hashins(struct mount *mp, struct null_node *xp)
144 {
145 	struct null_node_hashhead *hd;
146 #ifdef INVARIANTS
147 	struct null_node *oxp;
148 #endif
149 
150 	rw_assert(&null_hash_lock, RA_WLOCKED);
151 
152 	hd = NULL_NHASH(xp->null_lowervp);
153 #ifdef INVARIANTS
154 	LIST_FOREACH(oxp, hd, null_hash) {
155 		if (oxp->null_lowervp == xp->null_lowervp &&
156 		    NULLTOV(oxp)->v_mount == mp) {
157 			VNASSERT(0, NULLTOV(oxp),
158 			    ("vnode already in hash"));
159 		}
160 	}
161 #endif
162 	LIST_INSERT_HEAD(hd, xp, null_hash);
163 }
164 
165 static void
166 null_destroy_proto(struct vnode *vp, void *xp)
167 {
168 
169 	lockmgr(&vp->v_lock, LK_EXCLUSIVE, NULL);
170 	VI_LOCK(vp);
171 	vp->v_data = NULL;
172 	vp->v_vnlock = &vp->v_lock;
173 	vp->v_op = &dead_vnodeops;
174 	VI_UNLOCK(vp);
175 	vgone(vp);
176 	vput(vp);
177 	free(xp, M_NULLFSNODE);
178 }
179 
180 /*
181  * Make a new or get existing nullfs node.
182  * Vp is the alias vnode, lowervp is the lower vnode.
183  *
184  * The lowervp assumed to be locked and having "spare" reference. This routine
185  * vrele lowervp if nullfs node was taken from hash. Otherwise it "transfers"
186  * the caller's "spare" reference to created nullfs vnode.
187  */
188 int
189 null_nodeget(struct mount *mp, struct vnode *lowervp, struct vnode **vpp)
190 {
191 	struct null_node *xp;
192 	struct vnode *vp;
193 	int error;
194 
195 	ASSERT_VOP_LOCKED(lowervp, "lowervp");
196 	VNPASS(lowervp->v_usecount > 0, lowervp);
197 
198 	/* Lookup the hash firstly. */
199 	*vpp = null_hashget(mp, lowervp);
200 	if (*vpp != NULL) {
201 		vrele(lowervp);
202 		return (0);
203 	}
204 
205 	/*
206 	 * We do not serialize vnode creation, instead we will check for
207 	 * duplicates later, when adding new vnode to hash.
208 	 * Note that duplicate can only appear in hash if the lowervp is
209 	 * locked LK_SHARED.
210 	 */
211 	xp = malloc(sizeof(struct null_node), M_NULLFSNODE, M_WAITOK);
212 
213 	error = getnewvnode("nullfs", mp, &null_vnodeops, &vp);
214 	if (error) {
215 		vput(lowervp);
216 		free(xp, M_NULLFSNODE);
217 		return (error);
218 	}
219 
220 	VNPASS(vp->v_object == NULL, vp);
221 	VNPASS((vn_irflag_read(vp) & VIRF_PGREAD) == 0, vp);
222 
223 	rw_wlock(&null_hash_lock);
224 	xp->null_vnode = vp;
225 	xp->null_lowervp = lowervp;
226 	xp->null_flags = 0;
227 	vp->v_type = lowervp->v_type;
228 	vp->v_data = xp;
229 	vp->v_vnlock = lowervp->v_vnlock;
230 	*vpp = null_hashget_locked(mp, lowervp);
231 	if (*vpp != NULL) {
232 		rw_wunlock(&null_hash_lock);
233 		vrele(lowervp);
234 		null_destroy_proto(vp, xp);
235 		return (0);
236 	}
237 
238 	/*
239 	 * We might miss the case where lower vnode sets VIRF_PGREAD
240 	 * some time after construction, which is typical case.
241 	 * null_open rechecks.
242 	 */
243 	if ((vn_irflag_read(lowervp) & VIRF_PGREAD) != 0) {
244 		MPASS(lowervp->v_object != NULL);
245 		vp->v_object = lowervp->v_object;
246 		vn_irflag_set(vp, VIRF_PGREAD);
247 	}
248 	if (lowervp == MOUNTTONULLMOUNT(mp)->nullm_lowerrootvp)
249 		vp->v_vflag |= VV_ROOT;
250 
251 	error = insmntque1(vp, mp);
252 	if (error != 0) {
253 		rw_wunlock(&null_hash_lock);
254 		vput(lowervp);
255 		vp->v_object = NULL;
256 		null_destroy_proto(vp, xp);
257 		return (error);
258 	}
259 
260 	null_hashins(mp, xp);
261 	vn_set_state(vp, VSTATE_CONSTRUCTED);
262 	rw_wunlock(&null_hash_lock);
263 	*vpp = vp;
264 
265 	return (0);
266 }
267 
268 /*
269  * Remove node from hash.
270  */
271 void
272 null_hashrem(struct null_node *xp)
273 {
274 
275 	rw_wlock(&null_hash_lock);
276 	LIST_REMOVE(xp, null_hash);
277 	rw_wunlock(&null_hash_lock);
278 }
279 
280 #ifdef DIAGNOSTIC
281 
282 struct vnode *
283 null_checkvp(struct vnode *vp, char *fil, int lno)
284 {
285 	struct null_node *a = VTONULL(vp);
286 
287 #ifdef notyet
288 	/*
289 	 * Can't do this check because vop_reclaim runs
290 	 * with a funny vop vector.
291 	 */
292 	if (vp->v_op != null_vnodeop_p) {
293 		printf ("null_checkvp: on non-null-node\n");
294 		panic("null_checkvp");
295 	}
296 #endif
297 	if (a->null_lowervp == NULLVP) {
298 		/* Should never happen */
299 		panic("null_checkvp %p", vp);
300 	}
301 	VI_LOCK_FLAGS(a->null_lowervp, MTX_DUPOK);
302 	if (a->null_lowervp->v_usecount < 1)
303 		panic ("null with unref'ed lowervp, vp %p lvp %p",
304 		    vp, a->null_lowervp);
305 	VI_UNLOCK(a->null_lowervp);
306 #ifdef notyet
307 	printf("null %x/%d -> %x/%d [%s, %d]\n",
308 	        NULLTOV(a), vrefcnt(NULLTOV(a)),
309 		a->null_lowervp, vrefcnt(a->null_lowervp),
310 		fil, lno);
311 #endif
312 	return (a->null_lowervp);
313 }
314 #endif
315