xref: /freebsd/sys/kern/kern_osd.c (revision f7c32ed617858bcd22f8d1b03199099d50125721)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/kernel.h>
34 #include <sys/systm.h>
35 #include <sys/sysctl.h>
36 #include <sys/errno.h>
37 #include <sys/jail.h>
38 #include <sys/malloc.h>
39 #include <sys/lock.h>
40 #include <sys/mutex.h>
41 #include <sys/rmlock.h>
42 #include <sys/sx.h>
43 #include <sys/queue.h>
44 #include <sys/proc.h>
45 #include <sys/osd.h>
46 
47 /* OSD (Object Specific Data) */
48 
49 /*
50  * Lock key:
51  *  (m) osd_module_lock
52  *  (o) osd_object_lock
53  *  (l) osd_list_lock
54  */
55 struct osd_master {
56 	struct sx		 osd_module_lock;
57 	struct rmlock		 osd_object_lock;
58 	struct mtx		 osd_list_lock;
59 	LIST_HEAD(, osd)	 osd_list;		/* (l) */
60 	osd_destructor_t	*osd_destructors;	/* (o) */
61 	osd_method_t		*osd_methods;		/* (m) */
62 	u_int			 osd_ntslots;		/* (m) */
63 	const u_int		 osd_nmethods;
64 };
65 
66 static MALLOC_DEFINE(M_OSD, "osd", "Object Specific Data");
67 
68 static int osd_debug = 0;
69 SYSCTL_INT(_debug, OID_AUTO, osd, CTLFLAG_RWTUN, &osd_debug, 0, "OSD debug level");
70 
71 #define	OSD_DEBUG(...)	do {						\
72 	if (osd_debug) {						\
73 		printf("OSD (%s:%u): ", __func__, __LINE__);		\
74 		printf(__VA_ARGS__);					\
75 		printf("\n");						\
76 	}								\
77 } while (0)
78 
79 static void do_osd_del(u_int type, struct osd *osd, u_int slot,
80     int list_locked);
81 
82 /*
83  * List of objects with OSD.
84  */
85 struct osd_master osdm[OSD_LAST + 1] = {
86 	[OSD_JAIL] = { .osd_nmethods = PR_MAXMETHOD },
87 };
88 
89 static void
90 osd_default_destructor(void *value __unused)
91 {
92 	/* Do nothing. */
93 }
94 
95 int
96 osd_register(u_int type, osd_destructor_t destructor, osd_method_t *methods)
97 {
98 	void *newptr;
99 	u_int i, m;
100 
101 	KASSERT(type >= OSD_FIRST && type <= OSD_LAST, ("Invalid type."));
102 
103 	/*
104 	 * If no destructor is given, use default one. We need to use some
105 	 * destructor, because NULL destructor means unused slot.
106 	 */
107 	if (destructor == NULL)
108 		destructor = osd_default_destructor;
109 
110 	sx_xlock(&osdm[type].osd_module_lock);
111 	/*
112 	 * First, we try to find unused slot.
113 	 */
114 	for (i = 0; i < osdm[type].osd_ntslots; i++) {
115 		if (osdm[type].osd_destructors[i] == NULL) {
116 			OSD_DEBUG("Unused slot found (type=%u, slot=%u).",
117 			    type, i);
118 			break;
119 		}
120 	}
121 	/*
122 	 * If no unused slot was found, allocate one.
123 	 */
124 	if (i == osdm[type].osd_ntslots) {
125 		osdm[type].osd_ntslots++;
126 		if (osdm[type].osd_nmethods != 0)
127 			osdm[type].osd_methods = realloc(osdm[type].osd_methods,
128 			    sizeof(osd_method_t) * osdm[type].osd_ntslots *
129 			    osdm[type].osd_nmethods, M_OSD, M_WAITOK);
130 		newptr = malloc(sizeof(osd_destructor_t) *
131 		    osdm[type].osd_ntslots, M_OSD, M_WAITOK);
132 		rm_wlock(&osdm[type].osd_object_lock);
133 		bcopy(osdm[type].osd_destructors, newptr,
134 		    sizeof(osd_destructor_t) * i);
135 		free(osdm[type].osd_destructors, M_OSD);
136 		osdm[type].osd_destructors = newptr;
137 		rm_wunlock(&osdm[type].osd_object_lock);
138 		OSD_DEBUG("New slot allocated (type=%u, slot=%u).",
139 		    type, i + 1);
140 	}
141 
142 	osdm[type].osd_destructors[i] = destructor;
143 	if (osdm[type].osd_nmethods != 0) {
144 		for (m = 0; m < osdm[type].osd_nmethods; m++)
145 			osdm[type].osd_methods[i * osdm[type].osd_nmethods + m]
146 			    = methods != NULL ? methods[m] : NULL;
147 	}
148 	sx_xunlock(&osdm[type].osd_module_lock);
149 	return (i + 1);
150 }
151 
152 void
153 osd_deregister(u_int type, u_int slot)
154 {
155 	struct osd *osd, *tosd;
156 
157 	KASSERT(type >= OSD_FIRST && type <= OSD_LAST, ("Invalid type."));
158 	KASSERT(slot > 0, ("Invalid slot."));
159 
160 	sx_xlock(&osdm[type].osd_module_lock);
161 	rm_wlock(&osdm[type].osd_object_lock);
162 	KASSERT(osdm[type].osd_destructors[slot - 1] != NULL, ("Unused slot."));
163 
164 	/*
165 	 * Free all OSD for the given slot.
166 	 */
167 	mtx_lock(&osdm[type].osd_list_lock);
168 	LIST_FOREACH_SAFE(osd, &osdm[type].osd_list, osd_next, tosd)
169 		do_osd_del(type, osd, slot, 1);
170 	mtx_unlock(&osdm[type].osd_list_lock);
171 	/*
172 	 * Set destructor to NULL to free the slot.
173 	 */
174 	osdm[type].osd_destructors[slot - 1] = NULL;
175 	if (slot == osdm[type].osd_ntslots) {
176 		osdm[type].osd_ntslots--;
177 		osdm[type].osd_destructors = realloc(osdm[type].osd_destructors,
178 		    sizeof(osd_destructor_t) * osdm[type].osd_ntslots, M_OSD,
179 		    M_NOWAIT | M_ZERO);
180 		if (osdm[type].osd_nmethods != 0)
181 			osdm[type].osd_methods = realloc(osdm[type].osd_methods,
182 			    sizeof(osd_method_t) * osdm[type].osd_ntslots *
183 			    osdm[type].osd_nmethods, M_OSD, M_NOWAIT | M_ZERO);
184 		/*
185 		 * We always reallocate to smaller size, so we assume it will
186 		 * always succeed.
187 		 */
188 		KASSERT(osdm[type].osd_destructors != NULL &&
189 		    (osdm[type].osd_nmethods == 0 ||
190 		     osdm[type].osd_methods != NULL), ("realloc() failed"));
191 		OSD_DEBUG("Deregistration of the last slot (type=%u, slot=%u).",
192 		    type, slot);
193 	} else {
194 		OSD_DEBUG("Slot deregistration (type=%u, slot=%u).",
195 		    type, slot);
196 	}
197 	rm_wunlock(&osdm[type].osd_object_lock);
198 	sx_xunlock(&osdm[type].osd_module_lock);
199 }
200 
201 int
202 osd_set(u_int type, struct osd *osd, u_int slot, void *value)
203 {
204 
205 	return (osd_set_reserved(type, osd, slot, NULL, value));
206 }
207 
208 void **
209 osd_reserve(u_int slot)
210 {
211 
212 	KASSERT(slot > 0, ("Invalid slot."));
213 
214 	OSD_DEBUG("Reserving slot array (slot=%u).", slot);
215 	return (malloc(sizeof(void *) * slot, M_OSD, M_WAITOK | M_ZERO));
216 }
217 
218 int
219 osd_set_reserved(u_int type, struct osd *osd, u_int slot, void **rsv,
220     void *value)
221 {
222 	struct rm_priotracker tracker;
223 
224 	KASSERT(type >= OSD_FIRST && type <= OSD_LAST, ("Invalid type."));
225 	KASSERT(slot > 0, ("Invalid slot."));
226 
227 	rm_rlock(&osdm[type].osd_object_lock, &tracker);
228 	KASSERT(osdm[type].osd_destructors[slot - 1] != NULL, ("Unused slot."));
229 
230 	if (slot > osd->osd_nslots) {
231 		void **newptr;
232 
233 		if (value == NULL) {
234 			OSD_DEBUG(
235 			    "Not allocating null slot (type=%u, slot=%u).",
236 			    type, slot);
237 			rm_runlock(&osdm[type].osd_object_lock, &tracker);
238 			if (rsv)
239 				osd_free_reserved(rsv);
240 			return (0);
241 		}
242 
243 		/*
244 		 * Too few slots allocated here, so we need to extend or create
245 		 * the array.
246 		 */
247 		if (rsv) {
248 			/*
249 			 * Use the reserve passed in (assumed to be
250 			 * the right size).
251 			 */
252 			newptr = rsv;
253 			if (osd->osd_nslots != 0) {
254 				memcpy(newptr, osd->osd_slots,
255 				    sizeof(void *) * osd->osd_nslots);
256 				free(osd->osd_slots, M_OSD);
257 			}
258 		} else {
259 			newptr = realloc(osd->osd_slots, sizeof(void *) * slot,
260 			    M_OSD, M_NOWAIT | M_ZERO);
261 			if (newptr == NULL) {
262 				rm_runlock(&osdm[type].osd_object_lock,
263 				    &tracker);
264 				return (ENOMEM);
265 			}
266 		}
267 		if (osd->osd_nslots == 0) {
268 			/*
269 			 * First OSD for this object, so we need to put it
270 			 * onto the list.
271 			 */
272 			mtx_lock(&osdm[type].osd_list_lock);
273 			LIST_INSERT_HEAD(&osdm[type].osd_list, osd, osd_next);
274 			mtx_unlock(&osdm[type].osd_list_lock);
275 			OSD_DEBUG("Setting first slot (type=%u).", type);
276 		} else
277 			OSD_DEBUG("Growing slots array (type=%u).", type);
278 		osd->osd_slots = newptr;
279 		osd->osd_nslots = slot;
280 	} else if (rsv)
281 		osd_free_reserved(rsv);
282 	OSD_DEBUG("Setting slot value (type=%u, slot=%u, value=%p).", type,
283 	    slot, value);
284 	osd->osd_slots[slot - 1] = value;
285 	rm_runlock(&osdm[type].osd_object_lock, &tracker);
286 	return (0);
287 }
288 
289 void
290 osd_free_reserved(void **rsv)
291 {
292 
293 	OSD_DEBUG("Discarding reserved slot array.");
294 	free(rsv, M_OSD);
295 }
296 
297 void *
298 osd_get(u_int type, struct osd *osd, u_int slot)
299 {
300 	struct rm_priotracker tracker;
301 	void *value;
302 
303 	KASSERT(type >= OSD_FIRST && type <= OSD_LAST, ("Invalid type."));
304 	KASSERT(slot > 0, ("Invalid slot."));
305 
306 	rm_rlock(&osdm[type].osd_object_lock, &tracker);
307 	KASSERT(osdm[type].osd_destructors[slot - 1] != NULL, ("Unused slot."));
308 
309 	if (slot > osd->osd_nslots) {
310 		value = NULL;
311 		OSD_DEBUG("Slot doesn't exist (type=%u, slot=%u).", type, slot);
312 	} else {
313 		value = osd->osd_slots[slot - 1];
314 		OSD_DEBUG("Returning slot value (type=%u, slot=%u, value=%p).",
315 		    type, slot, value);
316 	}
317 	rm_runlock(&osdm[type].osd_object_lock, &tracker);
318 	return (value);
319 }
320 
321 void
322 osd_del(u_int type, struct osd *osd, u_int slot)
323 {
324 	struct rm_priotracker tracker;
325 
326 	rm_rlock(&osdm[type].osd_object_lock, &tracker);
327 	do_osd_del(type, osd, slot, 0);
328 	rm_runlock(&osdm[type].osd_object_lock, &tracker);
329 }
330 
331 static void
332 do_osd_del(u_int type, struct osd *osd, u_int slot, int list_locked)
333 {
334 	int i;
335 
336 	KASSERT(type >= OSD_FIRST && type <= OSD_LAST, ("Invalid type."));
337 	KASSERT(slot > 0, ("Invalid slot."));
338 	KASSERT(osdm[type].osd_destructors[slot - 1] != NULL, ("Unused slot."));
339 
340 	OSD_DEBUG("Deleting slot (type=%u, slot=%u).", type, slot);
341 
342 	if (slot > osd->osd_nslots) {
343 		OSD_DEBUG("Slot doesn't exist (type=%u, slot=%u).", type, slot);
344 		return;
345 	}
346 	if (osd->osd_slots[slot - 1] != NULL) {
347 		osdm[type].osd_destructors[slot - 1](osd->osd_slots[slot - 1]);
348 		osd->osd_slots[slot - 1] = NULL;
349 	}
350 	for (i = osd->osd_nslots - 1; i >= 0; i--) {
351 		if (osd->osd_slots[i] != NULL) {
352 			OSD_DEBUG("Slot still has a value (type=%u, slot=%u).",
353 			    type, i + 1);
354 			break;
355 		}
356 	}
357 	if (i == -1) {
358 		/* No values left for this object. */
359 		OSD_DEBUG("No more slots left (type=%u).", type);
360 		if (!list_locked)
361 			mtx_lock(&osdm[type].osd_list_lock);
362 		LIST_REMOVE(osd, osd_next);
363 		if (!list_locked)
364 			mtx_unlock(&osdm[type].osd_list_lock);
365 		free(osd->osd_slots, M_OSD);
366 		osd->osd_slots = NULL;
367 		osd->osd_nslots = 0;
368 	} else if (slot == osd->osd_nslots) {
369 		/* This was the last slot. */
370 		osd->osd_slots = realloc(osd->osd_slots,
371 		    sizeof(void *) * (i + 1), M_OSD, M_NOWAIT | M_ZERO);
372 		/*
373 		 * We always reallocate to smaller size, so we assume it will
374 		 * always succeed.
375 		 */
376 		KASSERT(osd->osd_slots != NULL, ("realloc() failed"));
377 		osd->osd_nslots = i + 1;
378 		OSD_DEBUG("Reducing slots array to %u (type=%u).",
379 		    osd->osd_nslots, type);
380 	}
381 }
382 
383 int
384 osd_call(u_int type, u_int method, void *obj, void *data)
385 {
386 	osd_method_t methodfun;
387 	int error, i;
388 
389 	KASSERT(type >= OSD_FIRST && type <= OSD_LAST, ("Invalid type."));
390 	KASSERT(method < osdm[type].osd_nmethods, ("Invalid method."));
391 
392 	/*
393 	 * Call this method for every slot that defines it, stopping if an
394 	 * error is encountered.
395 	 */
396 	error = 0;
397 	sx_slock(&osdm[type].osd_module_lock);
398 	for (i = 0; i < osdm[type].osd_ntslots; i++) {
399 		methodfun = osdm[type].osd_methods[i * osdm[type].osd_nmethods +
400 		    method];
401 		if (methodfun != NULL && (error = methodfun(obj, data)) != 0)
402 			break;
403 	}
404 	sx_sunlock(&osdm[type].osd_module_lock);
405 	return (error);
406 }
407 
408 void
409 osd_exit(u_int type, struct osd *osd)
410 {
411 	struct rm_priotracker tracker;
412 	u_int i;
413 
414 	KASSERT(type >= OSD_FIRST && type <= OSD_LAST, ("Invalid type."));
415 
416 	if (osd->osd_nslots == 0) {
417 		KASSERT(osd->osd_slots == NULL, ("Non-null osd_slots."));
418 		/* No OSD attached, just leave. */
419 		return;
420 	}
421 
422 	rm_rlock(&osdm[type].osd_object_lock, &tracker);
423 	for (i = 1; i <= osd->osd_nslots; i++) {
424 		if (osdm[type].osd_destructors[i - 1] != NULL)
425 			do_osd_del(type, osd, i, 0);
426 		else
427 			OSD_DEBUG("Unused slot (type=%u, slot=%u).", type, i);
428 	}
429 	rm_runlock(&osdm[type].osd_object_lock, &tracker);
430 	OSD_DEBUG("Object exit (type=%u).", type);
431 }
432 
433 static void
434 osd_init(void *arg __unused)
435 {
436 	u_int i;
437 
438 	for (i = OSD_FIRST; i <= OSD_LAST; i++) {
439 		sx_init(&osdm[i].osd_module_lock, "osd_module");
440 		rm_init(&osdm[i].osd_object_lock, "osd_object");
441 		mtx_init(&osdm[i].osd_list_lock, "osd_list", NULL, MTX_DEF);
442 		LIST_INIT(&osdm[i].osd_list);
443 		osdm[i].osd_destructors = NULL;
444 		osdm[i].osd_ntslots = 0;
445 		osdm[i].osd_methods = NULL;
446 	}
447 }
448 SYSINIT(osd, SI_SUB_LOCK, SI_ORDER_ANY, osd_init, NULL);
449