transaction.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. /*
  47. * true if there is free bgs operations in this transaction
  48. */
  49. int have_free_bgs;
  50. /* Be protected by fs_info->trans_lock when we want to change it. */
  51. enum btrfs_trans_state state;
  52. struct list_head list;
  53. struct extent_io_tree dirty_pages;
  54. unsigned long start_time;
  55. wait_queue_head_t writer_wait;
  56. wait_queue_head_t commit_wait;
  57. struct list_head pending_snapshots;
  58. struct list_head pending_chunks;
  59. struct list_head pending_ordered;
  60. struct list_head switch_commits;
  61. struct list_head dirty_bgs;
  62. struct list_head io_bgs;
  63. struct list_head dropped_roots;
  64. u64 num_dirty_bgs;
  65. /*
  66. * we need to make sure block group deletion doesn't race with
  67. * free space cache writeout. This mutex keeps them from stomping
  68. * on each other
  69. */
  70. struct mutex cache_write_mutex;
  71. spinlock_t dirty_bgs_lock;
  72. struct list_head deleted_bgs;
  73. spinlock_t deleted_bgs_lock;
  74. spinlock_t dropped_roots_lock;
  75. struct btrfs_delayed_ref_root delayed_refs;
  76. int aborted;
  77. int dirty_bg_run;
  78. };
  79. #define __TRANS_FREEZABLE (1U << 0)
  80. #define __TRANS_USERSPACE (1U << 8)
  81. #define __TRANS_START (1U << 9)
  82. #define __TRANS_ATTACH (1U << 10)
  83. #define __TRANS_JOIN (1U << 11)
  84. #define __TRANS_JOIN_NOLOCK (1U << 12)
  85. #define __TRANS_DUMMY (1U << 13)
  86. #define TRANS_USERSPACE (__TRANS_USERSPACE | __TRANS_FREEZABLE)
  87. #define TRANS_START (__TRANS_START | __TRANS_FREEZABLE)
  88. #define TRANS_ATTACH (__TRANS_ATTACH)
  89. #define TRANS_JOIN (__TRANS_JOIN | __TRANS_FREEZABLE)
  90. #define TRANS_JOIN_NOLOCK (__TRANS_JOIN_NOLOCK)
  91. #define TRANS_EXTWRITERS (__TRANS_USERSPACE | __TRANS_START | \
  92. __TRANS_ATTACH)
  93. #define BTRFS_SEND_TRANS_STUB ((void *)1)
  94. struct btrfs_trans_handle {
  95. u64 transid;
  96. u64 bytes_reserved;
  97. u64 chunk_bytes_reserved;
  98. u64 qgroup_reserved;
  99. unsigned long use_count;
  100. unsigned long blocks_reserved;
  101. unsigned long blocks_used;
  102. unsigned long delayed_ref_updates;
  103. struct btrfs_transaction *transaction;
  104. struct btrfs_block_rsv *block_rsv;
  105. struct btrfs_block_rsv *orig_rsv;
  106. short aborted;
  107. short adding_csums;
  108. bool allocating_chunk;
  109. bool reloc_reserved;
  110. bool sync;
  111. unsigned int type;
  112. /*
  113. * this root is only needed to validate that the root passed to
  114. * start_transaction is the same as the one passed to end_transaction.
  115. * Subvolume quota depends on this
  116. */
  117. struct btrfs_root *root;
  118. struct seq_list delayed_ref_elem;
  119. struct list_head ordered;
  120. struct list_head qgroup_ref_list;
  121. struct list_head new_bgs;
  122. };
  123. struct btrfs_pending_snapshot {
  124. struct dentry *dentry;
  125. struct inode *dir;
  126. struct btrfs_root *root;
  127. struct btrfs_root *snap;
  128. struct btrfs_qgroup_inherit *inherit;
  129. /* block reservation for the operation */
  130. struct btrfs_block_rsv block_rsv;
  131. u64 qgroup_reserved;
  132. /* extra metadata reseration for relocation */
  133. int error;
  134. bool readonly;
  135. struct list_head list;
  136. };
  137. static inline void btrfs_set_inode_last_trans(struct btrfs_trans_handle *trans,
  138. struct inode *inode)
  139. {
  140. spin_lock(&BTRFS_I(inode)->lock);
  141. BTRFS_I(inode)->last_trans = trans->transaction->transid;
  142. BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
  143. BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
  144. spin_unlock(&BTRFS_I(inode)->lock);
  145. }
  146. /*
  147. * Make qgroup codes to skip given qgroupid, means the old/new_roots for
  148. * qgroup won't contain the qgroupid in it.
  149. */
  150. static inline void btrfs_set_skip_qgroup(struct btrfs_trans_handle *trans,
  151. u64 qgroupid)
  152. {
  153. struct btrfs_delayed_ref_root *delayed_refs;
  154. delayed_refs = &trans->transaction->delayed_refs;
  155. WARN_ON(delayed_refs->qgroup_to_skip);
  156. delayed_refs->qgroup_to_skip = qgroupid;
  157. }
  158. static inline void btrfs_clear_skip_qgroup(struct btrfs_trans_handle *trans)
  159. {
  160. struct btrfs_delayed_ref_root *delayed_refs;
  161. delayed_refs = &trans->transaction->delayed_refs;
  162. WARN_ON(!delayed_refs->qgroup_to_skip);
  163. delayed_refs->qgroup_to_skip = 0;
  164. }
  165. int btrfs_end_transaction(struct btrfs_trans_handle *trans,
  166. struct btrfs_root *root);
  167. struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
  168. int num_items);
  169. struct btrfs_trans_handle *btrfs_start_transaction_lflush(
  170. struct btrfs_root *root, int num_items);
  171. struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root);
  172. struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root);
  173. struct btrfs_trans_handle *btrfs_attach_transaction(struct btrfs_root *root);
  174. struct btrfs_trans_handle *btrfs_attach_transaction_barrier(
  175. struct btrfs_root *root);
  176. struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *root);
  177. int btrfs_wait_for_commit(struct btrfs_root *root, u64 transid);
  178. void btrfs_add_dead_root(struct btrfs_root *root);
  179. int btrfs_defrag_root(struct btrfs_root *root);
  180. int btrfs_clean_one_deleted_snapshot(struct btrfs_root *root);
  181. int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
  182. struct btrfs_root *root);
  183. int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans,
  184. struct btrfs_root *root,
  185. int wait_for_unblock);
  186. int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
  187. struct btrfs_root *root);
  188. int btrfs_should_end_transaction(struct btrfs_trans_handle *trans,
  189. struct btrfs_root *root);
  190. void btrfs_throttle(struct btrfs_root *root);
  191. int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans,
  192. struct btrfs_root *root);
  193. int btrfs_write_marked_extents(struct btrfs_root *root,
  194. struct extent_io_tree *dirty_pages, int mark);
  195. int btrfs_wait_marked_extents(struct btrfs_root *root,
  196. struct extent_io_tree *dirty_pages, int mark);
  197. int btrfs_transaction_blocked(struct btrfs_fs_info *info);
  198. int btrfs_transaction_in_commit(struct btrfs_fs_info *info);
  199. void btrfs_put_transaction(struct btrfs_transaction *transaction);
  200. void btrfs_apply_pending_changes(struct btrfs_fs_info *fs_info);
  201. void btrfs_add_dropped_root(struct btrfs_trans_handle *trans,
  202. struct btrfs_root *root);
  203. #endif