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