Home | History | Annotate | Line # | Download | only in dm
dm_ioctl.c revision 1.16
      1 /*        $NetBSD: dm_ioctl.c,v 1.16 2009/12/05 01:21:41 haad 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 extern const struct dkdriver dmdkdriver;
     97 uint64_t dev_counter;
     98 
     99 #define DM_REMOVE_FLAG(flag, name) do {					\
    100 		prop_dictionary_get_uint32(dm_dict,DM_IOCTL_FLAGS,&flag); \
    101 		flag &= ~name;						\
    102 		prop_dictionary_set_uint32(dm_dict,DM_IOCTL_FLAGS,flag); \
    103 	} while (/*CONSTCOND*/0)
    104 
    105 #define DM_ADD_FLAG(flag, name) do {					\
    106 		prop_dictionary_get_uint32(dm_dict,DM_IOCTL_FLAGS,&flag); \
    107 		flag |= name;						\
    108 		prop_dictionary_set_uint32(dm_dict,DM_IOCTL_FLAGS,flag); \
    109 	} while (/*CONSTCOND*/0)
    110 
    111 static int dm_dbg_print_flags(int);
    112 
    113 /*
    114  * Print flags sent to the kernel from libevmapper.
    115  */
    116 static int
    117 dm_dbg_print_flags(int flags)
    118 {
    119 	aprint_debug("dbg_print --- %d\n",flags);
    120 
    121 	if (flags & DM_READONLY_FLAG)
    122 		aprint_debug("dbg_flags: DM_READONLY_FLAG set In/Out\n");
    123 
    124 	if (flags & DM_SUSPEND_FLAG)
    125 		aprint_debug("dbg_flags: DM_SUSPEND_FLAG set In/Out \n");
    126 
    127 	if (flags & DM_PERSISTENT_DEV_FLAG)
    128 		aprint_debug("db_flags: DM_PERSISTENT_DEV_FLAG set In\n");
    129 
    130 	if (flags & DM_STATUS_TABLE_FLAG)
    131 		aprint_debug("dbg_flags: DM_STATUS_TABLE_FLAG set In\n");
    132 
    133 	if (flags & DM_ACTIVE_PRESENT_FLAG)
    134 		aprint_debug("dbg_flags: DM_ACTIVE_PRESENT_FLAG set Out\n");
    135 
    136 	if (flags & DM_INACTIVE_PRESENT_FLAG)
    137 		aprint_debug("dbg_flags: DM_INACTIVE_PRESENT_FLAG set Out\n");
    138 
    139 	if (flags & DM_BUFFER_FULL_FLAG)
    140 		aprint_debug("dbg_flags: DM_BUFFER_FULL_FLAG set Out\n");
    141 
    142 	if (flags & DM_SKIP_BDGET_FLAG)
    143 		aprint_debug("dbg_flags: DM_SKIP_BDGET_FLAG set In\n");
    144 
    145 	if (flags & DM_SKIP_LOCKFS_FLAG)
    146 		aprint_debug("dbg_flags: DM_SKIP_LOCKFS_FLAG set In\n");
    147 
    148 	if (flags & DM_NOFLUSH_FLAG)
    149 		aprint_debug("dbg_flags: DM_NOFLUSH_FLAG set In\n");
    150 
    151 	return 0;
    152 }
    153 
    154 /*
    155  * Get version ioctl call I do it as default therefore this
    156  * function is unused now.
    157  */
    158 int
    159 dm_get_version_ioctl(prop_dictionary_t dm_dict)
    160 {
    161 	return 0;
    162 }
    163 
    164 /*
    165  * Get list of all available targets from global
    166  * target list and sent them back to libdevmapper.
    167  */
    168 int
    169 dm_list_versions_ioctl(prop_dictionary_t dm_dict)
    170 {
    171 	prop_array_t target_list;
    172 	uint32_t flags;
    173 
    174 	flags = 0;
    175 
    176 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
    177 
    178 	dm_dbg_print_flags(flags);
    179 
    180 	target_list = dm_target_prop_list();
    181 
    182 	prop_dictionary_set(dm_dict, DM_IOCTL_CMD_DATA, target_list);
    183 
    184 	prop_object_release(target_list);
    185 
    186 	return 0;
    187 }
    188 
    189 /*
    190  * Create in-kernel entry for device. Device attributes such as name, uuid are
    191  * taken from proplib dictionary.
    192  *
    193  */
    194 int
    195 dm_dev_create_ioctl(prop_dictionary_t dm_dict)
    196 {
    197 	dm_dev_t *dmv;
    198 	const char *name, *uuid;
    199 	int r, flags;
    200 
    201 	r = 0;
    202 	flags = 0;
    203 	name = NULL;
    204 	uuid = NULL;
    205 
    206 	/* Get needed values from dictionary. */
    207 	prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
    208 	prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
    209 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
    210 
    211 	dm_dbg_print_flags(flags);
    212 
    213 	/* Lookup name and uuid if device already exist quit. */
    214 	if ((dmv = dm_dev_lookup(name, uuid, -1)) != NULL) {
    215 		DM_ADD_FLAG(flags, DM_EXISTS_FLAG); /* Device already exists */
    216 		dm_dev_unbusy(dmv);
    217 		return EEXIST;
    218 	}
    219 
    220 	if ((dmv = dm_dev_alloc()) == NULL)
    221 		return ENOMEM;
    222 
    223 	if (uuid)
    224 		strncpy(dmv->uuid, uuid, DM_UUID_LEN);
    225 	else
    226 		dmv->uuid[0] = '\0';
    227 
    228 	if (name)
    229 		strlcpy(dmv->name, name, DM_NAME_LEN);
    230 
    231 	dmv->minor = atomic_inc_64_nv(&sc_minor_num);
    232 
    233 	dmv->flags = 0; /* device flags are set when needed */
    234 	dmv->ref_cnt = 0;
    235 	dmv->event_nr = 0;
    236 	dmv->dev_type = 0;
    237 
    238 	mutex_init(&dmv->dev_mtx, MUTEX_DEFAULT, IPL_NONE);
    239 	cv_init(&dmv->dev_cv, "dm_dev");
    240 
    241 	dm_table_head_init(&dmv->table_head);
    242 
    243 	if (flags & DM_READONLY_FLAG)
    244 		dmv->flags |= DM_READONLY_FLAG;
    245 
    246 	prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
    247 
    248 	disk_init(dmv->diskp, dmv->name, &dmdkdriver);
    249 	disk_attach(dmv->diskp);
    250 
    251 	dmv->diskp->dk_info = NULL;
    252 
    253 	if ((r = dm_dev_insert(dmv)) != 0)
    254 		dm_dev_free(dmv);
    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 	/* Destroy disk device structure */
    400 	disk_detach(dmv->diskp);
    401 	disk_destroy(dmv->diskp);
    402 
    403 	/* Destroy device */
    404 	(void)dm_dev_free(dmv);
    405 
    406 	/* Decrement device counter After removing device */
    407 	atomic_dec_64(&dev_counter);
    408 
    409 	return 0;
    410 }
    411 
    412 /*
    413  * Return actual state of device to libdevmapper.
    414  */
    415 int
    416 dm_dev_status_ioctl(prop_dictionary_t dm_dict)
    417 {
    418 	dm_dev_t *dmv;
    419 	const char *name, *uuid;
    420 	uint32_t flags, j, minor;
    421 
    422 	name = NULL;
    423 	uuid = NULL;
    424 	flags = 0;
    425 	j = 0;
    426 
    427 	prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
    428 	prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
    429 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
    430 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
    431 
    432 	if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL) {
    433 		DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
    434 		return ENOENT;
    435 	}
    436 
    437 	dm_dbg_print_flags(dmv->flags);
    438 
    439 	prop_dictionary_set_uint32(dm_dict, DM_IOCTL_OPEN, dmv->table_head.io_cnt);
    440 	prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
    441 	prop_dictionary_set_cstring(dm_dict, DM_IOCTL_UUID, dmv->uuid);
    442 
    443 	if (dmv->flags & DM_SUSPEND_FLAG)
    444 		DM_ADD_FLAG(flags, DM_SUSPEND_FLAG);
    445 
    446 	/* Add status flags for tables I have to check both
    447 	   active and inactive tables. */
    448 	if ((j = dm_table_get_target_count(&dmv->table_head, DM_TABLE_ACTIVE))) {
    449 		DM_ADD_FLAG(flags, DM_ACTIVE_PRESENT_FLAG);
    450 	} else
    451 		DM_REMOVE_FLAG(flags, DM_ACTIVE_PRESENT_FLAG);
    452 
    453 	prop_dictionary_set_uint32(dm_dict, DM_IOCTL_TARGET_COUNT, j);
    454 
    455 	if (dm_table_get_target_count(&dmv->table_head, DM_TABLE_INACTIVE))
    456 		DM_ADD_FLAG(flags, DM_INACTIVE_PRESENT_FLAG);
    457 	else
    458 		DM_REMOVE_FLAG(flags, DM_INACTIVE_PRESENT_FLAG);
    459 
    460 	dm_dev_unbusy(dmv);
    461 
    462 	return 0;
    463 }
    464 
    465 /*
    466  * Set only flag to suggest that device is suspended. This call is
    467  * not supported in NetBSD.
    468  *
    469  */
    470 int
    471 dm_dev_suspend_ioctl(prop_dictionary_t dm_dict)
    472 {
    473 	dm_dev_t *dmv;
    474 	const char *name, *uuid;
    475 	uint32_t flags, minor;
    476 
    477 	name = NULL;
    478 	uuid = NULL;
    479 	flags = 0;
    480 
    481 	prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
    482 	prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
    483 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
    484 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
    485 
    486 	if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL){
    487 		DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
    488 		return ENOENT;
    489 	}
    490 
    491 	atomic_or_32(&dmv->flags, DM_SUSPEND_FLAG);
    492 
    493 	dm_dbg_print_flags(dmv->flags);
    494 
    495 	prop_dictionary_set_uint32(dm_dict, DM_IOCTL_OPEN, dmv->table_head.io_cnt);
    496 	prop_dictionary_set_uint32(dm_dict, DM_IOCTL_FLAGS, dmv->flags);
    497 	prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
    498 
    499 	dm_dev_unbusy(dmv);
    500 
    501 	/* Add flags to dictionary flag after dmv -> dict copy */
    502 	DM_ADD_FLAG(flags, DM_EXISTS_FLAG);
    503 
    504 	return 0;
    505 }
    506 
    507 /*
    508  * Simulate Linux behaviour better and switch tables here and not in
    509  * dm_table_load_ioctl.
    510  */
    511 int
    512 dm_dev_resume_ioctl(prop_dictionary_t dm_dict)
    513 {
    514 	dm_dev_t *dmv;
    515 	const char *name, *uuid;
    516 	uint32_t flags, minor;
    517 
    518 	name = NULL;
    519 	uuid = NULL;
    520 	flags = 0;
    521 
    522 /*	char *xml;
    523 	xml = prop_dictionary_externalize(dm_dict);
    524 	printf("%s\n",xml);*/
    525 
    526 	prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
    527 	prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
    528 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
    529 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
    530 
    531 	/* Remove device from global device list */
    532 	if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL){
    533 		DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
    534 		return ENOENT;
    535 	}
    536 
    537 	atomic_and_32(&dmv->flags, ~(DM_SUSPEND_FLAG | DM_INACTIVE_PRESENT_FLAG));
    538 	atomic_or_32(&dmv->flags, DM_ACTIVE_PRESENT_FLAG);
    539 
    540 	dm_table_switch_tables(&dmv->table_head);
    541 
    542 	DM_ADD_FLAG(flags, DM_EXISTS_FLAG);
    543 
    544 	dmgetproperties(dmv->diskp, &dmv->table_head);
    545 
    546 	prop_dictionary_set_uint32(dm_dict, DM_IOCTL_OPEN, dmv->table_head.io_cnt);
    547 	prop_dictionary_set_uint32(dm_dict, DM_IOCTL_FLAGS, flags);
    548 	prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
    549 
    550 	dm_dev_unbusy(dmv);
    551 
    552 	/* Destroy inactive table after resume. */
    553 	dm_table_destroy(&dmv->table_head, DM_TABLE_INACTIVE);
    554 
    555 	return 0;
    556 }
    557 
    558 /*
    559  * Table management routines
    560  * lvm2tools doens't send name/uuid to kernel with table
    561  * for lookup I have to use minor number.
    562  */
    563 
    564 /*
    565  * Remove inactive table from device. Routines which work's with inactive tables
    566  * doesn't need to synchronise with dmstrategy. They can synchronise themselves with mutex?.
    567  *
    568  */
    569 int
    570 dm_table_clear_ioctl(prop_dictionary_t dm_dict)
    571 {
    572 	dm_dev_t *dmv;
    573 	const char *name, *uuid;
    574 	uint32_t flags, minor;
    575 
    576 	dmv  = NULL;
    577 	name = NULL;
    578 	uuid = NULL;
    579 	flags = 0;
    580 	minor = 0;
    581 
    582 	prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
    583 	prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
    584 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
    585 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
    586 
    587 	aprint_debug("Clearing inactive table from device: %s--%s\n",
    588 	    name, uuid);
    589 
    590 	if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL){
    591 		DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
    592 		return ENOENT;
    593 	}
    594 
    595 	/* Select unused table */
    596 	dm_table_destroy(&dmv->table_head, DM_TABLE_INACTIVE);
    597 
    598 	atomic_and_32(&dmv->flags, ~DM_INACTIVE_PRESENT_FLAG);
    599 
    600 	dm_dev_unbusy(dmv);
    601 
    602 	return 0;
    603 }
    604 
    605 /*
    606  * Get list of physical devices for active table.
    607  * Get dev_t from pdev vnode and insert it into cmd_array.
    608  *
    609  * XXX. This function is called from lvm2tools to get information
    610  *      about physical devices, too e.g. during vgcreate.
    611  */
    612 int
    613 dm_table_deps_ioctl(prop_dictionary_t dm_dict)
    614 {
    615 	dm_dev_t *dmv;
    616 	dm_table_t *tbl;
    617 	dm_table_entry_t *table_en;
    618 
    619 	prop_array_t cmd_array;
    620 	const char *name, *uuid;
    621 	uint32_t flags, minor;
    622 
    623 	size_t i;
    624 
    625 	name = NULL;
    626 	uuid = NULL;
    627 	dmv  = NULL;
    628 	flags = 0;
    629 
    630 	i = 0;
    631 
    632 	prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
    633 	prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
    634 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
    635 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
    636 
    637 	/* create array for dev_t's */
    638 	cmd_array = prop_array_create();
    639 
    640 	if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL){
    641 		DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
    642 		return ENOENT;
    643 	}
    644 
    645 	prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
    646 	prop_dictionary_set_cstring(dm_dict, DM_IOCTL_NAME, dmv->name);
    647 	prop_dictionary_set_cstring(dm_dict, DM_IOCTL_UUID, dmv->uuid);
    648 
    649 	aprint_debug("Getting table deps for device: %s\n", dmv->name);
    650 
    651 	tbl = dm_table_get_entry(&dmv->table_head, DM_TABLE_ACTIVE);
    652 
    653 	SLIST_FOREACH(table_en, tbl, next)
    654        		table_en->target->deps(table_en, cmd_array);
    655 
    656 	dm_table_release(&dmv->table_head, DM_TABLE_ACTIVE);
    657 	dm_dev_unbusy(dmv);
    658 
    659 	prop_dictionary_set(dm_dict, DM_IOCTL_CMD_DATA, cmd_array);
    660 	prop_object_release(cmd_array);
    661 
    662 	return 0;
    663 }
    664 
    665 /*
    666  * Load new table/tables to device.
    667  * Call apropriate target init routine open all physical pdev's and
    668  * link them to device. For other targets mirror, strip, snapshot
    669  * etc. also add dependency devices to upcalls list.
    670  *
    671  * Load table to inactive slot table are switched in dm_device_resume_ioctl.
    672  * This simulates Linux behaviour better there should not be any difference.
    673  *
    674  */
    675 int
    676 dm_table_load_ioctl(prop_dictionary_t dm_dict)
    677 {
    678 	dm_dev_t *dmv;
    679 	dm_table_entry_t *table_en, *last_table;
    680 	dm_table_t  *tbl;
    681 	dm_target_t *target;
    682 
    683 	prop_object_iterator_t iter;
    684 	prop_array_t cmd_array;
    685 	prop_dictionary_t target_dict;
    686 
    687 	const char *name, *uuid, *type;
    688 
    689 	uint32_t flags, ret, minor;
    690 
    691 	char *str;
    692 
    693 	ret = 0;
    694 	flags = 0;
    695 	name = NULL;
    696 	uuid = NULL;
    697 	dmv = NULL;
    698 	last_table = NULL;
    699 	str = NULL;
    700 
    701 	/* char *xml;
    702 	xml = prop_dictionary_externalize(dm_dict);
    703 	printf("%s\n",xml);*/
    704 
    705 	prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
    706 	prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
    707 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
    708 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
    709 
    710 	cmd_array = prop_dictionary_get(dm_dict, DM_IOCTL_CMD_DATA);
    711 	iter = prop_array_iterator(cmd_array);
    712 	dm_dbg_print_flags(flags);
    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 	aprint_debug("Loading table to device: %s--%d\n", name,
    720 	    dmv->table_head.cur_active_table);
    721 
    722 	/*
    723 	 * I have to check if this table slot is not used by another table list.
    724 	 * if it is used I should free them.
    725 	 */
    726 	if (dmv->flags & DM_INACTIVE_PRESENT_FLAG)
    727 		dm_table_destroy(&dmv->table_head, DM_TABLE_INACTIVE);
    728 
    729 	dm_dbg_print_flags(dmv->flags);
    730 	tbl = dm_table_get_entry(&dmv->table_head, DM_TABLE_INACTIVE);
    731 
    732 	aprint_debug("dmv->name = %s\n", dmv->name);
    733 
    734 	prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
    735 
    736 	while((target_dict = prop_object_iterator_next(iter)) != NULL){
    737 
    738 		prop_dictionary_get_cstring_nocopy(target_dict,
    739 		    DM_TABLE_TYPE, &type);
    740 		/*
    741 		 * If we want to deny table with 2 or more different
    742 		 * target we should do it here
    743 		 */
    744 		if (((target = dm_target_lookup(type)) == NULL) &&
    745 		    ((target = dm_target_autoload(type)) == NULL)) {
    746 			dm_table_release(&dmv->table_head, DM_TABLE_INACTIVE);
    747 			dm_dev_unbusy(dmv);
    748 			return ENOENT;
    749 		}
    750 
    751 		if ((table_en = kmem_alloc(sizeof(dm_table_entry_t),
    752 			    KM_SLEEP)) == NULL) {
    753 			dm_table_release(&dmv->table_head, DM_TABLE_INACTIVE);
    754 			dm_dev_unbusy(dmv);
    755 			return ENOMEM;
    756 		}
    757 
    758 		prop_dictionary_get_uint64(target_dict, DM_TABLE_START,
    759 		    &table_en->start);
    760 		prop_dictionary_get_uint64(target_dict, DM_TABLE_LENGTH,
    761 		    &table_en->length);
    762 
    763 		table_en->target = target;
    764 		table_en->dm_dev = dmv;
    765 		table_en->target_config = NULL;
    766 
    767 		/*
    768 		 * There is a parameter string after dm_target_spec
    769 		 * structure which  points to /dev/wd0a 284 part of
    770 		 * table. String str points to this text. This can be
    771 		 * null and therefore it should be checked before we try to
    772 		 * use it.
    773 		 */
    774 		prop_dictionary_get_cstring(target_dict,
    775 		    DM_TABLE_PARAMS, (char**)&str);
    776 
    777 		if (SLIST_EMPTY(tbl))
    778 			/* insert this table to head */
    779 			SLIST_INSERT_HEAD(tbl, table_en, next);
    780 		else
    781 			SLIST_INSERT_AFTER(last_table, table_en, next);
    782 
    783 		/*
    784 		 * Params string is different for every target,
    785 		 * therfore I have to pass it to target init
    786 		 * routine and parse parameters there.
    787 		 */
    788 
    789 		if ((ret = target->init(dmv, &table_en->target_config,
    790 			    str)) != 0) {
    791 
    792 			dm_table_release(&dmv->table_head, DM_TABLE_INACTIVE);
    793 			dm_table_destroy(&dmv->table_head, DM_TABLE_INACTIVE);
    794 			free(str, M_TEMP);
    795 
    796 			dm_dev_unbusy(dmv);
    797 			return ret;
    798 		}
    799 		last_table = table_en;
    800 		free(str, M_TEMP);
    801 	}
    802 	prop_object_iterator_release(iter);
    803 
    804 	DM_ADD_FLAG(flags, DM_INACTIVE_PRESENT_FLAG);
    805 	atomic_or_32(&dmv->flags, DM_INACTIVE_PRESENT_FLAG);
    806 
    807 	dm_table_release(&dmv->table_head, DM_TABLE_INACTIVE);
    808 	dm_dev_unbusy(dmv);
    809 	return 0;
    810 }
    811 
    812 /*
    813  * Get description of all tables loaded to device from kernel
    814  * and send it to libdevmapper.
    815  *
    816  * Output dictionary for every table:
    817  *
    818  * <key>cmd_data</key>
    819  * <array>
    820  *   <dict>
    821  *    <key>type<key>
    822  *    <string>...</string>
    823  *
    824  *    <key>start</key>
    825  *    <integer>...</integer>
    826  *
    827  *    <key>length</key>
    828  *    <integer>...</integer>
    829  *
    830  *    <key>params</key>
    831  *    <string>...</string>
    832  *   </dict>
    833  * </array>
    834  *
    835  */
    836 int
    837 dm_table_status_ioctl(prop_dictionary_t dm_dict)
    838 {
    839 	dm_dev_t *dmv;
    840 	dm_table_t *tbl;
    841 	dm_table_entry_t *table_en;
    842 
    843 	prop_array_t cmd_array;
    844 	prop_dictionary_t target_dict;
    845 
    846 	uint32_t rec_size, minor;
    847 
    848 	const char *name, *uuid;
    849 	char *params;
    850 	int flags;
    851 
    852 	dmv = NULL;
    853 	uuid = NULL;
    854 	name = NULL;
    855 	params = NULL;
    856 	flags = 0;
    857 	rec_size = 0;
    858 
    859 	prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
    860 	prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
    861 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
    862 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
    863 
    864 	cmd_array = prop_array_create();
    865 
    866 	if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL){
    867 		DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
    868 		return ENOENT;
    869 	}
    870 
    871 	if (dm_table_get_target_count(&dmv->table_head, DM_TABLE_ACTIVE))
    872 		DM_ADD_FLAG(flags, DM_ACTIVE_PRESENT_FLAG);
    873 	else {
    874 		DM_REMOVE_FLAG(flags, DM_ACTIVE_PRESENT_FLAG);
    875 
    876 		if (dm_table_get_target_count(&dmv->table_head, DM_TABLE_INACTIVE))
    877 			DM_ADD_FLAG(flags, DM_INACTIVE_PRESENT_FLAG);
    878 		else {
    879 			DM_REMOVE_FLAG(flags, DM_INACTIVE_PRESENT_FLAG);
    880 		}
    881 	}
    882 
    883 	if (dmv->flags & DM_SUSPEND_FLAG)
    884 		DM_ADD_FLAG(flags, DM_SUSPEND_FLAG);
    885 
    886 	prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmv->minor);
    887 
    888 	/* I should use mutex here and not rwlock there can be IO operation
    889 	   during this ioctl on device. */
    890 
    891 	aprint_debug("Status of device tables: %s--%d\n",
    892 	    name, dmv->table_head.cur_active_table);
    893 
    894 	tbl = dm_table_get_entry(&dmv->table_head, DM_TABLE_ACTIVE);
    895 
    896 	SLIST_FOREACH(table_en, tbl, next)
    897 	{
    898 		target_dict = prop_dictionary_create();
    899 		aprint_debug("%016" PRIu64 ", length %016" PRIu64
    900 		    ", target %s\n", table_en->start, table_en->length,
    901 		    table_en->target->name);
    902 
    903 		prop_dictionary_set_uint64(target_dict, DM_TABLE_START,
    904 		    table_en->start);
    905 		prop_dictionary_set_uint64(target_dict, DM_TABLE_LENGTH,
    906 		    table_en->length);
    907 
    908 		prop_dictionary_set_cstring(target_dict, DM_TABLE_TYPE,
    909 		    table_en->target->name);
    910 
    911 		/* dm_table_get_cur_actv.table ?? */
    912 		prop_dictionary_set_int32(target_dict, DM_TABLE_STAT,
    913 		    dmv->table_head.cur_active_table);
    914 
    915 		if (flags |= DM_STATUS_TABLE_FLAG) {
    916 			params = table_en->target->status
    917 			    (table_en->target_config);
    918 
    919 			if(params != NULL){
    920 				prop_dictionary_set_cstring(target_dict,
    921 				    DM_TABLE_PARAMS, params);
    922 
    923 				kmem_free(params, DM_MAX_PARAMS_SIZE);
    924 			}
    925 		}
    926 
    927 		prop_array_add(cmd_array, target_dict);
    928 		prop_object_release(target_dict);
    929 	}
    930 
    931 	dm_table_release(&dmv->table_head, DM_TABLE_ACTIVE);
    932 	dm_dev_unbusy(dmv);
    933 
    934 	prop_dictionary_set(dm_dict, DM_IOCTL_CMD_DATA, cmd_array);
    935 	prop_object_release(cmd_array);
    936 
    937 	return 0;
    938 }
    939 
    940 
    941 /*
    942  * For every call I have to set kernel driver version.
    943  * Because I can have commands supported only in other
    944  * newer/later version. This routine is called for every
    945  * ioctl command.
    946  */
    947 int
    948 dm_check_version(prop_dictionary_t dm_dict)
    949 {
    950 	size_t i;
    951 	int dm_version[3];
    952 	prop_array_t ver;
    953 
    954 	ver = prop_dictionary_get(dm_dict, DM_IOCTL_VERSION);
    955 
    956 	for(i=0; i < 3; i++)
    957 		prop_array_get_uint32(ver, i, &dm_version[i]);
    958 
    959 	if (DM_VERSION_MAJOR != dm_version[0] || DM_VERSION_MINOR < dm_version[1]){
    960 		aprint_debug("libdevmapper/kernel version mismatch "
    961 		    "kernel: %d.%d.%d libdevmapper: %d.%d.%d\n",
    962 		    DM_VERSION_MAJOR, DM_VERSION_MINOR, DM_VERSION_PATCHLEVEL,
    963 		    dm_version[0], dm_version[1], dm_version[2]);
    964 
    965 		return EIO;
    966 	}
    967 
    968 	prop_array_set_uint32(ver, 0, DM_VERSION_MAJOR);
    969 	prop_array_set_uint32(ver, 1, DM_VERSION_MINOR);
    970 	prop_array_set_uint32(ver, 2, DM_VERSION_PATCHLEVEL);
    971 
    972 	return 0;
    973 }
    974