2 * Copyright (C) 2001, 2002 Sistina Software (UK) Limited.
4 * This file is released under the GPL.
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
25 *---------------------------------------------------------------*/
27 struct list_head name_list
;
28 struct list_head uuid_list
;
32 struct mapped_device
*md
;
33 struct dm_table
*new_map
;
38 struct dm_target_versions
*vers
, *old_vers
;
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
)
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
);
72 static void dm_hash_exit(void)
78 /*-----------------------------------------------------------------
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;
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
)
100 unsigned int h
= hash_str(str
);
102 list_for_each_entry (hc
, _name_buckets
+ h
, name_list
)
103 if (!strcmp(hc
->name
, str
))
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
))
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
);
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
);
141 hc
->name
= kstrdup(name
);
151 hc
->uuid
= kstrdup(uuid
);
159 INIT_LIST_HEAD(&hc
->name_list
);
160 INIT_LIST_HEAD(&hc
->uuid_list
);
166 static void free_cell(struct hash_cell
*hc
)
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
);
188 static int unregister_with_devfs(struct hash_cell
*hc
)
190 devfs_remove(DM_DIR
"/%s", hc
->name
);
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
);
210 * Insert the cell into both hash tables.
212 down_write(&_hash_lock
);
213 if (__get_name_cell(name
))
216 list_add(&cell
->name_list
, _name_buckets
+ hash_str(name
));
219 if (__get_uuid_cell(uuid
)) {
220 list_del(&cell
->name_list
);
223 list_add(&cell
->uuid_list
, _uuid_buckets
+ hash_str(uuid
));
225 register_with_devfs(cell
);
227 up_write(&_hash_lock
);
232 up_write(&_hash_lock
);
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
);
245 dm_table_put(hc
->new_map
);
249 static void dm_hash_remove_all(void)
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
);
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
;
273 new_name
= kstrdup(new);
277 down_write(&_hash_lock
);
282 hc
= __get_name_cell(new);
284 DMWARN("asked to rename to an already existing name %s -> %s",
286 up_write(&_hash_lock
);
292 * Is there such a device as 'old' ?
294 hc
= __get_name_cell(old
);
296 DMWARN("asked to rename a non existent device %s -> %s",
298 up_write(&_hash_lock
);
304 * rename and move the name cell.
306 unregister_with_devfs(hc
);
308 list_del(&hc
->name_list
);
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
);
321 /*-----------------------------------------------------------------
322 * Implementation of the ioctl commands
323 *---------------------------------------------------------------*/
325 * All the ioctl commands get dispatched to functions with this
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;
338 * Round up the ptr to an 8-byte boundary.
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
350 static void *get_result_buffer(struct dm_ioctl
*param
, size_t param_size
,
353 param
->data_start
= align_ptr(param
+ 1) - (void *) param
;
355 if (param
->data_start
< param_size
)
356 *len
= param_size
- param
->data_start
;
360 return ((void *) param
) + param
->data_start
;
363 static int list_devices(struct dm_ioctl
*param
, size_t param_size
)
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
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
);
390 param
->flags
|= DM_BUFFER_FULL_FLAG
;
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
) {
403 old_nl
->next
= (uint32_t) ((void *) nl
-
405 disk
= dm_disk(hc
->md
);
406 nl
->dev
= huge_encode_dev(MKDEV(disk
->major
, disk
->first_minor
));
408 strcpy(nl
->name
, hc
->name
);
411 nl
= align_ptr(((void *) ++nl
) + strlen(hc
->name
) + 1);
416 up_write(&_hash_lock
);
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 >
437 info
->flags
= DM_BUFFER_FULL_FLAG
;
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
464 dm_target_iterate(list_version_get_needed
, &needed
);
467 * Grab our output buffer.
469 vers
= get_result_buffer(param
, param_size
, &len
);
471 param
->flags
|= DM_BUFFER_FULL_FLAG
;
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
;
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
;
494 static int check_name(const char *name
)
496 if (strchr(name
, '/')) {
497 DMWARN("invalid device name");
505 * Fills in a dm_ioctl structure, ready for sending back to
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);
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
531 param
->open_count
= bdev
->bd_openers
;
535 param
->flags
|= DM_READONLY_FLAG
;
537 param
->event_nr
= dm_get_event_nr(md
);
539 table
= dm_get_table(md
);
541 param
->flags
|= DM_ACTIVE_PRESENT_FLAG
;
542 param
->target_count
= dm_table_get_num_targets(table
);
545 param
->target_count
= 0;
550 static int dev_create(struct dm_ioctl
*param
, size_t param_size
)
553 struct mapped_device
*md
;
555 r
= check_name(param
->name
);
559 if (param
->flags
& DM_PERSISTENT_DEV_FLAG
)
560 r
= dm_create_with_minor(MINOR(huge_decode_dev(param
->dev
)), &md
);
567 r
= dm_hash_insert(param
->name
, *param
->uuid
? param
->uuid
: NULL
, md
);
573 param
->flags
&= ~DM_INACTIVE_PRESENT_FLAG
;
575 r
= __dev_status(md
, param
);
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
);
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
));
606 strncpy(param
->uuid
, hc
->uuid
, sizeof(param
->uuid
)-1);
608 param
->uuid
[0] = '\0';
611 param
->flags
|= DM_INACTIVE_PRESENT_FLAG
;
613 param
->flags
&= ~DM_INACTIVE_PRESENT_FLAG
;
617 up_read(&_hash_lock
);
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
);
630 DMWARN("device doesn't appear to be in the dev hash table.");
631 up_write(&_hash_lock
);
636 up_write(&_hash_lock
);
637 param
->data_size
= 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
)
654 static int dev_rename(struct dm_ioctl
*param
, size_t param_size
)
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.");
665 r
= check_name(new_name
);
669 param
->data_size
= 0;
670 return dm_hash_rename(param
->name
, new_name
);
673 static int do_suspend(struct dm_ioctl
*param
)
676 struct mapped_device
*md
;
678 md
= find_device(param
);
682 if (!dm_suspended(md
))
686 r
= __dev_status(md
, param
);
692 static int do_resume(struct dm_ioctl
*param
)
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
);
703 DMWARN("device doesn't appear to be in the dev hash table.");
704 up_write(&_hash_lock
);
711 new_map
= hc
->new_map
;
713 param
->flags
&= ~DM_INACTIVE_PRESENT_FLAG
;
715 up_write(&_hash_lock
);
717 /* Do we need to load a new map ? */
719 /* Suspend if it isn't already suspended */
720 if (!dm_suspended(md
))
723 r
= dm_swap_table(md
, new_map
);
726 dm_table_put(new_map
);
730 if (dm_table_get_mode(new_map
) & FMODE_WRITE
)
731 set_disk_ro(dm_disk(md
), 0);
733 set_disk_ro(dm_disk(md
), 1);
735 dm_table_put(new_map
);
738 if (dm_suspended(md
))
742 r
= __dev_status(md
, param
);
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
)
767 struct mapped_device
*md
;
769 md
= find_device(param
);
773 r
= __dev_status(md
, param
);
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
;
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
;
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
;
808 spec
= (struct dm_target_spec
*) outptr
;
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
;
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
;
832 outptr
+= strlen(outptr
) + 1;
833 used
= param
->data_start
+ (outptr
- outbuf
);
835 outptr
= align_ptr(outptr
);
836 spec
->next
= outptr
- outbuf
;
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
)
851 struct mapped_device
*md
;
852 struct dm_table
*table
;
854 md
= find_device(param
);
859 * Wait for a notification event
861 if (dm_wait_event(md
, param
->event_nr
)) {
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
);
875 table
= dm_get_table(md
);
877 retrieve_status(table
, param
, param_size
);
886 static inline int get_mode(struct dm_ioctl
*param
)
888 int mode
= FMODE_READ
| FMODE_WRITE
;
890 if (param
->flags
& DM_READONLY_FLAG
)
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))
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
)
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
;
918 if (!param
->target_count
) {
919 DMWARN("populate_table: no targets specified");
923 for (i
= 0; i
< param
->target_count
; i
++) {
925 r
= next_target(spec
, next
, end
, &spec
, &target_params
);
927 DMWARN("unable to find target");
931 r
= dm_table_add_target(table
, spec
->target_type
,
932 (sector_t
) spec
->sector_start
,
933 (sector_t
) spec
->length
,
936 DMWARN("error adding target to table");
943 return dm_table_complete(table
);
946 static int table_load(struct dm_ioctl
*param
, size_t param_size
)
949 struct hash_cell
*hc
;
952 r
= dm_table_create(&t
, get_mode(param
), param
->target_count
);
956 r
= populate_table(t
, param
, param_size
);
962 down_write(&_hash_lock
);
963 hc
= __find_device_hash_cell(param
);
965 DMWARN("device doesn't appear to be in the dev hash table.");
966 up_write(&_hash_lock
);
971 dm_table_put(hc
->new_map
);
973 param
->flags
|= DM_INACTIVE_PRESENT_FLAG
;
975 r
= __dev_status(hc
->md
, param
);
976 up_write(&_hash_lock
);
980 static int table_clear(struct dm_ioctl
*param
, size_t param_size
)
983 struct hash_cell
*hc
;
985 down_write(&_hash_lock
);
987 hc
= __find_device_hash_cell(param
);
989 DMWARN("device doesn't appear to be in the dev hash table.");
990 up_write(&_hash_lock
);
995 dm_table_put(hc
->new_map
);
999 param
->flags
&= ~DM_INACTIVE_PRESENT_FLAG
;
1001 r
= __dev_status(hc
->md
, param
);
1002 up_write(&_hash_lock
);
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
;
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
))
1027 * Check we have enough space.
1029 needed
= sizeof(*deps
) + (sizeof(*deps
->dev
) * count
);
1031 param
->flags
|= DM_BUFFER_FULL_FLAG
;
1036 * Fill in the devices.
1038 deps
->count
= count
;
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
)
1049 struct mapped_device
*md
;
1050 struct dm_table
*table
;
1052 md
= find_device(param
);
1056 r
= __dev_status(md
, param
);
1060 table
= dm_get_table(md
);
1062 retrieve_deps(table
, param
, param_size
);
1063 dm_table_put(table
);
1072 * Return the status of a device as a text string for each
1075 static int table_status(struct dm_ioctl
*param
, size_t param_size
)
1078 struct mapped_device
*md
;
1079 struct dm_table
*table
;
1081 md
= find_device(param
);
1085 r
= __dev_status(md
, param
);
1089 table
= dm_get_table(md
);
1091 retrieve_status(table
, param
, param_size
);
1092 dm_table_put(table
);
1100 /*-----------------------------------------------------------------
1101 * Implementation of open/close/ioctl on the special char
1103 *---------------------------------------------------------------*/
1104 static ioctl_fn
lookup_ioctl(unsigned int cmd
)
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];
1141 if (copy_from_user(version
, user
->version
, sizeof(version
)))
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
);
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
)))
1166 static void free_params(struct dm_ioctl
*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
)))
1178 if (tmp
.data_size
< sizeof(tmp
))
1181 dmi
= (struct dm_ioctl
*) vmalloc(tmp
.data_size
);
1185 if (copy_from_user(dmi
, user
, tmp
.data_size
)) {
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
)
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)",
1215 /* Ensure strings are terminated */
1216 param
->name
[DM_NAME_LEN
- 1] = '\0';
1217 param
->uuid
[DM_UUID_LEN
- 1] = '\0';
1222 static int ctl_ioctl(struct inode
*inode
, struct file
*file
,
1223 uint command
, ulong u
)
1227 struct dm_ioctl
*param
;
1228 struct dm_ioctl __user
*user
= (struct dm_ioctl __user
*) u
;
1232 /* only root can play with this */
1233 if (!capable(CAP_SYS_ADMIN
))
1236 if (_IOC_TYPE(command
) != DM_IOCTL
)
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
);
1250 * Nothing more to do for the version command.
1252 if (cmd
== DM_VERSION_CMD
)
1255 fn
= lookup_ioctl(cmd
);
1257 DMWARN("dm_ctl_ioctl: unknown command 0x%x", command
);
1262 * Trying to avoid low memory issues when a device is
1265 current
->flags
|= PF_MEMALLOC
;
1268 * Copy the parameters into kernel space.
1270 r
= copy_params(user
, ¶m
);
1272 current
->flags
&= ~PF_MEMALLOC
;
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
);
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
))
1298 current
->flags
&= ~PF_MEMALLOC
;
1302 static struct file_operations _ctl_fops
= {
1304 .owner
= THIS_MODULE
,
1307 static struct miscdevice _dm_misc
= {
1308 .minor
= MISC_DYNAMIC_MINOR
,
1310 .devfs_name
= "mapper/control",
1315 * Create misc character device and link to DM_DIR/control.
1317 int __init
dm_interface_init(void)
1325 r
= misc_register(&_dm_misc
);
1327 DMERR("misc_register failed for control device");
1332 DMINFO("%d.%d.%d%s initialised: %s", DM_VERSION_MAJOR
,
1333 DM_VERSION_MINOR
, DM_VERSION_PATCHLEVEL
, DM_VERSION_EXTRA
,
1338 void dm_interface_exit(void)
1340 if (misc_deregister(&_dm_misc
) < 0)
1341 DMERR("misc_deregister failed for control device");