1 /* $NetBSD: dm_ioctl.c,v 1.21 2010/02/25 20:48:58 jakllsch 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.
33 * Locking is used to synchronise between ioctl calls and between dm_table's
37 * Simple reference counting, to count users of device will be used routines
38 * dm_dev_busy/dm_dev_unbusy are used for that.
39 * dm_dev_lookup/dm_dev_rem call dm_dev_busy before return(caller is therefore
40 * holder of reference_counter last).
42 * ioctl routines which change/remove dm_dev parameters must wait on
43 * dm_dev::dev_cv and when last user will call dm_dev_unbusy they will wake
47 * To access table entries dm_table_* routines must be used.
49 * dm_table_get_entry will increment table users reference
50 * counter. It will return active or inactive table depends
51 * on uint8_t argument.
53 * dm_table_release must be called for every table_entry from
54 * dm_table_get_entry. Between these to calls tables can'tbe switched
57 * dm_table_head_init initialize talbe_entries SLISTS and io_cv.
59 * dm_table_head_destroy destroy cv.
61 * There are two types of users for dm_table_head first type will
62 * only read list and try to do anything with it e.g. dmstrategy,
63 * dm_table_size etc. There is another user for table_head which wants
64 * to change table lists e.g. dm_dev_resume_ioctl, dm_dev_remove_ioctl,
65 * dm_table_clear_ioctl.
67 * NOTE: It is not allowed to call dm_table_destroy, dm_table_switch_tables
68 * with hold table reference counter. Table reference counter is hold
69 * after calling dm_table_get_entry routine. After calling this
70 * function user must call dm_table_release before any writer table
73 * Example: dm_table_get_entry
74 * dm_table_destroy/dm_table_switch_tables
75 * This exaple will lead to deadlock situation because after dm_table_get_entry
76 * table reference counter is != 0 and dm_table_destroy have to wait on cv until
77 * reference counter is 0.
81 #include <sys/types.h>
82 #include <sys/param.h>
84 #include <sys/device.h>
86 #include <sys/disklabel.h>
87 #include <sys/malloc.h>
89 #include <sys/vnode.h>
91 #include "netbsd-dm.h"
95 static uint64_t sc_minor_num;
96 extern struct dev_ops dm_ops;
97 uint64_t dm_dev_counter;
100 /* Generic cf_data for device-mapper driver */
101 static struct cfdata dm_cfdata = {
104 .cf_fstate = FSTATE_STAR,
109 #define DM_REMOVE_FLAG(flag, name) do { \
110 prop_dictionary_get_uint32(dm_dict,DM_IOCTL_FLAGS,&flag); \
112 prop_dictionary_set_uint32(dm_dict,DM_IOCTL_FLAGS,flag); \
113 } while (/*CONSTCOND*/0)
115 #define DM_ADD_FLAG(flag, name) do { \
116 prop_dictionary_get_uint32(dm_dict,DM_IOCTL_FLAGS,&flag); \
118 prop_dictionary_set_uint32(dm_dict,DM_IOCTL_FLAGS,flag); \
119 } while (/*CONSTCOND*/0)
121 static int dm_dbg_print_flags(int);
124 * Print flags sent to the kernel from libevmapper.
127 dm_dbg_print_flags(int flags)
129 aprint_debug("dbg_print --- %d\n", flags);
131 if (flags & DM_READONLY_FLAG)
132 aprint_debug("dbg_flags: DM_READONLY_FLAG set In/Out\n");
134 if (flags & DM_SUSPEND_FLAG)
135 aprint_debug("dbg_flags: DM_SUSPEND_FLAG set In/Out \n");
137 if (flags & DM_PERSISTENT_DEV_FLAG)
138 aprint_debug("db_flags: DM_PERSISTENT_DEV_FLAG set In\n");
140 if (flags & DM_STATUS_TABLE_FLAG)
141 aprint_debug("dbg_flags: DM_STATUS_TABLE_FLAG set In\n");
143 if (flags & DM_ACTIVE_PRESENT_FLAG)
144 aprint_debug("dbg_flags: DM_ACTIVE_PRESENT_FLAG set Out\n");
146 if (flags & DM_INACTIVE_PRESENT_FLAG)
147 aprint_debug("dbg_flags: DM_INACTIVE_PRESENT_FLAG set Out\n");
149 if (flags & DM_BUFFER_FULL_FLAG)
150 aprint_debug("dbg_flags: DM_BUFFER_FULL_FLAG set Out\n");
152 if (flags & DM_SKIP_BDGET_FLAG)
153 aprint_debug("dbg_flags: DM_SKIP_BDGET_FLAG set In\n");
155 if (flags & DM_SKIP_LOCKFS_FLAG)
156 aprint_debug("dbg_flags: DM_SKIP_LOCKFS_FLAG set In\n");
158 if (flags & DM_NOFLUSH_FLAG)
159 aprint_debug("dbg_flags: DM_NOFLUSH_FLAG set In\n");
164 * Get version ioctl call I do it as default therefore this
165 * function is unused now.
168 dm_get_version_ioctl(prop_dictionary_t dm_dict)
174 * Get list of all available targets from global
175 * target list and sent them back to libdevmapper.
178 dm_list_versions_ioctl(prop_dictionary_t dm_dict)
180 prop_array_t target_list;
185 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
187 dm_dbg_print_flags(flags);
188 target_list = dm_target_prop_list();
190 prop_dictionary_set(dm_dict, DM_IOCTL_CMD_DATA, target_list);
191 prop_object_release(target_list);
196 * Create in-kernel entry for device. Device attributes such as name, uuid are
197 * taken from proplib dictionary.
201 dm_dev_create_ioctl(prop_dictionary_t dm_dict)
204 const char *name, *uuid;
212 /* Get needed values from dictionary. */
213 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
214 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
215 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
217 dm_dbg_print_flags(flags);
219 /* Lookup name and uuid if device already exist quit. */
220 if ((dmv = dm_dev_lookup(name, uuid, -1)) != NULL) {
221 DM_ADD_FLAG(flags, DM_EXISTS_FLAG); /* Device already exists */
226 if ((devt = config_attach_pseudo(&dm_cfdata)) == NULL) {
227 aprint_error("Unable to attach pseudo device dm/%s\n", name);
231 if ((dmv = dm_dev_alloc()) == NULL)
235 strncpy(dmv->uuid, uuid, DM_UUID_LEN);
240 strlcpy(dmv->name, name, DM_NAME_LEN);
242 dmv->minor = ++sc_minor_num; /* XXX: was atomic 64 */
243 dmv->flags = 0; /* device flags are set when needed */
248 dm_table_head_init(&dmv->table_head);
250 lockinit(&dmv->dev_mtx, "dmdev", 0, LK_CANRECURSE);
251 lockinit(&dmv->diskp_mtx, "dmdisk", 0, LK_CANRECURSE);
252 cv_init(&dmv->dev_cv, "dm_dev");
254 if (flags & DM_READONLY_FLAG)
255 dmv->flags |= DM_READONLY_FLAG;
257 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
259 aprint_debug("Creating device dm/%s\n", name);
260 dmv->devt = make_dev(&dm_ops, dmv->minor, UID_ROOT, GID_OPERATOR, 0640, "mapper/%s", dmv->name);
261 udev_dict_set_cstr(dmv->devt, "subsystem", "disk");
263 disk_init(dmv->diskp, dmv->name, &dmdkdriver);
264 disk_attach(dmv->diskp);
266 dmv->diskp->dk_info = NULL;
269 if ((r = dm_dev_insert(dmv)) != 0)
272 DM_ADD_FLAG(flags, DM_EXISTS_FLAG);
273 DM_REMOVE_FLAG(flags, DM_INACTIVE_PRESENT_FLAG);
275 /* Increment device counter After creating device */
276 ++dm_dev_counter; /* XXX: was atomic 64 */
281 * Get list of created device-mapper devices fromglobal list and
286 * <key>cmd_data</key>
290 * <string>...</string>
293 * <integer>...</integer>
299 dm_dev_list_ioctl(prop_dictionary_t dm_dict)
301 prop_array_t dev_list;
307 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
309 dm_dbg_print_flags(flags);
311 dev_list = dm_dev_prop_list();
313 prop_dictionary_set(dm_dict, DM_IOCTL_CMD_DATA, dev_list);
314 prop_object_release(dev_list);
319 * Rename selected devices old name is in struct dm_ioctl.
320 * newname is taken from dictionary
322 * <key>cmd_data</key>
324 * <string>...</string>
328 dm_dev_rename_ioctl(prop_dictionary_t dm_dict)
330 prop_array_t cmd_array;
333 const char *name, *uuid, *n_name;
334 uint32_t flags, minor;
340 /* Get needed values from dictionary. */
341 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
342 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
343 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
344 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
346 dm_dbg_print_flags(flags);
348 cmd_array = prop_dictionary_get(dm_dict, DM_IOCTL_CMD_DATA);
350 prop_array_get_cstring_nocopy(cmd_array, 0, &n_name);
352 if (strlen(n_name) + 1 > DM_NAME_LEN)
355 if ((dmv = dm_dev_rem(name, uuid, minor)) == NULL) {
356 DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
359 /* change device name */
361 * XXX How to deal with this change, name only used in
362 * dm_dev_routines, should I add dm_dev_change_name which will run
363 * under the dm_dev_list mutex ?
365 strlcpy(dmv->name, n_name, DM_NAME_LEN);
367 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_OPEN, dmv->table_head.io_cnt);
368 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
369 prop_dictionary_set_cstring(dm_dict, DM_IOCTL_UUID, dmv->uuid);
376 * Remove device from global list I have to remove active
377 * and inactive tables first.
380 dm_dev_remove_ioctl(prop_dictionary_t dm_dict)
383 const char *name, *uuid;
384 uint32_t flags, minor;
390 /* Get needed values from dictionary. */
391 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
392 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
393 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
394 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
396 dm_dbg_print_flags(flags);
399 * This seems as hack to me, probably use routine dm_dev_get_devt to
400 * atomicaly get devt from device.
402 if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL) {
403 DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
410 * This will call dm_detach routine which will actually removes
413 return dm_detach(dmv);
416 * Return actual state of device to libdevmapper.
419 dm_dev_status_ioctl(prop_dictionary_t dm_dict)
422 const char *name, *uuid;
423 uint32_t flags, j, minor;
430 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
431 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
432 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
433 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
435 if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL) {
436 DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
439 dm_dbg_print_flags(dmv->flags);
441 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_OPEN, dmv->table_head.io_cnt);
442 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
443 prop_dictionary_set_cstring(dm_dict, DM_IOCTL_UUID, dmv->uuid);
445 if (dmv->flags & DM_SUSPEND_FLAG)
446 DM_ADD_FLAG(flags, DM_SUSPEND_FLAG);
449 * Add status flags for tables I have to check both active and
452 if ((j = dm_table_get_target_count(&dmv->table_head, DM_TABLE_ACTIVE))) {
453 DM_ADD_FLAG(flags, DM_ACTIVE_PRESENT_FLAG);
455 DM_REMOVE_FLAG(flags, DM_ACTIVE_PRESENT_FLAG);
457 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_TARGET_COUNT, j);
459 if (dm_table_get_target_count(&dmv->table_head, DM_TABLE_INACTIVE))
460 DM_ADD_FLAG(flags, DM_INACTIVE_PRESENT_FLAG);
462 DM_REMOVE_FLAG(flags, DM_INACTIVE_PRESENT_FLAG);
469 * Set only flag to suggest that device is suspended. This call is
470 * not supported in NetBSD.
474 dm_dev_suspend_ioctl(prop_dictionary_t dm_dict)
477 const char *name, *uuid;
478 uint32_t flags, minor;
484 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
485 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
486 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
487 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
489 if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL) {
490 DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
493 atomic_set_int(&dmv->flags, DM_SUSPEND_FLAG);
495 dm_dbg_print_flags(dmv->flags);
497 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_OPEN, dmv->table_head.io_cnt);
498 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_FLAGS, dmv->flags);
499 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
503 /* Add flags to dictionary flag after dmv -> dict copy */
504 DM_ADD_FLAG(flags, DM_EXISTS_FLAG);
509 * Simulate Linux behaviour better and switch tables here and not in
510 * dm_table_load_ioctl.
513 dm_dev_resume_ioctl(prop_dictionary_t dm_dict)
516 const char *name, *uuid;
517 uint32_t flags, minor;
524 * char *xml; xml = prop_dictionary_externalize(dm_dict);
525 * printf("%s\n",xml);
528 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
529 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
530 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
531 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
533 /* Remove device from global device list */
534 if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL) {
535 DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
538 atomic_clear_int(&dmv->flags, (DM_SUSPEND_FLAG | DM_INACTIVE_PRESENT_FLAG));
539 atomic_set_int(&dmv->flags, DM_ACTIVE_PRESENT_FLAG);
541 dm_table_switch_tables(&dmv->table_head);
543 DM_ADD_FLAG(flags, DM_EXISTS_FLAG);
545 dmsetdiskinfo(dmv->diskp, &dmv->table_head);
547 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_OPEN, dmv->table_head.io_cnt);
548 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_FLAGS, flags);
549 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
553 /* Destroy inactive table after resume. */
554 dm_table_destroy(&dmv->table_head, DM_TABLE_INACTIVE);
559 * Table management routines
560 * lvm2tools doens't send name/uuid to kernel with table
561 * for lookup I have to use minor number.
565 * Remove inactive table from device. Routines which work's with inactive tables
566 * doesn't need to synchronise with dmstrategy. They can synchronise themselves with mutex?.
570 dm_table_clear_ioctl(prop_dictionary_t dm_dict)
573 const char *name, *uuid;
574 uint32_t flags, minor;
582 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
583 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
584 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
585 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
587 aprint_debug("Clearing inactive table from device: %s--%s\n",
590 if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL) {
591 DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
594 /* Select unused table */
595 dm_table_destroy(&dmv->table_head, DM_TABLE_INACTIVE);
597 atomic_clear_int(&dmv->flags, DM_INACTIVE_PRESENT_FLAG);
604 * Get list of physical devices for active table.
605 * Get dev_t from pdev vnode and insert it into cmd_array.
607 * XXX. This function is called from lvm2tools to get information
608 * about physical devices, too e.g. during vgcreate.
611 dm_table_deps_ioctl(prop_dictionary_t dm_dict)
615 dm_table_entry_t *table_en;
617 prop_array_t cmd_array;
618 const char *name, *uuid;
619 uint32_t flags, minor;
631 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
632 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
633 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
634 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
636 /* create array for dev_t's */
637 cmd_array = prop_array_create();
639 if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL) {
640 DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
643 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
644 prop_dictionary_set_cstring(dm_dict, DM_IOCTL_NAME, dmv->name);
645 prop_dictionary_set_cstring(dm_dict, DM_IOCTL_UUID, dmv->uuid);
647 aprint_debug("Getting table deps for device: %s\n", dmv->name);
650 * if DM_QUERY_INACTIVE_TABLE_FLAG is passed we need to query
653 if (flags & DM_QUERY_INACTIVE_TABLE_FLAG)
654 table_type = DM_TABLE_INACTIVE;
656 table_type = DM_TABLE_ACTIVE;
658 tbl = dm_table_get_entry(&dmv->table_head, table_type);
660 SLIST_FOREACH(table_en, tbl, next)
661 table_en->target->deps(table_en, cmd_array);
663 dm_table_release(&dmv->table_head, table_type);
666 prop_dictionary_set(dm_dict, DM_IOCTL_CMD_DATA, cmd_array);
667 prop_object_release(cmd_array);
672 * Load new table/tables to device.
673 * Call apropriate target init routine open all physical pdev's and
674 * link them to device. For other targets mirror, strip, snapshot
675 * etc. also add dependency devices to upcalls list.
677 * Load table to inactive slot table are switched in dm_device_resume_ioctl.
678 * This simulates Linux behaviour better there should not be any difference.
682 dm_table_load_ioctl(prop_dictionary_t dm_dict)
685 dm_table_entry_t *table_en, *last_table;
689 prop_object_iterator_t iter;
690 prop_array_t cmd_array;
691 prop_dictionary_t target_dict;
693 const char *name, *uuid, *type;
695 uint32_t flags, ret, minor;
708 * char *xml; xml = prop_dictionary_externalize(dm_dict);
709 * printf("%s\n",xml);
712 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
713 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
714 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
715 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
717 cmd_array = prop_dictionary_get(dm_dict, DM_IOCTL_CMD_DATA);
718 iter = prop_array_iterator(cmd_array);
719 dm_dbg_print_flags(flags);
721 if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL) {
722 DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
725 aprint_debug("Loading table to device: %s--%d\n", name,
726 dmv->table_head.cur_active_table);
729 * I have to check if this table slot is not used by another table list.
730 * if it is used I should free them.
732 if (dmv->flags & DM_INACTIVE_PRESENT_FLAG)
733 dm_table_destroy(&dmv->table_head, DM_TABLE_INACTIVE);
735 dm_dbg_print_flags(dmv->flags);
736 tbl = dm_table_get_entry(&dmv->table_head, DM_TABLE_INACTIVE);
738 aprint_debug("dmv->name = %s\n", dmv->name);
740 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
742 while ((target_dict = prop_object_iterator_next(iter)) != NULL) {
743 prop_dictionary_get_cstring_nocopy(target_dict,
744 DM_TABLE_TYPE, &type);
746 * If we want to deny table with 2 or more different
747 * target we should do it here
749 if (((target = dm_target_lookup(type)) == NULL) &&
750 ((target = dm_target_autoload(type)) == NULL)) {
751 dm_table_release(&dmv->table_head, DM_TABLE_INACTIVE);
755 if ((table_en = kmalloc(sizeof(dm_table_entry_t),
756 M_DM, M_WAITOK)) == NULL) {
757 dm_table_release(&dmv->table_head, DM_TABLE_INACTIVE);
761 prop_dictionary_get_uint64(target_dict, DM_TABLE_START,
763 prop_dictionary_get_uint64(target_dict, DM_TABLE_LENGTH,
766 aprint_debug("dm_ioctl.c... table_en->start = %ju, "
767 "table_en->length = %ju\n",
768 (uintmax_t)table_en->start,
769 (uintmax_t)table_en->length);
771 table_en->target = target;
772 table_en->dm_dev = dmv;
773 table_en->target_config = NULL;
776 * There is a parameter string after dm_target_spec
777 * structure which points to /dev/wd0a 284 part of
778 * table. String str points to this text. This can be
779 * null and therefore it should be checked before we try to
782 prop_dictionary_get_cstring(target_dict,
783 DM_TABLE_PARAMS, (char **) &str);
785 if (SLIST_EMPTY(tbl))
786 /* insert this table to head */
787 SLIST_INSERT_HEAD(tbl, table_en, next);
789 SLIST_INSERT_AFTER(last_table, table_en, next);
792 * Params string is different for every target,
793 * therfore I have to pass it to target init
794 * routine and parse parameters there.
796 aprint_debug("DM: str passed in is: %s", str);
797 if ((ret = target->init(dmv, &table_en->target_config,
800 dm_table_release(&dmv->table_head, DM_TABLE_INACTIVE);
801 dm_table_destroy(&dmv->table_head, DM_TABLE_INACTIVE);
807 last_table = table_en;
810 prop_object_iterator_release(iter);
812 DM_ADD_FLAG(flags, DM_INACTIVE_PRESENT_FLAG);
813 atomic_set_int(&dmv->flags, DM_INACTIVE_PRESENT_FLAG);
815 dm_table_release(&dmv->table_head, DM_TABLE_INACTIVE);
820 * Get description of all tables loaded to device from kernel
821 * and send it to libdevmapper.
823 * Output dictionary for every table:
825 * <key>cmd_data</key>
829 * <string>...</string>
832 * <integer>...</integer>
835 * <integer>...</integer>
838 * <string>...</string>
844 dm_table_status_ioctl(prop_dictionary_t dm_dict)
848 dm_table_entry_t *table_en;
850 prop_array_t cmd_array;
851 prop_dictionary_t target_dict;
853 uint32_t rec_size, minor;
855 const char *name, *uuid;
867 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
868 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
869 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
870 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
872 cmd_array = prop_array_create();
874 if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL) {
875 DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
879 * if DM_QUERY_INACTIVE_TABLE_FLAG is passed we need to query
882 if (flags & DM_QUERY_INACTIVE_TABLE_FLAG)
883 table_type = DM_TABLE_INACTIVE;
885 table_type = DM_TABLE_ACTIVE;
887 if (dm_table_get_target_count(&dmv->table_head, DM_TABLE_ACTIVE))
888 DM_ADD_FLAG(flags, DM_ACTIVE_PRESENT_FLAG);
890 DM_REMOVE_FLAG(flags, DM_ACTIVE_PRESENT_FLAG);
892 if (dm_table_get_target_count(&dmv->table_head, DM_TABLE_INACTIVE))
893 DM_ADD_FLAG(flags, DM_INACTIVE_PRESENT_FLAG);
895 DM_REMOVE_FLAG(flags, DM_INACTIVE_PRESENT_FLAG);
899 if (dmv->flags & DM_SUSPEND_FLAG)
900 DM_ADD_FLAG(flags, DM_SUSPEND_FLAG);
902 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
904 aprint_debug("Status of device tables: %s--%d\n",
905 name, dmv->table_head.cur_active_table);
907 tbl = dm_table_get_entry(&dmv->table_head, table_type);
909 SLIST_FOREACH(table_en, tbl, next) {
910 target_dict = prop_dictionary_create();
911 aprint_debug("%016" PRIu64 ", length %016" PRIu64
912 ", target %s\n", table_en->start, table_en->length,
913 table_en->target->name);
915 prop_dictionary_set_uint64(target_dict, DM_TABLE_START,
917 prop_dictionary_set_uint64(target_dict, DM_TABLE_LENGTH,
920 prop_dictionary_set_cstring(target_dict, DM_TABLE_TYPE,
921 table_en->target->name);
923 /* dm_table_get_cur_actv.table ?? */
924 prop_dictionary_set_int32(target_dict, DM_TABLE_STAT,
925 dmv->table_head.cur_active_table);
927 if (flags |= DM_STATUS_TABLE_FLAG) {
928 params = table_en->target->status
929 (table_en->target_config);
931 if (params != NULL) {
932 prop_dictionary_set_cstring(target_dict,
933 DM_TABLE_PARAMS, params);
938 prop_array_add(cmd_array, target_dict);
939 prop_object_release(target_dict);
942 dm_table_release(&dmv->table_head, table_type);
945 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_FLAGS, flags);
946 prop_dictionary_set(dm_dict, DM_IOCTL_CMD_DATA, cmd_array);
947 prop_object_release(cmd_array);
954 * For every call I have to set kernel driver version.
955 * Because I can have commands supported only in other
956 * newer/later version. This routine is called for every
960 dm_check_version(prop_dictionary_t dm_dict)
966 ver = prop_dictionary_get(dm_dict, DM_IOCTL_VERSION);
968 for (i = 0; i < 3; i++)
969 prop_array_get_uint32(ver, i, &dm_version[i]);
971 if (DM_VERSION_MAJOR != dm_version[0] || DM_VERSION_MINOR < dm_version[1]) {
972 aprint_debug("libdevmapper/kernel version mismatch "
973 "kernel: %d.%d.%d libdevmapper: %d.%d.%d\n",
974 DM_VERSION_MAJOR, DM_VERSION_MINOR, DM_VERSION_PATCHLEVEL,
975 dm_version[0], dm_version[1], dm_version[2]);
979 prop_array_set_uint32(ver, 0, DM_VERSION_MAJOR);
980 prop_array_set_uint32(ver, 1, DM_VERSION_MINOR);
981 prop_array_set_uint32(ver, 2, DM_VERSION_PATCHLEVEL);
983 prop_dictionary_set(dm_dict, DM_IOCTL_VERSION, ver);