dm-linear.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. static void linear_status(struct dm_target *ti, status_type_t type,
  87. unsigned status_flags, char *result, unsigned maxlen)
  88. {
  89. struct linear_c *lc = (struct linear_c *) ti->private;
  90. switch (type) {
  91. case STATUSTYPE_INFO:
  92. result[0] = '\0';
  93. break;
  94. case STATUSTYPE_TABLE:
  95. snprintf(result, maxlen, "%s %llu", lc->dev->name,
  96. (unsigned long long)lc->start);
  97. break;
  98. }
  99. }
  100. static int linear_prepare_ioctl(struct dm_target *ti, struct block_device **bdev)
  101. {
  102. struct linear_c *lc = (struct linear_c *) ti->private;
  103. struct dm_dev *dev = lc->dev;
  104. *bdev = dev->bdev;
  105. /*
  106. * Only pass ioctls through if the device sizes match exactly.
  107. */
  108. if (lc->start ||
  109. ti->len != i_size_read(dev->bdev->bd_inode) >> SECTOR_SHIFT)
  110. return 1;
  111. return 0;
  112. }
  113. #ifdef CONFIG_BLK_DEV_ZONED
  114. static int linear_report_zones(struct dm_target *ti, sector_t sector,
  115. struct blk_zone *zones, unsigned int *nr_zones)
  116. {
  117. struct linear_c *lc = (struct linear_c *) ti->private;
  118. int ret;
  119. /* Do report and remap it */
  120. ret = blkdev_report_zones(lc->dev->bdev, linear_map_sector(ti, sector),
  121. zones, nr_zones);
  122. if (ret != 0)
  123. return ret;
  124. if (*nr_zones)
  125. dm_remap_zone_report(ti, lc->start, zones, nr_zones);
  126. return 0;
  127. }
  128. #endif
  129. static int linear_iterate_devices(struct dm_target *ti,
  130. iterate_devices_callout_fn fn, void *data)
  131. {
  132. struct linear_c *lc = ti->private;
  133. return fn(ti, lc->dev, lc->start, ti->len, data);
  134. }
  135. #if IS_ENABLED(CONFIG_DAX_DRIVER)
  136. static long linear_dax_direct_access(struct dm_target *ti, pgoff_t pgoff,
  137. long nr_pages, void **kaddr, pfn_t *pfn)
  138. {
  139. long ret;
  140. struct linear_c *lc = ti->private;
  141. struct block_device *bdev = lc->dev->bdev;
  142. struct dax_device *dax_dev = lc->dev->dax_dev;
  143. sector_t dev_sector, sector = pgoff * PAGE_SECTORS;
  144. dev_sector = linear_map_sector(ti, sector);
  145. ret = bdev_dax_pgoff(bdev, dev_sector, nr_pages * PAGE_SIZE, &pgoff);
  146. if (ret)
  147. return ret;
  148. return dax_direct_access(dax_dev, pgoff, nr_pages, kaddr, pfn);
  149. }
  150. static size_t linear_dax_copy_from_iter(struct dm_target *ti, pgoff_t pgoff,
  151. void *addr, size_t bytes, struct iov_iter *i)
  152. {
  153. struct linear_c *lc = ti->private;
  154. struct block_device *bdev = lc->dev->bdev;
  155. struct dax_device *dax_dev = lc->dev->dax_dev;
  156. sector_t dev_sector, sector = pgoff * PAGE_SECTORS;
  157. dev_sector = linear_map_sector(ti, sector);
  158. if (bdev_dax_pgoff(bdev, dev_sector, ALIGN(bytes, PAGE_SIZE), &pgoff))
  159. return 0;
  160. return dax_copy_from_iter(dax_dev, pgoff, addr, bytes, i);
  161. }
  162. static size_t linear_dax_copy_to_iter(struct dm_target *ti, pgoff_t pgoff,
  163. void *addr, size_t bytes, struct iov_iter *i)
  164. {
  165. struct linear_c *lc = ti->private;
  166. struct block_device *bdev = lc->dev->bdev;
  167. struct dax_device *dax_dev = lc->dev->dax_dev;
  168. sector_t dev_sector, sector = pgoff * PAGE_SECTORS;
  169. dev_sector = linear_map_sector(ti, sector);
  170. if (bdev_dax_pgoff(bdev, dev_sector, ALIGN(bytes, PAGE_SIZE), &pgoff))
  171. return 0;
  172. return dax_copy_to_iter(dax_dev, pgoff, addr, bytes, i);
  173. }
  174. #else
  175. #define linear_dax_direct_access NULL
  176. #define linear_dax_copy_from_iter NULL
  177. #define linear_dax_copy_to_iter NULL
  178. #endif
  179. static struct target_type linear_target = {
  180. .name = "linear",
  181. .version = {1, 4, 0},
  182. #ifdef CONFIG_BLK_DEV_ZONED
  183. .features = DM_TARGET_PASSES_INTEGRITY | DM_TARGET_ZONED_HM,
  184. .report_zones = linear_report_zones,
  185. #else
  186. .features = DM_TARGET_PASSES_INTEGRITY,
  187. #endif
  188. .module = THIS_MODULE,
  189. .ctr = linear_ctr,
  190. .dtr = linear_dtr,
  191. .map = linear_map,
  192. .status = linear_status,
  193. .prepare_ioctl = linear_prepare_ioctl,
  194. .iterate_devices = linear_iterate_devices,
  195. .direct_access = linear_dax_direct_access,
  196. .dax_copy_from_iter = linear_dax_copy_from_iter,
  197. .dax_copy_to_iter = linear_dax_copy_to_iter,
  198. };
  199. int __init dm_linear_init(void)
  200. {
  201. int r = dm_register_target(&linear_target);
  202. if (r < 0)
  203. DMERR("register failed %d", r);
  204. return r;
  205. }
  206. void dm_linear_exit(void)
  207. {
  208. dm_unregister_target(&linear_target);
  209. }