xref: /xnu-8020.121.3/bsd/miscfs/bindfs/bind_subr.c (revision fdd8201d7b966f0c3ea610489d29bd841d358941)
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_GRP_DECLARE(bind_hashlck_grp, "com.apple.filesystems.bindfs");
86 static LCK_MTX_DECLARE(bind_hashmtx, &bind_hashlck_grp);
87 static u_long bind_hash_mask;
88 
89 /*  xnu doesn't have hashes built into vnodes. This mimics what freebsd does
90  *  9 is an eyeball of the log 2 size of vnode */
91 static int vnsz2log = 9;
92 
93 static int bind_hashins(struct mount *, struct bind_node *, struct vnode **);
94 
95 /*
96  * Initialise cache headers
97  */
98 int
bindfs_init(__unused struct vfsconf * vfsp)99 bindfs_init(__unused struct vfsconf * vfsp)
100 {
101 	BINDFSDEBUG("%s\n", __FUNCTION__);
102 
103 	bind_node_hashtbl = hashinit(BIND_HASH_SIZE, M_TEMP, &bind_hash_mask);
104 	if (bind_node_hashtbl == NULL) {
105 		goto error;
106 	}
107 
108 	BINDFSDEBUG("%s finished\n", __FUNCTION__);
109 	return 0;
110 error:
111 	printf("BINDFS: failed to initialize globals\n");
112 	return KERN_FAILURE;
113 }
114 
115 int
bindfs_destroy(void)116 bindfs_destroy(void)
117 {
118 	/* This gets called when the fs is uninstalled, there wasn't an exact
119 	 * equivalent in vfsops */
120 	hashdestroy(bind_node_hashtbl, M_TEMP, bind_hash_mask);
121 	return 0;
122 }
123 
124 /*
125  * Find the bindfs vnode mapped to lowervp. Return it in *vpp with an iocount if found.
126  * Return 0 on success. On failure *vpp will be NULL and a non-zero error code will be returned.
127  */
128 int
bind_hashget(struct mount * mp,struct vnode * lowervp,struct vnode ** vpp)129 bind_hashget(struct mount * mp, struct vnode * lowervp, struct vnode ** vpp)
130 {
131 	struct bind_node_hashhead * hd;
132 	struct bind_node * a;
133 	struct vnode * vp = NULL;
134 	uint32_t vp_vid = 0;
135 	int error = ENOENT;
136 
137 	/*
138 	 * Find hash base, and then search the (two-way) linked
139 	 * list looking for a bind_node structure which is referencing
140 	 * the lower vnode. We only give up our reference at reclaim so
141 	 * just check whether the lowervp has gotten pulled from under us
142 	 */
143 	hd = BIND_NHASH(lowervp);
144 	lck_mtx_lock(&bind_hashmtx);
145 	LIST_FOREACH(a, hd, bind_hash)
146 	{
147 		if (a->bind_lowervp == lowervp && vnode_mount(BINDTOV(a)) == mp) {
148 			vp = BINDTOV(a);
149 			if (a->bind_lowervid != vnode_vid(lowervp)) {
150 				/*lowervp has reved */
151 				error = EIO;
152 				vp = NULL;
153 			} else {
154 				vp_vid = a->bind_myvid;
155 			}
156 			break;
157 		}
158 	}
159 	lck_mtx_unlock(&bind_hashmtx);
160 
161 	if (vp != NULL) {
162 		error = vnode_getwithvid(vp, vp_vid);
163 		if (error == 0) {
164 			*vpp = vp;
165 		}
166 	}
167 	return error;
168 }
169 
170 /*
171  * Act like bind_hashget, but add passed bind_node to hash if no existing
172  * node found.
173  * If we find a vnode in the hash table it is returned via vpp. If we don't
174  * find a hit in the table, then vpp is NULL on return and xp is added to the table.
175  * 0 is returned if a hash table hit occurs or if we insert the bind_node.
176  * EIO is returned if we found a hash table hit but the lower vnode was recycled.
177  */
178 static int
bind_hashins(struct mount * mp,struct bind_node * xp,struct vnode ** vpp)179 bind_hashins(struct mount * mp, struct bind_node * xp, struct vnode ** vpp)
180 {
181 	struct bind_node_hashhead * hd;
182 	struct bind_node * oxp;
183 	struct vnode * ovp = NULL;
184 	uint32_t oxp_vid = 0;
185 	int error = 0;
186 
187 	hd = BIND_NHASH(xp->bind_lowervp);
188 	lck_mtx_lock(&bind_hashmtx);
189 	LIST_FOREACH(oxp, hd, bind_hash)
190 	{
191 		if (oxp->bind_lowervp == xp->bind_lowervp && vnode_mount(BINDTOV(oxp)) == mp) {
192 			ovp = BINDTOV(oxp);
193 			if (oxp->bind_lowervid != vnode_vid(oxp->bind_lowervp)) {
194 				/*	vp doesn't exist so return null (not sure we are actually gonna catch
195 				 *  recycle right now
196 				 *  This is an exceptional case right now, it suggests the vnode we are
197 				 *  trying to add has been recycled
198 				 *  don't add it.*/
199 				error = EIO;
200 				ovp = NULL;
201 			} else {
202 				oxp_vid = oxp->bind_myvid;
203 			}
204 			goto end;
205 		}
206 	}
207 	/* if it wasn't in the hash map then the vnode pointed to by xp already has a
208 	 * iocount so don't get another. */
209 	LIST_INSERT_HEAD(hd, xp, bind_hash);
210 	xp->bind_flags |= BIND_FLAG_HASHED;
211 end:
212 	lck_mtx_unlock(&bind_hashmtx);
213 	if (ovp != NULL) {
214 		/* if we found something in the hash map then grab an iocount */
215 		error = vnode_getwithvid(ovp, oxp_vid);
216 		if (error == 0) {
217 			*vpp = ovp;
218 		}
219 	}
220 	return error;
221 }
222 
223 /*
224  * Remove node from hash.
225  */
226 void
bind_hashrem(struct bind_node * xp)227 bind_hashrem(struct bind_node * xp)
228 {
229 	if (xp->bind_flags & BIND_FLAG_HASHED) {
230 		lck_mtx_lock(&bind_hashmtx);
231 		LIST_REMOVE(xp, bind_hash);
232 		lck_mtx_unlock(&bind_hashmtx);
233 	}
234 }
235 
236 static struct bind_node *
bind_nodecreate(struct vnode * lowervp)237 bind_nodecreate(struct vnode * lowervp)
238 {
239 	struct bind_node * xp;
240 
241 	xp = kalloc_type(struct bind_node, Z_WAITOK | Z_ZERO | Z_NOFAIL);
242 	if (lowervp) {
243 		xp->bind_lowervp  = lowervp;
244 		xp->bind_lowervid = vnode_vid(lowervp);
245 	}
246 	return xp;
247 }
248 
249 /* assumption is that vnode has iocount on it after vnode create */
250 int
bind_getnewvnode(struct mount * mp,struct vnode * lowervp,struct vnode * dvp,struct vnode ** vpp,struct componentname * cnp,int root)251 bind_getnewvnode(
252 	struct mount * mp, struct vnode * lowervp, struct vnode * dvp, struct vnode ** vpp, struct componentname * cnp, int root)
253 {
254 	struct vnode_fsparam vnfs_param;
255 	int error             = 0;
256 	enum vtype type       = VDIR;
257 	struct bind_node * xp = bind_nodecreate(lowervp);
258 
259 	if (xp == NULL) {
260 		return ENOMEM;
261 	}
262 
263 	if (lowervp) {
264 		type = vnode_vtype(lowervp);
265 	}
266 
267 	vnfs_param.vnfs_mp         = mp;
268 	vnfs_param.vnfs_vtype      = type;
269 	vnfs_param.vnfs_str        = "bindfs";
270 	vnfs_param.vnfs_dvp        = dvp;
271 	vnfs_param.vnfs_fsnode     = (void *)xp;
272 	vnfs_param.vnfs_vops       = bindfs_vnodeop_p;
273 	vnfs_param.vnfs_markroot   = root;
274 	vnfs_param.vnfs_marksystem = 0;
275 	vnfs_param.vnfs_rdev       = 0;
276 	vnfs_param.vnfs_filesize   = 0; // set this to 0 since we should only be shadowing non-regular files
277 	vnfs_param.vnfs_cnp        = cnp;
278 	vnfs_param.vnfs_flags      = VNFS_ADDFSREF;
279 
280 	error = vnode_create(VNCREATE_FLAVOR, VCREATESIZE, &vnfs_param, vpp);
281 	if (error == 0) {
282 		xp->bind_vnode = *vpp;
283 		xp->bind_myvid = vnode_vid(*vpp);
284 		vnode_settag(*vpp, VT_BINDFS);
285 	} else {
286 		kfree_type(struct bind_node, xp);
287 	}
288 	return error;
289 }
290 
291 /*
292  * Make a new or get existing bindfs node.
293  * Vp is the alias vnode, lowervp is the lower vnode.
294  *
295  * lowervp is assumed to have an iocount on it from the caller
296  */
297 int
bind_nodeget(struct mount * mp,struct vnode * lowervp,struct vnode * dvp,struct vnode ** vpp,struct componentname * cnp,int root)298 bind_nodeget(
299 	struct mount * mp, struct vnode * lowervp, struct vnode * dvp, struct vnode ** vpp, struct componentname * cnp, int root)
300 {
301 	struct vnode * vp;
302 	int error;
303 
304 	/* Lookup the hash firstly. */
305 	error = bind_hashget(mp, lowervp, vpp);
306 	/* ENOENT means it wasn't found, EIO is a failure we should bail from, 0 is it
307 	 * was found */
308 	if (error != ENOENT) {
309 		/* bind_hashget checked the vid, so if we got something here its legit to
310 		 * the best of our knowledge*/
311 		/* if we found something then there is an iocount on vpp,
312 		 *  if we didn't find something then vpp shouldn't be used by the caller */
313 		return error;
314 	}
315 
316 	/*
317 	 * We do not serialize vnode creation, instead we will check for
318 	 * duplicates later, when adding new vnode to hash.
319 	 */
320 	error = vnode_ref(lowervp); // take a ref on lowervp so we let the system know we care about it
321 	if (error) {
322 		// Failed to get a reference on the lower vp so bail. Lowervp may be gone already.
323 		return error;
324 	}
325 
326 	error = bind_getnewvnode(mp, lowervp, dvp, &vp, cnp, root);
327 
328 	if (error) {
329 		vnode_rele(lowervp);
330 		return error;
331 	}
332 
333 	/*
334 	 * Atomically insert our new node into the hash or vget existing
335 	 * if someone else has beaten us to it.
336 	 */
337 	error = bind_hashins(mp, VTOBIND(vp), vpp);
338 	if (error || *vpp != NULL) {
339 		/* recycle will call reclaim which will get rid of the internals */
340 		vnode_recycle(vp);
341 		vnode_put(vp);
342 		/* if we found vpp, then bind_hashins put an iocount on it */
343 		return error;
344 	}
345 
346 	/* vp has an iocount from bind_getnewvnode */
347 	*vpp = vp;
348 
349 	return 0;
350 }
351