[MTD] AMD Geode NAND support can depend on X86_32; we won't see it on x86_64
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / md / dm-ioctl.c
blob8edd6435414df900777cbc78d4bf4988d0be37e1
1 /*
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.
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_DRIVER_EMAIL "dm-devel@redhat.com"
24 /*-----------------------------------------------------------------
25 * The ioctl interface needs to be able to look up devices by
26 * name or uuid.
27 *---------------------------------------------------------------*/
28 struct hash_cell {
29 struct list_head name_list;
30 struct list_head uuid_list;
32 char *name;
33 char *uuid;
34 struct mapped_device *md;
35 struct dm_table *new_map;
38 struct vers_iter {
39 size_t param_size;
40 struct dm_target_versions *vers, *old_vers;
41 char *end;
42 uint32_t flags;
46 #define NUM_BUCKETS 64
47 #define MASK_BUCKETS (NUM_BUCKETS - 1)
48 static struct list_head _name_buckets[NUM_BUCKETS];
49 static struct list_head _uuid_buckets[NUM_BUCKETS];
51 static void dm_hash_remove_all(void);
54 * Guards access to both hash tables.
56 static DECLARE_RWSEM(_hash_lock);
58 static void init_buckets(struct list_head *buckets)
60 unsigned int i;
62 for (i = 0; i < NUM_BUCKETS; i++)
63 INIT_LIST_HEAD(buckets + i);
66 static int dm_hash_init(void)
68 init_buckets(_name_buckets);
69 init_buckets(_uuid_buckets);
70 devfs_mk_dir(DM_DIR);
71 return 0;
74 static void dm_hash_exit(void)
76 dm_hash_remove_all();
77 devfs_remove(DM_DIR);
80 /*-----------------------------------------------------------------
81 * Hash function:
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;
88 unsigned int h = 0;
90 while (*str)
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))
106 return hc;
108 return NULL;
111 static struct hash_cell *__get_uuid_cell(const char *str)
113 struct hash_cell *hc;
114 unsigned int h = hash_str(str);
116 list_for_each_entry (hc, _uuid_buckets + h, uuid_list)
117 if (!strcmp(hc->uuid, str))
118 return hc;
120 return NULL;
123 /*-----------------------------------------------------------------
124 * Inserting, removing and renaming a device.
125 *---------------------------------------------------------------*/
126 static struct hash_cell *alloc_cell(const char *name, const char *uuid,
127 struct mapped_device *md)
129 struct hash_cell *hc;
131 hc = kmalloc(sizeof(*hc), GFP_KERNEL);
132 if (!hc)
133 return NULL;
135 hc->name = kstrdup(name, GFP_KERNEL);
136 if (!hc->name) {
137 kfree(hc);
138 return NULL;
141 if (!uuid)
142 hc->uuid = NULL;
144 else {
145 hc->uuid = kstrdup(uuid, GFP_KERNEL);
146 if (!hc->uuid) {
147 kfree(hc->name);
148 kfree(hc);
149 return NULL;
153 INIT_LIST_HEAD(&hc->name_list);
154 INIT_LIST_HEAD(&hc->uuid_list);
155 hc->md = md;
156 hc->new_map = NULL;
157 return hc;
160 static void free_cell(struct hash_cell *hc)
162 if (hc) {
163 kfree(hc->name);
164 kfree(hc->uuid);
165 kfree(hc);
170 * devfs stuff.
172 static int register_with_devfs(struct hash_cell *hc)
174 struct gendisk *disk = dm_disk(hc->md);
176 devfs_mk_bdev(MKDEV(disk->major, disk->first_minor),
177 S_IFBLK | S_IRUSR | S_IWUSR | S_IRGRP,
178 DM_DIR "/%s", hc->name);
179 return 0;
182 static int unregister_with_devfs(struct hash_cell *hc)
184 devfs_remove(DM_DIR"/%s", hc->name);
185 return 0;
189 * The kdev_t and uuid of a device can never change once it is
190 * initially inserted.
192 static int dm_hash_insert(const char *name, const char *uuid, struct mapped_device *md)
194 struct hash_cell *cell;
197 * Allocate the new cells.
199 cell = alloc_cell(name, uuid, md);
200 if (!cell)
201 return -ENOMEM;
204 * Insert the cell into both hash tables.
206 down_write(&_hash_lock);
207 if (__get_name_cell(name))
208 goto bad;
210 list_add(&cell->name_list, _name_buckets + hash_str(name));
212 if (uuid) {
213 if (__get_uuid_cell(uuid)) {
214 list_del(&cell->name_list);
215 goto bad;
217 list_add(&cell->uuid_list, _uuid_buckets + hash_str(uuid));
219 register_with_devfs(cell);
220 dm_get(md);
221 dm_set_mdptr(md, cell);
222 up_write(&_hash_lock);
224 return 0;
226 bad:
227 up_write(&_hash_lock);
228 free_cell(cell);
229 return -EBUSY;
232 static void __hash_remove(struct hash_cell *hc)
234 struct dm_table *table;
236 /* remove from the dev hash */
237 list_del(&hc->uuid_list);
238 list_del(&hc->name_list);
239 unregister_with_devfs(hc);
240 dm_set_mdptr(hc->md, NULL);
242 table = dm_get_table(hc->md);
243 if (table) {
244 dm_table_event(table);
245 dm_table_put(table);
248 if (hc->new_map)
249 dm_table_put(hc->new_map);
250 dm_put(hc->md);
251 free_cell(hc);
254 static void dm_hash_remove_all(void)
256 int i;
257 struct hash_cell *hc;
258 struct list_head *tmp, *n;
260 down_write(&_hash_lock);
261 for (i = 0; i < NUM_BUCKETS; i++) {
262 list_for_each_safe (tmp, n, _name_buckets + i) {
263 hc = list_entry(tmp, struct hash_cell, name_list);
264 __hash_remove(hc);
267 up_write(&_hash_lock);
270 static int dm_hash_rename(const char *old, const char *new)
272 char *new_name, *old_name;
273 struct hash_cell *hc;
274 struct dm_table *table;
277 * duplicate new.
279 new_name = kstrdup(new, GFP_KERNEL);
280 if (!new_name)
281 return -ENOMEM;
283 down_write(&_hash_lock);
286 * Is new free ?
288 hc = __get_name_cell(new);
289 if (hc) {
290 DMWARN("asked to rename to an already existing name %s -> %s",
291 old, new);
292 up_write(&_hash_lock);
293 kfree(new_name);
294 return -EBUSY;
298 * Is there such a device as 'old' ?
300 hc = __get_name_cell(old);
301 if (!hc) {
302 DMWARN("asked to rename a non existent device %s -> %s",
303 old, new);
304 up_write(&_hash_lock);
305 kfree(new_name);
306 return -ENXIO;
310 * rename and move the name cell.
312 unregister_with_devfs(hc);
314 list_del(&hc->name_list);
315 old_name = hc->name;
316 hc->name = new_name;
317 list_add(&hc->name_list, _name_buckets + hash_str(new_name));
319 /* rename the device node in devfs */
320 register_with_devfs(hc);
323 * Wake up any dm event waiters.
325 table = dm_get_table(hc->md);
326 if (table) {
327 dm_table_event(table);
328 dm_table_put(table);
331 up_write(&_hash_lock);
332 kfree(old_name);
333 return 0;
336 /*-----------------------------------------------------------------
337 * Implementation of the ioctl commands
338 *---------------------------------------------------------------*/
340 * All the ioctl commands get dispatched to functions with this
341 * prototype.
343 typedef int (*ioctl_fn)(struct dm_ioctl *param, size_t param_size);
345 static int remove_all(struct dm_ioctl *param, size_t param_size)
347 dm_hash_remove_all();
348 param->data_size = 0;
349 return 0;
353 * Round up the ptr to an 8-byte boundary.
355 #define ALIGN_MASK 7
356 static inline void *align_ptr(void *ptr)
358 return (void *) (((size_t) (ptr + ALIGN_MASK)) & ~ALIGN_MASK);
362 * Retrieves the data payload buffer from an already allocated
363 * struct dm_ioctl.
365 static void *get_result_buffer(struct dm_ioctl *param, size_t param_size,
366 size_t *len)
368 param->data_start = align_ptr(param + 1) - (void *) param;
370 if (param->data_start < param_size)
371 *len = param_size - param->data_start;
372 else
373 *len = 0;
375 return ((void *) param) + param->data_start;
378 static int list_devices(struct dm_ioctl *param, size_t param_size)
380 unsigned int i;
381 struct hash_cell *hc;
382 size_t len, needed = 0;
383 struct gendisk *disk;
384 struct dm_name_list *nl, *old_nl = NULL;
386 down_write(&_hash_lock);
389 * Loop through all the devices working out how much
390 * space we need.
392 for (i = 0; i < NUM_BUCKETS; i++) {
393 list_for_each_entry (hc, _name_buckets + i, name_list) {
394 needed += sizeof(struct dm_name_list);
395 needed += strlen(hc->name) + 1;
396 needed += ALIGN_MASK;
401 * Grab our output buffer.
403 nl = get_result_buffer(param, param_size, &len);
404 if (len < needed) {
405 param->flags |= DM_BUFFER_FULL_FLAG;
406 goto out;
408 param->data_size = param->data_start + needed;
410 nl->dev = 0; /* Flags no data */
413 * Now loop through filling out the names.
415 for (i = 0; i < NUM_BUCKETS; i++) {
416 list_for_each_entry (hc, _name_buckets + i, name_list) {
417 if (old_nl)
418 old_nl->next = (uint32_t) ((void *) nl -
419 (void *) old_nl);
420 disk = dm_disk(hc->md);
421 nl->dev = huge_encode_dev(MKDEV(disk->major, disk->first_minor));
422 nl->next = 0;
423 strcpy(nl->name, hc->name);
425 old_nl = nl;
426 nl = align_ptr(((void *) ++nl) + strlen(hc->name) + 1);
430 out:
431 up_write(&_hash_lock);
432 return 0;
435 static void list_version_get_needed(struct target_type *tt, void *needed_param)
437 size_t *needed = needed_param;
439 *needed += sizeof(struct dm_target_versions);
440 *needed += strlen(tt->name);
441 *needed += ALIGN_MASK;
444 static void list_version_get_info(struct target_type *tt, void *param)
446 struct vers_iter *info = param;
448 /* Check space - it might have changed since the first iteration */
449 if ((char *)info->vers + sizeof(tt->version) + strlen(tt->name) + 1 >
450 info->end) {
452 info->flags = DM_BUFFER_FULL_FLAG;
453 return;
456 if (info->old_vers)
457 info->old_vers->next = (uint32_t) ((void *)info->vers -
458 (void *)info->old_vers);
459 info->vers->version[0] = tt->version[0];
460 info->vers->version[1] = tt->version[1];
461 info->vers->version[2] = tt->version[2];
462 info->vers->next = 0;
463 strcpy(info->vers->name, tt->name);
465 info->old_vers = info->vers;
466 info->vers = align_ptr(((void *) ++info->vers) + strlen(tt->name) + 1);
469 static int list_versions(struct dm_ioctl *param, size_t param_size)
471 size_t len, needed = 0;
472 struct dm_target_versions *vers;
473 struct vers_iter iter_info;
476 * Loop through all the devices working out how much
477 * space we need.
479 dm_target_iterate(list_version_get_needed, &needed);
482 * Grab our output buffer.
484 vers = get_result_buffer(param, param_size, &len);
485 if (len < needed) {
486 param->flags |= DM_BUFFER_FULL_FLAG;
487 goto out;
489 param->data_size = param->data_start + needed;
491 iter_info.param_size = param_size;
492 iter_info.old_vers = NULL;
493 iter_info.vers = vers;
494 iter_info.flags = 0;
495 iter_info.end = (char *)vers+len;
498 * Now loop through filling out the names & versions.
500 dm_target_iterate(list_version_get_info, &iter_info);
501 param->flags |= iter_info.flags;
503 out:
504 return 0;
509 static int check_name(const char *name)
511 if (strchr(name, '/')) {
512 DMWARN("invalid device name");
513 return -EINVAL;
516 return 0;
520 * Fills in a dm_ioctl structure, ready for sending back to
521 * userland.
523 static int __dev_status(struct mapped_device *md, struct dm_ioctl *param)
525 struct gendisk *disk = dm_disk(md);
526 struct dm_table *table;
527 struct block_device *bdev;
529 param->flags &= ~(DM_SUSPEND_FLAG | DM_READONLY_FLAG |
530 DM_ACTIVE_PRESENT_FLAG);
532 if (dm_suspended(md))
533 param->flags |= DM_SUSPEND_FLAG;
535 param->dev = huge_encode_dev(MKDEV(disk->major, disk->first_minor));
537 if (!(param->flags & DM_SKIP_BDGET_FLAG)) {
538 bdev = bdget_disk(disk, 0);
539 if (!bdev)
540 return -ENXIO;
543 * Yes, this will be out of date by the time it gets back
544 * to userland, but it is still very useful for
545 * debugging.
547 param->open_count = bdev->bd_openers;
548 bdput(bdev);
549 } else
550 param->open_count = -1;
552 if (disk->policy)
553 param->flags |= DM_READONLY_FLAG;
555 param->event_nr = dm_get_event_nr(md);
557 table = dm_get_table(md);
558 if (table) {
559 param->flags |= DM_ACTIVE_PRESENT_FLAG;
560 param->target_count = dm_table_get_num_targets(table);
561 dm_table_put(table);
562 } else
563 param->target_count = 0;
565 return 0;
568 static int dev_create(struct dm_ioctl *param, size_t param_size)
570 int r;
571 struct mapped_device *md;
573 r = check_name(param->name);
574 if (r)
575 return r;
577 if (param->flags & DM_PERSISTENT_DEV_FLAG)
578 r = dm_create_with_minor(MINOR(huge_decode_dev(param->dev)), &md);
579 else
580 r = dm_create(&md);
582 if (r)
583 return r;
585 r = dm_hash_insert(param->name, *param->uuid ? param->uuid : NULL, md);
586 if (r) {
587 dm_put(md);
588 return r;
591 param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
593 r = __dev_status(md, param);
594 dm_put(md);
596 return r;
600 * Always use UUID for lookups if it's present, otherwise use name or dev.
602 static struct hash_cell *__find_device_hash_cell(struct dm_ioctl *param)
604 struct mapped_device *md;
605 void *mdptr = NULL;
607 if (*param->uuid)
608 return __get_uuid_cell(param->uuid);
610 if (*param->name)
611 return __get_name_cell(param->name);
613 md = dm_get_md(huge_decode_dev(param->dev));
614 if (md) {
615 mdptr = dm_get_mdptr(md);
616 dm_put(md);
619 return mdptr;
622 static struct mapped_device *find_device(struct dm_ioctl *param)
624 struct hash_cell *hc;
625 struct mapped_device *md = NULL;
627 down_read(&_hash_lock);
628 hc = __find_device_hash_cell(param);
629 if (hc) {
630 md = hc->md;
631 dm_get(md);
634 * Sneakily write in both the name and the uuid
635 * while we have the cell.
637 strncpy(param->name, hc->name, sizeof(param->name));
638 if (hc->uuid)
639 strncpy(param->uuid, hc->uuid, sizeof(param->uuid)-1);
640 else
641 param->uuid[0] = '\0';
643 if (hc->new_map)
644 param->flags |= DM_INACTIVE_PRESENT_FLAG;
645 else
646 param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
648 up_read(&_hash_lock);
650 return md;
653 static int dev_remove(struct dm_ioctl *param, size_t param_size)
655 struct hash_cell *hc;
657 down_write(&_hash_lock);
658 hc = __find_device_hash_cell(param);
660 if (!hc) {
661 DMWARN("device doesn't appear to be in the dev hash table.");
662 up_write(&_hash_lock);
663 return -ENXIO;
666 __hash_remove(hc);
667 up_write(&_hash_lock);
668 param->data_size = 0;
669 return 0;
673 * Check a string doesn't overrun the chunk of
674 * memory we copied from userland.
676 static int invalid_str(char *str, void *end)
678 while ((void *) str < end)
679 if (!*str++)
680 return 0;
682 return -EINVAL;
685 static int dev_rename(struct dm_ioctl *param, size_t param_size)
687 int r;
688 char *new_name = (char *) param + param->data_start;
690 if (new_name < (char *) (param + 1) ||
691 invalid_str(new_name, (void *) param + param_size)) {
692 DMWARN("Invalid new logical volume name supplied.");
693 return -EINVAL;
696 r = check_name(new_name);
697 if (r)
698 return r;
700 param->data_size = 0;
701 return dm_hash_rename(param->name, new_name);
704 static int dev_set_geometry(struct dm_ioctl *param, size_t param_size)
706 int r = -EINVAL, x;
707 struct mapped_device *md;
708 struct hd_geometry geometry;
709 unsigned long indata[4];
710 char *geostr = (char *) param + param->data_start;
712 md = find_device(param);
713 if (!md)
714 return -ENXIO;
716 if (geostr < (char *) (param + 1) ||
717 invalid_str(geostr, (void *) param + param_size)) {
718 DMWARN("Invalid geometry supplied.");
719 goto out;
722 x = sscanf(geostr, "%lu %lu %lu %lu", indata,
723 indata + 1, indata + 2, indata + 3);
725 if (x != 4) {
726 DMWARN("Unable to interpret geometry settings.");
727 goto out;
730 if (indata[0] > 65535 || indata[1] > 255 ||
731 indata[2] > 255 || indata[3] > ULONG_MAX) {
732 DMWARN("Geometry exceeds range limits.");
733 goto out;
736 geometry.cylinders = indata[0];
737 geometry.heads = indata[1];
738 geometry.sectors = indata[2];
739 geometry.start = indata[3];
741 r = dm_set_geometry(md, &geometry);
742 if (!r)
743 r = __dev_status(md, param);
745 param->data_size = 0;
747 out:
748 dm_put(md);
749 return r;
752 static int do_suspend(struct dm_ioctl *param)
754 int r = 0;
755 int do_lockfs = 1;
756 struct mapped_device *md;
758 md = find_device(param);
759 if (!md)
760 return -ENXIO;
762 if (param->flags & DM_SKIP_LOCKFS_FLAG)
763 do_lockfs = 0;
765 if (!dm_suspended(md))
766 r = dm_suspend(md, do_lockfs);
768 if (!r)
769 r = __dev_status(md, param);
771 dm_put(md);
772 return r;
775 static int do_resume(struct dm_ioctl *param)
777 int r = 0;
778 int do_lockfs = 1;
779 struct hash_cell *hc;
780 struct mapped_device *md;
781 struct dm_table *new_map;
783 down_write(&_hash_lock);
785 hc = __find_device_hash_cell(param);
786 if (!hc) {
787 DMWARN("device doesn't appear to be in the dev hash table.");
788 up_write(&_hash_lock);
789 return -ENXIO;
792 md = hc->md;
793 dm_get(md);
795 new_map = hc->new_map;
796 hc->new_map = NULL;
797 param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
799 up_write(&_hash_lock);
801 /* Do we need to load a new map ? */
802 if (new_map) {
803 /* Suspend if it isn't already suspended */
804 if (param->flags & DM_SKIP_LOCKFS_FLAG)
805 do_lockfs = 0;
806 if (!dm_suspended(md))
807 dm_suspend(md, do_lockfs);
809 r = dm_swap_table(md, new_map);
810 if (r) {
811 dm_put(md);
812 dm_table_put(new_map);
813 return r;
816 if (dm_table_get_mode(new_map) & FMODE_WRITE)
817 set_disk_ro(dm_disk(md), 0);
818 else
819 set_disk_ro(dm_disk(md), 1);
821 dm_table_put(new_map);
824 if (dm_suspended(md))
825 r = dm_resume(md);
827 if (!r)
828 r = __dev_status(md, param);
830 dm_put(md);
831 return r;
835 * Set or unset the suspension state of a device.
836 * If the device already is in the requested state we just return its status.
838 static int dev_suspend(struct dm_ioctl *param, size_t param_size)
840 if (param->flags & DM_SUSPEND_FLAG)
841 return do_suspend(param);
843 return do_resume(param);
847 * Copies device info back to user space, used by
848 * the create and info ioctls.
850 static int dev_status(struct dm_ioctl *param, size_t param_size)
852 int r;
853 struct mapped_device *md;
855 md = find_device(param);
856 if (!md)
857 return -ENXIO;
859 r = __dev_status(md, param);
860 dm_put(md);
861 return r;
865 * Build up the status struct for each target
867 static void retrieve_status(struct dm_table *table,
868 struct dm_ioctl *param, size_t param_size)
870 unsigned int i, num_targets;
871 struct dm_target_spec *spec;
872 char *outbuf, *outptr;
873 status_type_t type;
874 size_t remaining, len, used = 0;
876 outptr = outbuf = get_result_buffer(param, param_size, &len);
878 if (param->flags & DM_STATUS_TABLE_FLAG)
879 type = STATUSTYPE_TABLE;
880 else
881 type = STATUSTYPE_INFO;
883 /* Get all the target info */
884 num_targets = dm_table_get_num_targets(table);
885 for (i = 0; i < num_targets; i++) {
886 struct dm_target *ti = dm_table_get_target(table, i);
888 remaining = len - (outptr - outbuf);
889 if (remaining <= sizeof(struct dm_target_spec)) {
890 param->flags |= DM_BUFFER_FULL_FLAG;
891 break;
894 spec = (struct dm_target_spec *) outptr;
896 spec->status = 0;
897 spec->sector_start = ti->begin;
898 spec->length = ti->len;
899 strncpy(spec->target_type, ti->type->name,
900 sizeof(spec->target_type));
902 outptr += sizeof(struct dm_target_spec);
903 remaining = len - (outptr - outbuf);
904 if (remaining <= 0) {
905 param->flags |= DM_BUFFER_FULL_FLAG;
906 break;
909 /* Get the status/table string from the target driver */
910 if (ti->type->status) {
911 if (ti->type->status(ti, type, outptr, remaining)) {
912 param->flags |= DM_BUFFER_FULL_FLAG;
913 break;
915 } else
916 outptr[0] = '\0';
918 outptr += strlen(outptr) + 1;
919 used = param->data_start + (outptr - outbuf);
921 outptr = align_ptr(outptr);
922 spec->next = outptr - outbuf;
925 if (used)
926 param->data_size = used;
928 param->target_count = num_targets;
932 * Wait for a device to report an event
934 static int dev_wait(struct dm_ioctl *param, size_t param_size)
936 int r;
937 struct mapped_device *md;
938 struct dm_table *table;
940 md = find_device(param);
941 if (!md)
942 return -ENXIO;
945 * Wait for a notification event
947 if (dm_wait_event(md, param->event_nr)) {
948 r = -ERESTARTSYS;
949 goto out;
953 * The userland program is going to want to know what
954 * changed to trigger the event, so we may as well tell
955 * him and save an ioctl.
957 r = __dev_status(md, param);
958 if (r)
959 goto out;
961 table = dm_get_table(md);
962 if (table) {
963 retrieve_status(table, param, param_size);
964 dm_table_put(table);
967 out:
968 dm_put(md);
969 return r;
972 static inline int get_mode(struct dm_ioctl *param)
974 int mode = FMODE_READ | FMODE_WRITE;
976 if (param->flags & DM_READONLY_FLAG)
977 mode = FMODE_READ;
979 return mode;
982 static int next_target(struct dm_target_spec *last, uint32_t next, void *end,
983 struct dm_target_spec **spec, char **target_params)
985 *spec = (struct dm_target_spec *) ((unsigned char *) last + next);
986 *target_params = (char *) (*spec + 1);
988 if (*spec < (last + 1))
989 return -EINVAL;
991 return invalid_str(*target_params, end);
994 static int populate_table(struct dm_table *table,
995 struct dm_ioctl *param, size_t param_size)
997 int r;
998 unsigned int i = 0;
999 struct dm_target_spec *spec = (struct dm_target_spec *) param;
1000 uint32_t next = param->data_start;
1001 void *end = (void *) param + param_size;
1002 char *target_params;
1004 if (!param->target_count) {
1005 DMWARN("populate_table: no targets specified");
1006 return -EINVAL;
1009 for (i = 0; i < param->target_count; i++) {
1011 r = next_target(spec, next, end, &spec, &target_params);
1012 if (r) {
1013 DMWARN("unable to find target");
1014 return r;
1017 r = dm_table_add_target(table, spec->target_type,
1018 (sector_t) spec->sector_start,
1019 (sector_t) spec->length,
1020 target_params);
1021 if (r) {
1022 DMWARN("error adding target to table");
1023 return r;
1026 next = spec->next;
1029 return dm_table_complete(table);
1032 static int table_load(struct dm_ioctl *param, size_t param_size)
1034 int r;
1035 struct hash_cell *hc;
1036 struct dm_table *t;
1037 struct mapped_device *md;
1039 md = find_device(param);
1040 if (!md)
1041 return -ENXIO;
1043 r = dm_table_create(&t, get_mode(param), param->target_count, md);
1044 if (r)
1045 goto out;
1047 r = populate_table(t, param, param_size);
1048 if (r) {
1049 dm_table_put(t);
1050 goto out;
1053 down_write(&_hash_lock);
1054 hc = dm_get_mdptr(md);
1055 if (!hc || hc->md != md) {
1056 DMWARN("device has been removed from the dev hash table.");
1057 dm_table_put(t);
1058 up_write(&_hash_lock);
1059 r = -ENXIO;
1060 goto out;
1063 if (hc->new_map)
1064 dm_table_put(hc->new_map);
1065 hc->new_map = t;
1066 up_write(&_hash_lock);
1068 param->flags |= DM_INACTIVE_PRESENT_FLAG;
1069 r = __dev_status(md, param);
1071 out:
1072 dm_put(md);
1074 return r;
1077 static int table_clear(struct dm_ioctl *param, size_t param_size)
1079 int r;
1080 struct hash_cell *hc;
1082 down_write(&_hash_lock);
1084 hc = __find_device_hash_cell(param);
1085 if (!hc) {
1086 DMWARN("device doesn't appear to be in the dev hash table.");
1087 up_write(&_hash_lock);
1088 return -ENXIO;
1091 if (hc->new_map) {
1092 dm_table_put(hc->new_map);
1093 hc->new_map = NULL;
1096 param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
1098 r = __dev_status(hc->md, param);
1099 up_write(&_hash_lock);
1100 return r;
1104 * Retrieves a list of devices used by a particular dm device.
1106 static void retrieve_deps(struct dm_table *table,
1107 struct dm_ioctl *param, size_t param_size)
1109 unsigned int count = 0;
1110 struct list_head *tmp;
1111 size_t len, needed;
1112 struct dm_dev *dd;
1113 struct dm_target_deps *deps;
1115 deps = get_result_buffer(param, param_size, &len);
1118 * Count the devices.
1120 list_for_each (tmp, dm_table_get_devices(table))
1121 count++;
1124 * Check we have enough space.
1126 needed = sizeof(*deps) + (sizeof(*deps->dev) * count);
1127 if (len < needed) {
1128 param->flags |= DM_BUFFER_FULL_FLAG;
1129 return;
1133 * Fill in the devices.
1135 deps->count = count;
1136 count = 0;
1137 list_for_each_entry (dd, dm_table_get_devices(table), list)
1138 deps->dev[count++] = huge_encode_dev(dd->bdev->bd_dev);
1140 param->data_size = param->data_start + needed;
1143 static int table_deps(struct dm_ioctl *param, size_t param_size)
1145 int r = 0;
1146 struct mapped_device *md;
1147 struct dm_table *table;
1149 md = find_device(param);
1150 if (!md)
1151 return -ENXIO;
1153 r = __dev_status(md, param);
1154 if (r)
1155 goto out;
1157 table = dm_get_table(md);
1158 if (table) {
1159 retrieve_deps(table, param, param_size);
1160 dm_table_put(table);
1163 out:
1164 dm_put(md);
1165 return r;
1169 * Return the status of a device as a text string for each
1170 * target.
1172 static int table_status(struct dm_ioctl *param, size_t param_size)
1174 int r;
1175 struct mapped_device *md;
1176 struct dm_table *table;
1178 md = find_device(param);
1179 if (!md)
1180 return -ENXIO;
1182 r = __dev_status(md, param);
1183 if (r)
1184 goto out;
1186 table = dm_get_table(md);
1187 if (table) {
1188 retrieve_status(table, param, param_size);
1189 dm_table_put(table);
1192 out:
1193 dm_put(md);
1194 return r;
1198 * Pass a message to the target that's at the supplied device offset.
1200 static int target_message(struct dm_ioctl *param, size_t param_size)
1202 int r, argc;
1203 char **argv;
1204 struct mapped_device *md;
1205 struct dm_table *table;
1206 struct dm_target *ti;
1207 struct dm_target_msg *tmsg = (void *) param + param->data_start;
1209 md = find_device(param);
1210 if (!md)
1211 return -ENXIO;
1213 r = __dev_status(md, param);
1214 if (r)
1215 goto out;
1217 if (tmsg < (struct dm_target_msg *) (param + 1) ||
1218 invalid_str(tmsg->message, (void *) param + param_size)) {
1219 DMWARN("Invalid target message parameters.");
1220 r = -EINVAL;
1221 goto out;
1224 r = dm_split_args(&argc, &argv, tmsg->message);
1225 if (r) {
1226 DMWARN("Failed to split target message parameters");
1227 goto out;
1230 table = dm_get_table(md);
1231 if (!table)
1232 goto out_argv;
1234 if (tmsg->sector >= dm_table_get_size(table)) {
1235 DMWARN("Target message sector outside device.");
1236 r = -EINVAL;
1237 goto out_table;
1240 ti = dm_table_find_target(table, tmsg->sector);
1241 if (ti->type->message)
1242 r = ti->type->message(ti, argc, argv);
1243 else {
1244 DMWARN("Target type does not support messages");
1245 r = -EINVAL;
1248 out_table:
1249 dm_table_put(table);
1250 out_argv:
1251 kfree(argv);
1252 out:
1253 param->data_size = 0;
1254 dm_put(md);
1255 return r;
1258 /*-----------------------------------------------------------------
1259 * Implementation of open/close/ioctl on the special char
1260 * device.
1261 *---------------------------------------------------------------*/
1262 static ioctl_fn lookup_ioctl(unsigned int cmd)
1264 static struct {
1265 int cmd;
1266 ioctl_fn fn;
1267 } _ioctls[] = {
1268 {DM_VERSION_CMD, NULL}, /* version is dealt with elsewhere */
1269 {DM_REMOVE_ALL_CMD, remove_all},
1270 {DM_LIST_DEVICES_CMD, list_devices},
1272 {DM_DEV_CREATE_CMD, dev_create},
1273 {DM_DEV_REMOVE_CMD, dev_remove},
1274 {DM_DEV_RENAME_CMD, dev_rename},
1275 {DM_DEV_SUSPEND_CMD, dev_suspend},
1276 {DM_DEV_STATUS_CMD, dev_status},
1277 {DM_DEV_WAIT_CMD, dev_wait},
1279 {DM_TABLE_LOAD_CMD, table_load},
1280 {DM_TABLE_CLEAR_CMD, table_clear},
1281 {DM_TABLE_DEPS_CMD, table_deps},
1282 {DM_TABLE_STATUS_CMD, table_status},
1284 {DM_LIST_VERSIONS_CMD, list_versions},
1286 {DM_TARGET_MSG_CMD, target_message},
1287 {DM_DEV_SET_GEOMETRY_CMD, dev_set_geometry}
1290 return (cmd >= ARRAY_SIZE(_ioctls)) ? NULL : _ioctls[cmd].fn;
1294 * As well as checking the version compatibility this always
1295 * copies the kernel interface version out.
1297 static int check_version(unsigned int cmd, struct dm_ioctl __user *user)
1299 uint32_t version[3];
1300 int r = 0;
1302 if (copy_from_user(version, user->version, sizeof(version)))
1303 return -EFAULT;
1305 if ((DM_VERSION_MAJOR != version[0]) ||
1306 (DM_VERSION_MINOR < version[1])) {
1307 DMWARN("ioctl interface mismatch: "
1308 "kernel(%u.%u.%u), user(%u.%u.%u), cmd(%d)",
1309 DM_VERSION_MAJOR, DM_VERSION_MINOR,
1310 DM_VERSION_PATCHLEVEL,
1311 version[0], version[1], version[2], cmd);
1312 r = -EINVAL;
1316 * Fill in the kernel version.
1318 version[0] = DM_VERSION_MAJOR;
1319 version[1] = DM_VERSION_MINOR;
1320 version[2] = DM_VERSION_PATCHLEVEL;
1321 if (copy_to_user(user->version, version, sizeof(version)))
1322 return -EFAULT;
1324 return r;
1327 static void free_params(struct dm_ioctl *param)
1329 vfree(param);
1332 static int copy_params(struct dm_ioctl __user *user, struct dm_ioctl **param)
1334 struct dm_ioctl tmp, *dmi;
1336 if (copy_from_user(&tmp, user, sizeof(tmp)))
1337 return -EFAULT;
1339 if (tmp.data_size < sizeof(tmp))
1340 return -EINVAL;
1342 dmi = (struct dm_ioctl *) vmalloc(tmp.data_size);
1343 if (!dmi)
1344 return -ENOMEM;
1346 if (copy_from_user(dmi, user, tmp.data_size)) {
1347 vfree(dmi);
1348 return -EFAULT;
1351 *param = dmi;
1352 return 0;
1355 static int validate_params(uint cmd, struct dm_ioctl *param)
1357 /* Always clear this flag */
1358 param->flags &= ~DM_BUFFER_FULL_FLAG;
1360 /* Ignores parameters */
1361 if (cmd == DM_REMOVE_ALL_CMD ||
1362 cmd == DM_LIST_DEVICES_CMD ||
1363 cmd == DM_LIST_VERSIONS_CMD)
1364 return 0;
1366 if ((cmd == DM_DEV_CREATE_CMD)) {
1367 if (!*param->name) {
1368 DMWARN("name not supplied when creating device");
1369 return -EINVAL;
1371 } else if ((*param->uuid && *param->name)) {
1372 DMWARN("only supply one of name or uuid, cmd(%u)", cmd);
1373 return -EINVAL;
1376 /* Ensure strings are terminated */
1377 param->name[DM_NAME_LEN - 1] = '\0';
1378 param->uuid[DM_UUID_LEN - 1] = '\0';
1380 return 0;
1383 static int ctl_ioctl(struct inode *inode, struct file *file,
1384 uint command, ulong u)
1386 int r = 0;
1387 unsigned int cmd;
1388 struct dm_ioctl *param;
1389 struct dm_ioctl __user *user = (struct dm_ioctl __user *) u;
1390 ioctl_fn fn = NULL;
1391 size_t param_size;
1393 /* only root can play with this */
1394 if (!capable(CAP_SYS_ADMIN))
1395 return -EACCES;
1397 if (_IOC_TYPE(command) != DM_IOCTL)
1398 return -ENOTTY;
1400 cmd = _IOC_NR(command);
1403 * Check the interface version passed in. This also
1404 * writes out the kernel's interface version.
1406 r = check_version(cmd, user);
1407 if (r)
1408 return r;
1411 * Nothing more to do for the version command.
1413 if (cmd == DM_VERSION_CMD)
1414 return 0;
1416 fn = lookup_ioctl(cmd);
1417 if (!fn) {
1418 DMWARN("dm_ctl_ioctl: unknown command 0x%x", command);
1419 return -ENOTTY;
1423 * Trying to avoid low memory issues when a device is
1424 * suspended.
1426 current->flags |= PF_MEMALLOC;
1429 * Copy the parameters into kernel space.
1431 r = copy_params(user, &param);
1433 current->flags &= ~PF_MEMALLOC;
1435 if (r)
1436 return r;
1438 r = validate_params(cmd, param);
1439 if (r)
1440 goto out;
1442 param_size = param->data_size;
1443 param->data_size = sizeof(*param);
1444 r = fn(param, param_size);
1447 * Copy the results back to userland.
1449 if (!r && copy_to_user(user, param, param->data_size))
1450 r = -EFAULT;
1452 out:
1453 free_params(param);
1454 return r;
1457 static struct file_operations _ctl_fops = {
1458 .ioctl = ctl_ioctl,
1459 .owner = THIS_MODULE,
1462 static struct miscdevice _dm_misc = {
1463 .minor = MISC_DYNAMIC_MINOR,
1464 .name = DM_NAME,
1465 .devfs_name = "mapper/control",
1466 .fops = &_ctl_fops
1470 * Create misc character device and link to DM_DIR/control.
1472 int __init dm_interface_init(void)
1474 int r;
1476 r = dm_hash_init();
1477 if (r)
1478 return r;
1480 r = misc_register(&_dm_misc);
1481 if (r) {
1482 DMERR("misc_register failed for control device");
1483 dm_hash_exit();
1484 return r;
1487 DMINFO("%d.%d.%d%s initialised: %s", DM_VERSION_MAJOR,
1488 DM_VERSION_MINOR, DM_VERSION_PATCHLEVEL, DM_VERSION_EXTRA,
1489 DM_DRIVER_EMAIL);
1490 return 0;
1493 void dm_interface_exit(void)
1495 if (misc_deregister(&_dm_misc) < 0)
1496 DMERR("misc_deregister failed for control device");
1498 dm_hash_exit();