dm-linear.c 2.4 KB

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