blob: 61a377bcb2fbb4ef74fc863b43df034fa554a857 [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 Masond3c2fdc2007-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);
Yanbcc63ab2008-07-30 16:29:20 -0400112 INIT_LIST_HEAD(&dirty->root->dead_list);
Yan Zheng31153d82008-07-28 15:32:19 -0400113 dirty->root->node = root->commit_root;
114 dirty->root->commit_root = NULL;
Yanbcc63ab2008-07-30 16:29:20 -0400115
116 spin_lock(&root->list_lock);
117 list_add(&dirty->root->dead_list, &root->dead_list);
118 spin_unlock(&root->list_lock);
119
120 root->dirty_root = dirty;
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 Mason37d1aee2008-07-31 10:48:37 -0400129static void wait_current_trans(struct btrfs_root *root)
Chris Mason79154b12007-03-22 15:59:16 -0400130{
Chris Masonf9295742008-07-17 12:54:14 -0400131 struct btrfs_transaction *cur_trans;
Chris Mason79154b12007-03-22 15:59:16 -0400132
Chris Masonf9295742008-07-17 12:54:14 -0400133 cur_trans = root->fs_info->running_transaction;
Chris Mason37d1aee2008-07-31 10:48:37 -0400134 if (cur_trans && cur_trans->blocked) {
Chris Masonf9295742008-07-17 12:54:14 -0400135 DEFINE_WAIT(wait);
136 cur_trans->use_count++;
137 while(1) {
138 prepare_to_wait(&root->fs_info->transaction_wait, &wait,
139 TASK_UNINTERRUPTIBLE);
140 if (cur_trans->blocked) {
141 mutex_unlock(&root->fs_info->trans_mutex);
142 schedule();
143 mutex_lock(&root->fs_info->trans_mutex);
144 finish_wait(&root->fs_info->transaction_wait,
145 &wait);
146 } else {
147 finish_wait(&root->fs_info->transaction_wait,
148 &wait);
149 break;
150 }
151 }
152 put_transaction(cur_trans);
153 }
Chris Mason37d1aee2008-07-31 10:48:37 -0400154}
155
Chris Masone02119d2008-09-05 16:13:11 -0400156static struct btrfs_trans_handle *start_transaction(struct btrfs_root *root,
Sage Weil9ca9ee02008-08-04 10:41:27 -0400157 int num_blocks, int wait)
Chris Mason37d1aee2008-07-31 10:48:37 -0400158{
159 struct btrfs_trans_handle *h =
160 kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
161 int ret;
162
163 mutex_lock(&root->fs_info->trans_mutex);
Chris Mason4bef0842008-09-08 11:18:08 -0400164 if (!root->fs_info->log_root_recovering &&
165 ((wait == 1 && !root->fs_info->open_ioctl_trans) || wait == 2))
Chris Mason37d1aee2008-07-31 10:48:37 -0400166 wait_current_trans(root);
Chris Mason79154b12007-03-22 15:59:16 -0400167 ret = join_transaction(root);
168 BUG_ON(ret);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400169
Chris Masone02119d2008-09-05 16:13:11 -0400170 btrfs_record_root_in_trans(root);
Chris Mason6702ed42007-08-07 16:15:09 -0400171 h->transid = root->fs_info->running_transaction->transid;
Chris Mason79154b12007-03-22 15:59:16 -0400172 h->transaction = root->fs_info->running_transaction;
173 h->blocks_reserved = num_blocks;
174 h->blocks_used = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400175 h->block_group = NULL;
Chris Mason26b80032007-08-08 20:17:12 -0400176 h->alloc_exclude_nr = 0;
177 h->alloc_exclude_start = 0;
Chris Mason79154b12007-03-22 15:59:16 -0400178 root->fs_info->running_transaction->use_count++;
179 mutex_unlock(&root->fs_info->trans_mutex);
180 return h;
181}
182
Chris Masonf9295742008-07-17 12:54:14 -0400183struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
184 int num_blocks)
185{
Sage Weil9ca9ee02008-08-04 10:41:27 -0400186 return start_transaction(root, num_blocks, 1);
Chris Masonf9295742008-07-17 12:54:14 -0400187}
188struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root,
189 int num_blocks)
190{
Sage Weil9ca9ee02008-08-04 10:41:27 -0400191 return start_transaction(root, num_blocks, 0);
Chris Masonf9295742008-07-17 12:54:14 -0400192}
193
Sage Weil9ca9ee02008-08-04 10:41:27 -0400194struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *r,
195 int num_blocks)
196{
197 return start_transaction(r, num_blocks, 2);
198}
199
200
Chris Mason89ce8a62008-06-25 16:01:31 -0400201static noinline int wait_for_commit(struct btrfs_root *root,
202 struct btrfs_transaction *commit)
203{
204 DEFINE_WAIT(wait);
205 mutex_lock(&root->fs_info->trans_mutex);
206 while(!commit->commit_done) {
207 prepare_to_wait(&commit->commit_wait, &wait,
208 TASK_UNINTERRUPTIBLE);
209 if (commit->commit_done)
210 break;
211 mutex_unlock(&root->fs_info->trans_mutex);
212 schedule();
213 mutex_lock(&root->fs_info->trans_mutex);
214 }
215 mutex_unlock(&root->fs_info->trans_mutex);
216 finish_wait(&commit->commit_wait, &wait);
217 return 0;
218}
219
Chris Mason37d1aee2008-07-31 10:48:37 -0400220static void throttle_on_drops(struct btrfs_root *root)
Chris Masonab78c842008-07-29 16:15:18 -0400221{
222 struct btrfs_fs_info *info = root->fs_info;
Chris Mason2dd3e672008-08-04 08:20:15 -0400223 int harder_count = 0;
Chris Masonab78c842008-07-29 16:15:18 -0400224
Chris Mason2dd3e672008-08-04 08:20:15 -0400225harder:
Chris Masonab78c842008-07-29 16:15:18 -0400226 if (atomic_read(&info->throttles)) {
227 DEFINE_WAIT(wait);
228 int thr;
Chris Masonab78c842008-07-29 16:15:18 -0400229 thr = atomic_read(&info->throttle_gen);
230
231 do {
232 prepare_to_wait(&info->transaction_throttle,
233 &wait, TASK_UNINTERRUPTIBLE);
234 if (!atomic_read(&info->throttles)) {
235 finish_wait(&info->transaction_throttle, &wait);
236 break;
237 }
238 schedule();
239 finish_wait(&info->transaction_throttle, &wait);
240 } while (thr == atomic_read(&info->throttle_gen));
Chris Mason2dd3e672008-08-04 08:20:15 -0400241 harder_count++;
242
243 if (root->fs_info->total_ref_cache_size > 1 * 1024 * 1024 &&
244 harder_count < 2)
245 goto harder;
246
247 if (root->fs_info->total_ref_cache_size > 5 * 1024 * 1024 &&
248 harder_count < 10)
249 goto harder;
250
251 if (root->fs_info->total_ref_cache_size > 10 * 1024 * 1024 &&
252 harder_count < 20)
253 goto harder;
Chris Masonab78c842008-07-29 16:15:18 -0400254 }
255}
256
Chris Mason37d1aee2008-07-31 10:48:37 -0400257void btrfs_throttle(struct btrfs_root *root)
258{
259 mutex_lock(&root->fs_info->trans_mutex);
Sage Weil9ca9ee02008-08-04 10:41:27 -0400260 if (!root->fs_info->open_ioctl_trans)
261 wait_current_trans(root);
Chris Mason37d1aee2008-07-31 10:48:37 -0400262 mutex_unlock(&root->fs_info->trans_mutex);
263
264 throttle_on_drops(root);
265}
266
Chris Mason89ce8a62008-06-25 16:01:31 -0400267static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
268 struct btrfs_root *root, int throttle)
Chris Mason79154b12007-03-22 15:59:16 -0400269{
270 struct btrfs_transaction *cur_trans;
Chris Masonab78c842008-07-29 16:15:18 -0400271 struct btrfs_fs_info *info = root->fs_info;
Chris Masond6e4a422007-04-06 15:37:36 -0400272
Chris Masonab78c842008-07-29 16:15:18 -0400273 mutex_lock(&info->trans_mutex);
274 cur_trans = info->running_transaction;
Chris Masonccd467d2007-06-28 15:57:36 -0400275 WARN_ON(cur_trans != trans->transaction);
Chris Masond5719762007-03-23 10:01:08 -0400276 WARN_ON(cur_trans->num_writers < 1);
Chris Masonccd467d2007-06-28 15:57:36 -0400277 cur_trans->num_writers--;
Chris Mason89ce8a62008-06-25 16:01:31 -0400278
Chris Mason79154b12007-03-22 15:59:16 -0400279 if (waitqueue_active(&cur_trans->writer_wait))
280 wake_up(&cur_trans->writer_wait);
Chris Mason79154b12007-03-22 15:59:16 -0400281 put_transaction(cur_trans);
Chris Masonab78c842008-07-29 16:15:18 -0400282 mutex_unlock(&info->trans_mutex);
Chris Masond6025572007-03-30 14:27:56 -0400283 memset(trans, 0, sizeof(*trans));
Chris Mason2c90e5d2007-04-02 10:50:19 -0400284 kmem_cache_free(btrfs_trans_handle_cachep, trans);
Chris Masonab78c842008-07-29 16:15:18 -0400285
286 if (throttle)
Chris Mason37d1aee2008-07-31 10:48:37 -0400287 throttle_on_drops(root);
Chris Masonab78c842008-07-29 16:15:18 -0400288
Chris Mason79154b12007-03-22 15:59:16 -0400289 return 0;
290}
291
Chris Mason89ce8a62008-06-25 16:01:31 -0400292int btrfs_end_transaction(struct btrfs_trans_handle *trans,
293 struct btrfs_root *root)
294{
295 return __btrfs_end_transaction(trans, root, 0);
296}
297
298int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
299 struct btrfs_root *root)
300{
301 return __btrfs_end_transaction(trans, root, 1);
302}
303
Chris Mason79154b12007-03-22 15:59:16 -0400304
305int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
306 struct btrfs_root *root)
307{
Chris Mason7c4452b2007-04-28 09:29:35 -0400308 int ret;
Chris Mason777e6bd2008-08-15 15:34:15 -0400309 int err = 0;
Chris Mason7c4452b2007-04-28 09:29:35 -0400310 int werr = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500311 struct extent_io_tree *dirty_pages;
Chris Mason7c4452b2007-04-28 09:29:35 -0400312 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
318 if (!trans || !trans->transaction) {
319 return filemap_write_and_wait(btree_inode->i_mapping);
320 }
321 dirty_pages = &trans->transaction->dirty_pages;
322 while(1) {
Chris Mason777e6bd2008-08-15 15:34:15 -0400323 ret = find_first_extent_bit(dirty_pages, start, &start, &end,
Chris Mason5f39d392007-10-15 16:14:19 -0400324 EXTENT_DIRTY);
325 if (ret)
Chris Mason7c4452b2007-04-28 09:29:35 -0400326 break;
Chris Mason5f39d392007-10-15 16:14:19 -0400327 while(start <= end) {
Chris Mason777e6bd2008-08-15 15:34:15 -0400328 cond_resched();
329
Chris Mason5f39d392007-10-15 16:14:19 -0400330 index = start >> PAGE_CACHE_SHIFT;
Chris Mason35ebb932007-10-30 16:56:53 -0400331 start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
Chris Mason4bef0842008-09-08 11:18:08 -0400332 page = find_get_page(btree_inode->i_mapping, index);
Chris Mason7c4452b2007-04-28 09:29:35 -0400333 if (!page)
334 continue;
Chris Mason4bef0842008-09-08 11:18:08 -0400335
336 btree_lock_page_hook(page);
337 if (!page->mapping) {
338 unlock_page(page);
339 page_cache_release(page);
340 continue;
341 }
342
Chris Mason6702ed42007-08-07 16:15:09 -0400343 if (PageWriteback(page)) {
344 if (PageDirty(page))
345 wait_on_page_writeback(page);
346 else {
347 unlock_page(page);
348 page_cache_release(page);
349 continue;
350 }
351 }
Chris Mason7c4452b2007-04-28 09:29:35 -0400352 err = write_one_page(page, 0);
353 if (err)
354 werr = err;
355 page_cache_release(page);
356 }
357 }
Chris Mason777e6bd2008-08-15 15:34:15 -0400358 while(1) {
359 ret = find_first_extent_bit(dirty_pages, 0, &start, &end,
360 EXTENT_DIRTY);
361 if (ret)
362 break;
363
364 clear_extent_dirty(dirty_pages, start, end, GFP_NOFS);
365 while(start <= end) {
366 index = start >> PAGE_CACHE_SHIFT;
367 start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
368 page = find_get_page(btree_inode->i_mapping, index);
369 if (!page)
370 continue;
371 if (PageDirty(page)) {
Chris Mason4bef0842008-09-08 11:18:08 -0400372 btree_lock_page_hook(page);
373 wait_on_page_writeback(page);
Chris Mason777e6bd2008-08-15 15:34:15 -0400374 err = write_one_page(page, 0);
375 if (err)
376 werr = err;
377 }
378 wait_on_page_writeback(page);
379 page_cache_release(page);
380 cond_resched();
381 }
382 }
Chris Mason7c4452b2007-04-28 09:29:35 -0400383 if (err)
384 werr = err;
385 return werr;
Chris Mason79154b12007-03-22 15:59:16 -0400386}
387
Chris Mason0b86a832008-03-24 15:01:56 -0400388static int update_cowonly_root(struct btrfs_trans_handle *trans,
389 struct btrfs_root *root)
390{
391 int ret;
392 u64 old_root_bytenr;
393 struct btrfs_root *tree_root = root->fs_info->tree_root;
394
395 btrfs_write_dirty_block_groups(trans, root);
396 while(1) {
397 old_root_bytenr = btrfs_root_bytenr(&root->root_item);
398 if (old_root_bytenr == root->node->start)
399 break;
400 btrfs_set_root_bytenr(&root->root_item,
401 root->node->start);
402 btrfs_set_root_level(&root->root_item,
403 btrfs_header_level(root->node));
404 ret = btrfs_update_root(trans, tree_root,
405 &root->root_key,
406 &root->root_item);
407 BUG_ON(ret);
408 btrfs_write_dirty_block_groups(trans, root);
409 }
410 return 0;
411}
412
Chris Mason79154b12007-03-22 15:59:16 -0400413int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans,
414 struct btrfs_root *root)
415{
Chris Mason79154b12007-03-22 15:59:16 -0400416 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason0b86a832008-03-24 15:01:56 -0400417 struct list_head *next;
Chris Mason79154b12007-03-22 15:59:16 -0400418
Chris Mason0b86a832008-03-24 15:01:56 -0400419 while(!list_empty(&fs_info->dirty_cowonly_roots)) {
420 next = fs_info->dirty_cowonly_roots.next;
421 list_del_init(next);
422 root = list_entry(next, struct btrfs_root, dirty_list);
423 update_cowonly_root(trans, root);
Chris Mason79154b12007-03-22 15:59:16 -0400424 }
425 return 0;
426}
427
Yan Zhengb48652c2008-08-04 23:23:47 -0400428int btrfs_add_dead_root(struct btrfs_root *root, struct btrfs_root *latest)
Chris Mason5eda7b52007-06-22 14:16:25 -0400429{
Yan Zhengf321e492008-07-30 09:26:11 -0400430 struct btrfs_dirty_root *dirty;
Chris Mason5eda7b52007-06-22 14:16:25 -0400431
432 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
433 if (!dirty)
434 return -ENOMEM;
Chris Mason5eda7b52007-06-22 14:16:25 -0400435 dirty->root = root;
Chris Mason5ce14bb2007-09-11 11:15:39 -0400436 dirty->latest_root = latest;
Yan Zhengb48652c2008-08-04 23:23:47 -0400437
438 mutex_lock(&root->fs_info->trans_mutex);
439 list_add(&dirty->list, &latest->fs_info->dead_roots);
440 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason5eda7b52007-06-22 14:16:25 -0400441 return 0;
442}
443
Chris Mason80b67942008-02-01 16:35:04 -0500444static noinline int add_dirty_roots(struct btrfs_trans_handle *trans,
445 struct radix_tree_root *radix,
446 struct list_head *list)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400447{
Yan Zhengf321e492008-07-30 09:26:11 -0400448 struct btrfs_dirty_root *dirty;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400449 struct btrfs_root *gang[8];
450 struct btrfs_root *root;
451 int i;
452 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400453 int err = 0;
Chris Mason5eda7b52007-06-22 14:16:25 -0400454 u32 refs;
Chris Mason54aa1f42007-06-22 14:16:25 -0400455
Chris Mason0f7d52f2007-04-09 10:42:37 -0400456 while(1) {
457 ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0,
458 ARRAY_SIZE(gang),
459 BTRFS_ROOT_TRANS_TAG);
460 if (ret == 0)
461 break;
462 for (i = 0; i < ret; i++) {
463 root = gang[i];
Chris Mason2619ba12007-04-10 16:58:11 -0400464 radix_tree_tag_clear(radix,
465 (unsigned long)root->root_key.objectid,
466 BTRFS_ROOT_TRANS_TAG);
Yan Zheng31153d82008-07-28 15:32:19 -0400467
468 BUG_ON(!root->ref_tree);
Chris Mason017e5362008-07-28 15:32:51 -0400469 dirty = root->dirty_root;
Yan Zheng31153d82008-07-28 15:32:19 -0400470
Chris Masone02119d2008-09-05 16:13:11 -0400471 btrfs_free_log(trans, root);
472
Chris Mason0f7d52f2007-04-09 10:42:37 -0400473 if (root->commit_root == root->node) {
Chris Masondb945352007-10-15 16:15:53 -0400474 WARN_ON(root->node->start !=
475 btrfs_root_bytenr(&root->root_item));
Yan Zheng31153d82008-07-28 15:32:19 -0400476
Chris Mason5f39d392007-10-15 16:14:19 -0400477 free_extent_buffer(root->commit_root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400478 root->commit_root = NULL;
Yan Zheng7ea394f2008-08-05 13:05:02 -0400479 root->dirty_root = NULL;
Yanbcc63ab2008-07-30 16:29:20 -0400480
481 spin_lock(&root->list_lock);
482 list_del_init(&dirty->root->dead_list);
483 spin_unlock(&root->list_lock);
484
Yan Zheng31153d82008-07-28 15:32:19 -0400485 kfree(dirty->root);
486 kfree(dirty);
Josef Bacik58176a92007-08-29 15:47:34 -0400487
488 /* make sure to update the root on disk
489 * so we get any updates to the block used
490 * counts
491 */
492 err = btrfs_update_root(trans,
493 root->fs_info->tree_root,
494 &root->root_key,
495 &root->root_item);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400496 continue;
497 }
Chris Mason9f3a7422007-08-07 15:52:19 -0400498
499 memset(&root->root_item.drop_progress, 0,
500 sizeof(struct btrfs_disk_key));
501 root->root_item.drop_level = 0;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400502 root->commit_root = NULL;
Yan Zheng7ea394f2008-08-05 13:05:02 -0400503 root->dirty_root = NULL;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400504 root->root_key.offset = root->fs_info->generation;
Chris Masondb945352007-10-15 16:15:53 -0400505 btrfs_set_root_bytenr(&root->root_item,
506 root->node->start);
507 btrfs_set_root_level(&root->root_item,
508 btrfs_header_level(root->node));
Chris Mason0f7d52f2007-04-09 10:42:37 -0400509 err = btrfs_insert_root(trans, root->fs_info->tree_root,
510 &root->root_key,
511 &root->root_item);
Chris Mason54aa1f42007-06-22 14:16:25 -0400512 if (err)
513 break;
Chris Mason9f3a7422007-08-07 15:52:19 -0400514
515 refs = btrfs_root_refs(&dirty->root->root_item);
516 btrfs_set_root_refs(&dirty->root->root_item, refs - 1);
Chris Mason5eda7b52007-06-22 14:16:25 -0400517 err = btrfs_update_root(trans, root->fs_info->tree_root,
Chris Mason9f3a7422007-08-07 15:52:19 -0400518 &dirty->root->root_key,
519 &dirty->root->root_item);
Chris Mason5eda7b52007-06-22 14:16:25 -0400520
521 BUG_ON(err);
Chris Mason9f3a7422007-08-07 15:52:19 -0400522 if (refs == 1) {
Chris Mason5eda7b52007-06-22 14:16:25 -0400523 list_add(&dirty->list, list);
Chris Mason9f3a7422007-08-07 15:52:19 -0400524 } else {
525 WARN_ON(1);
Yan Zheng31153d82008-07-28 15:32:19 -0400526 free_extent_buffer(dirty->root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -0400527 kfree(dirty->root);
Chris Mason5eda7b52007-06-22 14:16:25 -0400528 kfree(dirty);
Chris Mason9f3a7422007-08-07 15:52:19 -0400529 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400530 }
531 }
Chris Mason54aa1f42007-06-22 14:16:25 -0400532 return err;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400533}
534
Chris Masone9d0b132007-08-10 14:06:19 -0400535int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
536{
537 struct btrfs_fs_info *info = root->fs_info;
538 int ret;
539 struct btrfs_trans_handle *trans;
Chris Masond3c2fdc2007-09-17 10:58:06 -0400540 unsigned long nr;
Chris Masone9d0b132007-08-10 14:06:19 -0400541
Chris Masona2135012008-06-25 16:01:30 -0400542 smp_mb();
Chris Masone9d0b132007-08-10 14:06:19 -0400543 if (root->defrag_running)
544 return 0;
Chris Masone9d0b132007-08-10 14:06:19 -0400545 trans = btrfs_start_transaction(root, 1);
Chris Mason6b800532007-10-15 16:17:34 -0400546 while (1) {
Chris Masone9d0b132007-08-10 14:06:19 -0400547 root->defrag_running = 1;
548 ret = btrfs_defrag_leaves(trans, root, cacheonly);
Chris Masond3c2fdc2007-09-17 10:58:06 -0400549 nr = trans->blocks_used;
Chris Masone9d0b132007-08-10 14:06:19 -0400550 btrfs_end_transaction(trans, root);
Chris Masond3c2fdc2007-09-17 10:58:06 -0400551 btrfs_btree_balance_dirty(info->tree_root, nr);
Chris Masone9d0b132007-08-10 14:06:19 -0400552 cond_resched();
553
Chris Masone9d0b132007-08-10 14:06:19 -0400554 trans = btrfs_start_transaction(root, 1);
Chris Mason3f157a22008-06-25 16:01:31 -0400555 if (root->fs_info->closing || ret != -EAGAIN)
Chris Masone9d0b132007-08-10 14:06:19 -0400556 break;
557 }
558 root->defrag_running = 0;
Chris Masona2135012008-06-25 16:01:30 -0400559 smp_mb();
Chris Masone9d0b132007-08-10 14:06:19 -0400560 btrfs_end_transaction(trans, root);
561 return 0;
562}
563
Chris Mason80b67942008-02-01 16:35:04 -0500564static noinline int drop_dirty_roots(struct btrfs_root *tree_root,
565 struct list_head *list)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400566{
Yan Zhengf321e492008-07-30 09:26:11 -0400567 struct btrfs_dirty_root *dirty;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400568 struct btrfs_trans_handle *trans;
Chris Masond3c2fdc2007-09-17 10:58:06 -0400569 unsigned long nr;
Chris Masondb945352007-10-15 16:15:53 -0400570 u64 num_bytes;
571 u64 bytes_used;
Yanbcc63ab2008-07-30 16:29:20 -0400572 u64 max_useless;
Chris Mason54aa1f42007-06-22 14:16:25 -0400573 int ret = 0;
Chris Mason9f3a7422007-08-07 15:52:19 -0400574 int err;
575
Chris Mason0f7d52f2007-04-09 10:42:37 -0400576 while(!list_empty(list)) {
Josef Bacik58176a92007-08-29 15:47:34 -0400577 struct btrfs_root *root;
578
Yan Zhengf321e492008-07-30 09:26:11 -0400579 dirty = list_entry(list->prev, struct btrfs_dirty_root, list);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400580 list_del_init(&dirty->list);
Chris Mason5eda7b52007-06-22 14:16:25 -0400581
Chris Masondb945352007-10-15 16:15:53 -0400582 num_bytes = btrfs_root_used(&dirty->root->root_item);
Josef Bacik58176a92007-08-29 15:47:34 -0400583 root = dirty->latest_root;
Chris Masona2135012008-06-25 16:01:30 -0400584 atomic_inc(&root->fs_info->throttles);
Josef Bacik58176a92007-08-29 15:47:34 -0400585
Chris Masona2135012008-06-25 16:01:30 -0400586 mutex_lock(&root->fs_info->drop_mutex);
Chris Mason9f3a7422007-08-07 15:52:19 -0400587 while(1) {
588 trans = btrfs_start_transaction(tree_root, 1);
589 ret = btrfs_drop_snapshot(trans, dirty->root);
590 if (ret != -EAGAIN) {
591 break;
592 }
Josef Bacik58176a92007-08-29 15:47:34 -0400593
Chris Mason9f3a7422007-08-07 15:52:19 -0400594 err = btrfs_update_root(trans,
595 tree_root,
596 &dirty->root->root_key,
597 &dirty->root->root_item);
598 if (err)
599 ret = err;
Chris Masond3c2fdc2007-09-17 10:58:06 -0400600 nr = trans->blocks_used;
Chris Mason017e5362008-07-28 15:32:51 -0400601 ret = btrfs_end_transaction(trans, tree_root);
Chris Mason9f3a7422007-08-07 15:52:19 -0400602 BUG_ON(ret);
Chris Masona2135012008-06-25 16:01:30 -0400603
604 mutex_unlock(&root->fs_info->drop_mutex);
Chris Masond3c2fdc2007-09-17 10:58:06 -0400605 btrfs_btree_balance_dirty(tree_root, nr);
Chris Mason4dc119042007-10-15 16:18:14 -0400606 cond_resched();
Chris Masona2135012008-06-25 16:01:30 -0400607 mutex_lock(&root->fs_info->drop_mutex);
Chris Mason9f3a7422007-08-07 15:52:19 -0400608 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400609 BUG_ON(ret);
Chris Masona2135012008-06-25 16:01:30 -0400610 atomic_dec(&root->fs_info->throttles);
Chris Mason017e5362008-07-28 15:32:51 -0400611 wake_up(&root->fs_info->transaction_throttle);
Josef Bacik58176a92007-08-29 15:47:34 -0400612
Chris Masona2135012008-06-25 16:01:30 -0400613 mutex_lock(&root->fs_info->alloc_mutex);
Chris Masondb945352007-10-15 16:15:53 -0400614 num_bytes -= btrfs_root_used(&dirty->root->root_item);
615 bytes_used = btrfs_root_used(&root->root_item);
616 if (num_bytes) {
Chris Masone02119d2008-09-05 16:13:11 -0400617 btrfs_record_root_in_trans(root);
Chris Mason5f39d392007-10-15 16:14:19 -0400618 btrfs_set_root_used(&root->root_item,
Chris Masondb945352007-10-15 16:15:53 -0400619 bytes_used - num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -0400620 }
Chris Masona2135012008-06-25 16:01:30 -0400621 mutex_unlock(&root->fs_info->alloc_mutex);
622
Chris Mason9f3a7422007-08-07 15:52:19 -0400623 ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key);
Josef Bacik58176a92007-08-29 15:47:34 -0400624 if (ret) {
625 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -0400626 break;
Josef Bacik58176a92007-08-29 15:47:34 -0400627 }
Chris Masona2135012008-06-25 16:01:30 -0400628 mutex_unlock(&root->fs_info->drop_mutex);
629
Yanbcc63ab2008-07-30 16:29:20 -0400630 spin_lock(&root->list_lock);
631 list_del_init(&dirty->root->dead_list);
632 if (!list_empty(&root->dead_list)) {
633 struct btrfs_root *oldest;
634 oldest = list_entry(root->dead_list.prev,
635 struct btrfs_root, dead_list);
636 max_useless = oldest->root_key.offset - 1;
637 } else {
638 max_useless = root->root_key.offset - 1;
639 }
640 spin_unlock(&root->list_lock);
641
Chris Masond3c2fdc2007-09-17 10:58:06 -0400642 nr = trans->blocks_used;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400643 ret = btrfs_end_transaction(trans, tree_root);
644 BUG_ON(ret);
Chris Mason5eda7b52007-06-22 14:16:25 -0400645
Yanbcc63ab2008-07-30 16:29:20 -0400646 ret = btrfs_remove_leaf_refs(root, max_useless);
647 BUG_ON(ret);
648
Chris Masonf510cfe2007-10-15 16:14:48 -0400649 free_extent_buffer(dirty->root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -0400650 kfree(dirty->root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400651 kfree(dirty);
Chris Masond3c2fdc2007-09-17 10:58:06 -0400652
653 btrfs_btree_balance_dirty(tree_root, nr);
Chris Mason4dc119042007-10-15 16:18:14 -0400654 cond_resched();
Chris Mason0f7d52f2007-04-09 10:42:37 -0400655 }
Chris Mason54aa1f42007-06-22 14:16:25 -0400656 return ret;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400657}
658
Chris Mason80b67942008-02-01 16:35:04 -0500659static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
Chris Mason3063d292008-01-08 15:46:30 -0500660 struct btrfs_fs_info *fs_info,
661 struct btrfs_pending_snapshot *pending)
662{
663 struct btrfs_key key;
Chris Mason80b67942008-02-01 16:35:04 -0500664 struct btrfs_root_item *new_root_item;
Chris Mason3063d292008-01-08 15:46:30 -0500665 struct btrfs_root *tree_root = fs_info->tree_root;
666 struct btrfs_root *root = pending->root;
667 struct extent_buffer *tmp;
Chris Mason925baed2008-06-25 16:01:30 -0400668 struct extent_buffer *old;
Chris Mason3063d292008-01-08 15:46:30 -0500669 int ret;
Sven Wegener3b963622008-06-09 21:57:42 -0400670 int namelen;
Chris Mason3063d292008-01-08 15:46:30 -0500671 u64 objectid;
672
Chris Mason80b67942008-02-01 16:35:04 -0500673 new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
674 if (!new_root_item) {
675 ret = -ENOMEM;
676 goto fail;
677 }
Chris Mason3063d292008-01-08 15:46:30 -0500678 ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid);
679 if (ret)
680 goto fail;
681
Chris Mason80b67942008-02-01 16:35:04 -0500682 memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
Chris Mason3063d292008-01-08 15:46:30 -0500683
684 key.objectid = objectid;
685 key.offset = 1;
686 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
687
Chris Mason925baed2008-06-25 16:01:30 -0400688 old = btrfs_lock_root_node(root);
Chris Mason65b51a02008-08-01 15:11:20 -0400689 btrfs_cow_block(trans, root, old, NULL, 0, &old, 0);
Chris Mason3063d292008-01-08 15:46:30 -0500690
Chris Mason925baed2008-06-25 16:01:30 -0400691 btrfs_copy_root(trans, root, old, &tmp, objectid);
692 btrfs_tree_unlock(old);
693 free_extent_buffer(old);
Chris Mason3063d292008-01-08 15:46:30 -0500694
Chris Mason80b67942008-02-01 16:35:04 -0500695 btrfs_set_root_bytenr(new_root_item, tmp->start);
696 btrfs_set_root_level(new_root_item, btrfs_header_level(tmp));
Chris Mason3063d292008-01-08 15:46:30 -0500697 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
Chris Mason80b67942008-02-01 16:35:04 -0500698 new_root_item);
Chris Mason925baed2008-06-25 16:01:30 -0400699 btrfs_tree_unlock(tmp);
Chris Mason3063d292008-01-08 15:46:30 -0500700 free_extent_buffer(tmp);
701 if (ret)
702 goto fail;
703
704 /*
705 * insert the directory item
706 */
707 key.offset = (u64)-1;
Sven Wegener3b963622008-06-09 21:57:42 -0400708 namelen = strlen(pending->name);
Chris Mason3063d292008-01-08 15:46:30 -0500709 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
Sven Wegener3b963622008-06-09 21:57:42 -0400710 pending->name, namelen,
Chris Mason3063d292008-01-08 15:46:30 -0500711 root->fs_info->sb->s_root->d_inode->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -0400712 &key, BTRFS_FT_DIR, 0);
Chris Mason3063d292008-01-08 15:46:30 -0500713
714 if (ret)
715 goto fail;
716
717 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
718 pending->name, strlen(pending->name), objectid,
Josef Bacikaec74772008-07-24 12:12:38 -0400719 root->fs_info->sb->s_root->d_inode->i_ino, 0);
Sven Wegener3b963622008-06-09 21:57:42 -0400720
721 /* Invalidate existing dcache entry for new snapshot. */
722 btrfs_invalidate_dcache_root(root, pending->name, namelen);
723
Chris Mason3063d292008-01-08 15:46:30 -0500724fail:
Chris Mason80b67942008-02-01 16:35:04 -0500725 kfree(new_root_item);
Chris Mason3063d292008-01-08 15:46:30 -0500726 return ret;
727}
728
Chris Mason80b67942008-02-01 16:35:04 -0500729static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
730 struct btrfs_fs_info *fs_info)
Chris Mason3063d292008-01-08 15:46:30 -0500731{
732 struct btrfs_pending_snapshot *pending;
733 struct list_head *head = &trans->transaction->pending_snapshots;
734 int ret;
735
736 while(!list_empty(head)) {
737 pending = list_entry(head->next,
738 struct btrfs_pending_snapshot, list);
739 ret = create_pending_snapshot(trans, fs_info, pending);
740 BUG_ON(ret);
741 list_del(&pending->list);
742 kfree(pending->name);
743 kfree(pending);
744 }
Chris Masondc17ff82008-01-08 15:46:30 -0500745 return 0;
746}
747
Chris Mason79154b12007-03-22 15:59:16 -0400748int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
749 struct btrfs_root *root)
750{
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400751 unsigned long joined = 0;
752 unsigned long timeout = 1;
Chris Mason79154b12007-03-22 15:59:16 -0400753 struct btrfs_transaction *cur_trans;
Chris Mason8fd17792007-04-19 21:01:03 -0400754 struct btrfs_transaction *prev_trans = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -0400755 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400756 struct list_head dirty_fs_roots;
Chris Masond1310b22008-01-24 16:13:08 -0500757 struct extent_io_tree *pinned_copy;
Chris Mason79154b12007-03-22 15:59:16 -0400758 DEFINE_WAIT(wait);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400759 int ret;
Chris Mason79154b12007-03-22 15:59:16 -0400760
Chris Mason0f7d52f2007-04-09 10:42:37 -0400761 INIT_LIST_HEAD(&dirty_fs_roots);
Chris Mason79154b12007-03-22 15:59:16 -0400762 mutex_lock(&root->fs_info->trans_mutex);
763 if (trans->transaction->in_commit) {
764 cur_trans = trans->transaction;
765 trans->transaction->use_count++;
Chris Masonccd467d2007-06-28 15:57:36 -0400766 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400767 btrfs_end_transaction(trans, root);
Chris Masonccd467d2007-06-28 15:57:36 -0400768
Chris Mason79154b12007-03-22 15:59:16 -0400769 ret = wait_for_commit(root, cur_trans);
770 BUG_ON(ret);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400771
772 mutex_lock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400773 put_transaction(cur_trans);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400774 mutex_unlock(&root->fs_info->trans_mutex);
775
Chris Mason79154b12007-03-22 15:59:16 -0400776 return 0;
777 }
Chris Mason4313b392008-01-03 09:08:48 -0500778
779 pinned_copy = kmalloc(sizeof(*pinned_copy), GFP_NOFS);
780 if (!pinned_copy)
781 return -ENOMEM;
782
Chris Masond1310b22008-01-24 16:13:08 -0500783 extent_io_tree_init(pinned_copy,
Chris Mason4313b392008-01-03 09:08:48 -0500784 root->fs_info->btree_inode->i_mapping, GFP_NOFS);
785
Chris Mason2c90e5d2007-04-02 10:50:19 -0400786 trans->transaction->in_commit = 1;
Chris Masonf9295742008-07-17 12:54:14 -0400787 trans->transaction->blocked = 1;
Chris Masonccd467d2007-06-28 15:57:36 -0400788 cur_trans = trans->transaction;
789 if (cur_trans->list.prev != &root->fs_info->trans_list) {
790 prev_trans = list_entry(cur_trans->list.prev,
791 struct btrfs_transaction, list);
792 if (!prev_trans->commit_done) {
793 prev_trans->use_count++;
Chris Masonccd467d2007-06-28 15:57:36 -0400794 mutex_unlock(&root->fs_info->trans_mutex);
795
796 wait_for_commit(root, prev_trans);
Chris Masonccd467d2007-06-28 15:57:36 -0400797
Chris Masonccd467d2007-06-28 15:57:36 -0400798 mutex_lock(&root->fs_info->trans_mutex);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400799 put_transaction(prev_trans);
Chris Masonccd467d2007-06-28 15:57:36 -0400800 }
801 }
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400802
803 do {
Yan Zheng7ea394f2008-08-05 13:05:02 -0400804 int snap_pending = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400805 joined = cur_trans->num_joined;
Yan Zheng7ea394f2008-08-05 13:05:02 -0400806 if (!list_empty(&trans->transaction->pending_snapshots))
807 snap_pending = 1;
808
Chris Mason2c90e5d2007-04-02 10:50:19 -0400809 WARN_ON(cur_trans != trans->transaction);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400810 prepare_to_wait(&cur_trans->writer_wait, &wait,
Chris Mason79154b12007-03-22 15:59:16 -0400811 TASK_UNINTERRUPTIBLE);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400812
813 if (cur_trans->num_writers > 1)
814 timeout = MAX_SCHEDULE_TIMEOUT;
815 else
816 timeout = 1;
817
Chris Mason79154b12007-03-22 15:59:16 -0400818 mutex_unlock(&root->fs_info->trans_mutex);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400819
Yan Zheng7ea394f2008-08-05 13:05:02 -0400820 if (snap_pending) {
821 ret = btrfs_wait_ordered_extents(root, 1);
822 BUG_ON(ret);
823 }
824
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400825 schedule_timeout(timeout);
826
Chris Mason79154b12007-03-22 15:59:16 -0400827 mutex_lock(&root->fs_info->trans_mutex);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400828 finish_wait(&cur_trans->writer_wait, &wait);
829 } while (cur_trans->num_writers > 1 ||
830 (cur_trans->num_joined != joined));
831
Chris Mason3063d292008-01-08 15:46:30 -0500832 ret = create_pending_snapshots(trans, root->fs_info);
833 BUG_ON(ret);
834
Chris Mason2c90e5d2007-04-02 10:50:19 -0400835 WARN_ON(cur_trans != trans->transaction);
Chris Masondc17ff82008-01-08 15:46:30 -0500836
Chris Masone02119d2008-09-05 16:13:11 -0400837 /* btrfs_commit_tree_roots is responsible for getting the
838 * various roots consistent with each other. Every pointer
839 * in the tree of tree roots has to point to the most up to date
840 * root for every subvolume and other tree. So, we have to keep
841 * the tree logging code from jumping in and changing any
842 * of the trees.
843 *
844 * At this point in the commit, there can't be any tree-log
845 * writers, but a little lower down we drop the trans mutex
846 * and let new people in. By holding the tree_log_mutex
847 * from now until after the super is written, we avoid races
848 * with the tree-log code.
849 */
850 mutex_lock(&root->fs_info->tree_log_mutex);
851
Chris Mason54aa1f42007-06-22 14:16:25 -0400852 ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix,
853 &dirty_fs_roots);
854 BUG_ON(ret);
855
Chris Masone02119d2008-09-05 16:13:11 -0400856 /* add_dirty_roots gets rid of all the tree log roots, it is now
857 * safe to free the root of tree log roots
858 */
859 btrfs_free_log_root_tree(trans, root->fs_info);
860
Chris Mason79154b12007-03-22 15:59:16 -0400861 ret = btrfs_commit_tree_roots(trans, root);
862 BUG_ON(ret);
Chris Mason54aa1f42007-06-22 14:16:25 -0400863
Chris Mason78fae272007-03-25 11:35:08 -0400864 cur_trans = root->fs_info->running_transaction;
Chris Masoncee36a02008-01-15 08:40:48 -0500865 spin_lock(&root->fs_info->new_trans_lock);
Chris Mason78fae272007-03-25 11:35:08 -0400866 root->fs_info->running_transaction = NULL;
Chris Masoncee36a02008-01-15 08:40:48 -0500867 spin_unlock(&root->fs_info->new_trans_lock);
Chris Mason4b52dff2007-06-26 10:06:50 -0400868 btrfs_set_super_generation(&root->fs_info->super_copy,
869 cur_trans->transid);
870 btrfs_set_super_root(&root->fs_info->super_copy,
Chris Masondb945352007-10-15 16:15:53 -0400871 root->fs_info->tree_root->node->start);
872 btrfs_set_super_root_level(&root->fs_info->super_copy,
873 btrfs_header_level(root->fs_info->tree_root->node));
Chris Mason5f39d392007-10-15 16:14:19 -0400874
Chris Mason0b86a832008-03-24 15:01:56 -0400875 btrfs_set_super_chunk_root(&root->fs_info->super_copy,
876 chunk_root->node->start);
877 btrfs_set_super_chunk_root_level(&root->fs_info->super_copy,
878 btrfs_header_level(chunk_root->node));
Chris Masone02119d2008-09-05 16:13:11 -0400879
880 if (!root->fs_info->log_root_recovering) {
881 btrfs_set_super_log_root(&root->fs_info->super_copy, 0);
882 btrfs_set_super_log_root_level(&root->fs_info->super_copy, 0);
883 }
884
Chris Masona061fc82008-05-07 11:43:44 -0400885 memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy,
886 sizeof(root->fs_info->super_copy));
Chris Masonccd467d2007-06-28 15:57:36 -0400887
Chris Mason4313b392008-01-03 09:08:48 -0500888 btrfs_copy_pinned(root, pinned_copy);
Chris Masonccd467d2007-06-28 15:57:36 -0400889
Chris Masonf9295742008-07-17 12:54:14 -0400890 trans->transaction->blocked = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400891 wake_up(&root->fs_info->transaction_throttle);
Chris Masonf9295742008-07-17 12:54:14 -0400892 wake_up(&root->fs_info->transaction_wait);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400893
Chris Mason78fae272007-03-25 11:35:08 -0400894 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400895 ret = btrfs_write_and_wait_transaction(trans, root);
896 BUG_ON(ret);
Chris Mason79154b12007-03-22 15:59:16 -0400897 write_ctree_super(trans, root);
Chris Mason4313b392008-01-03 09:08:48 -0500898
Chris Masone02119d2008-09-05 16:13:11 -0400899 /*
900 * the super is written, we can safely allow the tree-loggers
901 * to go about their business
902 */
903 mutex_unlock(&root->fs_info->tree_log_mutex);
904
Chris Mason4313b392008-01-03 09:08:48 -0500905 btrfs_finish_extent_commit(trans, root, pinned_copy);
Chris Mason78fae272007-03-25 11:35:08 -0400906 mutex_lock(&root->fs_info->trans_mutex);
Chris Mason4313b392008-01-03 09:08:48 -0500907
908 kfree(pinned_copy);
909
Chris Mason2c90e5d2007-04-02 10:50:19 -0400910 cur_trans->commit_done = 1;
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400911 root->fs_info->last_trans_committed = cur_trans->transid;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400912 wake_up(&cur_trans->commit_wait);
Chris Mason79154b12007-03-22 15:59:16 -0400913 put_transaction(cur_trans);
Chris Mason78fae272007-03-25 11:35:08 -0400914 put_transaction(cur_trans);
Josef Bacik58176a92007-08-29 15:47:34 -0400915
Yanbcc63ab2008-07-30 16:29:20 -0400916 list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots);
Chris Masonfacda1e2007-06-08 18:11:48 -0400917 if (root->fs_info->closing)
918 list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots);
Josef Bacik58176a92007-08-29 15:47:34 -0400919
Chris Mason78fae272007-03-25 11:35:08 -0400920 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400921 kmem_cache_free(btrfs_trans_handle_cachep, trans);
Chris Mason79154b12007-03-22 15:59:16 -0400922
Chris Masonfacda1e2007-06-08 18:11:48 -0400923 if (root->fs_info->closing) {
Chris Masonfacda1e2007-06-08 18:11:48 -0400924 drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);
Chris Masonfacda1e2007-06-08 18:11:48 -0400925 }
Chris Mason79154b12007-03-22 15:59:16 -0400926 return ret;
927}
928
Chris Masone9d0b132007-08-10 14:06:19 -0400929int btrfs_clean_old_snapshots(struct btrfs_root *root)
930{
931 struct list_head dirty_roots;
932 INIT_LIST_HEAD(&dirty_roots);
Chris Masona74a4b92008-06-25 16:01:31 -0400933again:
Chris Masone9d0b132007-08-10 14:06:19 -0400934 mutex_lock(&root->fs_info->trans_mutex);
935 list_splice_init(&root->fs_info->dead_roots, &dirty_roots);
936 mutex_unlock(&root->fs_info->trans_mutex);
937
938 if (!list_empty(&dirty_roots)) {
939 drop_dirty_roots(root, &dirty_roots);
Chris Masona74a4b92008-06-25 16:01:31 -0400940 goto again;
Chris Masone9d0b132007-08-10 14:06:19 -0400941 }
942 return 0;
943}