thinkpad-acpi: fix ALSA callback return status
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / md / dm-ioctl.c
blobc4819b6b77b2b64444e6855402d77d23aebb4c70
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/dm-ioctl.h>
17 #include <linux/hdreg.h>
18 #include <linux/compat.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 return 0;
74 static void dm_hash_exit(void)
76 dm_hash_remove_all(0);
79 /*-----------------------------------------------------------------
80 * Hash function:
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;
87 unsigned int h = 0;
89 while (*str)
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)) {
105 dm_get(hc->md);
106 return hc;
109 return NULL;
112 static struct hash_cell *__get_uuid_cell(const char *str)
114 struct hash_cell *hc;
115 unsigned int h = hash_str(str);
117 list_for_each_entry (hc, _uuid_buckets + h, uuid_list)
118 if (!strcmp(hc->uuid, str)) {
119 dm_get(hc->md);
120 return hc;
123 return NULL;
126 /*-----------------------------------------------------------------
127 * Inserting, removing and renaming a device.
128 *---------------------------------------------------------------*/
129 static struct hash_cell *alloc_cell(const char *name, const char *uuid,
130 struct mapped_device *md)
132 struct hash_cell *hc;
134 hc = kmalloc(sizeof(*hc), GFP_KERNEL);
135 if (!hc)
136 return NULL;
138 hc->name = kstrdup(name, GFP_KERNEL);
139 if (!hc->name) {
140 kfree(hc);
141 return NULL;
144 if (!uuid)
145 hc->uuid = NULL;
147 else {
148 hc->uuid = kstrdup(uuid, GFP_KERNEL);
149 if (!hc->uuid) {
150 kfree(hc->name);
151 kfree(hc);
152 return NULL;
156 INIT_LIST_HEAD(&hc->name_list);
157 INIT_LIST_HEAD(&hc->uuid_list);
158 hc->md = md;
159 hc->new_map = NULL;
160 return hc;
163 static void free_cell(struct hash_cell *hc)
165 if (hc) {
166 kfree(hc->name);
167 kfree(hc->uuid);
168 kfree(hc);
173 * The kdev_t and uuid of a device can never change once it is
174 * initially inserted.
176 static int dm_hash_insert(const char *name, const char *uuid, struct mapped_device *md)
178 struct hash_cell *cell, *hc;
181 * Allocate the new cells.
183 cell = alloc_cell(name, uuid, md);
184 if (!cell)
185 return -ENOMEM;
188 * Insert the cell into both hash tables.
190 down_write(&_hash_lock);
191 hc = __get_name_cell(name);
192 if (hc) {
193 dm_put(hc->md);
194 goto bad;
197 list_add(&cell->name_list, _name_buckets + hash_str(name));
199 if (uuid) {
200 hc = __get_uuid_cell(uuid);
201 if (hc) {
202 list_del(&cell->name_list);
203 dm_put(hc->md);
204 goto bad;
206 list_add(&cell->uuid_list, _uuid_buckets + hash_str(uuid));
208 dm_get(md);
209 dm_set_mdptr(md, cell);
210 up_write(&_hash_lock);
212 return 0;
214 bad:
215 up_write(&_hash_lock);
216 free_cell(cell);
217 return -EBUSY;
220 static void __hash_remove(struct hash_cell *hc)
222 struct dm_table *table;
224 /* remove from the dev hash */
225 list_del(&hc->uuid_list);
226 list_del(&hc->name_list);
227 dm_set_mdptr(hc->md, NULL);
229 table = dm_get_table(hc->md);
230 if (table) {
231 dm_table_event(table);
232 dm_table_put(table);
235 if (hc->new_map)
236 dm_table_put(hc->new_map);
237 dm_put(hc->md);
238 free_cell(hc);
241 static void dm_hash_remove_all(int keep_open_devices)
243 int i, dev_skipped, dev_removed;
244 struct hash_cell *hc;
245 struct list_head *tmp, *n;
247 down_write(&_hash_lock);
249 retry:
250 dev_skipped = dev_removed = 0;
251 for (i = 0; i < NUM_BUCKETS; i++) {
252 list_for_each_safe (tmp, n, _name_buckets + i) {
253 hc = list_entry(tmp, struct hash_cell, name_list);
255 if (keep_open_devices &&
256 dm_lock_for_deletion(hc->md)) {
257 dev_skipped++;
258 continue;
260 __hash_remove(hc);
261 dev_removed = 1;
266 * Some mapped devices may be using other mapped devices, so if any
267 * still exist, repeat until we make no further progress.
269 if (dev_skipped) {
270 if (dev_removed)
271 goto retry;
273 DMWARN("remove_all left %d open device(s)", dev_skipped);
276 up_write(&_hash_lock);
279 static int dm_hash_rename(const char *old, const char *new)
281 char *new_name, *old_name;
282 struct hash_cell *hc;
283 struct dm_table *table;
286 * duplicate new.
288 new_name = kstrdup(new, GFP_KERNEL);
289 if (!new_name)
290 return -ENOMEM;
292 down_write(&_hash_lock);
295 * Is new free ?
297 hc = __get_name_cell(new);
298 if (hc) {
299 DMWARN("asked to rename to an already existing name %s -> %s",
300 old, new);
301 dm_put(hc->md);
302 up_write(&_hash_lock);
303 kfree(new_name);
304 return -EBUSY;
308 * Is there such a device as 'old' ?
310 hc = __get_name_cell(old);
311 if (!hc) {
312 DMWARN("asked to rename a non existent device %s -> %s",
313 old, new);
314 up_write(&_hash_lock);
315 kfree(new_name);
316 return -ENXIO;
320 * rename and move the name cell.
322 list_del(&hc->name_list);
323 old_name = hc->name;
324 hc->name = new_name;
325 list_add(&hc->name_list, _name_buckets + hash_str(new_name));
328 * Wake up any dm event waiters.
330 table = dm_get_table(hc->md);
331 if (table) {
332 dm_table_event(table);
333 dm_table_put(table);
336 dm_kobject_uevent(hc->md);
338 dm_put(hc->md);
339 up_write(&_hash_lock);
340 kfree(old_name);
341 return 0;
344 /*-----------------------------------------------------------------
345 * Implementation of the ioctl commands
346 *---------------------------------------------------------------*/
348 * All the ioctl commands get dispatched to functions with this
349 * prototype.
351 typedef int (*ioctl_fn)(struct dm_ioctl *param, size_t param_size);
353 static int remove_all(struct dm_ioctl *param, size_t param_size)
355 dm_hash_remove_all(1);
356 param->data_size = 0;
357 return 0;
361 * Round up the ptr to an 8-byte boundary.
363 #define ALIGN_MASK 7
364 static inline void *align_ptr(void *ptr)
366 return (void *) (((size_t) (ptr + ALIGN_MASK)) & ~ALIGN_MASK);
370 * Retrieves the data payload buffer from an already allocated
371 * struct dm_ioctl.
373 static void *get_result_buffer(struct dm_ioctl *param, size_t param_size,
374 size_t *len)
376 param->data_start = align_ptr(param + 1) - (void *) param;
378 if (param->data_start < param_size)
379 *len = param_size - param->data_start;
380 else
381 *len = 0;
383 return ((void *) param) + param->data_start;
386 static int list_devices(struct dm_ioctl *param, size_t param_size)
388 unsigned int i;
389 struct hash_cell *hc;
390 size_t len, needed = 0;
391 struct gendisk *disk;
392 struct dm_name_list *nl, *old_nl = NULL;
394 down_write(&_hash_lock);
397 * Loop through all the devices working out how much
398 * space we need.
400 for (i = 0; i < NUM_BUCKETS; i++) {
401 list_for_each_entry (hc, _name_buckets + i, name_list) {
402 needed += sizeof(struct dm_name_list);
403 needed += strlen(hc->name) + 1;
404 needed += ALIGN_MASK;
409 * Grab our output buffer.
411 nl = get_result_buffer(param, param_size, &len);
412 if (len < needed) {
413 param->flags |= DM_BUFFER_FULL_FLAG;
414 goto out;
416 param->data_size = param->data_start + needed;
418 nl->dev = 0; /* Flags no data */
421 * Now loop through filling out the names.
423 for (i = 0; i < NUM_BUCKETS; i++) {
424 list_for_each_entry (hc, _name_buckets + i, name_list) {
425 if (old_nl)
426 old_nl->next = (uint32_t) ((void *) nl -
427 (void *) old_nl);
428 disk = dm_disk(hc->md);
429 nl->dev = huge_encode_dev(disk_devt(disk));
430 nl->next = 0;
431 strcpy(nl->name, hc->name);
433 old_nl = nl;
434 nl = align_ptr(((void *) ++nl) + strlen(hc->name) + 1);
438 out:
439 up_write(&_hash_lock);
440 return 0;
443 static void list_version_get_needed(struct target_type *tt, void *needed_param)
445 size_t *needed = needed_param;
447 *needed += sizeof(struct dm_target_versions);
448 *needed += strlen(tt->name);
449 *needed += ALIGN_MASK;
452 static void list_version_get_info(struct target_type *tt, void *param)
454 struct vers_iter *info = param;
456 /* Check space - it might have changed since the first iteration */
457 if ((char *)info->vers + sizeof(tt->version) + strlen(tt->name) + 1 >
458 info->end) {
460 info->flags = DM_BUFFER_FULL_FLAG;
461 return;
464 if (info->old_vers)
465 info->old_vers->next = (uint32_t) ((void *)info->vers -
466 (void *)info->old_vers);
467 info->vers->version[0] = tt->version[0];
468 info->vers->version[1] = tt->version[1];
469 info->vers->version[2] = tt->version[2];
470 info->vers->next = 0;
471 strcpy(info->vers->name, tt->name);
473 info->old_vers = info->vers;
474 info->vers = align_ptr(((void *) ++info->vers) + strlen(tt->name) + 1);
477 static int list_versions(struct dm_ioctl *param, size_t param_size)
479 size_t len, needed = 0;
480 struct dm_target_versions *vers;
481 struct vers_iter iter_info;
484 * Loop through all the devices working out how much
485 * space we need.
487 dm_target_iterate(list_version_get_needed, &needed);
490 * Grab our output buffer.
492 vers = get_result_buffer(param, param_size, &len);
493 if (len < needed) {
494 param->flags |= DM_BUFFER_FULL_FLAG;
495 goto out;
497 param->data_size = param->data_start + needed;
499 iter_info.param_size = param_size;
500 iter_info.old_vers = NULL;
501 iter_info.vers = vers;
502 iter_info.flags = 0;
503 iter_info.end = (char *)vers+len;
506 * Now loop through filling out the names & versions.
508 dm_target_iterate(list_version_get_info, &iter_info);
509 param->flags |= iter_info.flags;
511 out:
512 return 0;
517 static int check_name(const char *name)
519 if (strchr(name, '/')) {
520 DMWARN("invalid device name");
521 return -EINVAL;
524 return 0;
528 * Fills in a dm_ioctl structure, ready for sending back to
529 * userland.
531 static int __dev_status(struct mapped_device *md, struct dm_ioctl *param)
533 struct gendisk *disk = dm_disk(md);
534 struct dm_table *table;
536 param->flags &= ~(DM_SUSPEND_FLAG | DM_READONLY_FLAG |
537 DM_ACTIVE_PRESENT_FLAG);
539 if (dm_suspended(md))
540 param->flags |= DM_SUSPEND_FLAG;
542 param->dev = huge_encode_dev(disk_devt(disk));
545 * Yes, this will be out of date by the time it gets back
546 * to userland, but it is still very useful for
547 * debugging.
549 param->open_count = dm_open_count(md);
551 if (get_disk_ro(disk))
552 param->flags |= DM_READONLY_FLAG;
554 param->event_nr = dm_get_event_nr(md);
556 table = dm_get_table(md);
557 if (table) {
558 param->flags |= DM_ACTIVE_PRESENT_FLAG;
559 param->target_count = dm_table_get_num_targets(table);
560 dm_table_put(table);
561 } else
562 param->target_count = 0;
564 return 0;
567 static int dev_create(struct dm_ioctl *param, size_t param_size)
569 int r, m = DM_ANY_MINOR;
570 struct mapped_device *md;
572 r = check_name(param->name);
573 if (r)
574 return r;
576 if (param->flags & DM_PERSISTENT_DEV_FLAG)
577 m = MINOR(huge_decode_dev(param->dev));
579 r = dm_create(m, &md);
580 if (r)
581 return r;
583 r = dm_hash_insert(param->name, *param->uuid ? param->uuid : NULL, md);
584 if (r) {
585 dm_put(md);
586 return r;
589 param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
591 r = __dev_status(md, param);
592 dm_put(md);
594 return r;
598 * Always use UUID for lookups if it's present, otherwise use name or dev.
600 static struct hash_cell *__find_device_hash_cell(struct dm_ioctl *param)
602 struct mapped_device *md;
603 void *mdptr = NULL;
605 if (*param->uuid)
606 return __get_uuid_cell(param->uuid);
608 if (*param->name)
609 return __get_name_cell(param->name);
611 md = dm_get_md(huge_decode_dev(param->dev));
612 if (!md)
613 goto out;
615 mdptr = dm_get_mdptr(md);
616 if (!mdptr)
617 dm_put(md);
619 out:
620 return mdptr;
623 static struct mapped_device *find_device(struct dm_ioctl *param)
625 struct hash_cell *hc;
626 struct mapped_device *md = NULL;
628 down_read(&_hash_lock);
629 hc = __find_device_hash_cell(param);
630 if (hc) {
631 md = hc->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;
656 struct mapped_device *md;
657 int r;
659 down_write(&_hash_lock);
660 hc = __find_device_hash_cell(param);
662 if (!hc) {
663 DMWARN("device doesn't appear to be in the dev hash table.");
664 up_write(&_hash_lock);
665 return -ENXIO;
668 md = hc->md;
671 * Ensure the device is not open and nothing further can open it.
673 r = dm_lock_for_deletion(md);
674 if (r) {
675 DMWARN("unable to remove open device %s", hc->name);
676 up_write(&_hash_lock);
677 dm_put(md);
678 return r;
681 __hash_remove(hc);
682 up_write(&_hash_lock);
683 dm_put(md);
684 param->data_size = 0;
685 return 0;
689 * Check a string doesn't overrun the chunk of
690 * memory we copied from userland.
692 static int invalid_str(char *str, void *end)
694 while ((void *) str < end)
695 if (!*str++)
696 return 0;
698 return -EINVAL;
701 static int dev_rename(struct dm_ioctl *param, size_t param_size)
703 int r;
704 char *new_name = (char *) param + param->data_start;
706 if (new_name < param->data ||
707 invalid_str(new_name, (void *) param + param_size) ||
708 strlen(new_name) > DM_NAME_LEN - 1) {
709 DMWARN("Invalid new logical volume name supplied.");
710 return -EINVAL;
713 r = check_name(new_name);
714 if (r)
715 return r;
717 param->data_size = 0;
718 return dm_hash_rename(param->name, new_name);
721 static int dev_set_geometry(struct dm_ioctl *param, size_t param_size)
723 int r = -EINVAL, x;
724 struct mapped_device *md;
725 struct hd_geometry geometry;
726 unsigned long indata[4];
727 char *geostr = (char *) param + param->data_start;
729 md = find_device(param);
730 if (!md)
731 return -ENXIO;
733 if (geostr < param->data ||
734 invalid_str(geostr, (void *) param + param_size)) {
735 DMWARN("Invalid geometry supplied.");
736 goto out;
739 x = sscanf(geostr, "%lu %lu %lu %lu", indata,
740 indata + 1, indata + 2, indata + 3);
742 if (x != 4) {
743 DMWARN("Unable to interpret geometry settings.");
744 goto out;
747 if (indata[0] > 65535 || indata[1] > 255 ||
748 indata[2] > 255 || indata[3] > ULONG_MAX) {
749 DMWARN("Geometry exceeds range limits.");
750 goto out;
753 geometry.cylinders = indata[0];
754 geometry.heads = indata[1];
755 geometry.sectors = indata[2];
756 geometry.start = indata[3];
758 r = dm_set_geometry(md, &geometry);
759 if (!r)
760 r = __dev_status(md, param);
762 param->data_size = 0;
764 out:
765 dm_put(md);
766 return r;
769 static int do_suspend(struct dm_ioctl *param)
771 int r = 0;
772 unsigned suspend_flags = DM_SUSPEND_LOCKFS_FLAG;
773 struct mapped_device *md;
775 md = find_device(param);
776 if (!md)
777 return -ENXIO;
779 if (param->flags & DM_SKIP_LOCKFS_FLAG)
780 suspend_flags &= ~DM_SUSPEND_LOCKFS_FLAG;
781 if (param->flags & DM_NOFLUSH_FLAG)
782 suspend_flags |= DM_SUSPEND_NOFLUSH_FLAG;
784 if (!dm_suspended(md))
785 r = dm_suspend(md, suspend_flags);
787 if (!r)
788 r = __dev_status(md, param);
790 dm_put(md);
791 return r;
794 static int do_resume(struct dm_ioctl *param)
796 int r = 0;
797 unsigned suspend_flags = DM_SUSPEND_LOCKFS_FLAG;
798 struct hash_cell *hc;
799 struct mapped_device *md;
800 struct dm_table *new_map;
802 down_write(&_hash_lock);
804 hc = __find_device_hash_cell(param);
805 if (!hc) {
806 DMWARN("device doesn't appear to be in the dev hash table.");
807 up_write(&_hash_lock);
808 return -ENXIO;
811 md = hc->md;
813 new_map = hc->new_map;
814 hc->new_map = NULL;
815 param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
817 up_write(&_hash_lock);
819 /* Do we need to load a new map ? */
820 if (new_map) {
821 /* Suspend if it isn't already suspended */
822 if (param->flags & DM_SKIP_LOCKFS_FLAG)
823 suspend_flags &= ~DM_SUSPEND_LOCKFS_FLAG;
824 if (param->flags & DM_NOFLUSH_FLAG)
825 suspend_flags |= DM_SUSPEND_NOFLUSH_FLAG;
826 if (!dm_suspended(md))
827 dm_suspend(md, suspend_flags);
829 r = dm_swap_table(md, new_map);
830 if (r) {
831 dm_put(md);
832 dm_table_put(new_map);
833 return r;
836 if (dm_table_get_mode(new_map) & FMODE_WRITE)
837 set_disk_ro(dm_disk(md), 0);
838 else
839 set_disk_ro(dm_disk(md), 1);
841 dm_table_put(new_map);
844 if (dm_suspended(md))
845 r = dm_resume(md);
847 if (!r)
848 r = __dev_status(md, param);
850 dm_put(md);
851 return r;
855 * Set or unset the suspension state of a device.
856 * If the device already is in the requested state we just return its status.
858 static int dev_suspend(struct dm_ioctl *param, size_t param_size)
860 if (param->flags & DM_SUSPEND_FLAG)
861 return do_suspend(param);
863 return do_resume(param);
867 * Copies device info back to user space, used by
868 * the create and info ioctls.
870 static int dev_status(struct dm_ioctl *param, size_t param_size)
872 int r;
873 struct mapped_device *md;
875 md = find_device(param);
876 if (!md)
877 return -ENXIO;
879 r = __dev_status(md, param);
880 dm_put(md);
881 return r;
885 * Build up the status struct for each target
887 static void retrieve_status(struct dm_table *table,
888 struct dm_ioctl *param, size_t param_size)
890 unsigned int i, num_targets;
891 struct dm_target_spec *spec;
892 char *outbuf, *outptr;
893 status_type_t type;
894 size_t remaining, len, used = 0;
896 outptr = outbuf = get_result_buffer(param, param_size, &len);
898 if (param->flags & DM_STATUS_TABLE_FLAG)
899 type = STATUSTYPE_TABLE;
900 else
901 type = STATUSTYPE_INFO;
903 /* Get all the target info */
904 num_targets = dm_table_get_num_targets(table);
905 for (i = 0; i < num_targets; i++) {
906 struct dm_target *ti = dm_table_get_target(table, i);
908 remaining = len - (outptr - outbuf);
909 if (remaining <= sizeof(struct dm_target_spec)) {
910 param->flags |= DM_BUFFER_FULL_FLAG;
911 break;
914 spec = (struct dm_target_spec *) outptr;
916 spec->status = 0;
917 spec->sector_start = ti->begin;
918 spec->length = ti->len;
919 strncpy(spec->target_type, ti->type->name,
920 sizeof(spec->target_type));
922 outptr += sizeof(struct dm_target_spec);
923 remaining = len - (outptr - outbuf);
924 if (remaining <= 0) {
925 param->flags |= DM_BUFFER_FULL_FLAG;
926 break;
929 /* Get the status/table string from the target driver */
930 if (ti->type->status) {
931 if (ti->type->status(ti, type, outptr, remaining)) {
932 param->flags |= DM_BUFFER_FULL_FLAG;
933 break;
935 } else
936 outptr[0] = '\0';
938 outptr += strlen(outptr) + 1;
939 used = param->data_start + (outptr - outbuf);
941 outptr = align_ptr(outptr);
942 spec->next = outptr - outbuf;
945 if (used)
946 param->data_size = used;
948 param->target_count = num_targets;
952 * Wait for a device to report an event
954 static int dev_wait(struct dm_ioctl *param, size_t param_size)
956 int r;
957 struct mapped_device *md;
958 struct dm_table *table;
960 md = find_device(param);
961 if (!md)
962 return -ENXIO;
965 * Wait for a notification event
967 if (dm_wait_event(md, param->event_nr)) {
968 r = -ERESTARTSYS;
969 goto out;
973 * The userland program is going to want to know what
974 * changed to trigger the event, so we may as well tell
975 * him and save an ioctl.
977 r = __dev_status(md, param);
978 if (r)
979 goto out;
981 table = dm_get_table(md);
982 if (table) {
983 retrieve_status(table, param, param_size);
984 dm_table_put(table);
987 out:
988 dm_put(md);
989 return r;
992 static inline fmode_t get_mode(struct dm_ioctl *param)
994 fmode_t mode = FMODE_READ | FMODE_WRITE;
996 if (param->flags & DM_READONLY_FLAG)
997 mode = FMODE_READ;
999 return mode;
1002 static int next_target(struct dm_target_spec *last, uint32_t next, void *end,
1003 struct dm_target_spec **spec, char **target_params)
1005 *spec = (struct dm_target_spec *) ((unsigned char *) last + next);
1006 *target_params = (char *) (*spec + 1);
1008 if (*spec < (last + 1))
1009 return -EINVAL;
1011 return invalid_str(*target_params, end);
1014 static int populate_table(struct dm_table *table,
1015 struct dm_ioctl *param, size_t param_size)
1017 int r;
1018 unsigned int i = 0;
1019 struct dm_target_spec *spec = (struct dm_target_spec *) param;
1020 uint32_t next = param->data_start;
1021 void *end = (void *) param + param_size;
1022 char *target_params;
1024 if (!param->target_count) {
1025 DMWARN("populate_table: no targets specified");
1026 return -EINVAL;
1029 for (i = 0; i < param->target_count; i++) {
1031 r = next_target(spec, next, end, &spec, &target_params);
1032 if (r) {
1033 DMWARN("unable to find target");
1034 return r;
1037 r = dm_table_add_target(table, spec->target_type,
1038 (sector_t) spec->sector_start,
1039 (sector_t) spec->length,
1040 target_params);
1041 if (r) {
1042 DMWARN("error adding target to table");
1043 return r;
1046 next = spec->next;
1049 return dm_table_complete(table);
1052 static int table_load(struct dm_ioctl *param, size_t param_size)
1054 int r;
1055 struct hash_cell *hc;
1056 struct dm_table *t;
1057 struct mapped_device *md;
1059 md = find_device(param);
1060 if (!md)
1061 return -ENXIO;
1063 r = dm_table_create(&t, get_mode(param), param->target_count, md);
1064 if (r)
1065 goto out;
1067 r = populate_table(t, param, param_size);
1068 if (r) {
1069 dm_table_put(t);
1070 goto out;
1073 down_write(&_hash_lock);
1074 hc = dm_get_mdptr(md);
1075 if (!hc || hc->md != md) {
1076 DMWARN("device has been removed from the dev hash table.");
1077 dm_table_put(t);
1078 up_write(&_hash_lock);
1079 r = -ENXIO;
1080 goto out;
1083 if (hc->new_map)
1084 dm_table_put(hc->new_map);
1085 hc->new_map = t;
1086 up_write(&_hash_lock);
1088 param->flags |= DM_INACTIVE_PRESENT_FLAG;
1089 r = __dev_status(md, param);
1091 out:
1092 dm_put(md);
1094 return r;
1097 static int table_clear(struct dm_ioctl *param, size_t param_size)
1099 int r;
1100 struct hash_cell *hc;
1101 struct mapped_device *md;
1103 down_write(&_hash_lock);
1105 hc = __find_device_hash_cell(param);
1106 if (!hc) {
1107 DMWARN("device doesn't appear to be in the dev hash table.");
1108 up_write(&_hash_lock);
1109 return -ENXIO;
1112 if (hc->new_map) {
1113 dm_table_put(hc->new_map);
1114 hc->new_map = NULL;
1117 param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
1119 r = __dev_status(hc->md, param);
1120 md = hc->md;
1121 up_write(&_hash_lock);
1122 dm_put(md);
1123 return r;
1127 * Retrieves a list of devices used by a particular dm device.
1129 static void retrieve_deps(struct dm_table *table,
1130 struct dm_ioctl *param, size_t param_size)
1132 unsigned int count = 0;
1133 struct list_head *tmp;
1134 size_t len, needed;
1135 struct dm_dev_internal *dd;
1136 struct dm_target_deps *deps;
1138 deps = get_result_buffer(param, param_size, &len);
1141 * Count the devices.
1143 list_for_each (tmp, dm_table_get_devices(table))
1144 count++;
1147 * Check we have enough space.
1149 needed = sizeof(*deps) + (sizeof(*deps->dev) * count);
1150 if (len < needed) {
1151 param->flags |= DM_BUFFER_FULL_FLAG;
1152 return;
1156 * Fill in the devices.
1158 deps->count = count;
1159 count = 0;
1160 list_for_each_entry (dd, dm_table_get_devices(table), list)
1161 deps->dev[count++] = huge_encode_dev(dd->dm_dev.bdev->bd_dev);
1163 param->data_size = param->data_start + needed;
1166 static int table_deps(struct dm_ioctl *param, size_t param_size)
1168 int r = 0;
1169 struct mapped_device *md;
1170 struct dm_table *table;
1172 md = find_device(param);
1173 if (!md)
1174 return -ENXIO;
1176 r = __dev_status(md, param);
1177 if (r)
1178 goto out;
1180 table = dm_get_table(md);
1181 if (table) {
1182 retrieve_deps(table, param, param_size);
1183 dm_table_put(table);
1186 out:
1187 dm_put(md);
1188 return r;
1192 * Return the status of a device as a text string for each
1193 * target.
1195 static int table_status(struct dm_ioctl *param, size_t param_size)
1197 int r;
1198 struct mapped_device *md;
1199 struct dm_table *table;
1201 md = find_device(param);
1202 if (!md)
1203 return -ENXIO;
1205 r = __dev_status(md, param);
1206 if (r)
1207 goto out;
1209 table = dm_get_table(md);
1210 if (table) {
1211 retrieve_status(table, param, param_size);
1212 dm_table_put(table);
1215 out:
1216 dm_put(md);
1217 return r;
1221 * Pass a message to the target that's at the supplied device offset.
1223 static int target_message(struct dm_ioctl *param, size_t param_size)
1225 int r, argc;
1226 char **argv;
1227 struct mapped_device *md;
1228 struct dm_table *table;
1229 struct dm_target *ti;
1230 struct dm_target_msg *tmsg = (void *) param + param->data_start;
1232 md = find_device(param);
1233 if (!md)
1234 return -ENXIO;
1236 r = __dev_status(md, param);
1237 if (r)
1238 goto out;
1240 if (tmsg < (struct dm_target_msg *) param->data ||
1241 invalid_str(tmsg->message, (void *) param + param_size)) {
1242 DMWARN("Invalid target message parameters.");
1243 r = -EINVAL;
1244 goto out;
1247 r = dm_split_args(&argc, &argv, tmsg->message);
1248 if (r) {
1249 DMWARN("Failed to split target message parameters");
1250 goto out;
1253 table = dm_get_table(md);
1254 if (!table)
1255 goto out_argv;
1257 ti = dm_table_find_target(table, tmsg->sector);
1258 if (!dm_target_is_valid(ti)) {
1259 DMWARN("Target message sector outside device.");
1260 r = -EINVAL;
1261 } else if (ti->type->message)
1262 r = ti->type->message(ti, argc, argv);
1263 else {
1264 DMWARN("Target type does not support messages");
1265 r = -EINVAL;
1268 dm_table_put(table);
1269 out_argv:
1270 kfree(argv);
1271 out:
1272 param->data_size = 0;
1273 dm_put(md);
1274 return r;
1277 /*-----------------------------------------------------------------
1278 * Implementation of open/close/ioctl on the special char
1279 * device.
1280 *---------------------------------------------------------------*/
1281 static ioctl_fn lookup_ioctl(unsigned int cmd)
1283 static struct {
1284 int cmd;
1285 ioctl_fn fn;
1286 } _ioctls[] = {
1287 {DM_VERSION_CMD, NULL}, /* version is dealt with elsewhere */
1288 {DM_REMOVE_ALL_CMD, remove_all},
1289 {DM_LIST_DEVICES_CMD, list_devices},
1291 {DM_DEV_CREATE_CMD, dev_create},
1292 {DM_DEV_REMOVE_CMD, dev_remove},
1293 {DM_DEV_RENAME_CMD, dev_rename},
1294 {DM_DEV_SUSPEND_CMD, dev_suspend},
1295 {DM_DEV_STATUS_CMD, dev_status},
1296 {DM_DEV_WAIT_CMD, dev_wait},
1298 {DM_TABLE_LOAD_CMD, table_load},
1299 {DM_TABLE_CLEAR_CMD, table_clear},
1300 {DM_TABLE_DEPS_CMD, table_deps},
1301 {DM_TABLE_STATUS_CMD, table_status},
1303 {DM_LIST_VERSIONS_CMD, list_versions},
1305 {DM_TARGET_MSG_CMD, target_message},
1306 {DM_DEV_SET_GEOMETRY_CMD, dev_set_geometry}
1309 return (cmd >= ARRAY_SIZE(_ioctls)) ? NULL : _ioctls[cmd].fn;
1313 * As well as checking the version compatibility this always
1314 * copies the kernel interface version out.
1316 static int check_version(unsigned int cmd, struct dm_ioctl __user *user)
1318 uint32_t version[3];
1319 int r = 0;
1321 if (copy_from_user(version, user->version, sizeof(version)))
1322 return -EFAULT;
1324 if ((DM_VERSION_MAJOR != version[0]) ||
1325 (DM_VERSION_MINOR < version[1])) {
1326 DMWARN("ioctl interface mismatch: "
1327 "kernel(%u.%u.%u), user(%u.%u.%u), cmd(%d)",
1328 DM_VERSION_MAJOR, DM_VERSION_MINOR,
1329 DM_VERSION_PATCHLEVEL,
1330 version[0], version[1], version[2], cmd);
1331 r = -EINVAL;
1335 * Fill in the kernel version.
1337 version[0] = DM_VERSION_MAJOR;
1338 version[1] = DM_VERSION_MINOR;
1339 version[2] = DM_VERSION_PATCHLEVEL;
1340 if (copy_to_user(user->version, version, sizeof(version)))
1341 return -EFAULT;
1343 return r;
1346 static void free_params(struct dm_ioctl *param)
1348 vfree(param);
1351 static int copy_params(struct dm_ioctl __user *user, struct dm_ioctl **param)
1353 struct dm_ioctl tmp, *dmi;
1355 if (copy_from_user(&tmp, user, sizeof(tmp) - sizeof(tmp.data)))
1356 return -EFAULT;
1358 if (tmp.data_size < (sizeof(tmp) - sizeof(tmp.data)))
1359 return -EINVAL;
1361 dmi = vmalloc(tmp.data_size);
1362 if (!dmi)
1363 return -ENOMEM;
1365 if (copy_from_user(dmi, user, tmp.data_size)) {
1366 vfree(dmi);
1367 return -EFAULT;
1370 *param = dmi;
1371 return 0;
1374 static int validate_params(uint cmd, struct dm_ioctl *param)
1376 /* Always clear this flag */
1377 param->flags &= ~DM_BUFFER_FULL_FLAG;
1379 /* Ignores parameters */
1380 if (cmd == DM_REMOVE_ALL_CMD ||
1381 cmd == DM_LIST_DEVICES_CMD ||
1382 cmd == DM_LIST_VERSIONS_CMD)
1383 return 0;
1385 if ((cmd == DM_DEV_CREATE_CMD)) {
1386 if (!*param->name) {
1387 DMWARN("name not supplied when creating device");
1388 return -EINVAL;
1390 } else if ((*param->uuid && *param->name)) {
1391 DMWARN("only supply one of name or uuid, cmd(%u)", cmd);
1392 return -EINVAL;
1395 /* Ensure strings are terminated */
1396 param->name[DM_NAME_LEN - 1] = '\0';
1397 param->uuid[DM_UUID_LEN - 1] = '\0';
1399 return 0;
1402 static int ctl_ioctl(uint command, struct dm_ioctl __user *user)
1404 int r = 0;
1405 unsigned int cmd;
1406 struct dm_ioctl *uninitialized_var(param);
1407 ioctl_fn fn = NULL;
1408 size_t param_size;
1410 /* only root can play with this */
1411 if (!capable(CAP_SYS_ADMIN))
1412 return -EACCES;
1414 if (_IOC_TYPE(command) != DM_IOCTL)
1415 return -ENOTTY;
1417 cmd = _IOC_NR(command);
1420 * Check the interface version passed in. This also
1421 * writes out the kernel's interface version.
1423 r = check_version(cmd, user);
1424 if (r)
1425 return r;
1428 * Nothing more to do for the version command.
1430 if (cmd == DM_VERSION_CMD)
1431 return 0;
1433 fn = lookup_ioctl(cmd);
1434 if (!fn) {
1435 DMWARN("dm_ctl_ioctl: unknown command 0x%x", command);
1436 return -ENOTTY;
1440 * Trying to avoid low memory issues when a device is
1441 * suspended.
1443 current->flags |= PF_MEMALLOC;
1446 * Copy the parameters into kernel space.
1448 r = copy_params(user, &param);
1450 current->flags &= ~PF_MEMALLOC;
1452 if (r)
1453 return r;
1455 r = validate_params(cmd, param);
1456 if (r)
1457 goto out;
1459 param_size = param->data_size;
1460 param->data_size = sizeof(*param);
1461 r = fn(param, param_size);
1464 * Copy the results back to userland.
1466 if (!r && copy_to_user(user, param, param->data_size))
1467 r = -EFAULT;
1469 out:
1470 free_params(param);
1471 return r;
1474 static long dm_ctl_ioctl(struct file *file, uint command, ulong u)
1476 return (long)ctl_ioctl(command, (struct dm_ioctl __user *)u);
1479 #ifdef CONFIG_COMPAT
1480 static long dm_compat_ctl_ioctl(struct file *file, uint command, ulong u)
1482 return (long)dm_ctl_ioctl(file, command, (ulong) compat_ptr(u));
1484 #else
1485 #define dm_compat_ctl_ioctl NULL
1486 #endif
1488 static const struct file_operations _ctl_fops = {
1489 .unlocked_ioctl = dm_ctl_ioctl,
1490 .compat_ioctl = dm_compat_ctl_ioctl,
1491 .owner = THIS_MODULE,
1494 static struct miscdevice _dm_misc = {
1495 .minor = MISC_DYNAMIC_MINOR,
1496 .name = DM_NAME,
1497 .fops = &_ctl_fops
1501 * Create misc character device and link to DM_DIR/control.
1503 int __init dm_interface_init(void)
1505 int r;
1507 r = dm_hash_init();
1508 if (r)
1509 return r;
1511 r = misc_register(&_dm_misc);
1512 if (r) {
1513 DMERR("misc_register failed for control device");
1514 dm_hash_exit();
1515 return r;
1518 DMINFO("%d.%d.%d%s initialised: %s", DM_VERSION_MAJOR,
1519 DM_VERSION_MINOR, DM_VERSION_PATCHLEVEL, DM_VERSION_EXTRA,
1520 DM_DRIVER_EMAIL);
1521 return 0;
1524 void dm_interface_exit(void)
1526 if (misc_deregister(&_dm_misc) < 0)
1527 DMERR("misc_deregister failed for control device");
1529 dm_hash_exit();
1533 * dm_copy_name_and_uuid - Copy mapped device name & uuid into supplied buffers
1534 * @md: Pointer to mapped_device
1535 * @name: Buffer (size DM_NAME_LEN) for name
1536 * @uuid: Buffer (size DM_UUID_LEN) for uuid or empty string if uuid not defined
1538 int dm_copy_name_and_uuid(struct mapped_device *md, char *name, char *uuid)
1540 int r = 0;
1541 struct hash_cell *hc;
1543 if (!md)
1544 return -ENXIO;
1546 dm_get(md);
1547 down_read(&_hash_lock);
1548 hc = dm_get_mdptr(md);
1549 if (!hc || hc->md != md) {
1550 r = -ENXIO;
1551 goto out;
1554 strcpy(name, hc->name);
1555 strcpy(uuid, hc->uuid ? : "");
1557 out:
1558 up_read(&_hash_lock);
1559 dm_put(md);
1561 return r;