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_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
)
252 int i
, dev_skipped
, dev_removed
;
253 struct hash_cell
*hc
;
254 struct list_head
*tmp
, *n
;
256 down_write(&_hash_lock
);
259 dev_skipped
= dev_removed
= 0;
260 for (i
= 0; i
< NUM_BUCKETS
; i
++) {
261 list_for_each_safe (tmp
, n
, _name_buckets
+ i
) {
262 hc
= list_entry(tmp
, struct hash_cell
, name_list
);
264 if (keep_open_devices
&&
265 dm_lock_for_deletion(hc
->md
)) {
275 * Some mapped devices may be using other mapped devices, so if any
276 * still exist, repeat until we make no further progress.
282 DMWARN("remove_all left %d open device(s)", dev_skipped
);
285 up_write(&_hash_lock
);
288 static int dm_hash_rename(uint32_t cookie
, const char *old
, const char *new)
290 char *new_name
, *old_name
;
291 struct hash_cell
*hc
;
292 struct dm_table
*table
;
297 new_name
= kstrdup(new, GFP_KERNEL
);
301 down_write(&_hash_lock
);
306 hc
= __get_name_cell(new);
308 DMWARN("asked to rename to an already existing name %s -> %s",
311 up_write(&_hash_lock
);
317 * Is there such a device as 'old' ?
319 hc
= __get_name_cell(old
);
321 DMWARN("asked to rename a non existent device %s -> %s",
323 up_write(&_hash_lock
);
329 * rename and move the name cell.
331 list_del(&hc
->name_list
);
333 mutex_lock(&dm_hash_cells_mutex
);
335 mutex_unlock(&dm_hash_cells_mutex
);
336 list_add(&hc
->name_list
, _name_buckets
+ hash_str(new_name
));
339 * Wake up any dm event waiters.
341 table
= dm_get_table(hc
->md
);
343 dm_table_event(table
);
347 dm_kobject_uevent(hc
->md
, KOBJ_CHANGE
, cookie
);
350 up_write(&_hash_lock
);
355 /*-----------------------------------------------------------------
356 * Implementation of the ioctl commands
357 *---------------------------------------------------------------*/
359 * All the ioctl commands get dispatched to functions with this
362 typedef int (*ioctl_fn
)(struct dm_ioctl
*param
, size_t param_size
);
364 static int remove_all(struct dm_ioctl
*param
, size_t param_size
)
366 dm_hash_remove_all(1);
367 param
->data_size
= 0;
372 * Round up the ptr to an 8-byte boundary.
375 static inline void *align_ptr(void *ptr
)
377 return (void *) (((size_t) (ptr
+ ALIGN_MASK
)) & ~ALIGN_MASK
);
381 * Retrieves the data payload buffer from an already allocated
384 static void *get_result_buffer(struct dm_ioctl
*param
, size_t param_size
,
387 param
->data_start
= align_ptr(param
+ 1) - (void *) param
;
389 if (param
->data_start
< param_size
)
390 *len
= param_size
- param
->data_start
;
394 return ((void *) param
) + param
->data_start
;
397 static int list_devices(struct dm_ioctl
*param
, size_t param_size
)
400 struct hash_cell
*hc
;
401 size_t len
, needed
= 0;
402 struct gendisk
*disk
;
403 struct dm_name_list
*nl
, *old_nl
= NULL
;
405 down_write(&_hash_lock
);
408 * Loop through all the devices working out how much
411 for (i
= 0; i
< NUM_BUCKETS
; i
++) {
412 list_for_each_entry (hc
, _name_buckets
+ i
, name_list
) {
413 needed
+= sizeof(struct dm_name_list
);
414 needed
+= strlen(hc
->name
) + 1;
415 needed
+= ALIGN_MASK
;
420 * Grab our output buffer.
422 nl
= get_result_buffer(param
, param_size
, &len
);
424 param
->flags
|= DM_BUFFER_FULL_FLAG
;
427 param
->data_size
= param
->data_start
+ needed
;
429 nl
->dev
= 0; /* Flags no data */
432 * Now loop through filling out the names.
434 for (i
= 0; i
< NUM_BUCKETS
; i
++) {
435 list_for_each_entry (hc
, _name_buckets
+ i
, name_list
) {
437 old_nl
->next
= (uint32_t) ((void *) nl
-
439 disk
= dm_disk(hc
->md
);
440 nl
->dev
= huge_encode_dev(disk_devt(disk
));
442 strcpy(nl
->name
, hc
->name
);
445 nl
= align_ptr(((void *) ++nl
) + strlen(hc
->name
) + 1);
450 up_write(&_hash_lock
);
454 static void list_version_get_needed(struct target_type
*tt
, void *needed_param
)
456 size_t *needed
= needed_param
;
458 *needed
+= sizeof(struct dm_target_versions
);
459 *needed
+= strlen(tt
->name
);
460 *needed
+= ALIGN_MASK
;
463 static void list_version_get_info(struct target_type
*tt
, void *param
)
465 struct vers_iter
*info
= param
;
467 /* Check space - it might have changed since the first iteration */
468 if ((char *)info
->vers
+ sizeof(tt
->version
) + strlen(tt
->name
) + 1 >
471 info
->flags
= DM_BUFFER_FULL_FLAG
;
476 info
->old_vers
->next
= (uint32_t) ((void *)info
->vers
-
477 (void *)info
->old_vers
);
478 info
->vers
->version
[0] = tt
->version
[0];
479 info
->vers
->version
[1] = tt
->version
[1];
480 info
->vers
->version
[2] = tt
->version
[2];
481 info
->vers
->next
= 0;
482 strcpy(info
->vers
->name
, tt
->name
);
484 info
->old_vers
= info
->vers
;
485 info
->vers
= align_ptr(((void *) ++info
->vers
) + strlen(tt
->name
) + 1);
488 static int list_versions(struct dm_ioctl
*param
, size_t param_size
)
490 size_t len
, needed
= 0;
491 struct dm_target_versions
*vers
;
492 struct vers_iter iter_info
;
495 * Loop through all the devices working out how much
498 dm_target_iterate(list_version_get_needed
, &needed
);
501 * Grab our output buffer.
503 vers
= get_result_buffer(param
, param_size
, &len
);
505 param
->flags
|= DM_BUFFER_FULL_FLAG
;
508 param
->data_size
= param
->data_start
+ needed
;
510 iter_info
.param_size
= param_size
;
511 iter_info
.old_vers
= NULL
;
512 iter_info
.vers
= vers
;
514 iter_info
.end
= (char *)vers
+len
;
517 * Now loop through filling out the names & versions.
519 dm_target_iterate(list_version_get_info
, &iter_info
);
520 param
->flags
|= iter_info
.flags
;
528 static int check_name(const char *name
)
530 if (strchr(name
, '/')) {
531 DMWARN("invalid device name");
539 * Fills in a dm_ioctl structure, ready for sending back to
542 static int __dev_status(struct mapped_device
*md
, struct dm_ioctl
*param
)
544 struct gendisk
*disk
= dm_disk(md
);
545 struct dm_table
*table
;
547 param
->flags
&= ~(DM_SUSPEND_FLAG
| DM_READONLY_FLAG
|
548 DM_ACTIVE_PRESENT_FLAG
);
550 if (dm_suspended(md
))
551 param
->flags
|= DM_SUSPEND_FLAG
;
553 param
->dev
= huge_encode_dev(disk_devt(disk
));
556 * Yes, this will be out of date by the time it gets back
557 * to userland, but it is still very useful for
560 param
->open_count
= dm_open_count(md
);
562 if (get_disk_ro(disk
))
563 param
->flags
|= DM_READONLY_FLAG
;
565 param
->event_nr
= dm_get_event_nr(md
);
567 table
= dm_get_table(md
);
569 param
->flags
|= DM_ACTIVE_PRESENT_FLAG
;
570 param
->target_count
= dm_table_get_num_targets(table
);
573 param
->target_count
= 0;
578 static int dev_create(struct dm_ioctl
*param
, size_t param_size
)
580 int r
, m
= DM_ANY_MINOR
;
581 struct mapped_device
*md
;
583 r
= check_name(param
->name
);
587 if (param
->flags
& DM_PERSISTENT_DEV_FLAG
)
588 m
= MINOR(huge_decode_dev(param
->dev
));
590 r
= dm_create(m
, &md
);
594 r
= dm_hash_insert(param
->name
, *param
->uuid
? param
->uuid
: NULL
, md
);
600 param
->flags
&= ~DM_INACTIVE_PRESENT_FLAG
;
602 r
= __dev_status(md
, param
);
609 * Always use UUID for lookups if it's present, otherwise use name or dev.
611 static struct hash_cell
*__find_device_hash_cell(struct dm_ioctl
*param
)
613 struct mapped_device
*md
;
617 return __get_uuid_cell(param
->uuid
);
620 return __get_name_cell(param
->name
);
622 md
= dm_get_md(huge_decode_dev(param
->dev
));
626 mdptr
= dm_get_mdptr(md
);
634 static struct mapped_device
*find_device(struct dm_ioctl
*param
)
636 struct hash_cell
*hc
;
637 struct mapped_device
*md
= NULL
;
639 down_read(&_hash_lock
);
640 hc
= __find_device_hash_cell(param
);
645 * Sneakily write in both the name and the uuid
646 * while we have the cell.
648 strncpy(param
->name
, hc
->name
, sizeof(param
->name
));
650 strncpy(param
->uuid
, hc
->uuid
, sizeof(param
->uuid
)-1);
652 param
->uuid
[0] = '\0';
655 param
->flags
|= DM_INACTIVE_PRESENT_FLAG
;
657 param
->flags
&= ~DM_INACTIVE_PRESENT_FLAG
;
659 up_read(&_hash_lock
);
664 static int dev_remove(struct dm_ioctl
*param
, size_t param_size
)
666 struct hash_cell
*hc
;
667 struct mapped_device
*md
;
670 down_write(&_hash_lock
);
671 hc
= __find_device_hash_cell(param
);
674 DMWARN("device doesn't appear to be in the dev hash table.");
675 up_write(&_hash_lock
);
682 * Ensure the device is not open and nothing further can open it.
684 r
= dm_lock_for_deletion(md
);
686 DMWARN("unable to remove open device %s", hc
->name
);
687 up_write(&_hash_lock
);
693 up_write(&_hash_lock
);
695 dm_kobject_uevent(md
, KOBJ_REMOVE
, param
->event_nr
);
698 param
->data_size
= 0;
703 * Check a string doesn't overrun the chunk of
704 * memory we copied from userland.
706 static int invalid_str(char *str
, void *end
)
708 while ((void *) str
< end
)
715 static int dev_rename(struct dm_ioctl
*param
, size_t param_size
)
718 char *new_name
= (char *) param
+ param
->data_start
;
720 if (new_name
< param
->data
||
721 invalid_str(new_name
, (void *) param
+ param_size
) ||
722 strlen(new_name
) > DM_NAME_LEN
- 1) {
723 DMWARN("Invalid new logical volume name supplied.");
727 r
= check_name(new_name
);
731 param
->data_size
= 0;
732 return dm_hash_rename(param
->event_nr
, param
->name
, new_name
);
735 static int dev_set_geometry(struct dm_ioctl
*param
, size_t param_size
)
738 struct mapped_device
*md
;
739 struct hd_geometry geometry
;
740 unsigned long indata
[4];
741 char *geostr
= (char *) param
+ param
->data_start
;
743 md
= find_device(param
);
747 if (geostr
< param
->data
||
748 invalid_str(geostr
, (void *) param
+ param_size
)) {
749 DMWARN("Invalid geometry supplied.");
753 x
= sscanf(geostr
, "%lu %lu %lu %lu", indata
,
754 indata
+ 1, indata
+ 2, indata
+ 3);
757 DMWARN("Unable to interpret geometry settings.");
761 if (indata
[0] > 65535 || indata
[1] > 255 ||
762 indata
[2] > 255 || indata
[3] > ULONG_MAX
) {
763 DMWARN("Geometry exceeds range limits.");
767 geometry
.cylinders
= indata
[0];
768 geometry
.heads
= indata
[1];
769 geometry
.sectors
= indata
[2];
770 geometry
.start
= indata
[3];
772 r
= dm_set_geometry(md
, &geometry
);
774 r
= __dev_status(md
, param
);
776 param
->data_size
= 0;
783 static int do_suspend(struct dm_ioctl
*param
)
786 unsigned suspend_flags
= DM_SUSPEND_LOCKFS_FLAG
;
787 struct mapped_device
*md
;
789 md
= find_device(param
);
793 if (param
->flags
& DM_SKIP_LOCKFS_FLAG
)
794 suspend_flags
&= ~DM_SUSPEND_LOCKFS_FLAG
;
795 if (param
->flags
& DM_NOFLUSH_FLAG
)
796 suspend_flags
|= DM_SUSPEND_NOFLUSH_FLAG
;
798 if (!dm_suspended(md
))
799 r
= dm_suspend(md
, suspend_flags
);
802 r
= __dev_status(md
, param
);
808 static int do_resume(struct dm_ioctl
*param
)
811 unsigned suspend_flags
= DM_SUSPEND_LOCKFS_FLAG
;
812 struct hash_cell
*hc
;
813 struct mapped_device
*md
;
814 struct dm_table
*new_map
;
816 down_write(&_hash_lock
);
818 hc
= __find_device_hash_cell(param
);
820 DMWARN("device doesn't appear to be in the dev hash table.");
821 up_write(&_hash_lock
);
827 new_map
= hc
->new_map
;
829 param
->flags
&= ~DM_INACTIVE_PRESENT_FLAG
;
831 up_write(&_hash_lock
);
833 /* Do we need to load a new map ? */
835 /* Suspend if it isn't already suspended */
836 if (param
->flags
& DM_SKIP_LOCKFS_FLAG
)
837 suspend_flags
&= ~DM_SUSPEND_LOCKFS_FLAG
;
838 if (param
->flags
& DM_NOFLUSH_FLAG
)
839 suspend_flags
|= DM_SUSPEND_NOFLUSH_FLAG
;
840 if (!dm_suspended(md
))
841 dm_suspend(md
, suspend_flags
);
843 r
= dm_swap_table(md
, new_map
);
845 dm_table_destroy(new_map
);
850 if (dm_table_get_mode(new_map
) & FMODE_WRITE
)
851 set_disk_ro(dm_disk(md
), 0);
853 set_disk_ro(dm_disk(md
), 1);
856 if (dm_suspended(md
))
861 dm_kobject_uevent(md
, KOBJ_CHANGE
, param
->event_nr
);
862 r
= __dev_status(md
, param
);
870 * Set or unset the suspension state of a device.
871 * If the device already is in the requested state we just return its status.
873 static int dev_suspend(struct dm_ioctl
*param
, size_t param_size
)
875 if (param
->flags
& DM_SUSPEND_FLAG
)
876 return do_suspend(param
);
878 return do_resume(param
);
882 * Copies device info back to user space, used by
883 * the create and info ioctls.
885 static int dev_status(struct dm_ioctl
*param
, size_t param_size
)
888 struct mapped_device
*md
;
890 md
= find_device(param
);
894 r
= __dev_status(md
, param
);
900 * Build up the status struct for each target
902 static void retrieve_status(struct dm_table
*table
,
903 struct dm_ioctl
*param
, size_t param_size
)
905 unsigned int i
, num_targets
;
906 struct dm_target_spec
*spec
;
907 char *outbuf
, *outptr
;
909 size_t remaining
, len
, used
= 0;
911 outptr
= outbuf
= get_result_buffer(param
, param_size
, &len
);
913 if (param
->flags
& DM_STATUS_TABLE_FLAG
)
914 type
= STATUSTYPE_TABLE
;
916 type
= STATUSTYPE_INFO
;
918 /* Get all the target info */
919 num_targets
= dm_table_get_num_targets(table
);
920 for (i
= 0; i
< num_targets
; i
++) {
921 struct dm_target
*ti
= dm_table_get_target(table
, i
);
923 remaining
= len
- (outptr
- outbuf
);
924 if (remaining
<= sizeof(struct dm_target_spec
)) {
925 param
->flags
|= DM_BUFFER_FULL_FLAG
;
929 spec
= (struct dm_target_spec
*) outptr
;
932 spec
->sector_start
= ti
->begin
;
933 spec
->length
= ti
->len
;
934 strncpy(spec
->target_type
, ti
->type
->name
,
935 sizeof(spec
->target_type
));
937 outptr
+= sizeof(struct dm_target_spec
);
938 remaining
= len
- (outptr
- outbuf
);
939 if (remaining
<= 0) {
940 param
->flags
|= DM_BUFFER_FULL_FLAG
;
944 /* Get the status/table string from the target driver */
945 if (ti
->type
->status
) {
946 if (ti
->type
->status(ti
, type
, outptr
, remaining
)) {
947 param
->flags
|= DM_BUFFER_FULL_FLAG
;
953 outptr
+= strlen(outptr
) + 1;
954 used
= param
->data_start
+ (outptr
- outbuf
);
956 outptr
= align_ptr(outptr
);
957 spec
->next
= outptr
- outbuf
;
961 param
->data_size
= used
;
963 param
->target_count
= num_targets
;
967 * Wait for a device to report an event
969 static int dev_wait(struct dm_ioctl
*param
, size_t param_size
)
972 struct mapped_device
*md
;
973 struct dm_table
*table
;
975 md
= find_device(param
);
980 * Wait for a notification event
982 if (dm_wait_event(md
, param
->event_nr
)) {
988 * The userland program is going to want to know what
989 * changed to trigger the event, so we may as well tell
990 * him and save an ioctl.
992 r
= __dev_status(md
, param
);
996 table
= dm_get_table(md
);
998 retrieve_status(table
, param
, param_size
);
1007 static inline fmode_t
get_mode(struct dm_ioctl
*param
)
1009 fmode_t mode
= FMODE_READ
| FMODE_WRITE
;
1011 if (param
->flags
& DM_READONLY_FLAG
)
1017 static int next_target(struct dm_target_spec
*last
, uint32_t next
, void *end
,
1018 struct dm_target_spec
**spec
, char **target_params
)
1020 *spec
= (struct dm_target_spec
*) ((unsigned char *) last
+ next
);
1021 *target_params
= (char *) (*spec
+ 1);
1023 if (*spec
< (last
+ 1))
1026 return invalid_str(*target_params
, end
);
1029 static int populate_table(struct dm_table
*table
,
1030 struct dm_ioctl
*param
, size_t param_size
)
1034 struct dm_target_spec
*spec
= (struct dm_target_spec
*) param
;
1035 uint32_t next
= param
->data_start
;
1036 void *end
= (void *) param
+ param_size
;
1037 char *target_params
;
1039 if (!param
->target_count
) {
1040 DMWARN("populate_table: no targets specified");
1044 for (i
= 0; i
< param
->target_count
; i
++) {
1046 r
= next_target(spec
, next
, end
, &spec
, &target_params
);
1048 DMWARN("unable to find target");
1052 r
= dm_table_add_target(table
, spec
->target_type
,
1053 (sector_t
) spec
->sector_start
,
1054 (sector_t
) spec
->length
,
1057 DMWARN("error adding target to table");
1064 r
= dm_table_set_type(table
);
1066 DMWARN("unable to set table type");
1070 return dm_table_complete(table
);
1073 static int table_prealloc_integrity(struct dm_table
*t
,
1074 struct mapped_device
*md
)
1076 struct list_head
*devices
= dm_table_get_devices(t
);
1077 struct dm_dev_internal
*dd
;
1079 list_for_each_entry(dd
, devices
, list
)
1080 if (bdev_get_integrity(dd
->dm_dev
.bdev
))
1081 return blk_integrity_register(dm_disk(md
), NULL
);
1086 static int table_load(struct dm_ioctl
*param
, size_t param_size
)
1089 struct hash_cell
*hc
;
1091 struct mapped_device
*md
;
1093 md
= find_device(param
);
1097 r
= dm_table_create(&t
, get_mode(param
), param
->target_count
, md
);
1101 r
= populate_table(t
, param
, param_size
);
1103 dm_table_destroy(t
);
1107 r
= table_prealloc_integrity(t
, md
);
1109 DMERR("%s: could not register integrity profile.",
1110 dm_device_name(md
));
1111 dm_table_destroy(t
);
1115 r
= dm_table_alloc_md_mempools(t
);
1117 DMWARN("unable to allocate mempools for this table");
1118 dm_table_destroy(t
);
1122 down_write(&_hash_lock
);
1123 hc
= dm_get_mdptr(md
);
1124 if (!hc
|| hc
->md
!= md
) {
1125 DMWARN("device has been removed from the dev hash table.");
1126 dm_table_destroy(t
);
1127 up_write(&_hash_lock
);
1133 dm_table_destroy(hc
->new_map
);
1135 up_write(&_hash_lock
);
1137 param
->flags
|= DM_INACTIVE_PRESENT_FLAG
;
1138 r
= __dev_status(md
, param
);
1146 static int table_clear(struct dm_ioctl
*param
, size_t param_size
)
1149 struct hash_cell
*hc
;
1150 struct mapped_device
*md
;
1152 down_write(&_hash_lock
);
1154 hc
= __find_device_hash_cell(param
);
1156 DMWARN("device doesn't appear to be in the dev hash table.");
1157 up_write(&_hash_lock
);
1162 dm_table_destroy(hc
->new_map
);
1166 param
->flags
&= ~DM_INACTIVE_PRESENT_FLAG
;
1168 r
= __dev_status(hc
->md
, param
);
1170 up_write(&_hash_lock
);
1176 * Retrieves a list of devices used by a particular dm device.
1178 static void retrieve_deps(struct dm_table
*table
,
1179 struct dm_ioctl
*param
, size_t param_size
)
1181 unsigned int count
= 0;
1182 struct list_head
*tmp
;
1184 struct dm_dev_internal
*dd
;
1185 struct dm_target_deps
*deps
;
1187 deps
= get_result_buffer(param
, param_size
, &len
);
1190 * Count the devices.
1192 list_for_each (tmp
, dm_table_get_devices(table
))
1196 * Check we have enough space.
1198 needed
= sizeof(*deps
) + (sizeof(*deps
->dev
) * count
);
1200 param
->flags
|= DM_BUFFER_FULL_FLAG
;
1205 * Fill in the devices.
1207 deps
->count
= count
;
1209 list_for_each_entry (dd
, dm_table_get_devices(table
), list
)
1210 deps
->dev
[count
++] = huge_encode_dev(dd
->dm_dev
.bdev
->bd_dev
);
1212 param
->data_size
= param
->data_start
+ needed
;
1215 static int table_deps(struct dm_ioctl
*param
, size_t param_size
)
1218 struct mapped_device
*md
;
1219 struct dm_table
*table
;
1221 md
= find_device(param
);
1225 r
= __dev_status(md
, param
);
1229 table
= dm_get_table(md
);
1231 retrieve_deps(table
, param
, param_size
);
1232 dm_table_put(table
);
1241 * Return the status of a device as a text string for each
1244 static int table_status(struct dm_ioctl
*param
, size_t param_size
)
1247 struct mapped_device
*md
;
1248 struct dm_table
*table
;
1250 md
= find_device(param
);
1254 r
= __dev_status(md
, param
);
1258 table
= dm_get_table(md
);
1260 retrieve_status(table
, param
, param_size
);
1261 dm_table_put(table
);
1270 * Pass a message to the target that's at the supplied device offset.
1272 static int target_message(struct dm_ioctl
*param
, size_t param_size
)
1276 struct mapped_device
*md
;
1277 struct dm_table
*table
;
1278 struct dm_target
*ti
;
1279 struct dm_target_msg
*tmsg
= (void *) param
+ param
->data_start
;
1281 md
= find_device(param
);
1285 r
= __dev_status(md
, param
);
1289 if (tmsg
< (struct dm_target_msg
*) param
->data
||
1290 invalid_str(tmsg
->message
, (void *) param
+ param_size
)) {
1291 DMWARN("Invalid target message parameters.");
1296 r
= dm_split_args(&argc
, &argv
, tmsg
->message
);
1298 DMWARN("Failed to split target message parameters");
1302 table
= dm_get_table(md
);
1306 ti
= dm_table_find_target(table
, tmsg
->sector
);
1307 if (!dm_target_is_valid(ti
)) {
1308 DMWARN("Target message sector outside device.");
1310 } else if (ti
->type
->message
)
1311 r
= ti
->type
->message(ti
, argc
, argv
);
1313 DMWARN("Target type does not support messages");
1317 dm_table_put(table
);
1321 param
->data_size
= 0;
1326 /*-----------------------------------------------------------------
1327 * Implementation of open/close/ioctl on the special char
1329 *---------------------------------------------------------------*/
1330 static ioctl_fn
lookup_ioctl(unsigned int cmd
)
1336 {DM_VERSION_CMD
, NULL
}, /* version is dealt with elsewhere */
1337 {DM_REMOVE_ALL_CMD
, remove_all
},
1338 {DM_LIST_DEVICES_CMD
, list_devices
},
1340 {DM_DEV_CREATE_CMD
, dev_create
},
1341 {DM_DEV_REMOVE_CMD
, dev_remove
},
1342 {DM_DEV_RENAME_CMD
, dev_rename
},
1343 {DM_DEV_SUSPEND_CMD
, dev_suspend
},
1344 {DM_DEV_STATUS_CMD
, dev_status
},
1345 {DM_DEV_WAIT_CMD
, dev_wait
},
1347 {DM_TABLE_LOAD_CMD
, table_load
},
1348 {DM_TABLE_CLEAR_CMD
, table_clear
},
1349 {DM_TABLE_DEPS_CMD
, table_deps
},
1350 {DM_TABLE_STATUS_CMD
, table_status
},
1352 {DM_LIST_VERSIONS_CMD
, list_versions
},
1354 {DM_TARGET_MSG_CMD
, target_message
},
1355 {DM_DEV_SET_GEOMETRY_CMD
, dev_set_geometry
}
1358 return (cmd
>= ARRAY_SIZE(_ioctls
)) ? NULL
: _ioctls
[cmd
].fn
;
1362 * As well as checking the version compatibility this always
1363 * copies the kernel interface version out.
1365 static int check_version(unsigned int cmd
, struct dm_ioctl __user
*user
)
1367 uint32_t version
[3];
1370 if (copy_from_user(version
, user
->version
, sizeof(version
)))
1373 if ((DM_VERSION_MAJOR
!= version
[0]) ||
1374 (DM_VERSION_MINOR
< version
[1])) {
1375 DMWARN("ioctl interface mismatch: "
1376 "kernel(%u.%u.%u), user(%u.%u.%u), cmd(%d)",
1377 DM_VERSION_MAJOR
, DM_VERSION_MINOR
,
1378 DM_VERSION_PATCHLEVEL
,
1379 version
[0], version
[1], version
[2], cmd
);
1384 * Fill in the kernel version.
1386 version
[0] = DM_VERSION_MAJOR
;
1387 version
[1] = DM_VERSION_MINOR
;
1388 version
[2] = DM_VERSION_PATCHLEVEL
;
1389 if (copy_to_user(user
->version
, version
, sizeof(version
)))
1395 static void free_params(struct dm_ioctl
*param
)
1400 static int copy_params(struct dm_ioctl __user
*user
, struct dm_ioctl
**param
)
1402 struct dm_ioctl tmp
, *dmi
;
1404 if (copy_from_user(&tmp
, user
, sizeof(tmp
) - sizeof(tmp
.data
)))
1407 if (tmp
.data_size
< (sizeof(tmp
) - sizeof(tmp
.data
)))
1410 dmi
= vmalloc(tmp
.data_size
);
1414 if (copy_from_user(dmi
, user
, tmp
.data_size
)) {
1423 static int validate_params(uint cmd
, struct dm_ioctl
*param
)
1425 /* Always clear this flag */
1426 param
->flags
&= ~DM_BUFFER_FULL_FLAG
;
1428 /* Ignores parameters */
1429 if (cmd
== DM_REMOVE_ALL_CMD
||
1430 cmd
== DM_LIST_DEVICES_CMD
||
1431 cmd
== DM_LIST_VERSIONS_CMD
)
1434 if ((cmd
== DM_DEV_CREATE_CMD
)) {
1435 if (!*param
->name
) {
1436 DMWARN("name not supplied when creating device");
1439 } else if ((*param
->uuid
&& *param
->name
)) {
1440 DMWARN("only supply one of name or uuid, cmd(%u)", cmd
);
1444 /* Ensure strings are terminated */
1445 param
->name
[DM_NAME_LEN
- 1] = '\0';
1446 param
->uuid
[DM_UUID_LEN
- 1] = '\0';
1451 static int ctl_ioctl(uint command
, struct dm_ioctl __user
*user
)
1455 struct dm_ioctl
*uninitialized_var(param
);
1459 /* only root can play with this */
1460 if (!capable(CAP_SYS_ADMIN
))
1463 if (_IOC_TYPE(command
) != DM_IOCTL
)
1466 cmd
= _IOC_NR(command
);
1469 * Check the interface version passed in. This also
1470 * writes out the kernel's interface version.
1472 r
= check_version(cmd
, user
);
1477 * Nothing more to do for the version command.
1479 if (cmd
== DM_VERSION_CMD
)
1482 fn
= lookup_ioctl(cmd
);
1484 DMWARN("dm_ctl_ioctl: unknown command 0x%x", command
);
1489 * Trying to avoid low memory issues when a device is
1492 current
->flags
|= PF_MEMALLOC
;
1495 * Copy the parameters into kernel space.
1497 r
= copy_params(user
, ¶m
);
1499 current
->flags
&= ~PF_MEMALLOC
;
1504 r
= validate_params(cmd
, param
);
1508 param_size
= param
->data_size
;
1509 param
->data_size
= sizeof(*param
);
1510 r
= fn(param
, param_size
);
1513 * Copy the results back to userland.
1515 if (!r
&& copy_to_user(user
, param
, param
->data_size
))
1523 static long dm_ctl_ioctl(struct file
*file
, uint command
, ulong u
)
1525 return (long)ctl_ioctl(command
, (struct dm_ioctl __user
*)u
);
1528 #ifdef CONFIG_COMPAT
1529 static long dm_compat_ctl_ioctl(struct file
*file
, uint command
, ulong u
)
1531 return (long)dm_ctl_ioctl(file
, command
, (ulong
) compat_ptr(u
));
1534 #define dm_compat_ctl_ioctl NULL
1537 static const struct file_operations _ctl_fops
= {
1538 .unlocked_ioctl
= dm_ctl_ioctl
,
1539 .compat_ioctl
= dm_compat_ctl_ioctl
,
1540 .owner
= THIS_MODULE
,
1543 static struct miscdevice _dm_misc
= {
1544 .minor
= MISC_DYNAMIC_MINOR
,
1546 .nodename
= "mapper/control",
1551 * Create misc character device and link to DM_DIR/control.
1553 int __init
dm_interface_init(void)
1561 r
= misc_register(&_dm_misc
);
1563 DMERR("misc_register failed for control device");
1568 DMINFO("%d.%d.%d%s initialised: %s", DM_VERSION_MAJOR
,
1569 DM_VERSION_MINOR
, DM_VERSION_PATCHLEVEL
, DM_VERSION_EXTRA
,
1574 void dm_interface_exit(void)
1576 if (misc_deregister(&_dm_misc
) < 0)
1577 DMERR("misc_deregister failed for control device");
1583 * dm_copy_name_and_uuid - Copy mapped device name & uuid into supplied buffers
1584 * @md: Pointer to mapped_device
1585 * @name: Buffer (size DM_NAME_LEN) for name
1586 * @uuid: Buffer (size DM_UUID_LEN) for uuid or empty string if uuid not defined
1588 int dm_copy_name_and_uuid(struct mapped_device
*md
, char *name
, char *uuid
)
1591 struct hash_cell
*hc
;
1596 mutex_lock(&dm_hash_cells_mutex
);
1597 hc
= dm_get_mdptr(md
);
1598 if (!hc
|| hc
->md
!= md
) {
1604 strcpy(name
, hc
->name
);
1606 strcpy(uuid
, hc
->uuid
? : "");
1609 mutex_unlock(&dm_hash_cells_mutex
);