transaction.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. #define BTRFS_TRANS_HAVE_FREE_BGS 0
  33. #define BTRFS_TRANS_DIRTY_BG_RUN 1
  34. #define BTRFS_TRANS_CACHE_ENOSPC 2
  35. struct btrfs_transaction {
  36. u64 transid;
  37. /*
  38. * total external writers(USERSPACE/START/ATTACH) in this
  39. * transaction, it must be zero before the transaction is
  40. * being committed
  41. */
  42. atomic_t num_extwriters;
  43. /*
  44. * total writers in this transaction, it must be zero before the
  45. * transaction can end
  46. */
  47. atomic_t num_writers;
  48. atomic_t use_count;
  49. atomic_t pending_ordered;
  50. unsigned long flags;
  51. /* Be protected by fs_info->trans_lock when we want to change it. */
  52. enum btrfs_trans_state state;
  53. struct list_head list;
  54. struct extent_io_tree dirty_pages;
  55. unsigned long start_time;
  56. wait_queue_head_t writer_wait;
  57. wait_queue_head_t commit_wait;
  58. wait_queue_head_t pending_wait;
  59. struct list_head pending_snapshots;
  60. struct list_head pending_chunks;
  61. struct list_head switch_commits;
  62. struct list_head dirty_bgs;
  63. struct list_head io_bgs;
  64. struct list_head dropped_roots;
  65. u64 num_dirty_bgs;
  66. /*
  67. * we need to make sure block group deletion doesn't race with
  68. * free space cache writeout. This mutex keeps them from stomping
  69. * on each other
  70. */
  71. struct mutex cache_write_mutex;
  72. spinlock_t dirty_bgs_lock;
  73. /* Protected by spin lock fs_info->unused_bgs_lock. */
  74. struct list_head deleted_bgs;
  75. spinlock_t dropped_roots_lock;
  76. struct btrfs_delayed_ref_root delayed_refs;
  77. int aborted;
  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. unsigned long use_count;
  99. unsigned long blocks_reserved;
  100. unsigned long delayed_ref_updates;
  101. struct btrfs_transaction *transaction;
  102. struct btrfs_block_rsv *block_rsv;
  103. struct btrfs_block_rsv *orig_rsv;
  104. short aborted;
  105. short adding_csums;
  106. bool allocating_chunk;
  107. bool can_flush_pending_bgs;
  108. bool reloc_reserved;
  109. bool sync;
  110. bool dirty;
  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 qgroup_ref_list;
  120. struct list_head new_bgs;
  121. };
  122. struct btrfs_pending_snapshot {
  123. struct dentry *dentry;
  124. struct inode *dir;
  125. struct btrfs_root *root;
  126. struct btrfs_root *snap;
  127. struct btrfs_qgroup_inherit *inherit;
  128. /* block reservation for the operation */
  129. struct btrfs_block_rsv block_rsv;
  130. u64 qgroup_reserved;
  131. /* extra metadata reseration for relocation */
  132. int error;
  133. bool readonly;
  134. struct list_head list;
  135. };
  136. static inline void btrfs_set_inode_last_trans(struct btrfs_trans_handle *trans,
  137. struct inode *inode)
  138. {
  139. spin_lock(&BTRFS_I(inode)->lock);
  140. BTRFS_I(inode)->last_trans = trans->transaction->transid;
  141. BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
  142. BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
  143. spin_unlock(&BTRFS_I(inode)->lock);
  144. }
  145. /*
  146. * Make qgroup codes to skip given qgroupid, means the old/new_roots for
  147. * qgroup won't contain the qgroupid in it.
  148. */
  149. static inline void btrfs_set_skip_qgroup(struct btrfs_trans_handle *trans,
  150. u64 qgroupid)
  151. {
  152. struct btrfs_delayed_ref_root *delayed_refs;
  153. delayed_refs = &trans->transaction->delayed_refs;
  154. WARN_ON(delayed_refs->qgroup_to_skip);
  155. delayed_refs->qgroup_to_skip = qgroupid;
  156. }
  157. static inline void btrfs_clear_skip_qgroup(struct btrfs_trans_handle *trans)
  158. {
  159. struct btrfs_delayed_ref_root *delayed_refs;
  160. delayed_refs = &trans->transaction->delayed_refs;
  161. WARN_ON(!delayed_refs->qgroup_to_skip);
  162. delayed_refs->qgroup_to_skip = 0;
  163. }
  164. int btrfs_end_transaction(struct btrfs_trans_handle *trans,
  165. struct btrfs_root *root);
  166. struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
  167. unsigned int num_items);
  168. struct btrfs_trans_handle *btrfs_start_transaction_fallback_global_rsv(
  169. struct btrfs_root *root,
  170. unsigned int num_items,
  171. int min_factor);
  172. struct btrfs_trans_handle *btrfs_start_transaction_lflush(
  173. struct btrfs_root *root,
  174. unsigned int num_items);
  175. struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root);
  176. struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root);
  177. struct btrfs_trans_handle *btrfs_attach_transaction(struct btrfs_root *root);
  178. struct btrfs_trans_handle *btrfs_attach_transaction_barrier(
  179. struct btrfs_root *root);
  180. struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *root);
  181. int btrfs_wait_for_commit(struct btrfs_root *root, u64 transid);
  182. void btrfs_add_dead_root(struct btrfs_root *root);
  183. int btrfs_defrag_root(struct btrfs_root *root);
  184. int btrfs_clean_one_deleted_snapshot(struct btrfs_root *root);
  185. int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
  186. struct btrfs_root *root);
  187. int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans,
  188. struct btrfs_root *root,
  189. int wait_for_unblock);
  190. int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
  191. struct btrfs_root *root);
  192. int btrfs_should_end_transaction(struct btrfs_trans_handle *trans,
  193. struct btrfs_root *root);
  194. void btrfs_throttle(struct btrfs_root *root);
  195. int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans,
  196. struct btrfs_root *root);
  197. int btrfs_write_marked_extents(struct btrfs_root *root,
  198. struct extent_io_tree *dirty_pages, int mark);
  199. int btrfs_wait_marked_extents(struct btrfs_root *root,
  200. struct extent_io_tree *dirty_pages, int mark);
  201. int btrfs_transaction_blocked(struct btrfs_fs_info *info);
  202. int btrfs_transaction_in_commit(struct btrfs_fs_info *info);
  203. void btrfs_put_transaction(struct btrfs_transaction *transaction);
  204. void btrfs_apply_pending_changes(struct btrfs_fs_info *fs_info);
  205. void btrfs_add_dropped_root(struct btrfs_trans_handle *trans,
  206. struct btrfs_root *root);
  207. #endif