dm-stripe.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. #define DM_MSG_PREFIX "striped"
  13. struct stripe {
  14. struct dm_dev *dev;
  15. sector_t physical_start;
  16. };
  17. struct stripe_c {
  18. uint32_t stripes;
  19. /* The size of this target / num. stripes */
  20. sector_t stripe_width;
  21. /* stripe chunk size */
  22. uint32_t chunk_shift;
  23. sector_t chunk_mask;
  24. struct stripe stripe[0];
  25. };
  26. static inline struct stripe_c *alloc_context(unsigned int stripes)
  27. {
  28. size_t len;
  29. if (array_too_big(sizeof(struct stripe_c), sizeof(struct stripe),
  30. stripes))
  31. return NULL;
  32. len = sizeof(struct stripe_c) + (sizeof(struct stripe) * stripes);
  33. return kmalloc(len, GFP_KERNEL);
  34. }
  35. /*
  36. * Parse a single <dev> <sector> pair
  37. */
  38. static int get_stripe(struct dm_target *ti, struct stripe_c *sc,
  39. unsigned int stripe, char **argv)
  40. {
  41. unsigned long long start;
  42. if (sscanf(argv[1], "%llu", &start) != 1)
  43. return -EINVAL;
  44. if (dm_get_device(ti, argv[0], start, sc->stripe_width,
  45. dm_table_get_mode(ti->table),
  46. &sc->stripe[stripe].dev))
  47. return -ENXIO;
  48. sc->stripe[stripe].physical_start = start;
  49. return 0;
  50. }
  51. /*
  52. * Construct a striped mapping.
  53. * <number of stripes> <chunk size (2^^n)> [<dev_path> <offset>]+
  54. */
  55. static int stripe_ctr(struct dm_target *ti, unsigned int argc, char **argv)
  56. {
  57. struct stripe_c *sc;
  58. sector_t width;
  59. uint32_t stripes;
  60. uint32_t chunk_size;
  61. char *end;
  62. int r;
  63. unsigned int i;
  64. if (argc < 2) {
  65. ti->error = "Not enough arguments";
  66. return -EINVAL;
  67. }
  68. stripes = simple_strtoul(argv[0], &end, 10);
  69. if (*end) {
  70. ti->error = "Invalid stripe count";
  71. return -EINVAL;
  72. }
  73. chunk_size = simple_strtoul(argv[1], &end, 10);
  74. if (*end) {
  75. ti->error = "Invalid chunk_size";
  76. return -EINVAL;
  77. }
  78. /*
  79. * chunk_size is a power of two
  80. */
  81. if (!chunk_size || (chunk_size & (chunk_size - 1)) ||
  82. (chunk_size < (PAGE_SIZE >> SECTOR_SHIFT))) {
  83. ti->error = "Invalid chunk size";
  84. return -EINVAL;
  85. }
  86. if (ti->len & (chunk_size - 1)) {
  87. ti->error = "Target length not divisible by "
  88. "chunk size";
  89. return -EINVAL;
  90. }
  91. width = ti->len;
  92. if (sector_div(width, stripes)) {
  93. ti->error = "Target length not divisible by "
  94. "number of stripes";
  95. return -EINVAL;
  96. }
  97. /*
  98. * Do we have enough arguments for that many stripes ?
  99. */
  100. if (argc != (2 + 2 * stripes)) {
  101. ti->error = "Not enough destinations "
  102. "specified";
  103. return -EINVAL;
  104. }
  105. sc = alloc_context(stripes);
  106. if (!sc) {
  107. ti->error = "Memory allocation for striped context "
  108. "failed";
  109. return -ENOMEM;
  110. }
  111. sc->stripes = stripes;
  112. sc->stripe_width = width;
  113. ti->split_io = chunk_size;
  114. sc->chunk_mask = ((sector_t) chunk_size) - 1;
  115. for (sc->chunk_shift = 0; chunk_size; sc->chunk_shift++)
  116. chunk_size >>= 1;
  117. sc->chunk_shift--;
  118. /*
  119. * Get the stripe destinations.
  120. */
  121. for (i = 0; i < stripes; i++) {
  122. argv += 2;
  123. r = get_stripe(ti, sc, i, argv);
  124. if (r < 0) {
  125. ti->error = "Couldn't parse stripe 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("target registration failed");
  190. return r;
  191. }
  192. void dm_stripe_exit(void)
  193. {
  194. if (dm_unregister_target(&stripe_target))
  195. DMWARN("target unregistration failed");
  196. return;
  197. }