No empty .Rs/.Re
[netbsd-mini2440.git] / sys / dev / dm / dm_target.c
blobb1e5f4f7174bb9e1ac7cc63b2ed917b3f2350da9
1 /* $NetBSD: dm_target.c,v 1.11 2009/09/09 22:38:49 haad Exp $ */
3 /*
4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Adam Hamsik.
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.
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.
32 #include <sys/types.h>
33 #include <sys/param.h>
35 #include <sys/kmem.h>
36 #include <sys/module.h>
39 #include "netbsd-dm.h"
40 #include "dm.h"
42 static dm_target_t *dm_target_lookup_name(const char *);
44 TAILQ_HEAD(dm_target_head, dm_target);
46 static struct dm_target_head dm_target_list =
47 TAILQ_HEAD_INITIALIZER(dm_target_list);
49 kmutex_t dm_target_mutex;
52 * Called indirectly from dm_table_load_ioctl to mark target as used.
54 void
55 dm_target_busy(dm_target_t * target)
57 atomic_inc_32(&target->ref_cnt);
60 * Release reference counter on target.
62 void
63 dm_target_unbusy(dm_target_t * target)
65 KASSERT(target->ref_cnt > 0);
66 atomic_dec_32(&target->ref_cnt);
69 * Try to autoload target module if it was not found in current
70 * target list.
72 dm_target_t *
73 dm_target_autoload(const char *dm_target_name)
75 char name[30];
76 u_int gen;
77 dm_target_t *dmt;
79 snprintf(name, sizeof(name), "dm_target_%s", dm_target_name);
80 name[29] = '\0';
82 do {
83 gen = module_gen;
85 /* Try to autoload target module */
86 mutex_enter(&module_lock);
87 (void) module_autoload(name, MODULE_CLASS_MISC);
88 mutex_exit(&module_lock);
89 } while (gen != module_gen);
91 mutex_enter(&dm_target_mutex);
92 dmt = dm_target_lookup_name(dm_target_name);
93 if (dmt != NULL)
94 dm_target_busy(dmt);
95 mutex_exit(&dm_target_mutex);
97 return dmt;
100 * Lookup for target in global target list.
102 dm_target_t *
103 dm_target_lookup(const char *dm_target_name)
105 dm_target_t *dmt;
107 dmt = NULL;
109 if (dm_target_name == NULL)
110 return NULL;
112 mutex_enter(&dm_target_mutex);
114 dmt = dm_target_lookup_name(dm_target_name);
115 if (dmt != NULL)
116 dm_target_busy(dmt);
118 mutex_exit(&dm_target_mutex);
120 return dmt;
123 * Search for name in TAIL and return apropriate pointer.
125 static dm_target_t *
126 dm_target_lookup_name(const char *dm_target_name)
128 dm_target_t *dm_target;
129 int dlen;
130 int slen;
132 slen = strlen(dm_target_name) + 1;
134 TAILQ_FOREACH(dm_target, &dm_target_list, dm_target_next) {
135 dlen = strlen(dm_target->name) + 1;
136 if (dlen != slen)
137 continue;
139 if (strncmp(dm_target_name, dm_target->name, slen) == 0)
140 return dm_target;
143 return NULL;
146 * Insert new target struct into the TAIL.
147 * dm_target
148 * contains name, version, function pointer to specifif target functions.
151 dm_target_insert(dm_target_t * dm_target)
153 dm_target_t *dmt;
155 mutex_enter(&dm_target_mutex);
157 dmt = dm_target_lookup_name(dm_target->name);
158 if (dmt != NULL) {
159 mutex_exit(&dm_target_mutex);
160 return EEXIST;
162 TAILQ_INSERT_TAIL(&dm_target_list, dm_target, dm_target_next);
164 mutex_exit(&dm_target_mutex);
166 return 0;
171 * Remove target from TAIL, target is selected with it's name.
174 dm_target_rem(char *dm_target_name)
176 dm_target_t *dmt;
178 KASSERT(dm_target_name != NULL);
180 mutex_enter(&dm_target_mutex);
182 dmt = dm_target_lookup_name(dm_target_name);
183 if (dmt == NULL) {
184 mutex_exit(&dm_target_mutex);
185 return ENOENT;
187 if (dmt->ref_cnt > 0) {
188 mutex_exit(&dm_target_mutex);
189 return EBUSY;
191 TAILQ_REMOVE(&dm_target_list,
192 dmt, dm_target_next);
194 mutex_exit(&dm_target_mutex);
196 (void) kmem_free(dmt, sizeof(dm_target_t));
198 return 0;
201 * Destroy all targets and remove them from queue.
202 * This routine is called from dm_detach, before module
203 * is unloaded.
206 dm_target_destroy(void)
208 dm_target_t *dm_target;
210 mutex_enter(&dm_target_mutex);
211 while (TAILQ_FIRST(&dm_target_list) != NULL) {
213 dm_target = TAILQ_FIRST(&dm_target_list);
215 TAILQ_REMOVE(&dm_target_list, TAILQ_FIRST(&dm_target_list),
216 dm_target_next);
218 (void) kmem_free(dm_target, sizeof(dm_target_t));
220 mutex_exit(&dm_target_mutex);
222 mutex_destroy(&dm_target_mutex);
224 return 0;
227 * Allocate new target entry.
229 dm_target_t *
230 dm_target_alloc(const char *name)
232 return kmem_zalloc(sizeof(dm_target_t), KM_SLEEP);
235 * Return prop_array of dm_target dictionaries.
237 prop_array_t
238 dm_target_prop_list(void)
240 prop_array_t target_array, ver;
241 prop_dictionary_t target_dict;
242 dm_target_t *dm_target;
244 size_t i;
246 target_array = prop_array_create();
248 mutex_enter(&dm_target_mutex);
250 TAILQ_FOREACH(dm_target, &dm_target_list, dm_target_next) {
252 target_dict = prop_dictionary_create();
253 ver = prop_array_create();
254 prop_dictionary_set_cstring(target_dict, DM_TARGETS_NAME,
255 dm_target->name);
257 for (i = 0; i < 3; i++)
258 prop_array_add_uint32(ver, dm_target->version[i]);
260 prop_dictionary_set(target_dict, DM_TARGETS_VERSION, ver);
261 prop_array_add(target_array, target_dict);
263 prop_object_release(ver);
264 prop_object_release(target_dict);
267 mutex_exit(&dm_target_mutex);
269 return target_array;
271 /* Initialize dm_target subsystem. */
273 dm_target_init(void)
275 dm_target_t *dmt, *dmt3;
276 int r;
278 r = 0;
280 mutex_init(&dm_target_mutex, MUTEX_DEFAULT, IPL_NONE);
282 dmt = dm_target_alloc("linear");
283 dmt3 = dm_target_alloc("striped");
285 dmt->version[0] = 1;
286 dmt->version[1] = 0;
287 dmt->version[2] = 2;
288 strlcpy(dmt->name, "linear", DM_MAX_TYPE_NAME);
289 dmt->init = &dm_target_linear_init;
290 dmt->status = &dm_target_linear_status;
291 dmt->strategy = &dm_target_linear_strategy;
292 dmt->deps = &dm_target_linear_deps;
293 dmt->destroy = &dm_target_linear_destroy;
294 dmt->upcall = &dm_target_linear_upcall;
296 r = dm_target_insert(dmt);
298 dmt3->version[0] = 1;
299 dmt3->version[1] = 0;
300 dmt3->version[2] = 3;
301 strlcpy(dmt3->name, "striped", DM_MAX_TYPE_NAME);
302 dmt3->init = &dm_target_stripe_init;
303 dmt3->status = &dm_target_stripe_status;
304 dmt3->strategy = &dm_target_stripe_strategy;
305 dmt3->deps = &dm_target_stripe_deps;
306 dmt3->destroy = &dm_target_stripe_destroy;
307 dmt3->upcall = &dm_target_stripe_upcall;
309 r = dm_target_insert(dmt3);
311 #ifdef notyet
312 dmt5->version[0] = 1;
313 dmt5->version[1] = 0;
314 dmt5->version[2] = 5;
315 strlcpy(dmt5->name, "snapshot", DM_MAX_TYPE_NAME);
316 dmt5->init = &dm_target_snapshot_init;
317 dmt5->status = &dm_target_snapshot_status;
318 dmt5->strategy = &dm_target_snapshot_strategy;
319 dmt5->deps = &dm_target_snapshot_deps;
320 dmt5->destroy = &dm_target_snapshot_destroy;
321 dmt5->upcall = &dm_target_snapshot_upcall;
323 r = dm_target_insert(dmt5);
325 dmt6->version[0] = 1;
326 dmt6->version[1] = 0;
327 dmt6->version[2] = 5;
328 strlcpy(dmt6->name, "snapshot-origin", DM_MAX_TYPE_NAME);
329 dmt6->init = &dm_target_snapshot_orig_init;
330 dmt6->status = &dm_target_snapshot_orig_status;
331 dmt6->strategy = &dm_target_snapshot_orig_strategy;
332 dmt6->deps = &dm_target_snapshot_orig_deps;
333 dmt6->destroy = &dm_target_snapshot_orig_destroy;
334 dmt6->upcall = &dm_target_snapshot_orig_upcall;
336 r = dm_target_insert(dmt6);
337 #endif
339 return r;