blob: b8be6703189ad2f33c26d0bd0033c210f44984b4 [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
Chris Masonab78c842008-07-29 16:15:18 -0400205void btrfs_throttle(struct btrfs_root *root)
206{
207 struct btrfs_fs_info *info = root->fs_info;
208
209harder:
210 if (atomic_read(&info->throttles)) {
211 DEFINE_WAIT(wait);
212 int thr;
213 int harder_count = 0;
214 thr = atomic_read(&info->throttle_gen);
215
216 do {
217 prepare_to_wait(&info->transaction_throttle,
218 &wait, TASK_UNINTERRUPTIBLE);
219 if (!atomic_read(&info->throttles)) {
220 finish_wait(&info->transaction_throttle, &wait);
221 break;
222 }
223 schedule();
224 finish_wait(&info->transaction_throttle, &wait);
225 } while (thr == atomic_read(&info->throttle_gen));
226
227 if (harder_count < 5 &&
228 info->total_ref_cache_size > 5 * 1024 * 1024) {
229 harder_count++;
230 goto harder;
231 }
232
233 if (harder_count < 10 &&
234 info->total_ref_cache_size > 10 * 1024 * 1024) {
235 harder_count++;
236 goto harder;
237 }
238 }
239}
240
Chris Mason89ce8a62008-06-25 16:01:31 -0400241static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
242 struct btrfs_root *root, int throttle)
Chris Mason79154b12007-03-22 15:59:16 -0400243{
244 struct btrfs_transaction *cur_trans;
Chris Masonab78c842008-07-29 16:15:18 -0400245 struct btrfs_fs_info *info = root->fs_info;
Chris Masond6e4a422007-04-06 15:37:36 -0400246
Chris Masonab78c842008-07-29 16:15:18 -0400247 mutex_lock(&info->trans_mutex);
248 cur_trans = info->running_transaction;
Chris Masonccd467d2007-06-28 15:57:36 -0400249 WARN_ON(cur_trans != trans->transaction);
Chris Masond5719762007-03-23 10:01:08 -0400250 WARN_ON(cur_trans->num_writers < 1);
Chris Masonccd467d2007-06-28 15:57:36 -0400251 cur_trans->num_writers--;
Chris Mason89ce8a62008-06-25 16:01:31 -0400252
Chris Mason79154b12007-03-22 15:59:16 -0400253 if (waitqueue_active(&cur_trans->writer_wait))
254 wake_up(&cur_trans->writer_wait);
Chris Mason79154b12007-03-22 15:59:16 -0400255 put_transaction(cur_trans);
Chris Masonab78c842008-07-29 16:15:18 -0400256 mutex_unlock(&info->trans_mutex);
Chris Masond6025572007-03-30 14:27:56 -0400257 memset(trans, 0, sizeof(*trans));
Chris Mason2c90e5d2007-04-02 10:50:19 -0400258 kmem_cache_free(btrfs_trans_handle_cachep, trans);
Chris Masonab78c842008-07-29 16:15:18 -0400259
260 if (throttle)
261 btrfs_throttle(root);
262
Chris Mason79154b12007-03-22 15:59:16 -0400263 return 0;
264}
265
Chris Mason89ce8a62008-06-25 16:01:31 -0400266int btrfs_end_transaction(struct btrfs_trans_handle *trans,
267 struct btrfs_root *root)
268{
269 return __btrfs_end_transaction(trans, root, 0);
270}
271
272int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
273 struct btrfs_root *root)
274{
275 return __btrfs_end_transaction(trans, root, 1);
276}
277
Chris Mason79154b12007-03-22 15:59:16 -0400278
279int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
280 struct btrfs_root *root)
281{
Chris Mason7c4452b2007-04-28 09:29:35 -0400282 int ret;
Chris Mason7c4452b2007-04-28 09:29:35 -0400283 int err;
284 int werr = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500285 struct extent_io_tree *dirty_pages;
Chris Mason7c4452b2007-04-28 09:29:35 -0400286 struct page *page;
Chris Mason7c4452b2007-04-28 09:29:35 -0400287 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Mason5f39d392007-10-15 16:14:19 -0400288 u64 start;
289 u64 end;
290 unsigned long index;
Chris Mason7c4452b2007-04-28 09:29:35 -0400291
292 if (!trans || !trans->transaction) {
293 return filemap_write_and_wait(btree_inode->i_mapping);
294 }
295 dirty_pages = &trans->transaction->dirty_pages;
296 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400297 ret = find_first_extent_bit(dirty_pages, 0, &start, &end,
298 EXTENT_DIRTY);
299 if (ret)
Chris Mason7c4452b2007-04-28 09:29:35 -0400300 break;
Chris Mason5f39d392007-10-15 16:14:19 -0400301 clear_extent_dirty(dirty_pages, start, end, GFP_NOFS);
302 while(start <= end) {
303 index = start >> PAGE_CACHE_SHIFT;
Chris Mason35ebb932007-10-30 16:56:53 -0400304 start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
Chris Mason5f39d392007-10-15 16:14:19 -0400305 page = find_lock_page(btree_inode->i_mapping, index);
Chris Mason7c4452b2007-04-28 09:29:35 -0400306 if (!page)
307 continue;
Chris Mason6702ed42007-08-07 16:15:09 -0400308 if (PageWriteback(page)) {
309 if (PageDirty(page))
310 wait_on_page_writeback(page);
311 else {
312 unlock_page(page);
313 page_cache_release(page);
314 continue;
315 }
316 }
Chris Mason7c4452b2007-04-28 09:29:35 -0400317 err = write_one_page(page, 0);
318 if (err)
319 werr = err;
320 page_cache_release(page);
321 }
322 }
323 err = filemap_fdatawait(btree_inode->i_mapping);
324 if (err)
325 werr = err;
326 return werr;
Chris Mason79154b12007-03-22 15:59:16 -0400327}
328
Chris Mason0b86a832008-03-24 15:01:56 -0400329static int update_cowonly_root(struct btrfs_trans_handle *trans,
330 struct btrfs_root *root)
331{
332 int ret;
333 u64 old_root_bytenr;
334 struct btrfs_root *tree_root = root->fs_info->tree_root;
335
336 btrfs_write_dirty_block_groups(trans, root);
337 while(1) {
338 old_root_bytenr = btrfs_root_bytenr(&root->root_item);
339 if (old_root_bytenr == root->node->start)
340 break;
341 btrfs_set_root_bytenr(&root->root_item,
342 root->node->start);
343 btrfs_set_root_level(&root->root_item,
344 btrfs_header_level(root->node));
345 ret = btrfs_update_root(trans, tree_root,
346 &root->root_key,
347 &root->root_item);
348 BUG_ON(ret);
349 btrfs_write_dirty_block_groups(trans, root);
350 }
351 return 0;
352}
353
Chris Mason79154b12007-03-22 15:59:16 -0400354int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans,
355 struct btrfs_root *root)
356{
Chris Mason79154b12007-03-22 15:59:16 -0400357 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason0b86a832008-03-24 15:01:56 -0400358 struct list_head *next;
Chris Mason79154b12007-03-22 15:59:16 -0400359
Chris Mason0b86a832008-03-24 15:01:56 -0400360 while(!list_empty(&fs_info->dirty_cowonly_roots)) {
361 next = fs_info->dirty_cowonly_roots.next;
362 list_del_init(next);
363 root = list_entry(next, struct btrfs_root, dirty_list);
364 update_cowonly_root(trans, root);
Chris Mason017e5362008-07-28 15:32:51 -0400365 if (root->fs_info->closing)
366 btrfs_remove_leaf_refs(root);
Chris Mason79154b12007-03-22 15:59:16 -0400367 }
368 return 0;
369}
370
Chris Mason5ce14bb2007-09-11 11:15:39 -0400371int btrfs_add_dead_root(struct btrfs_root *root,
372 struct btrfs_root *latest,
373 struct list_head *dead_list)
Chris Mason5eda7b52007-06-22 14:16:25 -0400374{
375 struct dirty_root *dirty;
376
377 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
378 if (!dirty)
379 return -ENOMEM;
Chris Mason5eda7b52007-06-22 14:16:25 -0400380 dirty->root = root;
Chris Mason5ce14bb2007-09-11 11:15:39 -0400381 dirty->latest_root = latest;
Chris Mason5eda7b52007-06-22 14:16:25 -0400382 list_add(&dirty->list, dead_list);
383 return 0;
384}
385
Chris Mason80b67942008-02-01 16:35:04 -0500386static noinline int add_dirty_roots(struct btrfs_trans_handle *trans,
387 struct radix_tree_root *radix,
388 struct list_head *list)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400389{
390 struct dirty_root *dirty;
391 struct btrfs_root *gang[8];
392 struct btrfs_root *root;
393 int i;
394 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400395 int err = 0;
Chris Mason5eda7b52007-06-22 14:16:25 -0400396 u32 refs;
Chris Mason54aa1f42007-06-22 14:16:25 -0400397
Chris Mason0f7d52f2007-04-09 10:42:37 -0400398 while(1) {
399 ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0,
400 ARRAY_SIZE(gang),
401 BTRFS_ROOT_TRANS_TAG);
402 if (ret == 0)
403 break;
404 for (i = 0; i < ret; i++) {
405 root = gang[i];
Chris Mason2619ba12007-04-10 16:58:11 -0400406 radix_tree_tag_clear(radix,
407 (unsigned long)root->root_key.objectid,
408 BTRFS_ROOT_TRANS_TAG);
Yan Zheng31153d82008-07-28 15:32:19 -0400409
410 BUG_ON(!root->ref_tree);
Chris Mason017e5362008-07-28 15:32:51 -0400411 dirty = root->dirty_root;
Yan Zheng31153d82008-07-28 15:32:19 -0400412
Chris Mason0f7d52f2007-04-09 10:42:37 -0400413 if (root->commit_root == root->node) {
Chris Masondb945352007-10-15 16:15:53 -0400414 WARN_ON(root->node->start !=
415 btrfs_root_bytenr(&root->root_item));
Yan Zheng31153d82008-07-28 15:32:19 -0400416
Chris Mason5f39d392007-10-15 16:14:19 -0400417 free_extent_buffer(root->commit_root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400418 root->commit_root = NULL;
Yan Zheng31153d82008-07-28 15:32:19 -0400419
420 kfree(dirty->root);
421 kfree(dirty);
Josef Bacik58176a92007-08-29 15:47:34 -0400422
423 /* make sure to update the root on disk
424 * so we get any updates to the block used
425 * counts
426 */
427 err = btrfs_update_root(trans,
428 root->fs_info->tree_root,
429 &root->root_key,
430 &root->root_item);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400431 continue;
432 }
Chris Mason9f3a7422007-08-07 15:52:19 -0400433
434 memset(&root->root_item.drop_progress, 0,
435 sizeof(struct btrfs_disk_key));
436 root->root_item.drop_level = 0;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400437 root->commit_root = NULL;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400438 root->root_key.offset = root->fs_info->generation;
Chris Masondb945352007-10-15 16:15:53 -0400439 btrfs_set_root_bytenr(&root->root_item,
440 root->node->start);
441 btrfs_set_root_level(&root->root_item,
442 btrfs_header_level(root->node));
Chris Mason0f7d52f2007-04-09 10:42:37 -0400443 err = btrfs_insert_root(trans, root->fs_info->tree_root,
444 &root->root_key,
445 &root->root_item);
Chris Mason54aa1f42007-06-22 14:16:25 -0400446 if (err)
447 break;
Chris Mason9f3a7422007-08-07 15:52:19 -0400448
449 refs = btrfs_root_refs(&dirty->root->root_item);
450 btrfs_set_root_refs(&dirty->root->root_item, refs - 1);
Chris Mason5eda7b52007-06-22 14:16:25 -0400451 err = btrfs_update_root(trans, root->fs_info->tree_root,
Chris Mason9f3a7422007-08-07 15:52:19 -0400452 &dirty->root->root_key,
453 &dirty->root->root_item);
Chris Mason5eda7b52007-06-22 14:16:25 -0400454
455 BUG_ON(err);
Chris Mason9f3a7422007-08-07 15:52:19 -0400456 if (refs == 1) {
Chris Mason5eda7b52007-06-22 14:16:25 -0400457 list_add(&dirty->list, list);
Chris Mason9f3a7422007-08-07 15:52:19 -0400458 } else {
459 WARN_ON(1);
Yan Zheng31153d82008-07-28 15:32:19 -0400460 free_extent_buffer(dirty->root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -0400461 kfree(dirty->root);
Chris Mason5eda7b52007-06-22 14:16:25 -0400462 kfree(dirty);
Chris Mason9f3a7422007-08-07 15:52:19 -0400463 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400464 }
465 }
Chris Mason54aa1f42007-06-22 14:16:25 -0400466 return err;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400467}
468
Chris Masone9d0b132007-08-10 14:06:19 -0400469int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
470{
471 struct btrfs_fs_info *info = root->fs_info;
472 int ret;
473 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400474 unsigned long nr;
Chris Masone9d0b132007-08-10 14:06:19 -0400475
Chris Masona2135012008-06-25 16:01:30 -0400476 smp_mb();
Chris Masone9d0b132007-08-10 14:06:19 -0400477 if (root->defrag_running)
478 return 0;
Chris Masone9d0b132007-08-10 14:06:19 -0400479 trans = btrfs_start_transaction(root, 1);
Chris Mason6b800532007-10-15 16:17:34 -0400480 while (1) {
Chris Masone9d0b132007-08-10 14:06:19 -0400481 root->defrag_running = 1;
482 ret = btrfs_defrag_leaves(trans, root, cacheonly);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400483 nr = trans->blocks_used;
Chris Masone9d0b132007-08-10 14:06:19 -0400484 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400485 btrfs_btree_balance_dirty(info->tree_root, nr);
Chris Masone9d0b132007-08-10 14:06:19 -0400486 cond_resched();
487
Chris Masone9d0b132007-08-10 14:06:19 -0400488 trans = btrfs_start_transaction(root, 1);
Chris Mason3f157a22008-06-25 16:01:31 -0400489 if (root->fs_info->closing || ret != -EAGAIN)
Chris Masone9d0b132007-08-10 14:06:19 -0400490 break;
491 }
492 root->defrag_running = 0;
Chris Masona2135012008-06-25 16:01:30 -0400493 smp_mb();
Chris Masone9d0b132007-08-10 14:06:19 -0400494 btrfs_end_transaction(trans, root);
495 return 0;
496}
497
Chris Mason80b67942008-02-01 16:35:04 -0500498static noinline int drop_dirty_roots(struct btrfs_root *tree_root,
499 struct list_head *list)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400500{
501 struct dirty_root *dirty;
502 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400503 unsigned long nr;
Chris Masondb945352007-10-15 16:15:53 -0400504 u64 num_bytes;
505 u64 bytes_used;
Chris Mason54aa1f42007-06-22 14:16:25 -0400506 int ret = 0;
Chris Mason9f3a7422007-08-07 15:52:19 -0400507 int err;
508
Chris Mason0f7d52f2007-04-09 10:42:37 -0400509 while(!list_empty(list)) {
Josef Bacik58176a92007-08-29 15:47:34 -0400510 struct btrfs_root *root;
511
Chris Mason017e5362008-07-28 15:32:51 -0400512 dirty = list_entry(list->prev, struct dirty_root, list);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400513 list_del_init(&dirty->list);
Chris Mason5eda7b52007-06-22 14:16:25 -0400514
Chris Masondb945352007-10-15 16:15:53 -0400515 num_bytes = btrfs_root_used(&dirty->root->root_item);
Josef Bacik58176a92007-08-29 15:47:34 -0400516 root = dirty->latest_root;
Chris Masona2135012008-06-25 16:01:30 -0400517 atomic_inc(&root->fs_info->throttles);
Josef Bacik58176a92007-08-29 15:47:34 -0400518
Chris Masona2135012008-06-25 16:01:30 -0400519 mutex_lock(&root->fs_info->drop_mutex);
Chris Mason9f3a7422007-08-07 15:52:19 -0400520 while(1) {
521 trans = btrfs_start_transaction(tree_root, 1);
522 ret = btrfs_drop_snapshot(trans, dirty->root);
523 if (ret != -EAGAIN) {
524 break;
525 }
Josef Bacik58176a92007-08-29 15:47:34 -0400526
Chris Mason9f3a7422007-08-07 15:52:19 -0400527 err = btrfs_update_root(trans,
528 tree_root,
529 &dirty->root->root_key,
530 &dirty->root->root_item);
531 if (err)
532 ret = err;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400533 nr = trans->blocks_used;
Chris Mason017e5362008-07-28 15:32:51 -0400534 ret = btrfs_end_transaction(trans, tree_root);
Chris Mason9f3a7422007-08-07 15:52:19 -0400535 BUG_ON(ret);
Chris Masona2135012008-06-25 16:01:30 -0400536
537 mutex_unlock(&root->fs_info->drop_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400538 btrfs_btree_balance_dirty(tree_root, nr);
Chris Mason4dc119042007-10-15 16:18:14 -0400539 cond_resched();
Chris Masona2135012008-06-25 16:01:30 -0400540 mutex_lock(&root->fs_info->drop_mutex);
Chris Mason9f3a7422007-08-07 15:52:19 -0400541 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400542 BUG_ON(ret);
Chris Masona2135012008-06-25 16:01:30 -0400543 atomic_dec(&root->fs_info->throttles);
Chris Mason017e5362008-07-28 15:32:51 -0400544 wake_up(&root->fs_info->transaction_throttle);
Josef Bacik58176a92007-08-29 15:47:34 -0400545
Chris Masona2135012008-06-25 16:01:30 -0400546 mutex_lock(&root->fs_info->alloc_mutex);
Chris Masondb945352007-10-15 16:15:53 -0400547 num_bytes -= btrfs_root_used(&dirty->root->root_item);
548 bytes_used = btrfs_root_used(&root->root_item);
549 if (num_bytes) {
Josef Bacik58176a92007-08-29 15:47:34 -0400550 record_root_in_trans(root);
Chris Mason5f39d392007-10-15 16:14:19 -0400551 btrfs_set_root_used(&root->root_item,
Chris Masondb945352007-10-15 16:15:53 -0400552 bytes_used - num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -0400553 }
Chris Masona2135012008-06-25 16:01:30 -0400554 mutex_unlock(&root->fs_info->alloc_mutex);
555
Chris Mason9f3a7422007-08-07 15:52:19 -0400556 ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key);
Josef Bacik58176a92007-08-29 15:47:34 -0400557 if (ret) {
558 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -0400559 break;
Josef Bacik58176a92007-08-29 15:47:34 -0400560 }
Chris Masona2135012008-06-25 16:01:30 -0400561 mutex_unlock(&root->fs_info->drop_mutex);
562
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400563 nr = trans->blocks_used;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400564 ret = btrfs_end_transaction(trans, tree_root);
565 BUG_ON(ret);
Chris Mason5eda7b52007-06-22 14:16:25 -0400566
Chris Masonf510cfe2007-10-15 16:14:48 -0400567 free_extent_buffer(dirty->root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -0400568 kfree(dirty->root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400569 kfree(dirty);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400570
571 btrfs_btree_balance_dirty(tree_root, nr);
Chris Mason4dc119042007-10-15 16:18:14 -0400572 cond_resched();
Chris Mason0f7d52f2007-04-09 10:42:37 -0400573 }
Chris Mason54aa1f42007-06-22 14:16:25 -0400574 return ret;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400575}
576
Chris Mason80b67942008-02-01 16:35:04 -0500577static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
Chris Mason3063d292008-01-08 15:46:30 -0500578 struct btrfs_fs_info *fs_info,
579 struct btrfs_pending_snapshot *pending)
580{
581 struct btrfs_key key;
Chris Mason80b67942008-02-01 16:35:04 -0500582 struct btrfs_root_item *new_root_item;
Chris Mason3063d292008-01-08 15:46:30 -0500583 struct btrfs_root *tree_root = fs_info->tree_root;
584 struct btrfs_root *root = pending->root;
585 struct extent_buffer *tmp;
Chris Mason925baed2008-06-25 16:01:30 -0400586 struct extent_buffer *old;
Chris Mason3063d292008-01-08 15:46:30 -0500587 int ret;
Sven Wegener3b963622008-06-09 21:57:42 -0400588 int namelen;
Chris Mason3063d292008-01-08 15:46:30 -0500589 u64 objectid;
590
Chris Mason80b67942008-02-01 16:35:04 -0500591 new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
592 if (!new_root_item) {
593 ret = -ENOMEM;
594 goto fail;
595 }
Chris Mason3063d292008-01-08 15:46:30 -0500596 ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid);
597 if (ret)
598 goto fail;
599
Chris Mason80b67942008-02-01 16:35:04 -0500600 memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
Chris Mason3063d292008-01-08 15:46:30 -0500601
602 key.objectid = objectid;
603 key.offset = 1;
604 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
605
Chris Mason925baed2008-06-25 16:01:30 -0400606 old = btrfs_lock_root_node(root);
607 btrfs_cow_block(trans, root, old, NULL, 0, &old);
Chris Mason3063d292008-01-08 15:46:30 -0500608
Chris Mason925baed2008-06-25 16:01:30 -0400609 btrfs_copy_root(trans, root, old, &tmp, objectid);
610 btrfs_tree_unlock(old);
611 free_extent_buffer(old);
Chris Mason3063d292008-01-08 15:46:30 -0500612
Chris Mason80b67942008-02-01 16:35:04 -0500613 btrfs_set_root_bytenr(new_root_item, tmp->start);
614 btrfs_set_root_level(new_root_item, btrfs_header_level(tmp));
Chris Mason3063d292008-01-08 15:46:30 -0500615 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
Chris Mason80b67942008-02-01 16:35:04 -0500616 new_root_item);
Chris Mason925baed2008-06-25 16:01:30 -0400617 btrfs_tree_unlock(tmp);
Chris Mason3063d292008-01-08 15:46:30 -0500618 free_extent_buffer(tmp);
619 if (ret)
620 goto fail;
621
622 /*
623 * insert the directory item
624 */
625 key.offset = (u64)-1;
Sven Wegener3b963622008-06-09 21:57:42 -0400626 namelen = strlen(pending->name);
Chris Mason3063d292008-01-08 15:46:30 -0500627 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
Sven Wegener3b963622008-06-09 21:57:42 -0400628 pending->name, namelen,
Chris Mason3063d292008-01-08 15:46:30 -0500629 root->fs_info->sb->s_root->d_inode->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -0400630 &key, BTRFS_FT_DIR, 0);
Chris Mason3063d292008-01-08 15:46:30 -0500631
632 if (ret)
633 goto fail;
634
635 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
636 pending->name, strlen(pending->name), objectid,
Josef Bacikaec74772008-07-24 12:12:38 -0400637 root->fs_info->sb->s_root->d_inode->i_ino, 0);
Sven Wegener3b963622008-06-09 21:57:42 -0400638
639 /* Invalidate existing dcache entry for new snapshot. */
640 btrfs_invalidate_dcache_root(root, pending->name, namelen);
641
Chris Mason3063d292008-01-08 15:46:30 -0500642fail:
Chris Mason80b67942008-02-01 16:35:04 -0500643 kfree(new_root_item);
Chris Mason3063d292008-01-08 15:46:30 -0500644 return ret;
645}
646
Chris Mason80b67942008-02-01 16:35:04 -0500647static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
648 struct btrfs_fs_info *fs_info)
Chris Mason3063d292008-01-08 15:46:30 -0500649{
650 struct btrfs_pending_snapshot *pending;
651 struct list_head *head = &trans->transaction->pending_snapshots;
652 int ret;
653
654 while(!list_empty(head)) {
655 pending = list_entry(head->next,
656 struct btrfs_pending_snapshot, list);
657 ret = create_pending_snapshot(trans, fs_info, pending);
658 BUG_ON(ret);
659 list_del(&pending->list);
660 kfree(pending->name);
661 kfree(pending);
662 }
Chris Masondc17ff82008-01-08 15:46:30 -0500663 return 0;
664}
665
Chris Mason79154b12007-03-22 15:59:16 -0400666int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
667 struct btrfs_root *root)
668{
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400669 unsigned long joined = 0;
670 unsigned long timeout = 1;
Chris Mason79154b12007-03-22 15:59:16 -0400671 struct btrfs_transaction *cur_trans;
Chris Mason8fd17792007-04-19 21:01:03 -0400672 struct btrfs_transaction *prev_trans = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -0400673 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400674 struct list_head dirty_fs_roots;
Chris Masond1310b22008-01-24 16:13:08 -0500675 struct extent_io_tree *pinned_copy;
Chris Mason79154b12007-03-22 15:59:16 -0400676 DEFINE_WAIT(wait);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400677 int ret;
Chris Mason79154b12007-03-22 15:59:16 -0400678
Chris Mason0f7d52f2007-04-09 10:42:37 -0400679 INIT_LIST_HEAD(&dirty_fs_roots);
Chris Masond6e4a422007-04-06 15:37:36 -0400680
Chris Mason79154b12007-03-22 15:59:16 -0400681 mutex_lock(&root->fs_info->trans_mutex);
682 if (trans->transaction->in_commit) {
683 cur_trans = trans->transaction;
684 trans->transaction->use_count++;
Chris Masonccd467d2007-06-28 15:57:36 -0400685 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400686 btrfs_end_transaction(trans, root);
Chris Masonccd467d2007-06-28 15:57:36 -0400687
Chris Mason79154b12007-03-22 15:59:16 -0400688 ret = wait_for_commit(root, cur_trans);
689 BUG_ON(ret);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400690
691 mutex_lock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400692 put_transaction(cur_trans);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400693 mutex_unlock(&root->fs_info->trans_mutex);
694
Chris Mason79154b12007-03-22 15:59:16 -0400695 return 0;
696 }
Chris Mason4313b392008-01-03 09:08:48 -0500697
698 pinned_copy = kmalloc(sizeof(*pinned_copy), GFP_NOFS);
699 if (!pinned_copy)
700 return -ENOMEM;
701
Chris Masond1310b22008-01-24 16:13:08 -0500702 extent_io_tree_init(pinned_copy,
Chris Mason4313b392008-01-03 09:08:48 -0500703 root->fs_info->btree_inode->i_mapping, GFP_NOFS);
704
Chris Mason2c90e5d2007-04-02 10:50:19 -0400705 trans->transaction->in_commit = 1;
Chris Masonf9295742008-07-17 12:54:14 -0400706 trans->transaction->blocked = 1;
Chris Masonccd467d2007-06-28 15:57:36 -0400707 cur_trans = trans->transaction;
708 if (cur_trans->list.prev != &root->fs_info->trans_list) {
709 prev_trans = list_entry(cur_trans->list.prev,
710 struct btrfs_transaction, list);
711 if (!prev_trans->commit_done) {
712 prev_trans->use_count++;
Chris Masonccd467d2007-06-28 15:57:36 -0400713 mutex_unlock(&root->fs_info->trans_mutex);
714
715 wait_for_commit(root, prev_trans);
Chris Masonccd467d2007-06-28 15:57:36 -0400716
Chris Masonccd467d2007-06-28 15:57:36 -0400717 mutex_lock(&root->fs_info->trans_mutex);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400718 put_transaction(prev_trans);
Chris Masonccd467d2007-06-28 15:57:36 -0400719 }
720 }
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400721
722 do {
723 joined = cur_trans->num_joined;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400724 WARN_ON(cur_trans != trans->transaction);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400725 prepare_to_wait(&cur_trans->writer_wait, &wait,
Chris Mason79154b12007-03-22 15:59:16 -0400726 TASK_UNINTERRUPTIBLE);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400727
728 if (cur_trans->num_writers > 1)
729 timeout = MAX_SCHEDULE_TIMEOUT;
730 else
731 timeout = 1;
732
Chris Mason79154b12007-03-22 15:59:16 -0400733 mutex_unlock(&root->fs_info->trans_mutex);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400734
735 schedule_timeout(timeout);
736
Chris Mason79154b12007-03-22 15:59:16 -0400737 mutex_lock(&root->fs_info->trans_mutex);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400738 finish_wait(&cur_trans->writer_wait, &wait);
739 } while (cur_trans->num_writers > 1 ||
740 (cur_trans->num_joined != joined));
741
Chris Mason3063d292008-01-08 15:46:30 -0500742 ret = create_pending_snapshots(trans, root->fs_info);
743 BUG_ON(ret);
744
Chris Mason2c90e5d2007-04-02 10:50:19 -0400745 WARN_ON(cur_trans != trans->transaction);
Chris Masondc17ff82008-01-08 15:46:30 -0500746
Chris Mason54aa1f42007-06-22 14:16:25 -0400747 ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix,
748 &dirty_fs_roots);
749 BUG_ON(ret);
750
Chris Mason79154b12007-03-22 15:59:16 -0400751 ret = btrfs_commit_tree_roots(trans, root);
752 BUG_ON(ret);
Chris Mason54aa1f42007-06-22 14:16:25 -0400753
Chris Mason78fae272007-03-25 11:35:08 -0400754 cur_trans = root->fs_info->running_transaction;
Chris Masoncee36a02008-01-15 08:40:48 -0500755 spin_lock(&root->fs_info->new_trans_lock);
Chris Mason78fae272007-03-25 11:35:08 -0400756 root->fs_info->running_transaction = NULL;
Chris Masoncee36a02008-01-15 08:40:48 -0500757 spin_unlock(&root->fs_info->new_trans_lock);
Chris Mason4b52dff2007-06-26 10:06:50 -0400758 btrfs_set_super_generation(&root->fs_info->super_copy,
759 cur_trans->transid);
760 btrfs_set_super_root(&root->fs_info->super_copy,
Chris Masondb945352007-10-15 16:15:53 -0400761 root->fs_info->tree_root->node->start);
762 btrfs_set_super_root_level(&root->fs_info->super_copy,
763 btrfs_header_level(root->fs_info->tree_root->node));
Chris Mason5f39d392007-10-15 16:14:19 -0400764
Chris Mason0b86a832008-03-24 15:01:56 -0400765 btrfs_set_super_chunk_root(&root->fs_info->super_copy,
766 chunk_root->node->start);
767 btrfs_set_super_chunk_root_level(&root->fs_info->super_copy,
768 btrfs_header_level(chunk_root->node));
Chris Masona061fc82008-05-07 11:43:44 -0400769 memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy,
770 sizeof(root->fs_info->super_copy));
Chris Masonccd467d2007-06-28 15:57:36 -0400771
Chris Mason4313b392008-01-03 09:08:48 -0500772 btrfs_copy_pinned(root, pinned_copy);
Chris Masonccd467d2007-06-28 15:57:36 -0400773
Chris Masonf9295742008-07-17 12:54:14 -0400774 trans->transaction->blocked = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400775 wake_up(&root->fs_info->transaction_throttle);
Chris Masonf9295742008-07-17 12:54:14 -0400776 wake_up(&root->fs_info->transaction_wait);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400777
Chris Mason78fae272007-03-25 11:35:08 -0400778 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400779 ret = btrfs_write_and_wait_transaction(trans, root);
780 BUG_ON(ret);
Chris Mason79154b12007-03-22 15:59:16 -0400781 write_ctree_super(trans, root);
Chris Mason4313b392008-01-03 09:08:48 -0500782
Chris Mason4313b392008-01-03 09:08:48 -0500783 btrfs_finish_extent_commit(trans, root, pinned_copy);
Chris Mason78fae272007-03-25 11:35:08 -0400784 mutex_lock(&root->fs_info->trans_mutex);
Chris Mason4313b392008-01-03 09:08:48 -0500785
786 kfree(pinned_copy);
787
Chris Mason2c90e5d2007-04-02 10:50:19 -0400788 cur_trans->commit_done = 1;
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400789 root->fs_info->last_trans_committed = cur_trans->transid;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400790 wake_up(&cur_trans->commit_wait);
Chris Mason79154b12007-03-22 15:59:16 -0400791 put_transaction(cur_trans);
Chris Mason78fae272007-03-25 11:35:08 -0400792 put_transaction(cur_trans);
Josef Bacik58176a92007-08-29 15:47:34 -0400793
Chris Masonfacda1e2007-06-08 18:11:48 -0400794 if (root->fs_info->closing)
795 list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots);
796 else
797 list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots);
Josef Bacik58176a92007-08-29 15:47:34 -0400798
Chris Mason78fae272007-03-25 11:35:08 -0400799 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400800 kmem_cache_free(btrfs_trans_handle_cachep, trans);
Chris Mason79154b12007-03-22 15:59:16 -0400801
Chris Masonfacda1e2007-06-08 18:11:48 -0400802 if (root->fs_info->closing) {
Chris Masonfacda1e2007-06-08 18:11:48 -0400803 drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);
Chris Masonfacda1e2007-06-08 18:11:48 -0400804 }
Chris Mason79154b12007-03-22 15:59:16 -0400805 return ret;
806}
807
Chris Masone9d0b132007-08-10 14:06:19 -0400808int btrfs_clean_old_snapshots(struct btrfs_root *root)
809{
810 struct list_head dirty_roots;
811 INIT_LIST_HEAD(&dirty_roots);
Chris Masona74a4b92008-06-25 16:01:31 -0400812again:
Chris Masone9d0b132007-08-10 14:06:19 -0400813 mutex_lock(&root->fs_info->trans_mutex);
814 list_splice_init(&root->fs_info->dead_roots, &dirty_roots);
815 mutex_unlock(&root->fs_info->trans_mutex);
816
817 if (!list_empty(&dirty_roots)) {
818 drop_dirty_roots(root, &dirty_roots);
Chris Masona74a4b92008-06-25 16:01:31 -0400819 goto again;
Chris Masone9d0b132007-08-10 14:06:19 -0400820 }
821 return 0;
822}
Chris Mason08607c12007-06-08 15:33:54 -0400823