xref: /linux/fs/btrfs/transaction.h (revision 93aac90b2d5bce5697d43d7b664102abe3f649c1)
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 #define BTRFS_TRANS_CACHE_ENOSPC	2
51 
52 struct btrfs_transaction {
53 	u64 transid;
54 	/*
55 	 * total external writers(USERSPACE/START/ATTACH) in this
56 	 * transaction, it must be zero before the transaction is
57 	 * being committed
58 	 */
59 	atomic_t num_extwriters;
60 	/*
61 	 * total writers in this transaction, it must be zero before the
62 	 * transaction can end
63 	 */
64 	atomic_t num_writers;
65 	refcount_t use_count;
66 
67 	unsigned long flags;
68 
69 	/* Be protected by fs_info->trans_lock when we want to change it. */
70 	enum btrfs_trans_state state;
71 	int aborted;
72 	struct list_head list;
73 	struct extent_io_tree dirty_pages;
74 	time64_t start_time;
75 	wait_queue_head_t writer_wait;
76 	wait_queue_head_t commit_wait;
77 	struct list_head pending_snapshots;
78 	struct list_head dev_update_list;
79 	struct list_head switch_commits;
80 	struct list_head dirty_bgs;
81 
82 	/*
83 	 * There is no explicit lock which protects io_bgs, rather its
84 	 * consistency is implied by the fact that all the sites which modify
85 	 * it do so under some form of transaction critical section, namely:
86 	 *
87 	 * - btrfs_start_dirty_block_groups - This function can only ever be
88 	 *   run by one of the transaction committers. Refer to
89 	 *   BTRFS_TRANS_DIRTY_BG_RUN usage in btrfs_commit_transaction
90 	 *
91 	 * - btrfs_write_dirty_blockgroups - this is called by
92 	 *   commit_cowonly_roots from transaction critical section
93 	 *   (TRANS_STATE_COMMIT_DOING)
94 	 *
95 	 * - btrfs_cleanup_dirty_bgs - called on transaction abort
96 	 */
97 	struct list_head io_bgs;
98 	struct list_head dropped_roots;
99 	struct extent_io_tree pinned_extents;
100 
101 	/*
102 	 * we need to make sure block group deletion doesn't race with
103 	 * free space cache writeout.  This mutex keeps them from stomping
104 	 * on each other
105 	 */
106 	struct mutex cache_write_mutex;
107 	spinlock_t dirty_bgs_lock;
108 	/* Protected by spin lock fs_info->unused_bgs_lock. */
109 	struct list_head deleted_bgs;
110 	spinlock_t dropped_roots_lock;
111 	struct btrfs_delayed_ref_root delayed_refs;
112 	struct btrfs_fs_info *fs_info;
113 
114 	/*
115 	 * Number of ordered extents the transaction must wait for before
116 	 * committing. These are ordered extents started by a fast fsync.
117 	 */
118 	atomic_t pending_ordered;
119 	wait_queue_head_t pending_wait;
120 };
121 
122 /*
123  * Number of extent buffers a transaction handle tracks for writeback
124  * inhibition. The CLOCK reference bits pack into a u32 so this must not exceed
125  * 32, and keeping it a power of two lets the compiler reduce the CLOCK hand
126  * modulo to a mask.
127  */
128 #define BTRFS_INHIBITED_EBS_SLOTS				8
129 
130 static_assert(BTRFS_INHIBITED_EBS_SLOTS <= 32);
131 static_assert(BTRFS_INHIBITED_EBS_SLOTS != 0 &&
132 	      (BTRFS_INHIBITED_EBS_SLOTS & (BTRFS_INHIBITED_EBS_SLOTS - 1)) == 0);
133 
134 struct btrfs_trans_handle {
135 	u64 transid;
136 	u64 bytes_reserved;
137 	u64 delayed_refs_bytes_reserved;
138 	u64 chunk_bytes_reserved;
139 	unsigned long delayed_ref_updates;
140 	unsigned long delayed_ref_csum_deletions;
141 	struct btrfs_transaction *transaction;
142 	struct btrfs_block_rsv *block_rsv;
143 	struct btrfs_block_rsv *orig_rsv;
144 	/* Set by a task that wants to create a snapshot. */
145 	struct btrfs_pending_snapshot *pending_snapshot;
146 	refcount_t use_count;
147 	unsigned int type;
148 	/*
149 	 * Error code of transaction abort, set outside of locks and must use
150 	 * the READ_ONCE/WRITE_ONCE access
151 	 */
152 	short aborted;
153 	bool adding_csums;
154 	bool allocating_chunk;
155 	bool removing_chunk;
156 	bool reloc_reserved;
157 	bool in_fsync;
158 	struct btrfs_fs_info *fs_info;
159 	struct list_head new_bgs;
160 	struct btrfs_block_rsv delayed_rsv;
161 
162 	/* Extent buffers this handle has inhibited writeback on. */
163 	struct extent_buffer *inhibited_ebs[BTRFS_INHIBITED_EBS_SLOTS];
164 	/* CLOCK reference bit per slot. */
165 	u32 inhibited_ebs_referenced;
166 	u32 nr_inhibited_ebs;
167 	/* CLOCK hand. */
168 	u32 inhibited_ebs_hand;
169 };
170 
171 /*
172  * The abort status can be changed between calls and is not protected by locks.
173  * This accepts btrfs_transaction and btrfs_trans_handle as types. Once it's
174  * set to a non-zero value it does not change, so the macro should be in checks
175  * but is not necessary for further reads of the value.
176  */
177 #define TRANS_ABORTED(trans)		(unlikely(READ_ONCE((trans)->aborted)))
178 
179 struct btrfs_pending_snapshot {
180 	struct dentry *dentry;
181 	struct btrfs_inode *dir;
182 	struct btrfs_root *root;
183 	struct btrfs_root_item *root_item;
184 	struct btrfs_root *snap;
185 	struct btrfs_qgroup_inherit *inherit;
186 	struct btrfs_path *path;
187 	/* block reservation for the operation */
188 	struct btrfs_block_rsv block_rsv;
189 	/* extra metadata reservation for relocation */
190 	int error;
191 	/* Preallocated anonymous block device number */
192 	dev_t anon_dev;
193 	bool readonly;
194 	struct list_head list;
195 };
196 
197 static inline void btrfs_set_inode_last_trans(struct btrfs_trans_handle *trans,
198 					      struct btrfs_inode *inode)
199 {
200 	spin_lock(&inode->lock);
201 	inode->last_trans = trans->transaction->transid;
202 	inode->last_sub_trans = btrfs_get_root_log_transid(inode->root);
203 	inode->last_log_commit = inode->last_sub_trans - 1;
204 	spin_unlock(&inode->lock);
205 }
206 
207 /*
208  * Make qgroup codes to skip given qgroupid, means the old/new_roots for
209  * qgroup won't contain the qgroupid in it.
210  */
211 static inline void btrfs_set_skip_qgroup(struct btrfs_trans_handle *trans,
212 					 u64 qgroupid)
213 {
214 	struct btrfs_delayed_ref_root *delayed_refs;
215 
216 	delayed_refs = &trans->transaction->delayed_refs;
217 	WARN_ON(delayed_refs->qgroup_to_skip);
218 	delayed_refs->qgroup_to_skip = qgroupid;
219 }
220 
221 static inline void btrfs_clear_skip_qgroup(struct btrfs_trans_handle *trans)
222 {
223 	struct btrfs_delayed_ref_root *delayed_refs;
224 
225 	delayed_refs = &trans->transaction->delayed_refs;
226 	WARN_ON(!delayed_refs->qgroup_to_skip);
227 	delayed_refs->qgroup_to_skip = 0;
228 }
229 
230 /*
231  * We want the transaction abort to print stack trace only for errors where the
232  * cause could be a bug, eg. due to ENOSPC, and not for common errors that are
233  * caused by external factors.
234  */
235 static inline bool btrfs_abort_should_print_stack(int error)
236 {
237 	switch (error) {
238 	case -EIO:
239 	case -EROFS:
240 	case -ENOMEM:
241 		return false;
242 	}
243 	return true;
244 }
245 
246 /*
247  * Compile-time and run-time verification of error passed to transaction abort.
248  * Direct constants will be caught at compile time, errors read from variables
249  * can be caught only at run-time and will warn under debugging config.
250  *
251  * How verification works:
252  * - accepted builtin constants are all -EIO and such
253  * - for compile-time check, invalid condition produces a negative-sized array
254  *   type, valid zero-sized
255  * - when a variable is passed as error the first check is a no-op
256  * - with enabled debugging, the second array type size is constructed from the
257  *   real variable value, valid condition produces array of size 1
258  * - sizeof(type) does not generate any code
259  */
260 #define VERIFY_NEGATIVE_ERROR(error)						\
261 do {										\
262 	(void)sizeof(char[-!(__builtin_constant_p(error) ? (error) < 0 : 1)]);	\
263 	if (IS_ENABLED(CONFIG_BTRFS_DEBUG)) {					\
264 		if (sizeof(char[(error) < 0]) != 1)				\
265 			DEBUG_WARN("error >= 0 passed to btrfs_abort_transaction()"); \
266 	}									\
267 } while(0)
268 
269 /*
270  * Call btrfs_abort_transaction() as early as possible when an error condition
271  * is detected, that way the exact stack trace is reported for some errors.
272  *
273  * Error number must be negative as it encodes whether it's the first abort.
274  */
275 #define btrfs_abort_transaction(trans, error)		\
276 do {								\
277 	int __error = (error);					\
278 								\
279 	VERIFY_NEGATIVE_ERROR(error);				\
280 	/* Report first abort since mount */			\
281 	if (!test_and_set_bit(BTRFS_FS_STATE_TRANS_ABORTED,	\
282 			&((trans)->fs_info->fs_state))) {	\
283 		WARN_ON(btrfs_abort_should_print_stack(__error)); \
284 		__error = -__error;				\
285 	}							\
286 	__btrfs_abort_transaction((trans), __func__,		\
287 				  __LINE__, __error);		\
288 } while (0)
289 
290 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
291 bool btrfs_is_dummy_transaction(const struct btrfs_trans_handle *trans);
292 void btrfs_init_dummy_trans(struct btrfs_trans_handle *trans,
293 			    struct btrfs_fs_info *fs_info);
294 #else
295 static inline bool btrfs_is_dummy_transaction(const struct btrfs_trans_handle *trans)
296 {
297 	return false;
298 }
299 #endif
300 
301 int btrfs_end_transaction(struct btrfs_trans_handle *trans);
302 struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
303 						   unsigned int num_items);
304 struct btrfs_trans_handle *btrfs_start_transaction_fallback_global_rsv(
305 					struct btrfs_root *root,
306 					unsigned int num_items);
307 struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root);
308 struct btrfs_trans_handle *btrfs_join_transaction_spacecache(struct btrfs_root *root);
309 struct btrfs_trans_handle *btrfs_join_transaction_nostart(struct btrfs_root *root);
310 struct btrfs_trans_handle *btrfs_attach_transaction(struct btrfs_root *root);
311 struct btrfs_trans_handle *btrfs_attach_transaction_barrier(
312 					struct btrfs_root *root);
313 int btrfs_wait_for_commit(struct btrfs_fs_info *fs_info, u64 transid);
314 
315 void btrfs_add_dead_root(struct btrfs_root *root);
316 void btrfs_maybe_wake_unfinished_drop(struct btrfs_fs_info *fs_info);
317 int btrfs_clean_one_deleted_snapshot(struct btrfs_fs_info *fs_info);
318 int btrfs_commit_transaction(struct btrfs_trans_handle *trans);
319 void btrfs_commit_transaction_async(struct btrfs_trans_handle *trans);
320 int btrfs_commit_current_transaction(struct btrfs_root *root);
321 int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans);
322 bool btrfs_should_end_transaction(struct btrfs_trans_handle *trans);
323 void btrfs_throttle(struct btrfs_fs_info *fs_info);
324 int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans,
325 				struct btrfs_root *root);
326 int btrfs_write_marked_extents(struct btrfs_fs_info *fs_info,
327 				struct extent_io_tree *dirty_pages, int mark);
328 int btrfs_wait_tree_log_extents(struct btrfs_root *root, int mark);
329 int btrfs_transaction_blocked(struct btrfs_fs_info *info);
330 void btrfs_put_transaction(struct btrfs_transaction *transaction);
331 void btrfs_add_dropped_root(struct btrfs_trans_handle *trans,
332 			    struct btrfs_root *root);
333 void btrfs_trans_release_chunk_metadata(struct btrfs_trans_handle *trans);
334 void __cold __btrfs_abort_transaction(struct btrfs_trans_handle *trans,
335 				      const char *function,
336 				      unsigned int line, int error);
337 
338 int __init btrfs_transaction_init(void);
339 void __cold btrfs_transaction_exit(void);
340 
341 #endif
342