dm-transaction-manager.c 9.4 KB

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