xref: /freebsd/sys/kern/kern_osd.c (revision 0b3105a37d7adcadcb720112fed4dc4e8040be99)
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/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 static MALLOC_DEFINE(M_OSD, "osd", "Object Specific Data");
48 
49 static int osd_debug = 0;
50 SYSCTL_INT(_debug, OID_AUTO, osd, CTLFLAG_RWTUN, &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 	[OSD_JAIL] = PR_MAXMETHOD,
77 };
78 
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];
82 
83 static void
84 osd_default_destructor(void *value __unused)
85 {
86 	/* Do nothing. */
87 }
88 
89 int
90 osd_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 
146 void
147 osd_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 
194 int
195 osd_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 
252 void *
253 osd_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 
275 void
276 osd_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 
285 static void
286 do_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 	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;
303 	}
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).",
307 			    type, i + 1);
308 			break;
309 		}
310 	}
311 	if (i == -1) {
312 		/* No values left for this object. */
313 		OSD_DEBUG("No more slots left (type=%u).", type);
314 		if (!list_locked)
315 			mtx_lock(&osd_list_lock[type]);
316 		LIST_REMOVE(osd, osd_next);
317 		if (!list_locked)
318 			mtx_unlock(&osd_list_lock[type]);
319 		free(osd->osd_slots, M_OSD);
320 		osd->osd_slots = NULL;
321 		osd->osd_nslots = 0;
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);
326 		/*
327 		 * We always reallocate to smaller size, so we assume it will
328 		 * always succeed.
329 		 */
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);
334 	}
335 }
336 
337 int
338 osd_call(u_int type, u_int method, void *obj, void *data)
339 {
340 	osd_method_t methodfun;
341 	int error, i;
342 
343 	KASSERT(type >= OSD_FIRST && type <= OSD_LAST, ("Invalid type."));
344 	KASSERT(method < osd_nmethods[type], ("Invalid method."));
345 
346 	/*
347 	 * Call this method for every slot that defines it, stopping if an
348 	 * error is encountered.
349 	 */
350 	error = 0;
351 	sx_slock(&osd_module_lock[type]);
352 	for (i = 0; i < osd_nslots[type]; i++) {
353 		methodfun =
354 		    osd_methods[type][i * osd_nmethods[type] + method];
355 		if (methodfun != NULL && (error = methodfun(obj, data)) != 0)
356 			break;
357 	}
358 	sx_sunlock(&osd_module_lock[type]);
359 	return (error);
360 }
361 
362 void
363 osd_exit(u_int type, struct osd *osd)
364 {
365 	struct rm_priotracker tracker;
366 	u_int i;
367 
368 	KASSERT(type >= OSD_FIRST && type <= OSD_LAST, ("Invalid type."));
369 
370 	if (osd->osd_nslots == 0) {
371 		KASSERT(osd->osd_slots == NULL, ("Non-null osd_slots."));
372 		/* No OSD attached, just leave. */
373 		return;
374 	}
375 
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);
380 		else
381 			OSD_DEBUG("Unused slot (type=%u, slot=%u).", type, i);
382 	}
383 	rm_runlock(&osd_object_lock[type], &tracker);
384 	OSD_DEBUG("Object exit (type=%u).", type);
385 }
386 
387 static void
388 osd_init(void *arg __unused)
389 {
390 	u_int i;
391 
392 	for (i = OSD_FIRST; i <= OSD_LAST; i++) {
393 		osd_nslots[i] = 0;
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;
400 	}
401 }
402 SYSINIT(osd, SI_SUB_LOCK, SI_ORDER_ANY, osd_init, NULL);
403