blob: f94c2ad8996c4e7e8cff2d1546d412c6b6e9b139 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Mason79154b12007-03-22 15:59:16 -040019#include <linux/fs.h>
Chris Mason34088782007-06-12 11:36:58 -040020#include <linux/sched.h>
Chris Masond3c2fdc2007-09-17 10:58:06 -040021#include <linux/writeback.h>
Chris Mason5f39d392007-10-15 16:14:19 -040022#include <linux/pagemap.h>
Chris Mason5f2cc082008-11-07 18:22:45 -050023#include <linux/blkdev.h>
Chris Mason79154b12007-03-22 15:59:16 -040024#include "ctree.h"
25#include "disk-io.h"
26#include "transaction.h"
Chris Mason925baed2008-06-25 16:01:30 -040027#include "locking.h"
Yan Zheng31153d82008-07-28 15:32:19 -040028#include "ref-cache.h"
Chris Masone02119d2008-09-05 16:13:11 -040029#include "tree-log.h"
Chris Mason79154b12007-03-22 15:59:16 -040030
Chris Mason0f7d52f2007-04-09 10:42:37 -040031#define BTRFS_ROOT_TRANS_TAG 0
32
Chris Mason80b67942008-02-01 16:35:04 -050033static noinline void put_transaction(struct btrfs_transaction *transaction)
Chris Mason79154b12007-03-22 15:59:16 -040034{
Chris Mason2c90e5d2007-04-02 10:50:19 -040035 WARN_ON(transaction->use_count == 0);
Chris Mason79154b12007-03-22 15:59:16 -040036 transaction->use_count--;
Chris Mason78fae272007-03-25 11:35:08 -040037 if (transaction->use_count == 0) {
Chris Mason8fd17792007-04-19 21:01:03 -040038 list_del_init(&transaction->list);
Chris Mason2c90e5d2007-04-02 10:50:19 -040039 memset(transaction, 0, sizeof(*transaction));
40 kmem_cache_free(btrfs_transaction_cachep, transaction);
Chris Mason78fae272007-03-25 11:35:08 -040041 }
Chris Mason79154b12007-03-22 15:59:16 -040042}
43
Chris Masond352ac62008-09-29 15:18:18 -040044/*
45 * either allocate a new transaction or hop into the existing one
46 */
Chris Mason80b67942008-02-01 16:35:04 -050047static noinline int join_transaction(struct btrfs_root *root)
Chris Mason79154b12007-03-22 15:59:16 -040048{
49 struct btrfs_transaction *cur_trans;
50 cur_trans = root->fs_info->running_transaction;
51 if (!cur_trans) {
Chris Mason2c90e5d2007-04-02 10:50:19 -040052 cur_trans = kmem_cache_alloc(btrfs_transaction_cachep,
53 GFP_NOFS);
Chris Mason79154b12007-03-22 15:59:16 -040054 BUG_ON(!cur_trans);
Chris Mason0f7d52f2007-04-09 10:42:37 -040055 root->fs_info->generation++;
Chris Masone18e4802008-01-18 10:54:22 -050056 root->fs_info->last_alloc = 0;
Chris Mason4529ba42008-01-31 16:45:07 -050057 root->fs_info->last_data_alloc = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -040058 cur_trans->num_writers = 1;
59 cur_trans->num_joined = 0;
Chris Mason0f7d52f2007-04-09 10:42:37 -040060 cur_trans->transid = root->fs_info->generation;
Chris Mason79154b12007-03-22 15:59:16 -040061 init_waitqueue_head(&cur_trans->writer_wait);
62 init_waitqueue_head(&cur_trans->commit_wait);
63 cur_trans->in_commit = 0;
Chris Masonf9295742008-07-17 12:54:14 -040064 cur_trans->blocked = 0;
Chris Masond5719762007-03-23 10:01:08 -040065 cur_trans->use_count = 1;
Chris Mason79154b12007-03-22 15:59:16 -040066 cur_trans->commit_done = 0;
Chris Mason08607c12007-06-08 15:33:54 -040067 cur_trans->start_time = get_seconds();
Chris Mason56bec292009-03-13 10:10:06 -040068
69 cur_trans->delayed_refs.root.rb_node = NULL;
70 cur_trans->delayed_refs.num_entries = 0;
71 cur_trans->delayed_refs.flushing = 0;
72 spin_lock_init(&cur_trans->delayed_refs.lock);
73
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/*
Chris Masond3977122009-01-05 21:25:51 -050091 * this does all the record keeping required to make sure that a reference
92 * counted root is properly recorded in a given transaction. This is required
93 * to make sure the old root from before we joined the transaction is deleted
94 * when the transaction commits
Chris Masond352ac62008-09-29 15:18:18 -040095 */
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++;
Chris Masond3977122009-01-05 21:25:51 -0500150 while (1) {
Chris Masonf9295742008-07-17 12:54:14 -0400151 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;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500188 h->block_group = 0;
Chris Mason26b80032007-08-08 20:17:12 -0400189 h->alloc_exclude_nr = 0;
190 h->alloc_exclude_start = 0;
Chris Mason56bec292009-03-13 10:10:06 -0400191 h->delayed_ref_updates = 0;
Chris Mason79154b12007-03-22 15:59:16 -0400192 root->fs_info->running_transaction->use_count++;
193 mutex_unlock(&root->fs_info->trans_mutex);
194 return h;
195}
196
Chris Masonf9295742008-07-17 12:54:14 -0400197struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
198 int num_blocks)
199{
Sage Weil9ca9ee02008-08-04 10:41:27 -0400200 return start_transaction(root, num_blocks, 1);
Chris Masonf9295742008-07-17 12:54:14 -0400201}
202struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root,
203 int num_blocks)
204{
Sage Weil9ca9ee02008-08-04 10:41:27 -0400205 return start_transaction(root, num_blocks, 0);
Chris Masonf9295742008-07-17 12:54:14 -0400206}
207
Sage Weil9ca9ee02008-08-04 10:41:27 -0400208struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *r,
209 int num_blocks)
210{
211 return start_transaction(r, num_blocks, 2);
212}
213
Chris Masond352ac62008-09-29 15:18:18 -0400214/* wait for a transaction commit to be fully complete */
Chris Mason89ce8a62008-06-25 16:01:31 -0400215static noinline int wait_for_commit(struct btrfs_root *root,
216 struct btrfs_transaction *commit)
217{
218 DEFINE_WAIT(wait);
219 mutex_lock(&root->fs_info->trans_mutex);
Chris Masond3977122009-01-05 21:25:51 -0500220 while (!commit->commit_done) {
Chris Mason89ce8a62008-06-25 16:01:31 -0400221 prepare_to_wait(&commit->commit_wait, &wait,
222 TASK_UNINTERRUPTIBLE);
223 if (commit->commit_done)
224 break;
225 mutex_unlock(&root->fs_info->trans_mutex);
226 schedule();
227 mutex_lock(&root->fs_info->trans_mutex);
228 }
229 mutex_unlock(&root->fs_info->trans_mutex);
230 finish_wait(&commit->commit_wait, &wait);
231 return 0;
232}
233
Chris Masond352ac62008-09-29 15:18:18 -0400234/*
Chris Masond3977122009-01-05 21:25:51 -0500235 * rate limit against the drop_snapshot code. This helps to slow down new
236 * operations if the drop_snapshot code isn't able to keep up.
Chris Masond352ac62008-09-29 15:18:18 -0400237 */
Chris Mason37d1aee2008-07-31 10:48:37 -0400238static void throttle_on_drops(struct btrfs_root *root)
Chris Masonab78c842008-07-29 16:15:18 -0400239{
240 struct btrfs_fs_info *info = root->fs_info;
Chris Mason2dd3e672008-08-04 08:20:15 -0400241 int harder_count = 0;
Chris Masonab78c842008-07-29 16:15:18 -0400242
Chris Mason2dd3e672008-08-04 08:20:15 -0400243harder:
Chris Masonab78c842008-07-29 16:15:18 -0400244 if (atomic_read(&info->throttles)) {
245 DEFINE_WAIT(wait);
246 int thr;
Chris Masonab78c842008-07-29 16:15:18 -0400247 thr = atomic_read(&info->throttle_gen);
248
249 do {
250 prepare_to_wait(&info->transaction_throttle,
251 &wait, TASK_UNINTERRUPTIBLE);
252 if (!atomic_read(&info->throttles)) {
253 finish_wait(&info->transaction_throttle, &wait);
254 break;
255 }
256 schedule();
257 finish_wait(&info->transaction_throttle, &wait);
258 } while (thr == atomic_read(&info->throttle_gen));
Chris Mason2dd3e672008-08-04 08:20:15 -0400259 harder_count++;
260
261 if (root->fs_info->total_ref_cache_size > 1 * 1024 * 1024 &&
262 harder_count < 2)
263 goto harder;
264
265 if (root->fs_info->total_ref_cache_size > 5 * 1024 * 1024 &&
266 harder_count < 10)
267 goto harder;
268
269 if (root->fs_info->total_ref_cache_size > 10 * 1024 * 1024 &&
270 harder_count < 20)
271 goto harder;
Chris Masonab78c842008-07-29 16:15:18 -0400272 }
273}
274
Chris Mason37d1aee2008-07-31 10:48:37 -0400275void btrfs_throttle(struct btrfs_root *root)
276{
277 mutex_lock(&root->fs_info->trans_mutex);
Sage Weil9ca9ee02008-08-04 10:41:27 -0400278 if (!root->fs_info->open_ioctl_trans)
279 wait_current_trans(root);
Chris Mason37d1aee2008-07-31 10:48:37 -0400280 mutex_unlock(&root->fs_info->trans_mutex);
281
282 throttle_on_drops(root);
283}
284
Chris Mason89ce8a62008-06-25 16:01:31 -0400285static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
286 struct btrfs_root *root, int throttle)
Chris Mason79154b12007-03-22 15:59:16 -0400287{
288 struct btrfs_transaction *cur_trans;
Chris Masonab78c842008-07-29 16:15:18 -0400289 struct btrfs_fs_info *info = root->fs_info;
Chris Masond6e4a422007-04-06 15:37:36 -0400290
Chris Mason56bec292009-03-13 10:10:06 -0400291 if (trans->delayed_ref_updates &&
292 (trans->transaction->delayed_refs.flushing ||
293 trans->transaction->delayed_refs.num_entries > 16384)) {
294 btrfs_run_delayed_refs(trans, root, trans->delayed_ref_updates);
295 } else if (trans->transaction->delayed_refs.num_entries > 64) {
296 wake_up_process(root->fs_info->transaction_kthread);
297 }
298
Chris Masonab78c842008-07-29 16:15:18 -0400299 mutex_lock(&info->trans_mutex);
300 cur_trans = info->running_transaction;
Chris Masonccd467d2007-06-28 15:57:36 -0400301 WARN_ON(cur_trans != trans->transaction);
Chris Masond5719762007-03-23 10:01:08 -0400302 WARN_ON(cur_trans->num_writers < 1);
Chris Masonccd467d2007-06-28 15:57:36 -0400303 cur_trans->num_writers--;
Chris Mason89ce8a62008-06-25 16:01:31 -0400304
Chris Mason79154b12007-03-22 15:59:16 -0400305 if (waitqueue_active(&cur_trans->writer_wait))
306 wake_up(&cur_trans->writer_wait);
Chris Mason79154b12007-03-22 15:59:16 -0400307 put_transaction(cur_trans);
Chris Masonab78c842008-07-29 16:15:18 -0400308 mutex_unlock(&info->trans_mutex);
Chris Masond6025572007-03-30 14:27:56 -0400309 memset(trans, 0, sizeof(*trans));
Chris Mason2c90e5d2007-04-02 10:50:19 -0400310 kmem_cache_free(btrfs_trans_handle_cachep, trans);
Chris Masonab78c842008-07-29 16:15:18 -0400311
312 if (throttle)
Chris Mason37d1aee2008-07-31 10:48:37 -0400313 throttle_on_drops(root);
Chris Masonab78c842008-07-29 16:15:18 -0400314
Chris Mason79154b12007-03-22 15:59:16 -0400315 return 0;
316}
317
Chris Mason89ce8a62008-06-25 16:01:31 -0400318int btrfs_end_transaction(struct btrfs_trans_handle *trans,
319 struct btrfs_root *root)
320{
321 return __btrfs_end_transaction(trans, root, 0);
322}
323
324int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
325 struct btrfs_root *root)
326{
327 return __btrfs_end_transaction(trans, root, 1);
328}
329
Chris Masond352ac62008-09-29 15:18:18 -0400330/*
331 * when btree blocks are allocated, they have some corresponding bits set for
332 * them in one of two extent_io trees. This is used to make sure all of
333 * those extents are on disk for transaction or log commit
334 */
Chris Masond0c803c2008-09-11 16:17:57 -0400335int btrfs_write_and_wait_marked_extents(struct btrfs_root *root,
336 struct extent_io_tree *dirty_pages)
Chris Mason79154b12007-03-22 15:59:16 -0400337{
Chris Mason7c4452b2007-04-28 09:29:35 -0400338 int ret;
Chris Mason777e6bd2008-08-15 15:34:15 -0400339 int err = 0;
Chris Mason7c4452b2007-04-28 09:29:35 -0400340 int werr = 0;
341 struct page *page;
Chris Mason7c4452b2007-04-28 09:29:35 -0400342 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Mason777e6bd2008-08-15 15:34:15 -0400343 u64 start = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400344 u64 end;
345 unsigned long index;
Chris Mason7c4452b2007-04-28 09:29:35 -0400346
Chris Masond3977122009-01-05 21:25:51 -0500347 while (1) {
Chris Mason777e6bd2008-08-15 15:34:15 -0400348 ret = find_first_extent_bit(dirty_pages, start, &start, &end,
Chris Mason5f39d392007-10-15 16:14:19 -0400349 EXTENT_DIRTY);
350 if (ret)
Chris Mason7c4452b2007-04-28 09:29:35 -0400351 break;
Chris Masond3977122009-01-05 21:25:51 -0500352 while (start <= end) {
Chris Mason777e6bd2008-08-15 15:34:15 -0400353 cond_resched();
354
Chris Mason5f39d392007-10-15 16:14:19 -0400355 index = start >> PAGE_CACHE_SHIFT;
Chris Mason35ebb932007-10-30 16:56:53 -0400356 start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
Chris Mason4bef0842008-09-08 11:18:08 -0400357 page = find_get_page(btree_inode->i_mapping, index);
Chris Mason7c4452b2007-04-28 09:29:35 -0400358 if (!page)
359 continue;
Chris Mason4bef0842008-09-08 11:18:08 -0400360
361 btree_lock_page_hook(page);
362 if (!page->mapping) {
363 unlock_page(page);
364 page_cache_release(page);
365 continue;
366 }
367
Chris Mason6702ed42007-08-07 16:15:09 -0400368 if (PageWriteback(page)) {
369 if (PageDirty(page))
370 wait_on_page_writeback(page);
371 else {
372 unlock_page(page);
373 page_cache_release(page);
374 continue;
375 }
376 }
Chris Mason7c4452b2007-04-28 09:29:35 -0400377 err = write_one_page(page, 0);
378 if (err)
379 werr = err;
380 page_cache_release(page);
381 }
382 }
Chris Masond3977122009-01-05 21:25:51 -0500383 while (1) {
Chris Mason777e6bd2008-08-15 15:34:15 -0400384 ret = find_first_extent_bit(dirty_pages, 0, &start, &end,
385 EXTENT_DIRTY);
386 if (ret)
387 break;
388
389 clear_extent_dirty(dirty_pages, start, end, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -0500390 while (start <= end) {
Chris Mason777e6bd2008-08-15 15:34:15 -0400391 index = start >> PAGE_CACHE_SHIFT;
392 start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
393 page = find_get_page(btree_inode->i_mapping, index);
394 if (!page)
395 continue;
396 if (PageDirty(page)) {
Chris Mason4bef0842008-09-08 11:18:08 -0400397 btree_lock_page_hook(page);
398 wait_on_page_writeback(page);
Chris Mason777e6bd2008-08-15 15:34:15 -0400399 err = write_one_page(page, 0);
400 if (err)
401 werr = err;
402 }
Chris Mason105d9312008-11-18 12:13:12 -0500403 wait_on_page_writeback(page);
Chris Mason777e6bd2008-08-15 15:34:15 -0400404 page_cache_release(page);
405 cond_resched();
406 }
407 }
Chris Mason7c4452b2007-04-28 09:29:35 -0400408 if (err)
409 werr = err;
410 return werr;
Chris Mason79154b12007-03-22 15:59:16 -0400411}
412
Chris Masond0c803c2008-09-11 16:17:57 -0400413int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
414 struct btrfs_root *root)
415{
416 if (!trans || !trans->transaction) {
417 struct inode *btree_inode;
418 btree_inode = root->fs_info->btree_inode;
419 return filemap_write_and_wait(btree_inode->i_mapping);
420 }
421 return btrfs_write_and_wait_marked_extents(root,
422 &trans->transaction->dirty_pages);
423}
424
Chris Masond352ac62008-09-29 15:18:18 -0400425/*
426 * this is used to update the root pointer in the tree of tree roots.
427 *
428 * But, in the case of the extent allocation tree, updating the root
429 * pointer may allocate blocks which may change the root of the extent
430 * allocation tree.
431 *
432 * So, this loops and repeats and makes sure the cowonly root didn't
433 * change while the root pointer was being updated in the metadata.
434 */
Chris Mason0b86a832008-03-24 15:01:56 -0400435static int update_cowonly_root(struct btrfs_trans_handle *trans,
436 struct btrfs_root *root)
437{
438 int ret;
439 u64 old_root_bytenr;
440 struct btrfs_root *tree_root = root->fs_info->tree_root;
441
442 btrfs_write_dirty_block_groups(trans, root);
Chris Mason56bec292009-03-13 10:10:06 -0400443
444 ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
445 BUG_ON(ret);
Chris Mason87ef2bb2008-10-30 11:23:27 -0400446
Chris Masond3977122009-01-05 21:25:51 -0500447 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -0400448 old_root_bytenr = btrfs_root_bytenr(&root->root_item);
449 if (old_root_bytenr == root->node->start)
450 break;
451 btrfs_set_root_bytenr(&root->root_item,
452 root->node->start);
453 btrfs_set_root_level(&root->root_item,
454 btrfs_header_level(root->node));
Yan Zheng84234f32008-10-29 14:49:05 -0400455 btrfs_set_root_generation(&root->root_item, trans->transid);
Chris Mason87ef2bb2008-10-30 11:23:27 -0400456
Chris Mason0b86a832008-03-24 15:01:56 -0400457 ret = btrfs_update_root(trans, tree_root,
458 &root->root_key,
459 &root->root_item);
460 BUG_ON(ret);
461 btrfs_write_dirty_block_groups(trans, root);
Chris Mason56bec292009-03-13 10:10:06 -0400462
463 ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
464 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -0400465 }
466 return 0;
467}
468
Chris Masond352ac62008-09-29 15:18:18 -0400469/*
470 * update all the cowonly tree roots on disk
471 */
Chris Mason79154b12007-03-22 15:59:16 -0400472int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans,
473 struct btrfs_root *root)
474{
Chris Mason79154b12007-03-22 15:59:16 -0400475 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason0b86a832008-03-24 15:01:56 -0400476 struct list_head *next;
Yan Zheng84234f32008-10-29 14:49:05 -0400477 struct extent_buffer *eb;
Chris Mason56bec292009-03-13 10:10:06 -0400478 int ret;
Yan Zheng84234f32008-10-29 14:49:05 -0400479
Chris Mason56bec292009-03-13 10:10:06 -0400480 ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
481 BUG_ON(ret);
Chris Mason87ef2bb2008-10-30 11:23:27 -0400482
Yan Zheng84234f32008-10-29 14:49:05 -0400483 eb = btrfs_lock_root_node(fs_info->tree_root);
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400484 btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 0, &eb);
Yan Zheng84234f32008-10-29 14:49:05 -0400485 btrfs_tree_unlock(eb);
486 free_extent_buffer(eb);
Chris Mason79154b12007-03-22 15:59:16 -0400487
Chris Mason56bec292009-03-13 10:10:06 -0400488 ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
489 BUG_ON(ret);
Chris Mason87ef2bb2008-10-30 11:23:27 -0400490
Chris Masond3977122009-01-05 21:25:51 -0500491 while (!list_empty(&fs_info->dirty_cowonly_roots)) {
Chris Mason0b86a832008-03-24 15:01:56 -0400492 next = fs_info->dirty_cowonly_roots.next;
493 list_del_init(next);
494 root = list_entry(next, struct btrfs_root, dirty_list);
Chris Mason87ef2bb2008-10-30 11:23:27 -0400495
Chris Mason0b86a832008-03-24 15:01:56 -0400496 update_cowonly_root(trans, root);
Chris Mason56bec292009-03-13 10:10:06 -0400497
498 ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
499 BUG_ON(ret);
Chris Mason79154b12007-03-22 15:59:16 -0400500 }
501 return 0;
502}
503
Chris Masond352ac62008-09-29 15:18:18 -0400504/*
505 * dead roots are old snapshots that need to be deleted. This allocates
506 * a dirty root struct and adds it into the list of dead roots that need to
507 * be deleted
508 */
Yan Zhengb48652c2008-08-04 23:23:47 -0400509int btrfs_add_dead_root(struct btrfs_root *root, struct btrfs_root *latest)
Chris Mason5eda7b52007-06-22 14:16:25 -0400510{
Yan Zhengf321e492008-07-30 09:26:11 -0400511 struct btrfs_dirty_root *dirty;
Chris Mason5eda7b52007-06-22 14:16:25 -0400512
513 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
514 if (!dirty)
515 return -ENOMEM;
Chris Mason5eda7b52007-06-22 14:16:25 -0400516 dirty->root = root;
Chris Mason5ce14bb2007-09-11 11:15:39 -0400517 dirty->latest_root = latest;
Yan Zhengb48652c2008-08-04 23:23:47 -0400518
519 mutex_lock(&root->fs_info->trans_mutex);
520 list_add(&dirty->list, &latest->fs_info->dead_roots);
521 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason5eda7b52007-06-22 14:16:25 -0400522 return 0;
523}
524
Chris Masond352ac62008-09-29 15:18:18 -0400525/*
526 * at transaction commit time we need to schedule the old roots for
527 * deletion via btrfs_drop_snapshot. This runs through all the
528 * reference counted roots that were modified in the current
529 * transaction and puts them into the drop list
530 */
Chris Mason80b67942008-02-01 16:35:04 -0500531static noinline int add_dirty_roots(struct btrfs_trans_handle *trans,
532 struct radix_tree_root *radix,
533 struct list_head *list)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400534{
Yan Zhengf321e492008-07-30 09:26:11 -0400535 struct btrfs_dirty_root *dirty;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400536 struct btrfs_root *gang[8];
537 struct btrfs_root *root;
538 int i;
539 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400540 int err = 0;
Chris Mason5eda7b52007-06-22 14:16:25 -0400541 u32 refs;
Chris Mason54aa1f42007-06-22 14:16:25 -0400542
Chris Masond3977122009-01-05 21:25:51 -0500543 while (1) {
Chris Mason0f7d52f2007-04-09 10:42:37 -0400544 ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0,
545 ARRAY_SIZE(gang),
546 BTRFS_ROOT_TRANS_TAG);
547 if (ret == 0)
548 break;
549 for (i = 0; i < ret; i++) {
550 root = gang[i];
Chris Mason2619ba12007-04-10 16:58:11 -0400551 radix_tree_tag_clear(radix,
552 (unsigned long)root->root_key.objectid,
553 BTRFS_ROOT_TRANS_TAG);
Yan Zheng31153d82008-07-28 15:32:19 -0400554
555 BUG_ON(!root->ref_tree);
Chris Mason017e5362008-07-28 15:32:51 -0400556 dirty = root->dirty_root;
Yan Zheng31153d82008-07-28 15:32:19 -0400557
Chris Masone02119d2008-09-05 16:13:11 -0400558 btrfs_free_log(trans, root);
Yan Zhengf82d02d2008-10-29 14:49:05 -0400559 btrfs_free_reloc_root(trans, root);
Chris Masone02119d2008-09-05 16:13:11 -0400560
Chris Mason0f7d52f2007-04-09 10:42:37 -0400561 if (root->commit_root == root->node) {
Chris Masondb945352007-10-15 16:15:53 -0400562 WARN_ON(root->node->start !=
563 btrfs_root_bytenr(&root->root_item));
Yan Zheng31153d82008-07-28 15:32:19 -0400564
Chris Mason5f39d392007-10-15 16:14:19 -0400565 free_extent_buffer(root->commit_root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400566 root->commit_root = NULL;
Yan Zheng7ea394f2008-08-05 13:05:02 -0400567 root->dirty_root = NULL;
Yanbcc63ab2008-07-30 16:29:20 -0400568
569 spin_lock(&root->list_lock);
570 list_del_init(&dirty->root->dead_list);
571 spin_unlock(&root->list_lock);
572
Yan Zheng31153d82008-07-28 15:32:19 -0400573 kfree(dirty->root);
574 kfree(dirty);
Josef Bacik58176a92007-08-29 15:47:34 -0400575
576 /* make sure to update the root on disk
577 * so we get any updates to the block used
578 * counts
579 */
580 err = btrfs_update_root(trans,
581 root->fs_info->tree_root,
582 &root->root_key,
583 &root->root_item);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400584 continue;
585 }
Chris Mason9f3a7422007-08-07 15:52:19 -0400586
587 memset(&root->root_item.drop_progress, 0,
588 sizeof(struct btrfs_disk_key));
589 root->root_item.drop_level = 0;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400590 root->commit_root = NULL;
Yan Zheng7ea394f2008-08-05 13:05:02 -0400591 root->dirty_root = NULL;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400592 root->root_key.offset = root->fs_info->generation;
Chris Masondb945352007-10-15 16:15:53 -0400593 btrfs_set_root_bytenr(&root->root_item,
594 root->node->start);
595 btrfs_set_root_level(&root->root_item,
596 btrfs_header_level(root->node));
Yan Zheng84234f32008-10-29 14:49:05 -0400597 btrfs_set_root_generation(&root->root_item,
598 root->root_key.offset);
599
Chris Mason0f7d52f2007-04-09 10:42:37 -0400600 err = btrfs_insert_root(trans, root->fs_info->tree_root,
601 &root->root_key,
602 &root->root_item);
Chris Mason54aa1f42007-06-22 14:16:25 -0400603 if (err)
604 break;
Chris Mason9f3a7422007-08-07 15:52:19 -0400605
606 refs = btrfs_root_refs(&dirty->root->root_item);
607 btrfs_set_root_refs(&dirty->root->root_item, refs - 1);
Chris Mason5eda7b52007-06-22 14:16:25 -0400608 err = btrfs_update_root(trans, root->fs_info->tree_root,
Chris Mason9f3a7422007-08-07 15:52:19 -0400609 &dirty->root->root_key,
610 &dirty->root->root_item);
Chris Mason5eda7b52007-06-22 14:16:25 -0400611
612 BUG_ON(err);
Chris Mason9f3a7422007-08-07 15:52:19 -0400613 if (refs == 1) {
Chris Mason5eda7b52007-06-22 14:16:25 -0400614 list_add(&dirty->list, list);
Chris Mason9f3a7422007-08-07 15:52:19 -0400615 } else {
616 WARN_ON(1);
Yan Zheng31153d82008-07-28 15:32:19 -0400617 free_extent_buffer(dirty->root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -0400618 kfree(dirty->root);
Chris Mason5eda7b52007-06-22 14:16:25 -0400619 kfree(dirty);
Chris Mason9f3a7422007-08-07 15:52:19 -0400620 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400621 }
622 }
Chris Mason54aa1f42007-06-22 14:16:25 -0400623 return err;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400624}
625
Chris Masond352ac62008-09-29 15:18:18 -0400626/*
627 * defrag a given btree. If cacheonly == 1, this won't read from the disk,
628 * otherwise every leaf in the btree is read and defragged.
629 */
Chris Masone9d0b132007-08-10 14:06:19 -0400630int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
631{
632 struct btrfs_fs_info *info = root->fs_info;
633 int ret;
634 struct btrfs_trans_handle *trans;
Chris Masond3c2fdc2007-09-17 10:58:06 -0400635 unsigned long nr;
Chris Masone9d0b132007-08-10 14:06:19 -0400636
Chris Masona2135012008-06-25 16:01:30 -0400637 smp_mb();
Chris Masone9d0b132007-08-10 14:06:19 -0400638 if (root->defrag_running)
639 return 0;
Chris Masone9d0b132007-08-10 14:06:19 -0400640 trans = btrfs_start_transaction(root, 1);
Chris Mason6b800532007-10-15 16:17:34 -0400641 while (1) {
Chris Masone9d0b132007-08-10 14:06:19 -0400642 root->defrag_running = 1;
643 ret = btrfs_defrag_leaves(trans, root, cacheonly);
Chris Masond3c2fdc2007-09-17 10:58:06 -0400644 nr = trans->blocks_used;
Chris Masone9d0b132007-08-10 14:06:19 -0400645 btrfs_end_transaction(trans, root);
Chris Masond3c2fdc2007-09-17 10:58:06 -0400646 btrfs_btree_balance_dirty(info->tree_root, nr);
Chris Masone9d0b132007-08-10 14:06:19 -0400647 cond_resched();
648
Chris Masone9d0b132007-08-10 14:06:19 -0400649 trans = btrfs_start_transaction(root, 1);
Chris Mason3f157a22008-06-25 16:01:31 -0400650 if (root->fs_info->closing || ret != -EAGAIN)
Chris Masone9d0b132007-08-10 14:06:19 -0400651 break;
652 }
653 root->defrag_running = 0;
Chris Masona2135012008-06-25 16:01:30 -0400654 smp_mb();
Chris Masone9d0b132007-08-10 14:06:19 -0400655 btrfs_end_transaction(trans, root);
656 return 0;
657}
658
Chris Masond352ac62008-09-29 15:18:18 -0400659/*
660 * Given a list of roots that need to be deleted, call btrfs_drop_snapshot on
661 * all of them
662 */
Chris Mason80b67942008-02-01 16:35:04 -0500663static noinline int drop_dirty_roots(struct btrfs_root *tree_root,
664 struct list_head *list)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400665{
Yan Zhengf321e492008-07-30 09:26:11 -0400666 struct btrfs_dirty_root *dirty;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400667 struct btrfs_trans_handle *trans;
Chris Masond3c2fdc2007-09-17 10:58:06 -0400668 unsigned long nr;
Chris Masondb945352007-10-15 16:15:53 -0400669 u64 num_bytes;
670 u64 bytes_used;
Yanbcc63ab2008-07-30 16:29:20 -0400671 u64 max_useless;
Chris Mason54aa1f42007-06-22 14:16:25 -0400672 int ret = 0;
Chris Mason9f3a7422007-08-07 15:52:19 -0400673 int err;
674
Chris Masond3977122009-01-05 21:25:51 -0500675 while (!list_empty(list)) {
Josef Bacik58176a92007-08-29 15:47:34 -0400676 struct btrfs_root *root;
677
Yan Zhengf321e492008-07-30 09:26:11 -0400678 dirty = list_entry(list->prev, struct btrfs_dirty_root, list);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400679 list_del_init(&dirty->list);
Chris Mason5eda7b52007-06-22 14:16:25 -0400680
Chris Masondb945352007-10-15 16:15:53 -0400681 num_bytes = btrfs_root_used(&dirty->root->root_item);
Josef Bacik58176a92007-08-29 15:47:34 -0400682 root = dirty->latest_root;
Chris Masona2135012008-06-25 16:01:30 -0400683 atomic_inc(&root->fs_info->throttles);
Josef Bacik58176a92007-08-29 15:47:34 -0400684
Chris Masond3977122009-01-05 21:25:51 -0500685 while (1) {
Chris Mason9f3a7422007-08-07 15:52:19 -0400686 trans = btrfs_start_transaction(tree_root, 1);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400687 mutex_lock(&root->fs_info->drop_mutex);
Chris Mason9f3a7422007-08-07 15:52:19 -0400688 ret = btrfs_drop_snapshot(trans, dirty->root);
Chris Masond3977122009-01-05 21:25:51 -0500689 if (ret != -EAGAIN)
Chris Mason9f3a7422007-08-07 15:52:19 -0400690 break;
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400691 mutex_unlock(&root->fs_info->drop_mutex);
Josef Bacik58176a92007-08-29 15:47:34 -0400692
Chris Mason9f3a7422007-08-07 15:52:19 -0400693 err = btrfs_update_root(trans,
694 tree_root,
695 &dirty->root->root_key,
696 &dirty->root->root_item);
697 if (err)
698 ret = err;
Chris Masond3c2fdc2007-09-17 10:58:06 -0400699 nr = trans->blocks_used;
Chris Mason017e5362008-07-28 15:32:51 -0400700 ret = btrfs_end_transaction(trans, tree_root);
Chris Mason9f3a7422007-08-07 15:52:19 -0400701 BUG_ON(ret);
Chris Masona2135012008-06-25 16:01:30 -0400702
Chris Masond3c2fdc2007-09-17 10:58:06 -0400703 btrfs_btree_balance_dirty(tree_root, nr);
Chris Mason4dc119042007-10-15 16:18:14 -0400704 cond_resched();
Chris Mason9f3a7422007-08-07 15:52:19 -0400705 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400706 BUG_ON(ret);
Chris Masona2135012008-06-25 16:01:30 -0400707 atomic_dec(&root->fs_info->throttles);
Chris Mason017e5362008-07-28 15:32:51 -0400708 wake_up(&root->fs_info->transaction_throttle);
Josef Bacik58176a92007-08-29 15:47:34 -0400709
Chris Masondb945352007-10-15 16:15:53 -0400710 num_bytes -= btrfs_root_used(&dirty->root->root_item);
711 bytes_used = btrfs_root_used(&root->root_item);
712 if (num_bytes) {
Yan Zheng24562422009-02-12 14:14:53 -0500713 mutex_lock(&root->fs_info->trans_mutex);
Chris Masone02119d2008-09-05 16:13:11 -0400714 btrfs_record_root_in_trans(root);
Yan Zheng24562422009-02-12 14:14:53 -0500715 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason5f39d392007-10-15 16:14:19 -0400716 btrfs_set_root_used(&root->root_item,
Chris Masondb945352007-10-15 16:15:53 -0400717 bytes_used - num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -0400718 }
Chris Masona2135012008-06-25 16:01:30 -0400719
Chris Mason9f3a7422007-08-07 15:52:19 -0400720 ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key);
Josef Bacik58176a92007-08-29 15:47:34 -0400721 if (ret) {
722 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -0400723 break;
Josef Bacik58176a92007-08-29 15:47:34 -0400724 }
Chris Masona2135012008-06-25 16:01:30 -0400725 mutex_unlock(&root->fs_info->drop_mutex);
726
Yanbcc63ab2008-07-30 16:29:20 -0400727 spin_lock(&root->list_lock);
728 list_del_init(&dirty->root->dead_list);
729 if (!list_empty(&root->dead_list)) {
730 struct btrfs_root *oldest;
731 oldest = list_entry(root->dead_list.prev,
732 struct btrfs_root, dead_list);
733 max_useless = oldest->root_key.offset - 1;
734 } else {
735 max_useless = root->root_key.offset - 1;
736 }
737 spin_unlock(&root->list_lock);
738
Chris Masond3c2fdc2007-09-17 10:58:06 -0400739 nr = trans->blocks_used;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400740 ret = btrfs_end_transaction(trans, tree_root);
741 BUG_ON(ret);
Chris Mason5eda7b52007-06-22 14:16:25 -0400742
Zheng Yane4657682008-09-26 10:04:53 -0400743 ret = btrfs_remove_leaf_refs(root, max_useless, 0);
Yanbcc63ab2008-07-30 16:29:20 -0400744 BUG_ON(ret);
745
Chris Masonf510cfe2007-10-15 16:14:48 -0400746 free_extent_buffer(dirty->root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -0400747 kfree(dirty->root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400748 kfree(dirty);
Chris Masond3c2fdc2007-09-17 10:58:06 -0400749
750 btrfs_btree_balance_dirty(tree_root, nr);
Chris Mason4dc119042007-10-15 16:18:14 -0400751 cond_resched();
Chris Mason0f7d52f2007-04-09 10:42:37 -0400752 }
Chris Mason54aa1f42007-06-22 14:16:25 -0400753 return ret;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400754}
755
Chris Masond352ac62008-09-29 15:18:18 -0400756/*
757 * new snapshots need to be created at a very specific time in the
758 * transaction commit. This does the actual creation
759 */
Chris Mason80b67942008-02-01 16:35:04 -0500760static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
Chris Mason3063d292008-01-08 15:46:30 -0500761 struct btrfs_fs_info *fs_info,
762 struct btrfs_pending_snapshot *pending)
763{
764 struct btrfs_key key;
Chris Mason80b67942008-02-01 16:35:04 -0500765 struct btrfs_root_item *new_root_item;
Chris Mason3063d292008-01-08 15:46:30 -0500766 struct btrfs_root *tree_root = fs_info->tree_root;
767 struct btrfs_root *root = pending->root;
768 struct extent_buffer *tmp;
Chris Mason925baed2008-06-25 16:01:30 -0400769 struct extent_buffer *old;
Chris Mason3063d292008-01-08 15:46:30 -0500770 int ret;
771 u64 objectid;
772
Chris Mason80b67942008-02-01 16:35:04 -0500773 new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
774 if (!new_root_item) {
775 ret = -ENOMEM;
776 goto fail;
777 }
Chris Mason3063d292008-01-08 15:46:30 -0500778 ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid);
779 if (ret)
780 goto fail;
781
Yan Zheng80ff3852008-10-30 14:20:02 -0400782 btrfs_record_root_in_trans(root);
783 btrfs_set_root_last_snapshot(&root->root_item, trans->transid);
Chris Mason80b67942008-02-01 16:35:04 -0500784 memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
Chris Mason3063d292008-01-08 15:46:30 -0500785
786 key.objectid = objectid;
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400787 key.offset = trans->transid;
Chris Mason3063d292008-01-08 15:46:30 -0500788 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
789
Chris Mason925baed2008-06-25 16:01:30 -0400790 old = btrfs_lock_root_node(root);
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400791 btrfs_cow_block(trans, root, old, NULL, 0, &old);
Chris Mason3063d292008-01-08 15:46:30 -0500792
Chris Mason925baed2008-06-25 16:01:30 -0400793 btrfs_copy_root(trans, root, old, &tmp, objectid);
794 btrfs_tree_unlock(old);
795 free_extent_buffer(old);
Chris Mason3063d292008-01-08 15:46:30 -0500796
Chris Mason80b67942008-02-01 16:35:04 -0500797 btrfs_set_root_bytenr(new_root_item, tmp->start);
798 btrfs_set_root_level(new_root_item, btrfs_header_level(tmp));
Yan Zheng84234f32008-10-29 14:49:05 -0400799 btrfs_set_root_generation(new_root_item, trans->transid);
Chris Mason3063d292008-01-08 15:46:30 -0500800 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
Chris Mason80b67942008-02-01 16:35:04 -0500801 new_root_item);
Chris Mason925baed2008-06-25 16:01:30 -0400802 btrfs_tree_unlock(tmp);
Chris Mason3063d292008-01-08 15:46:30 -0500803 free_extent_buffer(tmp);
804 if (ret)
805 goto fail;
806
Chris Mason3de45862008-11-17 21:02:50 -0500807 key.offset = (u64)-1;
808 memcpy(&pending->root_key, &key, sizeof(key));
809fail:
810 kfree(new_root_item);
811 return ret;
812}
813
814static noinline int finish_pending_snapshot(struct btrfs_fs_info *fs_info,
815 struct btrfs_pending_snapshot *pending)
816{
817 int ret;
818 int namelen;
819 u64 index = 0;
820 struct btrfs_trans_handle *trans;
821 struct inode *parent_inode;
822 struct inode *inode;
Chris Mason0660b5a2008-11-17 20:37:39 -0500823 struct btrfs_root *parent_root;
Chris Mason3de45862008-11-17 21:02:50 -0500824
Chris Mason3394e162008-11-17 20:42:26 -0500825 parent_inode = pending->dentry->d_parent->d_inode;
Chris Mason0660b5a2008-11-17 20:37:39 -0500826 parent_root = BTRFS_I(parent_inode)->root;
Yan Zheng180591b2009-01-06 09:58:06 -0500827 trans = btrfs_join_transaction(parent_root, 1);
Chris Mason3de45862008-11-17 21:02:50 -0500828
Chris Mason3063d292008-01-08 15:46:30 -0500829 /*
830 * insert the directory item
831 */
Sven Wegener3b963622008-06-09 21:57:42 -0400832 namelen = strlen(pending->name);
Chris Mason3de45862008-11-17 21:02:50 -0500833 ret = btrfs_set_inode_index(parent_inode, &index);
Chris Mason0660b5a2008-11-17 20:37:39 -0500834 ret = btrfs_insert_dir_item(trans, parent_root,
Chris Mason3de45862008-11-17 21:02:50 -0500835 pending->name, namelen,
836 parent_inode->i_ino,
837 &pending->root_key, BTRFS_FT_DIR, index);
Chris Mason3063d292008-01-08 15:46:30 -0500838
839 if (ret)
840 goto fail;
Chris Mason0660b5a2008-11-17 20:37:39 -0500841
Yan Zheng52c26172009-01-05 15:43:43 -0500842 btrfs_i_size_write(parent_inode, parent_inode->i_size + namelen * 2);
843 ret = btrfs_update_inode(trans, parent_root, parent_inode);
844 BUG_ON(ret);
845
Chris Mason0660b5a2008-11-17 20:37:39 -0500846 /* add the backref first */
847 ret = btrfs_add_root_ref(trans, parent_root->fs_info->tree_root,
848 pending->root_key.objectid,
849 BTRFS_ROOT_BACKREF_KEY,
850 parent_root->root_key.objectid,
851 parent_inode->i_ino, index, pending->name,
852 namelen);
853
854 BUG_ON(ret);
855
856 /* now add the forward ref */
857 ret = btrfs_add_root_ref(trans, parent_root->fs_info->tree_root,
858 parent_root->root_key.objectid,
859 BTRFS_ROOT_REF_KEY,
860 pending->root_key.objectid,
861 parent_inode->i_ino, index, pending->name,
862 namelen);
863
Chris Mason3de45862008-11-17 21:02:50 -0500864 inode = btrfs_lookup_dentry(parent_inode, pending->dentry);
865 d_instantiate(pending->dentry, inode);
Chris Mason3063d292008-01-08 15:46:30 -0500866fail:
Chris Mason3de45862008-11-17 21:02:50 -0500867 btrfs_end_transaction(trans, fs_info->fs_root);
Chris Mason3063d292008-01-08 15:46:30 -0500868 return ret;
869}
870
Chris Masond352ac62008-09-29 15:18:18 -0400871/*
872 * create all the snapshots we've scheduled for creation
873 */
Chris Mason80b67942008-02-01 16:35:04 -0500874static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
875 struct btrfs_fs_info *fs_info)
Chris Mason3063d292008-01-08 15:46:30 -0500876{
877 struct btrfs_pending_snapshot *pending;
878 struct list_head *head = &trans->transaction->pending_snapshots;
Chris Mason3de45862008-11-17 21:02:50 -0500879 int ret;
880
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500881 list_for_each_entry(pending, head, list) {
Chris Mason3de45862008-11-17 21:02:50 -0500882 ret = create_pending_snapshot(trans, fs_info, pending);
883 BUG_ON(ret);
884 }
885 return 0;
886}
887
888static noinline int finish_pending_snapshots(struct btrfs_trans_handle *trans,
889 struct btrfs_fs_info *fs_info)
890{
891 struct btrfs_pending_snapshot *pending;
892 struct list_head *head = &trans->transaction->pending_snapshots;
Chris Mason3063d292008-01-08 15:46:30 -0500893 int ret;
894
Chris Masond3977122009-01-05 21:25:51 -0500895 while (!list_empty(head)) {
Chris Mason3063d292008-01-08 15:46:30 -0500896 pending = list_entry(head->next,
897 struct btrfs_pending_snapshot, list);
Chris Mason3de45862008-11-17 21:02:50 -0500898 ret = finish_pending_snapshot(fs_info, pending);
Chris Mason3063d292008-01-08 15:46:30 -0500899 BUG_ON(ret);
900 list_del(&pending->list);
901 kfree(pending->name);
902 kfree(pending);
903 }
Chris Masondc17ff82008-01-08 15:46:30 -0500904 return 0;
905}
906
Chris Mason79154b12007-03-22 15:59:16 -0400907int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
908 struct btrfs_root *root)
909{
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400910 unsigned long joined = 0;
911 unsigned long timeout = 1;
Chris Mason79154b12007-03-22 15:59:16 -0400912 struct btrfs_transaction *cur_trans;
Chris Mason8fd17792007-04-19 21:01:03 -0400913 struct btrfs_transaction *prev_trans = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -0400914 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400915 struct list_head dirty_fs_roots;
Chris Masond1310b22008-01-24 16:13:08 -0500916 struct extent_io_tree *pinned_copy;
Chris Mason79154b12007-03-22 15:59:16 -0400917 DEFINE_WAIT(wait);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400918 int ret;
Chris Mason79154b12007-03-22 15:59:16 -0400919
Chris Mason56bec292009-03-13 10:10:06 -0400920 /* make a pass through all the delayed refs we have so far
921 * any runnings procs may add more while we are here
922 */
923 ret = btrfs_run_delayed_refs(trans, root, 0);
924 BUG_ON(ret);
925
926 /*
927 * set the flushing flag so procs in this transaction have to
928 * start sending their work down.
929 */
930 trans->transaction->delayed_refs.flushing = 1;
931
932 ret = btrfs_run_delayed_refs(trans, root, (u64)-1);
933 BUG_ON(ret);
934
Chris Mason0f7d52f2007-04-09 10:42:37 -0400935 INIT_LIST_HEAD(&dirty_fs_roots);
Chris Mason79154b12007-03-22 15:59:16 -0400936 mutex_lock(&root->fs_info->trans_mutex);
937 if (trans->transaction->in_commit) {
938 cur_trans = trans->transaction;
939 trans->transaction->use_count++;
Chris Masonccd467d2007-06-28 15:57:36 -0400940 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400941 btrfs_end_transaction(trans, root);
Chris Masonccd467d2007-06-28 15:57:36 -0400942
Chris Mason79154b12007-03-22 15:59:16 -0400943 ret = wait_for_commit(root, cur_trans);
944 BUG_ON(ret);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400945
946 mutex_lock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400947 put_transaction(cur_trans);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400948 mutex_unlock(&root->fs_info->trans_mutex);
949
Chris Mason79154b12007-03-22 15:59:16 -0400950 return 0;
951 }
Chris Mason4313b392008-01-03 09:08:48 -0500952
953 pinned_copy = kmalloc(sizeof(*pinned_copy), GFP_NOFS);
954 if (!pinned_copy)
955 return -ENOMEM;
956
Chris Masond1310b22008-01-24 16:13:08 -0500957 extent_io_tree_init(pinned_copy,
Chris Mason4313b392008-01-03 09:08:48 -0500958 root->fs_info->btree_inode->i_mapping, GFP_NOFS);
959
Chris Mason2c90e5d2007-04-02 10:50:19 -0400960 trans->transaction->in_commit = 1;
Chris Masonf9295742008-07-17 12:54:14 -0400961 trans->transaction->blocked = 1;
Chris Masonccd467d2007-06-28 15:57:36 -0400962 cur_trans = trans->transaction;
963 if (cur_trans->list.prev != &root->fs_info->trans_list) {
964 prev_trans = list_entry(cur_trans->list.prev,
965 struct btrfs_transaction, list);
966 if (!prev_trans->commit_done) {
967 prev_trans->use_count++;
Chris Masonccd467d2007-06-28 15:57:36 -0400968 mutex_unlock(&root->fs_info->trans_mutex);
969
970 wait_for_commit(root, prev_trans);
Chris Masonccd467d2007-06-28 15:57:36 -0400971
Chris Masonccd467d2007-06-28 15:57:36 -0400972 mutex_lock(&root->fs_info->trans_mutex);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400973 put_transaction(prev_trans);
Chris Masonccd467d2007-06-28 15:57:36 -0400974 }
975 }
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400976
977 do {
Yan Zheng7ea394f2008-08-05 13:05:02 -0400978 int snap_pending = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400979 joined = cur_trans->num_joined;
Yan Zheng7ea394f2008-08-05 13:05:02 -0400980 if (!list_empty(&trans->transaction->pending_snapshots))
981 snap_pending = 1;
982
Chris Mason2c90e5d2007-04-02 10:50:19 -0400983 WARN_ON(cur_trans != trans->transaction);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400984 prepare_to_wait(&cur_trans->writer_wait, &wait,
Chris Mason79154b12007-03-22 15:59:16 -0400985 TASK_UNINTERRUPTIBLE);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400986
987 if (cur_trans->num_writers > 1)
988 timeout = MAX_SCHEDULE_TIMEOUT;
989 else
990 timeout = 1;
991
Chris Mason79154b12007-03-22 15:59:16 -0400992 mutex_unlock(&root->fs_info->trans_mutex);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400993
Yan Zheng7ea394f2008-08-05 13:05:02 -0400994 if (snap_pending) {
995 ret = btrfs_wait_ordered_extents(root, 1);
996 BUG_ON(ret);
997 }
998
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400999 schedule_timeout(timeout);
1000
Chris Mason79154b12007-03-22 15:59:16 -04001001 mutex_lock(&root->fs_info->trans_mutex);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001002 finish_wait(&cur_trans->writer_wait, &wait);
1003 } while (cur_trans->num_writers > 1 ||
1004 (cur_trans->num_joined != joined));
1005
Chris Mason3063d292008-01-08 15:46:30 -05001006 ret = create_pending_snapshots(trans, root->fs_info);
1007 BUG_ON(ret);
1008
Chris Mason56bec292009-03-13 10:10:06 -04001009 ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
1010 BUG_ON(ret);
1011
Chris Mason2c90e5d2007-04-02 10:50:19 -04001012 WARN_ON(cur_trans != trans->transaction);
Chris Masondc17ff82008-01-08 15:46:30 -05001013
Chris Masone02119d2008-09-05 16:13:11 -04001014 /* btrfs_commit_tree_roots is responsible for getting the
1015 * various roots consistent with each other. Every pointer
1016 * in the tree of tree roots has to point to the most up to date
1017 * root for every subvolume and other tree. So, we have to keep
1018 * the tree logging code from jumping in and changing any
1019 * of the trees.
1020 *
1021 * At this point in the commit, there can't be any tree-log
1022 * writers, but a little lower down we drop the trans mutex
1023 * and let new people in. By holding the tree_log_mutex
1024 * from now until after the super is written, we avoid races
1025 * with the tree-log code.
1026 */
1027 mutex_lock(&root->fs_info->tree_log_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04001028 /*
1029 * keep tree reloc code from adding new reloc trees
1030 */
1031 mutex_lock(&root->fs_info->tree_reloc_mutex);
1032
Chris Masone02119d2008-09-05 16:13:11 -04001033
Chris Mason54aa1f42007-06-22 14:16:25 -04001034 ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix,
1035 &dirty_fs_roots);
1036 BUG_ON(ret);
1037
Chris Masone02119d2008-09-05 16:13:11 -04001038 /* add_dirty_roots gets rid of all the tree log roots, it is now
1039 * safe to free the root of tree log roots
1040 */
1041 btrfs_free_log_root_tree(trans, root->fs_info);
1042
Chris Mason79154b12007-03-22 15:59:16 -04001043 ret = btrfs_commit_tree_roots(trans, root);
1044 BUG_ON(ret);
Chris Mason54aa1f42007-06-22 14:16:25 -04001045
Chris Mason78fae272007-03-25 11:35:08 -04001046 cur_trans = root->fs_info->running_transaction;
Chris Masoncee36a02008-01-15 08:40:48 -05001047 spin_lock(&root->fs_info->new_trans_lock);
Chris Mason78fae272007-03-25 11:35:08 -04001048 root->fs_info->running_transaction = NULL;
Chris Masoncee36a02008-01-15 08:40:48 -05001049 spin_unlock(&root->fs_info->new_trans_lock);
Chris Mason4b52dff2007-06-26 10:06:50 -04001050 btrfs_set_super_generation(&root->fs_info->super_copy,
1051 cur_trans->transid);
1052 btrfs_set_super_root(&root->fs_info->super_copy,
Chris Masondb945352007-10-15 16:15:53 -04001053 root->fs_info->tree_root->node->start);
1054 btrfs_set_super_root_level(&root->fs_info->super_copy,
1055 btrfs_header_level(root->fs_info->tree_root->node));
Chris Mason5f39d392007-10-15 16:14:19 -04001056
Chris Mason0b86a832008-03-24 15:01:56 -04001057 btrfs_set_super_chunk_root(&root->fs_info->super_copy,
1058 chunk_root->node->start);
1059 btrfs_set_super_chunk_root_level(&root->fs_info->super_copy,
1060 btrfs_header_level(chunk_root->node));
Yan Zheng84234f32008-10-29 14:49:05 -04001061 btrfs_set_super_chunk_root_generation(&root->fs_info->super_copy,
1062 btrfs_header_generation(chunk_root->node));
Chris Masone02119d2008-09-05 16:13:11 -04001063
1064 if (!root->fs_info->log_root_recovering) {
1065 btrfs_set_super_log_root(&root->fs_info->super_copy, 0);
1066 btrfs_set_super_log_root_level(&root->fs_info->super_copy, 0);
1067 }
1068
Chris Masona061fc82008-05-07 11:43:44 -04001069 memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy,
1070 sizeof(root->fs_info->super_copy));
Chris Masonccd467d2007-06-28 15:57:36 -04001071
Chris Mason4313b392008-01-03 09:08:48 -05001072 btrfs_copy_pinned(root, pinned_copy);
Chris Masonccd467d2007-06-28 15:57:36 -04001073
Chris Masonf9295742008-07-17 12:54:14 -04001074 trans->transaction->blocked = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001075 wake_up(&root->fs_info->transaction_throttle);
Chris Masonf9295742008-07-17 12:54:14 -04001076 wake_up(&root->fs_info->transaction_wait);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001077
Chris Mason78fae272007-03-25 11:35:08 -04001078 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -04001079 ret = btrfs_write_and_wait_transaction(trans, root);
1080 BUG_ON(ret);
Yan Zhenga512bbf2008-12-08 16:46:26 -05001081 write_ctree_super(trans, root, 0);
Chris Mason4313b392008-01-03 09:08:48 -05001082
Chris Masone02119d2008-09-05 16:13:11 -04001083 /*
1084 * the super is written, we can safely allow the tree-loggers
1085 * to go about their business
1086 */
1087 mutex_unlock(&root->fs_info->tree_log_mutex);
1088
Chris Mason4313b392008-01-03 09:08:48 -05001089 btrfs_finish_extent_commit(trans, root, pinned_copy);
Chris Mason4313b392008-01-03 09:08:48 -05001090 kfree(pinned_copy);
1091
Zheng Yan1a40e232008-09-26 10:09:34 -04001092 btrfs_drop_dead_reloc_roots(root);
1093 mutex_unlock(&root->fs_info->tree_reloc_mutex);
1094
Chris Mason3de45862008-11-17 21:02:50 -05001095 /* do the directory inserts of any pending snapshot creations */
1096 finish_pending_snapshots(trans, root->fs_info);
1097
Zheng Yan1a40e232008-09-26 10:09:34 -04001098 mutex_lock(&root->fs_info->trans_mutex);
1099
Chris Mason2c90e5d2007-04-02 10:50:19 -04001100 cur_trans->commit_done = 1;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001101 root->fs_info->last_trans_committed = cur_trans->transid;
Chris Mason2c90e5d2007-04-02 10:50:19 -04001102 wake_up(&cur_trans->commit_wait);
Chris Mason3de45862008-11-17 21:02:50 -05001103
Chris Mason79154b12007-03-22 15:59:16 -04001104 put_transaction(cur_trans);
Chris Mason78fae272007-03-25 11:35:08 -04001105 put_transaction(cur_trans);
Josef Bacik58176a92007-08-29 15:47:34 -04001106
Yanbcc63ab2008-07-30 16:29:20 -04001107 list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots);
Chris Masonfacda1e2007-06-08 18:11:48 -04001108 if (root->fs_info->closing)
1109 list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots);
Josef Bacik58176a92007-08-29 15:47:34 -04001110
Chris Mason78fae272007-03-25 11:35:08 -04001111 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason3de45862008-11-17 21:02:50 -05001112
Chris Mason2c90e5d2007-04-02 10:50:19 -04001113 kmem_cache_free(btrfs_trans_handle_cachep, trans);
Chris Mason79154b12007-03-22 15:59:16 -04001114
Chris Masond3977122009-01-05 21:25:51 -05001115 if (root->fs_info->closing)
Chris Masonfacda1e2007-06-08 18:11:48 -04001116 drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);
Chris Mason79154b12007-03-22 15:59:16 -04001117 return ret;
1118}
1119
Chris Masond352ac62008-09-29 15:18:18 -04001120/*
1121 * interface function to delete all the snapshots we have scheduled for deletion
1122 */
Chris Masone9d0b132007-08-10 14:06:19 -04001123int btrfs_clean_old_snapshots(struct btrfs_root *root)
1124{
1125 struct list_head dirty_roots;
1126 INIT_LIST_HEAD(&dirty_roots);
Chris Masona74a4b92008-06-25 16:01:31 -04001127again:
Chris Masone9d0b132007-08-10 14:06:19 -04001128 mutex_lock(&root->fs_info->trans_mutex);
1129 list_splice_init(&root->fs_info->dead_roots, &dirty_roots);
1130 mutex_unlock(&root->fs_info->trans_mutex);
1131
1132 if (!list_empty(&dirty_roots)) {
1133 drop_dirty_roots(root, &dirty_roots);
Chris Masona74a4b92008-06-25 16:01:31 -04001134 goto again;
Chris Masone9d0b132007-08-10 14:06:19 -04001135 }
1136 return 0;
1137}