dm-exception-store.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. /*
  112. * Round a number up to the nearest 'size' boundary. size must
  113. * be a power of 2.
  114. */
  115. static ulong round_up(ulong n, ulong size)
  116. {
  117. size--;
  118. return (n + size) & ~size;
  119. }
  120. static int set_chunk_size(struct dm_exception_store *store,
  121. const char *chunk_size_arg, char **error)
  122. {
  123. unsigned long chunk_size_ulong;
  124. char *value;
  125. chunk_size_ulong = simple_strtoul(chunk_size_arg, &value, 10);
  126. if (*chunk_size_arg == '\0' || *value != '\0') {
  127. *error = "Invalid chunk size";
  128. return -EINVAL;
  129. }
  130. if (!chunk_size_ulong) {
  131. store->chunk_size = store->chunk_mask = store->chunk_shift = 0;
  132. return 0;
  133. }
  134. /*
  135. * Chunk size must be multiple of page size. Silently
  136. * round up if it's not.
  137. */
  138. chunk_size_ulong = round_up(chunk_size_ulong, PAGE_SIZE >> 9);
  139. /* Check chunk_size is a power of 2 */
  140. if (!is_power_of_2(chunk_size_ulong)) {
  141. *error = "Chunk size is not a power of 2";
  142. return -EINVAL;
  143. }
  144. /* Validate the chunk size against the device block size */
  145. if (chunk_size_ulong % (bdev_logical_block_size(store->cow->bdev) >> 9)) {
  146. *error = "Chunk size is not a multiple of device blocksize";
  147. return -EINVAL;
  148. }
  149. store->chunk_size = chunk_size_ulong;
  150. store->chunk_mask = chunk_size_ulong - 1;
  151. store->chunk_shift = ffs(chunk_size_ulong) - 1;
  152. return 0;
  153. }
  154. int dm_exception_store_create(struct dm_target *ti, int argc, char **argv,
  155. unsigned *args_used,
  156. struct dm_exception_store **store)
  157. {
  158. int r = 0;
  159. struct dm_exception_store_type *type = NULL;
  160. struct dm_exception_store *tmp_store;
  161. char persistent;
  162. if (argc < 3) {
  163. ti->error = "Insufficient exception store arguments";
  164. return -EINVAL;
  165. }
  166. tmp_store = kmalloc(sizeof(*tmp_store), GFP_KERNEL);
  167. if (!tmp_store) {
  168. ti->error = "Exception store allocation failed";
  169. return -ENOMEM;
  170. }
  171. persistent = toupper(*argv[1]);
  172. if (persistent == 'P')
  173. type = get_type("P");
  174. else if (persistent == 'N')
  175. type = get_type("N");
  176. else {
  177. ti->error = "Persistent flag is not P or N";
  178. return -EINVAL;
  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->ti = ti;
  187. r = dm_get_device(ti, argv[0], 0, 0,
  188. FMODE_READ | FMODE_WRITE, &tmp_store->cow);
  189. if (r) {
  190. ti->error = "Cannot get COW device";
  191. goto bad_cow;
  192. }
  193. r = set_chunk_size(tmp_store, argv[2], &ti->error);
  194. if (r)
  195. goto bad_cow;
  196. r = type->ctr(tmp_store, 0, NULL);
  197. if (r) {
  198. ti->error = "Exception store type constructor failed";
  199. goto bad_ctr;
  200. }
  201. *args_used = 3;
  202. *store = tmp_store;
  203. return 0;
  204. bad_ctr:
  205. dm_put_device(ti, tmp_store->cow);
  206. bad_cow:
  207. put_type(type);
  208. bad_type:
  209. kfree(tmp_store);
  210. return r;
  211. }
  212. EXPORT_SYMBOL(dm_exception_store_create);
  213. void dm_exception_store_destroy(struct dm_exception_store *store)
  214. {
  215. store->type->dtr(store);
  216. dm_put_device(store->ti, store->cow);
  217. put_type(store->type);
  218. kfree(store);
  219. }
  220. EXPORT_SYMBOL(dm_exception_store_destroy);
  221. int dm_exception_store_init(void)
  222. {
  223. int r;
  224. r = dm_transient_snapshot_init();
  225. if (r) {
  226. DMERR("Unable to register transient exception store type.");
  227. goto transient_fail;
  228. }
  229. r = dm_persistent_snapshot_init();
  230. if (r) {
  231. DMERR("Unable to register persistent exception store type");
  232. goto persistent_fail;
  233. }
  234. return 0;
  235. persistent_fail:
  236. dm_persistent_snapshot_exit();
  237. transient_fail:
  238. return r;
  239. }
  240. void dm_exception_store_exit(void)
  241. {
  242. dm_persistent_snapshot_exit();
  243. dm_transient_snapshot_exit();
  244. }