dm-transaction-manager.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /*
  2. * Copyright (C) 2011 Red Hat, Inc.
  3. *
  4. * This file is released under the GPL.
  5. */
  6. #include "dm-transaction-manager.h"
  7. #include "dm-space-map.h"
  8. #include "dm-space-map-disk.h"
  9. #include "dm-space-map-metadata.h"
  10. #include "dm-persistent-data-internal.h"
  11. #include <linux/export.h>
  12. #include <linux/slab.h>
  13. #include <linux/device-mapper.h>
  14. #define DM_MSG_PREFIX "transaction manager"
  15. /*----------------------------------------------------------------*/
  16. struct shadow_info {
  17. struct hlist_node hlist;
  18. dm_block_t where;
  19. };
  20. /*
  21. * It would be nice if we scaled with the size of transaction.
  22. */
  23. #define DM_HASH_SIZE 256
  24. #define DM_HASH_MASK (DM_HASH_SIZE - 1)
  25. struct dm_transaction_manager {
  26. int is_clone;
  27. struct dm_transaction_manager *real;
  28. struct dm_block_manager *bm;
  29. struct dm_space_map *sm;
  30. spinlock_t lock;
  31. struct hlist_head buckets[DM_HASH_SIZE];
  32. };
  33. /*----------------------------------------------------------------*/
  34. static int is_shadow(struct dm_transaction_manager *tm, dm_block_t b)
  35. {
  36. int r = 0;
  37. unsigned bucket = dm_hash_block(b, DM_HASH_MASK);
  38. struct shadow_info *si;
  39. spin_lock(&tm->lock);
  40. hlist_for_each_entry(si, tm->buckets + bucket, hlist)
  41. if (si->where == b) {
  42. r = 1;
  43. break;
  44. }
  45. spin_unlock(&tm->lock);
  46. return r;
  47. }
  48. /*
  49. * This can silently fail if there's no memory. We're ok with this since
  50. * creating redundant shadows causes no harm.
  51. */
  52. static void insert_shadow(struct dm_transaction_manager *tm, dm_block_t b)
  53. {
  54. unsigned bucket;
  55. struct shadow_info *si;
  56. si = kmalloc(sizeof(*si), GFP_NOIO);
  57. if (si) {
  58. si->where = b;
  59. bucket = dm_hash_block(b, DM_HASH_MASK);
  60. spin_lock(&tm->lock);
  61. hlist_add_head(&si->hlist, tm->buckets + bucket);
  62. spin_unlock(&tm->lock);
  63. }
  64. }
  65. static void wipe_shadow_table(struct dm_transaction_manager *tm)
  66. {
  67. struct shadow_info *si;
  68. struct hlist_node *tmp;
  69. struct hlist_head *bucket;
  70. int i;
  71. spin_lock(&tm->lock);
  72. for (i = 0; i < DM_HASH_SIZE; i++) {
  73. bucket = tm->buckets + i;
  74. hlist_for_each_entry_safe(si, tmp, bucket, hlist)
  75. kfree(si);
  76. INIT_HLIST_HEAD(bucket);
  77. }
  78. spin_unlock(&tm->lock);
  79. }
  80. /*----------------------------------------------------------------*/
  81. static struct dm_transaction_manager *dm_tm_create(struct dm_block_manager *bm,
  82. struct dm_space_map *sm)
  83. {
  84. int i;
  85. struct dm_transaction_manager *tm;
  86. tm = kmalloc(sizeof(*tm), GFP_KERNEL);
  87. if (!tm)
  88. return ERR_PTR(-ENOMEM);
  89. tm->is_clone = 0;
  90. tm->real = NULL;
  91. tm->bm = bm;
  92. tm->sm = sm;
  93. spin_lock_init(&tm->lock);
  94. for (i = 0; i < DM_HASH_SIZE; i++)
  95. INIT_HLIST_HEAD(tm->buckets + i);
  96. return tm;
  97. }
  98. struct dm_transaction_manager *dm_tm_create_non_blocking_clone(struct dm_transaction_manager *real)
  99. {
  100. struct dm_transaction_manager *tm;
  101. tm = kmalloc(sizeof(*tm), GFP_KERNEL);
  102. if (tm) {
  103. tm->is_clone = 1;
  104. tm->real = real;
  105. }
  106. return tm;
  107. }
  108. EXPORT_SYMBOL_GPL(dm_tm_create_non_blocking_clone);
  109. void dm_tm_destroy(struct dm_transaction_manager *tm)
  110. {
  111. if (!tm->is_clone)
  112. wipe_shadow_table(tm);
  113. kfree(tm);
  114. }
  115. EXPORT_SYMBOL_GPL(dm_tm_destroy);
  116. int dm_tm_pre_commit(struct dm_transaction_manager *tm)
  117. {
  118. int r;
  119. if (tm->is_clone)
  120. return -EWOULDBLOCK;
  121. r = dm_sm_commit(tm->sm);
  122. if (r < 0)
  123. return r;
  124. return 0;
  125. }
  126. EXPORT_SYMBOL_GPL(dm_tm_pre_commit);
  127. int dm_tm_commit(struct dm_transaction_manager *tm, struct dm_block *root)
  128. {
  129. if (tm->is_clone)
  130. return -EWOULDBLOCK;
  131. wipe_shadow_table(tm);
  132. return dm_bm_flush_and_unlock(tm->bm, root);
  133. }
  134. EXPORT_SYMBOL_GPL(dm_tm_commit);
  135. int dm_tm_new_block(struct dm_transaction_manager *tm,
  136. struct dm_block_validator *v,
  137. struct dm_block **result)
  138. {
  139. int r;
  140. dm_block_t new_block;
  141. if (tm->is_clone)
  142. return -EWOULDBLOCK;
  143. r = dm_sm_new_block(tm->sm, &new_block);
  144. if (r < 0)
  145. return r;
  146. r = dm_bm_write_lock_zero(tm->bm, new_block, v, result);
  147. if (r < 0) {
  148. dm_sm_dec_block(tm->sm, new_block);
  149. return r;
  150. }
  151. /*
  152. * New blocks count as shadows in that they don't need to be
  153. * shadowed again.
  154. */
  155. insert_shadow(tm, new_block);
  156. return 0;
  157. }
  158. static int __shadow_block(struct dm_transaction_manager *tm, dm_block_t orig,
  159. struct dm_block_validator *v,
  160. struct dm_block **result)
  161. {
  162. int r;
  163. dm_block_t new;
  164. struct dm_block *orig_block;
  165. r = dm_sm_new_block(tm->sm, &new);
  166. if (r < 0)
  167. return r;
  168. r = dm_sm_dec_block(tm->sm, orig);
  169. if (r < 0)
  170. return r;
  171. r = dm_bm_read_lock(tm->bm, orig, v, &orig_block);
  172. if (r < 0)
  173. return r;
  174. /*
  175. * It would be tempting to use dm_bm_unlock_move here, but some
  176. * code, such as the space maps, keeps using the old data structures
  177. * secure in the knowledge they won't be changed until the next
  178. * transaction. Using unlock_move would force a synchronous read
  179. * since the old block would no longer be in the cache.
  180. */
  181. r = dm_bm_write_lock_zero(tm->bm, new, v, result);
  182. if (r) {
  183. dm_bm_unlock(orig_block);
  184. return r;
  185. }
  186. memcpy(dm_block_data(*result), dm_block_data(orig_block),
  187. dm_bm_block_size(tm->bm));
  188. dm_bm_unlock(orig_block);
  189. return r;
  190. }
  191. int dm_tm_shadow_block(struct dm_transaction_manager *tm, dm_block_t orig,
  192. struct dm_block_validator *v, struct dm_block **result,
  193. int *inc_children)
  194. {
  195. int r;
  196. if (tm->is_clone)
  197. return -EWOULDBLOCK;
  198. r = dm_sm_count_is_more_than_one(tm->sm, orig, inc_children);
  199. if (r < 0)
  200. return r;
  201. if (is_shadow(tm, orig) && !*inc_children)
  202. return dm_bm_write_lock(tm->bm, orig, v, result);
  203. r = __shadow_block(tm, orig, v, result);
  204. if (r < 0)
  205. return r;
  206. insert_shadow(tm, dm_block_location(*result));
  207. return r;
  208. }
  209. EXPORT_SYMBOL_GPL(dm_tm_shadow_block);
  210. int dm_tm_read_lock(struct dm_transaction_manager *tm, dm_block_t b,
  211. struct dm_block_validator *v,
  212. struct dm_block **blk)
  213. {
  214. if (tm->is_clone)
  215. return dm_bm_read_try_lock(tm->real->bm, b, v, blk);
  216. return dm_bm_read_lock(tm->bm, b, v, blk);
  217. }
  218. EXPORT_SYMBOL_GPL(dm_tm_read_lock);
  219. int dm_tm_unlock(struct dm_transaction_manager *tm, struct dm_block *b)
  220. {
  221. return dm_bm_unlock(b);
  222. }
  223. EXPORT_SYMBOL_GPL(dm_tm_unlock);
  224. void dm_tm_inc(struct dm_transaction_manager *tm, dm_block_t b)
  225. {
  226. /*
  227. * The non-blocking clone doesn't support this.
  228. */
  229. BUG_ON(tm->is_clone);
  230. dm_sm_inc_block(tm->sm, b);
  231. }
  232. EXPORT_SYMBOL_GPL(dm_tm_inc);
  233. void dm_tm_dec(struct dm_transaction_manager *tm, dm_block_t b)
  234. {
  235. /*
  236. * The non-blocking clone doesn't support this.
  237. */
  238. BUG_ON(tm->is_clone);
  239. dm_sm_dec_block(tm->sm, b);
  240. }
  241. EXPORT_SYMBOL_GPL(dm_tm_dec);
  242. int dm_tm_ref(struct dm_transaction_manager *tm, dm_block_t b,
  243. uint32_t *result)
  244. {
  245. if (tm->is_clone)
  246. return -EWOULDBLOCK;
  247. return dm_sm_get_count(tm->sm, b, result);
  248. }
  249. struct dm_block_manager *dm_tm_get_bm(struct dm_transaction_manager *tm)
  250. {
  251. return tm->bm;
  252. }
  253. /*----------------------------------------------------------------*/
  254. static int dm_tm_create_internal(struct dm_block_manager *bm,
  255. dm_block_t sb_location,
  256. struct dm_transaction_manager **tm,
  257. struct dm_space_map **sm,
  258. int create,
  259. void *sm_root, size_t sm_len)
  260. {
  261. int r;
  262. *sm = dm_sm_metadata_init();
  263. if (IS_ERR(*sm))
  264. return PTR_ERR(*sm);
  265. *tm = dm_tm_create(bm, *sm);
  266. if (IS_ERR(*tm)) {
  267. dm_sm_destroy(*sm);
  268. return PTR_ERR(*tm);
  269. }
  270. if (create) {
  271. r = dm_sm_metadata_create(*sm, *tm, dm_bm_nr_blocks(bm),
  272. sb_location);
  273. if (r) {
  274. DMERR("couldn't create metadata space map");
  275. goto bad;
  276. }
  277. } else {
  278. r = dm_sm_metadata_open(*sm, *tm, sm_root, sm_len);
  279. if (r) {
  280. DMERR("couldn't open metadata space map");
  281. goto bad;
  282. }
  283. }
  284. return 0;
  285. bad:
  286. dm_tm_destroy(*tm);
  287. dm_sm_destroy(*sm);
  288. return r;
  289. }
  290. int dm_tm_create_with_sm(struct dm_block_manager *bm, dm_block_t sb_location,
  291. struct dm_transaction_manager **tm,
  292. struct dm_space_map **sm)
  293. {
  294. return dm_tm_create_internal(bm, sb_location, tm, sm, 1, NULL, 0);
  295. }
  296. EXPORT_SYMBOL_GPL(dm_tm_create_with_sm);
  297. int dm_tm_open_with_sm(struct dm_block_manager *bm, dm_block_t sb_location,
  298. void *sm_root, size_t root_len,
  299. struct dm_transaction_manager **tm,
  300. struct dm_space_map **sm)
  301. {
  302. return dm_tm_create_internal(bm, sb_location, tm, sm, 0, sm_root, root_len);
  303. }
  304. EXPORT_SYMBOL_GPL(dm_tm_open_with_sm);
  305. /*----------------------------------------------------------------*/