1 /* $NetBSD: dm_target.c,v 1.12 2010/01/04 00:14:41 haad Exp $ */
4 * Copyright (c) 2010-2011 Alex Hornung <alex@alexhornung.com>
5 * Copyright (c) 2008 The NetBSD Foundation, Inc.
8 * This code is derived from software contributed to The NetBSD Foundation
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code Must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/types.h>
35 #include <sys/malloc.h>
36 #include <sys/module.h>
37 #include <sys/linker.h>
38 #include <dev/disk/dm/dm.h>
40 #include "netbsd-dm.h"
42 static dm_target_t *dm_target_lookup_name(const char *);
44 static TAILQ_HEAD(, dm_target) dm_target_list;
46 static struct lock dm_target_mutex;
49 * Called indirectly from dm_table_load_ioctl to mark target as used.
52 dm_target_busy(dm_target_t *target)
54 atomic_add_int(&target->ref_cnt, 1);
57 * Release reference counter on target.
60 dm_target_unbusy(dm_target_t *target)
62 KKASSERT(target->ref_cnt > 0);
63 atomic_subtract_int(&target->ref_cnt, 1);
67 * Try to autoload the module for the requested target.
70 dm_target_autoload(const char *dm_target_name)
74 linker_file_t linker_file;
77 ksnprintf(mod_name, sizeof(mod_name), "dm_target_%s", dm_target_name);
78 error = linker_reference_module(mod_name, NULL, &linker_file);
80 kprintf("dm: could not autoload module for target %s\n",
85 dmt = dm_target_lookup(dm_target_name);
87 linker_release_module(NULL, NULL, linker_file);
91 /* XXX: extra-big hack to allow users to kldunload the module */
92 linker_file->userrefs = 1;
98 * Lookup for target in global target list.
101 dm_target_lookup(const char *dm_target_name)
107 if (dm_target_name == NULL)
110 lockmgr(&dm_target_mutex, LK_EXCLUSIVE);
112 dmt = dm_target_lookup_name(dm_target_name);
116 lockmgr(&dm_target_mutex, LK_RELEASE);
121 * Search for name in TAIL and return apropriate pointer.
124 dm_target_lookup_name(const char *dm_target_name)
126 dm_target_t *dm_target;
128 TAILQ_FOREACH(dm_target, &dm_target_list, dm_target_next) {
129 if (strcmp(dm_target_name, dm_target->name) == 0)
136 * Insert new target struct into the TAIL.
138 * contains name, version, function pointer to specifif target functions.
141 dm_target_insert(dm_target_t *dm_target)
145 if (dm_target->init == NULL) {
146 kprintf("dm: %s missing init\n", dm_target->name);
149 if (dm_target->destroy == NULL) {
150 kprintf("dm: %s missing destroy\n", dm_target->name);
153 if (dm_target->strategy == NULL) {
154 kprintf("dm: %s missing strategy\n", dm_target->name);
158 lockmgr(&dm_target_mutex, LK_EXCLUSIVE);
160 dmt = dm_target_lookup_name(dm_target->name);
162 kprintf("uhoh, target_insert EEXIST\n");
163 lockmgr(&dm_target_mutex, LK_RELEASE);
166 TAILQ_INSERT_TAIL(&dm_target_list, dm_target, dm_target_next);
168 lockmgr(&dm_target_mutex, LK_RELEASE);
175 * Remove target from TAIL, target is selected with it's name.
178 dm_target_remove(char *dm_target_name)
182 KKASSERT(dm_target_name != NULL);
184 lockmgr(&dm_target_mutex, LK_EXCLUSIVE);
186 dmt = dm_target_lookup_name(dm_target_name);
188 lockmgr(&dm_target_mutex, LK_RELEASE);
191 if (dmt->ref_cnt > 0) {
192 lockmgr(&dm_target_mutex, LK_RELEASE);
195 TAILQ_REMOVE(&dm_target_list,
196 dmt, dm_target_next);
198 lockmgr(&dm_target_mutex, LK_RELEASE);
206 * Allocate new target entry.
209 dm_target_alloc(const char *name)
213 dmt = kmalloc(sizeof(*dmt), M_DM, M_WAITOK | M_ZERO);
218 strlcpy(dmt->name, name, sizeof(dmt->name));
224 dm_target_free(dm_target_t *dmt)
226 KKASSERT(dmt != NULL);
234 * Return prop_array of dm_target dictionaries.
237 dm_target_prop_list(void)
239 prop_array_t target_array, ver;
240 prop_dictionary_t target_dict;
241 dm_target_t *dm_target;
245 target_array = prop_array_create();
247 lockmgr(&dm_target_mutex, LK_EXCLUSIVE);
249 TAILQ_FOREACH(dm_target, &dm_target_list, dm_target_next) {
251 target_dict = prop_dictionary_create();
252 ver = prop_array_create();
253 prop_dictionary_set_cstring(target_dict, DM_TARGETS_NAME,
256 for (i = 0; i < 3; i++)
257 prop_array_add_uint32(ver, dm_target->version[i]);
259 prop_dictionary_set(target_dict, DM_TARGETS_VERSION, ver);
260 prop_array_add(target_array, target_dict);
262 prop_object_release(ver);
263 prop_object_release(target_dict);
266 lockmgr(&dm_target_mutex, LK_RELEASE);
271 /* Initialize dm_target subsystem. */
275 TAILQ_INIT(&dm_target_list); /* initialize global target list */
276 lockinit(&dm_target_mutex, "dmtrgt", 0, LK_CANRECURSE);
282 * Destroy all targets and remove them from queue.
283 * This routine is called from dmdestroy, before module
287 dm_target_uninit(void)
289 dm_target_t *dm_target;
291 lockmgr(&dm_target_mutex, LK_EXCLUSIVE);
293 while ((dm_target = TAILQ_FIRST(&dm_target_list)) != NULL) {
294 TAILQ_REMOVE(&dm_target_list, dm_target, dm_target_next);
295 dm_target_free(dm_target);
297 KKASSERT(TAILQ_EMPTY(&dm_target_list));
299 lockmgr(&dm_target_mutex, LK_RELEASE);
301 lockuninit(&dm_target_mutex);