Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sys / dev / dm / dm_dev.c
blob7ad966828978b54868b8bf307fab85ae776b8004
1 /* $NetBSD: dm_dev.c,v 1.7 2009/12/29 23:37:48 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/disk.h>
36 #include <sys/disklabel.h>
37 #include <sys/ioctl.h>
38 #include <sys/ioccom.h>
39 #include <sys/kmem.h>
41 #include "netbsd-dm.h"
42 #include "dm.h"
44 static dm_dev_t *dm_dev_lookup_name(const char *);
45 static dm_dev_t *dm_dev_lookup_uuid(const char *);
46 static dm_dev_t *dm_dev_lookup_minor(int);
48 static struct dm_dev_head dm_dev_list =
49 TAILQ_HEAD_INITIALIZER(dm_dev_list);
51 kmutex_t dm_dev_mutex;
53 /* dm_dev_mutex must be holdby caller before using disable_dev. */
54 __inline static void
55 disable_dev(dm_dev_t * dmv)
57 TAILQ_REMOVE(&dm_dev_list, dmv, next_devlist);
58 mutex_enter(&dmv->dev_mtx);
59 mutex_exit(&dm_dev_mutex);
60 while (dmv->ref_cnt != 0)
61 cv_wait(&dmv->dev_cv, &dmv->dev_mtx);
62 mutex_exit(&dmv->dev_mtx);
65 * Generic function used to lookup dm_dev_t. Calling with dm_dev_name
66 * and dm_dev_uuid NULL is allowed.
68 dm_dev_t *
69 dm_dev_lookup(const char *dm_dev_name, const char *dm_dev_uuid,
70 int dm_dev_minor)
72 dm_dev_t *dmv;
74 dmv = NULL;
75 mutex_enter(&dm_dev_mutex);
77 /* KASSERT(dm_dev_name != NULL && dm_dev_uuid != NULL && dm_dev_minor
78 * > 0); */
79 if (dm_dev_minor > 0)
80 if ((dmv = dm_dev_lookup_minor(dm_dev_minor)) != NULL) {
81 dm_dev_busy(dmv);
82 mutex_exit(&dm_dev_mutex);
83 return dmv;
85 if (dm_dev_name != NULL)
86 if ((dmv = dm_dev_lookup_name(dm_dev_name)) != NULL) {
87 dm_dev_busy(dmv);
88 mutex_exit(&dm_dev_mutex);
89 return dmv;
91 if (dm_dev_uuid != NULL)
92 if ((dmv = dm_dev_lookup_uuid(dm_dev_uuid)) != NULL) {
93 dm_dev_busy(dmv);
94 mutex_exit(&dm_dev_mutex);
95 return dmv;
97 mutex_exit(&dm_dev_mutex);
98 return NULL;
103 * Lookup device with its minor number.
105 static dm_dev_t *
106 dm_dev_lookup_minor(int dm_dev_minor)
108 dm_dev_t *dmv;
110 TAILQ_FOREACH(dmv, &dm_dev_list, next_devlist) {
111 if (dm_dev_minor == dmv->minor)
112 return dmv;
115 return NULL;
118 * Lookup device with it's device name.
120 static dm_dev_t *
121 dm_dev_lookup_name(const char *dm_dev_name)
123 dm_dev_t *dmv;
124 int dlen;
125 int slen;
127 slen = strlen(dm_dev_name);
129 if (slen == 0)
130 return NULL;
132 TAILQ_FOREACH(dmv, &dm_dev_list, next_devlist) {
134 dlen = strlen(dmv->name);
136 if (slen != dlen)
137 continue;
139 if (strncmp(dm_dev_name, dmv->name, slen) == 0)
140 return dmv;
143 return NULL;
146 * Lookup device with it's device uuid. Used mostly by LVM2tools.
148 static dm_dev_t *
149 dm_dev_lookup_uuid(const char *dm_dev_uuid)
151 dm_dev_t *dmv;
152 size_t len;
154 len = 0;
155 len = strlen(dm_dev_uuid);
157 if (len == 0)
158 return NULL;
160 TAILQ_FOREACH(dmv, &dm_dev_list, next_devlist) {
162 if (strlen(dmv->uuid) != len)
163 continue;
165 if (strncmp(dm_dev_uuid, dmv->uuid, strlen(dmv->uuid)) == 0)
166 return dmv;
169 return NULL;
172 * Insert new device to the global list of devices.
175 dm_dev_insert(dm_dev_t * dev)
177 dm_dev_t *dmv;
178 int r;
180 dmv = NULL;
181 r = 0;
183 KASSERT(dev != NULL);
184 mutex_enter(&dm_dev_mutex);
185 if (((dmv = dm_dev_lookup_uuid(dev->uuid)) == NULL) &&
186 ((dmv = dm_dev_lookup_name(dev->name)) == NULL) &&
187 ((dmv = dm_dev_lookup_minor(dev->minor)) == NULL)) {
189 TAILQ_INSERT_TAIL(&dm_dev_list, dev, next_devlist);
191 } else
192 r = EEXIST;
194 mutex_exit(&dm_dev_mutex);
195 return r;
197 #ifdef notyet
199 * Lookup device with its minor number.
202 dm_dev_test_minor(int dm_dev_minor)
204 dm_dev_t *dmv;
206 mutex_enter(&dm_dev_mutex);
207 TAILQ_FOREACH(dmv, &dm_dev_list, next_devlist) {
208 if (dm_dev_minor == dmv->minor) {
209 mutex_exit(&dm_dev_mutex);
210 return 1;
213 mutex_exit(&dm_dev_mutex);
215 return 0;
217 #endif
220 * dm_dev_lookup_devt look for selected device_t. We keep this routine
221 * outside of dm_dev_lookup because it is a temporally solution.
223 * TODO: This is a hack autoconf should be more flexible.
225 dm_dev_t *
226 dm_dev_detach(device_t devt)
228 dm_dev_t *dmv;
230 mutex_enter(&dm_dev_mutex);
231 TAILQ_FOREACH(dmv, &dm_dev_list, next_devlist) {
232 if (devt == dmv->devt) {
233 disable_dev(dmv);
234 return dmv;
237 mutex_exit(&dm_dev_mutex);
239 return NULL;
242 * Remove device selected with dm_dev from global list of devices.
244 dm_dev_t *
245 dm_dev_rem(const char *dm_dev_name, const char *dm_dev_uuid,
246 int dm_dev_minor)
248 dm_dev_t *dmv;
249 dmv = NULL;
251 mutex_enter(&dm_dev_mutex);
253 if (dm_dev_minor > 0)
254 if ((dmv = dm_dev_lookup_minor(dm_dev_minor)) != NULL) {
255 disable_dev(dmv);
256 return dmv;
258 if (dm_dev_name != NULL)
259 if ((dmv = dm_dev_lookup_name(dm_dev_name)) != NULL) {
260 disable_dev(dmv);
261 return dmv;
263 if (dm_dev_uuid != NULL)
264 if ((dmv = dm_dev_lookup_name(dm_dev_uuid)) != NULL) {
265 disable_dev(dmv);
266 return dmv;
268 mutex_exit(&dm_dev_mutex);
270 return NULL;
273 * Destroy all devices created in device-mapper. Remove all tables
274 * free all allocated memmory.
277 dm_dev_destroy(void)
279 dm_dev_t *dmv;
280 mutex_enter(&dm_dev_mutex);
282 while (TAILQ_FIRST(&dm_dev_list) != NULL) {
284 dmv = TAILQ_FIRST(&dm_dev_list);
286 TAILQ_REMOVE(&dm_dev_list, TAILQ_FIRST(&dm_dev_list),
287 next_devlist);
289 mutex_enter(&dmv->dev_mtx);
291 while (dmv->ref_cnt != 0)
292 cv_wait(&dmv->dev_cv, &dmv->dev_mtx);
294 /* Destroy active table first. */
295 dm_table_destroy(&dmv->table_head, DM_TABLE_ACTIVE);
297 /* Destroy inactive table if exits, too. */
298 dm_table_destroy(&dmv->table_head, DM_TABLE_INACTIVE);
300 dm_table_head_destroy(&dmv->table_head);
302 mutex_exit(&dmv->dev_mtx);
303 mutex_destroy(&dmv->dev_mtx);
304 cv_destroy(&dmv->dev_cv);
306 (void) kmem_free(dmv, sizeof(dm_dev_t));
308 mutex_exit(&dm_dev_mutex);
310 mutex_destroy(&dm_dev_mutex);
311 return 0;
314 * Allocate new device entry.
316 dm_dev_t *
317 dm_dev_alloc(void)
319 dm_dev_t *dmv;
321 dmv = kmem_zalloc(sizeof(dm_dev_t), KM_SLEEP);
323 if (dmv != NULL)
324 dmv->diskp = kmem_zalloc(sizeof(struct disk), KM_SLEEP);
326 return dmv;
329 * Freed device entry.
332 dm_dev_free(dm_dev_t * dmv)
334 KASSERT(dmv != NULL);
336 mutex_destroy(&dmv->dev_mtx);
337 mutex_destroy(&dmv->diskp_mtx);
338 cv_destroy(&dmv->dev_cv);
340 if (dmv->diskp != NULL)
341 (void) kmem_free(dmv->diskp, sizeof(struct disk));
343 (void) kmem_free(dmv, sizeof(dm_dev_t));
345 return 0;
348 void
349 dm_dev_busy(dm_dev_t * dmv)
351 mutex_enter(&dmv->dev_mtx);
352 dmv->ref_cnt++;
353 mutex_exit(&dmv->dev_mtx);
356 void
357 dm_dev_unbusy(dm_dev_t * dmv)
359 KASSERT(dmv->ref_cnt != 0);
361 mutex_enter(&dmv->dev_mtx);
362 if (--dmv->ref_cnt == 0)
363 cv_broadcast(&dmv->dev_cv);
364 mutex_exit(&dmv->dev_mtx);
367 * Return prop_array of dm_targer_list dictionaries.
369 prop_array_t
370 dm_dev_prop_list(void)
372 dm_dev_t *dmv;
373 prop_array_t dev_array;
374 prop_dictionary_t dev_dict;
376 dev_array = prop_array_create();
378 mutex_enter(&dm_dev_mutex);
380 TAILQ_FOREACH(dmv, &dm_dev_list, next_devlist) {
381 dev_dict = prop_dictionary_create();
383 prop_dictionary_set_cstring(dev_dict, DM_DEV_NAME, dmv->name);
384 prop_dictionary_set_uint32(dev_dict, DM_DEV_DEV, dmv->minor);
386 prop_array_add(dev_array, dev_dict);
387 prop_object_release(dev_dict);
390 mutex_exit(&dm_dev_mutex);
391 return dev_array;
394 * Initialize global device mutex.
397 dm_dev_init(void)
399 TAILQ_INIT(&dm_dev_list); /* initialize global dev list */
400 mutex_init(&dm_dev_mutex, MUTEX_DEFAULT, IPL_NONE);
401 return 0;