]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/btrfs/transaction.c
Btrfs: drop the inode map tree
[net-next-2.6.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--;
2c90e5d6
CM
22 memset(transaction, 0, sizeof(*transaction));
23 kmem_cache_free(btrfs_transaction_cachep, transaction);
78fae27e 24 }
79154b1b
CM
25}
26
27static int join_transaction(struct btrfs_root *root)
28{
29 struct btrfs_transaction *cur_trans;
30 cur_trans = root->fs_info->running_transaction;
31 if (!cur_trans) {
2c90e5d6
CM
32 cur_trans = kmem_cache_alloc(btrfs_transaction_cachep,
33 GFP_NOFS);
78fae27e 34 total_trans++;
79154b1b 35 BUG_ON(!cur_trans);
0f7d52f4 36 root->fs_info->generation++;
79154b1b
CM
37 root->fs_info->running_transaction = cur_trans;
38 cur_trans->num_writers = 0;
0f7d52f4 39 cur_trans->transid = root->fs_info->generation;
79154b1b
CM
40 init_waitqueue_head(&cur_trans->writer_wait);
41 init_waitqueue_head(&cur_trans->commit_wait);
2c90e5d6 42 cur_trans->magic = TRANS_MAGIC;
79154b1b 43 cur_trans->in_commit = 0;
d5719762 44 cur_trans->use_count = 1;
79154b1b
CM
45 cur_trans->commit_done = 0;
46 }
47 cur_trans->num_writers++;
48 return 0;
49}
50
51struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
52 int num_blocks)
53{
2c90e5d6
CM
54 struct btrfs_trans_handle *h =
55 kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
79154b1b 56 int ret;
0f7d52f4 57 u64 running_trans_id;
79154b1b
CM
58
59 mutex_lock(&root->fs_info->trans_mutex);
60 ret = join_transaction(root);
61 BUG_ON(ret);
0f7d52f4
CM
62 running_trans_id = root->fs_info->running_transaction->transid;
63
64 if (root != root->fs_info->tree_root && root->last_trans <
65 running_trans_id) {
66 radix_tree_tag_set(&root->fs_info->fs_roots_radix,
67 (unsigned long)root, BTRFS_ROOT_TRANS_TAG);
68 root->commit_root = root->node;
69 get_bh(root->node);
70 }
71 root->last_trans = running_trans_id;
72 h->transid = running_trans_id;
79154b1b
CM
73 h->transaction = root->fs_info->running_transaction;
74 h->blocks_reserved = num_blocks;
75 h->blocks_used = 0;
76 root->fs_info->running_transaction->use_count++;
77 mutex_unlock(&root->fs_info->trans_mutex);
2c90e5d6 78 h->magic = h->magic2 = TRANS_MAGIC;
79154b1b
CM
79 return h;
80}
81
82int btrfs_end_transaction(struct btrfs_trans_handle *trans,
83 struct btrfs_root *root)
84{
85 struct btrfs_transaction *cur_trans;
d6e4a428 86
2c90e5d6
CM
87 WARN_ON(trans->magic != TRANS_MAGIC);
88 WARN_ON(trans->magic2 != TRANS_MAGIC);
79154b1b
CM
89 mutex_lock(&root->fs_info->trans_mutex);
90 cur_trans = root->fs_info->running_transaction;
d5719762 91 WARN_ON(cur_trans->num_writers < 1);
79154b1b
CM
92 if (waitqueue_active(&cur_trans->writer_wait))
93 wake_up(&cur_trans->writer_wait);
94 cur_trans->num_writers--;
95 put_transaction(cur_trans);
96 mutex_unlock(&root->fs_info->trans_mutex);
d6025579 97 memset(trans, 0, sizeof(*trans));
2c90e5d6 98 kmem_cache_free(btrfs_trans_handle_cachep, trans);
79154b1b
CM
99 return 0;
100}
101
102
103int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
104 struct btrfs_root *root)
105{
7cfcc17e 106 filemap_write_and_wait(root->fs_info->btree_inode->i_mapping);
79154b1b
CM
107 return 0;
108}
109
110int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans,
111 struct btrfs_root *root)
112{
113 int ret;
114 u64 old_extent_block;
115 struct btrfs_fs_info *fs_info = root->fs_info;
116 struct btrfs_root *tree_root = fs_info->tree_root;
117 struct btrfs_root *extent_root = fs_info->extent_root;
79154b1b 118
79154b1b
CM
119 while(1) {
120 old_extent_block = btrfs_root_blocknr(&extent_root->root_item);
121 if (old_extent_block == extent_root->node->b_blocknr)
122 break;
123 btrfs_set_root_blocknr(&extent_root->root_item,
124 extent_root->node->b_blocknr);
125 ret = btrfs_update_root(trans, tree_root,
126 &extent_root->root_key,
127 &extent_root->root_item);
128 BUG_ON(ret);
129 }
130 return 0;
131}
132
133static int wait_for_commit(struct btrfs_root *root,
134 struct btrfs_transaction *commit)
135{
136 DEFINE_WAIT(wait);
79154b1b
CM
137 while(!commit->commit_done) {
138 prepare_to_wait(&commit->commit_wait, &wait,
139 TASK_UNINTERRUPTIBLE);
140 if (commit->commit_done)
141 break;
142 mutex_unlock(&root->fs_info->trans_mutex);
143 schedule();
144 mutex_lock(&root->fs_info->trans_mutex);
145 }
146 finish_wait(&commit->commit_wait, &wait);
147 return 0;
148}
149
0f7d52f4
CM
150struct dirty_root {
151 struct list_head list;
152 struct btrfs_key snap_key;
153 struct buffer_head *commit_root;
154 struct btrfs_root *root;
155};
156
157int add_dirty_roots(struct btrfs_trans_handle *trans,
158 struct radix_tree_root *radix, struct list_head *list)
159{
160 struct dirty_root *dirty;
161 struct btrfs_root *gang[8];
162 struct btrfs_root *root;
163 int i;
164 int ret;
165 int err;
166printk("add dirty\n");
167 while(1) {
168 ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0,
169 ARRAY_SIZE(gang),
170 BTRFS_ROOT_TRANS_TAG);
171 if (ret == 0)
172 break;
173 for (i = 0; i < ret; i++) {
174 root = gang[i];
175 radix_tree_tag_clear(radix, (unsigned long)root,
176 BTRFS_ROOT_TRANS_TAG);
177 if (root->commit_root == root->node) {
178 WARN_ON(root->node->b_blocknr !=
179 btrfs_root_blocknr(&root->root_item));
180 brelse(root->commit_root);
181 root->commit_root = NULL;
182 continue;
183 }
184 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
185 BUG_ON(!dirty);
186 memcpy(&dirty->snap_key, &root->root_key,
187 sizeof(root->root_key));
188 dirty->commit_root = root->commit_root;
189 root->commit_root = NULL;
190 dirty->root = root;
191printk("adding dirty root %Lu gen %Lu blocknr %Lu\n", root->root_key.objectid, root->root_key.offset, dirty->commit_root->b_blocknr);
192 root->root_key.offset = root->fs_info->generation;
193 btrfs_set_root_blocknr(&root->root_item,
194 root->node->b_blocknr);
195 err = btrfs_insert_root(trans, root->fs_info->tree_root,
196 &root->root_key,
197 &root->root_item);
198 BUG_ON(err);
199 list_add(&dirty->list, list);
200 }
201 }
202printk("add dirty done\n");
203 return 0;
204}
205
206int drop_dirty_roots(struct btrfs_root *tree_root, struct list_head *list)
207{
208 struct dirty_root *dirty;
209 struct btrfs_trans_handle *trans;
210 int ret;
211
212 while(!list_empty(list)) {
213 dirty = list_entry(list->next, struct dirty_root, list);
214 list_del_init(&dirty->list);
215 trans = btrfs_start_transaction(tree_root, 1);
216printk("drop snapshot root %p, commit_root blocknr %Lu generation %Lu\n", dirty->root, dirty->commit_root->b_blocknr, dirty->snap_key.offset);
217 ret = btrfs_drop_snapshot(trans, dirty->root,
218 dirty->commit_root);
219 BUG_ON(ret);
220
221printk("del root objectid %Lu, offset %Lu\n", dirty->snap_key.objectid, dirty->snap_key.offset);
222 ret = btrfs_del_root(trans, tree_root, &dirty->snap_key);
223 BUG_ON(ret);
224 ret = btrfs_end_transaction(trans, tree_root);
225 BUG_ON(ret);
226 kfree(dirty);
227 }
228 return 0;
229}
230
79154b1b
CM
231int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
232 struct btrfs_root *root)
233{
234 int ret = 0;
79154b1b 235 struct btrfs_transaction *cur_trans;
0f7d52f4 236 struct list_head dirty_fs_roots;
79154b1b
CM
237 DEFINE_WAIT(wait);
238
0f7d52f4 239 INIT_LIST_HEAD(&dirty_fs_roots);
d6e4a428 240
79154b1b
CM
241 mutex_lock(&root->fs_info->trans_mutex);
242 if (trans->transaction->in_commit) {
2c90e5d6 243printk("already in commit!, waiting\n");
79154b1b
CM
244 cur_trans = trans->transaction;
245 trans->transaction->use_count++;
246 btrfs_end_transaction(trans, root);
247 ret = wait_for_commit(root, cur_trans);
248 BUG_ON(ret);
249 put_transaction(cur_trans);
250 mutex_unlock(&root->fs_info->trans_mutex);
251 return 0;
252 }
2c90e5d6
CM
253 cur_trans = trans->transaction;
254 trans->transaction->in_commit = 1;
79154b1b 255 while (trans->transaction->num_writers > 1) {
2c90e5d6 256 WARN_ON(cur_trans != trans->transaction);
79154b1b
CM
257 prepare_to_wait(&trans->transaction->writer_wait, &wait,
258 TASK_UNINTERRUPTIBLE);
259 if (trans->transaction->num_writers <= 1)
260 break;
261 mutex_unlock(&root->fs_info->trans_mutex);
262 schedule();
263 mutex_lock(&root->fs_info->trans_mutex);
2c90e5d6 264 finish_wait(&trans->transaction->writer_wait, &wait);
79154b1b
CM
265 }
266 finish_wait(&trans->transaction->writer_wait, &wait);
2c90e5d6 267 WARN_ON(cur_trans != trans->transaction);
0f7d52f4 268 add_dirty_roots(trans, &root->fs_info->fs_roots_radix, &dirty_fs_roots);
79154b1b
CM
269 ret = btrfs_commit_tree_roots(trans, root);
270 BUG_ON(ret);
78fae27e
CM
271 cur_trans = root->fs_info->running_transaction;
272 root->fs_info->running_transaction = NULL;
0f7d52f4
CM
273 btrfs_set_super_generation(root->fs_info->disk_super,
274 root->fs_info->generation + 1);
78fae27e 275 mutex_unlock(&root->fs_info->trans_mutex);
79154b1b
CM
276 ret = btrfs_write_and_wait_transaction(trans, root);
277 BUG_ON(ret);
278
279 write_ctree_super(trans, root);
78fae27e
CM
280 btrfs_finish_extent_commit(trans, root);
281 mutex_lock(&root->fs_info->trans_mutex);
2c90e5d6
CM
282 cur_trans->commit_done = 1;
283 wake_up(&cur_trans->commit_wait);
78fae27e 284 put_transaction(cur_trans);
79154b1b 285 put_transaction(cur_trans);
78fae27e 286 mutex_unlock(&root->fs_info->trans_mutex);
2c90e5d6 287 kmem_cache_free(btrfs_trans_handle_cachep, trans);
79154b1b 288
0f7d52f4 289 drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);
79154b1b
CM
290 return ret;
291}
292