blob: 664782c6a2dfbf6e65628f09fe2ff8552335f016 [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 Mason5f2cc082008-11-07 18:22:45 -050023#include <linux/blkdev.h>
Chris Mason79154b12007-03-22 15:59:16 -040024#include "ctree.h"
25#include "disk-io.h"
26#include "transaction.h"
Chris Mason925baed2008-06-25 16:01:30 -040027#include "locking.h"
Yan Zheng31153d82008-07-28 15:32:19 -040028#include "ref-cache.h"
Chris Masone02119d2008-09-05 16:13:11 -040029#include "tree-log.h"
Chris Mason79154b12007-03-22 15:59:16 -040030
Chris Mason0f7d52f2007-04-09 10:42:37 -040031#define BTRFS_ROOT_TRANS_TAG 0
32
Chris Mason80b67942008-02-01 16:35:04 -050033static noinline void put_transaction(struct btrfs_transaction *transaction)
Chris Mason79154b12007-03-22 15:59:16 -040034{
Chris Mason2c90e5d2007-04-02 10:50:19 -040035 WARN_ON(transaction->use_count == 0);
Chris Mason79154b12007-03-22 15:59:16 -040036 transaction->use_count--;
Chris Mason78fae272007-03-25 11:35:08 -040037 if (transaction->use_count == 0) {
Chris Mason8fd17792007-04-19 21:01:03 -040038 list_del_init(&transaction->list);
Chris Mason2c90e5d2007-04-02 10:50:19 -040039 memset(transaction, 0, sizeof(*transaction));
40 kmem_cache_free(btrfs_transaction_cachep, transaction);
Chris Mason78fae272007-03-25 11:35:08 -040041 }
Chris Mason79154b12007-03-22 15:59:16 -040042}
43
Chris Masond352ac62008-09-29 15:18:18 -040044/*
45 * either allocate a new transaction or hop into the existing one
46 */
Chris Mason80b67942008-02-01 16:35:04 -050047static noinline int join_transaction(struct btrfs_root *root)
Chris Mason79154b12007-03-22 15:59:16 -040048{
49 struct btrfs_transaction *cur_trans;
50 cur_trans = root->fs_info->running_transaction;
51 if (!cur_trans) {
Chris Mason2c90e5d2007-04-02 10:50:19 -040052 cur_trans = kmem_cache_alloc(btrfs_transaction_cachep,
53 GFP_NOFS);
Chris Mason79154b12007-03-22 15:59:16 -040054 BUG_ON(!cur_trans);
Chris Mason0f7d52f2007-04-09 10:42:37 -040055 root->fs_info->generation++;
Chris Masone18e4802008-01-18 10:54:22 -050056 root->fs_info->last_alloc = 0;
Chris Mason4529ba42008-01-31 16:45:07 -050057 root->fs_info->last_data_alloc = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -040058 cur_trans->num_writers = 1;
59 cur_trans->num_joined = 0;
Chris Mason0f7d52f2007-04-09 10:42:37 -040060 cur_trans->transid = root->fs_info->generation;
Chris Mason79154b12007-03-22 15:59:16 -040061 init_waitqueue_head(&cur_trans->writer_wait);
62 init_waitqueue_head(&cur_trans->commit_wait);
63 cur_trans->in_commit = 0;
Chris Masonf9295742008-07-17 12:54:14 -040064 cur_trans->blocked = 0;
Chris Masond5719762007-03-23 10:01:08 -040065 cur_trans->use_count = 1;
Chris Mason79154b12007-03-22 15:59:16 -040066 cur_trans->commit_done = 0;
Chris Mason08607c12007-06-08 15:33:54 -040067 cur_trans->start_time = get_seconds();
Chris Mason56bec292009-03-13 10:10:06 -040068
69 cur_trans->delayed_refs.root.rb_node = NULL;
70 cur_trans->delayed_refs.num_entries = 0;
Chris Masonc3e69d52009-03-13 10:17:05 -040071 cur_trans->delayed_refs.num_heads_ready = 0;
72 cur_trans->delayed_refs.num_heads = 0;
Chris Mason56bec292009-03-13 10:10:06 -040073 cur_trans->delayed_refs.flushing = 0;
Chris Masonc3e69d52009-03-13 10:17:05 -040074 cur_trans->delayed_refs.run_delayed_start = 0;
Chris Mason56bec292009-03-13 10:10:06 -040075 spin_lock_init(&cur_trans->delayed_refs.lock);
76
Chris Mason3063d292008-01-08 15:46:30 -050077 INIT_LIST_HEAD(&cur_trans->pending_snapshots);
Chris Mason8fd17792007-04-19 21:01:03 -040078 list_add_tail(&cur_trans->list, &root->fs_info->trans_list);
Chris Masond1310b22008-01-24 16:13:08 -050079 extent_io_tree_init(&cur_trans->dirty_pages,
Chris Mason5f39d392007-10-15 16:14:19 -040080 root->fs_info->btree_inode->i_mapping,
81 GFP_NOFS);
Chris Mason48ec2cf2008-06-09 09:35:50 -040082 spin_lock(&root->fs_info->new_trans_lock);
83 root->fs_info->running_transaction = cur_trans;
84 spin_unlock(&root->fs_info->new_trans_lock);
Josef Bacik15ee9bc2007-08-10 16:22:09 -040085 } else {
86 cur_trans->num_writers++;
87 cur_trans->num_joined++;
Chris Mason79154b12007-03-22 15:59:16 -040088 }
Josef Bacik15ee9bc2007-08-10 16:22:09 -040089
Chris Mason79154b12007-03-22 15:59:16 -040090 return 0;
91}
92
Chris Masond352ac62008-09-29 15:18:18 -040093/*
Chris Masond3977122009-01-05 21:25:51 -050094 * this does all the record keeping required to make sure that a reference
95 * counted root is properly recorded in a given transaction. This is required
96 * to make sure the old root from before we joined the transaction is deleted
97 * when the transaction commits
Chris Masond352ac62008-09-29 15:18:18 -040098 */
Chris Masone02119d2008-09-05 16:13:11 -040099noinline int btrfs_record_root_in_trans(struct btrfs_root *root)
Chris Mason6702ed42007-08-07 16:15:09 -0400100{
Yan Zhengf321e492008-07-30 09:26:11 -0400101 struct btrfs_dirty_root *dirty;
Chris Mason6702ed42007-08-07 16:15:09 -0400102 u64 running_trans_id = root->fs_info->running_transaction->transid;
103 if (root->ref_cows && root->last_trans < running_trans_id) {
104 WARN_ON(root == root->fs_info->extent_root);
105 if (root->root_item.refs != 0) {
106 radix_tree_tag_set(&root->fs_info->fs_roots_radix,
107 (unsigned long)root->root_key.objectid,
108 BTRFS_ROOT_TRANS_TAG);
Yan Zheng31153d82008-07-28 15:32:19 -0400109
110 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
111 BUG_ON(!dirty);
112 dirty->root = kmalloc(sizeof(*dirty->root), GFP_NOFS);
113 BUG_ON(!dirty->root);
Yan Zheng31153d82008-07-28 15:32:19 -0400114 dirty->latest_root = root;
115 INIT_LIST_HEAD(&dirty->list);
Yan Zheng31153d82008-07-28 15:32:19 -0400116
Chris Mason925baed2008-06-25 16:01:30 -0400117 root->commit_root = btrfs_root_node(root);
Yan Zheng31153d82008-07-28 15:32:19 -0400118
119 memcpy(dirty->root, root, sizeof(*root));
120 spin_lock_init(&dirty->root->node_lock);
Yanbcc63ab2008-07-30 16:29:20 -0400121 spin_lock_init(&dirty->root->list_lock);
Yan Zheng31153d82008-07-28 15:32:19 -0400122 mutex_init(&dirty->root->objectid_mutex);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400123 mutex_init(&dirty->root->log_mutex);
Yanbcc63ab2008-07-30 16:29:20 -0400124 INIT_LIST_HEAD(&dirty->root->dead_list);
Yan Zheng31153d82008-07-28 15:32:19 -0400125 dirty->root->node = root->commit_root;
126 dirty->root->commit_root = NULL;
Yanbcc63ab2008-07-30 16:29:20 -0400127
128 spin_lock(&root->list_lock);
129 list_add(&dirty->root->dead_list, &root->dead_list);
130 spin_unlock(&root->list_lock);
131
132 root->dirty_root = dirty;
Chris Mason6702ed42007-08-07 16:15:09 -0400133 } else {
134 WARN_ON(1);
135 }
136 root->last_trans = running_trans_id;
137 }
138 return 0;
139}
140
Chris Masond352ac62008-09-29 15:18:18 -0400141/* wait for commit against the current transaction to become unblocked
142 * when this is done, it is safe to start a new transaction, but the current
143 * transaction might not be fully on disk.
144 */
Chris Mason37d1aee2008-07-31 10:48:37 -0400145static void wait_current_trans(struct btrfs_root *root)
Chris Mason79154b12007-03-22 15:59:16 -0400146{
Chris Masonf9295742008-07-17 12:54:14 -0400147 struct btrfs_transaction *cur_trans;
Chris Mason79154b12007-03-22 15:59:16 -0400148
Chris Masonf9295742008-07-17 12:54:14 -0400149 cur_trans = root->fs_info->running_transaction;
Chris Mason37d1aee2008-07-31 10:48:37 -0400150 if (cur_trans && cur_trans->blocked) {
Chris Masonf9295742008-07-17 12:54:14 -0400151 DEFINE_WAIT(wait);
152 cur_trans->use_count++;
Chris Masond3977122009-01-05 21:25:51 -0500153 while (1) {
Chris Masonf9295742008-07-17 12:54:14 -0400154 prepare_to_wait(&root->fs_info->transaction_wait, &wait,
155 TASK_UNINTERRUPTIBLE);
156 if (cur_trans->blocked) {
157 mutex_unlock(&root->fs_info->trans_mutex);
158 schedule();
159 mutex_lock(&root->fs_info->trans_mutex);
160 finish_wait(&root->fs_info->transaction_wait,
161 &wait);
162 } else {
163 finish_wait(&root->fs_info->transaction_wait,
164 &wait);
165 break;
166 }
167 }
168 put_transaction(cur_trans);
169 }
Chris Mason37d1aee2008-07-31 10:48:37 -0400170}
171
Chris Masone02119d2008-09-05 16:13:11 -0400172static struct btrfs_trans_handle *start_transaction(struct btrfs_root *root,
Sage Weil9ca9ee02008-08-04 10:41:27 -0400173 int num_blocks, int wait)
Chris Mason37d1aee2008-07-31 10:48:37 -0400174{
175 struct btrfs_trans_handle *h =
176 kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
177 int ret;
178
179 mutex_lock(&root->fs_info->trans_mutex);
Chris Mason4bef0842008-09-08 11:18:08 -0400180 if (!root->fs_info->log_root_recovering &&
181 ((wait == 1 && !root->fs_info->open_ioctl_trans) || wait == 2))
Chris Mason37d1aee2008-07-31 10:48:37 -0400182 wait_current_trans(root);
Chris Mason79154b12007-03-22 15:59:16 -0400183 ret = join_transaction(root);
184 BUG_ON(ret);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400185
Chris Masone02119d2008-09-05 16:13:11 -0400186 btrfs_record_root_in_trans(root);
Chris Mason6702ed42007-08-07 16:15:09 -0400187 h->transid = root->fs_info->running_transaction->transid;
Chris Mason79154b12007-03-22 15:59:16 -0400188 h->transaction = root->fs_info->running_transaction;
189 h->blocks_reserved = num_blocks;
190 h->blocks_used = 0;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500191 h->block_group = 0;
Chris Mason26b80032007-08-08 20:17:12 -0400192 h->alloc_exclude_nr = 0;
193 h->alloc_exclude_start = 0;
Chris Mason56bec292009-03-13 10:10:06 -0400194 h->delayed_ref_updates = 0;
Chris Masonb7ec40d2009-03-12 20:12:45 -0400195
Chris Mason79154b12007-03-22 15:59:16 -0400196 root->fs_info->running_transaction->use_count++;
197 mutex_unlock(&root->fs_info->trans_mutex);
198 return h;
199}
200
Chris Masonf9295742008-07-17 12:54:14 -0400201struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
202 int num_blocks)
203{
Sage Weil9ca9ee02008-08-04 10:41:27 -0400204 return start_transaction(root, num_blocks, 1);
Chris Masonf9295742008-07-17 12:54:14 -0400205}
206struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root,
207 int num_blocks)
208{
Sage Weil9ca9ee02008-08-04 10:41:27 -0400209 return start_transaction(root, num_blocks, 0);
Chris Masonf9295742008-07-17 12:54:14 -0400210}
211
Sage Weil9ca9ee02008-08-04 10:41:27 -0400212struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *r,
213 int num_blocks)
214{
215 return start_transaction(r, num_blocks, 2);
216}
217
Chris Masond352ac62008-09-29 15:18:18 -0400218/* wait for a transaction commit to be fully complete */
Chris Mason89ce8a62008-06-25 16:01:31 -0400219static noinline int wait_for_commit(struct btrfs_root *root,
220 struct btrfs_transaction *commit)
221{
222 DEFINE_WAIT(wait);
223 mutex_lock(&root->fs_info->trans_mutex);
Chris Masond3977122009-01-05 21:25:51 -0500224 while (!commit->commit_done) {
Chris Mason89ce8a62008-06-25 16:01:31 -0400225 prepare_to_wait(&commit->commit_wait, &wait,
226 TASK_UNINTERRUPTIBLE);
227 if (commit->commit_done)
228 break;
229 mutex_unlock(&root->fs_info->trans_mutex);
230 schedule();
231 mutex_lock(&root->fs_info->trans_mutex);
232 }
233 mutex_unlock(&root->fs_info->trans_mutex);
234 finish_wait(&commit->commit_wait, &wait);
235 return 0;
236}
237
Chris Masond352ac62008-09-29 15:18:18 -0400238/*
Chris Masond3977122009-01-05 21:25:51 -0500239 * rate limit against the drop_snapshot code. This helps to slow down new
240 * operations if the drop_snapshot code isn't able to keep up.
Chris Masond352ac62008-09-29 15:18:18 -0400241 */
Chris Mason37d1aee2008-07-31 10:48:37 -0400242static void throttle_on_drops(struct btrfs_root *root)
Chris Masonab78c842008-07-29 16:15:18 -0400243{
244 struct btrfs_fs_info *info = root->fs_info;
Chris Mason2dd3e672008-08-04 08:20:15 -0400245 int harder_count = 0;
Chris Masonab78c842008-07-29 16:15:18 -0400246
Chris Mason2dd3e672008-08-04 08:20:15 -0400247harder:
Chris Masonab78c842008-07-29 16:15:18 -0400248 if (atomic_read(&info->throttles)) {
249 DEFINE_WAIT(wait);
250 int thr;
Chris Masonab78c842008-07-29 16:15:18 -0400251 thr = atomic_read(&info->throttle_gen);
252
253 do {
254 prepare_to_wait(&info->transaction_throttle,
255 &wait, TASK_UNINTERRUPTIBLE);
256 if (!atomic_read(&info->throttles)) {
257 finish_wait(&info->transaction_throttle, &wait);
258 break;
259 }
260 schedule();
261 finish_wait(&info->transaction_throttle, &wait);
262 } while (thr == atomic_read(&info->throttle_gen));
Chris Mason2dd3e672008-08-04 08:20:15 -0400263 harder_count++;
264
265 if (root->fs_info->total_ref_cache_size > 1 * 1024 * 1024 &&
266 harder_count < 2)
267 goto harder;
268
269 if (root->fs_info->total_ref_cache_size > 5 * 1024 * 1024 &&
270 harder_count < 10)
271 goto harder;
272
273 if (root->fs_info->total_ref_cache_size > 10 * 1024 * 1024 &&
274 harder_count < 20)
275 goto harder;
Chris Masonab78c842008-07-29 16:15:18 -0400276 }
277}
278
Chris Mason37d1aee2008-07-31 10:48:37 -0400279void btrfs_throttle(struct btrfs_root *root)
280{
281 mutex_lock(&root->fs_info->trans_mutex);
Sage Weil9ca9ee02008-08-04 10:41:27 -0400282 if (!root->fs_info->open_ioctl_trans)
283 wait_current_trans(root);
Chris Mason37d1aee2008-07-31 10:48:37 -0400284 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason37d1aee2008-07-31 10:48:37 -0400285 throttle_on_drops(root);
286}
287
Chris Mason89ce8a62008-06-25 16:01:31 -0400288static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
289 struct btrfs_root *root, int throttle)
Chris Mason79154b12007-03-22 15:59:16 -0400290{
291 struct btrfs_transaction *cur_trans;
Chris Masonab78c842008-07-29 16:15:18 -0400292 struct btrfs_fs_info *info = root->fs_info;
Chris Masonc3e69d52009-03-13 10:17:05 -0400293 int count = 0;
Chris Masond6e4a422007-04-06 15:37:36 -0400294
Chris Masonc3e69d52009-03-13 10:17:05 -0400295 while (count < 4) {
296 unsigned long cur = trans->delayed_ref_updates;
297 trans->delayed_ref_updates = 0;
298 if (cur &&
299 trans->transaction->delayed_refs.num_heads_ready > 64) {
300 trans->delayed_ref_updates = 0;
Chris Masonb7ec40d2009-03-12 20:12:45 -0400301
302 /*
303 * do a full flush if the transaction is trying
304 * to close
305 */
306 if (trans->transaction->delayed_refs.flushing)
307 cur = 0;
Chris Masonc3e69d52009-03-13 10:17:05 -0400308 btrfs_run_delayed_refs(trans, root, cur);
309 } else {
310 break;
311 }
312 count++;
Chris Mason56bec292009-03-13 10:10:06 -0400313 }
314
Chris Masonab78c842008-07-29 16:15:18 -0400315 mutex_lock(&info->trans_mutex);
316 cur_trans = info->running_transaction;
Chris Masonccd467d2007-06-28 15:57:36 -0400317 WARN_ON(cur_trans != trans->transaction);
Chris Masond5719762007-03-23 10:01:08 -0400318 WARN_ON(cur_trans->num_writers < 1);
Chris Masonccd467d2007-06-28 15:57:36 -0400319 cur_trans->num_writers--;
Chris Mason89ce8a62008-06-25 16:01:31 -0400320
Chris Mason79154b12007-03-22 15:59:16 -0400321 if (waitqueue_active(&cur_trans->writer_wait))
322 wake_up(&cur_trans->writer_wait);
Chris Mason79154b12007-03-22 15:59:16 -0400323 put_transaction(cur_trans);
Chris Masonab78c842008-07-29 16:15:18 -0400324 mutex_unlock(&info->trans_mutex);
Chris Masond6025572007-03-30 14:27:56 -0400325 memset(trans, 0, sizeof(*trans));
Chris Mason2c90e5d2007-04-02 10:50:19 -0400326 kmem_cache_free(btrfs_trans_handle_cachep, trans);
Chris Masonab78c842008-07-29 16:15:18 -0400327
328 if (throttle)
Chris Mason37d1aee2008-07-31 10:48:37 -0400329 throttle_on_drops(root);
Chris Masonab78c842008-07-29 16:15:18 -0400330
Chris Mason79154b12007-03-22 15:59:16 -0400331 return 0;
332}
333
Chris Mason89ce8a62008-06-25 16:01:31 -0400334int btrfs_end_transaction(struct btrfs_trans_handle *trans,
335 struct btrfs_root *root)
336{
337 return __btrfs_end_transaction(trans, root, 0);
338}
339
340int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
341 struct btrfs_root *root)
342{
343 return __btrfs_end_transaction(trans, root, 1);
344}
345
Chris Masond352ac62008-09-29 15:18:18 -0400346/*
347 * when btree blocks are allocated, they have some corresponding bits set for
348 * them in one of two extent_io trees. This is used to make sure all of
349 * those extents are on disk for transaction or log commit
350 */
Chris Masond0c803c2008-09-11 16:17:57 -0400351int btrfs_write_and_wait_marked_extents(struct btrfs_root *root,
352 struct extent_io_tree *dirty_pages)
Chris Mason79154b12007-03-22 15:59:16 -0400353{
Chris Mason7c4452b2007-04-28 09:29:35 -0400354 int ret;
Chris Mason777e6bd2008-08-15 15:34:15 -0400355 int err = 0;
Chris Mason7c4452b2007-04-28 09:29:35 -0400356 int werr = 0;
357 struct page *page;
Chris Mason7c4452b2007-04-28 09:29:35 -0400358 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Mason777e6bd2008-08-15 15:34:15 -0400359 u64 start = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400360 u64 end;
361 unsigned long index;
Chris Mason7c4452b2007-04-28 09:29:35 -0400362
Chris Masond3977122009-01-05 21:25:51 -0500363 while (1) {
Chris Mason777e6bd2008-08-15 15:34:15 -0400364 ret = find_first_extent_bit(dirty_pages, start, &start, &end,
Chris Mason5f39d392007-10-15 16:14:19 -0400365 EXTENT_DIRTY);
366 if (ret)
Chris Mason7c4452b2007-04-28 09:29:35 -0400367 break;
Chris Masond3977122009-01-05 21:25:51 -0500368 while (start <= end) {
Chris Mason777e6bd2008-08-15 15:34:15 -0400369 cond_resched();
370
Chris Mason5f39d392007-10-15 16:14:19 -0400371 index = start >> PAGE_CACHE_SHIFT;
Chris Mason35ebb932007-10-30 16:56:53 -0400372 start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
Chris Mason4bef0842008-09-08 11:18:08 -0400373 page = find_get_page(btree_inode->i_mapping, index);
Chris Mason7c4452b2007-04-28 09:29:35 -0400374 if (!page)
375 continue;
Chris Mason4bef0842008-09-08 11:18:08 -0400376
377 btree_lock_page_hook(page);
378 if (!page->mapping) {
379 unlock_page(page);
380 page_cache_release(page);
381 continue;
382 }
383
Chris Mason6702ed42007-08-07 16:15:09 -0400384 if (PageWriteback(page)) {
385 if (PageDirty(page))
386 wait_on_page_writeback(page);
387 else {
388 unlock_page(page);
389 page_cache_release(page);
390 continue;
391 }
392 }
Chris Mason7c4452b2007-04-28 09:29:35 -0400393 err = write_one_page(page, 0);
394 if (err)
395 werr = err;
396 page_cache_release(page);
397 }
398 }
Chris Masond3977122009-01-05 21:25:51 -0500399 while (1) {
Chris Mason777e6bd2008-08-15 15:34:15 -0400400 ret = find_first_extent_bit(dirty_pages, 0, &start, &end,
401 EXTENT_DIRTY);
402 if (ret)
403 break;
404
405 clear_extent_dirty(dirty_pages, start, end, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -0500406 while (start <= end) {
Chris Mason777e6bd2008-08-15 15:34:15 -0400407 index = start >> PAGE_CACHE_SHIFT;
408 start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
409 page = find_get_page(btree_inode->i_mapping, index);
410 if (!page)
411 continue;
412 if (PageDirty(page)) {
Chris Mason4bef0842008-09-08 11:18:08 -0400413 btree_lock_page_hook(page);
414 wait_on_page_writeback(page);
Chris Mason777e6bd2008-08-15 15:34:15 -0400415 err = write_one_page(page, 0);
416 if (err)
417 werr = err;
418 }
Chris Mason105d9312008-11-18 12:13:12 -0500419 wait_on_page_writeback(page);
Chris Mason777e6bd2008-08-15 15:34:15 -0400420 page_cache_release(page);
421 cond_resched();
422 }
423 }
Chris Mason7c4452b2007-04-28 09:29:35 -0400424 if (err)
425 werr = err;
426 return werr;
Chris Mason79154b12007-03-22 15:59:16 -0400427}
428
Chris Masond0c803c2008-09-11 16:17:57 -0400429int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
430 struct btrfs_root *root)
431{
432 if (!trans || !trans->transaction) {
433 struct inode *btree_inode;
434 btree_inode = root->fs_info->btree_inode;
435 return filemap_write_and_wait(btree_inode->i_mapping);
436 }
437 return btrfs_write_and_wait_marked_extents(root,
438 &trans->transaction->dirty_pages);
439}
440
Chris Masond352ac62008-09-29 15:18:18 -0400441/*
442 * this is used to update the root pointer in the tree of tree roots.
443 *
444 * But, in the case of the extent allocation tree, updating the root
445 * pointer may allocate blocks which may change the root of the extent
446 * allocation tree.
447 *
448 * So, this loops and repeats and makes sure the cowonly root didn't
449 * change while the root pointer was being updated in the metadata.
450 */
Chris Mason0b86a832008-03-24 15:01:56 -0400451static int update_cowonly_root(struct btrfs_trans_handle *trans,
452 struct btrfs_root *root)
453{
454 int ret;
455 u64 old_root_bytenr;
456 struct btrfs_root *tree_root = root->fs_info->tree_root;
457
458 btrfs_write_dirty_block_groups(trans, root);
Chris Mason56bec292009-03-13 10:10:06 -0400459
460 ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
461 BUG_ON(ret);
Chris Mason87ef2bb2008-10-30 11:23:27 -0400462
Chris Masond3977122009-01-05 21:25:51 -0500463 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -0400464 old_root_bytenr = btrfs_root_bytenr(&root->root_item);
465 if (old_root_bytenr == root->node->start)
466 break;
467 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));
Yan Zheng84234f32008-10-29 14:49:05 -0400471 btrfs_set_root_generation(&root->root_item, trans->transid);
Chris Mason87ef2bb2008-10-30 11:23:27 -0400472
Chris Mason0b86a832008-03-24 15:01:56 -0400473 ret = btrfs_update_root(trans, tree_root,
474 &root->root_key,
475 &root->root_item);
476 BUG_ON(ret);
477 btrfs_write_dirty_block_groups(trans, root);
Chris Mason56bec292009-03-13 10:10:06 -0400478
479 ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
480 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -0400481 }
482 return 0;
483}
484
Chris Masond352ac62008-09-29 15:18:18 -0400485/*
486 * update all the cowonly tree roots on disk
487 */
Chris Mason79154b12007-03-22 15:59:16 -0400488int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans,
489 struct btrfs_root *root)
490{
Chris Mason79154b12007-03-22 15:59:16 -0400491 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason0b86a832008-03-24 15:01:56 -0400492 struct list_head *next;
Yan Zheng84234f32008-10-29 14:49:05 -0400493 struct extent_buffer *eb;
Chris Mason56bec292009-03-13 10:10:06 -0400494 int ret;
Yan Zheng84234f32008-10-29 14:49:05 -0400495
Chris Mason56bec292009-03-13 10:10:06 -0400496 ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
497 BUG_ON(ret);
Chris Mason87ef2bb2008-10-30 11:23:27 -0400498
Yan Zheng84234f32008-10-29 14:49:05 -0400499 eb = btrfs_lock_root_node(fs_info->tree_root);
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400500 btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 0, &eb);
Yan Zheng84234f32008-10-29 14:49:05 -0400501 btrfs_tree_unlock(eb);
502 free_extent_buffer(eb);
Chris Mason79154b12007-03-22 15:59:16 -0400503
Chris Mason56bec292009-03-13 10:10:06 -0400504 ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
505 BUG_ON(ret);
Chris Mason87ef2bb2008-10-30 11:23:27 -0400506
Chris Masond3977122009-01-05 21:25:51 -0500507 while (!list_empty(&fs_info->dirty_cowonly_roots)) {
Chris Mason0b86a832008-03-24 15:01:56 -0400508 next = fs_info->dirty_cowonly_roots.next;
509 list_del_init(next);
510 root = list_entry(next, struct btrfs_root, dirty_list);
Chris Mason87ef2bb2008-10-30 11:23:27 -0400511
Chris Mason0b86a832008-03-24 15:01:56 -0400512 update_cowonly_root(trans, root);
Chris Mason56bec292009-03-13 10:10:06 -0400513
514 ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
515 BUG_ON(ret);
Chris Mason79154b12007-03-22 15:59:16 -0400516 }
517 return 0;
518}
519
Chris Masond352ac62008-09-29 15:18:18 -0400520/*
521 * dead roots are old snapshots that need to be deleted. This allocates
522 * a dirty root struct and adds it into the list of dead roots that need to
523 * be deleted
524 */
Yan Zhengb48652c2008-08-04 23:23:47 -0400525int btrfs_add_dead_root(struct btrfs_root *root, struct btrfs_root *latest)
Chris Mason5eda7b52007-06-22 14:16:25 -0400526{
Yan Zhengf321e492008-07-30 09:26:11 -0400527 struct btrfs_dirty_root *dirty;
Chris Mason5eda7b52007-06-22 14:16:25 -0400528
529 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
530 if (!dirty)
531 return -ENOMEM;
Chris Mason5eda7b52007-06-22 14:16:25 -0400532 dirty->root = root;
Chris Mason5ce14bb2007-09-11 11:15:39 -0400533 dirty->latest_root = latest;
Yan Zhengb48652c2008-08-04 23:23:47 -0400534
535 mutex_lock(&root->fs_info->trans_mutex);
536 list_add(&dirty->list, &latest->fs_info->dead_roots);
537 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason5eda7b52007-06-22 14:16:25 -0400538 return 0;
539}
540
Chris Masond352ac62008-09-29 15:18:18 -0400541/*
542 * at transaction commit time we need to schedule the old roots for
543 * deletion via btrfs_drop_snapshot. This runs through all the
544 * reference counted roots that were modified in the current
545 * transaction and puts them into the drop list
546 */
Chris Mason80b67942008-02-01 16:35:04 -0500547static noinline int add_dirty_roots(struct btrfs_trans_handle *trans,
548 struct radix_tree_root *radix,
549 struct list_head *list)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400550{
Yan Zhengf321e492008-07-30 09:26:11 -0400551 struct btrfs_dirty_root *dirty;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400552 struct btrfs_root *gang[8];
553 struct btrfs_root *root;
554 int i;
555 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400556 int err = 0;
Chris Mason5eda7b52007-06-22 14:16:25 -0400557 u32 refs;
Chris Mason54aa1f42007-06-22 14:16:25 -0400558
Chris Masond3977122009-01-05 21:25:51 -0500559 while (1) {
Chris Mason0f7d52f2007-04-09 10:42:37 -0400560 ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0,
561 ARRAY_SIZE(gang),
562 BTRFS_ROOT_TRANS_TAG);
563 if (ret == 0)
564 break;
565 for (i = 0; i < ret; i++) {
566 root = gang[i];
Chris Mason2619ba12007-04-10 16:58:11 -0400567 radix_tree_tag_clear(radix,
568 (unsigned long)root->root_key.objectid,
569 BTRFS_ROOT_TRANS_TAG);
Yan Zheng31153d82008-07-28 15:32:19 -0400570
571 BUG_ON(!root->ref_tree);
Chris Mason017e5362008-07-28 15:32:51 -0400572 dirty = root->dirty_root;
Yan Zheng31153d82008-07-28 15:32:19 -0400573
Chris Masone02119d2008-09-05 16:13:11 -0400574 btrfs_free_log(trans, root);
Yan Zhengf82d02d2008-10-29 14:49:05 -0400575 btrfs_free_reloc_root(trans, root);
Chris Masone02119d2008-09-05 16:13:11 -0400576
Chris Mason0f7d52f2007-04-09 10:42:37 -0400577 if (root->commit_root == root->node) {
Chris Masondb945352007-10-15 16:15:53 -0400578 WARN_ON(root->node->start !=
579 btrfs_root_bytenr(&root->root_item));
Yan Zheng31153d82008-07-28 15:32:19 -0400580
Chris Mason5f39d392007-10-15 16:14:19 -0400581 free_extent_buffer(root->commit_root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400582 root->commit_root = NULL;
Yan Zheng7ea394f2008-08-05 13:05:02 -0400583 root->dirty_root = NULL;
Yanbcc63ab2008-07-30 16:29:20 -0400584
585 spin_lock(&root->list_lock);
586 list_del_init(&dirty->root->dead_list);
587 spin_unlock(&root->list_lock);
588
Yan Zheng31153d82008-07-28 15:32:19 -0400589 kfree(dirty->root);
590 kfree(dirty);
Josef Bacik58176a92007-08-29 15:47:34 -0400591
592 /* make sure to update the root on disk
593 * so we get any updates to the block used
594 * counts
595 */
596 err = btrfs_update_root(trans,
597 root->fs_info->tree_root,
598 &root->root_key,
599 &root->root_item);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400600 continue;
601 }
Chris Mason9f3a7422007-08-07 15:52:19 -0400602
603 memset(&root->root_item.drop_progress, 0,
604 sizeof(struct btrfs_disk_key));
605 root->root_item.drop_level = 0;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400606 root->commit_root = NULL;
Yan Zheng7ea394f2008-08-05 13:05:02 -0400607 root->dirty_root = NULL;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400608 root->root_key.offset = root->fs_info->generation;
Chris Masondb945352007-10-15 16:15:53 -0400609 btrfs_set_root_bytenr(&root->root_item,
610 root->node->start);
611 btrfs_set_root_level(&root->root_item,
612 btrfs_header_level(root->node));
Yan Zheng84234f32008-10-29 14:49:05 -0400613 btrfs_set_root_generation(&root->root_item,
614 root->root_key.offset);
615
Chris Mason0f7d52f2007-04-09 10:42:37 -0400616 err = btrfs_insert_root(trans, root->fs_info->tree_root,
617 &root->root_key,
618 &root->root_item);
Chris Mason54aa1f42007-06-22 14:16:25 -0400619 if (err)
620 break;
Chris Mason9f3a7422007-08-07 15:52:19 -0400621
622 refs = btrfs_root_refs(&dirty->root->root_item);
623 btrfs_set_root_refs(&dirty->root->root_item, refs - 1);
Chris Mason5eda7b52007-06-22 14:16:25 -0400624 err = btrfs_update_root(trans, root->fs_info->tree_root,
Chris Mason9f3a7422007-08-07 15:52:19 -0400625 &dirty->root->root_key,
626 &dirty->root->root_item);
Chris Mason5eda7b52007-06-22 14:16:25 -0400627
628 BUG_ON(err);
Chris Mason9f3a7422007-08-07 15:52:19 -0400629 if (refs == 1) {
Chris Mason5eda7b52007-06-22 14:16:25 -0400630 list_add(&dirty->list, list);
Chris Mason9f3a7422007-08-07 15:52:19 -0400631 } else {
632 WARN_ON(1);
Yan Zheng31153d82008-07-28 15:32:19 -0400633 free_extent_buffer(dirty->root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -0400634 kfree(dirty->root);
Chris Mason5eda7b52007-06-22 14:16:25 -0400635 kfree(dirty);
Chris Mason9f3a7422007-08-07 15:52:19 -0400636 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400637 }
638 }
Chris Mason54aa1f42007-06-22 14:16:25 -0400639 return err;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400640}
641
Chris Masond352ac62008-09-29 15:18:18 -0400642/*
643 * defrag a given btree. If cacheonly == 1, this won't read from the disk,
644 * otherwise every leaf in the btree is read and defragged.
645 */
Chris Masone9d0b132007-08-10 14:06:19 -0400646int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
647{
648 struct btrfs_fs_info *info = root->fs_info;
649 int ret;
650 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400651 unsigned long nr;
Chris Masone9d0b132007-08-10 14:06:19 -0400652
Chris Masona2135012008-06-25 16:01:30 -0400653 smp_mb();
Chris Masone9d0b132007-08-10 14:06:19 -0400654 if (root->defrag_running)
655 return 0;
Chris Masone9d0b132007-08-10 14:06:19 -0400656 trans = btrfs_start_transaction(root, 1);
Chris Mason6b800532007-10-15 16:17:34 -0400657 while (1) {
Chris Masone9d0b132007-08-10 14:06:19 -0400658 root->defrag_running = 1;
659 ret = btrfs_defrag_leaves(trans, root, cacheonly);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400660 nr = trans->blocks_used;
Chris Masone9d0b132007-08-10 14:06:19 -0400661 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400662 btrfs_btree_balance_dirty(info->tree_root, nr);
Chris Masone9d0b132007-08-10 14:06:19 -0400663 cond_resched();
664
Chris Masone9d0b132007-08-10 14:06:19 -0400665 trans = btrfs_start_transaction(root, 1);
Chris Mason3f157a22008-06-25 16:01:31 -0400666 if (root->fs_info->closing || ret != -EAGAIN)
Chris Masone9d0b132007-08-10 14:06:19 -0400667 break;
668 }
669 root->defrag_running = 0;
Chris Masona2135012008-06-25 16:01:30 -0400670 smp_mb();
Chris Masone9d0b132007-08-10 14:06:19 -0400671 btrfs_end_transaction(trans, root);
672 return 0;
673}
674
Chris Masond352ac62008-09-29 15:18:18 -0400675/*
Chris Masonb7ec40d2009-03-12 20:12:45 -0400676 * when dropping snapshots, we generate a ton of delayed refs, and it makes
677 * sense not to join the transaction while it is trying to flush the current
678 * queue of delayed refs out.
679 *
680 * This is used by the drop snapshot code only
681 */
682static noinline int wait_transaction_pre_flush(struct btrfs_fs_info *info)
683{
684 DEFINE_WAIT(wait);
685
686 mutex_lock(&info->trans_mutex);
687 while (info->running_transaction &&
688 info->running_transaction->delayed_refs.flushing) {
689 prepare_to_wait(&info->transaction_wait, &wait,
690 TASK_UNINTERRUPTIBLE);
691 mutex_unlock(&info->trans_mutex);
692 schedule();
693 mutex_lock(&info->trans_mutex);
694 finish_wait(&info->transaction_wait, &wait);
695 }
696 mutex_unlock(&info->trans_mutex);
697 return 0;
698}
699
700/*
Chris Masond352ac62008-09-29 15:18:18 -0400701 * Given a list of roots that need to be deleted, call btrfs_drop_snapshot on
702 * all of them
703 */
Chris Mason80b67942008-02-01 16:35:04 -0500704static noinline int drop_dirty_roots(struct btrfs_root *tree_root,
705 struct list_head *list)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400706{
Yan Zhengf321e492008-07-30 09:26:11 -0400707 struct btrfs_dirty_root *dirty;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400708 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400709 unsigned long nr;
Chris Masondb945352007-10-15 16:15:53 -0400710 u64 num_bytes;
711 u64 bytes_used;
Yanbcc63ab2008-07-30 16:29:20 -0400712 u64 max_useless;
Chris Mason54aa1f42007-06-22 14:16:25 -0400713 int ret = 0;
Chris Mason9f3a7422007-08-07 15:52:19 -0400714 int err;
715
Chris Masond3977122009-01-05 21:25:51 -0500716 while (!list_empty(list)) {
Josef Bacik58176a92007-08-29 15:47:34 -0400717 struct btrfs_root *root;
718
Yan Zhengf321e492008-07-30 09:26:11 -0400719 dirty = list_entry(list->prev, struct btrfs_dirty_root, list);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400720 list_del_init(&dirty->list);
Chris Mason5eda7b52007-06-22 14:16:25 -0400721
Chris Masondb945352007-10-15 16:15:53 -0400722 num_bytes = btrfs_root_used(&dirty->root->root_item);
Josef Bacik58176a92007-08-29 15:47:34 -0400723 root = dirty->latest_root;
Chris Masona2135012008-06-25 16:01:30 -0400724 atomic_inc(&root->fs_info->throttles);
Josef Bacik58176a92007-08-29 15:47:34 -0400725
Chris Masond3977122009-01-05 21:25:51 -0500726 while (1) {
Chris Masonb7ec40d2009-03-12 20:12:45 -0400727 /*
728 * we don't want to jump in and create a bunch of
729 * delayed refs if the transaction is starting to close
730 */
731 wait_transaction_pre_flush(tree_root->fs_info);
Chris Mason9f3a7422007-08-07 15:52:19 -0400732 trans = btrfs_start_transaction(tree_root, 1);
Chris Masonb7ec40d2009-03-12 20:12:45 -0400733
734 /*
735 * we've joined a transaction, make sure it isn't
736 * closing right now
737 */
738 if (trans->transaction->delayed_refs.flushing) {
739 btrfs_end_transaction(trans, tree_root);
740 continue;
741 }
742
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400743 mutex_lock(&root->fs_info->drop_mutex);
Chris Mason9f3a7422007-08-07 15:52:19 -0400744 ret = btrfs_drop_snapshot(trans, dirty->root);
Chris Masond3977122009-01-05 21:25:51 -0500745 if (ret != -EAGAIN)
Chris Mason9f3a7422007-08-07 15:52:19 -0400746 break;
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400747 mutex_unlock(&root->fs_info->drop_mutex);
Josef Bacik58176a92007-08-29 15:47:34 -0400748
Chris Mason9f3a7422007-08-07 15:52:19 -0400749 err = btrfs_update_root(trans,
750 tree_root,
751 &dirty->root->root_key,
752 &dirty->root->root_item);
753 if (err)
754 ret = err;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400755 nr = trans->blocks_used;
Chris Mason017e5362008-07-28 15:32:51 -0400756 ret = btrfs_end_transaction(trans, tree_root);
Chris Mason9f3a7422007-08-07 15:52:19 -0400757 BUG_ON(ret);
Chris Masona2135012008-06-25 16:01:30 -0400758
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400759 btrfs_btree_balance_dirty(tree_root, nr);
Chris Mason4dc119042007-10-15 16:18:14 -0400760 cond_resched();
Chris Mason9f3a7422007-08-07 15:52:19 -0400761 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400762 BUG_ON(ret);
Chris Masona2135012008-06-25 16:01:30 -0400763 atomic_dec(&root->fs_info->throttles);
Chris Mason017e5362008-07-28 15:32:51 -0400764 wake_up(&root->fs_info->transaction_throttle);
Josef Bacik58176a92007-08-29 15:47:34 -0400765
Chris Masondb945352007-10-15 16:15:53 -0400766 num_bytes -= btrfs_root_used(&dirty->root->root_item);
767 bytes_used = btrfs_root_used(&root->root_item);
768 if (num_bytes) {
Yan Zheng24562422009-02-12 14:14:53 -0500769 mutex_lock(&root->fs_info->trans_mutex);
Chris Masone02119d2008-09-05 16:13:11 -0400770 btrfs_record_root_in_trans(root);
Yan Zheng24562422009-02-12 14:14:53 -0500771 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason5f39d392007-10-15 16:14:19 -0400772 btrfs_set_root_used(&root->root_item,
Chris Masondb945352007-10-15 16:15:53 -0400773 bytes_used - num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -0400774 }
Chris Masona2135012008-06-25 16:01:30 -0400775
Chris Mason9f3a7422007-08-07 15:52:19 -0400776 ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key);
Josef Bacik58176a92007-08-29 15:47:34 -0400777 if (ret) {
778 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -0400779 break;
Josef Bacik58176a92007-08-29 15:47:34 -0400780 }
Chris Masona2135012008-06-25 16:01:30 -0400781 mutex_unlock(&root->fs_info->drop_mutex);
782
Yanbcc63ab2008-07-30 16:29:20 -0400783 spin_lock(&root->list_lock);
784 list_del_init(&dirty->root->dead_list);
785 if (!list_empty(&root->dead_list)) {
786 struct btrfs_root *oldest;
787 oldest = list_entry(root->dead_list.prev,
788 struct btrfs_root, dead_list);
789 max_useless = oldest->root_key.offset - 1;
790 } else {
791 max_useless = root->root_key.offset - 1;
792 }
793 spin_unlock(&root->list_lock);
794
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400795 nr = trans->blocks_used;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400796 ret = btrfs_end_transaction(trans, tree_root);
797 BUG_ON(ret);
Chris Mason5eda7b52007-06-22 14:16:25 -0400798
Zheng Yane4657682008-09-26 10:04:53 -0400799 ret = btrfs_remove_leaf_refs(root, max_useless, 0);
Yanbcc63ab2008-07-30 16:29:20 -0400800 BUG_ON(ret);
801
Chris Masonf510cfe2007-10-15 16:14:48 -0400802 free_extent_buffer(dirty->root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -0400803 kfree(dirty->root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400804 kfree(dirty);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400805
806 btrfs_btree_balance_dirty(tree_root, nr);
Chris Mason4dc119042007-10-15 16:18:14 -0400807 cond_resched();
Chris Mason0f7d52f2007-04-09 10:42:37 -0400808 }
Chris Mason54aa1f42007-06-22 14:16:25 -0400809 return ret;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400810}
811
Chris Masond352ac62008-09-29 15:18:18 -0400812/*
813 * new snapshots need to be created at a very specific time in the
814 * transaction commit. This does the actual creation
815 */
Chris Mason80b67942008-02-01 16:35:04 -0500816static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
Chris Mason3063d292008-01-08 15:46:30 -0500817 struct btrfs_fs_info *fs_info,
818 struct btrfs_pending_snapshot *pending)
819{
820 struct btrfs_key key;
Chris Mason80b67942008-02-01 16:35:04 -0500821 struct btrfs_root_item *new_root_item;
Chris Mason3063d292008-01-08 15:46:30 -0500822 struct btrfs_root *tree_root = fs_info->tree_root;
823 struct btrfs_root *root = pending->root;
824 struct extent_buffer *tmp;
Chris Mason925baed2008-06-25 16:01:30 -0400825 struct extent_buffer *old;
Chris Mason3063d292008-01-08 15:46:30 -0500826 int ret;
827 u64 objectid;
828
Chris Mason80b67942008-02-01 16:35:04 -0500829 new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
830 if (!new_root_item) {
831 ret = -ENOMEM;
832 goto fail;
833 }
Chris Mason3063d292008-01-08 15:46:30 -0500834 ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid);
835 if (ret)
836 goto fail;
837
Yan Zheng80ff3852008-10-30 14:20:02 -0400838 btrfs_record_root_in_trans(root);
839 btrfs_set_root_last_snapshot(&root->root_item, trans->transid);
Chris Mason80b67942008-02-01 16:35:04 -0500840 memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
Chris Mason3063d292008-01-08 15:46:30 -0500841
842 key.objectid = objectid;
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400843 key.offset = trans->transid;
Chris Mason3063d292008-01-08 15:46:30 -0500844 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
845
Chris Mason925baed2008-06-25 16:01:30 -0400846 old = btrfs_lock_root_node(root);
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400847 btrfs_cow_block(trans, root, old, NULL, 0, &old);
Chris Mason3063d292008-01-08 15:46:30 -0500848
Chris Mason925baed2008-06-25 16:01:30 -0400849 btrfs_copy_root(trans, root, old, &tmp, objectid);
850 btrfs_tree_unlock(old);
851 free_extent_buffer(old);
Chris Mason3063d292008-01-08 15:46:30 -0500852
Chris Mason80b67942008-02-01 16:35:04 -0500853 btrfs_set_root_bytenr(new_root_item, tmp->start);
854 btrfs_set_root_level(new_root_item, btrfs_header_level(tmp));
Yan Zheng84234f32008-10-29 14:49:05 -0400855 btrfs_set_root_generation(new_root_item, trans->transid);
Chris Mason3063d292008-01-08 15:46:30 -0500856 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
Chris Mason80b67942008-02-01 16:35:04 -0500857 new_root_item);
Chris Mason925baed2008-06-25 16:01:30 -0400858 btrfs_tree_unlock(tmp);
Chris Mason3063d292008-01-08 15:46:30 -0500859 free_extent_buffer(tmp);
860 if (ret)
861 goto fail;
862
Chris Mason3de45862008-11-17 21:02:50 -0500863 key.offset = (u64)-1;
864 memcpy(&pending->root_key, &key, sizeof(key));
865fail:
866 kfree(new_root_item);
867 return ret;
868}
869
870static noinline int finish_pending_snapshot(struct btrfs_fs_info *fs_info,
871 struct btrfs_pending_snapshot *pending)
872{
873 int ret;
874 int namelen;
875 u64 index = 0;
876 struct btrfs_trans_handle *trans;
877 struct inode *parent_inode;
878 struct inode *inode;
Chris Mason0660b5a2008-11-17 20:37:39 -0500879 struct btrfs_root *parent_root;
Chris Mason3de45862008-11-17 21:02:50 -0500880
Chris Mason3394e162008-11-17 20:42:26 -0500881 parent_inode = pending->dentry->d_parent->d_inode;
Chris Mason0660b5a2008-11-17 20:37:39 -0500882 parent_root = BTRFS_I(parent_inode)->root;
Yan Zheng180591b2009-01-06 09:58:06 -0500883 trans = btrfs_join_transaction(parent_root, 1);
Chris Mason3de45862008-11-17 21:02:50 -0500884
Chris Mason3063d292008-01-08 15:46:30 -0500885 /*
886 * insert the directory item
887 */
Sven Wegener3b963622008-06-09 21:57:42 -0400888 namelen = strlen(pending->name);
Chris Mason3de45862008-11-17 21:02:50 -0500889 ret = btrfs_set_inode_index(parent_inode, &index);
Chris Mason0660b5a2008-11-17 20:37:39 -0500890 ret = btrfs_insert_dir_item(trans, parent_root,
Chris Mason3de45862008-11-17 21:02:50 -0500891 pending->name, namelen,
892 parent_inode->i_ino,
893 &pending->root_key, BTRFS_FT_DIR, index);
Chris Mason3063d292008-01-08 15:46:30 -0500894
895 if (ret)
896 goto fail;
Chris Mason0660b5a2008-11-17 20:37:39 -0500897
Yan Zheng52c26172009-01-05 15:43:43 -0500898 btrfs_i_size_write(parent_inode, parent_inode->i_size + namelen * 2);
899 ret = btrfs_update_inode(trans, parent_root, parent_inode);
900 BUG_ON(ret);
901
Chris Mason0660b5a2008-11-17 20:37:39 -0500902 /* add the backref first */
903 ret = btrfs_add_root_ref(trans, parent_root->fs_info->tree_root,
904 pending->root_key.objectid,
905 BTRFS_ROOT_BACKREF_KEY,
906 parent_root->root_key.objectid,
907 parent_inode->i_ino, index, pending->name,
908 namelen);
909
910 BUG_ON(ret);
911
912 /* now add the forward ref */
913 ret = btrfs_add_root_ref(trans, parent_root->fs_info->tree_root,
914 parent_root->root_key.objectid,
915 BTRFS_ROOT_REF_KEY,
916 pending->root_key.objectid,
917 parent_inode->i_ino, index, pending->name,
918 namelen);
919
Chris Mason3de45862008-11-17 21:02:50 -0500920 inode = btrfs_lookup_dentry(parent_inode, pending->dentry);
921 d_instantiate(pending->dentry, inode);
Chris Mason3063d292008-01-08 15:46:30 -0500922fail:
Chris Mason3de45862008-11-17 21:02:50 -0500923 btrfs_end_transaction(trans, fs_info->fs_root);
Chris Mason3063d292008-01-08 15:46:30 -0500924 return ret;
925}
926
Chris Masond352ac62008-09-29 15:18:18 -0400927/*
928 * create all the snapshots we've scheduled for creation
929 */
Chris Mason80b67942008-02-01 16:35:04 -0500930static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
931 struct btrfs_fs_info *fs_info)
Chris Mason3063d292008-01-08 15:46:30 -0500932{
933 struct btrfs_pending_snapshot *pending;
934 struct list_head *head = &trans->transaction->pending_snapshots;
Chris Mason3de45862008-11-17 21:02:50 -0500935 int ret;
936
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500937 list_for_each_entry(pending, head, list) {
Chris Mason3de45862008-11-17 21:02:50 -0500938 ret = create_pending_snapshot(trans, fs_info, pending);
939 BUG_ON(ret);
940 }
941 return 0;
942}
943
944static noinline int finish_pending_snapshots(struct btrfs_trans_handle *trans,
945 struct btrfs_fs_info *fs_info)
946{
947 struct btrfs_pending_snapshot *pending;
948 struct list_head *head = &trans->transaction->pending_snapshots;
Chris Mason3063d292008-01-08 15:46:30 -0500949 int ret;
950
Chris Masond3977122009-01-05 21:25:51 -0500951 while (!list_empty(head)) {
Chris Mason3063d292008-01-08 15:46:30 -0500952 pending = list_entry(head->next,
953 struct btrfs_pending_snapshot, list);
Chris Mason3de45862008-11-17 21:02:50 -0500954 ret = finish_pending_snapshot(fs_info, pending);
Chris Mason3063d292008-01-08 15:46:30 -0500955 BUG_ON(ret);
956 list_del(&pending->list);
957 kfree(pending->name);
958 kfree(pending);
959 }
Chris Masondc17ff82008-01-08 15:46:30 -0500960 return 0;
961}
962
Chris Mason79154b12007-03-22 15:59:16 -0400963int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
964 struct btrfs_root *root)
965{
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400966 unsigned long joined = 0;
967 unsigned long timeout = 1;
Chris Mason79154b12007-03-22 15:59:16 -0400968 struct btrfs_transaction *cur_trans;
Chris Mason8fd17792007-04-19 21:01:03 -0400969 struct btrfs_transaction *prev_trans = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -0400970 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400971 struct list_head dirty_fs_roots;
Chris Masond1310b22008-01-24 16:13:08 -0500972 struct extent_io_tree *pinned_copy;
Chris Mason79154b12007-03-22 15:59:16 -0400973 DEFINE_WAIT(wait);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400974 int ret;
Chris Mason89573b92009-03-12 20:12:45 -0400975 int should_grow = 0;
976 unsigned long now = get_seconds();
Chris Mason79154b12007-03-22 15:59:16 -0400977
Chris Mason5a3f23d2009-03-31 13:27:11 -0400978 btrfs_run_ordered_operations(root, 0);
979
Chris Mason56bec292009-03-13 10:10:06 -0400980 /* make a pass through all the delayed refs we have so far
981 * any runnings procs may add more while we are here
982 */
983 ret = btrfs_run_delayed_refs(trans, root, 0);
984 BUG_ON(ret);
985
Chris Masonb7ec40d2009-03-12 20:12:45 -0400986 cur_trans = trans->transaction;
Chris Mason56bec292009-03-13 10:10:06 -0400987 /*
988 * set the flushing flag so procs in this transaction have to
989 * start sending their work down.
990 */
Chris Masonb7ec40d2009-03-12 20:12:45 -0400991 cur_trans->delayed_refs.flushing = 1;
Chris Mason56bec292009-03-13 10:10:06 -0400992
Chris Masonc3e69d52009-03-13 10:17:05 -0400993 ret = btrfs_run_delayed_refs(trans, root, 0);
Chris Mason56bec292009-03-13 10:10:06 -0400994 BUG_ON(ret);
995
Chris Mason79154b12007-03-22 15:59:16 -0400996 mutex_lock(&root->fs_info->trans_mutex);
Chris Masonb7ec40d2009-03-12 20:12:45 -0400997 INIT_LIST_HEAD(&dirty_fs_roots);
998 if (cur_trans->in_commit) {
999 cur_trans->use_count++;
Chris Masonccd467d2007-06-28 15:57:36 -04001000 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -04001001 btrfs_end_transaction(trans, root);
Chris Masonccd467d2007-06-28 15:57:36 -04001002
Chris Mason79154b12007-03-22 15:59:16 -04001003 ret = wait_for_commit(root, cur_trans);
1004 BUG_ON(ret);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001005
1006 mutex_lock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -04001007 put_transaction(cur_trans);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001008 mutex_unlock(&root->fs_info->trans_mutex);
1009
Chris Mason79154b12007-03-22 15:59:16 -04001010 return 0;
1011 }
Chris Mason4313b392008-01-03 09:08:48 -05001012
1013 pinned_copy = kmalloc(sizeof(*pinned_copy), GFP_NOFS);
1014 if (!pinned_copy)
1015 return -ENOMEM;
1016
Chris Masond1310b22008-01-24 16:13:08 -05001017 extent_io_tree_init(pinned_copy,
Chris Mason4313b392008-01-03 09:08:48 -05001018 root->fs_info->btree_inode->i_mapping, GFP_NOFS);
1019
Chris Mason2c90e5d2007-04-02 10:50:19 -04001020 trans->transaction->in_commit = 1;
Chris Masonf9295742008-07-17 12:54:14 -04001021 trans->transaction->blocked = 1;
Chris Masonccd467d2007-06-28 15:57:36 -04001022 if (cur_trans->list.prev != &root->fs_info->trans_list) {
1023 prev_trans = list_entry(cur_trans->list.prev,
1024 struct btrfs_transaction, list);
1025 if (!prev_trans->commit_done) {
1026 prev_trans->use_count++;
Chris Masonccd467d2007-06-28 15:57:36 -04001027 mutex_unlock(&root->fs_info->trans_mutex);
1028
1029 wait_for_commit(root, prev_trans);
Chris Masonccd467d2007-06-28 15:57:36 -04001030
Chris Masonccd467d2007-06-28 15:57:36 -04001031 mutex_lock(&root->fs_info->trans_mutex);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001032 put_transaction(prev_trans);
Chris Masonccd467d2007-06-28 15:57:36 -04001033 }
1034 }
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001035
Chris Mason89573b92009-03-12 20:12:45 -04001036 if (now < cur_trans->start_time || now - cur_trans->start_time < 1)
1037 should_grow = 1;
1038
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001039 do {
Yan Zheng7ea394f2008-08-05 13:05:02 -04001040 int snap_pending = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001041 joined = cur_trans->num_joined;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001042 if (!list_empty(&trans->transaction->pending_snapshots))
1043 snap_pending = 1;
1044
Chris Mason2c90e5d2007-04-02 10:50:19 -04001045 WARN_ON(cur_trans != trans->transaction);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001046 prepare_to_wait(&cur_trans->writer_wait, &wait,
Chris Mason79154b12007-03-22 15:59:16 -04001047 TASK_UNINTERRUPTIBLE);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001048
1049 if (cur_trans->num_writers > 1)
1050 timeout = MAX_SCHEDULE_TIMEOUT;
Chris Mason89573b92009-03-12 20:12:45 -04001051 else if (should_grow)
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001052 timeout = 1;
1053
Chris Mason79154b12007-03-22 15:59:16 -04001054 mutex_unlock(&root->fs_info->trans_mutex);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001055
Yan Zheng7ea394f2008-08-05 13:05:02 -04001056 if (snap_pending) {
1057 ret = btrfs_wait_ordered_extents(root, 1);
1058 BUG_ON(ret);
1059 }
1060
Chris Mason5a3f23d2009-03-31 13:27:11 -04001061 /*
1062 * rename don't use btrfs_join_transaction, so, once we
1063 * set the transaction to blocked above, we aren't going
1064 * to get any new ordered operations. We can safely run
1065 * it here and no for sure that nothing new will be added
1066 * to the list
1067 */
1068 btrfs_run_ordered_operations(root, 1);
1069
Chris Mason89573b92009-03-12 20:12:45 -04001070 smp_mb();
1071 if (cur_trans->num_writers > 1 || should_grow)
1072 schedule_timeout(timeout);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001073
Chris Mason79154b12007-03-22 15:59:16 -04001074 mutex_lock(&root->fs_info->trans_mutex);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001075 finish_wait(&cur_trans->writer_wait, &wait);
1076 } while (cur_trans->num_writers > 1 ||
Chris Mason89573b92009-03-12 20:12:45 -04001077 (should_grow && cur_trans->num_joined != joined));
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001078
Chris Mason3063d292008-01-08 15:46:30 -05001079 ret = create_pending_snapshots(trans, root->fs_info);
1080 BUG_ON(ret);
1081
Chris Mason56bec292009-03-13 10:10:06 -04001082 ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
1083 BUG_ON(ret);
1084
Chris Mason2c90e5d2007-04-02 10:50:19 -04001085 WARN_ON(cur_trans != trans->transaction);
Chris Masondc17ff82008-01-08 15:46:30 -05001086
Chris Masone02119d2008-09-05 16:13:11 -04001087 /* btrfs_commit_tree_roots is responsible for getting the
1088 * various roots consistent with each other. Every pointer
1089 * in the tree of tree roots has to point to the most up to date
1090 * root for every subvolume and other tree. So, we have to keep
1091 * the tree logging code from jumping in and changing any
1092 * of the trees.
1093 *
1094 * At this point in the commit, there can't be any tree-log
1095 * writers, but a little lower down we drop the trans mutex
1096 * and let new people in. By holding the tree_log_mutex
1097 * from now until after the super is written, we avoid races
1098 * with the tree-log code.
1099 */
1100 mutex_lock(&root->fs_info->tree_log_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04001101 /*
1102 * keep tree reloc code from adding new reloc trees
1103 */
1104 mutex_lock(&root->fs_info->tree_reloc_mutex);
1105
Chris Masone02119d2008-09-05 16:13:11 -04001106
Chris Mason54aa1f42007-06-22 14:16:25 -04001107 ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix,
1108 &dirty_fs_roots);
1109 BUG_ON(ret);
1110
Chris Masone02119d2008-09-05 16:13:11 -04001111 /* add_dirty_roots gets rid of all the tree log roots, it is now
1112 * safe to free the root of tree log roots
1113 */
1114 btrfs_free_log_root_tree(trans, root->fs_info);
1115
Chris Mason79154b12007-03-22 15:59:16 -04001116 ret = btrfs_commit_tree_roots(trans, root);
1117 BUG_ON(ret);
Chris Mason54aa1f42007-06-22 14:16:25 -04001118
Chris Mason78fae272007-03-25 11:35:08 -04001119 cur_trans = root->fs_info->running_transaction;
Chris Masoncee36a02008-01-15 08:40:48 -05001120 spin_lock(&root->fs_info->new_trans_lock);
Chris Mason78fae272007-03-25 11:35:08 -04001121 root->fs_info->running_transaction = NULL;
Chris Masoncee36a02008-01-15 08:40:48 -05001122 spin_unlock(&root->fs_info->new_trans_lock);
Chris Mason4b52dff2007-06-26 10:06:50 -04001123 btrfs_set_super_generation(&root->fs_info->super_copy,
1124 cur_trans->transid);
1125 btrfs_set_super_root(&root->fs_info->super_copy,
Chris Masondb945352007-10-15 16:15:53 -04001126 root->fs_info->tree_root->node->start);
1127 btrfs_set_super_root_level(&root->fs_info->super_copy,
1128 btrfs_header_level(root->fs_info->tree_root->node));
Chris Mason5f39d392007-10-15 16:14:19 -04001129
Chris Mason0b86a832008-03-24 15:01:56 -04001130 btrfs_set_super_chunk_root(&root->fs_info->super_copy,
1131 chunk_root->node->start);
1132 btrfs_set_super_chunk_root_level(&root->fs_info->super_copy,
1133 btrfs_header_level(chunk_root->node));
Yan Zheng84234f32008-10-29 14:49:05 -04001134 btrfs_set_super_chunk_root_generation(&root->fs_info->super_copy,
1135 btrfs_header_generation(chunk_root->node));
Chris Masone02119d2008-09-05 16:13:11 -04001136
1137 if (!root->fs_info->log_root_recovering) {
1138 btrfs_set_super_log_root(&root->fs_info->super_copy, 0);
1139 btrfs_set_super_log_root_level(&root->fs_info->super_copy, 0);
1140 }
1141
Chris Masona061fc82008-05-07 11:43:44 -04001142 memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy,
1143 sizeof(root->fs_info->super_copy));
Chris Masonccd467d2007-06-28 15:57:36 -04001144
Chris Mason4313b392008-01-03 09:08:48 -05001145 btrfs_copy_pinned(root, pinned_copy);
Chris Masonccd467d2007-06-28 15:57:36 -04001146
Chris Masonf9295742008-07-17 12:54:14 -04001147 trans->transaction->blocked = 0;
Chris Masonb7ec40d2009-03-12 20:12:45 -04001148
Chris Masone6dcd2d2008-07-17 12:53:50 -04001149 wake_up(&root->fs_info->transaction_throttle);
Chris Masonf9295742008-07-17 12:54:14 -04001150 wake_up(&root->fs_info->transaction_wait);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001151
Chris Mason78fae272007-03-25 11:35:08 -04001152 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -04001153 ret = btrfs_write_and_wait_transaction(trans, root);
1154 BUG_ON(ret);
Yan Zhenga512bbf2008-12-08 16:46:26 -05001155 write_ctree_super(trans, root, 0);
Chris Mason4313b392008-01-03 09:08:48 -05001156
Chris Masone02119d2008-09-05 16:13:11 -04001157 /*
1158 * the super is written, we can safely allow the tree-loggers
1159 * to go about their business
1160 */
1161 mutex_unlock(&root->fs_info->tree_log_mutex);
1162
Chris Mason4313b392008-01-03 09:08:48 -05001163 btrfs_finish_extent_commit(trans, root, pinned_copy);
Chris Mason4313b392008-01-03 09:08:48 -05001164 kfree(pinned_copy);
1165
Zheng Yan1a40e232008-09-26 10:09:34 -04001166 btrfs_drop_dead_reloc_roots(root);
1167 mutex_unlock(&root->fs_info->tree_reloc_mutex);
1168
Chris Mason3de45862008-11-17 21:02:50 -05001169 /* do the directory inserts of any pending snapshot creations */
1170 finish_pending_snapshots(trans, root->fs_info);
1171
Zheng Yan1a40e232008-09-26 10:09:34 -04001172 mutex_lock(&root->fs_info->trans_mutex);
1173
Chris Mason2c90e5d2007-04-02 10:50:19 -04001174 cur_trans->commit_done = 1;
Chris Masonb7ec40d2009-03-12 20:12:45 -04001175
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001176 root->fs_info->last_trans_committed = cur_trans->transid;
Chris Mason2c90e5d2007-04-02 10:50:19 -04001177 wake_up(&cur_trans->commit_wait);
Chris Mason3de45862008-11-17 21:02:50 -05001178
Chris Mason79154b12007-03-22 15:59:16 -04001179 put_transaction(cur_trans);
Chris Mason78fae272007-03-25 11:35:08 -04001180 put_transaction(cur_trans);
Josef Bacik58176a92007-08-29 15:47:34 -04001181
Yanbcc63ab2008-07-30 16:29:20 -04001182 list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots);
Chris Masonfacda1e2007-06-08 18:11:48 -04001183 if (root->fs_info->closing)
1184 list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots);
Josef Bacik58176a92007-08-29 15:47:34 -04001185
Chris Mason78fae272007-03-25 11:35:08 -04001186 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason3de45862008-11-17 21:02:50 -05001187
Chris Mason2c90e5d2007-04-02 10:50:19 -04001188 kmem_cache_free(btrfs_trans_handle_cachep, trans);
Chris Mason79154b12007-03-22 15:59:16 -04001189
Chris Masond3977122009-01-05 21:25:51 -05001190 if (root->fs_info->closing)
Chris Masonfacda1e2007-06-08 18:11:48 -04001191 drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);
Chris Mason79154b12007-03-22 15:59:16 -04001192 return ret;
1193}
1194
Chris Masond352ac62008-09-29 15:18:18 -04001195/*
1196 * interface function to delete all the snapshots we have scheduled for deletion
1197 */
Chris Masone9d0b132007-08-10 14:06:19 -04001198int btrfs_clean_old_snapshots(struct btrfs_root *root)
1199{
1200 struct list_head dirty_roots;
1201 INIT_LIST_HEAD(&dirty_roots);
Chris Masona74a4b92008-06-25 16:01:31 -04001202again:
Chris Masone9d0b132007-08-10 14:06:19 -04001203 mutex_lock(&root->fs_info->trans_mutex);
1204 list_splice_init(&root->fs_info->dead_roots, &dirty_roots);
1205 mutex_unlock(&root->fs_info->trans_mutex);
1206
1207 if (!list_empty(&dirty_roots)) {
1208 drop_dirty_roots(root, &dirty_roots);
Chris Masona74a4b92008-06-25 16:01:31 -04001209 goto again;
Chris Masone9d0b132007-08-10 14:06:19 -04001210 }
1211 return 0;
1212}