blob: 8c83cf464c83b480e663f8d9b65df76f03b95f6a [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 Masone02119d2008-09-05 16:13:11 -040028#include "tree-log.h"
Chris Mason79154b12007-03-22 15:59:16 -040029
Chris Mason78fae272007-03-25 11:35:08 -040030static int total_trans = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -040031extern struct kmem_cache *btrfs_trans_handle_cachep;
32extern struct kmem_cache *btrfs_transaction_cachep;
33
Chris Mason0f7d52f2007-04-09 10:42:37 -040034#define BTRFS_ROOT_TRANS_TAG 0
35
Chris Mason80b67942008-02-01 16:35:04 -050036static noinline void put_transaction(struct btrfs_transaction *transaction)
Chris Mason79154b12007-03-22 15:59:16 -040037{
Chris Mason2c90e5d2007-04-02 10:50:19 -040038 WARN_ON(transaction->use_count == 0);
Chris Mason79154b12007-03-22 15:59:16 -040039 transaction->use_count--;
Chris Mason78fae272007-03-25 11:35:08 -040040 if (transaction->use_count == 0) {
41 WARN_ON(total_trans == 0);
42 total_trans--;
Chris Mason8fd17792007-04-19 21:01:03 -040043 list_del_init(&transaction->list);
Chris Mason2c90e5d2007-04-02 10:50:19 -040044 memset(transaction, 0, sizeof(*transaction));
45 kmem_cache_free(btrfs_transaction_cachep, transaction);
Chris Mason78fae272007-03-25 11:35:08 -040046 }
Chris Mason79154b12007-03-22 15:59:16 -040047}
48
Chris Mason80b67942008-02-01 16:35:04 -050049static noinline int join_transaction(struct btrfs_root *root)
Chris Mason79154b12007-03-22 15:59:16 -040050{
51 struct btrfs_transaction *cur_trans;
52 cur_trans = root->fs_info->running_transaction;
53 if (!cur_trans) {
Chris Mason2c90e5d2007-04-02 10:50:19 -040054 cur_trans = kmem_cache_alloc(btrfs_transaction_cachep,
55 GFP_NOFS);
Chris Mason78fae272007-03-25 11:35:08 -040056 total_trans++;
Chris Mason79154b12007-03-22 15:59:16 -040057 BUG_ON(!cur_trans);
Chris Mason0f7d52f2007-04-09 10:42:37 -040058 root->fs_info->generation++;
Chris Masone18e4802008-01-18 10:54:22 -050059 root->fs_info->last_alloc = 0;
Chris Mason4529ba42008-01-31 16:45:07 -050060 root->fs_info->last_data_alloc = 0;
Chris Masone02119d2008-09-05 16:13:11 -040061 root->fs_info->last_log_alloc = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -040062 cur_trans->num_writers = 1;
63 cur_trans->num_joined = 0;
Chris Mason0f7d52f2007-04-09 10:42:37 -040064 cur_trans->transid = root->fs_info->generation;
Chris Mason79154b12007-03-22 15:59:16 -040065 init_waitqueue_head(&cur_trans->writer_wait);
66 init_waitqueue_head(&cur_trans->commit_wait);
67 cur_trans->in_commit = 0;
Chris Masonf9295742008-07-17 12:54:14 -040068 cur_trans->blocked = 0;
Chris Masond5719762007-03-23 10:01:08 -040069 cur_trans->use_count = 1;
Chris Mason79154b12007-03-22 15:59:16 -040070 cur_trans->commit_done = 0;
Chris Mason08607c12007-06-08 15:33:54 -040071 cur_trans->start_time = get_seconds();
Chris Mason3063d292008-01-08 15:46:30 -050072 INIT_LIST_HEAD(&cur_trans->pending_snapshots);
Chris Mason8fd17792007-04-19 21:01:03 -040073 list_add_tail(&cur_trans->list, &root->fs_info->trans_list);
Chris Masond1310b22008-01-24 16:13:08 -050074 extent_io_tree_init(&cur_trans->dirty_pages,
Chris Mason5f39d392007-10-15 16:14:19 -040075 root->fs_info->btree_inode->i_mapping,
76 GFP_NOFS);
Chris Mason48ec2cf2008-06-09 09:35:50 -040077 spin_lock(&root->fs_info->new_trans_lock);
78 root->fs_info->running_transaction = cur_trans;
79 spin_unlock(&root->fs_info->new_trans_lock);
Josef Bacik15ee9bc2007-08-10 16:22:09 -040080 } else {
81 cur_trans->num_writers++;
82 cur_trans->num_joined++;
Chris Mason79154b12007-03-22 15:59:16 -040083 }
Josef Bacik15ee9bc2007-08-10 16:22:09 -040084
Chris Mason79154b12007-03-22 15:59:16 -040085 return 0;
86}
87
Chris Masone02119d2008-09-05 16:13:11 -040088noinline int btrfs_record_root_in_trans(struct btrfs_root *root)
Chris Mason6702ed42007-08-07 16:15:09 -040089{
Yan Zhengf321e492008-07-30 09:26:11 -040090 struct btrfs_dirty_root *dirty;
Chris Mason6702ed42007-08-07 16:15:09 -040091 u64 running_trans_id = root->fs_info->running_transaction->transid;
92 if (root->ref_cows && root->last_trans < running_trans_id) {
93 WARN_ON(root == root->fs_info->extent_root);
94 if (root->root_item.refs != 0) {
95 radix_tree_tag_set(&root->fs_info->fs_roots_radix,
96 (unsigned long)root->root_key.objectid,
97 BTRFS_ROOT_TRANS_TAG);
Yan Zheng31153d82008-07-28 15:32:19 -040098
99 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
100 BUG_ON(!dirty);
101 dirty->root = kmalloc(sizeof(*dirty->root), GFP_NOFS);
102 BUG_ON(!dirty->root);
Yan Zheng31153d82008-07-28 15:32:19 -0400103 dirty->latest_root = root;
104 INIT_LIST_HEAD(&dirty->list);
Yan Zheng31153d82008-07-28 15:32:19 -0400105
Chris Mason925baed2008-06-25 16:01:30 -0400106 root->commit_root = btrfs_root_node(root);
Yan Zheng31153d82008-07-28 15:32:19 -0400107
108 memcpy(dirty->root, root, sizeof(*root));
109 spin_lock_init(&dirty->root->node_lock);
Yanbcc63ab2008-07-30 16:29:20 -0400110 spin_lock_init(&dirty->root->list_lock);
Yan Zheng31153d82008-07-28 15:32:19 -0400111 mutex_init(&dirty->root->objectid_mutex);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400112 mutex_init(&dirty->root->log_mutex);
Yanbcc63ab2008-07-30 16:29:20 -0400113 INIT_LIST_HEAD(&dirty->root->dead_list);
Yan Zheng31153d82008-07-28 15:32:19 -0400114 dirty->root->node = root->commit_root;
115 dirty->root->commit_root = NULL;
Yanbcc63ab2008-07-30 16:29:20 -0400116
117 spin_lock(&root->list_lock);
118 list_add(&dirty->root->dead_list, &root->dead_list);
119 spin_unlock(&root->list_lock);
120
121 root->dirty_root = dirty;
Chris Mason6702ed42007-08-07 16:15:09 -0400122 } else {
123 WARN_ON(1);
124 }
125 root->last_trans = running_trans_id;
126 }
127 return 0;
128}
129
Chris Mason37d1aee2008-07-31 10:48:37 -0400130static void wait_current_trans(struct btrfs_root *root)
Chris Mason79154b12007-03-22 15:59:16 -0400131{
Chris Masonf9295742008-07-17 12:54:14 -0400132 struct btrfs_transaction *cur_trans;
Chris Mason79154b12007-03-22 15:59:16 -0400133
Chris Masonf9295742008-07-17 12:54:14 -0400134 cur_trans = root->fs_info->running_transaction;
Chris Mason37d1aee2008-07-31 10:48:37 -0400135 if (cur_trans && cur_trans->blocked) {
Chris Masonf9295742008-07-17 12:54:14 -0400136 DEFINE_WAIT(wait);
137 cur_trans->use_count++;
138 while(1) {
139 prepare_to_wait(&root->fs_info->transaction_wait, &wait,
140 TASK_UNINTERRUPTIBLE);
141 if (cur_trans->blocked) {
142 mutex_unlock(&root->fs_info->trans_mutex);
143 schedule();
144 mutex_lock(&root->fs_info->trans_mutex);
145 finish_wait(&root->fs_info->transaction_wait,
146 &wait);
147 } else {
148 finish_wait(&root->fs_info->transaction_wait,
149 &wait);
150 break;
151 }
152 }
153 put_transaction(cur_trans);
154 }
Chris Mason37d1aee2008-07-31 10:48:37 -0400155}
156
Chris Masone02119d2008-09-05 16:13:11 -0400157static struct btrfs_trans_handle *start_transaction(struct btrfs_root *root,
Sage Weil9ca9ee02008-08-04 10:41:27 -0400158 int num_blocks, int wait)
Chris Mason37d1aee2008-07-31 10:48:37 -0400159{
160 struct btrfs_trans_handle *h =
161 kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
162 int ret;
163
164 mutex_lock(&root->fs_info->trans_mutex);
Chris Mason4bef0842008-09-08 11:18:08 -0400165 if (!root->fs_info->log_root_recovering &&
166 ((wait == 1 && !root->fs_info->open_ioctl_trans) || wait == 2))
Chris Mason37d1aee2008-07-31 10:48:37 -0400167 wait_current_trans(root);
Chris Mason79154b12007-03-22 15:59:16 -0400168 ret = join_transaction(root);
169 BUG_ON(ret);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400170
Chris Masone02119d2008-09-05 16:13:11 -0400171 btrfs_record_root_in_trans(root);
Chris Mason6702ed42007-08-07 16:15:09 -0400172 h->transid = root->fs_info->running_transaction->transid;
Chris Mason79154b12007-03-22 15:59:16 -0400173 h->transaction = root->fs_info->running_transaction;
174 h->blocks_reserved = num_blocks;
175 h->blocks_used = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400176 h->block_group = NULL;
Chris Mason26b80032007-08-08 20:17:12 -0400177 h->alloc_exclude_nr = 0;
178 h->alloc_exclude_start = 0;
Chris Mason79154b12007-03-22 15:59:16 -0400179 root->fs_info->running_transaction->use_count++;
180 mutex_unlock(&root->fs_info->trans_mutex);
181 return h;
182}
183
Chris Masonf9295742008-07-17 12:54:14 -0400184struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
185 int num_blocks)
186{
Sage Weil9ca9ee02008-08-04 10:41:27 -0400187 return start_transaction(root, num_blocks, 1);
Chris Masonf9295742008-07-17 12:54:14 -0400188}
189struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root,
190 int num_blocks)
191{
Sage Weil9ca9ee02008-08-04 10:41:27 -0400192 return start_transaction(root, num_blocks, 0);
Chris Masonf9295742008-07-17 12:54:14 -0400193}
194
Sage Weil9ca9ee02008-08-04 10:41:27 -0400195struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *r,
196 int num_blocks)
197{
198 return start_transaction(r, num_blocks, 2);
199}
200
201
Chris Mason89ce8a62008-06-25 16:01:31 -0400202static noinline int wait_for_commit(struct btrfs_root *root,
203 struct btrfs_transaction *commit)
204{
205 DEFINE_WAIT(wait);
206 mutex_lock(&root->fs_info->trans_mutex);
207 while(!commit->commit_done) {
208 prepare_to_wait(&commit->commit_wait, &wait,
209 TASK_UNINTERRUPTIBLE);
210 if (commit->commit_done)
211 break;
212 mutex_unlock(&root->fs_info->trans_mutex);
213 schedule();
214 mutex_lock(&root->fs_info->trans_mutex);
215 }
216 mutex_unlock(&root->fs_info->trans_mutex);
217 finish_wait(&commit->commit_wait, &wait);
218 return 0;
219}
220
Chris Mason37d1aee2008-07-31 10:48:37 -0400221static void throttle_on_drops(struct btrfs_root *root)
Chris Masonab78c842008-07-29 16:15:18 -0400222{
223 struct btrfs_fs_info *info = root->fs_info;
Chris Mason2dd3e672008-08-04 08:20:15 -0400224 int harder_count = 0;
Chris Masonab78c842008-07-29 16:15:18 -0400225
Chris Mason2dd3e672008-08-04 08:20:15 -0400226harder:
Chris Masonab78c842008-07-29 16:15:18 -0400227 if (atomic_read(&info->throttles)) {
228 DEFINE_WAIT(wait);
229 int thr;
Chris Masonab78c842008-07-29 16:15:18 -0400230 thr = atomic_read(&info->throttle_gen);
231
232 do {
233 prepare_to_wait(&info->transaction_throttle,
234 &wait, TASK_UNINTERRUPTIBLE);
235 if (!atomic_read(&info->throttles)) {
236 finish_wait(&info->transaction_throttle, &wait);
237 break;
238 }
239 schedule();
240 finish_wait(&info->transaction_throttle, &wait);
241 } while (thr == atomic_read(&info->throttle_gen));
Chris Mason2dd3e672008-08-04 08:20:15 -0400242 harder_count++;
243
244 if (root->fs_info->total_ref_cache_size > 1 * 1024 * 1024 &&
245 harder_count < 2)
246 goto harder;
247
248 if (root->fs_info->total_ref_cache_size > 5 * 1024 * 1024 &&
249 harder_count < 10)
250 goto harder;
251
252 if (root->fs_info->total_ref_cache_size > 10 * 1024 * 1024 &&
253 harder_count < 20)
254 goto harder;
Chris Masonab78c842008-07-29 16:15:18 -0400255 }
256}
257
Chris Mason37d1aee2008-07-31 10:48:37 -0400258void btrfs_throttle(struct btrfs_root *root)
259{
260 mutex_lock(&root->fs_info->trans_mutex);
Sage Weil9ca9ee02008-08-04 10:41:27 -0400261 if (!root->fs_info->open_ioctl_trans)
262 wait_current_trans(root);
Chris Mason37d1aee2008-07-31 10:48:37 -0400263 mutex_unlock(&root->fs_info->trans_mutex);
264
265 throttle_on_drops(root);
266}
267
Chris Mason89ce8a62008-06-25 16:01:31 -0400268static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
269 struct btrfs_root *root, int throttle)
Chris Mason79154b12007-03-22 15:59:16 -0400270{
271 struct btrfs_transaction *cur_trans;
Chris Masonab78c842008-07-29 16:15:18 -0400272 struct btrfs_fs_info *info = root->fs_info;
Chris Masond6e4a422007-04-06 15:37:36 -0400273
Chris Masonab78c842008-07-29 16:15:18 -0400274 mutex_lock(&info->trans_mutex);
275 cur_trans = info->running_transaction;
Chris Masonccd467d2007-06-28 15:57:36 -0400276 WARN_ON(cur_trans != trans->transaction);
Chris Masond5719762007-03-23 10:01:08 -0400277 WARN_ON(cur_trans->num_writers < 1);
Chris Masonccd467d2007-06-28 15:57:36 -0400278 cur_trans->num_writers--;
Chris Mason89ce8a62008-06-25 16:01:31 -0400279
Chris Mason79154b12007-03-22 15:59:16 -0400280 if (waitqueue_active(&cur_trans->writer_wait))
281 wake_up(&cur_trans->writer_wait);
Chris Mason79154b12007-03-22 15:59:16 -0400282 put_transaction(cur_trans);
Chris Masonab78c842008-07-29 16:15:18 -0400283 mutex_unlock(&info->trans_mutex);
Chris Masond6025572007-03-30 14:27:56 -0400284 memset(trans, 0, sizeof(*trans));
Chris Mason2c90e5d2007-04-02 10:50:19 -0400285 kmem_cache_free(btrfs_trans_handle_cachep, trans);
Chris Masonab78c842008-07-29 16:15:18 -0400286
287 if (throttle)
Chris Mason37d1aee2008-07-31 10:48:37 -0400288 throttle_on_drops(root);
Chris Masonab78c842008-07-29 16:15:18 -0400289
Chris Mason79154b12007-03-22 15:59:16 -0400290 return 0;
291}
292
Chris Mason89ce8a62008-06-25 16:01:31 -0400293int btrfs_end_transaction(struct btrfs_trans_handle *trans,
294 struct btrfs_root *root)
295{
296 return __btrfs_end_transaction(trans, root, 0);
297}
298
299int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
300 struct btrfs_root *root)
301{
302 return __btrfs_end_transaction(trans, root, 1);
303}
304
Chris Mason79154b12007-03-22 15:59:16 -0400305
Chris Masond0c803c2008-09-11 16:17:57 -0400306int btrfs_write_and_wait_marked_extents(struct btrfs_root *root,
307 struct extent_io_tree *dirty_pages)
Chris Mason79154b12007-03-22 15:59:16 -0400308{
Chris Mason7c4452b2007-04-28 09:29:35 -0400309 int ret;
Chris Mason777e6bd2008-08-15 15:34:15 -0400310 int err = 0;
Chris Mason7c4452b2007-04-28 09:29:35 -0400311 int werr = 0;
312 struct page *page;
Chris Mason7c4452b2007-04-28 09:29:35 -0400313 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Mason777e6bd2008-08-15 15:34:15 -0400314 u64 start = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400315 u64 end;
316 unsigned long index;
Chris Mason7c4452b2007-04-28 09:29:35 -0400317
Chris Mason7c4452b2007-04-28 09:29:35 -0400318 while(1) {
Chris Mason777e6bd2008-08-15 15:34:15 -0400319 ret = find_first_extent_bit(dirty_pages, start, &start, &end,
Chris Mason5f39d392007-10-15 16:14:19 -0400320 EXTENT_DIRTY);
321 if (ret)
Chris Mason7c4452b2007-04-28 09:29:35 -0400322 break;
Chris Mason5f39d392007-10-15 16:14:19 -0400323 while(start <= end) {
Chris Mason777e6bd2008-08-15 15:34:15 -0400324 cond_resched();
325
Chris Mason5f39d392007-10-15 16:14:19 -0400326 index = start >> PAGE_CACHE_SHIFT;
Chris Mason35ebb932007-10-30 16:56:53 -0400327 start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
Chris Mason4bef0842008-09-08 11:18:08 -0400328 page = find_get_page(btree_inode->i_mapping, index);
Chris Mason7c4452b2007-04-28 09:29:35 -0400329 if (!page)
330 continue;
Chris Mason4bef0842008-09-08 11:18:08 -0400331
332 btree_lock_page_hook(page);
333 if (!page->mapping) {
334 unlock_page(page);
335 page_cache_release(page);
336 continue;
337 }
338
Chris Mason6702ed42007-08-07 16:15:09 -0400339 if (PageWriteback(page)) {
340 if (PageDirty(page))
341 wait_on_page_writeback(page);
342 else {
343 unlock_page(page);
344 page_cache_release(page);
345 continue;
346 }
347 }
Chris Mason7c4452b2007-04-28 09:29:35 -0400348 err = write_one_page(page, 0);
349 if (err)
350 werr = err;
351 page_cache_release(page);
352 }
353 }
Chris Mason777e6bd2008-08-15 15:34:15 -0400354 while(1) {
355 ret = find_first_extent_bit(dirty_pages, 0, &start, &end,
356 EXTENT_DIRTY);
357 if (ret)
358 break;
359
360 clear_extent_dirty(dirty_pages, start, end, GFP_NOFS);
361 while(start <= end) {
362 index = start >> PAGE_CACHE_SHIFT;
363 start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
364 page = find_get_page(btree_inode->i_mapping, index);
365 if (!page)
366 continue;
367 if (PageDirty(page)) {
Chris Mason4bef0842008-09-08 11:18:08 -0400368 btree_lock_page_hook(page);
369 wait_on_page_writeback(page);
Chris Mason777e6bd2008-08-15 15:34:15 -0400370 err = write_one_page(page, 0);
371 if (err)
372 werr = err;
373 }
374 wait_on_page_writeback(page);
375 page_cache_release(page);
376 cond_resched();
377 }
378 }
Chris Mason7c4452b2007-04-28 09:29:35 -0400379 if (err)
380 werr = err;
381 return werr;
Chris Mason79154b12007-03-22 15:59:16 -0400382}
383
Chris Masond0c803c2008-09-11 16:17:57 -0400384int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
385 struct btrfs_root *root)
386{
387 if (!trans || !trans->transaction) {
388 struct inode *btree_inode;
389 btree_inode = root->fs_info->btree_inode;
390 return filemap_write_and_wait(btree_inode->i_mapping);
391 }
392 return btrfs_write_and_wait_marked_extents(root,
393 &trans->transaction->dirty_pages);
394}
395
Chris Mason0b86a832008-03-24 15:01:56 -0400396static int update_cowonly_root(struct btrfs_trans_handle *trans,
397 struct btrfs_root *root)
398{
399 int ret;
400 u64 old_root_bytenr;
401 struct btrfs_root *tree_root = root->fs_info->tree_root;
402
403 btrfs_write_dirty_block_groups(trans, root);
404 while(1) {
405 old_root_bytenr = btrfs_root_bytenr(&root->root_item);
406 if (old_root_bytenr == root->node->start)
407 break;
408 btrfs_set_root_bytenr(&root->root_item,
409 root->node->start);
410 btrfs_set_root_level(&root->root_item,
411 btrfs_header_level(root->node));
412 ret = btrfs_update_root(trans, tree_root,
413 &root->root_key,
414 &root->root_item);
415 BUG_ON(ret);
416 btrfs_write_dirty_block_groups(trans, root);
417 }
418 return 0;
419}
420
Chris Mason79154b12007-03-22 15:59:16 -0400421int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans,
422 struct btrfs_root *root)
423{
Chris Mason79154b12007-03-22 15:59:16 -0400424 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason0b86a832008-03-24 15:01:56 -0400425 struct list_head *next;
Chris Mason79154b12007-03-22 15:59:16 -0400426
Chris Mason0b86a832008-03-24 15:01:56 -0400427 while(!list_empty(&fs_info->dirty_cowonly_roots)) {
428 next = fs_info->dirty_cowonly_roots.next;
429 list_del_init(next);
430 root = list_entry(next, struct btrfs_root, dirty_list);
431 update_cowonly_root(trans, root);
Chris Mason79154b12007-03-22 15:59:16 -0400432 }
433 return 0;
434}
435
Yan Zhengb48652c2008-08-04 23:23:47 -0400436int btrfs_add_dead_root(struct btrfs_root *root, struct btrfs_root *latest)
Chris Mason5eda7b52007-06-22 14:16:25 -0400437{
Yan Zhengf321e492008-07-30 09:26:11 -0400438 struct btrfs_dirty_root *dirty;
Chris Mason5eda7b52007-06-22 14:16:25 -0400439
440 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
441 if (!dirty)
442 return -ENOMEM;
Chris Mason5eda7b52007-06-22 14:16:25 -0400443 dirty->root = root;
Chris Mason5ce14bb2007-09-11 11:15:39 -0400444 dirty->latest_root = latest;
Yan Zhengb48652c2008-08-04 23:23:47 -0400445
446 mutex_lock(&root->fs_info->trans_mutex);
447 list_add(&dirty->list, &latest->fs_info->dead_roots);
448 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason5eda7b52007-06-22 14:16:25 -0400449 return 0;
450}
451
Chris Mason80b67942008-02-01 16:35:04 -0500452static noinline int add_dirty_roots(struct btrfs_trans_handle *trans,
453 struct radix_tree_root *radix,
454 struct list_head *list)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400455{
Yan Zhengf321e492008-07-30 09:26:11 -0400456 struct btrfs_dirty_root *dirty;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400457 struct btrfs_root *gang[8];
458 struct btrfs_root *root;
459 int i;
460 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400461 int err = 0;
Chris Mason5eda7b52007-06-22 14:16:25 -0400462 u32 refs;
Chris Mason54aa1f42007-06-22 14:16:25 -0400463
Chris Mason0f7d52f2007-04-09 10:42:37 -0400464 while(1) {
465 ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0,
466 ARRAY_SIZE(gang),
467 BTRFS_ROOT_TRANS_TAG);
468 if (ret == 0)
469 break;
470 for (i = 0; i < ret; i++) {
471 root = gang[i];
Chris Mason2619ba12007-04-10 16:58:11 -0400472 radix_tree_tag_clear(radix,
473 (unsigned long)root->root_key.objectid,
474 BTRFS_ROOT_TRANS_TAG);
Yan Zheng31153d82008-07-28 15:32:19 -0400475
476 BUG_ON(!root->ref_tree);
Chris Mason017e5362008-07-28 15:32:51 -0400477 dirty = root->dirty_root;
Yan Zheng31153d82008-07-28 15:32:19 -0400478
Chris Masone02119d2008-09-05 16:13:11 -0400479 btrfs_free_log(trans, root);
480
Chris Mason0f7d52f2007-04-09 10:42:37 -0400481 if (root->commit_root == root->node) {
Chris Masondb945352007-10-15 16:15:53 -0400482 WARN_ON(root->node->start !=
483 btrfs_root_bytenr(&root->root_item));
Yan Zheng31153d82008-07-28 15:32:19 -0400484
Chris Mason5f39d392007-10-15 16:14:19 -0400485 free_extent_buffer(root->commit_root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400486 root->commit_root = NULL;
Yan Zheng7ea394f2008-08-05 13:05:02 -0400487 root->dirty_root = NULL;
Yanbcc63ab2008-07-30 16:29:20 -0400488
489 spin_lock(&root->list_lock);
490 list_del_init(&dirty->root->dead_list);
491 spin_unlock(&root->list_lock);
492
Yan Zheng31153d82008-07-28 15:32:19 -0400493 kfree(dirty->root);
494 kfree(dirty);
Josef Bacik58176a92007-08-29 15:47:34 -0400495
496 /* make sure to update the root on disk
497 * so we get any updates to the block used
498 * counts
499 */
500 err = btrfs_update_root(trans,
501 root->fs_info->tree_root,
502 &root->root_key,
503 &root->root_item);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400504 continue;
505 }
Chris Mason9f3a7422007-08-07 15:52:19 -0400506
507 memset(&root->root_item.drop_progress, 0,
508 sizeof(struct btrfs_disk_key));
509 root->root_item.drop_level = 0;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400510 root->commit_root = NULL;
Yan Zheng7ea394f2008-08-05 13:05:02 -0400511 root->dirty_root = NULL;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400512 root->root_key.offset = root->fs_info->generation;
Chris Masondb945352007-10-15 16:15:53 -0400513 btrfs_set_root_bytenr(&root->root_item,
514 root->node->start);
515 btrfs_set_root_level(&root->root_item,
516 btrfs_header_level(root->node));
Chris Mason0f7d52f2007-04-09 10:42:37 -0400517 err = btrfs_insert_root(trans, root->fs_info->tree_root,
518 &root->root_key,
519 &root->root_item);
Chris Mason54aa1f42007-06-22 14:16:25 -0400520 if (err)
521 break;
Chris Mason9f3a7422007-08-07 15:52:19 -0400522
523 refs = btrfs_root_refs(&dirty->root->root_item);
524 btrfs_set_root_refs(&dirty->root->root_item, refs - 1);
Chris Mason5eda7b52007-06-22 14:16:25 -0400525 err = btrfs_update_root(trans, root->fs_info->tree_root,
Chris Mason9f3a7422007-08-07 15:52:19 -0400526 &dirty->root->root_key,
527 &dirty->root->root_item);
Chris Mason5eda7b52007-06-22 14:16:25 -0400528
529 BUG_ON(err);
Chris Mason9f3a7422007-08-07 15:52:19 -0400530 if (refs == 1) {
Chris Mason5eda7b52007-06-22 14:16:25 -0400531 list_add(&dirty->list, list);
Chris Mason9f3a7422007-08-07 15:52:19 -0400532 } else {
533 WARN_ON(1);
Yan Zheng31153d82008-07-28 15:32:19 -0400534 free_extent_buffer(dirty->root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -0400535 kfree(dirty->root);
Chris Mason5eda7b52007-06-22 14:16:25 -0400536 kfree(dirty);
Chris Mason9f3a7422007-08-07 15:52:19 -0400537 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400538 }
539 }
Chris Mason54aa1f42007-06-22 14:16:25 -0400540 return err;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400541}
542
Chris Masone9d0b132007-08-10 14:06:19 -0400543int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
544{
545 struct btrfs_fs_info *info = root->fs_info;
546 int ret;
547 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400548 unsigned long nr;
Chris Masone9d0b132007-08-10 14:06:19 -0400549
Chris Masona2135012008-06-25 16:01:30 -0400550 smp_mb();
Chris Masone9d0b132007-08-10 14:06:19 -0400551 if (root->defrag_running)
552 return 0;
Chris Masone9d0b132007-08-10 14:06:19 -0400553 trans = btrfs_start_transaction(root, 1);
Chris Mason6b800532007-10-15 16:17:34 -0400554 while (1) {
Chris Masone9d0b132007-08-10 14:06:19 -0400555 root->defrag_running = 1;
556 ret = btrfs_defrag_leaves(trans, root, cacheonly);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400557 nr = trans->blocks_used;
Chris Masone9d0b132007-08-10 14:06:19 -0400558 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400559 btrfs_btree_balance_dirty(info->tree_root, nr);
Chris Masone9d0b132007-08-10 14:06:19 -0400560 cond_resched();
561
Chris Masone9d0b132007-08-10 14:06:19 -0400562 trans = btrfs_start_transaction(root, 1);
Chris Mason3f157a22008-06-25 16:01:31 -0400563 if (root->fs_info->closing || ret != -EAGAIN)
Chris Masone9d0b132007-08-10 14:06:19 -0400564 break;
565 }
566 root->defrag_running = 0;
Chris Masona2135012008-06-25 16:01:30 -0400567 smp_mb();
Chris Masone9d0b132007-08-10 14:06:19 -0400568 btrfs_end_transaction(trans, root);
569 return 0;
570}
571
Chris Mason80b67942008-02-01 16:35:04 -0500572static noinline int drop_dirty_roots(struct btrfs_root *tree_root,
573 struct list_head *list)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400574{
Yan Zhengf321e492008-07-30 09:26:11 -0400575 struct btrfs_dirty_root *dirty;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400576 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400577 unsigned long nr;
Chris Masondb945352007-10-15 16:15:53 -0400578 u64 num_bytes;
579 u64 bytes_used;
Yanbcc63ab2008-07-30 16:29:20 -0400580 u64 max_useless;
Chris Mason54aa1f42007-06-22 14:16:25 -0400581 int ret = 0;
Chris Mason9f3a7422007-08-07 15:52:19 -0400582 int err;
583
Chris Mason0f7d52f2007-04-09 10:42:37 -0400584 while(!list_empty(list)) {
Josef Bacik58176a92007-08-29 15:47:34 -0400585 struct btrfs_root *root;
586
Yan Zhengf321e492008-07-30 09:26:11 -0400587 dirty = list_entry(list->prev, struct btrfs_dirty_root, list);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400588 list_del_init(&dirty->list);
Chris Mason5eda7b52007-06-22 14:16:25 -0400589
Chris Masondb945352007-10-15 16:15:53 -0400590 num_bytes = btrfs_root_used(&dirty->root->root_item);
Josef Bacik58176a92007-08-29 15:47:34 -0400591 root = dirty->latest_root;
Chris Masona2135012008-06-25 16:01:30 -0400592 atomic_inc(&root->fs_info->throttles);
Josef Bacik58176a92007-08-29 15:47:34 -0400593
Chris Mason9f3a7422007-08-07 15:52:19 -0400594 while(1) {
595 trans = btrfs_start_transaction(tree_root, 1);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400596 mutex_lock(&root->fs_info->drop_mutex);
Chris Mason9f3a7422007-08-07 15:52:19 -0400597 ret = btrfs_drop_snapshot(trans, dirty->root);
598 if (ret != -EAGAIN) {
599 break;
600 }
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400601 mutex_unlock(&root->fs_info->drop_mutex);
Josef Bacik58176a92007-08-29 15:47:34 -0400602
Chris Mason9f3a7422007-08-07 15:52:19 -0400603 err = btrfs_update_root(trans,
604 tree_root,
605 &dirty->root->root_key,
606 &dirty->root->root_item);
607 if (err)
608 ret = err;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400609 nr = trans->blocks_used;
Chris Mason017e5362008-07-28 15:32:51 -0400610 ret = btrfs_end_transaction(trans, tree_root);
Chris Mason9f3a7422007-08-07 15:52:19 -0400611 BUG_ON(ret);
Chris Masona2135012008-06-25 16:01:30 -0400612
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400613 btrfs_btree_balance_dirty(tree_root, nr);
Chris Mason4dc119042007-10-15 16:18:14 -0400614 cond_resched();
Chris Mason9f3a7422007-08-07 15:52:19 -0400615 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400616 BUG_ON(ret);
Chris Masona2135012008-06-25 16:01:30 -0400617 atomic_dec(&root->fs_info->throttles);
Chris Mason017e5362008-07-28 15:32:51 -0400618 wake_up(&root->fs_info->transaction_throttle);
Josef Bacik58176a92007-08-29 15:47:34 -0400619
Chris Masona2135012008-06-25 16:01:30 -0400620 mutex_lock(&root->fs_info->alloc_mutex);
Chris Masondb945352007-10-15 16:15:53 -0400621 num_bytes -= btrfs_root_used(&dirty->root->root_item);
622 bytes_used = btrfs_root_used(&root->root_item);
623 if (num_bytes) {
Chris Masone02119d2008-09-05 16:13:11 -0400624 btrfs_record_root_in_trans(root);
Chris Mason5f39d392007-10-15 16:14:19 -0400625 btrfs_set_root_used(&root->root_item,
Chris Masondb945352007-10-15 16:15:53 -0400626 bytes_used - num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -0400627 }
Chris Masona2135012008-06-25 16:01:30 -0400628 mutex_unlock(&root->fs_info->alloc_mutex);
629
Chris Mason9f3a7422007-08-07 15:52:19 -0400630 ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key);
Josef Bacik58176a92007-08-29 15:47:34 -0400631 if (ret) {
632 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -0400633 break;
Josef Bacik58176a92007-08-29 15:47:34 -0400634 }
Chris Masona2135012008-06-25 16:01:30 -0400635 mutex_unlock(&root->fs_info->drop_mutex);
636
Yanbcc63ab2008-07-30 16:29:20 -0400637 spin_lock(&root->list_lock);
638 list_del_init(&dirty->root->dead_list);
639 if (!list_empty(&root->dead_list)) {
640 struct btrfs_root *oldest;
641 oldest = list_entry(root->dead_list.prev,
642 struct btrfs_root, dead_list);
643 max_useless = oldest->root_key.offset - 1;
644 } else {
645 max_useless = root->root_key.offset - 1;
646 }
647 spin_unlock(&root->list_lock);
648
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400649 nr = trans->blocks_used;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400650 ret = btrfs_end_transaction(trans, tree_root);
651 BUG_ON(ret);
Chris Mason5eda7b52007-06-22 14:16:25 -0400652
Zheng Yane4657682008-09-26 10:04:53 -0400653 ret = btrfs_remove_leaf_refs(root, max_useless, 0);
Yanbcc63ab2008-07-30 16:29:20 -0400654 BUG_ON(ret);
655
Chris Masonf510cfe2007-10-15 16:14:48 -0400656 free_extent_buffer(dirty->root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -0400657 kfree(dirty->root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400658 kfree(dirty);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400659
660 btrfs_btree_balance_dirty(tree_root, nr);
Chris Mason4dc119042007-10-15 16:18:14 -0400661 cond_resched();
Chris Mason0f7d52f2007-04-09 10:42:37 -0400662 }
Chris Mason54aa1f42007-06-22 14:16:25 -0400663 return ret;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400664}
665
Chris Mason80b67942008-02-01 16:35:04 -0500666static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
Chris Mason3063d292008-01-08 15:46:30 -0500667 struct btrfs_fs_info *fs_info,
668 struct btrfs_pending_snapshot *pending)
669{
670 struct btrfs_key key;
Chris Mason80b67942008-02-01 16:35:04 -0500671 struct btrfs_root_item *new_root_item;
Chris Mason3063d292008-01-08 15:46:30 -0500672 struct btrfs_root *tree_root = fs_info->tree_root;
673 struct btrfs_root *root = pending->root;
674 struct extent_buffer *tmp;
Chris Mason925baed2008-06-25 16:01:30 -0400675 struct extent_buffer *old;
Chris Mason3063d292008-01-08 15:46:30 -0500676 int ret;
Sven Wegener3b963622008-06-09 21:57:42 -0400677 int namelen;
Chris Mason3063d292008-01-08 15:46:30 -0500678 u64 objectid;
679
Chris Mason80b67942008-02-01 16:35:04 -0500680 new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
681 if (!new_root_item) {
682 ret = -ENOMEM;
683 goto fail;
684 }
Chris Mason3063d292008-01-08 15:46:30 -0500685 ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid);
686 if (ret)
687 goto fail;
688
Chris Mason80b67942008-02-01 16:35:04 -0500689 memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
Chris Mason3063d292008-01-08 15:46:30 -0500690
691 key.objectid = objectid;
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400692 key.offset = trans->transid;
Chris Mason3063d292008-01-08 15:46:30 -0500693 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
694
Chris Mason925baed2008-06-25 16:01:30 -0400695 old = btrfs_lock_root_node(root);
Chris Mason65b51a02008-08-01 15:11:20 -0400696 btrfs_cow_block(trans, root, old, NULL, 0, &old, 0);
Chris Mason3063d292008-01-08 15:46:30 -0500697
Chris Mason925baed2008-06-25 16:01:30 -0400698 btrfs_copy_root(trans, root, old, &tmp, objectid);
699 btrfs_tree_unlock(old);
700 free_extent_buffer(old);
Chris Mason3063d292008-01-08 15:46:30 -0500701
Chris Mason80b67942008-02-01 16:35:04 -0500702 btrfs_set_root_bytenr(new_root_item, tmp->start);
703 btrfs_set_root_level(new_root_item, btrfs_header_level(tmp));
Chris Mason3063d292008-01-08 15:46:30 -0500704 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
Chris Mason80b67942008-02-01 16:35:04 -0500705 new_root_item);
Chris Mason925baed2008-06-25 16:01:30 -0400706 btrfs_tree_unlock(tmp);
Chris Mason3063d292008-01-08 15:46:30 -0500707 free_extent_buffer(tmp);
708 if (ret)
709 goto fail;
710
711 /*
712 * insert the directory item
713 */
714 key.offset = (u64)-1;
Sven Wegener3b963622008-06-09 21:57:42 -0400715 namelen = strlen(pending->name);
Chris Mason3063d292008-01-08 15:46:30 -0500716 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
Sven Wegener3b963622008-06-09 21:57:42 -0400717 pending->name, namelen,
Chris Mason3063d292008-01-08 15:46:30 -0500718 root->fs_info->sb->s_root->d_inode->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -0400719 &key, BTRFS_FT_DIR, 0);
Chris Mason3063d292008-01-08 15:46:30 -0500720
721 if (ret)
722 goto fail;
723
724 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
725 pending->name, strlen(pending->name), objectid,
Josef Bacikaec74772008-07-24 12:12:38 -0400726 root->fs_info->sb->s_root->d_inode->i_ino, 0);
Sven Wegener3b963622008-06-09 21:57:42 -0400727
728 /* Invalidate existing dcache entry for new snapshot. */
729 btrfs_invalidate_dcache_root(root, pending->name, namelen);
730
Chris Mason3063d292008-01-08 15:46:30 -0500731fail:
Chris Mason80b67942008-02-01 16:35:04 -0500732 kfree(new_root_item);
Chris Mason3063d292008-01-08 15:46:30 -0500733 return ret;
734}
735
Chris Mason80b67942008-02-01 16:35:04 -0500736static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
737 struct btrfs_fs_info *fs_info)
Chris Mason3063d292008-01-08 15:46:30 -0500738{
739 struct btrfs_pending_snapshot *pending;
740 struct list_head *head = &trans->transaction->pending_snapshots;
741 int ret;
742
743 while(!list_empty(head)) {
744 pending = list_entry(head->next,
745 struct btrfs_pending_snapshot, list);
746 ret = create_pending_snapshot(trans, fs_info, pending);
747 BUG_ON(ret);
748 list_del(&pending->list);
749 kfree(pending->name);
750 kfree(pending);
751 }
Chris Masondc17ff82008-01-08 15:46:30 -0500752 return 0;
753}
754
Chris Mason79154b12007-03-22 15:59:16 -0400755int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
756 struct btrfs_root *root)
757{
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400758 unsigned long joined = 0;
759 unsigned long timeout = 1;
Chris Mason79154b12007-03-22 15:59:16 -0400760 struct btrfs_transaction *cur_trans;
Chris Mason8fd17792007-04-19 21:01:03 -0400761 struct btrfs_transaction *prev_trans = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -0400762 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400763 struct list_head dirty_fs_roots;
Chris Masond1310b22008-01-24 16:13:08 -0500764 struct extent_io_tree *pinned_copy;
Chris Mason79154b12007-03-22 15:59:16 -0400765 DEFINE_WAIT(wait);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400766 int ret;
Chris Mason79154b12007-03-22 15:59:16 -0400767
Chris Mason0f7d52f2007-04-09 10:42:37 -0400768 INIT_LIST_HEAD(&dirty_fs_roots);
Chris Mason79154b12007-03-22 15:59:16 -0400769 mutex_lock(&root->fs_info->trans_mutex);
770 if (trans->transaction->in_commit) {
771 cur_trans = trans->transaction;
772 trans->transaction->use_count++;
Chris Masonccd467d2007-06-28 15:57:36 -0400773 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400774 btrfs_end_transaction(trans, root);
Chris Masonccd467d2007-06-28 15:57:36 -0400775
Chris Mason79154b12007-03-22 15:59:16 -0400776 ret = wait_for_commit(root, cur_trans);
777 BUG_ON(ret);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400778
779 mutex_lock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400780 put_transaction(cur_trans);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400781 mutex_unlock(&root->fs_info->trans_mutex);
782
Chris Mason79154b12007-03-22 15:59:16 -0400783 return 0;
784 }
Chris Mason4313b392008-01-03 09:08:48 -0500785
786 pinned_copy = kmalloc(sizeof(*pinned_copy), GFP_NOFS);
787 if (!pinned_copy)
788 return -ENOMEM;
789
Chris Masond1310b22008-01-24 16:13:08 -0500790 extent_io_tree_init(pinned_copy,
Chris Mason4313b392008-01-03 09:08:48 -0500791 root->fs_info->btree_inode->i_mapping, GFP_NOFS);
792
Chris Mason2c90e5d2007-04-02 10:50:19 -0400793 trans->transaction->in_commit = 1;
Chris Masonf9295742008-07-17 12:54:14 -0400794 trans->transaction->blocked = 1;
Chris Masonccd467d2007-06-28 15:57:36 -0400795 cur_trans = trans->transaction;
796 if (cur_trans->list.prev != &root->fs_info->trans_list) {
797 prev_trans = list_entry(cur_trans->list.prev,
798 struct btrfs_transaction, list);
799 if (!prev_trans->commit_done) {
800 prev_trans->use_count++;
Chris Masonccd467d2007-06-28 15:57:36 -0400801 mutex_unlock(&root->fs_info->trans_mutex);
802
803 wait_for_commit(root, prev_trans);
Chris Masonccd467d2007-06-28 15:57:36 -0400804
Chris Masonccd467d2007-06-28 15:57:36 -0400805 mutex_lock(&root->fs_info->trans_mutex);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400806 put_transaction(prev_trans);
Chris Masonccd467d2007-06-28 15:57:36 -0400807 }
808 }
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400809
810 do {
Yan Zheng7ea394f2008-08-05 13:05:02 -0400811 int snap_pending = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400812 joined = cur_trans->num_joined;
Yan Zheng7ea394f2008-08-05 13:05:02 -0400813 if (!list_empty(&trans->transaction->pending_snapshots))
814 snap_pending = 1;
815
Chris Mason2c90e5d2007-04-02 10:50:19 -0400816 WARN_ON(cur_trans != trans->transaction);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400817 prepare_to_wait(&cur_trans->writer_wait, &wait,
Chris Mason79154b12007-03-22 15:59:16 -0400818 TASK_UNINTERRUPTIBLE);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400819
820 if (cur_trans->num_writers > 1)
821 timeout = MAX_SCHEDULE_TIMEOUT;
822 else
823 timeout = 1;
824
Chris Mason79154b12007-03-22 15:59:16 -0400825 mutex_unlock(&root->fs_info->trans_mutex);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400826
Yan Zheng7ea394f2008-08-05 13:05:02 -0400827 if (snap_pending) {
828 ret = btrfs_wait_ordered_extents(root, 1);
829 BUG_ON(ret);
830 }
831
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400832 schedule_timeout(timeout);
833
Chris Mason79154b12007-03-22 15:59:16 -0400834 mutex_lock(&root->fs_info->trans_mutex);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400835 finish_wait(&cur_trans->writer_wait, &wait);
836 } while (cur_trans->num_writers > 1 ||
837 (cur_trans->num_joined != joined));
838
Chris Mason3063d292008-01-08 15:46:30 -0500839 ret = create_pending_snapshots(trans, root->fs_info);
840 BUG_ON(ret);
841
Chris Mason2c90e5d2007-04-02 10:50:19 -0400842 WARN_ON(cur_trans != trans->transaction);
Chris Masondc17ff82008-01-08 15:46:30 -0500843
Chris Masone02119d2008-09-05 16:13:11 -0400844 /* btrfs_commit_tree_roots is responsible for getting the
845 * various roots consistent with each other. Every pointer
846 * in the tree of tree roots has to point to the most up to date
847 * root for every subvolume and other tree. So, we have to keep
848 * the tree logging code from jumping in and changing any
849 * of the trees.
850 *
851 * At this point in the commit, there can't be any tree-log
852 * writers, but a little lower down we drop the trans mutex
853 * and let new people in. By holding the tree_log_mutex
854 * from now until after the super is written, we avoid races
855 * with the tree-log code.
856 */
857 mutex_lock(&root->fs_info->tree_log_mutex);
858
Chris Mason54aa1f42007-06-22 14:16:25 -0400859 ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix,
860 &dirty_fs_roots);
861 BUG_ON(ret);
862
Chris Masone02119d2008-09-05 16:13:11 -0400863 /* add_dirty_roots gets rid of all the tree log roots, it is now
864 * safe to free the root of tree log roots
865 */
866 btrfs_free_log_root_tree(trans, root->fs_info);
867
Chris Mason79154b12007-03-22 15:59:16 -0400868 ret = btrfs_commit_tree_roots(trans, root);
869 BUG_ON(ret);
Chris Mason54aa1f42007-06-22 14:16:25 -0400870
Chris Mason78fae272007-03-25 11:35:08 -0400871 cur_trans = root->fs_info->running_transaction;
Chris Masoncee36a02008-01-15 08:40:48 -0500872 spin_lock(&root->fs_info->new_trans_lock);
Chris Mason78fae272007-03-25 11:35:08 -0400873 root->fs_info->running_transaction = NULL;
Chris Masoncee36a02008-01-15 08:40:48 -0500874 spin_unlock(&root->fs_info->new_trans_lock);
Chris Mason4b52dff2007-06-26 10:06:50 -0400875 btrfs_set_super_generation(&root->fs_info->super_copy,
876 cur_trans->transid);
877 btrfs_set_super_root(&root->fs_info->super_copy,
Chris Masondb945352007-10-15 16:15:53 -0400878 root->fs_info->tree_root->node->start);
879 btrfs_set_super_root_level(&root->fs_info->super_copy,
880 btrfs_header_level(root->fs_info->tree_root->node));
Chris Mason5f39d392007-10-15 16:14:19 -0400881
Chris Mason0b86a832008-03-24 15:01:56 -0400882 btrfs_set_super_chunk_root(&root->fs_info->super_copy,
883 chunk_root->node->start);
884 btrfs_set_super_chunk_root_level(&root->fs_info->super_copy,
885 btrfs_header_level(chunk_root->node));
Chris Masone02119d2008-09-05 16:13:11 -0400886
887 if (!root->fs_info->log_root_recovering) {
888 btrfs_set_super_log_root(&root->fs_info->super_copy, 0);
889 btrfs_set_super_log_root_level(&root->fs_info->super_copy, 0);
890 }
891
Chris Masona061fc82008-05-07 11:43:44 -0400892 memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy,
893 sizeof(root->fs_info->super_copy));
Chris Masonccd467d2007-06-28 15:57:36 -0400894
Chris Mason4313b392008-01-03 09:08:48 -0500895 btrfs_copy_pinned(root, pinned_copy);
Chris Masonccd467d2007-06-28 15:57:36 -0400896
Chris Masonf9295742008-07-17 12:54:14 -0400897 trans->transaction->blocked = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400898 wake_up(&root->fs_info->transaction_throttle);
Chris Masonf9295742008-07-17 12:54:14 -0400899 wake_up(&root->fs_info->transaction_wait);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400900
Chris Mason78fae272007-03-25 11:35:08 -0400901 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400902 ret = btrfs_write_and_wait_transaction(trans, root);
903 BUG_ON(ret);
Chris Mason79154b12007-03-22 15:59:16 -0400904 write_ctree_super(trans, root);
Chris Mason4313b392008-01-03 09:08:48 -0500905
Chris Masone02119d2008-09-05 16:13:11 -0400906 /*
907 * the super is written, we can safely allow the tree-loggers
908 * to go about their business
909 */
910 mutex_unlock(&root->fs_info->tree_log_mutex);
911
Chris Mason4313b392008-01-03 09:08:48 -0500912 btrfs_finish_extent_commit(trans, root, pinned_copy);
Chris Mason78fae272007-03-25 11:35:08 -0400913 mutex_lock(&root->fs_info->trans_mutex);
Chris Mason4313b392008-01-03 09:08:48 -0500914
915 kfree(pinned_copy);
916
Chris Mason2c90e5d2007-04-02 10:50:19 -0400917 cur_trans->commit_done = 1;
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400918 root->fs_info->last_trans_committed = cur_trans->transid;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400919 wake_up(&cur_trans->commit_wait);
Chris Mason79154b12007-03-22 15:59:16 -0400920 put_transaction(cur_trans);
Chris Mason78fae272007-03-25 11:35:08 -0400921 put_transaction(cur_trans);
Josef Bacik58176a92007-08-29 15:47:34 -0400922
Yanbcc63ab2008-07-30 16:29:20 -0400923 list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots);
Chris Masonfacda1e2007-06-08 18:11:48 -0400924 if (root->fs_info->closing)
925 list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots);
Josef Bacik58176a92007-08-29 15:47:34 -0400926
Chris Mason78fae272007-03-25 11:35:08 -0400927 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400928 kmem_cache_free(btrfs_trans_handle_cachep, trans);
Chris Mason79154b12007-03-22 15:59:16 -0400929
Chris Masonfacda1e2007-06-08 18:11:48 -0400930 if (root->fs_info->closing) {
Chris Masonfacda1e2007-06-08 18:11:48 -0400931 drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);
Chris Masonfacda1e2007-06-08 18:11:48 -0400932 }
Chris Mason79154b12007-03-22 15:59:16 -0400933 return ret;
934}
935
Chris Masone9d0b132007-08-10 14:06:19 -0400936int btrfs_clean_old_snapshots(struct btrfs_root *root)
937{
938 struct list_head dirty_roots;
939 INIT_LIST_HEAD(&dirty_roots);
Chris Masona74a4b92008-06-25 16:01:31 -0400940again:
Chris Masone9d0b132007-08-10 14:06:19 -0400941 mutex_lock(&root->fs_info->trans_mutex);
942 list_splice_init(&root->fs_info->dead_roots, &dirty_roots);
943 mutex_unlock(&root->fs_info->trans_mutex);
944
945 if (!list_empty(&dirty_roots)) {
946 drop_dirty_roots(root, &dirty_roots);
Chris Masona74a4b92008-06-25 16:01:31 -0400947 goto again;
Chris Masone9d0b132007-08-10 14:06:19 -0400948 }
949 return 0;
950}