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
);
60 * Protects use of mdptr to obtain hash cell name and uuid from mapped device.
62 static DEFINE_MUTEX(dm_hash_cells_mutex
);
64 static void init_buckets(struct list_head
*buckets
)
68 for (i
= 0; i
< NUM_BUCKETS
; i
++)
69 INIT_LIST_HEAD(buckets
+ i
);
72 static int dm_hash_init(void)
74 init_buckets(_name_buckets
);
75 init_buckets(_uuid_buckets
);
79 static void dm_hash_exit(void)
81 dm_hash_remove_all(0);
84 /*-----------------------------------------------------------------
86 * We're not really concerned with the str hash function being
87 * fast since it's only used by the ioctl interface.
88 *---------------------------------------------------------------*/
89 static unsigned int hash_str(const char *str
)
91 const unsigned int hash_mult
= 2654435387U;
95 h
= (h
+ (unsigned int) *str
++) * hash_mult
;
97 return h
& MASK_BUCKETS
;
100 /*-----------------------------------------------------------------
101 * Code for looking up a device by name
102 *---------------------------------------------------------------*/
103 static struct hash_cell
*__get_name_cell(const char *str
)
105 struct hash_cell
*hc
;
106 unsigned int h
= hash_str(str
);
108 list_for_each_entry (hc
, _name_buckets
+ h
, name_list
)
109 if (!strcmp(hc
->name
, str
)) {
117 static struct hash_cell
*__get_uuid_cell(const char *str
)
119 struct hash_cell
*hc
;
120 unsigned int h
= hash_str(str
);
122 list_for_each_entry (hc
, _uuid_buckets
+ h
, uuid_list
)
123 if (!strcmp(hc
->uuid
, str
)) {
131 /*-----------------------------------------------------------------
132 * Inserting, removing and renaming a device.
133 *---------------------------------------------------------------*/
134 static struct hash_cell
*alloc_cell(const char *name
, const char *uuid
,
135 struct mapped_device
*md
)
137 struct hash_cell
*hc
;
139 hc
= kmalloc(sizeof(*hc
), GFP_KERNEL
);
143 hc
->name
= kstrdup(name
, GFP_KERNEL
);
153 hc
->uuid
= kstrdup(uuid
, GFP_KERNEL
);
161 INIT_LIST_HEAD(&hc
->name_list
);
162 INIT_LIST_HEAD(&hc
->uuid_list
);
168 static void free_cell(struct hash_cell
*hc
)
178 * The kdev_t and uuid of a device can never change once it is
179 * initially inserted.
181 static int dm_hash_insert(const char *name
, const char *uuid
, struct mapped_device
*md
)
183 struct hash_cell
*cell
, *hc
;
186 * Allocate the new cells.
188 cell
= alloc_cell(name
, uuid
, md
);
193 * Insert the cell into both hash tables.
195 down_write(&_hash_lock
);
196 hc
= __get_name_cell(name
);
202 list_add(&cell
->name_list
, _name_buckets
+ hash_str(name
));
205 hc
= __get_uuid_cell(uuid
);
207 list_del(&cell
->name_list
);
211 list_add(&cell
->uuid_list
, _uuid_buckets
+ hash_str(uuid
));
214 mutex_lock(&dm_hash_cells_mutex
);
215 dm_set_mdptr(md
, cell
);
216 mutex_unlock(&dm_hash_cells_mutex
);
217 up_write(&_hash_lock
);
222 up_write(&_hash_lock
);
227 static void __hash_remove(struct hash_cell
*hc
)
229 struct dm_table
*table
;
231 /* remove from the dev hash */
232 list_del(&hc
->uuid_list
);
233 list_del(&hc
->name_list
);
234 mutex_lock(&dm_hash_cells_mutex
);
235 dm_set_mdptr(hc
->md
, NULL
);
236 mutex_unlock(&dm_hash_cells_mutex
);
238 table
= dm_get_live_table(hc
->md
);
240 dm_table_event(table
);
245 dm_table_destroy(hc
->new_map
);
250 static void dm_hash_remove_all(int keep_open_devices
)
253 struct hash_cell
*hc
;
254 struct mapped_device
*md
;
259 down_write(&_hash_lock
);
261 for (i
= 0; i
< NUM_BUCKETS
; i
++) {
262 list_for_each_entry(hc
, _name_buckets
+ i
, name_list
) {
266 if (keep_open_devices
&& dm_lock_for_deletion(md
)) {
274 up_write(&_hash_lock
);
277 if (likely(keep_open_devices
))
280 dm_destroy_immediate(md
);
283 * Some mapped devices may be using other mapped
284 * devices, so repeat until we make no further
285 * progress. If a new mapped device is created
286 * here it will also get removed.
292 up_write(&_hash_lock
);
295 DMWARN("remove_all left %d open device(s)", dev_skipped
);
299 * Set the uuid of a hash_cell that isn't already set.
301 static void __set_cell_uuid(struct hash_cell
*hc
, char *new_uuid
)
303 mutex_lock(&dm_hash_cells_mutex
);
305 mutex_unlock(&dm_hash_cells_mutex
);
307 list_add(&hc
->uuid_list
, _uuid_buckets
+ hash_str(new_uuid
));
311 * Changes the name of a hash_cell and returns the old name for
312 * the caller to free.
314 static char *__change_cell_name(struct hash_cell
*hc
, char *new_name
)
319 * Rename and move the name cell.
321 list_del(&hc
->name_list
);
324 mutex_lock(&dm_hash_cells_mutex
);
326 mutex_unlock(&dm_hash_cells_mutex
);
328 list_add(&hc
->name_list
, _name_buckets
+ hash_str(new_name
));
333 static struct mapped_device
*dm_hash_rename(struct dm_ioctl
*param
,
336 char *new_data
, *old_name
= NULL
;
337 struct hash_cell
*hc
;
338 struct dm_table
*table
;
339 struct mapped_device
*md
;
340 unsigned change_uuid
= (param
->flags
& DM_UUID_FLAG
) ? 1 : 0;
345 new_data
= kstrdup(new, GFP_KERNEL
);
347 return ERR_PTR(-ENOMEM
);
349 down_write(&_hash_lock
);
355 hc
= __get_uuid_cell(new);
357 hc
= __get_name_cell(new);
360 DMWARN("Unable to change %s on mapped device %s to one that "
361 "already exists: %s",
362 change_uuid
? "uuid" : "name",
365 up_write(&_hash_lock
);
367 return ERR_PTR(-EBUSY
);
371 * Is there such a device as 'old' ?
373 hc
= __get_name_cell(param
->name
);
375 DMWARN("Unable to rename non-existent device, %s to %s%s",
376 param
->name
, change_uuid
? "uuid " : "", new);
377 up_write(&_hash_lock
);
379 return ERR_PTR(-ENXIO
);
383 * Does this device already have a uuid?
385 if (change_uuid
&& hc
->uuid
) {
386 DMWARN("Unable to change uuid of mapped device %s to %s "
387 "because uuid is already set to %s",
388 param
->name
, new, hc
->uuid
);
390 up_write(&_hash_lock
);
392 return ERR_PTR(-EINVAL
);
396 __set_cell_uuid(hc
, new_data
);
398 old_name
= __change_cell_name(hc
, new_data
);
401 * Wake up any dm event waiters.
403 table
= dm_get_live_table(hc
->md
);
405 dm_table_event(table
);
409 if (!dm_kobject_uevent(hc
->md
, KOBJ_CHANGE
, param
->event_nr
))
410 param
->flags
|= DM_UEVENT_GENERATED_FLAG
;
413 up_write(&_hash_lock
);
419 /*-----------------------------------------------------------------
420 * Implementation of the ioctl commands
421 *---------------------------------------------------------------*/
423 * All the ioctl commands get dispatched to functions with this
426 typedef int (*ioctl_fn
)(struct dm_ioctl
*param
, size_t param_size
);
428 static int remove_all(struct dm_ioctl
*param
, size_t param_size
)
430 dm_hash_remove_all(1);
431 param
->data_size
= 0;
436 * Round up the ptr to an 8-byte boundary.
439 static inline void *align_ptr(void *ptr
)
441 return (void *) (((size_t) (ptr
+ ALIGN_MASK
)) & ~ALIGN_MASK
);
445 * Retrieves the data payload buffer from an already allocated
448 static void *get_result_buffer(struct dm_ioctl
*param
, size_t param_size
,
451 param
->data_start
= align_ptr(param
+ 1) - (void *) param
;
453 if (param
->data_start
< param_size
)
454 *len
= param_size
- param
->data_start
;
458 return ((void *) param
) + param
->data_start
;
461 static int list_devices(struct dm_ioctl
*param
, size_t param_size
)
464 struct hash_cell
*hc
;
465 size_t len
, needed
= 0;
466 struct gendisk
*disk
;
467 struct dm_name_list
*nl
, *old_nl
= NULL
;
469 down_write(&_hash_lock
);
472 * Loop through all the devices working out how much
475 for (i
= 0; i
< NUM_BUCKETS
; i
++) {
476 list_for_each_entry (hc
, _name_buckets
+ i
, name_list
) {
477 needed
+= sizeof(struct dm_name_list
);
478 needed
+= strlen(hc
->name
) + 1;
479 needed
+= ALIGN_MASK
;
484 * Grab our output buffer.
486 nl
= get_result_buffer(param
, param_size
, &len
);
488 param
->flags
|= DM_BUFFER_FULL_FLAG
;
491 param
->data_size
= param
->data_start
+ needed
;
493 nl
->dev
= 0; /* Flags no data */
496 * Now loop through filling out the names.
498 for (i
= 0; i
< NUM_BUCKETS
; i
++) {
499 list_for_each_entry (hc
, _name_buckets
+ i
, name_list
) {
501 old_nl
->next
= (uint32_t) ((void *) nl
-
503 disk
= dm_disk(hc
->md
);
504 nl
->dev
= huge_encode_dev(disk_devt(disk
));
506 strcpy(nl
->name
, hc
->name
);
509 nl
= align_ptr(((void *) ++nl
) + strlen(hc
->name
) + 1);
514 up_write(&_hash_lock
);
518 static void list_version_get_needed(struct target_type
*tt
, void *needed_param
)
520 size_t *needed
= needed_param
;
522 *needed
+= sizeof(struct dm_target_versions
);
523 *needed
+= strlen(tt
->name
);
524 *needed
+= ALIGN_MASK
;
527 static void list_version_get_info(struct target_type
*tt
, void *param
)
529 struct vers_iter
*info
= param
;
531 /* Check space - it might have changed since the first iteration */
532 if ((char *)info
->vers
+ sizeof(tt
->version
) + strlen(tt
->name
) + 1 >
535 info
->flags
= DM_BUFFER_FULL_FLAG
;
540 info
->old_vers
->next
= (uint32_t) ((void *)info
->vers
-
541 (void *)info
->old_vers
);
542 info
->vers
->version
[0] = tt
->version
[0];
543 info
->vers
->version
[1] = tt
->version
[1];
544 info
->vers
->version
[2] = tt
->version
[2];
545 info
->vers
->next
= 0;
546 strcpy(info
->vers
->name
, tt
->name
);
548 info
->old_vers
= info
->vers
;
549 info
->vers
= align_ptr(((void *) ++info
->vers
) + strlen(tt
->name
) + 1);
552 static int list_versions(struct dm_ioctl
*param
, size_t param_size
)
554 size_t len
, needed
= 0;
555 struct dm_target_versions
*vers
;
556 struct vers_iter iter_info
;
559 * Loop through all the devices working out how much
562 dm_target_iterate(list_version_get_needed
, &needed
);
565 * Grab our output buffer.
567 vers
= get_result_buffer(param
, param_size
, &len
);
569 param
->flags
|= DM_BUFFER_FULL_FLAG
;
572 param
->data_size
= param
->data_start
+ needed
;
574 iter_info
.param_size
= param_size
;
575 iter_info
.old_vers
= NULL
;
576 iter_info
.vers
= vers
;
578 iter_info
.end
= (char *)vers
+len
;
581 * Now loop through filling out the names & versions.
583 dm_target_iterate(list_version_get_info
, &iter_info
);
584 param
->flags
|= iter_info
.flags
;
590 static int check_name(const char *name
)
592 if (strchr(name
, '/')) {
593 DMWARN("invalid device name");
601 * On successful return, the caller must not attempt to acquire
602 * _hash_lock without first calling dm_table_put, because dm_table_destroy
603 * waits for this dm_table_put and could be called under this lock.
605 static struct dm_table
*dm_get_inactive_table(struct mapped_device
*md
)
607 struct hash_cell
*hc
;
608 struct dm_table
*table
= NULL
;
610 down_read(&_hash_lock
);
611 hc
= dm_get_mdptr(md
);
612 if (!hc
|| hc
->md
!= md
) {
613 DMWARN("device has been removed from the dev hash table.");
622 up_read(&_hash_lock
);
627 static struct dm_table
*dm_get_live_or_inactive_table(struct mapped_device
*md
,
628 struct dm_ioctl
*param
)
630 return (param
->flags
& DM_QUERY_INACTIVE_TABLE_FLAG
) ?
631 dm_get_inactive_table(md
) : dm_get_live_table(md
);
635 * Fills in a dm_ioctl structure, ready for sending back to
638 static void __dev_status(struct mapped_device
*md
, struct dm_ioctl
*param
)
640 struct gendisk
*disk
= dm_disk(md
);
641 struct dm_table
*table
;
643 param
->flags
&= ~(DM_SUSPEND_FLAG
| DM_READONLY_FLAG
|
644 DM_ACTIVE_PRESENT_FLAG
);
646 if (dm_suspended_md(md
))
647 param
->flags
|= DM_SUSPEND_FLAG
;
649 param
->dev
= huge_encode_dev(disk_devt(disk
));
652 * Yes, this will be out of date by the time it gets back
653 * to userland, but it is still very useful for
656 param
->open_count
= dm_open_count(md
);
658 param
->event_nr
= dm_get_event_nr(md
);
659 param
->target_count
= 0;
661 table
= dm_get_live_table(md
);
663 if (!(param
->flags
& DM_QUERY_INACTIVE_TABLE_FLAG
)) {
664 if (get_disk_ro(disk
))
665 param
->flags
|= DM_READONLY_FLAG
;
666 param
->target_count
= dm_table_get_num_targets(table
);
670 param
->flags
|= DM_ACTIVE_PRESENT_FLAG
;
673 if (param
->flags
& DM_QUERY_INACTIVE_TABLE_FLAG
) {
674 table
= dm_get_inactive_table(md
);
676 if (!(dm_table_get_mode(table
) & FMODE_WRITE
))
677 param
->flags
|= DM_READONLY_FLAG
;
678 param
->target_count
= dm_table_get_num_targets(table
);
684 static int dev_create(struct dm_ioctl
*param
, size_t param_size
)
686 int r
, m
= DM_ANY_MINOR
;
687 struct mapped_device
*md
;
689 r
= check_name(param
->name
);
693 if (param
->flags
& DM_PERSISTENT_DEV_FLAG
)
694 m
= MINOR(huge_decode_dev(param
->dev
));
696 r
= dm_create(m
, &md
);
700 r
= dm_hash_insert(param
->name
, *param
->uuid
? param
->uuid
: NULL
, md
);
707 param
->flags
&= ~DM_INACTIVE_PRESENT_FLAG
;
709 __dev_status(md
, param
);
717 * Always use UUID for lookups if it's present, otherwise use name or dev.
719 static struct hash_cell
*__find_device_hash_cell(struct dm_ioctl
*param
)
721 struct mapped_device
*md
;
725 return __get_uuid_cell(param
->uuid
);
728 return __get_name_cell(param
->name
);
730 md
= dm_get_md(huge_decode_dev(param
->dev
));
734 mdptr
= dm_get_mdptr(md
);
742 static struct mapped_device
*find_device(struct dm_ioctl
*param
)
744 struct hash_cell
*hc
;
745 struct mapped_device
*md
= NULL
;
747 down_read(&_hash_lock
);
748 hc
= __find_device_hash_cell(param
);
753 * Sneakily write in both the name and the uuid
754 * while we have the cell.
756 strlcpy(param
->name
, hc
->name
, sizeof(param
->name
));
758 strlcpy(param
->uuid
, hc
->uuid
, sizeof(param
->uuid
));
760 param
->uuid
[0] = '\0';
763 param
->flags
|= DM_INACTIVE_PRESENT_FLAG
;
765 param
->flags
&= ~DM_INACTIVE_PRESENT_FLAG
;
767 up_read(&_hash_lock
);
772 static int dev_remove(struct dm_ioctl
*param
, size_t param_size
)
774 struct hash_cell
*hc
;
775 struct mapped_device
*md
;
778 down_write(&_hash_lock
);
779 hc
= __find_device_hash_cell(param
);
782 DMDEBUG_LIMIT("device doesn't appear to be in the dev hash table.");
783 up_write(&_hash_lock
);
790 * Ensure the device is not open and nothing further can open it.
792 r
= dm_lock_for_deletion(md
);
794 DMDEBUG_LIMIT("unable to remove open device %s", hc
->name
);
795 up_write(&_hash_lock
);
801 up_write(&_hash_lock
);
803 if (!dm_kobject_uevent(md
, KOBJ_REMOVE
, param
->event_nr
))
804 param
->flags
|= DM_UEVENT_GENERATED_FLAG
;
812 * Check a string doesn't overrun the chunk of
813 * memory we copied from userland.
815 static int invalid_str(char *str
, void *end
)
817 while ((void *) str
< end
)
824 static int dev_rename(struct dm_ioctl
*param
, size_t param_size
)
827 char *new_data
= (char *) param
+ param
->data_start
;
828 struct mapped_device
*md
;
829 unsigned change_uuid
= (param
->flags
& DM_UUID_FLAG
) ? 1 : 0;
831 if (new_data
< param
->data
||
832 invalid_str(new_data
, (void *) param
+ param_size
) ||
833 strlen(new_data
) > (change_uuid
? DM_UUID_LEN
- 1 : DM_NAME_LEN
- 1)) {
834 DMWARN("Invalid new mapped device name or uuid string supplied.");
839 r
= check_name(new_data
);
844 md
= dm_hash_rename(param
, new_data
);
848 __dev_status(md
, param
);
854 static int dev_set_geometry(struct dm_ioctl
*param
, size_t param_size
)
857 struct mapped_device
*md
;
858 struct hd_geometry geometry
;
859 unsigned long indata
[4];
860 char *geostr
= (char *) param
+ param
->data_start
;
862 md
= find_device(param
);
866 if (geostr
< param
->data
||
867 invalid_str(geostr
, (void *) param
+ param_size
)) {
868 DMWARN("Invalid geometry supplied.");
872 x
= sscanf(geostr
, "%lu %lu %lu %lu", indata
,
873 indata
+ 1, indata
+ 2, indata
+ 3);
876 DMWARN("Unable to interpret geometry settings.");
880 if (indata
[0] > 65535 || indata
[1] > 255 ||
881 indata
[2] > 255 || indata
[3] > ULONG_MAX
) {
882 DMWARN("Geometry exceeds range limits.");
886 geometry
.cylinders
= indata
[0];
887 geometry
.heads
= indata
[1];
888 geometry
.sectors
= indata
[2];
889 geometry
.start
= indata
[3];
891 r
= dm_set_geometry(md
, &geometry
);
893 param
->data_size
= 0;
900 static int do_suspend(struct dm_ioctl
*param
)
903 unsigned suspend_flags
= DM_SUSPEND_LOCKFS_FLAG
;
904 struct mapped_device
*md
;
906 md
= find_device(param
);
910 if (param
->flags
& DM_SKIP_LOCKFS_FLAG
)
911 suspend_flags
&= ~DM_SUSPEND_LOCKFS_FLAG
;
912 if (param
->flags
& DM_NOFLUSH_FLAG
)
913 suspend_flags
|= DM_SUSPEND_NOFLUSH_FLAG
;
915 if (!dm_suspended_md(md
)) {
916 r
= dm_suspend(md
, suspend_flags
);
921 __dev_status(md
, param
);
929 static int do_resume(struct dm_ioctl
*param
)
932 unsigned suspend_flags
= DM_SUSPEND_LOCKFS_FLAG
;
933 struct hash_cell
*hc
;
934 struct mapped_device
*md
;
935 struct dm_table
*new_map
, *old_map
= NULL
;
937 down_write(&_hash_lock
);
939 hc
= __find_device_hash_cell(param
);
941 DMDEBUG_LIMIT("device doesn't appear to be in the dev hash table.");
942 up_write(&_hash_lock
);
948 new_map
= hc
->new_map
;
950 param
->flags
&= ~DM_INACTIVE_PRESENT_FLAG
;
952 up_write(&_hash_lock
);
954 /* Do we need to load a new map ? */
956 /* Suspend if it isn't already suspended */
957 if (param
->flags
& DM_SKIP_LOCKFS_FLAG
)
958 suspend_flags
&= ~DM_SUSPEND_LOCKFS_FLAG
;
959 if (param
->flags
& DM_NOFLUSH_FLAG
)
960 suspend_flags
|= DM_SUSPEND_NOFLUSH_FLAG
;
961 if (!dm_suspended_md(md
))
962 dm_suspend(md
, suspend_flags
);
964 old_map
= dm_swap_table(md
, new_map
);
965 if (IS_ERR(old_map
)) {
966 dm_table_destroy(new_map
);
968 return PTR_ERR(old_map
);
971 if (dm_table_get_mode(new_map
) & FMODE_WRITE
)
972 set_disk_ro(dm_disk(md
), 0);
974 set_disk_ro(dm_disk(md
), 1);
977 if (dm_suspended_md(md
)) {
979 if (!r
&& !dm_kobject_uevent(md
, KOBJ_CHANGE
, param
->event_nr
))
980 param
->flags
|= DM_UEVENT_GENERATED_FLAG
;
984 dm_table_destroy(old_map
);
987 __dev_status(md
, param
);
994 * Set or unset the suspension state of a device.
995 * If the device already is in the requested state we just return its status.
997 static int dev_suspend(struct dm_ioctl
*param
, size_t param_size
)
999 if (param
->flags
& DM_SUSPEND_FLAG
)
1000 return do_suspend(param
);
1002 return do_resume(param
);
1006 * Copies device info back to user space, used by
1007 * the create and info ioctls.
1009 static int dev_status(struct dm_ioctl
*param
, size_t param_size
)
1011 struct mapped_device
*md
;
1013 md
= find_device(param
);
1017 __dev_status(md
, param
);
1024 * Build up the status struct for each target
1026 static void retrieve_status(struct dm_table
*table
,
1027 struct dm_ioctl
*param
, size_t param_size
)
1029 unsigned int i
, num_targets
;
1030 struct dm_target_spec
*spec
;
1031 char *outbuf
, *outptr
;
1033 size_t remaining
, len
, used
= 0;
1035 outptr
= outbuf
= get_result_buffer(param
, param_size
, &len
);
1037 if (param
->flags
& DM_STATUS_TABLE_FLAG
)
1038 type
= STATUSTYPE_TABLE
;
1040 type
= STATUSTYPE_INFO
;
1042 /* Get all the target info */
1043 num_targets
= dm_table_get_num_targets(table
);
1044 for (i
= 0; i
< num_targets
; i
++) {
1045 struct dm_target
*ti
= dm_table_get_target(table
, i
);
1047 remaining
= len
- (outptr
- outbuf
);
1048 if (remaining
<= sizeof(struct dm_target_spec
)) {
1049 param
->flags
|= DM_BUFFER_FULL_FLAG
;
1053 spec
= (struct dm_target_spec
*) outptr
;
1056 spec
->sector_start
= ti
->begin
;
1057 spec
->length
= ti
->len
;
1058 strncpy(spec
->target_type
, ti
->type
->name
,
1059 sizeof(spec
->target_type
));
1061 outptr
+= sizeof(struct dm_target_spec
);
1062 remaining
= len
- (outptr
- outbuf
);
1063 if (remaining
<= 0) {
1064 param
->flags
|= DM_BUFFER_FULL_FLAG
;
1068 /* Get the status/table string from the target driver */
1069 if (ti
->type
->status
) {
1070 if (ti
->type
->status(ti
, type
, outptr
, remaining
)) {
1071 param
->flags
|= DM_BUFFER_FULL_FLAG
;
1077 outptr
+= strlen(outptr
) + 1;
1078 used
= param
->data_start
+ (outptr
- outbuf
);
1080 outptr
= align_ptr(outptr
);
1081 spec
->next
= outptr
- outbuf
;
1085 param
->data_size
= used
;
1087 param
->target_count
= num_targets
;
1091 * Wait for a device to report an event
1093 static int dev_wait(struct dm_ioctl
*param
, size_t param_size
)
1096 struct mapped_device
*md
;
1097 struct dm_table
*table
;
1099 md
= find_device(param
);
1104 * Wait for a notification event
1106 if (dm_wait_event(md
, param
->event_nr
)) {
1112 * The userland program is going to want to know what
1113 * changed to trigger the event, so we may as well tell
1114 * him and save an ioctl.
1116 __dev_status(md
, param
);
1118 table
= dm_get_live_or_inactive_table(md
, param
);
1120 retrieve_status(table
, param
, param_size
);
1121 dm_table_put(table
);
1130 static inline fmode_t
get_mode(struct dm_ioctl
*param
)
1132 fmode_t mode
= FMODE_READ
| FMODE_WRITE
;
1134 if (param
->flags
& DM_READONLY_FLAG
)
1140 static int next_target(struct dm_target_spec
*last
, uint32_t next
, void *end
,
1141 struct dm_target_spec
**spec
, char **target_params
)
1143 *spec
= (struct dm_target_spec
*) ((unsigned char *) last
+ next
);
1144 *target_params
= (char *) (*spec
+ 1);
1146 if (*spec
< (last
+ 1))
1149 return invalid_str(*target_params
, end
);
1152 static int populate_table(struct dm_table
*table
,
1153 struct dm_ioctl
*param
, size_t param_size
)
1157 struct dm_target_spec
*spec
= (struct dm_target_spec
*) param
;
1158 uint32_t next
= param
->data_start
;
1159 void *end
= (void *) param
+ param_size
;
1160 char *target_params
;
1162 if (!param
->target_count
) {
1163 DMWARN("populate_table: no targets specified");
1167 for (i
= 0; i
< param
->target_count
; i
++) {
1169 r
= next_target(spec
, next
, end
, &spec
, &target_params
);
1171 DMWARN("unable to find target");
1175 r
= dm_table_add_target(table
, spec
->target_type
,
1176 (sector_t
) spec
->sector_start
,
1177 (sector_t
) spec
->length
,
1180 DMWARN("error adding target to table");
1187 return dm_table_complete(table
);
1190 static int table_load(struct dm_ioctl
*param
, size_t param_size
)
1193 struct hash_cell
*hc
;
1195 struct mapped_device
*md
;
1197 md
= find_device(param
);
1201 r
= dm_table_create(&t
, get_mode(param
), param
->target_count
, md
);
1205 r
= populate_table(t
, param
, param_size
);
1207 dm_table_destroy(t
);
1211 /* Protect md->type and md->queue against concurrent table loads. */
1212 dm_lock_md_type(md
);
1213 if (dm_get_md_type(md
) == DM_TYPE_NONE
)
1214 /* Initial table load: acquire type of table. */
1215 dm_set_md_type(md
, dm_table_get_type(t
));
1216 else if (dm_get_md_type(md
) != dm_table_get_type(t
)) {
1217 DMWARN("can't change device type after initial table load.");
1218 dm_table_destroy(t
);
1219 dm_unlock_md_type(md
);
1224 /* setup md->queue to reflect md's type (may block) */
1225 r
= dm_setup_md_queue(md
);
1227 DMWARN("unable to set up device queue for new table.");
1228 dm_table_destroy(t
);
1229 dm_unlock_md_type(md
);
1232 dm_unlock_md_type(md
);
1234 /* stage inactive table */
1235 down_write(&_hash_lock
);
1236 hc
= dm_get_mdptr(md
);
1237 if (!hc
|| hc
->md
!= md
) {
1238 DMWARN("device has been removed from the dev hash table.");
1239 dm_table_destroy(t
);
1240 up_write(&_hash_lock
);
1246 dm_table_destroy(hc
->new_map
);
1248 up_write(&_hash_lock
);
1250 param
->flags
|= DM_INACTIVE_PRESENT_FLAG
;
1251 __dev_status(md
, param
);
1259 static int table_clear(struct dm_ioctl
*param
, size_t param_size
)
1261 struct hash_cell
*hc
;
1262 struct mapped_device
*md
;
1264 down_write(&_hash_lock
);
1266 hc
= __find_device_hash_cell(param
);
1268 DMDEBUG_LIMIT("device doesn't appear to be in the dev hash table.");
1269 up_write(&_hash_lock
);
1274 dm_table_destroy(hc
->new_map
);
1278 param
->flags
&= ~DM_INACTIVE_PRESENT_FLAG
;
1280 __dev_status(hc
->md
, param
);
1282 up_write(&_hash_lock
);
1289 * Retrieves a list of devices used by a particular dm device.
1291 static void retrieve_deps(struct dm_table
*table
,
1292 struct dm_ioctl
*param
, size_t param_size
)
1294 unsigned int count
= 0;
1295 struct list_head
*tmp
;
1297 struct dm_dev_internal
*dd
;
1298 struct dm_target_deps
*deps
;
1300 deps
= get_result_buffer(param
, param_size
, &len
);
1303 * Count the devices.
1305 list_for_each (tmp
, dm_table_get_devices(table
))
1309 * Check we have enough space.
1311 needed
= sizeof(*deps
) + (sizeof(*deps
->dev
) * count
);
1313 param
->flags
|= DM_BUFFER_FULL_FLAG
;
1318 * Fill in the devices.
1320 deps
->count
= count
;
1322 list_for_each_entry (dd
, dm_table_get_devices(table
), list
)
1323 deps
->dev
[count
++] = huge_encode_dev(dd
->dm_dev
.bdev
->bd_dev
);
1325 param
->data_size
= param
->data_start
+ needed
;
1328 static int table_deps(struct dm_ioctl
*param
, size_t param_size
)
1330 struct mapped_device
*md
;
1331 struct dm_table
*table
;
1333 md
= find_device(param
);
1337 __dev_status(md
, param
);
1339 table
= dm_get_live_or_inactive_table(md
, param
);
1341 retrieve_deps(table
, param
, param_size
);
1342 dm_table_put(table
);
1351 * Return the status of a device as a text string for each
1354 static int table_status(struct dm_ioctl
*param
, size_t param_size
)
1356 struct mapped_device
*md
;
1357 struct dm_table
*table
;
1359 md
= find_device(param
);
1363 __dev_status(md
, param
);
1365 table
= dm_get_live_or_inactive_table(md
, param
);
1367 retrieve_status(table
, param
, param_size
);
1368 dm_table_put(table
);
1377 * Pass a message to the target that's at the supplied device offset.
1379 static int target_message(struct dm_ioctl
*param
, size_t param_size
)
1383 struct mapped_device
*md
;
1384 struct dm_table
*table
;
1385 struct dm_target
*ti
;
1386 struct dm_target_msg
*tmsg
= (void *) param
+ param
->data_start
;
1388 md
= find_device(param
);
1392 if (tmsg
< (struct dm_target_msg
*) param
->data
||
1393 invalid_str(tmsg
->message
, (void *) param
+ param_size
)) {
1394 DMWARN("Invalid target message parameters.");
1399 r
= dm_split_args(&argc
, &argv
, tmsg
->message
);
1401 DMWARN("Failed to split target message parameters");
1405 table
= dm_get_live_table(md
);
1409 if (dm_deleting_md(md
)) {
1414 ti
= dm_table_find_target(table
, tmsg
->sector
);
1415 if (!dm_target_is_valid(ti
)) {
1416 DMWARN("Target message sector outside device.");
1418 } else if (ti
->type
->message
)
1419 r
= ti
->type
->message(ti
, argc
, argv
);
1421 DMWARN("Target type does not support messages");
1426 dm_table_put(table
);
1430 param
->data_size
= 0;
1435 /*-----------------------------------------------------------------
1436 * Implementation of open/close/ioctl on the special char
1438 *---------------------------------------------------------------*/
1439 static ioctl_fn
lookup_ioctl(unsigned int cmd
)
1445 {DM_VERSION_CMD
, NULL
}, /* version is dealt with elsewhere */
1446 {DM_REMOVE_ALL_CMD
, remove_all
},
1447 {DM_LIST_DEVICES_CMD
, list_devices
},
1449 {DM_DEV_CREATE_CMD
, dev_create
},
1450 {DM_DEV_REMOVE_CMD
, dev_remove
},
1451 {DM_DEV_RENAME_CMD
, dev_rename
},
1452 {DM_DEV_SUSPEND_CMD
, dev_suspend
},
1453 {DM_DEV_STATUS_CMD
, dev_status
},
1454 {DM_DEV_WAIT_CMD
, dev_wait
},
1456 {DM_TABLE_LOAD_CMD
, table_load
},
1457 {DM_TABLE_CLEAR_CMD
, table_clear
},
1458 {DM_TABLE_DEPS_CMD
, table_deps
},
1459 {DM_TABLE_STATUS_CMD
, table_status
},
1461 {DM_LIST_VERSIONS_CMD
, list_versions
},
1463 {DM_TARGET_MSG_CMD
, target_message
},
1464 {DM_DEV_SET_GEOMETRY_CMD
, dev_set_geometry
}
1467 return (cmd
>= ARRAY_SIZE(_ioctls
)) ? NULL
: _ioctls
[cmd
].fn
;
1471 * As well as checking the version compatibility this always
1472 * copies the kernel interface version out.
1474 static int check_version(unsigned int cmd
, struct dm_ioctl __user
*user
)
1476 uint32_t version
[3];
1479 if (copy_from_user(version
, user
->version
, sizeof(version
)))
1482 if ((DM_VERSION_MAJOR
!= version
[0]) ||
1483 (DM_VERSION_MINOR
< version
[1])) {
1484 DMWARN("ioctl interface mismatch: "
1485 "kernel(%u.%u.%u), user(%u.%u.%u), cmd(%d)",
1486 DM_VERSION_MAJOR
, DM_VERSION_MINOR
,
1487 DM_VERSION_PATCHLEVEL
,
1488 version
[0], version
[1], version
[2], cmd
);
1493 * Fill in the kernel version.
1495 version
[0] = DM_VERSION_MAJOR
;
1496 version
[1] = DM_VERSION_MINOR
;
1497 version
[2] = DM_VERSION_PATCHLEVEL
;
1498 if (copy_to_user(user
->version
, version
, sizeof(version
)))
1504 static int copy_params(struct dm_ioctl __user
*user
, struct dm_ioctl
**param
)
1506 struct dm_ioctl tmp
, *dmi
;
1509 if (copy_from_user(&tmp
, user
, sizeof(tmp
) - sizeof(tmp
.data
)))
1512 if (tmp
.data_size
< (sizeof(tmp
) - sizeof(tmp
.data
)))
1515 secure_data
= tmp
.flags
& DM_SECURE_DATA_FLAG
;
1517 dmi
= vmalloc(tmp
.data_size
);
1519 if (secure_data
&& clear_user(user
, tmp
.data_size
))
1524 if (copy_from_user(dmi
, user
, tmp
.data_size
))
1527 /* Wipe the user buffer so we do not return it to userspace */
1528 if (secure_data
&& clear_user(user
, tmp
.data_size
))
1536 memset(dmi
, 0, tmp
.data_size
);
1541 static int validate_params(uint cmd
, struct dm_ioctl
*param
)
1543 /* Always clear this flag */
1544 param
->flags
&= ~DM_BUFFER_FULL_FLAG
;
1545 param
->flags
&= ~DM_UEVENT_GENERATED_FLAG
;
1546 param
->flags
&= ~DM_SECURE_DATA_FLAG
;
1548 /* Ignores parameters */
1549 if (cmd
== DM_REMOVE_ALL_CMD
||
1550 cmd
== DM_LIST_DEVICES_CMD
||
1551 cmd
== DM_LIST_VERSIONS_CMD
)
1554 if ((cmd
== DM_DEV_CREATE_CMD
)) {
1555 if (!*param
->name
) {
1556 DMWARN("name not supplied when creating device");
1559 } else if ((*param
->uuid
&& *param
->name
)) {
1560 DMWARN("only supply one of name or uuid, cmd(%u)", cmd
);
1564 /* Ensure strings are terminated */
1565 param
->name
[DM_NAME_LEN
- 1] = '\0';
1566 param
->uuid
[DM_UUID_LEN
- 1] = '\0';
1571 static int ctl_ioctl(uint command
, struct dm_ioctl __user
*user
)
1576 struct dm_ioctl
*uninitialized_var(param
);
1578 size_t input_param_size
;
1580 /* only root can play with this */
1581 if (!capable(CAP_SYS_ADMIN
))
1584 if (_IOC_TYPE(command
) != DM_IOCTL
)
1587 cmd
= _IOC_NR(command
);
1590 * Check the interface version passed in. This also
1591 * writes out the kernel's interface version.
1593 r
= check_version(cmd
, user
);
1598 * Nothing more to do for the version command.
1600 if (cmd
== DM_VERSION_CMD
)
1603 fn
= lookup_ioctl(cmd
);
1605 DMWARN("dm_ctl_ioctl: unknown command 0x%x", command
);
1610 * Trying to avoid low memory issues when a device is
1613 current
->flags
|= PF_MEMALLOC
;
1616 * Copy the parameters into kernel space.
1618 r
= copy_params(user
, ¶m
);
1620 current
->flags
&= ~PF_MEMALLOC
;
1625 input_param_size
= param
->data_size
;
1626 wipe_buffer
= param
->flags
& DM_SECURE_DATA_FLAG
;
1628 r
= validate_params(cmd
, param
);
1632 param
->data_size
= sizeof(*param
);
1633 r
= fn(param
, input_param_size
);
1636 * Copy the results back to userland.
1638 if (!r
&& copy_to_user(user
, param
, param
->data_size
))
1643 memset(param
, 0, input_param_size
);
1649 static long dm_ctl_ioctl(struct file
*file
, uint command
, ulong u
)
1651 return (long)ctl_ioctl(command
, (struct dm_ioctl __user
*)u
);
1654 #ifdef CONFIG_COMPAT
1655 static long dm_compat_ctl_ioctl(struct file
*file
, uint command
, ulong u
)
1657 return (long)dm_ctl_ioctl(file
, command
, (ulong
) compat_ptr(u
));
1660 #define dm_compat_ctl_ioctl NULL
1663 static const struct file_operations _ctl_fops
= {
1664 .open
= nonseekable_open
,
1665 .unlocked_ioctl
= dm_ctl_ioctl
,
1666 .compat_ioctl
= dm_compat_ctl_ioctl
,
1667 .owner
= THIS_MODULE
,
1668 .llseek
= noop_llseek
,
1671 static struct miscdevice _dm_misc
= {
1672 .minor
= MAPPER_CTRL_MINOR
,
1674 .nodename
= DM_DIR
"/" DM_CONTROL_NODE
,
1678 MODULE_ALIAS_MISCDEV(MAPPER_CTRL_MINOR
);
1679 MODULE_ALIAS("devname:" DM_DIR
"/" DM_CONTROL_NODE
);
1682 * Create misc character device and link to DM_DIR/control.
1684 int __init
dm_interface_init(void)
1692 r
= misc_register(&_dm_misc
);
1694 DMERR("misc_register failed for control device");
1699 DMINFO("%d.%d.%d%s initialised: %s", DM_VERSION_MAJOR
,
1700 DM_VERSION_MINOR
, DM_VERSION_PATCHLEVEL
, DM_VERSION_EXTRA
,
1705 void dm_interface_exit(void)
1707 if (misc_deregister(&_dm_misc
) < 0)
1708 DMERR("misc_deregister failed for control device");
1714 * dm_copy_name_and_uuid - Copy mapped device name & uuid into supplied buffers
1715 * @md: Pointer to mapped_device
1716 * @name: Buffer (size DM_NAME_LEN) for name
1717 * @uuid: Buffer (size DM_UUID_LEN) for uuid or empty string if uuid not defined
1719 int dm_copy_name_and_uuid(struct mapped_device
*md
, char *name
, char *uuid
)
1722 struct hash_cell
*hc
;
1727 mutex_lock(&dm_hash_cells_mutex
);
1728 hc
= dm_get_mdptr(md
);
1729 if (!hc
|| hc
->md
!= md
) {
1735 strcpy(name
, hc
->name
);
1737 strcpy(uuid
, hc
->uuid
? : "");
1740 mutex_unlock(&dm_hash_cells_mutex
);