blob: e64ecec3f52eab3773f4b276ec29059e6774151d [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
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
Chris Mason79154b12007-03-22 15:59:16 -040019#include <linux/fs.h>
Chris Mason34088782007-06-12 11:36:58 -040020#include <linux/sched.h>
Chris Mason79154b12007-03-22 15:59:16 -040021#include "ctree.h"
22#include "disk-io.h"
23#include "transaction.h"
24
Chris Mason78fae272007-03-25 11:35:08 -040025static int total_trans = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -040026extern struct kmem_cache *btrfs_trans_handle_cachep;
27extern struct kmem_cache *btrfs_transaction_cachep;
28
Chris Mason08607c12007-06-08 15:33:54 -040029static struct workqueue_struct *trans_wq;
30
Chris Mason0f7d52f2007-04-09 10:42:37 -040031#define BTRFS_ROOT_TRANS_TAG 0
Chris Mason6702ed42007-08-07 16:15:09 -040032#define BTRFS_ROOT_DEFRAG_TAG 1
Chris Mason0f7d52f2007-04-09 10:42:37 -040033
Chris Mason79154b12007-03-22 15:59:16 -040034static void put_transaction(struct btrfs_transaction *transaction)
35{
Chris Mason2c90e5d2007-04-02 10:50:19 -040036 WARN_ON(transaction->use_count == 0);
Chris Mason79154b12007-03-22 15:59:16 -040037 transaction->use_count--;
Chris Mason78fae272007-03-25 11:35:08 -040038 if (transaction->use_count == 0) {
39 WARN_ON(total_trans == 0);
40 total_trans--;
Chris Mason8fd17792007-04-19 21:01:03 -040041 list_del_init(&transaction->list);
Chris Mason2c90e5d2007-04-02 10:50:19 -040042 memset(transaction, 0, sizeof(*transaction));
43 kmem_cache_free(btrfs_transaction_cachep, transaction);
Chris Mason78fae272007-03-25 11:35:08 -040044 }
Chris Mason79154b12007-03-22 15:59:16 -040045}
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) {
Chris Mason2c90e5d2007-04-02 10:50:19 -040052 cur_trans = kmem_cache_alloc(btrfs_transaction_cachep,
53 GFP_NOFS);
Chris Mason78fae272007-03-25 11:35:08 -040054 total_trans++;
Chris Mason79154b12007-03-22 15:59:16 -040055 BUG_ON(!cur_trans);
Chris Mason0f7d52f2007-04-09 10:42:37 -040056 root->fs_info->generation++;
Chris Mason79154b12007-03-22 15:59:16 -040057 root->fs_info->running_transaction = cur_trans;
58 cur_trans->num_writers = 0;
Chris Mason0f7d52f2007-04-09 10:42:37 -040059 cur_trans->transid = root->fs_info->generation;
Chris Mason79154b12007-03-22 15:59:16 -040060 init_waitqueue_head(&cur_trans->writer_wait);
61 init_waitqueue_head(&cur_trans->commit_wait);
62 cur_trans->in_commit = 0;
Chris Masond5719762007-03-23 10:01:08 -040063 cur_trans->use_count = 1;
Chris Mason79154b12007-03-22 15:59:16 -040064 cur_trans->commit_done = 0;
Chris Mason08607c12007-06-08 15:33:54 -040065 cur_trans->start_time = get_seconds();
Chris Mason8fd17792007-04-19 21:01:03 -040066 list_add_tail(&cur_trans->list, &root->fs_info->trans_list);
Chris Mason7c4452b2007-04-28 09:29:35 -040067 init_bit_radix(&cur_trans->dirty_pages);
Chris Mason79154b12007-03-22 15:59:16 -040068 }
69 cur_trans->num_writers++;
70 return 0;
71}
72
Chris Mason6702ed42007-08-07 16:15:09 -040073static int record_root_in_trans(struct btrfs_root *root)
74{
75 u64 running_trans_id = root->fs_info->running_transaction->transid;
76 if (root->ref_cows && root->last_trans < running_trans_id) {
77 WARN_ON(root == root->fs_info->extent_root);
78 if (root->root_item.refs != 0) {
79 radix_tree_tag_set(&root->fs_info->fs_roots_radix,
80 (unsigned long)root->root_key.objectid,
81 BTRFS_ROOT_TRANS_TAG);
82 radix_tree_tag_set(&root->fs_info->fs_roots_radix,
83 (unsigned long)root->root_key.objectid,
84 BTRFS_ROOT_DEFRAG_TAG);
85 root->commit_root = root->node;
86 get_bh(root->node);
87 } else {
88 WARN_ON(1);
89 }
90 root->last_trans = running_trans_id;
91 }
92 return 0;
93}
94
Chris Mason79154b12007-03-22 15:59:16 -040095struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
96 int num_blocks)
97{
Chris Mason2c90e5d2007-04-02 10:50:19 -040098 struct btrfs_trans_handle *h =
99 kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
Chris Mason79154b12007-03-22 15:59:16 -0400100 int ret;
101
102 mutex_lock(&root->fs_info->trans_mutex);
103 ret = join_transaction(root);
104 BUG_ON(ret);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400105
Chris Mason6702ed42007-08-07 16:15:09 -0400106 record_root_in_trans(root);
107 h->transid = root->fs_info->running_transaction->transid;
Chris Mason79154b12007-03-22 15:59:16 -0400108 h->transaction = root->fs_info->running_transaction;
109 h->blocks_reserved = num_blocks;
110 h->blocks_used = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400111 h->block_group = NULL;
Chris Mason26b80032007-08-08 20:17:12 -0400112 h->alloc_exclude_nr = 0;
113 h->alloc_exclude_start = 0;
Chris Mason79154b12007-03-22 15:59:16 -0400114 root->fs_info->running_transaction->use_count++;
115 mutex_unlock(&root->fs_info->trans_mutex);
116 return h;
117}
118
119int btrfs_end_transaction(struct btrfs_trans_handle *trans,
120 struct btrfs_root *root)
121{
122 struct btrfs_transaction *cur_trans;
Chris Masond6e4a422007-04-06 15:37:36 -0400123
Chris Mason79154b12007-03-22 15:59:16 -0400124 mutex_lock(&root->fs_info->trans_mutex);
125 cur_trans = root->fs_info->running_transaction;
Chris Masonccd467d2007-06-28 15:57:36 -0400126 WARN_ON(cur_trans != trans->transaction);
Chris Masond5719762007-03-23 10:01:08 -0400127 WARN_ON(cur_trans->num_writers < 1);
Chris Masonccd467d2007-06-28 15:57:36 -0400128 cur_trans->num_writers--;
Chris Mason79154b12007-03-22 15:59:16 -0400129 if (waitqueue_active(&cur_trans->writer_wait))
130 wake_up(&cur_trans->writer_wait);
Chris Mason79154b12007-03-22 15:59:16 -0400131 put_transaction(cur_trans);
132 mutex_unlock(&root->fs_info->trans_mutex);
Chris Masond6025572007-03-30 14:27:56 -0400133 memset(trans, 0, sizeof(*trans));
Chris Mason2c90e5d2007-04-02 10:50:19 -0400134 kmem_cache_free(btrfs_trans_handle_cachep, trans);
Chris Mason79154b12007-03-22 15:59:16 -0400135 return 0;
136}
137
138
139int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
140 struct btrfs_root *root)
141{
Chris Mason7c4452b2007-04-28 09:29:35 -0400142 unsigned long gang[16];
143 int ret;
144 int i;
145 int err;
146 int werr = 0;
147 struct page *page;
148 struct radix_tree_root *dirty_pages;
149 struct inode *btree_inode = root->fs_info->btree_inode;
150
151 if (!trans || !trans->transaction) {
152 return filemap_write_and_wait(btree_inode->i_mapping);
153 }
154 dirty_pages = &trans->transaction->dirty_pages;
155 while(1) {
Chris Masone37c9e62007-05-09 20:13:14 -0400156 ret = find_first_radix_bit(dirty_pages, gang,
157 0, ARRAY_SIZE(gang));
Chris Mason7c4452b2007-04-28 09:29:35 -0400158 if (!ret)
159 break;
160 for (i = 0; i < ret; i++) {
161 /* FIXME EIO */
162 clear_radix_bit(dirty_pages, gang[i]);
163 page = find_lock_page(btree_inode->i_mapping,
164 gang[i]);
165 if (!page)
166 continue;
Chris Mason6702ed42007-08-07 16:15:09 -0400167 if (PageWriteback(page)) {
168 if (PageDirty(page))
169 wait_on_page_writeback(page);
170 else {
171 unlock_page(page);
172 page_cache_release(page);
173 continue;
174 }
175 }
Chris Mason7c4452b2007-04-28 09:29:35 -0400176 err = write_one_page(page, 0);
177 if (err)
178 werr = err;
179 page_cache_release(page);
180 }
181 }
182 err = filemap_fdatawait(btree_inode->i_mapping);
183 if (err)
184 werr = err;
185 return werr;
Chris Mason79154b12007-03-22 15:59:16 -0400186}
187
188int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans,
189 struct btrfs_root *root)
190{
191 int ret;
192 u64 old_extent_block;
193 struct btrfs_fs_info *fs_info = root->fs_info;
194 struct btrfs_root *tree_root = fs_info->tree_root;
195 struct btrfs_root *extent_root = fs_info->extent_root;
Chris Mason79154b12007-03-22 15:59:16 -0400196
Chris Mason9078a3e2007-04-26 16:46:15 -0400197 btrfs_write_dirty_block_groups(trans, extent_root);
Chris Mason79154b12007-03-22 15:59:16 -0400198 while(1) {
199 old_extent_block = btrfs_root_blocknr(&extent_root->root_item);
Chris Mason7eccb902007-04-11 15:53:25 -0400200 if (old_extent_block == bh_blocknr(extent_root->node))
Chris Mason79154b12007-03-22 15:59:16 -0400201 break;
202 btrfs_set_root_blocknr(&extent_root->root_item,
Chris Mason7eccb902007-04-11 15:53:25 -0400203 bh_blocknr(extent_root->node));
Chris Mason79154b12007-03-22 15:59:16 -0400204 ret = btrfs_update_root(trans, tree_root,
205 &extent_root->root_key,
206 &extent_root->root_item);
207 BUG_ON(ret);
Chris Mason9078a3e2007-04-26 16:46:15 -0400208 btrfs_write_dirty_block_groups(trans, extent_root);
Chris Mason79154b12007-03-22 15:59:16 -0400209 }
210 return 0;
211}
212
213static int wait_for_commit(struct btrfs_root *root,
214 struct btrfs_transaction *commit)
215{
216 DEFINE_WAIT(wait);
Chris Masonccd467d2007-06-28 15:57:36 -0400217 mutex_lock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400218 while(!commit->commit_done) {
219 prepare_to_wait(&commit->commit_wait, &wait,
220 TASK_UNINTERRUPTIBLE);
221 if (commit->commit_done)
222 break;
223 mutex_unlock(&root->fs_info->trans_mutex);
224 schedule();
225 mutex_lock(&root->fs_info->trans_mutex);
226 }
Chris Masonccd467d2007-06-28 15:57:36 -0400227 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400228 finish_wait(&commit->commit_wait, &wait);
229 return 0;
230}
231
Chris Mason0f7d52f2007-04-09 10:42:37 -0400232struct dirty_root {
233 struct list_head list;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400234 struct btrfs_root *root;
235};
236
Chris Mason5eda7b52007-06-22 14:16:25 -0400237int btrfs_add_dead_root(struct btrfs_root *root, struct list_head *dead_list)
238{
239 struct dirty_root *dirty;
240
241 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
242 if (!dirty)
243 return -ENOMEM;
Chris Mason5eda7b52007-06-22 14:16:25 -0400244 dirty->root = root;
Chris Mason5eda7b52007-06-22 14:16:25 -0400245 list_add(&dirty->list, dead_list);
246 return 0;
247}
248
Chris Mason35b7e472007-05-02 15:53:43 -0400249static int add_dirty_roots(struct btrfs_trans_handle *trans,
250 struct radix_tree_root *radix,
251 struct list_head *list)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400252{
253 struct dirty_root *dirty;
254 struct btrfs_root *gang[8];
255 struct btrfs_root *root;
256 int i;
257 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400258 int err = 0;
Chris Mason5eda7b52007-06-22 14:16:25 -0400259 u32 refs;
Chris Mason54aa1f42007-06-22 14:16:25 -0400260
Chris Mason0f7d52f2007-04-09 10:42:37 -0400261 while(1) {
262 ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0,
263 ARRAY_SIZE(gang),
264 BTRFS_ROOT_TRANS_TAG);
265 if (ret == 0)
266 break;
267 for (i = 0; i < ret; i++) {
268 root = gang[i];
Chris Mason2619ba12007-04-10 16:58:11 -0400269 radix_tree_tag_clear(radix,
270 (unsigned long)root->root_key.objectid,
271 BTRFS_ROOT_TRANS_TAG);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400272 if (root->commit_root == root->node) {
Chris Mason7eccb902007-04-11 15:53:25 -0400273 WARN_ON(bh_blocknr(root->node) !=
Chris Mason0f7d52f2007-04-09 10:42:37 -0400274 btrfs_root_blocknr(&root->root_item));
275 brelse(root->commit_root);
276 root->commit_root = NULL;
277 continue;
278 }
279 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
280 BUG_ON(!dirty);
Chris Mason9f3a7422007-08-07 15:52:19 -0400281 dirty->root = kmalloc(sizeof(*dirty->root), GFP_NOFS);
282 BUG_ON(!dirty->root);
283
284 memset(&root->root_item.drop_progress, 0,
285 sizeof(struct btrfs_disk_key));
286 root->root_item.drop_level = 0;
287
288 memcpy(dirty->root, root, sizeof(*root));
289 dirty->root->node = root->commit_root;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400290 root->commit_root = NULL;
Chris Mason5eda7b52007-06-22 14:16:25 -0400291
Chris Mason0f7d52f2007-04-09 10:42:37 -0400292 root->root_key.offset = root->fs_info->generation;
293 btrfs_set_root_blocknr(&root->root_item,
Chris Mason7eccb902007-04-11 15:53:25 -0400294 bh_blocknr(root->node));
Chris Mason0f7d52f2007-04-09 10:42:37 -0400295 err = btrfs_insert_root(trans, root->fs_info->tree_root,
296 &root->root_key,
297 &root->root_item);
Chris Mason54aa1f42007-06-22 14:16:25 -0400298 if (err)
299 break;
Chris Mason9f3a7422007-08-07 15:52:19 -0400300
301 refs = btrfs_root_refs(&dirty->root->root_item);
302 btrfs_set_root_refs(&dirty->root->root_item, refs - 1);
Chris Mason5eda7b52007-06-22 14:16:25 -0400303 err = btrfs_update_root(trans, root->fs_info->tree_root,
Chris Mason9f3a7422007-08-07 15:52:19 -0400304 &dirty->root->root_key,
305 &dirty->root->root_item);
Chris Mason5eda7b52007-06-22 14:16:25 -0400306
307 BUG_ON(err);
Chris Mason9f3a7422007-08-07 15:52:19 -0400308 if (refs == 1) {
Chris Mason5eda7b52007-06-22 14:16:25 -0400309 list_add(&dirty->list, list);
Chris Mason9f3a7422007-08-07 15:52:19 -0400310 } else {
311 WARN_ON(1);
312 kfree(dirty->root);
Chris Mason5eda7b52007-06-22 14:16:25 -0400313 kfree(dirty);
Chris Mason9f3a7422007-08-07 15:52:19 -0400314 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400315 }
316 }
Chris Mason54aa1f42007-06-22 14:16:25 -0400317 return err;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400318}
319
Chris Mason6702ed42007-08-07 16:15:09 -0400320int btrfs_defrag_dirty_roots(struct btrfs_fs_info *info)
321{
322 struct btrfs_root *gang[1];
323 struct btrfs_root *root;
324 struct btrfs_root *tree_root = info->tree_root;
325 struct btrfs_trans_handle *trans;
326 int i;
327 int ret;
328 int err = 0;
329 u64 last = 0;
330
331 trans = btrfs_start_transaction(tree_root, 1);
332 while(1) {
333 ret = radix_tree_gang_lookup_tag(&info->fs_roots_radix,
334 (void **)gang, last,
335 ARRAY_SIZE(gang),
336 BTRFS_ROOT_DEFRAG_TAG);
337 if (ret == 0)
338 break;
339 for (i = 0; i < ret; i++) {
340 root = gang[i];
341 last = root->root_key.objectid + 1;
342 radix_tree_tag_clear(&info->fs_roots_radix,
343 (unsigned long)root->root_key.objectid,
344 BTRFS_ROOT_DEFRAG_TAG);
345 if (root->defrag_running)
346 continue;
347
348 while (1) {
349 mutex_lock(&root->fs_info->trans_mutex);
350 record_root_in_trans(root);
351 mutex_unlock(&root->fs_info->trans_mutex);
352
353 root->defrag_running = 1;
354 err = btrfs_defrag_leaves(trans, root, 1);
355 btrfs_end_transaction(trans, tree_root);
356 mutex_unlock(&info->fs_mutex);
357
358 btrfs_btree_balance_dirty(root);
Chris Masonf4468e92007-08-08 10:08:58 -0400359 cond_resched();
Chris Mason6702ed42007-08-07 16:15:09 -0400360
361 mutex_lock(&info->fs_mutex);
362 trans = btrfs_start_transaction(tree_root, 1);
363 if (err != -EAGAIN)
364 break;
365 }
366 root->defrag_running = 0;
367 }
368 }
369 btrfs_end_transaction(trans, tree_root);
370 return err;
371}
372
Chris Mason35b7e472007-05-02 15:53:43 -0400373static int drop_dirty_roots(struct btrfs_root *tree_root,
374 struct list_head *list)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400375{
376 struct dirty_root *dirty;
377 struct btrfs_trans_handle *trans;
Chris Mason54aa1f42007-06-22 14:16:25 -0400378 int ret = 0;
Chris Mason9f3a7422007-08-07 15:52:19 -0400379 int err;
380
Chris Mason0f7d52f2007-04-09 10:42:37 -0400381 while(!list_empty(list)) {
Chris Masonfacda1e2007-06-08 18:11:48 -0400382 mutex_lock(&tree_root->fs_info->fs_mutex);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400383 dirty = list_entry(list->next, struct dirty_root, list);
384 list_del_init(&dirty->list);
Chris Mason5eda7b52007-06-22 14:16:25 -0400385
Chris Mason9f3a7422007-08-07 15:52:19 -0400386 while(1) {
387 trans = btrfs_start_transaction(tree_root, 1);
388 ret = btrfs_drop_snapshot(trans, dirty->root);
389 if (ret != -EAGAIN) {
390 break;
391 }
392 err = btrfs_update_root(trans,
393 tree_root,
394 &dirty->root->root_key,
395 &dirty->root->root_item);
396 if (err)
397 ret = err;
398 ret = btrfs_end_transaction(trans, tree_root);
399 BUG_ON(ret);
Chris Masonf4468e92007-08-08 10:08:58 -0400400 mutex_unlock(&tree_root->fs_info->fs_mutex);
401
402 btrfs_btree_balance_dirty(tree_root);
403 schedule();
404
405 mutex_lock(&tree_root->fs_info->fs_mutex);
Chris Mason9f3a7422007-08-07 15:52:19 -0400406 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400407 BUG_ON(ret);
Chris Mason9f3a7422007-08-07 15:52:19 -0400408 ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key);
Chris Mason54aa1f42007-06-22 14:16:25 -0400409 if (ret)
410 break;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400411 ret = btrfs_end_transaction(trans, tree_root);
412 BUG_ON(ret);
Chris Mason5eda7b52007-06-22 14:16:25 -0400413
Chris Mason9f3a7422007-08-07 15:52:19 -0400414 kfree(dirty->root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400415 kfree(dirty);
Chris Masonfacda1e2007-06-08 18:11:48 -0400416 mutex_unlock(&tree_root->fs_info->fs_mutex);
Chris Mason8c2383c2007-06-18 09:57:58 -0400417 btrfs_btree_balance_dirty(tree_root);
Chris Masonf4468e92007-08-08 10:08:58 -0400418 schedule();
Chris Mason0f7d52f2007-04-09 10:42:37 -0400419 }
Chris Mason54aa1f42007-06-22 14:16:25 -0400420 return ret;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400421}
422
Chris Mason79154b12007-03-22 15:59:16 -0400423int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
424 struct btrfs_root *root)
425{
426 int ret = 0;
Chris Mason79154b12007-03-22 15:59:16 -0400427 struct btrfs_transaction *cur_trans;
Chris Mason8fd17792007-04-19 21:01:03 -0400428 struct btrfs_transaction *prev_trans = NULL;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400429 struct list_head dirty_fs_roots;
Chris Masonccd467d2007-06-28 15:57:36 -0400430 struct radix_tree_root pinned_copy;
Chris Mason79154b12007-03-22 15:59:16 -0400431 DEFINE_WAIT(wait);
432
Chris Masonccd467d2007-06-28 15:57:36 -0400433 init_bit_radix(&pinned_copy);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400434 INIT_LIST_HEAD(&dirty_fs_roots);
Chris Masond6e4a422007-04-06 15:37:36 -0400435
Chris Mason79154b12007-03-22 15:59:16 -0400436 mutex_lock(&root->fs_info->trans_mutex);
437 if (trans->transaction->in_commit) {
438 cur_trans = trans->transaction;
439 trans->transaction->use_count++;
Chris Masonccd467d2007-06-28 15:57:36 -0400440 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400441 btrfs_end_transaction(trans, root);
Chris Masonccd467d2007-06-28 15:57:36 -0400442
443 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400444 ret = wait_for_commit(root, cur_trans);
445 BUG_ON(ret);
446 put_transaction(cur_trans);
Chris Masonccd467d2007-06-28 15:57:36 -0400447 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400448 return 0;
449 }
Chris Mason2c90e5d2007-04-02 10:50:19 -0400450 trans->transaction->in_commit = 1;
Chris Masonccd467d2007-06-28 15:57:36 -0400451 cur_trans = trans->transaction;
452 if (cur_trans->list.prev != &root->fs_info->trans_list) {
453 prev_trans = list_entry(cur_trans->list.prev,
454 struct btrfs_transaction, list);
455 if (!prev_trans->commit_done) {
456 prev_trans->use_count++;
457 mutex_unlock(&root->fs_info->fs_mutex);
458 mutex_unlock(&root->fs_info->trans_mutex);
459
460 wait_for_commit(root, prev_trans);
461 put_transaction(prev_trans);
462
463 mutex_lock(&root->fs_info->fs_mutex);
464 mutex_lock(&root->fs_info->trans_mutex);
465 }
466 }
Chris Mason79154b12007-03-22 15:59:16 -0400467 while (trans->transaction->num_writers > 1) {
Chris Mason2c90e5d2007-04-02 10:50:19 -0400468 WARN_ON(cur_trans != trans->transaction);
Chris Mason79154b12007-03-22 15:59:16 -0400469 prepare_to_wait(&trans->transaction->writer_wait, &wait,
470 TASK_UNINTERRUPTIBLE);
471 if (trans->transaction->num_writers <= 1)
472 break;
Chris Masonccd467d2007-06-28 15:57:36 -0400473 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400474 mutex_unlock(&root->fs_info->trans_mutex);
475 schedule();
Chris Masonccd467d2007-06-28 15:57:36 -0400476 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400477 mutex_lock(&root->fs_info->trans_mutex);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400478 finish_wait(&trans->transaction->writer_wait, &wait);
Chris Mason79154b12007-03-22 15:59:16 -0400479 }
480 finish_wait(&trans->transaction->writer_wait, &wait);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400481 WARN_ON(cur_trans != trans->transaction);
Chris Mason54aa1f42007-06-22 14:16:25 -0400482 ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix,
483 &dirty_fs_roots);
484 BUG_ON(ret);
485
Chris Mason79154b12007-03-22 15:59:16 -0400486 ret = btrfs_commit_tree_roots(trans, root);
487 BUG_ON(ret);
Chris Mason54aa1f42007-06-22 14:16:25 -0400488
Chris Mason78fae272007-03-25 11:35:08 -0400489 cur_trans = root->fs_info->running_transaction;
490 root->fs_info->running_transaction = NULL;
Chris Mason4b52dff2007-06-26 10:06:50 -0400491 btrfs_set_super_generation(&root->fs_info->super_copy,
492 cur_trans->transid);
493 btrfs_set_super_root(&root->fs_info->super_copy,
494 bh_blocknr(root->fs_info->tree_root->node));
495 memcpy(root->fs_info->disk_super, &root->fs_info->super_copy,
496 sizeof(root->fs_info->super_copy));
Chris Masonccd467d2007-06-28 15:57:36 -0400497
498 btrfs_copy_pinned(root, &pinned_copy);
499
Chris Mason78fae272007-03-25 11:35:08 -0400500 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason8fd17792007-04-19 21:01:03 -0400501 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400502 ret = btrfs_write_and_wait_transaction(trans, root);
503 BUG_ON(ret);
Chris Mason79154b12007-03-22 15:59:16 -0400504 write_ctree_super(trans, root);
Chris Mason8fd17792007-04-19 21:01:03 -0400505 mutex_lock(&root->fs_info->fs_mutex);
Chris Masonccd467d2007-06-28 15:57:36 -0400506 btrfs_finish_extent_commit(trans, root, &pinned_copy);
Chris Mason78fae272007-03-25 11:35:08 -0400507 mutex_lock(&root->fs_info->trans_mutex);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400508 cur_trans->commit_done = 1;
509 wake_up(&cur_trans->commit_wait);
Chris Mason79154b12007-03-22 15:59:16 -0400510 put_transaction(cur_trans);
Chris Mason78fae272007-03-25 11:35:08 -0400511 put_transaction(cur_trans);
Chris Masonfacda1e2007-06-08 18:11:48 -0400512 if (root->fs_info->closing)
513 list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots);
514 else
515 list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots);
Chris Mason78fae272007-03-25 11:35:08 -0400516 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400517 kmem_cache_free(btrfs_trans_handle_cachep, trans);
Chris Mason79154b12007-03-22 15:59:16 -0400518
Chris Masonfacda1e2007-06-08 18:11:48 -0400519 if (root->fs_info->closing) {
520 mutex_unlock(&root->fs_info->fs_mutex);
521 drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);
522 mutex_lock(&root->fs_info->fs_mutex);
523 }
Chris Mason79154b12007-03-22 15:59:16 -0400524 return ret;
525}
526
Chris Mason08607c12007-06-08 15:33:54 -0400527void btrfs_transaction_cleaner(struct work_struct *work)
528{
529 struct btrfs_fs_info *fs_info = container_of(work,
530 struct btrfs_fs_info,
531 trans_work.work);
532
533 struct btrfs_root *root = fs_info->tree_root;
534 struct btrfs_transaction *cur;
535 struct btrfs_trans_handle *trans;
Chris Masonfacda1e2007-06-08 18:11:48 -0400536 struct list_head dirty_roots;
Chris Mason08607c12007-06-08 15:33:54 -0400537 unsigned long now;
538 unsigned long delay = HZ * 30;
539 int ret;
540
Chris Masonfacda1e2007-06-08 18:11:48 -0400541 INIT_LIST_HEAD(&dirty_roots);
Chris Mason08607c12007-06-08 15:33:54 -0400542 mutex_lock(&root->fs_info->fs_mutex);
543 mutex_lock(&root->fs_info->trans_mutex);
544 cur = root->fs_info->running_transaction;
545 if (!cur) {
546 mutex_unlock(&root->fs_info->trans_mutex);
547 goto out;
548 }
549 now = get_seconds();
550 if (now < cur->start_time || now - cur->start_time < 30) {
551 mutex_unlock(&root->fs_info->trans_mutex);
552 delay = HZ * 5;
553 goto out;
554 }
555 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason6702ed42007-08-07 16:15:09 -0400556 btrfs_defrag_dirty_roots(root->fs_info);
Chris Mason08607c12007-06-08 15:33:54 -0400557 trans = btrfs_start_transaction(root, 1);
558 ret = btrfs_commit_transaction(trans, root);
559out:
560 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masonad693af2007-06-09 08:19:57 -0400561
562 mutex_lock(&root->fs_info->trans_mutex);
563 list_splice_init(&root->fs_info->dead_roots, &dirty_roots);
564 mutex_unlock(&root->fs_info->trans_mutex);
565
566 if (!list_empty(&dirty_roots)) {
567 drop_dirty_roots(root, &dirty_roots);
568 }
Chris Mason08607c12007-06-08 15:33:54 -0400569 btrfs_transaction_queue_work(root, delay);
570}
571
572void btrfs_transaction_queue_work(struct btrfs_root *root, int delay)
573{
574 queue_delayed_work(trans_wq, &root->fs_info->trans_work, delay);
575}
576
577void btrfs_transaction_flush_work(struct btrfs_root *root)
578{
579 cancel_rearming_delayed_workqueue(trans_wq, &root->fs_info->trans_work);
580 flush_workqueue(trans_wq);
581}
582
583void __init btrfs_init_transaction_sys(void)
584{
585 trans_wq = create_workqueue("btrfs");
586}
587
588void __exit btrfs_exit_transaction_sys(void)
589{
590 destroy_workqueue(trans_wq);
591}
592