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