transaction.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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/atomic.h>
  8. #include <linux/refcount.h>
  9. #include <linux/list.h>
  10. #include <linux/time64.h>
  11. #include <linux/mutex.h>
  12. #include <linux/wait.h>
  13. #include "btrfs_inode.h"
  14. #include "delayed-ref.h"
  15. #include "extent-io-tree.h"
  16. #include "block-rsv.h"
  17. #include "messages.h"
  18. #include "misc.h"
  19. struct dentry;
  20. struct inode;
  21. struct btrfs_pending_snapshot;
  22. struct btrfs_fs_info;
  23. struct btrfs_root_item;
  24. struct btrfs_root;
  25. struct btrfs_path;
  26. /*
  27. * Signal that a direct IO write is in progress, to avoid deadlock for sync
  28. * direct IO writes when fsync is called during the direct IO write path.
  29. */
  30. #define BTRFS_TRANS_DIO_WRITE_STUB ((void *) 1)
  31. /* Radix-tree tag for roots that are part of the trasaction. */
  32. #define BTRFS_ROOT_TRANS_TAG 0
  33. enum btrfs_trans_state {
  34. TRANS_STATE_RUNNING,
  35. TRANS_STATE_COMMIT_PREP,
  36. TRANS_STATE_COMMIT_START,
  37. TRANS_STATE_COMMIT_DOING,
  38. TRANS_STATE_UNBLOCKED,
  39. TRANS_STATE_SUPER_COMMITTED,
  40. TRANS_STATE_COMPLETED,
  41. TRANS_STATE_MAX,
  42. };
  43. #define BTRFS_TRANS_HAVE_FREE_BGS 0
  44. #define BTRFS_TRANS_DIRTY_BG_RUN 1
  45. #define BTRFS_TRANS_CACHE_ENOSPC 2
  46. struct btrfs_transaction {
  47. u64 transid;
  48. /*
  49. * total external writers(USERSPACE/START/ATTACH) in this
  50. * transaction, it must be zero before the transaction is
  51. * being committed
  52. */
  53. atomic_t num_extwriters;
  54. /*
  55. * total writers in this transaction, it must be zero before the
  56. * transaction can end
  57. */
  58. atomic_t num_writers;
  59. refcount_t use_count;
  60. unsigned long flags;
  61. /* Be protected by fs_info->trans_lock when we want to change it. */
  62. enum btrfs_trans_state state;
  63. int aborted;
  64. struct list_head list;
  65. struct extent_io_tree dirty_pages;
  66. time64_t start_time;
  67. wait_queue_head_t writer_wait;
  68. wait_queue_head_t commit_wait;
  69. struct list_head pending_snapshots;
  70. struct list_head dev_update_list;
  71. struct list_head switch_commits;
  72. struct list_head dirty_bgs;
  73. /*
  74. * There is no explicit lock which protects io_bgs, rather its
  75. * consistency is implied by the fact that all the sites which modify
  76. * it do so under some form of transaction critical section, namely:
  77. *
  78. * - btrfs_start_dirty_block_groups - This function can only ever be
  79. * run by one of the transaction committers. Refer to
  80. * BTRFS_TRANS_DIRTY_BG_RUN usage in btrfs_commit_transaction
  81. *
  82. * - btrfs_write_dirty_blockgroups - this is called by
  83. * commit_cowonly_roots from transaction critical section
  84. * (TRANS_STATE_COMMIT_DOING)
  85. *
  86. * - btrfs_cleanup_dirty_bgs - called on transaction abort
  87. */
  88. struct list_head io_bgs;
  89. struct list_head dropped_roots;
  90. struct extent_io_tree pinned_extents;
  91. /*
  92. * we need to make sure block group deletion doesn't race with
  93. * free space cache writeout. This mutex keeps them from stomping
  94. * on each other
  95. */
  96. struct mutex cache_write_mutex;
  97. spinlock_t dirty_bgs_lock;
  98. /* Protected by spin lock fs_info->unused_bgs_lock. */
  99. struct list_head deleted_bgs;
  100. spinlock_t dropped_roots_lock;
  101. struct btrfs_delayed_ref_root delayed_refs;
  102. struct btrfs_fs_info *fs_info;
  103. /*
  104. * Number of ordered extents the transaction must wait for before
  105. * committing. These are ordered extents started by a fast fsync.
  106. */
  107. atomic_t pending_ordered;
  108. wait_queue_head_t pending_wait;
  109. };
  110. enum {
  111. ENUM_BIT(__TRANS_FREEZABLE),
  112. ENUM_BIT(__TRANS_START),
  113. ENUM_BIT(__TRANS_ATTACH),
  114. ENUM_BIT(__TRANS_JOIN),
  115. ENUM_BIT(__TRANS_JOIN_NOLOCK),
  116. ENUM_BIT(__TRANS_DUMMY),
  117. ENUM_BIT(__TRANS_JOIN_NOSTART),
  118. };
  119. #define TRANS_START (__TRANS_START | __TRANS_FREEZABLE)
  120. #define TRANS_ATTACH (__TRANS_ATTACH)
  121. #define TRANS_JOIN (__TRANS_JOIN | __TRANS_FREEZABLE)
  122. #define TRANS_JOIN_NOLOCK (__TRANS_JOIN_NOLOCK)
  123. #define TRANS_JOIN_NOSTART (__TRANS_JOIN_NOSTART)
  124. #define TRANS_EXTWRITERS (__TRANS_START | __TRANS_ATTACH)
  125. struct btrfs_trans_handle {
  126. u64 transid;
  127. u64 bytes_reserved;
  128. u64 delayed_refs_bytes_reserved;
  129. u64 chunk_bytes_reserved;
  130. unsigned long delayed_ref_updates;
  131. unsigned long delayed_ref_csum_deletions;
  132. struct btrfs_transaction *transaction;
  133. struct btrfs_block_rsv *block_rsv;
  134. struct btrfs_block_rsv *orig_rsv;
  135. /* Set by a task that wants to create a snapshot. */
  136. struct btrfs_pending_snapshot *pending_snapshot;
  137. refcount_t use_count;
  138. unsigned int type;
  139. /*
  140. * Error code of transaction abort, set outside of locks and must use
  141. * the READ_ONCE/WRITE_ONCE access
  142. */
  143. short aborted;
  144. bool adding_csums;
  145. bool allocating_chunk;
  146. bool removing_chunk;
  147. bool reloc_reserved;
  148. bool in_fsync;
  149. struct btrfs_fs_info *fs_info;
  150. struct list_head new_bgs;
  151. struct btrfs_block_rsv delayed_rsv;
  152. };
  153. /*
  154. * The abort status can be changed between calls and is not protected by locks.
  155. * This accepts btrfs_transaction and btrfs_trans_handle as types. Once it's
  156. * set to a non-zero value it does not change, so the macro should be in checks
  157. * but is not necessary for further reads of the value.
  158. */
  159. #define TRANS_ABORTED(trans) (unlikely(READ_ONCE((trans)->aborted)))
  160. struct btrfs_pending_snapshot {
  161. struct dentry *dentry;
  162. struct btrfs_inode *dir;
  163. struct btrfs_root *root;
  164. struct btrfs_root_item *root_item;
  165. struct btrfs_root *snap;
  166. struct btrfs_qgroup_inherit *inherit;
  167. struct btrfs_path *path;
  168. /* block reservation for the operation */
  169. struct btrfs_block_rsv block_rsv;
  170. /* extra metadata reservation for relocation */
  171. int error;
  172. /* Preallocated anonymous block device number */
  173. dev_t anon_dev;
  174. bool readonly;
  175. struct list_head list;
  176. };
  177. static inline void btrfs_set_inode_last_trans(struct btrfs_trans_handle *trans,
  178. struct btrfs_inode *inode)
  179. {
  180. spin_lock(&inode->lock);
  181. inode->last_trans = trans->transaction->transid;
  182. inode->last_sub_trans = btrfs_get_root_log_transid(inode->root);
  183. inode->last_log_commit = inode->last_sub_trans - 1;
  184. spin_unlock(&inode->lock);
  185. }
  186. /*
  187. * Make qgroup codes to skip given qgroupid, means the old/new_roots for
  188. * qgroup won't contain the qgroupid in it.
  189. */
  190. static inline void btrfs_set_skip_qgroup(struct btrfs_trans_handle *trans,
  191. u64 qgroupid)
  192. {
  193. struct btrfs_delayed_ref_root *delayed_refs;
  194. delayed_refs = &trans->transaction->delayed_refs;
  195. WARN_ON(delayed_refs->qgroup_to_skip);
  196. delayed_refs->qgroup_to_skip = qgroupid;
  197. }
  198. static inline void btrfs_clear_skip_qgroup(struct btrfs_trans_handle *trans)
  199. {
  200. struct btrfs_delayed_ref_root *delayed_refs;
  201. delayed_refs = &trans->transaction->delayed_refs;
  202. WARN_ON(!delayed_refs->qgroup_to_skip);
  203. delayed_refs->qgroup_to_skip = 0;
  204. }
  205. bool __cold abort_should_print_stack(int error);
  206. /*
  207. * Call btrfs_abort_transaction as early as possible when an error condition is
  208. * detected, that way the exact stack trace is reported for some errors.
  209. */
  210. #define btrfs_abort_transaction(trans, error) \
  211. do { \
  212. bool __first = false; \
  213. /* Report first abort since mount */ \
  214. if (!test_and_set_bit(BTRFS_FS_STATE_TRANS_ABORTED, \
  215. &((trans)->fs_info->fs_state))) { \
  216. __first = true; \
  217. if (WARN(abort_should_print_stack(error), \
  218. KERN_ERR \
  219. "BTRFS: Transaction aborted (error %d)\n", \
  220. (error))) { \
  221. /* Stack trace printed. */ \
  222. } else { \
  223. btrfs_err((trans)->fs_info, \
  224. "Transaction aborted (error %d)", \
  225. (error)); \
  226. } \
  227. } \
  228. __btrfs_abort_transaction((trans), __func__, \
  229. __LINE__, (error), __first); \
  230. } while (0)
  231. int btrfs_end_transaction(struct btrfs_trans_handle *trans);
  232. struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
  233. unsigned int num_items);
  234. struct btrfs_trans_handle *btrfs_start_transaction_fallback_global_rsv(
  235. struct btrfs_root *root,
  236. unsigned int num_items);
  237. struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root);
  238. struct btrfs_trans_handle *btrfs_join_transaction_spacecache(struct btrfs_root *root);
  239. struct btrfs_trans_handle *btrfs_join_transaction_nostart(struct btrfs_root *root);
  240. struct btrfs_trans_handle *btrfs_attach_transaction(struct btrfs_root *root);
  241. struct btrfs_trans_handle *btrfs_attach_transaction_barrier(
  242. struct btrfs_root *root);
  243. int btrfs_wait_for_commit(struct btrfs_fs_info *fs_info, u64 transid);
  244. void btrfs_add_dead_root(struct btrfs_root *root);
  245. void btrfs_maybe_wake_unfinished_drop(struct btrfs_fs_info *fs_info);
  246. int btrfs_clean_one_deleted_snapshot(struct btrfs_fs_info *fs_info);
  247. int btrfs_commit_transaction(struct btrfs_trans_handle *trans);
  248. void btrfs_commit_transaction_async(struct btrfs_trans_handle *trans);
  249. int btrfs_commit_current_transaction(struct btrfs_root *root);
  250. int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans);
  251. bool btrfs_should_end_transaction(struct btrfs_trans_handle *trans);
  252. void btrfs_throttle(struct btrfs_fs_info *fs_info);
  253. int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans,
  254. struct btrfs_root *root);
  255. int btrfs_write_marked_extents(struct btrfs_fs_info *fs_info,
  256. struct extent_io_tree *dirty_pages, int mark);
  257. int btrfs_wait_tree_log_extents(struct btrfs_root *root, int mark);
  258. int btrfs_transaction_blocked(struct btrfs_fs_info *info);
  259. void btrfs_put_transaction(struct btrfs_transaction *transaction);
  260. void btrfs_add_dropped_root(struct btrfs_trans_handle *trans,
  261. struct btrfs_root *root);
  262. void btrfs_trans_release_chunk_metadata(struct btrfs_trans_handle *trans);
  263. void __cold __btrfs_abort_transaction(struct btrfs_trans_handle *trans,
  264. const char *function,
  265. unsigned int line, int error, bool first_hit);
  266. int __init btrfs_transaction_init(void);
  267. void __cold btrfs_transaction_exit(void);
  268. #endif