dm-cache-policy.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2012 Red Hat. All rights reserved.
  4. *
  5. * This file is released under the GPL.
  6. */
  7. #include "dm-cache-policy-internal.h"
  8. #include "dm.h"
  9. #include <linux/module.h>
  10. #include <linux/slab.h>
  11. /*----------------------------------------------------------------*/
  12. #define DM_MSG_PREFIX "cache-policy"
  13. static DEFINE_SPINLOCK(register_lock);
  14. static LIST_HEAD(register_list);
  15. static struct dm_cache_policy_type *__find_policy(const char *name)
  16. {
  17. struct dm_cache_policy_type *t;
  18. list_for_each_entry(t, &register_list, list)
  19. if (!strcmp(t->name, name))
  20. return t;
  21. return NULL;
  22. }
  23. static struct dm_cache_policy_type *__get_policy_once(const char *name)
  24. {
  25. struct dm_cache_policy_type *t = __find_policy(name);
  26. if (t && !try_module_get(t->owner)) {
  27. DMWARN("couldn't get module %s", name);
  28. t = ERR_PTR(-EINVAL);
  29. }
  30. return t;
  31. }
  32. static struct dm_cache_policy_type *get_policy_once(const char *name)
  33. {
  34. struct dm_cache_policy_type *t;
  35. spin_lock(&register_lock);
  36. t = __get_policy_once(name);
  37. spin_unlock(&register_lock);
  38. return t;
  39. }
  40. static struct dm_cache_policy_type *get_policy(const char *name)
  41. {
  42. struct dm_cache_policy_type *t;
  43. t = get_policy_once(name);
  44. if (IS_ERR(t))
  45. return NULL;
  46. if (t)
  47. return t;
  48. request_module("dm-cache-%s", name);
  49. t = get_policy_once(name);
  50. if (IS_ERR(t))
  51. return NULL;
  52. return t;
  53. }
  54. static void put_policy(struct dm_cache_policy_type *t)
  55. {
  56. module_put(t->owner);
  57. }
  58. int dm_cache_policy_register(struct dm_cache_policy_type *type)
  59. {
  60. int r;
  61. /* One size fits all for now */
  62. if (type->hint_size != 0 && type->hint_size != 4) {
  63. DMWARN("hint size must be 0 or 4 but %llu supplied.", (unsigned long long) type->hint_size);
  64. return -EINVAL;
  65. }
  66. spin_lock(&register_lock);
  67. if (__find_policy(type->name)) {
  68. DMWARN("attempt to register policy under duplicate name %s", type->name);
  69. r = -EINVAL;
  70. } else {
  71. list_add(&type->list, &register_list);
  72. r = 0;
  73. }
  74. spin_unlock(&register_lock);
  75. return r;
  76. }
  77. EXPORT_SYMBOL_GPL(dm_cache_policy_register);
  78. void dm_cache_policy_unregister(struct dm_cache_policy_type *type)
  79. {
  80. spin_lock(&register_lock);
  81. list_del_init(&type->list);
  82. spin_unlock(&register_lock);
  83. }
  84. EXPORT_SYMBOL_GPL(dm_cache_policy_unregister);
  85. struct dm_cache_policy *dm_cache_policy_create(const char *name,
  86. dm_cblock_t cache_size,
  87. sector_t origin_size,
  88. sector_t cache_block_size)
  89. {
  90. struct dm_cache_policy *p = NULL;
  91. struct dm_cache_policy_type *type;
  92. type = get_policy(name);
  93. if (!type) {
  94. DMWARN("unknown policy type");
  95. return ERR_PTR(-EINVAL);
  96. }
  97. p = type->create(cache_size, origin_size, cache_block_size);
  98. if (!p) {
  99. put_policy(type);
  100. return ERR_PTR(-ENOMEM);
  101. }
  102. p->private = type;
  103. return p;
  104. }
  105. EXPORT_SYMBOL_GPL(dm_cache_policy_create);
  106. void dm_cache_policy_destroy(struct dm_cache_policy *p)
  107. {
  108. struct dm_cache_policy_type *t = p->private;
  109. p->destroy(p);
  110. put_policy(t);
  111. }
  112. EXPORT_SYMBOL_GPL(dm_cache_policy_destroy);
  113. const char *dm_cache_policy_get_name(struct dm_cache_policy *p)
  114. {
  115. struct dm_cache_policy_type *t = p->private;
  116. /* if t->real is set then an alias was used (e.g. "default") */
  117. if (t->real)
  118. return t->real->name;
  119. return t->name;
  120. }
  121. EXPORT_SYMBOL_GPL(dm_cache_policy_get_name);
  122. const unsigned int *dm_cache_policy_get_version(struct dm_cache_policy *p)
  123. {
  124. struct dm_cache_policy_type *t = p->private;
  125. return t->version;
  126. }
  127. EXPORT_SYMBOL_GPL(dm_cache_policy_get_version);
  128. size_t dm_cache_policy_get_hint_size(struct dm_cache_policy *p)
  129. {
  130. struct dm_cache_policy_type *t = p->private;
  131. return t->hint_size;
  132. }
  133. EXPORT_SYMBOL_GPL(dm_cache_policy_get_hint_size);
  134. /*----------------------------------------------------------------*/