dm-linear.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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/slab.h>
  12. #include <linux/device-mapper.h>
  13. #define DM_MSG_PREFIX "linear"
  14. /*
  15. * Linear: maps a linear range of a device.
  16. */
  17. struct linear_c {
  18. struct dm_dev *dev;
  19. sector_t start;
  20. };
  21. /*
  22. * Construct a linear mapping: <dev_path> <offset>
  23. */
  24. static int linear_ctr(struct dm_target *ti, unsigned int argc, char **argv)
  25. {
  26. struct linear_c *lc;
  27. unsigned long long tmp;
  28. char dummy;
  29. int ret;
  30. if (argc != 2) {
  31. ti->error = "Invalid argument count";
  32. return -EINVAL;
  33. }
  34. lc = kmalloc(sizeof(*lc), GFP_KERNEL);
  35. if (lc == NULL) {
  36. ti->error = "Cannot allocate linear context";
  37. return -ENOMEM;
  38. }
  39. ret = -EINVAL;
  40. if (sscanf(argv[1], "%llu%c", &tmp, &dummy) != 1) {
  41. ti->error = "Invalid device sector";
  42. goto bad;
  43. }
  44. lc->start = tmp;
  45. ret = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &lc->dev);
  46. if (ret) {
  47. ti->error = "Device lookup failed";
  48. goto bad;
  49. }
  50. ti->num_flush_bios = 1;
  51. ti->num_discard_bios = 1;
  52. ti->num_write_same_bios = 1;
  53. ti->private = lc;
  54. return 0;
  55. bad:
  56. kfree(lc);
  57. return ret;
  58. }
  59. static void linear_dtr(struct dm_target *ti)
  60. {
  61. struct linear_c *lc = (struct linear_c *) ti->private;
  62. dm_put_device(ti, lc->dev);
  63. kfree(lc);
  64. }
  65. static sector_t linear_map_sector(struct dm_target *ti, sector_t bi_sector)
  66. {
  67. struct linear_c *lc = ti->private;
  68. return lc->start + dm_target_offset(ti, bi_sector);
  69. }
  70. static void linear_map_bio(struct dm_target *ti, struct bio *bio)
  71. {
  72. struct linear_c *lc = ti->private;
  73. bio->bi_bdev = lc->dev->bdev;
  74. if (bio_sectors(bio))
  75. bio->bi_iter.bi_sector =
  76. linear_map_sector(ti, bio->bi_iter.bi_sector);
  77. }
  78. static int linear_map(struct dm_target *ti, struct bio *bio)
  79. {
  80. linear_map_bio(ti, bio);
  81. return DM_MAPIO_REMAPPED;
  82. }
  83. static void linear_status(struct dm_target *ti, status_type_t type,
  84. unsigned status_flags, char *result, unsigned maxlen)
  85. {
  86. struct linear_c *lc = (struct linear_c *) ti->private;
  87. switch (type) {
  88. case STATUSTYPE_INFO:
  89. result[0] = '\0';
  90. break;
  91. case STATUSTYPE_TABLE:
  92. snprintf(result, maxlen, "%s %llu", lc->dev->name,
  93. (unsigned long long)lc->start);
  94. break;
  95. }
  96. }
  97. static int linear_prepare_ioctl(struct dm_target *ti,
  98. struct block_device **bdev, fmode_t *mode)
  99. {
  100. struct linear_c *lc = (struct linear_c *) ti->private;
  101. struct dm_dev *dev = lc->dev;
  102. *bdev = dev->bdev;
  103. /*
  104. * Only pass ioctls through if the device sizes match exactly.
  105. */
  106. if (lc->start ||
  107. ti->len != i_size_read(dev->bdev->bd_inode) >> SECTOR_SHIFT)
  108. return 1;
  109. return 0;
  110. }
  111. static int linear_iterate_devices(struct dm_target *ti,
  112. iterate_devices_callout_fn fn, void *data)
  113. {
  114. struct linear_c *lc = ti->private;
  115. return fn(ti, lc->dev, lc->start, ti->len, data);
  116. }
  117. static long linear_direct_access(struct dm_target *ti, sector_t sector,
  118. void **kaddr, pfn_t *pfn, long size)
  119. {
  120. struct linear_c *lc = ti->private;
  121. struct block_device *bdev = lc->dev->bdev;
  122. struct blk_dax_ctl dax = {
  123. .sector = linear_map_sector(ti, sector),
  124. .size = size,
  125. };
  126. long ret;
  127. ret = bdev_direct_access(bdev, &dax);
  128. *kaddr = dax.addr;
  129. *pfn = dax.pfn;
  130. return ret;
  131. }
  132. static struct target_type linear_target = {
  133. .name = "linear",
  134. .version = {1, 3, 0},
  135. .module = THIS_MODULE,
  136. .ctr = linear_ctr,
  137. .dtr = linear_dtr,
  138. .map = linear_map,
  139. .status = linear_status,
  140. .prepare_ioctl = linear_prepare_ioctl,
  141. .iterate_devices = linear_iterate_devices,
  142. .direct_access = linear_direct_access,
  143. };
  144. int __init dm_linear_init(void)
  145. {
  146. int r = dm_register_target(&linear_target);
  147. if (r < 0)
  148. DMERR("register failed %d", r);
  149. return r;
  150. }
  151. void dm_linear_exit(void)
  152. {
  153. dm_unregister_target(&linear_target);
  154. }