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