[PATCH] dm: improve error message consistency
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / md / dm-ioctl.c
blob3edb3477f987d4199c32d94e96d03e2c40e3ef1f
1 /*
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.
6 */
8 #include "dm.h"
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
27 * name or uuid.
28 *---------------------------------------------------------------*/
29 struct hash_cell {
30 struct list_head name_list;
31 struct list_head uuid_list;
33 char *name;
34 char *uuid;
35 struct mapped_device *md;
36 struct dm_table *new_map;
39 struct vers_iter {
40 size_t param_size;
41 struct dm_target_versions *vers, *old_vers;
42 char *end;
43 uint32_t flags;
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)
61 unsigned int i;
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);
71 devfs_mk_dir(DM_DIR);
72 return 0;
75 static void dm_hash_exit(void)
77 dm_hash_remove_all(0);
78 devfs_remove(DM_DIR);
81 /*-----------------------------------------------------------------
82 * Hash function:
83 * We're not really concerned with the str hash function being
84 * fast since it's only used by the ioctl interface.
85 *---------------------------------------------------------------*/
86 static unsigned int hash_str(const char *str)
88 const unsigned int hash_mult = 2654435387U;
89 unsigned int h = 0;
91 while (*str)
92 h = (h + (unsigned int) *str++) * hash_mult;
94 return h & MASK_BUCKETS;
97 /*-----------------------------------------------------------------
98 * Code for looking up a device by name
99 *---------------------------------------------------------------*/
100 static struct hash_cell *__get_name_cell(const char *str)
102 struct hash_cell *hc;
103 unsigned int h = hash_str(str);
105 list_for_each_entry (hc, _name_buckets + h, name_list)
106 if (!strcmp(hc->name, str)) {
107 dm_get(hc->md);
108 return hc;
111 return NULL;
114 static struct hash_cell *__get_uuid_cell(const char *str)
116 struct hash_cell *hc;
117 unsigned int h = hash_str(str);
119 list_for_each_entry (hc, _uuid_buckets + h, uuid_list)
120 if (!strcmp(hc->uuid, str)) {
121 dm_get(hc->md);
122 return hc;
125 return NULL;
128 /*-----------------------------------------------------------------
129 * Inserting, removing and renaming a device.
130 *---------------------------------------------------------------*/
131 static struct hash_cell *alloc_cell(const char *name, const char *uuid,
132 struct mapped_device *md)
134 struct hash_cell *hc;
136 hc = kmalloc(sizeof(*hc), GFP_KERNEL);
137 if (!hc)
138 return NULL;
140 hc->name = kstrdup(name, GFP_KERNEL);
141 if (!hc->name) {
142 kfree(hc);
143 return NULL;
146 if (!uuid)
147 hc->uuid = NULL;
149 else {
150 hc->uuid = kstrdup(uuid, GFP_KERNEL);
151 if (!hc->uuid) {
152 kfree(hc->name);
153 kfree(hc);
154 return NULL;
158 INIT_LIST_HEAD(&hc->name_list);
159 INIT_LIST_HEAD(&hc->uuid_list);
160 hc->md = md;
161 hc->new_map = NULL;
162 return hc;
165 static void free_cell(struct hash_cell *hc)
167 if (hc) {
168 kfree(hc->name);
169 kfree(hc->uuid);
170 kfree(hc);
175 * devfs stuff.
177 static int register_with_devfs(struct hash_cell *hc)
179 struct gendisk *disk = dm_disk(hc->md);
181 devfs_mk_bdev(MKDEV(disk->major, disk->first_minor),
182 S_IFBLK | S_IRUSR | S_IWUSR | S_IRGRP,
183 DM_DIR "/%s", hc->name);
184 return 0;
187 static int unregister_with_devfs(struct hash_cell *hc)
189 devfs_remove(DM_DIR"/%s", hc->name);
190 return 0;
194 * The kdev_t and uuid of a device can never change once it is
195 * initially inserted.
197 static int dm_hash_insert(const char *name, const char *uuid, struct mapped_device *md)
199 struct hash_cell *cell, *hc;
202 * Allocate the new cells.
204 cell = alloc_cell(name, uuid, md);
205 if (!cell)
206 return -ENOMEM;
209 * Insert the cell into both hash tables.
211 down_write(&_hash_lock);
212 hc = __get_name_cell(name);
213 if (hc) {
214 dm_put(hc->md);
215 goto bad;
218 list_add(&cell->name_list, _name_buckets + hash_str(name));
220 if (uuid) {
221 hc = __get_uuid_cell(uuid);
222 if (hc) {
223 list_del(&cell->name_list);
224 dm_put(hc->md);
225 goto bad;
227 list_add(&cell->uuid_list, _uuid_buckets + hash_str(uuid));
229 register_with_devfs(cell);
230 dm_get(md);
231 dm_set_mdptr(md, cell);
232 up_write(&_hash_lock);
234 return 0;
236 bad:
237 up_write(&_hash_lock);
238 free_cell(cell);
239 return -EBUSY;
242 static void __hash_remove(struct hash_cell *hc)
244 struct dm_table *table;
246 /* remove from the dev hash */
247 list_del(&hc->uuid_list);
248 list_del(&hc->name_list);
249 unregister_with_devfs(hc);
250 dm_set_mdptr(hc->md, NULL);
252 table = dm_get_table(hc->md);
253 if (table) {
254 dm_table_event(table);
255 dm_table_put(table);
258 if (hc->new_map)
259 dm_table_put(hc->new_map);
260 dm_put(hc->md);
261 free_cell(hc);
264 static void dm_hash_remove_all(int keep_open_devices)
266 int i, dev_skipped, dev_removed;
267 struct hash_cell *hc;
268 struct list_head *tmp, *n;
270 down_write(&_hash_lock);
272 retry:
273 dev_skipped = dev_removed = 0;
274 for (i = 0; i < NUM_BUCKETS; i++) {
275 list_for_each_safe (tmp, n, _name_buckets + i) {
276 hc = list_entry(tmp, struct hash_cell, name_list);
278 if (keep_open_devices &&
279 dm_lock_for_deletion(hc->md)) {
280 dev_skipped++;
281 continue;
283 __hash_remove(hc);
284 dev_removed = 1;
289 * Some mapped devices may be using other mapped devices, so if any
290 * still exist, repeat until we make no further progress.
292 if (dev_skipped) {
293 if (dev_removed)
294 goto retry;
296 DMWARN("remove_all left %d open device(s)", dev_skipped);
299 up_write(&_hash_lock);
302 static int dm_hash_rename(const char *old, const char *new)
304 char *new_name, *old_name;
305 struct hash_cell *hc;
306 struct dm_table *table;
309 * duplicate new.
311 new_name = kstrdup(new, GFP_KERNEL);
312 if (!new_name)
313 return -ENOMEM;
315 down_write(&_hash_lock);
318 * Is new free ?
320 hc = __get_name_cell(new);
321 if (hc) {
322 DMWARN("asked to rename to an already existing name %s -> %s",
323 old, new);
324 dm_put(hc->md);
325 up_write(&_hash_lock);
326 kfree(new_name);
327 return -EBUSY;
331 * Is there such a device as 'old' ?
333 hc = __get_name_cell(old);
334 if (!hc) {
335 DMWARN("asked to rename a non existent device %s -> %s",
336 old, new);
337 up_write(&_hash_lock);
338 kfree(new_name);
339 return -ENXIO;
343 * rename and move the name cell.
345 unregister_with_devfs(hc);
347 list_del(&hc->name_list);
348 old_name = hc->name;
349 hc->name = new_name;
350 list_add(&hc->name_list, _name_buckets + hash_str(new_name));
352 /* rename the device node in devfs */
353 register_with_devfs(hc);
356 * Wake up any dm event waiters.
358 table = dm_get_table(hc->md);
359 if (table) {
360 dm_table_event(table);
361 dm_table_put(table);
364 dm_put(hc->md);
365 up_write(&_hash_lock);
366 kfree(old_name);
367 return 0;
370 /*-----------------------------------------------------------------
371 * Implementation of the ioctl commands
372 *---------------------------------------------------------------*/
374 * All the ioctl commands get dispatched to functions with this
375 * prototype.
377 typedef int (*ioctl_fn)(struct dm_ioctl *param, size_t param_size);
379 static int remove_all(struct dm_ioctl *param, size_t param_size)
381 dm_hash_remove_all(1);
382 param->data_size = 0;
383 return 0;
387 * Round up the ptr to an 8-byte boundary.
389 #define ALIGN_MASK 7
390 static inline void *align_ptr(void *ptr)
392 return (void *) (((size_t) (ptr + ALIGN_MASK)) & ~ALIGN_MASK);
396 * Retrieves the data payload buffer from an already allocated
397 * struct dm_ioctl.
399 static void *get_result_buffer(struct dm_ioctl *param, size_t param_size,
400 size_t *len)
402 param->data_start = align_ptr(param + 1) - (void *) param;
404 if (param->data_start < param_size)
405 *len = param_size - param->data_start;
406 else
407 *len = 0;
409 return ((void *) param) + param->data_start;
412 static int list_devices(struct dm_ioctl *param, size_t param_size)
414 unsigned int i;
415 struct hash_cell *hc;
416 size_t len, needed = 0;
417 struct gendisk *disk;
418 struct dm_name_list *nl, *old_nl = NULL;
420 down_write(&_hash_lock);
423 * Loop through all the devices working out how much
424 * space we need.
426 for (i = 0; i < NUM_BUCKETS; i++) {
427 list_for_each_entry (hc, _name_buckets + i, name_list) {
428 needed += sizeof(struct dm_name_list);
429 needed += strlen(hc->name) + 1;
430 needed += ALIGN_MASK;
435 * Grab our output buffer.
437 nl = get_result_buffer(param, param_size, &len);
438 if (len < needed) {
439 param->flags |= DM_BUFFER_FULL_FLAG;
440 goto out;
442 param->data_size = param->data_start + needed;
444 nl->dev = 0; /* Flags no data */
447 * Now loop through filling out the names.
449 for (i = 0; i < NUM_BUCKETS; i++) {
450 list_for_each_entry (hc, _name_buckets + i, name_list) {
451 if (old_nl)
452 old_nl->next = (uint32_t) ((void *) nl -
453 (void *) old_nl);
454 disk = dm_disk(hc->md);
455 nl->dev = huge_encode_dev(MKDEV(disk->major, disk->first_minor));
456 nl->next = 0;
457 strcpy(nl->name, hc->name);
459 old_nl = nl;
460 nl = align_ptr(((void *) ++nl) + strlen(hc->name) + 1);
464 out:
465 up_write(&_hash_lock);
466 return 0;
469 static void list_version_get_needed(struct target_type *tt, void *needed_param)
471 size_t *needed = needed_param;
473 *needed += sizeof(struct dm_target_versions);
474 *needed += strlen(tt->name);
475 *needed += ALIGN_MASK;
478 static void list_version_get_info(struct target_type *tt, void *param)
480 struct vers_iter *info = param;
482 /* Check space - it might have changed since the first iteration */
483 if ((char *)info->vers + sizeof(tt->version) + strlen(tt->name) + 1 >
484 info->end) {
486 info->flags = DM_BUFFER_FULL_FLAG;
487 return;
490 if (info->old_vers)
491 info->old_vers->next = (uint32_t) ((void *)info->vers -
492 (void *)info->old_vers);
493 info->vers->version[0] = tt->version[0];
494 info->vers->version[1] = tt->version[1];
495 info->vers->version[2] = tt->version[2];
496 info->vers->next = 0;
497 strcpy(info->vers->name, tt->name);
499 info->old_vers = info->vers;
500 info->vers = align_ptr(((void *) ++info->vers) + strlen(tt->name) + 1);
503 static int list_versions(struct dm_ioctl *param, size_t param_size)
505 size_t len, needed = 0;
506 struct dm_target_versions *vers;
507 struct vers_iter iter_info;
510 * Loop through all the devices working out how much
511 * space we need.
513 dm_target_iterate(list_version_get_needed, &needed);
516 * Grab our output buffer.
518 vers = get_result_buffer(param, param_size, &len);
519 if (len < needed) {
520 param->flags |= DM_BUFFER_FULL_FLAG;
521 goto out;
523 param->data_size = param->data_start + needed;
525 iter_info.param_size = param_size;
526 iter_info.old_vers = NULL;
527 iter_info.vers = vers;
528 iter_info.flags = 0;
529 iter_info.end = (char *)vers+len;
532 * Now loop through filling out the names & versions.
534 dm_target_iterate(list_version_get_info, &iter_info);
535 param->flags |= iter_info.flags;
537 out:
538 return 0;
543 static int check_name(const char *name)
545 if (strchr(name, '/')) {
546 DMWARN("invalid device name");
547 return -EINVAL;
550 return 0;
554 * Fills in a dm_ioctl structure, ready for sending back to
555 * userland.
557 static int __dev_status(struct mapped_device *md, struct dm_ioctl *param)
559 struct gendisk *disk = dm_disk(md);
560 struct dm_table *table;
562 param->flags &= ~(DM_SUSPEND_FLAG | DM_READONLY_FLAG |
563 DM_ACTIVE_PRESENT_FLAG);
565 if (dm_suspended(md))
566 param->flags |= DM_SUSPEND_FLAG;
568 param->dev = huge_encode_dev(MKDEV(disk->major, disk->first_minor));
571 * Yes, this will be out of date by the time it gets back
572 * to userland, but it is still very useful for
573 * debugging.
575 param->open_count = dm_open_count(md);
577 if (disk->policy)
578 param->flags |= DM_READONLY_FLAG;
580 param->event_nr = dm_get_event_nr(md);
582 table = dm_get_table(md);
583 if (table) {
584 param->flags |= DM_ACTIVE_PRESENT_FLAG;
585 param->target_count = dm_table_get_num_targets(table);
586 dm_table_put(table);
587 } else
588 param->target_count = 0;
590 return 0;
593 static int dev_create(struct dm_ioctl *param, size_t param_size)
595 int r, m = DM_ANY_MINOR;
596 struct mapped_device *md;
598 r = check_name(param->name);
599 if (r)
600 return r;
602 if (param->flags & DM_PERSISTENT_DEV_FLAG)
603 m = MINOR(huge_decode_dev(param->dev));
605 r = dm_create(m, &md);
606 if (r)
607 return r;
609 r = dm_hash_insert(param->name, *param->uuid ? param->uuid : NULL, md);
610 if (r) {
611 dm_put(md);
612 return r;
615 param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
617 r = __dev_status(md, param);
618 dm_put(md);
620 return r;
624 * Always use UUID for lookups if it's present, otherwise use name or dev.
626 static struct hash_cell *__find_device_hash_cell(struct dm_ioctl *param)
628 struct mapped_device *md;
629 void *mdptr = NULL;
631 if (*param->uuid)
632 return __get_uuid_cell(param->uuid);
634 if (*param->name)
635 return __get_name_cell(param->name);
637 md = dm_get_md(huge_decode_dev(param->dev));
638 if (md)
639 mdptr = dm_get_mdptr(md);
641 return mdptr;
644 static struct mapped_device *find_device(struct dm_ioctl *param)
646 struct hash_cell *hc;
647 struct mapped_device *md = NULL;
649 down_read(&_hash_lock);
650 hc = __find_device_hash_cell(param);
651 if (hc) {
652 md = hc->md;
655 * Sneakily write in both the name and the uuid
656 * while we have the cell.
658 strncpy(param->name, hc->name, sizeof(param->name));
659 if (hc->uuid)
660 strncpy(param->uuid, hc->uuid, sizeof(param->uuid)-1);
661 else
662 param->uuid[0] = '\0';
664 if (hc->new_map)
665 param->flags |= DM_INACTIVE_PRESENT_FLAG;
666 else
667 param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
669 up_read(&_hash_lock);
671 return md;
674 static int dev_remove(struct dm_ioctl *param, size_t param_size)
676 struct hash_cell *hc;
677 struct mapped_device *md;
678 int r;
680 down_write(&_hash_lock);
681 hc = __find_device_hash_cell(param);
683 if (!hc) {
684 DMWARN("device doesn't appear to be in the dev hash table.");
685 up_write(&_hash_lock);
686 return -ENXIO;
689 md = hc->md;
692 * Ensure the device is not open and nothing further can open it.
694 r = dm_lock_for_deletion(md);
695 if (r) {
696 DMWARN("unable to remove open device %s", hc->name);
697 up_write(&_hash_lock);
698 dm_put(md);
699 return r;
702 __hash_remove(hc);
703 up_write(&_hash_lock);
704 dm_put(md);
705 param->data_size = 0;
706 return 0;
710 * Check a string doesn't overrun the chunk of
711 * memory we copied from userland.
713 static int invalid_str(char *str, void *end)
715 while ((void *) str < end)
716 if (!*str++)
717 return 0;
719 return -EINVAL;
722 static int dev_rename(struct dm_ioctl *param, size_t param_size)
724 int r;
725 char *new_name = (char *) param + param->data_start;
727 if (new_name < (char *) (param + 1) ||
728 invalid_str(new_name, (void *) param + param_size)) {
729 DMWARN("Invalid new logical volume name supplied.");
730 return -EINVAL;
733 r = check_name(new_name);
734 if (r)
735 return r;
737 param->data_size = 0;
738 return dm_hash_rename(param->name, new_name);
741 static int dev_set_geometry(struct dm_ioctl *param, size_t param_size)
743 int r = -EINVAL, x;
744 struct mapped_device *md;
745 struct hd_geometry geometry;
746 unsigned long indata[4];
747 char *geostr = (char *) param + param->data_start;
749 md = find_device(param);
750 if (!md)
751 return -ENXIO;
753 if (geostr < (char *) (param + 1) ||
754 invalid_str(geostr, (void *) param + param_size)) {
755 DMWARN("Invalid geometry supplied.");
756 goto out;
759 x = sscanf(geostr, "%lu %lu %lu %lu", indata,
760 indata + 1, indata + 2, indata + 3);
762 if (x != 4) {
763 DMWARN("Unable to interpret geometry settings.");
764 goto out;
767 if (indata[0] > 65535 || indata[1] > 255 ||
768 indata[2] > 255 || indata[3] > ULONG_MAX) {
769 DMWARN("Geometry exceeds range limits.");
770 goto out;
773 geometry.cylinders = indata[0];
774 geometry.heads = indata[1];
775 geometry.sectors = indata[2];
776 geometry.start = indata[3];
778 r = dm_set_geometry(md, &geometry);
779 if (!r)
780 r = __dev_status(md, param);
782 param->data_size = 0;
784 out:
785 dm_put(md);
786 return r;
789 static int do_suspend(struct dm_ioctl *param)
791 int r = 0;
792 int do_lockfs = 1;
793 struct mapped_device *md;
795 md = find_device(param);
796 if (!md)
797 return -ENXIO;
799 if (param->flags & DM_SKIP_LOCKFS_FLAG)
800 do_lockfs = 0;
802 if (!dm_suspended(md))
803 r = dm_suspend(md, do_lockfs);
805 if (!r)
806 r = __dev_status(md, param);
808 dm_put(md);
809 return r;
812 static int do_resume(struct dm_ioctl *param)
814 int r = 0;
815 int do_lockfs = 1;
816 struct hash_cell *hc;
817 struct mapped_device *md;
818 struct dm_table *new_map;
820 down_write(&_hash_lock);
822 hc = __find_device_hash_cell(param);
823 if (!hc) {
824 DMWARN("device doesn't appear to be in the dev hash table.");
825 up_write(&_hash_lock);
826 return -ENXIO;
829 md = hc->md;
831 new_map = hc->new_map;
832 hc->new_map = NULL;
833 param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
835 up_write(&_hash_lock);
837 /* Do we need to load a new map ? */
838 if (new_map) {
839 /* Suspend if it isn't already suspended */
840 if (param->flags & DM_SKIP_LOCKFS_FLAG)
841 do_lockfs = 0;
842 if (!dm_suspended(md))
843 dm_suspend(md, do_lockfs);
845 r = dm_swap_table(md, new_map);
846 if (r) {
847 dm_put(md);
848 dm_table_put(new_map);
849 return r;
852 if (dm_table_get_mode(new_map) & FMODE_WRITE)
853 set_disk_ro(dm_disk(md), 0);
854 else
855 set_disk_ro(dm_disk(md), 1);
857 dm_table_put(new_map);
860 if (dm_suspended(md))
861 r = dm_resume(md);
863 if (!r)
864 r = __dev_status(md, param);
866 dm_put(md);
867 return r;
871 * Set or unset the suspension state of a device.
872 * If the device already is in the requested state we just return its status.
874 static int dev_suspend(struct dm_ioctl *param, size_t param_size)
876 if (param->flags & DM_SUSPEND_FLAG)
877 return do_suspend(param);
879 return do_resume(param);
883 * Copies device info back to user space, used by
884 * the create and info ioctls.
886 static int dev_status(struct dm_ioctl *param, size_t param_size)
888 int r;
889 struct mapped_device *md;
891 md = find_device(param);
892 if (!md)
893 return -ENXIO;
895 r = __dev_status(md, param);
896 dm_put(md);
897 return r;
901 * Build up the status struct for each target
903 static void retrieve_status(struct dm_table *table,
904 struct dm_ioctl *param, size_t param_size)
906 unsigned int i, num_targets;
907 struct dm_target_spec *spec;
908 char *outbuf, *outptr;
909 status_type_t type;
910 size_t remaining, len, used = 0;
912 outptr = outbuf = get_result_buffer(param, param_size, &len);
914 if (param->flags & DM_STATUS_TABLE_FLAG)
915 type = STATUSTYPE_TABLE;
916 else
917 type = STATUSTYPE_INFO;
919 /* Get all the target info */
920 num_targets = dm_table_get_num_targets(table);
921 for (i = 0; i < num_targets; i++) {
922 struct dm_target *ti = dm_table_get_target(table, i);
924 remaining = len - (outptr - outbuf);
925 if (remaining <= sizeof(struct dm_target_spec)) {
926 param->flags |= DM_BUFFER_FULL_FLAG;
927 break;
930 spec = (struct dm_target_spec *) outptr;
932 spec->status = 0;
933 spec->sector_start = ti->begin;
934 spec->length = ti->len;
935 strncpy(spec->target_type, ti->type->name,
936 sizeof(spec->target_type));
938 outptr += sizeof(struct dm_target_spec);
939 remaining = len - (outptr - outbuf);
940 if (remaining <= 0) {
941 param->flags |= DM_BUFFER_FULL_FLAG;
942 break;
945 /* Get the status/table string from the target driver */
946 if (ti->type->status) {
947 if (ti->type->status(ti, type, outptr, remaining)) {
948 param->flags |= DM_BUFFER_FULL_FLAG;
949 break;
951 } else
952 outptr[0] = '\0';
954 outptr += strlen(outptr) + 1;
955 used = param->data_start + (outptr - outbuf);
957 outptr = align_ptr(outptr);
958 spec->next = outptr - outbuf;
961 if (used)
962 param->data_size = used;
964 param->target_count = num_targets;
968 * Wait for a device to report an event
970 static int dev_wait(struct dm_ioctl *param, size_t param_size)
972 int r;
973 struct mapped_device *md;
974 struct dm_table *table;
976 md = find_device(param);
977 if (!md)
978 return -ENXIO;
981 * Wait for a notification event
983 if (dm_wait_event(md, param->event_nr)) {
984 r = -ERESTARTSYS;
985 goto out;
989 * The userland program is going to want to know what
990 * changed to trigger the event, so we may as well tell
991 * him and save an ioctl.
993 r = __dev_status(md, param);
994 if (r)
995 goto out;
997 table = dm_get_table(md);
998 if (table) {
999 retrieve_status(table, param, param_size);
1000 dm_table_put(table);
1003 out:
1004 dm_put(md);
1005 return r;
1008 static inline int get_mode(struct dm_ioctl *param)
1010 int mode = FMODE_READ | FMODE_WRITE;
1012 if (param->flags & DM_READONLY_FLAG)
1013 mode = FMODE_READ;
1015 return mode;
1018 static int next_target(struct dm_target_spec *last, uint32_t next, void *end,
1019 struct dm_target_spec **spec, char **target_params)
1021 *spec = (struct dm_target_spec *) ((unsigned char *) last + next);
1022 *target_params = (char *) (*spec + 1);
1024 if (*spec < (last + 1))
1025 return -EINVAL;
1027 return invalid_str(*target_params, end);
1030 static int populate_table(struct dm_table *table,
1031 struct dm_ioctl *param, size_t param_size)
1033 int r;
1034 unsigned int i = 0;
1035 struct dm_target_spec *spec = (struct dm_target_spec *) param;
1036 uint32_t next = param->data_start;
1037 void *end = (void *) param + param_size;
1038 char *target_params;
1040 if (!param->target_count) {
1041 DMWARN("populate_table: no targets specified");
1042 return -EINVAL;
1045 for (i = 0; i < param->target_count; i++) {
1047 r = next_target(spec, next, end, &spec, &target_params);
1048 if (r) {
1049 DMWARN("unable to find target");
1050 return r;
1053 r = dm_table_add_target(table, spec->target_type,
1054 (sector_t) spec->sector_start,
1055 (sector_t) spec->length,
1056 target_params);
1057 if (r) {
1058 DMWARN("error adding target to table");
1059 return r;
1062 next = spec->next;
1065 return dm_table_complete(table);
1068 static int table_load(struct dm_ioctl *param, size_t param_size)
1070 int r;
1071 struct hash_cell *hc;
1072 struct dm_table *t;
1073 struct mapped_device *md;
1075 md = find_device(param);
1076 if (!md)
1077 return -ENXIO;
1079 r = dm_table_create(&t, get_mode(param), param->target_count, md);
1080 if (r)
1081 goto out;
1083 r = populate_table(t, param, param_size);
1084 if (r) {
1085 dm_table_put(t);
1086 goto out;
1089 down_write(&_hash_lock);
1090 hc = dm_get_mdptr(md);
1091 if (!hc || hc->md != md) {
1092 DMWARN("device has been removed from the dev hash table.");
1093 dm_table_put(t);
1094 up_write(&_hash_lock);
1095 r = -ENXIO;
1096 goto out;
1099 if (hc->new_map)
1100 dm_table_put(hc->new_map);
1101 hc->new_map = t;
1102 up_write(&_hash_lock);
1104 param->flags |= DM_INACTIVE_PRESENT_FLAG;
1105 r = __dev_status(md, param);
1107 out:
1108 dm_put(md);
1110 return r;
1113 static int table_clear(struct dm_ioctl *param, size_t param_size)
1115 int r;
1116 struct hash_cell *hc;
1117 struct mapped_device *md;
1119 down_write(&_hash_lock);
1121 hc = __find_device_hash_cell(param);
1122 if (!hc) {
1123 DMWARN("device doesn't appear to be in the dev hash table.");
1124 up_write(&_hash_lock);
1125 return -ENXIO;
1128 if (hc->new_map) {
1129 dm_table_put(hc->new_map);
1130 hc->new_map = NULL;
1133 param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
1135 r = __dev_status(hc->md, param);
1136 md = hc->md;
1137 up_write(&_hash_lock);
1138 dm_put(md);
1139 return r;
1143 * Retrieves a list of devices used by a particular dm device.
1145 static void retrieve_deps(struct dm_table *table,
1146 struct dm_ioctl *param, size_t param_size)
1148 unsigned int count = 0;
1149 struct list_head *tmp;
1150 size_t len, needed;
1151 struct dm_dev *dd;
1152 struct dm_target_deps *deps;
1154 deps = get_result_buffer(param, param_size, &len);
1157 * Count the devices.
1159 list_for_each (tmp, dm_table_get_devices(table))
1160 count++;
1163 * Check we have enough space.
1165 needed = sizeof(*deps) + (sizeof(*deps->dev) * count);
1166 if (len < needed) {
1167 param->flags |= DM_BUFFER_FULL_FLAG;
1168 return;
1172 * Fill in the devices.
1174 deps->count = count;
1175 count = 0;
1176 list_for_each_entry (dd, dm_table_get_devices(table), list)
1177 deps->dev[count++] = huge_encode_dev(dd->bdev->bd_dev);
1179 param->data_size = param->data_start + needed;
1182 static int table_deps(struct dm_ioctl *param, size_t param_size)
1184 int r = 0;
1185 struct mapped_device *md;
1186 struct dm_table *table;
1188 md = find_device(param);
1189 if (!md)
1190 return -ENXIO;
1192 r = __dev_status(md, param);
1193 if (r)
1194 goto out;
1196 table = dm_get_table(md);
1197 if (table) {
1198 retrieve_deps(table, param, param_size);
1199 dm_table_put(table);
1202 out:
1203 dm_put(md);
1204 return r;
1208 * Return the status of a device as a text string for each
1209 * target.
1211 static int table_status(struct dm_ioctl *param, size_t param_size)
1213 int r;
1214 struct mapped_device *md;
1215 struct dm_table *table;
1217 md = find_device(param);
1218 if (!md)
1219 return -ENXIO;
1221 r = __dev_status(md, param);
1222 if (r)
1223 goto out;
1225 table = dm_get_table(md);
1226 if (table) {
1227 retrieve_status(table, param, param_size);
1228 dm_table_put(table);
1231 out:
1232 dm_put(md);
1233 return r;
1237 * Pass a message to the target that's at the supplied device offset.
1239 static int target_message(struct dm_ioctl *param, size_t param_size)
1241 int r, argc;
1242 char **argv;
1243 struct mapped_device *md;
1244 struct dm_table *table;
1245 struct dm_target *ti;
1246 struct dm_target_msg *tmsg = (void *) param + param->data_start;
1248 md = find_device(param);
1249 if (!md)
1250 return -ENXIO;
1252 r = __dev_status(md, param);
1253 if (r)
1254 goto out;
1256 if (tmsg < (struct dm_target_msg *) (param + 1) ||
1257 invalid_str(tmsg->message, (void *) param + param_size)) {
1258 DMWARN("Invalid target message parameters.");
1259 r = -EINVAL;
1260 goto out;
1263 r = dm_split_args(&argc, &argv, tmsg->message);
1264 if (r) {
1265 DMWARN("Failed to split target message parameters");
1266 goto out;
1269 table = dm_get_table(md);
1270 if (!table)
1271 goto out_argv;
1273 if (tmsg->sector >= dm_table_get_size(table)) {
1274 DMWARN("Target message sector outside device.");
1275 r = -EINVAL;
1276 goto out_table;
1279 ti = dm_table_find_target(table, tmsg->sector);
1280 if (ti->type->message)
1281 r = ti->type->message(ti, argc, argv);
1282 else {
1283 DMWARN("Target type does not support messages");
1284 r = -EINVAL;
1287 out_table:
1288 dm_table_put(table);
1289 out_argv:
1290 kfree(argv);
1291 out:
1292 param->data_size = 0;
1293 dm_put(md);
1294 return r;
1297 /*-----------------------------------------------------------------
1298 * Implementation of open/close/ioctl on the special char
1299 * device.
1300 *---------------------------------------------------------------*/
1301 static ioctl_fn lookup_ioctl(unsigned int cmd)
1303 static struct {
1304 int cmd;
1305 ioctl_fn fn;
1306 } _ioctls[] = {
1307 {DM_VERSION_CMD, NULL}, /* version is dealt with elsewhere */
1308 {DM_REMOVE_ALL_CMD, remove_all},
1309 {DM_LIST_DEVICES_CMD, list_devices},
1311 {DM_DEV_CREATE_CMD, dev_create},
1312 {DM_DEV_REMOVE_CMD, dev_remove},
1313 {DM_DEV_RENAME_CMD, dev_rename},
1314 {DM_DEV_SUSPEND_CMD, dev_suspend},
1315 {DM_DEV_STATUS_CMD, dev_status},
1316 {DM_DEV_WAIT_CMD, dev_wait},
1318 {DM_TABLE_LOAD_CMD, table_load},
1319 {DM_TABLE_CLEAR_CMD, table_clear},
1320 {DM_TABLE_DEPS_CMD, table_deps},
1321 {DM_TABLE_STATUS_CMD, table_status},
1323 {DM_LIST_VERSIONS_CMD, list_versions},
1325 {DM_TARGET_MSG_CMD, target_message},
1326 {DM_DEV_SET_GEOMETRY_CMD, dev_set_geometry}
1329 return (cmd >= ARRAY_SIZE(_ioctls)) ? NULL : _ioctls[cmd].fn;
1333 * As well as checking the version compatibility this always
1334 * copies the kernel interface version out.
1336 static int check_version(unsigned int cmd, struct dm_ioctl __user *user)
1338 uint32_t version[3];
1339 int r = 0;
1341 if (copy_from_user(version, user->version, sizeof(version)))
1342 return -EFAULT;
1344 if ((DM_VERSION_MAJOR != version[0]) ||
1345 (DM_VERSION_MINOR < version[1])) {
1346 DMWARN("ioctl interface mismatch: "
1347 "kernel(%u.%u.%u), user(%u.%u.%u), cmd(%d)",
1348 DM_VERSION_MAJOR, DM_VERSION_MINOR,
1349 DM_VERSION_PATCHLEVEL,
1350 version[0], version[1], version[2], cmd);
1351 r = -EINVAL;
1355 * Fill in the kernel version.
1357 version[0] = DM_VERSION_MAJOR;
1358 version[1] = DM_VERSION_MINOR;
1359 version[2] = DM_VERSION_PATCHLEVEL;
1360 if (copy_to_user(user->version, version, sizeof(version)))
1361 return -EFAULT;
1363 return r;
1366 static void free_params(struct dm_ioctl *param)
1368 vfree(param);
1371 static int copy_params(struct dm_ioctl __user *user, struct dm_ioctl **param)
1373 struct dm_ioctl tmp, *dmi;
1375 if (copy_from_user(&tmp, user, sizeof(tmp)))
1376 return -EFAULT;
1378 if (tmp.data_size < sizeof(tmp))
1379 return -EINVAL;
1381 dmi = (struct dm_ioctl *) vmalloc(tmp.data_size);
1382 if (!dmi)
1383 return -ENOMEM;
1385 if (copy_from_user(dmi, user, tmp.data_size)) {
1386 vfree(dmi);
1387 return -EFAULT;
1390 *param = dmi;
1391 return 0;
1394 static int validate_params(uint cmd, struct dm_ioctl *param)
1396 /* Always clear this flag */
1397 param->flags &= ~DM_BUFFER_FULL_FLAG;
1399 /* Ignores parameters */
1400 if (cmd == DM_REMOVE_ALL_CMD ||
1401 cmd == DM_LIST_DEVICES_CMD ||
1402 cmd == DM_LIST_VERSIONS_CMD)
1403 return 0;
1405 if ((cmd == DM_DEV_CREATE_CMD)) {
1406 if (!*param->name) {
1407 DMWARN("name not supplied when creating device");
1408 return -EINVAL;
1410 } else if ((*param->uuid && *param->name)) {
1411 DMWARN("only supply one of name or uuid, cmd(%u)", cmd);
1412 return -EINVAL;
1415 /* Ensure strings are terminated */
1416 param->name[DM_NAME_LEN - 1] = '\0';
1417 param->uuid[DM_UUID_LEN - 1] = '\0';
1419 return 0;
1422 static int ctl_ioctl(struct inode *inode, struct file *file,
1423 uint command, ulong u)
1425 int r = 0;
1426 unsigned int cmd;
1427 struct dm_ioctl *param;
1428 struct dm_ioctl __user *user = (struct dm_ioctl __user *) u;
1429 ioctl_fn fn = NULL;
1430 size_t param_size;
1432 /* only root can play with this */
1433 if (!capable(CAP_SYS_ADMIN))
1434 return -EACCES;
1436 if (_IOC_TYPE(command) != DM_IOCTL)
1437 return -ENOTTY;
1439 cmd = _IOC_NR(command);
1442 * Check the interface version passed in. This also
1443 * writes out the kernel's interface version.
1445 r = check_version(cmd, user);
1446 if (r)
1447 return r;
1450 * Nothing more to do for the version command.
1452 if (cmd == DM_VERSION_CMD)
1453 return 0;
1455 fn = lookup_ioctl(cmd);
1456 if (!fn) {
1457 DMWARN("dm_ctl_ioctl: unknown command 0x%x", command);
1458 return -ENOTTY;
1462 * Trying to avoid low memory issues when a device is
1463 * suspended.
1465 current->flags |= PF_MEMALLOC;
1468 * Copy the parameters into kernel space.
1470 r = copy_params(user, &param);
1472 current->flags &= ~PF_MEMALLOC;
1474 if (r)
1475 return r;
1477 r = validate_params(cmd, param);
1478 if (r)
1479 goto out;
1481 param_size = param->data_size;
1482 param->data_size = sizeof(*param);
1483 r = fn(param, param_size);
1486 * Copy the results back to userland.
1488 if (!r && copy_to_user(user, param, param->data_size))
1489 r = -EFAULT;
1491 out:
1492 free_params(param);
1493 return r;
1496 static struct file_operations _ctl_fops = {
1497 .ioctl = ctl_ioctl,
1498 .owner = THIS_MODULE,
1501 static struct miscdevice _dm_misc = {
1502 .minor = MISC_DYNAMIC_MINOR,
1503 .name = DM_NAME,
1504 .devfs_name = "mapper/control",
1505 .fops = &_ctl_fops
1509 * Create misc character device and link to DM_DIR/control.
1511 int __init dm_interface_init(void)
1513 int r;
1515 r = dm_hash_init();
1516 if (r)
1517 return r;
1519 r = misc_register(&_dm_misc);
1520 if (r) {
1521 DMERR("misc_register failed for control device");
1522 dm_hash_exit();
1523 return r;
1526 DMINFO("%d.%d.%d%s initialised: %s", DM_VERSION_MAJOR,
1527 DM_VERSION_MINOR, DM_VERSION_PATCHLEVEL, DM_VERSION_EXTRA,
1528 DM_DRIVER_EMAIL);
1529 return 0;
1532 void dm_interface_exit(void)
1534 if (misc_deregister(&_dm_misc) < 0)
1535 DMERR("misc_deregister failed for control device");
1537 dm_hash_exit();