[PATCH] dm: fix mapped device ref counting
[linux-2.6/verdex.git] / drivers / md / dm-ioctl.c
blobf7e743691aa8198d03293e8e04b9195c54c757fa
1 /*
2 * Copyright (C) 2001, 2002 Sistina Software (UK) Limited.
3 * Copyright (C) 2004 - 2005 Red Hat, Inc. All rights reserved.
5 * This file is released under the GPL.
6 */
8 #include "dm.h"
10 #include <linux/module.h>
11 #include <linux/vmalloc.h>
12 #include <linux/miscdevice.h>
13 #include <linux/init.h>
14 #include <linux/wait.h>
15 #include <linux/slab.h>
16 #include <linux/devfs_fs_kernel.h>
17 #include <linux/dm-ioctl.h>
18 #include <linux/hdreg.h>
20 #include <asm/uaccess.h>
22 #define DM_DRIVER_EMAIL "dm-devel@redhat.com"
24 /*-----------------------------------------------------------------
25 * The ioctl interface needs to be able to look up devices by
26 * name or uuid.
27 *---------------------------------------------------------------*/
28 struct hash_cell {
29 struct list_head name_list;
30 struct list_head uuid_list;
32 char *name;
33 char *uuid;
34 struct mapped_device *md;
35 struct dm_table *new_map;
38 struct vers_iter {
39 size_t param_size;
40 struct dm_target_versions *vers, *old_vers;
41 char *end;
42 uint32_t flags;
46 #define NUM_BUCKETS 64
47 #define MASK_BUCKETS (NUM_BUCKETS - 1)
48 static struct list_head _name_buckets[NUM_BUCKETS];
49 static struct list_head _uuid_buckets[NUM_BUCKETS];
51 static void dm_hash_remove_all(void);
54 * Guards access to both hash tables.
56 static DECLARE_RWSEM(_hash_lock);
58 static void init_buckets(struct list_head *buckets)
60 unsigned int i;
62 for (i = 0; i < NUM_BUCKETS; i++)
63 INIT_LIST_HEAD(buckets + i);
66 static int dm_hash_init(void)
68 init_buckets(_name_buckets);
69 init_buckets(_uuid_buckets);
70 devfs_mk_dir(DM_DIR);
71 return 0;
74 static void dm_hash_exit(void)
76 dm_hash_remove_all();
77 devfs_remove(DM_DIR);
80 /*-----------------------------------------------------------------
81 * Hash function:
82 * We're not really concerned with the str hash function being
83 * fast since it's only used by the ioctl interface.
84 *---------------------------------------------------------------*/
85 static unsigned int hash_str(const char *str)
87 const unsigned int hash_mult = 2654435387U;
88 unsigned int h = 0;
90 while (*str)
91 h = (h + (unsigned int) *str++) * hash_mult;
93 return h & MASK_BUCKETS;
96 /*-----------------------------------------------------------------
97 * Code for looking up a device by name
98 *---------------------------------------------------------------*/
99 static struct hash_cell *__get_name_cell(const char *str)
101 struct hash_cell *hc;
102 unsigned int h = hash_str(str);
104 list_for_each_entry (hc, _name_buckets + h, name_list)
105 if (!strcmp(hc->name, str)) {
106 dm_get(hc->md);
107 return hc;
110 return NULL;
113 static struct hash_cell *__get_uuid_cell(const char *str)
115 struct hash_cell *hc;
116 unsigned int h = hash_str(str);
118 list_for_each_entry (hc, _uuid_buckets + h, uuid_list)
119 if (!strcmp(hc->uuid, str)) {
120 dm_get(hc->md);
121 return hc;
124 return NULL;
127 /*-----------------------------------------------------------------
128 * Inserting, removing and renaming a device.
129 *---------------------------------------------------------------*/
130 static struct hash_cell *alloc_cell(const char *name, const char *uuid,
131 struct mapped_device *md)
133 struct hash_cell *hc;
135 hc = kmalloc(sizeof(*hc), GFP_KERNEL);
136 if (!hc)
137 return NULL;
139 hc->name = kstrdup(name, GFP_KERNEL);
140 if (!hc->name) {
141 kfree(hc);
142 return NULL;
145 if (!uuid)
146 hc->uuid = NULL;
148 else {
149 hc->uuid = kstrdup(uuid, GFP_KERNEL);
150 if (!hc->uuid) {
151 kfree(hc->name);
152 kfree(hc);
153 return NULL;
157 INIT_LIST_HEAD(&hc->name_list);
158 INIT_LIST_HEAD(&hc->uuid_list);
159 hc->md = md;
160 hc->new_map = NULL;
161 return hc;
164 static void free_cell(struct hash_cell *hc)
166 if (hc) {
167 kfree(hc->name);
168 kfree(hc->uuid);
169 kfree(hc);
174 * devfs stuff.
176 static int register_with_devfs(struct hash_cell *hc)
178 struct gendisk *disk = dm_disk(hc->md);
180 devfs_mk_bdev(MKDEV(disk->major, disk->first_minor),
181 S_IFBLK | S_IRUSR | S_IWUSR | S_IRGRP,
182 DM_DIR "/%s", hc->name);
183 return 0;
186 static int unregister_with_devfs(struct hash_cell *hc)
188 devfs_remove(DM_DIR"/%s", hc->name);
189 return 0;
193 * The kdev_t and uuid of a device can never change once it is
194 * initially inserted.
196 static int dm_hash_insert(const char *name, const char *uuid, struct mapped_device *md)
198 struct hash_cell *cell, *hc;
201 * Allocate the new cells.
203 cell = alloc_cell(name, uuid, md);
204 if (!cell)
205 return -ENOMEM;
208 * Insert the cell into both hash tables.
210 down_write(&_hash_lock);
211 hc = __get_name_cell(name);
212 if (hc) {
213 dm_put(hc->md);
214 goto bad;
217 list_add(&cell->name_list, _name_buckets + hash_str(name));
219 if (uuid) {
220 hc = __get_uuid_cell(uuid);
221 if (hc) {
222 list_del(&cell->name_list);
223 dm_put(hc->md);
224 goto bad;
226 list_add(&cell->uuid_list, _uuid_buckets + hash_str(uuid));
228 register_with_devfs(cell);
229 dm_get(md);
230 dm_set_mdptr(md, cell);
231 up_write(&_hash_lock);
233 return 0;
235 bad:
236 up_write(&_hash_lock);
237 free_cell(cell);
238 return -EBUSY;
241 static void __hash_remove(struct hash_cell *hc)
243 struct dm_table *table;
245 /* remove from the dev hash */
246 list_del(&hc->uuid_list);
247 list_del(&hc->name_list);
248 unregister_with_devfs(hc);
249 dm_set_mdptr(hc->md, NULL);
251 table = dm_get_table(hc->md);
252 if (table) {
253 dm_table_event(table);
254 dm_table_put(table);
257 if (hc->new_map)
258 dm_table_put(hc->new_map);
259 dm_put(hc->md);
260 free_cell(hc);
263 static void dm_hash_remove_all(void)
265 int i;
266 struct hash_cell *hc;
267 struct list_head *tmp, *n;
269 down_write(&_hash_lock);
270 for (i = 0; i < NUM_BUCKETS; i++) {
271 list_for_each_safe (tmp, n, _name_buckets + i) {
272 hc = list_entry(tmp, struct hash_cell, name_list);
273 __hash_remove(hc);
276 up_write(&_hash_lock);
279 static int dm_hash_rename(const char *old, const char *new)
281 char *new_name, *old_name;
282 struct hash_cell *hc;
283 struct dm_table *table;
286 * duplicate new.
288 new_name = kstrdup(new, GFP_KERNEL);
289 if (!new_name)
290 return -ENOMEM;
292 down_write(&_hash_lock);
295 * Is new free ?
297 hc = __get_name_cell(new);
298 if (hc) {
299 DMWARN("asked to rename to an already existing name %s -> %s",
300 old, new);
301 dm_put(hc->md);
302 up_write(&_hash_lock);
303 kfree(new_name);
304 return -EBUSY;
308 * Is there such a device as 'old' ?
310 hc = __get_name_cell(old);
311 if (!hc) {
312 DMWARN("asked to rename a non existent device %s -> %s",
313 old, new);
314 up_write(&_hash_lock);
315 kfree(new_name);
316 return -ENXIO;
320 * rename and move the name cell.
322 unregister_with_devfs(hc);
324 list_del(&hc->name_list);
325 old_name = hc->name;
326 hc->name = new_name;
327 list_add(&hc->name_list, _name_buckets + hash_str(new_name));
329 /* rename the device node in devfs */
330 register_with_devfs(hc);
333 * Wake up any dm event waiters.
335 table = dm_get_table(hc->md);
336 if (table) {
337 dm_table_event(table);
338 dm_table_put(table);
341 dm_put(hc->md);
342 up_write(&_hash_lock);
343 kfree(old_name);
344 return 0;
347 /*-----------------------------------------------------------------
348 * Implementation of the ioctl commands
349 *---------------------------------------------------------------*/
351 * All the ioctl commands get dispatched to functions with this
352 * prototype.
354 typedef int (*ioctl_fn)(struct dm_ioctl *param, size_t param_size);
356 static int remove_all(struct dm_ioctl *param, size_t param_size)
358 dm_hash_remove_all();
359 param->data_size = 0;
360 return 0;
364 * Round up the ptr to an 8-byte boundary.
366 #define ALIGN_MASK 7
367 static inline void *align_ptr(void *ptr)
369 return (void *) (((size_t) (ptr + ALIGN_MASK)) & ~ALIGN_MASK);
373 * Retrieves the data payload buffer from an already allocated
374 * struct dm_ioctl.
376 static void *get_result_buffer(struct dm_ioctl *param, size_t param_size,
377 size_t *len)
379 param->data_start = align_ptr(param + 1) - (void *) param;
381 if (param->data_start < param_size)
382 *len = param_size - param->data_start;
383 else
384 *len = 0;
386 return ((void *) param) + param->data_start;
389 static int list_devices(struct dm_ioctl *param, size_t param_size)
391 unsigned int i;
392 struct hash_cell *hc;
393 size_t len, needed = 0;
394 struct gendisk *disk;
395 struct dm_name_list *nl, *old_nl = NULL;
397 down_write(&_hash_lock);
400 * Loop through all the devices working out how much
401 * space we need.
403 for (i = 0; i < NUM_BUCKETS; i++) {
404 list_for_each_entry (hc, _name_buckets + i, name_list) {
405 needed += sizeof(struct dm_name_list);
406 needed += strlen(hc->name) + 1;
407 needed += ALIGN_MASK;
412 * Grab our output buffer.
414 nl = get_result_buffer(param, param_size, &len);
415 if (len < needed) {
416 param->flags |= DM_BUFFER_FULL_FLAG;
417 goto out;
419 param->data_size = param->data_start + needed;
421 nl->dev = 0; /* Flags no data */
424 * Now loop through filling out the names.
426 for (i = 0; i < NUM_BUCKETS; i++) {
427 list_for_each_entry (hc, _name_buckets + i, name_list) {
428 if (old_nl)
429 old_nl->next = (uint32_t) ((void *) nl -
430 (void *) old_nl);
431 disk = dm_disk(hc->md);
432 nl->dev = huge_encode_dev(MKDEV(disk->major, disk->first_minor));
433 nl->next = 0;
434 strcpy(nl->name, hc->name);
436 old_nl = nl;
437 nl = align_ptr(((void *) ++nl) + strlen(hc->name) + 1);
441 out:
442 up_write(&_hash_lock);
443 return 0;
446 static void list_version_get_needed(struct target_type *tt, void *needed_param)
448 size_t *needed = needed_param;
450 *needed += sizeof(struct dm_target_versions);
451 *needed += strlen(tt->name);
452 *needed += ALIGN_MASK;
455 static void list_version_get_info(struct target_type *tt, void *param)
457 struct vers_iter *info = param;
459 /* Check space - it might have changed since the first iteration */
460 if ((char *)info->vers + sizeof(tt->version) + strlen(tt->name) + 1 >
461 info->end) {
463 info->flags = DM_BUFFER_FULL_FLAG;
464 return;
467 if (info->old_vers)
468 info->old_vers->next = (uint32_t) ((void *)info->vers -
469 (void *)info->old_vers);
470 info->vers->version[0] = tt->version[0];
471 info->vers->version[1] = tt->version[1];
472 info->vers->version[2] = tt->version[2];
473 info->vers->next = 0;
474 strcpy(info->vers->name, tt->name);
476 info->old_vers = info->vers;
477 info->vers = align_ptr(((void *) ++info->vers) + strlen(tt->name) + 1);
480 static int list_versions(struct dm_ioctl *param, size_t param_size)
482 size_t len, needed = 0;
483 struct dm_target_versions *vers;
484 struct vers_iter iter_info;
487 * Loop through all the devices working out how much
488 * space we need.
490 dm_target_iterate(list_version_get_needed, &needed);
493 * Grab our output buffer.
495 vers = get_result_buffer(param, param_size, &len);
496 if (len < needed) {
497 param->flags |= DM_BUFFER_FULL_FLAG;
498 goto out;
500 param->data_size = param->data_start + needed;
502 iter_info.param_size = param_size;
503 iter_info.old_vers = NULL;
504 iter_info.vers = vers;
505 iter_info.flags = 0;
506 iter_info.end = (char *)vers+len;
509 * Now loop through filling out the names & versions.
511 dm_target_iterate(list_version_get_info, &iter_info);
512 param->flags |= iter_info.flags;
514 out:
515 return 0;
520 static int check_name(const char *name)
522 if (strchr(name, '/')) {
523 DMWARN("invalid device name");
524 return -EINVAL;
527 return 0;
531 * Fills in a dm_ioctl structure, ready for sending back to
532 * userland.
534 static int __dev_status(struct mapped_device *md, struct dm_ioctl *param)
536 struct gendisk *disk = dm_disk(md);
537 struct dm_table *table;
538 struct block_device *bdev;
540 param->flags &= ~(DM_SUSPEND_FLAG | DM_READONLY_FLAG |
541 DM_ACTIVE_PRESENT_FLAG);
543 if (dm_suspended(md))
544 param->flags |= DM_SUSPEND_FLAG;
546 param->dev = huge_encode_dev(MKDEV(disk->major, disk->first_minor));
548 if (!(param->flags & DM_SKIP_BDGET_FLAG)) {
549 bdev = bdget_disk(disk, 0);
550 if (!bdev)
551 return -ENXIO;
554 * Yes, this will be out of date by the time it gets back
555 * to userland, but it is still very useful for
556 * debugging.
558 param->open_count = bdev->bd_openers;
559 bdput(bdev);
560 } else
561 param->open_count = -1;
563 if (disk->policy)
564 param->flags |= DM_READONLY_FLAG;
566 param->event_nr = dm_get_event_nr(md);
568 table = dm_get_table(md);
569 if (table) {
570 param->flags |= DM_ACTIVE_PRESENT_FLAG;
571 param->target_count = dm_table_get_num_targets(table);
572 dm_table_put(table);
573 } else
574 param->target_count = 0;
576 return 0;
579 static int dev_create(struct dm_ioctl *param, size_t param_size)
581 int r;
582 struct mapped_device *md;
584 r = check_name(param->name);
585 if (r)
586 return r;
588 if (param->flags & DM_PERSISTENT_DEV_FLAG)
589 r = dm_create_with_minor(MINOR(huge_decode_dev(param->dev)), &md);
590 else
591 r = dm_create(&md);
593 if (r)
594 return r;
596 r = dm_hash_insert(param->name, *param->uuid ? param->uuid : NULL, md);
597 if (r) {
598 dm_put(md);
599 return r;
602 param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
604 r = __dev_status(md, param);
605 dm_put(md);
607 return r;
611 * Always use UUID for lookups if it's present, otherwise use name or dev.
613 static struct hash_cell *__find_device_hash_cell(struct dm_ioctl *param)
615 struct mapped_device *md;
616 void *mdptr = NULL;
618 if (*param->uuid)
619 return __get_uuid_cell(param->uuid);
621 if (*param->name)
622 return __get_name_cell(param->name);
624 md = dm_get_md(huge_decode_dev(param->dev));
625 if (md)
626 mdptr = dm_get_mdptr(md);
628 return mdptr;
631 static struct mapped_device *find_device(struct dm_ioctl *param)
633 struct hash_cell *hc;
634 struct mapped_device *md = NULL;
636 down_read(&_hash_lock);
637 hc = __find_device_hash_cell(param);
638 if (hc) {
639 md = hc->md;
642 * Sneakily write in both the name and the uuid
643 * while we have the cell.
645 strncpy(param->name, hc->name, sizeof(param->name));
646 if (hc->uuid)
647 strncpy(param->uuid, hc->uuid, sizeof(param->uuid)-1);
648 else
649 param->uuid[0] = '\0';
651 if (hc->new_map)
652 param->flags |= DM_INACTIVE_PRESENT_FLAG;
653 else
654 param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
656 up_read(&_hash_lock);
658 return md;
661 static int dev_remove(struct dm_ioctl *param, size_t param_size)
663 struct hash_cell *hc;
664 struct mapped_device *md;
666 down_write(&_hash_lock);
667 hc = __find_device_hash_cell(param);
669 if (!hc) {
670 DMWARN("device doesn't appear to be in the dev hash table.");
671 up_write(&_hash_lock);
672 return -ENXIO;
675 md = hc->md;
677 __hash_remove(hc);
678 up_write(&_hash_lock);
679 dm_put(md);
680 param->data_size = 0;
681 return 0;
685 * Check a string doesn't overrun the chunk of
686 * memory we copied from userland.
688 static int invalid_str(char *str, void *end)
690 while ((void *) str < end)
691 if (!*str++)
692 return 0;
694 return -EINVAL;
697 static int dev_rename(struct dm_ioctl *param, size_t param_size)
699 int r;
700 char *new_name = (char *) param + param->data_start;
702 if (new_name < (char *) (param + 1) ||
703 invalid_str(new_name, (void *) param + param_size)) {
704 DMWARN("Invalid new logical volume name supplied.");
705 return -EINVAL;
708 r = check_name(new_name);
709 if (r)
710 return r;
712 param->data_size = 0;
713 return dm_hash_rename(param->name, new_name);
716 static int dev_set_geometry(struct dm_ioctl *param, size_t param_size)
718 int r = -EINVAL, x;
719 struct mapped_device *md;
720 struct hd_geometry geometry;
721 unsigned long indata[4];
722 char *geostr = (char *) param + param->data_start;
724 md = find_device(param);
725 if (!md)
726 return -ENXIO;
728 if (geostr < (char *) (param + 1) ||
729 invalid_str(geostr, (void *) param + param_size)) {
730 DMWARN("Invalid geometry supplied.");
731 goto out;
734 x = sscanf(geostr, "%lu %lu %lu %lu", indata,
735 indata + 1, indata + 2, indata + 3);
737 if (x != 4) {
738 DMWARN("Unable to interpret geometry settings.");
739 goto out;
742 if (indata[0] > 65535 || indata[1] > 255 ||
743 indata[2] > 255 || indata[3] > ULONG_MAX) {
744 DMWARN("Geometry exceeds range limits.");
745 goto out;
748 geometry.cylinders = indata[0];
749 geometry.heads = indata[1];
750 geometry.sectors = indata[2];
751 geometry.start = indata[3];
753 r = dm_set_geometry(md, &geometry);
754 if (!r)
755 r = __dev_status(md, param);
757 param->data_size = 0;
759 out:
760 dm_put(md);
761 return r;
764 static int do_suspend(struct dm_ioctl *param)
766 int r = 0;
767 int do_lockfs = 1;
768 struct mapped_device *md;
770 md = find_device(param);
771 if (!md)
772 return -ENXIO;
774 if (param->flags & DM_SKIP_LOCKFS_FLAG)
775 do_lockfs = 0;
777 if (!dm_suspended(md))
778 r = dm_suspend(md, do_lockfs);
780 if (!r)
781 r = __dev_status(md, param);
783 dm_put(md);
784 return r;
787 static int do_resume(struct dm_ioctl *param)
789 int r = 0;
790 int do_lockfs = 1;
791 struct hash_cell *hc;
792 struct mapped_device *md;
793 struct dm_table *new_map;
795 down_write(&_hash_lock);
797 hc = __find_device_hash_cell(param);
798 if (!hc) {
799 DMWARN("device doesn't appear to be in the dev hash table.");
800 up_write(&_hash_lock);
801 return -ENXIO;
804 md = hc->md;
806 new_map = hc->new_map;
807 hc->new_map = NULL;
808 param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
810 up_write(&_hash_lock);
812 /* Do we need to load a new map ? */
813 if (new_map) {
814 /* Suspend if it isn't already suspended */
815 if (param->flags & DM_SKIP_LOCKFS_FLAG)
816 do_lockfs = 0;
817 if (!dm_suspended(md))
818 dm_suspend(md, do_lockfs);
820 r = dm_swap_table(md, new_map);
821 if (r) {
822 dm_put(md);
823 dm_table_put(new_map);
824 return r;
827 if (dm_table_get_mode(new_map) & FMODE_WRITE)
828 set_disk_ro(dm_disk(md), 0);
829 else
830 set_disk_ro(dm_disk(md), 1);
832 dm_table_put(new_map);
835 if (dm_suspended(md))
836 r = dm_resume(md);
838 if (!r)
839 r = __dev_status(md, param);
841 dm_put(md);
842 return r;
846 * Set or unset the suspension state of a device.
847 * If the device already is in the requested state we just return its status.
849 static int dev_suspend(struct dm_ioctl *param, size_t param_size)
851 if (param->flags & DM_SUSPEND_FLAG)
852 return do_suspend(param);
854 return do_resume(param);
858 * Copies device info back to user space, used by
859 * the create and info ioctls.
861 static int dev_status(struct dm_ioctl *param, size_t param_size)
863 int r;
864 struct mapped_device *md;
866 md = find_device(param);
867 if (!md)
868 return -ENXIO;
870 r = __dev_status(md, param);
871 dm_put(md);
872 return r;
876 * Build up the status struct for each target
878 static void retrieve_status(struct dm_table *table,
879 struct dm_ioctl *param, size_t param_size)
881 unsigned int i, num_targets;
882 struct dm_target_spec *spec;
883 char *outbuf, *outptr;
884 status_type_t type;
885 size_t remaining, len, used = 0;
887 outptr = outbuf = get_result_buffer(param, param_size, &len);
889 if (param->flags & DM_STATUS_TABLE_FLAG)
890 type = STATUSTYPE_TABLE;
891 else
892 type = STATUSTYPE_INFO;
894 /* Get all the target info */
895 num_targets = dm_table_get_num_targets(table);
896 for (i = 0; i < num_targets; i++) {
897 struct dm_target *ti = dm_table_get_target(table, i);
899 remaining = len - (outptr - outbuf);
900 if (remaining <= sizeof(struct dm_target_spec)) {
901 param->flags |= DM_BUFFER_FULL_FLAG;
902 break;
905 spec = (struct dm_target_spec *) outptr;
907 spec->status = 0;
908 spec->sector_start = ti->begin;
909 spec->length = ti->len;
910 strncpy(spec->target_type, ti->type->name,
911 sizeof(spec->target_type));
913 outptr += sizeof(struct dm_target_spec);
914 remaining = len - (outptr - outbuf);
915 if (remaining <= 0) {
916 param->flags |= DM_BUFFER_FULL_FLAG;
917 break;
920 /* Get the status/table string from the target driver */
921 if (ti->type->status) {
922 if (ti->type->status(ti, type, outptr, remaining)) {
923 param->flags |= DM_BUFFER_FULL_FLAG;
924 break;
926 } else
927 outptr[0] = '\0';
929 outptr += strlen(outptr) + 1;
930 used = param->data_start + (outptr - outbuf);
932 outptr = align_ptr(outptr);
933 spec->next = outptr - outbuf;
936 if (used)
937 param->data_size = used;
939 param->target_count = num_targets;
943 * Wait for a device to report an event
945 static int dev_wait(struct dm_ioctl *param, size_t param_size)
947 int r;
948 struct mapped_device *md;
949 struct dm_table *table;
951 md = find_device(param);
952 if (!md)
953 return -ENXIO;
956 * Wait for a notification event
958 if (dm_wait_event(md, param->event_nr)) {
959 r = -ERESTARTSYS;
960 goto out;
964 * The userland program is going to want to know what
965 * changed to trigger the event, so we may as well tell
966 * him and save an ioctl.
968 r = __dev_status(md, param);
969 if (r)
970 goto out;
972 table = dm_get_table(md);
973 if (table) {
974 retrieve_status(table, param, param_size);
975 dm_table_put(table);
978 out:
979 dm_put(md);
980 return r;
983 static inline int get_mode(struct dm_ioctl *param)
985 int mode = FMODE_READ | FMODE_WRITE;
987 if (param->flags & DM_READONLY_FLAG)
988 mode = FMODE_READ;
990 return mode;
993 static int next_target(struct dm_target_spec *last, uint32_t next, void *end,
994 struct dm_target_spec **spec, char **target_params)
996 *spec = (struct dm_target_spec *) ((unsigned char *) last + next);
997 *target_params = (char *) (*spec + 1);
999 if (*spec < (last + 1))
1000 return -EINVAL;
1002 return invalid_str(*target_params, end);
1005 static int populate_table(struct dm_table *table,
1006 struct dm_ioctl *param, size_t param_size)
1008 int r;
1009 unsigned int i = 0;
1010 struct dm_target_spec *spec = (struct dm_target_spec *) param;
1011 uint32_t next = param->data_start;
1012 void *end = (void *) param + param_size;
1013 char *target_params;
1015 if (!param->target_count) {
1016 DMWARN("populate_table: no targets specified");
1017 return -EINVAL;
1020 for (i = 0; i < param->target_count; i++) {
1022 r = next_target(spec, next, end, &spec, &target_params);
1023 if (r) {
1024 DMWARN("unable to find target");
1025 return r;
1028 r = dm_table_add_target(table, spec->target_type,
1029 (sector_t) spec->sector_start,
1030 (sector_t) spec->length,
1031 target_params);
1032 if (r) {
1033 DMWARN("error adding target to table");
1034 return r;
1037 next = spec->next;
1040 return dm_table_complete(table);
1043 static int table_load(struct dm_ioctl *param, size_t param_size)
1045 int r;
1046 struct hash_cell *hc;
1047 struct dm_table *t;
1048 struct mapped_device *md;
1050 md = find_device(param);
1051 if (!md)
1052 return -ENXIO;
1054 r = dm_table_create(&t, get_mode(param), param->target_count, md);
1055 if (r)
1056 goto out;
1058 r = populate_table(t, param, param_size);
1059 if (r) {
1060 dm_table_put(t);
1061 goto out;
1064 down_write(&_hash_lock);
1065 hc = dm_get_mdptr(md);
1066 if (!hc || hc->md != md) {
1067 DMWARN("device has been removed from the dev hash table.");
1068 dm_table_put(t);
1069 up_write(&_hash_lock);
1070 r = -ENXIO;
1071 goto out;
1074 if (hc->new_map)
1075 dm_table_put(hc->new_map);
1076 hc->new_map = t;
1077 up_write(&_hash_lock);
1079 param->flags |= DM_INACTIVE_PRESENT_FLAG;
1080 r = __dev_status(md, param);
1082 out:
1083 dm_put(md);
1085 return r;
1088 static int table_clear(struct dm_ioctl *param, size_t param_size)
1090 int r;
1091 struct hash_cell *hc;
1092 struct mapped_device *md;
1094 down_write(&_hash_lock);
1096 hc = __find_device_hash_cell(param);
1097 if (!hc) {
1098 DMWARN("device doesn't appear to be in the dev hash table.");
1099 up_write(&_hash_lock);
1100 return -ENXIO;
1103 if (hc->new_map) {
1104 dm_table_put(hc->new_map);
1105 hc->new_map = NULL;
1108 param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
1110 r = __dev_status(hc->md, param);
1111 md = hc->md;
1112 up_write(&_hash_lock);
1113 dm_put(md);
1114 return r;
1118 * Retrieves a list of devices used by a particular dm device.
1120 static void retrieve_deps(struct dm_table *table,
1121 struct dm_ioctl *param, size_t param_size)
1123 unsigned int count = 0;
1124 struct list_head *tmp;
1125 size_t len, needed;
1126 struct dm_dev *dd;
1127 struct dm_target_deps *deps;
1129 deps = get_result_buffer(param, param_size, &len);
1132 * Count the devices.
1134 list_for_each (tmp, dm_table_get_devices(table))
1135 count++;
1138 * Check we have enough space.
1140 needed = sizeof(*deps) + (sizeof(*deps->dev) * count);
1141 if (len < needed) {
1142 param->flags |= DM_BUFFER_FULL_FLAG;
1143 return;
1147 * Fill in the devices.
1149 deps->count = count;
1150 count = 0;
1151 list_for_each_entry (dd, dm_table_get_devices(table), list)
1152 deps->dev[count++] = huge_encode_dev(dd->bdev->bd_dev);
1154 param->data_size = param->data_start + needed;
1157 static int table_deps(struct dm_ioctl *param, size_t param_size)
1159 int r = 0;
1160 struct mapped_device *md;
1161 struct dm_table *table;
1163 md = find_device(param);
1164 if (!md)
1165 return -ENXIO;
1167 r = __dev_status(md, param);
1168 if (r)
1169 goto out;
1171 table = dm_get_table(md);
1172 if (table) {
1173 retrieve_deps(table, param, param_size);
1174 dm_table_put(table);
1177 out:
1178 dm_put(md);
1179 return r;
1183 * Return the status of a device as a text string for each
1184 * target.
1186 static int table_status(struct dm_ioctl *param, size_t param_size)
1188 int r;
1189 struct mapped_device *md;
1190 struct dm_table *table;
1192 md = find_device(param);
1193 if (!md)
1194 return -ENXIO;
1196 r = __dev_status(md, param);
1197 if (r)
1198 goto out;
1200 table = dm_get_table(md);
1201 if (table) {
1202 retrieve_status(table, param, param_size);
1203 dm_table_put(table);
1206 out:
1207 dm_put(md);
1208 return r;
1212 * Pass a message to the target that's at the supplied device offset.
1214 static int target_message(struct dm_ioctl *param, size_t param_size)
1216 int r, argc;
1217 char **argv;
1218 struct mapped_device *md;
1219 struct dm_table *table;
1220 struct dm_target *ti;
1221 struct dm_target_msg *tmsg = (void *) param + param->data_start;
1223 md = find_device(param);
1224 if (!md)
1225 return -ENXIO;
1227 r = __dev_status(md, param);
1228 if (r)
1229 goto out;
1231 if (tmsg < (struct dm_target_msg *) (param + 1) ||
1232 invalid_str(tmsg->message, (void *) param + param_size)) {
1233 DMWARN("Invalid target message parameters.");
1234 r = -EINVAL;
1235 goto out;
1238 r = dm_split_args(&argc, &argv, tmsg->message);
1239 if (r) {
1240 DMWARN("Failed to split target message parameters");
1241 goto out;
1244 table = dm_get_table(md);
1245 if (!table)
1246 goto out_argv;
1248 if (tmsg->sector >= dm_table_get_size(table)) {
1249 DMWARN("Target message sector outside device.");
1250 r = -EINVAL;
1251 goto out_table;
1254 ti = dm_table_find_target(table, tmsg->sector);
1255 if (ti->type->message)
1256 r = ti->type->message(ti, argc, argv);
1257 else {
1258 DMWARN("Target type does not support messages");
1259 r = -EINVAL;
1262 out_table:
1263 dm_table_put(table);
1264 out_argv:
1265 kfree(argv);
1266 out:
1267 param->data_size = 0;
1268 dm_put(md);
1269 return r;
1272 /*-----------------------------------------------------------------
1273 * Implementation of open/close/ioctl on the special char
1274 * device.
1275 *---------------------------------------------------------------*/
1276 static ioctl_fn lookup_ioctl(unsigned int cmd)
1278 static struct {
1279 int cmd;
1280 ioctl_fn fn;
1281 } _ioctls[] = {
1282 {DM_VERSION_CMD, NULL}, /* version is dealt with elsewhere */
1283 {DM_REMOVE_ALL_CMD, remove_all},
1284 {DM_LIST_DEVICES_CMD, list_devices},
1286 {DM_DEV_CREATE_CMD, dev_create},
1287 {DM_DEV_REMOVE_CMD, dev_remove},
1288 {DM_DEV_RENAME_CMD, dev_rename},
1289 {DM_DEV_SUSPEND_CMD, dev_suspend},
1290 {DM_DEV_STATUS_CMD, dev_status},
1291 {DM_DEV_WAIT_CMD, dev_wait},
1293 {DM_TABLE_LOAD_CMD, table_load},
1294 {DM_TABLE_CLEAR_CMD, table_clear},
1295 {DM_TABLE_DEPS_CMD, table_deps},
1296 {DM_TABLE_STATUS_CMD, table_status},
1298 {DM_LIST_VERSIONS_CMD, list_versions},
1300 {DM_TARGET_MSG_CMD, target_message},
1301 {DM_DEV_SET_GEOMETRY_CMD, dev_set_geometry}
1304 return (cmd >= ARRAY_SIZE(_ioctls)) ? NULL : _ioctls[cmd].fn;
1308 * As well as checking the version compatibility this always
1309 * copies the kernel interface version out.
1311 static int check_version(unsigned int cmd, struct dm_ioctl __user *user)
1313 uint32_t version[3];
1314 int r = 0;
1316 if (copy_from_user(version, user->version, sizeof(version)))
1317 return -EFAULT;
1319 if ((DM_VERSION_MAJOR != version[0]) ||
1320 (DM_VERSION_MINOR < version[1])) {
1321 DMWARN("ioctl interface mismatch: "
1322 "kernel(%u.%u.%u), user(%u.%u.%u), cmd(%d)",
1323 DM_VERSION_MAJOR, DM_VERSION_MINOR,
1324 DM_VERSION_PATCHLEVEL,
1325 version[0], version[1], version[2], cmd);
1326 r = -EINVAL;
1330 * Fill in the kernel version.
1332 version[0] = DM_VERSION_MAJOR;
1333 version[1] = DM_VERSION_MINOR;
1334 version[2] = DM_VERSION_PATCHLEVEL;
1335 if (copy_to_user(user->version, version, sizeof(version)))
1336 return -EFAULT;
1338 return r;
1341 static void free_params(struct dm_ioctl *param)
1343 vfree(param);
1346 static int copy_params(struct dm_ioctl __user *user, struct dm_ioctl **param)
1348 struct dm_ioctl tmp, *dmi;
1350 if (copy_from_user(&tmp, user, sizeof(tmp)))
1351 return -EFAULT;
1353 if (tmp.data_size < sizeof(tmp))
1354 return -EINVAL;
1356 dmi = (struct dm_ioctl *) vmalloc(tmp.data_size);
1357 if (!dmi)
1358 return -ENOMEM;
1360 if (copy_from_user(dmi, user, tmp.data_size)) {
1361 vfree(dmi);
1362 return -EFAULT;
1365 *param = dmi;
1366 return 0;
1369 static int validate_params(uint cmd, struct dm_ioctl *param)
1371 /* Always clear this flag */
1372 param->flags &= ~DM_BUFFER_FULL_FLAG;
1374 /* Ignores parameters */
1375 if (cmd == DM_REMOVE_ALL_CMD ||
1376 cmd == DM_LIST_DEVICES_CMD ||
1377 cmd == DM_LIST_VERSIONS_CMD)
1378 return 0;
1380 if ((cmd == DM_DEV_CREATE_CMD)) {
1381 if (!*param->name) {
1382 DMWARN("name not supplied when creating device");
1383 return -EINVAL;
1385 } else if ((*param->uuid && *param->name)) {
1386 DMWARN("only supply one of name or uuid, cmd(%u)", cmd);
1387 return -EINVAL;
1390 /* Ensure strings are terminated */
1391 param->name[DM_NAME_LEN - 1] = '\0';
1392 param->uuid[DM_UUID_LEN - 1] = '\0';
1394 return 0;
1397 static int ctl_ioctl(struct inode *inode, struct file *file,
1398 uint command, ulong u)
1400 int r = 0;
1401 unsigned int cmd;
1402 struct dm_ioctl *param;
1403 struct dm_ioctl __user *user = (struct dm_ioctl __user *) u;
1404 ioctl_fn fn = NULL;
1405 size_t param_size;
1407 /* only root can play with this */
1408 if (!capable(CAP_SYS_ADMIN))
1409 return -EACCES;
1411 if (_IOC_TYPE(command) != DM_IOCTL)
1412 return -ENOTTY;
1414 cmd = _IOC_NR(command);
1417 * Check the interface version passed in. This also
1418 * writes out the kernel's interface version.
1420 r = check_version(cmd, user);
1421 if (r)
1422 return r;
1425 * Nothing more to do for the version command.
1427 if (cmd == DM_VERSION_CMD)
1428 return 0;
1430 fn = lookup_ioctl(cmd);
1431 if (!fn) {
1432 DMWARN("dm_ctl_ioctl: unknown command 0x%x", command);
1433 return -ENOTTY;
1437 * Trying to avoid low memory issues when a device is
1438 * suspended.
1440 current->flags |= PF_MEMALLOC;
1443 * Copy the parameters into kernel space.
1445 r = copy_params(user, &param);
1447 current->flags &= ~PF_MEMALLOC;
1449 if (r)
1450 return r;
1452 r = validate_params(cmd, param);
1453 if (r)
1454 goto out;
1456 param_size = param->data_size;
1457 param->data_size = sizeof(*param);
1458 r = fn(param, param_size);
1461 * Copy the results back to userland.
1463 if (!r && copy_to_user(user, param, param->data_size))
1464 r = -EFAULT;
1466 out:
1467 free_params(param);
1468 return r;
1471 static struct file_operations _ctl_fops = {
1472 .ioctl = ctl_ioctl,
1473 .owner = THIS_MODULE,
1476 static struct miscdevice _dm_misc = {
1477 .minor = MISC_DYNAMIC_MINOR,
1478 .name = DM_NAME,
1479 .devfs_name = "mapper/control",
1480 .fops = &_ctl_fops
1484 * Create misc character device and link to DM_DIR/control.
1486 int __init dm_interface_init(void)
1488 int r;
1490 r = dm_hash_init();
1491 if (r)
1492 return r;
1494 r = misc_register(&_dm_misc);
1495 if (r) {
1496 DMERR("misc_register failed for control device");
1497 dm_hash_exit();
1498 return r;
1501 DMINFO("%d.%d.%d%s initialised: %s", DM_VERSION_MAJOR,
1502 DM_VERSION_MINOR, DM_VERSION_PATCHLEVEL, DM_VERSION_EXTRA,
1503 DM_DRIVER_EMAIL);
1504 return 0;
1507 void dm_interface_exit(void)
1509 if (misc_deregister(&_dm_misc) < 0)
1510 DMERR("misc_deregister failed for control device");
1512 dm_hash_exit();