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.
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
27 *---------------------------------------------------------------*/
29 struct list_head name_list
;
30 struct list_head uuid_list
;
34 struct mapped_device
*md
;
35 struct dm_table
*new_map
;
40 struct dm_target_versions
*vers
, *old_vers
;
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
)
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
);
74 static void dm_hash_exit(void)
80 /*-----------------------------------------------------------------
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;
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
))
111 static struct hash_cell
*__get_uuid_cell(const char *str
)
113 struct hash_cell
*hc
;
114 unsigned int h
= hash_str(str
);
116 list_for_each_entry (hc
, _uuid_buckets
+ h
, uuid_list
)
117 if (!strcmp(hc
->uuid
, str
))
123 /*-----------------------------------------------------------------
124 * Inserting, removing and renaming a device.
125 *---------------------------------------------------------------*/
126 static struct hash_cell
*alloc_cell(const char *name
, const char *uuid
,
127 struct mapped_device
*md
)
129 struct hash_cell
*hc
;
131 hc
= kmalloc(sizeof(*hc
), GFP_KERNEL
);
135 hc
->name
= kstrdup(name
, GFP_KERNEL
);
145 hc
->uuid
= kstrdup(uuid
, GFP_KERNEL
);
153 INIT_LIST_HEAD(&hc
->name_list
);
154 INIT_LIST_HEAD(&hc
->uuid_list
);
160 static void free_cell(struct hash_cell
*hc
)
172 static int register_with_devfs(struct hash_cell
*hc
)
174 struct gendisk
*disk
= dm_disk(hc
->md
);
176 devfs_mk_bdev(MKDEV(disk
->major
, disk
->first_minor
),
177 S_IFBLK
| S_IRUSR
| S_IWUSR
| S_IRGRP
,
178 DM_DIR
"/%s", hc
->name
);
182 static int unregister_with_devfs(struct hash_cell
*hc
)
184 devfs_remove(DM_DIR
"/%s", hc
->name
);
189 * The kdev_t and uuid of a device can never change once it is
190 * initially inserted.
192 static int dm_hash_insert(const char *name
, const char *uuid
, struct mapped_device
*md
)
194 struct hash_cell
*cell
;
197 * Allocate the new cells.
199 cell
= alloc_cell(name
, uuid
, md
);
204 * Insert the cell into both hash tables.
206 down_write(&_hash_lock
);
207 if (__get_name_cell(name
))
210 list_add(&cell
->name_list
, _name_buckets
+ hash_str(name
));
213 if (__get_uuid_cell(uuid
)) {
214 list_del(&cell
->name_list
);
217 list_add(&cell
->uuid_list
, _uuid_buckets
+ hash_str(uuid
));
219 register_with_devfs(cell
);
221 dm_set_mdptr(md
, cell
);
222 up_write(&_hash_lock
);
227 up_write(&_hash_lock
);
232 static void __hash_remove(struct hash_cell
*hc
)
234 struct dm_table
*table
;
236 /* remove from the dev hash */
237 list_del(&hc
->uuid_list
);
238 list_del(&hc
->name_list
);
239 unregister_with_devfs(hc
);
240 dm_set_mdptr(hc
->md
, NULL
);
242 table
= dm_get_table(hc
->md
);
244 dm_table_event(table
);
249 dm_table_put(hc
->new_map
);
254 static void dm_hash_remove_all(void)
257 struct hash_cell
*hc
;
258 struct list_head
*tmp
, *n
;
260 down_write(&_hash_lock
);
261 for (i
= 0; i
< NUM_BUCKETS
; i
++) {
262 list_for_each_safe (tmp
, n
, _name_buckets
+ i
) {
263 hc
= list_entry(tmp
, struct hash_cell
, name_list
);
267 up_write(&_hash_lock
);
270 static int dm_hash_rename(const char *old
, const char *new)
272 char *new_name
, *old_name
;
273 struct hash_cell
*hc
;
274 struct dm_table
*table
;
279 new_name
= kstrdup(new, GFP_KERNEL
);
283 down_write(&_hash_lock
);
288 hc
= __get_name_cell(new);
290 DMWARN("asked to rename to an already existing name %s -> %s",
292 up_write(&_hash_lock
);
298 * Is there such a device as 'old' ?
300 hc
= __get_name_cell(old
);
302 DMWARN("asked to rename a non existent device %s -> %s",
304 up_write(&_hash_lock
);
310 * rename and move the name cell.
312 unregister_with_devfs(hc
);
314 list_del(&hc
->name_list
);
317 list_add(&hc
->name_list
, _name_buckets
+ hash_str(new_name
));
319 /* rename the device node in devfs */
320 register_with_devfs(hc
);
323 * Wake up any dm event waiters.
325 table
= dm_get_table(hc
->md
);
327 dm_table_event(table
);
331 up_write(&_hash_lock
);
336 /*-----------------------------------------------------------------
337 * Implementation of the ioctl commands
338 *---------------------------------------------------------------*/
340 * All the ioctl commands get dispatched to functions with this
343 typedef int (*ioctl_fn
)(struct dm_ioctl
*param
, size_t param_size
);
345 static int remove_all(struct dm_ioctl
*param
, size_t param_size
)
347 dm_hash_remove_all();
348 param
->data_size
= 0;
353 * Round up the ptr to an 8-byte boundary.
356 static inline void *align_ptr(void *ptr
)
358 return (void *) (((size_t) (ptr
+ ALIGN_MASK
)) & ~ALIGN_MASK
);
362 * Retrieves the data payload buffer from an already allocated
365 static void *get_result_buffer(struct dm_ioctl
*param
, size_t param_size
,
368 param
->data_start
= align_ptr(param
+ 1) - (void *) param
;
370 if (param
->data_start
< param_size
)
371 *len
= param_size
- param
->data_start
;
375 return ((void *) param
) + param
->data_start
;
378 static int list_devices(struct dm_ioctl
*param
, size_t param_size
)
381 struct hash_cell
*hc
;
382 size_t len
, needed
= 0;
383 struct gendisk
*disk
;
384 struct dm_name_list
*nl
, *old_nl
= NULL
;
386 down_write(&_hash_lock
);
389 * Loop through all the devices working out how much
392 for (i
= 0; i
< NUM_BUCKETS
; i
++) {
393 list_for_each_entry (hc
, _name_buckets
+ i
, name_list
) {
394 needed
+= sizeof(struct dm_name_list
);
395 needed
+= strlen(hc
->name
) + 1;
396 needed
+= ALIGN_MASK
;
401 * Grab our output buffer.
403 nl
= get_result_buffer(param
, param_size
, &len
);
405 param
->flags
|= DM_BUFFER_FULL_FLAG
;
408 param
->data_size
= param
->data_start
+ needed
;
410 nl
->dev
= 0; /* Flags no data */
413 * Now loop through filling out the names.
415 for (i
= 0; i
< NUM_BUCKETS
; i
++) {
416 list_for_each_entry (hc
, _name_buckets
+ i
, name_list
) {
418 old_nl
->next
= (uint32_t) ((void *) nl
-
420 disk
= dm_disk(hc
->md
);
421 nl
->dev
= huge_encode_dev(MKDEV(disk
->major
, disk
->first_minor
));
423 strcpy(nl
->name
, hc
->name
);
426 nl
= align_ptr(((void *) ++nl
) + strlen(hc
->name
) + 1);
431 up_write(&_hash_lock
);
435 static void list_version_get_needed(struct target_type
*tt
, void *needed_param
)
437 size_t *needed
= needed_param
;
439 *needed
+= sizeof(struct dm_target_versions
);
440 *needed
+= strlen(tt
->name
);
441 *needed
+= ALIGN_MASK
;
444 static void list_version_get_info(struct target_type
*tt
, void *param
)
446 struct vers_iter
*info
= param
;
448 /* Check space - it might have changed since the first iteration */
449 if ((char *)info
->vers
+ sizeof(tt
->version
) + strlen(tt
->name
) + 1 >
452 info
->flags
= DM_BUFFER_FULL_FLAG
;
457 info
->old_vers
->next
= (uint32_t) ((void *)info
->vers
-
458 (void *)info
->old_vers
);
459 info
->vers
->version
[0] = tt
->version
[0];
460 info
->vers
->version
[1] = tt
->version
[1];
461 info
->vers
->version
[2] = tt
->version
[2];
462 info
->vers
->next
= 0;
463 strcpy(info
->vers
->name
, tt
->name
);
465 info
->old_vers
= info
->vers
;
466 info
->vers
= align_ptr(((void *) ++info
->vers
) + strlen(tt
->name
) + 1);
469 static int list_versions(struct dm_ioctl
*param
, size_t param_size
)
471 size_t len
, needed
= 0;
472 struct dm_target_versions
*vers
;
473 struct vers_iter iter_info
;
476 * Loop through all the devices working out how much
479 dm_target_iterate(list_version_get_needed
, &needed
);
482 * Grab our output buffer.
484 vers
= get_result_buffer(param
, param_size
, &len
);
486 param
->flags
|= DM_BUFFER_FULL_FLAG
;
489 param
->data_size
= param
->data_start
+ needed
;
491 iter_info
.param_size
= param_size
;
492 iter_info
.old_vers
= NULL
;
493 iter_info
.vers
= vers
;
495 iter_info
.end
= (char *)vers
+len
;
498 * Now loop through filling out the names & versions.
500 dm_target_iterate(list_version_get_info
, &iter_info
);
501 param
->flags
|= iter_info
.flags
;
509 static int check_name(const char *name
)
511 if (strchr(name
, '/')) {
512 DMWARN("invalid device name");
520 * Fills in a dm_ioctl structure, ready for sending back to
523 static int __dev_status(struct mapped_device
*md
, struct dm_ioctl
*param
)
525 struct gendisk
*disk
= dm_disk(md
);
526 struct dm_table
*table
;
527 struct block_device
*bdev
;
529 param
->flags
&= ~(DM_SUSPEND_FLAG
| DM_READONLY_FLAG
|
530 DM_ACTIVE_PRESENT_FLAG
);
532 if (dm_suspended(md
))
533 param
->flags
|= DM_SUSPEND_FLAG
;
535 param
->dev
= huge_encode_dev(MKDEV(disk
->major
, disk
->first_minor
));
537 if (!(param
->flags
& DM_SKIP_BDGET_FLAG
)) {
538 bdev
= bdget_disk(disk
, 0);
543 * Yes, this will be out of date by the time it gets back
544 * to userland, but it is still very useful for
547 param
->open_count
= bdev
->bd_openers
;
550 param
->open_count
= -1;
553 param
->flags
|= DM_READONLY_FLAG
;
555 param
->event_nr
= dm_get_event_nr(md
);
557 table
= dm_get_table(md
);
559 param
->flags
|= DM_ACTIVE_PRESENT_FLAG
;
560 param
->target_count
= dm_table_get_num_targets(table
);
563 param
->target_count
= 0;
568 static int dev_create(struct dm_ioctl
*param
, size_t param_size
)
571 struct mapped_device
*md
;
573 r
= check_name(param
->name
);
577 if (param
->flags
& DM_PERSISTENT_DEV_FLAG
)
578 r
= dm_create_with_minor(MINOR(huge_decode_dev(param
->dev
)), &md
);
585 r
= dm_hash_insert(param
->name
, *param
->uuid
? param
->uuid
: NULL
, md
);
591 param
->flags
&= ~DM_INACTIVE_PRESENT_FLAG
;
593 r
= __dev_status(md
, param
);
600 * Always use UUID for lookups if it's present, otherwise use name or dev.
602 static struct hash_cell
*__find_device_hash_cell(struct dm_ioctl
*param
)
604 struct mapped_device
*md
;
608 return __get_uuid_cell(param
->uuid
);
611 return __get_name_cell(param
->name
);
613 md
= dm_get_md(huge_decode_dev(param
->dev
));
615 mdptr
= dm_get_mdptr(md
);
622 static struct mapped_device
*find_device(struct dm_ioctl
*param
)
624 struct hash_cell
*hc
;
625 struct mapped_device
*md
= NULL
;
627 down_read(&_hash_lock
);
628 hc
= __find_device_hash_cell(param
);
634 * Sneakily write in both the name and the uuid
635 * while we have the cell.
637 strncpy(param
->name
, hc
->name
, sizeof(param
->name
));
639 strncpy(param
->uuid
, hc
->uuid
, sizeof(param
->uuid
)-1);
641 param
->uuid
[0] = '\0';
644 param
->flags
|= DM_INACTIVE_PRESENT_FLAG
;
646 param
->flags
&= ~DM_INACTIVE_PRESENT_FLAG
;
648 up_read(&_hash_lock
);
653 static int dev_remove(struct dm_ioctl
*param
, size_t param_size
)
655 struct hash_cell
*hc
;
657 down_write(&_hash_lock
);
658 hc
= __find_device_hash_cell(param
);
661 DMWARN("device doesn't appear to be in the dev hash table.");
662 up_write(&_hash_lock
);
667 up_write(&_hash_lock
);
668 param
->data_size
= 0;
673 * Check a string doesn't overrun the chunk of
674 * memory we copied from userland.
676 static int invalid_str(char *str
, void *end
)
678 while ((void *) str
< end
)
685 static int dev_rename(struct dm_ioctl
*param
, size_t param_size
)
688 char *new_name
= (char *) param
+ param
->data_start
;
690 if (new_name
< (char *) (param
+ 1) ||
691 invalid_str(new_name
, (void *) param
+ param_size
)) {
692 DMWARN("Invalid new logical volume name supplied.");
696 r
= check_name(new_name
);
700 param
->data_size
= 0;
701 return dm_hash_rename(param
->name
, new_name
);
704 static int dev_set_geometry(struct dm_ioctl
*param
, size_t param_size
)
707 struct mapped_device
*md
;
708 struct hd_geometry geometry
;
709 unsigned long indata
[4];
710 char *geostr
= (char *) param
+ param
->data_start
;
712 md
= find_device(param
);
716 if (geostr
< (char *) (param
+ 1) ||
717 invalid_str(geostr
, (void *) param
+ param_size
)) {
718 DMWARN("Invalid geometry supplied.");
722 x
= sscanf(geostr
, "%lu %lu %lu %lu", indata
,
723 indata
+ 1, indata
+ 2, indata
+ 3);
726 DMWARN("Unable to interpret geometry settings.");
730 if (indata
[0] > 65535 || indata
[1] > 255 ||
731 indata
[2] > 255 || indata
[3] > ULONG_MAX
) {
732 DMWARN("Geometry exceeds range limits.");
736 geometry
.cylinders
= indata
[0];
737 geometry
.heads
= indata
[1];
738 geometry
.sectors
= indata
[2];
739 geometry
.start
= indata
[3];
741 r
= dm_set_geometry(md
, &geometry
);
743 r
= __dev_status(md
, param
);
745 param
->data_size
= 0;
752 static int do_suspend(struct dm_ioctl
*param
)
756 struct mapped_device
*md
;
758 md
= find_device(param
);
762 if (param
->flags
& DM_SKIP_LOCKFS_FLAG
)
765 if (!dm_suspended(md
))
766 r
= dm_suspend(md
, do_lockfs
);
769 r
= __dev_status(md
, param
);
775 static int do_resume(struct dm_ioctl
*param
)
779 struct hash_cell
*hc
;
780 struct mapped_device
*md
;
781 struct dm_table
*new_map
;
783 down_write(&_hash_lock
);
785 hc
= __find_device_hash_cell(param
);
787 DMWARN("device doesn't appear to be in the dev hash table.");
788 up_write(&_hash_lock
);
795 new_map
= hc
->new_map
;
797 param
->flags
&= ~DM_INACTIVE_PRESENT_FLAG
;
799 up_write(&_hash_lock
);
801 /* Do we need to load a new map ? */
803 /* Suspend if it isn't already suspended */
804 if (param
->flags
& DM_SKIP_LOCKFS_FLAG
)
806 if (!dm_suspended(md
))
807 dm_suspend(md
, do_lockfs
);
809 r
= dm_swap_table(md
, new_map
);
812 dm_table_put(new_map
);
816 if (dm_table_get_mode(new_map
) & FMODE_WRITE
)
817 set_disk_ro(dm_disk(md
), 0);
819 set_disk_ro(dm_disk(md
), 1);
821 dm_table_put(new_map
);
824 if (dm_suspended(md
))
828 r
= __dev_status(md
, param
);
835 * Set or unset the suspension state of a device.
836 * If the device already is in the requested state we just return its status.
838 static int dev_suspend(struct dm_ioctl
*param
, size_t param_size
)
840 if (param
->flags
& DM_SUSPEND_FLAG
)
841 return do_suspend(param
);
843 return do_resume(param
);
847 * Copies device info back to user space, used by
848 * the create and info ioctls.
850 static int dev_status(struct dm_ioctl
*param
, size_t param_size
)
853 struct mapped_device
*md
;
855 md
= find_device(param
);
859 r
= __dev_status(md
, param
);
865 * Build up the status struct for each target
867 static void retrieve_status(struct dm_table
*table
,
868 struct dm_ioctl
*param
, size_t param_size
)
870 unsigned int i
, num_targets
;
871 struct dm_target_spec
*spec
;
872 char *outbuf
, *outptr
;
874 size_t remaining
, len
, used
= 0;
876 outptr
= outbuf
= get_result_buffer(param
, param_size
, &len
);
878 if (param
->flags
& DM_STATUS_TABLE_FLAG
)
879 type
= STATUSTYPE_TABLE
;
881 type
= STATUSTYPE_INFO
;
883 /* Get all the target info */
884 num_targets
= dm_table_get_num_targets(table
);
885 for (i
= 0; i
< num_targets
; i
++) {
886 struct dm_target
*ti
= dm_table_get_target(table
, i
);
888 remaining
= len
- (outptr
- outbuf
);
889 if (remaining
<= sizeof(struct dm_target_spec
)) {
890 param
->flags
|= DM_BUFFER_FULL_FLAG
;
894 spec
= (struct dm_target_spec
*) outptr
;
897 spec
->sector_start
= ti
->begin
;
898 spec
->length
= ti
->len
;
899 strncpy(spec
->target_type
, ti
->type
->name
,
900 sizeof(spec
->target_type
));
902 outptr
+= sizeof(struct dm_target_spec
);
903 remaining
= len
- (outptr
- outbuf
);
904 if (remaining
<= 0) {
905 param
->flags
|= DM_BUFFER_FULL_FLAG
;
909 /* Get the status/table string from the target driver */
910 if (ti
->type
->status
) {
911 if (ti
->type
->status(ti
, type
, outptr
, remaining
)) {
912 param
->flags
|= DM_BUFFER_FULL_FLAG
;
918 outptr
+= strlen(outptr
) + 1;
919 used
= param
->data_start
+ (outptr
- outbuf
);
921 outptr
= align_ptr(outptr
);
922 spec
->next
= outptr
- outbuf
;
926 param
->data_size
= used
;
928 param
->target_count
= num_targets
;
932 * Wait for a device to report an event
934 static int dev_wait(struct dm_ioctl
*param
, size_t param_size
)
937 struct mapped_device
*md
;
938 struct dm_table
*table
;
940 md
= find_device(param
);
945 * Wait for a notification event
947 if (dm_wait_event(md
, param
->event_nr
)) {
953 * The userland program is going to want to know what
954 * changed to trigger the event, so we may as well tell
955 * him and save an ioctl.
957 r
= __dev_status(md
, param
);
961 table
= dm_get_table(md
);
963 retrieve_status(table
, param
, param_size
);
972 static inline int get_mode(struct dm_ioctl
*param
)
974 int mode
= FMODE_READ
| FMODE_WRITE
;
976 if (param
->flags
& DM_READONLY_FLAG
)
982 static int next_target(struct dm_target_spec
*last
, uint32_t next
, void *end
,
983 struct dm_target_spec
**spec
, char **target_params
)
985 *spec
= (struct dm_target_spec
*) ((unsigned char *) last
+ next
);
986 *target_params
= (char *) (*spec
+ 1);
988 if (*spec
< (last
+ 1))
991 return invalid_str(*target_params
, end
);
994 static int populate_table(struct dm_table
*table
,
995 struct dm_ioctl
*param
, size_t param_size
)
999 struct dm_target_spec
*spec
= (struct dm_target_spec
*) param
;
1000 uint32_t next
= param
->data_start
;
1001 void *end
= (void *) param
+ param_size
;
1002 char *target_params
;
1004 if (!param
->target_count
) {
1005 DMWARN("populate_table: no targets specified");
1009 for (i
= 0; i
< param
->target_count
; i
++) {
1011 r
= next_target(spec
, next
, end
, &spec
, &target_params
);
1013 DMWARN("unable to find target");
1017 r
= dm_table_add_target(table
, spec
->target_type
,
1018 (sector_t
) spec
->sector_start
,
1019 (sector_t
) spec
->length
,
1022 DMWARN("error adding target to table");
1029 return dm_table_complete(table
);
1032 static int table_load(struct dm_ioctl
*param
, size_t param_size
)
1035 struct hash_cell
*hc
;
1037 struct mapped_device
*md
;
1039 md
= find_device(param
);
1043 r
= dm_table_create(&t
, get_mode(param
), param
->target_count
, md
);
1047 r
= populate_table(t
, param
, param_size
);
1053 down_write(&_hash_lock
);
1054 hc
= dm_get_mdptr(md
);
1055 if (!hc
|| hc
->md
!= md
) {
1056 DMWARN("device has been removed from the dev hash table.");
1058 up_write(&_hash_lock
);
1064 dm_table_put(hc
->new_map
);
1066 up_write(&_hash_lock
);
1068 param
->flags
|= DM_INACTIVE_PRESENT_FLAG
;
1069 r
= __dev_status(md
, param
);
1077 static int table_clear(struct dm_ioctl
*param
, size_t param_size
)
1080 struct hash_cell
*hc
;
1082 down_write(&_hash_lock
);
1084 hc
= __find_device_hash_cell(param
);
1086 DMWARN("device doesn't appear to be in the dev hash table.");
1087 up_write(&_hash_lock
);
1092 dm_table_put(hc
->new_map
);
1096 param
->flags
&= ~DM_INACTIVE_PRESENT_FLAG
;
1098 r
= __dev_status(hc
->md
, param
);
1099 up_write(&_hash_lock
);
1104 * Retrieves a list of devices used by a particular dm device.
1106 static void retrieve_deps(struct dm_table
*table
,
1107 struct dm_ioctl
*param
, size_t param_size
)
1109 unsigned int count
= 0;
1110 struct list_head
*tmp
;
1113 struct dm_target_deps
*deps
;
1115 deps
= get_result_buffer(param
, param_size
, &len
);
1118 * Count the devices.
1120 list_for_each (tmp
, dm_table_get_devices(table
))
1124 * Check we have enough space.
1126 needed
= sizeof(*deps
) + (sizeof(*deps
->dev
) * count
);
1128 param
->flags
|= DM_BUFFER_FULL_FLAG
;
1133 * Fill in the devices.
1135 deps
->count
= count
;
1137 list_for_each_entry (dd
, dm_table_get_devices(table
), list
)
1138 deps
->dev
[count
++] = huge_encode_dev(dd
->bdev
->bd_dev
);
1140 param
->data_size
= param
->data_start
+ needed
;
1143 static int table_deps(struct dm_ioctl
*param
, size_t param_size
)
1146 struct mapped_device
*md
;
1147 struct dm_table
*table
;
1149 md
= find_device(param
);
1153 r
= __dev_status(md
, param
);
1157 table
= dm_get_table(md
);
1159 retrieve_deps(table
, param
, param_size
);
1160 dm_table_put(table
);
1169 * Return the status of a device as a text string for each
1172 static int table_status(struct dm_ioctl
*param
, size_t param_size
)
1175 struct mapped_device
*md
;
1176 struct dm_table
*table
;
1178 md
= find_device(param
);
1182 r
= __dev_status(md
, param
);
1186 table
= dm_get_table(md
);
1188 retrieve_status(table
, param
, param_size
);
1189 dm_table_put(table
);
1198 * Pass a message to the target that's at the supplied device offset.
1200 static int target_message(struct dm_ioctl
*param
, size_t param_size
)
1204 struct mapped_device
*md
;
1205 struct dm_table
*table
;
1206 struct dm_target
*ti
;
1207 struct dm_target_msg
*tmsg
= (void *) param
+ param
->data_start
;
1209 md
= find_device(param
);
1213 r
= __dev_status(md
, param
);
1217 if (tmsg
< (struct dm_target_msg
*) (param
+ 1) ||
1218 invalid_str(tmsg
->message
, (void *) param
+ param_size
)) {
1219 DMWARN("Invalid target message parameters.");
1224 r
= dm_split_args(&argc
, &argv
, tmsg
->message
);
1226 DMWARN("Failed to split target message parameters");
1230 table
= dm_get_table(md
);
1234 if (tmsg
->sector
>= dm_table_get_size(table
)) {
1235 DMWARN("Target message sector outside device.");
1240 ti
= dm_table_find_target(table
, tmsg
->sector
);
1241 if (ti
->type
->message
)
1242 r
= ti
->type
->message(ti
, argc
, argv
);
1244 DMWARN("Target type does not support messages");
1249 dm_table_put(table
);
1253 param
->data_size
= 0;
1258 /*-----------------------------------------------------------------
1259 * Implementation of open/close/ioctl on the special char
1261 *---------------------------------------------------------------*/
1262 static ioctl_fn
lookup_ioctl(unsigned int cmd
)
1268 {DM_VERSION_CMD
, NULL
}, /* version is dealt with elsewhere */
1269 {DM_REMOVE_ALL_CMD
, remove_all
},
1270 {DM_LIST_DEVICES_CMD
, list_devices
},
1272 {DM_DEV_CREATE_CMD
, dev_create
},
1273 {DM_DEV_REMOVE_CMD
, dev_remove
},
1274 {DM_DEV_RENAME_CMD
, dev_rename
},
1275 {DM_DEV_SUSPEND_CMD
, dev_suspend
},
1276 {DM_DEV_STATUS_CMD
, dev_status
},
1277 {DM_DEV_WAIT_CMD
, dev_wait
},
1279 {DM_TABLE_LOAD_CMD
, table_load
},
1280 {DM_TABLE_CLEAR_CMD
, table_clear
},
1281 {DM_TABLE_DEPS_CMD
, table_deps
},
1282 {DM_TABLE_STATUS_CMD
, table_status
},
1284 {DM_LIST_VERSIONS_CMD
, list_versions
},
1286 {DM_TARGET_MSG_CMD
, target_message
},
1287 {DM_DEV_SET_GEOMETRY_CMD
, dev_set_geometry
}
1290 return (cmd
>= ARRAY_SIZE(_ioctls
)) ? NULL
: _ioctls
[cmd
].fn
;
1294 * As well as checking the version compatibility this always
1295 * copies the kernel interface version out.
1297 static int check_version(unsigned int cmd
, struct dm_ioctl __user
*user
)
1299 uint32_t version
[3];
1302 if (copy_from_user(version
, user
->version
, sizeof(version
)))
1305 if ((DM_VERSION_MAJOR
!= version
[0]) ||
1306 (DM_VERSION_MINOR
< version
[1])) {
1307 DMWARN("ioctl interface mismatch: "
1308 "kernel(%u.%u.%u), user(%u.%u.%u), cmd(%d)",
1309 DM_VERSION_MAJOR
, DM_VERSION_MINOR
,
1310 DM_VERSION_PATCHLEVEL
,
1311 version
[0], version
[1], version
[2], cmd
);
1316 * Fill in the kernel version.
1318 version
[0] = DM_VERSION_MAJOR
;
1319 version
[1] = DM_VERSION_MINOR
;
1320 version
[2] = DM_VERSION_PATCHLEVEL
;
1321 if (copy_to_user(user
->version
, version
, sizeof(version
)))
1327 static void free_params(struct dm_ioctl
*param
)
1332 static int copy_params(struct dm_ioctl __user
*user
, struct dm_ioctl
**param
)
1334 struct dm_ioctl tmp
, *dmi
;
1336 if (copy_from_user(&tmp
, user
, sizeof(tmp
)))
1339 if (tmp
.data_size
< sizeof(tmp
))
1342 dmi
= (struct dm_ioctl
*) vmalloc(tmp
.data_size
);
1346 if (copy_from_user(dmi
, user
, tmp
.data_size
)) {
1355 static int validate_params(uint cmd
, struct dm_ioctl
*param
)
1357 /* Always clear this flag */
1358 param
->flags
&= ~DM_BUFFER_FULL_FLAG
;
1360 /* Ignores parameters */
1361 if (cmd
== DM_REMOVE_ALL_CMD
||
1362 cmd
== DM_LIST_DEVICES_CMD
||
1363 cmd
== DM_LIST_VERSIONS_CMD
)
1366 if ((cmd
== DM_DEV_CREATE_CMD
)) {
1367 if (!*param
->name
) {
1368 DMWARN("name not supplied when creating device");
1371 } else if ((*param
->uuid
&& *param
->name
)) {
1372 DMWARN("only supply one of name or uuid, cmd(%u)", cmd
);
1376 /* Ensure strings are terminated */
1377 param
->name
[DM_NAME_LEN
- 1] = '\0';
1378 param
->uuid
[DM_UUID_LEN
- 1] = '\0';
1383 static int ctl_ioctl(struct inode
*inode
, struct file
*file
,
1384 uint command
, ulong u
)
1388 struct dm_ioctl
*param
;
1389 struct dm_ioctl __user
*user
= (struct dm_ioctl __user
*) u
;
1393 /* only root can play with this */
1394 if (!capable(CAP_SYS_ADMIN
))
1397 if (_IOC_TYPE(command
) != DM_IOCTL
)
1400 cmd
= _IOC_NR(command
);
1403 * Check the interface version passed in. This also
1404 * writes out the kernel's interface version.
1406 r
= check_version(cmd
, user
);
1411 * Nothing more to do for the version command.
1413 if (cmd
== DM_VERSION_CMD
)
1416 fn
= lookup_ioctl(cmd
);
1418 DMWARN("dm_ctl_ioctl: unknown command 0x%x", command
);
1423 * Trying to avoid low memory issues when a device is
1426 current
->flags
|= PF_MEMALLOC
;
1429 * Copy the parameters into kernel space.
1431 r
= copy_params(user
, ¶m
);
1433 current
->flags
&= ~PF_MEMALLOC
;
1438 r
= validate_params(cmd
, param
);
1442 param_size
= param
->data_size
;
1443 param
->data_size
= sizeof(*param
);
1444 r
= fn(param
, param_size
);
1447 * Copy the results back to userland.
1449 if (!r
&& copy_to_user(user
, param
, param
->data_size
))
1457 static struct file_operations _ctl_fops
= {
1459 .owner
= THIS_MODULE
,
1462 static struct miscdevice _dm_misc
= {
1463 .minor
= MISC_DYNAMIC_MINOR
,
1465 .devfs_name
= "mapper/control",
1470 * Create misc character device and link to DM_DIR/control.
1472 int __init
dm_interface_init(void)
1480 r
= misc_register(&_dm_misc
);
1482 DMERR("misc_register failed for control device");
1487 DMINFO("%d.%d.%d%s initialised: %s", DM_VERSION_MAJOR
,
1488 DM_VERSION_MINOR
, DM_VERSION_PATCHLEVEL
, DM_VERSION_EXTRA
,
1493 void dm_interface_exit(void)
1495 if (misc_deregister(&_dm_misc
) < 0)
1496 DMERR("misc_deregister failed for control device");