1 /* $NetBSD: dm_ioctl.c,v 1.21 2010/02/25 20:48:58 jakllsch Exp $ */
4 * Copyright (c) 2010-2011 Alex Hornung <alex@alexhornung.com>
5 * Copyright (c) 2008 The NetBSD Foundation, Inc.
8 * This code is derived from software contributed to The NetBSD Foundation
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
34 * Locking is used to synchronise between ioctl calls and between dm_table's
38 * Simple reference counting, to count users of device will be used routines
39 * dm_dev_busy/dm_dev_unbusy are used for that.
40 * dm_dev_lookup/dm_dev_rem call dm_dev_busy before return(caller is therefore
41 * holder of reference_counter last).
43 * ioctl routines which change/remove dm_dev parameters must wait on
44 * dm_dev::dev_cv and when last user will call dm_dev_unbusy they will wake
48 * To access table entries dm_table_* routines must be used.
50 * dm_table_get_entry will increment table users reference
51 * counter. It will return active or inactive table depends
52 * on uint8_t argument.
54 * dm_table_release must be called for every table_entry from
55 * dm_table_get_entry. Between these to calls tables can'tbe switched
58 * dm_table_head_init initialize talbe_entries TAILQS and io_cv.
60 * dm_table_head_destroy destroy cv.
62 * There are two types of users for dm_table_head first type will
63 * only read list and try to do anything with it e.g. dmstrategy,
64 * dm_table_size etc. There is another user for table_head which wants
65 * to change table lists e.g. dm_dev_resume_ioctl, dm_dev_remove_ioctl,
66 * dm_table_clear_ioctl.
68 * NOTE: It is not allowed to call dm_table_destroy, dm_table_switch_tables
69 * with hold table reference counter. Table reference counter is hold
70 * after calling dm_table_get_entry routine. After calling this
71 * function user must call dm_table_release before any writer table
74 * Example: dm_table_get_entry
75 * dm_table_destroy/dm_table_switch_tables
76 * This exaple will lead to deadlock situation because after dm_table_get_entry
77 * table reference counter is != 0 and dm_table_destroy have to wait on cv until
78 * reference counter is 0.
82 #include <sys/types.h>
83 #include <sys/device.h>
84 #include <sys/malloc.h>
85 #include <sys/vnode.h>
86 #include <dev/disk/dm/dm.h>
88 #include "netbsd-dm.h"
90 #define DM_REMOVE_FLAG(flag, name) do { \
91 prop_dictionary_get_uint32(dm_dict,DM_IOCTL_FLAGS,&flag); \
93 prop_dictionary_set_uint32(dm_dict,DM_IOCTL_FLAGS,flag); \
94 } while (/*CONSTCOND*/0)
96 #define DM_ADD_FLAG(flag, name) do { \
97 prop_dictionary_get_uint32(dm_dict,DM_IOCTL_FLAGS,&flag); \
99 prop_dictionary_set_uint32(dm_dict,DM_IOCTL_FLAGS,flag); \
100 } while (/*CONSTCOND*/0)
103 dm_table_deps(dm_table_entry_t *, prop_array_t);
106 * Print flags sent to the kernel from libevmapper.
109 dm_dbg_print_flags(int flags)
111 aprint_debug("dbg_print --- %d\n", flags);
113 if (flags & DM_READONLY_FLAG)
114 aprint_debug("dbg_flags: DM_READONLY_FLAG set In/Out\n");
116 if (flags & DM_SUSPEND_FLAG)
117 aprint_debug("dbg_flags: DM_SUSPEND_FLAG set In/Out\n");
119 if (flags & DM_PERSISTENT_DEV_FLAG)
120 aprint_debug("dbg_flags: DM_PERSISTENT_DEV_FLAG set In\n");
122 if (flags & DM_STATUS_TABLE_FLAG)
123 aprint_debug("dbg_flags: DM_STATUS_TABLE_FLAG set In\n");
125 if (flags & DM_ACTIVE_PRESENT_FLAG)
126 aprint_debug("dbg_flags: DM_ACTIVE_PRESENT_FLAG set Out\n");
128 if (flags & DM_INACTIVE_PRESENT_FLAG)
129 aprint_debug("dbg_flags: DM_INACTIVE_PRESENT_FLAG set Out\n");
131 if (flags & DM_BUFFER_FULL_FLAG)
132 aprint_debug("dbg_flags: DM_BUFFER_FULL_FLAG set Out\n");
134 if (flags & DM_SKIP_BDGET_FLAG)
135 aprint_debug("dbg_flags: DM_SKIP_BDGET_FLAG set In\n");
137 if (flags & DM_SKIP_LOCKFS_FLAG)
138 aprint_debug("dbg_flags: DM_SKIP_LOCKFS_FLAG set In\n");
140 if (flags & DM_NOFLUSH_FLAG)
141 aprint_debug("dbg_flags: DM_NOFLUSH_FLAG set In\n");
146 * Get list of all available targets from global
147 * target list and sent them back to libdevmapper.
150 dm_list_versions_ioctl(prop_dictionary_t dm_dict)
152 prop_array_t target_list;
157 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
159 dm_dbg_print_flags(flags);
160 target_list = dm_target_prop_list();
162 prop_dictionary_set(dm_dict, DM_IOCTL_CMD_DATA, target_list);
163 prop_object_release(target_list);
168 * Create in-kernel entry for device. Device attributes such as name, uuid are
169 * taken from proplib dictionary.
173 dm_dev_create_ioctl(prop_dictionary_t dm_dict)
176 const char *name, *uuid;
184 /* Get needed values from dictionary. */
185 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
186 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
187 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
189 dm_dbg_print_flags(flags);
191 /* Lookup name and uuid if device already exist quit. */
192 if ((dmv = dm_dev_lookup(name, uuid, -1)) != NULL) {
193 DM_ADD_FLAG(flags, DM_EXISTS_FLAG); /* Device already exists */
198 r = dm_dev_create(&dmv, name, uuid, flags);
200 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
201 DM_ADD_FLAG(flags, DM_EXISTS_FLAG);
202 DM_REMOVE_FLAG(flags, DM_INACTIVE_PRESENT_FLAG);
208 * Get list of created device-mapper devices fromglobal list and
213 * <key>cmd_data</key>
217 * <string>...</string>
220 * <integer>...</integer>
226 dm_dev_list_ioctl(prop_dictionary_t dm_dict)
228 prop_array_t dev_list;
234 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
236 dm_dbg_print_flags(flags);
238 dev_list = dm_dev_prop_list();
240 prop_dictionary_set(dm_dict, DM_IOCTL_CMD_DATA, dev_list);
241 prop_object_release(dev_list);
246 * Rename selected devices old name is in struct dm_ioctl.
247 * newname is taken from dictionary
249 * <key>cmd_data</key>
251 * <string>...</string>
255 dm_dev_rename_ioctl(prop_dictionary_t dm_dict)
258 prop_array_t cmd_array;
261 const char *name, *uuid, *n_name;
262 uint32_t flags, minor;
268 /* Get needed values from dictionary. */
269 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
270 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
271 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
272 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
274 dm_dbg_print_flags(flags);
276 cmd_array = prop_dictionary_get(dm_dict, DM_IOCTL_CMD_DATA);
278 prop_array_get_cstring_nocopy(cmd_array, 0, &n_name);
280 if (strlen(n_name) + 1 > DM_NAME_LEN)
283 if ((dmv = dm_dev_rem(name, uuid, minor)) == NULL) {
284 DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
287 /* change device name */
289 * XXX How to deal with this change, name only used in
290 * dm_dev_routines, should I add dm_dev_change_name which will run
291 * under the dm_dev_list mutex ?
293 strlcpy(dmv->name, n_name, DM_NAME_LEN);
295 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_OPEN, dmv->table_head.io_cnt);
296 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
297 prop_dictionary_set_cstring(dm_dict, DM_IOCTL_UUID, dmv->uuid);
303 * XXX: the rename is not yet implemented. The main complication
304 * here is devfs. We'd probably need a new function, rename_dev()
305 * that would trigger a node rename in devfs.
307 kprintf("dm_dev_rename_ioctl called, but not implemented!\n");
315 dm_dev_remove_ioctl(prop_dictionary_t dm_dict)
318 const char *name, *uuid;
319 uint32_t flags, minor, is_open;
325 /* Get needed values from dictionary. */
326 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
327 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
328 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
329 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
331 dm_dbg_print_flags(flags);
333 if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL) {
334 DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
338 is_open = dmv->is_open;
346 * This will call dm_dev_rem_dev routine which will actually remove
349 return dm_dev_remove(dmv);
353 * Try to remove all devices
356 dm_dev_remove_all_ioctl(prop_dictionary_t dm_dict)
360 /* Get needed values from dictionary. */
361 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
363 dm_dbg_print_flags(flags);
365 /* Gently remove all devices, if possible */
366 return dm_dev_remove_all(1);
370 * Return actual state of device to libdevmapper.
373 dm_dev_status_ioctl(prop_dictionary_t dm_dict)
376 const char *name, *uuid;
377 uint32_t flags, j, minor;
384 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
385 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
386 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
387 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
389 if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL) {
390 DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
393 dm_dbg_print_flags(dmv->flags);
395 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_OPEN, dmv->table_head.io_cnt);
396 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
397 prop_dictionary_set_cstring(dm_dict, DM_IOCTL_UUID, dmv->uuid);
399 if (dmv->flags & DM_SUSPEND_FLAG)
400 DM_ADD_FLAG(flags, DM_SUSPEND_FLAG);
403 * Add status flags for tables I have to check both active and
406 if ((j = dm_table_get_target_count(&dmv->table_head, DM_TABLE_ACTIVE))) {
407 DM_ADD_FLAG(flags, DM_ACTIVE_PRESENT_FLAG);
409 DM_REMOVE_FLAG(flags, DM_ACTIVE_PRESENT_FLAG);
411 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_TARGET_COUNT, j);
413 if (dm_table_get_target_count(&dmv->table_head, DM_TABLE_INACTIVE))
414 DM_ADD_FLAG(flags, DM_INACTIVE_PRESENT_FLAG);
416 DM_REMOVE_FLAG(flags, DM_INACTIVE_PRESENT_FLAG);
423 * Set only flag to suggest that device is suspended. This call is
424 * not supported in NetBSD.
428 dm_dev_suspend_ioctl(prop_dictionary_t dm_dict)
431 const char *name, *uuid;
432 uint32_t flags, minor;
438 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
439 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
440 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
441 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
443 if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL) {
444 DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
447 atomic_set_int(&dmv->flags, DM_SUSPEND_FLAG);
449 dm_dbg_print_flags(dmv->flags);
451 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_OPEN, dmv->table_head.io_cnt);
452 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_FLAGS, dmv->flags);
453 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
457 /* Add flags to dictionary flag after dmv -> dict copy */
458 DM_ADD_FLAG(flags, DM_EXISTS_FLAG);
463 * Simulate Linux behaviour better and switch tables here and not in
464 * dm_table_load_ioctl.
467 dm_dev_resume_ioctl(prop_dictionary_t dm_dict)
470 const char *name, *uuid;
471 uint32_t flags, minor;
478 * char *xml; xml = prop_dictionary_externalize(dm_dict);
479 * kprintf("%s\n",xml);
482 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
483 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
484 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
485 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
487 /* Remove device from global device list */
488 if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL) {
489 DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
492 atomic_clear_int(&dmv->flags, (DM_SUSPEND_FLAG | DM_INACTIVE_PRESENT_FLAG));
493 atomic_set_int(&dmv->flags, DM_ACTIVE_PRESENT_FLAG);
495 dm_table_switch_tables(&dmv->table_head);
497 DM_ADD_FLAG(flags, DM_EXISTS_FLAG);
499 dmsetdiskinfo(dmv->diskp, &dmv->table_head);
501 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_OPEN, dmv->table_head.io_cnt);
502 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_FLAGS, flags);
503 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
507 /* Destroy inactive table after resume. */
508 dm_table_destroy(&dmv->table_head, DM_TABLE_INACTIVE);
513 * Table management routines
514 * lvm2tools doens't send name/uuid to kernel with table
515 * for lookup I have to use minor number.
519 * Remove inactive table from device. Routines which work's with inactive tables
520 * doesn't need to synchronise with dmstrategy. They can synchronise themselves with mutex?.
524 dm_table_clear_ioctl(prop_dictionary_t dm_dict)
527 const char *name, *uuid;
528 uint32_t flags, minor;
536 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
537 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
538 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
539 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
541 aprint_debug("Clearing inactive table from device: %s--%s\n",
544 if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL) {
545 DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
548 /* Select unused table */
549 dm_table_destroy(&dmv->table_head, DM_TABLE_INACTIVE);
551 atomic_clear_int(&dmv->flags, DM_INACTIVE_PRESENT_FLAG);
558 * Get list of physical devices for active table.
559 * Get dev_t from pdev vnode and insert it into cmd_array.
561 * XXX. This function is called from lvm2tools to get information
562 * about physical devices, too e.g. during vgcreate.
565 dm_table_deps_ioctl(prop_dictionary_t dm_dict)
569 dm_table_entry_t *table_en;
571 prop_array_t cmd_array;
572 const char *name, *uuid;
573 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 /* create array for dev_t's */
588 cmd_array = prop_array_create();
590 if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL) {
591 DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
594 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
595 prop_dictionary_set_cstring(dm_dict, DM_IOCTL_NAME, dmv->name);
596 prop_dictionary_set_cstring(dm_dict, DM_IOCTL_UUID, dmv->uuid);
598 aprint_debug("Getting table deps for device: %s\n", dmv->name);
601 * if DM_QUERY_INACTIVE_TABLE_FLAG is passed we need to query
604 if (flags & DM_QUERY_INACTIVE_TABLE_FLAG)
605 table_type = DM_TABLE_INACTIVE;
607 table_type = DM_TABLE_ACTIVE;
609 tbl = dm_table_get_entry(&dmv->table_head, table_type);
611 TAILQ_FOREACH(table_en, tbl, next)
612 dm_table_deps(table_en, cmd_array);
614 dm_table_release(&dmv->table_head, table_type);
617 prop_dictionary_set(dm_dict, DM_IOCTL_CMD_DATA, cmd_array);
618 prop_object_release(cmd_array);
624 dm_table_deps(dm_table_entry_t *table_en, prop_array_t array)
630 size = prop_array_count(array);
632 TAILQ_FOREACH(map, &table_en->pdev_maps, next) {
633 ret = dm_pdev_get_udev(map->data.pdev);
634 for (i = 0; i < size; i++) {
635 if (prop_array_get_uint64(array, i, &tmp) == true)
640 * Ignore if the device has already been added by
644 prop_array_add_uint64(array, ret);
651 * Load new table/tables to device.
652 * Call apropriate target init routine open all physical pdev's and
653 * link them to device. For other targets mirror, strip, snapshot
654 * etc. also add dependency devices to upcalls list.
656 * Load table to inactive slot table are switched in dm_device_resume_ioctl.
657 * This simulates Linux behaviour better there should not be any difference.
661 dm_table_load_ioctl(prop_dictionary_t dm_dict)
664 dm_table_entry_t *table_en;
668 prop_object_iterator_t iter;
669 prop_array_t cmd_array;
670 prop_dictionary_t target_dict;
672 const char *name, *uuid, *type;
674 uint32_t flags, ret, minor;
686 * char *xml; xml = prop_dictionary_externalize(dm_dict);
687 * kprintf("%s\n",xml);
690 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
691 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
692 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
693 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
695 cmd_array = prop_dictionary_get(dm_dict, DM_IOCTL_CMD_DATA);
696 iter = prop_array_iterator(cmd_array);
697 dm_dbg_print_flags(flags);
699 if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL) {
700 DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
703 aprint_debug("Loading table to device: %s--%d\n", name,
704 dmv->table_head.cur_active_table);
707 * I have to check if this table slot is not used by another table list.
708 * if it is used I should free them.
710 if (dmv->flags & DM_INACTIVE_PRESENT_FLAG)
711 dm_table_destroy(&dmv->table_head, DM_TABLE_INACTIVE);
713 dm_dbg_print_flags(dmv->flags);
714 tbl = dm_table_get_entry(&dmv->table_head, DM_TABLE_INACTIVE);
716 aprint_debug("dmv->name = %s\n", dmv->name);
718 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
720 while ((target_dict = prop_object_iterator_next(iter)) != NULL) {
721 prop_dictionary_get_cstring_nocopy(target_dict,
722 DM_TABLE_TYPE, &type);
724 * If we want to deny table with 2 or more different
725 * target we should do it here
727 if (((target = dm_target_lookup(type)) == NULL) &&
728 ((target = dm_target_autoload(type)) == NULL)) {
729 dm_table_release(&dmv->table_head, DM_TABLE_INACTIVE);
733 if ((table_en = kmalloc(sizeof(dm_table_entry_t),
734 M_DM, M_WAITOK)) == NULL) {
735 dm_table_release(&dmv->table_head, DM_TABLE_INACTIVE);
737 dm_target_unbusy(target);
740 prop_dictionary_get_uint64(target_dict, DM_TABLE_START,
742 prop_dictionary_get_uint64(target_dict, DM_TABLE_LENGTH,
745 aprint_debug("dm_ioctl.c... table_en->start = %ju, "
746 "table_en->length = %ju\n",
747 (uintmax_t)table_en->start,
748 (uintmax_t)table_en->length);
750 table_en->target = target;
752 table_en->target_config = NULL;
753 TAILQ_INIT(&table_en->pdev_maps);
756 * There is a parameter string after dm_target_spec
757 * structure which points to /dev/wd0a 284 part of
758 * table. String str points to this text. This can be
759 * null and therefore it should be checked before we try to
762 prop_dictionary_get_cstring(target_dict,
763 DM_TABLE_PARAMS, &str);
765 TAILQ_INSERT_TAIL(tbl, table_en, next);
768 * Params string is different for every target,
769 * therfore I have to pass it to target init
770 * routine and parse parameters there.
772 aprint_debug("DM: str passed in is: \"%s\"\n", str);
773 KKASSERT(target->init);
775 if ((ret = target->init(table_en, str)) != 0) {
776 dm_table_release(&dmv->table_head, DM_TABLE_INACTIVE);
777 dm_table_destroy(&dmv->table_head, DM_TABLE_INACTIVE);
785 prop_object_iterator_release(iter);
787 DM_ADD_FLAG(flags, DM_INACTIVE_PRESENT_FLAG);
788 atomic_set_int(&dmv->flags, DM_INACTIVE_PRESENT_FLAG);
790 dm_table_release(&dmv->table_head, DM_TABLE_INACTIVE);
794 dmsetdiskinfo(dmv->diskp, &dmv->table_head);
799 * Get description of all tables loaded to device from kernel
800 * and send it to libdevmapper.
802 * Output dictionary for every table:
804 * <key>cmd_data</key>
808 * <string>...</string>
811 * <integer>...</integer>
814 * <integer>...</integer>
817 * <string>...</string>
823 dm_table_status_ioctl(prop_dictionary_t dm_dict)
827 dm_table_entry_t *table_en;
829 prop_array_t cmd_array;
830 prop_dictionary_t target_dict;
834 const char *name, *uuid;
845 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
846 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
847 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
848 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
850 cmd_array = prop_array_create();
852 if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL) {
853 DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
857 * if DM_QUERY_INACTIVE_TABLE_FLAG is passed we need to query
860 if (flags & DM_QUERY_INACTIVE_TABLE_FLAG)
861 table_type = DM_TABLE_INACTIVE;
863 table_type = DM_TABLE_ACTIVE;
865 if (dm_table_get_target_count(&dmv->table_head, DM_TABLE_ACTIVE))
866 DM_ADD_FLAG(flags, DM_ACTIVE_PRESENT_FLAG);
868 DM_REMOVE_FLAG(flags, DM_ACTIVE_PRESENT_FLAG);
870 if (dm_table_get_target_count(&dmv->table_head, DM_TABLE_INACTIVE))
871 DM_ADD_FLAG(flags, DM_INACTIVE_PRESENT_FLAG);
873 DM_REMOVE_FLAG(flags, DM_INACTIVE_PRESENT_FLAG);
877 if (dmv->flags & DM_SUSPEND_FLAG)
878 DM_ADD_FLAG(flags, DM_SUSPEND_FLAG);
880 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
882 aprint_debug("Status of device tables: %s--%d\n",
883 name, dmv->table_head.cur_active_table);
885 tbl = dm_table_get_entry(&dmv->table_head, table_type);
887 TAILQ_FOREACH(table_en, tbl, next) {
888 target_dict = prop_dictionary_create();
889 aprint_debug("%016" PRIu64 ", length %016" PRIu64
890 ", target %s\n", table_en->start, table_en->length,
891 table_en->target->name);
893 prop_dictionary_set_uint64(target_dict, DM_TABLE_START,
895 prop_dictionary_set_uint64(target_dict, DM_TABLE_LENGTH,
898 prop_dictionary_set_cstring(target_dict, DM_TABLE_TYPE,
899 table_en->target->name);
901 /* dm_table_get_cur_actv.table ?? */
902 prop_dictionary_set_int32(target_dict, DM_TABLE_STAT,
903 dmv->table_head.cur_active_table);
905 if (flags & DM_STATUS_TABLE_FLAG) {
906 params = table_en->target->table
907 (table_en->target_config);
908 } else if (table_en->target->info) {
909 params = table_en->target->info
910 (table_en->target_config);
915 if (params != NULL) {
916 prop_dictionary_set_cstring(target_dict,
917 DM_TABLE_PARAMS, params);
920 prop_dictionary_set_cstring(target_dict,
921 DM_TABLE_PARAMS, "");
924 prop_array_add(cmd_array, target_dict);
925 prop_object_release(target_dict);
928 dm_table_release(&dmv->table_head, table_type);
931 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_FLAGS, flags);
932 prop_dictionary_set(dm_dict, DM_IOCTL_CMD_DATA, cmd_array);
933 prop_object_release(cmd_array);
939 dm_message_ioctl(prop_dictionary_t dm_dict)
942 dm_table_entry_t *table_en;
944 const char *name, *uuid;
945 uint32_t flags, minor;
946 uint64_t table_start, table_end, sector;
954 /* Get needed values from dictionary. */
955 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
956 prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
957 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
958 prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
959 prop_dictionary_get_uint64(dm_dict, DM_MESSAGE_SECTOR, §or);
961 dm_dbg_print_flags(flags);
963 if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL) {
964 DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
968 /* Get message string */
969 prop_dictionary_get_cstring(dm_dict, DM_MESSAGE_STR, &msg);
971 tbl = dm_table_get_entry(&dmv->table_head, DM_TABLE_ACTIVE);
976 if (!TAILQ_EMPTY(tbl)) {
977 table_en = TAILQ_FIRST(tbl);
981 TAILQ_FOREACH(table_en, tbl, next) {
982 table_start = table_en->start;
983 table_end = table_start + table_en->length;
985 if ((sector >= table_start) && (sector < table_end)) {
993 if (table_en->target->message != NULL)
994 ret = table_en->target->message(table_en, msg);
997 dm_table_release(&dmv->table_head, DM_TABLE_ACTIVE);
1007 * For every call I have to set kernel driver version.
1008 * Because I can have commands supported only in other
1009 * newer/later version. This routine is called for every
1013 dm_check_version(prop_dictionary_t dm_dict)
1019 ver = prop_dictionary_get(dm_dict, DM_IOCTL_VERSION);
1021 for (i = 0; i < 3; i++)
1022 prop_array_get_uint32(ver, i, &dm_version[i]);
1024 if (DM_VERSION_MAJOR != dm_version[0] || DM_VERSION_MINOR < dm_version[1]) {
1025 aprint_debug("libdevmapper/kernel version mismatch "
1026 "kernel: %d.%d.%d libdevmapper: %d.%d.%d\n",
1027 DM_VERSION_MAJOR, DM_VERSION_MINOR, DM_VERSION_PATCHLEVEL,
1028 dm_version[0], dm_version[1], dm_version[2]);
1032 prop_array_set_uint32(ver, 0, DM_VERSION_MAJOR);
1033 prop_array_set_uint32(ver, 1, DM_VERSION_MINOR);
1034 prop_array_set_uint32(ver, 2, DM_VERSION_PATCHLEVEL);
1036 prop_dictionary_set(dm_dict, DM_IOCTL_VERSION, ver);