dm-target.c 3.2 KB

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