dm-path-selector.c 2.6 KB

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