]> git.saurik.com Git - apple/xnu.git/blame - bsd/vfs/vfs_init.c
xnu-517.tar.gz
[apple/xnu.git] / bsd / vfs / vfs_init.c
CommitLineData
1c79356b 1/*
55e303ae 2 * Copyright (c) 2000-2002 Apple Computer, Inc. All rights reserved.
1c79356b
A
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
43866e37 6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
1c79356b 7 *
43866e37
A
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
1c79356b
A
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
43866e37
A
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
1c79356b
A
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25/* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
26/*
27 * Copyright (c) 1989, 1993
28 * The Regents of the University of California. All rights reserved.
29 *
30 * This code is derived from software contributed
31 * to Berkeley by John Heidemann of the UCLA Ficus project.
32 *
33 * Source: * @(#)i405_init.c 2.10 92/04/27 UCLA Ficus project
34 *
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
37 * are met:
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 3. All advertising materials mentioning features or use of this software
44 * must display the following acknowledgement:
45 * This product includes software developed by the University of
46 * California, Berkeley and its contributors.
47 * 4. Neither the name of the University nor the names of its contributors
48 * may be used to endorse or promote products derived from this software
49 * without specific prior written permission.
50 *
51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * SUCH DAMAGE.
62 *
63 * @(#)vfs_init.c 8.5 (Berkeley) 5/11/95
64 */
65
66
67#include <sys/param.h>
68#include <sys/mount.h>
69#include <sys/time.h>
70#include <sys/vm.h>
71#include <sys/vnode.h>
72#include <sys/stat.h>
73#include <sys/namei.h>
74#include <sys/ucred.h>
75#include <sys/buf.h>
76#include <sys/errno.h>
77#include <sys/malloc.h>
78
79
80/*
81 * Sigh, such primitive tools are these...
82 */
83#if 0
84#define DODEBUG(A) A
85#else
86#define DODEBUG(A)
87#endif
88
89extern uid_t console_user;
90extern struct vnodeopv_desc *vfs_opv_descs[];
91 /* a list of lists of vnodeops defns */
92extern struct vnodeop_desc *vfs_op_descs[];
93 /* and the operations they perform */
94/*
95 * This code doesn't work if the defn is **vnodop_defns with cc.
96 * The problem is because of the compiler sometimes putting in an
97 * extra level of indirection for arrays. It's an interesting
98 * "feature" of C.
99 */
100int vfs_opv_numops;
101
102typedef (*PFI)(); /* the standard Pointer to a Function returning an Int */
103
104/*
105 * A miscellaneous routine.
106 * A generic "default" routine that just returns an error.
107 */
108int
109vn_default_error()
110{
111
112 return (EOPNOTSUPP);
113}
114
115/*
116 * vfs_init.c
117 *
118 * Allocate and fill in operations vectors.
119 *
120 * An undocumented feature of this approach to defining operations is that
121 * there can be multiple entries in vfs_opv_descs for the same operations
122 * vector. This allows third parties to extend the set of operations
123 * supported by another layer in a binary compatibile way. For example,
124 * assume that NFS needed to be modified to support Ficus. NFS has an entry
125 * (probably nfs_vnopdeop_decls) declaring all the operations NFS supports by
126 * default. Ficus could add another entry (ficus_nfs_vnodeop_decl_entensions)
127 * listing those new operations Ficus adds to NFS, all without modifying the
128 * NFS code. (Of couse, the OTW NFS protocol still needs to be munged, but
129 * that is a(whole)nother story.) This is a feature.
130 */
131void
132vfs_opv_init()
133{
134 int i, j, k;
135 int (***opv_desc_vector_p)(void *);
136 int (**opv_desc_vector)(void *);
137 struct vnodeopv_entry_desc *opve_descp;
138
139 /*
140 * Allocate the dynamic vectors and fill them in.
141 */
142 for (i=0; vfs_opv_descs[i]; i++) {
143 opv_desc_vector_p = vfs_opv_descs[i]->opv_desc_vector_p;
144 /*
145 * Allocate and init the vector, if it needs it.
146 * Also handle backwards compatibility.
147 */
148 if (*opv_desc_vector_p == NULL) {
1c79356b 149 MALLOC(*opv_desc_vector_p, PFI*,
55e303ae 150 vfs_opv_numops*sizeof(PFI), M_TEMP, M_WAITOK);
1c79356b
A
151 bzero (*opv_desc_vector_p, vfs_opv_numops*sizeof(PFI));
152 DODEBUG(printf("vector at %x allocated\n",
153 opv_desc_vector_p));
154 }
155 opv_desc_vector = *opv_desc_vector_p;
156 for (j=0; vfs_opv_descs[i]->opv_desc_ops[j].opve_op; j++) {
157 opve_descp = &(vfs_opv_descs[i]->opv_desc_ops[j]);
158
159 /*
160 * Sanity check: is this operation listed
161 * in the list of operations? We check this
162 * by seeing if its offest is zero. Since
163 * the default routine should always be listed
164 * first, it should be the only one with a zero
165 * offset. Any other operation with a zero
166 * offset is probably not listed in
167 * vfs_op_descs, and so is probably an error.
168 *
169 * A panic here means the layer programmer
170 * has committed the all-too common bug
171 * of adding a new operation to the layer's
172 * list of vnode operations but
173 * not adding the operation to the system-wide
174 * list of supported operations.
175 */
176 if (opve_descp->opve_op->vdesc_offset == 0 &&
177 opve_descp->opve_op->vdesc_offset !=
178 VOFFSET(vop_default)) {
179 printf("operation %s not listed in %s.\n",
180 opve_descp->opve_op->vdesc_name,
181 "vfs_op_descs");
182 panic ("vfs_opv_init: bad operation");
183 }
184 /*
185 * Fill in this entry.
186 */
187 opv_desc_vector[opve_descp->opve_op->vdesc_offset] =
188 opve_descp->opve_impl;
189 }
190 }
191 /*
192 * Finally, go back and replace unfilled routines
193 * with their default. (Sigh, an O(n^3) algorithm. I
194 * could make it better, but that'd be work, and n is small.)
195 */
196 for (i = 0; vfs_opv_descs[i]; i++) {
197 opv_desc_vector = *(vfs_opv_descs[i]->opv_desc_vector_p);
198 /*
199 * Force every operations vector to have a default routine.
200 */
201 if (opv_desc_vector[VOFFSET(vop_default)]==NULL) {
202 panic("vfs_opv_init: operation vector without default routine.");
203 }
204 for (k = 0; k<vfs_opv_numops; k++)
205 if (opv_desc_vector[k] == NULL)
206 opv_desc_vector[k] =
207 opv_desc_vector[VOFFSET(vop_default)];
208 }
209}
210
211/*
212 * Initialize known vnode operations vectors.
213 */
214void
215vfs_op_init()
216{
217 int i;
218
219 DODEBUG(printf("Vnode_interface_init.\n"));
220 /*
221 * Set all vnode vectors to a well known value.
222 */
223 for (i = 0; vfs_opv_descs[i]; i++)
224 *(vfs_opv_descs[i]->opv_desc_vector_p) = NULL;
225 /*
226 * Figure out how many ops there are by counting the table,
227 * and assign each its offset.
228 */
229 for (vfs_opv_numops = 0, i = 0; vfs_op_descs[i]; i++) {
230 vfs_op_descs[i]->vdesc_offset = vfs_opv_numops;
231 vfs_opv_numops++;
232 }
233 DODEBUG(printf ("vfs_opv_numops=%d\n", vfs_opv_numops));
234}
235
236/*
237 * Routines having to do with the management of the vnode table.
238 */
239extern struct vnodeops dead_vnodeops;
240extern struct vnodeops spec_vnodeops;
241struct vattr va_null;
242
243/*
244 * Initialize the vnode structures and initialize each file system type.
245 */
246vfsinit()
247{
248 struct vfsconf *vfsp;
249 int i, maxtypenum;
250
251 /*
252 * Initialize the "console user" for access purposes:
253 */
254 console_user = (uid_t)0;
255
256 /*
257 * Initialize the vnode table
258 */
259 vntblinit();
55e303ae
A
260 /*
261 * Initialize the filesystem event mechanism.
262 */
263 vfs_event_init();
1c79356b
A
264 /*
265 * Initialize the vnode name cache
266 */
267 nchinit();
268 /*
269 * Build vnode operation vectors.
270 */
271 vfs_op_init();
272 vfs_opv_init(); /* finish the job */
273 /*
55e303ae
A
274 * Initialize each file system type in the static list,
275 * until the first NULL ->vfs_vfsops is encountered.
1c79356b
A
276 */
277 vattr_null(&va_null);
278 numused_vfsslots = maxtypenum = 0;
279 for (vfsp = vfsconf, i = 0; i < maxvfsconf; i++, vfsp++) {
280 if (vfsp->vfc_vfsops == (struct vfsops *)0)
281 break;
282 if (i) vfsconf[i-1].vfc_next = vfsp;
283 if (maxtypenum <= vfsp->vfc_typenum)
284 maxtypenum = vfsp->vfc_typenum + 1;
285 (*vfsp->vfc_vfsops->vfs_init)(vfsp);
286 numused_vfsslots++;
287 }
288 /* next vfc_typenum to be used */
289 maxvfsconf = maxtypenum;
290}
291
55e303ae
A
292/*
293 * Name: vfsconf_add
294 *
295 * Description: Add a filesystem to the vfsconf list at the first
296 * unused slot. If no slots are available, return an
297 * error.
298 *
299 * Parameter: nvfsp vfsconf for VFS to add
300 *
301 * Returns: 0 Success
302 * -1 Failure
303 *
304 * Notes: The vfsconf should be treated as a linked list by
305 * all external references, as the implementation is
306 * expected to change in the future. The linkage is
307 * through ->vfc_next, and the list is NULL terminated.
308 *
309 * Warning: This code assumes that vfsconf[0] is non-empty.
310 */
1c79356b
A
311int
312vfsconf_add(struct vfsconf *nvfsp)
313{
55e303ae
A
314 int slot;
315 struct vfsconf *slotp;
1c79356b 316
55e303ae 317 if (nvfsp == NULL) /* overkill */
1c79356b 318 return (-1);
1c79356b 319
55e303ae
A
320 /*
321 * Find the next empty slot; we recognize an empty slot by a
322 * NULL-valued ->vfc_vfsops, so if we delete a VFS, we must
323 * ensure we set the entry back to NULL.
324 */
325 for (slot = 0; slot < maxvfsslots; slot++) {
326 if (vfsconf[slot].vfc_vfsops == NULL)
327 break;
328 }
329 if (slot == maxvfsslots) {
330 /* out of static slots; allocate one instead */
331 MALLOC(slotp, struct vfsconf *, sizeof(struct vfsconf),
332 M_TEMP, M_WAITOK);
333 } else {
334 slotp = &vfsconf[slot];
335 }
336
337 /*
338 * Replace the contents of the next empty slot with the contents
339 * of the provided nvfsp.
340 *
341 * Note; Takes advantage of the fact that 'slot' was left
342 * with the value of 'maxvfslots' in the allocation case.
343 */
344 bcopy(nvfsp, slotp, sizeof(struct vfsconf));
345 if (slot != 0) {
346 slotp->vfc_next = vfsconf[slot - 1].vfc_next;
347 vfsconf[slot - 1].vfc_next = slotp;
348 } else {
349 slotp->vfc_next = NULL;
350 }
1c79356b 351 numused_vfsslots++;
55e303ae
A
352
353 /*
354 * Call through the ->vfs_init(); use slotp instead of nvfsp,
355 * so that if the FS cares where it's instance record is, it
356 * can find it later.
357 *
358 * XXX All code that calls ->vfs_init treats it as if it
359 * XXX returns a "void', and can never fail.
360 */
1c79356b 361 if (nvfsp->vfc_vfsops->vfs_init)
55e303ae
A
362 (*nvfsp->vfc_vfsops->vfs_init)(slotp);
363
1c79356b
A
364 return(0);
365}
366
55e303ae
A
367/*
368 * Name: vfsconf_del
369 *
370 * Description: Remove a filesystem from the vfsconf list by name.
371 * If no such filesystem exists, return an error.
372 *
373 * Parameter: fs_name name of VFS to remove
374 *
375 * Returns: 0 Success
376 * -1 Failure
377 *
378 * Notes: Hopefully all filesystems have unique names.
379 */
1c79356b
A
380int
381vfsconf_del(char * fs_name)
382{
55e303ae
A
383 struct vfsconf **vcpp;
384 struct vfsconf *vcdelp;
1c79356b 385
55e303ae
A
386 /*
387 * Traverse the list looking for fs_name; if found, *vcpp
388 * will contain the address of the pointer to the entry to
389 * be removed.
390 */
391 for( vcpp = &vfsconf; *vcpp; vcpp = &(*vcpp)->vfc_next) {
392 if (strcmp( (*vcpp)->vfc_name, fs_name) == 0)
1c79356b 393 break;
1c79356b 394 }
55e303ae
A
395
396 if (*vcpp == NULL) {
1c79356b
A
397 /* XXX need real error code for entry not found */
398 return(-1);
55e303ae
A
399 }
400
401 /* Unlink entry */
402 vcdelp = *vcpp;
403 *vcpp = (*vcpp)->vfc_next;
404
405 /*
406 * Is this an entry from our static table? We find out by
407 * seeing if the pointer to the object to be deleted places
408 * the object in the address space containing the table (or not).
409 */
410 if (vcdelp >= vfsconf && vcdelp < (vfsconf + maxvfsslots)) { /* Y */
411 /* Mark as empty for vfscon_add() */
412 bzero(vcdelp, sizeof(struct vfsconf));
413 numused_vfsslots--;
414 } else { /* N */
415 /*
416 * This entry was dynamically allocated; we must free it;
417 * we would prefer to have just linked the caller's
418 * vfsconf onto our list, but it may not be persistent
419 * because of the previous (copying) implementation.
420 */
421 FREE(vcdelp, M_TEMP);
422 }
423
1c79356b
A
424 return(0);
425}