xref: /freebsd/sys/fs/nullfs/null_subr.c (revision 13ec1e3155c7e9bf037b12af186351b7fa9b9450)
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  *	@(#)null_subr.c	8.7 (Berkeley) 5/14/95
35  *
36  * $FreeBSD$
37  */
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/lock.h>
43 #include <sys/rwlock.h>
44 #include <sys/malloc.h>
45 #include <sys/mount.h>
46 #include <sys/proc.h>
47 #include <sys/vnode.h>
48 
49 #include <fs/nullfs/null.h>
50 
51 /*
52  * Null layer cache:
53  * Each cache entry holds a reference to the lower vnode
54  * along with a pointer to the alias vnode.  When an
55  * entry is added the lower vnode is VREF'd.  When the
56  * alias is removed the lower vnode is vrele'd.
57  */
58 
59 #define	NULL_NHASH(vp) (&null_node_hashtbl[vfs_hash_index(vp) & null_hash_mask])
60 
61 static LIST_HEAD(null_node_hashhead, null_node) *null_node_hashtbl;
62 static struct rwlock null_hash_lock;
63 static u_long null_hash_mask;
64 
65 static MALLOC_DEFINE(M_NULLFSHASH, "nullfs_hash", "NULLFS hash table");
66 MALLOC_DEFINE(M_NULLFSNODE, "nullfs_node", "NULLFS vnode private part");
67 
68 static struct vnode * null_hashins(struct mount *, struct null_node *);
69 
70 /*
71  * Initialise cache headers
72  */
73 int
74 nullfs_init(struct vfsconf *vfsp)
75 {
76 
77 	null_node_hashtbl = hashinit(desiredvnodes, M_NULLFSHASH,
78 	    &null_hash_mask);
79 	rw_init(&null_hash_lock, "nullhs");
80 	return (0);
81 }
82 
83 int
84 nullfs_uninit(struct vfsconf *vfsp)
85 {
86 
87 	rw_destroy(&null_hash_lock);
88 	hashdestroy(null_node_hashtbl, M_NULLFSHASH, null_hash_mask);
89 	return (0);
90 }
91 
92 /*
93  * Return a VREF'ed alias for lower vnode if already exists, else 0.
94  * Lower vnode should be locked on entry and will be left locked on exit.
95  */
96 struct vnode *
97 null_hashget(struct mount *mp, struct vnode *lowervp)
98 {
99 	struct null_node_hashhead *hd;
100 	struct null_node *a;
101 	struct vnode *vp;
102 
103 	ASSERT_VOP_LOCKED(lowervp, "null_hashget");
104 
105 	/*
106 	 * Find hash base, and then search the (two-way) linked
107 	 * list looking for a null_node structure which is referencing
108 	 * the lower vnode.  If found, the increment the null_node
109 	 * reference count (but NOT the lower vnode's VREF counter).
110 	 */
111 	hd = NULL_NHASH(lowervp);
112 	if (LIST_EMPTY(hd))
113 		return (NULLVP);
114 	rw_rlock(&null_hash_lock);
115 	LIST_FOREACH(a, hd, null_hash) {
116 		if (a->null_lowervp == lowervp && NULLTOV(a)->v_mount == mp) {
117 			/*
118 			 * Since we have the lower node locked the nullfs
119 			 * node can not be in the process of recycling.  If
120 			 * it had been recycled before we grabed the lower
121 			 * lock it would not have been found on the hash.
122 			 */
123 			vp = NULLTOV(a);
124 			vref(vp);
125 			rw_runlock(&null_hash_lock);
126 			return (vp);
127 		}
128 	}
129 	rw_runlock(&null_hash_lock);
130 	return (NULLVP);
131 }
132 
133 /*
134  * Act like null_hashget, but add passed null_node to hash if no existing
135  * node found.
136  */
137 static struct vnode *
138 null_hashins(struct mount *mp, struct null_node *xp)
139 {
140 	struct null_node_hashhead *hd;
141 	struct null_node *oxp;
142 	struct vnode *ovp;
143 
144 	hd = NULL_NHASH(xp->null_lowervp);
145 	rw_wlock(&null_hash_lock);
146 	LIST_FOREACH(oxp, hd, null_hash) {
147 		if (oxp->null_lowervp == xp->null_lowervp &&
148 		    NULLTOV(oxp)->v_mount == mp) {
149 			/*
150 			 * See null_hashget for a description of this
151 			 * operation.
152 			 */
153 			ovp = NULLTOV(oxp);
154 			vref(ovp);
155 			rw_wunlock(&null_hash_lock);
156 			return (ovp);
157 		}
158 	}
159 	LIST_INSERT_HEAD(hd, xp, null_hash);
160 	rw_wunlock(&null_hash_lock);
161 	return (NULLVP);
162 }
163 
164 static void
165 null_destroy_proto(struct vnode *vp, void *xp)
166 {
167 
168 	lockmgr(&vp->v_lock, LK_EXCLUSIVE, NULL);
169 	VI_LOCK(vp);
170 	vp->v_data = NULL;
171 	vp->v_vnlock = &vp->v_lock;
172 	vp->v_op = &dead_vnodeops;
173 	VI_UNLOCK(vp);
174 	vgone(vp);
175 	vput(vp);
176 	free(xp, M_NULLFSNODE);
177 }
178 
179 /*
180  * Make a new or get existing nullfs node.
181  * Vp is the alias vnode, lowervp is the lower vnode.
182  *
183  * The lowervp assumed to be locked and having "spare" reference. This routine
184  * vrele lowervp if nullfs node was taken from hash. Otherwise it "transfers"
185  * the caller's "spare" reference to created nullfs vnode.
186  */
187 int
188 null_nodeget(struct mount *mp, struct vnode *lowervp, struct vnode **vpp)
189 {
190 	struct null_node *xp;
191 	struct vnode *vp;
192 	int error;
193 
194 	ASSERT_VOP_LOCKED(lowervp, "lowervp");
195 	VNPASS(lowervp->v_usecount > 0, lowervp);
196 
197 	/* Lookup the hash firstly. */
198 	*vpp = null_hashget(mp, lowervp);
199 	if (*vpp != NULL) {
200 		vrele(lowervp);
201 		return (0);
202 	}
203 
204 	/*
205 	 * The insmntque1() call below requires the exclusive lock on
206 	 * the nullfs vnode.  Upgrade the lock now if hash failed to
207 	 * provide ready to use vnode.
208 	 */
209 	if (VOP_ISLOCKED(lowervp) != LK_EXCLUSIVE) {
210 		vn_lock(lowervp, LK_UPGRADE | LK_RETRY);
211 		if (VN_IS_DOOMED(lowervp)) {
212 			vput(lowervp);
213 			return (ENOENT);
214 		}
215 	}
216 
217 	/*
218 	 * We do not serialize vnode creation, instead we will check for
219 	 * duplicates later, when adding new vnode to hash.
220 	 * Note that duplicate can only appear in hash if the lowervp is
221 	 * locked LK_SHARED.
222 	 */
223 	xp = malloc(sizeof(struct null_node), M_NULLFSNODE, M_WAITOK);
224 
225 	error = getnewvnode("nullfs", mp, &null_vnodeops, &vp);
226 	if (error) {
227 		vput(lowervp);
228 		free(xp, M_NULLFSNODE);
229 		return (error);
230 	}
231 
232 	xp->null_vnode = vp;
233 	xp->null_lowervp = lowervp;
234 	xp->null_flags = 0;
235 	vp->v_type = lowervp->v_type;
236 	vp->v_data = xp;
237 	vp->v_vnlock = lowervp->v_vnlock;
238 	error = insmntque1(vp, mp);
239 	if (error != 0) {
240 		vput(lowervp);
241 		null_destroy_proto(vp, xp);
242 		return (error);
243 	}
244 	if (lowervp == MOUNTTONULLMOUNT(mp)->nullm_lowerrootvp)
245 		vp->v_vflag |= VV_ROOT;
246 
247 	/*
248 	 * We might miss the case where lower vnode sets VIRF_PGREAD
249 	 * some time after construction, which is typical case.
250 	 * null_open rechecks.
251 	 */
252 	if ((vn_irflag_read(lowervp) & VIRF_PGREAD) != 0) {
253 		MPASS(lowervp->v_object != NULL);
254 		if ((vn_irflag_read(vp) & VIRF_PGREAD) == 0) {
255 			if (vp->v_object == NULL)
256 				vp->v_object = lowervp->v_object;
257 			else
258 				MPASS(vp->v_object == lowervp->v_object);
259 			vn_irflag_set_cond(vp, VIRF_PGREAD);
260 		} else {
261 			MPASS(vp->v_object != NULL);
262 		}
263 	}
264 
265 	/*
266 	 * Atomically insert our new node into the hash or vget existing
267 	 * if someone else has beaten us to it.
268 	 */
269 	*vpp = null_hashins(mp, xp);
270 	if (*vpp != NULL) {
271 		vrele(lowervp);
272 		vp->v_object = NULL;	/* in case VIRF_PGREAD set it */
273 		null_destroy_proto(vp, xp);
274 		return (0);
275 	}
276 	*vpp = vp;
277 
278 	return (0);
279 }
280 
281 /*
282  * Remove node from hash.
283  */
284 void
285 null_hashrem(struct null_node *xp)
286 {
287 
288 	rw_wlock(&null_hash_lock);
289 	LIST_REMOVE(xp, null_hash);
290 	rw_wunlock(&null_hash_lock);
291 }
292 
293 #ifdef DIAGNOSTIC
294 
295 struct vnode *
296 null_checkvp(struct vnode *vp, char *fil, int lno)
297 {
298 	struct null_node *a = VTONULL(vp);
299 
300 #ifdef notyet
301 	/*
302 	 * Can't do this check because vop_reclaim runs
303 	 * with a funny vop vector.
304 	 */
305 	if (vp->v_op != null_vnodeop_p) {
306 		printf ("null_checkvp: on non-null-node\n");
307 		panic("null_checkvp");
308 	}
309 #endif
310 	if (a->null_lowervp == NULLVP) {
311 		/* Should never happen */
312 		panic("null_checkvp %p", vp);
313 	}
314 	VI_LOCK_FLAGS(a->null_lowervp, MTX_DUPOK);
315 	if (a->null_lowervp->v_usecount < 1)
316 		panic ("null with unref'ed lowervp, vp %p lvp %p",
317 		    vp, a->null_lowervp);
318 	VI_UNLOCK(a->null_lowervp);
319 #ifdef notyet
320 	printf("null %x/%d -> %x/%d [%s, %d]\n",
321 	        NULLTOV(a), vrefcnt(NULLTOV(a)),
322 		a->null_lowervp, vrefcnt(a->null_lowervp),
323 		fil, lno);
324 #endif
325 	return (a->null_lowervp);
326 }
327 #endif
328