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