]>
Commit | Line | Data |
---|---|---|
f427ee49 A |
1 | /* |
2 | * Copyright (c) 2019 Apple Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
6 | * This file contains Original Code and/or Modifications of Original Code | |
7 | * as defined in and that are subject to the Apple Public Source License | |
8 | * Version 2.0 (the 'License'). You may not use this file except in | |
9 | * compliance with the License. Please obtain a copy of the License at | |
10 | * http://www.opensource.apple.com/apsl/ and read it before using this | |
11 | * file. | |
12 | * | |
13 | * The Original Code and all software distributed under the License are | |
14 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
17 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. | |
18 | * Please see the License for the specific language governing rights and | |
19 | * limitations under the License. | |
20 | * | |
21 | * @APPLE_LICENSE_HEADER_END@ | |
22 | */ | |
23 | ||
24 | /*- | |
25 | * Portions Copyright (c) 1992, 1993 | |
26 | * The Regents of the University of California. All rights reserved. | |
27 | * | |
28 | * This code is derived from software donated to Berkeley by | |
29 | * Jan-Simon Pendry. | |
30 | * | |
31 | * Redistribution and use in source and binary forms, with or without | |
32 | * modification, are permitted provided that the following conditions | |
33 | * are met: | |
34 | * 1. Redistributions of source code must retain the above copyright | |
35 | * notice, this list of conditions and the following disclaimer. | |
36 | * 2. Redistributions in binary form must reproduce the above copyright | |
37 | * notice, this list of conditions and the following disclaimer in the | |
38 | * documentation and/or other materials provided with the distribution. | |
39 | * 4. Neither the name of the University nor the names of its contributors | |
40 | * may be used to endorse or promote products derived from this software | |
41 | * without specific prior written permission. | |
42 | * | |
43 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
44 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
45 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
46 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
47 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
48 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
49 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
50 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
51 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
52 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
53 | * SUCH DAMAGE. | |
54 | * | |
55 | * @(#)null_subr.c 8.7 (Berkeley) 5/14/95 | |
56 | * | |
57 | * $FreeBSD$ | |
58 | */ | |
59 | #include <sys/param.h> | |
60 | #include <sys/systm.h> | |
61 | #include <sys/kernel.h> | |
62 | #include <sys/lock.h> | |
63 | #include <sys/malloc.h> | |
64 | #include <sys/mount.h> | |
65 | #include <sys/proc.h> | |
66 | #include <sys/vnode.h> | |
67 | ||
68 | #include "bindfs.h" | |
69 | ||
70 | /* | |
71 | * Null layer cache: | |
72 | * Each cache entry holds a reference to the lower vnode | |
73 | * along with a pointer to the alias vnode. When an | |
74 | * entry is added the lower vnode is VREF'd. When the | |
75 | * alias is removed the lower vnode is vrele'd. | |
76 | */ | |
77 | ||
78 | #define BIND_HASH_SIZE (desiredvnodes / 10) | |
79 | ||
80 | /* xnu doesn't really have the functionality freebsd uses here..gonna try this | |
81 | * hacked hash...*/ | |
82 | #define BIND_NHASH(vp) (&bind_node_hashtbl[((((uintptr_t)vp) >> vnsz2log) + (uintptr_t)vnode_mount(vp)) & bind_hash_mask]) | |
83 | ||
84 | static LIST_HEAD(bind_node_hashhead, bind_node) * bind_node_hashtbl; | |
85 | static lck_mtx_t bind_hashmtx; | |
86 | static lck_attr_t * bind_hashlck_attr; | |
87 | static lck_grp_t * bind_hashlck_grp; | |
88 | static lck_grp_attr_t * bind_hashlck_grp_attr; | |
89 | static u_long bind_hash_mask; | |
90 | ||
91 | /* xnu doesn't have hashes built into vnodes. This mimics what freebsd does | |
92 | * 9 is an eyeball of the log 2 size of vnode */ | |
93 | static int vnsz2log = 9; | |
94 | ||
95 | static int bind_hashins(struct mount *, struct bind_node *, struct vnode **); | |
96 | ||
97 | int | |
98 | bindfs_init_lck(lck_mtx_t * lck) | |
99 | { | |
100 | int error = 1; | |
101 | if (lck && bind_hashlck_grp && bind_hashlck_attr) { | |
102 | lck_mtx_init(lck, bind_hashlck_grp, bind_hashlck_attr); | |
103 | error = 0; | |
104 | } | |
105 | return error; | |
106 | } | |
107 | ||
108 | int | |
109 | bindfs_destroy_lck(lck_mtx_t * lck) | |
110 | { | |
111 | int error = 1; | |
112 | if (lck && bind_hashlck_grp) { | |
113 | lck_mtx_destroy(lck, bind_hashlck_grp); | |
114 | error = 0; | |
115 | } | |
116 | return error; | |
117 | } | |
118 | ||
119 | /* | |
120 | * Initialise cache headers | |
121 | */ | |
122 | int | |
123 | bindfs_init(__unused struct vfsconf * vfsp) | |
124 | { | |
125 | BINDFSDEBUG("%s\n", __FUNCTION__); | |
126 | ||
127 | /* assuming for now that this happens immediately and by default after fs | |
128 | * installation */ | |
129 | bind_hashlck_grp_attr = lck_grp_attr_alloc_init(); | |
130 | if (bind_hashlck_grp_attr == NULL) { | |
131 | goto error; | |
132 | } | |
133 | bind_hashlck_grp = lck_grp_alloc_init("com.apple.filesystems.bindfs", bind_hashlck_grp_attr); | |
134 | if (bind_hashlck_grp == NULL) { | |
135 | goto error; | |
136 | } | |
137 | bind_hashlck_attr = lck_attr_alloc_init(); | |
138 | if (bind_hashlck_attr == NULL) { | |
139 | goto error; | |
140 | } | |
141 | ||
142 | bind_node_hashtbl = hashinit(BIND_HASH_SIZE, M_TEMP, &bind_hash_mask); | |
143 | if (bind_node_hashtbl == NULL) { | |
144 | goto error; | |
145 | } | |
146 | lck_mtx_init(&bind_hashmtx, bind_hashlck_grp, bind_hashlck_attr); | |
147 | ||
148 | BINDFSDEBUG("%s finished\n", __FUNCTION__); | |
149 | return 0; | |
150 | error: | |
151 | printf("BINDFS: failed to initialize globals\n"); | |
152 | if (bind_hashlck_grp_attr) { | |
153 | lck_grp_attr_free(bind_hashlck_grp_attr); | |
154 | bind_hashlck_grp_attr = NULL; | |
155 | } | |
156 | if (bind_hashlck_grp) { | |
157 | lck_grp_free(bind_hashlck_grp); | |
158 | bind_hashlck_grp = NULL; | |
159 | } | |
160 | if (bind_hashlck_attr) { | |
161 | lck_attr_free(bind_hashlck_attr); | |
162 | bind_hashlck_attr = NULL; | |
163 | } | |
164 | return KERN_FAILURE; | |
165 | } | |
166 | ||
167 | int | |
168 | bindfs_destroy(void) | |
169 | { | |
170 | /* This gets called when the fs is uninstalled, there wasn't an exact | |
171 | * equivalent in vfsops */ | |
172 | lck_mtx_destroy(&bind_hashmtx, bind_hashlck_grp); | |
173 | hashdestroy(bind_node_hashtbl, M_TEMP, bind_hash_mask); | |
174 | if (bind_hashlck_grp_attr) { | |
175 | lck_grp_attr_free(bind_hashlck_grp_attr); | |
176 | bind_hashlck_grp_attr = NULL; | |
177 | } | |
178 | if (bind_hashlck_grp) { | |
179 | lck_grp_free(bind_hashlck_grp); | |
180 | bind_hashlck_grp = NULL; | |
181 | } | |
182 | if (bind_hashlck_attr) { | |
183 | lck_attr_free(bind_hashlck_attr); | |
184 | bind_hashlck_attr = NULL; | |
185 | } | |
186 | return 0; | |
187 | } | |
188 | ||
189 | /* | |
190 | * Find the bindfs vnode mapped to lowervp. Return it in *vpp with an iocount if found. | |
191 | * Return 0 on success. On failure *vpp will be NULL and a non-zero error code will be returned. | |
192 | */ | |
193 | int | |
194 | bind_hashget(struct mount * mp, struct vnode * lowervp, struct vnode ** vpp) | |
195 | { | |
196 | struct bind_node_hashhead * hd; | |
197 | struct bind_node * a; | |
198 | struct vnode * vp = NULL; | |
2a1bd2d3 | 199 | uint32_t vp_vid = 0; |
f427ee49 A |
200 | int error = ENOENT; |
201 | ||
202 | /* | |
203 | * Find hash base, and then search the (two-way) linked | |
204 | * list looking for a bind_node structure which is referencing | |
205 | * the lower vnode. We only give up our reference at reclaim so | |
206 | * just check whether the lowervp has gotten pulled from under us | |
207 | */ | |
208 | hd = BIND_NHASH(lowervp); | |
209 | lck_mtx_lock(&bind_hashmtx); | |
210 | LIST_FOREACH(a, hd, bind_hash) | |
211 | { | |
212 | if (a->bind_lowervp == lowervp && vnode_mount(BINDTOV(a)) == mp) { | |
213 | vp = BINDTOV(a); | |
214 | if (a->bind_lowervid != vnode_vid(lowervp)) { | |
215 | /*lowervp has reved */ | |
216 | error = EIO; | |
217 | vp = NULL; | |
2a1bd2d3 A |
218 | } else { |
219 | vp_vid = a->bind_myvid; | |
f427ee49 A |
220 | } |
221 | break; | |
222 | } | |
223 | } | |
224 | lck_mtx_unlock(&bind_hashmtx); | |
225 | ||
226 | if (vp != NULL) { | |
2a1bd2d3 | 227 | error = vnode_getwithvid(vp, vp_vid); |
f427ee49 A |
228 | if (error == 0) { |
229 | *vpp = vp; | |
230 | } | |
231 | } | |
232 | return error; | |
233 | } | |
234 | ||
235 | /* | |
236 | * Act like bind_hashget, but add passed bind_node to hash if no existing | |
237 | * node found. | |
238 | * If we find a vnode in the hash table it is returned via vpp. If we don't | |
239 | * find a hit in the table, then vpp is NULL on return and xp is added to the table. | |
240 | * 0 is returned if a hash table hit occurs or if we insert the bind_node. | |
241 | * EIO is returned if we found a hash table hit but the lower vnode was recycled. | |
242 | */ | |
243 | static int | |
244 | bind_hashins(struct mount * mp, struct bind_node * xp, struct vnode ** vpp) | |
245 | { | |
246 | struct bind_node_hashhead * hd; | |
247 | struct bind_node * oxp; | |
248 | struct vnode * ovp = NULL; | |
2a1bd2d3 | 249 | uint32_t oxp_vid = 0; |
f427ee49 A |
250 | int error = 0; |
251 | ||
252 | hd = BIND_NHASH(xp->bind_lowervp); | |
253 | lck_mtx_lock(&bind_hashmtx); | |
254 | LIST_FOREACH(oxp, hd, bind_hash) | |
255 | { | |
256 | if (oxp->bind_lowervp == xp->bind_lowervp && vnode_mount(BINDTOV(oxp)) == mp) { | |
257 | ovp = BINDTOV(oxp); | |
258 | if (oxp->bind_lowervid != vnode_vid(oxp->bind_lowervp)) { | |
259 | /* vp doesn't exist so return null (not sure we are actually gonna catch | |
260 | * recycle right now | |
261 | * This is an exceptional case right now, it suggests the vnode we are | |
262 | * trying to add has been recycled | |
263 | * don't add it.*/ | |
264 | error = EIO; | |
265 | ovp = NULL; | |
2a1bd2d3 A |
266 | } else { |
267 | oxp_vid = oxp->bind_myvid; | |
f427ee49 A |
268 | } |
269 | goto end; | |
270 | } | |
271 | } | |
272 | /* if it wasn't in the hash map then the vnode pointed to by xp already has a | |
273 | * iocount so don't get another. */ | |
274 | LIST_INSERT_HEAD(hd, xp, bind_hash); | |
275 | xp->bind_flags |= BIND_FLAG_HASHED; | |
276 | end: | |
277 | lck_mtx_unlock(&bind_hashmtx); | |
278 | if (ovp != NULL) { | |
279 | /* if we found something in the hash map then grab an iocount */ | |
2a1bd2d3 | 280 | error = vnode_getwithvid(ovp, oxp_vid); |
f427ee49 A |
281 | if (error == 0) { |
282 | *vpp = ovp; | |
283 | } | |
284 | } | |
285 | return error; | |
286 | } | |
287 | ||
288 | /* | |
289 | * Remove node from hash. | |
290 | */ | |
291 | void | |
292 | bind_hashrem(struct bind_node * xp) | |
293 | { | |
294 | if (xp->bind_flags & BIND_FLAG_HASHED) { | |
295 | lck_mtx_lock(&bind_hashmtx); | |
296 | LIST_REMOVE(xp, bind_hash); | |
297 | lck_mtx_unlock(&bind_hashmtx); | |
298 | } | |
299 | } | |
300 | ||
301 | static struct bind_node * | |
302 | bind_nodecreate(struct vnode * lowervp) | |
303 | { | |
304 | struct bind_node * xp; | |
305 | ||
306 | MALLOC(xp, struct bind_node *, sizeof(struct bind_node), M_TEMP, M_WAITOK | M_ZERO); | |
307 | if (xp != NULL) { | |
308 | if (lowervp) { | |
309 | xp->bind_lowervp = lowervp; | |
310 | xp->bind_lowervid = vnode_vid(lowervp); | |
311 | } | |
312 | } | |
313 | return xp; | |
314 | } | |
315 | ||
316 | /* assumption is that vnode has iocount on it after vnode create */ | |
317 | int | |
318 | bind_getnewvnode( | |
319 | struct mount * mp, struct vnode * lowervp, struct vnode * dvp, struct vnode ** vpp, struct componentname * cnp, int root) | |
320 | { | |
321 | struct vnode_fsparam vnfs_param; | |
322 | int error = 0; | |
323 | enum vtype type = VDIR; | |
324 | struct bind_node * xp = bind_nodecreate(lowervp); | |
325 | ||
326 | if (xp == NULL) { | |
327 | return ENOMEM; | |
328 | } | |
329 | ||
330 | if (lowervp) { | |
331 | type = vnode_vtype(lowervp); | |
332 | } | |
333 | ||
334 | vnfs_param.vnfs_mp = mp; | |
335 | vnfs_param.vnfs_vtype = type; | |
336 | vnfs_param.vnfs_str = "bindfs"; | |
337 | vnfs_param.vnfs_dvp = dvp; | |
338 | vnfs_param.vnfs_fsnode = (void *)xp; | |
339 | vnfs_param.vnfs_vops = bindfs_vnodeop_p; | |
340 | vnfs_param.vnfs_markroot = root; | |
341 | vnfs_param.vnfs_marksystem = 0; | |
342 | vnfs_param.vnfs_rdev = 0; | |
343 | vnfs_param.vnfs_filesize = 0; // set this to 0 since we should only be shadowing non-regular files | |
344 | vnfs_param.vnfs_cnp = cnp; | |
345 | vnfs_param.vnfs_flags = VNFS_ADDFSREF; | |
346 | ||
347 | error = vnode_create(VNCREATE_FLAVOR, VCREATESIZE, &vnfs_param, vpp); | |
348 | if (error == 0) { | |
349 | xp->bind_vnode = *vpp; | |
350 | xp->bind_myvid = vnode_vid(*vpp); | |
351 | vnode_settag(*vpp, VT_BINDFS); | |
352 | } else { | |
353 | FREE(xp, M_TEMP); | |
354 | } | |
355 | return error; | |
356 | } | |
357 | ||
358 | /* | |
359 | * Make a new or get existing bindfs node. | |
360 | * Vp is the alias vnode, lowervp is the lower vnode. | |
361 | * | |
362 | * lowervp is assumed to have an iocount on it from the caller | |
363 | */ | |
364 | int | |
365 | bind_nodeget( | |
366 | struct mount * mp, struct vnode * lowervp, struct vnode * dvp, struct vnode ** vpp, struct componentname * cnp, int root) | |
367 | { | |
368 | struct vnode * vp; | |
369 | int error; | |
370 | ||
371 | /* Lookup the hash firstly. */ | |
372 | error = bind_hashget(mp, lowervp, vpp); | |
373 | /* ENOENT means it wasn't found, EIO is a failure we should bail from, 0 is it | |
374 | * was found */ | |
375 | if (error != ENOENT) { | |
376 | /* bind_hashget checked the vid, so if we got something here its legit to | |
377 | * the best of our knowledge*/ | |
378 | /* if we found something then there is an iocount on vpp, | |
379 | * if we didn't find something then vpp shouldn't be used by the caller */ | |
380 | return error; | |
381 | } | |
382 | ||
383 | /* | |
384 | * We do not serialize vnode creation, instead we will check for | |
385 | * duplicates later, when adding new vnode to hash. | |
386 | */ | |
387 | error = vnode_ref(lowervp); // take a ref on lowervp so we let the system know we care about it | |
388 | if (error) { | |
389 | // Failed to get a reference on the lower vp so bail. Lowervp may be gone already. | |
390 | return error; | |
391 | } | |
392 | ||
393 | error = bind_getnewvnode(mp, lowervp, dvp, &vp, cnp, root); | |
394 | ||
395 | if (error) { | |
396 | vnode_rele(lowervp); | |
397 | return error; | |
398 | } | |
399 | ||
400 | /* | |
401 | * Atomically insert our new node into the hash or vget existing | |
402 | * if someone else has beaten us to it. | |
403 | */ | |
404 | error = bind_hashins(mp, VTOBIND(vp), vpp); | |
405 | if (error || *vpp != NULL) { | |
406 | /* recycle will call reclaim which will get rid of the internals */ | |
407 | vnode_recycle(vp); | |
408 | vnode_put(vp); | |
409 | /* if we found vpp, then bind_hashins put an iocount on it */ | |
410 | return error; | |
411 | } | |
412 | ||
413 | /* vp has an iocount from bind_getnewvnode */ | |
414 | *vpp = vp; | |
415 | ||
416 | return 0; | |
417 | } |