blob: 087074db0bd56fffd2718f6dcd8ded88ad3f3644 [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 Masond3c2fdcf2007-09-17 10:58:06 -040021#include <linux/writeback.h>
Chris Mason79154b12007-03-22 15:59:16 -040022#include "ctree.h"
23#include "disk-io.h"
24#include "transaction.h"
25
Chris Mason78fae272007-03-25 11:35:08 -040026static int total_trans = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -040027extern struct kmem_cache *btrfs_trans_handle_cachep;
28extern struct kmem_cache *btrfs_transaction_cachep;
29
Chris Mason08607c12007-06-08 15:33:54 -040030static struct workqueue_struct *trans_wq;
31
Chris Mason0f7d52f2007-04-09 10:42:37 -040032#define BTRFS_ROOT_TRANS_TAG 0
Chris Mason6702ed42007-08-07 16:15:09 -040033#define BTRFS_ROOT_DEFRAG_TAG 1
Chris Mason0f7d52f2007-04-09 10:42:37 -040034
Chris Mason79154b12007-03-22 15:59:16 -040035static void put_transaction(struct btrfs_transaction *transaction)
36{
Chris Mason2c90e5d2007-04-02 10:50:19 -040037 WARN_ON(transaction->use_count == 0);
Chris Mason79154b12007-03-22 15:59:16 -040038 transaction->use_count--;
Chris Mason78fae272007-03-25 11:35:08 -040039 if (transaction->use_count == 0) {
40 WARN_ON(total_trans == 0);
41 total_trans--;
Chris Mason8fd17792007-04-19 21:01:03 -040042 list_del_init(&transaction->list);
Chris Mason2c90e5d2007-04-02 10:50:19 -040043 memset(transaction, 0, sizeof(*transaction));
44 kmem_cache_free(btrfs_transaction_cachep, transaction);
Chris Mason78fae272007-03-25 11:35:08 -040045 }
Chris Mason79154b12007-03-22 15:59:16 -040046}
47
48static int join_transaction(struct btrfs_root *root)
49{
50 struct btrfs_transaction *cur_trans;
51 cur_trans = root->fs_info->running_transaction;
52 if (!cur_trans) {
Chris Mason2c90e5d2007-04-02 10:50:19 -040053 cur_trans = kmem_cache_alloc(btrfs_transaction_cachep,
54 GFP_NOFS);
Chris Mason78fae272007-03-25 11:35:08 -040055 total_trans++;
Chris Mason79154b12007-03-22 15:59:16 -040056 BUG_ON(!cur_trans);
Chris Mason0f7d52f2007-04-09 10:42:37 -040057 root->fs_info->generation++;
Chris Mason79154b12007-03-22 15:59:16 -040058 root->fs_info->running_transaction = cur_trans;
Josef Bacik15ee9bc2007-08-10 16:22:09 -040059 cur_trans->num_writers = 1;
60 cur_trans->num_joined = 0;
Chris Mason0f7d52f2007-04-09 10:42:37 -040061 cur_trans->transid = root->fs_info->generation;
Chris Mason79154b12007-03-22 15:59:16 -040062 init_waitqueue_head(&cur_trans->writer_wait);
63 init_waitqueue_head(&cur_trans->commit_wait);
64 cur_trans->in_commit = 0;
Chris Masond5719762007-03-23 10:01:08 -040065 cur_trans->use_count = 1;
Chris Mason79154b12007-03-22 15:59:16 -040066 cur_trans->commit_done = 0;
Chris Mason08607c12007-06-08 15:33:54 -040067 cur_trans->start_time = get_seconds();
Chris Mason8fd17792007-04-19 21:01:03 -040068 list_add_tail(&cur_trans->list, &root->fs_info->trans_list);
Chris Mason7c4452b2007-04-28 09:29:35 -040069 init_bit_radix(&cur_trans->dirty_pages);
Josef Bacik15ee9bc2007-08-10 16:22:09 -040070 } else {
71 cur_trans->num_writers++;
72 cur_trans->num_joined++;
Chris Mason79154b12007-03-22 15:59:16 -040073 }
Josef Bacik15ee9bc2007-08-10 16:22:09 -040074
Chris Mason79154b12007-03-22 15:59:16 -040075 return 0;
76}
77
Chris Mason6702ed42007-08-07 16:15:09 -040078static int record_root_in_trans(struct btrfs_root *root)
79{
80 u64 running_trans_id = root->fs_info->running_transaction->transid;
81 if (root->ref_cows && root->last_trans < running_trans_id) {
82 WARN_ON(root == root->fs_info->extent_root);
83 if (root->root_item.refs != 0) {
84 radix_tree_tag_set(&root->fs_info->fs_roots_radix,
85 (unsigned long)root->root_key.objectid,
86 BTRFS_ROOT_TRANS_TAG);
87 radix_tree_tag_set(&root->fs_info->fs_roots_radix,
88 (unsigned long)root->root_key.objectid,
89 BTRFS_ROOT_DEFRAG_TAG);
90 root->commit_root = root->node;
91 get_bh(root->node);
92 } else {
93 WARN_ON(1);
94 }
95 root->last_trans = running_trans_id;
96 }
97 return 0;
98}
99
Chris Mason79154b12007-03-22 15:59:16 -0400100struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
101 int num_blocks)
102{
Chris Mason2c90e5d2007-04-02 10:50:19 -0400103 struct btrfs_trans_handle *h =
104 kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
Chris Mason79154b12007-03-22 15:59:16 -0400105 int ret;
106
107 mutex_lock(&root->fs_info->trans_mutex);
108 ret = join_transaction(root);
109 BUG_ON(ret);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400110
Chris Mason6702ed42007-08-07 16:15:09 -0400111 record_root_in_trans(root);
112 h->transid = root->fs_info->running_transaction->transid;
Chris Mason79154b12007-03-22 15:59:16 -0400113 h->transaction = root->fs_info->running_transaction;
114 h->blocks_reserved = num_blocks;
115 h->blocks_used = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400116 h->block_group = NULL;
Chris Mason26b80032007-08-08 20:17:12 -0400117 h->alloc_exclude_nr = 0;
118 h->alloc_exclude_start = 0;
Chris Mason79154b12007-03-22 15:59:16 -0400119 root->fs_info->running_transaction->use_count++;
120 mutex_unlock(&root->fs_info->trans_mutex);
121 return h;
122}
123
124int btrfs_end_transaction(struct btrfs_trans_handle *trans,
125 struct btrfs_root *root)
126{
127 struct btrfs_transaction *cur_trans;
Chris Masond6e4a422007-04-06 15:37:36 -0400128
Chris Mason79154b12007-03-22 15:59:16 -0400129 mutex_lock(&root->fs_info->trans_mutex);
130 cur_trans = root->fs_info->running_transaction;
Chris Masonccd467d2007-06-28 15:57:36 -0400131 WARN_ON(cur_trans != trans->transaction);
Chris Masond5719762007-03-23 10:01:08 -0400132 WARN_ON(cur_trans->num_writers < 1);
Chris Masonccd467d2007-06-28 15:57:36 -0400133 cur_trans->num_writers--;
Chris Mason79154b12007-03-22 15:59:16 -0400134 if (waitqueue_active(&cur_trans->writer_wait))
135 wake_up(&cur_trans->writer_wait);
Chris Mason79154b12007-03-22 15:59:16 -0400136 put_transaction(cur_trans);
137 mutex_unlock(&root->fs_info->trans_mutex);
Chris Masond6025572007-03-30 14:27:56 -0400138 memset(trans, 0, sizeof(*trans));
Chris Mason2c90e5d2007-04-02 10:50:19 -0400139 kmem_cache_free(btrfs_trans_handle_cachep, trans);
Chris Mason79154b12007-03-22 15:59:16 -0400140 return 0;
141}
142
143
144int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
145 struct btrfs_root *root)
146{
Chris Mason7c4452b2007-04-28 09:29:35 -0400147 unsigned long gang[16];
148 int ret;
149 int i;
150 int err;
151 int werr = 0;
152 struct page *page;
153 struct radix_tree_root *dirty_pages;
154 struct inode *btree_inode = root->fs_info->btree_inode;
155
156 if (!trans || !trans->transaction) {
157 return filemap_write_and_wait(btree_inode->i_mapping);
158 }
159 dirty_pages = &trans->transaction->dirty_pages;
160 while(1) {
Chris Masone37c9e62007-05-09 20:13:14 -0400161 ret = find_first_radix_bit(dirty_pages, gang,
162 0, ARRAY_SIZE(gang));
Chris Mason7c4452b2007-04-28 09:29:35 -0400163 if (!ret)
164 break;
165 for (i = 0; i < ret; i++) {
166 /* FIXME EIO */
167 clear_radix_bit(dirty_pages, gang[i]);
168 page = find_lock_page(btree_inode->i_mapping,
169 gang[i]);
170 if (!page)
171 continue;
Chris Mason6702ed42007-08-07 16:15:09 -0400172 if (PageWriteback(page)) {
173 if (PageDirty(page))
174 wait_on_page_writeback(page);
175 else {
176 unlock_page(page);
177 page_cache_release(page);
178 continue;
179 }
180 }
Chris Mason7c4452b2007-04-28 09:29:35 -0400181 err = write_one_page(page, 0);
182 if (err)
183 werr = err;
184 page_cache_release(page);
185 }
186 }
187 err = filemap_fdatawait(btree_inode->i_mapping);
188 if (err)
189 werr = err;
190 return werr;
Chris Mason79154b12007-03-22 15:59:16 -0400191}
192
193int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans,
194 struct btrfs_root *root)
195{
196 int ret;
197 u64 old_extent_block;
198 struct btrfs_fs_info *fs_info = root->fs_info;
199 struct btrfs_root *tree_root = fs_info->tree_root;
200 struct btrfs_root *extent_root = fs_info->extent_root;
Chris Mason79154b12007-03-22 15:59:16 -0400201
Chris Mason9078a3e2007-04-26 16:46:15 -0400202 btrfs_write_dirty_block_groups(trans, extent_root);
Chris Mason79154b12007-03-22 15:59:16 -0400203 while(1) {
204 old_extent_block = btrfs_root_blocknr(&extent_root->root_item);
Chris Mason7eccb902007-04-11 15:53:25 -0400205 if (old_extent_block == bh_blocknr(extent_root->node))
Chris Mason79154b12007-03-22 15:59:16 -0400206 break;
207 btrfs_set_root_blocknr(&extent_root->root_item,
Chris Mason7eccb902007-04-11 15:53:25 -0400208 bh_blocknr(extent_root->node));
Chris Mason79154b12007-03-22 15:59:16 -0400209 ret = btrfs_update_root(trans, tree_root,
210 &extent_root->root_key,
211 &extent_root->root_item);
212 BUG_ON(ret);
Chris Mason9078a3e2007-04-26 16:46:15 -0400213 btrfs_write_dirty_block_groups(trans, extent_root);
Chris Mason79154b12007-03-22 15:59:16 -0400214 }
215 return 0;
216}
217
218static int wait_for_commit(struct btrfs_root *root,
219 struct btrfs_transaction *commit)
220{
221 DEFINE_WAIT(wait);
Chris Masonccd467d2007-06-28 15:57:36 -0400222 mutex_lock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400223 while(!commit->commit_done) {
224 prepare_to_wait(&commit->commit_wait, &wait,
225 TASK_UNINTERRUPTIBLE);
226 if (commit->commit_done)
227 break;
228 mutex_unlock(&root->fs_info->trans_mutex);
229 schedule();
230 mutex_lock(&root->fs_info->trans_mutex);
231 }
Chris Masonccd467d2007-06-28 15:57:36 -0400232 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400233 finish_wait(&commit->commit_wait, &wait);
234 return 0;
235}
236
Chris Mason0f7d52f2007-04-09 10:42:37 -0400237struct dirty_root {
238 struct list_head list;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400239 struct btrfs_root *root;
Josef Bacik58176a92007-08-29 15:47:34 -0400240 struct btrfs_root *latest_root;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400241};
242
Chris Mason5ce14bb2007-09-11 11:15:39 -0400243int btrfs_add_dead_root(struct btrfs_root *root,
244 struct btrfs_root *latest,
245 struct list_head *dead_list)
Chris Mason5eda7b52007-06-22 14:16:25 -0400246{
247 struct dirty_root *dirty;
248
249 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
250 if (!dirty)
251 return -ENOMEM;
Chris Mason5eda7b52007-06-22 14:16:25 -0400252 dirty->root = root;
Chris Mason5ce14bb2007-09-11 11:15:39 -0400253 dirty->latest_root = latest;
Chris Mason5eda7b52007-06-22 14:16:25 -0400254 list_add(&dirty->list, dead_list);
255 return 0;
256}
257
Chris Mason35b7e472007-05-02 15:53:43 -0400258static int add_dirty_roots(struct btrfs_trans_handle *trans,
259 struct radix_tree_root *radix,
260 struct list_head *list)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400261{
262 struct dirty_root *dirty;
263 struct btrfs_root *gang[8];
264 struct btrfs_root *root;
265 int i;
266 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400267 int err = 0;
Chris Mason5eda7b52007-06-22 14:16:25 -0400268 u32 refs;
Chris Mason54aa1f42007-06-22 14:16:25 -0400269
Chris Mason0f7d52f2007-04-09 10:42:37 -0400270 while(1) {
271 ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0,
272 ARRAY_SIZE(gang),
273 BTRFS_ROOT_TRANS_TAG);
274 if (ret == 0)
275 break;
276 for (i = 0; i < ret; i++) {
277 root = gang[i];
Chris Mason2619ba12007-04-10 16:58:11 -0400278 radix_tree_tag_clear(radix,
279 (unsigned long)root->root_key.objectid,
280 BTRFS_ROOT_TRANS_TAG);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400281 if (root->commit_root == root->node) {
Chris Mason7eccb902007-04-11 15:53:25 -0400282 WARN_ON(bh_blocknr(root->node) !=
Chris Mason0f7d52f2007-04-09 10:42:37 -0400283 btrfs_root_blocknr(&root->root_item));
284 brelse(root->commit_root);
285 root->commit_root = NULL;
Josef Bacik58176a92007-08-29 15:47:34 -0400286
287 /* make sure to update the root on disk
288 * so we get any updates to the block used
289 * counts
290 */
291 err = btrfs_update_root(trans,
292 root->fs_info->tree_root,
293 &root->root_key,
294 &root->root_item);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400295 continue;
296 }
297 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
298 BUG_ON(!dirty);
Chris Mason9f3a7422007-08-07 15:52:19 -0400299 dirty->root = kmalloc(sizeof(*dirty->root), GFP_NOFS);
300 BUG_ON(!dirty->root);
301
302 memset(&root->root_item.drop_progress, 0,
303 sizeof(struct btrfs_disk_key));
304 root->root_item.drop_level = 0;
305
306 memcpy(dirty->root, root, sizeof(*root));
307 dirty->root->node = root->commit_root;
Josef Bacik58176a92007-08-29 15:47:34 -0400308 dirty->latest_root = root;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400309 root->commit_root = NULL;
Chris Mason5eda7b52007-06-22 14:16:25 -0400310
Chris Mason0f7d52f2007-04-09 10:42:37 -0400311 root->root_key.offset = root->fs_info->generation;
312 btrfs_set_root_blocknr(&root->root_item,
Chris Mason7eccb902007-04-11 15:53:25 -0400313 bh_blocknr(root->node));
Chris Mason0f7d52f2007-04-09 10:42:37 -0400314 err = btrfs_insert_root(trans, root->fs_info->tree_root,
315 &root->root_key,
316 &root->root_item);
Chris Mason54aa1f42007-06-22 14:16:25 -0400317 if (err)
318 break;
Chris Mason9f3a7422007-08-07 15:52:19 -0400319
320 refs = btrfs_root_refs(&dirty->root->root_item);
321 btrfs_set_root_refs(&dirty->root->root_item, refs - 1);
Chris Mason5eda7b52007-06-22 14:16:25 -0400322 err = btrfs_update_root(trans, root->fs_info->tree_root,
Chris Mason9f3a7422007-08-07 15:52:19 -0400323 &dirty->root->root_key,
324 &dirty->root->root_item);
Chris Mason5eda7b52007-06-22 14:16:25 -0400325
326 BUG_ON(err);
Chris Mason9f3a7422007-08-07 15:52:19 -0400327 if (refs == 1) {
Chris Mason5eda7b52007-06-22 14:16:25 -0400328 list_add(&dirty->list, list);
Chris Mason9f3a7422007-08-07 15:52:19 -0400329 } else {
330 WARN_ON(1);
331 kfree(dirty->root);
Chris Mason5eda7b52007-06-22 14:16:25 -0400332 kfree(dirty);
Chris Mason9f3a7422007-08-07 15:52:19 -0400333 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400334 }
335 }
Chris Mason54aa1f42007-06-22 14:16:25 -0400336 return err;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400337}
338
Chris Masone9d0b132007-08-10 14:06:19 -0400339int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
340{
341 struct btrfs_fs_info *info = root->fs_info;
342 int ret;
343 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400344 unsigned long nr;
Chris Masone9d0b132007-08-10 14:06:19 -0400345
346 if (root->defrag_running)
347 return 0;
348
349 trans = btrfs_start_transaction(root, 1);
350 while (1) {
351 root->defrag_running = 1;
352 ret = btrfs_defrag_leaves(trans, root, cacheonly);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400353 nr = trans->blocks_used;
Chris Masone9d0b132007-08-10 14:06:19 -0400354 btrfs_end_transaction(trans, root);
355 mutex_unlock(&info->fs_mutex);
356
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400357 btrfs_btree_balance_dirty(info->tree_root, nr);
Chris Masone9d0b132007-08-10 14:06:19 -0400358 cond_resched();
359
360 mutex_lock(&info->fs_mutex);
361 trans = btrfs_start_transaction(root, 1);
362 if (ret != -EAGAIN)
363 break;
364 }
365 root->defrag_running = 0;
366 radix_tree_tag_clear(&info->fs_roots_radix,
367 (unsigned long)root->root_key.objectid,
368 BTRFS_ROOT_DEFRAG_TAG);
369 btrfs_end_transaction(trans, root);
370 return 0;
371}
372
Chris Mason6702ed42007-08-07 16:15:09 -0400373int btrfs_defrag_dirty_roots(struct btrfs_fs_info *info)
374{
375 struct btrfs_root *gang[1];
376 struct btrfs_root *root;
Chris Mason6702ed42007-08-07 16:15:09 -0400377 int i;
378 int ret;
379 int err = 0;
380 u64 last = 0;
381
Chris Mason6702ed42007-08-07 16:15:09 -0400382 while(1) {
383 ret = radix_tree_gang_lookup_tag(&info->fs_roots_radix,
384 (void **)gang, last,
385 ARRAY_SIZE(gang),
386 BTRFS_ROOT_DEFRAG_TAG);
387 if (ret == 0)
388 break;
389 for (i = 0; i < ret; i++) {
390 root = gang[i];
391 last = root->root_key.objectid + 1;
Chris Masone9d0b132007-08-10 14:06:19 -0400392 btrfs_defrag_root(root, 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400393 }
394 }
Chris Masone9d0b132007-08-10 14:06:19 -0400395 btrfs_defrag_root(info->extent_root, 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400396 return err;
397}
398
Chris Mason35b7e472007-05-02 15:53:43 -0400399static int drop_dirty_roots(struct btrfs_root *tree_root,
400 struct list_head *list)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400401{
402 struct dirty_root *dirty;
403 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400404 unsigned long nr;
Josef Bacik58176a92007-08-29 15:47:34 -0400405 u64 num_blocks;
406 u64 blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -0400407 int ret = 0;
Chris Mason9f3a7422007-08-07 15:52:19 -0400408 int err;
409
Chris Mason0f7d52f2007-04-09 10:42:37 -0400410 while(!list_empty(list)) {
Josef Bacik58176a92007-08-29 15:47:34 -0400411 struct btrfs_root *root;
412
Chris Masonfacda1e2007-06-08 18:11:48 -0400413 mutex_lock(&tree_root->fs_info->fs_mutex);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400414 dirty = list_entry(list->next, struct dirty_root, list);
415 list_del_init(&dirty->list);
Chris Mason5eda7b52007-06-22 14:16:25 -0400416
Josef Bacik58176a92007-08-29 15:47:34 -0400417 num_blocks = btrfs_root_blocks_used(&dirty->root->root_item);
418 root = dirty->latest_root;
419
Chris Mason9f3a7422007-08-07 15:52:19 -0400420 while(1) {
421 trans = btrfs_start_transaction(tree_root, 1);
422 ret = btrfs_drop_snapshot(trans, dirty->root);
423 if (ret != -EAGAIN) {
424 break;
425 }
Josef Bacik58176a92007-08-29 15:47:34 -0400426
Chris Mason9f3a7422007-08-07 15:52:19 -0400427 err = btrfs_update_root(trans,
428 tree_root,
429 &dirty->root->root_key,
430 &dirty->root->root_item);
431 if (err)
432 ret = err;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400433 nr = trans->blocks_used;
Chris Mason9f3a7422007-08-07 15:52:19 -0400434 ret = btrfs_end_transaction(trans, tree_root);
435 BUG_ON(ret);
Chris Masonf4468e92007-08-08 10:08:58 -0400436 mutex_unlock(&tree_root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400437 btrfs_btree_balance_dirty(tree_root, nr);
Chris Masonf4468e92007-08-08 10:08:58 -0400438 schedule();
439
440 mutex_lock(&tree_root->fs_info->fs_mutex);
Chris Mason9f3a7422007-08-07 15:52:19 -0400441 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400442 BUG_ON(ret);
Josef Bacik58176a92007-08-29 15:47:34 -0400443
444 num_blocks -= btrfs_root_blocks_used(&dirty->root->root_item);
445 blocks_used = btrfs_root_blocks_used(&root->root_item);
446 if (num_blocks) {
447 record_root_in_trans(root);
448 btrfs_set_root_blocks_used(&root->root_item,
449 blocks_used - num_blocks);
450 }
Chris Mason9f3a7422007-08-07 15:52:19 -0400451 ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key);
Josef Bacik58176a92007-08-29 15:47:34 -0400452 if (ret) {
453 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -0400454 break;
Josef Bacik58176a92007-08-29 15:47:34 -0400455 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400456 nr = trans->blocks_used;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400457 ret = btrfs_end_transaction(trans, tree_root);
458 BUG_ON(ret);
Chris Mason5eda7b52007-06-22 14:16:25 -0400459
Chris Mason9f3a7422007-08-07 15:52:19 -0400460 kfree(dirty->root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400461 kfree(dirty);
Chris Masonfacda1e2007-06-08 18:11:48 -0400462 mutex_unlock(&tree_root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400463
464 btrfs_btree_balance_dirty(tree_root, nr);
Chris Masonf4468e92007-08-08 10:08:58 -0400465 schedule();
Chris Mason0f7d52f2007-04-09 10:42:37 -0400466 }
Chris Mason54aa1f42007-06-22 14:16:25 -0400467 return ret;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400468}
469
Chris Mason79154b12007-03-22 15:59:16 -0400470int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
471 struct btrfs_root *root)
472{
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400473 unsigned long joined = 0;
474 unsigned long timeout = 1;
Chris Mason79154b12007-03-22 15:59:16 -0400475 struct btrfs_transaction *cur_trans;
Chris Mason8fd17792007-04-19 21:01:03 -0400476 struct btrfs_transaction *prev_trans = NULL;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400477 struct list_head dirty_fs_roots;
Chris Masonccd467d2007-06-28 15:57:36 -0400478 struct radix_tree_root pinned_copy;
Chris Mason79154b12007-03-22 15:59:16 -0400479 DEFINE_WAIT(wait);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400480 int ret;
Chris Mason79154b12007-03-22 15:59:16 -0400481
Chris Masonccd467d2007-06-28 15:57:36 -0400482 init_bit_radix(&pinned_copy);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400483 INIT_LIST_HEAD(&dirty_fs_roots);
Chris Masond6e4a422007-04-06 15:37:36 -0400484
Chris Mason79154b12007-03-22 15:59:16 -0400485 mutex_lock(&root->fs_info->trans_mutex);
486 if (trans->transaction->in_commit) {
487 cur_trans = trans->transaction;
488 trans->transaction->use_count++;
Chris Masonccd467d2007-06-28 15:57:36 -0400489 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400490 btrfs_end_transaction(trans, root);
Chris Masonccd467d2007-06-28 15:57:36 -0400491
492 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400493 ret = wait_for_commit(root, cur_trans);
494 BUG_ON(ret);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400495
496 mutex_lock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400497 put_transaction(cur_trans);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400498 mutex_unlock(&root->fs_info->trans_mutex);
499
Chris Masonccd467d2007-06-28 15:57:36 -0400500 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400501 return 0;
502 }
Chris Mason2c90e5d2007-04-02 10:50:19 -0400503 trans->transaction->in_commit = 1;
Chris Masonccd467d2007-06-28 15:57:36 -0400504 cur_trans = trans->transaction;
505 if (cur_trans->list.prev != &root->fs_info->trans_list) {
506 prev_trans = list_entry(cur_trans->list.prev,
507 struct btrfs_transaction, list);
508 if (!prev_trans->commit_done) {
509 prev_trans->use_count++;
510 mutex_unlock(&root->fs_info->fs_mutex);
511 mutex_unlock(&root->fs_info->trans_mutex);
512
513 wait_for_commit(root, prev_trans);
Chris Masonccd467d2007-06-28 15:57:36 -0400514
515 mutex_lock(&root->fs_info->fs_mutex);
516 mutex_lock(&root->fs_info->trans_mutex);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400517 put_transaction(prev_trans);
Chris Masonccd467d2007-06-28 15:57:36 -0400518 }
519 }
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400520
521 do {
522 joined = cur_trans->num_joined;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400523 WARN_ON(cur_trans != trans->transaction);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400524 prepare_to_wait(&cur_trans->writer_wait, &wait,
Chris Mason79154b12007-03-22 15:59:16 -0400525 TASK_UNINTERRUPTIBLE);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400526
527 if (cur_trans->num_writers > 1)
528 timeout = MAX_SCHEDULE_TIMEOUT;
529 else
530 timeout = 1;
531
Chris Masonccd467d2007-06-28 15:57:36 -0400532 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400533 mutex_unlock(&root->fs_info->trans_mutex);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400534
535 schedule_timeout(timeout);
536
Chris Masonccd467d2007-06-28 15:57:36 -0400537 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400538 mutex_lock(&root->fs_info->trans_mutex);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400539 finish_wait(&cur_trans->writer_wait, &wait);
540 } while (cur_trans->num_writers > 1 ||
541 (cur_trans->num_joined != joined));
542
Chris Mason2c90e5d2007-04-02 10:50:19 -0400543 WARN_ON(cur_trans != trans->transaction);
Chris Mason54aa1f42007-06-22 14:16:25 -0400544 ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix,
545 &dirty_fs_roots);
546 BUG_ON(ret);
547
Chris Mason79154b12007-03-22 15:59:16 -0400548 ret = btrfs_commit_tree_roots(trans, root);
549 BUG_ON(ret);
Chris Mason54aa1f42007-06-22 14:16:25 -0400550
Chris Mason78fae272007-03-25 11:35:08 -0400551 cur_trans = root->fs_info->running_transaction;
552 root->fs_info->running_transaction = NULL;
Chris Mason4b52dff2007-06-26 10:06:50 -0400553 btrfs_set_super_generation(&root->fs_info->super_copy,
554 cur_trans->transid);
555 btrfs_set_super_root(&root->fs_info->super_copy,
556 bh_blocknr(root->fs_info->tree_root->node));
557 memcpy(root->fs_info->disk_super, &root->fs_info->super_copy,
558 sizeof(root->fs_info->super_copy));
Chris Masonccd467d2007-06-28 15:57:36 -0400559
560 btrfs_copy_pinned(root, &pinned_copy);
561
Chris Mason78fae272007-03-25 11:35:08 -0400562 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason8fd17792007-04-19 21:01:03 -0400563 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400564 ret = btrfs_write_and_wait_transaction(trans, root);
565 BUG_ON(ret);
Chris Mason79154b12007-03-22 15:59:16 -0400566 write_ctree_super(trans, root);
Chris Mason8fd17792007-04-19 21:01:03 -0400567 mutex_lock(&root->fs_info->fs_mutex);
Chris Masonccd467d2007-06-28 15:57:36 -0400568 btrfs_finish_extent_commit(trans, root, &pinned_copy);
Chris Mason78fae272007-03-25 11:35:08 -0400569 mutex_lock(&root->fs_info->trans_mutex);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400570 cur_trans->commit_done = 1;
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400571 root->fs_info->last_trans_committed = cur_trans->transid;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400572 wake_up(&cur_trans->commit_wait);
Chris Mason79154b12007-03-22 15:59:16 -0400573 put_transaction(cur_trans);
Chris Mason78fae272007-03-25 11:35:08 -0400574 put_transaction(cur_trans);
Josef Bacik58176a92007-08-29 15:47:34 -0400575
Chris Masonfacda1e2007-06-08 18:11:48 -0400576 if (root->fs_info->closing)
577 list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots);
578 else
579 list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots);
Josef Bacik58176a92007-08-29 15:47:34 -0400580
Chris Mason78fae272007-03-25 11:35:08 -0400581 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400582 kmem_cache_free(btrfs_trans_handle_cachep, trans);
Chris Mason79154b12007-03-22 15:59:16 -0400583
Chris Masonfacda1e2007-06-08 18:11:48 -0400584 if (root->fs_info->closing) {
585 mutex_unlock(&root->fs_info->fs_mutex);
586 drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);
587 mutex_lock(&root->fs_info->fs_mutex);
588 }
Chris Mason79154b12007-03-22 15:59:16 -0400589 return ret;
590}
591
Chris Masone9d0b132007-08-10 14:06:19 -0400592int btrfs_clean_old_snapshots(struct btrfs_root *root)
593{
594 struct list_head dirty_roots;
595 INIT_LIST_HEAD(&dirty_roots);
596
597 mutex_lock(&root->fs_info->trans_mutex);
598 list_splice_init(&root->fs_info->dead_roots, &dirty_roots);
599 mutex_unlock(&root->fs_info->trans_mutex);
600
601 if (!list_empty(&dirty_roots)) {
602 drop_dirty_roots(root, &dirty_roots);
603 }
604 return 0;
605}
Chris Mason08607c12007-06-08 15:33:54 -0400606void btrfs_transaction_cleaner(struct work_struct *work)
607{
608 struct btrfs_fs_info *fs_info = container_of(work,
609 struct btrfs_fs_info,
610 trans_work.work);
611
612 struct btrfs_root *root = fs_info->tree_root;
613 struct btrfs_transaction *cur;
614 struct btrfs_trans_handle *trans;
615 unsigned long now;
616 unsigned long delay = HZ * 30;
617 int ret;
618
Chris Mason08607c12007-06-08 15:33:54 -0400619 mutex_lock(&root->fs_info->fs_mutex);
620 mutex_lock(&root->fs_info->trans_mutex);
621 cur = root->fs_info->running_transaction;
622 if (!cur) {
623 mutex_unlock(&root->fs_info->trans_mutex);
624 goto out;
625 }
626 now = get_seconds();
627 if (now < cur->start_time || now - cur->start_time < 30) {
628 mutex_unlock(&root->fs_info->trans_mutex);
629 delay = HZ * 5;
630 goto out;
631 }
632 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason6702ed42007-08-07 16:15:09 -0400633 btrfs_defrag_dirty_roots(root->fs_info);
Chris Mason08607c12007-06-08 15:33:54 -0400634 trans = btrfs_start_transaction(root, 1);
635 ret = btrfs_commit_transaction(trans, root);
636out:
637 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masone9d0b132007-08-10 14:06:19 -0400638 btrfs_clean_old_snapshots(root);
Chris Mason08607c12007-06-08 15:33:54 -0400639 btrfs_transaction_queue_work(root, delay);
640}
641
642void btrfs_transaction_queue_work(struct btrfs_root *root, int delay)
643{
644 queue_delayed_work(trans_wq, &root->fs_info->trans_work, delay);
645}
646
647void btrfs_transaction_flush_work(struct btrfs_root *root)
648{
649 cancel_rearming_delayed_workqueue(trans_wq, &root->fs_info->trans_work);
650 flush_workqueue(trans_wq);
651}
652
653void __init btrfs_init_transaction_sys(void)
654{
655 trans_wq = create_workqueue("btrfs");
656}
657
658void __exit btrfs_exit_transaction_sys(void)
659{
660 destroy_workqueue(trans_wq);
661}
662