]> git.saurik.com Git - apple/xnu.git/blob - bsd/miscfs/nullfs/null_subr.c
xnu-7195.60.75.tar.gz
[apple/xnu.git] / bsd / miscfs / nullfs / null_subr.c
1 /*
2 * Copyright (c) 2016 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 "nullfs.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 NULL_HASH_SIZE (desiredvnodes / 10)
79
80 /* osx doesn't really have the functionality freebsd uses here..gonna try this
81 * hacked hash...*/
82 #define NULL_NHASH(vp) (&null_node_hashtbl[((((uintptr_t)vp) >> vnsz2log) + (uintptr_t)vnode_mount(vp)) & null_hash_mask])
83
84 static LIST_HEAD(null_node_hashhead, null_node) * null_node_hashtbl;
85 static lck_mtx_t null_hashmtx;
86 static lck_attr_t * null_hashlck_attr;
87 static lck_grp_t * null_hashlck_grp;
88 static lck_grp_attr_t * null_hashlck_grp_attr;
89 static u_long null_hash_mask;
90
91 /* os x doesn't have hashes built into vnode. gonna try doing what freebsd does
92 * anyway
93 * Don't want to create a dependency on vnode_internal.h and the real struct
94 * vnode.
95 * 9 is an eyeball of the log 2 size of vnode */
96 static int vnsz2log = 9;
97
98 static int null_hashins(struct mount *, struct null_node *, struct vnode **);
99
100 int
101 nullfs_init_lck(lck_mtx_t * lck)
102 {
103 int error = 1;
104 if (lck && null_hashlck_grp && null_hashlck_attr) {
105 lck_mtx_init(lck, null_hashlck_grp, null_hashlck_attr);
106 error = 0;
107 }
108 return error;
109 }
110
111 int
112 nullfs_destroy_lck(lck_mtx_t * lck)
113 {
114 int error = 1;
115 if (lck && null_hashlck_grp) {
116 lck_mtx_destroy(lck, null_hashlck_grp);
117 error = 0;
118 }
119 return error;
120 }
121
122 /*
123 * Initialise cache headers
124 */
125 int
126 nullfs_init(__unused struct vfsconf * vfsp)
127 {
128 NULLFSDEBUG("%s\n", __FUNCTION__);
129
130 /* assuming for now that this happens immediately and by default after fs
131 * installation */
132 null_hashlck_grp_attr = lck_grp_attr_alloc_init();
133 if (null_hashlck_grp_attr == NULL) {
134 goto error;
135 }
136 null_hashlck_grp = lck_grp_alloc_init("com.apple.filesystems.nullfs", null_hashlck_grp_attr);
137 if (null_hashlck_grp == NULL) {
138 goto error;
139 }
140 null_hashlck_attr = lck_attr_alloc_init();
141 if (null_hashlck_attr == NULL) {
142 goto error;
143 }
144
145 lck_mtx_init(&null_hashmtx, null_hashlck_grp, null_hashlck_attr);
146 null_node_hashtbl = hashinit(NULL_HASH_SIZE, M_TEMP, &null_hash_mask);
147 NULLFSDEBUG("%s finished\n", __FUNCTION__);
148 return 0;
149 error:
150 printf("NULLFS: failed to get lock element\n");
151 if (null_hashlck_grp_attr) {
152 lck_grp_attr_free(null_hashlck_grp_attr);
153 null_hashlck_grp_attr = NULL;
154 }
155 if (null_hashlck_grp) {
156 lck_grp_free(null_hashlck_grp);
157 null_hashlck_grp = NULL;
158 }
159 if (null_hashlck_attr) {
160 lck_attr_free(null_hashlck_attr);
161 null_hashlck_attr = NULL;
162 }
163 return KERN_FAILURE;
164 }
165
166 int
167 nullfs_uninit()
168 {
169 /* This gets called when the fs is uninstalled, there wasn't an exact
170 * equivalent in vfsops */
171 lck_mtx_destroy(&null_hashmtx, null_hashlck_grp);
172 hashdestroy(null_node_hashtbl, M_TEMP, null_hash_mask);
173 if (null_hashlck_grp_attr) {
174 lck_grp_attr_free(null_hashlck_grp_attr);
175 null_hashlck_grp_attr = NULL;
176 }
177 if (null_hashlck_grp) {
178 lck_grp_free(null_hashlck_grp);
179 null_hashlck_grp = NULL;
180 }
181 if (null_hashlck_attr) {
182 lck_attr_free(null_hashlck_attr);
183 null_hashlck_attr = NULL;
184 }
185 return 0;
186 }
187
188 /*
189 * Find the nullfs vnode mapped to lowervp. Return it in *vpp with an iocount if found.
190 * Return 0 on success. On failure *vpp will be null and a non-zero error code will be returned.
191 */
192 int
193 null_hashget(struct mount * mp, struct vnode * lowervp, struct vnode ** vpp)
194 {
195 struct null_node_hashhead * hd = NULL;
196 struct null_node * a = NULL;
197 struct vnode * vp = NULL;
198 uint32_t vp_vid = 0;
199 int error = ENOENT;
200
201 /*
202 * Find hash base, and then search the (two-way) linked
203 * list looking for a null_node structure which is referencing
204 * the lower vnode. We only give up our reference at reclaim so
205 * just check whether the lowervp has gotten pulled from under us
206 */
207 hd = NULL_NHASH(lowervp);
208 // In the future we should consider using a per bucket lock
209 lck_mtx_lock(&null_hashmtx);
210 LIST_FOREACH(a, hd, null_hash)
211 {
212 if (a->null_lowervp == lowervp && vnode_mount(NULLTOV(a)) == mp) {
213 vp = NULLTOV(a);
214 if (a->null_lowervid != vnode_vid(lowervp)) {
215 /*lowervp has reved */
216 error = EIO;
217 vp = NULL;
218 } else {
219 vp_vid = a->null_myvid;
220 }
221 // In the case of a succesful look-up we should consider moving the object to the top of the head
222 break;
223 }
224 }
225 lck_mtx_unlock(&null_hashmtx);
226 if (vp != NULL) {
227 error = vnode_getwithvid(vp, vp_vid);
228 if (error == 0) {
229 *vpp = vp;
230 }
231 }
232 return error;
233 }
234
235 /*
236 * Act like null_hashget, but add passed null_node to hash if no existing
237 * node found.
238 */
239 static int
240 null_hashins(struct mount * mp, struct null_node * xp, struct vnode ** vpp)
241 {
242 struct null_node_hashhead * hd = NULL;
243 struct null_node * oxp = NULL;
244 struct vnode * ovp = NULL;
245 uint32_t oxp_vid = 0;
246 int error = 0;
247
248 hd = NULL_NHASH(xp->null_lowervp);
249 lck_mtx_lock(&null_hashmtx);
250 LIST_FOREACH(oxp, hd, null_hash)
251 {
252 if (oxp->null_lowervp == xp->null_lowervp && vnode_mount(NULLTOV(oxp)) == mp) {
253 /*
254 * See null_hashget for a description of this
255 * operation.
256 */
257 ovp = NULLTOV(oxp);
258 if (oxp->null_lowervid != vnode_vid(oxp->null_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;
266 } else {
267 oxp_vid = oxp->null_myvid;
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 bother */
274 LIST_INSERT_HEAD(hd, xp, null_hash);
275 xp->null_flags |= NULL_FLAG_HASHED;
276 end:
277 lck_mtx_unlock(&null_hashmtx);
278 if (ovp != NULL) {
279 /* if we found something in the hash map then grab an iocount */
280 error = vnode_getwithvid(ovp, oxp_vid);
281 if (error == 0) {
282 *vpp = ovp;
283 }
284 }
285 return error;
286 }
287
288 /*
289 * Remove node from hash.
290 */
291 void
292 null_hashrem(struct null_node * xp)
293 {
294 lck_mtx_lock(&null_hashmtx);
295 LIST_REMOVE(xp, null_hash);
296 lck_mtx_unlock(&null_hashmtx);
297 }
298
299 static struct null_node *
300 null_nodecreate(struct vnode * lowervp)
301 {
302 struct null_node * xp;
303
304 MALLOC(xp, struct null_node *, sizeof(struct null_node), M_TEMP, M_WAITOK | M_ZERO);
305 if (xp != NULL) {
306 if (lowervp) {
307 xp->null_lowervp = lowervp;
308 xp->null_lowervid = vnode_vid(lowervp);
309 }
310 }
311 return xp;
312 }
313
314 /* assumption is that vnode has iocount on it after vnode create */
315 int
316 null_getnewvnode(
317 struct mount * mp, struct vnode * lowervp, struct vnode * dvp, struct vnode ** vpp, struct componentname * cnp, int root)
318 {
319 struct vnode_fsparam vnfs_param;
320 int error = 0;
321 enum vtype type = VDIR;
322 struct null_node * xp = null_nodecreate(lowervp);
323
324 if (xp == NULL) {
325 return ENOMEM;
326 }
327
328 if (lowervp) {
329 type = vnode_vtype(lowervp);
330 }
331
332 vnfs_param.vnfs_mp = mp;
333 vnfs_param.vnfs_vtype = type;
334 vnfs_param.vnfs_str = "nullfs";
335 vnfs_param.vnfs_dvp = dvp;
336 vnfs_param.vnfs_fsnode = (void *)xp;
337 vnfs_param.vnfs_vops = nullfs_vnodeop_p;
338 vnfs_param.vnfs_markroot = root;
339 vnfs_param.vnfs_marksystem = 0;
340 vnfs_param.vnfs_rdev = 0;
341 vnfs_param.vnfs_filesize = 0; // set this to 0 since we should only be shadowing non-regular files
342 vnfs_param.vnfs_cnp = cnp;
343 vnfs_param.vnfs_flags = VNFS_ADDFSREF;
344
345 error = vnode_create(VNCREATE_FLAVOR, VCREATESIZE, &vnfs_param, vpp);
346 if (error == 0) {
347 xp->null_vnode = *vpp;
348 xp->null_myvid = vnode_vid(*vpp);
349 vnode_settag(*vpp, VT_NULL);
350 } else {
351 FREE(xp, M_TEMP);
352 }
353 return error;
354 }
355
356 /*
357 * Make a new or get existing nullfs node.
358 * Vp is the alias vnode, lowervp is the lower vnode.
359 *
360 * lowervp is assumed to have an iocount on it from the caller
361 */
362 int
363 null_nodeget(
364 struct mount * mp, struct vnode * lowervp, struct vnode * dvp, struct vnode ** vpp, struct componentname * cnp, int root)
365 {
366 struct vnode * vp;
367 int error;
368
369 /* Lookup the hash firstly. */
370 error = null_hashget(mp, lowervp, vpp);
371 /* ENOENT means it wasn't found, EIO is a failure we should bail from, 0 is it
372 * was found */
373 if (error != ENOENT) {
374 /* null_hashget checked the vid, so if we got something here its legit to
375 * the best of our knowledge*/
376 /* if we found something then there is an iocount on vpp,
377 * if we didn't find something then vpp shouldn't be used by the caller */
378 return error;
379 }
380
381 /*
382 * We do not serialize vnode creation, instead we will check for
383 * duplicates later, when adding new vnode to hash.
384 */
385 error = vnode_ref(lowervp); // take a ref on lowervp so we let the system know we care about it
386 if (error) {
387 // Failed to get a reference on the lower vp so bail. Lowervp may be gone already.
388 return error;
389 }
390
391 error = null_getnewvnode(mp, lowervp, dvp, &vp, cnp, root);
392
393 if (error) {
394 vnode_rele(lowervp);
395 return error;
396 }
397
398 /*
399 * Atomically insert our new node into the hash or vget existing
400 * if someone else has beaten us to it.
401 */
402 error = null_hashins(mp, VTONULL(vp), vpp);
403 if (error || *vpp != NULL) {
404 /* recycle will call reclaim which will get rid of the internals */
405 vnode_recycle(vp);
406 vnode_put(vp);
407 /* if we found vpp, then null_hashins put an iocount on it */
408 return error;
409 }
410
411 /* vp has an iocount from null_getnewvnode */
412 *vpp = vp;
413
414 return 0;
415 }