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/init.h>
14 #include <linux/wait.h>
15 #include <linux/slab.h>
16 #include <linux/devfs_fs_kernel.h>
17 #include <linux/dm-ioctl.h>
18 #include <linux/hdreg.h>
20 #include <asm/uaccess.h>
22 #define DM_MSG_PREFIX "ioctl"
23 #define DM_DRIVER_EMAIL "dm-devel@redhat.com"
25 /*-----------------------------------------------------------------
26 * The ioctl interface needs to be able to look up devices by
28 *---------------------------------------------------------------*/
30 struct list_head name_list;
31 struct list_head uuid_list;
35 struct mapped_device *md;
36 struct dm_table *new_map;
41 struct dm_target_versions *vers, *old_vers;
47 #define NUM_BUCKETS 64
48 #define MASK_BUCKETS (NUM_BUCKETS - 1)
49 static struct list_head _name_buckets[NUM_BUCKETS];
50 static struct list_head _uuid_buckets[NUM_BUCKETS];
52 static void dm_hash_remove_all(int keep_open_devices);
55 * Guards access to both hash tables.
57 static DECLARE_RWSEM(_hash_lock);
59 static void init_buckets(struct list_head *buckets)
63 for (i = 0; i < NUM_BUCKETS; i++)
64 INIT_LIST_HEAD(buckets + i);
67 static int dm_hash_init(void)
69 init_buckets(_name_buckets);
70 init_buckets(_uuid_buckets);
74 static void dm_hash_exit(void)
76 dm_hash_remove_all(0);
80 /*-----------------------------------------------------------------
82 * We're not really concerned with the str hash function being
83 * fast since it's only used by the ioctl interface.
84 *---------------------------------------------------------------*/
85 static unsigned int hash_str(const char *str)
87 const unsigned int hash_mult = 2654435387U;
91 h = (h + (unsigned int) *str++) * hash_mult;
93 return h & MASK_BUCKETS;
96 /*-----------------------------------------------------------------
97 * Code for looking up a device by name
98 *---------------------------------------------------------------*/
99 static struct hash_cell *__get_name_cell(const char *str)
101 struct hash_cell *hc;
102 unsigned int h = hash_str(str);
104 list_for_each_entry (hc, _name_buckets + h, name_list)
105 if (!strcmp(hc->name, str)) {
113 static struct hash_cell *__get_uuid_cell(const char *str)
115 struct hash_cell *hc;
116 unsigned int h = hash_str(str);
118 list_for_each_entry (hc, _uuid_buckets + h, uuid_list)
119 if (!strcmp(hc->uuid, str)) {
127 /*-----------------------------------------------------------------
128 * Inserting, removing and renaming a device.
129 *---------------------------------------------------------------*/
130 static struct hash_cell *alloc_cell(const char *name, const char *uuid,
131 struct mapped_device *md)
133 struct hash_cell *hc;
135 hc = kmalloc(sizeof(*hc), GFP_KERNEL);
139 hc->name = kstrdup(name, GFP_KERNEL);
149 hc->uuid = kstrdup(uuid, GFP_KERNEL);
157 INIT_LIST_HEAD(&hc->name_list);
158 INIT_LIST_HEAD(&hc->uuid_list);
164 static void free_cell(struct hash_cell *hc)
176 static int unregister_with_devfs(struct hash_cell *hc)
178 devfs_remove(DM_DIR"/%s", hc->name);
183 * The kdev_t and uuid of a device can never change once it is
184 * initially inserted.
186 static int dm_hash_insert(const char *name, const char *uuid, struct mapped_device *md)
188 struct hash_cell *cell, *hc;
191 * Allocate the new cells.
193 cell = alloc_cell(name, uuid, md);
198 * Insert the cell into both hash tables.
200 down_write(&_hash_lock);
201 hc = __get_name_cell(name);
207 list_add(&cell->name_list, _name_buckets + hash_str(name));
210 hc = __get_uuid_cell(uuid);
212 list_del(&cell->name_list);
216 list_add(&cell->uuid_list, _uuid_buckets + hash_str(uuid));
219 dm_set_mdptr(md, cell);
220 up_write(&_hash_lock);
225 up_write(&_hash_lock);
230 static void __hash_remove(struct hash_cell *hc)
232 struct dm_table *table;
234 /* remove from the dev hash */
235 list_del(&hc->uuid_list);
236 list_del(&hc->name_list);
237 unregister_with_devfs(hc);
238 dm_set_mdptr(hc->md, NULL);
240 table = dm_get_table(hc->md);
242 dm_table_event(table);
247 dm_table_put(hc->new_map);
252 static void dm_hash_remove_all(int keep_open_devices)
254 int i, dev_skipped, dev_removed;
255 struct hash_cell *hc;
256 struct list_head *tmp, *n;
258 down_write(&_hash_lock);
261 dev_skipped = dev_removed = 0;
262 for (i = 0; i < NUM_BUCKETS; i++) {
263 list_for_each_safe (tmp, n, _name_buckets + i) {
264 hc = list_entry(tmp, struct hash_cell, name_list);
266 if (keep_open_devices &&
267 dm_lock_for_deletion(hc->md)) {
277 * Some mapped devices may be using other mapped devices, so if any
278 * still exist, repeat until we make no further progress.
284 DMWARN("remove_all left %d open device(s)", dev_skipped);
287 up_write(&_hash_lock);
290 static int dm_hash_rename(const char *old, const char *new)
292 char *new_name, *old_name;
293 struct hash_cell *hc;
294 struct dm_table *table;
299 new_name = kstrdup(new, GFP_KERNEL);
303 down_write(&_hash_lock);
308 hc = __get_name_cell(new);
310 DMWARN("asked to rename to an already existing name %s -> %s",
313 up_write(&_hash_lock);
319 * Is there such a device as 'old' ?
321 hc = __get_name_cell(old);
323 DMWARN("asked to rename a non existent device %s -> %s",
325 up_write(&_hash_lock);
331 * rename and move the name cell.
333 unregister_with_devfs(hc);
335 list_del(&hc->name_list);
338 list_add(&hc->name_list, _name_buckets + hash_str(new_name));
341 * Wake up any dm event waiters.
343 table = dm_get_table(hc->md);
345 dm_table_event(table);
350 up_write(&_hash_lock);
355 /*-----------------------------------------------------------------
356 * Implementation of the ioctl commands
357 *---------------------------------------------------------------*/
359 * All the ioctl commands get dispatched to functions with this
362 typedef int (*ioctl_fn)(struct dm_ioctl *param, size_t param_size);
364 static int remove_all(struct dm_ioctl *param, size_t param_size)
366 dm_hash_remove_all(1);
367 param->data_size = 0;
372 * Round up the ptr to an 8-byte boundary.
375 static inline void *align_ptr(void *ptr)
377 return (void *) (((size_t) (ptr + ALIGN_MASK)) & ~ALIGN_MASK);
381 * Retrieves the data payload buffer from an already allocated
384 static void *get_result_buffer(struct dm_ioctl *param, size_t param_size,
387 param->data_start = align_ptr(param + 1) - (void *) param;
389 if (param->data_start < param_size)
390 *len = param_size - param->data_start;
394 return ((void *) param) + param->data_start;
397 static int list_devices(struct dm_ioctl *param, size_t param_size)
400 struct hash_cell *hc;
401 size_t len, needed = 0;
402 struct gendisk *disk;
403 struct dm_name_list *nl, *old_nl = NULL;
405 down_write(&_hash_lock);
408 * Loop through all the devices working out how much
411 for (i = 0; i < NUM_BUCKETS; i++) {
412 list_for_each_entry (hc, _name_buckets + i, name_list) {
413 needed += sizeof(struct dm_name_list);
414 needed += strlen(hc->name) + 1;
415 needed += ALIGN_MASK;
420 * Grab our output buffer.
422 nl = get_result_buffer(param, param_size, &len);
424 param->flags |= DM_BUFFER_FULL_FLAG;
427 param->data_size = param->data_start + needed;
429 nl->dev = 0; /* Flags no data */
432 * Now loop through filling out the names.
434 for (i = 0; i < NUM_BUCKETS; i++) {
435 list_for_each_entry (hc, _name_buckets + i, name_list) {
437 old_nl->next = (uint32_t) ((void *) nl -
439 disk = dm_disk(hc->md);
440 nl->dev = huge_encode_dev(MKDEV(disk->major, disk->first_minor));
442 strcpy(nl->name, hc->name);
445 nl = align_ptr(((void *) ++nl) + strlen(hc->name) + 1);
450 up_write(&_hash_lock);
454 static void list_version_get_needed(struct target_type *tt, void *needed_param)
456 size_t *needed = needed_param;
458 *needed += sizeof(struct dm_target_versions);
459 *needed += strlen(tt->name);
460 *needed += ALIGN_MASK;
463 static void list_version_get_info(struct target_type *tt, void *param)
465 struct vers_iter *info = param;
467 /* Check space - it might have changed since the first iteration */
468 if ((char *)info->vers + sizeof(tt->version) + strlen(tt->name) + 1 >
471 info->flags = DM_BUFFER_FULL_FLAG;
476 info->old_vers->next = (uint32_t) ((void *)info->vers -
477 (void *)info->old_vers);
478 info->vers->version[0] = tt->version[0];
479 info->vers->version[1] = tt->version[1];
480 info->vers->version[2] = tt->version[2];
481 info->vers->next = 0;
482 strcpy(info->vers->name, tt->name);
484 info->old_vers = info->vers;
485 info->vers = align_ptr(((void *) ++info->vers) + strlen(tt->name) + 1);
488 static int list_versions(struct dm_ioctl *param, size_t param_size)
490 size_t len, needed = 0;
491 struct dm_target_versions *vers;
492 struct vers_iter iter_info;
495 * Loop through all the devices working out how much
498 dm_target_iterate(list_version_get_needed, &needed);
501 * Grab our output buffer.
503 vers = get_result_buffer(param, param_size, &len);
505 param->flags |= DM_BUFFER_FULL_FLAG;
508 param->data_size = param->data_start + needed;
510 iter_info.param_size = param_size;
511 iter_info.old_vers = NULL;
512 iter_info.vers = vers;
514 iter_info.end = (char *)vers+len;
517 * Now loop through filling out the names & versions.
519 dm_target_iterate(list_version_get_info, &iter_info);
520 param->flags |= iter_info.flags;
528 static int check_name(const char *name)
530 if (strchr(name, '/')) {
531 DMWARN("invalid device name");
539 * Fills in a dm_ioctl structure, ready for sending back to
542 static int __dev_status(struct mapped_device *md, struct dm_ioctl *param)
544 struct gendisk *disk = dm_disk(md);
545 struct dm_table *table;
547 param->flags &= ~(DM_SUSPEND_FLAG | DM_READONLY_FLAG |
548 DM_ACTIVE_PRESENT_FLAG);
550 if (dm_suspended(md))
551 param->flags |= DM_SUSPEND_FLAG;
553 param->dev = huge_encode_dev(MKDEV(disk->major, disk->first_minor));
556 * Yes, this will be out of date by the time it gets back
557 * to userland, but it is still very useful for
560 param->open_count = dm_open_count(md);
563 param->flags |= DM_READONLY_FLAG;
565 param->event_nr = dm_get_event_nr(md);
567 table = dm_get_table(md);
569 param->flags |= DM_ACTIVE_PRESENT_FLAG;
570 param->target_count = dm_table_get_num_targets(table);
573 param->target_count = 0;
578 static int dev_create(struct dm_ioctl *param, size_t param_size)
580 int r, m = DM_ANY_MINOR;
581 struct mapped_device *md;
583 r = check_name(param->name);
587 if (param->flags & DM_PERSISTENT_DEV_FLAG)
588 m = MINOR(huge_decode_dev(param->dev));
590 r = dm_create(m, &md);
594 r = dm_hash_insert(param->name, *param->uuid ? param->uuid : NULL, md);
600 param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
602 r = __dev_status(md, param);
609 * Always use UUID for lookups if it's present, otherwise use name or dev.
611 static struct hash_cell *__find_device_hash_cell(struct dm_ioctl *param)
613 struct mapped_device *md;
617 return __get_uuid_cell(param->uuid);
620 return __get_name_cell(param->name);
622 md = dm_get_md(huge_decode_dev(param->dev));
624 mdptr = dm_get_mdptr(md);
629 static struct mapped_device *find_device(struct dm_ioctl *param)
631 struct hash_cell *hc;
632 struct mapped_device *md = NULL;
634 down_read(&_hash_lock);
635 hc = __find_device_hash_cell(param);
640 * Sneakily write in both the name and the uuid
641 * while we have the cell.
643 strncpy(param->name, hc->name, sizeof(param->name));
645 strncpy(param->uuid, hc->uuid, sizeof(param->uuid)-1);
647 param->uuid[0] = '\0';
650 param->flags |= DM_INACTIVE_PRESENT_FLAG;
652 param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
654 up_read(&_hash_lock);
659 static int dev_remove(struct dm_ioctl *param, size_t param_size)
661 struct hash_cell *hc;
662 struct mapped_device *md;
665 down_write(&_hash_lock);
666 hc = __find_device_hash_cell(param);
669 DMWARN("device doesn't appear to be in the dev hash table.");
670 up_write(&_hash_lock);
677 * Ensure the device is not open and nothing further can open it.
679 r = dm_lock_for_deletion(md);
681 DMWARN("unable to remove open device %s", hc->name);
682 up_write(&_hash_lock);
688 up_write(&_hash_lock);
690 param->data_size = 0;
695 * Check a string doesn't overrun the chunk of
696 * memory we copied from userland.
698 static int invalid_str(char *str, void *end)
700 while ((void *) str < end)
707 static int dev_rename(struct dm_ioctl *param, size_t param_size)
710 char *new_name = (char *) param + param->data_start;
712 if (new_name < (char *) (param + 1) ||
713 invalid_str(new_name, (void *) param + param_size)) {
714 DMWARN("Invalid new logical volume name supplied.");
718 r = check_name(new_name);
722 param->data_size = 0;
723 return dm_hash_rename(param->name, new_name);
726 static int dev_set_geometry(struct dm_ioctl *param, size_t param_size)
729 struct mapped_device *md;
730 struct hd_geometry geometry;
731 unsigned long indata[4];
732 char *geostr = (char *) param + param->data_start;
734 md = find_device(param);
738 if (geostr < (char *) (param + 1) ||
739 invalid_str(geostr, (void *) param + param_size)) {
740 DMWARN("Invalid geometry supplied.");
744 x = sscanf(geostr, "%lu %lu %lu %lu", indata,
745 indata + 1, indata + 2, indata + 3);
748 DMWARN("Unable to interpret geometry settings.");
752 if (indata[0] > 65535 || indata[1] > 255 ||
753 indata[2] > 255 || indata[3] > ULONG_MAX) {
754 DMWARN("Geometry exceeds range limits.");
758 geometry.cylinders = indata[0];
759 geometry.heads = indata[1];
760 geometry.sectors = indata[2];
761 geometry.start = indata[3];
763 r = dm_set_geometry(md, &geometry);
765 r = __dev_status(md, param);
767 param->data_size = 0;
774 static int do_suspend(struct dm_ioctl *param)
778 struct mapped_device *md;
780 md = find_device(param);
784 if (param->flags & DM_SKIP_LOCKFS_FLAG)
787 if (!dm_suspended(md))
788 r = dm_suspend(md, do_lockfs);
791 r = __dev_status(md, param);
797 static int do_resume(struct dm_ioctl *param)
801 struct hash_cell *hc;
802 struct mapped_device *md;
803 struct dm_table *new_map;
805 down_write(&_hash_lock);
807 hc = __find_device_hash_cell(param);
809 DMWARN("device doesn't appear to be in the dev hash table.");
810 up_write(&_hash_lock);
816 new_map = hc->new_map;
818 param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
820 up_write(&_hash_lock);
822 /* Do we need to load a new map ? */
824 /* Suspend if it isn't already suspended */
825 if (param->flags & DM_SKIP_LOCKFS_FLAG)
827 if (!dm_suspended(md))
828 dm_suspend(md, do_lockfs);
830 r = dm_swap_table(md, new_map);
833 dm_table_put(new_map);
837 if (dm_table_get_mode(new_map) & FMODE_WRITE)
838 set_disk_ro(dm_disk(md), 0);
840 set_disk_ro(dm_disk(md), 1);
842 dm_table_put(new_map);
845 if (dm_suspended(md))
849 r = __dev_status(md, param);
856 * Set or unset the suspension state of a device.
857 * If the device already is in the requested state we just return its status.
859 static int dev_suspend(struct dm_ioctl *param, size_t param_size)
861 if (param->flags & DM_SUSPEND_FLAG)
862 return do_suspend(param);
864 return do_resume(param);
868 * Copies device info back to user space, used by
869 * the create and info ioctls.
871 static int dev_status(struct dm_ioctl *param, size_t param_size)
874 struct mapped_device *md;
876 md = find_device(param);
880 r = __dev_status(md, param);
886 * Build up the status struct for each target
888 static void retrieve_status(struct dm_table *table,
889 struct dm_ioctl *param, size_t param_size)
891 unsigned int i, num_targets;
892 struct dm_target_spec *spec;
893 char *outbuf, *outptr;
895 size_t remaining, len, used = 0;
897 outptr = outbuf = get_result_buffer(param, param_size, &len);
899 if (param->flags & DM_STATUS_TABLE_FLAG)
900 type = STATUSTYPE_TABLE;
902 type = STATUSTYPE_INFO;
904 /* Get all the target info */
905 num_targets = dm_table_get_num_targets(table);
906 for (i = 0; i < num_targets; i++) {
907 struct dm_target *ti = dm_table_get_target(table, i);
909 remaining = len - (outptr - outbuf);
910 if (remaining <= sizeof(struct dm_target_spec)) {
911 param->flags |= DM_BUFFER_FULL_FLAG;
915 spec = (struct dm_target_spec *) outptr;
918 spec->sector_start = ti->begin;
919 spec->length = ti->len;
920 strncpy(spec->target_type, ti->type->name,
921 sizeof(spec->target_type));
923 outptr += sizeof(struct dm_target_spec);
924 remaining = len - (outptr - outbuf);
925 if (remaining <= 0) {
926 param->flags |= DM_BUFFER_FULL_FLAG;
930 /* Get the status/table string from the target driver */
931 if (ti->type->status) {
932 if (ti->type->status(ti, type, outptr, remaining)) {
933 param->flags |= DM_BUFFER_FULL_FLAG;
939 outptr += strlen(outptr) + 1;
940 used = param->data_start + (outptr - outbuf);
942 outptr = align_ptr(outptr);
943 spec->next = outptr - outbuf;
947 param->data_size = used;
949 param->target_count = num_targets;
953 * Wait for a device to report an event
955 static int dev_wait(struct dm_ioctl *param, size_t param_size)
958 struct mapped_device *md;
959 struct dm_table *table;
961 md = find_device(param);
966 * Wait for a notification event
968 if (dm_wait_event(md, param->event_nr)) {
974 * The userland program is going to want to know what
975 * changed to trigger the event, so we may as well tell
976 * him and save an ioctl.
978 r = __dev_status(md, param);
982 table = dm_get_table(md);
984 retrieve_status(table, param, param_size);
993 static inline int get_mode(struct dm_ioctl *param)
995 int mode = FMODE_READ | FMODE_WRITE;
997 if (param->flags & DM_READONLY_FLAG)
1003 static int next_target(struct dm_target_spec *last, uint32_t next, void *end,
1004 struct dm_target_spec **spec, char **target_params)
1006 *spec = (struct dm_target_spec *) ((unsigned char *) last + next);
1007 *target_params = (char *) (*spec + 1);
1009 if (*spec < (last + 1))
1012 return invalid_str(*target_params, end);
1015 static int populate_table(struct dm_table *table,
1016 struct dm_ioctl *param, size_t param_size)
1020 struct dm_target_spec *spec = (struct dm_target_spec *) param;
1021 uint32_t next = param->data_start;
1022 void *end = (void *) param + param_size;
1023 char *target_params;
1025 if (!param->target_count) {
1026 DMWARN("populate_table: no targets specified");
1030 for (i = 0; i < param->target_count; i++) {
1032 r = next_target(spec, next, end, &spec, &target_params);
1034 DMWARN("unable to find target");
1038 r = dm_table_add_target(table, spec->target_type,
1039 (sector_t) spec->sector_start,
1040 (sector_t) spec->length,
1043 DMWARN("error adding target to table");
1050 return dm_table_complete(table);
1053 static int table_load(struct dm_ioctl *param, size_t param_size)
1056 struct hash_cell *hc;
1058 struct mapped_device *md;
1060 md = find_device(param);
1064 r = dm_table_create(&t, get_mode(param), param->target_count, md);
1068 r = populate_table(t, param, param_size);
1074 down_write(&_hash_lock);
1075 hc = dm_get_mdptr(md);
1076 if (!hc || hc->md != md) {
1077 DMWARN("device has been removed from the dev hash table.");
1079 up_write(&_hash_lock);
1085 dm_table_put(hc->new_map);
1087 up_write(&_hash_lock);
1089 param->flags |= DM_INACTIVE_PRESENT_FLAG;
1090 r = __dev_status(md, param);
1098 static int table_clear(struct dm_ioctl *param, size_t param_size)
1101 struct hash_cell *hc;
1102 struct mapped_device *md;
1104 down_write(&_hash_lock);
1106 hc = __find_device_hash_cell(param);
1108 DMWARN("device doesn't appear to be in the dev hash table.");
1109 up_write(&_hash_lock);
1114 dm_table_put(hc->new_map);
1118 param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
1120 r = __dev_status(hc->md, param);
1122 up_write(&_hash_lock);
1128 * Retrieves a list of devices used by a particular dm device.
1130 static void retrieve_deps(struct dm_table *table,
1131 struct dm_ioctl *param, size_t param_size)
1133 unsigned int count = 0;
1134 struct list_head *tmp;
1137 struct dm_target_deps *deps;
1139 deps = get_result_buffer(param, param_size, &len);
1142 * Count the devices.
1144 list_for_each (tmp, dm_table_get_devices(table))
1148 * Check we have enough space.
1150 needed = sizeof(*deps) + (sizeof(*deps->dev) * count);
1152 param->flags |= DM_BUFFER_FULL_FLAG;
1157 * Fill in the devices.
1159 deps->count = count;
1161 list_for_each_entry (dd, dm_table_get_devices(table), list)
1162 deps->dev[count++] = huge_encode_dev(dd->bdev->bd_dev);
1164 param->data_size = param->data_start + needed;
1167 static int table_deps(struct dm_ioctl *param, size_t param_size)
1170 struct mapped_device *md;
1171 struct dm_table *table;
1173 md = find_device(param);
1177 r = __dev_status(md, param);
1181 table = dm_get_table(md);
1183 retrieve_deps(table, param, param_size);
1184 dm_table_put(table);
1193 * Return the status of a device as a text string for each
1196 static int table_status(struct dm_ioctl *param, size_t param_size)
1199 struct mapped_device *md;
1200 struct dm_table *table;
1202 md = find_device(param);
1206 r = __dev_status(md, param);
1210 table = dm_get_table(md);
1212 retrieve_status(table, param, param_size);
1213 dm_table_put(table);
1222 * Pass a message to the target that's at the supplied device offset.
1224 static int target_message(struct dm_ioctl *param, size_t param_size)
1228 struct mapped_device *md;
1229 struct dm_table *table;
1230 struct dm_target *ti;
1231 struct dm_target_msg *tmsg = (void *) param + param->data_start;
1233 md = find_device(param);
1237 r = __dev_status(md, param);
1241 if (tmsg < (struct dm_target_msg *) (param + 1) ||
1242 invalid_str(tmsg->message, (void *) param + param_size)) {
1243 DMWARN("Invalid target message parameters.");
1248 r = dm_split_args(&argc, &argv, tmsg->message);
1250 DMWARN("Failed to split target message parameters");
1254 table = dm_get_table(md);
1258 if (tmsg->sector >= dm_table_get_size(table)) {
1259 DMWARN("Target message sector outside device.");
1264 ti = dm_table_find_target(table, tmsg->sector);
1265 if (ti->type->message)
1266 r = ti->type->message(ti, argc, argv);
1268 DMWARN("Target type does not support messages");
1273 dm_table_put(table);
1277 param->data_size = 0;
1282 /*-----------------------------------------------------------------
1283 * Implementation of open/close/ioctl on the special char
1285 *---------------------------------------------------------------*/
1286 static ioctl_fn lookup_ioctl(unsigned int cmd)
1292 {DM_VERSION_CMD, NULL}, /* version is dealt with elsewhere */
1293 {DM_REMOVE_ALL_CMD, remove_all},
1294 {DM_LIST_DEVICES_CMD, list_devices},
1296 {DM_DEV_CREATE_CMD, dev_create},
1297 {DM_DEV_REMOVE_CMD, dev_remove},
1298 {DM_DEV_RENAME_CMD, dev_rename},
1299 {DM_DEV_SUSPEND_CMD, dev_suspend},
1300 {DM_DEV_STATUS_CMD, dev_status},
1301 {DM_DEV_WAIT_CMD, dev_wait},
1303 {DM_TABLE_LOAD_CMD, table_load},
1304 {DM_TABLE_CLEAR_CMD, table_clear},
1305 {DM_TABLE_DEPS_CMD, table_deps},
1306 {DM_TABLE_STATUS_CMD, table_status},
1308 {DM_LIST_VERSIONS_CMD, list_versions},
1310 {DM_TARGET_MSG_CMD, target_message},
1311 {DM_DEV_SET_GEOMETRY_CMD, dev_set_geometry}
1314 return (cmd >= ARRAY_SIZE(_ioctls)) ? NULL : _ioctls[cmd].fn;
1318 * As well as checking the version compatibility this always
1319 * copies the kernel interface version out.
1321 static int check_version(unsigned int cmd, struct dm_ioctl __user *user)
1323 uint32_t version[3];
1326 if (copy_from_user(version, user->version, sizeof(version)))
1329 if ((DM_VERSION_MAJOR != version[0]) ||
1330 (DM_VERSION_MINOR < version[1])) {
1331 DMWARN("ioctl interface mismatch: "
1332 "kernel(%u.%u.%u), user(%u.%u.%u), cmd(%d)",
1333 DM_VERSION_MAJOR, DM_VERSION_MINOR,
1334 DM_VERSION_PATCHLEVEL,
1335 version[0], version[1], version[2], cmd);
1340 * Fill in the kernel version.
1342 version[0] = DM_VERSION_MAJOR;
1343 version[1] = DM_VERSION_MINOR;
1344 version[2] = DM_VERSION_PATCHLEVEL;
1345 if (copy_to_user(user->version, version, sizeof(version)))
1351 static void free_params(struct dm_ioctl *param)
1356 static int copy_params(struct dm_ioctl __user *user, struct dm_ioctl **param)
1358 struct dm_ioctl tmp, *dmi;
1360 if (copy_from_user(&tmp, user, sizeof(tmp)))
1363 if (tmp.data_size < sizeof(tmp))
1366 dmi = (struct dm_ioctl *) vmalloc(tmp.data_size);
1370 if (copy_from_user(dmi, user, tmp.data_size)) {
1379 static int validate_params(uint cmd, struct dm_ioctl *param)
1381 /* Always clear this flag */
1382 param->flags &= ~DM_BUFFER_FULL_FLAG;
1384 /* Ignores parameters */
1385 if (cmd == DM_REMOVE_ALL_CMD ||
1386 cmd == DM_LIST_DEVICES_CMD ||
1387 cmd == DM_LIST_VERSIONS_CMD)
1390 if ((cmd == DM_DEV_CREATE_CMD)) {
1391 if (!*param->name) {
1392 DMWARN("name not supplied when creating device");
1395 } else if ((*param->uuid && *param->name)) {
1396 DMWARN("only supply one of name or uuid, cmd(%u)", cmd);
1400 /* Ensure strings are terminated */
1401 param->name[DM_NAME_LEN - 1] = '\0';
1402 param->uuid[DM_UUID_LEN - 1] = '\0';
1407 static int ctl_ioctl(struct inode *inode, struct file *file,
1408 uint command, ulong u)
1412 struct dm_ioctl *param;
1413 struct dm_ioctl __user *user = (struct dm_ioctl __user *) u;
1417 /* only root can play with this */
1418 if (!capable(CAP_SYS_ADMIN))
1421 if (_IOC_TYPE(command) != DM_IOCTL)
1424 cmd = _IOC_NR(command);
1427 * Check the interface version passed in. This also
1428 * writes out the kernel's interface version.
1430 r = check_version(cmd, user);
1435 * Nothing more to do for the version command.
1437 if (cmd == DM_VERSION_CMD)
1440 fn = lookup_ioctl(cmd);
1442 DMWARN("dm_ctl_ioctl: unknown command 0x%x", command);
1447 * Trying to avoid low memory issues when a device is
1450 current->flags |= PF_MEMALLOC;
1453 * Copy the parameters into kernel space.
1455 r = copy_params(user, ¶m);
1457 current->flags &= ~PF_MEMALLOC;
1462 r = validate_params(cmd, param);
1466 param_size = param->data_size;
1467 param->data_size = sizeof(*param);
1468 r = fn(param, param_size);
1471 * Copy the results back to userland.
1473 if (!r && copy_to_user(user, param, param->data_size))
1481 static struct file_operations _ctl_fops = {
1483 .owner = THIS_MODULE,
1486 static struct miscdevice _dm_misc = {
1487 .minor = MISC_DYNAMIC_MINOR,
1489 .devfs_name = "mapper/control",
1494 * Create misc character device and link to DM_DIR/control.
1496 int __init dm_interface_init(void)
1504 r = misc_register(&_dm_misc);
1506 DMERR("misc_register failed for control device");
1511 DMINFO("%d.%d.%d%s initialised: %s", DM_VERSION_MAJOR,
1512 DM_VERSION_MINOR, DM_VERSION_PATCHLEVEL, DM_VERSION_EXTRA,
1517 void dm_interface_exit(void)
1519 if (misc_deregister(&_dm_misc) < 0)
1520 DMERR("misc_deregister failed for control device");