1 /* 2 * Copyright (C) 2001, 2002 Sistina Software (UK) Limited. 3 * Copyright (C) 2004 - 2005 Red Hat, Inc. All rights reserved. 4 * 5 * This file is released under the GPL. 6 */ 7 8 #include "dm.h" 9 10 #include <linux/module.h> 11 #include <linux/vmalloc.h> 12 #include <linux/miscdevice.h> 13 #include <linux/init.h> 14 #include <linux/wait.h> 15 #include <linux/slab.h> 16 #include <linux/devfs_fs_kernel.h> 17 #include <linux/dm-ioctl.h> 18 19 #include <asm/uaccess.h> 20 21 #define DM_DRIVER_EMAIL "dm-devel@redhat.com" 22 23 /*----------------------------------------------------------------- 24 * The ioctl interface needs to be able to look up devices by 25 * name or uuid. 26 *---------------------------------------------------------------*/ 27 struct hash_cell { 28 struct list_head name_list; 29 struct list_head uuid_list; 30 31 char *name; 32 char *uuid; 33 struct mapped_device *md; 34 struct dm_table *new_map; 35 }; 36 37 struct vers_iter { 38 size_t param_size; 39 struct dm_target_versions *vers, *old_vers; 40 char *end; 41 uint32_t flags; 42 }; 43 44 45 #define NUM_BUCKETS 64 46 #define MASK_BUCKETS (NUM_BUCKETS - 1) 47 static struct list_head _name_buckets[NUM_BUCKETS]; 48 static struct list_head _uuid_buckets[NUM_BUCKETS]; 49 50 static void dm_hash_remove_all(void); 51 52 /* 53 * Guards access to both hash tables. 54 */ 55 static DECLARE_RWSEM(_hash_lock); 56 57 static void init_buckets(struct list_head *buckets) 58 { 59 unsigned int i; 60 61 for (i = 0; i < NUM_BUCKETS; i++) 62 INIT_LIST_HEAD(buckets + i); 63 } 64 65 static int dm_hash_init(void) 66 { 67 init_buckets(_name_buckets); 68 init_buckets(_uuid_buckets); 69 devfs_mk_dir(DM_DIR); 70 return 0; 71 } 72 73 static void dm_hash_exit(void) 74 { 75 dm_hash_remove_all(); 76 devfs_remove(DM_DIR); 77 } 78 79 /*----------------------------------------------------------------- 80 * Hash function: 81 * We're not really concerned with the str hash function being 82 * fast since it's only used by the ioctl interface. 83 *---------------------------------------------------------------*/ 84 static unsigned int hash_str(const char *str) 85 { 86 const unsigned int hash_mult = 2654435387U; 87 unsigned int h = 0; 88 89 while (*str) 90 h = (h + (unsigned int) *str++) * hash_mult; 91 92 return h & MASK_BUCKETS; 93 } 94 95 /*----------------------------------------------------------------- 96 * Code for looking up a device by name 97 *---------------------------------------------------------------*/ 98 static struct hash_cell *__get_name_cell(const char *str) 99 { 100 struct hash_cell *hc; 101 unsigned int h = hash_str(str); 102 103 list_for_each_entry (hc, _name_buckets + h, name_list) 104 if (!strcmp(hc->name, str)) 105 return hc; 106 107 return NULL; 108 } 109 110 static struct hash_cell *__get_uuid_cell(const char *str) 111 { 112 struct hash_cell *hc; 113 unsigned int h = hash_str(str); 114 115 list_for_each_entry (hc, _uuid_buckets + h, uuid_list) 116 if (!strcmp(hc->uuid, str)) 117 return hc; 118 119 return NULL; 120 } 121 122 /*----------------------------------------------------------------- 123 * Inserting, removing and renaming a device. 124 *---------------------------------------------------------------*/ 125 static struct hash_cell *alloc_cell(const char *name, const char *uuid, 126 struct mapped_device *md) 127 { 128 struct hash_cell *hc; 129 130 hc = kmalloc(sizeof(*hc), GFP_KERNEL); 131 if (!hc) 132 return NULL; 133 134 hc->name = kstrdup(name, GFP_KERNEL); 135 if (!hc->name) { 136 kfree(hc); 137 return NULL; 138 } 139 140 if (!uuid) 141 hc->uuid = NULL; 142 143 else { 144 hc->uuid = kstrdup(uuid, GFP_KERNEL); 145 if (!hc->uuid) { 146 kfree(hc->name); 147 kfree(hc); 148 return NULL; 149 } 150 } 151 152 INIT_LIST_HEAD(&hc->name_list); 153 INIT_LIST_HEAD(&hc->uuid_list); 154 hc->md = md; 155 hc->new_map = NULL; 156 return hc; 157 } 158 159 static void free_cell(struct hash_cell *hc) 160 { 161 if (hc) { 162 kfree(hc->name); 163 kfree(hc->uuid); 164 kfree(hc); 165 } 166 } 167 168 /* 169 * devfs stuff. 170 */ 171 static int register_with_devfs(struct hash_cell *hc) 172 { 173 struct gendisk *disk = dm_disk(hc->md); 174 175 devfs_mk_bdev(MKDEV(disk->major, disk->first_minor), 176 S_IFBLK | S_IRUSR | S_IWUSR | S_IRGRP, 177 DM_DIR "/%s", hc->name); 178 return 0; 179 } 180 181 static int unregister_with_devfs(struct hash_cell *hc) 182 { 183 devfs_remove(DM_DIR"/%s", hc->name); 184 return 0; 185 } 186 187 /* 188 * The kdev_t and uuid of a device can never change once it is 189 * initially inserted. 190 */ 191 static int dm_hash_insert(const char *name, const char *uuid, struct mapped_device *md) 192 { 193 struct hash_cell *cell; 194 195 /* 196 * Allocate the new cells. 197 */ 198 cell = alloc_cell(name, uuid, md); 199 if (!cell) 200 return -ENOMEM; 201 202 /* 203 * Insert the cell into both hash tables. 204 */ 205 down_write(&_hash_lock); 206 if (__get_name_cell(name)) 207 goto bad; 208 209 list_add(&cell->name_list, _name_buckets + hash_str(name)); 210 211 if (uuid) { 212 if (__get_uuid_cell(uuid)) { 213 list_del(&cell->name_list); 214 goto bad; 215 } 216 list_add(&cell->uuid_list, _uuid_buckets + hash_str(uuid)); 217 } 218 register_with_devfs(cell); 219 dm_get(md); 220 dm_set_mdptr(md, cell); 221 up_write(&_hash_lock); 222 223 return 0; 224 225 bad: 226 up_write(&_hash_lock); 227 free_cell(cell); 228 return -EBUSY; 229 } 230 231 static void __hash_remove(struct hash_cell *hc) 232 { 233 /* remove from the dev hash */ 234 list_del(&hc->uuid_list); 235 list_del(&hc->name_list); 236 unregister_with_devfs(hc); 237 dm_set_mdptr(hc->md, NULL); 238 dm_put(hc->md); 239 if (hc->new_map) 240 dm_table_put(hc->new_map); 241 free_cell(hc); 242 } 243 244 static void dm_hash_remove_all(void) 245 { 246 int i; 247 struct hash_cell *hc; 248 struct list_head *tmp, *n; 249 250 down_write(&_hash_lock); 251 for (i = 0; i < NUM_BUCKETS; i++) { 252 list_for_each_safe (tmp, n, _name_buckets + i) { 253 hc = list_entry(tmp, struct hash_cell, name_list); 254 __hash_remove(hc); 255 } 256 } 257 up_write(&_hash_lock); 258 } 259 260 static int dm_hash_rename(const char *old, const char *new) 261 { 262 char *new_name, *old_name; 263 struct hash_cell *hc; 264 265 /* 266 * duplicate new. 267 */ 268 new_name = kstrdup(new, GFP_KERNEL); 269 if (!new_name) 270 return -ENOMEM; 271 272 down_write(&_hash_lock); 273 274 /* 275 * Is new free ? 276 */ 277 hc = __get_name_cell(new); 278 if (hc) { 279 DMWARN("asked to rename to an already existing name %s -> %s", 280 old, new); 281 up_write(&_hash_lock); 282 kfree(new_name); 283 return -EBUSY; 284 } 285 286 /* 287 * Is there such a device as 'old' ? 288 */ 289 hc = __get_name_cell(old); 290 if (!hc) { 291 DMWARN("asked to rename a non existent device %s -> %s", 292 old, new); 293 up_write(&_hash_lock); 294 kfree(new_name); 295 return -ENXIO; 296 } 297 298 /* 299 * rename and move the name cell. 300 */ 301 unregister_with_devfs(hc); 302 303 list_del(&hc->name_list); 304 old_name = hc->name; 305 hc->name = new_name; 306 list_add(&hc->name_list, _name_buckets + hash_str(new_name)); 307 308 /* rename the device node in devfs */ 309 register_with_devfs(hc); 310 311 up_write(&_hash_lock); 312 kfree(old_name); 313 return 0; 314 } 315 316 /*----------------------------------------------------------------- 317 * Implementation of the ioctl commands 318 *---------------------------------------------------------------*/ 319 /* 320 * All the ioctl commands get dispatched to functions with this 321 * prototype. 322 */ 323 typedef int (*ioctl_fn)(struct dm_ioctl *param, size_t param_size); 324 325 static int remove_all(struct dm_ioctl *param, size_t param_size) 326 { 327 dm_hash_remove_all(); 328 param->data_size = 0; 329 return 0; 330 } 331 332 /* 333 * Round up the ptr to an 8-byte boundary. 334 */ 335 #define ALIGN_MASK 7 336 static inline void *align_ptr(void *ptr) 337 { 338 return (void *) (((size_t) (ptr + ALIGN_MASK)) & ~ALIGN_MASK); 339 } 340 341 /* 342 * Retrieves the data payload buffer from an already allocated 343 * struct dm_ioctl. 344 */ 345 static void *get_result_buffer(struct dm_ioctl *param, size_t param_size, 346 size_t *len) 347 { 348 param->data_start = align_ptr(param + 1) - (void *) param; 349 350 if (param->data_start < param_size) 351 *len = param_size - param->data_start; 352 else 353 *len = 0; 354 355 return ((void *) param) + param->data_start; 356 } 357 358 static int list_devices(struct dm_ioctl *param, size_t param_size) 359 { 360 unsigned int i; 361 struct hash_cell *hc; 362 size_t len, needed = 0; 363 struct gendisk *disk; 364 struct dm_name_list *nl, *old_nl = NULL; 365 366 down_write(&_hash_lock); 367 368 /* 369 * Loop through all the devices working out how much 370 * space we need. 371 */ 372 for (i = 0; i < NUM_BUCKETS; i++) { 373 list_for_each_entry (hc, _name_buckets + i, name_list) { 374 needed += sizeof(struct dm_name_list); 375 needed += strlen(hc->name) + 1; 376 needed += ALIGN_MASK; 377 } 378 } 379 380 /* 381 * Grab our output buffer. 382 */ 383 nl = get_result_buffer(param, param_size, &len); 384 if (len < needed) { 385 param->flags |= DM_BUFFER_FULL_FLAG; 386 goto out; 387 } 388 param->data_size = param->data_start + needed; 389 390 nl->dev = 0; /* Flags no data */ 391 392 /* 393 * Now loop through filling out the names. 394 */ 395 for (i = 0; i < NUM_BUCKETS; i++) { 396 list_for_each_entry (hc, _name_buckets + i, name_list) { 397 if (old_nl) 398 old_nl->next = (uint32_t) ((void *) nl - 399 (void *) old_nl); 400 disk = dm_disk(hc->md); 401 nl->dev = huge_encode_dev(MKDEV(disk->major, disk->first_minor)); 402 nl->next = 0; 403 strcpy(nl->name, hc->name); 404 405 old_nl = nl; 406 nl = align_ptr(((void *) ++nl) + strlen(hc->name) + 1); 407 } 408 } 409 410 out: 411 up_write(&_hash_lock); 412 return 0; 413 } 414 415 static void list_version_get_needed(struct target_type *tt, void *needed_param) 416 { 417 size_t *needed = needed_param; 418 419 *needed += strlen(tt->name); 420 *needed += sizeof(tt->version); 421 *needed += ALIGN_MASK; 422 } 423 424 static void list_version_get_info(struct target_type *tt, void *param) 425 { 426 struct vers_iter *info = param; 427 428 /* Check space - it might have changed since the first iteration */ 429 if ((char *)info->vers + sizeof(tt->version) + strlen(tt->name) + 1 > 430 info->end) { 431 432 info->flags = DM_BUFFER_FULL_FLAG; 433 return; 434 } 435 436 if (info->old_vers) 437 info->old_vers->next = (uint32_t) ((void *)info->vers - 438 (void *)info->old_vers); 439 info->vers->version[0] = tt->version[0]; 440 info->vers->version[1] = tt->version[1]; 441 info->vers->version[2] = tt->version[2]; 442 info->vers->next = 0; 443 strcpy(info->vers->name, tt->name); 444 445 info->old_vers = info->vers; 446 info->vers = align_ptr(((void *) ++info->vers) + strlen(tt->name) + 1); 447 } 448 449 static int list_versions(struct dm_ioctl *param, size_t param_size) 450 { 451 size_t len, needed = 0; 452 struct dm_target_versions *vers; 453 struct vers_iter iter_info; 454 455 /* 456 * Loop through all the devices working out how much 457 * space we need. 458 */ 459 dm_target_iterate(list_version_get_needed, &needed); 460 461 /* 462 * Grab our output buffer. 463 */ 464 vers = get_result_buffer(param, param_size, &len); 465 if (len < needed) { 466 param->flags |= DM_BUFFER_FULL_FLAG; 467 goto out; 468 } 469 param->data_size = param->data_start + needed; 470 471 iter_info.param_size = param_size; 472 iter_info.old_vers = NULL; 473 iter_info.vers = vers; 474 iter_info.flags = 0; 475 iter_info.end = (char *)vers+len; 476 477 /* 478 * Now loop through filling out the names & versions. 479 */ 480 dm_target_iterate(list_version_get_info, &iter_info); 481 param->flags |= iter_info.flags; 482 483 out: 484 return 0; 485 } 486 487 488 489 static int check_name(const char *name) 490 { 491 if (strchr(name, '/')) { 492 DMWARN("invalid device name"); 493 return -EINVAL; 494 } 495 496 return 0; 497 } 498 499 /* 500 * Fills in a dm_ioctl structure, ready for sending back to 501 * userland. 502 */ 503 static int __dev_status(struct mapped_device *md, struct dm_ioctl *param) 504 { 505 struct gendisk *disk = dm_disk(md); 506 struct dm_table *table; 507 struct block_device *bdev; 508 509 param->flags &= ~(DM_SUSPEND_FLAG | DM_READONLY_FLAG | 510 DM_ACTIVE_PRESENT_FLAG); 511 512 if (dm_suspended(md)) 513 param->flags |= DM_SUSPEND_FLAG; 514 515 param->dev = huge_encode_dev(MKDEV(disk->major, disk->first_minor)); 516 517 if (!(param->flags & DM_SKIP_BDGET_FLAG)) { 518 bdev = bdget_disk(disk, 0); 519 if (!bdev) 520 return -ENXIO; 521 522 /* 523 * Yes, this will be out of date by the time it gets back 524 * to userland, but it is still very useful for 525 * debugging. 526 */ 527 param->open_count = bdev->bd_openers; 528 bdput(bdev); 529 } else 530 param->open_count = -1; 531 532 if (disk->policy) 533 param->flags |= DM_READONLY_FLAG; 534 535 param->event_nr = dm_get_event_nr(md); 536 537 table = dm_get_table(md); 538 if (table) { 539 param->flags |= DM_ACTIVE_PRESENT_FLAG; 540 param->target_count = dm_table_get_num_targets(table); 541 dm_table_put(table); 542 } else 543 param->target_count = 0; 544 545 return 0; 546 } 547 548 static int dev_create(struct dm_ioctl *param, size_t param_size) 549 { 550 int r; 551 struct mapped_device *md; 552 553 r = check_name(param->name); 554 if (r) 555 return r; 556 557 if (param->flags & DM_PERSISTENT_DEV_FLAG) 558 r = dm_create_with_minor(MINOR(huge_decode_dev(param->dev)), &md); 559 else 560 r = dm_create(&md); 561 562 if (r) 563 return r; 564 565 r = dm_hash_insert(param->name, *param->uuid ? param->uuid : NULL, md); 566 if (r) { 567 dm_put(md); 568 return r; 569 } 570 571 param->flags &= ~DM_INACTIVE_PRESENT_FLAG; 572 573 r = __dev_status(md, param); 574 dm_put(md); 575 576 return r; 577 } 578 579 /* 580 * Always use UUID for lookups if it's present, otherwise use name or dev. 581 */ 582 static inline struct hash_cell *__find_device_hash_cell(struct dm_ioctl *param) 583 { 584 if (*param->uuid) 585 return __get_uuid_cell(param->uuid); 586 else if (*param->name) 587 return __get_name_cell(param->name); 588 else 589 return dm_get_mdptr(huge_decode_dev(param->dev)); 590 } 591 592 static inline struct mapped_device *find_device(struct dm_ioctl *param) 593 { 594 struct hash_cell *hc; 595 struct mapped_device *md = NULL; 596 597 down_read(&_hash_lock); 598 hc = __find_device_hash_cell(param); 599 if (hc) { 600 md = hc->md; 601 dm_get(md); 602 603 /* 604 * Sneakily write in both the name and the uuid 605 * while we have the cell. 606 */ 607 strncpy(param->name, hc->name, sizeof(param->name)); 608 if (hc->uuid) 609 strncpy(param->uuid, hc->uuid, sizeof(param->uuid)-1); 610 else 611 param->uuid[0] = '\0'; 612 613 if (hc->new_map) 614 param->flags |= DM_INACTIVE_PRESENT_FLAG; 615 else 616 param->flags &= ~DM_INACTIVE_PRESENT_FLAG; 617 } 618 up_read(&_hash_lock); 619 620 return md; 621 } 622 623 static int dev_remove(struct dm_ioctl *param, size_t param_size) 624 { 625 struct hash_cell *hc; 626 627 down_write(&_hash_lock); 628 hc = __find_device_hash_cell(param); 629 630 if (!hc) { 631 DMWARN("device doesn't appear to be in the dev hash table."); 632 up_write(&_hash_lock); 633 return -ENXIO; 634 } 635 636 __hash_remove(hc); 637 up_write(&_hash_lock); 638 param->data_size = 0; 639 return 0; 640 } 641 642 /* 643 * Check a string doesn't overrun the chunk of 644 * memory we copied from userland. 645 */ 646 static int invalid_str(char *str, void *end) 647 { 648 while ((void *) str < end) 649 if (!*str++) 650 return 0; 651 652 return -EINVAL; 653 } 654 655 static int dev_rename(struct dm_ioctl *param, size_t param_size) 656 { 657 int r; 658 char *new_name = (char *) param + param->data_start; 659 660 if (new_name < (char *) (param + 1) || 661 invalid_str(new_name, (void *) param + param_size)) { 662 DMWARN("Invalid new logical volume name supplied."); 663 return -EINVAL; 664 } 665 666 r = check_name(new_name); 667 if (r) 668 return r; 669 670 param->data_size = 0; 671 return dm_hash_rename(param->name, new_name); 672 } 673 674 static int do_suspend(struct dm_ioctl *param) 675 { 676 int r = 0; 677 struct mapped_device *md; 678 679 md = find_device(param); 680 if (!md) 681 return -ENXIO; 682 683 if (!dm_suspended(md)) 684 r = dm_suspend(md); 685 686 if (!r) 687 r = __dev_status(md, param); 688 689 dm_put(md); 690 return r; 691 } 692 693 static int do_resume(struct dm_ioctl *param) 694 { 695 int r = 0; 696 struct hash_cell *hc; 697 struct mapped_device *md; 698 struct dm_table *new_map; 699 700 down_write(&_hash_lock); 701 702 hc = __find_device_hash_cell(param); 703 if (!hc) { 704 DMWARN("device doesn't appear to be in the dev hash table."); 705 up_write(&_hash_lock); 706 return -ENXIO; 707 } 708 709 md = hc->md; 710 dm_get(md); 711 712 new_map = hc->new_map; 713 hc->new_map = NULL; 714 param->flags &= ~DM_INACTIVE_PRESENT_FLAG; 715 716 up_write(&_hash_lock); 717 718 /* Do we need to load a new map ? */ 719 if (new_map) { 720 /* Suspend if it isn't already suspended */ 721 if (!dm_suspended(md)) 722 dm_suspend(md); 723 724 r = dm_swap_table(md, new_map); 725 if (r) { 726 dm_put(md); 727 dm_table_put(new_map); 728 return r; 729 } 730 731 if (dm_table_get_mode(new_map) & FMODE_WRITE) 732 set_disk_ro(dm_disk(md), 0); 733 else 734 set_disk_ro(dm_disk(md), 1); 735 736 dm_table_put(new_map); 737 } 738 739 if (dm_suspended(md)) 740 r = dm_resume(md); 741 742 if (!r) 743 r = __dev_status(md, param); 744 745 dm_put(md); 746 return r; 747 } 748 749 /* 750 * Set or unset the suspension state of a device. 751 * If the device already is in the requested state we just return its status. 752 */ 753 static int dev_suspend(struct dm_ioctl *param, size_t param_size) 754 { 755 if (param->flags & DM_SUSPEND_FLAG) 756 return do_suspend(param); 757 758 return do_resume(param); 759 } 760 761 /* 762 * Copies device info back to user space, used by 763 * the create and info ioctls. 764 */ 765 static int dev_status(struct dm_ioctl *param, size_t param_size) 766 { 767 int r; 768 struct mapped_device *md; 769 770 md = find_device(param); 771 if (!md) 772 return -ENXIO; 773 774 r = __dev_status(md, param); 775 dm_put(md); 776 return r; 777 } 778 779 /* 780 * Build up the status struct for each target 781 */ 782 static void retrieve_status(struct dm_table *table, 783 struct dm_ioctl *param, size_t param_size) 784 { 785 unsigned int i, num_targets; 786 struct dm_target_spec *spec; 787 char *outbuf, *outptr; 788 status_type_t type; 789 size_t remaining, len, used = 0; 790 791 outptr = outbuf = get_result_buffer(param, param_size, &len); 792 793 if (param->flags & DM_STATUS_TABLE_FLAG) 794 type = STATUSTYPE_TABLE; 795 else 796 type = STATUSTYPE_INFO; 797 798 /* Get all the target info */ 799 num_targets = dm_table_get_num_targets(table); 800 for (i = 0; i < num_targets; i++) { 801 struct dm_target *ti = dm_table_get_target(table, i); 802 803 remaining = len - (outptr - outbuf); 804 if (remaining <= sizeof(struct dm_target_spec)) { 805 param->flags |= DM_BUFFER_FULL_FLAG; 806 break; 807 } 808 809 spec = (struct dm_target_spec *) outptr; 810 811 spec->status = 0; 812 spec->sector_start = ti->begin; 813 spec->length = ti->len; 814 strncpy(spec->target_type, ti->type->name, 815 sizeof(spec->target_type)); 816 817 outptr += sizeof(struct dm_target_spec); 818 remaining = len - (outptr - outbuf); 819 if (remaining <= 0) { 820 param->flags |= DM_BUFFER_FULL_FLAG; 821 break; 822 } 823 824 /* Get the status/table string from the target driver */ 825 if (ti->type->status) { 826 if (ti->type->status(ti, type, outptr, remaining)) { 827 param->flags |= DM_BUFFER_FULL_FLAG; 828 break; 829 } 830 } else 831 outptr[0] = '\0'; 832 833 outptr += strlen(outptr) + 1; 834 used = param->data_start + (outptr - outbuf); 835 836 outptr = align_ptr(outptr); 837 spec->next = outptr - outbuf; 838 } 839 840 if (used) 841 param->data_size = used; 842 843 param->target_count = num_targets; 844 } 845 846 /* 847 * Wait for a device to report an event 848 */ 849 static int dev_wait(struct dm_ioctl *param, size_t param_size) 850 { 851 int r; 852 struct mapped_device *md; 853 struct dm_table *table; 854 855 md = find_device(param); 856 if (!md) 857 return -ENXIO; 858 859 /* 860 * Wait for a notification event 861 */ 862 if (dm_wait_event(md, param->event_nr)) { 863 r = -ERESTARTSYS; 864 goto out; 865 } 866 867 /* 868 * The userland program is going to want to know what 869 * changed to trigger the event, so we may as well tell 870 * him and save an ioctl. 871 */ 872 r = __dev_status(md, param); 873 if (r) 874 goto out; 875 876 table = dm_get_table(md); 877 if (table) { 878 retrieve_status(table, param, param_size); 879 dm_table_put(table); 880 } 881 882 out: 883 dm_put(md); 884 return r; 885 } 886 887 static inline int get_mode(struct dm_ioctl *param) 888 { 889 int mode = FMODE_READ | FMODE_WRITE; 890 891 if (param->flags & DM_READONLY_FLAG) 892 mode = FMODE_READ; 893 894 return mode; 895 } 896 897 static int next_target(struct dm_target_spec *last, uint32_t next, void *end, 898 struct dm_target_spec **spec, char **target_params) 899 { 900 *spec = (struct dm_target_spec *) ((unsigned char *) last + next); 901 *target_params = (char *) (*spec + 1); 902 903 if (*spec < (last + 1)) 904 return -EINVAL; 905 906 return invalid_str(*target_params, end); 907 } 908 909 static int populate_table(struct dm_table *table, 910 struct dm_ioctl *param, size_t param_size) 911 { 912 int r; 913 unsigned int i = 0; 914 struct dm_target_spec *spec = (struct dm_target_spec *) param; 915 uint32_t next = param->data_start; 916 void *end = (void *) param + param_size; 917 char *target_params; 918 919 if (!param->target_count) { 920 DMWARN("populate_table: no targets specified"); 921 return -EINVAL; 922 } 923 924 for (i = 0; i < param->target_count; i++) { 925 926 r = next_target(spec, next, end, &spec, &target_params); 927 if (r) { 928 DMWARN("unable to find target"); 929 return r; 930 } 931 932 r = dm_table_add_target(table, spec->target_type, 933 (sector_t) spec->sector_start, 934 (sector_t) spec->length, 935 target_params); 936 if (r) { 937 DMWARN("error adding target to table"); 938 return r; 939 } 940 941 next = spec->next; 942 } 943 944 return dm_table_complete(table); 945 } 946 947 static int table_load(struct dm_ioctl *param, size_t param_size) 948 { 949 int r; 950 struct hash_cell *hc; 951 struct dm_table *t; 952 953 r = dm_table_create(&t, get_mode(param), param->target_count); 954 if (r) 955 return r; 956 957 r = populate_table(t, param, param_size); 958 if (r) { 959 dm_table_put(t); 960 return r; 961 } 962 963 down_write(&_hash_lock); 964 hc = __find_device_hash_cell(param); 965 if (!hc) { 966 DMWARN("device doesn't appear to be in the dev hash table."); 967 up_write(&_hash_lock); 968 return -ENXIO; 969 } 970 971 if (hc->new_map) 972 dm_table_put(hc->new_map); 973 hc->new_map = t; 974 param->flags |= DM_INACTIVE_PRESENT_FLAG; 975 976 r = __dev_status(hc->md, param); 977 up_write(&_hash_lock); 978 return r; 979 } 980 981 static int table_clear(struct dm_ioctl *param, size_t param_size) 982 { 983 int r; 984 struct hash_cell *hc; 985 986 down_write(&_hash_lock); 987 988 hc = __find_device_hash_cell(param); 989 if (!hc) { 990 DMWARN("device doesn't appear to be in the dev hash table."); 991 up_write(&_hash_lock); 992 return -ENXIO; 993 } 994 995 if (hc->new_map) { 996 dm_table_put(hc->new_map); 997 hc->new_map = NULL; 998 } 999 1000 param->flags &= ~DM_INACTIVE_PRESENT_FLAG; 1001 1002 r = __dev_status(hc->md, param); 1003 up_write(&_hash_lock); 1004 return r; 1005 } 1006 1007 /* 1008 * Retrieves a list of devices used by a particular dm device. 1009 */ 1010 static void retrieve_deps(struct dm_table *table, 1011 struct dm_ioctl *param, size_t param_size) 1012 { 1013 unsigned int count = 0; 1014 struct list_head *tmp; 1015 size_t len, needed; 1016 struct dm_dev *dd; 1017 struct dm_target_deps *deps; 1018 1019 deps = get_result_buffer(param, param_size, &len); 1020 1021 /* 1022 * Count the devices. 1023 */ 1024 list_for_each (tmp, dm_table_get_devices(table)) 1025 count++; 1026 1027 /* 1028 * Check we have enough space. 1029 */ 1030 needed = sizeof(*deps) + (sizeof(*deps->dev) * count); 1031 if (len < needed) { 1032 param->flags |= DM_BUFFER_FULL_FLAG; 1033 return; 1034 } 1035 1036 /* 1037 * Fill in the devices. 1038 */ 1039 deps->count = count; 1040 count = 0; 1041 list_for_each_entry (dd, dm_table_get_devices(table), list) 1042 deps->dev[count++] = huge_encode_dev(dd->bdev->bd_dev); 1043 1044 param->data_size = param->data_start + needed; 1045 } 1046 1047 static int table_deps(struct dm_ioctl *param, size_t param_size) 1048 { 1049 int r = 0; 1050 struct mapped_device *md; 1051 struct dm_table *table; 1052 1053 md = find_device(param); 1054 if (!md) 1055 return -ENXIO; 1056 1057 r = __dev_status(md, param); 1058 if (r) 1059 goto out; 1060 1061 table = dm_get_table(md); 1062 if (table) { 1063 retrieve_deps(table, param, param_size); 1064 dm_table_put(table); 1065 } 1066 1067 out: 1068 dm_put(md); 1069 return r; 1070 } 1071 1072 /* 1073 * Return the status of a device as a text string for each 1074 * target. 1075 */ 1076 static int table_status(struct dm_ioctl *param, size_t param_size) 1077 { 1078 int r; 1079 struct mapped_device *md; 1080 struct dm_table *table; 1081 1082 md = find_device(param); 1083 if (!md) 1084 return -ENXIO; 1085 1086 r = __dev_status(md, param); 1087 if (r) 1088 goto out; 1089 1090 table = dm_get_table(md); 1091 if (table) { 1092 retrieve_status(table, param, param_size); 1093 dm_table_put(table); 1094 } 1095 1096 out: 1097 dm_put(md); 1098 return r; 1099 } 1100 1101 /* 1102 * Pass a message to the target that's at the supplied device offset. 1103 */ 1104 static int target_message(struct dm_ioctl *param, size_t param_size) 1105 { 1106 int r, argc; 1107 char **argv; 1108 struct mapped_device *md; 1109 struct dm_table *table; 1110 struct dm_target *ti; 1111 struct dm_target_msg *tmsg = (void *) param + param->data_start; 1112 1113 md = find_device(param); 1114 if (!md) 1115 return -ENXIO; 1116 1117 r = __dev_status(md, param); 1118 if (r) 1119 goto out; 1120 1121 if (tmsg < (struct dm_target_msg *) (param + 1) || 1122 invalid_str(tmsg->message, (void *) param + param_size)) { 1123 DMWARN("Invalid target message parameters."); 1124 r = -EINVAL; 1125 goto out; 1126 } 1127 1128 r = dm_split_args(&argc, &argv, tmsg->message); 1129 if (r) { 1130 DMWARN("Failed to split target message parameters"); 1131 goto out; 1132 } 1133 1134 table = dm_get_table(md); 1135 if (!table) 1136 goto out_argv; 1137 1138 if (tmsg->sector >= dm_table_get_size(table)) { 1139 DMWARN("Target message sector outside device."); 1140 r = -EINVAL; 1141 goto out_table; 1142 } 1143 1144 ti = dm_table_find_target(table, tmsg->sector); 1145 if (ti->type->message) 1146 r = ti->type->message(ti, argc, argv); 1147 else { 1148 DMWARN("Target type does not support messages"); 1149 r = -EINVAL; 1150 } 1151 1152 out_table: 1153 dm_table_put(table); 1154 out_argv: 1155 kfree(argv); 1156 out: 1157 param->data_size = 0; 1158 dm_put(md); 1159 return r; 1160 } 1161 1162 /*----------------------------------------------------------------- 1163 * Implementation of open/close/ioctl on the special char 1164 * device. 1165 *---------------------------------------------------------------*/ 1166 static ioctl_fn lookup_ioctl(unsigned int cmd) 1167 { 1168 static struct { 1169 int cmd; 1170 ioctl_fn fn; 1171 } _ioctls[] = { 1172 {DM_VERSION_CMD, NULL}, /* version is dealt with elsewhere */ 1173 {DM_REMOVE_ALL_CMD, remove_all}, 1174 {DM_LIST_DEVICES_CMD, list_devices}, 1175 1176 {DM_DEV_CREATE_CMD, dev_create}, 1177 {DM_DEV_REMOVE_CMD, dev_remove}, 1178 {DM_DEV_RENAME_CMD, dev_rename}, 1179 {DM_DEV_SUSPEND_CMD, dev_suspend}, 1180 {DM_DEV_STATUS_CMD, dev_status}, 1181 {DM_DEV_WAIT_CMD, dev_wait}, 1182 1183 {DM_TABLE_LOAD_CMD, table_load}, 1184 {DM_TABLE_CLEAR_CMD, table_clear}, 1185 {DM_TABLE_DEPS_CMD, table_deps}, 1186 {DM_TABLE_STATUS_CMD, table_status}, 1187 1188 {DM_LIST_VERSIONS_CMD, list_versions}, 1189 1190 {DM_TARGET_MSG_CMD, target_message} 1191 }; 1192 1193 return (cmd >= ARRAY_SIZE(_ioctls)) ? NULL : _ioctls[cmd].fn; 1194 } 1195 1196 /* 1197 * As well as checking the version compatibility this always 1198 * copies the kernel interface version out. 1199 */ 1200 static int check_version(unsigned int cmd, struct dm_ioctl __user *user) 1201 { 1202 uint32_t version[3]; 1203 int r = 0; 1204 1205 if (copy_from_user(version, user->version, sizeof(version))) 1206 return -EFAULT; 1207 1208 if ((DM_VERSION_MAJOR != version[0]) || 1209 (DM_VERSION_MINOR < version[1])) { 1210 DMWARN("ioctl interface mismatch: " 1211 "kernel(%u.%u.%u), user(%u.%u.%u), cmd(%d)", 1212 DM_VERSION_MAJOR, DM_VERSION_MINOR, 1213 DM_VERSION_PATCHLEVEL, 1214 version[0], version[1], version[2], cmd); 1215 r = -EINVAL; 1216 } 1217 1218 /* 1219 * Fill in the kernel version. 1220 */ 1221 version[0] = DM_VERSION_MAJOR; 1222 version[1] = DM_VERSION_MINOR; 1223 version[2] = DM_VERSION_PATCHLEVEL; 1224 if (copy_to_user(user->version, version, sizeof(version))) 1225 return -EFAULT; 1226 1227 return r; 1228 } 1229 1230 static void free_params(struct dm_ioctl *param) 1231 { 1232 vfree(param); 1233 } 1234 1235 static int copy_params(struct dm_ioctl __user *user, struct dm_ioctl **param) 1236 { 1237 struct dm_ioctl tmp, *dmi; 1238 1239 if (copy_from_user(&tmp, user, sizeof(tmp))) 1240 return -EFAULT; 1241 1242 if (tmp.data_size < sizeof(tmp)) 1243 return -EINVAL; 1244 1245 dmi = (struct dm_ioctl *) vmalloc(tmp.data_size); 1246 if (!dmi) 1247 return -ENOMEM; 1248 1249 if (copy_from_user(dmi, user, tmp.data_size)) { 1250 vfree(dmi); 1251 return -EFAULT; 1252 } 1253 1254 *param = dmi; 1255 return 0; 1256 } 1257 1258 static int validate_params(uint cmd, struct dm_ioctl *param) 1259 { 1260 /* Always clear this flag */ 1261 param->flags &= ~DM_BUFFER_FULL_FLAG; 1262 1263 /* Ignores parameters */ 1264 if (cmd == DM_REMOVE_ALL_CMD || 1265 cmd == DM_LIST_DEVICES_CMD || 1266 cmd == DM_LIST_VERSIONS_CMD) 1267 return 0; 1268 1269 if ((cmd == DM_DEV_CREATE_CMD)) { 1270 if (!*param->name) { 1271 DMWARN("name not supplied when creating device"); 1272 return -EINVAL; 1273 } 1274 } else if ((*param->uuid && *param->name)) { 1275 DMWARN("only supply one of name or uuid, cmd(%u)", cmd); 1276 return -EINVAL; 1277 } 1278 1279 /* Ensure strings are terminated */ 1280 param->name[DM_NAME_LEN - 1] = '\0'; 1281 param->uuid[DM_UUID_LEN - 1] = '\0'; 1282 1283 return 0; 1284 } 1285 1286 static int ctl_ioctl(struct inode *inode, struct file *file, 1287 uint command, ulong u) 1288 { 1289 int r = 0; 1290 unsigned int cmd; 1291 struct dm_ioctl *param; 1292 struct dm_ioctl __user *user = (struct dm_ioctl __user *) u; 1293 ioctl_fn fn = NULL; 1294 size_t param_size; 1295 1296 /* only root can play with this */ 1297 if (!capable(CAP_SYS_ADMIN)) 1298 return -EACCES; 1299 1300 if (_IOC_TYPE(command) != DM_IOCTL) 1301 return -ENOTTY; 1302 1303 cmd = _IOC_NR(command); 1304 1305 /* 1306 * Check the interface version passed in. This also 1307 * writes out the kernel's interface version. 1308 */ 1309 r = check_version(cmd, user); 1310 if (r) 1311 return r; 1312 1313 /* 1314 * Nothing more to do for the version command. 1315 */ 1316 if (cmd == DM_VERSION_CMD) 1317 return 0; 1318 1319 fn = lookup_ioctl(cmd); 1320 if (!fn) { 1321 DMWARN("dm_ctl_ioctl: unknown command 0x%x", command); 1322 return -ENOTTY; 1323 } 1324 1325 /* 1326 * Trying to avoid low memory issues when a device is 1327 * suspended. 1328 */ 1329 current->flags |= PF_MEMALLOC; 1330 1331 /* 1332 * Copy the parameters into kernel space. 1333 */ 1334 r = copy_params(user, ¶m); 1335 if (r) { 1336 current->flags &= ~PF_MEMALLOC; 1337 return r; 1338 } 1339 1340 /* 1341 * FIXME: eventually we will remove the PF_MEMALLOC flag 1342 * here. However the tools still do nasty things like 1343 * 'load' while a device is suspended. 1344 */ 1345 1346 r = validate_params(cmd, param); 1347 if (r) 1348 goto out; 1349 1350 param_size = param->data_size; 1351 param->data_size = sizeof(*param); 1352 r = fn(param, param_size); 1353 1354 /* 1355 * Copy the results back to userland. 1356 */ 1357 if (!r && copy_to_user(user, param, param->data_size)) 1358 r = -EFAULT; 1359 1360 out: 1361 free_params(param); 1362 current->flags &= ~PF_MEMALLOC; 1363 return r; 1364 } 1365 1366 static struct file_operations _ctl_fops = { 1367 .ioctl = ctl_ioctl, 1368 .owner = THIS_MODULE, 1369 }; 1370 1371 static struct miscdevice _dm_misc = { 1372 .minor = MISC_DYNAMIC_MINOR, 1373 .name = DM_NAME, 1374 .devfs_name = "mapper/control", 1375 .fops = &_ctl_fops 1376 }; 1377 1378 /* 1379 * Create misc character device and link to DM_DIR/control. 1380 */ 1381 int __init dm_interface_init(void) 1382 { 1383 int r; 1384 1385 r = dm_hash_init(); 1386 if (r) 1387 return r; 1388 1389 r = misc_register(&_dm_misc); 1390 if (r) { 1391 DMERR("misc_register failed for control device"); 1392 dm_hash_exit(); 1393 return r; 1394 } 1395 1396 DMINFO("%d.%d.%d%s initialised: %s", DM_VERSION_MAJOR, 1397 DM_VERSION_MINOR, DM_VERSION_PATCHLEVEL, DM_VERSION_EXTRA, 1398 DM_DRIVER_EMAIL); 1399 return 0; 1400 } 1401 1402 void dm_interface_exit(void) 1403 { 1404 if (misc_deregister(&_dm_misc) < 0) 1405 DMERR("misc_deregister failed for control device"); 1406 1407 dm_hash_exit(); 1408 } 1409