dm-linear.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. #define DM_MSG_PREFIX "linear"
  13. /*
  14. * Linear: maps a linear range of a device.
  15. */
  16. struct linear_c {
  17. struct dm_dev *dev;
  18. sector_t start;
  19. };
  20. /*
  21. * Construct a linear mapping: <dev_path> <offset>
  22. */
  23. static int linear_ctr(struct dm_target *ti, unsigned int argc, char **argv)
  24. {
  25. struct linear_c *lc;
  26. unsigned long long tmp;
  27. if (argc != 2) {
  28. ti->error = "Invalid argument count";
  29. return -EINVAL;
  30. }
  31. lc = kmalloc(sizeof(*lc), GFP_KERNEL);
  32. if (lc == NULL) {
  33. ti->error = "dm-linear: Cannot allocate linear context";
  34. return -ENOMEM;
  35. }
  36. if (sscanf(argv[1], "%llu", &tmp) != 1) {
  37. ti->error = "dm-linear: Invalid device sector";
  38. goto bad;
  39. }
  40. lc->start = tmp;
  41. if (dm_get_device(ti, argv[0], lc->start, ti->len,
  42. dm_table_get_mode(ti->table), &lc->dev)) {
  43. ti->error = "dm-linear: Device lookup failed";
  44. goto bad;
  45. }
  46. ti->private = lc;
  47. return 0;
  48. bad:
  49. kfree(lc);
  50. return -EINVAL;
  51. }
  52. static void linear_dtr(struct dm_target *ti)
  53. {
  54. struct linear_c *lc = (struct linear_c *) ti->private;
  55. dm_put_device(ti, lc->dev);
  56. kfree(lc);
  57. }
  58. static int linear_map(struct dm_target *ti, struct bio *bio,
  59. union map_info *map_context)
  60. {
  61. struct linear_c *lc = (struct linear_c *) ti->private;
  62. bio->bi_bdev = lc->dev->bdev;
  63. bio->bi_sector = lc->start + (bio->bi_sector - ti->begin);
  64. return 1;
  65. }
  66. static int linear_status(struct dm_target *ti, status_type_t type,
  67. char *result, unsigned int maxlen)
  68. {
  69. struct linear_c *lc = (struct linear_c *) ti->private;
  70. switch (type) {
  71. case STATUSTYPE_INFO:
  72. result[0] = '\0';
  73. break;
  74. case STATUSTYPE_TABLE:
  75. snprintf(result, maxlen, "%s %llu", lc->dev->name,
  76. (unsigned long long)lc->start);
  77. break;
  78. }
  79. return 0;
  80. }
  81. static int linear_ioctl(struct dm_target *ti, struct inode *inode,
  82. struct file *filp, unsigned int cmd,
  83. unsigned long arg)
  84. {
  85. struct linear_c *lc = (struct linear_c *) ti->private;
  86. struct block_device *bdev = lc->dev->bdev;
  87. struct file fake_file = {};
  88. struct dentry fake_dentry = {};
  89. fake_file.f_mode = lc->dev->mode;
  90. fake_file.f_dentry = &fake_dentry;
  91. fake_dentry.d_inode = bdev->bd_inode;
  92. return blkdev_driver_ioctl(bdev->bd_inode, &fake_file, bdev->bd_disk, cmd, arg);
  93. }
  94. static struct target_type linear_target = {
  95. .name = "linear",
  96. .version= {1, 0, 2},
  97. .module = THIS_MODULE,
  98. .ctr = linear_ctr,
  99. .dtr = linear_dtr,
  100. .map = linear_map,
  101. .status = linear_status,
  102. .ioctl = linear_ioctl,
  103. };
  104. int __init dm_linear_init(void)
  105. {
  106. int r = dm_register_target(&linear_target);
  107. if (r < 0)
  108. DMERR("register failed %d", r);
  109. return r;
  110. }
  111. void dm_linear_exit(void)
  112. {
  113. int r = dm_unregister_target(&linear_target);
  114. if (r < 0)
  115. DMERR("unregister failed %d", r);
  116. }