1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2001, 2002 Sistina Software (UK) Limited. 4 * Copyright (C) 2004 - 2006 Red Hat, Inc. All rights reserved. 5 * 6 * This file is released under the GPL. 7 */ 8 9 #include "dm-core.h" 10 #include "dm-ima.h" 11 #include <linux/module.h> 12 #include <linux/vmalloc.h> 13 #include <linux/miscdevice.h> 14 #include <linux/sched/mm.h> 15 #include <linux/init.h> 16 #include <linux/wait.h> 17 #include <linux/slab.h> 18 #include <linux/rbtree.h> 19 #include <linux/dm-ioctl.h> 20 #include <linux/hdreg.h> 21 #include <linux/compat.h> 22 #include <linux/nospec.h> 23 24 #include <linux/uaccess.h> 25 #include <linux/ima.h> 26 27 #define DM_MSG_PREFIX "ioctl" 28 #define DM_DRIVER_EMAIL "dm-devel@lists.linux.dev" 29 30 struct dm_file { 31 /* 32 * poll will wait until the global event number is greater than 33 * this value. 34 */ 35 volatile unsigned int global_event_nr; 36 }; 37 38 /* 39 *--------------------------------------------------------------- 40 * The ioctl interface needs to be able to look up devices by 41 * name or uuid. 42 *--------------------------------------------------------------- 43 */ 44 struct hash_cell { 45 struct rb_node name_node; 46 struct rb_node uuid_node; 47 bool name_set; 48 bool uuid_set; 49 50 char *name; 51 char *uuid; 52 struct mapped_device *md; 53 struct dm_table *new_map; 54 }; 55 56 struct vers_iter { 57 struct dm_target_versions *vers, *old_vers; 58 char *end; 59 }; 60 61 62 static struct rb_root name_rb_tree = RB_ROOT; 63 static struct rb_root uuid_rb_tree = RB_ROOT; 64 65 #define DM_REMOVE_KEEP_OPEN_DEVICES 1 66 #define DM_REMOVE_MARK_DEFERRED 2 67 #define DM_REMOVE_ONLY_DEFERRED 4 68 #define DM_REMOVE_INTERRUPTIBLE 8 69 static int dm_hash_remove_all(unsigned flags); 70 71 /* 72 * Guards access to both hash tables. 73 */ 74 static DECLARE_RWSEM(_hash_lock); 75 76 /* 77 * Protects use of mdptr to obtain hash cell name and uuid from mapped device. 78 */ 79 static DEFINE_MUTEX(dm_hash_cells_mutex); 80 81 static void dm_hash_exit(void) 82 { 83 dm_hash_remove_all(0); 84 } 85 86 /* 87 *--------------------------------------------------------------- 88 * Code for looking up a device by name 89 *--------------------------------------------------------------- 90 */ 91 static struct hash_cell *__get_name_cell(const char *str) 92 { 93 struct rb_node *n = name_rb_tree.rb_node; 94 95 while (n) { 96 struct hash_cell *hc = container_of(n, struct hash_cell, name_node); 97 int c; 98 99 c = strcmp(hc->name, str); 100 if (!c) { 101 dm_get(hc->md); 102 return hc; 103 } 104 n = c >= 0 ? n->rb_left : n->rb_right; 105 } 106 107 return NULL; 108 } 109 110 static struct hash_cell *__get_uuid_cell(const char *str) 111 { 112 struct rb_node *n = uuid_rb_tree.rb_node; 113 114 while (n) { 115 struct hash_cell *hc = container_of(n, struct hash_cell, uuid_node); 116 int c; 117 118 c = strcmp(hc->uuid, str); 119 if (!c) { 120 dm_get(hc->md); 121 return hc; 122 } 123 n = c >= 0 ? n->rb_left : n->rb_right; 124 } 125 126 return NULL; 127 } 128 129 static void __unlink_name(struct hash_cell *hc) 130 { 131 if (hc->name_set) { 132 hc->name_set = false; 133 rb_erase(&hc->name_node, &name_rb_tree); 134 } 135 } 136 137 static void __unlink_uuid(struct hash_cell *hc) 138 { 139 if (hc->uuid_set) { 140 hc->uuid_set = false; 141 rb_erase(&hc->uuid_node, &uuid_rb_tree); 142 } 143 } 144 145 static void __link_name(struct hash_cell *new_hc) 146 { 147 struct rb_node **n, *parent; 148 149 __unlink_name(new_hc); 150 151 new_hc->name_set = true; 152 153 n = &name_rb_tree.rb_node; 154 parent = NULL; 155 156 while (*n) { 157 struct hash_cell *hc = container_of(*n, struct hash_cell, name_node); 158 int c; 159 160 c = strcmp(hc->name, new_hc->name); 161 BUG_ON(!c); 162 parent = *n; 163 n = c >= 0 ? &hc->name_node.rb_left : &hc->name_node.rb_right; 164 } 165 166 rb_link_node(&new_hc->name_node, parent, n); 167 rb_insert_color(&new_hc->name_node, &name_rb_tree); 168 } 169 170 static void __link_uuid(struct hash_cell *new_hc) 171 { 172 struct rb_node **n, *parent; 173 174 __unlink_uuid(new_hc); 175 176 new_hc->uuid_set = true; 177 178 n = &uuid_rb_tree.rb_node; 179 parent = NULL; 180 181 while (*n) { 182 struct hash_cell *hc = container_of(*n, struct hash_cell, uuid_node); 183 int c; 184 185 c = strcmp(hc->uuid, new_hc->uuid); 186 BUG_ON(!c); 187 parent = *n; 188 n = c > 0 ? &hc->uuid_node.rb_left : &hc->uuid_node.rb_right; 189 } 190 191 rb_link_node(&new_hc->uuid_node, parent, n); 192 rb_insert_color(&new_hc->uuid_node, &uuid_rb_tree); 193 } 194 195 static struct hash_cell *__get_dev_cell(uint64_t dev) 196 { 197 struct mapped_device *md; 198 struct hash_cell *hc; 199 200 md = dm_get_md(huge_decode_dev(dev)); 201 if (!md) 202 return NULL; 203 204 hc = dm_get_mdptr(md); 205 if (!hc) { 206 dm_put(md); 207 return NULL; 208 } 209 210 return hc; 211 } 212 213 /* 214 *--------------------------------------------------------------- 215 * Inserting, removing and renaming a device. 216 *--------------------------------------------------------------- 217 */ 218 static struct hash_cell *alloc_cell(const char *name, const char *uuid, 219 struct mapped_device *md) 220 { 221 struct hash_cell *hc; 222 223 hc = kmalloc_obj(*hc); 224 if (!hc) 225 return NULL; 226 227 hc->name = kstrdup(name, GFP_KERNEL); 228 if (!hc->name) { 229 kfree(hc); 230 return NULL; 231 } 232 233 if (!uuid) 234 hc->uuid = NULL; 235 236 else { 237 hc->uuid = kstrdup(uuid, GFP_KERNEL); 238 if (!hc->uuid) { 239 kfree(hc->name); 240 kfree(hc); 241 return NULL; 242 } 243 } 244 245 hc->name_set = hc->uuid_set = false; 246 hc->md = md; 247 hc->new_map = NULL; 248 return hc; 249 } 250 251 static void free_cell(struct hash_cell *hc) 252 { 253 if (hc) { 254 kfree(hc->name); 255 kfree(hc->uuid); 256 kfree(hc); 257 } 258 } 259 260 #ifdef CONFIG_IMA 261 262 /* 263 * Called while holding to _hash_lock, to guarantee the ordering of the 264 * following dm_ima_measure_on_* functions, which should be called 265 * right after dropping the _hash_lock 266 */ 267 static unsigned int dm_ima_init_context(struct hash_cell *hc, 268 struct dm_ima_context *context, 269 bool need_idx) 270 { 271 lockdep_assert_held(&_hash_lock); 272 273 if (unlikely(!context)) 274 return need_idx ? hc->md->ima.update_idx++ : 0; 275 276 context->update_idx = hc->md->ima.update_idx++; 277 strcpy(context->dev_name, hc->name); 278 strcpy(context->dev_uuid, hc->uuid ? : ""); 279 280 return context->update_idx; 281 } 282 283 /* 284 * Called by do_resume() to guarantee correct ordering, since do_resume() 285 * does not grab the _hash_lock when the table is not getting swapped or 286 * when actually swapping the active table 287 */ 288 static bool dm_ima_need_measure(struct mapped_device *md, 289 struct dm_table *table, 290 struct dm_ima_context *context) 291 { 292 int srcu_idx; 293 struct hash_cell *hc; 294 bool need_measure = false; 295 296 if (unlikely(!context)) 297 return false; 298 299 down_write(&_hash_lock); 300 /* Check if the device has been removed */ 301 hc = dm_get_mdptr(md); 302 if (hc) { 303 /* 304 * If we have a table, we need to make sure that it's the 305 * active table. Otherwise we raced with another process 306 * setting the active table and it will do the measurement 307 */ 308 if (!table || dm_get_live_table(md, &srcu_idx) == table) { 309 dm_ima_init_context(hc, context, false); 310 need_measure = true; 311 } 312 if (table) 313 dm_put_live_table(md, srcu_idx); 314 } 315 up_write(&_hash_lock); 316 317 return need_measure; 318 } 319 #else 320 static inline unsigned int dm_ima_init_context(struct hash_cell *hc, 321 struct dm_ima_context *context, 322 bool neex_idx) 323 { 324 return 0; 325 } 326 static inline bool dm_ima_need_measure(struct mapped_device *md, 327 struct dm_table *table, 328 struct dm_ima_context *context) 329 { 330 return false; 331 } 332 #endif 333 334 /* 335 * The kdev_t and uuid of a device can never change once it is 336 * initially inserted. 337 */ 338 static int dm_hash_insert(const char *name, const char *uuid, struct mapped_device *md) 339 { 340 struct hash_cell *cell, *hc; 341 342 /* 343 * Allocate the new cells. 344 */ 345 cell = alloc_cell(name, uuid, md); 346 if (!cell) 347 return -ENOMEM; 348 349 /* 350 * Insert the cell into both hash tables. 351 */ 352 down_write(&_hash_lock); 353 hc = __get_name_cell(name); 354 if (hc) { 355 dm_put(hc->md); 356 goto bad; 357 } 358 359 __link_name(cell); 360 361 if (uuid) { 362 hc = __get_uuid_cell(uuid); 363 if (hc) { 364 __unlink_name(cell); 365 dm_put(hc->md); 366 goto bad; 367 } 368 __link_uuid(cell); 369 } 370 dm_get(md); 371 mutex_lock(&dm_hash_cells_mutex); 372 dm_set_mdptr(md, cell); 373 mutex_unlock(&dm_hash_cells_mutex); 374 up_write(&_hash_lock); 375 376 return 0; 377 378 bad: 379 up_write(&_hash_lock); 380 free_cell(cell); 381 return -EBUSY; 382 } 383 384 static struct dm_table *__hash_remove(struct hash_cell *hc) 385 { 386 struct dm_table *table; 387 int srcu_idx; 388 389 lockdep_assert_held(&_hash_lock); 390 391 /* remove from the dev trees */ 392 __unlink_name(hc); 393 __unlink_uuid(hc); 394 mutex_lock(&dm_hash_cells_mutex); 395 dm_set_mdptr(hc->md, NULL); 396 mutex_unlock(&dm_hash_cells_mutex); 397 398 table = dm_get_live_table(hc->md, &srcu_idx); 399 if (table) 400 dm_table_event(table); 401 dm_put_live_table(hc->md, srcu_idx); 402 403 table = NULL; 404 if (hc->new_map) 405 table = hc->new_map; 406 dm_put(hc->md); 407 free_cell(hc); 408 409 return table; 410 } 411 412 static int dm_hash_remove_all(unsigned flags) 413 { 414 int dev_skipped; 415 struct rb_node *n; 416 struct hash_cell *hc; 417 struct mapped_device *md; 418 struct dm_table *t; 419 struct dm_ima_context *ima_context = NULL; 420 unsigned int ima_idx; 421 422 dm_ima_alloc_context(&ima_context, true); 423 retry: 424 dev_skipped = 0; 425 426 down_write(&_hash_lock); 427 428 for (n = rb_first(&name_rb_tree); n; n = rb_next(n)) { 429 if (flags & DM_REMOVE_INTERRUPTIBLE && fatal_signal_pending(current)) { 430 up_write(&_hash_lock); 431 dm_ima_free_context(ima_context); 432 return -EINTR; 433 } 434 435 hc = container_of(n, struct hash_cell, name_node); 436 md = hc->md; 437 dm_get(md); 438 439 if (flags & DM_REMOVE_KEEP_OPEN_DEVICES && 440 dm_lock_for_deletion(md, !!(flags & DM_REMOVE_MARK_DEFERRED), !!(flags & DM_REMOVE_ONLY_DEFERRED))) { 441 dm_put(md); 442 dev_skipped++; 443 continue; 444 } 445 446 ima_idx = dm_ima_init_context(hc, ima_context, true); 447 t = __hash_remove(hc); 448 449 up_write(&_hash_lock); 450 451 if (t) { 452 dm_sync_table(md); 453 dm_table_destroy(t); 454 } 455 dm_ima_measure_on_device_remove(md, true, ima_context, ima_idx); 456 dm_put(md); 457 if (likely(flags & DM_REMOVE_KEEP_OPEN_DEVICES)) 458 dm_destroy(md); 459 else 460 dm_destroy_immediate(md); 461 462 /* 463 * Some mapped devices may be using other mapped 464 * devices, so repeat until we make no further 465 * progress. If a new mapped device is created 466 * here it will also get removed. 467 */ 468 goto retry; 469 } 470 471 up_write(&_hash_lock); 472 473 if (dev_skipped && !(flags & DM_REMOVE_ONLY_DEFERRED)) 474 DMWARN("remove_all left %d open device(s)", dev_skipped); 475 476 dm_ima_free_context(ima_context); 477 return 0; 478 } 479 480 /* 481 * Set the uuid of a hash_cell that isn't already set. 482 */ 483 static void __set_cell_uuid(struct hash_cell *hc, char *new_uuid) 484 { 485 mutex_lock(&dm_hash_cells_mutex); 486 hc->uuid = new_uuid; 487 mutex_unlock(&dm_hash_cells_mutex); 488 489 __link_uuid(hc); 490 } 491 492 /* 493 * Changes the name of a hash_cell and returns the old name for 494 * the caller to free. 495 */ 496 static char *__change_cell_name(struct hash_cell *hc, char *new_name) 497 { 498 char *old_name; 499 500 /* 501 * Rename and move the name cell. 502 */ 503 __unlink_name(hc); 504 old_name = hc->name; 505 506 mutex_lock(&dm_hash_cells_mutex); 507 hc->name = new_name; 508 mutex_unlock(&dm_hash_cells_mutex); 509 510 __link_name(hc); 511 512 return old_name; 513 } 514 515 static struct mapped_device *dm_hash_rename(struct dm_ioctl *param, 516 const char *new) 517 { 518 char *new_data, *old_name = NULL; 519 struct hash_cell *hc; 520 struct dm_table *table; 521 struct mapped_device *md; 522 unsigned int change_uuid = (param->flags & DM_UUID_FLAG) ? 1 : 0; 523 int srcu_idx; 524 struct dm_ima_context *ima_context = NULL; 525 526 /* 527 * duplicate new. 528 */ 529 new_data = kstrdup(new, GFP_KERNEL); 530 if (!new_data) 531 return ERR_PTR(-ENOMEM); 532 533 dm_ima_alloc_context(&ima_context, true); 534 down_write(&_hash_lock); 535 536 /* 537 * Is new free ? 538 */ 539 if (change_uuid) 540 hc = __get_uuid_cell(new); 541 else 542 hc = __get_name_cell(new); 543 544 if (hc) { 545 DMERR("Unable to change %s on mapped device %s to one that already exists: %s", 546 change_uuid ? "uuid" : "name", 547 param->name, new); 548 dm_put(hc->md); 549 up_write(&_hash_lock); 550 dm_ima_free_context(ima_context); 551 kfree(new_data); 552 return ERR_PTR(-EBUSY); 553 } 554 555 /* 556 * Is there such a device as 'old' ? 557 */ 558 hc = __get_name_cell(param->name); 559 if (!hc) { 560 DMERR("Unable to rename non-existent device, %s to %s%s", 561 param->name, change_uuid ? "uuid " : "", new); 562 up_write(&_hash_lock); 563 dm_ima_free_context(ima_context); 564 kfree(new_data); 565 return ERR_PTR(-ENXIO); 566 } 567 568 /* 569 * Does this device already have a uuid? 570 */ 571 if (change_uuid && hc->uuid) { 572 DMERR("Unable to change uuid of mapped device %s to %s " 573 "because uuid is already set to %s", 574 param->name, new, hc->uuid); 575 dm_put(hc->md); 576 up_write(&_hash_lock); 577 dm_ima_free_context(ima_context); 578 kfree(new_data); 579 return ERR_PTR(-EINVAL); 580 } 581 582 if (change_uuid) 583 __set_cell_uuid(hc, new_data); 584 else 585 old_name = __change_cell_name(hc, new_data); 586 587 /* 588 * Wake up any dm event waiters. 589 */ 590 table = dm_get_live_table(hc->md, &srcu_idx); 591 if (table) 592 dm_table_event(table); 593 dm_put_live_table(hc->md, srcu_idx); 594 595 if (!dm_kobject_uevent(hc->md, KOBJ_CHANGE, param->event_nr, false)) 596 param->flags |= DM_UEVENT_GENERATED_FLAG; 597 598 md = hc->md; 599 600 dm_ima_init_context(hc, ima_context, false); 601 602 up_write(&_hash_lock); 603 dm_ima_measure_on_device_rename(md, ima_context); 604 dm_ima_free_context(ima_context); 605 kfree(old_name); 606 607 return md; 608 } 609 610 void dm_deferred_remove(void) 611 { 612 dm_hash_remove_all(DM_REMOVE_KEEP_OPEN_DEVICES | DM_REMOVE_ONLY_DEFERRED); 613 } 614 615 /* 616 *--------------------------------------------------------------- 617 * Implementation of the ioctl commands 618 *--------------------------------------------------------------- 619 */ 620 /* 621 * All the ioctl commands get dispatched to functions with this 622 * prototype. 623 */ 624 typedef int (*ioctl_fn)(struct file *filp, struct dm_ioctl *param, size_t param_size); 625 626 static int remove_all(struct file *filp, struct dm_ioctl *param, size_t param_size) 627 { 628 int r; 629 int flags = DM_REMOVE_KEEP_OPEN_DEVICES | DM_REMOVE_INTERRUPTIBLE; 630 if (param->flags & DM_DEFERRED_REMOVE) 631 flags |= DM_REMOVE_MARK_DEFERRED; 632 r = dm_hash_remove_all(flags); 633 param->data_size = 0; 634 return r; 635 } 636 637 /* 638 * Round up the ptr to an 8-byte boundary. 639 */ 640 #define ALIGN_MASK 7 641 static inline size_t align_val(size_t val) 642 { 643 return (val + ALIGN_MASK) & ~ALIGN_MASK; 644 } 645 static inline void *align_ptr(void *ptr) 646 { 647 return (void *)align_val((size_t)ptr); 648 } 649 650 /* 651 * Retrieves the data payload buffer from an already allocated 652 * struct dm_ioctl. 653 */ 654 static void *get_result_buffer(struct dm_ioctl *param, size_t param_size, 655 size_t *len) 656 { 657 param->data_start = align_ptr(param + 1) - (void *) param; 658 659 if (param->data_start < param_size) 660 *len = param_size - param->data_start; 661 else 662 *len = 0; 663 664 return ((void *) param) + param->data_start; 665 } 666 667 static bool filter_device(struct hash_cell *hc, const char *pfx_name, const char *pfx_uuid) 668 { 669 const char *val; 670 size_t val_len, pfx_len; 671 672 val = hc->name; 673 val_len = strlen(val); 674 pfx_len = strnlen(pfx_name, DM_NAME_LEN); 675 if (pfx_len > val_len) 676 return false; 677 if (memcmp(val, pfx_name, pfx_len)) 678 return false; 679 680 val = hc->uuid ? hc->uuid : ""; 681 val_len = strlen(val); 682 pfx_len = strnlen(pfx_uuid, DM_UUID_LEN); 683 if (pfx_len > val_len) 684 return false; 685 if (memcmp(val, pfx_uuid, pfx_len)) 686 return false; 687 688 return true; 689 } 690 691 static int list_devices(struct file *filp, struct dm_ioctl *param, size_t param_size) 692 { 693 struct rb_node *n; 694 struct hash_cell *hc; 695 size_t len; 696 struct dm_name_list *nl, *old_nl = NULL; 697 void *result_start, *result_limit; 698 uint32_t *event_nr; 699 700 /* 701 * Grab our output buffer. 702 */ 703 nl = result_start = get_result_buffer(param, param_size, &len); 704 result_limit = result_start + len; 705 706 if (len >= sizeof(*nl)) 707 nl->dev = 0; /* Flags no data */ 708 709 down_write(&_hash_lock); 710 711 /* 712 * Loop through filling out the names. 713 */ 714 for (n = rb_first(&name_rb_tree); n; n = rb_next(n)) { 715 void *next_nl; 716 717 hc = container_of(n, struct hash_cell, name_node); 718 if (!filter_device(hc, param->name, param->uuid)) 719 continue; 720 721 len = strlen(hc->name); 722 event_nr = align_ptr(nl->name + len + 1); 723 next_nl = event_nr + 2; 724 if (next_nl > result_limit) 725 break; 726 727 ((u64 *)event_nr)[-1] = 0; 728 memcpy(nl->name, hc->name, len); 729 730 nl->dev = huge_encode_dev(disk_devt(dm_disk(hc->md))); 731 732 event_nr[0] = dm_get_event_nr(hc->md); 733 event_nr[1] = 0; 734 735 if (param->flags & DM_UUID_FLAG) { 736 if (hc->uuid) { 737 len = strlen(hc->uuid); 738 next_nl = align_ptr(next_nl + len + 1); 739 if (next_nl > result_limit) 740 break; 741 event_nr[1] |= DM_NAME_LIST_FLAG_HAS_UUID; 742 ((u64 *)next_nl)[-1] = 0; 743 memcpy(event_nr + 2, hc->uuid, len); 744 } else { 745 event_nr[1] |= DM_NAME_LIST_FLAG_DOESNT_HAVE_UUID; 746 } 747 } 748 nl->next = next_nl - (void *)nl; 749 old_nl = nl; 750 nl = next_nl; 751 } 752 753 if (old_nl) 754 old_nl->next = 0; 755 756 if (n) 757 param->flags |= DM_BUFFER_FULL_FLAG; 758 else 759 param->data_size = param->data_start + ((void *)nl - result_start); 760 761 up_write(&_hash_lock); 762 return 0; 763 } 764 765 static void list_version_get_info(struct target_type *tt, void *param) 766 { 767 struct vers_iter *info = param; 768 struct dm_target_versions *vers = info->vers; 769 size_t name_len = strlen(tt->name); 770 771 if (!vers) 772 return; 773 774 info->old_vers = vers; 775 info->vers = align_ptr((void *)(info->vers + 1) + name_len + 1); 776 777 /* Check space */ 778 if ((char *)info->vers > info->end) { 779 info->vers = NULL; 780 return; 781 } 782 783 /* Zero padding and terminate vers->name[] */ 784 ((u64 *)info->vers)[-1] = 0; 785 786 vers->next = (char *)info->vers - (char *)vers; 787 788 vers->version[0] = tt->version[0]; 789 vers->version[1] = tt->version[1]; 790 vers->version[2] = tt->version[2]; 791 memcpy(vers->name, tt->name, name_len); 792 } 793 794 static int __list_versions(struct dm_ioctl *param, size_t param_size, const char *name) 795 { 796 size_t len; 797 struct dm_target_versions *vers; 798 struct vers_iter iter_info; 799 struct target_type *tt = NULL; 800 801 if (name) { 802 tt = dm_get_target_type(name); 803 if (!tt) 804 return -EINVAL; 805 } 806 807 /* 808 * Grab our output buffer. 809 */ 810 vers = get_result_buffer(param, param_size, &len); 811 812 iter_info.old_vers = NULL; 813 iter_info.vers = vers; 814 iter_info.end = (char *)vers + len; 815 816 /* 817 * Loop through filling out the names & versions. 818 */ 819 if (!tt) 820 dm_target_iterate(list_version_get_info, &iter_info); 821 else 822 list_version_get_info(tt, &iter_info); 823 824 if (iter_info.vers) { 825 if (iter_info.old_vers) 826 iter_info.old_vers->next = 0; 827 param->data_size = param->data_start + ((char *)iter_info.vers - (char *)vers); 828 } else { 829 param->flags |= DM_BUFFER_FULL_FLAG; 830 } 831 832 if (tt) 833 dm_put_target_type(tt); 834 return 0; 835 } 836 837 static int list_versions(struct file *filp, struct dm_ioctl *param, size_t param_size) 838 { 839 return __list_versions(param, param_size, NULL); 840 } 841 842 static int get_target_version(struct file *filp, struct dm_ioctl *param, size_t param_size) 843 { 844 return __list_versions(param, param_size, param->name); 845 } 846 847 static int check_name(const char *name) 848 { 849 if (strchr(name, '/')) { 850 DMERR("device name cannot contain '/'"); 851 return -EINVAL; 852 } 853 854 if (strcmp(name, DM_CONTROL_NODE) == 0 || 855 strcmp(name, ".") == 0 || 856 strcmp(name, "..") == 0) { 857 DMERR("device name cannot be \"%s\", \".\", or \"..\"", DM_CONTROL_NODE); 858 return -EINVAL; 859 } 860 861 return 0; 862 } 863 864 /* 865 * On successful return, the caller must not attempt to acquire 866 * _hash_lock without first calling dm_put_live_table, because dm_table_destroy 867 * waits for this dm_put_live_table and could be called under this lock. 868 */ 869 static struct dm_table *dm_get_inactive_table(struct mapped_device *md, int *srcu_idx) 870 { 871 struct hash_cell *hc; 872 struct dm_table *table = NULL; 873 874 /* increment rcu count, we don't care about the table pointer */ 875 dm_get_live_table(md, srcu_idx); 876 877 down_read(&_hash_lock); 878 hc = dm_get_mdptr(md); 879 if (!hc) { 880 DMERR("device has been removed from the dev hash table."); 881 goto out; 882 } 883 884 table = hc->new_map; 885 886 out: 887 up_read(&_hash_lock); 888 889 return table; 890 } 891 892 static struct dm_table *dm_get_live_or_inactive_table(struct mapped_device *md, 893 struct dm_ioctl *param, 894 int *srcu_idx) 895 { 896 return (param->flags & DM_QUERY_INACTIVE_TABLE_FLAG) ? 897 dm_get_inactive_table(md, srcu_idx) : dm_get_live_table(md, srcu_idx); 898 } 899 900 /* 901 * Fills in a dm_ioctl structure, ready for sending back to 902 * userland. 903 */ 904 static void __dev_status(struct mapped_device *md, struct dm_ioctl *param) 905 { 906 struct gendisk *disk = dm_disk(md); 907 struct dm_table *table; 908 int srcu_idx; 909 910 param->flags &= ~(DM_SUSPEND_FLAG | DM_READONLY_FLAG | 911 DM_ACTIVE_PRESENT_FLAG | DM_INTERNAL_SUSPEND_FLAG); 912 913 if (dm_suspended_md(md)) 914 param->flags |= DM_SUSPEND_FLAG; 915 916 if (dm_suspended_internally_md(md)) 917 param->flags |= DM_INTERNAL_SUSPEND_FLAG; 918 919 if (dm_test_deferred_remove_flag(md)) 920 param->flags |= DM_DEFERRED_REMOVE; 921 922 param->dev = huge_encode_dev(disk_devt(disk)); 923 924 /* 925 * Yes, this will be out of date by the time it gets back 926 * to userland, but it is still very useful for 927 * debugging. 928 */ 929 param->open_count = dm_open_count(md); 930 931 param->event_nr = dm_get_event_nr(md); 932 param->target_count = 0; 933 934 table = dm_get_live_table(md, &srcu_idx); 935 if (table) { 936 if (!(param->flags & DM_QUERY_INACTIVE_TABLE_FLAG)) { 937 if (get_disk_ro(disk)) 938 param->flags |= DM_READONLY_FLAG; 939 param->target_count = table->num_targets; 940 } 941 942 param->flags |= DM_ACTIVE_PRESENT_FLAG; 943 } 944 dm_put_live_table(md, srcu_idx); 945 946 if (param->flags & DM_QUERY_INACTIVE_TABLE_FLAG) { 947 int srcu_idx; 948 949 table = dm_get_inactive_table(md, &srcu_idx); 950 if (table) { 951 if (!(dm_table_get_mode(table) & BLK_OPEN_WRITE)) 952 param->flags |= DM_READONLY_FLAG; 953 param->target_count = table->num_targets; 954 } 955 dm_put_live_table(md, srcu_idx); 956 } 957 } 958 959 static int dev_create(struct file *filp, struct dm_ioctl *param, size_t param_size) 960 { 961 int r, m = DM_ANY_MINOR; 962 struct mapped_device *md; 963 964 r = check_name(param->name); 965 if (r) 966 return r; 967 968 if (param->flags & DM_PERSISTENT_DEV_FLAG) 969 m = MINOR(huge_decode_dev(param->dev)); 970 971 r = dm_create(m, &md); 972 if (r) 973 return r; 974 975 r = dm_hash_insert(param->name, *param->uuid ? param->uuid : NULL, md); 976 if (r) { 977 dm_put(md); 978 dm_destroy(md); 979 return r; 980 } 981 982 param->flags &= ~DM_INACTIVE_PRESENT_FLAG; 983 984 __dev_status(md, param); 985 986 dm_put(md); 987 988 return 0; 989 } 990 991 /* 992 * Always use UUID for lookups if it's present, otherwise use name or dev. 993 */ 994 static struct hash_cell *__find_device_hash_cell(struct dm_ioctl *param) 995 { 996 struct hash_cell *hc = NULL; 997 998 if (*param->uuid) { 999 if (*param->name || param->dev) { 1000 DMERR("Invalid ioctl structure: uuid %s, name %s, dev %llx", 1001 param->uuid, param->name, (unsigned long long)param->dev); 1002 return NULL; 1003 } 1004 1005 hc = __get_uuid_cell(param->uuid); 1006 if (!hc) 1007 return NULL; 1008 } else if (*param->name) { 1009 if (param->dev) { 1010 DMERR("Invalid ioctl structure: name %s, dev %llx", 1011 param->name, (unsigned long long)param->dev); 1012 return NULL; 1013 } 1014 1015 hc = __get_name_cell(param->name); 1016 if (!hc) 1017 return NULL; 1018 } else if (param->dev) { 1019 hc = __get_dev_cell(param->dev); 1020 if (!hc) 1021 return NULL; 1022 } else 1023 return NULL; 1024 1025 /* 1026 * Sneakily write in both the name and the uuid 1027 * while we have the cell. 1028 */ 1029 strscpy(param->name, hc->name, sizeof(param->name)); 1030 if (hc->uuid) 1031 strscpy(param->uuid, hc->uuid, sizeof(param->uuid)); 1032 else 1033 param->uuid[0] = '\0'; 1034 1035 if (hc->new_map) 1036 param->flags |= DM_INACTIVE_PRESENT_FLAG; 1037 else 1038 param->flags &= ~DM_INACTIVE_PRESENT_FLAG; 1039 1040 return hc; 1041 } 1042 1043 static struct mapped_device *find_device(struct dm_ioctl *param) 1044 { 1045 struct hash_cell *hc; 1046 struct mapped_device *md = NULL; 1047 1048 down_read(&_hash_lock); 1049 hc = __find_device_hash_cell(param); 1050 if (hc) 1051 md = hc->md; 1052 up_read(&_hash_lock); 1053 1054 return md; 1055 } 1056 1057 static int dev_remove(struct file *filp, struct dm_ioctl *param, size_t param_size) 1058 { 1059 struct hash_cell *hc; 1060 struct mapped_device *md; 1061 int r; 1062 struct dm_table *t; 1063 struct dm_ima_context *ima_context = NULL; 1064 unsigned int ima_idx; 1065 1066 dm_ima_alloc_context(&ima_context, true); 1067 down_write(&_hash_lock); 1068 hc = __find_device_hash_cell(param); 1069 1070 if (!hc) { 1071 DMDEBUG_LIMIT("device doesn't appear to be in the dev hash table."); 1072 up_write(&_hash_lock); 1073 dm_ima_free_context(ima_context); 1074 return -ENXIO; 1075 } 1076 1077 md = hc->md; 1078 1079 /* 1080 * Ensure the device is not open and nothing further can open it. 1081 */ 1082 r = dm_lock_for_deletion(md, !!(param->flags & DM_DEFERRED_REMOVE), false); 1083 if (r) { 1084 if (r == -EBUSY && param->flags & DM_DEFERRED_REMOVE) { 1085 up_write(&_hash_lock); 1086 dm_put(md); 1087 dm_ima_free_context(ima_context); 1088 return 0; 1089 } 1090 DMDEBUG_LIMIT("unable to remove open device %s", hc->name); 1091 up_write(&_hash_lock); 1092 dm_put(md); 1093 dm_ima_free_context(ima_context); 1094 return r; 1095 } 1096 1097 ima_idx = dm_ima_init_context(hc, ima_context, true); 1098 t = __hash_remove(hc); 1099 up_write(&_hash_lock); 1100 1101 if (t) { 1102 dm_sync_table(md); 1103 dm_table_destroy(t); 1104 } 1105 1106 param->flags &= ~DM_DEFERRED_REMOVE; 1107 1108 dm_ima_measure_on_device_remove(md, false, ima_context, ima_idx); 1109 dm_ima_free_context(ima_context); 1110 1111 if (!dm_kobject_uevent(md, KOBJ_REMOVE, param->event_nr, false)) 1112 param->flags |= DM_UEVENT_GENERATED_FLAG; 1113 1114 dm_put(md); 1115 dm_destroy(md); 1116 return 0; 1117 } 1118 1119 /* 1120 * Check a string doesn't overrun the chunk of 1121 * memory we copied from userland. 1122 */ 1123 static int invalid_str(char *str, void *end) 1124 { 1125 while ((void *) str < end) 1126 if (!*str++) 1127 return 0; 1128 1129 return -EINVAL; 1130 } 1131 1132 static int dev_rename(struct file *filp, struct dm_ioctl *param, size_t param_size) 1133 { 1134 int r; 1135 char *new_data = (char *) param + param->data_start; 1136 struct mapped_device *md; 1137 unsigned int change_uuid = (param->flags & DM_UUID_FLAG) ? 1 : 0; 1138 1139 if (new_data < param->data || 1140 invalid_str(new_data, (void *) param + param_size) || !*new_data || 1141 strlen(new_data) > (change_uuid ? DM_UUID_LEN - 1 : DM_NAME_LEN - 1)) { 1142 DMERR("Invalid new mapped device name or uuid string supplied."); 1143 return -EINVAL; 1144 } 1145 1146 if (!change_uuid) { 1147 r = check_name(new_data); 1148 if (r) 1149 return r; 1150 } 1151 1152 md = dm_hash_rename(param, new_data); 1153 if (IS_ERR(md)) 1154 return PTR_ERR(md); 1155 1156 __dev_status(md, param); 1157 dm_put(md); 1158 1159 return 0; 1160 } 1161 1162 static int dev_set_geometry(struct file *filp, struct dm_ioctl *param, size_t param_size) 1163 { 1164 int r = -EINVAL, x; 1165 struct mapped_device *md; 1166 struct hd_geometry geometry; 1167 unsigned long indata[4]; 1168 char *geostr = (char *) param + param->data_start; 1169 char dummy; 1170 1171 md = find_device(param); 1172 if (!md) 1173 return -ENXIO; 1174 1175 if (geostr < param->data || 1176 invalid_str(geostr, (void *) param + param_size)) { 1177 DMERR("Invalid geometry supplied."); 1178 goto out; 1179 } 1180 1181 x = sscanf(geostr, "%lu %lu %lu %lu%c", indata, 1182 indata + 1, indata + 2, indata + 3, &dummy); 1183 1184 if (x != 4) { 1185 DMERR("Unable to interpret geometry settings."); 1186 goto out; 1187 } 1188 1189 if (indata[0] > 65535 || indata[1] > 255 || indata[2] > 255) { 1190 DMERR("Geometry exceeds range limits."); 1191 goto out; 1192 } 1193 1194 geometry.cylinders = indata[0]; 1195 geometry.heads = indata[1]; 1196 geometry.sectors = indata[2]; 1197 geometry.start = indata[3]; 1198 1199 r = dm_set_geometry(md, &geometry); 1200 1201 param->data_size = 0; 1202 1203 out: 1204 dm_put(md); 1205 return r; 1206 } 1207 1208 static int do_suspend(struct dm_ioctl *param) 1209 { 1210 int r = 0; 1211 unsigned int suspend_flags = DM_SUSPEND_LOCKFS_FLAG; 1212 struct mapped_device *md; 1213 1214 md = find_device(param); 1215 if (!md) 1216 return -ENXIO; 1217 1218 if (param->flags & DM_SKIP_LOCKFS_FLAG) 1219 suspend_flags &= ~DM_SUSPEND_LOCKFS_FLAG; 1220 if (param->flags & DM_NOFLUSH_FLAG) 1221 suspend_flags |= DM_SUSPEND_NOFLUSH_FLAG; 1222 1223 if (!dm_suspended_md(md)) { 1224 r = dm_suspend(md, suspend_flags); 1225 if (r) 1226 goto out; 1227 } 1228 1229 __dev_status(md, param); 1230 1231 out: 1232 dm_put(md); 1233 1234 return r; 1235 } 1236 1237 static int do_resume(struct dm_ioctl *param) 1238 { 1239 int r = 0; 1240 unsigned int suspend_flags = DM_SUSPEND_LOCKFS_FLAG; 1241 struct hash_cell *hc; 1242 struct mapped_device *md; 1243 struct dm_table *new_map, *old_map = NULL; 1244 bool need_resize_uevent = false; 1245 struct dm_ima_context *ima_context = NULL; 1246 1247 dm_ima_alloc_context(&ima_context, true); 1248 down_write(&_hash_lock); 1249 1250 hc = __find_device_hash_cell(param); 1251 if (!hc) { 1252 DMDEBUG_LIMIT("device doesn't appear to be in the dev hash table."); 1253 up_write(&_hash_lock); 1254 dm_ima_free_context(ima_context); 1255 return -ENXIO; 1256 } 1257 1258 md = hc->md; 1259 1260 new_map = hc->new_map; 1261 hc->new_map = NULL; 1262 param->flags &= ~DM_INACTIVE_PRESENT_FLAG; 1263 if (new_map) 1264 dm_ima_init_context(hc, ima_context, false); 1265 up_write(&_hash_lock); 1266 1267 /* Do we need to load a new map ? */ 1268 if (new_map) { 1269 sector_t old_size, new_size; 1270 1271 dm_ima_context_table_op(md, ima_context, DM_IMA_TABLE_SAVE); 1272 /* Suspend if it isn't already suspended */ 1273 if (param->flags & DM_SKIP_LOCKFS_FLAG) 1274 suspend_flags &= ~DM_SUSPEND_LOCKFS_FLAG; 1275 if (param->flags & DM_NOFLUSH_FLAG) 1276 suspend_flags |= DM_SUSPEND_NOFLUSH_FLAG; 1277 if (!dm_suspended_md(md)) { 1278 r = dm_suspend(md, suspend_flags); 1279 if (r) { 1280 down_write(&_hash_lock); 1281 hc = dm_get_mdptr(md); 1282 if (hc && !hc->new_map) { 1283 hc->new_map = new_map; 1284 new_map = NULL; 1285 dm_ima_init_context(hc, ima_context, 1286 false); 1287 } else { 1288 r = -ENXIO; 1289 } 1290 up_write(&_hash_lock); 1291 if (new_map) { 1292 dm_sync_table(md); 1293 dm_table_destroy(new_map); 1294 } else 1295 dm_ima_context_table_op(md, ima_context, DM_IMA_TABLE_RESTORE); 1296 dm_ima_free_context(ima_context); 1297 dm_put(md); 1298 return r; 1299 } 1300 } 1301 1302 old_size = dm_get_size(md); 1303 old_map = dm_swap_table(md, new_map); 1304 if (IS_ERR(old_map)) { 1305 dm_sync_table(md); 1306 dm_table_destroy(new_map); 1307 dm_ima_free_context(ima_context); 1308 dm_put(md); 1309 return PTR_ERR(old_map); 1310 } 1311 if (dm_ima_need_measure(md, new_map, ima_context)) 1312 dm_ima_measure_on_device_resume(md, true, ima_context); 1313 new_size = dm_get_size(md); 1314 if (old_size && new_size && old_size != new_size) 1315 need_resize_uevent = true; 1316 1317 if (dm_table_get_mode(new_map) & BLK_OPEN_WRITE) 1318 set_disk_ro(dm_disk(md), 0); 1319 else 1320 set_disk_ro(dm_disk(md), 1); 1321 } 1322 1323 if (dm_suspended_md(md)) { 1324 r = dm_resume(md); 1325 if (!r) { 1326 if (!new_map && dm_ima_need_measure(md, NULL, 1327 ima_context)) 1328 dm_ima_measure_on_device_resume(md, false, 1329 ima_context); 1330 1331 if (!dm_kobject_uevent(md, KOBJ_CHANGE, param->event_nr, need_resize_uevent)) 1332 param->flags |= DM_UEVENT_GENERATED_FLAG; 1333 } 1334 } 1335 1336 /* 1337 * Since dm_swap_table synchronizes RCU, nobody should be in 1338 * read-side critical section already. 1339 */ 1340 if (old_map) 1341 dm_table_destroy(old_map); 1342 1343 if (!r) 1344 __dev_status(md, param); 1345 1346 dm_ima_free_context(ima_context); 1347 dm_put(md); 1348 return r; 1349 } 1350 1351 /* 1352 * Set or unset the suspension state of a device. 1353 * If the device already is in the requested state we just return its status. 1354 */ 1355 static int dev_suspend(struct file *filp, struct dm_ioctl *param, size_t param_size) 1356 { 1357 if (param->flags & DM_SUSPEND_FLAG) 1358 return do_suspend(param); 1359 1360 return do_resume(param); 1361 } 1362 1363 /* 1364 * Copies device info back to user space, used by 1365 * the create and info ioctls. 1366 */ 1367 static int dev_status(struct file *filp, struct dm_ioctl *param, size_t param_size) 1368 { 1369 struct mapped_device *md; 1370 1371 md = find_device(param); 1372 if (!md) 1373 return -ENXIO; 1374 1375 __dev_status(md, param); 1376 dm_put(md); 1377 1378 return 0; 1379 } 1380 1381 /* 1382 * Build up the status struct for each target 1383 */ 1384 static void retrieve_status(struct dm_table *table, 1385 struct dm_ioctl *param, size_t param_size) 1386 { 1387 unsigned int i, num_targets; 1388 struct dm_target_spec *spec; 1389 char *outbuf, *outptr; 1390 status_type_t type; 1391 size_t remaining, len, used = 0; 1392 unsigned int status_flags = 0; 1393 1394 outptr = outbuf = get_result_buffer(param, param_size, &len); 1395 1396 if (param->flags & DM_STATUS_TABLE_FLAG) 1397 type = STATUSTYPE_TABLE; 1398 else if (param->flags & DM_IMA_MEASUREMENT_FLAG) 1399 type = STATUSTYPE_IMA; 1400 else 1401 type = STATUSTYPE_INFO; 1402 1403 /* Get all the target info */ 1404 num_targets = table->num_targets; 1405 for (i = 0; i < num_targets; i++) { 1406 struct dm_target *ti = dm_table_get_target(table, i); 1407 size_t l; 1408 1409 remaining = len - (outptr - outbuf); 1410 if (remaining <= sizeof(struct dm_target_spec)) { 1411 param->flags |= DM_BUFFER_FULL_FLAG; 1412 break; 1413 } 1414 1415 spec = (struct dm_target_spec *) outptr; 1416 1417 spec->status = 0; 1418 spec->sector_start = ti->begin; 1419 spec->length = ti->len; 1420 strscpy_pad(spec->target_type, ti->type->name, 1421 sizeof(spec->target_type)); 1422 1423 outptr += sizeof(struct dm_target_spec); 1424 remaining = len - (outptr - outbuf); 1425 1426 /* Get the status/table string from the target driver */ 1427 if (ti->type->status) { 1428 if (param->flags & DM_NOFLUSH_FLAG) 1429 status_flags |= DM_STATUS_NOFLUSH_FLAG; 1430 ti->type->status(ti, type, status_flags, outptr, remaining); 1431 } else 1432 outptr[0] = '\0'; 1433 1434 l = strlen(outptr) + 1; 1435 if (l == remaining) { 1436 param->flags |= DM_BUFFER_FULL_FLAG; 1437 break; 1438 } 1439 1440 outptr += l; 1441 used = param->data_start + (outptr - outbuf); 1442 1443 outptr = align_ptr(outptr); 1444 if (!outptr || outptr > outbuf + len) { 1445 param->flags |= DM_BUFFER_FULL_FLAG; 1446 break; 1447 } 1448 spec->next = outptr - outbuf; 1449 } 1450 1451 if (used) 1452 param->data_size = used; 1453 1454 param->target_count = num_targets; 1455 } 1456 1457 /* 1458 * Wait for a device to report an event 1459 */ 1460 static int dev_wait(struct file *filp, struct dm_ioctl *param, size_t param_size) 1461 { 1462 int r = 0; 1463 struct mapped_device *md; 1464 struct dm_table *table; 1465 int srcu_idx; 1466 1467 md = find_device(param); 1468 if (!md) 1469 return -ENXIO; 1470 1471 /* 1472 * Wait for a notification event 1473 */ 1474 if (dm_wait_event(md, param->event_nr)) { 1475 r = -ERESTARTSYS; 1476 goto out; 1477 } 1478 1479 /* 1480 * The userland program is going to want to know what 1481 * changed to trigger the event, so we may as well tell 1482 * him and save an ioctl. 1483 */ 1484 __dev_status(md, param); 1485 1486 table = dm_get_live_or_inactive_table(md, param, &srcu_idx); 1487 if (table) 1488 retrieve_status(table, param, param_size); 1489 dm_put_live_table(md, srcu_idx); 1490 1491 out: 1492 dm_put(md); 1493 1494 return r; 1495 } 1496 1497 /* 1498 * Remember the global event number and make it possible to poll 1499 * for further events. 1500 */ 1501 static int dev_arm_poll(struct file *filp, struct dm_ioctl *param, size_t param_size) 1502 { 1503 struct dm_file *priv = filp->private_data; 1504 1505 priv->global_event_nr = atomic_read(&dm_global_event_nr); 1506 1507 return 0; 1508 } 1509 1510 static inline blk_mode_t get_mode(struct dm_ioctl *param) 1511 { 1512 blk_mode_t mode = BLK_OPEN_READ | BLK_OPEN_WRITE; 1513 1514 if (param->flags & DM_READONLY_FLAG) 1515 mode = BLK_OPEN_READ; 1516 1517 return mode; 1518 } 1519 1520 static int next_target(struct dm_target_spec *last, uint32_t next, const char *end, 1521 struct dm_target_spec **spec, char **target_params) 1522 { 1523 static_assert(__alignof__(struct dm_target_spec) <= 8, 1524 "struct dm_target_spec must not require more than 8-byte alignment"); 1525 1526 /* 1527 * Number of bytes remaining, starting with last. This is always 1528 * sizeof(struct dm_target_spec) or more, as otherwise *last was 1529 * out of bounds already. 1530 */ 1531 size_t remaining = end - (char *)last; 1532 1533 /* 1534 * There must be room for both the next target spec and the 1535 * NUL-terminator of the target itself. 1536 */ 1537 if (remaining - sizeof(struct dm_target_spec) <= next) { 1538 DMERR("Target spec extends beyond end of parameters"); 1539 return -EINVAL; 1540 } 1541 1542 if (next % __alignof__(struct dm_target_spec)) { 1543 DMERR("Next dm_target_spec (offset %u) is not %zu-byte aligned", 1544 next, __alignof__(struct dm_target_spec)); 1545 return -EINVAL; 1546 } 1547 1548 *spec = (struct dm_target_spec *) ((unsigned char *) last + next); 1549 *target_params = (char *) (*spec + 1); 1550 1551 return 0; 1552 } 1553 1554 static int populate_table(struct dm_table *table, 1555 struct dm_ioctl *param, size_t param_size) 1556 { 1557 int r; 1558 unsigned int i = 0; 1559 struct dm_target_spec *spec = (struct dm_target_spec *) param; 1560 uint32_t next = param->data_start; 1561 const char *const end = (const char *) param + param_size; 1562 char *target_params; 1563 size_t min_size = sizeof(struct dm_ioctl); 1564 1565 if (!param->target_count) { 1566 DMERR("%s: no targets specified", __func__); 1567 return -EINVAL; 1568 } 1569 1570 for (i = 0; i < param->target_count; i++) { 1571 const char *nul_terminator; 1572 1573 if (next < min_size) { 1574 DMERR("%s: next target spec (offset %u) overlaps %s", 1575 __func__, next, i ? "previous target" : "'struct dm_ioctl'"); 1576 return -EINVAL; 1577 } 1578 1579 r = next_target(spec, next, end, &spec, &target_params); 1580 if (r) { 1581 DMERR("unable to find target"); 1582 return r; 1583 } 1584 1585 nul_terminator = memchr(target_params, 0, (size_t)(end - target_params)); 1586 if (nul_terminator == NULL) { 1587 DMERR("%s: target parameters not NUL-terminated", __func__); 1588 return -EINVAL; 1589 } 1590 1591 /* Add 1 for NUL terminator */ 1592 min_size = (size_t)(nul_terminator - (const char *)spec) + 1; 1593 1594 r = dm_table_add_target(table, spec->target_type, 1595 (sector_t) spec->sector_start, 1596 (sector_t) spec->length, 1597 target_params); 1598 if (r) { 1599 DMERR("error adding target to table"); 1600 return r; 1601 } 1602 1603 next = spec->next; 1604 } 1605 1606 return dm_table_complete(table); 1607 } 1608 1609 static bool is_valid_type(enum dm_queue_mode cur, enum dm_queue_mode new) 1610 { 1611 if (cur == new || 1612 (cur == DM_TYPE_BIO_BASED && new == DM_TYPE_DAX_BIO_BASED)) 1613 return true; 1614 1615 return false; 1616 } 1617 1618 static int table_load(struct file *filp, struct dm_ioctl *param, size_t param_size) 1619 { 1620 int r, srcu_idx; 1621 struct hash_cell *hc; 1622 struct dm_table *t, *old_map = NULL; 1623 struct mapped_device *md; 1624 struct target_type *immutable_target_type; 1625 struct dm_ima_context *ima_context = NULL; 1626 1627 md = find_device(param); 1628 if (!md) 1629 return -ENXIO; 1630 1631 r = dm_table_create(&t, get_mode(param), param->target_count, md); 1632 if (r) 1633 goto err; 1634 1635 /* Protect md->type and md->queue against concurrent table loads. */ 1636 dm_lock_md_type(md); 1637 r = populate_table(t, param, param_size); 1638 if (r) 1639 goto err_unlock_md_type; 1640 1641 immutable_target_type = dm_get_immutable_target_type(md); 1642 if (immutable_target_type && 1643 (immutable_target_type != dm_table_get_immutable_target_type(t)) && 1644 !dm_table_get_wildcard_target(t)) { 1645 DMERR("can't replace immutable target type %s", 1646 immutable_target_type->name); 1647 r = -EINVAL; 1648 goto err_unlock_md_type; 1649 } 1650 1651 if (dm_get_md_type(md) == DM_TYPE_NONE) { 1652 /* setup md->queue to reflect md's type (may block) */ 1653 r = dm_setup_md_queue(md, t); 1654 if (r) { 1655 DMERR("unable to set up device queue for new table."); 1656 goto err_unlock_md_type; 1657 } 1658 } else if (!is_valid_type(dm_get_md_type(md), dm_table_get_type(t))) { 1659 DMERR("can't change device type (old=%u vs new=%u) after initial table load.", 1660 dm_get_md_type(md), dm_table_get_type(t)); 1661 r = -EINVAL; 1662 goto err_unlock_md_type; 1663 } 1664 1665 dm_unlock_md_type(md); 1666 1667 dm_ima_alloc_context(&ima_context, false); 1668 /* stage inactive table */ 1669 down_write(&_hash_lock); 1670 hc = dm_get_mdptr(md); 1671 if (!hc) { 1672 DMERR("device has been removed from the dev hash table."); 1673 up_write(&_hash_lock); 1674 dm_ima_free_context(ima_context); 1675 r = -ENXIO; 1676 goto err_destroy_table; 1677 } 1678 1679 if (hc->new_map) 1680 old_map = hc->new_map; 1681 hc->new_map = t; 1682 dm_ima_init_context(hc, ima_context, false); 1683 /* Make sure new_map doesn't get freed before we measure it*/ 1684 dm_get_live_table(md, &srcu_idx); 1685 up_write(&_hash_lock); 1686 1687 dm_ima_measure_on_table_load(t, ima_context); 1688 dm_ima_free_context(ima_context); 1689 dm_put_live_table(md, srcu_idx); 1690 1691 param->flags |= DM_INACTIVE_PRESENT_FLAG; 1692 __dev_status(md, param); 1693 1694 if (old_map) { 1695 dm_sync_table(md); 1696 dm_table_destroy(old_map); 1697 } 1698 1699 dm_put(md); 1700 1701 return 0; 1702 1703 err_unlock_md_type: 1704 dm_unlock_md_type(md); 1705 err_destroy_table: 1706 dm_table_destroy(t); 1707 err: 1708 dm_put(md); 1709 1710 return r; 1711 } 1712 1713 static int table_clear(struct file *filp, struct dm_ioctl *param, size_t param_size) 1714 { 1715 struct hash_cell *hc; 1716 struct mapped_device *md; 1717 struct dm_table *old_map = NULL; 1718 struct dm_ima_context *ima_context = NULL; 1719 1720 dm_ima_alloc_context(&ima_context, true); 1721 down_write(&_hash_lock); 1722 1723 hc = __find_device_hash_cell(param); 1724 if (!hc) { 1725 DMDEBUG_LIMIT("device doesn't appear to be in the dev hash table."); 1726 up_write(&_hash_lock); 1727 dm_ima_free_context(ima_context); 1728 return -ENXIO; 1729 } 1730 1731 if (hc->new_map) { 1732 old_map = hc->new_map; 1733 hc->new_map = NULL; 1734 } 1735 1736 dm_ima_init_context(hc, ima_context, false); 1737 md = hc->md; 1738 up_write(&_hash_lock); 1739 dm_ima_measure_on_table_clear(md, ima_context); 1740 dm_ima_free_context(ima_context); 1741 1742 param->flags &= ~DM_INACTIVE_PRESENT_FLAG; 1743 __dev_status(md, param); 1744 1745 if (old_map) { 1746 dm_sync_table(md); 1747 dm_table_destroy(old_map); 1748 } 1749 dm_put(md); 1750 1751 return 0; 1752 } 1753 1754 /* 1755 * Retrieves a list of devices used by a particular dm device. 1756 */ 1757 static void retrieve_deps(struct dm_table *table, 1758 struct dm_ioctl *param, size_t param_size) 1759 { 1760 unsigned int count = 0; 1761 struct list_head *tmp; 1762 size_t len, needed; 1763 struct dm_dev_internal *dd; 1764 struct dm_target_deps *deps; 1765 1766 deps = get_result_buffer(param, param_size, &len); 1767 1768 /* 1769 * Count the devices. 1770 */ 1771 list_for_each(tmp, dm_table_get_devices(table)) 1772 count++; 1773 1774 /* 1775 * Check we have enough space. 1776 */ 1777 needed = struct_size(deps, dev, count); 1778 if (len < needed) { 1779 param->flags |= DM_BUFFER_FULL_FLAG; 1780 return; 1781 } 1782 1783 /* 1784 * Fill in the devices. 1785 */ 1786 deps->count = count; 1787 count = 0; 1788 list_for_each_entry(dd, dm_table_get_devices(table), list) 1789 deps->dev[count++] = huge_encode_dev(dd->dm_dev->bdev->bd_dev); 1790 1791 param->data_size = param->data_start + needed; 1792 } 1793 1794 static int table_deps(struct file *filp, struct dm_ioctl *param, size_t param_size) 1795 { 1796 struct mapped_device *md; 1797 struct dm_table *table; 1798 int srcu_idx; 1799 1800 md = find_device(param); 1801 if (!md) 1802 return -ENXIO; 1803 1804 __dev_status(md, param); 1805 1806 table = dm_get_live_or_inactive_table(md, param, &srcu_idx); 1807 if (table) 1808 retrieve_deps(table, param, param_size); 1809 dm_put_live_table(md, srcu_idx); 1810 1811 dm_put(md); 1812 1813 return 0; 1814 } 1815 1816 /* 1817 * Return the status of a device as a text string for each 1818 * target. 1819 */ 1820 static int table_status(struct file *filp, struct dm_ioctl *param, size_t param_size) 1821 { 1822 struct mapped_device *md; 1823 struct dm_table *table; 1824 int srcu_idx; 1825 1826 md = find_device(param); 1827 if (!md) 1828 return -ENXIO; 1829 1830 __dev_status(md, param); 1831 1832 table = dm_get_live_or_inactive_table(md, param, &srcu_idx); 1833 if (table) 1834 retrieve_status(table, param, param_size); 1835 dm_put_live_table(md, srcu_idx); 1836 1837 dm_put(md); 1838 1839 return 0; 1840 } 1841 1842 /* 1843 * Process device-mapper dependent messages. Messages prefixed with '@' 1844 * are processed by the DM core. All others are delivered to the target. 1845 * Returns a number <= 1 if message was processed by device mapper. 1846 * Returns 2 if message should be delivered to the target. 1847 */ 1848 static int message_for_md(struct mapped_device *md, unsigned int argc, char **argv, 1849 char *result, unsigned int maxlen) 1850 { 1851 int r; 1852 1853 if (**argv != '@') 1854 return 2; /* no '@' prefix, deliver to target */ 1855 1856 if (!strcasecmp(argv[0], "@cancel_deferred_remove")) { 1857 if (argc != 1) { 1858 DMERR("Invalid arguments for @cancel_deferred_remove"); 1859 return -EINVAL; 1860 } 1861 return dm_cancel_deferred_remove(md); 1862 } 1863 1864 r = dm_stats_message(md, argc, argv, result, maxlen); 1865 if (r < 2) 1866 return r; 1867 1868 DMERR("Unsupported message sent to DM core: %s", argv[0]); 1869 return -EINVAL; 1870 } 1871 1872 /* 1873 * Pass a message to the target that's at the supplied device offset. 1874 */ 1875 static int target_message(struct file *filp, struct dm_ioctl *param, size_t param_size) 1876 { 1877 int r, argc; 1878 char **argv; 1879 struct mapped_device *md; 1880 struct dm_table *table; 1881 struct dm_target *ti; 1882 struct dm_target_msg *tmsg = (void *) param + param->data_start; 1883 size_t maxlen; 1884 char *result = get_result_buffer(param, param_size, &maxlen); 1885 int srcu_idx; 1886 1887 md = find_device(param); 1888 if (!md) 1889 return -ENXIO; 1890 1891 if (tmsg < (struct dm_target_msg *) param->data || 1892 invalid_str(tmsg->message, (void *) param + param_size)) { 1893 DMERR("Invalid target message parameters."); 1894 r = -EINVAL; 1895 goto out; 1896 } 1897 1898 r = dm_split_args(&argc, &argv, tmsg->message); 1899 if (r) { 1900 DMERR("Failed to split target message parameters"); 1901 goto out; 1902 } 1903 1904 if (!argc) { 1905 DMERR("Empty message received."); 1906 r = -EINVAL; 1907 goto out_argv; 1908 } 1909 1910 r = message_for_md(md, argc, argv, result, maxlen); 1911 if (r <= 1) 1912 goto out_argv; 1913 1914 table = dm_get_live_table(md, &srcu_idx); 1915 if (!table) { 1916 DMERR("The device has no table."); 1917 r = -EINVAL; 1918 goto out_table; 1919 } 1920 1921 if (dm_deleting_md(md)) { 1922 r = -ENXIO; 1923 goto out_table; 1924 } 1925 1926 ti = dm_table_find_target(table, tmsg->sector); 1927 if (!ti) { 1928 DMERR("Target message sector outside device."); 1929 r = -EINVAL; 1930 } else if (ti->type->message) 1931 r = ti->type->message(ti, argc, argv, result, maxlen); 1932 else { 1933 DMERR("Target type does not support messages"); 1934 r = -EINVAL; 1935 } 1936 1937 out_table: 1938 dm_put_live_table(md, srcu_idx); 1939 out_argv: 1940 kfree(argv); 1941 out: 1942 if (r >= 0) 1943 __dev_status(md, param); 1944 1945 if (r == 1) { 1946 param->flags |= DM_DATA_OUT_FLAG; 1947 if (dm_message_test_buffer_overflow(result, maxlen)) 1948 param->flags |= DM_BUFFER_FULL_FLAG; 1949 else 1950 param->data_size = param->data_start + strlen(result) + 1; 1951 r = 0; 1952 } 1953 1954 dm_put(md); 1955 return r; 1956 } 1957 1958 /* 1959 * The ioctl parameter block consists of two parts, a dm_ioctl struct 1960 * followed by a data buffer. This flag is set if the second part, 1961 * which has a variable size, is not used by the function processing 1962 * the ioctl. 1963 */ 1964 #define IOCTL_FLAGS_NO_PARAMS 1 1965 #define IOCTL_FLAGS_ISSUE_GLOBAL_EVENT 2 1966 1967 /* 1968 *--------------------------------------------------------------- 1969 * Implementation of open/close/ioctl on the special char device. 1970 *--------------------------------------------------------------- 1971 */ 1972 static ioctl_fn lookup_ioctl(unsigned int cmd, int *ioctl_flags) 1973 { 1974 static const struct { 1975 int flags; 1976 ioctl_fn fn; 1977 } _ioctls[] = { 1978 [DM_VERSION_CMD] = {0, NULL}, /* version is dealt with elsewhere */ 1979 [DM_REMOVE_ALL_CMD] = {IOCTL_FLAGS_NO_PARAMS | IOCTL_FLAGS_ISSUE_GLOBAL_EVENT, remove_all}, 1980 [DM_LIST_DEVICES_CMD] = {0, list_devices}, 1981 1982 [DM_DEV_CREATE_CMD] = {IOCTL_FLAGS_NO_PARAMS | IOCTL_FLAGS_ISSUE_GLOBAL_EVENT, dev_create}, 1983 [DM_DEV_REMOVE_CMD] = {IOCTL_FLAGS_NO_PARAMS | IOCTL_FLAGS_ISSUE_GLOBAL_EVENT, dev_remove}, 1984 [DM_DEV_RENAME_CMD] = {IOCTL_FLAGS_ISSUE_GLOBAL_EVENT, dev_rename}, 1985 [DM_DEV_SUSPEND_CMD] = {IOCTL_FLAGS_NO_PARAMS, dev_suspend}, 1986 [DM_DEV_STATUS_CMD] = {IOCTL_FLAGS_NO_PARAMS, dev_status}, 1987 [DM_DEV_WAIT_CMD] = {0, dev_wait}, 1988 1989 [DM_TABLE_LOAD_CMD] = {0, table_load}, 1990 [DM_TABLE_CLEAR_CMD] = {IOCTL_FLAGS_NO_PARAMS, table_clear}, 1991 [DM_TABLE_DEPS_CMD] = {0, table_deps}, 1992 [DM_TABLE_STATUS_CMD] = {0, table_status}, 1993 1994 [DM_LIST_VERSIONS_CMD] = {0, list_versions}, 1995 1996 [DM_TARGET_MSG_CMD] = {0, target_message}, 1997 [DM_DEV_SET_GEOMETRY_CMD] = {0, dev_set_geometry}, 1998 [DM_DEV_ARM_POLL_CMD] = {IOCTL_FLAGS_NO_PARAMS, dev_arm_poll}, 1999 [DM_GET_TARGET_VERSION_CMD] = {0, get_target_version}, 2000 [DM_MPATH_PROBE_PATHS_CMD] = {0, NULL}, /* block device ioctl */ 2001 }; 2002 2003 if (unlikely(cmd >= ARRAY_SIZE(_ioctls))) 2004 return NULL; 2005 2006 cmd = array_index_nospec(cmd, ARRAY_SIZE(_ioctls)); 2007 *ioctl_flags = _ioctls[cmd].flags; 2008 return _ioctls[cmd].fn; 2009 } 2010 2011 /* 2012 * As well as checking the version compatibility this always 2013 * copies the kernel interface version out. 2014 */ 2015 static int check_version(unsigned int cmd, struct dm_ioctl __user *user, 2016 struct dm_ioctl *kernel_params) 2017 { 2018 int r = 0; 2019 2020 /* Make certain version is first member of dm_ioctl struct */ 2021 BUILD_BUG_ON(offsetof(struct dm_ioctl, version) != 0); 2022 2023 if (copy_from_user(kernel_params->version, user->version, sizeof(kernel_params->version))) 2024 return -EFAULT; 2025 2026 if ((kernel_params->version[0] != DM_VERSION_MAJOR) || 2027 (kernel_params->version[1] > DM_VERSION_MINOR)) { 2028 DMERR_LIMIT("ioctl interface mismatch: kernel(%u.%u.%u), user(%u.%u.%u), cmd(%d)", 2029 DM_VERSION_MAJOR, DM_VERSION_MINOR, 2030 DM_VERSION_PATCHLEVEL, 2031 kernel_params->version[0], 2032 kernel_params->version[1], 2033 kernel_params->version[2], 2034 cmd); 2035 r = -EINVAL; 2036 } 2037 2038 /* 2039 * Fill in the kernel version. 2040 */ 2041 kernel_params->version[0] = DM_VERSION_MAJOR; 2042 kernel_params->version[1] = DM_VERSION_MINOR; 2043 kernel_params->version[2] = DM_VERSION_PATCHLEVEL; 2044 if (copy_to_user(user->version, kernel_params->version, sizeof(kernel_params->version))) 2045 return -EFAULT; 2046 2047 return r; 2048 } 2049 2050 #define DM_PARAMS_MALLOC 0x0001 /* Params allocated with kvmalloc() */ 2051 #define DM_WIPE_BUFFER 0x0010 /* Wipe input buffer before returning from ioctl */ 2052 2053 static void free_params(struct dm_ioctl *param, size_t param_size, int param_flags) 2054 { 2055 if (param_flags & DM_WIPE_BUFFER) 2056 memset(param, 0, param_size); 2057 2058 if (param_flags & DM_PARAMS_MALLOC) 2059 kvfree(param); 2060 } 2061 2062 static int copy_params(struct dm_ioctl __user *user, struct dm_ioctl *param_kernel, 2063 int ioctl_flags, struct dm_ioctl **param, int *param_flags) 2064 { 2065 struct dm_ioctl *dmi; 2066 int secure_data; 2067 const size_t minimum_data_size = offsetof(struct dm_ioctl, data); 2068 2069 /* check_version() already copied version from userspace, avoid TOCTOU */ 2070 if (copy_from_user((char *)param_kernel + sizeof(param_kernel->version), 2071 (char __user *)user + sizeof(param_kernel->version), 2072 minimum_data_size - sizeof(param_kernel->version))) 2073 return -EFAULT; 2074 2075 if (unlikely(param_kernel->data_size < minimum_data_size) || 2076 unlikely(param_kernel->data_size > DM_MAX_TARGETS * DM_MAX_TARGET_PARAMS)) { 2077 DMERR_LIMIT("Invalid data size in the ioctl structure: %u", 2078 param_kernel->data_size); 2079 return -EINVAL; 2080 } 2081 2082 secure_data = param_kernel->flags & DM_SECURE_DATA_FLAG; 2083 2084 *param_flags = secure_data ? DM_WIPE_BUFFER : 0; 2085 2086 if (ioctl_flags & IOCTL_FLAGS_NO_PARAMS) { 2087 dmi = param_kernel; 2088 dmi->data_size = minimum_data_size; 2089 goto data_copied; 2090 } 2091 2092 /* 2093 * Use __GFP_HIGH to avoid low memory issues when a device is 2094 * suspended and the ioctl is needed to resume it. 2095 * Use kmalloc() rather than vmalloc() when we can. 2096 */ 2097 dmi = NULL; 2098 dmi = kvmalloc(param_kernel->data_size, GFP_NOIO | __GFP_HIGH); 2099 2100 if (!dmi) { 2101 if (secure_data && clear_user(user, param_kernel->data_size)) 2102 return -EFAULT; 2103 return -ENOMEM; 2104 } 2105 2106 *param_flags |= DM_PARAMS_MALLOC; 2107 2108 /* Copy from param_kernel (which was already copied from user) */ 2109 memcpy(dmi, param_kernel, minimum_data_size); 2110 2111 if (copy_from_user(&dmi->data, (char __user *)user + minimum_data_size, 2112 param_kernel->data_size - minimum_data_size)) 2113 goto bad; 2114 data_copied: 2115 /* Wipe the user buffer so we do not return it to userspace */ 2116 if (secure_data && clear_user(user, param_kernel->data_size)) 2117 goto bad; 2118 2119 *param = dmi; 2120 return 0; 2121 2122 bad: 2123 free_params(dmi, param_kernel->data_size, *param_flags); 2124 2125 return -EFAULT; 2126 } 2127 2128 static int validate_params(uint cmd, struct dm_ioctl *param) 2129 { 2130 /* Always clear this flag */ 2131 param->flags &= ~DM_BUFFER_FULL_FLAG; 2132 param->flags &= ~DM_UEVENT_GENERATED_FLAG; 2133 param->flags &= ~DM_SECURE_DATA_FLAG; 2134 param->flags &= ~DM_DATA_OUT_FLAG; 2135 2136 /* Ignores parameters */ 2137 if (cmd == DM_REMOVE_ALL_CMD || 2138 cmd == DM_LIST_DEVICES_CMD || 2139 cmd == DM_LIST_VERSIONS_CMD) 2140 return 0; 2141 2142 if (cmd == DM_DEV_CREATE_CMD) { 2143 if (!*param->name) { 2144 DMERR("name not supplied when creating device"); 2145 return -EINVAL; 2146 } 2147 } else if (*param->uuid && *param->name) { 2148 DMERR("only supply one of name or uuid, cmd(%u)", cmd); 2149 return -EINVAL; 2150 } 2151 2152 /* Ensure strings are terminated */ 2153 param->name[DM_NAME_LEN - 1] = '\0'; 2154 param->uuid[DM_UUID_LEN - 1] = '\0'; 2155 2156 return 0; 2157 } 2158 2159 static int ctl_ioctl(struct file *file, uint command, struct dm_ioctl __user *user) 2160 { 2161 int r = 0; 2162 int ioctl_flags; 2163 int param_flags; 2164 unsigned int cmd; 2165 struct dm_ioctl *param; 2166 ioctl_fn fn = NULL; 2167 size_t input_param_size; 2168 struct dm_ioctl param_kernel; 2169 2170 /* only root can play with this */ 2171 if (!capable(CAP_SYS_ADMIN)) 2172 return -EACCES; 2173 2174 if (_IOC_TYPE(command) != DM_IOCTL) 2175 return -ENOTTY; 2176 2177 cmd = _IOC_NR(command); 2178 2179 /* 2180 * Check the interface version passed in. This also 2181 * writes out the kernel's interface version. 2182 */ 2183 r = check_version(cmd, user, ¶m_kernel); 2184 if (r) 2185 return r; 2186 2187 /* 2188 * Nothing more to do for the version command. 2189 */ 2190 if (cmd == DM_VERSION_CMD) 2191 return 0; 2192 2193 fn = lookup_ioctl(cmd, &ioctl_flags); 2194 if (!fn) { 2195 DMERR("dm_ctl_ioctl: unknown command 0x%x", command); 2196 return -ENOTTY; 2197 } 2198 2199 /* 2200 * Copy the parameters into kernel space. 2201 */ 2202 r = copy_params(user, ¶m_kernel, ioctl_flags, ¶m, ¶m_flags); 2203 2204 if (r) 2205 return r; 2206 2207 input_param_size = param->data_size; 2208 r = validate_params(cmd, param); 2209 if (r) 2210 goto out; 2211 2212 param->data_size = offsetof(struct dm_ioctl, data); 2213 r = fn(file, param, input_param_size); 2214 2215 if (unlikely(param->flags & DM_BUFFER_FULL_FLAG) && 2216 unlikely(ioctl_flags & IOCTL_FLAGS_NO_PARAMS)) 2217 DMERR("ioctl %d tried to output some data but has IOCTL_FLAGS_NO_PARAMS set", cmd); 2218 2219 if (!r && ioctl_flags & IOCTL_FLAGS_ISSUE_GLOBAL_EVENT) 2220 dm_issue_global_event(); 2221 2222 /* 2223 * Copy the results back to userland. 2224 */ 2225 if (!r && copy_to_user(user, param, param->data_size)) 2226 r = -EFAULT; 2227 2228 out: 2229 free_params(param, input_param_size, param_flags); 2230 return r; 2231 } 2232 2233 static long dm_ctl_ioctl(struct file *file, uint command, ulong u) 2234 { 2235 return (long)ctl_ioctl(file, command, (struct dm_ioctl __user *)u); 2236 } 2237 2238 #ifdef CONFIG_COMPAT 2239 static long dm_compat_ctl_ioctl(struct file *file, uint command, ulong u) 2240 { 2241 return (long)dm_ctl_ioctl(file, command, (ulong) compat_ptr(u)); 2242 } 2243 #else 2244 #define dm_compat_ctl_ioctl NULL 2245 #endif 2246 2247 static int dm_open(struct inode *inode, struct file *filp) 2248 { 2249 struct dm_file *priv; 2250 2251 nonseekable_open(inode, filp); 2252 2253 priv = filp->private_data = kmalloc_obj(struct dm_file); 2254 if (!priv) 2255 return -ENOMEM; 2256 2257 priv->global_event_nr = atomic_read(&dm_global_event_nr); 2258 2259 return 0; 2260 } 2261 2262 static int dm_release(struct inode *inode, struct file *filp) 2263 { 2264 kfree(filp->private_data); 2265 return 0; 2266 } 2267 2268 static __poll_t dm_poll(struct file *filp, poll_table *wait) 2269 { 2270 struct dm_file *priv = filp->private_data; 2271 __poll_t mask = 0; 2272 2273 poll_wait(filp, &dm_global_eventq, wait); 2274 2275 if ((int)(atomic_read(&dm_global_event_nr) - priv->global_event_nr) > 0) 2276 mask |= EPOLLIN; 2277 2278 return mask; 2279 } 2280 2281 static const struct file_operations _ctl_fops = { 2282 .open = dm_open, 2283 .release = dm_release, 2284 .poll = dm_poll, 2285 .unlocked_ioctl = dm_ctl_ioctl, 2286 .compat_ioctl = dm_compat_ctl_ioctl, 2287 .owner = THIS_MODULE, 2288 .llseek = noop_llseek, 2289 }; 2290 2291 static struct miscdevice _dm_misc = { 2292 .minor = MAPPER_CTRL_MINOR, 2293 .name = DM_NAME, 2294 .nodename = DM_DIR "/" DM_CONTROL_NODE, 2295 .fops = &_ctl_fops 2296 }; 2297 2298 MODULE_ALIAS_MISCDEV(MAPPER_CTRL_MINOR); 2299 MODULE_ALIAS("devname:" DM_DIR "/" DM_CONTROL_NODE); 2300 2301 /* 2302 * Create misc character device and link to DM_DIR/control. 2303 */ 2304 int __init dm_interface_init(void) 2305 { 2306 int r; 2307 2308 r = misc_register(&_dm_misc); 2309 if (r) { 2310 DMERR("misc_register failed for control device"); 2311 return r; 2312 } 2313 2314 DMINFO("%d.%d.%d%s initialised: %s", DM_VERSION_MAJOR, 2315 DM_VERSION_MINOR, DM_VERSION_PATCHLEVEL, DM_VERSION_EXTRA, 2316 DM_DRIVER_EMAIL); 2317 return 0; 2318 } 2319 2320 void dm_interface_exit(void) 2321 { 2322 misc_deregister(&_dm_misc); 2323 dm_hash_exit(); 2324 } 2325 2326 /** 2327 * dm_copy_name_and_uuid - Copy mapped device name & uuid into supplied buffers 2328 * @md: Pointer to mapped_device 2329 * @name: Buffer (size DM_NAME_LEN) for name 2330 * @uuid: Buffer (size DM_UUID_LEN) for uuid or empty string if uuid not defined 2331 */ 2332 int dm_copy_name_and_uuid(struct mapped_device *md, char *name, char *uuid) 2333 { 2334 int r = 0; 2335 struct hash_cell *hc; 2336 2337 if (!md) 2338 return -ENXIO; 2339 2340 mutex_lock(&dm_hash_cells_mutex); 2341 hc = dm_get_mdptr(md); 2342 if (!hc) { 2343 r = -ENXIO; 2344 goto out; 2345 } 2346 2347 if (name) 2348 strcpy(name, hc->name); 2349 if (uuid) 2350 strcpy(uuid, hc->uuid ? : ""); 2351 2352 out: 2353 mutex_unlock(&dm_hash_cells_mutex); 2354 2355 return r; 2356 } 2357 EXPORT_SYMBOL_GPL(dm_copy_name_and_uuid); 2358 2359 /** 2360 * dm_early_create - create a mapped device in early boot. 2361 * 2362 * @dmi: Contains main information of the device mapping to be created. 2363 * @spec_array: array of pointers to struct dm_target_spec. Describes the 2364 * mapping table of the device. 2365 * @target_params_array: array of strings with the parameters to a specific 2366 * target. 2367 * 2368 * Instead of having the struct dm_target_spec and the parameters for every 2369 * target embedded at the end of struct dm_ioctl (as performed in a normal 2370 * ioctl), pass them as arguments, so the caller doesn't need to serialize them. 2371 * The size of the spec_array and target_params_array is given by 2372 * @dmi->target_count. 2373 * This function is supposed to be called in early boot, so locking mechanisms 2374 * to protect against concurrent loads are not required. 2375 */ 2376 int __init dm_early_create(struct dm_ioctl *dmi, 2377 struct dm_target_spec **spec_array, 2378 char **target_params_array) 2379 { 2380 int r, m = DM_ANY_MINOR; 2381 struct dm_table *t, *old_map; 2382 struct mapped_device *md; 2383 unsigned int i; 2384 2385 if (!dmi->target_count) 2386 return -EINVAL; 2387 2388 r = check_name(dmi->name); 2389 if (r) 2390 return r; 2391 2392 if (dmi->flags & DM_PERSISTENT_DEV_FLAG) 2393 m = MINOR(huge_decode_dev(dmi->dev)); 2394 2395 /* alloc dm device */ 2396 r = dm_create(m, &md); 2397 if (r) 2398 return r; 2399 2400 /* hash insert */ 2401 r = dm_hash_insert(dmi->name, *dmi->uuid ? dmi->uuid : NULL, md); 2402 if (r) 2403 goto err_destroy_dm; 2404 2405 /* alloc table */ 2406 r = dm_table_create(&t, get_mode(dmi), dmi->target_count, md); 2407 if (r) 2408 goto err_hash_remove; 2409 2410 /* add targets */ 2411 for (i = 0; i < dmi->target_count; i++) { 2412 r = dm_table_add_target(t, spec_array[i]->target_type, 2413 (sector_t) spec_array[i]->sector_start, 2414 (sector_t) spec_array[i]->length, 2415 target_params_array[i]); 2416 if (r) { 2417 DMERR("error adding target to table"); 2418 goto err_destroy_table; 2419 } 2420 } 2421 2422 /* finish table */ 2423 r = dm_table_complete(t); 2424 if (r) 2425 goto err_destroy_table; 2426 2427 /* setup md->queue to reflect md's type (may block) */ 2428 r = dm_setup_md_queue(md, t); 2429 if (r) { 2430 DMERR("unable to set up device queue for new table."); 2431 goto err_destroy_table; 2432 } 2433 2434 /* Set new map */ 2435 dm_suspend(md, 0); 2436 old_map = dm_swap_table(md, t); 2437 if (IS_ERR(old_map)) { 2438 r = PTR_ERR(old_map); 2439 goto err_destroy_table; 2440 } 2441 set_disk_ro(dm_disk(md), !!(dmi->flags & DM_READONLY_FLAG)); 2442 2443 /* resume device */ 2444 r = dm_resume(md); 2445 if (r) 2446 goto err_hash_remove; 2447 2448 DMINFO("%s (%s) is ready", md->disk->disk_name, dmi->name); 2449 dm_put(md); 2450 return 0; 2451 2452 err_destroy_table: 2453 dm_table_destroy(t); 2454 err_hash_remove: 2455 down_write(&_hash_lock); 2456 (void) __hash_remove(__get_name_cell(dmi->name)); 2457 up_write(&_hash_lock); 2458 /* release reference from __get_name_cell */ 2459 dm_put(md); 2460 err_destroy_dm: 2461 dm_put(md); 2462 dm_destroy(md); 2463 return r; 2464 } 2465