dm-exception-store.c 6.9 KB

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