transaction.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * Copyright (C) 2007 Oracle. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public
  6. * License v2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public
  14. * License along with this program; if not, write to the
  15. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  16. * Boston, MA 021110-1307, USA.
  17. */
  18. #ifndef __BTRFS_TRANSACTION__
  19. #define __BTRFS_TRANSACTION__
  20. #include "btrfs_inode.h"
  21. #include "delayed-ref.h"
  22. #include "ctree.h"
  23. enum btrfs_trans_state {
  24. TRANS_STATE_RUNNING = 0,
  25. TRANS_STATE_BLOCKED = 1,
  26. TRANS_STATE_COMMIT_START = 2,
  27. TRANS_STATE_COMMIT_DOING = 3,
  28. TRANS_STATE_UNBLOCKED = 4,
  29. TRANS_STATE_COMPLETED = 5,
  30. TRANS_STATE_MAX = 6,
  31. };
  32. struct btrfs_transaction {
  33. u64 transid;
  34. /*
  35. * total external writers(USERSPACE/START/ATTACH) in this
  36. * transaction, it must be zero before the transaction is
  37. * being committed
  38. */
  39. atomic_t num_extwriters;
  40. /*
  41. * total writers in this transaction, it must be zero before the
  42. * transaction can end
  43. */
  44. atomic_t num_writers;
  45. atomic_t use_count;
  46. /* Be protected by fs_info->trans_lock when we want to change it. */
  47. enum btrfs_trans_state state;
  48. struct list_head list;
  49. struct extent_io_tree dirty_pages;
  50. unsigned long start_time;
  51. wait_queue_head_t writer_wait;
  52. wait_queue_head_t commit_wait;
  53. struct list_head pending_snapshots;
  54. struct list_head ordered_operations;
  55. struct list_head pending_chunks;
  56. struct btrfs_delayed_ref_root delayed_refs;
  57. int aborted;
  58. };
  59. #define __TRANS_FREEZABLE (1U << 0)
  60. #define __TRANS_USERSPACE (1U << 8)
  61. #define __TRANS_START (1U << 9)
  62. #define __TRANS_ATTACH (1U << 10)
  63. #define __TRANS_JOIN (1U << 11)
  64. #define __TRANS_JOIN_NOLOCK (1U << 12)
  65. #define TRANS_USERSPACE (__TRANS_USERSPACE | __TRANS_FREEZABLE)
  66. #define TRANS_START (__TRANS_START | __TRANS_FREEZABLE)
  67. #define TRANS_ATTACH (__TRANS_ATTACH)
  68. #define TRANS_JOIN (__TRANS_JOIN | __TRANS_FREEZABLE)
  69. #define TRANS_JOIN_NOLOCK (__TRANS_JOIN_NOLOCK)
  70. #define TRANS_EXTWRITERS (__TRANS_USERSPACE | __TRANS_START | \
  71. __TRANS_ATTACH)
  72. struct btrfs_trans_handle {
  73. u64 transid;
  74. u64 bytes_reserved;
  75. u64 qgroup_reserved;
  76. unsigned long use_count;
  77. unsigned long blocks_reserved;
  78. unsigned long blocks_used;
  79. unsigned long delayed_ref_updates;
  80. struct btrfs_transaction *transaction;
  81. struct btrfs_block_rsv *block_rsv;
  82. struct btrfs_block_rsv *orig_rsv;
  83. short aborted;
  84. short adding_csums;
  85. bool allocating_chunk;
  86. unsigned int type;
  87. /*
  88. * this root is only needed to validate that the root passed to
  89. * start_transaction is the same as the one passed to end_transaction.
  90. * Subvolume quota depends on this
  91. */
  92. struct btrfs_root *root;
  93. struct seq_list delayed_ref_elem;
  94. struct list_head qgroup_ref_list;
  95. struct list_head new_bgs;
  96. };
  97. struct btrfs_pending_snapshot {
  98. struct dentry *dentry;
  99. struct inode *dir;
  100. struct btrfs_root *root;
  101. struct btrfs_root *snap;
  102. struct btrfs_qgroup_inherit *inherit;
  103. /* block reservation for the operation */
  104. struct btrfs_block_rsv block_rsv;
  105. u64 qgroup_reserved;
  106. /* extra metadata reseration for relocation */
  107. int error;
  108. bool readonly;
  109. struct list_head list;
  110. };
  111. static inline void btrfs_set_inode_last_trans(struct btrfs_trans_handle *trans,
  112. struct inode *inode)
  113. {
  114. BTRFS_I(inode)->last_trans = trans->transaction->transid;
  115. BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
  116. BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
  117. }
  118. int btrfs_end_transaction(struct btrfs_trans_handle *trans,
  119. struct btrfs_root *root);
  120. struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
  121. int num_items);
  122. struct btrfs_trans_handle *btrfs_start_transaction_lflush(
  123. struct btrfs_root *root, int num_items);
  124. struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root);
  125. struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root);
  126. struct btrfs_trans_handle *btrfs_attach_transaction(struct btrfs_root *root);
  127. struct btrfs_trans_handle *btrfs_attach_transaction_barrier(
  128. struct btrfs_root *root);
  129. struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *root);
  130. int btrfs_wait_for_commit(struct btrfs_root *root, u64 transid);
  131. int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
  132. struct btrfs_root *root);
  133. void btrfs_add_dead_root(struct btrfs_root *root);
  134. int btrfs_defrag_root(struct btrfs_root *root);
  135. int btrfs_clean_one_deleted_snapshot(struct btrfs_root *root);
  136. int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
  137. struct btrfs_root *root);
  138. int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans,
  139. struct btrfs_root *root,
  140. int wait_for_unblock);
  141. int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
  142. struct btrfs_root *root);
  143. int btrfs_end_transaction_dmeta(struct btrfs_trans_handle *trans,
  144. struct btrfs_root *root);
  145. int btrfs_should_end_transaction(struct btrfs_trans_handle *trans,
  146. struct btrfs_root *root);
  147. void btrfs_throttle(struct btrfs_root *root);
  148. int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans,
  149. struct btrfs_root *root);
  150. int btrfs_write_and_wait_marked_extents(struct btrfs_root *root,
  151. struct extent_io_tree *dirty_pages, int mark);
  152. int btrfs_write_marked_extents(struct btrfs_root *root,
  153. struct extent_io_tree *dirty_pages, int mark);
  154. int btrfs_wait_marked_extents(struct btrfs_root *root,
  155. struct extent_io_tree *dirty_pages, int mark);
  156. int btrfs_transaction_blocked(struct btrfs_fs_info *info);
  157. int btrfs_transaction_in_commit(struct btrfs_fs_info *info);
  158. #endif