2 * Copyright (C) 2001, 2002 Sistina Software (UK) Limited.
3 * Copyright (C) 2004 - 2006 Red Hat, Inc. All rights reserved.
5 * This file is released under the GPL.
10 #include <linux/module.h>
11 #include <linux/vmalloc.h>
12 #include <linux/miscdevice.h>
13 #include <linux/sched/mm.h>
14 #include <linux/init.h>
15 #include <linux/wait.h>
16 #include <linux/slab.h>
17 #include <linux/dm-ioctl.h>
18 #include <linux/hdreg.h>
19 #include <linux/compat.h>
20 #include <linux/nospec.h>
22 #include <linux/uaccess.h>
24 #define DM_MSG_PREFIX "ioctl"
25 #define DM_DRIVER_EMAIL "dm-devel@redhat.com"
29 * poll will wait until the global event number is greater than
32 volatile unsigned global_event_nr;
35 /*-----------------------------------------------------------------
36 * The ioctl interface needs to be able to look up devices by
38 *---------------------------------------------------------------*/
40 struct list_head name_list;
41 struct list_head uuid_list;
45 struct mapped_device *md;
46 struct dm_table *new_map;
51 struct dm_target_versions *vers, *old_vers;
57 #define NUM_BUCKETS 64
58 #define MASK_BUCKETS (NUM_BUCKETS - 1)
59 static struct list_head _name_buckets[NUM_BUCKETS];
60 static struct list_head _uuid_buckets[NUM_BUCKETS];
62 static void dm_hash_remove_all(bool keep_open_devices, bool mark_deferred, bool only_deferred);
65 * Guards access to both hash tables.
67 static DECLARE_RWSEM(_hash_lock);
70 * Protects use of mdptr to obtain hash cell name and uuid from mapped device.
72 static DEFINE_MUTEX(dm_hash_cells_mutex);
74 static void init_buckets(struct list_head *buckets)
78 for (i = 0; i < NUM_BUCKETS; i++)
79 INIT_LIST_HEAD(buckets + i);
82 static int dm_hash_init(void)
84 init_buckets(_name_buckets);
85 init_buckets(_uuid_buckets);
89 static void dm_hash_exit(void)
91 dm_hash_remove_all(false, false, false);
94 /*-----------------------------------------------------------------
96 * We're not really concerned with the str hash function being
97 * fast since it's only used by the ioctl interface.
98 *---------------------------------------------------------------*/
99 static unsigned int hash_str(const char *str)
101 const unsigned int hash_mult = 2654435387U;
105 h = (h + (unsigned int) *str++) * hash_mult;
107 return h & MASK_BUCKETS;
110 /*-----------------------------------------------------------------
111 * Code for looking up a device by name
112 *---------------------------------------------------------------*/
113 static struct hash_cell *__get_name_cell(const char *str)
115 struct hash_cell *hc;
116 unsigned int h = hash_str(str);
118 list_for_each_entry (hc, _name_buckets + h, name_list)
119 if (!strcmp(hc->name, str)) {
127 static struct hash_cell *__get_uuid_cell(const char *str)
129 struct hash_cell *hc;
130 unsigned int h = hash_str(str);
132 list_for_each_entry (hc, _uuid_buckets + h, uuid_list)
133 if (!strcmp(hc->uuid, str)) {
141 static struct hash_cell *__get_dev_cell(uint64_t dev)
143 struct mapped_device *md;
144 struct hash_cell *hc;
146 md = dm_get_md(huge_decode_dev(dev));
150 hc = dm_get_mdptr(md);
159 /*-----------------------------------------------------------------
160 * Inserting, removing and renaming a device.
161 *---------------------------------------------------------------*/
162 static struct hash_cell *alloc_cell(const char *name, const char *uuid,
163 struct mapped_device *md)
165 struct hash_cell *hc;
167 hc = kmalloc(sizeof(*hc), GFP_KERNEL);
171 hc->name = kstrdup(name, GFP_KERNEL);
181 hc->uuid = kstrdup(uuid, GFP_KERNEL);
189 INIT_LIST_HEAD(&hc->name_list);
190 INIT_LIST_HEAD(&hc->uuid_list);
196 static void free_cell(struct hash_cell *hc)
206 * The kdev_t and uuid of a device can never change once it is
207 * initially inserted.
209 static int dm_hash_insert(const char *name, const char *uuid, struct mapped_device *md)
211 struct hash_cell *cell, *hc;
214 * Allocate the new cells.
216 cell = alloc_cell(name, uuid, md);
221 * Insert the cell into both hash tables.
223 down_write(&_hash_lock);
224 hc = __get_name_cell(name);
230 list_add(&cell->name_list, _name_buckets + hash_str(name));
233 hc = __get_uuid_cell(uuid);
235 list_del(&cell->name_list);
239 list_add(&cell->uuid_list, _uuid_buckets + hash_str(uuid));
242 mutex_lock(&dm_hash_cells_mutex);
243 dm_set_mdptr(md, cell);
244 mutex_unlock(&dm_hash_cells_mutex);
245 up_write(&_hash_lock);
250 up_write(&_hash_lock);
255 static struct dm_table *__hash_remove(struct hash_cell *hc)
257 struct dm_table *table;
260 /* remove from the dev hash */
261 list_del(&hc->uuid_list);
262 list_del(&hc->name_list);
263 mutex_lock(&dm_hash_cells_mutex);
264 dm_set_mdptr(hc->md, NULL);
265 mutex_unlock(&dm_hash_cells_mutex);
267 table = dm_get_live_table(hc->md, &srcu_idx);
269 dm_table_event(table);
270 dm_put_live_table(hc->md, srcu_idx);
281 static void dm_hash_remove_all(bool keep_open_devices, bool mark_deferred, bool only_deferred)
284 struct hash_cell *hc;
285 struct mapped_device *md;
291 down_write(&_hash_lock);
293 for (i = 0; i < NUM_BUCKETS; i++) {
294 list_for_each_entry(hc, _name_buckets + i, name_list) {
298 if (keep_open_devices &&
299 dm_lock_for_deletion(md, mark_deferred, only_deferred)) {
305 t = __hash_remove(hc);
307 up_write(&_hash_lock);
314 if (likely(keep_open_devices))
317 dm_destroy_immediate(md);
320 * Some mapped devices may be using other mapped
321 * devices, so repeat until we make no further
322 * progress. If a new mapped device is created
323 * here it will also get removed.
329 up_write(&_hash_lock);
332 DMWARN("remove_all left %d open device(s)", dev_skipped);
336 * Set the uuid of a hash_cell that isn't already set.
338 static void __set_cell_uuid(struct hash_cell *hc, char *new_uuid)
340 mutex_lock(&dm_hash_cells_mutex);
342 mutex_unlock(&dm_hash_cells_mutex);
344 list_add(&hc->uuid_list, _uuid_buckets + hash_str(new_uuid));
348 * Changes the name of a hash_cell and returns the old name for
349 * the caller to free.
351 static char *__change_cell_name(struct hash_cell *hc, char *new_name)
356 * Rename and move the name cell.
358 list_del(&hc->name_list);
361 mutex_lock(&dm_hash_cells_mutex);
363 mutex_unlock(&dm_hash_cells_mutex);
365 list_add(&hc->name_list, _name_buckets + hash_str(new_name));
370 static struct mapped_device *dm_hash_rename(struct dm_ioctl *param,
373 char *new_data, *old_name = NULL;
374 struct hash_cell *hc;
375 struct dm_table *table;
376 struct mapped_device *md;
377 unsigned change_uuid = (param->flags & DM_UUID_FLAG) ? 1 : 0;
383 new_data = kstrdup(new, GFP_KERNEL);
385 return ERR_PTR(-ENOMEM);
387 down_write(&_hash_lock);
393 hc = __get_uuid_cell(new);
395 hc = __get_name_cell(new);
398 DMWARN("Unable to change %s on mapped device %s to one that "
399 "already exists: %s",
400 change_uuid ? "uuid" : "name",
403 up_write(&_hash_lock);
405 return ERR_PTR(-EBUSY);
409 * Is there such a device as 'old' ?
411 hc = __get_name_cell(param->name);
413 DMWARN("Unable to rename non-existent device, %s to %s%s",
414 param->name, change_uuid ? "uuid " : "", new);
415 up_write(&_hash_lock);
417 return ERR_PTR(-ENXIO);
421 * Does this device already have a uuid?
423 if (change_uuid && hc->uuid) {
424 DMWARN("Unable to change uuid of mapped device %s to %s "
425 "because uuid is already set to %s",
426 param->name, new, hc->uuid);
428 up_write(&_hash_lock);
430 return ERR_PTR(-EINVAL);
434 __set_cell_uuid(hc, new_data);
436 old_name = __change_cell_name(hc, new_data);
439 * Wake up any dm event waiters.
441 table = dm_get_live_table(hc->md, &srcu_idx);
443 dm_table_event(table);
444 dm_put_live_table(hc->md, srcu_idx);
446 if (!dm_kobject_uevent(hc->md, KOBJ_CHANGE, param->event_nr))
447 param->flags |= DM_UEVENT_GENERATED_FLAG;
450 up_write(&_hash_lock);
456 void dm_deferred_remove(void)
458 dm_hash_remove_all(true, false, true);
461 /*-----------------------------------------------------------------
462 * Implementation of the ioctl commands
463 *---------------------------------------------------------------*/
465 * All the ioctl commands get dispatched to functions with this
468 typedef int (*ioctl_fn)(struct file *filp, struct dm_ioctl *param, size_t param_size);
470 static int remove_all(struct file *filp, struct dm_ioctl *param, size_t param_size)
472 dm_hash_remove_all(true, !!(param->flags & DM_DEFERRED_REMOVE), false);
473 param->data_size = 0;
478 * Round up the ptr to an 8-byte boundary.
481 static inline size_t align_val(size_t val)
483 return (val + ALIGN_MASK) & ~ALIGN_MASK;
485 static inline void *align_ptr(void *ptr)
487 return (void *)align_val((size_t)ptr);
491 * Retrieves the data payload buffer from an already allocated
494 static void *get_result_buffer(struct dm_ioctl *param, size_t param_size,
497 param->data_start = align_ptr(param + 1) - (void *) param;
499 if (param->data_start < param_size)
500 *len = param_size - param->data_start;
504 return ((void *) param) + param->data_start;
507 static int list_devices(struct file *filp, struct dm_ioctl *param, size_t param_size)
510 struct hash_cell *hc;
511 size_t len, needed = 0;
512 struct gendisk *disk;
513 struct dm_name_list *orig_nl, *nl, *old_nl = NULL;
516 down_write(&_hash_lock);
519 * Loop through all the devices working out how much
522 for (i = 0; i < NUM_BUCKETS; i++) {
523 list_for_each_entry (hc, _name_buckets + i, name_list) {
524 needed += align_val(offsetof(struct dm_name_list, name) + strlen(hc->name) + 1);
525 needed += align_val(sizeof(uint32_t));
530 * Grab our output buffer.
532 nl = orig_nl = get_result_buffer(param, param_size, &len);
533 if (len < needed || len < sizeof(nl->dev)) {
534 param->flags |= DM_BUFFER_FULL_FLAG;
537 param->data_size = param->data_start + needed;
539 nl->dev = 0; /* Flags no data */
542 * Now loop through filling out the names.
544 for (i = 0; i < NUM_BUCKETS; i++) {
545 list_for_each_entry (hc, _name_buckets + i, name_list) {
547 old_nl->next = (uint32_t) ((void *) nl -
549 disk = dm_disk(hc->md);
550 nl->dev = huge_encode_dev(disk_devt(disk));
552 strcpy(nl->name, hc->name);
555 event_nr = align_ptr(nl->name + strlen(hc->name) + 1);
556 *event_nr = dm_get_event_nr(hc->md);
557 nl = align_ptr(event_nr + 1);
561 * If mismatch happens, security may be compromised due to buffer
562 * overflow, so it's better to crash.
564 BUG_ON((char *)nl - (char *)orig_nl != needed);
567 up_write(&_hash_lock);
571 static void list_version_get_needed(struct target_type *tt, void *needed_param)
573 size_t *needed = needed_param;
575 *needed += sizeof(struct dm_target_versions);
576 *needed += strlen(tt->name);
577 *needed += ALIGN_MASK;
580 static void list_version_get_info(struct target_type *tt, void *param)
582 struct vers_iter *info = param;
584 /* Check space - it might have changed since the first iteration */
585 if ((char *)info->vers + sizeof(tt->version) + strlen(tt->name) + 1 >
588 info->flags = DM_BUFFER_FULL_FLAG;
593 info->old_vers->next = (uint32_t) ((void *)info->vers -
594 (void *)info->old_vers);
595 info->vers->version[0] = tt->version[0];
596 info->vers->version[1] = tt->version[1];
597 info->vers->version[2] = tt->version[2];
598 info->vers->next = 0;
599 strcpy(info->vers->name, tt->name);
601 info->old_vers = info->vers;
602 info->vers = align_ptr(((void *) ++info->vers) + strlen(tt->name) + 1);
605 static int list_versions(struct file *filp, struct dm_ioctl *param, size_t param_size)
607 size_t len, needed = 0;
608 struct dm_target_versions *vers;
609 struct vers_iter iter_info;
612 * Loop through all the devices working out how much
615 dm_target_iterate(list_version_get_needed, &needed);
618 * Grab our output buffer.
620 vers = get_result_buffer(param, param_size, &len);
622 param->flags |= DM_BUFFER_FULL_FLAG;
625 param->data_size = param->data_start + needed;
627 iter_info.param_size = param_size;
628 iter_info.old_vers = NULL;
629 iter_info.vers = vers;
631 iter_info.end = (char *)vers+len;
634 * Now loop through filling out the names & versions.
636 dm_target_iterate(list_version_get_info, &iter_info);
637 param->flags |= iter_info.flags;
643 static int check_name(const char *name)
645 if (strchr(name, '/')) {
646 DMWARN("invalid device name");
654 * On successful return, the caller must not attempt to acquire
655 * _hash_lock without first calling dm_put_live_table, because dm_table_destroy
656 * waits for this dm_put_live_table and could be called under this lock.
658 static struct dm_table *dm_get_inactive_table(struct mapped_device *md, int *srcu_idx)
660 struct hash_cell *hc;
661 struct dm_table *table = NULL;
663 /* increment rcu count, we don't care about the table pointer */
664 dm_get_live_table(md, srcu_idx);
666 down_read(&_hash_lock);
667 hc = dm_get_mdptr(md);
668 if (!hc || hc->md != md) {
669 DMWARN("device has been removed from the dev hash table.");
676 up_read(&_hash_lock);
681 static struct dm_table *dm_get_live_or_inactive_table(struct mapped_device *md,
682 struct dm_ioctl *param,
685 return (param->flags & DM_QUERY_INACTIVE_TABLE_FLAG) ?
686 dm_get_inactive_table(md, srcu_idx) : dm_get_live_table(md, srcu_idx);
690 * Fills in a dm_ioctl structure, ready for sending back to
693 static void __dev_status(struct mapped_device *md, struct dm_ioctl *param)
695 struct gendisk *disk = dm_disk(md);
696 struct dm_table *table;
699 param->flags &= ~(DM_SUSPEND_FLAG | DM_READONLY_FLAG |
700 DM_ACTIVE_PRESENT_FLAG | DM_INTERNAL_SUSPEND_FLAG);
702 if (dm_suspended_md(md))
703 param->flags |= DM_SUSPEND_FLAG;
705 if (dm_suspended_internally_md(md))
706 param->flags |= DM_INTERNAL_SUSPEND_FLAG;
708 if (dm_test_deferred_remove_flag(md))
709 param->flags |= DM_DEFERRED_REMOVE;
711 param->dev = huge_encode_dev(disk_devt(disk));
714 * Yes, this will be out of date by the time it gets back
715 * to userland, but it is still very useful for
718 param->open_count = dm_open_count(md);
720 param->event_nr = dm_get_event_nr(md);
721 param->target_count = 0;
723 table = dm_get_live_table(md, &srcu_idx);
725 if (!(param->flags & DM_QUERY_INACTIVE_TABLE_FLAG)) {
726 if (get_disk_ro(disk))
727 param->flags |= DM_READONLY_FLAG;
728 param->target_count = dm_table_get_num_targets(table);
731 param->flags |= DM_ACTIVE_PRESENT_FLAG;
733 dm_put_live_table(md, srcu_idx);
735 if (param->flags & DM_QUERY_INACTIVE_TABLE_FLAG) {
737 table = dm_get_inactive_table(md, &srcu_idx);
739 if (!(dm_table_get_mode(table) & FMODE_WRITE))
740 param->flags |= DM_READONLY_FLAG;
741 param->target_count = dm_table_get_num_targets(table);
743 dm_put_live_table(md, srcu_idx);
747 static int dev_create(struct file *filp, struct dm_ioctl *param, size_t param_size)
749 int r, m = DM_ANY_MINOR;
750 struct mapped_device *md;
752 r = check_name(param->name);
756 if (param->flags & DM_PERSISTENT_DEV_FLAG)
757 m = MINOR(huge_decode_dev(param->dev));
759 r = dm_create(m, &md);
763 r = dm_hash_insert(param->name, *param->uuid ? param->uuid : NULL, md);
770 param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
772 __dev_status(md, param);
780 * Always use UUID for lookups if it's present, otherwise use name or dev.
782 static struct hash_cell *__find_device_hash_cell(struct dm_ioctl *param)
784 struct hash_cell *hc = NULL;
787 if (*param->name || param->dev)
790 hc = __get_uuid_cell(param->uuid);
793 } else if (*param->name) {
797 hc = __get_name_cell(param->name);
800 } else if (param->dev) {
801 hc = __get_dev_cell(param->dev);
808 * Sneakily write in both the name and the uuid
809 * while we have the cell.
811 strlcpy(param->name, hc->name, sizeof(param->name));
813 strlcpy(param->uuid, hc->uuid, sizeof(param->uuid));
815 param->uuid[0] = '\0';
818 param->flags |= DM_INACTIVE_PRESENT_FLAG;
820 param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
825 static struct mapped_device *find_device(struct dm_ioctl *param)
827 struct hash_cell *hc;
828 struct mapped_device *md = NULL;
830 down_read(&_hash_lock);
831 hc = __find_device_hash_cell(param);
834 up_read(&_hash_lock);
839 static int dev_remove(struct file *filp, struct dm_ioctl *param, size_t param_size)
841 struct hash_cell *hc;
842 struct mapped_device *md;
846 down_write(&_hash_lock);
847 hc = __find_device_hash_cell(param);
850 DMDEBUG_LIMIT("device doesn't appear to be in the dev hash table.");
851 up_write(&_hash_lock);
858 * Ensure the device is not open and nothing further can open it.
860 r = dm_lock_for_deletion(md, !!(param->flags & DM_DEFERRED_REMOVE), false);
862 if (r == -EBUSY && param->flags & DM_DEFERRED_REMOVE) {
863 up_write(&_hash_lock);
867 DMDEBUG_LIMIT("unable to remove open device %s", hc->name);
868 up_write(&_hash_lock);
873 t = __hash_remove(hc);
874 up_write(&_hash_lock);
881 param->flags &= ~DM_DEFERRED_REMOVE;
883 if (!dm_kobject_uevent(md, KOBJ_REMOVE, param->event_nr))
884 param->flags |= DM_UEVENT_GENERATED_FLAG;
892 * Check a string doesn't overrun the chunk of
893 * memory we copied from userland.
895 static int invalid_str(char *str, void *end)
897 while ((void *) str < end)
904 static int dev_rename(struct file *filp, struct dm_ioctl *param, size_t param_size)
907 char *new_data = (char *) param + param->data_start;
908 struct mapped_device *md;
909 unsigned change_uuid = (param->flags & DM_UUID_FLAG) ? 1 : 0;
911 if (new_data < param->data ||
912 invalid_str(new_data, (void *) param + param_size) || !*new_data ||
913 strlen(new_data) > (change_uuid ? DM_UUID_LEN - 1 : DM_NAME_LEN - 1)) {
914 DMWARN("Invalid new mapped device name or uuid string supplied.");
919 r = check_name(new_data);
924 md = dm_hash_rename(param, new_data);
928 __dev_status(md, param);
934 static int dev_set_geometry(struct file *filp, struct dm_ioctl *param, size_t param_size)
937 struct mapped_device *md;
938 struct hd_geometry geometry;
939 unsigned long indata[4];
940 char *geostr = (char *) param + param->data_start;
943 md = find_device(param);
947 if (geostr < param->data ||
948 invalid_str(geostr, (void *) param + param_size)) {
949 DMWARN("Invalid geometry supplied.");
953 x = sscanf(geostr, "%lu %lu %lu %lu%c", indata,
954 indata + 1, indata + 2, indata + 3, &dummy);
957 DMWARN("Unable to interpret geometry settings.");
961 if (indata[0] > 65535 || indata[1] > 255 ||
962 indata[2] > 255 || indata[3] > ULONG_MAX) {
963 DMWARN("Geometry exceeds range limits.");
967 geometry.cylinders = indata[0];
968 geometry.heads = indata[1];
969 geometry.sectors = indata[2];
970 geometry.start = indata[3];
972 r = dm_set_geometry(md, &geometry);
974 param->data_size = 0;
981 static int do_suspend(struct dm_ioctl *param)
984 unsigned suspend_flags = DM_SUSPEND_LOCKFS_FLAG;
985 struct mapped_device *md;
987 md = find_device(param);
991 if (param->flags & DM_SKIP_LOCKFS_FLAG)
992 suspend_flags &= ~DM_SUSPEND_LOCKFS_FLAG;
993 if (param->flags & DM_NOFLUSH_FLAG)
994 suspend_flags |= DM_SUSPEND_NOFLUSH_FLAG;
996 if (!dm_suspended_md(md)) {
997 r = dm_suspend(md, suspend_flags);
1002 __dev_status(md, param);
1010 static int do_resume(struct dm_ioctl *param)
1013 unsigned suspend_flags = DM_SUSPEND_LOCKFS_FLAG;
1014 struct hash_cell *hc;
1015 struct mapped_device *md;
1016 struct dm_table *new_map, *old_map = NULL;
1018 down_write(&_hash_lock);
1020 hc = __find_device_hash_cell(param);
1022 DMDEBUG_LIMIT("device doesn't appear to be in the dev hash table.");
1023 up_write(&_hash_lock);
1029 new_map = hc->new_map;
1031 param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
1033 up_write(&_hash_lock);
1035 /* Do we need to load a new map ? */
1037 /* Suspend if it isn't already suspended */
1038 if (param->flags & DM_SKIP_LOCKFS_FLAG)
1039 suspend_flags &= ~DM_SUSPEND_LOCKFS_FLAG;
1040 if (param->flags & DM_NOFLUSH_FLAG)
1041 suspend_flags |= DM_SUSPEND_NOFLUSH_FLAG;
1042 if (!dm_suspended_md(md))
1043 dm_suspend(md, suspend_flags);
1045 old_map = dm_swap_table(md, new_map);
1046 if (IS_ERR(old_map)) {
1048 dm_table_destroy(new_map);
1050 return PTR_ERR(old_map);
1053 if (dm_table_get_mode(new_map) & FMODE_WRITE)
1054 set_disk_ro(dm_disk(md), 0);
1056 set_disk_ro(dm_disk(md), 1);
1059 if (dm_suspended_md(md)) {
1061 if (!r && !dm_kobject_uevent(md, KOBJ_CHANGE, param->event_nr))
1062 param->flags |= DM_UEVENT_GENERATED_FLAG;
1066 * Since dm_swap_table synchronizes RCU, nobody should be in
1067 * read-side critical section already.
1070 dm_table_destroy(old_map);
1073 __dev_status(md, param);
1080 * Set or unset the suspension state of a device.
1081 * If the device already is in the requested state we just return its status.
1083 static int dev_suspend(struct file *filp, struct dm_ioctl *param, size_t param_size)
1085 if (param->flags & DM_SUSPEND_FLAG)
1086 return do_suspend(param);
1088 return do_resume(param);
1092 * Copies device info back to user space, used by
1093 * the create and info ioctls.
1095 static int dev_status(struct file *filp, struct dm_ioctl *param, size_t param_size)
1097 struct mapped_device *md;
1099 md = find_device(param);
1103 __dev_status(md, param);
1110 * Build up the status struct for each target
1112 static void retrieve_status(struct dm_table *table,
1113 struct dm_ioctl *param, size_t param_size)
1115 unsigned int i, num_targets;
1116 struct dm_target_spec *spec;
1117 char *outbuf, *outptr;
1119 size_t remaining, len, used = 0;
1120 unsigned status_flags = 0;
1122 outptr = outbuf = get_result_buffer(param, param_size, &len);
1124 if (param->flags & DM_STATUS_TABLE_FLAG)
1125 type = STATUSTYPE_TABLE;
1127 type = STATUSTYPE_INFO;
1129 /* Get all the target info */
1130 num_targets = dm_table_get_num_targets(table);
1131 for (i = 0; i < num_targets; i++) {
1132 struct dm_target *ti = dm_table_get_target(table, i);
1135 remaining = len - (outptr - outbuf);
1136 if (remaining <= sizeof(struct dm_target_spec)) {
1137 param->flags |= DM_BUFFER_FULL_FLAG;
1141 spec = (struct dm_target_spec *) outptr;
1144 spec->sector_start = ti->begin;
1145 spec->length = ti->len;
1146 strncpy(spec->target_type, ti->type->name,
1147 sizeof(spec->target_type));
1149 outptr += sizeof(struct dm_target_spec);
1150 remaining = len - (outptr - outbuf);
1151 if (remaining <= 0) {
1152 param->flags |= DM_BUFFER_FULL_FLAG;
1156 /* Get the status/table string from the target driver */
1157 if (ti->type->status) {
1158 if (param->flags & DM_NOFLUSH_FLAG)
1159 status_flags |= DM_STATUS_NOFLUSH_FLAG;
1160 ti->type->status(ti, type, status_flags, outptr, remaining);
1164 l = strlen(outptr) + 1;
1165 if (l == remaining) {
1166 param->flags |= DM_BUFFER_FULL_FLAG;
1171 used = param->data_start + (outptr - outbuf);
1173 outptr = align_ptr(outptr);
1174 spec->next = outptr - outbuf;
1178 param->data_size = used;
1180 param->target_count = num_targets;
1184 * Wait for a device to report an event
1186 static int dev_wait(struct file *filp, struct dm_ioctl *param, size_t param_size)
1189 struct mapped_device *md;
1190 struct dm_table *table;
1193 md = find_device(param);
1198 * Wait for a notification event
1200 if (dm_wait_event(md, param->event_nr)) {
1206 * The userland program is going to want to know what
1207 * changed to trigger the event, so we may as well tell
1208 * him and save an ioctl.
1210 __dev_status(md, param);
1212 table = dm_get_live_or_inactive_table(md, param, &srcu_idx);
1214 retrieve_status(table, param, param_size);
1215 dm_put_live_table(md, srcu_idx);
1224 * Remember the global event number and make it possible to poll
1225 * for further events.
1227 static int dev_arm_poll(struct file *filp, struct dm_ioctl *param, size_t param_size)
1229 struct dm_file *priv = filp->private_data;
1231 priv->global_event_nr = atomic_read(&dm_global_event_nr);
1236 static inline fmode_t get_mode(struct dm_ioctl *param)
1238 fmode_t mode = FMODE_READ | FMODE_WRITE;
1240 if (param->flags & DM_READONLY_FLAG)
1246 static int next_target(struct dm_target_spec *last, uint32_t next, void *end,
1247 struct dm_target_spec **spec, char **target_params)
1249 *spec = (struct dm_target_spec *) ((unsigned char *) last + next);
1250 *target_params = (char *) (*spec + 1);
1252 if (*spec < (last + 1))
1255 return invalid_str(*target_params, end);
1258 static int populate_table(struct dm_table *table,
1259 struct dm_ioctl *param, size_t param_size)
1263 struct dm_target_spec *spec = (struct dm_target_spec *) param;
1264 uint32_t next = param->data_start;
1265 void *end = (void *) param + param_size;
1266 char *target_params;
1268 if (!param->target_count) {
1269 DMWARN("populate_table: no targets specified");
1273 for (i = 0; i < param->target_count; i++) {
1275 r = next_target(spec, next, end, &spec, &target_params);
1277 DMWARN("unable to find target");
1281 r = dm_table_add_target(table, spec->target_type,
1282 (sector_t) spec->sector_start,
1283 (sector_t) spec->length,
1286 DMWARN("error adding target to table");
1293 return dm_table_complete(table);
1296 static bool is_valid_type(enum dm_queue_mode cur, enum dm_queue_mode new)
1299 (cur == DM_TYPE_BIO_BASED && new == DM_TYPE_DAX_BIO_BASED))
1305 static int table_load(struct file *filp, struct dm_ioctl *param, size_t param_size)
1308 struct hash_cell *hc;
1309 struct dm_table *t, *old_map = NULL;
1310 struct mapped_device *md;
1311 struct target_type *immutable_target_type;
1313 md = find_device(param);
1317 r = dm_table_create(&t, get_mode(param), param->target_count, md);
1321 /* Protect md->type and md->queue against concurrent table loads. */
1322 dm_lock_md_type(md);
1323 r = populate_table(t, param, param_size);
1325 goto err_unlock_md_type;
1327 immutable_target_type = dm_get_immutable_target_type(md);
1328 if (immutable_target_type &&
1329 (immutable_target_type != dm_table_get_immutable_target_type(t)) &&
1330 !dm_table_get_wildcard_target(t)) {
1331 DMWARN("can't replace immutable target type %s",
1332 immutable_target_type->name);
1334 goto err_unlock_md_type;
1337 if (dm_get_md_type(md) == DM_TYPE_NONE) {
1338 /* Initial table load: acquire type of table. */
1339 dm_set_md_type(md, dm_table_get_type(t));
1341 /* setup md->queue to reflect md's type (may block) */
1342 r = dm_setup_md_queue(md, t);
1344 DMWARN("unable to set up device queue for new table.");
1345 goto err_unlock_md_type;
1347 } else if (!is_valid_type(dm_get_md_type(md), dm_table_get_type(t))) {
1348 DMWARN("can't change device type (old=%u vs new=%u) after initial table load.",
1349 dm_get_md_type(md), dm_table_get_type(t));
1351 goto err_unlock_md_type;
1354 dm_unlock_md_type(md);
1356 /* stage inactive table */
1357 down_write(&_hash_lock);
1358 hc = dm_get_mdptr(md);
1359 if (!hc || hc->md != md) {
1360 DMWARN("device has been removed from the dev hash table.");
1361 up_write(&_hash_lock);
1363 goto err_destroy_table;
1367 old_map = hc->new_map;
1369 up_write(&_hash_lock);
1371 param->flags |= DM_INACTIVE_PRESENT_FLAG;
1372 __dev_status(md, param);
1376 dm_table_destroy(old_map);
1384 dm_unlock_md_type(md);
1386 dm_table_destroy(t);
1393 static int table_clear(struct file *filp, struct dm_ioctl *param, size_t param_size)
1395 struct hash_cell *hc;
1396 struct mapped_device *md;
1397 struct dm_table *old_map = NULL;
1399 down_write(&_hash_lock);
1401 hc = __find_device_hash_cell(param);
1403 DMDEBUG_LIMIT("device doesn't appear to be in the dev hash table.");
1404 up_write(&_hash_lock);
1409 old_map = hc->new_map;
1413 param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
1415 __dev_status(hc->md, param);
1417 up_write(&_hash_lock);
1420 dm_table_destroy(old_map);
1428 * Retrieves a list of devices used by a particular dm device.
1430 static void retrieve_deps(struct dm_table *table,
1431 struct dm_ioctl *param, size_t param_size)
1433 unsigned int count = 0;
1434 struct list_head *tmp;
1436 struct dm_dev_internal *dd;
1437 struct dm_target_deps *deps;
1439 deps = get_result_buffer(param, param_size, &len);
1442 * Count the devices.
1444 list_for_each (tmp, dm_table_get_devices(table))
1448 * Check we have enough space.
1450 needed = sizeof(*deps) + (sizeof(*deps->dev) * count);
1452 param->flags |= DM_BUFFER_FULL_FLAG;
1457 * Fill in the devices.
1459 deps->count = count;
1461 list_for_each_entry (dd, dm_table_get_devices(table), list)
1462 deps->dev[count++] = huge_encode_dev(dd->dm_dev->bdev->bd_dev);
1464 param->data_size = param->data_start + needed;
1467 static int table_deps(struct file *filp, struct dm_ioctl *param, size_t param_size)
1469 struct mapped_device *md;
1470 struct dm_table *table;
1473 md = find_device(param);
1477 __dev_status(md, param);
1479 table = dm_get_live_or_inactive_table(md, param, &srcu_idx);
1481 retrieve_deps(table, param, param_size);
1482 dm_put_live_table(md, srcu_idx);
1490 * Return the status of a device as a text string for each
1493 static int table_status(struct file *filp, struct dm_ioctl *param, size_t param_size)
1495 struct mapped_device *md;
1496 struct dm_table *table;
1499 md = find_device(param);
1503 __dev_status(md, param);
1505 table = dm_get_live_or_inactive_table(md, param, &srcu_idx);
1507 retrieve_status(table, param, param_size);
1508 dm_put_live_table(md, srcu_idx);
1516 * Process device-mapper dependent messages. Messages prefixed with '@'
1517 * are processed by the DM core. All others are delivered to the target.
1518 * Returns a number <= 1 if message was processed by device mapper.
1519 * Returns 2 if message should be delivered to the target.
1521 static int message_for_md(struct mapped_device *md, unsigned argc, char **argv,
1522 char *result, unsigned maxlen)
1527 return 2; /* no '@' prefix, deliver to target */
1529 if (!strcasecmp(argv[0], "@cancel_deferred_remove")) {
1531 DMERR("Invalid arguments for @cancel_deferred_remove");
1534 return dm_cancel_deferred_remove(md);
1537 r = dm_stats_message(md, argc, argv, result, maxlen);
1541 DMERR("Unsupported message sent to DM core: %s", argv[0]);
1546 * Pass a message to the target that's at the supplied device offset.
1548 static int target_message(struct file *filp, struct dm_ioctl *param, size_t param_size)
1552 struct mapped_device *md;
1553 struct dm_table *table;
1554 struct dm_target *ti;
1555 struct dm_target_msg *tmsg = (void *) param + param->data_start;
1557 char *result = get_result_buffer(param, param_size, &maxlen);
1560 md = find_device(param);
1564 if (tmsg < (struct dm_target_msg *) param->data ||
1565 invalid_str(tmsg->message, (void *) param + param_size)) {
1566 DMWARN("Invalid target message parameters.");
1571 r = dm_split_args(&argc, &argv, tmsg->message);
1573 DMWARN("Failed to split target message parameters");
1578 DMWARN("Empty message received.");
1583 r = message_for_md(md, argc, argv, result, maxlen);
1587 table = dm_get_live_table(md, &srcu_idx);
1591 if (dm_deleting_md(md)) {
1596 ti = dm_table_find_target(table, tmsg->sector);
1597 if (!dm_target_is_valid(ti)) {
1598 DMWARN("Target message sector outside device.");
1600 } else if (ti->type->message)
1601 r = ti->type->message(ti, argc, argv, result, maxlen);
1603 DMWARN("Target type does not support messages");
1608 dm_put_live_table(md, srcu_idx);
1613 __dev_status(md, param);
1616 param->flags |= DM_DATA_OUT_FLAG;
1617 if (dm_message_test_buffer_overflow(result, maxlen))
1618 param->flags |= DM_BUFFER_FULL_FLAG;
1620 param->data_size = param->data_start + strlen(result) + 1;
1629 * The ioctl parameter block consists of two parts, a dm_ioctl struct
1630 * followed by a data buffer. This flag is set if the second part,
1631 * which has a variable size, is not used by the function processing
1634 #define IOCTL_FLAGS_NO_PARAMS 1
1635 #define IOCTL_FLAGS_ISSUE_GLOBAL_EVENT 2
1637 /*-----------------------------------------------------------------
1638 * Implementation of open/close/ioctl on the special char
1640 *---------------------------------------------------------------*/
1641 static ioctl_fn lookup_ioctl(unsigned int cmd, int *ioctl_flags)
1643 static const struct {
1648 {DM_VERSION_CMD, 0, NULL}, /* version is dealt with elsewhere */
1649 {DM_REMOVE_ALL_CMD, IOCTL_FLAGS_NO_PARAMS | IOCTL_FLAGS_ISSUE_GLOBAL_EVENT, remove_all},
1650 {DM_LIST_DEVICES_CMD, 0, list_devices},
1652 {DM_DEV_CREATE_CMD, IOCTL_FLAGS_NO_PARAMS | IOCTL_FLAGS_ISSUE_GLOBAL_EVENT, dev_create},
1653 {DM_DEV_REMOVE_CMD, IOCTL_FLAGS_NO_PARAMS | IOCTL_FLAGS_ISSUE_GLOBAL_EVENT, dev_remove},
1654 {DM_DEV_RENAME_CMD, IOCTL_FLAGS_ISSUE_GLOBAL_EVENT, dev_rename},
1655 {DM_DEV_SUSPEND_CMD, IOCTL_FLAGS_NO_PARAMS, dev_suspend},
1656 {DM_DEV_STATUS_CMD, IOCTL_FLAGS_NO_PARAMS, dev_status},
1657 {DM_DEV_WAIT_CMD, 0, dev_wait},
1659 {DM_TABLE_LOAD_CMD, 0, table_load},
1660 {DM_TABLE_CLEAR_CMD, IOCTL_FLAGS_NO_PARAMS, table_clear},
1661 {DM_TABLE_DEPS_CMD, 0, table_deps},
1662 {DM_TABLE_STATUS_CMD, 0, table_status},
1664 {DM_LIST_VERSIONS_CMD, 0, list_versions},
1666 {DM_TARGET_MSG_CMD, 0, target_message},
1667 {DM_DEV_SET_GEOMETRY_CMD, 0, dev_set_geometry},
1668 {DM_DEV_ARM_POLL, IOCTL_FLAGS_NO_PARAMS, dev_arm_poll},
1671 if (unlikely(cmd >= ARRAY_SIZE(_ioctls)))
1674 cmd = array_index_nospec(cmd, ARRAY_SIZE(_ioctls));
1675 *ioctl_flags = _ioctls[cmd].flags;
1676 return _ioctls[cmd].fn;
1680 * As well as checking the version compatibility this always
1681 * copies the kernel interface version out.
1683 static int check_version(unsigned int cmd, struct dm_ioctl __user *user)
1685 uint32_t version[3];
1688 if (copy_from_user(version, user->version, sizeof(version)))
1691 if ((DM_VERSION_MAJOR != version[0]) ||
1692 (DM_VERSION_MINOR < version[1])) {
1693 DMWARN("ioctl interface mismatch: "
1694 "kernel(%u.%u.%u), user(%u.%u.%u), cmd(%d)",
1695 DM_VERSION_MAJOR, DM_VERSION_MINOR,
1696 DM_VERSION_PATCHLEVEL,
1697 version[0], version[1], version[2], cmd);
1702 * Fill in the kernel version.
1704 version[0] = DM_VERSION_MAJOR;
1705 version[1] = DM_VERSION_MINOR;
1706 version[2] = DM_VERSION_PATCHLEVEL;
1707 if (copy_to_user(user->version, version, sizeof(version)))
1713 #define DM_PARAMS_MALLOC 0x0001 /* Params allocated with kvmalloc() */
1714 #define DM_WIPE_BUFFER 0x0010 /* Wipe input buffer before returning from ioctl */
1716 static void free_params(struct dm_ioctl *param, size_t param_size, int param_flags)
1718 if (param_flags & DM_WIPE_BUFFER)
1719 memset(param, 0, param_size);
1721 if (param_flags & DM_PARAMS_MALLOC)
1725 static int copy_params(struct dm_ioctl __user *user, struct dm_ioctl *param_kernel,
1726 int ioctl_flags, struct dm_ioctl **param, int *param_flags)
1728 struct dm_ioctl *dmi;
1730 const size_t minimum_data_size = offsetof(struct dm_ioctl, data);
1733 if (copy_from_user(param_kernel, user, minimum_data_size))
1736 if (param_kernel->data_size < minimum_data_size)
1739 secure_data = param_kernel->flags & DM_SECURE_DATA_FLAG;
1741 *param_flags = secure_data ? DM_WIPE_BUFFER : 0;
1743 if (ioctl_flags & IOCTL_FLAGS_NO_PARAMS) {
1745 dmi->data_size = minimum_data_size;
1750 * Use __GFP_HIGH to avoid low memory issues when a device is
1751 * suspended and the ioctl is needed to resume it.
1752 * Use kmalloc() rather than vmalloc() when we can.
1755 noio_flag = memalloc_noio_save();
1756 dmi = kvmalloc(param_kernel->data_size, GFP_KERNEL | __GFP_HIGH);
1757 memalloc_noio_restore(noio_flag);
1760 if (secure_data && clear_user(user, param_kernel->data_size))
1765 *param_flags |= DM_PARAMS_MALLOC;
1767 /* Copy from param_kernel (which was already copied from user) */
1768 memcpy(dmi, param_kernel, minimum_data_size);
1770 if (copy_from_user(&dmi->data, (char __user *)user + minimum_data_size,
1771 param_kernel->data_size - minimum_data_size))
1774 /* Wipe the user buffer so we do not return it to userspace */
1775 if (secure_data && clear_user(user, param_kernel->data_size))
1782 free_params(dmi, param_kernel->data_size, *param_flags);
1787 static int validate_params(uint cmd, struct dm_ioctl *param)
1789 /* Always clear this flag */
1790 param->flags &= ~DM_BUFFER_FULL_FLAG;
1791 param->flags &= ~DM_UEVENT_GENERATED_FLAG;
1792 param->flags &= ~DM_SECURE_DATA_FLAG;
1793 param->flags &= ~DM_DATA_OUT_FLAG;
1795 /* Ignores parameters */
1796 if (cmd == DM_REMOVE_ALL_CMD ||
1797 cmd == DM_LIST_DEVICES_CMD ||
1798 cmd == DM_LIST_VERSIONS_CMD)
1801 if (cmd == DM_DEV_CREATE_CMD) {
1802 if (!*param->name) {
1803 DMWARN("name not supplied when creating device");
1806 } else if (*param->uuid && *param->name) {
1807 DMWARN("only supply one of name or uuid, cmd(%u)", cmd);
1811 /* Ensure strings are terminated */
1812 param->name[DM_NAME_LEN - 1] = '\0';
1813 param->uuid[DM_UUID_LEN - 1] = '\0';
1818 static int ctl_ioctl(struct file *file, uint command, struct dm_ioctl __user *user)
1824 struct dm_ioctl *uninitialized_var(param);
1826 size_t input_param_size;
1827 struct dm_ioctl param_kernel;
1829 /* only root can play with this */
1830 if (!capable(CAP_SYS_ADMIN))
1833 if (_IOC_TYPE(command) != DM_IOCTL)
1836 cmd = _IOC_NR(command);
1839 * Check the interface version passed in. This also
1840 * writes out the kernel's interface version.
1842 r = check_version(cmd, user);
1847 * Nothing more to do for the version command.
1849 if (cmd == DM_VERSION_CMD)
1852 fn = lookup_ioctl(cmd, &ioctl_flags);
1854 DMWARN("dm_ctl_ioctl: unknown command 0x%x", command);
1859 * Copy the parameters into kernel space.
1861 r = copy_params(user, ¶m_kernel, ioctl_flags, ¶m, ¶m_flags);
1866 input_param_size = param->data_size;
1867 r = validate_params(cmd, param);
1871 param->data_size = offsetof(struct dm_ioctl, data);
1872 r = fn(file, param, input_param_size);
1874 if (unlikely(param->flags & DM_BUFFER_FULL_FLAG) &&
1875 unlikely(ioctl_flags & IOCTL_FLAGS_NO_PARAMS))
1876 DMERR("ioctl %d tried to output some data but has IOCTL_FLAGS_NO_PARAMS set", cmd);
1878 if (!r && ioctl_flags & IOCTL_FLAGS_ISSUE_GLOBAL_EVENT)
1879 dm_issue_global_event();
1882 * Copy the results back to userland.
1884 if (!r && copy_to_user(user, param, param->data_size))
1888 free_params(param, input_param_size, param_flags);
1892 static long dm_ctl_ioctl(struct file *file, uint command, ulong u)
1894 return (long)ctl_ioctl(file, command, (struct dm_ioctl __user *)u);
1897 #ifdef CONFIG_COMPAT
1898 static long dm_compat_ctl_ioctl(struct file *file, uint command, ulong u)
1900 return (long)dm_ctl_ioctl(file, command, (ulong) compat_ptr(u));
1903 #define dm_compat_ctl_ioctl NULL
1906 static int dm_open(struct inode *inode, struct file *filp)
1909 struct dm_file *priv;
1911 r = nonseekable_open(inode, filp);
1915 priv = filp->private_data = kmalloc(sizeof(struct dm_file), GFP_KERNEL);
1919 priv->global_event_nr = atomic_read(&dm_global_event_nr);
1924 static int dm_release(struct inode *inode, struct file *filp)
1926 kfree(filp->private_data);
1930 static __poll_t dm_poll(struct file *filp, poll_table *wait)
1932 struct dm_file *priv = filp->private_data;
1935 poll_wait(filp, &dm_global_eventq, wait);
1937 if ((int)(atomic_read(&dm_global_event_nr) - priv->global_event_nr) > 0)
1943 static const struct file_operations _ctl_fops = {
1945 .release = dm_release,
1947 .unlocked_ioctl = dm_ctl_ioctl,
1948 .compat_ioctl = dm_compat_ctl_ioctl,
1949 .owner = THIS_MODULE,
1950 .llseek = noop_llseek,
1953 static struct miscdevice _dm_misc = {
1954 .minor = MAPPER_CTRL_MINOR,
1956 .nodename = DM_DIR "/" DM_CONTROL_NODE,
1960 MODULE_ALIAS_MISCDEV(MAPPER_CTRL_MINOR);
1961 MODULE_ALIAS("devname:" DM_DIR "/" DM_CONTROL_NODE);
1964 * Create misc character device and link to DM_DIR/control.
1966 int __init dm_interface_init(void)
1974 r = misc_register(&_dm_misc);
1976 DMERR("misc_register failed for control device");
1981 DMINFO("%d.%d.%d%s initialised: %s", DM_VERSION_MAJOR,
1982 DM_VERSION_MINOR, DM_VERSION_PATCHLEVEL, DM_VERSION_EXTRA,
1987 void dm_interface_exit(void)
1989 misc_deregister(&_dm_misc);
1994 * dm_copy_name_and_uuid - Copy mapped device name & uuid into supplied buffers
1995 * @md: Pointer to mapped_device
1996 * @name: Buffer (size DM_NAME_LEN) for name
1997 * @uuid: Buffer (size DM_UUID_LEN) for uuid or empty string if uuid not defined
1999 int dm_copy_name_and_uuid(struct mapped_device *md, char *name, char *uuid)
2002 struct hash_cell *hc;
2007 mutex_lock(&dm_hash_cells_mutex);
2008 hc = dm_get_mdptr(md);
2009 if (!hc || hc->md != md) {
2015 strcpy(name, hc->name);
2017 strcpy(uuid, hc->uuid ? : "");
2020 mutex_unlock(&dm_hash_cells_mutex);