2 * Copyright (C) 2001, 2002 Sistina Software (UK) Limited.
3 * Copyright (C) 2004 - 2005 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>
19 #include <asm/uaccess.h>
21 #define DM_DRIVER_EMAIL "dm-devel@redhat.com"
23 /*-----------------------------------------------------------------
24 * The ioctl interface needs to be able to look up devices by
26 *---------------------------------------------------------------*/
28 struct list_head name_list;
29 struct list_head uuid_list;
33 struct mapped_device *md;
34 struct dm_table *new_map;
39 struct dm_target_versions *vers, *old_vers;
45 #define NUM_BUCKETS 64
46 #define MASK_BUCKETS (NUM_BUCKETS - 1)
47 static struct list_head _name_buckets[NUM_BUCKETS];
48 static struct list_head _uuid_buckets[NUM_BUCKETS];
50 static void dm_hash_remove_all(void);
53 * Guards access to both hash tables.
55 static DECLARE_RWSEM(_hash_lock);
57 static void init_buckets(struct list_head *buckets)
61 for (i = 0; i < NUM_BUCKETS; i++)
62 INIT_LIST_HEAD(buckets + i);
65 static int dm_hash_init(void)
67 init_buckets(_name_buckets);
68 init_buckets(_uuid_buckets);
73 static void dm_hash_exit(void)
79 /*-----------------------------------------------------------------
81 * We're not really concerned with the str hash function being
82 * fast since it's only used by the ioctl interface.
83 *---------------------------------------------------------------*/
84 static unsigned int hash_str(const char *str)
86 const unsigned int hash_mult = 2654435387U;
90 h = (h + (unsigned int) *str++) * hash_mult;
92 return h & MASK_BUCKETS;
95 /*-----------------------------------------------------------------
96 * Code for looking up a device by name
97 *---------------------------------------------------------------*/
98 static struct hash_cell *__get_name_cell(const char *str)
100 struct hash_cell *hc;
101 unsigned int h = hash_str(str);
103 list_for_each_entry (hc, _name_buckets + h, name_list)
104 if (!strcmp(hc->name, str))
110 static struct hash_cell *__get_uuid_cell(const char *str)
112 struct hash_cell *hc;
113 unsigned int h = hash_str(str);
115 list_for_each_entry (hc, _uuid_buckets + h, uuid_list)
116 if (!strcmp(hc->uuid, str))
122 /*-----------------------------------------------------------------
123 * Inserting, removing and renaming a device.
124 *---------------------------------------------------------------*/
125 static struct hash_cell *alloc_cell(const char *name, const char *uuid,
126 struct mapped_device *md)
128 struct hash_cell *hc;
130 hc = kmalloc(sizeof(*hc), GFP_KERNEL);
134 hc->name = kstrdup(name, GFP_KERNEL);
144 hc->uuid = kstrdup(uuid, GFP_KERNEL);
152 INIT_LIST_HEAD(&hc->name_list);
153 INIT_LIST_HEAD(&hc->uuid_list);
159 static void free_cell(struct hash_cell *hc)
171 static int register_with_devfs(struct hash_cell *hc)
173 struct gendisk *disk = dm_disk(hc->md);
175 devfs_mk_bdev(MKDEV(disk->major, disk->first_minor),
176 S_IFBLK | S_IRUSR | S_IWUSR | S_IRGRP,
177 DM_DIR "/%s", hc->name);
181 static int unregister_with_devfs(struct hash_cell *hc)
183 devfs_remove(DM_DIR"/%s", hc->name);
188 * The kdev_t and uuid of a device can never change once it is
189 * initially inserted.
191 static int dm_hash_insert(const char *name, const char *uuid, struct mapped_device *md)
193 struct hash_cell *cell;
196 * Allocate the new cells.
198 cell = alloc_cell(name, uuid, md);
203 * Insert the cell into both hash tables.
205 down_write(&_hash_lock);
206 if (__get_name_cell(name))
209 list_add(&cell->name_list, _name_buckets + hash_str(name));
212 if (__get_uuid_cell(uuid)) {
213 list_del(&cell->name_list);
216 list_add(&cell->uuid_list, _uuid_buckets + hash_str(uuid));
218 register_with_devfs(cell);
220 dm_set_mdptr(md, cell);
221 up_write(&_hash_lock);
226 up_write(&_hash_lock);
231 static void __hash_remove(struct hash_cell *hc)
233 struct dm_table *table;
235 /* remove from the dev hash */
236 list_del(&hc->uuid_list);
237 list_del(&hc->name_list);
238 unregister_with_devfs(hc);
239 dm_set_mdptr(hc->md, NULL);
241 table = dm_get_table(hc->md);
243 dm_table_event(table);
249 dm_table_put(hc->new_map);
253 static void dm_hash_remove_all(void)
256 struct hash_cell *hc;
257 struct list_head *tmp, *n;
259 down_write(&_hash_lock);
260 for (i = 0; i < NUM_BUCKETS; i++) {
261 list_for_each_safe (tmp, n, _name_buckets + i) {
262 hc = list_entry(tmp, struct hash_cell, name_list);
266 up_write(&_hash_lock);
269 static int dm_hash_rename(const char *old, const char *new)
271 char *new_name, *old_name;
272 struct hash_cell *hc;
273 struct dm_table *table;
278 new_name = kstrdup(new, GFP_KERNEL);
282 down_write(&_hash_lock);
287 hc = __get_name_cell(new);
289 DMWARN("asked to rename to an already existing name %s -> %s",
291 up_write(&_hash_lock);
297 * Is there such a device as 'old' ?
299 hc = __get_name_cell(old);
301 DMWARN("asked to rename a non existent device %s -> %s",
303 up_write(&_hash_lock);
309 * rename and move the name cell.
311 unregister_with_devfs(hc);
313 list_del(&hc->name_list);
316 list_add(&hc->name_list, _name_buckets + hash_str(new_name));
318 /* rename the device node in devfs */
319 register_with_devfs(hc);
322 * Wake up any dm event waiters.
324 table = dm_get_table(hc->md);
326 dm_table_event(table);
330 up_write(&_hash_lock);
335 /*-----------------------------------------------------------------
336 * Implementation of the ioctl commands
337 *---------------------------------------------------------------*/
339 * All the ioctl commands get dispatched to functions with this
342 typedef int (*ioctl_fn)(struct dm_ioctl *param, size_t param_size);
344 static int remove_all(struct dm_ioctl *param, size_t param_size)
346 dm_hash_remove_all();
347 param->data_size = 0;
352 * Round up the ptr to an 8-byte boundary.
355 static inline void *align_ptr(void *ptr)
357 return (void *) (((size_t) (ptr + ALIGN_MASK)) & ~ALIGN_MASK);
361 * Retrieves the data payload buffer from an already allocated
364 static void *get_result_buffer(struct dm_ioctl *param, size_t param_size,
367 param->data_start = align_ptr(param + 1) - (void *) param;
369 if (param->data_start < param_size)
370 *len = param_size - param->data_start;
374 return ((void *) param) + param->data_start;
377 static int list_devices(struct dm_ioctl *param, size_t param_size)
380 struct hash_cell *hc;
381 size_t len, needed = 0;
382 struct gendisk *disk;
383 struct dm_name_list *nl, *old_nl = NULL;
385 down_write(&_hash_lock);
388 * Loop through all the devices working out how much
391 for (i = 0; i < NUM_BUCKETS; i++) {
392 list_for_each_entry (hc, _name_buckets + i, name_list) {
393 needed += sizeof(struct dm_name_list);
394 needed += strlen(hc->name) + 1;
395 needed += ALIGN_MASK;
400 * Grab our output buffer.
402 nl = get_result_buffer(param, param_size, &len);
404 param->flags |= DM_BUFFER_FULL_FLAG;
407 param->data_size = param->data_start + needed;
409 nl->dev = 0; /* Flags no data */
412 * Now loop through filling out the names.
414 for (i = 0; i < NUM_BUCKETS; i++) {
415 list_for_each_entry (hc, _name_buckets + i, name_list) {
417 old_nl->next = (uint32_t) ((void *) nl -
419 disk = dm_disk(hc->md);
420 nl->dev = huge_encode_dev(MKDEV(disk->major, disk->first_minor));
422 strcpy(nl->name, hc->name);
425 nl = align_ptr(((void *) ++nl) + strlen(hc->name) + 1);
430 up_write(&_hash_lock);
434 static void list_version_get_needed(struct target_type *tt, void *needed_param)
436 size_t *needed = needed_param;
438 *needed += sizeof(struct dm_target_versions);
439 *needed += strlen(tt->name);
440 *needed += ALIGN_MASK;
443 static void list_version_get_info(struct target_type *tt, void *param)
445 struct vers_iter *info = param;
447 /* Check space - it might have changed since the first iteration */
448 if ((char *)info->vers + sizeof(tt->version) + strlen(tt->name) + 1 >
451 info->flags = DM_BUFFER_FULL_FLAG;
456 info->old_vers->next = (uint32_t) ((void *)info->vers -
457 (void *)info->old_vers);
458 info->vers->version[0] = tt->version[0];
459 info->vers->version[1] = tt->version[1];
460 info->vers->version[2] = tt->version[2];
461 info->vers->next = 0;
462 strcpy(info->vers->name, tt->name);
464 info->old_vers = info->vers;
465 info->vers = align_ptr(((void *) ++info->vers) + strlen(tt->name) + 1);
468 static int list_versions(struct dm_ioctl *param, size_t param_size)
470 size_t len, needed = 0;
471 struct dm_target_versions *vers;
472 struct vers_iter iter_info;
475 * Loop through all the devices working out how much
478 dm_target_iterate(list_version_get_needed, &needed);
481 * Grab our output buffer.
483 vers = get_result_buffer(param, param_size, &len);
485 param->flags |= DM_BUFFER_FULL_FLAG;
488 param->data_size = param->data_start + needed;
490 iter_info.param_size = param_size;
491 iter_info.old_vers = NULL;
492 iter_info.vers = vers;
494 iter_info.end = (char *)vers+len;
497 * Now loop through filling out the names & versions.
499 dm_target_iterate(list_version_get_info, &iter_info);
500 param->flags |= iter_info.flags;
508 static int check_name(const char *name)
510 if (strchr(name, '/')) {
511 DMWARN("invalid device name");
519 * Fills in a dm_ioctl structure, ready for sending back to
522 static int __dev_status(struct mapped_device *md, struct dm_ioctl *param)
524 struct gendisk *disk = dm_disk(md);
525 struct dm_table *table;
526 struct block_device *bdev;
528 param->flags &= ~(DM_SUSPEND_FLAG | DM_READONLY_FLAG |
529 DM_ACTIVE_PRESENT_FLAG);
531 if (dm_suspended(md))
532 param->flags |= DM_SUSPEND_FLAG;
534 param->dev = huge_encode_dev(MKDEV(disk->major, disk->first_minor));
536 if (!(param->flags & DM_SKIP_BDGET_FLAG)) {
537 bdev = bdget_disk(disk, 0);
542 * Yes, this will be out of date by the time it gets back
543 * to userland, but it is still very useful for
546 param->open_count = bdev->bd_openers;
549 param->open_count = -1;
552 param->flags |= DM_READONLY_FLAG;
554 param->event_nr = dm_get_event_nr(md);
556 table = dm_get_table(md);
558 param->flags |= DM_ACTIVE_PRESENT_FLAG;
559 param->target_count = dm_table_get_num_targets(table);
562 param->target_count = 0;
567 static int dev_create(struct dm_ioctl *param, size_t param_size)
570 struct mapped_device *md;
572 r = check_name(param->name);
576 if (param->flags & DM_PERSISTENT_DEV_FLAG)
577 r = dm_create_with_minor(MINOR(huge_decode_dev(param->dev)), &md);
584 r = dm_hash_insert(param->name, *param->uuid ? param->uuid : NULL, md);
590 param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
592 r = __dev_status(md, param);
599 * Always use UUID for lookups if it's present, otherwise use name or dev.
601 static inline struct hash_cell *__find_device_hash_cell(struct dm_ioctl *param)
604 return __get_uuid_cell(param->uuid);
605 else if (*param->name)
606 return __get_name_cell(param->name);
608 return dm_get_mdptr(huge_decode_dev(param->dev));
611 static inline struct mapped_device *find_device(struct dm_ioctl *param)
613 struct hash_cell *hc;
614 struct mapped_device *md = NULL;
616 down_read(&_hash_lock);
617 hc = __find_device_hash_cell(param);
623 * Sneakily write in both the name and the uuid
624 * while we have the cell.
626 strncpy(param->name, hc->name, sizeof(param->name));
628 strncpy(param->uuid, hc->uuid, sizeof(param->uuid)-1);
630 param->uuid[0] = '\0';
633 param->flags |= DM_INACTIVE_PRESENT_FLAG;
635 param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
637 up_read(&_hash_lock);
642 static int dev_remove(struct dm_ioctl *param, size_t param_size)
644 struct hash_cell *hc;
646 down_write(&_hash_lock);
647 hc = __find_device_hash_cell(param);
650 DMWARN("device doesn't appear to be in the dev hash table.");
651 up_write(&_hash_lock);
656 up_write(&_hash_lock);
657 param->data_size = 0;
662 * Check a string doesn't overrun the chunk of
663 * memory we copied from userland.
665 static int invalid_str(char *str, void *end)
667 while ((void *) str < end)
674 static int dev_rename(struct dm_ioctl *param, size_t param_size)
677 char *new_name = (char *) param + param->data_start;
679 if (new_name < (char *) (param + 1) ||
680 invalid_str(new_name, (void *) param + param_size)) {
681 DMWARN("Invalid new logical volume name supplied.");
685 r = check_name(new_name);
689 param->data_size = 0;
690 return dm_hash_rename(param->name, new_name);
693 static int do_suspend(struct dm_ioctl *param)
696 struct mapped_device *md;
698 md = find_device(param);
702 if (!dm_suspended(md))
703 r = dm_suspend(md, 1);
706 r = __dev_status(md, param);
712 static int do_resume(struct dm_ioctl *param)
715 struct hash_cell *hc;
716 struct mapped_device *md;
717 struct dm_table *new_map;
719 down_write(&_hash_lock);
721 hc = __find_device_hash_cell(param);
723 DMWARN("device doesn't appear to be in the dev hash table.");
724 up_write(&_hash_lock);
731 new_map = hc->new_map;
733 param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
735 up_write(&_hash_lock);
737 /* Do we need to load a new map ? */
739 /* Suspend if it isn't already suspended */
740 if (!dm_suspended(md))
743 r = dm_swap_table(md, new_map);
746 dm_table_put(new_map);
750 if (dm_table_get_mode(new_map) & FMODE_WRITE)
751 set_disk_ro(dm_disk(md), 0);
753 set_disk_ro(dm_disk(md), 1);
755 dm_table_put(new_map);
758 if (dm_suspended(md))
762 r = __dev_status(md, param);
769 * Set or unset the suspension state of a device.
770 * If the device already is in the requested state we just return its status.
772 static int dev_suspend(struct dm_ioctl *param, size_t param_size)
774 if (param->flags & DM_SUSPEND_FLAG)
775 return do_suspend(param);
777 return do_resume(param);
781 * Copies device info back to user space, used by
782 * the create and info ioctls.
784 static int dev_status(struct dm_ioctl *param, size_t param_size)
787 struct mapped_device *md;
789 md = find_device(param);
793 r = __dev_status(md, param);
799 * Build up the status struct for each target
801 static void retrieve_status(struct dm_table *table,
802 struct dm_ioctl *param, size_t param_size)
804 unsigned int i, num_targets;
805 struct dm_target_spec *spec;
806 char *outbuf, *outptr;
808 size_t remaining, len, used = 0;
810 outptr = outbuf = get_result_buffer(param, param_size, &len);
812 if (param->flags & DM_STATUS_TABLE_FLAG)
813 type = STATUSTYPE_TABLE;
815 type = STATUSTYPE_INFO;
817 /* Get all the target info */
818 num_targets = dm_table_get_num_targets(table);
819 for (i = 0; i < num_targets; i++) {
820 struct dm_target *ti = dm_table_get_target(table, i);
822 remaining = len - (outptr - outbuf);
823 if (remaining <= sizeof(struct dm_target_spec)) {
824 param->flags |= DM_BUFFER_FULL_FLAG;
828 spec = (struct dm_target_spec *) outptr;
831 spec->sector_start = ti->begin;
832 spec->length = ti->len;
833 strncpy(spec->target_type, ti->type->name,
834 sizeof(spec->target_type));
836 outptr += sizeof(struct dm_target_spec);
837 remaining = len - (outptr - outbuf);
838 if (remaining <= 0) {
839 param->flags |= DM_BUFFER_FULL_FLAG;
843 /* Get the status/table string from the target driver */
844 if (ti->type->status) {
845 if (ti->type->status(ti, type, outptr, remaining)) {
846 param->flags |= DM_BUFFER_FULL_FLAG;
852 outptr += strlen(outptr) + 1;
853 used = param->data_start + (outptr - outbuf);
855 outptr = align_ptr(outptr);
856 spec->next = outptr - outbuf;
860 param->data_size = used;
862 param->target_count = num_targets;
866 * Wait for a device to report an event
868 static int dev_wait(struct dm_ioctl *param, size_t param_size)
871 struct mapped_device *md;
872 struct dm_table *table;
874 md = find_device(param);
879 * Wait for a notification event
881 if (dm_wait_event(md, param->event_nr)) {
887 * The userland program is going to want to know what
888 * changed to trigger the event, so we may as well tell
889 * him and save an ioctl.
891 r = __dev_status(md, param);
895 table = dm_get_table(md);
897 retrieve_status(table, param, param_size);
906 static inline int get_mode(struct dm_ioctl *param)
908 int mode = FMODE_READ | FMODE_WRITE;
910 if (param->flags & DM_READONLY_FLAG)
916 static int next_target(struct dm_target_spec *last, uint32_t next, void *end,
917 struct dm_target_spec **spec, char **target_params)
919 *spec = (struct dm_target_spec *) ((unsigned char *) last + next);
920 *target_params = (char *) (*spec + 1);
922 if (*spec < (last + 1))
925 return invalid_str(*target_params, end);
928 static int populate_table(struct dm_table *table,
929 struct dm_ioctl *param, size_t param_size)
933 struct dm_target_spec *spec = (struct dm_target_spec *) param;
934 uint32_t next = param->data_start;
935 void *end = (void *) param + param_size;
938 if (!param->target_count) {
939 DMWARN("populate_table: no targets specified");
943 for (i = 0; i < param->target_count; i++) {
945 r = next_target(spec, next, end, &spec, &target_params);
947 DMWARN("unable to find target");
951 r = dm_table_add_target(table, spec->target_type,
952 (sector_t) spec->sector_start,
953 (sector_t) spec->length,
956 DMWARN("error adding target to table");
963 return dm_table_complete(table);
966 static int table_load(struct dm_ioctl *param, size_t param_size)
969 struct hash_cell *hc;
972 r = dm_table_create(&t, get_mode(param), param->target_count);
976 r = populate_table(t, param, param_size);
982 down_write(&_hash_lock);
983 hc = __find_device_hash_cell(param);
985 DMWARN("device doesn't appear to be in the dev hash table.");
986 up_write(&_hash_lock);
992 dm_table_put(hc->new_map);
994 param->flags |= DM_INACTIVE_PRESENT_FLAG;
996 r = __dev_status(hc->md, param);
997 up_write(&_hash_lock);
1001 static int table_clear(struct dm_ioctl *param, size_t param_size)
1004 struct hash_cell *hc;
1006 down_write(&_hash_lock);
1008 hc = __find_device_hash_cell(param);
1010 DMWARN("device doesn't appear to be in the dev hash table.");
1011 up_write(&_hash_lock);
1016 dm_table_put(hc->new_map);
1020 param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
1022 r = __dev_status(hc->md, param);
1023 up_write(&_hash_lock);
1028 * Retrieves a list of devices used by a particular dm device.
1030 static void retrieve_deps(struct dm_table *table,
1031 struct dm_ioctl *param, size_t param_size)
1033 unsigned int count = 0;
1034 struct list_head *tmp;
1037 struct dm_target_deps *deps;
1039 deps = get_result_buffer(param, param_size, &len);
1042 * Count the devices.
1044 list_for_each (tmp, dm_table_get_devices(table))
1048 * Check we have enough space.
1050 needed = sizeof(*deps) + (sizeof(*deps->dev) * count);
1052 param->flags |= DM_BUFFER_FULL_FLAG;
1057 * Fill in the devices.
1059 deps->count = count;
1061 list_for_each_entry (dd, dm_table_get_devices(table), list)
1062 deps->dev[count++] = huge_encode_dev(dd->bdev->bd_dev);
1064 param->data_size = param->data_start + needed;
1067 static int table_deps(struct dm_ioctl *param, size_t param_size)
1070 struct mapped_device *md;
1071 struct dm_table *table;
1073 md = find_device(param);
1077 r = __dev_status(md, param);
1081 table = dm_get_table(md);
1083 retrieve_deps(table, param, param_size);
1084 dm_table_put(table);
1093 * Return the status of a device as a text string for each
1096 static int table_status(struct dm_ioctl *param, size_t param_size)
1099 struct mapped_device *md;
1100 struct dm_table *table;
1102 md = find_device(param);
1106 r = __dev_status(md, param);
1110 table = dm_get_table(md);
1112 retrieve_status(table, param, param_size);
1113 dm_table_put(table);
1122 * Pass a message to the target that's at the supplied device offset.
1124 static int target_message(struct dm_ioctl *param, size_t param_size)
1128 struct mapped_device *md;
1129 struct dm_table *table;
1130 struct dm_target *ti;
1131 struct dm_target_msg *tmsg = (void *) param + param->data_start;
1133 md = find_device(param);
1137 r = __dev_status(md, param);
1141 if (tmsg < (struct dm_target_msg *) (param + 1) ||
1142 invalid_str(tmsg->message, (void *) param + param_size)) {
1143 DMWARN("Invalid target message parameters.");
1148 r = dm_split_args(&argc, &argv, tmsg->message);
1150 DMWARN("Failed to split target message parameters");
1154 table = dm_get_table(md);
1158 if (tmsg->sector >= dm_table_get_size(table)) {
1159 DMWARN("Target message sector outside device.");
1164 ti = dm_table_find_target(table, tmsg->sector);
1165 if (ti->type->message)
1166 r = ti->type->message(ti, argc, argv);
1168 DMWARN("Target type does not support messages");
1173 dm_table_put(table);
1177 param->data_size = 0;
1182 /*-----------------------------------------------------------------
1183 * Implementation of open/close/ioctl on the special char
1185 *---------------------------------------------------------------*/
1186 static ioctl_fn lookup_ioctl(unsigned int cmd)
1192 {DM_VERSION_CMD, NULL}, /* version is dealt with elsewhere */
1193 {DM_REMOVE_ALL_CMD, remove_all},
1194 {DM_LIST_DEVICES_CMD, list_devices},
1196 {DM_DEV_CREATE_CMD, dev_create},
1197 {DM_DEV_REMOVE_CMD, dev_remove},
1198 {DM_DEV_RENAME_CMD, dev_rename},
1199 {DM_DEV_SUSPEND_CMD, dev_suspend},
1200 {DM_DEV_STATUS_CMD, dev_status},
1201 {DM_DEV_WAIT_CMD, dev_wait},
1203 {DM_TABLE_LOAD_CMD, table_load},
1204 {DM_TABLE_CLEAR_CMD, table_clear},
1205 {DM_TABLE_DEPS_CMD, table_deps},
1206 {DM_TABLE_STATUS_CMD, table_status},
1208 {DM_LIST_VERSIONS_CMD, list_versions},
1210 {DM_TARGET_MSG_CMD, target_message}
1213 return (cmd >= ARRAY_SIZE(_ioctls)) ? NULL : _ioctls[cmd].fn;
1217 * As well as checking the version compatibility this always
1218 * copies the kernel interface version out.
1220 static int check_version(unsigned int cmd, struct dm_ioctl __user *user)
1222 uint32_t version[3];
1225 if (copy_from_user(version, user->version, sizeof(version)))
1228 if ((DM_VERSION_MAJOR != version[0]) ||
1229 (DM_VERSION_MINOR < version[1])) {
1230 DMWARN("ioctl interface mismatch: "
1231 "kernel(%u.%u.%u), user(%u.%u.%u), cmd(%d)",
1232 DM_VERSION_MAJOR, DM_VERSION_MINOR,
1233 DM_VERSION_PATCHLEVEL,
1234 version[0], version[1], version[2], cmd);
1239 * Fill in the kernel version.
1241 version[0] = DM_VERSION_MAJOR;
1242 version[1] = DM_VERSION_MINOR;
1243 version[2] = DM_VERSION_PATCHLEVEL;
1244 if (copy_to_user(user->version, version, sizeof(version)))
1250 static void free_params(struct dm_ioctl *param)
1255 static int copy_params(struct dm_ioctl __user *user, struct dm_ioctl **param)
1257 struct dm_ioctl tmp, *dmi;
1259 if (copy_from_user(&tmp, user, sizeof(tmp)))
1262 if (tmp.data_size < sizeof(tmp))
1265 dmi = (struct dm_ioctl *) vmalloc(tmp.data_size);
1269 if (copy_from_user(dmi, user, tmp.data_size)) {
1278 static int validate_params(uint cmd, struct dm_ioctl *param)
1280 /* Always clear this flag */
1281 param->flags &= ~DM_BUFFER_FULL_FLAG;
1283 /* Ignores parameters */
1284 if (cmd == DM_REMOVE_ALL_CMD ||
1285 cmd == DM_LIST_DEVICES_CMD ||
1286 cmd == DM_LIST_VERSIONS_CMD)
1289 if ((cmd == DM_DEV_CREATE_CMD)) {
1290 if (!*param->name) {
1291 DMWARN("name not supplied when creating device");
1294 } else if ((*param->uuid && *param->name)) {
1295 DMWARN("only supply one of name or uuid, cmd(%u)", cmd);
1299 /* Ensure strings are terminated */
1300 param->name[DM_NAME_LEN - 1] = '\0';
1301 param->uuid[DM_UUID_LEN - 1] = '\0';
1306 static int ctl_ioctl(struct inode *inode, struct file *file,
1307 uint command, ulong u)
1311 struct dm_ioctl *param;
1312 struct dm_ioctl __user *user = (struct dm_ioctl __user *) u;
1316 /* only root can play with this */
1317 if (!capable(CAP_SYS_ADMIN))
1320 if (_IOC_TYPE(command) != DM_IOCTL)
1323 cmd = _IOC_NR(command);
1326 * Check the interface version passed in. This also
1327 * writes out the kernel's interface version.
1329 r = check_version(cmd, user);
1334 * Nothing more to do for the version command.
1336 if (cmd == DM_VERSION_CMD)
1339 fn = lookup_ioctl(cmd);
1341 DMWARN("dm_ctl_ioctl: unknown command 0x%x", command);
1346 * Trying to avoid low memory issues when a device is
1349 current->flags |= PF_MEMALLOC;
1352 * Copy the parameters into kernel space.
1354 r = copy_params(user, ¶m);
1356 current->flags &= ~PF_MEMALLOC;
1361 * FIXME: eventually we will remove the PF_MEMALLOC flag
1362 * here. However the tools still do nasty things like
1363 * 'load' while a device is suspended.
1366 r = validate_params(cmd, param);
1370 param_size = param->data_size;
1371 param->data_size = sizeof(*param);
1372 r = fn(param, param_size);
1375 * Copy the results back to userland.
1377 if (!r && copy_to_user(user, param, param->data_size))
1382 current->flags &= ~PF_MEMALLOC;
1386 static struct file_operations _ctl_fops = {
1388 .owner = THIS_MODULE,
1391 static struct miscdevice _dm_misc = {
1392 .minor = MISC_DYNAMIC_MINOR,
1394 .devfs_name = "mapper/control",
1399 * Create misc character device and link to DM_DIR/control.
1401 int __init dm_interface_init(void)
1409 r = misc_register(&_dm_misc);
1411 DMERR("misc_register failed for control device");
1416 DMINFO("%d.%d.%d%s initialised: %s", DM_VERSION_MAJOR,
1417 DM_VERSION_MINOR, DM_VERSION_PATCHLEVEL, DM_VERSION_EXTRA,
1422 void dm_interface_exit(void)
1424 if (misc_deregister(&_dm_misc) < 0)
1425 DMERR("misc_deregister failed for control device");