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