]> git.saurik.com Git - apple/xnu.git/blob - bsd/vfs/vfs_init.c
xnu-792.2.4.tar.gz
[apple/xnu.git] / bsd / vfs / vfs_init.c
1 /*
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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.
11 *
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
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
23 /*
24 * Copyright (c) 1989, 1993
25 * The Regents of the University of California. All rights reserved.
26 *
27 * This code is derived from software contributed
28 * to Berkeley by John Heidemann of the UCLA Ficus project.
29 *
30 * Source: * @(#)i405_init.c 2.10 92/04/27 UCLA Ficus project
31 *
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
34 * are met:
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.
47 *
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
58 * SUCH DAMAGE.
59 *
60 * @(#)vfs_init.c 8.5 (Berkeley) 5/11/95
61 */
62
63
64 #include <sys/param.h>
65 #include <sys/mount_internal.h>
66 #include <sys/time.h>
67 #include <sys/vm.h>
68 #include <sys/vnode_internal.h>
69 #include <sys/stat.h>
70 #include <sys/namei.h>
71 #include <sys/ucred.h>
72 #include <sys/errno.h>
73 #include <sys/malloc.h>
74
75
76 /*
77 * Sigh, such primitive tools are these...
78 */
79 #if 0
80 #define DODEBUG(A) A
81 #else
82 #define DODEBUG(A)
83 #endif
84
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 */
90 /*
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
94 * "feature" of C.
95 */
96 int vfs_opv_numops;
97
98 typedef (*PFI)(); /* the standard Pointer to a Function returning an Int */
99
100 /*
101 * A miscellaneous routine.
102 * A generic "default" routine that just returns an error.
103 */
104 int
105 vn_default_error()
106 {
107
108 return (ENOTSUP);
109 }
110
111 /*
112 * vfs_init.c
113 *
114 * Allocate and fill in operations vectors.
115 *
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.
126 */
127 void
128 vfs_opv_init()
129 {
130 int i, j, k;
131 int (***opv_desc_vector_p)(void *);
132 int (**opv_desc_vector)(void *);
133 struct vnodeopv_entry_desc *opve_descp;
134
135 /*
136 * Allocate the dynamic vectors and fill them in.
137 */
138 for (i=0; vfs_opv_descs[i]; i++) {
139 opv_desc_vector_p = vfs_opv_descs[i]->opv_desc_vector_p;
140 /*
141 * Allocate and init the vector, if it needs it.
142 * Also handle backwards compatibility.
143 */
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",
149 opv_desc_vector_p));
150 }
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]);
154
155 /*
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.
164 *
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.
171 */
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,
177 "vfs_op_descs");
178 panic ("vfs_opv_init: bad operation");
179 }
180 /*
181 * Fill in this entry.
182 */
183 opv_desc_vector[opve_descp->opve_op->vdesc_offset] =
184 opve_descp->opve_impl;
185 }
186 }
187 /*
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.)
191 */
192 for (i = 0; vfs_opv_descs[i]; i++) {
193 opv_desc_vector = *(vfs_opv_descs[i]->opv_desc_vector_p);
194 /*
195 * Force every operations vector to have a default routine.
196 */
197 if (opv_desc_vector[VOFFSET(vnop_default)]==NULL) {
198 panic("vfs_opv_init: operation vector without default routine.");
199 }
200 for (k = 0; k<vfs_opv_numops; k++)
201 if (opv_desc_vector[k] == NULL)
202 opv_desc_vector[k] =
203 opv_desc_vector[VOFFSET(vnop_default)];
204 }
205 }
206
207 /*
208 * Initialize known vnode operations vectors.
209 */
210 void
211 vfs_op_init()
212 {
213 int i;
214
215 DODEBUG(printf("Vnode_interface_init.\n"));
216 /*
217 * Set all vnode vectors to a well known value.
218 */
219 for (i = 0; vfs_opv_descs[i]; i++)
220 *(vfs_opv_descs[i]->opv_desc_vector_p) = NULL;
221 /*
222 * Figure out how many ops there are by counting the table,
223 * and assign each its offset.
224 */
225 for (vfs_opv_numops = 0, i = 0; vfs_op_descs[i]; i++) {
226 vfs_op_descs[i]->vdesc_offset = vfs_opv_numops;
227 vfs_opv_numops++;
228 }
229 DODEBUG(printf ("vfs_opv_numops=%d\n", vfs_opv_numops));
230 }
231
232 /*
233 * Routines having to do with the management of the vnode table.
234 */
235 extern struct vnodeops dead_vnodeops;
236 extern struct vnodeops spec_vnodeops;
237
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;
242
243
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);
253
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;
258
259
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;
264
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;
270
271 extern void journal_init();
272
273 struct mount * dead_mountp;
274 /*
275 * Initialize the vnode structures and initialize each file system type.
276 */
277 void
278 vfsinit()
279 {
280 struct vfstable *vfsp;
281 int i, maxtypenum;
282 struct mount * mp;
283
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);
287
288 vnode_list_lck_grp = lck_grp_alloc_init("vnode list", vnode_list_lck_grp_attr);
289
290 /* Allocate vnode list lock attribute */
291 vnode_list_lck_attr = lck_attr_alloc_init();
292 //lck_attr_setdebug(vnode_list_lck_attr);
293
294 /* Allocate vnode list lock */
295 vnode_list_mtx_lock = lck_mtx_alloc_init(vnode_list_lck_grp, vnode_list_lck_attr);
296
297 /* Allocate spec hash list lock */
298 spechash_mtx_lock = lck_mtx_alloc_init(vnode_list_lck_grp, vnode_list_lck_attr);
299
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);
303
304 vnode_lck_grp = lck_grp_alloc_init("vnode", vnode_lck_grp_attr);
305
306 /* Allocate vnode lock attribute */
307 vnode_lck_attr = lck_attr_alloc_init();
308 //lck_attr_setdebug(vnode_lck_attr);
309
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);
313
314 fsconf_lck_grp = lck_grp_alloc_init("fs conf", fsconf_lck_grp_attr);
315
316 /* Allocate fs config lock attribute */
317 fsconf_lck_attr = lck_attr_alloc_init();
318 //lck_attr_setdebug(fsconf_lck_attr);
319
320
321 /* Allocate mount point related lock structures */
322
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);
326
327 mnt_list_lck_grp = lck_grp_alloc_init("mount list", mnt_list_lck_grp_attr);
328
329 /* Allocate mount list lock attribute */
330 mnt_list_lck_attr = lck_attr_alloc_init();
331 //lck_attr_setdebug(mnt_list_lck_attr);
332
333 /* Allocate mount list lock */
334 mnt_list_mtx_lock = lck_mtx_alloc_init(mnt_list_lck_grp, mnt_list_lck_attr);
335
336
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);
340
341 mnt_lck_grp = lck_grp_alloc_init("mount", mnt_lck_grp_attr);
342
343 /* Allocate mount lock attribute */
344 mnt_lck_attr = lck_attr_alloc_init();
345 //lck_attr_setdebug(mnt_lck_attr);
346
347 /*
348 * Initialize the "console user" for access purposes:
349 */
350 console_user = (uid_t)0;
351
352 /*
353 * Initialize the vnode table
354 */
355 vntblinit();
356 /*
357 * Initialize the filesystem event mechanism.
358 */
359 vfs_event_init();
360 /*
361 * Initialize the vnode name cache
362 */
363 nchinit();
364 /*
365 * Initialize the journaling locks
366 */
367 journal_init();
368 /*
369 * Build vnode operation vectors.
370 */
371 vfs_op_init();
372 vfs_opv_init(); /* finish the job */
373 /*
374 * Initialize each file system type in the static list,
375 * until the first NULL ->vfs_vfsops is encountered.
376 */
377 numused_vfsslots = maxtypenum = 0;
378 for (vfsp = vfsconf, i = 0; i < maxvfsconf; i++, vfsp++) {
379 if (vfsp->vfc_vfsops == (struct vfsops *)0)
380 break;
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);
385
386 lck_mtx_init(&vfsp->vfc_lock, fsconf_lck_grp, fsconf_lck_attr);
387
388 numused_vfsslots++;
389 }
390 /* next vfc_typenum to be used */
391 maxvfsconf = maxtypenum;
392
393 /*
394 * Initialize the vnop authorization scope.
395 */
396 vnode_authorize_init();
397
398 /*
399 * create a mount point for dead vnodes
400 */
401 MALLOC_ZONE(mp, struct mount *, (u_long)sizeof(struct mount),
402 M_MOUNT, M_WAITOK);
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;
410
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;
416 mount_lock_init(mp);
417 dead_mountp = mp;
418 }
419
420 void
421 vnode_list_lock()
422 {
423 lck_mtx_lock(vnode_list_mtx_lock);
424 }
425
426 void
427 vnode_list_unlock()
428 {
429 lck_mtx_unlock(vnode_list_mtx_lock);
430 }
431
432 void
433 mount_list_lock()
434 {
435 lck_mtx_lock(mnt_list_mtx_lock);
436 }
437
438 void
439 mount_list_unlock()
440 {
441 lck_mtx_unlock(mnt_list_mtx_lock);
442 }
443
444 void
445 mount_lock_init(mount_t mp)
446 {
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);
450 }
451
452 void
453 mount_lock_destroy(mount_t mp)
454 {
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);
458 }
459
460
461 /*
462 * Name: vfstable_add
463 *
464 * Description: Add a filesystem to the vfsconf list at the first
465 * unused slot. If no slots are available, return an
466 * error.
467 *
468 * Parameter: nvfsp vfsconf for VFS to add
469 *
470 * Returns: 0 Success
471 * -1 Failure
472 *
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.
477 *
478 * Warning: This code assumes that vfsconf[0] is non-empty.
479 */
480 struct vfstable *
481 vfstable_add(struct vfstable *nvfsp)
482 {
483 int slot;
484 struct vfstable *slotp;
485
486 /*
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.
490 */
491 for (slot = 0; slot < maxvfsslots; slot++) {
492 if (vfsconf[slot].vfc_vfsops == NULL)
493 break;
494 }
495 if (slot == maxvfsslots) {
496 /* out of static slots; allocate one instead */
497 MALLOC(slotp, struct vfstable *, sizeof(struct vfstable),
498 M_TEMP, M_WAITOK);
499 } else {
500 slotp = &vfsconf[slot];
501 }
502
503 /*
504 * Replace the contents of the next empty slot with the contents
505 * of the provided nvfsp.
506 *
507 * Note; Takes advantage of the fact that 'slot' was left
508 * with the value of 'maxvfslots' in the allocation case.
509 */
510 bcopy(nvfsp, slotp, sizeof(struct vfstable));
511 lck_mtx_init(&slotp->vfc_lock, fsconf_lck_grp, fsconf_lck_attr);
512 if (slot != 0) {
513 slotp->vfc_next = vfsconf[slot - 1].vfc_next;
514 vfsconf[slot - 1].vfc_next = slotp;
515 } else {
516 slotp->vfc_next = NULL;
517 }
518 numused_vfsslots++;
519
520 return(slotp);
521 }
522
523 /*
524 * Name: vfstable_del
525 *
526 * Description: Remove a filesystem from the vfsconf list by name.
527 * If no such filesystem exists, return an error.
528 *
529 * Parameter: fs_name name of VFS to remove
530 *
531 * Returns: 0 Success
532 * -1 Failure
533 *
534 * Notes: Hopefully all filesystems have unique names.
535 */
536 int
537 vfstable_del(struct vfstable * vtbl)
538 {
539 struct vfstable **vcpp;
540 struct vfstable *vcdelp;
541
542 /*
543 * Traverse the list looking for vtbl; if found, *vcpp
544 * will contain the address of the pointer to the entry to
545 * be removed.
546 */
547 for( vcpp = &vfsconf; *vcpp; vcpp = &(*vcpp)->vfc_next) {
548 if (*vcpp == vtbl)
549 break;
550 }
551
552 if (*vcpp == NULL)
553 return(ESRCH); /* vtbl not on vfsconf list */
554
555 /* Unlink entry */
556 vcdelp = *vcpp;
557 *vcpp = (*vcpp)->vfc_next;
558
559 lck_mtx_destroy(&vcdelp->vfc_lock, fsconf_lck_grp);
560
561 /*
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).
565 */
566 if (vcdelp >= vfsconf && vcdelp < (vfsconf + maxvfsslots)) { /* Y */
567 /* Mark as empty for vfscon_add() */
568 bzero(vcdelp, sizeof(struct vfstable));
569 numused_vfsslots--;
570 } else { /* N */
571 /*
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.
576 */
577 FREE(vcdelp, M_TEMP);
578 }
579
580 return(0);
581 }
582
583 void
584 SPECHASH_LOCK(void)
585 {
586 lck_mtx_lock(spechash_mtx_lock);
587 }
588
589 void
590 SPECHASH_UNLOCK(void)
591 {
592 lck_mtx_unlock(spechash_mtx_lock);
593 }
594