dm-target.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Copyright (C) 2001 Sistina Software (UK) Limited
  3. *
  4. * This file is released under the GPL.
  5. */
  6. #include "dm-core.h"
  7. #include <linux/module.h>
  8. #include <linux/init.h>
  9. #include <linux/kmod.h>
  10. #include <linux/bio.h>
  11. #define DM_MSG_PREFIX "target"
  12. static LIST_HEAD(_targets);
  13. static DECLARE_RWSEM(_lock);
  14. static inline struct target_type *__find_target_type(const char *name)
  15. {
  16. struct target_type *tt;
  17. list_for_each_entry(tt, &_targets, list)
  18. if (!strcmp(name, tt->name))
  19. return tt;
  20. return NULL;
  21. }
  22. static struct target_type *get_target_type(const char *name)
  23. {
  24. struct target_type *tt;
  25. down_read(&_lock);
  26. tt = __find_target_type(name);
  27. if (tt && !try_module_get(tt->module))
  28. tt = NULL;
  29. up_read(&_lock);
  30. return tt;
  31. }
  32. static void load_module(const char *name)
  33. {
  34. request_module("dm-%s", name);
  35. }
  36. struct target_type *dm_get_target_type(const char *name)
  37. {
  38. struct target_type *tt = get_target_type(name);
  39. if (!tt) {
  40. load_module(name);
  41. tt = get_target_type(name);
  42. }
  43. return tt;
  44. }
  45. void dm_put_target_type(struct target_type *tt)
  46. {
  47. down_read(&_lock);
  48. module_put(tt->module);
  49. up_read(&_lock);
  50. }
  51. int dm_target_iterate(void (*iter_func)(struct target_type *tt,
  52. void *param), void *param)
  53. {
  54. struct target_type *tt;
  55. down_read(&_lock);
  56. list_for_each_entry(tt, &_targets, list)
  57. iter_func(tt, param);
  58. up_read(&_lock);
  59. return 0;
  60. }
  61. int dm_register_target(struct target_type *tt)
  62. {
  63. int rv = 0;
  64. down_write(&_lock);
  65. if (__find_target_type(tt->name))
  66. rv = -EEXIST;
  67. else
  68. list_add(&tt->list, &_targets);
  69. up_write(&_lock);
  70. return rv;
  71. }
  72. void dm_unregister_target(struct target_type *tt)
  73. {
  74. down_write(&_lock);
  75. if (!__find_target_type(tt->name)) {
  76. DMCRIT("Unregistering unrecognised target: %s", tt->name);
  77. BUG();
  78. }
  79. list_del(&tt->list);
  80. up_write(&_lock);
  81. }
  82. /*
  83. * io-err: always fails an io, useful for bringing
  84. * up LVs that have holes in them.
  85. */
  86. static int io_err_ctr(struct dm_target *tt, unsigned int argc, char **args)
  87. {
  88. /*
  89. * Return error for discards instead of -EOPNOTSUPP
  90. */
  91. tt->num_discard_bios = 1;
  92. return 0;
  93. }
  94. static void io_err_dtr(struct dm_target *tt)
  95. {
  96. /* empty */
  97. }
  98. static int io_err_map(struct dm_target *tt, struct bio *bio)
  99. {
  100. return DM_MAPIO_KILL;
  101. }
  102. static int io_err_clone_and_map_rq(struct dm_target *ti, struct request *rq,
  103. union map_info *map_context,
  104. struct request **clone)
  105. {
  106. return DM_MAPIO_KILL;
  107. }
  108. static void io_err_release_clone_rq(struct request *clone,
  109. union map_info *map_context)
  110. {
  111. }
  112. static long io_err_dax_direct_access(struct dm_target *ti, pgoff_t pgoff,
  113. long nr_pages, void **kaddr, pfn_t *pfn)
  114. {
  115. return -EIO;
  116. }
  117. static struct target_type error_target = {
  118. .name = "error",
  119. .version = {1, 5, 0},
  120. .features = DM_TARGET_WILDCARD,
  121. .ctr = io_err_ctr,
  122. .dtr = io_err_dtr,
  123. .map = io_err_map,
  124. .clone_and_map_rq = io_err_clone_and_map_rq,
  125. .release_clone_rq = io_err_release_clone_rq,
  126. .direct_access = io_err_dax_direct_access,
  127. };
  128. int __init dm_target_init(void)
  129. {
  130. return dm_register_target(&error_target);
  131. }
  132. void dm_target_exit(void)
  133. {
  134. dm_unregister_target(&error_target);
  135. }
  136. EXPORT_SYMBOL(dm_register_target);
  137. EXPORT_SYMBOL(dm_unregister_target);