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