dm-stripe.c 4.9 KB

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