blob: 9d3d08e9f8d172e6ac061d7efa962ffdc38ef0d6 [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);
Yan Zheng31153d82008-07-28 15:32:19 -0400101 dirty->latest_root = root;
102 INIT_LIST_HEAD(&dirty->list);
Yan Zheng31153d82008-07-28 15:32:19 -0400103
Chris Mason925baed2008-06-25 16:01:30 -0400104 root->commit_root = btrfs_root_node(root);
Yan Zheng31153d82008-07-28 15:32:19 -0400105
106 memcpy(dirty->root, root, sizeof(*root));
107 spin_lock_init(&dirty->root->node_lock);
Yanbcc63ab2008-07-30 16:29:20 -0400108 spin_lock_init(&dirty->root->list_lock);
Yan Zheng31153d82008-07-28 15:32:19 -0400109 mutex_init(&dirty->root->objectid_mutex);
Yanbcc63ab2008-07-30 16:29:20 -0400110 INIT_LIST_HEAD(&dirty->root->dead_list);
Yan Zheng31153d82008-07-28 15:32:19 -0400111 dirty->root->node = root->commit_root;
112 dirty->root->commit_root = NULL;
Yanbcc63ab2008-07-30 16:29:20 -0400113
114 spin_lock(&root->list_lock);
115 list_add(&dirty->root->dead_list, &root->dead_list);
116 spin_unlock(&root->list_lock);
117
118 root->dirty_root = dirty;
Chris Mason6702ed42007-08-07 16:15:09 -0400119 } else {
120 WARN_ON(1);
121 }
122 root->last_trans = running_trans_id;
123 }
124 return 0;
125}
126
Chris Mason37d1aee2008-07-31 10:48:37 -0400127static void wait_current_trans(struct btrfs_root *root)
Chris Mason79154b12007-03-22 15:59:16 -0400128{
Chris Masonf9295742008-07-17 12:54:14 -0400129 struct btrfs_transaction *cur_trans;
Chris Mason79154b12007-03-22 15:59:16 -0400130
Chris Masonf9295742008-07-17 12:54:14 -0400131 cur_trans = root->fs_info->running_transaction;
Chris Mason37d1aee2008-07-31 10:48:37 -0400132 if (cur_trans && cur_trans->blocked) {
Chris Masonf9295742008-07-17 12:54:14 -0400133 DEFINE_WAIT(wait);
134 cur_trans->use_count++;
135 while(1) {
136 prepare_to_wait(&root->fs_info->transaction_wait, &wait,
137 TASK_UNINTERRUPTIBLE);
138 if (cur_trans->blocked) {
139 mutex_unlock(&root->fs_info->trans_mutex);
140 schedule();
141 mutex_lock(&root->fs_info->trans_mutex);
142 finish_wait(&root->fs_info->transaction_wait,
143 &wait);
144 } else {
145 finish_wait(&root->fs_info->transaction_wait,
146 &wait);
147 break;
148 }
149 }
150 put_transaction(cur_trans);
151 }
Chris Mason37d1aee2008-07-31 10:48:37 -0400152}
153
154struct btrfs_trans_handle *start_transaction(struct btrfs_root *root,
Sage Weil9ca9ee02008-08-04 10:41:27 -0400155 int num_blocks, int wait)
Chris Mason37d1aee2008-07-31 10:48:37 -0400156{
157 struct btrfs_trans_handle *h =
158 kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
159 int ret;
160
161 mutex_lock(&root->fs_info->trans_mutex);
Sage Weil9ca9ee02008-08-04 10:41:27 -0400162 if ((wait == 1 && !root->fs_info->open_ioctl_trans) || wait == 2)
Chris Mason37d1aee2008-07-31 10:48:37 -0400163 wait_current_trans(root);
Chris Mason79154b12007-03-22 15:59:16 -0400164 ret = join_transaction(root);
165 BUG_ON(ret);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400166
Chris Mason6702ed42007-08-07 16:15:09 -0400167 record_root_in_trans(root);
168 h->transid = root->fs_info->running_transaction->transid;
Chris Mason79154b12007-03-22 15:59:16 -0400169 h->transaction = root->fs_info->running_transaction;
170 h->blocks_reserved = num_blocks;
171 h->blocks_used = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400172 h->block_group = NULL;
Chris Mason26b80032007-08-08 20:17:12 -0400173 h->alloc_exclude_nr = 0;
174 h->alloc_exclude_start = 0;
Chris Mason79154b12007-03-22 15:59:16 -0400175 root->fs_info->running_transaction->use_count++;
176 mutex_unlock(&root->fs_info->trans_mutex);
177 return h;
178}
179
Chris Masonf9295742008-07-17 12:54:14 -0400180struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
181 int num_blocks)
182{
Sage Weil9ca9ee02008-08-04 10:41:27 -0400183 return start_transaction(root, num_blocks, 1);
Chris Masonf9295742008-07-17 12:54:14 -0400184}
185struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root,
186 int num_blocks)
187{
Sage Weil9ca9ee02008-08-04 10:41:27 -0400188 return start_transaction(root, num_blocks, 0);
Chris Masonf9295742008-07-17 12:54:14 -0400189}
190
Sage Weil9ca9ee02008-08-04 10:41:27 -0400191struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *r,
192 int num_blocks)
193{
194 return start_transaction(r, num_blocks, 2);
195}
196
197
Chris Mason89ce8a62008-06-25 16:01:31 -0400198static noinline int wait_for_commit(struct btrfs_root *root,
199 struct btrfs_transaction *commit)
200{
201 DEFINE_WAIT(wait);
202 mutex_lock(&root->fs_info->trans_mutex);
203 while(!commit->commit_done) {
204 prepare_to_wait(&commit->commit_wait, &wait,
205 TASK_UNINTERRUPTIBLE);
206 if (commit->commit_done)
207 break;
208 mutex_unlock(&root->fs_info->trans_mutex);
209 schedule();
210 mutex_lock(&root->fs_info->trans_mutex);
211 }
212 mutex_unlock(&root->fs_info->trans_mutex);
213 finish_wait(&commit->commit_wait, &wait);
214 return 0;
215}
216
Chris Mason37d1aee2008-07-31 10:48:37 -0400217static void throttle_on_drops(struct btrfs_root *root)
Chris Masonab78c842008-07-29 16:15:18 -0400218{
219 struct btrfs_fs_info *info = root->fs_info;
Chris Mason2dd3e672008-08-04 08:20:15 -0400220 int harder_count = 0;
Chris Masonab78c842008-07-29 16:15:18 -0400221
Chris Mason2dd3e672008-08-04 08:20:15 -0400222harder:
Chris Masonab78c842008-07-29 16:15:18 -0400223 if (atomic_read(&info->throttles)) {
224 DEFINE_WAIT(wait);
225 int thr;
Chris Masonab78c842008-07-29 16:15:18 -0400226 thr = atomic_read(&info->throttle_gen);
227
228 do {
229 prepare_to_wait(&info->transaction_throttle,
230 &wait, TASK_UNINTERRUPTIBLE);
231 if (!atomic_read(&info->throttles)) {
232 finish_wait(&info->transaction_throttle, &wait);
233 break;
234 }
235 schedule();
236 finish_wait(&info->transaction_throttle, &wait);
237 } while (thr == atomic_read(&info->throttle_gen));
Chris Mason2dd3e672008-08-04 08:20:15 -0400238 harder_count++;
239
240 if (root->fs_info->total_ref_cache_size > 1 * 1024 * 1024 &&
241 harder_count < 2)
242 goto harder;
243
244 if (root->fs_info->total_ref_cache_size > 5 * 1024 * 1024 &&
245 harder_count < 10)
246 goto harder;
247
248 if (root->fs_info->total_ref_cache_size > 10 * 1024 * 1024 &&
249 harder_count < 20)
250 goto harder;
Chris Masonab78c842008-07-29 16:15:18 -0400251 }
252}
253
Chris Mason37d1aee2008-07-31 10:48:37 -0400254void btrfs_throttle(struct btrfs_root *root)
255{
256 mutex_lock(&root->fs_info->trans_mutex);
Sage Weil9ca9ee02008-08-04 10:41:27 -0400257 if (!root->fs_info->open_ioctl_trans)
258 wait_current_trans(root);
Chris Mason37d1aee2008-07-31 10:48:37 -0400259 mutex_unlock(&root->fs_info->trans_mutex);
260
261 throttle_on_drops(root);
262}
263
Chris Mason89ce8a62008-06-25 16:01:31 -0400264static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
265 struct btrfs_root *root, int throttle)
Chris Mason79154b12007-03-22 15:59:16 -0400266{
267 struct btrfs_transaction *cur_trans;
Chris Masonab78c842008-07-29 16:15:18 -0400268 struct btrfs_fs_info *info = root->fs_info;
Chris Masond6e4a422007-04-06 15:37:36 -0400269
Chris Masonab78c842008-07-29 16:15:18 -0400270 mutex_lock(&info->trans_mutex);
271 cur_trans = info->running_transaction;
Chris Masonccd467d2007-06-28 15:57:36 -0400272 WARN_ON(cur_trans != trans->transaction);
Chris Masond5719762007-03-23 10:01:08 -0400273 WARN_ON(cur_trans->num_writers < 1);
Chris Masonccd467d2007-06-28 15:57:36 -0400274 cur_trans->num_writers--;
Chris Mason89ce8a62008-06-25 16:01:31 -0400275
Chris Mason79154b12007-03-22 15:59:16 -0400276 if (waitqueue_active(&cur_trans->writer_wait))
277 wake_up(&cur_trans->writer_wait);
Chris Mason79154b12007-03-22 15:59:16 -0400278 put_transaction(cur_trans);
Chris Masonab78c842008-07-29 16:15:18 -0400279 mutex_unlock(&info->trans_mutex);
Chris Masond6025572007-03-30 14:27:56 -0400280 memset(trans, 0, sizeof(*trans));
Chris Mason2c90e5d2007-04-02 10:50:19 -0400281 kmem_cache_free(btrfs_trans_handle_cachep, trans);
Chris Masonab78c842008-07-29 16:15:18 -0400282
283 if (throttle)
Chris Mason37d1aee2008-07-31 10:48:37 -0400284 throttle_on_drops(root);
Chris Masonab78c842008-07-29 16:15:18 -0400285
Chris Mason79154b12007-03-22 15:59:16 -0400286 return 0;
287}
288
Chris Mason89ce8a62008-06-25 16:01:31 -0400289int btrfs_end_transaction(struct btrfs_trans_handle *trans,
290 struct btrfs_root *root)
291{
292 return __btrfs_end_transaction(trans, root, 0);
293}
294
295int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
296 struct btrfs_root *root)
297{
298 return __btrfs_end_transaction(trans, root, 1);
299}
300
Chris Mason79154b12007-03-22 15:59:16 -0400301
302int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
303 struct btrfs_root *root)
304{
Chris Mason7c4452b2007-04-28 09:29:35 -0400305 int ret;
Chris Mason7c4452b2007-04-28 09:29:35 -0400306 int err;
307 int werr = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500308 struct extent_io_tree *dirty_pages;
Chris Mason7c4452b2007-04-28 09:29:35 -0400309 struct page *page;
Chris Mason7c4452b2007-04-28 09:29:35 -0400310 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Mason5f39d392007-10-15 16:14:19 -0400311 u64 start;
312 u64 end;
313 unsigned long index;
Chris Mason7c4452b2007-04-28 09:29:35 -0400314
315 if (!trans || !trans->transaction) {
316 return filemap_write_and_wait(btree_inode->i_mapping);
317 }
318 dirty_pages = &trans->transaction->dirty_pages;
319 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400320 ret = find_first_extent_bit(dirty_pages, 0, &start, &end,
321 EXTENT_DIRTY);
322 if (ret)
Chris Mason7c4452b2007-04-28 09:29:35 -0400323 break;
Chris Mason5f39d392007-10-15 16:14:19 -0400324 clear_extent_dirty(dirty_pages, start, end, GFP_NOFS);
325 while(start <= end) {
326 index = start >> PAGE_CACHE_SHIFT;
Chris Mason35ebb932007-10-30 16:56:53 -0400327 start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
Chris Mason5f39d392007-10-15 16:14:19 -0400328 page = find_lock_page(btree_inode->i_mapping, index);
Chris Mason7c4452b2007-04-28 09:29:35 -0400329 if (!page)
330 continue;
Chris Mason6702ed42007-08-07 16:15:09 -0400331 if (PageWriteback(page)) {
332 if (PageDirty(page))
333 wait_on_page_writeback(page);
334 else {
335 unlock_page(page);
336 page_cache_release(page);
337 continue;
338 }
339 }
Chris Mason7c4452b2007-04-28 09:29:35 -0400340 err = write_one_page(page, 0);
341 if (err)
342 werr = err;
343 page_cache_release(page);
344 }
345 }
346 err = filemap_fdatawait(btree_inode->i_mapping);
347 if (err)
348 werr = err;
349 return werr;
Chris Mason79154b12007-03-22 15:59:16 -0400350}
351
Chris Mason0b86a832008-03-24 15:01:56 -0400352static int update_cowonly_root(struct btrfs_trans_handle *trans,
353 struct btrfs_root *root)
354{
355 int ret;
356 u64 old_root_bytenr;
357 struct btrfs_root *tree_root = root->fs_info->tree_root;
358
359 btrfs_write_dirty_block_groups(trans, root);
360 while(1) {
361 old_root_bytenr = btrfs_root_bytenr(&root->root_item);
362 if (old_root_bytenr == root->node->start)
363 break;
364 btrfs_set_root_bytenr(&root->root_item,
365 root->node->start);
366 btrfs_set_root_level(&root->root_item,
367 btrfs_header_level(root->node));
368 ret = btrfs_update_root(trans, tree_root,
369 &root->root_key,
370 &root->root_item);
371 BUG_ON(ret);
372 btrfs_write_dirty_block_groups(trans, root);
373 }
374 return 0;
375}
376
Chris Mason79154b12007-03-22 15:59:16 -0400377int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans,
378 struct btrfs_root *root)
379{
Chris Mason79154b12007-03-22 15:59:16 -0400380 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason0b86a832008-03-24 15:01:56 -0400381 struct list_head *next;
Chris Mason79154b12007-03-22 15:59:16 -0400382
Chris Mason0b86a832008-03-24 15:01:56 -0400383 while(!list_empty(&fs_info->dirty_cowonly_roots)) {
384 next = fs_info->dirty_cowonly_roots.next;
385 list_del_init(next);
386 root = list_entry(next, struct btrfs_root, dirty_list);
387 update_cowonly_root(trans, root);
Chris Mason79154b12007-03-22 15:59:16 -0400388 }
389 return 0;
390}
391
Yan Zhengb48652c2008-08-04 23:23:47 -0400392int btrfs_add_dead_root(struct btrfs_root *root, struct btrfs_root *latest)
Chris Mason5eda7b52007-06-22 14:16:25 -0400393{
Yan Zhengf321e492008-07-30 09:26:11 -0400394 struct btrfs_dirty_root *dirty;
Chris Mason5eda7b52007-06-22 14:16:25 -0400395
396 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
397 if (!dirty)
398 return -ENOMEM;
Chris Mason5eda7b52007-06-22 14:16:25 -0400399 dirty->root = root;
Chris Mason5ce14bb2007-09-11 11:15:39 -0400400 dirty->latest_root = latest;
Yan Zhengb48652c2008-08-04 23:23:47 -0400401
402 mutex_lock(&root->fs_info->trans_mutex);
403 list_add(&dirty->list, &latest->fs_info->dead_roots);
404 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason5eda7b52007-06-22 14:16:25 -0400405 return 0;
406}
407
Chris Mason80b67942008-02-01 16:35:04 -0500408static noinline int add_dirty_roots(struct btrfs_trans_handle *trans,
409 struct radix_tree_root *radix,
410 struct list_head *list)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400411{
Yan Zhengf321e492008-07-30 09:26:11 -0400412 struct btrfs_dirty_root *dirty;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400413 struct btrfs_root *gang[8];
414 struct btrfs_root *root;
415 int i;
416 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400417 int err = 0;
Chris Mason5eda7b52007-06-22 14:16:25 -0400418 u32 refs;
Chris Mason54aa1f42007-06-22 14:16:25 -0400419
Chris Mason0f7d52f2007-04-09 10:42:37 -0400420 while(1) {
421 ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0,
422 ARRAY_SIZE(gang),
423 BTRFS_ROOT_TRANS_TAG);
424 if (ret == 0)
425 break;
426 for (i = 0; i < ret; i++) {
427 root = gang[i];
Chris Mason2619ba12007-04-10 16:58:11 -0400428 radix_tree_tag_clear(radix,
429 (unsigned long)root->root_key.objectid,
430 BTRFS_ROOT_TRANS_TAG);
Yan Zheng31153d82008-07-28 15:32:19 -0400431
432 BUG_ON(!root->ref_tree);
Chris Mason017e5362008-07-28 15:32:51 -0400433 dirty = root->dirty_root;
Yan Zheng31153d82008-07-28 15:32:19 -0400434
Chris Mason0f7d52f2007-04-09 10:42:37 -0400435 if (root->commit_root == root->node) {
Chris Masondb945352007-10-15 16:15:53 -0400436 WARN_ON(root->node->start !=
437 btrfs_root_bytenr(&root->root_item));
Yan Zheng31153d82008-07-28 15:32:19 -0400438
Chris Mason5f39d392007-10-15 16:14:19 -0400439 free_extent_buffer(root->commit_root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400440 root->commit_root = NULL;
Yan Zheng7ea394f2008-08-05 13:05:02 -0400441 root->dirty_root = NULL;
Yanbcc63ab2008-07-30 16:29:20 -0400442
443 spin_lock(&root->list_lock);
444 list_del_init(&dirty->root->dead_list);
445 spin_unlock(&root->list_lock);
446
Yan Zheng31153d82008-07-28 15:32:19 -0400447 kfree(dirty->root);
448 kfree(dirty);
Josef Bacik58176a92007-08-29 15:47:34 -0400449
450 /* make sure to update the root on disk
451 * so we get any updates to the block used
452 * counts
453 */
454 err = btrfs_update_root(trans,
455 root->fs_info->tree_root,
456 &root->root_key,
457 &root->root_item);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400458 continue;
459 }
Chris Mason9f3a7422007-08-07 15:52:19 -0400460
461 memset(&root->root_item.drop_progress, 0,
462 sizeof(struct btrfs_disk_key));
463 root->root_item.drop_level = 0;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400464 root->commit_root = NULL;
Yan Zheng7ea394f2008-08-05 13:05:02 -0400465 root->dirty_root = NULL;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400466 root->root_key.offset = root->fs_info->generation;
Chris Masondb945352007-10-15 16:15:53 -0400467 btrfs_set_root_bytenr(&root->root_item,
468 root->node->start);
469 btrfs_set_root_level(&root->root_item,
470 btrfs_header_level(root->node));
Chris Mason0f7d52f2007-04-09 10:42:37 -0400471 err = btrfs_insert_root(trans, root->fs_info->tree_root,
472 &root->root_key,
473 &root->root_item);
Chris Mason54aa1f42007-06-22 14:16:25 -0400474 if (err)
475 break;
Chris Mason9f3a7422007-08-07 15:52:19 -0400476
477 refs = btrfs_root_refs(&dirty->root->root_item);
478 btrfs_set_root_refs(&dirty->root->root_item, refs - 1);
Chris Mason5eda7b52007-06-22 14:16:25 -0400479 err = btrfs_update_root(trans, root->fs_info->tree_root,
Chris Mason9f3a7422007-08-07 15:52:19 -0400480 &dirty->root->root_key,
481 &dirty->root->root_item);
Chris Mason5eda7b52007-06-22 14:16:25 -0400482
483 BUG_ON(err);
Chris Mason9f3a7422007-08-07 15:52:19 -0400484 if (refs == 1) {
Chris Mason5eda7b52007-06-22 14:16:25 -0400485 list_add(&dirty->list, list);
Chris Mason9f3a7422007-08-07 15:52:19 -0400486 } else {
487 WARN_ON(1);
Yan Zheng31153d82008-07-28 15:32:19 -0400488 free_extent_buffer(dirty->root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -0400489 kfree(dirty->root);
Chris Mason5eda7b52007-06-22 14:16:25 -0400490 kfree(dirty);
Chris Mason9f3a7422007-08-07 15:52:19 -0400491 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400492 }
493 }
Chris Mason54aa1f42007-06-22 14:16:25 -0400494 return err;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400495}
496
Chris Masone9d0b132007-08-10 14:06:19 -0400497int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
498{
499 struct btrfs_fs_info *info = root->fs_info;
500 int ret;
501 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400502 unsigned long nr;
Chris Masone9d0b132007-08-10 14:06:19 -0400503
Chris Masona2135012008-06-25 16:01:30 -0400504 smp_mb();
Chris Masone9d0b132007-08-10 14:06:19 -0400505 if (root->defrag_running)
506 return 0;
Chris Masone9d0b132007-08-10 14:06:19 -0400507 trans = btrfs_start_transaction(root, 1);
Chris Mason6b800532007-10-15 16:17:34 -0400508 while (1) {
Chris Masone9d0b132007-08-10 14:06:19 -0400509 root->defrag_running = 1;
510 ret = btrfs_defrag_leaves(trans, root, cacheonly);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400511 nr = trans->blocks_used;
Chris Masone9d0b132007-08-10 14:06:19 -0400512 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400513 btrfs_btree_balance_dirty(info->tree_root, nr);
Chris Masone9d0b132007-08-10 14:06:19 -0400514 cond_resched();
515
Chris Masone9d0b132007-08-10 14:06:19 -0400516 trans = btrfs_start_transaction(root, 1);
Chris Mason3f157a22008-06-25 16:01:31 -0400517 if (root->fs_info->closing || ret != -EAGAIN)
Chris Masone9d0b132007-08-10 14:06:19 -0400518 break;
519 }
520 root->defrag_running = 0;
Chris Masona2135012008-06-25 16:01:30 -0400521 smp_mb();
Chris Masone9d0b132007-08-10 14:06:19 -0400522 btrfs_end_transaction(trans, root);
523 return 0;
524}
525
Chris Mason80b67942008-02-01 16:35:04 -0500526static noinline int drop_dirty_roots(struct btrfs_root *tree_root,
527 struct list_head *list)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400528{
Yan Zhengf321e492008-07-30 09:26:11 -0400529 struct btrfs_dirty_root *dirty;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400530 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400531 unsigned long nr;
Chris Masondb945352007-10-15 16:15:53 -0400532 u64 num_bytes;
533 u64 bytes_used;
Yanbcc63ab2008-07-30 16:29:20 -0400534 u64 max_useless;
Chris Mason54aa1f42007-06-22 14:16:25 -0400535 int ret = 0;
Chris Mason9f3a7422007-08-07 15:52:19 -0400536 int err;
537
Chris Mason0f7d52f2007-04-09 10:42:37 -0400538 while(!list_empty(list)) {
Josef Bacik58176a92007-08-29 15:47:34 -0400539 struct btrfs_root *root;
540
Yan Zhengf321e492008-07-30 09:26:11 -0400541 dirty = list_entry(list->prev, struct btrfs_dirty_root, list);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400542 list_del_init(&dirty->list);
Chris Mason5eda7b52007-06-22 14:16:25 -0400543
Chris Masondb945352007-10-15 16:15:53 -0400544 num_bytes = btrfs_root_used(&dirty->root->root_item);
Josef Bacik58176a92007-08-29 15:47:34 -0400545 root = dirty->latest_root;
Chris Masona2135012008-06-25 16:01:30 -0400546 atomic_inc(&root->fs_info->throttles);
Josef Bacik58176a92007-08-29 15:47:34 -0400547
Chris Masona2135012008-06-25 16:01:30 -0400548 mutex_lock(&root->fs_info->drop_mutex);
Chris Mason9f3a7422007-08-07 15:52:19 -0400549 while(1) {
550 trans = btrfs_start_transaction(tree_root, 1);
551 ret = btrfs_drop_snapshot(trans, dirty->root);
552 if (ret != -EAGAIN) {
553 break;
554 }
Josef Bacik58176a92007-08-29 15:47:34 -0400555
Chris Mason9f3a7422007-08-07 15:52:19 -0400556 err = btrfs_update_root(trans,
557 tree_root,
558 &dirty->root->root_key,
559 &dirty->root->root_item);
560 if (err)
561 ret = err;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400562 nr = trans->blocks_used;
Chris Mason017e5362008-07-28 15:32:51 -0400563 ret = btrfs_end_transaction(trans, tree_root);
Chris Mason9f3a7422007-08-07 15:52:19 -0400564 BUG_ON(ret);
Chris Masona2135012008-06-25 16:01:30 -0400565
566 mutex_unlock(&root->fs_info->drop_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400567 btrfs_btree_balance_dirty(tree_root, nr);
Chris Mason4dc119042007-10-15 16:18:14 -0400568 cond_resched();
Chris Masona2135012008-06-25 16:01:30 -0400569 mutex_lock(&root->fs_info->drop_mutex);
Chris Mason9f3a7422007-08-07 15:52:19 -0400570 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400571 BUG_ON(ret);
Chris Masona2135012008-06-25 16:01:30 -0400572 atomic_dec(&root->fs_info->throttles);
Chris Mason017e5362008-07-28 15:32:51 -0400573 wake_up(&root->fs_info->transaction_throttle);
Josef Bacik58176a92007-08-29 15:47:34 -0400574
Chris Masona2135012008-06-25 16:01:30 -0400575 mutex_lock(&root->fs_info->alloc_mutex);
Chris Masondb945352007-10-15 16:15:53 -0400576 num_bytes -= btrfs_root_used(&dirty->root->root_item);
577 bytes_used = btrfs_root_used(&root->root_item);
578 if (num_bytes) {
Josef Bacik58176a92007-08-29 15:47:34 -0400579 record_root_in_trans(root);
Chris Mason5f39d392007-10-15 16:14:19 -0400580 btrfs_set_root_used(&root->root_item,
Chris Masondb945352007-10-15 16:15:53 -0400581 bytes_used - num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -0400582 }
Chris Masona2135012008-06-25 16:01:30 -0400583 mutex_unlock(&root->fs_info->alloc_mutex);
584
Chris Mason9f3a7422007-08-07 15:52:19 -0400585 ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key);
Josef Bacik58176a92007-08-29 15:47:34 -0400586 if (ret) {
587 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -0400588 break;
Josef Bacik58176a92007-08-29 15:47:34 -0400589 }
Chris Masona2135012008-06-25 16:01:30 -0400590 mutex_unlock(&root->fs_info->drop_mutex);
591
Yanbcc63ab2008-07-30 16:29:20 -0400592 spin_lock(&root->list_lock);
593 list_del_init(&dirty->root->dead_list);
594 if (!list_empty(&root->dead_list)) {
595 struct btrfs_root *oldest;
596 oldest = list_entry(root->dead_list.prev,
597 struct btrfs_root, dead_list);
598 max_useless = oldest->root_key.offset - 1;
599 } else {
600 max_useless = root->root_key.offset - 1;
601 }
602 spin_unlock(&root->list_lock);
603
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400604 nr = trans->blocks_used;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400605 ret = btrfs_end_transaction(trans, tree_root);
606 BUG_ON(ret);
Chris Mason5eda7b52007-06-22 14:16:25 -0400607
Yanbcc63ab2008-07-30 16:29:20 -0400608 ret = btrfs_remove_leaf_refs(root, max_useless);
609 BUG_ON(ret);
610
Chris Masonf510cfe2007-10-15 16:14:48 -0400611 free_extent_buffer(dirty->root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -0400612 kfree(dirty->root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400613 kfree(dirty);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400614
615 btrfs_btree_balance_dirty(tree_root, nr);
Chris Mason4dc119042007-10-15 16:18:14 -0400616 cond_resched();
Chris Mason0f7d52f2007-04-09 10:42:37 -0400617 }
Chris Mason54aa1f42007-06-22 14:16:25 -0400618 return ret;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400619}
620
Chris Mason80b67942008-02-01 16:35:04 -0500621static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
Chris Mason3063d292008-01-08 15:46:30 -0500622 struct btrfs_fs_info *fs_info,
623 struct btrfs_pending_snapshot *pending)
624{
625 struct btrfs_key key;
Chris Mason80b67942008-02-01 16:35:04 -0500626 struct btrfs_root_item *new_root_item;
Chris Mason3063d292008-01-08 15:46:30 -0500627 struct btrfs_root *tree_root = fs_info->tree_root;
628 struct btrfs_root *root = pending->root;
629 struct extent_buffer *tmp;
Chris Mason925baed2008-06-25 16:01:30 -0400630 struct extent_buffer *old;
Chris Mason3063d292008-01-08 15:46:30 -0500631 int ret;
Sven Wegener3b963622008-06-09 21:57:42 -0400632 int namelen;
Chris Mason3063d292008-01-08 15:46:30 -0500633 u64 objectid;
634
Chris Mason80b67942008-02-01 16:35:04 -0500635 new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
636 if (!new_root_item) {
637 ret = -ENOMEM;
638 goto fail;
639 }
Chris Mason3063d292008-01-08 15:46:30 -0500640 ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid);
641 if (ret)
642 goto fail;
643
Chris Mason80b67942008-02-01 16:35:04 -0500644 memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
Chris Mason3063d292008-01-08 15:46:30 -0500645
646 key.objectid = objectid;
647 key.offset = 1;
648 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
649
Chris Mason925baed2008-06-25 16:01:30 -0400650 old = btrfs_lock_root_node(root);
Chris Mason65b51a02008-08-01 15:11:20 -0400651 btrfs_cow_block(trans, root, old, NULL, 0, &old, 0);
Chris Mason3063d292008-01-08 15:46:30 -0500652
Chris Mason925baed2008-06-25 16:01:30 -0400653 btrfs_copy_root(trans, root, old, &tmp, objectid);
654 btrfs_tree_unlock(old);
655 free_extent_buffer(old);
Chris Mason3063d292008-01-08 15:46:30 -0500656
Chris Mason80b67942008-02-01 16:35:04 -0500657 btrfs_set_root_bytenr(new_root_item, tmp->start);
658 btrfs_set_root_level(new_root_item, btrfs_header_level(tmp));
Chris Mason3063d292008-01-08 15:46:30 -0500659 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
Chris Mason80b67942008-02-01 16:35:04 -0500660 new_root_item);
Chris Mason925baed2008-06-25 16:01:30 -0400661 btrfs_tree_unlock(tmp);
Chris Mason3063d292008-01-08 15:46:30 -0500662 free_extent_buffer(tmp);
663 if (ret)
664 goto fail;
665
666 /*
667 * insert the directory item
668 */
669 key.offset = (u64)-1;
Sven Wegener3b963622008-06-09 21:57:42 -0400670 namelen = strlen(pending->name);
Chris Mason3063d292008-01-08 15:46:30 -0500671 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
Sven Wegener3b963622008-06-09 21:57:42 -0400672 pending->name, namelen,
Chris Mason3063d292008-01-08 15:46:30 -0500673 root->fs_info->sb->s_root->d_inode->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -0400674 &key, BTRFS_FT_DIR, 0);
Chris Mason3063d292008-01-08 15:46:30 -0500675
676 if (ret)
677 goto fail;
678
679 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
680 pending->name, strlen(pending->name), objectid,
Josef Bacikaec74772008-07-24 12:12:38 -0400681 root->fs_info->sb->s_root->d_inode->i_ino, 0);
Sven Wegener3b963622008-06-09 21:57:42 -0400682
683 /* Invalidate existing dcache entry for new snapshot. */
684 btrfs_invalidate_dcache_root(root, pending->name, namelen);
685
Chris Mason3063d292008-01-08 15:46:30 -0500686fail:
Chris Mason80b67942008-02-01 16:35:04 -0500687 kfree(new_root_item);
Chris Mason3063d292008-01-08 15:46:30 -0500688 return ret;
689}
690
Chris Mason80b67942008-02-01 16:35:04 -0500691static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
692 struct btrfs_fs_info *fs_info)
Chris Mason3063d292008-01-08 15:46:30 -0500693{
694 struct btrfs_pending_snapshot *pending;
695 struct list_head *head = &trans->transaction->pending_snapshots;
696 int ret;
697
698 while(!list_empty(head)) {
699 pending = list_entry(head->next,
700 struct btrfs_pending_snapshot, list);
701 ret = create_pending_snapshot(trans, fs_info, pending);
702 BUG_ON(ret);
703 list_del(&pending->list);
704 kfree(pending->name);
705 kfree(pending);
706 }
Chris Masondc17ff82008-01-08 15:46:30 -0500707 return 0;
708}
709
Chris Mason79154b12007-03-22 15:59:16 -0400710int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
711 struct btrfs_root *root)
712{
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400713 unsigned long joined = 0;
714 unsigned long timeout = 1;
Chris Mason79154b12007-03-22 15:59:16 -0400715 struct btrfs_transaction *cur_trans;
Chris Mason8fd17792007-04-19 21:01:03 -0400716 struct btrfs_transaction *prev_trans = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -0400717 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400718 struct list_head dirty_fs_roots;
Chris Masond1310b22008-01-24 16:13:08 -0500719 struct extent_io_tree *pinned_copy;
Chris Mason79154b12007-03-22 15:59:16 -0400720 DEFINE_WAIT(wait);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400721 int ret;
Chris Mason79154b12007-03-22 15:59:16 -0400722
Chris Mason0f7d52f2007-04-09 10:42:37 -0400723 INIT_LIST_HEAD(&dirty_fs_roots);
Chris Masond6e4a422007-04-06 15:37:36 -0400724
Chris Mason79154b12007-03-22 15:59:16 -0400725 mutex_lock(&root->fs_info->trans_mutex);
726 if (trans->transaction->in_commit) {
727 cur_trans = trans->transaction;
728 trans->transaction->use_count++;
Chris Masonccd467d2007-06-28 15:57:36 -0400729 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400730 btrfs_end_transaction(trans, root);
Chris Masonccd467d2007-06-28 15:57:36 -0400731
Chris Mason79154b12007-03-22 15:59:16 -0400732 ret = wait_for_commit(root, cur_trans);
733 BUG_ON(ret);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400734
735 mutex_lock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400736 put_transaction(cur_trans);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400737 mutex_unlock(&root->fs_info->trans_mutex);
738
Chris Mason79154b12007-03-22 15:59:16 -0400739 return 0;
740 }
Chris Mason4313b392008-01-03 09:08:48 -0500741
742 pinned_copy = kmalloc(sizeof(*pinned_copy), GFP_NOFS);
743 if (!pinned_copy)
744 return -ENOMEM;
745
Chris Masond1310b22008-01-24 16:13:08 -0500746 extent_io_tree_init(pinned_copy,
Chris Mason4313b392008-01-03 09:08:48 -0500747 root->fs_info->btree_inode->i_mapping, GFP_NOFS);
748
Chris Mason2c90e5d2007-04-02 10:50:19 -0400749 trans->transaction->in_commit = 1;
Chris Masonf9295742008-07-17 12:54:14 -0400750 trans->transaction->blocked = 1;
Chris Masonccd467d2007-06-28 15:57:36 -0400751 cur_trans = trans->transaction;
752 if (cur_trans->list.prev != &root->fs_info->trans_list) {
753 prev_trans = list_entry(cur_trans->list.prev,
754 struct btrfs_transaction, list);
755 if (!prev_trans->commit_done) {
756 prev_trans->use_count++;
Chris Masonccd467d2007-06-28 15:57:36 -0400757 mutex_unlock(&root->fs_info->trans_mutex);
758
759 wait_for_commit(root, prev_trans);
Chris Masonccd467d2007-06-28 15:57:36 -0400760
Chris Masonccd467d2007-06-28 15:57:36 -0400761 mutex_lock(&root->fs_info->trans_mutex);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400762 put_transaction(prev_trans);
Chris Masonccd467d2007-06-28 15:57:36 -0400763 }
764 }
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400765
766 do {
Yan Zheng7ea394f2008-08-05 13:05:02 -0400767 int snap_pending = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400768 joined = cur_trans->num_joined;
Yan Zheng7ea394f2008-08-05 13:05:02 -0400769 if (!list_empty(&trans->transaction->pending_snapshots))
770 snap_pending = 1;
771
Chris Mason2c90e5d2007-04-02 10:50:19 -0400772 WARN_ON(cur_trans != trans->transaction);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400773 prepare_to_wait(&cur_trans->writer_wait, &wait,
Chris Mason79154b12007-03-22 15:59:16 -0400774 TASK_UNINTERRUPTIBLE);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400775
776 if (cur_trans->num_writers > 1)
777 timeout = MAX_SCHEDULE_TIMEOUT;
778 else
779 timeout = 1;
780
Chris Mason79154b12007-03-22 15:59:16 -0400781 mutex_unlock(&root->fs_info->trans_mutex);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400782
Yan Zheng7ea394f2008-08-05 13:05:02 -0400783 if (snap_pending) {
784 ret = btrfs_wait_ordered_extents(root, 1);
785 BUG_ON(ret);
786 }
787
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400788 schedule_timeout(timeout);
789
Chris Mason79154b12007-03-22 15:59:16 -0400790 mutex_lock(&root->fs_info->trans_mutex);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400791 finish_wait(&cur_trans->writer_wait, &wait);
792 } while (cur_trans->num_writers > 1 ||
793 (cur_trans->num_joined != joined));
794
Chris Mason3063d292008-01-08 15:46:30 -0500795 ret = create_pending_snapshots(trans, root->fs_info);
796 BUG_ON(ret);
797
Chris Mason2c90e5d2007-04-02 10:50:19 -0400798 WARN_ON(cur_trans != trans->transaction);
Chris Masondc17ff82008-01-08 15:46:30 -0500799
Chris Mason54aa1f42007-06-22 14:16:25 -0400800 ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix,
801 &dirty_fs_roots);
802 BUG_ON(ret);
803
Chris Mason79154b12007-03-22 15:59:16 -0400804 ret = btrfs_commit_tree_roots(trans, root);
805 BUG_ON(ret);
Chris Mason54aa1f42007-06-22 14:16:25 -0400806
Chris Mason78fae272007-03-25 11:35:08 -0400807 cur_trans = root->fs_info->running_transaction;
Chris Masoncee36a02008-01-15 08:40:48 -0500808 spin_lock(&root->fs_info->new_trans_lock);
Chris Mason78fae272007-03-25 11:35:08 -0400809 root->fs_info->running_transaction = NULL;
Chris Masoncee36a02008-01-15 08:40:48 -0500810 spin_unlock(&root->fs_info->new_trans_lock);
Chris Mason4b52dff2007-06-26 10:06:50 -0400811 btrfs_set_super_generation(&root->fs_info->super_copy,
812 cur_trans->transid);
813 btrfs_set_super_root(&root->fs_info->super_copy,
Chris Masondb945352007-10-15 16:15:53 -0400814 root->fs_info->tree_root->node->start);
815 btrfs_set_super_root_level(&root->fs_info->super_copy,
816 btrfs_header_level(root->fs_info->tree_root->node));
Chris Mason5f39d392007-10-15 16:14:19 -0400817
Chris Mason0b86a832008-03-24 15:01:56 -0400818 btrfs_set_super_chunk_root(&root->fs_info->super_copy,
819 chunk_root->node->start);
820 btrfs_set_super_chunk_root_level(&root->fs_info->super_copy,
821 btrfs_header_level(chunk_root->node));
Chris Masona061fc82008-05-07 11:43:44 -0400822 memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy,
823 sizeof(root->fs_info->super_copy));
Chris Masonccd467d2007-06-28 15:57:36 -0400824
Chris Mason4313b392008-01-03 09:08:48 -0500825 btrfs_copy_pinned(root, pinned_copy);
Chris Masonccd467d2007-06-28 15:57:36 -0400826
Chris Masonf9295742008-07-17 12:54:14 -0400827 trans->transaction->blocked = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400828 wake_up(&root->fs_info->transaction_throttle);
Chris Masonf9295742008-07-17 12:54:14 -0400829 wake_up(&root->fs_info->transaction_wait);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400830
Chris Mason78fae272007-03-25 11:35:08 -0400831 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400832 ret = btrfs_write_and_wait_transaction(trans, root);
833 BUG_ON(ret);
Chris Mason79154b12007-03-22 15:59:16 -0400834 write_ctree_super(trans, root);
Chris Mason4313b392008-01-03 09:08:48 -0500835
Chris Mason4313b392008-01-03 09:08:48 -0500836 btrfs_finish_extent_commit(trans, root, pinned_copy);
Chris Mason78fae272007-03-25 11:35:08 -0400837 mutex_lock(&root->fs_info->trans_mutex);
Chris Mason4313b392008-01-03 09:08:48 -0500838
839 kfree(pinned_copy);
840
Chris Mason2c90e5d2007-04-02 10:50:19 -0400841 cur_trans->commit_done = 1;
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400842 root->fs_info->last_trans_committed = cur_trans->transid;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400843 wake_up(&cur_trans->commit_wait);
Chris Mason79154b12007-03-22 15:59:16 -0400844 put_transaction(cur_trans);
Chris Mason78fae272007-03-25 11:35:08 -0400845 put_transaction(cur_trans);
Josef Bacik58176a92007-08-29 15:47:34 -0400846
Yanbcc63ab2008-07-30 16:29:20 -0400847 list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots);
Chris Masonfacda1e2007-06-08 18:11:48 -0400848 if (root->fs_info->closing)
849 list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots);
Josef Bacik58176a92007-08-29 15:47:34 -0400850
Chris Mason78fae272007-03-25 11:35:08 -0400851 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400852 kmem_cache_free(btrfs_trans_handle_cachep, trans);
Chris Mason79154b12007-03-22 15:59:16 -0400853
Chris Masonfacda1e2007-06-08 18:11:48 -0400854 if (root->fs_info->closing) {
Chris Masonfacda1e2007-06-08 18:11:48 -0400855 drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);
Chris Masonfacda1e2007-06-08 18:11:48 -0400856 }
Chris Mason79154b12007-03-22 15:59:16 -0400857 return ret;
858}
859
Chris Masone9d0b132007-08-10 14:06:19 -0400860int btrfs_clean_old_snapshots(struct btrfs_root *root)
861{
862 struct list_head dirty_roots;
863 INIT_LIST_HEAD(&dirty_roots);
Chris Masona74a4b92008-06-25 16:01:31 -0400864again:
Chris Masone9d0b132007-08-10 14:06:19 -0400865 mutex_lock(&root->fs_info->trans_mutex);
866 list_splice_init(&root->fs_info->dead_roots, &dirty_roots);
867 mutex_unlock(&root->fs_info->trans_mutex);
868
869 if (!list_empty(&dirty_roots)) {
870 drop_dirty_roots(root, &dirty_roots);
Chris Masona74a4b92008-06-25 16:01:31 -0400871 goto again;
Chris Masone9d0b132007-08-10 14:06:19 -0400872 }
873 return 0;
874}