dm-transaction-manager.h 5.0 KB

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