xref: /freebsd-13-stable/sys/kern/kern_osd.c (revision 08eab7b3eb49034add710863760acc8406649eed)
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
osd_default_destructor(void * value __unused)88 osd_default_destructor(void *value __unused)
89 {
90 	/* Do nothing. */
91 }
92 
93 int
osd_register(u_int type,osd_destructor_t destructor,const osd_method_t * methods)94 osd_register(u_int type, osd_destructor_t destructor, const 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);
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
osd_deregister(u_int type,u_int slot)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 	 * Set destructor to NULL to free the slot.
171 	 */
172 	osdm[type].osd_destructors[slot - 1] = NULL;
173 	if (slot == osdm[type].osd_ntslots) {
174 		osdm[type].osd_ntslots--;
175 		osdm[type].osd_destructors = realloc(osdm[type].osd_destructors,
176 		    sizeof(osd_destructor_t) * osdm[type].osd_ntslots, M_OSD,
177 		    M_NOWAIT | M_ZERO);
178 		if (osdm[type].osd_nmethods != 0)
179 			osdm[type].osd_methods = realloc(osdm[type].osd_methods,
180 			    sizeof(osd_method_t) * osdm[type].osd_ntslots *
181 			    osdm[type].osd_nmethods, M_OSD, M_NOWAIT | M_ZERO);
182 		/*
183 		 * We always reallocate to smaller size, so we assume it will
184 		 * always succeed.
185 		 */
186 		KASSERT(osdm[type].osd_destructors != NULL &&
187 		    (osdm[type].osd_nmethods == 0 ||
188 		     osdm[type].osd_methods != NULL), ("realloc() failed"));
189 		OSD_DEBUG("Deregistration of the last slot (type=%u, slot=%u).",
190 		    type, slot);
191 	} else {
192 		OSD_DEBUG("Slot deregistration (type=%u, slot=%u).",
193 		    type, slot);
194 	}
195 	rm_wunlock(&osdm[type].osd_object_lock);
196 	sx_xunlock(&osdm[type].osd_module_lock);
197 }
198 
199 int
osd_set(u_int type,struct osd * osd,u_int slot,void * value)200 osd_set(u_int type, struct osd *osd, u_int slot, void *value)
201 {
202 
203 	return (osd_set_reserved(type, osd, slot, NULL, value));
204 }
205 
206 void **
osd_reserve(u_int slot)207 osd_reserve(u_int slot)
208 {
209 
210 	KASSERT(slot > 0, ("Invalid slot."));
211 
212 	OSD_DEBUG("Reserving slot array (slot=%u).", slot);
213 	return (malloc(sizeof(void *) * slot, M_OSD, M_WAITOK | M_ZERO));
214 }
215 
216 int
osd_set_reserved(u_int type,struct osd * osd,u_int slot,void ** rsv,void * value)217 osd_set_reserved(u_int type, struct osd *osd, u_int slot, void **rsv,
218     void *value)
219 {
220 	struct rm_priotracker tracker;
221 
222 	KASSERT(type >= OSD_FIRST && type <= OSD_LAST, ("Invalid type."));
223 	KASSERT(slot > 0, ("Invalid slot."));
224 
225 	rm_rlock(&osdm[type].osd_object_lock, &tracker);
226 	KASSERT(osdm[type].osd_destructors[slot - 1] != NULL, ("Unused slot."));
227 
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
osd_free_reserved(void ** rsv)288 osd_free_reserved(void **rsv)
289 {
290 
291 	OSD_DEBUG("Discarding reserved slot array.");
292 	free(rsv, M_OSD);
293 }
294 
295 void *
osd_get_unlocked(u_int type,struct osd * osd,u_int slot)296 osd_get_unlocked(u_int type, struct osd *osd, u_int slot)
297 {
298 	void *value;
299 
300 	KASSERT(osdm[type].osd_destructors[slot - 1] != NULL, ("Unused slot."));
301 
302 	if (slot > osd->osd_nslots) {
303 		value = NULL;
304 		OSD_DEBUG("Slot doesn't exist (type=%u, slot=%u).", type, slot);
305 	} else {
306 		value = atomic_load_ptr(&osd->osd_slots[slot - 1]);
307 		OSD_DEBUG("Returning slot value (type=%u, slot=%u, value=%p).",
308 		    type, slot, value);
309 	}
310 	return (value);
311 }
312 
313 void *
osd_get(u_int type,struct osd * osd,u_int slot)314 osd_get(u_int type, struct osd *osd, u_int slot)
315 {
316 	struct rm_priotracker tracker;
317 	void *value;
318 
319 	KASSERT(type >= OSD_FIRST && type <= OSD_LAST, ("Invalid type."));
320 	KASSERT(slot > 0, ("Invalid slot."));
321 
322 	rm_rlock(&osdm[type].osd_object_lock, &tracker);
323 	value = osd_get_unlocked(type, osd, slot);
324 	rm_runlock(&osdm[type].osd_object_lock, &tracker);
325 	return (value);
326 }
327 
328 void
osd_del(u_int type,struct osd * osd,u_int slot)329 osd_del(u_int type, struct osd *osd, u_int slot)
330 {
331 	struct rm_priotracker tracker;
332 
333 	rm_rlock(&osdm[type].osd_object_lock, &tracker);
334 	do_osd_del(type, osd, slot, 0);
335 	rm_runlock(&osdm[type].osd_object_lock, &tracker);
336 }
337 
338 static void
do_osd_del(u_int type,struct osd * osd,u_int slot,int list_locked)339 do_osd_del(u_int type, struct osd *osd, u_int slot, int list_locked)
340 {
341 	int i;
342 
343 	KASSERT(type >= OSD_FIRST && type <= OSD_LAST, ("Invalid type."));
344 	KASSERT(slot > 0, ("Invalid slot."));
345 	KASSERT(osdm[type].osd_destructors[slot - 1] != NULL, ("Unused slot."));
346 
347 	OSD_DEBUG("Deleting slot (type=%u, slot=%u).", type, slot);
348 
349 	if (slot > osd->osd_nslots) {
350 		OSD_DEBUG("Slot doesn't exist (type=%u, slot=%u).", type, slot);
351 		return;
352 	}
353 	if (osd->osd_slots[slot - 1] != NULL) {
354 		osdm[type].osd_destructors[slot - 1](osd->osd_slots[slot - 1]);
355 		osd->osd_slots[slot - 1] = NULL;
356 	}
357 	for (i = osd->osd_nslots - 1; i >= 0; i--) {
358 		if (osd->osd_slots[i] != NULL) {
359 			OSD_DEBUG("Slot still has a value (type=%u, slot=%u).",
360 			    type, i + 1);
361 			break;
362 		}
363 	}
364 	if (i == -1) {
365 		/* No values left for this object. */
366 		OSD_DEBUG("No more slots left (type=%u).", type);
367 		if (!list_locked)
368 			mtx_lock(&osdm[type].osd_list_lock);
369 		LIST_REMOVE(osd, osd_next);
370 		if (!list_locked)
371 			mtx_unlock(&osdm[type].osd_list_lock);
372 		free(osd->osd_slots, M_OSD);
373 		osd->osd_slots = NULL;
374 		osd->osd_nslots = 0;
375 	} else if (slot == osd->osd_nslots) {
376 		/* This was the last slot. */
377 		osd->osd_slots = realloc(osd->osd_slots,
378 		    sizeof(void *) * (i + 1), M_OSD, M_NOWAIT | M_ZERO);
379 		/*
380 		 * We always reallocate to smaller size, so we assume it will
381 		 * always succeed.
382 		 */
383 		KASSERT(osd->osd_slots != NULL, ("realloc() failed"));
384 		osd->osd_nslots = i + 1;
385 		OSD_DEBUG("Reducing slots array to %u (type=%u).",
386 		    osd->osd_nslots, type);
387 	}
388 }
389 
390 int
osd_call(u_int type,u_int method,void * obj,void * data)391 osd_call(u_int type, u_int method, void *obj, void *data)
392 {
393 	osd_method_t methodfun;
394 	int error, i;
395 
396 	KASSERT(type >= OSD_FIRST && type <= OSD_LAST, ("Invalid type."));
397 	KASSERT(method < osdm[type].osd_nmethods, ("Invalid method."));
398 
399 	/*
400 	 * Call this method for every slot that defines it, stopping if an
401 	 * error is encountered.
402 	 */
403 	error = 0;
404 	sx_slock(&osdm[type].osd_module_lock);
405 	for (i = 0; i < osdm[type].osd_ntslots; i++) {
406 		/* Hole in the slot map; avoid dereferencing. */
407 		if (osdm[type].osd_destructors[i] == NULL)
408 			continue;
409 		methodfun = osdm[type].osd_methods[i * osdm[type].osd_nmethods +
410 		    method];
411 		if (methodfun != NULL && (error = methodfun(obj, data)) != 0)
412 			break;
413 	}
414 	sx_sunlock(&osdm[type].osd_module_lock);
415 	return (error);
416 }
417 
418 void
osd_exit(u_int type,struct osd * osd)419 osd_exit(u_int type, struct osd *osd)
420 {
421 	struct rm_priotracker tracker;
422 	u_int i;
423 
424 	KASSERT(type >= OSD_FIRST && type <= OSD_LAST, ("Invalid type."));
425 
426 	if (osd->osd_nslots == 0) {
427 		KASSERT(osd->osd_slots == NULL, ("Non-null osd_slots."));
428 		/* No OSD attached, just leave. */
429 		return;
430 	}
431 
432 	rm_rlock(&osdm[type].osd_object_lock, &tracker);
433 	for (i = 1; i <= osd->osd_nslots; i++) {
434 		if (osdm[type].osd_destructors[i - 1] != NULL)
435 			do_osd_del(type, osd, i, 0);
436 		else
437 			OSD_DEBUG("Unused slot (type=%u, slot=%u).", type, i);
438 	}
439 	rm_runlock(&osdm[type].osd_object_lock, &tracker);
440 	OSD_DEBUG("Object exit (type=%u).", type);
441 }
442 
443 static void
osd_init(void * arg __unused)444 osd_init(void *arg __unused)
445 {
446 	u_int i;
447 
448 	for (i = OSD_FIRST; i <= OSD_LAST; i++) {
449 		sx_init(&osdm[i].osd_module_lock, "osd_module");
450 		rm_init(&osdm[i].osd_object_lock, "osd_object");
451 		mtx_init(&osdm[i].osd_list_lock, "osd_list", NULL, MTX_DEF);
452 		LIST_INIT(&osdm[i].osd_list);
453 		osdm[i].osd_destructors = NULL;
454 		osdm[i].osd_ntslots = 0;
455 		osdm[i].osd_methods = NULL;
456 	}
457 }
458 SYSINIT(osd, SI_SUB_LOCK, SI_ORDER_ANY, osd_init, NULL);
459