MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / md / dm-ioctl.c
blobe1703f77c19633f8fd4537da1d5ff1a51083d3cb
1 /*
2 * Copyright (C) 2001, 2002 Sistina Software (UK) Limited.
4 * This file is released under the GPL.
5 */
7 #include "dm.h"
9 #include <linux/module.h>
10 #include <linux/vmalloc.h>
11 #include <linux/miscdevice.h>
12 #include <linux/init.h>
13 #include <linux/wait.h>
14 #include <linux/slab.h>
15 #include <linux/devfs_fs_kernel.h>
16 #include <linux/dm-ioctl.h>
18 #include <asm/uaccess.h>
20 #define DM_DRIVER_EMAIL "dm@uk.sistina.com"
22 /*-----------------------------------------------------------------
23 * The ioctl interface needs to be able to look up devices by
24 * name or uuid.
25 *---------------------------------------------------------------*/
26 struct hash_cell {
27 struct list_head name_list;
28 struct list_head uuid_list;
30 char *name;
31 char *uuid;
32 struct mapped_device *md;
33 struct dm_table *new_map;
36 struct vers_iter {
37 size_t param_size;
38 struct dm_target_versions *vers, *old_vers;
39 char *end;
40 uint32_t flags;
44 #define NUM_BUCKETS 64
45 #define MASK_BUCKETS (NUM_BUCKETS - 1)
46 static struct list_head _name_buckets[NUM_BUCKETS];
47 static struct list_head _uuid_buckets[NUM_BUCKETS];
49 static void dm_hash_remove_all(void);
52 * Guards access to both hash tables.
54 static DECLARE_RWSEM(_hash_lock);
56 static void init_buckets(struct list_head *buckets)
58 unsigned int i;
60 for (i = 0; i < NUM_BUCKETS; i++)
61 INIT_LIST_HEAD(buckets + i);
64 static int dm_hash_init(void)
66 init_buckets(_name_buckets);
67 init_buckets(_uuid_buckets);
68 devfs_mk_dir(DM_DIR);
69 return 0;
72 static void dm_hash_exit(void)
74 dm_hash_remove_all();
75 devfs_remove(DM_DIR);
78 /*-----------------------------------------------------------------
79 * Hash function:
80 * We're not really concerned with the str hash function being
81 * fast since it's only used by the ioctl interface.
82 *---------------------------------------------------------------*/
83 static unsigned int hash_str(const char *str)
85 const unsigned int hash_mult = 2654435387U;
86 unsigned int h = 0;
88 while (*str)
89 h = (h + (unsigned int) *str++) * hash_mult;
91 return h & MASK_BUCKETS;
94 /*-----------------------------------------------------------------
95 * Code for looking up a device by name
96 *---------------------------------------------------------------*/
97 static struct hash_cell *__get_name_cell(const char *str)
99 struct hash_cell *hc;
100 unsigned int h = hash_str(str);
102 list_for_each_entry (hc, _name_buckets + h, name_list)
103 if (!strcmp(hc->name, str))
104 return hc;
106 return NULL;
109 static struct hash_cell *__get_uuid_cell(const char *str)
111 struct hash_cell *hc;
112 unsigned int h = hash_str(str);
114 list_for_each_entry (hc, _uuid_buckets + h, uuid_list)
115 if (!strcmp(hc->uuid, str))
116 return hc;
118 return NULL;
121 /*-----------------------------------------------------------------
122 * Inserting, removing and renaming a device.
123 *---------------------------------------------------------------*/
124 static inline char *kstrdup(const char *str)
126 char *r = kmalloc(strlen(str) + 1, GFP_KERNEL);
127 if (r)
128 strcpy(r, str);
129 return r;
132 static struct hash_cell *alloc_cell(const char *name, const char *uuid,
133 struct mapped_device *md)
135 struct hash_cell *hc;
137 hc = kmalloc(sizeof(*hc), GFP_KERNEL);
138 if (!hc)
139 return NULL;
141 hc->name = kstrdup(name);
142 if (!hc->name) {
143 kfree(hc);
144 return NULL;
147 if (!uuid)
148 hc->uuid = NULL;
150 else {
151 hc->uuid = kstrdup(uuid);
152 if (!hc->uuid) {
153 kfree(hc->name);
154 kfree(hc);
155 return NULL;
159 INIT_LIST_HEAD(&hc->name_list);
160 INIT_LIST_HEAD(&hc->uuid_list);
161 hc->md = md;
162 hc->new_map = NULL;
163 return hc;
166 static void free_cell(struct hash_cell *hc)
168 if (hc) {
169 kfree(hc->name);
170 kfree(hc->uuid);
171 kfree(hc);
176 * devfs stuff.
178 static int register_with_devfs(struct hash_cell *hc)
180 struct gendisk *disk = dm_disk(hc->md);
182 devfs_mk_bdev(MKDEV(disk->major, disk->first_minor),
183 S_IFBLK | S_IRUSR | S_IWUSR | S_IRGRP,
184 DM_DIR "/%s", hc->name);
185 return 0;
188 static int unregister_with_devfs(struct hash_cell *hc)
190 devfs_remove(DM_DIR"/%s", hc->name);
191 return 0;
195 * The kdev_t and uuid of a device can never change once it is
196 * initially inserted.
198 static int dm_hash_insert(const char *name, const char *uuid, struct mapped_device *md)
200 struct hash_cell *cell;
203 * Allocate the new cells.
205 cell = alloc_cell(name, uuid, md);
206 if (!cell)
207 return -ENOMEM;
210 * Insert the cell into both hash tables.
212 down_write(&_hash_lock);
213 if (__get_name_cell(name))
214 goto bad;
216 list_add(&cell->name_list, _name_buckets + hash_str(name));
218 if (uuid) {
219 if (__get_uuid_cell(uuid)) {
220 list_del(&cell->name_list);
221 goto bad;
223 list_add(&cell->uuid_list, _uuid_buckets + hash_str(uuid));
225 register_with_devfs(cell);
226 dm_get(md);
227 up_write(&_hash_lock);
229 return 0;
231 bad:
232 up_write(&_hash_lock);
233 free_cell(cell);
234 return -EBUSY;
237 static void __hash_remove(struct hash_cell *hc)
239 /* remove from the dev hash */
240 list_del(&hc->uuid_list);
241 list_del(&hc->name_list);
242 unregister_with_devfs(hc);
243 dm_put(hc->md);
244 if (hc->new_map)
245 dm_table_put(hc->new_map);
246 free_cell(hc);
249 static void dm_hash_remove_all(void)
251 int i;
252 struct hash_cell *hc;
253 struct list_head *tmp, *n;
255 down_write(&_hash_lock);
256 for (i = 0; i < NUM_BUCKETS; i++) {
257 list_for_each_safe (tmp, n, _name_buckets + i) {
258 hc = list_entry(tmp, struct hash_cell, name_list);
259 __hash_remove(hc);
262 up_write(&_hash_lock);
265 static int dm_hash_rename(const char *old, const char *new)
267 char *new_name, *old_name;
268 struct hash_cell *hc;
271 * duplicate new.
273 new_name = kstrdup(new);
274 if (!new_name)
275 return -ENOMEM;
277 down_write(&_hash_lock);
280 * Is new free ?
282 hc = __get_name_cell(new);
283 if (hc) {
284 DMWARN("asked to rename to an already existing name %s -> %s",
285 old, new);
286 up_write(&_hash_lock);
287 kfree(new_name);
288 return -EBUSY;
292 * Is there such a device as 'old' ?
294 hc = __get_name_cell(old);
295 if (!hc) {
296 DMWARN("asked to rename a non existent device %s -> %s",
297 old, new);
298 up_write(&_hash_lock);
299 kfree(new_name);
300 return -ENXIO;
304 * rename and move the name cell.
306 unregister_with_devfs(hc);
308 list_del(&hc->name_list);
309 old_name = hc->name;
310 hc->name = new_name;
311 list_add(&hc->name_list, _name_buckets + hash_str(new_name));
313 /* rename the device node in devfs */
314 register_with_devfs(hc);
316 up_write(&_hash_lock);
317 kfree(old_name);
318 return 0;
321 /*-----------------------------------------------------------------
322 * Implementation of the ioctl commands
323 *---------------------------------------------------------------*/
325 * All the ioctl commands get dispatched to functions with this
326 * prototype.
328 typedef int (*ioctl_fn)(struct dm_ioctl *param, size_t param_size);
330 static int remove_all(struct dm_ioctl *param, size_t param_size)
332 dm_hash_remove_all();
333 param->data_size = 0;
334 return 0;
338 * Round up the ptr to an 8-byte boundary.
340 #define ALIGN_MASK 7
341 static inline void *align_ptr(void *ptr)
343 return (void *) (((size_t) (ptr + ALIGN_MASK)) & ~ALIGN_MASK);
347 * Retrieves the data payload buffer from an already allocated
348 * struct dm_ioctl.
350 static void *get_result_buffer(struct dm_ioctl *param, size_t param_size,
351 size_t *len)
353 param->data_start = align_ptr(param + 1) - (void *) param;
355 if (param->data_start < param_size)
356 *len = param_size - param->data_start;
357 else
358 *len = 0;
360 return ((void *) param) + param->data_start;
363 static int list_devices(struct dm_ioctl *param, size_t param_size)
365 unsigned int i;
366 struct hash_cell *hc;
367 size_t len, needed = 0;
368 struct gendisk *disk;
369 struct dm_name_list *nl, *old_nl = NULL;
371 down_write(&_hash_lock);
374 * Loop through all the devices working out how much
375 * space we need.
377 for (i = 0; i < NUM_BUCKETS; i++) {
378 list_for_each_entry (hc, _name_buckets + i, name_list) {
379 needed += sizeof(struct dm_name_list);
380 needed += strlen(hc->name) + 1;
381 needed += ALIGN_MASK;
386 * Grab our output buffer.
388 nl = get_result_buffer(param, param_size, &len);
389 if (len < needed) {
390 param->flags |= DM_BUFFER_FULL_FLAG;
391 goto out;
393 param->data_size = param->data_start + needed;
395 nl->dev = 0; /* Flags no data */
398 * Now loop through filling out the names.
400 for (i = 0; i < NUM_BUCKETS; i++) {
401 list_for_each_entry (hc, _name_buckets + i, name_list) {
402 if (old_nl)
403 old_nl->next = (uint32_t) ((void *) nl -
404 (void *) old_nl);
405 disk = dm_disk(hc->md);
406 nl->dev = huge_encode_dev(MKDEV(disk->major, disk->first_minor));
407 nl->next = 0;
408 strcpy(nl->name, hc->name);
410 old_nl = nl;
411 nl = align_ptr(((void *) ++nl) + strlen(hc->name) + 1);
415 out:
416 up_write(&_hash_lock);
417 return 0;
420 static void list_version_get_needed(struct target_type *tt, void *needed_param)
422 size_t *needed = needed_param;
424 *needed += strlen(tt->name);
425 *needed += sizeof(tt->version);
426 *needed += ALIGN_MASK;
429 static void list_version_get_info(struct target_type *tt, void *param)
431 struct vers_iter *info = param;
433 /* Check space - it might have changed since the first iteration */
434 if ((char *)info->vers + sizeof(tt->version) + strlen(tt->name) + 1 >
435 info->end) {
437 info->flags = DM_BUFFER_FULL_FLAG;
438 return;
441 if (info->old_vers)
442 info->old_vers->next = (uint32_t) ((void *)info->vers -
443 (void *)info->old_vers);
444 info->vers->version[0] = tt->version[0];
445 info->vers->version[1] = tt->version[1];
446 info->vers->version[2] = tt->version[2];
447 info->vers->next = 0;
448 strcpy(info->vers->name, tt->name);
450 info->old_vers = info->vers;
451 info->vers = align_ptr(((void *) ++info->vers) + strlen(tt->name) + 1);
454 static int list_versions(struct dm_ioctl *param, size_t param_size)
456 size_t len, needed = 0;
457 struct dm_target_versions *vers;
458 struct vers_iter iter_info;
461 * Loop through all the devices working out how much
462 * space we need.
464 dm_target_iterate(list_version_get_needed, &needed);
467 * Grab our output buffer.
469 vers = get_result_buffer(param, param_size, &len);
470 if (len < needed) {
471 param->flags |= DM_BUFFER_FULL_FLAG;
472 goto out;
474 param->data_size = param->data_start + needed;
476 iter_info.param_size = param_size;
477 iter_info.old_vers = NULL;
478 iter_info.vers = vers;
479 iter_info.flags = 0;
480 iter_info.end = (char *)vers+len;
483 * Now loop through filling out the names & versions.
485 dm_target_iterate(list_version_get_info, &iter_info);
486 param->flags |= iter_info.flags;
488 out:
489 return 0;
494 static int check_name(const char *name)
496 if (strchr(name, '/')) {
497 DMWARN("invalid device name");
498 return -EINVAL;
501 return 0;
505 * Fills in a dm_ioctl structure, ready for sending back to
506 * userland.
508 static int __dev_status(struct mapped_device *md, struct dm_ioctl *param)
510 struct gendisk *disk = dm_disk(md);
511 struct dm_table *table;
512 struct block_device *bdev;
514 param->flags &= ~(DM_SUSPEND_FLAG | DM_READONLY_FLAG |
515 DM_ACTIVE_PRESENT_FLAG);
517 if (dm_suspended(md))
518 param->flags |= DM_SUSPEND_FLAG;
520 bdev = bdget_disk(disk, 0);
521 if (!bdev)
522 return -ENXIO;
524 param->dev = huge_encode_dev(MKDEV(disk->major, disk->first_minor));
527 * Yes, this will be out of date by the time it gets back
528 * to userland, but it is still very useful ofr
529 * debugging.
531 param->open_count = bdev->bd_openers;
532 bdput(bdev);
534 if (disk->policy)
535 param->flags |= DM_READONLY_FLAG;
537 param->event_nr = dm_get_event_nr(md);
539 table = dm_get_table(md);
540 if (table) {
541 param->flags |= DM_ACTIVE_PRESENT_FLAG;
542 param->target_count = dm_table_get_num_targets(table);
543 dm_table_put(table);
544 } else
545 param->target_count = 0;
547 return 0;
550 static int dev_create(struct dm_ioctl *param, size_t param_size)
552 int r;
553 struct mapped_device *md;
555 r = check_name(param->name);
556 if (r)
557 return r;
559 if (param->flags & DM_PERSISTENT_DEV_FLAG)
560 r = dm_create_with_minor(MINOR(huge_decode_dev(param->dev)), &md);
561 else
562 r = dm_create(&md);
564 if (r)
565 return r;
567 r = dm_hash_insert(param->name, *param->uuid ? param->uuid : NULL, md);
568 if (r) {
569 dm_put(md);
570 return r;
573 param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
575 r = __dev_status(md, param);
576 dm_put(md);
578 return r;
582 * Always use UUID for lookups if it's present, otherwise use name.
584 static inline struct hash_cell *__find_device_hash_cell(struct dm_ioctl *param)
586 return *param->uuid ?
587 __get_uuid_cell(param->uuid) : __get_name_cell(param->name);
590 static inline struct mapped_device *find_device(struct dm_ioctl *param)
592 struct hash_cell *hc;
593 struct mapped_device *md = NULL;
595 down_read(&_hash_lock);
596 hc = __find_device_hash_cell(param);
597 if (hc) {
598 md = hc->md;
601 * Sneakily write in both the name and the uuid
602 * while we have the cell.
604 strncpy(param->name, hc->name, sizeof(param->name));
605 if (hc->uuid)
606 strncpy(param->uuid, hc->uuid, sizeof(param->uuid)-1);
607 else
608 param->uuid[0] = '\0';
610 if (hc->new_map)
611 param->flags |= DM_INACTIVE_PRESENT_FLAG;
612 else
613 param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
615 dm_get(md);
617 up_read(&_hash_lock);
619 return md;
622 static int dev_remove(struct dm_ioctl *param, size_t param_size)
624 struct hash_cell *hc;
626 down_write(&_hash_lock);
627 hc = __find_device_hash_cell(param);
629 if (!hc) {
630 DMWARN("device doesn't appear to be in the dev hash table.");
631 up_write(&_hash_lock);
632 return -ENXIO;
635 __hash_remove(hc);
636 up_write(&_hash_lock);
637 param->data_size = 0;
638 return 0;
642 * Check a string doesn't overrun the chunk of
643 * memory we copied from userland.
645 static int invalid_str(char *str, void *end)
647 while ((void *) str < end)
648 if (!*str++)
649 return 0;
651 return -EINVAL;
654 static int dev_rename(struct dm_ioctl *param, size_t param_size)
656 int r;
657 char *new_name = (char *) param + param->data_start;
659 if (new_name < (char *) (param + 1) ||
660 invalid_str(new_name, (void *) param + param_size)) {
661 DMWARN("Invalid new logical volume name supplied.");
662 return -EINVAL;
665 r = check_name(new_name);
666 if (r)
667 return r;
669 param->data_size = 0;
670 return dm_hash_rename(param->name, new_name);
673 static int do_suspend(struct dm_ioctl *param)
675 int r = 0;
676 struct mapped_device *md;
678 md = find_device(param);
679 if (!md)
680 return -ENXIO;
682 if (!dm_suspended(md))
683 r = dm_suspend(md);
685 if (!r)
686 r = __dev_status(md, param);
688 dm_put(md);
689 return r;
692 static int do_resume(struct dm_ioctl *param)
694 int r = 0;
695 struct hash_cell *hc;
696 struct mapped_device *md;
697 struct dm_table *new_map;
699 down_write(&_hash_lock);
701 hc = __find_device_hash_cell(param);
702 if (!hc) {
703 DMWARN("device doesn't appear to be in the dev hash table.");
704 up_write(&_hash_lock);
705 return -ENXIO;
708 md = hc->md;
709 dm_get(md);
711 new_map = hc->new_map;
712 hc->new_map = NULL;
713 param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
715 up_write(&_hash_lock);
717 /* Do we need to load a new map ? */
718 if (new_map) {
719 /* Suspend if it isn't already suspended */
720 if (!dm_suspended(md))
721 dm_suspend(md);
723 r = dm_swap_table(md, new_map);
724 if (r) {
725 dm_put(md);
726 dm_table_put(new_map);
727 return r;
730 if (dm_table_get_mode(new_map) & FMODE_WRITE)
731 set_disk_ro(dm_disk(md), 0);
732 else
733 set_disk_ro(dm_disk(md), 1);
735 dm_table_put(new_map);
738 if (dm_suspended(md))
739 r = dm_resume(md);
741 if (!r)
742 r = __dev_status(md, param);
744 dm_put(md);
745 return r;
749 * Set or unset the suspension state of a device.
750 * If the device already is in the requested state we just return its status.
752 static int dev_suspend(struct dm_ioctl *param, size_t param_size)
754 if (param->flags & DM_SUSPEND_FLAG)
755 return do_suspend(param);
757 return do_resume(param);
761 * Copies device info back to user space, used by
762 * the create and info ioctls.
764 static int dev_status(struct dm_ioctl *param, size_t param_size)
766 int r;
767 struct mapped_device *md;
769 md = find_device(param);
770 if (!md)
771 return -ENXIO;
773 r = __dev_status(md, param);
774 dm_put(md);
775 return r;
779 * Build up the status struct for each target
781 static void retrieve_status(struct dm_table *table,
782 struct dm_ioctl *param, size_t param_size)
784 unsigned int i, num_targets;
785 struct dm_target_spec *spec;
786 char *outbuf, *outptr;
787 status_type_t type;
788 size_t remaining, len, used = 0;
790 outptr = outbuf = get_result_buffer(param, param_size, &len);
792 if (param->flags & DM_STATUS_TABLE_FLAG)
793 type = STATUSTYPE_TABLE;
794 else
795 type = STATUSTYPE_INFO;
797 /* Get all the target info */
798 num_targets = dm_table_get_num_targets(table);
799 for (i = 0; i < num_targets; i++) {
800 struct dm_target *ti = dm_table_get_target(table, i);
802 remaining = len - (outptr - outbuf);
803 if (remaining <= sizeof(struct dm_target_spec)) {
804 param->flags |= DM_BUFFER_FULL_FLAG;
805 break;
808 spec = (struct dm_target_spec *) outptr;
810 spec->status = 0;
811 spec->sector_start = ti->begin;
812 spec->length = ti->len;
813 strncpy(spec->target_type, ti->type->name,
814 sizeof(spec->target_type));
816 outptr += sizeof(struct dm_target_spec);
817 remaining = len - (outptr - outbuf);
818 if (remaining <= 0) {
819 param->flags |= DM_BUFFER_FULL_FLAG;
820 break;
823 /* Get the status/table string from the target driver */
824 if (ti->type->status) {
825 if (ti->type->status(ti, type, outptr, remaining)) {
826 param->flags |= DM_BUFFER_FULL_FLAG;
827 break;
829 } else
830 outptr[0] = '\0';
832 outptr += strlen(outptr) + 1;
833 used = param->data_start + (outptr - outbuf);
835 outptr = align_ptr(outptr);
836 spec->next = outptr - outbuf;
839 if (used)
840 param->data_size = used;
842 param->target_count = num_targets;
846 * Wait for a device to report an event
848 static int dev_wait(struct dm_ioctl *param, size_t param_size)
850 int r;
851 struct mapped_device *md;
852 struct dm_table *table;
854 md = find_device(param);
855 if (!md)
856 return -ENXIO;
859 * Wait for a notification event
861 if (dm_wait_event(md, param->event_nr)) {
862 r = -ERESTARTSYS;
863 goto out;
867 * The userland program is going to want to know what
868 * changed to trigger the event, so we may as well tell
869 * him and save an ioctl.
871 r = __dev_status(md, param);
872 if (r)
873 goto out;
875 table = dm_get_table(md);
876 if (table) {
877 retrieve_status(table, param, param_size);
878 dm_table_put(table);
881 out:
882 dm_put(md);
883 return r;
886 static inline int get_mode(struct dm_ioctl *param)
888 int mode = FMODE_READ | FMODE_WRITE;
890 if (param->flags & DM_READONLY_FLAG)
891 mode = FMODE_READ;
893 return mode;
896 static int next_target(struct dm_target_spec *last, uint32_t next, void *end,
897 struct dm_target_spec **spec, char **target_params)
899 *spec = (struct dm_target_spec *) ((unsigned char *) last + next);
900 *target_params = (char *) (*spec + 1);
902 if (*spec < (last + 1))
903 return -EINVAL;
905 return invalid_str(*target_params, end);
908 static int populate_table(struct dm_table *table,
909 struct dm_ioctl *param, size_t param_size)
911 int r;
912 unsigned int i = 0;
913 struct dm_target_spec *spec = (struct dm_target_spec *) param;
914 uint32_t next = param->data_start;
915 void *end = (void *) param + param_size;
916 char *target_params;
918 if (!param->target_count) {
919 DMWARN("populate_table: no targets specified");
920 return -EINVAL;
923 for (i = 0; i < param->target_count; i++) {
925 r = next_target(spec, next, end, &spec, &target_params);
926 if (r) {
927 DMWARN("unable to find target");
928 return r;
931 r = dm_table_add_target(table, spec->target_type,
932 (sector_t) spec->sector_start,
933 (sector_t) spec->length,
934 target_params);
935 if (r) {
936 DMWARN("error adding target to table");
937 return r;
940 next = spec->next;
943 return dm_table_complete(table);
946 static int table_load(struct dm_ioctl *param, size_t param_size)
948 int r;
949 struct hash_cell *hc;
950 struct dm_table *t;
952 r = dm_table_create(&t, get_mode(param), param->target_count);
953 if (r)
954 return r;
956 r = populate_table(t, param, param_size);
957 if (r) {
958 dm_table_put(t);
959 return r;
962 down_write(&_hash_lock);
963 hc = __find_device_hash_cell(param);
964 if (!hc) {
965 DMWARN("device doesn't appear to be in the dev hash table.");
966 up_write(&_hash_lock);
967 return -ENXIO;
970 if (hc->new_map)
971 dm_table_put(hc->new_map);
972 hc->new_map = t;
973 param->flags |= DM_INACTIVE_PRESENT_FLAG;
975 r = __dev_status(hc->md, param);
976 up_write(&_hash_lock);
977 return r;
980 static int table_clear(struct dm_ioctl *param, size_t param_size)
982 int r;
983 struct hash_cell *hc;
985 down_write(&_hash_lock);
987 hc = __find_device_hash_cell(param);
988 if (!hc) {
989 DMWARN("device doesn't appear to be in the dev hash table.");
990 up_write(&_hash_lock);
991 return -ENXIO;
994 if (hc->new_map) {
995 dm_table_put(hc->new_map);
996 hc->new_map = NULL;
999 param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
1001 r = __dev_status(hc->md, param);
1002 up_write(&_hash_lock);
1003 return r;
1007 * Retrieves a list of devices used by a particular dm device.
1009 static void retrieve_deps(struct dm_table *table,
1010 struct dm_ioctl *param, size_t param_size)
1012 unsigned int count = 0;
1013 struct list_head *tmp;
1014 size_t len, needed;
1015 struct dm_dev *dd;
1016 struct dm_target_deps *deps;
1018 deps = get_result_buffer(param, param_size, &len);
1021 * Count the devices.
1023 list_for_each (tmp, dm_table_get_devices(table))
1024 count++;
1027 * Check we have enough space.
1029 needed = sizeof(*deps) + (sizeof(*deps->dev) * count);
1030 if (len < needed) {
1031 param->flags |= DM_BUFFER_FULL_FLAG;
1032 return;
1036 * Fill in the devices.
1038 deps->count = count;
1039 count = 0;
1040 list_for_each_entry (dd, dm_table_get_devices(table), list)
1041 deps->dev[count++] = huge_encode_dev(dd->bdev->bd_dev);
1043 param->data_size = param->data_start + needed;
1046 static int table_deps(struct dm_ioctl *param, size_t param_size)
1048 int r = 0;
1049 struct mapped_device *md;
1050 struct dm_table *table;
1052 md = find_device(param);
1053 if (!md)
1054 return -ENXIO;
1056 r = __dev_status(md, param);
1057 if (r)
1058 goto out;
1060 table = dm_get_table(md);
1061 if (table) {
1062 retrieve_deps(table, param, param_size);
1063 dm_table_put(table);
1066 out:
1067 dm_put(md);
1068 return r;
1072 * Return the status of a device as a text string for each
1073 * target.
1075 static int table_status(struct dm_ioctl *param, size_t param_size)
1077 int r;
1078 struct mapped_device *md;
1079 struct dm_table *table;
1081 md = find_device(param);
1082 if (!md)
1083 return -ENXIO;
1085 r = __dev_status(md, param);
1086 if (r)
1087 goto out;
1089 table = dm_get_table(md);
1090 if (table) {
1091 retrieve_status(table, param, param_size);
1092 dm_table_put(table);
1095 out:
1096 dm_put(md);
1097 return r;
1100 /*-----------------------------------------------------------------
1101 * Implementation of open/close/ioctl on the special char
1102 * device.
1103 *---------------------------------------------------------------*/
1104 static ioctl_fn lookup_ioctl(unsigned int cmd)
1106 static struct {
1107 int cmd;
1108 ioctl_fn fn;
1109 } _ioctls[] = {
1110 {DM_VERSION_CMD, NULL}, /* version is dealt with elsewhere */
1111 {DM_REMOVE_ALL_CMD, remove_all},
1112 {DM_LIST_DEVICES_CMD, list_devices},
1114 {DM_DEV_CREATE_CMD, dev_create},
1115 {DM_DEV_REMOVE_CMD, dev_remove},
1116 {DM_DEV_RENAME_CMD, dev_rename},
1117 {DM_DEV_SUSPEND_CMD, dev_suspend},
1118 {DM_DEV_STATUS_CMD, dev_status},
1119 {DM_DEV_WAIT_CMD, dev_wait},
1121 {DM_TABLE_LOAD_CMD, table_load},
1122 {DM_TABLE_CLEAR_CMD, table_clear},
1123 {DM_TABLE_DEPS_CMD, table_deps},
1124 {DM_TABLE_STATUS_CMD, table_status},
1126 {DM_LIST_VERSIONS_CMD, list_versions}
1129 return (cmd >= ARRAY_SIZE(_ioctls)) ? NULL : _ioctls[cmd].fn;
1133 * As well as checking the version compatibility this always
1134 * copies the kernel interface version out.
1136 static int check_version(unsigned int cmd, struct dm_ioctl __user *user)
1138 uint32_t version[3];
1139 int r = 0;
1141 if (copy_from_user(version, user->version, sizeof(version)))
1142 return -EFAULT;
1144 if ((DM_VERSION_MAJOR != version[0]) ||
1145 (DM_VERSION_MINOR < version[1])) {
1146 DMWARN("ioctl interface mismatch: "
1147 "kernel(%u.%u.%u), user(%u.%u.%u), cmd(%d)",
1148 DM_VERSION_MAJOR, DM_VERSION_MINOR,
1149 DM_VERSION_PATCHLEVEL,
1150 version[0], version[1], version[2], cmd);
1151 r = -EINVAL;
1155 * Fill in the kernel version.
1157 version[0] = DM_VERSION_MAJOR;
1158 version[1] = DM_VERSION_MINOR;
1159 version[2] = DM_VERSION_PATCHLEVEL;
1160 if (copy_to_user(user->version, version, sizeof(version)))
1161 return -EFAULT;
1163 return r;
1166 static void free_params(struct dm_ioctl *param)
1168 vfree(param);
1171 static int copy_params(struct dm_ioctl __user *user, struct dm_ioctl **param)
1173 struct dm_ioctl tmp, *dmi;
1175 if (copy_from_user(&tmp, user, sizeof(tmp)))
1176 return -EFAULT;
1178 if (tmp.data_size < sizeof(tmp))
1179 return -EINVAL;
1181 dmi = (struct dm_ioctl *) vmalloc(tmp.data_size);
1182 if (!dmi)
1183 return -ENOMEM;
1185 if (copy_from_user(dmi, user, tmp.data_size)) {
1186 vfree(dmi);
1187 return -EFAULT;
1190 *param = dmi;
1191 return 0;
1194 static int validate_params(uint cmd, struct dm_ioctl *param)
1196 /* Always clear this flag */
1197 param->flags &= ~DM_BUFFER_FULL_FLAG;
1199 /* Ignores parameters */
1200 if (cmd == DM_REMOVE_ALL_CMD ||
1201 cmd == DM_LIST_DEVICES_CMD ||
1202 cmd == DM_LIST_VERSIONS_CMD)
1203 return 0;
1205 /* Unless creating, either name or uuid but not both */
1206 if (cmd != DM_DEV_CREATE_CMD) {
1207 if ((!*param->uuid && !*param->name) ||
1208 (*param->uuid && *param->name)) {
1209 DMWARN("one of name or uuid must be supplied, cmd(%u)",
1210 cmd);
1211 return -EINVAL;
1215 /* Ensure strings are terminated */
1216 param->name[DM_NAME_LEN - 1] = '\0';
1217 param->uuid[DM_UUID_LEN - 1] = '\0';
1219 return 0;
1222 static int ctl_ioctl(struct inode *inode, struct file *file,
1223 uint command, ulong u)
1225 int r = 0;
1226 unsigned int cmd;
1227 struct dm_ioctl *param;
1228 struct dm_ioctl __user *user = (struct dm_ioctl __user *) u;
1229 ioctl_fn fn = NULL;
1230 size_t param_size;
1232 /* only root can play with this */
1233 if (!capable(CAP_SYS_ADMIN))
1234 return -EACCES;
1236 if (_IOC_TYPE(command) != DM_IOCTL)
1237 return -ENOTTY;
1239 cmd = _IOC_NR(command);
1242 * Check the interface version passed in. This also
1243 * writes out the kernel's interface version.
1245 r = check_version(cmd, user);
1246 if (r)
1247 return r;
1250 * Nothing more to do for the version command.
1252 if (cmd == DM_VERSION_CMD)
1253 return 0;
1255 fn = lookup_ioctl(cmd);
1256 if (!fn) {
1257 DMWARN("dm_ctl_ioctl: unknown command 0x%x", command);
1258 return -ENOTTY;
1262 * Trying to avoid low memory issues when a device is
1263 * suspended.
1265 current->flags |= PF_MEMALLOC;
1268 * Copy the parameters into kernel space.
1270 r = copy_params(user, &param);
1271 if (r) {
1272 current->flags &= ~PF_MEMALLOC;
1273 return r;
1277 * FIXME: eventually we will remove the PF_MEMALLOC flag
1278 * here. However the tools still do nasty things like
1279 * 'load' while a device is suspended.
1282 r = validate_params(cmd, param);
1283 if (r)
1284 goto out;
1286 param_size = param->data_size;
1287 param->data_size = sizeof(*param);
1288 r = fn(param, param_size);
1291 * Copy the results back to userland.
1293 if (!r && copy_to_user(user, param, param->data_size))
1294 r = -EFAULT;
1296 out:
1297 free_params(param);
1298 current->flags &= ~PF_MEMALLOC;
1299 return r;
1302 static struct file_operations _ctl_fops = {
1303 .ioctl = ctl_ioctl,
1304 .owner = THIS_MODULE,
1307 static struct miscdevice _dm_misc = {
1308 .minor = MISC_DYNAMIC_MINOR,
1309 .name = DM_NAME,
1310 .devfs_name = "mapper/control",
1311 .fops = &_ctl_fops
1315 * Create misc character device and link to DM_DIR/control.
1317 int __init dm_interface_init(void)
1319 int r;
1321 r = dm_hash_init();
1322 if (r)
1323 return r;
1325 r = misc_register(&_dm_misc);
1326 if (r) {
1327 DMERR("misc_register failed for control device");
1328 dm_hash_exit();
1329 return r;
1332 DMINFO("%d.%d.%d%s initialised: %s", DM_VERSION_MAJOR,
1333 DM_VERSION_MINOR, DM_VERSION_PATCHLEVEL, DM_VERSION_EXTRA,
1334 DM_DRIVER_EMAIL);
1335 return 0;
1338 void dm_interface_exit(void)
1340 if (misc_deregister(&_dm_misc) < 0)
1341 DMERR("misc_deregister failed for control device");
1343 dm_hash_exit();