dm-transaction-manager.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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) In the first phase the block manager is told to start flushing, and
  34. * the changes to the space map are written to disk. You should interrogate
  35. * your particular space map to get detail of its root node etc. to be
  36. * included in your superblock.
  37. *
  38. * ii) @root will be committed last. You shouldn't use more than the
  39. * first 512 bytes of @root if you wish the transaction to survive a power
  40. * failure. You *must* have a write lock held on @root for both stage (i)
  41. * and (ii). The commit will drop the write 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 *root);
  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. 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. 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. struct dm_block_validator *v,
  84. struct dm_block **result);
  85. int 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_dec(struct dm_transaction_manager *tm, dm_block_t b);
  91. int dm_tm_ref(struct dm_transaction_manager *tm, dm_block_t b,
  92. uint32_t *result);
  93. struct dm_block_manager *dm_tm_get_bm(struct dm_transaction_manager *tm);
  94. /*
  95. * A little utility that ties the knot by producing a transaction manager
  96. * that has a space map managed by the transaction manager...
  97. *
  98. * Returns a tm that has an open transaction to write the new disk sm.
  99. * Caller should store the new sm root and commit.
  100. */
  101. int dm_tm_create_with_sm(struct dm_block_manager *bm, dm_block_t sb_location,
  102. struct dm_block_validator *sb_validator,
  103. struct dm_transaction_manager **tm,
  104. struct dm_space_map **sm, struct dm_block **sblock);
  105. int dm_tm_open_with_sm(struct dm_block_manager *bm, dm_block_t sb_location,
  106. struct dm_block_validator *sb_validator,
  107. size_t root_offset, size_t root_max_len,
  108. struct dm_transaction_manager **tm,
  109. struct dm_space_map **sm, struct dm_block **sblock);
  110. #endif /* _LINUX_DM_TRANSACTION_MANAGER_H */