dm-unstripe.c 4.5 KB

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