dm-transaction-manager.c 11 KB

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