dm-exception-store.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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/ctype.h>
  9. #include <linux/mm.h>
  10. #include <linux/pagemap.h>
  11. #include <linux/vmalloc.h>
  12. #include <linux/module.h>
  13. #include <linux/slab.h>
  14. #define DM_MSG_PREFIX "snapshot exception stores"
  15. static LIST_HEAD(_exception_store_types);
  16. static DEFINE_SPINLOCK(_lock);
  17. static struct dm_exception_store_type *__find_exception_store_type(const char *name)
  18. {
  19. struct dm_exception_store_type *type;
  20. list_for_each_entry(type, &_exception_store_types, list)
  21. if (!strcmp(name, type->name))
  22. return type;
  23. return NULL;
  24. }
  25. static struct dm_exception_store_type *_get_exception_store_type(const char *name)
  26. {
  27. struct dm_exception_store_type *type;
  28. spin_lock(&_lock);
  29. type = __find_exception_store_type(name);
  30. if (type && !try_module_get(type->module))
  31. type = NULL;
  32. spin_unlock(&_lock);
  33. return type;
  34. }
  35. /*
  36. * get_type
  37. * @type_name
  38. *
  39. * Attempt to retrieve the dm_exception_store_type by name. If not already
  40. * available, attempt to load the appropriate module.
  41. *
  42. * Exstore modules are named "dm-exstore-" followed by the 'type_name'.
  43. * Modules may contain multiple types.
  44. * This function will first try the module "dm-exstore-<type_name>",
  45. * then truncate 'type_name' on the last '-' and try again.
  46. *
  47. * For example, if type_name was "clustered-shared", it would search
  48. * 'dm-exstore-clustered-shared' then 'dm-exstore-clustered'.
  49. *
  50. * 'dm-exception-store-<type_name>' is too long of a name in my
  51. * opinion, which is why I've chosen to have the files
  52. * containing exception store implementations be 'dm-exstore-<type_name>'.
  53. * If you want your module to be autoloaded, you will follow this
  54. * naming convention.
  55. *
  56. * Returns: dm_exception_store_type* on success, NULL on failure
  57. */
  58. static struct dm_exception_store_type *get_type(const char *type_name)
  59. {
  60. char *p, *type_name_dup;
  61. struct dm_exception_store_type *type;
  62. type = _get_exception_store_type(type_name);
  63. if (type)
  64. return type;
  65. type_name_dup = kstrdup(type_name, GFP_KERNEL);
  66. if (!type_name_dup) {
  67. DMERR("No memory left to attempt load for \"%s\"", type_name);
  68. return NULL;
  69. }
  70. while (request_module("dm-exstore-%s", type_name_dup) ||
  71. !(type = _get_exception_store_type(type_name))) {
  72. p = strrchr(type_name_dup, '-');
  73. if (!p)
  74. break;
  75. p[0] = '\0';
  76. }
  77. if (!type)
  78. DMWARN("Module for exstore type \"%s\" not found.", type_name);
  79. kfree(type_name_dup);
  80. return type;
  81. }
  82. static void put_type(struct dm_exception_store_type *type)
  83. {
  84. spin_lock(&_lock);
  85. module_put(type->module);
  86. spin_unlock(&_lock);
  87. }
  88. int dm_exception_store_type_register(struct dm_exception_store_type *type)
  89. {
  90. int r = 0;
  91. spin_lock(&_lock);
  92. if (!__find_exception_store_type(type->name))
  93. list_add(&type->list, &_exception_store_types);
  94. else
  95. r = -EEXIST;
  96. spin_unlock(&_lock);
  97. return r;
  98. }
  99. EXPORT_SYMBOL(dm_exception_store_type_register);
  100. int dm_exception_store_type_unregister(struct dm_exception_store_type *type)
  101. {
  102. spin_lock(&_lock);
  103. if (!__find_exception_store_type(type->name)) {
  104. spin_unlock(&_lock);
  105. return -EINVAL;
  106. }
  107. list_del(&type->list);
  108. spin_unlock(&_lock);
  109. return 0;
  110. }
  111. EXPORT_SYMBOL(dm_exception_store_type_unregister);
  112. static int set_chunk_size(struct dm_exception_store *store,
  113. const char *chunk_size_arg, char **error)
  114. {
  115. unsigned chunk_size;
  116. if (kstrtouint(chunk_size_arg, 10, &chunk_size)) {
  117. *error = "Invalid chunk size";
  118. return -EINVAL;
  119. }
  120. if (!chunk_size) {
  121. store->chunk_size = store->chunk_mask = store->chunk_shift = 0;
  122. return 0;
  123. }
  124. return dm_exception_store_set_chunk_size(store, chunk_size, error);
  125. }
  126. int dm_exception_store_set_chunk_size(struct dm_exception_store *store,
  127. unsigned chunk_size,
  128. char **error)
  129. {
  130. /* Check chunk_size is a power of 2 */
  131. if (!is_power_of_2(chunk_size)) {
  132. *error = "Chunk size is not a power of 2";
  133. return -EINVAL;
  134. }
  135. /* Validate the chunk size against the device block size */
  136. if (chunk_size %
  137. (bdev_logical_block_size(dm_snap_cow(store->snap)->bdev) >> 9) ||
  138. chunk_size %
  139. (bdev_logical_block_size(dm_snap_origin(store->snap)->bdev) >> 9)) {
  140. *error = "Chunk size is not a multiple of device blocksize";
  141. return -EINVAL;
  142. }
  143. if (chunk_size > INT_MAX >> SECTOR_SHIFT) {
  144. *error = "Chunk size is too high";
  145. return -EINVAL;
  146. }
  147. store->chunk_size = chunk_size;
  148. store->chunk_mask = chunk_size - 1;
  149. store->chunk_shift = __ffs(chunk_size);
  150. return 0;
  151. }
  152. int dm_exception_store_create(struct dm_target *ti, int argc, char **argv,
  153. struct dm_snapshot *snap,
  154. unsigned *args_used,
  155. struct dm_exception_store **store)
  156. {
  157. int r = 0;
  158. struct dm_exception_store_type *type = NULL;
  159. struct dm_exception_store *tmp_store;
  160. char persistent;
  161. if (argc < 2) {
  162. ti->error = "Insufficient exception store arguments";
  163. return -EINVAL;
  164. }
  165. tmp_store = kzalloc(sizeof(*tmp_store), GFP_KERNEL);
  166. if (!tmp_store) {
  167. ti->error = "Exception store allocation failed";
  168. return -ENOMEM;
  169. }
  170. persistent = toupper(*argv[0]);
  171. if (persistent == 'P')
  172. type = get_type("P");
  173. else if (persistent == 'N')
  174. type = get_type("N");
  175. else {
  176. ti->error = "Exception store type is not P or N";
  177. r = -EINVAL;
  178. goto bad_type;
  179. }
  180. if (!type) {
  181. ti->error = "Exception store type not recognised";
  182. r = -EINVAL;
  183. goto bad_type;
  184. }
  185. tmp_store->type = type;
  186. tmp_store->snap = snap;
  187. r = set_chunk_size(tmp_store, argv[1], &ti->error);
  188. if (r)
  189. goto bad;
  190. r = type->ctr(tmp_store, (strlen(argv[0]) > 1 ? &argv[0][1] : NULL));
  191. if (r) {
  192. ti->error = "Exception store type constructor failed";
  193. goto bad;
  194. }
  195. *args_used = 2;
  196. *store = tmp_store;
  197. return 0;
  198. bad:
  199. put_type(type);
  200. bad_type:
  201. kfree(tmp_store);
  202. return r;
  203. }
  204. EXPORT_SYMBOL(dm_exception_store_create);
  205. void dm_exception_store_destroy(struct dm_exception_store *store)
  206. {
  207. store->type->dtr(store);
  208. put_type(store->type);
  209. kfree(store);
  210. }
  211. EXPORT_SYMBOL(dm_exception_store_destroy);
  212. int dm_exception_store_init(void)
  213. {
  214. int r;
  215. r = dm_transient_snapshot_init();
  216. if (r) {
  217. DMERR("Unable to register transient exception store type.");
  218. goto transient_fail;
  219. }
  220. r = dm_persistent_snapshot_init();
  221. if (r) {
  222. DMERR("Unable to register persistent exception store type");
  223. goto persistent_fail;
  224. }
  225. return 0;
  226. persistent_fail:
  227. dm_transient_snapshot_exit();
  228. transient_fail:
  229. return r;
  230. }
  231. void dm_exception_store_exit(void)
  232. {
  233. dm_persistent_snapshot_exit();
  234. dm_transient_snapshot_exit();
  235. }