xref: /freebsd/sys/kern/kern_osd.c (revision 22cf89c938886d14f5796fc49f9f020c23ea8eaf)
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 + 1);
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 	/*
171 	 * Set destructor to NULL to free the slot.  We don't bother actually
172 	 * freeing any memory here because we'll gracefully reuse any freed
173 	 * slots, and reallocating the arrays as a smaller chunk of memory isn't
174 	 * actually guaranteed to succeed.  As such, we'll err on the side of
175 	 * caution and just leave it be since these are generally modestly sized
176 	 * allocations.
177 	 */
178 	osdm[type].osd_destructors[slot - 1] = NULL;
179 	OSD_DEBUG("Slot deregistration (type=%u, slot=%u).", type, slot);
180 
181 	rm_wunlock(&osdm[type].osd_object_lock);
182 	sx_xunlock(&osdm[type].osd_module_lock);
183 }
184 
185 int
186 osd_set(u_int type, struct osd *osd, u_int slot, void *value)
187 {
188 
189 	return (osd_set_reserved(type, osd, slot, NULL, value));
190 }
191 
192 void **
193 osd_reserve(u_int slot)
194 {
195 
196 	KASSERT(slot > 0, ("Invalid slot."));
197 
198 	OSD_DEBUG("Reserving slot array (slot=%u).", slot);
199 	return (malloc(sizeof(void *) * slot, M_OSD, M_WAITOK | M_ZERO));
200 }
201 
202 int
203 osd_set_reserved(u_int type, struct osd *osd, u_int slot, void **rsv,
204     void *value)
205 {
206 	struct rm_priotracker tracker;
207 
208 	KASSERT(type >= OSD_FIRST && type <= OSD_LAST, ("Invalid type."));
209 	KASSERT(slot > 0, ("Invalid slot."));
210 
211 	rm_rlock(&osdm[type].osd_object_lock, &tracker);
212 	KASSERT(osdm[type].osd_destructors[slot - 1] != NULL, ("Unused slot."));
213 
214 	if (slot > osd->osd_nslots) {
215 		void **newptr;
216 
217 		if (value == NULL) {
218 			OSD_DEBUG(
219 			    "Not allocating null slot (type=%u, slot=%u).",
220 			    type, slot);
221 			rm_runlock(&osdm[type].osd_object_lock, &tracker);
222 			if (rsv)
223 				osd_free_reserved(rsv);
224 			return (0);
225 		}
226 
227 		/*
228 		 * Too few slots allocated here, so we need to extend or create
229 		 * the array.
230 		 */
231 		if (rsv) {
232 			/*
233 			 * Use the reserve passed in (assumed to be
234 			 * the right size).
235 			 */
236 			newptr = rsv;
237 			if (osd->osd_nslots != 0) {
238 				memcpy(newptr, osd->osd_slots,
239 				    sizeof(void *) * osd->osd_nslots);
240 				free(osd->osd_slots, M_OSD);
241 			}
242 		} else {
243 			newptr = realloc(osd->osd_slots, sizeof(void *) * slot,
244 			    M_OSD, M_NOWAIT | M_ZERO);
245 			if (newptr == NULL) {
246 				rm_runlock(&osdm[type].osd_object_lock,
247 				    &tracker);
248 				return (ENOMEM);
249 			}
250 		}
251 		if (osd->osd_nslots == 0) {
252 			/*
253 			 * First OSD for this object, so we need to put it
254 			 * onto the list.
255 			 */
256 			mtx_lock(&osdm[type].osd_list_lock);
257 			LIST_INSERT_HEAD(&osdm[type].osd_list, osd, osd_next);
258 			mtx_unlock(&osdm[type].osd_list_lock);
259 			OSD_DEBUG("Setting first slot (type=%u).", type);
260 		} else
261 			OSD_DEBUG("Growing slots array (type=%u).", type);
262 		osd->osd_slots = newptr;
263 		osd->osd_nslots = slot;
264 	} else if (rsv)
265 		osd_free_reserved(rsv);
266 	OSD_DEBUG("Setting slot value (type=%u, slot=%u, value=%p).", type,
267 	    slot, value);
268 	osd->osd_slots[slot - 1] = value;
269 	rm_runlock(&osdm[type].osd_object_lock, &tracker);
270 	return (0);
271 }
272 
273 void
274 osd_free_reserved(void **rsv)
275 {
276 
277 	OSD_DEBUG("Discarding reserved slot array.");
278 	free(rsv, M_OSD);
279 }
280 
281 void *
282 osd_get(u_int type, struct osd *osd, u_int slot)
283 {
284 	struct rm_priotracker tracker;
285 	void *value;
286 
287 	KASSERT(type >= OSD_FIRST && type <= OSD_LAST, ("Invalid type."));
288 	KASSERT(slot > 0, ("Invalid slot."));
289 
290 	rm_rlock(&osdm[type].osd_object_lock, &tracker);
291 	KASSERT(osdm[type].osd_destructors[slot - 1] != NULL, ("Unused slot."));
292 
293 	if (slot > osd->osd_nslots) {
294 		value = NULL;
295 		OSD_DEBUG("Slot doesn't exist (type=%u, slot=%u).", type, slot);
296 	} else {
297 		value = osd->osd_slots[slot - 1];
298 		OSD_DEBUG("Returning slot value (type=%u, slot=%u, value=%p).",
299 		    type, slot, value);
300 	}
301 	rm_runlock(&osdm[type].osd_object_lock, &tracker);
302 	return (value);
303 }
304 
305 void
306 osd_del(u_int type, struct osd *osd, u_int slot)
307 {
308 	struct rm_priotracker tracker;
309 
310 	rm_rlock(&osdm[type].osd_object_lock, &tracker);
311 	do_osd_del(type, osd, slot, 0);
312 	rm_runlock(&osdm[type].osd_object_lock, &tracker);
313 }
314 
315 static void
316 do_osd_del(u_int type, struct osd *osd, u_int slot, int list_locked)
317 {
318 	int i;
319 
320 	KASSERT(type >= OSD_FIRST && type <= OSD_LAST, ("Invalid type."));
321 	KASSERT(slot > 0, ("Invalid slot."));
322 	KASSERT(osdm[type].osd_destructors[slot - 1] != NULL, ("Unused slot."));
323 
324 	OSD_DEBUG("Deleting slot (type=%u, slot=%u).", type, slot);
325 
326 	if (slot > osd->osd_nslots) {
327 		OSD_DEBUG("Slot doesn't exist (type=%u, slot=%u).", type, slot);
328 		return;
329 	}
330 	if (osd->osd_slots[slot - 1] != NULL) {
331 		osdm[type].osd_destructors[slot - 1](osd->osd_slots[slot - 1]);
332 		osd->osd_slots[slot - 1] = NULL;
333 	}
334 	for (i = osd->osd_nslots - 1; i >= 0; i--) {
335 		if (osd->osd_slots[i] != NULL) {
336 			OSD_DEBUG("Slot still has a value (type=%u, slot=%u).",
337 			    type, i + 1);
338 			break;
339 		}
340 	}
341 	if (i == -1) {
342 		/* No values left for this object. */
343 		OSD_DEBUG("No more slots left (type=%u).", type);
344 		if (!list_locked)
345 			mtx_lock(&osdm[type].osd_list_lock);
346 		LIST_REMOVE(osd, osd_next);
347 		if (!list_locked)
348 			mtx_unlock(&osdm[type].osd_list_lock);
349 		free(osd->osd_slots, M_OSD);
350 		osd->osd_slots = NULL;
351 		osd->osd_nslots = 0;
352 	} else if (slot == osd->osd_nslots) {
353 		/* This was the last slot. */
354 		osd->osd_slots = realloc(osd->osd_slots,
355 		    sizeof(void *) * (i + 1), M_OSD, M_NOWAIT | M_ZERO);
356 		/*
357 		 * We always reallocate to smaller size, so we assume it will
358 		 * always succeed.
359 		 */
360 		KASSERT(osd->osd_slots != NULL, ("realloc() failed"));
361 		osd->osd_nslots = i + 1;
362 		OSD_DEBUG("Reducing slots array to %u (type=%u).",
363 		    osd->osd_nslots, type);
364 	}
365 }
366 
367 int
368 osd_call(u_int type, u_int method, void *obj, void *data)
369 {
370 	osd_method_t methodfun;
371 	int error, i;
372 
373 	KASSERT(type >= OSD_FIRST && type <= OSD_LAST, ("Invalid type."));
374 	KASSERT(method < osdm[type].osd_nmethods, ("Invalid method."));
375 
376 	/*
377 	 * Call this method for every slot that defines it, stopping if an
378 	 * error is encountered.
379 	 */
380 	error = 0;
381 	sx_slock(&osdm[type].osd_module_lock);
382 	for (i = 0; i < osdm[type].osd_ntslots; i++) {
383 		/* Hole in the slot map; avoid dereferencing. */
384 		if (osdm[type].osd_destructors[i] == NULL)
385 			continue;
386 		methodfun = osdm[type].osd_methods[i * osdm[type].osd_nmethods +
387 		    method];
388 		if (methodfun != NULL && (error = methodfun(obj, data)) != 0)
389 			break;
390 	}
391 	sx_sunlock(&osdm[type].osd_module_lock);
392 	return (error);
393 }
394 
395 void
396 osd_exit(u_int type, struct osd *osd)
397 {
398 	struct rm_priotracker tracker;
399 	u_int i;
400 
401 	KASSERT(type >= OSD_FIRST && type <= OSD_LAST, ("Invalid type."));
402 
403 	if (osd->osd_nslots == 0) {
404 		KASSERT(osd->osd_slots == NULL, ("Non-null osd_slots."));
405 		/* No OSD attached, just leave. */
406 		return;
407 	}
408 
409 	rm_rlock(&osdm[type].osd_object_lock, &tracker);
410 	for (i = 1; i <= osd->osd_nslots; i++) {
411 		if (osdm[type].osd_destructors[i - 1] != NULL)
412 			do_osd_del(type, osd, i, 0);
413 		else
414 			OSD_DEBUG("Unused slot (type=%u, slot=%u).", type, i);
415 	}
416 	rm_runlock(&osdm[type].osd_object_lock, &tracker);
417 	OSD_DEBUG("Object exit (type=%u).", type);
418 }
419 
420 static void
421 osd_init(void *arg __unused)
422 {
423 	u_int i;
424 
425 	for (i = OSD_FIRST; i <= OSD_LAST; i++) {
426 		sx_init(&osdm[i].osd_module_lock, "osd_module");
427 		rm_init(&osdm[i].osd_object_lock, "osd_object");
428 		mtx_init(&osdm[i].osd_list_lock, "osd_list", NULL, MTX_DEF);
429 		LIST_INIT(&osdm[i].osd_list);
430 		osdm[i].osd_destructors = NULL;
431 		osdm[i].osd_ntslots = 0;
432 		osdm[i].osd_methods = NULL;
433 	}
434 }
435 SYSINIT(osd, SI_SUB_LOCK, SI_ORDER_ANY, osd_init, NULL);
436