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