dm ioctl: retrieve status from inactive table
[linux-2.6.git] / drivers / md / dm-ioctl.c
bloba3d20265ffc1b700f198fb583c47162c4ebd1a23
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);
60 * Protects use of mdptr to obtain hash cell name and uuid from mapped device.
62 static DEFINE_MUTEX(dm_hash_cells_mutex);
64 static void init_buckets(struct list_head *buckets)
66 unsigned int i;
68 for (i = 0; i < NUM_BUCKETS; i++)
69 INIT_LIST_HEAD(buckets + i);
72 static int dm_hash_init(void)
74 init_buckets(_name_buckets);
75 init_buckets(_uuid_buckets);
76 return 0;
79 static void dm_hash_exit(void)
81 dm_hash_remove_all(0);
84 /*-----------------------------------------------------------------
85 * Hash function:
86 * We're not really concerned with the str hash function being
87 * fast since it's only used by the ioctl interface.
88 *---------------------------------------------------------------*/
89 static unsigned int hash_str(const char *str)
91 const unsigned int hash_mult = 2654435387U;
92 unsigned int h = 0;
94 while (*str)
95 h = (h + (unsigned int) *str++) * hash_mult;
97 return h & MASK_BUCKETS;
100 /*-----------------------------------------------------------------
101 * Code for looking up a device by name
102 *---------------------------------------------------------------*/
103 static struct hash_cell *__get_name_cell(const char *str)
105 struct hash_cell *hc;
106 unsigned int h = hash_str(str);
108 list_for_each_entry (hc, _name_buckets + h, name_list)
109 if (!strcmp(hc->name, str)) {
110 dm_get(hc->md);
111 return hc;
114 return NULL;
117 static struct hash_cell *__get_uuid_cell(const char *str)
119 struct hash_cell *hc;
120 unsigned int h = hash_str(str);
122 list_for_each_entry (hc, _uuid_buckets + h, uuid_list)
123 if (!strcmp(hc->uuid, str)) {
124 dm_get(hc->md);
125 return hc;
128 return NULL;
131 /*-----------------------------------------------------------------
132 * Inserting, removing and renaming a device.
133 *---------------------------------------------------------------*/
134 static struct hash_cell *alloc_cell(const char *name, const char *uuid,
135 struct mapped_device *md)
137 struct hash_cell *hc;
139 hc = kmalloc(sizeof(*hc), GFP_KERNEL);
140 if (!hc)
141 return NULL;
143 hc->name = kstrdup(name, GFP_KERNEL);
144 if (!hc->name) {
145 kfree(hc);
146 return NULL;
149 if (!uuid)
150 hc->uuid = NULL;
152 else {
153 hc->uuid = kstrdup(uuid, GFP_KERNEL);
154 if (!hc->uuid) {
155 kfree(hc->name);
156 kfree(hc);
157 return NULL;
161 INIT_LIST_HEAD(&hc->name_list);
162 INIT_LIST_HEAD(&hc->uuid_list);
163 hc->md = md;
164 hc->new_map = NULL;
165 return hc;
168 static void free_cell(struct hash_cell *hc)
170 if (hc) {
171 kfree(hc->name);
172 kfree(hc->uuid);
173 kfree(hc);
178 * The kdev_t and uuid of a device can never change once it is
179 * initially inserted.
181 static int dm_hash_insert(const char *name, const char *uuid, struct mapped_device *md)
183 struct hash_cell *cell, *hc;
186 * Allocate the new cells.
188 cell = alloc_cell(name, uuid, md);
189 if (!cell)
190 return -ENOMEM;
193 * Insert the cell into both hash tables.
195 down_write(&_hash_lock);
196 hc = __get_name_cell(name);
197 if (hc) {
198 dm_put(hc->md);
199 goto bad;
202 list_add(&cell->name_list, _name_buckets + hash_str(name));
204 if (uuid) {
205 hc = __get_uuid_cell(uuid);
206 if (hc) {
207 list_del(&cell->name_list);
208 dm_put(hc->md);
209 goto bad;
211 list_add(&cell->uuid_list, _uuid_buckets + hash_str(uuid));
213 dm_get(md);
214 mutex_lock(&dm_hash_cells_mutex);
215 dm_set_mdptr(md, cell);
216 mutex_unlock(&dm_hash_cells_mutex);
217 up_write(&_hash_lock);
219 return 0;
221 bad:
222 up_write(&_hash_lock);
223 free_cell(cell);
224 return -EBUSY;
227 static void __hash_remove(struct hash_cell *hc)
229 struct dm_table *table;
231 /* remove from the dev hash */
232 list_del(&hc->uuid_list);
233 list_del(&hc->name_list);
234 mutex_lock(&dm_hash_cells_mutex);
235 dm_set_mdptr(hc->md, NULL);
236 mutex_unlock(&dm_hash_cells_mutex);
238 table = dm_get_live_table(hc->md);
239 if (table) {
240 dm_table_event(table);
241 dm_table_put(table);
244 if (hc->new_map)
245 dm_table_destroy(hc->new_map);
246 dm_put(hc->md);
247 free_cell(hc);
250 static void dm_hash_remove_all(int keep_open_devices)
252 int i, dev_skipped, dev_removed;
253 struct hash_cell *hc;
254 struct list_head *tmp, *n;
256 down_write(&_hash_lock);
258 retry:
259 dev_skipped = dev_removed = 0;
260 for (i = 0; i < NUM_BUCKETS; i++) {
261 list_for_each_safe (tmp, n, _name_buckets + i) {
262 hc = list_entry(tmp, struct hash_cell, name_list);
264 if (keep_open_devices &&
265 dm_lock_for_deletion(hc->md)) {
266 dev_skipped++;
267 continue;
269 __hash_remove(hc);
270 dev_removed = 1;
275 * Some mapped devices may be using other mapped devices, so if any
276 * still exist, repeat until we make no further progress.
278 if (dev_skipped) {
279 if (dev_removed)
280 goto retry;
282 DMWARN("remove_all left %d open device(s)", dev_skipped);
285 up_write(&_hash_lock);
288 static int dm_hash_rename(uint32_t cookie, const char *old, const char *new)
290 char *new_name, *old_name;
291 struct hash_cell *hc;
292 struct dm_table *table;
295 * duplicate new.
297 new_name = kstrdup(new, GFP_KERNEL);
298 if (!new_name)
299 return -ENOMEM;
301 down_write(&_hash_lock);
304 * Is new free ?
306 hc = __get_name_cell(new);
307 if (hc) {
308 DMWARN("asked to rename to an already existing name %s -> %s",
309 old, new);
310 dm_put(hc->md);
311 up_write(&_hash_lock);
312 kfree(new_name);
313 return -EBUSY;
317 * Is there such a device as 'old' ?
319 hc = __get_name_cell(old);
320 if (!hc) {
321 DMWARN("asked to rename a non existent device %s -> %s",
322 old, new);
323 up_write(&_hash_lock);
324 kfree(new_name);
325 return -ENXIO;
329 * rename and move the name cell.
331 list_del(&hc->name_list);
332 old_name = hc->name;
333 mutex_lock(&dm_hash_cells_mutex);
334 hc->name = new_name;
335 mutex_unlock(&dm_hash_cells_mutex);
336 list_add(&hc->name_list, _name_buckets + hash_str(new_name));
339 * Wake up any dm event waiters.
341 table = dm_get_live_table(hc->md);
342 if (table) {
343 dm_table_event(table);
344 dm_table_put(table);
347 dm_kobject_uevent(hc->md, KOBJ_CHANGE, cookie);
349 dm_put(hc->md);
350 up_write(&_hash_lock);
351 kfree(old_name);
352 return 0;
355 /*-----------------------------------------------------------------
356 * Implementation of the ioctl commands
357 *---------------------------------------------------------------*/
359 * All the ioctl commands get dispatched to functions with this
360 * prototype.
362 typedef int (*ioctl_fn)(struct dm_ioctl *param, size_t param_size);
364 static int remove_all(struct dm_ioctl *param, size_t param_size)
366 dm_hash_remove_all(1);
367 param->data_size = 0;
368 return 0;
372 * Round up the ptr to an 8-byte boundary.
374 #define ALIGN_MASK 7
375 static inline void *align_ptr(void *ptr)
377 return (void *) (((size_t) (ptr + ALIGN_MASK)) & ~ALIGN_MASK);
381 * Retrieves the data payload buffer from an already allocated
382 * struct dm_ioctl.
384 static void *get_result_buffer(struct dm_ioctl *param, size_t param_size,
385 size_t *len)
387 param->data_start = align_ptr(param + 1) - (void *) param;
389 if (param->data_start < param_size)
390 *len = param_size - param->data_start;
391 else
392 *len = 0;
394 return ((void *) param) + param->data_start;
397 static int list_devices(struct dm_ioctl *param, size_t param_size)
399 unsigned int i;
400 struct hash_cell *hc;
401 size_t len, needed = 0;
402 struct gendisk *disk;
403 struct dm_name_list *nl, *old_nl = NULL;
405 down_write(&_hash_lock);
408 * Loop through all the devices working out how much
409 * space we need.
411 for (i = 0; i < NUM_BUCKETS; i++) {
412 list_for_each_entry (hc, _name_buckets + i, name_list) {
413 needed += sizeof(struct dm_name_list);
414 needed += strlen(hc->name) + 1;
415 needed += ALIGN_MASK;
420 * Grab our output buffer.
422 nl = get_result_buffer(param, param_size, &len);
423 if (len < needed) {
424 param->flags |= DM_BUFFER_FULL_FLAG;
425 goto out;
427 param->data_size = param->data_start + needed;
429 nl->dev = 0; /* Flags no data */
432 * Now loop through filling out the names.
434 for (i = 0; i < NUM_BUCKETS; i++) {
435 list_for_each_entry (hc, _name_buckets + i, name_list) {
436 if (old_nl)
437 old_nl->next = (uint32_t) ((void *) nl -
438 (void *) old_nl);
439 disk = dm_disk(hc->md);
440 nl->dev = huge_encode_dev(disk_devt(disk));
441 nl->next = 0;
442 strcpy(nl->name, hc->name);
444 old_nl = nl;
445 nl = align_ptr(((void *) ++nl) + strlen(hc->name) + 1);
449 out:
450 up_write(&_hash_lock);
451 return 0;
454 static void list_version_get_needed(struct target_type *tt, void *needed_param)
456 size_t *needed = needed_param;
458 *needed += sizeof(struct dm_target_versions);
459 *needed += strlen(tt->name);
460 *needed += ALIGN_MASK;
463 static void list_version_get_info(struct target_type *tt, void *param)
465 struct vers_iter *info = param;
467 /* Check space - it might have changed since the first iteration */
468 if ((char *)info->vers + sizeof(tt->version) + strlen(tt->name) + 1 >
469 info->end) {
471 info->flags = DM_BUFFER_FULL_FLAG;
472 return;
475 if (info->old_vers)
476 info->old_vers->next = (uint32_t) ((void *)info->vers -
477 (void *)info->old_vers);
478 info->vers->version[0] = tt->version[0];
479 info->vers->version[1] = tt->version[1];
480 info->vers->version[2] = tt->version[2];
481 info->vers->next = 0;
482 strcpy(info->vers->name, tt->name);
484 info->old_vers = info->vers;
485 info->vers = align_ptr(((void *) ++info->vers) + strlen(tt->name) + 1);
488 static int list_versions(struct dm_ioctl *param, size_t param_size)
490 size_t len, needed = 0;
491 struct dm_target_versions *vers;
492 struct vers_iter iter_info;
495 * Loop through all the devices working out how much
496 * space we need.
498 dm_target_iterate(list_version_get_needed, &needed);
501 * Grab our output buffer.
503 vers = get_result_buffer(param, param_size, &len);
504 if (len < needed) {
505 param->flags |= DM_BUFFER_FULL_FLAG;
506 goto out;
508 param->data_size = param->data_start + needed;
510 iter_info.param_size = param_size;
511 iter_info.old_vers = NULL;
512 iter_info.vers = vers;
513 iter_info.flags = 0;
514 iter_info.end = (char *)vers+len;
517 * Now loop through filling out the names & versions.
519 dm_target_iterate(list_version_get_info, &iter_info);
520 param->flags |= iter_info.flags;
522 out:
523 return 0;
526 static int check_name(const char *name)
528 if (strchr(name, '/')) {
529 DMWARN("invalid device name");
530 return -EINVAL;
533 return 0;
537 * On successful return, the caller must not attempt to acquire
538 * _hash_lock without first calling dm_table_put, because dm_table_destroy
539 * waits for this dm_table_put and could be called under this lock.
541 static struct dm_table *dm_get_inactive_table(struct mapped_device *md)
543 struct hash_cell *hc;
544 struct dm_table *table = NULL;
546 down_read(&_hash_lock);
547 hc = dm_get_mdptr(md);
548 if (!hc || hc->md != md) {
549 DMWARN("device has been removed from the dev hash table.");
550 goto out;
553 table = hc->new_map;
554 if (table)
555 dm_table_get(table);
557 out:
558 up_read(&_hash_lock);
560 return table;
563 static struct dm_table *dm_get_live_or_inactive_table(struct mapped_device *md,
564 struct dm_ioctl *param)
566 return (param->flags & DM_QUERY_INACTIVE_TABLE_FLAG) ?
567 dm_get_inactive_table(md) : dm_get_live_table(md);
571 * Fills in a dm_ioctl structure, ready for sending back to
572 * userland.
574 static int __dev_status(struct mapped_device *md, struct dm_ioctl *param)
576 struct gendisk *disk = dm_disk(md);
577 struct dm_table *table;
579 param->flags &= ~(DM_SUSPEND_FLAG | DM_READONLY_FLAG |
580 DM_ACTIVE_PRESENT_FLAG);
582 if (dm_suspended(md))
583 param->flags |= DM_SUSPEND_FLAG;
585 param->dev = huge_encode_dev(disk_devt(disk));
588 * Yes, this will be out of date by the time it gets back
589 * to userland, but it is still very useful for
590 * debugging.
592 param->open_count = dm_open_count(md);
594 param->event_nr = dm_get_event_nr(md);
595 param->target_count = 0;
597 table = dm_get_live_table(md);
598 if (table) {
599 if (!(param->flags & DM_QUERY_INACTIVE_TABLE_FLAG)) {
600 if (get_disk_ro(disk))
601 param->flags |= DM_READONLY_FLAG;
602 param->target_count = dm_table_get_num_targets(table);
604 dm_table_put(table);
606 param->flags |= DM_ACTIVE_PRESENT_FLAG;
609 if (param->flags & DM_QUERY_INACTIVE_TABLE_FLAG) {
610 table = dm_get_inactive_table(md);
611 if (table) {
612 if (!(dm_table_get_mode(table) & FMODE_WRITE))
613 param->flags |= DM_READONLY_FLAG;
614 param->target_count = dm_table_get_num_targets(table);
615 dm_table_put(table);
619 return 0;
622 static int dev_create(struct dm_ioctl *param, size_t param_size)
624 int r, m = DM_ANY_MINOR;
625 struct mapped_device *md;
627 r = check_name(param->name);
628 if (r)
629 return r;
631 if (param->flags & DM_PERSISTENT_DEV_FLAG)
632 m = MINOR(huge_decode_dev(param->dev));
634 r = dm_create(m, &md);
635 if (r)
636 return r;
638 r = dm_hash_insert(param->name, *param->uuid ? param->uuid : NULL, md);
639 if (r) {
640 dm_put(md);
641 return r;
644 param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
646 r = __dev_status(md, param);
647 dm_put(md);
649 return r;
653 * Always use UUID for lookups if it's present, otherwise use name or dev.
655 static struct hash_cell *__find_device_hash_cell(struct dm_ioctl *param)
657 struct mapped_device *md;
658 void *mdptr = NULL;
660 if (*param->uuid)
661 return __get_uuid_cell(param->uuid);
663 if (*param->name)
664 return __get_name_cell(param->name);
666 md = dm_get_md(huge_decode_dev(param->dev));
667 if (!md)
668 goto out;
670 mdptr = dm_get_mdptr(md);
671 if (!mdptr)
672 dm_put(md);
674 out:
675 return mdptr;
678 static struct mapped_device *find_device(struct dm_ioctl *param)
680 struct hash_cell *hc;
681 struct mapped_device *md = NULL;
683 down_read(&_hash_lock);
684 hc = __find_device_hash_cell(param);
685 if (hc) {
686 md = hc->md;
689 * Sneakily write in both the name and the uuid
690 * while we have the cell.
692 strlcpy(param->name, hc->name, sizeof(param->name));
693 if (hc->uuid)
694 strlcpy(param->uuid, hc->uuid, sizeof(param->uuid));
695 else
696 param->uuid[0] = '\0';
698 if (hc->new_map)
699 param->flags |= DM_INACTIVE_PRESENT_FLAG;
700 else
701 param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
703 up_read(&_hash_lock);
705 return md;
708 static int dev_remove(struct dm_ioctl *param, size_t param_size)
710 struct hash_cell *hc;
711 struct mapped_device *md;
712 int r;
714 down_write(&_hash_lock);
715 hc = __find_device_hash_cell(param);
717 if (!hc) {
718 DMWARN("device doesn't appear to be in the dev hash table.");
719 up_write(&_hash_lock);
720 return -ENXIO;
723 md = hc->md;
726 * Ensure the device is not open and nothing further can open it.
728 r = dm_lock_for_deletion(md);
729 if (r) {
730 DMWARN("unable to remove open device %s", hc->name);
731 up_write(&_hash_lock);
732 dm_put(md);
733 return r;
736 __hash_remove(hc);
737 up_write(&_hash_lock);
739 dm_kobject_uevent(md, KOBJ_REMOVE, param->event_nr);
741 dm_put(md);
742 param->data_size = 0;
743 return 0;
747 * Check a string doesn't overrun the chunk of
748 * memory we copied from userland.
750 static int invalid_str(char *str, void *end)
752 while ((void *) str < end)
753 if (!*str++)
754 return 0;
756 return -EINVAL;
759 static int dev_rename(struct dm_ioctl *param, size_t param_size)
761 int r;
762 char *new_name = (char *) param + param->data_start;
764 if (new_name < param->data ||
765 invalid_str(new_name, (void *) param + param_size) ||
766 strlen(new_name) > DM_NAME_LEN - 1) {
767 DMWARN("Invalid new logical volume name supplied.");
768 return -EINVAL;
771 r = check_name(new_name);
772 if (r)
773 return r;
775 param->data_size = 0;
776 return dm_hash_rename(param->event_nr, param->name, new_name);
779 static int dev_set_geometry(struct dm_ioctl *param, size_t param_size)
781 int r = -EINVAL, x;
782 struct mapped_device *md;
783 struct hd_geometry geometry;
784 unsigned long indata[4];
785 char *geostr = (char *) param + param->data_start;
787 md = find_device(param);
788 if (!md)
789 return -ENXIO;
791 if (geostr < param->data ||
792 invalid_str(geostr, (void *) param + param_size)) {
793 DMWARN("Invalid geometry supplied.");
794 goto out;
797 x = sscanf(geostr, "%lu %lu %lu %lu", indata,
798 indata + 1, indata + 2, indata + 3);
800 if (x != 4) {
801 DMWARN("Unable to interpret geometry settings.");
802 goto out;
805 if (indata[0] > 65535 || indata[1] > 255 ||
806 indata[2] > 255 || indata[3] > ULONG_MAX) {
807 DMWARN("Geometry exceeds range limits.");
808 goto out;
811 geometry.cylinders = indata[0];
812 geometry.heads = indata[1];
813 geometry.sectors = indata[2];
814 geometry.start = indata[3];
816 r = dm_set_geometry(md, &geometry);
817 if (!r)
818 r = __dev_status(md, param);
820 param->data_size = 0;
822 out:
823 dm_put(md);
824 return r;
827 static int do_suspend(struct dm_ioctl *param)
829 int r = 0;
830 unsigned suspend_flags = DM_SUSPEND_LOCKFS_FLAG;
831 struct mapped_device *md;
833 md = find_device(param);
834 if (!md)
835 return -ENXIO;
837 if (param->flags & DM_SKIP_LOCKFS_FLAG)
838 suspend_flags &= ~DM_SUSPEND_LOCKFS_FLAG;
839 if (param->flags & DM_NOFLUSH_FLAG)
840 suspend_flags |= DM_SUSPEND_NOFLUSH_FLAG;
842 if (!dm_suspended(md))
843 r = dm_suspend(md, suspend_flags);
845 if (!r)
846 r = __dev_status(md, param);
848 dm_put(md);
849 return r;
852 static int do_resume(struct dm_ioctl *param)
854 int r = 0;
855 unsigned suspend_flags = DM_SUSPEND_LOCKFS_FLAG;
856 struct hash_cell *hc;
857 struct mapped_device *md;
858 struct dm_table *new_map;
860 down_write(&_hash_lock);
862 hc = __find_device_hash_cell(param);
863 if (!hc) {
864 DMWARN("device doesn't appear to be in the dev hash table.");
865 up_write(&_hash_lock);
866 return -ENXIO;
869 md = hc->md;
871 new_map = hc->new_map;
872 hc->new_map = NULL;
873 param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
875 up_write(&_hash_lock);
877 /* Do we need to load a new map ? */
878 if (new_map) {
879 /* Suspend if it isn't already suspended */
880 if (param->flags & DM_SKIP_LOCKFS_FLAG)
881 suspend_flags &= ~DM_SUSPEND_LOCKFS_FLAG;
882 if (param->flags & DM_NOFLUSH_FLAG)
883 suspend_flags |= DM_SUSPEND_NOFLUSH_FLAG;
884 if (!dm_suspended(md))
885 dm_suspend(md, suspend_flags);
887 r = dm_swap_table(md, new_map);
888 if (r) {
889 dm_table_destroy(new_map);
890 dm_put(md);
891 return r;
894 if (dm_table_get_mode(new_map) & FMODE_WRITE)
895 set_disk_ro(dm_disk(md), 0);
896 else
897 set_disk_ro(dm_disk(md), 1);
900 if (dm_suspended(md))
901 r = dm_resume(md);
904 if (!r) {
905 dm_kobject_uevent(md, KOBJ_CHANGE, param->event_nr);
906 r = __dev_status(md, param);
909 dm_put(md);
910 return r;
914 * Set or unset the suspension state of a device.
915 * If the device already is in the requested state we just return its status.
917 static int dev_suspend(struct dm_ioctl *param, size_t param_size)
919 if (param->flags & DM_SUSPEND_FLAG)
920 return do_suspend(param);
922 return do_resume(param);
926 * Copies device info back to user space, used by
927 * the create and info ioctls.
929 static int dev_status(struct dm_ioctl *param, size_t param_size)
931 int r;
932 struct mapped_device *md;
934 md = find_device(param);
935 if (!md)
936 return -ENXIO;
938 r = __dev_status(md, param);
939 dm_put(md);
940 return r;
944 * Build up the status struct for each target
946 static void retrieve_status(struct dm_table *table,
947 struct dm_ioctl *param, size_t param_size)
949 unsigned int i, num_targets;
950 struct dm_target_spec *spec;
951 char *outbuf, *outptr;
952 status_type_t type;
953 size_t remaining, len, used = 0;
955 outptr = outbuf = get_result_buffer(param, param_size, &len);
957 if (param->flags & DM_STATUS_TABLE_FLAG)
958 type = STATUSTYPE_TABLE;
959 else
960 type = STATUSTYPE_INFO;
962 /* Get all the target info */
963 num_targets = dm_table_get_num_targets(table);
964 for (i = 0; i < num_targets; i++) {
965 struct dm_target *ti = dm_table_get_target(table, i);
967 remaining = len - (outptr - outbuf);
968 if (remaining <= sizeof(struct dm_target_spec)) {
969 param->flags |= DM_BUFFER_FULL_FLAG;
970 break;
973 spec = (struct dm_target_spec *) outptr;
975 spec->status = 0;
976 spec->sector_start = ti->begin;
977 spec->length = ti->len;
978 strncpy(spec->target_type, ti->type->name,
979 sizeof(spec->target_type));
981 outptr += sizeof(struct dm_target_spec);
982 remaining = len - (outptr - outbuf);
983 if (remaining <= 0) {
984 param->flags |= DM_BUFFER_FULL_FLAG;
985 break;
988 /* Get the status/table string from the target driver */
989 if (ti->type->status) {
990 if (ti->type->status(ti, type, outptr, remaining)) {
991 param->flags |= DM_BUFFER_FULL_FLAG;
992 break;
994 } else
995 outptr[0] = '\0';
997 outptr += strlen(outptr) + 1;
998 used = param->data_start + (outptr - outbuf);
1000 outptr = align_ptr(outptr);
1001 spec->next = outptr - outbuf;
1004 if (used)
1005 param->data_size = used;
1007 param->target_count = num_targets;
1011 * Wait for a device to report an event
1013 static int dev_wait(struct dm_ioctl *param, size_t param_size)
1015 int r;
1016 struct mapped_device *md;
1017 struct dm_table *table;
1019 md = find_device(param);
1020 if (!md)
1021 return -ENXIO;
1024 * Wait for a notification event
1026 if (dm_wait_event(md, param->event_nr)) {
1027 r = -ERESTARTSYS;
1028 goto out;
1032 * The userland program is going to want to know what
1033 * changed to trigger the event, so we may as well tell
1034 * him and save an ioctl.
1036 r = __dev_status(md, param);
1037 if (r)
1038 goto out;
1040 table = dm_get_live_or_inactive_table(md, param);
1041 if (table) {
1042 retrieve_status(table, param, param_size);
1043 dm_table_put(table);
1046 out:
1047 dm_put(md);
1048 return r;
1051 static inline fmode_t get_mode(struct dm_ioctl *param)
1053 fmode_t mode = FMODE_READ | FMODE_WRITE;
1055 if (param->flags & DM_READONLY_FLAG)
1056 mode = FMODE_READ;
1058 return mode;
1061 static int next_target(struct dm_target_spec *last, uint32_t next, void *end,
1062 struct dm_target_spec **spec, char **target_params)
1064 *spec = (struct dm_target_spec *) ((unsigned char *) last + next);
1065 *target_params = (char *) (*spec + 1);
1067 if (*spec < (last + 1))
1068 return -EINVAL;
1070 return invalid_str(*target_params, end);
1073 static int populate_table(struct dm_table *table,
1074 struct dm_ioctl *param, size_t param_size)
1076 int r;
1077 unsigned int i = 0;
1078 struct dm_target_spec *spec = (struct dm_target_spec *) param;
1079 uint32_t next = param->data_start;
1080 void *end = (void *) param + param_size;
1081 char *target_params;
1083 if (!param->target_count) {
1084 DMWARN("populate_table: no targets specified");
1085 return -EINVAL;
1088 for (i = 0; i < param->target_count; i++) {
1090 r = next_target(spec, next, end, &spec, &target_params);
1091 if (r) {
1092 DMWARN("unable to find target");
1093 return r;
1096 r = dm_table_add_target(table, spec->target_type,
1097 (sector_t) spec->sector_start,
1098 (sector_t) spec->length,
1099 target_params);
1100 if (r) {
1101 DMWARN("error adding target to table");
1102 return r;
1105 next = spec->next;
1108 r = dm_table_set_type(table);
1109 if (r) {
1110 DMWARN("unable to set table type");
1111 return r;
1114 return dm_table_complete(table);
1117 static int table_prealloc_integrity(struct dm_table *t,
1118 struct mapped_device *md)
1120 struct list_head *devices = dm_table_get_devices(t);
1121 struct dm_dev_internal *dd;
1123 list_for_each_entry(dd, devices, list)
1124 if (bdev_get_integrity(dd->dm_dev.bdev))
1125 return blk_integrity_register(dm_disk(md), NULL);
1127 return 0;
1130 static int table_load(struct dm_ioctl *param, size_t param_size)
1132 int r;
1133 struct hash_cell *hc;
1134 struct dm_table *t;
1135 struct mapped_device *md;
1137 md = find_device(param);
1138 if (!md)
1139 return -ENXIO;
1141 r = dm_table_create(&t, get_mode(param), param->target_count, md);
1142 if (r)
1143 goto out;
1145 r = populate_table(t, param, param_size);
1146 if (r) {
1147 dm_table_destroy(t);
1148 goto out;
1151 r = table_prealloc_integrity(t, md);
1152 if (r) {
1153 DMERR("%s: could not register integrity profile.",
1154 dm_device_name(md));
1155 dm_table_destroy(t);
1156 goto out;
1159 r = dm_table_alloc_md_mempools(t);
1160 if (r) {
1161 DMWARN("unable to allocate mempools for this table");
1162 dm_table_destroy(t);
1163 goto out;
1166 down_write(&_hash_lock);
1167 hc = dm_get_mdptr(md);
1168 if (!hc || hc->md != md) {
1169 DMWARN("device has been removed from the dev hash table.");
1170 dm_table_destroy(t);
1171 up_write(&_hash_lock);
1172 r = -ENXIO;
1173 goto out;
1176 if (hc->new_map)
1177 dm_table_destroy(hc->new_map);
1178 hc->new_map = t;
1179 up_write(&_hash_lock);
1181 param->flags |= DM_INACTIVE_PRESENT_FLAG;
1182 r = __dev_status(md, param);
1184 out:
1185 dm_put(md);
1187 return r;
1190 static int table_clear(struct dm_ioctl *param, size_t param_size)
1192 int r;
1193 struct hash_cell *hc;
1194 struct mapped_device *md;
1196 down_write(&_hash_lock);
1198 hc = __find_device_hash_cell(param);
1199 if (!hc) {
1200 DMWARN("device doesn't appear to be in the dev hash table.");
1201 up_write(&_hash_lock);
1202 return -ENXIO;
1205 if (hc->new_map) {
1206 dm_table_destroy(hc->new_map);
1207 hc->new_map = NULL;
1210 param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
1212 r = __dev_status(hc->md, param);
1213 md = hc->md;
1214 up_write(&_hash_lock);
1215 dm_put(md);
1216 return r;
1220 * Retrieves a list of devices used by a particular dm device.
1222 static void retrieve_deps(struct dm_table *table,
1223 struct dm_ioctl *param, size_t param_size)
1225 unsigned int count = 0;
1226 struct list_head *tmp;
1227 size_t len, needed;
1228 struct dm_dev_internal *dd;
1229 struct dm_target_deps *deps;
1231 deps = get_result_buffer(param, param_size, &len);
1234 * Count the devices.
1236 list_for_each (tmp, dm_table_get_devices(table))
1237 count++;
1240 * Check we have enough space.
1242 needed = sizeof(*deps) + (sizeof(*deps->dev) * count);
1243 if (len < needed) {
1244 param->flags |= DM_BUFFER_FULL_FLAG;
1245 return;
1249 * Fill in the devices.
1251 deps->count = count;
1252 count = 0;
1253 list_for_each_entry (dd, dm_table_get_devices(table), list)
1254 deps->dev[count++] = huge_encode_dev(dd->dm_dev.bdev->bd_dev);
1256 param->data_size = param->data_start + needed;
1259 static int table_deps(struct dm_ioctl *param, size_t param_size)
1261 int r = 0;
1262 struct mapped_device *md;
1263 struct dm_table *table;
1265 md = find_device(param);
1266 if (!md)
1267 return -ENXIO;
1269 r = __dev_status(md, param);
1270 if (r)
1271 goto out;
1273 table = dm_get_live_or_inactive_table(md, param);
1274 if (table) {
1275 retrieve_deps(table, param, param_size);
1276 dm_table_put(table);
1279 out:
1280 dm_put(md);
1281 return r;
1285 * Return the status of a device as a text string for each
1286 * target.
1288 static int table_status(struct dm_ioctl *param, size_t param_size)
1290 int r;
1291 struct mapped_device *md;
1292 struct dm_table *table;
1294 md = find_device(param);
1295 if (!md)
1296 return -ENXIO;
1298 r = __dev_status(md, param);
1299 if (r)
1300 goto out;
1302 table = dm_get_live_or_inactive_table(md, param);
1303 if (table) {
1304 retrieve_status(table, param, param_size);
1305 dm_table_put(table);
1308 out:
1309 dm_put(md);
1310 return r;
1314 * Pass a message to the target that's at the supplied device offset.
1316 static int target_message(struct dm_ioctl *param, size_t param_size)
1318 int r, argc;
1319 char **argv;
1320 struct mapped_device *md;
1321 struct dm_table *table;
1322 struct dm_target *ti;
1323 struct dm_target_msg *tmsg = (void *) param + param->data_start;
1325 md = find_device(param);
1326 if (!md)
1327 return -ENXIO;
1329 r = __dev_status(md, param);
1330 if (r)
1331 goto out;
1333 if (tmsg < (struct dm_target_msg *) param->data ||
1334 invalid_str(tmsg->message, (void *) param + param_size)) {
1335 DMWARN("Invalid target message parameters.");
1336 r = -EINVAL;
1337 goto out;
1340 r = dm_split_args(&argc, &argv, tmsg->message);
1341 if (r) {
1342 DMWARN("Failed to split target message parameters");
1343 goto out;
1346 table = dm_get_live_table(md);
1347 if (!table)
1348 goto out_argv;
1350 if (dm_deleting_md(md)) {
1351 r = -ENXIO;
1352 goto out_table;
1355 ti = dm_table_find_target(table, tmsg->sector);
1356 if (!dm_target_is_valid(ti)) {
1357 DMWARN("Target message sector outside device.");
1358 r = -EINVAL;
1359 } else if (ti->type->message)
1360 r = ti->type->message(ti, argc, argv);
1361 else {
1362 DMWARN("Target type does not support messages");
1363 r = -EINVAL;
1366 out_table:
1367 dm_table_put(table);
1368 out_argv:
1369 kfree(argv);
1370 out:
1371 param->data_size = 0;
1372 dm_put(md);
1373 return r;
1376 /*-----------------------------------------------------------------
1377 * Implementation of open/close/ioctl on the special char
1378 * device.
1379 *---------------------------------------------------------------*/
1380 static ioctl_fn lookup_ioctl(unsigned int cmd)
1382 static struct {
1383 int cmd;
1384 ioctl_fn fn;
1385 } _ioctls[] = {
1386 {DM_VERSION_CMD, NULL}, /* version is dealt with elsewhere */
1387 {DM_REMOVE_ALL_CMD, remove_all},
1388 {DM_LIST_DEVICES_CMD, list_devices},
1390 {DM_DEV_CREATE_CMD, dev_create},
1391 {DM_DEV_REMOVE_CMD, dev_remove},
1392 {DM_DEV_RENAME_CMD, dev_rename},
1393 {DM_DEV_SUSPEND_CMD, dev_suspend},
1394 {DM_DEV_STATUS_CMD, dev_status},
1395 {DM_DEV_WAIT_CMD, dev_wait},
1397 {DM_TABLE_LOAD_CMD, table_load},
1398 {DM_TABLE_CLEAR_CMD, table_clear},
1399 {DM_TABLE_DEPS_CMD, table_deps},
1400 {DM_TABLE_STATUS_CMD, table_status},
1402 {DM_LIST_VERSIONS_CMD, list_versions},
1404 {DM_TARGET_MSG_CMD, target_message},
1405 {DM_DEV_SET_GEOMETRY_CMD, dev_set_geometry}
1408 return (cmd >= ARRAY_SIZE(_ioctls)) ? NULL : _ioctls[cmd].fn;
1412 * As well as checking the version compatibility this always
1413 * copies the kernel interface version out.
1415 static int check_version(unsigned int cmd, struct dm_ioctl __user *user)
1417 uint32_t version[3];
1418 int r = 0;
1420 if (copy_from_user(version, user->version, sizeof(version)))
1421 return -EFAULT;
1423 if ((DM_VERSION_MAJOR != version[0]) ||
1424 (DM_VERSION_MINOR < version[1])) {
1425 DMWARN("ioctl interface mismatch: "
1426 "kernel(%u.%u.%u), user(%u.%u.%u), cmd(%d)",
1427 DM_VERSION_MAJOR, DM_VERSION_MINOR,
1428 DM_VERSION_PATCHLEVEL,
1429 version[0], version[1], version[2], cmd);
1430 r = -EINVAL;
1434 * Fill in the kernel version.
1436 version[0] = DM_VERSION_MAJOR;
1437 version[1] = DM_VERSION_MINOR;
1438 version[2] = DM_VERSION_PATCHLEVEL;
1439 if (copy_to_user(user->version, version, sizeof(version)))
1440 return -EFAULT;
1442 return r;
1445 static void free_params(struct dm_ioctl *param)
1447 vfree(param);
1450 static int copy_params(struct dm_ioctl __user *user, struct dm_ioctl **param)
1452 struct dm_ioctl tmp, *dmi;
1454 if (copy_from_user(&tmp, user, sizeof(tmp) - sizeof(tmp.data)))
1455 return -EFAULT;
1457 if (tmp.data_size < (sizeof(tmp) - sizeof(tmp.data)))
1458 return -EINVAL;
1460 dmi = vmalloc(tmp.data_size);
1461 if (!dmi)
1462 return -ENOMEM;
1464 if (copy_from_user(dmi, user, tmp.data_size)) {
1465 vfree(dmi);
1466 return -EFAULT;
1469 *param = dmi;
1470 return 0;
1473 static int validate_params(uint cmd, struct dm_ioctl *param)
1475 /* Always clear this flag */
1476 param->flags &= ~DM_BUFFER_FULL_FLAG;
1478 /* Ignores parameters */
1479 if (cmd == DM_REMOVE_ALL_CMD ||
1480 cmd == DM_LIST_DEVICES_CMD ||
1481 cmd == DM_LIST_VERSIONS_CMD)
1482 return 0;
1484 if ((cmd == DM_DEV_CREATE_CMD)) {
1485 if (!*param->name) {
1486 DMWARN("name not supplied when creating device");
1487 return -EINVAL;
1489 } else if ((*param->uuid && *param->name)) {
1490 DMWARN("only supply one of name or uuid, cmd(%u)", cmd);
1491 return -EINVAL;
1494 /* Ensure strings are terminated */
1495 param->name[DM_NAME_LEN - 1] = '\0';
1496 param->uuid[DM_UUID_LEN - 1] = '\0';
1498 return 0;
1501 static int ctl_ioctl(uint command, struct dm_ioctl __user *user)
1503 int r = 0;
1504 unsigned int cmd;
1505 struct dm_ioctl *uninitialized_var(param);
1506 ioctl_fn fn = NULL;
1507 size_t param_size;
1509 /* only root can play with this */
1510 if (!capable(CAP_SYS_ADMIN))
1511 return -EACCES;
1513 if (_IOC_TYPE(command) != DM_IOCTL)
1514 return -ENOTTY;
1516 cmd = _IOC_NR(command);
1519 * Check the interface version passed in. This also
1520 * writes out the kernel's interface version.
1522 r = check_version(cmd, user);
1523 if (r)
1524 return r;
1527 * Nothing more to do for the version command.
1529 if (cmd == DM_VERSION_CMD)
1530 return 0;
1532 fn = lookup_ioctl(cmd);
1533 if (!fn) {
1534 DMWARN("dm_ctl_ioctl: unknown command 0x%x", command);
1535 return -ENOTTY;
1539 * Trying to avoid low memory issues when a device is
1540 * suspended.
1542 current->flags |= PF_MEMALLOC;
1545 * Copy the parameters into kernel space.
1547 r = copy_params(user, &param);
1549 current->flags &= ~PF_MEMALLOC;
1551 if (r)
1552 return r;
1554 r = validate_params(cmd, param);
1555 if (r)
1556 goto out;
1558 param_size = param->data_size;
1559 param->data_size = sizeof(*param);
1560 r = fn(param, param_size);
1563 * Copy the results back to userland.
1565 if (!r && copy_to_user(user, param, param->data_size))
1566 r = -EFAULT;
1568 out:
1569 free_params(param);
1570 return r;
1573 static long dm_ctl_ioctl(struct file *file, uint command, ulong u)
1575 return (long)ctl_ioctl(command, (struct dm_ioctl __user *)u);
1578 #ifdef CONFIG_COMPAT
1579 static long dm_compat_ctl_ioctl(struct file *file, uint command, ulong u)
1581 return (long)dm_ctl_ioctl(file, command, (ulong) compat_ptr(u));
1583 #else
1584 #define dm_compat_ctl_ioctl NULL
1585 #endif
1587 static const struct file_operations _ctl_fops = {
1588 .unlocked_ioctl = dm_ctl_ioctl,
1589 .compat_ioctl = dm_compat_ctl_ioctl,
1590 .owner = THIS_MODULE,
1593 static struct miscdevice _dm_misc = {
1594 .minor = MISC_DYNAMIC_MINOR,
1595 .name = DM_NAME,
1596 .nodename = "mapper/control",
1597 .fops = &_ctl_fops
1601 * Create misc character device and link to DM_DIR/control.
1603 int __init dm_interface_init(void)
1605 int r;
1607 r = dm_hash_init();
1608 if (r)
1609 return r;
1611 r = misc_register(&_dm_misc);
1612 if (r) {
1613 DMERR("misc_register failed for control device");
1614 dm_hash_exit();
1615 return r;
1618 DMINFO("%d.%d.%d%s initialised: %s", DM_VERSION_MAJOR,
1619 DM_VERSION_MINOR, DM_VERSION_PATCHLEVEL, DM_VERSION_EXTRA,
1620 DM_DRIVER_EMAIL);
1621 return 0;
1624 void dm_interface_exit(void)
1626 if (misc_deregister(&_dm_misc) < 0)
1627 DMERR("misc_deregister failed for control device");
1629 dm_hash_exit();
1633 * dm_copy_name_and_uuid - Copy mapped device name & uuid into supplied buffers
1634 * @md: Pointer to mapped_device
1635 * @name: Buffer (size DM_NAME_LEN) for name
1636 * @uuid: Buffer (size DM_UUID_LEN) for uuid or empty string if uuid not defined
1638 int dm_copy_name_and_uuid(struct mapped_device *md, char *name, char *uuid)
1640 int r = 0;
1641 struct hash_cell *hc;
1643 if (!md)
1644 return -ENXIO;
1646 mutex_lock(&dm_hash_cells_mutex);
1647 hc = dm_get_mdptr(md);
1648 if (!hc || hc->md != md) {
1649 r = -ENXIO;
1650 goto out;
1653 if (name)
1654 strcpy(name, hc->name);
1655 if (uuid)
1656 strcpy(uuid, hc->uuid ? : "");
1658 out:
1659 mutex_unlock(&dm_hash_cells_mutex);
1661 return r;