]> git.saurik.com Git - apple/xnu.git/blob - bsd/vfs/vfs_init.c
7a5c95a61ebf8bffe9c86622bd1e4ba00a7fbc51
[apple/xnu.git] / bsd / vfs / vfs_init.c
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 <sys/malloc.h>
86 #include <sys/decmpfs.h>
87
88 #if CONFIG_MACF
89 #include <security/mac_framework.h>
90 #include <sys/kauth.h>
91 #endif
92 #if QUOTA
93 #include <sys/quota.h>
94 #endif
95
96 /*
97 * Sigh, such primitive tools are these...
98 */
99 #if 0
100 #define DODEBUG(A) A
101 #else
102 #define DODEBUG(A)
103 #endif
104
105 __private_extern__ void vntblinit(void);
106
107 extern struct vnodeopv_desc *vfs_opv_descs[];
108 /* a list of lists of vnodeops defns */
109 extern struct vnodeop_desc *vfs_op_descs[];
110 /* and the operations they perform */
111 /*
112 * This code doesn't work if the defn is **vnodop_defns with cc.
113 * The problem is because of the compiler sometimes putting in an
114 * extra level of indirection for arrays. It's an interesting
115 * "feature" of C.
116 */
117 int vfs_opv_numops;
118
119 typedef int (*PFIvp)(void *);
120
121 /*
122 * A miscellaneous routine.
123 * A generic "default" routine that just returns an error.
124 */
125 int
126 vn_default_error(void)
127 {
128
129 return (ENOTSUP);
130 }
131
132 /*
133 * vfs_init.c
134 *
135 * Allocate and fill in operations vectors.
136 *
137 * An undocumented feature of this approach to defining operations is that
138 * there can be multiple entries in vfs_opv_descs for the same operations
139 * vector. This allows third parties to extend the set of operations
140 * supported by another layer in a binary compatibile way. For example,
141 * assume that NFS needed to be modified to support Ficus. NFS has an entry
142 * (probably nfs_vnopdeop_decls) declaring all the operations NFS supports by
143 * default. Ficus could add another entry (ficus_nfs_vnodeop_decl_entensions)
144 * listing those new operations Ficus adds to NFS, all without modifying the
145 * NFS code. (Of couse, the OTW NFS protocol still needs to be munged, but
146 * that is a(whole)nother story.) This is a feature.
147 */
148 void
149 vfs_opv_init(void)
150 {
151 int i, j, k;
152 int (***opv_desc_vector_p)(void *);
153 int (**opv_desc_vector)(void *);
154 struct vnodeopv_entry_desc *opve_descp;
155
156 /*
157 * Allocate the dynamic vectors and fill them in.
158 */
159 for (i=0; vfs_opv_descs[i]; i++) {
160 opv_desc_vector_p = vfs_opv_descs[i]->opv_desc_vector_p;
161 /*
162 * Allocate and init the vector, if it needs it.
163 * Also handle backwards compatibility.
164 */
165 if (*opv_desc_vector_p == NULL) {
166 MALLOC(*opv_desc_vector_p, PFIvp*,
167 vfs_opv_numops*sizeof(PFIvp), M_TEMP, M_WAITOK);
168 bzero (*opv_desc_vector_p, vfs_opv_numops*sizeof(PFIvp));
169 DODEBUG(printf("vector at %x allocated\n",
170 opv_desc_vector_p));
171 }
172 opv_desc_vector = *opv_desc_vector_p;
173 for (j=0; vfs_opv_descs[i]->opv_desc_ops[j].opve_op; j++) {
174 opve_descp = &(vfs_opv_descs[i]->opv_desc_ops[j]);
175
176 /*
177 * Sanity check: is this operation listed
178 * in the list of operations? We check this
179 * by seeing if its offest is zero. Since
180 * the default routine should always be listed
181 * first, it should be the only one with a zero
182 * offset. Any other operation with a zero
183 * offset is probably not listed in
184 * vfs_op_descs, and so is probably an error.
185 *
186 * A panic here means the layer programmer
187 * has committed the all-too common bug
188 * of adding a new operation to the layer's
189 * list of vnode operations but
190 * not adding the operation to the system-wide
191 * list of supported operations.
192 */
193 if (opve_descp->opve_op->vdesc_offset == 0 &&
194 opve_descp->opve_op->vdesc_offset !=
195 VOFFSET(vnop_default)) {
196 printf("operation %s not listed in %s.\n",
197 opve_descp->opve_op->vdesc_name,
198 "vfs_op_descs");
199 panic ("vfs_opv_init: bad operation");
200 }
201 /*
202 * Fill in this entry.
203 */
204 opv_desc_vector[opve_descp->opve_op->vdesc_offset] =
205 opve_descp->opve_impl;
206 }
207 }
208 /*
209 * Finally, go back and replace unfilled routines
210 * with their default. (Sigh, an O(n^3) algorithm. I
211 * could make it better, but that'd be work, and n is small.)
212 */
213 for (i = 0; vfs_opv_descs[i]; i++) {
214 opv_desc_vector = *(vfs_opv_descs[i]->opv_desc_vector_p);
215 /*
216 * Force every operations vector to have a default routine.
217 */
218 if (opv_desc_vector[VOFFSET(vnop_default)]==NULL) {
219 panic("vfs_opv_init: operation vector without default routine.");
220 }
221 for (k = 0; k<vfs_opv_numops; k++)
222 if (opv_desc_vector[k] == NULL)
223 opv_desc_vector[k] =
224 opv_desc_vector[VOFFSET(vnop_default)];
225 }
226 }
227
228 /*
229 * Initialize known vnode operations vectors.
230 */
231 void
232 vfs_op_init(void)
233 {
234 int i;
235
236 DODEBUG(printf("Vnode_interface_init.\n"));
237 /*
238 * Set all vnode vectors to a well known value.
239 */
240 for (i = 0; vfs_opv_descs[i]; i++)
241 *(vfs_opv_descs[i]->opv_desc_vector_p) = NULL;
242 /*
243 * Figure out how many ops there are by counting the table,
244 * and assign each its offset.
245 */
246 for (vfs_opv_numops = 0, i = 0; vfs_op_descs[i]; i++) {
247 vfs_op_descs[i]->vdesc_offset = vfs_opv_numops;
248 vfs_opv_numops++;
249 }
250 DODEBUG(printf ("vfs_opv_numops=%d\n", vfs_opv_numops));
251 }
252
253 /*
254 * Routines having to do with the management of the vnode table.
255 */
256 extern struct vnodeops dead_vnodeops;
257 extern struct vnodeops spec_vnodeops;
258
259 /* vars for vnode lock */
260 lck_grp_t * vnode_lck_grp;
261 lck_grp_attr_t * vnode_lck_grp_attr;
262 lck_attr_t * vnode_lck_attr;
263
264 #if CONFIG_TRIGGERS
265 /* vars for vnode trigger resolver */
266 lck_grp_t * trigger_vnode_lck_grp;
267 lck_grp_attr_t * trigger_vnode_lck_grp_attr;
268 lck_attr_t * trigger_vnode_lck_attr;
269 #endif
270
271 lck_grp_t * fd_vn_lck_grp;
272 lck_grp_attr_t * fd_vn_lck_grp_attr;
273 lck_attr_t * fd_vn_lck_attr;
274
275 /* vars for vnode list lock */
276 lck_grp_t * vnode_list_lck_grp;
277 lck_grp_attr_t * vnode_list_lck_grp_attr;
278 lck_attr_t * vnode_list_lck_attr;
279 lck_spin_t * vnode_list_spin_lock;
280 lck_mtx_t * spechash_mtx_lock;
281
282 /* vars for vfsconf lock */
283 lck_grp_t * fsconf_lck_grp;
284 lck_grp_attr_t * fsconf_lck_grp_attr;
285 lck_attr_t * fsconf_lck_attr;
286
287
288 /* vars for mount lock */
289 lck_grp_t * mnt_lck_grp;
290 lck_grp_attr_t * mnt_lck_grp_attr;
291 lck_attr_t * mnt_lck_attr;
292
293 /* vars for mount list lock */
294 lck_grp_t * mnt_list_lck_grp;
295 lck_grp_attr_t * mnt_list_lck_grp_attr;
296 lck_attr_t * mnt_list_lck_attr;
297 lck_mtx_t * mnt_list_mtx_lock;
298
299 /* vars for sync mutex */
300 lck_grp_t * sync_mtx_lck_grp;
301 lck_grp_attr_t * sync_mtx_lck_grp_attr;
302 lck_attr_t * sync_mtx_lck_attr;
303 lck_mtx_t * sync_mtx_lck;
304
305 lck_mtx_t *pkg_extensions_lck;
306
307 struct mount * dead_mountp;
308
309 extern void nspace_handler_init(void);
310
311 /*
312 * Initialize the vnode structures and initialize each file system type.
313 */
314 void
315 vfsinit(void)
316 {
317 struct vfstable *vfsp;
318 int i, maxtypenum;
319 struct mount * mp;
320
321 /* Allocate vnode list lock group attribute and group */
322 vnode_list_lck_grp_attr = lck_grp_attr_alloc_init();
323
324 vnode_list_lck_grp = lck_grp_alloc_init("vnode list", vnode_list_lck_grp_attr);
325
326 /* Allocate vnode list lock attribute */
327 vnode_list_lck_attr = lck_attr_alloc_init();
328
329 /* Allocate vnode list lock */
330 vnode_list_spin_lock = lck_spin_alloc_init(vnode_list_lck_grp, vnode_list_lck_attr);
331
332 /* Allocate spec hash list lock */
333 spechash_mtx_lock = lck_mtx_alloc_init(vnode_list_lck_grp, vnode_list_lck_attr);
334
335 /* Allocate the package extensions table lock */
336 pkg_extensions_lck = lck_mtx_alloc_init(vnode_list_lck_grp, vnode_list_lck_attr);
337
338 /* allocate vnode lock group attribute and group */
339 vnode_lck_grp_attr= lck_grp_attr_alloc_init();
340
341 vnode_lck_grp = lck_grp_alloc_init("vnode", vnode_lck_grp_attr);
342
343 /* Allocate vnode lock attribute */
344 vnode_lck_attr = lck_attr_alloc_init();
345
346 #if CONFIG_TRIGGERS
347 trigger_vnode_lck_grp_attr = lck_grp_attr_alloc_init();
348 trigger_vnode_lck_grp = lck_grp_alloc_init("trigger_vnode", trigger_vnode_lck_grp_attr);
349 trigger_vnode_lck_attr = lck_attr_alloc_init();
350 #endif
351 /* Allocate per fd vnode data lock attribute and group */
352 fd_vn_lck_grp_attr = lck_grp_attr_alloc_init();
353 fd_vn_lck_grp = lck_grp_alloc_init("fd_vnode_data", fd_vn_lck_grp_attr);
354 fd_vn_lck_attr = lck_attr_alloc_init();
355
356 /* Allocate fs config lock group attribute and group */
357 fsconf_lck_grp_attr= lck_grp_attr_alloc_init();
358
359 fsconf_lck_grp = lck_grp_alloc_init("fs conf", fsconf_lck_grp_attr);
360
361 /* Allocate fs config lock attribute */
362 fsconf_lck_attr = lck_attr_alloc_init();
363
364 /* Allocate mount point related lock structures */
365
366 /* Allocate mount list lock group attribute and group */
367 mnt_list_lck_grp_attr= lck_grp_attr_alloc_init();
368
369 mnt_list_lck_grp = lck_grp_alloc_init("mount list", mnt_list_lck_grp_attr);
370
371 /* Allocate mount list lock attribute */
372 mnt_list_lck_attr = lck_attr_alloc_init();
373
374 /* Allocate mount list lock */
375 mnt_list_mtx_lock = lck_mtx_alloc_init(mnt_list_lck_grp, mnt_list_lck_attr);
376
377
378 /* allocate mount lock group attribute and group */
379 mnt_lck_grp_attr= lck_grp_attr_alloc_init();
380
381 mnt_lck_grp = lck_grp_alloc_init("mount", mnt_lck_grp_attr);
382
383 /* Allocate mount lock attribute */
384 mnt_lck_attr = lck_attr_alloc_init();
385
386 /* Allocate sync lock */
387 sync_mtx_lck_grp_attr = lck_grp_attr_alloc_init();
388 sync_mtx_lck_grp = lck_grp_alloc_init("sync thread", sync_mtx_lck_grp_attr);
389 sync_mtx_lck_attr = lck_attr_alloc_init();
390 sync_mtx_lck = lck_mtx_alloc_init(sync_mtx_lck_grp, sync_mtx_lck_attr);
391
392 /*
393 * Initialize the vnode table
394 */
395 vntblinit();
396 /*
397 * Initialize the filesystem event mechanism.
398 */
399 vfs_event_init();
400 /*
401 * Initialize the vnode name cache
402 */
403 nchinit();
404
405 nspace_handler_init();
406
407 /*
408 * Build vnode operation vectors.
409 */
410 vfs_op_init();
411 vfs_opv_init(); /* finish the job */
412 /*
413 * Initialize each file system type in the static list,
414 * until the first NULL ->vfs_vfsops is encountered.
415 */
416 maxtypenum = VT_NON;
417 for (vfsp = vfsconf, i = 0; i < maxvfsslots; i++, vfsp++) {
418 struct vfsconf vfsc;
419 if (vfsp->vfc_vfsops == (struct vfsops *)0)
420 break;
421 if (i) vfsconf[i-1].vfc_next = vfsp;
422 if (maxtypenum <= vfsp->vfc_typenum)
423 maxtypenum = vfsp->vfc_typenum + 1;
424
425 bzero(&vfsc, sizeof(struct vfsconf));
426 vfsc.vfc_reserved1 = 0;
427 bcopy(vfsp->vfc_name, vfsc.vfc_name, sizeof(vfsc.vfc_name));
428 vfsc.vfc_typenum = vfsp->vfc_typenum;
429 vfsc.vfc_refcount = vfsp->vfc_refcount;
430 vfsc.vfc_flags = vfsp->vfc_flags;
431 vfsc.vfc_reserved2 = 0;
432 vfsc.vfc_reserved3 = 0;
433
434 if (vfsp->vfc_vfsops->vfs_sysctl) {
435 struct sysctl_oid *oidp = NULL;
436 struct sysctl_oid oid = SYSCTL_STRUCT_INIT(_vfs, vfsp->vfc_typenum, , CTLTYPE_NODE | CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED, NULL, 0, vfs_sysctl_node, "-", "");
437
438 MALLOC(oidp, struct sysctl_oid *, sizeof(struct sysctl_oid), M_TEMP, M_WAITOK);
439 *oidp = oid;
440
441 /* Memory for VFS oid held by vfsentry forever */
442 vfsp->vfc_sysctl = oidp;
443 oidp->oid_name = vfsp->vfc_name;
444 sysctl_register_oid(vfsp->vfc_sysctl);
445 }
446
447 (*vfsp->vfc_vfsops->vfs_init)(&vfsc);
448
449 numused_vfsslots++;
450 numregistered_fses++;
451 }
452 /* next vfc_typenum to be used */
453 maxvfstypenum = maxtypenum;
454
455 /*
456 * Initialize the vnop authorization scope.
457 */
458 vnode_authorize_init();
459
460 /*
461 * Initialiize the quota system.
462 */
463 #if QUOTA
464 dqinit();
465 #endif
466
467 /*
468 * create a mount point for dead vnodes
469 */
470 MALLOC_ZONE(mp, struct mount *, sizeof(struct mount),
471 M_MOUNT, M_WAITOK);
472 bzero((char *)mp, sizeof(struct mount));
473 /* Initialize the default IO constraints */
474 mp->mnt_maxreadcnt = mp->mnt_maxwritecnt = MAXPHYS;
475 mp->mnt_segreadcnt = mp->mnt_segwritecnt = 32;
476 mp->mnt_maxsegreadsize = mp->mnt_maxreadcnt;
477 mp->mnt_maxsegwritesize = mp->mnt_maxwritecnt;
478 mp->mnt_devblocksize = DEV_BSIZE;
479 mp->mnt_alignmentmask = PAGE_MASK;
480 mp->mnt_ioqueue_depth = MNT_DEFAULT_IOQUEUE_DEPTH;
481 mp->mnt_ioscale = 1;
482 mp->mnt_ioflags = 0;
483 mp->mnt_realrootvp = NULLVP;
484 mp->mnt_authcache_ttl = CACHED_LOOKUP_RIGHT_TTL;
485
486 TAILQ_INIT(&mp->mnt_vnodelist);
487 TAILQ_INIT(&mp->mnt_workerqueue);
488 TAILQ_INIT(&mp->mnt_newvnodes);
489 mp->mnt_flag = MNT_LOCAL;
490 mp->mnt_lflag = MNT_LDEAD;
491 mount_lock_init(mp);
492
493 #if CONFIG_MACF
494 mac_mount_label_init(mp);
495 mac_mount_label_associate(vfs_context_kernel(), mp);
496 #endif
497 dead_mountp = mp;
498
499 #if FS_COMPRESSION
500 decmpfs_init();
501 #endif
502 }
503
504 void
505 vnode_list_lock(void)
506 {
507 lck_spin_lock(vnode_list_spin_lock);
508 }
509
510 void
511 vnode_list_unlock(void)
512 {
513 lck_spin_unlock(vnode_list_spin_lock);
514 }
515
516 void
517 mount_list_lock(void)
518 {
519 lck_mtx_lock(mnt_list_mtx_lock);
520 }
521
522 void
523 mount_list_unlock(void)
524 {
525 lck_mtx_unlock(mnt_list_mtx_lock);
526 }
527
528 void
529 mount_lock_init(mount_t mp)
530 {
531 lck_mtx_init(&mp->mnt_mlock, mnt_lck_grp, mnt_lck_attr);
532 lck_mtx_init(&mp->mnt_renamelock, mnt_lck_grp, mnt_lck_attr);
533 lck_rw_init(&mp->mnt_rwlock, mnt_lck_grp, mnt_lck_attr);
534 }
535
536 void
537 mount_lock_destroy(mount_t mp)
538 {
539 lck_mtx_destroy(&mp->mnt_mlock, mnt_lck_grp);
540 lck_mtx_destroy(&mp->mnt_renamelock, mnt_lck_grp);
541 lck_rw_destroy(&mp->mnt_rwlock, mnt_lck_grp);
542 }
543
544
545 /*
546 * Name: vfstable_add
547 *
548 * Description: Add a filesystem to the vfsconf list at the first
549 * unused slot. If no slots are available, return an
550 * error.
551 *
552 * Parameter: nvfsp vfsconf for VFS to add
553 *
554 * Returns: 0 Success
555 * -1 Failure
556 *
557 * Notes: The vfsconf should be treated as a linked list by
558 * all external references, as the implementation is
559 * expected to change in the future. The linkage is
560 * through ->vfc_next, and the list is NULL terminated.
561 *
562 * Warning: This code assumes that vfsconf[0] is non-empty.
563 */
564 struct vfstable *
565 vfstable_add(struct vfstable *nvfsp)
566 {
567 int slot;
568 struct vfstable *slotp, *allocated = NULL;
569 struct sysctl_oid *oidp = NULL;
570
571
572 if (nvfsp->vfc_vfsops->vfs_sysctl) {
573 struct sysctl_oid oid = SYSCTL_STRUCT_INIT(_vfs, nvfsp->vfc_typenum, , CTLTYPE_NODE | CTLFLAG_KERN | CTLFLAG_RW | CTLFLAG_LOCKED, NULL, 0, vfs_sysctl_node, "-", "");
574
575 MALLOC(oidp, struct sysctl_oid *, sizeof(struct sysctl_oid), M_TEMP, M_WAITOK);
576 *oidp = oid;
577 }
578
579 /*
580 * Find the next empty slot; we recognize an empty slot by a
581 * NULL-valued ->vfc_vfsops, so if we delete a VFS, we must
582 * ensure we set the entry back to NULL.
583 */
584 findslot:
585 mount_list_lock();
586 for (slot = 0; slot < maxvfsslots; slot++) {
587 if (vfsconf[slot].vfc_vfsops == NULL)
588 break;
589 }
590 if (slot == maxvfsslots) {
591 if (allocated == NULL) {
592 mount_list_unlock();
593 /* out of static slots; allocate one instead */
594 MALLOC(allocated, struct vfstable *, sizeof(struct vfstable),
595 M_TEMP, M_WAITOK);
596 goto findslot;
597 } else {
598 slotp = allocated;
599 }
600 } else {
601 slotp = &vfsconf[slot];
602 }
603
604 /*
605 * Replace the contents of the next empty slot with the contents
606 * of the provided nvfsp.
607 *
608 * Note; Takes advantage of the fact that 'slot' was left
609 * with the value of 'maxvfslots' in the allocation case.
610 */
611 bcopy(nvfsp, slotp, sizeof(struct vfstable));
612 if (slot != 0) {
613 slotp->vfc_next = vfsconf[slot - 1].vfc_next;
614 vfsconf[slot - 1].vfc_next = slotp;
615 } else {
616 slotp->vfc_next = NULL;
617 }
618
619 if (slotp != allocated) {
620 /* used a statically allocated slot */
621 numused_vfsslots++;
622 }
623 numregistered_fses++;
624
625 if (oidp) {
626 /* Memory freed in vfstable_del after unregistration */
627 slotp->vfc_sysctl = oidp;
628 oidp->oid_name = slotp->vfc_name;
629 sysctl_register_oid(slotp->vfc_sysctl);
630 }
631
632 mount_list_unlock();
633
634 if (allocated && allocated != slotp) {
635 /* did allocation, but ended up using static slot */
636 FREE(allocated, M_TEMP);
637 }
638
639 return(slotp);
640 }
641
642 /*
643 * Name: vfstable_del
644 *
645 * Description: Remove a filesystem from the vfsconf list by name.
646 * If no such filesystem exists, return an error.
647 *
648 * Parameter: fs_name name of VFS to remove
649 *
650 * Returns: 0 Success
651 * -1 Failure
652 *
653 * Notes: Hopefully all filesystems have unique names.
654 */
655 int
656 vfstable_del(struct vfstable * vtbl)
657 {
658 struct vfstable **vcpp;
659 struct vfstable *vcdelp;
660
661 #if DEBUG
662 lck_mtx_assert(mnt_list_mtx_lock, LCK_MTX_ASSERT_OWNED);
663 #endif /* DEBUG */
664
665 /*
666 * Traverse the list looking for vtbl; if found, *vcpp
667 * will contain the address of the pointer to the entry to
668 * be removed.
669 */
670 for( vcpp = &vfsconf; *vcpp; vcpp = &(*vcpp)->vfc_next) {
671 if (*vcpp == vtbl)
672 break;
673 }
674
675 if (*vcpp == NULL)
676 return(ESRCH); /* vtbl not on vfsconf list */
677
678 if ((*vcpp)->vfc_sysctl) {
679 sysctl_unregister_oid((*vcpp)->vfc_sysctl);
680 (*vcpp)->vfc_sysctl->oid_name = NULL;
681 FREE((*vcpp)->vfc_sysctl, M_TEMP);
682 (*vcpp)->vfc_sysctl = NULL;
683 }
684
685 /* Unlink entry */
686 vcdelp = *vcpp;
687 *vcpp = (*vcpp)->vfc_next;
688
689 /*
690 * Is this an entry from our static table? We find out by
691 * seeing if the pointer to the object to be deleted places
692 * the object in the address space containing the table (or not).
693 */
694 if (vcdelp >= vfsconf && vcdelp < (vfsconf + maxvfsslots)) { /* Y */
695 /* Mark as empty for vfscon_add() */
696 bzero(vcdelp, sizeof(struct vfstable));
697 numregistered_fses--;
698 numused_vfsslots--;
699 } else { /* N */
700 /*
701 * This entry was dynamically allocated; we must free it;
702 * we would prefer to have just linked the caller's
703 * vfsconf onto our list, but it may not be persistent
704 * because of the previous (copying) implementation.
705 */
706 numregistered_fses--;
707 mount_list_unlock();
708 FREE(vcdelp, M_TEMP);
709 mount_list_lock();
710 }
711
712 #if DEBUG
713 lck_mtx_assert(mnt_list_mtx_lock, LCK_MTX_ASSERT_OWNED);
714 #endif /* DEBUG */
715
716 return(0);
717 }
718
719 void
720 SPECHASH_LOCK(void)
721 {
722 lck_mtx_lock(spechash_mtx_lock);
723 }
724
725 void
726 SPECHASH_UNLOCK(void)
727 {
728 lck_mtx_unlock(spechash_mtx_lock);
729 }
730