blob: 903edab3659a3ce46af880e611416205cf300e92 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Mason79154b12007-03-22 15:59:16 -040019#include <linux/fs.h>
Chris Mason34088782007-06-12 11:36:58 -040020#include <linux/sched.h>
Chris Masond3c2fdcf2007-09-17 10:58:06 -040021#include <linux/writeback.h>
Chris Mason5f39d392007-10-15 16:14:19 -040022#include <linux/pagemap.h>
Chris Mason5f2cc082008-11-07 18:22:45 -050023#include <linux/blkdev.h>
Chris Mason79154b12007-03-22 15:59:16 -040024#include "ctree.h"
25#include "disk-io.h"
26#include "transaction.h"
Chris Mason925baed2008-06-25 16:01:30 -040027#include "locking.h"
Yan Zheng31153d82008-07-28 15:32:19 -040028#include "ref-cache.h"
Chris Masone02119d2008-09-05 16:13:11 -040029#include "tree-log.h"
Chris Mason79154b12007-03-22 15:59:16 -040030
Chris Mason0f7d52f2007-04-09 10:42:37 -040031#define BTRFS_ROOT_TRANS_TAG 0
32
Chris Mason80b67942008-02-01 16:35:04 -050033static noinline void put_transaction(struct btrfs_transaction *transaction)
Chris Mason79154b12007-03-22 15:59:16 -040034{
Chris Mason2c90e5d2007-04-02 10:50:19 -040035 WARN_ON(transaction->use_count == 0);
Chris Mason79154b12007-03-22 15:59:16 -040036 transaction->use_count--;
Chris Mason78fae272007-03-25 11:35:08 -040037 if (transaction->use_count == 0) {
Chris Mason8fd17792007-04-19 21:01:03 -040038 list_del_init(&transaction->list);
Chris Mason2c90e5d2007-04-02 10:50:19 -040039 memset(transaction, 0, sizeof(*transaction));
40 kmem_cache_free(btrfs_transaction_cachep, transaction);
Chris Mason78fae272007-03-25 11:35:08 -040041 }
Chris Mason79154b12007-03-22 15:59:16 -040042}
43
Chris Masond352ac62008-09-29 15:18:18 -040044/*
45 * either allocate a new transaction or hop into the existing one
46 */
Chris Mason80b67942008-02-01 16:35:04 -050047static noinline int join_transaction(struct btrfs_root *root)
Chris Mason79154b12007-03-22 15:59:16 -040048{
49 struct btrfs_transaction *cur_trans;
50 cur_trans = root->fs_info->running_transaction;
51 if (!cur_trans) {
Chris Mason2c90e5d2007-04-02 10:50:19 -040052 cur_trans = kmem_cache_alloc(btrfs_transaction_cachep,
53 GFP_NOFS);
Chris Mason79154b12007-03-22 15:59:16 -040054 BUG_ON(!cur_trans);
Chris Mason0f7d52f2007-04-09 10:42:37 -040055 root->fs_info->generation++;
Chris Masone18e4802008-01-18 10:54:22 -050056 root->fs_info->last_alloc = 0;
Chris Mason4529ba42008-01-31 16:45:07 -050057 root->fs_info->last_data_alloc = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -040058 cur_trans->num_writers = 1;
59 cur_trans->num_joined = 0;
Chris Mason0f7d52f2007-04-09 10:42:37 -040060 cur_trans->transid = root->fs_info->generation;
Chris Mason79154b12007-03-22 15:59:16 -040061 init_waitqueue_head(&cur_trans->writer_wait);
62 init_waitqueue_head(&cur_trans->commit_wait);
63 cur_trans->in_commit = 0;
Chris Masonf9295742008-07-17 12:54:14 -040064 cur_trans->blocked = 0;
Chris Masond5719762007-03-23 10:01:08 -040065 cur_trans->use_count = 1;
Chris Mason79154b12007-03-22 15:59:16 -040066 cur_trans->commit_done = 0;
Chris Mason08607c12007-06-08 15:33:54 -040067 cur_trans->start_time = get_seconds();
Chris Mason56bec292009-03-13 10:10:06 -040068
69 cur_trans->delayed_refs.root.rb_node = NULL;
70 cur_trans->delayed_refs.num_entries = 0;
Chris Masonc3e69d52009-03-13 10:17:05 -040071 cur_trans->delayed_refs.num_heads_ready = 0;
72 cur_trans->delayed_refs.num_heads = 0;
Chris Mason56bec292009-03-13 10:10:06 -040073 cur_trans->delayed_refs.flushing = 0;
Chris Masonc3e69d52009-03-13 10:17:05 -040074 cur_trans->delayed_refs.run_delayed_start = 0;
Chris Mason56bec292009-03-13 10:10:06 -040075 spin_lock_init(&cur_trans->delayed_refs.lock);
76
Chris Mason3063d292008-01-08 15:46:30 -050077 INIT_LIST_HEAD(&cur_trans->pending_snapshots);
Chris Mason8fd17792007-04-19 21:01:03 -040078 list_add_tail(&cur_trans->list, &root->fs_info->trans_list);
Chris Masond1310b22008-01-24 16:13:08 -050079 extent_io_tree_init(&cur_trans->dirty_pages,
Chris Mason5f39d392007-10-15 16:14:19 -040080 root->fs_info->btree_inode->i_mapping,
81 GFP_NOFS);
Chris Mason48ec2cf2008-06-09 09:35:50 -040082 spin_lock(&root->fs_info->new_trans_lock);
83 root->fs_info->running_transaction = cur_trans;
84 spin_unlock(&root->fs_info->new_trans_lock);
Josef Bacik15ee9bc2007-08-10 16:22:09 -040085 } else {
86 cur_trans->num_writers++;
87 cur_trans->num_joined++;
Chris Mason79154b12007-03-22 15:59:16 -040088 }
Josef Bacik15ee9bc2007-08-10 16:22:09 -040089
Chris Mason79154b12007-03-22 15:59:16 -040090 return 0;
91}
92
Chris Masond352ac62008-09-29 15:18:18 -040093/*
Chris Masond3977122009-01-05 21:25:51 -050094 * this does all the record keeping required to make sure that a reference
95 * counted root is properly recorded in a given transaction. This is required
96 * to make sure the old root from before we joined the transaction is deleted
97 * when the transaction commits
Chris Masond352ac62008-09-29 15:18:18 -040098 */
Chris Masone02119d2008-09-05 16:13:11 -040099noinline int btrfs_record_root_in_trans(struct btrfs_root *root)
Chris Mason6702ed42007-08-07 16:15:09 -0400100{
Yan Zhengf321e492008-07-30 09:26:11 -0400101 struct btrfs_dirty_root *dirty;
Chris Mason6702ed42007-08-07 16:15:09 -0400102 u64 running_trans_id = root->fs_info->running_transaction->transid;
103 if (root->ref_cows && root->last_trans < running_trans_id) {
104 WARN_ON(root == root->fs_info->extent_root);
105 if (root->root_item.refs != 0) {
106 radix_tree_tag_set(&root->fs_info->fs_roots_radix,
107 (unsigned long)root->root_key.objectid,
108 BTRFS_ROOT_TRANS_TAG);
Yan Zheng31153d82008-07-28 15:32:19 -0400109
110 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
111 BUG_ON(!dirty);
112 dirty->root = kmalloc(sizeof(*dirty->root), GFP_NOFS);
113 BUG_ON(!dirty->root);
Yan Zheng31153d82008-07-28 15:32:19 -0400114 dirty->latest_root = root;
115 INIT_LIST_HEAD(&dirty->list);
Yan Zheng31153d82008-07-28 15:32:19 -0400116
Chris Mason925baed2008-06-25 16:01:30 -0400117 root->commit_root = btrfs_root_node(root);
Yan Zheng31153d82008-07-28 15:32:19 -0400118
119 memcpy(dirty->root, root, sizeof(*root));
120 spin_lock_init(&dirty->root->node_lock);
Yanbcc63ab2008-07-30 16:29:20 -0400121 spin_lock_init(&dirty->root->list_lock);
Yan Zheng31153d82008-07-28 15:32:19 -0400122 mutex_init(&dirty->root->objectid_mutex);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400123 mutex_init(&dirty->root->log_mutex);
Yanbcc63ab2008-07-30 16:29:20 -0400124 INIT_LIST_HEAD(&dirty->root->dead_list);
Yan Zheng31153d82008-07-28 15:32:19 -0400125 dirty->root->node = root->commit_root;
126 dirty->root->commit_root = NULL;
Yanbcc63ab2008-07-30 16:29:20 -0400127
128 spin_lock(&root->list_lock);
129 list_add(&dirty->root->dead_list, &root->dead_list);
130 spin_unlock(&root->list_lock);
131
132 root->dirty_root = dirty;
Chris Mason6702ed42007-08-07 16:15:09 -0400133 } else {
134 WARN_ON(1);
135 }
136 root->last_trans = running_trans_id;
137 }
138 return 0;
139}
140
Chris Masond352ac62008-09-29 15:18:18 -0400141/* wait for commit against the current transaction to become unblocked
142 * when this is done, it is safe to start a new transaction, but the current
143 * transaction might not be fully on disk.
144 */
Chris Mason37d1aee2008-07-31 10:48:37 -0400145static void wait_current_trans(struct btrfs_root *root)
Chris Mason79154b12007-03-22 15:59:16 -0400146{
Chris Masonf9295742008-07-17 12:54:14 -0400147 struct btrfs_transaction *cur_trans;
Chris Mason79154b12007-03-22 15:59:16 -0400148
Chris Masonf9295742008-07-17 12:54:14 -0400149 cur_trans = root->fs_info->running_transaction;
Chris Mason37d1aee2008-07-31 10:48:37 -0400150 if (cur_trans && cur_trans->blocked) {
Chris Masonf9295742008-07-17 12:54:14 -0400151 DEFINE_WAIT(wait);
152 cur_trans->use_count++;
Chris Masond3977122009-01-05 21:25:51 -0500153 while (1) {
Chris Masonf9295742008-07-17 12:54:14 -0400154 prepare_to_wait(&root->fs_info->transaction_wait, &wait,
155 TASK_UNINTERRUPTIBLE);
156 if (cur_trans->blocked) {
157 mutex_unlock(&root->fs_info->trans_mutex);
158 schedule();
159 mutex_lock(&root->fs_info->trans_mutex);
160 finish_wait(&root->fs_info->transaction_wait,
161 &wait);
162 } else {
163 finish_wait(&root->fs_info->transaction_wait,
164 &wait);
165 break;
166 }
167 }
168 put_transaction(cur_trans);
169 }
Chris Mason37d1aee2008-07-31 10:48:37 -0400170}
171
Chris Masone02119d2008-09-05 16:13:11 -0400172static struct btrfs_trans_handle *start_transaction(struct btrfs_root *root,
Sage Weil9ca9ee02008-08-04 10:41:27 -0400173 int num_blocks, int wait)
Chris Mason37d1aee2008-07-31 10:48:37 -0400174{
175 struct btrfs_trans_handle *h =
176 kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
177 int ret;
178
179 mutex_lock(&root->fs_info->trans_mutex);
Chris Mason4bef0842008-09-08 11:18:08 -0400180 if (!root->fs_info->log_root_recovering &&
181 ((wait == 1 && !root->fs_info->open_ioctl_trans) || wait == 2))
Chris Mason37d1aee2008-07-31 10:48:37 -0400182 wait_current_trans(root);
Chris Mason79154b12007-03-22 15:59:16 -0400183 ret = join_transaction(root);
184 BUG_ON(ret);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400185
Chris Masone02119d2008-09-05 16:13:11 -0400186 btrfs_record_root_in_trans(root);
Chris Mason6702ed42007-08-07 16:15:09 -0400187 h->transid = root->fs_info->running_transaction->transid;
Chris Mason79154b12007-03-22 15:59:16 -0400188 h->transaction = root->fs_info->running_transaction;
189 h->blocks_reserved = num_blocks;
190 h->blocks_used = 0;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500191 h->block_group = 0;
Chris Mason26b80032007-08-08 20:17:12 -0400192 h->alloc_exclude_nr = 0;
193 h->alloc_exclude_start = 0;
Chris Mason56bec292009-03-13 10:10:06 -0400194 h->delayed_ref_updates = 0;
Chris Mason79154b12007-03-22 15:59:16 -0400195 root->fs_info->running_transaction->use_count++;
196 mutex_unlock(&root->fs_info->trans_mutex);
197 return h;
198}
199
Chris Masonf9295742008-07-17 12:54:14 -0400200struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
201 int num_blocks)
202{
Sage Weil9ca9ee02008-08-04 10:41:27 -0400203 return start_transaction(root, num_blocks, 1);
Chris Masonf9295742008-07-17 12:54:14 -0400204}
205struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root,
206 int num_blocks)
207{
Sage Weil9ca9ee02008-08-04 10:41:27 -0400208 return start_transaction(root, num_blocks, 0);
Chris Masonf9295742008-07-17 12:54:14 -0400209}
210
Sage Weil9ca9ee02008-08-04 10:41:27 -0400211struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *r,
212 int num_blocks)
213{
214 return start_transaction(r, num_blocks, 2);
215}
216
Chris Masond352ac62008-09-29 15:18:18 -0400217/* wait for a transaction commit to be fully complete */
Chris Mason89ce8a62008-06-25 16:01:31 -0400218static noinline int wait_for_commit(struct btrfs_root *root,
219 struct btrfs_transaction *commit)
220{
221 DEFINE_WAIT(wait);
222 mutex_lock(&root->fs_info->trans_mutex);
Chris Masond3977122009-01-05 21:25:51 -0500223 while (!commit->commit_done) {
Chris Mason89ce8a62008-06-25 16:01:31 -0400224 prepare_to_wait(&commit->commit_wait, &wait,
225 TASK_UNINTERRUPTIBLE);
226 if (commit->commit_done)
227 break;
228 mutex_unlock(&root->fs_info->trans_mutex);
229 schedule();
230 mutex_lock(&root->fs_info->trans_mutex);
231 }
232 mutex_unlock(&root->fs_info->trans_mutex);
233 finish_wait(&commit->commit_wait, &wait);
234 return 0;
235}
236
Chris Masond352ac62008-09-29 15:18:18 -0400237/*
Chris Masond3977122009-01-05 21:25:51 -0500238 * rate limit against the drop_snapshot code. This helps to slow down new
239 * operations if the drop_snapshot code isn't able to keep up.
Chris Masond352ac62008-09-29 15:18:18 -0400240 */
Chris Mason37d1aee2008-07-31 10:48:37 -0400241static void throttle_on_drops(struct btrfs_root *root)
Chris Masonab78c842008-07-29 16:15:18 -0400242{
243 struct btrfs_fs_info *info = root->fs_info;
Chris Mason2dd3e672008-08-04 08:20:15 -0400244 int harder_count = 0;
Chris Masonab78c842008-07-29 16:15:18 -0400245
Chris Mason2dd3e672008-08-04 08:20:15 -0400246harder:
Chris Masonab78c842008-07-29 16:15:18 -0400247 if (atomic_read(&info->throttles)) {
248 DEFINE_WAIT(wait);
249 int thr;
Chris Masonab78c842008-07-29 16:15:18 -0400250 thr = atomic_read(&info->throttle_gen);
251
252 do {
253 prepare_to_wait(&info->transaction_throttle,
254 &wait, TASK_UNINTERRUPTIBLE);
255 if (!atomic_read(&info->throttles)) {
256 finish_wait(&info->transaction_throttle, &wait);
257 break;
258 }
259 schedule();
260 finish_wait(&info->transaction_throttle, &wait);
261 } while (thr == atomic_read(&info->throttle_gen));
Chris Mason2dd3e672008-08-04 08:20:15 -0400262 harder_count++;
263
264 if (root->fs_info->total_ref_cache_size > 1 * 1024 * 1024 &&
265 harder_count < 2)
266 goto harder;
267
268 if (root->fs_info->total_ref_cache_size > 5 * 1024 * 1024 &&
269 harder_count < 10)
270 goto harder;
271
272 if (root->fs_info->total_ref_cache_size > 10 * 1024 * 1024 &&
273 harder_count < 20)
274 goto harder;
Chris Masonab78c842008-07-29 16:15:18 -0400275 }
276}
277
Chris Mason37d1aee2008-07-31 10:48:37 -0400278void btrfs_throttle(struct btrfs_root *root)
279{
280 mutex_lock(&root->fs_info->trans_mutex);
Sage Weil9ca9ee02008-08-04 10:41:27 -0400281 if (!root->fs_info->open_ioctl_trans)
282 wait_current_trans(root);
Chris Mason37d1aee2008-07-31 10:48:37 -0400283 mutex_unlock(&root->fs_info->trans_mutex);
284
285 throttle_on_drops(root);
286}
287
Chris Mason89ce8a62008-06-25 16:01:31 -0400288static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
289 struct btrfs_root *root, int throttle)
Chris Mason79154b12007-03-22 15:59:16 -0400290{
291 struct btrfs_transaction *cur_trans;
Chris Masonab78c842008-07-29 16:15:18 -0400292 struct btrfs_fs_info *info = root->fs_info;
Chris Masonc3e69d52009-03-13 10:17:05 -0400293 int count = 0;
Chris Masond6e4a422007-04-06 15:37:36 -0400294
Chris Masonc3e69d52009-03-13 10:17:05 -0400295 while (count < 4) {
296 unsigned long cur = trans->delayed_ref_updates;
297 trans->delayed_ref_updates = 0;
298 if (cur &&
299 trans->transaction->delayed_refs.num_heads_ready > 64) {
300 trans->delayed_ref_updates = 0;
301 btrfs_run_delayed_refs(trans, root, cur);
302 } else {
303 break;
304 }
305 count++;
Chris Mason56bec292009-03-13 10:10:06 -0400306 }
307
Chris Masonab78c842008-07-29 16:15:18 -0400308 mutex_lock(&info->trans_mutex);
309 cur_trans = info->running_transaction;
Chris Masonccd467d2007-06-28 15:57:36 -0400310 WARN_ON(cur_trans != trans->transaction);
Chris Masond5719762007-03-23 10:01:08 -0400311 WARN_ON(cur_trans->num_writers < 1);
Chris Masonccd467d2007-06-28 15:57:36 -0400312 cur_trans->num_writers--;
Chris Mason89ce8a62008-06-25 16:01:31 -0400313
Chris Mason79154b12007-03-22 15:59:16 -0400314 if (waitqueue_active(&cur_trans->writer_wait))
315 wake_up(&cur_trans->writer_wait);
Chris Mason79154b12007-03-22 15:59:16 -0400316 put_transaction(cur_trans);
Chris Masonab78c842008-07-29 16:15:18 -0400317 mutex_unlock(&info->trans_mutex);
Chris Masond6025572007-03-30 14:27:56 -0400318 memset(trans, 0, sizeof(*trans));
Chris Mason2c90e5d2007-04-02 10:50:19 -0400319 kmem_cache_free(btrfs_trans_handle_cachep, trans);
Chris Masonab78c842008-07-29 16:15:18 -0400320
321 if (throttle)
Chris Mason37d1aee2008-07-31 10:48:37 -0400322 throttle_on_drops(root);
Chris Masonab78c842008-07-29 16:15:18 -0400323
Chris Mason79154b12007-03-22 15:59:16 -0400324 return 0;
325}
326
Chris Mason89ce8a62008-06-25 16:01:31 -0400327int btrfs_end_transaction(struct btrfs_trans_handle *trans,
328 struct btrfs_root *root)
329{
330 return __btrfs_end_transaction(trans, root, 0);
331}
332
333int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
334 struct btrfs_root *root)
335{
336 return __btrfs_end_transaction(trans, root, 1);
337}
338
Chris Masond352ac62008-09-29 15:18:18 -0400339/*
340 * when btree blocks are allocated, they have some corresponding bits set for
341 * them in one of two extent_io trees. This is used to make sure all of
342 * those extents are on disk for transaction or log commit
343 */
Chris Masond0c803c2008-09-11 16:17:57 -0400344int btrfs_write_and_wait_marked_extents(struct btrfs_root *root,
345 struct extent_io_tree *dirty_pages)
Chris Mason79154b12007-03-22 15:59:16 -0400346{
Chris Mason7c4452b2007-04-28 09:29:35 -0400347 int ret;
Chris Mason777e6bd2008-08-15 15:34:15 -0400348 int err = 0;
Chris Mason7c4452b2007-04-28 09:29:35 -0400349 int werr = 0;
350 struct page *page;
Chris Mason7c4452b2007-04-28 09:29:35 -0400351 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Mason777e6bd2008-08-15 15:34:15 -0400352 u64 start = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400353 u64 end;
354 unsigned long index;
Chris Mason7c4452b2007-04-28 09:29:35 -0400355
Chris Masond3977122009-01-05 21:25:51 -0500356 while (1) {
Chris Mason777e6bd2008-08-15 15:34:15 -0400357 ret = find_first_extent_bit(dirty_pages, start, &start, &end,
Chris Mason5f39d392007-10-15 16:14:19 -0400358 EXTENT_DIRTY);
359 if (ret)
Chris Mason7c4452b2007-04-28 09:29:35 -0400360 break;
Chris Masond3977122009-01-05 21:25:51 -0500361 while (start <= end) {
Chris Mason777e6bd2008-08-15 15:34:15 -0400362 cond_resched();
363
Chris Mason5f39d392007-10-15 16:14:19 -0400364 index = start >> PAGE_CACHE_SHIFT;
Chris Mason35ebb932007-10-30 16:56:53 -0400365 start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
Chris Mason4bef0842008-09-08 11:18:08 -0400366 page = find_get_page(btree_inode->i_mapping, index);
Chris Mason7c4452b2007-04-28 09:29:35 -0400367 if (!page)
368 continue;
Chris Mason4bef0842008-09-08 11:18:08 -0400369
370 btree_lock_page_hook(page);
371 if (!page->mapping) {
372 unlock_page(page);
373 page_cache_release(page);
374 continue;
375 }
376
Chris Mason6702ed42007-08-07 16:15:09 -0400377 if (PageWriteback(page)) {
378 if (PageDirty(page))
379 wait_on_page_writeback(page);
380 else {
381 unlock_page(page);
382 page_cache_release(page);
383 continue;
384 }
385 }
Chris Mason7c4452b2007-04-28 09:29:35 -0400386 err = write_one_page(page, 0);
387 if (err)
388 werr = err;
389 page_cache_release(page);
390 }
391 }
Chris Masond3977122009-01-05 21:25:51 -0500392 while (1) {
Chris Mason777e6bd2008-08-15 15:34:15 -0400393 ret = find_first_extent_bit(dirty_pages, 0, &start, &end,
394 EXTENT_DIRTY);
395 if (ret)
396 break;
397
398 clear_extent_dirty(dirty_pages, start, end, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -0500399 while (start <= end) {
Chris Mason777e6bd2008-08-15 15:34:15 -0400400 index = start >> PAGE_CACHE_SHIFT;
401 start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
402 page = find_get_page(btree_inode->i_mapping, index);
403 if (!page)
404 continue;
405 if (PageDirty(page)) {
Chris Mason4bef0842008-09-08 11:18:08 -0400406 btree_lock_page_hook(page);
407 wait_on_page_writeback(page);
Chris Mason777e6bd2008-08-15 15:34:15 -0400408 err = write_one_page(page, 0);
409 if (err)
410 werr = err;
411 }
Chris Mason105d9312008-11-18 12:13:12 -0500412 wait_on_page_writeback(page);
Chris Mason777e6bd2008-08-15 15:34:15 -0400413 page_cache_release(page);
414 cond_resched();
415 }
416 }
Chris Mason7c4452b2007-04-28 09:29:35 -0400417 if (err)
418 werr = err;
419 return werr;
Chris Mason79154b12007-03-22 15:59:16 -0400420}
421
Chris Masond0c803c2008-09-11 16:17:57 -0400422int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
423 struct btrfs_root *root)
424{
425 if (!trans || !trans->transaction) {
426 struct inode *btree_inode;
427 btree_inode = root->fs_info->btree_inode;
428 return filemap_write_and_wait(btree_inode->i_mapping);
429 }
430 return btrfs_write_and_wait_marked_extents(root,
431 &trans->transaction->dirty_pages);
432}
433
Chris Masond352ac62008-09-29 15:18:18 -0400434/*
435 * this is used to update the root pointer in the tree of tree roots.
436 *
437 * But, in the case of the extent allocation tree, updating the root
438 * pointer may allocate blocks which may change the root of the extent
439 * allocation tree.
440 *
441 * So, this loops and repeats and makes sure the cowonly root didn't
442 * change while the root pointer was being updated in the metadata.
443 */
Chris Mason0b86a832008-03-24 15:01:56 -0400444static int update_cowonly_root(struct btrfs_trans_handle *trans,
445 struct btrfs_root *root)
446{
447 int ret;
448 u64 old_root_bytenr;
449 struct btrfs_root *tree_root = root->fs_info->tree_root;
450
451 btrfs_write_dirty_block_groups(trans, root);
Chris Mason56bec292009-03-13 10:10:06 -0400452
453 ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
454 BUG_ON(ret);
Chris Mason87ef2bb2008-10-30 11:23:27 -0400455
Chris Masond3977122009-01-05 21:25:51 -0500456 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -0400457 old_root_bytenr = btrfs_root_bytenr(&root->root_item);
458 if (old_root_bytenr == root->node->start)
459 break;
460 btrfs_set_root_bytenr(&root->root_item,
461 root->node->start);
462 btrfs_set_root_level(&root->root_item,
463 btrfs_header_level(root->node));
Yan Zheng84234f32008-10-29 14:49:05 -0400464 btrfs_set_root_generation(&root->root_item, trans->transid);
Chris Mason87ef2bb2008-10-30 11:23:27 -0400465
Chris Mason0b86a832008-03-24 15:01:56 -0400466 ret = btrfs_update_root(trans, tree_root,
467 &root->root_key,
468 &root->root_item);
469 BUG_ON(ret);
470 btrfs_write_dirty_block_groups(trans, root);
Chris Mason56bec292009-03-13 10:10:06 -0400471
472 ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
473 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -0400474 }
475 return 0;
476}
477
Chris Masond352ac62008-09-29 15:18:18 -0400478/*
479 * update all the cowonly tree roots on disk
480 */
Chris Mason79154b12007-03-22 15:59:16 -0400481int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans,
482 struct btrfs_root *root)
483{
Chris Mason79154b12007-03-22 15:59:16 -0400484 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason0b86a832008-03-24 15:01:56 -0400485 struct list_head *next;
Yan Zheng84234f32008-10-29 14:49:05 -0400486 struct extent_buffer *eb;
Chris Mason56bec292009-03-13 10:10:06 -0400487 int ret;
Yan Zheng84234f32008-10-29 14:49:05 -0400488
Chris Mason56bec292009-03-13 10:10:06 -0400489 ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
490 BUG_ON(ret);
Chris Mason87ef2bb2008-10-30 11:23:27 -0400491
Yan Zheng84234f32008-10-29 14:49:05 -0400492 eb = btrfs_lock_root_node(fs_info->tree_root);
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400493 btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 0, &eb);
Yan Zheng84234f32008-10-29 14:49:05 -0400494 btrfs_tree_unlock(eb);
495 free_extent_buffer(eb);
Chris Mason79154b12007-03-22 15:59:16 -0400496
Chris Mason56bec292009-03-13 10:10:06 -0400497 ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
498 BUG_ON(ret);
Chris Mason87ef2bb2008-10-30 11:23:27 -0400499
Chris Masond3977122009-01-05 21:25:51 -0500500 while (!list_empty(&fs_info->dirty_cowonly_roots)) {
Chris Mason0b86a832008-03-24 15:01:56 -0400501 next = fs_info->dirty_cowonly_roots.next;
502 list_del_init(next);
503 root = list_entry(next, struct btrfs_root, dirty_list);
Chris Mason87ef2bb2008-10-30 11:23:27 -0400504
Chris Mason0b86a832008-03-24 15:01:56 -0400505 update_cowonly_root(trans, root);
Chris Mason56bec292009-03-13 10:10:06 -0400506
507 ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
508 BUG_ON(ret);
Chris Mason79154b12007-03-22 15:59:16 -0400509 }
510 return 0;
511}
512
Chris Masond352ac62008-09-29 15:18:18 -0400513/*
514 * dead roots are old snapshots that need to be deleted. This allocates
515 * a dirty root struct and adds it into the list of dead roots that need to
516 * be deleted
517 */
Yan Zhengb48652c2008-08-04 23:23:47 -0400518int btrfs_add_dead_root(struct btrfs_root *root, struct btrfs_root *latest)
Chris Mason5eda7b52007-06-22 14:16:25 -0400519{
Yan Zhengf321e492008-07-30 09:26:11 -0400520 struct btrfs_dirty_root *dirty;
Chris Mason5eda7b52007-06-22 14:16:25 -0400521
522 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
523 if (!dirty)
524 return -ENOMEM;
Chris Mason5eda7b52007-06-22 14:16:25 -0400525 dirty->root = root;
Chris Mason5ce14bb2007-09-11 11:15:39 -0400526 dirty->latest_root = latest;
Yan Zhengb48652c2008-08-04 23:23:47 -0400527
528 mutex_lock(&root->fs_info->trans_mutex);
529 list_add(&dirty->list, &latest->fs_info->dead_roots);
530 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason5eda7b52007-06-22 14:16:25 -0400531 return 0;
532}
533
Chris Masond352ac62008-09-29 15:18:18 -0400534/*
535 * at transaction commit time we need to schedule the old roots for
536 * deletion via btrfs_drop_snapshot. This runs through all the
537 * reference counted roots that were modified in the current
538 * transaction and puts them into the drop list
539 */
Chris Mason80b67942008-02-01 16:35:04 -0500540static noinline int add_dirty_roots(struct btrfs_trans_handle *trans,
541 struct radix_tree_root *radix,
542 struct list_head *list)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400543{
Yan Zhengf321e492008-07-30 09:26:11 -0400544 struct btrfs_dirty_root *dirty;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400545 struct btrfs_root *gang[8];
546 struct btrfs_root *root;
547 int i;
548 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400549 int err = 0;
Chris Mason5eda7b52007-06-22 14:16:25 -0400550 u32 refs;
Chris Mason54aa1f42007-06-22 14:16:25 -0400551
Chris Masond3977122009-01-05 21:25:51 -0500552 while (1) {
Chris Mason0f7d52f2007-04-09 10:42:37 -0400553 ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0,
554 ARRAY_SIZE(gang),
555 BTRFS_ROOT_TRANS_TAG);
556 if (ret == 0)
557 break;
558 for (i = 0; i < ret; i++) {
559 root = gang[i];
Chris Mason2619ba12007-04-10 16:58:11 -0400560 radix_tree_tag_clear(radix,
561 (unsigned long)root->root_key.objectid,
562 BTRFS_ROOT_TRANS_TAG);
Yan Zheng31153d82008-07-28 15:32:19 -0400563
564 BUG_ON(!root->ref_tree);
Chris Mason017e5362008-07-28 15:32:51 -0400565 dirty = root->dirty_root;
Yan Zheng31153d82008-07-28 15:32:19 -0400566
Chris Masone02119d2008-09-05 16:13:11 -0400567 btrfs_free_log(trans, root);
Yan Zhengf82d02d2008-10-29 14:49:05 -0400568 btrfs_free_reloc_root(trans, root);
Chris Masone02119d2008-09-05 16:13:11 -0400569
Chris Mason0f7d52f2007-04-09 10:42:37 -0400570 if (root->commit_root == root->node) {
Chris Masondb945352007-10-15 16:15:53 -0400571 WARN_ON(root->node->start !=
572 btrfs_root_bytenr(&root->root_item));
Yan Zheng31153d82008-07-28 15:32:19 -0400573
Chris Mason5f39d392007-10-15 16:14:19 -0400574 free_extent_buffer(root->commit_root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400575 root->commit_root = NULL;
Yan Zheng7ea394f2008-08-05 13:05:02 -0400576 root->dirty_root = NULL;
Yanbcc63ab2008-07-30 16:29:20 -0400577
578 spin_lock(&root->list_lock);
579 list_del_init(&dirty->root->dead_list);
580 spin_unlock(&root->list_lock);
581
Yan Zheng31153d82008-07-28 15:32:19 -0400582 kfree(dirty->root);
583 kfree(dirty);
Josef Bacik58176a92007-08-29 15:47:34 -0400584
585 /* make sure to update the root on disk
586 * so we get any updates to the block used
587 * counts
588 */
589 err = btrfs_update_root(trans,
590 root->fs_info->tree_root,
591 &root->root_key,
592 &root->root_item);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400593 continue;
594 }
Chris Mason9f3a7422007-08-07 15:52:19 -0400595
596 memset(&root->root_item.drop_progress, 0,
597 sizeof(struct btrfs_disk_key));
598 root->root_item.drop_level = 0;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400599 root->commit_root = NULL;
Yan Zheng7ea394f2008-08-05 13:05:02 -0400600 root->dirty_root = NULL;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400601 root->root_key.offset = root->fs_info->generation;
Chris Masondb945352007-10-15 16:15:53 -0400602 btrfs_set_root_bytenr(&root->root_item,
603 root->node->start);
604 btrfs_set_root_level(&root->root_item,
605 btrfs_header_level(root->node));
Yan Zheng84234f32008-10-29 14:49:05 -0400606 btrfs_set_root_generation(&root->root_item,
607 root->root_key.offset);
608
Chris Mason0f7d52f2007-04-09 10:42:37 -0400609 err = btrfs_insert_root(trans, root->fs_info->tree_root,
610 &root->root_key,
611 &root->root_item);
Chris Mason54aa1f42007-06-22 14:16:25 -0400612 if (err)
613 break;
Chris Mason9f3a7422007-08-07 15:52:19 -0400614
615 refs = btrfs_root_refs(&dirty->root->root_item);
616 btrfs_set_root_refs(&dirty->root->root_item, refs - 1);
Chris Mason5eda7b52007-06-22 14:16:25 -0400617 err = btrfs_update_root(trans, root->fs_info->tree_root,
Chris Mason9f3a7422007-08-07 15:52:19 -0400618 &dirty->root->root_key,
619 &dirty->root->root_item);
Chris Mason5eda7b52007-06-22 14:16:25 -0400620
621 BUG_ON(err);
Chris Mason9f3a7422007-08-07 15:52:19 -0400622 if (refs == 1) {
Chris Mason5eda7b52007-06-22 14:16:25 -0400623 list_add(&dirty->list, list);
Chris Mason9f3a7422007-08-07 15:52:19 -0400624 } else {
625 WARN_ON(1);
Yan Zheng31153d82008-07-28 15:32:19 -0400626 free_extent_buffer(dirty->root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -0400627 kfree(dirty->root);
Chris Mason5eda7b52007-06-22 14:16:25 -0400628 kfree(dirty);
Chris Mason9f3a7422007-08-07 15:52:19 -0400629 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400630 }
631 }
Chris Mason54aa1f42007-06-22 14:16:25 -0400632 return err;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400633}
634
Chris Masond352ac62008-09-29 15:18:18 -0400635/*
636 * defrag a given btree. If cacheonly == 1, this won't read from the disk,
637 * otherwise every leaf in the btree is read and defragged.
638 */
Chris Masone9d0b132007-08-10 14:06:19 -0400639int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
640{
641 struct btrfs_fs_info *info = root->fs_info;
642 int ret;
643 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400644 unsigned long nr;
Chris Masone9d0b132007-08-10 14:06:19 -0400645
Chris Masona2135012008-06-25 16:01:30 -0400646 smp_mb();
Chris Masone9d0b132007-08-10 14:06:19 -0400647 if (root->defrag_running)
648 return 0;
Chris Masone9d0b132007-08-10 14:06:19 -0400649 trans = btrfs_start_transaction(root, 1);
Chris Mason6b800532007-10-15 16:17:34 -0400650 while (1) {
Chris Masone9d0b132007-08-10 14:06:19 -0400651 root->defrag_running = 1;
652 ret = btrfs_defrag_leaves(trans, root, cacheonly);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400653 nr = trans->blocks_used;
Chris Masone9d0b132007-08-10 14:06:19 -0400654 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400655 btrfs_btree_balance_dirty(info->tree_root, nr);
Chris Masone9d0b132007-08-10 14:06:19 -0400656 cond_resched();
657
Chris Masone9d0b132007-08-10 14:06:19 -0400658 trans = btrfs_start_transaction(root, 1);
Chris Mason3f157a22008-06-25 16:01:31 -0400659 if (root->fs_info->closing || ret != -EAGAIN)
Chris Masone9d0b132007-08-10 14:06:19 -0400660 break;
661 }
662 root->defrag_running = 0;
Chris Masona2135012008-06-25 16:01:30 -0400663 smp_mb();
Chris Masone9d0b132007-08-10 14:06:19 -0400664 btrfs_end_transaction(trans, root);
665 return 0;
666}
667
Chris Masond352ac62008-09-29 15:18:18 -0400668/*
669 * Given a list of roots that need to be deleted, call btrfs_drop_snapshot on
670 * all of them
671 */
Chris Mason80b67942008-02-01 16:35:04 -0500672static noinline int drop_dirty_roots(struct btrfs_root *tree_root,
673 struct list_head *list)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400674{
Yan Zhengf321e492008-07-30 09:26:11 -0400675 struct btrfs_dirty_root *dirty;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400676 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400677 unsigned long nr;
Chris Masondb945352007-10-15 16:15:53 -0400678 u64 num_bytes;
679 u64 bytes_used;
Yanbcc63ab2008-07-30 16:29:20 -0400680 u64 max_useless;
Chris Mason54aa1f42007-06-22 14:16:25 -0400681 int ret = 0;
Chris Mason9f3a7422007-08-07 15:52:19 -0400682 int err;
683
Chris Masond3977122009-01-05 21:25:51 -0500684 while (!list_empty(list)) {
Josef Bacik58176a92007-08-29 15:47:34 -0400685 struct btrfs_root *root;
686
Yan Zhengf321e492008-07-30 09:26:11 -0400687 dirty = list_entry(list->prev, struct btrfs_dirty_root, list);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400688 list_del_init(&dirty->list);
Chris Mason5eda7b52007-06-22 14:16:25 -0400689
Chris Masondb945352007-10-15 16:15:53 -0400690 num_bytes = btrfs_root_used(&dirty->root->root_item);
Josef Bacik58176a92007-08-29 15:47:34 -0400691 root = dirty->latest_root;
Chris Masona2135012008-06-25 16:01:30 -0400692 atomic_inc(&root->fs_info->throttles);
Josef Bacik58176a92007-08-29 15:47:34 -0400693
Chris Masond3977122009-01-05 21:25:51 -0500694 while (1) {
Chris Mason9f3a7422007-08-07 15:52:19 -0400695 trans = btrfs_start_transaction(tree_root, 1);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400696 mutex_lock(&root->fs_info->drop_mutex);
Chris Mason9f3a7422007-08-07 15:52:19 -0400697 ret = btrfs_drop_snapshot(trans, dirty->root);
Chris Masond3977122009-01-05 21:25:51 -0500698 if (ret != -EAGAIN)
Chris Mason9f3a7422007-08-07 15:52:19 -0400699 break;
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400700 mutex_unlock(&root->fs_info->drop_mutex);
Josef Bacik58176a92007-08-29 15:47:34 -0400701
Chris Mason9f3a7422007-08-07 15:52:19 -0400702 err = btrfs_update_root(trans,
703 tree_root,
704 &dirty->root->root_key,
705 &dirty->root->root_item);
706 if (err)
707 ret = err;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400708 nr = trans->blocks_used;
Chris Mason017e5362008-07-28 15:32:51 -0400709 ret = btrfs_end_transaction(trans, tree_root);
Chris Mason9f3a7422007-08-07 15:52:19 -0400710 BUG_ON(ret);
Chris Masona2135012008-06-25 16:01:30 -0400711
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400712 btrfs_btree_balance_dirty(tree_root, nr);
Chris Mason4dc119042007-10-15 16:18:14 -0400713 cond_resched();
Chris Mason9f3a7422007-08-07 15:52:19 -0400714 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400715 BUG_ON(ret);
Chris Masona2135012008-06-25 16:01:30 -0400716 atomic_dec(&root->fs_info->throttles);
Chris Mason017e5362008-07-28 15:32:51 -0400717 wake_up(&root->fs_info->transaction_throttle);
Josef Bacik58176a92007-08-29 15:47:34 -0400718
Chris Masondb945352007-10-15 16:15:53 -0400719 num_bytes -= btrfs_root_used(&dirty->root->root_item);
720 bytes_used = btrfs_root_used(&root->root_item);
721 if (num_bytes) {
Yan Zheng24562422009-02-12 14:14:53 -0500722 mutex_lock(&root->fs_info->trans_mutex);
Chris Masone02119d2008-09-05 16:13:11 -0400723 btrfs_record_root_in_trans(root);
Yan Zheng24562422009-02-12 14:14:53 -0500724 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason5f39d392007-10-15 16:14:19 -0400725 btrfs_set_root_used(&root->root_item,
Chris Masondb945352007-10-15 16:15:53 -0400726 bytes_used - num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -0400727 }
Chris Masona2135012008-06-25 16:01:30 -0400728
Chris Mason9f3a7422007-08-07 15:52:19 -0400729 ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key);
Josef Bacik58176a92007-08-29 15:47:34 -0400730 if (ret) {
731 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -0400732 break;
Josef Bacik58176a92007-08-29 15:47:34 -0400733 }
Chris Masona2135012008-06-25 16:01:30 -0400734 mutex_unlock(&root->fs_info->drop_mutex);
735
Yanbcc63ab2008-07-30 16:29:20 -0400736 spin_lock(&root->list_lock);
737 list_del_init(&dirty->root->dead_list);
738 if (!list_empty(&root->dead_list)) {
739 struct btrfs_root *oldest;
740 oldest = list_entry(root->dead_list.prev,
741 struct btrfs_root, dead_list);
742 max_useless = oldest->root_key.offset - 1;
743 } else {
744 max_useless = root->root_key.offset - 1;
745 }
746 spin_unlock(&root->list_lock);
747
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400748 nr = trans->blocks_used;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400749 ret = btrfs_end_transaction(trans, tree_root);
750 BUG_ON(ret);
Chris Mason5eda7b52007-06-22 14:16:25 -0400751
Zheng Yane4657682008-09-26 10:04:53 -0400752 ret = btrfs_remove_leaf_refs(root, max_useless, 0);
Yanbcc63ab2008-07-30 16:29:20 -0400753 BUG_ON(ret);
754
Chris Masonf510cfe2007-10-15 16:14:48 -0400755 free_extent_buffer(dirty->root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -0400756 kfree(dirty->root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400757 kfree(dirty);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400758
759 btrfs_btree_balance_dirty(tree_root, nr);
Chris Mason4dc119042007-10-15 16:18:14 -0400760 cond_resched();
Chris Mason0f7d52f2007-04-09 10:42:37 -0400761 }
Chris Mason54aa1f42007-06-22 14:16:25 -0400762 return ret;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400763}
764
Chris Masond352ac62008-09-29 15:18:18 -0400765/*
766 * new snapshots need to be created at a very specific time in the
767 * transaction commit. This does the actual creation
768 */
Chris Mason80b67942008-02-01 16:35:04 -0500769static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
Chris Mason3063d292008-01-08 15:46:30 -0500770 struct btrfs_fs_info *fs_info,
771 struct btrfs_pending_snapshot *pending)
772{
773 struct btrfs_key key;
Chris Mason80b67942008-02-01 16:35:04 -0500774 struct btrfs_root_item *new_root_item;
Chris Mason3063d292008-01-08 15:46:30 -0500775 struct btrfs_root *tree_root = fs_info->tree_root;
776 struct btrfs_root *root = pending->root;
777 struct extent_buffer *tmp;
Chris Mason925baed2008-06-25 16:01:30 -0400778 struct extent_buffer *old;
Chris Mason3063d292008-01-08 15:46:30 -0500779 int ret;
780 u64 objectid;
781
Chris Mason80b67942008-02-01 16:35:04 -0500782 new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
783 if (!new_root_item) {
784 ret = -ENOMEM;
785 goto fail;
786 }
Chris Mason3063d292008-01-08 15:46:30 -0500787 ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid);
788 if (ret)
789 goto fail;
790
Yan Zheng80ff3852008-10-30 14:20:02 -0400791 btrfs_record_root_in_trans(root);
792 btrfs_set_root_last_snapshot(&root->root_item, trans->transid);
Chris Mason80b67942008-02-01 16:35:04 -0500793 memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
Chris Mason3063d292008-01-08 15:46:30 -0500794
795 key.objectid = objectid;
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400796 key.offset = trans->transid;
Chris Mason3063d292008-01-08 15:46:30 -0500797 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
798
Chris Mason925baed2008-06-25 16:01:30 -0400799 old = btrfs_lock_root_node(root);
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400800 btrfs_cow_block(trans, root, old, NULL, 0, &old);
Chris Mason3063d292008-01-08 15:46:30 -0500801
Chris Mason925baed2008-06-25 16:01:30 -0400802 btrfs_copy_root(trans, root, old, &tmp, objectid);
803 btrfs_tree_unlock(old);
804 free_extent_buffer(old);
Chris Mason3063d292008-01-08 15:46:30 -0500805
Chris Mason80b67942008-02-01 16:35:04 -0500806 btrfs_set_root_bytenr(new_root_item, tmp->start);
807 btrfs_set_root_level(new_root_item, btrfs_header_level(tmp));
Yan Zheng84234f32008-10-29 14:49:05 -0400808 btrfs_set_root_generation(new_root_item, trans->transid);
Chris Mason3063d292008-01-08 15:46:30 -0500809 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
Chris Mason80b67942008-02-01 16:35:04 -0500810 new_root_item);
Chris Mason925baed2008-06-25 16:01:30 -0400811 btrfs_tree_unlock(tmp);
Chris Mason3063d292008-01-08 15:46:30 -0500812 free_extent_buffer(tmp);
813 if (ret)
814 goto fail;
815
Chris Mason3de45862008-11-17 21:02:50 -0500816 key.offset = (u64)-1;
817 memcpy(&pending->root_key, &key, sizeof(key));
818fail:
819 kfree(new_root_item);
820 return ret;
821}
822
823static noinline int finish_pending_snapshot(struct btrfs_fs_info *fs_info,
824 struct btrfs_pending_snapshot *pending)
825{
826 int ret;
827 int namelen;
828 u64 index = 0;
829 struct btrfs_trans_handle *trans;
830 struct inode *parent_inode;
831 struct inode *inode;
Chris Mason0660b5a2008-11-17 20:37:39 -0500832 struct btrfs_root *parent_root;
Chris Mason3de45862008-11-17 21:02:50 -0500833
Chris Mason3394e162008-11-17 20:42:26 -0500834 parent_inode = pending->dentry->d_parent->d_inode;
Chris Mason0660b5a2008-11-17 20:37:39 -0500835 parent_root = BTRFS_I(parent_inode)->root;
Yan Zheng180591b2009-01-06 09:58:06 -0500836 trans = btrfs_join_transaction(parent_root, 1);
Chris Mason3de45862008-11-17 21:02:50 -0500837
Chris Mason3063d292008-01-08 15:46:30 -0500838 /*
839 * insert the directory item
840 */
Sven Wegener3b963622008-06-09 21:57:42 -0400841 namelen = strlen(pending->name);
Chris Mason3de45862008-11-17 21:02:50 -0500842 ret = btrfs_set_inode_index(parent_inode, &index);
Chris Mason0660b5a2008-11-17 20:37:39 -0500843 ret = btrfs_insert_dir_item(trans, parent_root,
Chris Mason3de45862008-11-17 21:02:50 -0500844 pending->name, namelen,
845 parent_inode->i_ino,
846 &pending->root_key, BTRFS_FT_DIR, index);
Chris Mason3063d292008-01-08 15:46:30 -0500847
848 if (ret)
849 goto fail;
Chris Mason0660b5a2008-11-17 20:37:39 -0500850
Yan Zheng52c26172009-01-05 15:43:43 -0500851 btrfs_i_size_write(parent_inode, parent_inode->i_size + namelen * 2);
852 ret = btrfs_update_inode(trans, parent_root, parent_inode);
853 BUG_ON(ret);
854
Chris Mason0660b5a2008-11-17 20:37:39 -0500855 /* add the backref first */
856 ret = btrfs_add_root_ref(trans, parent_root->fs_info->tree_root,
857 pending->root_key.objectid,
858 BTRFS_ROOT_BACKREF_KEY,
859 parent_root->root_key.objectid,
860 parent_inode->i_ino, index, pending->name,
861 namelen);
862
863 BUG_ON(ret);
864
865 /* now add the forward ref */
866 ret = btrfs_add_root_ref(trans, parent_root->fs_info->tree_root,
867 parent_root->root_key.objectid,
868 BTRFS_ROOT_REF_KEY,
869 pending->root_key.objectid,
870 parent_inode->i_ino, index, pending->name,
871 namelen);
872
Chris Mason3de45862008-11-17 21:02:50 -0500873 inode = btrfs_lookup_dentry(parent_inode, pending->dentry);
874 d_instantiate(pending->dentry, inode);
Chris Mason3063d292008-01-08 15:46:30 -0500875fail:
Chris Mason3de45862008-11-17 21:02:50 -0500876 btrfs_end_transaction(trans, fs_info->fs_root);
Chris Mason3063d292008-01-08 15:46:30 -0500877 return ret;
878}
879
Chris Masond352ac62008-09-29 15:18:18 -0400880/*
881 * create all the snapshots we've scheduled for creation
882 */
Chris Mason80b67942008-02-01 16:35:04 -0500883static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
884 struct btrfs_fs_info *fs_info)
Chris Mason3063d292008-01-08 15:46:30 -0500885{
886 struct btrfs_pending_snapshot *pending;
887 struct list_head *head = &trans->transaction->pending_snapshots;
Chris Mason3de45862008-11-17 21:02:50 -0500888 int ret;
889
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500890 list_for_each_entry(pending, head, list) {
Chris Mason3de45862008-11-17 21:02:50 -0500891 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
Chris Masond3977122009-01-05 21:25:51 -0500904 while (!list_empty(head)) {
Chris Mason3063d292008-01-08 15:46:30 -0500905 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 Mason56bec292009-03-13 10:10:06 -0400929 /* make a pass through all the delayed refs we have so far
930 * any runnings procs may add more while we are here
931 */
932 ret = btrfs_run_delayed_refs(trans, root, 0);
933 BUG_ON(ret);
934
935 /*
936 * set the flushing flag so procs in this transaction have to
937 * start sending their work down.
938 */
939 trans->transaction->delayed_refs.flushing = 1;
940
Chris Masonc3e69d52009-03-13 10:17:05 -0400941 ret = btrfs_run_delayed_refs(trans, root, 0);
Chris Mason56bec292009-03-13 10:10:06 -0400942 BUG_ON(ret);
943
Chris Mason0f7d52f2007-04-09 10:42:37 -0400944 INIT_LIST_HEAD(&dirty_fs_roots);
Chris Mason79154b12007-03-22 15:59:16 -0400945 mutex_lock(&root->fs_info->trans_mutex);
946 if (trans->transaction->in_commit) {
947 cur_trans = trans->transaction;
948 trans->transaction->use_count++;
Chris Masonccd467d2007-06-28 15:57:36 -0400949 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400950 btrfs_end_transaction(trans, root);
Chris Masonccd467d2007-06-28 15:57:36 -0400951
Chris Mason79154b12007-03-22 15:59:16 -0400952 ret = wait_for_commit(root, cur_trans);
953 BUG_ON(ret);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400954
955 mutex_lock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400956 put_transaction(cur_trans);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400957 mutex_unlock(&root->fs_info->trans_mutex);
958
Chris Mason79154b12007-03-22 15:59:16 -0400959 return 0;
960 }
Chris Mason4313b392008-01-03 09:08:48 -0500961
962 pinned_copy = kmalloc(sizeof(*pinned_copy), GFP_NOFS);
963 if (!pinned_copy)
964 return -ENOMEM;
965
Chris Masond1310b22008-01-24 16:13:08 -0500966 extent_io_tree_init(pinned_copy,
Chris Mason4313b392008-01-03 09:08:48 -0500967 root->fs_info->btree_inode->i_mapping, GFP_NOFS);
968
Chris Mason2c90e5d2007-04-02 10:50:19 -0400969 trans->transaction->in_commit = 1;
Chris Masonf9295742008-07-17 12:54:14 -0400970 trans->transaction->blocked = 1;
Chris Masonccd467d2007-06-28 15:57:36 -0400971 cur_trans = trans->transaction;
972 if (cur_trans->list.prev != &root->fs_info->trans_list) {
973 prev_trans = list_entry(cur_trans->list.prev,
974 struct btrfs_transaction, list);
975 if (!prev_trans->commit_done) {
976 prev_trans->use_count++;
Chris Masonccd467d2007-06-28 15:57:36 -0400977 mutex_unlock(&root->fs_info->trans_mutex);
978
979 wait_for_commit(root, prev_trans);
Chris Masonccd467d2007-06-28 15:57:36 -0400980
Chris Masonccd467d2007-06-28 15:57:36 -0400981 mutex_lock(&root->fs_info->trans_mutex);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400982 put_transaction(prev_trans);
Chris Masonccd467d2007-06-28 15:57:36 -0400983 }
984 }
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400985
986 do {
Yan Zheng7ea394f2008-08-05 13:05:02 -0400987 int snap_pending = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400988 joined = cur_trans->num_joined;
Yan Zheng7ea394f2008-08-05 13:05:02 -0400989 if (!list_empty(&trans->transaction->pending_snapshots))
990 snap_pending = 1;
991
Chris Mason2c90e5d2007-04-02 10:50:19 -0400992 WARN_ON(cur_trans != trans->transaction);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400993 prepare_to_wait(&cur_trans->writer_wait, &wait,
Chris Mason79154b12007-03-22 15:59:16 -0400994 TASK_UNINTERRUPTIBLE);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400995
996 if (cur_trans->num_writers > 1)
997 timeout = MAX_SCHEDULE_TIMEOUT;
998 else
999 timeout = 1;
1000
Chris Mason79154b12007-03-22 15:59:16 -04001001 mutex_unlock(&root->fs_info->trans_mutex);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001002
Yan Zheng7ea394f2008-08-05 13:05:02 -04001003 if (snap_pending) {
1004 ret = btrfs_wait_ordered_extents(root, 1);
1005 BUG_ON(ret);
1006 }
1007
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001008 schedule_timeout(timeout);
1009
Chris Mason79154b12007-03-22 15:59:16 -04001010 mutex_lock(&root->fs_info->trans_mutex);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001011 finish_wait(&cur_trans->writer_wait, &wait);
1012 } while (cur_trans->num_writers > 1 ||
1013 (cur_trans->num_joined != joined));
1014
Chris Mason3063d292008-01-08 15:46:30 -05001015 ret = create_pending_snapshots(trans, root->fs_info);
1016 BUG_ON(ret);
1017
Chris Mason56bec292009-03-13 10:10:06 -04001018 ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
1019 BUG_ON(ret);
1020
Chris Mason2c90e5d2007-04-02 10:50:19 -04001021 WARN_ON(cur_trans != trans->transaction);
Chris Masondc17ff82008-01-08 15:46:30 -05001022
Chris Masone02119d2008-09-05 16:13:11 -04001023 /* btrfs_commit_tree_roots is responsible for getting the
1024 * various roots consistent with each other. Every pointer
1025 * in the tree of tree roots has to point to the most up to date
1026 * root for every subvolume and other tree. So, we have to keep
1027 * the tree logging code from jumping in and changing any
1028 * of the trees.
1029 *
1030 * At this point in the commit, there can't be any tree-log
1031 * writers, but a little lower down we drop the trans mutex
1032 * and let new people in. By holding the tree_log_mutex
1033 * from now until after the super is written, we avoid races
1034 * with the tree-log code.
1035 */
1036 mutex_lock(&root->fs_info->tree_log_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04001037 /*
1038 * keep tree reloc code from adding new reloc trees
1039 */
1040 mutex_lock(&root->fs_info->tree_reloc_mutex);
1041
Chris Masone02119d2008-09-05 16:13:11 -04001042
Chris Mason54aa1f42007-06-22 14:16:25 -04001043 ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix,
1044 &dirty_fs_roots);
1045 BUG_ON(ret);
1046
Chris Masone02119d2008-09-05 16:13:11 -04001047 /* add_dirty_roots gets rid of all the tree log roots, it is now
1048 * safe to free the root of tree log roots
1049 */
1050 btrfs_free_log_root_tree(trans, root->fs_info);
1051
Chris Mason79154b12007-03-22 15:59:16 -04001052 ret = btrfs_commit_tree_roots(trans, root);
1053 BUG_ON(ret);
Chris Mason54aa1f42007-06-22 14:16:25 -04001054
Chris Mason78fae272007-03-25 11:35:08 -04001055 cur_trans = root->fs_info->running_transaction;
Chris Masoncee36a02008-01-15 08:40:48 -05001056 spin_lock(&root->fs_info->new_trans_lock);
Chris Mason78fae272007-03-25 11:35:08 -04001057 root->fs_info->running_transaction = NULL;
Chris Masoncee36a02008-01-15 08:40:48 -05001058 spin_unlock(&root->fs_info->new_trans_lock);
Chris Mason4b52dff2007-06-26 10:06:50 -04001059 btrfs_set_super_generation(&root->fs_info->super_copy,
1060 cur_trans->transid);
1061 btrfs_set_super_root(&root->fs_info->super_copy,
Chris Masondb945352007-10-15 16:15:53 -04001062 root->fs_info->tree_root->node->start);
1063 btrfs_set_super_root_level(&root->fs_info->super_copy,
1064 btrfs_header_level(root->fs_info->tree_root->node));
Chris Mason5f39d392007-10-15 16:14:19 -04001065
Chris Mason0b86a832008-03-24 15:01:56 -04001066 btrfs_set_super_chunk_root(&root->fs_info->super_copy,
1067 chunk_root->node->start);
1068 btrfs_set_super_chunk_root_level(&root->fs_info->super_copy,
1069 btrfs_header_level(chunk_root->node));
Yan Zheng84234f32008-10-29 14:49:05 -04001070 btrfs_set_super_chunk_root_generation(&root->fs_info->super_copy,
1071 btrfs_header_generation(chunk_root->node));
Chris Masone02119d2008-09-05 16:13:11 -04001072
1073 if (!root->fs_info->log_root_recovering) {
1074 btrfs_set_super_log_root(&root->fs_info->super_copy, 0);
1075 btrfs_set_super_log_root_level(&root->fs_info->super_copy, 0);
1076 }
1077
Chris Masona061fc82008-05-07 11:43:44 -04001078 memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy,
1079 sizeof(root->fs_info->super_copy));
Chris Masonccd467d2007-06-28 15:57:36 -04001080
Chris Mason4313b392008-01-03 09:08:48 -05001081 btrfs_copy_pinned(root, pinned_copy);
Chris Masonccd467d2007-06-28 15:57:36 -04001082
Chris Masonf9295742008-07-17 12:54:14 -04001083 trans->transaction->blocked = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001084 wake_up(&root->fs_info->transaction_throttle);
Chris Masonf9295742008-07-17 12:54:14 -04001085 wake_up(&root->fs_info->transaction_wait);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001086
Chris Mason78fae272007-03-25 11:35:08 -04001087 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -04001088 ret = btrfs_write_and_wait_transaction(trans, root);
1089 BUG_ON(ret);
Yan Zhenga512bbf2008-12-08 16:46:26 -05001090 write_ctree_super(trans, root, 0);
Chris Mason4313b392008-01-03 09:08:48 -05001091
Chris Masone02119d2008-09-05 16:13:11 -04001092 /*
1093 * the super is written, we can safely allow the tree-loggers
1094 * to go about their business
1095 */
1096 mutex_unlock(&root->fs_info->tree_log_mutex);
1097
Chris Mason4313b392008-01-03 09:08:48 -05001098 btrfs_finish_extent_commit(trans, root, pinned_copy);
Chris Mason4313b392008-01-03 09:08:48 -05001099 kfree(pinned_copy);
1100
Zheng Yan1a40e232008-09-26 10:09:34 -04001101 btrfs_drop_dead_reloc_roots(root);
1102 mutex_unlock(&root->fs_info->tree_reloc_mutex);
1103
Chris Mason3de45862008-11-17 21:02:50 -05001104 /* do the directory inserts of any pending snapshot creations */
1105 finish_pending_snapshots(trans, root->fs_info);
1106
Zheng Yan1a40e232008-09-26 10:09:34 -04001107 mutex_lock(&root->fs_info->trans_mutex);
1108
Chris Mason2c90e5d2007-04-02 10:50:19 -04001109 cur_trans->commit_done = 1;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001110 root->fs_info->last_trans_committed = cur_trans->transid;
Chris Mason2c90e5d2007-04-02 10:50:19 -04001111 wake_up(&cur_trans->commit_wait);
Chris Mason3de45862008-11-17 21:02:50 -05001112
Chris Mason79154b12007-03-22 15:59:16 -04001113 put_transaction(cur_trans);
Chris Mason78fae272007-03-25 11:35:08 -04001114 put_transaction(cur_trans);
Josef Bacik58176a92007-08-29 15:47:34 -04001115
Yanbcc63ab2008-07-30 16:29:20 -04001116 list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots);
Chris Masonfacda1e2007-06-08 18:11:48 -04001117 if (root->fs_info->closing)
1118 list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots);
Josef Bacik58176a92007-08-29 15:47:34 -04001119
Chris Mason78fae272007-03-25 11:35:08 -04001120 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason3de45862008-11-17 21:02:50 -05001121
Chris Mason2c90e5d2007-04-02 10:50:19 -04001122 kmem_cache_free(btrfs_trans_handle_cachep, trans);
Chris Mason79154b12007-03-22 15:59:16 -04001123
Chris Masond3977122009-01-05 21:25:51 -05001124 if (root->fs_info->closing)
Chris Masonfacda1e2007-06-08 18:11:48 -04001125 drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);
Chris Mason79154b12007-03-22 15:59:16 -04001126 return ret;
1127}
1128
Chris Masond352ac62008-09-29 15:18:18 -04001129/*
1130 * interface function to delete all the snapshots we have scheduled for deletion
1131 */
Chris Masone9d0b132007-08-10 14:06:19 -04001132int btrfs_clean_old_snapshots(struct btrfs_root *root)
1133{
1134 struct list_head dirty_roots;
1135 INIT_LIST_HEAD(&dirty_roots);
Chris Masona74a4b92008-06-25 16:01:31 -04001136again:
Chris Masone9d0b132007-08-10 14:06:19 -04001137 mutex_lock(&root->fs_info->trans_mutex);
1138 list_splice_init(&root->fs_info->dead_roots, &dirty_roots);
1139 mutex_unlock(&root->fs_info->trans_mutex);
1140
1141 if (!list_empty(&dirty_roots)) {
1142 drop_dirty_roots(root, &dirty_roots);
Chris Masona74a4b92008-06-25 16:01:31 -04001143 goto again;
Chris Masone9d0b132007-08-10 14:06:19 -04001144 }
1145 return 0;
1146}