[XFRM]: skb_cow_data() does not set proper owner for new skbs.
[linux-2.6/verdex.git] / drivers / md / dm-ioctl.c
blobee3c869d9701933d366dfdf18fc023eb5cef2da8
1 /*
2 * Copyright (C) 2001, 2002 Sistina Software (UK) Limited.
3 * Copyright (C) 2004 - 2005 Red Hat, Inc. All rights reserved.
5 * This file is released under the GPL.
6 */
8 #include "dm.h"
10 #include <linux/module.h>
11 #include <linux/vmalloc.h>
12 #include <linux/miscdevice.h>
13 #include <linux/init.h>
14 #include <linux/wait.h>
15 #include <linux/slab.h>
16 #include <linux/devfs_fs_kernel.h>
17 #include <linux/dm-ioctl.h>
19 #include <asm/uaccess.h>
21 #define DM_DRIVER_EMAIL "dm-devel@redhat.com"
23 /*-----------------------------------------------------------------
24 * The ioctl interface needs to be able to look up devices by
25 * name or uuid.
26 *---------------------------------------------------------------*/
27 struct hash_cell {
28 struct list_head name_list;
29 struct list_head uuid_list;
31 char *name;
32 char *uuid;
33 struct mapped_device *md;
34 struct dm_table *new_map;
37 struct vers_iter {
38 size_t param_size;
39 struct dm_target_versions *vers, *old_vers;
40 char *end;
41 uint32_t flags;
45 #define NUM_BUCKETS 64
46 #define MASK_BUCKETS (NUM_BUCKETS - 1)
47 static struct list_head _name_buckets[NUM_BUCKETS];
48 static struct list_head _uuid_buckets[NUM_BUCKETS];
50 static void dm_hash_remove_all(void);
53 * Guards access to both hash tables.
55 static DECLARE_RWSEM(_hash_lock);
57 static void init_buckets(struct list_head *buckets)
59 unsigned int i;
61 for (i = 0; i < NUM_BUCKETS; i++)
62 INIT_LIST_HEAD(buckets + i);
65 static int dm_hash_init(void)
67 init_buckets(_name_buckets);
68 init_buckets(_uuid_buckets);
69 devfs_mk_dir(DM_DIR);
70 return 0;
73 static void dm_hash_exit(void)
75 dm_hash_remove_all();
76 devfs_remove(DM_DIR);
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 return hc;
107 return NULL;
110 static struct hash_cell *__get_uuid_cell(const char *str)
112 struct hash_cell *hc;
113 unsigned int h = hash_str(str);
115 list_for_each_entry (hc, _uuid_buckets + h, uuid_list)
116 if (!strcmp(hc->uuid, str))
117 return hc;
119 return NULL;
122 /*-----------------------------------------------------------------
123 * Inserting, removing and renaming a device.
124 *---------------------------------------------------------------*/
125 static inline char *kstrdup(const char *str)
127 char *r = kmalloc(strlen(str) + 1, GFP_KERNEL);
128 if (r)
129 strcpy(r, str);
130 return r;
133 static struct hash_cell *alloc_cell(const char *name, const char *uuid,
134 struct mapped_device *md)
136 struct hash_cell *hc;
138 hc = kmalloc(sizeof(*hc), GFP_KERNEL);
139 if (!hc)
140 return NULL;
142 hc->name = kstrdup(name);
143 if (!hc->name) {
144 kfree(hc);
145 return NULL;
148 if (!uuid)
149 hc->uuid = NULL;
151 else {
152 hc->uuid = kstrdup(uuid);
153 if (!hc->uuid) {
154 kfree(hc->name);
155 kfree(hc);
156 return NULL;
160 INIT_LIST_HEAD(&hc->name_list);
161 INIT_LIST_HEAD(&hc->uuid_list);
162 hc->md = md;
163 hc->new_map = NULL;
164 return hc;
167 static void free_cell(struct hash_cell *hc)
169 if (hc) {
170 kfree(hc->name);
171 kfree(hc->uuid);
172 kfree(hc);
177 * devfs stuff.
179 static int register_with_devfs(struct hash_cell *hc)
181 struct gendisk *disk = dm_disk(hc->md);
183 devfs_mk_bdev(MKDEV(disk->major, disk->first_minor),
184 S_IFBLK | S_IRUSR | S_IWUSR | S_IRGRP,
185 DM_DIR "/%s", hc->name);
186 return 0;
189 static int unregister_with_devfs(struct hash_cell *hc)
191 devfs_remove(DM_DIR"/%s", hc->name);
192 return 0;
196 * The kdev_t and uuid of a device can never change once it is
197 * initially inserted.
199 static int dm_hash_insert(const char *name, const char *uuid, struct mapped_device *md)
201 struct hash_cell *cell;
204 * Allocate the new cells.
206 cell = alloc_cell(name, uuid, md);
207 if (!cell)
208 return -ENOMEM;
211 * Insert the cell into both hash tables.
213 down_write(&_hash_lock);
214 if (__get_name_cell(name))
215 goto bad;
217 list_add(&cell->name_list, _name_buckets + hash_str(name));
219 if (uuid) {
220 if (__get_uuid_cell(uuid)) {
221 list_del(&cell->name_list);
222 goto bad;
224 list_add(&cell->uuid_list, _uuid_buckets + hash_str(uuid));
226 register_with_devfs(cell);
227 dm_get(md);
228 dm_set_mdptr(md, cell);
229 up_write(&_hash_lock);
231 return 0;
233 bad:
234 up_write(&_hash_lock);
235 free_cell(cell);
236 return -EBUSY;
239 static void __hash_remove(struct hash_cell *hc)
241 /* remove from the dev hash */
242 list_del(&hc->uuid_list);
243 list_del(&hc->name_list);
244 unregister_with_devfs(hc);
245 dm_set_mdptr(hc->md, NULL);
246 dm_put(hc->md);
247 if (hc->new_map)
248 dm_table_put(hc->new_map);
249 free_cell(hc);
252 static void dm_hash_remove_all(void)
254 int i;
255 struct hash_cell *hc;
256 struct list_head *tmp, *n;
258 down_write(&_hash_lock);
259 for (i = 0; i < NUM_BUCKETS; i++) {
260 list_for_each_safe (tmp, n, _name_buckets + i) {
261 hc = list_entry(tmp, struct hash_cell, name_list);
262 __hash_remove(hc);
265 up_write(&_hash_lock);
268 static int dm_hash_rename(const char *old, const char *new)
270 char *new_name, *old_name;
271 struct hash_cell *hc;
274 * duplicate new.
276 new_name = kstrdup(new);
277 if (!new_name)
278 return -ENOMEM;
280 down_write(&_hash_lock);
283 * Is new free ?
285 hc = __get_name_cell(new);
286 if (hc) {
287 DMWARN("asked to rename to an already existing name %s -> %s",
288 old, new);
289 up_write(&_hash_lock);
290 kfree(new_name);
291 return -EBUSY;
295 * Is there such a device as 'old' ?
297 hc = __get_name_cell(old);
298 if (!hc) {
299 DMWARN("asked to rename a non existent device %s -> %s",
300 old, new);
301 up_write(&_hash_lock);
302 kfree(new_name);
303 return -ENXIO;
307 * rename and move the name cell.
309 unregister_with_devfs(hc);
311 list_del(&hc->name_list);
312 old_name = hc->name;
313 hc->name = new_name;
314 list_add(&hc->name_list, _name_buckets + hash_str(new_name));
316 /* rename the device node in devfs */
317 register_with_devfs(hc);
319 up_write(&_hash_lock);
320 kfree(old_name);
321 return 0;
324 /*-----------------------------------------------------------------
325 * Implementation of the ioctl commands
326 *---------------------------------------------------------------*/
328 * All the ioctl commands get dispatched to functions with this
329 * prototype.
331 typedef int (*ioctl_fn)(struct dm_ioctl *param, size_t param_size);
333 static int remove_all(struct dm_ioctl *param, size_t param_size)
335 dm_hash_remove_all();
336 param->data_size = 0;
337 return 0;
341 * Round up the ptr to an 8-byte boundary.
343 #define ALIGN_MASK 7
344 static inline void *align_ptr(void *ptr)
346 return (void *) (((size_t) (ptr + ALIGN_MASK)) & ~ALIGN_MASK);
350 * Retrieves the data payload buffer from an already allocated
351 * struct dm_ioctl.
353 static void *get_result_buffer(struct dm_ioctl *param, size_t param_size,
354 size_t *len)
356 param->data_start = align_ptr(param + 1) - (void *) param;
358 if (param->data_start < param_size)
359 *len = param_size - param->data_start;
360 else
361 *len = 0;
363 return ((void *) param) + param->data_start;
366 static int list_devices(struct dm_ioctl *param, size_t param_size)
368 unsigned int i;
369 struct hash_cell *hc;
370 size_t len, needed = 0;
371 struct gendisk *disk;
372 struct dm_name_list *nl, *old_nl = NULL;
374 down_write(&_hash_lock);
377 * Loop through all the devices working out how much
378 * space we need.
380 for (i = 0; i < NUM_BUCKETS; i++) {
381 list_for_each_entry (hc, _name_buckets + i, name_list) {
382 needed += sizeof(struct dm_name_list);
383 needed += strlen(hc->name) + 1;
384 needed += ALIGN_MASK;
389 * Grab our output buffer.
391 nl = get_result_buffer(param, param_size, &len);
392 if (len < needed) {
393 param->flags |= DM_BUFFER_FULL_FLAG;
394 goto out;
396 param->data_size = param->data_start + needed;
398 nl->dev = 0; /* Flags no data */
401 * Now loop through filling out the names.
403 for (i = 0; i < NUM_BUCKETS; i++) {
404 list_for_each_entry (hc, _name_buckets + i, name_list) {
405 if (old_nl)
406 old_nl->next = (uint32_t) ((void *) nl -
407 (void *) old_nl);
408 disk = dm_disk(hc->md);
409 nl->dev = huge_encode_dev(MKDEV(disk->major, disk->first_minor));
410 nl->next = 0;
411 strcpy(nl->name, hc->name);
413 old_nl = nl;
414 nl = align_ptr(((void *) ++nl) + strlen(hc->name) + 1);
418 out:
419 up_write(&_hash_lock);
420 return 0;
423 static void list_version_get_needed(struct target_type *tt, void *needed_param)
425 size_t *needed = needed_param;
427 *needed += strlen(tt->name);
428 *needed += sizeof(tt->version);
429 *needed += ALIGN_MASK;
432 static void list_version_get_info(struct target_type *tt, void *param)
434 struct vers_iter *info = param;
436 /* Check space - it might have changed since the first iteration */
437 if ((char *)info->vers + sizeof(tt->version) + strlen(tt->name) + 1 >
438 info->end) {
440 info->flags = DM_BUFFER_FULL_FLAG;
441 return;
444 if (info->old_vers)
445 info->old_vers->next = (uint32_t) ((void *)info->vers -
446 (void *)info->old_vers);
447 info->vers->version[0] = tt->version[0];
448 info->vers->version[1] = tt->version[1];
449 info->vers->version[2] = tt->version[2];
450 info->vers->next = 0;
451 strcpy(info->vers->name, tt->name);
453 info->old_vers = info->vers;
454 info->vers = align_ptr(((void *) ++info->vers) + strlen(tt->name) + 1);
457 static int list_versions(struct dm_ioctl *param, size_t param_size)
459 size_t len, needed = 0;
460 struct dm_target_versions *vers;
461 struct vers_iter iter_info;
464 * Loop through all the devices working out how much
465 * space we need.
467 dm_target_iterate(list_version_get_needed, &needed);
470 * Grab our output buffer.
472 vers = get_result_buffer(param, param_size, &len);
473 if (len < needed) {
474 param->flags |= DM_BUFFER_FULL_FLAG;
475 goto out;
477 param->data_size = param->data_start + needed;
479 iter_info.param_size = param_size;
480 iter_info.old_vers = NULL;
481 iter_info.vers = vers;
482 iter_info.flags = 0;
483 iter_info.end = (char *)vers+len;
486 * Now loop through filling out the names & versions.
488 dm_target_iterate(list_version_get_info, &iter_info);
489 param->flags |= iter_info.flags;
491 out:
492 return 0;
497 static int check_name(const char *name)
499 if (strchr(name, '/')) {
500 DMWARN("invalid device name");
501 return -EINVAL;
504 return 0;
508 * Fills in a dm_ioctl structure, ready for sending back to
509 * userland.
511 static int __dev_status(struct mapped_device *md, struct dm_ioctl *param)
513 struct gendisk *disk = dm_disk(md);
514 struct dm_table *table;
515 struct block_device *bdev;
517 param->flags &= ~(DM_SUSPEND_FLAG | DM_READONLY_FLAG |
518 DM_ACTIVE_PRESENT_FLAG);
520 if (dm_suspended(md))
521 param->flags |= DM_SUSPEND_FLAG;
523 param->dev = huge_encode_dev(MKDEV(disk->major, disk->first_minor));
525 if (!(param->flags & DM_SKIP_BDGET_FLAG)) {
526 bdev = bdget_disk(disk, 0);
527 if (!bdev)
528 return -ENXIO;
531 * Yes, this will be out of date by the time it gets back
532 * to userland, but it is still very useful for
533 * debugging.
535 param->open_count = bdev->bd_openers;
536 bdput(bdev);
537 } else
538 param->open_count = -1;
540 if (disk->policy)
541 param->flags |= DM_READONLY_FLAG;
543 param->event_nr = dm_get_event_nr(md);
545 table = dm_get_table(md);
546 if (table) {
547 param->flags |= DM_ACTIVE_PRESENT_FLAG;
548 param->target_count = dm_table_get_num_targets(table);
549 dm_table_put(table);
550 } else
551 param->target_count = 0;
553 return 0;
556 static int dev_create(struct dm_ioctl *param, size_t param_size)
558 int r;
559 struct mapped_device *md;
561 r = check_name(param->name);
562 if (r)
563 return r;
565 if (param->flags & DM_PERSISTENT_DEV_FLAG)
566 r = dm_create_with_minor(MINOR(huge_decode_dev(param->dev)), &md);
567 else
568 r = dm_create(&md);
570 if (r)
571 return r;
573 r = dm_hash_insert(param->name, *param->uuid ? param->uuid : NULL, md);
574 if (r) {
575 dm_put(md);
576 return r;
579 param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
581 r = __dev_status(md, param);
582 dm_put(md);
584 return r;
588 * Always use UUID for lookups if it's present, otherwise use name or dev.
590 static inline struct hash_cell *__find_device_hash_cell(struct dm_ioctl *param)
592 if (*param->uuid)
593 return __get_uuid_cell(param->uuid);
594 else if (*param->name)
595 return __get_name_cell(param->name);
596 else
597 return dm_get_mdptr(huge_decode_dev(param->dev));
600 static inline struct mapped_device *find_device(struct dm_ioctl *param)
602 struct hash_cell *hc;
603 struct mapped_device *md = NULL;
605 down_read(&_hash_lock);
606 hc = __find_device_hash_cell(param);
607 if (hc) {
608 md = hc->md;
609 dm_get(md);
612 * Sneakily write in both the name and the uuid
613 * while we have the cell.
615 strncpy(param->name, hc->name, sizeof(param->name));
616 if (hc->uuid)
617 strncpy(param->uuid, hc->uuid, sizeof(param->uuid)-1);
618 else
619 param->uuid[0] = '\0';
621 if (hc->new_map)
622 param->flags |= DM_INACTIVE_PRESENT_FLAG;
623 else
624 param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
626 up_read(&_hash_lock);
628 return md;
631 static int dev_remove(struct dm_ioctl *param, size_t param_size)
633 struct hash_cell *hc;
635 down_write(&_hash_lock);
636 hc = __find_device_hash_cell(param);
638 if (!hc) {
639 DMWARN("device doesn't appear to be in the dev hash table.");
640 up_write(&_hash_lock);
641 return -ENXIO;
644 __hash_remove(hc);
645 up_write(&_hash_lock);
646 param->data_size = 0;
647 return 0;
651 * Check a string doesn't overrun the chunk of
652 * memory we copied from userland.
654 static int invalid_str(char *str, void *end)
656 while ((void *) str < end)
657 if (!*str++)
658 return 0;
660 return -EINVAL;
663 static int dev_rename(struct dm_ioctl *param, size_t param_size)
665 int r;
666 char *new_name = (char *) param + param->data_start;
668 if (new_name < (char *) (param + 1) ||
669 invalid_str(new_name, (void *) param + param_size)) {
670 DMWARN("Invalid new logical volume name supplied.");
671 return -EINVAL;
674 r = check_name(new_name);
675 if (r)
676 return r;
678 param->data_size = 0;
679 return dm_hash_rename(param->name, new_name);
682 static int do_suspend(struct dm_ioctl *param)
684 int r = 0;
685 struct mapped_device *md;
687 md = find_device(param);
688 if (!md)
689 return -ENXIO;
691 if (!dm_suspended(md))
692 r = dm_suspend(md);
694 if (!r)
695 r = __dev_status(md, param);
697 dm_put(md);
698 return r;
701 static int do_resume(struct dm_ioctl *param)
703 int r = 0;
704 struct hash_cell *hc;
705 struct mapped_device *md;
706 struct dm_table *new_map;
708 down_write(&_hash_lock);
710 hc = __find_device_hash_cell(param);
711 if (!hc) {
712 DMWARN("device doesn't appear to be in the dev hash table.");
713 up_write(&_hash_lock);
714 return -ENXIO;
717 md = hc->md;
718 dm_get(md);
720 new_map = hc->new_map;
721 hc->new_map = NULL;
722 param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
724 up_write(&_hash_lock);
726 /* Do we need to load a new map ? */
727 if (new_map) {
728 /* Suspend if it isn't already suspended */
729 if (!dm_suspended(md))
730 dm_suspend(md);
732 r = dm_swap_table(md, new_map);
733 if (r) {
734 dm_put(md);
735 dm_table_put(new_map);
736 return r;
739 if (dm_table_get_mode(new_map) & FMODE_WRITE)
740 set_disk_ro(dm_disk(md), 0);
741 else
742 set_disk_ro(dm_disk(md), 1);
744 dm_table_put(new_map);
747 if (dm_suspended(md))
748 r = dm_resume(md);
750 if (!r)
751 r = __dev_status(md, param);
753 dm_put(md);
754 return r;
758 * Set or unset the suspension state of a device.
759 * If the device already is in the requested state we just return its status.
761 static int dev_suspend(struct dm_ioctl *param, size_t param_size)
763 if (param->flags & DM_SUSPEND_FLAG)
764 return do_suspend(param);
766 return do_resume(param);
770 * Copies device info back to user space, used by
771 * the create and info ioctls.
773 static int dev_status(struct dm_ioctl *param, size_t param_size)
775 int r;
776 struct mapped_device *md;
778 md = find_device(param);
779 if (!md)
780 return -ENXIO;
782 r = __dev_status(md, param);
783 dm_put(md);
784 return r;
788 * Build up the status struct for each target
790 static void retrieve_status(struct dm_table *table,
791 struct dm_ioctl *param, size_t param_size)
793 unsigned int i, num_targets;
794 struct dm_target_spec *spec;
795 char *outbuf, *outptr;
796 status_type_t type;
797 size_t remaining, len, used = 0;
799 outptr = outbuf = get_result_buffer(param, param_size, &len);
801 if (param->flags & DM_STATUS_TABLE_FLAG)
802 type = STATUSTYPE_TABLE;
803 else
804 type = STATUSTYPE_INFO;
806 /* Get all the target info */
807 num_targets = dm_table_get_num_targets(table);
808 for (i = 0; i < num_targets; i++) {
809 struct dm_target *ti = dm_table_get_target(table, i);
811 remaining = len - (outptr - outbuf);
812 if (remaining <= sizeof(struct dm_target_spec)) {
813 param->flags |= DM_BUFFER_FULL_FLAG;
814 break;
817 spec = (struct dm_target_spec *) outptr;
819 spec->status = 0;
820 spec->sector_start = ti->begin;
821 spec->length = ti->len;
822 strncpy(spec->target_type, ti->type->name,
823 sizeof(spec->target_type));
825 outptr += sizeof(struct dm_target_spec);
826 remaining = len - (outptr - outbuf);
827 if (remaining <= 0) {
828 param->flags |= DM_BUFFER_FULL_FLAG;
829 break;
832 /* Get the status/table string from the target driver */
833 if (ti->type->status) {
834 if (ti->type->status(ti, type, outptr, remaining)) {
835 param->flags |= DM_BUFFER_FULL_FLAG;
836 break;
838 } else
839 outptr[0] = '\0';
841 outptr += strlen(outptr) + 1;
842 used = param->data_start + (outptr - outbuf);
844 outptr = align_ptr(outptr);
845 spec->next = outptr - outbuf;
848 if (used)
849 param->data_size = used;
851 param->target_count = num_targets;
855 * Wait for a device to report an event
857 static int dev_wait(struct dm_ioctl *param, size_t param_size)
859 int r;
860 struct mapped_device *md;
861 struct dm_table *table;
863 md = find_device(param);
864 if (!md)
865 return -ENXIO;
868 * Wait for a notification event
870 if (dm_wait_event(md, param->event_nr)) {
871 r = -ERESTARTSYS;
872 goto out;
876 * The userland program is going to want to know what
877 * changed to trigger the event, so we may as well tell
878 * him and save an ioctl.
880 r = __dev_status(md, param);
881 if (r)
882 goto out;
884 table = dm_get_table(md);
885 if (table) {
886 retrieve_status(table, param, param_size);
887 dm_table_put(table);
890 out:
891 dm_put(md);
892 return r;
895 static inline int get_mode(struct dm_ioctl *param)
897 int mode = FMODE_READ | FMODE_WRITE;
899 if (param->flags & DM_READONLY_FLAG)
900 mode = FMODE_READ;
902 return mode;
905 static int next_target(struct dm_target_spec *last, uint32_t next, void *end,
906 struct dm_target_spec **spec, char **target_params)
908 *spec = (struct dm_target_spec *) ((unsigned char *) last + next);
909 *target_params = (char *) (*spec + 1);
911 if (*spec < (last + 1))
912 return -EINVAL;
914 return invalid_str(*target_params, end);
917 static int populate_table(struct dm_table *table,
918 struct dm_ioctl *param, size_t param_size)
920 int r;
921 unsigned int i = 0;
922 struct dm_target_spec *spec = (struct dm_target_spec *) param;
923 uint32_t next = param->data_start;
924 void *end = (void *) param + param_size;
925 char *target_params;
927 if (!param->target_count) {
928 DMWARN("populate_table: no targets specified");
929 return -EINVAL;
932 for (i = 0; i < param->target_count; i++) {
934 r = next_target(spec, next, end, &spec, &target_params);
935 if (r) {
936 DMWARN("unable to find target");
937 return r;
940 r = dm_table_add_target(table, spec->target_type,
941 (sector_t) spec->sector_start,
942 (sector_t) spec->length,
943 target_params);
944 if (r) {
945 DMWARN("error adding target to table");
946 return r;
949 next = spec->next;
952 return dm_table_complete(table);
955 static int table_load(struct dm_ioctl *param, size_t param_size)
957 int r;
958 struct hash_cell *hc;
959 struct dm_table *t;
961 r = dm_table_create(&t, get_mode(param), param->target_count);
962 if (r)
963 return r;
965 r = populate_table(t, param, param_size);
966 if (r) {
967 dm_table_put(t);
968 return r;
971 down_write(&_hash_lock);
972 hc = __find_device_hash_cell(param);
973 if (!hc) {
974 DMWARN("device doesn't appear to be in the dev hash table.");
975 up_write(&_hash_lock);
976 return -ENXIO;
979 if (hc->new_map)
980 dm_table_put(hc->new_map);
981 hc->new_map = t;
982 param->flags |= DM_INACTIVE_PRESENT_FLAG;
984 r = __dev_status(hc->md, param);
985 up_write(&_hash_lock);
986 return r;
989 static int table_clear(struct dm_ioctl *param, size_t param_size)
991 int r;
992 struct hash_cell *hc;
994 down_write(&_hash_lock);
996 hc = __find_device_hash_cell(param);
997 if (!hc) {
998 DMWARN("device doesn't appear to be in the dev hash table.");
999 up_write(&_hash_lock);
1000 return -ENXIO;
1003 if (hc->new_map) {
1004 dm_table_put(hc->new_map);
1005 hc->new_map = NULL;
1008 param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
1010 r = __dev_status(hc->md, param);
1011 up_write(&_hash_lock);
1012 return r;
1016 * Retrieves a list of devices used by a particular dm device.
1018 static void retrieve_deps(struct dm_table *table,
1019 struct dm_ioctl *param, size_t param_size)
1021 unsigned int count = 0;
1022 struct list_head *tmp;
1023 size_t len, needed;
1024 struct dm_dev *dd;
1025 struct dm_target_deps *deps;
1027 deps = get_result_buffer(param, param_size, &len);
1030 * Count the devices.
1032 list_for_each (tmp, dm_table_get_devices(table))
1033 count++;
1036 * Check we have enough space.
1038 needed = sizeof(*deps) + (sizeof(*deps->dev) * count);
1039 if (len < needed) {
1040 param->flags |= DM_BUFFER_FULL_FLAG;
1041 return;
1045 * Fill in the devices.
1047 deps->count = count;
1048 count = 0;
1049 list_for_each_entry (dd, dm_table_get_devices(table), list)
1050 deps->dev[count++] = huge_encode_dev(dd->bdev->bd_dev);
1052 param->data_size = param->data_start + needed;
1055 static int table_deps(struct dm_ioctl *param, size_t param_size)
1057 int r = 0;
1058 struct mapped_device *md;
1059 struct dm_table *table;
1061 md = find_device(param);
1062 if (!md)
1063 return -ENXIO;
1065 r = __dev_status(md, param);
1066 if (r)
1067 goto out;
1069 table = dm_get_table(md);
1070 if (table) {
1071 retrieve_deps(table, param, param_size);
1072 dm_table_put(table);
1075 out:
1076 dm_put(md);
1077 return r;
1081 * Return the status of a device as a text string for each
1082 * target.
1084 static int table_status(struct dm_ioctl *param, size_t param_size)
1086 int r;
1087 struct mapped_device *md;
1088 struct dm_table *table;
1090 md = find_device(param);
1091 if (!md)
1092 return -ENXIO;
1094 r = __dev_status(md, param);
1095 if (r)
1096 goto out;
1098 table = dm_get_table(md);
1099 if (table) {
1100 retrieve_status(table, param, param_size);
1101 dm_table_put(table);
1104 out:
1105 dm_put(md);
1106 return r;
1110 * Pass a message to the target that's at the supplied device offset.
1112 static int target_message(struct dm_ioctl *param, size_t param_size)
1114 int r, argc;
1115 char **argv;
1116 struct mapped_device *md;
1117 struct dm_table *table;
1118 struct dm_target *ti;
1119 struct dm_target_msg *tmsg = (void *) param + param->data_start;
1121 md = find_device(param);
1122 if (!md)
1123 return -ENXIO;
1125 r = __dev_status(md, param);
1126 if (r)
1127 goto out;
1129 if (tmsg < (struct dm_target_msg *) (param + 1) ||
1130 invalid_str(tmsg->message, (void *) param + param_size)) {
1131 DMWARN("Invalid target message parameters.");
1132 r = -EINVAL;
1133 goto out;
1136 r = dm_split_args(&argc, &argv, tmsg->message);
1137 if (r) {
1138 DMWARN("Failed to split target message parameters");
1139 goto out;
1142 table = dm_get_table(md);
1143 if (!table)
1144 goto out_argv;
1146 if (tmsg->sector >= dm_table_get_size(table)) {
1147 DMWARN("Target message sector outside device.");
1148 r = -EINVAL;
1149 goto out_table;
1152 ti = dm_table_find_target(table, tmsg->sector);
1153 if (ti->type->message)
1154 r = ti->type->message(ti, argc, argv);
1155 else {
1156 DMWARN("Target type does not support messages");
1157 r = -EINVAL;
1160 out_table:
1161 dm_table_put(table);
1162 out_argv:
1163 kfree(argv);
1164 out:
1165 param->data_size = 0;
1166 dm_put(md);
1167 return r;
1170 /*-----------------------------------------------------------------
1171 * Implementation of open/close/ioctl on the special char
1172 * device.
1173 *---------------------------------------------------------------*/
1174 static ioctl_fn lookup_ioctl(unsigned int cmd)
1176 static struct {
1177 int cmd;
1178 ioctl_fn fn;
1179 } _ioctls[] = {
1180 {DM_VERSION_CMD, NULL}, /* version is dealt with elsewhere */
1181 {DM_REMOVE_ALL_CMD, remove_all},
1182 {DM_LIST_DEVICES_CMD, list_devices},
1184 {DM_DEV_CREATE_CMD, dev_create},
1185 {DM_DEV_REMOVE_CMD, dev_remove},
1186 {DM_DEV_RENAME_CMD, dev_rename},
1187 {DM_DEV_SUSPEND_CMD, dev_suspend},
1188 {DM_DEV_STATUS_CMD, dev_status},
1189 {DM_DEV_WAIT_CMD, dev_wait},
1191 {DM_TABLE_LOAD_CMD, table_load},
1192 {DM_TABLE_CLEAR_CMD, table_clear},
1193 {DM_TABLE_DEPS_CMD, table_deps},
1194 {DM_TABLE_STATUS_CMD, table_status},
1196 {DM_LIST_VERSIONS_CMD, list_versions},
1198 {DM_TARGET_MSG_CMD, target_message}
1201 return (cmd >= ARRAY_SIZE(_ioctls)) ? NULL : _ioctls[cmd].fn;
1205 * As well as checking the version compatibility this always
1206 * copies the kernel interface version out.
1208 static int check_version(unsigned int cmd, struct dm_ioctl __user *user)
1210 uint32_t version[3];
1211 int r = 0;
1213 if (copy_from_user(version, user->version, sizeof(version)))
1214 return -EFAULT;
1216 if ((DM_VERSION_MAJOR != version[0]) ||
1217 (DM_VERSION_MINOR < version[1])) {
1218 DMWARN("ioctl interface mismatch: "
1219 "kernel(%u.%u.%u), user(%u.%u.%u), cmd(%d)",
1220 DM_VERSION_MAJOR, DM_VERSION_MINOR,
1221 DM_VERSION_PATCHLEVEL,
1222 version[0], version[1], version[2], cmd);
1223 r = -EINVAL;
1227 * Fill in the kernel version.
1229 version[0] = DM_VERSION_MAJOR;
1230 version[1] = DM_VERSION_MINOR;
1231 version[2] = DM_VERSION_PATCHLEVEL;
1232 if (copy_to_user(user->version, version, sizeof(version)))
1233 return -EFAULT;
1235 return r;
1238 static void free_params(struct dm_ioctl *param)
1240 vfree(param);
1243 static int copy_params(struct dm_ioctl __user *user, struct dm_ioctl **param)
1245 struct dm_ioctl tmp, *dmi;
1247 if (copy_from_user(&tmp, user, sizeof(tmp)))
1248 return -EFAULT;
1250 if (tmp.data_size < sizeof(tmp))
1251 return -EINVAL;
1253 dmi = (struct dm_ioctl *) vmalloc(tmp.data_size);
1254 if (!dmi)
1255 return -ENOMEM;
1257 if (copy_from_user(dmi, user, tmp.data_size)) {
1258 vfree(dmi);
1259 return -EFAULT;
1262 *param = dmi;
1263 return 0;
1266 static int validate_params(uint cmd, struct dm_ioctl *param)
1268 /* Always clear this flag */
1269 param->flags &= ~DM_BUFFER_FULL_FLAG;
1271 /* Ignores parameters */
1272 if (cmd == DM_REMOVE_ALL_CMD ||
1273 cmd == DM_LIST_DEVICES_CMD ||
1274 cmd == DM_LIST_VERSIONS_CMD)
1275 return 0;
1277 if ((cmd == DM_DEV_CREATE_CMD)) {
1278 if (!*param->name) {
1279 DMWARN("name not supplied when creating device");
1280 return -EINVAL;
1282 } else if ((*param->uuid && *param->name)) {
1283 DMWARN("only supply one of name or uuid, cmd(%u)", cmd);
1284 return -EINVAL;
1287 /* Ensure strings are terminated */
1288 param->name[DM_NAME_LEN - 1] = '\0';
1289 param->uuid[DM_UUID_LEN - 1] = '\0';
1291 return 0;
1294 static int ctl_ioctl(struct inode *inode, struct file *file,
1295 uint command, ulong u)
1297 int r = 0;
1298 unsigned int cmd;
1299 struct dm_ioctl *param;
1300 struct dm_ioctl __user *user = (struct dm_ioctl __user *) u;
1301 ioctl_fn fn = NULL;
1302 size_t param_size;
1304 /* only root can play with this */
1305 if (!capable(CAP_SYS_ADMIN))
1306 return -EACCES;
1308 if (_IOC_TYPE(command) != DM_IOCTL)
1309 return -ENOTTY;
1311 cmd = _IOC_NR(command);
1314 * Check the interface version passed in. This also
1315 * writes out the kernel's interface version.
1317 r = check_version(cmd, user);
1318 if (r)
1319 return r;
1322 * Nothing more to do for the version command.
1324 if (cmd == DM_VERSION_CMD)
1325 return 0;
1327 fn = lookup_ioctl(cmd);
1328 if (!fn) {
1329 DMWARN("dm_ctl_ioctl: unknown command 0x%x", command);
1330 return -ENOTTY;
1334 * Trying to avoid low memory issues when a device is
1335 * suspended.
1337 current->flags |= PF_MEMALLOC;
1340 * Copy the parameters into kernel space.
1342 r = copy_params(user, &param);
1343 if (r) {
1344 current->flags &= ~PF_MEMALLOC;
1345 return r;
1349 * FIXME: eventually we will remove the PF_MEMALLOC flag
1350 * here. However the tools still do nasty things like
1351 * 'load' while a device is suspended.
1354 r = validate_params(cmd, param);
1355 if (r)
1356 goto out;
1358 param_size = param->data_size;
1359 param->data_size = sizeof(*param);
1360 r = fn(param, param_size);
1363 * Copy the results back to userland.
1365 if (!r && copy_to_user(user, param, param->data_size))
1366 r = -EFAULT;
1368 out:
1369 free_params(param);
1370 current->flags &= ~PF_MEMALLOC;
1371 return r;
1374 static struct file_operations _ctl_fops = {
1375 .ioctl = ctl_ioctl,
1376 .owner = THIS_MODULE,
1379 static struct miscdevice _dm_misc = {
1380 .minor = MISC_DYNAMIC_MINOR,
1381 .name = DM_NAME,
1382 .devfs_name = "mapper/control",
1383 .fops = &_ctl_fops
1387 * Create misc character device and link to DM_DIR/control.
1389 int __init dm_interface_init(void)
1391 int r;
1393 r = dm_hash_init();
1394 if (r)
1395 return r;
1397 r = misc_register(&_dm_misc);
1398 if (r) {
1399 DMERR("misc_register failed for control device");
1400 dm_hash_exit();
1401 return r;
1404 DMINFO("%d.%d.%d%s initialised: %s", DM_VERSION_MAJOR,
1405 DM_VERSION_MINOR, DM_VERSION_PATCHLEVEL, DM_VERSION_EXTRA,
1406 DM_DRIVER_EMAIL);
1407 return 0;
1410 void dm_interface_exit(void)
1412 if (misc_deregister(&_dm_misc) < 0)
1413 DMERR("misc_deregister failed for control device");
1415 dm_hash_exit();