dm-transaction-manager.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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. r = dm_bm_unlock_move(orig_block, new);
  176. if (r < 0) {
  177. dm_bm_unlock(orig_block);
  178. return r;
  179. }
  180. return dm_bm_write_lock(tm->bm, new, v, result);
  181. }
  182. int dm_tm_shadow_block(struct dm_transaction_manager *tm, dm_block_t orig,
  183. struct dm_block_validator *v, struct dm_block **result,
  184. int *inc_children)
  185. {
  186. int r;
  187. if (tm->is_clone)
  188. return -EWOULDBLOCK;
  189. r = dm_sm_count_is_more_than_one(tm->sm, orig, inc_children);
  190. if (r < 0)
  191. return r;
  192. if (is_shadow(tm, orig) && !*inc_children)
  193. return dm_bm_write_lock(tm->bm, orig, v, result);
  194. r = __shadow_block(tm, orig, v, result);
  195. if (r < 0)
  196. return r;
  197. insert_shadow(tm, dm_block_location(*result));
  198. return r;
  199. }
  200. EXPORT_SYMBOL_GPL(dm_tm_shadow_block);
  201. int dm_tm_read_lock(struct dm_transaction_manager *tm, dm_block_t b,
  202. struct dm_block_validator *v,
  203. struct dm_block **blk)
  204. {
  205. if (tm->is_clone)
  206. return dm_bm_read_try_lock(tm->real->bm, b, v, blk);
  207. return dm_bm_read_lock(tm->bm, b, v, blk);
  208. }
  209. EXPORT_SYMBOL_GPL(dm_tm_read_lock);
  210. int dm_tm_unlock(struct dm_transaction_manager *tm, struct dm_block *b)
  211. {
  212. return dm_bm_unlock(b);
  213. }
  214. EXPORT_SYMBOL_GPL(dm_tm_unlock);
  215. void dm_tm_inc(struct dm_transaction_manager *tm, dm_block_t b)
  216. {
  217. /*
  218. * The non-blocking clone doesn't support this.
  219. */
  220. BUG_ON(tm->is_clone);
  221. dm_sm_inc_block(tm->sm, b);
  222. }
  223. EXPORT_SYMBOL_GPL(dm_tm_inc);
  224. void dm_tm_dec(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_dec_block(tm->sm, b);
  231. }
  232. EXPORT_SYMBOL_GPL(dm_tm_dec);
  233. int dm_tm_ref(struct dm_transaction_manager *tm, dm_block_t b,
  234. uint32_t *result)
  235. {
  236. if (tm->is_clone)
  237. return -EWOULDBLOCK;
  238. return dm_sm_get_count(tm->sm, b, result);
  239. }
  240. struct dm_block_manager *dm_tm_get_bm(struct dm_transaction_manager *tm)
  241. {
  242. return tm->bm;
  243. }
  244. /*----------------------------------------------------------------*/
  245. static int dm_tm_create_internal(struct dm_block_manager *bm,
  246. dm_block_t sb_location,
  247. struct dm_block_validator *sb_validator,
  248. size_t root_offset, size_t root_max_len,
  249. struct dm_transaction_manager **tm,
  250. struct dm_space_map **sm,
  251. struct dm_block **sblock,
  252. int create)
  253. {
  254. int r;
  255. *sm = dm_sm_metadata_init();
  256. if (IS_ERR(*sm))
  257. return PTR_ERR(*sm);
  258. *tm = dm_tm_create(bm, *sm);
  259. if (IS_ERR(*tm)) {
  260. dm_sm_destroy(*sm);
  261. return PTR_ERR(*tm);
  262. }
  263. if (create) {
  264. r = dm_bm_write_lock_zero(dm_tm_get_bm(*tm), sb_location,
  265. sb_validator, sblock);
  266. if (r < 0) {
  267. DMERR("couldn't lock superblock");
  268. goto bad1;
  269. }
  270. r = dm_sm_metadata_create(*sm, *tm, dm_bm_nr_blocks(bm),
  271. sb_location);
  272. if (r) {
  273. DMERR("couldn't create metadata space map");
  274. goto bad2;
  275. }
  276. } else {
  277. r = dm_bm_write_lock(dm_tm_get_bm(*tm), sb_location,
  278. sb_validator, sblock);
  279. if (r < 0) {
  280. DMERR("couldn't lock superblock");
  281. goto bad1;
  282. }
  283. r = dm_sm_metadata_open(*sm, *tm,
  284. dm_block_data(*sblock) + root_offset,
  285. root_max_len);
  286. if (r) {
  287. DMERR("couldn't open metadata space map");
  288. goto bad2;
  289. }
  290. }
  291. return 0;
  292. bad2:
  293. dm_tm_unlock(*tm, *sblock);
  294. bad1:
  295. dm_tm_destroy(*tm);
  296. return r;
  297. }
  298. int dm_tm_create_with_sm(struct dm_block_manager *bm, dm_block_t sb_location,
  299. struct dm_block_validator *sb_validator,
  300. struct dm_transaction_manager **tm,
  301. struct dm_space_map **sm, struct dm_block **sblock)
  302. {
  303. return dm_tm_create_internal(bm, sb_location, sb_validator,
  304. 0, 0, tm, sm, sblock, 1);
  305. }
  306. EXPORT_SYMBOL_GPL(dm_tm_create_with_sm);
  307. int dm_tm_open_with_sm(struct dm_block_manager *bm, dm_block_t sb_location,
  308. struct dm_block_validator *sb_validator,
  309. size_t root_offset, size_t root_max_len,
  310. struct dm_transaction_manager **tm,
  311. struct dm_space_map **sm, struct dm_block **sblock)
  312. {
  313. return dm_tm_create_internal(bm, sb_location, sb_validator, root_offset,
  314. root_max_len, tm, sm, sblock, 0);
  315. }
  316. EXPORT_SYMBOL_GPL(dm_tm_open_with_sm);
  317. /*----------------------------------------------------------------*/