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