vfs_init.c revision 75858
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 * $FreeBSD: head/sys/kern/vfs_init.c 75858 2001-04-23 09:05:15Z grog $ 40 */ 41 42 43#include <sys/param.h> 44#include <sys/systm.h> 45#include <sys/kernel.h> 46#include <net/radix.h> 47#include <sys/socket.h> 48#include <sys/mount.h> 49#include <sys/sysctl.h> 50#include <sys/vnode.h> 51#include <sys/malloc.h> 52#include <vm/vm_zone.h> 53 54 55MALLOC_DEFINE(M_VNODE, "vnodes", "Dynamically allocated vnodes"); 56 57/* 58 * The highest defined VFS number. 59 */ 60int maxvfsconf = VFS_GENERIC + 1; 61struct vfsconf *vfsconf; 62 63/* 64 * vfs_init.c 65 * 66 * Allocate and fill in operations vectors. 67 * 68 * An undocumented feature of this approach to defining operations is that 69 * there can be multiple entries in vfs_opv_descs for the same operations 70 * vector. This allows third parties to extend the set of operations 71 * supported by another layer in a binary compatibile way. For example, 72 * assume that NFS needed to be modified to support Ficus. NFS has an entry 73 * (probably nfs_vnopdeop_decls) declaring all the operations NFS supports by 74 * default. Ficus could add another entry (ficus_nfs_vnodeop_decl_entensions) 75 * listing those new operations Ficus adds to NFS, all without modifying the 76 * NFS code. (Of couse, the OTW NFS protocol still needs to be munged, but 77 * that is a(whole)nother story.) This is a feature. 78 */ 79 80/* Table of known vnodeop vectors (list of VFS vnode vectors) */ 81static const struct vnodeopv_desc **vnodeopv_descs; 82static int vnodeopv_num; 83 84/* Table of known descs (list of vnode op handlers "vop_access_desc") */ 85static struct vnodeop_desc **vfs_op_descs; 86static int *vfs_op_desc_refs; /* reference counts */ 87static int num_op_descs; 88static int vfs_opv_numops; 89 90static void 91vfs_opv_recalc(void) 92{ 93 int i, j; 94 vop_t ***opv_desc_vector_p; 95 vop_t **opv_desc_vector; 96 struct vnodeopv_entry_desc *opve_descp; 97 const struct vnodeopv_desc *opv; 98 99 if (vfs_op_descs == NULL) 100 panic("vfs_opv_recalc called with null vfs_op_descs"); 101 102 /* 103 * Run through and make sure all known descs have an offset 104 * 105 * vop_default_desc is hardwired at offset 1, and offset 0 106 * is a panic sanity check. 107 */ 108 vfs_opv_numops = 0; 109 for (i = 0; i < num_op_descs; i++) 110 if (vfs_opv_numops < (vfs_op_descs[i]->vdesc_offset + 1)) 111 vfs_opv_numops = vfs_op_descs[i]->vdesc_offset + 1; 112 for (i = 0; i < num_op_descs; i++) 113 if (vfs_op_descs[i]->vdesc_offset == 0) 114 vfs_op_descs[i]->vdesc_offset = vfs_opv_numops++; 115 /* 116 * Allocate and fill in the vectors 117 */ 118 for (i = 0; i < vnodeopv_num; i++) { 119 opv = vnodeopv_descs[i]; 120 opv_desc_vector_p = opv->opv_desc_vector_p; 121 if (*opv_desc_vector_p) 122 FREE(*opv_desc_vector_p, M_VNODE); 123 MALLOC(*opv_desc_vector_p, vop_t **, 124 vfs_opv_numops * sizeof(vop_t *), M_VNODE, 125 M_WAITOK | M_ZERO); 126 if (*opv_desc_vector_p == NULL) 127 panic("no memory for vop_t ** vector"); 128 129 /* Fill in, with slot 0 being to return EOPNOTSUPP */ 130 opv_desc_vector = *opv_desc_vector_p; 131 opv_desc_vector[0] = (vop_t *)vop_eopnotsupp; 132 for (j = 0; opv->opv_desc_ops[j].opve_op; j++) { 133 opve_descp = &(opv->opv_desc_ops[j]); 134 opv_desc_vector[opve_descp->opve_op->vdesc_offset] = 135 opve_descp->opve_impl; 136 } 137 138 /* Replace unfilled routines with their default (slot 1). */ 139 opv_desc_vector = *(opv->opv_desc_vector_p); 140 if (opv_desc_vector[1] == NULL) 141 panic("vfs_opv_recalc: vector without a default."); 142 for (j = 0; j < vfs_opv_numops; j++) 143 if (opv_desc_vector[j] == NULL) 144 opv_desc_vector[j] = opv_desc_vector[1]; 145 } 146} 147 148void 149vfs_add_vnodeops(const void *data) 150{ 151 const struct vnodeopv_desc *opv; 152 const struct vnodeopv_desc **newopv; 153 struct vnodeop_desc **newop; 154 int *newref; 155 vop_t **opv_desc_vector; 156 struct vnodeop_desc *desc; 157 int i, j; 158 159 opv = (const struct vnodeopv_desc *)data; 160 MALLOC(newopv, const struct vnodeopv_desc **, 161 (vnodeopv_num + 1) * sizeof(*newopv), M_VNODE, M_WAITOK); 162 if (newopv == NULL) 163 panic("vfs_add_vnodeops: no memory"); 164 if (vnodeopv_descs) { 165 bcopy(vnodeopv_descs, newopv, vnodeopv_num * sizeof(*newopv)); 166 FREE(vnodeopv_descs, M_VNODE); 167 } 168 newopv[vnodeopv_num] = opv; 169 vnodeopv_descs = newopv; 170 vnodeopv_num++; 171 172 /* See if we have turned up a new vnode op desc */ 173 opv_desc_vector = *(opv->opv_desc_vector_p); 174 for (i = 0; (desc = opv->opv_desc_ops[i].opve_op); i++) { 175 for (j = 0; j < num_op_descs; j++) { 176 if (desc == vfs_op_descs[j]) { 177 /* found it, increase reference count */ 178 vfs_op_desc_refs[j]++; 179 break; 180 } 181 } 182 if (j == num_op_descs) { 183 /* not found, new entry */ 184 MALLOC(newop, struct vnodeop_desc **, 185 (num_op_descs + 1) * sizeof(*newop), 186 M_VNODE, M_WAITOK); 187 if (newop == NULL) 188 panic("vfs_add_vnodeops: no memory for desc"); 189 /* new reference count (for unload) */ 190 MALLOC(newref, int *, 191 (num_op_descs + 1) * sizeof(*newref), 192 M_VNODE, M_WAITOK); 193 if (newref == NULL) 194 panic("vfs_add_vnodeops: no memory for refs"); 195 if (vfs_op_descs) { 196 bcopy(vfs_op_descs, newop, 197 num_op_descs * sizeof(*newop)); 198 FREE(vfs_op_descs, M_VNODE); 199 } 200 if (vfs_op_desc_refs) { 201 bcopy(vfs_op_desc_refs, newref, 202 num_op_descs * sizeof(*newref)); 203 FREE(vfs_op_desc_refs, M_VNODE); 204 } 205 newop[num_op_descs] = desc; 206 newref[num_op_descs] = 1; 207 vfs_op_descs = newop; 208 vfs_op_desc_refs = newref; 209 num_op_descs++; 210 } 211 } 212 vfs_opv_recalc(); 213} 214 215void 216vfs_rm_vnodeops(const void *data) 217{ 218 const struct vnodeopv_desc *opv; 219 const struct vnodeopv_desc **newopv; 220 struct vnodeop_desc **newop; 221 int *newref; 222 vop_t **opv_desc_vector; 223 struct vnodeop_desc *desc; 224 int i, j, k; 225 226 opv = (const struct vnodeopv_desc *)data; 227 /* Lower ref counts on descs in the table and release if zero */ 228 opv_desc_vector = *(opv->opv_desc_vector_p); 229 for (i = 0; (desc = opv->opv_desc_ops[i].opve_op); i++) { 230 for (j = 0; j < num_op_descs; j++) { 231 if (desc == vfs_op_descs[j]) { 232 /* found it, decrease reference count */ 233 vfs_op_desc_refs[j]--; 234 break; 235 } 236 } 237 for (j = 0; j < num_op_descs; j++) { 238 if (vfs_op_desc_refs[j] > 0) 239 continue; 240 if (vfs_op_desc_refs[j] < 0) 241 panic("vfs_remove_vnodeops: negative refcnt"); 242 MALLOC(newop, struct vnodeop_desc **, 243 (num_op_descs - 1) * sizeof(*newop), 244 M_VNODE, M_WAITOK); 245 if (newop == NULL) 246 panic("vfs_remove_vnodeops: no memory for desc"); 247 /* new reference count (for unload) */ 248 MALLOC(newref, int *, 249 (num_op_descs - 1) * sizeof(*newref), 250 M_VNODE, M_WAITOK); 251 if (newref == NULL) 252 panic("vfs_remove_vnodeops: no memory for refs"); 253 for (k = j; k < (num_op_descs - 1); k++) { 254 vfs_op_descs[k] = vfs_op_descs[k + 1]; 255 vfs_op_desc_refs[k] = vfs_op_desc_refs[k + 1]; 256 } 257 bcopy(vfs_op_descs, newop, 258 (num_op_descs - 1) * sizeof(*newop)); 259 bcopy(vfs_op_desc_refs, newref, 260 (num_op_descs - 1) * sizeof(*newref)); 261 FREE(vfs_op_descs, M_VNODE); 262 FREE(vfs_op_desc_refs, M_VNODE); 263 vfs_op_descs = newop; 264 vfs_op_desc_refs = newref; 265 num_op_descs--; 266 } 267 } 268 269 for (i = 0; i < vnodeopv_num; i++) { 270 if (vnodeopv_descs[i] == opv) { 271 for (j = i; j < (vnodeopv_num - 1); j++) 272 vnodeopv_descs[j] = vnodeopv_descs[j + 1]; 273 break; 274 } 275 } 276 if (i == vnodeopv_num) 277 panic("vfs_remove_vnodeops: opv not found"); 278 MALLOC(newopv, const struct vnodeopv_desc **, 279 (vnodeopv_num - 1) * sizeof(*newopv), M_VNODE, M_WAITOK); 280 if (newopv == NULL) 281 panic("vfs_remove_vnodeops: no memory"); 282 bcopy(vnodeopv_descs, newopv, (vnodeopv_num - 1) * sizeof(*newopv)); 283 FREE(vnodeopv_descs, M_VNODE); 284 vnodeopv_descs = newopv; 285 vnodeopv_num--; 286 287 vfs_opv_recalc(); 288} 289 290/* 291 * Routines having to do with the management of the vnode table. 292 */ 293struct vattr va_null; 294 295/* 296 * Initialize the vnode structures and initialize each file system type. 297 */ 298/* ARGSUSED*/ 299static void 300vfsinit(void *dummy) 301{ 302 303 vattr_null(&va_null); 304} 305SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_FIRST, vfsinit, NULL) 306 307int 308vfs_register(struct vfsconf *vfc) 309{ 310 struct sysctl_oid *oidp; 311 struct vfsconf *vfsp; 312 313 vfsp = NULL; 314 if (vfsconf) 315 for (vfsp = vfsconf; vfsp->vfc_next; vfsp = vfsp->vfc_next) 316 if (strcmp(vfc->vfc_name, vfsp->vfc_name) == 0) 317 return EEXIST; 318 319 vfc->vfc_typenum = maxvfsconf++; 320 if (vfsp) 321 vfsp->vfc_next = vfc; 322 else 323 vfsconf = vfc; 324 vfc->vfc_next = NULL; 325 326 /* 327 * If this filesystem has a sysctl node under vfs 328 * (i.e. vfs.xxfs), then change the oid number of that node to 329 * match the filesystem's type number. This allows user code 330 * which uses the type number to read sysctl variables defined 331 * by the filesystem to continue working. Since the oids are 332 * in a sorted list, we need to make sure the order is 333 * preserved by re-registering the oid after modifying its 334 * number. 335 */ 336 SLIST_FOREACH(oidp, &sysctl__vfs_children, oid_link) 337 if (strcmp(oidp->oid_name, vfc->vfc_name) == 0) { 338 sysctl_unregister_oid(oidp); 339 oidp->oid_number = vfc->vfc_typenum; 340 sysctl_register_oid(oidp); 341 } 342 343 /* 344 * Call init function for this VFS... 345 */ 346 (*(vfc->vfc_vfsops->vfs_init))(vfc); 347 348 return 0; 349} 350 351 352int 353vfs_unregister(struct vfsconf *vfc) 354{ 355 struct vfsconf *vfsp, *prev_vfsp; 356 int error, i, maxtypenum; 357 358 i = vfc->vfc_typenum; 359 360 prev_vfsp = NULL; 361 for (vfsp = vfsconf; vfsp; 362 prev_vfsp = vfsp, vfsp = vfsp->vfc_next) { 363 if (!strcmp(vfc->vfc_name, vfsp->vfc_name)) 364 break; 365 } 366 if (vfsp == NULL) 367 return EINVAL; 368 if (vfsp->vfc_refcount) 369 return EBUSY; 370 if (vfc->vfc_vfsops->vfs_uninit != NULL) { 371 error = (*vfc->vfc_vfsops->vfs_uninit)(vfsp); 372 if (error) 373 return (error); 374 } 375 if (prev_vfsp) 376 prev_vfsp->vfc_next = vfsp->vfc_next; 377 else 378 vfsconf = vfsp->vfc_next; 379 maxtypenum = VFS_GENERIC; 380 for (vfsp = vfsconf; vfsp != NULL; vfsp = vfsp->vfc_next) 381 if (maxtypenum < vfsp->vfc_typenum) 382 maxtypenum = vfsp->vfc_typenum; 383 maxvfsconf = maxtypenum + 1; 384 return 0; 385} 386 387int 388vfs_modevent(module_t mod, int type, void *data) 389{ 390 struct vfsconf *vfc; 391 int error = 0; 392 393 vfc = (struct vfsconf *)data; 394 395 switch (type) { 396 case MOD_LOAD: 397 if (vfc) 398 error = vfs_register(vfc); 399 break; 400 401 case MOD_UNLOAD: 402 if (vfc) 403 error = vfs_unregister(vfc); 404 break; 405 default: /* including MOD_SHUTDOWN */ 406 break; 407 } 408 return (error); 409} 410