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