blob: 2869b3361eb6be3cf82e60ba58ce1d9191360c46 [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++;
Josef Bacik15ee9bc2007-08-10 16:22:09 -040056 cur_trans->num_writers = 1;
57 cur_trans->num_joined = 0;
Chris Mason0f7d52f2007-04-09 10:42:37 -040058 cur_trans->transid = root->fs_info->generation;
Chris Mason79154b12007-03-22 15:59:16 -040059 init_waitqueue_head(&cur_trans->writer_wait);
60 init_waitqueue_head(&cur_trans->commit_wait);
61 cur_trans->in_commit = 0;
Chris Masonf9295742008-07-17 12:54:14 -040062 cur_trans->blocked = 0;
Chris Masond5719762007-03-23 10:01:08 -040063 cur_trans->use_count = 1;
Chris Mason79154b12007-03-22 15:59:16 -040064 cur_trans->commit_done = 0;
Chris Mason08607c12007-06-08 15:33:54 -040065 cur_trans->start_time = get_seconds();
Chris Mason56bec292009-03-13 10:10:06 -040066
67 cur_trans->delayed_refs.root.rb_node = NULL;
68 cur_trans->delayed_refs.num_entries = 0;
Chris Masonc3e69d52009-03-13 10:17:05 -040069 cur_trans->delayed_refs.num_heads_ready = 0;
70 cur_trans->delayed_refs.num_heads = 0;
Chris Mason56bec292009-03-13 10:10:06 -040071 cur_trans->delayed_refs.flushing = 0;
Chris Masonc3e69d52009-03-13 10:17:05 -040072 cur_trans->delayed_refs.run_delayed_start = 0;
Chris Mason56bec292009-03-13 10:10:06 -040073 spin_lock_init(&cur_trans->delayed_refs.lock);
74
Chris Mason3063d292008-01-08 15:46:30 -050075 INIT_LIST_HEAD(&cur_trans->pending_snapshots);
Chris Mason8fd17792007-04-19 21:01:03 -040076 list_add_tail(&cur_trans->list, &root->fs_info->trans_list);
Chris Masond1310b22008-01-24 16:13:08 -050077 extent_io_tree_init(&cur_trans->dirty_pages,
Chris Mason5f39d392007-10-15 16:14:19 -040078 root->fs_info->btree_inode->i_mapping,
79 GFP_NOFS);
Chris Mason48ec2cf2008-06-09 09:35:50 -040080 spin_lock(&root->fs_info->new_trans_lock);
81 root->fs_info->running_transaction = cur_trans;
82 spin_unlock(&root->fs_info->new_trans_lock);
Josef Bacik15ee9bc2007-08-10 16:22:09 -040083 } else {
84 cur_trans->num_writers++;
85 cur_trans->num_joined++;
Chris Mason79154b12007-03-22 15:59:16 -040086 }
Josef Bacik15ee9bc2007-08-10 16:22:09 -040087
Chris Mason79154b12007-03-22 15:59:16 -040088 return 0;
89}
90
Chris Masond352ac62008-09-29 15:18:18 -040091/*
Chris Masond3977122009-01-05 21:25:51 -050092 * this does all the record keeping required to make sure that a reference
93 * counted root is properly recorded in a given transaction. This is required
94 * to make sure the old root from before we joined the transaction is deleted
95 * when the transaction commits
Chris Masond352ac62008-09-29 15:18:18 -040096 */
Chris Masone02119d2008-09-05 16:13:11 -040097noinline int btrfs_record_root_in_trans(struct btrfs_root *root)
Chris Mason6702ed42007-08-07 16:15:09 -040098{
Yan Zhengf321e492008-07-30 09:26:11 -040099 struct btrfs_dirty_root *dirty;
Chris Mason6702ed42007-08-07 16:15:09 -0400100 u64 running_trans_id = root->fs_info->running_transaction->transid;
101 if (root->ref_cows && root->last_trans < running_trans_id) {
102 WARN_ON(root == root->fs_info->extent_root);
103 if (root->root_item.refs != 0) {
104 radix_tree_tag_set(&root->fs_info->fs_roots_radix,
105 (unsigned long)root->root_key.objectid,
106 BTRFS_ROOT_TRANS_TAG);
Yan Zheng31153d82008-07-28 15:32:19 -0400107
108 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
109 BUG_ON(!dirty);
110 dirty->root = kmalloc(sizeof(*dirty->root), GFP_NOFS);
111 BUG_ON(!dirty->root);
Yan Zheng31153d82008-07-28 15:32:19 -0400112 dirty->latest_root = root;
113 INIT_LIST_HEAD(&dirty->list);
Yan Zheng31153d82008-07-28 15:32:19 -0400114
Chris Mason925baed2008-06-25 16:01:30 -0400115 root->commit_root = btrfs_root_node(root);
Yan Zheng31153d82008-07-28 15:32:19 -0400116
117 memcpy(dirty->root, root, sizeof(*root));
118 spin_lock_init(&dirty->root->node_lock);
Yanbcc63ab2008-07-30 16:29:20 -0400119 spin_lock_init(&dirty->root->list_lock);
Yan Zheng31153d82008-07-28 15:32:19 -0400120 mutex_init(&dirty->root->objectid_mutex);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400121 mutex_init(&dirty->root->log_mutex);
Yanbcc63ab2008-07-30 16:29:20 -0400122 INIT_LIST_HEAD(&dirty->root->dead_list);
Yan Zheng31153d82008-07-28 15:32:19 -0400123 dirty->root->node = root->commit_root;
124 dirty->root->commit_root = NULL;
Yanbcc63ab2008-07-30 16:29:20 -0400125
126 spin_lock(&root->list_lock);
127 list_add(&dirty->root->dead_list, &root->dead_list);
128 spin_unlock(&root->list_lock);
129
130 root->dirty_root = dirty;
Chris Mason6702ed42007-08-07 16:15:09 -0400131 } else {
132 WARN_ON(1);
133 }
134 root->last_trans = running_trans_id;
135 }
136 return 0;
137}
138
Chris Masond352ac62008-09-29 15:18:18 -0400139/* wait for commit against the current transaction to become unblocked
140 * when this is done, it is safe to start a new transaction, but the current
141 * transaction might not be fully on disk.
142 */
Chris Mason37d1aee2008-07-31 10:48:37 -0400143static void wait_current_trans(struct btrfs_root *root)
Chris Mason79154b12007-03-22 15:59:16 -0400144{
Chris Masonf9295742008-07-17 12:54:14 -0400145 struct btrfs_transaction *cur_trans;
Chris Mason79154b12007-03-22 15:59:16 -0400146
Chris Masonf9295742008-07-17 12:54:14 -0400147 cur_trans = root->fs_info->running_transaction;
Chris Mason37d1aee2008-07-31 10:48:37 -0400148 if (cur_trans && cur_trans->blocked) {
Chris Masonf9295742008-07-17 12:54:14 -0400149 DEFINE_WAIT(wait);
150 cur_trans->use_count++;
Chris Masond3977122009-01-05 21:25:51 -0500151 while (1) {
Chris Masonf9295742008-07-17 12:54:14 -0400152 prepare_to_wait(&root->fs_info->transaction_wait, &wait,
153 TASK_UNINTERRUPTIBLE);
154 if (cur_trans->blocked) {
155 mutex_unlock(&root->fs_info->trans_mutex);
156 schedule();
157 mutex_lock(&root->fs_info->trans_mutex);
158 finish_wait(&root->fs_info->transaction_wait,
159 &wait);
160 } else {
161 finish_wait(&root->fs_info->transaction_wait,
162 &wait);
163 break;
164 }
165 }
166 put_transaction(cur_trans);
167 }
Chris Mason37d1aee2008-07-31 10:48:37 -0400168}
169
Chris Masone02119d2008-09-05 16:13:11 -0400170static struct btrfs_trans_handle *start_transaction(struct btrfs_root *root,
Sage Weil9ca9ee02008-08-04 10:41:27 -0400171 int num_blocks, int wait)
Chris Mason37d1aee2008-07-31 10:48:37 -0400172{
173 struct btrfs_trans_handle *h =
174 kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
175 int ret;
176
177 mutex_lock(&root->fs_info->trans_mutex);
Chris Mason4bef0842008-09-08 11:18:08 -0400178 if (!root->fs_info->log_root_recovering &&
179 ((wait == 1 && !root->fs_info->open_ioctl_trans) || wait == 2))
Chris Mason37d1aee2008-07-31 10:48:37 -0400180 wait_current_trans(root);
Chris Mason79154b12007-03-22 15:59:16 -0400181 ret = join_transaction(root);
182 BUG_ON(ret);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400183
Chris Masone02119d2008-09-05 16:13:11 -0400184 btrfs_record_root_in_trans(root);
Chris Mason6702ed42007-08-07 16:15:09 -0400185 h->transid = root->fs_info->running_transaction->transid;
Chris Mason79154b12007-03-22 15:59:16 -0400186 h->transaction = root->fs_info->running_transaction;
187 h->blocks_reserved = num_blocks;
188 h->blocks_used = 0;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500189 h->block_group = 0;
Chris Mason26b80032007-08-08 20:17:12 -0400190 h->alloc_exclude_nr = 0;
191 h->alloc_exclude_start = 0;
Chris Mason56bec292009-03-13 10:10:06 -0400192 h->delayed_ref_updates = 0;
Chris Masonb7ec40d2009-03-12 20:12:45 -0400193
Chris Mason79154b12007-03-22 15:59:16 -0400194 root->fs_info->running_transaction->use_count++;
195 mutex_unlock(&root->fs_info->trans_mutex);
196 return h;
197}
198
Chris Masonf9295742008-07-17 12:54:14 -0400199struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
200 int num_blocks)
201{
Sage Weil9ca9ee02008-08-04 10:41:27 -0400202 return start_transaction(root, num_blocks, 1);
Chris Masonf9295742008-07-17 12:54:14 -0400203}
204struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root,
205 int num_blocks)
206{
Sage Weil9ca9ee02008-08-04 10:41:27 -0400207 return start_transaction(root, num_blocks, 0);
Chris Masonf9295742008-07-17 12:54:14 -0400208}
209
Sage Weil9ca9ee02008-08-04 10:41:27 -0400210struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *r,
211 int num_blocks)
212{
213 return start_transaction(r, num_blocks, 2);
214}
215
Chris Masond352ac62008-09-29 15:18:18 -0400216/* wait for a transaction commit to be fully complete */
Chris Mason89ce8a62008-06-25 16:01:31 -0400217static noinline int wait_for_commit(struct btrfs_root *root,
218 struct btrfs_transaction *commit)
219{
220 DEFINE_WAIT(wait);
221 mutex_lock(&root->fs_info->trans_mutex);
Chris Masond3977122009-01-05 21:25:51 -0500222 while (!commit->commit_done) {
Chris Mason89ce8a62008-06-25 16:01:31 -0400223 prepare_to_wait(&commit->commit_wait, &wait,
224 TASK_UNINTERRUPTIBLE);
225 if (commit->commit_done)
226 break;
227 mutex_unlock(&root->fs_info->trans_mutex);
228 schedule();
229 mutex_lock(&root->fs_info->trans_mutex);
230 }
231 mutex_unlock(&root->fs_info->trans_mutex);
232 finish_wait(&commit->commit_wait, &wait);
233 return 0;
234}
235
Chris Masond352ac62008-09-29 15:18:18 -0400236/*
Chris Masond3977122009-01-05 21:25:51 -0500237 * rate limit against the drop_snapshot code. This helps to slow down new
238 * operations if the drop_snapshot code isn't able to keep up.
Chris Masond352ac62008-09-29 15:18:18 -0400239 */
Chris Mason37d1aee2008-07-31 10:48:37 -0400240static void throttle_on_drops(struct btrfs_root *root)
Chris Masonab78c842008-07-29 16:15:18 -0400241{
242 struct btrfs_fs_info *info = root->fs_info;
Chris Mason2dd3e672008-08-04 08:20:15 -0400243 int harder_count = 0;
Chris Masonab78c842008-07-29 16:15:18 -0400244
Chris Mason2dd3e672008-08-04 08:20:15 -0400245harder:
Chris Masonab78c842008-07-29 16:15:18 -0400246 if (atomic_read(&info->throttles)) {
247 DEFINE_WAIT(wait);
248 int thr;
Chris Masonab78c842008-07-29 16:15:18 -0400249 thr = atomic_read(&info->throttle_gen);
250
251 do {
252 prepare_to_wait(&info->transaction_throttle,
253 &wait, TASK_UNINTERRUPTIBLE);
254 if (!atomic_read(&info->throttles)) {
255 finish_wait(&info->transaction_throttle, &wait);
256 break;
257 }
258 schedule();
259 finish_wait(&info->transaction_throttle, &wait);
260 } while (thr == atomic_read(&info->throttle_gen));
Chris Mason2dd3e672008-08-04 08:20:15 -0400261 harder_count++;
262
263 if (root->fs_info->total_ref_cache_size > 1 * 1024 * 1024 &&
264 harder_count < 2)
265 goto harder;
266
267 if (root->fs_info->total_ref_cache_size > 5 * 1024 * 1024 &&
268 harder_count < 10)
269 goto harder;
270
271 if (root->fs_info->total_ref_cache_size > 10 * 1024 * 1024 &&
272 harder_count < 20)
273 goto harder;
Chris Masonab78c842008-07-29 16:15:18 -0400274 }
275}
276
Chris Mason37d1aee2008-07-31 10:48:37 -0400277void btrfs_throttle(struct btrfs_root *root)
278{
279 mutex_lock(&root->fs_info->trans_mutex);
Sage Weil9ca9ee02008-08-04 10:41:27 -0400280 if (!root->fs_info->open_ioctl_trans)
281 wait_current_trans(root);
Chris Mason37d1aee2008-07-31 10:48:37 -0400282 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason37d1aee2008-07-31 10:48:37 -0400283 throttle_on_drops(root);
284}
285
Chris Mason89ce8a62008-06-25 16:01:31 -0400286static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
287 struct btrfs_root *root, int throttle)
Chris Mason79154b12007-03-22 15:59:16 -0400288{
289 struct btrfs_transaction *cur_trans;
Chris Masonab78c842008-07-29 16:15:18 -0400290 struct btrfs_fs_info *info = root->fs_info;
Chris Masonc3e69d52009-03-13 10:17:05 -0400291 int count = 0;
Chris Masond6e4a422007-04-06 15:37:36 -0400292
Chris Masonc3e69d52009-03-13 10:17:05 -0400293 while (count < 4) {
294 unsigned long cur = trans->delayed_ref_updates;
295 trans->delayed_ref_updates = 0;
296 if (cur &&
297 trans->transaction->delayed_refs.num_heads_ready > 64) {
298 trans->delayed_ref_updates = 0;
Chris Masonb7ec40d2009-03-12 20:12:45 -0400299
300 /*
301 * do a full flush if the transaction is trying
302 * to close
303 */
304 if (trans->transaction->delayed_refs.flushing)
305 cur = 0;
Chris Masonc3e69d52009-03-13 10:17:05 -0400306 btrfs_run_delayed_refs(trans, root, cur);
307 } else {
308 break;
309 }
310 count++;
Chris Mason56bec292009-03-13 10:10:06 -0400311 }
312
Chris Masonab78c842008-07-29 16:15:18 -0400313 mutex_lock(&info->trans_mutex);
314 cur_trans = info->running_transaction;
Chris Masonccd467d2007-06-28 15:57:36 -0400315 WARN_ON(cur_trans != trans->transaction);
Chris Masond5719762007-03-23 10:01:08 -0400316 WARN_ON(cur_trans->num_writers < 1);
Chris Masonccd467d2007-06-28 15:57:36 -0400317 cur_trans->num_writers--;
Chris Mason89ce8a62008-06-25 16:01:31 -0400318
Chris Mason79154b12007-03-22 15:59:16 -0400319 if (waitqueue_active(&cur_trans->writer_wait))
320 wake_up(&cur_trans->writer_wait);
Chris Mason79154b12007-03-22 15:59:16 -0400321 put_transaction(cur_trans);
Chris Masonab78c842008-07-29 16:15:18 -0400322 mutex_unlock(&info->trans_mutex);
Chris Masond6025572007-03-30 14:27:56 -0400323 memset(trans, 0, sizeof(*trans));
Chris Mason2c90e5d2007-04-02 10:50:19 -0400324 kmem_cache_free(btrfs_trans_handle_cachep, trans);
Chris Masonab78c842008-07-29 16:15:18 -0400325
326 if (throttle)
Chris Mason37d1aee2008-07-31 10:48:37 -0400327 throttle_on_drops(root);
Chris Masonab78c842008-07-29 16:15:18 -0400328
Chris Mason79154b12007-03-22 15:59:16 -0400329 return 0;
330}
331
Chris Mason89ce8a62008-06-25 16:01:31 -0400332int btrfs_end_transaction(struct btrfs_trans_handle *trans,
333 struct btrfs_root *root)
334{
335 return __btrfs_end_transaction(trans, root, 0);
336}
337
338int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
339 struct btrfs_root *root)
340{
341 return __btrfs_end_transaction(trans, root, 1);
342}
343
Chris Masond352ac62008-09-29 15:18:18 -0400344/*
345 * when btree blocks are allocated, they have some corresponding bits set for
346 * them in one of two extent_io trees. This is used to make sure all of
347 * those extents are on disk for transaction or log commit
348 */
Chris Masond0c803c2008-09-11 16:17:57 -0400349int btrfs_write_and_wait_marked_extents(struct btrfs_root *root,
350 struct extent_io_tree *dirty_pages)
Chris Mason79154b12007-03-22 15:59:16 -0400351{
Chris Mason7c4452b2007-04-28 09:29:35 -0400352 int ret;
Chris Mason777e6bd2008-08-15 15:34:15 -0400353 int err = 0;
Chris Mason7c4452b2007-04-28 09:29:35 -0400354 int werr = 0;
355 struct page *page;
Chris Mason7c4452b2007-04-28 09:29:35 -0400356 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Mason777e6bd2008-08-15 15:34:15 -0400357 u64 start = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400358 u64 end;
359 unsigned long index;
Chris Mason7c4452b2007-04-28 09:29:35 -0400360
Chris Masond3977122009-01-05 21:25:51 -0500361 while (1) {
Chris Mason777e6bd2008-08-15 15:34:15 -0400362 ret = find_first_extent_bit(dirty_pages, start, &start, &end,
Chris Mason5f39d392007-10-15 16:14:19 -0400363 EXTENT_DIRTY);
364 if (ret)
Chris Mason7c4452b2007-04-28 09:29:35 -0400365 break;
Chris Masond3977122009-01-05 21:25:51 -0500366 while (start <= end) {
Chris Mason777e6bd2008-08-15 15:34:15 -0400367 cond_resched();
368
Chris Mason5f39d392007-10-15 16:14:19 -0400369 index = start >> PAGE_CACHE_SHIFT;
Chris Mason35ebb932007-10-30 16:56:53 -0400370 start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
Chris Mason4bef0842008-09-08 11:18:08 -0400371 page = find_get_page(btree_inode->i_mapping, index);
Chris Mason7c4452b2007-04-28 09:29:35 -0400372 if (!page)
373 continue;
Chris Mason4bef0842008-09-08 11:18:08 -0400374
375 btree_lock_page_hook(page);
376 if (!page->mapping) {
377 unlock_page(page);
378 page_cache_release(page);
379 continue;
380 }
381
Chris Mason6702ed42007-08-07 16:15:09 -0400382 if (PageWriteback(page)) {
383 if (PageDirty(page))
384 wait_on_page_writeback(page);
385 else {
386 unlock_page(page);
387 page_cache_release(page);
388 continue;
389 }
390 }
Chris Mason7c4452b2007-04-28 09:29:35 -0400391 err = write_one_page(page, 0);
392 if (err)
393 werr = err;
394 page_cache_release(page);
395 }
396 }
Chris Masond3977122009-01-05 21:25:51 -0500397 while (1) {
Chris Mason777e6bd2008-08-15 15:34:15 -0400398 ret = find_first_extent_bit(dirty_pages, 0, &start, &end,
399 EXTENT_DIRTY);
400 if (ret)
401 break;
402
403 clear_extent_dirty(dirty_pages, start, end, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -0500404 while (start <= end) {
Chris Mason777e6bd2008-08-15 15:34:15 -0400405 index = start >> PAGE_CACHE_SHIFT;
406 start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
407 page = find_get_page(btree_inode->i_mapping, index);
408 if (!page)
409 continue;
410 if (PageDirty(page)) {
Chris Mason4bef0842008-09-08 11:18:08 -0400411 btree_lock_page_hook(page);
412 wait_on_page_writeback(page);
Chris Mason777e6bd2008-08-15 15:34:15 -0400413 err = write_one_page(page, 0);
414 if (err)
415 werr = err;
416 }
Chris Mason105d9312008-11-18 12:13:12 -0500417 wait_on_page_writeback(page);
Chris Mason777e6bd2008-08-15 15:34:15 -0400418 page_cache_release(page);
419 cond_resched();
420 }
421 }
Chris Mason7c4452b2007-04-28 09:29:35 -0400422 if (err)
423 werr = err;
424 return werr;
Chris Mason79154b12007-03-22 15:59:16 -0400425}
426
Chris Masond0c803c2008-09-11 16:17:57 -0400427int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
428 struct btrfs_root *root)
429{
430 if (!trans || !trans->transaction) {
431 struct inode *btree_inode;
432 btree_inode = root->fs_info->btree_inode;
433 return filemap_write_and_wait(btree_inode->i_mapping);
434 }
435 return btrfs_write_and_wait_marked_extents(root,
436 &trans->transaction->dirty_pages);
437}
438
Chris Masond352ac62008-09-29 15:18:18 -0400439/*
440 * this is used to update the root pointer in the tree of tree roots.
441 *
442 * But, in the case of the extent allocation tree, updating the root
443 * pointer may allocate blocks which may change the root of the extent
444 * allocation tree.
445 *
446 * So, this loops and repeats and makes sure the cowonly root didn't
447 * change while the root pointer was being updated in the metadata.
448 */
Chris Mason0b86a832008-03-24 15:01:56 -0400449static int update_cowonly_root(struct btrfs_trans_handle *trans,
450 struct btrfs_root *root)
451{
452 int ret;
453 u64 old_root_bytenr;
454 struct btrfs_root *tree_root = root->fs_info->tree_root;
455
456 btrfs_write_dirty_block_groups(trans, root);
Chris Mason56bec292009-03-13 10:10:06 -0400457
458 ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
459 BUG_ON(ret);
Chris Mason87ef2bb2008-10-30 11:23:27 -0400460
Chris Masond3977122009-01-05 21:25:51 -0500461 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -0400462 old_root_bytenr = btrfs_root_bytenr(&root->root_item);
463 if (old_root_bytenr == root->node->start)
464 break;
465 btrfs_set_root_bytenr(&root->root_item,
466 root->node->start);
467 btrfs_set_root_level(&root->root_item,
468 btrfs_header_level(root->node));
Yan Zheng84234f32008-10-29 14:49:05 -0400469 btrfs_set_root_generation(&root->root_item, trans->transid);
Chris Mason87ef2bb2008-10-30 11:23:27 -0400470
Chris Mason0b86a832008-03-24 15:01:56 -0400471 ret = btrfs_update_root(trans, tree_root,
472 &root->root_key,
473 &root->root_item);
474 BUG_ON(ret);
475 btrfs_write_dirty_block_groups(trans, root);
Chris Mason56bec292009-03-13 10:10:06 -0400476
477 ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
478 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -0400479 }
480 return 0;
481}
482
Chris Masond352ac62008-09-29 15:18:18 -0400483/*
484 * update all the cowonly tree roots on disk
485 */
Chris Mason79154b12007-03-22 15:59:16 -0400486int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans,
487 struct btrfs_root *root)
488{
Chris Mason79154b12007-03-22 15:59:16 -0400489 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason0b86a832008-03-24 15:01:56 -0400490 struct list_head *next;
Yan Zheng84234f32008-10-29 14:49:05 -0400491 struct extent_buffer *eb;
Chris Mason56bec292009-03-13 10:10:06 -0400492 int ret;
Yan Zheng84234f32008-10-29 14:49:05 -0400493
Chris Mason56bec292009-03-13 10:10:06 -0400494 ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
495 BUG_ON(ret);
Chris Mason87ef2bb2008-10-30 11:23:27 -0400496
Yan Zheng84234f32008-10-29 14:49:05 -0400497 eb = btrfs_lock_root_node(fs_info->tree_root);
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400498 btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 0, &eb);
Yan Zheng84234f32008-10-29 14:49:05 -0400499 btrfs_tree_unlock(eb);
500 free_extent_buffer(eb);
Chris Mason79154b12007-03-22 15:59:16 -0400501
Chris Mason56bec292009-03-13 10:10:06 -0400502 ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
503 BUG_ON(ret);
Chris Mason87ef2bb2008-10-30 11:23:27 -0400504
Chris Masond3977122009-01-05 21:25:51 -0500505 while (!list_empty(&fs_info->dirty_cowonly_roots)) {
Chris Mason0b86a832008-03-24 15:01:56 -0400506 next = fs_info->dirty_cowonly_roots.next;
507 list_del_init(next);
508 root = list_entry(next, struct btrfs_root, dirty_list);
Chris Mason87ef2bb2008-10-30 11:23:27 -0400509
Chris Mason0b86a832008-03-24 15:01:56 -0400510 update_cowonly_root(trans, root);
Chris Mason56bec292009-03-13 10:10:06 -0400511
512 ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
513 BUG_ON(ret);
Chris Mason79154b12007-03-22 15:59:16 -0400514 }
515 return 0;
516}
517
Chris Masond352ac62008-09-29 15:18:18 -0400518/*
519 * dead roots are old snapshots that need to be deleted. This allocates
520 * a dirty root struct and adds it into the list of dead roots that need to
521 * be deleted
522 */
Yan Zhengb48652c2008-08-04 23:23:47 -0400523int btrfs_add_dead_root(struct btrfs_root *root, struct btrfs_root *latest)
Chris Mason5eda7b52007-06-22 14:16:25 -0400524{
Yan Zhengf321e492008-07-30 09:26:11 -0400525 struct btrfs_dirty_root *dirty;
Chris Mason5eda7b52007-06-22 14:16:25 -0400526
527 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
528 if (!dirty)
529 return -ENOMEM;
Chris Mason5eda7b52007-06-22 14:16:25 -0400530 dirty->root = root;
Chris Mason5ce14bb2007-09-11 11:15:39 -0400531 dirty->latest_root = latest;
Yan Zhengb48652c2008-08-04 23:23:47 -0400532
533 mutex_lock(&root->fs_info->trans_mutex);
534 list_add(&dirty->list, &latest->fs_info->dead_roots);
535 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason5eda7b52007-06-22 14:16:25 -0400536 return 0;
537}
538
Chris Masond352ac62008-09-29 15:18:18 -0400539/*
540 * at transaction commit time we need to schedule the old roots for
541 * deletion via btrfs_drop_snapshot. This runs through all the
542 * reference counted roots that were modified in the current
543 * transaction and puts them into the drop list
544 */
Chris Mason80b67942008-02-01 16:35:04 -0500545static noinline int add_dirty_roots(struct btrfs_trans_handle *trans,
546 struct radix_tree_root *radix,
547 struct list_head *list)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400548{
Yan Zhengf321e492008-07-30 09:26:11 -0400549 struct btrfs_dirty_root *dirty;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400550 struct btrfs_root *gang[8];
551 struct btrfs_root *root;
552 int i;
553 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400554 int err = 0;
Chris Mason5eda7b52007-06-22 14:16:25 -0400555 u32 refs;
Chris Mason54aa1f42007-06-22 14:16:25 -0400556
Chris Masond3977122009-01-05 21:25:51 -0500557 while (1) {
Chris Mason0f7d52f2007-04-09 10:42:37 -0400558 ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0,
559 ARRAY_SIZE(gang),
560 BTRFS_ROOT_TRANS_TAG);
561 if (ret == 0)
562 break;
563 for (i = 0; i < ret; i++) {
564 root = gang[i];
Chris Mason2619ba12007-04-10 16:58:11 -0400565 radix_tree_tag_clear(radix,
566 (unsigned long)root->root_key.objectid,
567 BTRFS_ROOT_TRANS_TAG);
Yan Zheng31153d82008-07-28 15:32:19 -0400568
569 BUG_ON(!root->ref_tree);
Chris Mason017e5362008-07-28 15:32:51 -0400570 dirty = root->dirty_root;
Yan Zheng31153d82008-07-28 15:32:19 -0400571
Chris Masone02119d2008-09-05 16:13:11 -0400572 btrfs_free_log(trans, root);
Yan Zhengf82d02d2008-10-29 14:49:05 -0400573 btrfs_free_reloc_root(trans, root);
Chris Masone02119d2008-09-05 16:13:11 -0400574
Chris Mason0f7d52f2007-04-09 10:42:37 -0400575 if (root->commit_root == root->node) {
Chris Masondb945352007-10-15 16:15:53 -0400576 WARN_ON(root->node->start !=
577 btrfs_root_bytenr(&root->root_item));
Yan Zheng31153d82008-07-28 15:32:19 -0400578
Chris Mason5f39d392007-10-15 16:14:19 -0400579 free_extent_buffer(root->commit_root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400580 root->commit_root = NULL;
Yan Zheng7ea394f2008-08-05 13:05:02 -0400581 root->dirty_root = NULL;
Yanbcc63ab2008-07-30 16:29:20 -0400582
583 spin_lock(&root->list_lock);
584 list_del_init(&dirty->root->dead_list);
585 spin_unlock(&root->list_lock);
586
Yan Zheng31153d82008-07-28 15:32:19 -0400587 kfree(dirty->root);
588 kfree(dirty);
Josef Bacik58176a92007-08-29 15:47:34 -0400589
590 /* make sure to update the root on disk
591 * so we get any updates to the block used
592 * counts
593 */
594 err = btrfs_update_root(trans,
595 root->fs_info->tree_root,
596 &root->root_key,
597 &root->root_item);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400598 continue;
599 }
Chris Mason9f3a7422007-08-07 15:52:19 -0400600
601 memset(&root->root_item.drop_progress, 0,
602 sizeof(struct btrfs_disk_key));
603 root->root_item.drop_level = 0;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400604 root->commit_root = NULL;
Yan Zheng7ea394f2008-08-05 13:05:02 -0400605 root->dirty_root = NULL;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400606 root->root_key.offset = root->fs_info->generation;
Chris Masondb945352007-10-15 16:15:53 -0400607 btrfs_set_root_bytenr(&root->root_item,
608 root->node->start);
609 btrfs_set_root_level(&root->root_item,
610 btrfs_header_level(root->node));
Yan Zheng84234f32008-10-29 14:49:05 -0400611 btrfs_set_root_generation(&root->root_item,
612 root->root_key.offset);
613
Chris Mason0f7d52f2007-04-09 10:42:37 -0400614 err = btrfs_insert_root(trans, root->fs_info->tree_root,
615 &root->root_key,
616 &root->root_item);
Chris Mason54aa1f42007-06-22 14:16:25 -0400617 if (err)
618 break;
Chris Mason9f3a7422007-08-07 15:52:19 -0400619
620 refs = btrfs_root_refs(&dirty->root->root_item);
621 btrfs_set_root_refs(&dirty->root->root_item, refs - 1);
Chris Mason5eda7b52007-06-22 14:16:25 -0400622 err = btrfs_update_root(trans, root->fs_info->tree_root,
Chris Mason9f3a7422007-08-07 15:52:19 -0400623 &dirty->root->root_key,
624 &dirty->root->root_item);
Chris Mason5eda7b52007-06-22 14:16:25 -0400625
626 BUG_ON(err);
Chris Mason9f3a7422007-08-07 15:52:19 -0400627 if (refs == 1) {
Chris Mason5eda7b52007-06-22 14:16:25 -0400628 list_add(&dirty->list, list);
Chris Mason9f3a7422007-08-07 15:52:19 -0400629 } else {
630 WARN_ON(1);
Yan Zheng31153d82008-07-28 15:32:19 -0400631 free_extent_buffer(dirty->root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -0400632 kfree(dirty->root);
Chris Mason5eda7b52007-06-22 14:16:25 -0400633 kfree(dirty);
Chris Mason9f3a7422007-08-07 15:52:19 -0400634 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400635 }
636 }
Chris Mason54aa1f42007-06-22 14:16:25 -0400637 return err;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400638}
639
Chris Masond352ac62008-09-29 15:18:18 -0400640/*
641 * defrag a given btree. If cacheonly == 1, this won't read from the disk,
642 * otherwise every leaf in the btree is read and defragged.
643 */
Chris Masone9d0b132007-08-10 14:06:19 -0400644int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
645{
646 struct btrfs_fs_info *info = root->fs_info;
647 int ret;
648 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400649 unsigned long nr;
Chris Masone9d0b132007-08-10 14:06:19 -0400650
Chris Masona2135012008-06-25 16:01:30 -0400651 smp_mb();
Chris Masone9d0b132007-08-10 14:06:19 -0400652 if (root->defrag_running)
653 return 0;
Chris Masone9d0b132007-08-10 14:06:19 -0400654 trans = btrfs_start_transaction(root, 1);
Chris Mason6b800532007-10-15 16:17:34 -0400655 while (1) {
Chris Masone9d0b132007-08-10 14:06:19 -0400656 root->defrag_running = 1;
657 ret = btrfs_defrag_leaves(trans, root, cacheonly);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400658 nr = trans->blocks_used;
Chris Masone9d0b132007-08-10 14:06:19 -0400659 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400660 btrfs_btree_balance_dirty(info->tree_root, nr);
Chris Masone9d0b132007-08-10 14:06:19 -0400661 cond_resched();
662
Chris Masone9d0b132007-08-10 14:06:19 -0400663 trans = btrfs_start_transaction(root, 1);
Chris Mason3f157a22008-06-25 16:01:31 -0400664 if (root->fs_info->closing || ret != -EAGAIN)
Chris Masone9d0b132007-08-10 14:06:19 -0400665 break;
666 }
667 root->defrag_running = 0;
Chris Masona2135012008-06-25 16:01:30 -0400668 smp_mb();
Chris Masone9d0b132007-08-10 14:06:19 -0400669 btrfs_end_transaction(trans, root);
670 return 0;
671}
672
Chris Masond352ac62008-09-29 15:18:18 -0400673/*
Chris Masonb7ec40d2009-03-12 20:12:45 -0400674 * when dropping snapshots, we generate a ton of delayed refs, and it makes
675 * sense not to join the transaction while it is trying to flush the current
676 * queue of delayed refs out.
677 *
678 * This is used by the drop snapshot code only
679 */
680static noinline int wait_transaction_pre_flush(struct btrfs_fs_info *info)
681{
682 DEFINE_WAIT(wait);
683
684 mutex_lock(&info->trans_mutex);
685 while (info->running_transaction &&
686 info->running_transaction->delayed_refs.flushing) {
687 prepare_to_wait(&info->transaction_wait, &wait,
688 TASK_UNINTERRUPTIBLE);
689 mutex_unlock(&info->trans_mutex);
690 schedule();
691 mutex_lock(&info->trans_mutex);
692 finish_wait(&info->transaction_wait, &wait);
693 }
694 mutex_unlock(&info->trans_mutex);
695 return 0;
696}
697
698/*
Chris Masond352ac62008-09-29 15:18:18 -0400699 * Given a list of roots that need to be deleted, call btrfs_drop_snapshot on
700 * all of them
701 */
Chris Mason80b67942008-02-01 16:35:04 -0500702static noinline int drop_dirty_roots(struct btrfs_root *tree_root,
703 struct list_head *list)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400704{
Yan Zhengf321e492008-07-30 09:26:11 -0400705 struct btrfs_dirty_root *dirty;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400706 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400707 unsigned long nr;
Chris Masondb945352007-10-15 16:15:53 -0400708 u64 num_bytes;
709 u64 bytes_used;
Yanbcc63ab2008-07-30 16:29:20 -0400710 u64 max_useless;
Chris Mason54aa1f42007-06-22 14:16:25 -0400711 int ret = 0;
Chris Mason9f3a7422007-08-07 15:52:19 -0400712 int err;
713
Chris Masond3977122009-01-05 21:25:51 -0500714 while (!list_empty(list)) {
Josef Bacik58176a92007-08-29 15:47:34 -0400715 struct btrfs_root *root;
716
Yan Zhengf321e492008-07-30 09:26:11 -0400717 dirty = list_entry(list->prev, struct btrfs_dirty_root, list);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400718 list_del_init(&dirty->list);
Chris Mason5eda7b52007-06-22 14:16:25 -0400719
Chris Masondb945352007-10-15 16:15:53 -0400720 num_bytes = btrfs_root_used(&dirty->root->root_item);
Josef Bacik58176a92007-08-29 15:47:34 -0400721 root = dirty->latest_root;
Chris Masona2135012008-06-25 16:01:30 -0400722 atomic_inc(&root->fs_info->throttles);
Josef Bacik58176a92007-08-29 15:47:34 -0400723
Chris Masond3977122009-01-05 21:25:51 -0500724 while (1) {
Chris Masonb7ec40d2009-03-12 20:12:45 -0400725 /*
726 * we don't want to jump in and create a bunch of
727 * delayed refs if the transaction is starting to close
728 */
729 wait_transaction_pre_flush(tree_root->fs_info);
Chris Mason9f3a7422007-08-07 15:52:19 -0400730 trans = btrfs_start_transaction(tree_root, 1);
Chris Masonb7ec40d2009-03-12 20:12:45 -0400731
732 /*
733 * we've joined a transaction, make sure it isn't
734 * closing right now
735 */
736 if (trans->transaction->delayed_refs.flushing) {
737 btrfs_end_transaction(trans, tree_root);
738 continue;
739 }
740
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400741 mutex_lock(&root->fs_info->drop_mutex);
Chris Mason9f3a7422007-08-07 15:52:19 -0400742 ret = btrfs_drop_snapshot(trans, dirty->root);
Chris Masond3977122009-01-05 21:25:51 -0500743 if (ret != -EAGAIN)
Chris Mason9f3a7422007-08-07 15:52:19 -0400744 break;
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400745 mutex_unlock(&root->fs_info->drop_mutex);
Josef Bacik58176a92007-08-29 15:47:34 -0400746
Chris Mason9f3a7422007-08-07 15:52:19 -0400747 err = btrfs_update_root(trans,
748 tree_root,
749 &dirty->root->root_key,
750 &dirty->root->root_item);
751 if (err)
752 ret = err;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400753 nr = trans->blocks_used;
Chris Mason017e5362008-07-28 15:32:51 -0400754 ret = btrfs_end_transaction(trans, tree_root);
Chris Mason9f3a7422007-08-07 15:52:19 -0400755 BUG_ON(ret);
Chris Masona2135012008-06-25 16:01:30 -0400756
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400757 btrfs_btree_balance_dirty(tree_root, nr);
Chris Mason4dc119042007-10-15 16:18:14 -0400758 cond_resched();
Chris Mason9f3a7422007-08-07 15:52:19 -0400759 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400760 BUG_ON(ret);
Chris Masona2135012008-06-25 16:01:30 -0400761 atomic_dec(&root->fs_info->throttles);
Chris Mason017e5362008-07-28 15:32:51 -0400762 wake_up(&root->fs_info->transaction_throttle);
Josef Bacik58176a92007-08-29 15:47:34 -0400763
Chris Masondb945352007-10-15 16:15:53 -0400764 num_bytes -= btrfs_root_used(&dirty->root->root_item);
765 bytes_used = btrfs_root_used(&root->root_item);
766 if (num_bytes) {
Yan Zheng24562422009-02-12 14:14:53 -0500767 mutex_lock(&root->fs_info->trans_mutex);
Chris Masone02119d2008-09-05 16:13:11 -0400768 btrfs_record_root_in_trans(root);
Yan Zheng24562422009-02-12 14:14:53 -0500769 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason5f39d392007-10-15 16:14:19 -0400770 btrfs_set_root_used(&root->root_item,
Chris Masondb945352007-10-15 16:15:53 -0400771 bytes_used - num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -0400772 }
Chris Masona2135012008-06-25 16:01:30 -0400773
Chris Mason9f3a7422007-08-07 15:52:19 -0400774 ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key);
Josef Bacik58176a92007-08-29 15:47:34 -0400775 if (ret) {
776 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -0400777 break;
Josef Bacik58176a92007-08-29 15:47:34 -0400778 }
Chris Masona2135012008-06-25 16:01:30 -0400779 mutex_unlock(&root->fs_info->drop_mutex);
780
Yanbcc63ab2008-07-30 16:29:20 -0400781 spin_lock(&root->list_lock);
782 list_del_init(&dirty->root->dead_list);
783 if (!list_empty(&root->dead_list)) {
784 struct btrfs_root *oldest;
785 oldest = list_entry(root->dead_list.prev,
786 struct btrfs_root, dead_list);
787 max_useless = oldest->root_key.offset - 1;
788 } else {
789 max_useless = root->root_key.offset - 1;
790 }
791 spin_unlock(&root->list_lock);
792
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400793 nr = trans->blocks_used;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400794 ret = btrfs_end_transaction(trans, tree_root);
795 BUG_ON(ret);
Chris Mason5eda7b52007-06-22 14:16:25 -0400796
Zheng Yane4657682008-09-26 10:04:53 -0400797 ret = btrfs_remove_leaf_refs(root, max_useless, 0);
Yanbcc63ab2008-07-30 16:29:20 -0400798 BUG_ON(ret);
799
Chris Masonf510cfe2007-10-15 16:14:48 -0400800 free_extent_buffer(dirty->root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -0400801 kfree(dirty->root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400802 kfree(dirty);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400803
804 btrfs_btree_balance_dirty(tree_root, nr);
Chris Mason4dc119042007-10-15 16:18:14 -0400805 cond_resched();
Chris Mason0f7d52f2007-04-09 10:42:37 -0400806 }
Chris Mason54aa1f42007-06-22 14:16:25 -0400807 return ret;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400808}
809
Chris Masond352ac62008-09-29 15:18:18 -0400810/*
811 * new snapshots need to be created at a very specific time in the
812 * transaction commit. This does the actual creation
813 */
Chris Mason80b67942008-02-01 16:35:04 -0500814static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
Chris Mason3063d292008-01-08 15:46:30 -0500815 struct btrfs_fs_info *fs_info,
816 struct btrfs_pending_snapshot *pending)
817{
818 struct btrfs_key key;
Chris Mason80b67942008-02-01 16:35:04 -0500819 struct btrfs_root_item *new_root_item;
Chris Mason3063d292008-01-08 15:46:30 -0500820 struct btrfs_root *tree_root = fs_info->tree_root;
821 struct btrfs_root *root = pending->root;
822 struct extent_buffer *tmp;
Chris Mason925baed2008-06-25 16:01:30 -0400823 struct extent_buffer *old;
Chris Mason3063d292008-01-08 15:46:30 -0500824 int ret;
825 u64 objectid;
826
Chris Mason80b67942008-02-01 16:35:04 -0500827 new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
828 if (!new_root_item) {
829 ret = -ENOMEM;
830 goto fail;
831 }
Chris Mason3063d292008-01-08 15:46:30 -0500832 ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid);
833 if (ret)
834 goto fail;
835
Yan Zheng80ff3852008-10-30 14:20:02 -0400836 btrfs_record_root_in_trans(root);
837 btrfs_set_root_last_snapshot(&root->root_item, trans->transid);
Chris Mason80b67942008-02-01 16:35:04 -0500838 memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
Chris Mason3063d292008-01-08 15:46:30 -0500839
840 key.objectid = objectid;
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400841 key.offset = trans->transid;
Chris Mason3063d292008-01-08 15:46:30 -0500842 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
843
Chris Mason925baed2008-06-25 16:01:30 -0400844 old = btrfs_lock_root_node(root);
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400845 btrfs_cow_block(trans, root, old, NULL, 0, &old);
Chris Mason3063d292008-01-08 15:46:30 -0500846
Chris Mason925baed2008-06-25 16:01:30 -0400847 btrfs_copy_root(trans, root, old, &tmp, objectid);
848 btrfs_tree_unlock(old);
849 free_extent_buffer(old);
Chris Mason3063d292008-01-08 15:46:30 -0500850
Chris Mason80b67942008-02-01 16:35:04 -0500851 btrfs_set_root_bytenr(new_root_item, tmp->start);
852 btrfs_set_root_level(new_root_item, btrfs_header_level(tmp));
Yan Zheng84234f32008-10-29 14:49:05 -0400853 btrfs_set_root_generation(new_root_item, trans->transid);
Chris Mason3063d292008-01-08 15:46:30 -0500854 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
Chris Mason80b67942008-02-01 16:35:04 -0500855 new_root_item);
Chris Mason925baed2008-06-25 16:01:30 -0400856 btrfs_tree_unlock(tmp);
Chris Mason3063d292008-01-08 15:46:30 -0500857 free_extent_buffer(tmp);
858 if (ret)
859 goto fail;
860
Chris Mason3de45862008-11-17 21:02:50 -0500861 key.offset = (u64)-1;
862 memcpy(&pending->root_key, &key, sizeof(key));
863fail:
864 kfree(new_root_item);
865 return ret;
866}
867
868static noinline int finish_pending_snapshot(struct btrfs_fs_info *fs_info,
869 struct btrfs_pending_snapshot *pending)
870{
871 int ret;
872 int namelen;
873 u64 index = 0;
874 struct btrfs_trans_handle *trans;
875 struct inode *parent_inode;
876 struct inode *inode;
Chris Mason0660b5a2008-11-17 20:37:39 -0500877 struct btrfs_root *parent_root;
Chris Mason3de45862008-11-17 21:02:50 -0500878
Chris Mason3394e162008-11-17 20:42:26 -0500879 parent_inode = pending->dentry->d_parent->d_inode;
Chris Mason0660b5a2008-11-17 20:37:39 -0500880 parent_root = BTRFS_I(parent_inode)->root;
Yan Zheng180591b2009-01-06 09:58:06 -0500881 trans = btrfs_join_transaction(parent_root, 1);
Chris Mason3de45862008-11-17 21:02:50 -0500882
Chris Mason3063d292008-01-08 15:46:30 -0500883 /*
884 * insert the directory item
885 */
Sven Wegener3b963622008-06-09 21:57:42 -0400886 namelen = strlen(pending->name);
Chris Mason3de45862008-11-17 21:02:50 -0500887 ret = btrfs_set_inode_index(parent_inode, &index);
Chris Mason0660b5a2008-11-17 20:37:39 -0500888 ret = btrfs_insert_dir_item(trans, parent_root,
Chris Mason3de45862008-11-17 21:02:50 -0500889 pending->name, namelen,
890 parent_inode->i_ino,
891 &pending->root_key, BTRFS_FT_DIR, index);
Chris Mason3063d292008-01-08 15:46:30 -0500892
893 if (ret)
894 goto fail;
Chris Mason0660b5a2008-11-17 20:37:39 -0500895
Yan Zheng52c26172009-01-05 15:43:43 -0500896 btrfs_i_size_write(parent_inode, parent_inode->i_size + namelen * 2);
897 ret = btrfs_update_inode(trans, parent_root, parent_inode);
898 BUG_ON(ret);
899
Chris Mason0660b5a2008-11-17 20:37:39 -0500900 /* add the backref first */
901 ret = btrfs_add_root_ref(trans, parent_root->fs_info->tree_root,
902 pending->root_key.objectid,
903 BTRFS_ROOT_BACKREF_KEY,
904 parent_root->root_key.objectid,
905 parent_inode->i_ino, index, pending->name,
906 namelen);
907
908 BUG_ON(ret);
909
910 /* now add the forward ref */
911 ret = btrfs_add_root_ref(trans, parent_root->fs_info->tree_root,
912 parent_root->root_key.objectid,
913 BTRFS_ROOT_REF_KEY,
914 pending->root_key.objectid,
915 parent_inode->i_ino, index, pending->name,
916 namelen);
917
Chris Mason3de45862008-11-17 21:02:50 -0500918 inode = btrfs_lookup_dentry(parent_inode, pending->dentry);
919 d_instantiate(pending->dentry, inode);
Chris Mason3063d292008-01-08 15:46:30 -0500920fail:
Chris Mason3de45862008-11-17 21:02:50 -0500921 btrfs_end_transaction(trans, fs_info->fs_root);
Chris Mason3063d292008-01-08 15:46:30 -0500922 return ret;
923}
924
Chris Masond352ac62008-09-29 15:18:18 -0400925/*
926 * create all the snapshots we've scheduled for creation
927 */
Chris Mason80b67942008-02-01 16:35:04 -0500928static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
929 struct btrfs_fs_info *fs_info)
Chris Mason3063d292008-01-08 15:46:30 -0500930{
931 struct btrfs_pending_snapshot *pending;
932 struct list_head *head = &trans->transaction->pending_snapshots;
Chris Mason3de45862008-11-17 21:02:50 -0500933 int ret;
934
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500935 list_for_each_entry(pending, head, list) {
Chris Mason3de45862008-11-17 21:02:50 -0500936 ret = create_pending_snapshot(trans, fs_info, pending);
937 BUG_ON(ret);
938 }
939 return 0;
940}
941
942static noinline int finish_pending_snapshots(struct btrfs_trans_handle *trans,
943 struct btrfs_fs_info *fs_info)
944{
945 struct btrfs_pending_snapshot *pending;
946 struct list_head *head = &trans->transaction->pending_snapshots;
Chris Mason3063d292008-01-08 15:46:30 -0500947 int ret;
948
Chris Masond3977122009-01-05 21:25:51 -0500949 while (!list_empty(head)) {
Chris Mason3063d292008-01-08 15:46:30 -0500950 pending = list_entry(head->next,
951 struct btrfs_pending_snapshot, list);
Chris Mason3de45862008-11-17 21:02:50 -0500952 ret = finish_pending_snapshot(fs_info, pending);
Chris Mason3063d292008-01-08 15:46:30 -0500953 BUG_ON(ret);
954 list_del(&pending->list);
955 kfree(pending->name);
956 kfree(pending);
957 }
Chris Masondc17ff82008-01-08 15:46:30 -0500958 return 0;
959}
960
Chris Mason79154b12007-03-22 15:59:16 -0400961int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
962 struct btrfs_root *root)
963{
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400964 unsigned long joined = 0;
965 unsigned long timeout = 1;
Chris Mason79154b12007-03-22 15:59:16 -0400966 struct btrfs_transaction *cur_trans;
Chris Mason8fd17792007-04-19 21:01:03 -0400967 struct btrfs_transaction *prev_trans = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -0400968 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400969 struct list_head dirty_fs_roots;
Chris Masond1310b22008-01-24 16:13:08 -0500970 struct extent_io_tree *pinned_copy;
Chris Mason79154b12007-03-22 15:59:16 -0400971 DEFINE_WAIT(wait);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400972 int ret;
Chris Mason89573b92009-03-12 20:12:45 -0400973 int should_grow = 0;
974 unsigned long now = get_seconds();
Sage Weildccae992009-04-02 16:59:01 -0400975 int flush_on_commit = btrfs_test_opt(root, FLUSHONCOMMIT);
Chris Mason79154b12007-03-22 15:59:16 -0400976
Chris Mason5a3f23d2009-03-31 13:27:11 -0400977 btrfs_run_ordered_operations(root, 0);
978
Chris Mason56bec292009-03-13 10:10:06 -0400979 /* make a pass through all the delayed refs we have so far
980 * any runnings procs may add more while we are here
981 */
982 ret = btrfs_run_delayed_refs(trans, root, 0);
983 BUG_ON(ret);
984
Chris Masonb7ec40d2009-03-12 20:12:45 -0400985 cur_trans = trans->transaction;
Chris Mason56bec292009-03-13 10:10:06 -0400986 /*
987 * set the flushing flag so procs in this transaction have to
988 * start sending their work down.
989 */
Chris Masonb7ec40d2009-03-12 20:12:45 -0400990 cur_trans->delayed_refs.flushing = 1;
Chris Mason56bec292009-03-13 10:10:06 -0400991
Chris Masonc3e69d52009-03-13 10:17:05 -0400992 ret = btrfs_run_delayed_refs(trans, root, 0);
Chris Mason56bec292009-03-13 10:10:06 -0400993 BUG_ON(ret);
994
Chris Mason79154b12007-03-22 15:59:16 -0400995 mutex_lock(&root->fs_info->trans_mutex);
Chris Masonb7ec40d2009-03-12 20:12:45 -0400996 INIT_LIST_HEAD(&dirty_fs_roots);
997 if (cur_trans->in_commit) {
998 cur_trans->use_count++;
Chris Masonccd467d2007-06-28 15:57:36 -0400999 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -04001000 btrfs_end_transaction(trans, root);
Chris Masonccd467d2007-06-28 15:57:36 -04001001
Chris Mason79154b12007-03-22 15:59:16 -04001002 ret = wait_for_commit(root, cur_trans);
1003 BUG_ON(ret);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001004
1005 mutex_lock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -04001006 put_transaction(cur_trans);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001007 mutex_unlock(&root->fs_info->trans_mutex);
1008
Chris Mason79154b12007-03-22 15:59:16 -04001009 return 0;
1010 }
Chris Mason4313b392008-01-03 09:08:48 -05001011
1012 pinned_copy = kmalloc(sizeof(*pinned_copy), GFP_NOFS);
1013 if (!pinned_copy)
1014 return -ENOMEM;
1015
Chris Masond1310b22008-01-24 16:13:08 -05001016 extent_io_tree_init(pinned_copy,
Chris Mason4313b392008-01-03 09:08:48 -05001017 root->fs_info->btree_inode->i_mapping, GFP_NOFS);
1018
Chris Mason2c90e5d2007-04-02 10:50:19 -04001019 trans->transaction->in_commit = 1;
Chris Masonf9295742008-07-17 12:54:14 -04001020 trans->transaction->blocked = 1;
Chris Masonccd467d2007-06-28 15:57:36 -04001021 if (cur_trans->list.prev != &root->fs_info->trans_list) {
1022 prev_trans = list_entry(cur_trans->list.prev,
1023 struct btrfs_transaction, list);
1024 if (!prev_trans->commit_done) {
1025 prev_trans->use_count++;
Chris Masonccd467d2007-06-28 15:57:36 -04001026 mutex_unlock(&root->fs_info->trans_mutex);
1027
1028 wait_for_commit(root, prev_trans);
Chris Masonccd467d2007-06-28 15:57:36 -04001029
Chris Masonccd467d2007-06-28 15:57:36 -04001030 mutex_lock(&root->fs_info->trans_mutex);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001031 put_transaction(prev_trans);
Chris Masonccd467d2007-06-28 15:57:36 -04001032 }
1033 }
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001034
Chris Mason89573b92009-03-12 20:12:45 -04001035 if (now < cur_trans->start_time || now - cur_trans->start_time < 1)
1036 should_grow = 1;
1037
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001038 do {
Yan Zheng7ea394f2008-08-05 13:05:02 -04001039 int snap_pending = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001040 joined = cur_trans->num_joined;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001041 if (!list_empty(&trans->transaction->pending_snapshots))
1042 snap_pending = 1;
1043
Chris Mason2c90e5d2007-04-02 10:50:19 -04001044 WARN_ON(cur_trans != trans->transaction);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001045 prepare_to_wait(&cur_trans->writer_wait, &wait,
Chris Mason79154b12007-03-22 15:59:16 -04001046 TASK_UNINTERRUPTIBLE);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001047
1048 if (cur_trans->num_writers > 1)
1049 timeout = MAX_SCHEDULE_TIMEOUT;
Chris Mason89573b92009-03-12 20:12:45 -04001050 else if (should_grow)
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001051 timeout = 1;
1052
Chris Mason79154b12007-03-22 15:59:16 -04001053 mutex_unlock(&root->fs_info->trans_mutex);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001054
Sage Weildccae992009-04-02 16:59:01 -04001055 if (flush_on_commit || snap_pending) {
1056 if (flush_on_commit)
1057 btrfs_start_delalloc_inodes(root);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001058 ret = btrfs_wait_ordered_extents(root, 1);
1059 BUG_ON(ret);
1060 }
1061
Chris Mason5a3f23d2009-03-31 13:27:11 -04001062 /*
1063 * rename don't use btrfs_join_transaction, so, once we
1064 * set the transaction to blocked above, we aren't going
1065 * to get any new ordered operations. We can safely run
1066 * it here and no for sure that nothing new will be added
1067 * to the list
1068 */
1069 btrfs_run_ordered_operations(root, 1);
1070
Chris Mason89573b92009-03-12 20:12:45 -04001071 smp_mb();
1072 if (cur_trans->num_writers > 1 || should_grow)
1073 schedule_timeout(timeout);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001074
Chris Mason79154b12007-03-22 15:59:16 -04001075 mutex_lock(&root->fs_info->trans_mutex);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001076 finish_wait(&cur_trans->writer_wait, &wait);
1077 } while (cur_trans->num_writers > 1 ||
Chris Mason89573b92009-03-12 20:12:45 -04001078 (should_grow && cur_trans->num_joined != joined));
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001079
Chris Mason3063d292008-01-08 15:46:30 -05001080 ret = create_pending_snapshots(trans, root->fs_info);
1081 BUG_ON(ret);
1082
Chris Mason56bec292009-03-13 10:10:06 -04001083 ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
1084 BUG_ON(ret);
1085
Chris Mason2c90e5d2007-04-02 10:50:19 -04001086 WARN_ON(cur_trans != trans->transaction);
Chris Masondc17ff82008-01-08 15:46:30 -05001087
Chris Masone02119d2008-09-05 16:13:11 -04001088 /* btrfs_commit_tree_roots is responsible for getting the
1089 * various roots consistent with each other. Every pointer
1090 * in the tree of tree roots has to point to the most up to date
1091 * root for every subvolume and other tree. So, we have to keep
1092 * the tree logging code from jumping in and changing any
1093 * of the trees.
1094 *
1095 * At this point in the commit, there can't be any tree-log
1096 * writers, but a little lower down we drop the trans mutex
1097 * and let new people in. By holding the tree_log_mutex
1098 * from now until after the super is written, we avoid races
1099 * with the tree-log code.
1100 */
1101 mutex_lock(&root->fs_info->tree_log_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04001102 /*
1103 * keep tree reloc code from adding new reloc trees
1104 */
1105 mutex_lock(&root->fs_info->tree_reloc_mutex);
1106
Chris Masone02119d2008-09-05 16:13:11 -04001107
Chris Mason54aa1f42007-06-22 14:16:25 -04001108 ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix,
1109 &dirty_fs_roots);
1110 BUG_ON(ret);
1111
Chris Masone02119d2008-09-05 16:13:11 -04001112 /* add_dirty_roots gets rid of all the tree log roots, it is now
1113 * safe to free the root of tree log roots
1114 */
1115 btrfs_free_log_root_tree(trans, root->fs_info);
1116
Chris Mason79154b12007-03-22 15:59:16 -04001117 ret = btrfs_commit_tree_roots(trans, root);
1118 BUG_ON(ret);
Chris Mason54aa1f42007-06-22 14:16:25 -04001119
Chris Mason78fae272007-03-25 11:35:08 -04001120 cur_trans = root->fs_info->running_transaction;
Chris Masoncee36a02008-01-15 08:40:48 -05001121 spin_lock(&root->fs_info->new_trans_lock);
Chris Mason78fae272007-03-25 11:35:08 -04001122 root->fs_info->running_transaction = NULL;
Chris Masoncee36a02008-01-15 08:40:48 -05001123 spin_unlock(&root->fs_info->new_trans_lock);
Chris Mason4b52dff2007-06-26 10:06:50 -04001124 btrfs_set_super_generation(&root->fs_info->super_copy,
1125 cur_trans->transid);
1126 btrfs_set_super_root(&root->fs_info->super_copy,
Chris Masondb945352007-10-15 16:15:53 -04001127 root->fs_info->tree_root->node->start);
1128 btrfs_set_super_root_level(&root->fs_info->super_copy,
1129 btrfs_header_level(root->fs_info->tree_root->node));
Chris Mason5f39d392007-10-15 16:14:19 -04001130
Chris Mason0b86a832008-03-24 15:01:56 -04001131 btrfs_set_super_chunk_root(&root->fs_info->super_copy,
1132 chunk_root->node->start);
1133 btrfs_set_super_chunk_root_level(&root->fs_info->super_copy,
1134 btrfs_header_level(chunk_root->node));
Yan Zheng84234f32008-10-29 14:49:05 -04001135 btrfs_set_super_chunk_root_generation(&root->fs_info->super_copy,
1136 btrfs_header_generation(chunk_root->node));
Chris Masone02119d2008-09-05 16:13:11 -04001137
1138 if (!root->fs_info->log_root_recovering) {
1139 btrfs_set_super_log_root(&root->fs_info->super_copy, 0);
1140 btrfs_set_super_log_root_level(&root->fs_info->super_copy, 0);
1141 }
1142
Chris Masona061fc82008-05-07 11:43:44 -04001143 memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy,
1144 sizeof(root->fs_info->super_copy));
Chris Masonccd467d2007-06-28 15:57:36 -04001145
Chris Mason4313b392008-01-03 09:08:48 -05001146 btrfs_copy_pinned(root, pinned_copy);
Chris Masonccd467d2007-06-28 15:57:36 -04001147
Chris Masonf9295742008-07-17 12:54:14 -04001148 trans->transaction->blocked = 0;
Chris Masonb7ec40d2009-03-12 20:12:45 -04001149
Chris Masone6dcd2d2008-07-17 12:53:50 -04001150 wake_up(&root->fs_info->transaction_throttle);
Chris Masonf9295742008-07-17 12:54:14 -04001151 wake_up(&root->fs_info->transaction_wait);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001152
Chris Mason78fae272007-03-25 11:35:08 -04001153 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -04001154 ret = btrfs_write_and_wait_transaction(trans, root);
1155 BUG_ON(ret);
Yan Zhenga512bbf2008-12-08 16:46:26 -05001156 write_ctree_super(trans, root, 0);
Chris Mason4313b392008-01-03 09:08:48 -05001157
Chris Masone02119d2008-09-05 16:13:11 -04001158 /*
1159 * the super is written, we can safely allow the tree-loggers
1160 * to go about their business
1161 */
1162 mutex_unlock(&root->fs_info->tree_log_mutex);
1163
Chris Mason4313b392008-01-03 09:08:48 -05001164 btrfs_finish_extent_commit(trans, root, pinned_copy);
Chris Mason4313b392008-01-03 09:08:48 -05001165 kfree(pinned_copy);
1166
Zheng Yan1a40e232008-09-26 10:09:34 -04001167 btrfs_drop_dead_reloc_roots(root);
1168 mutex_unlock(&root->fs_info->tree_reloc_mutex);
1169
Chris Mason3de45862008-11-17 21:02:50 -05001170 /* do the directory inserts of any pending snapshot creations */
1171 finish_pending_snapshots(trans, root->fs_info);
1172
Zheng Yan1a40e232008-09-26 10:09:34 -04001173 mutex_lock(&root->fs_info->trans_mutex);
1174
Chris Mason2c90e5d2007-04-02 10:50:19 -04001175 cur_trans->commit_done = 1;
Chris Masonb7ec40d2009-03-12 20:12:45 -04001176
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001177 root->fs_info->last_trans_committed = cur_trans->transid;
Chris Mason2c90e5d2007-04-02 10:50:19 -04001178 wake_up(&cur_trans->commit_wait);
Chris Mason3de45862008-11-17 21:02:50 -05001179
Chris Mason79154b12007-03-22 15:59:16 -04001180 put_transaction(cur_trans);
Chris Mason78fae272007-03-25 11:35:08 -04001181 put_transaction(cur_trans);
Josef Bacik58176a92007-08-29 15:47:34 -04001182
Yanbcc63ab2008-07-30 16:29:20 -04001183 list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots);
Chris Masonfacda1e2007-06-08 18:11:48 -04001184 if (root->fs_info->closing)
1185 list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots);
Josef Bacik58176a92007-08-29 15:47:34 -04001186
Chris Mason78fae272007-03-25 11:35:08 -04001187 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason3de45862008-11-17 21:02:50 -05001188
Chris Mason2c90e5d2007-04-02 10:50:19 -04001189 kmem_cache_free(btrfs_trans_handle_cachep, trans);
Chris Mason79154b12007-03-22 15:59:16 -04001190
Chris Masond3977122009-01-05 21:25:51 -05001191 if (root->fs_info->closing)
Chris Masonfacda1e2007-06-08 18:11:48 -04001192 drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);
Chris Mason79154b12007-03-22 15:59:16 -04001193 return ret;
1194}
1195
Chris Masond352ac62008-09-29 15:18:18 -04001196/*
1197 * interface function to delete all the snapshots we have scheduled for deletion
1198 */
Chris Masone9d0b132007-08-10 14:06:19 -04001199int btrfs_clean_old_snapshots(struct btrfs_root *root)
1200{
1201 struct list_head dirty_roots;
1202 INIT_LIST_HEAD(&dirty_roots);
Chris Masona74a4b92008-06-25 16:01:31 -04001203again:
Chris Masone9d0b132007-08-10 14:06:19 -04001204 mutex_lock(&root->fs_info->trans_mutex);
1205 list_splice_init(&root->fs_info->dead_roots, &dirty_roots);
1206 mutex_unlock(&root->fs_info->trans_mutex);
1207
1208 if (!list_empty(&dirty_roots)) {
1209 drop_dirty_roots(root, &dirty_roots);
Chris Masona74a4b92008-06-25 16:01:31 -04001210 goto again;
Chris Masone9d0b132007-08-10 14:06:19 -04001211 }
1212 return 0;
1213}