]>
git.saurik.com Git - apple/xnu.git/blob - bsd/vfs/vfs_init.c
252b326afa083b43d1cc673844f3100336bd9dcc
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
23 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
25 * Copyright (c) 1989, 1993
26 * The Regents of the University of California. All rights reserved.
28 * This code is derived from software contributed
29 * to Berkeley by John Heidemann of the UCLA Ficus project.
31 * Source: * @(#)i405_init.c 2.10 92/04/27 UCLA Ficus project
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. All advertising materials mentioning features or use of this software
42 * must display the following acknowledgement:
43 * This product includes software developed by the University of
44 * California, Berkeley and its contributors.
45 * 4. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * @(#)vfs_init.c 8.5 (Berkeley) 5/11/95
65 #include <sys/param.h>
66 #include <sys/mount_internal.h>
69 #include <sys/vnode_internal.h>
71 #include <sys/namei.h>
72 #include <sys/ucred.h>
73 #include <sys/errno.h>
74 #include <sys/malloc.h>
78 * Sigh, such primitive tools are these...
86 extern uid_t console_user
;
87 extern struct vnodeopv_desc
*vfs_opv_descs
[];
88 /* a list of lists of vnodeops defns */
89 extern struct vnodeop_desc
*vfs_op_descs
[];
90 /* and the operations they perform */
92 * This code doesn't work if the defn is **vnodop_defns with cc.
93 * The problem is because of the compiler sometimes putting in an
94 * extra level of indirection for arrays. It's an interesting
99 typedef (*PFI
)(); /* the standard Pointer to a Function returning an Int */
102 * A miscellaneous routine.
103 * A generic "default" routine that just returns an error.
115 * Allocate and fill in operations vectors.
117 * An undocumented feature of this approach to defining operations is that
118 * there can be multiple entries in vfs_opv_descs for the same operations
119 * vector. This allows third parties to extend the set of operations
120 * supported by another layer in a binary compatibile way. For example,
121 * assume that NFS needed to be modified to support Ficus. NFS has an entry
122 * (probably nfs_vnopdeop_decls) declaring all the operations NFS supports by
123 * default. Ficus could add another entry (ficus_nfs_vnodeop_decl_entensions)
124 * listing those new operations Ficus adds to NFS, all without modifying the
125 * NFS code. (Of couse, the OTW NFS protocol still needs to be munged, but
126 * that is a(whole)nother story.) This is a feature.
132 int (***opv_desc_vector_p
)(void *);
133 int (**opv_desc_vector
)(void *);
134 struct vnodeopv_entry_desc
*opve_descp
;
137 * Allocate the dynamic vectors and fill them in.
139 for (i
=0; vfs_opv_descs
[i
]; i
++) {
140 opv_desc_vector_p
= vfs_opv_descs
[i
]->opv_desc_vector_p
;
142 * Allocate and init the vector, if it needs it.
143 * Also handle backwards compatibility.
145 if (*opv_desc_vector_p
== NULL
) {
146 MALLOC(*opv_desc_vector_p
, PFI
*,
147 vfs_opv_numops
*sizeof(PFI
), M_TEMP
, M_WAITOK
);
148 bzero (*opv_desc_vector_p
, vfs_opv_numops
*sizeof(PFI
));
149 DODEBUG(printf("vector at %x allocated\n",
152 opv_desc_vector
= *opv_desc_vector_p
;
153 for (j
=0; vfs_opv_descs
[i
]->opv_desc_ops
[j
].opve_op
; j
++) {
154 opve_descp
= &(vfs_opv_descs
[i
]->opv_desc_ops
[j
]);
157 * Sanity check: is this operation listed
158 * in the list of operations? We check this
159 * by seeing if its offest is zero. Since
160 * the default routine should always be listed
161 * first, it should be the only one with a zero
162 * offset. Any other operation with a zero
163 * offset is probably not listed in
164 * vfs_op_descs, and so is probably an error.
166 * A panic here means the layer programmer
167 * has committed the all-too common bug
168 * of adding a new operation to the layer's
169 * list of vnode operations but
170 * not adding the operation to the system-wide
171 * list of supported operations.
173 if (opve_descp
->opve_op
->vdesc_offset
== 0 &&
174 opve_descp
->opve_op
->vdesc_offset
!=
175 VOFFSET(vnop_default
)) {
176 printf("operation %s not listed in %s.\n",
177 opve_descp
->opve_op
->vdesc_name
,
179 panic ("vfs_opv_init: bad operation");
182 * Fill in this entry.
184 opv_desc_vector
[opve_descp
->opve_op
->vdesc_offset
] =
185 opve_descp
->opve_impl
;
189 * Finally, go back and replace unfilled routines
190 * with their default. (Sigh, an O(n^3) algorithm. I
191 * could make it better, but that'd be work, and n is small.)
193 for (i
= 0; vfs_opv_descs
[i
]; i
++) {
194 opv_desc_vector
= *(vfs_opv_descs
[i
]->opv_desc_vector_p
);
196 * Force every operations vector to have a default routine.
198 if (opv_desc_vector
[VOFFSET(vnop_default
)]==NULL
) {
199 panic("vfs_opv_init: operation vector without default routine.");
201 for (k
= 0; k
<vfs_opv_numops
; k
++)
202 if (opv_desc_vector
[k
] == NULL
)
204 opv_desc_vector
[VOFFSET(vnop_default
)];
209 * Initialize known vnode operations vectors.
216 DODEBUG(printf("Vnode_interface_init.\n"));
218 * Set all vnode vectors to a well known value.
220 for (i
= 0; vfs_opv_descs
[i
]; i
++)
221 *(vfs_opv_descs
[i
]->opv_desc_vector_p
) = NULL
;
223 * Figure out how many ops there are by counting the table,
224 * and assign each its offset.
226 for (vfs_opv_numops
= 0, i
= 0; vfs_op_descs
[i
]; i
++) {
227 vfs_op_descs
[i
]->vdesc_offset
= vfs_opv_numops
;
230 DODEBUG(printf ("vfs_opv_numops=%d\n", vfs_opv_numops
));
234 * Routines having to do with the management of the vnode table.
236 extern struct vnodeops dead_vnodeops
;
237 extern struct vnodeops spec_vnodeops
;
239 /* vars for vnode lock */
240 lck_grp_t
* vnode_lck_grp
;
241 lck_grp_attr_t
* vnode_lck_grp_attr
;
242 lck_attr_t
* vnode_lck_attr
;
245 /* vars for vnode list lock */
246 lck_grp_t
* vnode_list_lck_grp
;
247 lck_grp_attr_t
* vnode_list_lck_grp_attr
;
248 lck_attr_t
* vnode_list_lck_attr
;
249 lck_mtx_t
* vnode_list_mtx_lock
;
250 lck_mtx_t
* spechash_mtx_lock
;
251 /* Routine to lock and unlock the vnode lists */
252 void vnode_list_lock(void);
253 void vnode_list_unlock(void);
255 /* vars for vfsconf lock */
256 lck_grp_t
* fsconf_lck_grp
;
257 lck_grp_attr_t
* fsconf_lck_grp_attr
;
258 lck_attr_t
* fsconf_lck_attr
;
261 /* vars for mount lock */
262 lck_grp_t
* mnt_lck_grp
;
263 lck_grp_attr_t
* mnt_lck_grp_attr
;
264 lck_attr_t
* mnt_lck_attr
;
266 /* vars for mount list lock */
267 lck_grp_t
* mnt_list_lck_grp
;
268 lck_grp_attr_t
* mnt_list_lck_grp_attr
;
269 lck_attr_t
* mnt_list_lck_attr
;
270 lck_mtx_t
* mnt_list_mtx_lock
;
272 extern void journal_init();
274 struct mount
* dead_mountp
;
276 * Initialize the vnode structures and initialize each file system type.
281 struct vfstable
*vfsp
;
285 /* Allocate vnode list lock group attribute and group */
286 vnode_list_lck_grp_attr
= lck_grp_attr_alloc_init();
287 lck_grp_attr_setstat(vnode_list_lck_grp_attr
);
289 vnode_list_lck_grp
= lck_grp_alloc_init("vnode list", vnode_list_lck_grp_attr
);
291 /* Allocate vnode list lock attribute */
292 vnode_list_lck_attr
= lck_attr_alloc_init();
293 //lck_attr_setdebug(vnode_list_lck_attr);
295 /* Allocate vnode list lock */
296 vnode_list_mtx_lock
= lck_mtx_alloc_init(vnode_list_lck_grp
, vnode_list_lck_attr
);
298 /* Allocate spec hash list lock */
299 spechash_mtx_lock
= lck_mtx_alloc_init(vnode_list_lck_grp
, vnode_list_lck_attr
);
301 /* allocate vnode lock group attribute and group */
302 vnode_lck_grp_attr
= lck_grp_attr_alloc_init();
303 lck_grp_attr_setstat(vnode_lck_grp_attr
);
305 vnode_lck_grp
= lck_grp_alloc_init("vnode", vnode_lck_grp_attr
);
307 /* Allocate vnode lock attribute */
308 vnode_lck_attr
= lck_attr_alloc_init();
309 //lck_attr_setdebug(vnode_lck_attr);
311 /* Allocate fs config lock group attribute and group */
312 fsconf_lck_grp_attr
= lck_grp_attr_alloc_init();
313 lck_grp_attr_setstat(fsconf_lck_grp_attr
);
315 fsconf_lck_grp
= lck_grp_alloc_init("fs conf", fsconf_lck_grp_attr
);
317 /* Allocate fs config lock attribute */
318 fsconf_lck_attr
= lck_attr_alloc_init();
319 //lck_attr_setdebug(fsconf_lck_attr);
322 /* Allocate mount point related lock structures */
324 /* Allocate mount list lock group attribute and group */
325 mnt_list_lck_grp_attr
= lck_grp_attr_alloc_init();
326 lck_grp_attr_setstat(mnt_list_lck_grp_attr
);
328 mnt_list_lck_grp
= lck_grp_alloc_init("mount list", mnt_list_lck_grp_attr
);
330 /* Allocate mount list lock attribute */
331 mnt_list_lck_attr
= lck_attr_alloc_init();
332 //lck_attr_setdebug(mnt_list_lck_attr);
334 /* Allocate mount list lock */
335 mnt_list_mtx_lock
= lck_mtx_alloc_init(mnt_list_lck_grp
, mnt_list_lck_attr
);
338 /* allocate mount lock group attribute and group */
339 mnt_lck_grp_attr
= lck_grp_attr_alloc_init();
340 lck_grp_attr_setstat(mnt_lck_grp_attr
);
342 mnt_lck_grp
= lck_grp_alloc_init("mount", mnt_lck_grp_attr
);
344 /* Allocate mount lock attribute */
345 mnt_lck_attr
= lck_attr_alloc_init();
346 //lck_attr_setdebug(mnt_lck_attr);
349 * Initialize the "console user" for access purposes:
351 console_user
= (uid_t
)0;
354 * Initialize the vnode table
358 * Initialize the filesystem event mechanism.
362 * Initialize the vnode name cache
366 * Initialize the journaling locks
370 * Build vnode operation vectors.
373 vfs_opv_init(); /* finish the job */
375 * Initialize each file system type in the static list,
376 * until the first NULL ->vfs_vfsops is encountered.
378 numused_vfsslots
= maxtypenum
= 0;
379 for (vfsp
= vfsconf
, i
= 0; i
< maxvfsconf
; i
++, vfsp
++) {
380 if (vfsp
->vfc_vfsops
== (struct vfsops
*)0)
382 if (i
) vfsconf
[i
-1].vfc_next
= vfsp
;
383 if (maxtypenum
<= vfsp
->vfc_typenum
)
384 maxtypenum
= vfsp
->vfc_typenum
+ 1;
385 (*vfsp
->vfc_vfsops
->vfs_init
)(vfsp
);
387 lck_mtx_init(&vfsp
->vfc_lock
, fsconf_lck_grp
, fsconf_lck_attr
);
391 /* next vfc_typenum to be used */
392 maxvfsconf
= maxtypenum
;
395 * Initialize the vnop authorization scope.
397 vnode_authorize_init();
400 * create a mount point for dead vnodes
402 MALLOC_ZONE(mp
, struct mount
*, (u_long
)sizeof(struct mount
),
404 bzero((char *)mp
, (u_long
)sizeof(struct mount
));
405 /* Initialize the default IO constraints */
406 mp
->mnt_maxreadcnt
= mp
->mnt_maxwritecnt
= MAXPHYS
;
407 mp
->mnt_segreadcnt
= mp
->mnt_segwritecnt
= 32;
408 mp
->mnt_maxsegreadsize
= mp
->mnt_maxreadcnt
;
409 mp
->mnt_maxsegwritesize
= mp
->mnt_maxwritecnt
;
410 mp
->mnt_devblocksize
= DEV_BSIZE
;
412 TAILQ_INIT(&mp
->mnt_vnodelist
);
413 TAILQ_INIT(&mp
->mnt_workerqueue
);
414 TAILQ_INIT(&mp
->mnt_newvnodes
);
415 mp
->mnt_flag
= MNT_LOCAL
;
416 mp
->mnt_lflag
= MNT_LDEAD
;
424 lck_mtx_lock(vnode_list_mtx_lock
);
430 lck_mtx_unlock(vnode_list_mtx_lock
);
436 lck_mtx_lock(mnt_list_mtx_lock
);
442 lck_mtx_unlock(mnt_list_mtx_lock
);
446 mount_lock_init(mount_t mp
)
448 lck_mtx_init(&mp
->mnt_mlock
, mnt_lck_grp
, mnt_lck_attr
);
449 lck_mtx_init(&mp
->mnt_renamelock
, mnt_lck_grp
, mnt_lck_attr
);
450 lck_rw_init(&mp
->mnt_rwlock
, mnt_lck_grp
, mnt_lck_attr
);
454 mount_lock_destroy(mount_t mp
)
456 lck_mtx_destroy(&mp
->mnt_mlock
, mnt_lck_grp
);
457 lck_mtx_destroy(&mp
->mnt_renamelock
, mnt_lck_grp
);
458 lck_rw_destroy(&mp
->mnt_rwlock
, mnt_lck_grp
);
465 * Description: Add a filesystem to the vfsconf list at the first
466 * unused slot. If no slots are available, return an
469 * Parameter: nvfsp vfsconf for VFS to add
474 * Notes: The vfsconf should be treated as a linked list by
475 * all external references, as the implementation is
476 * expected to change in the future. The linkage is
477 * through ->vfc_next, and the list is NULL terminated.
479 * Warning: This code assumes that vfsconf[0] is non-empty.
482 vfstable_add(struct vfstable
*nvfsp
)
485 struct vfstable
*slotp
;
488 * Find the next empty slot; we recognize an empty slot by a
489 * NULL-valued ->vfc_vfsops, so if we delete a VFS, we must
490 * ensure we set the entry back to NULL.
492 for (slot
= 0; slot
< maxvfsslots
; slot
++) {
493 if (vfsconf
[slot
].vfc_vfsops
== NULL
)
496 if (slot
== maxvfsslots
) {
497 /* out of static slots; allocate one instead */
498 MALLOC(slotp
, struct vfstable
*, sizeof(struct vfstable
),
501 slotp
= &vfsconf
[slot
];
505 * Replace the contents of the next empty slot with the contents
506 * of the provided nvfsp.
508 * Note; Takes advantage of the fact that 'slot' was left
509 * with the value of 'maxvfslots' in the allocation case.
511 bcopy(nvfsp
, slotp
, sizeof(struct vfstable
));
512 lck_mtx_init(&slotp
->vfc_lock
, fsconf_lck_grp
, fsconf_lck_attr
);
514 slotp
->vfc_next
= vfsconf
[slot
- 1].vfc_next
;
515 vfsconf
[slot
- 1].vfc_next
= slotp
;
517 slotp
->vfc_next
= NULL
;
527 * Description: Remove a filesystem from the vfsconf list by name.
528 * If no such filesystem exists, return an error.
530 * Parameter: fs_name name of VFS to remove
535 * Notes: Hopefully all filesystems have unique names.
538 vfstable_del(struct vfstable
* vtbl
)
540 struct vfstable
**vcpp
;
541 struct vfstable
*vcdelp
;
544 * Traverse the list looking for vtbl; if found, *vcpp
545 * will contain the address of the pointer to the entry to
548 for( vcpp
= &vfsconf
; *vcpp
; vcpp
= &(*vcpp
)->vfc_next
) {
554 return(ESRCH
); /* vtbl not on vfsconf list */
558 *vcpp
= (*vcpp
)->vfc_next
;
560 lck_mtx_destroy(&vcdelp
->vfc_lock
, fsconf_lck_grp
);
563 * Is this an entry from our static table? We find out by
564 * seeing if the pointer to the object to be deleted places
565 * the object in the address space containing the table (or not).
567 if (vcdelp
>= vfsconf
&& vcdelp
< (vfsconf
+ maxvfsslots
)) { /* Y */
568 /* Mark as empty for vfscon_add() */
569 bzero(vcdelp
, sizeof(struct vfstable
));
573 * This entry was dynamically allocated; we must free it;
574 * we would prefer to have just linked the caller's
575 * vfsconf onto our list, but it may not be persistent
576 * because of the previous (copying) implementation.
578 FREE(vcdelp
, M_TEMP
);
587 lck_mtx_lock(spechash_mtx_lock
);
591 SPECHASH_UNLOCK(void)
593 lck_mtx_unlock(spechash_mtx_lock
);