dm-path-selector.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Copyright (C) 2003 Sistina Software.
  3. * Copyright (C) 2004 Red Hat, Inc. All rights reserved.
  4. *
  5. * Module Author: Heinz Mauelshagen
  6. *
  7. * This file is released under the GPL.
  8. *
  9. * Path selector registration.
  10. */
  11. #include "dm.h"
  12. #include "dm-path-selector.h"
  13. #include <linux/slab.h>
  14. struct ps_internal {
  15. struct path_selector_type pst;
  16. struct list_head list;
  17. long use;
  18. };
  19. #define pst_to_psi(__pst) container_of((__pst), struct ps_internal, pst)
  20. static LIST_HEAD(_path_selectors);
  21. static DECLARE_RWSEM(_ps_lock);
  22. static struct ps_internal *__find_path_selector_type(const char *name)
  23. {
  24. struct ps_internal *psi;
  25. list_for_each_entry(psi, &_path_selectors, list) {
  26. if (!strcmp(name, psi->pst.name))
  27. return psi;
  28. }
  29. return NULL;
  30. }
  31. static struct ps_internal *get_path_selector(const char *name)
  32. {
  33. struct ps_internal *psi;
  34. down_read(&_ps_lock);
  35. psi = __find_path_selector_type(name);
  36. if (psi) {
  37. if ((psi->use == 0) && !try_module_get(psi->pst.module))
  38. psi = NULL;
  39. else
  40. psi->use++;
  41. }
  42. up_read(&_ps_lock);
  43. return psi;
  44. }
  45. struct path_selector_type *dm_get_path_selector(const char *name)
  46. {
  47. struct ps_internal *psi;
  48. if (!name)
  49. return NULL;
  50. psi = get_path_selector(name);
  51. if (!psi) {
  52. request_module("dm-%s", name);
  53. psi = get_path_selector(name);
  54. }
  55. return psi ? &psi->pst : NULL;
  56. }
  57. void dm_put_path_selector(struct path_selector_type *pst)
  58. {
  59. struct ps_internal *psi;
  60. if (!pst)
  61. return;
  62. down_read(&_ps_lock);
  63. psi = __find_path_selector_type(pst->name);
  64. if (!psi)
  65. goto out;
  66. if (--psi->use == 0)
  67. module_put(psi->pst.module);
  68. BUG_ON(psi->use < 0);
  69. out:
  70. up_read(&_ps_lock);
  71. }
  72. static struct ps_internal *_alloc_path_selector(struct path_selector_type *pst)
  73. {
  74. struct ps_internal *psi = kzalloc(sizeof(*psi), GFP_KERNEL);
  75. if (psi)
  76. psi->pst = *pst;
  77. return psi;
  78. }
  79. int dm_register_path_selector(struct path_selector_type *pst)
  80. {
  81. int r = 0;
  82. struct ps_internal *psi = _alloc_path_selector(pst);
  83. if (!psi)
  84. return -ENOMEM;
  85. down_write(&_ps_lock);
  86. if (__find_path_selector_type(pst->name)) {
  87. kfree(psi);
  88. r = -EEXIST;
  89. } else
  90. list_add(&psi->list, &_path_selectors);
  91. up_write(&_ps_lock);
  92. return r;
  93. }
  94. int dm_unregister_path_selector(struct path_selector_type *pst)
  95. {
  96. struct ps_internal *psi;
  97. down_write(&_ps_lock);
  98. psi = __find_path_selector_type(pst->name);
  99. if (!psi) {
  100. up_write(&_ps_lock);
  101. return -EINVAL;
  102. }
  103. if (psi->use) {
  104. up_write(&_ps_lock);
  105. return -ETXTBSY;
  106. }
  107. list_del(&psi->list);
  108. up_write(&_ps_lock);
  109. kfree(psi);
  110. return 0;
  111. }
  112. EXPORT_SYMBOL_GPL(dm_register_path_selector);
  113. EXPORT_SYMBOL_GPL(dm_unregister_path_selector);