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