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