dm-target.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. #define DM_MOD_NAME_SIZE 32
  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 -EIO;
  102. }
  103. static int io_err_map_rq(struct dm_target *ti, struct request *clone,
  104. union map_info *map_context)
  105. {
  106. return -EIO;
  107. }
  108. static int io_err_clone_and_map_rq(struct dm_target *ti, struct request *rq,
  109. union map_info *map_context,
  110. struct request **clone)
  111. {
  112. return -EIO;
  113. }
  114. static void io_err_release_clone_rq(struct request *clone)
  115. {
  116. }
  117. static long io_err_direct_access(struct dm_target *ti, sector_t sector,
  118. void **kaddr, pfn_t *pfn, long size)
  119. {
  120. return -EIO;
  121. }
  122. static struct target_type error_target = {
  123. .name = "error",
  124. .version = {1, 5, 0},
  125. .features = DM_TARGET_WILDCARD,
  126. .ctr = io_err_ctr,
  127. .dtr = io_err_dtr,
  128. .map = io_err_map,
  129. .map_rq = io_err_map_rq,
  130. .clone_and_map_rq = io_err_clone_and_map_rq,
  131. .release_clone_rq = io_err_release_clone_rq,
  132. .direct_access = io_err_direct_access,
  133. };
  134. int __init dm_target_init(void)
  135. {
  136. return dm_register_target(&error_target);
  137. }
  138. void dm_target_exit(void)
  139. {
  140. dm_unregister_target(&error_target);
  141. }
  142. EXPORT_SYMBOL(dm_register_target);
  143. EXPORT_SYMBOL(dm_unregister_target);