dm - undo my pointless moving of dm.h
[dragonfly.git] / sys / dev / disk / dm / dm_target.c
CommitLineData
ff56536e
AH
1/* $NetBSD: dm_target.c,v 1.12 2010/01/04 00:14:41 haad Exp $ */
2
3/*
4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Adam Hamsik.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code Must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#include <sys/types.h>
33#include <sys/param.h>
34
5b279a20 35#include <sys/malloc.h>
ff56536e 36#include <sys/module.h>
7115a22b 37#include <sys/linker.h>
a84e173e 38#include <dev/disk/dm/dm.h>
ff56536e
AH
39
40#include "netbsd-dm.h"
ff56536e
AH
41
42static dm_target_t *dm_target_lookup_name(const char *);
43
44TAILQ_HEAD(dm_target_head, dm_target);
45
46static struct dm_target_head dm_target_list =
47TAILQ_HEAD_INITIALIZER(dm_target_list);
48
5b279a20 49struct lock dm_target_mutex;
ff56536e
AH
50
51/*
52 * Called indirectly from dm_table_load_ioctl to mark target as used.
53 */
54void
55dm_target_busy(dm_target_t * target)
56{
5b279a20 57 atomic_add_int(&target->ref_cnt, 1);
ff56536e
AH
58}
59/*
60 * Release reference counter on target.
61 */
62void
63dm_target_unbusy(dm_target_t * target)
64{
5b279a20
AH
65 KKASSERT(target->ref_cnt > 0);
66 atomic_subtract_int(&target->ref_cnt, 1);
ff56536e 67}
ff56536e 68
ff56536e 69/*
7115a22b
AH
70 * Try to autoload the module for the requested target.
71 */
72dm_target_t *
73dm_target_autoload(const char *dm_target_name)
74{
75 char mod_name[128];
76 dm_target_t *dmt;
77 linker_file_t linker_file;
78 int error;
79
80 ksnprintf(mod_name, sizeof(mod_name), "dm_target_%s", dm_target_name);
81 error = linker_reference_module(mod_name, NULL, &linker_file);
82 if (error != 0) {
83 kprintf("dm: could not autoload module for target %s\n",
84 dm_target_name);
85 return NULL;
86 }
87
88 dmt = dm_target_lookup(dm_target_name);
89 if (dmt == NULL) {
90 linker_release_module(NULL, NULL, linker_file);
91 return NULL;
92 }
93
94 /* XXX: extra-big hack to allow users to kldunload the module */
95 linker_file->userrefs = 1;
96
97 return dmt;
98}
99
100/*
ff56536e
AH
101 * Lookup for target in global target list.
102 */
103dm_target_t *
104dm_target_lookup(const char *dm_target_name)
105{
106 dm_target_t *dmt;
107
108 dmt = NULL;
109
110 if (dm_target_name == NULL)
111 return NULL;
112
5b279a20 113 lockmgr(&dm_target_mutex, LK_EXCLUSIVE);
ff56536e
AH
114
115 dmt = dm_target_lookup_name(dm_target_name);
116 if (dmt != NULL)
117 dm_target_busy(dmt);
118
5b279a20 119 lockmgr(&dm_target_mutex, LK_RELEASE);
ff56536e
AH
120
121 return dmt;
122}
123/*
124 * Search for name in TAIL and return apropriate pointer.
125 */
126static dm_target_t *
127dm_target_lookup_name(const char *dm_target_name)
128{
129 dm_target_t *dm_target;
ff56536e
AH
130
131 TAILQ_FOREACH(dm_target, &dm_target_list, dm_target_next) {
0d42d35b 132 if (strcmp(dm_target_name, dm_target->name) == 0)
ff56536e
AH
133 return dm_target;
134 }
135
136 return NULL;
137}
138/*
139 * Insert new target struct into the TAIL.
140 * dm_target
141 * contains name, version, function pointer to specifif target functions.
142 */
143int
144dm_target_insert(dm_target_t * dm_target)
145{
146 dm_target_t *dmt;
147
5b279a20 148 lockmgr(&dm_target_mutex, LK_EXCLUSIVE);
ff56536e
AH
149
150 dmt = dm_target_lookup_name(dm_target->name);
151 if (dmt != NULL) {
5b279a20
AH
152 kprintf("uhoh, target_insert EEXIST\n");
153 lockmgr(&dm_target_mutex, LK_RELEASE);
ff56536e
AH
154 return EEXIST;
155 }
156 TAILQ_INSERT_TAIL(&dm_target_list, dm_target, dm_target_next);
157
5b279a20 158 lockmgr(&dm_target_mutex, LK_RELEASE);
ff56536e
AH
159
160 return 0;
161}
162
163
164/*
165 * Remove target from TAIL, target is selected with it's name.
166 */
167int
168dm_target_rem(char *dm_target_name)
169{
170 dm_target_t *dmt;
171
5b279a20 172 KKASSERT(dm_target_name != NULL);
ff56536e 173
5b279a20 174 lockmgr(&dm_target_mutex, LK_EXCLUSIVE);
ff56536e
AH
175
176 dmt = dm_target_lookup_name(dm_target_name);
177 if (dmt == NULL) {
5b279a20 178 lockmgr(&dm_target_mutex, LK_RELEASE);
ff56536e
AH
179 return ENOENT;
180 }
181 if (dmt->ref_cnt > 0) {
5b279a20 182 lockmgr(&dm_target_mutex, LK_RELEASE);
ff56536e
AH
183 return EBUSY;
184 }
185 TAILQ_REMOVE(&dm_target_list,
186 dmt, dm_target_next);
187
5b279a20 188 lockmgr(&dm_target_mutex, LK_RELEASE);
ff56536e 189
5b279a20 190 (void) kfree(dmt, M_DM);
ff56536e
AH
191
192 return 0;
193}
194/*
195 * Destroy all targets and remove them from queue.
196 * This routine is called from dm_detach, before module
197 * is unloaded.
198 */
199int
200dm_target_destroy(void)
201{
202 dm_target_t *dm_target;
203
5b279a20 204 lockmgr(&dm_target_mutex, LK_EXCLUSIVE);
ff56536e
AH
205 while (TAILQ_FIRST(&dm_target_list) != NULL) {
206
207 dm_target = TAILQ_FIRST(&dm_target_list);
208
209 TAILQ_REMOVE(&dm_target_list, TAILQ_FIRST(&dm_target_list),
210 dm_target_next);
211
5b279a20 212 (void) kfree(dm_target, M_DM);
ff56536e 213 }
5b279a20 214 lockmgr(&dm_target_mutex, LK_RELEASE);
ff56536e 215
5b279a20 216 lockuninit(&dm_target_mutex);
ff56536e
AH
217
218 return 0;
219}
220/*
221 * Allocate new target entry.
222 */
223dm_target_t *
224dm_target_alloc(const char *name)
225{
5b279a20 226 return kmalloc(sizeof(dm_target_t), M_DM, M_WAITOK | M_ZERO);
ff56536e
AH
227}
228/*
229 * Return prop_array of dm_target dictionaries.
230 */
231prop_array_t
232dm_target_prop_list(void)
233{
234 prop_array_t target_array, ver;
235 prop_dictionary_t target_dict;
236 dm_target_t *dm_target;
237
238 size_t i;
239
240 target_array = prop_array_create();
241
5b279a20 242 lockmgr(&dm_target_mutex, LK_EXCLUSIVE);
ff56536e
AH
243
244 TAILQ_FOREACH(dm_target, &dm_target_list, dm_target_next) {
245
246 target_dict = prop_dictionary_create();
247 ver = prop_array_create();
248 prop_dictionary_set_cstring(target_dict, DM_TARGETS_NAME,
249 dm_target->name);
250
251 for (i = 0; i < 3; i++)
252 prop_array_add_uint32(ver, dm_target->version[i]);
253
254 prop_dictionary_set(target_dict, DM_TARGETS_VERSION, ver);
255 prop_array_add(target_array, target_dict);
256
257 prop_object_release(ver);
258 prop_object_release(target_dict);
259 }
260
5b279a20 261 lockmgr(&dm_target_mutex, LK_RELEASE);
ff56536e
AH
262
263 return target_array;
264}
5c411e8e 265
ff56536e
AH
266/* Initialize dm_target subsystem. */
267int
268dm_target_init(void)
269{
5b279a20 270 lockinit(&dm_target_mutex, "dmtrgt", 0, LK_CANRECURSE);
ff56536e 271
5c411e8e 272 return 0;
ff56536e 273}