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