dm-path-selector.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 = kmalloc(sizeof(*psi), GFP_KERNEL);
  75. if (psi) {
  76. memset(psi, 0, sizeof(*psi));
  77. psi->pst = *pst;
  78. }
  79. return psi;
  80. }
  81. int dm_register_path_selector(struct path_selector_type *pst)
  82. {
  83. int r = 0;
  84. struct ps_internal *psi = _alloc_path_selector(pst);
  85. if (!psi)
  86. return -ENOMEM;
  87. down_write(&_ps_lock);
  88. if (__find_path_selector_type(pst->name)) {
  89. kfree(psi);
  90. r = -EEXIST;
  91. } else
  92. list_add(&psi->list, &_path_selectors);
  93. up_write(&_ps_lock);
  94. return r;
  95. }
  96. int dm_unregister_path_selector(struct path_selector_type *pst)
  97. {
  98. struct ps_internal *psi;
  99. down_write(&_ps_lock);
  100. psi = __find_path_selector_type(pst->name);
  101. if (!psi) {
  102. up_write(&_ps_lock);
  103. return -EINVAL;
  104. }
  105. if (psi->use) {
  106. up_write(&_ps_lock);
  107. return -ETXTBSY;
  108. }
  109. list_del(&psi->list);
  110. up_write(&_ps_lock);
  111. kfree(psi);
  112. return 0;
  113. }
  114. EXPORT_SYMBOL_GPL(dm_register_path_selector);
  115. EXPORT_SYMBOL_GPL(dm_unregister_path_selector);