transaction.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (C) 2007 Oracle. All rights reserved.
  4. */
  5. #ifndef BTRFS_TRANSACTION_H
  6. #define BTRFS_TRANSACTION_H
  7. #include <linux/refcount.h>
  8. #include "btrfs_inode.h"
  9. #include "delayed-ref.h"
  10. #include "ctree.h"
  11. enum btrfs_trans_state {
  12. TRANS_STATE_RUNNING,
  13. TRANS_STATE_COMMIT_START,
  14. TRANS_STATE_COMMIT_DOING,
  15. TRANS_STATE_UNBLOCKED,
  16. TRANS_STATE_COMPLETED,
  17. TRANS_STATE_MAX,
  18. };
  19. #define BTRFS_TRANS_HAVE_FREE_BGS 0
  20. #define BTRFS_TRANS_DIRTY_BG_RUN 1
  21. #define BTRFS_TRANS_CACHE_ENOSPC 2
  22. struct btrfs_transaction {
  23. u64 transid;
  24. /*
  25. * total external writers(USERSPACE/START/ATTACH) in this
  26. * transaction, it must be zero before the transaction is
  27. * being committed
  28. */
  29. atomic_t num_extwriters;
  30. /*
  31. * total writers in this transaction, it must be zero before the
  32. * transaction can end
  33. */
  34. atomic_t num_writers;
  35. refcount_t use_count;
  36. unsigned long flags;
  37. /* Be protected by fs_info->trans_lock when we want to change it. */
  38. enum btrfs_trans_state state;
  39. int aborted;
  40. struct list_head list;
  41. struct extent_io_tree dirty_pages;
  42. time64_t start_time;
  43. wait_queue_head_t writer_wait;
  44. wait_queue_head_t commit_wait;
  45. struct list_head pending_snapshots;
  46. struct list_head dev_update_list;
  47. struct list_head switch_commits;
  48. struct list_head dirty_bgs;
  49. /*
  50. * There is no explicit lock which protects io_bgs, rather its
  51. * consistency is implied by the fact that all the sites which modify
  52. * it do so under some form of transaction critical section, namely:
  53. *
  54. * - btrfs_start_dirty_block_groups - This function can only ever be
  55. * run by one of the transaction committers. Refer to
  56. * BTRFS_TRANS_DIRTY_BG_RUN usage in btrfs_commit_transaction
  57. *
  58. * - btrfs_write_dirty_blockgroups - this is called by
  59. * commit_cowonly_roots from transaction critical section
  60. * (TRANS_STATE_COMMIT_DOING)
  61. *
  62. * - btrfs_cleanup_dirty_bgs - called on transaction abort
  63. */
  64. struct list_head io_bgs;
  65. struct list_head dropped_roots;
  66. struct extent_io_tree pinned_extents;
  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. struct btrfs_fs_info *fs_info;
  79. /*
  80. * Number of ordered extents the transaction must wait for before
  81. * committing. These are ordered extents started by a fast fsync.
  82. */
  83. atomic_t pending_ordered;
  84. wait_queue_head_t pending_wait;
  85. };
  86. #define __TRANS_FREEZABLE (1U << 0)
  87. #define __TRANS_START (1U << 9)
  88. #define __TRANS_ATTACH (1U << 10)
  89. #define __TRANS_JOIN (1U << 11)
  90. #define __TRANS_JOIN_NOLOCK (1U << 12)
  91. #define __TRANS_DUMMY (1U << 13)
  92. #define __TRANS_JOIN_NOSTART (1U << 14)
  93. #define TRANS_START (__TRANS_START | __TRANS_FREEZABLE)
  94. #define TRANS_ATTACH (__TRANS_ATTACH)
  95. #define TRANS_JOIN (__TRANS_JOIN | __TRANS_FREEZABLE)
  96. #define TRANS_JOIN_NOLOCK (__TRANS_JOIN_NOLOCK)
  97. #define TRANS_JOIN_NOSTART (__TRANS_JOIN_NOSTART)
  98. #define TRANS_EXTWRITERS (__TRANS_START | __TRANS_ATTACH)
  99. #define BTRFS_SEND_TRANS_STUB ((void *)1)
  100. #define BTRFS_DIO_SYNC_STUB ((void *)2)
  101. struct btrfs_trans_handle {
  102. u64 transid;
  103. u64 bytes_reserved;
  104. u64 chunk_bytes_reserved;
  105. unsigned long delayed_ref_updates;
  106. struct btrfs_transaction *transaction;
  107. struct btrfs_block_rsv *block_rsv;
  108. struct btrfs_block_rsv *orig_rsv;
  109. refcount_t use_count;
  110. unsigned int type;
  111. /*
  112. * Error code of transaction abort, set outside of locks and must use
  113. * the READ_ONCE/WRITE_ONCE access
  114. */
  115. short aborted;
  116. bool adding_csums;
  117. bool allocating_chunk;
  118. bool can_flush_pending_bgs;
  119. bool reloc_reserved;
  120. bool dirty;
  121. struct btrfs_root *root;
  122. struct btrfs_fs_info *fs_info;
  123. struct list_head new_bgs;
  124. };
  125. /*
  126. * The abort status can be changed between calls and is not protected by locks.
  127. * This accepts btrfs_transaction and btrfs_trans_handle as types. Once it's
  128. * set to a non-zero value it does not change, so the macro should be in checks
  129. * but is not necessary for further reads of the value.
  130. */
  131. #define TRANS_ABORTED(trans) (unlikely(READ_ONCE((trans)->aborted)))
  132. struct btrfs_pending_snapshot {
  133. struct dentry *dentry;
  134. struct inode *dir;
  135. struct btrfs_root *root;
  136. struct btrfs_root_item *root_item;
  137. struct btrfs_root *snap;
  138. struct btrfs_qgroup_inherit *inherit;
  139. struct btrfs_path *path;
  140. /* block reservation for the operation */
  141. struct btrfs_block_rsv block_rsv;
  142. /* extra metadata reservation for relocation */
  143. int error;
  144. /* Preallocated anonymous block device number */
  145. dev_t anon_dev;
  146. bool readonly;
  147. struct list_head list;
  148. };
  149. static inline void btrfs_set_inode_last_trans(struct btrfs_trans_handle *trans,
  150. struct btrfs_inode *inode)
  151. {
  152. spin_lock(&inode->lock);
  153. inode->last_trans = trans->transaction->transid;
  154. inode->last_sub_trans = inode->root->log_transid;
  155. inode->last_log_commit = inode->last_sub_trans - 1;
  156. spin_unlock(&inode->lock);
  157. }
  158. /*
  159. * Make qgroup codes to skip given qgroupid, means the old/new_roots for
  160. * qgroup won't contain the qgroupid in it.
  161. */
  162. static inline void btrfs_set_skip_qgroup(struct btrfs_trans_handle *trans,
  163. u64 qgroupid)
  164. {
  165. struct btrfs_delayed_ref_root *delayed_refs;
  166. delayed_refs = &trans->transaction->delayed_refs;
  167. WARN_ON(delayed_refs->qgroup_to_skip);
  168. delayed_refs->qgroup_to_skip = qgroupid;
  169. }
  170. static inline void btrfs_clear_skip_qgroup(struct btrfs_trans_handle *trans)
  171. {
  172. struct btrfs_delayed_ref_root *delayed_refs;
  173. delayed_refs = &trans->transaction->delayed_refs;
  174. WARN_ON(!delayed_refs->qgroup_to_skip);
  175. delayed_refs->qgroup_to_skip = 0;
  176. }
  177. int btrfs_end_transaction(struct btrfs_trans_handle *trans);
  178. struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
  179. unsigned int num_items);
  180. struct btrfs_trans_handle *btrfs_start_transaction_fallback_global_rsv(
  181. struct btrfs_root *root,
  182. unsigned int num_items);
  183. struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root);
  184. struct btrfs_trans_handle *btrfs_join_transaction_spacecache(struct btrfs_root *root);
  185. struct btrfs_trans_handle *btrfs_join_transaction_nostart(struct btrfs_root *root);
  186. struct btrfs_trans_handle *btrfs_attach_transaction(struct btrfs_root *root);
  187. struct btrfs_trans_handle *btrfs_attach_transaction_barrier(
  188. struct btrfs_root *root);
  189. int btrfs_wait_for_commit(struct btrfs_fs_info *fs_info, u64 transid);
  190. void btrfs_add_dead_root(struct btrfs_root *root);
  191. int btrfs_defrag_root(struct btrfs_root *root);
  192. int btrfs_clean_one_deleted_snapshot(struct btrfs_root *root);
  193. int btrfs_commit_transaction(struct btrfs_trans_handle *trans);
  194. int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans,
  195. int wait_for_unblock);
  196. int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans);
  197. int btrfs_should_end_transaction(struct btrfs_trans_handle *trans);
  198. void btrfs_throttle(struct btrfs_fs_info *fs_info);
  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_fs_info *fs_info,
  202. struct extent_io_tree *dirty_pages, int mark);
  203. int btrfs_wait_tree_log_extents(struct btrfs_root *root, int mark);
  204. int btrfs_transaction_blocked(struct btrfs_fs_info *info);
  205. int btrfs_transaction_in_commit(struct btrfs_fs_info *info);
  206. void btrfs_put_transaction(struct btrfs_transaction *transaction);
  207. void btrfs_apply_pending_changes(struct btrfs_fs_info *fs_info);
  208. void btrfs_add_dropped_root(struct btrfs_trans_handle *trans,
  209. struct btrfs_root *root);
  210. void btrfs_trans_release_chunk_metadata(struct btrfs_trans_handle *trans);
  211. #endif