2 * Copyright (c) 2007 Pawel Jakub Dawidek <pjd@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
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>
36 #include <sys/malloc.h>
38 #include <sys/mutex.h>
39 #include <sys/rmlock.h>
41 #include <sys/queue.h>
45 /* OSD (Object Specific Data) */
47 static MALLOC_DEFINE(M_OSD
, "osd", "Object Specific Data");
49 static int osd_debug
= 0;
50 SYSCTL_INT(_debug
, OID_AUTO
, osd
, CTLFLAG_RWTUN
, &osd_debug
, 0, "OSD debug level");
52 #define OSD_DEBUG(...) do { \
54 printf("OSD (%s:%u): ", __func__, __LINE__); \
55 printf(__VA_ARGS__); \
60 static void do_osd_del(u_int type
, struct osd
*osd
, u_int slot
,
64 * Lists of objects with OSD.
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 [OSD_JAIL
] = PR_MAXMETHOD
,
79 static struct sx osd_module_lock
[OSD_LAST
+ 1];
80 static struct rmlock osd_object_lock
[OSD_LAST
+ 1];
81 static struct mtx osd_list_lock
[OSD_LAST
+ 1];
84 osd_default_destructor(void *value __unused
)
90 osd_register(u_int type
, osd_destructor_t destructor
, osd_method_t
*methods
)
95 KASSERT(type
>= OSD_FIRST
&& type
<= OSD_LAST
, ("Invalid type."));
98 * If no destructor is given, use default one. We need to use some
99 * destructor, because NULL destructor means unused slot.
101 if (destructor
== NULL
)
102 destructor
= osd_default_destructor
;
104 sx_xlock(&osd_module_lock
[type
]);
106 * First, we try to find unused slot.
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).",
116 * If no unused slot was found, allocate one.
118 if (i
== 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
],
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).",
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
;
142 sx_xunlock(&osd_module_lock
[type
]);
147 osd_deregister(u_int type
, u_int slot
)
149 struct osd
*osd
, *tosd
;
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."));
155 sx_xlock(&osd_module_lock
[type
]);
156 rm_wlock(&osd_object_lock
[type
]);
158 * Free all OSD for the given slot.
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
]);
165 * Set destructor to NULL to free the slot.
167 osd_destructors
[type
][slot
- 1] = NULL
;
168 if (slot
== osd_nslots
[type
]) {
170 osd_destructors
[type
] = realloc(osd_destructors
[type
],
171 sizeof(osd_destructor_t
) * osd_nslots
[type
], M_OSD
,
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
);
178 * We always reallocate to smaller size, so we assume it will
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).",
187 OSD_DEBUG("Slot deregistration (type=%u, slot=%u).",
190 rm_wunlock(&osd_object_lock
[type
]);
191 sx_xunlock(&osd_module_lock
[type
]);
195 osd_set(u_int type
, struct osd
*osd
, u_int slot
, void *value
)
197 struct rm_priotracker tracker
;
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."));
203 rm_rlock(&osd_object_lock
[type
], &tracker
);
204 if (slot
> osd
->osd_nslots
) {
207 "Not allocating null slot (type=%u, slot=%u).",
209 rm_runlock(&osd_object_lock
[type
], &tracker
);
211 } else if (osd
->osd_nslots
== 0) {
213 * First OSD for this object, so we need to allocate
214 * space and put it onto the list.
216 osd
->osd_slots
= malloc(sizeof(void *) * slot
, M_OSD
,
218 if (osd
->osd_slots
== NULL
) {
219 rm_runlock(&osd_object_lock
[type
], &tracker
);
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
);
231 * Too few slots allocated here, needs to extend
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
);
240 osd
->osd_slots
= newptr
;
241 osd
->osd_nslots
= slot
;
242 OSD_DEBUG("Growing slots array (type=%u).", type
);
245 OSD_DEBUG("Setting slot value (type=%u, slot=%u, value=%p).", type
,
247 osd
->osd_slots
[slot
- 1] = value
;
248 rm_runlock(&osd_object_lock
[type
], &tracker
);
253 osd_get(u_int type
, struct osd
*osd
, u_int slot
)
255 struct rm_priotracker tracker
;
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."));
262 rm_rlock(&osd_object_lock
[type
], &tracker
);
263 if (slot
> osd
->osd_nslots
) {
265 OSD_DEBUG("Slot doesn't exist (type=%u, slot=%u).", type
, slot
);
267 value
= osd
->osd_slots
[slot
- 1];
268 OSD_DEBUG("Returning slot value (type=%u, slot=%u, value=%p).",
271 rm_runlock(&osd_object_lock
[type
], &tracker
);
276 osd_del(u_int type
, struct osd
*osd
, u_int slot
)
278 struct rm_priotracker tracker
;
280 rm_rlock(&osd_object_lock
[type
], &tracker
);
281 do_osd_del(type
, osd
, slot
, 0);
282 rm_runlock(&osd_object_lock
[type
], &tracker
);
286 do_osd_del(u_int type
, struct osd
*osd
, u_int slot
, int list_locked
)
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."));
294 OSD_DEBUG("Deleting slot (type=%u, slot=%u).", type
, slot
);
296 if (slot
> osd
->osd_nslots
) {
297 OSD_DEBUG("Slot doesn't exist (type=%u, slot=%u).", type
, slot
);
300 if (osd
->osd_slots
[slot
- 1] != NULL
) {
301 osd_destructors
[type
][slot
- 1](osd
->osd_slots
[slot
- 1]);
302 osd
->osd_slots
[slot
- 1] = NULL
;
304 for (i
= osd
->osd_nslots
- 1; i
>= 0; i
--) {
305 if (osd
->osd_slots
[i
] != NULL
) {
306 OSD_DEBUG("Slot still has a value (type=%u, slot=%u).",
312 /* No values left for this object. */
313 OSD_DEBUG("No more slots left (type=%u).", type
);
315 mtx_lock(&osd_list_lock
[type
]);
316 LIST_REMOVE(osd
, osd_next
);
318 mtx_unlock(&osd_list_lock
[type
]);
319 free(osd
->osd_slots
, M_OSD
);
320 osd
->osd_slots
= NULL
;
322 } else if (slot
== osd
->osd_nslots
) {
323 /* This was the last slot. */
324 osd
->osd_slots
= realloc(osd
->osd_slots
,
325 sizeof(void *) * (i
+ 1), M_OSD
, M_NOWAIT
| M_ZERO
);
327 * We always reallocate to smaller size, so we assume it will
330 KASSERT(osd
->osd_slots
!= NULL
, ("realloc() failed"));
331 osd
->osd_nslots
= i
+ 1;
332 OSD_DEBUG("Reducing slots array to %u (type=%u).",
333 osd
->osd_nslots
, type
);
338 osd_call(u_int type
, u_int method
, void *obj
, void *data
)
340 osd_method_t methodfun
;
343 KASSERT(type
>= OSD_FIRST
&& type
<= OSD_LAST
, ("Invalid type."));
344 KASSERT(method
< osd_nmethods
[type
], ("Invalid method."));
347 * Call this method for every slot that defines it, stopping if an
348 * error is encountered.
351 sx_slock(&osd_module_lock
[type
]);
352 for (i
= 0; i
< osd_nslots
[type
]; i
++) {
354 osd_methods
[type
][i
* osd_nmethods
[type
] + method
];
355 if (methodfun
!= NULL
&& (error
= methodfun(obj
, data
)) != 0)
358 sx_sunlock(&osd_module_lock
[type
]);
363 osd_exit(u_int type
, struct osd
*osd
)
365 struct rm_priotracker tracker
;
368 KASSERT(type
>= OSD_FIRST
&& type
<= OSD_LAST
, ("Invalid type."));
370 if (osd
->osd_nslots
== 0) {
371 KASSERT(osd
->osd_slots
== NULL
, ("Non-null osd_slots."));
372 /* No OSD attached, just leave. */
376 rm_rlock(&osd_object_lock
[type
], &tracker
);
377 for (i
= 1; i
<= osd
->osd_nslots
; i
++) {
378 if (osd_destructors
[type
][i
- 1] != NULL
)
379 do_osd_del(type
, osd
, i
, 0);
381 OSD_DEBUG("Unused slot (type=%u, slot=%u).", type
, i
);
383 rm_runlock(&osd_object_lock
[type
], &tracker
);
384 OSD_DEBUG("Object exit (type=%u).", type
);
388 osd_init(void *arg __unused
)
392 for (i
= OSD_FIRST
; i
<= OSD_LAST
; i
++) {
394 LIST_INIT(&osd_list
[i
]);
395 sx_init(&osd_module_lock
[i
], "osd_module");
396 rm_init(&osd_object_lock
[i
], "osd_object");
397 mtx_init(&osd_list_lock
[i
], "osd_list", NULL
, MTX_DEF
);
398 osd_destructors
[i
] = NULL
;
399 osd_methods
[i
] = NULL
;
402 SYSINIT(osd
, SI_SUB_LOCK
, SI_ORDER_ANY
, osd_init
, NULL
);