dm-transaction-manager.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. #ifndef _LINUX_DM_TRANSACTION_MANAGER_H
  8. #define _LINUX_DM_TRANSACTION_MANAGER_H
  9. #include "dm-block-manager.h"
  10. struct dm_transaction_manager;
  11. struct dm_space_map;
  12. /*----------------------------------------------------------------*/
  13. /*
  14. * This manages the scope of a transaction. It also enforces immutability
  15. * of the on-disk data structures by limiting access to writeable blocks.
  16. *
  17. * Clients should not fiddle with the block manager directly.
  18. */
  19. void dm_tm_destroy(struct dm_transaction_manager *tm);
  20. /*
  21. * The non-blocking version of a transaction manager is intended for use in
  22. * fast path code that needs to do lookups e.g. a dm mapping function.
  23. * You create the non-blocking variant from a normal tm. The interface is
  24. * the same, except that most functions will just return -EWOULDBLOCK.
  25. * Methods that return void yet may block should not be called on a clone
  26. * viz. dm_tm_inc, dm_tm_dec. Call dm_tm_destroy() as you would with a normal
  27. * tm when you've finished with it. You may not destroy the original prior
  28. * to clones.
  29. */
  30. struct dm_transaction_manager *dm_tm_create_non_blocking_clone(struct dm_transaction_manager *real);
  31. /*
  32. * We use a 2-phase commit here.
  33. *
  34. * i) Make all changes for the transaction *except* for the superblock.
  35. * Then call dm_tm_pre_commit() to flush them to disk.
  36. *
  37. * ii) Lock your superblock. Update. Then call dm_tm_commit() which will
  38. * unlock the superblock and flush it. No other blocks should be updated
  39. * during this period. Care should be taken to never unlock a partially
  40. * updated superblock; perform any operations that could fail *before* you
  41. * take the superblock lock.
  42. */
  43. int dm_tm_pre_commit(struct dm_transaction_manager *tm);
  44. int dm_tm_commit(struct dm_transaction_manager *tm, struct dm_block *superblock);
  45. /*
  46. * These methods are the only way to get hold of a writeable block.
  47. */
  48. /*
  49. * dm_tm_new_block() is pretty self-explanatory. Make sure you do actually
  50. * write to the whole of @data before you unlock, otherwise you could get
  51. * a data leak. (The other option is for tm_new_block() to zero new blocks
  52. * before handing them out, which will be redundant in most, if not all,
  53. * cases).
  54. * Zeroes the new block and returns with write lock held.
  55. */
  56. int dm_tm_new_block(struct dm_transaction_manager *tm,
  57. const struct dm_block_validator *v,
  58. struct dm_block **result);
  59. /*
  60. * dm_tm_shadow_block() allocates a new block and copies the data from @orig
  61. * to it. It then decrements the reference count on original block. Use
  62. * this to update the contents of a block in a data structure, don't
  63. * confuse this with a clone - you shouldn't access the orig block after
  64. * this operation. Because the tm knows the scope of the transaction it
  65. * can optimise requests for a shadow of a shadow to a no-op. Don't forget
  66. * to unlock when you've finished with the shadow.
  67. *
  68. * The @inc_children flag is used to tell the caller whether it needs to
  69. * adjust reference counts for children. (Data in the block may refer to
  70. * other blocks.)
  71. *
  72. * Shadowing implicitly drops a reference on @orig so you must not have
  73. * it locked when you call this.
  74. */
  75. int dm_tm_shadow_block(struct dm_transaction_manager *tm, dm_block_t orig,
  76. const struct dm_block_validator *v,
  77. struct dm_block **result, int *inc_children);
  78. /*
  79. * Read access. You can lock any block you want. If there's a write lock
  80. * on it outstanding then it'll block.
  81. */
  82. int dm_tm_read_lock(struct dm_transaction_manager *tm, dm_block_t b,
  83. const struct dm_block_validator *v,
  84. struct dm_block **result);
  85. void dm_tm_unlock(struct dm_transaction_manager *tm, struct dm_block *b);
  86. /*
  87. * Functions for altering the reference count of a block directly.
  88. */
  89. void dm_tm_inc(struct dm_transaction_manager *tm, dm_block_t b);
  90. void dm_tm_inc_range(struct dm_transaction_manager *tm, dm_block_t b, dm_block_t e);
  91. void dm_tm_dec(struct dm_transaction_manager *tm, dm_block_t b);
  92. void dm_tm_dec_range(struct dm_transaction_manager *tm, dm_block_t b, dm_block_t e);
  93. /*
  94. * Builds up runs of adjacent blocks, and then calls the given fn
  95. * (typically dm_tm_inc/dec). Very useful when you have to perform
  96. * the same tm operation on all values in a btree leaf.
  97. */
  98. typedef void (*dm_tm_run_fn)(struct dm_transaction_manager *, dm_block_t, dm_block_t);
  99. void dm_tm_with_runs(struct dm_transaction_manager *tm,
  100. const __le64 *value_le, unsigned int count, dm_tm_run_fn fn);
  101. int dm_tm_ref(struct dm_transaction_manager *tm, dm_block_t b, uint32_t *result);
  102. /*
  103. * Finds out if a given block is shared (ie. has a reference count higher
  104. * than one).
  105. */
  106. int dm_tm_block_is_shared(struct dm_transaction_manager *tm, dm_block_t b,
  107. int *result);
  108. struct dm_block_manager *dm_tm_get_bm(struct dm_transaction_manager *tm);
  109. /*
  110. * If you're using a non-blocking clone the tm will build up a list of
  111. * requested blocks that weren't in core. This call will request those
  112. * blocks to be prefetched.
  113. */
  114. void dm_tm_issue_prefetches(struct dm_transaction_manager *tm);
  115. /*
  116. * A little utility that ties the knot by producing a transaction manager
  117. * that has a space map managed by the transaction manager...
  118. *
  119. * Returns a tm that has an open transaction to write the new disk sm.
  120. * Caller should store the new sm root and commit.
  121. *
  122. * The superblock location is passed so the metadata space map knows it
  123. * shouldn't be used.
  124. */
  125. int dm_tm_create_with_sm(struct dm_block_manager *bm, dm_block_t sb_location,
  126. struct dm_transaction_manager **tm,
  127. struct dm_space_map **sm);
  128. int dm_tm_open_with_sm(struct dm_block_manager *bm, dm_block_t sb_location,
  129. void *sm_root, size_t root_len,
  130. struct dm_transaction_manager **tm,
  131. struct dm_space_map **sm);
  132. #endif /* _LINUX_DM_TRANSACTION_MANAGER_H */