]>
git.saurik.com Git - apple/xnu.git/blob - bsd/vfs/vfs_init.c
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
24 * Copyright (c) 1989, 1993
25 * The Regents of the University of California. All rights reserved.
27 * This code is derived from software contributed
28 * to Berkeley by John Heidemann of the UCLA Ficus project.
30 * Source: * @(#)i405_init.c 2.10 92/04/27 UCLA Ficus project
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * @(#)vfs_init.c 8.5 (Berkeley) 5/11/95
64 #include <sys/param.h>
65 #include <sys/mount_internal.h>
68 #include <sys/vnode_internal.h>
70 #include <sys/namei.h>
71 #include <sys/ucred.h>
72 #include <sys/errno.h>
73 #include <sys/malloc.h>
77 * Sigh, such primitive tools are these...
85 extern uid_t console_user
;
86 extern struct vnodeopv_desc
*vfs_opv_descs
[];
87 /* a list of lists of vnodeops defns */
88 extern struct vnodeop_desc
*vfs_op_descs
[];
89 /* and the operations they perform */
91 * This code doesn't work if the defn is **vnodop_defns with cc.
92 * The problem is because of the compiler sometimes putting in an
93 * extra level of indirection for arrays. It's an interesting
98 typedef (*PFI
)(); /* the standard Pointer to a Function returning an Int */
101 * A miscellaneous routine.
102 * A generic "default" routine that just returns an error.
114 * Allocate and fill in operations vectors.
116 * An undocumented feature of this approach to defining operations is that
117 * there can be multiple entries in vfs_opv_descs for the same operations
118 * vector. This allows third parties to extend the set of operations
119 * supported by another layer in a binary compatibile way. For example,
120 * assume that NFS needed to be modified to support Ficus. NFS has an entry
121 * (probably nfs_vnopdeop_decls) declaring all the operations NFS supports by
122 * default. Ficus could add another entry (ficus_nfs_vnodeop_decl_entensions)
123 * listing those new operations Ficus adds to NFS, all without modifying the
124 * NFS code. (Of couse, the OTW NFS protocol still needs to be munged, but
125 * that is a(whole)nother story.) This is a feature.
131 int (***opv_desc_vector_p
)(void *);
132 int (**opv_desc_vector
)(void *);
133 struct vnodeopv_entry_desc
*opve_descp
;
136 * Allocate the dynamic vectors and fill them in.
138 for (i
=0; vfs_opv_descs
[i
]; i
++) {
139 opv_desc_vector_p
= vfs_opv_descs
[i
]->opv_desc_vector_p
;
141 * Allocate and init the vector, if it needs it.
142 * Also handle backwards compatibility.
144 if (*opv_desc_vector_p
== NULL
) {
145 MALLOC(*opv_desc_vector_p
, PFI
*,
146 vfs_opv_numops
*sizeof(PFI
), M_TEMP
, M_WAITOK
);
147 bzero (*opv_desc_vector_p
, vfs_opv_numops
*sizeof(PFI
));
148 DODEBUG(printf("vector at %x allocated\n",
151 opv_desc_vector
= *opv_desc_vector_p
;
152 for (j
=0; vfs_opv_descs
[i
]->opv_desc_ops
[j
].opve_op
; j
++) {
153 opve_descp
= &(vfs_opv_descs
[i
]->opv_desc_ops
[j
]);
156 * Sanity check: is this operation listed
157 * in the list of operations? We check this
158 * by seeing if its offest is zero. Since
159 * the default routine should always be listed
160 * first, it should be the only one with a zero
161 * offset. Any other operation with a zero
162 * offset is probably not listed in
163 * vfs_op_descs, and so is probably an error.
165 * A panic here means the layer programmer
166 * has committed the all-too common bug
167 * of adding a new operation to the layer's
168 * list of vnode operations but
169 * not adding the operation to the system-wide
170 * list of supported operations.
172 if (opve_descp
->opve_op
->vdesc_offset
== 0 &&
173 opve_descp
->opve_op
->vdesc_offset
!=
174 VOFFSET(vnop_default
)) {
175 printf("operation %s not listed in %s.\n",
176 opve_descp
->opve_op
->vdesc_name
,
178 panic ("vfs_opv_init: bad operation");
181 * Fill in this entry.
183 opv_desc_vector
[opve_descp
->opve_op
->vdesc_offset
] =
184 opve_descp
->opve_impl
;
188 * Finally, go back and replace unfilled routines
189 * with their default. (Sigh, an O(n^3) algorithm. I
190 * could make it better, but that'd be work, and n is small.)
192 for (i
= 0; vfs_opv_descs
[i
]; i
++) {
193 opv_desc_vector
= *(vfs_opv_descs
[i
]->opv_desc_vector_p
);
195 * Force every operations vector to have a default routine.
197 if (opv_desc_vector
[VOFFSET(vnop_default
)]==NULL
) {
198 panic("vfs_opv_init: operation vector without default routine.");
200 for (k
= 0; k
<vfs_opv_numops
; k
++)
201 if (opv_desc_vector
[k
] == NULL
)
203 opv_desc_vector
[VOFFSET(vnop_default
)];
208 * Initialize known vnode operations vectors.
215 DODEBUG(printf("Vnode_interface_init.\n"));
217 * Set all vnode vectors to a well known value.
219 for (i
= 0; vfs_opv_descs
[i
]; i
++)
220 *(vfs_opv_descs
[i
]->opv_desc_vector_p
) = NULL
;
222 * Figure out how many ops there are by counting the table,
223 * and assign each its offset.
225 for (vfs_opv_numops
= 0, i
= 0; vfs_op_descs
[i
]; i
++) {
226 vfs_op_descs
[i
]->vdesc_offset
= vfs_opv_numops
;
229 DODEBUG(printf ("vfs_opv_numops=%d\n", vfs_opv_numops
));
233 * Routines having to do with the management of the vnode table.
235 extern struct vnodeops dead_vnodeops
;
236 extern struct vnodeops spec_vnodeops
;
238 /* vars for vnode lock */
239 lck_grp_t
* vnode_lck_grp
;
240 lck_grp_attr_t
* vnode_lck_grp_attr
;
241 lck_attr_t
* vnode_lck_attr
;
244 /* vars for vnode list lock */
245 lck_grp_t
* vnode_list_lck_grp
;
246 lck_grp_attr_t
* vnode_list_lck_grp_attr
;
247 lck_attr_t
* vnode_list_lck_attr
;
248 lck_mtx_t
* vnode_list_mtx_lock
;
249 lck_mtx_t
* spechash_mtx_lock
;
250 /* Routine to lock and unlock the vnode lists */
251 void vnode_list_lock(void);
252 void vnode_list_unlock(void);
254 /* vars for vfsconf lock */
255 lck_grp_t
* fsconf_lck_grp
;
256 lck_grp_attr_t
* fsconf_lck_grp_attr
;
257 lck_attr_t
* fsconf_lck_attr
;
260 /* vars for mount lock */
261 lck_grp_t
* mnt_lck_grp
;
262 lck_grp_attr_t
* mnt_lck_grp_attr
;
263 lck_attr_t
* mnt_lck_attr
;
265 /* vars for mount list lock */
266 lck_grp_t
* mnt_list_lck_grp
;
267 lck_grp_attr_t
* mnt_list_lck_grp_attr
;
268 lck_attr_t
* mnt_list_lck_attr
;
269 lck_mtx_t
* mnt_list_mtx_lock
;
271 extern void journal_init();
273 struct mount
* dead_mountp
;
275 * Initialize the vnode structures and initialize each file system type.
280 struct vfstable
*vfsp
;
284 /* Allocate vnode list lock group attribute and group */
285 vnode_list_lck_grp_attr
= lck_grp_attr_alloc_init();
286 lck_grp_attr_setstat(vnode_list_lck_grp_attr
);
288 vnode_list_lck_grp
= lck_grp_alloc_init("vnode list", vnode_list_lck_grp_attr
);
290 /* Allocate vnode list lock attribute */
291 vnode_list_lck_attr
= lck_attr_alloc_init();
292 //lck_attr_setdebug(vnode_list_lck_attr);
294 /* Allocate vnode list lock */
295 vnode_list_mtx_lock
= lck_mtx_alloc_init(vnode_list_lck_grp
, vnode_list_lck_attr
);
297 /* Allocate spec hash list lock */
298 spechash_mtx_lock
= lck_mtx_alloc_init(vnode_list_lck_grp
, vnode_list_lck_attr
);
300 /* allocate vnode lock group attribute and group */
301 vnode_lck_grp_attr
= lck_grp_attr_alloc_init();
302 lck_grp_attr_setstat(vnode_lck_grp_attr
);
304 vnode_lck_grp
= lck_grp_alloc_init("vnode", vnode_lck_grp_attr
);
306 /* Allocate vnode lock attribute */
307 vnode_lck_attr
= lck_attr_alloc_init();
308 //lck_attr_setdebug(vnode_lck_attr);
310 /* Allocate fs config lock group attribute and group */
311 fsconf_lck_grp_attr
= lck_grp_attr_alloc_init();
312 lck_grp_attr_setstat(fsconf_lck_grp_attr
);
314 fsconf_lck_grp
= lck_grp_alloc_init("fs conf", fsconf_lck_grp_attr
);
316 /* Allocate fs config lock attribute */
317 fsconf_lck_attr
= lck_attr_alloc_init();
318 //lck_attr_setdebug(fsconf_lck_attr);
321 /* Allocate mount point related lock structures */
323 /* Allocate mount list lock group attribute and group */
324 mnt_list_lck_grp_attr
= lck_grp_attr_alloc_init();
325 lck_grp_attr_setstat(mnt_list_lck_grp_attr
);
327 mnt_list_lck_grp
= lck_grp_alloc_init("mount list", mnt_list_lck_grp_attr
);
329 /* Allocate mount list lock attribute */
330 mnt_list_lck_attr
= lck_attr_alloc_init();
331 //lck_attr_setdebug(mnt_list_lck_attr);
333 /* Allocate mount list lock */
334 mnt_list_mtx_lock
= lck_mtx_alloc_init(mnt_list_lck_grp
, mnt_list_lck_attr
);
337 /* allocate mount lock group attribute and group */
338 mnt_lck_grp_attr
= lck_grp_attr_alloc_init();
339 lck_grp_attr_setstat(mnt_lck_grp_attr
);
341 mnt_lck_grp
= lck_grp_alloc_init("mount", mnt_lck_grp_attr
);
343 /* Allocate mount lock attribute */
344 mnt_lck_attr
= lck_attr_alloc_init();
345 //lck_attr_setdebug(mnt_lck_attr);
348 * Initialize the "console user" for access purposes:
350 console_user
= (uid_t
)0;
353 * Initialize the vnode table
357 * Initialize the filesystem event mechanism.
361 * Initialize the vnode name cache
365 * Initialize the journaling locks
369 * Build vnode operation vectors.
372 vfs_opv_init(); /* finish the job */
374 * Initialize each file system type in the static list,
375 * until the first NULL ->vfs_vfsops is encountered.
377 numused_vfsslots
= maxtypenum
= 0;
378 for (vfsp
= vfsconf
, i
= 0; i
< maxvfsconf
; i
++, vfsp
++) {
379 if (vfsp
->vfc_vfsops
== (struct vfsops
*)0)
381 if (i
) vfsconf
[i
-1].vfc_next
= vfsp
;
382 if (maxtypenum
<= vfsp
->vfc_typenum
)
383 maxtypenum
= vfsp
->vfc_typenum
+ 1;
384 (*vfsp
->vfc_vfsops
->vfs_init
)(vfsp
);
386 lck_mtx_init(&vfsp
->vfc_lock
, fsconf_lck_grp
, fsconf_lck_attr
);
390 /* next vfc_typenum to be used */
391 maxvfsconf
= maxtypenum
;
394 * Initialize the vnop authorization scope.
396 vnode_authorize_init();
399 * create a mount point for dead vnodes
401 MALLOC_ZONE(mp
, struct mount
*, (u_long
)sizeof(struct mount
),
403 bzero((char *)mp
, (u_long
)sizeof(struct mount
));
404 /* Initialize the default IO constraints */
405 mp
->mnt_maxreadcnt
= mp
->mnt_maxwritecnt
= MAXPHYS
;
406 mp
->mnt_segreadcnt
= mp
->mnt_segwritecnt
= 32;
407 mp
->mnt_maxsegreadsize
= mp
->mnt_maxreadcnt
;
408 mp
->mnt_maxsegwritesize
= mp
->mnt_maxwritecnt
;
409 mp
->mnt_devblocksize
= DEV_BSIZE
;
411 TAILQ_INIT(&mp
->mnt_vnodelist
);
412 TAILQ_INIT(&mp
->mnt_workerqueue
);
413 TAILQ_INIT(&mp
->mnt_newvnodes
);
414 mp
->mnt_flag
= MNT_LOCAL
;
415 mp
->mnt_lflag
= MNT_LDEAD
;
423 lck_mtx_lock(vnode_list_mtx_lock
);
429 lck_mtx_unlock(vnode_list_mtx_lock
);
435 lck_mtx_lock(mnt_list_mtx_lock
);
441 lck_mtx_unlock(mnt_list_mtx_lock
);
445 mount_lock_init(mount_t mp
)
447 lck_mtx_init(&mp
->mnt_mlock
, mnt_lck_grp
, mnt_lck_attr
);
448 lck_mtx_init(&mp
->mnt_renamelock
, mnt_lck_grp
, mnt_lck_attr
);
449 lck_rw_init(&mp
->mnt_rwlock
, mnt_lck_grp
, mnt_lck_attr
);
453 mount_lock_destroy(mount_t mp
)
455 lck_mtx_destroy(&mp
->mnt_mlock
, mnt_lck_grp
);
456 lck_mtx_destroy(&mp
->mnt_renamelock
, mnt_lck_grp
);
457 lck_rw_destroy(&mp
->mnt_rwlock
, mnt_lck_grp
);
464 * Description: Add a filesystem to the vfsconf list at the first
465 * unused slot. If no slots are available, return an
468 * Parameter: nvfsp vfsconf for VFS to add
473 * Notes: The vfsconf should be treated as a linked list by
474 * all external references, as the implementation is
475 * expected to change in the future. The linkage is
476 * through ->vfc_next, and the list is NULL terminated.
478 * Warning: This code assumes that vfsconf[0] is non-empty.
481 vfstable_add(struct vfstable
*nvfsp
)
484 struct vfstable
*slotp
;
487 * Find the next empty slot; we recognize an empty slot by a
488 * NULL-valued ->vfc_vfsops, so if we delete a VFS, we must
489 * ensure we set the entry back to NULL.
491 for (slot
= 0; slot
< maxvfsslots
; slot
++) {
492 if (vfsconf
[slot
].vfc_vfsops
== NULL
)
495 if (slot
== maxvfsslots
) {
496 /* out of static slots; allocate one instead */
497 MALLOC(slotp
, struct vfstable
*, sizeof(struct vfstable
),
500 slotp
= &vfsconf
[slot
];
504 * Replace the contents of the next empty slot with the contents
505 * of the provided nvfsp.
507 * Note; Takes advantage of the fact that 'slot' was left
508 * with the value of 'maxvfslots' in the allocation case.
510 bcopy(nvfsp
, slotp
, sizeof(struct vfstable
));
511 lck_mtx_init(&slotp
->vfc_lock
, fsconf_lck_grp
, fsconf_lck_attr
);
513 slotp
->vfc_next
= vfsconf
[slot
- 1].vfc_next
;
514 vfsconf
[slot
- 1].vfc_next
= slotp
;
516 slotp
->vfc_next
= NULL
;
526 * Description: Remove a filesystem from the vfsconf list by name.
527 * If no such filesystem exists, return an error.
529 * Parameter: fs_name name of VFS to remove
534 * Notes: Hopefully all filesystems have unique names.
537 vfstable_del(struct vfstable
* vtbl
)
539 struct vfstable
**vcpp
;
540 struct vfstable
*vcdelp
;
543 * Traverse the list looking for vtbl; if found, *vcpp
544 * will contain the address of the pointer to the entry to
547 for( vcpp
= &vfsconf
; *vcpp
; vcpp
= &(*vcpp
)->vfc_next
) {
553 return(ESRCH
); /* vtbl not on vfsconf list */
557 *vcpp
= (*vcpp
)->vfc_next
;
559 lck_mtx_destroy(&vcdelp
->vfc_lock
, fsconf_lck_grp
);
562 * Is this an entry from our static table? We find out by
563 * seeing if the pointer to the object to be deleted places
564 * the object in the address space containing the table (or not).
566 if (vcdelp
>= vfsconf
&& vcdelp
< (vfsconf
+ maxvfsslots
)) { /* Y */
567 /* Mark as empty for vfscon_add() */
568 bzero(vcdelp
, sizeof(struct vfstable
));
572 * This entry was dynamically allocated; we must free it;
573 * we would prefer to have just linked the caller's
574 * vfsconf onto our list, but it may not be persistent
575 * because of the previous (copying) implementation.
577 FREE(vcdelp
, M_TEMP
);
586 lck_mtx_lock(spechash_mtx_lock
);
590 SPECHASH_UNLOCK(void)
592 lck_mtx_unlock(spechash_mtx_lock
);