dm-stripe.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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/device-mapper.h>
  8. #include <linux/module.h>
  9. #include <linux/init.h>
  10. #include <linux/blkdev.h>
  11. #include <linux/bio.h>
  12. #include <linux/slab.h>
  13. #include <linux/log2.h>
  14. #define DM_MSG_PREFIX "striped"
  15. #define DM_IO_ERROR_THRESHOLD 15
  16. struct stripe {
  17. struct dm_dev *dev;
  18. sector_t physical_start;
  19. atomic_t error_count;
  20. };
  21. struct stripe_c {
  22. uint32_t stripes;
  23. int stripes_shift;
  24. /* The size of this target / num. stripes */
  25. sector_t stripe_width;
  26. uint32_t chunk_size;
  27. int chunk_size_shift;
  28. /* Needed for handling events */
  29. struct dm_target *ti;
  30. /* Work struct used for triggering events*/
  31. struct work_struct trigger_event;
  32. struct stripe stripe[0];
  33. };
  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,
  41. trigger_event);
  42. dm_table_event(sc->ti->table);
  43. }
  44. static inline struct stripe_c *alloc_context(unsigned int stripes)
  45. {
  46. size_t len;
  47. if (dm_array_too_big(sizeof(struct stripe_c), sizeof(struct stripe),
  48. stripes))
  49. return NULL;
  50. len = sizeof(struct stripe_c) + (sizeof(struct stripe) * stripes);
  51. return kmalloc(len, GFP_KERNEL);
  52. }
  53. /*
  54. * Parse a single <dev> <sector> pair
  55. */
  56. static int get_stripe(struct dm_target *ti, struct stripe_c *sc,
  57. unsigned int stripe, char **argv)
  58. {
  59. unsigned long long start;
  60. char dummy;
  61. int ret;
  62. if (sscanf(argv[1], "%llu%c", &start, &dummy) != 1)
  63. return -EINVAL;
  64. ret = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table),
  65. &sc->stripe[stripe].dev);
  66. if (ret)
  67. return ret;
  68. sc->stripe[stripe].physical_start = start;
  69. return 0;
  70. }
  71. /*
  72. * Construct a striped mapping.
  73. * <number of stripes> <chunk size> [<dev_path> <offset>]+
  74. */
  75. static int stripe_ctr(struct dm_target *ti, unsigned int argc, char **argv)
  76. {
  77. struct stripe_c *sc;
  78. sector_t width, tmp_len;
  79. uint32_t stripes;
  80. uint32_t chunk_size;
  81. int r;
  82. unsigned int i;
  83. if (argc < 2) {
  84. ti->error = "Not enough arguments";
  85. return -EINVAL;
  86. }
  87. if (kstrtouint(argv[0], 10, &stripes) || !stripes) {
  88. ti->error = "Invalid stripe count";
  89. return -EINVAL;
  90. }
  91. if (kstrtouint(argv[1], 10, &chunk_size) || !chunk_size) {
  92. ti->error = "Invalid chunk_size";
  93. return -EINVAL;
  94. }
  95. width = ti->len;
  96. if (sector_div(width, stripes)) {
  97. ti->error = "Target length not divisible by "
  98. "number of stripes";
  99. return -EINVAL;
  100. }
  101. tmp_len = width;
  102. if (sector_div(tmp_len, chunk_size)) {
  103. ti->error = "Target length not divisible by "
  104. "chunk size";
  105. return -EINVAL;
  106. }
  107. /*
  108. * Do we have enough arguments for that many stripes ?
  109. */
  110. if (argc != (2 + 2 * stripes)) {
  111. ti->error = "Not enough destinations "
  112. "specified";
  113. return -EINVAL;
  114. }
  115. sc = alloc_context(stripes);
  116. if (!sc) {
  117. ti->error = "Memory allocation for striped context "
  118. "failed";
  119. return -ENOMEM;
  120. }
  121. INIT_WORK(&sc->trigger_event, trigger_event);
  122. /* Set pointer to dm target; used in trigger_event */
  123. sc->ti = ti;
  124. sc->stripes = stripes;
  125. sc->stripe_width = width;
  126. if (stripes & (stripes - 1))
  127. sc->stripes_shift = -1;
  128. else
  129. sc->stripes_shift = __ffs(stripes);
  130. r = dm_set_target_max_io_len(ti, chunk_size);
  131. if (r) {
  132. kfree(sc);
  133. return r;
  134. }
  135. ti->num_flush_bios = stripes;
  136. ti->num_discard_bios = stripes;
  137. ti->num_write_same_bios = stripes;
  138. sc->chunk_size = chunk_size;
  139. if (chunk_size & (chunk_size - 1))
  140. sc->chunk_size_shift = -1;
  141. else
  142. sc->chunk_size_shift = __ffs(chunk_size);
  143. /*
  144. * Get the stripe destinations.
  145. */
  146. for (i = 0; i < stripes; i++) {
  147. argv += 2;
  148. r = get_stripe(ti, sc, i, argv);
  149. if (r < 0) {
  150. ti->error = "Couldn't parse stripe destination";
  151. while (i--)
  152. dm_put_device(ti, sc->stripe[i].dev);
  153. kfree(sc);
  154. return r;
  155. }
  156. atomic_set(&(sc->stripe[i].error_count), 0);
  157. }
  158. ti->private = sc;
  159. return 0;
  160. }
  161. static void stripe_dtr(struct dm_target *ti)
  162. {
  163. unsigned int i;
  164. struct stripe_c *sc = (struct stripe_c *) ti->private;
  165. for (i = 0; i < sc->stripes; i++)
  166. dm_put_device(ti, sc->stripe[i].dev);
  167. flush_work(&sc->trigger_event);
  168. kfree(sc);
  169. }
  170. static void stripe_map_sector(struct stripe_c *sc, sector_t sector,
  171. uint32_t *stripe, sector_t *result)
  172. {
  173. sector_t chunk = dm_target_offset(sc->ti, sector);
  174. sector_t chunk_offset;
  175. if (sc->chunk_size_shift < 0)
  176. chunk_offset = sector_div(chunk, sc->chunk_size);
  177. else {
  178. chunk_offset = chunk & (sc->chunk_size - 1);
  179. chunk >>= sc->chunk_size_shift;
  180. }
  181. if (sc->stripes_shift < 0)
  182. *stripe = sector_div(chunk, sc->stripes);
  183. else {
  184. *stripe = chunk & (sc->stripes - 1);
  185. chunk >>= sc->stripes_shift;
  186. }
  187. if (sc->chunk_size_shift < 0)
  188. chunk *= sc->chunk_size;
  189. else
  190. chunk <<= sc->chunk_size_shift;
  191. *result = chunk + chunk_offset;
  192. }
  193. static void stripe_map_range_sector(struct stripe_c *sc, sector_t sector,
  194. uint32_t target_stripe, sector_t *result)
  195. {
  196. uint32_t stripe;
  197. stripe_map_sector(sc, sector, &stripe, result);
  198. if (stripe == target_stripe)
  199. return;
  200. /* round down */
  201. sector = *result;
  202. if (sc->chunk_size_shift < 0)
  203. *result -= sector_div(sector, sc->chunk_size);
  204. else
  205. *result = sector & ~(sector_t)(sc->chunk_size - 1);
  206. if (target_stripe < stripe)
  207. *result += sc->chunk_size; /* next chunk */
  208. }
  209. static int stripe_map_range(struct stripe_c *sc, struct bio *bio,
  210. uint32_t target_stripe)
  211. {
  212. sector_t begin, end;
  213. stripe_map_range_sector(sc, bio->bi_iter.bi_sector,
  214. target_stripe, &begin);
  215. stripe_map_range_sector(sc, bio_end_sector(bio),
  216. target_stripe, &end);
  217. if (begin < end) {
  218. bio->bi_bdev = sc->stripe[target_stripe].dev->bdev;
  219. bio->bi_iter.bi_sector = begin +
  220. sc->stripe[target_stripe].physical_start;
  221. bio->bi_iter.bi_size = to_bytes(end - begin);
  222. return DM_MAPIO_REMAPPED;
  223. } else {
  224. /* The range doesn't map to the target stripe */
  225. bio_endio(bio);
  226. return DM_MAPIO_SUBMITTED;
  227. }
  228. }
  229. static int stripe_map(struct dm_target *ti, struct bio *bio)
  230. {
  231. struct stripe_c *sc = ti->private;
  232. uint32_t stripe;
  233. unsigned target_bio_nr;
  234. if (bio->bi_rw & REQ_FLUSH) {
  235. target_bio_nr = dm_bio_get_target_bio_nr(bio);
  236. BUG_ON(target_bio_nr >= sc->stripes);
  237. bio->bi_bdev = sc->stripe[target_bio_nr].dev->bdev;
  238. return DM_MAPIO_REMAPPED;
  239. }
  240. if (unlikely(bio->bi_rw & REQ_DISCARD) ||
  241. unlikely(bio->bi_rw & REQ_WRITE_SAME)) {
  242. target_bio_nr = dm_bio_get_target_bio_nr(bio);
  243. BUG_ON(target_bio_nr >= sc->stripes);
  244. return stripe_map_range(sc, bio, target_bio_nr);
  245. }
  246. stripe_map_sector(sc, bio->bi_iter.bi_sector,
  247. &stripe, &bio->bi_iter.bi_sector);
  248. bio->bi_iter.bi_sector += sc->stripe[stripe].physical_start;
  249. bio->bi_bdev = sc->stripe[stripe].dev->bdev;
  250. return DM_MAPIO_REMAPPED;
  251. }
  252. /*
  253. * Stripe status:
  254. *
  255. * INFO
  256. * #stripes [stripe_name <stripe_name>] [group word count]
  257. * [error count 'A|D' <error count 'A|D'>]
  258. *
  259. * TABLE
  260. * #stripes [stripe chunk size]
  261. * [stripe_name physical_start <stripe_name physical_start>]
  262. *
  263. */
  264. static void stripe_status(struct dm_target *ti, status_type_t type,
  265. unsigned status_flags, char *result, unsigned maxlen)
  266. {
  267. struct stripe_c *sc = (struct stripe_c *) ti->private;
  268. char buffer[sc->stripes + 1];
  269. unsigned int sz = 0;
  270. unsigned int i;
  271. switch (type) {
  272. case STATUSTYPE_INFO:
  273. DMEMIT("%d ", sc->stripes);
  274. for (i = 0; i < sc->stripes; i++) {
  275. DMEMIT("%s ", sc->stripe[i].dev->name);
  276. buffer[i] = atomic_read(&(sc->stripe[i].error_count)) ?
  277. 'D' : 'A';
  278. }
  279. buffer[i] = '\0';
  280. DMEMIT("1 %s", buffer);
  281. break;
  282. case STATUSTYPE_TABLE:
  283. DMEMIT("%d %llu", sc->stripes,
  284. (unsigned long long)sc->chunk_size);
  285. for (i = 0; i < sc->stripes; i++)
  286. DMEMIT(" %s %llu", sc->stripe[i].dev->name,
  287. (unsigned long long)sc->stripe[i].physical_start);
  288. break;
  289. }
  290. }
  291. static int stripe_end_io(struct dm_target *ti, struct bio *bio, int error)
  292. {
  293. unsigned i;
  294. char major_minor[16];
  295. struct stripe_c *sc = ti->private;
  296. if (!error)
  297. return 0; /* I/O complete */
  298. if ((error == -EWOULDBLOCK) && (bio->bi_rw & REQ_RAHEAD))
  299. return error;
  300. if (error == -EOPNOTSUPP)
  301. return error;
  302. memset(major_minor, 0, sizeof(major_minor));
  303. sprintf(major_minor, "%d:%d",
  304. MAJOR(disk_devt(bio->bi_bdev->bd_disk)),
  305. MINOR(disk_devt(bio->bi_bdev->bd_disk)));
  306. /*
  307. * Test to see which stripe drive triggered the event
  308. * and increment error count for all stripes on that device.
  309. * If the error count for a given device exceeds the threshold
  310. * value we will no longer trigger any further events.
  311. */
  312. for (i = 0; i < sc->stripes; i++)
  313. if (!strcmp(sc->stripe[i].dev->name, major_minor)) {
  314. atomic_inc(&(sc->stripe[i].error_count));
  315. if (atomic_read(&(sc->stripe[i].error_count)) <
  316. DM_IO_ERROR_THRESHOLD)
  317. schedule_work(&sc->trigger_event);
  318. }
  319. return error;
  320. }
  321. static int stripe_iterate_devices(struct dm_target *ti,
  322. iterate_devices_callout_fn fn, void *data)
  323. {
  324. struct stripe_c *sc = ti->private;
  325. int ret = 0;
  326. unsigned i = 0;
  327. do {
  328. ret = fn(ti, sc->stripe[i].dev,
  329. sc->stripe[i].physical_start,
  330. sc->stripe_width, data);
  331. } while (!ret && ++i < sc->stripes);
  332. return ret;
  333. }
  334. static void stripe_io_hints(struct dm_target *ti,
  335. struct queue_limits *limits)
  336. {
  337. struct stripe_c *sc = ti->private;
  338. unsigned chunk_size = sc->chunk_size << SECTOR_SHIFT;
  339. blk_limits_io_min(limits, chunk_size);
  340. blk_limits_io_opt(limits, chunk_size * sc->stripes);
  341. }
  342. static struct target_type stripe_target = {
  343. .name = "striped",
  344. .version = {1, 5, 1},
  345. .module = THIS_MODULE,
  346. .ctr = stripe_ctr,
  347. .dtr = stripe_dtr,
  348. .map = stripe_map,
  349. .end_io = stripe_end_io,
  350. .status = stripe_status,
  351. .iterate_devices = stripe_iterate_devices,
  352. .io_hints = stripe_io_hints,
  353. };
  354. int __init dm_stripe_init(void)
  355. {
  356. int r;
  357. r = dm_register_target(&stripe_target);
  358. if (r < 0)
  359. DMWARN("target registration failed");
  360. return r;
  361. }
  362. void dm_stripe_exit(void)
  363. {
  364. dm_unregister_target(&stripe_target);
  365. }