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>
19 #include <asm/uaccess.h>
21 #define DM_MSG_PREFIX "ioctl"
22 #define DM_DRIVER_EMAIL "dm-devel@redhat.com"
24 /*-----------------------------------------------------------------
25 * The ioctl interface needs to be able to look up devices by
27 *---------------------------------------------------------------*/
29 struct list_head name_list
;
30 struct list_head uuid_list
;
34 struct mapped_device
*md
;
35 struct dm_table
*new_map
;
40 struct dm_target_versions
*vers
, *old_vers
;
46 #define NUM_BUCKETS 64
47 #define MASK_BUCKETS (NUM_BUCKETS - 1)
48 static struct list_head _name_buckets
[NUM_BUCKETS
];
49 static struct list_head _uuid_buckets
[NUM_BUCKETS
];
51 static void dm_hash_remove_all(int keep_open_devices
);
54 * Guards access to both hash tables.
56 static DECLARE_RWSEM(_hash_lock
);
58 static void init_buckets(struct list_head
*buckets
)
62 for (i
= 0; i
< NUM_BUCKETS
; i
++)
63 INIT_LIST_HEAD(buckets
+ i
);
66 static int dm_hash_init(void)
68 init_buckets(_name_buckets
);
69 init_buckets(_uuid_buckets
);
73 static void dm_hash_exit(void)
75 dm_hash_remove_all(0);
78 /*-----------------------------------------------------------------
80 * We're not really concerned with the str hash function being
81 * fast since it's only used by the ioctl interface.
82 *---------------------------------------------------------------*/
83 static unsigned int hash_str(const char *str
)
85 const unsigned int hash_mult
= 2654435387U;
89 h
= (h
+ (unsigned int) *str
++) * hash_mult
;
91 return h
& MASK_BUCKETS
;
94 /*-----------------------------------------------------------------
95 * Code for looking up a device by name
96 *---------------------------------------------------------------*/
97 static struct hash_cell
*__get_name_cell(const char *str
)
100 unsigned int h
= hash_str(str
);
102 list_for_each_entry (hc
, _name_buckets
+ h
, name_list
)
103 if (!strcmp(hc
->name
, str
)) {
111 static struct hash_cell
*__get_uuid_cell(const char *str
)
113 struct hash_cell
*hc
;
114 unsigned int h
= hash_str(str
);
116 list_for_each_entry (hc
, _uuid_buckets
+ h
, uuid_list
)
117 if (!strcmp(hc
->uuid
, str
)) {
125 /*-----------------------------------------------------------------
126 * Inserting, removing and renaming a device.
127 *---------------------------------------------------------------*/
128 static struct hash_cell
*alloc_cell(const char *name
, const char *uuid
,
129 struct mapped_device
*md
)
131 struct hash_cell
*hc
;
133 hc
= kmalloc(sizeof(*hc
), GFP_KERNEL
);
137 hc
->name
= kstrdup(name
, GFP_KERNEL
);
147 hc
->uuid
= kstrdup(uuid
, GFP_KERNEL
);
155 INIT_LIST_HEAD(&hc
->name_list
);
156 INIT_LIST_HEAD(&hc
->uuid_list
);
162 static void free_cell(struct hash_cell
*hc
)
172 * The kdev_t and uuid of a device can never change once it is
173 * initially inserted.
175 static int dm_hash_insert(const char *name
, const char *uuid
, struct mapped_device
*md
)
177 struct hash_cell
*cell
, *hc
;
180 * Allocate the new cells.
182 cell
= alloc_cell(name
, uuid
, md
);
187 * Insert the cell into both hash tables.
189 down_write(&_hash_lock
);
190 hc
= __get_name_cell(name
);
196 list_add(&cell
->name_list
, _name_buckets
+ hash_str(name
));
199 hc
= __get_uuid_cell(uuid
);
201 list_del(&cell
->name_list
);
205 list_add(&cell
->uuid_list
, _uuid_buckets
+ hash_str(uuid
));
208 dm_set_mdptr(md
, cell
);
209 up_write(&_hash_lock
);
214 up_write(&_hash_lock
);
219 static void __hash_remove(struct hash_cell
*hc
)
221 struct dm_table
*table
;
223 /* remove from the dev hash */
224 list_del(&hc
->uuid_list
);
225 list_del(&hc
->name_list
);
226 dm_set_mdptr(hc
->md
, NULL
);
228 table
= dm_get_table(hc
->md
);
230 dm_table_event(table
);
235 dm_table_put(hc
->new_map
);
240 static void dm_hash_remove_all(int keep_open_devices
)
242 int i
, dev_skipped
, dev_removed
;
243 struct hash_cell
*hc
;
244 struct list_head
*tmp
, *n
;
246 down_write(&_hash_lock
);
249 dev_skipped
= dev_removed
= 0;
250 for (i
= 0; i
< NUM_BUCKETS
; i
++) {
251 list_for_each_safe (tmp
, n
, _name_buckets
+ i
) {
252 hc
= list_entry(tmp
, struct hash_cell
, name_list
);
254 if (keep_open_devices
&&
255 dm_lock_for_deletion(hc
->md
)) {
265 * Some mapped devices may be using other mapped devices, so if any
266 * still exist, repeat until we make no further progress.
272 DMWARN("remove_all left %d open device(s)", dev_skipped
);
275 up_write(&_hash_lock
);
278 static int dm_hash_rename(const char *old
, const char *new)
280 char *new_name
, *old_name
;
281 struct hash_cell
*hc
;
282 struct dm_table
*table
;
287 new_name
= kstrdup(new, GFP_KERNEL
);
291 down_write(&_hash_lock
);
296 hc
= __get_name_cell(new);
298 DMWARN("asked to rename to an already existing name %s -> %s",
301 up_write(&_hash_lock
);
307 * Is there such a device as 'old' ?
309 hc
= __get_name_cell(old
);
311 DMWARN("asked to rename a non existent device %s -> %s",
313 up_write(&_hash_lock
);
319 * rename and move the name cell.
321 list_del(&hc
->name_list
);
324 list_add(&hc
->name_list
, _name_buckets
+ hash_str(new_name
));
327 * Wake up any dm event waiters.
329 table
= dm_get_table(hc
->md
);
331 dm_table_event(table
);
336 up_write(&_hash_lock
);
341 /*-----------------------------------------------------------------
342 * Implementation of the ioctl commands
343 *---------------------------------------------------------------*/
345 * All the ioctl commands get dispatched to functions with this
348 typedef int (*ioctl_fn
)(struct dm_ioctl
*param
, size_t param_size
);
350 static int remove_all(struct dm_ioctl
*param
, size_t param_size
)
352 dm_hash_remove_all(1);
353 param
->data_size
= 0;
358 * Round up the ptr to an 8-byte boundary.
361 static inline void *align_ptr(void *ptr
)
363 return (void *) (((size_t) (ptr
+ ALIGN_MASK
)) & ~ALIGN_MASK
);
367 * Retrieves the data payload buffer from an already allocated
370 static void *get_result_buffer(struct dm_ioctl
*param
, size_t param_size
,
373 param
->data_start
= align_ptr(param
+ 1) - (void *) param
;
375 if (param
->data_start
< param_size
)
376 *len
= param_size
- param
->data_start
;
380 return ((void *) param
) + param
->data_start
;
383 static int list_devices(struct dm_ioctl
*param
, size_t param_size
)
386 struct hash_cell
*hc
;
387 size_t len
, needed
= 0;
388 struct gendisk
*disk
;
389 struct dm_name_list
*nl
, *old_nl
= NULL
;
391 down_write(&_hash_lock
);
394 * Loop through all the devices working out how much
397 for (i
= 0; i
< NUM_BUCKETS
; i
++) {
398 list_for_each_entry (hc
, _name_buckets
+ i
, name_list
) {
399 needed
+= sizeof(struct dm_name_list
);
400 needed
+= strlen(hc
->name
) + 1;
401 needed
+= ALIGN_MASK
;
406 * Grab our output buffer.
408 nl
= get_result_buffer(param
, param_size
, &len
);
410 param
->flags
|= DM_BUFFER_FULL_FLAG
;
413 param
->data_size
= param
->data_start
+ needed
;
415 nl
->dev
= 0; /* Flags no data */
418 * Now loop through filling out the names.
420 for (i
= 0; i
< NUM_BUCKETS
; i
++) {
421 list_for_each_entry (hc
, _name_buckets
+ i
, name_list
) {
423 old_nl
->next
= (uint32_t) ((void *) nl
-
425 disk
= dm_disk(hc
->md
);
426 nl
->dev
= huge_encode_dev(MKDEV(disk
->major
, disk
->first_minor
));
428 strcpy(nl
->name
, hc
->name
);
431 nl
= align_ptr(((void *) ++nl
) + strlen(hc
->name
) + 1);
436 up_write(&_hash_lock
);
440 static void list_version_get_needed(struct target_type
*tt
, void *needed_param
)
442 size_t *needed
= needed_param
;
444 *needed
+= sizeof(struct dm_target_versions
);
445 *needed
+= strlen(tt
->name
);
446 *needed
+= ALIGN_MASK
;
449 static void list_version_get_info(struct target_type
*tt
, void *param
)
451 struct vers_iter
*info
= param
;
453 /* Check space - it might have changed since the first iteration */
454 if ((char *)info
->vers
+ sizeof(tt
->version
) + strlen(tt
->name
) + 1 >
457 info
->flags
= DM_BUFFER_FULL_FLAG
;
462 info
->old_vers
->next
= (uint32_t) ((void *)info
->vers
-
463 (void *)info
->old_vers
);
464 info
->vers
->version
[0] = tt
->version
[0];
465 info
->vers
->version
[1] = tt
->version
[1];
466 info
->vers
->version
[2] = tt
->version
[2];
467 info
->vers
->next
= 0;
468 strcpy(info
->vers
->name
, tt
->name
);
470 info
->old_vers
= info
->vers
;
471 info
->vers
= align_ptr(((void *) ++info
->vers
) + strlen(tt
->name
) + 1);
474 static int list_versions(struct dm_ioctl
*param
, size_t param_size
)
476 size_t len
, needed
= 0;
477 struct dm_target_versions
*vers
;
478 struct vers_iter iter_info
;
481 * Loop through all the devices working out how much
484 dm_target_iterate(list_version_get_needed
, &needed
);
487 * Grab our output buffer.
489 vers
= get_result_buffer(param
, param_size
, &len
);
491 param
->flags
|= DM_BUFFER_FULL_FLAG
;
494 param
->data_size
= param
->data_start
+ needed
;
496 iter_info
.param_size
= param_size
;
497 iter_info
.old_vers
= NULL
;
498 iter_info
.vers
= vers
;
500 iter_info
.end
= (char *)vers
+len
;
503 * Now loop through filling out the names & versions.
505 dm_target_iterate(list_version_get_info
, &iter_info
);
506 param
->flags
|= iter_info
.flags
;
514 static int check_name(const char *name
)
516 if (strchr(name
, '/')) {
517 DMWARN("invalid device name");
525 * Fills in a dm_ioctl structure, ready for sending back to
528 static int __dev_status(struct mapped_device
*md
, struct dm_ioctl
*param
)
530 struct gendisk
*disk
= dm_disk(md
);
531 struct dm_table
*table
;
533 param
->flags
&= ~(DM_SUSPEND_FLAG
| DM_READONLY_FLAG
|
534 DM_ACTIVE_PRESENT_FLAG
);
536 if (dm_suspended(md
))
537 param
->flags
|= DM_SUSPEND_FLAG
;
539 param
->dev
= huge_encode_dev(MKDEV(disk
->major
, disk
->first_minor
));
542 * Yes, this will be out of date by the time it gets back
543 * to userland, but it is still very useful for
546 param
->open_count
= dm_open_count(md
);
549 param
->flags
|= DM_READONLY_FLAG
;
551 param
->event_nr
= dm_get_event_nr(md
);
553 table
= dm_get_table(md
);
555 param
->flags
|= DM_ACTIVE_PRESENT_FLAG
;
556 param
->target_count
= dm_table_get_num_targets(table
);
559 param
->target_count
= 0;
564 static int dev_create(struct dm_ioctl
*param
, size_t param_size
)
566 int r
, m
= DM_ANY_MINOR
;
567 struct mapped_device
*md
;
569 r
= check_name(param
->name
);
573 if (param
->flags
& DM_PERSISTENT_DEV_FLAG
)
574 m
= MINOR(huge_decode_dev(param
->dev
));
576 r
= dm_create(m
, &md
);
580 r
= dm_hash_insert(param
->name
, *param
->uuid
? param
->uuid
: NULL
, md
);
586 param
->flags
&= ~DM_INACTIVE_PRESENT_FLAG
;
588 r
= __dev_status(md
, param
);
595 * Always use UUID for lookups if it's present, otherwise use name or dev.
597 static struct hash_cell
*__find_device_hash_cell(struct dm_ioctl
*param
)
599 struct mapped_device
*md
;
603 return __get_uuid_cell(param
->uuid
);
606 return __get_name_cell(param
->name
);
608 md
= dm_get_md(huge_decode_dev(param
->dev
));
612 mdptr
= dm_get_mdptr(md
);
620 static struct mapped_device
*find_device(struct dm_ioctl
*param
)
622 struct hash_cell
*hc
;
623 struct mapped_device
*md
= NULL
;
625 down_read(&_hash_lock
);
626 hc
= __find_device_hash_cell(param
);
631 * Sneakily write in both the name and the uuid
632 * while we have the cell.
634 strncpy(param
->name
, hc
->name
, sizeof(param
->name
));
636 strncpy(param
->uuid
, hc
->uuid
, sizeof(param
->uuid
)-1);
638 param
->uuid
[0] = '\0';
641 param
->flags
|= DM_INACTIVE_PRESENT_FLAG
;
643 param
->flags
&= ~DM_INACTIVE_PRESENT_FLAG
;
645 up_read(&_hash_lock
);
650 static int dev_remove(struct dm_ioctl
*param
, size_t param_size
)
652 struct hash_cell
*hc
;
653 struct mapped_device
*md
;
656 down_write(&_hash_lock
);
657 hc
= __find_device_hash_cell(param
);
660 DMWARN("device doesn't appear to be in the dev hash table.");
661 up_write(&_hash_lock
);
668 * Ensure the device is not open and nothing further can open it.
670 r
= dm_lock_for_deletion(md
);
672 DMWARN("unable to remove open device %s", hc
->name
);
673 up_write(&_hash_lock
);
679 up_write(&_hash_lock
);
681 param
->data_size
= 0;
686 * Check a string doesn't overrun the chunk of
687 * memory we copied from userland.
689 static int invalid_str(char *str
, void *end
)
691 while ((void *) str
< end
)
698 static int dev_rename(struct dm_ioctl
*param
, size_t param_size
)
701 char *new_name
= (char *) param
+ param
->data_start
;
703 if (new_name
< (char *) (param
+ 1) ||
704 invalid_str(new_name
, (void *) param
+ param_size
)) {
705 DMWARN("Invalid new logical volume name supplied.");
709 r
= check_name(new_name
);
713 param
->data_size
= 0;
714 return dm_hash_rename(param
->name
, new_name
);
717 static int dev_set_geometry(struct dm_ioctl
*param
, size_t param_size
)
720 struct mapped_device
*md
;
721 struct hd_geometry geometry
;
722 unsigned long indata
[4];
723 char *geostr
= (char *) param
+ param
->data_start
;
725 md
= find_device(param
);
729 if (geostr
< (char *) (param
+ 1) ||
730 invalid_str(geostr
, (void *) param
+ param_size
)) {
731 DMWARN("Invalid geometry supplied.");
735 x
= sscanf(geostr
, "%lu %lu %lu %lu", indata
,
736 indata
+ 1, indata
+ 2, indata
+ 3);
739 DMWARN("Unable to interpret geometry settings.");
743 if (indata
[0] > 65535 || indata
[1] > 255 ||
744 indata
[2] > 255 || indata
[3] > ULONG_MAX
) {
745 DMWARN("Geometry exceeds range limits.");
749 geometry
.cylinders
= indata
[0];
750 geometry
.heads
= indata
[1];
751 geometry
.sectors
= indata
[2];
752 geometry
.start
= indata
[3];
754 r
= dm_set_geometry(md
, &geometry
);
756 r
= __dev_status(md
, param
);
758 param
->data_size
= 0;
765 static int do_suspend(struct dm_ioctl
*param
)
768 unsigned suspend_flags
= DM_SUSPEND_LOCKFS_FLAG
;
769 struct mapped_device
*md
;
771 md
= find_device(param
);
775 if (param
->flags
& DM_SKIP_LOCKFS_FLAG
)
776 suspend_flags
&= ~DM_SUSPEND_LOCKFS_FLAG
;
777 if (param
->flags
& DM_NOFLUSH_FLAG
)
778 suspend_flags
|= DM_SUSPEND_NOFLUSH_FLAG
;
780 if (!dm_suspended(md
))
781 r
= dm_suspend(md
, suspend_flags
);
784 r
= __dev_status(md
, param
);
790 static int do_resume(struct dm_ioctl
*param
)
793 unsigned suspend_flags
= DM_SUSPEND_LOCKFS_FLAG
;
794 struct hash_cell
*hc
;
795 struct mapped_device
*md
;
796 struct dm_table
*new_map
;
798 down_write(&_hash_lock
);
800 hc
= __find_device_hash_cell(param
);
802 DMWARN("device doesn't appear to be in the dev hash table.");
803 up_write(&_hash_lock
);
809 new_map
= hc
->new_map
;
811 param
->flags
&= ~DM_INACTIVE_PRESENT_FLAG
;
813 up_write(&_hash_lock
);
815 /* Do we need to load a new map ? */
817 /* Suspend if it isn't already suspended */
818 if (param
->flags
& DM_SKIP_LOCKFS_FLAG
)
819 suspend_flags
&= ~DM_SUSPEND_LOCKFS_FLAG
;
820 if (param
->flags
& DM_NOFLUSH_FLAG
)
821 suspend_flags
|= DM_SUSPEND_NOFLUSH_FLAG
;
822 if (!dm_suspended(md
))
823 dm_suspend(md
, suspend_flags
);
825 r
= dm_swap_table(md
, new_map
);
828 dm_table_put(new_map
);
832 if (dm_table_get_mode(new_map
) & FMODE_WRITE
)
833 set_disk_ro(dm_disk(md
), 0);
835 set_disk_ro(dm_disk(md
), 1);
837 dm_table_put(new_map
);
840 if (dm_suspended(md
))
844 r
= __dev_status(md
, param
);
851 * Set or unset the suspension state of a device.
852 * If the device already is in the requested state we just return its status.
854 static int dev_suspend(struct dm_ioctl
*param
, size_t param_size
)
856 if (param
->flags
& DM_SUSPEND_FLAG
)
857 return do_suspend(param
);
859 return do_resume(param
);
863 * Copies device info back to user space, used by
864 * the create and info ioctls.
866 static int dev_status(struct dm_ioctl
*param
, size_t param_size
)
869 struct mapped_device
*md
;
871 md
= find_device(param
);
875 r
= __dev_status(md
, param
);
881 * Build up the status struct for each target
883 static void retrieve_status(struct dm_table
*table
,
884 struct dm_ioctl
*param
, size_t param_size
)
886 unsigned int i
, num_targets
;
887 struct dm_target_spec
*spec
;
888 char *outbuf
, *outptr
;
890 size_t remaining
, len
, used
= 0;
892 outptr
= outbuf
= get_result_buffer(param
, param_size
, &len
);
894 if (param
->flags
& DM_STATUS_TABLE_FLAG
)
895 type
= STATUSTYPE_TABLE
;
897 type
= STATUSTYPE_INFO
;
899 /* Get all the target info */
900 num_targets
= dm_table_get_num_targets(table
);
901 for (i
= 0; i
< num_targets
; i
++) {
902 struct dm_target
*ti
= dm_table_get_target(table
, i
);
904 remaining
= len
- (outptr
- outbuf
);
905 if (remaining
<= sizeof(struct dm_target_spec
)) {
906 param
->flags
|= DM_BUFFER_FULL_FLAG
;
910 spec
= (struct dm_target_spec
*) outptr
;
913 spec
->sector_start
= ti
->begin
;
914 spec
->length
= ti
->len
;
915 strncpy(spec
->target_type
, ti
->type
->name
,
916 sizeof(spec
->target_type
));
918 outptr
+= sizeof(struct dm_target_spec
);
919 remaining
= len
- (outptr
- outbuf
);
920 if (remaining
<= 0) {
921 param
->flags
|= DM_BUFFER_FULL_FLAG
;
925 /* Get the status/table string from the target driver */
926 if (ti
->type
->status
) {
927 if (ti
->type
->status(ti
, type
, outptr
, remaining
)) {
928 param
->flags
|= DM_BUFFER_FULL_FLAG
;
934 outptr
+= strlen(outptr
) + 1;
935 used
= param
->data_start
+ (outptr
- outbuf
);
937 outptr
= align_ptr(outptr
);
938 spec
->next
= outptr
- outbuf
;
942 param
->data_size
= used
;
944 param
->target_count
= num_targets
;
948 * Wait for a device to report an event
950 static int dev_wait(struct dm_ioctl
*param
, size_t param_size
)
953 struct mapped_device
*md
;
954 struct dm_table
*table
;
956 md
= find_device(param
);
961 * Wait for a notification event
963 if (dm_wait_event(md
, param
->event_nr
)) {
969 * The userland program is going to want to know what
970 * changed to trigger the event, so we may as well tell
971 * him and save an ioctl.
973 r
= __dev_status(md
, param
);
977 table
= dm_get_table(md
);
979 retrieve_status(table
, param
, param_size
);
988 static inline int get_mode(struct dm_ioctl
*param
)
990 int mode
= FMODE_READ
| FMODE_WRITE
;
992 if (param
->flags
& DM_READONLY_FLAG
)
998 static int next_target(struct dm_target_spec
*last
, uint32_t next
, void *end
,
999 struct dm_target_spec
**spec
, char **target_params
)
1001 *spec
= (struct dm_target_spec
*) ((unsigned char *) last
+ next
);
1002 *target_params
= (char *) (*spec
+ 1);
1004 if (*spec
< (last
+ 1))
1007 return invalid_str(*target_params
, end
);
1010 static int populate_table(struct dm_table
*table
,
1011 struct dm_ioctl
*param
, size_t param_size
)
1015 struct dm_target_spec
*spec
= (struct dm_target_spec
*) param
;
1016 uint32_t next
= param
->data_start
;
1017 void *end
= (void *) param
+ param_size
;
1018 char *target_params
;
1020 if (!param
->target_count
) {
1021 DMWARN("populate_table: no targets specified");
1025 for (i
= 0; i
< param
->target_count
; i
++) {
1027 r
= next_target(spec
, next
, end
, &spec
, &target_params
);
1029 DMWARN("unable to find target");
1033 r
= dm_table_add_target(table
, spec
->target_type
,
1034 (sector_t
) spec
->sector_start
,
1035 (sector_t
) spec
->length
,
1038 DMWARN("error adding target to table");
1045 return dm_table_complete(table
);
1048 static int table_load(struct dm_ioctl
*param
, size_t param_size
)
1051 struct hash_cell
*hc
;
1053 struct mapped_device
*md
;
1055 md
= find_device(param
);
1059 r
= dm_table_create(&t
, get_mode(param
), param
->target_count
, md
);
1063 r
= populate_table(t
, param
, param_size
);
1069 down_write(&_hash_lock
);
1070 hc
= dm_get_mdptr(md
);
1071 if (!hc
|| hc
->md
!= md
) {
1072 DMWARN("device has been removed from the dev hash table.");
1074 up_write(&_hash_lock
);
1080 dm_table_put(hc
->new_map
);
1082 up_write(&_hash_lock
);
1084 param
->flags
|= DM_INACTIVE_PRESENT_FLAG
;
1085 r
= __dev_status(md
, param
);
1093 static int table_clear(struct dm_ioctl
*param
, size_t param_size
)
1096 struct hash_cell
*hc
;
1097 struct mapped_device
*md
;
1099 down_write(&_hash_lock
);
1101 hc
= __find_device_hash_cell(param
);
1103 DMWARN("device doesn't appear to be in the dev hash table.");
1104 up_write(&_hash_lock
);
1109 dm_table_put(hc
->new_map
);
1113 param
->flags
&= ~DM_INACTIVE_PRESENT_FLAG
;
1115 r
= __dev_status(hc
->md
, param
);
1117 up_write(&_hash_lock
);
1123 * Retrieves a list of devices used by a particular dm device.
1125 static void retrieve_deps(struct dm_table
*table
,
1126 struct dm_ioctl
*param
, size_t param_size
)
1128 unsigned int count
= 0;
1129 struct list_head
*tmp
;
1132 struct dm_target_deps
*deps
;
1134 deps
= get_result_buffer(param
, param_size
, &len
);
1137 * Count the devices.
1139 list_for_each (tmp
, dm_table_get_devices(table
))
1143 * Check we have enough space.
1145 needed
= sizeof(*deps
) + (sizeof(*deps
->dev
) * count
);
1147 param
->flags
|= DM_BUFFER_FULL_FLAG
;
1152 * Fill in the devices.
1154 deps
->count
= count
;
1156 list_for_each_entry (dd
, dm_table_get_devices(table
), list
)
1157 deps
->dev
[count
++] = huge_encode_dev(dd
->bdev
->bd_dev
);
1159 param
->data_size
= param
->data_start
+ needed
;
1162 static int table_deps(struct dm_ioctl
*param
, size_t param_size
)
1165 struct mapped_device
*md
;
1166 struct dm_table
*table
;
1168 md
= find_device(param
);
1172 r
= __dev_status(md
, param
);
1176 table
= dm_get_table(md
);
1178 retrieve_deps(table
, param
, param_size
);
1179 dm_table_put(table
);
1188 * Return the status of a device as a text string for each
1191 static int table_status(struct dm_ioctl
*param
, size_t param_size
)
1194 struct mapped_device
*md
;
1195 struct dm_table
*table
;
1197 md
= find_device(param
);
1201 r
= __dev_status(md
, param
);
1205 table
= dm_get_table(md
);
1207 retrieve_status(table
, param
, param_size
);
1208 dm_table_put(table
);
1217 * Pass a message to the target that's at the supplied device offset.
1219 static int target_message(struct dm_ioctl
*param
, size_t param_size
)
1223 struct mapped_device
*md
;
1224 struct dm_table
*table
;
1225 struct dm_target
*ti
;
1226 struct dm_target_msg
*tmsg
= (void *) param
+ param
->data_start
;
1228 md
= find_device(param
);
1232 r
= __dev_status(md
, param
);
1236 if (tmsg
< (struct dm_target_msg
*) (param
+ 1) ||
1237 invalid_str(tmsg
->message
, (void *) param
+ param_size
)) {
1238 DMWARN("Invalid target message parameters.");
1243 r
= dm_split_args(&argc
, &argv
, tmsg
->message
);
1245 DMWARN("Failed to split target message parameters");
1249 table
= dm_get_table(md
);
1253 if (tmsg
->sector
>= dm_table_get_size(table
)) {
1254 DMWARN("Target message sector outside device.");
1259 ti
= dm_table_find_target(table
, tmsg
->sector
);
1260 if (ti
->type
->message
)
1261 r
= ti
->type
->message(ti
, argc
, argv
);
1263 DMWARN("Target type does not support messages");
1268 dm_table_put(table
);
1272 param
->data_size
= 0;
1277 /*-----------------------------------------------------------------
1278 * Implementation of open/close/ioctl on the special char
1280 *---------------------------------------------------------------*/
1281 static ioctl_fn
lookup_ioctl(unsigned int cmd
)
1287 {DM_VERSION_CMD
, NULL
}, /* version is dealt with elsewhere */
1288 {DM_REMOVE_ALL_CMD
, remove_all
},
1289 {DM_LIST_DEVICES_CMD
, list_devices
},
1291 {DM_DEV_CREATE_CMD
, dev_create
},
1292 {DM_DEV_REMOVE_CMD
, dev_remove
},
1293 {DM_DEV_RENAME_CMD
, dev_rename
},
1294 {DM_DEV_SUSPEND_CMD
, dev_suspend
},
1295 {DM_DEV_STATUS_CMD
, dev_status
},
1296 {DM_DEV_WAIT_CMD
, dev_wait
},
1298 {DM_TABLE_LOAD_CMD
, table_load
},
1299 {DM_TABLE_CLEAR_CMD
, table_clear
},
1300 {DM_TABLE_DEPS_CMD
, table_deps
},
1301 {DM_TABLE_STATUS_CMD
, table_status
},
1303 {DM_LIST_VERSIONS_CMD
, list_versions
},
1305 {DM_TARGET_MSG_CMD
, target_message
},
1306 {DM_DEV_SET_GEOMETRY_CMD
, dev_set_geometry
}
1309 return (cmd
>= ARRAY_SIZE(_ioctls
)) ? NULL
: _ioctls
[cmd
].fn
;
1313 * As well as checking the version compatibility this always
1314 * copies the kernel interface version out.
1316 static int check_version(unsigned int cmd
, struct dm_ioctl __user
*user
)
1318 uint32_t version
[3];
1321 if (copy_from_user(version
, user
->version
, sizeof(version
)))
1324 if ((DM_VERSION_MAJOR
!= version
[0]) ||
1325 (DM_VERSION_MINOR
< version
[1])) {
1326 DMWARN("ioctl interface mismatch: "
1327 "kernel(%u.%u.%u), user(%u.%u.%u), cmd(%d)",
1328 DM_VERSION_MAJOR
, DM_VERSION_MINOR
,
1329 DM_VERSION_PATCHLEVEL
,
1330 version
[0], version
[1], version
[2], cmd
);
1335 * Fill in the kernel version.
1337 version
[0] = DM_VERSION_MAJOR
;
1338 version
[1] = DM_VERSION_MINOR
;
1339 version
[2] = DM_VERSION_PATCHLEVEL
;
1340 if (copy_to_user(user
->version
, version
, sizeof(version
)))
1346 static void free_params(struct dm_ioctl
*param
)
1351 static int copy_params(struct dm_ioctl __user
*user
, struct dm_ioctl
**param
)
1353 struct dm_ioctl tmp
, *dmi
;
1355 if (copy_from_user(&tmp
, user
, sizeof(tmp
)))
1358 if (tmp
.data_size
< sizeof(tmp
))
1361 dmi
= (struct dm_ioctl
*) vmalloc(tmp
.data_size
);
1365 if (copy_from_user(dmi
, user
, tmp
.data_size
)) {
1374 static int validate_params(uint cmd
, struct dm_ioctl
*param
)
1376 /* Always clear this flag */
1377 param
->flags
&= ~DM_BUFFER_FULL_FLAG
;
1379 /* Ignores parameters */
1380 if (cmd
== DM_REMOVE_ALL_CMD
||
1381 cmd
== DM_LIST_DEVICES_CMD
||
1382 cmd
== DM_LIST_VERSIONS_CMD
)
1385 if ((cmd
== DM_DEV_CREATE_CMD
)) {
1386 if (!*param
->name
) {
1387 DMWARN("name not supplied when creating device");
1390 } else if ((*param
->uuid
&& *param
->name
)) {
1391 DMWARN("only supply one of name or uuid, cmd(%u)", cmd
);
1395 /* Ensure strings are terminated */
1396 param
->name
[DM_NAME_LEN
- 1] = '\0';
1397 param
->uuid
[DM_UUID_LEN
- 1] = '\0';
1402 static int ctl_ioctl(struct inode
*inode
, struct file
*file
,
1403 uint command
, ulong u
)
1407 struct dm_ioctl
*param
;
1408 struct dm_ioctl __user
*user
= (struct dm_ioctl __user
*) u
;
1412 /* only root can play with this */
1413 if (!capable(CAP_SYS_ADMIN
))
1416 if (_IOC_TYPE(command
) != DM_IOCTL
)
1419 cmd
= _IOC_NR(command
);
1422 * Check the interface version passed in. This also
1423 * writes out the kernel's interface version.
1425 r
= check_version(cmd
, user
);
1430 * Nothing more to do for the version command.
1432 if (cmd
== DM_VERSION_CMD
)
1435 fn
= lookup_ioctl(cmd
);
1437 DMWARN("dm_ctl_ioctl: unknown command 0x%x", command
);
1442 * Trying to avoid low memory issues when a device is
1445 current
->flags
|= PF_MEMALLOC
;
1448 * Copy the parameters into kernel space.
1450 r
= copy_params(user
, ¶m
);
1452 current
->flags
&= ~PF_MEMALLOC
;
1457 r
= validate_params(cmd
, param
);
1461 param_size
= param
->data_size
;
1462 param
->data_size
= sizeof(*param
);
1463 r
= fn(param
, param_size
);
1466 * Copy the results back to userland.
1468 if (!r
&& copy_to_user(user
, param
, param
->data_size
))
1476 static const struct file_operations _ctl_fops
= {
1478 .owner
= THIS_MODULE
,
1481 static struct miscdevice _dm_misc
= {
1482 .minor
= MISC_DYNAMIC_MINOR
,
1488 * Create misc character device and link to DM_DIR/control.
1490 int __init
dm_interface_init(void)
1498 r
= misc_register(&_dm_misc
);
1500 DMERR("misc_register failed for control device");
1505 DMINFO("%d.%d.%d%s initialised: %s", DM_VERSION_MAJOR
,
1506 DM_VERSION_MINOR
, DM_VERSION_PATCHLEVEL
, DM_VERSION_EXTRA
,
1511 void dm_interface_exit(void)
1513 if (misc_deregister(&_dm_misc
) < 0)
1514 DMERR("misc_deregister failed for control device");