Home | History | Annotate | Line # | Download | only in dm
      1 /* $NetBSD: dm_ioctl.c,v 1.57 2024/01/14 07:56:53 mlelstv Exp $      */
      2 
      3 /*
      4  * Copyright (c) 2008 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Adam Hamsik.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     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.
     18  *
     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.
     30  */
     31 #include <sys/cdefs.h>
     32 __KERNEL_RCSID(0, "$NetBSD: dm_ioctl.c,v 1.57 2024/01/14 07:56:53 mlelstv Exp $");
     33 
     34 /*
     35  * Locking is used to synchronise between ioctl calls and between dm_table's
     36  * users.
     37  *
     38  * ioctl locking:
     39  * Simple reference counting, to count users of device will be used routines
     40  * dm_dev_busy/dm_dev_unbusy are used for that.
     41  * dm_dev_lookup/dm_dev_rem call dm_dev_busy before return(caller is therefore
     42  * holder of reference_counter last).
     43  *
     44  * ioctl routines which change/remove dm_dev parameters must wait on
     45  * dm_dev::dev_cv and when last user will call dm_dev_unbusy they will wake
     46  * up them.
     47  *
     48  * table_head locking:
     49  * To access table entries dm_table_* routines must be used.
     50  *
     51  * dm_table_get_entry will increment table users reference
     52  * counter. It will return active or inactive table depends
     53  * on uint8_t argument.
     54  *
     55  * dm_table_release must be called for every table_entry from
     56  * dm_table_get_entry. Between these two calls tables can't be switched
     57  * or destroyed.
     58  *
     59  * dm_table_head_init initialize table_entries SLISTS and io_cv.
     60  *
     61  * dm_table_head_destroy destroy cv.
     62  *
     63  * There are two types of users for dm_table_head first type will
     64  * only read list and try to do anything with it e.g. dmstrategy,
     65  * dm_table_size etc. There is another user for table_head which wants
     66  * to change table lists e.g. dm_dev_resume_ioctl, dm_dev_remove_ioctl,
     67  * dm_table_clear_ioctl.
     68  *
     69  * NOTE: It is not allowed to call dm_table_destroy, dm_table_switch_tables
     70  *       with hold table reference counter. Table reference counter is hold
     71  *       after calling dm_table_get_entry routine. After calling this
     72  *       function user must call dm_table_release before any writer table
     73  *       operation.
     74  *
     75  * Example: dm_table_get_entry
     76  *          dm_table_destroy/dm_table_switch_tables
     77  * This example will lead to deadlock situation because after dm_table_get_entry
     78  * table reference counter is != 0 and dm_table_destroy have to wait on cv until
     79  * reference counter is 0.
     80  *
     81  */
     82 
     83 #include <sys/types.h>
     84 #include <sys/param.h>
     85 #include <sys/device.h>
     86 #include <sys/disk.h>
     87 #include <sys/disklabel.h>
     88 #include <sys/kmem.h>
     89 #include <sys/malloc.h>
     90 
     91 #include <machine/int_fmtio.h>
     92 
     93 #include "netbsd-dm.h"
     94 #include "dm.h"
     95 #include "ioconf.h"
     96 
     97 extern struct cfattach dm_ca;
     98 static uint32_t sc_minor_num;
     99 uint32_t dm_dev_counter;
    100 
    101 #define DM_REMOVE_FLAG(flag, name) do {					\
    102 	prop_dictionary_get_uint32(dm_dict,DM_IOCTL_FLAGS,&flag);	\
    103 	flag &= ~name;							\
    104 	prop_dictionary_set_uint32(dm_dict,DM_IOCTL_FLAGS,flag);	\
    105 } while (/*CONSTCOND*/0)
    106 
    107 #define DM_ADD_FLAG(flag, name) do {					\
    108 	prop_dictionary_get_uint32(dm_dict,DM_IOCTL_FLAGS,&flag);	\
    109 	flag |= name;							\
    110 	prop_dictionary_set_uint32(dm_dict,DM_IOCTL_FLAGS,flag);	\
    111 } while (/*CONSTCOND*/0)
    112 
    113 static int dm_table_deps(dm_table_entry_t *, prop_array_t);
    114 static int dm_table_init(dm_target_t *, dm_table_entry_t *, char *);
    115 
    116 /*
    117  * Print flags sent to the kernel from libdevmapper.
    118  */
    119 static int
    120 dm_dbg_print_flags(uint32_t flags)
    121 {
    122 
    123 	aprint_debug("dbg_print --- %d\n", flags);
    124 
    125 	if (flags & DM_READONLY_FLAG)
    126 		aprint_debug("dbg_flags: DM_READONLY_FLAG set In/Out\n");
    127 
    128 	if (flags & DM_SUSPEND_FLAG)
    129 		aprint_debug("dbg_flags: DM_SUSPEND_FLAG set In/Out\n");
    130 
    131 	if (flags & DM_PERSISTENT_DEV_FLAG)
    132 		aprint_debug("dbg_flags: DM_PERSISTENT_DEV_FLAG set In\n");
    133 
    134 	if (flags & DM_STATUS_TABLE_FLAG)
    135 		aprint_debug("dbg_flags: DM_STATUS_TABLE_FLAG set In\n");
    136 
    137 	if (flags & DM_ACTIVE_PRESENT_FLAG)
    138 		aprint_debug("dbg_flags: DM_ACTIVE_PRESENT_FLAG set Out\n");
    139 
    140 	if (flags & DM_INACTIVE_PRESENT_FLAG)
    141 		aprint_debug("dbg_flags: DM_INACTIVE_PRESENT_FLAG set Out\n");
    142 
    143 	if (flags & DM_BUFFER_FULL_FLAG)
    144 		aprint_debug("dbg_flags: DM_BUFFER_FULL_FLAG set Out\n");
    145 
    146 	if (flags & DM_SKIP_BDGET_FLAG)
    147 		aprint_debug("dbg_flags: DM_SKIP_BDGET_FLAG set In\n");
    148 
    149 	if (flags & DM_SKIP_LOCKFS_FLAG)
    150 		aprint_debug("dbg_flags: DM_SKIP_LOCKFS_FLAG set In\n");
    151 
    152 	if (flags & DM_NOFLUSH_FLAG)
    153 		aprint_debug("dbg_flags: DM_NOFLUSH_FLAG set In\n");
    154 
    155 	return 0;
    156 }
    157 
    158 /*
    159  * Get list of all available targets from global
    160  * target list and sent them back to libdevmapper.
    161  */
    162 int
    163 dm_list_versions_ioctl(prop_dictionary_t dm_dict)
    164 {
    165 	prop_array_t target_list;
    166 	uint32_t flags;
    167 
    168 	flags = 0;
    169 
    170 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
    171 
    172 	dm_dbg_print_flags(flags);
    173 	target_list = dm_target_prop_list();
    174 
    175 	prop_dictionary_set(dm_dict, DM_IOCTL_CMD_DATA, target_list);
    176 	prop_object_release(target_list);
    177 
    178 	return 0;
    179 }
    180 
    181 /*
    182  * Create in-kernel entry for device. Device attributes such as name, uuid are
    183  * taken from proplib dictionary.
    184  */
    185 int
    186 dm_dev_create_ioctl(prop_dictionary_t dm_dict)
    187 {
    188 	dm_dev_t *dmv;
    189 	const char *name, *uuid;
    190 	int r;
    191 	uint32_t flags;
    192 	device_t devt;
    193 	cfdata_t cf;
    194 
    195 	flags = 0;
    196 	name = NULL;
    197 	uuid = NULL;
    198 
    199 	/* Get needed values from dictionary. */
    200 	prop_dictionary_get_string(dm_dict, DM_IOCTL_NAME, &name);
    201 	prop_dictionary_get_string(dm_dict, DM_IOCTL_UUID, &uuid);
    202 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
    203 
    204 	dm_dbg_print_flags(flags);
    205 
    206 	/* Lookup name and uuid if device already exist quit. */
    207 	if ((dmv = dm_dev_lookup(name, uuid, -1)) != NULL) {
    208 		DM_ADD_FLAG(flags, DM_EXISTS_FLAG);	/* Device already exists */
    209 		dm_dev_unbusy(dmv);
    210 		return EEXIST;
    211 	}
    212 
    213 	if ((dmv = dm_dev_alloc()) == NULL)
    214 		return ENOMEM;
    215 
    216 	cf = kmem_alloc(sizeof(*cf), KM_SLEEP);
    217 	cf->cf_name = dm_cd.cd_name;
    218 	cf->cf_atname = dm_ca.ca_name;
    219 	cf->cf_unit = atomic_inc_32_nv(&sc_minor_num);
    220 	cf->cf_fstate = FSTATE_NOTFOUND;
    221 	if ((devt = config_attach_pseudo(cf)) == NULL) {
    222 		dm_dev_free(dmv);
    223 		kmem_free(cf, sizeof(*cf));
    224 		aprint_error("Unable to attach pseudo device dm/%s\n", name);
    225 		return (ENOMEM);
    226 	}
    227 
    228 	if (uuid)
    229 		strncpy(dmv->uuid, uuid, DM_UUID_LEN);
    230 	else
    231 		dmv->uuid[0] = '\0';
    232 
    233 	if (name)
    234 		strlcpy(dmv->name, name, DM_NAME_LEN);
    235 
    236 	dmv->minor = cf->cf_unit;
    237 	dmv->flags = 0;		/* device flags are set when needed */
    238 	dmv->ref_cnt = 0;
    239 	dmv->event_nr = 0;
    240 	dmv->devt = devt;
    241 
    242 	dm_table_head_init(&dmv->table_head);
    243 
    244 	mutex_init(&dmv->dev_mtx, MUTEX_DEFAULT, IPL_NONE);
    245 	mutex_init(&dmv->diskp_mtx, MUTEX_DEFAULT, IPL_NONE);
    246 	cv_init(&dmv->dev_cv, "dm_dev");
    247 
    248 	if (flags & DM_READONLY_FLAG)
    249 		dmv->flags |= DM_READONLY_FLAG;
    250 
    251 	prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
    252 
    253 	disk_init(dmv->diskp, device_xname(devt), &dmdkdriver);
    254 	disk_attach(dmv->diskp);
    255 
    256 	dmv->diskp->dk_info = NULL;
    257 
    258 	if ((r = dm_dev_insert(dmv)) != 0)
    259 		dm_dev_free(dmv);
    260 
    261 	DM_ADD_FLAG(flags, DM_EXISTS_FLAG);
    262 	DM_REMOVE_FLAG(flags, DM_INACTIVE_PRESENT_FLAG);
    263 
    264 	/* Increment device counter After creating device */
    265 	atomic_inc_32(&dm_dev_counter);
    266 
    267 	return r;
    268 }
    269 
    270 /*
    271  * Get list of created device-mapper devices from global list and
    272  * send it to kernel.
    273  *
    274  * Output dictionary:
    275  *
    276  * <key>cmd_data</key>
    277  *  <array>
    278  *   <dict>
    279  *    <key>name<key>
    280  *    <string>...</string>
    281  *
    282  *    <key>dev</key>
    283  *    <integer>...</integer>
    284  *   </dict>
    285  *  </array>
    286  */
    287 int
    288 dm_dev_list_ioctl(prop_dictionary_t dm_dict)
    289 {
    290 	prop_array_t dev_list;
    291 	uint32_t flags;
    292 
    293 	flags = 0;
    294 
    295 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
    296 
    297 	dm_dbg_print_flags(flags);
    298 
    299 	dev_list = dm_dev_prop_list();
    300 
    301 	prop_dictionary_set(dm_dict, DM_IOCTL_CMD_DATA, dev_list);
    302 	prop_object_release(dev_list);
    303 
    304 	return 0;
    305 }
    306 
    307 /*
    308  * Rename selected devices old name is in struct dm_ioctl.
    309  * newname is taken from dictionary
    310  *
    311  * <key>cmd_data</key>
    312  *  <array>
    313  *   <string>...</string>
    314  *  </array>
    315  */
    316 int
    317 dm_dev_rename_ioctl(prop_dictionary_t dm_dict)
    318 {
    319 	prop_array_t cmd_array;
    320 	dm_dev_t *dmv;
    321 
    322 	const char *name, *uuid, *n_name;
    323 	uint32_t flags, minor;
    324 
    325 	name = NULL;
    326 	uuid = NULL;
    327 	minor = 0;
    328 
    329 	/* Get needed values from dictionary. */
    330 	prop_dictionary_get_string(dm_dict, DM_IOCTL_NAME, &name);
    331 	prop_dictionary_get_string(dm_dict, DM_IOCTL_UUID, &uuid);
    332 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
    333 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
    334 
    335 	dm_dbg_print_flags(flags);
    336 
    337 	cmd_array = prop_dictionary_get(dm_dict, DM_IOCTL_CMD_DATA);
    338 
    339 	prop_array_get_string(cmd_array, 0, &n_name);
    340 
    341 	if (strlen(n_name) + 1 > DM_NAME_LEN)
    342 		return EINVAL;
    343 
    344 	if ((dmv = dm_dev_rem(name, uuid, minor)) == NULL) {
    345 		DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
    346 		return ENOENT;
    347 	}
    348 	/* change device name */
    349 	/*
    350 	 * XXX How to deal with this change, name only used in
    351 	 * dm_dev_routines, should I add dm_dev_change_name which will run
    352 	 * under the dm_dev_list mutex ?
    353 	 */
    354 	strlcpy(dmv->name, n_name, DM_NAME_LEN);
    355 
    356 	prop_dictionary_set_uint32(dm_dict, DM_IOCTL_OPEN, dmv->table_head.io_cnt);
    357 	prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
    358 	prop_dictionary_set_string(dm_dict, DM_IOCTL_UUID, dmv->uuid);
    359 
    360 	dm_dev_insert(dmv);
    361 
    362 	return 0;
    363 }
    364 
    365 /*
    366  * Remove device from global list I have to remove active
    367  * and inactive tables first.
    368  */
    369 int
    370 dm_dev_remove_ioctl(prop_dictionary_t dm_dict)
    371 {
    372 	int error;
    373 	cfdata_t cf;
    374 	dm_dev_t *dmv;
    375 	const char *name, *uuid;
    376 	uint32_t flags, minor;
    377 	device_t devt;
    378 
    379 	flags = 0;
    380 	name = NULL;
    381 	uuid = NULL;
    382 
    383 	/* Get needed values from dictionary. */
    384 	prop_dictionary_get_string(dm_dict, DM_IOCTL_NAME, &name);
    385 	prop_dictionary_get_string(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);
    388 
    389 	dm_dbg_print_flags(flags);
    390 
    391 	/*
    392 	 * This seems as hack to me, probably use routine dm_dev_get_devt to
    393 	 * atomically get devt from device.
    394 	 */
    395 	if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL) {
    396 		DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
    397 		return ENOENT;
    398 	}
    399 	devt = dmv->devt;
    400 
    401 	dm_dev_unbusy(dmv);
    402 
    403 	/*
    404 	 * This will call dm_detach routine which will actually removes
    405 	 * device.
    406 	 */
    407 	cf = device_cfdata(devt);
    408 	error = config_detach(devt, DETACH_QUIET);
    409 	if (error == 0)
    410 		kmem_free(cf, sizeof(*cf));
    411 	return error;
    412 }
    413 
    414 /*
    415  * Return actual state of device to libdevmapper.
    416  */
    417 int
    418 dm_dev_status_ioctl(prop_dictionary_t dm_dict)
    419 {
    420 	dm_dev_t *dmv;
    421 	const char *name, *uuid;
    422 	uint32_t flags, j, minor;
    423 
    424 	name = NULL;
    425 	uuid = NULL;
    426 	flags = 0;
    427 
    428 	prop_dictionary_get_string(dm_dict, DM_IOCTL_NAME, &name);
    429 	prop_dictionary_get_string(dm_dict, DM_IOCTL_UUID, &uuid);
    430 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
    431 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
    432 
    433 	if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL) {
    434 		DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
    435 		return ENOENT;
    436 	}
    437 	dm_dbg_print_flags(dmv->flags);
    438 
    439 	prop_dictionary_set_uint32(dm_dict, DM_IOCTL_OPEN, dmv->table_head.io_cnt);
    440 	prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
    441 	prop_dictionary_set_string(dm_dict, DM_IOCTL_UUID, dmv->uuid);
    442 
    443 	if (dmv->flags & DM_SUSPEND_FLAG)
    444 		DM_ADD_FLAG(flags, DM_SUSPEND_FLAG);
    445 
    446 	/*
    447 	 * Add status flags for tables I have to check both active and
    448 	 * inactive tables.
    449 	 */
    450 	if ((j = dm_table_get_target_count(&dmv->table_head, DM_TABLE_ACTIVE)))
    451 		DM_ADD_FLAG(flags, DM_ACTIVE_PRESENT_FLAG);
    452 	else
    453 		DM_REMOVE_FLAG(flags, DM_ACTIVE_PRESENT_FLAG);
    454 
    455 	prop_dictionary_set_uint32(dm_dict, DM_IOCTL_TARGET_COUNT, j);
    456 
    457 	if (dm_table_get_target_count(&dmv->table_head, DM_TABLE_INACTIVE))
    458 		DM_ADD_FLAG(flags, DM_INACTIVE_PRESENT_FLAG);
    459 	else
    460 		DM_REMOVE_FLAG(flags, DM_INACTIVE_PRESENT_FLAG);
    461 
    462 	dm_dev_unbusy(dmv);
    463 
    464 	return 0;
    465 }
    466 
    467 /*
    468  * Set only flag to suggest that device is suspended. This call is
    469  * not supported in NetBSD.
    470  */
    471 int
    472 dm_dev_suspend_ioctl(prop_dictionary_t dm_dict)
    473 {
    474 	dm_dev_t *dmv;
    475 	const char *name, *uuid;
    476 	uint32_t flags, minor;
    477 
    478 	name = NULL;
    479 	uuid = NULL;
    480 	flags = 0;
    481 
    482 	prop_dictionary_get_string(dm_dict, DM_IOCTL_NAME, &name);
    483 	prop_dictionary_get_string(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);
    486 
    487 	if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL) {
    488 		DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
    489 		return ENOENT;
    490 	}
    491 	atomic_or_32(&dmv->flags, DM_SUSPEND_FLAG);
    492 
    493 	dm_dbg_print_flags(dmv->flags);
    494 
    495 	prop_dictionary_set_uint32(dm_dict, DM_IOCTL_OPEN, dmv->table_head.io_cnt);
    496 	prop_dictionary_set_uint32(dm_dict, DM_IOCTL_FLAGS, dmv->flags);
    497 	prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
    498 
    499 	dm_dev_unbusy(dmv);
    500 
    501 	/* Add flags to dictionary flag after dmv -> dict copy */
    502 	DM_ADD_FLAG(flags, DM_EXISTS_FLAG);
    503 
    504 	return 0;
    505 }
    506 
    507 /*
    508  * Simulate Linux behaviour better and switch tables here and not in
    509  * dm_table_load_ioctl.
    510  */
    511 int
    512 dm_dev_resume_ioctl(prop_dictionary_t dm_dict)
    513 {
    514 	dm_dev_t *dmv;
    515 	const char *name, *uuid;
    516 	uint32_t flags, minor;
    517 
    518 	name = NULL;
    519 	uuid = NULL;
    520 	flags = 0;
    521 
    522 	/*
    523 	 * char *xml; xml = prop_dictionary_externalize(dm_dict);
    524 	 * printf("%s\n",xml);
    525 	 */
    526 
    527 	prop_dictionary_get_string(dm_dict, DM_IOCTL_NAME, &name);
    528 	prop_dictionary_get_string(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);
    531 
    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);
    535 		return ENOENT;
    536 	}
    537 
    538 	/* Make inactive table active, if it exists */
    539 	if (dmv->flags & DM_INACTIVE_PRESENT_FLAG)
    540 		dm_table_switch_tables(&dmv->table_head);
    541 
    542 	atomic_and_32(&dmv->flags, ~(DM_SUSPEND_FLAG | DM_INACTIVE_PRESENT_FLAG));
    543 	atomic_or_32(&dmv->flags, DM_ACTIVE_PRESENT_FLAG);
    544 
    545 	DM_ADD_FLAG(flags, DM_EXISTS_FLAG);
    546 
    547 	dmgetproperties(dmv->diskp, &dmv->table_head);
    548 
    549 	prop_dictionary_set_uint32(dm_dict, DM_IOCTL_OPEN, dmv->table_head.io_cnt);
    550 	prop_dictionary_set_uint32(dm_dict, DM_IOCTL_FLAGS, flags);
    551 	prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
    552 
    553 	dm_dev_unbusy(dmv);
    554 
    555 	/* Destroy inactive table after resume. */
    556 	dm_table_destroy(&dmv->table_head, DM_TABLE_INACTIVE);
    557 
    558 	return 0;
    559 }
    560 
    561 /*
    562  * Table management routines
    563  * lvm2tools doesn't send name/uuid to kernel with table
    564  * for lookup I have to use minor number.
    565  */
    566 
    567 /*
    568  * Remove inactive table from device. Routines which work's with inactive tables
    569  * doesn't need to synchronise with dmstrategy. They can synchronise themselves with mutex?.
    570  */
    571 int
    572 dm_table_clear_ioctl(prop_dictionary_t dm_dict)
    573 {
    574 	dm_dev_t *dmv;
    575 	const char *name, *uuid;
    576 	uint32_t flags, minor;
    577 
    578 	name = NULL;
    579 	uuid = NULL;
    580 	flags = 0;
    581 	minor = 0;
    582 
    583 	prop_dictionary_get_string(dm_dict, DM_IOCTL_NAME, &name);
    584 	prop_dictionary_get_string(dm_dict, DM_IOCTL_UUID, &uuid);
    585 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
    586 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
    587 
    588 	aprint_debug("Clearing inactive table from device: %s--%s\n",
    589 	    name, uuid);
    590 
    591 	if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL) {
    592 		DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
    593 		return ENOENT;
    594 	}
    595 	/* Select unused table */
    596 	dm_table_destroy(&dmv->table_head, DM_TABLE_INACTIVE);
    597 
    598 	atomic_and_32(&dmv->flags, ~DM_INACTIVE_PRESENT_FLAG);
    599 
    600 	dm_dev_unbusy(dmv);
    601 
    602 	return 0;
    603 }
    604 
    605 /*
    606  * Get list of physical devices for active table.
    607  * Get dev_t from pdev vnode and insert it into cmd_array.
    608  *
    609  * XXX. This function is called from lvm2tools to get information
    610  *      about physical devices, too e.g. during vgcreate.
    611  */
    612 int
    613 dm_table_deps_ioctl(prop_dictionary_t dm_dict)
    614 {
    615 	dm_dev_t *dmv;
    616 	dm_table_t *tbl;
    617 	dm_table_entry_t *table_en;
    618 
    619 	prop_array_t cmd_array;
    620 	const char *name, *uuid;
    621 	uint32_t flags, minor;
    622 	int table_type;
    623 
    624 	name = NULL;
    625 	uuid = NULL;
    626 	flags = 0;
    627 
    628 	prop_dictionary_get_string(dm_dict, DM_IOCTL_NAME, &name);
    629 	prop_dictionary_get_string(dm_dict, DM_IOCTL_UUID, &uuid);
    630 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
    631 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
    632 
    633 	/* create array for dev_t's */
    634 	cmd_array = prop_array_create();
    635 
    636 	if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL) {
    637 		DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
    638 		return ENOENT;
    639 	}
    640 	prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
    641 	prop_dictionary_set_string(dm_dict, DM_IOCTL_NAME, dmv->name);
    642 	prop_dictionary_set_string(dm_dict, DM_IOCTL_UUID, dmv->uuid);
    643 
    644 	aprint_debug("Getting table deps for device: %s\n", dmv->name);
    645 
    646 	/*
    647 	 * if DM_QUERY_INACTIVE_TABLE_FLAG is passed we need to query
    648 	 * INACTIVE TABLE
    649 	 */
    650 	if (flags & DM_QUERY_INACTIVE_TABLE_FLAG)
    651 		table_type = DM_TABLE_INACTIVE;
    652 	else
    653 		table_type = DM_TABLE_ACTIVE;
    654 
    655 	tbl = dm_table_get_entry(&dmv->table_head, table_type);
    656 
    657 	SLIST_FOREACH(table_en, tbl, next)
    658 		dm_table_deps(table_en, cmd_array);
    659 
    660 	dm_table_release(&dmv->table_head, table_type);
    661 	dm_dev_unbusy(dmv);
    662 
    663 	prop_dictionary_set(dm_dict, DM_IOCTL_CMD_DATA, cmd_array);
    664 	prop_object_release(cmd_array);
    665 
    666 	return 0;
    667 }
    668 
    669 static int
    670 dm_table_deps(dm_table_entry_t *table_en, prop_array_t array)
    671 {
    672 	dm_mapping_t *map;
    673 	int i, size;
    674 	uint64_t rdev, tmp;
    675 
    676 	size = prop_array_count(array);
    677 
    678 	TAILQ_FOREACH(map, &table_en->pdev_maps, next) {
    679 		rdev = map->data.pdev->pdev_vnode->v_rdev;
    680 		for (i = 0; i < size; i++) {
    681 			if (prop_array_get_uint64(array, i, &tmp) == true)
    682 				if (rdev == tmp)
    683 					break; /* exists */
    684 		}
    685 		/*
    686 		 * Ignore if the device has already been added by
    687 		 * other tables.
    688 		 */
    689 		if (i == size) {
    690 			prop_array_add_uint64(array, rdev);
    691 			aprint_debug("%s: %d:%d\n", __func__, major(rdev),
    692 			    minor(rdev));
    693 		}
    694 	}
    695 
    696 	return 0;
    697 }
    698 
    699 /*
    700  * Load new table/tables to device.
    701  * Call appropriate target init routine to open all physical pdev's and
    702  * link them to device. For other targets mirror, strip, snapshot
    703  * etc. also add dependency devices to upcalls list.
    704  *
    705  * Load table to inactive slot table are switched in dm_device_resume_ioctl.
    706  * This simulates Linux behaviour better there should not be any difference.
    707  */
    708 int
    709 dm_table_load_ioctl(prop_dictionary_t dm_dict)
    710 {
    711 	dm_dev_t *dmv;
    712 	dm_table_entry_t *table_en, *last_table;
    713 	dm_table_t *tbl;
    714 	dm_target_t *target;
    715 
    716 	prop_object_iterator_t iter;
    717 	prop_array_t cmd_array;
    718 	prop_dictionary_t target_dict;
    719 
    720 	const char *name, *uuid, *type;
    721 	uint32_t flags, minor;
    722 
    723 	flags = 0;
    724 	name = NULL;
    725 	uuid = NULL;
    726 	last_table = NULL;
    727 
    728 	/*
    729 	 * char *xml; xml = prop_dictionary_externalize(dm_dict);
    730 	 * printf("%s\n",xml);
    731 	 */
    732 
    733 	prop_dictionary_get_string(dm_dict, DM_IOCTL_NAME, &name);
    734 	prop_dictionary_get_string(dm_dict, DM_IOCTL_UUID, &uuid);
    735 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
    736 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
    737 
    738 	cmd_array = prop_dictionary_get(dm_dict, DM_IOCTL_CMD_DATA);
    739 	iter = prop_array_iterator(cmd_array);
    740 	dm_dbg_print_flags(flags);
    741 
    742 	if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL) {
    743 		DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
    744 		prop_object_iterator_release(iter);
    745 		return ENOENT;
    746 	}
    747 	aprint_debug("Loading table to device: %s--%d\n", name,
    748 	    dmv->table_head.cur_active_table);
    749 
    750 	/*
    751 	 * I have to check if this table slot is not used by another table list.
    752 	 * if it is used I should free them.
    753 	 */
    754 	if (dmv->flags & DM_INACTIVE_PRESENT_FLAG)
    755 		dm_table_destroy(&dmv->table_head, DM_TABLE_INACTIVE);
    756 
    757 	dm_dbg_print_flags(dmv->flags);
    758 	tbl = dm_table_get_entry(&dmv->table_head, DM_TABLE_INACTIVE);
    759 
    760 	aprint_debug("dmv->name = %s\n", dmv->name);
    761 
    762 	prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
    763 
    764 	while ((target_dict = prop_object_iterator_next(iter)) != NULL) {
    765 		int ret;
    766 		const char *cp;
    767 		char *str;
    768 		size_t strsz;
    769 
    770 		prop_dictionary_get_string(target_dict,
    771 		    DM_TABLE_TYPE, &type);
    772 		/*
    773 		 * If we want to deny table with 2 or more different
    774 		 * target we should do it here
    775 		 */
    776 		if (((target = dm_target_lookup(type)) == NULL) &&
    777 		    ((target = dm_target_autoload(type)) == NULL)) {
    778 			dm_table_release(&dmv->table_head, DM_TABLE_INACTIVE);
    779 			dm_dev_unbusy(dmv);
    780 			prop_object_iterator_release(iter);
    781 			return ENOENT;
    782 		}
    783 		table_en = kmem_alloc(sizeof(dm_table_entry_t), KM_SLEEP);
    784 		prop_dictionary_get_uint64(target_dict, DM_TABLE_START,
    785 		    &table_en->start);
    786 		prop_dictionary_get_uint64(target_dict, DM_TABLE_LENGTH,
    787 		    &table_en->length);
    788 
    789 		table_en->target = target;
    790 		table_en->dm_dev = dmv;
    791 		table_en->target_config = NULL;
    792 		TAILQ_INIT(&table_en->pdev_maps);
    793 
    794 		/*
    795 		 * There is a parameter string after dm_target_spec
    796 		 * structure which  points to /dev/wd0a 284 part of
    797 		 * table. String str points to this text. This can be
    798 		 * null and therefore it should be checked before we try to
    799 		 * use it.
    800 		 */
    801 		cp = NULL;
    802 		prop_dictionary_get_string(target_dict,
    803 		    DM_TABLE_PARAMS, &cp);
    804 		if (cp == NULL)
    805 			str = NULL;
    806 		else
    807 			str = kmem_strdupsize(cp, &strsz, KM_SLEEP);
    808 
    809 		if (SLIST_EMPTY(tbl) || last_table == NULL)
    810 			/* insert this table to head */
    811 			SLIST_INSERT_HEAD(tbl, table_en, next);
    812 		else
    813 			SLIST_INSERT_AFTER(last_table, table_en, next);
    814 
    815 		if ((ret = dm_table_init(target, table_en, str)) != 0) {
    816 			dm_table_release(&dmv->table_head, DM_TABLE_INACTIVE);
    817 			dm_table_destroy(&dmv->table_head, DM_TABLE_INACTIVE);
    818 
    819 			if (str != NULL)
    820 				kmem_free(str, strsz);
    821 
    822 			dm_dev_unbusy(dmv);
    823 			prop_object_iterator_release(iter);
    824 			return ret;
    825 		}
    826 		last_table = table_en;
    827 		if (str != NULL)
    828 			kmem_free(str, strsz);
    829 	}
    830 	prop_object_iterator_release(iter);
    831 
    832 	DM_ADD_FLAG(flags, DM_INACTIVE_PRESENT_FLAG);
    833 	atomic_or_32(&dmv->flags, DM_INACTIVE_PRESENT_FLAG);
    834 
    835 	dm_table_release(&dmv->table_head, DM_TABLE_INACTIVE);
    836 	dm_dev_unbusy(dmv);
    837 	return 0;
    838 }
    839 
    840 static int
    841 dm_table_init(dm_target_t *target, dm_table_entry_t *table_en, char *params)
    842 {
    843 	int i, n, ret, argc;
    844 	char **ap, **argv;
    845 
    846 	if (params == NULL)
    847 		return EINVAL;
    848 
    849 	n = target->max_argc;
    850 	if (n)
    851 		aprint_debug("Max argc %d for %s target\n", n, target->name);
    852 	else
    853 		n = 32; /* large enough for most targets */
    854 
    855 	argv = kmem_alloc(sizeof(*argv) * n, KM_SLEEP);
    856 
    857 	for (ap = argv;
    858 	     ap < &argv[n] && (*ap = strsep(&params, " \t")) != NULL;) {
    859 		if (**ap != '\0')
    860 			ap++;
    861 	}
    862 	argc = ap - argv;
    863 
    864 	for (i = 0; i < argc; i++)
    865 		aprint_debug("DM: argv[%d] = \"%s\"\n", i, argv[i]);
    866 
    867 	KASSERT(target->init);
    868 	ret = target->init(table_en, argc, argv);
    869 
    870 	kmem_free(argv, sizeof(*argv) * n);
    871 
    872 	return ret;
    873 }
    874 
    875 /*
    876  * Get description of all tables loaded to device from kernel
    877  * and send it to libdevmapper.
    878  *
    879  * Output dictionary for every table:
    880  *
    881  * <key>cmd_data</key>
    882  * <array>
    883  *   <dict>
    884  *    <key>type<key>
    885  *    <string>...</string>
    886  *
    887  *    <key>start</key>
    888  *    <integer>...</integer>
    889  *
    890  *    <key>length</key>
    891  *    <integer>...</integer>
    892  *
    893  *    <key>params</key>
    894  *    <string>...</string>
    895  *   </dict>
    896  * </array>
    897  */
    898 int
    899 dm_table_status_ioctl(prop_dictionary_t dm_dict)
    900 {
    901 	dm_dev_t *dmv;
    902 	dm_table_t *tbl;
    903 	dm_table_entry_t *table_en;
    904 
    905 	prop_array_t cmd_array;
    906 	prop_dictionary_t target_dict;
    907 
    908 	uint32_t minor, flags;
    909 	const char *name, *uuid;
    910 	int table_type;
    911 
    912 	uuid = NULL;
    913 	name = NULL;
    914 	flags = 0;
    915 
    916 	prop_dictionary_get_string(dm_dict, DM_IOCTL_NAME, &name);
    917 	prop_dictionary_get_string(dm_dict, DM_IOCTL_UUID, &uuid);
    918 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
    919 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
    920 
    921 	cmd_array = prop_array_create();
    922 
    923 	if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL) {
    924 		DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
    925 		return ENOENT;
    926 	}
    927 	/*
    928 	 * if DM_QUERY_INACTIVE_TABLE_FLAG is passed we need to query
    929 	 * INACTIVE TABLE
    930 	 */
    931 	if (flags & DM_QUERY_INACTIVE_TABLE_FLAG)
    932 		table_type = DM_TABLE_INACTIVE;
    933 	else
    934 		table_type = DM_TABLE_ACTIVE;
    935 
    936 	if (dm_table_get_target_count(&dmv->table_head, DM_TABLE_ACTIVE)) {
    937 		DM_ADD_FLAG(flags, DM_ACTIVE_PRESENT_FLAG);
    938 	} else {
    939 		DM_REMOVE_FLAG(flags, DM_ACTIVE_PRESENT_FLAG);
    940 
    941 		if (dm_table_get_target_count(&dmv->table_head, DM_TABLE_INACTIVE))
    942 			DM_ADD_FLAG(flags, DM_INACTIVE_PRESENT_FLAG);
    943 		else
    944 			DM_REMOVE_FLAG(flags, DM_INACTIVE_PRESENT_FLAG);
    945 	}
    946 
    947 	if (dmv->flags & DM_SUSPEND_FLAG)
    948 		DM_ADD_FLAG(flags, DM_SUSPEND_FLAG);
    949 
    950 	prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
    951 
    952 	aprint_debug("Status of device tables: %s--%d\n",
    953 	    name, dmv->table_head.cur_active_table);
    954 
    955 	tbl = dm_table_get_entry(&dmv->table_head, table_type);
    956 
    957 	SLIST_FOREACH(table_en, tbl, next) {
    958 		char *params;
    959 		int is_table;
    960 
    961 		target_dict = prop_dictionary_create();
    962 		aprint_debug("%016" PRIu64 ", length %016" PRIu64
    963 		    ", target %s\n", table_en->start, table_en->length,
    964 		    table_en->target->name);
    965 
    966 		prop_dictionary_set_uint64(target_dict, DM_TABLE_START,
    967 		    table_en->start);
    968 		prop_dictionary_set_uint64(target_dict, DM_TABLE_LENGTH,
    969 		    table_en->length);
    970 
    971 		prop_dictionary_set_string(target_dict, DM_TABLE_TYPE,
    972 		    table_en->target->name);
    973 
    974 		/* dm_table_get_cur_actv.table ?? */
    975 		prop_dictionary_set_int32(target_dict, DM_TABLE_STAT,
    976 		    dmv->table_head.cur_active_table);
    977 
    978 		/*
    979 		 * Explicitly clear DM_TABLE_PARAMS to prevent dmsetup(8) from
    980 		 * printing junk when DM_TABLE_PARAMS was never initialized.
    981 		 */
    982 		prop_dictionary_set_string(target_dict, DM_TABLE_PARAMS, "");
    983 
    984 		is_table = (flags & DM_STATUS_TABLE_FLAG) ? 1 : 0;
    985 		if (is_table && table_en->target->table)
    986 			params = table_en->target->table(
    987 			    table_en->target_config);
    988 		else if (!is_table && table_en->target->info)
    989 			params = table_en->target->info(
    990 			    table_en->target_config);
    991 		else
    992 			params = NULL;
    993 
    994 		if (params != NULL) {
    995 			prop_dictionary_set_string(target_dict,
    996 			    DM_TABLE_PARAMS, params);
    997 			kmem_free(params, DM_MAX_PARAMS_SIZE);
    998 		}
    999 
   1000 		prop_array_add(cmd_array, target_dict);
   1001 		prop_object_release(target_dict);
   1002 	}
   1003 
   1004 	dm_table_release(&dmv->table_head, table_type);
   1005 	dm_dev_unbusy(dmv);
   1006 
   1007 	prop_dictionary_set_uint32(dm_dict, DM_IOCTL_FLAGS, flags);
   1008 	prop_dictionary_set(dm_dict, DM_IOCTL_CMD_DATA, cmd_array);
   1009 	prop_object_release(cmd_array);
   1010 
   1011 	return 0;
   1012 }
   1013 
   1014 /*
   1015  * For every call I have to set kernel driver version.
   1016  * Because I can have commands supported only in other
   1017  * newer/later version. This routine is called for every
   1018  * ioctl command.
   1019  */
   1020 int
   1021 dm_check_version(prop_dictionary_t dm_dict)
   1022 {
   1023 	int i;
   1024 	uint32_t dm_version[3];
   1025 	prop_array_t ver;
   1026 
   1027 	ver = prop_dictionary_get(dm_dict, DM_IOCTL_VERSION);
   1028 
   1029 	for (i = 0; i < 3; i++)
   1030 		prop_array_get_uint32(ver, i, &dm_version[i]);
   1031 
   1032 	if (DM_VERSION_MAJOR != dm_version[0] || DM_VERSION_MINOR < dm_version[1]) {
   1033 		aprint_debug("libdevmapper/kernel version mismatch "
   1034 		    "kernel: %d.%d.%d libdevmapper: %d.%d.%d\n",
   1035 		    DM_VERSION_MAJOR, DM_VERSION_MINOR, DM_VERSION_PATCHLEVEL,
   1036 		    dm_version[0], dm_version[1], dm_version[2]);
   1037 
   1038 		return EIO;
   1039 	}
   1040 	prop_array_set_uint32(ver, 0, DM_VERSION_MAJOR);
   1041 	prop_array_set_uint32(ver, 1, DM_VERSION_MINOR);
   1042 	prop_array_set_uint32(ver, 2, DM_VERSION_PATCHLEVEL);
   1043 
   1044 	prop_dictionary_set(dm_dict, DM_IOCTL_VERSION, ver);
   1045 
   1046 	return 0;
   1047 }
   1048