1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2007 Pawel Jakub Dawidek <pjd@FreeBSD.org> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 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 158 sx_xlock(&osdm[type].osd_module_lock); 159 rm_wlock(&osdm[type].osd_object_lock); 160 KASSERT(osdm[type].osd_destructors[slot - 1] != NULL, ("Unused slot.")); 161 162 /* 163 * Free all OSD for the given slot. 164 */ 165 mtx_lock(&osdm[type].osd_list_lock); 166 LIST_FOREACH_SAFE(osd, &osdm[type].osd_list, osd_next, tosd) 167 do_osd_del(type, osd, slot, 1); 168 mtx_unlock(&osdm[type].osd_list_lock); 169 /* 170 * Set destructor to NULL to free the slot. 171 */ 172 osdm[type].osd_destructors[slot - 1] = NULL; 173 if (slot == osdm[type].osd_ntslots) { 174 osdm[type].osd_ntslots--; 175 osdm[type].osd_destructors = realloc(osdm[type].osd_destructors, 176 sizeof(osd_destructor_t) * osdm[type].osd_ntslots, M_OSD, 177 M_NOWAIT | M_ZERO); 178 if (osdm[type].osd_nmethods != 0) 179 osdm[type].osd_methods = realloc(osdm[type].osd_methods, 180 sizeof(osd_method_t) * osdm[type].osd_ntslots * 181 osdm[type].osd_nmethods, M_OSD, M_NOWAIT | M_ZERO); 182 /* 183 * We always reallocate to smaller size, so we assume it will 184 * always succeed. 185 */ 186 KASSERT(osdm[type].osd_destructors != NULL && 187 (osdm[type].osd_nmethods == 0 || 188 osdm[type].osd_methods != NULL), ("realloc() failed")); 189 OSD_DEBUG("Deregistration of the last slot (type=%u, slot=%u).", 190 type, slot); 191 } else { 192 OSD_DEBUG("Slot deregistration (type=%u, slot=%u).", 193 type, slot); 194 } 195 rm_wunlock(&osdm[type].osd_object_lock); 196 sx_xunlock(&osdm[type].osd_module_lock); 197 } 198 199 int 200 osd_set(u_int type, struct osd *osd, u_int slot, void *value) 201 { 202 203 return (osd_set_reserved(type, osd, slot, NULL, value)); 204 } 205 206 void ** 207 osd_reserve(u_int slot) 208 { 209 210 KASSERT(slot > 0, ("Invalid slot.")); 211 212 OSD_DEBUG("Reserving slot array (slot=%u).", slot); 213 return (malloc(sizeof(void *) * slot, M_OSD, M_WAITOK | M_ZERO)); 214 } 215 216 int 217 osd_set_reserved(u_int type, struct osd *osd, u_int slot, void **rsv, 218 void *value) 219 { 220 struct rm_priotracker tracker; 221 222 KASSERT(type >= OSD_FIRST && type <= OSD_LAST, ("Invalid type.")); 223 KASSERT(slot > 0, ("Invalid slot.")); 224 225 rm_rlock(&osdm[type].osd_object_lock, &tracker); 226 KASSERT(osdm[type].osd_destructors[slot - 1] != NULL, ("Unused slot.")); 227 228 if (slot > osd->osd_nslots) { 229 void **newptr; 230 231 if (value == NULL) { 232 OSD_DEBUG( 233 "Not allocating null slot (type=%u, slot=%u).", 234 type, slot); 235 rm_runlock(&osdm[type].osd_object_lock, &tracker); 236 if (rsv) 237 osd_free_reserved(rsv); 238 return (0); 239 } 240 241 /* 242 * Too few slots allocated here, so we need to extend or create 243 * the array. 244 */ 245 if (rsv) { 246 /* 247 * Use the reserve passed in (assumed to be 248 * the right size). 249 */ 250 newptr = rsv; 251 if (osd->osd_nslots != 0) { 252 memcpy(newptr, osd->osd_slots, 253 sizeof(void *) * osd->osd_nslots); 254 free(osd->osd_slots, M_OSD); 255 } 256 } else { 257 newptr = realloc(osd->osd_slots, sizeof(void *) * slot, 258 M_OSD, M_NOWAIT | M_ZERO); 259 if (newptr == NULL) { 260 rm_runlock(&osdm[type].osd_object_lock, 261 &tracker); 262 return (ENOMEM); 263 } 264 } 265 if (osd->osd_nslots == 0) { 266 /* 267 * First OSD for this object, so we need to put it 268 * onto the list. 269 */ 270 mtx_lock(&osdm[type].osd_list_lock); 271 LIST_INSERT_HEAD(&osdm[type].osd_list, osd, osd_next); 272 mtx_unlock(&osdm[type].osd_list_lock); 273 OSD_DEBUG("Setting first slot (type=%u).", type); 274 } else 275 OSD_DEBUG("Growing slots array (type=%u).", type); 276 osd->osd_slots = newptr; 277 osd->osd_nslots = slot; 278 } else if (rsv) 279 osd_free_reserved(rsv); 280 OSD_DEBUG("Setting slot value (type=%u, slot=%u, value=%p).", type, 281 slot, value); 282 osd->osd_slots[slot - 1] = value; 283 rm_runlock(&osdm[type].osd_object_lock, &tracker); 284 return (0); 285 } 286 287 void 288 osd_free_reserved(void **rsv) 289 { 290 291 OSD_DEBUG("Discarding reserved slot array."); 292 free(rsv, M_OSD); 293 } 294 295 void * 296 osd_get(u_int type, struct osd *osd, u_int slot) 297 { 298 struct rm_priotracker tracker; 299 void *value; 300 301 KASSERT(type >= OSD_FIRST && type <= OSD_LAST, ("Invalid type.")); 302 KASSERT(slot > 0, ("Invalid slot.")); 303 304 rm_rlock(&osdm[type].osd_object_lock, &tracker); 305 KASSERT(osdm[type].osd_destructors[slot - 1] != NULL, ("Unused slot.")); 306 307 if (slot > osd->osd_nslots) { 308 value = NULL; 309 OSD_DEBUG("Slot doesn't exist (type=%u, slot=%u).", type, slot); 310 } else { 311 value = osd->osd_slots[slot - 1]; 312 OSD_DEBUG("Returning slot value (type=%u, slot=%u, value=%p).", 313 type, slot, value); 314 } 315 rm_runlock(&osdm[type].osd_object_lock, &tracker); 316 return (value); 317 } 318 319 void 320 osd_del(u_int type, struct osd *osd, u_int slot) 321 { 322 struct rm_priotracker tracker; 323 324 rm_rlock(&osdm[type].osd_object_lock, &tracker); 325 do_osd_del(type, osd, slot, 0); 326 rm_runlock(&osdm[type].osd_object_lock, &tracker); 327 } 328 329 static void 330 do_osd_del(u_int type, struct osd *osd, u_int slot, int list_locked) 331 { 332 int i; 333 334 KASSERT(type >= OSD_FIRST && type <= OSD_LAST, ("Invalid type.")); 335 KASSERT(slot > 0, ("Invalid slot.")); 336 KASSERT(osdm[type].osd_destructors[slot - 1] != NULL, ("Unused slot.")); 337 338 OSD_DEBUG("Deleting slot (type=%u, slot=%u).", type, slot); 339 340 if (slot > osd->osd_nslots) { 341 OSD_DEBUG("Slot doesn't exist (type=%u, slot=%u).", type, slot); 342 return; 343 } 344 if (osd->osd_slots[slot - 1] != NULL) { 345 osdm[type].osd_destructors[slot - 1](osd->osd_slots[slot - 1]); 346 osd->osd_slots[slot - 1] = NULL; 347 } 348 for (i = osd->osd_nslots - 1; i >= 0; i--) { 349 if (osd->osd_slots[i] != NULL) { 350 OSD_DEBUG("Slot still has a value (type=%u, slot=%u).", 351 type, i + 1); 352 break; 353 } 354 } 355 if (i == -1) { 356 /* No values left for this object. */ 357 OSD_DEBUG("No more slots left (type=%u).", type); 358 if (!list_locked) 359 mtx_lock(&osdm[type].osd_list_lock); 360 LIST_REMOVE(osd, osd_next); 361 if (!list_locked) 362 mtx_unlock(&osdm[type].osd_list_lock); 363 free(osd->osd_slots, M_OSD); 364 osd->osd_slots = NULL; 365 osd->osd_nslots = 0; 366 } else if (slot == osd->osd_nslots) { 367 /* This was the last slot. */ 368 osd->osd_slots = realloc(osd->osd_slots, 369 sizeof(void *) * (i + 1), M_OSD, M_NOWAIT | M_ZERO); 370 /* 371 * We always reallocate to smaller size, so we assume it will 372 * always succeed. 373 */ 374 KASSERT(osd->osd_slots != NULL, ("realloc() failed")); 375 osd->osd_nslots = i + 1; 376 OSD_DEBUG("Reducing slots array to %u (type=%u).", 377 osd->osd_nslots, type); 378 } 379 } 380 381 int 382 osd_call(u_int type, u_int method, void *obj, void *data) 383 { 384 osd_method_t methodfun; 385 int error, i; 386 387 KASSERT(type >= OSD_FIRST && type <= OSD_LAST, ("Invalid type.")); 388 KASSERT(method < osdm[type].osd_nmethods, ("Invalid method.")); 389 390 /* 391 * Call this method for every slot that defines it, stopping if an 392 * error is encountered. 393 */ 394 error = 0; 395 sx_slock(&osdm[type].osd_module_lock); 396 for (i = 0; i < osdm[type].osd_ntslots; i++) { 397 /* Hole in the slot map; avoid dereferencing. */ 398 if (osdm[type].osd_destructors[i] == NULL) 399 continue; 400 methodfun = osdm[type].osd_methods[i * osdm[type].osd_nmethods + 401 method]; 402 if (methodfun != NULL && (error = methodfun(obj, data)) != 0) 403 break; 404 } 405 sx_sunlock(&osdm[type].osd_module_lock); 406 return (error); 407 } 408 409 void 410 osd_exit(u_int type, struct osd *osd) 411 { 412 struct rm_priotracker tracker; 413 u_int i; 414 415 KASSERT(type >= OSD_FIRST && type <= OSD_LAST, ("Invalid type.")); 416 417 if (osd->osd_nslots == 0) { 418 KASSERT(osd->osd_slots == NULL, ("Non-null osd_slots.")); 419 /* No OSD attached, just leave. */ 420 return; 421 } 422 423 rm_rlock(&osdm[type].osd_object_lock, &tracker); 424 for (i = 1; i <= osd->osd_nslots; i++) { 425 if (osdm[type].osd_destructors[i - 1] != NULL) 426 do_osd_del(type, osd, i, 0); 427 else 428 OSD_DEBUG("Unused slot (type=%u, slot=%u).", type, i); 429 } 430 rm_runlock(&osdm[type].osd_object_lock, &tracker); 431 OSD_DEBUG("Object exit (type=%u).", type); 432 } 433 434 static void 435 osd_init(void *arg __unused) 436 { 437 u_int i; 438 439 for (i = OSD_FIRST; i <= OSD_LAST; i++) { 440 sx_init(&osdm[i].osd_module_lock, "osd_module"); 441 rm_init(&osdm[i].osd_object_lock, "osd_object"); 442 mtx_init(&osdm[i].osd_list_lock, "osd_list", NULL, MTX_DEF); 443 LIST_INIT(&osdm[i].osd_list); 444 osdm[i].osd_destructors = NULL; 445 osdm[i].osd_ntslots = 0; 446 osdm[i].osd_methods = NULL; 447 } 448 } 449 SYSINIT(osd, SI_SUB_LOCK, SI_ORDER_ANY, osd_init, NULL); 450