dm-exception-store.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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 % (bdev_logical_block_size(store->cow->bdev) >> 9)) {
  141. *error = "Chunk size is not a multiple of device blocksize";
  142. return -EINVAL;
  143. }
  144. if (chunk_size > INT_MAX >> SECTOR_SHIFT) {
  145. *error = "Chunk size is too high";
  146. return -EINVAL;
  147. }
  148. store->chunk_size = chunk_size;
  149. store->chunk_mask = chunk_size - 1;
  150. store->chunk_shift = ffs(chunk_size) - 1;
  151. return 0;
  152. }
  153. int dm_exception_store_create(struct dm_target *ti, int argc, char **argv,
  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 < 3) {
  162. ti->error = "Insufficient exception store arguments";
  163. return -EINVAL;
  164. }
  165. tmp_store = kmalloc(sizeof(*tmp_store), GFP_KERNEL);
  166. if (!tmp_store) {
  167. ti->error = "Exception store allocation failed";
  168. return -ENOMEM;
  169. }
  170. persistent = toupper(*argv[1]);
  171. if (persistent == 'P')
  172. type = get_type("P");
  173. else if (persistent == 'N')
  174. type = get_type("N");
  175. else {
  176. ti->error = "Persistent flag is not P or N";
  177. return -EINVAL;
  178. }
  179. if (!type) {
  180. ti->error = "Exception store type not recognised";
  181. r = -EINVAL;
  182. goto bad_type;
  183. }
  184. tmp_store->type = type;
  185. tmp_store->ti = ti;
  186. r = dm_get_device(ti, argv[0], 0, 0,
  187. FMODE_READ | FMODE_WRITE, &tmp_store->cow);
  188. if (r) {
  189. ti->error = "Cannot get COW device";
  190. goto bad_cow;
  191. }
  192. r = set_chunk_size(tmp_store, argv[2], &ti->error);
  193. if (r)
  194. goto bad_ctr;
  195. r = type->ctr(tmp_store, 0, NULL);
  196. if (r) {
  197. ti->error = "Exception store type constructor failed";
  198. goto bad_ctr;
  199. }
  200. *args_used = 3;
  201. *store = tmp_store;
  202. return 0;
  203. bad_ctr:
  204. dm_put_device(ti, tmp_store->cow);
  205. bad_cow:
  206. put_type(type);
  207. bad_type:
  208. kfree(tmp_store);
  209. return r;
  210. }
  211. EXPORT_SYMBOL(dm_exception_store_create);
  212. void dm_exception_store_destroy(struct dm_exception_store *store)
  213. {
  214. store->type->dtr(store);
  215. dm_put_device(store->ti, store->cow);
  216. put_type(store->type);
  217. kfree(store);
  218. }
  219. EXPORT_SYMBOL(dm_exception_store_destroy);
  220. int dm_exception_store_init(void)
  221. {
  222. int r;
  223. r = dm_transient_snapshot_init();
  224. if (r) {
  225. DMERR("Unable to register transient exception store type.");
  226. goto transient_fail;
  227. }
  228. r = dm_persistent_snapshot_init();
  229. if (r) {
  230. DMERR("Unable to register persistent exception store type");
  231. goto persistent_fail;
  232. }
  233. return 0;
  234. persistent_fail:
  235. dm_persistent_snapshot_exit();
  236. transient_fail:
  237. return r;
  238. }
  239. void dm_exception_store_exit(void)
  240. {
  241. dm_persistent_snapshot_exit();
  242. dm_transient_snapshot_exit();
  243. }