Btrfs: no slashes in subvolume names
[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
08607c1b
CM
11static struct workqueue_struct *trans_wq;
12
0f7d52f4
CM
13#define BTRFS_ROOT_TRANS_TAG 0
14
79154b1b
CM
15static void put_transaction(struct btrfs_transaction *transaction)
16{
2c90e5d6 17 WARN_ON(transaction->use_count == 0);
79154b1b 18 transaction->use_count--;
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);
43 cur_trans->in_commit = 0;
d5719762 44 cur_trans->use_count = 1;
79154b1b 45 cur_trans->commit_done = 0;
08607c1b 46 cur_trans->start_time = get_seconds();
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);
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
79154b1b
CM
91 mutex_lock(&root->fs_info->trans_mutex);
92 cur_trans = root->fs_info->running_transaction;
d5719762 93 WARN_ON(cur_trans->num_writers < 1);
79154b1b
CM
94 if (waitqueue_active(&cur_trans->writer_wait))
95 wake_up(&cur_trans->writer_wait);
96 cur_trans->num_writers--;
97 put_transaction(cur_trans);
98 mutex_unlock(&root->fs_info->trans_mutex);
d6025579 99 memset(trans, 0, sizeof(*trans));
2c90e5d6 100 kmem_cache_free(btrfs_trans_handle_cachep, trans);
79154b1b
CM
101 return 0;
102}
103
104
105int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
106 struct btrfs_root *root)
107{
7c4452b9
CM
108 unsigned long gang[16];
109 int ret;
110 int i;
111 int err;
112 int werr = 0;
113 struct page *page;
114 struct radix_tree_root *dirty_pages;
115 struct inode *btree_inode = root->fs_info->btree_inode;
116
117 if (!trans || !trans->transaction) {
118 return filemap_write_and_wait(btree_inode->i_mapping);
119 }
120 dirty_pages = &trans->transaction->dirty_pages;
121 while(1) {
e37c9e69
CM
122 ret = find_first_radix_bit(dirty_pages, gang,
123 0, ARRAY_SIZE(gang));
7c4452b9
CM
124 if (!ret)
125 break;
126 for (i = 0; i < ret; i++) {
127 /* FIXME EIO */
128 clear_radix_bit(dirty_pages, gang[i]);
129 page = find_lock_page(btree_inode->i_mapping,
130 gang[i]);
131 if (!page)
132 continue;
133 err = write_one_page(page, 0);
134 if (err)
135 werr = err;
136 page_cache_release(page);
137 }
138 }
139 err = filemap_fdatawait(btree_inode->i_mapping);
140 if (err)
141 werr = err;
142 return werr;
79154b1b
CM
143}
144
145int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans,
146 struct btrfs_root *root)
147{
148 int ret;
149 u64 old_extent_block;
150 struct btrfs_fs_info *fs_info = root->fs_info;
151 struct btrfs_root *tree_root = fs_info->tree_root;
152 struct btrfs_root *extent_root = fs_info->extent_root;
79154b1b 153
9078a3e1 154 btrfs_write_dirty_block_groups(trans, extent_root);
79154b1b
CM
155 while(1) {
156 old_extent_block = btrfs_root_blocknr(&extent_root->root_item);
7eccb903 157 if (old_extent_block == bh_blocknr(extent_root->node))
79154b1b
CM
158 break;
159 btrfs_set_root_blocknr(&extent_root->root_item,
7eccb903 160 bh_blocknr(extent_root->node));
79154b1b
CM
161 ret = btrfs_update_root(trans, tree_root,
162 &extent_root->root_key,
163 &extent_root->root_item);
164 BUG_ON(ret);
9078a3e1 165 btrfs_write_dirty_block_groups(trans, extent_root);
79154b1b
CM
166 }
167 return 0;
168}
169
170static int wait_for_commit(struct btrfs_root *root,
171 struct btrfs_transaction *commit)
172{
173 DEFINE_WAIT(wait);
79154b1b
CM
174 while(!commit->commit_done) {
175 prepare_to_wait(&commit->commit_wait, &wait,
176 TASK_UNINTERRUPTIBLE);
177 if (commit->commit_done)
178 break;
179 mutex_unlock(&root->fs_info->trans_mutex);
180 schedule();
181 mutex_lock(&root->fs_info->trans_mutex);
182 }
183 finish_wait(&commit->commit_wait, &wait);
184 return 0;
185}
186
0f7d52f4
CM
187struct dirty_root {
188 struct list_head list;
189 struct btrfs_key snap_key;
190 struct buffer_head *commit_root;
191 struct btrfs_root *root;
192};
193
35b7e476
CM
194static int add_dirty_roots(struct btrfs_trans_handle *trans,
195 struct radix_tree_root *radix,
196 struct list_head *list)
0f7d52f4
CM
197{
198 struct dirty_root *dirty;
199 struct btrfs_root *gang[8];
200 struct btrfs_root *root;
201 int i;
202 int ret;
203 int err;
0f7d52f4
CM
204 while(1) {
205 ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0,
206 ARRAY_SIZE(gang),
207 BTRFS_ROOT_TRANS_TAG);
208 if (ret == 0)
209 break;
210 for (i = 0; i < ret; i++) {
211 root = gang[i];
2619ba1f
CM
212 radix_tree_tag_clear(radix,
213 (unsigned long)root->root_key.objectid,
214 BTRFS_ROOT_TRANS_TAG);
0f7d52f4 215 if (root->commit_root == root->node) {
7eccb903 216 WARN_ON(bh_blocknr(root->node) !=
0f7d52f4
CM
217 btrfs_root_blocknr(&root->root_item));
218 brelse(root->commit_root);
219 root->commit_root = NULL;
220 continue;
221 }
222 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
223 BUG_ON(!dirty);
224 memcpy(&dirty->snap_key, &root->root_key,
225 sizeof(root->root_key));
226 dirty->commit_root = root->commit_root;
227 root->commit_root = NULL;
228 dirty->root = root;
0f7d52f4
CM
229 root->root_key.offset = root->fs_info->generation;
230 btrfs_set_root_blocknr(&root->root_item,
7eccb903 231 bh_blocknr(root->node));
0f7d52f4
CM
232 err = btrfs_insert_root(trans, root->fs_info->tree_root,
233 &root->root_key,
234 &root->root_item);
235 BUG_ON(err);
236 list_add(&dirty->list, list);
237 }
238 }
0f7d52f4
CM
239 return 0;
240}
241
35b7e476
CM
242static int drop_dirty_roots(struct btrfs_root *tree_root,
243 struct list_head *list)
0f7d52f4
CM
244{
245 struct dirty_root *dirty;
246 struct btrfs_trans_handle *trans;
247 int ret;
0f7d52f4 248 while(!list_empty(list)) {
facda1e7 249 mutex_lock(&tree_root->fs_info->fs_mutex);
0f7d52f4
CM
250 dirty = list_entry(list->next, struct dirty_root, list);
251 list_del_init(&dirty->list);
252 trans = btrfs_start_transaction(tree_root, 1);
0f7d52f4
CM
253 ret = btrfs_drop_snapshot(trans, dirty->root,
254 dirty->commit_root);
255 BUG_ON(ret);
256
0f7d52f4
CM
257 ret = btrfs_del_root(trans, tree_root, &dirty->snap_key);
258 BUG_ON(ret);
259 ret = btrfs_end_transaction(trans, tree_root);
260 BUG_ON(ret);
261 kfree(dirty);
facda1e7 262 mutex_unlock(&tree_root->fs_info->fs_mutex);
0f7d52f4
CM
263 }
264 return 0;
265}
266
79154b1b
CM
267int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
268 struct btrfs_root *root)
269{
270 int ret = 0;
79154b1b 271 struct btrfs_transaction *cur_trans;
8fd17795 272 struct btrfs_transaction *prev_trans = NULL;
0f7d52f4 273 struct list_head dirty_fs_roots;
79154b1b
CM
274 DEFINE_WAIT(wait);
275
0f7d52f4 276 INIT_LIST_HEAD(&dirty_fs_roots);
d6e4a428 277
79154b1b
CM
278 mutex_lock(&root->fs_info->trans_mutex);
279 if (trans->transaction->in_commit) {
280 cur_trans = trans->transaction;
281 trans->transaction->use_count++;
282 btrfs_end_transaction(trans, root);
283 ret = wait_for_commit(root, cur_trans);
284 BUG_ON(ret);
285 put_transaction(cur_trans);
286 mutex_unlock(&root->fs_info->trans_mutex);
287 return 0;
288 }
2c90e5d6
CM
289 cur_trans = trans->transaction;
290 trans->transaction->in_commit = 1;
79154b1b 291 while (trans->transaction->num_writers > 1) {
2c90e5d6 292 WARN_ON(cur_trans != trans->transaction);
79154b1b
CM
293 prepare_to_wait(&trans->transaction->writer_wait, &wait,
294 TASK_UNINTERRUPTIBLE);
295 if (trans->transaction->num_writers <= 1)
296 break;
297 mutex_unlock(&root->fs_info->trans_mutex);
298 schedule();
299 mutex_lock(&root->fs_info->trans_mutex);
2c90e5d6 300 finish_wait(&trans->transaction->writer_wait, &wait);
79154b1b
CM
301 }
302 finish_wait(&trans->transaction->writer_wait, &wait);
2c90e5d6 303 WARN_ON(cur_trans != trans->transaction);
0f7d52f4 304 add_dirty_roots(trans, &root->fs_info->fs_roots_radix, &dirty_fs_roots);
79154b1b
CM
305 ret = btrfs_commit_tree_roots(trans, root);
306 BUG_ON(ret);
78fae27e
CM
307 cur_trans = root->fs_info->running_transaction;
308 root->fs_info->running_transaction = NULL;
8fd17795
CM
309 if (cur_trans->list.prev != &root->fs_info->trans_list) {
310 prev_trans = list_entry(cur_trans->list.prev,
311 struct btrfs_transaction, list);
312 if (prev_trans->commit_done)
313 prev_trans = NULL;
314 else
315 prev_trans->use_count++;
316 }
78fae27e 317 mutex_unlock(&root->fs_info->trans_mutex);
8fd17795 318 mutex_unlock(&root->fs_info->fs_mutex);
79154b1b 319 ret = btrfs_write_and_wait_transaction(trans, root);
8fd17795
CM
320 if (prev_trans) {
321 mutex_lock(&root->fs_info->trans_mutex);
322 wait_for_commit(root, prev_trans);
323 put_transaction(prev_trans);
324 mutex_unlock(&root->fs_info->trans_mutex);
325 }
326 btrfs_set_super_generation(root->fs_info->disk_super,
327 cur_trans->transid);
79154b1b 328 BUG_ON(ret);
79154b1b 329 write_ctree_super(trans, root);
8fd17795
CM
330
331 mutex_lock(&root->fs_info->fs_mutex);
78fae27e
CM
332 btrfs_finish_extent_commit(trans, root);
333 mutex_lock(&root->fs_info->trans_mutex);
2c90e5d6
CM
334 cur_trans->commit_done = 1;
335 wake_up(&cur_trans->commit_wait);
78fae27e 336 put_transaction(cur_trans);
79154b1b 337 put_transaction(cur_trans);
facda1e7
CM
338 if (root->fs_info->closing)
339 list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots);
340 else
341 list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots);
78fae27e 342 mutex_unlock(&root->fs_info->trans_mutex);
2c90e5d6 343 kmem_cache_free(btrfs_trans_handle_cachep, trans);
79154b1b 344
facda1e7
CM
345 if (root->fs_info->closing) {
346 mutex_unlock(&root->fs_info->fs_mutex);
347 drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);
348 mutex_lock(&root->fs_info->fs_mutex);
349 }
79154b1b
CM
350 return ret;
351}
352
08607c1b
CM
353void btrfs_transaction_cleaner(struct work_struct *work)
354{
355 struct btrfs_fs_info *fs_info = container_of(work,
356 struct btrfs_fs_info,
357 trans_work.work);
358
359 struct btrfs_root *root = fs_info->tree_root;
360 struct btrfs_transaction *cur;
361 struct btrfs_trans_handle *trans;
facda1e7 362 struct list_head dirty_roots;
08607c1b
CM
363 unsigned long now;
364 unsigned long delay = HZ * 30;
365 int ret;
366
facda1e7 367 INIT_LIST_HEAD(&dirty_roots);
08607c1b
CM
368 mutex_lock(&root->fs_info->fs_mutex);
369 mutex_lock(&root->fs_info->trans_mutex);
370 cur = root->fs_info->running_transaction;
371 if (!cur) {
372 mutex_unlock(&root->fs_info->trans_mutex);
373 goto out;
374 }
375 now = get_seconds();
376 if (now < cur->start_time || now - cur->start_time < 30) {
377 mutex_unlock(&root->fs_info->trans_mutex);
378 delay = HZ * 5;
379 goto out;
380 }
381 mutex_unlock(&root->fs_info->trans_mutex);
08607c1b
CM
382 trans = btrfs_start_transaction(root, 1);
383 ret = btrfs_commit_transaction(trans, root);
384out:
385 mutex_unlock(&root->fs_info->fs_mutex);
ad693af6
CM
386
387 mutex_lock(&root->fs_info->trans_mutex);
388 list_splice_init(&root->fs_info->dead_roots, &dirty_roots);
389 mutex_unlock(&root->fs_info->trans_mutex);
390
391 if (!list_empty(&dirty_roots)) {
392 drop_dirty_roots(root, &dirty_roots);
393 }
08607c1b
CM
394 btrfs_transaction_queue_work(root, delay);
395}
396
397void btrfs_transaction_queue_work(struct btrfs_root *root, int delay)
398{
399 queue_delayed_work(trans_wq, &root->fs_info->trans_work, delay);
400}
401
402void btrfs_transaction_flush_work(struct btrfs_root *root)
403{
404 cancel_rearming_delayed_workqueue(trans_wq, &root->fs_info->trans_work);
405 flush_workqueue(trans_wq);
406}
407
408void __init btrfs_init_transaction_sys(void)
409{
410 trans_wq = create_workqueue("btrfs");
411}
412
413void __exit btrfs_exit_transaction_sys(void)
414{
415 destroy_workqueue(trans_wq);
416}
417