dm-stripe.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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/dax.h>
  13. #include <linux/slab.h>
  14. #include <linux/log2.h>
  15. #define DM_MSG_PREFIX "striped"
  16. #define DM_IO_ERROR_THRESHOLD 15
  17. struct stripe {
  18. struct dm_dev *dev;
  19. sector_t physical_start;
  20. atomic_t error_count;
  21. };
  22. struct stripe_c {
  23. uint32_t stripes;
  24. int stripes_shift;
  25. /* The size of this target / num. stripes */
  26. sector_t stripe_width;
  27. uint32_t chunk_size;
  28. int chunk_size_shift;
  29. /* Needed for handling events */
  30. struct dm_target *ti;
  31. /* Work struct used for triggering events*/
  32. struct work_struct trigger_event;
  33. struct stripe stripe[0];
  34. };
  35. /*
  36. * An event is triggered whenever a drive
  37. * drops out of a stripe volume.
  38. */
  39. static void trigger_event(struct work_struct *work)
  40. {
  41. struct stripe_c *sc = container_of(work, struct stripe_c,
  42. trigger_event);
  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. char dummy;
  62. int ret;
  63. if (sscanf(argv[1], "%llu%c", &start, &dummy) != 1)
  64. return -EINVAL;
  65. ret = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table),
  66. &sc->stripe[stripe].dev);
  67. if (ret)
  68. return ret;
  69. sc->stripe[stripe].physical_start = start;
  70. return 0;
  71. }
  72. /*
  73. * Construct a striped mapping.
  74. * <number of stripes> <chunk size> [<dev_path> <offset>]+
  75. */
  76. static int stripe_ctr(struct dm_target *ti, unsigned int argc, char **argv)
  77. {
  78. struct stripe_c *sc;
  79. sector_t width, tmp_len;
  80. uint32_t stripes;
  81. uint32_t chunk_size;
  82. int r;
  83. unsigned int i;
  84. if (argc < 2) {
  85. ti->error = "Not enough arguments";
  86. return -EINVAL;
  87. }
  88. if (kstrtouint(argv[0], 10, &stripes) || !stripes) {
  89. ti->error = "Invalid stripe count";
  90. return -EINVAL;
  91. }
  92. if (kstrtouint(argv[1], 10, &chunk_size) || !chunk_size) {
  93. ti->error = "Invalid chunk_size";
  94. return -EINVAL;
  95. }
  96. width = ti->len;
  97. if (sector_div(width, stripes)) {
  98. ti->error = "Target length not divisible by "
  99. "number of stripes";
  100. return -EINVAL;
  101. }
  102. tmp_len = width;
  103. if (sector_div(tmp_len, chunk_size)) {
  104. ti->error = "Target length not divisible by "
  105. "chunk size";
  106. return -EINVAL;
  107. }
  108. /*
  109. * Do we have enough arguments for that many stripes ?
  110. */
  111. if (argc != (2 + 2 * stripes)) {
  112. ti->error = "Not enough destinations "
  113. "specified";
  114. return -EINVAL;
  115. }
  116. sc = alloc_context(stripes);
  117. if (!sc) {
  118. ti->error = "Memory allocation for striped context "
  119. "failed";
  120. return -ENOMEM;
  121. }
  122. INIT_WORK(&sc->trigger_event, trigger_event);
  123. /* Set pointer to dm target; used in trigger_event */
  124. sc->ti = ti;
  125. sc->stripes = stripes;
  126. sc->stripe_width = width;
  127. if (stripes & (stripes - 1))
  128. sc->stripes_shift = -1;
  129. else
  130. sc->stripes_shift = __ffs(stripes);
  131. r = dm_set_target_max_io_len(ti, chunk_size);
  132. if (r) {
  133. kfree(sc);
  134. return r;
  135. }
  136. ti->num_flush_bios = stripes;
  137. ti->num_discard_bios = stripes;
  138. ti->num_secure_erase_bios = stripes;
  139. ti->num_write_same_bios = stripes;
  140. ti->num_write_zeroes_bios = stripes;
  141. sc->chunk_size = chunk_size;
  142. if (chunk_size & (chunk_size - 1))
  143. sc->chunk_size_shift = -1;
  144. else
  145. sc->chunk_size_shift = __ffs(chunk_size);
  146. /*
  147. * Get the stripe destinations.
  148. */
  149. for (i = 0; i < stripes; i++) {
  150. argv += 2;
  151. r = get_stripe(ti, sc, i, argv);
  152. if (r < 0) {
  153. ti->error = "Couldn't parse stripe destination";
  154. while (i--)
  155. dm_put_device(ti, sc->stripe[i].dev);
  156. kfree(sc);
  157. return r;
  158. }
  159. atomic_set(&(sc->stripe[i].error_count), 0);
  160. }
  161. ti->private = sc;
  162. return 0;
  163. }
  164. static void stripe_dtr(struct dm_target *ti)
  165. {
  166. unsigned int i;
  167. struct stripe_c *sc = (struct stripe_c *) ti->private;
  168. for (i = 0; i < sc->stripes; i++)
  169. dm_put_device(ti, sc->stripe[i].dev);
  170. flush_work(&sc->trigger_event);
  171. kfree(sc);
  172. }
  173. static void stripe_map_sector(struct stripe_c *sc, sector_t sector,
  174. uint32_t *stripe, sector_t *result)
  175. {
  176. sector_t chunk = dm_target_offset(sc->ti, sector);
  177. sector_t chunk_offset;
  178. if (sc->chunk_size_shift < 0)
  179. chunk_offset = sector_div(chunk, sc->chunk_size);
  180. else {
  181. chunk_offset = chunk & (sc->chunk_size - 1);
  182. chunk >>= sc->chunk_size_shift;
  183. }
  184. if (sc->stripes_shift < 0)
  185. *stripe = sector_div(chunk, sc->stripes);
  186. else {
  187. *stripe = chunk & (sc->stripes - 1);
  188. chunk >>= sc->stripes_shift;
  189. }
  190. if (sc->chunk_size_shift < 0)
  191. chunk *= sc->chunk_size;
  192. else
  193. chunk <<= sc->chunk_size_shift;
  194. *result = chunk + chunk_offset;
  195. }
  196. static void stripe_map_range_sector(struct stripe_c *sc, sector_t sector,
  197. uint32_t target_stripe, sector_t *result)
  198. {
  199. uint32_t stripe;
  200. stripe_map_sector(sc, sector, &stripe, result);
  201. if (stripe == target_stripe)
  202. return;
  203. /* round down */
  204. sector = *result;
  205. if (sc->chunk_size_shift < 0)
  206. *result -= sector_div(sector, sc->chunk_size);
  207. else
  208. *result = sector & ~(sector_t)(sc->chunk_size - 1);
  209. if (target_stripe < stripe)
  210. *result += sc->chunk_size; /* next chunk */
  211. }
  212. static int stripe_map_range(struct stripe_c *sc, struct bio *bio,
  213. uint32_t target_stripe)
  214. {
  215. sector_t begin, end;
  216. stripe_map_range_sector(sc, bio->bi_iter.bi_sector,
  217. target_stripe, &begin);
  218. stripe_map_range_sector(sc, bio_end_sector(bio),
  219. target_stripe, &end);
  220. if (begin < end) {
  221. bio_set_dev(bio, sc->stripe[target_stripe].dev->bdev);
  222. bio->bi_iter.bi_sector = begin +
  223. sc->stripe[target_stripe].physical_start;
  224. bio->bi_iter.bi_size = to_bytes(end - begin);
  225. return DM_MAPIO_REMAPPED;
  226. } else {
  227. /* The range doesn't map to the target stripe */
  228. bio_endio(bio);
  229. return DM_MAPIO_SUBMITTED;
  230. }
  231. }
  232. static int stripe_map(struct dm_target *ti, struct bio *bio)
  233. {
  234. struct stripe_c *sc = ti->private;
  235. uint32_t stripe;
  236. unsigned target_bio_nr;
  237. if (bio->bi_opf & REQ_PREFLUSH) {
  238. target_bio_nr = dm_bio_get_target_bio_nr(bio);
  239. BUG_ON(target_bio_nr >= sc->stripes);
  240. bio_set_dev(bio, sc->stripe[target_bio_nr].dev->bdev);
  241. return DM_MAPIO_REMAPPED;
  242. }
  243. if (unlikely(bio_op(bio) == REQ_OP_DISCARD) ||
  244. unlikely(bio_op(bio) == REQ_OP_SECURE_ERASE) ||
  245. unlikely(bio_op(bio) == REQ_OP_WRITE_ZEROES) ||
  246. unlikely(bio_op(bio) == REQ_OP_WRITE_SAME)) {
  247. target_bio_nr = dm_bio_get_target_bio_nr(bio);
  248. BUG_ON(target_bio_nr >= sc->stripes);
  249. return stripe_map_range(sc, bio, target_bio_nr);
  250. }
  251. stripe_map_sector(sc, bio->bi_iter.bi_sector,
  252. &stripe, &bio->bi_iter.bi_sector);
  253. bio->bi_iter.bi_sector += sc->stripe[stripe].physical_start;
  254. bio_set_dev(bio, sc->stripe[stripe].dev->bdev);
  255. return DM_MAPIO_REMAPPED;
  256. }
  257. #if IS_ENABLED(CONFIG_DAX_DRIVER)
  258. static long stripe_dax_direct_access(struct dm_target *ti, pgoff_t pgoff,
  259. long nr_pages, void **kaddr, pfn_t *pfn)
  260. {
  261. sector_t dev_sector, sector = pgoff * PAGE_SECTORS;
  262. struct stripe_c *sc = ti->private;
  263. struct dax_device *dax_dev;
  264. struct block_device *bdev;
  265. uint32_t stripe;
  266. long ret;
  267. stripe_map_sector(sc, sector, &stripe, &dev_sector);
  268. dev_sector += sc->stripe[stripe].physical_start;
  269. dax_dev = sc->stripe[stripe].dev->dax_dev;
  270. bdev = sc->stripe[stripe].dev->bdev;
  271. ret = bdev_dax_pgoff(bdev, dev_sector, nr_pages * PAGE_SIZE, &pgoff);
  272. if (ret)
  273. return ret;
  274. return dax_direct_access(dax_dev, pgoff, nr_pages, kaddr, pfn);
  275. }
  276. static size_t stripe_dax_copy_from_iter(struct dm_target *ti, pgoff_t pgoff,
  277. void *addr, size_t bytes, struct iov_iter *i)
  278. {
  279. sector_t dev_sector, sector = pgoff * PAGE_SECTORS;
  280. struct stripe_c *sc = ti->private;
  281. struct dax_device *dax_dev;
  282. struct block_device *bdev;
  283. uint32_t stripe;
  284. stripe_map_sector(sc, sector, &stripe, &dev_sector);
  285. dev_sector += sc->stripe[stripe].physical_start;
  286. dax_dev = sc->stripe[stripe].dev->dax_dev;
  287. bdev = sc->stripe[stripe].dev->bdev;
  288. if (bdev_dax_pgoff(bdev, dev_sector, ALIGN(bytes, PAGE_SIZE), &pgoff))
  289. return 0;
  290. return dax_copy_from_iter(dax_dev, pgoff, addr, bytes, i);
  291. }
  292. static size_t stripe_dax_copy_to_iter(struct dm_target *ti, pgoff_t pgoff,
  293. void *addr, size_t bytes, struct iov_iter *i)
  294. {
  295. sector_t dev_sector, sector = pgoff * PAGE_SECTORS;
  296. struct stripe_c *sc = ti->private;
  297. struct dax_device *dax_dev;
  298. struct block_device *bdev;
  299. uint32_t stripe;
  300. stripe_map_sector(sc, sector, &stripe, &dev_sector);
  301. dev_sector += sc->stripe[stripe].physical_start;
  302. dax_dev = sc->stripe[stripe].dev->dax_dev;
  303. bdev = sc->stripe[stripe].dev->bdev;
  304. if (bdev_dax_pgoff(bdev, dev_sector, ALIGN(bytes, PAGE_SIZE), &pgoff))
  305. return 0;
  306. return dax_copy_to_iter(dax_dev, pgoff, addr, bytes, i);
  307. }
  308. #else
  309. #define stripe_dax_direct_access NULL
  310. #define stripe_dax_copy_from_iter NULL
  311. #define stripe_dax_copy_to_iter NULL
  312. #endif
  313. /*
  314. * Stripe status:
  315. *
  316. * INFO
  317. * #stripes [stripe_name <stripe_name>] [group word count]
  318. * [error count 'A|D' <error count 'A|D'>]
  319. *
  320. * TABLE
  321. * #stripes [stripe chunk size]
  322. * [stripe_name physical_start <stripe_name physical_start>]
  323. *
  324. */
  325. static void stripe_status(struct dm_target *ti, status_type_t type,
  326. unsigned status_flags, char *result, unsigned maxlen)
  327. {
  328. struct stripe_c *sc = (struct stripe_c *) ti->private;
  329. unsigned int sz = 0;
  330. unsigned int i;
  331. switch (type) {
  332. case STATUSTYPE_INFO:
  333. DMEMIT("%d ", sc->stripes);
  334. for (i = 0; i < sc->stripes; i++) {
  335. DMEMIT("%s ", sc->stripe[i].dev->name);
  336. }
  337. DMEMIT("1 ");
  338. for (i = 0; i < sc->stripes; i++) {
  339. DMEMIT("%c", atomic_read(&(sc->stripe[i].error_count)) ?
  340. 'D' : 'A');
  341. }
  342. break;
  343. case STATUSTYPE_TABLE:
  344. DMEMIT("%d %llu", sc->stripes,
  345. (unsigned long long)sc->chunk_size);
  346. for (i = 0; i < sc->stripes; i++)
  347. DMEMIT(" %s %llu", sc->stripe[i].dev->name,
  348. (unsigned long long)sc->stripe[i].physical_start);
  349. break;
  350. }
  351. }
  352. static int stripe_end_io(struct dm_target *ti, struct bio *bio,
  353. blk_status_t *error)
  354. {
  355. unsigned i;
  356. char major_minor[16];
  357. struct stripe_c *sc = ti->private;
  358. if (!*error)
  359. return DM_ENDIO_DONE; /* I/O complete */
  360. if (bio->bi_opf & REQ_RAHEAD)
  361. return DM_ENDIO_DONE;
  362. if (*error == BLK_STS_NOTSUPP)
  363. return DM_ENDIO_DONE;
  364. memset(major_minor, 0, sizeof(major_minor));
  365. sprintf(major_minor, "%d:%d", MAJOR(bio_dev(bio)), MINOR(bio_dev(bio)));
  366. /*
  367. * Test to see which stripe drive triggered the event
  368. * and increment error count for all stripes on that device.
  369. * If the error count for a given device exceeds the threshold
  370. * value we will no longer trigger any further events.
  371. */
  372. for (i = 0; i < sc->stripes; i++)
  373. if (!strcmp(sc->stripe[i].dev->name, major_minor)) {
  374. atomic_inc(&(sc->stripe[i].error_count));
  375. if (atomic_read(&(sc->stripe[i].error_count)) <
  376. DM_IO_ERROR_THRESHOLD)
  377. schedule_work(&sc->trigger_event);
  378. }
  379. return DM_ENDIO_DONE;
  380. }
  381. static int stripe_iterate_devices(struct dm_target *ti,
  382. iterate_devices_callout_fn fn, void *data)
  383. {
  384. struct stripe_c *sc = ti->private;
  385. int ret = 0;
  386. unsigned i = 0;
  387. do {
  388. ret = fn(ti, sc->stripe[i].dev,
  389. sc->stripe[i].physical_start,
  390. sc->stripe_width, data);
  391. } while (!ret && ++i < sc->stripes);
  392. return ret;
  393. }
  394. static void stripe_io_hints(struct dm_target *ti,
  395. struct queue_limits *limits)
  396. {
  397. struct stripe_c *sc = ti->private;
  398. unsigned chunk_size = sc->chunk_size << SECTOR_SHIFT;
  399. blk_limits_io_min(limits, chunk_size);
  400. blk_limits_io_opt(limits, chunk_size * sc->stripes);
  401. }
  402. static struct target_type stripe_target = {
  403. .name = "striped",
  404. .version = {1, 6, 0},
  405. .features = DM_TARGET_PASSES_INTEGRITY,
  406. .module = THIS_MODULE,
  407. .ctr = stripe_ctr,
  408. .dtr = stripe_dtr,
  409. .map = stripe_map,
  410. .end_io = stripe_end_io,
  411. .status = stripe_status,
  412. .iterate_devices = stripe_iterate_devices,
  413. .io_hints = stripe_io_hints,
  414. .direct_access = stripe_dax_direct_access,
  415. .dax_copy_from_iter = stripe_dax_copy_from_iter,
  416. .dax_copy_to_iter = stripe_dax_copy_to_iter,
  417. };
  418. int __init dm_stripe_init(void)
  419. {
  420. int r;
  421. r = dm_register_target(&stripe_target);
  422. if (r < 0)
  423. DMWARN("target registration failed");
  424. return r;
  425. }
  426. void dm_stripe_exit(void)
  427. {
  428. dm_unregister_target(&stripe_target);
  429. }