dm-linear.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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/module.h>
  8. #include <linux/init.h>
  9. #include <linux/blkdev.h>
  10. #include <linux/bio.h>
  11. #include <linux/dax.h>
  12. #include <linux/slab.h>
  13. #include <linux/device-mapper.h>
  14. #define DM_MSG_PREFIX "linear"
  15. /*
  16. * Linear: maps a linear range of a device.
  17. */
  18. struct linear_c {
  19. struct dm_dev *dev;
  20. sector_t start;
  21. };
  22. /*
  23. * Construct a linear mapping: <dev_path> <offset>
  24. */
  25. static int linear_ctr(struct dm_target *ti, unsigned int argc, char **argv)
  26. {
  27. struct linear_c *lc;
  28. unsigned long long tmp;
  29. char dummy;
  30. int ret;
  31. if (argc != 2) {
  32. ti->error = "Invalid argument count";
  33. return -EINVAL;
  34. }
  35. lc = kmalloc(sizeof(*lc), GFP_KERNEL);
  36. if (lc == NULL) {
  37. ti->error = "Cannot allocate linear context";
  38. return -ENOMEM;
  39. }
  40. ret = -EINVAL;
  41. if (sscanf(argv[1], "%llu%c", &tmp, &dummy) != 1 || tmp != (sector_t)tmp) {
  42. ti->error = "Invalid device sector";
  43. goto bad;
  44. }
  45. lc->start = tmp;
  46. ret = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &lc->dev);
  47. if (ret) {
  48. ti->error = "Device lookup failed";
  49. goto bad;
  50. }
  51. ti->num_flush_bios = 1;
  52. ti->num_discard_bios = 1;
  53. ti->num_secure_erase_bios = 1;
  54. ti->num_write_same_bios = 1;
  55. ti->num_write_zeroes_bios = 1;
  56. ti->private = lc;
  57. return 0;
  58. bad:
  59. kfree(lc);
  60. return ret;
  61. }
  62. static void linear_dtr(struct dm_target *ti)
  63. {
  64. struct linear_c *lc = (struct linear_c *) ti->private;
  65. dm_put_device(ti, lc->dev);
  66. kfree(lc);
  67. }
  68. static sector_t linear_map_sector(struct dm_target *ti, sector_t bi_sector)
  69. {
  70. struct linear_c *lc = ti->private;
  71. return lc->start + dm_target_offset(ti, bi_sector);
  72. }
  73. static void linear_map_bio(struct dm_target *ti, struct bio *bio)
  74. {
  75. struct linear_c *lc = ti->private;
  76. bio_set_dev(bio, lc->dev->bdev);
  77. if (bio_sectors(bio) || bio_op(bio) == REQ_OP_ZONE_RESET)
  78. bio->bi_iter.bi_sector =
  79. linear_map_sector(ti, bio->bi_iter.bi_sector);
  80. }
  81. static int linear_map(struct dm_target *ti, struct bio *bio)
  82. {
  83. linear_map_bio(ti, bio);
  84. return DM_MAPIO_REMAPPED;
  85. }
  86. #ifdef CONFIG_BLK_DEV_ZONED
  87. static int linear_end_io(struct dm_target *ti, struct bio *bio,
  88. blk_status_t *error)
  89. {
  90. struct linear_c *lc = ti->private;
  91. if (!*error && bio_op(bio) == REQ_OP_ZONE_REPORT)
  92. dm_remap_zone_report(ti, bio, lc->start);
  93. return DM_ENDIO_DONE;
  94. }
  95. #endif
  96. static void linear_status(struct dm_target *ti, status_type_t type,
  97. unsigned status_flags, char *result, unsigned maxlen)
  98. {
  99. struct linear_c *lc = (struct linear_c *) ti->private;
  100. switch (type) {
  101. case STATUSTYPE_INFO:
  102. result[0] = '\0';
  103. break;
  104. case STATUSTYPE_TABLE:
  105. snprintf(result, maxlen, "%s %llu", lc->dev->name,
  106. (unsigned long long)lc->start);
  107. break;
  108. }
  109. }
  110. static int linear_prepare_ioctl(struct dm_target *ti, struct block_device **bdev)
  111. {
  112. struct linear_c *lc = (struct linear_c *) ti->private;
  113. struct dm_dev *dev = lc->dev;
  114. *bdev = dev->bdev;
  115. /*
  116. * Only pass ioctls through if the device sizes match exactly.
  117. */
  118. if (lc->start ||
  119. ti->len != i_size_read(dev->bdev->bd_inode) >> SECTOR_SHIFT)
  120. return 1;
  121. return 0;
  122. }
  123. static int linear_iterate_devices(struct dm_target *ti,
  124. iterate_devices_callout_fn fn, void *data)
  125. {
  126. struct linear_c *lc = ti->private;
  127. return fn(ti, lc->dev, lc->start, ti->len, data);
  128. }
  129. #if IS_ENABLED(CONFIG_DAX_DRIVER)
  130. static long linear_dax_direct_access(struct dm_target *ti, pgoff_t pgoff,
  131. long nr_pages, void **kaddr, pfn_t *pfn)
  132. {
  133. long ret;
  134. struct linear_c *lc = ti->private;
  135. struct block_device *bdev = lc->dev->bdev;
  136. struct dax_device *dax_dev = lc->dev->dax_dev;
  137. sector_t dev_sector, sector = pgoff * PAGE_SECTORS;
  138. dev_sector = linear_map_sector(ti, sector);
  139. ret = bdev_dax_pgoff(bdev, dev_sector, nr_pages * PAGE_SIZE, &pgoff);
  140. if (ret)
  141. return ret;
  142. return dax_direct_access(dax_dev, pgoff, nr_pages, kaddr, pfn);
  143. }
  144. static size_t linear_dax_copy_from_iter(struct dm_target *ti, pgoff_t pgoff,
  145. void *addr, size_t bytes, struct iov_iter *i)
  146. {
  147. struct linear_c *lc = ti->private;
  148. struct block_device *bdev = lc->dev->bdev;
  149. struct dax_device *dax_dev = lc->dev->dax_dev;
  150. sector_t dev_sector, sector = pgoff * PAGE_SECTORS;
  151. dev_sector = linear_map_sector(ti, sector);
  152. if (bdev_dax_pgoff(bdev, dev_sector, ALIGN(bytes, PAGE_SIZE), &pgoff))
  153. return 0;
  154. return dax_copy_from_iter(dax_dev, pgoff, addr, bytes, i);
  155. }
  156. static size_t linear_dax_copy_to_iter(struct dm_target *ti, pgoff_t pgoff,
  157. void *addr, size_t bytes, struct iov_iter *i)
  158. {
  159. struct linear_c *lc = ti->private;
  160. struct block_device *bdev = lc->dev->bdev;
  161. struct dax_device *dax_dev = lc->dev->dax_dev;
  162. sector_t dev_sector, sector = pgoff * PAGE_SECTORS;
  163. dev_sector = linear_map_sector(ti, sector);
  164. if (bdev_dax_pgoff(bdev, dev_sector, ALIGN(bytes, PAGE_SIZE), &pgoff))
  165. return 0;
  166. return dax_copy_to_iter(dax_dev, pgoff, addr, bytes, i);
  167. }
  168. #else
  169. #define linear_dax_direct_access NULL
  170. #define linear_dax_copy_from_iter NULL
  171. #define linear_dax_copy_to_iter NULL
  172. #endif
  173. static struct target_type linear_target = {
  174. .name = "linear",
  175. .version = {1, 4, 0},
  176. #ifdef CONFIG_BLK_DEV_ZONED
  177. .end_io = linear_end_io,
  178. .features = DM_TARGET_PASSES_INTEGRITY | DM_TARGET_ZONED_HM,
  179. #else
  180. .features = DM_TARGET_PASSES_INTEGRITY,
  181. #endif
  182. .module = THIS_MODULE,
  183. .ctr = linear_ctr,
  184. .dtr = linear_dtr,
  185. .map = linear_map,
  186. .status = linear_status,
  187. .prepare_ioctl = linear_prepare_ioctl,
  188. .iterate_devices = linear_iterate_devices,
  189. .direct_access = linear_dax_direct_access,
  190. .dax_copy_from_iter = linear_dax_copy_from_iter,
  191. .dax_copy_to_iter = linear_dax_copy_to_iter,
  192. };
  193. int __init dm_linear_init(void)
  194. {
  195. int r = dm_register_target(&linear_target);
  196. if (r < 0)
  197. DMERR("register failed %d", r);
  198. return r;
  199. }
  200. void dm_linear_exit(void)
  201. {
  202. dm_unregister_target(&linear_target);
  203. }