dm-unstripe.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * Copyright (C) 2017 Intel Corporation.
  3. *
  4. * This file is released under the GPL.
  5. */
  6. #include "dm.h"
  7. #include <linux/module.h>
  8. struct unstripe_c {
  9. struct dm_dev *dev;
  10. sector_t physical_start;
  11. uint32_t stripes;
  12. uint32_t unstripe;
  13. sector_t unstripe_width;
  14. sector_t unstripe_offset;
  15. uint32_t chunk_size;
  16. u8 chunk_shift;
  17. };
  18. #define DM_MSG_PREFIX "unstriped"
  19. static void cleanup_unstripe(struct unstripe_c *uc, struct dm_target *ti)
  20. {
  21. if (uc->dev)
  22. dm_put_device(ti, uc->dev);
  23. kfree(uc);
  24. }
  25. /*
  26. * Contruct an unstriped mapping.
  27. * <number of stripes> <chunk size> <stripe #> <dev_path> <offset>
  28. */
  29. static int unstripe_ctr(struct dm_target *ti, unsigned int argc, char **argv)
  30. {
  31. struct unstripe_c *uc;
  32. sector_t tmp_len;
  33. unsigned long long start;
  34. char dummy;
  35. if (argc != 5) {
  36. ti->error = "Invalid number of arguments";
  37. return -EINVAL;
  38. }
  39. uc = kzalloc(sizeof(*uc), GFP_KERNEL);
  40. if (!uc) {
  41. ti->error = "Memory allocation for unstriped context failed";
  42. return -ENOMEM;
  43. }
  44. if (kstrtouint(argv[0], 10, &uc->stripes) || !uc->stripes) {
  45. ti->error = "Invalid stripe count";
  46. goto err;
  47. }
  48. if (kstrtouint(argv[1], 10, &uc->chunk_size) || !uc->chunk_size) {
  49. ti->error = "Invalid chunk_size";
  50. goto err;
  51. }
  52. if (kstrtouint(argv[2], 10, &uc->unstripe)) {
  53. ti->error = "Invalid stripe number";
  54. goto err;
  55. }
  56. if (uc->unstripe > uc->stripes && uc->stripes > 1) {
  57. ti->error = "Please provide stripe between [0, # of stripes]";
  58. goto err;
  59. }
  60. if (dm_get_device(ti, argv[3], dm_table_get_mode(ti->table), &uc->dev)) {
  61. ti->error = "Couldn't get striped device";
  62. goto err;
  63. }
  64. if (sscanf(argv[4], "%llu%c", &start, &dummy) != 1 || start != (sector_t)start) {
  65. ti->error = "Invalid striped device offset";
  66. goto err;
  67. }
  68. uc->physical_start = start;
  69. uc->unstripe_offset = uc->unstripe * uc->chunk_size;
  70. uc->unstripe_width = (uc->stripes - 1) * uc->chunk_size;
  71. uc->chunk_shift = is_power_of_2(uc->chunk_size) ? fls(uc->chunk_size) - 1 : 0;
  72. tmp_len = ti->len;
  73. if (sector_div(tmp_len, uc->chunk_size)) {
  74. ti->error = "Target length not divisible by chunk size";
  75. goto err;
  76. }
  77. if (dm_set_target_max_io_len(ti, uc->chunk_size)) {
  78. ti->error = "Failed to set max io len";
  79. goto err;
  80. }
  81. ti->private = uc;
  82. return 0;
  83. err:
  84. cleanup_unstripe(uc, ti);
  85. return -EINVAL;
  86. }
  87. static void unstripe_dtr(struct dm_target *ti)
  88. {
  89. struct unstripe_c *uc = ti->private;
  90. cleanup_unstripe(uc, ti);
  91. }
  92. static sector_t map_to_core(struct dm_target *ti, struct bio *bio)
  93. {
  94. struct unstripe_c *uc = ti->private;
  95. sector_t sector = bio->bi_iter.bi_sector;
  96. sector_t tmp_sector = sector;
  97. /* Shift us up to the right "row" on the stripe */
  98. if (uc->chunk_shift)
  99. tmp_sector >>= uc->chunk_shift;
  100. else
  101. sector_div(tmp_sector, uc->chunk_size);
  102. sector += uc->unstripe_width * tmp_sector;
  103. /* Account for what stripe we're operating on */
  104. return sector + uc->unstripe_offset;
  105. }
  106. static int unstripe_map(struct dm_target *ti, struct bio *bio)
  107. {
  108. struct unstripe_c *uc = ti->private;
  109. bio_set_dev(bio, uc->dev->bdev);
  110. bio->bi_iter.bi_sector = map_to_core(ti, bio) + uc->physical_start;
  111. return DM_MAPIO_REMAPPED;
  112. }
  113. static void unstripe_status(struct dm_target *ti, status_type_t type,
  114. unsigned int status_flags, char *result, unsigned int maxlen)
  115. {
  116. struct unstripe_c *uc = ti->private;
  117. unsigned int sz = 0;
  118. switch (type) {
  119. case STATUSTYPE_INFO:
  120. break;
  121. case STATUSTYPE_TABLE:
  122. DMEMIT("%d %llu %d %s %llu",
  123. uc->stripes, (unsigned long long)uc->chunk_size, uc->unstripe,
  124. uc->dev->name, (unsigned long long)uc->physical_start);
  125. break;
  126. }
  127. }
  128. static int unstripe_iterate_devices(struct dm_target *ti,
  129. iterate_devices_callout_fn fn, void *data)
  130. {
  131. struct unstripe_c *uc = ti->private;
  132. return fn(ti, uc->dev, uc->physical_start, ti->len, data);
  133. }
  134. static void unstripe_io_hints(struct dm_target *ti,
  135. struct queue_limits *limits)
  136. {
  137. struct unstripe_c *uc = ti->private;
  138. limits->chunk_sectors = uc->chunk_size;
  139. }
  140. static struct target_type unstripe_target = {
  141. .name = "unstriped",
  142. .version = {1, 1, 0},
  143. .module = THIS_MODULE,
  144. .ctr = unstripe_ctr,
  145. .dtr = unstripe_dtr,
  146. .map = unstripe_map,
  147. .status = unstripe_status,
  148. .iterate_devices = unstripe_iterate_devices,
  149. .io_hints = unstripe_io_hints,
  150. };
  151. static int __init dm_unstripe_init(void)
  152. {
  153. return dm_register_target(&unstripe_target);
  154. }
  155. static void __exit dm_unstripe_exit(void)
  156. {
  157. dm_unregister_target(&unstripe_target);
  158. }
  159. module_init(dm_unstripe_init);
  160. module_exit(dm_unstripe_exit);
  161. MODULE_DESCRIPTION(DM_NAME " unstriped target");
  162. MODULE_ALIAS("dm-unstriped");
  163. MODULE_AUTHOR("Scott Bauer <scott.bauer@intel.com>");
  164. MODULE_LICENSE("GPL");