blob: 88b0752550b63d0a84cebc2580cce4340f6b9c16 [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 Mason79154b12007-03-22 15:59:16 -0400112 root->fs_info->running_transaction->use_count++;
113 mutex_unlock(&root->fs_info->trans_mutex);
114 return h;
115}
116
117int btrfs_end_transaction(struct btrfs_trans_handle *trans,
118 struct btrfs_root *root)
119{
120 struct btrfs_transaction *cur_trans;
Chris Masond6e4a422007-04-06 15:37:36 -0400121
Chris Mason79154b12007-03-22 15:59:16 -0400122 mutex_lock(&root->fs_info->trans_mutex);
123 cur_trans = root->fs_info->running_transaction;
Chris Masonccd467d2007-06-28 15:57:36 -0400124 WARN_ON(cur_trans != trans->transaction);
Chris Masond5719762007-03-23 10:01:08 -0400125 WARN_ON(cur_trans->num_writers < 1);
Chris Masonccd467d2007-06-28 15:57:36 -0400126 cur_trans->num_writers--;
Chris Mason79154b12007-03-22 15:59:16 -0400127 if (waitqueue_active(&cur_trans->writer_wait))
128 wake_up(&cur_trans->writer_wait);
Chris Mason79154b12007-03-22 15:59:16 -0400129 put_transaction(cur_trans);
130 mutex_unlock(&root->fs_info->trans_mutex);
Chris Masond6025572007-03-30 14:27:56 -0400131 memset(trans, 0, sizeof(*trans));
Chris Mason2c90e5d2007-04-02 10:50:19 -0400132 kmem_cache_free(btrfs_trans_handle_cachep, trans);
Chris Mason79154b12007-03-22 15:59:16 -0400133 return 0;
134}
135
136
137int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
138 struct btrfs_root *root)
139{
Chris Mason7c4452b2007-04-28 09:29:35 -0400140 unsigned long gang[16];
141 int ret;
142 int i;
143 int err;
144 int werr = 0;
145 struct page *page;
146 struct radix_tree_root *dirty_pages;
147 struct inode *btree_inode = root->fs_info->btree_inode;
148
149 if (!trans || !trans->transaction) {
150 return filemap_write_and_wait(btree_inode->i_mapping);
151 }
152 dirty_pages = &trans->transaction->dirty_pages;
153 while(1) {
Chris Masone37c9e62007-05-09 20:13:14 -0400154 ret = find_first_radix_bit(dirty_pages, gang,
155 0, ARRAY_SIZE(gang));
Chris Mason7c4452b2007-04-28 09:29:35 -0400156 if (!ret)
157 break;
158 for (i = 0; i < ret; i++) {
159 /* FIXME EIO */
160 clear_radix_bit(dirty_pages, gang[i]);
161 page = find_lock_page(btree_inode->i_mapping,
162 gang[i]);
163 if (!page)
164 continue;
Chris Mason6702ed42007-08-07 16:15:09 -0400165 if (PageWriteback(page)) {
166 if (PageDirty(page))
167 wait_on_page_writeback(page);
168 else {
169 unlock_page(page);
170 page_cache_release(page);
171 continue;
172 }
173 }
Chris Mason7c4452b2007-04-28 09:29:35 -0400174 err = write_one_page(page, 0);
175 if (err)
176 werr = err;
177 page_cache_release(page);
178 }
179 }
180 err = filemap_fdatawait(btree_inode->i_mapping);
181 if (err)
182 werr = err;
183 return werr;
Chris Mason79154b12007-03-22 15:59:16 -0400184}
185
186int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans,
187 struct btrfs_root *root)
188{
189 int ret;
190 u64 old_extent_block;
191 struct btrfs_fs_info *fs_info = root->fs_info;
192 struct btrfs_root *tree_root = fs_info->tree_root;
193 struct btrfs_root *extent_root = fs_info->extent_root;
Chris Mason79154b12007-03-22 15:59:16 -0400194
Chris Mason9078a3e2007-04-26 16:46:15 -0400195 btrfs_write_dirty_block_groups(trans, extent_root);
Chris Mason79154b12007-03-22 15:59:16 -0400196 while(1) {
197 old_extent_block = btrfs_root_blocknr(&extent_root->root_item);
Chris Mason7eccb902007-04-11 15:53:25 -0400198 if (old_extent_block == bh_blocknr(extent_root->node))
Chris Mason79154b12007-03-22 15:59:16 -0400199 break;
200 btrfs_set_root_blocknr(&extent_root->root_item,
Chris Mason7eccb902007-04-11 15:53:25 -0400201 bh_blocknr(extent_root->node));
Chris Mason79154b12007-03-22 15:59:16 -0400202 ret = btrfs_update_root(trans, tree_root,
203 &extent_root->root_key,
204 &extent_root->root_item);
205 BUG_ON(ret);
Chris Mason9078a3e2007-04-26 16:46:15 -0400206 btrfs_write_dirty_block_groups(trans, extent_root);
Chris Mason79154b12007-03-22 15:59:16 -0400207 }
208 return 0;
209}
210
211static int wait_for_commit(struct btrfs_root *root,
212 struct btrfs_transaction *commit)
213{
214 DEFINE_WAIT(wait);
Chris Masonccd467d2007-06-28 15:57:36 -0400215 mutex_lock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400216 while(!commit->commit_done) {
217 prepare_to_wait(&commit->commit_wait, &wait,
218 TASK_UNINTERRUPTIBLE);
219 if (commit->commit_done)
220 break;
221 mutex_unlock(&root->fs_info->trans_mutex);
222 schedule();
223 mutex_lock(&root->fs_info->trans_mutex);
224 }
Chris Masonccd467d2007-06-28 15:57:36 -0400225 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400226 finish_wait(&commit->commit_wait, &wait);
227 return 0;
228}
229
Chris Mason0f7d52f2007-04-09 10:42:37 -0400230struct dirty_root {
231 struct list_head list;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400232 struct btrfs_root *root;
233};
234
Chris Mason5eda7b52007-06-22 14:16:25 -0400235int btrfs_add_dead_root(struct btrfs_root *root, struct list_head *dead_list)
236{
237 struct dirty_root *dirty;
238
239 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
240 if (!dirty)
241 return -ENOMEM;
Chris Mason5eda7b52007-06-22 14:16:25 -0400242 dirty->root = root;
Chris Mason5eda7b52007-06-22 14:16:25 -0400243 list_add(&dirty->list, dead_list);
244 return 0;
245}
246
Chris Mason35b7e472007-05-02 15:53:43 -0400247static int add_dirty_roots(struct btrfs_trans_handle *trans,
248 struct radix_tree_root *radix,
249 struct list_head *list)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400250{
251 struct dirty_root *dirty;
252 struct btrfs_root *gang[8];
253 struct btrfs_root *root;
254 int i;
255 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400256 int err = 0;
Chris Mason5eda7b52007-06-22 14:16:25 -0400257 u32 refs;
Chris Mason54aa1f42007-06-22 14:16:25 -0400258
Chris Mason0f7d52f2007-04-09 10:42:37 -0400259 while(1) {
260 ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0,
261 ARRAY_SIZE(gang),
262 BTRFS_ROOT_TRANS_TAG);
263 if (ret == 0)
264 break;
265 for (i = 0; i < ret; i++) {
266 root = gang[i];
Chris Mason2619ba12007-04-10 16:58:11 -0400267 radix_tree_tag_clear(radix,
268 (unsigned long)root->root_key.objectid,
269 BTRFS_ROOT_TRANS_TAG);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400270 if (root->commit_root == root->node) {
Chris Mason7eccb902007-04-11 15:53:25 -0400271 WARN_ON(bh_blocknr(root->node) !=
Chris Mason0f7d52f2007-04-09 10:42:37 -0400272 btrfs_root_blocknr(&root->root_item));
273 brelse(root->commit_root);
274 root->commit_root = NULL;
275 continue;
276 }
277 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
278 BUG_ON(!dirty);
Chris Mason9f3a7422007-08-07 15:52:19 -0400279 dirty->root = kmalloc(sizeof(*dirty->root), GFP_NOFS);
280 BUG_ON(!dirty->root);
281
282 memset(&root->root_item.drop_progress, 0,
283 sizeof(struct btrfs_disk_key));
284 root->root_item.drop_level = 0;
285
286 memcpy(dirty->root, root, sizeof(*root));
287 dirty->root->node = root->commit_root;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400288 root->commit_root = NULL;
Chris Mason5eda7b52007-06-22 14:16:25 -0400289
Chris Mason0f7d52f2007-04-09 10:42:37 -0400290 root->root_key.offset = root->fs_info->generation;
291 btrfs_set_root_blocknr(&root->root_item,
Chris Mason7eccb902007-04-11 15:53:25 -0400292 bh_blocknr(root->node));
Chris Mason0f7d52f2007-04-09 10:42:37 -0400293 err = btrfs_insert_root(trans, root->fs_info->tree_root,
294 &root->root_key,
295 &root->root_item);
Chris Mason54aa1f42007-06-22 14:16:25 -0400296 if (err)
297 break;
Chris Mason9f3a7422007-08-07 15:52:19 -0400298
299 refs = btrfs_root_refs(&dirty->root->root_item);
300 btrfs_set_root_refs(&dirty->root->root_item, refs - 1);
Chris Mason5eda7b52007-06-22 14:16:25 -0400301 err = btrfs_update_root(trans, root->fs_info->tree_root,
Chris Mason9f3a7422007-08-07 15:52:19 -0400302 &dirty->root->root_key,
303 &dirty->root->root_item);
Chris Mason5eda7b52007-06-22 14:16:25 -0400304
305 BUG_ON(err);
Chris Mason9f3a7422007-08-07 15:52:19 -0400306 if (refs == 1) {
Chris Mason5eda7b52007-06-22 14:16:25 -0400307 list_add(&dirty->list, list);
Chris Mason9f3a7422007-08-07 15:52:19 -0400308 } else {
309 WARN_ON(1);
310 kfree(dirty->root);
Chris Mason5eda7b52007-06-22 14:16:25 -0400311 kfree(dirty);
Chris Mason9f3a7422007-08-07 15:52:19 -0400312 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400313 }
314 }
Chris Mason54aa1f42007-06-22 14:16:25 -0400315 return err;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400316}
317
Chris Mason6702ed42007-08-07 16:15:09 -0400318int btrfs_defrag_dirty_roots(struct btrfs_fs_info *info)
319{
320 struct btrfs_root *gang[1];
321 struct btrfs_root *root;
322 struct btrfs_root *tree_root = info->tree_root;
323 struct btrfs_trans_handle *trans;
324 int i;
325 int ret;
326 int err = 0;
327 u64 last = 0;
328
329 trans = btrfs_start_transaction(tree_root, 1);
330 while(1) {
331 ret = radix_tree_gang_lookup_tag(&info->fs_roots_radix,
332 (void **)gang, last,
333 ARRAY_SIZE(gang),
334 BTRFS_ROOT_DEFRAG_TAG);
335 if (ret == 0)
336 break;
337 for (i = 0; i < ret; i++) {
338 root = gang[i];
339 last = root->root_key.objectid + 1;
340 radix_tree_tag_clear(&info->fs_roots_radix,
341 (unsigned long)root->root_key.objectid,
342 BTRFS_ROOT_DEFRAG_TAG);
343 if (root->defrag_running)
344 continue;
345
346 while (1) {
347 mutex_lock(&root->fs_info->trans_mutex);
348 record_root_in_trans(root);
349 mutex_unlock(&root->fs_info->trans_mutex);
350
351 root->defrag_running = 1;
352 err = btrfs_defrag_leaves(trans, root, 1);
353 btrfs_end_transaction(trans, tree_root);
354 mutex_unlock(&info->fs_mutex);
355
356 btrfs_btree_balance_dirty(root);
Chris Masonf4468e92007-08-08 10:08:58 -0400357 cond_resched();
Chris Mason6702ed42007-08-07 16:15:09 -0400358
359 mutex_lock(&info->fs_mutex);
360 trans = btrfs_start_transaction(tree_root, 1);
361 if (err != -EAGAIN)
362 break;
363 }
364 root->defrag_running = 0;
365 }
366 }
367 btrfs_end_transaction(trans, tree_root);
368 return err;
369}
370
Chris Mason35b7e472007-05-02 15:53:43 -0400371static int drop_dirty_roots(struct btrfs_root *tree_root,
372 struct list_head *list)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400373{
374 struct dirty_root *dirty;
375 struct btrfs_trans_handle *trans;
Chris Mason54aa1f42007-06-22 14:16:25 -0400376 int ret = 0;
Chris Mason9f3a7422007-08-07 15:52:19 -0400377 int err;
378
Chris Mason0f7d52f2007-04-09 10:42:37 -0400379 while(!list_empty(list)) {
Chris Masonfacda1e2007-06-08 18:11:48 -0400380 mutex_lock(&tree_root->fs_info->fs_mutex);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400381 dirty = list_entry(list->next, struct dirty_root, list);
382 list_del_init(&dirty->list);
Chris Mason5eda7b52007-06-22 14:16:25 -0400383
Chris Mason9f3a7422007-08-07 15:52:19 -0400384 while(1) {
385 trans = btrfs_start_transaction(tree_root, 1);
386 ret = btrfs_drop_snapshot(trans, dirty->root);
387 if (ret != -EAGAIN) {
388 break;
389 }
390 err = btrfs_update_root(trans,
391 tree_root,
392 &dirty->root->root_key,
393 &dirty->root->root_item);
394 if (err)
395 ret = err;
396 ret = btrfs_end_transaction(trans, tree_root);
397 BUG_ON(ret);
Chris Masonf4468e92007-08-08 10:08:58 -0400398 mutex_unlock(&tree_root->fs_info->fs_mutex);
399
400 btrfs_btree_balance_dirty(tree_root);
401 schedule();
402
403 mutex_lock(&tree_root->fs_info->fs_mutex);
Chris Mason9f3a7422007-08-07 15:52:19 -0400404 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400405 BUG_ON(ret);
Chris Mason9f3a7422007-08-07 15:52:19 -0400406 ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key);
Chris Mason54aa1f42007-06-22 14:16:25 -0400407 if (ret)
408 break;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400409 ret = btrfs_end_transaction(trans, tree_root);
410 BUG_ON(ret);
Chris Mason5eda7b52007-06-22 14:16:25 -0400411
Chris Mason9f3a7422007-08-07 15:52:19 -0400412 kfree(dirty->root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400413 kfree(dirty);
Chris Masonfacda1e2007-06-08 18:11:48 -0400414 mutex_unlock(&tree_root->fs_info->fs_mutex);
Chris Mason8c2383c2007-06-18 09:57:58 -0400415 btrfs_btree_balance_dirty(tree_root);
Chris Masonf4468e92007-08-08 10:08:58 -0400416 schedule();
Chris Mason0f7d52f2007-04-09 10:42:37 -0400417 }
Chris Mason54aa1f42007-06-22 14:16:25 -0400418 return ret;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400419}
420
Chris Mason79154b12007-03-22 15:59:16 -0400421int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
422 struct btrfs_root *root)
423{
424 int ret = 0;
Chris Mason79154b12007-03-22 15:59:16 -0400425 struct btrfs_transaction *cur_trans;
Chris Mason8fd17792007-04-19 21:01:03 -0400426 struct btrfs_transaction *prev_trans = NULL;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400427 struct list_head dirty_fs_roots;
Chris Masonccd467d2007-06-28 15:57:36 -0400428 struct radix_tree_root pinned_copy;
Chris Mason79154b12007-03-22 15:59:16 -0400429 DEFINE_WAIT(wait);
430
Chris Masonccd467d2007-06-28 15:57:36 -0400431 init_bit_radix(&pinned_copy);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400432 INIT_LIST_HEAD(&dirty_fs_roots);
Chris Masond6e4a422007-04-06 15:37:36 -0400433
Chris Mason79154b12007-03-22 15:59:16 -0400434 mutex_lock(&root->fs_info->trans_mutex);
435 if (trans->transaction->in_commit) {
436 cur_trans = trans->transaction;
437 trans->transaction->use_count++;
Chris Masonccd467d2007-06-28 15:57:36 -0400438 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400439 btrfs_end_transaction(trans, root);
Chris Masonccd467d2007-06-28 15:57:36 -0400440
441 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400442 ret = wait_for_commit(root, cur_trans);
443 BUG_ON(ret);
444 put_transaction(cur_trans);
Chris Masonccd467d2007-06-28 15:57:36 -0400445 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400446 return 0;
447 }
Chris Mason2c90e5d2007-04-02 10:50:19 -0400448 trans->transaction->in_commit = 1;
Chris Masonccd467d2007-06-28 15:57:36 -0400449 cur_trans = trans->transaction;
450 if (cur_trans->list.prev != &root->fs_info->trans_list) {
451 prev_trans = list_entry(cur_trans->list.prev,
452 struct btrfs_transaction, list);
453 if (!prev_trans->commit_done) {
454 prev_trans->use_count++;
455 mutex_unlock(&root->fs_info->fs_mutex);
456 mutex_unlock(&root->fs_info->trans_mutex);
457
458 wait_for_commit(root, prev_trans);
459 put_transaction(prev_trans);
460
461 mutex_lock(&root->fs_info->fs_mutex);
462 mutex_lock(&root->fs_info->trans_mutex);
463 }
464 }
Chris Mason79154b12007-03-22 15:59:16 -0400465 while (trans->transaction->num_writers > 1) {
Chris Mason2c90e5d2007-04-02 10:50:19 -0400466 WARN_ON(cur_trans != trans->transaction);
Chris Mason79154b12007-03-22 15:59:16 -0400467 prepare_to_wait(&trans->transaction->writer_wait, &wait,
468 TASK_UNINTERRUPTIBLE);
469 if (trans->transaction->num_writers <= 1)
470 break;
Chris Masonccd467d2007-06-28 15:57:36 -0400471 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400472 mutex_unlock(&root->fs_info->trans_mutex);
473 schedule();
Chris Masonccd467d2007-06-28 15:57:36 -0400474 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400475 mutex_lock(&root->fs_info->trans_mutex);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400476 finish_wait(&trans->transaction->writer_wait, &wait);
Chris Mason79154b12007-03-22 15:59:16 -0400477 }
478 finish_wait(&trans->transaction->writer_wait, &wait);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400479 WARN_ON(cur_trans != trans->transaction);
Chris Mason54aa1f42007-06-22 14:16:25 -0400480 ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix,
481 &dirty_fs_roots);
482 BUG_ON(ret);
483
Chris Mason79154b12007-03-22 15:59:16 -0400484 ret = btrfs_commit_tree_roots(trans, root);
485 BUG_ON(ret);
Chris Mason54aa1f42007-06-22 14:16:25 -0400486
Chris Mason78fae272007-03-25 11:35:08 -0400487 cur_trans = root->fs_info->running_transaction;
488 root->fs_info->running_transaction = NULL;
Chris Mason4b52dff2007-06-26 10:06:50 -0400489 btrfs_set_super_generation(&root->fs_info->super_copy,
490 cur_trans->transid);
491 btrfs_set_super_root(&root->fs_info->super_copy,
492 bh_blocknr(root->fs_info->tree_root->node));
493 memcpy(root->fs_info->disk_super, &root->fs_info->super_copy,
494 sizeof(root->fs_info->super_copy));
Chris Masonccd467d2007-06-28 15:57:36 -0400495
496 btrfs_copy_pinned(root, &pinned_copy);
497
Chris Mason78fae272007-03-25 11:35:08 -0400498 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason8fd17792007-04-19 21:01:03 -0400499 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400500 ret = btrfs_write_and_wait_transaction(trans, root);
501 BUG_ON(ret);
Chris Mason79154b12007-03-22 15:59:16 -0400502 write_ctree_super(trans, root);
Chris Mason8fd17792007-04-19 21:01:03 -0400503 mutex_lock(&root->fs_info->fs_mutex);
Chris Masonccd467d2007-06-28 15:57:36 -0400504 btrfs_finish_extent_commit(trans, root, &pinned_copy);
Chris Mason78fae272007-03-25 11:35:08 -0400505 mutex_lock(&root->fs_info->trans_mutex);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400506 cur_trans->commit_done = 1;
507 wake_up(&cur_trans->commit_wait);
Chris Mason79154b12007-03-22 15:59:16 -0400508 put_transaction(cur_trans);
Chris Mason78fae272007-03-25 11:35:08 -0400509 put_transaction(cur_trans);
Chris Masonfacda1e2007-06-08 18:11:48 -0400510 if (root->fs_info->closing)
511 list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots);
512 else
513 list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots);
Chris Mason78fae272007-03-25 11:35:08 -0400514 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400515 kmem_cache_free(btrfs_trans_handle_cachep, trans);
Chris Mason79154b12007-03-22 15:59:16 -0400516
Chris Masonfacda1e2007-06-08 18:11:48 -0400517 if (root->fs_info->closing) {
518 mutex_unlock(&root->fs_info->fs_mutex);
519 drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);
520 mutex_lock(&root->fs_info->fs_mutex);
521 }
Chris Mason79154b12007-03-22 15:59:16 -0400522 return ret;
523}
524
Chris Mason08607c12007-06-08 15:33:54 -0400525void btrfs_transaction_cleaner(struct work_struct *work)
526{
527 struct btrfs_fs_info *fs_info = container_of(work,
528 struct btrfs_fs_info,
529 trans_work.work);
530
531 struct btrfs_root *root = fs_info->tree_root;
532 struct btrfs_transaction *cur;
533 struct btrfs_trans_handle *trans;
Chris Masonfacda1e2007-06-08 18:11:48 -0400534 struct list_head dirty_roots;
Chris Mason08607c12007-06-08 15:33:54 -0400535 unsigned long now;
536 unsigned long delay = HZ * 30;
537 int ret;
538
Chris Masonfacda1e2007-06-08 18:11:48 -0400539 INIT_LIST_HEAD(&dirty_roots);
Chris Mason08607c12007-06-08 15:33:54 -0400540 mutex_lock(&root->fs_info->fs_mutex);
541 mutex_lock(&root->fs_info->trans_mutex);
542 cur = root->fs_info->running_transaction;
543 if (!cur) {
544 mutex_unlock(&root->fs_info->trans_mutex);
545 goto out;
546 }
547 now = get_seconds();
548 if (now < cur->start_time || now - cur->start_time < 30) {
549 mutex_unlock(&root->fs_info->trans_mutex);
550 delay = HZ * 5;
551 goto out;
552 }
553 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason6702ed42007-08-07 16:15:09 -0400554 btrfs_defrag_dirty_roots(root->fs_info);
Chris Mason08607c12007-06-08 15:33:54 -0400555 trans = btrfs_start_transaction(root, 1);
556 ret = btrfs_commit_transaction(trans, root);
557out:
558 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masonad693af2007-06-09 08:19:57 -0400559
560 mutex_lock(&root->fs_info->trans_mutex);
561 list_splice_init(&root->fs_info->dead_roots, &dirty_roots);
562 mutex_unlock(&root->fs_info->trans_mutex);
563
564 if (!list_empty(&dirty_roots)) {
565 drop_dirty_roots(root, &dirty_roots);
566 }
Chris Mason08607c12007-06-08 15:33:54 -0400567 btrfs_transaction_queue_work(root, delay);
568}
569
570void btrfs_transaction_queue_work(struct btrfs_root *root, int delay)
571{
572 queue_delayed_work(trans_wq, &root->fs_info->trans_work, delay);
573}
574
575void btrfs_transaction_flush_work(struct btrfs_root *root)
576{
577 cancel_rearming_delayed_workqueue(trans_wq, &root->fs_info->trans_work);
578 flush_workqueue(trans_wq);
579}
580
581void __init btrfs_init_transaction_sys(void)
582{
583 trans_wq = create_workqueue("btrfs");
584}
585
586void __exit btrfs_exit_transaction_sys(void)
587{
588 destroy_workqueue(trans_wq);
589}
590