dm-exception-store.c 6.5 KB

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