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