dm-target.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Copyright (C) 2001 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/kmod.h>
  10. #include <linux/bio.h>
  11. #include <linux/slab.h>
  12. #define DM_MSG_PREFIX "target"
  13. static LIST_HEAD(_targets);
  14. static DECLARE_RWSEM(_lock);
  15. #define DM_MOD_NAME_SIZE 32
  16. static inline struct target_type *__find_target_type(const char *name)
  17. {
  18. struct target_type *tt;
  19. list_for_each_entry(tt, &_targets, list)
  20. if (!strcmp(name, tt->name))
  21. return tt;
  22. return NULL;
  23. }
  24. static struct target_type *get_target_type(const char *name)
  25. {
  26. struct target_type *tt;
  27. down_read(&_lock);
  28. tt = __find_target_type(name);
  29. if (tt && !try_module_get(tt->module))
  30. tt = NULL;
  31. up_read(&_lock);
  32. return tt;
  33. }
  34. static void load_module(const char *name)
  35. {
  36. request_module("dm-%s", name);
  37. }
  38. struct target_type *dm_get_target_type(const char *name)
  39. {
  40. struct target_type *tt = get_target_type(name);
  41. if (!tt) {
  42. load_module(name);
  43. tt = get_target_type(name);
  44. }
  45. return tt;
  46. }
  47. void dm_put_target_type(struct target_type *tt)
  48. {
  49. down_read(&_lock);
  50. module_put(tt->module);
  51. up_read(&_lock);
  52. }
  53. int dm_target_iterate(void (*iter_func)(struct target_type *tt,
  54. void *param), void *param)
  55. {
  56. struct target_type *tt;
  57. down_read(&_lock);
  58. list_for_each_entry(tt, &_targets, list)
  59. iter_func(tt, param);
  60. up_read(&_lock);
  61. return 0;
  62. }
  63. int dm_register_target(struct target_type *tt)
  64. {
  65. int rv = 0;
  66. down_write(&_lock);
  67. if (__find_target_type(tt->name))
  68. rv = -EEXIST;
  69. else
  70. list_add(&tt->list, &_targets);
  71. up_write(&_lock);
  72. return rv;
  73. }
  74. void dm_unregister_target(struct target_type *tt)
  75. {
  76. down_write(&_lock);
  77. if (!__find_target_type(tt->name)) {
  78. DMCRIT("Unregistering unrecognised target: %s", tt->name);
  79. BUG();
  80. }
  81. list_del(&tt->list);
  82. up_write(&_lock);
  83. }
  84. /*
  85. * io-err: always fails an io, useful for bringing
  86. * up LVs that have holes in them.
  87. */
  88. static int io_err_ctr(struct dm_target *tt, unsigned int argc, char **args)
  89. {
  90. return 0;
  91. }
  92. static void io_err_dtr(struct dm_target *tt)
  93. {
  94. /* empty */
  95. }
  96. static int io_err_map(struct dm_target *tt, struct bio *bio,
  97. union map_info *map_context)
  98. {
  99. return -EIO;
  100. }
  101. static struct target_type error_target = {
  102. .name = "error",
  103. .version = {1, 0, 1},
  104. .ctr = io_err_ctr,
  105. .dtr = io_err_dtr,
  106. .map = io_err_map,
  107. };
  108. int __init dm_target_init(void)
  109. {
  110. return dm_register_target(&error_target);
  111. }
  112. void dm_target_exit(void)
  113. {
  114. dm_unregister_target(&error_target);
  115. }
  116. EXPORT_SYMBOL(dm_register_target);
  117. EXPORT_SYMBOL(dm_unregister_target);