dm-path-selector.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 <linux/device-mapper.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. };
  18. #define pst_to_psi(__pst) container_of((__pst), struct ps_internal, pst)
  19. static LIST_HEAD(_path_selectors);
  20. static DECLARE_RWSEM(_ps_lock);
  21. static struct ps_internal *__find_path_selector_type(const char *name)
  22. {
  23. struct ps_internal *psi;
  24. list_for_each_entry(psi, &_path_selectors, list) {
  25. if (!strcmp(name, psi->pst.name))
  26. return psi;
  27. }
  28. return NULL;
  29. }
  30. static struct ps_internal *get_path_selector(const char *name)
  31. {
  32. struct ps_internal *psi;
  33. down_read(&_ps_lock);
  34. psi = __find_path_selector_type(name);
  35. if (psi && !try_module_get(psi->pst.module))
  36. psi = NULL;
  37. up_read(&_ps_lock);
  38. return psi;
  39. }
  40. struct path_selector_type *dm_get_path_selector(const char *name)
  41. {
  42. struct ps_internal *psi;
  43. if (!name)
  44. return NULL;
  45. psi = get_path_selector(name);
  46. if (!psi) {
  47. request_module("dm-%s", name);
  48. psi = get_path_selector(name);
  49. }
  50. return psi ? &psi->pst : NULL;
  51. }
  52. void dm_put_path_selector(struct path_selector_type *pst)
  53. {
  54. struct ps_internal *psi;
  55. if (!pst)
  56. return;
  57. down_read(&_ps_lock);
  58. psi = __find_path_selector_type(pst->name);
  59. if (!psi)
  60. goto out;
  61. module_put(psi->pst.module);
  62. out:
  63. up_read(&_ps_lock);
  64. }
  65. static struct ps_internal *_alloc_path_selector(struct path_selector_type *pst)
  66. {
  67. struct ps_internal *psi = kzalloc(sizeof(*psi), GFP_KERNEL);
  68. if (psi)
  69. psi->pst = *pst;
  70. return psi;
  71. }
  72. int dm_register_path_selector(struct path_selector_type *pst)
  73. {
  74. int r = 0;
  75. struct ps_internal *psi = _alloc_path_selector(pst);
  76. if (!psi)
  77. return -ENOMEM;
  78. down_write(&_ps_lock);
  79. if (__find_path_selector_type(pst->name)) {
  80. kfree(psi);
  81. r = -EEXIST;
  82. } else
  83. list_add(&psi->list, &_path_selectors);
  84. up_write(&_ps_lock);
  85. return r;
  86. }
  87. int dm_unregister_path_selector(struct path_selector_type *pst)
  88. {
  89. struct ps_internal *psi;
  90. down_write(&_ps_lock);
  91. psi = __find_path_selector_type(pst->name);
  92. if (!psi) {
  93. up_write(&_ps_lock);
  94. return -EINVAL;
  95. }
  96. list_del(&psi->list);
  97. up_write(&_ps_lock);
  98. kfree(psi);
  99. return 0;
  100. }
  101. EXPORT_SYMBOL_GPL(dm_register_path_selector);
  102. EXPORT_SYMBOL_GPL(dm_unregister_path_selector);