dm-transaction-manager.c 11 KB

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