| 1 | /* |
| 2 | * Copyright (c) 2000-2010 Apple Inc. All rights reserved. |
| 3 | * |
| 4 | * @APPLE_OSREFERENCE_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. The rights granted to you under the License |
| 10 | * may not be used to create, or enable the creation or redistribution of, |
| 11 | * unlawful or unlicensed copies of an Apple operating system, or to |
| 12 | * circumvent, violate, or enable the circumvention or violation of, any |
| 13 | * terms of an Apple operating system software license agreement. |
| 14 | * |
| 15 | * Please obtain a copy of the License at |
| 16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. |
| 17 | * |
| 18 | * The Original Code and all software distributed under the License are |
| 19 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER |
| 20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
| 21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, |
| 22 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. |
| 23 | * Please see the License for the specific language governing rights and |
| 24 | * limitations under the License. |
| 25 | * |
| 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
| 27 | */ |
| 28 | /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */ |
| 29 | /* |
| 30 | * Copyright (c) 1989, 1993 |
| 31 | * The Regents of the University of California. All rights reserved. |
| 32 | * |
| 33 | * This code is derived from software contributed |
| 34 | * to Berkeley by John Heidemann of the UCLA Ficus project. |
| 35 | * |
| 36 | * Source: * @(#)i405_init.c 2.10 92/04/27 UCLA Ficus project |
| 37 | * |
| 38 | * Redistribution and use in source and binary forms, with or without |
| 39 | * modification, are permitted provided that the following conditions |
| 40 | * are met: |
| 41 | * 1. Redistributions of source code must retain the above copyright |
| 42 | * notice, this list of conditions and the following disclaimer. |
| 43 | * 2. Redistributions in binary form must reproduce the above copyright |
| 44 | * notice, this list of conditions and the following disclaimer in the |
| 45 | * documentation and/or other materials provided with the distribution. |
| 46 | * 3. All advertising materials mentioning features or use of this software |
| 47 | * must display the following acknowledgement: |
| 48 | * This product includes software developed by the University of |
| 49 | * California, Berkeley and its contributors. |
| 50 | * 4. Neither the name of the University nor the names of its contributors |
| 51 | * may be used to endorse or promote products derived from this software |
| 52 | * without specific prior written permission. |
| 53 | * |
| 54 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 55 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 56 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 57 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 58 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 59 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 60 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 61 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 62 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 63 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 64 | * SUCH DAMAGE. |
| 65 | * |
| 66 | * @(#)vfs_init.c 8.5 (Berkeley) 5/11/95 |
| 67 | */ |
| 68 | /* |
| 69 | * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce |
| 70 | * support for mandatory and extensible security protections. This notice |
| 71 | * is included in support of clause 2.2 (b) of the Apple Public License, |
| 72 | * Version 2.0. |
| 73 | */ |
| 74 | |
| 75 | |
| 76 | #include <sys/param.h> |
| 77 | #include <sys/mount_internal.h> |
| 78 | #include <sys/time.h> |
| 79 | #include <sys/vm.h> |
| 80 | #include <sys/vnode_internal.h> |
| 81 | #include <sys/stat.h> |
| 82 | #include <sys/namei.h> |
| 83 | #include <sys/ucred.h> |
| 84 | #include <sys/errno.h> |
| 85 | #include <kern/kalloc.h> |
| 86 | #include <kern/smr.h> |
| 87 | #include <sys/decmpfs.h> |
| 88 | |
| 89 | #if CONFIG_MACF |
| 90 | #include <security/mac_framework.h> |
| 91 | #include <sys/kauth.h> |
| 92 | #endif |
| 93 | #if QUOTA |
| 94 | #include <sys/quota.h> |
| 95 | #endif |
| 96 | |
| 97 | #if CONFIG_EXCLAVES |
| 98 | #include <vfs/vfs_exclave_fs.h> |
| 99 | #endif |
| 100 | |
| 101 | /* |
| 102 | * Sigh, such primitive tools are these... |
| 103 | */ |
| 104 | #if 0 |
| 105 | #define DODEBUG(A) A |
| 106 | #else |
| 107 | #define DODEBUG(A) |
| 108 | #endif |
| 109 | |
| 110 | KALLOC_TYPE_DEFINE(mount_zone, struct mount, KT_DEFAULT); |
| 111 | |
| 112 | SMR_DEFINE(_vfs_smr, "VFS" ); |
| 113 | const smr_t vfs_smr = &_vfs_smr; |
| 114 | |
| 115 | __private_extern__ void vntblinit(void); |
| 116 | |
| 117 | extern const struct vnodeopv_desc *vfs_opv_descs[]; |
| 118 | /* a list of lists of vnodeops defns */ |
| 119 | extern struct vnodeop_desc *vfs_op_descs[]; |
| 120 | /* and the operations they perform */ |
| 121 | /* |
| 122 | * This code doesn't work if the defn is **vnodop_defns with cc. |
| 123 | * The problem is because of the compiler sometimes putting in an |
| 124 | * extra level of indirection for arrays. It's an interesting |
| 125 | * "feature" of C. |
| 126 | */ |
| 127 | int vfs_opv_numops; |
| 128 | |
| 129 | typedef int (*PFIvp)(void *); |
| 130 | |
| 131 | /* |
| 132 | * A miscellaneous routine. |
| 133 | * A generic "default" routine that just returns an error. |
| 134 | */ |
| 135 | int |
| 136 | vn_default_error(void) |
| 137 | { |
| 138 | return ENOTSUP; |
| 139 | } |
| 140 | |
| 141 | /* |
| 142 | * vfs_init.c |
| 143 | * |
| 144 | * Allocate and fill in operations vectors. |
| 145 | * |
| 146 | * An undocumented feature of this approach to defining operations is that |
| 147 | * there can be multiple entries in vfs_opv_descs for the same operations |
| 148 | * vector. This allows third parties to extend the set of operations |
| 149 | * supported by another layer in a binary compatibile way. For example, |
| 150 | * assume that NFS needed to be modified to support Ficus. NFS has an entry |
| 151 | * (probably nfs_vnopdeop_decls) declaring all the operations NFS supports by |
| 152 | * default. Ficus could add another entry (ficus_nfs_vnodeop_decl_entensions) |
| 153 | * listing those new operations Ficus adds to NFS, all without modifying the |
| 154 | * NFS code. (Of couse, the OTW NFS protocol still needs to be munged, but |
| 155 | * that is a(whole)nother story.) This is a feature. |
| 156 | */ |
| 157 | void |
| 158 | vfs_opv_init(void) |
| 159 | { |
| 160 | int i, j, k; |
| 161 | int(***opv_desc_vector_p)(void *); |
| 162 | int(**opv_desc_vector)(void *); |
| 163 | const struct vnodeopv_entry_desc *opve_descp; |
| 164 | |
| 165 | /* |
| 166 | * Allocate the dynamic vectors and fill them in. |
| 167 | */ |
| 168 | for (i = 0; vfs_opv_descs[i]; i++) { |
| 169 | opv_desc_vector_p = vfs_opv_descs[i]->opv_desc_vector_p; |
| 170 | /* |
| 171 | * Allocate and init the vector, if it needs it. |
| 172 | * Also handle backwards compatibility. |
| 173 | */ |
| 174 | if (*opv_desc_vector_p == NULL) { |
| 175 | *opv_desc_vector_p = zalloc_permanent(vfs_opv_numops * sizeof(PFIvp), |
| 176 | ZALIGN(PFIvp)); |
| 177 | DODEBUG(printf("vector at %x allocated\n" , |
| 178 | opv_desc_vector_p)); |
| 179 | } |
| 180 | opv_desc_vector = *opv_desc_vector_p; |
| 181 | for (j = 0; vfs_opv_descs[i]->opv_desc_ops[j].opve_op; j++) { |
| 182 | opve_descp = &(vfs_opv_descs[i]->opv_desc_ops[j]); |
| 183 | |
| 184 | /* Silently skip known-disabled operations */ |
| 185 | if (opve_descp->opve_op->vdesc_flags & VDESC_DISABLED) { |
| 186 | printf("vfs_fsadd: Ignoring reference in %p to disabled operation %s.\n" , |
| 187 | vfs_opv_descs[i], opve_descp->opve_op->vdesc_name); |
| 188 | continue; |
| 189 | } |
| 190 | |
| 191 | /* |
| 192 | * Sanity check: is this operation listed |
| 193 | * in the list of operations? We check this |
| 194 | * by seeing if its offest is zero. Since |
| 195 | * the default routine should always be listed |
| 196 | * first, it should be the only one with a zero |
| 197 | * offset. Any other operation with a zero |
| 198 | * offset is probably not listed in |
| 199 | * vfs_op_descs, and so is probably an error. |
| 200 | * |
| 201 | * A panic here means the layer programmer |
| 202 | * has committed the all-too common bug |
| 203 | * of adding a new operation to the layer's |
| 204 | * list of vnode operations but |
| 205 | * not adding the operation to the system-wide |
| 206 | * list of supported operations. |
| 207 | */ |
| 208 | if (opve_descp->opve_op->vdesc_offset == 0 && |
| 209 | opve_descp->opve_op != |
| 210 | VDESC(vnop_default)) { |
| 211 | printf("operation %s not listed in %s.\n" , |
| 212 | opve_descp->opve_op->vdesc_name, |
| 213 | "vfs_op_descs" ); |
| 214 | panic("vfs_opv_init: bad operation" ); |
| 215 | } |
| 216 | /* |
| 217 | * Fill in this entry. |
| 218 | */ |
| 219 | opv_desc_vector[opve_descp->opve_op->vdesc_offset] = |
| 220 | opve_descp->opve_impl; |
| 221 | } |
| 222 | } |
| 223 | /* |
| 224 | * Finally, go back and replace unfilled routines |
| 225 | * with their default. (Sigh, an O(n^3) algorithm. I |
| 226 | * could make it better, but that'd be work, and n is small.) |
| 227 | */ |
| 228 | for (i = 0; vfs_opv_descs[i]; i++) { |
| 229 | opv_desc_vector = *(vfs_opv_descs[i]->opv_desc_vector_p); |
| 230 | /* |
| 231 | * Force every operations vector to have a default routine. |
| 232 | */ |
| 233 | if (opv_desc_vector[VOFFSET(vnop_default)] == NULL) { |
| 234 | panic("vfs_opv_init: operation vector without default routine." ); |
| 235 | } |
| 236 | for (k = 0; k < vfs_opv_numops; k++) { |
| 237 | if (opv_desc_vector[k] == NULL) { |
| 238 | opv_desc_vector[k] = |
| 239 | opv_desc_vector[VOFFSET(vnop_default)]; |
| 240 | } |
| 241 | } |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | /* |
| 246 | * Initialize known vnode operations vectors. |
| 247 | */ |
| 248 | void |
| 249 | vfs_op_init(void) |
| 250 | { |
| 251 | int i; |
| 252 | |
| 253 | DODEBUG(printf("Vnode_interface_init.\n" )); |
| 254 | /* |
| 255 | * Set all vnode vectors to a well known value. |
| 256 | */ |
| 257 | for (i = 0; vfs_opv_descs[i]; i++) { |
| 258 | *(vfs_opv_descs[i]->opv_desc_vector_p) = NULL; |
| 259 | } |
| 260 | /* |
| 261 | * Figure out how many ops there are by counting the table, |
| 262 | * and assign each its offset. |
| 263 | */ |
| 264 | for (vfs_opv_numops = 0, i = 0; vfs_op_descs[i]; i++) { |
| 265 | /* Silently skip known-disabled operations */ |
| 266 | if (vfs_op_descs[i]->vdesc_flags & VDESC_DISABLED) { |
| 267 | continue; |
| 268 | } |
| 269 | vfs_op_descs[i]->vdesc_offset = vfs_opv_numops; |
| 270 | vfs_opv_numops++; |
| 271 | } |
| 272 | DODEBUG(printf("vfs_opv_numops=%d\n" , vfs_opv_numops)); |
| 273 | } |
| 274 | |
| 275 | /* |
| 276 | * Routines having to do with the management of the vnode table. |
| 277 | */ |
| 278 | extern struct vnodeops dead_vnodeops; |
| 279 | extern struct vnodeops spec_vnodeops; |
| 280 | |
| 281 | /* vars for vnode list lock */ |
| 282 | static LCK_GRP_DECLARE(vnode_list_lck_grp, "vnode list" ); |
| 283 | static LCK_ATTR_DECLARE(vnode_list_lck_attr, 0, 0); |
| 284 | static LCK_SPIN_DECLARE_ATTR(vnode_list_spin_lock, |
| 285 | &vnode_list_lck_grp, &vnode_list_lck_attr); |
| 286 | static LCK_MTX_DECLARE_ATTR(spechash_mtx_lock, |
| 287 | &vnode_list_lck_grp, &vnode_list_lck_attr); |
| 288 | LCK_MTX_DECLARE_ATTR(pkg_extensions_lck, |
| 289 | &vnode_list_lck_grp, &vnode_list_lck_attr); |
| 290 | |
| 291 | /* vars for mount lock */ |
| 292 | static LCK_GRP_DECLARE(mnt_lck_grp, "mount" ); |
| 293 | static LCK_ATTR_DECLARE(mnt_lck_attr, 0, 0); |
| 294 | |
| 295 | /* vars for mount list lock */ |
| 296 | static LCK_GRP_DECLARE(mnt_list_lck_grp, "mount list" ); |
| 297 | LCK_MTX_DECLARE(mnt_list_mtx_lock, &mnt_list_lck_grp); |
| 298 | |
| 299 | /* |
| 300 | * We want dead_mountp to be a constant pointer, but vfsinit() runs |
| 301 | * pretty late, so we'll allocate the dead_mount statically and |
| 302 | * statically-initialized dead_mountp. |
| 303 | */ |
| 304 | static struct mount dead_mount_store; |
| 305 | struct mount * const dead_mountp = &dead_mount_store; |
| 306 | |
| 307 | /* |
| 308 | * Initialize the vnode structures and initialize each file system type. |
| 309 | */ |
| 310 | void |
| 311 | vfsinit(void) |
| 312 | { |
| 313 | struct vfstable *vfsp; |
| 314 | int i, maxtypenum; |
| 315 | struct mount * mp; |
| 316 | |
| 317 | /* |
| 318 | * Initialize the vnode table |
| 319 | */ |
| 320 | vntblinit(); |
| 321 | /* |
| 322 | * Initialize the filesystem event mechanism. |
| 323 | */ |
| 324 | vfs_event_init(); |
| 325 | /* |
| 326 | * Initialize the vnode name cache |
| 327 | */ |
| 328 | nchinit(); |
| 329 | |
| 330 | /* |
| 331 | * Build vnode operation vectors. |
| 332 | */ |
| 333 | vfs_op_init(); |
| 334 | vfs_opv_init(); /* finish the job */ |
| 335 | /* |
| 336 | * Initialize each file system type in the static list, |
| 337 | * until the first NULL ->vfs_vfsops is encountered. |
| 338 | */ |
| 339 | maxtypenum = VT_NON; |
| 340 | for (vfsp = vfsconf, i = 0; i < maxvfsslots; i++, vfsp++) { |
| 341 | struct vfsconf vfsc; |
| 342 | if (vfsp->vfc_vfsops == (struct vfsops *)0) { |
| 343 | break; |
| 344 | } |
| 345 | if (i) { |
| 346 | vfsconf[i - 1].vfc_next = vfsp; |
| 347 | } |
| 348 | if (maxtypenum <= vfsp->vfc_typenum) { |
| 349 | maxtypenum = vfsp->vfc_typenum + 1; |
| 350 | } |
| 351 | |
| 352 | bzero(s: &vfsc, n: sizeof(struct vfsconf)); |
| 353 | vfsc.vfc_reserved1 = 0; |
| 354 | bcopy(src: vfsp->vfc_name, dst: vfsc.vfc_name, n: sizeof(vfsc.vfc_name)); |
| 355 | vfsc.vfc_typenum = vfsp->vfc_typenum; |
| 356 | vfsc.vfc_refcount = vfsp->vfc_refcount; |
| 357 | vfsc.vfc_flags = vfsp->vfc_flags; |
| 358 | vfsc.vfc_reserved2 = 0; |
| 359 | vfsc.vfc_reserved3 = 0; |
| 360 | |
| 361 | if (vfsp->vfc_vfsops->vfs_sysctl) { |
| 362 | struct sysctl_oid *oidp = NULL; |
| 363 | struct sysctl_oid oid = SYSCTL_STRUCT_INIT(_vfs, vfsp->vfc_typenum, , CTLTYPE_NODE | CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED, NULL, 0, vfs_sysctl_node, "-" , "" ); |
| 364 | |
| 365 | oidp = kalloc_type(struct sysctl_oid, Z_WAITOK); |
| 366 | *oidp = oid; |
| 367 | |
| 368 | /* Memory for VFS oid held by vfsentry forever */ |
| 369 | vfsp->vfc_sysctl = oidp; |
| 370 | oidp->oid_name = vfsp->vfc_name; |
| 371 | sysctl_register_oid(oidp: vfsp->vfc_sysctl); |
| 372 | } |
| 373 | |
| 374 | (*vfsp->vfc_vfsops->vfs_init)(&vfsc); |
| 375 | |
| 376 | numused_vfsslots++; |
| 377 | numregistered_fses++; |
| 378 | } |
| 379 | /* next vfc_typenum to be used */ |
| 380 | maxvfstypenum = maxtypenum; |
| 381 | |
| 382 | /* |
| 383 | * Initialize the vnop authorization scope. |
| 384 | */ |
| 385 | vnode_authorize_init(); |
| 386 | |
| 387 | /* |
| 388 | * create a mount point for dead vnodes |
| 389 | */ |
| 390 | mp = &dead_mount_store; |
| 391 | /* Initialize the default IO constraints */ |
| 392 | mp->mnt_maxreadcnt = mp->mnt_maxwritecnt = MAXPHYS; |
| 393 | mp->mnt_segreadcnt = mp->mnt_segwritecnt = 32; |
| 394 | mp->mnt_maxsegreadsize = mp->mnt_maxreadcnt; |
| 395 | mp->mnt_maxsegwritesize = mp->mnt_maxwritecnt; |
| 396 | mp->mnt_devblocksize = DEV_BSIZE; |
| 397 | mp->mnt_alignmentmask = PAGE_MASK; |
| 398 | mp->mnt_ioqueue_depth = MNT_DEFAULT_IOQUEUE_DEPTH; |
| 399 | mp->mnt_ioscale = 1; |
| 400 | mp->mnt_ioflags = 0; |
| 401 | mp->mnt_realrootvp = NULLVP; |
| 402 | mp->mnt_authcache_ttl = CACHED_LOOKUP_RIGHT_TTL; |
| 403 | |
| 404 | TAILQ_INIT(&mp->mnt_vnodelist); |
| 405 | TAILQ_INIT(&mp->mnt_workerqueue); |
| 406 | TAILQ_INIT(&mp->mnt_newvnodes); |
| 407 | mp->mnt_flag = MNT_LOCAL; |
| 408 | mp->mnt_lflag = MNT_LDEAD; |
| 409 | mount_lock_init(mp); |
| 410 | |
| 411 | #if CONFIG_MACF |
| 412 | mac_mount_label_init(mp); |
| 413 | mac_mount_label_associate(ctx: vfs_context_kernel(), mp); |
| 414 | #endif |
| 415 | /* |
| 416 | * dead_mountp is a statically-initialized constant pointer |
| 417 | * to dead_mount_store. |
| 418 | */ |
| 419 | |
| 420 | #if FS_COMPRESSION |
| 421 | decmpfs_init(); |
| 422 | #endif |
| 423 | |
| 424 | nspace_resolver_init(); |
| 425 | |
| 426 | #if CONFIG_EXCLAVES |
| 427 | vfs_exclave_fs_start(); |
| 428 | #endif |
| 429 | } |
| 430 | |
| 431 | void |
| 432 | vnode_list_lock(void) |
| 433 | { |
| 434 | lck_spin_lock_grp(lck: &vnode_list_spin_lock, grp: &vnode_list_lck_grp); |
| 435 | } |
| 436 | |
| 437 | void |
| 438 | vnode_list_unlock(void) |
| 439 | { |
| 440 | lck_spin_unlock(lck: &vnode_list_spin_lock); |
| 441 | } |
| 442 | |
| 443 | void |
| 444 | mount_list_lock(void) |
| 445 | { |
| 446 | lck_mtx_lock(lck: &mnt_list_mtx_lock); |
| 447 | } |
| 448 | |
| 449 | void |
| 450 | mount_list_unlock(void) |
| 451 | { |
| 452 | lck_mtx_unlock(lck: &mnt_list_mtx_lock); |
| 453 | } |
| 454 | |
| 455 | void |
| 456 | mount_lock_init(mount_t mp) |
| 457 | { |
| 458 | lck_mtx_init(lck: &mp->mnt_mlock, grp: &mnt_lck_grp, attr: &mnt_lck_attr); |
| 459 | lck_mtx_init(lck: &mp->mnt_iter_lock, grp: &mnt_lck_grp, attr: &mnt_lck_attr); |
| 460 | lck_mtx_init(lck: &mp->mnt_renamelock, grp: &mnt_lck_grp, attr: &mnt_lck_attr); |
| 461 | lck_rw_init(lck: &mp->mnt_rwlock, grp: &mnt_lck_grp, attr: &mnt_lck_attr); |
| 462 | } |
| 463 | |
| 464 | void |
| 465 | mount_lock_destroy(mount_t mp) |
| 466 | { |
| 467 | lck_mtx_destroy(lck: &mp->mnt_mlock, grp: &mnt_lck_grp); |
| 468 | lck_mtx_destroy(lck: &mp->mnt_iter_lock, grp: &mnt_lck_grp); |
| 469 | lck_mtx_destroy(lck: &mp->mnt_renamelock, grp: &mnt_lck_grp); |
| 470 | lck_rw_destroy(lck: &mp->mnt_rwlock, grp: &mnt_lck_grp); |
| 471 | } |
| 472 | |
| 473 | |
| 474 | /* |
| 475 | * Name: vfstable_add |
| 476 | * |
| 477 | * Description: Add a filesystem to the vfsconf list at the first |
| 478 | * unused slot. If no slots are available, return an |
| 479 | * error. |
| 480 | * |
| 481 | * Parameter: nvfsp vfsconf for VFS to add |
| 482 | * |
| 483 | * Returns: 0 Success |
| 484 | * -1 Failure |
| 485 | * |
| 486 | * Notes: The vfsconf should be treated as a linked list by |
| 487 | * all external references, as the implementation is |
| 488 | * expected to change in the future. The linkage is |
| 489 | * through ->vfc_next, and the list is NULL terminated. |
| 490 | * |
| 491 | * Warning: This code assumes that vfsconf[0] is non-empty. |
| 492 | */ |
| 493 | struct vfstable * |
| 494 | vfstable_add(struct vfstable *nvfsp) |
| 495 | { |
| 496 | int slot; |
| 497 | struct vfstable *slotp, *allocated = NULL; |
| 498 | struct sysctl_oid *oidp = NULL; |
| 499 | |
| 500 | |
| 501 | if (nvfsp->vfc_vfsops->vfs_sysctl) { |
| 502 | struct sysctl_oid oid = SYSCTL_STRUCT_INIT(_vfs, nvfsp->vfc_typenum, , CTLTYPE_NODE | CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED, NULL, 0, vfs_sysctl_node, "-" , "" ); |
| 503 | |
| 504 | oidp = kalloc_type(struct sysctl_oid, Z_WAITOK); |
| 505 | *oidp = oid; |
| 506 | } |
| 507 | |
| 508 | /* |
| 509 | * Find the next empty slot; we recognize an empty slot by a |
| 510 | * NULL-valued ->vfc_vfsops, so if we delete a VFS, we must |
| 511 | * ensure we set the entry back to NULL. |
| 512 | */ |
| 513 | findslot: |
| 514 | mount_list_lock(); |
| 515 | for (slot = 0; slot < maxvfsslots; slot++) { |
| 516 | if (vfsconf[slot].vfc_vfsops == NULL) { |
| 517 | break; |
| 518 | } |
| 519 | } |
| 520 | if (slot == maxvfsslots) { |
| 521 | if (allocated == NULL) { |
| 522 | mount_list_unlock(); |
| 523 | /* out of static slots; allocate one instead */ |
| 524 | allocated = kalloc_type(struct vfstable, Z_WAITOK); |
| 525 | goto findslot; |
| 526 | } else { |
| 527 | slotp = allocated; |
| 528 | } |
| 529 | } else { |
| 530 | slotp = &vfsconf[slot]; |
| 531 | } |
| 532 | |
| 533 | /* |
| 534 | * Replace the contents of the next empty slot with the contents |
| 535 | * of the provided nvfsp. |
| 536 | * |
| 537 | * Note; Takes advantage of the fact that 'slot' was left |
| 538 | * with the value of 'maxvfslots' in the allocation case. |
| 539 | */ |
| 540 | bcopy(src: nvfsp, dst: slotp, n: sizeof(struct vfstable)); |
| 541 | if (slot != 0) { |
| 542 | slotp->vfc_next = vfsconf[slot - 1].vfc_next; |
| 543 | vfsconf[slot - 1].vfc_next = slotp; |
| 544 | } else { |
| 545 | slotp->vfc_next = NULL; |
| 546 | } |
| 547 | |
| 548 | if (slotp != allocated) { |
| 549 | /* used a statically allocated slot */ |
| 550 | numused_vfsslots++; |
| 551 | } |
| 552 | numregistered_fses++; |
| 553 | |
| 554 | if (oidp) { |
| 555 | /* Memory freed in vfstable_del after unregistration */ |
| 556 | slotp->vfc_sysctl = oidp; |
| 557 | oidp->oid_name = slotp->vfc_name; |
| 558 | sysctl_register_oid(oidp: slotp->vfc_sysctl); |
| 559 | } |
| 560 | |
| 561 | mount_list_unlock(); |
| 562 | |
| 563 | if (allocated && allocated != slotp) { |
| 564 | /* did allocation, but ended up using static slot */ |
| 565 | kfree_type(struct vfstable, allocated); |
| 566 | } |
| 567 | |
| 568 | return slotp; |
| 569 | } |
| 570 | |
| 571 | /* |
| 572 | * Name: vfstable_del |
| 573 | * |
| 574 | * Description: Remove a filesystem from the vfsconf list by name. |
| 575 | * If no such filesystem exists, return an error. |
| 576 | * |
| 577 | * Parameter: fs_name name of VFS to remove |
| 578 | * |
| 579 | * Returns: 0 Success |
| 580 | * -1 Failure |
| 581 | * |
| 582 | * Notes: Hopefully all filesystems have unique names. |
| 583 | */ |
| 584 | int |
| 585 | vfstable_del(struct vfstable * vtbl) |
| 586 | { |
| 587 | struct vfstable **vcpp; |
| 588 | struct vfstable *vcdelp; |
| 589 | |
| 590 | #if DEBUG |
| 591 | lck_mtx_assert(&mnt_list_mtx_lock, LCK_MTX_ASSERT_OWNED); |
| 592 | #endif /* DEBUG */ |
| 593 | |
| 594 | /* |
| 595 | * Traverse the list looking for vtbl; if found, *vcpp |
| 596 | * will contain the address of the pointer to the entry to |
| 597 | * be removed. |
| 598 | */ |
| 599 | for (vcpp = &vfsconf; *vcpp; vcpp = &(*vcpp)->vfc_next) { |
| 600 | if (*vcpp == vtbl) { |
| 601 | break; |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | if (*vcpp == NULL) { |
| 606 | return ESRCH; /* vtbl not on vfsconf list */ |
| 607 | } |
| 608 | if ((*vcpp)->vfc_sysctl) { |
| 609 | sysctl_unregister_oid(oidp: (*vcpp)->vfc_sysctl); |
| 610 | (*vcpp)->vfc_sysctl->oid_name = NULL; |
| 611 | kfree_type(struct sysctl_oid, (*vcpp)->vfc_sysctl); |
| 612 | } |
| 613 | |
| 614 | /* Unlink entry */ |
| 615 | vcdelp = *vcpp; |
| 616 | *vcpp = (*vcpp)->vfc_next; |
| 617 | |
| 618 | /* |
| 619 | * Is this an entry from our static table? We find out by |
| 620 | * seeing if the pointer to the object to be deleted places |
| 621 | * the object in the address space containing the table (or not). |
| 622 | */ |
| 623 | if (vcdelp >= vfsconf && vcdelp < (vfsconf + maxvfsslots)) { /* Y */ |
| 624 | /* Mark as empty for vfscon_add() */ |
| 625 | bzero(s: vcdelp, n: sizeof(struct vfstable)); |
| 626 | numregistered_fses--; |
| 627 | numused_vfsslots--; |
| 628 | } else { /* N */ |
| 629 | /* |
| 630 | * This entry was dynamically allocated; we must free it; |
| 631 | * we would prefer to have just linked the caller's |
| 632 | * vfsconf onto our list, but it may not be persistent |
| 633 | * because of the previous (copying) implementation. |
| 634 | */ |
| 635 | numregistered_fses--; |
| 636 | mount_list_unlock(); |
| 637 | kfree_type(struct vfstable, vcdelp); |
| 638 | mount_list_lock(); |
| 639 | } |
| 640 | |
| 641 | #if DEBUG |
| 642 | lck_mtx_assert(&mnt_list_mtx_lock, LCK_MTX_ASSERT_OWNED); |
| 643 | #endif /* DEBUG */ |
| 644 | |
| 645 | return 0; |
| 646 | } |
| 647 | |
| 648 | lck_mtx_t * |
| 649 | SPECHASH_LOCK_ADDR(void) |
| 650 | { |
| 651 | return &spechash_mtx_lock; |
| 652 | } |
| 653 | |
| 654 | void |
| 655 | SPECHASH_LOCK(void) |
| 656 | { |
| 657 | lck_mtx_lock(lck: &spechash_mtx_lock); |
| 658 | } |
| 659 | |
| 660 | void |
| 661 | SPECHASH_UNLOCK(void) |
| 662 | { |
| 663 | lck_mtx_unlock(lck: &spechash_mtx_lock); |
| 664 | } |
| 665 | |