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/devfs_fs_kernel.h>
17 #include <linux/dm-ioctl.h>
18 #include <linux/hdreg.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
);
59 static void init_buckets(struct list_head
*buckets
)
63 for (i
= 0; i
< NUM_BUCKETS
; i
++)
64 INIT_LIST_HEAD(buckets
+ i
);
67 static int dm_hash_init(void)
69 init_buckets(_name_buckets
);
70 init_buckets(_uuid_buckets
);
75 static void dm_hash_exit(void)
77 dm_hash_remove_all(0);
81 /*-----------------------------------------------------------------
83 * We're not really concerned with the str hash function being
84 * fast since it's only used by the ioctl interface.
85 *---------------------------------------------------------------*/
86 static unsigned int hash_str(const char *str
)
88 const unsigned int hash_mult
= 2654435387U;
92 h
= (h
+ (unsigned int) *str
++) * hash_mult
;
94 return h
& MASK_BUCKETS
;
97 /*-----------------------------------------------------------------
98 * Code for looking up a device by name
99 *---------------------------------------------------------------*/
100 static struct hash_cell
*__get_name_cell(const char *str
)
102 struct hash_cell
*hc
;
103 unsigned int h
= hash_str(str
);
105 list_for_each_entry (hc
, _name_buckets
+ h
, name_list
)
106 if (!strcmp(hc
->name
, str
)) {
114 static struct hash_cell
*__get_uuid_cell(const char *str
)
116 struct hash_cell
*hc
;
117 unsigned int h
= hash_str(str
);
119 list_for_each_entry (hc
, _uuid_buckets
+ h
, uuid_list
)
120 if (!strcmp(hc
->uuid
, str
)) {
128 /*-----------------------------------------------------------------
129 * Inserting, removing and renaming a device.
130 *---------------------------------------------------------------*/
131 static struct hash_cell
*alloc_cell(const char *name
, const char *uuid
,
132 struct mapped_device
*md
)
134 struct hash_cell
*hc
;
136 hc
= kmalloc(sizeof(*hc
), GFP_KERNEL
);
140 hc
->name
= kstrdup(name
, GFP_KERNEL
);
150 hc
->uuid
= kstrdup(uuid
, GFP_KERNEL
);
158 INIT_LIST_HEAD(&hc
->name_list
);
159 INIT_LIST_HEAD(&hc
->uuid_list
);
165 static void free_cell(struct hash_cell
*hc
)
177 static int register_with_devfs(struct hash_cell
*hc
)
179 struct gendisk
*disk
= dm_disk(hc
->md
);
181 devfs_mk_bdev(MKDEV(disk
->major
, disk
->first_minor
),
182 S_IFBLK
| S_IRUSR
| S_IWUSR
| S_IRGRP
,
183 DM_DIR
"/%s", hc
->name
);
187 static int unregister_with_devfs(struct hash_cell
*hc
)
189 devfs_remove(DM_DIR
"/%s", hc
->name
);
194 * The kdev_t and uuid of a device can never change once it is
195 * initially inserted.
197 static int dm_hash_insert(const char *name
, const char *uuid
, struct mapped_device
*md
)
199 struct hash_cell
*cell
, *hc
;
202 * Allocate the new cells.
204 cell
= alloc_cell(name
, uuid
, md
);
209 * Insert the cell into both hash tables.
211 down_write(&_hash_lock
);
212 hc
= __get_name_cell(name
);
218 list_add(&cell
->name_list
, _name_buckets
+ hash_str(name
));
221 hc
= __get_uuid_cell(uuid
);
223 list_del(&cell
->name_list
);
227 list_add(&cell
->uuid_list
, _uuid_buckets
+ hash_str(uuid
));
229 register_with_devfs(cell
);
231 dm_set_mdptr(md
, cell
);
232 up_write(&_hash_lock
);
237 up_write(&_hash_lock
);
242 static void __hash_remove(struct hash_cell
*hc
)
244 struct dm_table
*table
;
246 /* remove from the dev hash */
247 list_del(&hc
->uuid_list
);
248 list_del(&hc
->name_list
);
249 unregister_with_devfs(hc
);
250 dm_set_mdptr(hc
->md
, NULL
);
252 table
= dm_get_table(hc
->md
);
254 dm_table_event(table
);
259 dm_table_put(hc
->new_map
);
264 static void dm_hash_remove_all(int keep_open_devices
)
266 int i
, dev_skipped
, dev_removed
;
267 struct hash_cell
*hc
;
268 struct list_head
*tmp
, *n
;
270 down_write(&_hash_lock
);
273 dev_skipped
= dev_removed
= 0;
274 for (i
= 0; i
< NUM_BUCKETS
; i
++) {
275 list_for_each_safe (tmp
, n
, _name_buckets
+ i
) {
276 hc
= list_entry(tmp
, struct hash_cell
, name_list
);
278 if (keep_open_devices
&&
279 dm_lock_for_deletion(hc
->md
)) {
289 * Some mapped devices may be using other mapped devices, so if any
290 * still exist, repeat until we make no further progress.
296 DMWARN("remove_all left %d open device(s)", dev_skipped
);
299 up_write(&_hash_lock
);
302 static int dm_hash_rename(const char *old
, const char *new)
304 char *new_name
, *old_name
;
305 struct hash_cell
*hc
;
306 struct dm_table
*table
;
311 new_name
= kstrdup(new, GFP_KERNEL
);
315 down_write(&_hash_lock
);
320 hc
= __get_name_cell(new);
322 DMWARN("asked to rename to an already existing name %s -> %s",
325 up_write(&_hash_lock
);
331 * Is there such a device as 'old' ?
333 hc
= __get_name_cell(old
);
335 DMWARN("asked to rename a non existent device %s -> %s",
337 up_write(&_hash_lock
);
343 * rename and move the name cell.
345 unregister_with_devfs(hc
);
347 list_del(&hc
->name_list
);
350 list_add(&hc
->name_list
, _name_buckets
+ hash_str(new_name
));
352 /* rename the device node in devfs */
353 register_with_devfs(hc
);
356 * Wake up any dm event waiters.
358 table
= dm_get_table(hc
->md
);
360 dm_table_event(table
);
365 up_write(&_hash_lock
);
370 /*-----------------------------------------------------------------
371 * Implementation of the ioctl commands
372 *---------------------------------------------------------------*/
374 * All the ioctl commands get dispatched to functions with this
377 typedef int (*ioctl_fn
)(struct dm_ioctl
*param
, size_t param_size
);
379 static int remove_all(struct dm_ioctl
*param
, size_t param_size
)
381 dm_hash_remove_all(1);
382 param
->data_size
= 0;
387 * Round up the ptr to an 8-byte boundary.
390 static inline void *align_ptr(void *ptr
)
392 return (void *) (((size_t) (ptr
+ ALIGN_MASK
)) & ~ALIGN_MASK
);
396 * Retrieves the data payload buffer from an already allocated
399 static void *get_result_buffer(struct dm_ioctl
*param
, size_t param_size
,
402 param
->data_start
= align_ptr(param
+ 1) - (void *) param
;
404 if (param
->data_start
< param_size
)
405 *len
= param_size
- param
->data_start
;
409 return ((void *) param
) + param
->data_start
;
412 static int list_devices(struct dm_ioctl
*param
, size_t param_size
)
415 struct hash_cell
*hc
;
416 size_t len
, needed
= 0;
417 struct gendisk
*disk
;
418 struct dm_name_list
*nl
, *old_nl
= NULL
;
420 down_write(&_hash_lock
);
423 * Loop through all the devices working out how much
426 for (i
= 0; i
< NUM_BUCKETS
; i
++) {
427 list_for_each_entry (hc
, _name_buckets
+ i
, name_list
) {
428 needed
+= sizeof(struct dm_name_list
);
429 needed
+= strlen(hc
->name
) + 1;
430 needed
+= ALIGN_MASK
;
435 * Grab our output buffer.
437 nl
= get_result_buffer(param
, param_size
, &len
);
439 param
->flags
|= DM_BUFFER_FULL_FLAG
;
442 param
->data_size
= param
->data_start
+ needed
;
444 nl
->dev
= 0; /* Flags no data */
447 * Now loop through filling out the names.
449 for (i
= 0; i
< NUM_BUCKETS
; i
++) {
450 list_for_each_entry (hc
, _name_buckets
+ i
, name_list
) {
452 old_nl
->next
= (uint32_t) ((void *) nl
-
454 disk
= dm_disk(hc
->md
);
455 nl
->dev
= huge_encode_dev(MKDEV(disk
->major
, disk
->first_minor
));
457 strcpy(nl
->name
, hc
->name
);
460 nl
= align_ptr(((void *) ++nl
) + strlen(hc
->name
) + 1);
465 up_write(&_hash_lock
);
469 static void list_version_get_needed(struct target_type
*tt
, void *needed_param
)
471 size_t *needed
= needed_param
;
473 *needed
+= sizeof(struct dm_target_versions
);
474 *needed
+= strlen(tt
->name
);
475 *needed
+= ALIGN_MASK
;
478 static void list_version_get_info(struct target_type
*tt
, void *param
)
480 struct vers_iter
*info
= param
;
482 /* Check space - it might have changed since the first iteration */
483 if ((char *)info
->vers
+ sizeof(tt
->version
) + strlen(tt
->name
) + 1 >
486 info
->flags
= DM_BUFFER_FULL_FLAG
;
491 info
->old_vers
->next
= (uint32_t) ((void *)info
->vers
-
492 (void *)info
->old_vers
);
493 info
->vers
->version
[0] = tt
->version
[0];
494 info
->vers
->version
[1] = tt
->version
[1];
495 info
->vers
->version
[2] = tt
->version
[2];
496 info
->vers
->next
= 0;
497 strcpy(info
->vers
->name
, tt
->name
);
499 info
->old_vers
= info
->vers
;
500 info
->vers
= align_ptr(((void *) ++info
->vers
) + strlen(tt
->name
) + 1);
503 static int list_versions(struct dm_ioctl
*param
, size_t param_size
)
505 size_t len
, needed
= 0;
506 struct dm_target_versions
*vers
;
507 struct vers_iter iter_info
;
510 * Loop through all the devices working out how much
513 dm_target_iterate(list_version_get_needed
, &needed
);
516 * Grab our output buffer.
518 vers
= get_result_buffer(param
, param_size
, &len
);
520 param
->flags
|= DM_BUFFER_FULL_FLAG
;
523 param
->data_size
= param
->data_start
+ needed
;
525 iter_info
.param_size
= param_size
;
526 iter_info
.old_vers
= NULL
;
527 iter_info
.vers
= vers
;
529 iter_info
.end
= (char *)vers
+len
;
532 * Now loop through filling out the names & versions.
534 dm_target_iterate(list_version_get_info
, &iter_info
);
535 param
->flags
|= iter_info
.flags
;
543 static int check_name(const char *name
)
545 if (strchr(name
, '/')) {
546 DMWARN("invalid device name");
554 * Fills in a dm_ioctl structure, ready for sending back to
557 static int __dev_status(struct mapped_device
*md
, struct dm_ioctl
*param
)
559 struct gendisk
*disk
= dm_disk(md
);
560 struct dm_table
*table
;
562 param
->flags
&= ~(DM_SUSPEND_FLAG
| DM_READONLY_FLAG
|
563 DM_ACTIVE_PRESENT_FLAG
);
565 if (dm_suspended(md
))
566 param
->flags
|= DM_SUSPEND_FLAG
;
568 param
->dev
= huge_encode_dev(MKDEV(disk
->major
, disk
->first_minor
));
571 * Yes, this will be out of date by the time it gets back
572 * to userland, but it is still very useful for
575 param
->open_count
= dm_open_count(md
);
578 param
->flags
|= DM_READONLY_FLAG
;
580 param
->event_nr
= dm_get_event_nr(md
);
582 table
= dm_get_table(md
);
584 param
->flags
|= DM_ACTIVE_PRESENT_FLAG
;
585 param
->target_count
= dm_table_get_num_targets(table
);
588 param
->target_count
= 0;
593 static int dev_create(struct dm_ioctl
*param
, size_t param_size
)
595 int r
, m
= DM_ANY_MINOR
;
596 struct mapped_device
*md
;
598 r
= check_name(param
->name
);
602 if (param
->flags
& DM_PERSISTENT_DEV_FLAG
)
603 m
= MINOR(huge_decode_dev(param
->dev
));
605 r
= dm_create(m
, &md
);
609 r
= dm_hash_insert(param
->name
, *param
->uuid
? param
->uuid
: NULL
, md
);
615 param
->flags
&= ~DM_INACTIVE_PRESENT_FLAG
;
617 r
= __dev_status(md
, param
);
624 * Always use UUID for lookups if it's present, otherwise use name or dev.
626 static struct hash_cell
*__find_device_hash_cell(struct dm_ioctl
*param
)
628 struct mapped_device
*md
;
632 return __get_uuid_cell(param
->uuid
);
635 return __get_name_cell(param
->name
);
637 md
= dm_get_md(huge_decode_dev(param
->dev
));
639 mdptr
= dm_get_mdptr(md
);
644 static struct mapped_device
*find_device(struct dm_ioctl
*param
)
646 struct hash_cell
*hc
;
647 struct mapped_device
*md
= NULL
;
649 down_read(&_hash_lock
);
650 hc
= __find_device_hash_cell(param
);
655 * Sneakily write in both the name and the uuid
656 * while we have the cell.
658 strncpy(param
->name
, hc
->name
, sizeof(param
->name
));
660 strncpy(param
->uuid
, hc
->uuid
, sizeof(param
->uuid
)-1);
662 param
->uuid
[0] = '\0';
665 param
->flags
|= DM_INACTIVE_PRESENT_FLAG
;
667 param
->flags
&= ~DM_INACTIVE_PRESENT_FLAG
;
669 up_read(&_hash_lock
);
674 static int dev_remove(struct dm_ioctl
*param
, size_t param_size
)
676 struct hash_cell
*hc
;
677 struct mapped_device
*md
;
680 down_write(&_hash_lock
);
681 hc
= __find_device_hash_cell(param
);
684 DMWARN("device doesn't appear to be in the dev hash table.");
685 up_write(&_hash_lock
);
692 * Ensure the device is not open and nothing further can open it.
694 r
= dm_lock_for_deletion(md
);
696 DMWARN("unable to remove open device %s", hc
->name
);
697 up_write(&_hash_lock
);
703 up_write(&_hash_lock
);
705 param
->data_size
= 0;
710 * Check a string doesn't overrun the chunk of
711 * memory we copied from userland.
713 static int invalid_str(char *str
, void *end
)
715 while ((void *) str
< end
)
722 static int dev_rename(struct dm_ioctl
*param
, size_t param_size
)
725 char *new_name
= (char *) param
+ param
->data_start
;
727 if (new_name
< (char *) (param
+ 1) ||
728 invalid_str(new_name
, (void *) param
+ param_size
)) {
729 DMWARN("Invalid new logical volume name supplied.");
733 r
= check_name(new_name
);
737 param
->data_size
= 0;
738 return dm_hash_rename(param
->name
, new_name
);
741 static int dev_set_geometry(struct dm_ioctl
*param
, size_t param_size
)
744 struct mapped_device
*md
;
745 struct hd_geometry geometry
;
746 unsigned long indata
[4];
747 char *geostr
= (char *) param
+ param
->data_start
;
749 md
= find_device(param
);
753 if (geostr
< (char *) (param
+ 1) ||
754 invalid_str(geostr
, (void *) param
+ param_size
)) {
755 DMWARN("Invalid geometry supplied.");
759 x
= sscanf(geostr
, "%lu %lu %lu %lu", indata
,
760 indata
+ 1, indata
+ 2, indata
+ 3);
763 DMWARN("Unable to interpret geometry settings.");
767 if (indata
[0] > 65535 || indata
[1] > 255 ||
768 indata
[2] > 255 || indata
[3] > ULONG_MAX
) {
769 DMWARN("Geometry exceeds range limits.");
773 geometry
.cylinders
= indata
[0];
774 geometry
.heads
= indata
[1];
775 geometry
.sectors
= indata
[2];
776 geometry
.start
= indata
[3];
778 r
= dm_set_geometry(md
, &geometry
);
780 r
= __dev_status(md
, param
);
782 param
->data_size
= 0;
789 static int do_suspend(struct dm_ioctl
*param
)
793 struct mapped_device
*md
;
795 md
= find_device(param
);
799 if (param
->flags
& DM_SKIP_LOCKFS_FLAG
)
802 if (!dm_suspended(md
))
803 r
= dm_suspend(md
, do_lockfs
);
806 r
= __dev_status(md
, param
);
812 static int do_resume(struct dm_ioctl
*param
)
816 struct hash_cell
*hc
;
817 struct mapped_device
*md
;
818 struct dm_table
*new_map
;
820 down_write(&_hash_lock
);
822 hc
= __find_device_hash_cell(param
);
824 DMWARN("device doesn't appear to be in the dev hash table.");
825 up_write(&_hash_lock
);
831 new_map
= hc
->new_map
;
833 param
->flags
&= ~DM_INACTIVE_PRESENT_FLAG
;
835 up_write(&_hash_lock
);
837 /* Do we need to load a new map ? */
839 /* Suspend if it isn't already suspended */
840 if (param
->flags
& DM_SKIP_LOCKFS_FLAG
)
842 if (!dm_suspended(md
))
843 dm_suspend(md
, do_lockfs
);
845 r
= dm_swap_table(md
, new_map
);
848 dm_table_put(new_map
);
852 if (dm_table_get_mode(new_map
) & FMODE_WRITE
)
853 set_disk_ro(dm_disk(md
), 0);
855 set_disk_ro(dm_disk(md
), 1);
857 dm_table_put(new_map
);
860 if (dm_suspended(md
))
864 r
= __dev_status(md
, param
);
871 * Set or unset the suspension state of a device.
872 * If the device already is in the requested state we just return its status.
874 static int dev_suspend(struct dm_ioctl
*param
, size_t param_size
)
876 if (param
->flags
& DM_SUSPEND_FLAG
)
877 return do_suspend(param
);
879 return do_resume(param
);
883 * Copies device info back to user space, used by
884 * the create and info ioctls.
886 static int dev_status(struct dm_ioctl
*param
, size_t param_size
)
889 struct mapped_device
*md
;
891 md
= find_device(param
);
895 r
= __dev_status(md
, param
);
901 * Build up the status struct for each target
903 static void retrieve_status(struct dm_table
*table
,
904 struct dm_ioctl
*param
, size_t param_size
)
906 unsigned int i
, num_targets
;
907 struct dm_target_spec
*spec
;
908 char *outbuf
, *outptr
;
910 size_t remaining
, len
, used
= 0;
912 outptr
= outbuf
= get_result_buffer(param
, param_size
, &len
);
914 if (param
->flags
& DM_STATUS_TABLE_FLAG
)
915 type
= STATUSTYPE_TABLE
;
917 type
= STATUSTYPE_INFO
;
919 /* Get all the target info */
920 num_targets
= dm_table_get_num_targets(table
);
921 for (i
= 0; i
< num_targets
; i
++) {
922 struct dm_target
*ti
= dm_table_get_target(table
, i
);
924 remaining
= len
- (outptr
- outbuf
);
925 if (remaining
<= sizeof(struct dm_target_spec
)) {
926 param
->flags
|= DM_BUFFER_FULL_FLAG
;
930 spec
= (struct dm_target_spec
*) outptr
;
933 spec
->sector_start
= ti
->begin
;
934 spec
->length
= ti
->len
;
935 strncpy(spec
->target_type
, ti
->type
->name
,
936 sizeof(spec
->target_type
));
938 outptr
+= sizeof(struct dm_target_spec
);
939 remaining
= len
- (outptr
- outbuf
);
940 if (remaining
<= 0) {
941 param
->flags
|= DM_BUFFER_FULL_FLAG
;
945 /* Get the status/table string from the target driver */
946 if (ti
->type
->status
) {
947 if (ti
->type
->status(ti
, type
, outptr
, remaining
)) {
948 param
->flags
|= DM_BUFFER_FULL_FLAG
;
954 outptr
+= strlen(outptr
) + 1;
955 used
= param
->data_start
+ (outptr
- outbuf
);
957 outptr
= align_ptr(outptr
);
958 spec
->next
= outptr
- outbuf
;
962 param
->data_size
= used
;
964 param
->target_count
= num_targets
;
968 * Wait for a device to report an event
970 static int dev_wait(struct dm_ioctl
*param
, size_t param_size
)
973 struct mapped_device
*md
;
974 struct dm_table
*table
;
976 md
= find_device(param
);
981 * Wait for a notification event
983 if (dm_wait_event(md
, param
->event_nr
)) {
989 * The userland program is going to want to know what
990 * changed to trigger the event, so we may as well tell
991 * him and save an ioctl.
993 r
= __dev_status(md
, param
);
997 table
= dm_get_table(md
);
999 retrieve_status(table
, param
, param_size
);
1000 dm_table_put(table
);
1008 static inline int get_mode(struct dm_ioctl
*param
)
1010 int mode
= FMODE_READ
| FMODE_WRITE
;
1012 if (param
->flags
& DM_READONLY_FLAG
)
1018 static int next_target(struct dm_target_spec
*last
, uint32_t next
, void *end
,
1019 struct dm_target_spec
**spec
, char **target_params
)
1021 *spec
= (struct dm_target_spec
*) ((unsigned char *) last
+ next
);
1022 *target_params
= (char *) (*spec
+ 1);
1024 if (*spec
< (last
+ 1))
1027 return invalid_str(*target_params
, end
);
1030 static int populate_table(struct dm_table
*table
,
1031 struct dm_ioctl
*param
, size_t param_size
)
1035 struct dm_target_spec
*spec
= (struct dm_target_spec
*) param
;
1036 uint32_t next
= param
->data_start
;
1037 void *end
= (void *) param
+ param_size
;
1038 char *target_params
;
1040 if (!param
->target_count
) {
1041 DMWARN("populate_table: no targets specified");
1045 for (i
= 0; i
< param
->target_count
; i
++) {
1047 r
= next_target(spec
, next
, end
, &spec
, &target_params
);
1049 DMWARN("unable to find target");
1053 r
= dm_table_add_target(table
, spec
->target_type
,
1054 (sector_t
) spec
->sector_start
,
1055 (sector_t
) spec
->length
,
1058 DMWARN("error adding target to table");
1065 return dm_table_complete(table
);
1068 static int table_load(struct dm_ioctl
*param
, size_t param_size
)
1071 struct hash_cell
*hc
;
1073 struct mapped_device
*md
;
1075 md
= find_device(param
);
1079 r
= dm_table_create(&t
, get_mode(param
), param
->target_count
, md
);
1083 r
= populate_table(t
, param
, param_size
);
1089 down_write(&_hash_lock
);
1090 hc
= dm_get_mdptr(md
);
1091 if (!hc
|| hc
->md
!= md
) {
1092 DMWARN("device has been removed from the dev hash table.");
1094 up_write(&_hash_lock
);
1100 dm_table_put(hc
->new_map
);
1102 up_write(&_hash_lock
);
1104 param
->flags
|= DM_INACTIVE_PRESENT_FLAG
;
1105 r
= __dev_status(md
, param
);
1113 static int table_clear(struct dm_ioctl
*param
, size_t param_size
)
1116 struct hash_cell
*hc
;
1117 struct mapped_device
*md
;
1119 down_write(&_hash_lock
);
1121 hc
= __find_device_hash_cell(param
);
1123 DMWARN("device doesn't appear to be in the dev hash table.");
1124 up_write(&_hash_lock
);
1129 dm_table_put(hc
->new_map
);
1133 param
->flags
&= ~DM_INACTIVE_PRESENT_FLAG
;
1135 r
= __dev_status(hc
->md
, param
);
1137 up_write(&_hash_lock
);
1143 * Retrieves a list of devices used by a particular dm device.
1145 static void retrieve_deps(struct dm_table
*table
,
1146 struct dm_ioctl
*param
, size_t param_size
)
1148 unsigned int count
= 0;
1149 struct list_head
*tmp
;
1152 struct dm_target_deps
*deps
;
1154 deps
= get_result_buffer(param
, param_size
, &len
);
1157 * Count the devices.
1159 list_for_each (tmp
, dm_table_get_devices(table
))
1163 * Check we have enough space.
1165 needed
= sizeof(*deps
) + (sizeof(*deps
->dev
) * count
);
1167 param
->flags
|= DM_BUFFER_FULL_FLAG
;
1172 * Fill in the devices.
1174 deps
->count
= count
;
1176 list_for_each_entry (dd
, dm_table_get_devices(table
), list
)
1177 deps
->dev
[count
++] = huge_encode_dev(dd
->bdev
->bd_dev
);
1179 param
->data_size
= param
->data_start
+ needed
;
1182 static int table_deps(struct dm_ioctl
*param
, size_t param_size
)
1185 struct mapped_device
*md
;
1186 struct dm_table
*table
;
1188 md
= find_device(param
);
1192 r
= __dev_status(md
, param
);
1196 table
= dm_get_table(md
);
1198 retrieve_deps(table
, param
, param_size
);
1199 dm_table_put(table
);
1208 * Return the status of a device as a text string for each
1211 static int table_status(struct dm_ioctl
*param
, size_t param_size
)
1214 struct mapped_device
*md
;
1215 struct dm_table
*table
;
1217 md
= find_device(param
);
1221 r
= __dev_status(md
, param
);
1225 table
= dm_get_table(md
);
1227 retrieve_status(table
, param
, param_size
);
1228 dm_table_put(table
);
1237 * Pass a message to the target that's at the supplied device offset.
1239 static int target_message(struct dm_ioctl
*param
, size_t param_size
)
1243 struct mapped_device
*md
;
1244 struct dm_table
*table
;
1245 struct dm_target
*ti
;
1246 struct dm_target_msg
*tmsg
= (void *) param
+ param
->data_start
;
1248 md
= find_device(param
);
1252 r
= __dev_status(md
, param
);
1256 if (tmsg
< (struct dm_target_msg
*) (param
+ 1) ||
1257 invalid_str(tmsg
->message
, (void *) param
+ param_size
)) {
1258 DMWARN("Invalid target message parameters.");
1263 r
= dm_split_args(&argc
, &argv
, tmsg
->message
);
1265 DMWARN("Failed to split target message parameters");
1269 table
= dm_get_table(md
);
1273 if (tmsg
->sector
>= dm_table_get_size(table
)) {
1274 DMWARN("Target message sector outside device.");
1279 ti
= dm_table_find_target(table
, tmsg
->sector
);
1280 if (ti
->type
->message
)
1281 r
= ti
->type
->message(ti
, argc
, argv
);
1283 DMWARN("Target type does not support messages");
1288 dm_table_put(table
);
1292 param
->data_size
= 0;
1297 /*-----------------------------------------------------------------
1298 * Implementation of open/close/ioctl on the special char
1300 *---------------------------------------------------------------*/
1301 static ioctl_fn
lookup_ioctl(unsigned int cmd
)
1307 {DM_VERSION_CMD
, NULL
}, /* version is dealt with elsewhere */
1308 {DM_REMOVE_ALL_CMD
, remove_all
},
1309 {DM_LIST_DEVICES_CMD
, list_devices
},
1311 {DM_DEV_CREATE_CMD
, dev_create
},
1312 {DM_DEV_REMOVE_CMD
, dev_remove
},
1313 {DM_DEV_RENAME_CMD
, dev_rename
},
1314 {DM_DEV_SUSPEND_CMD
, dev_suspend
},
1315 {DM_DEV_STATUS_CMD
, dev_status
},
1316 {DM_DEV_WAIT_CMD
, dev_wait
},
1318 {DM_TABLE_LOAD_CMD
, table_load
},
1319 {DM_TABLE_CLEAR_CMD
, table_clear
},
1320 {DM_TABLE_DEPS_CMD
, table_deps
},
1321 {DM_TABLE_STATUS_CMD
, table_status
},
1323 {DM_LIST_VERSIONS_CMD
, list_versions
},
1325 {DM_TARGET_MSG_CMD
, target_message
},
1326 {DM_DEV_SET_GEOMETRY_CMD
, dev_set_geometry
}
1329 return (cmd
>= ARRAY_SIZE(_ioctls
)) ? NULL
: _ioctls
[cmd
].fn
;
1333 * As well as checking the version compatibility this always
1334 * copies the kernel interface version out.
1336 static int check_version(unsigned int cmd
, struct dm_ioctl __user
*user
)
1338 uint32_t version
[3];
1341 if (copy_from_user(version
, user
->version
, sizeof(version
)))
1344 if ((DM_VERSION_MAJOR
!= version
[0]) ||
1345 (DM_VERSION_MINOR
< version
[1])) {
1346 DMWARN("ioctl interface mismatch: "
1347 "kernel(%u.%u.%u), user(%u.%u.%u), cmd(%d)",
1348 DM_VERSION_MAJOR
, DM_VERSION_MINOR
,
1349 DM_VERSION_PATCHLEVEL
,
1350 version
[0], version
[1], version
[2], cmd
);
1355 * Fill in the kernel version.
1357 version
[0] = DM_VERSION_MAJOR
;
1358 version
[1] = DM_VERSION_MINOR
;
1359 version
[2] = DM_VERSION_PATCHLEVEL
;
1360 if (copy_to_user(user
->version
, version
, sizeof(version
)))
1366 static void free_params(struct dm_ioctl
*param
)
1371 static int copy_params(struct dm_ioctl __user
*user
, struct dm_ioctl
**param
)
1373 struct dm_ioctl tmp
, *dmi
;
1375 if (copy_from_user(&tmp
, user
, sizeof(tmp
)))
1378 if (tmp
.data_size
< sizeof(tmp
))
1381 dmi
= (struct dm_ioctl
*) vmalloc(tmp
.data_size
);
1385 if (copy_from_user(dmi
, user
, tmp
.data_size
)) {
1394 static int validate_params(uint cmd
, struct dm_ioctl
*param
)
1396 /* Always clear this flag */
1397 param
->flags
&= ~DM_BUFFER_FULL_FLAG
;
1399 /* Ignores parameters */
1400 if (cmd
== DM_REMOVE_ALL_CMD
||
1401 cmd
== DM_LIST_DEVICES_CMD
||
1402 cmd
== DM_LIST_VERSIONS_CMD
)
1405 if ((cmd
== DM_DEV_CREATE_CMD
)) {
1406 if (!*param
->name
) {
1407 DMWARN("name not supplied when creating device");
1410 } else if ((*param
->uuid
&& *param
->name
)) {
1411 DMWARN("only supply one of name or uuid, cmd(%u)", cmd
);
1415 /* Ensure strings are terminated */
1416 param
->name
[DM_NAME_LEN
- 1] = '\0';
1417 param
->uuid
[DM_UUID_LEN
- 1] = '\0';
1422 static int ctl_ioctl(struct inode
*inode
, struct file
*file
,
1423 uint command
, ulong u
)
1427 struct dm_ioctl
*param
;
1428 struct dm_ioctl __user
*user
= (struct dm_ioctl __user
*) u
;
1432 /* only root can play with this */
1433 if (!capable(CAP_SYS_ADMIN
))
1436 if (_IOC_TYPE(command
) != DM_IOCTL
)
1439 cmd
= _IOC_NR(command
);
1442 * Check the interface version passed in. This also
1443 * writes out the kernel's interface version.
1445 r
= check_version(cmd
, user
);
1450 * Nothing more to do for the version command.
1452 if (cmd
== DM_VERSION_CMD
)
1455 fn
= lookup_ioctl(cmd
);
1457 DMWARN("dm_ctl_ioctl: unknown command 0x%x", command
);
1462 * Trying to avoid low memory issues when a device is
1465 current
->flags
|= PF_MEMALLOC
;
1468 * Copy the parameters into kernel space.
1470 r
= copy_params(user
, ¶m
);
1472 current
->flags
&= ~PF_MEMALLOC
;
1477 r
= validate_params(cmd
, param
);
1481 param_size
= param
->data_size
;
1482 param
->data_size
= sizeof(*param
);
1483 r
= fn(param
, param_size
);
1486 * Copy the results back to userland.
1488 if (!r
&& copy_to_user(user
, param
, param
->data_size
))
1496 static struct file_operations _ctl_fops
= {
1498 .owner
= THIS_MODULE
,
1501 static struct miscdevice _dm_misc
= {
1502 .minor
= MISC_DYNAMIC_MINOR
,
1504 .devfs_name
= "mapper/control",
1509 * Create misc character device and link to DM_DIR/control.
1511 int __init
dm_interface_init(void)
1519 r
= misc_register(&_dm_misc
);
1521 DMERR("misc_register failed for control device");
1526 DMINFO("%d.%d.%d%s initialised: %s", DM_VERSION_MAJOR
,
1527 DM_VERSION_MINOR
, DM_VERSION_PATCHLEVEL
, DM_VERSION_EXTRA
,
1532 void dm_interface_exit(void)
1534 if (misc_deregister(&_dm_misc
) < 0)
1535 DMERR("misc_deregister failed for control device");