blob: 216f31571620928506b37ddb5d6780e08f24cc06 [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
Chris Mason80b67942008-02-01 16:35:04 -050035static noinline void put_transaction(struct btrfs_transaction *transaction)
Chris Mason79154b12007-03-22 15:59:16 -040036{
Chris Mason2c90e5d2007-04-02 10:50:19 -040037 WARN_ON(transaction->use_count == 0);
Chris Mason79154b12007-03-22 15:59:16 -040038 transaction->use_count--;
Chris Mason78fae272007-03-25 11:35:08 -040039 if (transaction->use_count == 0) {
40 WARN_ON(total_trans == 0);
41 total_trans--;
Chris Mason8fd17792007-04-19 21:01:03 -040042 list_del_init(&transaction->list);
Chris Mason2c90e5d2007-04-02 10:50:19 -040043 memset(transaction, 0, sizeof(*transaction));
44 kmem_cache_free(btrfs_transaction_cachep, transaction);
Chris Mason78fae272007-03-25 11:35:08 -040045 }
Chris Mason79154b12007-03-22 15:59:16 -040046}
47
Chris Mason80b67942008-02-01 16:35:04 -050048static noinline int join_transaction(struct btrfs_root *root)
Chris Mason79154b12007-03-22 15:59:16 -040049{
50 struct btrfs_transaction *cur_trans;
51 cur_trans = root->fs_info->running_transaction;
52 if (!cur_trans) {
Chris Mason2c90e5d2007-04-02 10:50:19 -040053 cur_trans = kmem_cache_alloc(btrfs_transaction_cachep,
54 GFP_NOFS);
Chris Mason78fae272007-03-25 11:35:08 -040055 total_trans++;
Chris Mason79154b12007-03-22 15:59:16 -040056 BUG_ON(!cur_trans);
Chris Mason0f7d52f2007-04-09 10:42:37 -040057 root->fs_info->generation++;
Chris Masone18e4802008-01-18 10:54:22 -050058 root->fs_info->last_alloc = 0;
Chris Mason4529ba42008-01-31 16:45:07 -050059 root->fs_info->last_data_alloc = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -040060 cur_trans->num_writers = 1;
61 cur_trans->num_joined = 0;
Chris Mason0f7d52f2007-04-09 10:42:37 -040062 cur_trans->transid = root->fs_info->generation;
Chris Mason79154b12007-03-22 15:59:16 -040063 init_waitqueue_head(&cur_trans->writer_wait);
64 init_waitqueue_head(&cur_trans->commit_wait);
65 cur_trans->in_commit = 0;
Chris Masonf9295742008-07-17 12:54:14 -040066 cur_trans->blocked = 0;
Chris Masond5719762007-03-23 10:01:08 -040067 cur_trans->use_count = 1;
Chris Mason79154b12007-03-22 15:59:16 -040068 cur_trans->commit_done = 0;
Chris Mason08607c12007-06-08 15:33:54 -040069 cur_trans->start_time = get_seconds();
Chris Mason3063d292008-01-08 15:46:30 -050070 INIT_LIST_HEAD(&cur_trans->pending_snapshots);
Chris Mason8fd17792007-04-19 21:01:03 -040071 list_add_tail(&cur_trans->list, &root->fs_info->trans_list);
Chris Masond1310b22008-01-24 16:13:08 -050072 extent_io_tree_init(&cur_trans->dirty_pages,
Chris Mason5f39d392007-10-15 16:14:19 -040073 root->fs_info->btree_inode->i_mapping,
74 GFP_NOFS);
Chris Mason48ec2cf2008-06-09 09:35:50 -040075 spin_lock(&root->fs_info->new_trans_lock);
76 root->fs_info->running_transaction = cur_trans;
77 spin_unlock(&root->fs_info->new_trans_lock);
Josef Bacik15ee9bc2007-08-10 16:22:09 -040078 } else {
79 cur_trans->num_writers++;
80 cur_trans->num_joined++;
Chris Mason79154b12007-03-22 15:59:16 -040081 }
Josef Bacik15ee9bc2007-08-10 16:22:09 -040082
Chris Mason79154b12007-03-22 15:59:16 -040083 return 0;
84}
85
Chris Mason80b67942008-02-01 16:35:04 -050086static noinline int record_root_in_trans(struct btrfs_root *root)
Chris Mason6702ed42007-08-07 16:15:09 -040087{
Yan Zhengf321e492008-07-30 09:26:11 -040088 struct btrfs_dirty_root *dirty;
Chris Mason6702ed42007-08-07 16:15:09 -040089 u64 running_trans_id = root->fs_info->running_transaction->transid;
90 if (root->ref_cows && root->last_trans < running_trans_id) {
91 WARN_ON(root == root->fs_info->extent_root);
92 if (root->root_item.refs != 0) {
93 radix_tree_tag_set(&root->fs_info->fs_roots_radix,
94 (unsigned long)root->root_key.objectid,
95 BTRFS_ROOT_TRANS_TAG);
Yan Zheng31153d82008-07-28 15:32:19 -040096
97 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
98 BUG_ON(!dirty);
99 dirty->root = kmalloc(sizeof(*dirty->root), GFP_NOFS);
100 BUG_ON(!dirty->root);
101
102 dirty->latest_root = root;
103 INIT_LIST_HEAD(&dirty->list);
Yan Zheng31153d82008-07-28 15:32:19 -0400104
Chris Mason925baed2008-06-25 16:01:30 -0400105 root->commit_root = btrfs_root_node(root);
Chris Mason017e5362008-07-28 15:32:51 -0400106 root->dirty_root = dirty;
Yan Zheng31153d82008-07-28 15:32:19 -0400107
108 memcpy(dirty->root, root, sizeof(*root));
Chris Mason017e5362008-07-28 15:32:51 -0400109 dirty->root->ref_tree = &root->ref_tree_struct;
110
Yan Zheng31153d82008-07-28 15:32:19 -0400111 spin_lock_init(&dirty->root->node_lock);
112 mutex_init(&dirty->root->objectid_mutex);
113 dirty->root->node = root->commit_root;
114 dirty->root->commit_root = NULL;
Chris Mason6702ed42007-08-07 16:15:09 -0400115 } else {
116 WARN_ON(1);
117 }
118 root->last_trans = running_trans_id;
119 }
120 return 0;
121}
122
Chris Masonf9295742008-07-17 12:54:14 -0400123struct btrfs_trans_handle *start_transaction(struct btrfs_root *root,
124 int num_blocks, int join)
Chris Mason79154b12007-03-22 15:59:16 -0400125{
Chris Mason2c90e5d2007-04-02 10:50:19 -0400126 struct btrfs_trans_handle *h =
127 kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
Chris Masonf9295742008-07-17 12:54:14 -0400128 struct btrfs_transaction *cur_trans;
Chris Mason79154b12007-03-22 15:59:16 -0400129 int ret;
130
131 mutex_lock(&root->fs_info->trans_mutex);
Chris Masonf9295742008-07-17 12:54:14 -0400132 cur_trans = root->fs_info->running_transaction;
133 if (cur_trans && cur_trans->blocked && !join) {
134 DEFINE_WAIT(wait);
135 cur_trans->use_count++;
136 while(1) {
137 prepare_to_wait(&root->fs_info->transaction_wait, &wait,
138 TASK_UNINTERRUPTIBLE);
139 if (cur_trans->blocked) {
140 mutex_unlock(&root->fs_info->trans_mutex);
141 schedule();
142 mutex_lock(&root->fs_info->trans_mutex);
143 finish_wait(&root->fs_info->transaction_wait,
144 &wait);
145 } else {
146 finish_wait(&root->fs_info->transaction_wait,
147 &wait);
148 break;
149 }
150 }
151 put_transaction(cur_trans);
152 }
Chris Mason79154b12007-03-22 15:59:16 -0400153 ret = join_transaction(root);
154 BUG_ON(ret);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400155
Chris Mason6702ed42007-08-07 16:15:09 -0400156 record_root_in_trans(root);
157 h->transid = root->fs_info->running_transaction->transid;
Chris Mason79154b12007-03-22 15:59:16 -0400158 h->transaction = root->fs_info->running_transaction;
159 h->blocks_reserved = num_blocks;
160 h->blocks_used = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400161 h->block_group = NULL;
Chris Mason26b80032007-08-08 20:17:12 -0400162 h->alloc_exclude_nr = 0;
163 h->alloc_exclude_start = 0;
Chris Mason79154b12007-03-22 15:59:16 -0400164 root->fs_info->running_transaction->use_count++;
165 mutex_unlock(&root->fs_info->trans_mutex);
166 return h;
167}
168
Chris Masonf9295742008-07-17 12:54:14 -0400169struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
170 int num_blocks)
171{
172 return start_transaction(root, num_blocks, 0);
173}
174struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root,
175 int num_blocks)
176{
177 return start_transaction(root, num_blocks, 1);
178}
179
Chris Mason89ce8a62008-06-25 16:01:31 -0400180static noinline int wait_for_commit(struct btrfs_root *root,
181 struct btrfs_transaction *commit)
182{
183 DEFINE_WAIT(wait);
184 mutex_lock(&root->fs_info->trans_mutex);
185 while(!commit->commit_done) {
186 prepare_to_wait(&commit->commit_wait, &wait,
187 TASK_UNINTERRUPTIBLE);
188 if (commit->commit_done)
189 break;
190 mutex_unlock(&root->fs_info->trans_mutex);
191 schedule();
192 mutex_lock(&root->fs_info->trans_mutex);
193 }
194 mutex_unlock(&root->fs_info->trans_mutex);
195 finish_wait(&commit->commit_wait, &wait);
196 return 0;
197}
198
Chris Masonab78c842008-07-29 16:15:18 -0400199void btrfs_throttle(struct btrfs_root *root)
200{
201 struct btrfs_fs_info *info = root->fs_info;
202
203harder:
204 if (atomic_read(&info->throttles)) {
205 DEFINE_WAIT(wait);
206 int thr;
207 int harder_count = 0;
208 thr = atomic_read(&info->throttle_gen);
209
210 do {
211 prepare_to_wait(&info->transaction_throttle,
212 &wait, TASK_UNINTERRUPTIBLE);
213 if (!atomic_read(&info->throttles)) {
214 finish_wait(&info->transaction_throttle, &wait);
215 break;
216 }
217 schedule();
218 finish_wait(&info->transaction_throttle, &wait);
219 } while (thr == atomic_read(&info->throttle_gen));
220
221 if (harder_count < 5 &&
222 info->total_ref_cache_size > 5 * 1024 * 1024) {
223 harder_count++;
224 goto harder;
225 }
226
227 if (harder_count < 10 &&
228 info->total_ref_cache_size > 10 * 1024 * 1024) {
229 harder_count++;
230 goto harder;
231 }
232 }
233}
234
Chris Mason89ce8a62008-06-25 16:01:31 -0400235static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
236 struct btrfs_root *root, int throttle)
Chris Mason79154b12007-03-22 15:59:16 -0400237{
238 struct btrfs_transaction *cur_trans;
Chris Masonab78c842008-07-29 16:15:18 -0400239 struct btrfs_fs_info *info = root->fs_info;
Chris Masond6e4a422007-04-06 15:37:36 -0400240
Chris Masonab78c842008-07-29 16:15:18 -0400241 mutex_lock(&info->trans_mutex);
242 cur_trans = info->running_transaction;
Chris Masonccd467d2007-06-28 15:57:36 -0400243 WARN_ON(cur_trans != trans->transaction);
Chris Masond5719762007-03-23 10:01:08 -0400244 WARN_ON(cur_trans->num_writers < 1);
Chris Masonccd467d2007-06-28 15:57:36 -0400245 cur_trans->num_writers--;
Chris Mason89ce8a62008-06-25 16:01:31 -0400246
Chris Mason79154b12007-03-22 15:59:16 -0400247 if (waitqueue_active(&cur_trans->writer_wait))
248 wake_up(&cur_trans->writer_wait);
Chris Mason79154b12007-03-22 15:59:16 -0400249 put_transaction(cur_trans);
Chris Masonab78c842008-07-29 16:15:18 -0400250 mutex_unlock(&info->trans_mutex);
Chris Masond6025572007-03-30 14:27:56 -0400251 memset(trans, 0, sizeof(*trans));
Chris Mason2c90e5d2007-04-02 10:50:19 -0400252 kmem_cache_free(btrfs_trans_handle_cachep, trans);
Chris Masonab78c842008-07-29 16:15:18 -0400253
254 if (throttle)
255 btrfs_throttle(root);
256
Chris Mason79154b12007-03-22 15:59:16 -0400257 return 0;
258}
259
Chris Mason89ce8a62008-06-25 16:01:31 -0400260int btrfs_end_transaction(struct btrfs_trans_handle *trans,
261 struct btrfs_root *root)
262{
263 return __btrfs_end_transaction(trans, root, 0);
264}
265
266int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
267 struct btrfs_root *root)
268{
269 return __btrfs_end_transaction(trans, root, 1);
270}
271
Chris Mason79154b12007-03-22 15:59:16 -0400272
273int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
274 struct btrfs_root *root)
275{
Chris Mason7c4452b2007-04-28 09:29:35 -0400276 int ret;
Chris Mason7c4452b2007-04-28 09:29:35 -0400277 int err;
278 int werr = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500279 struct extent_io_tree *dirty_pages;
Chris Mason7c4452b2007-04-28 09:29:35 -0400280 struct page *page;
Chris Mason7c4452b2007-04-28 09:29:35 -0400281 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Mason5f39d392007-10-15 16:14:19 -0400282 u64 start;
283 u64 end;
284 unsigned long index;
Chris Mason7c4452b2007-04-28 09:29:35 -0400285
286 if (!trans || !trans->transaction) {
287 return filemap_write_and_wait(btree_inode->i_mapping);
288 }
289 dirty_pages = &trans->transaction->dirty_pages;
290 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400291 ret = find_first_extent_bit(dirty_pages, 0, &start, &end,
292 EXTENT_DIRTY);
293 if (ret)
Chris Mason7c4452b2007-04-28 09:29:35 -0400294 break;
Chris Mason5f39d392007-10-15 16:14:19 -0400295 clear_extent_dirty(dirty_pages, start, end, GFP_NOFS);
296 while(start <= end) {
297 index = start >> PAGE_CACHE_SHIFT;
Chris Mason35ebb932007-10-30 16:56:53 -0400298 start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
Chris Mason5f39d392007-10-15 16:14:19 -0400299 page = find_lock_page(btree_inode->i_mapping, index);
Chris Mason7c4452b2007-04-28 09:29:35 -0400300 if (!page)
301 continue;
Chris Mason6702ed42007-08-07 16:15:09 -0400302 if (PageWriteback(page)) {
303 if (PageDirty(page))
304 wait_on_page_writeback(page);
305 else {
306 unlock_page(page);
307 page_cache_release(page);
308 continue;
309 }
310 }
Chris Mason7c4452b2007-04-28 09:29:35 -0400311 err = write_one_page(page, 0);
312 if (err)
313 werr = err;
314 page_cache_release(page);
315 }
316 }
317 err = filemap_fdatawait(btree_inode->i_mapping);
318 if (err)
319 werr = err;
320 return werr;
Chris Mason79154b12007-03-22 15:59:16 -0400321}
322
Chris Mason0b86a832008-03-24 15:01:56 -0400323static int update_cowonly_root(struct btrfs_trans_handle *trans,
324 struct btrfs_root *root)
325{
326 int ret;
327 u64 old_root_bytenr;
328 struct btrfs_root *tree_root = root->fs_info->tree_root;
329
330 btrfs_write_dirty_block_groups(trans, root);
331 while(1) {
332 old_root_bytenr = btrfs_root_bytenr(&root->root_item);
333 if (old_root_bytenr == root->node->start)
334 break;
335 btrfs_set_root_bytenr(&root->root_item,
336 root->node->start);
337 btrfs_set_root_level(&root->root_item,
338 btrfs_header_level(root->node));
339 ret = btrfs_update_root(trans, tree_root,
340 &root->root_key,
341 &root->root_item);
342 BUG_ON(ret);
343 btrfs_write_dirty_block_groups(trans, root);
344 }
345 return 0;
346}
347
Chris Mason79154b12007-03-22 15:59:16 -0400348int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans,
349 struct btrfs_root *root)
350{
Chris Mason79154b12007-03-22 15:59:16 -0400351 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason0b86a832008-03-24 15:01:56 -0400352 struct list_head *next;
Chris Mason79154b12007-03-22 15:59:16 -0400353
Chris Mason0b86a832008-03-24 15:01:56 -0400354 while(!list_empty(&fs_info->dirty_cowonly_roots)) {
355 next = fs_info->dirty_cowonly_roots.next;
356 list_del_init(next);
357 root = list_entry(next, struct btrfs_root, dirty_list);
358 update_cowonly_root(trans, root);
Chris Mason017e5362008-07-28 15:32:51 -0400359 if (root->fs_info->closing)
360 btrfs_remove_leaf_refs(root);
Chris Mason79154b12007-03-22 15:59:16 -0400361 }
362 return 0;
363}
364
Chris Mason5ce14bb2007-09-11 11:15:39 -0400365int btrfs_add_dead_root(struct btrfs_root *root,
366 struct btrfs_root *latest,
367 struct list_head *dead_list)
Chris Mason5eda7b52007-06-22 14:16:25 -0400368{
Yan Zhengf321e492008-07-30 09:26:11 -0400369 struct btrfs_dirty_root *dirty;
Chris Mason5eda7b52007-06-22 14:16:25 -0400370
371 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
372 if (!dirty)
373 return -ENOMEM;
Chris Mason5eda7b52007-06-22 14:16:25 -0400374 dirty->root = root;
Chris Mason5ce14bb2007-09-11 11:15:39 -0400375 dirty->latest_root = latest;
Chris Mason5eda7b52007-06-22 14:16:25 -0400376 list_add(&dirty->list, dead_list);
377 return 0;
378}
379
Chris Mason80b67942008-02-01 16:35:04 -0500380static noinline int add_dirty_roots(struct btrfs_trans_handle *trans,
381 struct radix_tree_root *radix,
382 struct list_head *list)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400383{
Yan Zhengf321e492008-07-30 09:26:11 -0400384 struct btrfs_dirty_root *dirty;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400385 struct btrfs_root *gang[8];
386 struct btrfs_root *root;
387 int i;
388 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400389 int err = 0;
Chris Mason5eda7b52007-06-22 14:16:25 -0400390 u32 refs;
Chris Mason54aa1f42007-06-22 14:16:25 -0400391
Chris Mason0f7d52f2007-04-09 10:42:37 -0400392 while(1) {
393 ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0,
394 ARRAY_SIZE(gang),
395 BTRFS_ROOT_TRANS_TAG);
396 if (ret == 0)
397 break;
398 for (i = 0; i < ret; i++) {
399 root = gang[i];
Chris Mason2619ba12007-04-10 16:58:11 -0400400 radix_tree_tag_clear(radix,
401 (unsigned long)root->root_key.objectid,
402 BTRFS_ROOT_TRANS_TAG);
Yan Zheng31153d82008-07-28 15:32:19 -0400403
404 BUG_ON(!root->ref_tree);
Chris Mason017e5362008-07-28 15:32:51 -0400405 dirty = root->dirty_root;
Yan Zheng31153d82008-07-28 15:32:19 -0400406
Chris Mason0f7d52f2007-04-09 10:42:37 -0400407 if (root->commit_root == root->node) {
Chris Masondb945352007-10-15 16:15:53 -0400408 WARN_ON(root->node->start !=
409 btrfs_root_bytenr(&root->root_item));
Yan Zheng31153d82008-07-28 15:32:19 -0400410
Chris Mason5f39d392007-10-15 16:14:19 -0400411 free_extent_buffer(root->commit_root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400412 root->commit_root = NULL;
Yan Zheng31153d82008-07-28 15:32:19 -0400413
414 kfree(dirty->root);
415 kfree(dirty);
Josef Bacik58176a92007-08-29 15:47:34 -0400416
417 /* make sure to update the root on disk
418 * so we get any updates to the block used
419 * counts
420 */
421 err = btrfs_update_root(trans,
422 root->fs_info->tree_root,
423 &root->root_key,
424 &root->root_item);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400425 continue;
426 }
Chris Mason9f3a7422007-08-07 15:52:19 -0400427
428 memset(&root->root_item.drop_progress, 0,
429 sizeof(struct btrfs_disk_key));
430 root->root_item.drop_level = 0;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400431 root->commit_root = NULL;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400432 root->root_key.offset = root->fs_info->generation;
Chris Masondb945352007-10-15 16:15:53 -0400433 btrfs_set_root_bytenr(&root->root_item,
434 root->node->start);
435 btrfs_set_root_level(&root->root_item,
436 btrfs_header_level(root->node));
Chris Mason0f7d52f2007-04-09 10:42:37 -0400437 err = btrfs_insert_root(trans, root->fs_info->tree_root,
438 &root->root_key,
439 &root->root_item);
Chris Mason54aa1f42007-06-22 14:16:25 -0400440 if (err)
441 break;
Chris Mason9f3a7422007-08-07 15:52:19 -0400442
443 refs = btrfs_root_refs(&dirty->root->root_item);
444 btrfs_set_root_refs(&dirty->root->root_item, refs - 1);
Chris Mason5eda7b52007-06-22 14:16:25 -0400445 err = btrfs_update_root(trans, root->fs_info->tree_root,
Chris Mason9f3a7422007-08-07 15:52:19 -0400446 &dirty->root->root_key,
447 &dirty->root->root_item);
Chris Mason5eda7b52007-06-22 14:16:25 -0400448
449 BUG_ON(err);
Chris Mason9f3a7422007-08-07 15:52:19 -0400450 if (refs == 1) {
Chris Mason5eda7b52007-06-22 14:16:25 -0400451 list_add(&dirty->list, list);
Chris Mason9f3a7422007-08-07 15:52:19 -0400452 } else {
453 WARN_ON(1);
Yan Zheng31153d82008-07-28 15:32:19 -0400454 free_extent_buffer(dirty->root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -0400455 kfree(dirty->root);
Chris Mason5eda7b52007-06-22 14:16:25 -0400456 kfree(dirty);
Chris Mason9f3a7422007-08-07 15:52:19 -0400457 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400458 }
459 }
Chris Mason54aa1f42007-06-22 14:16:25 -0400460 return err;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400461}
462
Chris Masone9d0b132007-08-10 14:06:19 -0400463int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
464{
465 struct btrfs_fs_info *info = root->fs_info;
466 int ret;
467 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400468 unsigned long nr;
Chris Masone9d0b132007-08-10 14:06:19 -0400469
Chris Masona2135012008-06-25 16:01:30 -0400470 smp_mb();
Chris Masone9d0b132007-08-10 14:06:19 -0400471 if (root->defrag_running)
472 return 0;
Chris Masone9d0b132007-08-10 14:06:19 -0400473 trans = btrfs_start_transaction(root, 1);
Chris Mason6b800532007-10-15 16:17:34 -0400474 while (1) {
Chris Masone9d0b132007-08-10 14:06:19 -0400475 root->defrag_running = 1;
476 ret = btrfs_defrag_leaves(trans, root, cacheonly);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400477 nr = trans->blocks_used;
Chris Masone9d0b132007-08-10 14:06:19 -0400478 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400479 btrfs_btree_balance_dirty(info->tree_root, nr);
Chris Masone9d0b132007-08-10 14:06:19 -0400480 cond_resched();
481
Chris Masone9d0b132007-08-10 14:06:19 -0400482 trans = btrfs_start_transaction(root, 1);
Chris Mason3f157a22008-06-25 16:01:31 -0400483 if (root->fs_info->closing || ret != -EAGAIN)
Chris Masone9d0b132007-08-10 14:06:19 -0400484 break;
485 }
486 root->defrag_running = 0;
Chris Masona2135012008-06-25 16:01:30 -0400487 smp_mb();
Chris Masone9d0b132007-08-10 14:06:19 -0400488 btrfs_end_transaction(trans, root);
489 return 0;
490}
491
Chris Mason80b67942008-02-01 16:35:04 -0500492static noinline int drop_dirty_roots(struct btrfs_root *tree_root,
493 struct list_head *list)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400494{
Yan Zhengf321e492008-07-30 09:26:11 -0400495 struct btrfs_dirty_root *dirty;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400496 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400497 unsigned long nr;
Chris Masondb945352007-10-15 16:15:53 -0400498 u64 num_bytes;
499 u64 bytes_used;
Chris Mason54aa1f42007-06-22 14:16:25 -0400500 int ret = 0;
Chris Mason9f3a7422007-08-07 15:52:19 -0400501 int err;
502
Chris Mason0f7d52f2007-04-09 10:42:37 -0400503 while(!list_empty(list)) {
Josef Bacik58176a92007-08-29 15:47:34 -0400504 struct btrfs_root *root;
505
Yan Zhengf321e492008-07-30 09:26:11 -0400506 dirty = list_entry(list->prev, struct btrfs_dirty_root, list);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400507 list_del_init(&dirty->list);
Chris Mason5eda7b52007-06-22 14:16:25 -0400508
Chris Masondb945352007-10-15 16:15:53 -0400509 num_bytes = btrfs_root_used(&dirty->root->root_item);
Josef Bacik58176a92007-08-29 15:47:34 -0400510 root = dirty->latest_root;
Chris Masona2135012008-06-25 16:01:30 -0400511 atomic_inc(&root->fs_info->throttles);
Josef Bacik58176a92007-08-29 15:47:34 -0400512
Chris Masona2135012008-06-25 16:01:30 -0400513 mutex_lock(&root->fs_info->drop_mutex);
Chris Mason9f3a7422007-08-07 15:52:19 -0400514 while(1) {
515 trans = btrfs_start_transaction(tree_root, 1);
516 ret = btrfs_drop_snapshot(trans, dirty->root);
517 if (ret != -EAGAIN) {
518 break;
519 }
Josef Bacik58176a92007-08-29 15:47:34 -0400520
Chris Mason9f3a7422007-08-07 15:52:19 -0400521 err = btrfs_update_root(trans,
522 tree_root,
523 &dirty->root->root_key,
524 &dirty->root->root_item);
525 if (err)
526 ret = err;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400527 nr = trans->blocks_used;
Chris Mason017e5362008-07-28 15:32:51 -0400528 ret = btrfs_end_transaction(trans, tree_root);
Chris Mason9f3a7422007-08-07 15:52:19 -0400529 BUG_ON(ret);
Chris Masona2135012008-06-25 16:01:30 -0400530
531 mutex_unlock(&root->fs_info->drop_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400532 btrfs_btree_balance_dirty(tree_root, nr);
Chris Mason4dc119042007-10-15 16:18:14 -0400533 cond_resched();
Chris Masona2135012008-06-25 16:01:30 -0400534 mutex_lock(&root->fs_info->drop_mutex);
Chris Mason9f3a7422007-08-07 15:52:19 -0400535 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400536 BUG_ON(ret);
Chris Masona2135012008-06-25 16:01:30 -0400537 atomic_dec(&root->fs_info->throttles);
Chris Mason017e5362008-07-28 15:32:51 -0400538 wake_up(&root->fs_info->transaction_throttle);
Josef Bacik58176a92007-08-29 15:47:34 -0400539
Chris Masona2135012008-06-25 16:01:30 -0400540 mutex_lock(&root->fs_info->alloc_mutex);
Chris Masondb945352007-10-15 16:15:53 -0400541 num_bytes -= btrfs_root_used(&dirty->root->root_item);
542 bytes_used = btrfs_root_used(&root->root_item);
543 if (num_bytes) {
Josef Bacik58176a92007-08-29 15:47:34 -0400544 record_root_in_trans(root);
Chris Mason5f39d392007-10-15 16:14:19 -0400545 btrfs_set_root_used(&root->root_item,
Chris Masondb945352007-10-15 16:15:53 -0400546 bytes_used - num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -0400547 }
Chris Masona2135012008-06-25 16:01:30 -0400548 mutex_unlock(&root->fs_info->alloc_mutex);
549
Chris Mason9f3a7422007-08-07 15:52:19 -0400550 ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key);
Josef Bacik58176a92007-08-29 15:47:34 -0400551 if (ret) {
552 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -0400553 break;
Josef Bacik58176a92007-08-29 15:47:34 -0400554 }
Chris Masona2135012008-06-25 16:01:30 -0400555 mutex_unlock(&root->fs_info->drop_mutex);
556
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400557 nr = trans->blocks_used;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400558 ret = btrfs_end_transaction(trans, tree_root);
559 BUG_ON(ret);
Chris Mason5eda7b52007-06-22 14:16:25 -0400560
Chris Masonf510cfe2007-10-15 16:14:48 -0400561 free_extent_buffer(dirty->root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -0400562 kfree(dirty->root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400563 kfree(dirty);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400564
565 btrfs_btree_balance_dirty(tree_root, nr);
Chris Mason4dc119042007-10-15 16:18:14 -0400566 cond_resched();
Chris Mason0f7d52f2007-04-09 10:42:37 -0400567 }
Chris Mason54aa1f42007-06-22 14:16:25 -0400568 return ret;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400569}
570
Chris Mason80b67942008-02-01 16:35:04 -0500571static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
Chris Mason3063d292008-01-08 15:46:30 -0500572 struct btrfs_fs_info *fs_info,
573 struct btrfs_pending_snapshot *pending)
574{
575 struct btrfs_key key;
Chris Mason80b67942008-02-01 16:35:04 -0500576 struct btrfs_root_item *new_root_item;
Chris Mason3063d292008-01-08 15:46:30 -0500577 struct btrfs_root *tree_root = fs_info->tree_root;
578 struct btrfs_root *root = pending->root;
579 struct extent_buffer *tmp;
Chris Mason925baed2008-06-25 16:01:30 -0400580 struct extent_buffer *old;
Chris Mason3063d292008-01-08 15:46:30 -0500581 int ret;
Sven Wegener3b963622008-06-09 21:57:42 -0400582 int namelen;
Chris Mason3063d292008-01-08 15:46:30 -0500583 u64 objectid;
584
Chris Mason80b67942008-02-01 16:35:04 -0500585 new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
586 if (!new_root_item) {
587 ret = -ENOMEM;
588 goto fail;
589 }
Chris Mason3063d292008-01-08 15:46:30 -0500590 ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid);
591 if (ret)
592 goto fail;
593
Chris Mason80b67942008-02-01 16:35:04 -0500594 memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
Chris Mason3063d292008-01-08 15:46:30 -0500595
596 key.objectid = objectid;
597 key.offset = 1;
598 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
599
Chris Mason925baed2008-06-25 16:01:30 -0400600 old = btrfs_lock_root_node(root);
601 btrfs_cow_block(trans, root, old, NULL, 0, &old);
Chris Mason3063d292008-01-08 15:46:30 -0500602
Chris Mason925baed2008-06-25 16:01:30 -0400603 btrfs_copy_root(trans, root, old, &tmp, objectid);
604 btrfs_tree_unlock(old);
605 free_extent_buffer(old);
Chris Mason3063d292008-01-08 15:46:30 -0500606
Chris Mason80b67942008-02-01 16:35:04 -0500607 btrfs_set_root_bytenr(new_root_item, tmp->start);
608 btrfs_set_root_level(new_root_item, btrfs_header_level(tmp));
Chris Mason3063d292008-01-08 15:46:30 -0500609 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
Chris Mason80b67942008-02-01 16:35:04 -0500610 new_root_item);
Chris Mason925baed2008-06-25 16:01:30 -0400611 btrfs_tree_unlock(tmp);
Chris Mason3063d292008-01-08 15:46:30 -0500612 free_extent_buffer(tmp);
613 if (ret)
614 goto fail;
615
616 /*
617 * insert the directory item
618 */
619 key.offset = (u64)-1;
Sven Wegener3b963622008-06-09 21:57:42 -0400620 namelen = strlen(pending->name);
Chris Mason3063d292008-01-08 15:46:30 -0500621 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
Sven Wegener3b963622008-06-09 21:57:42 -0400622 pending->name, namelen,
Chris Mason3063d292008-01-08 15:46:30 -0500623 root->fs_info->sb->s_root->d_inode->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -0400624 &key, BTRFS_FT_DIR, 0);
Chris Mason3063d292008-01-08 15:46:30 -0500625
626 if (ret)
627 goto fail;
628
629 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
630 pending->name, strlen(pending->name), objectid,
Josef Bacikaec74772008-07-24 12:12:38 -0400631 root->fs_info->sb->s_root->d_inode->i_ino, 0);
Sven Wegener3b963622008-06-09 21:57:42 -0400632
633 /* Invalidate existing dcache entry for new snapshot. */
634 btrfs_invalidate_dcache_root(root, pending->name, namelen);
635
Chris Mason3063d292008-01-08 15:46:30 -0500636fail:
Chris Mason80b67942008-02-01 16:35:04 -0500637 kfree(new_root_item);
Chris Mason3063d292008-01-08 15:46:30 -0500638 return ret;
639}
640
Chris Mason80b67942008-02-01 16:35:04 -0500641static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
642 struct btrfs_fs_info *fs_info)
Chris Mason3063d292008-01-08 15:46:30 -0500643{
644 struct btrfs_pending_snapshot *pending;
645 struct list_head *head = &trans->transaction->pending_snapshots;
646 int ret;
647
648 while(!list_empty(head)) {
649 pending = list_entry(head->next,
650 struct btrfs_pending_snapshot, list);
651 ret = create_pending_snapshot(trans, fs_info, pending);
652 BUG_ON(ret);
653 list_del(&pending->list);
654 kfree(pending->name);
655 kfree(pending);
656 }
Chris Masondc17ff82008-01-08 15:46:30 -0500657 return 0;
658}
659
Chris Mason79154b12007-03-22 15:59:16 -0400660int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
661 struct btrfs_root *root)
662{
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400663 unsigned long joined = 0;
664 unsigned long timeout = 1;
Chris Mason79154b12007-03-22 15:59:16 -0400665 struct btrfs_transaction *cur_trans;
Chris Mason8fd17792007-04-19 21:01:03 -0400666 struct btrfs_transaction *prev_trans = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -0400667 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400668 struct list_head dirty_fs_roots;
Chris Masond1310b22008-01-24 16:13:08 -0500669 struct extent_io_tree *pinned_copy;
Chris Mason79154b12007-03-22 15:59:16 -0400670 DEFINE_WAIT(wait);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400671 int ret;
Chris Mason79154b12007-03-22 15:59:16 -0400672
Chris Mason0f7d52f2007-04-09 10:42:37 -0400673 INIT_LIST_HEAD(&dirty_fs_roots);
Chris Masond6e4a422007-04-06 15:37:36 -0400674
Chris Mason79154b12007-03-22 15:59:16 -0400675 mutex_lock(&root->fs_info->trans_mutex);
676 if (trans->transaction->in_commit) {
677 cur_trans = trans->transaction;
678 trans->transaction->use_count++;
Chris Masonccd467d2007-06-28 15:57:36 -0400679 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400680 btrfs_end_transaction(trans, root);
Chris Masonccd467d2007-06-28 15:57:36 -0400681
Chris Mason79154b12007-03-22 15:59:16 -0400682 ret = wait_for_commit(root, cur_trans);
683 BUG_ON(ret);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400684
685 mutex_lock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400686 put_transaction(cur_trans);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400687 mutex_unlock(&root->fs_info->trans_mutex);
688
Chris Mason79154b12007-03-22 15:59:16 -0400689 return 0;
690 }
Chris Mason4313b392008-01-03 09:08:48 -0500691
692 pinned_copy = kmalloc(sizeof(*pinned_copy), GFP_NOFS);
693 if (!pinned_copy)
694 return -ENOMEM;
695
Chris Masond1310b22008-01-24 16:13:08 -0500696 extent_io_tree_init(pinned_copy,
Chris Mason4313b392008-01-03 09:08:48 -0500697 root->fs_info->btree_inode->i_mapping, GFP_NOFS);
698
Chris Mason2c90e5d2007-04-02 10:50:19 -0400699 trans->transaction->in_commit = 1;
Chris Masonf9295742008-07-17 12:54:14 -0400700 trans->transaction->blocked = 1;
Chris Masonccd467d2007-06-28 15:57:36 -0400701 cur_trans = trans->transaction;
702 if (cur_trans->list.prev != &root->fs_info->trans_list) {
703 prev_trans = list_entry(cur_trans->list.prev,
704 struct btrfs_transaction, list);
705 if (!prev_trans->commit_done) {
706 prev_trans->use_count++;
Chris Masonccd467d2007-06-28 15:57:36 -0400707 mutex_unlock(&root->fs_info->trans_mutex);
708
709 wait_for_commit(root, prev_trans);
Chris Masonccd467d2007-06-28 15:57:36 -0400710
Chris Masonccd467d2007-06-28 15:57:36 -0400711 mutex_lock(&root->fs_info->trans_mutex);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400712 put_transaction(prev_trans);
Chris Masonccd467d2007-06-28 15:57:36 -0400713 }
714 }
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400715
716 do {
717 joined = cur_trans->num_joined;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400718 WARN_ON(cur_trans != trans->transaction);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400719 prepare_to_wait(&cur_trans->writer_wait, &wait,
Chris Mason79154b12007-03-22 15:59:16 -0400720 TASK_UNINTERRUPTIBLE);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400721
722 if (cur_trans->num_writers > 1)
723 timeout = MAX_SCHEDULE_TIMEOUT;
724 else
725 timeout = 1;
726
Chris Mason79154b12007-03-22 15:59:16 -0400727 mutex_unlock(&root->fs_info->trans_mutex);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400728
729 schedule_timeout(timeout);
730
Chris Mason79154b12007-03-22 15:59:16 -0400731 mutex_lock(&root->fs_info->trans_mutex);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400732 finish_wait(&cur_trans->writer_wait, &wait);
733 } while (cur_trans->num_writers > 1 ||
734 (cur_trans->num_joined != joined));
735
Chris Mason3063d292008-01-08 15:46:30 -0500736 ret = create_pending_snapshots(trans, root->fs_info);
737 BUG_ON(ret);
738
Chris Mason2c90e5d2007-04-02 10:50:19 -0400739 WARN_ON(cur_trans != trans->transaction);
Chris Masondc17ff82008-01-08 15:46:30 -0500740
Chris Mason54aa1f42007-06-22 14:16:25 -0400741 ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix,
742 &dirty_fs_roots);
743 BUG_ON(ret);
744
Chris Mason79154b12007-03-22 15:59:16 -0400745 ret = btrfs_commit_tree_roots(trans, root);
746 BUG_ON(ret);
Chris Mason54aa1f42007-06-22 14:16:25 -0400747
Chris Mason78fae272007-03-25 11:35:08 -0400748 cur_trans = root->fs_info->running_transaction;
Chris Masoncee36a02008-01-15 08:40:48 -0500749 spin_lock(&root->fs_info->new_trans_lock);
Chris Mason78fae272007-03-25 11:35:08 -0400750 root->fs_info->running_transaction = NULL;
Chris Masoncee36a02008-01-15 08:40:48 -0500751 spin_unlock(&root->fs_info->new_trans_lock);
Chris Mason4b52dff2007-06-26 10:06:50 -0400752 btrfs_set_super_generation(&root->fs_info->super_copy,
753 cur_trans->transid);
754 btrfs_set_super_root(&root->fs_info->super_copy,
Chris Masondb945352007-10-15 16:15:53 -0400755 root->fs_info->tree_root->node->start);
756 btrfs_set_super_root_level(&root->fs_info->super_copy,
757 btrfs_header_level(root->fs_info->tree_root->node));
Chris Mason5f39d392007-10-15 16:14:19 -0400758
Chris Mason0b86a832008-03-24 15:01:56 -0400759 btrfs_set_super_chunk_root(&root->fs_info->super_copy,
760 chunk_root->node->start);
761 btrfs_set_super_chunk_root_level(&root->fs_info->super_copy,
762 btrfs_header_level(chunk_root->node));
Chris Masona061fc82008-05-07 11:43:44 -0400763 memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy,
764 sizeof(root->fs_info->super_copy));
Chris Masonccd467d2007-06-28 15:57:36 -0400765
Chris Mason4313b392008-01-03 09:08:48 -0500766 btrfs_copy_pinned(root, pinned_copy);
Chris Masonccd467d2007-06-28 15:57:36 -0400767
Chris Masonf9295742008-07-17 12:54:14 -0400768 trans->transaction->blocked = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400769 wake_up(&root->fs_info->transaction_throttle);
Chris Masonf9295742008-07-17 12:54:14 -0400770 wake_up(&root->fs_info->transaction_wait);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400771
Chris Mason78fae272007-03-25 11:35:08 -0400772 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400773 ret = btrfs_write_and_wait_transaction(trans, root);
774 BUG_ON(ret);
Chris Mason79154b12007-03-22 15:59:16 -0400775 write_ctree_super(trans, root);
Chris Mason4313b392008-01-03 09:08:48 -0500776
Chris Mason4313b392008-01-03 09:08:48 -0500777 btrfs_finish_extent_commit(trans, root, pinned_copy);
Chris Mason78fae272007-03-25 11:35:08 -0400778 mutex_lock(&root->fs_info->trans_mutex);
Chris Mason4313b392008-01-03 09:08:48 -0500779
780 kfree(pinned_copy);
781
Chris Mason2c90e5d2007-04-02 10:50:19 -0400782 cur_trans->commit_done = 1;
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400783 root->fs_info->last_trans_committed = cur_trans->transid;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400784 wake_up(&cur_trans->commit_wait);
Chris Mason79154b12007-03-22 15:59:16 -0400785 put_transaction(cur_trans);
Chris Mason78fae272007-03-25 11:35:08 -0400786 put_transaction(cur_trans);
Josef Bacik58176a92007-08-29 15:47:34 -0400787
Chris Masonfacda1e2007-06-08 18:11:48 -0400788 if (root->fs_info->closing)
789 list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots);
790 else
791 list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots);
Josef Bacik58176a92007-08-29 15:47:34 -0400792
Chris Mason78fae272007-03-25 11:35:08 -0400793 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400794 kmem_cache_free(btrfs_trans_handle_cachep, trans);
Chris Mason79154b12007-03-22 15:59:16 -0400795
Chris Masonfacda1e2007-06-08 18:11:48 -0400796 if (root->fs_info->closing) {
Chris Masonfacda1e2007-06-08 18:11:48 -0400797 drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);
Chris Masonfacda1e2007-06-08 18:11:48 -0400798 }
Chris Mason79154b12007-03-22 15:59:16 -0400799 return ret;
800}
801
Chris Masone9d0b132007-08-10 14:06:19 -0400802int btrfs_clean_old_snapshots(struct btrfs_root *root)
803{
804 struct list_head dirty_roots;
805 INIT_LIST_HEAD(&dirty_roots);
Chris Masona74a4b92008-06-25 16:01:31 -0400806again:
Chris Masone9d0b132007-08-10 14:06:19 -0400807 mutex_lock(&root->fs_info->trans_mutex);
808 list_splice_init(&root->fs_info->dead_roots, &dirty_roots);
809 mutex_unlock(&root->fs_info->trans_mutex);
810
811 if (!list_empty(&dirty_roots)) {
812 drop_dirty_roots(root, &dirty_roots);
Chris Masona74a4b92008-06-25 16:01:31 -0400813 goto again;
Chris Masone9d0b132007-08-10 14:06:19 -0400814 }
815 return 0;
816}
Chris Mason08607c12007-06-08 15:33:54 -0400817