blob: fcef3cae0c925cb31a34dfa10133e042f382f338 [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"
Yan Zheng31153d82008-07-28 15:32:19 -040027#include "ref-cache.h"
Chris Mason79154b12007-03-22 15:59:16 -040028
Chris Mason78fae272007-03-25 11:35:08 -040029static int total_trans = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -040030extern struct kmem_cache *btrfs_trans_handle_cachep;
31extern struct kmem_cache *btrfs_transaction_cachep;
32
Chris Mason0f7d52f2007-04-09 10:42:37 -040033#define BTRFS_ROOT_TRANS_TAG 0
34
Yan Zheng31153d82008-07-28 15:32:19 -040035struct dirty_root {
36 struct list_head list;
37 struct btrfs_root *root;
38 struct btrfs_root *latest_root;
Yan Zheng31153d82008-07-28 15:32:19 -040039};
40
Chris Mason80b67942008-02-01 16:35:04 -050041static noinline void put_transaction(struct btrfs_transaction *transaction)
Chris Mason79154b12007-03-22 15:59:16 -040042{
Chris Mason2c90e5d2007-04-02 10:50:19 -040043 WARN_ON(transaction->use_count == 0);
Chris Mason79154b12007-03-22 15:59:16 -040044 transaction->use_count--;
Chris Mason78fae272007-03-25 11:35:08 -040045 if (transaction->use_count == 0) {
46 WARN_ON(total_trans == 0);
47 total_trans--;
Chris Mason8fd17792007-04-19 21:01:03 -040048 list_del_init(&transaction->list);
Chris Mason2c90e5d2007-04-02 10:50:19 -040049 memset(transaction, 0, sizeof(*transaction));
50 kmem_cache_free(btrfs_transaction_cachep, transaction);
Chris Mason78fae272007-03-25 11:35:08 -040051 }
Chris Mason79154b12007-03-22 15:59:16 -040052}
53
Chris Mason80b67942008-02-01 16:35:04 -050054static noinline int join_transaction(struct btrfs_root *root)
Chris Mason79154b12007-03-22 15:59:16 -040055{
56 struct btrfs_transaction *cur_trans;
57 cur_trans = root->fs_info->running_transaction;
58 if (!cur_trans) {
Chris Mason2c90e5d2007-04-02 10:50:19 -040059 cur_trans = kmem_cache_alloc(btrfs_transaction_cachep,
60 GFP_NOFS);
Chris Mason78fae272007-03-25 11:35:08 -040061 total_trans++;
Chris Mason79154b12007-03-22 15:59:16 -040062 BUG_ON(!cur_trans);
Chris Mason0f7d52f2007-04-09 10:42:37 -040063 root->fs_info->generation++;
Chris Masone18e4802008-01-18 10:54:22 -050064 root->fs_info->last_alloc = 0;
Chris Mason4529ba42008-01-31 16:45:07 -050065 root->fs_info->last_data_alloc = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -040066 cur_trans->num_writers = 1;
67 cur_trans->num_joined = 0;
Chris Mason0f7d52f2007-04-09 10:42:37 -040068 cur_trans->transid = root->fs_info->generation;
Chris Mason79154b12007-03-22 15:59:16 -040069 init_waitqueue_head(&cur_trans->writer_wait);
70 init_waitqueue_head(&cur_trans->commit_wait);
71 cur_trans->in_commit = 0;
Chris Masonf9295742008-07-17 12:54:14 -040072 cur_trans->blocked = 0;
Chris Masond5719762007-03-23 10:01:08 -040073 cur_trans->use_count = 1;
Chris Mason79154b12007-03-22 15:59:16 -040074 cur_trans->commit_done = 0;
Chris Mason08607c12007-06-08 15:33:54 -040075 cur_trans->start_time = get_seconds();
Chris Mason3063d292008-01-08 15:46:30 -050076 INIT_LIST_HEAD(&cur_trans->pending_snapshots);
Chris Mason8fd17792007-04-19 21:01:03 -040077 list_add_tail(&cur_trans->list, &root->fs_info->trans_list);
Chris Masond1310b22008-01-24 16:13:08 -050078 extent_io_tree_init(&cur_trans->dirty_pages,
Chris Mason5f39d392007-10-15 16:14:19 -040079 root->fs_info->btree_inode->i_mapping,
80 GFP_NOFS);
Chris Mason48ec2cf2008-06-09 09:35:50 -040081 spin_lock(&root->fs_info->new_trans_lock);
82 root->fs_info->running_transaction = cur_trans;
83 spin_unlock(&root->fs_info->new_trans_lock);
Josef Bacik15ee9bc2007-08-10 16:22:09 -040084 } else {
85 cur_trans->num_writers++;
86 cur_trans->num_joined++;
Chris Mason79154b12007-03-22 15:59:16 -040087 }
Josef Bacik15ee9bc2007-08-10 16:22:09 -040088
Chris Mason79154b12007-03-22 15:59:16 -040089 return 0;
90}
91
Chris Mason80b67942008-02-01 16:35:04 -050092static noinline int record_root_in_trans(struct btrfs_root *root)
Chris Mason6702ed42007-08-07 16:15:09 -040093{
Yan Zheng31153d82008-07-28 15:32:19 -040094 struct dirty_root *dirty;
Chris Mason6702ed42007-08-07 16:15:09 -040095 u64 running_trans_id = root->fs_info->running_transaction->transid;
96 if (root->ref_cows && root->last_trans < running_trans_id) {
97 WARN_ON(root == root->fs_info->extent_root);
98 if (root->root_item.refs != 0) {
99 radix_tree_tag_set(&root->fs_info->fs_roots_radix,
100 (unsigned long)root->root_key.objectid,
101 BTRFS_ROOT_TRANS_TAG);
Yan Zheng31153d82008-07-28 15:32:19 -0400102
103 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
104 BUG_ON(!dirty);
105 dirty->root = kmalloc(sizeof(*dirty->root), GFP_NOFS);
106 BUG_ON(!dirty->root);
107
108 dirty->latest_root = root;
109 INIT_LIST_HEAD(&dirty->list);
Yan Zheng31153d82008-07-28 15:32:19 -0400110
Chris Mason925baed2008-06-25 16:01:30 -0400111 root->commit_root = btrfs_root_node(root);
Chris Mason017e5362008-07-28 15:32:51 -0400112 root->dirty_root = dirty;
Yan Zheng31153d82008-07-28 15:32:19 -0400113
114 memcpy(dirty->root, root, sizeof(*root));
Chris Mason017e5362008-07-28 15:32:51 -0400115 dirty->root->ref_tree = &root->ref_tree_struct;
116
Yan Zheng31153d82008-07-28 15:32:19 -0400117 spin_lock_init(&dirty->root->node_lock);
118 mutex_init(&dirty->root->objectid_mutex);
119 dirty->root->node = root->commit_root;
120 dirty->root->commit_root = NULL;
Chris Mason6702ed42007-08-07 16:15:09 -0400121 } else {
122 WARN_ON(1);
123 }
124 root->last_trans = running_trans_id;
125 }
126 return 0;
127}
128
Chris Masonf9295742008-07-17 12:54:14 -0400129struct btrfs_trans_handle *start_transaction(struct btrfs_root *root,
130 int num_blocks, int join)
Chris Mason79154b12007-03-22 15:59:16 -0400131{
Chris Mason2c90e5d2007-04-02 10:50:19 -0400132 struct btrfs_trans_handle *h =
133 kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
Chris Masonf9295742008-07-17 12:54:14 -0400134 struct btrfs_transaction *cur_trans;
Chris Mason79154b12007-03-22 15:59:16 -0400135 int ret;
136
137 mutex_lock(&root->fs_info->trans_mutex);
Chris Masonf9295742008-07-17 12:54:14 -0400138 cur_trans = root->fs_info->running_transaction;
139 if (cur_trans && cur_trans->blocked && !join) {
140 DEFINE_WAIT(wait);
141 cur_trans->use_count++;
142 while(1) {
143 prepare_to_wait(&root->fs_info->transaction_wait, &wait,
144 TASK_UNINTERRUPTIBLE);
145 if (cur_trans->blocked) {
146 mutex_unlock(&root->fs_info->trans_mutex);
147 schedule();
148 mutex_lock(&root->fs_info->trans_mutex);
149 finish_wait(&root->fs_info->transaction_wait,
150 &wait);
151 } else {
152 finish_wait(&root->fs_info->transaction_wait,
153 &wait);
154 break;
155 }
156 }
157 put_transaction(cur_trans);
158 }
Chris Mason79154b12007-03-22 15:59:16 -0400159 ret = join_transaction(root);
160 BUG_ON(ret);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400161
Chris Mason6702ed42007-08-07 16:15:09 -0400162 record_root_in_trans(root);
163 h->transid = root->fs_info->running_transaction->transid;
Chris Mason79154b12007-03-22 15:59:16 -0400164 h->transaction = root->fs_info->running_transaction;
165 h->blocks_reserved = num_blocks;
166 h->blocks_used = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400167 h->block_group = NULL;
Chris Mason26b80032007-08-08 20:17:12 -0400168 h->alloc_exclude_nr = 0;
169 h->alloc_exclude_start = 0;
Chris Mason79154b12007-03-22 15:59:16 -0400170 root->fs_info->running_transaction->use_count++;
171 mutex_unlock(&root->fs_info->trans_mutex);
172 return h;
173}
174
Chris Masonf9295742008-07-17 12:54:14 -0400175struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
176 int num_blocks)
177{
178 return start_transaction(root, num_blocks, 0);
179}
180struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root,
181 int num_blocks)
182{
183 return start_transaction(root, num_blocks, 1);
184}
185
Chris Mason89ce8a62008-06-25 16:01:31 -0400186static noinline int wait_for_commit(struct btrfs_root *root,
187 struct btrfs_transaction *commit)
188{
189 DEFINE_WAIT(wait);
190 mutex_lock(&root->fs_info->trans_mutex);
191 while(!commit->commit_done) {
192 prepare_to_wait(&commit->commit_wait, &wait,
193 TASK_UNINTERRUPTIBLE);
194 if (commit->commit_done)
195 break;
196 mutex_unlock(&root->fs_info->trans_mutex);
197 schedule();
198 mutex_lock(&root->fs_info->trans_mutex);
199 }
200 mutex_unlock(&root->fs_info->trans_mutex);
201 finish_wait(&commit->commit_wait, &wait);
202 return 0;
203}
204
205static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
206 struct btrfs_root *root, int throttle)
Chris Mason79154b12007-03-22 15:59:16 -0400207{
208 struct btrfs_transaction *cur_trans;
Chris Masond6e4a422007-04-06 15:37:36 -0400209
Chris Mason79154b12007-03-22 15:59:16 -0400210 mutex_lock(&root->fs_info->trans_mutex);
211 cur_trans = root->fs_info->running_transaction;
Chris Masonccd467d2007-06-28 15:57:36 -0400212 WARN_ON(cur_trans != trans->transaction);
Chris Masond5719762007-03-23 10:01:08 -0400213 WARN_ON(cur_trans->num_writers < 1);
Chris Masonccd467d2007-06-28 15:57:36 -0400214 cur_trans->num_writers--;
Chris Mason89ce8a62008-06-25 16:01:31 -0400215
Chris Mason79154b12007-03-22 15:59:16 -0400216 if (waitqueue_active(&cur_trans->writer_wait))
217 wake_up(&cur_trans->writer_wait);
Chris Mason89ce8a62008-06-25 16:01:31 -0400218
Chris Mason017e5362008-07-28 15:32:51 -0400219 if (throttle && atomic_read(&root->fs_info->throttles)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -0400220 DEFINE_WAIT(wait);
Chris Mason89ce8a62008-06-25 16:01:31 -0400221 mutex_unlock(&root->fs_info->trans_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400222 prepare_to_wait(&root->fs_info->transaction_throttle, &wait,
223 TASK_UNINTERRUPTIBLE);
Chris Mason017e5362008-07-28 15:32:51 -0400224 if (atomic_read(&root->fs_info->throttles))
225 schedule();
Chris Masone6dcd2d2008-07-17 12:53:50 -0400226 finish_wait(&root->fs_info->transaction_throttle, &wait);
Chris Mason89ce8a62008-06-25 16:01:31 -0400227 mutex_lock(&root->fs_info->trans_mutex);
228 }
229
Chris Mason79154b12007-03-22 15:59:16 -0400230 put_transaction(cur_trans);
231 mutex_unlock(&root->fs_info->trans_mutex);
Chris Masond6025572007-03-30 14:27:56 -0400232 memset(trans, 0, sizeof(*trans));
Chris Mason2c90e5d2007-04-02 10:50:19 -0400233 kmem_cache_free(btrfs_trans_handle_cachep, trans);
Chris Mason79154b12007-03-22 15:59:16 -0400234 return 0;
235}
236
Chris Mason89ce8a62008-06-25 16:01:31 -0400237int btrfs_end_transaction(struct btrfs_trans_handle *trans,
238 struct btrfs_root *root)
239{
240 return __btrfs_end_transaction(trans, root, 0);
241}
242
243int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
244 struct btrfs_root *root)
245{
246 return __btrfs_end_transaction(trans, root, 1);
247}
248
Chris Mason79154b12007-03-22 15:59:16 -0400249
250int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
251 struct btrfs_root *root)
252{
Chris Mason7c4452b2007-04-28 09:29:35 -0400253 int ret;
Chris Mason7c4452b2007-04-28 09:29:35 -0400254 int err;
255 int werr = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500256 struct extent_io_tree *dirty_pages;
Chris Mason7c4452b2007-04-28 09:29:35 -0400257 struct page *page;
Chris Mason7c4452b2007-04-28 09:29:35 -0400258 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Mason5f39d392007-10-15 16:14:19 -0400259 u64 start;
260 u64 end;
261 unsigned long index;
Chris Mason7c4452b2007-04-28 09:29:35 -0400262
263 if (!trans || !trans->transaction) {
264 return filemap_write_and_wait(btree_inode->i_mapping);
265 }
266 dirty_pages = &trans->transaction->dirty_pages;
267 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400268 ret = find_first_extent_bit(dirty_pages, 0, &start, &end,
269 EXTENT_DIRTY);
270 if (ret)
Chris Mason7c4452b2007-04-28 09:29:35 -0400271 break;
Chris Mason5f39d392007-10-15 16:14:19 -0400272 clear_extent_dirty(dirty_pages, start, end, GFP_NOFS);
273 while(start <= end) {
274 index = start >> PAGE_CACHE_SHIFT;
Chris Mason35ebb932007-10-30 16:56:53 -0400275 start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
Chris Mason5f39d392007-10-15 16:14:19 -0400276 page = find_lock_page(btree_inode->i_mapping, index);
Chris Mason7c4452b2007-04-28 09:29:35 -0400277 if (!page)
278 continue;
Chris Mason6702ed42007-08-07 16:15:09 -0400279 if (PageWriteback(page)) {
280 if (PageDirty(page))
281 wait_on_page_writeback(page);
282 else {
283 unlock_page(page);
284 page_cache_release(page);
285 continue;
286 }
287 }
Chris Mason7c4452b2007-04-28 09:29:35 -0400288 err = write_one_page(page, 0);
289 if (err)
290 werr = err;
291 page_cache_release(page);
292 }
293 }
294 err = filemap_fdatawait(btree_inode->i_mapping);
295 if (err)
296 werr = err;
297 return werr;
Chris Mason79154b12007-03-22 15:59:16 -0400298}
299
Chris Mason0b86a832008-03-24 15:01:56 -0400300static int update_cowonly_root(struct btrfs_trans_handle *trans,
301 struct btrfs_root *root)
302{
303 int ret;
304 u64 old_root_bytenr;
305 struct btrfs_root *tree_root = root->fs_info->tree_root;
306
307 btrfs_write_dirty_block_groups(trans, root);
308 while(1) {
309 old_root_bytenr = btrfs_root_bytenr(&root->root_item);
310 if (old_root_bytenr == root->node->start)
311 break;
312 btrfs_set_root_bytenr(&root->root_item,
313 root->node->start);
314 btrfs_set_root_level(&root->root_item,
315 btrfs_header_level(root->node));
316 ret = btrfs_update_root(trans, tree_root,
317 &root->root_key,
318 &root->root_item);
319 BUG_ON(ret);
320 btrfs_write_dirty_block_groups(trans, root);
321 }
322 return 0;
323}
324
Chris Mason79154b12007-03-22 15:59:16 -0400325int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans,
326 struct btrfs_root *root)
327{
Chris Mason79154b12007-03-22 15:59:16 -0400328 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason0b86a832008-03-24 15:01:56 -0400329 struct list_head *next;
Chris Mason79154b12007-03-22 15:59:16 -0400330
Chris Mason0b86a832008-03-24 15:01:56 -0400331 while(!list_empty(&fs_info->dirty_cowonly_roots)) {
332 next = fs_info->dirty_cowonly_roots.next;
333 list_del_init(next);
334 root = list_entry(next, struct btrfs_root, dirty_list);
335 update_cowonly_root(trans, root);
Chris Mason017e5362008-07-28 15:32:51 -0400336 if (root->fs_info->closing)
337 btrfs_remove_leaf_refs(root);
Chris Mason79154b12007-03-22 15:59:16 -0400338 }
339 return 0;
340}
341
Chris Mason5ce14bb2007-09-11 11:15:39 -0400342int btrfs_add_dead_root(struct btrfs_root *root,
343 struct btrfs_root *latest,
344 struct list_head *dead_list)
Chris Mason5eda7b52007-06-22 14:16:25 -0400345{
346 struct dirty_root *dirty;
347
348 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
349 if (!dirty)
350 return -ENOMEM;
Chris Mason5eda7b52007-06-22 14:16:25 -0400351 dirty->root = root;
Chris Mason5ce14bb2007-09-11 11:15:39 -0400352 dirty->latest_root = latest;
Chris Mason5eda7b52007-06-22 14:16:25 -0400353 list_add(&dirty->list, dead_list);
354 return 0;
355}
356
Chris Mason80b67942008-02-01 16:35:04 -0500357static noinline int add_dirty_roots(struct btrfs_trans_handle *trans,
358 struct radix_tree_root *radix,
359 struct list_head *list)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400360{
361 struct dirty_root *dirty;
362 struct btrfs_root *gang[8];
363 struct btrfs_root *root;
364 int i;
365 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400366 int err = 0;
Chris Mason5eda7b52007-06-22 14:16:25 -0400367 u32 refs;
Chris Mason54aa1f42007-06-22 14:16:25 -0400368
Chris Mason0f7d52f2007-04-09 10:42:37 -0400369 while(1) {
370 ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0,
371 ARRAY_SIZE(gang),
372 BTRFS_ROOT_TRANS_TAG);
373 if (ret == 0)
374 break;
375 for (i = 0; i < ret; i++) {
376 root = gang[i];
Chris Mason2619ba12007-04-10 16:58:11 -0400377 radix_tree_tag_clear(radix,
378 (unsigned long)root->root_key.objectid,
379 BTRFS_ROOT_TRANS_TAG);
Yan Zheng31153d82008-07-28 15:32:19 -0400380
381 BUG_ON(!root->ref_tree);
Chris Mason017e5362008-07-28 15:32:51 -0400382 dirty = root->dirty_root;
Yan Zheng31153d82008-07-28 15:32:19 -0400383
Chris Mason0f7d52f2007-04-09 10:42:37 -0400384 if (root->commit_root == root->node) {
Chris Masondb945352007-10-15 16:15:53 -0400385 WARN_ON(root->node->start !=
386 btrfs_root_bytenr(&root->root_item));
Yan Zheng31153d82008-07-28 15:32:19 -0400387
Chris Mason5f39d392007-10-15 16:14:19 -0400388 free_extent_buffer(root->commit_root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400389 root->commit_root = NULL;
Yan Zheng31153d82008-07-28 15:32:19 -0400390
391 kfree(dirty->root);
392 kfree(dirty);
Josef Bacik58176a92007-08-29 15:47:34 -0400393
394 /* make sure to update the root on disk
395 * so we get any updates to the block used
396 * counts
397 */
398 err = btrfs_update_root(trans,
399 root->fs_info->tree_root,
400 &root->root_key,
401 &root->root_item);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400402 continue;
403 }
Chris Mason9f3a7422007-08-07 15:52:19 -0400404
405 memset(&root->root_item.drop_progress, 0,
406 sizeof(struct btrfs_disk_key));
407 root->root_item.drop_level = 0;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400408 root->commit_root = NULL;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400409 root->root_key.offset = root->fs_info->generation;
Chris Masondb945352007-10-15 16:15:53 -0400410 btrfs_set_root_bytenr(&root->root_item,
411 root->node->start);
412 btrfs_set_root_level(&root->root_item,
413 btrfs_header_level(root->node));
Chris Mason0f7d52f2007-04-09 10:42:37 -0400414 err = btrfs_insert_root(trans, root->fs_info->tree_root,
415 &root->root_key,
416 &root->root_item);
Chris Mason54aa1f42007-06-22 14:16:25 -0400417 if (err)
418 break;
Chris Mason9f3a7422007-08-07 15:52:19 -0400419
420 refs = btrfs_root_refs(&dirty->root->root_item);
421 btrfs_set_root_refs(&dirty->root->root_item, refs - 1);
Chris Mason5eda7b52007-06-22 14:16:25 -0400422 err = btrfs_update_root(trans, root->fs_info->tree_root,
Chris Mason9f3a7422007-08-07 15:52:19 -0400423 &dirty->root->root_key,
424 &dirty->root->root_item);
Chris Mason5eda7b52007-06-22 14:16:25 -0400425
426 BUG_ON(err);
Chris Mason9f3a7422007-08-07 15:52:19 -0400427 if (refs == 1) {
Chris Mason5eda7b52007-06-22 14:16:25 -0400428 list_add(&dirty->list, list);
Chris Mason9f3a7422007-08-07 15:52:19 -0400429 } else {
430 WARN_ON(1);
Yan Zheng31153d82008-07-28 15:32:19 -0400431 free_extent_buffer(dirty->root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -0400432 kfree(dirty->root);
Chris Mason5eda7b52007-06-22 14:16:25 -0400433 kfree(dirty);
Chris Mason9f3a7422007-08-07 15:52:19 -0400434 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400435 }
436 }
Chris Mason54aa1f42007-06-22 14:16:25 -0400437 return err;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400438}
439
Chris Masone9d0b132007-08-10 14:06:19 -0400440int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
441{
442 struct btrfs_fs_info *info = root->fs_info;
443 int ret;
444 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400445 unsigned long nr;
Chris Masone9d0b132007-08-10 14:06:19 -0400446
Chris Masona2135012008-06-25 16:01:30 -0400447 smp_mb();
Chris Masone9d0b132007-08-10 14:06:19 -0400448 if (root->defrag_running)
449 return 0;
Chris Masone9d0b132007-08-10 14:06:19 -0400450 trans = btrfs_start_transaction(root, 1);
Chris Mason6b800532007-10-15 16:17:34 -0400451 while (1) {
Chris Masone9d0b132007-08-10 14:06:19 -0400452 root->defrag_running = 1;
453 ret = btrfs_defrag_leaves(trans, root, cacheonly);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400454 nr = trans->blocks_used;
Chris Masone9d0b132007-08-10 14:06:19 -0400455 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400456 btrfs_btree_balance_dirty(info->tree_root, nr);
Chris Masone9d0b132007-08-10 14:06:19 -0400457 cond_resched();
458
Chris Masone9d0b132007-08-10 14:06:19 -0400459 trans = btrfs_start_transaction(root, 1);
Chris Mason3f157a22008-06-25 16:01:31 -0400460 if (root->fs_info->closing || ret != -EAGAIN)
Chris Masone9d0b132007-08-10 14:06:19 -0400461 break;
462 }
463 root->defrag_running = 0;
Chris Masona2135012008-06-25 16:01:30 -0400464 smp_mb();
Chris Masone9d0b132007-08-10 14:06:19 -0400465 btrfs_end_transaction(trans, root);
466 return 0;
467}
468
Chris Mason80b67942008-02-01 16:35:04 -0500469static noinline int drop_dirty_roots(struct btrfs_root *tree_root,
470 struct list_head *list)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400471{
472 struct dirty_root *dirty;
473 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400474 unsigned long nr;
Chris Masondb945352007-10-15 16:15:53 -0400475 u64 num_bytes;
476 u64 bytes_used;
Chris Mason54aa1f42007-06-22 14:16:25 -0400477 int ret = 0;
Chris Mason9f3a7422007-08-07 15:52:19 -0400478 int err;
479
Chris Mason0f7d52f2007-04-09 10:42:37 -0400480 while(!list_empty(list)) {
Josef Bacik58176a92007-08-29 15:47:34 -0400481 struct btrfs_root *root;
482
Chris Mason017e5362008-07-28 15:32:51 -0400483 dirty = list_entry(list->prev, struct dirty_root, list);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400484 list_del_init(&dirty->list);
Chris Mason5eda7b52007-06-22 14:16:25 -0400485
Chris Masondb945352007-10-15 16:15:53 -0400486 num_bytes = btrfs_root_used(&dirty->root->root_item);
Josef Bacik58176a92007-08-29 15:47:34 -0400487 root = dirty->latest_root;
Chris Masona2135012008-06-25 16:01:30 -0400488 atomic_inc(&root->fs_info->throttles);
Josef Bacik58176a92007-08-29 15:47:34 -0400489
Chris Masona2135012008-06-25 16:01:30 -0400490 mutex_lock(&root->fs_info->drop_mutex);
Chris Mason9f3a7422007-08-07 15:52:19 -0400491 while(1) {
492 trans = btrfs_start_transaction(tree_root, 1);
493 ret = btrfs_drop_snapshot(trans, dirty->root);
494 if (ret != -EAGAIN) {
495 break;
496 }
Josef Bacik58176a92007-08-29 15:47:34 -0400497
Chris Mason9f3a7422007-08-07 15:52:19 -0400498 err = btrfs_update_root(trans,
499 tree_root,
500 &dirty->root->root_key,
501 &dirty->root->root_item);
502 if (err)
503 ret = err;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400504 nr = trans->blocks_used;
Chris Mason017e5362008-07-28 15:32:51 -0400505 ret = btrfs_end_transaction(trans, tree_root);
Chris Mason9f3a7422007-08-07 15:52:19 -0400506 BUG_ON(ret);
Chris Masona2135012008-06-25 16:01:30 -0400507
508 mutex_unlock(&root->fs_info->drop_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400509 btrfs_btree_balance_dirty(tree_root, nr);
Chris Mason4dc119042007-10-15 16:18:14 -0400510 cond_resched();
Chris Masona2135012008-06-25 16:01:30 -0400511 mutex_lock(&root->fs_info->drop_mutex);
Chris Mason9f3a7422007-08-07 15:52:19 -0400512 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400513 BUG_ON(ret);
Chris Masona2135012008-06-25 16:01:30 -0400514 atomic_dec(&root->fs_info->throttles);
Chris Mason017e5362008-07-28 15:32:51 -0400515 wake_up(&root->fs_info->transaction_throttle);
Josef Bacik58176a92007-08-29 15:47:34 -0400516
Chris Masona2135012008-06-25 16:01:30 -0400517 mutex_lock(&root->fs_info->alloc_mutex);
Chris Masondb945352007-10-15 16:15:53 -0400518 num_bytes -= btrfs_root_used(&dirty->root->root_item);
519 bytes_used = btrfs_root_used(&root->root_item);
520 if (num_bytes) {
Josef Bacik58176a92007-08-29 15:47:34 -0400521 record_root_in_trans(root);
Chris Mason5f39d392007-10-15 16:14:19 -0400522 btrfs_set_root_used(&root->root_item,
Chris Masondb945352007-10-15 16:15:53 -0400523 bytes_used - num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -0400524 }
Chris Masona2135012008-06-25 16:01:30 -0400525 mutex_unlock(&root->fs_info->alloc_mutex);
526
Chris Mason9f3a7422007-08-07 15:52:19 -0400527 ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key);
Josef Bacik58176a92007-08-29 15:47:34 -0400528 if (ret) {
529 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -0400530 break;
Josef Bacik58176a92007-08-29 15:47:34 -0400531 }
Chris Masona2135012008-06-25 16:01:30 -0400532 mutex_unlock(&root->fs_info->drop_mutex);
533
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400534 nr = trans->blocks_used;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400535 ret = btrfs_end_transaction(trans, tree_root);
536 BUG_ON(ret);
Chris Mason5eda7b52007-06-22 14:16:25 -0400537
Chris Masonf510cfe2007-10-15 16:14:48 -0400538 free_extent_buffer(dirty->root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -0400539 kfree(dirty->root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400540 kfree(dirty);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400541
542 btrfs_btree_balance_dirty(tree_root, nr);
Chris Mason4dc119042007-10-15 16:18:14 -0400543 cond_resched();
Chris Mason0f7d52f2007-04-09 10:42:37 -0400544 }
Chris Mason54aa1f42007-06-22 14:16:25 -0400545 return ret;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400546}
547
Chris Mason80b67942008-02-01 16:35:04 -0500548static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
Chris Mason3063d292008-01-08 15:46:30 -0500549 struct btrfs_fs_info *fs_info,
550 struct btrfs_pending_snapshot *pending)
551{
552 struct btrfs_key key;
Chris Mason80b67942008-02-01 16:35:04 -0500553 struct btrfs_root_item *new_root_item;
Chris Mason3063d292008-01-08 15:46:30 -0500554 struct btrfs_root *tree_root = fs_info->tree_root;
555 struct btrfs_root *root = pending->root;
556 struct extent_buffer *tmp;
Chris Mason925baed2008-06-25 16:01:30 -0400557 struct extent_buffer *old;
Chris Mason3063d292008-01-08 15:46:30 -0500558 int ret;
Sven Wegener3b963622008-06-09 21:57:42 -0400559 int namelen;
Chris Mason3063d292008-01-08 15:46:30 -0500560 u64 objectid;
561
Chris Mason80b67942008-02-01 16:35:04 -0500562 new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
563 if (!new_root_item) {
564 ret = -ENOMEM;
565 goto fail;
566 }
Chris Mason3063d292008-01-08 15:46:30 -0500567 ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid);
568 if (ret)
569 goto fail;
570
Chris Mason80b67942008-02-01 16:35:04 -0500571 memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
Chris Mason3063d292008-01-08 15:46:30 -0500572
573 key.objectid = objectid;
574 key.offset = 1;
575 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
576
Chris Mason925baed2008-06-25 16:01:30 -0400577 old = btrfs_lock_root_node(root);
578 btrfs_cow_block(trans, root, old, NULL, 0, &old);
Chris Mason3063d292008-01-08 15:46:30 -0500579
Chris Mason925baed2008-06-25 16:01:30 -0400580 btrfs_copy_root(trans, root, old, &tmp, objectid);
581 btrfs_tree_unlock(old);
582 free_extent_buffer(old);
Chris Mason3063d292008-01-08 15:46:30 -0500583
Chris Mason80b67942008-02-01 16:35:04 -0500584 btrfs_set_root_bytenr(new_root_item, tmp->start);
585 btrfs_set_root_level(new_root_item, btrfs_header_level(tmp));
Chris Mason3063d292008-01-08 15:46:30 -0500586 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
Chris Mason80b67942008-02-01 16:35:04 -0500587 new_root_item);
Chris Mason925baed2008-06-25 16:01:30 -0400588 btrfs_tree_unlock(tmp);
Chris Mason3063d292008-01-08 15:46:30 -0500589 free_extent_buffer(tmp);
590 if (ret)
591 goto fail;
592
593 /*
594 * insert the directory item
595 */
596 key.offset = (u64)-1;
Sven Wegener3b963622008-06-09 21:57:42 -0400597 namelen = strlen(pending->name);
Chris Mason3063d292008-01-08 15:46:30 -0500598 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
Sven Wegener3b963622008-06-09 21:57:42 -0400599 pending->name, namelen,
Chris Mason3063d292008-01-08 15:46:30 -0500600 root->fs_info->sb->s_root->d_inode->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -0400601 &key, BTRFS_FT_DIR, 0);
Chris Mason3063d292008-01-08 15:46:30 -0500602
603 if (ret)
604 goto fail;
605
606 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
607 pending->name, strlen(pending->name), objectid,
Josef Bacikaec74772008-07-24 12:12:38 -0400608 root->fs_info->sb->s_root->d_inode->i_ino, 0);
Sven Wegener3b963622008-06-09 21:57:42 -0400609
610 /* Invalidate existing dcache entry for new snapshot. */
611 btrfs_invalidate_dcache_root(root, pending->name, namelen);
612
Chris Mason3063d292008-01-08 15:46:30 -0500613fail:
Chris Mason80b67942008-02-01 16:35:04 -0500614 kfree(new_root_item);
Chris Mason3063d292008-01-08 15:46:30 -0500615 return ret;
616}
617
Chris Mason80b67942008-02-01 16:35:04 -0500618static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
619 struct btrfs_fs_info *fs_info)
Chris Mason3063d292008-01-08 15:46:30 -0500620{
621 struct btrfs_pending_snapshot *pending;
622 struct list_head *head = &trans->transaction->pending_snapshots;
623 int ret;
624
625 while(!list_empty(head)) {
626 pending = list_entry(head->next,
627 struct btrfs_pending_snapshot, list);
628 ret = create_pending_snapshot(trans, fs_info, pending);
629 BUG_ON(ret);
630 list_del(&pending->list);
631 kfree(pending->name);
632 kfree(pending);
633 }
Chris Masondc17ff82008-01-08 15:46:30 -0500634 return 0;
635}
636
Chris Mason79154b12007-03-22 15:59:16 -0400637int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
638 struct btrfs_root *root)
639{
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400640 unsigned long joined = 0;
641 unsigned long timeout = 1;
Chris Mason79154b12007-03-22 15:59:16 -0400642 struct btrfs_transaction *cur_trans;
Chris Mason8fd17792007-04-19 21:01:03 -0400643 struct btrfs_transaction *prev_trans = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -0400644 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400645 struct list_head dirty_fs_roots;
Chris Masond1310b22008-01-24 16:13:08 -0500646 struct extent_io_tree *pinned_copy;
Chris Mason79154b12007-03-22 15:59:16 -0400647 DEFINE_WAIT(wait);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400648 int ret;
Chris Mason79154b12007-03-22 15:59:16 -0400649
Chris Mason0f7d52f2007-04-09 10:42:37 -0400650 INIT_LIST_HEAD(&dirty_fs_roots);
Chris Masond6e4a422007-04-06 15:37:36 -0400651
Chris Mason79154b12007-03-22 15:59:16 -0400652 mutex_lock(&root->fs_info->trans_mutex);
653 if (trans->transaction->in_commit) {
654 cur_trans = trans->transaction;
655 trans->transaction->use_count++;
Chris Masonccd467d2007-06-28 15:57:36 -0400656 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400657 btrfs_end_transaction(trans, root);
Chris Masonccd467d2007-06-28 15:57:36 -0400658
Chris Mason79154b12007-03-22 15:59:16 -0400659 ret = wait_for_commit(root, cur_trans);
660 BUG_ON(ret);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400661
662 mutex_lock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400663 put_transaction(cur_trans);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400664 mutex_unlock(&root->fs_info->trans_mutex);
665
Chris Mason79154b12007-03-22 15:59:16 -0400666 return 0;
667 }
Chris Mason4313b392008-01-03 09:08:48 -0500668
669 pinned_copy = kmalloc(sizeof(*pinned_copy), GFP_NOFS);
670 if (!pinned_copy)
671 return -ENOMEM;
672
Chris Masond1310b22008-01-24 16:13:08 -0500673 extent_io_tree_init(pinned_copy,
Chris Mason4313b392008-01-03 09:08:48 -0500674 root->fs_info->btree_inode->i_mapping, GFP_NOFS);
675
Chris Mason2c90e5d2007-04-02 10:50:19 -0400676 trans->transaction->in_commit = 1;
Chris Masonf9295742008-07-17 12:54:14 -0400677 trans->transaction->blocked = 1;
Chris Masonccd467d2007-06-28 15:57:36 -0400678 cur_trans = trans->transaction;
679 if (cur_trans->list.prev != &root->fs_info->trans_list) {
680 prev_trans = list_entry(cur_trans->list.prev,
681 struct btrfs_transaction, list);
682 if (!prev_trans->commit_done) {
683 prev_trans->use_count++;
Chris Masonccd467d2007-06-28 15:57:36 -0400684 mutex_unlock(&root->fs_info->trans_mutex);
685
686 wait_for_commit(root, prev_trans);
Chris Masonccd467d2007-06-28 15:57:36 -0400687
Chris Masonccd467d2007-06-28 15:57:36 -0400688 mutex_lock(&root->fs_info->trans_mutex);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400689 put_transaction(prev_trans);
Chris Masonccd467d2007-06-28 15:57:36 -0400690 }
691 }
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400692
693 do {
694 joined = cur_trans->num_joined;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400695 WARN_ON(cur_trans != trans->transaction);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400696 prepare_to_wait(&cur_trans->writer_wait, &wait,
Chris Mason79154b12007-03-22 15:59:16 -0400697 TASK_UNINTERRUPTIBLE);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400698
699 if (cur_trans->num_writers > 1)
700 timeout = MAX_SCHEDULE_TIMEOUT;
701 else
702 timeout = 1;
703
Chris Mason79154b12007-03-22 15:59:16 -0400704 mutex_unlock(&root->fs_info->trans_mutex);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400705
706 schedule_timeout(timeout);
707
Chris Mason79154b12007-03-22 15:59:16 -0400708 mutex_lock(&root->fs_info->trans_mutex);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400709 finish_wait(&cur_trans->writer_wait, &wait);
710 } while (cur_trans->num_writers > 1 ||
711 (cur_trans->num_joined != joined));
712
Chris Mason3063d292008-01-08 15:46:30 -0500713 ret = create_pending_snapshots(trans, root->fs_info);
714 BUG_ON(ret);
715
Chris Mason2c90e5d2007-04-02 10:50:19 -0400716 WARN_ON(cur_trans != trans->transaction);
Chris Masondc17ff82008-01-08 15:46:30 -0500717
Chris Mason54aa1f42007-06-22 14:16:25 -0400718 ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix,
719 &dirty_fs_roots);
720 BUG_ON(ret);
721
Chris Mason79154b12007-03-22 15:59:16 -0400722 ret = btrfs_commit_tree_roots(trans, root);
723 BUG_ON(ret);
Chris Mason54aa1f42007-06-22 14:16:25 -0400724
Chris Mason78fae272007-03-25 11:35:08 -0400725 cur_trans = root->fs_info->running_transaction;
Chris Masoncee36a02008-01-15 08:40:48 -0500726 spin_lock(&root->fs_info->new_trans_lock);
Chris Mason78fae272007-03-25 11:35:08 -0400727 root->fs_info->running_transaction = NULL;
Chris Masoncee36a02008-01-15 08:40:48 -0500728 spin_unlock(&root->fs_info->new_trans_lock);
Chris Mason4b52dff2007-06-26 10:06:50 -0400729 btrfs_set_super_generation(&root->fs_info->super_copy,
730 cur_trans->transid);
731 btrfs_set_super_root(&root->fs_info->super_copy,
Chris Masondb945352007-10-15 16:15:53 -0400732 root->fs_info->tree_root->node->start);
733 btrfs_set_super_root_level(&root->fs_info->super_copy,
734 btrfs_header_level(root->fs_info->tree_root->node));
Chris Mason5f39d392007-10-15 16:14:19 -0400735
Chris Mason0b86a832008-03-24 15:01:56 -0400736 btrfs_set_super_chunk_root(&root->fs_info->super_copy,
737 chunk_root->node->start);
738 btrfs_set_super_chunk_root_level(&root->fs_info->super_copy,
739 btrfs_header_level(chunk_root->node));
Chris Masona061fc82008-05-07 11:43:44 -0400740 memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy,
741 sizeof(root->fs_info->super_copy));
Chris Masonccd467d2007-06-28 15:57:36 -0400742
Chris Mason4313b392008-01-03 09:08:48 -0500743 btrfs_copy_pinned(root, pinned_copy);
Chris Masonccd467d2007-06-28 15:57:36 -0400744
Chris Masonf9295742008-07-17 12:54:14 -0400745 trans->transaction->blocked = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400746 wake_up(&root->fs_info->transaction_throttle);
Chris Masonf9295742008-07-17 12:54:14 -0400747 wake_up(&root->fs_info->transaction_wait);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400748
Chris Mason78fae272007-03-25 11:35:08 -0400749 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400750 ret = btrfs_write_and_wait_transaction(trans, root);
751 BUG_ON(ret);
Chris Mason79154b12007-03-22 15:59:16 -0400752 write_ctree_super(trans, root);
Chris Mason4313b392008-01-03 09:08:48 -0500753
Chris Mason4313b392008-01-03 09:08:48 -0500754 btrfs_finish_extent_commit(trans, root, pinned_copy);
Chris Mason78fae272007-03-25 11:35:08 -0400755 mutex_lock(&root->fs_info->trans_mutex);
Chris Mason4313b392008-01-03 09:08:48 -0500756
757 kfree(pinned_copy);
758
Chris Mason2c90e5d2007-04-02 10:50:19 -0400759 cur_trans->commit_done = 1;
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400760 root->fs_info->last_trans_committed = cur_trans->transid;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400761 wake_up(&cur_trans->commit_wait);
Chris Mason79154b12007-03-22 15:59:16 -0400762 put_transaction(cur_trans);
Chris Mason78fae272007-03-25 11:35:08 -0400763 put_transaction(cur_trans);
Josef Bacik58176a92007-08-29 15:47:34 -0400764
Chris Masonfacda1e2007-06-08 18:11:48 -0400765 if (root->fs_info->closing)
766 list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots);
767 else
768 list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots);
Josef Bacik58176a92007-08-29 15:47:34 -0400769
Chris Mason78fae272007-03-25 11:35:08 -0400770 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400771 kmem_cache_free(btrfs_trans_handle_cachep, trans);
Chris Mason79154b12007-03-22 15:59:16 -0400772
Chris Masonfacda1e2007-06-08 18:11:48 -0400773 if (root->fs_info->closing) {
Chris Masonfacda1e2007-06-08 18:11:48 -0400774 drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);
Chris Masonfacda1e2007-06-08 18:11:48 -0400775 }
Chris Mason79154b12007-03-22 15:59:16 -0400776 return ret;
777}
778
Chris Masone9d0b132007-08-10 14:06:19 -0400779int btrfs_clean_old_snapshots(struct btrfs_root *root)
780{
781 struct list_head dirty_roots;
782 INIT_LIST_HEAD(&dirty_roots);
Chris Masona74a4b92008-06-25 16:01:31 -0400783again:
Chris Masone9d0b132007-08-10 14:06:19 -0400784 mutex_lock(&root->fs_info->trans_mutex);
785 list_splice_init(&root->fs_info->dead_roots, &dirty_roots);
786 mutex_unlock(&root->fs_info->trans_mutex);
787
788 if (!list_empty(&dirty_roots)) {
789 drop_dirty_roots(root, &dirty_roots);
Chris Masona74a4b92008-06-25 16:01:31 -0400790 goto again;
Chris Masone9d0b132007-08-10 14:06:19 -0400791 }
792 return 0;
793}
Chris Mason08607c12007-06-08 15:33:54 -0400794