2 * Copyright (C) 2001, 2002 Sistina Software (UK) Limited.
4 * This file is released under the GPL.
9 #include <linux/module.h>
10 #include <linux/vmalloc.h>
11 #include <linux/miscdevice.h>
12 #include <linux/dm-ioctl.h>
13 #include <linux/init.h>
14 #include <linux/wait.h>
15 #include <linux/blk.h>
16 #include <linux/slab.h>
17 #include <linux/devfs_fs_kernel.h>
19 #include <asm/uaccess.h>
21 #define DM_DRIVER_EMAIL "dm@uk.sistina.com"
23 /*-----------------------------------------------------------------
24 * The ioctl interface needs to be able to look up devices by
26 *---------------------------------------------------------------*/
28 struct list_head name_list
;
29 struct list_head uuid_list
;
33 struct mapped_device
*md
;
36 #define NUM_BUCKETS 64
37 #define MASK_BUCKETS (NUM_BUCKETS - 1)
38 static struct list_head _name_buckets
[NUM_BUCKETS
];
39 static struct list_head _uuid_buckets
[NUM_BUCKETS
];
41 void dm_hash_remove_all(void);
44 * Guards access to all three tables.
46 static DECLARE_RWSEM(_hash_lock
);
48 static void init_buckets(struct list_head
*buckets
)
52 for (i
= 0; i
< NUM_BUCKETS
; i
++)
53 INIT_LIST_HEAD(buckets
+ i
);
56 int dm_hash_init(void)
58 init_buckets(_name_buckets
);
59 init_buckets(_uuid_buckets
);
64 void dm_hash_exit(void)
70 /*-----------------------------------------------------------------
72 * We're not really concerned with the str hash function being
73 * fast since it's only used by the ioctl interface.
74 *---------------------------------------------------------------*/
75 static unsigned int hash_str(const char *str
)
77 const unsigned int hash_mult
= 2654435387U;
81 h
= (h
+ (unsigned int) *str
++) * hash_mult
;
83 return h
& MASK_BUCKETS
;
86 /*-----------------------------------------------------------------
87 * Code for looking up a device by name
88 *---------------------------------------------------------------*/
89 static struct hash_cell
*__get_name_cell(const char *str
)
91 struct list_head
*tmp
;
93 unsigned int h
= hash_str(str
);
95 list_for_each (tmp
, _name_buckets
+ h
) {
96 hc
= list_entry(tmp
, struct hash_cell
, name_list
);
97 if (!strcmp(hc
->name
, str
))
104 static struct hash_cell
*__get_uuid_cell(const char *str
)
106 struct list_head
*tmp
;
107 struct hash_cell
*hc
;
108 unsigned int h
= hash_str(str
);
110 list_for_each (tmp
, _uuid_buckets
+ h
) {
111 hc
= list_entry(tmp
, struct hash_cell
, uuid_list
);
112 if (!strcmp(hc
->uuid
, str
))
119 /*-----------------------------------------------------------------
120 * Inserting, removing and renaming a device.
121 *---------------------------------------------------------------*/
122 static inline char *kstrdup(const char *str
)
124 char *r
= kmalloc(strlen(str
) + 1, GFP_KERNEL
);
130 static struct hash_cell
*alloc_cell(const char *name
, const char *uuid
,
131 struct mapped_device
*md
)
133 struct hash_cell
*hc
;
135 hc
= kmalloc(sizeof(*hc
), GFP_KERNEL
);
139 hc
->name
= kstrdup(name
);
149 hc
->uuid
= kstrdup(uuid
);
157 INIT_LIST_HEAD(&hc
->name_list
);
158 INIT_LIST_HEAD(&hc
->uuid_list
);
163 static void free_cell(struct hash_cell
*hc
)
175 static int register_with_devfs(struct hash_cell
*hc
)
177 struct gendisk
*disk
= dm_disk(hc
->md
);
179 devfs_mk_bdev(MKDEV(disk
->major
, disk
->first_minor
),
180 S_IFBLK
| S_IRUSR
| S_IWUSR
| S_IRGRP
,
181 DM_DIR
"/%s", hc
->name
);
185 static int unregister_with_devfs(struct hash_cell
*hc
)
187 devfs_remove(DM_DIR
"/%s", hc
->name
);
192 * The kdev_t and uuid of a device can never change once it is
193 * initially inserted.
195 int dm_hash_insert(const char *name
, const char *uuid
, struct mapped_device
*md
)
197 struct hash_cell
*cell
;
200 * Allocate the new cells.
202 cell
= alloc_cell(name
, uuid
, md
);
207 * Insert the cell into all three hash tables.
209 down_write(&_hash_lock
);
210 if (__get_name_cell(name
))
213 list_add(&cell
->name_list
, _name_buckets
+ hash_str(name
));
216 if (__get_uuid_cell(uuid
)) {
217 list_del(&cell
->name_list
);
220 list_add(&cell
->uuid_list
, _uuid_buckets
+ hash_str(uuid
));
222 register_with_devfs(cell
);
224 up_write(&_hash_lock
);
229 up_write(&_hash_lock
);
234 void __hash_remove(struct hash_cell
*hc
)
236 /* remove from the dev hash */
237 list_del(&hc
->uuid_list
);
238 list_del(&hc
->name_list
);
239 unregister_with_devfs(hc
);
244 void dm_hash_remove_all(void)
247 struct hash_cell
*hc
;
248 struct list_head
*tmp
, *n
;
250 down_write(&_hash_lock
);
251 for (i
= 0; i
< NUM_BUCKETS
; i
++) {
252 list_for_each_safe (tmp
, n
, _name_buckets
+ i
) {
253 hc
= list_entry(tmp
, struct hash_cell
, name_list
);
257 up_write(&_hash_lock
);
260 int dm_hash_rename(const char *old
, const char *new)
262 char *new_name
, *old_name
;
263 struct hash_cell
*hc
;
268 new_name
= kstrdup(new);
272 down_write(&_hash_lock
);
277 hc
= __get_name_cell(new);
279 DMWARN("asked to rename to an already existing name %s -> %s",
281 up_write(&_hash_lock
);
287 * Is there such a device as 'old' ?
289 hc
= __get_name_cell(old
);
291 DMWARN("asked to rename a non existent device %s -> %s",
293 up_write(&_hash_lock
);
299 * rename and move the name cell.
301 unregister_with_devfs(hc
);
303 list_del(&hc
->name_list
);
306 list_add(&hc
->name_list
, _name_buckets
+ hash_str(new_name
));
308 /* rename the device node in devfs */
309 register_with_devfs(hc
);
311 up_write(&_hash_lock
);
317 /*-----------------------------------------------------------------
318 * Implementation of the ioctl commands
319 *---------------------------------------------------------------*/
322 * All the ioctl commands get dispatched to functions with this
325 typedef int (*ioctl_fn
)(struct dm_ioctl
*param
, struct dm_ioctl
*user
);
328 * Check a string doesn't overrun the chunk of
329 * memory we copied from userland.
331 static int valid_str(char *str
, void *begin
, void *end
)
333 while (((void *) str
>= begin
) && ((void *) str
< end
))
340 static int next_target(struct dm_target_spec
*last
, uint32_t next
,
341 void *begin
, void *end
,
342 struct dm_target_spec
**spec
, char **params
)
344 *spec
= (struct dm_target_spec
*)
345 ((unsigned char *) last
+ next
);
346 *params
= (char *) (*spec
+ 1);
348 if (*spec
< (last
+ 1) || ((void *) *spec
> end
))
351 return valid_str(*params
, begin
, end
);
354 static int populate_table(struct dm_table
*table
, struct dm_ioctl
*args
)
358 struct dm_target_spec
*spec
;
362 if (!args
->target_count
) {
363 DMWARN("populate_table: no targets specified");
367 begin
= (void *) args
;
368 end
= begin
+ args
->data_size
;
370 for (i
= 0; i
< args
->target_count
; i
++) {
373 r
= next_target((struct dm_target_spec
*) args
,
375 begin
, end
, &spec
, ¶ms
);
377 r
= next_target(spec
, spec
->next
, begin
, end
,
381 DMWARN("unable to find target");
385 r
= dm_table_add_target(table
, spec
->target_type
,
386 (sector_t
) spec
->sector_start
,
387 (sector_t
) spec
->length
,
390 DMWARN("internal error adding target to table");
397 return dm_table_complete(table
);
401 * Round up the ptr to the next 'align' boundary. Obviously
402 * 'align' must be a power of 2.
404 static inline void *align_ptr(void *ptr
, unsigned int align
)
407 return (void *) (((unsigned long) (ptr
+ align
)) & ~align
);
411 * Copies a dm_ioctl and an optional additional payload to
414 static int results_to_user(struct dm_ioctl
*user
, struct dm_ioctl
*param
,
415 void *data
, uint32_t len
)
421 ptr
= align_ptr(user
+ 1, sizeof(unsigned long));
422 param
->data_start
= ptr
- (void *) user
;
426 * The version number has already been filled in, so we
427 * just copy later fields.
429 r
= copy_to_user(&user
->data_size
, ¶m
->data_size
,
430 sizeof(*param
) - sizeof(param
->version
));
435 if (param
->data_start
+ len
> param
->data_size
)
438 if (copy_to_user(ptr
, data
, len
))
446 * Fills in a dm_ioctl structure, ready for sending back to
449 static int __info(struct mapped_device
*md
, struct dm_ioctl
*param
)
451 struct dm_table
*table
;
452 struct block_device
*bdev
;
453 struct gendisk
*disk
= dm_disk(md
);
455 param
->flags
= DM_EXISTS_FLAG
;
456 if (dm_suspended(md
))
457 param
->flags
|= DM_SUSPEND_FLAG
;
459 bdev
= bdget_disk(disk
, 0);
463 param
->dev
= bdev
->bd_dev
;
464 param
->open_count
= bdev
->bd_openers
;
468 param
->flags
|= DM_READONLY_FLAG
;
470 table
= dm_get_table(md
);
471 param
->target_count
= dm_table_get_num_targets(table
);
478 * Always use UUID for lookups if it's present, otherwise use name.
480 static inline struct mapped_device
*find_device(struct dm_ioctl
*param
)
482 struct hash_cell
*hc
;
483 struct mapped_device
*md
= NULL
;
485 down_read(&_hash_lock
);
486 hc
= *param
->uuid
? __get_uuid_cell(param
->uuid
) :
487 __get_name_cell(param
->name
);
492 * Sneakily write in both the name and the uuid
493 * while we have the cell.
495 strlcpy(param
->name
, hc
->name
, sizeof(param
->name
));
497 strlcpy(param
->uuid
, hc
->uuid
, sizeof(param
->uuid
));
499 param
->uuid
[0] = '\0';
503 up_read(&_hash_lock
);
508 #define ALIGNMENT sizeof(int)
509 static void *_align(void *ptr
, unsigned int a
)
511 register unsigned long align
= --a
;
513 return (void *) (((unsigned long) ptr
+ align
) & ~align
);
517 * Copies device info back to user space, used by
518 * the create and info ioctls.
520 static int info(struct dm_ioctl
*param
, struct dm_ioctl
*user
)
522 struct mapped_device
*md
;
526 md
= find_device(param
);
529 * Device not found - returns cleared exists flag.
537 return results_to_user(user
, param
, NULL
, 0);
540 static inline int get_mode(struct dm_ioctl
*param
)
542 int mode
= FMODE_READ
| FMODE_WRITE
;
544 if (param
->flags
& DM_READONLY_FLAG
)
550 static int check_name(const char *name
)
552 if (name
[0] == '/') {
553 DMWARN("invalid device name");
560 static int create(struct dm_ioctl
*param
, struct dm_ioctl
*user
)
564 struct mapped_device
*md
;
566 r
= check_name(param
->name
);
570 r
= dm_table_create(&t
, get_mode(param
));
574 r
= populate_table(t
, param
);
580 if (param
->flags
& DM_PERSISTENT_DEV_FLAG
)
581 r
= dm_create_with_minor(minor(to_kdev_t(param
->dev
)), t
, &md
);
583 r
= dm_create(t
, &md
);
589 dm_table_put(t
); /* md will have grabbed its own reference */
591 set_disk_ro(dm_disk(md
), (param
->flags
& DM_READONLY_FLAG
) ? 1 : 0);
592 r
= dm_hash_insert(param
->name
, *param
->uuid
? param
->uuid
: NULL
, md
);
595 return r
? r
: info(param
, user
);
599 * Build up the status struct for each target
601 static int __status(struct mapped_device
*md
, struct dm_ioctl
*param
,
602 char *outbuf
, size_t *len
)
604 unsigned int i
, num_targets
;
605 struct dm_target_spec
*spec
;
608 struct dm_table
*table
= dm_get_table(md
);
610 if (param
->flags
& DM_STATUS_TABLE_FLAG
)
611 type
= STATUSTYPE_TABLE
;
613 type
= STATUSTYPE_INFO
;
617 /* Get all the target info */
618 num_targets
= dm_table_get_num_targets(table
);
619 for (i
= 0; i
< num_targets
; i
++) {
620 struct dm_target
*ti
= dm_table_get_target(table
, i
);
622 if (outptr
- outbuf
+
623 sizeof(struct dm_target_spec
) > param
->data_size
) {
628 spec
= (struct dm_target_spec
*) outptr
;
631 spec
->sector_start
= ti
->begin
;
632 spec
->length
= ti
->len
;
633 strlcpy(spec
->target_type
, ti
->type
->name
,
634 sizeof(spec
->target_type
));
636 outptr
+= sizeof(struct dm_target_spec
);
638 /* Get the status/table string from the target driver */
639 if (ti
->type
->status
)
640 ti
->type
->status(ti
, type
, outptr
,
641 outbuf
+ param
->data_size
- outptr
);
645 outptr
+= strlen(outptr
) + 1;
646 _align(outptr
, ALIGNMENT
);
647 spec
->next
= outptr
- outbuf
;
650 param
->target_count
= num_targets
;
651 *len
= outptr
- outbuf
;
658 * Return the status of a device as a text string for each
661 static int get_status(struct dm_ioctl
*param
, struct dm_ioctl
*user
)
663 struct mapped_device
*md
;
668 md
= find_device(param
);
671 * Device not found - returns cleared exists flag.
675 /* We haven't a clue how long the resultant data will be so
676 just allocate as much as userland has allowed us and make sure
677 we don't overun it */
678 outbuf
= kmalloc(param
->data_size
, GFP_KERNEL
);
682 * Get the status of all targets
684 __status(md
, param
, outbuf
, &len
);
687 * Setup the basic dm_ioctl structure.
695 ret
= results_to_user(user
, param
, outbuf
, len
);
704 * Wait for a device to report an event
706 static int wait_device_event(struct dm_ioctl
*param
, struct dm_ioctl
*user
)
708 struct mapped_device
*md
;
709 struct dm_table
*table
;
710 DECLARE_WAITQUEUE(wq
, current
);
712 md
= find_device(param
);
715 * Device not found - returns cleared exists flag.
720 * Setup the basic dm_ioctl structure.
725 * Wait for a notification event
727 set_current_state(TASK_INTERRUPTIBLE
);
728 table
= dm_get_table(md
);
729 dm_table_add_wait_queue(table
, &wq
);
736 return results_to_user(user
, param
, NULL
, 0);
740 * Retrieves a list of devices used by a particular dm device.
742 static int dep(struct dm_ioctl
*param
, struct dm_ioctl
*user
)
746 struct mapped_device
*md
;
747 struct list_head
*tmp
;
749 struct dm_target_deps
*deps
= NULL
;
750 struct dm_table
*table
;
752 md
= find_device(param
);
755 table
= dm_get_table(md
);
758 * Setup the basic dm_ioctl structure.
766 list_for_each(tmp
, dm_table_get_devices(table
))
770 * Allocate a kernel space version of the dm_target_status
773 if (array_too_big(sizeof(*deps
), sizeof(*deps
->dev
), count
)) {
779 len
= sizeof(*deps
) + (sizeof(*deps
->dev
) * count
);
780 deps
= kmalloc(len
, GFP_KERNEL
);
788 * Fill in the devices.
792 list_for_each(tmp
, dm_table_get_devices(table
)) {
793 struct dm_dev
*dd
= list_entry(tmp
, struct dm_dev
, list
);
794 deps
->dev
[count
++] = dd
->bdev
->bd_dev
;
800 r
= results_to_user(user
, param
, deps
, len
);
806 static int remove(struct dm_ioctl
*param
, struct dm_ioctl
*user
)
808 struct hash_cell
*hc
;
810 down_write(&_hash_lock
);
811 hc
= *param
->uuid
? __get_uuid_cell(param
->uuid
) :
812 __get_name_cell(param
->name
);
814 DMWARN("device doesn't appear to be in the dev hash table.");
815 up_write(&_hash_lock
);
820 * You may ask the interface to drop its reference to an
821 * in use device. This is no different to unlinking a
822 * file that someone still has open. The device will not
823 * actually be destroyed until the last opener closes it.
824 * The name and uuid of the device (both are interface
825 * properties) will be available for reuse immediately.
827 * You don't want to drop a _suspended_ device from the
828 * interface, since that will leave you with no way of
831 if (dm_suspended(hc
->md
)) {
832 DMWARN("refusing to remove a suspended device.");
833 up_write(&_hash_lock
);
838 up_write(&_hash_lock
);
842 static int remove_all(struct dm_ioctl
*param
, struct dm_ioctl
*user
)
844 dm_hash_remove_all();
848 static int suspend(struct dm_ioctl
*param
, struct dm_ioctl
*user
)
851 struct mapped_device
*md
;
853 md
= find_device(param
);
857 if (param
->flags
& DM_SUSPEND_FLAG
)
866 static int reload(struct dm_ioctl
*param
, struct dm_ioctl
*user
)
869 struct mapped_device
*md
;
872 r
= dm_table_create(&t
, get_mode(param
));
876 r
= populate_table(t
, param
);
882 md
= find_device(param
);
888 r
= dm_swap_table(md
, t
);
894 dm_table_put(t
); /* md will have taken its own reference */
896 set_disk_ro(dm_disk(md
), (param
->flags
& DM_READONLY_FLAG
) ? 1 : 0);
899 r
= info(param
, user
);
903 static int rename(struct dm_ioctl
*param
, struct dm_ioctl
*user
)
906 char *new_name
= (char *) param
+ param
->data_start
;
908 if (valid_str(new_name
, (void *) param
,
909 (void *) param
+ param
->data_size
)) {
910 DMWARN("Invalid new logical volume name supplied.");
914 r
= check_name(new_name
);
918 return dm_hash_rename(param
->name
, new_name
);
922 /*-----------------------------------------------------------------
923 * Implementation of open/close/ioctl on the special char
925 *---------------------------------------------------------------*/
926 static ioctl_fn
lookup_ioctl(unsigned int cmd
)
932 {DM_VERSION_CMD
, NULL
}, /* version is dealt with elsewhere */
933 {DM_REMOVE_ALL_CMD
, remove_all
},
934 {DM_DEV_CREATE_CMD
, create
},
935 {DM_DEV_REMOVE_CMD
, remove
},
936 {DM_DEV_RELOAD_CMD
, reload
},
937 {DM_DEV_RENAME_CMD
, rename
},
938 {DM_DEV_SUSPEND_CMD
, suspend
},
939 {DM_DEV_DEPS_CMD
, dep
},
940 {DM_DEV_STATUS_CMD
, info
},
941 {DM_TARGET_STATUS_CMD
, get_status
},
942 {DM_TARGET_WAIT_CMD
, wait_device_event
},
945 return (cmd
>= ARRAY_SIZE(_ioctls
)) ? NULL
: _ioctls
[cmd
].fn
;
949 * As well as checking the version compatibility this always
950 * copies the kernel interface version out.
952 static int check_version(unsigned int cmd
, struct dm_ioctl
*user
)
957 if (copy_from_user(version
, user
->version
, sizeof(version
)))
960 if ((DM_VERSION_MAJOR
!= version
[0]) ||
961 (DM_VERSION_MINOR
< version
[1])) {
962 DMWARN("ioctl interface mismatch: "
963 "kernel(%u.%u.%u), user(%u.%u.%u), cmd(%d)",
964 DM_VERSION_MAJOR
, DM_VERSION_MINOR
,
965 DM_VERSION_PATCHLEVEL
,
966 version
[0], version
[1], version
[2], cmd
);
971 * Fill in the kernel version.
973 version
[0] = DM_VERSION_MAJOR
;
974 version
[1] = DM_VERSION_MINOR
;
975 version
[2] = DM_VERSION_PATCHLEVEL
;
976 if (copy_to_user(user
->version
, version
, sizeof(version
)))
982 static void free_params(struct dm_ioctl
*param
)
987 static int copy_params(struct dm_ioctl
*user
, struct dm_ioctl
**param
)
989 struct dm_ioctl tmp
, *dmi
;
991 if (copy_from_user(&tmp
, user
, sizeof(tmp
)))
994 if (tmp
.data_size
< sizeof(tmp
))
997 dmi
= (struct dm_ioctl
*) vmalloc(tmp
.data_size
);
1001 if (copy_from_user(dmi
, user
, tmp
.data_size
)) {
1010 static int validate_params(uint cmd
, struct dm_ioctl
*param
)
1012 /* Ignores parameters */
1013 if (cmd
== DM_REMOVE_ALL_CMD
)
1016 /* Unless creating, either name of uuid but not both */
1017 if (cmd
!= DM_DEV_CREATE_CMD
) {
1018 if ((!*param
->uuid
&& !*param
->name
) ||
1019 (*param
->uuid
&& *param
->name
)) {
1020 DMWARN("one of name or uuid must be supplied");
1025 /* Ensure strings are terminated */
1026 param
->name
[DM_NAME_LEN
- 1] = '\0';
1027 param
->uuid
[DM_UUID_LEN
- 1] = '\0';
1032 static int ctl_ioctl(struct inode
*inode
, struct file
*file
,
1033 uint command
, ulong u
)
1037 struct dm_ioctl
*param
;
1038 struct dm_ioctl
*user
= (struct dm_ioctl
*) u
;
1041 /* only root can play with this */
1042 if (!capable(CAP_SYS_ADMIN
))
1045 if (_IOC_TYPE(command
) != DM_IOCTL
)
1048 cmd
= _IOC_NR(command
);
1051 * Check the interface version passed in. This also
1052 * writes out the kernels interface version.
1054 r
= check_version(cmd
, user
);
1059 * Nothing more to do for the version command.
1061 if (cmd
== DM_VERSION_CMD
)
1064 fn
= lookup_ioctl(cmd
);
1066 DMWARN("dm_ctl_ioctl: unknown command 0x%x", command
);
1071 * Copy the parameters into kernel space.
1073 r
= copy_params(user
, ¶m
);
1077 r
= validate_params(cmd
, param
);
1083 r
= fn(param
, user
);
1088 static struct file_operations _ctl_fops
= {
1090 .owner
= THIS_MODULE
,
1093 static struct miscdevice _dm_misc
= {
1094 .minor
= MISC_DYNAMIC_MINOR
,
1096 .devfs_name
= "mapper/control",
1101 * Create misc character device and link to DM_DIR/control.
1103 int __init
dm_interface_init(void)
1111 r
= misc_register(&_dm_misc
);
1113 DMERR("misc_register failed for control device");
1118 DMINFO("%d.%d.%d%s initialised: %s", DM_VERSION_MAJOR
,
1119 DM_VERSION_MINOR
, DM_VERSION_PATCHLEVEL
, DM_VERSION_EXTRA
,
1123 if (misc_deregister(&_dm_misc
) < 0)
1124 DMERR("misc_deregister failed for control device");
1129 void dm_interface_exit(void)
1131 if (misc_deregister(&_dm_misc
) < 0)
1132 DMERR("misc_deregister failed for control device");