1 /*- 2 * Copyright (c) 2007 Pawel Jakub Dawidek <pjd@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 #include <sys/param.h> 31 #include <sys/kernel.h> 32 #include <sys/systm.h> 33 #include <sys/sysctl.h> 34 #include <sys/errno.h> 35 #include <sys/jail.h> 36 #include <sys/malloc.h> 37 #include <sys/lock.h> 38 #include <sys/mutex.h> 39 #include <sys/rmlock.h> 40 #include <sys/sx.h> 41 #include <sys/queue.h> 42 #include <sys/proc.h> 43 #include <sys/osd.h> 44 45 /* OSD (Object Specific Data) */ 46 47 /* 48 * Lock key: 49 * (m) osd_module_lock 50 * (o) osd_object_lock 51 * (l) osd_list_lock 52 */ 53 struct osd_master { 54 struct sx osd_module_lock; 55 struct rmlock osd_object_lock; 56 struct mtx osd_list_lock; 57 LIST_HEAD(, osd) osd_list; /* (l) */ 58 osd_destructor_t *osd_destructors; /* (o) */ 59 osd_method_t *osd_methods; /* (m) */ 60 u_int osd_ntslots; /* (m) */ 61 const u_int osd_nmethods; 62 }; 63 64 static MALLOC_DEFINE(M_OSD, "osd", "Object Specific Data"); 65 66 static int osd_debug = 0; 67 SYSCTL_INT(_debug, OID_AUTO, osd, CTLFLAG_RWTUN, &osd_debug, 0, "OSD debug level"); 68 69 #define OSD_DEBUG(...) do { \ 70 if (osd_debug) { \ 71 printf("OSD (%s:%u): ", __func__, __LINE__); \ 72 printf(__VA_ARGS__); \ 73 printf("\n"); \ 74 } \ 75 } while (0) 76 77 static void do_osd_del(u_int type, struct osd *osd, u_int slot, 78 int list_locked); 79 80 /* 81 * List of objects with OSD. 82 */ 83 struct osd_master osdm[OSD_LAST + 1] = { 84 [OSD_JAIL] = { .osd_nmethods = PR_MAXMETHOD }, 85 }; 86 87 static void 88 osd_default_destructor(void *value __unused) 89 { 90 /* Do nothing. */ 91 } 92 93 int 94 osd_register(u_int type, osd_destructor_t destructor, osd_method_t *methods) 95 { 96 void *newptr; 97 u_int i, m; 98 99 KASSERT(type >= OSD_FIRST && type <= OSD_LAST, ("Invalid type.")); 100 101 /* 102 * If no destructor is given, use default one. We need to use some 103 * destructor, because NULL destructor means unused slot. 104 */ 105 if (destructor == NULL) 106 destructor = osd_default_destructor; 107 108 sx_xlock(&osdm[type].osd_module_lock); 109 /* 110 * First, we try to find unused slot. 111 */ 112 for (i = 0; i < osdm[type].osd_ntslots; i++) { 113 if (osdm[type].osd_destructors[i] == NULL) { 114 OSD_DEBUG("Unused slot found (type=%u, slot=%u).", 115 type, i); 116 break; 117 } 118 } 119 /* 120 * If no unused slot was found, allocate one. 121 */ 122 if (i == osdm[type].osd_ntslots) { 123 osdm[type].osd_ntslots++; 124 if (osdm[type].osd_nmethods != 0) 125 osdm[type].osd_methods = realloc(osdm[type].osd_methods, 126 sizeof(osd_method_t) * osdm[type].osd_ntslots * 127 osdm[type].osd_nmethods, M_OSD, M_WAITOK); 128 newptr = malloc(sizeof(osd_destructor_t) * 129 osdm[type].osd_ntslots, M_OSD, M_WAITOK); 130 rm_wlock(&osdm[type].osd_object_lock); 131 bcopy(osdm[type].osd_destructors, newptr, 132 sizeof(osd_destructor_t) * i); 133 free(osdm[type].osd_destructors, M_OSD); 134 osdm[type].osd_destructors = newptr; 135 rm_wunlock(&osdm[type].osd_object_lock); 136 OSD_DEBUG("New slot allocated (type=%u, slot=%u).", 137 type, i + 1); 138 } 139 140 osdm[type].osd_destructors[i] = destructor; 141 if (osdm[type].osd_nmethods != 0) { 142 for (m = 0; m < osdm[type].osd_nmethods; m++) 143 osdm[type].osd_methods[i * osdm[type].osd_nmethods + m] 144 = methods != NULL ? methods[m] : NULL; 145 } 146 sx_xunlock(&osdm[type].osd_module_lock); 147 return (i + 1); 148 } 149 150 void 151 osd_deregister(u_int type, u_int slot) 152 { 153 struct osd *osd, *tosd; 154 155 KASSERT(type >= OSD_FIRST && type <= OSD_LAST, ("Invalid type.")); 156 KASSERT(slot > 0, ("Invalid slot.")); 157 KASSERT(osdm[type].osd_destructors[slot - 1] != NULL, ("Unused slot.")); 158 159 sx_xlock(&osdm[type].osd_module_lock); 160 rm_wlock(&osdm[type].osd_object_lock); 161 /* 162 * Free all OSD for the given slot. 163 */ 164 mtx_lock(&osdm[type].osd_list_lock); 165 LIST_FOREACH_SAFE(osd, &osdm[type].osd_list, osd_next, tosd) 166 do_osd_del(type, osd, slot, 1); 167 mtx_unlock(&osdm[type].osd_list_lock); 168 /* 169 * Set destructor to NULL to free the slot. 170 */ 171 osdm[type].osd_destructors[slot - 1] = NULL; 172 if (slot == osdm[type].osd_ntslots) { 173 osdm[type].osd_ntslots--; 174 osdm[type].osd_destructors = realloc(osdm[type].osd_destructors, 175 sizeof(osd_destructor_t) * osdm[type].osd_ntslots, M_OSD, 176 M_NOWAIT | M_ZERO); 177 if (osdm[type].osd_nmethods != 0) 178 osdm[type].osd_methods = realloc(osdm[type].osd_methods, 179 sizeof(osd_method_t) * osdm[type].osd_ntslots * 180 osdm[type].osd_nmethods, M_OSD, M_NOWAIT | M_ZERO); 181 /* 182 * We always reallocate to smaller size, so we assume it will 183 * always succeed. 184 */ 185 KASSERT(osdm[type].osd_destructors != NULL && 186 (osdm[type].osd_nmethods == 0 || 187 osdm[type].osd_methods != NULL), ("realloc() failed")); 188 OSD_DEBUG("Deregistration of the last slot (type=%u, slot=%u).", 189 type, slot); 190 } else { 191 OSD_DEBUG("Slot deregistration (type=%u, slot=%u).", 192 type, slot); 193 } 194 rm_wunlock(&osdm[type].osd_object_lock); 195 sx_xunlock(&osdm[type].osd_module_lock); 196 } 197 198 int 199 osd_set(u_int type, struct osd *osd, u_int slot, void *value) 200 { 201 202 return (osd_set_reserved(type, osd, slot, NULL, value)); 203 } 204 205 void ** 206 osd_reserve(u_int slot) 207 { 208 209 KASSERT(slot > 0, ("Invalid slot.")); 210 211 OSD_DEBUG("Reserving slot array (slot=%u).", slot); 212 return (malloc(sizeof(void *) * slot, M_OSD, M_WAITOK | M_ZERO)); 213 } 214 215 int 216 osd_set_reserved(u_int type, struct osd *osd, u_int slot, void **rsv, 217 void *value) 218 { 219 struct rm_priotracker tracker; 220 221 KASSERT(type >= OSD_FIRST && type <= OSD_LAST, ("Invalid type.")); 222 KASSERT(slot > 0, ("Invalid slot.")); 223 KASSERT(osdm[type].osd_destructors[slot - 1] != NULL, ("Unused slot.")); 224 225 rm_rlock(&osdm[type].osd_object_lock, &tracker); 226 if (slot > osd->osd_nslots) { 227 void **newptr; 228 229 if (value == NULL) { 230 OSD_DEBUG( 231 "Not allocating null slot (type=%u, slot=%u).", 232 type, slot); 233 rm_runlock(&osdm[type].osd_object_lock, &tracker); 234 if (rsv) 235 osd_free_reserved(rsv); 236 return (0); 237 } 238 239 /* 240 * Too few slots allocated here, so we need to extend or create 241 * the array. 242 */ 243 if (rsv) { 244 /* 245 * Use the reserve passed in (assumed to be 246 * the right size). 247 */ 248 newptr = rsv; 249 if (osd->osd_nslots != 0) { 250 memcpy(newptr, osd->osd_slots, 251 sizeof(void *) * osd->osd_nslots); 252 free(osd->osd_slots, M_OSD); 253 } 254 } else { 255 newptr = realloc(osd->osd_slots, sizeof(void *) * slot, 256 M_OSD, M_NOWAIT | M_ZERO); 257 if (newptr == NULL) { 258 rm_runlock(&osdm[type].osd_object_lock, 259 &tracker); 260 return (ENOMEM); 261 } 262 } 263 if (osd->osd_nslots == 0) { 264 /* 265 * First OSD for this object, so we need to put it 266 * onto the list. 267 */ 268 mtx_lock(&osdm[type].osd_list_lock); 269 LIST_INSERT_HEAD(&osdm[type].osd_list, osd, osd_next); 270 mtx_unlock(&osdm[type].osd_list_lock); 271 OSD_DEBUG("Setting first slot (type=%u).", type); 272 } else 273 OSD_DEBUG("Growing slots array (type=%u).", type); 274 osd->osd_slots = newptr; 275 osd->osd_nslots = slot; 276 } else if (rsv) 277 osd_free_reserved(rsv); 278 OSD_DEBUG("Setting slot value (type=%u, slot=%u, value=%p).", type, 279 slot, value); 280 osd->osd_slots[slot - 1] = value; 281 rm_runlock(&osdm[type].osd_object_lock, &tracker); 282 return (0); 283 } 284 285 void 286 osd_free_reserved(void **rsv) 287 { 288 289 OSD_DEBUG("Discarding reserved slot array."); 290 free(rsv, M_OSD); 291 } 292 293 void * 294 osd_get(u_int type, struct osd *osd, u_int slot) 295 { 296 struct rm_priotracker tracker; 297 void *value; 298 299 KASSERT(type >= OSD_FIRST && type <= OSD_LAST, ("Invalid type.")); 300 KASSERT(slot > 0, ("Invalid slot.")); 301 KASSERT(osdm[type].osd_destructors[slot - 1] != NULL, ("Unused slot.")); 302 303 rm_rlock(&osdm[type].osd_object_lock, &tracker); 304 if (slot > osd->osd_nslots) { 305 value = NULL; 306 OSD_DEBUG("Slot doesn't exist (type=%u, slot=%u).", type, slot); 307 } else { 308 value = osd->osd_slots[slot - 1]; 309 OSD_DEBUG("Returning slot value (type=%u, slot=%u, value=%p).", 310 type, slot, value); 311 } 312 rm_runlock(&osdm[type].osd_object_lock, &tracker); 313 return (value); 314 } 315 316 void 317 osd_del(u_int type, struct osd *osd, u_int slot) 318 { 319 struct rm_priotracker tracker; 320 321 rm_rlock(&osdm[type].osd_object_lock, &tracker); 322 do_osd_del(type, osd, slot, 0); 323 rm_runlock(&osdm[type].osd_object_lock, &tracker); 324 } 325 326 static void 327 do_osd_del(u_int type, struct osd *osd, u_int slot, int list_locked) 328 { 329 int i; 330 331 KASSERT(type >= OSD_FIRST && type <= OSD_LAST, ("Invalid type.")); 332 KASSERT(slot > 0, ("Invalid slot.")); 333 KASSERT(osdm[type].osd_destructors[slot - 1] != NULL, ("Unused slot.")); 334 335 OSD_DEBUG("Deleting slot (type=%u, slot=%u).", type, slot); 336 337 if (slot > osd->osd_nslots) { 338 OSD_DEBUG("Slot doesn't exist (type=%u, slot=%u).", type, slot); 339 return; 340 } 341 if (osd->osd_slots[slot - 1] != NULL) { 342 osdm[type].osd_destructors[slot - 1](osd->osd_slots[slot - 1]); 343 osd->osd_slots[slot - 1] = NULL; 344 } 345 for (i = osd->osd_nslots - 1; i >= 0; i--) { 346 if (osd->osd_slots[i] != NULL) { 347 OSD_DEBUG("Slot still has a value (type=%u, slot=%u).", 348 type, i + 1); 349 break; 350 } 351 } 352 if (i == -1) { 353 /* No values left for this object. */ 354 OSD_DEBUG("No more slots left (type=%u).", type); 355 if (!list_locked) 356 mtx_lock(&osdm[type].osd_list_lock); 357 LIST_REMOVE(osd, osd_next); 358 if (!list_locked) 359 mtx_unlock(&osdm[type].osd_list_lock); 360 free(osd->osd_slots, M_OSD); 361 osd->osd_slots = NULL; 362 osd->osd_nslots = 0; 363 } else if (slot == osd->osd_nslots) { 364 /* This was the last slot. */ 365 osd->osd_slots = realloc(osd->osd_slots, 366 sizeof(void *) * (i + 1), M_OSD, M_NOWAIT | M_ZERO); 367 /* 368 * We always reallocate to smaller size, so we assume it will 369 * always succeed. 370 */ 371 KASSERT(osd->osd_slots != NULL, ("realloc() failed")); 372 osd->osd_nslots = i + 1; 373 OSD_DEBUG("Reducing slots array to %u (type=%u).", 374 osd->osd_nslots, type); 375 } 376 } 377 378 int 379 osd_call(u_int type, u_int method, void *obj, void *data) 380 { 381 osd_method_t methodfun; 382 int error, i; 383 384 KASSERT(type >= OSD_FIRST && type <= OSD_LAST, ("Invalid type.")); 385 KASSERT(method < osdm[type].osd_nmethods, ("Invalid method.")); 386 387 /* 388 * Call this method for every slot that defines it, stopping if an 389 * error is encountered. 390 */ 391 error = 0; 392 sx_slock(&osdm[type].osd_module_lock); 393 for (i = 0; i < osdm[type].osd_ntslots; i++) { 394 methodfun = osdm[type].osd_methods[i * osdm[type].osd_nmethods + 395 method]; 396 if (methodfun != NULL && (error = methodfun(obj, data)) != 0) 397 break; 398 } 399 sx_sunlock(&osdm[type].osd_module_lock); 400 return (error); 401 } 402 403 void 404 osd_exit(u_int type, struct osd *osd) 405 { 406 struct rm_priotracker tracker; 407 u_int i; 408 409 KASSERT(type >= OSD_FIRST && type <= OSD_LAST, ("Invalid type.")); 410 411 if (osd->osd_nslots == 0) { 412 KASSERT(osd->osd_slots == NULL, ("Non-null osd_slots.")); 413 /* No OSD attached, just leave. */ 414 return; 415 } 416 417 rm_rlock(&osdm[type].osd_object_lock, &tracker); 418 for (i = 1; i <= osd->osd_nslots; i++) { 419 if (osdm[type].osd_destructors[i - 1] != NULL) 420 do_osd_del(type, osd, i, 0); 421 else 422 OSD_DEBUG("Unused slot (type=%u, slot=%u).", type, i); 423 } 424 rm_runlock(&osdm[type].osd_object_lock, &tracker); 425 OSD_DEBUG("Object exit (type=%u).", type); 426 } 427 428 static void 429 osd_init(void *arg __unused) 430 { 431 u_int i; 432 433 for (i = OSD_FIRST; i <= OSD_LAST; i++) { 434 sx_init(&osdm[i].osd_module_lock, "osd_module"); 435 rm_init(&osdm[i].osd_object_lock, "osd_object"); 436 mtx_init(&osdm[i].osd_list_lock, "osd_list", NULL, MTX_DEF); 437 LIST_INIT(&osdm[i].osd_list); 438 osdm[i].osd_destructors = NULL; 439 osdm[i].osd_ntslots = 0; 440 osdm[i].osd_methods = NULL; 441 } 442 } 443 SYSINIT(osd, SI_SUB_LOCK, SI_ORDER_ANY, osd_init, NULL); 444