kern_osd.c revision 191673
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: head/sys/kern/kern_osd.c 191673 2009-04-29 21:14:15Z jamie $"); 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/malloc.h> 36#include <sys/lock.h> 37#include <sys/mutex.h> 38#include <sys/rmlock.h> 39#include <sys/sx.h> 40#include <sys/queue.h> 41#include <sys/proc.h> 42#include <sys/osd.h> 43 44/* OSD (Object Specific Data) */ 45 46static MALLOC_DEFINE(M_OSD, "osd", "Object Specific Data"); 47 48static int osd_debug = 0; 49TUNABLE_INT("debug.osd", &osd_debug); 50SYSCTL_INT(_debug, OID_AUTO, osd, CTLFLAG_RW, &osd_debug, 0, "OSD debug level"); 51 52#define OSD_DEBUG(...) do { \ 53 if (osd_debug) { \ 54 printf("OSD (%s:%u): ", __func__, __LINE__); \ 55 printf(__VA_ARGS__); \ 56 printf("\n"); \ 57 } \ 58} while (0) 59 60static void do_osd_del(u_int type, struct osd *osd, u_int slot, 61 int list_locked); 62 63/* 64 * Lists of objects with OSD. 65 * 66 * Lock key: 67 * (m) osd_module_lock 68 * (o) osd_object_lock 69 * (l) osd_list_lock 70 */ 71static LIST_HEAD(, osd) osd_list[OSD_LAST + 1]; /* (m) */ 72static osd_method_t *osd_methods[OSD_LAST + 1]; /* (m) */ 73static u_int osd_nslots[OSD_LAST + 1]; /* (m) */ 74static osd_destructor_t *osd_destructors[OSD_LAST + 1]; /* (o) */ 75static const u_int osd_nmethods[OSD_LAST + 1] = { 76 [OSD_JAIL] = 5, 77}; 78 79static struct sx osd_module_lock[OSD_LAST + 1]; 80static struct rmlock osd_object_lock[OSD_LAST + 1]; 81static struct mtx osd_list_lock[OSD_LAST + 1]; 82 83static void 84osd_default_destructor(void *value __unused) 85{ 86 /* Do nothing. */ 87} 88 89int 90osd_register(u_int type, osd_destructor_t destructor, osd_method_t *methods) 91{ 92 void *newptr; 93 u_int i, m; 94 95 KASSERT(type >= OSD_FIRST && type <= OSD_LAST, ("Invalid type.")); 96 97 /* 98 * If no destructor is given, use default one. We need to use some 99 * destructor, because NULL destructor means unused slot. 100 */ 101 if (destructor == NULL) 102 destructor = osd_default_destructor; 103 104 sx_xlock(&osd_module_lock[type]); 105 /* 106 * First, we try to find unused slot. 107 */ 108 for (i = 0; i < osd_nslots[type]; i++) { 109 if (osd_destructors[type][i] == NULL) { 110 OSD_DEBUG("Unused slot found (type=%u, slot=%u).", 111 type, i); 112 break; 113 } 114 } 115 /* 116 * If no unused slot was found, allocate one. 117 */ 118 if (i == osd_nslots[type]) { 119 osd_nslots[type]++; 120 if (osd_nmethods[type] != 0) 121 osd_methods[type] = realloc(osd_methods[type], 122 sizeof(osd_method_t) * osd_nslots[type] * 123 osd_nmethods[type], M_OSD, M_WAITOK); 124 newptr = malloc(sizeof(osd_destructor_t) * osd_nslots[type], 125 M_OSD, M_WAITOK); 126 rm_wlock(&osd_object_lock[type]); 127 bcopy(osd_destructors[type], newptr, 128 sizeof(osd_destructor_t) * i); 129 free(osd_destructors[type], M_OSD); 130 osd_destructors[type] = newptr; 131 rm_wunlock(&osd_object_lock[type]); 132 OSD_DEBUG("New slot allocated (type=%u, slot=%u).", 133 type, i + 1); 134 } 135 136 osd_destructors[type][i] = destructor; 137 if (osd_nmethods[type] != 0) { 138 for (m = 0; m < osd_nmethods[type]; m++) 139 osd_methods[type][i * osd_nmethods[type] + m] = 140 methods != NULL ? methods[m] : NULL; 141 } 142 sx_xunlock(&osd_module_lock[type]); 143 return (i + 1); 144} 145 146void 147osd_deregister(u_int type, u_int slot) 148{ 149 struct osd *osd, *tosd; 150 151 KASSERT(type >= OSD_FIRST && type <= OSD_LAST, ("Invalid type.")); 152 KASSERT(slot > 0, ("Invalid slot.")); 153 KASSERT(osd_destructors[type][slot - 1] != NULL, ("Unused slot.")); 154 155 sx_xlock(&osd_module_lock[type]); 156 rm_wlock(&osd_object_lock[type]); 157 /* 158 * Free all OSD for the given slot. 159 */ 160 mtx_lock(&osd_list_lock[type]); 161 LIST_FOREACH_SAFE(osd, &osd_list[type], osd_next, tosd) 162 do_osd_del(type, osd, slot, 1); 163 mtx_unlock(&osd_list_lock[type]); 164 /* 165 * Set destructor to NULL to free the slot. 166 */ 167 osd_destructors[type][slot - 1] = NULL; 168 if (slot == osd_nslots[type]) { 169 osd_nslots[type]--; 170 osd_destructors[type] = realloc(osd_destructors[type], 171 sizeof(osd_destructor_t) * osd_nslots[type], M_OSD, 172 M_NOWAIT | M_ZERO); 173 if (osd_nmethods[type] != 0) 174 osd_methods[type] = realloc(osd_methods[type], 175 sizeof(osd_method_t) * osd_nslots[type] * 176 osd_nmethods[type], M_OSD, M_NOWAIT | M_ZERO); 177 /* 178 * We always reallocate to smaller size, so we assume it will 179 * always succeed. 180 */ 181 KASSERT(osd_destructors[type] != NULL && 182 (osd_nmethods[type] == 0 || osd_methods[type] != NULL), 183 ("realloc() failed")); 184 OSD_DEBUG("Deregistration of the last slot (type=%u, slot=%u).", 185 type, slot); 186 } else { 187 OSD_DEBUG("Slot deregistration (type=%u, slot=%u).", 188 type, slot); 189 } 190 rm_wunlock(&osd_object_lock[type]); 191 sx_xunlock(&osd_module_lock[type]); 192} 193 194int 195osd_set(u_int type, struct osd *osd, u_int slot, void *value) 196{ 197 struct rm_priotracker tracker; 198 199 KASSERT(type >= OSD_FIRST && type <= OSD_LAST, ("Invalid type.")); 200 KASSERT(slot > 0, ("Invalid slot.")); 201 KASSERT(osd_destructors[type][slot - 1] != NULL, ("Unused slot.")); 202 203 rm_rlock(&osd_object_lock[type], &tracker); 204 if (slot > osd->osd_nslots) { 205 if (value == NULL) { 206 OSD_DEBUG( 207 "Not allocating null slot (type=%u, slot=%u).", 208 type, slot); 209 rm_runlock(&osd_object_lock[type], &tracker); 210 return (0); 211 } else if (osd->osd_nslots == 0) { 212 /* 213 * First OSD for this object, so we need to allocate 214 * space and put it onto the list. 215 */ 216 osd->osd_slots = malloc(sizeof(void *) * slot, M_OSD, 217 M_NOWAIT | M_ZERO); 218 if (osd->osd_slots == NULL) { 219 rm_runlock(&osd_object_lock[type], &tracker); 220 return (ENOMEM); 221 } 222 osd->osd_nslots = slot; 223 mtx_lock(&osd_list_lock[type]); 224 LIST_INSERT_HEAD(&osd_list[type], osd, osd_next); 225 mtx_unlock(&osd_list_lock[type]); 226 OSD_DEBUG("Setting first slot (type=%u).", type); 227 } else { 228 void *newptr; 229 230 /* 231 * Too few slots allocated here, needs to extend 232 * the array. 233 */ 234 newptr = realloc(osd->osd_slots, sizeof(void *) * slot, 235 M_OSD, M_NOWAIT | M_ZERO); 236 if (newptr == NULL) { 237 rm_runlock(&osd_object_lock[type], &tracker); 238 return (ENOMEM); 239 } 240 osd->osd_slots = newptr; 241 osd->osd_nslots = slot; 242 OSD_DEBUG("Growing slots array (type=%u).", type); 243 } 244 } 245 OSD_DEBUG("Setting slot value (type=%u, slot=%u, value=%p).", type, 246 slot, value); 247 osd->osd_slots[slot - 1] = value; 248 rm_runlock(&osd_object_lock[type], &tracker); 249 return (0); 250} 251 252void * 253osd_get(u_int type, struct osd *osd, u_int slot) 254{ 255 struct rm_priotracker tracker; 256 void *value; 257 258 KASSERT(type >= OSD_FIRST && type <= OSD_LAST, ("Invalid type.")); 259 KASSERT(slot > 0, ("Invalid slot.")); 260 KASSERT(osd_destructors[type][slot - 1] != NULL, ("Unused slot.")); 261 262 rm_rlock(&osd_object_lock[type], &tracker); 263 if (slot > osd->osd_nslots) { 264 value = NULL; 265 OSD_DEBUG("Slot doesn't exist (type=%u, slot=%u).", type, slot); 266 } else { 267 value = osd->osd_slots[slot - 1]; 268 OSD_DEBUG("Returning slot value (type=%u, slot=%u, value=%p).", 269 type, slot, value); 270 } 271 rm_runlock(&osd_object_lock[type], &tracker); 272 return (value); 273} 274 275void 276osd_del(u_int type, struct osd *osd, u_int slot) 277{ 278 struct rm_priotracker tracker; 279 280 rm_rlock(&osd_object_lock[type], &tracker); 281 do_osd_del(type, osd, slot, 0); 282 rm_runlock(&osd_object_lock[type], &tracker); 283} 284 285static void 286do_osd_del(u_int type, struct osd *osd, u_int slot, int list_locked) 287{ 288 int i; 289 290 KASSERT(type >= OSD_FIRST && type <= OSD_LAST, ("Invalid type.")); 291 KASSERT(slot > 0, ("Invalid slot.")); 292 KASSERT(osd_destructors[type][slot - 1] != NULL, ("Unused slot.")); 293 294 OSD_DEBUG("Deleting slot (type=%u, slot=%u).", type, slot); 295 296 if (slot > osd->osd_nslots) { 297 OSD_DEBUG("Slot doesn't exist (type=%u, slot=%u).", type, slot); 298 return; 299 } 300 osd_destructors[type][slot - 1](osd->osd_slots[slot - 1]); 301 osd->osd_slots[slot - 1] = NULL; 302 for (i = osd->osd_nslots - 1; i >= 0; i--) { 303 if (osd->osd_slots[i] != NULL) { 304 OSD_DEBUG("Slot still has a value (type=%u, slot=%u).", 305 type, i + 1); 306 break; 307 } 308 } 309 if (i == -1) { 310 /* No values left for this object. */ 311 OSD_DEBUG("No more slots left (type=%u).", type); 312 if (!list_locked) 313 mtx_lock(&osd_list_lock[type]); 314 LIST_REMOVE(osd, osd_next); 315 if (!list_locked) 316 mtx_unlock(&osd_list_lock[type]); 317 free(osd->osd_slots, M_OSD); 318 osd->osd_slots = NULL; 319 osd->osd_nslots = 0; 320 } else if (slot == osd->osd_nslots) { 321 /* This was the last slot. */ 322 osd->osd_slots = realloc(osd->osd_slots, 323 sizeof(void *) * (i + 1), M_OSD, M_NOWAIT | M_ZERO); 324 /* 325 * We always reallocate to smaller size, so we assume it will 326 * always succeed. 327 */ 328 KASSERT(osd->osd_slots != NULL, ("realloc() failed")); 329 osd->osd_nslots = i + 1; 330 OSD_DEBUG("Reducing slots array to %u (type=%u).", 331 osd->osd_nslots, type); 332 } 333} 334 335int 336osd_call(u_int type, u_int method, void *obj, void *data) 337{ 338 osd_method_t methodfun; 339 int error, i; 340 341 KASSERT(type >= OSD_FIRST && type <= OSD_LAST, ("Invalid type.")); 342 KASSERT(method < osd_nmethods[type], ("Invalid method.")); 343 344 /* 345 * Call this method for every slot that defines it, stopping if an 346 * error is encountered. 347 */ 348 error = 0; 349 sx_slock(&osd_module_lock[type]); 350 for (i = 0; i < osd_nslots[type]; i++) { 351 methodfun = 352 osd_methods[type][i * osd_nmethods[type] + method]; 353 if (methodfun != NULL && (error = methodfun(obj, data)) != 0) 354 break; 355 } 356 sx_sunlock(&osd_module_lock[type]); 357 return (error); 358} 359 360void 361osd_exit(u_int type, struct osd *osd) 362{ 363 struct rm_priotracker tracker; 364 u_int i; 365 366 KASSERT(type >= OSD_FIRST && type <= OSD_LAST, ("Invalid type.")); 367 368 if (osd->osd_nslots == 0) { 369 KASSERT(osd->osd_slots == NULL, ("Non-null osd_slots.")); 370 /* No OSD attached, just leave. */ 371 return; 372 } 373 374 rm_rlock(&osd_object_lock[type], &tracker); 375 for (i = 1; i <= osd->osd_nslots; i++) { 376 if (osd_destructors[type][i - 1] != NULL) 377 do_osd_del(type, osd, i, 0); 378 else 379 OSD_DEBUG("Unused slot (type=%u, slot=%u).", type, i); 380 } 381 rm_runlock(&osd_object_lock[type], &tracker); 382 OSD_DEBUG("Object exit (type=%u).", type); 383} 384 385static void 386osd_init(void *arg __unused) 387{ 388 u_int i; 389 390 for (i = OSD_FIRST; i <= OSD_LAST; i++) { 391 osd_nslots[i] = 0; 392 LIST_INIT(&osd_list[i]); 393 sx_init(&osd_module_lock[i], "osd_module"); 394 rm_init(&osd_object_lock[i], "osd_object", 0); 395 mtx_init(&osd_list_lock[i], "osd_list", NULL, MTX_DEF); 396 osd_destructors[i] = NULL; 397 osd_methods[i] = NULL; 398 } 399} 400SYSINIT(osd, SI_SUB_LOCK, SI_ORDER_ANY, osd_init, NULL); 401