Home | History | Annotate | Line # | Download | only in dm
dm_ioctl.c revision 1.1.2.10
      1 /*        $NetBSD: dm_ioctl.c,v 1.1.2.10 2008/08/20 16:01:37 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 		while (!SLIST_EMPTY(&dmv->pdevs)) {
    392 
    393 			dmp = SLIST_FIRST(&dmv->pdevs);
    394 
    395 			SLIST_REMOVE_HEAD(&dmv->pdevs, next_pdev);
    396 
    397 			dm_pdev_decr(dmp);
    398 		}
    399 
    400 		/* Test readonly flag change anything only if it is not set*/
    401 
    402 		dm_dev_rem(name); /* XXX. try to use uuid, too */
    403 
    404 		rw_exit(&dmv->dev_rwlock);
    405 	}
    406 
    407 	return 0;
    408 }
    409 
    410 /*
    411  * Return actual state of device to libdevmapper.
    412  *
    413  * Locking: dev_mutex ??
    414  */
    415 int
    416 dm_dev_status_ioctl(prop_dictionary_t dm_dict)
    417 {
    418 	struct dm_table  *tbl;
    419 	struct dm_dev *dmv;
    420 	const char *name, *uuid;
    421 	uint32_t flags;
    422 
    423 	name = NULL;
    424 	uuid = NULL;
    425 	flags = 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 
    431 	if (((dmv = dm_dev_lookup_name(name)) == NULL) &&
    432 	    ((dmv = dm_dev_lookup_uuid(uuid)) == NULL)) {
    433 		DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
    434 		return 0;
    435 	}
    436 
    437 	prop_dictionary_set_uint32(dm_dict, DM_IOCTL_OPEN, dmv->ref_cnt);
    438 	prop_dictionary_set_uint32(dm_dict, DM_IOCTL_FLAGS, dmv->flags);
    439 
    440 	prop_dictionary_set_uint64(dm_dict, DM_IOCTL_DEV, dmv->minor);
    441 
    442 	tbl = &dmv->tables[dmv->cur_active_table];
    443 
    444 	if (tbl != NULL)
    445 		DM_ADD_FLAG(flags, DM_ACTIVE_PRESENT_FLAG);
    446 	else
    447 		DM_REMOVE_FLAG(flags, DM_ACTIVE_PRESENT_FLAG);
    448 
    449 	return 0;
    450 }
    451 
    452 /*
    453  * Set only flag to suggest that device is suspended. This call is
    454  * not supported in NetBSD.
    455  *
    456  * Locking: null
    457  */
    458 int
    459 dm_dev_suspend_ioctl(prop_dictionary_t dm_dict)
    460 {
    461 	struct dm_dev *dmv;
    462 	const char *name, *uuid;
    463 	uint32_t flags;
    464 
    465 	name = NULL;
    466 	uuid = NULL;
    467 	flags = 0;
    468 
    469 	prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
    470 	prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
    471 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
    472 
    473 	if (((dmv = dm_dev_lookup_name(name)) == NULL) &&
    474 	    ((dmv = dm_dev_lookup_uuid(uuid)) == NULL)) {
    475 		DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
    476 		return 0;
    477 	}
    478 
    479 	dmv->flags |= DM_SUSPEND_FLAG;
    480 
    481 	prop_dictionary_set_uint32(dm_dict, DM_IOCTL_OPEN, dmv->ref_cnt);
    482 	prop_dictionary_set_uint32(dm_dict, DM_IOCTL_FLAGS, dmv->flags);
    483 
    484 	prop_dictionary_set_uint64(dm_dict, DM_IOCTL_DEV, dmv->minor);
    485 
    486 	return 0;
    487 }
    488 
    489 /*
    490  * Simulate Linux behaviour better and switch tables here and not in
    491  * dm_table_load_ioctl.
    492  *
    493  * Locking: dev_mutex ??, dev_rwlock
    494  */
    495 int
    496 dm_dev_resume_ioctl(prop_dictionary_t dm_dict)
    497 {
    498 	struct dm_dev *dmv;
    499 	const char *name, *uuid;
    500 	uint32_t flags;
    501 
    502 	name = NULL;
    503 	uuid = NULL;
    504 	flags = 0;
    505 
    506 	prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
    507 	prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
    508 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
    509 
    510 	if (((dmv = dm_dev_lookup_name(name)) == NULL) &&
    511 	    ((dmv = dm_dev_lookup_uuid(uuid)) == NULL)) {
    512 		DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
    513 		return 0;
    514 	}
    515 
    516 	rw_enter(&dmv->dev_rwlock, RW_WRITER);
    517 
    518 	dmv->flags &= ~DM_SUSPEND_FLAG;
    519 
    520 	prop_dictionary_set_uint32(dm_dict, DM_IOCTL_OPEN, dmv->ref_cnt);
    521 	prop_dictionary_set_uint32(dm_dict, DM_IOCTL_FLAGS, dmv->flags);
    522 
    523 	prop_dictionary_set_uint64(dm_dict, DM_IOCTL_DEV, dmv->minor);
    524 
    525 	dmv->cur_active_table = 1 - dmv->cur_active_table;
    526 
    527 	rw_exit(&dmv->dev_rwlock);
    528 
    529 	return 0;
    530 }
    531 
    532 /*
    533  * Table management routines
    534  * lvm2tools doens't send name/uuid to kernel with table
    535  * for lookup I have to use minor number.
    536  */
    537 
    538 
    539 /*
    540  * Remove inactive table from device. Routines which work's with inactive tables
    541  * doesn't need to held write rw_lock. They can synchronise themselves with mutex?.
    542  *
    543  * Locking: dev_mutex
    544  */
    545 int
    546 dm_table_clear_ioctl(prop_dictionary_t dm_dict)
    547 {
    548 	struct dm_dev *dmv;
    549 	struct dm_table  *tbl;
    550 
    551 	const char *name, *uuid;
    552 	int flags;
    553 
    554 	dmv  = NULL;
    555 	name = NULL;
    556 	uuid = NULL;
    557 
    558 	prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
    559 	prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
    560 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
    561 
    562 	aprint_verbose("Clearing inactive table from device: %s--%s\n",
    563 	    name, uuid);
    564 
    565 	if (((dmv = dm_dev_lookup_name(name)) == NULL) &&
    566 	    ((dmv = dm_dev_lookup_uuid(uuid)) == NULL)) {
    567 		DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
    568 		return ENOENT;
    569 	}
    570 
    571 	/* Select unused table */
    572 	tbl = &dmv->tables[1-dmv->cur_active_table];
    573 
    574 	/* Test readonly flag change anything only if it is not set*/
    575 	if (!(flags & DM_READONLY_FLAG))
    576 			dm_table_destroy(tbl);
    577 
    578 	dmv->dev_type = 0;
    579 
    580 	return 0;
    581 }
    582 
    583 /*
    584  * Get list of physical devices for active table.
    585  * Get dev_t from pdev vnode and insert it into cmd_array.
    586  *
    587  * XXX. This function is called from lvm2tools to get information
    588  *      about physical devices, too e.g. during vgcreate.
    589  *
    590  * Locking: dev_mutex ??, dev_rwlock
    591  */
    592 int
    593 dm_table_deps_ioctl(prop_dictionary_t dm_dict)
    594 {
    595 	int error;
    596 	struct dm_dev *dmv;
    597 	struct dm_pdev *dmp;
    598 	struct vattr va;
    599 
    600 	prop_array_t cmd_array;
    601 
    602 	const char *name, *uuid;
    603 	int flags;
    604 	uint64_t dev;
    605 
    606 	size_t i;
    607 
    608 	name = NULL;
    609 	uuid = NULL;
    610 	dmv  = NULL;
    611 	flags = 0;
    612 
    613 	i=0;
    614 
    615 	prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
    616 	prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
    617 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
    618 	prop_dictionary_get_uint64(dm_dict, DM_IOCTL_DEV, &dev);
    619 
    620 	cmd_array = prop_dictionary_get(dm_dict,DM_IOCTL_CMD_DATA);
    621 
    622 	if (((dmv = dm_dev_lookup_name(name)) == NULL) &&
    623 	    ((dmv = dm_dev_lookup_uuid(uuid)) == NULL) &&
    624 	    ((dmv = dm_dev_lookup_minor(minor(dev))) == NULL)) {
    625 		DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
    626 		return ENOENT;
    627 	}
    628 
    629 	aprint_verbose("Getting table deps for device: %s\n", dmv->name);
    630 
    631 	/* XXX DO I really need to write lock it here */
    632 	rw_enter(&dmv->dev_rwlock, RW_WRITER);
    633 
    634 	SLIST_FOREACH(dmp, &dmv->pdevs, next_pdev){
    635 
    636 		if ((error = VOP_GETATTR(dmp->pdev_vnode, &va, curlwp->l_cred))
    637 		    != 0)
    638 			return (error);
    639 
    640 		prop_array_set_uint64(cmd_array, i, (uint64_t)va.va_rdev);
    641 
    642 		i++;
    643 	}
    644 
    645 	rw_exit(&dmv->dev_rwlock);
    646 
    647 	char *xml;
    648 	xml = prop_dictionary_externalize(dm_dict);
    649 	printf("%s\n",xml);
    650 
    651 	return 0;
    652 }
    653 
    654 /*
    655  * Load new table/tables to device.
    656  * Call apropriate target init routine open all physical pdev's and
    657  * link them to device. For other targets mirror, strip, snapshot
    658  * etc. also add dependency devices to upcalls list.
    659  *
    660  * Load table to inactive slot table are switched in dm_device_resume_ioctl.
    661  * This simulates Linux behaviour better there should not be any difference.
    662  *
    663  * Locking: dev_mtx
    664  */
    665 int
    666 dm_table_load_ioctl(prop_dictionary_t dm_dict)
    667 {
    668 	struct dm_dev *dmv;
    669 	struct dm_table_entry *table_en, *last_table;
    670 	struct dm_table  *tbl;
    671 	struct dm_target *target;
    672 
    673 	prop_object_iterator_t iter;
    674 	prop_array_t cmd_array;
    675 	prop_dictionary_t target_dict;
    676 
    677 	const char *name, *uuid, *type;
    678 
    679 	uint32_t flags, ret;
    680 
    681 	uint64_t dev;
    682 
    683 	char *str;
    684 
    685 	ret = 0;
    686 	flags = 0;
    687 	name = NULL;
    688 	uuid = NULL;
    689 	dmv = NULL;
    690 	last_table = NULL;
    691 
    692 	char *xml;
    693 	xml = prop_dictionary_externalize(dm_dict);
    694 	printf("%s\n",xml);
    695 
    696 	prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_NAME, &name);
    697 	prop_dictionary_get_cstring_nocopy(dm_dict, DM_IOCTL_UUID, &uuid);
    698 	prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
    699 	prop_dictionary_get_uint64(dm_dict, DM_IOCTL_DEV, &dev);
    700 
    701 	cmd_array = prop_dictionary_get(dm_dict, DM_IOCTL_CMD_DATA);
    702 	iter = prop_array_iterator(cmd_array);
    703 
    704 	dm_dbg_print_flags(flags);
    705 
    706 	aprint_verbose("Loading table to device: %s--%s\n",name,uuid);
    707 
    708 	if (((dmv = dm_dev_lookup_name(name)) == NULL) &&
    709 	    ((dmv = dm_dev_lookup_uuid(uuid)) == NULL) &&
    710 	    ((dmv = dm_dev_lookup_minor(minor(dev))) == NULL)) {
    711 		DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
    712 		return ENOENT;
    713 	}
    714 
    715 	/* Select unused table */
    716 	tbl = &dmv->tables[1-dmv->cur_active_table];
    717 
    718 	/*
    719 	 * I have to check if this table slot is not used by another table list.
    720 	 * if it is used I should free them.
    721 	 */
    722 
    723 	dm_table_destroy(tbl);
    724 
    725 	while((target_dict = prop_object_iterator_next(iter)) != NULL){
    726 
    727 		prop_dictionary_get_cstring_nocopy(target_dict,
    728 		    DM_TABLE_TYPE, &type);
    729 
    730 		/*
    731 		 * If we want to deny table with 2 or more different
    732 		 * target we should do it here
    733 		 */
    734 		if ((target = dm_target_lookup_name(type)) == NULL)
    735 			return ENOENT;
    736 
    737 		if ((table_en=kmem_alloc(sizeof(struct dm_table_entry),
    738 			    KM_NOSLEEP)) == NULL)
    739 			return ENOMEM;
    740 
    741 		prop_dictionary_get_uint64(target_dict, DM_TABLE_START,
    742 		    &table_en->start);
    743 		prop_dictionary_get_uint64(target_dict, DM_TABLE_LENGTH,
    744 		    &table_en->length);
    745 
    746 		table_en->target = target;
    747 		table_en->dm_dev = dmv;
    748 		table_en->target_config = NULL;
    749 
    750 		/*
    751 		 * There is a parameter string after dm_target_spec
    752 		 * structure which  points to /dev/wd0a 284 part of
    753 		 * table. String str points to this text.
    754 		 */
    755 
    756 		prop_dictionary_get_cstring(target_dict,
    757 		    DM_TABLE_PARAMS, (char**)&str);
    758 
    759 		/* Test readonly flag change anything only if it is not set*/
    760 		if (!(flags & DM_READONLY_FLAG)){
    761 
    762 			if (SLIST_EMPTY(tbl))
    763 				/* insert this table to head */
    764 				SLIST_INSERT_HEAD(tbl, table_en, next);
    765 			else
    766 				SLIST_INSERT_AFTER(last_table, table_en, next);
    767 
    768 			/*
    769 			 * Params string is different for every target,
    770 			 * therfore I have to pass it to target init
    771 			 * routine and parse parameters there.
    772 			 */
    773 			if (target->init != NULL && strlen(str) != 0)
    774 			       ret = target->init(dmv, &table_en->target_config,
    775 				   str);
    776 
    777 			if (ret != 0) {
    778 				dm_table_destroy(tbl);
    779 
    780 				return ret;
    781 			}
    782 
    783 			last_table = table_en;
    784 
    785 		} else
    786 			kmem_free(table_en, sizeof(struct dm_table_entry));
    787 
    788 		prop_object_release(str);
    789 	}
    790 
    791 	prop_object_release(cmd_array);
    792 
    793 	DM_ADD_FLAG(flags, DM_ACTIVE_PRESENT_FLAG);
    794 
    795 	return 0;
    796 }
    797 
    798 /*
    799  * Get description of all tables loaded to device from kernel
    800  * and send it to libdevmapper.
    801  *
    802  * Output dictionary for every table:
    803  *
    804  * <key>cmd_data</key>
    805  * <array>
    806  *   <dict>
    807  *    <key>type<key>
    808  *    <string>...</string>
    809  *
    810  *    <key>start</key>
    811  *    <integer>...</integer>
    812  *
    813  *    <key>length</key>
    814  *    <integer>...</integer>
    815  *
    816  *    <key>params</key>
    817  *    <string>...</string>
    818  *   </dict>
    819  * </array>
    820  *
    821  * Locking: dev_mtx
    822  */
    823 int
    824 dm_table_status_ioctl(prop_dictionary_t dm_dict)
    825 {
    826 	struct dm_dev *dmv;
    827 	struct dm_table  *tbl;
    828 	struct dm_table_entry *table_en;
    829 
    830 	prop_array_t cmd_array;
    831 	prop_dictionary_t target_dict;
    832 
    833 	uint32_t rec_size;
    834 	uint64_t dev;
    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_uint64(dm_dict, DM_IOCTL_DEV, &dev);
    852 
    853 	cmd_array = prop_dictionary_get(dm_dict, DM_IOCTL_CMD_DATA);
    854 
    855 	if (((dmv = dm_dev_lookup_name(name)) == NULL) &&
    856 	    ((dmv = dm_dev_lookup_uuid(uuid)) == NULL) &&
    857 	    ((dmv = dm_dev_lookup_minor(minor(dev)))) == NULL) {
    858 		DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
    859 		return ENOENT;
    860 	}
    861 
    862 	/* I should use mutex here and not rwlock there can be IO operation
    863 	   during this ioctl on device. */
    864 
    865 	/*  mutex_enter(&dmv->dev_mtx); */
    866 
    867 	aprint_verbose("Status of device tables: %s--%d\n",
    868 	    name, dmv->cur_active_table);
    869 
    870 	tbl = &dmv->tables[dmv->cur_active_table];
    871 
    872 	if (tbl != NULL)
    873 		DM_ADD_FLAG(flags, DM_ACTIVE_PRESENT_FLAG);
    874 	else {
    875 		DM_REMOVE_FLAG(flags, DM_ACTIVE_PRESENT_FLAG);
    876 
    877 		return 0;
    878 	}
    879 
    880 	SLIST_FOREACH(table_en,tbl,next)
    881 	{
    882 		target_dict = prop_dictionary_create();
    883 		aprint_verbose("%016" PRIu64 ", length %016" PRIu64
    884 		    ", target %s\n", table_en->start, table_en->length,
    885 		    table_en->target->name);
    886 
    887 		prop_dictionary_set_uint64(target_dict, DM_TABLE_START,
    888 		    table_en->start);
    889 		prop_dictionary_set_uint64(target_dict, DM_TABLE_LENGTH,
    890 		    table_en->length);
    891 
    892 		prop_dictionary_set_cstring(target_dict, DM_TABLE_TYPE,
    893 		    table_en->target->name);
    894 
    895 		prop_dictionary_set_int32(target_dict, DM_TABLE_STAT,
    896 		    dmv->cur_active_table);
    897 
    898 		if (flags |= DM_STATUS_TABLE_FLAG) {
    899 			params = table_en->target->status
    900 			    (table_en->target_config);
    901 
    902 			if(params != NULL){
    903 				prop_dictionary_set_cstring(target_dict,
    904 				    DM_TABLE_PARAMS, params);
    905 
    906 				kmem_free(params,strlen(params)+1);
    907 			}
    908 		}
    909 		prop_array_set(cmd_array, i, target_dict);
    910 
    911 		prop_object_release(target_dict);
    912 
    913 		i++;
    914 	}
    915 
    916 	/*mutex_exit(&dmv->dev_mtx);*/
    917 
    918 	return 0;
    919 }
    920 
    921 
    922 /*
    923  * For every call I have to set kernel driver version.
    924  * Because I can have commands supported only in other
    925  * newer/later version. This routine is called for every
    926  * ioctl command.
    927  */
    928 int
    929 dm_check_version(prop_dictionary_t dm_dict)
    930 {
    931 	size_t i;
    932 	int dm_version[3];
    933 	prop_array_t ver;
    934 	int r;
    935 
    936 	r = 0;
    937 
    938 	ver = prop_dictionary_get(dm_dict, DM_IOCTL_VERSION);
    939 
    940 	for(i=0; i < 3; i++)
    941 		prop_array_get_uint32(ver, i, &dm_version[i]);
    942 
    943 
    944 	if (DM_VERSION_MAJOR != dm_version[0] ||
    945 	    DM_VERSION_MINOR < dm_version[1])
    946 	{
    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 		r = EIO;
    953 		goto out;
    954 	}
    955 
    956 	prop_array_set_uint32(ver, 0, DM_VERSION_MAJOR);
    957 	prop_array_set_uint32(ver, 1, DM_VERSION_MINOR);
    958 	prop_array_set_uint32(ver, 2, DM_VERSION_PATCHLEVEL);
    959 
    960 out:
    961 	return r;
    962 }
    963