dm-path-selector.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 <linux/module.h>
  13. #include "dm-path-selector.h"
  14. #include <linux/slab.h>
  15. struct ps_internal {
  16. struct path_selector_type pst;
  17. struct list_head list;
  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 && !try_module_get(psi->pst.module))
  37. psi = NULL;
  38. up_read(&_ps_lock);
  39. return psi;
  40. }
  41. struct path_selector_type *dm_get_path_selector(const char *name)
  42. {
  43. struct ps_internal *psi;
  44. if (!name)
  45. return NULL;
  46. psi = get_path_selector(name);
  47. if (!psi) {
  48. request_module("dm-%s", name);
  49. psi = get_path_selector(name);
  50. }
  51. return psi ? &psi->pst : NULL;
  52. }
  53. void dm_put_path_selector(struct path_selector_type *pst)
  54. {
  55. struct ps_internal *psi;
  56. if (!pst)
  57. return;
  58. down_read(&_ps_lock);
  59. psi = __find_path_selector_type(pst->name);
  60. if (!psi)
  61. goto out;
  62. module_put(psi->pst.module);
  63. out:
  64. up_read(&_ps_lock);
  65. }
  66. static struct ps_internal *_alloc_path_selector(struct path_selector_type *pst)
  67. {
  68. struct ps_internal *psi = kzalloc(sizeof(*psi), GFP_KERNEL);
  69. if (psi)
  70. psi->pst = *pst;
  71. return psi;
  72. }
  73. int dm_register_path_selector(struct path_selector_type *pst)
  74. {
  75. int r = 0;
  76. struct ps_internal *psi = _alloc_path_selector(pst);
  77. if (!psi)
  78. return -ENOMEM;
  79. down_write(&_ps_lock);
  80. if (__find_path_selector_type(pst->name)) {
  81. kfree(psi);
  82. r = -EEXIST;
  83. } else
  84. list_add(&psi->list, &_path_selectors);
  85. up_write(&_ps_lock);
  86. return r;
  87. }
  88. int dm_unregister_path_selector(struct path_selector_type *pst)
  89. {
  90. struct ps_internal *psi;
  91. down_write(&_ps_lock);
  92. psi = __find_path_selector_type(pst->name);
  93. if (!psi) {
  94. up_write(&_ps_lock);
  95. return -EINVAL;
  96. }
  97. list_del(&psi->list);
  98. up_write(&_ps_lock);
  99. kfree(psi);
  100. return 0;
  101. }
  102. EXPORT_SYMBOL_GPL(dm_register_path_selector);
  103. EXPORT_SYMBOL_GPL(dm_unregister_path_selector);