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>
85 #include <sys/devicestat.h>
86 #include <sys/devfs.h>
88 #include <sys/disklabel.h>
89 #include <sys/malloc.h>
91 #include <sys/vnode.h>
93 #include "netbsd-dm.h"
96 extern struct dev_ops dm_ops;
97 extern struct devfs_bitmap dm_minor_bitmap;
98 uint64_t dm_dev_counter;
101 /* Generic cf_data for device-mapper driver */
102 static struct cfdata dm_cfdata = {
105 .cf_fstate = FSTATE_STAR,
110 #define DM_REMOVE_FLAG(flag, name) do { \
111 prop_dictionary_get_uint32(dm_dict,DM_IOCTL_FLAGS,&flag); \
113 prop_dictionary_set_uint32(dm_dict,DM_IOCTL_FLAGS,flag); \
114 } while (/*CONSTCOND*/0)
116 #define DM_ADD_FLAG(flag, name) do { \
117 prop_dictionary_get_uint32(dm_dict,DM_IOCTL_FLAGS,&flag); \
119 prop_dictionary_set_uint32(dm_dict,DM_IOCTL_FLAGS,flag); \
120 } while (/*CONSTCOND*/0)
122 static int dm_dbg_print_flags(int);
125 * Print flags sent to the kernel from libevmapper.
128 dm_dbg_print_flags(int flags)
130 aprint_debug("dbg_print --- %d\n", flags);
132 if (flags & DM_READONLY_FLAG)
133 aprint_debug("dbg_flags: DM_READONLY_FLAG set In/Out\n");
135 if (flags & DM_SUSPEND_FLAG)
136 aprint_debug("dbg_flags: DM_SUSPEND_FLAG set In/Out \n");
138 if (flags & DM_PERSISTENT_DEV_FLAG)
139 aprint_debug("db_flags: DM_PERSISTENT_DEV_FLAG set In\n");
141 if (flags & DM_STATUS_TABLE_FLAG)
142 aprint_debug("dbg_flags: DM_STATUS_TABLE_FLAG set In\n");
144 if (flags & DM_ACTIVE_PRESENT_FLAG)
145 aprint_debug("dbg_flags: DM_ACTIVE_PRESENT_FLAG set Out\n");
147 if (flags & DM_INACTIVE_PRESENT_FLAG)
148 aprint_debug("dbg_flags: DM_INACTIVE_PRESENT_FLAG set Out\n");
150 if (flags & DM_BUFFER_FULL_FLAG)
151 aprint_debug("dbg_flags: DM_BUFFER_FULL_FLAG set Out\n");
153 if (flags & DM_SKIP_BDGET_FLAG)
154 aprint_debug("dbg_flags: DM_SKIP_BDGET_FLAG set In\n");
156 if (flags & DM_SKIP_LOCKFS_FLAG)
157 aprint_debug("dbg_flags: DM_SKIP_LOCKFS_FLAG set In\n");
159 if (flags & DM_NOFLUSH_FLAG)
160 aprint_debug("dbg_flags: DM_NOFLUSH_FLAG set In\n");
165 * Get version ioctl call I do it as default therefore this
166 * function is unused now.
169 dm_get_version_ioctl(prop_dictionary_t dm_dict)
175 * Get list of all available targets from global
176 * target list and sent them back to libdevmapper.
179 dm_list_versions_ioctl(prop_dictionary_t dm_dict)
181 prop_array_t target_list;
186 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
188 dm_dbg_print_flags(flags);
189 target_list = dm_target_prop_list();
191 prop_dictionary_set(dm_dict, DM_IOCTL_CMD_DATA, target_list);
192 prop_object_release(target_list);
197 * Create in-kernel entry for device. Device attributes such as name, uuid are
198 * taken from proplib dictionary.
202 dm_dev_create_ioctl(prop_dictionary_t dm_dict)
205 const char *name, *uuid;
206 char name_buf[MAXPATHLEN];
207 int r, flags, dm_minor;
214 /* Get needed values from dictionary. */
215 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
216 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
217 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
219 dm_dbg_print_flags(flags);
221 /* Lookup name and uuid if device already exist quit. */
222 if ((dmv = dm_dev_lookup(name, uuid, -1)) != NULL) {
223 DM_ADD_FLAG(flags, DM_EXISTS_FLAG); /* Device already exists */
228 if ((devt = config_attach_pseudo(&dm_cfdata)) == NULL) {
229 aprint_error("Unable to attach pseudo device dm/%s\n", name);
233 if ((dmv = dm_dev_alloc()) == NULL)
237 strncpy(dmv->uuid, uuid, DM_UUID_LEN);
242 strlcpy(dmv->name, name, DM_NAME_LEN);
244 dm_minor = devfs_clone_bitmap_get(&dm_minor_bitmap, 0);
245 dmv->flags = 0; /* device flags are set when needed */
251 dm_table_head_init(&dmv->table_head);
253 lockinit(&dmv->dev_mtx, "dmdev", 0, LK_CANRECURSE);
254 lockinit(&dmv->diskp_mtx, "dmdisk", 0, LK_CANRECURSE);
255 cv_init(&dmv->dev_cv, "dm_dev");
257 if (flags & DM_READONLY_FLAG)
258 dmv->flags |= DM_READONLY_FLAG;
260 aprint_debug("Creating device dm/%s\n", name);
261 ksnprintf(name_buf, sizeof(name_buf), "mapper/%s", dmv->name);
263 devstat_add_entry(&dmv->stats, name, 0, DEV_BSIZE,
264 DEVSTAT_NO_ORDERED_TAGS,
265 DEVSTAT_TYPE_DIRECT | DEVSTAT_TYPE_IF_OTHER,
266 DEVSTAT_PRIORITY_DISK);
268 dmv->devt = disk_create_named(name_buf, dm_minor, dmv->diskp, &dm_ops);
269 reference_dev(dmv->devt);
271 dmv->minor = minor(dmv->devt);
272 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
273 udev_dict_set_cstr(dmv->devt, "subsystem", "disk");
275 if ((r = dm_dev_insert(dmv)) != 0)
278 DM_ADD_FLAG(flags, DM_EXISTS_FLAG);
279 DM_REMOVE_FLAG(flags, DM_INACTIVE_PRESENT_FLAG);
281 /* Increment device counter After creating device */
282 ++dm_dev_counter; /* XXX: was atomic 64 */
287 * Get list of created device-mapper devices fromglobal list and
292 * <key>cmd_data</key>
296 * <string>...</string>
299 * <integer>...</integer>
305 dm_dev_list_ioctl(prop_dictionary_t dm_dict)
307 prop_array_t dev_list;
313 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
315 dm_dbg_print_flags(flags);
317 dev_list = dm_dev_prop_list();
319 prop_dictionary_set(dm_dict, DM_IOCTL_CMD_DATA, dev_list);
320 prop_object_release(dev_list);
325 * Rename selected devices old name is in struct dm_ioctl.
326 * newname is taken from dictionary
328 * <key>cmd_data</key>
330 * <string>...</string>
334 dm_dev_rename_ioctl(prop_dictionary_t dm_dict)
336 prop_array_t cmd_array;
339 const char *name, *uuid, *n_name;
340 uint32_t flags, minor;
346 /* Get needed values from dictionary. */
347 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
348 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
349 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
350 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
352 dm_dbg_print_flags(flags);
354 cmd_array = prop_dictionary_get(dm_dict, DM_IOCTL_CMD_DATA);
356 prop_array_get_cstring_nocopy(cmd_array, 0, &n_name);
358 if (strlen(n_name) + 1 > DM_NAME_LEN)
361 if ((dmv = dm_dev_rem(name, uuid, minor)) == NULL) {
362 DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
365 /* change device name */
367 * XXX How to deal with this change, name only used in
368 * dm_dev_routines, should I add dm_dev_change_name which will run
369 * under the dm_dev_list mutex ?
371 strlcpy(dmv->name, n_name, DM_NAME_LEN);
373 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_OPEN, dmv->table_head.io_cnt);
374 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
375 prop_dictionary_set_cstring(dm_dict, DM_IOCTL_UUID, dmv->uuid);
382 * Remove device from global list I have to remove active
383 * and inactive tables first.
386 dm_dev_remove_ioctl(prop_dictionary_t dm_dict)
389 const char *name, *uuid;
390 uint32_t flags, minor;
396 /* Get needed values from dictionary. */
397 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
398 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
399 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
400 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
402 dm_dbg_print_flags(flags);
405 * This seems as hack to me, probably use routine dm_dev_get_devt to
406 * atomicaly get devt from device.
408 if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL) {
409 DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
419 * This will call dm_detach routine which will actually removes
422 return dm_detach(dmv);
425 * Return actual state of device to libdevmapper.
428 dm_dev_status_ioctl(prop_dictionary_t dm_dict)
431 const char *name, *uuid;
432 uint32_t flags, j, minor;
439 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
440 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
441 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
442 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
444 if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL) {
445 DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
448 dm_dbg_print_flags(dmv->flags);
450 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_OPEN, dmv->table_head.io_cnt);
451 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
452 prop_dictionary_set_cstring(dm_dict, DM_IOCTL_UUID, dmv->uuid);
454 if (dmv->flags & DM_SUSPEND_FLAG)
455 DM_ADD_FLAG(flags, DM_SUSPEND_FLAG);
458 * Add status flags for tables I have to check both active and
461 if ((j = dm_table_get_target_count(&dmv->table_head, DM_TABLE_ACTIVE))) {
462 DM_ADD_FLAG(flags, DM_ACTIVE_PRESENT_FLAG);
464 DM_REMOVE_FLAG(flags, DM_ACTIVE_PRESENT_FLAG);
466 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_TARGET_COUNT, j);
468 if (dm_table_get_target_count(&dmv->table_head, DM_TABLE_INACTIVE))
469 DM_ADD_FLAG(flags, DM_INACTIVE_PRESENT_FLAG);
471 DM_REMOVE_FLAG(flags, DM_INACTIVE_PRESENT_FLAG);
478 * Set only flag to suggest that device is suspended. This call is
479 * not supported in NetBSD.
483 dm_dev_suspend_ioctl(prop_dictionary_t dm_dict)
486 const char *name, *uuid;
487 uint32_t flags, minor;
493 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
494 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
495 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
496 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
498 if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL) {
499 DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
502 atomic_set_int(&dmv->flags, DM_SUSPEND_FLAG);
504 dm_dbg_print_flags(dmv->flags);
506 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_OPEN, dmv->table_head.io_cnt);
507 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_FLAGS, dmv->flags);
508 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
512 /* Add flags to dictionary flag after dmv -> dict copy */
513 DM_ADD_FLAG(flags, DM_EXISTS_FLAG);
518 * Simulate Linux behaviour better and switch tables here and not in
519 * dm_table_load_ioctl.
522 dm_dev_resume_ioctl(prop_dictionary_t dm_dict)
525 const char *name, *uuid;
526 uint32_t flags, minor;
533 * char *xml; xml = prop_dictionary_externalize(dm_dict);
534 * printf("%s\n",xml);
537 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
538 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
539 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
540 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
542 /* Remove device from global device list */
543 if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL) {
544 DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
547 atomic_clear_int(&dmv->flags, (DM_SUSPEND_FLAG | DM_INACTIVE_PRESENT_FLAG));
548 atomic_set_int(&dmv->flags, DM_ACTIVE_PRESENT_FLAG);
550 dm_table_switch_tables(&dmv->table_head);
552 DM_ADD_FLAG(flags, DM_EXISTS_FLAG);
554 dmsetdiskinfo(dmv->diskp, &dmv->table_head);
556 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_OPEN, dmv->table_head.io_cnt);
557 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_FLAGS, flags);
558 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
562 /* Destroy inactive table after resume. */
563 dm_table_destroy(&dmv->table_head, DM_TABLE_INACTIVE);
568 * Table management routines
569 * lvm2tools doens't send name/uuid to kernel with table
570 * for lookup I have to use minor number.
574 * Remove inactive table from device. Routines which work's with inactive tables
575 * doesn't need to synchronise with dmstrategy. They can synchronise themselves with mutex?.
579 dm_table_clear_ioctl(prop_dictionary_t dm_dict)
582 const char *name, *uuid;
583 uint32_t flags, minor;
591 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
592 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
593 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
594 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
596 aprint_debug("Clearing inactive table from device: %s--%s\n",
599 if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL) {
600 DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
603 /* Select unused table */
604 dm_table_destroy(&dmv->table_head, DM_TABLE_INACTIVE);
606 atomic_clear_int(&dmv->flags, DM_INACTIVE_PRESENT_FLAG);
613 * Get list of physical devices for active table.
614 * Get dev_t from pdev vnode and insert it into cmd_array.
616 * XXX. This function is called from lvm2tools to get information
617 * about physical devices, too e.g. during vgcreate.
620 dm_table_deps_ioctl(prop_dictionary_t dm_dict)
624 dm_table_entry_t *table_en;
626 prop_array_t cmd_array;
627 const char *name, *uuid;
628 uint32_t flags, minor;
640 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
641 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
642 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
643 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
645 /* create array for dev_t's */
646 cmd_array = prop_array_create();
648 if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL) {
649 DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
652 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
653 prop_dictionary_set_cstring(dm_dict, DM_IOCTL_NAME, dmv->name);
654 prop_dictionary_set_cstring(dm_dict, DM_IOCTL_UUID, dmv->uuid);
656 aprint_debug("Getting table deps for device: %s\n", dmv->name);
659 * if DM_QUERY_INACTIVE_TABLE_FLAG is passed we need to query
662 if (flags & DM_QUERY_INACTIVE_TABLE_FLAG)
663 table_type = DM_TABLE_INACTIVE;
665 table_type = DM_TABLE_ACTIVE;
667 tbl = dm_table_get_entry(&dmv->table_head, table_type);
669 SLIST_FOREACH(table_en, tbl, next)
670 table_en->target->deps(table_en, cmd_array);
672 dm_table_release(&dmv->table_head, table_type);
675 prop_dictionary_set(dm_dict, DM_IOCTL_CMD_DATA, cmd_array);
676 prop_object_release(cmd_array);
681 * Load new table/tables to device.
682 * Call apropriate target init routine open all physical pdev's and
683 * link them to device. For other targets mirror, strip, snapshot
684 * etc. also add dependency devices to upcalls list.
686 * Load table to inactive slot table are switched in dm_device_resume_ioctl.
687 * This simulates Linux behaviour better there should not be any difference.
691 dm_table_load_ioctl(prop_dictionary_t dm_dict)
694 dm_table_entry_t *table_en, *last_table;
698 prop_object_iterator_t iter;
699 prop_array_t cmd_array;
700 prop_dictionary_t target_dict;
702 const char *name, *uuid, *type;
704 uint32_t flags, ret, minor;
717 * char *xml; xml = prop_dictionary_externalize(dm_dict);
718 * printf("%s\n",xml);
721 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
722 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
723 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
724 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
726 cmd_array = prop_dictionary_get(dm_dict, DM_IOCTL_CMD_DATA);
727 iter = prop_array_iterator(cmd_array);
728 dm_dbg_print_flags(flags);
730 if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL) {
731 DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
734 aprint_debug("Loading table to device: %s--%d\n", name,
735 dmv->table_head.cur_active_table);
738 * I have to check if this table slot is not used by another table list.
739 * if it is used I should free them.
741 if (dmv->flags & DM_INACTIVE_PRESENT_FLAG)
742 dm_table_destroy(&dmv->table_head, DM_TABLE_INACTIVE);
744 dm_dbg_print_flags(dmv->flags);
745 tbl = dm_table_get_entry(&dmv->table_head, DM_TABLE_INACTIVE);
747 aprint_debug("dmv->name = %s\n", dmv->name);
749 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
751 while ((target_dict = prop_object_iterator_next(iter)) != NULL) {
752 prop_dictionary_get_cstring_nocopy(target_dict,
753 DM_TABLE_TYPE, &type);
755 * If we want to deny table with 2 or more different
756 * target we should do it here
758 if (((target = dm_target_lookup(type)) == NULL) &&
759 ((target = dm_target_autoload(type)) == NULL)) {
760 dm_table_release(&dmv->table_head, DM_TABLE_INACTIVE);
764 if ((table_en = kmalloc(sizeof(dm_table_entry_t),
765 M_DM, M_WAITOK)) == NULL) {
766 dm_table_release(&dmv->table_head, DM_TABLE_INACTIVE);
770 prop_dictionary_get_uint64(target_dict, DM_TABLE_START,
772 prop_dictionary_get_uint64(target_dict, DM_TABLE_LENGTH,
775 aprint_debug("dm_ioctl.c... table_en->start = %ju, "
776 "table_en->length = %ju\n",
777 (uintmax_t)table_en->start,
778 (uintmax_t)table_en->length);
780 table_en->target = target;
781 table_en->dm_dev = dmv;
782 table_en->target_config = NULL;
785 * There is a parameter string after dm_target_spec
786 * structure which points to /dev/wd0a 284 part of
787 * table. String str points to this text. This can be
788 * null and therefore it should be checked before we try to
791 prop_dictionary_get_cstring(target_dict,
792 DM_TABLE_PARAMS, (char **) &str);
794 if (SLIST_EMPTY(tbl))
795 /* insert this table to head */
796 SLIST_INSERT_HEAD(tbl, table_en, next);
798 SLIST_INSERT_AFTER(last_table, table_en, next);
801 * Params string is different for every target,
802 * therfore I have to pass it to target init
803 * routine and parse parameters there.
805 aprint_debug("DM: str passed in is: %s", str);
806 if ((ret = target->init(dmv, &table_en->target_config,
809 dm_table_release(&dmv->table_head, DM_TABLE_INACTIVE);
810 dm_table_destroy(&dmv->table_head, DM_TABLE_INACTIVE);
816 last_table = table_en;
819 prop_object_iterator_release(iter);
821 DM_ADD_FLAG(flags, DM_INACTIVE_PRESENT_FLAG);
822 atomic_set_int(&dmv->flags, DM_INACTIVE_PRESENT_FLAG);
824 dm_table_release(&dmv->table_head, DM_TABLE_INACTIVE);
828 dmsetdiskinfo(dmv->diskp, &dmv->table_head);
833 * Get description of all tables loaded to device from kernel
834 * and send it to libdevmapper.
836 * Output dictionary for every table:
838 * <key>cmd_data</key>
842 * <string>...</string>
845 * <integer>...</integer>
848 * <integer>...</integer>
851 * <string>...</string>
857 dm_table_status_ioctl(prop_dictionary_t dm_dict)
861 dm_table_entry_t *table_en;
863 prop_array_t cmd_array;
864 prop_dictionary_t target_dict;
866 uint32_t rec_size, minor;
868 const char *name, *uuid;
880 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
881 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
882 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
883 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
885 cmd_array = prop_array_create();
887 if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL) {
888 DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
892 * if DM_QUERY_INACTIVE_TABLE_FLAG is passed we need to query
895 if (flags & DM_QUERY_INACTIVE_TABLE_FLAG)
896 table_type = DM_TABLE_INACTIVE;
898 table_type = DM_TABLE_ACTIVE;
900 if (dm_table_get_target_count(&dmv->table_head, DM_TABLE_ACTIVE))
901 DM_ADD_FLAG(flags, DM_ACTIVE_PRESENT_FLAG);
903 DM_REMOVE_FLAG(flags, DM_ACTIVE_PRESENT_FLAG);
905 if (dm_table_get_target_count(&dmv->table_head, DM_TABLE_INACTIVE))
906 DM_ADD_FLAG(flags, DM_INACTIVE_PRESENT_FLAG);
908 DM_REMOVE_FLAG(flags, DM_INACTIVE_PRESENT_FLAG);
912 if (dmv->flags & DM_SUSPEND_FLAG)
913 DM_ADD_FLAG(flags, DM_SUSPEND_FLAG);
915 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
917 aprint_debug("Status of device tables: %s--%d\n",
918 name, dmv->table_head.cur_active_table);
920 tbl = dm_table_get_entry(&dmv->table_head, table_type);
922 SLIST_FOREACH(table_en, tbl, next) {
923 target_dict = prop_dictionary_create();
924 aprint_debug("%016" PRIu64 ", length %016" PRIu64
925 ", target %s\n", table_en->start, table_en->length,
926 table_en->target->name);
928 prop_dictionary_set_uint64(target_dict, DM_TABLE_START,
930 prop_dictionary_set_uint64(target_dict, DM_TABLE_LENGTH,
933 prop_dictionary_set_cstring(target_dict, DM_TABLE_TYPE,
934 table_en->target->name);
936 /* dm_table_get_cur_actv.table ?? */
937 prop_dictionary_set_int32(target_dict, DM_TABLE_STAT,
938 dmv->table_head.cur_active_table);
940 if (flags |= DM_STATUS_TABLE_FLAG) {
941 params = table_en->target->status
942 (table_en->target_config);
944 if (params != NULL) {
945 prop_dictionary_set_cstring(target_dict,
946 DM_TABLE_PARAMS, params);
951 prop_array_add(cmd_array, target_dict);
952 prop_object_release(target_dict);
955 dm_table_release(&dmv->table_head, table_type);
958 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_FLAGS, flags);
959 prop_dictionary_set(dm_dict, DM_IOCTL_CMD_DATA, cmd_array);
960 prop_object_release(cmd_array);
967 * For every call I have to set kernel driver version.
968 * Because I can have commands supported only in other
969 * newer/later version. This routine is called for every
973 dm_check_version(prop_dictionary_t dm_dict)
979 ver = prop_dictionary_get(dm_dict, DM_IOCTL_VERSION);
981 for (i = 0; i < 3; i++)
982 prop_array_get_uint32(ver, i, &dm_version[i]);
984 if (DM_VERSION_MAJOR != dm_version[0] || DM_VERSION_MINOR < dm_version[1]) {
985 aprint_debug("libdevmapper/kernel version mismatch "
986 "kernel: %d.%d.%d libdevmapper: %d.%d.%d\n",
987 DM_VERSION_MAJOR, DM_VERSION_MINOR, DM_VERSION_PATCHLEVEL,
988 dm_version[0], dm_version[1], dm_version[2]);
992 prop_array_set_uint32(ver, 0, DM_VERSION_MAJOR);
993 prop_array_set_uint32(ver, 1, DM_VERSION_MINOR);
994 prop_array_set_uint32(ver, 2, DM_VERSION_PATCHLEVEL);
996 prop_dictionary_set(dm_dict, DM_IOCTL_VERSION, ver);