]> git.saurik.com Git - apple/xnu.git/blame - bsd/vfs/vfs_init.c
xnu-517.9.4.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 *
e5568f75
A
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.
1c79356b 11 *
e5568f75
A
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
1c79356b
A
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
e5568f75
A
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.
1c79356b
A
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.h>
66#include <sys/time.h>
67#include <sys/vm.h>
68#include <sys/vnode.h>
69#include <sys/stat.h>
70#include <sys/namei.h>
71#include <sys/ucred.h>
72#include <sys/buf.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
86extern uid_t console_user;
87extern struct vnodeopv_desc *vfs_opv_descs[];
88 /* a list of lists of vnodeops defns */
89extern 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 */
97int vfs_opv_numops;
98
99typedef (*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 */
105int
106vn_default_error()
107{
108
109 return (EOPNOTSUPP);
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 */
128void
129vfs_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) {
1c79356b 146 MALLOC(*opv_desc_vector_p, PFI*,
55e303ae 147 vfs_opv_numops*sizeof(PFI), M_TEMP, M_WAITOK);
1c79356b
A
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(vop_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(vop_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(vop_default)];
205 }
206}
207
208/*
209 * Initialize known vnode operations vectors.
210 */
211void
212vfs_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 */
236extern struct vnodeops dead_vnodeops;
237extern struct vnodeops spec_vnodeops;
238struct vattr va_null;
239
240/*
241 * Initialize the vnode structures and initialize each file system type.
242 */
243vfsinit()
244{
245 struct vfsconf *vfsp;
246 int i, maxtypenum;
247
248 /*
249 * Initialize the "console user" for access purposes:
250 */
251 console_user = (uid_t)0;
252
253 /*
254 * Initialize the vnode table
255 */
256 vntblinit();
55e303ae
A
257 /*
258 * Initialize the filesystem event mechanism.
259 */
260 vfs_event_init();
1c79356b
A
261 /*
262 * Initialize the vnode name cache
263 */
264 nchinit();
265 /*
266 * Build vnode operation vectors.
267 */
268 vfs_op_init();
269 vfs_opv_init(); /* finish the job */
270 /*
55e303ae
A
271 * Initialize each file system type in the static list,
272 * until the first NULL ->vfs_vfsops is encountered.
1c79356b
A
273 */
274 vattr_null(&va_null);
275 numused_vfsslots = maxtypenum = 0;
276 for (vfsp = vfsconf, i = 0; i < maxvfsconf; i++, vfsp++) {
277 if (vfsp->vfc_vfsops == (struct vfsops *)0)
278 break;
279 if (i) vfsconf[i-1].vfc_next = vfsp;
280 if (maxtypenum <= vfsp->vfc_typenum)
281 maxtypenum = vfsp->vfc_typenum + 1;
282 (*vfsp->vfc_vfsops->vfs_init)(vfsp);
283 numused_vfsslots++;
284 }
285 /* next vfc_typenum to be used */
286 maxvfsconf = maxtypenum;
287}
288
55e303ae
A
289/*
290 * Name: vfsconf_add
291 *
292 * Description: Add a filesystem to the vfsconf list at the first
293 * unused slot. If no slots are available, return an
294 * error.
295 *
296 * Parameter: nvfsp vfsconf for VFS to add
297 *
298 * Returns: 0 Success
299 * -1 Failure
300 *
301 * Notes: The vfsconf should be treated as a linked list by
302 * all external references, as the implementation is
303 * expected to change in the future. The linkage is
304 * through ->vfc_next, and the list is NULL terminated.
305 *
306 * Warning: This code assumes that vfsconf[0] is non-empty.
307 */
1c79356b
A
308int
309vfsconf_add(struct vfsconf *nvfsp)
310{
55e303ae
A
311 int slot;
312 struct vfsconf *slotp;
1c79356b 313
55e303ae 314 if (nvfsp == NULL) /* overkill */
1c79356b 315 return (-1);
1c79356b 316
55e303ae
A
317 /*
318 * Find the next empty slot; we recognize an empty slot by a
319 * NULL-valued ->vfc_vfsops, so if we delete a VFS, we must
320 * ensure we set the entry back to NULL.
321 */
322 for (slot = 0; slot < maxvfsslots; slot++) {
323 if (vfsconf[slot].vfc_vfsops == NULL)
324 break;
325 }
326 if (slot == maxvfsslots) {
327 /* out of static slots; allocate one instead */
328 MALLOC(slotp, struct vfsconf *, sizeof(struct vfsconf),
329 M_TEMP, M_WAITOK);
330 } else {
331 slotp = &vfsconf[slot];
332 }
333
334 /*
335 * Replace the contents of the next empty slot with the contents
336 * of the provided nvfsp.
337 *
338 * Note; Takes advantage of the fact that 'slot' was left
339 * with the value of 'maxvfslots' in the allocation case.
340 */
341 bcopy(nvfsp, slotp, sizeof(struct vfsconf));
342 if (slot != 0) {
343 slotp->vfc_next = vfsconf[slot - 1].vfc_next;
344 vfsconf[slot - 1].vfc_next = slotp;
345 } else {
346 slotp->vfc_next = NULL;
347 }
1c79356b 348 numused_vfsslots++;
55e303ae
A
349
350 /*
351 * Call through the ->vfs_init(); use slotp instead of nvfsp,
352 * so that if the FS cares where it's instance record is, it
353 * can find it later.
354 *
355 * XXX All code that calls ->vfs_init treats it as if it
356 * XXX returns a "void', and can never fail.
357 */
1c79356b 358 if (nvfsp->vfc_vfsops->vfs_init)
55e303ae
A
359 (*nvfsp->vfc_vfsops->vfs_init)(slotp);
360
1c79356b
A
361 return(0);
362}
363
55e303ae
A
364/*
365 * Name: vfsconf_del
366 *
367 * Description: Remove a filesystem from the vfsconf list by name.
368 * If no such filesystem exists, return an error.
369 *
370 * Parameter: fs_name name of VFS to remove
371 *
372 * Returns: 0 Success
373 * -1 Failure
374 *
375 * Notes: Hopefully all filesystems have unique names.
376 */
1c79356b
A
377int
378vfsconf_del(char * fs_name)
379{
55e303ae
A
380 struct vfsconf **vcpp;
381 struct vfsconf *vcdelp;
1c79356b 382
55e303ae
A
383 /*
384 * Traverse the list looking for fs_name; if found, *vcpp
385 * will contain the address of the pointer to the entry to
386 * be removed.
387 */
388 for( vcpp = &vfsconf; *vcpp; vcpp = &(*vcpp)->vfc_next) {
389 if (strcmp( (*vcpp)->vfc_name, fs_name) == 0)
1c79356b 390 break;
1c79356b 391 }
55e303ae
A
392
393 if (*vcpp == NULL) {
1c79356b
A
394 /* XXX need real error code for entry not found */
395 return(-1);
55e303ae
A
396 }
397
398 /* Unlink entry */
399 vcdelp = *vcpp;
400 *vcpp = (*vcpp)->vfc_next;
401
402 /*
403 * Is this an entry from our static table? We find out by
404 * seeing if the pointer to the object to be deleted places
405 * the object in the address space containing the table (or not).
406 */
407 if (vcdelp >= vfsconf && vcdelp < (vfsconf + maxvfsslots)) { /* Y */
408 /* Mark as empty for vfscon_add() */
409 bzero(vcdelp, sizeof(struct vfsconf));
410 numused_vfsslots--;
411 } else { /* N */
412 /*
413 * This entry was dynamically allocated; we must free it;
414 * we would prefer to have just linked the caller's
415 * vfsconf onto our list, but it may not be persistent
416 * because of the previous (copying) implementation.
417 */
418 FREE(vcdelp, M_TEMP);
419 }
420
1c79356b
A
421 return(0);
422}