Btrfs: allocator and tuning
[deliverable/linux.git] / fs / btrfs / transaction.c
CommitLineData
79154b1b
CM
1#include <linux/module.h>
2#include <linux/fs.h>
3#include "ctree.h"
4#include "disk-io.h"
5#include "transaction.h"
6
78fae27e 7static int total_trans = 0;
2c90e5d6
CM
8extern struct kmem_cache *btrfs_trans_handle_cachep;
9extern struct kmem_cache *btrfs_transaction_cachep;
10
0f7d52f4
CM
11#define BTRFS_ROOT_TRANS_TAG 0
12
2c90e5d6 13#define TRANS_MAGIC 0xE1E10E
79154b1b
CM
14static void put_transaction(struct btrfs_transaction *transaction)
15{
2c90e5d6 16 WARN_ON(transaction->use_count == 0);
79154b1b 17 transaction->use_count--;
2c90e5d6 18 WARN_ON(transaction->magic != TRANS_MAGIC);
78fae27e
CM
19 if (transaction->use_count == 0) {
20 WARN_ON(total_trans == 0);
21 total_trans--;
8fd17795 22 list_del_init(&transaction->list);
2c90e5d6
CM
23 memset(transaction, 0, sizeof(*transaction));
24 kmem_cache_free(btrfs_transaction_cachep, transaction);
78fae27e 25 }
79154b1b
CM
26}
27
28static int join_transaction(struct btrfs_root *root)
29{
30 struct btrfs_transaction *cur_trans;
31 cur_trans = root->fs_info->running_transaction;
32 if (!cur_trans) {
2c90e5d6
CM
33 cur_trans = kmem_cache_alloc(btrfs_transaction_cachep,
34 GFP_NOFS);
78fae27e 35 total_trans++;
79154b1b 36 BUG_ON(!cur_trans);
0f7d52f4 37 root->fs_info->generation++;
79154b1b
CM
38 root->fs_info->running_transaction = cur_trans;
39 cur_trans->num_writers = 0;
0f7d52f4 40 cur_trans->transid = root->fs_info->generation;
79154b1b
CM
41 init_waitqueue_head(&cur_trans->writer_wait);
42 init_waitqueue_head(&cur_trans->commit_wait);
2c90e5d6 43 cur_trans->magic = TRANS_MAGIC;
79154b1b 44 cur_trans->in_commit = 0;
d5719762 45 cur_trans->use_count = 1;
79154b1b 46 cur_trans->commit_done = 0;
8fd17795 47 list_add_tail(&cur_trans->list, &root->fs_info->trans_list);
7c4452b9 48 init_bit_radix(&cur_trans->dirty_pages);
79154b1b
CM
49 }
50 cur_trans->num_writers++;
51 return 0;
52}
53
54struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
55 int num_blocks)
56{
2c90e5d6
CM
57 struct btrfs_trans_handle *h =
58 kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
79154b1b 59 int ret;
0f7d52f4 60 u64 running_trans_id;
79154b1b
CM
61
62 mutex_lock(&root->fs_info->trans_mutex);
63 ret = join_transaction(root);
64 BUG_ON(ret);
0f7d52f4
CM
65 running_trans_id = root->fs_info->running_transaction->transid;
66
67 if (root != root->fs_info->tree_root && root->last_trans <
68 running_trans_id) {
69 radix_tree_tag_set(&root->fs_info->fs_roots_radix,
2619ba1f
CM
70 (unsigned long)root->root_key.objectid,
71 BTRFS_ROOT_TRANS_TAG);
0f7d52f4
CM
72 root->commit_root = root->node;
73 get_bh(root->node);
74 }
75 root->last_trans = running_trans_id;
76 h->transid = running_trans_id;
79154b1b
CM
77 h->transaction = root->fs_info->running_transaction;
78 h->blocks_reserved = num_blocks;
79 h->blocks_used = 0;
31f3c99b 80 h->block_group = NULL;
79154b1b
CM
81 root->fs_info->running_transaction->use_count++;
82 mutex_unlock(&root->fs_info->trans_mutex);
2c90e5d6 83 h->magic = h->magic2 = TRANS_MAGIC;
79154b1b
CM
84 return h;
85}
86
87int btrfs_end_transaction(struct btrfs_trans_handle *trans,
88 struct btrfs_root *root)
89{
90 struct btrfs_transaction *cur_trans;
d6e4a428 91
2c90e5d6
CM
92 WARN_ON(trans->magic != TRANS_MAGIC);
93 WARN_ON(trans->magic2 != TRANS_MAGIC);
79154b1b
CM
94 mutex_lock(&root->fs_info->trans_mutex);
95 cur_trans = root->fs_info->running_transaction;
d5719762 96 WARN_ON(cur_trans->num_writers < 1);
79154b1b
CM
97 if (waitqueue_active(&cur_trans->writer_wait))
98 wake_up(&cur_trans->writer_wait);
99 cur_trans->num_writers--;
100 put_transaction(cur_trans);
101 mutex_unlock(&root->fs_info->trans_mutex);
d6025579 102 memset(trans, 0, sizeof(*trans));
2c90e5d6 103 kmem_cache_free(btrfs_trans_handle_cachep, trans);
79154b1b
CM
104 return 0;
105}
106
107
108int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
109 struct btrfs_root *root)
110{
7c4452b9
CM
111 unsigned long gang[16];
112 int ret;
113 int i;
114 int err;
115 int werr = 0;
116 struct page *page;
117 struct radix_tree_root *dirty_pages;
118 struct inode *btree_inode = root->fs_info->btree_inode;
119
120 if (!trans || !trans->transaction) {
121 return filemap_write_and_wait(btree_inode->i_mapping);
122 }
123 dirty_pages = &trans->transaction->dirty_pages;
124 while(1) {
125 ret = find_first_radix_bit(dirty_pages, gang, ARRAY_SIZE(gang));
126 if (!ret)
127 break;
128 for (i = 0; i < ret; i++) {
129 /* FIXME EIO */
130 clear_radix_bit(dirty_pages, gang[i]);
131 page = find_lock_page(btree_inode->i_mapping,
132 gang[i]);
133 if (!page)
134 continue;
135 err = write_one_page(page, 0);
136 if (err)
137 werr = err;
138 page_cache_release(page);
139 }
140 }
141 err = filemap_fdatawait(btree_inode->i_mapping);
142 if (err)
143 werr = err;
144 return werr;
79154b1b
CM
145}
146
147int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans,
148 struct btrfs_root *root)
149{
150 int ret;
151 u64 old_extent_block;
152 struct btrfs_fs_info *fs_info = root->fs_info;
153 struct btrfs_root *tree_root = fs_info->tree_root;
154 struct btrfs_root *extent_root = fs_info->extent_root;
8352d8a4 155 struct btrfs_root *dev_root = fs_info->dev_root;
79154b1b 156
8352d8a4
CM
157 if (btrfs_super_device_root(fs_info->disk_super) !=
158 bh_blocknr(dev_root->node)) {
159 btrfs_set_super_device_root(fs_info->disk_super,
160 bh_blocknr(dev_root->node));
161 }
9078a3e1 162 btrfs_write_dirty_block_groups(trans, extent_root);
79154b1b
CM
163 while(1) {
164 old_extent_block = btrfs_root_blocknr(&extent_root->root_item);
7eccb903 165 if (old_extent_block == bh_blocknr(extent_root->node))
79154b1b
CM
166 break;
167 btrfs_set_root_blocknr(&extent_root->root_item,
7eccb903 168 bh_blocknr(extent_root->node));
79154b1b
CM
169 ret = btrfs_update_root(trans, tree_root,
170 &extent_root->root_key,
171 &extent_root->root_item);
172 BUG_ON(ret);
9078a3e1 173 btrfs_write_dirty_block_groups(trans, extent_root);
79154b1b
CM
174 }
175 return 0;
176}
177
178static int wait_for_commit(struct btrfs_root *root,
179 struct btrfs_transaction *commit)
180{
181 DEFINE_WAIT(wait);
79154b1b
CM
182 while(!commit->commit_done) {
183 prepare_to_wait(&commit->commit_wait, &wait,
184 TASK_UNINTERRUPTIBLE);
185 if (commit->commit_done)
186 break;
187 mutex_unlock(&root->fs_info->trans_mutex);
188 schedule();
189 mutex_lock(&root->fs_info->trans_mutex);
190 }
191 finish_wait(&commit->commit_wait, &wait);
192 return 0;
193}
194
0f7d52f4
CM
195struct dirty_root {
196 struct list_head list;
197 struct btrfs_key snap_key;
198 struct buffer_head *commit_root;
199 struct btrfs_root *root;
200};
201
35b7e476
CM
202static int add_dirty_roots(struct btrfs_trans_handle *trans,
203 struct radix_tree_root *radix,
204 struct list_head *list)
0f7d52f4
CM
205{
206 struct dirty_root *dirty;
207 struct btrfs_root *gang[8];
208 struct btrfs_root *root;
209 int i;
210 int ret;
211 int err;
0f7d52f4
CM
212 while(1) {
213 ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0,
214 ARRAY_SIZE(gang),
215 BTRFS_ROOT_TRANS_TAG);
216 if (ret == 0)
217 break;
218 for (i = 0; i < ret; i++) {
219 root = gang[i];
2619ba1f
CM
220 radix_tree_tag_clear(radix,
221 (unsigned long)root->root_key.objectid,
222 BTRFS_ROOT_TRANS_TAG);
0f7d52f4 223 if (root->commit_root == root->node) {
7eccb903 224 WARN_ON(bh_blocknr(root->node) !=
0f7d52f4
CM
225 btrfs_root_blocknr(&root->root_item));
226 brelse(root->commit_root);
227 root->commit_root = NULL;
228 continue;
229 }
230 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
231 BUG_ON(!dirty);
232 memcpy(&dirty->snap_key, &root->root_key,
233 sizeof(root->root_key));
234 dirty->commit_root = root->commit_root;
235 root->commit_root = NULL;
236 dirty->root = root;
0f7d52f4
CM
237 root->root_key.offset = root->fs_info->generation;
238 btrfs_set_root_blocknr(&root->root_item,
7eccb903 239 bh_blocknr(root->node));
0f7d52f4
CM
240 err = btrfs_insert_root(trans, root->fs_info->tree_root,
241 &root->root_key,
242 &root->root_item);
243 BUG_ON(err);
244 list_add(&dirty->list, list);
245 }
246 }
0f7d52f4
CM
247 return 0;
248}
249
35b7e476
CM
250static int drop_dirty_roots(struct btrfs_root *tree_root,
251 struct list_head *list)
0f7d52f4
CM
252{
253 struct dirty_root *dirty;
254 struct btrfs_trans_handle *trans;
255 int ret;
256
257 while(!list_empty(list)) {
258 dirty = list_entry(list->next, struct dirty_root, list);
259 list_del_init(&dirty->list);
260 trans = btrfs_start_transaction(tree_root, 1);
0f7d52f4
CM
261 ret = btrfs_drop_snapshot(trans, dirty->root,
262 dirty->commit_root);
263 BUG_ON(ret);
264
0f7d52f4
CM
265 ret = btrfs_del_root(trans, tree_root, &dirty->snap_key);
266 BUG_ON(ret);
267 ret = btrfs_end_transaction(trans, tree_root);
268 BUG_ON(ret);
269 kfree(dirty);
270 }
271 return 0;
272}
273
79154b1b
CM
274int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
275 struct btrfs_root *root)
276{
277 int ret = 0;
79154b1b 278 struct btrfs_transaction *cur_trans;
8fd17795 279 struct btrfs_transaction *prev_trans = NULL;
0f7d52f4 280 struct list_head dirty_fs_roots;
79154b1b
CM
281 DEFINE_WAIT(wait);
282
0f7d52f4 283 INIT_LIST_HEAD(&dirty_fs_roots);
d6e4a428 284
79154b1b
CM
285 mutex_lock(&root->fs_info->trans_mutex);
286 if (trans->transaction->in_commit) {
287 cur_trans = trans->transaction;
288 trans->transaction->use_count++;
289 btrfs_end_transaction(trans, root);
290 ret = wait_for_commit(root, cur_trans);
291 BUG_ON(ret);
292 put_transaction(cur_trans);
293 mutex_unlock(&root->fs_info->trans_mutex);
294 return 0;
295 }
2c90e5d6
CM
296 cur_trans = trans->transaction;
297 trans->transaction->in_commit = 1;
79154b1b 298 while (trans->transaction->num_writers > 1) {
2c90e5d6 299 WARN_ON(cur_trans != trans->transaction);
79154b1b
CM
300 prepare_to_wait(&trans->transaction->writer_wait, &wait,
301 TASK_UNINTERRUPTIBLE);
302 if (trans->transaction->num_writers <= 1)
303 break;
304 mutex_unlock(&root->fs_info->trans_mutex);
305 schedule();
306 mutex_lock(&root->fs_info->trans_mutex);
2c90e5d6 307 finish_wait(&trans->transaction->writer_wait, &wait);
79154b1b
CM
308 }
309 finish_wait(&trans->transaction->writer_wait, &wait);
2c90e5d6 310 WARN_ON(cur_trans != trans->transaction);
0f7d52f4 311 add_dirty_roots(trans, &root->fs_info->fs_roots_radix, &dirty_fs_roots);
79154b1b
CM
312 ret = btrfs_commit_tree_roots(trans, root);
313 BUG_ON(ret);
78fae27e
CM
314 cur_trans = root->fs_info->running_transaction;
315 root->fs_info->running_transaction = NULL;
8fd17795
CM
316 if (cur_trans->list.prev != &root->fs_info->trans_list) {
317 prev_trans = list_entry(cur_trans->list.prev,
318 struct btrfs_transaction, list);
319 if (prev_trans->commit_done)
320 prev_trans = NULL;
321 else
322 prev_trans->use_count++;
323 }
78fae27e 324 mutex_unlock(&root->fs_info->trans_mutex);
8fd17795 325 mutex_unlock(&root->fs_info->fs_mutex);
79154b1b 326 ret = btrfs_write_and_wait_transaction(trans, root);
8fd17795
CM
327 if (prev_trans) {
328 mutex_lock(&root->fs_info->trans_mutex);
329 wait_for_commit(root, prev_trans);
330 put_transaction(prev_trans);
331 mutex_unlock(&root->fs_info->trans_mutex);
332 }
333 btrfs_set_super_generation(root->fs_info->disk_super,
334 cur_trans->transid);
79154b1b 335 BUG_ON(ret);
79154b1b 336 write_ctree_super(trans, root);
8fd17795
CM
337
338 mutex_lock(&root->fs_info->fs_mutex);
78fae27e
CM
339 btrfs_finish_extent_commit(trans, root);
340 mutex_lock(&root->fs_info->trans_mutex);
2c90e5d6
CM
341 cur_trans->commit_done = 1;
342 wake_up(&cur_trans->commit_wait);
78fae27e 343 put_transaction(cur_trans);
79154b1b 344 put_transaction(cur_trans);
78fae27e 345 mutex_unlock(&root->fs_info->trans_mutex);
2c90e5d6 346 kmem_cache_free(btrfs_trans_handle_cachep, trans);
79154b1b 347
0f7d52f4 348 drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);
79154b1b
CM
349 return ret;
350}
351
This page took 0.066057 seconds and 5 git commands to generate.