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 <linux/refcount.h>
  21. #include "btrfs_inode.h"
  22. #include "delayed-ref.h"
  23. #include "ctree.h"
  24. enum btrfs_trans_state {
  25. TRANS_STATE_RUNNING = 0,
  26. TRANS_STATE_BLOCKED = 1,
  27. TRANS_STATE_COMMIT_START = 2,
  28. TRANS_STATE_COMMIT_DOING = 3,
  29. TRANS_STATE_UNBLOCKED = 4,
  30. TRANS_STATE_COMPLETED = 5,
  31. TRANS_STATE_MAX = 6,
  32. };
  33. #define BTRFS_TRANS_HAVE_FREE_BGS 0
  34. #define BTRFS_TRANS_DIRTY_BG_RUN 1
  35. #define BTRFS_TRANS_CACHE_ENOSPC 2
  36. struct btrfs_transaction {
  37. u64 transid;
  38. /*
  39. * total external writers(USERSPACE/START/ATTACH) in this
  40. * transaction, it must be zero before the transaction is
  41. * being committed
  42. */
  43. atomic_t num_extwriters;
  44. /*
  45. * total writers in this transaction, it must be zero before the
  46. * transaction can end
  47. */
  48. atomic_t num_writers;
  49. refcount_t use_count;
  50. atomic_t pending_ordered;
  51. unsigned long flags;
  52. /* Be protected by fs_info->trans_lock when we want to change it. */
  53. enum btrfs_trans_state state;
  54. struct list_head list;
  55. struct extent_io_tree dirty_pages;
  56. unsigned long start_time;
  57. wait_queue_head_t writer_wait;
  58. wait_queue_head_t commit_wait;
  59. wait_queue_head_t pending_wait;
  60. struct list_head pending_snapshots;
  61. struct list_head pending_chunks;
  62. struct list_head switch_commits;
  63. struct list_head dirty_bgs;
  64. struct list_head io_bgs;
  65. struct list_head dropped_roots;
  66. u64 num_dirty_bgs;
  67. /*
  68. * we need to make sure block group deletion doesn't race with
  69. * free space cache writeout. This mutex keeps them from stomping
  70. * on each other
  71. */
  72. struct mutex cache_write_mutex;
  73. spinlock_t dirty_bgs_lock;
  74. /* Protected by spin lock fs_info->unused_bgs_lock. */
  75. struct list_head deleted_bgs;
  76. spinlock_t dropped_roots_lock;
  77. struct btrfs_delayed_ref_root delayed_refs;
  78. int aborted;
  79. struct btrfs_fs_info *fs_info;
  80. };
  81. #define __TRANS_FREEZABLE (1U << 0)
  82. #define __TRANS_USERSPACE (1U << 8)
  83. #define __TRANS_START (1U << 9)
  84. #define __TRANS_ATTACH (1U << 10)
  85. #define __TRANS_JOIN (1U << 11)
  86. #define __TRANS_JOIN_NOLOCK (1U << 12)
  87. #define __TRANS_DUMMY (1U << 13)
  88. #define TRANS_USERSPACE (__TRANS_USERSPACE | __TRANS_FREEZABLE)
  89. #define TRANS_START (__TRANS_START | __TRANS_FREEZABLE)
  90. #define TRANS_ATTACH (__TRANS_ATTACH)
  91. #define TRANS_JOIN (__TRANS_JOIN | __TRANS_FREEZABLE)
  92. #define TRANS_JOIN_NOLOCK (__TRANS_JOIN_NOLOCK)
  93. #define TRANS_EXTWRITERS (__TRANS_USERSPACE | __TRANS_START | \
  94. __TRANS_ATTACH)
  95. #define BTRFS_SEND_TRANS_STUB ((void *)1)
  96. struct btrfs_trans_handle {
  97. u64 transid;
  98. u64 bytes_reserved;
  99. u64 chunk_bytes_reserved;
  100. unsigned long use_count;
  101. unsigned long blocks_reserved;
  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 can_flush_pending_bgs;
  110. bool reloc_reserved;
  111. bool sync;
  112. bool dirty;
  113. unsigned int type;
  114. struct btrfs_root *root;
  115. struct btrfs_fs_info *fs_info;
  116. struct list_head new_bgs;
  117. };
  118. struct btrfs_pending_snapshot {
  119. struct dentry *dentry;
  120. struct inode *dir;
  121. struct btrfs_root *root;
  122. struct btrfs_root_item *root_item;
  123. struct btrfs_root *snap;
  124. struct btrfs_qgroup_inherit *inherit;
  125. struct btrfs_path *path;
  126. /* block reservation for the operation */
  127. struct btrfs_block_rsv block_rsv;
  128. u64 qgroup_reserved;
  129. /* extra metadata reservation for relocation */
  130. int error;
  131. bool readonly;
  132. struct list_head list;
  133. };
  134. static inline void btrfs_set_inode_last_trans(struct btrfs_trans_handle *trans,
  135. struct inode *inode)
  136. {
  137. spin_lock(&BTRFS_I(inode)->lock);
  138. BTRFS_I(inode)->last_trans = trans->transaction->transid;
  139. BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
  140. BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
  141. spin_unlock(&BTRFS_I(inode)->lock);
  142. }
  143. /*
  144. * Make qgroup codes to skip given qgroupid, means the old/new_roots for
  145. * qgroup won't contain the qgroupid in it.
  146. */
  147. static inline void btrfs_set_skip_qgroup(struct btrfs_trans_handle *trans,
  148. u64 qgroupid)
  149. {
  150. struct btrfs_delayed_ref_root *delayed_refs;
  151. delayed_refs = &trans->transaction->delayed_refs;
  152. WARN_ON(delayed_refs->qgroup_to_skip);
  153. delayed_refs->qgroup_to_skip = qgroupid;
  154. }
  155. static inline void btrfs_clear_skip_qgroup(struct btrfs_trans_handle *trans)
  156. {
  157. struct btrfs_delayed_ref_root *delayed_refs;
  158. delayed_refs = &trans->transaction->delayed_refs;
  159. WARN_ON(!delayed_refs->qgroup_to_skip);
  160. delayed_refs->qgroup_to_skip = 0;
  161. }
  162. int btrfs_end_transaction(struct btrfs_trans_handle *trans);
  163. struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
  164. unsigned int num_items);
  165. struct btrfs_trans_handle *btrfs_start_transaction_fallback_global_rsv(
  166. struct btrfs_root *root,
  167. unsigned int num_items,
  168. int min_factor);
  169. struct btrfs_trans_handle *btrfs_start_transaction_lflush(
  170. struct btrfs_root *root,
  171. unsigned int num_items);
  172. struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root);
  173. struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root);
  174. struct btrfs_trans_handle *btrfs_attach_transaction(struct btrfs_root *root);
  175. struct btrfs_trans_handle *btrfs_attach_transaction_barrier(
  176. struct btrfs_root *root);
  177. struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *root);
  178. int btrfs_wait_for_commit(struct btrfs_fs_info *fs_info, u64 transid);
  179. void btrfs_add_dead_root(struct btrfs_root *root);
  180. int btrfs_defrag_root(struct btrfs_root *root);
  181. int btrfs_clean_one_deleted_snapshot(struct btrfs_root *root);
  182. int btrfs_commit_transaction(struct btrfs_trans_handle *trans);
  183. int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans,
  184. int wait_for_unblock);
  185. int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans);
  186. int btrfs_should_end_transaction(struct btrfs_trans_handle *trans);
  187. void btrfs_throttle(struct btrfs_fs_info *fs_info);
  188. int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans,
  189. struct btrfs_root *root);
  190. int btrfs_write_marked_extents(struct btrfs_fs_info *fs_info,
  191. struct extent_io_tree *dirty_pages, int mark);
  192. int btrfs_wait_extents(struct btrfs_fs_info *fs_info,
  193. struct extent_io_tree *dirty_pages);
  194. int btrfs_wait_tree_log_extents(struct btrfs_root *root, int mark);
  195. int btrfs_transaction_blocked(struct btrfs_fs_info *info);
  196. int btrfs_transaction_in_commit(struct btrfs_fs_info *info);
  197. void btrfs_put_transaction(struct btrfs_transaction *transaction);
  198. void btrfs_apply_pending_changes(struct btrfs_fs_info *fs_info);
  199. void btrfs_add_dropped_root(struct btrfs_trans_handle *trans,
  200. struct btrfs_root *root);
  201. #endif