transaction.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. struct btrfs_fs_info *fs_info;
  79. };
  80. #define __TRANS_FREEZABLE (1U << 0)
  81. #define __TRANS_USERSPACE (1U << 8)
  82. #define __TRANS_START (1U << 9)
  83. #define __TRANS_ATTACH (1U << 10)
  84. #define __TRANS_JOIN (1U << 11)
  85. #define __TRANS_JOIN_NOLOCK (1U << 12)
  86. #define __TRANS_DUMMY (1U << 13)
  87. #define TRANS_USERSPACE (__TRANS_USERSPACE | __TRANS_FREEZABLE)
  88. #define TRANS_START (__TRANS_START | __TRANS_FREEZABLE)
  89. #define TRANS_ATTACH (__TRANS_ATTACH)
  90. #define TRANS_JOIN (__TRANS_JOIN | __TRANS_FREEZABLE)
  91. #define TRANS_JOIN_NOLOCK (__TRANS_JOIN_NOLOCK)
  92. #define TRANS_EXTWRITERS (__TRANS_USERSPACE | __TRANS_START | \
  93. __TRANS_ATTACH)
  94. #define BTRFS_SEND_TRANS_STUB ((void *)1)
  95. struct btrfs_trans_handle {
  96. u64 transid;
  97. u64 bytes_reserved;
  98. u64 chunk_bytes_reserved;
  99. unsigned long use_count;
  100. unsigned long blocks_reserved;
  101. unsigned long delayed_ref_updates;
  102. struct btrfs_transaction *transaction;
  103. struct btrfs_block_rsv *block_rsv;
  104. struct btrfs_block_rsv *orig_rsv;
  105. short aborted;
  106. short adding_csums;
  107. bool allocating_chunk;
  108. bool can_flush_pending_bgs;
  109. bool reloc_reserved;
  110. bool sync;
  111. bool dirty;
  112. unsigned int type;
  113. /*
  114. * this root is only needed to validate that the root passed to
  115. * start_transaction is the same as the one passed to end_transaction.
  116. * Subvolume quota depends on this
  117. */
  118. struct btrfs_root *root;
  119. struct btrfs_fs_info *fs_info;
  120. struct seq_list delayed_ref_elem;
  121. struct list_head qgroup_ref_list;
  122. struct list_head new_bgs;
  123. };
  124. struct btrfs_pending_snapshot {
  125. struct dentry *dentry;
  126. struct inode *dir;
  127. struct btrfs_root *root;
  128. struct btrfs_root_item *root_item;
  129. struct btrfs_root *snap;
  130. struct btrfs_qgroup_inherit *inherit;
  131. struct btrfs_path *path;
  132. /* block reservation for the operation */
  133. struct btrfs_block_rsv block_rsv;
  134. u64 qgroup_reserved;
  135. /* extra metadata reservation for relocation */
  136. int error;
  137. bool readonly;
  138. struct list_head list;
  139. };
  140. static inline void btrfs_set_inode_last_trans(struct btrfs_trans_handle *trans,
  141. struct inode *inode)
  142. {
  143. spin_lock(&BTRFS_I(inode)->lock);
  144. BTRFS_I(inode)->last_trans = trans->transaction->transid;
  145. BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
  146. BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
  147. spin_unlock(&BTRFS_I(inode)->lock);
  148. }
  149. /*
  150. * Make qgroup codes to skip given qgroupid, means the old/new_roots for
  151. * qgroup won't contain the qgroupid in it.
  152. */
  153. static inline void btrfs_set_skip_qgroup(struct btrfs_trans_handle *trans,
  154. u64 qgroupid)
  155. {
  156. struct btrfs_delayed_ref_root *delayed_refs;
  157. delayed_refs = &trans->transaction->delayed_refs;
  158. WARN_ON(delayed_refs->qgroup_to_skip);
  159. delayed_refs->qgroup_to_skip = qgroupid;
  160. }
  161. static inline void btrfs_clear_skip_qgroup(struct btrfs_trans_handle *trans)
  162. {
  163. struct btrfs_delayed_ref_root *delayed_refs;
  164. delayed_refs = &trans->transaction->delayed_refs;
  165. WARN_ON(!delayed_refs->qgroup_to_skip);
  166. delayed_refs->qgroup_to_skip = 0;
  167. }
  168. int btrfs_end_transaction(struct btrfs_trans_handle *trans,
  169. struct btrfs_root *root);
  170. struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
  171. unsigned int num_items);
  172. struct btrfs_trans_handle *btrfs_start_transaction_fallback_global_rsv(
  173. struct btrfs_root *root,
  174. unsigned int num_items,
  175. int min_factor);
  176. struct btrfs_trans_handle *btrfs_start_transaction_lflush(
  177. struct btrfs_root *root,
  178. unsigned int num_items);
  179. struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root);
  180. struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root);
  181. struct btrfs_trans_handle *btrfs_attach_transaction(struct btrfs_root *root);
  182. struct btrfs_trans_handle *btrfs_attach_transaction_barrier(
  183. struct btrfs_root *root);
  184. struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *root);
  185. int btrfs_wait_for_commit(struct btrfs_root *root, u64 transid);
  186. void btrfs_add_dead_root(struct btrfs_root *root);
  187. int btrfs_defrag_root(struct btrfs_root *root);
  188. int btrfs_clean_one_deleted_snapshot(struct btrfs_root *root);
  189. int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
  190. struct btrfs_root *root);
  191. int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans,
  192. struct btrfs_root *root,
  193. int wait_for_unblock);
  194. int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
  195. struct btrfs_root *root);
  196. int btrfs_should_end_transaction(struct btrfs_trans_handle *trans,
  197. struct btrfs_root *root);
  198. void btrfs_throttle(struct btrfs_root *root);
  199. int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans,
  200. struct btrfs_root *root);
  201. int btrfs_write_marked_extents(struct btrfs_root *root,
  202. struct extent_io_tree *dirty_pages, int mark);
  203. int btrfs_wait_marked_extents(struct btrfs_root *root,
  204. struct extent_io_tree *dirty_pages, int mark);
  205. int btrfs_transaction_blocked(struct btrfs_fs_info *info);
  206. int btrfs_transaction_in_commit(struct btrfs_fs_info *info);
  207. void btrfs_put_transaction(struct btrfs_transaction *transaction);
  208. void btrfs_apply_pending_changes(struct btrfs_fs_info *fs_info);
  209. void btrfs_add_dropped_root(struct btrfs_trans_handle *trans,
  210. struct btrfs_root *root);
  211. #endif