xref: /linux/fs/btrfs/transaction.h (revision 50c44fea13ec339d0d457079b254e8c8420d6511)
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 enum {
123 	ENUM_BIT(__TRANS_FREEZABLE),
124 	ENUM_BIT(__TRANS_START),
125 	ENUM_BIT(__TRANS_ATTACH),
126 	ENUM_BIT(__TRANS_JOIN),
127 	ENUM_BIT(__TRANS_JOIN_NOLOCK),
128 	ENUM_BIT(__TRANS_DUMMY),
129 	ENUM_BIT(__TRANS_JOIN_NOSTART),
130 };
131 
132 #define TRANS_START		(__TRANS_START | __TRANS_FREEZABLE)
133 #define TRANS_ATTACH		(__TRANS_ATTACH)
134 #define TRANS_JOIN		(__TRANS_JOIN | __TRANS_FREEZABLE)
135 #define TRANS_JOIN_NOLOCK	(__TRANS_JOIN_NOLOCK)
136 #define TRANS_JOIN_NOSTART	(__TRANS_JOIN_NOSTART)
137 
138 #define TRANS_EXTWRITERS	(__TRANS_START | __TRANS_ATTACH)
139 
140 /*
141  * Number of extent buffers a transaction handle tracks for writeback
142  * inhibition. The CLOCK reference bits pack into a u32 so this must not exceed
143  * 32, and keeping it a power of two lets the compiler reduce the CLOCK hand
144  * modulo to a mask.
145  */
146 #define BTRFS_INHIBITED_EBS_SLOTS				8
147 
148 static_assert(BTRFS_INHIBITED_EBS_SLOTS <= 32);
149 static_assert(BTRFS_INHIBITED_EBS_SLOTS != 0 &&
150 	      (BTRFS_INHIBITED_EBS_SLOTS & (BTRFS_INHIBITED_EBS_SLOTS - 1)) == 0);
151 
152 struct btrfs_trans_handle {
153 	u64 transid;
154 	u64 bytes_reserved;
155 	u64 delayed_refs_bytes_reserved;
156 	u64 chunk_bytes_reserved;
157 	unsigned long delayed_ref_updates;
158 	unsigned long delayed_ref_csum_deletions;
159 	struct btrfs_transaction *transaction;
160 	struct btrfs_block_rsv *block_rsv;
161 	struct btrfs_block_rsv *orig_rsv;
162 	/* Set by a task that wants to create a snapshot. */
163 	struct btrfs_pending_snapshot *pending_snapshot;
164 	refcount_t use_count;
165 	unsigned int type;
166 	/*
167 	 * Error code of transaction abort, set outside of locks and must use
168 	 * the READ_ONCE/WRITE_ONCE access
169 	 */
170 	short aborted;
171 	bool adding_csums;
172 	bool allocating_chunk;
173 	bool removing_chunk;
174 	bool reloc_reserved;
175 	bool in_fsync;
176 	struct btrfs_fs_info *fs_info;
177 	struct list_head new_bgs;
178 	struct btrfs_block_rsv delayed_rsv;
179 
180 	/* Extent buffers this handle has inhibited writeback on. */
181 	struct extent_buffer *inhibited_ebs[BTRFS_INHIBITED_EBS_SLOTS];
182 	/* CLOCK reference bit per slot. */
183 	u32 inhibited_ebs_referenced;
184 	u32 nr_inhibited_ebs;
185 	/* CLOCK hand. */
186 	u32 inhibited_ebs_hand;
187 };
188 
189 /*
190  * The abort status can be changed between calls and is not protected by locks.
191  * This accepts btrfs_transaction and btrfs_trans_handle as types. Once it's
192  * set to a non-zero value it does not change, so the macro should be in checks
193  * but is not necessary for further reads of the value.
194  */
195 #define TRANS_ABORTED(trans)		(unlikely(READ_ONCE((trans)->aborted)))
196 
197 struct btrfs_pending_snapshot {
198 	struct dentry *dentry;
199 	struct btrfs_inode *dir;
200 	struct btrfs_root *root;
201 	struct btrfs_root_item *root_item;
202 	struct btrfs_root *snap;
203 	struct btrfs_qgroup_inherit *inherit;
204 	struct btrfs_path *path;
205 	/* block reservation for the operation */
206 	struct btrfs_block_rsv block_rsv;
207 	/* extra metadata reservation for relocation */
208 	int error;
209 	/* Preallocated anonymous block device number */
210 	dev_t anon_dev;
211 	bool readonly;
212 	struct list_head list;
213 };
214 
btrfs_set_inode_last_trans(struct btrfs_trans_handle * trans,struct btrfs_inode * inode)215 static inline void btrfs_set_inode_last_trans(struct btrfs_trans_handle *trans,
216 					      struct btrfs_inode *inode)
217 {
218 	spin_lock(&inode->lock);
219 	inode->last_trans = trans->transaction->transid;
220 	inode->last_sub_trans = btrfs_get_root_log_transid(inode->root);
221 	inode->last_log_commit = inode->last_sub_trans - 1;
222 	spin_unlock(&inode->lock);
223 }
224 
225 /*
226  * Make qgroup codes to skip given qgroupid, means the old/new_roots for
227  * qgroup won't contain the qgroupid in it.
228  */
btrfs_set_skip_qgroup(struct btrfs_trans_handle * trans,u64 qgroupid)229 static inline void btrfs_set_skip_qgroup(struct btrfs_trans_handle *trans,
230 					 u64 qgroupid)
231 {
232 	struct btrfs_delayed_ref_root *delayed_refs;
233 
234 	delayed_refs = &trans->transaction->delayed_refs;
235 	WARN_ON(delayed_refs->qgroup_to_skip);
236 	delayed_refs->qgroup_to_skip = qgroupid;
237 }
238 
btrfs_clear_skip_qgroup(struct btrfs_trans_handle * trans)239 static inline void btrfs_clear_skip_qgroup(struct btrfs_trans_handle *trans)
240 {
241 	struct btrfs_delayed_ref_root *delayed_refs;
242 
243 	delayed_refs = &trans->transaction->delayed_refs;
244 	WARN_ON(!delayed_refs->qgroup_to_skip);
245 	delayed_refs->qgroup_to_skip = 0;
246 }
247 
248 /*
249  * We want the transaction abort to print stack trace only for errors where the
250  * cause could be a bug, eg. due to ENOSPC, and not for common errors that are
251  * caused by external factors.
252  */
btrfs_abort_should_print_stack(int error)253 static inline bool btrfs_abort_should_print_stack(int error)
254 {
255 	switch (error) {
256 	case -EIO:
257 	case -EROFS:
258 	case -ENOMEM:
259 		return false;
260 	}
261 	return true;
262 }
263 
264 /*
265  * Compile-time and run-time verification of error passed to transaction abort.
266  * Direct constants will be caught at compile time, errors read from variables
267  * can be caught only at run-time and will warn under debugging config.
268  *
269  * How verification works:
270  * - accepted builtin constants are all -EIO and such
271  * - for compile-time check, invalid condition produces a negative-sized array
272  *   type, valid zero-sized
273  * - when a variable is passed as error the first check is a no-op
274  * - with enabled debugging, the second array type size is constructed from the
275  *   real variable value, valid condition produces array of size 1
276  * - sizeof(type) does not generate any code
277  */
278 #define VERIFY_NEGATIVE_ERROR(error)						\
279 do {										\
280 	(void)sizeof(char[-!(__builtin_constant_p(error) ? (error) < 0 : 1)]);	\
281 	if (IS_ENABLED(CONFIG_BTRFS_DEBUG)) {					\
282 		if (sizeof(char[(error) < 0]) != 1)				\
283 			DEBUG_WARN("error >= 0 passed to btrfs_abort_transaction()"); \
284 	}									\
285 } while(0)
286 
287 /*
288  * Call btrfs_abort_transaction() as early as possible when an error condition
289  * is detected, that way the exact stack trace is reported for some errors.
290  *
291  * Error number must be negative as it encodes wheather it's the first abort.
292  */
293 #define btrfs_abort_transaction(trans, error)		\
294 do {								\
295 	int __error = (error);					\
296 								\
297 	VERIFY_NEGATIVE_ERROR(error);				\
298 	/* Report first abort since mount */			\
299 	if (!test_and_set_bit(BTRFS_FS_STATE_TRANS_ABORTED,	\
300 			&((trans)->fs_info->fs_state))) {	\
301 		WARN_ON(btrfs_abort_should_print_stack(__error)); \
302 		__error = -__error;				\
303 	}							\
304 	__btrfs_abort_transaction((trans), __func__,		\
305 				  __LINE__, __error);		\
306 } while (0)
307 
308 int btrfs_end_transaction(struct btrfs_trans_handle *trans);
309 struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
310 						   unsigned int num_items);
311 struct btrfs_trans_handle *btrfs_start_transaction_fallback_global_rsv(
312 					struct btrfs_root *root,
313 					unsigned int num_items);
314 struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root);
315 struct btrfs_trans_handle *btrfs_join_transaction_spacecache(struct btrfs_root *root);
316 struct btrfs_trans_handle *btrfs_join_transaction_nostart(struct btrfs_root *root);
317 struct btrfs_trans_handle *btrfs_attach_transaction(struct btrfs_root *root);
318 struct btrfs_trans_handle *btrfs_attach_transaction_barrier(
319 					struct btrfs_root *root);
320 int btrfs_wait_for_commit(struct btrfs_fs_info *fs_info, u64 transid);
321 
322 void btrfs_add_dead_root(struct btrfs_root *root);
323 void btrfs_maybe_wake_unfinished_drop(struct btrfs_fs_info *fs_info);
324 int btrfs_clean_one_deleted_snapshot(struct btrfs_fs_info *fs_info);
325 int btrfs_commit_transaction(struct btrfs_trans_handle *trans);
326 void btrfs_commit_transaction_async(struct btrfs_trans_handle *trans);
327 int btrfs_commit_current_transaction(struct btrfs_root *root);
328 int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans);
329 bool btrfs_should_end_transaction(struct btrfs_trans_handle *trans);
330 void btrfs_throttle(struct btrfs_fs_info *fs_info);
331 int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans,
332 				struct btrfs_root *root);
333 int btrfs_write_marked_extents(struct btrfs_fs_info *fs_info,
334 				struct extent_io_tree *dirty_pages, int mark);
335 int btrfs_wait_tree_log_extents(struct btrfs_root *root, int mark);
336 int btrfs_transaction_blocked(struct btrfs_fs_info *info);
337 void btrfs_put_transaction(struct btrfs_transaction *transaction);
338 void btrfs_add_dropped_root(struct btrfs_trans_handle *trans,
339 			    struct btrfs_root *root);
340 void btrfs_trans_release_chunk_metadata(struct btrfs_trans_handle *trans);
341 void __cold __btrfs_abort_transaction(struct btrfs_trans_handle *trans,
342 				      const char *function,
343 				      unsigned int line, int error);
344 
345 int __init btrfs_transaction_init(void);
346 void __cold btrfs_transaction_exit(void);
347 
348 #endif
349