dm-linear.c 2.3 KB

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