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
);
298 static struct mapped_device
*dm_hash_rename(struct dm_ioctl
*param
,
301 char *new_name
, *old_name
;
302 struct hash_cell
*hc
;
303 struct dm_table
*table
;
304 struct mapped_device
*md
;
309 new_name
= kstrdup(new, GFP_KERNEL
);
311 return ERR_PTR(-ENOMEM
);
313 down_write(&_hash_lock
);
318 hc
= __get_name_cell(new);
320 DMWARN("asked to rename to an already-existing name %s -> %s",
323 up_write(&_hash_lock
);
325 return ERR_PTR(-EBUSY
);
329 * Is there such a device as 'old' ?
331 hc
= __get_name_cell(param
->name
);
333 DMWARN("asked to rename a non-existent device %s -> %s",
335 up_write(&_hash_lock
);
337 return ERR_PTR(-ENXIO
);
341 * rename and move the name cell.
343 list_del(&hc
->name_list
);
345 mutex_lock(&dm_hash_cells_mutex
);
347 mutex_unlock(&dm_hash_cells_mutex
);
348 list_add(&hc
->name_list
, _name_buckets
+ hash_str(new_name
));
351 * Wake up any dm event waiters.
353 table
= dm_get_live_table(hc
->md
);
355 dm_table_event(table
);
359 if (!dm_kobject_uevent(hc
->md
, KOBJ_CHANGE
, param
->event_nr
))
360 param
->flags
|= DM_UEVENT_GENERATED_FLAG
;
363 up_write(&_hash_lock
);
369 /*-----------------------------------------------------------------
370 * Implementation of the ioctl commands
371 *---------------------------------------------------------------*/
373 * All the ioctl commands get dispatched to functions with this
376 typedef int (*ioctl_fn
)(struct dm_ioctl
*param
, size_t param_size
);
378 static int remove_all(struct dm_ioctl
*param
, size_t param_size
)
380 dm_hash_remove_all(1);
381 param
->data_size
= 0;
386 * Round up the ptr to an 8-byte boundary.
389 static inline void *align_ptr(void *ptr
)
391 return (void *) (((size_t) (ptr
+ ALIGN_MASK
)) & ~ALIGN_MASK
);
395 * Retrieves the data payload buffer from an already allocated
398 static void *get_result_buffer(struct dm_ioctl
*param
, size_t param_size
,
401 param
->data_start
= align_ptr(param
+ 1) - (void *) param
;
403 if (param
->data_start
< param_size
)
404 *len
= param_size
- param
->data_start
;
408 return ((void *) param
) + param
->data_start
;
411 static int list_devices(struct dm_ioctl
*param
, size_t param_size
)
414 struct hash_cell
*hc
;
415 size_t len
, needed
= 0;
416 struct gendisk
*disk
;
417 struct dm_name_list
*nl
, *old_nl
= NULL
;
419 down_write(&_hash_lock
);
422 * Loop through all the devices working out how much
425 for (i
= 0; i
< NUM_BUCKETS
; i
++) {
426 list_for_each_entry (hc
, _name_buckets
+ i
, name_list
) {
427 needed
+= sizeof(struct dm_name_list
);
428 needed
+= strlen(hc
->name
) + 1;
429 needed
+= ALIGN_MASK
;
434 * Grab our output buffer.
436 nl
= get_result_buffer(param
, param_size
, &len
);
438 param
->flags
|= DM_BUFFER_FULL_FLAG
;
441 param
->data_size
= param
->data_start
+ needed
;
443 nl
->dev
= 0; /* Flags no data */
446 * Now loop through filling out the names.
448 for (i
= 0; i
< NUM_BUCKETS
; i
++) {
449 list_for_each_entry (hc
, _name_buckets
+ i
, name_list
) {
451 old_nl
->next
= (uint32_t) ((void *) nl
-
453 disk
= dm_disk(hc
->md
);
454 nl
->dev
= huge_encode_dev(disk_devt(disk
));
456 strcpy(nl
->name
, hc
->name
);
459 nl
= align_ptr(((void *) ++nl
) + strlen(hc
->name
) + 1);
464 up_write(&_hash_lock
);
468 static void list_version_get_needed(struct target_type
*tt
, void *needed_param
)
470 size_t *needed
= needed_param
;
472 *needed
+= sizeof(struct dm_target_versions
);
473 *needed
+= strlen(tt
->name
);
474 *needed
+= ALIGN_MASK
;
477 static void list_version_get_info(struct target_type
*tt
, void *param
)
479 struct vers_iter
*info
= param
;
481 /* Check space - it might have changed since the first iteration */
482 if ((char *)info
->vers
+ sizeof(tt
->version
) + strlen(tt
->name
) + 1 >
485 info
->flags
= DM_BUFFER_FULL_FLAG
;
490 info
->old_vers
->next
= (uint32_t) ((void *)info
->vers
-
491 (void *)info
->old_vers
);
492 info
->vers
->version
[0] = tt
->version
[0];
493 info
->vers
->version
[1] = tt
->version
[1];
494 info
->vers
->version
[2] = tt
->version
[2];
495 info
->vers
->next
= 0;
496 strcpy(info
->vers
->name
, tt
->name
);
498 info
->old_vers
= info
->vers
;
499 info
->vers
= align_ptr(((void *) ++info
->vers
) + strlen(tt
->name
) + 1);
502 static int list_versions(struct dm_ioctl
*param
, size_t param_size
)
504 size_t len
, needed
= 0;
505 struct dm_target_versions
*vers
;
506 struct vers_iter iter_info
;
509 * Loop through all the devices working out how much
512 dm_target_iterate(list_version_get_needed
, &needed
);
515 * Grab our output buffer.
517 vers
= get_result_buffer(param
, param_size
, &len
);
519 param
->flags
|= DM_BUFFER_FULL_FLAG
;
522 param
->data_size
= param
->data_start
+ needed
;
524 iter_info
.param_size
= param_size
;
525 iter_info
.old_vers
= NULL
;
526 iter_info
.vers
= vers
;
528 iter_info
.end
= (char *)vers
+len
;
531 * Now loop through filling out the names & versions.
533 dm_target_iterate(list_version_get_info
, &iter_info
);
534 param
->flags
|= iter_info
.flags
;
540 static int check_name(const char *name
)
542 if (strchr(name
, '/')) {
543 DMWARN("invalid device name");
551 * On successful return, the caller must not attempt to acquire
552 * _hash_lock without first calling dm_table_put, because dm_table_destroy
553 * waits for this dm_table_put and could be called under this lock.
555 static struct dm_table
*dm_get_inactive_table(struct mapped_device
*md
)
557 struct hash_cell
*hc
;
558 struct dm_table
*table
= NULL
;
560 down_read(&_hash_lock
);
561 hc
= dm_get_mdptr(md
);
562 if (!hc
|| hc
->md
!= md
) {
563 DMWARN("device has been removed from the dev hash table.");
572 up_read(&_hash_lock
);
577 static struct dm_table
*dm_get_live_or_inactive_table(struct mapped_device
*md
,
578 struct dm_ioctl
*param
)
580 return (param
->flags
& DM_QUERY_INACTIVE_TABLE_FLAG
) ?
581 dm_get_inactive_table(md
) : dm_get_live_table(md
);
585 * Fills in a dm_ioctl structure, ready for sending back to
588 static void __dev_status(struct mapped_device
*md
, struct dm_ioctl
*param
)
590 struct gendisk
*disk
= dm_disk(md
);
591 struct dm_table
*table
;
593 param
->flags
&= ~(DM_SUSPEND_FLAG
| DM_READONLY_FLAG
|
594 DM_ACTIVE_PRESENT_FLAG
);
596 if (dm_suspended_md(md
))
597 param
->flags
|= DM_SUSPEND_FLAG
;
599 param
->dev
= huge_encode_dev(disk_devt(disk
));
602 * Yes, this will be out of date by the time it gets back
603 * to userland, but it is still very useful for
606 param
->open_count
= dm_open_count(md
);
608 param
->event_nr
= dm_get_event_nr(md
);
609 param
->target_count
= 0;
611 table
= dm_get_live_table(md
);
613 if (!(param
->flags
& DM_QUERY_INACTIVE_TABLE_FLAG
)) {
614 if (get_disk_ro(disk
))
615 param
->flags
|= DM_READONLY_FLAG
;
616 param
->target_count
= dm_table_get_num_targets(table
);
620 param
->flags
|= DM_ACTIVE_PRESENT_FLAG
;
623 if (param
->flags
& DM_QUERY_INACTIVE_TABLE_FLAG
) {
624 table
= dm_get_inactive_table(md
);
626 if (!(dm_table_get_mode(table
) & FMODE_WRITE
))
627 param
->flags
|= DM_READONLY_FLAG
;
628 param
->target_count
= dm_table_get_num_targets(table
);
634 static int dev_create(struct dm_ioctl
*param
, size_t param_size
)
636 int r
, m
= DM_ANY_MINOR
;
637 struct mapped_device
*md
;
639 r
= check_name(param
->name
);
643 if (param
->flags
& DM_PERSISTENT_DEV_FLAG
)
644 m
= MINOR(huge_decode_dev(param
->dev
));
646 r
= dm_create(m
, &md
);
650 r
= dm_hash_insert(param
->name
, *param
->uuid
? param
->uuid
: NULL
, md
);
657 param
->flags
&= ~DM_INACTIVE_PRESENT_FLAG
;
659 __dev_status(md
, param
);
667 * Always use UUID for lookups if it's present, otherwise use name or dev.
669 static struct hash_cell
*__find_device_hash_cell(struct dm_ioctl
*param
)
671 struct mapped_device
*md
;
675 return __get_uuid_cell(param
->uuid
);
678 return __get_name_cell(param
->name
);
680 md
= dm_get_md(huge_decode_dev(param
->dev
));
684 mdptr
= dm_get_mdptr(md
);
692 static struct mapped_device
*find_device(struct dm_ioctl
*param
)
694 struct hash_cell
*hc
;
695 struct mapped_device
*md
= NULL
;
697 down_read(&_hash_lock
);
698 hc
= __find_device_hash_cell(param
);
703 * Sneakily write in both the name and the uuid
704 * while we have the cell.
706 strlcpy(param
->name
, hc
->name
, sizeof(param
->name
));
708 strlcpy(param
->uuid
, hc
->uuid
, sizeof(param
->uuid
));
710 param
->uuid
[0] = '\0';
713 param
->flags
|= DM_INACTIVE_PRESENT_FLAG
;
715 param
->flags
&= ~DM_INACTIVE_PRESENT_FLAG
;
717 up_read(&_hash_lock
);
722 static int dev_remove(struct dm_ioctl
*param
, size_t param_size
)
724 struct hash_cell
*hc
;
725 struct mapped_device
*md
;
728 down_write(&_hash_lock
);
729 hc
= __find_device_hash_cell(param
);
732 DMWARN("device doesn't appear to be in the dev hash table.");
733 up_write(&_hash_lock
);
740 * Ensure the device is not open and nothing further can open it.
742 r
= dm_lock_for_deletion(md
);
744 DMWARN("unable to remove open device %s", hc
->name
);
745 up_write(&_hash_lock
);
751 up_write(&_hash_lock
);
753 if (!dm_kobject_uevent(md
, KOBJ_REMOVE
, param
->event_nr
))
754 param
->flags
|= DM_UEVENT_GENERATED_FLAG
;
762 * Check a string doesn't overrun the chunk of
763 * memory we copied from userland.
765 static int invalid_str(char *str
, void *end
)
767 while ((void *) str
< end
)
774 static int dev_rename(struct dm_ioctl
*param
, size_t param_size
)
777 char *new_name
= (char *) param
+ param
->data_start
;
778 struct mapped_device
*md
;
780 if (new_name
< param
->data
||
781 invalid_str(new_name
, (void *) param
+ param_size
) ||
782 strlen(new_name
) > DM_NAME_LEN
- 1) {
783 DMWARN("Invalid new logical volume name supplied.");
787 r
= check_name(new_name
);
791 md
= dm_hash_rename(param
, new_name
);
795 __dev_status(md
, param
);
801 static int dev_set_geometry(struct dm_ioctl
*param
, size_t param_size
)
804 struct mapped_device
*md
;
805 struct hd_geometry geometry
;
806 unsigned long indata
[4];
807 char *geostr
= (char *) param
+ param
->data_start
;
809 md
= find_device(param
);
813 if (geostr
< param
->data
||
814 invalid_str(geostr
, (void *) param
+ param_size
)) {
815 DMWARN("Invalid geometry supplied.");
819 x
= sscanf(geostr
, "%lu %lu %lu %lu", indata
,
820 indata
+ 1, indata
+ 2, indata
+ 3);
823 DMWARN("Unable to interpret geometry settings.");
827 if (indata
[0] > 65535 || indata
[1] > 255 ||
828 indata
[2] > 255 || indata
[3] > ULONG_MAX
) {
829 DMWARN("Geometry exceeds range limits.");
833 geometry
.cylinders
= indata
[0];
834 geometry
.heads
= indata
[1];
835 geometry
.sectors
= indata
[2];
836 geometry
.start
= indata
[3];
838 r
= dm_set_geometry(md
, &geometry
);
840 param
->data_size
= 0;
847 static int do_suspend(struct dm_ioctl
*param
)
850 unsigned suspend_flags
= DM_SUSPEND_LOCKFS_FLAG
;
851 struct mapped_device
*md
;
853 md
= find_device(param
);
857 if (param
->flags
& DM_SKIP_LOCKFS_FLAG
)
858 suspend_flags
&= ~DM_SUSPEND_LOCKFS_FLAG
;
859 if (param
->flags
& DM_NOFLUSH_FLAG
)
860 suspend_flags
|= DM_SUSPEND_NOFLUSH_FLAG
;
862 if (!dm_suspended_md(md
)) {
863 r
= dm_suspend(md
, suspend_flags
);
868 __dev_status(md
, param
);
876 static int do_resume(struct dm_ioctl
*param
)
879 unsigned suspend_flags
= DM_SUSPEND_LOCKFS_FLAG
;
880 struct hash_cell
*hc
;
881 struct mapped_device
*md
;
882 struct dm_table
*new_map
, *old_map
= NULL
;
884 down_write(&_hash_lock
);
886 hc
= __find_device_hash_cell(param
);
888 DMWARN("device doesn't appear to be in the dev hash table.");
889 up_write(&_hash_lock
);
895 new_map
= hc
->new_map
;
897 param
->flags
&= ~DM_INACTIVE_PRESENT_FLAG
;
899 up_write(&_hash_lock
);
901 /* Do we need to load a new map ? */
903 /* Suspend if it isn't already suspended */
904 if (param
->flags
& DM_SKIP_LOCKFS_FLAG
)
905 suspend_flags
&= ~DM_SUSPEND_LOCKFS_FLAG
;
906 if (param
->flags
& DM_NOFLUSH_FLAG
)
907 suspend_flags
|= DM_SUSPEND_NOFLUSH_FLAG
;
908 if (!dm_suspended_md(md
))
909 dm_suspend(md
, suspend_flags
);
911 old_map
= dm_swap_table(md
, new_map
);
912 if (IS_ERR(old_map
)) {
913 dm_table_destroy(new_map
);
915 return PTR_ERR(old_map
);
918 if (dm_table_get_mode(new_map
) & FMODE_WRITE
)
919 set_disk_ro(dm_disk(md
), 0);
921 set_disk_ro(dm_disk(md
), 1);
924 if (dm_suspended_md(md
)) {
926 if (!r
&& !dm_kobject_uevent(md
, KOBJ_CHANGE
, param
->event_nr
))
927 param
->flags
|= DM_UEVENT_GENERATED_FLAG
;
931 dm_table_destroy(old_map
);
934 __dev_status(md
, param
);
941 * Set or unset the suspension state of a device.
942 * If the device already is in the requested state we just return its status.
944 static int dev_suspend(struct dm_ioctl
*param
, size_t param_size
)
946 if (param
->flags
& DM_SUSPEND_FLAG
)
947 return do_suspend(param
);
949 return do_resume(param
);
953 * Copies device info back to user space, used by
954 * the create and info ioctls.
956 static int dev_status(struct dm_ioctl
*param
, size_t param_size
)
958 struct mapped_device
*md
;
960 md
= find_device(param
);
964 __dev_status(md
, param
);
971 * Build up the status struct for each target
973 static void retrieve_status(struct dm_table
*table
,
974 struct dm_ioctl
*param
, size_t param_size
)
976 unsigned int i
, num_targets
;
977 struct dm_target_spec
*spec
;
978 char *outbuf
, *outptr
;
980 size_t remaining
, len
, used
= 0;
982 outptr
= outbuf
= get_result_buffer(param
, param_size
, &len
);
984 if (param
->flags
& DM_STATUS_TABLE_FLAG
)
985 type
= STATUSTYPE_TABLE
;
987 type
= STATUSTYPE_INFO
;
989 /* Get all the target info */
990 num_targets
= dm_table_get_num_targets(table
);
991 for (i
= 0; i
< num_targets
; i
++) {
992 struct dm_target
*ti
= dm_table_get_target(table
, i
);
994 remaining
= len
- (outptr
- outbuf
);
995 if (remaining
<= sizeof(struct dm_target_spec
)) {
996 param
->flags
|= DM_BUFFER_FULL_FLAG
;
1000 spec
= (struct dm_target_spec
*) outptr
;
1003 spec
->sector_start
= ti
->begin
;
1004 spec
->length
= ti
->len
;
1005 strncpy(spec
->target_type
, ti
->type
->name
,
1006 sizeof(spec
->target_type
));
1008 outptr
+= sizeof(struct dm_target_spec
);
1009 remaining
= len
- (outptr
- outbuf
);
1010 if (remaining
<= 0) {
1011 param
->flags
|= DM_BUFFER_FULL_FLAG
;
1015 /* Get the status/table string from the target driver */
1016 if (ti
->type
->status
) {
1017 if (ti
->type
->status(ti
, type
, outptr
, remaining
)) {
1018 param
->flags
|= DM_BUFFER_FULL_FLAG
;
1024 outptr
+= strlen(outptr
) + 1;
1025 used
= param
->data_start
+ (outptr
- outbuf
);
1027 outptr
= align_ptr(outptr
);
1028 spec
->next
= outptr
- outbuf
;
1032 param
->data_size
= used
;
1034 param
->target_count
= num_targets
;
1038 * Wait for a device to report an event
1040 static int dev_wait(struct dm_ioctl
*param
, size_t param_size
)
1043 struct mapped_device
*md
;
1044 struct dm_table
*table
;
1046 md
= find_device(param
);
1051 * Wait for a notification event
1053 if (dm_wait_event(md
, param
->event_nr
)) {
1059 * The userland program is going to want to know what
1060 * changed to trigger the event, so we may as well tell
1061 * him and save an ioctl.
1063 __dev_status(md
, param
);
1065 table
= dm_get_live_or_inactive_table(md
, param
);
1067 retrieve_status(table
, param
, param_size
);
1068 dm_table_put(table
);
1077 static inline fmode_t
get_mode(struct dm_ioctl
*param
)
1079 fmode_t mode
= FMODE_READ
| FMODE_WRITE
;
1081 if (param
->flags
& DM_READONLY_FLAG
)
1087 static int next_target(struct dm_target_spec
*last
, uint32_t next
, void *end
,
1088 struct dm_target_spec
**spec
, char **target_params
)
1090 *spec
= (struct dm_target_spec
*) ((unsigned char *) last
+ next
);
1091 *target_params
= (char *) (*spec
+ 1);
1093 if (*spec
< (last
+ 1))
1096 return invalid_str(*target_params
, end
);
1099 static int populate_table(struct dm_table
*table
,
1100 struct dm_ioctl
*param
, size_t param_size
)
1104 struct dm_target_spec
*spec
= (struct dm_target_spec
*) param
;
1105 uint32_t next
= param
->data_start
;
1106 void *end
= (void *) param
+ param_size
;
1107 char *target_params
;
1109 if (!param
->target_count
) {
1110 DMWARN("populate_table: no targets specified");
1114 for (i
= 0; i
< param
->target_count
; i
++) {
1116 r
= next_target(spec
, next
, end
, &spec
, &target_params
);
1118 DMWARN("unable to find target");
1122 r
= dm_table_add_target(table
, spec
->target_type
,
1123 (sector_t
) spec
->sector_start
,
1124 (sector_t
) spec
->length
,
1127 DMWARN("error adding target to table");
1134 r
= dm_table_set_type(table
);
1136 DMWARN("unable to set table type");
1140 return dm_table_complete(table
);
1143 static int table_prealloc_integrity(struct dm_table
*t
,
1144 struct mapped_device
*md
)
1146 struct list_head
*devices
= dm_table_get_devices(t
);
1147 struct dm_dev_internal
*dd
;
1149 list_for_each_entry(dd
, devices
, list
)
1150 if (bdev_get_integrity(dd
->dm_dev
.bdev
))
1151 return blk_integrity_register(dm_disk(md
), NULL
);
1156 static int table_load(struct dm_ioctl
*param
, size_t param_size
)
1159 struct hash_cell
*hc
;
1161 struct mapped_device
*md
;
1163 md
= find_device(param
);
1167 r
= dm_table_create(&t
, get_mode(param
), param
->target_count
, md
);
1171 r
= populate_table(t
, param
, param_size
);
1173 dm_table_destroy(t
);
1177 r
= table_prealloc_integrity(t
, md
);
1179 DMERR("%s: could not register integrity profile.",
1180 dm_device_name(md
));
1181 dm_table_destroy(t
);
1185 r
= dm_table_alloc_md_mempools(t
);
1187 DMWARN("unable to allocate mempools for this table");
1188 dm_table_destroy(t
);
1192 /* Protect md->type against concurrent table loads. */
1193 dm_lock_md_type(md
);
1194 if (dm_get_md_type(md
) == DM_TYPE_NONE
)
1195 /* Initial table load: acquire type of table. */
1196 dm_set_md_type(md
, dm_table_get_type(t
));
1197 else if (dm_get_md_type(md
) != dm_table_get_type(t
)) {
1198 DMWARN("can't change device type after initial table load.");
1199 dm_table_destroy(t
);
1200 dm_unlock_md_type(md
);
1204 dm_unlock_md_type(md
);
1206 /* stage inactive table */
1207 down_write(&_hash_lock
);
1208 hc
= dm_get_mdptr(md
);
1209 if (!hc
|| hc
->md
!= md
) {
1210 DMWARN("device has been removed from the dev hash table.");
1211 dm_table_destroy(t
);
1212 up_write(&_hash_lock
);
1218 dm_table_destroy(hc
->new_map
);
1220 up_write(&_hash_lock
);
1222 param
->flags
|= DM_INACTIVE_PRESENT_FLAG
;
1223 __dev_status(md
, param
);
1231 static int table_clear(struct dm_ioctl
*param
, size_t param_size
)
1233 struct hash_cell
*hc
;
1234 struct mapped_device
*md
;
1236 down_write(&_hash_lock
);
1238 hc
= __find_device_hash_cell(param
);
1240 DMWARN("device doesn't appear to be in the dev hash table.");
1241 up_write(&_hash_lock
);
1246 dm_table_destroy(hc
->new_map
);
1250 param
->flags
&= ~DM_INACTIVE_PRESENT_FLAG
;
1252 __dev_status(hc
->md
, param
);
1254 up_write(&_hash_lock
);
1261 * Retrieves a list of devices used by a particular dm device.
1263 static void retrieve_deps(struct dm_table
*table
,
1264 struct dm_ioctl
*param
, size_t param_size
)
1266 unsigned int count
= 0;
1267 struct list_head
*tmp
;
1269 struct dm_dev_internal
*dd
;
1270 struct dm_target_deps
*deps
;
1272 deps
= get_result_buffer(param
, param_size
, &len
);
1275 * Count the devices.
1277 list_for_each (tmp
, dm_table_get_devices(table
))
1281 * Check we have enough space.
1283 needed
= sizeof(*deps
) + (sizeof(*deps
->dev
) * count
);
1285 param
->flags
|= DM_BUFFER_FULL_FLAG
;
1290 * Fill in the devices.
1292 deps
->count
= count
;
1294 list_for_each_entry (dd
, dm_table_get_devices(table
), list
)
1295 deps
->dev
[count
++] = huge_encode_dev(dd
->dm_dev
.bdev
->bd_dev
);
1297 param
->data_size
= param
->data_start
+ needed
;
1300 static int table_deps(struct dm_ioctl
*param
, size_t param_size
)
1302 struct mapped_device
*md
;
1303 struct dm_table
*table
;
1305 md
= find_device(param
);
1309 __dev_status(md
, param
);
1311 table
= dm_get_live_or_inactive_table(md
, param
);
1313 retrieve_deps(table
, param
, param_size
);
1314 dm_table_put(table
);
1323 * Return the status of a device as a text string for each
1326 static int table_status(struct dm_ioctl
*param
, size_t param_size
)
1328 struct mapped_device
*md
;
1329 struct dm_table
*table
;
1331 md
= find_device(param
);
1335 __dev_status(md
, param
);
1337 table
= dm_get_live_or_inactive_table(md
, param
);
1339 retrieve_status(table
, param
, param_size
);
1340 dm_table_put(table
);
1349 * Pass a message to the target that's at the supplied device offset.
1351 static int target_message(struct dm_ioctl
*param
, size_t param_size
)
1355 struct mapped_device
*md
;
1356 struct dm_table
*table
;
1357 struct dm_target
*ti
;
1358 struct dm_target_msg
*tmsg
= (void *) param
+ param
->data_start
;
1360 md
= find_device(param
);
1364 if (tmsg
< (struct dm_target_msg
*) param
->data
||
1365 invalid_str(tmsg
->message
, (void *) param
+ param_size
)) {
1366 DMWARN("Invalid target message parameters.");
1371 r
= dm_split_args(&argc
, &argv
, tmsg
->message
);
1373 DMWARN("Failed to split target message parameters");
1377 table
= dm_get_live_table(md
);
1381 if (dm_deleting_md(md
)) {
1386 ti
= dm_table_find_target(table
, tmsg
->sector
);
1387 if (!dm_target_is_valid(ti
)) {
1388 DMWARN("Target message sector outside device.");
1390 } else if (ti
->type
->message
)
1391 r
= ti
->type
->message(ti
, argc
, argv
);
1393 DMWARN("Target type does not support messages");
1398 dm_table_put(table
);
1402 param
->data_size
= 0;
1407 /*-----------------------------------------------------------------
1408 * Implementation of open/close/ioctl on the special char
1410 *---------------------------------------------------------------*/
1411 static ioctl_fn
lookup_ioctl(unsigned int cmd
)
1417 {DM_VERSION_CMD
, NULL
}, /* version is dealt with elsewhere */
1418 {DM_REMOVE_ALL_CMD
, remove_all
},
1419 {DM_LIST_DEVICES_CMD
, list_devices
},
1421 {DM_DEV_CREATE_CMD
, dev_create
},
1422 {DM_DEV_REMOVE_CMD
, dev_remove
},
1423 {DM_DEV_RENAME_CMD
, dev_rename
},
1424 {DM_DEV_SUSPEND_CMD
, dev_suspend
},
1425 {DM_DEV_STATUS_CMD
, dev_status
},
1426 {DM_DEV_WAIT_CMD
, dev_wait
},
1428 {DM_TABLE_LOAD_CMD
, table_load
},
1429 {DM_TABLE_CLEAR_CMD
, table_clear
},
1430 {DM_TABLE_DEPS_CMD
, table_deps
},
1431 {DM_TABLE_STATUS_CMD
, table_status
},
1433 {DM_LIST_VERSIONS_CMD
, list_versions
},
1435 {DM_TARGET_MSG_CMD
, target_message
},
1436 {DM_DEV_SET_GEOMETRY_CMD
, dev_set_geometry
}
1439 return (cmd
>= ARRAY_SIZE(_ioctls
)) ? NULL
: _ioctls
[cmd
].fn
;
1443 * As well as checking the version compatibility this always
1444 * copies the kernel interface version out.
1446 static int check_version(unsigned int cmd
, struct dm_ioctl __user
*user
)
1448 uint32_t version
[3];
1451 if (copy_from_user(version
, user
->version
, sizeof(version
)))
1454 if ((DM_VERSION_MAJOR
!= version
[0]) ||
1455 (DM_VERSION_MINOR
< version
[1])) {
1456 DMWARN("ioctl interface mismatch: "
1457 "kernel(%u.%u.%u), user(%u.%u.%u), cmd(%d)",
1458 DM_VERSION_MAJOR
, DM_VERSION_MINOR
,
1459 DM_VERSION_PATCHLEVEL
,
1460 version
[0], version
[1], version
[2], cmd
);
1465 * Fill in the kernel version.
1467 version
[0] = DM_VERSION_MAJOR
;
1468 version
[1] = DM_VERSION_MINOR
;
1469 version
[2] = DM_VERSION_PATCHLEVEL
;
1470 if (copy_to_user(user
->version
, version
, sizeof(version
)))
1476 static void free_params(struct dm_ioctl
*param
)
1481 static int copy_params(struct dm_ioctl __user
*user
, struct dm_ioctl
**param
)
1483 struct dm_ioctl tmp
, *dmi
;
1485 if (copy_from_user(&tmp
, user
, sizeof(tmp
) - sizeof(tmp
.data
)))
1488 if (tmp
.data_size
< (sizeof(tmp
) - sizeof(tmp
.data
)))
1491 dmi
= vmalloc(tmp
.data_size
);
1495 if (copy_from_user(dmi
, user
, tmp
.data_size
)) {
1504 static int validate_params(uint cmd
, struct dm_ioctl
*param
)
1506 /* Always clear this flag */
1507 param
->flags
&= ~DM_BUFFER_FULL_FLAG
;
1508 param
->flags
&= ~DM_UEVENT_GENERATED_FLAG
;
1510 /* Ignores parameters */
1511 if (cmd
== DM_REMOVE_ALL_CMD
||
1512 cmd
== DM_LIST_DEVICES_CMD
||
1513 cmd
== DM_LIST_VERSIONS_CMD
)
1516 if ((cmd
== DM_DEV_CREATE_CMD
)) {
1517 if (!*param
->name
) {
1518 DMWARN("name not supplied when creating device");
1521 } else if ((*param
->uuid
&& *param
->name
)) {
1522 DMWARN("only supply one of name or uuid, cmd(%u)", cmd
);
1526 /* Ensure strings are terminated */
1527 param
->name
[DM_NAME_LEN
- 1] = '\0';
1528 param
->uuid
[DM_UUID_LEN
- 1] = '\0';
1533 static int ctl_ioctl(uint command
, struct dm_ioctl __user
*user
)
1537 struct dm_ioctl
*uninitialized_var(param
);
1541 /* only root can play with this */
1542 if (!capable(CAP_SYS_ADMIN
))
1545 if (_IOC_TYPE(command
) != DM_IOCTL
)
1548 cmd
= _IOC_NR(command
);
1551 * Check the interface version passed in. This also
1552 * writes out the kernel's interface version.
1554 r
= check_version(cmd
, user
);
1559 * Nothing more to do for the version command.
1561 if (cmd
== DM_VERSION_CMD
)
1564 fn
= lookup_ioctl(cmd
);
1566 DMWARN("dm_ctl_ioctl: unknown command 0x%x", command
);
1571 * Trying to avoid low memory issues when a device is
1574 current
->flags
|= PF_MEMALLOC
;
1577 * Copy the parameters into kernel space.
1579 r
= copy_params(user
, ¶m
);
1581 current
->flags
&= ~PF_MEMALLOC
;
1586 r
= validate_params(cmd
, param
);
1590 param_size
= param
->data_size
;
1591 param
->data_size
= sizeof(*param
);
1592 r
= fn(param
, param_size
);
1595 * Copy the results back to userland.
1597 if (!r
&& copy_to_user(user
, param
, param
->data_size
))
1605 static long dm_ctl_ioctl(struct file
*file
, uint command
, ulong u
)
1607 return (long)ctl_ioctl(command
, (struct dm_ioctl __user
*)u
);
1610 #ifdef CONFIG_COMPAT
1611 static long dm_compat_ctl_ioctl(struct file
*file
, uint command
, ulong u
)
1613 return (long)dm_ctl_ioctl(file
, command
, (ulong
) compat_ptr(u
));
1616 #define dm_compat_ctl_ioctl NULL
1619 static const struct file_operations _ctl_fops
= {
1620 .open
= nonseekable_open
,
1621 .unlocked_ioctl
= dm_ctl_ioctl
,
1622 .compat_ioctl
= dm_compat_ctl_ioctl
,
1623 .owner
= THIS_MODULE
,
1626 static struct miscdevice _dm_misc
= {
1627 .minor
= MISC_DYNAMIC_MINOR
,
1629 .nodename
= "mapper/control",
1634 * Create misc character device and link to DM_DIR/control.
1636 int __init
dm_interface_init(void)
1644 r
= misc_register(&_dm_misc
);
1646 DMERR("misc_register failed for control device");
1651 DMINFO("%d.%d.%d%s initialised: %s", DM_VERSION_MAJOR
,
1652 DM_VERSION_MINOR
, DM_VERSION_PATCHLEVEL
, DM_VERSION_EXTRA
,
1657 void dm_interface_exit(void)
1659 if (misc_deregister(&_dm_misc
) < 0)
1660 DMERR("misc_deregister failed for control device");
1666 * dm_copy_name_and_uuid - Copy mapped device name & uuid into supplied buffers
1667 * @md: Pointer to mapped_device
1668 * @name: Buffer (size DM_NAME_LEN) for name
1669 * @uuid: Buffer (size DM_UUID_LEN) for uuid or empty string if uuid not defined
1671 int dm_copy_name_and_uuid(struct mapped_device
*md
, char *name
, char *uuid
)
1674 struct hash_cell
*hc
;
1679 mutex_lock(&dm_hash_cells_mutex
);
1680 hc
= dm_get_mdptr(md
);
1681 if (!hc
|| hc
->md
!= md
) {
1687 strcpy(name
, hc
->name
);
1689 strcpy(uuid
, hc
->uuid
? : "");
1692 mutex_unlock(&dm_hash_cells_mutex
);