dm-stripe.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * Copyright (C) 2001-2003 Sistina Software (UK) Limited.
  3. *
  4. * This file is released under the GPL.
  5. */
  6. #include "dm.h"
  7. #include <linux/module.h>
  8. #include <linux/init.h>
  9. #include <linux/blkdev.h>
  10. #include <linux/bio.h>
  11. #include <linux/slab.h>
  12. #include <linux/log2.h>
  13. #define DM_MSG_PREFIX "striped"
  14. struct stripe {
  15. struct dm_dev *dev;
  16. sector_t physical_start;
  17. };
  18. struct stripe_c {
  19. uint32_t stripes;
  20. /* The size of this target / num. stripes */
  21. sector_t stripe_width;
  22. /* stripe chunk size */
  23. uint32_t chunk_shift;
  24. sector_t chunk_mask;
  25. struct stripe stripe[0];
  26. };
  27. static inline struct stripe_c *alloc_context(unsigned int stripes)
  28. {
  29. size_t len;
  30. if (array_too_big(sizeof(struct stripe_c), sizeof(struct stripe),
  31. stripes))
  32. return NULL;
  33. len = sizeof(struct stripe_c) + (sizeof(struct stripe) * stripes);
  34. return kmalloc(len, GFP_KERNEL);
  35. }
  36. /*
  37. * Parse a single <dev> <sector> pair
  38. */
  39. static int get_stripe(struct dm_target *ti, struct stripe_c *sc,
  40. unsigned int stripe, char **argv)
  41. {
  42. unsigned long long start;
  43. if (sscanf(argv[1], "%llu", &start) != 1)
  44. return -EINVAL;
  45. if (dm_get_device(ti, argv[0], start, sc->stripe_width,
  46. dm_table_get_mode(ti->table),
  47. &sc->stripe[stripe].dev))
  48. return -ENXIO;
  49. sc->stripe[stripe].physical_start = start;
  50. return 0;
  51. }
  52. /*
  53. * Construct a striped mapping.
  54. * <number of stripes> <chunk size (2^^n)> [<dev_path> <offset>]+
  55. */
  56. static int stripe_ctr(struct dm_target *ti, unsigned int argc, char **argv)
  57. {
  58. struct stripe_c *sc;
  59. sector_t width;
  60. uint32_t stripes;
  61. uint32_t chunk_size;
  62. char *end;
  63. int r;
  64. unsigned int i;
  65. if (argc < 2) {
  66. ti->error = "Not enough arguments";
  67. return -EINVAL;
  68. }
  69. stripes = simple_strtoul(argv[0], &end, 10);
  70. if (*end) {
  71. ti->error = "Invalid stripe count";
  72. return -EINVAL;
  73. }
  74. chunk_size = simple_strtoul(argv[1], &end, 10);
  75. if (*end) {
  76. ti->error = "Invalid chunk_size";
  77. return -EINVAL;
  78. }
  79. /*
  80. * chunk_size is a power of two
  81. */
  82. if (!is_power_of_2(chunk_size) ||
  83. (chunk_size < (PAGE_SIZE >> SECTOR_SHIFT))) {
  84. ti->error = "Invalid chunk size";
  85. return -EINVAL;
  86. }
  87. if (ti->len & (chunk_size - 1)) {
  88. ti->error = "Target length not divisible by "
  89. "chunk size";
  90. return -EINVAL;
  91. }
  92. width = ti->len;
  93. if (sector_div(width, stripes)) {
  94. ti->error = "Target length not divisible by "
  95. "number of stripes";
  96. return -EINVAL;
  97. }
  98. /*
  99. * Do we have enough arguments for that many stripes ?
  100. */
  101. if (argc != (2 + 2 * stripes)) {
  102. ti->error = "Not enough destinations "
  103. "specified";
  104. return -EINVAL;
  105. }
  106. sc = alloc_context(stripes);
  107. if (!sc) {
  108. ti->error = "Memory allocation for striped context "
  109. "failed";
  110. return -ENOMEM;
  111. }
  112. sc->stripes = stripes;
  113. sc->stripe_width = width;
  114. ti->split_io = chunk_size;
  115. sc->chunk_mask = ((sector_t) chunk_size) - 1;
  116. for (sc->chunk_shift = 0; chunk_size; sc->chunk_shift++)
  117. chunk_size >>= 1;
  118. sc->chunk_shift--;
  119. /*
  120. * Get the stripe destinations.
  121. */
  122. for (i = 0; i < stripes; i++) {
  123. argv += 2;
  124. r = get_stripe(ti, sc, i, argv);
  125. if (r < 0) {
  126. ti->error = "Couldn't parse stripe destination";
  127. while (i--)
  128. dm_put_device(ti, sc->stripe[i].dev);
  129. kfree(sc);
  130. return r;
  131. }
  132. }
  133. ti->private = sc;
  134. return 0;
  135. }
  136. static void stripe_dtr(struct dm_target *ti)
  137. {
  138. unsigned int i;
  139. struct stripe_c *sc = (struct stripe_c *) ti->private;
  140. for (i = 0; i < sc->stripes; i++)
  141. dm_put_device(ti, sc->stripe[i].dev);
  142. kfree(sc);
  143. }
  144. static int stripe_map(struct dm_target *ti, struct bio *bio,
  145. union map_info *map_context)
  146. {
  147. struct stripe_c *sc = (struct stripe_c *) ti->private;
  148. sector_t offset = bio->bi_sector - ti->begin;
  149. sector_t chunk = offset >> sc->chunk_shift;
  150. uint32_t stripe = sector_div(chunk, sc->stripes);
  151. bio->bi_bdev = sc->stripe[stripe].dev->bdev;
  152. bio->bi_sector = sc->stripe[stripe].physical_start +
  153. (chunk << sc->chunk_shift) + (offset & sc->chunk_mask);
  154. return DM_MAPIO_REMAPPED;
  155. }
  156. static int stripe_status(struct dm_target *ti,
  157. status_type_t type, char *result, unsigned int maxlen)
  158. {
  159. struct stripe_c *sc = (struct stripe_c *) ti->private;
  160. unsigned int sz = 0;
  161. unsigned int i;
  162. switch (type) {
  163. case STATUSTYPE_INFO:
  164. result[0] = '\0';
  165. break;
  166. case STATUSTYPE_TABLE:
  167. DMEMIT("%d %llu", sc->stripes,
  168. (unsigned long long)sc->chunk_mask + 1);
  169. for (i = 0; i < sc->stripes; i++)
  170. DMEMIT(" %s %llu", sc->stripe[i].dev->name,
  171. (unsigned long long)sc->stripe[i].physical_start);
  172. break;
  173. }
  174. return 0;
  175. }
  176. static struct target_type stripe_target = {
  177. .name = "striped",
  178. .version= {1, 0, 2},
  179. .module = THIS_MODULE,
  180. .ctr = stripe_ctr,
  181. .dtr = stripe_dtr,
  182. .map = stripe_map,
  183. .status = stripe_status,
  184. };
  185. int __init dm_stripe_init(void)
  186. {
  187. int r;
  188. r = dm_register_target(&stripe_target);
  189. if (r < 0)
  190. DMWARN("target registration failed");
  191. return r;
  192. }
  193. void dm_stripe_exit(void)
  194. {
  195. if (dm_unregister_target(&stripe_target))
  196. DMWARN("target unregistration failed");
  197. return;
  198. }