dm-stripe.c 10 KB

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