xref: /linux/fs/btrfs/transaction.h (revision d6c5cf6c5834de8835500b0bbfb65b293427b127)
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/build_bug.h>
11 #include <linux/refcount.h>
12 #include <linux/list.h>
13 #include <linux/time64.h>
14 #include <linux/mutex.h>
15 #include <linux/wait.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 struct extent_buffer;
27 
28 /*
29  * Signal that a direct IO write is in progress, to avoid deadlock for sync
30  * direct IO writes when fsync is called during the direct IO write path.
31  */
32 #define BTRFS_TRANS_DIO_WRITE_STUB	((void *) 1)
33 
34 /* Radix-tree tag for roots that are part of the transaction. */
35 #define BTRFS_ROOT_TRANS_TAG			0
36 
37 enum btrfs_trans_state {
38 	TRANS_STATE_RUNNING,
39 	TRANS_STATE_COMMIT_PREP,
40 	TRANS_STATE_COMMIT_START,
41 	TRANS_STATE_COMMIT_DOING,
42 	TRANS_STATE_UNBLOCKED,
43 	TRANS_STATE_SUPER_COMMITTED,
44 	TRANS_STATE_COMPLETED,
45 	TRANS_STATE_MAX,
46 };
47 
48 #define BTRFS_TRANS_HAVE_FREE_BGS	0
49 #define BTRFS_TRANS_DIRTY_BG_RUN	1
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 	struct list_head dropped_roots;
81 	struct extent_io_tree pinned_extents;
82 
83 	/*
84 	 * We need to make sure block group deletion doesn't race with the
85 	 * dirty block group item updates done outside the commit critical
86 	 * section. This mutex keeps them from stomping on each other.
87 	 */
88 	struct mutex dirty_bgs_update_mutex;
89 	spinlock_t dirty_bgs_lock;
90 	/* Protected by spin lock fs_info->unused_bgs_lock. */
91 	struct list_head deleted_bgs;
92 	spinlock_t dropped_roots_lock;
93 	struct btrfs_delayed_ref_root delayed_refs;
94 	struct btrfs_fs_info *fs_info;
95 
96 	/*
97 	 * Number of ordered extents the transaction must wait for before
98 	 * committing. These are ordered extents started by a fast fsync.
99 	 */
100 	atomic_t pending_ordered;
101 	wait_queue_head_t pending_wait;
102 };
103 
104 /*
105  * Number of extent buffers a transaction handle tracks for writeback
106  * inhibition. The CLOCK reference bits pack into a u32 so this must not exceed
107  * 32, and keeping it a power of two lets the compiler reduce the CLOCK hand
108  * modulo to a mask.
109  */
110 #define BTRFS_INHIBITED_EBS_SLOTS				8
111 
112 static_assert(BTRFS_INHIBITED_EBS_SLOTS <= 32);
113 static_assert(BTRFS_INHIBITED_EBS_SLOTS != 0 &&
114 	      (BTRFS_INHIBITED_EBS_SLOTS & (BTRFS_INHIBITED_EBS_SLOTS - 1)) == 0);
115 
116 struct btrfs_trans_handle {
117 	u64 transid;
118 	u64 bytes_reserved;
119 	u64 delayed_refs_bytes_reserved;
120 	u64 chunk_bytes_reserved;
121 	unsigned long delayed_ref_updates;
122 	unsigned long delayed_ref_csum_deletions;
123 	struct btrfs_transaction *transaction;
124 	struct btrfs_block_rsv *block_rsv;
125 	struct btrfs_block_rsv *orig_rsv;
126 	/* Set by a task that wants to create a snapshot. */
127 	struct btrfs_pending_snapshot *pending_snapshot;
128 	refcount_t use_count;
129 	unsigned int type;
130 	/*
131 	 * Error code of transaction abort, set outside of locks and must use
132 	 * the READ_ONCE/WRITE_ONCE access
133 	 */
134 	short aborted;
135 	bool adding_csums;
136 	bool allocating_chunk;
137 	bool removing_chunk;
138 	bool reloc_reserved;
139 	bool in_fsync;
140 	struct btrfs_fs_info *fs_info;
141 	struct list_head new_bgs;
142 	struct btrfs_block_rsv delayed_rsv;
143 
144 	/* Extent buffers this handle has inhibited writeback on. */
145 	struct extent_buffer *inhibited_ebs[BTRFS_INHIBITED_EBS_SLOTS];
146 	/* CLOCK reference bit per slot. */
147 	u32 inhibited_ebs_referenced;
148 	u32 nr_inhibited_ebs;
149 	/* CLOCK hand. */
150 	u32 inhibited_ebs_hand;
151 };
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 
161 struct btrfs_pending_snapshot {
162 	struct dentry *dentry;
163 	struct btrfs_inode *dir;
164 	struct btrfs_root *root;
165 	struct btrfs_root_item *root_item;
166 	struct btrfs_root *snap;
167 	struct btrfs_qgroup_inherit *inherit;
168 	struct btrfs_path *path;
169 	/* block reservation for the operation */
170 	struct btrfs_block_rsv block_rsv;
171 	/* extra metadata reservation for relocation */
172 	int error;
173 	/* Preallocated anonymous block device number */
174 	dev_t anon_dev;
175 	bool readonly;
176 	struct list_head list;
177 };
178 
179 static inline void btrfs_set_inode_last_trans(struct btrfs_trans_handle *trans,
180 					      struct btrfs_inode *inode)
181 {
182 	spin_lock(&inode->lock);
183 	inode->last_trans = trans->transaction->transid;
184 	inode->last_sub_trans = btrfs_get_root_log_transid(inode->root);
185 	inode->last_log_commit = inode->last_sub_trans - 1;
186 	spin_unlock(&inode->lock);
187 }
188 
189 /*
190  * Make qgroup codes to skip given qgroupid, means the old/new_roots for
191  * qgroup won't contain the qgroupid in it.
192  */
193 static inline void btrfs_set_skip_qgroup(struct btrfs_trans_handle *trans,
194 					 u64 qgroupid)
195 {
196 	struct btrfs_delayed_ref_root *delayed_refs;
197 
198 	delayed_refs = &trans->transaction->delayed_refs;
199 	WARN_ON(delayed_refs->qgroup_to_skip);
200 	delayed_refs->qgroup_to_skip = qgroupid;
201 }
202 
203 static inline void btrfs_clear_skip_qgroup(struct btrfs_trans_handle *trans)
204 {
205 	struct btrfs_delayed_ref_root *delayed_refs;
206 
207 	delayed_refs = &trans->transaction->delayed_refs;
208 	WARN_ON(!delayed_refs->qgroup_to_skip);
209 	delayed_refs->qgroup_to_skip = 0;
210 }
211 
212 /*
213  * We want the transaction abort to print stack trace only for errors where the
214  * cause could be a bug, eg. due to ENOSPC, and not for common errors that are
215  * caused by external factors.
216  */
217 static inline bool btrfs_abort_should_print_stack(int error)
218 {
219 	switch (error) {
220 	case -EIO:
221 	case -EROFS:
222 	case -ENOMEM:
223 		return false;
224 	}
225 	return true;
226 }
227 
228 /*
229  * Compile-time and run-time verification of error passed to transaction abort.
230  * Direct constants will be caught at compile time, errors read from variables
231  * can be caught only at run-time and will warn under debugging config.
232  *
233  * How verification works:
234  * - accepted builtin constants are all -EIO and such
235  * - for compile-time check, invalid condition produces a negative-sized array
236  *   type, valid zero-sized
237  * - when a variable is passed as error the first check is a no-op
238  * - with enabled debugging, the second array type size is constructed from the
239  *   real variable value, valid condition produces array of size 1
240  * - sizeof(type) does not generate any code
241  */
242 #define VERIFY_NEGATIVE_ERROR(error)						\
243 do {										\
244 	(void)sizeof(char[-!(__builtin_constant_p(error) ? (error) < 0 : 1)]);	\
245 	if (IS_ENABLED(CONFIG_BTRFS_DEBUG)) {					\
246 		if (sizeof(char[(error) < 0]) != 1)				\
247 			DEBUG_WARN("error >= 0 passed to btrfs_abort_transaction()"); \
248 	}									\
249 } while(0)
250 
251 /*
252  * Call btrfs_abort_transaction() as early as possible when an error condition
253  * is detected, that way the exact stack trace is reported for some errors.
254  *
255  * Error number must be negative as it encodes whether it's the first abort.
256  */
257 #define btrfs_abort_transaction(trans, error)		\
258 do {								\
259 	int __error = (error);					\
260 								\
261 	VERIFY_NEGATIVE_ERROR(error);				\
262 	/* Report first abort since mount */			\
263 	if (!test_and_set_bit(BTRFS_FS_STATE_TRANS_ABORTED,	\
264 			&((trans)->fs_info->fs_state))) {	\
265 		WARN_ON(btrfs_abort_should_print_stack(__error)); \
266 		__error = -__error;				\
267 	}							\
268 	__btrfs_abort_transaction((trans), __func__,		\
269 				  __LINE__, __error);		\
270 } while (0)
271 
272 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
273 bool btrfs_is_dummy_transaction(const struct btrfs_trans_handle *trans);
274 void btrfs_init_dummy_trans(struct btrfs_trans_handle *trans,
275 			    struct btrfs_fs_info *fs_info);
276 #else
277 static inline bool btrfs_is_dummy_transaction(const struct btrfs_trans_handle *trans)
278 {
279 	return false;
280 }
281 #endif
282 
283 int btrfs_end_transaction(struct btrfs_trans_handle *trans);
284 struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
285 						   unsigned int num_items);
286 struct btrfs_trans_handle *btrfs_start_transaction_fallback_global_rsv(
287 					struct btrfs_root *root,
288 					unsigned int num_items);
289 struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root);
290 struct btrfs_trans_handle *btrfs_join_transaction_nostart(struct btrfs_root *root);
291 struct btrfs_trans_handle *btrfs_attach_transaction(struct btrfs_root *root);
292 struct btrfs_trans_handle *btrfs_attach_transaction_barrier(
293 					struct btrfs_root *root);
294 int btrfs_wait_for_commit(struct btrfs_fs_info *fs_info, u64 transid);
295 
296 void btrfs_add_dead_root(struct btrfs_root *root);
297 void btrfs_maybe_wake_unfinished_drop(struct btrfs_fs_info *fs_info);
298 int btrfs_clean_one_deleted_snapshot(struct btrfs_fs_info *fs_info);
299 int btrfs_commit_transaction(struct btrfs_trans_handle *trans);
300 void btrfs_commit_transaction_async(struct btrfs_trans_handle *trans);
301 int btrfs_commit_current_transaction(struct btrfs_root *root);
302 int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans);
303 bool btrfs_should_end_transaction(struct btrfs_trans_handle *trans);
304 void btrfs_throttle(struct btrfs_fs_info *fs_info);
305 int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans,
306 				struct btrfs_root *root);
307 int btrfs_write_marked_extents(struct btrfs_fs_info *fs_info,
308 				struct extent_io_tree *dirty_pages, int mark);
309 int btrfs_wait_tree_log_extents(struct btrfs_root *root, int mark);
310 int btrfs_transaction_blocked(struct btrfs_fs_info *info);
311 void btrfs_put_transaction(struct btrfs_transaction *transaction);
312 void btrfs_add_dropped_root(struct btrfs_trans_handle *trans,
313 			    struct btrfs_root *root);
314 void btrfs_trans_release_chunk_metadata(struct btrfs_trans_handle *trans);
315 void __cold __btrfs_abort_transaction(struct btrfs_trans_handle *trans,
316 				      const char *function,
317 				      unsigned int line, int error);
318 
319 int __init btrfs_transaction_init(void);
320 void __cold btrfs_transaction_exit(void);
321 
322 #endif
323