blob: 5a1ee0665ae890d6603ee4cb3cc237ae86888635 [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 Mason5f39d392007-10-15 16:14:19 -040022#include <linux/pagemap.h>
Chris Mason79154b12007-03-22 15:59:16 -040023#include "ctree.h"
24#include "disk-io.h"
25#include "transaction.h"
Chris Mason925baed2008-06-25 16:01:30 -040026#include "locking.h"
Chris Mason79154b12007-03-22 15:59:16 -040027
Chris Mason78fae272007-03-25 11:35:08 -040028static int total_trans = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -040029extern struct kmem_cache *btrfs_trans_handle_cachep;
30extern struct kmem_cache *btrfs_transaction_cachep;
31
Chris Mason08607c12007-06-08 15:33:54 -040032static struct workqueue_struct *trans_wq;
33
Chris Mason0f7d52f2007-04-09 10:42:37 -040034#define BTRFS_ROOT_TRANS_TAG 0
Chris Mason6702ed42007-08-07 16:15:09 -040035#define BTRFS_ROOT_DEFRAG_TAG 1
Chris Mason0f7d52f2007-04-09 10:42:37 -040036
Chris Mason80b67942008-02-01 16:35:04 -050037static noinline void put_transaction(struct btrfs_transaction *transaction)
Chris Mason79154b12007-03-22 15:59:16 -040038{
Chris Mason2c90e5d2007-04-02 10:50:19 -040039 WARN_ON(transaction->use_count == 0);
Chris Mason79154b12007-03-22 15:59:16 -040040 transaction->use_count--;
Chris Mason78fae272007-03-25 11:35:08 -040041 if (transaction->use_count == 0) {
42 WARN_ON(total_trans == 0);
43 total_trans--;
Chris Mason8fd17792007-04-19 21:01:03 -040044 list_del_init(&transaction->list);
Chris Mason2c90e5d2007-04-02 10:50:19 -040045 memset(transaction, 0, sizeof(*transaction));
46 kmem_cache_free(btrfs_transaction_cachep, transaction);
Chris Mason78fae272007-03-25 11:35:08 -040047 }
Chris Mason79154b12007-03-22 15:59:16 -040048}
49
Chris Mason80b67942008-02-01 16:35:04 -050050static noinline int join_transaction(struct btrfs_root *root)
Chris Mason79154b12007-03-22 15:59:16 -040051{
52 struct btrfs_transaction *cur_trans;
53 cur_trans = root->fs_info->running_transaction;
54 if (!cur_trans) {
Chris Mason2c90e5d2007-04-02 10:50:19 -040055 cur_trans = kmem_cache_alloc(btrfs_transaction_cachep,
56 GFP_NOFS);
Chris Mason78fae272007-03-25 11:35:08 -040057 total_trans++;
Chris Mason79154b12007-03-22 15:59:16 -040058 BUG_ON(!cur_trans);
Chris Mason0f7d52f2007-04-09 10:42:37 -040059 root->fs_info->generation++;
Chris Masone18e4802008-01-18 10:54:22 -050060 root->fs_info->last_alloc = 0;
Chris Mason4529ba42008-01-31 16:45:07 -050061 root->fs_info->last_data_alloc = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -040062 cur_trans->num_writers = 1;
63 cur_trans->num_joined = 0;
Chris Mason0f7d52f2007-04-09 10:42:37 -040064 cur_trans->transid = root->fs_info->generation;
Chris Mason79154b12007-03-22 15:59:16 -040065 init_waitqueue_head(&cur_trans->writer_wait);
66 init_waitqueue_head(&cur_trans->commit_wait);
67 cur_trans->in_commit = 0;
Chris Masond5719762007-03-23 10:01:08 -040068 cur_trans->use_count = 1;
Chris Mason79154b12007-03-22 15:59:16 -040069 cur_trans->commit_done = 0;
Chris Mason08607c12007-06-08 15:33:54 -040070 cur_trans->start_time = get_seconds();
Chris Mason3063d292008-01-08 15:46:30 -050071 INIT_LIST_HEAD(&cur_trans->pending_snapshots);
Chris Mason8fd17792007-04-19 21:01:03 -040072 list_add_tail(&cur_trans->list, &root->fs_info->trans_list);
Chris Masondc17ff82008-01-08 15:46:30 -050073 btrfs_ordered_inode_tree_init(&cur_trans->ordered_inode_tree);
Chris Masond1310b22008-01-24 16:13:08 -050074 extent_io_tree_init(&cur_trans->dirty_pages,
Chris Mason5f39d392007-10-15 16:14:19 -040075 root->fs_info->btree_inode->i_mapping,
76 GFP_NOFS);
Chris Mason48ec2cf2008-06-09 09:35:50 -040077 spin_lock(&root->fs_info->new_trans_lock);
78 root->fs_info->running_transaction = cur_trans;
79 spin_unlock(&root->fs_info->new_trans_lock);
Josef Bacik15ee9bc2007-08-10 16:22:09 -040080 } else {
81 cur_trans->num_writers++;
82 cur_trans->num_joined++;
Chris Mason79154b12007-03-22 15:59:16 -040083 }
Josef Bacik15ee9bc2007-08-10 16:22:09 -040084
Chris Mason79154b12007-03-22 15:59:16 -040085 return 0;
86}
87
Chris Mason80b67942008-02-01 16:35:04 -050088static noinline int record_root_in_trans(struct btrfs_root *root)
Chris Mason6702ed42007-08-07 16:15:09 -040089{
90 u64 running_trans_id = root->fs_info->running_transaction->transid;
91 if (root->ref_cows && root->last_trans < running_trans_id) {
92 WARN_ON(root == root->fs_info->extent_root);
93 if (root->root_item.refs != 0) {
94 radix_tree_tag_set(&root->fs_info->fs_roots_radix,
95 (unsigned long)root->root_key.objectid,
96 BTRFS_ROOT_TRANS_TAG);
97 radix_tree_tag_set(&root->fs_info->fs_roots_radix,
98 (unsigned long)root->root_key.objectid,
99 BTRFS_ROOT_DEFRAG_TAG);
Chris Mason925baed2008-06-25 16:01:30 -0400100 root->commit_root = btrfs_root_node(root);
Chris Mason6702ed42007-08-07 16:15:09 -0400101 } else {
102 WARN_ON(1);
103 }
104 root->last_trans = running_trans_id;
105 }
106 return 0;
107}
108
Chris Mason79154b12007-03-22 15:59:16 -0400109struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
110 int num_blocks)
111{
Chris Mason2c90e5d2007-04-02 10:50:19 -0400112 struct btrfs_trans_handle *h =
113 kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
Chris Mason79154b12007-03-22 15:59:16 -0400114 int ret;
115
116 mutex_lock(&root->fs_info->trans_mutex);
117 ret = join_transaction(root);
118 BUG_ON(ret);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400119
Chris Mason6702ed42007-08-07 16:15:09 -0400120 record_root_in_trans(root);
121 h->transid = root->fs_info->running_transaction->transid;
Chris Mason79154b12007-03-22 15:59:16 -0400122 h->transaction = root->fs_info->running_transaction;
123 h->blocks_reserved = num_blocks;
124 h->blocks_used = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400125 h->block_group = NULL;
Chris Mason26b80032007-08-08 20:17:12 -0400126 h->alloc_exclude_nr = 0;
127 h->alloc_exclude_start = 0;
Chris Mason79154b12007-03-22 15:59:16 -0400128 root->fs_info->running_transaction->use_count++;
129 mutex_unlock(&root->fs_info->trans_mutex);
130 return h;
131}
132
133int btrfs_end_transaction(struct btrfs_trans_handle *trans,
134 struct btrfs_root *root)
135{
136 struct btrfs_transaction *cur_trans;
Chris Masond6e4a422007-04-06 15:37:36 -0400137
Chris Mason79154b12007-03-22 15:59:16 -0400138 mutex_lock(&root->fs_info->trans_mutex);
139 cur_trans = root->fs_info->running_transaction;
Chris Masonccd467d2007-06-28 15:57:36 -0400140 WARN_ON(cur_trans != trans->transaction);
Chris Masond5719762007-03-23 10:01:08 -0400141 WARN_ON(cur_trans->num_writers < 1);
Chris Masonccd467d2007-06-28 15:57:36 -0400142 cur_trans->num_writers--;
Chris Mason79154b12007-03-22 15:59:16 -0400143 if (waitqueue_active(&cur_trans->writer_wait))
144 wake_up(&cur_trans->writer_wait);
Chris Mason79154b12007-03-22 15:59:16 -0400145 put_transaction(cur_trans);
146 mutex_unlock(&root->fs_info->trans_mutex);
Chris Masond6025572007-03-30 14:27:56 -0400147 memset(trans, 0, sizeof(*trans));
Chris Mason2c90e5d2007-04-02 10:50:19 -0400148 kmem_cache_free(btrfs_trans_handle_cachep, trans);
Chris Mason79154b12007-03-22 15:59:16 -0400149 return 0;
150}
151
152
153int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
154 struct btrfs_root *root)
155{
Chris Mason7c4452b2007-04-28 09:29:35 -0400156 int ret;
Chris Mason7c4452b2007-04-28 09:29:35 -0400157 int err;
158 int werr = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500159 struct extent_io_tree *dirty_pages;
Chris Mason7c4452b2007-04-28 09:29:35 -0400160 struct page *page;
Chris Mason7c4452b2007-04-28 09:29:35 -0400161 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Mason5f39d392007-10-15 16:14:19 -0400162 u64 start;
163 u64 end;
164 unsigned long index;
Chris Mason7c4452b2007-04-28 09:29:35 -0400165
166 if (!trans || !trans->transaction) {
167 return filemap_write_and_wait(btree_inode->i_mapping);
168 }
169 dirty_pages = &trans->transaction->dirty_pages;
170 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400171 ret = find_first_extent_bit(dirty_pages, 0, &start, &end,
172 EXTENT_DIRTY);
173 if (ret)
Chris Mason7c4452b2007-04-28 09:29:35 -0400174 break;
Chris Mason5f39d392007-10-15 16:14:19 -0400175 clear_extent_dirty(dirty_pages, start, end, GFP_NOFS);
176 while(start <= end) {
177 index = start >> PAGE_CACHE_SHIFT;
Chris Mason35ebb932007-10-30 16:56:53 -0400178 start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
Chris Mason5f39d392007-10-15 16:14:19 -0400179 page = find_lock_page(btree_inode->i_mapping, index);
Chris Mason7c4452b2007-04-28 09:29:35 -0400180 if (!page)
181 continue;
Chris Mason6702ed42007-08-07 16:15:09 -0400182 if (PageWriteback(page)) {
183 if (PageDirty(page))
184 wait_on_page_writeback(page);
185 else {
186 unlock_page(page);
187 page_cache_release(page);
188 continue;
189 }
190 }
Chris Mason7c4452b2007-04-28 09:29:35 -0400191 err = write_one_page(page, 0);
192 if (err)
193 werr = err;
194 page_cache_release(page);
195 }
196 }
197 err = filemap_fdatawait(btree_inode->i_mapping);
198 if (err)
199 werr = err;
200 return werr;
Chris Mason79154b12007-03-22 15:59:16 -0400201}
202
Chris Mason0b86a832008-03-24 15:01:56 -0400203static int update_cowonly_root(struct btrfs_trans_handle *trans,
204 struct btrfs_root *root)
205{
206 int ret;
207 u64 old_root_bytenr;
208 struct btrfs_root *tree_root = root->fs_info->tree_root;
209
210 btrfs_write_dirty_block_groups(trans, root);
211 while(1) {
212 old_root_bytenr = btrfs_root_bytenr(&root->root_item);
213 if (old_root_bytenr == root->node->start)
214 break;
215 btrfs_set_root_bytenr(&root->root_item,
216 root->node->start);
217 btrfs_set_root_level(&root->root_item,
218 btrfs_header_level(root->node));
219 ret = btrfs_update_root(trans, tree_root,
220 &root->root_key,
221 &root->root_item);
222 BUG_ON(ret);
223 btrfs_write_dirty_block_groups(trans, root);
224 }
225 return 0;
226}
227
Chris Mason79154b12007-03-22 15:59:16 -0400228int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans,
229 struct btrfs_root *root)
230{
Chris Mason79154b12007-03-22 15:59:16 -0400231 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason0b86a832008-03-24 15:01:56 -0400232 struct list_head *next;
Chris Mason79154b12007-03-22 15:59:16 -0400233
Chris Mason0b86a832008-03-24 15:01:56 -0400234 while(!list_empty(&fs_info->dirty_cowonly_roots)) {
235 next = fs_info->dirty_cowonly_roots.next;
236 list_del_init(next);
237 root = list_entry(next, struct btrfs_root, dirty_list);
238 update_cowonly_root(trans, root);
Chris Mason79154b12007-03-22 15:59:16 -0400239 }
240 return 0;
241}
242
Chris Mason80b67942008-02-01 16:35:04 -0500243static noinline int wait_for_commit(struct btrfs_root *root,
244 struct btrfs_transaction *commit)
Chris Mason79154b12007-03-22 15:59:16 -0400245{
246 DEFINE_WAIT(wait);
Chris Masonccd467d2007-06-28 15:57:36 -0400247 mutex_lock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400248 while(!commit->commit_done) {
249 prepare_to_wait(&commit->commit_wait, &wait,
250 TASK_UNINTERRUPTIBLE);
251 if (commit->commit_done)
252 break;
253 mutex_unlock(&root->fs_info->trans_mutex);
254 schedule();
255 mutex_lock(&root->fs_info->trans_mutex);
256 }
Chris Masonccd467d2007-06-28 15:57:36 -0400257 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400258 finish_wait(&commit->commit_wait, &wait);
259 return 0;
260}
261
Chris Mason0f7d52f2007-04-09 10:42:37 -0400262struct dirty_root {
263 struct list_head list;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400264 struct btrfs_root *root;
Josef Bacik58176a92007-08-29 15:47:34 -0400265 struct btrfs_root *latest_root;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400266};
267
Chris Mason5ce14bb2007-09-11 11:15:39 -0400268int btrfs_add_dead_root(struct btrfs_root *root,
269 struct btrfs_root *latest,
270 struct list_head *dead_list)
Chris Mason5eda7b52007-06-22 14:16:25 -0400271{
272 struct dirty_root *dirty;
273
274 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
275 if (!dirty)
276 return -ENOMEM;
Chris Mason5eda7b52007-06-22 14:16:25 -0400277 dirty->root = root;
Chris Mason5ce14bb2007-09-11 11:15:39 -0400278 dirty->latest_root = latest;
Chris Mason5eda7b52007-06-22 14:16:25 -0400279 list_add(&dirty->list, dead_list);
280 return 0;
281}
282
Chris Mason80b67942008-02-01 16:35:04 -0500283static noinline int add_dirty_roots(struct btrfs_trans_handle *trans,
284 struct radix_tree_root *radix,
285 struct list_head *list)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400286{
287 struct dirty_root *dirty;
288 struct btrfs_root *gang[8];
289 struct btrfs_root *root;
290 int i;
291 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400292 int err = 0;
Chris Mason5eda7b52007-06-22 14:16:25 -0400293 u32 refs;
Chris Mason54aa1f42007-06-22 14:16:25 -0400294
Chris Mason0f7d52f2007-04-09 10:42:37 -0400295 while(1) {
296 ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0,
297 ARRAY_SIZE(gang),
298 BTRFS_ROOT_TRANS_TAG);
299 if (ret == 0)
300 break;
301 for (i = 0; i < ret; i++) {
302 root = gang[i];
Chris Mason2619ba12007-04-10 16:58:11 -0400303 radix_tree_tag_clear(radix,
304 (unsigned long)root->root_key.objectid,
305 BTRFS_ROOT_TRANS_TAG);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400306 if (root->commit_root == root->node) {
Chris Masondb945352007-10-15 16:15:53 -0400307 WARN_ON(root->node->start !=
308 btrfs_root_bytenr(&root->root_item));
Chris Mason5f39d392007-10-15 16:14:19 -0400309 free_extent_buffer(root->commit_root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400310 root->commit_root = NULL;
Josef Bacik58176a92007-08-29 15:47:34 -0400311
312 /* make sure to update the root on disk
313 * so we get any updates to the block used
314 * counts
315 */
316 err = btrfs_update_root(trans,
317 root->fs_info->tree_root,
318 &root->root_key,
319 &root->root_item);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400320 continue;
321 }
322 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
323 BUG_ON(!dirty);
Chris Mason9f3a7422007-08-07 15:52:19 -0400324 dirty->root = kmalloc(sizeof(*dirty->root), GFP_NOFS);
325 BUG_ON(!dirty->root);
326
327 memset(&root->root_item.drop_progress, 0,
328 sizeof(struct btrfs_disk_key));
329 root->root_item.drop_level = 0;
330
331 memcpy(dirty->root, root, sizeof(*root));
332 dirty->root->node = root->commit_root;
Josef Bacik58176a92007-08-29 15:47:34 -0400333 dirty->latest_root = root;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400334 root->commit_root = NULL;
Chris Mason5eda7b52007-06-22 14:16:25 -0400335
Chris Mason0f7d52f2007-04-09 10:42:37 -0400336 root->root_key.offset = root->fs_info->generation;
Chris Masondb945352007-10-15 16:15:53 -0400337 btrfs_set_root_bytenr(&root->root_item,
338 root->node->start);
339 btrfs_set_root_level(&root->root_item,
340 btrfs_header_level(root->node));
Chris Mason0f7d52f2007-04-09 10:42:37 -0400341 err = btrfs_insert_root(trans, root->fs_info->tree_root,
342 &root->root_key,
343 &root->root_item);
Chris Mason54aa1f42007-06-22 14:16:25 -0400344 if (err)
345 break;
Chris Mason9f3a7422007-08-07 15:52:19 -0400346
347 refs = btrfs_root_refs(&dirty->root->root_item);
348 btrfs_set_root_refs(&dirty->root->root_item, refs - 1);
Chris Mason5eda7b52007-06-22 14:16:25 -0400349 err = btrfs_update_root(trans, root->fs_info->tree_root,
Chris Mason9f3a7422007-08-07 15:52:19 -0400350 &dirty->root->root_key,
351 &dirty->root->root_item);
Chris Mason5eda7b52007-06-22 14:16:25 -0400352
353 BUG_ON(err);
Chris Mason9f3a7422007-08-07 15:52:19 -0400354 if (refs == 1) {
Chris Mason5eda7b52007-06-22 14:16:25 -0400355 list_add(&dirty->list, list);
Chris Mason9f3a7422007-08-07 15:52:19 -0400356 } else {
357 WARN_ON(1);
358 kfree(dirty->root);
Chris Mason5eda7b52007-06-22 14:16:25 -0400359 kfree(dirty);
Chris Mason9f3a7422007-08-07 15:52:19 -0400360 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400361 }
362 }
Chris Mason54aa1f42007-06-22 14:16:25 -0400363 return err;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400364}
365
Chris Masone9d0b132007-08-10 14:06:19 -0400366int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
367{
368 struct btrfs_fs_info *info = root->fs_info;
369 int ret;
370 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400371 unsigned long nr;
Chris Masone9d0b132007-08-10 14:06:19 -0400372
Chris Masona2135012008-06-25 16:01:30 -0400373 smp_mb();
Chris Masone9d0b132007-08-10 14:06:19 -0400374 if (root->defrag_running)
375 return 0;
Chris Masone9d0b132007-08-10 14:06:19 -0400376 trans = btrfs_start_transaction(root, 1);
Chris Mason6b800532007-10-15 16:17:34 -0400377 while (1) {
Chris Masone9d0b132007-08-10 14:06:19 -0400378 root->defrag_running = 1;
379 ret = btrfs_defrag_leaves(trans, root, cacheonly);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400380 nr = trans->blocks_used;
Chris Masone9d0b132007-08-10 14:06:19 -0400381 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400382 btrfs_btree_balance_dirty(info->tree_root, nr);
Chris Masone9d0b132007-08-10 14:06:19 -0400383 cond_resched();
384
Chris Masone9d0b132007-08-10 14:06:19 -0400385 trans = btrfs_start_transaction(root, 1);
386 if (ret != -EAGAIN)
387 break;
388 }
389 root->defrag_running = 0;
Chris Masona2135012008-06-25 16:01:30 -0400390 smp_mb();
Chris Masone9d0b132007-08-10 14:06:19 -0400391 radix_tree_tag_clear(&info->fs_roots_radix,
392 (unsigned long)root->root_key.objectid,
393 BTRFS_ROOT_DEFRAG_TAG);
394 btrfs_end_transaction(trans, root);
395 return 0;
396}
397
Chris Mason6702ed42007-08-07 16:15:09 -0400398int btrfs_defrag_dirty_roots(struct btrfs_fs_info *info)
399{
400 struct btrfs_root *gang[1];
401 struct btrfs_root *root;
Chris Mason6702ed42007-08-07 16:15:09 -0400402 int i;
403 int ret;
404 int err = 0;
405 u64 last = 0;
406
Chris Mason6702ed42007-08-07 16:15:09 -0400407 while(1) {
408 ret = radix_tree_gang_lookup_tag(&info->fs_roots_radix,
409 (void **)gang, last,
410 ARRAY_SIZE(gang),
411 BTRFS_ROOT_DEFRAG_TAG);
412 if (ret == 0)
413 break;
414 for (i = 0; i < ret; i++) {
415 root = gang[i];
416 last = root->root_key.objectid + 1;
Chris Masonf510cfe2007-10-15 16:14:48 -0400417 btrfs_defrag_root(root, 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400418 }
419 }
Chris Mason6b800532007-10-15 16:17:34 -0400420 btrfs_defrag_root(info->extent_root, 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400421 return err;
422}
423
Chris Mason80b67942008-02-01 16:35:04 -0500424static noinline int drop_dirty_roots(struct btrfs_root *tree_root,
425 struct list_head *list)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400426{
427 struct dirty_root *dirty;
428 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400429 unsigned long nr;
Chris Masondb945352007-10-15 16:15:53 -0400430 u64 num_bytes;
431 u64 bytes_used;
Chris Mason54aa1f42007-06-22 14:16:25 -0400432 int ret = 0;
Chris Mason9f3a7422007-08-07 15:52:19 -0400433 int err;
434
Chris Mason0f7d52f2007-04-09 10:42:37 -0400435 while(!list_empty(list)) {
Josef Bacik58176a92007-08-29 15:47:34 -0400436 struct btrfs_root *root;
437
Chris Mason0f7d52f2007-04-09 10:42:37 -0400438 dirty = list_entry(list->next, struct dirty_root, list);
439 list_del_init(&dirty->list);
Chris Mason5eda7b52007-06-22 14:16:25 -0400440
Chris Masondb945352007-10-15 16:15:53 -0400441 num_bytes = btrfs_root_used(&dirty->root->root_item);
Josef Bacik58176a92007-08-29 15:47:34 -0400442 root = dirty->latest_root;
Chris Masona2135012008-06-25 16:01:30 -0400443 atomic_inc(&root->fs_info->throttles);
Josef Bacik58176a92007-08-29 15:47:34 -0400444
Chris Masona2135012008-06-25 16:01:30 -0400445 mutex_lock(&root->fs_info->drop_mutex);
Chris Mason9f3a7422007-08-07 15:52:19 -0400446 while(1) {
447 trans = btrfs_start_transaction(tree_root, 1);
448 ret = btrfs_drop_snapshot(trans, dirty->root);
449 if (ret != -EAGAIN) {
450 break;
451 }
Josef Bacik58176a92007-08-29 15:47:34 -0400452
Chris Mason9f3a7422007-08-07 15:52:19 -0400453 err = btrfs_update_root(trans,
454 tree_root,
455 &dirty->root->root_key,
456 &dirty->root->root_item);
457 if (err)
458 ret = err;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400459 nr = trans->blocks_used;
Chris Mason9f3a7422007-08-07 15:52:19 -0400460 ret = btrfs_end_transaction(trans, tree_root);
461 BUG_ON(ret);
Chris Masona2135012008-06-25 16:01:30 -0400462
463 mutex_unlock(&root->fs_info->drop_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400464 btrfs_btree_balance_dirty(tree_root, nr);
Chris Mason4dc119042007-10-15 16:18:14 -0400465 cond_resched();
Chris Masona2135012008-06-25 16:01:30 -0400466 mutex_lock(&root->fs_info->drop_mutex);
Chris Mason9f3a7422007-08-07 15:52:19 -0400467 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400468 BUG_ON(ret);
Chris Masona2135012008-06-25 16:01:30 -0400469 atomic_dec(&root->fs_info->throttles);
Josef Bacik58176a92007-08-29 15:47:34 -0400470
Chris Masona2135012008-06-25 16:01:30 -0400471 mutex_lock(&root->fs_info->alloc_mutex);
Chris Masondb945352007-10-15 16:15:53 -0400472 num_bytes -= btrfs_root_used(&dirty->root->root_item);
473 bytes_used = btrfs_root_used(&root->root_item);
474 if (num_bytes) {
Josef Bacik58176a92007-08-29 15:47:34 -0400475 record_root_in_trans(root);
Chris Mason5f39d392007-10-15 16:14:19 -0400476 btrfs_set_root_used(&root->root_item,
Chris Masondb945352007-10-15 16:15:53 -0400477 bytes_used - num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -0400478 }
Chris Masona2135012008-06-25 16:01:30 -0400479 mutex_unlock(&root->fs_info->alloc_mutex);
480
Chris Mason9f3a7422007-08-07 15:52:19 -0400481 ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key);
Josef Bacik58176a92007-08-29 15:47:34 -0400482 if (ret) {
483 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -0400484 break;
Josef Bacik58176a92007-08-29 15:47:34 -0400485 }
Chris Masona2135012008-06-25 16:01:30 -0400486 mutex_unlock(&root->fs_info->drop_mutex);
487
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400488 nr = trans->blocks_used;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400489 ret = btrfs_end_transaction(trans, tree_root);
490 BUG_ON(ret);
Chris Mason5eda7b52007-06-22 14:16:25 -0400491
Chris Masonf510cfe2007-10-15 16:14:48 -0400492 free_extent_buffer(dirty->root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -0400493 kfree(dirty->root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400494 kfree(dirty);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400495
496 btrfs_btree_balance_dirty(tree_root, nr);
Chris Mason4dc119042007-10-15 16:18:14 -0400497 cond_resched();
Chris Mason0f7d52f2007-04-09 10:42:37 -0400498 }
Chris Mason54aa1f42007-06-22 14:16:25 -0400499 return ret;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400500}
501
Chris Masondc17ff82008-01-08 15:46:30 -0500502int btrfs_write_ordered_inodes(struct btrfs_trans_handle *trans,
503 struct btrfs_root *root)
504{
505 struct btrfs_transaction *cur_trans = trans->transaction;
506 struct inode *inode;
507 u64 root_objectid = 0;
508 u64 objectid = 0;
Chris Masondc17ff82008-01-08 15:46:30 -0500509 int ret;
510
Chris Masona2135012008-06-25 16:01:30 -0400511 atomic_inc(&root->fs_info->throttles);
Chris Masondc17ff82008-01-08 15:46:30 -0500512 while(1) {
513 ret = btrfs_find_first_ordered_inode(
514 &cur_trans->ordered_inode_tree,
Chris Mason4d5e74b2008-01-16 16:09:22 -0500515 &root_objectid, &objectid, &inode);
Chris Masondc17ff82008-01-08 15:46:30 -0500516 if (!ret)
517 break;
518
519 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason4d5e74b2008-01-16 16:09:22 -0500520
Chris Mason81d7ed22008-04-25 08:51:48 -0400521 if (S_ISREG(inode->i_mode)) {
522 atomic_inc(&BTRFS_I(inode)->ordered_writeback);
Chris Mason4d5e74b2008-01-16 16:09:22 -0500523 filemap_fdatawrite(inode->i_mapping);
Chris Mason81d7ed22008-04-25 08:51:48 -0400524 atomic_dec(&BTRFS_I(inode)->ordered_writeback);
525 }
Chris Mason4d5e74b2008-01-16 16:09:22 -0500526 iput(inode);
527
Chris Masondc17ff82008-01-08 15:46:30 -0500528 mutex_lock(&root->fs_info->trans_mutex);
529 }
530 while(1) {
531 root_objectid = 0;
532 objectid = 0;
533 ret = btrfs_find_del_first_ordered_inode(
534 &cur_trans->ordered_inode_tree,
Chris Mason4d5e74b2008-01-16 16:09:22 -0500535 &root_objectid, &objectid, &inode);
Chris Masondc17ff82008-01-08 15:46:30 -0500536 if (!ret)
537 break;
538 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason4d5e74b2008-01-16 16:09:22 -0500539
Chris Mason81d7ed22008-04-25 08:51:48 -0400540 if (S_ISREG(inode->i_mode)) {
541 atomic_inc(&BTRFS_I(inode)->ordered_writeback);
Chris Mason4d5e74b2008-01-16 16:09:22 -0500542 filemap_write_and_wait(inode->i_mapping);
Chris Mason81d7ed22008-04-25 08:51:48 -0400543 atomic_dec(&BTRFS_I(inode)->ordered_writeback);
544 }
Chris Mason4d5e74b2008-01-16 16:09:22 -0500545 atomic_dec(&inode->i_count);
546 iput(inode);
547
Chris Masondc17ff82008-01-08 15:46:30 -0500548 mutex_lock(&root->fs_info->trans_mutex);
549 }
Chris Masona2135012008-06-25 16:01:30 -0400550 atomic_dec(&root->fs_info->throttles);
Chris Mason3063d292008-01-08 15:46:30 -0500551 return 0;
552}
553
Chris Mason80b67942008-02-01 16:35:04 -0500554static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
Chris Mason3063d292008-01-08 15:46:30 -0500555 struct btrfs_fs_info *fs_info,
556 struct btrfs_pending_snapshot *pending)
557{
558 struct btrfs_key key;
Chris Mason80b67942008-02-01 16:35:04 -0500559 struct btrfs_root_item *new_root_item;
Chris Mason3063d292008-01-08 15:46:30 -0500560 struct btrfs_root *tree_root = fs_info->tree_root;
561 struct btrfs_root *root = pending->root;
562 struct extent_buffer *tmp;
Chris Mason925baed2008-06-25 16:01:30 -0400563 struct extent_buffer *old;
Chris Mason3063d292008-01-08 15:46:30 -0500564 int ret;
Sven Wegener3b963622008-06-09 21:57:42 -0400565 int namelen;
Chris Mason3063d292008-01-08 15:46:30 -0500566 u64 objectid;
567
Chris Mason80b67942008-02-01 16:35:04 -0500568 new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
569 if (!new_root_item) {
570 ret = -ENOMEM;
571 goto fail;
572 }
Chris Mason3063d292008-01-08 15:46:30 -0500573 ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid);
574 if (ret)
575 goto fail;
576
Chris Mason80b67942008-02-01 16:35:04 -0500577 memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
Chris Mason3063d292008-01-08 15:46:30 -0500578
579 key.objectid = objectid;
580 key.offset = 1;
581 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
582
Chris Mason925baed2008-06-25 16:01:30 -0400583 old = btrfs_lock_root_node(root);
584 btrfs_cow_block(trans, root, old, NULL, 0, &old);
Chris Mason3063d292008-01-08 15:46:30 -0500585
Chris Mason925baed2008-06-25 16:01:30 -0400586 btrfs_copy_root(trans, root, old, &tmp, objectid);
587 btrfs_tree_unlock(old);
588 free_extent_buffer(old);
Chris Mason3063d292008-01-08 15:46:30 -0500589
Chris Mason80b67942008-02-01 16:35:04 -0500590 btrfs_set_root_bytenr(new_root_item, tmp->start);
591 btrfs_set_root_level(new_root_item, btrfs_header_level(tmp));
Chris Mason3063d292008-01-08 15:46:30 -0500592 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
Chris Mason80b67942008-02-01 16:35:04 -0500593 new_root_item);
Chris Mason925baed2008-06-25 16:01:30 -0400594 btrfs_tree_unlock(tmp);
Chris Mason3063d292008-01-08 15:46:30 -0500595 free_extent_buffer(tmp);
596 if (ret)
597 goto fail;
598
599 /*
600 * insert the directory item
601 */
602 key.offset = (u64)-1;
Sven Wegener3b963622008-06-09 21:57:42 -0400603 namelen = strlen(pending->name);
Chris Mason3063d292008-01-08 15:46:30 -0500604 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
Sven Wegener3b963622008-06-09 21:57:42 -0400605 pending->name, namelen,
Chris Mason3063d292008-01-08 15:46:30 -0500606 root->fs_info->sb->s_root->d_inode->i_ino,
607 &key, BTRFS_FT_DIR);
608
609 if (ret)
610 goto fail;
611
612 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
613 pending->name, strlen(pending->name), objectid,
614 root->fs_info->sb->s_root->d_inode->i_ino);
Sven Wegener3b963622008-06-09 21:57:42 -0400615
616 /* Invalidate existing dcache entry for new snapshot. */
617 btrfs_invalidate_dcache_root(root, pending->name, namelen);
618
Chris Mason3063d292008-01-08 15:46:30 -0500619fail:
Chris Mason80b67942008-02-01 16:35:04 -0500620 kfree(new_root_item);
Chris Mason3063d292008-01-08 15:46:30 -0500621 return ret;
622}
623
Chris Mason80b67942008-02-01 16:35:04 -0500624static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
625 struct btrfs_fs_info *fs_info)
Chris Mason3063d292008-01-08 15:46:30 -0500626{
627 struct btrfs_pending_snapshot *pending;
628 struct list_head *head = &trans->transaction->pending_snapshots;
629 int ret;
630
631 while(!list_empty(head)) {
632 pending = list_entry(head->next,
633 struct btrfs_pending_snapshot, list);
634 ret = create_pending_snapshot(trans, fs_info, pending);
635 BUG_ON(ret);
636 list_del(&pending->list);
637 kfree(pending->name);
638 kfree(pending);
639 }
Chris Masondc17ff82008-01-08 15:46:30 -0500640 return 0;
641}
642
Chris Mason79154b12007-03-22 15:59:16 -0400643int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
644 struct btrfs_root *root)
645{
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400646 unsigned long joined = 0;
647 unsigned long timeout = 1;
Chris Mason79154b12007-03-22 15:59:16 -0400648 struct btrfs_transaction *cur_trans;
Chris Mason8fd17792007-04-19 21:01:03 -0400649 struct btrfs_transaction *prev_trans = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -0400650 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400651 struct list_head dirty_fs_roots;
Chris Masond1310b22008-01-24 16:13:08 -0500652 struct extent_io_tree *pinned_copy;
Chris Mason79154b12007-03-22 15:59:16 -0400653 DEFINE_WAIT(wait);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400654 int ret;
Chris Mason79154b12007-03-22 15:59:16 -0400655
Chris Mason0f7d52f2007-04-09 10:42:37 -0400656 INIT_LIST_HEAD(&dirty_fs_roots);
Chris Masond6e4a422007-04-06 15:37:36 -0400657
Chris Mason79154b12007-03-22 15:59:16 -0400658 mutex_lock(&root->fs_info->trans_mutex);
659 if (trans->transaction->in_commit) {
660 cur_trans = trans->transaction;
661 trans->transaction->use_count++;
Chris Masonccd467d2007-06-28 15:57:36 -0400662 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400663 btrfs_end_transaction(trans, root);
Chris Masonccd467d2007-06-28 15:57:36 -0400664
Chris Mason79154b12007-03-22 15:59:16 -0400665 ret = wait_for_commit(root, cur_trans);
666 BUG_ON(ret);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400667
668 mutex_lock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400669 put_transaction(cur_trans);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400670 mutex_unlock(&root->fs_info->trans_mutex);
671
Chris Mason79154b12007-03-22 15:59:16 -0400672 return 0;
673 }
Chris Mason4313b392008-01-03 09:08:48 -0500674
675 pinned_copy = kmalloc(sizeof(*pinned_copy), GFP_NOFS);
676 if (!pinned_copy)
677 return -ENOMEM;
678
Chris Masond1310b22008-01-24 16:13:08 -0500679 extent_io_tree_init(pinned_copy,
Chris Mason4313b392008-01-03 09:08:48 -0500680 root->fs_info->btree_inode->i_mapping, GFP_NOFS);
681
Chris Mason2c90e5d2007-04-02 10:50:19 -0400682 trans->transaction->in_commit = 1;
Chris Masonccd467d2007-06-28 15:57:36 -0400683 cur_trans = trans->transaction;
684 if (cur_trans->list.prev != &root->fs_info->trans_list) {
685 prev_trans = list_entry(cur_trans->list.prev,
686 struct btrfs_transaction, list);
687 if (!prev_trans->commit_done) {
688 prev_trans->use_count++;
Chris Masonccd467d2007-06-28 15:57:36 -0400689 mutex_unlock(&root->fs_info->trans_mutex);
690
691 wait_for_commit(root, prev_trans);
Chris Masonccd467d2007-06-28 15:57:36 -0400692
Chris Masonccd467d2007-06-28 15:57:36 -0400693 mutex_lock(&root->fs_info->trans_mutex);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400694 put_transaction(prev_trans);
Chris Masonccd467d2007-06-28 15:57:36 -0400695 }
696 }
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400697
698 do {
699 joined = cur_trans->num_joined;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400700 WARN_ON(cur_trans != trans->transaction);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400701 prepare_to_wait(&cur_trans->writer_wait, &wait,
Chris Mason79154b12007-03-22 15:59:16 -0400702 TASK_UNINTERRUPTIBLE);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400703
704 if (cur_trans->num_writers > 1)
705 timeout = MAX_SCHEDULE_TIMEOUT;
706 else
707 timeout = 1;
708
Chris Mason79154b12007-03-22 15:59:16 -0400709 mutex_unlock(&root->fs_info->trans_mutex);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400710
711 schedule_timeout(timeout);
712
Chris Mason79154b12007-03-22 15:59:16 -0400713 mutex_lock(&root->fs_info->trans_mutex);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400714 finish_wait(&cur_trans->writer_wait, &wait);
Chris Masondc17ff82008-01-08 15:46:30 -0500715 ret = btrfs_write_ordered_inodes(trans, root);
716
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400717 } while (cur_trans->num_writers > 1 ||
718 (cur_trans->num_joined != joined));
719
Chris Mason3063d292008-01-08 15:46:30 -0500720 ret = create_pending_snapshots(trans, root->fs_info);
721 BUG_ON(ret);
722
Chris Mason2c90e5d2007-04-02 10:50:19 -0400723 WARN_ON(cur_trans != trans->transaction);
Chris Masondc17ff82008-01-08 15:46:30 -0500724
Chris Mason54aa1f42007-06-22 14:16:25 -0400725 ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix,
726 &dirty_fs_roots);
727 BUG_ON(ret);
728
Chris Mason79154b12007-03-22 15:59:16 -0400729 ret = btrfs_commit_tree_roots(trans, root);
730 BUG_ON(ret);
Chris Mason54aa1f42007-06-22 14:16:25 -0400731
Chris Mason78fae272007-03-25 11:35:08 -0400732 cur_trans = root->fs_info->running_transaction;
Chris Masoncee36a02008-01-15 08:40:48 -0500733 spin_lock(&root->fs_info->new_trans_lock);
Chris Mason78fae272007-03-25 11:35:08 -0400734 root->fs_info->running_transaction = NULL;
Chris Masoncee36a02008-01-15 08:40:48 -0500735 spin_unlock(&root->fs_info->new_trans_lock);
Chris Mason4b52dff2007-06-26 10:06:50 -0400736 btrfs_set_super_generation(&root->fs_info->super_copy,
737 cur_trans->transid);
738 btrfs_set_super_root(&root->fs_info->super_copy,
Chris Masondb945352007-10-15 16:15:53 -0400739 root->fs_info->tree_root->node->start);
740 btrfs_set_super_root_level(&root->fs_info->super_copy,
741 btrfs_header_level(root->fs_info->tree_root->node));
Chris Mason5f39d392007-10-15 16:14:19 -0400742
Chris Mason0b86a832008-03-24 15:01:56 -0400743 btrfs_set_super_chunk_root(&root->fs_info->super_copy,
744 chunk_root->node->start);
745 btrfs_set_super_chunk_root_level(&root->fs_info->super_copy,
746 btrfs_header_level(chunk_root->node));
Chris Masona061fc82008-05-07 11:43:44 -0400747 memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy,
748 sizeof(root->fs_info->super_copy));
Chris Masonccd467d2007-06-28 15:57:36 -0400749
Chris Mason4313b392008-01-03 09:08:48 -0500750 btrfs_copy_pinned(root, pinned_copy);
Chris Masonccd467d2007-06-28 15:57:36 -0400751
Chris Mason78fae272007-03-25 11:35:08 -0400752 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400753 ret = btrfs_write_and_wait_transaction(trans, root);
754 BUG_ON(ret);
Chris Mason79154b12007-03-22 15:59:16 -0400755 write_ctree_super(trans, root);
Chris Mason4313b392008-01-03 09:08:48 -0500756
Chris Mason4313b392008-01-03 09:08:48 -0500757 btrfs_finish_extent_commit(trans, root, pinned_copy);
Chris Mason78fae272007-03-25 11:35:08 -0400758 mutex_lock(&root->fs_info->trans_mutex);
Chris Mason4313b392008-01-03 09:08:48 -0500759
760 kfree(pinned_copy);
761
Chris Mason2c90e5d2007-04-02 10:50:19 -0400762 cur_trans->commit_done = 1;
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400763 root->fs_info->last_trans_committed = cur_trans->transid;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400764 wake_up(&cur_trans->commit_wait);
Chris Mason79154b12007-03-22 15:59:16 -0400765 put_transaction(cur_trans);
Chris Mason78fae272007-03-25 11:35:08 -0400766 put_transaction(cur_trans);
Josef Bacik58176a92007-08-29 15:47:34 -0400767
Chris Masonfacda1e2007-06-08 18:11:48 -0400768 if (root->fs_info->closing)
769 list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots);
770 else
771 list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots);
Josef Bacik58176a92007-08-29 15:47:34 -0400772
Chris Mason78fae272007-03-25 11:35:08 -0400773 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400774 kmem_cache_free(btrfs_trans_handle_cachep, trans);
Chris Mason79154b12007-03-22 15:59:16 -0400775
Chris Masonfacda1e2007-06-08 18:11:48 -0400776 if (root->fs_info->closing) {
Chris Masonfacda1e2007-06-08 18:11:48 -0400777 drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);
Chris Masonfacda1e2007-06-08 18:11:48 -0400778 }
Chris Mason79154b12007-03-22 15:59:16 -0400779 return ret;
780}
781
Chris Masone9d0b132007-08-10 14:06:19 -0400782int btrfs_clean_old_snapshots(struct btrfs_root *root)
783{
784 struct list_head dirty_roots;
785 INIT_LIST_HEAD(&dirty_roots);
786
787 mutex_lock(&root->fs_info->trans_mutex);
788 list_splice_init(&root->fs_info->dead_roots, &dirty_roots);
789 mutex_unlock(&root->fs_info->trans_mutex);
790
791 if (!list_empty(&dirty_roots)) {
792 drop_dirty_roots(root, &dirty_roots);
793 }
794 return 0;
795}
Chris Mason6da6aba2007-12-18 16:15:09 -0500796#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
797void btrfs_transaction_cleaner(void *p)
798#else
Chris Mason08607c12007-06-08 15:33:54 -0400799void btrfs_transaction_cleaner(struct work_struct *work)
Chris Mason6da6aba2007-12-18 16:15:09 -0500800#endif
Chris Mason08607c12007-06-08 15:33:54 -0400801{
Chris Mason6da6aba2007-12-18 16:15:09 -0500802#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
803 struct btrfs_fs_info *fs_info = p;
804#else
Chris Mason08607c12007-06-08 15:33:54 -0400805 struct btrfs_fs_info *fs_info = container_of(work,
806 struct btrfs_fs_info,
807 trans_work.work);
808
Chris Mason6da6aba2007-12-18 16:15:09 -0500809#endif
Chris Mason08607c12007-06-08 15:33:54 -0400810 struct btrfs_root *root = fs_info->tree_root;
811 struct btrfs_transaction *cur;
812 struct btrfs_trans_handle *trans;
813 unsigned long now;
814 unsigned long delay = HZ * 30;
815 int ret;
816
Chris Masona2135012008-06-25 16:01:30 -0400817 smp_mb();
Chris Masond6bfde82008-04-30 13:59:35 -0400818 if (root->fs_info->closing)
819 goto out;
820
Chris Mason08607c12007-06-08 15:33:54 -0400821 mutex_lock(&root->fs_info->trans_mutex);
822 cur = root->fs_info->running_transaction;
823 if (!cur) {
824 mutex_unlock(&root->fs_info->trans_mutex);
825 goto out;
826 }
827 now = get_seconds();
828 if (now < cur->start_time || now - cur->start_time < 30) {
829 mutex_unlock(&root->fs_info->trans_mutex);
830 delay = HZ * 5;
831 goto out;
832 }
833 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason6702ed42007-08-07 16:15:09 -0400834 btrfs_defrag_dirty_roots(root->fs_info);
Chris Mason08607c12007-06-08 15:33:54 -0400835 trans = btrfs_start_transaction(root, 1);
836 ret = btrfs_commit_transaction(trans, root);
837out:
Chris Masone9d0b132007-08-10 14:06:19 -0400838 btrfs_clean_old_snapshots(root);
Chris Mason08607c12007-06-08 15:33:54 -0400839 btrfs_transaction_queue_work(root, delay);
840}
841
842void btrfs_transaction_queue_work(struct btrfs_root *root, int delay)
843{
Chris Masond6bfde82008-04-30 13:59:35 -0400844 if (!root->fs_info->closing)
845 queue_delayed_work(trans_wq, &root->fs_info->trans_work, delay);
Chris Mason08607c12007-06-08 15:33:54 -0400846}
847
848void btrfs_transaction_flush_work(struct btrfs_root *root)
849{
Chris Masond6bfde82008-04-30 13:59:35 -0400850 cancel_delayed_work(&root->fs_info->trans_work);
Chris Mason08607c12007-06-08 15:33:54 -0400851 flush_workqueue(trans_wq);
852}
853
854void __init btrfs_init_transaction_sys(void)
855{
Chris Masonce9adaa2008-04-09 16:28:12 -0400856 trans_wq = create_workqueue("btrfs-transaction");
Chris Mason08607c12007-06-08 15:33:54 -0400857}
858
Christian Hesse17636e02007-12-11 09:25:06 -0500859void btrfs_exit_transaction_sys(void)
Chris Mason08607c12007-06-08 15:33:54 -0400860{
861 destroy_workqueue(trans_wq);
862}
863