1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2001, 2002 Sistina Software (UK) Limited.
4 * Copyright (C) 2004 - 2006 Red Hat, Inc. All rights reserved.
6 * This file is released under the GPL.
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>
24 #include <linux/uaccess.h>
25 #include <linux/ima.h>
27 #define DM_MSG_PREFIX "ioctl"
28 #define DM_DRIVER_EMAIL "dm-devel@redhat.com"
32 * poll will wait until the global event number is greater than
35 volatile unsigned int global_event_nr;
39 *---------------------------------------------------------------
40 * The ioctl interface needs to be able to look up devices by
42 *---------------------------------------------------------------
45 struct rb_node name_node;
46 struct rb_node uuid_node;
52 struct mapped_device *md;
53 struct dm_table *new_map;
58 struct dm_target_versions *vers, *old_vers;
64 static struct rb_root name_rb_tree = RB_ROOT;
65 static struct rb_root uuid_rb_tree = RB_ROOT;
67 static void dm_hash_remove_all(bool keep_open_devices, bool mark_deferred, bool only_deferred);
70 * Guards access to both hash tables.
72 static DECLARE_RWSEM(_hash_lock);
75 * Protects use of mdptr to obtain hash cell name and uuid from mapped device.
77 static DEFINE_MUTEX(dm_hash_cells_mutex);
79 static void dm_hash_exit(void)
81 dm_hash_remove_all(false, false, false);
85 *---------------------------------------------------------------
86 * Code for looking up a device by name
87 *---------------------------------------------------------------
89 static struct hash_cell *__get_name_cell(const char *str)
91 struct rb_node *n = name_rb_tree.rb_node;
94 struct hash_cell *hc = container_of(n, struct hash_cell, name_node);
97 c = strcmp(hc->name, str);
102 n = c >= 0 ? n->rb_left : n->rb_right;
108 static struct hash_cell *__get_uuid_cell(const char *str)
110 struct rb_node *n = uuid_rb_tree.rb_node;
113 struct hash_cell *hc = container_of(n, struct hash_cell, uuid_node);
116 c = strcmp(hc->uuid, str);
121 n = c >= 0 ? n->rb_left : n->rb_right;
127 static void __unlink_name(struct hash_cell *hc)
130 hc->name_set = false;
131 rb_erase(&hc->name_node, &name_rb_tree);
135 static void __unlink_uuid(struct hash_cell *hc)
138 hc->uuid_set = false;
139 rb_erase(&hc->uuid_node, &uuid_rb_tree);
143 static void __link_name(struct hash_cell *new_hc)
145 struct rb_node **n, *parent;
147 __unlink_name(new_hc);
149 new_hc->name_set = true;
151 n = &name_rb_tree.rb_node;
155 struct hash_cell *hc = container_of(*n, struct hash_cell, name_node);
158 c = strcmp(hc->name, new_hc->name);
161 n = c >= 0 ? &hc->name_node.rb_left : &hc->name_node.rb_right;
164 rb_link_node(&new_hc->name_node, parent, n);
165 rb_insert_color(&new_hc->name_node, &name_rb_tree);
168 static void __link_uuid(struct hash_cell *new_hc)
170 struct rb_node **n, *parent;
172 __unlink_uuid(new_hc);
174 new_hc->uuid_set = true;
176 n = &uuid_rb_tree.rb_node;
180 struct hash_cell *hc = container_of(*n, struct hash_cell, uuid_node);
183 c = strcmp(hc->uuid, new_hc->uuid);
186 n = c > 0 ? &hc->uuid_node.rb_left : &hc->uuid_node.rb_right;
189 rb_link_node(&new_hc->uuid_node, parent, n);
190 rb_insert_color(&new_hc->uuid_node, &uuid_rb_tree);
193 static struct hash_cell *__get_dev_cell(uint64_t dev)
195 struct mapped_device *md;
196 struct hash_cell *hc;
198 md = dm_get_md(huge_decode_dev(dev));
202 hc = dm_get_mdptr(md);
212 *---------------------------------------------------------------
213 * Inserting, removing and renaming a device.
214 *---------------------------------------------------------------
216 static struct hash_cell *alloc_cell(const char *name, const char *uuid,
217 struct mapped_device *md)
219 struct hash_cell *hc;
221 hc = kmalloc(sizeof(*hc), GFP_KERNEL);
225 hc->name = kstrdup(name, GFP_KERNEL);
235 hc->uuid = kstrdup(uuid, GFP_KERNEL);
243 hc->name_set = hc->uuid_set = false;
249 static void free_cell(struct hash_cell *hc)
259 * The kdev_t and uuid of a device can never change once it is
260 * initially inserted.
262 static int dm_hash_insert(const char *name, const char *uuid, struct mapped_device *md)
264 struct hash_cell *cell, *hc;
267 * Allocate the new cells.
269 cell = alloc_cell(name, uuid, md);
274 * Insert the cell into both hash tables.
276 down_write(&_hash_lock);
277 hc = __get_name_cell(name);
286 hc = __get_uuid_cell(uuid);
295 mutex_lock(&dm_hash_cells_mutex);
296 dm_set_mdptr(md, cell);
297 mutex_unlock(&dm_hash_cells_mutex);
298 up_write(&_hash_lock);
303 up_write(&_hash_lock);
308 static struct dm_table *__hash_remove(struct hash_cell *hc)
310 struct dm_table *table;
313 /* remove from the dev trees */
316 mutex_lock(&dm_hash_cells_mutex);
317 dm_set_mdptr(hc->md, NULL);
318 mutex_unlock(&dm_hash_cells_mutex);
320 table = dm_get_live_table(hc->md, &srcu_idx);
322 dm_table_event(table);
323 dm_put_live_table(hc->md, srcu_idx);
334 static void dm_hash_remove_all(bool keep_open_devices, bool mark_deferred, bool only_deferred)
338 struct hash_cell *hc;
339 struct mapped_device *md;
345 down_write(&_hash_lock);
347 for (n = rb_first(&name_rb_tree); n; n = rb_next(n)) {
348 hc = container_of(n, struct hash_cell, name_node);
352 if (keep_open_devices &&
353 dm_lock_for_deletion(md, mark_deferred, only_deferred)) {
359 t = __hash_remove(hc);
361 up_write(&_hash_lock);
367 dm_ima_measure_on_device_remove(md, true);
369 if (likely(keep_open_devices))
372 dm_destroy_immediate(md);
375 * Some mapped devices may be using other mapped
376 * devices, so repeat until we make no further
377 * progress. If a new mapped device is created
378 * here it will also get removed.
383 up_write(&_hash_lock);
386 DMWARN("remove_all left %d open device(s)", dev_skipped);
390 * Set the uuid of a hash_cell that isn't already set.
392 static void __set_cell_uuid(struct hash_cell *hc, char *new_uuid)
394 mutex_lock(&dm_hash_cells_mutex);
396 mutex_unlock(&dm_hash_cells_mutex);
402 * Changes the name of a hash_cell and returns the old name for
403 * the caller to free.
405 static char *__change_cell_name(struct hash_cell *hc, char *new_name)
410 * Rename and move the name cell.
415 mutex_lock(&dm_hash_cells_mutex);
417 mutex_unlock(&dm_hash_cells_mutex);
424 static struct mapped_device *dm_hash_rename(struct dm_ioctl *param,
427 char *new_data, *old_name = NULL;
428 struct hash_cell *hc;
429 struct dm_table *table;
430 struct mapped_device *md;
431 unsigned int change_uuid = (param->flags & DM_UUID_FLAG) ? 1 : 0;
437 new_data = kstrdup(new, GFP_KERNEL);
439 return ERR_PTR(-ENOMEM);
441 down_write(&_hash_lock);
447 hc = __get_uuid_cell(new);
449 hc = __get_name_cell(new);
452 DMERR("Unable to change %s on mapped device %s to one that already exists: %s",
453 change_uuid ? "uuid" : "name",
456 up_write(&_hash_lock);
458 return ERR_PTR(-EBUSY);
462 * Is there such a device as 'old' ?
464 hc = __get_name_cell(param->name);
466 DMERR("Unable to rename non-existent device, %s to %s%s",
467 param->name, change_uuid ? "uuid " : "", new);
468 up_write(&_hash_lock);
470 return ERR_PTR(-ENXIO);
474 * Does this device already have a uuid?
476 if (change_uuid && hc->uuid) {
477 DMERR("Unable to change uuid of mapped device %s to %s "
478 "because uuid is already set to %s",
479 param->name, new, hc->uuid);
481 up_write(&_hash_lock);
483 return ERR_PTR(-EINVAL);
487 __set_cell_uuid(hc, new_data);
489 old_name = __change_cell_name(hc, new_data);
492 * Wake up any dm event waiters.
494 table = dm_get_live_table(hc->md, &srcu_idx);
496 dm_table_event(table);
497 dm_put_live_table(hc->md, srcu_idx);
499 if (!dm_kobject_uevent(hc->md, KOBJ_CHANGE, param->event_nr, false))
500 param->flags |= DM_UEVENT_GENERATED_FLAG;
504 dm_ima_measure_on_device_rename(md);
506 up_write(&_hash_lock);
512 void dm_deferred_remove(void)
514 dm_hash_remove_all(true, false, true);
518 *---------------------------------------------------------------
519 * Implementation of the ioctl commands
520 *---------------------------------------------------------------
523 * All the ioctl commands get dispatched to functions with this
526 typedef int (*ioctl_fn)(struct file *filp, struct dm_ioctl *param, size_t param_size);
528 static int remove_all(struct file *filp, struct dm_ioctl *param, size_t param_size)
530 dm_hash_remove_all(true, !!(param->flags & DM_DEFERRED_REMOVE), false);
531 param->data_size = 0;
536 * Round up the ptr to an 8-byte boundary.
539 static inline size_t align_val(size_t val)
541 return (val + ALIGN_MASK) & ~ALIGN_MASK;
543 static inline void *align_ptr(void *ptr)
545 return (void *)align_val((size_t)ptr);
549 * Retrieves the data payload buffer from an already allocated
552 static void *get_result_buffer(struct dm_ioctl *param, size_t param_size,
555 param->data_start = align_ptr(param + 1) - (void *) param;
557 if (param->data_start < param_size)
558 *len = param_size - param->data_start;
562 return ((void *) param) + param->data_start;
565 static bool filter_device(struct hash_cell *hc, const char *pfx_name, const char *pfx_uuid)
568 size_t val_len, pfx_len;
571 val_len = strlen(val);
572 pfx_len = strnlen(pfx_name, DM_NAME_LEN);
573 if (pfx_len > val_len)
575 if (memcmp(val, pfx_name, pfx_len))
578 val = hc->uuid ? hc->uuid : "";
579 val_len = strlen(val);
580 pfx_len = strnlen(pfx_uuid, DM_UUID_LEN);
581 if (pfx_len > val_len)
583 if (memcmp(val, pfx_uuid, pfx_len))
589 static int list_devices(struct file *filp, struct dm_ioctl *param, size_t param_size)
592 struct hash_cell *hc;
593 size_t len, needed = 0;
594 struct gendisk *disk;
595 struct dm_name_list *orig_nl, *nl, *old_nl = NULL;
598 down_write(&_hash_lock);
601 * Loop through all the devices working out how much
604 for (n = rb_first(&name_rb_tree); n; n = rb_next(n)) {
605 hc = container_of(n, struct hash_cell, name_node);
606 if (!filter_device(hc, param->name, param->uuid))
608 needed += align_val(offsetof(struct dm_name_list, name) + strlen(hc->name) + 1);
609 needed += align_val(sizeof(uint32_t) * 2);
610 if (param->flags & DM_UUID_FLAG && hc->uuid)
611 needed += align_val(strlen(hc->uuid) + 1);
615 * Grab our output buffer.
617 nl = orig_nl = get_result_buffer(param, param_size, &len);
618 if (len < needed || len < sizeof(nl->dev)) {
619 param->flags |= DM_BUFFER_FULL_FLAG;
622 param->data_size = param->data_start + needed;
624 nl->dev = 0; /* Flags no data */
627 * Now loop through filling out the names.
629 for (n = rb_first(&name_rb_tree); n; n = rb_next(n)) {
632 hc = container_of(n, struct hash_cell, name_node);
633 if (!filter_device(hc, param->name, param->uuid))
636 old_nl->next = (uint32_t) ((void *) nl -
638 disk = dm_disk(hc->md);
639 nl->dev = huge_encode_dev(disk_devt(disk));
641 strcpy(nl->name, hc->name);
644 event_nr = align_ptr(nl->name + strlen(hc->name) + 1);
645 event_nr[0] = dm_get_event_nr(hc->md);
647 uuid_ptr = align_ptr(event_nr + 2);
648 if (param->flags & DM_UUID_FLAG) {
650 event_nr[1] |= DM_NAME_LIST_FLAG_HAS_UUID;
651 strcpy(uuid_ptr, hc->uuid);
652 uuid_ptr = align_ptr(uuid_ptr + strlen(hc->uuid) + 1);
654 event_nr[1] |= DM_NAME_LIST_FLAG_DOESNT_HAVE_UUID;
660 * If mismatch happens, security may be compromised due to buffer
661 * overflow, so it's better to crash.
663 BUG_ON((char *)nl - (char *)orig_nl != needed);
666 up_write(&_hash_lock);
670 static void list_version_get_needed(struct target_type *tt, void *needed_param)
672 size_t *needed = needed_param;
674 *needed += sizeof(struct dm_target_versions);
675 *needed += strlen(tt->name) + 1;
676 *needed += ALIGN_MASK;
679 static void list_version_get_info(struct target_type *tt, void *param)
681 struct vers_iter *info = param;
683 /* Check space - it might have changed since the first iteration */
684 if ((char *)info->vers + sizeof(tt->version) + strlen(tt->name) + 1 > info->end) {
685 info->flags = DM_BUFFER_FULL_FLAG;
690 info->old_vers->next = (uint32_t) ((void *)info->vers - (void *)info->old_vers);
692 info->vers->version[0] = tt->version[0];
693 info->vers->version[1] = tt->version[1];
694 info->vers->version[2] = tt->version[2];
695 info->vers->next = 0;
696 strcpy(info->vers->name, tt->name);
698 info->old_vers = info->vers;
699 info->vers = align_ptr((void *)(info->vers + 1) + strlen(tt->name) + 1);
702 static int __list_versions(struct dm_ioctl *param, size_t param_size, const char *name)
704 size_t len, needed = 0;
705 struct dm_target_versions *vers;
706 struct vers_iter iter_info;
707 struct target_type *tt = NULL;
710 tt = dm_get_target_type(name);
716 * Loop through all the devices working out how much
720 dm_target_iterate(list_version_get_needed, &needed);
722 list_version_get_needed(tt, &needed);
725 * Grab our output buffer.
727 vers = get_result_buffer(param, param_size, &len);
729 param->flags |= DM_BUFFER_FULL_FLAG;
732 param->data_size = param->data_start + needed;
734 iter_info.param_size = param_size;
735 iter_info.old_vers = NULL;
736 iter_info.vers = vers;
738 iter_info.end = (char *)vers + needed;
741 * Now loop through filling out the names & versions.
744 dm_target_iterate(list_version_get_info, &iter_info);
746 list_version_get_info(tt, &iter_info);
747 param->flags |= iter_info.flags;
751 dm_put_target_type(tt);
755 static int list_versions(struct file *filp, struct dm_ioctl *param, size_t param_size)
757 return __list_versions(param, param_size, NULL);
760 static int get_target_version(struct file *filp, struct dm_ioctl *param, size_t param_size)
762 return __list_versions(param, param_size, param->name);
765 static int check_name(const char *name)
767 if (strchr(name, '/')) {
768 DMERR("invalid device name");
776 * On successful return, the caller must not attempt to acquire
777 * _hash_lock without first calling dm_put_live_table, because dm_table_destroy
778 * waits for this dm_put_live_table and could be called under this lock.
780 static struct dm_table *dm_get_inactive_table(struct mapped_device *md, int *srcu_idx)
782 struct hash_cell *hc;
783 struct dm_table *table = NULL;
785 /* increment rcu count, we don't care about the table pointer */
786 dm_get_live_table(md, srcu_idx);
788 down_read(&_hash_lock);
789 hc = dm_get_mdptr(md);
790 if (!hc || hc->md != md) {
791 DMERR("device has been removed from the dev hash table.");
798 up_read(&_hash_lock);
803 static struct dm_table *dm_get_live_or_inactive_table(struct mapped_device *md,
804 struct dm_ioctl *param,
807 return (param->flags & DM_QUERY_INACTIVE_TABLE_FLAG) ?
808 dm_get_inactive_table(md, srcu_idx) : dm_get_live_table(md, srcu_idx);
812 * Fills in a dm_ioctl structure, ready for sending back to
815 static void __dev_status(struct mapped_device *md, struct dm_ioctl *param)
817 struct gendisk *disk = dm_disk(md);
818 struct dm_table *table;
821 param->flags &= ~(DM_SUSPEND_FLAG | DM_READONLY_FLAG |
822 DM_ACTIVE_PRESENT_FLAG | DM_INTERNAL_SUSPEND_FLAG);
824 if (dm_suspended_md(md))
825 param->flags |= DM_SUSPEND_FLAG;
827 if (dm_suspended_internally_md(md))
828 param->flags |= DM_INTERNAL_SUSPEND_FLAG;
830 if (dm_test_deferred_remove_flag(md))
831 param->flags |= DM_DEFERRED_REMOVE;
833 param->dev = huge_encode_dev(disk_devt(disk));
836 * Yes, this will be out of date by the time it gets back
837 * to userland, but it is still very useful for
840 param->open_count = dm_open_count(md);
842 param->event_nr = dm_get_event_nr(md);
843 param->target_count = 0;
845 table = dm_get_live_table(md, &srcu_idx);
847 if (!(param->flags & DM_QUERY_INACTIVE_TABLE_FLAG)) {
848 if (get_disk_ro(disk))
849 param->flags |= DM_READONLY_FLAG;
850 param->target_count = table->num_targets;
853 param->flags |= DM_ACTIVE_PRESENT_FLAG;
855 dm_put_live_table(md, srcu_idx);
857 if (param->flags & DM_QUERY_INACTIVE_TABLE_FLAG) {
860 table = dm_get_inactive_table(md, &srcu_idx);
862 if (!(dm_table_get_mode(table) & FMODE_WRITE))
863 param->flags |= DM_READONLY_FLAG;
864 param->target_count = table->num_targets;
866 dm_put_live_table(md, srcu_idx);
870 static int dev_create(struct file *filp, struct dm_ioctl *param, size_t param_size)
872 int r, m = DM_ANY_MINOR;
873 struct mapped_device *md;
875 r = check_name(param->name);
879 if (param->flags & DM_PERSISTENT_DEV_FLAG)
880 m = MINOR(huge_decode_dev(param->dev));
882 r = dm_create(m, &md);
886 r = dm_hash_insert(param->name, *param->uuid ? param->uuid : NULL, md);
893 param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
895 __dev_status(md, param);
903 * Always use UUID for lookups if it's present, otherwise use name or dev.
905 static struct hash_cell *__find_device_hash_cell(struct dm_ioctl *param)
907 struct hash_cell *hc = NULL;
910 if (*param->name || param->dev) {
911 DMERR("Invalid ioctl structure: uuid %s, name %s, dev %llx",
912 param->uuid, param->name, (unsigned long long)param->dev);
916 hc = __get_uuid_cell(param->uuid);
919 } else if (*param->name) {
921 DMERR("Invalid ioctl structure: name %s, dev %llx",
922 param->name, (unsigned long long)param->dev);
926 hc = __get_name_cell(param->name);
929 } else if (param->dev) {
930 hc = __get_dev_cell(param->dev);
937 * Sneakily write in both the name and the uuid
938 * while we have the cell.
940 strlcpy(param->name, hc->name, sizeof(param->name));
942 strlcpy(param->uuid, hc->uuid, sizeof(param->uuid));
944 param->uuid[0] = '\0';
947 param->flags |= DM_INACTIVE_PRESENT_FLAG;
949 param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
954 static struct mapped_device *find_device(struct dm_ioctl *param)
956 struct hash_cell *hc;
957 struct mapped_device *md = NULL;
959 down_read(&_hash_lock);
960 hc = __find_device_hash_cell(param);
963 up_read(&_hash_lock);
968 static int dev_remove(struct file *filp, struct dm_ioctl *param, size_t param_size)
970 struct hash_cell *hc;
971 struct mapped_device *md;
975 down_write(&_hash_lock);
976 hc = __find_device_hash_cell(param);
979 DMDEBUG_LIMIT("device doesn't appear to be in the dev hash table.");
980 up_write(&_hash_lock);
987 * Ensure the device is not open and nothing further can open it.
989 r = dm_lock_for_deletion(md, !!(param->flags & DM_DEFERRED_REMOVE), false);
991 if (r == -EBUSY && param->flags & DM_DEFERRED_REMOVE) {
992 up_write(&_hash_lock);
996 DMDEBUG_LIMIT("unable to remove open device %s", hc->name);
997 up_write(&_hash_lock);
1002 t = __hash_remove(hc);
1003 up_write(&_hash_lock);
1007 dm_table_destroy(t);
1010 param->flags &= ~DM_DEFERRED_REMOVE;
1012 dm_ima_measure_on_device_remove(md, false);
1014 if (!dm_kobject_uevent(md, KOBJ_REMOVE, param->event_nr, false))
1015 param->flags |= DM_UEVENT_GENERATED_FLAG;
1023 * Check a string doesn't overrun the chunk of
1024 * memory we copied from userland.
1026 static int invalid_str(char *str, void *end)
1028 while ((void *) str < end)
1035 static int dev_rename(struct file *filp, struct dm_ioctl *param, size_t param_size)
1038 char *new_data = (char *) param + param->data_start;
1039 struct mapped_device *md;
1040 unsigned int change_uuid = (param->flags & DM_UUID_FLAG) ? 1 : 0;
1042 if (new_data < param->data ||
1043 invalid_str(new_data, (void *) param + param_size) || !*new_data ||
1044 strlen(new_data) > (change_uuid ? DM_UUID_LEN - 1 : DM_NAME_LEN - 1)) {
1045 DMERR("Invalid new mapped device name or uuid string supplied.");
1050 r = check_name(new_data);
1055 md = dm_hash_rename(param, new_data);
1059 __dev_status(md, param);
1065 static int dev_set_geometry(struct file *filp, struct dm_ioctl *param, size_t param_size)
1068 struct mapped_device *md;
1069 struct hd_geometry geometry;
1070 unsigned long indata[4];
1071 char *geostr = (char *) param + param->data_start;
1074 md = find_device(param);
1078 if (geostr < param->data ||
1079 invalid_str(geostr, (void *) param + param_size)) {
1080 DMERR("Invalid geometry supplied.");
1084 x = sscanf(geostr, "%lu %lu %lu %lu%c", indata,
1085 indata + 1, indata + 2, indata + 3, &dummy);
1088 DMERR("Unable to interpret geometry settings.");
1092 if (indata[0] > 65535 || indata[1] > 255 || indata[2] > 255) {
1093 DMERR("Geometry exceeds range limits.");
1097 geometry.cylinders = indata[0];
1098 geometry.heads = indata[1];
1099 geometry.sectors = indata[2];
1100 geometry.start = indata[3];
1102 r = dm_set_geometry(md, &geometry);
1104 param->data_size = 0;
1111 static int do_suspend(struct dm_ioctl *param)
1114 unsigned int suspend_flags = DM_SUSPEND_LOCKFS_FLAG;
1115 struct mapped_device *md;
1117 md = find_device(param);
1121 if (param->flags & DM_SKIP_LOCKFS_FLAG)
1122 suspend_flags &= ~DM_SUSPEND_LOCKFS_FLAG;
1123 if (param->flags & DM_NOFLUSH_FLAG)
1124 suspend_flags |= DM_SUSPEND_NOFLUSH_FLAG;
1126 if (!dm_suspended_md(md)) {
1127 r = dm_suspend(md, suspend_flags);
1132 __dev_status(md, param);
1140 static int do_resume(struct dm_ioctl *param)
1143 unsigned int suspend_flags = DM_SUSPEND_LOCKFS_FLAG;
1144 struct hash_cell *hc;
1145 struct mapped_device *md;
1146 struct dm_table *new_map, *old_map = NULL;
1147 bool need_resize_uevent = false;
1149 down_write(&_hash_lock);
1151 hc = __find_device_hash_cell(param);
1153 DMDEBUG_LIMIT("device doesn't appear to be in the dev hash table.");
1154 up_write(&_hash_lock);
1160 new_map = hc->new_map;
1162 param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
1164 up_write(&_hash_lock);
1166 /* Do we need to load a new map ? */
1168 sector_t old_size, new_size;
1170 /* Suspend if it isn't already suspended */
1171 if (param->flags & DM_SKIP_LOCKFS_FLAG)
1172 suspend_flags &= ~DM_SUSPEND_LOCKFS_FLAG;
1173 if (param->flags & DM_NOFLUSH_FLAG)
1174 suspend_flags |= DM_SUSPEND_NOFLUSH_FLAG;
1175 if (!dm_suspended_md(md))
1176 dm_suspend(md, suspend_flags);
1178 old_size = dm_get_size(md);
1179 old_map = dm_swap_table(md, new_map);
1180 if (IS_ERR(old_map)) {
1182 dm_table_destroy(new_map);
1184 return PTR_ERR(old_map);
1186 new_size = dm_get_size(md);
1187 if (old_size && new_size && old_size != new_size)
1188 need_resize_uevent = true;
1190 if (dm_table_get_mode(new_map) & FMODE_WRITE)
1191 set_disk_ro(dm_disk(md), 0);
1193 set_disk_ro(dm_disk(md), 1);
1196 if (dm_suspended_md(md)) {
1199 dm_ima_measure_on_device_resume(md, new_map ? true : false);
1201 if (!dm_kobject_uevent(md, KOBJ_CHANGE, param->event_nr, need_resize_uevent))
1202 param->flags |= DM_UEVENT_GENERATED_FLAG;
1207 * Since dm_swap_table synchronizes RCU, nobody should be in
1208 * read-side critical section already.
1211 dm_table_destroy(old_map);
1214 __dev_status(md, param);
1221 * Set or unset the suspension state of a device.
1222 * If the device already is in the requested state we just return its status.
1224 static int dev_suspend(struct file *filp, struct dm_ioctl *param, size_t param_size)
1226 if (param->flags & DM_SUSPEND_FLAG)
1227 return do_suspend(param);
1229 return do_resume(param);
1233 * Copies device info back to user space, used by
1234 * the create and info ioctls.
1236 static int dev_status(struct file *filp, struct dm_ioctl *param, size_t param_size)
1238 struct mapped_device *md;
1240 md = find_device(param);
1244 __dev_status(md, param);
1251 * Build up the status struct for each target
1253 static void retrieve_status(struct dm_table *table,
1254 struct dm_ioctl *param, size_t param_size)
1256 unsigned int i, num_targets;
1257 struct dm_target_spec *spec;
1258 char *outbuf, *outptr;
1260 size_t remaining, len, used = 0;
1261 unsigned int status_flags = 0;
1263 outptr = outbuf = get_result_buffer(param, param_size, &len);
1265 if (param->flags & DM_STATUS_TABLE_FLAG)
1266 type = STATUSTYPE_TABLE;
1267 else if (param->flags & DM_IMA_MEASUREMENT_FLAG)
1268 type = STATUSTYPE_IMA;
1270 type = STATUSTYPE_INFO;
1272 /* Get all the target info */
1273 num_targets = table->num_targets;
1274 for (i = 0; i < num_targets; i++) {
1275 struct dm_target *ti = dm_table_get_target(table, i);
1278 remaining = len - (outptr - outbuf);
1279 if (remaining <= sizeof(struct dm_target_spec)) {
1280 param->flags |= DM_BUFFER_FULL_FLAG;
1284 spec = (struct dm_target_spec *) outptr;
1287 spec->sector_start = ti->begin;
1288 spec->length = ti->len;
1289 strncpy(spec->target_type, ti->type->name,
1290 sizeof(spec->target_type) - 1);
1292 outptr += sizeof(struct dm_target_spec);
1293 remaining = len - (outptr - outbuf);
1294 if (remaining <= 0) {
1295 param->flags |= DM_BUFFER_FULL_FLAG;
1299 /* Get the status/table string from the target driver */
1300 if (ti->type->status) {
1301 if (param->flags & DM_NOFLUSH_FLAG)
1302 status_flags |= DM_STATUS_NOFLUSH_FLAG;
1303 ti->type->status(ti, type, status_flags, outptr, remaining);
1307 l = strlen(outptr) + 1;
1308 if (l == remaining) {
1309 param->flags |= DM_BUFFER_FULL_FLAG;
1314 used = param->data_start + (outptr - outbuf);
1316 outptr = align_ptr(outptr);
1317 spec->next = outptr - outbuf;
1321 param->data_size = used;
1323 param->target_count = num_targets;
1327 * Wait for a device to report an event
1329 static int dev_wait(struct file *filp, struct dm_ioctl *param, size_t param_size)
1332 struct mapped_device *md;
1333 struct dm_table *table;
1336 md = find_device(param);
1341 * Wait for a notification event
1343 if (dm_wait_event(md, param->event_nr)) {
1349 * The userland program is going to want to know what
1350 * changed to trigger the event, so we may as well tell
1351 * him and save an ioctl.
1353 __dev_status(md, param);
1355 table = dm_get_live_or_inactive_table(md, param, &srcu_idx);
1357 retrieve_status(table, param, param_size);
1358 dm_put_live_table(md, srcu_idx);
1367 * Remember the global event number and make it possible to poll
1368 * for further events.
1370 static int dev_arm_poll(struct file *filp, struct dm_ioctl *param, size_t param_size)
1372 struct dm_file *priv = filp->private_data;
1374 priv->global_event_nr = atomic_read(&dm_global_event_nr);
1379 static inline fmode_t get_mode(struct dm_ioctl *param)
1381 fmode_t mode = FMODE_READ | FMODE_WRITE;
1383 if (param->flags & DM_READONLY_FLAG)
1389 static int next_target(struct dm_target_spec *last, uint32_t next, void *end,
1390 struct dm_target_spec **spec, char **target_params)
1392 *spec = (struct dm_target_spec *) ((unsigned char *) last + next);
1393 *target_params = (char *) (*spec + 1);
1395 if (*spec < (last + 1))
1398 return invalid_str(*target_params, end);
1401 static int populate_table(struct dm_table *table,
1402 struct dm_ioctl *param, size_t param_size)
1406 struct dm_target_spec *spec = (struct dm_target_spec *) param;
1407 uint32_t next = param->data_start;
1408 void *end = (void *) param + param_size;
1409 char *target_params;
1411 if (!param->target_count) {
1412 DMERR("populate_table: no targets specified");
1416 for (i = 0; i < param->target_count; i++) {
1418 r = next_target(spec, next, end, &spec, &target_params);
1420 DMERR("unable to find target");
1424 r = dm_table_add_target(table, spec->target_type,
1425 (sector_t) spec->sector_start,
1426 (sector_t) spec->length,
1429 DMERR("error adding target to table");
1436 return dm_table_complete(table);
1439 static bool is_valid_type(enum dm_queue_mode cur, enum dm_queue_mode new)
1442 (cur == DM_TYPE_BIO_BASED && new == DM_TYPE_DAX_BIO_BASED))
1448 static int table_load(struct file *filp, struct dm_ioctl *param, size_t param_size)
1451 struct hash_cell *hc;
1452 struct dm_table *t, *old_map = NULL;
1453 struct mapped_device *md;
1454 struct target_type *immutable_target_type;
1456 md = find_device(param);
1460 r = dm_table_create(&t, get_mode(param), param->target_count, md);
1464 /* Protect md->type and md->queue against concurrent table loads. */
1465 dm_lock_md_type(md);
1466 r = populate_table(t, param, param_size);
1468 goto err_unlock_md_type;
1470 dm_ima_measure_on_table_load(t, STATUSTYPE_IMA);
1472 immutable_target_type = dm_get_immutable_target_type(md);
1473 if (immutable_target_type &&
1474 (immutable_target_type != dm_table_get_immutable_target_type(t)) &&
1475 !dm_table_get_wildcard_target(t)) {
1476 DMERR("can't replace immutable target type %s",
1477 immutable_target_type->name);
1479 goto err_unlock_md_type;
1482 if (dm_get_md_type(md) == DM_TYPE_NONE) {
1483 /* setup md->queue to reflect md's type (may block) */
1484 r = dm_setup_md_queue(md, t);
1486 DMERR("unable to set up device queue for new table.");
1487 goto err_unlock_md_type;
1489 } else if (!is_valid_type(dm_get_md_type(md), dm_table_get_type(t))) {
1490 DMERR("can't change device type (old=%u vs new=%u) after initial table load.",
1491 dm_get_md_type(md), dm_table_get_type(t));
1493 goto err_unlock_md_type;
1496 dm_unlock_md_type(md);
1498 /* stage inactive table */
1499 down_write(&_hash_lock);
1500 hc = dm_get_mdptr(md);
1501 if (!hc || hc->md != md) {
1502 DMERR("device has been removed from the dev hash table.");
1503 up_write(&_hash_lock);
1505 goto err_destroy_table;
1509 old_map = hc->new_map;
1511 up_write(&_hash_lock);
1513 param->flags |= DM_INACTIVE_PRESENT_FLAG;
1514 __dev_status(md, param);
1518 dm_table_destroy(old_map);
1526 dm_unlock_md_type(md);
1528 dm_table_destroy(t);
1535 static int table_clear(struct file *filp, struct dm_ioctl *param, size_t param_size)
1537 struct hash_cell *hc;
1538 struct mapped_device *md;
1539 struct dm_table *old_map = NULL;
1540 bool has_new_map = false;
1542 down_write(&_hash_lock);
1544 hc = __find_device_hash_cell(param);
1546 DMDEBUG_LIMIT("device doesn't appear to be in the dev hash table.");
1547 up_write(&_hash_lock);
1552 old_map = hc->new_map;
1557 param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
1559 __dev_status(hc->md, param);
1561 up_write(&_hash_lock);
1564 dm_table_destroy(old_map);
1566 dm_ima_measure_on_table_clear(md, has_new_map);
1573 * Retrieves a list of devices used by a particular dm device.
1575 static void retrieve_deps(struct dm_table *table,
1576 struct dm_ioctl *param, size_t param_size)
1578 unsigned int count = 0;
1579 struct list_head *tmp;
1581 struct dm_dev_internal *dd;
1582 struct dm_target_deps *deps;
1584 deps = get_result_buffer(param, param_size, &len);
1587 * Count the devices.
1589 list_for_each(tmp, dm_table_get_devices(table))
1593 * Check we have enough space.
1595 needed = struct_size(deps, dev, count);
1597 param->flags |= DM_BUFFER_FULL_FLAG;
1602 * Fill in the devices.
1604 deps->count = count;
1606 list_for_each_entry(dd, dm_table_get_devices(table), list)
1607 deps->dev[count++] = huge_encode_dev(dd->dm_dev->bdev->bd_dev);
1609 param->data_size = param->data_start + needed;
1612 static int table_deps(struct file *filp, struct dm_ioctl *param, size_t param_size)
1614 struct mapped_device *md;
1615 struct dm_table *table;
1618 md = find_device(param);
1622 __dev_status(md, param);
1624 table = dm_get_live_or_inactive_table(md, param, &srcu_idx);
1626 retrieve_deps(table, param, param_size);
1627 dm_put_live_table(md, srcu_idx);
1635 * Return the status of a device as a text string for each
1638 static int table_status(struct file *filp, struct dm_ioctl *param, size_t param_size)
1640 struct mapped_device *md;
1641 struct dm_table *table;
1644 md = find_device(param);
1648 __dev_status(md, param);
1650 table = dm_get_live_or_inactive_table(md, param, &srcu_idx);
1652 retrieve_status(table, param, param_size);
1653 dm_put_live_table(md, srcu_idx);
1661 * Process device-mapper dependent messages. Messages prefixed with '@'
1662 * are processed by the DM core. All others are delivered to the target.
1663 * Returns a number <= 1 if message was processed by device mapper.
1664 * Returns 2 if message should be delivered to the target.
1666 static int message_for_md(struct mapped_device *md, unsigned int argc, char **argv,
1667 char *result, unsigned int maxlen)
1672 return 2; /* no '@' prefix, deliver to target */
1674 if (!strcasecmp(argv[0], "@cancel_deferred_remove")) {
1676 DMERR("Invalid arguments for @cancel_deferred_remove");
1679 return dm_cancel_deferred_remove(md);
1682 r = dm_stats_message(md, argc, argv, result, maxlen);
1686 DMERR("Unsupported message sent to DM core: %s", argv[0]);
1691 * Pass a message to the target that's at the supplied device offset.
1693 static int target_message(struct file *filp, struct dm_ioctl *param, size_t param_size)
1697 struct mapped_device *md;
1698 struct dm_table *table;
1699 struct dm_target *ti;
1700 struct dm_target_msg *tmsg = (void *) param + param->data_start;
1702 char *result = get_result_buffer(param, param_size, &maxlen);
1705 md = find_device(param);
1709 if (tmsg < (struct dm_target_msg *) param->data ||
1710 invalid_str(tmsg->message, (void *) param + param_size)) {
1711 DMERR("Invalid target message parameters.");
1716 r = dm_split_args(&argc, &argv, tmsg->message);
1718 DMERR("Failed to split target message parameters");
1723 DMERR("Empty message received.");
1728 r = message_for_md(md, argc, argv, result, maxlen);
1732 table = dm_get_live_table(md, &srcu_idx);
1736 if (dm_deleting_md(md)) {
1741 ti = dm_table_find_target(table, tmsg->sector);
1743 DMERR("Target message sector outside device.");
1745 } else if (ti->type->message)
1746 r = ti->type->message(ti, argc, argv, result, maxlen);
1748 DMERR("Target type does not support messages");
1753 dm_put_live_table(md, srcu_idx);
1758 __dev_status(md, param);
1761 param->flags |= DM_DATA_OUT_FLAG;
1762 if (dm_message_test_buffer_overflow(result, maxlen))
1763 param->flags |= DM_BUFFER_FULL_FLAG;
1765 param->data_size = param->data_start + strlen(result) + 1;
1774 * The ioctl parameter block consists of two parts, a dm_ioctl struct
1775 * followed by a data buffer. This flag is set if the second part,
1776 * which has a variable size, is not used by the function processing
1779 #define IOCTL_FLAGS_NO_PARAMS 1
1780 #define IOCTL_FLAGS_ISSUE_GLOBAL_EVENT 2
1783 *---------------------------------------------------------------
1784 * Implementation of open/close/ioctl on the special char device.
1785 *---------------------------------------------------------------
1787 static ioctl_fn lookup_ioctl(unsigned int cmd, int *ioctl_flags)
1789 static const struct {
1794 {DM_VERSION_CMD, 0, NULL}, /* version is dealt with elsewhere */
1795 {DM_REMOVE_ALL_CMD, IOCTL_FLAGS_NO_PARAMS | IOCTL_FLAGS_ISSUE_GLOBAL_EVENT, remove_all},
1796 {DM_LIST_DEVICES_CMD, 0, list_devices},
1798 {DM_DEV_CREATE_CMD, IOCTL_FLAGS_NO_PARAMS | IOCTL_FLAGS_ISSUE_GLOBAL_EVENT, dev_create},
1799 {DM_DEV_REMOVE_CMD, IOCTL_FLAGS_NO_PARAMS | IOCTL_FLAGS_ISSUE_GLOBAL_EVENT, dev_remove},
1800 {DM_DEV_RENAME_CMD, IOCTL_FLAGS_ISSUE_GLOBAL_EVENT, dev_rename},
1801 {DM_DEV_SUSPEND_CMD, IOCTL_FLAGS_NO_PARAMS, dev_suspend},
1802 {DM_DEV_STATUS_CMD, IOCTL_FLAGS_NO_PARAMS, dev_status},
1803 {DM_DEV_WAIT_CMD, 0, dev_wait},
1805 {DM_TABLE_LOAD_CMD, 0, table_load},
1806 {DM_TABLE_CLEAR_CMD, IOCTL_FLAGS_NO_PARAMS, table_clear},
1807 {DM_TABLE_DEPS_CMD, 0, table_deps},
1808 {DM_TABLE_STATUS_CMD, 0, table_status},
1810 {DM_LIST_VERSIONS_CMD, 0, list_versions},
1812 {DM_TARGET_MSG_CMD, 0, target_message},
1813 {DM_DEV_SET_GEOMETRY_CMD, 0, dev_set_geometry},
1814 {DM_DEV_ARM_POLL_CMD, IOCTL_FLAGS_NO_PARAMS, dev_arm_poll},
1815 {DM_GET_TARGET_VERSION_CMD, 0, get_target_version},
1818 if (unlikely(cmd >= ARRAY_SIZE(_ioctls)))
1821 cmd = array_index_nospec(cmd, ARRAY_SIZE(_ioctls));
1822 *ioctl_flags = _ioctls[cmd].flags;
1823 return _ioctls[cmd].fn;
1827 * As well as checking the version compatibility this always
1828 * copies the kernel interface version out.
1830 static int check_version(unsigned int cmd, struct dm_ioctl __user *user)
1832 uint32_t version[3];
1835 if (copy_from_user(version, user->version, sizeof(version)))
1838 if ((DM_VERSION_MAJOR != version[0]) ||
1839 (DM_VERSION_MINOR < version[1])) {
1840 DMERR("ioctl interface mismatch: kernel(%u.%u.%u), user(%u.%u.%u), cmd(%d)",
1841 DM_VERSION_MAJOR, DM_VERSION_MINOR,
1842 DM_VERSION_PATCHLEVEL,
1843 version[0], version[1], version[2], cmd);
1848 * Fill in the kernel version.
1850 version[0] = DM_VERSION_MAJOR;
1851 version[1] = DM_VERSION_MINOR;
1852 version[2] = DM_VERSION_PATCHLEVEL;
1853 if (copy_to_user(user->version, version, sizeof(version)))
1859 #define DM_PARAMS_MALLOC 0x0001 /* Params allocated with kvmalloc() */
1860 #define DM_WIPE_BUFFER 0x0010 /* Wipe input buffer before returning from ioctl */
1862 static void free_params(struct dm_ioctl *param, size_t param_size, int param_flags)
1864 if (param_flags & DM_WIPE_BUFFER)
1865 memset(param, 0, param_size);
1867 if (param_flags & DM_PARAMS_MALLOC)
1871 static int copy_params(struct dm_ioctl __user *user, struct dm_ioctl *param_kernel,
1872 int ioctl_flags, struct dm_ioctl **param, int *param_flags)
1874 struct dm_ioctl *dmi;
1876 const size_t minimum_data_size = offsetof(struct dm_ioctl, data);
1877 unsigned int noio_flag;
1879 if (copy_from_user(param_kernel, user, minimum_data_size))
1882 if (param_kernel->data_size < minimum_data_size) {
1883 DMERR("Invalid data size in the ioctl structure: %u",
1884 param_kernel->data_size);
1888 secure_data = param_kernel->flags & DM_SECURE_DATA_FLAG;
1890 *param_flags = secure_data ? DM_WIPE_BUFFER : 0;
1892 if (ioctl_flags & IOCTL_FLAGS_NO_PARAMS) {
1894 dmi->data_size = minimum_data_size;
1899 * Use __GFP_HIGH to avoid low memory issues when a device is
1900 * suspended and the ioctl is needed to resume it.
1901 * Use kmalloc() rather than vmalloc() when we can.
1904 noio_flag = memalloc_noio_save();
1905 dmi = kvmalloc(param_kernel->data_size, GFP_KERNEL | __GFP_HIGH);
1906 memalloc_noio_restore(noio_flag);
1909 if (secure_data && clear_user(user, param_kernel->data_size))
1914 *param_flags |= DM_PARAMS_MALLOC;
1916 /* Copy from param_kernel (which was already copied from user) */
1917 memcpy(dmi, param_kernel, minimum_data_size);
1919 if (copy_from_user(&dmi->data, (char __user *)user + minimum_data_size,
1920 param_kernel->data_size - minimum_data_size))
1923 /* Wipe the user buffer so we do not return it to userspace */
1924 if (secure_data && clear_user(user, param_kernel->data_size))
1931 free_params(dmi, param_kernel->data_size, *param_flags);
1936 static int validate_params(uint cmd, struct dm_ioctl *param)
1938 /* Always clear this flag */
1939 param->flags &= ~DM_BUFFER_FULL_FLAG;
1940 param->flags &= ~DM_UEVENT_GENERATED_FLAG;
1941 param->flags &= ~DM_SECURE_DATA_FLAG;
1942 param->flags &= ~DM_DATA_OUT_FLAG;
1944 /* Ignores parameters */
1945 if (cmd == DM_REMOVE_ALL_CMD ||
1946 cmd == DM_LIST_DEVICES_CMD ||
1947 cmd == DM_LIST_VERSIONS_CMD)
1950 if (cmd == DM_DEV_CREATE_CMD) {
1951 if (!*param->name) {
1952 DMERR("name not supplied when creating device");
1955 } else if (*param->uuid && *param->name) {
1956 DMERR("only supply one of name or uuid, cmd(%u)", cmd);
1960 /* Ensure strings are terminated */
1961 param->name[DM_NAME_LEN - 1] = '\0';
1962 param->uuid[DM_UUID_LEN - 1] = '\0';
1967 static int ctl_ioctl(struct file *file, uint command, struct dm_ioctl __user *user)
1973 struct dm_ioctl *param;
1975 size_t input_param_size;
1976 struct dm_ioctl param_kernel;
1978 /* only root can play with this */
1979 if (!capable(CAP_SYS_ADMIN))
1982 if (_IOC_TYPE(command) != DM_IOCTL)
1985 cmd = _IOC_NR(command);
1988 * Check the interface version passed in. This also
1989 * writes out the kernel's interface version.
1991 r = check_version(cmd, user);
1996 * Nothing more to do for the version command.
1998 if (cmd == DM_VERSION_CMD)
2001 fn = lookup_ioctl(cmd, &ioctl_flags);
2003 DMERR("dm_ctl_ioctl: unknown command 0x%x", command);
2008 * Copy the parameters into kernel space.
2010 r = copy_params(user, ¶m_kernel, ioctl_flags, ¶m, ¶m_flags);
2015 input_param_size = param->data_size;
2016 r = validate_params(cmd, param);
2020 param->data_size = offsetof(struct dm_ioctl, data);
2021 r = fn(file, param, input_param_size);
2023 if (unlikely(param->flags & DM_BUFFER_FULL_FLAG) &&
2024 unlikely(ioctl_flags & IOCTL_FLAGS_NO_PARAMS))
2025 DMERR("ioctl %d tried to output some data but has IOCTL_FLAGS_NO_PARAMS set", cmd);
2027 if (!r && ioctl_flags & IOCTL_FLAGS_ISSUE_GLOBAL_EVENT)
2028 dm_issue_global_event();
2031 * Copy the results back to userland.
2033 if (!r && copy_to_user(user, param, param->data_size))
2037 free_params(param, input_param_size, param_flags);
2041 static long dm_ctl_ioctl(struct file *file, uint command, ulong u)
2043 return (long)ctl_ioctl(file, command, (struct dm_ioctl __user *)u);
2046 #ifdef CONFIG_COMPAT
2047 static long dm_compat_ctl_ioctl(struct file *file, uint command, ulong u)
2049 return (long)dm_ctl_ioctl(file, command, (ulong) compat_ptr(u));
2052 #define dm_compat_ctl_ioctl NULL
2055 static int dm_open(struct inode *inode, struct file *filp)
2058 struct dm_file *priv;
2060 r = nonseekable_open(inode, filp);
2064 priv = filp->private_data = kmalloc(sizeof(struct dm_file), GFP_KERNEL);
2068 priv->global_event_nr = atomic_read(&dm_global_event_nr);
2073 static int dm_release(struct inode *inode, struct file *filp)
2075 kfree(filp->private_data);
2079 static __poll_t dm_poll(struct file *filp, poll_table *wait)
2081 struct dm_file *priv = filp->private_data;
2084 poll_wait(filp, &dm_global_eventq, wait);
2086 if ((int)(atomic_read(&dm_global_event_nr) - priv->global_event_nr) > 0)
2092 static const struct file_operations _ctl_fops = {
2094 .release = dm_release,
2096 .unlocked_ioctl = dm_ctl_ioctl,
2097 .compat_ioctl = dm_compat_ctl_ioctl,
2098 .owner = THIS_MODULE,
2099 .llseek = noop_llseek,
2102 static struct miscdevice _dm_misc = {
2103 .minor = MAPPER_CTRL_MINOR,
2105 .nodename = DM_DIR "/" DM_CONTROL_NODE,
2109 MODULE_ALIAS_MISCDEV(MAPPER_CTRL_MINOR);
2110 MODULE_ALIAS("devname:" DM_DIR "/" DM_CONTROL_NODE);
2113 * Create misc character device and link to DM_DIR/control.
2115 int __init dm_interface_init(void)
2119 r = misc_register(&_dm_misc);
2121 DMERR("misc_register failed for control device");
2125 DMINFO("%d.%d.%d%s initialised: %s", DM_VERSION_MAJOR,
2126 DM_VERSION_MINOR, DM_VERSION_PATCHLEVEL, DM_VERSION_EXTRA,
2131 void dm_interface_exit(void)
2133 misc_deregister(&_dm_misc);
2138 * dm_copy_name_and_uuid - Copy mapped device name & uuid into supplied buffers
2139 * @md: Pointer to mapped_device
2140 * @name: Buffer (size DM_NAME_LEN) for name
2141 * @uuid: Buffer (size DM_UUID_LEN) for uuid or empty string if uuid not defined
2143 int dm_copy_name_and_uuid(struct mapped_device *md, char *name, char *uuid)
2146 struct hash_cell *hc;
2151 mutex_lock(&dm_hash_cells_mutex);
2152 hc = dm_get_mdptr(md);
2153 if (!hc || hc->md != md) {
2159 strcpy(name, hc->name);
2161 strcpy(uuid, hc->uuid ? : "");
2164 mutex_unlock(&dm_hash_cells_mutex);
2168 EXPORT_SYMBOL_GPL(dm_copy_name_and_uuid);
2171 * dm_early_create - create a mapped device in early boot.
2173 * @dmi: Contains main information of the device mapping to be created.
2174 * @spec_array: array of pointers to struct dm_target_spec. Describes the
2175 * mapping table of the device.
2176 * @target_params_array: array of strings with the parameters to a specific
2179 * Instead of having the struct dm_target_spec and the parameters for every
2180 * target embedded at the end of struct dm_ioctl (as performed in a normal
2181 * ioctl), pass them as arguments, so the caller doesn't need to serialize them.
2182 * The size of the spec_array and target_params_array is given by
2183 * @dmi->target_count.
2184 * This function is supposed to be called in early boot, so locking mechanisms
2185 * to protect against concurrent loads are not required.
2187 int __init dm_early_create(struct dm_ioctl *dmi,
2188 struct dm_target_spec **spec_array,
2189 char **target_params_array)
2191 int r, m = DM_ANY_MINOR;
2192 struct dm_table *t, *old_map;
2193 struct mapped_device *md;
2196 if (!dmi->target_count)
2199 r = check_name(dmi->name);
2203 if (dmi->flags & DM_PERSISTENT_DEV_FLAG)
2204 m = MINOR(huge_decode_dev(dmi->dev));
2206 /* alloc dm device */
2207 r = dm_create(m, &md);
2212 r = dm_hash_insert(dmi->name, *dmi->uuid ? dmi->uuid : NULL, md);
2214 goto err_destroy_dm;
2217 r = dm_table_create(&t, get_mode(dmi), dmi->target_count, md);
2219 goto err_hash_remove;
2222 for (i = 0; i < dmi->target_count; i++) {
2223 r = dm_table_add_target(t, spec_array[i]->target_type,
2224 (sector_t) spec_array[i]->sector_start,
2225 (sector_t) spec_array[i]->length,
2226 target_params_array[i]);
2228 DMERR("error adding target to table");
2229 goto err_destroy_table;
2234 r = dm_table_complete(t);
2236 goto err_destroy_table;
2238 /* setup md->queue to reflect md's type (may block) */
2239 r = dm_setup_md_queue(md, t);
2241 DMERR("unable to set up device queue for new table.");
2242 goto err_destroy_table;
2247 old_map = dm_swap_table(md, t);
2248 if (IS_ERR(old_map)) {
2249 r = PTR_ERR(old_map);
2250 goto err_destroy_table;
2252 set_disk_ro(dm_disk(md), !!(dmi->flags & DM_READONLY_FLAG));
2257 goto err_destroy_table;
2259 DMINFO("%s (%s) is ready", md->disk->disk_name, dmi->name);
2264 dm_table_destroy(t);
2266 (void) __hash_remove(__get_name_cell(dmi->name));
2267 /* release reference from __get_name_cell */