dm - undo my pointless moving of dm.h
[dragonfly.git] / sys / dev / disk / dm / dm_dev.c
CommitLineData
ff56536e
AH
1/* $NetBSD: dm_dev.c,v 1.8 2010/01/04 00:19:08 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/disk.h>
36#include <sys/disklabel.h>
ff56536e 37#include <sys/ioccom.h>
5b279a20 38#include <sys/malloc.h>
a84e173e 39#include <dev/disk/dm/dm.h>
ff56536e
AH
40
41#include "netbsd-dm.h"
ff56536e
AH
42
43static dm_dev_t *dm_dev_lookup_name(const char *);
44static dm_dev_t *dm_dev_lookup_uuid(const char *);
45static dm_dev_t *dm_dev_lookup_minor(int);
5b279a20 46
ff56536e
AH
47static struct dm_dev_head dm_dev_list =
48TAILQ_HEAD_INITIALIZER(dm_dev_list);
49
5b279a20 50struct lock dm_dev_mutex;
ff56536e
AH
51
52/* dm_dev_mutex must be holdby caller before using disable_dev. */
e8f83946 53static void
ff56536e
AH
54disable_dev(dm_dev_t * dmv)
55{
56 TAILQ_REMOVE(&dm_dev_list, dmv, next_devlist);
5b279a20
AH
57 lockmgr(&dmv->dev_mtx, LK_EXCLUSIVE);
58 lockmgr(&dm_dev_mutex, LK_RELEASE);
ff56536e
AH
59 while (dmv->ref_cnt != 0)
60 cv_wait(&dmv->dev_cv, &dmv->dev_mtx);
5b279a20 61 lockmgr(&dmv->dev_mtx, LK_RELEASE);
ff56536e
AH
62}
63/*
64 * Generic function used to lookup dm_dev_t. Calling with dm_dev_name
65 * and dm_dev_uuid NULL is allowed.
66 */
67dm_dev_t *
68dm_dev_lookup(const char *dm_dev_name, const char *dm_dev_uuid,
69 int dm_dev_minor)
70{
71 dm_dev_t *dmv;
72
73 dmv = NULL;
5b279a20 74 lockmgr(&dm_dev_mutex, LK_EXCLUSIVE);
ff56536e 75
5b279a20 76 /* KKASSERT(dm_dev_name != NULL && dm_dev_uuid != NULL && dm_dev_minor
ff56536e
AH
77 * > 0); */
78 if (dm_dev_minor > 0)
79 if ((dmv = dm_dev_lookup_minor(dm_dev_minor)) != NULL) {
80 dm_dev_busy(dmv);
5b279a20 81 lockmgr(&dm_dev_mutex, LK_RELEASE);
ff56536e
AH
82 return dmv;
83 }
84 if (dm_dev_name != NULL)
85 if ((dmv = dm_dev_lookup_name(dm_dev_name)) != NULL) {
86 dm_dev_busy(dmv);
5b279a20 87 lockmgr(&dm_dev_mutex, LK_RELEASE);
ff56536e
AH
88 return dmv;
89 }
90 if (dm_dev_uuid != NULL)
91 if ((dmv = dm_dev_lookup_uuid(dm_dev_uuid)) != NULL) {
92 dm_dev_busy(dmv);
5b279a20 93 lockmgr(&dm_dev_mutex, LK_RELEASE);
ff56536e
AH
94 return dmv;
95 }
5b279a20 96 lockmgr(&dm_dev_mutex, LK_RELEASE);
ff56536e
AH
97 return NULL;
98}
99
100
101/*
102 * Lookup device with its minor number.
103 */
104static dm_dev_t *
105dm_dev_lookup_minor(int dm_dev_minor)
106{
107 dm_dev_t *dmv;
108
109 TAILQ_FOREACH(dmv, &dm_dev_list, next_devlist) {
110 if (dm_dev_minor == dmv->minor)
111 return dmv;
112 }
113
114 return NULL;
115}
116/*
117 * Lookup device with it's device name.
118 */
119static dm_dev_t *
120dm_dev_lookup_name(const char *dm_dev_name)
121{
122 dm_dev_t *dmv;
ff56536e
AH
123
124 TAILQ_FOREACH(dmv, &dm_dev_list, next_devlist) {
0d42d35b 125 if (strcmp(dm_dev_name, dmv->name) == 0)
ff56536e
AH
126 return dmv;
127 }
128
129 return NULL;
130}
131/*
132 * Lookup device with it's device uuid. Used mostly by LVM2tools.
133 */
134static dm_dev_t *
135dm_dev_lookup_uuid(const char *dm_dev_uuid)
136{
137 dm_dev_t *dmv;
ff56536e
AH
138
139 TAILQ_FOREACH(dmv, &dm_dev_list, next_devlist) {
0d42d35b 140 if (strcmp(dm_dev_uuid, dmv->uuid) == 0)
ff56536e
AH
141 return dmv;
142 }
143
144 return NULL;
145}
146/*
147 * Insert new device to the global list of devices.
148 */
149int
150dm_dev_insert(dm_dev_t * dev)
151{
152 dm_dev_t *dmv;
153 int r;
154
155 dmv = NULL;
156 r = 0;
157
5b279a20
AH
158 KKASSERT(dev != NULL);
159 lockmgr(&dm_dev_mutex, LK_EXCLUSIVE);
ff56536e
AH
160 if (((dmv = dm_dev_lookup_uuid(dev->uuid)) == NULL) &&
161 ((dmv = dm_dev_lookup_name(dev->name)) == NULL) &&
162 ((dmv = dm_dev_lookup_minor(dev->minor)) == NULL)) {
163
164 TAILQ_INSERT_TAIL(&dm_dev_list, dev, next_devlist);
165
166 } else
167 r = EEXIST;
168
5b279a20 169 lockmgr(&dm_dev_mutex, LK_RELEASE);
ff56536e
AH
170 return r;
171}
5b279a20 172
ff56536e
AH
173/*
174 * Remove device selected with dm_dev from global list of devices.
175 */
176dm_dev_t *
e8f83946 177dm_dev_rem(dm_dev_t *dmv, const char *dm_dev_name, const char *dm_dev_uuid,
ff56536e
AH
178 int dm_dev_minor)
179{
5b279a20 180 lockmgr(&dm_dev_mutex, LK_EXCLUSIVE);
ff56536e 181
e8f83946
AH
182 if (dmv != NULL) {
183 disable_dev(dmv);
184 return dmv;
185 }
186
ff56536e
AH
187 if (dm_dev_minor > 0)
188 if ((dmv = dm_dev_lookup_minor(dm_dev_minor)) != NULL) {
189 disable_dev(dmv);
190 return dmv;
191 }
192 if (dm_dev_name != NULL)
193 if ((dmv = dm_dev_lookup_name(dm_dev_name)) != NULL) {
194 disable_dev(dmv);
195 return dmv;
196 }
197 if (dm_dev_uuid != NULL)
198 if ((dmv = dm_dev_lookup_name(dm_dev_uuid)) != NULL) {
199 disable_dev(dmv);
200 return dmv;
201 }
5b279a20 202 lockmgr(&dm_dev_mutex, LK_RELEASE);
ff56536e
AH
203
204 return NULL;
205}
206/*
207 * Destroy all devices created in device-mapper. Remove all tables
208 * free all allocated memmory.
209 */
210int
211dm_dev_destroy(void)
212{
213 dm_dev_t *dmv;
5b279a20 214 lockmgr(&dm_dev_mutex, LK_EXCLUSIVE);
ff56536e
AH
215
216 while (TAILQ_FIRST(&dm_dev_list) != NULL) {
217
218 dmv = TAILQ_FIRST(&dm_dev_list);
219
220 TAILQ_REMOVE(&dm_dev_list, TAILQ_FIRST(&dm_dev_list),
221 next_devlist);
222
5b279a20 223 lockmgr(&dmv->dev_mtx, LK_EXCLUSIVE);
ff56536e
AH
224
225 while (dmv->ref_cnt != 0)
226 cv_wait(&dmv->dev_cv, &dmv->dev_mtx);
227
228 /* Destroy active table first. */
229 dm_table_destroy(&dmv->table_head, DM_TABLE_ACTIVE);
230
231 /* Destroy inactive table if exits, too. */
232 dm_table_destroy(&dmv->table_head, DM_TABLE_INACTIVE);
233
234 dm_table_head_destroy(&dmv->table_head);
235
5b279a20
AH
236 lockmgr(&dmv->dev_mtx, LK_RELEASE);
237 lockuninit(&dmv->dev_mtx);
ff56536e
AH
238 cv_destroy(&dmv->dev_cv);
239
5b279a20 240 (void) kfree(dmv, M_DM);
ff56536e 241 }
5b279a20 242 lockmgr(&dm_dev_mutex, LK_RELEASE);
ff56536e 243
5b279a20 244 lockuninit(&dm_dev_mutex);
ff56536e
AH
245 return 0;
246}
247/*
248 * Allocate new device entry.
249 */
250dm_dev_t *
251dm_dev_alloc(void)
252{
253 dm_dev_t *dmv;
254
5b279a20 255 dmv = kmalloc(sizeof(dm_dev_t), M_DM, M_WAITOK | M_ZERO);
ff56536e
AH
256
257 if (dmv != NULL)
5b279a20 258 dmv->diskp = kmalloc(sizeof(struct disk), M_DM, M_WAITOK | M_ZERO);
ff56536e
AH
259
260 return dmv;
261}
262/*
263 * Freed device entry.
264 */
265int
266dm_dev_free(dm_dev_t * dmv)
267{
5b279a20 268 KKASSERT(dmv != NULL);
ff56536e 269
5b279a20
AH
270 lockuninit(&dmv->dev_mtx);
271 lockuninit(&dmv->diskp_mtx);
ff56536e
AH
272 cv_destroy(&dmv->dev_cv);
273
274 if (dmv->diskp != NULL)
5b279a20 275 (void) kfree(dmv->diskp, M_DM);
ff56536e 276
5b279a20 277 (void) kfree(dmv, M_DM);
ff56536e
AH
278
279 return 0;
280}
281
282void
283dm_dev_busy(dm_dev_t * dmv)
284{
5b279a20 285 lockmgr(&dmv->dev_mtx, LK_EXCLUSIVE);
ff56536e 286 dmv->ref_cnt++;
5b279a20 287 lockmgr(&dmv->dev_mtx, LK_RELEASE);
ff56536e
AH
288}
289
290void
291dm_dev_unbusy(dm_dev_t * dmv)
292{
5b279a20 293 KKASSERT(dmv->ref_cnt != 0);
ff56536e 294
5b279a20 295 lockmgr(&dmv->dev_mtx, LK_EXCLUSIVE);
ff56536e
AH
296 if (--dmv->ref_cnt == 0)
297 cv_broadcast(&dmv->dev_cv);
5b279a20 298 lockmgr(&dmv->dev_mtx, LK_RELEASE);
ff56536e
AH
299}
300/*
301 * Return prop_array of dm_targer_list dictionaries.
302 */
303prop_array_t
304dm_dev_prop_list(void)
305{
306 dm_dev_t *dmv;
307 prop_array_t dev_array;
308 prop_dictionary_t dev_dict;
309
310 dev_array = prop_array_create();
311
5b279a20 312 lockmgr(&dm_dev_mutex, LK_EXCLUSIVE);
ff56536e
AH
313
314 TAILQ_FOREACH(dmv, &dm_dev_list, next_devlist) {
315 dev_dict = prop_dictionary_create();
316
317 prop_dictionary_set_cstring(dev_dict, DM_DEV_NAME, dmv->name);
318 prop_dictionary_set_uint32(dev_dict, DM_DEV_DEV, dmv->minor);
319
320 prop_array_add(dev_array, dev_dict);
321 prop_object_release(dev_dict);
322 }
323
5b279a20 324 lockmgr(&dm_dev_mutex, LK_RELEASE);
ff56536e
AH
325 return dev_array;
326}
327/*
328 * Initialize global device mutex.
329 */
330int
331dm_dev_init(void)
332{
333 TAILQ_INIT(&dm_dev_list); /* initialize global dev list */
5b279a20 334 lockinit(&dm_dev_mutex, "dmdevlist", 0, LK_CANRECURSE);
ff56536e
AH
335 return 0;
336}