2 * Copyright (C) 2001, 2002 Sistina Software (UK) Limited.
3 * Copyright (C) 2004 - 2006 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/dm-ioctl.h>
17 #include <linux/hdreg.h>
18 #include <linux/compat.h>
20 #include <asm/uaccess.h>
22 #define DM_MSG_PREFIX "ioctl"
23 #define DM_DRIVER_EMAIL "dm-devel@redhat.com"
25 /*-----------------------------------------------------------------
26 * The ioctl interface needs to be able to look up devices by
28 *---------------------------------------------------------------*/
30 struct list_head name_list
;
31 struct list_head uuid_list
;
35 struct mapped_device
*md
;
36 struct dm_table
*new_map
;
41 struct dm_target_versions
*vers
, *old_vers
;
47 #define NUM_BUCKETS 64
48 #define MASK_BUCKETS (NUM_BUCKETS - 1)
49 static struct list_head _name_buckets
[NUM_BUCKETS
];
50 static struct list_head _uuid_buckets
[NUM_BUCKETS
];
52 static void dm_hash_remove_all(int keep_open_devices
);
55 * Guards access to both hash tables.
57 static DECLARE_RWSEM(_hash_lock
);
59 static void init_buckets(struct list_head
*buckets
)
63 for (i
= 0; i
< NUM_BUCKETS
; i
++)
64 INIT_LIST_HEAD(buckets
+ i
);
67 static int dm_hash_init(void)
69 init_buckets(_name_buckets
);
70 init_buckets(_uuid_buckets
);
74 static void dm_hash_exit(void)
76 dm_hash_remove_all(0);
79 /*-----------------------------------------------------------------
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;
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
)) {
112 static struct hash_cell
*__get_uuid_cell(const char *str
)
114 struct hash_cell
*hc
;
115 unsigned int h
= hash_str(str
);
117 list_for_each_entry (hc
, _uuid_buckets
+ h
, uuid_list
)
118 if (!strcmp(hc
->uuid
, str
)) {
126 /*-----------------------------------------------------------------
127 * Inserting, removing and renaming a device.
128 *---------------------------------------------------------------*/
129 static struct hash_cell
*alloc_cell(const char *name
, const char *uuid
,
130 struct mapped_device
*md
)
132 struct hash_cell
*hc
;
134 hc
= kmalloc(sizeof(*hc
), GFP_KERNEL
);
138 hc
->name
= kstrdup(name
, GFP_KERNEL
);
148 hc
->uuid
= kstrdup(uuid
, GFP_KERNEL
);
156 INIT_LIST_HEAD(&hc
->name_list
);
157 INIT_LIST_HEAD(&hc
->uuid_list
);
163 static void free_cell(struct hash_cell
*hc
)
173 * The kdev_t and uuid of a device can never change once it is
174 * initially inserted.
176 static int dm_hash_insert(const char *name
, const char *uuid
, struct mapped_device
*md
)
178 struct hash_cell
*cell
, *hc
;
181 * Allocate the new cells.
183 cell
= alloc_cell(name
, uuid
, md
);
188 * Insert the cell into both hash tables.
190 down_write(&_hash_lock
);
191 hc
= __get_name_cell(name
);
197 list_add(&cell
->name_list
, _name_buckets
+ hash_str(name
));
200 hc
= __get_uuid_cell(uuid
);
202 list_del(&cell
->name_list
);
206 list_add(&cell
->uuid_list
, _uuid_buckets
+ hash_str(uuid
));
209 dm_set_mdptr(md
, cell
);
210 up_write(&_hash_lock
);
215 up_write(&_hash_lock
);
220 static void __hash_remove(struct hash_cell
*hc
)
222 struct dm_table
*table
;
224 /* remove from the dev hash */
225 list_del(&hc
->uuid_list
);
226 list_del(&hc
->name_list
);
227 dm_set_mdptr(hc
->md
, NULL
);
229 table
= dm_get_table(hc
->md
);
231 dm_table_event(table
);
236 dm_table_destroy(hc
->new_map
);
241 static void dm_hash_remove_all(int keep_open_devices
)
243 int i
, dev_skipped
, dev_removed
;
244 struct hash_cell
*hc
;
245 struct list_head
*tmp
, *n
;
247 down_write(&_hash_lock
);
250 dev_skipped
= dev_removed
= 0;
251 for (i
= 0; i
< NUM_BUCKETS
; i
++) {
252 list_for_each_safe (tmp
, n
, _name_buckets
+ i
) {
253 hc
= list_entry(tmp
, struct hash_cell
, name_list
);
255 if (keep_open_devices
&&
256 dm_lock_for_deletion(hc
->md
)) {
266 * Some mapped devices may be using other mapped devices, so if any
267 * still exist, repeat until we make no further progress.
273 DMWARN("remove_all left %d open device(s)", dev_skipped
);
276 up_write(&_hash_lock
);
279 static int dm_hash_rename(uint32_t cookie
, const char *old
, const char *new)
281 char *new_name
, *old_name
;
282 struct hash_cell
*hc
;
283 struct dm_table
*table
;
288 new_name
= kstrdup(new, GFP_KERNEL
);
292 down_write(&_hash_lock
);
297 hc
= __get_name_cell(new);
299 DMWARN("asked to rename to an already existing name %s -> %s",
302 up_write(&_hash_lock
);
308 * Is there such a device as 'old' ?
310 hc
= __get_name_cell(old
);
312 DMWARN("asked to rename a non existent device %s -> %s",
314 up_write(&_hash_lock
);
320 * rename and move the name cell.
322 list_del(&hc
->name_list
);
325 list_add(&hc
->name_list
, _name_buckets
+ hash_str(new_name
));
328 * Wake up any dm event waiters.
330 table
= dm_get_table(hc
->md
);
332 dm_table_event(table
);
336 dm_kobject_uevent(hc
->md
, KOBJ_CHANGE
, cookie
);
339 up_write(&_hash_lock
);
344 /*-----------------------------------------------------------------
345 * Implementation of the ioctl commands
346 *---------------------------------------------------------------*/
348 * All the ioctl commands get dispatched to functions with this
351 typedef int (*ioctl_fn
)(struct dm_ioctl
*param
, size_t param_size
);
353 static int remove_all(struct dm_ioctl
*param
, size_t param_size
)
355 dm_hash_remove_all(1);
356 param
->data_size
= 0;
361 * Round up the ptr to an 8-byte boundary.
364 static inline void *align_ptr(void *ptr
)
366 return (void *) (((size_t) (ptr
+ ALIGN_MASK
)) & ~ALIGN_MASK
);
370 * Retrieves the data payload buffer from an already allocated
373 static void *get_result_buffer(struct dm_ioctl
*param
, size_t param_size
,
376 param
->data_start
= align_ptr(param
+ 1) - (void *) param
;
378 if (param
->data_start
< param_size
)
379 *len
= param_size
- param
->data_start
;
383 return ((void *) param
) + param
->data_start
;
386 static int list_devices(struct dm_ioctl
*param
, size_t param_size
)
389 struct hash_cell
*hc
;
390 size_t len
, needed
= 0;
391 struct gendisk
*disk
;
392 struct dm_name_list
*nl
, *old_nl
= NULL
;
394 down_write(&_hash_lock
);
397 * Loop through all the devices working out how much
400 for (i
= 0; i
< NUM_BUCKETS
; i
++) {
401 list_for_each_entry (hc
, _name_buckets
+ i
, name_list
) {
402 needed
+= sizeof(struct dm_name_list
);
403 needed
+= strlen(hc
->name
) + 1;
404 needed
+= ALIGN_MASK
;
409 * Grab our output buffer.
411 nl
= get_result_buffer(param
, param_size
, &len
);
413 param
->flags
|= DM_BUFFER_FULL_FLAG
;
416 param
->data_size
= param
->data_start
+ needed
;
418 nl
->dev
= 0; /* Flags no data */
421 * Now loop through filling out the names.
423 for (i
= 0; i
< NUM_BUCKETS
; i
++) {
424 list_for_each_entry (hc
, _name_buckets
+ i
, name_list
) {
426 old_nl
->next
= (uint32_t) ((void *) nl
-
428 disk
= dm_disk(hc
->md
);
429 nl
->dev
= huge_encode_dev(disk_devt(disk
));
431 strcpy(nl
->name
, hc
->name
);
434 nl
= align_ptr(((void *) ++nl
) + strlen(hc
->name
) + 1);
439 up_write(&_hash_lock
);
443 static void list_version_get_needed(struct target_type
*tt
, void *needed_param
)
445 size_t *needed
= needed_param
;
447 *needed
+= sizeof(struct dm_target_versions
);
448 *needed
+= strlen(tt
->name
);
449 *needed
+= ALIGN_MASK
;
452 static void list_version_get_info(struct target_type
*tt
, void *param
)
454 struct vers_iter
*info
= param
;
456 /* Check space - it might have changed since the first iteration */
457 if ((char *)info
->vers
+ sizeof(tt
->version
) + strlen(tt
->name
) + 1 >
460 info
->flags
= DM_BUFFER_FULL_FLAG
;
465 info
->old_vers
->next
= (uint32_t) ((void *)info
->vers
-
466 (void *)info
->old_vers
);
467 info
->vers
->version
[0] = tt
->version
[0];
468 info
->vers
->version
[1] = tt
->version
[1];
469 info
->vers
->version
[2] = tt
->version
[2];
470 info
->vers
->next
= 0;
471 strcpy(info
->vers
->name
, tt
->name
);
473 info
->old_vers
= info
->vers
;
474 info
->vers
= align_ptr(((void *) ++info
->vers
) + strlen(tt
->name
) + 1);
477 static int list_versions(struct dm_ioctl
*param
, size_t param_size
)
479 size_t len
, needed
= 0;
480 struct dm_target_versions
*vers
;
481 struct vers_iter iter_info
;
484 * Loop through all the devices working out how much
487 dm_target_iterate(list_version_get_needed
, &needed
);
490 * Grab our output buffer.
492 vers
= get_result_buffer(param
, param_size
, &len
);
494 param
->flags
|= DM_BUFFER_FULL_FLAG
;
497 param
->data_size
= param
->data_start
+ needed
;
499 iter_info
.param_size
= param_size
;
500 iter_info
.old_vers
= NULL
;
501 iter_info
.vers
= vers
;
503 iter_info
.end
= (char *)vers
+len
;
506 * Now loop through filling out the names & versions.
508 dm_target_iterate(list_version_get_info
, &iter_info
);
509 param
->flags
|= iter_info
.flags
;
517 static int check_name(const char *name
)
519 if (strchr(name
, '/')) {
520 DMWARN("invalid device name");
528 * Fills in a dm_ioctl structure, ready for sending back to
531 static int __dev_status(struct mapped_device
*md
, struct dm_ioctl
*param
)
533 struct gendisk
*disk
= dm_disk(md
);
534 struct dm_table
*table
;
536 param
->flags
&= ~(DM_SUSPEND_FLAG
| DM_READONLY_FLAG
|
537 DM_ACTIVE_PRESENT_FLAG
);
539 if (dm_suspended(md
))
540 param
->flags
|= DM_SUSPEND_FLAG
;
542 param
->dev
= huge_encode_dev(disk_devt(disk
));
545 * Yes, this will be out of date by the time it gets back
546 * to userland, but it is still very useful for
549 param
->open_count
= dm_open_count(md
);
551 if (get_disk_ro(disk
))
552 param
->flags
|= DM_READONLY_FLAG
;
554 param
->event_nr
= dm_get_event_nr(md
);
556 table
= dm_get_table(md
);
558 param
->flags
|= DM_ACTIVE_PRESENT_FLAG
;
559 param
->target_count
= dm_table_get_num_targets(table
);
562 param
->target_count
= 0;
567 static int dev_create(struct dm_ioctl
*param
, size_t param_size
)
569 int r
, m
= DM_ANY_MINOR
;
570 struct mapped_device
*md
;
572 r
= check_name(param
->name
);
576 if (param
->flags
& DM_PERSISTENT_DEV_FLAG
)
577 m
= MINOR(huge_decode_dev(param
->dev
));
579 r
= dm_create(m
, &md
);
583 r
= dm_hash_insert(param
->name
, *param
->uuid
? param
->uuid
: NULL
, md
);
589 param
->flags
&= ~DM_INACTIVE_PRESENT_FLAG
;
591 r
= __dev_status(md
, param
);
598 * Always use UUID for lookups if it's present, otherwise use name or dev.
600 static struct hash_cell
*__find_device_hash_cell(struct dm_ioctl
*param
)
602 struct mapped_device
*md
;
606 return __get_uuid_cell(param
->uuid
);
609 return __get_name_cell(param
->name
);
611 md
= dm_get_md(huge_decode_dev(param
->dev
));
615 mdptr
= dm_get_mdptr(md
);
623 static struct mapped_device
*find_device(struct dm_ioctl
*param
)
625 struct hash_cell
*hc
;
626 struct mapped_device
*md
= NULL
;
628 down_read(&_hash_lock
);
629 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
;
656 struct mapped_device
*md
;
659 down_write(&_hash_lock
);
660 hc
= __find_device_hash_cell(param
);
663 DMWARN("device doesn't appear to be in the dev hash table.");
664 up_write(&_hash_lock
);
671 * Ensure the device is not open and nothing further can open it.
673 r
= dm_lock_for_deletion(md
);
675 DMWARN("unable to remove open device %s", hc
->name
);
676 up_write(&_hash_lock
);
682 up_write(&_hash_lock
);
684 dm_kobject_uevent(md
, KOBJ_REMOVE
, param
->event_nr
);
687 param
->data_size
= 0;
692 * Check a string doesn't overrun the chunk of
693 * memory we copied from userland.
695 static int invalid_str(char *str
, void *end
)
697 while ((void *) str
< end
)
704 static int dev_rename(struct dm_ioctl
*param
, size_t param_size
)
707 char *new_name
= (char *) param
+ param
->data_start
;
709 if (new_name
< param
->data
||
710 invalid_str(new_name
, (void *) param
+ param_size
) ||
711 strlen(new_name
) > DM_NAME_LEN
- 1) {
712 DMWARN("Invalid new logical volume name supplied.");
716 r
= check_name(new_name
);
720 param
->data_size
= 0;
721 return dm_hash_rename(param
->event_nr
, param
->name
, new_name
);
724 static int dev_set_geometry(struct dm_ioctl
*param
, size_t param_size
)
727 struct mapped_device
*md
;
728 struct hd_geometry geometry
;
729 unsigned long indata
[4];
730 char *geostr
= (char *) param
+ param
->data_start
;
732 md
= find_device(param
);
736 if (geostr
< param
->data
||
737 invalid_str(geostr
, (void *) param
+ param_size
)) {
738 DMWARN("Invalid geometry supplied.");
742 x
= sscanf(geostr
, "%lu %lu %lu %lu", indata
,
743 indata
+ 1, indata
+ 2, indata
+ 3);
746 DMWARN("Unable to interpret geometry settings.");
750 if (indata
[0] > 65535 || indata
[1] > 255 ||
751 indata
[2] > 255 || indata
[3] > ULONG_MAX
) {
752 DMWARN("Geometry exceeds range limits.");
756 geometry
.cylinders
= indata
[0];
757 geometry
.heads
= indata
[1];
758 geometry
.sectors
= indata
[2];
759 geometry
.start
= indata
[3];
761 r
= dm_set_geometry(md
, &geometry
);
763 r
= __dev_status(md
, param
);
765 param
->data_size
= 0;
772 static int do_suspend(struct dm_ioctl
*param
)
775 unsigned suspend_flags
= DM_SUSPEND_LOCKFS_FLAG
;
776 struct mapped_device
*md
;
778 md
= find_device(param
);
782 if (param
->flags
& DM_SKIP_LOCKFS_FLAG
)
783 suspend_flags
&= ~DM_SUSPEND_LOCKFS_FLAG
;
784 if (param
->flags
& DM_NOFLUSH_FLAG
)
785 suspend_flags
|= DM_SUSPEND_NOFLUSH_FLAG
;
787 if (!dm_suspended(md
))
788 r
= dm_suspend(md
, suspend_flags
);
791 r
= __dev_status(md
, param
);
797 static int do_resume(struct dm_ioctl
*param
)
800 unsigned suspend_flags
= DM_SUSPEND_LOCKFS_FLAG
;
801 struct hash_cell
*hc
;
802 struct mapped_device
*md
;
803 struct dm_table
*new_map
;
805 down_write(&_hash_lock
);
807 hc
= __find_device_hash_cell(param
);
809 DMWARN("device doesn't appear to be in the dev hash table.");
810 up_write(&_hash_lock
);
816 new_map
= hc
->new_map
;
818 param
->flags
&= ~DM_INACTIVE_PRESENT_FLAG
;
820 up_write(&_hash_lock
);
822 /* Do we need to load a new map ? */
824 /* Suspend if it isn't already suspended */
825 if (param
->flags
& DM_SKIP_LOCKFS_FLAG
)
826 suspend_flags
&= ~DM_SUSPEND_LOCKFS_FLAG
;
827 if (param
->flags
& DM_NOFLUSH_FLAG
)
828 suspend_flags
|= DM_SUSPEND_NOFLUSH_FLAG
;
829 if (!dm_suspended(md
))
830 dm_suspend(md
, suspend_flags
);
832 r
= dm_swap_table(md
, new_map
);
834 dm_table_destroy(new_map
);
839 if (dm_table_get_mode(new_map
) & FMODE_WRITE
)
840 set_disk_ro(dm_disk(md
), 0);
842 set_disk_ro(dm_disk(md
), 1);
845 if (dm_suspended(md
))
850 dm_kobject_uevent(md
, KOBJ_CHANGE
, param
->event_nr
);
851 r
= __dev_status(md
, param
);
859 * Set or unset the suspension state of a device.
860 * If the device already is in the requested state we just return its status.
862 static int dev_suspend(struct dm_ioctl
*param
, size_t param_size
)
864 if (param
->flags
& DM_SUSPEND_FLAG
)
865 return do_suspend(param
);
867 return do_resume(param
);
871 * Copies device info back to user space, used by
872 * the create and info ioctls.
874 static int dev_status(struct dm_ioctl
*param
, size_t param_size
)
877 struct mapped_device
*md
;
879 md
= find_device(param
);
883 r
= __dev_status(md
, param
);
889 * Build up the status struct for each target
891 static void retrieve_status(struct dm_table
*table
,
892 struct dm_ioctl
*param
, size_t param_size
)
894 unsigned int i
, num_targets
;
895 struct dm_target_spec
*spec
;
896 char *outbuf
, *outptr
;
898 size_t remaining
, len
, used
= 0;
900 outptr
= outbuf
= get_result_buffer(param
, param_size
, &len
);
902 if (param
->flags
& DM_STATUS_TABLE_FLAG
)
903 type
= STATUSTYPE_TABLE
;
905 type
= STATUSTYPE_INFO
;
907 /* Get all the target info */
908 num_targets
= dm_table_get_num_targets(table
);
909 for (i
= 0; i
< num_targets
; i
++) {
910 struct dm_target
*ti
= dm_table_get_target(table
, i
);
912 remaining
= len
- (outptr
- outbuf
);
913 if (remaining
<= sizeof(struct dm_target_spec
)) {
914 param
->flags
|= DM_BUFFER_FULL_FLAG
;
918 spec
= (struct dm_target_spec
*) outptr
;
921 spec
->sector_start
= ti
->begin
;
922 spec
->length
= ti
->len
;
923 strncpy(spec
->target_type
, ti
->type
->name
,
924 sizeof(spec
->target_type
));
926 outptr
+= sizeof(struct dm_target_spec
);
927 remaining
= len
- (outptr
- outbuf
);
928 if (remaining
<= 0) {
929 param
->flags
|= DM_BUFFER_FULL_FLAG
;
933 /* Get the status/table string from the target driver */
934 if (ti
->type
->status
) {
935 if (ti
->type
->status(ti
, type
, outptr
, remaining
)) {
936 param
->flags
|= DM_BUFFER_FULL_FLAG
;
942 outptr
+= strlen(outptr
) + 1;
943 used
= param
->data_start
+ (outptr
- outbuf
);
945 outptr
= align_ptr(outptr
);
946 spec
->next
= outptr
- outbuf
;
950 param
->data_size
= used
;
952 param
->target_count
= num_targets
;
956 * Wait for a device to report an event
958 static int dev_wait(struct dm_ioctl
*param
, size_t param_size
)
961 struct mapped_device
*md
;
962 struct dm_table
*table
;
964 md
= find_device(param
);
969 * Wait for a notification event
971 if (dm_wait_event(md
, param
->event_nr
)) {
977 * The userland program is going to want to know what
978 * changed to trigger the event, so we may as well tell
979 * him and save an ioctl.
981 r
= __dev_status(md
, param
);
985 table
= dm_get_table(md
);
987 retrieve_status(table
, param
, param_size
);
996 static inline fmode_t
get_mode(struct dm_ioctl
*param
)
998 fmode_t mode
= FMODE_READ
| FMODE_WRITE
;
1000 if (param
->flags
& DM_READONLY_FLAG
)
1006 static int next_target(struct dm_target_spec
*last
, uint32_t next
, void *end
,
1007 struct dm_target_spec
**spec
, char **target_params
)
1009 *spec
= (struct dm_target_spec
*) ((unsigned char *) last
+ next
);
1010 *target_params
= (char *) (*spec
+ 1);
1012 if (*spec
< (last
+ 1))
1015 return invalid_str(*target_params
, end
);
1018 static int populate_table(struct dm_table
*table
,
1019 struct dm_ioctl
*param
, size_t param_size
)
1023 struct dm_target_spec
*spec
= (struct dm_target_spec
*) param
;
1024 uint32_t next
= param
->data_start
;
1025 void *end
= (void *) param
+ param_size
;
1026 char *target_params
;
1028 if (!param
->target_count
) {
1029 DMWARN("populate_table: no targets specified");
1033 for (i
= 0; i
< param
->target_count
; i
++) {
1035 r
= next_target(spec
, next
, end
, &spec
, &target_params
);
1037 DMWARN("unable to find target");
1041 r
= dm_table_add_target(table
, spec
->target_type
,
1042 (sector_t
) spec
->sector_start
,
1043 (sector_t
) spec
->length
,
1046 DMWARN("error adding target to table");
1053 r
= dm_table_set_type(table
);
1055 DMWARN("unable to set table type");
1059 return dm_table_complete(table
);
1062 static int table_prealloc_integrity(struct dm_table
*t
,
1063 struct mapped_device
*md
)
1065 struct list_head
*devices
= dm_table_get_devices(t
);
1066 struct dm_dev_internal
*dd
;
1068 list_for_each_entry(dd
, devices
, list
)
1069 if (bdev_get_integrity(dd
->dm_dev
.bdev
))
1070 return blk_integrity_register(dm_disk(md
), NULL
);
1075 static int table_load(struct dm_ioctl
*param
, size_t param_size
)
1078 struct hash_cell
*hc
;
1080 struct mapped_device
*md
;
1082 md
= find_device(param
);
1086 r
= dm_table_create(&t
, get_mode(param
), param
->target_count
, md
);
1090 r
= populate_table(t
, param
, param_size
);
1092 dm_table_destroy(t
);
1096 r
= table_prealloc_integrity(t
, md
);
1098 DMERR("%s: could not register integrity profile.",
1099 dm_device_name(md
));
1100 dm_table_destroy(t
);
1104 r
= dm_table_alloc_md_mempools(t
);
1106 DMWARN("unable to allocate mempools for this table");
1107 dm_table_destroy(t
);
1111 down_write(&_hash_lock
);
1112 hc
= dm_get_mdptr(md
);
1113 if (!hc
|| hc
->md
!= md
) {
1114 DMWARN("device has been removed from the dev hash table.");
1115 dm_table_destroy(t
);
1116 up_write(&_hash_lock
);
1122 dm_table_destroy(hc
->new_map
);
1124 up_write(&_hash_lock
);
1126 param
->flags
|= DM_INACTIVE_PRESENT_FLAG
;
1127 r
= __dev_status(md
, param
);
1135 static int table_clear(struct dm_ioctl
*param
, size_t param_size
)
1138 struct hash_cell
*hc
;
1139 struct mapped_device
*md
;
1141 down_write(&_hash_lock
);
1143 hc
= __find_device_hash_cell(param
);
1145 DMWARN("device doesn't appear to be in the dev hash table.");
1146 up_write(&_hash_lock
);
1151 dm_table_destroy(hc
->new_map
);
1155 param
->flags
&= ~DM_INACTIVE_PRESENT_FLAG
;
1157 r
= __dev_status(hc
->md
, param
);
1159 up_write(&_hash_lock
);
1165 * Retrieves a list of devices used by a particular dm device.
1167 static void retrieve_deps(struct dm_table
*table
,
1168 struct dm_ioctl
*param
, size_t param_size
)
1170 unsigned int count
= 0;
1171 struct list_head
*tmp
;
1173 struct dm_dev_internal
*dd
;
1174 struct dm_target_deps
*deps
;
1176 deps
= get_result_buffer(param
, param_size
, &len
);
1179 * Count the devices.
1181 list_for_each (tmp
, dm_table_get_devices(table
))
1185 * Check we have enough space.
1187 needed
= sizeof(*deps
) + (sizeof(*deps
->dev
) * count
);
1189 param
->flags
|= DM_BUFFER_FULL_FLAG
;
1194 * Fill in the devices.
1196 deps
->count
= count
;
1198 list_for_each_entry (dd
, dm_table_get_devices(table
), list
)
1199 deps
->dev
[count
++] = huge_encode_dev(dd
->dm_dev
.bdev
->bd_dev
);
1201 param
->data_size
= param
->data_start
+ needed
;
1204 static int table_deps(struct dm_ioctl
*param
, size_t param_size
)
1207 struct mapped_device
*md
;
1208 struct dm_table
*table
;
1210 md
= find_device(param
);
1214 r
= __dev_status(md
, param
);
1218 table
= dm_get_table(md
);
1220 retrieve_deps(table
, param
, param_size
);
1221 dm_table_put(table
);
1230 * Return the status of a device as a text string for each
1233 static int table_status(struct dm_ioctl
*param
, size_t param_size
)
1236 struct mapped_device
*md
;
1237 struct dm_table
*table
;
1239 md
= find_device(param
);
1243 r
= __dev_status(md
, param
);
1247 table
= dm_get_table(md
);
1249 retrieve_status(table
, param
, param_size
);
1250 dm_table_put(table
);
1259 * Pass a message to the target that's at the supplied device offset.
1261 static int target_message(struct dm_ioctl
*param
, size_t param_size
)
1265 struct mapped_device
*md
;
1266 struct dm_table
*table
;
1267 struct dm_target
*ti
;
1268 struct dm_target_msg
*tmsg
= (void *) param
+ param
->data_start
;
1270 md
= find_device(param
);
1274 r
= __dev_status(md
, param
);
1278 if (tmsg
< (struct dm_target_msg
*) param
->data
||
1279 invalid_str(tmsg
->message
, (void *) param
+ param_size
)) {
1280 DMWARN("Invalid target message parameters.");
1285 r
= dm_split_args(&argc
, &argv
, tmsg
->message
);
1287 DMWARN("Failed to split target message parameters");
1291 table
= dm_get_table(md
);
1295 ti
= dm_table_find_target(table
, tmsg
->sector
);
1296 if (!dm_target_is_valid(ti
)) {
1297 DMWARN("Target message sector outside device.");
1299 } else if (ti
->type
->message
)
1300 r
= ti
->type
->message(ti
, argc
, argv
);
1302 DMWARN("Target type does not support messages");
1306 dm_table_put(table
);
1310 param
->data_size
= 0;
1315 /*-----------------------------------------------------------------
1316 * Implementation of open/close/ioctl on the special char
1318 *---------------------------------------------------------------*/
1319 static ioctl_fn
lookup_ioctl(unsigned int cmd
)
1325 {DM_VERSION_CMD
, NULL
}, /* version is dealt with elsewhere */
1326 {DM_REMOVE_ALL_CMD
, remove_all
},
1327 {DM_LIST_DEVICES_CMD
, list_devices
},
1329 {DM_DEV_CREATE_CMD
, dev_create
},
1330 {DM_DEV_REMOVE_CMD
, dev_remove
},
1331 {DM_DEV_RENAME_CMD
, dev_rename
},
1332 {DM_DEV_SUSPEND_CMD
, dev_suspend
},
1333 {DM_DEV_STATUS_CMD
, dev_status
},
1334 {DM_DEV_WAIT_CMD
, dev_wait
},
1336 {DM_TABLE_LOAD_CMD
, table_load
},
1337 {DM_TABLE_CLEAR_CMD
, table_clear
},
1338 {DM_TABLE_DEPS_CMD
, table_deps
},
1339 {DM_TABLE_STATUS_CMD
, table_status
},
1341 {DM_LIST_VERSIONS_CMD
, list_versions
},
1343 {DM_TARGET_MSG_CMD
, target_message
},
1344 {DM_DEV_SET_GEOMETRY_CMD
, dev_set_geometry
}
1347 return (cmd
>= ARRAY_SIZE(_ioctls
)) ? NULL
: _ioctls
[cmd
].fn
;
1351 * As well as checking the version compatibility this always
1352 * copies the kernel interface version out.
1354 static int check_version(unsigned int cmd
, struct dm_ioctl __user
*user
)
1356 uint32_t version
[3];
1359 if (copy_from_user(version
, user
->version
, sizeof(version
)))
1362 if ((DM_VERSION_MAJOR
!= version
[0]) ||
1363 (DM_VERSION_MINOR
< version
[1])) {
1364 DMWARN("ioctl interface mismatch: "
1365 "kernel(%u.%u.%u), user(%u.%u.%u), cmd(%d)",
1366 DM_VERSION_MAJOR
, DM_VERSION_MINOR
,
1367 DM_VERSION_PATCHLEVEL
,
1368 version
[0], version
[1], version
[2], cmd
);
1373 * Fill in the kernel version.
1375 version
[0] = DM_VERSION_MAJOR
;
1376 version
[1] = DM_VERSION_MINOR
;
1377 version
[2] = DM_VERSION_PATCHLEVEL
;
1378 if (copy_to_user(user
->version
, version
, sizeof(version
)))
1384 static void free_params(struct dm_ioctl
*param
)
1389 static int copy_params(struct dm_ioctl __user
*user
, struct dm_ioctl
**param
)
1391 struct dm_ioctl tmp
, *dmi
;
1393 if (copy_from_user(&tmp
, user
, sizeof(tmp
) - sizeof(tmp
.data
)))
1396 if (tmp
.data_size
< (sizeof(tmp
) - sizeof(tmp
.data
)))
1399 dmi
= vmalloc(tmp
.data_size
);
1403 if (copy_from_user(dmi
, user
, tmp
.data_size
)) {
1412 static int validate_params(uint cmd
, struct dm_ioctl
*param
)
1414 /* Always clear this flag */
1415 param
->flags
&= ~DM_BUFFER_FULL_FLAG
;
1417 /* Ignores parameters */
1418 if (cmd
== DM_REMOVE_ALL_CMD
||
1419 cmd
== DM_LIST_DEVICES_CMD
||
1420 cmd
== DM_LIST_VERSIONS_CMD
)
1423 if ((cmd
== DM_DEV_CREATE_CMD
)) {
1424 if (!*param
->name
) {
1425 DMWARN("name not supplied when creating device");
1428 } else if ((*param
->uuid
&& *param
->name
)) {
1429 DMWARN("only supply one of name or uuid, cmd(%u)", cmd
);
1433 /* Ensure strings are terminated */
1434 param
->name
[DM_NAME_LEN
- 1] = '\0';
1435 param
->uuid
[DM_UUID_LEN
- 1] = '\0';
1440 static int ctl_ioctl(uint command
, struct dm_ioctl __user
*user
)
1444 struct dm_ioctl
*uninitialized_var(param
);
1448 /* only root can play with this */
1449 if (!capable(CAP_SYS_ADMIN
))
1452 if (_IOC_TYPE(command
) != DM_IOCTL
)
1455 cmd
= _IOC_NR(command
);
1458 * Check the interface version passed in. This also
1459 * writes out the kernel's interface version.
1461 r
= check_version(cmd
, user
);
1466 * Nothing more to do for the version command.
1468 if (cmd
== DM_VERSION_CMD
)
1471 fn
= lookup_ioctl(cmd
);
1473 DMWARN("dm_ctl_ioctl: unknown command 0x%x", command
);
1478 * Trying to avoid low memory issues when a device is
1481 current
->flags
|= PF_MEMALLOC
;
1484 * Copy the parameters into kernel space.
1486 r
= copy_params(user
, ¶m
);
1488 current
->flags
&= ~PF_MEMALLOC
;
1493 r
= validate_params(cmd
, param
);
1497 param_size
= param
->data_size
;
1498 param
->data_size
= sizeof(*param
);
1499 r
= fn(param
, param_size
);
1502 * Copy the results back to userland.
1504 if (!r
&& copy_to_user(user
, param
, param
->data_size
))
1512 static long dm_ctl_ioctl(struct file
*file
, uint command
, ulong u
)
1514 return (long)ctl_ioctl(command
, (struct dm_ioctl __user
*)u
);
1517 #ifdef CONFIG_COMPAT
1518 static long dm_compat_ctl_ioctl(struct file
*file
, uint command
, ulong u
)
1520 return (long)dm_ctl_ioctl(file
, command
, (ulong
) compat_ptr(u
));
1523 #define dm_compat_ctl_ioctl NULL
1526 static const struct file_operations _ctl_fops
= {
1527 .unlocked_ioctl
= dm_ctl_ioctl
,
1528 .compat_ioctl
= dm_compat_ctl_ioctl
,
1529 .owner
= THIS_MODULE
,
1532 static struct miscdevice _dm_misc
= {
1533 .minor
= MISC_DYNAMIC_MINOR
,
1535 .devnode
= "mapper/control",
1540 * Create misc character device and link to DM_DIR/control.
1542 int __init
dm_interface_init(void)
1550 r
= misc_register(&_dm_misc
);
1552 DMERR("misc_register failed for control device");
1557 DMINFO("%d.%d.%d%s initialised: %s", DM_VERSION_MAJOR
,
1558 DM_VERSION_MINOR
, DM_VERSION_PATCHLEVEL
, DM_VERSION_EXTRA
,
1563 void dm_interface_exit(void)
1565 if (misc_deregister(&_dm_misc
) < 0)
1566 DMERR("misc_deregister failed for control device");
1572 * dm_copy_name_and_uuid - Copy mapped device name & uuid into supplied buffers
1573 * @md: Pointer to mapped_device
1574 * @name: Buffer (size DM_NAME_LEN) for name
1575 * @uuid: Buffer (size DM_UUID_LEN) for uuid or empty string if uuid not defined
1577 int dm_copy_name_and_uuid(struct mapped_device
*md
, char *name
, char *uuid
)
1580 struct hash_cell
*hc
;
1586 down_read(&_hash_lock
);
1587 hc
= dm_get_mdptr(md
);
1588 if (!hc
|| hc
->md
!= md
) {
1594 strcpy(name
, hc
->name
);
1596 strcpy(uuid
, hc
->uuid
? : "");
1599 up_read(&_hash_lock
);