Btrfs: [PATCH] extent_map: add writepage_end_io hook
[linux-2.6-block.git] / fs / btrfs / transaction.c
... / ...
CommitLineData
1/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
19#include <linux/fs.h>
20#include <linux/sched.h>
21#include "ctree.h"
22#include "disk-io.h"
23#include "transaction.h"
24
25static int total_trans = 0;
26extern struct kmem_cache *btrfs_trans_handle_cachep;
27extern struct kmem_cache *btrfs_transaction_cachep;
28
29static struct workqueue_struct *trans_wq;
30
31#define BTRFS_ROOT_TRANS_TAG 0
32#define BTRFS_ROOT_DEFRAG_TAG 1
33
34static void put_transaction(struct btrfs_transaction *transaction)
35{
36 WARN_ON(transaction->use_count == 0);
37 transaction->use_count--;
38 if (transaction->use_count == 0) {
39 WARN_ON(total_trans == 0);
40 total_trans--;
41 list_del_init(&transaction->list);
42 memset(transaction, 0, sizeof(*transaction));
43 kmem_cache_free(btrfs_transaction_cachep, transaction);
44 }
45}
46
47static int join_transaction(struct btrfs_root *root)
48{
49 struct btrfs_transaction *cur_trans;
50 cur_trans = root->fs_info->running_transaction;
51 if (!cur_trans) {
52 cur_trans = kmem_cache_alloc(btrfs_transaction_cachep,
53 GFP_NOFS);
54 total_trans++;
55 BUG_ON(!cur_trans);
56 root->fs_info->generation++;
57 root->fs_info->running_transaction = cur_trans;
58 cur_trans->num_writers = 1;
59 cur_trans->num_joined = 0;
60 cur_trans->transid = root->fs_info->generation;
61 init_waitqueue_head(&cur_trans->writer_wait);
62 init_waitqueue_head(&cur_trans->commit_wait);
63 cur_trans->in_commit = 0;
64 cur_trans->use_count = 1;
65 cur_trans->commit_done = 0;
66 cur_trans->start_time = get_seconds();
67 list_add_tail(&cur_trans->list, &root->fs_info->trans_list);
68 init_bit_radix(&cur_trans->dirty_pages);
69 } else {
70 cur_trans->num_writers++;
71 cur_trans->num_joined++;
72 }
73
74 return 0;
75}
76
77static int record_root_in_trans(struct btrfs_root *root)
78{
79 u64 running_trans_id = root->fs_info->running_transaction->transid;
80 if (root->ref_cows && root->last_trans < running_trans_id) {
81 WARN_ON(root == root->fs_info->extent_root);
82 if (root->root_item.refs != 0) {
83 radix_tree_tag_set(&root->fs_info->fs_roots_radix,
84 (unsigned long)root->root_key.objectid,
85 BTRFS_ROOT_TRANS_TAG);
86 radix_tree_tag_set(&root->fs_info->fs_roots_radix,
87 (unsigned long)root->root_key.objectid,
88 BTRFS_ROOT_DEFRAG_TAG);
89 root->commit_root = root->node;
90 get_bh(root->node);
91 } else {
92 WARN_ON(1);
93 }
94 root->last_trans = running_trans_id;
95 }
96 return 0;
97}
98
99struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
100 int num_blocks)
101{
102 struct btrfs_trans_handle *h =
103 kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
104 int ret;
105
106 mutex_lock(&root->fs_info->trans_mutex);
107 ret = join_transaction(root);
108 BUG_ON(ret);
109
110 record_root_in_trans(root);
111 h->transid = root->fs_info->running_transaction->transid;
112 h->transaction = root->fs_info->running_transaction;
113 h->blocks_reserved = num_blocks;
114 h->blocks_used = 0;
115 h->block_group = NULL;
116 h->alloc_exclude_nr = 0;
117 h->alloc_exclude_start = 0;
118 root->fs_info->running_transaction->use_count++;
119 mutex_unlock(&root->fs_info->trans_mutex);
120 return h;
121}
122
123int btrfs_end_transaction(struct btrfs_trans_handle *trans,
124 struct btrfs_root *root)
125{
126 struct btrfs_transaction *cur_trans;
127
128 mutex_lock(&root->fs_info->trans_mutex);
129 cur_trans = root->fs_info->running_transaction;
130 WARN_ON(cur_trans != trans->transaction);
131 WARN_ON(cur_trans->num_writers < 1);
132 cur_trans->num_writers--;
133 if (waitqueue_active(&cur_trans->writer_wait))
134 wake_up(&cur_trans->writer_wait);
135 put_transaction(cur_trans);
136 mutex_unlock(&root->fs_info->trans_mutex);
137 memset(trans, 0, sizeof(*trans));
138 kmem_cache_free(btrfs_trans_handle_cachep, trans);
139 return 0;
140}
141
142
143int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
144 struct btrfs_root *root)
145{
146 unsigned long gang[16];
147 int ret;
148 int i;
149 int err;
150 int werr = 0;
151 struct page *page;
152 struct radix_tree_root *dirty_pages;
153 struct inode *btree_inode = root->fs_info->btree_inode;
154
155 if (!trans || !trans->transaction) {
156 return filemap_write_and_wait(btree_inode->i_mapping);
157 }
158 dirty_pages = &trans->transaction->dirty_pages;
159 while(1) {
160 ret = find_first_radix_bit(dirty_pages, gang,
161 0, ARRAY_SIZE(gang));
162 if (!ret)
163 break;
164 for (i = 0; i < ret; i++) {
165 /* FIXME EIO */
166 clear_radix_bit(dirty_pages, gang[i]);
167 page = find_lock_page(btree_inode->i_mapping,
168 gang[i]);
169 if (!page)
170 continue;
171 if (PageWriteback(page)) {
172 if (PageDirty(page))
173 wait_on_page_writeback(page);
174 else {
175 unlock_page(page);
176 page_cache_release(page);
177 continue;
178 }
179 }
180 err = write_one_page(page, 0);
181 if (err)
182 werr = err;
183 page_cache_release(page);
184 }
185 }
186 err = filemap_fdatawait(btree_inode->i_mapping);
187 if (err)
188 werr = err;
189 return werr;
190}
191
192int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans,
193 struct btrfs_root *root)
194{
195 int ret;
196 u64 old_extent_block;
197 struct btrfs_fs_info *fs_info = root->fs_info;
198 struct btrfs_root *tree_root = fs_info->tree_root;
199 struct btrfs_root *extent_root = fs_info->extent_root;
200
201 btrfs_write_dirty_block_groups(trans, extent_root);
202 while(1) {
203 old_extent_block = btrfs_root_blocknr(&extent_root->root_item);
204 if (old_extent_block == bh_blocknr(extent_root->node))
205 break;
206 btrfs_set_root_blocknr(&extent_root->root_item,
207 bh_blocknr(extent_root->node));
208 ret = btrfs_update_root(trans, tree_root,
209 &extent_root->root_key,
210 &extent_root->root_item);
211 BUG_ON(ret);
212 btrfs_write_dirty_block_groups(trans, extent_root);
213 }
214 return 0;
215}
216
217static int wait_for_commit(struct btrfs_root *root,
218 struct btrfs_transaction *commit)
219{
220 DEFINE_WAIT(wait);
221 mutex_lock(&root->fs_info->trans_mutex);
222 while(!commit->commit_done) {
223 prepare_to_wait(&commit->commit_wait, &wait,
224 TASK_UNINTERRUPTIBLE);
225 if (commit->commit_done)
226 break;
227 mutex_unlock(&root->fs_info->trans_mutex);
228 schedule();
229 mutex_lock(&root->fs_info->trans_mutex);
230 }
231 mutex_unlock(&root->fs_info->trans_mutex);
232 finish_wait(&commit->commit_wait, &wait);
233 return 0;
234}
235
236struct dirty_root {
237 struct list_head list;
238 struct btrfs_root *root;
239 struct btrfs_root *latest_root;
240};
241
242int btrfs_add_dead_root(struct btrfs_root *root, struct list_head *dead_list)
243{
244 struct dirty_root *dirty;
245
246 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
247 if (!dirty)
248 return -ENOMEM;
249 dirty->root = root;
250 list_add(&dirty->list, dead_list);
251 return 0;
252}
253
254static int add_dirty_roots(struct btrfs_trans_handle *trans,
255 struct radix_tree_root *radix,
256 struct list_head *list)
257{
258 struct dirty_root *dirty;
259 struct btrfs_root *gang[8];
260 struct btrfs_root *root;
261 int i;
262 int ret;
263 int err = 0;
264 u32 refs;
265
266 while(1) {
267 ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0,
268 ARRAY_SIZE(gang),
269 BTRFS_ROOT_TRANS_TAG);
270 if (ret == 0)
271 break;
272 for (i = 0; i < ret; i++) {
273 root = gang[i];
274 radix_tree_tag_clear(radix,
275 (unsigned long)root->root_key.objectid,
276 BTRFS_ROOT_TRANS_TAG);
277 if (root->commit_root == root->node) {
278 WARN_ON(bh_blocknr(root->node) !=
279 btrfs_root_blocknr(&root->root_item));
280 brelse(root->commit_root);
281 root->commit_root = NULL;
282
283 /* make sure to update the root on disk
284 * so we get any updates to the block used
285 * counts
286 */
287 err = btrfs_update_root(trans,
288 root->fs_info->tree_root,
289 &root->root_key,
290 &root->root_item);
291 continue;
292 }
293 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
294 BUG_ON(!dirty);
295 dirty->root = kmalloc(sizeof(*dirty->root), GFP_NOFS);
296 BUG_ON(!dirty->root);
297
298 memset(&root->root_item.drop_progress, 0,
299 sizeof(struct btrfs_disk_key));
300 root->root_item.drop_level = 0;
301
302 memcpy(dirty->root, root, sizeof(*root));
303 dirty->root->node = root->commit_root;
304 dirty->latest_root = root;
305 root->commit_root = NULL;
306
307 root->root_key.offset = root->fs_info->generation;
308 btrfs_set_root_blocknr(&root->root_item,
309 bh_blocknr(root->node));
310 err = btrfs_insert_root(trans, root->fs_info->tree_root,
311 &root->root_key,
312 &root->root_item);
313 if (err)
314 break;
315
316 refs = btrfs_root_refs(&dirty->root->root_item);
317 btrfs_set_root_refs(&dirty->root->root_item, refs - 1);
318 err = btrfs_update_root(trans, root->fs_info->tree_root,
319 &dirty->root->root_key,
320 &dirty->root->root_item);
321
322 BUG_ON(err);
323 if (refs == 1) {
324 list_add(&dirty->list, list);
325 } else {
326 WARN_ON(1);
327 kfree(dirty->root);
328 kfree(dirty);
329 }
330 }
331 }
332 return err;
333}
334
335int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
336{
337 struct btrfs_fs_info *info = root->fs_info;
338 int ret;
339 struct btrfs_trans_handle *trans;
340
341 if (root->defrag_running)
342 return 0;
343
344 trans = btrfs_start_transaction(root, 1);
345 while (1) {
346 root->defrag_running = 1;
347 ret = btrfs_defrag_leaves(trans, root, cacheonly);
348 btrfs_end_transaction(trans, root);
349 mutex_unlock(&info->fs_mutex);
350
351 btrfs_btree_balance_dirty(root);
352 cond_resched();
353
354 mutex_lock(&info->fs_mutex);
355 trans = btrfs_start_transaction(root, 1);
356 if (ret != -EAGAIN)
357 break;
358 }
359 root->defrag_running = 0;
360 radix_tree_tag_clear(&info->fs_roots_radix,
361 (unsigned long)root->root_key.objectid,
362 BTRFS_ROOT_DEFRAG_TAG);
363 btrfs_end_transaction(trans, root);
364 return 0;
365}
366
367int btrfs_defrag_dirty_roots(struct btrfs_fs_info *info)
368{
369 struct btrfs_root *gang[1];
370 struct btrfs_root *root;
371 int i;
372 int ret;
373 int err = 0;
374 u64 last = 0;
375
376 while(1) {
377 ret = radix_tree_gang_lookup_tag(&info->fs_roots_radix,
378 (void **)gang, last,
379 ARRAY_SIZE(gang),
380 BTRFS_ROOT_DEFRAG_TAG);
381 if (ret == 0)
382 break;
383 for (i = 0; i < ret; i++) {
384 root = gang[i];
385 last = root->root_key.objectid + 1;
386 btrfs_defrag_root(root, 1);
387 }
388 }
389 btrfs_defrag_root(info->extent_root, 1);
390 return err;
391}
392
393static int drop_dirty_roots(struct btrfs_root *tree_root,
394 struct list_head *list)
395{
396 struct dirty_root *dirty;
397 struct btrfs_trans_handle *trans;
398 u64 num_blocks;
399 u64 blocks_used;
400 int ret = 0;
401 int err;
402
403 while(!list_empty(list)) {
404 struct btrfs_root *root;
405
406 mutex_lock(&tree_root->fs_info->fs_mutex);
407 dirty = list_entry(list->next, struct dirty_root, list);
408 list_del_init(&dirty->list);
409
410 num_blocks = btrfs_root_blocks_used(&dirty->root->root_item);
411 root = dirty->latest_root;
412
413 while(1) {
414 trans = btrfs_start_transaction(tree_root, 1);
415
416 ret = btrfs_drop_snapshot(trans, dirty->root);
417 if (ret != -EAGAIN) {
418 break;
419 }
420
421 err = btrfs_update_root(trans,
422 tree_root,
423 &dirty->root->root_key,
424 &dirty->root->root_item);
425 if (err)
426 ret = err;
427 ret = btrfs_end_transaction(trans, tree_root);
428 BUG_ON(ret);
429 mutex_unlock(&tree_root->fs_info->fs_mutex);
430
431 btrfs_btree_balance_dirty(tree_root);
432 schedule();
433
434 mutex_lock(&tree_root->fs_info->fs_mutex);
435 }
436 BUG_ON(ret);
437
438 num_blocks -= btrfs_root_blocks_used(&dirty->root->root_item);
439 blocks_used = btrfs_root_blocks_used(&root->root_item);
440 if (num_blocks) {
441 record_root_in_trans(root);
442 btrfs_set_root_blocks_used(&root->root_item,
443 blocks_used - num_blocks);
444 }
445 ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key);
446 if (ret) {
447 BUG();
448 break;
449 }
450 ret = btrfs_end_transaction(trans, tree_root);
451 BUG_ON(ret);
452
453 kfree(dirty->root);
454 kfree(dirty);
455 mutex_unlock(&tree_root->fs_info->fs_mutex);
456 btrfs_btree_balance_dirty(tree_root);
457 schedule();
458 }
459 return ret;
460}
461
462int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
463 struct btrfs_root *root)
464{
465 unsigned long joined = 0;
466 unsigned long timeout = 1;
467 struct btrfs_transaction *cur_trans;
468 struct btrfs_transaction *prev_trans = NULL;
469 struct list_head dirty_fs_roots;
470 struct radix_tree_root pinned_copy;
471 DEFINE_WAIT(wait);
472 int ret;
473
474 init_bit_radix(&pinned_copy);
475 INIT_LIST_HEAD(&dirty_fs_roots);
476
477 mutex_lock(&root->fs_info->trans_mutex);
478 if (trans->transaction->in_commit) {
479 cur_trans = trans->transaction;
480 trans->transaction->use_count++;
481 mutex_unlock(&root->fs_info->trans_mutex);
482 btrfs_end_transaction(trans, root);
483
484 mutex_unlock(&root->fs_info->fs_mutex);
485 ret = wait_for_commit(root, cur_trans);
486 BUG_ON(ret);
487
488 mutex_lock(&root->fs_info->trans_mutex);
489 put_transaction(cur_trans);
490 mutex_unlock(&root->fs_info->trans_mutex);
491
492 mutex_lock(&root->fs_info->fs_mutex);
493 return 0;
494 }
495 trans->transaction->in_commit = 1;
496 cur_trans = trans->transaction;
497 if (cur_trans->list.prev != &root->fs_info->trans_list) {
498 prev_trans = list_entry(cur_trans->list.prev,
499 struct btrfs_transaction, list);
500 if (!prev_trans->commit_done) {
501 prev_trans->use_count++;
502 mutex_unlock(&root->fs_info->fs_mutex);
503 mutex_unlock(&root->fs_info->trans_mutex);
504
505 wait_for_commit(root, prev_trans);
506
507 mutex_lock(&root->fs_info->fs_mutex);
508 mutex_lock(&root->fs_info->trans_mutex);
509 put_transaction(prev_trans);
510 }
511 }
512
513 do {
514 joined = cur_trans->num_joined;
515 WARN_ON(cur_trans != trans->transaction);
516 prepare_to_wait(&cur_trans->writer_wait, &wait,
517 TASK_UNINTERRUPTIBLE);
518
519 if (cur_trans->num_writers > 1)
520 timeout = MAX_SCHEDULE_TIMEOUT;
521 else
522 timeout = 1;
523
524 mutex_unlock(&root->fs_info->fs_mutex);
525 mutex_unlock(&root->fs_info->trans_mutex);
526
527 schedule_timeout(timeout);
528
529 mutex_lock(&root->fs_info->fs_mutex);
530 mutex_lock(&root->fs_info->trans_mutex);
531 finish_wait(&cur_trans->writer_wait, &wait);
532 } while (cur_trans->num_writers > 1 ||
533 (cur_trans->num_joined != joined));
534
535 WARN_ON(cur_trans != trans->transaction);
536 ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix,
537 &dirty_fs_roots);
538 BUG_ON(ret);
539
540 ret = btrfs_commit_tree_roots(trans, root);
541 BUG_ON(ret);
542
543 cur_trans = root->fs_info->running_transaction;
544 root->fs_info->running_transaction = NULL;
545 btrfs_set_super_generation(&root->fs_info->super_copy,
546 cur_trans->transid);
547 btrfs_set_super_root(&root->fs_info->super_copy,
548 bh_blocknr(root->fs_info->tree_root->node));
549 memcpy(root->fs_info->disk_super, &root->fs_info->super_copy,
550 sizeof(root->fs_info->super_copy));
551
552 btrfs_copy_pinned(root, &pinned_copy);
553
554 mutex_unlock(&root->fs_info->trans_mutex);
555 mutex_unlock(&root->fs_info->fs_mutex);
556 ret = btrfs_write_and_wait_transaction(trans, root);
557 BUG_ON(ret);
558 write_ctree_super(trans, root);
559 mutex_lock(&root->fs_info->fs_mutex);
560 btrfs_finish_extent_commit(trans, root, &pinned_copy);
561 mutex_lock(&root->fs_info->trans_mutex);
562 cur_trans->commit_done = 1;
563 root->fs_info->last_trans_committed = cur_trans->transid;
564 wake_up(&cur_trans->commit_wait);
565 put_transaction(cur_trans);
566 put_transaction(cur_trans);
567
568 if (root->fs_info->closing)
569 list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots);
570 else
571 list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots);
572
573 mutex_unlock(&root->fs_info->trans_mutex);
574 kmem_cache_free(btrfs_trans_handle_cachep, trans);
575
576 if (root->fs_info->closing) {
577 mutex_unlock(&root->fs_info->fs_mutex);
578 drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);
579 mutex_lock(&root->fs_info->fs_mutex);
580 }
581 return ret;
582}
583
584int btrfs_clean_old_snapshots(struct btrfs_root *root)
585{
586 struct list_head dirty_roots;
587 INIT_LIST_HEAD(&dirty_roots);
588
589 mutex_lock(&root->fs_info->trans_mutex);
590 list_splice_init(&root->fs_info->dead_roots, &dirty_roots);
591 mutex_unlock(&root->fs_info->trans_mutex);
592
593 if (!list_empty(&dirty_roots)) {
594 drop_dirty_roots(root, &dirty_roots);
595 }
596 return 0;
597}
598void btrfs_transaction_cleaner(struct work_struct *work)
599{
600 struct btrfs_fs_info *fs_info = container_of(work,
601 struct btrfs_fs_info,
602 trans_work.work);
603
604 struct btrfs_root *root = fs_info->tree_root;
605 struct btrfs_transaction *cur;
606 struct btrfs_trans_handle *trans;
607 unsigned long now;
608 unsigned long delay = HZ * 30;
609 int ret;
610
611 mutex_lock(&root->fs_info->fs_mutex);
612 mutex_lock(&root->fs_info->trans_mutex);
613 cur = root->fs_info->running_transaction;
614 if (!cur) {
615 mutex_unlock(&root->fs_info->trans_mutex);
616 goto out;
617 }
618 now = get_seconds();
619 if (now < cur->start_time || now - cur->start_time < 30) {
620 mutex_unlock(&root->fs_info->trans_mutex);
621 delay = HZ * 5;
622 goto out;
623 }
624 mutex_unlock(&root->fs_info->trans_mutex);
625 btrfs_defrag_dirty_roots(root->fs_info);
626 trans = btrfs_start_transaction(root, 1);
627 ret = btrfs_commit_transaction(trans, root);
628out:
629 mutex_unlock(&root->fs_info->fs_mutex);
630 btrfs_clean_old_snapshots(root);
631 btrfs_transaction_queue_work(root, delay);
632}
633
634void btrfs_transaction_queue_work(struct btrfs_root *root, int delay)
635{
636 queue_delayed_work(trans_wq, &root->fs_info->trans_work, delay);
637}
638
639void btrfs_transaction_flush_work(struct btrfs_root *root)
640{
641 cancel_rearming_delayed_workqueue(trans_wq, &root->fs_info->trans_work);
642 flush_workqueue(trans_wq);
643}
644
645void __init btrfs_init_transaction_sys(void)
646{
647 trans_wq = create_workqueue("btrfs");
648}
649
650void __exit btrfs_exit_transaction_sys(void)
651{
652 destroy_workqueue(trans_wq);
653}
654