2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #ifndef __BTRFS_TRANSACTION__
20 #define __BTRFS_TRANSACTION__
22 #include <linux/refcount.h>
23 #include "btrfs_inode.h"
24 #include "delayed-ref.h"
27 enum btrfs_trans_state {
28 TRANS_STATE_RUNNING = 0,
29 TRANS_STATE_BLOCKED = 1,
30 TRANS_STATE_COMMIT_START = 2,
31 TRANS_STATE_COMMIT_DOING = 3,
32 TRANS_STATE_UNBLOCKED = 4,
33 TRANS_STATE_COMPLETED = 5,
37 #define BTRFS_TRANS_HAVE_FREE_BGS 0
38 #define BTRFS_TRANS_DIRTY_BG_RUN 1
39 #define BTRFS_TRANS_CACHE_ENOSPC 2
41 struct btrfs_transaction {
44 * total external writers(USERSPACE/START/ATTACH) in this
45 * transaction, it must be zero before the transaction is
48 atomic_t num_extwriters;
50 * total writers in this transaction, it must be zero before the
55 atomic_t pending_ordered;
59 /* Be protected by fs_info->trans_lock when we want to change it. */
60 enum btrfs_trans_state state;
62 struct list_head list;
63 struct extent_io_tree dirty_pages;
64 unsigned long start_time;
65 wait_queue_head_t writer_wait;
66 wait_queue_head_t commit_wait;
67 wait_queue_head_t pending_wait;
68 struct list_head pending_snapshots;
69 struct list_head pending_chunks;
70 struct list_head switch_commits;
71 struct list_head dirty_bgs;
74 * There is no explicit lock which protects io_bgs, rather its
75 * consistency is implied by the fact that all the sites which modify
76 * it do so under some form of transaction critical section, namely:
78 * - btrfs_start_dirty_block_groups - This function can only ever be
79 * run by one of the transaction committers. Refer to
80 * BTRFS_TRANS_DIRTY_BG_RUN usage in btrfs_commit_transaction
82 * - btrfs_write_dirty_blockgroups - this is called by
83 * commit_cowonly_roots from transaction critical section
84 * (TRANS_STATE_COMMIT_DOING)
86 * - btrfs_cleanup_dirty_bgs - called on transaction abort
88 struct list_head io_bgs;
89 struct list_head dropped_roots;
92 * we need to make sure block group deletion doesn't race with
93 * free space cache writeout. This mutex keeps them from stomping
96 struct mutex cache_write_mutex;
97 spinlock_t dirty_bgs_lock;
98 unsigned int num_dirty_bgs;
99 /* Protected by spin lock fs_info->unused_bgs_lock. */
100 struct list_head deleted_bgs;
101 spinlock_t dropped_roots_lock;
102 struct btrfs_delayed_ref_root delayed_refs;
103 struct btrfs_fs_info *fs_info;
106 #define __TRANS_FREEZABLE (1U << 0)
108 #define __TRANS_START (1U << 9)
109 #define __TRANS_ATTACH (1U << 10)
110 #define __TRANS_JOIN (1U << 11)
111 #define __TRANS_JOIN_NOLOCK (1U << 12)
112 #define __TRANS_DUMMY (1U << 13)
114 #define TRANS_START (__TRANS_START | __TRANS_FREEZABLE)
115 #define TRANS_ATTACH (__TRANS_ATTACH)
116 #define TRANS_JOIN (__TRANS_JOIN | __TRANS_FREEZABLE)
117 #define TRANS_JOIN_NOLOCK (__TRANS_JOIN_NOLOCK)
119 #define TRANS_EXTWRITERS (__TRANS_START | __TRANS_ATTACH)
121 #define BTRFS_SEND_TRANS_STUB ((void *)1)
123 struct btrfs_trans_handle {
126 u64 chunk_bytes_reserved;
127 unsigned long delayed_ref_updates;
128 struct btrfs_transaction *transaction;
129 struct btrfs_block_rsv *block_rsv;
130 struct btrfs_block_rsv *orig_rsv;
131 refcount_t use_count;
135 bool allocating_chunk;
136 bool can_flush_pending_bgs;
140 struct btrfs_root *root;
141 struct btrfs_fs_info *fs_info;
142 struct list_head new_bgs;
145 struct btrfs_pending_snapshot {
146 struct dentry *dentry;
148 struct btrfs_root *root;
149 struct btrfs_root_item *root_item;
150 struct btrfs_root *snap;
151 struct btrfs_qgroup_inherit *inherit;
152 struct btrfs_path *path;
153 /* block reservation for the operation */
154 struct btrfs_block_rsv block_rsv;
156 /* extra metadata reservation for relocation */
159 struct list_head list;
162 static inline void btrfs_set_inode_last_trans(struct btrfs_trans_handle *trans,
165 spin_lock(&BTRFS_I(inode)->lock);
166 BTRFS_I(inode)->last_trans = trans->transaction->transid;
167 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
168 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
169 spin_unlock(&BTRFS_I(inode)->lock);
173 * Make qgroup codes to skip given qgroupid, means the old/new_roots for
174 * qgroup won't contain the qgroupid in it.
176 static inline void btrfs_set_skip_qgroup(struct btrfs_trans_handle *trans,
179 struct btrfs_delayed_ref_root *delayed_refs;
181 delayed_refs = &trans->transaction->delayed_refs;
182 WARN_ON(delayed_refs->qgroup_to_skip);
183 delayed_refs->qgroup_to_skip = qgroupid;
186 static inline void btrfs_clear_skip_qgroup(struct btrfs_trans_handle *trans)
188 struct btrfs_delayed_ref_root *delayed_refs;
190 delayed_refs = &trans->transaction->delayed_refs;
191 WARN_ON(!delayed_refs->qgroup_to_skip);
192 delayed_refs->qgroup_to_skip = 0;
195 int btrfs_end_transaction(struct btrfs_trans_handle *trans);
196 struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
197 unsigned int num_items);
198 struct btrfs_trans_handle *btrfs_start_transaction_fallback_global_rsv(
199 struct btrfs_root *root,
200 unsigned int num_items,
202 struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root);
203 struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root);
204 struct btrfs_trans_handle *btrfs_attach_transaction(struct btrfs_root *root);
205 struct btrfs_trans_handle *btrfs_attach_transaction_barrier(
206 struct btrfs_root *root);
207 int btrfs_wait_for_commit(struct btrfs_fs_info *fs_info, u64 transid);
209 void btrfs_add_dead_root(struct btrfs_root *root);
210 int btrfs_defrag_root(struct btrfs_root *root);
211 int btrfs_clean_one_deleted_snapshot(struct btrfs_root *root);
212 int btrfs_commit_transaction(struct btrfs_trans_handle *trans);
213 int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans,
214 int wait_for_unblock);
215 int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans);
216 int btrfs_should_end_transaction(struct btrfs_trans_handle *trans);
217 void btrfs_throttle(struct btrfs_fs_info *fs_info);
218 int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans,
219 struct btrfs_root *root);
220 int btrfs_write_marked_extents(struct btrfs_fs_info *fs_info,
221 struct extent_io_tree *dirty_pages, int mark);
222 int btrfs_wait_extents(struct btrfs_fs_info *fs_info,
223 struct extent_io_tree *dirty_pages);
224 int btrfs_wait_tree_log_extents(struct btrfs_root *root, int mark);
225 int btrfs_transaction_blocked(struct btrfs_fs_info *info);
226 int btrfs_transaction_in_commit(struct btrfs_fs_info *info);
227 void btrfs_put_transaction(struct btrfs_transaction *transaction);
228 void btrfs_apply_pending_changes(struct btrfs_fs_info *fs_info);
229 void btrfs_add_dropped_root(struct btrfs_trans_handle *trans,
230 struct btrfs_root *root);