dm-stripe.c 8.6 KB

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