2 * Copyright (C) 2010-2012 by Dell Inc. All rights reserved.
3 * Copyright (C) 2011-2013 Red Hat, Inc.
5 * This file is released under the GPL.
7 * dm-switch is a device-mapper target that maps IO to underlying block
8 * devices efficiently when there are a large number of fixed-sized
9 * address regions but there is no simple pattern to allow for a compact
10 * mapping representation such as dm-stripe.
13 #include <linux/device-mapper.h>
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/vmalloc.h>
19 #define DM_MSG_PREFIX "switch"
22 * One region_table_slot_t holds <region_entries_per_slot> region table
23 * entries each of which is <region_table_entry_bits> in size.
25 typedef unsigned long region_table_slot_t;
28 * A device with the offset to its start sector.
36 * Context block for a dm switch device.
41 unsigned nr_paths; /* Number of paths in path_list. */
43 unsigned region_size; /* Region size in 512-byte sectors */
44 unsigned long nr_regions; /* Number of regions making up the device */
45 signed char region_size_bits; /* log2 of region_size or -1 */
47 unsigned char region_table_entry_bits; /* Number of bits in one region table entry */
48 unsigned char region_entries_per_slot; /* Number of entries in one region table slot */
49 signed char region_entries_per_slot_bits; /* log2 of region_entries_per_slot or -1 */
51 region_table_slot_t *region_table; /* Region table */
54 * Array of dm devices to switch between.
56 struct switch_path path_list[0];
59 static struct switch_ctx *alloc_switch_ctx(struct dm_target *ti, unsigned nr_paths,
62 struct switch_ctx *sctx;
64 sctx = kzalloc(sizeof(struct switch_ctx) + nr_paths * sizeof(struct switch_path),
70 sctx->region_size = region_size;
77 static int alloc_region_table(struct dm_target *ti, unsigned nr_paths)
79 struct switch_ctx *sctx = ti->private;
80 sector_t nr_regions = ti->len;
83 if (!(sctx->region_size & (sctx->region_size - 1)))
84 sctx->region_size_bits = __ffs(sctx->region_size);
86 sctx->region_size_bits = -1;
88 sctx->region_table_entry_bits = 1;
89 while (sctx->region_table_entry_bits < sizeof(region_table_slot_t) * 8 &&
90 (region_table_slot_t)1 << sctx->region_table_entry_bits < nr_paths)
91 sctx->region_table_entry_bits++;
93 sctx->region_entries_per_slot = (sizeof(region_table_slot_t) * 8) / sctx->region_table_entry_bits;
94 if (!(sctx->region_entries_per_slot & (sctx->region_entries_per_slot - 1)))
95 sctx->region_entries_per_slot_bits = __ffs(sctx->region_entries_per_slot);
97 sctx->region_entries_per_slot_bits = -1;
99 if (sector_div(nr_regions, sctx->region_size))
102 sctx->nr_regions = nr_regions;
103 if (sctx->nr_regions != nr_regions || sctx->nr_regions >= ULONG_MAX) {
104 ti->error = "Region table too large";
108 nr_slots = nr_regions;
109 if (sector_div(nr_slots, sctx->region_entries_per_slot))
112 if (nr_slots > ULONG_MAX / sizeof(region_table_slot_t)) {
113 ti->error = "Region table too large";
117 sctx->region_table = vmalloc(nr_slots * sizeof(region_table_slot_t));
118 if (!sctx->region_table) {
119 ti->error = "Cannot allocate region table";
126 static void switch_get_position(struct switch_ctx *sctx, unsigned long region_nr,
127 unsigned long *region_index, unsigned *bit)
129 if (sctx->region_entries_per_slot_bits >= 0) {
130 *region_index = region_nr >> sctx->region_entries_per_slot_bits;
131 *bit = region_nr & (sctx->region_entries_per_slot - 1);
133 *region_index = region_nr / sctx->region_entries_per_slot;
134 *bit = region_nr % sctx->region_entries_per_slot;
137 *bit *= sctx->region_table_entry_bits;
140 static unsigned switch_region_table_read(struct switch_ctx *sctx, unsigned long region_nr)
142 unsigned long region_index;
145 switch_get_position(sctx, region_nr, ®ion_index, &bit);
147 return (ACCESS_ONCE(sctx->region_table[region_index]) >> bit) &
148 ((1 << sctx->region_table_entry_bits) - 1);
152 * Find which path to use at given offset.
154 static unsigned switch_get_path_nr(struct switch_ctx *sctx, sector_t offset)
160 if (sctx->region_size_bits >= 0)
161 p >>= sctx->region_size_bits;
163 sector_div(p, sctx->region_size);
165 path_nr = switch_region_table_read(sctx, p);
167 /* This can only happen if the processor uses non-atomic stores. */
168 if (unlikely(path_nr >= sctx->nr_paths))
174 static void switch_region_table_write(struct switch_ctx *sctx, unsigned long region_nr,
177 unsigned long region_index;
179 region_table_slot_t pte;
181 switch_get_position(sctx, region_nr, ®ion_index, &bit);
183 pte = sctx->region_table[region_index];
184 pte &= ~((((region_table_slot_t)1 << sctx->region_table_entry_bits) - 1) << bit);
185 pte |= (region_table_slot_t)value << bit;
186 sctx->region_table[region_index] = pte;
190 * Fill the region table with an initial round robin pattern.
192 static void initialise_region_table(struct switch_ctx *sctx)
194 unsigned path_nr = 0;
195 unsigned long region_nr;
197 for (region_nr = 0; region_nr < sctx->nr_regions; region_nr++) {
198 switch_region_table_write(sctx, region_nr, path_nr);
199 if (++path_nr >= sctx->nr_paths)
204 static int parse_path(struct dm_arg_set *as, struct dm_target *ti)
206 struct switch_ctx *sctx = ti->private;
207 unsigned long long start;
210 r = dm_get_device(ti, dm_shift_arg(as), dm_table_get_mode(ti->table),
211 &sctx->path_list[sctx->nr_paths].dmdev);
213 ti->error = "Device lookup failed";
217 if (kstrtoull(dm_shift_arg(as), 10, &start) || start != (sector_t)start) {
218 ti->error = "Invalid device starting offset";
219 dm_put_device(ti, sctx->path_list[sctx->nr_paths].dmdev);
223 sctx->path_list[sctx->nr_paths].start = start;
231 * Destructor: Don't free the dm_target, just the ti->private data (if any).
233 static void switch_dtr(struct dm_target *ti)
235 struct switch_ctx *sctx = ti->private;
237 while (sctx->nr_paths--)
238 dm_put_device(ti, sctx->path_list[sctx->nr_paths].dmdev);
240 vfree(sctx->region_table);
245 * Constructor arguments:
246 * <num_paths> <region_size> <num_optional_args> [<optional_args>...]
247 * [<dev_path> <offset>]+
249 * Optional args are to allow for future extension: currently this
250 * parameter must be 0.
252 static int switch_ctr(struct dm_target *ti, unsigned argc, char **argv)
254 static struct dm_arg _args[] = {
255 {1, (KMALLOC_MAX_SIZE - sizeof(struct switch_ctx)) / sizeof(struct switch_path), "Invalid number of paths"},
256 {1, UINT_MAX, "Invalid region size"},
257 {0, 0, "Invalid number of optional args"},
260 struct switch_ctx *sctx;
261 struct dm_arg_set as;
262 unsigned nr_paths, region_size, nr_optional_args;
268 r = dm_read_arg(_args, &as, &nr_paths, &ti->error);
272 r = dm_read_arg(_args + 1, &as, ®ion_size, &ti->error);
276 r = dm_read_arg_group(_args + 2, &as, &nr_optional_args, &ti->error);
279 /* parse optional arguments here, if we add any */
281 if (as.argc != nr_paths * 2) {
282 ti->error = "Incorrect number of path arguments";
286 sctx = alloc_switch_ctx(ti, nr_paths, region_size);
288 ti->error = "Cannot allocate redirection context";
292 r = dm_set_target_max_io_len(ti, region_size);
297 r = parse_path(&as, ti);
302 r = alloc_region_table(ti, nr_paths);
306 initialise_region_table(sctx);
308 /* For UNMAP, sending the request down any path is sufficient */
309 ti->num_discard_bios = 1;
319 static int switch_map(struct dm_target *ti, struct bio *bio)
321 struct switch_ctx *sctx = ti->private;
322 sector_t offset = dm_target_offset(ti, bio->bi_iter.bi_sector);
323 unsigned path_nr = switch_get_path_nr(sctx, offset);
325 bio->bi_bdev = sctx->path_list[path_nr].dmdev->bdev;
326 bio->bi_iter.bi_sector = sctx->path_list[path_nr].start + offset;
328 return DM_MAPIO_REMAPPED;
332 * We need to parse hex numbers in the message as quickly as possible.
334 * This table-based hex parser improves performance.
335 * It improves a time to load 1000000 entries compared to the condition-based
337 * table-based parser condition-based parser
338 * PA-RISC 0.29s 0.31s
339 * Opteron 0.0495s 0.0498s
341 static const unsigned char hex_table[256] = {
342 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
343 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
344 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
345 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 255, 255, 255, 255, 255, 255,
346 255, 10, 11, 12, 13, 14, 15, 255, 255, 255, 255, 255, 255, 255, 255, 255,
347 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
348 255, 10, 11, 12, 13, 14, 15, 255, 255, 255, 255, 255, 255, 255, 255, 255,
349 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
350 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
351 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
352 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
353 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
354 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
355 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
356 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
357 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255
360 static __always_inline unsigned long parse_hex(const char **string)
365 while ((d = hex_table[(unsigned char)**string]) < 16) {
373 static int process_set_region_mappings(struct switch_ctx *sctx,
374 unsigned argc, char **argv)
377 unsigned long region_index = 0;
379 for (i = 1; i < argc; i++) {
380 unsigned long path_nr;
381 const char *string = argv[i];
383 if ((*string & 0xdf) == 'R') {
384 unsigned long cycle_length, num_write;
387 if (unlikely(*string == ',')) {
388 DMWARN("invalid set_region_mappings argument: '%s'", argv[i]);
391 cycle_length = parse_hex(&string);
392 if (unlikely(*string != ',')) {
393 DMWARN("invalid set_region_mappings argument: '%s'", argv[i]);
397 if (unlikely(!*string)) {
398 DMWARN("invalid set_region_mappings argument: '%s'", argv[i]);
401 num_write = parse_hex(&string);
402 if (unlikely(*string)) {
403 DMWARN("invalid set_region_mappings argument: '%s'", argv[i]);
407 if (unlikely(!cycle_length) || unlikely(cycle_length - 1 > region_index)) {
408 DMWARN("invalid set_region_mappings cycle length: %lu > %lu",
409 cycle_length - 1, region_index);
412 if (unlikely(region_index + num_write < region_index) ||
413 unlikely(region_index + num_write >= sctx->nr_regions)) {
414 DMWARN("invalid set_region_mappings region number: %lu + %lu >= %lu",
415 region_index, num_write, sctx->nr_regions);
419 while (num_write--) {
421 path_nr = switch_region_table_read(sctx, region_index - cycle_length);
422 switch_region_table_write(sctx, region_index, path_nr);
431 region_index = parse_hex(&string);
432 if (unlikely(*string != ':')) {
433 DMWARN("invalid set_region_mappings argument: '%s'", argv[i]);
439 if (unlikely(!*string)) {
440 DMWARN("invalid set_region_mappings argument: '%s'", argv[i]);
444 path_nr = parse_hex(&string);
445 if (unlikely(*string)) {
446 DMWARN("invalid set_region_mappings argument: '%s'", argv[i]);
449 if (unlikely(region_index >= sctx->nr_regions)) {
450 DMWARN("invalid set_region_mappings region number: %lu >= %lu", region_index, sctx->nr_regions);
453 if (unlikely(path_nr >= sctx->nr_paths)) {
454 DMWARN("invalid set_region_mappings device: %lu >= %u", path_nr, sctx->nr_paths);
458 switch_region_table_write(sctx, region_index, path_nr);
465 * Messages are processed one-at-a-time.
467 * Only set_region_mappings is supported.
469 static int switch_message(struct dm_target *ti, unsigned argc, char **argv)
471 static DEFINE_MUTEX(message_mutex);
473 struct switch_ctx *sctx = ti->private;
476 mutex_lock(&message_mutex);
478 if (!strcasecmp(argv[0], "set_region_mappings"))
479 r = process_set_region_mappings(sctx, argc, argv);
481 DMWARN("Unrecognised message received.");
483 mutex_unlock(&message_mutex);
488 static void switch_status(struct dm_target *ti, status_type_t type,
489 unsigned status_flags, char *result, unsigned maxlen)
491 struct switch_ctx *sctx = ti->private;
496 case STATUSTYPE_INFO:
500 case STATUSTYPE_TABLE:
501 DMEMIT("%u %u 0", sctx->nr_paths, sctx->region_size);
502 for (path_nr = 0; path_nr < sctx->nr_paths; path_nr++)
503 DMEMIT(" %s %llu", sctx->path_list[path_nr].dmdev->name,
504 (unsigned long long)sctx->path_list[path_nr].start);
512 * Passthrough all ioctls to the path for sector 0
514 static int switch_ioctl(struct dm_target *ti, unsigned cmd,
517 struct switch_ctx *sctx = ti->private;
518 struct block_device *bdev;
523 path_nr = switch_get_path_nr(sctx, 0);
525 bdev = sctx->path_list[path_nr].dmdev->bdev;
526 mode = sctx->path_list[path_nr].dmdev->mode;
529 * Only pass ioctls through if the device sizes match exactly.
531 if (ti->len + sctx->path_list[path_nr].start != i_size_read(bdev->bd_inode) >> SECTOR_SHIFT)
532 r = scsi_verify_blk_ioctl(NULL, cmd);
534 return r ? : __blkdev_driver_ioctl(bdev, mode, cmd, arg);
537 static int switch_iterate_devices(struct dm_target *ti,
538 iterate_devices_callout_fn fn, void *data)
540 struct switch_ctx *sctx = ti->private;
544 for (path_nr = 0; path_nr < sctx->nr_paths; path_nr++) {
545 r = fn(ti, sctx->path_list[path_nr].dmdev,
546 sctx->path_list[path_nr].start, ti->len, data);
554 static struct target_type switch_target = {
556 .version = {1, 1, 0},
557 .module = THIS_MODULE,
561 .message = switch_message,
562 .status = switch_status,
563 .ioctl = switch_ioctl,
564 .iterate_devices = switch_iterate_devices,
567 static int __init dm_switch_init(void)
571 r = dm_register_target(&switch_target);
573 DMERR("dm_register_target() failed %d", r);
578 static void __exit dm_switch_exit(void)
580 dm_unregister_target(&switch_target);
583 module_init(dm_switch_init);
584 module_exit(dm_switch_exit);
586 MODULE_DESCRIPTION(DM_NAME " dynamic path switching target");
587 MODULE_AUTHOR("Kevin D. O'Kelley <Kevin_OKelley@dell.com>");
588 MODULE_AUTHOR("Narendran Ganapathy <Narendran_Ganapathy@dell.com>");
589 MODULE_AUTHOR("Jim Ramsay <Jim_Ramsay@dell.com>");
590 MODULE_AUTHOR("Mikulas Patocka <mpatocka@redhat.com>");
591 MODULE_LICENSE("GPL");