dm - Import verbatim from NetBSD
[dragonfly.git] / sys / dev / disk / dm / dm_target.c
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
35 #include <sys/kmem.h>
36 #include <sys/module.h>
37
38
39 #include "netbsd-dm.h"
40 #include "dm.h"
41
42 static dm_target_t *dm_target_lookup_name(const char *);
43
44 TAILQ_HEAD(dm_target_head, dm_target);
45
46 static struct dm_target_head dm_target_list =
47 TAILQ_HEAD_INITIALIZER(dm_target_list);
48
49 kmutex_t dm_target_mutex;
50
51 /*
52  * Called indirectly from dm_table_load_ioctl to mark target as used.
53  */
54 void
55 dm_target_busy(dm_target_t * target)
56 {
57         atomic_inc_32(&target->ref_cnt);
58 }
59 /*
60  * Release reference counter on target.
61  */
62 void
63 dm_target_unbusy(dm_target_t * target)
64 {
65         KASSERT(target->ref_cnt > 0);
66         atomic_dec_32(&target->ref_cnt);
67 }
68 /*
69  * Try to autoload target module if it was not found in current
70  * target list.
71  */
72 dm_target_t *
73 dm_target_autoload(const char *dm_target_name)
74 {
75         char name[30];
76         u_int gen;
77         dm_target_t *dmt;
78
79         snprintf(name, sizeof(name), "dm_target_%s", dm_target_name);
80         name[29] = '\0';
81
82         do {
83                 gen = module_gen;
84
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);
90
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);
96
97         return dmt;
98 }
99 /*
100  * Lookup for target in global target list.
101  */
102 dm_target_t *
103 dm_target_lookup(const char *dm_target_name)
104 {
105         dm_target_t *dmt;
106
107         dmt = NULL;
108
109         if (dm_target_name == NULL)
110                 return NULL;
111
112         mutex_enter(&dm_target_mutex);
113
114         dmt = dm_target_lookup_name(dm_target_name);
115         if (dmt != NULL)
116                 dm_target_busy(dmt);
117
118         mutex_exit(&dm_target_mutex);
119
120         return dmt;
121 }
122 /*
123  * Search for name in TAIL and return apropriate pointer.
124  */
125 static dm_target_t *
126 dm_target_lookup_name(const char *dm_target_name)
127 {
128         dm_target_t *dm_target;
129         int dlen;
130         int slen;
131
132         slen = strlen(dm_target_name) + 1;
133
134         TAILQ_FOREACH(dm_target, &dm_target_list, dm_target_next) {
135                 dlen = strlen(dm_target->name) + 1;
136                 if (dlen != slen)
137                         continue;
138
139                 if (strncmp(dm_target_name, dm_target->name, slen) == 0)
140                         return dm_target;
141         }
142
143         return NULL;
144 }
145 /*
146  * Insert new target struct into the TAIL.
147  * dm_target
148  *   contains name, version, function pointer to specifif target functions.
149  */
150 int
151 dm_target_insert(dm_target_t * dm_target)
152 {
153         dm_target_t *dmt;
154
155         mutex_enter(&dm_target_mutex);
156
157         dmt = dm_target_lookup_name(dm_target->name);
158         if (dmt != NULL) {
159                 mutex_exit(&dm_target_mutex);
160                 return EEXIST;
161         }
162         TAILQ_INSERT_TAIL(&dm_target_list, dm_target, dm_target_next);
163
164         mutex_exit(&dm_target_mutex);
165
166         return 0;
167 }
168
169
170 /*
171  * Remove target from TAIL, target is selected with it's name.
172  */
173 int
174 dm_target_rem(char *dm_target_name)
175 {
176         dm_target_t *dmt;
177
178         KASSERT(dm_target_name != NULL);
179
180         mutex_enter(&dm_target_mutex);
181
182         dmt = dm_target_lookup_name(dm_target_name);
183         if (dmt == NULL) {
184                 mutex_exit(&dm_target_mutex);
185                 return ENOENT;
186         }
187         if (dmt->ref_cnt > 0) {
188                 mutex_exit(&dm_target_mutex);
189                 return EBUSY;
190         }
191         TAILQ_REMOVE(&dm_target_list,
192             dmt, dm_target_next);
193
194         mutex_exit(&dm_target_mutex);
195
196         (void) kmem_free(dmt, sizeof(dm_target_t));
197
198         return 0;
199 }
200 /*
201  * Destroy all targets and remove them from queue.
202  * This routine is called from dm_detach, before module
203  * is unloaded.
204  */
205 int
206 dm_target_destroy(void)
207 {
208         dm_target_t *dm_target;
209
210         mutex_enter(&dm_target_mutex);
211         while (TAILQ_FIRST(&dm_target_list) != NULL) {
212
213                 dm_target = TAILQ_FIRST(&dm_target_list);
214
215                 TAILQ_REMOVE(&dm_target_list, TAILQ_FIRST(&dm_target_list),
216                     dm_target_next);
217
218                 (void) kmem_free(dm_target, sizeof(dm_target_t));
219         }
220         mutex_exit(&dm_target_mutex);
221
222         mutex_destroy(&dm_target_mutex);
223
224         return 0;
225 }
226 /*
227  * Allocate new target entry.
228  */
229 dm_target_t *
230 dm_target_alloc(const char *name)
231 {
232         return kmem_zalloc(sizeof(dm_target_t), KM_SLEEP);
233 }
234 /*
235  * Return prop_array of dm_target dictionaries.
236  */
237 prop_array_t
238 dm_target_prop_list(void)
239 {
240         prop_array_t target_array, ver;
241         prop_dictionary_t target_dict;
242         dm_target_t *dm_target;
243
244         size_t i;
245
246         target_array = prop_array_create();
247
248         mutex_enter(&dm_target_mutex);
249
250         TAILQ_FOREACH(dm_target, &dm_target_list, dm_target_next) {
251
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);
256
257                 for (i = 0; i < 3; i++)
258                         prop_array_add_uint32(ver, dm_target->version[i]);
259
260                 prop_dictionary_set(target_dict, DM_TARGETS_VERSION, ver);
261                 prop_array_add(target_array, target_dict);
262
263                 prop_object_release(ver);
264                 prop_object_release(target_dict);
265         }
266
267         mutex_exit(&dm_target_mutex);
268
269         return target_array;
270 }
271 /* Initialize dm_target subsystem. */
272 int
273 dm_target_init(void)
274 {
275         dm_target_t *dmt, *dmt3;
276         int r;
277
278         r = 0;
279
280         mutex_init(&dm_target_mutex, MUTEX_DEFAULT, IPL_NONE);
281
282         dmt = dm_target_alloc("linear");
283         dmt3 = dm_target_alloc("striped");
284
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;
295
296         r = dm_target_insert(dmt);
297
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;
308
309         r = dm_target_insert(dmt3);
310
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;
322
323         r = dm_target_insert(dmt5);
324
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;
335
336         r = dm_target_insert(dmt6);
337 #endif
338
339         return r;
340 }