1 /* 2 * Copyright (c) 1989, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed 6 * to Berkeley by John Heidemann of the UCLA Ficus project. 7 * 8 * Source: * @(#)i405_init.c 2.10 92/04/27 UCLA Ficus project 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 * 4. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 * 38 * @(#)vfs_init.c 8.3 (Berkeley) 1/4/94 39 * $Id: vfs_init.c,v 1.45 1999/03/07 16:06:41 dfr Exp $ 40 */ 41 42 43 #include <sys/param.h> 44 #include <sys/systm.h> 45 #include <sys/kernel.h> 46 #include <sys/mount.h> 47 #include <sys/sysctl.h> 48 #include <sys/vnode.h> 49 #include <sys/malloc.h> 50 #include <vm/vm_zone.h> 51 52 53 MALLOC_DEFINE(M_VNODE, "vnodes", "Dynamically allocated vnodes"); 54 55 /* 56 * XXX this bloat just exands the sysctl__vfs linker set a little so that 57 * we can attach sysctls for VFS modules without expanding the linker set. 58 * Currently (1998/09/06), only one VFS uses sysctls, so 2 extra linker 59 * set slots are more than sufficient. 60 */ 61 static int mod_xx; 62 SYSCTL_INT(_vfs, OID_AUTO, mod0, CTLFLAG_RD, &mod_xx, 0, ""); 63 SYSCTL_INT(_vfs, OID_AUTO, mod1, CTLFLAG_RD, &mod_xx, 0, ""); 64 65 /* 66 * Zone for namei 67 */ 68 struct vm_zone *namei_zone; 69 70 /* 71 * vfs_init.c 72 * 73 * Allocate and fill in operations vectors. 74 * 75 * An undocumented feature of this approach to defining operations is that 76 * there can be multiple entries in vfs_opv_descs for the same operations 77 * vector. This allows third parties to extend the set of operations 78 * supported by another layer in a binary compatibile way. For example, 79 * assume that NFS needed to be modified to support Ficus. NFS has an entry 80 * (probably nfs_vnopdeop_decls) declaring all the operations NFS supports by 81 * default. Ficus could add another entry (ficus_nfs_vnodeop_decl_entensions) 82 * listing those new operations Ficus adds to NFS, all without modifying the 83 * NFS code. (Of couse, the OTW NFS protocol still needs to be munged, but 84 * that is a(whole)nother story.) This is a feature. 85 */ 86 87 /* Table of known vnodeop vectors (list of VFS vnode vectors) */ 88 static const struct vnodeopv_desc **vnodeopv_descs; 89 static int vnodeopv_num; 90 91 /* Table of known descs (list of vnode op handlers "vop_access_desc") */ 92 static struct vnodeop_desc **vfs_op_descs; 93 static int *vfs_op_desc_refs; /* reference counts */ 94 static int num_op_descs; 95 static int vfs_opv_numops; 96 97 static void 98 vfs_opv_recalc(void) 99 { 100 int i, j; 101 vop_t ***opv_desc_vector_p; 102 vop_t **opv_desc_vector; 103 struct vnodeopv_entry_desc *opve_descp; 104 const struct vnodeopv_desc *opv; 105 106 if (vfs_op_descs == NULL) 107 panic("vfs_opv_recalc called with null vfs_op_descs"); 108 109 /* 110 * Run through and make sure all known descs have an offset 111 * 112 * vop_default_desc is hardwired at offset 1, and offset 0 113 * is a panic sanity check. 114 */ 115 vfs_opv_numops = 0; 116 for (i = 0; i < num_op_descs; i++) 117 if (vfs_opv_numops < (vfs_op_descs[i]->vdesc_offset + 1)) 118 vfs_opv_numops = vfs_op_descs[i]->vdesc_offset + 1; 119 for (i = 0; i < num_op_descs; i++) 120 if (vfs_op_descs[i]->vdesc_offset == 0) 121 vfs_op_descs[i]->vdesc_offset = vfs_opv_numops++; 122 /* 123 * Allocate and fill in the vectors 124 */ 125 for (i = 0; i < vnodeopv_num; i++) { 126 opv = vnodeopv_descs[i]; 127 opv_desc_vector_p = opv->opv_desc_vector_p; 128 if (*opv_desc_vector_p) 129 FREE(*opv_desc_vector_p, M_VNODE); 130 MALLOC(*opv_desc_vector_p, vop_t **, 131 vfs_opv_numops * sizeof(vop_t *), M_VNODE, M_WAITOK); 132 if (*opv_desc_vector_p == NULL) 133 panic("no memory for vop_t ** vector"); 134 bzero(*opv_desc_vector_p, vfs_opv_numops * sizeof(vop_t *)); 135 136 /* Fill in, with slot 0 being panic */ 137 opv_desc_vector = *opv_desc_vector_p; 138 opv_desc_vector[0] = (vop_t *)vop_panic; 139 for (j = 0; opv->opv_desc_ops[j].opve_op; j++) { 140 opve_descp = &(opv->opv_desc_ops[j]); 141 opv_desc_vector[opve_descp->opve_op->vdesc_offset] = 142 opve_descp->opve_impl; 143 } 144 145 /* Replace unfilled routines with their default (slot 1). */ 146 opv_desc_vector = *(opv->opv_desc_vector_p); 147 if (opv_desc_vector[1] == NULL) 148 panic("vfs_opv_recalc: vector without a default."); 149 for (j = 0; j < vfs_opv_numops; j++) 150 if (opv_desc_vector[j] == NULL) 151 opv_desc_vector[j] = opv_desc_vector[1]; 152 } 153 } 154 155 void 156 vfs_add_vnodeops(const void *data) 157 { 158 const struct vnodeopv_desc *opv; 159 const struct vnodeopv_desc **newopv; 160 struct vnodeop_desc **newop; 161 int *newref; 162 vop_t **opv_desc_vector; 163 struct vnodeop_desc *desc; 164 int i, j; 165 166 opv = (const struct vnodeopv_desc *)data; 167 MALLOC(newopv, const struct vnodeopv_desc **, 168 (vnodeopv_num + 1) * sizeof(*newopv), M_VNODE, M_WAITOK); 169 if (newopv == NULL) 170 panic("vfs_add_vnodeops: no memory"); 171 if (vnodeopv_descs) { 172 bcopy(vnodeopv_descs, newopv, vnodeopv_num * sizeof(*newopv)); 173 FREE(vnodeopv_descs, M_VNODE); 174 } 175 newopv[vnodeopv_num] = opv; 176 vnodeopv_descs = newopv; 177 vnodeopv_num++; 178 179 /* See if we have turned up a new vnode op desc */ 180 opv_desc_vector = *(opv->opv_desc_vector_p); 181 for (i = 0; (desc = opv->opv_desc_ops[i].opve_op); i++) { 182 for (j = 0; j < num_op_descs; j++) { 183 if (desc == vfs_op_descs[j]) { 184 /* found it, increase reference count */ 185 vfs_op_desc_refs[j]++; 186 break; 187 } 188 } 189 if (j == num_op_descs) { 190 /* not found, new entry */ 191 MALLOC(newop, struct vnodeop_desc **, 192 (num_op_descs + 1) * sizeof(*newop), 193 M_VNODE, M_WAITOK); 194 if (newop == NULL) 195 panic("vfs_add_vnodeops: no memory for desc"); 196 /* new reference count (for unload) */ 197 MALLOC(newref, int *, 198 (num_op_descs + 1) * sizeof(*newref), 199 M_VNODE, M_WAITOK); 200 if (newref == NULL) 201 panic("vfs_add_vnodeops: no memory for refs"); 202 if (vfs_op_descs) { 203 bcopy(vfs_op_descs, newop, 204 num_op_descs * sizeof(*newop)); 205 FREE(vfs_op_descs, M_VNODE); 206 } 207 if (vfs_op_desc_refs) { 208 bcopy(vfs_op_desc_refs, newref, 209 num_op_descs * sizeof(*newref)); 210 FREE(vfs_op_desc_refs, M_VNODE); 211 } 212 newop[num_op_descs] = desc; 213 newref[num_op_descs] = 1; 214 vfs_op_descs = newop; 215 vfs_op_desc_refs = newref; 216 num_op_descs++; 217 } 218 } 219 vfs_opv_recalc(); 220 } 221 222 void 223 vfs_rm_vnodeops(const void *data) 224 { 225 const struct vnodeopv_desc *opv; 226 const struct vnodeopv_desc **newopv; 227 struct vnodeop_desc **newop; 228 int *newref; 229 vop_t **opv_desc_vector; 230 struct vnodeop_desc *desc; 231 int i, j, k; 232 233 opv = (const struct vnodeopv_desc *)data; 234 /* Lower ref counts on descs in the table and release if zero */ 235 opv_desc_vector = *(opv->opv_desc_vector_p); 236 for (i = 0; (desc = opv->opv_desc_ops[i].opve_op); i++) { 237 for (j = 0; j < num_op_descs; j++) { 238 if (desc == vfs_op_descs[j]) { 239 /* found it, decrease reference count */ 240 vfs_op_desc_refs[j]--; 241 break; 242 } 243 } 244 for (j = 0; j < num_op_descs; j++) { 245 if (vfs_op_desc_refs[j] > 0) 246 continue; 247 if (vfs_op_desc_refs[j] < 0) 248 panic("vfs_remove_vnodeops: negative refcnt"); 249 MALLOC(newop, struct vnodeop_desc **, 250 (num_op_descs - 1) * sizeof(*newop), 251 M_VNODE, M_WAITOK); 252 if (newop == NULL) 253 panic("vfs_remove_vnodeops: no memory for desc"); 254 /* new reference count (for unload) */ 255 MALLOC(newref, int *, 256 (num_op_descs - 1) * sizeof(*newref), 257 M_VNODE, M_WAITOK); 258 if (newref == NULL) 259 panic("vfs_remove_vnodeops: no memory for refs"); 260 for (k = j; k < (num_op_descs - 1); k++) { 261 vfs_op_descs[k] = vfs_op_descs[k + 1]; 262 vfs_op_desc_refs[k] = vfs_op_desc_refs[k + 1]; 263 } 264 bcopy(vfs_op_descs, newop, 265 (num_op_descs - 1) * sizeof(*newop)); 266 bcopy(vfs_op_desc_refs, newref, 267 (num_op_descs - 1) * sizeof(*newref)); 268 FREE(vfs_op_descs, M_VNODE); 269 FREE(vfs_op_desc_refs, M_VNODE); 270 vfs_op_descs = newop; 271 vfs_op_desc_refs = newref; 272 num_op_descs--; 273 } 274 } 275 276 for (i = 0; i < vnodeopv_num; i++) { 277 if (vnodeopv_descs[i] == opv) { 278 for (j = i; j < (vnodeopv_num - 1); j++) 279 vnodeopv_descs[j] = vnodeopv_descs[j + 1]; 280 break; 281 } 282 } 283 if (i == vnodeopv_num) 284 panic("vfs_remove_vnodeops: opv not found"); 285 MALLOC(newopv, const struct vnodeopv_desc **, 286 (vnodeopv_num - 1) * sizeof(*newopv), M_VNODE, M_WAITOK); 287 if (newopv == NULL) 288 panic("vfs_remove_vnodeops: no memory"); 289 bcopy(vnodeopv_descs, newopv, (vnodeopv_num - 1) * sizeof(*newopv)); 290 FREE(vnodeopv_descs, M_VNODE); 291 vnodeopv_descs = newopv; 292 vnodeopv_num--; 293 294 vfs_opv_recalc(); 295 } 296 297 /* 298 * Routines having to do with the management of the vnode table. 299 */ 300 struct vattr va_null; 301 302 /* 303 * Initialize the vnode structures and initialize each file system type. 304 */ 305 /* ARGSUSED*/ 306 static void 307 vfsinit(void *dummy) 308 { 309 310 namei_zone = zinit("NAMEI", MAXPATHLEN, 0, 0, 2); 311 312 /* 313 * Initialize the vnode table 314 */ 315 vntblinit(); 316 /* 317 * Initialize the vnode name cache 318 */ 319 nchinit(); 320 /* 321 * Initialize each file system type. 322 * Vfs type numbers must be distinct from VFS_GENERIC (and VFS_VFSCONF). 323 */ 324 vattr_null(&va_null); 325 maxvfsconf = VFS_GENERIC + 1; 326 } 327 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_FIRST, vfsinit, NULL) 328 329 int 330 vfs_register(struct vfsconf *vfc) 331 { 332 struct sysctl_oid *oidp; 333 struct vfsconf *vfsp; 334 335 vfsp = NULL; 336 if (vfsconf) 337 for (vfsp = vfsconf; vfsp->vfc_next; vfsp = vfsp->vfc_next) 338 if (strcmp(vfc->vfc_name, vfsp->vfc_name) == 0) 339 return EEXIST; 340 341 vfc->vfc_typenum = maxvfsconf++; 342 if (vfsp) 343 vfsp->vfc_next = vfc; 344 else 345 vfsconf = vfc; 346 vfc->vfc_next = NULL; 347 348 /* 349 * If this filesystem has a sysctl node under vfs 350 * (i.e. vfs.xxfs), then change the oid number of that node to 351 * match the filesystem's type number. This allows user code 352 * which uses the type number to read sysctl variables defined 353 * by the filesystem to continue working. Since the oids are 354 * in a sorted list, we need to make sure the order is 355 * preserved by re-registering the oid after modifying its 356 * number. 357 */ 358 for (oidp = SLIST_FIRST(&sysctl__vfs_children); oidp; 359 oidp = SLIST_NEXT(oidp, oid_link)) 360 if (strcmp(oidp->oid_name, vfc->vfc_name) == 0) { 361 sysctl_unregister_oid(oidp); 362 oidp->oid_number = vfc->vfc_typenum; 363 sysctl_register_oid(oidp); 364 } 365 366 /* 367 * Call init function for this VFS... 368 */ 369 (*(vfc->vfc_vfsops->vfs_init))(vfc); 370 371 return 0; 372 } 373 374 375 int 376 vfs_unregister(struct vfsconf *vfc) 377 { 378 struct vfsconf *vfsp, *prev_vfsp; 379 int error, i, maxtypenum; 380 381 i = vfc->vfc_typenum; 382 383 prev_vfsp = NULL; 384 for (vfsp = vfsconf; vfsp; 385 prev_vfsp = vfsp, vfsp = vfsp->vfc_next) { 386 if (!strcmp(vfc->vfc_name, vfsp->vfc_name)) 387 break; 388 } 389 if (vfsp == NULL) 390 return EINVAL; 391 if (vfsp->vfc_refcount) 392 return EBUSY; 393 if (vfc->vfc_vfsops->vfs_uninit != NULL) { 394 error = (*vfc->vfc_vfsops->vfs_uninit)(vfsp); 395 if (error) 396 return (error); 397 } 398 if (prev_vfsp) 399 prev_vfsp->vfc_next = vfsp->vfc_next; 400 else 401 vfsconf = vfsp->vfc_next; 402 maxtypenum = VFS_GENERIC; 403 for (vfsp = vfsconf; vfsp != NULL; vfsp = vfsp->vfc_next) 404 if (maxtypenum < vfsp->vfc_typenum) 405 maxtypenum = vfsp->vfc_typenum; 406 maxvfsconf = maxtypenum + 1; 407 return 0; 408 } 409 410 int 411 vfs_modevent(module_t mod, int type, void *data) 412 { 413 struct vfsconf *vfc; 414 int error = 0; 415 416 vfc = (struct vfsconf *)data; 417 418 switch (type) { 419 case MOD_LOAD: 420 if (vfc) 421 error = vfs_register(vfc); 422 break; 423 424 case MOD_UNLOAD: 425 if (vfc) 426 error = vfs_unregister(vfc); 427 break; 428 default: /* including MOD_SHUTDOWN */ 429 break; 430 } 431 return (error); 432 } 433