Btrfs: prealloc more blocks for the extent map
[linux-2.6-block.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;
80 root->fs_info->running_transaction->use_count++;
81 mutex_unlock(&root->fs_info->trans_mutex);
2c90e5d6 82 h->magic = h->magic2 = TRANS_MAGIC;
79154b1b
CM
83 return h;
84}
85
86int btrfs_end_transaction(struct btrfs_trans_handle *trans,
87 struct btrfs_root *root)
88{
89 struct btrfs_transaction *cur_trans;
d6e4a428 90
2c90e5d6
CM
91 WARN_ON(trans->magic != TRANS_MAGIC);
92 WARN_ON(trans->magic2 != TRANS_MAGIC);
79154b1b
CM
93 mutex_lock(&root->fs_info->trans_mutex);
94 cur_trans = root->fs_info->running_transaction;
d5719762 95 WARN_ON(cur_trans->num_writers < 1);
79154b1b
CM
96 if (waitqueue_active(&cur_trans->writer_wait))
97 wake_up(&cur_trans->writer_wait);
98 cur_trans->num_writers--;
99 put_transaction(cur_trans);
100 mutex_unlock(&root->fs_info->trans_mutex);
d6025579 101 memset(trans, 0, sizeof(*trans));
2c90e5d6 102 kmem_cache_free(btrfs_trans_handle_cachep, trans);
79154b1b
CM
103 return 0;
104}
105
106
107int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
108 struct btrfs_root *root)
109{
7c4452b9
CM
110 unsigned long gang[16];
111 int ret;
112 int i;
113 int err;
114 int werr = 0;
115 struct page *page;
116 struct radix_tree_root *dirty_pages;
117 struct inode *btree_inode = root->fs_info->btree_inode;
118
119 if (!trans || !trans->transaction) {
120 return filemap_write_and_wait(btree_inode->i_mapping);
121 }
122 dirty_pages = &trans->transaction->dirty_pages;
123 while(1) {
124 ret = find_first_radix_bit(dirty_pages, gang, ARRAY_SIZE(gang));
125 if (!ret)
126 break;
127 for (i = 0; i < ret; i++) {
128 /* FIXME EIO */
129 clear_radix_bit(dirty_pages, gang[i]);
130 page = find_lock_page(btree_inode->i_mapping,
131 gang[i]);
132 if (!page)
133 continue;
134 err = write_one_page(page, 0);
135 if (err)
136 werr = err;
137 page_cache_release(page);
138 }
139 }
140 err = filemap_fdatawait(btree_inode->i_mapping);
141 if (err)
142 werr = err;
143 return werr;
79154b1b
CM
144}
145
146int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans,
147 struct btrfs_root *root)
148{
149 int ret;
150 u64 old_extent_block;
151 struct btrfs_fs_info *fs_info = root->fs_info;
152 struct btrfs_root *tree_root = fs_info->tree_root;
153 struct btrfs_root *extent_root = fs_info->extent_root;
8352d8a4 154 struct btrfs_root *dev_root = fs_info->dev_root;
79154b1b 155
8352d8a4
CM
156 if (btrfs_super_device_root(fs_info->disk_super) !=
157 bh_blocknr(dev_root->node)) {
158 btrfs_set_super_device_root(fs_info->disk_super,
159 bh_blocknr(dev_root->node));
160 }
9078a3e1 161 btrfs_write_dirty_block_groups(trans, extent_root);
79154b1b
CM
162 while(1) {
163 old_extent_block = btrfs_root_blocknr(&extent_root->root_item);
7eccb903 164 if (old_extent_block == bh_blocknr(extent_root->node))
79154b1b
CM
165 break;
166 btrfs_set_root_blocknr(&extent_root->root_item,
7eccb903 167 bh_blocknr(extent_root->node));
79154b1b
CM
168 ret = btrfs_update_root(trans, tree_root,
169 &extent_root->root_key,
170 &extent_root->root_item);
171 BUG_ON(ret);
9078a3e1 172 btrfs_write_dirty_block_groups(trans, extent_root);
79154b1b
CM
173 }
174 return 0;
175}
176
177static int wait_for_commit(struct btrfs_root *root,
178 struct btrfs_transaction *commit)
179{
180 DEFINE_WAIT(wait);
79154b1b
CM
181 while(!commit->commit_done) {
182 prepare_to_wait(&commit->commit_wait, &wait,
183 TASK_UNINTERRUPTIBLE);
184 if (commit->commit_done)
185 break;
186 mutex_unlock(&root->fs_info->trans_mutex);
187 schedule();
188 mutex_lock(&root->fs_info->trans_mutex);
189 }
190 finish_wait(&commit->commit_wait, &wait);
191 return 0;
192}
193
0f7d52f4
CM
194struct dirty_root {
195 struct list_head list;
196 struct btrfs_key snap_key;
197 struct buffer_head *commit_root;
198 struct btrfs_root *root;
199};
200
201int add_dirty_roots(struct btrfs_trans_handle *trans,
202 struct radix_tree_root *radix, struct list_head *list)
203{
204 struct dirty_root *dirty;
205 struct btrfs_root *gang[8];
206 struct btrfs_root *root;
207 int i;
208 int ret;
209 int err;
0f7d52f4
CM
210 while(1) {
211 ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0,
212 ARRAY_SIZE(gang),
213 BTRFS_ROOT_TRANS_TAG);
214 if (ret == 0)
215 break;
216 for (i = 0; i < ret; i++) {
217 root = gang[i];
2619ba1f
CM
218 radix_tree_tag_clear(radix,
219 (unsigned long)root->root_key.objectid,
220 BTRFS_ROOT_TRANS_TAG);
0f7d52f4 221 if (root->commit_root == root->node) {
7eccb903 222 WARN_ON(bh_blocknr(root->node) !=
0f7d52f4
CM
223 btrfs_root_blocknr(&root->root_item));
224 brelse(root->commit_root);
225 root->commit_root = NULL;
226 continue;
227 }
228 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
229 BUG_ON(!dirty);
230 memcpy(&dirty->snap_key, &root->root_key,
231 sizeof(root->root_key));
232 dirty->commit_root = root->commit_root;
233 root->commit_root = NULL;
234 dirty->root = root;
0f7d52f4
CM
235 root->root_key.offset = root->fs_info->generation;
236 btrfs_set_root_blocknr(&root->root_item,
7eccb903 237 bh_blocknr(root->node));
0f7d52f4
CM
238 err = btrfs_insert_root(trans, root->fs_info->tree_root,
239 &root->root_key,
240 &root->root_item);
241 BUG_ON(err);
242 list_add(&dirty->list, list);
243 }
244 }
0f7d52f4
CM
245 return 0;
246}
247
248int drop_dirty_roots(struct btrfs_root *tree_root, struct list_head *list)
249{
250 struct dirty_root *dirty;
251 struct btrfs_trans_handle *trans;
252 int ret;
253
254 while(!list_empty(list)) {
255 dirty = list_entry(list->next, struct dirty_root, list);
256 list_del_init(&dirty->list);
257 trans = btrfs_start_transaction(tree_root, 1);
0f7d52f4
CM
258 ret = btrfs_drop_snapshot(trans, dirty->root,
259 dirty->commit_root);
260 BUG_ON(ret);
261
0f7d52f4
CM
262 ret = btrfs_del_root(trans, tree_root, &dirty->snap_key);
263 BUG_ON(ret);
264 ret = btrfs_end_transaction(trans, tree_root);
265 BUG_ON(ret);
266 kfree(dirty);
267 }
268 return 0;
269}
270
79154b1b
CM
271int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
272 struct btrfs_root *root)
273{
274 int ret = 0;
79154b1b 275 struct btrfs_transaction *cur_trans;
8fd17795 276 struct btrfs_transaction *prev_trans = NULL;
0f7d52f4 277 struct list_head dirty_fs_roots;
79154b1b
CM
278 DEFINE_WAIT(wait);
279
0f7d52f4 280 INIT_LIST_HEAD(&dirty_fs_roots);
d6e4a428 281
79154b1b
CM
282 mutex_lock(&root->fs_info->trans_mutex);
283 if (trans->transaction->in_commit) {
284 cur_trans = trans->transaction;
285 trans->transaction->use_count++;
286 btrfs_end_transaction(trans, root);
287 ret = wait_for_commit(root, cur_trans);
288 BUG_ON(ret);
289 put_transaction(cur_trans);
290 mutex_unlock(&root->fs_info->trans_mutex);
291 return 0;
292 }
2c90e5d6
CM
293 cur_trans = trans->transaction;
294 trans->transaction->in_commit = 1;
79154b1b 295 while (trans->transaction->num_writers > 1) {
2c90e5d6 296 WARN_ON(cur_trans != trans->transaction);
79154b1b
CM
297 prepare_to_wait(&trans->transaction->writer_wait, &wait,
298 TASK_UNINTERRUPTIBLE);
299 if (trans->transaction->num_writers <= 1)
300 break;
301 mutex_unlock(&root->fs_info->trans_mutex);
302 schedule();
303 mutex_lock(&root->fs_info->trans_mutex);
2c90e5d6 304 finish_wait(&trans->transaction->writer_wait, &wait);
79154b1b
CM
305 }
306 finish_wait(&trans->transaction->writer_wait, &wait);
2c90e5d6 307 WARN_ON(cur_trans != trans->transaction);
0f7d52f4 308 add_dirty_roots(trans, &root->fs_info->fs_roots_radix, &dirty_fs_roots);
79154b1b
CM
309 ret = btrfs_commit_tree_roots(trans, root);
310 BUG_ON(ret);
78fae27e
CM
311 cur_trans = root->fs_info->running_transaction;
312 root->fs_info->running_transaction = NULL;
8fd17795
CM
313 if (cur_trans->list.prev != &root->fs_info->trans_list) {
314 prev_trans = list_entry(cur_trans->list.prev,
315 struct btrfs_transaction, list);
316 if (prev_trans->commit_done)
317 prev_trans = NULL;
318 else
319 prev_trans->use_count++;
320 }
78fae27e 321 mutex_unlock(&root->fs_info->trans_mutex);
8fd17795 322 mutex_unlock(&root->fs_info->fs_mutex);
79154b1b 323 ret = btrfs_write_and_wait_transaction(trans, root);
8fd17795
CM
324 if (prev_trans) {
325 mutex_lock(&root->fs_info->trans_mutex);
326 wait_for_commit(root, prev_trans);
327 put_transaction(prev_trans);
328 mutex_unlock(&root->fs_info->trans_mutex);
329 }
330 btrfs_set_super_generation(root->fs_info->disk_super,
331 cur_trans->transid);
79154b1b 332 BUG_ON(ret);
79154b1b 333 write_ctree_super(trans, root);
8fd17795
CM
334
335 mutex_lock(&root->fs_info->fs_mutex);
78fae27e
CM
336 btrfs_finish_extent_commit(trans, root);
337 mutex_lock(&root->fs_info->trans_mutex);
2c90e5d6
CM
338 cur_trans->commit_done = 1;
339 wake_up(&cur_trans->commit_wait);
78fae27e 340 put_transaction(cur_trans);
79154b1b 341 put_transaction(cur_trans);
78fae27e 342 mutex_unlock(&root->fs_info->trans_mutex);
2c90e5d6 343 kmem_cache_free(btrfs_trans_handle_cachep, trans);
79154b1b 344
0f7d52f4 345 drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);
79154b1b
CM
346 return ret;
347}
348