blob: 29755593de651c5dcac657996ee6b69fa703c1ed [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;
Josef Bacik15ee9bc2007-08-10 16:22:09 -040058 cur_trans->num_writers = 1;
59 cur_trans->num_joined = 0;
Chris Mason0f7d52f2007-04-09 10:42:37 -040060 cur_trans->transid = root->fs_info->generation;
Chris Mason79154b12007-03-22 15:59:16 -040061 init_waitqueue_head(&cur_trans->writer_wait);
62 init_waitqueue_head(&cur_trans->commit_wait);
63 cur_trans->in_commit = 0;
Chris Masond5719762007-03-23 10:01:08 -040064 cur_trans->use_count = 1;
Chris Mason79154b12007-03-22 15:59:16 -040065 cur_trans->commit_done = 0;
Chris Mason08607c12007-06-08 15:33:54 -040066 cur_trans->start_time = get_seconds();
Chris Mason8fd17792007-04-19 21:01:03 -040067 list_add_tail(&cur_trans->list, &root->fs_info->trans_list);
Chris Mason7c4452b2007-04-28 09:29:35 -040068 init_bit_radix(&cur_trans->dirty_pages);
Josef Bacik15ee9bc2007-08-10 16:22:09 -040069 } else {
70 cur_trans->num_writers++;
71 cur_trans->num_joined++;
Chris Mason79154b12007-03-22 15:59:16 -040072 }
Josef Bacik15ee9bc2007-08-10 16:22:09 -040073
Chris Mason79154b12007-03-22 15:59:16 -040074 return 0;
75}
76
Chris Mason6702ed42007-08-07 16:15:09 -040077static 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
Chris Mason79154b12007-03-22 15:59:16 -040099struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
100 int num_blocks)
101{
Chris Mason2c90e5d2007-04-02 10:50:19 -0400102 struct btrfs_trans_handle *h =
103 kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
Chris Mason79154b12007-03-22 15:59:16 -0400104 int ret;
105
106 mutex_lock(&root->fs_info->trans_mutex);
107 ret = join_transaction(root);
108 BUG_ON(ret);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400109
Chris Mason6702ed42007-08-07 16:15:09 -0400110 record_root_in_trans(root);
111 h->transid = root->fs_info->running_transaction->transid;
Chris Mason79154b12007-03-22 15:59:16 -0400112 h->transaction = root->fs_info->running_transaction;
113 h->blocks_reserved = num_blocks;
114 h->blocks_used = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400115 h->block_group = NULL;
Chris Mason26b80032007-08-08 20:17:12 -0400116 h->alloc_exclude_nr = 0;
117 h->alloc_exclude_start = 0;
Chris Mason79154b12007-03-22 15:59:16 -0400118 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;
Chris Masond6e4a422007-04-06 15:37:36 -0400127
Chris Mason79154b12007-03-22 15:59:16 -0400128 mutex_lock(&root->fs_info->trans_mutex);
129 cur_trans = root->fs_info->running_transaction;
Chris Masonccd467d2007-06-28 15:57:36 -0400130 WARN_ON(cur_trans != trans->transaction);
Chris Masond5719762007-03-23 10:01:08 -0400131 WARN_ON(cur_trans->num_writers < 1);
Chris Masonccd467d2007-06-28 15:57:36 -0400132 cur_trans->num_writers--;
Chris Mason79154b12007-03-22 15:59:16 -0400133 if (waitqueue_active(&cur_trans->writer_wait))
134 wake_up(&cur_trans->writer_wait);
Chris Mason79154b12007-03-22 15:59:16 -0400135 put_transaction(cur_trans);
136 mutex_unlock(&root->fs_info->trans_mutex);
Chris Masond6025572007-03-30 14:27:56 -0400137 memset(trans, 0, sizeof(*trans));
Chris Mason2c90e5d2007-04-02 10:50:19 -0400138 kmem_cache_free(btrfs_trans_handle_cachep, trans);
Chris Mason79154b12007-03-22 15:59:16 -0400139 return 0;
140}
141
142
143int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
144 struct btrfs_root *root)
145{
Chris Mason7c4452b2007-04-28 09:29:35 -0400146 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) {
Chris Masone37c9e62007-05-09 20:13:14 -0400160 ret = find_first_radix_bit(dirty_pages, gang,
161 0, ARRAY_SIZE(gang));
Chris Mason7c4452b2007-04-28 09:29:35 -0400162 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;
Chris Mason6702ed42007-08-07 16:15:09 -0400171 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 }
Chris Mason7c4452b2007-04-28 09:29:35 -0400180 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;
Chris Mason79154b12007-03-22 15:59:16 -0400190}
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;
Chris Mason79154b12007-03-22 15:59:16 -0400200
Chris Mason9078a3e2007-04-26 16:46:15 -0400201 btrfs_write_dirty_block_groups(trans, extent_root);
Chris Mason79154b12007-03-22 15:59:16 -0400202 while(1) {
203 old_extent_block = btrfs_root_blocknr(&extent_root->root_item);
Chris Mason7eccb902007-04-11 15:53:25 -0400204 if (old_extent_block == bh_blocknr(extent_root->node))
Chris Mason79154b12007-03-22 15:59:16 -0400205 break;
206 btrfs_set_root_blocknr(&extent_root->root_item,
Chris Mason7eccb902007-04-11 15:53:25 -0400207 bh_blocknr(extent_root->node));
Chris Mason79154b12007-03-22 15:59:16 -0400208 ret = btrfs_update_root(trans, tree_root,
209 &extent_root->root_key,
210 &extent_root->root_item);
211 BUG_ON(ret);
Chris Mason9078a3e2007-04-26 16:46:15 -0400212 btrfs_write_dirty_block_groups(trans, extent_root);
Chris Mason79154b12007-03-22 15:59:16 -0400213 }
214 return 0;
215}
216
217static int wait_for_commit(struct btrfs_root *root,
218 struct btrfs_transaction *commit)
219{
220 DEFINE_WAIT(wait);
Chris Masonccd467d2007-06-28 15:57:36 -0400221 mutex_lock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400222 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 }
Chris Masonccd467d2007-06-28 15:57:36 -0400231 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400232 finish_wait(&commit->commit_wait, &wait);
233 return 0;
234}
235
Chris Mason0f7d52f2007-04-09 10:42:37 -0400236struct dirty_root {
237 struct list_head list;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400238 struct btrfs_root *root;
Josef Bacik58176a92007-08-29 15:47:34 -0400239 struct btrfs_root *latest_root;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400240};
241
Chris Mason5ce14bb2007-09-11 11:15:39 -0400242int btrfs_add_dead_root(struct btrfs_root *root,
243 struct btrfs_root *latest,
244 struct list_head *dead_list)
Chris Mason5eda7b52007-06-22 14:16:25 -0400245{
246 struct dirty_root *dirty;
247
248 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
249 if (!dirty)
250 return -ENOMEM;
Chris Mason5eda7b52007-06-22 14:16:25 -0400251 dirty->root = root;
Chris Mason5ce14bb2007-09-11 11:15:39 -0400252 dirty->latest_root = latest;
Chris Mason5eda7b52007-06-22 14:16:25 -0400253 list_add(&dirty->list, dead_list);
254 return 0;
255}
256
Chris Mason35b7e472007-05-02 15:53:43 -0400257static int add_dirty_roots(struct btrfs_trans_handle *trans,
258 struct radix_tree_root *radix,
259 struct list_head *list)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400260{
261 struct dirty_root *dirty;
262 struct btrfs_root *gang[8];
263 struct btrfs_root *root;
264 int i;
265 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400266 int err = 0;
Chris Mason5eda7b52007-06-22 14:16:25 -0400267 u32 refs;
Chris Mason54aa1f42007-06-22 14:16:25 -0400268
Chris Mason0f7d52f2007-04-09 10:42:37 -0400269 while(1) {
270 ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0,
271 ARRAY_SIZE(gang),
272 BTRFS_ROOT_TRANS_TAG);
273 if (ret == 0)
274 break;
275 for (i = 0; i < ret; i++) {
276 root = gang[i];
Chris Mason2619ba12007-04-10 16:58:11 -0400277 radix_tree_tag_clear(radix,
278 (unsigned long)root->root_key.objectid,
279 BTRFS_ROOT_TRANS_TAG);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400280 if (root->commit_root == root->node) {
Chris Mason7eccb902007-04-11 15:53:25 -0400281 WARN_ON(bh_blocknr(root->node) !=
Chris Mason0f7d52f2007-04-09 10:42:37 -0400282 btrfs_root_blocknr(&root->root_item));
283 brelse(root->commit_root);
284 root->commit_root = NULL;
Josef Bacik58176a92007-08-29 15:47:34 -0400285
286 /* make sure to update the root on disk
287 * so we get any updates to the block used
288 * counts
289 */
290 err = btrfs_update_root(trans,
291 root->fs_info->tree_root,
292 &root->root_key,
293 &root->root_item);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400294 continue;
295 }
296 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
297 BUG_ON(!dirty);
Chris Mason9f3a7422007-08-07 15:52:19 -0400298 dirty->root = kmalloc(sizeof(*dirty->root), GFP_NOFS);
299 BUG_ON(!dirty->root);
300
301 memset(&root->root_item.drop_progress, 0,
302 sizeof(struct btrfs_disk_key));
303 root->root_item.drop_level = 0;
304
305 memcpy(dirty->root, root, sizeof(*root));
306 dirty->root->node = root->commit_root;
Josef Bacik58176a92007-08-29 15:47:34 -0400307 dirty->latest_root = root;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400308 root->commit_root = NULL;
Chris Mason5eda7b52007-06-22 14:16:25 -0400309
Chris Mason0f7d52f2007-04-09 10:42:37 -0400310 root->root_key.offset = root->fs_info->generation;
311 btrfs_set_root_blocknr(&root->root_item,
Chris Mason7eccb902007-04-11 15:53:25 -0400312 bh_blocknr(root->node));
Chris Mason0f7d52f2007-04-09 10:42:37 -0400313 err = btrfs_insert_root(trans, root->fs_info->tree_root,
314 &root->root_key,
315 &root->root_item);
Chris Mason54aa1f42007-06-22 14:16:25 -0400316 if (err)
317 break;
Chris Mason9f3a7422007-08-07 15:52:19 -0400318
319 refs = btrfs_root_refs(&dirty->root->root_item);
320 btrfs_set_root_refs(&dirty->root->root_item, refs - 1);
Chris Mason5eda7b52007-06-22 14:16:25 -0400321 err = btrfs_update_root(trans, root->fs_info->tree_root,
Chris Mason9f3a7422007-08-07 15:52:19 -0400322 &dirty->root->root_key,
323 &dirty->root->root_item);
Chris Mason5eda7b52007-06-22 14:16:25 -0400324
325 BUG_ON(err);
Chris Mason9f3a7422007-08-07 15:52:19 -0400326 if (refs == 1) {
Chris Mason5eda7b52007-06-22 14:16:25 -0400327 list_add(&dirty->list, list);
Chris Mason9f3a7422007-08-07 15:52:19 -0400328 } else {
329 WARN_ON(1);
330 kfree(dirty->root);
Chris Mason5eda7b52007-06-22 14:16:25 -0400331 kfree(dirty);
Chris Mason9f3a7422007-08-07 15:52:19 -0400332 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400333 }
334 }
Chris Mason54aa1f42007-06-22 14:16:25 -0400335 return err;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400336}
337
Chris Masone9d0b132007-08-10 14:06:19 -0400338int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
339{
340 struct btrfs_fs_info *info = root->fs_info;
341 int ret;
342 struct btrfs_trans_handle *trans;
343
344 if (root->defrag_running)
345 return 0;
346
347 trans = btrfs_start_transaction(root, 1);
348 while (1) {
349 root->defrag_running = 1;
350 ret = btrfs_defrag_leaves(trans, root, cacheonly);
351 btrfs_end_transaction(trans, root);
352 mutex_unlock(&info->fs_mutex);
353
354 btrfs_btree_balance_dirty(root);
355 cond_resched();
356
357 mutex_lock(&info->fs_mutex);
358 trans = btrfs_start_transaction(root, 1);
359 if (ret != -EAGAIN)
360 break;
361 }
362 root->defrag_running = 0;
363 radix_tree_tag_clear(&info->fs_roots_radix,
364 (unsigned long)root->root_key.objectid,
365 BTRFS_ROOT_DEFRAG_TAG);
366 btrfs_end_transaction(trans, root);
367 return 0;
368}
369
Chris Mason6702ed42007-08-07 16:15:09 -0400370int btrfs_defrag_dirty_roots(struct btrfs_fs_info *info)
371{
372 struct btrfs_root *gang[1];
373 struct btrfs_root *root;
Chris Mason6702ed42007-08-07 16:15:09 -0400374 int i;
375 int ret;
376 int err = 0;
377 u64 last = 0;
378
Chris Mason6702ed42007-08-07 16:15:09 -0400379 while(1) {
380 ret = radix_tree_gang_lookup_tag(&info->fs_roots_radix,
381 (void **)gang, last,
382 ARRAY_SIZE(gang),
383 BTRFS_ROOT_DEFRAG_TAG);
384 if (ret == 0)
385 break;
386 for (i = 0; i < ret; i++) {
387 root = gang[i];
388 last = root->root_key.objectid + 1;
Chris Masone9d0b132007-08-10 14:06:19 -0400389 btrfs_defrag_root(root, 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400390 }
391 }
Chris Masone9d0b132007-08-10 14:06:19 -0400392 btrfs_defrag_root(info->extent_root, 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400393 return err;
394}
395
Chris Mason35b7e472007-05-02 15:53:43 -0400396static int drop_dirty_roots(struct btrfs_root *tree_root,
397 struct list_head *list)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400398{
399 struct dirty_root *dirty;
400 struct btrfs_trans_handle *trans;
Josef Bacik58176a92007-08-29 15:47:34 -0400401 u64 num_blocks;
402 u64 blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -0400403 int ret = 0;
Chris Mason9f3a7422007-08-07 15:52:19 -0400404 int err;
405
Chris Mason0f7d52f2007-04-09 10:42:37 -0400406 while(!list_empty(list)) {
Josef Bacik58176a92007-08-29 15:47:34 -0400407 struct btrfs_root *root;
408
Chris Masonfacda1e2007-06-08 18:11:48 -0400409 mutex_lock(&tree_root->fs_info->fs_mutex);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400410 dirty = list_entry(list->next, struct dirty_root, list);
411 list_del_init(&dirty->list);
Chris Mason5eda7b52007-06-22 14:16:25 -0400412
Josef Bacik58176a92007-08-29 15:47:34 -0400413 num_blocks = btrfs_root_blocks_used(&dirty->root->root_item);
414 root = dirty->latest_root;
415
Chris Mason9f3a7422007-08-07 15:52:19 -0400416 while(1) {
417 trans = btrfs_start_transaction(tree_root, 1);
418 ret = btrfs_drop_snapshot(trans, dirty->root);
419 if (ret != -EAGAIN) {
420 break;
421 }
Josef Bacik58176a92007-08-29 15:47:34 -0400422
Chris Mason9f3a7422007-08-07 15:52:19 -0400423 err = btrfs_update_root(trans,
424 tree_root,
425 &dirty->root->root_key,
426 &dirty->root->root_item);
427 if (err)
428 ret = err;
429 ret = btrfs_end_transaction(trans, tree_root);
430 BUG_ON(ret);
Chris Masonf4468e92007-08-08 10:08:58 -0400431 mutex_unlock(&tree_root->fs_info->fs_mutex);
432
433 btrfs_btree_balance_dirty(tree_root);
434 schedule();
435
436 mutex_lock(&tree_root->fs_info->fs_mutex);
Chris Mason9f3a7422007-08-07 15:52:19 -0400437 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400438 BUG_ON(ret);
Josef Bacik58176a92007-08-29 15:47:34 -0400439
440 num_blocks -= btrfs_root_blocks_used(&dirty->root->root_item);
441 blocks_used = btrfs_root_blocks_used(&root->root_item);
442 if (num_blocks) {
443 record_root_in_trans(root);
444 btrfs_set_root_blocks_used(&root->root_item,
445 blocks_used - num_blocks);
446 }
Chris Mason9f3a7422007-08-07 15:52:19 -0400447 ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key);
Josef Bacik58176a92007-08-29 15:47:34 -0400448 if (ret) {
449 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -0400450 break;
Josef Bacik58176a92007-08-29 15:47:34 -0400451 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400452 ret = btrfs_end_transaction(trans, tree_root);
453 BUG_ON(ret);
Chris Mason5eda7b52007-06-22 14:16:25 -0400454
Chris Mason9f3a7422007-08-07 15:52:19 -0400455 kfree(dirty->root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400456 kfree(dirty);
Chris Masonfacda1e2007-06-08 18:11:48 -0400457 mutex_unlock(&tree_root->fs_info->fs_mutex);
Chris Mason8c2383c2007-06-18 09:57:58 -0400458 btrfs_btree_balance_dirty(tree_root);
Chris Masonf4468e92007-08-08 10:08:58 -0400459 schedule();
Chris Mason0f7d52f2007-04-09 10:42:37 -0400460 }
Chris Mason54aa1f42007-06-22 14:16:25 -0400461 return ret;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400462}
463
Chris Mason79154b12007-03-22 15:59:16 -0400464int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
465 struct btrfs_root *root)
466{
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400467 unsigned long joined = 0;
468 unsigned long timeout = 1;
Chris Mason79154b12007-03-22 15:59:16 -0400469 struct btrfs_transaction *cur_trans;
Chris Mason8fd17792007-04-19 21:01:03 -0400470 struct btrfs_transaction *prev_trans = NULL;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400471 struct list_head dirty_fs_roots;
Chris Masonccd467d2007-06-28 15:57:36 -0400472 struct radix_tree_root pinned_copy;
Chris Mason79154b12007-03-22 15:59:16 -0400473 DEFINE_WAIT(wait);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400474 int ret;
Chris Mason79154b12007-03-22 15:59:16 -0400475
Chris Masonccd467d2007-06-28 15:57:36 -0400476 init_bit_radix(&pinned_copy);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400477 INIT_LIST_HEAD(&dirty_fs_roots);
Chris Masond6e4a422007-04-06 15:37:36 -0400478
Chris Mason79154b12007-03-22 15:59:16 -0400479 mutex_lock(&root->fs_info->trans_mutex);
480 if (trans->transaction->in_commit) {
481 cur_trans = trans->transaction;
482 trans->transaction->use_count++;
Chris Masonccd467d2007-06-28 15:57:36 -0400483 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400484 btrfs_end_transaction(trans, root);
Chris Masonccd467d2007-06-28 15:57:36 -0400485
486 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400487 ret = wait_for_commit(root, cur_trans);
488 BUG_ON(ret);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400489
490 mutex_lock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400491 put_transaction(cur_trans);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400492 mutex_unlock(&root->fs_info->trans_mutex);
493
Chris Masonccd467d2007-06-28 15:57:36 -0400494 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400495 return 0;
496 }
Chris Mason2c90e5d2007-04-02 10:50:19 -0400497 trans->transaction->in_commit = 1;
Chris Masonccd467d2007-06-28 15:57:36 -0400498 cur_trans = trans->transaction;
499 if (cur_trans->list.prev != &root->fs_info->trans_list) {
500 prev_trans = list_entry(cur_trans->list.prev,
501 struct btrfs_transaction, list);
502 if (!prev_trans->commit_done) {
503 prev_trans->use_count++;
504 mutex_unlock(&root->fs_info->fs_mutex);
505 mutex_unlock(&root->fs_info->trans_mutex);
506
507 wait_for_commit(root, prev_trans);
Chris Masonccd467d2007-06-28 15:57:36 -0400508
509 mutex_lock(&root->fs_info->fs_mutex);
510 mutex_lock(&root->fs_info->trans_mutex);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400511 put_transaction(prev_trans);
Chris Masonccd467d2007-06-28 15:57:36 -0400512 }
513 }
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400514
515 do {
516 joined = cur_trans->num_joined;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400517 WARN_ON(cur_trans != trans->transaction);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400518 prepare_to_wait(&cur_trans->writer_wait, &wait,
Chris Mason79154b12007-03-22 15:59:16 -0400519 TASK_UNINTERRUPTIBLE);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400520
521 if (cur_trans->num_writers > 1)
522 timeout = MAX_SCHEDULE_TIMEOUT;
523 else
524 timeout = 1;
525
Chris Masonccd467d2007-06-28 15:57:36 -0400526 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400527 mutex_unlock(&root->fs_info->trans_mutex);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400528
529 schedule_timeout(timeout);
530
Chris Masonccd467d2007-06-28 15:57:36 -0400531 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400532 mutex_lock(&root->fs_info->trans_mutex);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400533 finish_wait(&cur_trans->writer_wait, &wait);
534 } while (cur_trans->num_writers > 1 ||
535 (cur_trans->num_joined != joined));
536
Chris Mason2c90e5d2007-04-02 10:50:19 -0400537 WARN_ON(cur_trans != trans->transaction);
Chris Mason54aa1f42007-06-22 14:16:25 -0400538 ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix,
539 &dirty_fs_roots);
540 BUG_ON(ret);
541
Chris Mason79154b12007-03-22 15:59:16 -0400542 ret = btrfs_commit_tree_roots(trans, root);
543 BUG_ON(ret);
Chris Mason54aa1f42007-06-22 14:16:25 -0400544
Chris Mason78fae272007-03-25 11:35:08 -0400545 cur_trans = root->fs_info->running_transaction;
546 root->fs_info->running_transaction = NULL;
Chris Mason4b52dff2007-06-26 10:06:50 -0400547 btrfs_set_super_generation(&root->fs_info->super_copy,
548 cur_trans->transid);
549 btrfs_set_super_root(&root->fs_info->super_copy,
550 bh_blocknr(root->fs_info->tree_root->node));
551 memcpy(root->fs_info->disk_super, &root->fs_info->super_copy,
552 sizeof(root->fs_info->super_copy));
Chris Masonccd467d2007-06-28 15:57:36 -0400553
554 btrfs_copy_pinned(root, &pinned_copy);
555
Chris Mason78fae272007-03-25 11:35:08 -0400556 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason8fd17792007-04-19 21:01:03 -0400557 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400558 ret = btrfs_write_and_wait_transaction(trans, root);
559 BUG_ON(ret);
Chris Mason79154b12007-03-22 15:59:16 -0400560 write_ctree_super(trans, root);
Chris Mason8fd17792007-04-19 21:01:03 -0400561 mutex_lock(&root->fs_info->fs_mutex);
Chris Masonccd467d2007-06-28 15:57:36 -0400562 btrfs_finish_extent_commit(trans, root, &pinned_copy);
Chris Mason78fae272007-03-25 11:35:08 -0400563 mutex_lock(&root->fs_info->trans_mutex);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400564 cur_trans->commit_done = 1;
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400565 root->fs_info->last_trans_committed = cur_trans->transid;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400566 wake_up(&cur_trans->commit_wait);
Chris Mason79154b12007-03-22 15:59:16 -0400567 put_transaction(cur_trans);
Chris Mason78fae272007-03-25 11:35:08 -0400568 put_transaction(cur_trans);
Josef Bacik58176a92007-08-29 15:47:34 -0400569
Chris Masonfacda1e2007-06-08 18:11:48 -0400570 if (root->fs_info->closing)
571 list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots);
572 else
573 list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots);
Josef Bacik58176a92007-08-29 15:47:34 -0400574
Chris Mason78fae272007-03-25 11:35:08 -0400575 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400576 kmem_cache_free(btrfs_trans_handle_cachep, trans);
Chris Mason79154b12007-03-22 15:59:16 -0400577
Chris Masonfacda1e2007-06-08 18:11:48 -0400578 if (root->fs_info->closing) {
579 mutex_unlock(&root->fs_info->fs_mutex);
580 drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);
581 mutex_lock(&root->fs_info->fs_mutex);
582 }
Chris Mason79154b12007-03-22 15:59:16 -0400583 return ret;
584}
585
Chris Masone9d0b132007-08-10 14:06:19 -0400586int btrfs_clean_old_snapshots(struct btrfs_root *root)
587{
588 struct list_head dirty_roots;
589 INIT_LIST_HEAD(&dirty_roots);
590
591 mutex_lock(&root->fs_info->trans_mutex);
592 list_splice_init(&root->fs_info->dead_roots, &dirty_roots);
593 mutex_unlock(&root->fs_info->trans_mutex);
594
595 if (!list_empty(&dirty_roots)) {
596 drop_dirty_roots(root, &dirty_roots);
597 }
598 return 0;
599}
Chris Mason08607c12007-06-08 15:33:54 -0400600void btrfs_transaction_cleaner(struct work_struct *work)
601{
602 struct btrfs_fs_info *fs_info = container_of(work,
603 struct btrfs_fs_info,
604 trans_work.work);
605
606 struct btrfs_root *root = fs_info->tree_root;
607 struct btrfs_transaction *cur;
608 struct btrfs_trans_handle *trans;
609 unsigned long now;
610 unsigned long delay = HZ * 30;
611 int ret;
612
Chris Mason08607c12007-06-08 15:33:54 -0400613 mutex_lock(&root->fs_info->fs_mutex);
614 mutex_lock(&root->fs_info->trans_mutex);
615 cur = root->fs_info->running_transaction;
616 if (!cur) {
617 mutex_unlock(&root->fs_info->trans_mutex);
618 goto out;
619 }
620 now = get_seconds();
621 if (now < cur->start_time || now - cur->start_time < 30) {
622 mutex_unlock(&root->fs_info->trans_mutex);
623 delay = HZ * 5;
624 goto out;
625 }
626 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason6702ed42007-08-07 16:15:09 -0400627 btrfs_defrag_dirty_roots(root->fs_info);
Chris Mason08607c12007-06-08 15:33:54 -0400628 trans = btrfs_start_transaction(root, 1);
629 ret = btrfs_commit_transaction(trans, root);
630out:
631 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masone9d0b132007-08-10 14:06:19 -0400632 btrfs_clean_old_snapshots(root);
Chris Mason08607c12007-06-08 15:33:54 -0400633 btrfs_transaction_queue_work(root, delay);
634}
635
636void btrfs_transaction_queue_work(struct btrfs_root *root, int delay)
637{
638 queue_delayed_work(trans_wq, &root->fs_info->trans_work, delay);
639}
640
641void btrfs_transaction_flush_work(struct btrfs_root *root)
642{
643 cancel_rearming_delayed_workqueue(trans_wq, &root->fs_info->trans_work);
644 flush_workqueue(trans_wq);
645}
646
647void __init btrfs_init_transaction_sys(void)
648{
649 trans_wq = create_workqueue("btrfs");
650}
651
652void __exit btrfs_exit_transaction_sys(void)
653{
654 destroy_workqueue(trans_wq);
655}
656