blob: e9c8ebeedd7ee876da479a73e42d28e64e22ff5d [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Mason79154b12007-03-22 15:59:16 -040019#include <linux/fs.h>
Chris Mason34088782007-06-12 11:36:58 -040020#include <linux/sched.h>
Chris Masond3c2fdcf2007-09-17 10:58:06 -040021#include <linux/writeback.h>
Chris Mason5f39d392007-10-15 16:14:19 -040022#include <linux/pagemap.h>
Chris Mason5f2cc082008-11-07 18:22:45 -050023#include <linux/blkdev.h>
Chris Mason79154b12007-03-22 15:59:16 -040024#include "ctree.h"
25#include "disk-io.h"
26#include "transaction.h"
Chris Mason925baed2008-06-25 16:01:30 -040027#include "locking.h"
Yan Zheng31153d82008-07-28 15:32:19 -040028#include "ref-cache.h"
Chris Masone02119d2008-09-05 16:13:11 -040029#include "tree-log.h"
Chris Mason79154b12007-03-22 15:59:16 -040030
Chris Mason78fae272007-03-25 11:35:08 -040031static int total_trans = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -040032extern struct kmem_cache *btrfs_trans_handle_cachep;
33extern struct kmem_cache *btrfs_transaction_cachep;
34
Chris Mason0f7d52f2007-04-09 10:42:37 -040035#define BTRFS_ROOT_TRANS_TAG 0
36
Chris Mason80b67942008-02-01 16:35:04 -050037static noinline void put_transaction(struct btrfs_transaction *transaction)
Chris Mason79154b12007-03-22 15:59:16 -040038{
Chris Mason2c90e5d2007-04-02 10:50:19 -040039 WARN_ON(transaction->use_count == 0);
Chris Mason79154b12007-03-22 15:59:16 -040040 transaction->use_count--;
Chris Mason78fae272007-03-25 11:35:08 -040041 if (transaction->use_count == 0) {
42 WARN_ON(total_trans == 0);
43 total_trans--;
Chris Mason8fd17792007-04-19 21:01:03 -040044 list_del_init(&transaction->list);
Chris Mason2c90e5d2007-04-02 10:50:19 -040045 memset(transaction, 0, sizeof(*transaction));
46 kmem_cache_free(btrfs_transaction_cachep, transaction);
Chris Mason78fae272007-03-25 11:35:08 -040047 }
Chris Mason79154b12007-03-22 15:59:16 -040048}
49
Chris Masond352ac62008-09-29 15:18:18 -040050/*
51 * either allocate a new transaction or hop into the existing one
52 */
Chris Mason80b67942008-02-01 16:35:04 -050053static noinline int join_transaction(struct btrfs_root *root)
Chris Mason79154b12007-03-22 15:59:16 -040054{
55 struct btrfs_transaction *cur_trans;
56 cur_trans = root->fs_info->running_transaction;
57 if (!cur_trans) {
Chris Mason2c90e5d2007-04-02 10:50:19 -040058 cur_trans = kmem_cache_alloc(btrfs_transaction_cachep,
59 GFP_NOFS);
Chris Mason78fae272007-03-25 11:35:08 -040060 total_trans++;
Chris Mason79154b12007-03-22 15:59:16 -040061 BUG_ON(!cur_trans);
Chris Mason0f7d52f2007-04-09 10:42:37 -040062 root->fs_info->generation++;
Chris Masone18e4802008-01-18 10:54:22 -050063 root->fs_info->last_alloc = 0;
Chris Mason4529ba42008-01-31 16:45:07 -050064 root->fs_info->last_data_alloc = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -040065 cur_trans->num_writers = 1;
66 cur_trans->num_joined = 0;
Chris Mason0f7d52f2007-04-09 10:42:37 -040067 cur_trans->transid = root->fs_info->generation;
Chris Mason79154b12007-03-22 15:59:16 -040068 init_waitqueue_head(&cur_trans->writer_wait);
69 init_waitqueue_head(&cur_trans->commit_wait);
70 cur_trans->in_commit = 0;
Chris Masonf9295742008-07-17 12:54:14 -040071 cur_trans->blocked = 0;
Chris Masond5719762007-03-23 10:01:08 -040072 cur_trans->use_count = 1;
Chris Mason79154b12007-03-22 15:59:16 -040073 cur_trans->commit_done = 0;
Chris Mason08607c12007-06-08 15:33:54 -040074 cur_trans->start_time = get_seconds();
Chris Mason3063d292008-01-08 15:46:30 -050075 INIT_LIST_HEAD(&cur_trans->pending_snapshots);
Chris Mason8fd17792007-04-19 21:01:03 -040076 list_add_tail(&cur_trans->list, &root->fs_info->trans_list);
Chris Masond1310b22008-01-24 16:13:08 -050077 extent_io_tree_init(&cur_trans->dirty_pages,
Chris Mason5f39d392007-10-15 16:14:19 -040078 root->fs_info->btree_inode->i_mapping,
79 GFP_NOFS);
Chris Mason48ec2cf2008-06-09 09:35:50 -040080 spin_lock(&root->fs_info->new_trans_lock);
81 root->fs_info->running_transaction = cur_trans;
82 spin_unlock(&root->fs_info->new_trans_lock);
Josef Bacik15ee9bc2007-08-10 16:22:09 -040083 } else {
84 cur_trans->num_writers++;
85 cur_trans->num_joined++;
Chris Mason79154b12007-03-22 15:59:16 -040086 }
Josef Bacik15ee9bc2007-08-10 16:22:09 -040087
Chris Mason79154b12007-03-22 15:59:16 -040088 return 0;
89}
90
Chris Masond352ac62008-09-29 15:18:18 -040091/*
92 * this does all the record keeping required to make sure that a
93 * reference counted root is properly recorded in a given transaction.
94 * This is required to make sure the old root from before we joined the transaction
95 * is deleted when the transaction commits
96 */
Chris Masone02119d2008-09-05 16:13:11 -040097noinline int btrfs_record_root_in_trans(struct btrfs_root *root)
Chris Mason6702ed42007-08-07 16:15:09 -040098{
Yan Zhengf321e492008-07-30 09:26:11 -040099 struct btrfs_dirty_root *dirty;
Chris Mason6702ed42007-08-07 16:15:09 -0400100 u64 running_trans_id = root->fs_info->running_transaction->transid;
101 if (root->ref_cows && root->last_trans < running_trans_id) {
102 WARN_ON(root == root->fs_info->extent_root);
103 if (root->root_item.refs != 0) {
104 radix_tree_tag_set(&root->fs_info->fs_roots_radix,
105 (unsigned long)root->root_key.objectid,
106 BTRFS_ROOT_TRANS_TAG);
Yan Zheng31153d82008-07-28 15:32:19 -0400107
108 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
109 BUG_ON(!dirty);
110 dirty->root = kmalloc(sizeof(*dirty->root), GFP_NOFS);
111 BUG_ON(!dirty->root);
Yan Zheng31153d82008-07-28 15:32:19 -0400112 dirty->latest_root = root;
113 INIT_LIST_HEAD(&dirty->list);
Yan Zheng31153d82008-07-28 15:32:19 -0400114
Chris Mason925baed2008-06-25 16:01:30 -0400115 root->commit_root = btrfs_root_node(root);
Yan Zheng31153d82008-07-28 15:32:19 -0400116
117 memcpy(dirty->root, root, sizeof(*root));
118 spin_lock_init(&dirty->root->node_lock);
Yanbcc63ab2008-07-30 16:29:20 -0400119 spin_lock_init(&dirty->root->list_lock);
Yan Zheng31153d82008-07-28 15:32:19 -0400120 mutex_init(&dirty->root->objectid_mutex);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400121 mutex_init(&dirty->root->log_mutex);
Yanbcc63ab2008-07-30 16:29:20 -0400122 INIT_LIST_HEAD(&dirty->root->dead_list);
Yan Zheng31153d82008-07-28 15:32:19 -0400123 dirty->root->node = root->commit_root;
124 dirty->root->commit_root = NULL;
Yanbcc63ab2008-07-30 16:29:20 -0400125
126 spin_lock(&root->list_lock);
127 list_add(&dirty->root->dead_list, &root->dead_list);
128 spin_unlock(&root->list_lock);
129
130 root->dirty_root = dirty;
Chris Mason6702ed42007-08-07 16:15:09 -0400131 } else {
132 WARN_ON(1);
133 }
134 root->last_trans = running_trans_id;
135 }
136 return 0;
137}
138
Chris Masond352ac62008-09-29 15:18:18 -0400139/* wait for commit against the current transaction to become unblocked
140 * when this is done, it is safe to start a new transaction, but the current
141 * transaction might not be fully on disk.
142 */
Chris Mason37d1aee2008-07-31 10:48:37 -0400143static void wait_current_trans(struct btrfs_root *root)
Chris Mason79154b12007-03-22 15:59:16 -0400144{
Chris Masonf9295742008-07-17 12:54:14 -0400145 struct btrfs_transaction *cur_trans;
Chris Mason79154b12007-03-22 15:59:16 -0400146
Chris Masonf9295742008-07-17 12:54:14 -0400147 cur_trans = root->fs_info->running_transaction;
Chris Mason37d1aee2008-07-31 10:48:37 -0400148 if (cur_trans && cur_trans->blocked) {
Chris Masonf9295742008-07-17 12:54:14 -0400149 DEFINE_WAIT(wait);
150 cur_trans->use_count++;
151 while(1) {
152 prepare_to_wait(&root->fs_info->transaction_wait, &wait,
153 TASK_UNINTERRUPTIBLE);
154 if (cur_trans->blocked) {
155 mutex_unlock(&root->fs_info->trans_mutex);
156 schedule();
157 mutex_lock(&root->fs_info->trans_mutex);
158 finish_wait(&root->fs_info->transaction_wait,
159 &wait);
160 } else {
161 finish_wait(&root->fs_info->transaction_wait,
162 &wait);
163 break;
164 }
165 }
166 put_transaction(cur_trans);
167 }
Chris Mason37d1aee2008-07-31 10:48:37 -0400168}
169
Chris Masone02119d2008-09-05 16:13:11 -0400170static struct btrfs_trans_handle *start_transaction(struct btrfs_root *root,
Sage Weil9ca9ee02008-08-04 10:41:27 -0400171 int num_blocks, int wait)
Chris Mason37d1aee2008-07-31 10:48:37 -0400172{
173 struct btrfs_trans_handle *h =
174 kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
175 int ret;
176
177 mutex_lock(&root->fs_info->trans_mutex);
Chris Mason4bef0842008-09-08 11:18:08 -0400178 if (!root->fs_info->log_root_recovering &&
179 ((wait == 1 && !root->fs_info->open_ioctl_trans) || wait == 2))
Chris Mason37d1aee2008-07-31 10:48:37 -0400180 wait_current_trans(root);
Chris Mason79154b12007-03-22 15:59:16 -0400181 ret = join_transaction(root);
182 BUG_ON(ret);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400183
Chris Masone02119d2008-09-05 16:13:11 -0400184 btrfs_record_root_in_trans(root);
Chris Mason6702ed42007-08-07 16:15:09 -0400185 h->transid = root->fs_info->running_transaction->transid;
Chris Mason79154b12007-03-22 15:59:16 -0400186 h->transaction = root->fs_info->running_transaction;
187 h->blocks_reserved = num_blocks;
188 h->blocks_used = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400189 h->block_group = NULL;
Chris Mason26b80032007-08-08 20:17:12 -0400190 h->alloc_exclude_nr = 0;
191 h->alloc_exclude_start = 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);
220 while(!commit->commit_done) {
221 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/*
235 * rate limit against the drop_snapshot code. This helps to slow down new operations
236 * if the drop_snapshot code isn't able to keep up.
237 */
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 Masonab78c842008-07-29 16:15:18 -0400291 mutex_lock(&info->trans_mutex);
292 cur_trans = info->running_transaction;
Chris Masonccd467d2007-06-28 15:57:36 -0400293 WARN_ON(cur_trans != trans->transaction);
Chris Masond5719762007-03-23 10:01:08 -0400294 WARN_ON(cur_trans->num_writers < 1);
Chris Masonccd467d2007-06-28 15:57:36 -0400295 cur_trans->num_writers--;
Chris Mason89ce8a62008-06-25 16:01:31 -0400296
Chris Mason79154b12007-03-22 15:59:16 -0400297 if (waitqueue_active(&cur_trans->writer_wait))
298 wake_up(&cur_trans->writer_wait);
Chris Mason79154b12007-03-22 15:59:16 -0400299 put_transaction(cur_trans);
Chris Masonab78c842008-07-29 16:15:18 -0400300 mutex_unlock(&info->trans_mutex);
Chris Masond6025572007-03-30 14:27:56 -0400301 memset(trans, 0, sizeof(*trans));
Chris Mason2c90e5d2007-04-02 10:50:19 -0400302 kmem_cache_free(btrfs_trans_handle_cachep, trans);
Chris Masonab78c842008-07-29 16:15:18 -0400303
304 if (throttle)
Chris Mason37d1aee2008-07-31 10:48:37 -0400305 throttle_on_drops(root);
Chris Masonab78c842008-07-29 16:15:18 -0400306
Chris Mason79154b12007-03-22 15:59:16 -0400307 return 0;
308}
309
Chris Mason89ce8a62008-06-25 16:01:31 -0400310int btrfs_end_transaction(struct btrfs_trans_handle *trans,
311 struct btrfs_root *root)
312{
313 return __btrfs_end_transaction(trans, root, 0);
314}
315
316int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
317 struct btrfs_root *root)
318{
319 return __btrfs_end_transaction(trans, root, 1);
320}
321
Chris Masond352ac62008-09-29 15:18:18 -0400322/*
323 * when btree blocks are allocated, they have some corresponding bits set for
324 * them in one of two extent_io trees. This is used to make sure all of
325 * those extents are on disk for transaction or log commit
326 */
Chris Masond0c803c2008-09-11 16:17:57 -0400327int btrfs_write_and_wait_marked_extents(struct btrfs_root *root,
328 struct extent_io_tree *dirty_pages)
Chris Mason79154b12007-03-22 15:59:16 -0400329{
Chris Mason7c4452b2007-04-28 09:29:35 -0400330 int ret;
Chris Mason777e6bd2008-08-15 15:34:15 -0400331 int err = 0;
Chris Mason7c4452b2007-04-28 09:29:35 -0400332 int werr = 0;
333 struct page *page;
Chris Mason7c4452b2007-04-28 09:29:35 -0400334 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Mason5f2cc082008-11-07 18:22:45 -0500335 struct extent_io_tree *io_tree = &BTRFS_I(btree_inode)->io_tree;
Chris Mason777e6bd2008-08-15 15:34:15 -0400336 u64 start = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400337 u64 end;
338 unsigned long index;
Chris Mason7c4452b2007-04-28 09:29:35 -0400339
Chris Mason7c4452b2007-04-28 09:29:35 -0400340 while(1) {
Chris Mason777e6bd2008-08-15 15:34:15 -0400341 ret = find_first_extent_bit(dirty_pages, start, &start, &end,
Chris Mason5f39d392007-10-15 16:14:19 -0400342 EXTENT_DIRTY);
343 if (ret)
Chris Mason7c4452b2007-04-28 09:29:35 -0400344 break;
Chris Mason5f39d392007-10-15 16:14:19 -0400345 while(start <= end) {
Chris Mason777e6bd2008-08-15 15:34:15 -0400346 cond_resched();
347
Chris Mason5f39d392007-10-15 16:14:19 -0400348 index = start >> PAGE_CACHE_SHIFT;
Chris Mason35ebb932007-10-30 16:56:53 -0400349 start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
Chris Mason4bef0842008-09-08 11:18:08 -0400350 page = find_get_page(btree_inode->i_mapping, index);
Chris Mason7c4452b2007-04-28 09:29:35 -0400351 if (!page)
352 continue;
Chris Mason4bef0842008-09-08 11:18:08 -0400353
354 btree_lock_page_hook(page);
355 if (!page->mapping) {
356 unlock_page(page);
357 page_cache_release(page);
358 continue;
359 }
360
Chris Mason6702ed42007-08-07 16:15:09 -0400361 if (PageWriteback(page)) {
362 if (PageDirty(page))
363 wait_on_page_writeback(page);
364 else {
365 unlock_page(page);
366 page_cache_release(page);
367 continue;
368 }
369 }
Chris Mason7c4452b2007-04-28 09:29:35 -0400370 err = write_one_page(page, 0);
371 if (err)
372 werr = err;
373 page_cache_release(page);
374 }
375 }
Chris Mason5f2cc082008-11-07 18:22:45 -0500376 /*
377 * we unplug once and then use the wait_on_extent_bit for
378 * everything else
379 */
380 blk_run_address_space(btree_inode->i_mapping);
Chris Mason777e6bd2008-08-15 15:34:15 -0400381 while(1) {
382 ret = find_first_extent_bit(dirty_pages, 0, &start, &end,
383 EXTENT_DIRTY);
384 if (ret)
385 break;
386
387 clear_extent_dirty(dirty_pages, start, end, GFP_NOFS);
388 while(start <= end) {
389 index = start >> PAGE_CACHE_SHIFT;
390 start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
391 page = find_get_page(btree_inode->i_mapping, index);
392 if (!page)
393 continue;
394 if (PageDirty(page)) {
Chris Mason4bef0842008-09-08 11:18:08 -0400395 btree_lock_page_hook(page);
396 wait_on_page_writeback(page);
Chris Mason777e6bd2008-08-15 15:34:15 -0400397 err = write_one_page(page, 0);
398 if (err)
399 werr = err;
400 }
Chris Mason5f2cc082008-11-07 18:22:45 -0500401 if (PageWriteback(page)) {
402 /*
403 * we don't wait on the page writeback bit
404 * because that triggers a lot of unplugs.
405 * The extent bits are much nicer to
406 * the disks, but come with a slightly
407 * higher latency because we aren't forcing
408 * unplugs.
409 */
410 wait_on_extent_writeback(io_tree,
411 page_offset(page),
412 page_offset(page) +
413 PAGE_CACHE_SIZE - 1);
414 }
415 if (PageWriteback(page)) {
416 /*
417 * the state bits get cleared before the
418 * page bits, lets add some extra
419 * paranoia here
420 */
421 wait_on_page_writeback(page);
422 }
Chris Mason777e6bd2008-08-15 15:34:15 -0400423 page_cache_release(page);
424 cond_resched();
425 }
426 }
Chris Mason7c4452b2007-04-28 09:29:35 -0400427 if (err)
428 werr = err;
429 return werr;
Chris Mason79154b12007-03-22 15:59:16 -0400430}
431
Chris Masond0c803c2008-09-11 16:17:57 -0400432int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
433 struct btrfs_root *root)
434{
435 if (!trans || !trans->transaction) {
436 struct inode *btree_inode;
437 btree_inode = root->fs_info->btree_inode;
438 return filemap_write_and_wait(btree_inode->i_mapping);
439 }
440 return btrfs_write_and_wait_marked_extents(root,
441 &trans->transaction->dirty_pages);
442}
443
Chris Masond352ac62008-09-29 15:18:18 -0400444/*
445 * this is used to update the root pointer in the tree of tree roots.
446 *
447 * But, in the case of the extent allocation tree, updating the root
448 * pointer may allocate blocks which may change the root of the extent
449 * allocation tree.
450 *
451 * So, this loops and repeats and makes sure the cowonly root didn't
452 * change while the root pointer was being updated in the metadata.
453 */
Chris Mason0b86a832008-03-24 15:01:56 -0400454static int update_cowonly_root(struct btrfs_trans_handle *trans,
455 struct btrfs_root *root)
456{
457 int ret;
458 u64 old_root_bytenr;
459 struct btrfs_root *tree_root = root->fs_info->tree_root;
460
Chris Mason87ef2bb2008-10-30 11:23:27 -0400461 btrfs_extent_post_op(trans, root);
Chris Mason0b86a832008-03-24 15:01:56 -0400462 btrfs_write_dirty_block_groups(trans, root);
Chris Mason87ef2bb2008-10-30 11:23:27 -0400463 btrfs_extent_post_op(trans, root);
464
Chris Mason0b86a832008-03-24 15:01:56 -0400465 while(1) {
466 old_root_bytenr = btrfs_root_bytenr(&root->root_item);
467 if (old_root_bytenr == root->node->start)
468 break;
469 btrfs_set_root_bytenr(&root->root_item,
470 root->node->start);
471 btrfs_set_root_level(&root->root_item,
472 btrfs_header_level(root->node));
Yan Zheng84234f32008-10-29 14:49:05 -0400473 btrfs_set_root_generation(&root->root_item, trans->transid);
Chris Mason87ef2bb2008-10-30 11:23:27 -0400474
475 btrfs_extent_post_op(trans, root);
476
Chris Mason0b86a832008-03-24 15:01:56 -0400477 ret = btrfs_update_root(trans, tree_root,
478 &root->root_key,
479 &root->root_item);
480 BUG_ON(ret);
481 btrfs_write_dirty_block_groups(trans, root);
Chris Mason87ef2bb2008-10-30 11:23:27 -0400482 btrfs_extent_post_op(trans, root);
Chris Mason0b86a832008-03-24 15:01:56 -0400483 }
484 return 0;
485}
486
Chris Masond352ac62008-09-29 15:18:18 -0400487/*
488 * update all the cowonly tree roots on disk
489 */
Chris Mason79154b12007-03-22 15:59:16 -0400490int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans,
491 struct btrfs_root *root)
492{
Chris Mason79154b12007-03-22 15:59:16 -0400493 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason0b86a832008-03-24 15:01:56 -0400494 struct list_head *next;
Yan Zheng84234f32008-10-29 14:49:05 -0400495 struct extent_buffer *eb;
496
Chris Mason87ef2bb2008-10-30 11:23:27 -0400497 btrfs_extent_post_op(trans, fs_info->tree_root);
498
Yan Zheng84234f32008-10-29 14:49:05 -0400499 eb = btrfs_lock_root_node(fs_info->tree_root);
500 btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 0, &eb, 0);
501 btrfs_tree_unlock(eb);
502 free_extent_buffer(eb);
Chris Mason79154b12007-03-22 15:59:16 -0400503
Chris Mason87ef2bb2008-10-30 11:23:27 -0400504 btrfs_extent_post_op(trans, fs_info->tree_root);
505
Chris Mason0b86a832008-03-24 15:01:56 -0400506 while(!list_empty(&fs_info->dirty_cowonly_roots)) {
507 next = fs_info->dirty_cowonly_roots.next;
508 list_del_init(next);
509 root = list_entry(next, struct btrfs_root, dirty_list);
Chris Mason87ef2bb2008-10-30 11:23:27 -0400510
Chris Mason0b86a832008-03-24 15:01:56 -0400511 update_cowonly_root(trans, root);
Chris Mason79154b12007-03-22 15:59:16 -0400512 }
513 return 0;
514}
515
Chris Masond352ac62008-09-29 15:18:18 -0400516/*
517 * dead roots are old snapshots that need to be deleted. This allocates
518 * a dirty root struct and adds it into the list of dead roots that need to
519 * be deleted
520 */
Yan Zhengb48652c2008-08-04 23:23:47 -0400521int btrfs_add_dead_root(struct btrfs_root *root, struct btrfs_root *latest)
Chris Mason5eda7b52007-06-22 14:16:25 -0400522{
Yan Zhengf321e492008-07-30 09:26:11 -0400523 struct btrfs_dirty_root *dirty;
Chris Mason5eda7b52007-06-22 14:16:25 -0400524
525 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
526 if (!dirty)
527 return -ENOMEM;
Chris Mason5eda7b52007-06-22 14:16:25 -0400528 dirty->root = root;
Chris Mason5ce14bb2007-09-11 11:15:39 -0400529 dirty->latest_root = latest;
Yan Zhengb48652c2008-08-04 23:23:47 -0400530
531 mutex_lock(&root->fs_info->trans_mutex);
532 list_add(&dirty->list, &latest->fs_info->dead_roots);
533 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason5eda7b52007-06-22 14:16:25 -0400534 return 0;
535}
536
Chris Masond352ac62008-09-29 15:18:18 -0400537/*
538 * at transaction commit time we need to schedule the old roots for
539 * deletion via btrfs_drop_snapshot. This runs through all the
540 * reference counted roots that were modified in the current
541 * transaction and puts them into the drop list
542 */
Chris Mason80b67942008-02-01 16:35:04 -0500543static noinline int add_dirty_roots(struct btrfs_trans_handle *trans,
544 struct radix_tree_root *radix,
545 struct list_head *list)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400546{
Yan Zhengf321e492008-07-30 09:26:11 -0400547 struct btrfs_dirty_root *dirty;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400548 struct btrfs_root *gang[8];
549 struct btrfs_root *root;
550 int i;
551 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400552 int err = 0;
Chris Mason5eda7b52007-06-22 14:16:25 -0400553 u32 refs;
Chris Mason54aa1f42007-06-22 14:16:25 -0400554
Chris Mason0f7d52f2007-04-09 10:42:37 -0400555 while(1) {
556 ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0,
557 ARRAY_SIZE(gang),
558 BTRFS_ROOT_TRANS_TAG);
559 if (ret == 0)
560 break;
561 for (i = 0; i < ret; i++) {
562 root = gang[i];
Chris Mason2619ba12007-04-10 16:58:11 -0400563 radix_tree_tag_clear(radix,
564 (unsigned long)root->root_key.objectid,
565 BTRFS_ROOT_TRANS_TAG);
Yan Zheng31153d82008-07-28 15:32:19 -0400566
567 BUG_ON(!root->ref_tree);
Chris Mason017e5362008-07-28 15:32:51 -0400568 dirty = root->dirty_root;
Yan Zheng31153d82008-07-28 15:32:19 -0400569
Chris Masone02119d2008-09-05 16:13:11 -0400570 btrfs_free_log(trans, root);
Yan Zhengf82d02d2008-10-29 14:49:05 -0400571 btrfs_free_reloc_root(trans, root);
Chris Masone02119d2008-09-05 16:13:11 -0400572
Chris Mason0f7d52f2007-04-09 10:42:37 -0400573 if (root->commit_root == root->node) {
Chris Masondb945352007-10-15 16:15:53 -0400574 WARN_ON(root->node->start !=
575 btrfs_root_bytenr(&root->root_item));
Yan Zheng31153d82008-07-28 15:32:19 -0400576
Chris Mason5f39d392007-10-15 16:14:19 -0400577 free_extent_buffer(root->commit_root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400578 root->commit_root = NULL;
Yan Zheng7ea394f2008-08-05 13:05:02 -0400579 root->dirty_root = NULL;
Yanbcc63ab2008-07-30 16:29:20 -0400580
581 spin_lock(&root->list_lock);
582 list_del_init(&dirty->root->dead_list);
583 spin_unlock(&root->list_lock);
584
Yan Zheng31153d82008-07-28 15:32:19 -0400585 kfree(dirty->root);
586 kfree(dirty);
Josef Bacik58176a92007-08-29 15:47:34 -0400587
588 /* make sure to update the root on disk
589 * so we get any updates to the block used
590 * counts
591 */
592 err = btrfs_update_root(trans,
593 root->fs_info->tree_root,
594 &root->root_key,
595 &root->root_item);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400596 continue;
597 }
Chris Mason9f3a7422007-08-07 15:52:19 -0400598
599 memset(&root->root_item.drop_progress, 0,
600 sizeof(struct btrfs_disk_key));
601 root->root_item.drop_level = 0;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400602 root->commit_root = NULL;
Yan Zheng7ea394f2008-08-05 13:05:02 -0400603 root->dirty_root = NULL;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400604 root->root_key.offset = root->fs_info->generation;
Chris Masondb945352007-10-15 16:15:53 -0400605 btrfs_set_root_bytenr(&root->root_item,
606 root->node->start);
607 btrfs_set_root_level(&root->root_item,
608 btrfs_header_level(root->node));
Yan Zheng84234f32008-10-29 14:49:05 -0400609 btrfs_set_root_generation(&root->root_item,
610 root->root_key.offset);
611
Chris Mason0f7d52f2007-04-09 10:42:37 -0400612 err = btrfs_insert_root(trans, root->fs_info->tree_root,
613 &root->root_key,
614 &root->root_item);
Chris Mason54aa1f42007-06-22 14:16:25 -0400615 if (err)
616 break;
Chris Mason9f3a7422007-08-07 15:52:19 -0400617
618 refs = btrfs_root_refs(&dirty->root->root_item);
619 btrfs_set_root_refs(&dirty->root->root_item, refs - 1);
Chris Mason5eda7b52007-06-22 14:16:25 -0400620 err = btrfs_update_root(trans, root->fs_info->tree_root,
Chris Mason9f3a7422007-08-07 15:52:19 -0400621 &dirty->root->root_key,
622 &dirty->root->root_item);
Chris Mason5eda7b52007-06-22 14:16:25 -0400623
624 BUG_ON(err);
Chris Mason9f3a7422007-08-07 15:52:19 -0400625 if (refs == 1) {
Chris Mason5eda7b52007-06-22 14:16:25 -0400626 list_add(&dirty->list, list);
Chris Mason9f3a7422007-08-07 15:52:19 -0400627 } else {
628 WARN_ON(1);
Yan Zheng31153d82008-07-28 15:32:19 -0400629 free_extent_buffer(dirty->root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -0400630 kfree(dirty->root);
Chris Mason5eda7b52007-06-22 14:16:25 -0400631 kfree(dirty);
Chris Mason9f3a7422007-08-07 15:52:19 -0400632 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400633 }
634 }
Chris Mason54aa1f42007-06-22 14:16:25 -0400635 return err;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400636}
637
Chris Masond352ac62008-09-29 15:18:18 -0400638/*
639 * defrag a given btree. If cacheonly == 1, this won't read from the disk,
640 * otherwise every leaf in the btree is read and defragged.
641 */
Chris Masone9d0b132007-08-10 14:06:19 -0400642int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
643{
644 struct btrfs_fs_info *info = root->fs_info;
645 int ret;
646 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400647 unsigned long nr;
Chris Masone9d0b132007-08-10 14:06:19 -0400648
Chris Masona2135012008-06-25 16:01:30 -0400649 smp_mb();
Chris Masone9d0b132007-08-10 14:06:19 -0400650 if (root->defrag_running)
651 return 0;
Chris Masone9d0b132007-08-10 14:06:19 -0400652 trans = btrfs_start_transaction(root, 1);
Chris Mason6b800532007-10-15 16:17:34 -0400653 while (1) {
Chris Masone9d0b132007-08-10 14:06:19 -0400654 root->defrag_running = 1;
655 ret = btrfs_defrag_leaves(trans, root, cacheonly);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400656 nr = trans->blocks_used;
Chris Masone9d0b132007-08-10 14:06:19 -0400657 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400658 btrfs_btree_balance_dirty(info->tree_root, nr);
Chris Masone9d0b132007-08-10 14:06:19 -0400659 cond_resched();
660
Chris Masone9d0b132007-08-10 14:06:19 -0400661 trans = btrfs_start_transaction(root, 1);
Chris Mason3f157a22008-06-25 16:01:31 -0400662 if (root->fs_info->closing || ret != -EAGAIN)
Chris Masone9d0b132007-08-10 14:06:19 -0400663 break;
664 }
665 root->defrag_running = 0;
Chris Masona2135012008-06-25 16:01:30 -0400666 smp_mb();
Chris Masone9d0b132007-08-10 14:06:19 -0400667 btrfs_end_transaction(trans, root);
668 return 0;
669}
670
Chris Masond352ac62008-09-29 15:18:18 -0400671/*
672 * Given a list of roots that need to be deleted, call btrfs_drop_snapshot on
673 * all of them
674 */
Chris Mason80b67942008-02-01 16:35:04 -0500675static noinline int drop_dirty_roots(struct btrfs_root *tree_root,
676 struct list_head *list)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400677{
Yan Zhengf321e492008-07-30 09:26:11 -0400678 struct btrfs_dirty_root *dirty;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400679 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400680 unsigned long nr;
Chris Masondb945352007-10-15 16:15:53 -0400681 u64 num_bytes;
682 u64 bytes_used;
Yanbcc63ab2008-07-30 16:29:20 -0400683 u64 max_useless;
Chris Mason54aa1f42007-06-22 14:16:25 -0400684 int ret = 0;
Chris Mason9f3a7422007-08-07 15:52:19 -0400685 int err;
686
Chris Mason0f7d52f2007-04-09 10:42:37 -0400687 while(!list_empty(list)) {
Josef Bacik58176a92007-08-29 15:47:34 -0400688 struct btrfs_root *root;
689
Yan Zhengf321e492008-07-30 09:26:11 -0400690 dirty = list_entry(list->prev, struct btrfs_dirty_root, list);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400691 list_del_init(&dirty->list);
Chris Mason5eda7b52007-06-22 14:16:25 -0400692
Chris Masondb945352007-10-15 16:15:53 -0400693 num_bytes = btrfs_root_used(&dirty->root->root_item);
Josef Bacik58176a92007-08-29 15:47:34 -0400694 root = dirty->latest_root;
Chris Masona2135012008-06-25 16:01:30 -0400695 atomic_inc(&root->fs_info->throttles);
Josef Bacik58176a92007-08-29 15:47:34 -0400696
Chris Mason9f3a7422007-08-07 15:52:19 -0400697 while(1) {
698 trans = btrfs_start_transaction(tree_root, 1);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400699 mutex_lock(&root->fs_info->drop_mutex);
Chris Mason9f3a7422007-08-07 15:52:19 -0400700 ret = btrfs_drop_snapshot(trans, dirty->root);
701 if (ret != -EAGAIN) {
702 break;
703 }
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400704 mutex_unlock(&root->fs_info->drop_mutex);
Josef Bacik58176a92007-08-29 15:47:34 -0400705
Chris Mason9f3a7422007-08-07 15:52:19 -0400706 err = btrfs_update_root(trans,
707 tree_root,
708 &dirty->root->root_key,
709 &dirty->root->root_item);
710 if (err)
711 ret = err;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400712 nr = trans->blocks_used;
Chris Mason017e5362008-07-28 15:32:51 -0400713 ret = btrfs_end_transaction(trans, tree_root);
Chris Mason9f3a7422007-08-07 15:52:19 -0400714 BUG_ON(ret);
Chris Masona2135012008-06-25 16:01:30 -0400715
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400716 btrfs_btree_balance_dirty(tree_root, nr);
Chris Mason4dc119042007-10-15 16:18:14 -0400717 cond_resched();
Chris Mason9f3a7422007-08-07 15:52:19 -0400718 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400719 BUG_ON(ret);
Chris Masona2135012008-06-25 16:01:30 -0400720 atomic_dec(&root->fs_info->throttles);
Chris Mason017e5362008-07-28 15:32:51 -0400721 wake_up(&root->fs_info->transaction_throttle);
Josef Bacik58176a92007-08-29 15:47:34 -0400722
Chris Masondb945352007-10-15 16:15:53 -0400723 num_bytes -= btrfs_root_used(&dirty->root->root_item);
724 bytes_used = btrfs_root_used(&root->root_item);
725 if (num_bytes) {
Chris Masone02119d2008-09-05 16:13:11 -0400726 btrfs_record_root_in_trans(root);
Chris Mason5f39d392007-10-15 16:14:19 -0400727 btrfs_set_root_used(&root->root_item,
Chris Masondb945352007-10-15 16:15:53 -0400728 bytes_used - num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -0400729 }
Chris Masona2135012008-06-25 16:01:30 -0400730
Chris Mason9f3a7422007-08-07 15:52:19 -0400731 ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key);
Josef Bacik58176a92007-08-29 15:47:34 -0400732 if (ret) {
733 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -0400734 break;
Josef Bacik58176a92007-08-29 15:47:34 -0400735 }
Chris Masona2135012008-06-25 16:01:30 -0400736 mutex_unlock(&root->fs_info->drop_mutex);
737
Yanbcc63ab2008-07-30 16:29:20 -0400738 spin_lock(&root->list_lock);
739 list_del_init(&dirty->root->dead_list);
740 if (!list_empty(&root->dead_list)) {
741 struct btrfs_root *oldest;
742 oldest = list_entry(root->dead_list.prev,
743 struct btrfs_root, dead_list);
744 max_useless = oldest->root_key.offset - 1;
745 } else {
746 max_useless = root->root_key.offset - 1;
747 }
748 spin_unlock(&root->list_lock);
749
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400750 nr = trans->blocks_used;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400751 ret = btrfs_end_transaction(trans, tree_root);
752 BUG_ON(ret);
Chris Mason5eda7b52007-06-22 14:16:25 -0400753
Zheng Yane4657682008-09-26 10:04:53 -0400754 ret = btrfs_remove_leaf_refs(root, max_useless, 0);
Yanbcc63ab2008-07-30 16:29:20 -0400755 BUG_ON(ret);
756
Chris Masonf510cfe2007-10-15 16:14:48 -0400757 free_extent_buffer(dirty->root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -0400758 kfree(dirty->root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400759 kfree(dirty);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400760
761 btrfs_btree_balance_dirty(tree_root, nr);
Chris Mason4dc119042007-10-15 16:18:14 -0400762 cond_resched();
Chris Mason0f7d52f2007-04-09 10:42:37 -0400763 }
Chris Mason54aa1f42007-06-22 14:16:25 -0400764 return ret;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400765}
766
Chris Masond352ac62008-09-29 15:18:18 -0400767/*
768 * new snapshots need to be created at a very specific time in the
769 * transaction commit. This does the actual creation
770 */
Chris Mason80b67942008-02-01 16:35:04 -0500771static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
Chris Mason3063d292008-01-08 15:46:30 -0500772 struct btrfs_fs_info *fs_info,
773 struct btrfs_pending_snapshot *pending)
774{
775 struct btrfs_key key;
Chris Mason80b67942008-02-01 16:35:04 -0500776 struct btrfs_root_item *new_root_item;
Chris Mason3063d292008-01-08 15:46:30 -0500777 struct btrfs_root *tree_root = fs_info->tree_root;
778 struct btrfs_root *root = pending->root;
779 struct extent_buffer *tmp;
Chris Mason925baed2008-06-25 16:01:30 -0400780 struct extent_buffer *old;
Chris Mason3063d292008-01-08 15:46:30 -0500781 int ret;
782 u64 objectid;
783
Chris Mason80b67942008-02-01 16:35:04 -0500784 new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
785 if (!new_root_item) {
786 ret = -ENOMEM;
787 goto fail;
788 }
Chris Mason3063d292008-01-08 15:46:30 -0500789 ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid);
790 if (ret)
791 goto fail;
792
Yan Zheng80ff3852008-10-30 14:20:02 -0400793 btrfs_record_root_in_trans(root);
794 btrfs_set_root_last_snapshot(&root->root_item, trans->transid);
Chris Mason80b67942008-02-01 16:35:04 -0500795 memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
Chris Mason3063d292008-01-08 15:46:30 -0500796
797 key.objectid = objectid;
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400798 key.offset = trans->transid;
Chris Mason3063d292008-01-08 15:46:30 -0500799 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
800
Chris Mason925baed2008-06-25 16:01:30 -0400801 old = btrfs_lock_root_node(root);
Chris Mason65b51a02008-08-01 15:11:20 -0400802 btrfs_cow_block(trans, root, old, NULL, 0, &old, 0);
Chris Mason3063d292008-01-08 15:46:30 -0500803
Chris Mason925baed2008-06-25 16:01:30 -0400804 btrfs_copy_root(trans, root, old, &tmp, objectid);
805 btrfs_tree_unlock(old);
806 free_extent_buffer(old);
Chris Mason3063d292008-01-08 15:46:30 -0500807
Chris Mason80b67942008-02-01 16:35:04 -0500808 btrfs_set_root_bytenr(new_root_item, tmp->start);
809 btrfs_set_root_level(new_root_item, btrfs_header_level(tmp));
Yan Zheng84234f32008-10-29 14:49:05 -0400810 btrfs_set_root_generation(new_root_item, trans->transid);
Chris Mason3063d292008-01-08 15:46:30 -0500811 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
Chris Mason80b67942008-02-01 16:35:04 -0500812 new_root_item);
Chris Mason925baed2008-06-25 16:01:30 -0400813 btrfs_tree_unlock(tmp);
Chris Mason3063d292008-01-08 15:46:30 -0500814 free_extent_buffer(tmp);
815 if (ret)
816 goto fail;
817
Chris Mason3de45862008-11-17 21:02:50 -0500818 key.offset = (u64)-1;
819 memcpy(&pending->root_key, &key, sizeof(key));
820fail:
821 kfree(new_root_item);
822 return ret;
823}
824
825static noinline int finish_pending_snapshot(struct btrfs_fs_info *fs_info,
826 struct btrfs_pending_snapshot *pending)
827{
828 int ret;
829 int namelen;
830 u64 index = 0;
831 struct btrfs_trans_handle *trans;
832 struct inode *parent_inode;
833 struct inode *inode;
Chris Mason0660b5a2008-11-17 20:37:39 -0500834 struct btrfs_root *parent_root;
Chris Mason3de45862008-11-17 21:02:50 -0500835
Chris Mason3394e162008-11-17 20:42:26 -0500836 parent_inode = pending->dentry->d_parent->d_inode;
Chris Mason0660b5a2008-11-17 20:37:39 -0500837 parent_root = BTRFS_I(parent_inode)->root;
838 trans = btrfs_start_transaction(parent_root, 1);
Chris Mason3de45862008-11-17 21:02:50 -0500839
Chris Mason3063d292008-01-08 15:46:30 -0500840 /*
841 * insert the directory item
842 */
Sven Wegener3b963622008-06-09 21:57:42 -0400843 namelen = strlen(pending->name);
Chris Mason3de45862008-11-17 21:02:50 -0500844 ret = btrfs_set_inode_index(parent_inode, &index);
Chris Mason0660b5a2008-11-17 20:37:39 -0500845 ret = btrfs_insert_dir_item(trans, parent_root,
Chris Mason3de45862008-11-17 21:02:50 -0500846 pending->name, namelen,
847 parent_inode->i_ino,
848 &pending->root_key, BTRFS_FT_DIR, index);
Chris Mason3063d292008-01-08 15:46:30 -0500849
850 if (ret)
851 goto fail;
Chris Mason0660b5a2008-11-17 20:37:39 -0500852
853 /* add the backref first */
854 ret = btrfs_add_root_ref(trans, parent_root->fs_info->tree_root,
855 pending->root_key.objectid,
856 BTRFS_ROOT_BACKREF_KEY,
857 parent_root->root_key.objectid,
858 parent_inode->i_ino, index, pending->name,
859 namelen);
860
861 BUG_ON(ret);
862
863 /* now add the forward ref */
864 ret = btrfs_add_root_ref(trans, parent_root->fs_info->tree_root,
865 parent_root->root_key.objectid,
866 BTRFS_ROOT_REF_KEY,
867 pending->root_key.objectid,
868 parent_inode->i_ino, index, pending->name,
869 namelen);
870
Chris Mason3de45862008-11-17 21:02:50 -0500871 inode = btrfs_lookup_dentry(parent_inode, pending->dentry);
872 d_instantiate(pending->dentry, inode);
Chris Mason3063d292008-01-08 15:46:30 -0500873fail:
Chris Mason3de45862008-11-17 21:02:50 -0500874 btrfs_end_transaction(trans, fs_info->fs_root);
Chris Mason3063d292008-01-08 15:46:30 -0500875 return ret;
876}
877
Chris Masond352ac62008-09-29 15:18:18 -0400878/*
879 * create all the snapshots we've scheduled for creation
880 */
Chris Mason80b67942008-02-01 16:35:04 -0500881static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
882 struct btrfs_fs_info *fs_info)
Chris Mason3063d292008-01-08 15:46:30 -0500883{
884 struct btrfs_pending_snapshot *pending;
885 struct list_head *head = &trans->transaction->pending_snapshots;
Chris Mason3de45862008-11-17 21:02:50 -0500886 struct list_head *cur;
887 int ret;
888
889 list_for_each(cur, head) {
890 pending = list_entry(cur, struct btrfs_pending_snapshot, list);
891 ret = create_pending_snapshot(trans, fs_info, pending);
892 BUG_ON(ret);
893 }
894 return 0;
895}
896
897static noinline int finish_pending_snapshots(struct btrfs_trans_handle *trans,
898 struct btrfs_fs_info *fs_info)
899{
900 struct btrfs_pending_snapshot *pending;
901 struct list_head *head = &trans->transaction->pending_snapshots;
Chris Mason3063d292008-01-08 15:46:30 -0500902 int ret;
903
904 while(!list_empty(head)) {
905 pending = list_entry(head->next,
906 struct btrfs_pending_snapshot, list);
Chris Mason3de45862008-11-17 21:02:50 -0500907 ret = finish_pending_snapshot(fs_info, pending);
Chris Mason3063d292008-01-08 15:46:30 -0500908 BUG_ON(ret);
909 list_del(&pending->list);
910 kfree(pending->name);
911 kfree(pending);
912 }
Chris Masondc17ff82008-01-08 15:46:30 -0500913 return 0;
914}
915
Chris Mason79154b12007-03-22 15:59:16 -0400916int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
917 struct btrfs_root *root)
918{
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400919 unsigned long joined = 0;
920 unsigned long timeout = 1;
Chris Mason79154b12007-03-22 15:59:16 -0400921 struct btrfs_transaction *cur_trans;
Chris Mason8fd17792007-04-19 21:01:03 -0400922 struct btrfs_transaction *prev_trans = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -0400923 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400924 struct list_head dirty_fs_roots;
Chris Masond1310b22008-01-24 16:13:08 -0500925 struct extent_io_tree *pinned_copy;
Chris Mason79154b12007-03-22 15:59:16 -0400926 DEFINE_WAIT(wait);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400927 int ret;
Chris Mason79154b12007-03-22 15:59:16 -0400928
Chris Mason0f7d52f2007-04-09 10:42:37 -0400929 INIT_LIST_HEAD(&dirty_fs_roots);
Chris Mason79154b12007-03-22 15:59:16 -0400930 mutex_lock(&root->fs_info->trans_mutex);
931 if (trans->transaction->in_commit) {
932 cur_trans = trans->transaction;
933 trans->transaction->use_count++;
Chris Masonccd467d2007-06-28 15:57:36 -0400934 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400935 btrfs_end_transaction(trans, root);
Chris Masonccd467d2007-06-28 15:57:36 -0400936
Chris Mason79154b12007-03-22 15:59:16 -0400937 ret = wait_for_commit(root, cur_trans);
938 BUG_ON(ret);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400939
940 mutex_lock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400941 put_transaction(cur_trans);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400942 mutex_unlock(&root->fs_info->trans_mutex);
943
Chris Mason79154b12007-03-22 15:59:16 -0400944 return 0;
945 }
Chris Mason4313b392008-01-03 09:08:48 -0500946
947 pinned_copy = kmalloc(sizeof(*pinned_copy), GFP_NOFS);
948 if (!pinned_copy)
949 return -ENOMEM;
950
Chris Masond1310b22008-01-24 16:13:08 -0500951 extent_io_tree_init(pinned_copy,
Chris Mason4313b392008-01-03 09:08:48 -0500952 root->fs_info->btree_inode->i_mapping, GFP_NOFS);
953
Chris Mason2c90e5d2007-04-02 10:50:19 -0400954 trans->transaction->in_commit = 1;
Chris Masonf9295742008-07-17 12:54:14 -0400955 trans->transaction->blocked = 1;
Chris Masonccd467d2007-06-28 15:57:36 -0400956 cur_trans = trans->transaction;
957 if (cur_trans->list.prev != &root->fs_info->trans_list) {
958 prev_trans = list_entry(cur_trans->list.prev,
959 struct btrfs_transaction, list);
960 if (!prev_trans->commit_done) {
961 prev_trans->use_count++;
Chris Masonccd467d2007-06-28 15:57:36 -0400962 mutex_unlock(&root->fs_info->trans_mutex);
963
964 wait_for_commit(root, prev_trans);
Chris Masonccd467d2007-06-28 15:57:36 -0400965
Chris Masonccd467d2007-06-28 15:57:36 -0400966 mutex_lock(&root->fs_info->trans_mutex);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400967 put_transaction(prev_trans);
Chris Masonccd467d2007-06-28 15:57:36 -0400968 }
969 }
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400970
971 do {
Yan Zheng7ea394f2008-08-05 13:05:02 -0400972 int snap_pending = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400973 joined = cur_trans->num_joined;
Yan Zheng7ea394f2008-08-05 13:05:02 -0400974 if (!list_empty(&trans->transaction->pending_snapshots))
975 snap_pending = 1;
976
Chris Mason2c90e5d2007-04-02 10:50:19 -0400977 WARN_ON(cur_trans != trans->transaction);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400978 prepare_to_wait(&cur_trans->writer_wait, &wait,
Chris Mason79154b12007-03-22 15:59:16 -0400979 TASK_UNINTERRUPTIBLE);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400980
981 if (cur_trans->num_writers > 1)
982 timeout = MAX_SCHEDULE_TIMEOUT;
983 else
984 timeout = 1;
985
Chris Mason79154b12007-03-22 15:59:16 -0400986 mutex_unlock(&root->fs_info->trans_mutex);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400987
Yan Zheng7ea394f2008-08-05 13:05:02 -0400988 if (snap_pending) {
989 ret = btrfs_wait_ordered_extents(root, 1);
990 BUG_ON(ret);
991 }
992
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400993 schedule_timeout(timeout);
994
Chris Mason79154b12007-03-22 15:59:16 -0400995 mutex_lock(&root->fs_info->trans_mutex);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400996 finish_wait(&cur_trans->writer_wait, &wait);
997 } while (cur_trans->num_writers > 1 ||
998 (cur_trans->num_joined != joined));
999
Chris Mason3063d292008-01-08 15:46:30 -05001000 ret = create_pending_snapshots(trans, root->fs_info);
1001 BUG_ON(ret);
1002
Chris Mason2c90e5d2007-04-02 10:50:19 -04001003 WARN_ON(cur_trans != trans->transaction);
Chris Masondc17ff82008-01-08 15:46:30 -05001004
Chris Masone02119d2008-09-05 16:13:11 -04001005 /* btrfs_commit_tree_roots is responsible for getting the
1006 * various roots consistent with each other. Every pointer
1007 * in the tree of tree roots has to point to the most up to date
1008 * root for every subvolume and other tree. So, we have to keep
1009 * the tree logging code from jumping in and changing any
1010 * of the trees.
1011 *
1012 * At this point in the commit, there can't be any tree-log
1013 * writers, but a little lower down we drop the trans mutex
1014 * and let new people in. By holding the tree_log_mutex
1015 * from now until after the super is written, we avoid races
1016 * with the tree-log code.
1017 */
1018 mutex_lock(&root->fs_info->tree_log_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04001019 /*
1020 * keep tree reloc code from adding new reloc trees
1021 */
1022 mutex_lock(&root->fs_info->tree_reloc_mutex);
1023
Chris Masone02119d2008-09-05 16:13:11 -04001024
Chris Mason54aa1f42007-06-22 14:16:25 -04001025 ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix,
1026 &dirty_fs_roots);
1027 BUG_ON(ret);
1028
Chris Masone02119d2008-09-05 16:13:11 -04001029 /* add_dirty_roots gets rid of all the tree log roots, it is now
1030 * safe to free the root of tree log roots
1031 */
1032 btrfs_free_log_root_tree(trans, root->fs_info);
1033
Chris Mason79154b12007-03-22 15:59:16 -04001034 ret = btrfs_commit_tree_roots(trans, root);
1035 BUG_ON(ret);
Chris Mason54aa1f42007-06-22 14:16:25 -04001036
Chris Mason78fae272007-03-25 11:35:08 -04001037 cur_trans = root->fs_info->running_transaction;
Chris Masoncee36a02008-01-15 08:40:48 -05001038 spin_lock(&root->fs_info->new_trans_lock);
Chris Mason78fae272007-03-25 11:35:08 -04001039 root->fs_info->running_transaction = NULL;
Chris Masoncee36a02008-01-15 08:40:48 -05001040 spin_unlock(&root->fs_info->new_trans_lock);
Chris Mason4b52dff2007-06-26 10:06:50 -04001041 btrfs_set_super_generation(&root->fs_info->super_copy,
1042 cur_trans->transid);
1043 btrfs_set_super_root(&root->fs_info->super_copy,
Chris Masondb945352007-10-15 16:15:53 -04001044 root->fs_info->tree_root->node->start);
1045 btrfs_set_super_root_level(&root->fs_info->super_copy,
1046 btrfs_header_level(root->fs_info->tree_root->node));
Chris Mason5f39d392007-10-15 16:14:19 -04001047
Chris Mason0b86a832008-03-24 15:01:56 -04001048 btrfs_set_super_chunk_root(&root->fs_info->super_copy,
1049 chunk_root->node->start);
1050 btrfs_set_super_chunk_root_level(&root->fs_info->super_copy,
1051 btrfs_header_level(chunk_root->node));
Yan Zheng84234f32008-10-29 14:49:05 -04001052 btrfs_set_super_chunk_root_generation(&root->fs_info->super_copy,
1053 btrfs_header_generation(chunk_root->node));
Chris Masone02119d2008-09-05 16:13:11 -04001054
1055 if (!root->fs_info->log_root_recovering) {
1056 btrfs_set_super_log_root(&root->fs_info->super_copy, 0);
1057 btrfs_set_super_log_root_level(&root->fs_info->super_copy, 0);
1058 }
1059
Chris Masona061fc82008-05-07 11:43:44 -04001060 memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy,
1061 sizeof(root->fs_info->super_copy));
Chris Masonccd467d2007-06-28 15:57:36 -04001062
Chris Mason4313b392008-01-03 09:08:48 -05001063 btrfs_copy_pinned(root, pinned_copy);
Chris Masonccd467d2007-06-28 15:57:36 -04001064
Chris Masonf9295742008-07-17 12:54:14 -04001065 trans->transaction->blocked = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001066 wake_up(&root->fs_info->transaction_throttle);
Chris Masonf9295742008-07-17 12:54:14 -04001067 wake_up(&root->fs_info->transaction_wait);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001068
Chris Mason78fae272007-03-25 11:35:08 -04001069 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -04001070 ret = btrfs_write_and_wait_transaction(trans, root);
1071 BUG_ON(ret);
Chris Mason79154b12007-03-22 15:59:16 -04001072 write_ctree_super(trans, root);
Chris Mason4313b392008-01-03 09:08:48 -05001073
Chris Masone02119d2008-09-05 16:13:11 -04001074 /*
1075 * the super is written, we can safely allow the tree-loggers
1076 * to go about their business
1077 */
1078 mutex_unlock(&root->fs_info->tree_log_mutex);
1079
Chris Mason4313b392008-01-03 09:08:48 -05001080 btrfs_finish_extent_commit(trans, root, pinned_copy);
Chris Mason4313b392008-01-03 09:08:48 -05001081 kfree(pinned_copy);
1082
Zheng Yan1a40e232008-09-26 10:09:34 -04001083 btrfs_drop_dead_reloc_roots(root);
1084 mutex_unlock(&root->fs_info->tree_reloc_mutex);
1085
Chris Mason3de45862008-11-17 21:02:50 -05001086 /* do the directory inserts of any pending snapshot creations */
1087 finish_pending_snapshots(trans, root->fs_info);
1088
Zheng Yan1a40e232008-09-26 10:09:34 -04001089 mutex_lock(&root->fs_info->trans_mutex);
1090
Chris Mason2c90e5d2007-04-02 10:50:19 -04001091 cur_trans->commit_done = 1;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001092 root->fs_info->last_trans_committed = cur_trans->transid;
Chris Mason2c90e5d2007-04-02 10:50:19 -04001093 wake_up(&cur_trans->commit_wait);
Chris Mason3de45862008-11-17 21:02:50 -05001094
Chris Mason79154b12007-03-22 15:59:16 -04001095 put_transaction(cur_trans);
Chris Mason78fae272007-03-25 11:35:08 -04001096 put_transaction(cur_trans);
Josef Bacik58176a92007-08-29 15:47:34 -04001097
Yanbcc63ab2008-07-30 16:29:20 -04001098 list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots);
Chris Masonfacda1e2007-06-08 18:11:48 -04001099 if (root->fs_info->closing)
1100 list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots);
Josef Bacik58176a92007-08-29 15:47:34 -04001101
Chris Mason78fae272007-03-25 11:35:08 -04001102 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason3de45862008-11-17 21:02:50 -05001103
Chris Mason2c90e5d2007-04-02 10:50:19 -04001104 kmem_cache_free(btrfs_trans_handle_cachep, trans);
Chris Mason79154b12007-03-22 15:59:16 -04001105
Chris Masonfacda1e2007-06-08 18:11:48 -04001106 if (root->fs_info->closing) {
Chris Masonfacda1e2007-06-08 18:11:48 -04001107 drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);
Chris Masonfacda1e2007-06-08 18:11:48 -04001108 }
Chris Mason79154b12007-03-22 15:59:16 -04001109 return ret;
1110}
1111
Chris Masond352ac62008-09-29 15:18:18 -04001112/*
1113 * interface function to delete all the snapshots we have scheduled for deletion
1114 */
Chris Masone9d0b132007-08-10 14:06:19 -04001115int btrfs_clean_old_snapshots(struct btrfs_root *root)
1116{
1117 struct list_head dirty_roots;
1118 INIT_LIST_HEAD(&dirty_roots);
Chris Masona74a4b92008-06-25 16:01:31 -04001119again:
Chris Masone9d0b132007-08-10 14:06:19 -04001120 mutex_lock(&root->fs_info->trans_mutex);
1121 list_splice_init(&root->fs_info->dead_roots, &dirty_roots);
1122 mutex_unlock(&root->fs_info->trans_mutex);
1123
1124 if (!list_empty(&dirty_roots)) {
1125 drop_dirty_roots(root, &dirty_roots);
Chris Masona74a4b92008-06-25 16:01:31 -04001126 goto again;
Chris Masone9d0b132007-08-10 14:06:19 -04001127 }
1128 return 0;
1129}