Home | History | Annotate | Line # | Download | only in dm
dm_ioctl.c revision 1.6.4.3
      1 /*        $NetBSD: dm_ioctl.c,v 1.6.4.3 2009/03/03 18:30:44 skrll 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 
     32 /*
     33  * Locking is used to synchronise between ioctl calls and between dm_table's
     34  * users.
     35  *
     36  * ioctl locking:
     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).
     41  *
     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
     44  * up them.
     45  *
     46  * table_head locking:
     47  * To access table entries dm_table_* routines must be used.
     48  *
     49  * dm_table_get_entry will increment table users reference
     50  * counter. It will return active or inactive table depedns
     51  * on uint8_t argument.
     52  *
     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
     55  * or destroyed.
     56  *
     57  * dm_table_head_init initialize talbe_entries SLISTS and io_cv.
     58  *
     59  * dm_table_head_destroy destroy cv.
     60  *
     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.
     66  *
     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
     71  *       operation.
     72  *
     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.
     78  *
     79  */
     80 
     81 #include <sys/types.h>
     82 #include <sys/param.h>
     83 
     84 #include <sys/disk.h>
     85 #include <sys/disklabel.h>
     86 #include <sys/kmem.h>
     87 #include <sys/malloc.h>
     88 #include <sys/vnode.h>
     89 
     90 #include <machine/int_fmtio.h>
     91 
     92 #include "netbsd-dm.h"
     93 #include "dm.h"
     94 
     95 static uint64_t      sc_minor_num;
     96 uint64_t dev_counter;
     97 
     98 #define DM_REMOVE_FLAG(flag, name) do {					\
     99 		prop_dictionary_get_uint32(dm_dict,DM_IOCTL_FLAGS,&flag); \
    100 		flag &= ~name;						\
    101 		prop_dictionary_set_uint32(dm_dict,DM_IOCTL_FLAGS,flag); \
    102 	} while (/*CONSTCOND*/0)
    103 
    104 #define DM_ADD_FLAG(flag, name) do {					\
    105 		prop_dictionary_get_uint32(dm_dict,DM_IOCTL_FLAGS,&flag); \
    106 		flag |= name;						\
    107 		prop_dictionary_set_uint32(dm_dict,DM_IOCTL_FLAGS,flag); \
    108 	} while (/*CONSTCOND*/0)
    109 
    110 static int dm_dbg_print_flags(int);
    111 
    112 /*
    113  * Print flags sent to the kernel from libevmapper.
    114  */
    115 static int
    116 dm_dbg_print_flags(int flags)
    117 {
    118 	aprint_debug("dbg_print --- %d\n",flags);
    119 
    120 	if (flags & DM_READONLY_FLAG)
    121 		aprint_debug("dbg_flags: DM_READONLY_FLAG set In/Out\n");
    122 
    123 	if (flags & DM_SUSPEND_FLAG)
    124 		aprint_debug("dbg_flags: DM_SUSPEND_FLAG set In/Out \n");
    125 
    126 	if (flags & DM_PERSISTENT_DEV_FLAG)
    127 		aprint_debug("db_flags: DM_PERSISTENT_DEV_FLAG set In\n");
    128 
    129 	if (flags & DM_STATUS_TABLE_FLAG)
    130 		aprint_debug("dbg_flags: DM_STATUS_TABLE_FLAG set In\n");
    131 
    132 	if (flags & DM_ACTIVE_PRESENT_FLAG)
    133 		aprint_debug("dbg_flags: DM_ACTIVE_PRESENT_FLAG set Out\n");
    134 
    135 	if (flags & DM_INACTIVE_PRESENT_FLAG)
    136 		aprint_debug("dbg_flags: DM_INACTIVE_PRESENT_FLAG set Out\n");
    137 
    138 	if (flags & DM_BUFFER_FULL_FLAG)
    139 		aprint_debug("dbg_flags: DM_BUFFER_FULL_FLAG set Out\n");
    140 
    141 	if (flags & DM_SKIP_BDGET_FLAG)
    142 		aprint_debug("dbg_flags: DM_SKIP_BDGET_FLAG set In\n");
    143 
    144 	if (flags & DM_SKIP_LOCKFS_FLAG)
    145 		aprint_debug("dbg_flags: DM_SKIP_LOCKFS_FLAG set In\n");
    146 
    147 	if (flags & DM_NOFLUSH_FLAG)
    148 		aprint_debug("dbg_flags: DM_NOFLUSH_FLAG set In\n");
    149 
    150 	return 0;
    151 }
    152 
    153 /*
    154  * Get version ioctl call I do it as default therefore this
    155  * function is unused now.
    156  */
    157 int
    158 dm_get_version_ioctl(prop_dictionary_t dm_dict)
    159 {
    160 	return 0;
    161 }
    162 
    163 /*
    164  * Get list of all available targets from global
    165  * target list and sent them back to libdevmapper.
    166  */
    167 int
    168 dm_list_versions_ioctl(prop_dictionary_t dm_dict)
    169 {
    170 	prop_array_t target_list;
    171 	uint32_t flags;
    172 
    173 	flags = 0;
    174 
    175 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
    176 
    177 	dm_dbg_print_flags(flags);
    178 
    179 	target_list = dm_target_prop_list();
    180 
    181 	prop_dictionary_set(dm_dict, DM_IOCTL_CMD_DATA, target_list);
    182 
    183 	prop_object_release(target_list);
    184 
    185 	return 0;
    186 }
    187 
    188 /*
    189  * Create in-kernel entry for device. Device attributes such as name, uuid are
    190  * taken from proplib dictionary.
    191  *
    192  */
    193 int
    194 dm_dev_create_ioctl(prop_dictionary_t dm_dict)
    195 {
    196 	dm_dev_t *dmv;
    197 	const char *name, *uuid;
    198 	int r, flags;
    199 
    200 	r = 0;
    201 	flags = 0;
    202 	name = NULL;
    203 	uuid = NULL;
    204 
    205 	/* Get needed values from dictionary. */
    206 	prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
    207 	prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
    208 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
    209 
    210 	dm_dbg_print_flags(flags);
    211 
    212 	/* Lookup name and uuid if device already exist quit. */
    213 	if ((dmv = dm_dev_lookup(name, uuid, -1)) != NULL) {
    214 		DM_ADD_FLAG(flags, DM_EXISTS_FLAG); /* Device already exists */
    215 		dm_dev_unbusy(dmv);
    216 		return EEXIST;
    217 	}
    218 
    219 	if ((dmv = dm_dev_alloc()) == NULL)
    220 		return ENOMEM;
    221 
    222 	if ((dmv->diskp = kmem_alloc(sizeof(struct disk), KM_NOSLEEP)) == NULL)
    223 		return ENOMEM;
    224 
    225 	if (uuid)
    226 		strncpy(dmv->uuid, uuid, DM_UUID_LEN);
    227 	else
    228 		dmv->uuid[0] = '\0';
    229 
    230 	if (name)
    231 		strlcpy(dmv->name, name, DM_NAME_LEN);
    232 
    233 	dmv->minor = atomic_inc_64_nv(&sc_minor_num);
    234 
    235 	dmv->flags = 0; /* device flags are set when needed */
    236 	dmv->ref_cnt = 0;
    237 	dmv->event_nr = 0;
    238 	dmv->dev_type = 0;
    239 
    240 	mutex_init(&dmv->dev_mtx, MUTEX_DEFAULT, IPL_NONE);
    241 	cv_init(&dmv->dev_cv, "dm_dev");
    242 
    243 	dm_table_head_init(&dmv->table_head);
    244 
    245 	if (flags & DM_READONLY_FLAG)
    246 		dmv->flags |= DM_READONLY_FLAG;
    247 
    248 	prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
    249 
    250 	disk_init(dmv->diskp, dmv->name, NULL);
    251 
    252 	if ((r = dm_dev_insert(dmv)) != 0){
    253 		dm_dev_free(dmv);
    254 	}
    255 
    256 	DM_ADD_FLAG(flags, DM_EXISTS_FLAG);
    257 	DM_REMOVE_FLAG(flags, DM_INACTIVE_PRESENT_FLAG);
    258 
    259 	/* Increment device counter After creating device */
    260 	atomic_inc_64(&dev_counter);
    261 
    262 	return r;
    263 }
    264 
    265 /*
    266  * Get list of created device-mapper devices fromglobal list and
    267  * send it to kernel.
    268  *
    269  * Output dictionary:
    270  *
    271  * <key>cmd_data</key>
    272  *  <array>
    273  *   <dict>
    274  *    <key>name<key>
    275  *    <string>...</string>
    276  *
    277  *    <key>dev</key>
    278  *    <integer>...</integer>
    279  *   </dict>
    280  *  </array>
    281  *
    282  */
    283 int
    284 dm_dev_list_ioctl(prop_dictionary_t dm_dict)
    285 {
    286 	prop_array_t dev_list;
    287 
    288 	uint32_t flags;
    289 
    290 	flags = 0;
    291 
    292 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
    293 
    294 	dm_dbg_print_flags(flags);
    295 
    296 	dev_list = dm_dev_prop_list();
    297 
    298 	prop_dictionary_set(dm_dict, DM_IOCTL_CMD_DATA, dev_list);
    299 	prop_object_release(dev_list);
    300 
    301 	return 0;
    302 }
    303 
    304 /*
    305  * Rename selected devices old name is in struct dm_ioctl.
    306  * newname is taken from dictionary
    307  *
    308  * <key>cmd_data</key>
    309  *  <array>
    310  *   <string>...</string>
    311  *  </array>
    312  */
    313 int
    314 dm_dev_rename_ioctl(prop_dictionary_t dm_dict)
    315 {
    316 	prop_array_t cmd_array;
    317 	dm_dev_t *dmv;
    318 
    319 	const char *name, *uuid, *n_name;
    320 	uint32_t flags, minor;
    321 
    322 	name = NULL;
    323 	uuid = NULL;
    324 	minor = 0;
    325 
    326 	/* Get needed values from dictionary. */
    327 	prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
    328 	prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
    329 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
    330 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
    331 
    332 	dm_dbg_print_flags(flags);
    333 
    334 	cmd_array = prop_dictionary_get(dm_dict, DM_IOCTL_CMD_DATA);
    335 
    336 	prop_array_get_cstring_nocopy(cmd_array, 0, &n_name);
    337 
    338 	if (strlen(n_name) + 1 > DM_NAME_LEN)
    339 		return EINVAL;
    340 
    341 	if ((dmv = dm_dev_rem(name, uuid, minor)) == NULL) {
    342 		DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
    343 		return ENOENT;
    344 	}
    345 
    346 	/* change device name */
    347 	/* XXX How to deal with this change, name only used in
    348 	 * dm_dev_routines, should I add dm_dev_change_name which will run under the
    349 	 * dm_dev_list mutex ?
    350 	 */
    351 	strlcpy(dmv->name, n_name, DM_NAME_LEN);
    352 
    353 	prop_dictionary_set_uint32(dm_dict, DM_IOCTL_OPEN, dmv->table_head.io_cnt);
    354 	prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
    355 	prop_dictionary_set_cstring(dm_dict, DM_IOCTL_UUID, dmv->uuid);
    356 
    357 	dm_dev_insert(dmv);
    358 
    359 	return 0;
    360 }
    361 
    362 /*
    363  * Remove device from global list I have to remove active
    364  * and inactive tables first.
    365  */
    366 int
    367 dm_dev_remove_ioctl(prop_dictionary_t dm_dict)
    368 {
    369 	dm_dev_t *dmv;
    370 	const char *name, *uuid;
    371 	uint32_t flags, minor;
    372 
    373 	flags = 0;
    374 	name = NULL;
    375 	uuid = NULL;
    376 
    377 	/* Get needed values from dictionary. */
    378 	prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
    379 	prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
    380 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
    381 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
    382 
    383 	dm_dbg_print_flags(flags);
    384 
    385 	/* Remove device from global device list */
    386 	if ((dmv = dm_dev_rem(name, uuid, minor)) == NULL){
    387 		DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
    388 		return ENOENT;
    389 	}
    390 
    391 	/* Destroy active table first.  */
    392 	dm_table_destroy(&dmv->table_head, DM_TABLE_ACTIVE);
    393 
    394 	/* Destroy inactive table if exits, too. */
    395 	dm_table_destroy(&dmv->table_head, DM_TABLE_INACTIVE);
    396 
    397 	dm_table_head_destroy(&dmv->table_head);
    398 
    399 	mutex_destroy(&dmv->dev_mtx);
    400 	cv_destroy(&dmv->dev_cv);
    401 
    402 	/* Destroy device */
    403 	(void)dm_dev_free(dmv);
    404 
    405 	/* Decrement device counter After removing device */
    406 	atomic_dec_64(&dev_counter);
    407 
    408 	return 0;
    409 }
    410 
    411 /*
    412  * Return actual state of device to libdevmapper.
    413  */
    414 int
    415 dm_dev_status_ioctl(prop_dictionary_t dm_dict)
    416 {
    417 	dm_dev_t *dmv;
    418 	const char *name, *uuid;
    419 	uint32_t flags, j, minor;
    420 
    421 	name = NULL;
    422 	uuid = NULL;
    423 	flags = 0;
    424 	j = 0;
    425 
    426 	prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
    427 	prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
    428 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
    429 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
    430 
    431 	if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL) {
    432 		DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
    433 		return ENOENT;
    434 	}
    435 
    436 	dm_dbg_print_flags(dmv->flags);
    437 
    438 	prop_dictionary_set_uint32(dm_dict, DM_IOCTL_OPEN, dmv->table_head.io_cnt);
    439 	prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
    440 	prop_dictionary_set_cstring(dm_dict, DM_IOCTL_UUID, dmv->uuid);
    441 
    442 	if (dmv->flags & DM_SUSPEND_FLAG)
    443 		DM_ADD_FLAG(flags, DM_SUSPEND_FLAG);
    444 
    445 	/* Add status flags for tables I have to check both
    446 	   active and inactive tables. */
    447 	if ((j = dm_table_get_target_count(&dmv->table_head, DM_TABLE_ACTIVE))) {
    448 		DM_ADD_FLAG(flags, DM_ACTIVE_PRESENT_FLAG);
    449 	} else
    450 		DM_REMOVE_FLAG(flags, DM_ACTIVE_PRESENT_FLAG);
    451 
    452 	prop_dictionary_set_uint32(dm_dict, DM_IOCTL_TARGET_COUNT, j);
    453 
    454 	if (dm_table_get_target_count(&dmv->table_head, DM_TABLE_INACTIVE))
    455 		DM_ADD_FLAG(flags, DM_INACTIVE_PRESENT_FLAG);
    456 	else
    457 		DM_REMOVE_FLAG(flags, DM_INACTIVE_PRESENT_FLAG);
    458 
    459 	dm_dev_unbusy(dmv);
    460 
    461 	return 0;
    462 }
    463 
    464 /*
    465  * Set only flag to suggest that device is suspended. This call is
    466  * not supported in NetBSD.
    467  *
    468  */
    469 int
    470 dm_dev_suspend_ioctl(prop_dictionary_t dm_dict)
    471 {
    472 	dm_dev_t *dmv;
    473 	const char *name, *uuid;
    474 	uint32_t flags, minor;
    475 
    476 	name = NULL;
    477 	uuid = NULL;
    478 	flags = 0;
    479 
    480 	prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
    481 	prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
    482 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
    483 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
    484 
    485 	if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL){
    486 		DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
    487 		return ENOENT;
    488 	}
    489 
    490 	atomic_or_32(&dmv->flags, DM_SUSPEND_FLAG);
    491 
    492 	dm_dbg_print_flags(dmv->flags);
    493 
    494 	prop_dictionary_set_uint32(dm_dict, DM_IOCTL_OPEN, dmv->table_head.io_cnt);
    495 	prop_dictionary_set_uint32(dm_dict, DM_IOCTL_FLAGS, dmv->flags);
    496 	prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
    497 
    498 	dm_dev_unbusy(dmv);
    499 
    500 	/* Add flags to dictionary flag after dmv -> dict copy */
    501 	DM_ADD_FLAG(flags, DM_EXISTS_FLAG);
    502 
    503 	return 0;
    504 }
    505 
    506 /*
    507  * Simulate Linux behaviour better and switch tables here and not in
    508  * dm_table_load_ioctl.
    509  */
    510 int
    511 dm_dev_resume_ioctl(prop_dictionary_t dm_dict)
    512 {
    513 	dm_dev_t *dmv;
    514 	const char *name, *uuid;
    515 	uint32_t flags, minor;
    516 
    517 	name = NULL;
    518 	uuid = NULL;
    519 	flags = 0;
    520 
    521 /*	char *xml;
    522 	xml = prop_dictionary_externalize(dm_dict);
    523 	printf("%s\n",xml);*/
    524 
    525 	prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
    526 	prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
    527 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
    528 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
    529 
    530 	/* Remove device from global device list */
    531 	if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL){
    532 		DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
    533 		return ENOENT;
    534 	}
    535 
    536 	atomic_and_32(&dmv->flags, ~(DM_SUSPEND_FLAG | DM_INACTIVE_PRESENT_FLAG));
    537 	atomic_or_32(&dmv->flags, DM_ACTIVE_PRESENT_FLAG);
    538 
    539 
    540 	dm_table_switch_tables(&dmv->table_head);
    541 
    542 	DM_ADD_FLAG(flags, DM_EXISTS_FLAG);
    543 
    544 	dmgetdisklabel(dmv->dk_label, &dmv->table_head);
    545 
    546 	disk_attach(dmv->diskp);
    547 	dmgetdisklabel(dmv->diskp->dk_label, &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, dmv->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 doens'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  */
    572 int
    573 dm_table_clear_ioctl(prop_dictionary_t dm_dict)
    574 {
    575 	dm_dev_t *dmv;
    576 	const char *name, *uuid;
    577 	uint32_t flags, minor;
    578 
    579 	dmv  = NULL;
    580 	name = NULL;
    581 	uuid = NULL;
    582 	flags = 0;
    583 	minor = 0;
    584 
    585 	prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
    586 	prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
    587 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
    588 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
    589 
    590 	aprint_debug("Clearing inactive table from device: %s--%s\n",
    591 	    name, uuid);
    592 
    593 	if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL){
    594 		DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
    595 		return ENOENT;
    596 	}
    597 
    598 	/* Select unused table */
    599 	dm_table_destroy(&dmv->table_head, DM_TABLE_INACTIVE);
    600 
    601 	atomic_and_32(&dmv->flags, ~DM_INACTIVE_PRESENT_FLAG);
    602 
    603 	dm_dev_unbusy(dmv);
    604 
    605 	return 0;
    606 }
    607 
    608 /*
    609  * Get list of physical devices for active table.
    610  * Get dev_t from pdev vnode and insert it into cmd_array.
    611  *
    612  * XXX. This function is called from lvm2tools to get information
    613  *      about physical devices, too e.g. during vgcreate.
    614  */
    615 int
    616 dm_table_deps_ioctl(prop_dictionary_t dm_dict)
    617 {
    618 	dm_dev_t *dmv;
    619 	dm_table_t *tbl;
    620 	dm_table_entry_t *table_en;
    621 
    622 	prop_array_t cmd_array;
    623 	const char *name, *uuid;
    624 	uint32_t flags, minor;
    625 
    626 	size_t i;
    627 
    628 	name = NULL;
    629 	uuid = NULL;
    630 	dmv  = NULL;
    631 	flags = 0;
    632 
    633 	i = 0;
    634 
    635 	prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
    636 	prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
    637 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
    638 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
    639 
    640 	/* create array for dev_t's */
    641 	cmd_array = prop_array_create();
    642 
    643 	if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL){
    644 		DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
    645 		return ENOENT;
    646 	}
    647 
    648 	prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
    649 	prop_dictionary_set_cstring(dm_dict, DM_IOCTL_NAME, dmv->name);
    650 	prop_dictionary_set_cstring(dm_dict, DM_IOCTL_UUID, dmv->uuid);
    651 
    652 	aprint_debug("Getting table deps for device: %s\n", dmv->name);
    653 
    654 	tbl = dm_table_get_entry(&dmv->table_head, DM_TABLE_ACTIVE);
    655 
    656 	SLIST_FOREACH(table_en, tbl, next)
    657        		table_en->target->deps(table_en, cmd_array);
    658 
    659 	dm_table_release(&dmv->table_head, DM_TABLE_ACTIVE);
    660 	dm_dev_unbusy(dmv);
    661 
    662 	prop_dictionary_set(dm_dict, DM_IOCTL_CMD_DATA, cmd_array);
    663 	prop_object_release(cmd_array);
    664 
    665 	return 0;
    666 }
    667 
    668 /*
    669  * Load new table/tables to device.
    670  * Call apropriate target init routine open all physical pdev's and
    671  * link them to device. For other targets mirror, strip, snapshot
    672  * etc. also add dependency devices to upcalls list.
    673  *
    674  * Load table to inactive slot table are switched in dm_device_resume_ioctl.
    675  * This simulates Linux behaviour better there should not be any difference.
    676  *
    677  */
    678 int
    679 dm_table_load_ioctl(prop_dictionary_t dm_dict)
    680 {
    681 	dm_dev_t *dmv;
    682 	dm_table_entry_t *table_en, *last_table;
    683 	dm_table_t  *tbl;
    684 	dm_target_t *target;
    685 
    686 	prop_object_iterator_t iter;
    687 	prop_array_t cmd_array;
    688 	prop_dictionary_t target_dict;
    689 
    690 	const char *name, *uuid, *type;
    691 
    692 	uint32_t flags, ret, minor;
    693 
    694 	char *str;
    695 
    696 	ret = 0;
    697 	flags = 0;
    698 	name = NULL;
    699 	uuid = NULL;
    700 	dmv = NULL;
    701 	last_table = NULL;
    702 	str = NULL;
    703 
    704 /*	char *xml;
    705 	xml = prop_dictionary_externalize(dm_dict);
    706 	printf("%s\n",xml);*/
    707 
    708 	prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
    709 	prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
    710 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
    711 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
    712 
    713 	cmd_array = prop_dictionary_get(dm_dict, DM_IOCTL_CMD_DATA);
    714 	iter = prop_array_iterator(cmd_array);
    715 	dm_dbg_print_flags(flags);
    716 
    717 	if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL){
    718 		DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
    719 		return ENOENT;
    720 	}
    721 
    722 	aprint_debug("Loading table to device: %s--%d\n", name,
    723 	    dmv->table_head.cur_active_table);
    724 
    725 	/*
    726 	 * I have to check if this table slot is not used by another table list.
    727 	 * if it is used I should free them.
    728 	 */
    729 	if (dmv->flags & DM_INACTIVE_PRESENT_FLAG)
    730 		dm_table_destroy(&dmv->table_head, DM_TABLE_INACTIVE);
    731 
    732 	dm_dbg_print_flags(dmv->flags);
    733 	tbl = dm_table_get_entry(&dmv->table_head, DM_TABLE_INACTIVE);
    734 
    735 	aprint_debug("dmv->name = %s\n", dmv->name);
    736 
    737 	prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
    738 
    739 	while((target_dict = prop_object_iterator_next(iter)) != NULL){
    740 
    741 		prop_dictionary_get_cstring_nocopy(target_dict,
    742 		    DM_TABLE_TYPE, &type);
    743 		/*
    744 		 * If we want to deny table with 2 or more different
    745 		 * target we should do it here
    746 		 */
    747 		if (((target = dm_target_lookup(type)) == NULL) &&
    748 		    ((target = dm_target_autoload(type)) == NULL)) {
    749 			dm_table_release(&dmv->table_head, DM_TABLE_INACTIVE);
    750 			dm_dev_unbusy(dmv);
    751 			return ENOENT;
    752 		}
    753 
    754 		if ((table_en = kmem_alloc(sizeof(dm_table_entry_t),
    755 			    KM_NOSLEEP)) == NULL) {
    756 			dm_table_release(&dmv->table_head, DM_TABLE_INACTIVE);
    757 			dm_dev_unbusy(dmv);
    758 			return ENOMEM;
    759 		}
    760 
    761 		prop_dictionary_get_uint64(target_dict, DM_TABLE_START,
    762 		    &table_en->start);
    763 		prop_dictionary_get_uint64(target_dict, DM_TABLE_LENGTH,
    764 		    &table_en->length);
    765 
    766 		table_en->target = target;
    767 		table_en->dm_dev = dmv;
    768 		table_en->target_config = NULL;
    769 
    770 		/*
    771 		 * There is a parameter string after dm_target_spec
    772 		 * structure which  points to /dev/wd0a 284 part of
    773 		 * table. String str points to this text. This can be
    774 		 * null and therefore it should be checked before we try to
    775 		 * use it.
    776 		 */
    777 		prop_dictionary_get_cstring(target_dict,
    778 		    DM_TABLE_PARAMS, (char**)&str);
    779 
    780 		if (SLIST_EMPTY(tbl))
    781 			/* insert this table to head */
    782 			SLIST_INSERT_HEAD(tbl, table_en, next);
    783 		else
    784 			SLIST_INSERT_AFTER(last_table, table_en, next);
    785 
    786 		/*
    787 		 * Params string is different for every target,
    788 		 * therfore I have to pass it to target init
    789 		 * routine and parse parameters there.
    790 		 */
    791 
    792 		if ((ret = target->init(dmv, &table_en->target_config,
    793 			    str)) != 0) {
    794 
    795 			dm_table_release(&dmv->table_head, DM_TABLE_INACTIVE);
    796 			dm_table_destroy(&dmv->table_head, DM_TABLE_INACTIVE);
    797 			free(str, M_TEMP);
    798 
    799 			dm_dev_unbusy(dmv);
    800 			return ret;
    801 		}
    802 		last_table = table_en;
    803 		free(str, M_TEMP);
    804 	}
    805 	prop_object_iterator_release(iter);
    806 
    807 	DM_ADD_FLAG(flags, DM_INACTIVE_PRESENT_FLAG);
    808 	atomic_or_32(&dmv->flags, DM_INACTIVE_PRESENT_FLAG);
    809 
    810 	dm_table_release(&dmv->table_head, DM_TABLE_INACTIVE);
    811 	dm_dev_unbusy(dmv);
    812 	return 0;
    813 }
    814 
    815 /*
    816  * Get description of all tables loaded to device from kernel
    817  * and send it to libdevmapper.
    818  *
    819  * Output dictionary for every table:
    820  *
    821  * <key>cmd_data</key>
    822  * <array>
    823  *   <dict>
    824  *    <key>type<key>
    825  *    <string>...</string>
    826  *
    827  *    <key>start</key>
    828  *    <integer>...</integer>
    829  *
    830  *    <key>length</key>
    831  *    <integer>...</integer>
    832  *
    833  *    <key>params</key>
    834  *    <string>...</string>
    835  *   </dict>
    836  * </array>
    837  *
    838  */
    839 int
    840 dm_table_status_ioctl(prop_dictionary_t dm_dict)
    841 {
    842 	dm_dev_t *dmv;
    843 	dm_table_t *tbl;
    844 	dm_table_entry_t *table_en;
    845 
    846 	prop_array_t cmd_array;
    847 	prop_dictionary_t target_dict;
    848 
    849 	uint32_t rec_size, minor;
    850 
    851 	const char *name, *uuid;
    852 	char *params;
    853 	int flags;
    854 
    855 	dmv = NULL;
    856 	uuid = NULL;
    857 	name = NULL;
    858 	params = NULL;
    859 	flags = 0;
    860 	rec_size = 0;
    861 
    862 	prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
    863 	prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
    864 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
    865 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
    866 
    867 	cmd_array = prop_array_create();
    868 
    869 	if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL){
    870 		DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
    871 		return ENOENT;
    872 	}
    873 
    874 	if (dm_table_get_target_count(&dmv->table_head, DM_TABLE_ACTIVE))
    875 		DM_ADD_FLAG(flags, DM_ACTIVE_PRESENT_FLAG);
    876 	else {
    877 		DM_REMOVE_FLAG(flags, DM_ACTIVE_PRESENT_FLAG);
    878 
    879 		if (dm_table_get_target_count(&dmv->table_head, DM_TABLE_INACTIVE))
    880 			DM_ADD_FLAG(flags, DM_INACTIVE_PRESENT_FLAG);
    881 		else {
    882 			DM_REMOVE_FLAG(flags, DM_INACTIVE_PRESENT_FLAG);
    883 		}
    884 	}
    885 
    886 	if (dmv->flags & DM_SUSPEND_FLAG)
    887 		DM_ADD_FLAG(flags, DM_SUSPEND_FLAG);
    888 
    889 	prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
    890 
    891 	/* I should use mutex here and not rwlock there can be IO operation
    892 	   during this ioctl on device. */
    893 
    894 	aprint_debug("Status of device tables: %s--%d\n",
    895 	    name, dmv->table_head.cur_active_table);
    896 
    897 	tbl = dm_table_get_entry(&dmv->table_head, DM_TABLE_ACTIVE);
    898 
    899 	SLIST_FOREACH(table_en, tbl, next)
    900 	{
    901 		target_dict = prop_dictionary_create();
    902 		aprint_debug("%016" PRIu64 ", length %016" PRIu64
    903 		    ", target %s\n", table_en->start, table_en->length,
    904 		    table_en->target->name);
    905 
    906 		prop_dictionary_set_uint64(target_dict, DM_TABLE_START,
    907 		    table_en->start);
    908 		prop_dictionary_set_uint64(target_dict, DM_TABLE_LENGTH,
    909 		    table_en->length);
    910 
    911 		prop_dictionary_set_cstring(target_dict, DM_TABLE_TYPE,
    912 		    table_en->target->name);
    913 
    914 		/* dm_table_get_cur_actv.table ?? */
    915 		prop_dictionary_set_int32(target_dict, DM_TABLE_STAT,
    916 		    dmv->table_head.cur_active_table);
    917 
    918 		if (flags |= DM_STATUS_TABLE_FLAG) {
    919 			params = table_en->target->status
    920 			    (table_en->target_config);
    921 
    922 			if(params != NULL){
    923 				prop_dictionary_set_cstring(target_dict,
    924 				    DM_TABLE_PARAMS, params);
    925 
    926 				kmem_free(params, strlen(params) + 1);
    927 			}
    928 		}
    929 
    930 		prop_array_add(cmd_array, target_dict);
    931 		prop_object_release(target_dict);
    932 	}
    933 
    934 	dm_table_release(&dmv->table_head, DM_TABLE_ACTIVE);
    935 	dm_dev_unbusy(dmv);
    936 
    937 	prop_dictionary_set(dm_dict, DM_IOCTL_CMD_DATA, cmd_array);
    938 	prop_object_release(cmd_array);
    939 
    940 	return 0;
    941 }
    942 
    943 
    944 /*
    945  * For every call I have to set kernel driver version.
    946  * Because I can have commands supported only in other
    947  * newer/later version. This routine is called for every
    948  * ioctl command.
    949  */
    950 int
    951 dm_check_version(prop_dictionary_t dm_dict)
    952 {
    953 	size_t i;
    954 	int dm_version[3];
    955 	prop_array_t ver;
    956 
    957 	ver = prop_dictionary_get(dm_dict, DM_IOCTL_VERSION);
    958 
    959 	for(i=0; i < 3; i++)
    960 		prop_array_get_uint32(ver, i, &dm_version[i]);
    961 
    962 	if (DM_VERSION_MAJOR != dm_version[0] || DM_VERSION_MINOR < dm_version[1]){
    963 		aprint_debug("libdevmapper/kernel version mismatch "
    964 		    "kernel: %d.%d.%d libdevmapper: %d.%d.%d\n",
    965 		    DM_VERSION_MAJOR, DM_VERSION_MINOR, DM_VERSION_PATCHLEVEL,
    966 		    dm_version[0], dm_version[1], dm_version[2]);
    967 
    968 		return EIO;
    969 	}
    970 
    971 	prop_array_set_uint32(ver, 0, DM_VERSION_MAJOR);
    972 	prop_array_set_uint32(ver, 1, DM_VERSION_MINOR);
    973 	prop_array_set_uint32(ver, 2, DM_VERSION_PATCHLEVEL);
    974 
    975 	return 0;
    976 }
    977