]> git.saurik.com Git - apple/xnu.git/blob - bsd/vfs/vfs_init.c
d845bb15086046c2512a58eaee098fb151fae294
[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
287 vnode_list_lck_grp = lck_grp_alloc_init("vnode list", vnode_list_lck_grp_attr);
288
289 /* Allocate vnode list lock attribute */
290 vnode_list_lck_attr = lck_attr_alloc_init();
291
292 /* Allocate vnode list lock */
293 vnode_list_mtx_lock = lck_mtx_alloc_init(vnode_list_lck_grp, vnode_list_lck_attr);
294
295 /* Allocate spec hash list lock */
296 spechash_mtx_lock = lck_mtx_alloc_init(vnode_list_lck_grp, vnode_list_lck_attr);
297
298 /* allocate vnode lock group attribute and group */
299 vnode_lck_grp_attr= lck_grp_attr_alloc_init();
300
301 vnode_lck_grp = lck_grp_alloc_init("vnode", vnode_lck_grp_attr);
302
303 /* Allocate vnode lock attribute */
304 vnode_lck_attr = lck_attr_alloc_init();
305
306 /* Allocate fs config lock group attribute and group */
307 fsconf_lck_grp_attr= lck_grp_attr_alloc_init();
308
309 fsconf_lck_grp = lck_grp_alloc_init("fs conf", fsconf_lck_grp_attr);
310
311 /* Allocate fs config lock attribute */
312 fsconf_lck_attr = lck_attr_alloc_init();
313
314 /* Allocate mount point related lock structures */
315
316 /* Allocate mount list lock group attribute and group */
317 mnt_list_lck_grp_attr= lck_grp_attr_alloc_init();
318
319 mnt_list_lck_grp = lck_grp_alloc_init("mount list", mnt_list_lck_grp_attr);
320
321 /* Allocate mount list lock attribute */
322 mnt_list_lck_attr = lck_attr_alloc_init();
323
324 /* Allocate mount list lock */
325 mnt_list_mtx_lock = lck_mtx_alloc_init(mnt_list_lck_grp, mnt_list_lck_attr);
326
327
328 /* allocate mount lock group attribute and group */
329 mnt_lck_grp_attr= lck_grp_attr_alloc_init();
330
331 mnt_lck_grp = lck_grp_alloc_init("mount", mnt_lck_grp_attr);
332
333 /* Allocate mount lock attribute */
334 mnt_lck_attr = lck_attr_alloc_init();
335
336 /*
337 * Initialize the "console user" for access purposes:
338 */
339 console_user = (uid_t)0;
340
341 /*
342 * Initialize the vnode table
343 */
344 vntblinit();
345 /*
346 * Initialize the filesystem event mechanism.
347 */
348 vfs_event_init();
349 /*
350 * Initialize the vnode name cache
351 */
352 nchinit();
353 /*
354 * Initialize the journaling locks
355 */
356 journal_init();
357 /*
358 * Build vnode operation vectors.
359 */
360 vfs_op_init();
361 vfs_opv_init(); /* finish the job */
362 /*
363 * Initialize each file system type in the static list,
364 * until the first NULL ->vfs_vfsops is encountered.
365 */
366 numused_vfsslots = maxtypenum = 0;
367 for (vfsp = vfsconf, i = 0; i < maxvfsconf; i++, vfsp++) {
368 if (vfsp->vfc_vfsops == (struct vfsops *)0)
369 break;
370 if (i) vfsconf[i-1].vfc_next = vfsp;
371 if (maxtypenum <= vfsp->vfc_typenum)
372 maxtypenum = vfsp->vfc_typenum + 1;
373 (*vfsp->vfc_vfsops->vfs_init)(vfsp);
374
375 lck_mtx_init(&vfsp->vfc_lock, fsconf_lck_grp, fsconf_lck_attr);
376
377 numused_vfsslots++;
378 }
379 /* next vfc_typenum to be used */
380 maxvfsconf = maxtypenum;
381
382 /*
383 * Initialize the vnop authorization scope.
384 */
385 vnode_authorize_init();
386
387 /*
388 * create a mount point for dead vnodes
389 */
390 MALLOC_ZONE(mp, struct mount *, (u_long)sizeof(struct mount),
391 M_MOUNT, M_WAITOK);
392 bzero((char *)mp, (u_long)sizeof(struct mount));
393 /* Initialize the default IO constraints */
394 mp->mnt_maxreadcnt = mp->mnt_maxwritecnt = MAXPHYS;
395 mp->mnt_segreadcnt = mp->mnt_segwritecnt = 32;
396 mp->mnt_maxsegreadsize = mp->mnt_maxreadcnt;
397 mp->mnt_maxsegwritesize = mp->mnt_maxwritecnt;
398 mp->mnt_devblocksize = DEV_BSIZE;
399
400 TAILQ_INIT(&mp->mnt_vnodelist);
401 TAILQ_INIT(&mp->mnt_workerqueue);
402 TAILQ_INIT(&mp->mnt_newvnodes);
403 mp->mnt_flag = MNT_LOCAL;
404 mp->mnt_lflag = MNT_LDEAD;
405 mount_lock_init(mp);
406 dead_mountp = mp;
407 }
408
409 void
410 vnode_list_lock()
411 {
412 lck_mtx_lock(vnode_list_mtx_lock);
413 }
414
415 void
416 vnode_list_unlock()
417 {
418 lck_mtx_unlock(vnode_list_mtx_lock);
419 }
420
421 void
422 mount_list_lock()
423 {
424 lck_mtx_lock(mnt_list_mtx_lock);
425 }
426
427 void
428 mount_list_unlock()
429 {
430 lck_mtx_unlock(mnt_list_mtx_lock);
431 }
432
433 void
434 mount_lock_init(mount_t mp)
435 {
436 lck_mtx_init(&mp->mnt_mlock, mnt_lck_grp, mnt_lck_attr);
437 lck_mtx_init(&mp->mnt_renamelock, mnt_lck_grp, mnt_lck_attr);
438 lck_rw_init(&mp->mnt_rwlock, mnt_lck_grp, mnt_lck_attr);
439 }
440
441 void
442 mount_lock_destroy(mount_t mp)
443 {
444 lck_mtx_destroy(&mp->mnt_mlock, mnt_lck_grp);
445 lck_mtx_destroy(&mp->mnt_renamelock, mnt_lck_grp);
446 lck_rw_destroy(&mp->mnt_rwlock, mnt_lck_grp);
447 }
448
449
450 /*
451 * Name: vfstable_add
452 *
453 * Description: Add a filesystem to the vfsconf list at the first
454 * unused slot. If no slots are available, return an
455 * error.
456 *
457 * Parameter: nvfsp vfsconf for VFS to add
458 *
459 * Returns: 0 Success
460 * -1 Failure
461 *
462 * Notes: The vfsconf should be treated as a linked list by
463 * all external references, as the implementation is
464 * expected to change in the future. The linkage is
465 * through ->vfc_next, and the list is NULL terminated.
466 *
467 * Warning: This code assumes that vfsconf[0] is non-empty.
468 */
469 struct vfstable *
470 vfstable_add(struct vfstable *nvfsp)
471 {
472 int slot;
473 struct vfstable *slotp;
474
475 /*
476 * Find the next empty slot; we recognize an empty slot by a
477 * NULL-valued ->vfc_vfsops, so if we delete a VFS, we must
478 * ensure we set the entry back to NULL.
479 */
480 for (slot = 0; slot < maxvfsslots; slot++) {
481 if (vfsconf[slot].vfc_vfsops == NULL)
482 break;
483 }
484 if (slot == maxvfsslots) {
485 /* out of static slots; allocate one instead */
486 MALLOC(slotp, struct vfstable *, sizeof(struct vfstable),
487 M_TEMP, M_WAITOK);
488 } else {
489 slotp = &vfsconf[slot];
490 }
491
492 /*
493 * Replace the contents of the next empty slot with the contents
494 * of the provided nvfsp.
495 *
496 * Note; Takes advantage of the fact that 'slot' was left
497 * with the value of 'maxvfslots' in the allocation case.
498 */
499 bcopy(nvfsp, slotp, sizeof(struct vfstable));
500 lck_mtx_init(&slotp->vfc_lock, fsconf_lck_grp, fsconf_lck_attr);
501 if (slot != 0) {
502 slotp->vfc_next = vfsconf[slot - 1].vfc_next;
503 vfsconf[slot - 1].vfc_next = slotp;
504 } else {
505 slotp->vfc_next = NULL;
506 }
507 numused_vfsslots++;
508
509 return(slotp);
510 }
511
512 /*
513 * Name: vfstable_del
514 *
515 * Description: Remove a filesystem from the vfsconf list by name.
516 * If no such filesystem exists, return an error.
517 *
518 * Parameter: fs_name name of VFS to remove
519 *
520 * Returns: 0 Success
521 * -1 Failure
522 *
523 * Notes: Hopefully all filesystems have unique names.
524 */
525 int
526 vfstable_del(struct vfstable * vtbl)
527 {
528 struct vfstable **vcpp;
529 struct vfstable *vcdelp;
530
531 /*
532 * Traverse the list looking for vtbl; if found, *vcpp
533 * will contain the address of the pointer to the entry to
534 * be removed.
535 */
536 for( vcpp = &vfsconf; *vcpp; vcpp = &(*vcpp)->vfc_next) {
537 if (*vcpp == vtbl)
538 break;
539 }
540
541 if (*vcpp == NULL)
542 return(ESRCH); /* vtbl not on vfsconf list */
543
544 /* Unlink entry */
545 vcdelp = *vcpp;
546 *vcpp = (*vcpp)->vfc_next;
547
548 lck_mtx_destroy(&vcdelp->vfc_lock, fsconf_lck_grp);
549
550 /*
551 * Is this an entry from our static table? We find out by
552 * seeing if the pointer to the object to be deleted places
553 * the object in the address space containing the table (or not).
554 */
555 if (vcdelp >= vfsconf && vcdelp < (vfsconf + maxvfsslots)) { /* Y */
556 /* Mark as empty for vfscon_add() */
557 bzero(vcdelp, sizeof(struct vfstable));
558 numused_vfsslots--;
559 } else { /* N */
560 /*
561 * This entry was dynamically allocated; we must free it;
562 * we would prefer to have just linked the caller's
563 * vfsconf onto our list, but it may not be persistent
564 * because of the previous (copying) implementation.
565 */
566 FREE(vcdelp, M_TEMP);
567 }
568
569 return(0);
570 }
571
572 void
573 SPECHASH_LOCK(void)
574 {
575 lck_mtx_lock(spechash_mtx_lock);
576 }
577
578 void
579 SPECHASH_UNLOCK(void)
580 {
581 lck_mtx_unlock(spechash_mtx_lock);
582 }
583