1 /* $NetBSD: libdm-nbsd-iface.c,v 1.1 2008/12/22 00:56:59 haad Exp $ */ 2 3 /* 4 * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved. 5 * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved. 6 * Copyright (C) 2008 Adam Hamsik. All rights reserved. 7 * 8 * This file is part of the device-mapper userspace tools. 9 * 10 * This copyrighted material is made available to anyone wishing to use, 11 * modify, copy, or redistribute it subject to the terms and conditions 12 * of the GNU Lesser General Public License v.2.1. 13 * 14 * You should have received a copy of the GNU Lesser General Public License 15 * along with this program; if not, write to the Free Software Foundation, 16 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17 */ 18 19 #include "dmlib.h" 20 #include "libdm-targets.h" 21 #include "libdm-common.h" 22 23 #include <sys/ioctl.h> 24 #include <sys/sysctl.h> 25 26 #include <fcntl.h> 27 #include <dirent.h> 28 #include <limits.h> 29 30 #include <netbsd-dm.h> 31 32 #include <dm-ioctl.h> 33 34 /* 35 * Ensure build compatibility. 36 * The hard-coded versions here are the highest present 37 * in the _cmd_data arrays. 38 */ 39 40 #if !((DM_VERSION_MAJOR == 1 && DM_VERSION_MINOR >= 0) || \ 41 (DM_VERSION_MAJOR == 4 && DM_VERSION_MINOR >= 0)) 42 #error The version of dm-ioctl.h included is incompatible. 43 #endif 44 45 /* dm major version no for running kernel */ 46 static unsigned _dm_version_minor = 0; 47 static unsigned _dm_version_patchlevel = 0; 48 49 static int _control_fd = -1; 50 static int _version_checked = 0; 51 static int _version_ok = 1; 52 static unsigned _ioctl_buffer_double_factor = 0; 53 54 /* *INDENT-OFF* */ 55 56 /* 57 * XXX Remove this structure and write another own one 58 * I don't understand why ioctl calls has different 59 * names then dm task type 60 */ 61 static struct cmd_data _cmd_data_v4[] = { 62 {"create", DM_DEV_CREATE, {4, 0, 0}}, 63 {"reload", DM_TABLE_LOAD, {4, 0, 0}}, /* DM_DEVICE_RELOAD */ 64 {"remove", DM_DEV_REMOVE, {4, 0, 0}}, 65 {"remove_all", DM_REMOVE_ALL, {4, 0, 0}}, 66 {"suspend", DM_DEV_SUSPEND, {4, 0, 0}}, 67 {"resume", DM_DEV_SUSPEND, {4, 0, 0}}, 68 {"info", DM_DEV_STATUS, {4, 0, 0}}, 69 {"deps", DM_TABLE_DEPS, {4, 0, 0}}, /* DM_DEVICE_DEPS */ 70 {"rename", DM_DEV_RENAME, {4, 0, 0}}, 71 {"version", DM_VERSION, {4, 0, 0}}, 72 {"status", DM_TABLE_STATUS, {4, 0, 0}}, 73 {"table", DM_TABLE_STATUS, {4, 0, 0}}, /* DM_DEVICE_TABLE */ 74 {"waitevent", DM_DEV_WAIT, {4, 0, 0}}, 75 {"names", DM_LIST_DEVICES, {4, 0, 0}}, 76 {"clear", DM_TABLE_CLEAR, {4, 0, 0}}, 77 {"mknodes", DM_DEV_STATUS, {4, 0, 0}}, 78 #ifdef DM_LIST_VERSIONS 79 {"targets", DM_LIST_VERSIONS, {4, 1, 0}}, 80 #endif 81 #ifdef DM_TARGET_MSG 82 {"message", DM_TARGET_MSG, {4, 2, 0}}, 83 #endif 84 #ifdef DM_DEV_SET_GEOMETRY 85 {"setgeometry", DM_DEV_SET_GEOMETRY, {4, 6, 0}}, 86 #endif 87 }; 88 /* *INDENT-ON* */ 89 90 /* 91 * In NetBSD we use sysctl to get kernel drivers info. control device 92 * has predefined minor number 0 and major number = char major number 93 * of dm driver. First slot is therefore ocupied with control device 94 * and minor device starts from 1; 95 */ 96 97 static int _control_device_number(uint32_t *major, uint32_t *minor) 98 { 99 100 nbsd_get_dm_major(major, DM_CHAR_MAJOR); 101 102 *minor = 0; 103 104 return 1; 105 } 106 107 /* 108 * Returns 1 if exists; 0 if it doesn't; -1 if it's wrong 109 */ 110 static int _control_exists(const char *control, uint32_t major, uint32_t minor) 111 { 112 struct stat buf; 113 114 if (stat(control, &buf) < 0) { 115 if (errno != ENOENT) 116 log_sys_error("stat", control); 117 return 0; 118 } 119 120 if (!S_ISCHR(buf.st_mode)) { 121 log_verbose("%s: Wrong inode type", control); 122 if (!unlink(control)) 123 return 0; 124 log_sys_error("unlink", control); 125 return -1; 126 } 127 128 if (major && buf.st_rdev != MKDEV(major, minor)) { 129 log_verbose("%s: Wrong device number: (%u, %u) instead of " 130 "(%u, %u)", control, 131 MAJOR(buf.st_mode), MINOR(buf.st_mode), 132 major, minor); 133 if (!unlink(control)) 134 return 0; 135 log_sys_error("unlink", control); 136 return -1; 137 } 138 139 return 1; 140 } 141 142 static int _create_control(const char *control, uint32_t major, uint32_t minor) 143 { 144 int ret; 145 mode_t old_umask; 146 147 if (!major) 148 return 0; 149 150 old_umask = umask(0022); 151 ret = dm_create_dir(dm_dir()); 152 umask(old_umask); 153 154 if (!ret) 155 return 0; 156 157 log_verbose("Creating device %s (%u, %u)", control, major, minor); 158 159 if (mknod(control, S_IFCHR | S_IRUSR | S_IWUSR, 160 MKDEV(major, minor)) < 0) { 161 log_sys_error("mknod", control); 162 return 0; 163 } 164 165 166 return 1; 167 } 168 169 /* Check if major is device-mapper block device major number */ 170 int dm_is_dm_major(uint32_t major) 171 { 172 uint32_t dm_major; 173 174 nbsd_get_dm_major(&dm_major, DM_BLOCK_MAJOR); 175 176 if (major == dm_major) 177 return 1; 178 179 return 0; 180 } 181 182 /* Open control device if doesn't exist create it. */ 183 static int _open_control(void) 184 { 185 char control[PATH_MAX]; 186 uint32_t major = 0, minor = 0; 187 188 if (_control_fd != -1) 189 return 1; 190 191 snprintf(control, sizeof(control), "%s/control", dm_dir()); 192 193 if (!_control_device_number(&major, &minor)) 194 log_error("Is device-mapper driver missing from kernel?"); 195 196 if (!_control_exists(control, major, minor) && 197 !_create_control(control, major, minor)) 198 goto error; 199 200 if ((_control_fd = open(control, O_RDWR)) < 0) { 201 log_sys_error("open", control); 202 goto error; 203 } 204 205 return 1; 206 207 error: 208 log_error("Failure to communicate with kernel device-mapper driver."); 209 return 0; 210 } 211 212 /* 213 * Destroy dm task structure there are some dynamically alocated values there. 214 * name, uuid, head, tail list. 215 */ 216 void dm_task_destroy(struct dm_task *dmt) 217 { 218 struct target *t, *n; 219 220 for (t = dmt->head; t; t = n) { 221 n = t->next; 222 dm_free(t->params); 223 dm_free(t->type); 224 dm_free(t); 225 } 226 227 if (dmt->dev_name) 228 dm_free(dmt->dev_name); 229 230 if (dmt->newname) 231 dm_free(dmt->newname); 232 233 if (dmt->message) 234 dm_free(dmt->message); 235 236 if (dmt->dmi.v4) 237 dm_free(dmt->dmi.v4); 238 239 if (dmt->uuid) 240 dm_free(dmt->uuid); 241 242 dm_free(dmt); 243 244 } 245 246 /* Get kernel driver version from dm_ioctl structure. */ 247 int dm_task_get_driver_version(struct dm_task *dmt, char *version, size_t size) 248 { 249 unsigned *v; 250 251 if (!dmt->dmi.v4) { 252 version[0] = '\0'; 253 return 0; 254 } 255 256 v = dmt->dmi.v4->version; 257 snprintf(version, size, "%u.%u.%u", v[0], v[1], v[2]); 258 _dm_version_minor = v[1]; 259 _dm_version_patchlevel = v[2]; 260 261 return 1; 262 } 263 264 /* Get kernel driver protocol version and comapre it with library version. */ 265 static int _check_version(char *version, size_t size) 266 { 267 struct dm_task *task; 268 int r; 269 270 if (!(task = dm_task_create(DM_DEVICE_VERSION))) { 271 log_error("Failed to get device-mapper version"); 272 version[0] = '\0'; 273 return 0; 274 } 275 276 r = dm_task_run(task); 277 dm_task_get_driver_version(task, version, size); 278 dm_task_destroy(task); 279 280 return r; 281 } 282 283 /* 284 * Find out device-mapper's major version number the first time 285 * this is called and whether or not we support it. 286 */ 287 int dm_check_version(void) 288 { 289 char dmversion[64]; 290 291 if (_version_checked) 292 return _version_ok; 293 294 _version_checked = 1; 295 296 if (_check_version(dmversion, sizeof(dmversion))) 297 return 1; 298 299 300 return 0; 301 } 302 303 /* Get next target(table description) from list pointed by dmt->head. */ 304 void *dm_get_next_target(struct dm_task *dmt, void *next, 305 uint64_t *start, uint64_t *length, 306 char **target_type, char **params) 307 { 308 struct target *t = (struct target *) next; 309 310 if (!t) 311 t = dmt->head; 312 313 if (!t) 314 return NULL; 315 316 *start = t->start; 317 *length = t->length; 318 *target_type = t->type; 319 *params = t->params; 320 321 return t->next; 322 } 323 324 /* Unmarshall the target info returned from a status call */ 325 static int _unmarshal_status(struct dm_task *dmt, struct dm_ioctl *dmi) 326 { 327 char *outbuf = (char *) dmi + dmi->data_start; 328 char *outptr = outbuf; 329 uint32_t i; 330 struct dm_target_spec *spec; 331 332 for (i = 0; i < dmi->target_count; i++) { 333 spec = (struct dm_target_spec *) outptr; 334 if (!dm_task_add_target(dmt, spec->sector_start, 335 spec->length, 336 spec->target_type, 337 outptr + sizeof(*spec))) { 338 return 0; 339 } 340 341 outptr = outbuf + spec->next; 342 } 343 344 return 1; 345 } 346 347 /* 348 * @dev_major is major number of char device 349 * 350 * I have to find it's block device number and lookup dev in 351 * device database to find device path. 352 * 353 */ 354 355 int dm_format_dev(char *buf, int bufsize, uint32_t dev_major, 356 uint32_t dev_minor) 357 { 358 int r; 359 uint32_t major, dm_major; 360 char *name; 361 mode_t mode; 362 dev_t dev; 363 size_t val_len,i; 364 struct kinfo_drivers *kd; 365 366 mode = 0; 367 368 nbsd_get_dm_major(&dm_major, DM_BLOCK_MAJOR); 369 370 log_error("format_dev %d--%d %d", dev_major, dev_minor, bufsize); 371 372 if (bufsize < 8) 373 return 0; 374 375 if (sysctlbyname("kern.drivers",NULL,&val_len,NULL,0) < 0) { 376 printf("sysctlbyname failed"); 377 return 0; 378 } 379 380 if ((kd = malloc (val_len)) == NULL){ 381 printf("malloc kd info error\n"); 382 return 0; 383 } 384 385 if (sysctlbyname("kern.drivers", kd, &val_len, NULL, 0) < 0) { 386 printf("sysctlbyname failed kd"); 387 return 0; 388 } 389 390 for (i = 0, val_len /= sizeof(*kd); i < val_len; i++){ 391 if (kd[i].d_cmajor == dev_major) { 392 major = kd[i].d_bmajor; 393 break; 394 } 395 } 396 397 dev = MKDEV(major,dev_minor); 398 399 mode |= S_IFBLK; 400 401 name = devname(dev,mode); 402 403 r = snprintf(buf, (size_t) bufsize, "/dev/%s",name); 404 405 free(kd); 406 407 if (r < 0 || r > bufsize - 1 || name == NULL) 408 return 0; 409 410 return 1; 411 } 412 413 /* Fill info from dm_ioctl structure. Look at DM_EXISTS_FLAG*/ 414 int dm_task_get_info(struct dm_task *dmt, struct dm_info *info) 415 { 416 if (!dmt->dmi.v4) 417 return 0; 418 419 memset(info, 0, sizeof(*info)); 420 421 info->exists = dmt->dmi.v4->flags & DM_EXISTS_FLAG ? 1 : 0; 422 if (!info->exists) 423 return 1; 424 425 info->suspended = dmt->dmi.v4->flags & DM_SUSPEND_FLAG ? 1 : 0; 426 info->read_only = dmt->dmi.v4->flags & DM_READONLY_FLAG ? 1 : 0; 427 info->live_table = dmt->dmi.v4->flags & DM_ACTIVE_PRESENT_FLAG ? 1 : 0; 428 info->inactive_table = dmt->dmi.v4->flags & DM_INACTIVE_PRESENT_FLAG ? 429 1 : 0; 430 info->target_count = dmt->dmi.v4->target_count; 431 info->open_count = dmt->dmi.v4->open_count; 432 info->event_nr = dmt->dmi.v4->event_nr; 433 434 nbsd_get_dm_major(&info->major, DM_BLOCK_MAJOR); /* get netbsd dm device major number */ 435 info->minor = MINOR(dmt->dmi.v4->dev); 436 437 return 1; 438 } 439 440 /* Unsupported on NetBSD */ 441 uint32_t dm_task_get_read_ahead(const struct dm_task *dmt, uint32_t *read_ahead) 442 { 443 *read_ahead = DM_READ_AHEAD_NONE; 444 return 1; 445 } 446 447 const char *dm_task_get_name(const struct dm_task *dmt) 448 { 449 450 return (dmt->dmi.v4->name); 451 } 452 453 const char *dm_task_get_uuid(const struct dm_task *dmt) 454 { 455 456 return (dmt->dmi.v4->uuid); 457 } 458 459 struct dm_deps *dm_task_get_deps(struct dm_task *dmt) 460 { 461 return (struct dm_deps *) (((void *) dmt->dmi.v4) + 462 dmt->dmi.v4->data_start); 463 } 464 465 struct dm_names *dm_task_get_names(struct dm_task *dmt) 466 { 467 return (struct dm_names *) (((void *) dmt->dmi.v4) + 468 dmt->dmi.v4->data_start); 469 } 470 471 struct dm_versions *dm_task_get_versions(struct dm_task *dmt) 472 { 473 return (struct dm_versions *) (((void *) dmt->dmi.v4) + 474 dmt->dmi.v4->data_start); 475 } 476 477 int dm_task_set_ro(struct dm_task *dmt) 478 { 479 dmt->read_only = 1; 480 return 1; 481 } 482 483 /* Unsupported on NetBSD */ 484 int dm_task_set_read_ahead(struct dm_task *dmt, uint32_t read_ahead, 485 uint32_t read_ahead_flags) 486 { 487 return 1; 488 } 489 490 int dm_task_suppress_identical_reload(struct dm_task *dmt) 491 { 492 dmt->suppress_identical_reload = 1; 493 return 1; 494 } 495 496 int dm_task_set_newname(struct dm_task *dmt, const char *newname) 497 { 498 if (!(dmt->newname = dm_strdup(newname))) { 499 log_error("dm_task_set_newname: strdup(%s) failed", newname); 500 return 0; 501 } 502 503 return 1; 504 } 505 506 int dm_task_set_message(struct dm_task *dmt, const char *message) 507 { 508 if (!(dmt->message = dm_strdup(message))) { 509 log_error("dm_task_set_message: strdup(%s) failed", message); 510 return 0; 511 } 512 513 return 1; 514 } 515 516 int dm_task_set_sector(struct dm_task *dmt, uint64_t sector) 517 { 518 dmt->sector = sector; 519 520 return 1; 521 } 522 523 /* Unsupported in NetBSD */ 524 int dm_task_set_geometry(struct dm_task *dmt, const char *cylinders, 525 const char *heads, const char *sectors, const char *start) 526 { 527 return 0; 528 } 529 530 int dm_task_no_flush(struct dm_task *dmt) 531 { 532 dmt->no_flush = 1; 533 534 return 1; 535 } 536 537 int dm_task_no_open_count(struct dm_task *dmt) 538 { 539 dmt->no_open_count = 1; 540 541 return 1; 542 } 543 544 int dm_task_skip_lockfs(struct dm_task *dmt) 545 { 546 dmt->skip_lockfs = 1; 547 548 return 1; 549 } 550 551 int dm_task_set_event_nr(struct dm_task *dmt, uint32_t event_nr) 552 { 553 dmt->event_nr = event_nr; 554 555 return 1; 556 } 557 558 /* Allocate one target(table description) entry. */ 559 struct target *create_target(uint64_t start, uint64_t len, const char *type, 560 const char *params) 561 { 562 struct target *t = dm_malloc(sizeof(*t)); 563 564 if (!t) { 565 log_error("create_target: malloc(%" PRIsize_t ") failed", 566 sizeof(*t)); 567 return NULL; 568 } 569 570 memset(t, 0, sizeof(*t)); 571 572 if (!(t->params = dm_strdup(params))) { 573 log_error("create_target: strdup(params) failed"); 574 goto bad; 575 } 576 577 if (!(t->type = dm_strdup(type))) { 578 log_error("create_target: strdup(type) failed"); 579 goto bad; 580 } 581 582 t->start = start; 583 t->length = len; 584 return t; 585 586 bad: 587 dm_free(t->params); 588 dm_free(t->type); 589 dm_free(t); 590 return NULL; 591 } 592 593 /* Parse given dm task structure to proplib dictionary. */ 594 static int _flatten(struct dm_task *dmt, prop_dictionary_t dm_dict) 595 { 596 prop_array_t cmd_array; 597 prop_dictionary_t target_spec; 598 599 struct target *t; 600 601 size_t len; 602 char type[DM_MAX_TYPE_NAME]; 603 604 uint32_t major, flags; 605 int count = 0; 606 const int (*version)[3]; 607 608 flags = 0; 609 version = &_cmd_data_v4[dmt->type].version; 610 611 cmd_array = prop_array_create(); 612 613 for (t = dmt->head; t; t = t->next) { 614 target_spec = prop_dictionary_create(); 615 616 prop_dictionary_set_uint64(target_spec,DM_TABLE_START,t->start); 617 prop_dictionary_set_uint64(target_spec,DM_TABLE_LENGTH,t->length); 618 619 strlcpy(type,t->type,DM_MAX_TYPE_NAME); 620 621 prop_dictionary_set_cstring(target_spec,DM_TABLE_TYPE,type); 622 prop_dictionary_set_cstring(target_spec,DM_TABLE_PARAMS,t->params); 623 624 prop_array_set(cmd_array,count,target_spec); 625 626 prop_object_release(target_spec); 627 628 count++; 629 } 630 631 632 if (count && (dmt->sector || dmt->message)) { 633 log_error("targets and message are incompatible"); 634 return -1; 635 } 636 637 if (count && dmt->newname) { 638 log_error("targets and newname are incompatible"); 639 return -1; 640 } 641 642 if (count && dmt->geometry) { 643 log_error("targets and geometry are incompatible"); 644 return -1; 645 } 646 647 if (dmt->newname && (dmt->sector || dmt->message)) { 648 log_error("message and newname are incompatible"); 649 return -1; 650 } 651 652 if (dmt->newname && dmt->geometry) { 653 log_error("geometry and newname are incompatible"); 654 return -1; 655 } 656 657 if (dmt->geometry && (dmt->sector || dmt->message)) { 658 log_error("geometry and message are incompatible"); 659 return -1; 660 } 661 662 if (dmt->sector && !dmt->message) { 663 log_error("message is required with sector"); 664 return -1; 665 } 666 667 if (dmt->newname) 668 len += strlen(dmt->newname) + 1; 669 670 if (dmt->message) 671 len += sizeof(struct dm_target_msg) + strlen(dmt->message) + 1; 672 673 if (dmt->geometry) 674 len += strlen(dmt->geometry) + 1; 675 676 nbsd_dmi_add_version((*version), dm_dict); 677 678 nbsd_get_dm_major(&major, DM_BLOCK_MAJOR); 679 /* 680 * Only devices with major which is equal to netbsd dm major 681 * dm devices in NetBSD can't have more majors then one assigned to dm. 682 */ 683 if (dmt->major != major && dmt->major != -1) 684 return -1; 685 686 if (dmt->minor >= 0) { 687 flags |= DM_PERSISTENT_DEV_FLAG; 688 689 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmt->minor); 690 } 691 692 /* Set values to dictionary. */ 693 if (dmt->dev_name) 694 prop_dictionary_set_cstring(dm_dict, DM_IOCTL_NAME, dmt->dev_name); 695 696 if (dmt->uuid) 697 prop_dictionary_set_cstring(dm_dict, DM_IOCTL_UUID, dmt->uuid); 698 699 if (dmt->type == DM_DEVICE_SUSPEND) 700 flags |= DM_SUSPEND_FLAG; 701 if (dmt->no_flush) 702 flags |= DM_NOFLUSH_FLAG; 703 if (dmt->read_only) 704 flags |= DM_READONLY_FLAG; 705 if (dmt->skip_lockfs) 706 flags |= DM_SKIP_LOCKFS_FLAG; 707 708 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_FLAGS, flags); 709 710 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_EVENT, dmt->event_nr); 711 712 if (dmt->newname) 713 prop_array_set_cstring(cmd_array, 0, dmt->newname); 714 715 /* Add array for all COMMAND specific data. */ 716 prop_dictionary_set(dm_dict, DM_IOCTL_CMD_DATA, cmd_array); 717 prop_object_release(cmd_array); 718 719 return 0; 720 } 721 722 static int _process_mapper_dir(struct dm_task *dmt) 723 { 724 struct dirent *dirent; 725 DIR *d; 726 const char *dir; 727 int r = 1; 728 729 dir = dm_dir(); 730 if (!(d = opendir(dir))) { 731 log_sys_error("opendir", dir); 732 return 0; 733 } 734 735 while ((dirent = readdir(d))) { 736 if (!strcmp(dirent->d_name, ".") || 737 !strcmp(dirent->d_name, "..") || 738 !strcmp(dirent->d_name, "control")) 739 continue; 740 dm_task_set_name(dmt, dirent->d_name); 741 dm_task_run(dmt); 742 } 743 744 if (closedir(d)) 745 log_sys_error("closedir", dir); 746 747 return r; 748 } 749 750 /* Get list of all devices. */ 751 static int _process_all_v4(struct dm_task *dmt) 752 { 753 struct dm_task *task; 754 struct dm_names *names; 755 unsigned next = 0; 756 int r = 1; 757 758 if (!(task = dm_task_create(DM_DEVICE_LIST))) 759 return 0; 760 761 if (!dm_task_run(task)) { 762 r = 0; 763 goto out; 764 } 765 766 if (!(names = dm_task_get_names(task))) { 767 r = 0; 768 goto out; 769 } 770 771 if (!names->dev) 772 goto out; 773 774 do { 775 names = (void *) names + next; 776 if (!dm_task_set_name(dmt, names->name)) { 777 r = 0; 778 goto out; 779 } 780 if (!dm_task_run(dmt)) 781 r = 0; 782 next = names->next; 783 } while (next); 784 785 out: 786 dm_task_destroy(task); 787 return r; 788 } 789 790 static int _mknodes_v4(struct dm_task *dmt) 791 { 792 (void) _process_mapper_dir(dmt); 793 794 return _process_all_v4(dmt); 795 } 796 797 /* Create new device and load table to it. */ 798 static int _create_and_load_v4(struct dm_task *dmt) 799 { 800 struct dm_task *task; 801 int r; 802 803 printf("create and load called \n"); 804 805 /* Use new task struct to create the device */ 806 if (!(task = dm_task_create(DM_DEVICE_CREATE))) { 807 log_error("Failed to create device-mapper task struct"); 808 return 0; 809 } 810 811 /* Copy across relevant fields */ 812 if (dmt->dev_name && !dm_task_set_name(task, dmt->dev_name)) { 813 dm_task_destroy(task); 814 return 0; 815 } 816 817 if (dmt->uuid && !dm_task_set_uuid(task, dmt->uuid)) { 818 dm_task_destroy(task); 819 return 0; 820 } 821 822 task->major = dmt->major; 823 task->minor = dmt->minor; 824 task->uid = dmt->uid; 825 task->gid = dmt->gid; 826 task->mode = dmt->mode; 827 828 r = dm_task_run(task); 829 dm_task_destroy(task); 830 if (!r) 831 return r; 832 833 /* Next load the table */ 834 if (!(task = dm_task_create(DM_DEVICE_RELOAD))) { 835 log_error("Failed to create device-mapper task struct"); 836 return 0; 837 } 838 839 /* Copy across relevant fields */ 840 if (dmt->dev_name && !dm_task_set_name(task, dmt->dev_name)) { 841 dm_task_destroy(task); 842 return 0; 843 } 844 845 task->read_only = dmt->read_only; 846 task->head = dmt->head; 847 task->tail = dmt->tail; 848 849 r = dm_task_run(task); 850 851 task->head = NULL; 852 task->tail = NULL; 853 dm_task_destroy(task); 854 if (!r) 855 goto revert; 856 857 /* Use the original structure last so the info will be correct */ 858 dmt->type = DM_DEVICE_RESUME; 859 dm_free(dmt->uuid); 860 dmt->uuid = NULL; 861 862 r = dm_task_run(dmt); 863 864 if (r) 865 return r; 866 867 revert: 868 dmt->type = DM_DEVICE_REMOVE; 869 dm_free(dmt->uuid); 870 dmt->uuid = NULL; 871 872 if (!dm_task_run(dmt)) 873 log_error("Failed to revert device creation."); 874 875 return r; 876 } 877 878 uint64_t dm_task_get_existing_table_size(struct dm_task *dmt) 879 { 880 return dmt->existing_table_size; 881 } 882 883 static int _reload_with_suppression_v4(struct dm_task *dmt) 884 { 885 struct dm_task *task; 886 struct target *t1, *t2; 887 int r; 888 889 /* New task to get existing table information */ 890 if (!(task = dm_task_create(DM_DEVICE_TABLE))) { 891 log_error("Failed to create device-mapper task struct"); 892 return 0; 893 } 894 895 /* Copy across relevant fields */ 896 if (dmt->dev_name && !dm_task_set_name(task, dmt->dev_name)) { 897 dm_task_destroy(task); 898 return 0; 899 } 900 901 if (dmt->uuid && !dm_task_set_uuid(task, dmt->uuid)) { 902 dm_task_destroy(task); 903 return 0; 904 } 905 906 task->major = dmt->major; 907 task->minor = dmt->minor; 908 909 r = dm_task_run(task); 910 911 if (!r) { 912 dm_task_destroy(task); 913 return r; 914 } 915 916 /* Store existing table size */ 917 t2 = task->head; 918 while (t2 && t2->next) 919 t2 = t2->next; 920 dmt->existing_table_size = t2 ? t2->start + t2->length : 0; 921 922 if ((task->dmi.v4->flags & DM_READONLY_FLAG) ? 1 : 0 != dmt->read_only) 923 goto no_match; 924 925 t1 = dmt->head; 926 t2 = task->head; 927 928 while (t1 && t2) { 929 while (t2->params[strlen(t2->params) - 1] == ' ') 930 t2->params[strlen(t2->params) - 1] = '\0'; 931 if ((t1->start != t2->start) || 932 (t1->length != t2->length) || 933 (strcmp(t1->type, t2->type)) || 934 (strcmp(t1->params, t2->params))) 935 goto no_match; 936 t1 = t1->next; 937 t2 = t2->next; 938 } 939 940 if (!t1 && !t2) { 941 dmt->dmi.v4 = task->dmi.v4; 942 task->dmi.v4 = NULL; 943 dm_task_destroy(task); 944 return 1; 945 } 946 947 no_match: 948 dm_task_destroy(task); 949 950 /* Now do the original reload */ 951 dmt->suppress_identical_reload = 0; 952 r = dm_task_run(dmt); 953 954 return r; 955 } 956 957 /* 958 * This function is heart of NetBSD libdevmapper-> device-mapper kernel protocol 959 * It creates proplib_dictionary from dm task structure and sends it to NetBSD 960 * kernel driver. After succesfull ioctl it create dmi structure from returned 961 * proplib dictionary. This way I keep number of changes in NetBSD version of 962 * libdevmapper as small as posible. 963 */ 964 static struct dm_ioctl *_do_dm_ioctl(struct dm_task *dmt, unsigned command) 965 { 966 struct dm_ioctl *dmi; 967 prop_dictionary_t dm_dict_in, dm_dict_out; 968 969 uint32_t flags; 970 971 dm_dict_in = NULL; 972 973 dm_dict_in = prop_dictionary_create(); /* Dictionary send to kernel */ 974 dm_dict_out = prop_dictionary_create(); /* Dictionary received from kernel */ 975 976 /* Set command name to dictionary */ 977 prop_dictionary_set_cstring(dm_dict_in, DM_IOCTL_COMMAND, 978 _cmd_data_v4[dmt->type].name); 979 980 /* Parse dmi from libdevmapper to dictionary */ 981 if (_flatten(dmt, dm_dict_in) < 0) 982 goto bad; 983 984 prop_dictionary_get_uint32(dm_dict_in, DM_IOCTL_FLAGS, &flags); 985 986 if (dmt->type == DM_DEVICE_TABLE) 987 flags |= DM_STATUS_TABLE_FLAG; 988 989 if (dmt->no_open_count) 990 flags |= DM_SKIP_BDGET_FLAG; 991 992 flags |= DM_EXISTS_FLAG; 993 994 /* Set flags to dictionary. */ 995 prop_dictionary_set_uint32(dm_dict_in,DM_IOCTL_FLAGS,flags); 996 997 prop_dictionary_externalize_to_file(dm_dict_in,"/tmp/test_in"); 998 999 log_very_verbose("Ioctl type %s --- flags %d",_cmd_data_v4[dmt->type].name,flags); 1000 //printf("name %s, major %d minor %d\n uuid %s\n", 1001 //dm_task_get_name(dmt), dmt->minor, dmt->major, dm_task_get_uuid(dmt)); 1002 /* Send dictionary to kernel and wait for reply. */ 1003 if (prop_dictionary_sendrecv_ioctl(dm_dict_in,_control_fd, 1004 NETBSD_DM_IOCTL,&dm_dict_out) != 0) { 1005 1006 if (errno == ENOENT && 1007 ((dmt->type == DM_DEVICE_INFO) || 1008 (dmt->type == DM_DEVICE_MKNODES) || 1009 (dmt->type == DM_DEVICE_STATUS))) { 1010 1011 /* 1012 * Linux version doesn't fail when ENOENT is returned 1013 * for nonexisting device after info, deps, mknodes call. 1014 * It returns dmi sent to kernel with DM_EXISTS_FLAG = 0; 1015 */ 1016 1017 dmi = nbsd_dm_dict_to_dmi(dm_dict_in,_cmd_data_v4[dmt->type].cmd); 1018 1019 dmi->flags &= ~DM_EXISTS_FLAG; 1020 1021 prop_object_release(dm_dict_in); 1022 prop_object_release(dm_dict_out); 1023 1024 goto out; 1025 } else { 1026 log_error("ioctl %s call failed with errno %d\n", 1027 _cmd_data_v4[dmt->type].name, errno); 1028 1029 prop_object_release(dm_dict_in); 1030 prop_object_release(dm_dict_out); 1031 1032 goto bad; 1033 } 1034 } 1035 1036 prop_dictionary_externalize_to_file(dm_dict_out,"/tmp/test_out"); 1037 1038 /* Parse kernel dictionary to dmi structure and return it to libdevmapper. */ 1039 dmi = nbsd_dm_dict_to_dmi(dm_dict_out,_cmd_data_v4[dmt->type].cmd); 1040 1041 prop_object_release(dm_dict_in); 1042 prop_object_release(dm_dict_out); 1043 out: 1044 return dmi; 1045 bad: 1046 return NULL; 1047 } 1048 1049 /* Create new edvice nodes in mapper/ dir. */ 1050 void dm_task_update_nodes(void) 1051 { 1052 update_devs(); 1053 } 1054 1055 /* Run dm command which is descirbed in dm_task structure. */ 1056 int dm_task_run(struct dm_task *dmt) 1057 { 1058 struct dm_ioctl *dmi; 1059 unsigned command; 1060 1061 if ((unsigned) dmt->type >= 1062 (sizeof(_cmd_data_v4) / sizeof(*_cmd_data_v4))) { 1063 log_error("Internal error: unknown device-mapper task %d", 1064 dmt->type); 1065 return 0; 1066 } 1067 1068 command = _cmd_data_v4[dmt->type].cmd; 1069 1070 /* Old-style creation had a table supplied */ 1071 if (dmt->type == DM_DEVICE_CREATE && dmt->head) 1072 return _create_and_load_v4(dmt); 1073 1074 if (dmt->type == DM_DEVICE_MKNODES && !dmt->dev_name && 1075 !dmt->uuid && dmt->major <= 0) 1076 return _mknodes_v4(dmt); 1077 1078 if ((dmt->type == DM_DEVICE_RELOAD) && dmt->suppress_identical_reload) 1079 return _reload_with_suppression_v4(dmt); 1080 1081 if (!_open_control()) 1082 return 0; 1083 1084 if (!(dmi = _do_dm_ioctl(dmt, command))) 1085 return 0; 1086 1087 switch (dmt->type) { 1088 case DM_DEVICE_CREATE: 1089 add_dev_node(dmt->dev_name, MAJOR(dmi->dev), MINOR(dmi->dev), 1090 dmt->uid, dmt->gid, dmt->mode); 1091 break; 1092 1093 case DM_DEVICE_REMOVE: 1094 /* FIXME Kernel needs to fill in dmi->name */ 1095 if (dmt->dev_name) 1096 rm_dev_node(dmt->dev_name); 1097 break; 1098 1099 case DM_DEVICE_RENAME: 1100 /* FIXME Kernel needs to fill in dmi->name */ 1101 if (dmt->dev_name) 1102 rename_dev_node(dmt->dev_name, dmt->newname); 1103 break; 1104 1105 case DM_DEVICE_RESUME: 1106 /* FIXME Kernel needs to fill in dmi->name */ 1107 set_dev_node_read_ahead(dmt->dev_name, dmt->read_ahead, 1108 dmt->read_ahead_flags); 1109 break; 1110 1111 case DM_DEVICE_MKNODES: 1112 if (dmi->flags & DM_EXISTS_FLAG) 1113 add_dev_node(dmi->name, MAJOR(dmi->dev), 1114 MINOR(dmi->dev), 1115 dmt->uid, dmt->gid, dmt->mode); 1116 else if (dmt->dev_name) 1117 rm_dev_node(dmt->dev_name); 1118 break; 1119 1120 case DM_DEVICE_STATUS: 1121 case DM_DEVICE_TABLE: 1122 case DM_DEVICE_WAITEVENT: 1123 if (!_unmarshal_status(dmt, dmi)) 1124 goto bad; 1125 break; 1126 } 1127 1128 /* Was structure reused? */ 1129 if (dmt->dmi.v4) 1130 dm_free(dmt->dmi.v4); 1131 1132 dmt->dmi.v4 = dmi; 1133 return 1; 1134 1135 bad: 1136 dm_free(dmi); 1137 return 0; 1138 } 1139 1140 void dm_lib_release(void) 1141 { 1142 if (_control_fd != -1) { 1143 close(_control_fd); 1144 _control_fd = -1; 1145 } 1146 update_devs(); 1147 } 1148 1149 void dm_lib_exit(void) 1150 { 1151 dm_lib_release(); 1152 dm_dump_memory(); 1153 _version_ok = 1; 1154 _version_checked = 0; 1155 } 1156