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"
94 static uint64_t sc_minor_num;
95 extern struct dev_ops dm_ops;
96 uint64_t dm_dev_counter;
99 /* Generic cf_data for device-mapper driver */
100 static struct cfdata dm_cfdata = {
103 .cf_fstate = FSTATE_STAR,
108 #define DM_REMOVE_FLAG(flag, name) do { \
109 prop_dictionary_get_uint32(dm_dict,DM_IOCTL_FLAGS,&flag); \
111 prop_dictionary_set_uint32(dm_dict,DM_IOCTL_FLAGS,flag); \
112 } while (/*CONSTCOND*/0)
114 #define DM_ADD_FLAG(flag, name) do { \
115 prop_dictionary_get_uint32(dm_dict,DM_IOCTL_FLAGS,&flag); \
117 prop_dictionary_set_uint32(dm_dict,DM_IOCTL_FLAGS,flag); \
118 } while (/*CONSTCOND*/0)
120 static int dm_dbg_print_flags(int);
123 * Print flags sent to the kernel from libevmapper.
126 dm_dbg_print_flags(int flags)
128 aprint_debug("dbg_print --- %d\n", flags);
130 if (flags & DM_READONLY_FLAG)
131 aprint_debug("dbg_flags: DM_READONLY_FLAG set In/Out\n");
133 if (flags & DM_SUSPEND_FLAG)
134 aprint_debug("dbg_flags: DM_SUSPEND_FLAG set In/Out \n");
136 if (flags & DM_PERSISTENT_DEV_FLAG)
137 aprint_debug("db_flags: DM_PERSISTENT_DEV_FLAG set In\n");
139 if (flags & DM_STATUS_TABLE_FLAG)
140 aprint_debug("dbg_flags: DM_STATUS_TABLE_FLAG set In\n");
142 if (flags & DM_ACTIVE_PRESENT_FLAG)
143 aprint_debug("dbg_flags: DM_ACTIVE_PRESENT_FLAG set Out\n");
145 if (flags & DM_INACTIVE_PRESENT_FLAG)
146 aprint_debug("dbg_flags: DM_INACTIVE_PRESENT_FLAG set Out\n");
148 if (flags & DM_BUFFER_FULL_FLAG)
149 aprint_debug("dbg_flags: DM_BUFFER_FULL_FLAG set Out\n");
151 if (flags & DM_SKIP_BDGET_FLAG)
152 aprint_debug("dbg_flags: DM_SKIP_BDGET_FLAG set In\n");
154 if (flags & DM_SKIP_LOCKFS_FLAG)
155 aprint_debug("dbg_flags: DM_SKIP_LOCKFS_FLAG set In\n");
157 if (flags & DM_NOFLUSH_FLAG)
158 aprint_debug("dbg_flags: DM_NOFLUSH_FLAG set In\n");
163 * Get version ioctl call I do it as default therefore this
164 * function is unused now.
167 dm_get_version_ioctl(prop_dictionary_t dm_dict)
173 * Get list of all available targets from global
174 * target list and sent them back to libdevmapper.
177 dm_list_versions_ioctl(prop_dictionary_t dm_dict)
179 prop_array_t target_list;
184 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
186 dm_dbg_print_flags(flags);
187 target_list = dm_target_prop_list();
189 prop_dictionary_set(dm_dict, DM_IOCTL_CMD_DATA, target_list);
190 prop_object_release(target_list);
195 * Create in-kernel entry for device. Device attributes such as name, uuid are
196 * taken from proplib dictionary.
200 dm_dev_create_ioctl(prop_dictionary_t dm_dict)
203 const char *name, *uuid;
211 /* Get needed values from dictionary. */
212 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
213 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
214 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
216 dm_dbg_print_flags(flags);
218 /* Lookup name and uuid if device already exist quit. */
219 if ((dmv = dm_dev_lookup(name, uuid, -1)) != NULL) {
220 DM_ADD_FLAG(flags, DM_EXISTS_FLAG); /* Device already exists */
225 if ((devt = config_attach_pseudo(&dm_cfdata)) == NULL) {
226 aprint_error("Unable to attach pseudo device dm/%s\n", name);
230 if ((dmv = dm_dev_alloc()) == NULL)
234 strncpy(dmv->uuid, uuid, DM_UUID_LEN);
239 strlcpy(dmv->name, name, DM_NAME_LEN);
241 dmv->minor = ++sc_minor_num; /* XXX: was atomic 64 */
242 dmv->flags = 0; /* device flags are set when needed */
247 dm_table_head_init(&dmv->table_head);
249 lockinit(&dmv->dev_mtx, "dmdev", 0, LK_CANRECURSE);
250 lockinit(&dmv->diskp_mtx, "dmdisk", 0, LK_CANRECURSE);
251 cv_init(&dmv->dev_cv, "dm_dev");
253 if (flags & DM_READONLY_FLAG)
254 dmv->flags |= DM_READONLY_FLAG;
256 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
258 aprint_debug("Creating device dm/%s\n", name);
259 dmv->devt = make_dev(&dm_ops, dmv->minor, UID_ROOT, GID_OPERATOR, 0640, "mapper/%s", dmv->name);
260 udev_dict_set_cstr(dmv->devt, "subsystem", "disk");
262 disk_init(dmv->diskp, dmv->name, &dmdkdriver);
263 disk_attach(dmv->diskp);
265 dmv->diskp->dk_info = NULL;
268 if ((r = dm_dev_insert(dmv)) != 0)
271 DM_ADD_FLAG(flags, DM_EXISTS_FLAG);
272 DM_REMOVE_FLAG(flags, DM_INACTIVE_PRESENT_FLAG);
274 /* Increment device counter After creating device */
275 ++dm_dev_counter; /* XXX: was atomic 64 */
280 * Get list of created device-mapper devices fromglobal list and
285 * <key>cmd_data</key>
289 * <string>...</string>
292 * <integer>...</integer>
298 dm_dev_list_ioctl(prop_dictionary_t dm_dict)
300 prop_array_t dev_list;
306 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
308 dm_dbg_print_flags(flags);
310 dev_list = dm_dev_prop_list();
312 prop_dictionary_set(dm_dict, DM_IOCTL_CMD_DATA, dev_list);
313 prop_object_release(dev_list);
318 * Rename selected devices old name is in struct dm_ioctl.
319 * newname is taken from dictionary
321 * <key>cmd_data</key>
323 * <string>...</string>
327 dm_dev_rename_ioctl(prop_dictionary_t dm_dict)
329 prop_array_t cmd_array;
332 const char *name, *uuid, *n_name;
333 uint32_t flags, minor;
339 /* Get needed values from dictionary. */
340 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
341 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
342 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
343 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
345 dm_dbg_print_flags(flags);
347 cmd_array = prop_dictionary_get(dm_dict, DM_IOCTL_CMD_DATA);
349 prop_array_get_cstring_nocopy(cmd_array, 0, &n_name);
351 if (strlen(n_name) + 1 > DM_NAME_LEN)
354 if ((dmv = dm_dev_rem(name, uuid, minor)) == NULL) {
355 DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
358 /* change device name */
360 * XXX How to deal with this change, name only used in
361 * dm_dev_routines, should I add dm_dev_change_name which will run
362 * under the dm_dev_list mutex ?
364 strlcpy(dmv->name, n_name, DM_NAME_LEN);
366 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_OPEN, dmv->table_head.io_cnt);
367 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
368 prop_dictionary_set_cstring(dm_dict, DM_IOCTL_UUID, dmv->uuid);
375 * Remove device from global list I have to remove active
376 * and inactive tables first.
379 dm_dev_remove_ioctl(prop_dictionary_t dm_dict)
382 const char *name, *uuid;
383 uint32_t flags, minor;
389 /* Get needed values from dictionary. */
390 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
391 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
392 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
393 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
395 dm_dbg_print_flags(flags);
398 * This seems as hack to me, probably use routine dm_dev_get_devt to
399 * atomicaly get devt from device.
401 if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL) {
402 DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
409 * This will call dm_detach routine which will actually removes
412 return dm_detach(dmv);
415 * Return actual state of device to libdevmapper.
418 dm_dev_status_ioctl(prop_dictionary_t dm_dict)
421 const char *name, *uuid;
422 uint32_t flags, j, minor;
429 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
430 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
431 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
432 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
434 if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL) {
435 DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
438 dm_dbg_print_flags(dmv->flags);
440 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_OPEN, dmv->table_head.io_cnt);
441 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
442 prop_dictionary_set_cstring(dm_dict, DM_IOCTL_UUID, dmv->uuid);
444 if (dmv->flags & DM_SUSPEND_FLAG)
445 DM_ADD_FLAG(flags, DM_SUSPEND_FLAG);
448 * Add status flags for tables I have to check both active and
451 if ((j = dm_table_get_target_count(&dmv->table_head, DM_TABLE_ACTIVE))) {
452 DM_ADD_FLAG(flags, DM_ACTIVE_PRESENT_FLAG);
454 DM_REMOVE_FLAG(flags, DM_ACTIVE_PRESENT_FLAG);
456 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_TARGET_COUNT, j);
458 if (dm_table_get_target_count(&dmv->table_head, DM_TABLE_INACTIVE))
459 DM_ADD_FLAG(flags, DM_INACTIVE_PRESENT_FLAG);
461 DM_REMOVE_FLAG(flags, DM_INACTIVE_PRESENT_FLAG);
468 * Set only flag to suggest that device is suspended. This call is
469 * not supported in NetBSD.
473 dm_dev_suspend_ioctl(prop_dictionary_t dm_dict)
476 const char *name, *uuid;
477 uint32_t flags, minor;
483 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
484 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
485 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
486 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
488 if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL) {
489 DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
492 atomic_set_int(&dmv->flags, DM_SUSPEND_FLAG);
494 dm_dbg_print_flags(dmv->flags);
496 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_OPEN, dmv->table_head.io_cnt);
497 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_FLAGS, dmv->flags);
498 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
502 /* Add flags to dictionary flag after dmv -> dict copy */
503 DM_ADD_FLAG(flags, DM_EXISTS_FLAG);
508 * Simulate Linux behaviour better and switch tables here and not in
509 * dm_table_load_ioctl.
512 dm_dev_resume_ioctl(prop_dictionary_t dm_dict)
515 const char *name, *uuid;
516 uint32_t flags, minor;
523 * char *xml; xml = prop_dictionary_externalize(dm_dict);
524 * printf("%s\n",xml);
527 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
528 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
529 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
530 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
532 /* Remove device from global device list */
533 if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL) {
534 DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
537 atomic_clear_int(&dmv->flags, (DM_SUSPEND_FLAG | DM_INACTIVE_PRESENT_FLAG));
538 atomic_set_int(&dmv->flags, DM_ACTIVE_PRESENT_FLAG);
540 dm_table_switch_tables(&dmv->table_head);
542 DM_ADD_FLAG(flags, DM_EXISTS_FLAG);
544 dmsetdiskinfo(dmv->diskp, &dmv->table_head);
546 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_OPEN, dmv->table_head.io_cnt);
547 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_FLAGS, flags);
548 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
552 /* Destroy inactive table after resume. */
553 dm_table_destroy(&dmv->table_head, DM_TABLE_INACTIVE);
558 * Table management routines
559 * lvm2tools doens't send name/uuid to kernel with table
560 * for lookup I have to use minor number.
564 * Remove inactive table from device. Routines which work's with inactive tables
565 * doesn't need to synchronise with dmstrategy. They can synchronise themselves with mutex?.
569 dm_table_clear_ioctl(prop_dictionary_t dm_dict)
572 const char *name, *uuid;
573 uint32_t flags, minor;
581 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
582 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
583 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
584 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
586 aprint_debug("Clearing inactive table from device: %s--%s\n",
589 if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL) {
590 DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
593 /* Select unused table */
594 dm_table_destroy(&dmv->table_head, DM_TABLE_INACTIVE);
596 atomic_clear_int(&dmv->flags, DM_INACTIVE_PRESENT_FLAG);
603 * Get list of physical devices for active table.
604 * Get dev_t from pdev vnode and insert it into cmd_array.
606 * XXX. This function is called from lvm2tools to get information
607 * about physical devices, too e.g. during vgcreate.
610 dm_table_deps_ioctl(prop_dictionary_t dm_dict)
614 dm_table_entry_t *table_en;
616 prop_array_t cmd_array;
617 const char *name, *uuid;
618 uint32_t flags, minor;
630 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
631 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
632 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
633 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
635 /* create array for dev_t's */
636 cmd_array = prop_array_create();
638 if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL) {
639 DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
642 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
643 prop_dictionary_set_cstring(dm_dict, DM_IOCTL_NAME, dmv->name);
644 prop_dictionary_set_cstring(dm_dict, DM_IOCTL_UUID, dmv->uuid);
646 aprint_debug("Getting table deps for device: %s\n", dmv->name);
649 * if DM_QUERY_INACTIVE_TABLE_FLAG is passed we need to query
652 if (flags & DM_QUERY_INACTIVE_TABLE_FLAG)
653 table_type = DM_TABLE_INACTIVE;
655 table_type = DM_TABLE_ACTIVE;
657 tbl = dm_table_get_entry(&dmv->table_head, table_type);
659 SLIST_FOREACH(table_en, tbl, next)
660 table_en->target->deps(table_en, cmd_array);
662 dm_table_release(&dmv->table_head, table_type);
665 prop_dictionary_set(dm_dict, DM_IOCTL_CMD_DATA, cmd_array);
666 prop_object_release(cmd_array);
671 * Load new table/tables to device.
672 * Call apropriate target init routine open all physical pdev's and
673 * link them to device. For other targets mirror, strip, snapshot
674 * etc. also add dependency devices to upcalls list.
676 * Load table to inactive slot table are switched in dm_device_resume_ioctl.
677 * This simulates Linux behaviour better there should not be any difference.
681 dm_table_load_ioctl(prop_dictionary_t dm_dict)
684 dm_table_entry_t *table_en, *last_table;
688 prop_object_iterator_t iter;
689 prop_array_t cmd_array;
690 prop_dictionary_t target_dict;
692 const char *name, *uuid, *type;
694 uint32_t flags, ret, minor;
707 * char *xml; xml = prop_dictionary_externalize(dm_dict);
708 * printf("%s\n",xml);
711 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
712 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
713 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
714 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
716 cmd_array = prop_dictionary_get(dm_dict, DM_IOCTL_CMD_DATA);
717 iter = prop_array_iterator(cmd_array);
718 dm_dbg_print_flags(flags);
720 if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL) {
721 DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
724 aprint_debug("Loading table to device: %s--%d\n", name,
725 dmv->table_head.cur_active_table);
728 * I have to check if this table slot is not used by another table list.
729 * if it is used I should free them.
731 if (dmv->flags & DM_INACTIVE_PRESENT_FLAG)
732 dm_table_destroy(&dmv->table_head, DM_TABLE_INACTIVE);
734 dm_dbg_print_flags(dmv->flags);
735 tbl = dm_table_get_entry(&dmv->table_head, DM_TABLE_INACTIVE);
737 aprint_debug("dmv->name = %s\n", dmv->name);
739 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
741 while ((target_dict = prop_object_iterator_next(iter)) != NULL) {
742 prop_dictionary_get_cstring_nocopy(target_dict,
743 DM_TABLE_TYPE, &type);
745 * If we want to deny table with 2 or more different
746 * target we should do it here
748 if (((target = dm_target_lookup(type)) == NULL) &&
749 ((target = dm_target_autoload(type)) == NULL)) {
750 dm_table_release(&dmv->table_head, DM_TABLE_INACTIVE);
754 if ((table_en = kmalloc(sizeof(dm_table_entry_t),
755 M_DM, M_WAITOK)) == NULL) {
756 dm_table_release(&dmv->table_head, DM_TABLE_INACTIVE);
760 prop_dictionary_get_uint64(target_dict, DM_TABLE_START,
762 prop_dictionary_get_uint64(target_dict, DM_TABLE_LENGTH,
765 aprint_debug("dm_ioctl.c... table_en->start = %ju, "
766 "table_en->length = %ju\n",
767 (uintmax_t)table_en->start,
768 (uintmax_t)table_en->length);
770 table_en->target = target;
771 table_en->dm_dev = dmv;
772 table_en->target_config = NULL;
775 * There is a parameter string after dm_target_spec
776 * structure which points to /dev/wd0a 284 part of
777 * table. String str points to this text. This can be
778 * null and therefore it should be checked before we try to
781 prop_dictionary_get_cstring(target_dict,
782 DM_TABLE_PARAMS, (char **) &str);
784 if (SLIST_EMPTY(tbl))
785 /* insert this table to head */
786 SLIST_INSERT_HEAD(tbl, table_en, next);
788 SLIST_INSERT_AFTER(last_table, table_en, next);
791 * Params string is different for every target,
792 * therfore I have to pass it to target init
793 * routine and parse parameters there.
795 aprint_debug("DM: str passed in is: %s", str);
796 if ((ret = target->init(dmv, &table_en->target_config,
799 dm_table_release(&dmv->table_head, DM_TABLE_INACTIVE);
800 dm_table_destroy(&dmv->table_head, DM_TABLE_INACTIVE);
806 last_table = table_en;
809 prop_object_iterator_release(iter);
811 DM_ADD_FLAG(flags, DM_INACTIVE_PRESENT_FLAG);
812 atomic_set_int(&dmv->flags, DM_INACTIVE_PRESENT_FLAG);
814 dm_table_release(&dmv->table_head, DM_TABLE_INACTIVE);
819 * Get description of all tables loaded to device from kernel
820 * and send it to libdevmapper.
822 * Output dictionary for every table:
824 * <key>cmd_data</key>
828 * <string>...</string>
831 * <integer>...</integer>
834 * <integer>...</integer>
837 * <string>...</string>
843 dm_table_status_ioctl(prop_dictionary_t dm_dict)
847 dm_table_entry_t *table_en;
849 prop_array_t cmd_array;
850 prop_dictionary_t target_dict;
852 uint32_t rec_size, minor;
854 const char *name, *uuid;
866 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
867 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
868 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
869 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
871 cmd_array = prop_array_create();
873 if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL) {
874 DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
878 * if DM_QUERY_INACTIVE_TABLE_FLAG is passed we need to query
881 if (flags & DM_QUERY_INACTIVE_TABLE_FLAG)
882 table_type = DM_TABLE_INACTIVE;
884 table_type = DM_TABLE_ACTIVE;
886 if (dm_table_get_target_count(&dmv->table_head, DM_TABLE_ACTIVE))
887 DM_ADD_FLAG(flags, DM_ACTIVE_PRESENT_FLAG);
889 DM_REMOVE_FLAG(flags, DM_ACTIVE_PRESENT_FLAG);
891 if (dm_table_get_target_count(&dmv->table_head, DM_TABLE_INACTIVE))
892 DM_ADD_FLAG(flags, DM_INACTIVE_PRESENT_FLAG);
894 DM_REMOVE_FLAG(flags, DM_INACTIVE_PRESENT_FLAG);
898 if (dmv->flags & DM_SUSPEND_FLAG)
899 DM_ADD_FLAG(flags, DM_SUSPEND_FLAG);
901 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
903 aprint_debug("Status of device tables: %s--%d\n",
904 name, dmv->table_head.cur_active_table);
906 tbl = dm_table_get_entry(&dmv->table_head, table_type);
908 SLIST_FOREACH(table_en, tbl, next) {
909 target_dict = prop_dictionary_create();
910 aprint_debug("%016" PRIu64 ", length %016" PRIu64
911 ", target %s\n", table_en->start, table_en->length,
912 table_en->target->name);
914 prop_dictionary_set_uint64(target_dict, DM_TABLE_START,
916 prop_dictionary_set_uint64(target_dict, DM_TABLE_LENGTH,
919 prop_dictionary_set_cstring(target_dict, DM_TABLE_TYPE,
920 table_en->target->name);
922 /* dm_table_get_cur_actv.table ?? */
923 prop_dictionary_set_int32(target_dict, DM_TABLE_STAT,
924 dmv->table_head.cur_active_table);
926 if (flags |= DM_STATUS_TABLE_FLAG) {
927 params = table_en->target->status
928 (table_en->target_config);
930 if (params != NULL) {
931 prop_dictionary_set_cstring(target_dict,
932 DM_TABLE_PARAMS, params);
937 prop_array_add(cmd_array, target_dict);
938 prop_object_release(target_dict);
941 dm_table_release(&dmv->table_head, table_type);
944 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_FLAGS, flags);
945 prop_dictionary_set(dm_dict, DM_IOCTL_CMD_DATA, cmd_array);
946 prop_object_release(cmd_array);
953 * For every call I have to set kernel driver version.
954 * Because I can have commands supported only in other
955 * newer/later version. This routine is called for every
959 dm_check_version(prop_dictionary_t dm_dict)
965 ver = prop_dictionary_get(dm_dict, DM_IOCTL_VERSION);
967 for (i = 0; i < 3; i++)
968 prop_array_get_uint32(ver, i, &dm_version[i]);
970 if (DM_VERSION_MAJOR != dm_version[0] || DM_VERSION_MINOR < dm_version[1]) {
971 aprint_debug("libdevmapper/kernel version mismatch "
972 "kernel: %d.%d.%d libdevmapper: %d.%d.%d\n",
973 DM_VERSION_MAJOR, DM_VERSION_MINOR, DM_VERSION_PATCHLEVEL,
974 dm_version[0], dm_version[1], dm_version[2]);
978 prop_array_set_uint32(ver, 0, DM_VERSION_MAJOR);
979 prop_array_set_uint32(ver, 1, DM_VERSION_MINOR);
980 prop_array_set_uint32(ver, 2, DM_VERSION_PATCHLEVEL);
982 prop_dictionary_set(dm_dict, DM_IOCTL_VERSION, ver);