dm-stripe.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /*
  2. * Copyright (C) 2001-2003 Sistina Software (UK) Limited.
  3. *
  4. * This file is released under the GPL.
  5. */
  6. #include <linux/device-mapper.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. #define DM_IO_ERROR_THRESHOLD 15
  15. struct stripe {
  16. struct dm_dev *dev;
  17. sector_t physical_start;
  18. atomic_t error_count;
  19. };
  20. struct stripe_c {
  21. uint32_t stripes;
  22. /* The size of this target / num. stripes */
  23. sector_t stripe_width;
  24. /* stripe chunk size */
  25. uint32_t chunk_shift;
  26. sector_t chunk_mask;
  27. /* Needed for handling events */
  28. struct dm_target *ti;
  29. /* Work struct used for triggering events*/
  30. struct work_struct kstriped_ws;
  31. struct stripe stripe[0];
  32. };
  33. static struct workqueue_struct *kstriped;
  34. /*
  35. * An event is triggered whenever a drive
  36. * drops out of a stripe volume.
  37. */
  38. static void trigger_event(struct work_struct *work)
  39. {
  40. struct stripe_c *sc = container_of(work, struct stripe_c, kstriped_ws);
  41. dm_table_event(sc->ti->table);
  42. }
  43. static inline struct stripe_c *alloc_context(unsigned int stripes)
  44. {
  45. size_t len;
  46. if (dm_array_too_big(sizeof(struct stripe_c), sizeof(struct stripe),
  47. stripes))
  48. return NULL;
  49. len = sizeof(struct stripe_c) + (sizeof(struct stripe) * stripes);
  50. return kmalloc(len, GFP_KERNEL);
  51. }
  52. /*
  53. * Parse a single <dev> <sector> pair
  54. */
  55. static int get_stripe(struct dm_target *ti, struct stripe_c *sc,
  56. unsigned int stripe, char **argv)
  57. {
  58. unsigned long long start;
  59. if (sscanf(argv[1], "%llu", &start) != 1)
  60. return -EINVAL;
  61. if (dm_get_device(ti, argv[0], start, sc->stripe_width,
  62. dm_table_get_mode(ti->table),
  63. &sc->stripe[stripe].dev))
  64. return -ENXIO;
  65. sc->stripe[stripe].physical_start = start;
  66. return 0;
  67. }
  68. /*
  69. * Construct a striped mapping.
  70. * <number of stripes> <chunk size (2^^n)> [<dev_path> <offset>]+
  71. */
  72. static int stripe_ctr(struct dm_target *ti, unsigned int argc, char **argv)
  73. {
  74. struct stripe_c *sc;
  75. sector_t width;
  76. uint32_t stripes;
  77. uint32_t chunk_size;
  78. char *end;
  79. int r;
  80. unsigned int i;
  81. if (argc < 2) {
  82. ti->error = "Not enough arguments";
  83. return -EINVAL;
  84. }
  85. stripes = simple_strtoul(argv[0], &end, 10);
  86. if (*end) {
  87. ti->error = "Invalid stripe count";
  88. return -EINVAL;
  89. }
  90. chunk_size = simple_strtoul(argv[1], &end, 10);
  91. if (*end) {
  92. ti->error = "Invalid chunk_size";
  93. return -EINVAL;
  94. }
  95. /*
  96. * chunk_size is a power of two
  97. */
  98. if (!is_power_of_2(chunk_size) ||
  99. (chunk_size < (PAGE_SIZE >> SECTOR_SHIFT))) {
  100. ti->error = "Invalid chunk size";
  101. return -EINVAL;
  102. }
  103. if (ti->len & (chunk_size - 1)) {
  104. ti->error = "Target length not divisible by "
  105. "chunk size";
  106. return -EINVAL;
  107. }
  108. width = ti->len;
  109. if (sector_div(width, stripes)) {
  110. ti->error = "Target length not divisible by "
  111. "number of stripes";
  112. return -EINVAL;
  113. }
  114. /*
  115. * Do we have enough arguments for that many stripes ?
  116. */
  117. if (argc != (2 + 2 * stripes)) {
  118. ti->error = "Not enough destinations "
  119. "specified";
  120. return -EINVAL;
  121. }
  122. sc = alloc_context(stripes);
  123. if (!sc) {
  124. ti->error = "Memory allocation for striped context "
  125. "failed";
  126. return -ENOMEM;
  127. }
  128. INIT_WORK(&sc->kstriped_ws, trigger_event);
  129. /* Set pointer to dm target; used in trigger_event */
  130. sc->ti = ti;
  131. sc->stripes = stripes;
  132. sc->stripe_width = width;
  133. ti->split_io = chunk_size;
  134. ti->num_flush_requests = stripes;
  135. sc->chunk_mask = ((sector_t) chunk_size) - 1;
  136. for (sc->chunk_shift = 0; chunk_size; sc->chunk_shift++)
  137. chunk_size >>= 1;
  138. sc->chunk_shift--;
  139. /*
  140. * Get the stripe destinations.
  141. */
  142. for (i = 0; i < stripes; i++) {
  143. argv += 2;
  144. r = get_stripe(ti, sc, i, argv);
  145. if (r < 0) {
  146. ti->error = "Couldn't parse stripe destination";
  147. while (i--)
  148. dm_put_device(ti, sc->stripe[i].dev);
  149. kfree(sc);
  150. return r;
  151. }
  152. atomic_set(&(sc->stripe[i].error_count), 0);
  153. }
  154. ti->private = sc;
  155. return 0;
  156. }
  157. static void stripe_dtr(struct dm_target *ti)
  158. {
  159. unsigned int i;
  160. struct stripe_c *sc = (struct stripe_c *) ti->private;
  161. for (i = 0; i < sc->stripes; i++)
  162. dm_put_device(ti, sc->stripe[i].dev);
  163. flush_workqueue(kstriped);
  164. kfree(sc);
  165. }
  166. static int stripe_map(struct dm_target *ti, struct bio *bio,
  167. union map_info *map_context)
  168. {
  169. struct stripe_c *sc = (struct stripe_c *) ti->private;
  170. sector_t offset, chunk;
  171. uint32_t stripe;
  172. if (unlikely(bio_empty_barrier(bio))) {
  173. BUG_ON(map_context->flush_request >= sc->stripes);
  174. bio->bi_bdev = sc->stripe[map_context->flush_request].dev->bdev;
  175. return DM_MAPIO_REMAPPED;
  176. }
  177. offset = bio->bi_sector - ti->begin;
  178. chunk = offset >> sc->chunk_shift;
  179. stripe = sector_div(chunk, sc->stripes);
  180. bio->bi_bdev = sc->stripe[stripe].dev->bdev;
  181. bio->bi_sector = sc->stripe[stripe].physical_start +
  182. (chunk << sc->chunk_shift) + (offset & sc->chunk_mask);
  183. return DM_MAPIO_REMAPPED;
  184. }
  185. /*
  186. * Stripe status:
  187. *
  188. * INFO
  189. * #stripes [stripe_name <stripe_name>] [group word count]
  190. * [error count 'A|D' <error count 'A|D'>]
  191. *
  192. * TABLE
  193. * #stripes [stripe chunk size]
  194. * [stripe_name physical_start <stripe_name physical_start>]
  195. *
  196. */
  197. static int stripe_status(struct dm_target *ti,
  198. status_type_t type, char *result, unsigned int maxlen)
  199. {
  200. struct stripe_c *sc = (struct stripe_c *) ti->private;
  201. char buffer[sc->stripes + 1];
  202. unsigned int sz = 0;
  203. unsigned int i;
  204. switch (type) {
  205. case STATUSTYPE_INFO:
  206. DMEMIT("%d ", sc->stripes);
  207. for (i = 0; i < sc->stripes; i++) {
  208. DMEMIT("%s ", sc->stripe[i].dev->name);
  209. buffer[i] = atomic_read(&(sc->stripe[i].error_count)) ?
  210. 'D' : 'A';
  211. }
  212. buffer[i] = '\0';
  213. DMEMIT("1 %s", buffer);
  214. break;
  215. case STATUSTYPE_TABLE:
  216. DMEMIT("%d %llu", sc->stripes,
  217. (unsigned long long)sc->chunk_mask + 1);
  218. for (i = 0; i < sc->stripes; i++)
  219. DMEMIT(" %s %llu", sc->stripe[i].dev->name,
  220. (unsigned long long)sc->stripe[i].physical_start);
  221. break;
  222. }
  223. return 0;
  224. }
  225. static int stripe_end_io(struct dm_target *ti, struct bio *bio,
  226. int error, union map_info *map_context)
  227. {
  228. unsigned i;
  229. char major_minor[16];
  230. struct stripe_c *sc = ti->private;
  231. if (!error)
  232. return 0; /* I/O complete */
  233. if ((error == -EWOULDBLOCK) && bio_rw_ahead(bio))
  234. return error;
  235. if (error == -EOPNOTSUPP)
  236. return error;
  237. memset(major_minor, 0, sizeof(major_minor));
  238. sprintf(major_minor, "%d:%d",
  239. MAJOR(disk_devt(bio->bi_bdev->bd_disk)),
  240. MINOR(disk_devt(bio->bi_bdev->bd_disk)));
  241. /*
  242. * Test to see which stripe drive triggered the event
  243. * and increment error count for all stripes on that device.
  244. * If the error count for a given device exceeds the threshold
  245. * value we will no longer trigger any further events.
  246. */
  247. for (i = 0; i < sc->stripes; i++)
  248. if (!strcmp(sc->stripe[i].dev->name, major_minor)) {
  249. atomic_inc(&(sc->stripe[i].error_count));
  250. if (atomic_read(&(sc->stripe[i].error_count)) <
  251. DM_IO_ERROR_THRESHOLD)
  252. queue_work(kstriped, &sc->kstriped_ws);
  253. }
  254. return error;
  255. }
  256. static int stripe_iterate_devices(struct dm_target *ti,
  257. iterate_devices_callout_fn fn, void *data)
  258. {
  259. struct stripe_c *sc = ti->private;
  260. int ret = 0;
  261. unsigned i = 0;
  262. do
  263. ret = fn(ti, sc->stripe[i].dev,
  264. sc->stripe[i].physical_start, data);
  265. while (!ret && ++i < sc->stripes);
  266. return ret;
  267. }
  268. static struct target_type stripe_target = {
  269. .name = "striped",
  270. .version = {1, 2, 0},
  271. .module = THIS_MODULE,
  272. .ctr = stripe_ctr,
  273. .dtr = stripe_dtr,
  274. .map = stripe_map,
  275. .end_io = stripe_end_io,
  276. .status = stripe_status,
  277. .iterate_devices = stripe_iterate_devices,
  278. };
  279. int __init dm_stripe_init(void)
  280. {
  281. int r;
  282. r = dm_register_target(&stripe_target);
  283. if (r < 0) {
  284. DMWARN("target registration failed");
  285. return r;
  286. }
  287. kstriped = create_singlethread_workqueue("kstriped");
  288. if (!kstriped) {
  289. DMERR("failed to create workqueue kstriped");
  290. dm_unregister_target(&stripe_target);
  291. return -ENOMEM;
  292. }
  293. return r;
  294. }
  295. void dm_stripe_exit(void)
  296. {
  297. dm_unregister_target(&stripe_target);
  298. destroy_workqueue(kstriped);
  299. return;
  300. }