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/malloc.h>
34 #include <sys/module.h>
35 #include <sys/linker.h>
36 #include <cpu/atomic.h>
37 #include <dev/disk/dm/dm.h>
38 #include <dev/disk/dm/netbsd-dm.h>
40 static dm_target_t *dm_target_lookup_name(const char *);
42 static TAILQ_HEAD(, dm_target) dm_target_list;
44 static struct lock dm_target_mutex;
47 dm_target_busy(dm_target_t *target)
49 atomic_add_int(&target->ref_cnt, 1);
53 * Release reference counter on target.
56 dm_target_unbusy(dm_target_t *target)
58 KKASSERT(target->ref_cnt > 0);
59 atomic_subtract_int(&target->ref_cnt, 1);
63 * Try to autoload the module for the requested target.
66 dm_target_autoload(const char *dm_target_name)
70 linker_file_t linker_file;
73 ksnprintf(mod_name, sizeof(mod_name), "dm_target_%s", dm_target_name);
74 error = linker_reference_module(mod_name, NULL, &linker_file);
76 kprintf("dm: could not autoload module for target %s\n",
81 dmt = dm_target_lookup(dm_target_name);
83 linker_release_module(NULL, NULL, linker_file);
87 /* XXX: extra-big hack to allow users to kldunload the module */
88 linker_file->userrefs = 1;
94 * Lookup for target in global target list.
97 dm_target_lookup(const char *dm_target_name)
103 if (dm_target_name == NULL)
106 lockmgr(&dm_target_mutex, LK_EXCLUSIVE);
108 dmt = dm_target_lookup_name(dm_target_name);
112 lockmgr(&dm_target_mutex, LK_RELEASE);
118 * Search for name in TAILQ and return apropriate pointer.
121 dm_target_lookup_name(const char *dm_target_name)
123 dm_target_t *dm_target;
125 TAILQ_FOREACH(dm_target, &dm_target_list, dm_target_next) {
126 if (strcmp(dm_target_name, dm_target->name) == 0)
134 * Insert new target struct into the TAILQ.
135 * dm_target contains name, version, function pointer to specific target
139 dm_target_insert(dm_target_t *dm_target)
143 if (dm_target->strategy == NULL) {
144 kprintf("dm: %s missing strategy\n", dm_target->name);
148 lockmgr(&dm_target_mutex, LK_EXCLUSIVE);
150 dmt = dm_target_lookup_name(dm_target->name);
152 kprintf("uhoh, target_insert EEXIST\n");
153 lockmgr(&dm_target_mutex, LK_RELEASE);
156 TAILQ_INSERT_TAIL(&dm_target_list, dm_target, dm_target_next);
158 lockmgr(&dm_target_mutex, LK_RELEASE);
164 * Remove target from TAILQ, target is selected with it's name.
167 dm_target_remove(char *dm_target_name)
171 KKASSERT(dm_target_name != NULL);
173 lockmgr(&dm_target_mutex, LK_EXCLUSIVE);
175 dmt = dm_target_lookup_name(dm_target_name);
177 lockmgr(&dm_target_mutex, LK_RELEASE);
180 if (dmt->ref_cnt > 0) {
181 lockmgr(&dm_target_mutex, LK_RELEASE);
184 TAILQ_REMOVE(&dm_target_list, dmt, dm_target_next);
186 lockmgr(&dm_target_mutex, LK_RELEASE);
194 * Allocate new target entry.
197 dm_target_alloc(const char *name)
201 dmt = kmalloc(sizeof(*dmt), M_DM, M_WAITOK | M_ZERO);
206 strlcpy(dmt->name, name, sizeof(dmt->name));
212 dm_target_free(dm_target_t *dmt)
214 KKASSERT(dmt != NULL);
222 * Return prop_array of dm_target dictionaries.
225 dm_target_prop_list(void)
227 prop_array_t target_array, ver;
228 prop_dictionary_t target_dict;
229 dm_target_t *dm_target;
233 target_array = prop_array_create();
235 lockmgr(&dm_target_mutex, LK_EXCLUSIVE);
237 TAILQ_FOREACH(dm_target, &dm_target_list, dm_target_next) {
239 target_dict = prop_dictionary_create();
240 ver = prop_array_create();
241 prop_dictionary_set_cstring(target_dict, DM_TARGETS_NAME,
244 for (i = 0; i < 3; i++)
245 prop_array_add_uint32(ver, dm_target->version[i]);
247 prop_dictionary_set(target_dict, DM_TARGETS_VERSION, ver);
248 prop_array_add(target_array, target_dict);
250 prop_object_release(ver);
251 prop_object_release(target_dict);
254 lockmgr(&dm_target_mutex, LK_RELEASE);
260 * Initialize dm_target subsystem.
265 TAILQ_INIT(&dm_target_list); /* initialize global target list */
266 lockinit(&dm_target_mutex, "dmtrgt", 0, LK_CANRECURSE);
272 * Destroy all targets and remove them from queue.
273 * This routine is called from dmdestroy, before module
277 dm_target_uninit(void)
279 dm_target_t *dm_target;
281 lockmgr(&dm_target_mutex, LK_EXCLUSIVE);
283 while ((dm_target = TAILQ_FIRST(&dm_target_list)) != NULL) {
284 TAILQ_REMOVE(&dm_target_list, dm_target, dm_target_next);
285 dm_target_free(dm_target);
287 KKASSERT(TAILQ_EMPTY(&dm_target_list));
289 lockmgr(&dm_target_mutex, LK_RELEASE);
291 lockuninit(&dm_target_mutex);