blob: 924af6f2aeac719688c118aac33620d5be607c09 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Mason79154b12007-03-22 15:59:16 -040019#include <linux/fs.h>
Chris Mason34088782007-06-12 11:36:58 -040020#include <linux/sched.h>
Chris Masond3c2fdcf2007-09-17 10:58:06 -040021#include <linux/writeback.h>
Chris Mason5f39d392007-10-15 16:14:19 -040022#include <linux/pagemap.h>
Chris Mason79154b12007-03-22 15:59:16 -040023#include "ctree.h"
24#include "disk-io.h"
25#include "transaction.h"
Chris Mason925baed2008-06-25 16:01:30 -040026#include "locking.h"
Yan Zheng31153d82008-07-28 15:32:19 -040027#include "ref-cache.h"
Chris Masone02119d2008-09-05 16:13:11 -040028#include "tree-log.h"
Chris Mason79154b12007-03-22 15:59:16 -040029
Chris Mason78fae272007-03-25 11:35:08 -040030static int total_trans = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -040031extern struct kmem_cache *btrfs_trans_handle_cachep;
32extern struct kmem_cache *btrfs_transaction_cachep;
33
Chris Mason0f7d52f2007-04-09 10:42:37 -040034#define BTRFS_ROOT_TRANS_TAG 0
35
Chris Mason80b67942008-02-01 16:35:04 -050036static noinline void put_transaction(struct btrfs_transaction *transaction)
Chris Mason79154b12007-03-22 15:59:16 -040037{
Chris Mason2c90e5d2007-04-02 10:50:19 -040038 WARN_ON(transaction->use_count == 0);
Chris Mason79154b12007-03-22 15:59:16 -040039 transaction->use_count--;
Chris Mason78fae272007-03-25 11:35:08 -040040 if (transaction->use_count == 0) {
41 WARN_ON(total_trans == 0);
42 total_trans--;
Chris Mason8fd17792007-04-19 21:01:03 -040043 list_del_init(&transaction->list);
Chris Mason2c90e5d2007-04-02 10:50:19 -040044 memset(transaction, 0, sizeof(*transaction));
45 kmem_cache_free(btrfs_transaction_cachep, transaction);
Chris Mason78fae272007-03-25 11:35:08 -040046 }
Chris Mason79154b12007-03-22 15:59:16 -040047}
48
Chris Masond352ac62008-09-29 15:18:18 -040049/*
50 * either allocate a new transaction or hop into the existing one
51 */
Chris Mason80b67942008-02-01 16:35:04 -050052static noinline int join_transaction(struct btrfs_root *root)
Chris Mason79154b12007-03-22 15:59:16 -040053{
54 struct btrfs_transaction *cur_trans;
55 cur_trans = root->fs_info->running_transaction;
56 if (!cur_trans) {
Chris Mason2c90e5d2007-04-02 10:50:19 -040057 cur_trans = kmem_cache_alloc(btrfs_transaction_cachep,
58 GFP_NOFS);
Chris Mason78fae272007-03-25 11:35:08 -040059 total_trans++;
Chris Mason79154b12007-03-22 15:59:16 -040060 BUG_ON(!cur_trans);
Chris Mason0f7d52f2007-04-09 10:42:37 -040061 root->fs_info->generation++;
Chris Masone18e4802008-01-18 10:54:22 -050062 root->fs_info->last_alloc = 0;
Chris Mason4529ba42008-01-31 16:45:07 -050063 root->fs_info->last_data_alloc = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -040064 cur_trans->num_writers = 1;
65 cur_trans->num_joined = 0;
Chris Mason0f7d52f2007-04-09 10:42:37 -040066 cur_trans->transid = root->fs_info->generation;
Chris Mason79154b12007-03-22 15:59:16 -040067 init_waitqueue_head(&cur_trans->writer_wait);
68 init_waitqueue_head(&cur_trans->commit_wait);
69 cur_trans->in_commit = 0;
Chris Masonf9295742008-07-17 12:54:14 -040070 cur_trans->blocked = 0;
Chris Masond5719762007-03-23 10:01:08 -040071 cur_trans->use_count = 1;
Chris Mason79154b12007-03-22 15:59:16 -040072 cur_trans->commit_done = 0;
Chris Mason08607c12007-06-08 15:33:54 -040073 cur_trans->start_time = get_seconds();
Chris Mason3063d292008-01-08 15:46:30 -050074 INIT_LIST_HEAD(&cur_trans->pending_snapshots);
Chris Mason8fd17792007-04-19 21:01:03 -040075 list_add_tail(&cur_trans->list, &root->fs_info->trans_list);
Chris Masond1310b22008-01-24 16:13:08 -050076 extent_io_tree_init(&cur_trans->dirty_pages,
Chris Mason5f39d392007-10-15 16:14:19 -040077 root->fs_info->btree_inode->i_mapping,
78 GFP_NOFS);
Chris Mason48ec2cf2008-06-09 09:35:50 -040079 spin_lock(&root->fs_info->new_trans_lock);
80 root->fs_info->running_transaction = cur_trans;
81 spin_unlock(&root->fs_info->new_trans_lock);
Josef Bacik15ee9bc2007-08-10 16:22:09 -040082 } else {
83 cur_trans->num_writers++;
84 cur_trans->num_joined++;
Chris Mason79154b12007-03-22 15:59:16 -040085 }
Josef Bacik15ee9bc2007-08-10 16:22:09 -040086
Chris Mason79154b12007-03-22 15:59:16 -040087 return 0;
88}
89
Chris Masond352ac62008-09-29 15:18:18 -040090/*
91 * this does all the record keeping required to make sure that a
92 * reference counted root is properly recorded in a given transaction.
93 * This is required to make sure the old root from before we joined the transaction
94 * is deleted when the transaction commits
95 */
Chris Masone02119d2008-09-05 16:13:11 -040096noinline int btrfs_record_root_in_trans(struct btrfs_root *root)
Chris Mason6702ed42007-08-07 16:15:09 -040097{
Yan Zhengf321e492008-07-30 09:26:11 -040098 struct btrfs_dirty_root *dirty;
Chris Mason6702ed42007-08-07 16:15:09 -040099 u64 running_trans_id = root->fs_info->running_transaction->transid;
100 if (root->ref_cows && root->last_trans < running_trans_id) {
101 WARN_ON(root == root->fs_info->extent_root);
102 if (root->root_item.refs != 0) {
103 radix_tree_tag_set(&root->fs_info->fs_roots_radix,
104 (unsigned long)root->root_key.objectid,
105 BTRFS_ROOT_TRANS_TAG);
Yan Zheng31153d82008-07-28 15:32:19 -0400106
107 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
108 BUG_ON(!dirty);
109 dirty->root = kmalloc(sizeof(*dirty->root), GFP_NOFS);
110 BUG_ON(!dirty->root);
Yan Zheng31153d82008-07-28 15:32:19 -0400111 dirty->latest_root = root;
112 INIT_LIST_HEAD(&dirty->list);
Yan Zheng31153d82008-07-28 15:32:19 -0400113
Chris Mason925baed2008-06-25 16:01:30 -0400114 root->commit_root = btrfs_root_node(root);
Yan Zheng31153d82008-07-28 15:32:19 -0400115
116 memcpy(dirty->root, root, sizeof(*root));
117 spin_lock_init(&dirty->root->node_lock);
Yanbcc63ab2008-07-30 16:29:20 -0400118 spin_lock_init(&dirty->root->list_lock);
Yan Zheng31153d82008-07-28 15:32:19 -0400119 mutex_init(&dirty->root->objectid_mutex);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400120 mutex_init(&dirty->root->log_mutex);
Yanbcc63ab2008-07-30 16:29:20 -0400121 INIT_LIST_HEAD(&dirty->root->dead_list);
Yan Zheng31153d82008-07-28 15:32:19 -0400122 dirty->root->node = root->commit_root;
123 dirty->root->commit_root = NULL;
Yanbcc63ab2008-07-30 16:29:20 -0400124
125 spin_lock(&root->list_lock);
126 list_add(&dirty->root->dead_list, &root->dead_list);
127 spin_unlock(&root->list_lock);
128
129 root->dirty_root = dirty;
Chris Mason6702ed42007-08-07 16:15:09 -0400130 } else {
131 WARN_ON(1);
132 }
133 root->last_trans = running_trans_id;
134 }
135 return 0;
136}
137
Chris Masond352ac62008-09-29 15:18:18 -0400138/* wait for commit against the current transaction to become unblocked
139 * when this is done, it is safe to start a new transaction, but the current
140 * transaction might not be fully on disk.
141 */
Chris Mason37d1aee2008-07-31 10:48:37 -0400142static void wait_current_trans(struct btrfs_root *root)
Chris Mason79154b12007-03-22 15:59:16 -0400143{
Chris Masonf9295742008-07-17 12:54:14 -0400144 struct btrfs_transaction *cur_trans;
Chris Mason79154b12007-03-22 15:59:16 -0400145
Chris Masonf9295742008-07-17 12:54:14 -0400146 cur_trans = root->fs_info->running_transaction;
Chris Mason37d1aee2008-07-31 10:48:37 -0400147 if (cur_trans && cur_trans->blocked) {
Chris Masonf9295742008-07-17 12:54:14 -0400148 DEFINE_WAIT(wait);
149 cur_trans->use_count++;
150 while(1) {
151 prepare_to_wait(&root->fs_info->transaction_wait, &wait,
152 TASK_UNINTERRUPTIBLE);
153 if (cur_trans->blocked) {
154 mutex_unlock(&root->fs_info->trans_mutex);
155 schedule();
156 mutex_lock(&root->fs_info->trans_mutex);
157 finish_wait(&root->fs_info->transaction_wait,
158 &wait);
159 } else {
160 finish_wait(&root->fs_info->transaction_wait,
161 &wait);
162 break;
163 }
164 }
165 put_transaction(cur_trans);
166 }
Chris Mason37d1aee2008-07-31 10:48:37 -0400167}
168
Chris Masone02119d2008-09-05 16:13:11 -0400169static struct btrfs_trans_handle *start_transaction(struct btrfs_root *root,
Sage Weil9ca9ee02008-08-04 10:41:27 -0400170 int num_blocks, int wait)
Chris Mason37d1aee2008-07-31 10:48:37 -0400171{
172 struct btrfs_trans_handle *h =
173 kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
174 int ret;
175
176 mutex_lock(&root->fs_info->trans_mutex);
Chris Mason4bef0842008-09-08 11:18:08 -0400177 if (!root->fs_info->log_root_recovering &&
178 ((wait == 1 && !root->fs_info->open_ioctl_trans) || wait == 2))
Chris Mason37d1aee2008-07-31 10:48:37 -0400179 wait_current_trans(root);
Chris Mason79154b12007-03-22 15:59:16 -0400180 ret = join_transaction(root);
181 BUG_ON(ret);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400182
Chris Masone02119d2008-09-05 16:13:11 -0400183 btrfs_record_root_in_trans(root);
Chris Mason6702ed42007-08-07 16:15:09 -0400184 h->transid = root->fs_info->running_transaction->transid;
Chris Mason79154b12007-03-22 15:59:16 -0400185 h->transaction = root->fs_info->running_transaction;
186 h->blocks_reserved = num_blocks;
187 h->blocks_used = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400188 h->block_group = NULL;
Chris Mason26b80032007-08-08 20:17:12 -0400189 h->alloc_exclude_nr = 0;
190 h->alloc_exclude_start = 0;
Chris Mason79154b12007-03-22 15:59:16 -0400191 root->fs_info->running_transaction->use_count++;
192 mutex_unlock(&root->fs_info->trans_mutex);
193 return h;
194}
195
Chris Masonf9295742008-07-17 12:54:14 -0400196struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
197 int num_blocks)
198{
Sage Weil9ca9ee02008-08-04 10:41:27 -0400199 return start_transaction(root, num_blocks, 1);
Chris Masonf9295742008-07-17 12:54:14 -0400200}
201struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root,
202 int num_blocks)
203{
Sage Weil9ca9ee02008-08-04 10:41:27 -0400204 return start_transaction(root, num_blocks, 0);
Chris Masonf9295742008-07-17 12:54:14 -0400205}
206
Sage Weil9ca9ee02008-08-04 10:41:27 -0400207struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *r,
208 int num_blocks)
209{
210 return start_transaction(r, num_blocks, 2);
211}
212
Chris Masond352ac62008-09-29 15:18:18 -0400213/* wait for a transaction commit to be fully complete */
Chris Mason89ce8a62008-06-25 16:01:31 -0400214static noinline int wait_for_commit(struct btrfs_root *root,
215 struct btrfs_transaction *commit)
216{
217 DEFINE_WAIT(wait);
218 mutex_lock(&root->fs_info->trans_mutex);
219 while(!commit->commit_done) {
220 prepare_to_wait(&commit->commit_wait, &wait,
221 TASK_UNINTERRUPTIBLE);
222 if (commit->commit_done)
223 break;
224 mutex_unlock(&root->fs_info->trans_mutex);
225 schedule();
226 mutex_lock(&root->fs_info->trans_mutex);
227 }
228 mutex_unlock(&root->fs_info->trans_mutex);
229 finish_wait(&commit->commit_wait, &wait);
230 return 0;
231}
232
Chris Masond352ac62008-09-29 15:18:18 -0400233/*
234 * rate limit against the drop_snapshot code. This helps to slow down new operations
235 * if the drop_snapshot code isn't able to keep up.
236 */
Chris Mason37d1aee2008-07-31 10:48:37 -0400237static void throttle_on_drops(struct btrfs_root *root)
Chris Masonab78c842008-07-29 16:15:18 -0400238{
239 struct btrfs_fs_info *info = root->fs_info;
Chris Mason2dd3e672008-08-04 08:20:15 -0400240 int harder_count = 0;
Chris Masonab78c842008-07-29 16:15:18 -0400241
Chris Mason2dd3e672008-08-04 08:20:15 -0400242harder:
Chris Masonab78c842008-07-29 16:15:18 -0400243 if (atomic_read(&info->throttles)) {
244 DEFINE_WAIT(wait);
245 int thr;
Chris Masonab78c842008-07-29 16:15:18 -0400246 thr = atomic_read(&info->throttle_gen);
247
248 do {
249 prepare_to_wait(&info->transaction_throttle,
250 &wait, TASK_UNINTERRUPTIBLE);
251 if (!atomic_read(&info->throttles)) {
252 finish_wait(&info->transaction_throttle, &wait);
253 break;
254 }
255 schedule();
256 finish_wait(&info->transaction_throttle, &wait);
257 } while (thr == atomic_read(&info->throttle_gen));
Chris Mason2dd3e672008-08-04 08:20:15 -0400258 harder_count++;
259
260 if (root->fs_info->total_ref_cache_size > 1 * 1024 * 1024 &&
261 harder_count < 2)
262 goto harder;
263
264 if (root->fs_info->total_ref_cache_size > 5 * 1024 * 1024 &&
265 harder_count < 10)
266 goto harder;
267
268 if (root->fs_info->total_ref_cache_size > 10 * 1024 * 1024 &&
269 harder_count < 20)
270 goto harder;
Chris Masonab78c842008-07-29 16:15:18 -0400271 }
272}
273
Chris Mason37d1aee2008-07-31 10:48:37 -0400274void btrfs_throttle(struct btrfs_root *root)
275{
276 mutex_lock(&root->fs_info->trans_mutex);
Sage Weil9ca9ee02008-08-04 10:41:27 -0400277 if (!root->fs_info->open_ioctl_trans)
278 wait_current_trans(root);
Chris Mason37d1aee2008-07-31 10:48:37 -0400279 mutex_unlock(&root->fs_info->trans_mutex);
280
281 throttle_on_drops(root);
282}
283
Chris Mason89ce8a62008-06-25 16:01:31 -0400284static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
285 struct btrfs_root *root, int throttle)
Chris Mason79154b12007-03-22 15:59:16 -0400286{
287 struct btrfs_transaction *cur_trans;
Chris Masonab78c842008-07-29 16:15:18 -0400288 struct btrfs_fs_info *info = root->fs_info;
Chris Masond6e4a422007-04-06 15:37:36 -0400289
Chris Masonab78c842008-07-29 16:15:18 -0400290 mutex_lock(&info->trans_mutex);
291 cur_trans = info->running_transaction;
Chris Masonccd467d2007-06-28 15:57:36 -0400292 WARN_ON(cur_trans != trans->transaction);
Chris Masond5719762007-03-23 10:01:08 -0400293 WARN_ON(cur_trans->num_writers < 1);
Chris Masonccd467d2007-06-28 15:57:36 -0400294 cur_trans->num_writers--;
Chris Mason89ce8a62008-06-25 16:01:31 -0400295
Chris Mason79154b12007-03-22 15:59:16 -0400296 if (waitqueue_active(&cur_trans->writer_wait))
297 wake_up(&cur_trans->writer_wait);
Chris Mason79154b12007-03-22 15:59:16 -0400298 put_transaction(cur_trans);
Chris Masonab78c842008-07-29 16:15:18 -0400299 mutex_unlock(&info->trans_mutex);
Chris Masond6025572007-03-30 14:27:56 -0400300 memset(trans, 0, sizeof(*trans));
Chris Mason2c90e5d2007-04-02 10:50:19 -0400301 kmem_cache_free(btrfs_trans_handle_cachep, trans);
Chris Masonab78c842008-07-29 16:15:18 -0400302
303 if (throttle)
Chris Mason37d1aee2008-07-31 10:48:37 -0400304 throttle_on_drops(root);
Chris Masonab78c842008-07-29 16:15:18 -0400305
Chris Mason79154b12007-03-22 15:59:16 -0400306 return 0;
307}
308
Chris Mason89ce8a62008-06-25 16:01:31 -0400309int btrfs_end_transaction(struct btrfs_trans_handle *trans,
310 struct btrfs_root *root)
311{
312 return __btrfs_end_transaction(trans, root, 0);
313}
314
315int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
316 struct btrfs_root *root)
317{
318 return __btrfs_end_transaction(trans, root, 1);
319}
320
Chris Masond352ac62008-09-29 15:18:18 -0400321/*
322 * when btree blocks are allocated, they have some corresponding bits set for
323 * them in one of two extent_io trees. This is used to make sure all of
324 * those extents are on disk for transaction or log commit
325 */
Chris Masond0c803c2008-09-11 16:17:57 -0400326int btrfs_write_and_wait_marked_extents(struct btrfs_root *root,
327 struct extent_io_tree *dirty_pages)
Chris Mason79154b12007-03-22 15:59:16 -0400328{
Chris Mason7c4452b2007-04-28 09:29:35 -0400329 int ret;
Chris Mason777e6bd2008-08-15 15:34:15 -0400330 int err = 0;
Chris Mason7c4452b2007-04-28 09:29:35 -0400331 int werr = 0;
332 struct page *page;
Chris Mason7c4452b2007-04-28 09:29:35 -0400333 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Mason777e6bd2008-08-15 15:34:15 -0400334 u64 start = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400335 u64 end;
336 unsigned long index;
Chris Mason7c4452b2007-04-28 09:29:35 -0400337
Chris Mason7c4452b2007-04-28 09:29:35 -0400338 while(1) {
Chris Mason777e6bd2008-08-15 15:34:15 -0400339 ret = find_first_extent_bit(dirty_pages, start, &start, &end,
Chris Mason5f39d392007-10-15 16:14:19 -0400340 EXTENT_DIRTY);
341 if (ret)
Chris Mason7c4452b2007-04-28 09:29:35 -0400342 break;
Chris Mason5f39d392007-10-15 16:14:19 -0400343 while(start <= end) {
Chris Mason777e6bd2008-08-15 15:34:15 -0400344 cond_resched();
345
Chris Mason5f39d392007-10-15 16:14:19 -0400346 index = start >> PAGE_CACHE_SHIFT;
Chris Mason35ebb932007-10-30 16:56:53 -0400347 start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
Chris Mason4bef0842008-09-08 11:18:08 -0400348 page = find_get_page(btree_inode->i_mapping, index);
Chris Mason7c4452b2007-04-28 09:29:35 -0400349 if (!page)
350 continue;
Chris Mason4bef0842008-09-08 11:18:08 -0400351
352 btree_lock_page_hook(page);
353 if (!page->mapping) {
354 unlock_page(page);
355 page_cache_release(page);
356 continue;
357 }
358
Chris Mason6702ed42007-08-07 16:15:09 -0400359 if (PageWriteback(page)) {
360 if (PageDirty(page))
361 wait_on_page_writeback(page);
362 else {
363 unlock_page(page);
364 page_cache_release(page);
365 continue;
366 }
367 }
Chris Mason7c4452b2007-04-28 09:29:35 -0400368 err = write_one_page(page, 0);
369 if (err)
370 werr = err;
371 page_cache_release(page);
372 }
373 }
Chris Mason777e6bd2008-08-15 15:34:15 -0400374 while(1) {
375 ret = find_first_extent_bit(dirty_pages, 0, &start, &end,
376 EXTENT_DIRTY);
377 if (ret)
378 break;
379
380 clear_extent_dirty(dirty_pages, start, end, GFP_NOFS);
381 while(start <= end) {
382 index = start >> PAGE_CACHE_SHIFT;
383 start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
384 page = find_get_page(btree_inode->i_mapping, index);
385 if (!page)
386 continue;
387 if (PageDirty(page)) {
Chris Mason4bef0842008-09-08 11:18:08 -0400388 btree_lock_page_hook(page);
389 wait_on_page_writeback(page);
Chris Mason777e6bd2008-08-15 15:34:15 -0400390 err = write_one_page(page, 0);
391 if (err)
392 werr = err;
393 }
394 wait_on_page_writeback(page);
395 page_cache_release(page);
396 cond_resched();
397 }
398 }
Chris Mason7c4452b2007-04-28 09:29:35 -0400399 if (err)
400 werr = err;
401 return werr;
Chris Mason79154b12007-03-22 15:59:16 -0400402}
403
Chris Masond0c803c2008-09-11 16:17:57 -0400404int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
405 struct btrfs_root *root)
406{
407 if (!trans || !trans->transaction) {
408 struct inode *btree_inode;
409 btree_inode = root->fs_info->btree_inode;
410 return filemap_write_and_wait(btree_inode->i_mapping);
411 }
412 return btrfs_write_and_wait_marked_extents(root,
413 &trans->transaction->dirty_pages);
414}
415
Chris Masond352ac62008-09-29 15:18:18 -0400416/*
417 * this is used to update the root pointer in the tree of tree roots.
418 *
419 * But, in the case of the extent allocation tree, updating the root
420 * pointer may allocate blocks which may change the root of the extent
421 * allocation tree.
422 *
423 * So, this loops and repeats and makes sure the cowonly root didn't
424 * change while the root pointer was being updated in the metadata.
425 */
Chris Mason0b86a832008-03-24 15:01:56 -0400426static int update_cowonly_root(struct btrfs_trans_handle *trans,
427 struct btrfs_root *root)
428{
429 int ret;
430 u64 old_root_bytenr;
431 struct btrfs_root *tree_root = root->fs_info->tree_root;
432
433 btrfs_write_dirty_block_groups(trans, root);
434 while(1) {
435 old_root_bytenr = btrfs_root_bytenr(&root->root_item);
436 if (old_root_bytenr == root->node->start)
437 break;
438 btrfs_set_root_bytenr(&root->root_item,
439 root->node->start);
440 btrfs_set_root_level(&root->root_item,
441 btrfs_header_level(root->node));
Yan Zheng84234f32008-10-29 14:49:05 -0400442 btrfs_set_root_generation(&root->root_item, trans->transid);
Chris Mason0b86a832008-03-24 15:01:56 -0400443 ret = btrfs_update_root(trans, tree_root,
444 &root->root_key,
445 &root->root_item);
446 BUG_ON(ret);
447 btrfs_write_dirty_block_groups(trans, root);
448 }
449 return 0;
450}
451
Chris Masond352ac62008-09-29 15:18:18 -0400452/*
453 * update all the cowonly tree roots on disk
454 */
Chris Mason79154b12007-03-22 15:59:16 -0400455int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans,
456 struct btrfs_root *root)
457{
Chris Mason79154b12007-03-22 15:59:16 -0400458 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason0b86a832008-03-24 15:01:56 -0400459 struct list_head *next;
Yan Zheng84234f32008-10-29 14:49:05 -0400460 struct extent_buffer *eb;
461
462 eb = btrfs_lock_root_node(fs_info->tree_root);
463 btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 0, &eb, 0);
464 btrfs_tree_unlock(eb);
465 free_extent_buffer(eb);
Chris Mason79154b12007-03-22 15:59:16 -0400466
Chris Mason0b86a832008-03-24 15:01:56 -0400467 while(!list_empty(&fs_info->dirty_cowonly_roots)) {
468 next = fs_info->dirty_cowonly_roots.next;
469 list_del_init(next);
470 root = list_entry(next, struct btrfs_root, dirty_list);
471 update_cowonly_root(trans, root);
Chris Mason79154b12007-03-22 15:59:16 -0400472 }
473 return 0;
474}
475
Chris Masond352ac62008-09-29 15:18:18 -0400476/*
477 * dead roots are old snapshots that need to be deleted. This allocates
478 * a dirty root struct and adds it into the list of dead roots that need to
479 * be deleted
480 */
Yan Zhengb48652c2008-08-04 23:23:47 -0400481int btrfs_add_dead_root(struct btrfs_root *root, struct btrfs_root *latest)
Chris Mason5eda7b52007-06-22 14:16:25 -0400482{
Yan Zhengf321e492008-07-30 09:26:11 -0400483 struct btrfs_dirty_root *dirty;
Chris Mason5eda7b52007-06-22 14:16:25 -0400484
485 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
486 if (!dirty)
487 return -ENOMEM;
Chris Mason5eda7b52007-06-22 14:16:25 -0400488 dirty->root = root;
Chris Mason5ce14bb2007-09-11 11:15:39 -0400489 dirty->latest_root = latest;
Yan Zhengb48652c2008-08-04 23:23:47 -0400490
491 mutex_lock(&root->fs_info->trans_mutex);
492 list_add(&dirty->list, &latest->fs_info->dead_roots);
493 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason5eda7b52007-06-22 14:16:25 -0400494 return 0;
495}
496
Chris Masond352ac62008-09-29 15:18:18 -0400497/*
498 * at transaction commit time we need to schedule the old roots for
499 * deletion via btrfs_drop_snapshot. This runs through all the
500 * reference counted roots that were modified in the current
501 * transaction and puts them into the drop list
502 */
Chris Mason80b67942008-02-01 16:35:04 -0500503static noinline int add_dirty_roots(struct btrfs_trans_handle *trans,
504 struct radix_tree_root *radix,
505 struct list_head *list)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400506{
Yan Zhengf321e492008-07-30 09:26:11 -0400507 struct btrfs_dirty_root *dirty;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400508 struct btrfs_root *gang[8];
509 struct btrfs_root *root;
510 int i;
511 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400512 int err = 0;
Chris Mason5eda7b52007-06-22 14:16:25 -0400513 u32 refs;
Chris Mason54aa1f42007-06-22 14:16:25 -0400514
Chris Mason0f7d52f2007-04-09 10:42:37 -0400515 while(1) {
516 ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0,
517 ARRAY_SIZE(gang),
518 BTRFS_ROOT_TRANS_TAG);
519 if (ret == 0)
520 break;
521 for (i = 0; i < ret; i++) {
522 root = gang[i];
Chris Mason2619ba12007-04-10 16:58:11 -0400523 radix_tree_tag_clear(radix,
524 (unsigned long)root->root_key.objectid,
525 BTRFS_ROOT_TRANS_TAG);
Yan Zheng31153d82008-07-28 15:32:19 -0400526
527 BUG_ON(!root->ref_tree);
Chris Mason017e5362008-07-28 15:32:51 -0400528 dirty = root->dirty_root;
Yan Zheng31153d82008-07-28 15:32:19 -0400529
Chris Masone02119d2008-09-05 16:13:11 -0400530 btrfs_free_log(trans, root);
Yan Zhengf82d02d2008-10-29 14:49:05 -0400531 btrfs_free_reloc_root(trans, root);
Chris Masone02119d2008-09-05 16:13:11 -0400532
Chris Mason0f7d52f2007-04-09 10:42:37 -0400533 if (root->commit_root == root->node) {
Chris Masondb945352007-10-15 16:15:53 -0400534 WARN_ON(root->node->start !=
535 btrfs_root_bytenr(&root->root_item));
Yan Zheng31153d82008-07-28 15:32:19 -0400536
Chris Mason5f39d392007-10-15 16:14:19 -0400537 free_extent_buffer(root->commit_root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400538 root->commit_root = NULL;
Yan Zheng7ea394f2008-08-05 13:05:02 -0400539 root->dirty_root = NULL;
Yanbcc63ab2008-07-30 16:29:20 -0400540
541 spin_lock(&root->list_lock);
542 list_del_init(&dirty->root->dead_list);
543 spin_unlock(&root->list_lock);
544
Yan Zheng31153d82008-07-28 15:32:19 -0400545 kfree(dirty->root);
546 kfree(dirty);
Josef Bacik58176a92007-08-29 15:47:34 -0400547
548 /* make sure to update the root on disk
549 * so we get any updates to the block used
550 * counts
551 */
552 err = btrfs_update_root(trans,
553 root->fs_info->tree_root,
554 &root->root_key,
555 &root->root_item);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400556 continue;
557 }
Chris Mason9f3a7422007-08-07 15:52:19 -0400558
559 memset(&root->root_item.drop_progress, 0,
560 sizeof(struct btrfs_disk_key));
561 root->root_item.drop_level = 0;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400562 root->commit_root = NULL;
Yan Zheng7ea394f2008-08-05 13:05:02 -0400563 root->dirty_root = NULL;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400564 root->root_key.offset = root->fs_info->generation;
Chris Masondb945352007-10-15 16:15:53 -0400565 btrfs_set_root_bytenr(&root->root_item,
566 root->node->start);
567 btrfs_set_root_level(&root->root_item,
568 btrfs_header_level(root->node));
Yan Zheng84234f32008-10-29 14:49:05 -0400569 btrfs_set_root_generation(&root->root_item,
570 root->root_key.offset);
571
Chris Mason0f7d52f2007-04-09 10:42:37 -0400572 err = btrfs_insert_root(trans, root->fs_info->tree_root,
573 &root->root_key,
574 &root->root_item);
Chris Mason54aa1f42007-06-22 14:16:25 -0400575 if (err)
576 break;
Chris Mason9f3a7422007-08-07 15:52:19 -0400577
578 refs = btrfs_root_refs(&dirty->root->root_item);
579 btrfs_set_root_refs(&dirty->root->root_item, refs - 1);
Chris Mason5eda7b52007-06-22 14:16:25 -0400580 err = btrfs_update_root(trans, root->fs_info->tree_root,
Chris Mason9f3a7422007-08-07 15:52:19 -0400581 &dirty->root->root_key,
582 &dirty->root->root_item);
Chris Mason5eda7b52007-06-22 14:16:25 -0400583
584 BUG_ON(err);
Chris Mason9f3a7422007-08-07 15:52:19 -0400585 if (refs == 1) {
Chris Mason5eda7b52007-06-22 14:16:25 -0400586 list_add(&dirty->list, list);
Chris Mason9f3a7422007-08-07 15:52:19 -0400587 } else {
588 WARN_ON(1);
Yan Zheng31153d82008-07-28 15:32:19 -0400589 free_extent_buffer(dirty->root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -0400590 kfree(dirty->root);
Chris Mason5eda7b52007-06-22 14:16:25 -0400591 kfree(dirty);
Chris Mason9f3a7422007-08-07 15:52:19 -0400592 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400593 }
594 }
Chris Mason54aa1f42007-06-22 14:16:25 -0400595 return err;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400596}
597
Chris Masond352ac62008-09-29 15:18:18 -0400598/*
599 * defrag a given btree. If cacheonly == 1, this won't read from the disk,
600 * otherwise every leaf in the btree is read and defragged.
601 */
Chris Masone9d0b132007-08-10 14:06:19 -0400602int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
603{
604 struct btrfs_fs_info *info = root->fs_info;
605 int ret;
606 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400607 unsigned long nr;
Chris Masone9d0b132007-08-10 14:06:19 -0400608
Chris Masona2135012008-06-25 16:01:30 -0400609 smp_mb();
Chris Masone9d0b132007-08-10 14:06:19 -0400610 if (root->defrag_running)
611 return 0;
Chris Masone9d0b132007-08-10 14:06:19 -0400612 trans = btrfs_start_transaction(root, 1);
Chris Mason6b800532007-10-15 16:17:34 -0400613 while (1) {
Chris Masone9d0b132007-08-10 14:06:19 -0400614 root->defrag_running = 1;
615 ret = btrfs_defrag_leaves(trans, root, cacheonly);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400616 nr = trans->blocks_used;
Chris Masone9d0b132007-08-10 14:06:19 -0400617 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400618 btrfs_btree_balance_dirty(info->tree_root, nr);
Chris Masone9d0b132007-08-10 14:06:19 -0400619 cond_resched();
620
Chris Masone9d0b132007-08-10 14:06:19 -0400621 trans = btrfs_start_transaction(root, 1);
Chris Mason3f157a22008-06-25 16:01:31 -0400622 if (root->fs_info->closing || ret != -EAGAIN)
Chris Masone9d0b132007-08-10 14:06:19 -0400623 break;
624 }
625 root->defrag_running = 0;
Chris Masona2135012008-06-25 16:01:30 -0400626 smp_mb();
Chris Masone9d0b132007-08-10 14:06:19 -0400627 btrfs_end_transaction(trans, root);
628 return 0;
629}
630
Chris Masond352ac62008-09-29 15:18:18 -0400631/*
632 * Given a list of roots that need to be deleted, call btrfs_drop_snapshot on
633 * all of them
634 */
Chris Mason80b67942008-02-01 16:35:04 -0500635static noinline int drop_dirty_roots(struct btrfs_root *tree_root,
636 struct list_head *list)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400637{
Yan Zhengf321e492008-07-30 09:26:11 -0400638 struct btrfs_dirty_root *dirty;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400639 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400640 unsigned long nr;
Chris Masondb945352007-10-15 16:15:53 -0400641 u64 num_bytes;
642 u64 bytes_used;
Yanbcc63ab2008-07-30 16:29:20 -0400643 u64 max_useless;
Chris Mason54aa1f42007-06-22 14:16:25 -0400644 int ret = 0;
Chris Mason9f3a7422007-08-07 15:52:19 -0400645 int err;
646
Chris Mason0f7d52f2007-04-09 10:42:37 -0400647 while(!list_empty(list)) {
Josef Bacik58176a92007-08-29 15:47:34 -0400648 struct btrfs_root *root;
649
Yan Zhengf321e492008-07-30 09:26:11 -0400650 dirty = list_entry(list->prev, struct btrfs_dirty_root, list);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400651 list_del_init(&dirty->list);
Chris Mason5eda7b52007-06-22 14:16:25 -0400652
Chris Masondb945352007-10-15 16:15:53 -0400653 num_bytes = btrfs_root_used(&dirty->root->root_item);
Josef Bacik58176a92007-08-29 15:47:34 -0400654 root = dirty->latest_root;
Chris Masona2135012008-06-25 16:01:30 -0400655 atomic_inc(&root->fs_info->throttles);
Josef Bacik58176a92007-08-29 15:47:34 -0400656
Chris Mason9f3a7422007-08-07 15:52:19 -0400657 while(1) {
658 trans = btrfs_start_transaction(tree_root, 1);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400659 mutex_lock(&root->fs_info->drop_mutex);
Chris Mason9f3a7422007-08-07 15:52:19 -0400660 ret = btrfs_drop_snapshot(trans, dirty->root);
661 if (ret != -EAGAIN) {
662 break;
663 }
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400664 mutex_unlock(&root->fs_info->drop_mutex);
Josef Bacik58176a92007-08-29 15:47:34 -0400665
Chris Mason9f3a7422007-08-07 15:52:19 -0400666 err = btrfs_update_root(trans,
667 tree_root,
668 &dirty->root->root_key,
669 &dirty->root->root_item);
670 if (err)
671 ret = err;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400672 nr = trans->blocks_used;
Chris Mason017e5362008-07-28 15:32:51 -0400673 ret = btrfs_end_transaction(trans, tree_root);
Chris Mason9f3a7422007-08-07 15:52:19 -0400674 BUG_ON(ret);
Chris Masona2135012008-06-25 16:01:30 -0400675
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400676 btrfs_btree_balance_dirty(tree_root, nr);
Chris Mason4dc119042007-10-15 16:18:14 -0400677 cond_resched();
Chris Mason9f3a7422007-08-07 15:52:19 -0400678 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400679 BUG_ON(ret);
Chris Masona2135012008-06-25 16:01:30 -0400680 atomic_dec(&root->fs_info->throttles);
Chris Mason017e5362008-07-28 15:32:51 -0400681 wake_up(&root->fs_info->transaction_throttle);
Josef Bacik58176a92007-08-29 15:47:34 -0400682
Chris Masondb945352007-10-15 16:15:53 -0400683 num_bytes -= btrfs_root_used(&dirty->root->root_item);
684 bytes_used = btrfs_root_used(&root->root_item);
685 if (num_bytes) {
Chris Masone02119d2008-09-05 16:13:11 -0400686 btrfs_record_root_in_trans(root);
Chris Mason5f39d392007-10-15 16:14:19 -0400687 btrfs_set_root_used(&root->root_item,
Chris Masondb945352007-10-15 16:15:53 -0400688 bytes_used - num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -0400689 }
Chris Masona2135012008-06-25 16:01:30 -0400690
Chris Mason9f3a7422007-08-07 15:52:19 -0400691 ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key);
Josef Bacik58176a92007-08-29 15:47:34 -0400692 if (ret) {
693 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -0400694 break;
Josef Bacik58176a92007-08-29 15:47:34 -0400695 }
Chris Masona2135012008-06-25 16:01:30 -0400696 mutex_unlock(&root->fs_info->drop_mutex);
697
Yanbcc63ab2008-07-30 16:29:20 -0400698 spin_lock(&root->list_lock);
699 list_del_init(&dirty->root->dead_list);
700 if (!list_empty(&root->dead_list)) {
701 struct btrfs_root *oldest;
702 oldest = list_entry(root->dead_list.prev,
703 struct btrfs_root, dead_list);
704 max_useless = oldest->root_key.offset - 1;
705 } else {
706 max_useless = root->root_key.offset - 1;
707 }
708 spin_unlock(&root->list_lock);
709
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400710 nr = trans->blocks_used;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400711 ret = btrfs_end_transaction(trans, tree_root);
712 BUG_ON(ret);
Chris Mason5eda7b52007-06-22 14:16:25 -0400713
Zheng Yane4657682008-09-26 10:04:53 -0400714 ret = btrfs_remove_leaf_refs(root, max_useless, 0);
Yanbcc63ab2008-07-30 16:29:20 -0400715 BUG_ON(ret);
716
Chris Masonf510cfe2007-10-15 16:14:48 -0400717 free_extent_buffer(dirty->root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -0400718 kfree(dirty->root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400719 kfree(dirty);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400720
721 btrfs_btree_balance_dirty(tree_root, nr);
Chris Mason4dc119042007-10-15 16:18:14 -0400722 cond_resched();
Chris Mason0f7d52f2007-04-09 10:42:37 -0400723 }
Chris Mason54aa1f42007-06-22 14:16:25 -0400724 return ret;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400725}
726
Chris Masond352ac62008-09-29 15:18:18 -0400727/*
728 * new snapshots need to be created at a very specific time in the
729 * transaction commit. This does the actual creation
730 */
Chris Mason80b67942008-02-01 16:35:04 -0500731static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
Chris Mason3063d292008-01-08 15:46:30 -0500732 struct btrfs_fs_info *fs_info,
733 struct btrfs_pending_snapshot *pending)
734{
735 struct btrfs_key key;
Chris Mason80b67942008-02-01 16:35:04 -0500736 struct btrfs_root_item *new_root_item;
Chris Mason3063d292008-01-08 15:46:30 -0500737 struct btrfs_root *tree_root = fs_info->tree_root;
738 struct btrfs_root *root = pending->root;
739 struct extent_buffer *tmp;
Chris Mason925baed2008-06-25 16:01:30 -0400740 struct extent_buffer *old;
Chris Mason3063d292008-01-08 15:46:30 -0500741 int ret;
Sven Wegener3b963622008-06-09 21:57:42 -0400742 int namelen;
Chris Mason3063d292008-01-08 15:46:30 -0500743 u64 objectid;
744
Chris Mason80b67942008-02-01 16:35:04 -0500745 new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
746 if (!new_root_item) {
747 ret = -ENOMEM;
748 goto fail;
749 }
Chris Mason3063d292008-01-08 15:46:30 -0500750 ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid);
751 if (ret)
752 goto fail;
753
Chris Mason80b67942008-02-01 16:35:04 -0500754 memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
Chris Mason3063d292008-01-08 15:46:30 -0500755
756 key.objectid = objectid;
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400757 key.offset = trans->transid;
Chris Mason3063d292008-01-08 15:46:30 -0500758 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
759
Chris Mason925baed2008-06-25 16:01:30 -0400760 old = btrfs_lock_root_node(root);
Chris Mason65b51a02008-08-01 15:11:20 -0400761 btrfs_cow_block(trans, root, old, NULL, 0, &old, 0);
Chris Mason3063d292008-01-08 15:46:30 -0500762
Chris Mason925baed2008-06-25 16:01:30 -0400763 btrfs_copy_root(trans, root, old, &tmp, objectid);
764 btrfs_tree_unlock(old);
765 free_extent_buffer(old);
Chris Mason3063d292008-01-08 15:46:30 -0500766
Chris Mason80b67942008-02-01 16:35:04 -0500767 btrfs_set_root_bytenr(new_root_item, tmp->start);
768 btrfs_set_root_level(new_root_item, btrfs_header_level(tmp));
Yan Zheng84234f32008-10-29 14:49:05 -0400769 btrfs_set_root_generation(new_root_item, trans->transid);
Chris Mason3063d292008-01-08 15:46:30 -0500770 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
Chris Mason80b67942008-02-01 16:35:04 -0500771 new_root_item);
Chris Mason925baed2008-06-25 16:01:30 -0400772 btrfs_tree_unlock(tmp);
Chris Mason3063d292008-01-08 15:46:30 -0500773 free_extent_buffer(tmp);
774 if (ret)
775 goto fail;
776
777 /*
778 * insert the directory item
779 */
780 key.offset = (u64)-1;
Sven Wegener3b963622008-06-09 21:57:42 -0400781 namelen = strlen(pending->name);
Chris Mason3063d292008-01-08 15:46:30 -0500782 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
Sven Wegener3b963622008-06-09 21:57:42 -0400783 pending->name, namelen,
Chris Mason3063d292008-01-08 15:46:30 -0500784 root->fs_info->sb->s_root->d_inode->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -0400785 &key, BTRFS_FT_DIR, 0);
Chris Mason3063d292008-01-08 15:46:30 -0500786
787 if (ret)
788 goto fail;
789
790 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
791 pending->name, strlen(pending->name), objectid,
Josef Bacikaec74772008-07-24 12:12:38 -0400792 root->fs_info->sb->s_root->d_inode->i_ino, 0);
Sven Wegener3b963622008-06-09 21:57:42 -0400793
794 /* Invalidate existing dcache entry for new snapshot. */
795 btrfs_invalidate_dcache_root(root, pending->name, namelen);
796
Chris Mason3063d292008-01-08 15:46:30 -0500797fail:
Chris Mason80b67942008-02-01 16:35:04 -0500798 kfree(new_root_item);
Chris Mason3063d292008-01-08 15:46:30 -0500799 return ret;
800}
801
Chris Masond352ac62008-09-29 15:18:18 -0400802/*
803 * create all the snapshots we've scheduled for creation
804 */
Chris Mason80b67942008-02-01 16:35:04 -0500805static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
806 struct btrfs_fs_info *fs_info)
Chris Mason3063d292008-01-08 15:46:30 -0500807{
808 struct btrfs_pending_snapshot *pending;
809 struct list_head *head = &trans->transaction->pending_snapshots;
810 int ret;
811
812 while(!list_empty(head)) {
813 pending = list_entry(head->next,
814 struct btrfs_pending_snapshot, list);
815 ret = create_pending_snapshot(trans, fs_info, pending);
816 BUG_ON(ret);
817 list_del(&pending->list);
818 kfree(pending->name);
819 kfree(pending);
820 }
Chris Masondc17ff82008-01-08 15:46:30 -0500821 return 0;
822}
823
Chris Mason79154b12007-03-22 15:59:16 -0400824int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
825 struct btrfs_root *root)
826{
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400827 unsigned long joined = 0;
828 unsigned long timeout = 1;
Chris Mason79154b12007-03-22 15:59:16 -0400829 struct btrfs_transaction *cur_trans;
Chris Mason8fd17792007-04-19 21:01:03 -0400830 struct btrfs_transaction *prev_trans = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -0400831 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400832 struct list_head dirty_fs_roots;
Chris Masond1310b22008-01-24 16:13:08 -0500833 struct extent_io_tree *pinned_copy;
Chris Mason79154b12007-03-22 15:59:16 -0400834 DEFINE_WAIT(wait);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400835 int ret;
Chris Mason79154b12007-03-22 15:59:16 -0400836
Chris Mason0f7d52f2007-04-09 10:42:37 -0400837 INIT_LIST_HEAD(&dirty_fs_roots);
Chris Mason79154b12007-03-22 15:59:16 -0400838 mutex_lock(&root->fs_info->trans_mutex);
839 if (trans->transaction->in_commit) {
840 cur_trans = trans->transaction;
841 trans->transaction->use_count++;
Chris Masonccd467d2007-06-28 15:57:36 -0400842 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400843 btrfs_end_transaction(trans, root);
Chris Masonccd467d2007-06-28 15:57:36 -0400844
Chris Mason79154b12007-03-22 15:59:16 -0400845 ret = wait_for_commit(root, cur_trans);
846 BUG_ON(ret);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400847
848 mutex_lock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400849 put_transaction(cur_trans);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400850 mutex_unlock(&root->fs_info->trans_mutex);
851
Chris Mason79154b12007-03-22 15:59:16 -0400852 return 0;
853 }
Chris Mason4313b392008-01-03 09:08:48 -0500854
855 pinned_copy = kmalloc(sizeof(*pinned_copy), GFP_NOFS);
856 if (!pinned_copy)
857 return -ENOMEM;
858
Chris Masond1310b22008-01-24 16:13:08 -0500859 extent_io_tree_init(pinned_copy,
Chris Mason4313b392008-01-03 09:08:48 -0500860 root->fs_info->btree_inode->i_mapping, GFP_NOFS);
861
Chris Mason2c90e5d2007-04-02 10:50:19 -0400862 trans->transaction->in_commit = 1;
Chris Masonf9295742008-07-17 12:54:14 -0400863 trans->transaction->blocked = 1;
Chris Masonccd467d2007-06-28 15:57:36 -0400864 cur_trans = trans->transaction;
865 if (cur_trans->list.prev != &root->fs_info->trans_list) {
866 prev_trans = list_entry(cur_trans->list.prev,
867 struct btrfs_transaction, list);
868 if (!prev_trans->commit_done) {
869 prev_trans->use_count++;
Chris Masonccd467d2007-06-28 15:57:36 -0400870 mutex_unlock(&root->fs_info->trans_mutex);
871
872 wait_for_commit(root, prev_trans);
Chris Masonccd467d2007-06-28 15:57:36 -0400873
Chris Masonccd467d2007-06-28 15:57:36 -0400874 mutex_lock(&root->fs_info->trans_mutex);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400875 put_transaction(prev_trans);
Chris Masonccd467d2007-06-28 15:57:36 -0400876 }
877 }
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400878
879 do {
Yan Zheng7ea394f2008-08-05 13:05:02 -0400880 int snap_pending = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400881 joined = cur_trans->num_joined;
Yan Zheng7ea394f2008-08-05 13:05:02 -0400882 if (!list_empty(&trans->transaction->pending_snapshots))
883 snap_pending = 1;
884
Chris Mason2c90e5d2007-04-02 10:50:19 -0400885 WARN_ON(cur_trans != trans->transaction);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400886 prepare_to_wait(&cur_trans->writer_wait, &wait,
Chris Mason79154b12007-03-22 15:59:16 -0400887 TASK_UNINTERRUPTIBLE);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400888
889 if (cur_trans->num_writers > 1)
890 timeout = MAX_SCHEDULE_TIMEOUT;
891 else
892 timeout = 1;
893
Chris Mason79154b12007-03-22 15:59:16 -0400894 mutex_unlock(&root->fs_info->trans_mutex);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400895
Yan Zheng7ea394f2008-08-05 13:05:02 -0400896 if (snap_pending) {
897 ret = btrfs_wait_ordered_extents(root, 1);
898 BUG_ON(ret);
899 }
900
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400901 schedule_timeout(timeout);
902
Chris Mason79154b12007-03-22 15:59:16 -0400903 mutex_lock(&root->fs_info->trans_mutex);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400904 finish_wait(&cur_trans->writer_wait, &wait);
905 } while (cur_trans->num_writers > 1 ||
906 (cur_trans->num_joined != joined));
907
Chris Mason3063d292008-01-08 15:46:30 -0500908 ret = create_pending_snapshots(trans, root->fs_info);
909 BUG_ON(ret);
910
Chris Mason2c90e5d2007-04-02 10:50:19 -0400911 WARN_ON(cur_trans != trans->transaction);
Chris Masondc17ff82008-01-08 15:46:30 -0500912
Chris Masone02119d2008-09-05 16:13:11 -0400913 /* btrfs_commit_tree_roots is responsible for getting the
914 * various roots consistent with each other. Every pointer
915 * in the tree of tree roots has to point to the most up to date
916 * root for every subvolume and other tree. So, we have to keep
917 * the tree logging code from jumping in and changing any
918 * of the trees.
919 *
920 * At this point in the commit, there can't be any tree-log
921 * writers, but a little lower down we drop the trans mutex
922 * and let new people in. By holding the tree_log_mutex
923 * from now until after the super is written, we avoid races
924 * with the tree-log code.
925 */
926 mutex_lock(&root->fs_info->tree_log_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -0400927 /*
928 * keep tree reloc code from adding new reloc trees
929 */
930 mutex_lock(&root->fs_info->tree_reloc_mutex);
931
Chris Masone02119d2008-09-05 16:13:11 -0400932
Chris Mason54aa1f42007-06-22 14:16:25 -0400933 ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix,
934 &dirty_fs_roots);
935 BUG_ON(ret);
936
Chris Masone02119d2008-09-05 16:13:11 -0400937 /* add_dirty_roots gets rid of all the tree log roots, it is now
938 * safe to free the root of tree log roots
939 */
940 btrfs_free_log_root_tree(trans, root->fs_info);
941
Chris Mason79154b12007-03-22 15:59:16 -0400942 ret = btrfs_commit_tree_roots(trans, root);
943 BUG_ON(ret);
Chris Mason54aa1f42007-06-22 14:16:25 -0400944
Chris Mason78fae272007-03-25 11:35:08 -0400945 cur_trans = root->fs_info->running_transaction;
Chris Masoncee36a02008-01-15 08:40:48 -0500946 spin_lock(&root->fs_info->new_trans_lock);
Chris Mason78fae272007-03-25 11:35:08 -0400947 root->fs_info->running_transaction = NULL;
Chris Masoncee36a02008-01-15 08:40:48 -0500948 spin_unlock(&root->fs_info->new_trans_lock);
Chris Mason4b52dff2007-06-26 10:06:50 -0400949 btrfs_set_super_generation(&root->fs_info->super_copy,
950 cur_trans->transid);
951 btrfs_set_super_root(&root->fs_info->super_copy,
Chris Masondb945352007-10-15 16:15:53 -0400952 root->fs_info->tree_root->node->start);
953 btrfs_set_super_root_level(&root->fs_info->super_copy,
954 btrfs_header_level(root->fs_info->tree_root->node));
Chris Mason5f39d392007-10-15 16:14:19 -0400955
Chris Mason0b86a832008-03-24 15:01:56 -0400956 btrfs_set_super_chunk_root(&root->fs_info->super_copy,
957 chunk_root->node->start);
958 btrfs_set_super_chunk_root_level(&root->fs_info->super_copy,
959 btrfs_header_level(chunk_root->node));
Yan Zheng84234f32008-10-29 14:49:05 -0400960 btrfs_set_super_chunk_root_generation(&root->fs_info->super_copy,
961 btrfs_header_generation(chunk_root->node));
Chris Masone02119d2008-09-05 16:13:11 -0400962
963 if (!root->fs_info->log_root_recovering) {
964 btrfs_set_super_log_root(&root->fs_info->super_copy, 0);
965 btrfs_set_super_log_root_level(&root->fs_info->super_copy, 0);
966 }
967
Chris Masona061fc82008-05-07 11:43:44 -0400968 memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy,
969 sizeof(root->fs_info->super_copy));
Chris Masonccd467d2007-06-28 15:57:36 -0400970
Chris Mason4313b392008-01-03 09:08:48 -0500971 btrfs_copy_pinned(root, pinned_copy);
Chris Masonccd467d2007-06-28 15:57:36 -0400972
Chris Masonf9295742008-07-17 12:54:14 -0400973 trans->transaction->blocked = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400974 wake_up(&root->fs_info->transaction_throttle);
Chris Masonf9295742008-07-17 12:54:14 -0400975 wake_up(&root->fs_info->transaction_wait);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400976
Chris Mason78fae272007-03-25 11:35:08 -0400977 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400978 ret = btrfs_write_and_wait_transaction(trans, root);
979 BUG_ON(ret);
Chris Mason79154b12007-03-22 15:59:16 -0400980 write_ctree_super(trans, root);
Chris Mason4313b392008-01-03 09:08:48 -0500981
Chris Masone02119d2008-09-05 16:13:11 -0400982 /*
983 * the super is written, we can safely allow the tree-loggers
984 * to go about their business
985 */
986 mutex_unlock(&root->fs_info->tree_log_mutex);
987
Chris Mason4313b392008-01-03 09:08:48 -0500988 btrfs_finish_extent_commit(trans, root, pinned_copy);
Chris Mason4313b392008-01-03 09:08:48 -0500989 kfree(pinned_copy);
990
Zheng Yan1a40e232008-09-26 10:09:34 -0400991 btrfs_drop_dead_reloc_roots(root);
992 mutex_unlock(&root->fs_info->tree_reloc_mutex);
993
994 mutex_lock(&root->fs_info->trans_mutex);
995
Chris Mason2c90e5d2007-04-02 10:50:19 -0400996 cur_trans->commit_done = 1;
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400997 root->fs_info->last_trans_committed = cur_trans->transid;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400998 wake_up(&cur_trans->commit_wait);
Chris Mason79154b12007-03-22 15:59:16 -0400999 put_transaction(cur_trans);
Chris Mason78fae272007-03-25 11:35:08 -04001000 put_transaction(cur_trans);
Josef Bacik58176a92007-08-29 15:47:34 -04001001
Yanbcc63ab2008-07-30 16:29:20 -04001002 list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots);
Chris Masonfacda1e2007-06-08 18:11:48 -04001003 if (root->fs_info->closing)
1004 list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots);
Josef Bacik58176a92007-08-29 15:47:34 -04001005
Chris Mason78fae272007-03-25 11:35:08 -04001006 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason2c90e5d2007-04-02 10:50:19 -04001007 kmem_cache_free(btrfs_trans_handle_cachep, trans);
Chris Mason79154b12007-03-22 15:59:16 -04001008
Chris Masonfacda1e2007-06-08 18:11:48 -04001009 if (root->fs_info->closing) {
Chris Masonfacda1e2007-06-08 18:11:48 -04001010 drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);
Chris Masonfacda1e2007-06-08 18:11:48 -04001011 }
Chris Mason79154b12007-03-22 15:59:16 -04001012 return ret;
1013}
1014
Chris Masond352ac62008-09-29 15:18:18 -04001015/*
1016 * interface function to delete all the snapshots we have scheduled for deletion
1017 */
Chris Masone9d0b132007-08-10 14:06:19 -04001018int btrfs_clean_old_snapshots(struct btrfs_root *root)
1019{
1020 struct list_head dirty_roots;
1021 INIT_LIST_HEAD(&dirty_roots);
Chris Masona74a4b92008-06-25 16:01:31 -04001022again:
Chris Masone9d0b132007-08-10 14:06:19 -04001023 mutex_lock(&root->fs_info->trans_mutex);
1024 list_splice_init(&root->fs_info->dead_roots, &dirty_roots);
1025 mutex_unlock(&root->fs_info->trans_mutex);
1026
1027 if (!list_empty(&dirty_roots)) {
1028 drop_dirty_roots(root, &dirty_roots);
Chris Masona74a4b92008-06-25 16:01:31 -04001029 goto again;
Chris Masone9d0b132007-08-10 14:06:19 -04001030 }
1031 return 0;
1032}