transaction.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. int aborted;
  55. struct list_head list;
  56. struct extent_io_tree dirty_pages;
  57. unsigned long start_time;
  58. wait_queue_head_t writer_wait;
  59. wait_queue_head_t commit_wait;
  60. wait_queue_head_t pending_wait;
  61. struct list_head pending_snapshots;
  62. struct list_head pending_chunks;
  63. struct list_head switch_commits;
  64. struct list_head dirty_bgs;
  65. struct list_head io_bgs;
  66. struct list_head dropped_roots;
  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. unsigned int num_dirty_bgs;
  75. /* Protected by spin lock fs_info->unused_bgs_lock. */
  76. struct list_head deleted_bgs;
  77. spinlock_t dropped_roots_lock;
  78. struct btrfs_delayed_ref_root delayed_refs;
  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 delayed_ref_updates;
  101. struct btrfs_transaction *transaction;
  102. struct btrfs_block_rsv *block_rsv;
  103. struct btrfs_block_rsv *orig_rsv;
  104. refcount_t use_count;
  105. unsigned int type;
  106. short aborted;
  107. bool adding_csums;
  108. bool allocating_chunk;
  109. bool can_flush_pending_bgs;
  110. bool reloc_reserved;
  111. bool sync;
  112. bool dirty;
  113. struct btrfs_root *root;
  114. struct btrfs_fs_info *fs_info;
  115. struct list_head new_bgs;
  116. };
  117. struct btrfs_pending_snapshot {
  118. struct dentry *dentry;
  119. struct inode *dir;
  120. struct btrfs_root *root;
  121. struct btrfs_root_item *root_item;
  122. struct btrfs_root *snap;
  123. struct btrfs_qgroup_inherit *inherit;
  124. struct btrfs_path *path;
  125. /* block reservation for the operation */
  126. struct btrfs_block_rsv block_rsv;
  127. u64 qgroup_reserved;
  128. /* extra metadata reservation for relocation */
  129. int error;
  130. bool readonly;
  131. struct list_head list;
  132. };
  133. static inline void btrfs_set_inode_last_trans(struct btrfs_trans_handle *trans,
  134. struct inode *inode)
  135. {
  136. spin_lock(&BTRFS_I(inode)->lock);
  137. BTRFS_I(inode)->last_trans = trans->transaction->transid;
  138. BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
  139. BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
  140. spin_unlock(&BTRFS_I(inode)->lock);
  141. }
  142. /*
  143. * Make qgroup codes to skip given qgroupid, means the old/new_roots for
  144. * qgroup won't contain the qgroupid in it.
  145. */
  146. static inline void btrfs_set_skip_qgroup(struct btrfs_trans_handle *trans,
  147. u64 qgroupid)
  148. {
  149. struct btrfs_delayed_ref_root *delayed_refs;
  150. delayed_refs = &trans->transaction->delayed_refs;
  151. WARN_ON(delayed_refs->qgroup_to_skip);
  152. delayed_refs->qgroup_to_skip = qgroupid;
  153. }
  154. static inline void btrfs_clear_skip_qgroup(struct btrfs_trans_handle *trans)
  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 = 0;
  160. }
  161. int btrfs_end_transaction(struct btrfs_trans_handle *trans);
  162. struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
  163. unsigned int num_items);
  164. struct btrfs_trans_handle *btrfs_start_transaction_fallback_global_rsv(
  165. struct btrfs_root *root,
  166. unsigned int num_items,
  167. int min_factor);
  168. struct btrfs_trans_handle *btrfs_start_transaction_lflush(
  169. struct btrfs_root *root,
  170. unsigned 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_fs_info *fs_info, 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. int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans,
  183. int wait_for_unblock);
  184. int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans);
  185. int btrfs_should_end_transaction(struct btrfs_trans_handle *trans);
  186. void btrfs_throttle(struct btrfs_fs_info *fs_info);
  187. int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans,
  188. struct btrfs_root *root);
  189. int btrfs_write_marked_extents(struct btrfs_fs_info *fs_info,
  190. struct extent_io_tree *dirty_pages, int mark);
  191. int btrfs_wait_extents(struct btrfs_fs_info *fs_info,
  192. struct extent_io_tree *dirty_pages);
  193. int btrfs_wait_tree_log_extents(struct btrfs_root *root, int mark);
  194. int btrfs_transaction_blocked(struct btrfs_fs_info *info);
  195. int btrfs_transaction_in_commit(struct btrfs_fs_info *info);
  196. void btrfs_put_transaction(struct btrfs_transaction *transaction);
  197. void btrfs_apply_pending_changes(struct btrfs_fs_info *fs_info);
  198. void btrfs_add_dropped_root(struct btrfs_trans_handle *trans,
  199. struct btrfs_root *root);
  200. #endif