dm-exception-store.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * Copyright (C) 2001-2002 Sistina Software (UK) Limited.
  3. * Copyright (C) 2006-2008 Red Hat GmbH
  4. *
  5. * This file is released under the GPL.
  6. */
  7. #include "dm-exception-store.h"
  8. #include <linux/mm.h>
  9. #include <linux/pagemap.h>
  10. #include <linux/vmalloc.h>
  11. #include <linux/slab.h>
  12. #define DM_MSG_PREFIX "snapshot exception stores"
  13. static LIST_HEAD(_exception_store_types);
  14. static DEFINE_SPINLOCK(_lock);
  15. static struct dm_exception_store_type *__find_exception_store_type(const char *name)
  16. {
  17. struct dm_exception_store_type *type;
  18. list_for_each_entry(type, &_exception_store_types, list)
  19. if (!strcmp(name, type->name))
  20. return type;
  21. return NULL;
  22. }
  23. static struct dm_exception_store_type *_get_exception_store_type(const char *name)
  24. {
  25. struct dm_exception_store_type *type;
  26. spin_lock(&_lock);
  27. type = __find_exception_store_type(name);
  28. if (type && !try_module_get(type->module))
  29. type = NULL;
  30. spin_unlock(&_lock);
  31. return type;
  32. }
  33. /*
  34. * get_type
  35. * @type_name
  36. *
  37. * Attempt to retrieve the dm_exception_store_type by name. If not already
  38. * available, attempt to load the appropriate module.
  39. *
  40. * Exstore modules are named "dm-exstore-" followed by the 'type_name'.
  41. * Modules may contain multiple types.
  42. * This function will first try the module "dm-exstore-<type_name>",
  43. * then truncate 'type_name' on the last '-' and try again.
  44. *
  45. * For example, if type_name was "clustered-shared", it would search
  46. * 'dm-exstore-clustered-shared' then 'dm-exstore-clustered'.
  47. *
  48. * 'dm-exception-store-<type_name>' is too long of a name in my
  49. * opinion, which is why I've chosen to have the files
  50. * containing exception store implementations be 'dm-exstore-<type_name>'.
  51. * If you want your module to be autoloaded, you will follow this
  52. * naming convention.
  53. *
  54. * Returns: dm_exception_store_type* on success, NULL on failure
  55. */
  56. static struct dm_exception_store_type *get_type(const char *type_name)
  57. {
  58. char *p, *type_name_dup;
  59. struct dm_exception_store_type *type;
  60. type = _get_exception_store_type(type_name);
  61. if (type)
  62. return type;
  63. type_name_dup = kstrdup(type_name, GFP_KERNEL);
  64. if (!type_name_dup) {
  65. DMERR("No memory left to attempt load for \"%s\"", type_name);
  66. return NULL;
  67. }
  68. while (request_module("dm-exstore-%s", type_name_dup) ||
  69. !(type = _get_exception_store_type(type_name))) {
  70. p = strrchr(type_name_dup, '-');
  71. if (!p)
  72. break;
  73. p[0] = '\0';
  74. }
  75. if (!type)
  76. DMWARN("Module for exstore type \"%s\" not found.", type_name);
  77. kfree(type_name_dup);
  78. return type;
  79. }
  80. static void put_type(struct dm_exception_store_type *type)
  81. {
  82. spin_lock(&_lock);
  83. module_put(type->module);
  84. spin_unlock(&_lock);
  85. }
  86. int dm_exception_store_type_register(struct dm_exception_store_type *type)
  87. {
  88. int r = 0;
  89. spin_lock(&_lock);
  90. if (!__find_exception_store_type(type->name))
  91. list_add(&type->list, &_exception_store_types);
  92. else
  93. r = -EEXIST;
  94. spin_unlock(&_lock);
  95. return r;
  96. }
  97. EXPORT_SYMBOL(dm_exception_store_type_register);
  98. int dm_exception_store_type_unregister(struct dm_exception_store_type *type)
  99. {
  100. spin_lock(&_lock);
  101. if (!__find_exception_store_type(type->name)) {
  102. spin_unlock(&_lock);
  103. return -EINVAL;
  104. }
  105. list_del(&type->list);
  106. spin_unlock(&_lock);
  107. return 0;
  108. }
  109. EXPORT_SYMBOL(dm_exception_store_type_unregister);
  110. int dm_exception_store_create(const char *type_name, struct dm_target *ti,
  111. chunk_t chunk_size, chunk_t chunk_mask,
  112. chunk_t chunk_shift, struct dm_dev *cow,
  113. struct dm_exception_store **store)
  114. {
  115. int r = 0;
  116. struct dm_exception_store_type *type;
  117. struct dm_exception_store *tmp_store;
  118. tmp_store = kmalloc(sizeof(*tmp_store), GFP_KERNEL);
  119. if (!tmp_store)
  120. return -ENOMEM;
  121. type = get_type(type_name);
  122. if (!type) {
  123. kfree(tmp_store);
  124. return -EINVAL;
  125. }
  126. tmp_store->type = type;
  127. tmp_store->ti = ti;
  128. tmp_store->chunk_size = chunk_size;
  129. tmp_store->chunk_mask = chunk_mask;
  130. tmp_store->chunk_shift = chunk_shift;
  131. tmp_store->cow = cow;
  132. r = type->ctr(tmp_store, 0, NULL);
  133. if (r) {
  134. put_type(type);
  135. kfree(tmp_store);
  136. return r;
  137. }
  138. *store = tmp_store;
  139. return 0;
  140. }
  141. EXPORT_SYMBOL(dm_exception_store_create);
  142. void dm_exception_store_destroy(struct dm_exception_store *store)
  143. {
  144. store->type->dtr(store);
  145. put_type(store->type);
  146. kfree(store);
  147. }
  148. EXPORT_SYMBOL(dm_exception_store_destroy);
  149. int dm_exception_store_init(void)
  150. {
  151. int r;
  152. r = dm_transient_snapshot_init();
  153. if (r) {
  154. DMERR("Unable to register transient exception store type.");
  155. goto transient_fail;
  156. }
  157. r = dm_persistent_snapshot_init();
  158. if (r) {
  159. DMERR("Unable to register persistent exception store type");
  160. goto persistent_fail;
  161. }
  162. return 0;
  163. persistent_fail:
  164. dm_persistent_snapshot_exit();
  165. transient_fail:
  166. return r;
  167. }
  168. void dm_exception_store_exit(void)
  169. {
  170. dm_persistent_snapshot_exit();
  171. dm_transient_snapshot_exit();
  172. }