dm-transaction-manager.c 8.0 KB

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