dm-stripe.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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_opf & REQ_PREFLUSH) {
  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_op(bio) == REQ_OP_DISCARD) ||
  241. unlikely(bio_op(bio) == REQ_OP_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. static long stripe_direct_access(struct dm_target *ti, sector_t sector,
  253. void **kaddr, pfn_t *pfn, long size)
  254. {
  255. struct stripe_c *sc = ti->private;
  256. uint32_t stripe;
  257. struct block_device *bdev;
  258. struct blk_dax_ctl dax = {
  259. .size = size,
  260. };
  261. long ret;
  262. stripe_map_sector(sc, sector, &stripe, &dax.sector);
  263. dax.sector += sc->stripe[stripe].physical_start;
  264. bdev = sc->stripe[stripe].dev->bdev;
  265. ret = bdev_direct_access(bdev, &dax);
  266. *kaddr = dax.addr;
  267. *pfn = dax.pfn;
  268. return ret;
  269. }
  270. /*
  271. * Stripe status:
  272. *
  273. * INFO
  274. * #stripes [stripe_name <stripe_name>] [group word count]
  275. * [error count 'A|D' <error count 'A|D'>]
  276. *
  277. * TABLE
  278. * #stripes [stripe chunk size]
  279. * [stripe_name physical_start <stripe_name physical_start>]
  280. *
  281. */
  282. static void stripe_status(struct dm_target *ti, status_type_t type,
  283. unsigned status_flags, char *result, unsigned maxlen)
  284. {
  285. struct stripe_c *sc = (struct stripe_c *) ti->private;
  286. char buffer[sc->stripes + 1];
  287. unsigned int sz = 0;
  288. unsigned int i;
  289. switch (type) {
  290. case STATUSTYPE_INFO:
  291. DMEMIT("%d ", sc->stripes);
  292. for (i = 0; i < sc->stripes; i++) {
  293. DMEMIT("%s ", sc->stripe[i].dev->name);
  294. buffer[i] = atomic_read(&(sc->stripe[i].error_count)) ?
  295. 'D' : 'A';
  296. }
  297. buffer[i] = '\0';
  298. DMEMIT("1 %s", buffer);
  299. break;
  300. case STATUSTYPE_TABLE:
  301. DMEMIT("%d %llu", sc->stripes,
  302. (unsigned long long)sc->chunk_size);
  303. for (i = 0; i < sc->stripes; i++)
  304. DMEMIT(" %s %llu", sc->stripe[i].dev->name,
  305. (unsigned long long)sc->stripe[i].physical_start);
  306. break;
  307. }
  308. }
  309. static int stripe_end_io(struct dm_target *ti, struct bio *bio, int error)
  310. {
  311. unsigned i;
  312. char major_minor[16];
  313. struct stripe_c *sc = ti->private;
  314. if (!error)
  315. return 0; /* I/O complete */
  316. if ((error == -EWOULDBLOCK) && (bio->bi_opf & REQ_RAHEAD))
  317. return error;
  318. if (error == -EOPNOTSUPP)
  319. return error;
  320. memset(major_minor, 0, sizeof(major_minor));
  321. sprintf(major_minor, "%d:%d",
  322. MAJOR(disk_devt(bio->bi_bdev->bd_disk)),
  323. MINOR(disk_devt(bio->bi_bdev->bd_disk)));
  324. /*
  325. * Test to see which stripe drive triggered the event
  326. * and increment error count for all stripes on that device.
  327. * If the error count for a given device exceeds the threshold
  328. * value we will no longer trigger any further events.
  329. */
  330. for (i = 0; i < sc->stripes; i++)
  331. if (!strcmp(sc->stripe[i].dev->name, major_minor)) {
  332. atomic_inc(&(sc->stripe[i].error_count));
  333. if (atomic_read(&(sc->stripe[i].error_count)) <
  334. DM_IO_ERROR_THRESHOLD)
  335. schedule_work(&sc->trigger_event);
  336. }
  337. return error;
  338. }
  339. static int stripe_iterate_devices(struct dm_target *ti,
  340. iterate_devices_callout_fn fn, void *data)
  341. {
  342. struct stripe_c *sc = ti->private;
  343. int ret = 0;
  344. unsigned i = 0;
  345. do {
  346. ret = fn(ti, sc->stripe[i].dev,
  347. sc->stripe[i].physical_start,
  348. sc->stripe_width, data);
  349. } while (!ret && ++i < sc->stripes);
  350. return ret;
  351. }
  352. static void stripe_io_hints(struct dm_target *ti,
  353. struct queue_limits *limits)
  354. {
  355. struct stripe_c *sc = ti->private;
  356. unsigned chunk_size = sc->chunk_size << SECTOR_SHIFT;
  357. blk_limits_io_min(limits, chunk_size);
  358. blk_limits_io_opt(limits, chunk_size * sc->stripes);
  359. }
  360. static struct target_type stripe_target = {
  361. .name = "striped",
  362. .version = {1, 6, 0},
  363. .module = THIS_MODULE,
  364. .ctr = stripe_ctr,
  365. .dtr = stripe_dtr,
  366. .map = stripe_map,
  367. .end_io = stripe_end_io,
  368. .status = stripe_status,
  369. .iterate_devices = stripe_iterate_devices,
  370. .io_hints = stripe_io_hints,
  371. .direct_access = stripe_direct_access,
  372. };
  373. int __init dm_stripe_init(void)
  374. {
  375. int r;
  376. r = dm_register_target(&stripe_target);
  377. if (r < 0)
  378. DMWARN("target registration failed");
  379. return r;
  380. }
  381. void dm_stripe_exit(void)
  382. {
  383. dm_unregister_target(&stripe_target);
  384. }