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