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