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