1 /* $NetBSD: dm_dev.c,v 1.7 2009/12/29 23:37:48 haad Exp $ */
4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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>
36 #include <sys/disklabel.h>
37 #include <sys/ioctl.h>
38 #include <sys/ioccom.h>
41 #include "netbsd-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. */
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.
69 dm_dev_lookup(const char *dm_dev_name
, const char *dm_dev_uuid
,
75 mutex_enter(&dm_dev_mutex
);
77 /* KASSERT(dm_dev_name != NULL && dm_dev_uuid != NULL && dm_dev_minor
80 if ((dmv
= dm_dev_lookup_minor(dm_dev_minor
)) != NULL
) {
82 mutex_exit(&dm_dev_mutex
);
85 if (dm_dev_name
!= NULL
)
86 if ((dmv
= dm_dev_lookup_name(dm_dev_name
)) != NULL
) {
88 mutex_exit(&dm_dev_mutex
);
91 if (dm_dev_uuid
!= NULL
)
92 if ((dmv
= dm_dev_lookup_uuid(dm_dev_uuid
)) != NULL
) {
94 mutex_exit(&dm_dev_mutex
);
97 mutex_exit(&dm_dev_mutex
);
103 * Lookup device with its minor number.
106 dm_dev_lookup_minor(int dm_dev_minor
)
110 TAILQ_FOREACH(dmv
, &dm_dev_list
, next_devlist
) {
111 if (dm_dev_minor
== dmv
->minor
)
118 * Lookup device with it's device name.
121 dm_dev_lookup_name(const char *dm_dev_name
)
127 slen
= strlen(dm_dev_name
);
132 TAILQ_FOREACH(dmv
, &dm_dev_list
, next_devlist
) {
134 dlen
= strlen(dmv
->name
);
139 if (strncmp(dm_dev_name
, dmv
->name
, slen
) == 0)
146 * Lookup device with it's device uuid. Used mostly by LVM2tools.
149 dm_dev_lookup_uuid(const char *dm_dev_uuid
)
155 len
= strlen(dm_dev_uuid
);
160 TAILQ_FOREACH(dmv
, &dm_dev_list
, next_devlist
) {
162 if (strlen(dmv
->uuid
) != len
)
165 if (strncmp(dm_dev_uuid
, dmv
->uuid
, strlen(dmv
->uuid
)) == 0)
172 * Insert new device to the global list of devices.
175 dm_dev_insert(dm_dev_t
* dev
)
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
);
194 mutex_exit(&dm_dev_mutex
);
199 * Lookup device with its minor number.
202 dm_dev_test_minor(int dm_dev_minor
)
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
);
213 mutex_exit(&dm_dev_mutex
);
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.
226 dm_dev_detach(device_t devt
)
230 mutex_enter(&dm_dev_mutex
);
231 TAILQ_FOREACH(dmv
, &dm_dev_list
, next_devlist
) {
232 if (devt
== dmv
->devt
) {
237 mutex_exit(&dm_dev_mutex
);
242 * Remove device selected with dm_dev from global list of devices.
245 dm_dev_rem(const char *dm_dev_name
, const char *dm_dev_uuid
,
251 mutex_enter(&dm_dev_mutex
);
253 if (dm_dev_minor
> 0)
254 if ((dmv
= dm_dev_lookup_minor(dm_dev_minor
)) != NULL
) {
258 if (dm_dev_name
!= NULL
)
259 if ((dmv
= dm_dev_lookup_name(dm_dev_name
)) != NULL
) {
263 if (dm_dev_uuid
!= NULL
)
264 if ((dmv
= dm_dev_lookup_name(dm_dev_uuid
)) != NULL
) {
268 mutex_exit(&dm_dev_mutex
);
273 * Destroy all devices created in device-mapper. Remove all tables
274 * free all allocated memmory.
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
),
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
);
314 * Allocate new device entry.
321 dmv
= kmem_zalloc(sizeof(dm_dev_t
), KM_SLEEP
);
324 dmv
->diskp
= kmem_zalloc(sizeof(struct disk
), KM_SLEEP
);
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
));
349 dm_dev_busy(dm_dev_t
* dmv
)
351 mutex_enter(&dmv
->dev_mtx
);
353 mutex_exit(&dmv
->dev_mtx
);
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.
370 dm_dev_prop_list(void)
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
);
394 * Initialize global device mutex.
399 TAILQ_INIT(&dm_dev_list
); /* initialize global dev list */
400 mutex_init(&dm_dev_mutex
, MUTEX_DEFAULT
, IPL_NONE
);