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/sched/mm.h>
14 #include <linux/init.h>
15 #include <linux/wait.h>
16 #include <linux/slab.h>
17 #include <linux/dm-ioctl.h>
18 #include <linux/hdreg.h>
19 #include <linux/compat.h>
21 #include <linux/uaccess.h>
23 #define DM_MSG_PREFIX "ioctl"
24 #define DM_DRIVER_EMAIL "dm-devel@redhat.com"
26 /*-----------------------------------------------------------------
27 * The ioctl interface needs to be able to look up devices by
29 *---------------------------------------------------------------*/
31 struct list_head name_list
;
32 struct list_head uuid_list
;
36 struct mapped_device
*md
;
37 struct dm_table
*new_map
;
42 struct dm_target_versions
*vers
, *old_vers
;
48 #define NUM_BUCKETS 64
49 #define MASK_BUCKETS (NUM_BUCKETS - 1)
50 static struct list_head _name_buckets
[NUM_BUCKETS
];
51 static struct list_head _uuid_buckets
[NUM_BUCKETS
];
53 static void dm_hash_remove_all(bool keep_open_devices
, bool mark_deferred
, bool only_deferred
);
56 * Guards access to both hash tables.
58 static DECLARE_RWSEM(_hash_lock
);
61 * Protects use of mdptr to obtain hash cell name and uuid from mapped device.
63 static DEFINE_MUTEX(dm_hash_cells_mutex
);
65 static void init_buckets(struct list_head
*buckets
)
69 for (i
= 0; i
< NUM_BUCKETS
; i
++)
70 INIT_LIST_HEAD(buckets
+ i
);
73 static int dm_hash_init(void)
75 init_buckets(_name_buckets
);
76 init_buckets(_uuid_buckets
);
80 static void dm_hash_exit(void)
82 dm_hash_remove_all(false, false, false);
85 /*-----------------------------------------------------------------
87 * We're not really concerned with the str hash function being
88 * fast since it's only used by the ioctl interface.
89 *---------------------------------------------------------------*/
90 static unsigned int hash_str(const char *str
)
92 const unsigned int hash_mult
= 2654435387U;
96 h
= (h
+ (unsigned int) *str
++) * hash_mult
;
98 return h
& MASK_BUCKETS
;
101 /*-----------------------------------------------------------------
102 * Code for looking up a device by name
103 *---------------------------------------------------------------*/
104 static struct hash_cell
*__get_name_cell(const char *str
)
106 struct hash_cell
*hc
;
107 unsigned int h
= hash_str(str
);
109 list_for_each_entry (hc
, _name_buckets
+ h
, name_list
)
110 if (!strcmp(hc
->name
, str
)) {
118 static struct hash_cell
*__get_uuid_cell(const char *str
)
120 struct hash_cell
*hc
;
121 unsigned int h
= hash_str(str
);
123 list_for_each_entry (hc
, _uuid_buckets
+ h
, uuid_list
)
124 if (!strcmp(hc
->uuid
, str
)) {
132 static struct hash_cell
*__get_dev_cell(uint64_t dev
)
134 struct mapped_device
*md
;
135 struct hash_cell
*hc
;
137 md
= dm_get_md(huge_decode_dev(dev
));
141 hc
= dm_get_mdptr(md
);
150 /*-----------------------------------------------------------------
151 * Inserting, removing and renaming a device.
152 *---------------------------------------------------------------*/
153 static struct hash_cell
*alloc_cell(const char *name
, const char *uuid
,
154 struct mapped_device
*md
)
156 struct hash_cell
*hc
;
158 hc
= kmalloc(sizeof(*hc
), GFP_KERNEL
);
162 hc
->name
= kstrdup(name
, GFP_KERNEL
);
172 hc
->uuid
= kstrdup(uuid
, GFP_KERNEL
);
180 INIT_LIST_HEAD(&hc
->name_list
);
181 INIT_LIST_HEAD(&hc
->uuid_list
);
187 static void free_cell(struct hash_cell
*hc
)
197 * The kdev_t and uuid of a device can never change once it is
198 * initially inserted.
200 static int dm_hash_insert(const char *name
, const char *uuid
, struct mapped_device
*md
)
202 struct hash_cell
*cell
, *hc
;
205 * Allocate the new cells.
207 cell
= alloc_cell(name
, uuid
, md
);
212 * Insert the cell into both hash tables.
214 down_write(&_hash_lock
);
215 hc
= __get_name_cell(name
);
221 list_add(&cell
->name_list
, _name_buckets
+ hash_str(name
));
224 hc
= __get_uuid_cell(uuid
);
226 list_del(&cell
->name_list
);
230 list_add(&cell
->uuid_list
, _uuid_buckets
+ hash_str(uuid
));
233 mutex_lock(&dm_hash_cells_mutex
);
234 dm_set_mdptr(md
, cell
);
235 mutex_unlock(&dm_hash_cells_mutex
);
236 up_write(&_hash_lock
);
241 up_write(&_hash_lock
);
246 static struct dm_table
*__hash_remove(struct hash_cell
*hc
)
248 struct dm_table
*table
;
251 /* remove from the dev hash */
252 list_del(&hc
->uuid_list
);
253 list_del(&hc
->name_list
);
254 mutex_lock(&dm_hash_cells_mutex
);
255 dm_set_mdptr(hc
->md
, NULL
);
256 mutex_unlock(&dm_hash_cells_mutex
);
258 table
= dm_get_live_table(hc
->md
, &srcu_idx
);
260 dm_table_event(table
);
261 dm_put_live_table(hc
->md
, srcu_idx
);
272 static void dm_hash_remove_all(bool keep_open_devices
, bool mark_deferred
, bool only_deferred
)
275 struct hash_cell
*hc
;
276 struct mapped_device
*md
;
282 down_write(&_hash_lock
);
284 for (i
= 0; i
< NUM_BUCKETS
; i
++) {
285 list_for_each_entry(hc
, _name_buckets
+ i
, name_list
) {
289 if (keep_open_devices
&&
290 dm_lock_for_deletion(md
, mark_deferred
, only_deferred
)) {
296 t
= __hash_remove(hc
);
298 up_write(&_hash_lock
);
305 if (likely(keep_open_devices
))
308 dm_destroy_immediate(md
);
311 * Some mapped devices may be using other mapped
312 * devices, so repeat until we make no further
313 * progress. If a new mapped device is created
314 * here it will also get removed.
320 up_write(&_hash_lock
);
323 DMWARN("remove_all left %d open device(s)", dev_skipped
);
327 * Set the uuid of a hash_cell that isn't already set.
329 static void __set_cell_uuid(struct hash_cell
*hc
, char *new_uuid
)
331 mutex_lock(&dm_hash_cells_mutex
);
333 mutex_unlock(&dm_hash_cells_mutex
);
335 list_add(&hc
->uuid_list
, _uuid_buckets
+ hash_str(new_uuid
));
339 * Changes the name of a hash_cell and returns the old name for
340 * the caller to free.
342 static char *__change_cell_name(struct hash_cell
*hc
, char *new_name
)
347 * Rename and move the name cell.
349 list_del(&hc
->name_list
);
352 mutex_lock(&dm_hash_cells_mutex
);
354 mutex_unlock(&dm_hash_cells_mutex
);
356 list_add(&hc
->name_list
, _name_buckets
+ hash_str(new_name
));
361 static struct mapped_device
*dm_hash_rename(struct dm_ioctl
*param
,
364 char *new_data
, *old_name
= NULL
;
365 struct hash_cell
*hc
;
366 struct dm_table
*table
;
367 struct mapped_device
*md
;
368 unsigned change_uuid
= (param
->flags
& DM_UUID_FLAG
) ? 1 : 0;
374 new_data
= kstrdup(new, GFP_KERNEL
);
376 return ERR_PTR(-ENOMEM
);
378 down_write(&_hash_lock
);
384 hc
= __get_uuid_cell(new);
386 hc
= __get_name_cell(new);
389 DMWARN("Unable to change %s on mapped device %s to one that "
390 "already exists: %s",
391 change_uuid
? "uuid" : "name",
394 up_write(&_hash_lock
);
396 return ERR_PTR(-EBUSY
);
400 * Is there such a device as 'old' ?
402 hc
= __get_name_cell(param
->name
);
404 DMWARN("Unable to rename non-existent device, %s to %s%s",
405 param
->name
, change_uuid
? "uuid " : "", new);
406 up_write(&_hash_lock
);
408 return ERR_PTR(-ENXIO
);
412 * Does this device already have a uuid?
414 if (change_uuid
&& hc
->uuid
) {
415 DMWARN("Unable to change uuid of mapped device %s to %s "
416 "because uuid is already set to %s",
417 param
->name
, new, hc
->uuid
);
419 up_write(&_hash_lock
);
421 return ERR_PTR(-EINVAL
);
425 __set_cell_uuid(hc
, new_data
);
427 old_name
= __change_cell_name(hc
, new_data
);
430 * Wake up any dm event waiters.
432 table
= dm_get_live_table(hc
->md
, &srcu_idx
);
434 dm_table_event(table
);
435 dm_put_live_table(hc
->md
, srcu_idx
);
437 if (!dm_kobject_uevent(hc
->md
, KOBJ_CHANGE
, param
->event_nr
))
438 param
->flags
|= DM_UEVENT_GENERATED_FLAG
;
441 up_write(&_hash_lock
);
447 void dm_deferred_remove(void)
449 dm_hash_remove_all(true, false, true);
452 /*-----------------------------------------------------------------
453 * Implementation of the ioctl commands
454 *---------------------------------------------------------------*/
456 * All the ioctl commands get dispatched to functions with this
459 typedef int (*ioctl_fn
)(struct dm_ioctl
*param
, size_t param_size
);
461 static int remove_all(struct dm_ioctl
*param
, size_t param_size
)
463 dm_hash_remove_all(true, !!(param
->flags
& DM_DEFERRED_REMOVE
), false);
464 param
->data_size
= 0;
469 * Round up the ptr to an 8-byte boundary.
472 static inline void *align_ptr(void *ptr
)
474 return (void *) (((size_t) (ptr
+ ALIGN_MASK
)) & ~ALIGN_MASK
);
478 * Retrieves the data payload buffer from an already allocated
481 static void *get_result_buffer(struct dm_ioctl
*param
, size_t param_size
,
484 param
->data_start
= align_ptr(param
+ 1) - (void *) param
;
486 if (param
->data_start
< param_size
)
487 *len
= param_size
- param
->data_start
;
491 return ((void *) param
) + param
->data_start
;
494 static int list_devices(struct dm_ioctl
*param
, size_t param_size
)
497 struct hash_cell
*hc
;
498 size_t len
, needed
= 0;
499 struct gendisk
*disk
;
500 struct dm_name_list
*nl
, *old_nl
= NULL
;
502 down_write(&_hash_lock
);
505 * Loop through all the devices working out how much
508 for (i
= 0; i
< NUM_BUCKETS
; i
++) {
509 list_for_each_entry (hc
, _name_buckets
+ i
, name_list
) {
510 needed
+= sizeof(struct dm_name_list
);
511 needed
+= strlen(hc
->name
) + 1;
512 needed
+= ALIGN_MASK
;
517 * Grab our output buffer.
519 nl
= get_result_buffer(param
, param_size
, &len
);
521 param
->flags
|= DM_BUFFER_FULL_FLAG
;
524 param
->data_size
= param
->data_start
+ needed
;
526 nl
->dev
= 0; /* Flags no data */
529 * Now loop through filling out the names.
531 for (i
= 0; i
< NUM_BUCKETS
; i
++) {
532 list_for_each_entry (hc
, _name_buckets
+ i
, name_list
) {
534 old_nl
->next
= (uint32_t) ((void *) nl
-
536 disk
= dm_disk(hc
->md
);
537 nl
->dev
= huge_encode_dev(disk_devt(disk
));
539 strcpy(nl
->name
, hc
->name
);
542 nl
= align_ptr(((void *) ++nl
) + strlen(hc
->name
) + 1);
547 up_write(&_hash_lock
);
551 static void list_version_get_needed(struct target_type
*tt
, void *needed_param
)
553 size_t *needed
= needed_param
;
555 *needed
+= sizeof(struct dm_target_versions
);
556 *needed
+= strlen(tt
->name
);
557 *needed
+= ALIGN_MASK
;
560 static void list_version_get_info(struct target_type
*tt
, void *param
)
562 struct vers_iter
*info
= param
;
564 /* Check space - it might have changed since the first iteration */
565 if ((char *)info
->vers
+ sizeof(tt
->version
) + strlen(tt
->name
) + 1 >
568 info
->flags
= DM_BUFFER_FULL_FLAG
;
573 info
->old_vers
->next
= (uint32_t) ((void *)info
->vers
-
574 (void *)info
->old_vers
);
575 info
->vers
->version
[0] = tt
->version
[0];
576 info
->vers
->version
[1] = tt
->version
[1];
577 info
->vers
->version
[2] = tt
->version
[2];
578 info
->vers
->next
= 0;
579 strcpy(info
->vers
->name
, tt
->name
);
581 info
->old_vers
= info
->vers
;
582 info
->vers
= align_ptr(((void *) ++info
->vers
) + strlen(tt
->name
) + 1);
585 static int list_versions(struct dm_ioctl
*param
, size_t param_size
)
587 size_t len
, needed
= 0;
588 struct dm_target_versions
*vers
;
589 struct vers_iter iter_info
;
592 * Loop through all the devices working out how much
595 dm_target_iterate(list_version_get_needed
, &needed
);
598 * Grab our output buffer.
600 vers
= get_result_buffer(param
, param_size
, &len
);
602 param
->flags
|= DM_BUFFER_FULL_FLAG
;
605 param
->data_size
= param
->data_start
+ needed
;
607 iter_info
.param_size
= param_size
;
608 iter_info
.old_vers
= NULL
;
609 iter_info
.vers
= vers
;
611 iter_info
.end
= (char *)vers
+len
;
614 * Now loop through filling out the names & versions.
616 dm_target_iterate(list_version_get_info
, &iter_info
);
617 param
->flags
|= iter_info
.flags
;
623 static int check_name(const char *name
)
625 if (strchr(name
, '/')) {
626 DMWARN("invalid device name");
634 * On successful return, the caller must not attempt to acquire
635 * _hash_lock without first calling dm_put_live_table, because dm_table_destroy
636 * waits for this dm_put_live_table and could be called under this lock.
638 static struct dm_table
*dm_get_inactive_table(struct mapped_device
*md
, int *srcu_idx
)
640 struct hash_cell
*hc
;
641 struct dm_table
*table
= NULL
;
643 /* increment rcu count, we don't care about the table pointer */
644 dm_get_live_table(md
, srcu_idx
);
646 down_read(&_hash_lock
);
647 hc
= dm_get_mdptr(md
);
648 if (!hc
|| hc
->md
!= md
) {
649 DMWARN("device has been removed from the dev hash table.");
656 up_read(&_hash_lock
);
661 static struct dm_table
*dm_get_live_or_inactive_table(struct mapped_device
*md
,
662 struct dm_ioctl
*param
,
665 return (param
->flags
& DM_QUERY_INACTIVE_TABLE_FLAG
) ?
666 dm_get_inactive_table(md
, srcu_idx
) : dm_get_live_table(md
, srcu_idx
);
670 * Fills in a dm_ioctl structure, ready for sending back to
673 static void __dev_status(struct mapped_device
*md
, struct dm_ioctl
*param
)
675 struct gendisk
*disk
= dm_disk(md
);
676 struct dm_table
*table
;
679 param
->flags
&= ~(DM_SUSPEND_FLAG
| DM_READONLY_FLAG
|
680 DM_ACTIVE_PRESENT_FLAG
| DM_INTERNAL_SUSPEND_FLAG
);
682 if (dm_suspended_md(md
))
683 param
->flags
|= DM_SUSPEND_FLAG
;
685 if (dm_suspended_internally_md(md
))
686 param
->flags
|= DM_INTERNAL_SUSPEND_FLAG
;
688 if (dm_test_deferred_remove_flag(md
))
689 param
->flags
|= DM_DEFERRED_REMOVE
;
691 param
->dev
= huge_encode_dev(disk_devt(disk
));
694 * Yes, this will be out of date by the time it gets back
695 * to userland, but it is still very useful for
698 param
->open_count
= dm_open_count(md
);
700 param
->event_nr
= dm_get_event_nr(md
);
701 param
->target_count
= 0;
703 table
= dm_get_live_table(md
, &srcu_idx
);
705 if (!(param
->flags
& DM_QUERY_INACTIVE_TABLE_FLAG
)) {
706 if (get_disk_ro(disk
))
707 param
->flags
|= DM_READONLY_FLAG
;
708 param
->target_count
= dm_table_get_num_targets(table
);
711 param
->flags
|= DM_ACTIVE_PRESENT_FLAG
;
713 dm_put_live_table(md
, srcu_idx
);
715 if (param
->flags
& DM_QUERY_INACTIVE_TABLE_FLAG
) {
717 table
= dm_get_inactive_table(md
, &srcu_idx
);
719 if (!(dm_table_get_mode(table
) & FMODE_WRITE
))
720 param
->flags
|= DM_READONLY_FLAG
;
721 param
->target_count
= dm_table_get_num_targets(table
);
723 dm_put_live_table(md
, srcu_idx
);
727 static int dev_create(struct dm_ioctl
*param
, size_t param_size
)
729 int r
, m
= DM_ANY_MINOR
;
730 struct mapped_device
*md
;
732 r
= check_name(param
->name
);
736 if (param
->flags
& DM_PERSISTENT_DEV_FLAG
)
737 m
= MINOR(huge_decode_dev(param
->dev
));
739 r
= dm_create(m
, &md
);
743 r
= dm_hash_insert(param
->name
, *param
->uuid
? param
->uuid
: NULL
, md
);
750 param
->flags
&= ~DM_INACTIVE_PRESENT_FLAG
;
752 __dev_status(md
, param
);
760 * Always use UUID for lookups if it's present, otherwise use name or dev.
762 static struct hash_cell
*__find_device_hash_cell(struct dm_ioctl
*param
)
764 struct hash_cell
*hc
= NULL
;
767 if (*param
->name
|| param
->dev
)
770 hc
= __get_uuid_cell(param
->uuid
);
773 } else if (*param
->name
) {
777 hc
= __get_name_cell(param
->name
);
780 } else if (param
->dev
) {
781 hc
= __get_dev_cell(param
->dev
);
788 * Sneakily write in both the name and the uuid
789 * while we have the cell.
791 strlcpy(param
->name
, hc
->name
, sizeof(param
->name
));
793 strlcpy(param
->uuid
, hc
->uuid
, sizeof(param
->uuid
));
795 param
->uuid
[0] = '\0';
798 param
->flags
|= DM_INACTIVE_PRESENT_FLAG
;
800 param
->flags
&= ~DM_INACTIVE_PRESENT_FLAG
;
805 static struct mapped_device
*find_device(struct dm_ioctl
*param
)
807 struct hash_cell
*hc
;
808 struct mapped_device
*md
= NULL
;
810 down_read(&_hash_lock
);
811 hc
= __find_device_hash_cell(param
);
814 up_read(&_hash_lock
);
819 static int dev_remove(struct dm_ioctl
*param
, size_t param_size
)
821 struct hash_cell
*hc
;
822 struct mapped_device
*md
;
826 down_write(&_hash_lock
);
827 hc
= __find_device_hash_cell(param
);
830 DMDEBUG_LIMIT("device doesn't appear to be in the dev hash table.");
831 up_write(&_hash_lock
);
838 * Ensure the device is not open and nothing further can open it.
840 r
= dm_lock_for_deletion(md
, !!(param
->flags
& DM_DEFERRED_REMOVE
), false);
842 if (r
== -EBUSY
&& param
->flags
& DM_DEFERRED_REMOVE
) {
843 up_write(&_hash_lock
);
847 DMDEBUG_LIMIT("unable to remove open device %s", hc
->name
);
848 up_write(&_hash_lock
);
853 t
= __hash_remove(hc
);
854 up_write(&_hash_lock
);
861 param
->flags
&= ~DM_DEFERRED_REMOVE
;
863 if (!dm_kobject_uevent(md
, KOBJ_REMOVE
, param
->event_nr
))
864 param
->flags
|= DM_UEVENT_GENERATED_FLAG
;
872 * Check a string doesn't overrun the chunk of
873 * memory we copied from userland.
875 static int invalid_str(char *str
, void *end
)
877 while ((void *) str
< end
)
884 static int dev_rename(struct dm_ioctl
*param
, size_t param_size
)
887 char *new_data
= (char *) param
+ param
->data_start
;
888 struct mapped_device
*md
;
889 unsigned change_uuid
= (param
->flags
& DM_UUID_FLAG
) ? 1 : 0;
891 if (new_data
< param
->data
||
892 invalid_str(new_data
, (void *) param
+ param_size
) || !*new_data
||
893 strlen(new_data
) > (change_uuid
? DM_UUID_LEN
- 1 : DM_NAME_LEN
- 1)) {
894 DMWARN("Invalid new mapped device name or uuid string supplied.");
899 r
= check_name(new_data
);
904 md
= dm_hash_rename(param
, new_data
);
908 __dev_status(md
, param
);
914 static int dev_set_geometry(struct dm_ioctl
*param
, size_t param_size
)
917 struct mapped_device
*md
;
918 struct hd_geometry geometry
;
919 unsigned long indata
[4];
920 char *geostr
= (char *) param
+ param
->data_start
;
923 md
= find_device(param
);
927 if (geostr
< param
->data
||
928 invalid_str(geostr
, (void *) param
+ param_size
)) {
929 DMWARN("Invalid geometry supplied.");
933 x
= sscanf(geostr
, "%lu %lu %lu %lu%c", indata
,
934 indata
+ 1, indata
+ 2, indata
+ 3, &dummy
);
937 DMWARN("Unable to interpret geometry settings.");
941 if (indata
[0] > 65535 || indata
[1] > 255 ||
942 indata
[2] > 255 || indata
[3] > ULONG_MAX
) {
943 DMWARN("Geometry exceeds range limits.");
947 geometry
.cylinders
= indata
[0];
948 geometry
.heads
= indata
[1];
949 geometry
.sectors
= indata
[2];
950 geometry
.start
= indata
[3];
952 r
= dm_set_geometry(md
, &geometry
);
954 param
->data_size
= 0;
961 static int do_suspend(struct dm_ioctl
*param
)
964 unsigned suspend_flags
= DM_SUSPEND_LOCKFS_FLAG
;
965 struct mapped_device
*md
;
967 md
= find_device(param
);
971 if (param
->flags
& DM_SKIP_LOCKFS_FLAG
)
972 suspend_flags
&= ~DM_SUSPEND_LOCKFS_FLAG
;
973 if (param
->flags
& DM_NOFLUSH_FLAG
)
974 suspend_flags
|= DM_SUSPEND_NOFLUSH_FLAG
;
976 if (!dm_suspended_md(md
)) {
977 r
= dm_suspend(md
, suspend_flags
);
982 __dev_status(md
, param
);
990 static int do_resume(struct dm_ioctl
*param
)
993 unsigned suspend_flags
= DM_SUSPEND_LOCKFS_FLAG
;
994 struct hash_cell
*hc
;
995 struct mapped_device
*md
;
996 struct dm_table
*new_map
, *old_map
= NULL
;
998 down_write(&_hash_lock
);
1000 hc
= __find_device_hash_cell(param
);
1002 DMDEBUG_LIMIT("device doesn't appear to be in the dev hash table.");
1003 up_write(&_hash_lock
);
1009 new_map
= hc
->new_map
;
1011 param
->flags
&= ~DM_INACTIVE_PRESENT_FLAG
;
1013 up_write(&_hash_lock
);
1015 /* Do we need to load a new map ? */
1017 /* Suspend if it isn't already suspended */
1018 if (param
->flags
& DM_SKIP_LOCKFS_FLAG
)
1019 suspend_flags
&= ~DM_SUSPEND_LOCKFS_FLAG
;
1020 if (param
->flags
& DM_NOFLUSH_FLAG
)
1021 suspend_flags
|= DM_SUSPEND_NOFLUSH_FLAG
;
1022 if (!dm_suspended_md(md
))
1023 dm_suspend(md
, suspend_flags
);
1025 old_map
= dm_swap_table(md
, new_map
);
1026 if (IS_ERR(old_map
)) {
1028 dm_table_destroy(new_map
);
1030 return PTR_ERR(old_map
);
1033 if (dm_table_get_mode(new_map
) & FMODE_WRITE
)
1034 set_disk_ro(dm_disk(md
), 0);
1036 set_disk_ro(dm_disk(md
), 1);
1039 if (dm_suspended_md(md
)) {
1041 if (!r
&& !dm_kobject_uevent(md
, KOBJ_CHANGE
, param
->event_nr
))
1042 param
->flags
|= DM_UEVENT_GENERATED_FLAG
;
1046 * Since dm_swap_table synchronizes RCU, nobody should be in
1047 * read-side critical section already.
1050 dm_table_destroy(old_map
);
1053 __dev_status(md
, param
);
1060 * Set or unset the suspension state of a device.
1061 * If the device already is in the requested state we just return its status.
1063 static int dev_suspend(struct dm_ioctl
*param
, size_t param_size
)
1065 if (param
->flags
& DM_SUSPEND_FLAG
)
1066 return do_suspend(param
);
1068 return do_resume(param
);
1072 * Copies device info back to user space, used by
1073 * the create and info ioctls.
1075 static int dev_status(struct dm_ioctl
*param
, size_t param_size
)
1077 struct mapped_device
*md
;
1079 md
= find_device(param
);
1083 __dev_status(md
, param
);
1090 * Build up the status struct for each target
1092 static void retrieve_status(struct dm_table
*table
,
1093 struct dm_ioctl
*param
, size_t param_size
)
1095 unsigned int i
, num_targets
;
1096 struct dm_target_spec
*spec
;
1097 char *outbuf
, *outptr
;
1099 size_t remaining
, len
, used
= 0;
1100 unsigned status_flags
= 0;
1102 outptr
= outbuf
= get_result_buffer(param
, param_size
, &len
);
1104 if (param
->flags
& DM_STATUS_TABLE_FLAG
)
1105 type
= STATUSTYPE_TABLE
;
1107 type
= STATUSTYPE_INFO
;
1109 /* Get all the target info */
1110 num_targets
= dm_table_get_num_targets(table
);
1111 for (i
= 0; i
< num_targets
; i
++) {
1112 struct dm_target
*ti
= dm_table_get_target(table
, i
);
1115 remaining
= len
- (outptr
- outbuf
);
1116 if (remaining
<= sizeof(struct dm_target_spec
)) {
1117 param
->flags
|= DM_BUFFER_FULL_FLAG
;
1121 spec
= (struct dm_target_spec
*) outptr
;
1124 spec
->sector_start
= ti
->begin
;
1125 spec
->length
= ti
->len
;
1126 strncpy(spec
->target_type
, ti
->type
->name
,
1127 sizeof(spec
->target_type
));
1129 outptr
+= sizeof(struct dm_target_spec
);
1130 remaining
= len
- (outptr
- outbuf
);
1131 if (remaining
<= 0) {
1132 param
->flags
|= DM_BUFFER_FULL_FLAG
;
1136 /* Get the status/table string from the target driver */
1137 if (ti
->type
->status
) {
1138 if (param
->flags
& DM_NOFLUSH_FLAG
)
1139 status_flags
|= DM_STATUS_NOFLUSH_FLAG
;
1140 ti
->type
->status(ti
, type
, status_flags
, outptr
, remaining
);
1144 l
= strlen(outptr
) + 1;
1145 if (l
== remaining
) {
1146 param
->flags
|= DM_BUFFER_FULL_FLAG
;
1151 used
= param
->data_start
+ (outptr
- outbuf
);
1153 outptr
= align_ptr(outptr
);
1154 spec
->next
= outptr
- outbuf
;
1158 param
->data_size
= used
;
1160 param
->target_count
= num_targets
;
1164 * Wait for a device to report an event
1166 static int dev_wait(struct dm_ioctl
*param
, size_t param_size
)
1169 struct mapped_device
*md
;
1170 struct dm_table
*table
;
1173 md
= find_device(param
);
1178 * Wait for a notification event
1180 if (dm_wait_event(md
, param
->event_nr
)) {
1186 * The userland program is going to want to know what
1187 * changed to trigger the event, so we may as well tell
1188 * him and save an ioctl.
1190 __dev_status(md
, param
);
1192 table
= dm_get_live_or_inactive_table(md
, param
, &srcu_idx
);
1194 retrieve_status(table
, param
, param_size
);
1195 dm_put_live_table(md
, srcu_idx
);
1203 static inline fmode_t
get_mode(struct dm_ioctl
*param
)
1205 fmode_t mode
= FMODE_READ
| FMODE_WRITE
;
1207 if (param
->flags
& DM_READONLY_FLAG
)
1213 static int next_target(struct dm_target_spec
*last
, uint32_t next
, void *end
,
1214 struct dm_target_spec
**spec
, char **target_params
)
1216 *spec
= (struct dm_target_spec
*) ((unsigned char *) last
+ next
);
1217 *target_params
= (char *) (*spec
+ 1);
1219 if (*spec
< (last
+ 1))
1222 return invalid_str(*target_params
, end
);
1225 static int populate_table(struct dm_table
*table
,
1226 struct dm_ioctl
*param
, size_t param_size
)
1230 struct dm_target_spec
*spec
= (struct dm_target_spec
*) param
;
1231 uint32_t next
= param
->data_start
;
1232 void *end
= (void *) param
+ param_size
;
1233 char *target_params
;
1235 if (!param
->target_count
) {
1236 DMWARN("populate_table: no targets specified");
1240 for (i
= 0; i
< param
->target_count
; i
++) {
1242 r
= next_target(spec
, next
, end
, &spec
, &target_params
);
1244 DMWARN("unable to find target");
1248 r
= dm_table_add_target(table
, spec
->target_type
,
1249 (sector_t
) spec
->sector_start
,
1250 (sector_t
) spec
->length
,
1253 DMWARN("error adding target to table");
1260 return dm_table_complete(table
);
1263 static bool is_valid_type(enum dm_queue_mode cur
, enum dm_queue_mode
new)
1266 (cur
== DM_TYPE_BIO_BASED
&& new == DM_TYPE_DAX_BIO_BASED
))
1272 static int table_load(struct dm_ioctl
*param
, size_t param_size
)
1275 struct hash_cell
*hc
;
1276 struct dm_table
*t
, *old_map
= NULL
;
1277 struct mapped_device
*md
;
1278 struct target_type
*immutable_target_type
;
1280 md
= find_device(param
);
1284 r
= dm_table_create(&t
, get_mode(param
), param
->target_count
, md
);
1288 /* Protect md->type and md->queue against concurrent table loads. */
1289 dm_lock_md_type(md
);
1290 r
= populate_table(t
, param
, param_size
);
1292 goto err_unlock_md_type
;
1294 immutable_target_type
= dm_get_immutable_target_type(md
);
1295 if (immutable_target_type
&&
1296 (immutable_target_type
!= dm_table_get_immutable_target_type(t
)) &&
1297 !dm_table_get_wildcard_target(t
)) {
1298 DMWARN("can't replace immutable target type %s",
1299 immutable_target_type
->name
);
1301 goto err_unlock_md_type
;
1304 if (dm_get_md_type(md
) == DM_TYPE_NONE
) {
1305 /* Initial table load: acquire type of table. */
1306 dm_set_md_type(md
, dm_table_get_type(t
));
1308 /* setup md->queue to reflect md's type (may block) */
1309 r
= dm_setup_md_queue(md
, t
);
1311 DMWARN("unable to set up device queue for new table.");
1312 goto err_unlock_md_type
;
1314 } else if (!is_valid_type(dm_get_md_type(md
), dm_table_get_type(t
))) {
1315 DMWARN("can't change device type after initial table load.");
1317 goto err_unlock_md_type
;
1320 dm_unlock_md_type(md
);
1322 /* stage inactive table */
1323 down_write(&_hash_lock
);
1324 hc
= dm_get_mdptr(md
);
1325 if (!hc
|| hc
->md
!= md
) {
1326 DMWARN("device has been removed from the dev hash table.");
1327 up_write(&_hash_lock
);
1329 goto err_destroy_table
;
1333 old_map
= hc
->new_map
;
1335 up_write(&_hash_lock
);
1337 param
->flags
|= DM_INACTIVE_PRESENT_FLAG
;
1338 __dev_status(md
, param
);
1342 dm_table_destroy(old_map
);
1350 dm_unlock_md_type(md
);
1352 dm_table_destroy(t
);
1359 static int table_clear(struct dm_ioctl
*param
, size_t param_size
)
1361 struct hash_cell
*hc
;
1362 struct mapped_device
*md
;
1363 struct dm_table
*old_map
= NULL
;
1365 down_write(&_hash_lock
);
1367 hc
= __find_device_hash_cell(param
);
1369 DMDEBUG_LIMIT("device doesn't appear to be in the dev hash table.");
1370 up_write(&_hash_lock
);
1375 old_map
= hc
->new_map
;
1379 param
->flags
&= ~DM_INACTIVE_PRESENT_FLAG
;
1381 __dev_status(hc
->md
, param
);
1383 up_write(&_hash_lock
);
1386 dm_table_destroy(old_map
);
1394 * Retrieves a list of devices used by a particular dm device.
1396 static void retrieve_deps(struct dm_table
*table
,
1397 struct dm_ioctl
*param
, size_t param_size
)
1399 unsigned int count
= 0;
1400 struct list_head
*tmp
;
1402 struct dm_dev_internal
*dd
;
1403 struct dm_target_deps
*deps
;
1405 deps
= get_result_buffer(param
, param_size
, &len
);
1408 * Count the devices.
1410 list_for_each (tmp
, dm_table_get_devices(table
))
1414 * Check we have enough space.
1416 needed
= sizeof(*deps
) + (sizeof(*deps
->dev
) * count
);
1418 param
->flags
|= DM_BUFFER_FULL_FLAG
;
1423 * Fill in the devices.
1425 deps
->count
= count
;
1427 list_for_each_entry (dd
, dm_table_get_devices(table
), list
)
1428 deps
->dev
[count
++] = huge_encode_dev(dd
->dm_dev
->bdev
->bd_dev
);
1430 param
->data_size
= param
->data_start
+ needed
;
1433 static int table_deps(struct dm_ioctl
*param
, size_t param_size
)
1435 struct mapped_device
*md
;
1436 struct dm_table
*table
;
1439 md
= find_device(param
);
1443 __dev_status(md
, param
);
1445 table
= dm_get_live_or_inactive_table(md
, param
, &srcu_idx
);
1447 retrieve_deps(table
, param
, param_size
);
1448 dm_put_live_table(md
, srcu_idx
);
1456 * Return the status of a device as a text string for each
1459 static int table_status(struct dm_ioctl
*param
, size_t param_size
)
1461 struct mapped_device
*md
;
1462 struct dm_table
*table
;
1465 md
= find_device(param
);
1469 __dev_status(md
, param
);
1471 table
= dm_get_live_or_inactive_table(md
, param
, &srcu_idx
);
1473 retrieve_status(table
, param
, param_size
);
1474 dm_put_live_table(md
, srcu_idx
);
1482 * Process device-mapper dependent messages. Messages prefixed with '@'
1483 * are processed by the DM core. All others are delivered to the target.
1484 * Returns a number <= 1 if message was processed by device mapper.
1485 * Returns 2 if message should be delivered to the target.
1487 static int message_for_md(struct mapped_device
*md
, unsigned argc
, char **argv
,
1488 char *result
, unsigned maxlen
)
1493 return 2; /* no '@' prefix, deliver to target */
1495 if (!strcasecmp(argv
[0], "@cancel_deferred_remove")) {
1497 DMERR("Invalid arguments for @cancel_deferred_remove");
1500 return dm_cancel_deferred_remove(md
);
1503 r
= dm_stats_message(md
, argc
, argv
, result
, maxlen
);
1507 DMERR("Unsupported message sent to DM core: %s", argv
[0]);
1512 * Pass a message to the target that's at the supplied device offset.
1514 static int target_message(struct dm_ioctl
*param
, size_t param_size
)
1518 struct mapped_device
*md
;
1519 struct dm_table
*table
;
1520 struct dm_target
*ti
;
1521 struct dm_target_msg
*tmsg
= (void *) param
+ param
->data_start
;
1523 char *result
= get_result_buffer(param
, param_size
, &maxlen
);
1526 md
= find_device(param
);
1530 if (tmsg
< (struct dm_target_msg
*) param
->data
||
1531 invalid_str(tmsg
->message
, (void *) param
+ param_size
)) {
1532 DMWARN("Invalid target message parameters.");
1537 r
= dm_split_args(&argc
, &argv
, tmsg
->message
);
1539 DMWARN("Failed to split target message parameters");
1544 DMWARN("Empty message received.");
1548 r
= message_for_md(md
, argc
, argv
, result
, maxlen
);
1552 table
= dm_get_live_table(md
, &srcu_idx
);
1556 if (dm_deleting_md(md
)) {
1561 ti
= dm_table_find_target(table
, tmsg
->sector
);
1562 if (!dm_target_is_valid(ti
)) {
1563 DMWARN("Target message sector outside device.");
1565 } else if (ti
->type
->message
)
1566 r
= ti
->type
->message(ti
, argc
, argv
);
1568 DMWARN("Target type does not support messages");
1573 dm_put_live_table(md
, srcu_idx
);
1578 __dev_status(md
, param
);
1581 param
->flags
|= DM_DATA_OUT_FLAG
;
1582 if (dm_message_test_buffer_overflow(result
, maxlen
))
1583 param
->flags
|= DM_BUFFER_FULL_FLAG
;
1585 param
->data_size
= param
->data_start
+ strlen(result
) + 1;
1594 * The ioctl parameter block consists of two parts, a dm_ioctl struct
1595 * followed by a data buffer. This flag is set if the second part,
1596 * which has a variable size, is not used by the function processing
1599 #define IOCTL_FLAGS_NO_PARAMS 1
1601 /*-----------------------------------------------------------------
1602 * Implementation of open/close/ioctl on the special char
1604 *---------------------------------------------------------------*/
1605 static ioctl_fn
lookup_ioctl(unsigned int cmd
, int *ioctl_flags
)
1612 {DM_VERSION_CMD
, 0, NULL
}, /* version is dealt with elsewhere */
1613 {DM_REMOVE_ALL_CMD
, IOCTL_FLAGS_NO_PARAMS
, remove_all
},
1614 {DM_LIST_DEVICES_CMD
, 0, list_devices
},
1616 {DM_DEV_CREATE_CMD
, IOCTL_FLAGS_NO_PARAMS
, dev_create
},
1617 {DM_DEV_REMOVE_CMD
, IOCTL_FLAGS_NO_PARAMS
, dev_remove
},
1618 {DM_DEV_RENAME_CMD
, 0, dev_rename
},
1619 {DM_DEV_SUSPEND_CMD
, IOCTL_FLAGS_NO_PARAMS
, dev_suspend
},
1620 {DM_DEV_STATUS_CMD
, IOCTL_FLAGS_NO_PARAMS
, dev_status
},
1621 {DM_DEV_WAIT_CMD
, 0, dev_wait
},
1623 {DM_TABLE_LOAD_CMD
, 0, table_load
},
1624 {DM_TABLE_CLEAR_CMD
, IOCTL_FLAGS_NO_PARAMS
, table_clear
},
1625 {DM_TABLE_DEPS_CMD
, 0, table_deps
},
1626 {DM_TABLE_STATUS_CMD
, 0, table_status
},
1628 {DM_LIST_VERSIONS_CMD
, 0, list_versions
},
1630 {DM_TARGET_MSG_CMD
, 0, target_message
},
1631 {DM_DEV_SET_GEOMETRY_CMD
, 0, dev_set_geometry
}
1634 if (unlikely(cmd
>= ARRAY_SIZE(_ioctls
)))
1637 *ioctl_flags
= _ioctls
[cmd
].flags
;
1638 return _ioctls
[cmd
].fn
;
1642 * As well as checking the version compatibility this always
1643 * copies the kernel interface version out.
1645 static int check_version(unsigned int cmd
, struct dm_ioctl __user
*user
)
1647 uint32_t version
[3];
1650 if (copy_from_user(version
, user
->version
, sizeof(version
)))
1653 if ((DM_VERSION_MAJOR
!= version
[0]) ||
1654 (DM_VERSION_MINOR
< version
[1])) {
1655 DMWARN("ioctl interface mismatch: "
1656 "kernel(%u.%u.%u), user(%u.%u.%u), cmd(%d)",
1657 DM_VERSION_MAJOR
, DM_VERSION_MINOR
,
1658 DM_VERSION_PATCHLEVEL
,
1659 version
[0], version
[1], version
[2], cmd
);
1664 * Fill in the kernel version.
1666 version
[0] = DM_VERSION_MAJOR
;
1667 version
[1] = DM_VERSION_MINOR
;
1668 version
[2] = DM_VERSION_PATCHLEVEL
;
1669 if (copy_to_user(user
->version
, version
, sizeof(version
)))
1675 #define DM_PARAMS_MALLOC 0x0001 /* Params allocated with kvmalloc() */
1676 #define DM_WIPE_BUFFER 0x0010 /* Wipe input buffer before returning from ioctl */
1678 static void free_params(struct dm_ioctl
*param
, size_t param_size
, int param_flags
)
1680 if (param_flags
& DM_WIPE_BUFFER
)
1681 memset(param
, 0, param_size
);
1683 if (param_flags
& DM_PARAMS_MALLOC
)
1687 static int copy_params(struct dm_ioctl __user
*user
, struct dm_ioctl
*param_kernel
,
1689 struct dm_ioctl
**param
, int *param_flags
)
1691 struct dm_ioctl
*dmi
;
1693 const size_t minimum_data_size
= offsetof(struct dm_ioctl
, data
);
1696 if (copy_from_user(param_kernel
, user
, minimum_data_size
))
1699 if (param_kernel
->data_size
< minimum_data_size
)
1702 secure_data
= param_kernel
->flags
& DM_SECURE_DATA_FLAG
;
1704 *param_flags
= secure_data
? DM_WIPE_BUFFER
: 0;
1706 if (ioctl_flags
& IOCTL_FLAGS_NO_PARAMS
) {
1708 dmi
->data_size
= minimum_data_size
;
1713 * Use __GFP_HIGH to avoid low memory issues when a device is
1714 * suspended and the ioctl is needed to resume it.
1715 * Use kmalloc() rather than vmalloc() when we can.
1718 noio_flag
= memalloc_noio_save();
1719 dmi
= kvmalloc(param_kernel
->data_size
, GFP_KERNEL
| __GFP_HIGH
);
1720 memalloc_noio_restore(noio_flag
);
1723 if (secure_data
&& clear_user(user
, param_kernel
->data_size
))
1728 *param_flags
|= DM_PARAMS_MALLOC
;
1730 if (copy_from_user(dmi
, user
, param_kernel
->data_size
))
1735 * Abort if something changed the ioctl data while it was being copied.
1737 if (dmi
->data_size
!= param_kernel
->data_size
) {
1738 DMERR("rejecting ioctl: data size modified while processing parameters");
1742 /* Wipe the user buffer so we do not return it to userspace */
1743 if (secure_data
&& clear_user(user
, param_kernel
->data_size
))
1750 free_params(dmi
, param_kernel
->data_size
, *param_flags
);
1755 static int validate_params(uint cmd
, struct dm_ioctl
*param
)
1757 /* Always clear this flag */
1758 param
->flags
&= ~DM_BUFFER_FULL_FLAG
;
1759 param
->flags
&= ~DM_UEVENT_GENERATED_FLAG
;
1760 param
->flags
&= ~DM_SECURE_DATA_FLAG
;
1761 param
->flags
&= ~DM_DATA_OUT_FLAG
;
1763 /* Ignores parameters */
1764 if (cmd
== DM_REMOVE_ALL_CMD
||
1765 cmd
== DM_LIST_DEVICES_CMD
||
1766 cmd
== DM_LIST_VERSIONS_CMD
)
1769 if (cmd
== DM_DEV_CREATE_CMD
) {
1770 if (!*param
->name
) {
1771 DMWARN("name not supplied when creating device");
1774 } else if (*param
->uuid
&& *param
->name
) {
1775 DMWARN("only supply one of name or uuid, cmd(%u)", cmd
);
1779 /* Ensure strings are terminated */
1780 param
->name
[DM_NAME_LEN
- 1] = '\0';
1781 param
->uuid
[DM_UUID_LEN
- 1] = '\0';
1786 static int ctl_ioctl(uint command
, struct dm_ioctl __user
*user
)
1792 struct dm_ioctl
*uninitialized_var(param
);
1794 size_t input_param_size
;
1795 struct dm_ioctl param_kernel
;
1797 /* only root can play with this */
1798 if (!capable(CAP_SYS_ADMIN
))
1801 if (_IOC_TYPE(command
) != DM_IOCTL
)
1804 cmd
= _IOC_NR(command
);
1807 * Check the interface version passed in. This also
1808 * writes out the kernel's interface version.
1810 r
= check_version(cmd
, user
);
1815 * Nothing more to do for the version command.
1817 if (cmd
== DM_VERSION_CMD
)
1820 fn
= lookup_ioctl(cmd
, &ioctl_flags
);
1822 DMWARN("dm_ctl_ioctl: unknown command 0x%x", command
);
1827 * Copy the parameters into kernel space.
1829 r
= copy_params(user
, ¶m_kernel
, ioctl_flags
, ¶m
, ¶m_flags
);
1834 input_param_size
= param
->data_size
;
1835 r
= validate_params(cmd
, param
);
1839 param
->data_size
= offsetof(struct dm_ioctl
, data
);
1840 r
= fn(param
, input_param_size
);
1842 if (unlikely(param
->flags
& DM_BUFFER_FULL_FLAG
) &&
1843 unlikely(ioctl_flags
& IOCTL_FLAGS_NO_PARAMS
))
1844 DMERR("ioctl %d tried to output some data but has IOCTL_FLAGS_NO_PARAMS set", cmd
);
1847 * Copy the results back to userland.
1849 if (!r
&& copy_to_user(user
, param
, param
->data_size
))
1853 free_params(param
, input_param_size
, param_flags
);
1857 static long dm_ctl_ioctl(struct file
*file
, uint command
, ulong u
)
1859 return (long)ctl_ioctl(command
, (struct dm_ioctl __user
*)u
);
1862 #ifdef CONFIG_COMPAT
1863 static long dm_compat_ctl_ioctl(struct file
*file
, uint command
, ulong u
)
1865 return (long)dm_ctl_ioctl(file
, command
, (ulong
) compat_ptr(u
));
1868 #define dm_compat_ctl_ioctl NULL
1871 static const struct file_operations _ctl_fops
= {
1872 .open
= nonseekable_open
,
1873 .unlocked_ioctl
= dm_ctl_ioctl
,
1874 .compat_ioctl
= dm_compat_ctl_ioctl
,
1875 .owner
= THIS_MODULE
,
1876 .llseek
= noop_llseek
,
1879 static struct miscdevice _dm_misc
= {
1880 .minor
= MAPPER_CTRL_MINOR
,
1882 .nodename
= DM_DIR
"/" DM_CONTROL_NODE
,
1886 MODULE_ALIAS_MISCDEV(MAPPER_CTRL_MINOR
);
1887 MODULE_ALIAS("devname:" DM_DIR
"/" DM_CONTROL_NODE
);
1890 * Create misc character device and link to DM_DIR/control.
1892 int __init
dm_interface_init(void)
1900 r
= misc_register(&_dm_misc
);
1902 DMERR("misc_register failed for control device");
1907 DMINFO("%d.%d.%d%s initialised: %s", DM_VERSION_MAJOR
,
1908 DM_VERSION_MINOR
, DM_VERSION_PATCHLEVEL
, DM_VERSION_EXTRA
,
1913 void dm_interface_exit(void)
1915 misc_deregister(&_dm_misc
);
1920 * dm_copy_name_and_uuid - Copy mapped device name & uuid into supplied buffers
1921 * @md: Pointer to mapped_device
1922 * @name: Buffer (size DM_NAME_LEN) for name
1923 * @uuid: Buffer (size DM_UUID_LEN) for uuid or empty string if uuid not defined
1925 int dm_copy_name_and_uuid(struct mapped_device
*md
, char *name
, char *uuid
)
1928 struct hash_cell
*hc
;
1933 mutex_lock(&dm_hash_cells_mutex
);
1934 hc
= dm_get_mdptr(md
);
1935 if (!hc
|| hc
->md
!= md
) {
1941 strcpy(name
, hc
->name
);
1943 strcpy(uuid
, hc
->uuid
? : "");
1946 mutex_unlock(&dm_hash_cells_mutex
);