dm-path-selector.c 2.5 KB

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