1 /* $NetBSD: dm_dev.c,v 1.8 2010/01/04 00:19:08 haad Exp $ */
4 * Copyright (c) 2010-2011 Alex Hornung <alex@alexhornung.com>
5 * Copyright (c) 2008 The NetBSD Foundation, Inc.
8 * This code is derived from software contributed to The NetBSD Foundation
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/param.h>
34 #include <machine/thread.h>
35 #include <sys/thread2.h>
38 #include <sys/disklabel.h>
39 #include <sys/devicestat.h>
40 #include <sys/device.h>
42 #include <sys/devfs.h>
43 #include <sys/malloc.h>
44 #include <dev/disk/dm/dm.h>
46 #include "netbsd-dm.h"
48 extern struct dev_ops dm_ops;
50 static struct devfs_bitmap dm_minor_bitmap;
51 uint64_t dm_dev_counter;
53 static dm_dev_t *dm_dev_lookup_name(const char *);
54 static dm_dev_t *dm_dev_lookup_uuid(const char *);
55 static dm_dev_t *dm_dev_lookup_minor(int);
57 static struct dm_dev_head dm_dev_list =
58 TAILQ_HEAD_INITIALIZER(dm_dev_list);
60 static struct lock dm_dev_mutex;
62 static char dummy_uuid[DM_UUID_LEN];
64 /* dm_dev_mutex must be held by caller before using disable_dev. */
66 disable_dev(dm_dev_t *dmv)
68 KKASSERT(lockstatus(&dm_dev_mutex, curthread) == LK_EXCLUSIVE);
70 TAILQ_REMOVE(&dm_dev_list, dmv, next_devlist);
72 lockmgr(&dmv->dev_mtx, LK_EXCLUSIVE);
73 while (dmv->ref_cnt != 0)
74 cv_wait(&dmv->dev_cv, &dmv->dev_mtx);
75 lockmgr(&dmv->dev_mtx, LK_RELEASE);
78 * Generic function used to lookup dm_dev_t. Calling with dm_dev_name
79 * and dm_dev_uuid NULL is allowed.
82 dm_dev_lookup(const char *dm_dev_name, const char *dm_dev_uuid,
88 lockmgr(&dm_dev_mutex, LK_EXCLUSIVE);
90 /* KKASSERT(dm_dev_name != NULL && dm_dev_uuid != NULL && dm_dev_minor
93 if ((dmv = dm_dev_lookup_minor(dm_dev_minor)) != NULL) {
95 lockmgr(&dm_dev_mutex, LK_RELEASE);
98 if (dm_dev_name != NULL)
99 if ((dmv = dm_dev_lookup_name(dm_dev_name)) != NULL) {
101 lockmgr(&dm_dev_mutex, LK_RELEASE);
104 if (dm_dev_uuid != NULL)
105 if ((dmv = dm_dev_lookup_uuid(dm_dev_uuid)) != NULL) {
107 lockmgr(&dm_dev_mutex, LK_RELEASE);
110 lockmgr(&dm_dev_mutex, LK_RELEASE);
116 * Lookup device with its minor number.
119 dm_dev_lookup_minor(int dm_dev_minor)
123 TAILQ_FOREACH(dmv, &dm_dev_list, next_devlist) {
124 if (dm_dev_minor == dmv->minor)
131 * Lookup device with it's device name.
134 dm_dev_lookup_name(const char *dm_dev_name)
138 TAILQ_FOREACH(dmv, &dm_dev_list, next_devlist) {
139 if (strcmp(dm_dev_name, dmv->name) == 0)
146 * Lookup device with it's device uuid. Used mostly by LVM2tools.
149 dm_dev_lookup_uuid(const char *dm_dev_uuid)
153 TAILQ_FOREACH(dmv, &dm_dev_list, next_devlist) {
154 if (strcmp(dm_dev_uuid, dmv->uuid) == 0)
161 * Insert new device to the global list of devices.
164 dm_dev_insert(dm_dev_t *dev)
172 KKASSERT(dev != NULL);
173 lockmgr(&dm_dev_mutex, LK_EXCLUSIVE);
176 * Ignore uuid lookup if dev->uuid is zero-filled.
178 if (memcmp(dev->uuid, dummy_uuid, DM_UUID_LEN))
179 dmv = dm_dev_lookup_uuid(dev->uuid);
182 ((dmv = dm_dev_lookup_name(dev->name)) == NULL) &&
183 ((dmv = dm_dev_lookup_minor(dev->minor)) == NULL)) {
184 TAILQ_INSERT_TAIL(&dm_dev_list, dev, next_devlist);
186 KKASSERT(dmv != NULL);
190 lockmgr(&dm_dev_mutex, LK_RELEASE);
195 dm_dev_rem_dev(dm_dev_t *dmv)
200 lockmgr(&dm_dev_mutex, LK_EXCLUSIVE);
202 lockmgr(&dm_dev_mutex, LK_RELEASE);
204 KKASSERT(dmv != NULL);
209 * Remove device selected with dm_dev from global list of devices.
212 dm_dev_rem(const char *dm_dev_name, const char *dm_dev_uuid, int dm_dev_minor)
214 dm_dev_t *dmv = NULL;
216 lockmgr(&dm_dev_mutex, LK_EXCLUSIVE);
218 if (dm_dev_minor > 0) {
219 if ((dmv = dm_dev_lookup_minor(dm_dev_minor)) != NULL)
221 } else if (dm_dev_name != NULL) {
222 if ((dmv = dm_dev_lookup_name(dm_dev_name)) != NULL)
224 } else if (dm_dev_uuid != NULL) {
225 if ((dmv = dm_dev_lookup_name(dm_dev_uuid)) != NULL)
229 lockmgr(&dm_dev_mutex, LK_RELEASE);
235 dm_dev_create(dm_dev_t **dmvp, const char *name, const char *uuid, int flags)
238 char name_buf[MAXPATHLEN];
241 if ((dmv = dm_dev_alloc()) == NULL)
245 strncpy(dmv->uuid, uuid, DM_UUID_LEN);
250 strlcpy(dmv->name, name, DM_NAME_LEN);
252 dm_minor = devfs_clone_bitmap_get(&dm_minor_bitmap, 0);
254 dm_table_head_init(&dmv->table_head);
256 lockinit(&dmv->dev_mtx, "dmdev", 0, LK_CANRECURSE);
257 cv_init(&dmv->dev_cv, "dm_dev");
259 if (flags & DM_READONLY_FLAG)
260 dmv->flags |= DM_READONLY_FLAG;
262 aprint_debug("Creating device dm/%s\n", name);
263 ksnprintf(name_buf, sizeof(name_buf), "mapper/%s", dmv->name);
265 devstat_add_entry(&dmv->stats, name, 0, DEV_BSIZE,
266 DEVSTAT_NO_ORDERED_TAGS,
267 DEVSTAT_TYPE_DIRECT | DEVSTAT_TYPE_IF_OTHER,
268 DEVSTAT_PRIORITY_DISK);
270 dmv->devt = disk_create_named(name_buf, dm_minor, dmv->diskp, &dm_ops);
271 reference_dev(dmv->devt);
273 /* Make sure the device are immediately available */
276 dmv->devt->si_drv1 = dmv;
277 dmv->devt->si_drv2 = dmv->diskp;
279 dmv->minor = minor(dmv->devt);
280 udev_dict_set_cstr(dmv->devt, "subsystem", "disk");
282 if ((r = dm_dev_insert(dmv)) != 0)
285 /* Increment device counter After creating device */
286 ++dm_dev_counter; /* XXX: was atomic 64 */
293 dm_dev_destroy(dm_dev_t *dmv)
297 /* Destroy active table first. */
298 dm_table_destroy(&dmv->table_head, DM_TABLE_ACTIVE);
300 /* Destroy inactive table if exits, too. */
301 dm_table_destroy(&dmv->table_head, DM_TABLE_INACTIVE);
303 dm_table_head_destroy(&dmv->table_head);
305 minor = dkunit(dmv->devt);
306 disk_destroy(dmv->diskp);
307 devstat_remove_entry(&dmv->stats);
309 release_dev(dmv->devt);
310 devfs_clone_bitmap_put(&dm_minor_bitmap, minor);
312 lockuninit(&dmv->dev_mtx);
313 cv_destroy(&dmv->dev_cv);
318 /* Decrement device counter After removing device */
319 --dm_dev_counter; /* XXX: was atomic 64 */
325 * dm_dev_remove is called to completely destroy & remove a dm disk device.
328 dm_dev_remove(dm_dev_t *dmv)
330 /* Remove device from list and wait for refcnt to drop to zero */
333 /* Destroy and free the device */
340 dm_dev_remove_all(int gentle)
342 dm_dev_t *dmv, *dmv2;
347 lockmgr(&dm_dev_mutex, LK_EXCLUSIVE);
350 * Process in reverse order so that it can deal with inter-depentent
353 TAILQ_FOREACH_REVERSE_MUTABLE(dmv, &dm_dev_list, dm_dev_head,
354 next_devlist, dmv2) {
355 if (gentle && dmv->is_open) {
363 lockmgr(&dm_dev_mutex, LK_RELEASE);
369 * Allocate new device entry.
376 dmv = kmalloc(sizeof(dm_dev_t), M_DM, M_WAITOK | M_ZERO);
379 dmv->diskp = kmalloc(sizeof(struct disk), M_DM, M_WAITOK | M_ZERO);
384 * Freed device entry.
387 dm_dev_free(dm_dev_t *dmv)
389 KKASSERT(dmv != NULL);
391 if (dmv->diskp != NULL)
392 kfree(dmv->diskp, M_DM);
400 dm_dev_busy(dm_dev_t *dmv)
402 lockmgr(&dmv->dev_mtx, LK_EXCLUSIVE);
404 lockmgr(&dmv->dev_mtx, LK_RELEASE);
408 dm_dev_unbusy(dm_dev_t *dmv)
410 KKASSERT(dmv->ref_cnt != 0);
412 lockmgr(&dmv->dev_mtx, LK_EXCLUSIVE);
413 if (--dmv->ref_cnt == 0)
414 cv_broadcast(&dmv->dev_cv);
415 lockmgr(&dmv->dev_mtx, LK_RELEASE);
418 * Return prop_array of dm_targer_list dictionaries.
421 dm_dev_prop_list(void)
424 prop_array_t dev_array;
425 prop_dictionary_t dev_dict;
427 dev_array = prop_array_create();
429 lockmgr(&dm_dev_mutex, LK_EXCLUSIVE);
431 TAILQ_FOREACH(dmv, &dm_dev_list, next_devlist) {
432 dev_dict = prop_dictionary_create();
434 prop_dictionary_set_cstring(dev_dict, DM_DEV_NAME, dmv->name);
435 prop_dictionary_set_uint32(dev_dict, DM_DEV_DEV, dmv->minor);
437 prop_array_add(dev_array, dev_dict);
438 prop_object_release(dev_dict);
441 lockmgr(&dm_dev_mutex, LK_RELEASE);
445 * Initialize global device mutex.
450 TAILQ_INIT(&dm_dev_list); /* initialize global dev list */
451 lockinit(&dm_dev_mutex, "dmdevlist", 0, LK_CANRECURSE);
452 devfs_clone_bitmap_init(&dm_minor_bitmap);
454 memset(dummy_uuid, 0, sizeof(dummy_uuid));
459 * Destroy all devices created in device-mapper. Remove all tables
460 * free all allocated memmory.
465 /* Force removal of all devices */
466 dm_dev_remove_all(0);
468 lockuninit(&dm_dev_mutex);