Btrfs: corruption hunt continues
[linux-2.6] / fs / btrfs / transaction.c
1 #include <linux/module.h>
2 #include <linux/fs.h>
3 #include "ctree.h"
4 #include "disk-io.h"
5 #include "transaction.h"
6
7 static int total_trans = 0;
8 static void put_transaction(struct btrfs_transaction *transaction)
9 {
10         transaction->use_count--;
11         if (transaction->use_count == 0) {
12                 WARN_ON(total_trans == 0);
13                 total_trans--;
14                 kfree(transaction);
15         }
16 }
17
18 static int join_transaction(struct btrfs_root *root)
19 {
20         struct btrfs_transaction *cur_trans;
21         cur_trans = root->fs_info->running_transaction;
22         if (!cur_trans) {
23                 cur_trans = kmalloc(sizeof(*cur_trans), GFP_NOFS);
24                 total_trans++;
25                 BUG_ON(!cur_trans);
26                 root->fs_info->running_transaction = cur_trans;
27                 cur_trans->num_writers = 0;
28                 cur_trans->transid = root->root_key.offset + 1;
29                 init_waitqueue_head(&cur_trans->writer_wait);
30                 init_waitqueue_head(&cur_trans->commit_wait);
31                 cur_trans->in_commit = 0;
32                 cur_trans->use_count = 1;
33                 cur_trans->commit_done = 0;
34         }
35         cur_trans->num_writers++;
36         return 0;
37 }
38
39 struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
40                                                    int num_blocks)
41 {
42         struct btrfs_trans_handle *h = kmalloc(sizeof(*h), GFP_NOFS);
43         int ret;
44
45         mutex_lock(&root->fs_info->trans_mutex);
46         ret = join_transaction(root);
47         BUG_ON(ret);
48         h->transid = root->fs_info->running_transaction->transid;
49         h->transaction = root->fs_info->running_transaction;
50         h->blocks_reserved = num_blocks;
51         h->blocks_used = 0;
52         root->fs_info->running_transaction->use_count++;
53         mutex_unlock(&root->fs_info->trans_mutex);
54         return h;
55 }
56
57 int btrfs_end_transaction(struct btrfs_trans_handle *trans,
58                           struct btrfs_root *root)
59 {
60         struct btrfs_transaction *cur_trans;
61         mutex_lock(&root->fs_info->trans_mutex);
62         cur_trans = root->fs_info->running_transaction;
63         WARN_ON(cur_trans->num_writers < 1);
64         if (waitqueue_active(&cur_trans->writer_wait))
65                 wake_up(&cur_trans->writer_wait);
66         cur_trans->num_writers--;
67         put_transaction(cur_trans);
68         mutex_unlock(&root->fs_info->trans_mutex);
69         memset(trans, 0, sizeof(*trans));
70         kfree(trans);
71         return 0;
72 }
73
74
75 int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
76                                      struct btrfs_root *root)
77 {
78         filemap_write_and_wait(root->fs_info->btree_inode->i_mapping);
79         return 0;
80 }
81
82 int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans,
83                             struct btrfs_root *root)
84 {
85         int ret;
86         u64 old_extent_block;
87         struct btrfs_fs_info *fs_info = root->fs_info;
88         struct btrfs_root *tree_root = fs_info->tree_root;
89         struct btrfs_root *extent_root = fs_info->extent_root;
90         struct btrfs_root *inode_root = fs_info->inode_root;
91
92         btrfs_set_root_blocknr(&inode_root->root_item,
93                                inode_root->node->b_blocknr);
94         ret = btrfs_update_root(trans, tree_root,
95                                 &inode_root->root_key,
96                                 &inode_root->root_item);
97         BUG_ON(ret);
98         while(1) {
99                 old_extent_block = btrfs_root_blocknr(&extent_root->root_item);
100                 if (old_extent_block == extent_root->node->b_blocknr)
101                         break;
102                 btrfs_set_root_blocknr(&extent_root->root_item,
103                                        extent_root->node->b_blocknr);
104                 ret = btrfs_update_root(trans, tree_root,
105                                         &extent_root->root_key,
106                                         &extent_root->root_item);
107                 BUG_ON(ret);
108         }
109         return 0;
110 }
111
112 static int wait_for_commit(struct btrfs_root *root,
113                            struct btrfs_transaction *commit)
114 {
115         DEFINE_WAIT(wait);
116         while(!commit->commit_done) {
117                 prepare_to_wait(&commit->commit_wait, &wait,
118                                 TASK_UNINTERRUPTIBLE);
119                 if (commit->commit_done)
120                         break;
121                 mutex_unlock(&root->fs_info->trans_mutex);
122                 schedule();
123                 mutex_lock(&root->fs_info->trans_mutex);
124         }
125         finish_wait(&commit->commit_wait, &wait);
126         return 0;
127 }
128
129 int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
130                              struct btrfs_root *root)
131 {
132         int ret = 0;
133         struct buffer_head *snap;
134         struct btrfs_key snap_key;
135         struct btrfs_transaction *cur_trans;
136         DEFINE_WAIT(wait);
137
138         mutex_lock(&root->fs_info->trans_mutex);
139         if (trans->transaction->in_commit) {
140                 cur_trans = trans->transaction;
141                 trans->transaction->use_count++;
142                 btrfs_end_transaction(trans, root);
143                 ret = wait_for_commit(root, cur_trans);
144                 BUG_ON(ret);
145                 put_transaction(cur_trans);
146                 mutex_unlock(&root->fs_info->trans_mutex);
147                 return 0;
148         }
149         while (trans->transaction->num_writers > 1) {
150                 prepare_to_wait(&trans->transaction->writer_wait, &wait,
151                                 TASK_UNINTERRUPTIBLE);
152                 if (trans->transaction->num_writers <= 1)
153                         break;
154                 mutex_unlock(&root->fs_info->trans_mutex);
155                 schedule();
156                 mutex_lock(&root->fs_info->trans_mutex);
157         }
158         finish_wait(&trans->transaction->writer_wait, &wait);
159
160         if (root->node != root->commit_root) {
161                 memcpy(&snap_key, &root->root_key, sizeof(snap_key));
162                 root->root_key.offset++;
163         }
164
165
166         if (btrfs_root_blocknr(&root->root_item) != root->node->b_blocknr) {
167                 btrfs_set_root_blocknr(&root->root_item, root->node->b_blocknr);
168                 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
169                                         &root->root_key, &root->root_item);
170                 BUG_ON(ret);
171         }
172
173         ret = btrfs_commit_tree_roots(trans, root);
174         BUG_ON(ret);
175
176         cur_trans = root->fs_info->running_transaction;
177         root->fs_info->running_transaction = NULL;
178         mutex_unlock(&root->fs_info->trans_mutex);
179
180         ret = btrfs_write_and_wait_transaction(trans, root);
181         BUG_ON(ret);
182
183         write_ctree_super(trans, root);
184         btrfs_finish_extent_commit(trans, root);
185         mutex_lock(&root->fs_info->trans_mutex);
186         put_transaction(cur_trans);
187         put_transaction(cur_trans);
188         mutex_unlock(&root->fs_info->trans_mutex);
189         kfree(trans);
190
191         if (root->node != root->commit_root) {
192                 trans = btrfs_start_transaction(root, 1);
193                 snap = root->commit_root;
194                 root->commit_root = root->node;
195                 get_bh(root->node);
196                 ret = btrfs_drop_snapshot(trans, root, snap);
197                 BUG_ON(ret);
198
199                 ret = btrfs_del_root(trans, root->fs_info->tree_root,
200                                      &snap_key);
201                 BUG_ON(ret);
202                 root->fs_info->generation = root->root_key.offset + 1;
203                 ret = btrfs_end_transaction(trans, root);
204                 BUG_ON(ret);
205         }
206
207         return ret;
208 }
209