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;
100 #define DM_REMOVE_FLAG(flag, name) do { \
101 prop_dictionary_get_uint32(dm_dict,DM_IOCTL_FLAGS,&flag); \
103 prop_dictionary_set_uint32(dm_dict,DM_IOCTL_FLAGS,flag); \
104 } while (/*CONSTCOND*/0)
106 #define DM_ADD_FLAG(flag, name) do { \
107 prop_dictionary_get_uint32(dm_dict,DM_IOCTL_FLAGS,&flag); \
109 prop_dictionary_set_uint32(dm_dict,DM_IOCTL_FLAGS,flag); \
110 } while (/*CONSTCOND*/0)
112 static int dm_dbg_print_flags(int);
115 * Print flags sent to the kernel from libevmapper.
118 dm_dbg_print_flags(int flags)
120 aprint_debug("dbg_print --- %d\n", flags);
122 if (flags & DM_READONLY_FLAG)
123 aprint_debug("dbg_flags: DM_READONLY_FLAG set In/Out\n");
125 if (flags & DM_SUSPEND_FLAG)
126 aprint_debug("dbg_flags: DM_SUSPEND_FLAG set In/Out \n");
128 if (flags & DM_PERSISTENT_DEV_FLAG)
129 aprint_debug("db_flags: DM_PERSISTENT_DEV_FLAG set In\n");
131 if (flags & DM_STATUS_TABLE_FLAG)
132 aprint_debug("dbg_flags: DM_STATUS_TABLE_FLAG set In\n");
134 if (flags & DM_ACTIVE_PRESENT_FLAG)
135 aprint_debug("dbg_flags: DM_ACTIVE_PRESENT_FLAG set Out\n");
137 if (flags & DM_INACTIVE_PRESENT_FLAG)
138 aprint_debug("dbg_flags: DM_INACTIVE_PRESENT_FLAG set Out\n");
140 if (flags & DM_BUFFER_FULL_FLAG)
141 aprint_debug("dbg_flags: DM_BUFFER_FULL_FLAG set Out\n");
143 if (flags & DM_SKIP_BDGET_FLAG)
144 aprint_debug("dbg_flags: DM_SKIP_BDGET_FLAG set In\n");
146 if (flags & DM_SKIP_LOCKFS_FLAG)
147 aprint_debug("dbg_flags: DM_SKIP_LOCKFS_FLAG set In\n");
149 if (flags & DM_NOFLUSH_FLAG)
150 aprint_debug("dbg_flags: DM_NOFLUSH_FLAG set In\n");
155 * Get version ioctl call I do it as default therefore this
156 * function is unused now.
159 dm_get_version_ioctl(prop_dictionary_t dm_dict)
164 * Get list of all available targets from global
165 * target list and sent them back to libdevmapper.
168 dm_list_versions_ioctl(prop_dictionary_t dm_dict)
170 prop_array_t target_list;
175 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
177 dm_dbg_print_flags(flags);
178 target_list = dm_target_prop_list();
180 prop_dictionary_set(dm_dict, DM_IOCTL_CMD_DATA, target_list);
181 prop_object_release(target_list);
186 * Create in-kernel entry for device. Device attributes such as name, uuid are
187 * taken from proplib dictionary.
191 dm_dev_create_ioctl(prop_dictionary_t dm_dict)
194 const char *name, *uuid;
195 char name_buf[MAXPATHLEN];
196 int r, flags, dm_minor;
203 /* Get needed values from dictionary. */
204 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
205 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
206 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
208 dm_dbg_print_flags(flags);
210 /* Lookup name and uuid if device already exist quit. */
211 if ((dmv = dm_dev_lookup(name, uuid, -1)) != NULL) {
212 DM_ADD_FLAG(flags, DM_EXISTS_FLAG); /* Device already exists */
217 if ((dmv = dm_dev_alloc()) == NULL)
221 strncpy(dmv->uuid, uuid, DM_UUID_LEN);
226 strlcpy(dmv->name, name, DM_NAME_LEN);
228 dm_minor = devfs_clone_bitmap_get(&dm_minor_bitmap, 0);
229 dmv->flags = 0; /* device flags are set when needed */
235 dm_table_head_init(&dmv->table_head);
237 lockinit(&dmv->dev_mtx, "dmdev", 0, LK_CANRECURSE);
238 lockinit(&dmv->diskp_mtx, "dmdisk", 0, LK_CANRECURSE);
239 cv_init(&dmv->dev_cv, "dm_dev");
241 if (flags & DM_READONLY_FLAG)
242 dmv->flags |= DM_READONLY_FLAG;
244 aprint_debug("Creating device dm/%s\n", name);
245 ksnprintf(name_buf, sizeof(name_buf), "mapper/%s", dmv->name);
247 devstat_add_entry(&dmv->stats, name, 0, DEV_BSIZE,
248 DEVSTAT_NO_ORDERED_TAGS,
249 DEVSTAT_TYPE_DIRECT | DEVSTAT_TYPE_IF_OTHER,
250 DEVSTAT_PRIORITY_DISK);
252 dmv->devt = disk_create_named(name_buf, dm_minor, dmv->diskp, &dm_ops);
253 reference_dev(dmv->devt);
255 dmv->minor = minor(dmv->devt);
256 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
257 udev_dict_set_cstr(dmv->devt, "subsystem", "disk");
259 if ((r = dm_dev_insert(dmv)) != 0)
262 DM_ADD_FLAG(flags, DM_EXISTS_FLAG);
263 DM_REMOVE_FLAG(flags, DM_INACTIVE_PRESENT_FLAG);
265 /* Increment device counter After creating device */
266 ++dm_dev_counter; /* XXX: was atomic 64 */
271 * Get list of created device-mapper devices fromglobal list and
276 * <key>cmd_data</key>
280 * <string>...</string>
283 * <integer>...</integer>
289 dm_dev_list_ioctl(prop_dictionary_t dm_dict)
291 prop_array_t dev_list;
297 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
299 dm_dbg_print_flags(flags);
301 dev_list = dm_dev_prop_list();
303 prop_dictionary_set(dm_dict, DM_IOCTL_CMD_DATA, dev_list);
304 prop_object_release(dev_list);
309 * Rename selected devices old name is in struct dm_ioctl.
310 * newname is taken from dictionary
312 * <key>cmd_data</key>
314 * <string>...</string>
318 dm_dev_rename_ioctl(prop_dictionary_t dm_dict)
320 prop_array_t cmd_array;
323 const char *name, *uuid, *n_name;
324 uint32_t flags, minor;
330 /* Get needed values from dictionary. */
331 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
332 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
333 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
334 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
336 dm_dbg_print_flags(flags);
338 cmd_array = prop_dictionary_get(dm_dict, DM_IOCTL_CMD_DATA);
340 prop_array_get_cstring_nocopy(cmd_array, 0, &n_name);
342 if (strlen(n_name) + 1 > DM_NAME_LEN)
345 if ((dmv = dm_dev_rem(NULL, name, uuid, minor)) == NULL) {
346 DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
349 /* change device name */
351 * XXX How to deal with this change, name only used in
352 * dm_dev_routines, should I add dm_dev_change_name which will run
353 * under the dm_dev_list mutex ?
355 strlcpy(dmv->name, n_name, DM_NAME_LEN);
357 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_OPEN, dmv->table_head.io_cnt);
358 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
359 prop_dictionary_set_cstring(dm_dict, DM_IOCTL_UUID, dmv->uuid);
366 * Remove device from global list I have to remove active
367 * and inactive tables first.
370 dm_dev_remove_ioctl(prop_dictionary_t dm_dict)
373 const char *name, *uuid;
374 uint32_t flags, minor;
380 /* Get needed values from dictionary. */
381 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
382 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
383 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
384 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
386 dm_dbg_print_flags(flags);
389 * This seems as hack to me, probably use routine dm_dev_get_devt to
390 * atomicaly get devt from device.
392 if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL) {
393 DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
403 * This will call dm_detach routine which will actually removes
406 return dm_detach(dmv);
409 * Return actual state of device to libdevmapper.
412 dm_dev_status_ioctl(prop_dictionary_t dm_dict)
415 const char *name, *uuid;
416 uint32_t flags, j, minor;
423 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
424 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
425 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
426 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
428 if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL) {
429 DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
432 dm_dbg_print_flags(dmv->flags);
434 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_OPEN, dmv->table_head.io_cnt);
435 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
436 prop_dictionary_set_cstring(dm_dict, DM_IOCTL_UUID, dmv->uuid);
438 if (dmv->flags & DM_SUSPEND_FLAG)
439 DM_ADD_FLAG(flags, DM_SUSPEND_FLAG);
442 * Add status flags for tables I have to check both active and
445 if ((j = dm_table_get_target_count(&dmv->table_head, DM_TABLE_ACTIVE))) {
446 DM_ADD_FLAG(flags, DM_ACTIVE_PRESENT_FLAG);
448 DM_REMOVE_FLAG(flags, DM_ACTIVE_PRESENT_FLAG);
450 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_TARGET_COUNT, j);
452 if (dm_table_get_target_count(&dmv->table_head, DM_TABLE_INACTIVE))
453 DM_ADD_FLAG(flags, DM_INACTIVE_PRESENT_FLAG);
455 DM_REMOVE_FLAG(flags, DM_INACTIVE_PRESENT_FLAG);
462 * Set only flag to suggest that device is suspended. This call is
463 * not supported in NetBSD.
467 dm_dev_suspend_ioctl(prop_dictionary_t dm_dict)
470 const char *name, *uuid;
471 uint32_t flags, minor;
477 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
478 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
479 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
480 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
482 if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL) {
483 DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
486 atomic_set_int(&dmv->flags, DM_SUSPEND_FLAG);
488 dm_dbg_print_flags(dmv->flags);
490 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_OPEN, dmv->table_head.io_cnt);
491 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_FLAGS, dmv->flags);
492 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
496 /* Add flags to dictionary flag after dmv -> dict copy */
497 DM_ADD_FLAG(flags, DM_EXISTS_FLAG);
502 * Simulate Linux behaviour better and switch tables here and not in
503 * dm_table_load_ioctl.
506 dm_dev_resume_ioctl(prop_dictionary_t dm_dict)
509 const char *name, *uuid;
510 uint32_t flags, minor;
517 * char *xml; xml = prop_dictionary_externalize(dm_dict);
518 * printf("%s\n",xml);
521 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
522 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
523 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
524 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
526 /* Remove device from global device list */
527 if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL) {
528 DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
531 atomic_clear_int(&dmv->flags, (DM_SUSPEND_FLAG | DM_INACTIVE_PRESENT_FLAG));
532 atomic_set_int(&dmv->flags, DM_ACTIVE_PRESENT_FLAG);
534 dm_table_switch_tables(&dmv->table_head);
536 DM_ADD_FLAG(flags, DM_EXISTS_FLAG);
538 dmsetdiskinfo(dmv->diskp, &dmv->table_head);
540 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_OPEN, dmv->table_head.io_cnt);
541 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_FLAGS, flags);
542 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
546 /* Destroy inactive table after resume. */
547 dm_table_destroy(&dmv->table_head, DM_TABLE_INACTIVE);
552 * Table management routines
553 * lvm2tools doens't send name/uuid to kernel with table
554 * for lookup I have to use minor number.
558 * Remove inactive table from device. Routines which work's with inactive tables
559 * doesn't need to synchronise with dmstrategy. They can synchronise themselves with mutex?.
563 dm_table_clear_ioctl(prop_dictionary_t dm_dict)
566 const char *name, *uuid;
567 uint32_t flags, minor;
575 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
576 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
577 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
578 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
580 aprint_debug("Clearing inactive table from device: %s--%s\n",
583 if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL) {
584 DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
587 /* Select unused table */
588 dm_table_destroy(&dmv->table_head, DM_TABLE_INACTIVE);
590 atomic_clear_int(&dmv->flags, DM_INACTIVE_PRESENT_FLAG);
597 * Get list of physical devices for active table.
598 * Get dev_t from pdev vnode and insert it into cmd_array.
600 * XXX. This function is called from lvm2tools to get information
601 * about physical devices, too e.g. during vgcreate.
604 dm_table_deps_ioctl(prop_dictionary_t dm_dict)
608 dm_table_entry_t *table_en;
610 prop_array_t cmd_array;
611 const char *name, *uuid;
612 uint32_t flags, minor;
624 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
625 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
626 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
627 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
629 /* create array for dev_t's */
630 cmd_array = prop_array_create();
632 if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL) {
633 DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
636 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
637 prop_dictionary_set_cstring(dm_dict, DM_IOCTL_NAME, dmv->name);
638 prop_dictionary_set_cstring(dm_dict, DM_IOCTL_UUID, dmv->uuid);
640 aprint_debug("Getting table deps for device: %s\n", dmv->name);
643 * if DM_QUERY_INACTIVE_TABLE_FLAG is passed we need to query
646 if (flags & DM_QUERY_INACTIVE_TABLE_FLAG)
647 table_type = DM_TABLE_INACTIVE;
649 table_type = DM_TABLE_ACTIVE;
651 tbl = dm_table_get_entry(&dmv->table_head, table_type);
653 SLIST_FOREACH(table_en, tbl, next)
654 table_en->target->deps(table_en, cmd_array);
656 dm_table_release(&dmv->table_head, table_type);
659 prop_dictionary_set(dm_dict, DM_IOCTL_CMD_DATA, cmd_array);
660 prop_object_release(cmd_array);
665 * Load new table/tables to device.
666 * Call apropriate target init routine open all physical pdev's and
667 * link them to device. For other targets mirror, strip, snapshot
668 * etc. also add dependency devices to upcalls list.
670 * Load table to inactive slot table are switched in dm_device_resume_ioctl.
671 * This simulates Linux behaviour better there should not be any difference.
675 dm_table_load_ioctl(prop_dictionary_t dm_dict)
678 dm_table_entry_t *table_en, *last_table;
682 prop_object_iterator_t iter;
683 prop_array_t cmd_array;
684 prop_dictionary_t target_dict;
686 const char *name, *uuid, *type;
688 uint32_t flags, ret, minor;
701 * char *xml; xml = prop_dictionary_externalize(dm_dict);
702 * printf("%s\n",xml);
705 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
706 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
707 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
708 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
710 cmd_array = prop_dictionary_get(dm_dict, DM_IOCTL_CMD_DATA);
711 iter = prop_array_iterator(cmd_array);
712 dm_dbg_print_flags(flags);
714 if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL) {
715 DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
718 aprint_debug("Loading table to device: %s--%d\n", name,
719 dmv->table_head.cur_active_table);
722 * I have to check if this table slot is not used by another table list.
723 * if it is used I should free them.
725 if (dmv->flags & DM_INACTIVE_PRESENT_FLAG)
726 dm_table_destroy(&dmv->table_head, DM_TABLE_INACTIVE);
728 dm_dbg_print_flags(dmv->flags);
729 tbl = dm_table_get_entry(&dmv->table_head, DM_TABLE_INACTIVE);
731 aprint_debug("dmv->name = %s\n", dmv->name);
733 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
735 while ((target_dict = prop_object_iterator_next(iter)) != NULL) {
736 prop_dictionary_get_cstring_nocopy(target_dict,
737 DM_TABLE_TYPE, &type);
739 * If we want to deny table with 2 or more different
740 * target we should do it here
742 if ((target = dm_target_lookup(type)) == NULL) {
743 dm_table_release(&dmv->table_head, DM_TABLE_INACTIVE);
747 if ((table_en = kmalloc(sizeof(dm_table_entry_t),
748 M_DM, M_WAITOK)) == NULL) {
749 dm_table_release(&dmv->table_head, DM_TABLE_INACTIVE);
751 dm_target_unbusy(target);
754 prop_dictionary_get_uint64(target_dict, DM_TABLE_START,
756 prop_dictionary_get_uint64(target_dict, DM_TABLE_LENGTH,
759 aprint_debug("dm_ioctl.c... table_en->start = %ju, "
760 "table_en->length = %ju\n",
761 (uintmax_t)table_en->start,
762 (uintmax_t)table_en->length);
764 table_en->target = target;
765 table_en->dm_dev = dmv;
766 table_en->target_config = NULL;
769 * There is a parameter string after dm_target_spec
770 * structure which points to /dev/wd0a 284 part of
771 * table. String str points to this text. This can be
772 * null and therefore it should be checked before we try to
775 prop_dictionary_get_cstring(target_dict,
776 DM_TABLE_PARAMS, (char **) &str);
778 if (SLIST_EMPTY(tbl))
779 /* insert this table to head */
780 SLIST_INSERT_HEAD(tbl, table_en, next);
782 SLIST_INSERT_AFTER(last_table, table_en, next);
785 * Params string is different for every target,
786 * therfore I have to pass it to target init
787 * routine and parse parameters there.
789 aprint_debug("DM: str passed in is: %s", str);
790 if ((ret = target->init(dmv, &table_en->target_config,
793 dm_table_release(&dmv->table_head, DM_TABLE_INACTIVE);
794 dm_table_destroy(&dmv->table_head, DM_TABLE_INACTIVE);
798 dm_target_unbusy(target);
801 last_table = table_en;
804 prop_object_iterator_release(iter);
806 DM_ADD_FLAG(flags, DM_INACTIVE_PRESENT_FLAG);
807 atomic_set_int(&dmv->flags, DM_INACTIVE_PRESENT_FLAG);
809 dm_table_release(&dmv->table_head, DM_TABLE_INACTIVE);
813 dmsetdiskinfo(dmv->diskp, &dmv->table_head);
818 * Get description of all tables loaded to device from kernel
819 * and send it to libdevmapper.
821 * Output dictionary for every table:
823 * <key>cmd_data</key>
827 * <string>...</string>
830 * <integer>...</integer>
833 * <integer>...</integer>
836 * <string>...</string>
842 dm_table_status_ioctl(prop_dictionary_t dm_dict)
846 dm_table_entry_t *table_en;
848 prop_array_t cmd_array;
849 prop_dictionary_t target_dict;
851 uint32_t rec_size, minor;
853 const char *name, *uuid;
865 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
866 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
867 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
868 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
870 cmd_array = prop_array_create();
872 if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL) {
873 DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
877 * if DM_QUERY_INACTIVE_TABLE_FLAG is passed we need to query
880 if (flags & DM_QUERY_INACTIVE_TABLE_FLAG)
881 table_type = DM_TABLE_INACTIVE;
883 table_type = DM_TABLE_ACTIVE;
885 if (dm_table_get_target_count(&dmv->table_head, DM_TABLE_ACTIVE))
886 DM_ADD_FLAG(flags, DM_ACTIVE_PRESENT_FLAG);
888 DM_REMOVE_FLAG(flags, DM_ACTIVE_PRESENT_FLAG);
890 if (dm_table_get_target_count(&dmv->table_head, DM_TABLE_INACTIVE))
891 DM_ADD_FLAG(flags, DM_INACTIVE_PRESENT_FLAG);
893 DM_REMOVE_FLAG(flags, DM_INACTIVE_PRESENT_FLAG);
897 if (dmv->flags & DM_SUSPEND_FLAG)
898 DM_ADD_FLAG(flags, DM_SUSPEND_FLAG);
900 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
902 aprint_debug("Status of device tables: %s--%d\n",
903 name, dmv->table_head.cur_active_table);
905 tbl = dm_table_get_entry(&dmv->table_head, table_type);
907 SLIST_FOREACH(table_en, tbl, next) {
908 target_dict = prop_dictionary_create();
909 aprint_debug("%016" PRIu64 ", length %016" PRIu64
910 ", target %s\n", table_en->start, table_en->length,
911 table_en->target->name);
913 prop_dictionary_set_uint64(target_dict, DM_TABLE_START,
915 prop_dictionary_set_uint64(target_dict, DM_TABLE_LENGTH,
918 prop_dictionary_set_cstring(target_dict, DM_TABLE_TYPE,
919 table_en->target->name);
921 /* dm_table_get_cur_actv.table ?? */
922 prop_dictionary_set_int32(target_dict, DM_TABLE_STAT,
923 dmv->table_head.cur_active_table);
925 if (flags |= DM_STATUS_TABLE_FLAG) {
926 params = table_en->target->status
927 (table_en->target_config);
929 if (params != NULL) {
930 prop_dictionary_set_cstring(target_dict,
931 DM_TABLE_PARAMS, params);
936 prop_array_add(cmd_array, target_dict);
937 prop_object_release(target_dict);
940 dm_table_release(&dmv->table_head, table_type);
943 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_FLAGS, flags);
944 prop_dictionary_set(dm_dict, DM_IOCTL_CMD_DATA, cmd_array);
945 prop_object_release(cmd_array);
952 * For every call I have to set kernel driver version.
953 * Because I can have commands supported only in other
954 * newer/later version. This routine is called for every
958 dm_check_version(prop_dictionary_t dm_dict)
964 ver = prop_dictionary_get(dm_dict, DM_IOCTL_VERSION);
966 for (i = 0; i < 3; i++)
967 prop_array_get_uint32(ver, i, &dm_version[i]);
969 if (DM_VERSION_MAJOR != dm_version[0] || DM_VERSION_MINOR < dm_version[1]) {
970 aprint_debug("libdevmapper/kernel version mismatch "
971 "kernel: %d.%d.%d libdevmapper: %d.%d.%d\n",
972 DM_VERSION_MAJOR, DM_VERSION_MINOR, DM_VERSION_PATCHLEVEL,
973 dm_version[0], dm_version[1], dm_version[2]);
977 prop_array_set_uint32(ver, 0, DM_VERSION_MAJOR);
978 prop_array_set_uint32(ver, 1, DM_VERSION_MINOR);
979 prop_array_set_uint32(ver, 2, DM_VERSION_PATCHLEVEL);
981 prop_dictionary_set(dm_dict, DM_IOCTL_VERSION, ver);