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_USERSPACE (1U << 8)
109 #define __TRANS_START (1U << 9)
110 #define __TRANS_ATTACH (1U << 10)
111 #define __TRANS_JOIN (1U << 11)
112 #define __TRANS_JOIN_NOLOCK (1U << 12)
113 #define __TRANS_DUMMY (1U << 13)
115 #define TRANS_USERSPACE (__TRANS_USERSPACE | __TRANS_FREEZABLE)
116 #define TRANS_START (__TRANS_START | __TRANS_FREEZABLE)
117 #define TRANS_ATTACH (__TRANS_ATTACH)
118 #define TRANS_JOIN (__TRANS_JOIN | __TRANS_FREEZABLE)
119 #define TRANS_JOIN_NOLOCK (__TRANS_JOIN_NOLOCK)
121 #define TRANS_EXTWRITERS (__TRANS_USERSPACE | __TRANS_START | \
124 #define BTRFS_SEND_TRANS_STUB ((void *)1)
126 struct btrfs_trans_handle {
129 u64 chunk_bytes_reserved;
130 unsigned long delayed_ref_updates;
131 struct btrfs_transaction *transaction;
132 struct btrfs_block_rsv *block_rsv;
133 struct btrfs_block_rsv *orig_rsv;
134 refcount_t use_count;
138 bool allocating_chunk;
139 bool can_flush_pending_bgs;
143 struct btrfs_root *root;
144 struct btrfs_fs_info *fs_info;
145 struct list_head new_bgs;
148 struct btrfs_pending_snapshot {
149 struct dentry *dentry;
151 struct btrfs_root *root;
152 struct btrfs_root_item *root_item;
153 struct btrfs_root *snap;
154 struct btrfs_qgroup_inherit *inherit;
155 struct btrfs_path *path;
156 /* block reservation for the operation */
157 struct btrfs_block_rsv block_rsv;
159 /* extra metadata reservation for relocation */
162 struct list_head list;
165 static inline void btrfs_set_inode_last_trans(struct btrfs_trans_handle *trans,
168 spin_lock(&BTRFS_I(inode)->lock);
169 BTRFS_I(inode)->last_trans = trans->transaction->transid;
170 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
171 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
172 spin_unlock(&BTRFS_I(inode)->lock);
176 * Make qgroup codes to skip given qgroupid, means the old/new_roots for
177 * qgroup won't contain the qgroupid in it.
179 static inline void btrfs_set_skip_qgroup(struct btrfs_trans_handle *trans,
182 struct btrfs_delayed_ref_root *delayed_refs;
184 delayed_refs = &trans->transaction->delayed_refs;
185 WARN_ON(delayed_refs->qgroup_to_skip);
186 delayed_refs->qgroup_to_skip = qgroupid;
189 static inline void btrfs_clear_skip_qgroup(struct btrfs_trans_handle *trans)
191 struct btrfs_delayed_ref_root *delayed_refs;
193 delayed_refs = &trans->transaction->delayed_refs;
194 WARN_ON(!delayed_refs->qgroup_to_skip);
195 delayed_refs->qgroup_to_skip = 0;
198 int btrfs_end_transaction(struct btrfs_trans_handle *trans);
199 struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
200 unsigned int num_items);
201 struct btrfs_trans_handle *btrfs_start_transaction_fallback_global_rsv(
202 struct btrfs_root *root,
203 unsigned int num_items,
205 struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root);
206 struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root);
207 struct btrfs_trans_handle *btrfs_attach_transaction(struct btrfs_root *root);
208 struct btrfs_trans_handle *btrfs_attach_transaction_barrier(
209 struct btrfs_root *root);
210 struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *root);
211 int btrfs_wait_for_commit(struct btrfs_fs_info *fs_info, u64 transid);
213 void btrfs_add_dead_root(struct btrfs_root *root);
214 int btrfs_defrag_root(struct btrfs_root *root);
215 int btrfs_clean_one_deleted_snapshot(struct btrfs_root *root);
216 int btrfs_commit_transaction(struct btrfs_trans_handle *trans);
217 int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans,
218 int wait_for_unblock);
219 int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans);
220 int btrfs_should_end_transaction(struct btrfs_trans_handle *trans);
221 void btrfs_throttle(struct btrfs_fs_info *fs_info);
222 int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans,
223 struct btrfs_root *root);
224 int btrfs_write_marked_extents(struct btrfs_fs_info *fs_info,
225 struct extent_io_tree *dirty_pages, int mark);
226 int btrfs_wait_extents(struct btrfs_fs_info *fs_info,
227 struct extent_io_tree *dirty_pages);
228 int btrfs_wait_tree_log_extents(struct btrfs_root *root, int mark);
229 int btrfs_transaction_blocked(struct btrfs_fs_info *info);
230 int btrfs_transaction_in_commit(struct btrfs_fs_info *info);
231 void btrfs_put_transaction(struct btrfs_transaction *transaction);
232 void btrfs_apply_pending_changes(struct btrfs_fs_info *fs_info);
233 void btrfs_add_dropped_root(struct btrfs_trans_handle *trans,
234 struct btrfs_root *root);