dm-cache-policy.c 3.6 KB

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